BitcodeReader.h 13 KB

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