Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 1 | //===- BitcodeReader.h - Internal BitcodeReader impl ------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This header defines the BitcodeReader class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame] | 14 | #ifndef LLVM_LIB_BITCODE_READER_BITCODEREADER_H |
| 15 | #define LLVM_LIB_BITCODE_READER_BITCODEREADER_H |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 16 | |
Chandler Carruth | a1514e2 | 2012-12-04 07:12:27 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/DenseMap.h" |
Chris Lattner | 48f8487 | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 18 | #include "llvm/Bitcode/BitstreamReader.h" |
Chris Lattner | 47f96bf | 2007-04-23 01:01:37 +0000 | [diff] [blame] | 19 | #include "llvm/Bitcode/LLVMBitCodes.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 20 | #include "llvm/IR/Attributes.h" |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 21 | #include "llvm/IR/GVMaterializer.h" |
Stephen Hines | ebe69fe | 2015-03-23 12:10:34 -0700 | [diff] [blame] | 22 | #include "llvm/IR/Metadata.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 23 | #include "llvm/IR/OperandTraits.h" |
Stephen Hines | ebe69fe | 2015-03-23 12:10:34 -0700 | [diff] [blame] | 24 | #include "llvm/IR/TrackingMDRef.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 25 | #include "llvm/IR/Type.h" |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 26 | #include "llvm/IR/ValueHandle.h" |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame] | 27 | #include <deque> |
Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 28 | #include <system_error> |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 29 | #include <vector> |
| 30 | |
| 31 | namespace llvm { |
Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 32 | class Comdat; |
Chris Lattner | c453f76 | 2007-04-29 07:54:31 +0000 | [diff] [blame] | 33 | class MemoryBuffer; |
Benjamin Kramer | 12ddd40 | 2009-08-11 17:45:13 +0000 | [diff] [blame] | 34 | class LLVMContext; |
Joe Abbey | 170a15e | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 35 | |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 36 | //===----------------------------------------------------------------------===// |
| 37 | // BitcodeReaderValueList Class |
| 38 | //===----------------------------------------------------------------------===// |
| 39 | |
Benjamin Kramer | 55c06ae | 2013-09-11 18:05:11 +0000 | [diff] [blame] | 40 | class BitcodeReaderValueList { |
Chris Lattner | 46e7740 | 2009-03-31 22:55:09 +0000 | [diff] [blame] | 41 | std::vector<WeakVH> ValuePtrs; |
Joe Abbey | 170a15e | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 42 | |
Chris Lattner | ea693df | 2008-08-21 02:34:16 +0000 | [diff] [blame] | 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; |
Chris Lattner | 7af453a | 2011-07-07 05:12:37 +0000 | [diff] [blame] | 52 | LLVMContext &Context; |
Chris Lattner | 522b7b1 | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 53 | public: |
Chris Lattner | 7af453a | 2011-07-07 05:12:37 +0000 | [diff] [blame] | 54 | BitcodeReaderValueList(LLVMContext &C) : Context(C) {} |
Chris Lattner | ea693df | 2008-08-21 02:34:16 +0000 | [diff] [blame] | 55 | ~BitcodeReaderValueList() { |
| 56 | assert(ResolveConstants.empty() && "Constants not resolved?"); |
| 57 | } |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 58 | |
Chris Lattner | 522b7b1 | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 59 | // vector compatibility methods |
Chris Lattner | 46e7740 | 2009-03-31 22:55:09 +0000 | [diff] [blame] | 60 | unsigned size() const { return ValuePtrs.size(); } |
| 61 | void resize(unsigned N) { ValuePtrs.resize(N); } |
Chris Lattner | 522b7b1 | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 62 | void push_back(Value *V) { |
Chris Lattner | 46e7740 | 2009-03-31 22:55:09 +0000 | [diff] [blame] | 63 | ValuePtrs.push_back(V); |
Chris Lattner | 522b7b1 | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 64 | } |
Joe Abbey | 170a15e | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 65 | |
Chris Lattner | b348bb8 | 2007-05-18 04:02:46 +0000 | [diff] [blame] | 66 | void clear() { |
Chris Lattner | ea693df | 2008-08-21 02:34:16 +0000 | [diff] [blame] | 67 | assert(ResolveConstants.empty() && "Constants not resolved?"); |
Chris Lattner | 46e7740 | 2009-03-31 22:55:09 +0000 | [diff] [blame] | 68 | ValuePtrs.clear(); |
Chris Lattner | b348bb8 | 2007-05-18 04:02:46 +0000 | [diff] [blame] | 69 | } |
Joe Abbey | 170a15e | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 70 | |
Chris Lattner | 46e7740 | 2009-03-31 22:55:09 +0000 | [diff] [blame] | 71 | Value *operator[](unsigned i) const { |
| 72 | assert(i < ValuePtrs.size()); |
| 73 | return ValuePtrs[i]; |
| 74 | } |
Joe Abbey | 170a15e | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 75 | |
Chris Lattner | 46e7740 | 2009-03-31 22:55:09 +0000 | [diff] [blame] | 76 | Value *back() const { return ValuePtrs.back(); } |
| 77 | void pop_back() { ValuePtrs.pop_back(); } |
| 78 | bool empty() const { return ValuePtrs.empty(); } |
Chris Lattner | 198f34a | 2007-04-26 03:27:58 +0000 | [diff] [blame] | 79 | void shrinkTo(unsigned N) { |
Chris Lattner | 46e7740 | 2009-03-31 22:55:09 +0000 | [diff] [blame] | 80 | assert(N <= size() && "Invalid shrinkTo request!"); |
| 81 | ValuePtrs.resize(N); |
Chris Lattner | 198f34a | 2007-04-26 03:27:58 +0000 | [diff] [blame] | 82 | } |
Joe Abbey | 170a15e | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 83 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 84 | Constant *getConstantFwdRef(unsigned Idx, Type *Ty); |
| 85 | Value *getValueFwdRef(unsigned Idx, Type *Ty); |
Joe Abbey | 170a15e | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 86 | |
Chris Lattner | 46e7740 | 2009-03-31 22:55:09 +0000 | [diff] [blame] | 87 | void AssignValue(Value *V, unsigned Idx); |
Joe Abbey | 170a15e | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 88 | |
Chris Lattner | ea693df | 2008-08-21 02:34:16 +0000 | [diff] [blame] | 89 | /// ResolveConstantForwardRefs - Once all constants are read, this method bulk |
| 90 | /// resolves any forward references. |
| 91 | void ResolveConstantForwardRefs(); |
Chris Lattner | 522b7b1 | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 92 | }; |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 93 | |
Devang Patel | d5ac404 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 94 | |
| 95 | //===----------------------------------------------------------------------===// |
| 96 | // BitcodeReaderMDValueList Class |
| 97 | //===----------------------------------------------------------------------===// |
| 98 | |
Benjamin Kramer | 55c06ae | 2013-09-11 18:05:11 +0000 | [diff] [blame] | 99 | class BitcodeReaderMDValueList { |
Stephen Hines | ebe69fe | 2015-03-23 12:10:34 -0700 | [diff] [blame] | 100 | unsigned NumFwdRefs; |
| 101 | bool AnyFwdRefs; |
| 102 | unsigned MinFwdRef; |
| 103 | unsigned MaxFwdRef; |
| 104 | std::vector<TrackingMDRef> MDValuePtrs; |
Joe Abbey | 170a15e | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 105 | |
Chris Lattner | 50b136d | 2009-10-28 05:53:48 +0000 | [diff] [blame] | 106 | LLVMContext &Context; |
Devang Patel | d5ac404 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 107 | public: |
Stephen Hines | ebe69fe | 2015-03-23 12:10:34 -0700 | [diff] [blame] | 108 | BitcodeReaderMDValueList(LLVMContext &C) |
| 109 | : NumFwdRefs(0), AnyFwdRefs(false), Context(C) {} |
Devang Patel | d5ac404 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 110 | |
| 111 | // vector compatibility methods |
| 112 | unsigned size() const { return MDValuePtrs.size(); } |
| 113 | void resize(unsigned N) { MDValuePtrs.resize(N); } |
Stephen Hines | ebe69fe | 2015-03-23 12:10:34 -0700 | [diff] [blame] | 114 | void push_back(Metadata *MD) { MDValuePtrs.emplace_back(MD); } |
Devang Patel | d5ac404 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 115 | void clear() { MDValuePtrs.clear(); } |
Stephen Hines | ebe69fe | 2015-03-23 12:10:34 -0700 | [diff] [blame] | 116 | Metadata *back() const { return MDValuePtrs.back(); } |
Devang Patel | d5ac404 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 117 | void pop_back() { MDValuePtrs.pop_back(); } |
| 118 | bool empty() const { return MDValuePtrs.empty(); } |
Joe Abbey | 170a15e | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 119 | |
Stephen Hines | ebe69fe | 2015-03-23 12:10:34 -0700 | [diff] [blame] | 120 | Metadata *operator[](unsigned i) const { |
Devang Patel | d5ac404 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 121 | assert(i < MDValuePtrs.size()); |
| 122 | return MDValuePtrs[i]; |
| 123 | } |
Joe Abbey | 170a15e | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 124 | |
Devang Patel | d5ac404 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 125 | void shrinkTo(unsigned N) { |
| 126 | assert(N <= size() && "Invalid shrinkTo request!"); |
| 127 | MDValuePtrs.resize(N); |
| 128 | } |
| 129 | |
Stephen Hines | ebe69fe | 2015-03-23 12:10:34 -0700 | [diff] [blame] | 130 | Metadata *getValueFwdRef(unsigned Idx); |
| 131 | void AssignValue(Metadata *MD, unsigned Idx); |
| 132 | void tryToResolveCycles(); |
Devang Patel | d5ac404 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 133 | }; |
| 134 | |
Benjamin Kramer | 55c06ae | 2013-09-11 18:05:11 +0000 | [diff] [blame] | 135 | class BitcodeReader : public GVMaterializer { |
Chris Lattner | 50b136d | 2009-10-28 05:53:48 +0000 | [diff] [blame] | 136 | LLVMContext &Context; |
Stephen Hines | ebe69fe | 2015-03-23 12:10:34 -0700 | [diff] [blame] | 137 | DiagnosticHandlerFunction DiagnosticHandler; |
Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 138 | Module *TheModule; |
Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 139 | std::unique_ptr<MemoryBuffer> Buffer; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 140 | std::unique_ptr<BitstreamReader> StreamFile; |
Chris Lattner | 962dde3 | 2009-04-26 20:59:02 +0000 | [diff] [blame] | 141 | BitstreamCursor Stream; |
Derek Schuff | 2ea9387 | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 142 | DataStreamer *LazyStreamer; |
| 143 | uint64_t NextUnreadBit; |
| 144 | bool SeenValueSymbolTable; |
Joe Abbey | 170a15e | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 145 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 146 | std::vector<Type*> TypeList; |
Chris Lattner | 522b7b1 | 2007-04-24 05:48:56 +0000 | [diff] [blame] | 147 | BitcodeReaderValueList ValueList; |
Devang Patel | d5ac404 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 148 | BitcodeReaderMDValueList MDValueList; |
Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 149 | std::vector<Comdat *> ComdatList; |
Devang Patel | e8e0213 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 150 | SmallVector<Instruction *, 64> InstructionList; |
| 151 | |
Chris Lattner | e16504e | 2007-04-24 03:30:34 +0000 | [diff] [blame] | 152 | std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits; |
Chris Lattner | 07d98b4 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 153 | std::vector<std::pair<GlobalAlias*, unsigned> > AliasInits; |
Peter Collingbourne | 1e3037f | 2013-09-16 01:08:15 +0000 | [diff] [blame] | 154 | std::vector<std::pair<Function*, unsigned> > FunctionPrefixes; |
Stephen Hines | ebe69fe | 2015-03-23 12:10:34 -0700 | [diff] [blame] | 155 | std::vector<std::pair<Function*, unsigned> > FunctionPrologues; |
Joe Abbey | 170a15e | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 156 | |
Manman Ren | 804f034 | 2013-09-28 00:22:27 +0000 | [diff] [blame] | 157 | SmallVector<Instruction*, 64> InstsWithTBAATag; |
| 158 | |
Devang Patel | 19c8746 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 159 | /// MAttributes - The set of attributes by index. Index zero in the |
Chris Lattner | 48c85b8 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 160 | /// file is for null, and is thus not represented here. As such all indices |
| 161 | /// are off by one. |
Bill Wendling | 99faa3b | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 162 | std::vector<AttributeSet> MAttributes; |
Joe Abbey | 170a15e | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 163 | |
Bill Wendling | c3ba0a8 | 2013-02-10 23:24:25 +0000 | [diff] [blame] | 164 | /// \brief The set of attribute groups. |
Bill Wendling | 04ef4be | 2013-02-11 22:32:29 +0000 | [diff] [blame] | 165 | std::map<unsigned, AttributeSet> MAttributeGroups; |
Bill Wendling | c3ba0a8 | 2013-02-10 23:24:25 +0000 | [diff] [blame] | 166 | |
Chris Lattner | 980e5aa | 2007-05-01 05:52:21 +0000 | [diff] [blame] | 167 | /// FunctionBBs - While parsing a function body, this is a list of the basic |
| 168 | /// blocks for the function. |
| 169 | std::vector<BasicBlock*> FunctionBBs; |
Joe Abbey | 170a15e | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 170 | |
Chris Lattner | 48f8487 | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 171 | // When reading the module header, this list is populated with functions that |
| 172 | // have bodies later in the file. |
| 173 | std::vector<Function*> FunctionsWithBodies; |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 174 | |
Joe Abbey | 170a15e | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 175 | // When intrinsic functions are encountered which require upgrading they are |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 176 | // stored here with their replacement function. |
| 177 | typedef std::vector<std::pair<Function*, Function*> > UpgradedIntrinsicMap; |
| 178 | UpgradedIntrinsicMap UpgradedIntrinsics; |
Dan Gohman | 19538d1 | 2010-07-20 21:42:28 +0000 | [diff] [blame] | 179 | |
| 180 | // Map the bitcode's custom MDKind ID to the Module's MDKind ID. |
| 181 | DenseMap<unsigned, unsigned> MDKindMap; |
Joe Abbey | 170a15e | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 182 | |
Derek Schuff | 2ea9387 | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 183 | // Several operations happen after the module header has been read, but |
| 184 | // before function bodies are processed. This keeps track of whether |
| 185 | // we've done this yet. |
| 186 | bool SeenFirstFunctionBody; |
Joe Abbey | 170a15e | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 187 | |
Chris Lattner | 48f8487 | 2007-05-01 04:59:48 +0000 | [diff] [blame] | 188 | /// DeferredFunctionInfo - When function bodies are initially scanned, this |
Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 189 | /// map contains info about where to find deferred function body in the |
| 190 | /// stream. |
| 191 | DenseMap<Function*, uint64_t> DeferredFunctionInfo; |
Joe Abbey | 170a15e | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 192 | |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame] | 193 | /// These are basic blocks forward-referenced by block addresses. They are |
| 194 | /// inserted lazily into functions when they're loaded. The basic block ID is |
| 195 | /// its index into the vector. |
| 196 | DenseMap<Function *, std::vector<BasicBlock *>> BasicBlockFwdRefs; |
| 197 | std::deque<Function *> BasicBlockFwdRefQueue; |
Dan Gohman | 9b10dfb | 2010-09-13 18:00:48 +0000 | [diff] [blame] | 198 | |
Jan Wen Voung | d9a3bad | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 199 | /// UseRelativeIDs - Indicates that we are using a new encoding for |
Jan Wen Voung | 7b8d949 | 2012-10-11 21:45:16 +0000 | [diff] [blame] | 200 | /// instruction operands where most operands in the current |
Jan Wen Voung | d9a3bad | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 201 | /// FUNCTION_BLOCK are encoded relative to the instruction number, |
| 202 | /// for a more compact encoding. Some instruction operands are not |
| 203 | /// relative to the instruction ID: basic block numbers, and types. |
| 204 | /// Once the old style function blocks have been phased out, we would |
| 205 | /// not need this flag. |
| 206 | bool UseRelativeIDs; |
| 207 | |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame] | 208 | /// True if all functions will be materialized, negating the need to process |
| 209 | /// (e.g.) blockaddress forward references. |
| 210 | bool WillMaterializeAllForwardRefs; |
| 211 | |
| 212 | /// Functions that have block addresses taken. This is usually empty. |
| 213 | SmallPtrSet<const Function *, 4> BlockAddressesTaken; |
Rafael Espindola | e076b53 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 214 | |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 215 | public: |
Stephen Hines | ebe69fe | 2015-03-23 12:10:34 -0700 | [diff] [blame] | 216 | std::error_code Error(BitcodeError E, const Twine &Message); |
| 217 | std::error_code Error(BitcodeError E); |
| 218 | std::error_code Error(const Twine &Message); |
Rafael Espindola | e076b53 | 2013-11-04 16:16:24 +0000 | [diff] [blame] | 219 | |
Stephen Hines | ebe69fe | 2015-03-23 12:10:34 -0700 | [diff] [blame] | 220 | explicit BitcodeReader(MemoryBuffer *buffer, LLVMContext &C, |
| 221 | DiagnosticHandlerFunction DiagnosticHandler); |
| 222 | explicit BitcodeReader(DataStreamer *streamer, LLVMContext &C, |
| 223 | DiagnosticHandlerFunction DiagnosticHandler); |
Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 224 | ~BitcodeReader() { FreeState(); } |
Rafael Espindola | 47f79bb | 2012-01-02 07:49:53 +0000 | [diff] [blame] | 225 | |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame] | 226 | std::error_code materializeForwardReferencedFunctions(); |
Rafael Espindola | 47f79bb | 2012-01-02 07:49:53 +0000 | [diff] [blame] | 227 | |
Chris Lattner | b348bb8 | 2007-05-18 04:02:46 +0000 | [diff] [blame] | 228 | void FreeState(); |
Joe Abbey | 170a15e | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 229 | |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame] | 230 | void releaseBuffer(); |
Joe Abbey | 170a15e | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 231 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 232 | bool isDematerializable(const GlobalValue *GV) const override; |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame] | 233 | std::error_code materialize(GlobalValue *GV) override; |
Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 234 | std::error_code MaterializeModule(Module *M) override; |
Stephen Hines | ebe69fe | 2015-03-23 12:10:34 -0700 | [diff] [blame] | 235 | std::vector<StructType *> getIdentifiedStructTypes() const override; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 236 | void Dematerialize(GlobalValue *GV) override; |
Chris Lattner | d67c632 | 2007-05-15 06:29:44 +0000 | [diff] [blame] | 237 | |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 238 | /// @brief Main interface to parsing a bitcode buffer. |
| 239 | /// @returns true if an error occurred. |
Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 240 | std::error_code ParseBitcodeInto(Module *M); |
Bill Wendling | 3471174 | 2010-10-06 01:22:42 +0000 | [diff] [blame] | 241 | |
| 242 | /// @brief Cheap mechanism to just extract module triple |
| 243 | /// @returns true if an error occurred. |
Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 244 | ErrorOr<std::string> parseTriple(); |
Jan Wen Voung | d9a3bad | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 245 | |
| 246 | static uint64_t decodeSignRotatedValue(uint64_t V); |
| 247 | |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 248 | private: |
Stephen Hines | ebe69fe | 2015-03-23 12:10:34 -0700 | [diff] [blame] | 249 | std::vector<StructType *> IdentifiedStructTypes; |
| 250 | StructType *createIdentifiedStructType(LLVMContext &Context, StringRef Name); |
| 251 | StructType *createIdentifiedStructType(LLVMContext &Context); |
| 252 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 253 | Type *getTypeByID(unsigned ID); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 254 | Value *getFnValueByID(unsigned ID, Type *Ty) { |
Chris Lattner | cbd40f8 | 2011-07-07 05:29:18 +0000 | [diff] [blame] | 255 | if (Ty && Ty->isMetadataTy()) |
Stephen Hines | ebe69fe | 2015-03-23 12:10:34 -0700 | [diff] [blame] | 256 | return MetadataAsValue::get(Ty->getContext(), getFnMetadataByID(ID)); |
Chris Lattner | 7af453a | 2011-07-07 05:12:37 +0000 | [diff] [blame] | 257 | return ValueList.getValueFwdRef(ID, Ty); |
Chris Lattner | a7c49aa | 2007-05-01 07:01:57 +0000 | [diff] [blame] | 258 | } |
Stephen Hines | ebe69fe | 2015-03-23 12:10:34 -0700 | [diff] [blame] | 259 | Metadata *getFnMetadataByID(unsigned ID) { |
| 260 | return MDValueList.getValueFwdRef(ID); |
| 261 | } |
Chris Lattner | f4c8e52 | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 262 | BasicBlock *getBasicBlock(unsigned ID) const { |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 263 | if (ID >= FunctionBBs.size()) return nullptr; // Invalid ID |
Chris Lattner | f4c8e52 | 2007-05-02 05:46:45 +0000 | [diff] [blame] | 264 | return FunctionBBs[ID]; |
| 265 | } |
Bill Wendling | 99faa3b | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 266 | AttributeSet getAttributes(unsigned i) const { |
Devang Patel | 19c8746 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 267 | if (i-1 < MAttributes.size()) |
| 268 | return MAttributes[i-1]; |
Bill Wendling | 99faa3b | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 269 | return AttributeSet(); |
Chris Lattner | 48c85b8 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 270 | } |
Joe Abbey | 170a15e | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 271 | |
Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 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. |
Craig Topper | 9e639e8 | 2013-07-11 16:22:38 +0000 | [diff] [blame] | 275 | bool getValueTypePair(SmallVectorImpl<uint64_t> &Record, unsigned &Slot, |
Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 276 | unsigned InstNum, Value *&ResVal) { |
| 277 | if (Slot == Record.size()) return true; |
Jeff Cohen | 650c938 | 2007-05-06 03:23:14 +0000 | [diff] [blame] | 278 | unsigned ValNo = (unsigned)Record[Slot++]; |
Jan Wen Voung | d9a3bad | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 279 | // Adjust the ValNo, if it was encoded relative to the InstNum. |
| 280 | if (UseRelativeIDs) |
| 281 | ValNo = InstNum - ValNo; |
Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 282 | if (ValNo < InstNum) { |
| 283 | // If this is not a forward reference, just return the value we already |
| 284 | // have. |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 285 | ResVal = getFnValueByID(ValNo, nullptr); |
| 286 | return ResVal == nullptr; |
Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 287 | } else if (Slot == Record.size()) { |
| 288 | return true; |
| 289 | } |
Jan Wen Voung | d9a3bad | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 290 | |
Jeff Cohen | 650c938 | 2007-05-06 03:23:14 +0000 | [diff] [blame] | 291 | unsigned TypeNo = (unsigned)Record[Slot++]; |
Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 292 | ResVal = getFnValueByID(ValNo, getTypeByID(TypeNo)); |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 293 | return ResVal == nullptr; |
Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 294 | } |
Jan Wen Voung | d9a3bad | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 295 | |
| 296 | /// popValue - Read a value out of the specified record from slot 'Slot'. |
| 297 | /// Increment Slot past the number of slots used by the value in the record. |
| 298 | /// Return true if there is an error. |
Craig Topper | 9e639e8 | 2013-07-11 16:22:38 +0000 | [diff] [blame] | 299 | bool popValue(SmallVectorImpl<uint64_t> &Record, unsigned &Slot, |
Jan Wen Voung | d9a3bad | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 300 | unsigned InstNum, Type *Ty, Value *&ResVal) { |
| 301 | if (getValue(Record, Slot, InstNum, Ty, ResVal)) |
| 302 | return true; |
| 303 | // All values currently take a single record slot. |
| 304 | ++Slot; |
| 305 | return false; |
| 306 | } |
| 307 | |
| 308 | /// getValue -- Like popValue, but does not increment the Slot number. |
Craig Topper | 9e639e8 | 2013-07-11 16:22:38 +0000 | [diff] [blame] | 309 | bool getValue(SmallVectorImpl<uint64_t> &Record, unsigned Slot, |
Jan Wen Voung | d9a3bad | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 310 | unsigned InstNum, Type *Ty, Value *&ResVal) { |
| 311 | ResVal = getValue(Record, Slot, InstNum, Ty); |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 312 | return ResVal == nullptr; |
Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 313 | } |
Chris Lattner | 48c85b8 | 2007-05-04 03:30:17 +0000 | [diff] [blame] | 314 | |
Jan Wen Voung | d9a3bad | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 315 | /// getValue -- Version of getValue that returns ResVal directly, |
| 316 | /// or 0 if there is an error. |
Craig Topper | 9e639e8 | 2013-07-11 16:22:38 +0000 | [diff] [blame] | 317 | Value *getValue(SmallVectorImpl<uint64_t> &Record, unsigned Slot, |
Jan Wen Voung | d9a3bad | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 318 | unsigned InstNum, Type *Ty) { |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 319 | if (Slot == Record.size()) return nullptr; |
Jan Wen Voung | d9a3bad | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 320 | unsigned ValNo = (unsigned)Record[Slot]; |
| 321 | // Adjust the ValNo, if it was encoded relative to the InstNum. |
| 322 | if (UseRelativeIDs) |
| 323 | ValNo = InstNum - ValNo; |
| 324 | return getFnValueByID(ValNo, Ty); |
| 325 | } |
| 326 | |
| 327 | /// getValueSigned -- Like getValue, but decodes signed VBRs. |
Craig Topper | 9e639e8 | 2013-07-11 16:22:38 +0000 | [diff] [blame] | 328 | Value *getValueSigned(SmallVectorImpl<uint64_t> &Record, unsigned Slot, |
Jan Wen Voung | d9a3bad | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 329 | unsigned InstNum, Type *Ty) { |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 330 | if (Slot == Record.size()) return nullptr; |
Jan Wen Voung | d9a3bad | 2012-10-11 20:20:40 +0000 | [diff] [blame] | 331 | unsigned ValNo = (unsigned)decodeSignRotatedValue(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 | |
Stephen Hines | ebe69fe | 2015-03-23 12:10:34 -0700 | [diff] [blame] | 338 | /// Converts alignment exponent (i.e. power of two (or zero)) to the |
| 339 | /// corresponding alignment to use. If alignment is too large, returns |
| 340 | /// a corresponding error code. |
| 341 | std::error_code parseAlignmentValue(uint64_t Exponent, unsigned &Alignment); |
Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 342 | std::error_code ParseAttrKind(uint64_t Code, Attribute::AttrKind *Kind); |
| 343 | std::error_code ParseModule(bool Resume); |
| 344 | std::error_code ParseAttributeBlock(); |
| 345 | std::error_code ParseAttributeGroupBlock(); |
| 346 | std::error_code ParseTypeTable(); |
| 347 | std::error_code ParseTypeTableBody(); |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 348 | |
Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 349 | std::error_code ParseValueSymbolTable(); |
| 350 | std::error_code ParseConstants(); |
| 351 | std::error_code RememberAndSkipFunctionBody(); |
| 352 | std::error_code ParseFunctionBody(Function *F); |
| 353 | std::error_code GlobalCleanup(); |
| 354 | std::error_code ResolveGlobalAndAliasInits(); |
| 355 | std::error_code ParseMetadata(); |
| 356 | std::error_code ParseMetadataAttachment(); |
| 357 | ErrorOr<std::string> parseModuleTriple(); |
| 358 | std::error_code ParseUseLists(); |
| 359 | std::error_code InitStream(); |
| 360 | std::error_code InitStreamFromBuffer(); |
| 361 | std::error_code InitLazyStream(); |
| 362 | std::error_code FindFunctionInStream( |
| 363 | Function *F, |
| 364 | DenseMap<Function *, uint64_t>::iterator DeferredFunctionInfoIterator); |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 365 | }; |
Joe Abbey | 170a15e | 2012-11-25 15:23:39 +0000 | [diff] [blame] | 366 | |
Chris Lattner | caee0dc | 2007-04-22 06:23:29 +0000 | [diff] [blame] | 367 | } // End llvm namespace |
| 368 | |
| 369 | #endif |