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 const char *ErrorString; 141 142 std::vector<Type*> TypeList; 143 BitcodeReaderValueList ValueList; 144 BitcodeReaderMDValueList MDValueList; 145 SmallVector<Instruction *, 64> InstructionList; 146 147 std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits; 148 std::vector<std::pair<GlobalAlias*, unsigned> > AliasInits; 149 150 /// MAttributes - The set of attributes by index. Index zero in the 151 /// file is for null, and is thus not represented here. As such all indices 152 /// are off by one. 153 std::vector<AttributeSet> MAttributes; 154 155 /// \brief The set of attribute groups. 156 std::map<unsigned, AttributeSet> MAttributeGroups; 157 158 /// FunctionBBs - While parsing a function body, this is a list of the basic 159 /// blocks for the function. 160 std::vector<BasicBlock*> FunctionBBs; 161 162 // When reading the module header, this list is populated with functions that 163 // have bodies later in the file. 164 std::vector<Function*> FunctionsWithBodies; 165 166 // When intrinsic functions are encountered which require upgrading they are 167 // stored here with their replacement function. 168 typedef std::vector<std::pair<Function*, Function*> > UpgradedIntrinsicMap; 169 UpgradedIntrinsicMap UpgradedIntrinsics; 170 171 // Map the bitcode's custom MDKind ID to the Module's MDKind ID. 172 DenseMap<unsigned, unsigned> MDKindMap; 173 174 // Several operations happen after the module header has been read, but 175 // before function bodies are processed. This keeps track of whether 176 // we've done this yet. 177 bool SeenFirstFunctionBody; 178 179 /// DeferredFunctionInfo - When function bodies are initially scanned, this 180 /// map contains info about where to find deferred function body in the 181 /// stream. 182 DenseMap<Function*, uint64_t> DeferredFunctionInfo; 183 184 /// BlockAddrFwdRefs - These are blockaddr references to basic blocks. These 185 /// are resolved lazily when functions are loaded. 186 typedef std::pair<unsigned, GlobalVariable*> BlockAddrRefTy; 187 DenseMap<Function*, std::vector<BlockAddrRefTy> > BlockAddrFwdRefs; 188 189 /// LLVM2_7MetadataDetected - True if metadata produced by LLVM 2.7 or 190 /// earlier was detected, in which case we behave slightly differently, 191 /// for compatibility. 192 /// FIXME: Remove in LLVM 3.0. 193 bool LLVM2_7MetadataDetected; 194 195public: 196 explicit BitcodeReader(MemoryBuffer *buffer, LLVMContext &C) 197 : Context(C), TheModule(0), Buffer(buffer), BufferOwned(false), 198 LazyStreamer(0), NextUnreadBit(0), SeenValueSymbolTable(false), 199 ErrorString(0), ValueList(C), MDValueList(C), 200 SeenFirstFunctionBody(false), LLVM2_7MetadataDetected(false) { 201 } 202 ~BitcodeReader() { 203 FreeState(); 204 } 205 206 void FreeState(); 207 208 /// setBufferOwned - If this is true, the reader will destroy the MemoryBuffer 209 /// when the reader is destroyed. 210 void setBufferOwned(bool Owned) { BufferOwned = Owned; } 211 212 virtual bool isMaterializable(const GlobalValue *GV) const; 213 virtual bool isDematerializable(const GlobalValue *GV) const; 214 virtual bool Materialize(GlobalValue *GV, std::string *ErrInfo = 0); 215 virtual bool MaterializeModule(Module *M, std::string *ErrInfo = 0); 216 virtual void Dematerialize(GlobalValue *GV); 217 218 bool Error(const char *Str) { 219 ErrorString = Str; 220 return true; 221 } 222 const char *getErrorString() const { return ErrorString; } 223 224 /// @brief Main interface to parsing a bitcode buffer. 225 /// @returns true if an error occurred. 226 bool ParseBitcodeInto(Module *M); 227 228 /// @brief Cheap mechanism to just extract module triple 229 /// @returns true if an error occurred. 230 bool ParseTriple(std::string &Triple); 231 232 static uint64_t decodeSignRotatedValue(uint64_t V); 233 234private: 235 Type *getTypeByID(unsigned ID); 236 Type *getTypeByIDOrNull(unsigned ID); 237 Value *getFnValueByID(unsigned ID, Type *Ty) { 238 if (Ty && Ty->isMetadataTy()) 239 return MDValueList.getValueFwdRef(ID); 240 return ValueList.getValueFwdRef(ID, Ty); 241 } 242 BasicBlock *getBasicBlock(unsigned ID) const { 243 if (ID >= FunctionBBs.size()) return 0; // Invalid ID 244 return FunctionBBs[ID]; 245 } 246 AttributeSet getAttributes(unsigned i) const { 247 if (i-1 < MAttributes.size()) 248 return MAttributes[i-1]; 249 return AttributeSet(); 250 } 251 252 /// getValueTypePair - Read a value/type pair out of the specified record from 253 /// slot 'Slot'. Increment Slot past the number of slots used in the record. 254 /// Return true on failure. 255 bool getValueTypePair(SmallVector<uint64_t, 64> &Record, unsigned &Slot, 256 unsigned InstNum, Value *&ResVal) { 257 if (Slot == Record.size()) return true; 258 unsigned ValNo = (unsigned)Record[Slot++]; 259 if (ValNo < InstNum) { 260 // If this is not a forward reference, just return the value we already 261 // have. 262 ResVal = getFnValueByID(ValNo, 0); 263 return ResVal == 0; 264 } else if (Slot == Record.size()) { 265 return true; 266 } 267 268 unsigned TypeNo = (unsigned)Record[Slot++]; 269 ResVal = getFnValueByID(ValNo, getTypeByID(TypeNo)); 270 return ResVal == 0; 271 } 272 bool getValue(SmallVector<uint64_t, 64> &Record, unsigned &Slot, 273 Type *Ty, Value *&ResVal) { 274 if (Slot == Record.size()) return true; 275 unsigned ValNo = (unsigned)Record[Slot++]; 276 ResVal = getFnValueByID(ValNo, Ty); 277 return ResVal == 0; 278 } 279 280 281 bool ParseModule(bool Resume); 282 bool ParseAttributeBlock(); 283 bool ParseTypeTable(); 284 bool ParseOldTypeTable(); // FIXME: Remove in LLVM 3.1 285 bool ParseTypeTableBody(); 286 287 bool ParseOldTypeSymbolTable(); // FIXME: Remove in LLVM 3.1 288 bool ParseValueSymbolTable(); 289 bool ParseConstants(); 290 bool RememberAndSkipFunctionBody(); 291 bool ParseFunctionBody(Function *F); 292 bool GlobalCleanup(); 293 bool ResolveGlobalAndAliasInits(); 294 bool ParseMetadata(); 295 bool ParseMetadataAttachment(); 296 bool ParseModuleTriple(std::string &Triple); 297 bool InitStream(); 298 bool InitStreamFromBuffer(); 299 bool InitLazyStream(); 300}; 301 302} // End llvm_2_7 namespace 303 304#endif 305