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