Chris Lattner | 1a9df8e | 2007-04-29 05:31:57 +0000 | [diff] [blame] | 1 | //===--- Bitcode/Writer/BitcodeWriter.cpp - Bitcode Writer ----------------===// |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 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 | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // Bitcode writer implementation. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/Bitcode/ReaderWriter.h" |
| 15 | #include "llvm/Bitcode/BitstreamWriter.h" |
Chris Lattner | 47f96bf | 2007-04-23 01:01:37 +0000 | [diff] [blame] | 16 | #include "llvm/Bitcode/LLVMBitCodes.h" |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 17 | #include "ValueEnumerator.h" |
Chris Lattner | 2edd22b | 2007-04-24 00:16:04 +0000 | [diff] [blame] | 18 | #include "llvm/Constants.h" |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 19 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 2bce93a | 2007-05-06 01:58:20 +0000 | [diff] [blame] | 20 | #include "llvm/InlineAsm.h" |
Chris Lattner | b9d0c2a | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 21 | #include "llvm/Instructions.h" |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 22 | #include "llvm/Module.h" |
Dan Gohman | 1224c38 | 2009-07-20 21:19:07 +0000 | [diff] [blame] | 23 | #include "llvm/Operator.h" |
Chris Lattner | b992be1 | 2007-04-23 20:35:01 +0000 | [diff] [blame] | 24 | #include "llvm/ValueSymbolTable.h" |
Evan Cheng | e889c69 | 2011-06-14 01:51:33 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/Triple.h" |
Chad Rosier | cbbb096 | 2011-12-07 21:44:12 +0000 | [diff] [blame] | 26 | #include "llvm/Support/CommandLine.h" |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 27 | #include "llvm/Support/ErrorHandling.h" |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 28 | #include "llvm/Support/MathExtras.h" |
Daniel Dunbar | d1ce3b4 | 2008-10-22 17:39:14 +0000 | [diff] [blame] | 29 | #include "llvm/Support/raw_ostream.h" |
Michael J. Spencer | 1f6efa3 | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Program.h" |
Nick Lewycky | 476b242 | 2010-12-19 20:43:38 +0000 | [diff] [blame] | 31 | #include <cctype> |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 32 | #include <map> |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 33 | using namespace llvm; |
| 34 | |
Chad Rosier | cbbb096 | 2011-12-07 21:44:12 +0000 | [diff] [blame] | 35 | static cl::opt<bool> |
| 36 | EnablePreserveUseListOrdering("enable-bc-uselist-preserve", |
| 37 | cl::desc("Turn on experimental support for " |
| 38 | "use-list order preservation."), |
| 39 | cl::init(false), cl::Hidden); |
| 40 | |
Chris Lattner | 5969830 | 2007-05-04 20:52:02 +0000 | [diff] [blame] | 41 | /// These are manifest constants used by the bitcode writer. They do not need to |
| 42 | /// be kept in sync with the reader, but need to be consistent within this file. |
| 43 | enum { |
| 44 | CurVersion = 0, |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 45 | |
Chris Lattner | 5969830 | 2007-05-04 20:52:02 +0000 | [diff] [blame] | 46 | // VALUE_SYMTAB_BLOCK abbrev id's. |
| 47 | VST_ENTRY_8_ABBREV = bitc::FIRST_APPLICATION_ABBREV, |
Chris Lattner | 2453f71 | 2007-05-04 20:58:35 +0000 | [diff] [blame] | 48 | VST_ENTRY_7_ABBREV, |
Chris Lattner | ff294a4 | 2007-05-05 01:26:50 +0000 | [diff] [blame] | 49 | VST_ENTRY_6_ABBREV, |
Chris Lattner | a0f1ecc | 2007-05-05 07:36:14 +0000 | [diff] [blame] | 50 | VST_BBENTRY_6_ABBREV, |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 51 | |
Chris Lattner | a0f1ecc | 2007-05-05 07:36:14 +0000 | [diff] [blame] | 52 | // CONSTANTS_BLOCK abbrev id's. |
| 53 | CONSTANTS_SETTYPE_ABBREV = bitc::FIRST_APPLICATION_ABBREV, |
| 54 | CONSTANTS_INTEGER_ABBREV, |
| 55 | CONSTANTS_CE_CAST_Abbrev, |
Chris Lattner | 440168b | 2007-05-05 07:44:49 +0000 | [diff] [blame] | 56 | CONSTANTS_NULL_Abbrev, |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 57 | |
Chris Lattner | 440168b | 2007-05-05 07:44:49 +0000 | [diff] [blame] | 58 | // FUNCTION_BLOCK abbrev id's. |
Chris Lattner | 94687ac | 2007-05-06 01:28:01 +0000 | [diff] [blame] | 59 | FUNCTION_INST_LOAD_ABBREV = bitc::FIRST_APPLICATION_ABBREV, |
Chris Lattner | f9f2e83 | 2007-05-06 02:38:57 +0000 | [diff] [blame] | 60 | FUNCTION_INST_BINOP_ABBREV, |
Dan Gohman | 1224c38 | 2009-07-20 21:19:07 +0000 | [diff] [blame] | 61 | FUNCTION_INST_BINOP_FLAGS_ABBREV, |
Chris Lattner | f9f2e83 | 2007-05-06 02:38:57 +0000 | [diff] [blame] | 62 | FUNCTION_INST_CAST_ABBREV, |
Chris Lattner | 94687ac | 2007-05-06 01:28:01 +0000 | [diff] [blame] | 63 | FUNCTION_INST_RET_VOID_ABBREV, |
| 64 | FUNCTION_INST_RET_VAL_ABBREV, |
Stepan Dyatkovskiy | 1f98383 | 2012-05-08 08:33:21 +0000 | [diff] [blame] | 65 | FUNCTION_INST_UNREACHABLE_ABBREV |
Chris Lattner | 5969830 | 2007-05-04 20:52:02 +0000 | [diff] [blame] | 66 | }; |
| 67 | |
Chris Lattner | f581c3b | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 68 | static unsigned GetEncodedCastOpcode(unsigned Opcode) { |
| 69 | switch (Opcode) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 70 | default: llvm_unreachable("Unknown cast instruction!"); |
Chris Lattner | f581c3b | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 71 | case Instruction::Trunc : return bitc::CAST_TRUNC; |
| 72 | case Instruction::ZExt : return bitc::CAST_ZEXT; |
| 73 | case Instruction::SExt : return bitc::CAST_SEXT; |
| 74 | case Instruction::FPToUI : return bitc::CAST_FPTOUI; |
| 75 | case Instruction::FPToSI : return bitc::CAST_FPTOSI; |
| 76 | case Instruction::UIToFP : return bitc::CAST_UITOFP; |
| 77 | case Instruction::SIToFP : return bitc::CAST_SITOFP; |
| 78 | case Instruction::FPTrunc : return bitc::CAST_FPTRUNC; |
| 79 | case Instruction::FPExt : return bitc::CAST_FPEXT; |
| 80 | case Instruction::PtrToInt: return bitc::CAST_PTRTOINT; |
| 81 | case Instruction::IntToPtr: return bitc::CAST_INTTOPTR; |
| 82 | case Instruction::BitCast : return bitc::CAST_BITCAST; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | static unsigned GetEncodedBinaryOpcode(unsigned Opcode) { |
| 87 | switch (Opcode) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 88 | default: llvm_unreachable("Unknown binary instruction!"); |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 89 | case Instruction::Add: |
| 90 | case Instruction::FAdd: return bitc::BINOP_ADD; |
| 91 | case Instruction::Sub: |
| 92 | case Instruction::FSub: return bitc::BINOP_SUB; |
| 93 | case Instruction::Mul: |
| 94 | case Instruction::FMul: return bitc::BINOP_MUL; |
Chris Lattner | f581c3b | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 95 | case Instruction::UDiv: return bitc::BINOP_UDIV; |
| 96 | case Instruction::FDiv: |
| 97 | case Instruction::SDiv: return bitc::BINOP_SDIV; |
| 98 | case Instruction::URem: return bitc::BINOP_UREM; |
| 99 | case Instruction::FRem: |
| 100 | case Instruction::SRem: return bitc::BINOP_SREM; |
| 101 | case Instruction::Shl: return bitc::BINOP_SHL; |
| 102 | case Instruction::LShr: return bitc::BINOP_LSHR; |
| 103 | case Instruction::AShr: return bitc::BINOP_ASHR; |
| 104 | case Instruction::And: return bitc::BINOP_AND; |
| 105 | case Instruction::Or: return bitc::BINOP_OR; |
| 106 | case Instruction::Xor: return bitc::BINOP_XOR; |
| 107 | } |
| 108 | } |
| 109 | |
Eli Friedman | ff03048 | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 110 | static unsigned GetEncodedRMWOperation(AtomicRMWInst::BinOp Op) { |
| 111 | switch (Op) { |
| 112 | default: llvm_unreachable("Unknown RMW operation!"); |
| 113 | case AtomicRMWInst::Xchg: return bitc::RMW_XCHG; |
| 114 | case AtomicRMWInst::Add: return bitc::RMW_ADD; |
| 115 | case AtomicRMWInst::Sub: return bitc::RMW_SUB; |
| 116 | case AtomicRMWInst::And: return bitc::RMW_AND; |
| 117 | case AtomicRMWInst::Nand: return bitc::RMW_NAND; |
| 118 | case AtomicRMWInst::Or: return bitc::RMW_OR; |
| 119 | case AtomicRMWInst::Xor: return bitc::RMW_XOR; |
| 120 | case AtomicRMWInst::Max: return bitc::RMW_MAX; |
| 121 | case AtomicRMWInst::Min: return bitc::RMW_MIN; |
| 122 | case AtomicRMWInst::UMax: return bitc::RMW_UMAX; |
| 123 | case AtomicRMWInst::UMin: return bitc::RMW_UMIN; |
| 124 | } |
| 125 | } |
| 126 | |
Eli Friedman | 47f3513 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 127 | static unsigned GetEncodedOrdering(AtomicOrdering Ordering) { |
| 128 | switch (Ordering) { |
Eli Friedman | 47f3513 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 129 | case NotAtomic: return bitc::ORDERING_NOTATOMIC; |
| 130 | case Unordered: return bitc::ORDERING_UNORDERED; |
| 131 | case Monotonic: return bitc::ORDERING_MONOTONIC; |
| 132 | case Acquire: return bitc::ORDERING_ACQUIRE; |
| 133 | case Release: return bitc::ORDERING_RELEASE; |
| 134 | case AcquireRelease: return bitc::ORDERING_ACQREL; |
| 135 | case SequentiallyConsistent: return bitc::ORDERING_SEQCST; |
| 136 | } |
Chandler Carruth | 732f05c | 2012-01-10 18:08:01 +0000 | [diff] [blame] | 137 | llvm_unreachable("Invalid ordering"); |
Eli Friedman | 47f3513 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | static unsigned GetEncodedSynchScope(SynchronizationScope SynchScope) { |
| 141 | switch (SynchScope) { |
Eli Friedman | 47f3513 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 142 | case SingleThread: return bitc::SYNCHSCOPE_SINGLETHREAD; |
| 143 | case CrossThread: return bitc::SYNCHSCOPE_CROSSTHREAD; |
| 144 | } |
Chandler Carruth | 732f05c | 2012-01-10 18:08:01 +0000 | [diff] [blame] | 145 | llvm_unreachable("Invalid synch scope"); |
Eli Friedman | 47f3513 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 148 | static void WriteStringRecord(unsigned Code, StringRef Str, |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 149 | unsigned AbbrevToUse, BitstreamWriter &Stream) { |
| 150 | SmallVector<unsigned, 64> Vals; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 151 | |
Chris Lattner | 15e6d17 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 152 | // Code: [strchar x N] |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 153 | for (unsigned i = 0, e = Str.size(); i != e; ++i) { |
| 154 | if (AbbrevToUse && !BitCodeAbbrevOp::isChar6(Str[i])) |
| 155 | AbbrevToUse = 0; |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 156 | Vals.push_back(Str[i]); |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 157 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 158 | |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 159 | // Emit the finished record. |
| 160 | Stream.EmitRecord(Code, Vals, AbbrevToUse); |
| 161 | } |
| 162 | |
Chris Lattner | 62bbeea | 2007-05-04 00:44:52 +0000 | [diff] [blame] | 163 | // Emit information about parameter attributes. |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 164 | static void WriteAttributeTable(const ValueEnumerator &VE, |
Chris Lattner | 62bbeea | 2007-05-04 00:44:52 +0000 | [diff] [blame] | 165 | BitstreamWriter &Stream) { |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 166 | const std::vector<AttrListPtr> &Attrs = VE.getAttributes(); |
Chris Lattner | 62bbeea | 2007-05-04 00:44:52 +0000 | [diff] [blame] | 167 | if (Attrs.empty()) return; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 168 | |
Chris Lattner | f0a6531 | 2007-05-04 02:59:04 +0000 | [diff] [blame] | 169 | Stream.EnterSubblock(bitc::PARAMATTR_BLOCK_ID, 3); |
| 170 | |
| 171 | SmallVector<uint64_t, 64> Record; |
| 172 | for (unsigned i = 0, e = Attrs.size(); i != e; ++i) { |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 173 | const AttrListPtr &A = Attrs[i]; |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 174 | for (unsigned i = 0, e = A.getNumSlots(); i != e; ++i) { |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 175 | const AttributeWithIndex &PAWI = A.getSlot(i); |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 176 | Record.push_back(PAWI.Index); |
Nick Lewycky | 73ddd4f | 2008-12-19 09:38:31 +0000 | [diff] [blame] | 177 | |
| 178 | // FIXME: remove in LLVM 3.0 |
| 179 | // Store the alignment in the bitcode as a 16-bit raw value instead of a |
| 180 | // 5-bit log2 encoded value. Shift the bits above the alignment up by |
| 181 | // 11 bits. |
Kostya Serebryany | 164b86b | 2012-01-20 17:56:17 +0000 | [diff] [blame] | 182 | uint64_t FauxAttr = PAWI.Attrs.Raw() & 0xffff; |
Nick Lewycky | 73ddd4f | 2008-12-19 09:38:31 +0000 | [diff] [blame] | 183 | if (PAWI.Attrs & Attribute::Alignment) |
Kostya Serebryany | 164b86b | 2012-01-20 17:56:17 +0000 | [diff] [blame] | 184 | FauxAttr |= (1ull<<16)<< |
| 185 | (((PAWI.Attrs & Attribute::Alignment).Raw()-1) >> 16); |
| 186 | FauxAttr |= (PAWI.Attrs.Raw() & (0x3FFull << 21)) << 11; |
Nick Lewycky | 73ddd4f | 2008-12-19 09:38:31 +0000 | [diff] [blame] | 187 | |
| 188 | Record.push_back(FauxAttr); |
Chris Lattner | f0a6531 | 2007-05-04 02:59:04 +0000 | [diff] [blame] | 189 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 190 | |
Chris Lattner | f0a6531 | 2007-05-04 02:59:04 +0000 | [diff] [blame] | 191 | Stream.EmitRecord(bitc::PARAMATTR_CODE_ENTRY, Record); |
| 192 | Record.clear(); |
| 193 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 194 | |
Chris Lattner | f0a6531 | 2007-05-04 02:59:04 +0000 | [diff] [blame] | 195 | Stream.ExitBlock(); |
Chris Lattner | 62bbeea | 2007-05-04 00:44:52 +0000 | [diff] [blame] | 196 | } |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 197 | |
| 198 | /// WriteTypeTable - Write out the type table for a module. |
| 199 | static void WriteTypeTable(const ValueEnumerator &VE, BitstreamWriter &Stream) { |
| 200 | const ValueEnumerator::TypeList &TypeList = VE.getTypes(); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 201 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 202 | Stream.EnterSubblock(bitc::TYPE_BLOCK_ID_NEW, 4 /*count from # abbrevs */); |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 203 | SmallVector<uint64_t, 64> TypeVals; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 204 | |
Chad Rosier | 667f826 | 2011-12-08 00:38:45 +0000 | [diff] [blame] | 205 | uint64_t NumBits = Log2_32_Ceil(VE.getTypes().size()+1); |
| 206 | |
Chris Lattner | d092e0e | 2007-05-05 06:30:12 +0000 | [diff] [blame] | 207 | // Abbrev for TYPE_CODE_POINTER. |
| 208 | BitCodeAbbrev *Abbv = new BitCodeAbbrev(); |
| 209 | Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_POINTER)); |
Chad Rosier | 667f826 | 2011-12-08 00:38:45 +0000 | [diff] [blame] | 210 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits)); |
Christopher Lamb | d49e18d | 2007-12-12 08:44:39 +0000 | [diff] [blame] | 211 | Abbv->Add(BitCodeAbbrevOp(0)); // Addrspace = 0 |
Chris Lattner | d092e0e | 2007-05-05 06:30:12 +0000 | [diff] [blame] | 212 | unsigned PtrAbbrev = Stream.EmitAbbrev(Abbv); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 213 | |
Chris Lattner | d092e0e | 2007-05-05 06:30:12 +0000 | [diff] [blame] | 214 | // Abbrev for TYPE_CODE_FUNCTION. |
| 215 | Abbv = new BitCodeAbbrev(); |
| 216 | Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_FUNCTION)); |
| 217 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isvararg |
Chris Lattner | d092e0e | 2007-05-05 06:30:12 +0000 | [diff] [blame] | 218 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); |
Chad Rosier | 667f826 | 2011-12-08 00:38:45 +0000 | [diff] [blame] | 219 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits)); |
| 220 | |
Chris Lattner | d092e0e | 2007-05-05 06:30:12 +0000 | [diff] [blame] | 221 | unsigned FunctionAbbrev = Stream.EmitAbbrev(Abbv); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 222 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 223 | // Abbrev for TYPE_CODE_STRUCT_ANON. |
Chris Lattner | d092e0e | 2007-05-05 06:30:12 +0000 | [diff] [blame] | 224 | Abbv = new BitCodeAbbrev(); |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 225 | Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_ANON)); |
Chris Lattner | d092e0e | 2007-05-05 06:30:12 +0000 | [diff] [blame] | 226 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ispacked |
| 227 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); |
Chad Rosier | 667f826 | 2011-12-08 00:38:45 +0000 | [diff] [blame] | 228 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits)); |
| 229 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 230 | unsigned StructAnonAbbrev = Stream.EmitAbbrev(Abbv); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 231 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 232 | // Abbrev for TYPE_CODE_STRUCT_NAME. |
| 233 | Abbv = new BitCodeAbbrev(); |
| 234 | Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_NAME)); |
| 235 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); |
| 236 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6)); |
| 237 | unsigned StructNameAbbrev = Stream.EmitAbbrev(Abbv); |
| 238 | |
| 239 | // Abbrev for TYPE_CODE_STRUCT_NAMED. |
| 240 | Abbv = new BitCodeAbbrev(); |
| 241 | Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_NAMED)); |
| 242 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ispacked |
| 243 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); |
Chad Rosier | 667f826 | 2011-12-08 00:38:45 +0000 | [diff] [blame] | 244 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits)); |
| 245 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 246 | unsigned StructNamedAbbrev = Stream.EmitAbbrev(Abbv); |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 247 | |
Chris Lattner | d092e0e | 2007-05-05 06:30:12 +0000 | [diff] [blame] | 248 | // Abbrev for TYPE_CODE_ARRAY. |
| 249 | Abbv = new BitCodeAbbrev(); |
| 250 | Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_ARRAY)); |
| 251 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // size |
Chad Rosier | 667f826 | 2011-12-08 00:38:45 +0000 | [diff] [blame] | 252 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits)); |
| 253 | |
Chris Lattner | d092e0e | 2007-05-05 06:30:12 +0000 | [diff] [blame] | 254 | unsigned ArrayAbbrev = Stream.EmitAbbrev(Abbv); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 255 | |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 256 | // Emit an entry count so the reader can reserve space. |
| 257 | TypeVals.push_back(TypeList.size()); |
| 258 | Stream.EmitRecord(bitc::TYPE_CODE_NUMENTRY, TypeVals); |
| 259 | TypeVals.clear(); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 260 | |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 261 | // Loop over all of the types, emitting each in turn. |
| 262 | for (unsigned i = 0, e = TypeList.size(); i != e; ++i) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 263 | Type *T = TypeList[i]; |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 264 | int AbbrevToUse = 0; |
| 265 | unsigned Code = 0; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 266 | |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 267 | switch (T->getTypeID()) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 268 | default: llvm_unreachable("Unknown type!"); |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 269 | case Type::VoidTyID: Code = bitc::TYPE_CODE_VOID; break; |
Dan Gohman | ce16339 | 2011-12-17 00:04:22 +0000 | [diff] [blame] | 270 | case Type::HalfTyID: Code = bitc::TYPE_CODE_HALF; break; |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 271 | case Type::FloatTyID: Code = bitc::TYPE_CODE_FLOAT; break; |
| 272 | case Type::DoubleTyID: Code = bitc::TYPE_CODE_DOUBLE; break; |
| 273 | case Type::X86_FP80TyID: Code = bitc::TYPE_CODE_X86_FP80; break; |
| 274 | case Type::FP128TyID: Code = bitc::TYPE_CODE_FP128; break; |
Dale Johannesen | 320fc8a | 2007-08-03 01:03:46 +0000 | [diff] [blame] | 275 | case Type::PPC_FP128TyID: Code = bitc::TYPE_CODE_PPC_FP128; break; |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 276 | case Type::LabelTyID: Code = bitc::TYPE_CODE_LABEL; break; |
| 277 | case Type::MetadataTyID: Code = bitc::TYPE_CODE_METADATA; break; |
| 278 | case Type::X86_MMXTyID: Code = bitc::TYPE_CODE_X86_MMX; break; |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 279 | case Type::IntegerTyID: |
| 280 | // INTEGER: [width] |
| 281 | Code = bitc::TYPE_CODE_INTEGER; |
| 282 | TypeVals.push_back(cast<IntegerType>(T)->getBitWidth()); |
| 283 | break; |
Duncan Sands | 216b74c | 2007-12-11 12:20:47 +0000 | [diff] [blame] | 284 | case Type::PointerTyID: { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 285 | PointerType *PTy = cast<PointerType>(T); |
Christopher Lamb | d49e18d | 2007-12-12 08:44:39 +0000 | [diff] [blame] | 286 | // POINTER: [pointee type, address space] |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 287 | Code = bitc::TYPE_CODE_POINTER; |
Christopher Lamb | fe63fb9 | 2007-12-11 08:59:05 +0000 | [diff] [blame] | 288 | TypeVals.push_back(VE.getTypeID(PTy->getElementType())); |
Christopher Lamb | d49e18d | 2007-12-12 08:44:39 +0000 | [diff] [blame] | 289 | unsigned AddressSpace = PTy->getAddressSpace(); |
| 290 | TypeVals.push_back(AddressSpace); |
| 291 | if (AddressSpace == 0) AbbrevToUse = PtrAbbrev; |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 292 | break; |
Duncan Sands | 216b74c | 2007-12-11 12:20:47 +0000 | [diff] [blame] | 293 | } |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 294 | case Type::FunctionTyID: { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 295 | FunctionType *FT = cast<FunctionType>(T); |
Chad Rosier | cde5464 | 2011-11-03 00:14:01 +0000 | [diff] [blame] | 296 | // FUNCTION: [isvararg, retty, paramty x N] |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 297 | Code = bitc::TYPE_CODE_FUNCTION; |
| 298 | TypeVals.push_back(FT->isVarArg()); |
| 299 | TypeVals.push_back(VE.getTypeID(FT->getReturnType())); |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 300 | for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) |
| 301 | TypeVals.push_back(VE.getTypeID(FT->getParamType(i))); |
Chris Lattner | d092e0e | 2007-05-05 06:30:12 +0000 | [diff] [blame] | 302 | AbbrevToUse = FunctionAbbrev; |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 303 | break; |
| 304 | } |
| 305 | case Type::StructTyID: { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 306 | StructType *ST = cast<StructType>(T); |
Chris Lattner | d092e0e | 2007-05-05 06:30:12 +0000 | [diff] [blame] | 307 | // STRUCT: [ispacked, eltty x N] |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 308 | TypeVals.push_back(ST->isPacked()); |
Chris Lattner | 15e6d17 | 2007-05-04 19:11:41 +0000 | [diff] [blame] | 309 | // Output all of the element types. |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 310 | for (StructType::element_iterator I = ST->element_begin(), |
| 311 | E = ST->element_end(); I != E; ++I) |
| 312 | TypeVals.push_back(VE.getTypeID(*I)); |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 313 | |
Chris Lattner | 3ebb649 | 2011-08-12 18:06:37 +0000 | [diff] [blame] | 314 | if (ST->isLiteral()) { |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 315 | Code = bitc::TYPE_CODE_STRUCT_ANON; |
| 316 | AbbrevToUse = StructAnonAbbrev; |
| 317 | } else { |
| 318 | if (ST->isOpaque()) { |
| 319 | Code = bitc::TYPE_CODE_OPAQUE; |
| 320 | } else { |
| 321 | Code = bitc::TYPE_CODE_STRUCT_NAMED; |
| 322 | AbbrevToUse = StructNamedAbbrev; |
| 323 | } |
| 324 | |
| 325 | // Emit the name if it is present. |
| 326 | if (!ST->getName().empty()) |
| 327 | WriteStringRecord(bitc::TYPE_CODE_STRUCT_NAME, ST->getName(), |
| 328 | StructNameAbbrev, Stream); |
| 329 | } |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 330 | break; |
| 331 | } |
| 332 | case Type::ArrayTyID: { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 333 | ArrayType *AT = cast<ArrayType>(T); |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 334 | // ARRAY: [numelts, eltty] |
| 335 | Code = bitc::TYPE_CODE_ARRAY; |
| 336 | TypeVals.push_back(AT->getNumElements()); |
| 337 | TypeVals.push_back(VE.getTypeID(AT->getElementType())); |
Chris Lattner | d092e0e | 2007-05-05 06:30:12 +0000 | [diff] [blame] | 338 | AbbrevToUse = ArrayAbbrev; |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 339 | break; |
| 340 | } |
| 341 | case Type::VectorTyID: { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 342 | VectorType *VT = cast<VectorType>(T); |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 343 | // VECTOR [numelts, eltty] |
| 344 | Code = bitc::TYPE_CODE_VECTOR; |
| 345 | TypeVals.push_back(VT->getNumElements()); |
| 346 | TypeVals.push_back(VE.getTypeID(VT->getElementType())); |
| 347 | break; |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | // Emit the finished record. |
| 352 | Stream.EmitRecord(Code, TypeVals, AbbrevToUse); |
| 353 | TypeVals.clear(); |
| 354 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 355 | |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 356 | Stream.ExitBlock(); |
| 357 | } |
| 358 | |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 359 | static unsigned getEncodedLinkage(const GlobalValue *GV) { |
| 360 | switch (GV->getLinkage()) { |
Bill Wendling | 55ae515 | 2010-08-20 22:05:50 +0000 | [diff] [blame] | 361 | case GlobalValue::ExternalLinkage: return 0; |
| 362 | case GlobalValue::WeakAnyLinkage: return 1; |
| 363 | case GlobalValue::AppendingLinkage: return 2; |
| 364 | case GlobalValue::InternalLinkage: return 3; |
| 365 | case GlobalValue::LinkOnceAnyLinkage: return 4; |
| 366 | case GlobalValue::DLLImportLinkage: return 5; |
| 367 | case GlobalValue::DLLExportLinkage: return 6; |
| 368 | case GlobalValue::ExternalWeakLinkage: return 7; |
| 369 | case GlobalValue::CommonLinkage: return 8; |
| 370 | case GlobalValue::PrivateLinkage: return 9; |
| 371 | case GlobalValue::WeakODRLinkage: return 10; |
| 372 | case GlobalValue::LinkOnceODRLinkage: return 11; |
| 373 | case GlobalValue::AvailableExternallyLinkage: return 12; |
| 374 | case GlobalValue::LinkerPrivateLinkage: return 13; |
| 375 | case GlobalValue::LinkerPrivateWeakLinkage: return 14; |
| 376 | case GlobalValue::LinkerPrivateWeakDefAutoLinkage: return 15; |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 377 | } |
Chandler Carruth | 732f05c | 2012-01-10 18:08:01 +0000 | [diff] [blame] | 378 | llvm_unreachable("Invalid linkage"); |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | static unsigned getEncodedVisibility(const GlobalValue *GV) { |
| 382 | switch (GV->getVisibility()) { |
Anton Korobeynikov | 9cd3ccf | 2007-04-29 20:56:48 +0000 | [diff] [blame] | 383 | case GlobalValue::DefaultVisibility: return 0; |
| 384 | case GlobalValue::HiddenVisibility: return 1; |
| 385 | case GlobalValue::ProtectedVisibility: return 2; |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 386 | } |
Chandler Carruth | 732f05c | 2012-01-10 18:08:01 +0000 | [diff] [blame] | 387 | llvm_unreachable("Invalid visibility"); |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | // Emit top-level description of module, including target triple, inline asm, |
| 391 | // descriptors for global variables, and function prototype info. |
| 392 | static void WriteModuleInfo(const Module *M, const ValueEnumerator &VE, |
| 393 | BitstreamWriter &Stream) { |
| 394 | // Emit the list of dependent libraries for the Module. |
| 395 | for (Module::lib_iterator I = M->lib_begin(), E = M->lib_end(); I != E; ++I) |
| 396 | WriteStringRecord(bitc::MODULE_CODE_DEPLIB, *I, 0/*TODO*/, Stream); |
| 397 | |
| 398 | // Emit various pieces of data attached to a module. |
| 399 | if (!M->getTargetTriple().empty()) |
| 400 | WriteStringRecord(bitc::MODULE_CODE_TRIPLE, M->getTargetTriple(), |
| 401 | 0/*TODO*/, Stream); |
| 402 | if (!M->getDataLayout().empty()) |
| 403 | WriteStringRecord(bitc::MODULE_CODE_DATALAYOUT, M->getDataLayout(), |
| 404 | 0/*TODO*/, Stream); |
| 405 | if (!M->getModuleInlineAsm().empty()) |
| 406 | WriteStringRecord(bitc::MODULE_CODE_ASM, M->getModuleInlineAsm(), |
| 407 | 0/*TODO*/, Stream); |
| 408 | |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 409 | // Emit information about sections and GC, computing how many there are. Also |
| 410 | // compute the maximum alignment value. |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 411 | std::map<std::string, unsigned> SectionMap; |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 412 | std::map<std::string, unsigned> GCMap; |
Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 413 | unsigned MaxAlignment = 0; |
Chris Lattner | d127c1b | 2007-04-23 18:58:34 +0000 | [diff] [blame] | 414 | unsigned MaxGlobalType = 0; |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 415 | for (Module::const_global_iterator GV = M->global_begin(),E = M->global_end(); |
| 416 | GV != E; ++GV) { |
Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 417 | MaxAlignment = std::max(MaxAlignment, GV->getAlignment()); |
Chris Lattner | d127c1b | 2007-04-23 18:58:34 +0000 | [diff] [blame] | 418 | MaxGlobalType = std::max(MaxGlobalType, VE.getTypeID(GV->getType())); |
Chad Rosier | 0871221 | 2011-08-12 16:45:18 +0000 | [diff] [blame] | 419 | if (GV->hasSection()) { |
| 420 | // Give section names unique ID's. |
| 421 | unsigned &Entry = SectionMap[GV->getSection()]; |
| 422 | if (!Entry) { |
| 423 | WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, GV->getSection(), |
| 424 | 0/*TODO*/, Stream); |
| 425 | Entry = SectionMap.size(); |
| 426 | } |
| 427 | } |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 428 | } |
| 429 | for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) { |
Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 430 | MaxAlignment = std::max(MaxAlignment, F->getAlignment()); |
Gordon Henriksen | 80a75bf | 2007-12-10 03:18:06 +0000 | [diff] [blame] | 431 | if (F->hasSection()) { |
| 432 | // Give section names unique ID's. |
| 433 | unsigned &Entry = SectionMap[F->getSection()]; |
| 434 | if (!Entry) { |
| 435 | WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, F->getSection(), |
| 436 | 0/*TODO*/, Stream); |
| 437 | Entry = SectionMap.size(); |
| 438 | } |
| 439 | } |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 440 | if (F->hasGC()) { |
| 441 | // Same for GC names. |
| 442 | unsigned &Entry = GCMap[F->getGC()]; |
Gordon Henriksen | 80a75bf | 2007-12-10 03:18:06 +0000 | [diff] [blame] | 443 | if (!Entry) { |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 444 | WriteStringRecord(bitc::MODULE_CODE_GCNAME, F->getGC(), |
Gordon Henriksen | 80a75bf | 2007-12-10 03:18:06 +0000 | [diff] [blame] | 445 | 0/*TODO*/, Stream); |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 446 | Entry = GCMap.size(); |
Gordon Henriksen | 80a75bf | 2007-12-10 03:18:06 +0000 | [diff] [blame] | 447 | } |
| 448 | } |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 449 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 450 | |
Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 451 | // Emit abbrev for globals, now that we know # sections and max alignment. |
| 452 | unsigned SimpleGVarAbbrev = 0; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 453 | if (!M->global_empty()) { |
Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 454 | // Add an abbrev for common globals with no visibility or thread localness. |
| 455 | BitCodeAbbrev *Abbv = new BitCodeAbbrev(); |
| 456 | Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_GLOBALVAR)); |
Chris Lattner | 2e7899d | 2007-05-04 20:34:50 +0000 | [diff] [blame] | 457 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, |
Chris Lattner | d127c1b | 2007-04-23 18:58:34 +0000 | [diff] [blame] | 458 | Log2_32_Ceil(MaxGlobalType+1))); |
Chris Lattner | 2e7899d | 2007-05-04 20:34:50 +0000 | [diff] [blame] | 459 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Constant. |
Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 460 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Initializer. |
Dale Johannesen | 6667646 | 2008-05-15 20:49:28 +0000 | [diff] [blame] | 461 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // Linkage. |
Chris Lattner | 2e7899d | 2007-05-04 20:34:50 +0000 | [diff] [blame] | 462 | if (MaxAlignment == 0) // Alignment. |
Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 463 | Abbv->Add(BitCodeAbbrevOp(0)); |
| 464 | else { |
| 465 | unsigned MaxEncAlignment = Log2_32(MaxAlignment)+1; |
Chris Lattner | 2e7899d | 2007-05-04 20:34:50 +0000 | [diff] [blame] | 466 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, |
Chris Lattner | d127c1b | 2007-04-23 18:58:34 +0000 | [diff] [blame] | 467 | Log2_32_Ceil(MaxEncAlignment+1))); |
Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 468 | } |
| 469 | if (SectionMap.empty()) // Section. |
| 470 | Abbv->Add(BitCodeAbbrevOp(0)); |
| 471 | else |
Chris Lattner | 2e7899d | 2007-05-04 20:34:50 +0000 | [diff] [blame] | 472 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, |
Chris Lattner | 631a8ed | 2007-04-24 03:29:47 +0000 | [diff] [blame] | 473 | Log2_32_Ceil(SectionMap.size()+1))); |
Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 474 | // Don't bother emitting vis + thread local. |
| 475 | SimpleGVarAbbrev = Stream.EmitAbbrev(Abbv); |
| 476 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 477 | |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 478 | // Emit the global variable information. |
| 479 | SmallVector<unsigned, 64> Vals; |
| 480 | for (Module::const_global_iterator GV = M->global_begin(),E = M->global_end(); |
| 481 | GV != E; ++GV) { |
Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 482 | unsigned AbbrevToUse = 0; |
| 483 | |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 484 | // GLOBALVAR: [type, isconst, initid, |
Rafael Espindola | bea4626 | 2011-01-08 16:42:36 +0000 | [diff] [blame] | 485 | // linkage, alignment, section, visibility, threadlocal, |
| 486 | // unnamed_addr] |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 487 | Vals.push_back(VE.getTypeID(GV->getType())); |
| 488 | Vals.push_back(GV->isConstant()); |
| 489 | Vals.push_back(GV->isDeclaration() ? 0 : |
| 490 | (VE.getValueID(GV->getInitializer()) + 1)); |
| 491 | Vals.push_back(getEncodedLinkage(GV)); |
| 492 | Vals.push_back(Log2_32(GV->getAlignment())+1); |
| 493 | Vals.push_back(GV->hasSection() ? SectionMap[GV->getSection()] : 0); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 494 | if (GV->isThreadLocal() || |
Rafael Espindola | bea4626 | 2011-01-08 16:42:36 +0000 | [diff] [blame] | 495 | GV->getVisibility() != GlobalValue::DefaultVisibility || |
| 496 | GV->hasUnnamedAddr()) { |
Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 497 | Vals.push_back(getEncodedVisibility(GV)); |
| 498 | Vals.push_back(GV->isThreadLocal()); |
Rafael Espindola | bea4626 | 2011-01-08 16:42:36 +0000 | [diff] [blame] | 499 | Vals.push_back(GV->hasUnnamedAddr()); |
Chris Lattner | 36d5e7d | 2007-04-23 16:04:05 +0000 | [diff] [blame] | 500 | } else { |
| 501 | AbbrevToUse = SimpleGVarAbbrev; |
| 502 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 503 | |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 504 | Stream.EmitRecord(bitc::MODULE_CODE_GLOBALVAR, Vals, AbbrevToUse); |
| 505 | Vals.clear(); |
| 506 | } |
| 507 | |
| 508 | // Emit the function proto information. |
| 509 | for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) { |
Chad Rosier | 0c89f7f | 2011-12-07 23:57:55 +0000 | [diff] [blame] | 510 | // FUNCTION: [type, callingconv, isproto, linkage, paramattrs, alignment, |
| 511 | // section, visibility, gc, unnamed_addr] |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 512 | Vals.push_back(VE.getTypeID(F->getType())); |
| 513 | Vals.push_back(F->getCallingConv()); |
| 514 | Vals.push_back(F->isDeclaration()); |
| 515 | Vals.push_back(getEncodedLinkage(F)); |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 516 | Vals.push_back(VE.getAttributeID(F->getAttributes())); |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 517 | Vals.push_back(Log2_32(F->getAlignment())+1); |
| 518 | Vals.push_back(F->hasSection() ? SectionMap[F->getSection()] : 0); |
| 519 | Vals.push_back(getEncodedVisibility(F)); |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 520 | Vals.push_back(F->hasGC() ? GCMap[F->getGC()] : 0); |
Rafael Espindola | bea4626 | 2011-01-08 16:42:36 +0000 | [diff] [blame] | 521 | Vals.push_back(F->hasUnnamedAddr()); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 522 | |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 523 | unsigned AbbrevToUse = 0; |
| 524 | Stream.EmitRecord(bitc::MODULE_CODE_FUNCTION, Vals, AbbrevToUse); |
| 525 | Vals.clear(); |
| 526 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 527 | |
Chris Lattner | 07d98b4 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 528 | // Emit the alias information. |
| 529 | for (Module::const_alias_iterator AI = M->alias_begin(), E = M->alias_end(); |
| 530 | AI != E; ++AI) { |
Chad Rosier | 14d622d | 2011-12-08 00:11:31 +0000 | [diff] [blame] | 531 | // ALIAS: [alias type, aliasee val#, linkage, visibility] |
Chris Lattner | 07d98b4 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 532 | Vals.push_back(VE.getTypeID(AI->getType())); |
| 533 | Vals.push_back(VE.getValueID(AI->getAliasee())); |
| 534 | Vals.push_back(getEncodedLinkage(AI)); |
Anton Korobeynikov | f8342b9 | 2008-03-11 21:40:17 +0000 | [diff] [blame] | 535 | Vals.push_back(getEncodedVisibility(AI)); |
Chris Lattner | 07d98b4 | 2007-04-26 02:46:40 +0000 | [diff] [blame] | 536 | unsigned AbbrevToUse = 0; |
| 537 | Stream.EmitRecord(bitc::MODULE_CODE_ALIAS, Vals, AbbrevToUse); |
| 538 | Vals.clear(); |
| 539 | } |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 540 | } |
| 541 | |
Dan Gohman | 1224c38 | 2009-07-20 21:19:07 +0000 | [diff] [blame] | 542 | static uint64_t GetOptimizationFlags(const Value *V) { |
| 543 | uint64_t Flags = 0; |
| 544 | |
| 545 | if (const OverflowingBinaryOperator *OBO = |
| 546 | dyn_cast<OverflowingBinaryOperator>(V)) { |
Dan Gohman | 5078f84 | 2009-08-20 17:11:38 +0000 | [diff] [blame] | 547 | if (OBO->hasNoSignedWrap()) |
| 548 | Flags |= 1 << bitc::OBO_NO_SIGNED_WRAP; |
| 549 | if (OBO->hasNoUnsignedWrap()) |
| 550 | Flags |= 1 << bitc::OBO_NO_UNSIGNED_WRAP; |
Chris Lattner | 35bda89 | 2011-02-06 21:44:57 +0000 | [diff] [blame] | 551 | } else if (const PossiblyExactOperator *PEO = |
| 552 | dyn_cast<PossiblyExactOperator>(V)) { |
| 553 | if (PEO->isExact()) |
| 554 | Flags |= 1 << bitc::PEO_EXACT; |
Dan Gohman | 1224c38 | 2009-07-20 21:19:07 +0000 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | return Flags; |
| 558 | } |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 559 | |
Devang Patel | 912538b | 2009-08-04 05:01:35 +0000 | [diff] [blame] | 560 | static void WriteMDNode(const MDNode *N, |
Devang Patel | 985fea2 | 2009-07-29 18:15:02 +0000 | [diff] [blame] | 561 | const ValueEnumerator &VE, |
Devang Patel | 912538b | 2009-08-04 05:01:35 +0000 | [diff] [blame] | 562 | BitstreamWriter &Stream, |
| 563 | SmallVector<uint64_t, 64> &Record) { |
Chris Lattner | 5d0cacd | 2009-12-31 01:22:29 +0000 | [diff] [blame] | 564 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { |
| 565 | if (N->getOperand(i)) { |
| 566 | Record.push_back(VE.getTypeID(N->getOperand(i)->getType())); |
| 567 | Record.push_back(VE.getValueID(N->getOperand(i))); |
Devang Patel | 912538b | 2009-08-04 05:01:35 +0000 | [diff] [blame] | 568 | } else { |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 569 | Record.push_back(VE.getTypeID(Type::getVoidTy(N->getContext()))); |
Devang Patel | 912538b | 2009-08-04 05:01:35 +0000 | [diff] [blame] | 570 | Record.push_back(0); |
| 571 | } |
| 572 | } |
Chris Lattner | 799a58a55 | 2011-06-17 17:56:00 +0000 | [diff] [blame] | 573 | unsigned MDCode = N->isFunctionLocal() ? bitc::METADATA_FN_NODE : |
| 574 | bitc::METADATA_NODE; |
Victor Hernandez | 24e64df | 2010-01-10 07:14:18 +0000 | [diff] [blame] | 575 | Stream.EmitRecord(MDCode, Record, 0); |
Devang Patel | 912538b | 2009-08-04 05:01:35 +0000 | [diff] [blame] | 576 | Record.clear(); |
| 577 | } |
Devang Patel | 104cf9e | 2009-07-23 01:07:34 +0000 | [diff] [blame] | 578 | |
Dan Gohman | 17aa92c | 2010-07-21 23:38:33 +0000 | [diff] [blame] | 579 | static void WriteModuleMetadata(const Module *M, |
| 580 | const ValueEnumerator &VE, |
Devang Patel | 912538b | 2009-08-04 05:01:35 +0000 | [diff] [blame] | 581 | BitstreamWriter &Stream) { |
Devang Patel | d5ac404 | 2009-08-04 06:00:18 +0000 | [diff] [blame] | 582 | const ValueEnumerator::ValueList &Vals = VE.getMDValues(); |
Devang Patel | 912538b | 2009-08-04 05:01:35 +0000 | [diff] [blame] | 583 | bool StartedMetadataBlock = false; |
| 584 | unsigned MDSAbbrev = 0; |
| 585 | SmallVector<uint64_t, 64> Record; |
| 586 | for (unsigned i = 0, e = Vals.size(); i != e; ++i) { |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 587 | |
Devang Patel | 912538b | 2009-08-04 05:01:35 +0000 | [diff] [blame] | 588 | if (const MDNode *N = dyn_cast<MDNode>(Vals[i].first)) { |
Victor Hernandez | 2c482f4 | 2010-01-29 21:19:19 +0000 | [diff] [blame] | 589 | if (!N->isFunctionLocal() || !N->getFunction()) { |
Victor Hernandez | 0c31641 | 2010-01-13 19:37:33 +0000 | [diff] [blame] | 590 | if (!StartedMetadataBlock) { |
| 591 | Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3); |
| 592 | StartedMetadataBlock = true; |
| 593 | } |
| 594 | WriteMDNode(N, VE, Stream, Record); |
Devang Patel | 912538b | 2009-08-04 05:01:35 +0000 | [diff] [blame] | 595 | } |
Devang Patel | 912538b | 2009-08-04 05:01:35 +0000 | [diff] [blame] | 596 | } else if (const MDString *MDS = dyn_cast<MDString>(Vals[i].first)) { |
| 597 | if (!StartedMetadataBlock) { |
| 598 | Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 599 | |
Devang Patel | 70c9d17 | 2009-07-22 21:10:50 +0000 | [diff] [blame] | 600 | // Abbrev for METADATA_STRING. |
| 601 | BitCodeAbbrev *Abbv = new BitCodeAbbrev(); |
| 602 | Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_STRING)); |
Devang Patel | e54abc9 | 2009-07-22 17:43:22 +0000 | [diff] [blame] | 603 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); |
Devang Patel | 70c9d17 | 2009-07-22 21:10:50 +0000 | [diff] [blame] | 604 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); |
| 605 | MDSAbbrev = Stream.EmitAbbrev(Abbv); |
Devang Patel | 912538b | 2009-08-04 05:01:35 +0000 | [diff] [blame] | 606 | StartedMetadataBlock = true; |
Devang Patel | 985fea2 | 2009-07-29 18:15:02 +0000 | [diff] [blame] | 607 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 608 | |
Devang Patel | 985fea2 | 2009-07-29 18:15:02 +0000 | [diff] [blame] | 609 | // Code: [strchar x N] |
Chris Lattner | 12f031d | 2009-10-19 05:51:03 +0000 | [diff] [blame] | 610 | Record.append(MDS->begin(), MDS->end()); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 611 | |
Devang Patel | 985fea2 | 2009-07-29 18:15:02 +0000 | [diff] [blame] | 612 | // Emit the finished record. |
Devang Patel | 104cf9e | 2009-07-23 01:07:34 +0000 | [diff] [blame] | 613 | Stream.EmitRecord(bitc::METADATA_STRING, Record, MDSAbbrev); |
| 614 | Record.clear(); |
Devang Patel | 912538b | 2009-08-04 05:01:35 +0000 | [diff] [blame] | 615 | } |
| 616 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 617 | |
Dan Gohman | 17aa92c | 2010-07-21 23:38:33 +0000 | [diff] [blame] | 618 | // Write named metadata. |
| 619 | for (Module::const_named_metadata_iterator I = M->named_metadata_begin(), |
| 620 | E = M->named_metadata_end(); I != E; ++I) { |
| 621 | const NamedMDNode *NMD = I; |
| 622 | if (!StartedMetadataBlock) { |
| 623 | Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3); |
| 624 | StartedMetadataBlock = true; |
| 625 | } |
| 626 | |
| 627 | // Write name. |
| 628 | StringRef Str = NMD->getName(); |
| 629 | for (unsigned i = 0, e = Str.size(); i != e; ++i) |
| 630 | Record.push_back(Str[i]); |
| 631 | Stream.EmitRecord(bitc::METADATA_NAME, Record, 0/*TODO*/); |
| 632 | Record.clear(); |
| 633 | |
| 634 | // Write named metadata operands. |
| 635 | for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) |
| 636 | Record.push_back(VE.getValueID(NMD->getOperand(i))); |
Chris Lattner | 799a58a55 | 2011-06-17 17:56:00 +0000 | [diff] [blame] | 637 | Stream.EmitRecord(bitc::METADATA_NAMED_NODE, Record, 0); |
Dan Gohman | 17aa92c | 2010-07-21 23:38:33 +0000 | [diff] [blame] | 638 | Record.clear(); |
| 639 | } |
| 640 | |
Devang Patel | 912538b | 2009-08-04 05:01:35 +0000 | [diff] [blame] | 641 | if (StartedMetadataBlock) |
Devang Patel | e8e0213 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 642 | Stream.ExitBlock(); |
| 643 | } |
| 644 | |
Victor Hernandez | 3cd7c3d | 2010-01-14 19:38:44 +0000 | [diff] [blame] | 645 | static void WriteFunctionLocalMetadata(const Function &F, |
| 646 | const ValueEnumerator &VE, |
Victor Hernandez | 0c31641 | 2010-01-13 19:37:33 +0000 | [diff] [blame] | 647 | BitstreamWriter &Stream) { |
| 648 | bool StartedMetadataBlock = false; |
| 649 | SmallVector<uint64_t, 64> Record; |
Devang Patel | 6209869 | 2010-06-02 23:05:04 +0000 | [diff] [blame] | 650 | const SmallVector<const MDNode *, 8> &Vals = VE.getFunctionLocalMDValues(); |
Victor Hernandez | 3cd7c3d | 2010-01-14 19:38:44 +0000 | [diff] [blame] | 651 | for (unsigned i = 0, e = Vals.size(); i != e; ++i) |
Devang Patel | 6209869 | 2010-06-02 23:05:04 +0000 | [diff] [blame] | 652 | if (const MDNode *N = Vals[i]) |
Victor Hernandez | 2c482f4 | 2010-01-29 21:19:19 +0000 | [diff] [blame] | 653 | if (N->isFunctionLocal() && N->getFunction() == &F) { |
Victor Hernandez | 0c31641 | 2010-01-13 19:37:33 +0000 | [diff] [blame] | 654 | if (!StartedMetadataBlock) { |
| 655 | Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3); |
| 656 | StartedMetadataBlock = true; |
| 657 | } |
| 658 | WriteMDNode(N, VE, Stream, Record); |
Victor Hernandez | 0c31641 | 2010-01-13 19:37:33 +0000 | [diff] [blame] | 659 | } |
Devang Patel | 6209869 | 2010-06-02 23:05:04 +0000 | [diff] [blame] | 660 | |
Victor Hernandez | 0c31641 | 2010-01-13 19:37:33 +0000 | [diff] [blame] | 661 | if (StartedMetadataBlock) |
| 662 | Stream.ExitBlock(); |
| 663 | } |
| 664 | |
Devang Patel | e8e0213 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 665 | static void WriteMetadataAttachment(const Function &F, |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 666 | const ValueEnumerator &VE, |
| 667 | BitstreamWriter &Stream) { |
Chris Lattner | a624524 | 2010-04-03 02:17:50 +0000 | [diff] [blame] | 668 | Stream.EnterSubblock(bitc::METADATA_ATTACHMENT_ID, 3); |
| 669 | |
Devang Patel | e8e0213 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 670 | SmallVector<uint64_t, 64> Record; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 671 | |
Devang Patel | e8e0213 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 672 | // Write metadata attachments |
Chris Lattner | 799a58a55 | 2011-06-17 17:56:00 +0000 | [diff] [blame] | 673 | // METADATA_ATTACHMENT - [m x [value, [n x [id, mdnode]]] |
Chris Lattner | 3990b12 | 2009-12-28 23:41:32 +0000 | [diff] [blame] | 674 | SmallVector<std::pair<unsigned, MDNode*>, 4> MDs; |
| 675 | |
Devang Patel | e8e0213 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 676 | for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) |
| 677 | for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); |
| 678 | I != E; ++I) { |
Devang Patel | f61b237 | 2009-10-22 18:55:16 +0000 | [diff] [blame] | 679 | MDs.clear(); |
Chris Lattner | a624524 | 2010-04-03 02:17:50 +0000 | [diff] [blame] | 680 | I->getAllMetadataOtherThanDebugLoc(MDs); |
Chris Lattner | 3990b12 | 2009-12-28 23:41:32 +0000 | [diff] [blame] | 681 | |
| 682 | // If no metadata, ignore instruction. |
| 683 | if (MDs.empty()) continue; |
| 684 | |
| 685 | Record.push_back(VE.getInstructionID(I)); |
| 686 | |
| 687 | for (unsigned i = 0, e = MDs.size(); i != e; ++i) { |
| 688 | Record.push_back(MDs[i].first); |
| 689 | Record.push_back(VE.getValueID(MDs[i].second)); |
Devang Patel | e8e0213 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 690 | } |
Chris Lattner | 799a58a55 | 2011-06-17 17:56:00 +0000 | [diff] [blame] | 691 | Stream.EmitRecord(bitc::METADATA_ATTACHMENT, Record, 0); |
Chris Lattner | 3990b12 | 2009-12-28 23:41:32 +0000 | [diff] [blame] | 692 | Record.clear(); |
Devang Patel | e8e0213 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 693 | } |
| 694 | |
Chris Lattner | a624524 | 2010-04-03 02:17:50 +0000 | [diff] [blame] | 695 | Stream.ExitBlock(); |
Devang Patel | e8e0213 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 696 | } |
| 697 | |
Chris Lattner | 7d05c46 | 2009-12-28 20:10:43 +0000 | [diff] [blame] | 698 | static void WriteModuleMetadataStore(const Module *M, BitstreamWriter &Stream) { |
Devang Patel | e8e0213 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 699 | SmallVector<uint64_t, 64> Record; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 700 | |
Devang Patel | e8e0213 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 701 | // Write metadata kinds |
| 702 | // METADATA_KIND - [n x [id, name]] |
Chris Lattner | 7d05c46 | 2009-12-28 20:10:43 +0000 | [diff] [blame] | 703 | SmallVector<StringRef, 4> Names; |
Chris Lattner | 0811347 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 704 | M->getMDKindNames(Names); |
Chris Lattner | 7d05c46 | 2009-12-28 20:10:43 +0000 | [diff] [blame] | 705 | |
Dan Gohman | 19538d1 | 2010-07-20 21:42:28 +0000 | [diff] [blame] | 706 | if (Names.empty()) return; |
Chris Lattner | 7d05c46 | 2009-12-28 20:10:43 +0000 | [diff] [blame] | 707 | |
| 708 | Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3); |
| 709 | |
Dan Gohman | 19538d1 | 2010-07-20 21:42:28 +0000 | [diff] [blame] | 710 | for (unsigned MDKindID = 0, e = Names.size(); MDKindID != e; ++MDKindID) { |
Chris Lattner | 7d05c46 | 2009-12-28 20:10:43 +0000 | [diff] [blame] | 711 | Record.push_back(MDKindID); |
| 712 | StringRef KName = Names[MDKindID]; |
| 713 | Record.append(KName.begin(), KName.end()); |
| 714 | |
Devang Patel | e8e0213 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 715 | Stream.EmitRecord(bitc::METADATA_KIND, Record, 0); |
| 716 | Record.clear(); |
| 717 | } |
| 718 | |
Chris Lattner | 7d05c46 | 2009-12-28 20:10:43 +0000 | [diff] [blame] | 719 | Stream.ExitBlock(); |
Devang Patel | 912538b | 2009-08-04 05:01:35 +0000 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | static void WriteConstants(unsigned FirstVal, unsigned LastVal, |
| 723 | const ValueEnumerator &VE, |
| 724 | BitstreamWriter &Stream, bool isGlobal) { |
| 725 | if (FirstVal == LastVal) return; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 726 | |
Devang Patel | 912538b | 2009-08-04 05:01:35 +0000 | [diff] [blame] | 727 | Stream.EnterSubblock(bitc::CONSTANTS_BLOCK_ID, 4); |
| 728 | |
| 729 | unsigned AggregateAbbrev = 0; |
| 730 | unsigned String8Abbrev = 0; |
| 731 | unsigned CString7Abbrev = 0; |
| 732 | unsigned CString6Abbrev = 0; |
| 733 | // If this is a constant pool for the module, emit module-specific abbrevs. |
| 734 | if (isGlobal) { |
| 735 | // Abbrev for CST_CODE_AGGREGATE. |
| 736 | BitCodeAbbrev *Abbv = new BitCodeAbbrev(); |
| 737 | Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_AGGREGATE)); |
| 738 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); |
| 739 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, Log2_32_Ceil(LastVal+1))); |
| 740 | AggregateAbbrev = Stream.EmitAbbrev(Abbv); |
| 741 | |
| 742 | // Abbrev for CST_CODE_STRING. |
| 743 | Abbv = new BitCodeAbbrev(); |
| 744 | Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_STRING)); |
| 745 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); |
| 746 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); |
| 747 | String8Abbrev = Stream.EmitAbbrev(Abbv); |
| 748 | // Abbrev for CST_CODE_CSTRING. |
| 749 | Abbv = new BitCodeAbbrev(); |
| 750 | Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING)); |
| 751 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); |
| 752 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7)); |
| 753 | CString7Abbrev = Stream.EmitAbbrev(Abbv); |
| 754 | // Abbrev for CST_CODE_CSTRING. |
| 755 | Abbv = new BitCodeAbbrev(); |
| 756 | Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING)); |
| 757 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); |
| 758 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6)); |
| 759 | CString6Abbrev = Stream.EmitAbbrev(Abbv); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 760 | } |
| 761 | |
Devang Patel | 912538b | 2009-08-04 05:01:35 +0000 | [diff] [blame] | 762 | SmallVector<uint64_t, 64> Record; |
| 763 | |
| 764 | const ValueEnumerator::ValueList &Vals = VE.getValues(); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 765 | Type *LastTy = 0; |
Devang Patel | 912538b | 2009-08-04 05:01:35 +0000 | [diff] [blame] | 766 | for (unsigned i = FirstVal; i != LastVal; ++i) { |
| 767 | const Value *V = Vals[i].first; |
Chris Lattner | 2edd22b | 2007-04-24 00:16:04 +0000 | [diff] [blame] | 768 | // If we need to switch types, do so now. |
| 769 | if (V->getType() != LastTy) { |
| 770 | LastTy = V->getType(); |
| 771 | Record.push_back(VE.getTypeID(LastTy)); |
Chris Lattner | a0f1ecc | 2007-05-05 07:36:14 +0000 | [diff] [blame] | 772 | Stream.EmitRecord(bitc::CST_CODE_SETTYPE, Record, |
| 773 | CONSTANTS_SETTYPE_ABBREV); |
Chris Lattner | 2edd22b | 2007-04-24 00:16:04 +0000 | [diff] [blame] | 774 | Record.clear(); |
| 775 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 776 | |
Chris Lattner | 2edd22b | 2007-04-24 00:16:04 +0000 | [diff] [blame] | 777 | if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) { |
Dale Johannesen | 4360298 | 2009-10-13 20:46:56 +0000 | [diff] [blame] | 778 | Record.push_back(unsigned(IA->hasSideEffects()) | |
Dale Johannesen | 8ba2d5b | 2009-10-21 23:28:00 +0000 | [diff] [blame] | 779 | unsigned(IA->isAlignStack()) << 1); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 780 | |
Chris Lattner | 2bce93a | 2007-05-06 01:58:20 +0000 | [diff] [blame] | 781 | // Add the asm string. |
| 782 | const std::string &AsmStr = IA->getAsmString(); |
| 783 | Record.push_back(AsmStr.size()); |
| 784 | for (unsigned i = 0, e = AsmStr.size(); i != e; ++i) |
| 785 | Record.push_back(AsmStr[i]); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 786 | |
Chris Lattner | 2bce93a | 2007-05-06 01:58:20 +0000 | [diff] [blame] | 787 | // Add the constraint string. |
| 788 | const std::string &ConstraintStr = IA->getConstraintString(); |
| 789 | Record.push_back(ConstraintStr.size()); |
| 790 | for (unsigned i = 0, e = ConstraintStr.size(); i != e; ++i) |
| 791 | Record.push_back(ConstraintStr[i]); |
| 792 | Stream.EmitRecord(bitc::CST_CODE_INLINEASM, Record); |
| 793 | Record.clear(); |
Chris Lattner | 2edd22b | 2007-04-24 00:16:04 +0000 | [diff] [blame] | 794 | continue; |
| 795 | } |
| 796 | const Constant *C = cast<Constant>(V); |
| 797 | unsigned Code = -1U; |
| 798 | unsigned AbbrevToUse = 0; |
| 799 | if (C->isNullValue()) { |
| 800 | Code = bitc::CST_CODE_NULL; |
| 801 | } else if (isa<UndefValue>(C)) { |
| 802 | Code = bitc::CST_CODE_UNDEF; |
| 803 | } else if (const ConstantInt *IV = dyn_cast<ConstantInt>(C)) { |
Stepan Dyatkovskiy | 1f98383 | 2012-05-08 08:33:21 +0000 | [diff] [blame] | 804 | if (IV->getBitWidth() <= 64) { |
| 805 | uint64_t V = IV->getSExtValue(); |
| 806 | if ((int64_t)V >= 0) |
| 807 | Record.push_back(V << 1); |
| 808 | else |
| 809 | Record.push_back((-V << 1) | 1); |
| 810 | Code = bitc::CST_CODE_INTEGER; |
| 811 | AbbrevToUse = CONSTANTS_INTEGER_ABBREV; |
| 812 | } else { // Wide integers, > 64 bits in size. |
| 813 | // We have an arbitrary precision integer value to write whose |
| 814 | // bit width is > 64. However, in canonical unsigned integer |
| 815 | // format it is likely that the high bits are going to be zero. |
| 816 | // So, we only write the number of active words. |
| 817 | unsigned NWords = IV->getValue().getActiveWords(); |
| 818 | const uint64_t *RawWords = IV->getValue().getRawData(); |
| 819 | for (unsigned i = 0; i != NWords; ++i) { |
| 820 | int64_t V = RawWords[i]; |
| 821 | if (V >= 0) |
| 822 | Record.push_back(V << 1); |
| 823 | else |
| 824 | Record.push_back((-V << 1) | 1); |
| 825 | } |
| 826 | Code = bitc::CST_CODE_WIDE_INTEGER; |
| 827 | } |
Chris Lattner | 2edd22b | 2007-04-24 00:16:04 +0000 | [diff] [blame] | 828 | } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) { |
| 829 | Code = bitc::CST_CODE_FLOAT; |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 830 | Type *Ty = CFP->getType(); |
Dan Gohman | ce16339 | 2011-12-17 00:04:22 +0000 | [diff] [blame] | 831 | if (Ty->isHalfTy() || Ty->isFloatTy() || Ty->isDoubleTy()) { |
Dale Johannesen | 7111b02 | 2008-10-09 18:53:47 +0000 | [diff] [blame] | 832 | Record.push_back(CFP->getValueAPF().bitcastToAPInt().getZExtValue()); |
Chris Lattner | cf0fe8d | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 833 | } else if (Ty->isX86_FP80Ty()) { |
Dale Johannesen | 693717f | 2007-09-26 23:20:33 +0000 | [diff] [blame] | 834 | // api needed to prevent premature destruction |
Dale Johannesen | 1b25cb2 | 2009-03-23 21:16:53 +0000 | [diff] [blame] | 835 | // bits are not in the same order as a normal i80 APInt, compensate. |
Dale Johannesen | 7111b02 | 2008-10-09 18:53:47 +0000 | [diff] [blame] | 836 | APInt api = CFP->getValueAPF().bitcastToAPInt(); |
Dale Johannesen | 693717f | 2007-09-26 23:20:33 +0000 | [diff] [blame] | 837 | const uint64_t *p = api.getRawData(); |
Dale Johannesen | 1b25cb2 | 2009-03-23 21:16:53 +0000 | [diff] [blame] | 838 | Record.push_back((p[1] << 48) | (p[0] >> 16)); |
| 839 | Record.push_back(p[0] & 0xffffLL); |
Chris Lattner | cf0fe8d | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 840 | } else if (Ty->isFP128Ty() || Ty->isPPC_FP128Ty()) { |
Dale Johannesen | 7111b02 | 2008-10-09 18:53:47 +0000 | [diff] [blame] | 841 | APInt api = CFP->getValueAPF().bitcastToAPInt(); |
Dale Johannesen | 693717f | 2007-09-26 23:20:33 +0000 | [diff] [blame] | 842 | const uint64_t *p = api.getRawData(); |
Dale Johannesen | 3f6eb74 | 2007-09-11 18:32:33 +0000 | [diff] [blame] | 843 | Record.push_back(p[0]); |
| 844 | Record.push_back(p[1]); |
Dale Johannesen | ebbc95d | 2007-08-09 22:51:36 +0000 | [diff] [blame] | 845 | } else { |
| 846 | assert (0 && "Unknown FP type!"); |
Chris Lattner | 2edd22b | 2007-04-24 00:16:04 +0000 | [diff] [blame] | 847 | } |
Chris Lattner | d408f06 | 2012-01-30 00:51:16 +0000 | [diff] [blame] | 848 | } else if (isa<ConstantDataSequential>(C) && |
| 849 | cast<ConstantDataSequential>(C)->isString()) { |
| 850 | const ConstantDataSequential *Str = cast<ConstantDataSequential>(C); |
| 851 | // Emit constant strings specially. |
| 852 | unsigned NumElts = Str->getNumElements(); |
| 853 | // If this is a null-terminated string, use the denser CSTRING encoding. |
| 854 | if (Str->isCString()) { |
| 855 | Code = bitc::CST_CODE_CSTRING; |
| 856 | --NumElts; // Don't encode the null, which isn't allowed by char6. |
| 857 | } else { |
| 858 | Code = bitc::CST_CODE_STRING; |
| 859 | AbbrevToUse = String8Abbrev; |
| 860 | } |
| 861 | bool isCStr7 = Code == bitc::CST_CODE_CSTRING; |
| 862 | bool isCStrChar6 = Code == bitc::CST_CODE_CSTRING; |
| 863 | for (unsigned i = 0; i != NumElts; ++i) { |
| 864 | unsigned char V = Str->getElementAsInteger(i); |
| 865 | Record.push_back(V); |
| 866 | isCStr7 &= (V & 128) == 0; |
| 867 | if (isCStrChar6) |
| 868 | isCStrChar6 = BitCodeAbbrevOp::isChar6(V); |
| 869 | } |
| 870 | |
| 871 | if (isCStrChar6) |
| 872 | AbbrevToUse = CString6Abbrev; |
| 873 | else if (isCStr7) |
| 874 | AbbrevToUse = CString7Abbrev; |
| 875 | } else if (const ConstantDataSequential *CDS = |
| 876 | dyn_cast<ConstantDataSequential>(C)) { |
Chris Lattner | 41b9920 | 2012-01-30 07:36:01 +0000 | [diff] [blame] | 877 | Code = bitc::CST_CODE_DATA; |
Chris Lattner | d408f06 | 2012-01-30 00:51:16 +0000 | [diff] [blame] | 878 | Type *EltTy = CDS->getType()->getElementType(); |
| 879 | if (isa<IntegerType>(EltTy)) { |
| 880 | for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) |
| 881 | Record.push_back(CDS->getElementAsInteger(i)); |
| 882 | } else if (EltTy->isFloatTy()) { |
| 883 | for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) { |
| 884 | union { float F; uint32_t I; }; |
| 885 | F = CDS->getElementAsFloat(i); |
| 886 | Record.push_back(I); |
| 887 | } |
| 888 | } else { |
| 889 | assert(EltTy->isDoubleTy() && "Unknown ConstantData element type"); |
| 890 | for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) { |
| 891 | union { double F; uint64_t I; }; |
| 892 | F = CDS->getElementAsDouble(i); |
| 893 | Record.push_back(I); |
| 894 | } |
| 895 | } |
| 896 | } else if (isa<ConstantArray>(C) || isa<ConstantStruct>(C) || |
| 897 | isa<ConstantVector>(C)) { |
Chris Lattner | 2edd22b | 2007-04-24 00:16:04 +0000 | [diff] [blame] | 898 | Code = bitc::CST_CODE_AGGREGATE; |
Chris Lattner | 2edd22b | 2007-04-24 00:16:04 +0000 | [diff] [blame] | 899 | for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) |
| 900 | Record.push_back(VE.getValueID(C->getOperand(i))); |
Chris Lattner | a0f1ecc | 2007-05-05 07:36:14 +0000 | [diff] [blame] | 901 | AbbrevToUse = AggregateAbbrev; |
Chris Lattner | 2edd22b | 2007-04-24 00:16:04 +0000 | [diff] [blame] | 902 | } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { |
Chris Lattner | f581c3b | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 903 | switch (CE->getOpcode()) { |
| 904 | default: |
| 905 | if (Instruction::isCast(CE->getOpcode())) { |
| 906 | Code = bitc::CST_CODE_CE_CAST; |
| 907 | Record.push_back(GetEncodedCastOpcode(CE->getOpcode())); |
| 908 | Record.push_back(VE.getTypeID(C->getOperand(0)->getType())); |
| 909 | Record.push_back(VE.getValueID(C->getOperand(0))); |
Chris Lattner | a0f1ecc | 2007-05-05 07:36:14 +0000 | [diff] [blame] | 910 | AbbrevToUse = CONSTANTS_CE_CAST_Abbrev; |
Chris Lattner | f581c3b | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 911 | } else { |
| 912 | assert(CE->getNumOperands() == 2 && "Unknown constant expr!"); |
| 913 | Code = bitc::CST_CODE_CE_BINOP; |
| 914 | Record.push_back(GetEncodedBinaryOpcode(CE->getOpcode())); |
| 915 | Record.push_back(VE.getValueID(C->getOperand(0))); |
| 916 | Record.push_back(VE.getValueID(C->getOperand(1))); |
Dan Gohman | 1224c38 | 2009-07-20 21:19:07 +0000 | [diff] [blame] | 917 | uint64_t Flags = GetOptimizationFlags(CE); |
| 918 | if (Flags != 0) |
| 919 | Record.push_back(Flags); |
Chris Lattner | f581c3b | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 920 | } |
| 921 | break; |
| 922 | case Instruction::GetElementPtr: |
| 923 | Code = bitc::CST_CODE_CE_GEP; |
Dan Gohman | dd8004d | 2009-07-27 21:53:46 +0000 | [diff] [blame] | 924 | if (cast<GEPOperator>(C)->isInBounds()) |
| 925 | Code = bitc::CST_CODE_CE_INBOUNDS_GEP; |
Chris Lattner | f581c3b | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 926 | for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) { |
| 927 | Record.push_back(VE.getTypeID(C->getOperand(i)->getType())); |
| 928 | Record.push_back(VE.getValueID(C->getOperand(i))); |
| 929 | } |
| 930 | break; |
| 931 | case Instruction::Select: |
| 932 | Code = bitc::CST_CODE_CE_SELECT; |
| 933 | Record.push_back(VE.getValueID(C->getOperand(0))); |
| 934 | Record.push_back(VE.getValueID(C->getOperand(1))); |
| 935 | Record.push_back(VE.getValueID(C->getOperand(2))); |
| 936 | break; |
| 937 | case Instruction::ExtractElement: |
| 938 | Code = bitc::CST_CODE_CE_EXTRACTELT; |
| 939 | Record.push_back(VE.getTypeID(C->getOperand(0)->getType())); |
| 940 | Record.push_back(VE.getValueID(C->getOperand(0))); |
| 941 | Record.push_back(VE.getValueID(C->getOperand(1))); |
| 942 | break; |
| 943 | case Instruction::InsertElement: |
| 944 | Code = bitc::CST_CODE_CE_INSERTELT; |
| 945 | Record.push_back(VE.getValueID(C->getOperand(0))); |
| 946 | Record.push_back(VE.getValueID(C->getOperand(1))); |
| 947 | Record.push_back(VE.getValueID(C->getOperand(2))); |
| 948 | break; |
| 949 | case Instruction::ShuffleVector: |
Nate Begeman | 0f123cf | 2009-02-12 21:28:33 +0000 | [diff] [blame] | 950 | // If the return type and argument types are the same, this is a |
| 951 | // standard shufflevector instruction. If the types are different, |
| 952 | // then the shuffle is widening or truncating the input vectors, and |
| 953 | // the argument type must also be encoded. |
| 954 | if (C->getType() == C->getOperand(0)->getType()) { |
| 955 | Code = bitc::CST_CODE_CE_SHUFFLEVEC; |
| 956 | } else { |
| 957 | Code = bitc::CST_CODE_CE_SHUFVEC_EX; |
| 958 | Record.push_back(VE.getTypeID(C->getOperand(0)->getType())); |
| 959 | } |
Chris Lattner | f581c3b | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 960 | Record.push_back(VE.getValueID(C->getOperand(0))); |
| 961 | Record.push_back(VE.getValueID(C->getOperand(1))); |
| 962 | Record.push_back(VE.getValueID(C->getOperand(2))); |
| 963 | break; |
| 964 | case Instruction::ICmp: |
| 965 | case Instruction::FCmp: |
Nick Lewycky | 7f6aa2b | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 966 | Code = bitc::CST_CODE_CE_CMP; |
Chris Lattner | f581c3b | 2007-04-24 07:07:11 +0000 | [diff] [blame] | 967 | Record.push_back(VE.getTypeID(C->getOperand(0)->getType())); |
| 968 | Record.push_back(VE.getValueID(C->getOperand(0))); |
| 969 | Record.push_back(VE.getValueID(C->getOperand(1))); |
| 970 | Record.push_back(CE->getPredicate()); |
| 971 | break; |
| 972 | } |
Chris Lattner | 837e04a | 2009-10-28 05:24:40 +0000 | [diff] [blame] | 973 | } else if (const BlockAddress *BA = dyn_cast<BlockAddress>(C)) { |
Chris Lattner | 837e04a | 2009-10-28 05:24:40 +0000 | [diff] [blame] | 974 | Code = bitc::CST_CODE_BLOCKADDRESS; |
| 975 | Record.push_back(VE.getTypeID(BA->getFunction()->getType())); |
| 976 | Record.push_back(VE.getValueID(BA->getFunction())); |
| 977 | Record.push_back(VE.getGlobalBasicBlockID(BA->getBasicBlock())); |
Chris Lattner | 2edd22b | 2007-04-24 00:16:04 +0000 | [diff] [blame] | 978 | } else { |
Dan Gohman | 29d2716 | 2010-07-21 21:18:37 +0000 | [diff] [blame] | 979 | #ifndef NDEBUG |
| 980 | C->dump(); |
| 981 | #endif |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 982 | llvm_unreachable("Unknown constant!"); |
Chris Lattner | 2edd22b | 2007-04-24 00:16:04 +0000 | [diff] [blame] | 983 | } |
| 984 | Stream.EmitRecord(Code, Record, AbbrevToUse); |
| 985 | Record.clear(); |
| 986 | } |
| 987 | |
| 988 | Stream.ExitBlock(); |
| 989 | } |
| 990 | |
| 991 | static void WriteModuleConstants(const ValueEnumerator &VE, |
| 992 | BitstreamWriter &Stream) { |
| 993 | const ValueEnumerator::ValueList &Vals = VE.getValues(); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 994 | |
Chris Lattner | 2edd22b | 2007-04-24 00:16:04 +0000 | [diff] [blame] | 995 | // Find the first constant to emit, which is the first non-globalvalue value. |
| 996 | // We know globalvalues have been emitted by WriteModuleInfo. |
| 997 | for (unsigned i = 0, e = Vals.size(); i != e; ++i) { |
| 998 | if (!isa<GlobalValue>(Vals[i].first)) { |
Devang Patel | 912538b | 2009-08-04 05:01:35 +0000 | [diff] [blame] | 999 | WriteConstants(i, Vals.size(), VE, Stream, true); |
Chris Lattner | 2edd22b | 2007-04-24 00:16:04 +0000 | [diff] [blame] | 1000 | return; |
| 1001 | } |
| 1002 | } |
| 1003 | } |
Chris Lattner | b992be1 | 2007-04-23 20:35:01 +0000 | [diff] [blame] | 1004 | |
Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1005 | /// PushValueAndType - The file has to encode both the value and type id for |
| 1006 | /// many values, because we need to know what type to create for forward |
| 1007 | /// references. However, most operands are not forward references, so this type |
| 1008 | /// field is not needed. |
| 1009 | /// |
| 1010 | /// This function adds V's value ID to Vals. If the value ID is higher than the |
| 1011 | /// instruction ID, then it is a forward reference, and it also includes the |
| 1012 | /// type ID. |
Gabor Greif | 75a46eb | 2009-01-16 18:40:27 +0000 | [diff] [blame] | 1013 | static bool PushValueAndType(const Value *V, unsigned InstID, |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1014 | SmallVector<unsigned, 64> &Vals, |
Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1015 | ValueEnumerator &VE) { |
| 1016 | unsigned ValID = VE.getValueID(V); |
| 1017 | Vals.push_back(ValID); |
| 1018 | if (ValID >= InstID) { |
| 1019 | Vals.push_back(VE.getTypeID(V->getType())); |
| 1020 | return true; |
| 1021 | } |
| 1022 | return false; |
| 1023 | } |
| 1024 | |
Chris Lattner | b9d0c2a | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 1025 | /// WriteInstruction - Emit an instruction to the specified stream. |
Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1026 | static void WriteInstruction(const Instruction &I, unsigned InstID, |
| 1027 | ValueEnumerator &VE, BitstreamWriter &Stream, |
Chris Lattner | b9d0c2a | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 1028 | SmallVector<unsigned, 64> &Vals) { |
Chris Lattner | b9d0c2a | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 1029 | unsigned Code = 0; |
| 1030 | unsigned AbbrevToUse = 0; |
Devang Patel | e8e0213 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 1031 | VE.setInstructionID(&I); |
Chris Lattner | b9d0c2a | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 1032 | switch (I.getOpcode()) { |
| 1033 | default: |
| 1034 | if (Instruction::isCast(I.getOpcode())) { |
Chris Lattner | d309c75 | 2007-05-01 02:13:26 +0000 | [diff] [blame] | 1035 | Code = bitc::FUNC_CODE_INST_CAST; |
Chris Lattner | f9f2e83 | 2007-05-06 02:38:57 +0000 | [diff] [blame] | 1036 | if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE)) |
| 1037 | AbbrevToUse = FUNCTION_INST_CAST_ABBREV; |
Chris Lattner | b9d0c2a | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 1038 | Vals.push_back(VE.getTypeID(I.getType())); |
Chris Lattner | abfbf85 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 1039 | Vals.push_back(GetEncodedCastOpcode(I.getOpcode())); |
Chris Lattner | b9d0c2a | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 1040 | } else { |
| 1041 | assert(isa<BinaryOperator>(I) && "Unknown instruction!"); |
Chris Lattner | f639875 | 2007-05-02 04:26:36 +0000 | [diff] [blame] | 1042 | Code = bitc::FUNC_CODE_INST_BINOP; |
Chris Lattner | f9f2e83 | 2007-05-06 02:38:57 +0000 | [diff] [blame] | 1043 | if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE)) |
| 1044 | AbbrevToUse = FUNCTION_INST_BINOP_ABBREV; |
Chris Lattner | b9d0c2a | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 1045 | Vals.push_back(VE.getValueID(I.getOperand(1))); |
Chris Lattner | abfbf85 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 1046 | Vals.push_back(GetEncodedBinaryOpcode(I.getOpcode())); |
Dan Gohman | 1224c38 | 2009-07-20 21:19:07 +0000 | [diff] [blame] | 1047 | uint64_t Flags = GetOptimizationFlags(&I); |
| 1048 | if (Flags != 0) { |
| 1049 | if (AbbrevToUse == FUNCTION_INST_BINOP_ABBREV) |
| 1050 | AbbrevToUse = FUNCTION_INST_BINOP_FLAGS_ABBREV; |
| 1051 | Vals.push_back(Flags); |
| 1052 | } |
Chris Lattner | b9d0c2a | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 1053 | } |
| 1054 | break; |
Chris Lattner | d309c75 | 2007-05-01 02:13:26 +0000 | [diff] [blame] | 1055 | |
| 1056 | case Instruction::GetElementPtr: |
| 1057 | Code = bitc::FUNC_CODE_INST_GEP; |
Dan Gohman | dd8004d | 2009-07-27 21:53:46 +0000 | [diff] [blame] | 1058 | if (cast<GEPOperator>(&I)->isInBounds()) |
| 1059 | Code = bitc::FUNC_CODE_INST_INBOUNDS_GEP; |
Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1060 | for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) |
| 1061 | PushValueAndType(I.getOperand(i), InstID, Vals, VE); |
Chris Lattner | d309c75 | 2007-05-01 02:13:26 +0000 | [diff] [blame] | 1062 | break; |
Dan Gohman | 0aab28b | 2008-05-31 19:11:15 +0000 | [diff] [blame] | 1063 | case Instruction::ExtractValue: { |
Dan Gohman | e4977cf | 2008-05-23 01:55:30 +0000 | [diff] [blame] | 1064 | Code = bitc::FUNC_CODE_INST_EXTRACTVAL; |
Dan Gohman | 0aab28b | 2008-05-31 19:11:15 +0000 | [diff] [blame] | 1065 | PushValueAndType(I.getOperand(0), InstID, Vals, VE); |
| 1066 | const ExtractValueInst *EVI = cast<ExtractValueInst>(&I); |
| 1067 | for (const unsigned *i = EVI->idx_begin(), *e = EVI->idx_end(); i != e; ++i) |
| 1068 | Vals.push_back(*i); |
Dan Gohman | e4977cf | 2008-05-23 01:55:30 +0000 | [diff] [blame] | 1069 | break; |
Dan Gohman | 0aab28b | 2008-05-31 19:11:15 +0000 | [diff] [blame] | 1070 | } |
| 1071 | case Instruction::InsertValue: { |
Dan Gohman | e4977cf | 2008-05-23 01:55:30 +0000 | [diff] [blame] | 1072 | Code = bitc::FUNC_CODE_INST_INSERTVAL; |
Dan Gohman | 0aab28b | 2008-05-31 19:11:15 +0000 | [diff] [blame] | 1073 | PushValueAndType(I.getOperand(0), InstID, Vals, VE); |
| 1074 | PushValueAndType(I.getOperand(1), InstID, Vals, VE); |
| 1075 | const InsertValueInst *IVI = cast<InsertValueInst>(&I); |
| 1076 | for (const unsigned *i = IVI->idx_begin(), *e = IVI->idx_end(); i != e; ++i) |
| 1077 | Vals.push_back(*i); |
Dan Gohman | e4977cf | 2008-05-23 01:55:30 +0000 | [diff] [blame] | 1078 | break; |
Dan Gohman | 0aab28b | 2008-05-31 19:11:15 +0000 | [diff] [blame] | 1079 | } |
Chris Lattner | d309c75 | 2007-05-01 02:13:26 +0000 | [diff] [blame] | 1080 | case Instruction::Select: |
Dan Gohman | fb2bbbe | 2008-09-16 01:01:33 +0000 | [diff] [blame] | 1081 | Code = bitc::FUNC_CODE_INST_VSELECT; |
Chris Lattner | abfbf85 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 1082 | PushValueAndType(I.getOperand(1), InstID, Vals, VE); |
Chris Lattner | d309c75 | 2007-05-01 02:13:26 +0000 | [diff] [blame] | 1083 | Vals.push_back(VE.getValueID(I.getOperand(2))); |
Dan Gohman | fb2bbbe | 2008-09-16 01:01:33 +0000 | [diff] [blame] | 1084 | PushValueAndType(I.getOperand(0), InstID, Vals, VE); |
Chris Lattner | d309c75 | 2007-05-01 02:13:26 +0000 | [diff] [blame] | 1085 | break; |
| 1086 | case Instruction::ExtractElement: |
| 1087 | Code = bitc::FUNC_CODE_INST_EXTRACTELT; |
Chris Lattner | abfbf85 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 1088 | PushValueAndType(I.getOperand(0), InstID, Vals, VE); |
Chris Lattner | d309c75 | 2007-05-01 02:13:26 +0000 | [diff] [blame] | 1089 | Vals.push_back(VE.getValueID(I.getOperand(1))); |
| 1090 | break; |
| 1091 | case Instruction::InsertElement: |
| 1092 | Code = bitc::FUNC_CODE_INST_INSERTELT; |
Chris Lattner | abfbf85 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 1093 | PushValueAndType(I.getOperand(0), InstID, Vals, VE); |
Chris Lattner | d309c75 | 2007-05-01 02:13:26 +0000 | [diff] [blame] | 1094 | Vals.push_back(VE.getValueID(I.getOperand(1))); |
| 1095 | Vals.push_back(VE.getValueID(I.getOperand(2))); |
| 1096 | break; |
| 1097 | case Instruction::ShuffleVector: |
| 1098 | Code = bitc::FUNC_CODE_INST_SHUFFLEVEC; |
Chris Lattner | abfbf85 | 2007-05-06 00:21:25 +0000 | [diff] [blame] | 1099 | PushValueAndType(I.getOperand(0), InstID, Vals, VE); |
Chris Lattner | d309c75 | 2007-05-01 02:13:26 +0000 | [diff] [blame] | 1100 | Vals.push_back(VE.getValueID(I.getOperand(1))); |
| 1101 | Vals.push_back(VE.getValueID(I.getOperand(2))); |
| 1102 | break; |
| 1103 | case Instruction::ICmp: |
| 1104 | case Instruction::FCmp: |
Nick Lewycky | 7f6aa2b | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 1105 | // compare returning Int1Ty or vector of Int1Ty |
| 1106 | Code = bitc::FUNC_CODE_INST_CMP2; |
Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1107 | PushValueAndType(I.getOperand(0), InstID, Vals, VE); |
Chris Lattner | d309c75 | 2007-05-01 02:13:26 +0000 | [diff] [blame] | 1108 | Vals.push_back(VE.getValueID(I.getOperand(1))); |
| 1109 | Vals.push_back(cast<CmpInst>(I).getPredicate()); |
| 1110 | break; |
| 1111 | |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1112 | case Instruction::Ret: |
Devang Patel | d9d99ff | 2008-02-26 01:29:32 +0000 | [diff] [blame] | 1113 | { |
| 1114 | Code = bitc::FUNC_CODE_INST_RET; |
| 1115 | unsigned NumOperands = I.getNumOperands(); |
Devang Patel | d9d99ff | 2008-02-26 01:29:32 +0000 | [diff] [blame] | 1116 | if (NumOperands == 0) |
| 1117 | AbbrevToUse = FUNCTION_INST_RET_VOID_ABBREV; |
| 1118 | else if (NumOperands == 1) { |
| 1119 | if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE)) |
| 1120 | AbbrevToUse = FUNCTION_INST_RET_VAL_ABBREV; |
| 1121 | } else { |
| 1122 | for (unsigned i = 0, e = NumOperands; i != e; ++i) |
| 1123 | PushValueAndType(I.getOperand(i), InstID, Vals, VE); |
| 1124 | } |
| 1125 | } |
Chris Lattner | d309c75 | 2007-05-01 02:13:26 +0000 | [diff] [blame] | 1126 | break; |
| 1127 | case Instruction::Br: |
Gabor Greif | ae6ab1e | 2009-01-30 18:27:21 +0000 | [diff] [blame] | 1128 | { |
| 1129 | Code = bitc::FUNC_CODE_INST_BR; |
Chris Lattner | f9be95f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 1130 | BranchInst &II = cast<BranchInst>(I); |
Gabor Greif | ae6ab1e | 2009-01-30 18:27:21 +0000 | [diff] [blame] | 1131 | Vals.push_back(VE.getValueID(II.getSuccessor(0))); |
| 1132 | if (II.isConditional()) { |
| 1133 | Vals.push_back(VE.getValueID(II.getSuccessor(1))); |
| 1134 | Vals.push_back(VE.getValueID(II.getCondition())); |
| 1135 | } |
Chris Lattner | d309c75 | 2007-05-01 02:13:26 +0000 | [diff] [blame] | 1136 | } |
| 1137 | break; |
| 1138 | case Instruction::Switch: |
Stepan Dyatkovskiy | 2447312 | 2012-02-01 07:49:51 +0000 | [diff] [blame] | 1139 | { |
| 1140 | Code = bitc::FUNC_CODE_INST_SWITCH; |
| 1141 | SwitchInst &SI = cast<SwitchInst>(I); |
| 1142 | Vals.push_back(VE.getTypeID(SI.getCondition()->getType())); |
| 1143 | Vals.push_back(VE.getValueID(SI.getCondition())); |
| 1144 | Vals.push_back(VE.getValueID(SI.getDefaultDest())); |
Stepan Dyatkovskiy | 3d3abe0 | 2012-03-11 06:09:17 +0000 | [diff] [blame] | 1145 | for (SwitchInst::CaseIt i = SI.case_begin(), e = SI.case_end(); |
Stepan Dyatkovskiy | c10fa6c | 2012-03-08 07:06:20 +0000 | [diff] [blame] | 1146 | i != e; ++i) { |
Stepan Dyatkovskiy | 1f98383 | 2012-05-08 08:33:21 +0000 | [diff] [blame] | 1147 | Vals.push_back(VE.getValueID(i.getCaseValue())); |
Stepan Dyatkovskiy | c10fa6c | 2012-03-08 07:06:20 +0000 | [diff] [blame] | 1148 | Vals.push_back(VE.getValueID(i.getCaseSuccessor())); |
Stepan Dyatkovskiy | 2447312 | 2012-02-01 07:49:51 +0000 | [diff] [blame] | 1149 | } |
| 1150 | } |
Chris Lattner | d309c75 | 2007-05-01 02:13:26 +0000 | [diff] [blame] | 1151 | break; |
Chris Lattner | ab21db7 | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 1152 | case Instruction::IndirectBr: |
| 1153 | Code = bitc::FUNC_CODE_INST_INDIRECTBR; |
Chris Lattner | f9be95f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 1154 | Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); |
| 1155 | for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) |
| 1156 | Vals.push_back(VE.getValueID(I.getOperand(i))); |
| 1157 | break; |
| 1158 | |
Chris Lattner | 60ce9b5 | 2007-05-01 07:03:37 +0000 | [diff] [blame] | 1159 | case Instruction::Invoke: { |
Gabor Greif | 75a46eb | 2009-01-16 18:40:27 +0000 | [diff] [blame] | 1160 | const InvokeInst *II = cast<InvokeInst>(&I); |
| 1161 | const Value *Callee(II->getCalledValue()); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1162 | PointerType *PTy = cast<PointerType>(Callee->getType()); |
| 1163 | FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); |
Chris Lattner | d309c75 | 2007-05-01 02:13:26 +0000 | [diff] [blame] | 1164 | Code = bitc::FUNC_CODE_INST_INVOKE; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1165 | |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 1166 | Vals.push_back(VE.getAttributeID(II->getAttributes())); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 1167 | Vals.push_back(II->getCallingConv()); |
Gabor Greif | 8fe53b7 | 2009-01-07 22:39:29 +0000 | [diff] [blame] | 1168 | Vals.push_back(VE.getValueID(II->getNormalDest())); |
| 1169 | Vals.push_back(VE.getValueID(II->getUnwindDest())); |
Gabor Greif | 75a46eb | 2009-01-16 18:40:27 +0000 | [diff] [blame] | 1170 | PushValueAndType(Callee, InstID, Vals, VE); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1171 | |
Chris Lattner | d309c75 | 2007-05-01 02:13:26 +0000 | [diff] [blame] | 1172 | // Emit value #'s for the fixed parameters. |
Chris Lattner | d309c75 | 2007-05-01 02:13:26 +0000 | [diff] [blame] | 1173 | for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) |
Gabor Greif | c9f7500 | 2010-03-24 13:21:49 +0000 | [diff] [blame] | 1174 | Vals.push_back(VE.getValueID(I.getOperand(i))); // fixed param. |
Chris Lattner | d309c75 | 2007-05-01 02:13:26 +0000 | [diff] [blame] | 1175 | |
| 1176 | // Emit type/value pairs for varargs params. |
| 1177 | if (FTy->isVarArg()) { |
Gabor Greif | c9f7500 | 2010-03-24 13:21:49 +0000 | [diff] [blame] | 1178 | for (unsigned i = FTy->getNumParams(), e = I.getNumOperands()-3; |
Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1179 | i != e; ++i) |
| 1180 | PushValueAndType(I.getOperand(i), InstID, Vals, VE); // vararg |
Chris Lattner | d309c75 | 2007-05-01 02:13:26 +0000 | [diff] [blame] | 1181 | } |
| 1182 | break; |
Chris Lattner | 60ce9b5 | 2007-05-01 07:03:37 +0000 | [diff] [blame] | 1183 | } |
Bill Wendling | dccc03b | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 1184 | case Instruction::Resume: |
| 1185 | Code = bitc::FUNC_CODE_INST_RESUME; |
| 1186 | PushValueAndType(I.getOperand(0), InstID, Vals, VE); |
| 1187 | break; |
Chris Lattner | b9d0c2a | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 1188 | case Instruction::Unreachable: |
| 1189 | Code = bitc::FUNC_CODE_INST_UNREACHABLE; |
Chris Lattner | 94687ac | 2007-05-06 01:28:01 +0000 | [diff] [blame] | 1190 | AbbrevToUse = FUNCTION_INST_UNREACHABLE_ABBREV; |
Chris Lattner | b9d0c2a | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 1191 | break; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1192 | |
Jay Foad | c137120 | 2011-06-20 14:18:48 +0000 | [diff] [blame] | 1193 | case Instruction::PHI: { |
| 1194 | const PHINode &PN = cast<PHINode>(I); |
Chris Lattner | d309c75 | 2007-05-01 02:13:26 +0000 | [diff] [blame] | 1195 | Code = bitc::FUNC_CODE_INST_PHI; |
Jay Foad | c137120 | 2011-06-20 14:18:48 +0000 | [diff] [blame] | 1196 | Vals.push_back(VE.getTypeID(PN.getType())); |
| 1197 | for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 1198 | Vals.push_back(VE.getValueID(PN.getIncomingValue(i))); |
| 1199 | Vals.push_back(VE.getValueID(PN.getIncomingBlock(i))); |
| 1200 | } |
Chris Lattner | d309c75 | 2007-05-01 02:13:26 +0000 | [diff] [blame] | 1201 | break; |
Jay Foad | c137120 | 2011-06-20 14:18:48 +0000 | [diff] [blame] | 1202 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1203 | |
Bill Wendling | e6e8826 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 1204 | case Instruction::LandingPad: { |
| 1205 | const LandingPadInst &LP = cast<LandingPadInst>(I); |
| 1206 | Code = bitc::FUNC_CODE_INST_LANDINGPAD; |
| 1207 | Vals.push_back(VE.getTypeID(LP.getType())); |
| 1208 | PushValueAndType(LP.getPersonalityFn(), InstID, Vals, VE); |
| 1209 | Vals.push_back(LP.isCleanup()); |
| 1210 | Vals.push_back(LP.getNumClauses()); |
| 1211 | for (unsigned I = 0, E = LP.getNumClauses(); I != E; ++I) { |
| 1212 | if (LP.isCatch(I)) |
| 1213 | Vals.push_back(LandingPadInst::Catch); |
| 1214 | else |
| 1215 | Vals.push_back(LandingPadInst::Filter); |
| 1216 | PushValueAndType(LP.getClause(I), InstID, Vals, VE); |
| 1217 | } |
| 1218 | break; |
| 1219 | } |
| 1220 | |
Chris Lattner | d309c75 | 2007-05-01 02:13:26 +0000 | [diff] [blame] | 1221 | case Instruction::Alloca: |
| 1222 | Code = bitc::FUNC_CODE_INST_ALLOCA; |
| 1223 | Vals.push_back(VE.getTypeID(I.getType())); |
Dan Gohman | a37dd3e | 2010-05-28 01:38:28 +0000 | [diff] [blame] | 1224 | Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); |
Chris Lattner | d309c75 | 2007-05-01 02:13:26 +0000 | [diff] [blame] | 1225 | Vals.push_back(VE.getValueID(I.getOperand(0))); // size. |
| 1226 | Vals.push_back(Log2_32(cast<AllocaInst>(I).getAlignment())+1); |
| 1227 | break; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1228 | |
Chris Lattner | d309c75 | 2007-05-01 02:13:26 +0000 | [diff] [blame] | 1229 | case Instruction::Load: |
Eli Friedman | 21006d4 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1230 | if (cast<LoadInst>(I).isAtomic()) { |
| 1231 | Code = bitc::FUNC_CODE_INST_LOADATOMIC; |
| 1232 | PushValueAndType(I.getOperand(0), InstID, Vals, VE); |
| 1233 | } else { |
| 1234 | Code = bitc::FUNC_CODE_INST_LOAD; |
| 1235 | if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE)) // ptr |
| 1236 | AbbrevToUse = FUNCTION_INST_LOAD_ABBREV; |
| 1237 | } |
Chris Lattner | d309c75 | 2007-05-01 02:13:26 +0000 | [diff] [blame] | 1238 | Vals.push_back(Log2_32(cast<LoadInst>(I).getAlignment())+1); |
| 1239 | Vals.push_back(cast<LoadInst>(I).isVolatile()); |
Eli Friedman | 21006d4 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1240 | if (cast<LoadInst>(I).isAtomic()) { |
| 1241 | Vals.push_back(GetEncodedOrdering(cast<LoadInst>(I).getOrdering())); |
| 1242 | Vals.push_back(GetEncodedSynchScope(cast<LoadInst>(I).getSynchScope())); |
| 1243 | } |
Chris Lattner | d309c75 | 2007-05-01 02:13:26 +0000 | [diff] [blame] | 1244 | break; |
| 1245 | case Instruction::Store: |
Eli Friedman | 21006d4 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1246 | if (cast<StoreInst>(I).isAtomic()) |
| 1247 | Code = bitc::FUNC_CODE_INST_STOREATOMIC; |
| 1248 | else |
| 1249 | Code = bitc::FUNC_CODE_INST_STORE; |
Christopher Lamb | fe63fb9 | 2007-12-11 08:59:05 +0000 | [diff] [blame] | 1250 | PushValueAndType(I.getOperand(1), InstID, Vals, VE); // ptrty + ptr |
| 1251 | Vals.push_back(VE.getValueID(I.getOperand(0))); // val. |
Chris Lattner | d309c75 | 2007-05-01 02:13:26 +0000 | [diff] [blame] | 1252 | Vals.push_back(Log2_32(cast<StoreInst>(I).getAlignment())+1); |
| 1253 | Vals.push_back(cast<StoreInst>(I).isVolatile()); |
Eli Friedman | 21006d4 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1254 | if (cast<StoreInst>(I).isAtomic()) { |
| 1255 | Vals.push_back(GetEncodedOrdering(cast<StoreInst>(I).getOrdering())); |
| 1256 | Vals.push_back(GetEncodedSynchScope(cast<StoreInst>(I).getSynchScope())); |
| 1257 | } |
Chris Lattner | d309c75 | 2007-05-01 02:13:26 +0000 | [diff] [blame] | 1258 | break; |
Eli Friedman | ff03048 | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1259 | case Instruction::AtomicCmpXchg: |
| 1260 | Code = bitc::FUNC_CODE_INST_CMPXCHG; |
| 1261 | PushValueAndType(I.getOperand(0), InstID, Vals, VE); // ptrty + ptr |
| 1262 | Vals.push_back(VE.getValueID(I.getOperand(1))); // cmp. |
| 1263 | Vals.push_back(VE.getValueID(I.getOperand(2))); // newval. |
| 1264 | Vals.push_back(cast<AtomicCmpXchgInst>(I).isVolatile()); |
| 1265 | Vals.push_back(GetEncodedOrdering( |
| 1266 | cast<AtomicCmpXchgInst>(I).getOrdering())); |
| 1267 | Vals.push_back(GetEncodedSynchScope( |
| 1268 | cast<AtomicCmpXchgInst>(I).getSynchScope())); |
| 1269 | break; |
| 1270 | case Instruction::AtomicRMW: |
| 1271 | Code = bitc::FUNC_CODE_INST_ATOMICRMW; |
| 1272 | PushValueAndType(I.getOperand(0), InstID, Vals, VE); // ptrty + ptr |
| 1273 | Vals.push_back(VE.getValueID(I.getOperand(1))); // val. |
| 1274 | Vals.push_back(GetEncodedRMWOperation( |
| 1275 | cast<AtomicRMWInst>(I).getOperation())); |
| 1276 | Vals.push_back(cast<AtomicRMWInst>(I).isVolatile()); |
| 1277 | Vals.push_back(GetEncodedOrdering(cast<AtomicRMWInst>(I).getOrdering())); |
| 1278 | Vals.push_back(GetEncodedSynchScope( |
| 1279 | cast<AtomicRMWInst>(I).getSynchScope())); |
| 1280 | break; |
Eli Friedman | 47f3513 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 1281 | case Instruction::Fence: |
| 1282 | Code = bitc::FUNC_CODE_INST_FENCE; |
| 1283 | Vals.push_back(GetEncodedOrdering(cast<FenceInst>(I).getOrdering())); |
| 1284 | Vals.push_back(GetEncodedSynchScope(cast<FenceInst>(I).getSynchScope())); |
| 1285 | break; |
Chris Lattner | d309c75 | 2007-05-01 02:13:26 +0000 | [diff] [blame] | 1286 | case Instruction::Call: { |
Gabor Greif | fc91e7d | 2010-06-26 09:35:09 +0000 | [diff] [blame] | 1287 | const CallInst &CI = cast<CallInst>(I); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1288 | PointerType *PTy = cast<PointerType>(CI.getCalledValue()->getType()); |
| 1289 | FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); |
Chris Lattner | a9bb713 | 2007-05-08 05:38:01 +0000 | [diff] [blame] | 1290 | |
Chris Lattner | 4f6bab9 | 2011-06-17 18:17:37 +0000 | [diff] [blame] | 1291 | Code = bitc::FUNC_CODE_INST_CALL; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1292 | |
Gabor Greif | fc91e7d | 2010-06-26 09:35:09 +0000 | [diff] [blame] | 1293 | Vals.push_back(VE.getAttributeID(CI.getAttributes())); |
| 1294 | Vals.push_back((CI.getCallingConv() << 1) | unsigned(CI.isTailCall())); |
| 1295 | PushValueAndType(CI.getCalledValue(), InstID, Vals, VE); // Callee |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1296 | |
Chris Lattner | d309c75 | 2007-05-01 02:13:26 +0000 | [diff] [blame] | 1297 | // Emit value #'s for the fixed parameters. |
Chris Lattner | d309c75 | 2007-05-01 02:13:26 +0000 | [diff] [blame] | 1298 | for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) |
Gabor Greif | fc91e7d | 2010-06-26 09:35:09 +0000 | [diff] [blame] | 1299 | Vals.push_back(VE.getValueID(CI.getArgOperand(i))); // fixed param. |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1300 | |
Chris Lattner | 60ce9b5 | 2007-05-01 07:03:37 +0000 | [diff] [blame] | 1301 | // Emit type/value pairs for varargs params. |
| 1302 | if (FTy->isVarArg()) { |
Gabor Greif | fc91e7d | 2010-06-26 09:35:09 +0000 | [diff] [blame] | 1303 | for (unsigned i = FTy->getNumParams(), e = CI.getNumArgOperands(); |
Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1304 | i != e; ++i) |
Gabor Greif | fc91e7d | 2010-06-26 09:35:09 +0000 | [diff] [blame] | 1305 | PushValueAndType(CI.getArgOperand(i), InstID, Vals, VE); // varargs |
Chris Lattner | d309c75 | 2007-05-01 02:13:26 +0000 | [diff] [blame] | 1306 | } |
| 1307 | break; |
Chris Lattner | 60ce9b5 | 2007-05-01 07:03:37 +0000 | [diff] [blame] | 1308 | } |
Chris Lattner | d309c75 | 2007-05-01 02:13:26 +0000 | [diff] [blame] | 1309 | case Instruction::VAArg: |
| 1310 | Code = bitc::FUNC_CODE_INST_VAARG; |
| 1311 | Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); // valistty |
| 1312 | Vals.push_back(VE.getValueID(I.getOperand(0))); // valist. |
| 1313 | Vals.push_back(VE.getTypeID(I.getType())); // restype. |
| 1314 | break; |
Chris Lattner | b9d0c2a | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 1315 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1316 | |
Chris Lattner | b9d0c2a | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 1317 | Stream.EmitRecord(Code, Vals, AbbrevToUse); |
| 1318 | Vals.clear(); |
| 1319 | } |
| 1320 | |
Chris Lattner | be1f993 | 2007-05-01 02:14:57 +0000 | [diff] [blame] | 1321 | // Emit names for globals/functions etc. |
| 1322 | static void WriteValueSymbolTable(const ValueSymbolTable &VST, |
| 1323 | const ValueEnumerator &VE, |
| 1324 | BitstreamWriter &Stream) { |
| 1325 | if (VST.empty()) return; |
Chris Lattner | ff294a4 | 2007-05-05 01:26:50 +0000 | [diff] [blame] | 1326 | Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 4); |
Chris Lattner | 2e7899d | 2007-05-04 20:34:50 +0000 | [diff] [blame] | 1327 | |
Chris Lattner | be1f993 | 2007-05-01 02:14:57 +0000 | [diff] [blame] | 1328 | // FIXME: Set up the abbrev, we know how many values there are! |
| 1329 | // FIXME: We know if the type names can use 7-bit ascii. |
| 1330 | SmallVector<unsigned, 64> NameVals; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1331 | |
Chris Lattner | be1f993 | 2007-05-01 02:14:57 +0000 | [diff] [blame] | 1332 | for (ValueSymbolTable::const_iterator SI = VST.begin(), SE = VST.end(); |
| 1333 | SI != SE; ++SI) { |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1334 | |
Chris Lattner | 5969830 | 2007-05-04 20:52:02 +0000 | [diff] [blame] | 1335 | const ValueName &Name = *SI; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1336 | |
Chris Lattner | 5969830 | 2007-05-04 20:52:02 +0000 | [diff] [blame] | 1337 | // Figure out the encoding to use for the name. |
| 1338 | bool is7Bit = true; |
Chris Lattner | ff294a4 | 2007-05-05 01:26:50 +0000 | [diff] [blame] | 1339 | bool isChar6 = true; |
| 1340 | for (const char *C = Name.getKeyData(), *E = C+Name.getKeyLength(); |
| 1341 | C != E; ++C) { |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1342 | if (isChar6) |
Chris Lattner | ff294a4 | 2007-05-05 01:26:50 +0000 | [diff] [blame] | 1343 | isChar6 = BitCodeAbbrevOp::isChar6(*C); |
| 1344 | if ((unsigned char)*C & 128) { |
Chris Lattner | 5969830 | 2007-05-04 20:52:02 +0000 | [diff] [blame] | 1345 | is7Bit = false; |
Chris Lattner | ff294a4 | 2007-05-05 01:26:50 +0000 | [diff] [blame] | 1346 | break; // don't bother scanning the rest. |
Chris Lattner | 5969830 | 2007-05-04 20:52:02 +0000 | [diff] [blame] | 1347 | } |
Chris Lattner | ff294a4 | 2007-05-05 01:26:50 +0000 | [diff] [blame] | 1348 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1349 | |
Chris Lattner | fd1ae95 | 2007-05-04 21:31:13 +0000 | [diff] [blame] | 1350 | unsigned AbbrevToUse = VST_ENTRY_8_ABBREV; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1351 | |
Bill Wendling | 5d7a5a4 | 2011-04-10 23:18:04 +0000 | [diff] [blame] | 1352 | // VST_ENTRY: [valueid, namechar x N] |
| 1353 | // VST_BBENTRY: [bbid, namechar x N] |
Chris Lattner | e825ed5 | 2007-05-03 22:18:21 +0000 | [diff] [blame] | 1354 | unsigned Code; |
Bill Wendling | 5d7a5a4 | 2011-04-10 23:18:04 +0000 | [diff] [blame] | 1355 | if (isa<BasicBlock>(SI->getValue())) { |
| 1356 | Code = bitc::VST_CODE_BBENTRY; |
| 1357 | if (isChar6) |
| 1358 | AbbrevToUse = VST_BBENTRY_6_ABBREV; |
Chris Lattner | e825ed5 | 2007-05-03 22:18:21 +0000 | [diff] [blame] | 1359 | } else { |
| 1360 | Code = bitc::VST_CODE_ENTRY; |
Chris Lattner | ff294a4 | 2007-05-05 01:26:50 +0000 | [diff] [blame] | 1361 | if (isChar6) |
| 1362 | AbbrevToUse = VST_ENTRY_6_ABBREV; |
| 1363 | else if (is7Bit) |
| 1364 | AbbrevToUse = VST_ENTRY_7_ABBREV; |
Chris Lattner | e825ed5 | 2007-05-03 22:18:21 +0000 | [diff] [blame] | 1365 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1366 | |
Chris Lattner | e825ed5 | 2007-05-03 22:18:21 +0000 | [diff] [blame] | 1367 | NameVals.push_back(VE.getValueID(SI->getValue())); |
Chris Lattner | 5969830 | 2007-05-04 20:52:02 +0000 | [diff] [blame] | 1368 | for (const char *P = Name.getKeyData(), |
| 1369 | *E = Name.getKeyData()+Name.getKeyLength(); P != E; ++P) |
Chris Lattner | be1f993 | 2007-05-01 02:14:57 +0000 | [diff] [blame] | 1370 | NameVals.push_back((unsigned char)*P); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1371 | |
Chris Lattner | be1f993 | 2007-05-01 02:14:57 +0000 | [diff] [blame] | 1372 | // Emit the finished record. |
Chris Lattner | e825ed5 | 2007-05-03 22:18:21 +0000 | [diff] [blame] | 1373 | Stream.EmitRecord(Code, NameVals, AbbrevToUse); |
Chris Lattner | be1f993 | 2007-05-01 02:14:57 +0000 | [diff] [blame] | 1374 | NameVals.clear(); |
| 1375 | } |
| 1376 | Stream.ExitBlock(); |
| 1377 | } |
| 1378 | |
Chris Lattner | 8d35c79 | 2007-04-26 03:50:57 +0000 | [diff] [blame] | 1379 | /// WriteFunction - Emit a function body to the module stream. |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1380 | static void WriteFunction(const Function &F, ValueEnumerator &VE, |
Chris Lattner | 198f34a | 2007-04-26 03:27:58 +0000 | [diff] [blame] | 1381 | BitstreamWriter &Stream) { |
Chris Lattner | f9f2e83 | 2007-05-06 02:38:57 +0000 | [diff] [blame] | 1382 | Stream.EnterSubblock(bitc::FUNCTION_BLOCK_ID, 4); |
Chad Rosier | 6a0c04d | 2011-06-03 17:02:19 +0000 | [diff] [blame] | 1383 | VE.incorporateFunction(F); |
Chris Lattner | b9d0c2a | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 1384 | |
| 1385 | SmallVector<unsigned, 64> Vals; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1386 | |
Chris Lattner | b9d0c2a | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 1387 | // Emit the number of basic blocks, so the reader can create them ahead of |
| 1388 | // time. |
| 1389 | Vals.push_back(VE.getBasicBlocks().size()); |
| 1390 | Stream.EmitRecord(bitc::FUNC_CODE_DECLAREBLOCKS, Vals); |
| 1391 | Vals.clear(); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1392 | |
Chris Lattner | b9d0c2a | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 1393 | // If there are function-local constants, emit them now. |
| 1394 | unsigned CstStart, CstEnd; |
| 1395 | VE.getFunctionConstantRange(CstStart, CstEnd); |
Devang Patel | 912538b | 2009-08-04 05:01:35 +0000 | [diff] [blame] | 1396 | WriteConstants(CstStart, CstEnd, VE, Stream, false); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1397 | |
Victor Hernandez | 8ea5d4c | 2010-01-14 01:50:08 +0000 | [diff] [blame] | 1398 | // If there is function-local metadata, emit it now. |
Victor Hernandez | 3cd7c3d | 2010-01-14 19:38:44 +0000 | [diff] [blame] | 1399 | WriteFunctionLocalMetadata(F, VE, Stream); |
Victor Hernandez | 8ea5d4c | 2010-01-14 01:50:08 +0000 | [diff] [blame] | 1400 | |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1401 | // Keep a running idea of what the instruction ID is. |
Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1402 | unsigned InstID = CstEnd; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1403 | |
Chris Lattner | a624524 | 2010-04-03 02:17:50 +0000 | [diff] [blame] | 1404 | bool NeedsMetadataAttachment = false; |
| 1405 | |
| 1406 | DebugLoc LastDL; |
| 1407 | |
Chris Lattner | b9d0c2a | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 1408 | // Finally, emit all the instructions, in order. |
Nick Lewycky | 280a6e6 | 2008-04-25 16:53:59 +0000 | [diff] [blame] | 1409 | for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) |
Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1410 | for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); |
| 1411 | I != E; ++I) { |
| 1412 | WriteInstruction(*I, InstID, VE, Stream, Vals); |
Chris Lattner | a624524 | 2010-04-03 02:17:50 +0000 | [diff] [blame] | 1413 | |
Chris Lattner | 3990b12 | 2009-12-28 23:41:32 +0000 | [diff] [blame] | 1414 | if (!I->getType()->isVoidTy()) |
Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1415 | ++InstID; |
Chris Lattner | a624524 | 2010-04-03 02:17:50 +0000 | [diff] [blame] | 1416 | |
| 1417 | // If the instruction has metadata, write a metadata attachment later. |
| 1418 | NeedsMetadataAttachment |= I->hasMetadataOtherThanDebugLoc(); |
| 1419 | |
| 1420 | // If the instruction has a debug location, emit it. |
| 1421 | DebugLoc DL = I->getDebugLoc(); |
| 1422 | if (DL.isUnknown()) { |
| 1423 | // nothing todo. |
| 1424 | } else if (DL == LastDL) { |
| 1425 | // Just repeat the same debug loc as last time. |
| 1426 | Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC_AGAIN, Vals); |
| 1427 | } else { |
| 1428 | MDNode *Scope, *IA; |
| 1429 | DL.getScopeAndInlinedAt(Scope, IA, I->getContext()); |
| 1430 | |
| 1431 | Vals.push_back(DL.getLine()); |
| 1432 | Vals.push_back(DL.getCol()); |
| 1433 | Vals.push_back(Scope ? VE.getValueID(Scope)+1 : 0); |
| 1434 | Vals.push_back(IA ? VE.getValueID(IA)+1 : 0); |
Chris Lattner | 4f6bab9 | 2011-06-17 18:17:37 +0000 | [diff] [blame] | 1435 | Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC, Vals); |
Chris Lattner | a624524 | 2010-04-03 02:17:50 +0000 | [diff] [blame] | 1436 | Vals.clear(); |
| 1437 | |
| 1438 | LastDL = DL; |
| 1439 | } |
Chris Lattner | 7337ab9 | 2007-05-06 00:00:00 +0000 | [diff] [blame] | 1440 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1441 | |
Chris Lattner | be1f993 | 2007-05-01 02:14:57 +0000 | [diff] [blame] | 1442 | // Emit names for all the instructions etc. |
| 1443 | WriteValueSymbolTable(F.getValueSymbolTable(), VE, Stream); |
Devang Patel | e8e0213 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 1444 | |
Chris Lattner | a624524 | 2010-04-03 02:17:50 +0000 | [diff] [blame] | 1445 | if (NeedsMetadataAttachment) |
| 1446 | WriteMetadataAttachment(F, VE, Stream); |
Chad Rosier | 6a0c04d | 2011-06-03 17:02:19 +0000 | [diff] [blame] | 1447 | VE.purgeFunction(); |
Chris Lattner | b9d0c2a | 2007-04-26 05:53:54 +0000 | [diff] [blame] | 1448 | Stream.ExitBlock(); |
Chris Lattner | 198f34a | 2007-04-26 03:27:58 +0000 | [diff] [blame] | 1449 | } |
| 1450 | |
Chris Lattner | 07faafc | 2007-05-04 18:26:27 +0000 | [diff] [blame] | 1451 | // Emit blockinfo, which defines the standard abbreviations etc. |
Chris Lattner | a0f1ecc | 2007-05-05 07:36:14 +0000 | [diff] [blame] | 1452 | static void WriteBlockInfo(const ValueEnumerator &VE, BitstreamWriter &Stream) { |
Chris Lattner | 07faafc | 2007-05-04 18:26:27 +0000 | [diff] [blame] | 1453 | // We only want to emit block info records for blocks that have multiple |
| 1454 | // instances: CONSTANTS_BLOCK, FUNCTION_BLOCK and VALUE_SYMTAB_BLOCK. Other |
| 1455 | // blocks can defined their abbrevs inline. |
Chris Lattner | e17b658 | 2007-05-05 00:17:00 +0000 | [diff] [blame] | 1456 | Stream.EnterBlockInfoBlock(2); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1457 | |
Chris Lattner | e17b658 | 2007-05-05 00:17:00 +0000 | [diff] [blame] | 1458 | { // 8-bit fixed-width VST_ENTRY/VST_BBENTRY strings. |
| 1459 | BitCodeAbbrev *Abbv = new BitCodeAbbrev(); |
| 1460 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); |
| 1461 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); |
| 1462 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); |
| 1463 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1464 | if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, |
Chris Lattner | e17b658 | 2007-05-05 00:17:00 +0000 | [diff] [blame] | 1465 | Abbv) != VST_ENTRY_8_ABBREV) |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1466 | llvm_unreachable("Unexpected abbrev ordering!"); |
Chris Lattner | e17b658 | 2007-05-05 00:17:00 +0000 | [diff] [blame] | 1467 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1468 | |
Chris Lattner | e17b658 | 2007-05-05 00:17:00 +0000 | [diff] [blame] | 1469 | { // 7-bit fixed width VST_ENTRY strings. |
| 1470 | BitCodeAbbrev *Abbv = new BitCodeAbbrev(); |
| 1471 | Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY)); |
| 1472 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); |
| 1473 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); |
| 1474 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7)); |
| 1475 | if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, |
| 1476 | Abbv) != VST_ENTRY_7_ABBREV) |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1477 | llvm_unreachable("Unexpected abbrev ordering!"); |
Chris Lattner | e17b658 | 2007-05-05 00:17:00 +0000 | [diff] [blame] | 1478 | } |
Chris Lattner | ff294a4 | 2007-05-05 01:26:50 +0000 | [diff] [blame] | 1479 | { // 6-bit char6 VST_ENTRY strings. |
| 1480 | BitCodeAbbrev *Abbv = new BitCodeAbbrev(); |
| 1481 | Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY)); |
| 1482 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); |
| 1483 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); |
| 1484 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6)); |
| 1485 | if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, |
| 1486 | Abbv) != VST_ENTRY_6_ABBREV) |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1487 | llvm_unreachable("Unexpected abbrev ordering!"); |
Chris Lattner | ff294a4 | 2007-05-05 01:26:50 +0000 | [diff] [blame] | 1488 | } |
| 1489 | { // 6-bit char6 VST_BBENTRY strings. |
Chris Lattner | e17b658 | 2007-05-05 00:17:00 +0000 | [diff] [blame] | 1490 | BitCodeAbbrev *Abbv = new BitCodeAbbrev(); |
| 1491 | Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_BBENTRY)); |
| 1492 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); |
| 1493 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); |
Chris Lattner | ff294a4 | 2007-05-05 01:26:50 +0000 | [diff] [blame] | 1494 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6)); |
Chris Lattner | e17b658 | 2007-05-05 00:17:00 +0000 | [diff] [blame] | 1495 | if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, |
Chris Lattner | ff294a4 | 2007-05-05 01:26:50 +0000 | [diff] [blame] | 1496 | Abbv) != VST_BBENTRY_6_ABBREV) |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1497 | llvm_unreachable("Unexpected abbrev ordering!"); |
Chris Lattner | e17b658 | 2007-05-05 00:17:00 +0000 | [diff] [blame] | 1498 | } |
Bill Wendling | 5d7a5a4 | 2011-04-10 23:18:04 +0000 | [diff] [blame] | 1499 | |
| 1500 | |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1501 | |
Chris Lattner | a0f1ecc | 2007-05-05 07:36:14 +0000 | [diff] [blame] | 1502 | { // SETTYPE abbrev for CONSTANTS_BLOCK. |
| 1503 | BitCodeAbbrev *Abbv = new BitCodeAbbrev(); |
| 1504 | Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_SETTYPE)); |
| 1505 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, |
| 1506 | Log2_32_Ceil(VE.getTypes().size()+1))); |
| 1507 | if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, |
| 1508 | Abbv) != CONSTANTS_SETTYPE_ABBREV) |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1509 | llvm_unreachable("Unexpected abbrev ordering!"); |
Chris Lattner | a0f1ecc | 2007-05-05 07:36:14 +0000 | [diff] [blame] | 1510 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1511 | |
Chris Lattner | a0f1ecc | 2007-05-05 07:36:14 +0000 | [diff] [blame] | 1512 | { // INTEGER abbrev for CONSTANTS_BLOCK. |
| 1513 | BitCodeAbbrev *Abbv = new BitCodeAbbrev(); |
| 1514 | Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_INTEGER)); |
| 1515 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); |
| 1516 | if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, |
| 1517 | Abbv) != CONSTANTS_INTEGER_ABBREV) |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1518 | llvm_unreachable("Unexpected abbrev ordering!"); |
Chris Lattner | a0f1ecc | 2007-05-05 07:36:14 +0000 | [diff] [blame] | 1519 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1520 | |
Chris Lattner | a0f1ecc | 2007-05-05 07:36:14 +0000 | [diff] [blame] | 1521 | { // CE_CAST abbrev for CONSTANTS_BLOCK. |
| 1522 | BitCodeAbbrev *Abbv = new BitCodeAbbrev(); |
| 1523 | Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CE_CAST)); |
| 1524 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // cast opc |
| 1525 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // typeid |
| 1526 | Log2_32_Ceil(VE.getTypes().size()+1))); |
| 1527 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id |
| 1528 | |
| 1529 | if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, |
| 1530 | Abbv) != CONSTANTS_CE_CAST_Abbrev) |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1531 | llvm_unreachable("Unexpected abbrev ordering!"); |
Chris Lattner | a0f1ecc | 2007-05-05 07:36:14 +0000 | [diff] [blame] | 1532 | } |
| 1533 | { // NULL abbrev for CONSTANTS_BLOCK. |
| 1534 | BitCodeAbbrev *Abbv = new BitCodeAbbrev(); |
| 1535 | Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_NULL)); |
| 1536 | if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, |
| 1537 | Abbv) != CONSTANTS_NULL_Abbrev) |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1538 | llvm_unreachable("Unexpected abbrev ordering!"); |
Chris Lattner | a0f1ecc | 2007-05-05 07:36:14 +0000 | [diff] [blame] | 1539 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1540 | |
Chris Lattner | 440168b | 2007-05-05 07:44:49 +0000 | [diff] [blame] | 1541 | // FIXME: This should only use space for first class types! |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1542 | |
Chris Lattner | 440168b | 2007-05-05 07:44:49 +0000 | [diff] [blame] | 1543 | { // INST_LOAD abbrev for FUNCTION_BLOCK. |
| 1544 | BitCodeAbbrev *Abbv = new BitCodeAbbrev(); |
| 1545 | Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_LOAD)); |
Chris Lattner | 440168b | 2007-05-05 07:44:49 +0000 | [diff] [blame] | 1546 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Ptr |
| 1547 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // Align |
| 1548 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // volatile |
| 1549 | if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, |
| 1550 | Abbv) != FUNCTION_INST_LOAD_ABBREV) |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1551 | llvm_unreachable("Unexpected abbrev ordering!"); |
Chris Lattner | 440168b | 2007-05-05 07:44:49 +0000 | [diff] [blame] | 1552 | } |
Chris Lattner | f9f2e83 | 2007-05-06 02:38:57 +0000 | [diff] [blame] | 1553 | { // INST_BINOP abbrev for FUNCTION_BLOCK. |
| 1554 | BitCodeAbbrev *Abbv = new BitCodeAbbrev(); |
| 1555 | Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP)); |
| 1556 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS |
| 1557 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS |
| 1558 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc |
| 1559 | if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, |
| 1560 | Abbv) != FUNCTION_INST_BINOP_ABBREV) |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1561 | llvm_unreachable("Unexpected abbrev ordering!"); |
Chris Lattner | f9f2e83 | 2007-05-06 02:38:57 +0000 | [diff] [blame] | 1562 | } |
Dan Gohman | 1224c38 | 2009-07-20 21:19:07 +0000 | [diff] [blame] | 1563 | { // INST_BINOP_FLAGS abbrev for FUNCTION_BLOCK. |
| 1564 | BitCodeAbbrev *Abbv = new BitCodeAbbrev(); |
| 1565 | Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP)); |
| 1566 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS |
| 1567 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS |
| 1568 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc |
| 1569 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7)); // flags |
| 1570 | if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, |
| 1571 | Abbv) != FUNCTION_INST_BINOP_FLAGS_ABBREV) |
| 1572 | llvm_unreachable("Unexpected abbrev ordering!"); |
| 1573 | } |
Chris Lattner | f9f2e83 | 2007-05-06 02:38:57 +0000 | [diff] [blame] | 1574 | { // INST_CAST abbrev for FUNCTION_BLOCK. |
| 1575 | BitCodeAbbrev *Abbv = new BitCodeAbbrev(); |
| 1576 | Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_CAST)); |
| 1577 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // OpVal |
| 1578 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty |
| 1579 | Log2_32_Ceil(VE.getTypes().size()+1))); |
| 1580 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc |
| 1581 | if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, |
| 1582 | Abbv) != FUNCTION_INST_CAST_ABBREV) |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1583 | llvm_unreachable("Unexpected abbrev ordering!"); |
Chris Lattner | f9f2e83 | 2007-05-06 02:38:57 +0000 | [diff] [blame] | 1584 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1585 | |
Chris Lattner | 94687ac | 2007-05-06 01:28:01 +0000 | [diff] [blame] | 1586 | { // INST_RET abbrev for FUNCTION_BLOCK. |
| 1587 | BitCodeAbbrev *Abbv = new BitCodeAbbrev(); |
| 1588 | Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET)); |
| 1589 | if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, |
| 1590 | Abbv) != FUNCTION_INST_RET_VOID_ABBREV) |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1591 | llvm_unreachable("Unexpected abbrev ordering!"); |
Chris Lattner | 94687ac | 2007-05-06 01:28:01 +0000 | [diff] [blame] | 1592 | } |
| 1593 | { // INST_RET abbrev for FUNCTION_BLOCK. |
| 1594 | BitCodeAbbrev *Abbv = new BitCodeAbbrev(); |
| 1595 | Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET)); |
| 1596 | Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ValID |
| 1597 | if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, |
| 1598 | Abbv) != FUNCTION_INST_RET_VAL_ABBREV) |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1599 | llvm_unreachable("Unexpected abbrev ordering!"); |
Chris Lattner | 94687ac | 2007-05-06 01:28:01 +0000 | [diff] [blame] | 1600 | } |
| 1601 | { // INST_UNREACHABLE abbrev for FUNCTION_BLOCK. |
| 1602 | BitCodeAbbrev *Abbv = new BitCodeAbbrev(); |
| 1603 | Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_UNREACHABLE)); |
| 1604 | if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, |
| 1605 | Abbv) != FUNCTION_INST_UNREACHABLE_ABBREV) |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1606 | llvm_unreachable("Unexpected abbrev ordering!"); |
Chris Lattner | 94687ac | 2007-05-06 01:28:01 +0000 | [diff] [blame] | 1607 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1608 | |
Chris Lattner | a0f1ecc | 2007-05-05 07:36:14 +0000 | [diff] [blame] | 1609 | Stream.ExitBlock(); |
| 1610 | } |
| 1611 | |
Chad Rosier | 32c5981 | 2011-12-07 22:49:05 +0000 | [diff] [blame] | 1612 | // Sort the Users based on the order in which the reader parses the bitcode |
| 1613 | // file. |
| 1614 | static bool bitcodereader_order(const User *lhs, const User *rhs) { |
| 1615 | // TODO: Implement. |
| 1616 | return true; |
| 1617 | } |
| 1618 | |
| 1619 | static void WriteUseList(const Value *V, const ValueEnumerator &VE, |
| 1620 | BitstreamWriter &Stream) { |
| 1621 | |
| 1622 | // One or zero uses can't get out of order. |
| 1623 | if (V->use_empty() || V->hasNUses(1)) |
| 1624 | return; |
| 1625 | |
| 1626 | // Make a copy of the in-memory use-list for sorting. |
| 1627 | unsigned UseListSize = std::distance(V->use_begin(), V->use_end()); |
| 1628 | SmallVector<const User*, 8> UseList; |
| 1629 | UseList.reserve(UseListSize); |
| 1630 | for (Value::const_use_iterator I = V->use_begin(), E = V->use_end(); |
| 1631 | I != E; ++I) { |
| 1632 | const User *U = *I; |
| 1633 | UseList.push_back(U); |
| 1634 | } |
| 1635 | |
| 1636 | // Sort the copy based on the order read by the BitcodeReader. |
| 1637 | std::sort(UseList.begin(), UseList.end(), bitcodereader_order); |
| 1638 | |
| 1639 | // TODO: Generate a diff between the BitcodeWriter in-memory use-list and the |
| 1640 | // sorted list (i.e., the expected BitcodeReader in-memory use-list). |
| 1641 | |
| 1642 | // TODO: Emit the USELIST_CODE_ENTRYs. |
| 1643 | } |
| 1644 | |
| 1645 | static void WriteFunctionUseList(const Function *F, ValueEnumerator &VE, |
| 1646 | BitstreamWriter &Stream) { |
| 1647 | VE.incorporateFunction(*F); |
| 1648 | |
| 1649 | for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end(); |
| 1650 | AI != AE; ++AI) |
| 1651 | WriteUseList(AI, VE, Stream); |
| 1652 | for (Function::const_iterator BB = F->begin(), FE = F->end(); BB != FE; |
| 1653 | ++BB) { |
| 1654 | WriteUseList(BB, VE, Stream); |
| 1655 | for (BasicBlock::const_iterator II = BB->begin(), IE = BB->end(); II != IE; |
| 1656 | ++II) { |
| 1657 | WriteUseList(II, VE, Stream); |
| 1658 | for (User::const_op_iterator OI = II->op_begin(), E = II->op_end(); |
| 1659 | OI != E; ++OI) { |
| 1660 | if ((isa<Constant>(*OI) && !isa<GlobalValue>(*OI)) || |
| 1661 | isa<InlineAsm>(*OI)) |
| 1662 | WriteUseList(*OI, VE, Stream); |
| 1663 | } |
| 1664 | } |
| 1665 | } |
| 1666 | VE.purgeFunction(); |
| 1667 | } |
| 1668 | |
Chad Rosier | cbbb096 | 2011-12-07 21:44:12 +0000 | [diff] [blame] | 1669 | // Emit use-lists. |
| 1670 | static void WriteModuleUseLists(const Module *M, ValueEnumerator &VE, |
| 1671 | BitstreamWriter &Stream) { |
| 1672 | Stream.EnterSubblock(bitc::USELIST_BLOCK_ID, 3); |
| 1673 | |
Chad Rosier | 32c5981 | 2011-12-07 22:49:05 +0000 | [diff] [blame] | 1674 | // XXX: this modifies the module, but in a way that should never change the |
| 1675 | // behavior of any pass or codegen in LLVM. The problem is that GVs may |
| 1676 | // contain entries in the use_list that do not exist in the Module and are |
| 1677 | // not stored in the .bc file. |
| 1678 | for (Module::const_global_iterator I = M->global_begin(), E = M->global_end(); |
| 1679 | I != E; ++I) |
| 1680 | I->removeDeadConstantUsers(); |
| 1681 | |
| 1682 | // Write the global variables. |
| 1683 | for (Module::const_global_iterator GI = M->global_begin(), |
| 1684 | GE = M->global_end(); GI != GE; ++GI) { |
| 1685 | WriteUseList(GI, VE, Stream); |
Chad Rosier | cbbb096 | 2011-12-07 21:44:12 +0000 | [diff] [blame] | 1686 | |
Chad Rosier | 32c5981 | 2011-12-07 22:49:05 +0000 | [diff] [blame] | 1687 | // Write the global variable initializers. |
| 1688 | if (GI->hasInitializer()) |
| 1689 | WriteUseList(GI->getInitializer(), VE, Stream); |
| 1690 | } |
| 1691 | |
| 1692 | // Write the functions. |
| 1693 | for (Module::const_iterator FI = M->begin(), FE = M->end(); FI != FE; ++FI) { |
| 1694 | WriteUseList(FI, VE, Stream); |
| 1695 | if (!FI->isDeclaration()) |
| 1696 | WriteFunctionUseList(FI, VE, Stream); |
| 1697 | } |
| 1698 | |
| 1699 | // Write the aliases. |
| 1700 | for (Module::const_alias_iterator AI = M->alias_begin(), AE = M->alias_end(); |
| 1701 | AI != AE; ++AI) { |
| 1702 | WriteUseList(AI, VE, Stream); |
| 1703 | WriteUseList(AI->getAliasee(), VE, Stream); |
| 1704 | } |
Chad Rosier | cbbb096 | 2011-12-07 21:44:12 +0000 | [diff] [blame] | 1705 | |
| 1706 | Stream.ExitBlock(); |
| 1707 | } |
Chris Lattner | a0f1ecc | 2007-05-05 07:36:14 +0000 | [diff] [blame] | 1708 | |
| 1709 | /// WriteModule - Emit the specified module to the bitstream. |
| 1710 | static void WriteModule(const Module *M, BitstreamWriter &Stream) { |
| 1711 | Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1712 | |
Chris Lattner | a0f1ecc | 2007-05-05 07:36:14 +0000 | [diff] [blame] | 1713 | // Emit the version number if it is non-zero. |
| 1714 | if (CurVersion) { |
| 1715 | SmallVector<unsigned, 1> Vals; |
| 1716 | Vals.push_back(CurVersion); |
| 1717 | Stream.EmitRecord(bitc::MODULE_CODE_VERSION, Vals); |
| 1718 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1719 | |
Chris Lattner | a0f1ecc | 2007-05-05 07:36:14 +0000 | [diff] [blame] | 1720 | // Analyze the module, enumerating globals, functions, etc. |
| 1721 | ValueEnumerator VE(M); |
| 1722 | |
| 1723 | // Emit blockinfo, which defines the standard abbreviations etc. |
| 1724 | WriteBlockInfo(VE, Stream); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1725 | |
Chris Lattner | a0f1ecc | 2007-05-05 07:36:14 +0000 | [diff] [blame] | 1726 | // Emit information about parameter attributes. |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 1727 | WriteAttributeTable(VE, Stream); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1728 | |
Chris Lattner | a0f1ecc | 2007-05-05 07:36:14 +0000 | [diff] [blame] | 1729 | // Emit information describing all of the types in the module. |
| 1730 | WriteTypeTable(VE, Stream); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1731 | |
Chris Lattner | a0f1ecc | 2007-05-05 07:36:14 +0000 | [diff] [blame] | 1732 | // Emit top-level description of module, including target triple, inline asm, |
| 1733 | // descriptors for global variables, and function prototype info. |
| 1734 | WriteModuleInfo(M, VE, Stream); |
Devang Patel | e54abc9 | 2009-07-22 17:43:22 +0000 | [diff] [blame] | 1735 | |
Devang Patel | 104cf9e | 2009-07-23 01:07:34 +0000 | [diff] [blame] | 1736 | // Emit constants. |
| 1737 | WriteModuleConstants(VE, Stream); |
| 1738 | |
Devang Patel | 912538b | 2009-08-04 05:01:35 +0000 | [diff] [blame] | 1739 | // Emit metadata. |
Dan Gohman | 17aa92c | 2010-07-21 23:38:33 +0000 | [diff] [blame] | 1740 | WriteModuleMetadata(M, VE, Stream); |
Devang Patel | 912538b | 2009-08-04 05:01:35 +0000 | [diff] [blame] | 1741 | |
Devang Patel | e8e0213 | 2009-09-18 19:26:43 +0000 | [diff] [blame] | 1742 | // Emit metadata. |
Chris Lattner | 7d05c46 | 2009-12-28 20:10:43 +0000 | [diff] [blame] | 1743 | WriteModuleMetadataStore(M, Stream); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1744 | |
Chris Lattner | a0f1ecc | 2007-05-05 07:36:14 +0000 | [diff] [blame] | 1745 | // Emit names for globals/functions etc. |
| 1746 | WriteValueSymbolTable(M->getValueSymbolTable(), VE, Stream); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1747 | |
Chad Rosier | cbbb096 | 2011-12-07 21:44:12 +0000 | [diff] [blame] | 1748 | // Emit use-lists. |
| 1749 | if (EnablePreserveUseListOrdering) |
| 1750 | WriteModuleUseLists(M, VE, Stream); |
| 1751 | |
Derek Schuff | 2ea9387 | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 1752 | // Emit function bodies. |
| 1753 | for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) |
| 1754 | if (!F->isDeclaration()) |
| 1755 | WriteFunction(*F, VE, Stream); |
| 1756 | |
Chris Lattner | 07faafc | 2007-05-04 18:26:27 +0000 | [diff] [blame] | 1757 | Stream.ExitBlock(); |
| 1758 | } |
| 1759 | |
Chris Lattner | 6fa6a32 | 2008-07-09 05:14:23 +0000 | [diff] [blame] | 1760 | /// EmitDarwinBCHeader - If generating a bc file on darwin, we have to emit a |
| 1761 | /// header and trailer to make it compatible with the system archiver. To do |
| 1762 | /// this we emit the following header, and then emit a trailer that pads the |
| 1763 | /// file out to be a multiple of 16 bytes. |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1764 | /// |
Chris Lattner | 6fa6a32 | 2008-07-09 05:14:23 +0000 | [diff] [blame] | 1765 | /// struct bc_header { |
| 1766 | /// uint32_t Magic; // 0x0B17C0DE |
| 1767 | /// uint32_t Version; // Version, currently always 0. |
| 1768 | /// uint32_t BitcodeOffset; // Offset to traditional bitcode file. |
| 1769 | /// uint32_t BitcodeSize; // Size of traditional bitcode file. |
| 1770 | /// uint32_t CPUType; // CPU specifier. |
| 1771 | /// ... potentially more later ... |
| 1772 | /// }; |
| 1773 | enum { |
| 1774 | DarwinBCSizeFieldOffset = 3*4, // Offset to bitcode_size. |
| 1775 | DarwinBCHeaderSize = 5*4 |
| 1776 | }; |
| 1777 | |
Daniel Dunbar | acbceea | 2012-02-29 20:31:09 +0000 | [diff] [blame] | 1778 | static void WriteInt32ToBuffer(uint32_t Value, SmallVectorImpl<char> &Buffer, |
Daniel Dunbar | 02a248a | 2012-02-29 20:31:01 +0000 | [diff] [blame] | 1779 | uint32_t &Position) { |
| 1780 | Buffer[Position + 0] = (unsigned char) (Value >> 0); |
| 1781 | Buffer[Position + 1] = (unsigned char) (Value >> 8); |
| 1782 | Buffer[Position + 2] = (unsigned char) (Value >> 16); |
| 1783 | Buffer[Position + 3] = (unsigned char) (Value >> 24); |
| 1784 | Position += 4; |
| 1785 | } |
| 1786 | |
Daniel Dunbar | acbceea | 2012-02-29 20:31:09 +0000 | [diff] [blame] | 1787 | static void EmitDarwinBCHeaderAndTrailer(SmallVectorImpl<char> &Buffer, |
Daniel Dunbar | 02a248a | 2012-02-29 20:31:01 +0000 | [diff] [blame] | 1788 | const Triple &TT) { |
Chris Lattner | 6fa6a32 | 2008-07-09 05:14:23 +0000 | [diff] [blame] | 1789 | unsigned CPUType = ~0U; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1790 | |
Evan Cheng | 0f8868b | 2010-02-12 20:13:44 +0000 | [diff] [blame] | 1791 | // Match x86_64-*, i[3-9]86-*, powerpc-*, powerpc64-*, arm-*, thumb-*, |
Evan Cheng | 3922a9b | 2010-02-12 20:39:35 +0000 | [diff] [blame] | 1792 | // armv[0-9]-*, thumbv[0-9]-*, armv5te-*, or armv6t2-*. The CPUType is a magic |
| 1793 | // number from /usr/include/mach/machine.h. It is ok to reproduce the |
| 1794 | // specific constants here because they are implicitly part of the Darwin ABI. |
Chris Lattner | 6fa6a32 | 2008-07-09 05:14:23 +0000 | [diff] [blame] | 1795 | enum { |
| 1796 | DARWIN_CPU_ARCH_ABI64 = 0x01000000, |
| 1797 | DARWIN_CPU_TYPE_X86 = 7, |
Evan Cheng | 0f8868b | 2010-02-12 20:13:44 +0000 | [diff] [blame] | 1798 | DARWIN_CPU_TYPE_ARM = 12, |
Chris Lattner | 6fa6a32 | 2008-07-09 05:14:23 +0000 | [diff] [blame] | 1799 | DARWIN_CPU_TYPE_POWERPC = 18 |
| 1800 | }; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1801 | |
Evan Cheng | e889c69 | 2011-06-14 01:51:33 +0000 | [diff] [blame] | 1802 | Triple::ArchType Arch = TT.getArch(); |
| 1803 | if (Arch == Triple::x86_64) |
Chris Lattner | 6fa6a32 | 2008-07-09 05:14:23 +0000 | [diff] [blame] | 1804 | CPUType = DARWIN_CPU_TYPE_X86 | DARWIN_CPU_ARCH_ABI64; |
Evan Cheng | e889c69 | 2011-06-14 01:51:33 +0000 | [diff] [blame] | 1805 | else if (Arch == Triple::x86) |
Chris Lattner | 6fa6a32 | 2008-07-09 05:14:23 +0000 | [diff] [blame] | 1806 | CPUType = DARWIN_CPU_TYPE_X86; |
Evan Cheng | e889c69 | 2011-06-14 01:51:33 +0000 | [diff] [blame] | 1807 | else if (Arch == Triple::ppc) |
Chris Lattner | 6fa6a32 | 2008-07-09 05:14:23 +0000 | [diff] [blame] | 1808 | CPUType = DARWIN_CPU_TYPE_POWERPC; |
Evan Cheng | e889c69 | 2011-06-14 01:51:33 +0000 | [diff] [blame] | 1809 | else if (Arch == Triple::ppc64) |
Chris Lattner | 6fa6a32 | 2008-07-09 05:14:23 +0000 | [diff] [blame] | 1810 | CPUType = DARWIN_CPU_TYPE_POWERPC | DARWIN_CPU_ARCH_ABI64; |
Evan Cheng | e889c69 | 2011-06-14 01:51:33 +0000 | [diff] [blame] | 1811 | else if (Arch == Triple::arm || Arch == Triple::thumb) |
Evan Cheng | 0f8868b | 2010-02-12 20:13:44 +0000 | [diff] [blame] | 1812 | CPUType = DARWIN_CPU_TYPE_ARM; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1813 | |
Chris Lattner | 6fa6a32 | 2008-07-09 05:14:23 +0000 | [diff] [blame] | 1814 | // Traditional Bitcode starts after header. |
Daniel Dunbar | 02a248a | 2012-02-29 20:31:01 +0000 | [diff] [blame] | 1815 | assert(Buffer.size() >= DarwinBCHeaderSize && |
| 1816 | "Expected header size to be reserved"); |
Chris Lattner | 6fa6a32 | 2008-07-09 05:14:23 +0000 | [diff] [blame] | 1817 | unsigned BCOffset = DarwinBCHeaderSize; |
Daniel Dunbar | 02a248a | 2012-02-29 20:31:01 +0000 | [diff] [blame] | 1818 | unsigned BCSize = Buffer.size()-DarwinBCHeaderSize; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1819 | |
Daniel Dunbar | 02a248a | 2012-02-29 20:31:01 +0000 | [diff] [blame] | 1820 | // Write the magic and version. |
| 1821 | unsigned Position = 0; |
| 1822 | WriteInt32ToBuffer(0x0B17C0DE , Buffer, Position); |
| 1823 | WriteInt32ToBuffer(0 , Buffer, Position); // Version. |
| 1824 | WriteInt32ToBuffer(BCOffset , Buffer, Position); |
| 1825 | WriteInt32ToBuffer(BCSize , Buffer, Position); |
| 1826 | WriteInt32ToBuffer(CPUType , Buffer, Position); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1827 | |
Chris Lattner | 6fa6a32 | 2008-07-09 05:14:23 +0000 | [diff] [blame] | 1828 | // If the file is not a multiple of 16 bytes, insert dummy padding. |
Daniel Dunbar | 02a248a | 2012-02-29 20:31:01 +0000 | [diff] [blame] | 1829 | while (Buffer.size() & 15) |
| 1830 | Buffer.push_back(0); |
Chris Lattner | 6fa6a32 | 2008-07-09 05:14:23 +0000 | [diff] [blame] | 1831 | } |
| 1832 | |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 1833 | /// WriteBitcodeToFile - Write the specified module to the specified output |
| 1834 | /// stream. |
Daniel Dunbar | d1ce3b4 | 2008-10-22 17:39:14 +0000 | [diff] [blame] | 1835 | void llvm::WriteBitcodeToFile(const Module *M, raw_ostream &Out) { |
Daniel Dunbar | acbceea | 2012-02-29 20:31:09 +0000 | [diff] [blame] | 1836 | SmallVector<char, 1024> Buffer; |
Chris Lattner | fd57cec | 2007-04-22 06:24:45 +0000 | [diff] [blame] | 1837 | Buffer.reserve(256*1024); |
Chris Lattner | 38e7721 | 2008-12-19 18:37:59 +0000 | [diff] [blame] | 1838 | |
Daniel Dunbar | 02a248a | 2012-02-29 20:31:01 +0000 | [diff] [blame] | 1839 | // If this is darwin or another generic macho target, reserve space for the |
| 1840 | // header. |
| 1841 | Triple TT(M->getTargetTriple()); |
| 1842 | if (TT.isOSDarwin()) |
| 1843 | Buffer.insert(Buffer.begin(), DarwinBCHeaderSize, 0); |
| 1844 | |
| 1845 | // Emit the module into the buffer. |
| 1846 | { |
| 1847 | BitstreamWriter Stream(Buffer); |
| 1848 | |
| 1849 | // Emit the file header. |
| 1850 | Stream.Emit((unsigned)'B', 8); |
| 1851 | Stream.Emit((unsigned)'C', 8); |
| 1852 | Stream.Emit(0x0, 4); |
| 1853 | Stream.Emit(0xC, 4); |
| 1854 | Stream.Emit(0xE, 4); |
| 1855 | Stream.Emit(0xD, 4); |
| 1856 | |
| 1857 | // Emit the module. |
| 1858 | WriteModule(M, Stream); |
| 1859 | } |
| 1860 | |
| 1861 | if (TT.isOSDarwin()) |
| 1862 | EmitDarwinBCHeaderAndTrailer(Buffer, TT); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1863 | |
Chris Lattner | 38e7721 | 2008-12-19 18:37:59 +0000 | [diff] [blame] | 1864 | // Write the generated bitstream to "Out". |
| 1865 | Out.write((char*)&Buffer.front(), Buffer.size()); |
Chris Lattner | 38e7721 | 2008-12-19 18:37:59 +0000 | [diff] [blame] | 1866 | } |