BitcodeReader.h 12 KB

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