BitcodeReader.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. //===- BitcodeReader.h - Internal BitcodeReader impl ------------*- C++ -*-===//
  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. // This header defines the BitcodeReader class.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_LIB_BITCODE_READER_BITCODEREADER_H
  14. #define LLVM_LIB_BITCODE_READER_BITCODEREADER_H
  15. #include "llvm/ADT/DenseMap.h"
  16. #include "llvm/Bitcode/BitstreamReader.h"
  17. #include "llvm/Bitcode/LLVMBitCodes.h"
  18. #include "llvm/IR/Attributes.h"
  19. #include "llvm/IR/GVMaterializer.h"
  20. #include "llvm/IR/Metadata.h"
  21. #include "llvm/IR/OperandTraits.h"
  22. #include "llvm/IR/TrackingMDRef.h"
  23. #include "llvm/IR/Type.h"
  24. #include "llvm/IR/ValueHandle.h"
  25. #include <deque>
  26. #include <system_error>
  27. #include <vector>
  28. namespace llvm {
  29. class Comdat;
  30. class MemoryBuffer;
  31. class LLVMContext;
  32. //===----------------------------------------------------------------------===//
  33. // BitcodeReaderValueList Class
  34. //===----------------------------------------------------------------------===//
  35. class BitcodeReaderValueList {
  36. std::vector<WeakVH> ValuePtrs;
  37. /// ResolveConstants - As we resolve forward-referenced constants, we add
  38. /// information about them to this vector. This allows us to resolve them in
  39. /// bulk instead of resolving each reference at a time. See the code in
  40. /// ResolveConstantForwardRefs for more information about this.
  41. ///
  42. /// The key of this vector is the placeholder constant, the value is the slot
  43. /// number that holds the resolved value.
  44. typedef std::vector<std::pair<Constant*, unsigned> > ResolveConstantsTy;
  45. ResolveConstantsTy ResolveConstants;
  46. LLVMContext &Context;
  47. public:
  48. BitcodeReaderValueList(LLVMContext &C) : Context(C) {}
  49. ~BitcodeReaderValueList() {
  50. assert(ResolveConstants.empty() && "Constants not resolved?");
  51. }
  52. // vector compatibility methods
  53. unsigned size() const { return ValuePtrs.size(); }
  54. void resize(unsigned N) { ValuePtrs.resize(N); }
  55. void push_back(Value *V) {
  56. ValuePtrs.push_back(V);
  57. }
  58. void clear() {
  59. assert(ResolveConstants.empty() && "Constants not resolved?");
  60. ValuePtrs.clear();
  61. }
  62. Value *operator[](unsigned i) const {
  63. assert(i < ValuePtrs.size());
  64. return ValuePtrs[i];
  65. }
  66. Value *back() const { return ValuePtrs.back(); }
  67. void pop_back() { ValuePtrs.pop_back(); }
  68. bool empty() const { return ValuePtrs.empty(); }
  69. void shrinkTo(unsigned N) {
  70. assert(N <= size() && "Invalid shrinkTo request!");
  71. ValuePtrs.resize(N);
  72. }
  73. Constant *getConstantFwdRef(unsigned Idx, Type *Ty);
  74. Value *getValueFwdRef(unsigned Idx, Type *Ty);
  75. void AssignValue(Value *V, unsigned Idx);
  76. /// ResolveConstantForwardRefs - Once all constants are read, this method bulk
  77. /// resolves any forward references.
  78. void ResolveConstantForwardRefs();
  79. };
  80. //===----------------------------------------------------------------------===//
  81. // BitcodeReaderMDValueList Class
  82. //===----------------------------------------------------------------------===//
  83. class BitcodeReaderMDValueList {
  84. unsigned NumFwdRefs;
  85. bool AnyFwdRefs;
  86. unsigned MinFwdRef;
  87. unsigned MaxFwdRef;
  88. std::vector<TrackingMDRef> MDValuePtrs;
  89. LLVMContext &Context;
  90. public:
  91. BitcodeReaderMDValueList(LLVMContext &C)
  92. : NumFwdRefs(0), AnyFwdRefs(false), Context(C) {}
  93. // vector compatibility methods
  94. unsigned size() const { return MDValuePtrs.size(); }
  95. void resize(unsigned N) { MDValuePtrs.resize(N); }
  96. void push_back(Metadata *MD) { MDValuePtrs.emplace_back(MD); }
  97. void clear() { MDValuePtrs.clear(); }
  98. Metadata *back() const { return MDValuePtrs.back(); }
  99. void pop_back() { MDValuePtrs.pop_back(); }
  100. bool empty() const { return MDValuePtrs.empty(); }
  101. Metadata *operator[](unsigned i) const {
  102. assert(i < MDValuePtrs.size());
  103. return MDValuePtrs[i];
  104. }
  105. void shrinkTo(unsigned N) {
  106. assert(N <= size() && "Invalid shrinkTo request!");
  107. MDValuePtrs.resize(N);
  108. }
  109. Metadata *getValueFwdRef(unsigned Idx);
  110. void AssignValue(Metadata *MD, unsigned Idx);
  111. void tryToResolveCycles();
  112. };
  113. class BitcodeReader : public GVMaterializer {
  114. LLVMContext &Context;
  115. DiagnosticHandlerFunction DiagnosticHandler;
  116. Module *TheModule;
  117. std::unique_ptr<MemoryBuffer> Buffer;
  118. std::unique_ptr<BitstreamReader> StreamFile;
  119. BitstreamCursor Stream;
  120. DataStreamer *LazyStreamer;
  121. uint64_t NextUnreadBit;
  122. bool SeenValueSymbolTable;
  123. std::vector<Type*> TypeList;
  124. BitcodeReaderValueList ValueList;
  125. BitcodeReaderMDValueList MDValueList;
  126. std::vector<Comdat *> ComdatList;
  127. SmallVector<Instruction *, 64> InstructionList;
  128. std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
  129. std::vector<std::pair<GlobalAlias*, unsigned> > AliasInits;
  130. std::vector<std::pair<Function*, unsigned> > FunctionPrefixes;
  131. std::vector<std::pair<Function*, unsigned> > FunctionPrologues;
  132. SmallVector<Instruction*, 64> InstsWithTBAATag;
  133. /// MAttributes - The set of attributes by index. Index zero in the
  134. /// file is for null, and is thus not represented here. As such all indices
  135. /// are off by one.
  136. std::vector<AttributeSet> MAttributes;
  137. /// \brief The set of attribute groups.
  138. std::map<unsigned, AttributeSet> MAttributeGroups;
  139. /// FunctionBBs - While parsing a function body, this is a list of the basic
  140. /// blocks for the function.
  141. std::vector<BasicBlock*> FunctionBBs;
  142. // When reading the module header, this list is populated with functions that
  143. // have bodies later in the file.
  144. std::vector<Function*> FunctionsWithBodies;
  145. // When intrinsic functions are encountered which require upgrading they are
  146. // stored here with their replacement function.
  147. typedef std::vector<std::pair<Function*, Function*> > UpgradedIntrinsicMap;
  148. UpgradedIntrinsicMap UpgradedIntrinsics;
  149. // Map the bitcode's custom MDKind ID to the Module's MDKind ID.
  150. DenseMap<unsigned, unsigned> MDKindMap;
  151. // Several operations happen after the module header has been read, but
  152. // before function bodies are processed. This keeps track of whether
  153. // we've done this yet.
  154. bool SeenFirstFunctionBody;
  155. /// DeferredFunctionInfo - When function bodies are initially scanned, this
  156. /// map contains info about where to find deferred function body in the
  157. /// stream.
  158. DenseMap<Function*, uint64_t> DeferredFunctionInfo;
  159. /// When Metadata block is initially scanned when parsing the module, we may
  160. /// choose to defer parsing of the metadata. This vector contains info about
  161. /// which Metadata blocks are deferred.
  162. std::vector<uint64_t> DeferredMetadataInfo;
  163. /// These are basic blocks forward-referenced by block addresses. They are
  164. /// inserted lazily into functions when they're loaded. The basic block ID is
  165. /// its index into the vector.
  166. DenseMap<Function *, std::vector<BasicBlock *>> BasicBlockFwdRefs;
  167. std::deque<Function *> BasicBlockFwdRefQueue;
  168. /// UseRelativeIDs - Indicates that we are using a new encoding for
  169. /// instruction operands where most operands in the current
  170. /// FUNCTION_BLOCK are encoded relative to the instruction number,
  171. /// for a more compact encoding. Some instruction operands are not
  172. /// relative to the instruction ID: basic block numbers, and types.
  173. /// Once the old style function blocks have been phased out, we would
  174. /// not need this flag.
  175. bool UseRelativeIDs;
  176. /// True if all functions will be materialized, negating the need to process
  177. /// (e.g.) blockaddress forward references.
  178. bool WillMaterializeAllForwardRefs;
  179. /// Functions that have block addresses taken. This is usually empty.
  180. SmallPtrSet<const Function *, 4> BlockAddressesTaken;
  181. /// True if any Metadata block has been materialized.
  182. bool IsMetadataMaterialized;
  183. public:
  184. std::error_code Error(BitcodeError E, const Twine &Message);
  185. std::error_code Error(BitcodeError E);
  186. std::error_code Error(const Twine &Message);
  187. explicit BitcodeReader(MemoryBuffer *buffer, LLVMContext &C,
  188. DiagnosticHandlerFunction DiagnosticHandler);
  189. explicit BitcodeReader(DataStreamer *streamer, LLVMContext &C,
  190. DiagnosticHandlerFunction DiagnosticHandler);
  191. ~BitcodeReader() { FreeState(); }
  192. std::error_code materializeForwardReferencedFunctions();
  193. void FreeState();
  194. void releaseBuffer();
  195. bool isDematerializable(const GlobalValue *GV) const override;
  196. std::error_code materialize(GlobalValue *GV) override;
  197. std::error_code MaterializeModule(Module *M) override;
  198. std::vector<StructType *> getIdentifiedStructTypes() const override;
  199. void Dematerialize(GlobalValue *GV) override;
  200. /// @brief Main interface to parsing a bitcode buffer.
  201. /// @returns true if an error occurred.
  202. std::error_code ParseBitcodeInto(Module *M,
  203. bool ShouldLazyLoadMetadata = false);
  204. /// @brief Cheap mechanism to just extract module triple
  205. /// @returns true if an error occurred.
  206. ErrorOr<std::string> parseTriple();
  207. static uint64_t decodeSignRotatedValue(uint64_t V);
  208. /// Materialize any deferred Metadata block.
  209. std::error_code materializeMetadata() override;
  210. private:
  211. std::vector<StructType *> IdentifiedStructTypes;
  212. StructType *createIdentifiedStructType(LLVMContext &Context, StringRef Name);
  213. StructType *createIdentifiedStructType(LLVMContext &Context);
  214. Type *getTypeByID(unsigned ID);
  215. Value *getFnValueByID(unsigned ID, Type *Ty) {
  216. if (Ty && Ty->isMetadataTy())
  217. return MetadataAsValue::get(Ty->getContext(), getFnMetadataByID(ID));
  218. return ValueList.getValueFwdRef(ID, Ty);
  219. }
  220. Metadata *getFnMetadataByID(unsigned ID) {
  221. return MDValueList.getValueFwdRef(ID);
  222. }
  223. BasicBlock *getBasicBlock(unsigned ID) const {
  224. if (ID >= FunctionBBs.size()) return nullptr; // Invalid ID
  225. return FunctionBBs[ID];
  226. }
  227. AttributeSet getAttributes(unsigned i) const {
  228. if (i-1 < MAttributes.size())
  229. return MAttributes[i-1];
  230. return AttributeSet();
  231. }
  232. /// getValueTypePair - Read a value/type pair out of the specified record from
  233. /// slot 'Slot'. Increment Slot past the number of slots used in the record.
  234. /// Return true on failure.
  235. bool getValueTypePair(SmallVectorImpl<uint64_t> &Record, unsigned &Slot,
  236. unsigned InstNum, Value *&ResVal) {
  237. if (Slot == Record.size()) return true;
  238. unsigned ValNo = (unsigned)Record[Slot++];
  239. // Adjust the ValNo, if it was encoded relative to the InstNum.
  240. if (UseRelativeIDs)
  241. ValNo = InstNum - ValNo;
  242. if (ValNo < InstNum) {
  243. // If this is not a forward reference, just return the value we already
  244. // have.
  245. ResVal = getFnValueByID(ValNo, nullptr);
  246. return ResVal == nullptr;
  247. } else if (Slot == Record.size()) {
  248. return true;
  249. }
  250. unsigned TypeNo = (unsigned)Record[Slot++];
  251. ResVal = getFnValueByID(ValNo, getTypeByID(TypeNo));
  252. return ResVal == nullptr;
  253. }
  254. /// popValue - Read a value out of the specified record from slot 'Slot'.
  255. /// Increment Slot past the number of slots used by the value in the record.
  256. /// Return true if there is an error.
  257. bool popValue(SmallVectorImpl<uint64_t> &Record, unsigned &Slot,
  258. unsigned InstNum, Type *Ty, Value *&ResVal) {
  259. if (getValue(Record, Slot, InstNum, Ty, ResVal))
  260. return true;
  261. // All values currently take a single record slot.
  262. ++Slot;
  263. return false;
  264. }
  265. /// getValue -- Like popValue, but does not increment the Slot number.
  266. bool getValue(SmallVectorImpl<uint64_t> &Record, unsigned Slot,
  267. unsigned InstNum, Type *Ty, Value *&ResVal) {
  268. ResVal = getValue(Record, Slot, InstNum, Ty);
  269. return ResVal == nullptr;
  270. }
  271. /// getValue -- Version of getValue that returns ResVal directly,
  272. /// or 0 if there is an error.
  273. Value *getValue(SmallVectorImpl<uint64_t> &Record, unsigned Slot,
  274. unsigned InstNum, Type *Ty) {
  275. if (Slot == Record.size()) return nullptr;
  276. unsigned ValNo = (unsigned)Record[Slot];
  277. // Adjust the ValNo, if it was encoded relative to the InstNum.
  278. if (UseRelativeIDs)
  279. ValNo = InstNum - ValNo;
  280. return getFnValueByID(ValNo, Ty);
  281. }
  282. /// getValueSigned -- Like getValue, but decodes signed VBRs.
  283. Value *getValueSigned(SmallVectorImpl<uint64_t> &Record, unsigned Slot,
  284. unsigned InstNum, Type *Ty) {
  285. if (Slot == Record.size()) return nullptr;
  286. unsigned ValNo = (unsigned)decodeSignRotatedValue(Record[Slot]);
  287. // Adjust the ValNo, if it was encoded relative to the InstNum.
  288. if (UseRelativeIDs)
  289. ValNo = InstNum - ValNo;
  290. return getFnValueByID(ValNo, Ty);
  291. }
  292. /// Converts alignment exponent (i.e. power of two (or zero)) to the
  293. /// corresponding alignment to use. If alignment is too large, returns
  294. /// a corresponding error code.
  295. std::error_code parseAlignmentValue(uint64_t Exponent, unsigned &Alignment);
  296. std::error_code ParseAttrKind(uint64_t Code, Attribute::AttrKind *Kind);
  297. std::error_code ParseModule(bool Resume, bool ShouldLazyLoadMetadata = false);
  298. std::error_code ParseAttributeBlock();
  299. std::error_code ParseAttributeGroupBlock();
  300. std::error_code ParseTypeTable();
  301. std::error_code ParseTypeTableBody();
  302. std::error_code ParseValueSymbolTable();
  303. std::error_code ParseConstants();
  304. std::error_code RememberAndSkipFunctionBody();
  305. /// Save the positions of the Metadata blocks and skip parsing the blocks.
  306. std::error_code rememberAndSkipMetadata();
  307. std::error_code ParseFunctionBody(Function *F);
  308. std::error_code GlobalCleanup();
  309. std::error_code ResolveGlobalAndAliasInits();
  310. std::error_code ParseMetadata();
  311. std::error_code ParseMetadataAttachment();
  312. ErrorOr<std::string> parseModuleTriple();
  313. std::error_code ParseUseLists();
  314. std::error_code InitStream();
  315. std::error_code InitStreamFromBuffer();
  316. std::error_code InitLazyStream();
  317. std::error_code FindFunctionInStream(
  318. Function *F,
  319. DenseMap<Function *, uint64_t>::iterator DeferredFunctionInfoIterator);
  320. };
  321. } // End llvm namespace
  322. #endif