LLVM API Documentation
00001 //===- BitcodeReader.h - Internal BitcodeReader impl ------------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This header defines the BitcodeReader class. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #ifndef LLVM_LIB_BITCODE_READER_BITCODEREADER_H 00015 #define LLVM_LIB_BITCODE_READER_BITCODEREADER_H 00016 00017 #include "llvm/ADT/DenseMap.h" 00018 #include "llvm/Bitcode/BitstreamReader.h" 00019 #include "llvm/Bitcode/LLVMBitCodes.h" 00020 #include "llvm/IR/Attributes.h" 00021 #include "llvm/IR/GVMaterializer.h" 00022 #include "llvm/IR/OperandTraits.h" 00023 #include "llvm/IR/Type.h" 00024 #include "llvm/IR/ValueHandle.h" 00025 #include <deque> 00026 #include <system_error> 00027 #include <vector> 00028 00029 namespace llvm { 00030 class Comdat; 00031 class MemoryBuffer; 00032 class LLVMContext; 00033 00034 //===----------------------------------------------------------------------===// 00035 // BitcodeReaderValueList Class 00036 //===----------------------------------------------------------------------===// 00037 00038 class BitcodeReaderValueList { 00039 std::vector<WeakVH> ValuePtrs; 00040 00041 /// ResolveConstants - As we resolve forward-referenced constants, we add 00042 /// information about them to this vector. This allows us to resolve them in 00043 /// bulk instead of resolving each reference at a time. See the code in 00044 /// ResolveConstantForwardRefs for more information about this. 00045 /// 00046 /// The key of this vector is the placeholder constant, the value is the slot 00047 /// number that holds the resolved value. 00048 typedef std::vector<std::pair<Constant*, unsigned> > ResolveConstantsTy; 00049 ResolveConstantsTy ResolveConstants; 00050 LLVMContext &Context; 00051 public: 00052 BitcodeReaderValueList(LLVMContext &C) : Context(C) {} 00053 ~BitcodeReaderValueList() { 00054 assert(ResolveConstants.empty() && "Constants not resolved?"); 00055 } 00056 00057 // vector compatibility methods 00058 unsigned size() const { return ValuePtrs.size(); } 00059 void resize(unsigned N) { ValuePtrs.resize(N); } 00060 void push_back(Value *V) { 00061 ValuePtrs.push_back(V); 00062 } 00063 00064 void clear() { 00065 assert(ResolveConstants.empty() && "Constants not resolved?"); 00066 ValuePtrs.clear(); 00067 } 00068 00069 Value *operator[](unsigned i) const { 00070 assert(i < ValuePtrs.size()); 00071 return ValuePtrs[i]; 00072 } 00073 00074 Value *back() const { return ValuePtrs.back(); } 00075 void pop_back() { ValuePtrs.pop_back(); } 00076 bool empty() const { return ValuePtrs.empty(); } 00077 void shrinkTo(unsigned N) { 00078 assert(N <= size() && "Invalid shrinkTo request!"); 00079 ValuePtrs.resize(N); 00080 } 00081 00082 Constant *getConstantFwdRef(unsigned Idx, Type *Ty); 00083 Value *getValueFwdRef(unsigned Idx, Type *Ty); 00084 00085 void AssignValue(Value *V, unsigned Idx); 00086 00087 /// ResolveConstantForwardRefs - Once all constants are read, this method bulk 00088 /// resolves any forward references. 00089 void ResolveConstantForwardRefs(); 00090 }; 00091 00092 00093 //===----------------------------------------------------------------------===// 00094 // BitcodeReaderMDValueList Class 00095 //===----------------------------------------------------------------------===// 00096 00097 class BitcodeReaderMDValueList { 00098 std::vector<WeakVH> MDValuePtrs; 00099 00100 LLVMContext &Context; 00101 public: 00102 BitcodeReaderMDValueList(LLVMContext& C) : Context(C) {} 00103 00104 // vector compatibility methods 00105 unsigned size() const { return MDValuePtrs.size(); } 00106 void resize(unsigned N) { MDValuePtrs.resize(N); } 00107 void push_back(Value *V) { MDValuePtrs.push_back(V); } 00108 void clear() { MDValuePtrs.clear(); } 00109 Value *back() const { return MDValuePtrs.back(); } 00110 void pop_back() { MDValuePtrs.pop_back(); } 00111 bool empty() const { return MDValuePtrs.empty(); } 00112 00113 Value *operator[](unsigned i) const { 00114 assert(i < MDValuePtrs.size()); 00115 return MDValuePtrs[i]; 00116 } 00117 00118 void shrinkTo(unsigned N) { 00119 assert(N <= size() && "Invalid shrinkTo request!"); 00120 MDValuePtrs.resize(N); 00121 } 00122 00123 Value *getValueFwdRef(unsigned Idx); 00124 void AssignValue(Value *V, unsigned Idx); 00125 }; 00126 00127 class BitcodeReader : public GVMaterializer { 00128 LLVMContext &Context; 00129 Module *TheModule; 00130 std::unique_ptr<MemoryBuffer> Buffer; 00131 std::unique_ptr<BitstreamReader> StreamFile; 00132 BitstreamCursor Stream; 00133 DataStreamer *LazyStreamer; 00134 uint64_t NextUnreadBit; 00135 bool SeenValueSymbolTable; 00136 00137 std::vector<Type*> TypeList; 00138 BitcodeReaderValueList ValueList; 00139 BitcodeReaderMDValueList MDValueList; 00140 std::vector<Comdat *> ComdatList; 00141 SmallVector<Instruction *, 64> InstructionList; 00142 00143 std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits; 00144 std::vector<std::pair<GlobalAlias*, unsigned> > AliasInits; 00145 std::vector<std::pair<Function*, unsigned> > FunctionPrefixes; 00146 00147 SmallVector<Instruction*, 64> InstsWithTBAATag; 00148 00149 /// MAttributes - The set of attributes by index. Index zero in the 00150 /// file is for null, and is thus not represented here. As such all indices 00151 /// are off by one. 00152 std::vector<AttributeSet> MAttributes; 00153 00154 /// \brief The set of attribute groups. 00155 std::map<unsigned, AttributeSet> MAttributeGroups; 00156 00157 /// FunctionBBs - While parsing a function body, this is a list of the basic 00158 /// blocks for the function. 00159 std::vector<BasicBlock*> FunctionBBs; 00160 00161 // When reading the module header, this list is populated with functions that 00162 // have bodies later in the file. 00163 std::vector<Function*> FunctionsWithBodies; 00164 00165 // When intrinsic functions are encountered which require upgrading they are 00166 // stored here with their replacement function. 00167 typedef std::vector<std::pair<Function*, Function*> > UpgradedIntrinsicMap; 00168 UpgradedIntrinsicMap UpgradedIntrinsics; 00169 00170 // Map the bitcode's custom MDKind ID to the Module's MDKind ID. 00171 DenseMap<unsigned, unsigned> MDKindMap; 00172 00173 // Several operations happen after the module header has been read, but 00174 // before function bodies are processed. This keeps track of whether 00175 // we've done this yet. 00176 bool SeenFirstFunctionBody; 00177 00178 /// DeferredFunctionInfo - When function bodies are initially scanned, this 00179 /// map contains info about where to find deferred function body in the 00180 /// stream. 00181 DenseMap<Function*, uint64_t> DeferredFunctionInfo; 00182 00183 /// These are basic blocks forward-referenced by block addresses. They are 00184 /// inserted lazily into functions when they're loaded. The basic block ID is 00185 /// its index into the vector. 00186 DenseMap<Function *, std::vector<BasicBlock *>> BasicBlockFwdRefs; 00187 std::deque<Function *> BasicBlockFwdRefQueue; 00188 00189 /// UseRelativeIDs - Indicates that we are using a new encoding for 00190 /// instruction operands where most operands in the current 00191 /// FUNCTION_BLOCK are encoded relative to the instruction number, 00192 /// for a more compact encoding. Some instruction operands are not 00193 /// relative to the instruction ID: basic block numbers, and types. 00194 /// Once the old style function blocks have been phased out, we would 00195 /// not need this flag. 00196 bool UseRelativeIDs; 00197 00198 /// True if all functions will be materialized, negating the need to process 00199 /// (e.g.) blockaddress forward references. 00200 bool WillMaterializeAllForwardRefs; 00201 00202 /// Functions that have block addresses taken. This is usually empty. 00203 SmallPtrSet<const Function *, 4> BlockAddressesTaken; 00204 00205 public: 00206 std::error_code Error(BitcodeError E) { return make_error_code(E); } 00207 00208 explicit BitcodeReader(MemoryBuffer *buffer, LLVMContext &C) 00209 : Context(C), TheModule(nullptr), Buffer(buffer), LazyStreamer(nullptr), 00210 NextUnreadBit(0), SeenValueSymbolTable(false), ValueList(C), 00211 MDValueList(C), SeenFirstFunctionBody(false), UseRelativeIDs(false), 00212 WillMaterializeAllForwardRefs(false) {} 00213 explicit BitcodeReader(DataStreamer *streamer, LLVMContext &C) 00214 : Context(C), TheModule(nullptr), Buffer(nullptr), LazyStreamer(streamer), 00215 NextUnreadBit(0), SeenValueSymbolTable(false), ValueList(C), 00216 MDValueList(C), SeenFirstFunctionBody(false), UseRelativeIDs(false), 00217 WillMaterializeAllForwardRefs(false) {} 00218 ~BitcodeReader() { FreeState(); } 00219 00220 std::error_code materializeForwardReferencedFunctions(); 00221 00222 void FreeState(); 00223 00224 void releaseBuffer(); 00225 00226 bool isMaterializable(const GlobalValue *GV) const override; 00227 bool isDematerializable(const GlobalValue *GV) const override; 00228 std::error_code Materialize(GlobalValue *GV) override; 00229 std::error_code MaterializeModule(Module *M) override; 00230 void Dematerialize(GlobalValue *GV) override; 00231 00232 /// @brief Main interface to parsing a bitcode buffer. 00233 /// @returns true if an error occurred. 00234 std::error_code ParseBitcodeInto(Module *M); 00235 00236 /// @brief Cheap mechanism to just extract module triple 00237 /// @returns true if an error occurred. 00238 ErrorOr<std::string> parseTriple(); 00239 00240 static uint64_t decodeSignRotatedValue(uint64_t V); 00241 00242 private: 00243 Type *getTypeByID(unsigned ID); 00244 Value *getFnValueByID(unsigned ID, Type *Ty) { 00245 if (Ty && Ty->isMetadataTy()) 00246 return MDValueList.getValueFwdRef(ID); 00247 return ValueList.getValueFwdRef(ID, Ty); 00248 } 00249 BasicBlock *getBasicBlock(unsigned ID) const { 00250 if (ID >= FunctionBBs.size()) return nullptr; // Invalid ID 00251 return FunctionBBs[ID]; 00252 } 00253 AttributeSet getAttributes(unsigned i) const { 00254 if (i-1 < MAttributes.size()) 00255 return MAttributes[i-1]; 00256 return AttributeSet(); 00257 } 00258 00259 /// getValueTypePair - Read a value/type pair out of the specified record from 00260 /// slot 'Slot'. Increment Slot past the number of slots used in the record. 00261 /// Return true on failure. 00262 bool getValueTypePair(SmallVectorImpl<uint64_t> &Record, unsigned &Slot, 00263 unsigned InstNum, Value *&ResVal) { 00264 if (Slot == Record.size()) return true; 00265 unsigned ValNo = (unsigned)Record[Slot++]; 00266 // Adjust the ValNo, if it was encoded relative to the InstNum. 00267 if (UseRelativeIDs) 00268 ValNo = InstNum - ValNo; 00269 if (ValNo < InstNum) { 00270 // If this is not a forward reference, just return the value we already 00271 // have. 00272 ResVal = getFnValueByID(ValNo, nullptr); 00273 return ResVal == nullptr; 00274 } else if (Slot == Record.size()) { 00275 return true; 00276 } 00277 00278 unsigned TypeNo = (unsigned)Record[Slot++]; 00279 ResVal = getFnValueByID(ValNo, getTypeByID(TypeNo)); 00280 return ResVal == nullptr; 00281 } 00282 00283 /// popValue - Read a value out of the specified record from slot 'Slot'. 00284 /// Increment Slot past the number of slots used by the value in the record. 00285 /// Return true if there is an error. 00286 bool popValue(SmallVectorImpl<uint64_t> &Record, unsigned &Slot, 00287 unsigned InstNum, Type *Ty, Value *&ResVal) { 00288 if (getValue(Record, Slot, InstNum, Ty, ResVal)) 00289 return true; 00290 // All values currently take a single record slot. 00291 ++Slot; 00292 return false; 00293 } 00294 00295 /// getValue -- Like popValue, but does not increment the Slot number. 00296 bool getValue(SmallVectorImpl<uint64_t> &Record, unsigned Slot, 00297 unsigned InstNum, Type *Ty, Value *&ResVal) { 00298 ResVal = getValue(Record, Slot, InstNum, Ty); 00299 return ResVal == nullptr; 00300 } 00301 00302 /// getValue -- Version of getValue that returns ResVal directly, 00303 /// or 0 if there is an error. 00304 Value *getValue(SmallVectorImpl<uint64_t> &Record, unsigned Slot, 00305 unsigned InstNum, Type *Ty) { 00306 if (Slot == Record.size()) return nullptr; 00307 unsigned ValNo = (unsigned)Record[Slot]; 00308 // Adjust the ValNo, if it was encoded relative to the InstNum. 00309 if (UseRelativeIDs) 00310 ValNo = InstNum - ValNo; 00311 return getFnValueByID(ValNo, Ty); 00312 } 00313 00314 /// getValueSigned -- Like getValue, but decodes signed VBRs. 00315 Value *getValueSigned(SmallVectorImpl<uint64_t> &Record, unsigned Slot, 00316 unsigned InstNum, Type *Ty) { 00317 if (Slot == Record.size()) return nullptr; 00318 unsigned ValNo = (unsigned)decodeSignRotatedValue(Record[Slot]); 00319 // Adjust the ValNo, if it was encoded relative to the InstNum. 00320 if (UseRelativeIDs) 00321 ValNo = InstNum - ValNo; 00322 return getFnValueByID(ValNo, Ty); 00323 } 00324 00325 std::error_code ParseAttrKind(uint64_t Code, Attribute::AttrKind *Kind); 00326 std::error_code ParseModule(bool Resume); 00327 std::error_code ParseAttributeBlock(); 00328 std::error_code ParseAttributeGroupBlock(); 00329 std::error_code ParseTypeTable(); 00330 std::error_code ParseTypeTableBody(); 00331 00332 std::error_code ParseValueSymbolTable(); 00333 std::error_code ParseConstants(); 00334 std::error_code RememberAndSkipFunctionBody(); 00335 std::error_code ParseFunctionBody(Function *F); 00336 std::error_code GlobalCleanup(); 00337 std::error_code ResolveGlobalAndAliasInits(); 00338 std::error_code ParseMetadata(); 00339 std::error_code ParseMetadataAttachment(); 00340 ErrorOr<std::string> parseModuleTriple(); 00341 std::error_code ParseUseLists(); 00342 std::error_code InitStream(); 00343 std::error_code InitStreamFromBuffer(); 00344 std::error_code InitLazyStream(); 00345 std::error_code FindFunctionInStream( 00346 Function *F, 00347 DenseMap<Function *, uint64_t>::iterator DeferredFunctionInfoIterator); 00348 }; 00349 00350 } // End llvm namespace 00351 00352 #endif