BitcodeWriter.cpp 130 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408
  1. //===--- Bitcode/Writer/BitcodeWriter.cpp - Bitcode Writer ----------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // Bitcode writer implementation.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "ValueEnumerator.h"
  14. #include "llvm/ADT/STLExtras.h"
  15. #include "llvm/ADT/Triple.h"
  16. #include "llvm/Analysis/BlockFrequencyInfo.h"
  17. #include "llvm/Analysis/BlockFrequencyInfoImpl.h"
  18. #include "llvm/Analysis/BranchProbabilityInfo.h"
  19. #include "llvm/Analysis/LoopInfo.h"
  20. #include "llvm/Bitcode/BitstreamWriter.h"
  21. #include "llvm/Bitcode/LLVMBitCodes.h"
  22. #include "llvm/Bitcode/ReaderWriter.h"
  23. #include "llvm/IR/CallSite.h"
  24. #include "llvm/IR/Constants.h"
  25. #include "llvm/IR/DebugInfoMetadata.h"
  26. #include "llvm/IR/DerivedTypes.h"
  27. #include "llvm/IR/Dominators.h"
  28. #include "llvm/IR/InlineAsm.h"
  29. #include "llvm/IR/Instructions.h"
  30. #include "llvm/IR/IntrinsicInst.h"
  31. #include "llvm/IR/LLVMContext.h"
  32. #include "llvm/IR/Module.h"
  33. #include "llvm/IR/Operator.h"
  34. #include "llvm/IR/UseListOrder.h"
  35. #include "llvm/IR/ValueSymbolTable.h"
  36. #include "llvm/Support/CommandLine.h"
  37. #include "llvm/Support/ErrorHandling.h"
  38. #include "llvm/Support/MathExtras.h"
  39. #include "llvm/Support/Program.h"
  40. #include "llvm/Support/raw_ostream.h"
  41. #include <cctype>
  42. #include <map>
  43. using namespace llvm;
  44. /// These are manifest constants used by the bitcode writer. They do not need to
  45. /// be kept in sync with the reader, but need to be consistent within this file.
  46. enum {
  47. // VALUE_SYMTAB_BLOCK abbrev id's.
  48. VST_ENTRY_8_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
  49. VST_ENTRY_7_ABBREV,
  50. VST_ENTRY_6_ABBREV,
  51. VST_BBENTRY_6_ABBREV,
  52. // CONSTANTS_BLOCK abbrev id's.
  53. CONSTANTS_SETTYPE_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
  54. CONSTANTS_INTEGER_ABBREV,
  55. CONSTANTS_CE_CAST_Abbrev,
  56. CONSTANTS_NULL_Abbrev,
  57. // FUNCTION_BLOCK abbrev id's.
  58. FUNCTION_INST_LOAD_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
  59. FUNCTION_INST_BINOP_ABBREV,
  60. FUNCTION_INST_BINOP_FLAGS_ABBREV,
  61. FUNCTION_INST_CAST_ABBREV,
  62. FUNCTION_INST_RET_VOID_ABBREV,
  63. FUNCTION_INST_RET_VAL_ABBREV,
  64. FUNCTION_INST_UNREACHABLE_ABBREV,
  65. FUNCTION_INST_GEP_ABBREV,
  66. };
  67. static unsigned GetEncodedCastOpcode(unsigned Opcode) {
  68. switch (Opcode) {
  69. default: llvm_unreachable("Unknown cast instruction!");
  70. case Instruction::Trunc : return bitc::CAST_TRUNC;
  71. case Instruction::ZExt : return bitc::CAST_ZEXT;
  72. case Instruction::SExt : return bitc::CAST_SEXT;
  73. case Instruction::FPToUI : return bitc::CAST_FPTOUI;
  74. case Instruction::FPToSI : return bitc::CAST_FPTOSI;
  75. case Instruction::UIToFP : return bitc::CAST_UITOFP;
  76. case Instruction::SIToFP : return bitc::CAST_SITOFP;
  77. case Instruction::FPTrunc : return bitc::CAST_FPTRUNC;
  78. case Instruction::FPExt : return bitc::CAST_FPEXT;
  79. case Instruction::PtrToInt: return bitc::CAST_PTRTOINT;
  80. case Instruction::IntToPtr: return bitc::CAST_INTTOPTR;
  81. case Instruction::BitCast : return bitc::CAST_BITCAST;
  82. case Instruction::AddrSpaceCast: return bitc::CAST_ADDRSPACECAST;
  83. }
  84. }
  85. static unsigned GetEncodedBinaryOpcode(unsigned Opcode) {
  86. switch (Opcode) {
  87. default: llvm_unreachable("Unknown binary instruction!");
  88. case Instruction::Add:
  89. case Instruction::FAdd: return bitc::BINOP_ADD;
  90. case Instruction::Sub:
  91. case Instruction::FSub: return bitc::BINOP_SUB;
  92. case Instruction::Mul:
  93. case Instruction::FMul: return bitc::BINOP_MUL;
  94. case Instruction::UDiv: return bitc::BINOP_UDIV;
  95. case Instruction::FDiv:
  96. case Instruction::SDiv: return bitc::BINOP_SDIV;
  97. case Instruction::URem: return bitc::BINOP_UREM;
  98. case Instruction::FRem:
  99. case Instruction::SRem: return bitc::BINOP_SREM;
  100. case Instruction::Shl: return bitc::BINOP_SHL;
  101. case Instruction::LShr: return bitc::BINOP_LSHR;
  102. case Instruction::AShr: return bitc::BINOP_ASHR;
  103. case Instruction::And: return bitc::BINOP_AND;
  104. case Instruction::Or: return bitc::BINOP_OR;
  105. case Instruction::Xor: return bitc::BINOP_XOR;
  106. }
  107. }
  108. static unsigned GetEncodedRMWOperation(AtomicRMWInst::BinOp Op) {
  109. switch (Op) {
  110. default: llvm_unreachable("Unknown RMW operation!");
  111. case AtomicRMWInst::Xchg: return bitc::RMW_XCHG;
  112. case AtomicRMWInst::Add: return bitc::RMW_ADD;
  113. case AtomicRMWInst::Sub: return bitc::RMW_SUB;
  114. case AtomicRMWInst::And: return bitc::RMW_AND;
  115. case AtomicRMWInst::Nand: return bitc::RMW_NAND;
  116. case AtomicRMWInst::Or: return bitc::RMW_OR;
  117. case AtomicRMWInst::Xor: return bitc::RMW_XOR;
  118. case AtomicRMWInst::Max: return bitc::RMW_MAX;
  119. case AtomicRMWInst::Min: return bitc::RMW_MIN;
  120. case AtomicRMWInst::UMax: return bitc::RMW_UMAX;
  121. case AtomicRMWInst::UMin: return bitc::RMW_UMIN;
  122. }
  123. }
  124. static unsigned GetEncodedOrdering(AtomicOrdering Ordering) {
  125. switch (Ordering) {
  126. case NotAtomic: return bitc::ORDERING_NOTATOMIC;
  127. case Unordered: return bitc::ORDERING_UNORDERED;
  128. case Monotonic: return bitc::ORDERING_MONOTONIC;
  129. case Acquire: return bitc::ORDERING_ACQUIRE;
  130. case Release: return bitc::ORDERING_RELEASE;
  131. case AcquireRelease: return bitc::ORDERING_ACQREL;
  132. case SequentiallyConsistent: return bitc::ORDERING_SEQCST;
  133. }
  134. llvm_unreachable("Invalid ordering");
  135. }
  136. static unsigned GetEncodedSynchScope(SynchronizationScope SynchScope) {
  137. switch (SynchScope) {
  138. case SingleThread: return bitc::SYNCHSCOPE_SINGLETHREAD;
  139. case CrossThread: return bitc::SYNCHSCOPE_CROSSTHREAD;
  140. }
  141. llvm_unreachable("Invalid synch scope");
  142. }
  143. static void WriteStringRecord(unsigned Code, StringRef Str,
  144. unsigned AbbrevToUse, BitstreamWriter &Stream) {
  145. SmallVector<unsigned, 64> Vals;
  146. // Code: [strchar x N]
  147. for (unsigned i = 0, e = Str.size(); i != e; ++i) {
  148. if (AbbrevToUse && !BitCodeAbbrevOp::isChar6(Str[i]))
  149. AbbrevToUse = 0;
  150. Vals.push_back(Str[i]);
  151. }
  152. // Emit the finished record.
  153. Stream.EmitRecord(Code, Vals, AbbrevToUse);
  154. }
  155. static uint64_t getAttrKindEncoding(Attribute::AttrKind Kind) {
  156. switch (Kind) {
  157. case Attribute::Alignment:
  158. return bitc::ATTR_KIND_ALIGNMENT;
  159. case Attribute::AlwaysInline:
  160. return bitc::ATTR_KIND_ALWAYS_INLINE;
  161. case Attribute::ArgMemOnly:
  162. return bitc::ATTR_KIND_ARGMEMONLY;
  163. case Attribute::Builtin:
  164. return bitc::ATTR_KIND_BUILTIN;
  165. case Attribute::ByVal:
  166. return bitc::ATTR_KIND_BY_VAL;
  167. case Attribute::Convergent:
  168. return bitc::ATTR_KIND_CONVERGENT;
  169. case Attribute::InAlloca:
  170. return bitc::ATTR_KIND_IN_ALLOCA;
  171. case Attribute::Cold:
  172. return bitc::ATTR_KIND_COLD;
  173. case Attribute::InaccessibleMemOnly:
  174. return bitc::ATTR_KIND_INACCESSIBLEMEM_ONLY;
  175. case Attribute::InaccessibleMemOrArgMemOnly:
  176. return bitc::ATTR_KIND_INACCESSIBLEMEM_OR_ARGMEMONLY;
  177. case Attribute::InlineHint:
  178. return bitc::ATTR_KIND_INLINE_HINT;
  179. case Attribute::InReg:
  180. return bitc::ATTR_KIND_IN_REG;
  181. case Attribute::JumpTable:
  182. return bitc::ATTR_KIND_JUMP_TABLE;
  183. case Attribute::MinSize:
  184. return bitc::ATTR_KIND_MIN_SIZE;
  185. case Attribute::Naked:
  186. return bitc::ATTR_KIND_NAKED;
  187. case Attribute::Nest:
  188. return bitc::ATTR_KIND_NEST;
  189. case Attribute::NoAlias:
  190. return bitc::ATTR_KIND_NO_ALIAS;
  191. case Attribute::NoBuiltin:
  192. return bitc::ATTR_KIND_NO_BUILTIN;
  193. case Attribute::NoCapture:
  194. return bitc::ATTR_KIND_NO_CAPTURE;
  195. case Attribute::NoDuplicate:
  196. return bitc::ATTR_KIND_NO_DUPLICATE;
  197. case Attribute::NoImplicitFloat:
  198. return bitc::ATTR_KIND_NO_IMPLICIT_FLOAT;
  199. case Attribute::NoInline:
  200. return bitc::ATTR_KIND_NO_INLINE;
  201. case Attribute::NoRecurse:
  202. return bitc::ATTR_KIND_NO_RECURSE;
  203. case Attribute::NonLazyBind:
  204. return bitc::ATTR_KIND_NON_LAZY_BIND;
  205. case Attribute::NonNull:
  206. return bitc::ATTR_KIND_NON_NULL;
  207. case Attribute::Dereferenceable:
  208. return bitc::ATTR_KIND_DEREFERENCEABLE;
  209. case Attribute::DereferenceableOrNull:
  210. return bitc::ATTR_KIND_DEREFERENCEABLE_OR_NULL;
  211. case Attribute::NoRedZone:
  212. return bitc::ATTR_KIND_NO_RED_ZONE;
  213. case Attribute::NoReturn:
  214. return bitc::ATTR_KIND_NO_RETURN;
  215. case Attribute::NoUnwind:
  216. return bitc::ATTR_KIND_NO_UNWIND;
  217. case Attribute::OptimizeForSize:
  218. return bitc::ATTR_KIND_OPTIMIZE_FOR_SIZE;
  219. case Attribute::OptimizeNone:
  220. return bitc::ATTR_KIND_OPTIMIZE_NONE;
  221. case Attribute::ReadNone:
  222. return bitc::ATTR_KIND_READ_NONE;
  223. case Attribute::ReadOnly:
  224. return bitc::ATTR_KIND_READ_ONLY;
  225. case Attribute::Returned:
  226. return bitc::ATTR_KIND_RETURNED;
  227. case Attribute::ReturnsTwice:
  228. return bitc::ATTR_KIND_RETURNS_TWICE;
  229. case Attribute::SExt:
  230. return bitc::ATTR_KIND_S_EXT;
  231. case Attribute::StackAlignment:
  232. return bitc::ATTR_KIND_STACK_ALIGNMENT;
  233. case Attribute::StackProtect:
  234. return bitc::ATTR_KIND_STACK_PROTECT;
  235. case Attribute::StackProtectReq:
  236. return bitc::ATTR_KIND_STACK_PROTECT_REQ;
  237. case Attribute::StackProtectStrong:
  238. return bitc::ATTR_KIND_STACK_PROTECT_STRONG;
  239. case Attribute::SafeStack:
  240. return bitc::ATTR_KIND_SAFESTACK;
  241. case Attribute::StructRet:
  242. return bitc::ATTR_KIND_STRUCT_RET;
  243. case Attribute::SanitizeAddress:
  244. return bitc::ATTR_KIND_SANITIZE_ADDRESS;
  245. case Attribute::SanitizeThread:
  246. return bitc::ATTR_KIND_SANITIZE_THREAD;
  247. case Attribute::SanitizeMemory:
  248. return bitc::ATTR_KIND_SANITIZE_MEMORY;
  249. case Attribute::SwiftSelf:
  250. return bitc::ATTR_KIND_SWIFT_SELF;
  251. case Attribute::UWTable:
  252. return bitc::ATTR_KIND_UW_TABLE;
  253. case Attribute::ZExt:
  254. return bitc::ATTR_KIND_Z_EXT;
  255. case Attribute::EndAttrKinds:
  256. llvm_unreachable("Can not encode end-attribute kinds marker.");
  257. case Attribute::None:
  258. llvm_unreachable("Can not encode none-attribute.");
  259. }
  260. llvm_unreachable("Trying to encode unknown attribute");
  261. }
  262. static void WriteAttributeGroupTable(const ValueEnumerator &VE,
  263. BitstreamWriter &Stream) {
  264. const std::vector<AttributeSet> &AttrGrps = VE.getAttributeGroups();
  265. if (AttrGrps.empty()) return;
  266. Stream.EnterSubblock(bitc::PARAMATTR_GROUP_BLOCK_ID, 3);
  267. SmallVector<uint64_t, 64> Record;
  268. for (unsigned i = 0, e = AttrGrps.size(); i != e; ++i) {
  269. AttributeSet AS = AttrGrps[i];
  270. for (unsigned i = 0, e = AS.getNumSlots(); i != e; ++i) {
  271. AttributeSet A = AS.getSlotAttributes(i);
  272. Record.push_back(VE.getAttributeGroupID(A));
  273. Record.push_back(AS.getSlotIndex(i));
  274. for (AttributeSet::iterator I = AS.begin(0), E = AS.end(0);
  275. I != E; ++I) {
  276. Attribute Attr = *I;
  277. if (Attr.isEnumAttribute()) {
  278. Record.push_back(0);
  279. Record.push_back(getAttrKindEncoding(Attr.getKindAsEnum()));
  280. } else if (Attr.isIntAttribute()) {
  281. Record.push_back(1);
  282. Record.push_back(getAttrKindEncoding(Attr.getKindAsEnum()));
  283. Record.push_back(Attr.getValueAsInt());
  284. } else {
  285. StringRef Kind = Attr.getKindAsString();
  286. StringRef Val = Attr.getValueAsString();
  287. Record.push_back(Val.empty() ? 3 : 4);
  288. Record.append(Kind.begin(), Kind.end());
  289. Record.push_back(0);
  290. if (!Val.empty()) {
  291. Record.append(Val.begin(), Val.end());
  292. Record.push_back(0);
  293. }
  294. }
  295. }
  296. Stream.EmitRecord(bitc::PARAMATTR_GRP_CODE_ENTRY, Record);
  297. Record.clear();
  298. }
  299. }
  300. Stream.ExitBlock();
  301. }
  302. static void WriteAttributeTable(const ValueEnumerator &VE,
  303. BitstreamWriter &Stream) {
  304. const std::vector<AttributeSet> &Attrs = VE.getAttributes();
  305. if (Attrs.empty()) return;
  306. Stream.EnterSubblock(bitc::PARAMATTR_BLOCK_ID, 3);
  307. SmallVector<uint64_t, 64> Record;
  308. for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
  309. const AttributeSet &A = Attrs[i];
  310. for (unsigned i = 0, e = A.getNumSlots(); i != e; ++i)
  311. Record.push_back(VE.getAttributeGroupID(A.getSlotAttributes(i)));
  312. Stream.EmitRecord(bitc::PARAMATTR_CODE_ENTRY, Record);
  313. Record.clear();
  314. }
  315. Stream.ExitBlock();
  316. }
  317. /// WriteTypeTable - Write out the type table for a module.
  318. static void WriteTypeTable(const ValueEnumerator &VE, BitstreamWriter &Stream) {
  319. const ValueEnumerator::TypeList &TypeList = VE.getTypes();
  320. Stream.EnterSubblock(bitc::TYPE_BLOCK_ID_NEW, 4 /*count from # abbrevs */);
  321. SmallVector<uint64_t, 64> TypeVals;
  322. uint64_t NumBits = VE.computeBitsRequiredForTypeIndicies();
  323. // Abbrev for TYPE_CODE_POINTER.
  324. BitCodeAbbrev *Abbv = new BitCodeAbbrev();
  325. Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_POINTER));
  326. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
  327. Abbv->Add(BitCodeAbbrevOp(0)); // Addrspace = 0
  328. unsigned PtrAbbrev = Stream.EmitAbbrev(Abbv);
  329. // Abbrev for TYPE_CODE_FUNCTION.
  330. Abbv = new BitCodeAbbrev();
  331. Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_FUNCTION));
  332. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isvararg
  333. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
  334. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
  335. unsigned FunctionAbbrev = Stream.EmitAbbrev(Abbv);
  336. // Abbrev for TYPE_CODE_STRUCT_ANON.
  337. Abbv = new BitCodeAbbrev();
  338. Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_ANON));
  339. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ispacked
  340. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
  341. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
  342. unsigned StructAnonAbbrev = Stream.EmitAbbrev(Abbv);
  343. // Abbrev for TYPE_CODE_STRUCT_NAME.
  344. Abbv = new BitCodeAbbrev();
  345. Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_NAME));
  346. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
  347. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
  348. unsigned StructNameAbbrev = Stream.EmitAbbrev(Abbv);
  349. // Abbrev for TYPE_CODE_STRUCT_NAMED.
  350. Abbv = new BitCodeAbbrev();
  351. Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_NAMED));
  352. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ispacked
  353. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
  354. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
  355. unsigned StructNamedAbbrev = Stream.EmitAbbrev(Abbv);
  356. // Abbrev for TYPE_CODE_ARRAY.
  357. Abbv = new BitCodeAbbrev();
  358. Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_ARRAY));
  359. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // size
  360. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
  361. unsigned ArrayAbbrev = Stream.EmitAbbrev(Abbv);
  362. // Emit an entry count so the reader can reserve space.
  363. TypeVals.push_back(TypeList.size());
  364. Stream.EmitRecord(bitc::TYPE_CODE_NUMENTRY, TypeVals);
  365. TypeVals.clear();
  366. // Loop over all of the types, emitting each in turn.
  367. for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
  368. Type *T = TypeList[i];
  369. int AbbrevToUse = 0;
  370. unsigned Code = 0;
  371. switch (T->getTypeID()) {
  372. case Type::VoidTyID: Code = bitc::TYPE_CODE_VOID; break;
  373. case Type::HalfTyID: Code = bitc::TYPE_CODE_HALF; break;
  374. case Type::FloatTyID: Code = bitc::TYPE_CODE_FLOAT; break;
  375. case Type::DoubleTyID: Code = bitc::TYPE_CODE_DOUBLE; break;
  376. case Type::X86_FP80TyID: Code = bitc::TYPE_CODE_X86_FP80; break;
  377. case Type::FP128TyID: Code = bitc::TYPE_CODE_FP128; break;
  378. case Type::PPC_FP128TyID: Code = bitc::TYPE_CODE_PPC_FP128; break;
  379. case Type::LabelTyID: Code = bitc::TYPE_CODE_LABEL; break;
  380. case Type::MetadataTyID: Code = bitc::TYPE_CODE_METADATA; break;
  381. case Type::X86_MMXTyID: Code = bitc::TYPE_CODE_X86_MMX; break;
  382. case Type::TokenTyID: Code = bitc::TYPE_CODE_TOKEN; break;
  383. case Type::IntegerTyID:
  384. // INTEGER: [width]
  385. Code = bitc::TYPE_CODE_INTEGER;
  386. TypeVals.push_back(cast<IntegerType>(T)->getBitWidth());
  387. break;
  388. case Type::PointerTyID: {
  389. PointerType *PTy = cast<PointerType>(T);
  390. // POINTER: [pointee type, address space]
  391. Code = bitc::TYPE_CODE_POINTER;
  392. TypeVals.push_back(VE.getTypeID(PTy->getElementType()));
  393. unsigned AddressSpace = PTy->getAddressSpace();
  394. TypeVals.push_back(AddressSpace);
  395. if (AddressSpace == 0) AbbrevToUse = PtrAbbrev;
  396. break;
  397. }
  398. case Type::FunctionTyID: {
  399. FunctionType *FT = cast<FunctionType>(T);
  400. // FUNCTION: [isvararg, retty, paramty x N]
  401. Code = bitc::TYPE_CODE_FUNCTION;
  402. TypeVals.push_back(FT->isVarArg());
  403. TypeVals.push_back(VE.getTypeID(FT->getReturnType()));
  404. for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i)
  405. TypeVals.push_back(VE.getTypeID(FT->getParamType(i)));
  406. AbbrevToUse = FunctionAbbrev;
  407. break;
  408. }
  409. case Type::StructTyID: {
  410. StructType *ST = cast<StructType>(T);
  411. // STRUCT: [ispacked, eltty x N]
  412. TypeVals.push_back(ST->isPacked());
  413. // Output all of the element types.
  414. for (StructType::element_iterator I = ST->element_begin(),
  415. E = ST->element_end(); I != E; ++I)
  416. TypeVals.push_back(VE.getTypeID(*I));
  417. if (ST->isLiteral()) {
  418. Code = bitc::TYPE_CODE_STRUCT_ANON;
  419. AbbrevToUse = StructAnonAbbrev;
  420. } else {
  421. if (ST->isOpaque()) {
  422. Code = bitc::TYPE_CODE_OPAQUE;
  423. } else {
  424. Code = bitc::TYPE_CODE_STRUCT_NAMED;
  425. AbbrevToUse = StructNamedAbbrev;
  426. }
  427. // Emit the name if it is present.
  428. if (!ST->getName().empty())
  429. WriteStringRecord(bitc::TYPE_CODE_STRUCT_NAME, ST->getName(),
  430. StructNameAbbrev, Stream);
  431. }
  432. break;
  433. }
  434. case Type::ArrayTyID: {
  435. ArrayType *AT = cast<ArrayType>(T);
  436. // ARRAY: [numelts, eltty]
  437. Code = bitc::TYPE_CODE_ARRAY;
  438. TypeVals.push_back(AT->getNumElements());
  439. TypeVals.push_back(VE.getTypeID(AT->getElementType()));
  440. AbbrevToUse = ArrayAbbrev;
  441. break;
  442. }
  443. case Type::VectorTyID: {
  444. VectorType *VT = cast<VectorType>(T);
  445. // VECTOR [numelts, eltty]
  446. Code = bitc::TYPE_CODE_VECTOR;
  447. TypeVals.push_back(VT->getNumElements());
  448. TypeVals.push_back(VE.getTypeID(VT->getElementType()));
  449. break;
  450. }
  451. }
  452. // Emit the finished record.
  453. Stream.EmitRecord(Code, TypeVals, AbbrevToUse);
  454. TypeVals.clear();
  455. }
  456. Stream.ExitBlock();
  457. }
  458. static unsigned getEncodedLinkage(const GlobalValue::LinkageTypes Linkage) {
  459. switch (Linkage) {
  460. case GlobalValue::ExternalLinkage:
  461. return 0;
  462. case GlobalValue::WeakAnyLinkage:
  463. return 16;
  464. case GlobalValue::AppendingLinkage:
  465. return 2;
  466. case GlobalValue::InternalLinkage:
  467. return 3;
  468. case GlobalValue::LinkOnceAnyLinkage:
  469. return 18;
  470. case GlobalValue::ExternalWeakLinkage:
  471. return 7;
  472. case GlobalValue::CommonLinkage:
  473. return 8;
  474. case GlobalValue::PrivateLinkage:
  475. return 9;
  476. case GlobalValue::WeakODRLinkage:
  477. return 17;
  478. case GlobalValue::LinkOnceODRLinkage:
  479. return 19;
  480. case GlobalValue::AvailableExternallyLinkage:
  481. return 12;
  482. }
  483. llvm_unreachable("Invalid linkage");
  484. }
  485. static unsigned getEncodedLinkage(const GlobalValue &GV) {
  486. return getEncodedLinkage(GV.getLinkage());
  487. }
  488. static unsigned getEncodedVisibility(const GlobalValue &GV) {
  489. switch (GV.getVisibility()) {
  490. case GlobalValue::DefaultVisibility: return 0;
  491. case GlobalValue::HiddenVisibility: return 1;
  492. case GlobalValue::ProtectedVisibility: return 2;
  493. }
  494. llvm_unreachable("Invalid visibility");
  495. }
  496. static unsigned getEncodedDLLStorageClass(const GlobalValue &GV) {
  497. switch (GV.getDLLStorageClass()) {
  498. case GlobalValue::DefaultStorageClass: return 0;
  499. case GlobalValue::DLLImportStorageClass: return 1;
  500. case GlobalValue::DLLExportStorageClass: return 2;
  501. }
  502. llvm_unreachable("Invalid DLL storage class");
  503. }
  504. static unsigned getEncodedThreadLocalMode(const GlobalValue &GV) {
  505. switch (GV.getThreadLocalMode()) {
  506. case GlobalVariable::NotThreadLocal: return 0;
  507. case GlobalVariable::GeneralDynamicTLSModel: return 1;
  508. case GlobalVariable::LocalDynamicTLSModel: return 2;
  509. case GlobalVariable::InitialExecTLSModel: return 3;
  510. case GlobalVariable::LocalExecTLSModel: return 4;
  511. }
  512. llvm_unreachable("Invalid TLS model");
  513. }
  514. static unsigned getEncodedComdatSelectionKind(const Comdat &C) {
  515. switch (C.getSelectionKind()) {
  516. case Comdat::Any:
  517. return bitc::COMDAT_SELECTION_KIND_ANY;
  518. case Comdat::ExactMatch:
  519. return bitc::COMDAT_SELECTION_KIND_EXACT_MATCH;
  520. case Comdat::Largest:
  521. return bitc::COMDAT_SELECTION_KIND_LARGEST;
  522. case Comdat::NoDuplicates:
  523. return bitc::COMDAT_SELECTION_KIND_NO_DUPLICATES;
  524. case Comdat::SameSize:
  525. return bitc::COMDAT_SELECTION_KIND_SAME_SIZE;
  526. }
  527. llvm_unreachable("Invalid selection kind");
  528. }
  529. static void writeComdats(const ValueEnumerator &VE, BitstreamWriter &Stream) {
  530. SmallVector<unsigned, 64> Vals;
  531. for (const Comdat *C : VE.getComdats()) {
  532. // COMDAT: [selection_kind, name]
  533. Vals.push_back(getEncodedComdatSelectionKind(*C));
  534. size_t Size = C->getName().size();
  535. assert(isUInt<32>(Size));
  536. Vals.push_back(Size);
  537. for (char Chr : C->getName())
  538. Vals.push_back((unsigned char)Chr);
  539. Stream.EmitRecord(bitc::MODULE_CODE_COMDAT, Vals, /*AbbrevToUse=*/0);
  540. Vals.clear();
  541. }
  542. }
  543. /// Write a record that will eventually hold the word offset of the
  544. /// module-level VST. For now the offset is 0, which will be backpatched
  545. /// after the real VST is written. Returns the bit offset to backpatch.
  546. static uint64_t WriteValueSymbolTableForwardDecl(BitstreamWriter &Stream) {
  547. // Write a placeholder value in for the offset of the real VST,
  548. // which is written after the function blocks so that it can include
  549. // the offset of each function. The placeholder offset will be
  550. // updated when the real VST is written.
  551. BitCodeAbbrev *Abbv = new BitCodeAbbrev();
  552. Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_VSTOFFSET));
  553. // Blocks are 32-bit aligned, so we can use a 32-bit word offset to
  554. // hold the real VST offset. Must use fixed instead of VBR as we don't
  555. // know how many VBR chunks to reserve ahead of time.
  556. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
  557. unsigned VSTOffsetAbbrev = Stream.EmitAbbrev(Abbv);
  558. // Emit the placeholder
  559. uint64_t Vals[] = {bitc::MODULE_CODE_VSTOFFSET, 0};
  560. Stream.EmitRecordWithAbbrev(VSTOffsetAbbrev, Vals);
  561. // Compute and return the bit offset to the placeholder, which will be
  562. // patched when the real VST is written. We can simply subtract the 32-bit
  563. // fixed size from the current bit number to get the location to backpatch.
  564. return Stream.GetCurrentBitNo() - 32;
  565. }
  566. enum StringEncoding { SE_Char6, SE_Fixed7, SE_Fixed8 };
  567. /// Determine the encoding to use for the given string name and length.
  568. static StringEncoding getStringEncoding(const char *Str, unsigned StrLen) {
  569. bool isChar6 = true;
  570. for (const char *C = Str, *E = C + StrLen; C != E; ++C) {
  571. if (isChar6)
  572. isChar6 = BitCodeAbbrevOp::isChar6(*C);
  573. if ((unsigned char)*C & 128)
  574. // don't bother scanning the rest.
  575. return SE_Fixed8;
  576. }
  577. if (isChar6)
  578. return SE_Char6;
  579. else
  580. return SE_Fixed7;
  581. }
  582. /// Emit top-level description of module, including target triple, inline asm,
  583. /// descriptors for global variables, and function prototype info.
  584. /// Returns the bit offset to backpatch with the location of the real VST.
  585. static uint64_t WriteModuleInfo(const Module *M, const ValueEnumerator &VE,
  586. BitstreamWriter &Stream) {
  587. // Emit various pieces of data attached to a module.
  588. if (!M->getTargetTriple().empty())
  589. WriteStringRecord(bitc::MODULE_CODE_TRIPLE, M->getTargetTriple(),
  590. 0/*TODO*/, Stream);
  591. const std::string &DL = M->getDataLayoutStr();
  592. if (!DL.empty())
  593. WriteStringRecord(bitc::MODULE_CODE_DATALAYOUT, DL, 0 /*TODO*/, Stream);
  594. if (!M->getModuleInlineAsm().empty())
  595. WriteStringRecord(bitc::MODULE_CODE_ASM, M->getModuleInlineAsm(),
  596. 0/*TODO*/, Stream);
  597. // Emit information about sections and GC, computing how many there are. Also
  598. // compute the maximum alignment value.
  599. std::map<std::string, unsigned> SectionMap;
  600. std::map<std::string, unsigned> GCMap;
  601. unsigned MaxAlignment = 0;
  602. unsigned MaxGlobalType = 0;
  603. for (const GlobalValue &GV : M->globals()) {
  604. MaxAlignment = std::max(MaxAlignment, GV.getAlignment());
  605. MaxGlobalType = std::max(MaxGlobalType, VE.getTypeID(GV.getValueType()));
  606. if (GV.hasSection()) {
  607. // Give section names unique ID's.
  608. unsigned &Entry = SectionMap[GV.getSection()];
  609. if (!Entry) {
  610. WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, GV.getSection(),
  611. 0/*TODO*/, Stream);
  612. Entry = SectionMap.size();
  613. }
  614. }
  615. }
  616. for (const Function &F : *M) {
  617. MaxAlignment = std::max(MaxAlignment, F.getAlignment());
  618. if (F.hasSection()) {
  619. // Give section names unique ID's.
  620. unsigned &Entry = SectionMap[F.getSection()];
  621. if (!Entry) {
  622. WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, F.getSection(),
  623. 0/*TODO*/, Stream);
  624. Entry = SectionMap.size();
  625. }
  626. }
  627. if (F.hasGC()) {
  628. // Same for GC names.
  629. unsigned &Entry = GCMap[F.getGC()];
  630. if (!Entry) {
  631. WriteStringRecord(bitc::MODULE_CODE_GCNAME, F.getGC(),
  632. 0/*TODO*/, Stream);
  633. Entry = GCMap.size();
  634. }
  635. }
  636. }
  637. // Emit abbrev for globals, now that we know # sections and max alignment.
  638. unsigned SimpleGVarAbbrev = 0;
  639. if (!M->global_empty()) {
  640. // Add an abbrev for common globals with no visibility or thread localness.
  641. BitCodeAbbrev *Abbv = new BitCodeAbbrev();
  642. Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_GLOBALVAR));
  643. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
  644. Log2_32_Ceil(MaxGlobalType+1)));
  645. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // AddrSpace << 2
  646. //| explicitType << 1
  647. //| constant
  648. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Initializer.
  649. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 5)); // Linkage.
  650. if (MaxAlignment == 0) // Alignment.
  651. Abbv->Add(BitCodeAbbrevOp(0));
  652. else {
  653. unsigned MaxEncAlignment = Log2_32(MaxAlignment)+1;
  654. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
  655. Log2_32_Ceil(MaxEncAlignment+1)));
  656. }
  657. if (SectionMap.empty()) // Section.
  658. Abbv->Add(BitCodeAbbrevOp(0));
  659. else
  660. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
  661. Log2_32_Ceil(SectionMap.size()+1)));
  662. // Don't bother emitting vis + thread local.
  663. SimpleGVarAbbrev = Stream.EmitAbbrev(Abbv);
  664. }
  665. // Emit the global variable information.
  666. SmallVector<unsigned, 64> Vals;
  667. for (const GlobalVariable &GV : M->globals()) {
  668. unsigned AbbrevToUse = 0;
  669. // GLOBALVAR: [type, isconst, initid,
  670. // linkage, alignment, section, visibility, threadlocal,
  671. // unnamed_addr, externally_initialized, dllstorageclass,
  672. // comdat]
  673. Vals.push_back(VE.getTypeID(GV.getValueType()));
  674. Vals.push_back(GV.getType()->getAddressSpace() << 2 | 2 | GV.isConstant());
  675. Vals.push_back(GV.isDeclaration() ? 0 :
  676. (VE.getValueID(GV.getInitializer()) + 1));
  677. Vals.push_back(getEncodedLinkage(GV));
  678. Vals.push_back(Log2_32(GV.getAlignment())+1);
  679. Vals.push_back(GV.hasSection() ? SectionMap[GV.getSection()] : 0);
  680. if (GV.isThreadLocal() ||
  681. GV.getVisibility() != GlobalValue::DefaultVisibility ||
  682. GV.hasUnnamedAddr() || GV.isExternallyInitialized() ||
  683. GV.getDLLStorageClass() != GlobalValue::DefaultStorageClass ||
  684. GV.hasComdat()) {
  685. Vals.push_back(getEncodedVisibility(GV));
  686. Vals.push_back(getEncodedThreadLocalMode(GV));
  687. Vals.push_back(GV.hasUnnamedAddr());
  688. Vals.push_back(GV.isExternallyInitialized());
  689. Vals.push_back(getEncodedDLLStorageClass(GV));
  690. Vals.push_back(GV.hasComdat() ? VE.getComdatID(GV.getComdat()) : 0);
  691. } else {
  692. AbbrevToUse = SimpleGVarAbbrev;
  693. }
  694. Stream.EmitRecord(bitc::MODULE_CODE_GLOBALVAR, Vals, AbbrevToUse);
  695. Vals.clear();
  696. }
  697. // Emit the function proto information.
  698. for (const Function &F : *M) {
  699. // FUNCTION: [type, callingconv, isproto, linkage, paramattrs, alignment,
  700. // section, visibility, gc, unnamed_addr, prologuedata,
  701. // dllstorageclass, comdat, prefixdata, personalityfn]
  702. Vals.push_back(VE.getTypeID(F.getFunctionType()));
  703. Vals.push_back(F.getCallingConv());
  704. Vals.push_back(F.isDeclaration());
  705. Vals.push_back(getEncodedLinkage(F));
  706. Vals.push_back(VE.getAttributeID(F.getAttributes()));
  707. Vals.push_back(Log2_32(F.getAlignment())+1);
  708. Vals.push_back(F.hasSection() ? SectionMap[F.getSection()] : 0);
  709. Vals.push_back(getEncodedVisibility(F));
  710. Vals.push_back(F.hasGC() ? GCMap[F.getGC()] : 0);
  711. Vals.push_back(F.hasUnnamedAddr());
  712. Vals.push_back(F.hasPrologueData() ? (VE.getValueID(F.getPrologueData()) + 1)
  713. : 0);
  714. Vals.push_back(getEncodedDLLStorageClass(F));
  715. Vals.push_back(F.hasComdat() ? VE.getComdatID(F.getComdat()) : 0);
  716. Vals.push_back(F.hasPrefixData() ? (VE.getValueID(F.getPrefixData()) + 1)
  717. : 0);
  718. Vals.push_back(
  719. F.hasPersonalityFn() ? (VE.getValueID(F.getPersonalityFn()) + 1) : 0);
  720. unsigned AbbrevToUse = 0;
  721. Stream.EmitRecord(bitc::MODULE_CODE_FUNCTION, Vals, AbbrevToUse);
  722. Vals.clear();
  723. }
  724. // Emit the alias information.
  725. for (const GlobalAlias &A : M->aliases()) {
  726. // ALIAS: [alias type, aliasee val#, linkage, visibility]
  727. Vals.push_back(VE.getTypeID(A.getValueType()));
  728. Vals.push_back(A.getType()->getAddressSpace());
  729. Vals.push_back(VE.getValueID(A.getAliasee()));
  730. Vals.push_back(getEncodedLinkage(A));
  731. Vals.push_back(getEncodedVisibility(A));
  732. Vals.push_back(getEncodedDLLStorageClass(A));
  733. Vals.push_back(getEncodedThreadLocalMode(A));
  734. Vals.push_back(A.hasUnnamedAddr());
  735. unsigned AbbrevToUse = 0;
  736. Stream.EmitRecord(bitc::MODULE_CODE_ALIAS, Vals, AbbrevToUse);
  737. Vals.clear();
  738. }
  739. // Emit the module's source file name.
  740. {
  741. StringEncoding Bits = getStringEncoding(M->getSourceFileName().data(),
  742. M->getSourceFileName().size());
  743. BitCodeAbbrevOp AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8);
  744. if (Bits == SE_Char6)
  745. AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Char6);
  746. else if (Bits == SE_Fixed7)
  747. AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7);
  748. // MODULE_CODE_SOURCE_FILENAME: [namechar x N]
  749. BitCodeAbbrev *Abbv = new BitCodeAbbrev();
  750. Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_SOURCE_FILENAME));
  751. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
  752. Abbv->Add(AbbrevOpToUse);
  753. unsigned FilenameAbbrev = Stream.EmitAbbrev(Abbv);
  754. for (const auto P : M->getSourceFileName())
  755. Vals.push_back((unsigned char)P);
  756. // Emit the finished record.
  757. Stream.EmitRecord(bitc::MODULE_CODE_SOURCE_FILENAME, Vals, FilenameAbbrev);
  758. Vals.clear();
  759. }
  760. // If we have a VST, write the VSTOFFSET record placeholder and return
  761. // its offset.
  762. if (M->getValueSymbolTable().empty())
  763. return 0;
  764. return WriteValueSymbolTableForwardDecl(Stream);
  765. }
  766. static uint64_t GetOptimizationFlags(const Value *V) {
  767. uint64_t Flags = 0;
  768. if (const auto *OBO = dyn_cast<OverflowingBinaryOperator>(V)) {
  769. if (OBO->hasNoSignedWrap())
  770. Flags |= 1 << bitc::OBO_NO_SIGNED_WRAP;
  771. if (OBO->hasNoUnsignedWrap())
  772. Flags |= 1 << bitc::OBO_NO_UNSIGNED_WRAP;
  773. } else if (const auto *PEO = dyn_cast<PossiblyExactOperator>(V)) {
  774. if (PEO->isExact())
  775. Flags |= 1 << bitc::PEO_EXACT;
  776. } else if (const auto *FPMO = dyn_cast<FPMathOperator>(V)) {
  777. if (FPMO->hasUnsafeAlgebra())
  778. Flags |= FastMathFlags::UnsafeAlgebra;
  779. if (FPMO->hasNoNaNs())
  780. Flags |= FastMathFlags::NoNaNs;
  781. if (FPMO->hasNoInfs())
  782. Flags |= FastMathFlags::NoInfs;
  783. if (FPMO->hasNoSignedZeros())
  784. Flags |= FastMathFlags::NoSignedZeros;
  785. if (FPMO->hasAllowReciprocal())
  786. Flags |= FastMathFlags::AllowReciprocal;
  787. }
  788. return Flags;
  789. }
  790. static void writeValueAsMetadata(const ValueAsMetadata *MD,
  791. const ValueEnumerator &VE,
  792. BitstreamWriter &Stream,
  793. SmallVectorImpl<uint64_t> &Record) {
  794. // Mimic an MDNode with a value as one operand.
  795. Value *V = MD->getValue();
  796. Record.push_back(VE.getTypeID(V->getType()));
  797. Record.push_back(VE.getValueID(V));
  798. Stream.EmitRecord(bitc::METADATA_VALUE, Record, 0);
  799. Record.clear();
  800. }
  801. static void writeMDTuple(const MDTuple *N, const ValueEnumerator &VE,
  802. BitstreamWriter &Stream,
  803. SmallVectorImpl<uint64_t> &Record, unsigned Abbrev) {
  804. for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
  805. Metadata *MD = N->getOperand(i);
  806. assert(!(MD && isa<LocalAsMetadata>(MD)) &&
  807. "Unexpected function-local metadata");
  808. Record.push_back(VE.getMetadataOrNullID(MD));
  809. }
  810. Stream.EmitRecord(N->isDistinct() ? bitc::METADATA_DISTINCT_NODE
  811. : bitc::METADATA_NODE,
  812. Record, Abbrev);
  813. Record.clear();
  814. }
  815. static unsigned createDILocationAbbrev(BitstreamWriter &Stream) {
  816. // Assume the column is usually under 128, and always output the inlined-at
  817. // location (it's never more expensive than building an array size 1).
  818. BitCodeAbbrev *Abbv = new BitCodeAbbrev();
  819. Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_LOCATION));
  820. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
  821. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
  822. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
  823. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
  824. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
  825. return Stream.EmitAbbrev(Abbv);
  826. }
  827. static void writeDILocation(const DILocation *N, const ValueEnumerator &VE,
  828. BitstreamWriter &Stream,
  829. SmallVectorImpl<uint64_t> &Record,
  830. unsigned &Abbrev) {
  831. if (!Abbrev)
  832. Abbrev = createDILocationAbbrev(Stream);
  833. Record.push_back(N->isDistinct());
  834. Record.push_back(N->getLine());
  835. Record.push_back(N->getColumn());
  836. Record.push_back(VE.getMetadataID(N->getScope()));
  837. Record.push_back(VE.getMetadataOrNullID(N->getInlinedAt()));
  838. Stream.EmitRecord(bitc::METADATA_LOCATION, Record, Abbrev);
  839. Record.clear();
  840. }
  841. static unsigned createGenericDINodeAbbrev(BitstreamWriter &Stream) {
  842. // Assume the column is usually under 128, and always output the inlined-at
  843. // location (it's never more expensive than building an array size 1).
  844. BitCodeAbbrev *Abbv = new BitCodeAbbrev();
  845. Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_GENERIC_DEBUG));
  846. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
  847. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
  848. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
  849. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
  850. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
  851. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
  852. return Stream.EmitAbbrev(Abbv);
  853. }
  854. static void writeGenericDINode(const GenericDINode *N,
  855. const ValueEnumerator &VE,
  856. BitstreamWriter &Stream,
  857. SmallVectorImpl<uint64_t> &Record,
  858. unsigned &Abbrev) {
  859. if (!Abbrev)
  860. Abbrev = createGenericDINodeAbbrev(Stream);
  861. Record.push_back(N->isDistinct());
  862. Record.push_back(N->getTag());
  863. Record.push_back(0); // Per-tag version field; unused for now.
  864. for (auto &I : N->operands())
  865. Record.push_back(VE.getMetadataOrNullID(I));
  866. Stream.EmitRecord(bitc::METADATA_GENERIC_DEBUG, Record, Abbrev);
  867. Record.clear();
  868. }
  869. static uint64_t rotateSign(int64_t I) {
  870. uint64_t U = I;
  871. return I < 0 ? ~(U << 1) : U << 1;
  872. }
  873. static void writeDISubrange(const DISubrange *N, const ValueEnumerator &,
  874. BitstreamWriter &Stream,
  875. SmallVectorImpl<uint64_t> &Record,
  876. unsigned Abbrev) {
  877. Record.push_back(N->isDistinct());
  878. Record.push_back(N->getCount());
  879. Record.push_back(rotateSign(N->getLowerBound()));
  880. Stream.EmitRecord(bitc::METADATA_SUBRANGE, Record, Abbrev);
  881. Record.clear();
  882. }
  883. static void writeDIEnumerator(const DIEnumerator *N, const ValueEnumerator &VE,
  884. BitstreamWriter &Stream,
  885. SmallVectorImpl<uint64_t> &Record,
  886. unsigned Abbrev) {
  887. Record.push_back(N->isDistinct());
  888. Record.push_back(rotateSign(N->getValue()));
  889. Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
  890. Stream.EmitRecord(bitc::METADATA_ENUMERATOR, Record, Abbrev);
  891. Record.clear();
  892. }
  893. static void writeDIBasicType(const DIBasicType *N, const ValueEnumerator &VE,
  894. BitstreamWriter &Stream,
  895. SmallVectorImpl<uint64_t> &Record,
  896. unsigned Abbrev) {
  897. Record.push_back(N->isDistinct());
  898. Record.push_back(N->getTag());
  899. Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
  900. Record.push_back(N->getSizeInBits());
  901. Record.push_back(N->getAlignInBits());
  902. Record.push_back(N->getEncoding());
  903. Stream.EmitRecord(bitc::METADATA_BASIC_TYPE, Record, Abbrev);
  904. Record.clear();
  905. }
  906. static void writeDIDerivedType(const DIDerivedType *N,
  907. const ValueEnumerator &VE,
  908. BitstreamWriter &Stream,
  909. SmallVectorImpl<uint64_t> &Record,
  910. unsigned Abbrev) {
  911. Record.push_back(N->isDistinct());
  912. Record.push_back(N->getTag());
  913. Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
  914. Record.push_back(VE.getMetadataOrNullID(N->getFile()));
  915. Record.push_back(N->getLine());
  916. Record.push_back(VE.getMetadataOrNullID(N->getScope()));
  917. Record.push_back(VE.getMetadataOrNullID(N->getBaseType()));
  918. Record.push_back(N->getSizeInBits());
  919. Record.push_back(N->getAlignInBits());
  920. Record.push_back(N->getOffsetInBits());
  921. Record.push_back(N->getFlags());
  922. Record.push_back(VE.getMetadataOrNullID(N->getExtraData()));
  923. Stream.EmitRecord(bitc::METADATA_DERIVED_TYPE, Record, Abbrev);
  924. Record.clear();
  925. }
  926. static void writeDICompositeType(const DICompositeType *N,
  927. const ValueEnumerator &VE,
  928. BitstreamWriter &Stream,
  929. SmallVectorImpl<uint64_t> &Record,
  930. unsigned Abbrev) {
  931. Record.push_back(N->isDistinct());
  932. Record.push_back(N->getTag());
  933. Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
  934. Record.push_back(VE.getMetadataOrNullID(N->getFile()));
  935. Record.push_back(N->getLine());
  936. Record.push_back(VE.getMetadataOrNullID(N->getScope()));
  937. Record.push_back(VE.getMetadataOrNullID(N->getBaseType()));
  938. Record.push_back(N->getSizeInBits());
  939. Record.push_back(N->getAlignInBits());
  940. Record.push_back(N->getOffsetInBits());
  941. Record.push_back(N->getFlags());
  942. Record.push_back(VE.getMetadataOrNullID(N->getElements().get()));
  943. Record.push_back(N->getRuntimeLang());
  944. Record.push_back(VE.getMetadataOrNullID(N->getVTableHolder()));
  945. Record.push_back(VE.getMetadataOrNullID(N->getTemplateParams().get()));
  946. Record.push_back(VE.getMetadataOrNullID(N->getRawIdentifier()));
  947. Stream.EmitRecord(bitc::METADATA_COMPOSITE_TYPE, Record, Abbrev);
  948. Record.clear();
  949. }
  950. static void writeDISubroutineType(const DISubroutineType *N,
  951. const ValueEnumerator &VE,
  952. BitstreamWriter &Stream,
  953. SmallVectorImpl<uint64_t> &Record,
  954. unsigned Abbrev) {
  955. Record.push_back(N->isDistinct());
  956. Record.push_back(N->getFlags());
  957. Record.push_back(VE.getMetadataOrNullID(N->getTypeArray().get()));
  958. Stream.EmitRecord(bitc::METADATA_SUBROUTINE_TYPE, Record, Abbrev);
  959. Record.clear();
  960. }
  961. static void writeDIFile(const DIFile *N, const ValueEnumerator &VE,
  962. BitstreamWriter &Stream,
  963. SmallVectorImpl<uint64_t> &Record, unsigned Abbrev) {
  964. Record.push_back(N->isDistinct());
  965. Record.push_back(VE.getMetadataOrNullID(N->getRawFilename()));
  966. Record.push_back(VE.getMetadataOrNullID(N->getRawDirectory()));
  967. Stream.EmitRecord(bitc::METADATA_FILE, Record, Abbrev);
  968. Record.clear();
  969. }
  970. static void writeDICompileUnit(const DICompileUnit *N,
  971. const ValueEnumerator &VE,
  972. BitstreamWriter &Stream,
  973. SmallVectorImpl<uint64_t> &Record,
  974. unsigned Abbrev) {
  975. assert(N->isDistinct() && "Expected distinct compile units");
  976. Record.push_back(/* IsDistinct */ true);
  977. Record.push_back(N->getSourceLanguage());
  978. Record.push_back(VE.getMetadataOrNullID(N->getFile()));
  979. Record.push_back(VE.getMetadataOrNullID(N->getRawProducer()));
  980. Record.push_back(N->isOptimized());
  981. Record.push_back(VE.getMetadataOrNullID(N->getRawFlags()));
  982. Record.push_back(N->getRuntimeVersion());
  983. Record.push_back(VE.getMetadataOrNullID(N->getRawSplitDebugFilename()));
  984. Record.push_back(N->getEmissionKind());
  985. Record.push_back(VE.getMetadataOrNullID(N->getEnumTypes().get()));
  986. Record.push_back(VE.getMetadataOrNullID(N->getRetainedTypes().get()));
  987. Record.push_back(VE.getMetadataOrNullID(N->getSubprograms().get()));
  988. Record.push_back(VE.getMetadataOrNullID(N->getGlobalVariables().get()));
  989. Record.push_back(VE.getMetadataOrNullID(N->getImportedEntities().get()));
  990. Record.push_back(N->getDWOId());
  991. Record.push_back(VE.getMetadataOrNullID(N->getMacros().get()));
  992. Stream.EmitRecord(bitc::METADATA_COMPILE_UNIT, Record, Abbrev);
  993. Record.clear();
  994. }
  995. static void writeDISubprogram(const DISubprogram *N, const ValueEnumerator &VE,
  996. BitstreamWriter &Stream,
  997. SmallVectorImpl<uint64_t> &Record,
  998. unsigned Abbrev) {
  999. Record.push_back(N->isDistinct());
  1000. Record.push_back(VE.getMetadataOrNullID(N->getScope()));
  1001. Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
  1002. Record.push_back(VE.getMetadataOrNullID(N->getRawLinkageName()));
  1003. Record.push_back(VE.getMetadataOrNullID(N->getFile()));
  1004. Record.push_back(N->getLine());
  1005. Record.push_back(VE.getMetadataOrNullID(N->getType()));
  1006. Record.push_back(N->isLocalToUnit());
  1007. Record.push_back(N->isDefinition());
  1008. Record.push_back(N->getScopeLine());
  1009. Record.push_back(VE.getMetadataOrNullID(N->getContainingType()));
  1010. Record.push_back(N->getVirtuality());
  1011. Record.push_back(N->getVirtualIndex());
  1012. Record.push_back(N->getFlags());
  1013. Record.push_back(N->isOptimized());
  1014. Record.push_back(VE.getMetadataOrNullID(N->getTemplateParams().get()));
  1015. Record.push_back(VE.getMetadataOrNullID(N->getDeclaration()));
  1016. Record.push_back(VE.getMetadataOrNullID(N->getVariables().get()));
  1017. Stream.EmitRecord(bitc::METADATA_SUBPROGRAM, Record, Abbrev);
  1018. Record.clear();
  1019. }
  1020. static void writeDILexicalBlock(const DILexicalBlock *N,
  1021. const ValueEnumerator &VE,
  1022. BitstreamWriter &Stream,
  1023. SmallVectorImpl<uint64_t> &Record,
  1024. unsigned Abbrev) {
  1025. Record.push_back(N->isDistinct());
  1026. Record.push_back(VE.getMetadataOrNullID(N->getScope()));
  1027. Record.push_back(VE.getMetadataOrNullID(N->getFile()));
  1028. Record.push_back(N->getLine());
  1029. Record.push_back(N->getColumn());
  1030. Stream.EmitRecord(bitc::METADATA_LEXICAL_BLOCK, Record, Abbrev);
  1031. Record.clear();
  1032. }
  1033. static void writeDILexicalBlockFile(const DILexicalBlockFile *N,
  1034. const ValueEnumerator &VE,
  1035. BitstreamWriter &Stream,
  1036. SmallVectorImpl<uint64_t> &Record,
  1037. unsigned Abbrev) {
  1038. Record.push_back(N->isDistinct());
  1039. Record.push_back(VE.getMetadataOrNullID(N->getScope()));
  1040. Record.push_back(VE.getMetadataOrNullID(N->getFile()));
  1041. Record.push_back(N->getDiscriminator());
  1042. Stream.EmitRecord(bitc::METADATA_LEXICAL_BLOCK_FILE, Record, Abbrev);
  1043. Record.clear();
  1044. }
  1045. static void writeDINamespace(const DINamespace *N, const ValueEnumerator &VE,
  1046. BitstreamWriter &Stream,
  1047. SmallVectorImpl<uint64_t> &Record,
  1048. unsigned Abbrev) {
  1049. Record.push_back(N->isDistinct());
  1050. Record.push_back(VE.getMetadataOrNullID(N->getScope()));
  1051. Record.push_back(VE.getMetadataOrNullID(N->getFile()));
  1052. Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
  1053. Record.push_back(N->getLine());
  1054. Stream.EmitRecord(bitc::METADATA_NAMESPACE, Record, Abbrev);
  1055. Record.clear();
  1056. }
  1057. static void writeDIMacro(const DIMacro *N, const ValueEnumerator &VE,
  1058. BitstreamWriter &Stream,
  1059. SmallVectorImpl<uint64_t> &Record, unsigned Abbrev) {
  1060. Record.push_back(N->isDistinct());
  1061. Record.push_back(N->getMacinfoType());
  1062. Record.push_back(N->getLine());
  1063. Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
  1064. Record.push_back(VE.getMetadataOrNullID(N->getRawValue()));
  1065. Stream.EmitRecord(bitc::METADATA_MACRO, Record, Abbrev);
  1066. Record.clear();
  1067. }
  1068. static void writeDIMacroFile(const DIMacroFile *N, const ValueEnumerator &VE,
  1069. BitstreamWriter &Stream,
  1070. SmallVectorImpl<uint64_t> &Record,
  1071. unsigned Abbrev) {
  1072. Record.push_back(N->isDistinct());
  1073. Record.push_back(N->getMacinfoType());
  1074. Record.push_back(N->getLine());
  1075. Record.push_back(VE.getMetadataOrNullID(N->getFile()));
  1076. Record.push_back(VE.getMetadataOrNullID(N->getElements().get()));
  1077. Stream.EmitRecord(bitc::METADATA_MACRO_FILE, Record, Abbrev);
  1078. Record.clear();
  1079. }
  1080. static void writeDIModule(const DIModule *N, const ValueEnumerator &VE,
  1081. BitstreamWriter &Stream,
  1082. SmallVectorImpl<uint64_t> &Record, unsigned Abbrev) {
  1083. Record.push_back(N->isDistinct());
  1084. for (auto &I : N->operands())
  1085. Record.push_back(VE.getMetadataOrNullID(I));
  1086. Stream.EmitRecord(bitc::METADATA_MODULE, Record, Abbrev);
  1087. Record.clear();
  1088. }
  1089. static void writeDITemplateTypeParameter(const DITemplateTypeParameter *N,
  1090. const ValueEnumerator &VE,
  1091. BitstreamWriter &Stream,
  1092. SmallVectorImpl<uint64_t> &Record,
  1093. unsigned Abbrev) {
  1094. Record.push_back(N->isDistinct());
  1095. Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
  1096. Record.push_back(VE.getMetadataOrNullID(N->getType()));
  1097. Stream.EmitRecord(bitc::METADATA_TEMPLATE_TYPE, Record, Abbrev);
  1098. Record.clear();
  1099. }
  1100. static void writeDITemplateValueParameter(const DITemplateValueParameter *N,
  1101. const ValueEnumerator &VE,
  1102. BitstreamWriter &Stream,
  1103. SmallVectorImpl<uint64_t> &Record,
  1104. unsigned Abbrev) {
  1105. Record.push_back(N->isDistinct());
  1106. Record.push_back(N->getTag());
  1107. Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
  1108. Record.push_back(VE.getMetadataOrNullID(N->getType()));
  1109. Record.push_back(VE.getMetadataOrNullID(N->getValue()));
  1110. Stream.EmitRecord(bitc::METADATA_TEMPLATE_VALUE, Record, Abbrev);
  1111. Record.clear();
  1112. }
  1113. static void writeDIGlobalVariable(const DIGlobalVariable *N,
  1114. const ValueEnumerator &VE,
  1115. BitstreamWriter &Stream,
  1116. SmallVectorImpl<uint64_t> &Record,
  1117. unsigned Abbrev) {
  1118. Record.push_back(N->isDistinct());
  1119. Record.push_back(VE.getMetadataOrNullID(N->getScope()));
  1120. Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
  1121. Record.push_back(VE.getMetadataOrNullID(N->getRawLinkageName()));
  1122. Record.push_back(VE.getMetadataOrNullID(N->getFile()));
  1123. Record.push_back(N->getLine());
  1124. Record.push_back(VE.getMetadataOrNullID(N->getType()));
  1125. Record.push_back(N->isLocalToUnit());
  1126. Record.push_back(N->isDefinition());
  1127. Record.push_back(VE.getMetadataOrNullID(N->getRawVariable()));
  1128. Record.push_back(VE.getMetadataOrNullID(N->getStaticDataMemberDeclaration()));
  1129. Stream.EmitRecord(bitc::METADATA_GLOBAL_VAR, Record, Abbrev);
  1130. Record.clear();
  1131. }
  1132. static void writeDILocalVariable(const DILocalVariable *N,
  1133. const ValueEnumerator &VE,
  1134. BitstreamWriter &Stream,
  1135. SmallVectorImpl<uint64_t> &Record,
  1136. unsigned Abbrev) {
  1137. Record.push_back(N->isDistinct());
  1138. Record.push_back(VE.getMetadataOrNullID(N->getScope()));
  1139. Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
  1140. Record.push_back(VE.getMetadataOrNullID(N->getFile()));
  1141. Record.push_back(N->getLine());
  1142. Record.push_back(VE.getMetadataOrNullID(N->getType()));
  1143. Record.push_back(N->getArg());
  1144. Record.push_back(N->getFlags());
  1145. Stream.EmitRecord(bitc::METADATA_LOCAL_VAR, Record, Abbrev);
  1146. Record.clear();
  1147. }
  1148. static void writeDIExpression(const DIExpression *N, const ValueEnumerator &,
  1149. BitstreamWriter &Stream,
  1150. SmallVectorImpl<uint64_t> &Record,
  1151. unsigned Abbrev) {
  1152. Record.reserve(N->getElements().size() + 1);
  1153. Record.push_back(N->isDistinct());
  1154. Record.append(N->elements_begin(), N->elements_end());
  1155. Stream.EmitRecord(bitc::METADATA_EXPRESSION, Record, Abbrev);
  1156. Record.clear();
  1157. }
  1158. static void writeDIObjCProperty(const DIObjCProperty *N,
  1159. const ValueEnumerator &VE,
  1160. BitstreamWriter &Stream,
  1161. SmallVectorImpl<uint64_t> &Record,
  1162. unsigned Abbrev) {
  1163. Record.push_back(N->isDistinct());
  1164. Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
  1165. Record.push_back(VE.getMetadataOrNullID(N->getFile()));
  1166. Record.push_back(N->getLine());
  1167. Record.push_back(VE.getMetadataOrNullID(N->getRawSetterName()));
  1168. Record.push_back(VE.getMetadataOrNullID(N->getRawGetterName()));
  1169. Record.push_back(N->getAttributes());
  1170. Record.push_back(VE.getMetadataOrNullID(N->getType()));
  1171. Stream.EmitRecord(bitc::METADATA_OBJC_PROPERTY, Record, Abbrev);
  1172. Record.clear();
  1173. }
  1174. static void writeDIImportedEntity(const DIImportedEntity *N,
  1175. const ValueEnumerator &VE,
  1176. BitstreamWriter &Stream,
  1177. SmallVectorImpl<uint64_t> &Record,
  1178. unsigned Abbrev) {
  1179. Record.push_back(N->isDistinct());
  1180. Record.push_back(N->getTag());
  1181. Record.push_back(VE.getMetadataOrNullID(N->getScope()));
  1182. Record.push_back(VE.getMetadataOrNullID(N->getEntity()));
  1183. Record.push_back(N->getLine());
  1184. Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
  1185. Stream.EmitRecord(bitc::METADATA_IMPORTED_ENTITY, Record, Abbrev);
  1186. Record.clear();
  1187. }
  1188. static unsigned createNamedMetadataAbbrev(BitstreamWriter &Stream) {
  1189. BitCodeAbbrev *Abbv = new BitCodeAbbrev();
  1190. Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_NAME));
  1191. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
  1192. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
  1193. return Stream.EmitAbbrev(Abbv);
  1194. }
  1195. static void writeNamedMetadata(const Module &M, const ValueEnumerator &VE,
  1196. BitstreamWriter &Stream,
  1197. SmallVectorImpl<uint64_t> &Record) {
  1198. if (M.named_metadata_empty())
  1199. return;
  1200. unsigned Abbrev = createNamedMetadataAbbrev(Stream);
  1201. for (const NamedMDNode &NMD : M.named_metadata()) {
  1202. // Write name.
  1203. StringRef Str = NMD.getName();
  1204. Record.append(Str.bytes_begin(), Str.bytes_end());
  1205. Stream.EmitRecord(bitc::METADATA_NAME, Record, Abbrev);
  1206. Record.clear();
  1207. // Write named metadata operands.
  1208. for (const MDNode *N : NMD.operands())
  1209. Record.push_back(VE.getMetadataID(N));
  1210. Stream.EmitRecord(bitc::METADATA_NAMED_NODE, Record, 0);
  1211. Record.clear();
  1212. }
  1213. }
  1214. static unsigned createMetadataStringsAbbrev(BitstreamWriter &Stream) {
  1215. BitCodeAbbrev *Abbv = new BitCodeAbbrev();
  1216. Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_STRINGS));
  1217. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of strings
  1218. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // offset to chars
  1219. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
  1220. return Stream.EmitAbbrev(Abbv);
  1221. }
  1222. /// Write out a record for MDString.
  1223. ///
  1224. /// All the metadata strings in a metadata block are emitted in a single
  1225. /// record. The sizes and strings themselves are shoved into a blob.
  1226. static void writeMetadataStrings(ArrayRef<const Metadata *> Strings,
  1227. BitstreamWriter &Stream,
  1228. SmallVectorImpl<uint64_t> &Record) {
  1229. if (Strings.empty())
  1230. return;
  1231. // Start the record with the number of strings.
  1232. Record.push_back(bitc::METADATA_STRINGS);
  1233. Record.push_back(Strings.size());
  1234. // Emit the sizes of the strings in the blob.
  1235. SmallString<256> Blob;
  1236. {
  1237. BitstreamWriter W(Blob);
  1238. for (const Metadata *MD : Strings)
  1239. W.EmitVBR(cast<MDString>(MD)->getLength(), 6);
  1240. W.FlushToWord();
  1241. }
  1242. // Add the offset to the strings to the record.
  1243. Record.push_back(Blob.size());
  1244. // Add the strings to the blob.
  1245. for (const Metadata *MD : Strings)
  1246. Blob.append(cast<MDString>(MD)->getString());
  1247. // Emit the final record.
  1248. Stream.EmitRecordWithBlob(createMetadataStringsAbbrev(Stream), Record, Blob);
  1249. Record.clear();
  1250. }
  1251. static void writeMetadataRecords(ArrayRef<const Metadata *> MDs,
  1252. const ValueEnumerator &VE,
  1253. BitstreamWriter &Stream,
  1254. SmallVectorImpl<uint64_t> &Record) {
  1255. if (MDs.empty())
  1256. return;
  1257. // Initialize MDNode abbreviations.
  1258. #define HANDLE_MDNODE_LEAF(CLASS) unsigned CLASS##Abbrev = 0;
  1259. #include "llvm/IR/Metadata.def"
  1260. for (const Metadata *MD : MDs) {
  1261. if (const MDNode *N = dyn_cast<MDNode>(MD)) {
  1262. assert(N->isResolved() && "Expected forward references to be resolved");
  1263. switch (N->getMetadataID()) {
  1264. default:
  1265. llvm_unreachable("Invalid MDNode subclass");
  1266. #define HANDLE_MDNODE_LEAF(CLASS) \
  1267. case Metadata::CLASS##Kind: \
  1268. write##CLASS(cast<CLASS>(N), VE, Stream, Record, CLASS##Abbrev); \
  1269. continue;
  1270. #include "llvm/IR/Metadata.def"
  1271. }
  1272. }
  1273. writeValueAsMetadata(cast<ValueAsMetadata>(MD), VE, Stream, Record);
  1274. }
  1275. }
  1276. static void writeModuleMetadata(const Module &M,
  1277. const ValueEnumerator &VE,
  1278. BitstreamWriter &Stream) {
  1279. if (VE.getMDs().empty() && M.named_metadata_empty())
  1280. return;
  1281. Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
  1282. SmallVector<uint64_t, 64> Record;
  1283. writeMetadataStrings(VE.getMDStrings(), Stream, Record);
  1284. writeMetadataRecords(VE.getNonMDStrings(), VE, Stream, Record);
  1285. writeNamedMetadata(M, VE, Stream, Record);
  1286. Stream.ExitBlock();
  1287. }
  1288. static void writeFunctionMetadata(const Function &F, const ValueEnumerator &VE,
  1289. BitstreamWriter &Stream) {
  1290. ArrayRef<const Metadata *> MDs = VE.getFunctionMDs();
  1291. if (MDs.empty())
  1292. return;
  1293. Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
  1294. SmallVector<uint64_t, 64> Record;
  1295. writeMetadataRecords(MDs, VE, Stream, Record);
  1296. Stream.ExitBlock();
  1297. }
  1298. static void WriteMetadataAttachment(const Function &F,
  1299. const ValueEnumerator &VE,
  1300. BitstreamWriter &Stream) {
  1301. Stream.EnterSubblock(bitc::METADATA_ATTACHMENT_ID, 3);
  1302. SmallVector<uint64_t, 64> Record;
  1303. // Write metadata attachments
  1304. // METADATA_ATTACHMENT - [m x [value, [n x [id, mdnode]]]
  1305. SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
  1306. F.getAllMetadata(MDs);
  1307. if (!MDs.empty()) {
  1308. for (const auto &I : MDs) {
  1309. Record.push_back(I.first);
  1310. Record.push_back(VE.getMetadataID(I.second));
  1311. }
  1312. Stream.EmitRecord(bitc::METADATA_ATTACHMENT, Record, 0);
  1313. Record.clear();
  1314. }
  1315. for (const BasicBlock &BB : F)
  1316. for (const Instruction &I : BB) {
  1317. MDs.clear();
  1318. I.getAllMetadataOtherThanDebugLoc(MDs);
  1319. // If no metadata, ignore instruction.
  1320. if (MDs.empty()) continue;
  1321. Record.push_back(VE.getInstructionID(&I));
  1322. for (unsigned i = 0, e = MDs.size(); i != e; ++i) {
  1323. Record.push_back(MDs[i].first);
  1324. Record.push_back(VE.getMetadataID(MDs[i].second));
  1325. }
  1326. Stream.EmitRecord(bitc::METADATA_ATTACHMENT, Record, 0);
  1327. Record.clear();
  1328. }
  1329. Stream.ExitBlock();
  1330. }
  1331. static void WriteModuleMetadataStore(const Module *M, BitstreamWriter &Stream) {
  1332. SmallVector<uint64_t, 64> Record;
  1333. // Write metadata kinds
  1334. // METADATA_KIND - [n x [id, name]]
  1335. SmallVector<StringRef, 8> Names;
  1336. M->getMDKindNames(Names);
  1337. if (Names.empty()) return;
  1338. Stream.EnterSubblock(bitc::METADATA_KIND_BLOCK_ID, 3);
  1339. for (unsigned MDKindID = 0, e = Names.size(); MDKindID != e; ++MDKindID) {
  1340. Record.push_back(MDKindID);
  1341. StringRef KName = Names[MDKindID];
  1342. Record.append(KName.begin(), KName.end());
  1343. Stream.EmitRecord(bitc::METADATA_KIND, Record, 0);
  1344. Record.clear();
  1345. }
  1346. Stream.ExitBlock();
  1347. }
  1348. static void WriteOperandBundleTags(const Module *M, BitstreamWriter &Stream) {
  1349. // Write metadata kinds
  1350. //
  1351. // OPERAND_BUNDLE_TAGS_BLOCK_ID : N x OPERAND_BUNDLE_TAG
  1352. //
  1353. // OPERAND_BUNDLE_TAG - [strchr x N]
  1354. SmallVector<StringRef, 8> Tags;
  1355. M->getOperandBundleTags(Tags);
  1356. if (Tags.empty())
  1357. return;
  1358. Stream.EnterSubblock(bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID, 3);
  1359. SmallVector<uint64_t, 64> Record;
  1360. for (auto Tag : Tags) {
  1361. Record.append(Tag.begin(), Tag.end());
  1362. Stream.EmitRecord(bitc::OPERAND_BUNDLE_TAG, Record, 0);
  1363. Record.clear();
  1364. }
  1365. Stream.ExitBlock();
  1366. }
  1367. static void emitSignedInt64(SmallVectorImpl<uint64_t> &Vals, uint64_t V) {
  1368. if ((int64_t)V >= 0)
  1369. Vals.push_back(V << 1);
  1370. else
  1371. Vals.push_back((-V << 1) | 1);
  1372. }
  1373. static void WriteConstants(unsigned FirstVal, unsigned LastVal,
  1374. const ValueEnumerator &VE,
  1375. BitstreamWriter &Stream, bool isGlobal) {
  1376. if (FirstVal == LastVal) return;
  1377. Stream.EnterSubblock(bitc::CONSTANTS_BLOCK_ID, 4);
  1378. unsigned AggregateAbbrev = 0;
  1379. unsigned String8Abbrev = 0;
  1380. unsigned CString7Abbrev = 0;
  1381. unsigned CString6Abbrev = 0;
  1382. // If this is a constant pool for the module, emit module-specific abbrevs.
  1383. if (isGlobal) {
  1384. // Abbrev for CST_CODE_AGGREGATE.
  1385. BitCodeAbbrev *Abbv = new BitCodeAbbrev();
  1386. Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_AGGREGATE));
  1387. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
  1388. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, Log2_32_Ceil(LastVal+1)));
  1389. AggregateAbbrev = Stream.EmitAbbrev(Abbv);
  1390. // Abbrev for CST_CODE_STRING.
  1391. Abbv = new BitCodeAbbrev();
  1392. Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_STRING));
  1393. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
  1394. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
  1395. String8Abbrev = Stream.EmitAbbrev(Abbv);
  1396. // Abbrev for CST_CODE_CSTRING.
  1397. Abbv = new BitCodeAbbrev();
  1398. Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
  1399. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
  1400. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
  1401. CString7Abbrev = Stream.EmitAbbrev(Abbv);
  1402. // Abbrev for CST_CODE_CSTRING.
  1403. Abbv = new BitCodeAbbrev();
  1404. Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
  1405. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
  1406. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
  1407. CString6Abbrev = Stream.EmitAbbrev(Abbv);
  1408. }
  1409. SmallVector<uint64_t, 64> Record;
  1410. const ValueEnumerator::ValueList &Vals = VE.getValues();
  1411. Type *LastTy = nullptr;
  1412. for (unsigned i = FirstVal; i != LastVal; ++i) {
  1413. const Value *V = Vals[i].first;
  1414. // If we need to switch types, do so now.
  1415. if (V->getType() != LastTy) {
  1416. LastTy = V->getType();
  1417. Record.push_back(VE.getTypeID(LastTy));
  1418. Stream.EmitRecord(bitc::CST_CODE_SETTYPE, Record,
  1419. CONSTANTS_SETTYPE_ABBREV);
  1420. Record.clear();
  1421. }
  1422. if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
  1423. Record.push_back(unsigned(IA->hasSideEffects()) |
  1424. unsigned(IA->isAlignStack()) << 1 |
  1425. unsigned(IA->getDialect()&1) << 2);
  1426. // Add the asm string.
  1427. const std::string &AsmStr = IA->getAsmString();
  1428. Record.push_back(AsmStr.size());
  1429. Record.append(AsmStr.begin(), AsmStr.end());
  1430. // Add the constraint string.
  1431. const std::string &ConstraintStr = IA->getConstraintString();
  1432. Record.push_back(ConstraintStr.size());
  1433. Record.append(ConstraintStr.begin(), ConstraintStr.end());
  1434. Stream.EmitRecord(bitc::CST_CODE_INLINEASM, Record);
  1435. Record.clear();
  1436. continue;
  1437. }
  1438. const Constant *C = cast<Constant>(V);
  1439. unsigned Code = -1U;
  1440. unsigned AbbrevToUse = 0;
  1441. if (C->isNullValue()) {
  1442. Code = bitc::CST_CODE_NULL;
  1443. } else if (isa<UndefValue>(C)) {
  1444. Code = bitc::CST_CODE_UNDEF;
  1445. } else if (const ConstantInt *IV = dyn_cast<ConstantInt>(C)) {
  1446. if (IV->getBitWidth() <= 64) {
  1447. uint64_t V = IV->getSExtValue();
  1448. emitSignedInt64(Record, V);
  1449. Code = bitc::CST_CODE_INTEGER;
  1450. AbbrevToUse = CONSTANTS_INTEGER_ABBREV;
  1451. } else { // Wide integers, > 64 bits in size.
  1452. // We have an arbitrary precision integer value to write whose
  1453. // bit width is > 64. However, in canonical unsigned integer
  1454. // format it is likely that the high bits are going to be zero.
  1455. // So, we only write the number of active words.
  1456. unsigned NWords = IV->getValue().getActiveWords();
  1457. const uint64_t *RawWords = IV->getValue().getRawData();
  1458. for (unsigned i = 0; i != NWords; ++i) {
  1459. emitSignedInt64(Record, RawWords[i]);
  1460. }
  1461. Code = bitc::CST_CODE_WIDE_INTEGER;
  1462. }
  1463. } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
  1464. Code = bitc::CST_CODE_FLOAT;
  1465. Type *Ty = CFP->getType();
  1466. if (Ty->isHalfTy() || Ty->isFloatTy() || Ty->isDoubleTy()) {
  1467. Record.push_back(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
  1468. } else if (Ty->isX86_FP80Ty()) {
  1469. // api needed to prevent premature destruction
  1470. // bits are not in the same order as a normal i80 APInt, compensate.
  1471. APInt api = CFP->getValueAPF().bitcastToAPInt();
  1472. const uint64_t *p = api.getRawData();
  1473. Record.push_back((p[1] << 48) | (p[0] >> 16));
  1474. Record.push_back(p[0] & 0xffffLL);
  1475. } else if (Ty->isFP128Ty() || Ty->isPPC_FP128Ty()) {
  1476. APInt api = CFP->getValueAPF().bitcastToAPInt();
  1477. const uint64_t *p = api.getRawData();
  1478. Record.push_back(p[0]);
  1479. Record.push_back(p[1]);
  1480. } else {
  1481. assert (0 && "Unknown FP type!");
  1482. }
  1483. } else if (isa<ConstantDataSequential>(C) &&
  1484. cast<ConstantDataSequential>(C)->isString()) {
  1485. const ConstantDataSequential *Str = cast<ConstantDataSequential>(C);
  1486. // Emit constant strings specially.
  1487. unsigned NumElts = Str->getNumElements();
  1488. // If this is a null-terminated string, use the denser CSTRING encoding.
  1489. if (Str->isCString()) {
  1490. Code = bitc::CST_CODE_CSTRING;
  1491. --NumElts; // Don't encode the null, which isn't allowed by char6.
  1492. } else {
  1493. Code = bitc::CST_CODE_STRING;
  1494. AbbrevToUse = String8Abbrev;
  1495. }
  1496. bool isCStr7 = Code == bitc::CST_CODE_CSTRING;
  1497. bool isCStrChar6 = Code == bitc::CST_CODE_CSTRING;
  1498. for (unsigned i = 0; i != NumElts; ++i) {
  1499. unsigned char V = Str->getElementAsInteger(i);
  1500. Record.push_back(V);
  1501. isCStr7 &= (V & 128) == 0;
  1502. if (isCStrChar6)
  1503. isCStrChar6 = BitCodeAbbrevOp::isChar6(V);
  1504. }
  1505. if (isCStrChar6)
  1506. AbbrevToUse = CString6Abbrev;
  1507. else if (isCStr7)
  1508. AbbrevToUse = CString7Abbrev;
  1509. } else if (const ConstantDataSequential *CDS =
  1510. dyn_cast<ConstantDataSequential>(C)) {
  1511. Code = bitc::CST_CODE_DATA;
  1512. Type *EltTy = CDS->getType()->getElementType();
  1513. if (isa<IntegerType>(EltTy)) {
  1514. for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i)
  1515. Record.push_back(CDS->getElementAsInteger(i));
  1516. } else {
  1517. for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i)
  1518. Record.push_back(
  1519. CDS->getElementAsAPFloat(i).bitcastToAPInt().getLimitedValue());
  1520. }
  1521. } else if (isa<ConstantArray>(C) || isa<ConstantStruct>(C) ||
  1522. isa<ConstantVector>(C)) {
  1523. Code = bitc::CST_CODE_AGGREGATE;
  1524. for (const Value *Op : C->operands())
  1525. Record.push_back(VE.getValueID(Op));
  1526. AbbrevToUse = AggregateAbbrev;
  1527. } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
  1528. switch (CE->getOpcode()) {
  1529. default:
  1530. if (Instruction::isCast(CE->getOpcode())) {
  1531. Code = bitc::CST_CODE_CE_CAST;
  1532. Record.push_back(GetEncodedCastOpcode(CE->getOpcode()));
  1533. Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
  1534. Record.push_back(VE.getValueID(C->getOperand(0)));
  1535. AbbrevToUse = CONSTANTS_CE_CAST_Abbrev;
  1536. } else {
  1537. assert(CE->getNumOperands() == 2 && "Unknown constant expr!");
  1538. Code = bitc::CST_CODE_CE_BINOP;
  1539. Record.push_back(GetEncodedBinaryOpcode(CE->getOpcode()));
  1540. Record.push_back(VE.getValueID(C->getOperand(0)));
  1541. Record.push_back(VE.getValueID(C->getOperand(1)));
  1542. uint64_t Flags = GetOptimizationFlags(CE);
  1543. if (Flags != 0)
  1544. Record.push_back(Flags);
  1545. }
  1546. break;
  1547. case Instruction::GetElementPtr: {
  1548. Code = bitc::CST_CODE_CE_GEP;
  1549. const auto *GO = cast<GEPOperator>(C);
  1550. if (GO->isInBounds())
  1551. Code = bitc::CST_CODE_CE_INBOUNDS_GEP;
  1552. Record.push_back(VE.getTypeID(GO->getSourceElementType()));
  1553. for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) {
  1554. Record.push_back(VE.getTypeID(C->getOperand(i)->getType()));
  1555. Record.push_back(VE.getValueID(C->getOperand(i)));
  1556. }
  1557. break;
  1558. }
  1559. case Instruction::Select:
  1560. Code = bitc::CST_CODE_CE_SELECT;
  1561. Record.push_back(VE.getValueID(C->getOperand(0)));
  1562. Record.push_back(VE.getValueID(C->getOperand(1)));
  1563. Record.push_back(VE.getValueID(C->getOperand(2)));
  1564. break;
  1565. case Instruction::ExtractElement:
  1566. Code = bitc::CST_CODE_CE_EXTRACTELT;
  1567. Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
  1568. Record.push_back(VE.getValueID(C->getOperand(0)));
  1569. Record.push_back(VE.getTypeID(C->getOperand(1)->getType()));
  1570. Record.push_back(VE.getValueID(C->getOperand(1)));
  1571. break;
  1572. case Instruction::InsertElement:
  1573. Code = bitc::CST_CODE_CE_INSERTELT;
  1574. Record.push_back(VE.getValueID(C->getOperand(0)));
  1575. Record.push_back(VE.getValueID(C->getOperand(1)));
  1576. Record.push_back(VE.getTypeID(C->getOperand(2)->getType()));
  1577. Record.push_back(VE.getValueID(C->getOperand(2)));
  1578. break;
  1579. case Instruction::ShuffleVector:
  1580. // If the return type and argument types are the same, this is a
  1581. // standard shufflevector instruction. If the types are different,
  1582. // then the shuffle is widening or truncating the input vectors, and
  1583. // the argument type must also be encoded.
  1584. if (C->getType() == C->getOperand(0)->getType()) {
  1585. Code = bitc::CST_CODE_CE_SHUFFLEVEC;
  1586. } else {
  1587. Code = bitc::CST_CODE_CE_SHUFVEC_EX;
  1588. Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
  1589. }
  1590. Record.push_back(VE.getValueID(C->getOperand(0)));
  1591. Record.push_back(VE.getValueID(C->getOperand(1)));
  1592. Record.push_back(VE.getValueID(C->getOperand(2)));
  1593. break;
  1594. case Instruction::ICmp:
  1595. case Instruction::FCmp:
  1596. Code = bitc::CST_CODE_CE_CMP;
  1597. Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
  1598. Record.push_back(VE.getValueID(C->getOperand(0)));
  1599. Record.push_back(VE.getValueID(C->getOperand(1)));
  1600. Record.push_back(CE->getPredicate());
  1601. break;
  1602. }
  1603. } else if (const BlockAddress *BA = dyn_cast<BlockAddress>(C)) {
  1604. Code = bitc::CST_CODE_BLOCKADDRESS;
  1605. Record.push_back(VE.getTypeID(BA->getFunction()->getType()));
  1606. Record.push_back(VE.getValueID(BA->getFunction()));
  1607. Record.push_back(VE.getGlobalBasicBlockID(BA->getBasicBlock()));
  1608. } else {
  1609. #ifndef NDEBUG
  1610. C->dump();
  1611. #endif
  1612. llvm_unreachable("Unknown constant!");
  1613. }
  1614. Stream.EmitRecord(Code, Record, AbbrevToUse);
  1615. Record.clear();
  1616. }
  1617. Stream.ExitBlock();
  1618. }
  1619. static void WriteModuleConstants(const ValueEnumerator &VE,
  1620. BitstreamWriter &Stream) {
  1621. const ValueEnumerator::ValueList &Vals = VE.getValues();
  1622. // Find the first constant to emit, which is the first non-globalvalue value.
  1623. // We know globalvalues have been emitted by WriteModuleInfo.
  1624. for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
  1625. if (!isa<GlobalValue>(Vals[i].first)) {
  1626. WriteConstants(i, Vals.size(), VE, Stream, true);
  1627. return;
  1628. }
  1629. }
  1630. }
  1631. /// PushValueAndType - The file has to encode both the value and type id for
  1632. /// many values, because we need to know what type to create for forward
  1633. /// references. However, most operands are not forward references, so this type
  1634. /// field is not needed.
  1635. ///
  1636. /// This function adds V's value ID to Vals. If the value ID is higher than the
  1637. /// instruction ID, then it is a forward reference, and it also includes the
  1638. /// type ID. The value ID that is written is encoded relative to the InstID.
  1639. static bool PushValueAndType(const Value *V, unsigned InstID,
  1640. SmallVectorImpl<unsigned> &Vals,
  1641. ValueEnumerator &VE) {
  1642. unsigned ValID = VE.getValueID(V);
  1643. // Make encoding relative to the InstID.
  1644. Vals.push_back(InstID - ValID);
  1645. if (ValID >= InstID) {
  1646. Vals.push_back(VE.getTypeID(V->getType()));
  1647. return true;
  1648. }
  1649. return false;
  1650. }
  1651. static void WriteOperandBundles(BitstreamWriter &Stream, ImmutableCallSite CS,
  1652. unsigned InstID, ValueEnumerator &VE) {
  1653. SmallVector<unsigned, 64> Record;
  1654. LLVMContext &C = CS.getInstruction()->getContext();
  1655. for (unsigned i = 0, e = CS.getNumOperandBundles(); i != e; ++i) {
  1656. const auto &Bundle = CS.getOperandBundleAt(i);
  1657. Record.push_back(C.getOperandBundleTagID(Bundle.getTagName()));
  1658. for (auto &Input : Bundle.Inputs)
  1659. PushValueAndType(Input, InstID, Record, VE);
  1660. Stream.EmitRecord(bitc::FUNC_CODE_OPERAND_BUNDLE, Record);
  1661. Record.clear();
  1662. }
  1663. }
  1664. /// pushValue - Like PushValueAndType, but where the type of the value is
  1665. /// omitted (perhaps it was already encoded in an earlier operand).
  1666. static void pushValue(const Value *V, unsigned InstID,
  1667. SmallVectorImpl<unsigned> &Vals,
  1668. ValueEnumerator &VE) {
  1669. unsigned ValID = VE.getValueID(V);
  1670. Vals.push_back(InstID - ValID);
  1671. }
  1672. static void pushValueSigned(const Value *V, unsigned InstID,
  1673. SmallVectorImpl<uint64_t> &Vals,
  1674. ValueEnumerator &VE) {
  1675. unsigned ValID = VE.getValueID(V);
  1676. int64_t diff = ((int32_t)InstID - (int32_t)ValID);
  1677. emitSignedInt64(Vals, diff);
  1678. }
  1679. /// WriteInstruction - Emit an instruction to the specified stream.
  1680. static void WriteInstruction(const Instruction &I, unsigned InstID,
  1681. ValueEnumerator &VE, BitstreamWriter &Stream,
  1682. SmallVectorImpl<unsigned> &Vals) {
  1683. unsigned Code = 0;
  1684. unsigned AbbrevToUse = 0;
  1685. VE.setInstructionID(&I);
  1686. switch (I.getOpcode()) {
  1687. default:
  1688. if (Instruction::isCast(I.getOpcode())) {
  1689. Code = bitc::FUNC_CODE_INST_CAST;
  1690. if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE))
  1691. AbbrevToUse = FUNCTION_INST_CAST_ABBREV;
  1692. Vals.push_back(VE.getTypeID(I.getType()));
  1693. Vals.push_back(GetEncodedCastOpcode(I.getOpcode()));
  1694. } else {
  1695. assert(isa<BinaryOperator>(I) && "Unknown instruction!");
  1696. Code = bitc::FUNC_CODE_INST_BINOP;
  1697. if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE))
  1698. AbbrevToUse = FUNCTION_INST_BINOP_ABBREV;
  1699. pushValue(I.getOperand(1), InstID, Vals, VE);
  1700. Vals.push_back(GetEncodedBinaryOpcode(I.getOpcode()));
  1701. uint64_t Flags = GetOptimizationFlags(&I);
  1702. if (Flags != 0) {
  1703. if (AbbrevToUse == FUNCTION_INST_BINOP_ABBREV)
  1704. AbbrevToUse = FUNCTION_INST_BINOP_FLAGS_ABBREV;
  1705. Vals.push_back(Flags);
  1706. }
  1707. }
  1708. break;
  1709. case Instruction::GetElementPtr: {
  1710. Code = bitc::FUNC_CODE_INST_GEP;
  1711. AbbrevToUse = FUNCTION_INST_GEP_ABBREV;
  1712. auto &GEPInst = cast<GetElementPtrInst>(I);
  1713. Vals.push_back(GEPInst.isInBounds());
  1714. Vals.push_back(VE.getTypeID(GEPInst.getSourceElementType()));
  1715. for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
  1716. PushValueAndType(I.getOperand(i), InstID, Vals, VE);
  1717. break;
  1718. }
  1719. case Instruction::ExtractValue: {
  1720. Code = bitc::FUNC_CODE_INST_EXTRACTVAL;
  1721. PushValueAndType(I.getOperand(0), InstID, Vals, VE);
  1722. const ExtractValueInst *EVI = cast<ExtractValueInst>(&I);
  1723. Vals.append(EVI->idx_begin(), EVI->idx_end());
  1724. break;
  1725. }
  1726. case Instruction::InsertValue: {
  1727. Code = bitc::FUNC_CODE_INST_INSERTVAL;
  1728. PushValueAndType(I.getOperand(0), InstID, Vals, VE);
  1729. PushValueAndType(I.getOperand(1), InstID, Vals, VE);
  1730. const InsertValueInst *IVI = cast<InsertValueInst>(&I);
  1731. Vals.append(IVI->idx_begin(), IVI->idx_end());
  1732. break;
  1733. }
  1734. case Instruction::Select:
  1735. Code = bitc::FUNC_CODE_INST_VSELECT;
  1736. PushValueAndType(I.getOperand(1), InstID, Vals, VE);
  1737. pushValue(I.getOperand(2), InstID, Vals, VE);
  1738. PushValueAndType(I.getOperand(0), InstID, Vals, VE);
  1739. break;
  1740. case Instruction::ExtractElement:
  1741. Code = bitc::FUNC_CODE_INST_EXTRACTELT;
  1742. PushValueAndType(I.getOperand(0), InstID, Vals, VE);
  1743. PushValueAndType(I.getOperand(1), InstID, Vals, VE);
  1744. break;
  1745. case Instruction::InsertElement:
  1746. Code = bitc::FUNC_CODE_INST_INSERTELT;
  1747. PushValueAndType(I.getOperand(0), InstID, Vals, VE);
  1748. pushValue(I.getOperand(1), InstID, Vals, VE);
  1749. PushValueAndType(I.getOperand(2), InstID, Vals, VE);
  1750. break;
  1751. case Instruction::ShuffleVector:
  1752. Code = bitc::FUNC_CODE_INST_SHUFFLEVEC;
  1753. PushValueAndType(I.getOperand(0), InstID, Vals, VE);
  1754. pushValue(I.getOperand(1), InstID, Vals, VE);
  1755. pushValue(I.getOperand(2), InstID, Vals, VE);
  1756. break;
  1757. case Instruction::ICmp:
  1758. case Instruction::FCmp: {
  1759. // compare returning Int1Ty or vector of Int1Ty
  1760. Code = bitc::FUNC_CODE_INST_CMP2;
  1761. PushValueAndType(I.getOperand(0), InstID, Vals, VE);
  1762. pushValue(I.getOperand(1), InstID, Vals, VE);
  1763. Vals.push_back(cast<CmpInst>(I).getPredicate());
  1764. uint64_t Flags = GetOptimizationFlags(&I);
  1765. if (Flags != 0)
  1766. Vals.push_back(Flags);
  1767. break;
  1768. }
  1769. case Instruction::Ret:
  1770. {
  1771. Code = bitc::FUNC_CODE_INST_RET;
  1772. unsigned NumOperands = I.getNumOperands();
  1773. if (NumOperands == 0)
  1774. AbbrevToUse = FUNCTION_INST_RET_VOID_ABBREV;
  1775. else if (NumOperands == 1) {
  1776. if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE))
  1777. AbbrevToUse = FUNCTION_INST_RET_VAL_ABBREV;
  1778. } else {
  1779. for (unsigned i = 0, e = NumOperands; i != e; ++i)
  1780. PushValueAndType(I.getOperand(i), InstID, Vals, VE);
  1781. }
  1782. }
  1783. break;
  1784. case Instruction::Br:
  1785. {
  1786. Code = bitc::FUNC_CODE_INST_BR;
  1787. const BranchInst &II = cast<BranchInst>(I);
  1788. Vals.push_back(VE.getValueID(II.getSuccessor(0)));
  1789. if (II.isConditional()) {
  1790. Vals.push_back(VE.getValueID(II.getSuccessor(1)));
  1791. pushValue(II.getCondition(), InstID, Vals, VE);
  1792. }
  1793. }
  1794. break;
  1795. case Instruction::Switch:
  1796. {
  1797. Code = bitc::FUNC_CODE_INST_SWITCH;
  1798. const SwitchInst &SI = cast<SwitchInst>(I);
  1799. Vals.push_back(VE.getTypeID(SI.getCondition()->getType()));
  1800. pushValue(SI.getCondition(), InstID, Vals, VE);
  1801. Vals.push_back(VE.getValueID(SI.getDefaultDest()));
  1802. for (SwitchInst::ConstCaseIt Case : SI.cases()) {
  1803. Vals.push_back(VE.getValueID(Case.getCaseValue()));
  1804. Vals.push_back(VE.getValueID(Case.getCaseSuccessor()));
  1805. }
  1806. }
  1807. break;
  1808. case Instruction::IndirectBr:
  1809. Code = bitc::FUNC_CODE_INST_INDIRECTBR;
  1810. Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
  1811. // Encode the address operand as relative, but not the basic blocks.
  1812. pushValue(I.getOperand(0), InstID, Vals, VE);
  1813. for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i)
  1814. Vals.push_back(VE.getValueID(I.getOperand(i)));
  1815. break;
  1816. case Instruction::Invoke: {
  1817. const InvokeInst *II = cast<InvokeInst>(&I);
  1818. const Value *Callee = II->getCalledValue();
  1819. FunctionType *FTy = II->getFunctionType();
  1820. if (II->hasOperandBundles())
  1821. WriteOperandBundles(Stream, II, InstID, VE);
  1822. Code = bitc::FUNC_CODE_INST_INVOKE;
  1823. Vals.push_back(VE.getAttributeID(II->getAttributes()));
  1824. Vals.push_back(II->getCallingConv() | 1 << 13);
  1825. Vals.push_back(VE.getValueID(II->getNormalDest()));
  1826. Vals.push_back(VE.getValueID(II->getUnwindDest()));
  1827. Vals.push_back(VE.getTypeID(FTy));
  1828. PushValueAndType(Callee, InstID, Vals, VE);
  1829. // Emit value #'s for the fixed parameters.
  1830. for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
  1831. pushValue(I.getOperand(i), InstID, Vals, VE); // fixed param.
  1832. // Emit type/value pairs for varargs params.
  1833. if (FTy->isVarArg()) {
  1834. for (unsigned i = FTy->getNumParams(), e = I.getNumOperands()-3;
  1835. i != e; ++i)
  1836. PushValueAndType(I.getOperand(i), InstID, Vals, VE); // vararg
  1837. }
  1838. break;
  1839. }
  1840. case Instruction::Resume:
  1841. Code = bitc::FUNC_CODE_INST_RESUME;
  1842. PushValueAndType(I.getOperand(0), InstID, Vals, VE);
  1843. break;
  1844. case Instruction::CleanupRet: {
  1845. Code = bitc::FUNC_CODE_INST_CLEANUPRET;
  1846. const auto &CRI = cast<CleanupReturnInst>(I);
  1847. pushValue(CRI.getCleanupPad(), InstID, Vals, VE);
  1848. if (CRI.hasUnwindDest())
  1849. Vals.push_back(VE.getValueID(CRI.getUnwindDest()));
  1850. break;
  1851. }
  1852. case Instruction::CatchRet: {
  1853. Code = bitc::FUNC_CODE_INST_CATCHRET;
  1854. const auto &CRI = cast<CatchReturnInst>(I);
  1855. pushValue(CRI.getCatchPad(), InstID, Vals, VE);
  1856. Vals.push_back(VE.getValueID(CRI.getSuccessor()));
  1857. break;
  1858. }
  1859. case Instruction::CleanupPad:
  1860. case Instruction::CatchPad: {
  1861. const auto &FuncletPad = cast<FuncletPadInst>(I);
  1862. Code = isa<CatchPadInst>(FuncletPad) ? bitc::FUNC_CODE_INST_CATCHPAD
  1863. : bitc::FUNC_CODE_INST_CLEANUPPAD;
  1864. pushValue(FuncletPad.getParentPad(), InstID, Vals, VE);
  1865. unsigned NumArgOperands = FuncletPad.getNumArgOperands();
  1866. Vals.push_back(NumArgOperands);
  1867. for (unsigned Op = 0; Op != NumArgOperands; ++Op)
  1868. PushValueAndType(FuncletPad.getArgOperand(Op), InstID, Vals, VE);
  1869. break;
  1870. }
  1871. case Instruction::CatchSwitch: {
  1872. Code = bitc::FUNC_CODE_INST_CATCHSWITCH;
  1873. const auto &CatchSwitch = cast<CatchSwitchInst>(I);
  1874. pushValue(CatchSwitch.getParentPad(), InstID, Vals, VE);
  1875. unsigned NumHandlers = CatchSwitch.getNumHandlers();
  1876. Vals.push_back(NumHandlers);
  1877. for (const BasicBlock *CatchPadBB : CatchSwitch.handlers())
  1878. Vals.push_back(VE.getValueID(CatchPadBB));
  1879. if (CatchSwitch.hasUnwindDest())
  1880. Vals.push_back(VE.getValueID(CatchSwitch.getUnwindDest()));
  1881. break;
  1882. }
  1883. case Instruction::Unreachable:
  1884. Code = bitc::FUNC_CODE_INST_UNREACHABLE;
  1885. AbbrevToUse = FUNCTION_INST_UNREACHABLE_ABBREV;
  1886. break;
  1887. case Instruction::PHI: {
  1888. const PHINode &PN = cast<PHINode>(I);
  1889. Code = bitc::FUNC_CODE_INST_PHI;
  1890. // With the newer instruction encoding, forward references could give
  1891. // negative valued IDs. This is most common for PHIs, so we use
  1892. // signed VBRs.
  1893. SmallVector<uint64_t, 128> Vals64;
  1894. Vals64.push_back(VE.getTypeID(PN.getType()));
  1895. for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
  1896. pushValueSigned(PN.getIncomingValue(i), InstID, Vals64, VE);
  1897. Vals64.push_back(VE.getValueID(PN.getIncomingBlock(i)));
  1898. }
  1899. // Emit a Vals64 vector and exit.
  1900. Stream.EmitRecord(Code, Vals64, AbbrevToUse);
  1901. Vals64.clear();
  1902. return;
  1903. }
  1904. case Instruction::LandingPad: {
  1905. const LandingPadInst &LP = cast<LandingPadInst>(I);
  1906. Code = bitc::FUNC_CODE_INST_LANDINGPAD;
  1907. Vals.push_back(VE.getTypeID(LP.getType()));
  1908. Vals.push_back(LP.isCleanup());
  1909. Vals.push_back(LP.getNumClauses());
  1910. for (unsigned I = 0, E = LP.getNumClauses(); I != E; ++I) {
  1911. if (LP.isCatch(I))
  1912. Vals.push_back(LandingPadInst::Catch);
  1913. else
  1914. Vals.push_back(LandingPadInst::Filter);
  1915. PushValueAndType(LP.getClause(I), InstID, Vals, VE);
  1916. }
  1917. break;
  1918. }
  1919. case Instruction::Alloca: {
  1920. Code = bitc::FUNC_CODE_INST_ALLOCA;
  1921. const AllocaInst &AI = cast<AllocaInst>(I);
  1922. Vals.push_back(VE.getTypeID(AI.getAllocatedType()));
  1923. Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
  1924. Vals.push_back(VE.getValueID(I.getOperand(0))); // size.
  1925. unsigned AlignRecord = Log2_32(AI.getAlignment()) + 1;
  1926. assert(Log2_32(Value::MaximumAlignment) + 1 < 1 << 5 &&
  1927. "not enough bits for maximum alignment");
  1928. assert(AlignRecord < 1 << 5 && "alignment greater than 1 << 64");
  1929. AlignRecord |= AI.isUsedWithInAlloca() << 5;
  1930. AlignRecord |= 1 << 6;
  1931. // Reserve bit 7 for SwiftError flag.
  1932. // AlignRecord |= AI.isSwiftError() << 7;
  1933. Vals.push_back(AlignRecord);
  1934. break;
  1935. }
  1936. case Instruction::Load:
  1937. if (cast<LoadInst>(I).isAtomic()) {
  1938. Code = bitc::FUNC_CODE_INST_LOADATOMIC;
  1939. PushValueAndType(I.getOperand(0), InstID, Vals, VE);
  1940. } else {
  1941. Code = bitc::FUNC_CODE_INST_LOAD;
  1942. if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE)) // ptr
  1943. AbbrevToUse = FUNCTION_INST_LOAD_ABBREV;
  1944. }
  1945. Vals.push_back(VE.getTypeID(I.getType()));
  1946. Vals.push_back(Log2_32(cast<LoadInst>(I).getAlignment())+1);
  1947. Vals.push_back(cast<LoadInst>(I).isVolatile());
  1948. if (cast<LoadInst>(I).isAtomic()) {
  1949. Vals.push_back(GetEncodedOrdering(cast<LoadInst>(I).getOrdering()));
  1950. Vals.push_back(GetEncodedSynchScope(cast<LoadInst>(I).getSynchScope()));
  1951. }
  1952. break;
  1953. case Instruction::Store:
  1954. if (cast<StoreInst>(I).isAtomic())
  1955. Code = bitc::FUNC_CODE_INST_STOREATOMIC;
  1956. else
  1957. Code = bitc::FUNC_CODE_INST_STORE;
  1958. PushValueAndType(I.getOperand(1), InstID, Vals, VE); // ptrty + ptr
  1959. PushValueAndType(I.getOperand(0), InstID, Vals, VE); // valty + val
  1960. Vals.push_back(Log2_32(cast<StoreInst>(I).getAlignment())+1);
  1961. Vals.push_back(cast<StoreInst>(I).isVolatile());
  1962. if (cast<StoreInst>(I).isAtomic()) {
  1963. Vals.push_back(GetEncodedOrdering(cast<StoreInst>(I).getOrdering()));
  1964. Vals.push_back(GetEncodedSynchScope(cast<StoreInst>(I).getSynchScope()));
  1965. }
  1966. break;
  1967. case Instruction::AtomicCmpXchg:
  1968. Code = bitc::FUNC_CODE_INST_CMPXCHG;
  1969. PushValueAndType(I.getOperand(0), InstID, Vals, VE); // ptrty + ptr
  1970. PushValueAndType(I.getOperand(1), InstID, Vals, VE); // cmp.
  1971. pushValue(I.getOperand(2), InstID, Vals, VE); // newval.
  1972. Vals.push_back(cast<AtomicCmpXchgInst>(I).isVolatile());
  1973. Vals.push_back(GetEncodedOrdering(
  1974. cast<AtomicCmpXchgInst>(I).getSuccessOrdering()));
  1975. Vals.push_back(GetEncodedSynchScope(
  1976. cast<AtomicCmpXchgInst>(I).getSynchScope()));
  1977. Vals.push_back(GetEncodedOrdering(
  1978. cast<AtomicCmpXchgInst>(I).getFailureOrdering()));
  1979. Vals.push_back(cast<AtomicCmpXchgInst>(I).isWeak());
  1980. break;
  1981. case Instruction::AtomicRMW:
  1982. Code = bitc::FUNC_CODE_INST_ATOMICRMW;
  1983. PushValueAndType(I.getOperand(0), InstID, Vals, VE); // ptrty + ptr
  1984. pushValue(I.getOperand(1), InstID, Vals, VE); // val.
  1985. Vals.push_back(GetEncodedRMWOperation(
  1986. cast<AtomicRMWInst>(I).getOperation()));
  1987. Vals.push_back(cast<AtomicRMWInst>(I).isVolatile());
  1988. Vals.push_back(GetEncodedOrdering(cast<AtomicRMWInst>(I).getOrdering()));
  1989. Vals.push_back(GetEncodedSynchScope(
  1990. cast<AtomicRMWInst>(I).getSynchScope()));
  1991. break;
  1992. case Instruction::Fence:
  1993. Code = bitc::FUNC_CODE_INST_FENCE;
  1994. Vals.push_back(GetEncodedOrdering(cast<FenceInst>(I).getOrdering()));
  1995. Vals.push_back(GetEncodedSynchScope(cast<FenceInst>(I).getSynchScope()));
  1996. break;
  1997. case Instruction::Call: {
  1998. const CallInst &CI = cast<CallInst>(I);
  1999. FunctionType *FTy = CI.getFunctionType();
  2000. if (CI.hasOperandBundles())
  2001. WriteOperandBundles(Stream, &CI, InstID, VE);
  2002. Code = bitc::FUNC_CODE_INST_CALL;
  2003. Vals.push_back(VE.getAttributeID(CI.getAttributes()));
  2004. unsigned Flags = GetOptimizationFlags(&I);
  2005. Vals.push_back(CI.getCallingConv() << bitc::CALL_CCONV |
  2006. unsigned(CI.isTailCall()) << bitc::CALL_TAIL |
  2007. unsigned(CI.isMustTailCall()) << bitc::CALL_MUSTTAIL |
  2008. 1 << bitc::CALL_EXPLICIT_TYPE |
  2009. unsigned(CI.isNoTailCall()) << bitc::CALL_NOTAIL |
  2010. unsigned(Flags != 0) << bitc::CALL_FMF);
  2011. if (Flags != 0)
  2012. Vals.push_back(Flags);
  2013. Vals.push_back(VE.getTypeID(FTy));
  2014. PushValueAndType(CI.getCalledValue(), InstID, Vals, VE); // Callee
  2015. // Emit value #'s for the fixed parameters.
  2016. for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) {
  2017. // Check for labels (can happen with asm labels).
  2018. if (FTy->getParamType(i)->isLabelTy())
  2019. Vals.push_back(VE.getValueID(CI.getArgOperand(i)));
  2020. else
  2021. pushValue(CI.getArgOperand(i), InstID, Vals, VE); // fixed param.
  2022. }
  2023. // Emit type/value pairs for varargs params.
  2024. if (FTy->isVarArg()) {
  2025. for (unsigned i = FTy->getNumParams(), e = CI.getNumArgOperands();
  2026. i != e; ++i)
  2027. PushValueAndType(CI.getArgOperand(i), InstID, Vals, VE); // varargs
  2028. }
  2029. break;
  2030. }
  2031. case Instruction::VAArg:
  2032. Code = bitc::FUNC_CODE_INST_VAARG;
  2033. Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); // valistty
  2034. pushValue(I.getOperand(0), InstID, Vals, VE); // valist.
  2035. Vals.push_back(VE.getTypeID(I.getType())); // restype.
  2036. break;
  2037. }
  2038. Stream.EmitRecord(Code, Vals, AbbrevToUse);
  2039. Vals.clear();
  2040. }
  2041. /// Emit names for globals/functions etc. The VSTOffsetPlaceholder,
  2042. /// BitcodeStartBit and ModuleSummaryIndex are only passed for the module-level
  2043. /// VST, where we are including a function bitcode index and need to
  2044. /// backpatch the VST forward declaration record.
  2045. static void WriteValueSymbolTable(
  2046. const ValueSymbolTable &VST, const ValueEnumerator &VE,
  2047. BitstreamWriter &Stream, uint64_t VSTOffsetPlaceholder = 0,
  2048. uint64_t BitcodeStartBit = 0,
  2049. DenseMap<const Function *, std::unique_ptr<GlobalValueInfo>>
  2050. *FunctionIndex = nullptr) {
  2051. if (VST.empty()) {
  2052. // WriteValueSymbolTableForwardDecl should have returned early as
  2053. // well. Ensure this handling remains in sync by asserting that
  2054. // the placeholder offset is not set.
  2055. assert(VSTOffsetPlaceholder == 0);
  2056. return;
  2057. }
  2058. if (VSTOffsetPlaceholder > 0) {
  2059. // Get the offset of the VST we are writing, and backpatch it into
  2060. // the VST forward declaration record.
  2061. uint64_t VSTOffset = Stream.GetCurrentBitNo();
  2062. // The BitcodeStartBit was the stream offset of the actual bitcode
  2063. // (e.g. excluding any initial darwin header).
  2064. VSTOffset -= BitcodeStartBit;
  2065. assert((VSTOffset & 31) == 0 && "VST block not 32-bit aligned");
  2066. Stream.BackpatchWord(VSTOffsetPlaceholder, VSTOffset / 32);
  2067. }
  2068. Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 4);
  2069. // For the module-level VST, add abbrev Ids for the VST_CODE_FNENTRY
  2070. // records, which are not used in the per-function VSTs.
  2071. unsigned FnEntry8BitAbbrev;
  2072. unsigned FnEntry7BitAbbrev;
  2073. unsigned FnEntry6BitAbbrev;
  2074. if (VSTOffsetPlaceholder > 0) {
  2075. // 8-bit fixed-width VST_CODE_FNENTRY function strings.
  2076. BitCodeAbbrev *Abbv = new BitCodeAbbrev();
  2077. Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_FNENTRY));
  2078. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
  2079. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // funcoffset
  2080. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
  2081. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
  2082. FnEntry8BitAbbrev = Stream.EmitAbbrev(Abbv);
  2083. // 7-bit fixed width VST_CODE_FNENTRY function strings.
  2084. Abbv = new BitCodeAbbrev();
  2085. Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_FNENTRY));
  2086. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
  2087. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // funcoffset
  2088. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
  2089. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
  2090. FnEntry7BitAbbrev = Stream.EmitAbbrev(Abbv);
  2091. // 6-bit char6 VST_CODE_FNENTRY function strings.
  2092. Abbv = new BitCodeAbbrev();
  2093. Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_FNENTRY));
  2094. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
  2095. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // funcoffset
  2096. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
  2097. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
  2098. FnEntry6BitAbbrev = Stream.EmitAbbrev(Abbv);
  2099. }
  2100. // FIXME: Set up the abbrev, we know how many values there are!
  2101. // FIXME: We know if the type names can use 7-bit ascii.
  2102. SmallVector<unsigned, 64> NameVals;
  2103. for (const ValueName &Name : VST) {
  2104. // Figure out the encoding to use for the name.
  2105. StringEncoding Bits =
  2106. getStringEncoding(Name.getKeyData(), Name.getKeyLength());
  2107. unsigned AbbrevToUse = VST_ENTRY_8_ABBREV;
  2108. NameVals.push_back(VE.getValueID(Name.getValue()));
  2109. Function *F = dyn_cast<Function>(Name.getValue());
  2110. if (!F) {
  2111. // If value is an alias, need to get the aliased base object to
  2112. // see if it is a function.
  2113. auto *GA = dyn_cast<GlobalAlias>(Name.getValue());
  2114. if (GA && GA->getBaseObject())
  2115. F = dyn_cast<Function>(GA->getBaseObject());
  2116. }
  2117. // VST_CODE_ENTRY: [valueid, namechar x N]
  2118. // VST_CODE_FNENTRY: [valueid, funcoffset, namechar x N]
  2119. // VST_CODE_BBENTRY: [bbid, namechar x N]
  2120. unsigned Code;
  2121. if (isa<BasicBlock>(Name.getValue())) {
  2122. Code = bitc::VST_CODE_BBENTRY;
  2123. if (Bits == SE_Char6)
  2124. AbbrevToUse = VST_BBENTRY_6_ABBREV;
  2125. } else if (F && !F->isDeclaration()) {
  2126. // Must be the module-level VST, where we pass in the Index and
  2127. // have a VSTOffsetPlaceholder. The function-level VST should not
  2128. // contain any Function symbols.
  2129. assert(FunctionIndex);
  2130. assert(VSTOffsetPlaceholder > 0);
  2131. // Save the word offset of the function (from the start of the
  2132. // actual bitcode written to the stream).
  2133. uint64_t BitcodeIndex =
  2134. (*FunctionIndex)[F]->bitcodeIndex() - BitcodeStartBit;
  2135. assert((BitcodeIndex & 31) == 0 && "function block not 32-bit aligned");
  2136. NameVals.push_back(BitcodeIndex / 32);
  2137. Code = bitc::VST_CODE_FNENTRY;
  2138. AbbrevToUse = FnEntry8BitAbbrev;
  2139. if (Bits == SE_Char6)
  2140. AbbrevToUse = FnEntry6BitAbbrev;
  2141. else if (Bits == SE_Fixed7)
  2142. AbbrevToUse = FnEntry7BitAbbrev;
  2143. } else {
  2144. Code = bitc::VST_CODE_ENTRY;
  2145. if (Bits == SE_Char6)
  2146. AbbrevToUse = VST_ENTRY_6_ABBREV;
  2147. else if (Bits == SE_Fixed7)
  2148. AbbrevToUse = VST_ENTRY_7_ABBREV;
  2149. }
  2150. for (const auto P : Name.getKey())
  2151. NameVals.push_back((unsigned char)P);
  2152. // Emit the finished record.
  2153. Stream.EmitRecord(Code, NameVals, AbbrevToUse);
  2154. NameVals.clear();
  2155. }
  2156. Stream.ExitBlock();
  2157. }
  2158. /// Emit function names and summary offsets for the combined index
  2159. /// used by ThinLTO.
  2160. static void
  2161. WriteCombinedValueSymbolTable(const ModuleSummaryIndex &Index,
  2162. BitstreamWriter &Stream,
  2163. std::map<uint64_t, unsigned> &GUIDToValueIdMap,
  2164. uint64_t VSTOffsetPlaceholder) {
  2165. assert(VSTOffsetPlaceholder > 0 && "Expected non-zero VSTOffsetPlaceholder");
  2166. // Get the offset of the VST we are writing, and backpatch it into
  2167. // the VST forward declaration record.
  2168. uint64_t VSTOffset = Stream.GetCurrentBitNo();
  2169. assert((VSTOffset & 31) == 0 && "VST block not 32-bit aligned");
  2170. Stream.BackpatchWord(VSTOffsetPlaceholder, VSTOffset / 32);
  2171. Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 4);
  2172. BitCodeAbbrev *Abbv = new BitCodeAbbrev();
  2173. Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_COMBINED_GVDEFENTRY));
  2174. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
  2175. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // sumoffset
  2176. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // guid
  2177. unsigned DefEntryAbbrev = Stream.EmitAbbrev(Abbv);
  2178. Abbv = new BitCodeAbbrev();
  2179. Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_COMBINED_ENTRY));
  2180. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
  2181. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // refguid
  2182. unsigned EntryAbbrev = Stream.EmitAbbrev(Abbv);
  2183. SmallVector<uint64_t, 64> NameVals;
  2184. for (const auto &FII : Index) {
  2185. uint64_t FuncGUID = FII.first;
  2186. const auto &VMI = GUIDToValueIdMap.find(FuncGUID);
  2187. assert(VMI != GUIDToValueIdMap.end());
  2188. for (const auto &FI : FII.second) {
  2189. // VST_CODE_COMBINED_GVDEFENTRY: [valueid, sumoffset, guid]
  2190. NameVals.push_back(VMI->second);
  2191. NameVals.push_back(FI->bitcodeIndex());
  2192. NameVals.push_back(FuncGUID);
  2193. // Emit the finished record.
  2194. Stream.EmitRecord(bitc::VST_CODE_COMBINED_GVDEFENTRY, NameVals,
  2195. DefEntryAbbrev);
  2196. NameVals.clear();
  2197. }
  2198. GUIDToValueIdMap.erase(VMI);
  2199. }
  2200. for (const auto &GVI : GUIDToValueIdMap) {
  2201. // VST_CODE_COMBINED_ENTRY: [valueid, refguid]
  2202. NameVals.push_back(GVI.second);
  2203. NameVals.push_back(GVI.first);
  2204. // Emit the finished record.
  2205. Stream.EmitRecord(bitc::VST_CODE_COMBINED_ENTRY, NameVals, EntryAbbrev);
  2206. NameVals.clear();
  2207. }
  2208. Stream.ExitBlock();
  2209. }
  2210. static void WriteUseList(ValueEnumerator &VE, UseListOrder &&Order,
  2211. BitstreamWriter &Stream) {
  2212. assert(Order.Shuffle.size() >= 2 && "Shuffle too small");
  2213. unsigned Code;
  2214. if (isa<BasicBlock>(Order.V))
  2215. Code = bitc::USELIST_CODE_BB;
  2216. else
  2217. Code = bitc::USELIST_CODE_DEFAULT;
  2218. SmallVector<uint64_t, 64> Record(Order.Shuffle.begin(), Order.Shuffle.end());
  2219. Record.push_back(VE.getValueID(Order.V));
  2220. Stream.EmitRecord(Code, Record);
  2221. }
  2222. static void WriteUseListBlock(const Function *F, ValueEnumerator &VE,
  2223. BitstreamWriter &Stream) {
  2224. assert(VE.shouldPreserveUseListOrder() &&
  2225. "Expected to be preserving use-list order");
  2226. auto hasMore = [&]() {
  2227. return !VE.UseListOrders.empty() && VE.UseListOrders.back().F == F;
  2228. };
  2229. if (!hasMore())
  2230. // Nothing to do.
  2231. return;
  2232. Stream.EnterSubblock(bitc::USELIST_BLOCK_ID, 3);
  2233. while (hasMore()) {
  2234. WriteUseList(VE, std::move(VE.UseListOrders.back()), Stream);
  2235. VE.UseListOrders.pop_back();
  2236. }
  2237. Stream.ExitBlock();
  2238. }
  2239. // Walk through the operands of a given User via worklist iteration and populate
  2240. // the set of GlobalValue references encountered. Invoked either on an
  2241. // Instruction or a GlobalVariable (which walks its initializer).
  2242. static void findRefEdges(const User *CurUser, const ValueEnumerator &VE,
  2243. DenseSet<unsigned> &RefEdges,
  2244. SmallPtrSet<const User *, 8> &Visited) {
  2245. SmallVector<const User *, 32> Worklist;
  2246. Worklist.push_back(CurUser);
  2247. while (!Worklist.empty()) {
  2248. const User *U = Worklist.pop_back_val();
  2249. if (!Visited.insert(U).second)
  2250. continue;
  2251. ImmutableCallSite CS(U);
  2252. for (const auto &OI : U->operands()) {
  2253. const User *Operand = dyn_cast<User>(OI);
  2254. if (!Operand)
  2255. continue;
  2256. if (isa<BlockAddress>(Operand))
  2257. continue;
  2258. if (isa<GlobalValue>(Operand)) {
  2259. // We have a reference to a global value. This should be added to
  2260. // the reference set unless it is a callee. Callees are handled
  2261. // specially by WriteFunction and are added to a separate list.
  2262. if (!(CS && CS.isCallee(&OI)))
  2263. RefEdges.insert(VE.getValueID(Operand));
  2264. continue;
  2265. }
  2266. Worklist.push_back(Operand);
  2267. }
  2268. }
  2269. }
  2270. /// Emit a function body to the module stream.
  2271. static void WriteFunction(
  2272. const Function &F, const Module *M, ValueEnumerator &VE,
  2273. BitstreamWriter &Stream,
  2274. DenseMap<const Function *, std::unique_ptr<GlobalValueInfo>> &FunctionIndex,
  2275. bool EmitSummaryIndex) {
  2276. // Save the bitcode index of the start of this function block for recording
  2277. // in the VST.
  2278. uint64_t BitcodeIndex = Stream.GetCurrentBitNo();
  2279. bool HasProfileData = F.getEntryCount().hasValue();
  2280. std::unique_ptr<BlockFrequencyInfo> BFI;
  2281. if (EmitSummaryIndex && HasProfileData) {
  2282. Function &Func = const_cast<Function &>(F);
  2283. LoopInfo LI{DominatorTree(Func)};
  2284. BranchProbabilityInfo BPI{Func, LI};
  2285. BFI = llvm::make_unique<BlockFrequencyInfo>(Func, BPI, LI);
  2286. }
  2287. Stream.EnterSubblock(bitc::FUNCTION_BLOCK_ID, 4);
  2288. VE.incorporateFunction(F);
  2289. SmallVector<unsigned, 64> Vals;
  2290. // Emit the number of basic blocks, so the reader can create them ahead of
  2291. // time.
  2292. Vals.push_back(VE.getBasicBlocks().size());
  2293. Stream.EmitRecord(bitc::FUNC_CODE_DECLAREBLOCKS, Vals);
  2294. Vals.clear();
  2295. // If there are function-local constants, emit them now.
  2296. unsigned CstStart, CstEnd;
  2297. VE.getFunctionConstantRange(CstStart, CstEnd);
  2298. WriteConstants(CstStart, CstEnd, VE, Stream, false);
  2299. // If there is function-local metadata, emit it now.
  2300. writeFunctionMetadata(F, VE, Stream);
  2301. // Keep a running idea of what the instruction ID is.
  2302. unsigned InstID = CstEnd;
  2303. bool NeedsMetadataAttachment = F.hasMetadata();
  2304. DILocation *LastDL = nullptr;
  2305. unsigned NumInsts = 0;
  2306. // Map from callee ValueId to profile count. Used to accumulate profile
  2307. // counts for all static calls to a given callee.
  2308. DenseMap<unsigned, CalleeInfo> CallGraphEdges;
  2309. DenseSet<unsigned> RefEdges;
  2310. SmallPtrSet<const User *, 8> Visited;
  2311. // Finally, emit all the instructions, in order.
  2312. for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
  2313. for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
  2314. I != E; ++I) {
  2315. WriteInstruction(*I, InstID, VE, Stream, Vals);
  2316. if (!isa<DbgInfoIntrinsic>(I))
  2317. ++NumInsts;
  2318. if (!I->getType()->isVoidTy())
  2319. ++InstID;
  2320. if (EmitSummaryIndex) {
  2321. if (auto CS = ImmutableCallSite(&*I)) {
  2322. auto *CalledFunction = CS.getCalledFunction();
  2323. if (CalledFunction && CalledFunction->hasName() &&
  2324. !CalledFunction->isIntrinsic()) {
  2325. auto ScaledCount = BFI ? BFI->getBlockProfileCount(&*BB) : None;
  2326. unsigned CalleeId = VE.getValueID(
  2327. M->getValueSymbolTable().lookup(CalledFunction->getName()));
  2328. CallGraphEdges[CalleeId] +=
  2329. (ScaledCount ? ScaledCount.getValue() : 0);
  2330. }
  2331. }
  2332. findRefEdges(&*I, VE, RefEdges, Visited);
  2333. }
  2334. // If the instruction has metadata, write a metadata attachment later.
  2335. NeedsMetadataAttachment |= I->hasMetadataOtherThanDebugLoc();
  2336. // If the instruction has a debug location, emit it.
  2337. DILocation *DL = I->getDebugLoc();
  2338. if (!DL)
  2339. continue;
  2340. if (DL == LastDL) {
  2341. // Just repeat the same debug loc as last time.
  2342. Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC_AGAIN, Vals);
  2343. continue;
  2344. }
  2345. Vals.push_back(DL->getLine());
  2346. Vals.push_back(DL->getColumn());
  2347. Vals.push_back(VE.getMetadataOrNullID(DL->getScope()));
  2348. Vals.push_back(VE.getMetadataOrNullID(DL->getInlinedAt()));
  2349. Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC, Vals);
  2350. Vals.clear();
  2351. LastDL = DL;
  2352. }
  2353. std::unique_ptr<FunctionSummary> FuncSummary;
  2354. if (EmitSummaryIndex) {
  2355. FuncSummary = llvm::make_unique<FunctionSummary>(F.getLinkage(), NumInsts);
  2356. FuncSummary->addCallGraphEdges(CallGraphEdges);
  2357. FuncSummary->addRefEdges(RefEdges);
  2358. }
  2359. FunctionIndex[&F] =
  2360. llvm::make_unique<GlobalValueInfo>(BitcodeIndex, std::move(FuncSummary));
  2361. // Emit names for all the instructions etc.
  2362. WriteValueSymbolTable(F.getValueSymbolTable(), VE, Stream);
  2363. if (NeedsMetadataAttachment)
  2364. WriteMetadataAttachment(F, VE, Stream);
  2365. if (VE.shouldPreserveUseListOrder())
  2366. WriteUseListBlock(&F, VE, Stream);
  2367. VE.purgeFunction();
  2368. Stream.ExitBlock();
  2369. }
  2370. // Emit blockinfo, which defines the standard abbreviations etc.
  2371. static void WriteBlockInfo(const ValueEnumerator &VE, BitstreamWriter &Stream) {
  2372. // We only want to emit block info records for blocks that have multiple
  2373. // instances: CONSTANTS_BLOCK, FUNCTION_BLOCK and VALUE_SYMTAB_BLOCK.
  2374. // Other blocks can define their abbrevs inline.
  2375. Stream.EnterBlockInfoBlock(2);
  2376. { // 8-bit fixed-width VST_CODE_ENTRY/VST_CODE_BBENTRY strings.
  2377. BitCodeAbbrev *Abbv = new BitCodeAbbrev();
  2378. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3));
  2379. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
  2380. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
  2381. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
  2382. if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
  2383. Abbv) != VST_ENTRY_8_ABBREV)
  2384. llvm_unreachable("Unexpected abbrev ordering!");
  2385. }
  2386. { // 7-bit fixed width VST_CODE_ENTRY strings.
  2387. BitCodeAbbrev *Abbv = new BitCodeAbbrev();
  2388. Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
  2389. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
  2390. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
  2391. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
  2392. if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
  2393. Abbv) != VST_ENTRY_7_ABBREV)
  2394. llvm_unreachable("Unexpected abbrev ordering!");
  2395. }
  2396. { // 6-bit char6 VST_CODE_ENTRY strings.
  2397. BitCodeAbbrev *Abbv = new BitCodeAbbrev();
  2398. Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
  2399. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
  2400. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
  2401. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
  2402. if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
  2403. Abbv) != VST_ENTRY_6_ABBREV)
  2404. llvm_unreachable("Unexpected abbrev ordering!");
  2405. }
  2406. { // 6-bit char6 VST_CODE_BBENTRY strings.
  2407. BitCodeAbbrev *Abbv = new BitCodeAbbrev();
  2408. Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_BBENTRY));
  2409. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
  2410. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
  2411. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
  2412. if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
  2413. Abbv) != VST_BBENTRY_6_ABBREV)
  2414. llvm_unreachable("Unexpected abbrev ordering!");
  2415. }
  2416. { // SETTYPE abbrev for CONSTANTS_BLOCK.
  2417. BitCodeAbbrev *Abbv = new BitCodeAbbrev();
  2418. Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_SETTYPE));
  2419. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
  2420. VE.computeBitsRequiredForTypeIndicies()));
  2421. if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
  2422. Abbv) != CONSTANTS_SETTYPE_ABBREV)
  2423. llvm_unreachable("Unexpected abbrev ordering!");
  2424. }
  2425. { // INTEGER abbrev for CONSTANTS_BLOCK.
  2426. BitCodeAbbrev *Abbv = new BitCodeAbbrev();
  2427. Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_INTEGER));
  2428. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
  2429. if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
  2430. Abbv) != CONSTANTS_INTEGER_ABBREV)
  2431. llvm_unreachable("Unexpected abbrev ordering!");
  2432. }
  2433. { // CE_CAST abbrev for CONSTANTS_BLOCK.
  2434. BitCodeAbbrev *Abbv = new BitCodeAbbrev();
  2435. Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CE_CAST));
  2436. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // cast opc
  2437. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // typeid
  2438. VE.computeBitsRequiredForTypeIndicies()));
  2439. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
  2440. if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
  2441. Abbv) != CONSTANTS_CE_CAST_Abbrev)
  2442. llvm_unreachable("Unexpected abbrev ordering!");
  2443. }
  2444. { // NULL abbrev for CONSTANTS_BLOCK.
  2445. BitCodeAbbrev *Abbv = new BitCodeAbbrev();
  2446. Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_NULL));
  2447. if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
  2448. Abbv) != CONSTANTS_NULL_Abbrev)
  2449. llvm_unreachable("Unexpected abbrev ordering!");
  2450. }
  2451. // FIXME: This should only use space for first class types!
  2452. { // INST_LOAD abbrev for FUNCTION_BLOCK.
  2453. BitCodeAbbrev *Abbv = new BitCodeAbbrev();
  2454. Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_LOAD));
  2455. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Ptr
  2456. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty
  2457. VE.computeBitsRequiredForTypeIndicies()));
  2458. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // Align
  2459. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // volatile
  2460. if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
  2461. Abbv) != FUNCTION_INST_LOAD_ABBREV)
  2462. llvm_unreachable("Unexpected abbrev ordering!");
  2463. }
  2464. { // INST_BINOP abbrev for FUNCTION_BLOCK.
  2465. BitCodeAbbrev *Abbv = new BitCodeAbbrev();
  2466. Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP));
  2467. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS
  2468. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS
  2469. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
  2470. if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
  2471. Abbv) != FUNCTION_INST_BINOP_ABBREV)
  2472. llvm_unreachable("Unexpected abbrev ordering!");
  2473. }
  2474. { // INST_BINOP_FLAGS abbrev for FUNCTION_BLOCK.
  2475. BitCodeAbbrev *Abbv = new BitCodeAbbrev();
  2476. Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP));
  2477. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS
  2478. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS
  2479. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
  2480. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7)); // flags
  2481. if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
  2482. Abbv) != FUNCTION_INST_BINOP_FLAGS_ABBREV)
  2483. llvm_unreachable("Unexpected abbrev ordering!");
  2484. }
  2485. { // INST_CAST abbrev for FUNCTION_BLOCK.
  2486. BitCodeAbbrev *Abbv = new BitCodeAbbrev();
  2487. Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_CAST));
  2488. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // OpVal
  2489. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty
  2490. VE.computeBitsRequiredForTypeIndicies()));
  2491. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
  2492. if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
  2493. Abbv) != FUNCTION_INST_CAST_ABBREV)
  2494. llvm_unreachable("Unexpected abbrev ordering!");
  2495. }
  2496. { // INST_RET abbrev for FUNCTION_BLOCK.
  2497. BitCodeAbbrev *Abbv = new BitCodeAbbrev();
  2498. Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET));
  2499. if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
  2500. Abbv) != FUNCTION_INST_RET_VOID_ABBREV)
  2501. llvm_unreachable("Unexpected abbrev ordering!");
  2502. }
  2503. { // INST_RET abbrev for FUNCTION_BLOCK.
  2504. BitCodeAbbrev *Abbv = new BitCodeAbbrev();
  2505. Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET));
  2506. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ValID
  2507. if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
  2508. Abbv) != FUNCTION_INST_RET_VAL_ABBREV)
  2509. llvm_unreachable("Unexpected abbrev ordering!");
  2510. }
  2511. { // INST_UNREACHABLE abbrev for FUNCTION_BLOCK.
  2512. BitCodeAbbrev *Abbv = new BitCodeAbbrev();
  2513. Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_UNREACHABLE));
  2514. if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
  2515. Abbv) != FUNCTION_INST_UNREACHABLE_ABBREV)
  2516. llvm_unreachable("Unexpected abbrev ordering!");
  2517. }
  2518. {
  2519. BitCodeAbbrev *Abbv = new BitCodeAbbrev();
  2520. Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_GEP));
  2521. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
  2522. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty
  2523. Log2_32_Ceil(VE.getTypes().size() + 1)));
  2524. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
  2525. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
  2526. if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
  2527. FUNCTION_INST_GEP_ABBREV)
  2528. llvm_unreachable("Unexpected abbrev ordering!");
  2529. }
  2530. Stream.ExitBlock();
  2531. }
  2532. /// Write the module path strings, currently only used when generating
  2533. /// a combined index file.
  2534. static void WriteModStrings(const ModuleSummaryIndex &I,
  2535. BitstreamWriter &Stream) {
  2536. Stream.EnterSubblock(bitc::MODULE_STRTAB_BLOCK_ID, 3);
  2537. // TODO: See which abbrev sizes we actually need to emit
  2538. // 8-bit fixed-width MST_ENTRY strings.
  2539. BitCodeAbbrev *Abbv = new BitCodeAbbrev();
  2540. Abbv->Add(BitCodeAbbrevOp(bitc::MST_CODE_ENTRY));
  2541. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
  2542. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
  2543. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
  2544. unsigned Abbrev8Bit = Stream.EmitAbbrev(Abbv);
  2545. // 7-bit fixed width MST_ENTRY strings.
  2546. Abbv = new BitCodeAbbrev();
  2547. Abbv->Add(BitCodeAbbrevOp(bitc::MST_CODE_ENTRY));
  2548. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
  2549. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
  2550. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
  2551. unsigned Abbrev7Bit = Stream.EmitAbbrev(Abbv);
  2552. // 6-bit char6 MST_ENTRY strings.
  2553. Abbv = new BitCodeAbbrev();
  2554. Abbv->Add(BitCodeAbbrevOp(bitc::MST_CODE_ENTRY));
  2555. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
  2556. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
  2557. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
  2558. unsigned Abbrev6Bit = Stream.EmitAbbrev(Abbv);
  2559. SmallVector<unsigned, 64> NameVals;
  2560. for (const StringMapEntry<uint64_t> &MPSE : I.modulePaths()) {
  2561. StringEncoding Bits =
  2562. getStringEncoding(MPSE.getKey().data(), MPSE.getKey().size());
  2563. unsigned AbbrevToUse = Abbrev8Bit;
  2564. if (Bits == SE_Char6)
  2565. AbbrevToUse = Abbrev6Bit;
  2566. else if (Bits == SE_Fixed7)
  2567. AbbrevToUse = Abbrev7Bit;
  2568. NameVals.push_back(MPSE.getValue());
  2569. for (const auto P : MPSE.getKey())
  2570. NameVals.push_back((unsigned char)P);
  2571. // Emit the finished record.
  2572. Stream.EmitRecord(bitc::MST_CODE_ENTRY, NameVals, AbbrevToUse);
  2573. NameVals.clear();
  2574. }
  2575. Stream.ExitBlock();
  2576. }
  2577. // Helper to emit a single function summary record.
  2578. static void WritePerModuleFunctionSummaryRecord(
  2579. SmallVector<uint64_t, 64> &NameVals, FunctionSummary *FS, unsigned ValueID,
  2580. unsigned FSCallsAbbrev, unsigned FSCallsProfileAbbrev,
  2581. BitstreamWriter &Stream, const Function &F) {
  2582. assert(FS);
  2583. NameVals.push_back(ValueID);
  2584. NameVals.push_back(getEncodedLinkage(FS->linkage()));
  2585. NameVals.push_back(FS->instCount());
  2586. NameVals.push_back(FS->refs().size());
  2587. for (auto &RI : FS->refs())
  2588. NameVals.push_back(RI);
  2589. bool HasProfileData = F.getEntryCount().hasValue();
  2590. for (auto &ECI : FS->calls()) {
  2591. NameVals.push_back(ECI.first);
  2592. assert(ECI.second.CallsiteCount > 0 && "Expected at least one callsite");
  2593. NameVals.push_back(ECI.second.CallsiteCount);
  2594. if (HasProfileData)
  2595. NameVals.push_back(ECI.second.ProfileCount);
  2596. }
  2597. unsigned FSAbbrev = (HasProfileData ? FSCallsProfileAbbrev : FSCallsAbbrev);
  2598. unsigned Code =
  2599. (HasProfileData ? bitc::FS_PERMODULE_PROFILE : bitc::FS_PERMODULE);
  2600. // Emit the finished record.
  2601. Stream.EmitRecord(Code, NameVals, FSAbbrev);
  2602. NameVals.clear();
  2603. }
  2604. // Collect the global value references in the given variable's initializer,
  2605. // and emit them in a summary record.
  2606. static void WriteModuleLevelReferences(const GlobalVariable &V,
  2607. const ValueEnumerator &VE,
  2608. SmallVector<uint64_t, 64> &NameVals,
  2609. unsigned FSModRefsAbbrev,
  2610. BitstreamWriter &Stream) {
  2611. // Only interested in recording variable defs in the summary.
  2612. if (V.isDeclaration())
  2613. return;
  2614. DenseSet<unsigned> RefEdges;
  2615. SmallPtrSet<const User *, 8> Visited;
  2616. findRefEdges(&V, VE, RefEdges, Visited);
  2617. NameVals.push_back(VE.getValueID(&V));
  2618. NameVals.push_back(getEncodedLinkage(V.getLinkage()));
  2619. for (auto RefId : RefEdges) {
  2620. NameVals.push_back(RefId);
  2621. }
  2622. Stream.EmitRecord(bitc::FS_PERMODULE_GLOBALVAR_INIT_REFS, NameVals,
  2623. FSModRefsAbbrev);
  2624. NameVals.clear();
  2625. }
  2626. /// Emit the per-module summary section alongside the rest of
  2627. /// the module's bitcode.
  2628. static void WritePerModuleGlobalValueSummary(
  2629. DenseMap<const Function *, std::unique_ptr<GlobalValueInfo>> &FunctionIndex,
  2630. const Module *M, const ValueEnumerator &VE, BitstreamWriter &Stream) {
  2631. if (M->empty())
  2632. return;
  2633. Stream.EnterSubblock(bitc::GLOBALVAL_SUMMARY_BLOCK_ID, 3);
  2634. // Abbrev for FS_PERMODULE.
  2635. BitCodeAbbrev *Abbv = new BitCodeAbbrev();
  2636. Abbv->Add(BitCodeAbbrevOp(bitc::FS_PERMODULE));
  2637. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
  2638. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 5)); // linkage
  2639. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // instcount
  2640. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numrefs
  2641. // numrefs x valueid, n x (valueid, callsitecount)
  2642. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
  2643. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
  2644. unsigned FSCallsAbbrev = Stream.EmitAbbrev(Abbv);
  2645. // Abbrev for FS_PERMODULE_PROFILE.
  2646. Abbv = new BitCodeAbbrev();
  2647. Abbv->Add(BitCodeAbbrevOp(bitc::FS_PERMODULE_PROFILE));
  2648. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
  2649. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 5)); // linkage
  2650. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // instcount
  2651. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numrefs
  2652. // numrefs x valueid, n x (valueid, callsitecount, profilecount)
  2653. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
  2654. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
  2655. unsigned FSCallsProfileAbbrev = Stream.EmitAbbrev(Abbv);
  2656. // Abbrev for FS_PERMODULE_GLOBALVAR_INIT_REFS.
  2657. Abbv = new BitCodeAbbrev();
  2658. Abbv->Add(BitCodeAbbrevOp(bitc::FS_PERMODULE_GLOBALVAR_INIT_REFS));
  2659. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
  2660. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 5)); // linkage
  2661. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); // valueids
  2662. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
  2663. unsigned FSModRefsAbbrev = Stream.EmitAbbrev(Abbv);
  2664. SmallVector<uint64_t, 64> NameVals;
  2665. // Iterate over the list of functions instead of the FunctionIndex map to
  2666. // ensure the ordering is stable.
  2667. for (const Function &F : *M) {
  2668. if (F.isDeclaration())
  2669. continue;
  2670. // Skip anonymous functions. We will emit a function summary for
  2671. // any aliases below.
  2672. if (!F.hasName())
  2673. continue;
  2674. assert(FunctionIndex.count(&F) == 1);
  2675. WritePerModuleFunctionSummaryRecord(
  2676. NameVals, cast<FunctionSummary>(FunctionIndex[&F]->summary()),
  2677. VE.getValueID(M->getValueSymbolTable().lookup(F.getName())),
  2678. FSCallsAbbrev, FSCallsProfileAbbrev, Stream, F);
  2679. }
  2680. for (const GlobalAlias &A : M->aliases()) {
  2681. if (!A.getBaseObject())
  2682. continue;
  2683. const Function *F = dyn_cast<Function>(A.getBaseObject());
  2684. if (!F || F->isDeclaration())
  2685. continue;
  2686. assert(FunctionIndex.count(F) == 1);
  2687. FunctionSummary *FS =
  2688. cast<FunctionSummary>(FunctionIndex[F]->summary());
  2689. // Add the alias to the reference list of aliasee function.
  2690. FS->addRefEdge(
  2691. VE.getValueID(M->getValueSymbolTable().lookup(A.getName())));
  2692. WritePerModuleFunctionSummaryRecord(
  2693. NameVals, FS,
  2694. VE.getValueID(M->getValueSymbolTable().lookup(A.getName())),
  2695. FSCallsAbbrev, FSCallsProfileAbbrev, Stream, *F);
  2696. }
  2697. // Capture references from GlobalVariable initializers, which are outside
  2698. // of a function scope.
  2699. for (const GlobalVariable &G : M->globals())
  2700. WriteModuleLevelReferences(G, VE, NameVals, FSModRefsAbbrev, Stream);
  2701. for (const GlobalAlias &A : M->aliases())
  2702. if (auto *GV = dyn_cast<GlobalVariable>(A.getBaseObject()))
  2703. WriteModuleLevelReferences(*GV, VE, NameVals, FSModRefsAbbrev, Stream);
  2704. Stream.ExitBlock();
  2705. }
  2706. /// Emit the combined summary section into the combined index file.
  2707. static void WriteCombinedGlobalValueSummary(
  2708. const ModuleSummaryIndex &I, BitstreamWriter &Stream,
  2709. std::map<uint64_t, unsigned> &GUIDToValueIdMap, unsigned GlobalValueId) {
  2710. Stream.EnterSubblock(bitc::GLOBALVAL_SUMMARY_BLOCK_ID, 3);
  2711. // Abbrev for FS_COMBINED.
  2712. BitCodeAbbrev *Abbv = new BitCodeAbbrev();
  2713. Abbv->Add(BitCodeAbbrevOp(bitc::FS_COMBINED));
  2714. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // modid
  2715. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 5)); // linkage
  2716. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // instcount
  2717. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numrefs
  2718. // numrefs x valueid, n x (valueid, callsitecount)
  2719. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
  2720. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
  2721. unsigned FSCallsAbbrev = Stream.EmitAbbrev(Abbv);
  2722. // Abbrev for FS_COMBINED_PROFILE.
  2723. Abbv = new BitCodeAbbrev();
  2724. Abbv->Add(BitCodeAbbrevOp(bitc::FS_COMBINED_PROFILE));
  2725. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // modid
  2726. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 5)); // linkage
  2727. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // instcount
  2728. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numrefs
  2729. // numrefs x valueid, n x (valueid, callsitecount, profilecount)
  2730. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
  2731. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
  2732. unsigned FSCallsProfileAbbrev = Stream.EmitAbbrev(Abbv);
  2733. // Abbrev for FS_COMBINED_GLOBALVAR_INIT_REFS.
  2734. Abbv = new BitCodeAbbrev();
  2735. Abbv->Add(BitCodeAbbrevOp(bitc::FS_COMBINED_GLOBALVAR_INIT_REFS));
  2736. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // modid
  2737. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 5)); // linkage
  2738. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); // valueids
  2739. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
  2740. unsigned FSModRefsAbbrev = Stream.EmitAbbrev(Abbv);
  2741. SmallVector<uint64_t, 64> NameVals;
  2742. for (const auto &FII : I) {
  2743. for (auto &FI : FII.second) {
  2744. GlobalValueSummary *S = FI->summary();
  2745. assert(S);
  2746. if (auto *VS = dyn_cast<GlobalVarSummary>(S)) {
  2747. NameVals.push_back(I.getModuleId(VS->modulePath()));
  2748. NameVals.push_back(getEncodedLinkage(VS->linkage()));
  2749. for (auto &RI : VS->refs()) {
  2750. const auto &VMI = GUIDToValueIdMap.find(RI);
  2751. unsigned RefId;
  2752. // If this GUID doesn't have an entry, assign one.
  2753. if (VMI == GUIDToValueIdMap.end()) {
  2754. GUIDToValueIdMap[RI] = ++GlobalValueId;
  2755. RefId = GlobalValueId;
  2756. } else {
  2757. RefId = VMI->second;
  2758. }
  2759. NameVals.push_back(RefId);
  2760. }
  2761. // Record the starting offset of this summary entry for use
  2762. // in the VST entry. Add the current code size since the
  2763. // reader will invoke readRecord after the abbrev id read.
  2764. FI->setBitcodeIndex(Stream.GetCurrentBitNo() +
  2765. Stream.GetAbbrevIDWidth());
  2766. // Emit the finished record.
  2767. Stream.EmitRecord(bitc::FS_COMBINED_GLOBALVAR_INIT_REFS, NameVals,
  2768. FSModRefsAbbrev);
  2769. NameVals.clear();
  2770. continue;
  2771. }
  2772. auto *FS = cast<FunctionSummary>(S);
  2773. NameVals.push_back(I.getModuleId(FS->modulePath()));
  2774. NameVals.push_back(getEncodedLinkage(FS->linkage()));
  2775. NameVals.push_back(FS->instCount());
  2776. NameVals.push_back(FS->refs().size());
  2777. for (auto &RI : FS->refs()) {
  2778. const auto &VMI = GUIDToValueIdMap.find(RI);
  2779. unsigned RefId;
  2780. // If this GUID doesn't have an entry, assign one.
  2781. if (VMI == GUIDToValueIdMap.end()) {
  2782. GUIDToValueIdMap[RI] = ++GlobalValueId;
  2783. RefId = GlobalValueId;
  2784. } else {
  2785. RefId = VMI->second;
  2786. }
  2787. NameVals.push_back(RefId);
  2788. }
  2789. bool HasProfileData = false;
  2790. for (auto &EI : FS->calls()) {
  2791. HasProfileData |= EI.second.ProfileCount != 0;
  2792. if (HasProfileData)
  2793. break;
  2794. }
  2795. for (auto &EI : FS->calls()) {
  2796. const auto &VMI = GUIDToValueIdMap.find(EI.first);
  2797. // If this GUID doesn't have an entry, it doesn't have a function
  2798. // summary and we don't need to record any calls to it.
  2799. if (VMI == GUIDToValueIdMap.end())
  2800. continue;
  2801. NameVals.push_back(VMI->second);
  2802. assert(EI.second.CallsiteCount > 0 && "Expected at least one callsite");
  2803. NameVals.push_back(EI.second.CallsiteCount);
  2804. if (HasProfileData)
  2805. NameVals.push_back(EI.second.ProfileCount);
  2806. }
  2807. // Record the starting offset of this summary entry for use
  2808. // in the VST entry. Add the current code size since the
  2809. // reader will invoke readRecord after the abbrev id read.
  2810. FI->setBitcodeIndex(Stream.GetCurrentBitNo() + Stream.GetAbbrevIDWidth());
  2811. unsigned FSAbbrev =
  2812. (HasProfileData ? FSCallsProfileAbbrev : FSCallsAbbrev);
  2813. unsigned Code =
  2814. (HasProfileData ? bitc::FS_COMBINED_PROFILE : bitc::FS_COMBINED);
  2815. // Emit the finished record.
  2816. Stream.EmitRecord(Code, NameVals, FSAbbrev);
  2817. NameVals.clear();
  2818. }
  2819. }
  2820. Stream.ExitBlock();
  2821. }
  2822. // Create the "IDENTIFICATION_BLOCK_ID" containing a single string with the
  2823. // current llvm version, and a record for the epoch number.
  2824. static void WriteIdentificationBlock(const Module *M, BitstreamWriter &Stream) {
  2825. Stream.EnterSubblock(bitc::IDENTIFICATION_BLOCK_ID, 5);
  2826. // Write the "user readable" string identifying the bitcode producer
  2827. BitCodeAbbrev *Abbv = new BitCodeAbbrev();
  2828. Abbv->Add(BitCodeAbbrevOp(bitc::IDENTIFICATION_CODE_STRING));
  2829. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
  2830. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
  2831. auto StringAbbrev = Stream.EmitAbbrev(Abbv);
  2832. WriteStringRecord(bitc::IDENTIFICATION_CODE_STRING,
  2833. "LLVM" LLVM_VERSION_STRING, StringAbbrev, Stream);
  2834. // Write the epoch version
  2835. Abbv = new BitCodeAbbrev();
  2836. Abbv->Add(BitCodeAbbrevOp(bitc::IDENTIFICATION_CODE_EPOCH));
  2837. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
  2838. auto EpochAbbrev = Stream.EmitAbbrev(Abbv);
  2839. SmallVector<unsigned, 1> Vals = {bitc::BITCODE_CURRENT_EPOCH};
  2840. Stream.EmitRecord(bitc::IDENTIFICATION_CODE_EPOCH, Vals, EpochAbbrev);
  2841. Stream.ExitBlock();
  2842. }
  2843. /// WriteModule - Emit the specified module to the bitstream.
  2844. static void WriteModule(const Module *M, BitstreamWriter &Stream,
  2845. bool ShouldPreserveUseListOrder,
  2846. uint64_t BitcodeStartBit, bool EmitSummaryIndex) {
  2847. Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3);
  2848. SmallVector<unsigned, 1> Vals;
  2849. unsigned CurVersion = 1;
  2850. Vals.push_back(CurVersion);
  2851. Stream.EmitRecord(bitc::MODULE_CODE_VERSION, Vals);
  2852. // Analyze the module, enumerating globals, functions, etc.
  2853. ValueEnumerator VE(*M, ShouldPreserveUseListOrder);
  2854. // Emit blockinfo, which defines the standard abbreviations etc.
  2855. WriteBlockInfo(VE, Stream);
  2856. // Emit information about attribute groups.
  2857. WriteAttributeGroupTable(VE, Stream);
  2858. // Emit information about parameter attributes.
  2859. WriteAttributeTable(VE, Stream);
  2860. // Emit information describing all of the types in the module.
  2861. WriteTypeTable(VE, Stream);
  2862. writeComdats(VE, Stream);
  2863. // Emit top-level description of module, including target triple, inline asm,
  2864. // descriptors for global variables, and function prototype info.
  2865. uint64_t VSTOffsetPlaceholder = WriteModuleInfo(M, VE, Stream);
  2866. // Emit constants.
  2867. WriteModuleConstants(VE, Stream);
  2868. // Emit metadata.
  2869. writeModuleMetadata(*M, VE, Stream);
  2870. // Emit metadata.
  2871. WriteModuleMetadataStore(M, Stream);
  2872. // Emit module-level use-lists.
  2873. if (VE.shouldPreserveUseListOrder())
  2874. WriteUseListBlock(nullptr, VE, Stream);
  2875. WriteOperandBundleTags(M, Stream);
  2876. // Emit function bodies.
  2877. DenseMap<const Function *, std::unique_ptr<GlobalValueInfo>> FunctionIndex;
  2878. for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F)
  2879. if (!F->isDeclaration())
  2880. WriteFunction(*F, M, VE, Stream, FunctionIndex, EmitSummaryIndex);
  2881. // Need to write after the above call to WriteFunction which populates
  2882. // the summary information in the index.
  2883. if (EmitSummaryIndex)
  2884. WritePerModuleGlobalValueSummary(FunctionIndex, M, VE, Stream);
  2885. WriteValueSymbolTable(M->getValueSymbolTable(), VE, Stream,
  2886. VSTOffsetPlaceholder, BitcodeStartBit, &FunctionIndex);
  2887. Stream.ExitBlock();
  2888. }
  2889. /// EmitDarwinBCHeader - If generating a bc file on darwin, we have to emit a
  2890. /// header and trailer to make it compatible with the system archiver. To do
  2891. /// this we emit the following header, and then emit a trailer that pads the
  2892. /// file out to be a multiple of 16 bytes.
  2893. ///
  2894. /// struct bc_header {
  2895. /// uint32_t Magic; // 0x0B17C0DE
  2896. /// uint32_t Version; // Version, currently always 0.
  2897. /// uint32_t BitcodeOffset; // Offset to traditional bitcode file.
  2898. /// uint32_t BitcodeSize; // Size of traditional bitcode file.
  2899. /// uint32_t CPUType; // CPU specifier.
  2900. /// ... potentially more later ...
  2901. /// };
  2902. static void WriteInt32ToBuffer(uint32_t Value, SmallVectorImpl<char> &Buffer,
  2903. uint32_t &Position) {
  2904. support::endian::write32le(&Buffer[Position], Value);
  2905. Position += 4;
  2906. }
  2907. static void EmitDarwinBCHeaderAndTrailer(SmallVectorImpl<char> &Buffer,
  2908. const Triple &TT) {
  2909. unsigned CPUType = ~0U;
  2910. // Match x86_64-*, i[3-9]86-*, powerpc-*, powerpc64-*, arm-*, thumb-*,
  2911. // armv[0-9]-*, thumbv[0-9]-*, armv5te-*, or armv6t2-*. The CPUType is a magic
  2912. // number from /usr/include/mach/machine.h. It is ok to reproduce the
  2913. // specific constants here because they are implicitly part of the Darwin ABI.
  2914. enum {
  2915. DARWIN_CPU_ARCH_ABI64 = 0x01000000,
  2916. DARWIN_CPU_TYPE_X86 = 7,
  2917. DARWIN_CPU_TYPE_ARM = 12,
  2918. DARWIN_CPU_TYPE_POWERPC = 18
  2919. };
  2920. Triple::ArchType Arch = TT.getArch();
  2921. if (Arch == Triple::x86_64)
  2922. CPUType = DARWIN_CPU_TYPE_X86 | DARWIN_CPU_ARCH_ABI64;
  2923. else if (Arch == Triple::x86)
  2924. CPUType = DARWIN_CPU_TYPE_X86;
  2925. else if (Arch == Triple::ppc)
  2926. CPUType = DARWIN_CPU_TYPE_POWERPC;
  2927. else if (Arch == Triple::ppc64)
  2928. CPUType = DARWIN_CPU_TYPE_POWERPC | DARWIN_CPU_ARCH_ABI64;
  2929. else if (Arch == Triple::arm || Arch == Triple::thumb)
  2930. CPUType = DARWIN_CPU_TYPE_ARM;
  2931. // Traditional Bitcode starts after header.
  2932. assert(Buffer.size() >= BWH_HeaderSize &&
  2933. "Expected header size to be reserved");
  2934. unsigned BCOffset = BWH_HeaderSize;
  2935. unsigned BCSize = Buffer.size() - BWH_HeaderSize;
  2936. // Write the magic and version.
  2937. unsigned Position = 0;
  2938. WriteInt32ToBuffer(0x0B17C0DE , Buffer, Position);
  2939. WriteInt32ToBuffer(0 , Buffer, Position); // Version.
  2940. WriteInt32ToBuffer(BCOffset , Buffer, Position);
  2941. WriteInt32ToBuffer(BCSize , Buffer, Position);
  2942. WriteInt32ToBuffer(CPUType , Buffer, Position);
  2943. // If the file is not a multiple of 16 bytes, insert dummy padding.
  2944. while (Buffer.size() & 15)
  2945. Buffer.push_back(0);
  2946. }
  2947. /// Helper to write the header common to all bitcode files.
  2948. static void WriteBitcodeHeader(BitstreamWriter &Stream) {
  2949. // Emit the file header.
  2950. Stream.Emit((unsigned)'B', 8);
  2951. Stream.Emit((unsigned)'C', 8);
  2952. Stream.Emit(0x0, 4);
  2953. Stream.Emit(0xC, 4);
  2954. Stream.Emit(0xE, 4);
  2955. Stream.Emit(0xD, 4);
  2956. }
  2957. /// WriteBitcodeToFile - Write the specified module to the specified output
  2958. /// stream.
  2959. void llvm::WriteBitcodeToFile(const Module *M, raw_ostream &Out,
  2960. bool ShouldPreserveUseListOrder,
  2961. bool EmitSummaryIndex) {
  2962. SmallVector<char, 0> Buffer;
  2963. Buffer.reserve(256*1024);
  2964. // If this is darwin or another generic macho target, reserve space for the
  2965. // header.
  2966. Triple TT(M->getTargetTriple());
  2967. if (TT.isOSDarwin() || TT.isOSBinFormatMachO())
  2968. Buffer.insert(Buffer.begin(), BWH_HeaderSize, 0);
  2969. // Emit the module into the buffer.
  2970. {
  2971. BitstreamWriter Stream(Buffer);
  2972. // Save the start bit of the actual bitcode, in case there is space
  2973. // saved at the start for the darwin header above. The reader stream
  2974. // will start at the bitcode, and we need the offset of the VST
  2975. // to line up.
  2976. uint64_t BitcodeStartBit = Stream.GetCurrentBitNo();
  2977. // Emit the file header.
  2978. WriteBitcodeHeader(Stream);
  2979. WriteIdentificationBlock(M, Stream);
  2980. // Emit the module.
  2981. WriteModule(M, Stream, ShouldPreserveUseListOrder, BitcodeStartBit,
  2982. EmitSummaryIndex);
  2983. }
  2984. if (TT.isOSDarwin() || TT.isOSBinFormatMachO())
  2985. EmitDarwinBCHeaderAndTrailer(Buffer, TT);
  2986. // Write the generated bitstream to "Out".
  2987. Out.write((char*)&Buffer.front(), Buffer.size());
  2988. }
  2989. // Write the specified module summary index to the given raw output stream,
  2990. // where it will be written in a new bitcode block. This is used when
  2991. // writing the combined index file for ThinLTO.
  2992. void llvm::WriteIndexToFile(const ModuleSummaryIndex &Index, raw_ostream &Out) {
  2993. SmallVector<char, 0> Buffer;
  2994. Buffer.reserve(256 * 1024);
  2995. BitstreamWriter Stream(Buffer);
  2996. // Emit the bitcode header.
  2997. WriteBitcodeHeader(Stream);
  2998. Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3);
  2999. SmallVector<unsigned, 1> Vals;
  3000. unsigned CurVersion = 1;
  3001. Vals.push_back(CurVersion);
  3002. Stream.EmitRecord(bitc::MODULE_CODE_VERSION, Vals);
  3003. // If we have a VST, write the VSTOFFSET record placeholder and record
  3004. // its offset.
  3005. uint64_t VSTOffsetPlaceholder = WriteValueSymbolTableForwardDecl(Stream);
  3006. // Write the module paths in the combined index.
  3007. WriteModStrings(Index, Stream);
  3008. // Assign unique value ids to all functions in the index for use
  3009. // in writing out the call graph edges. Save the mapping from GUID
  3010. // to the new global value id to use when writing those edges, which
  3011. // are currently saved in the index in terms of GUID.
  3012. std::map<uint64_t, unsigned> GUIDToValueIdMap;
  3013. unsigned GlobalValueId = 0;
  3014. for (auto &II : Index)
  3015. GUIDToValueIdMap[II.first] = ++GlobalValueId;
  3016. // Write the summary combined index records.
  3017. WriteCombinedGlobalValueSummary(Index, Stream, GUIDToValueIdMap,
  3018. GlobalValueId);
  3019. // Need a special VST writer for the combined index (we don't have a
  3020. // real VST and real values when this is invoked).
  3021. WriteCombinedValueSymbolTable(Index, Stream, GUIDToValueIdMap,
  3022. VSTOffsetPlaceholder);
  3023. Stream.ExitBlock();
  3024. Out.write((char *)&Buffer.front(), Buffer.size());
  3025. }