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