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