blob: 05f7e08555fb00650bed16a337ccee4a7894c4a0 [file] [log] [blame]
Chris Lattnerf7e79482002-04-07 22:31:46 +00001//===-- AsmWriter.cpp - Printing LLVM as an assembly file -----------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
Chandler Carruth9aca9182014-01-07 12:34:26 +000010// This library implements the functionality defined in llvm/IR/Writer.h
Chris Lattner2f7c9632001-06-06 20:29:01 +000011//
Chris Lattner189088e2002-04-12 18:21:53 +000012// Note that these routines must be extremely tolerant of various errors in the
Chris Lattnerf70da102003-05-08 02:44:12 +000013// LLVM code, because it can be used for debugging transformations.
Chris Lattner189088e2002-04-12 18:21:53 +000014//
Chris Lattner2f7c9632001-06-06 20:29:01 +000015//===----------------------------------------------------------------------===//
16
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/ADT/DenseMap.h"
18#include "llvm/ADT/STLExtras.h"
Benjamin Kramerba05a782015-03-17 19:53:41 +000019#include "llvm/ADT/SetVector.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/ADT/SmallString.h"
21#include "llvm/ADT/StringExtras.h"
Chandler Carruth9aca9182014-01-07 12:34:26 +000022#include "llvm/IR/AssemblyAnnotationWriter.h"
Chandler Carruth1305dc32014-03-04 11:45:46 +000023#include "llvm/IR/CFG.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000024#include "llvm/IR/CallingConv.h"
25#include "llvm/IR/Constants.h"
Chandler Carruth9a4c9e52014-03-06 00:46:21 +000026#include "llvm/IR/DebugInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000027#include "llvm/IR/DerivedTypes.h"
Chandler Carruthb8ddc702014-01-12 11:10:32 +000028#include "llvm/IR/IRPrintingPasses.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000029#include "llvm/IR/InlineAsm.h"
30#include "llvm/IR/IntrinsicInst.h"
31#include "llvm/IR/LLVMContext.h"
32#include "llvm/IR/Module.h"
33#include "llvm/IR/Operator.h"
Igor Laevsky2aa8caf2015-05-05 13:20:42 +000034#include "llvm/IR/Statepoint.h"
Chandler Carruthdcb603f2013-01-07 15:43:51 +000035#include "llvm/IR/TypeFinder.h"
Benjamin Kramerba05a782015-03-17 19:53:41 +000036#include "llvm/IR/UseListOrder.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000037#include "llvm/IR/ValueSymbolTable.h"
David Greenec7f9b122010-01-05 01:29:26 +000038#include "llvm/Support/Debug.h"
Devang Patel711ab5b2009-09-30 20:16:54 +000039#include "llvm/Support/Dwarf.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000040#include "llvm/Support/ErrorHandling.h"
Dan Gohmane2745262009-08-12 17:23:50 +000041#include "llvm/Support/FormattedStream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000042#include "llvm/Support/MathExtras.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000043#include "llvm/Support/raw_ostream.h"
Chris Lattnerfee714f2001-09-07 16:36:04 +000044#include <algorithm>
Reid Spencerbdf03b42007-05-22 19:27:35 +000045#include <cctype>
Chris Lattner189d19f2003-11-21 20:23:48 +000046using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000047
Reid Spencer294715b2005-05-15 16:13:11 +000048// Make virtual table appear in this compilation unit.
49AssemblyAnnotationWriter::~AssemblyAnnotationWriter() {}
50
Chris Lattner3eee99c2008-08-19 04:36:02 +000051//===----------------------------------------------------------------------===//
52// Helper Functions
53//===----------------------------------------------------------------------===//
54
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +000055namespace {
56struct OrderMap {
57 DenseMap<const Value *, std::pair<unsigned, bool>> IDs;
58
59 unsigned size() const { return IDs.size(); }
60 std::pair<unsigned, bool> &operator[](const Value *V) { return IDs[V]; }
61 std::pair<unsigned, bool> lookup(const Value *V) const {
62 return IDs.lookup(V);
63 }
64 void index(const Value *V) {
65 // Explicitly sequence get-size and insert-value operations to avoid UB.
66 unsigned ID = IDs.size() + 1;
67 IDs[V].first = ID;
68 }
69};
70}
71
72static void orderValue(const Value *V, OrderMap &OM) {
73 if (OM.lookup(V).first)
74 return;
75
76 if (const Constant *C = dyn_cast<Constant>(V))
77 if (C->getNumOperands() && !isa<GlobalValue>(C))
78 for (const Value *Op : C->operands())
79 if (!isa<BasicBlock>(Op) && !isa<GlobalValue>(Op))
80 orderValue(Op, OM);
81
82 // Note: we cannot cache this lookup above, since inserting into the map
83 // changes the map's size, and thus affects the other IDs.
84 OM.index(V);
85}
86
87static OrderMap orderModule(const Module *M) {
88 // This needs to match the order used by ValueEnumerator::ValueEnumerator()
89 // and ValueEnumerator::incorporateFunction().
90 OrderMap OM;
91
92 for (const GlobalVariable &G : M->globals()) {
93 if (G.hasInitializer())
94 if (!isa<GlobalValue>(G.getInitializer()))
95 orderValue(G.getInitializer(), OM);
96 orderValue(&G, OM);
97 }
98 for (const GlobalAlias &A : M->aliases()) {
99 if (!isa<GlobalValue>(A.getAliasee()))
100 orderValue(A.getAliasee(), OM);
101 orderValue(&A, OM);
102 }
103 for (const Function &F : *M) {
104 if (F.hasPrefixData())
105 if (!isa<GlobalValue>(F.getPrefixData()))
106 orderValue(F.getPrefixData(), OM);
Peter Collingbourne51d2de72014-12-03 02:08:38 +0000107
108 if (F.hasPrologueData())
109 if (!isa<GlobalValue>(F.getPrologueData()))
110 orderValue(F.getPrologueData(), OM);
111
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +0000112 orderValue(&F, OM);
113
114 if (F.isDeclaration())
115 continue;
116
117 for (const Argument &A : F.args())
118 orderValue(&A, OM);
119 for (const BasicBlock &BB : F) {
120 orderValue(&BB, OM);
121 for (const Instruction &I : BB) {
122 for (const Value *Op : I.operands())
123 if ((isa<Constant>(*Op) && !isa<GlobalValue>(*Op)) ||
124 isa<InlineAsm>(*Op))
125 orderValue(Op, OM);
126 orderValue(&I, OM);
127 }
128 }
129 }
130 return OM;
131}
132
133static void predictValueUseListOrderImpl(const Value *V, const Function *F,
134 unsigned ID, const OrderMap &OM,
135 UseListOrderStack &Stack) {
136 // Predict use-list order for this one.
137 typedef std::pair<const Use *, unsigned> Entry;
138 SmallVector<Entry, 64> List;
139 for (const Use &U : V->uses())
140 // Check if this user will be serialized.
141 if (OM.lookup(U.getUser()).first)
142 List.push_back(std::make_pair(&U, List.size()));
143
144 if (List.size() < 2)
145 // We may have lost some users.
146 return;
147
148 bool GetsReversed =
149 !isa<GlobalVariable>(V) && !isa<Function>(V) && !isa<BasicBlock>(V);
150 if (auto *BA = dyn_cast<BlockAddress>(V))
151 ID = OM.lookup(BA->getBasicBlock()).first;
152 std::sort(List.begin(), List.end(), [&](const Entry &L, const Entry &R) {
153 const Use *LU = L.first;
154 const Use *RU = R.first;
155 if (LU == RU)
156 return false;
157
158 auto LID = OM.lookup(LU->getUser()).first;
159 auto RID = OM.lookup(RU->getUser()).first;
160
161 // If ID is 4, then expect: 7 6 5 1 2 3.
162 if (LID < RID) {
163 if (GetsReversed)
164 if (RID <= ID)
165 return true;
166 return false;
167 }
168 if (RID < LID) {
169 if (GetsReversed)
170 if (LID <= ID)
171 return false;
172 return true;
173 }
174
175 // LID and RID are equal, so we have different operands of the same user.
176 // Assume operands are added in order for all instructions.
177 if (GetsReversed)
178 if (LID <= ID)
179 return LU->getOperandNo() < RU->getOperandNo();
180 return LU->getOperandNo() > RU->getOperandNo();
181 });
182
183 if (std::is_sorted(
184 List.begin(), List.end(),
185 [](const Entry &L, const Entry &R) { return L.second < R.second; }))
186 // Order is already correct.
187 return;
188
189 // Store the shuffle.
190 Stack.emplace_back(V, F, List.size());
191 assert(List.size() == Stack.back().Shuffle.size() && "Wrong size");
192 for (size_t I = 0, E = List.size(); I != E; ++I)
193 Stack.back().Shuffle[I] = List[I].second;
194}
195
196static void predictValueUseListOrder(const Value *V, const Function *F,
197 OrderMap &OM, UseListOrderStack &Stack) {
198 auto &IDPair = OM[V];
199 assert(IDPair.first && "Unmapped value");
200 if (IDPair.second)
201 // Already predicted.
202 return;
203
204 // Do the actual prediction.
205 IDPair.second = true;
206 if (!V->use_empty() && std::next(V->use_begin()) != V->use_end())
207 predictValueUseListOrderImpl(V, F, IDPair.first, OM, Stack);
208
209 // Recursive descent into constants.
210 if (const Constant *C = dyn_cast<Constant>(V))
211 if (C->getNumOperands()) // Visit GlobalValues.
212 for (const Value *Op : C->operands())
213 if (isa<Constant>(Op)) // Visit GlobalValues.
214 predictValueUseListOrder(Op, F, OM, Stack);
215}
216
217static UseListOrderStack predictUseListOrder(const Module *M) {
218 OrderMap OM = orderModule(M);
219
220 // Use-list orders need to be serialized after all the users have been added
221 // to a value, or else the shuffles will be incomplete. Store them per
222 // function in a stack.
223 //
224 // Aside from function order, the order of values doesn't matter much here.
225 UseListOrderStack Stack;
226
227 // We want to visit the functions backward now so we can list function-local
228 // constants in the last Function they're used in. Module-level constants
229 // have already been visited above.
230 for (auto I = M->rbegin(), E = M->rend(); I != E; ++I) {
231 const Function &F = *I;
232 if (F.isDeclaration())
233 continue;
234 for (const BasicBlock &BB : F)
235 predictValueUseListOrder(&BB, &F, OM, Stack);
236 for (const Argument &A : F.args())
237 predictValueUseListOrder(&A, &F, OM, Stack);
238 for (const BasicBlock &BB : F)
239 for (const Instruction &I : BB)
240 for (const Value *Op : I.operands())
241 if (isa<Constant>(*Op) || isa<InlineAsm>(*Op)) // Visit GlobalValues.
242 predictValueUseListOrder(Op, &F, OM, Stack);
243 for (const BasicBlock &BB : F)
244 for (const Instruction &I : BB)
245 predictValueUseListOrder(&I, &F, OM, Stack);
246 }
247
248 // Visit globals last.
249 for (const GlobalVariable &G : M->globals())
250 predictValueUseListOrder(&G, nullptr, OM, Stack);
251 for (const Function &F : *M)
252 predictValueUseListOrder(&F, nullptr, OM, Stack);
253 for (const GlobalAlias &A : M->aliases())
254 predictValueUseListOrder(&A, nullptr, OM, Stack);
255 for (const GlobalVariable &G : M->globals())
256 if (G.hasInitializer())
257 predictValueUseListOrder(G.getInitializer(), nullptr, OM, Stack);
258 for (const GlobalAlias &A : M->aliases())
259 predictValueUseListOrder(A.getAliasee(), nullptr, OM, Stack);
260 for (const Function &F : *M)
261 if (F.hasPrefixData())
262 predictValueUseListOrder(F.getPrefixData(), nullptr, OM, Stack);
263
264 return Stack;
265}
266
Chris Lattner3eee99c2008-08-19 04:36:02 +0000267static const Module *getModuleFromVal(const Value *V) {
268 if (const Argument *MA = dyn_cast<Argument>(V))
Craig Topperc6207612014-04-09 06:08:46 +0000269 return MA->getParent() ? MA->getParent()->getParent() : nullptr;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000270
Chris Lattner3eee99c2008-08-19 04:36:02 +0000271 if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
Craig Topperc6207612014-04-09 06:08:46 +0000272 return BB->getParent() ? BB->getParent()->getParent() : nullptr;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000273
Chris Lattner3eee99c2008-08-19 04:36:02 +0000274 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Craig Topperc6207612014-04-09 06:08:46 +0000275 const Function *M = I->getParent() ? I->getParent()->getParent() : nullptr;
276 return M ? M->getParent() : nullptr;
Chris Lattner3eee99c2008-08-19 04:36:02 +0000277 }
Andrew Trickec4b6e72011-09-30 19:48:58 +0000278
Chris Lattner3eee99c2008-08-19 04:36:02 +0000279 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
280 return GV->getParent();
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000281
282 if (const auto *MAV = dyn_cast<MetadataAsValue>(V)) {
283 for (const User *U : MAV->users())
284 if (isa<Instruction>(U))
285 if (const Module *M = getModuleFromVal(U))
286 return M;
287 return nullptr;
288 }
289
Craig Topperc6207612014-04-09 06:08:46 +0000290 return nullptr;
Chris Lattner3eee99c2008-08-19 04:36:02 +0000291}
292
Bill Wendling90bc19c2013-02-20 07:21:42 +0000293static void PrintCallingConv(unsigned cc, raw_ostream &Out) {
Micah Villmow857186d2012-09-13 15:11:12 +0000294 switch (cc) {
Bill Wendling90bc19c2013-02-20 07:21:42 +0000295 default: Out << "cc" << cc; break;
296 case CallingConv::Fast: Out << "fastcc"; break;
297 case CallingConv::Cold: Out << "coldcc"; break;
Andrew Trick5ae6ed82013-11-11 22:40:22 +0000298 case CallingConv::WebKit_JS: Out << "webkit_jscc"; break;
299 case CallingConv::AnyReg: Out << "anyregcc"; break;
Juergen Ributzkae6250132014-01-17 19:47:03 +0000300 case CallingConv::PreserveMost: Out << "preserve_mostcc"; break;
301 case CallingConv::PreserveAll: Out << "preserve_allcc"; break;
Reid Kleckner35fc3632014-12-01 21:04:44 +0000302 case CallingConv::GHC: Out << "ghccc"; break;
Bill Wendling90bc19c2013-02-20 07:21:42 +0000303 case CallingConv::X86_StdCall: Out << "x86_stdcallcc"; break;
304 case CallingConv::X86_FastCall: Out << "x86_fastcallcc"; break;
305 case CallingConv::X86_ThisCall: Out << "x86_thiscallcc"; break;
Reid Kleckner9ccce992014-10-28 01:29:26 +0000306 case CallingConv::X86_VectorCall:Out << "x86_vectorcallcc"; break;
Bill Wendling90bc19c2013-02-20 07:21:42 +0000307 case CallingConv::Intel_OCL_BI: Out << "intel_ocl_bicc"; break;
308 case CallingConv::ARM_APCS: Out << "arm_apcscc"; break;
309 case CallingConv::ARM_AAPCS: Out << "arm_aapcscc"; break;
310 case CallingConv::ARM_AAPCS_VFP: Out << "arm_aapcs_vfpcc"; break;
311 case CallingConv::MSP430_INTR: Out << "msp430_intrcc"; break;
312 case CallingConv::PTX_Kernel: Out << "ptx_kernel"; break;
313 case CallingConv::PTX_Device: Out << "ptx_device"; break;
Charles Davise8f297c2013-07-12 06:02:35 +0000314 case CallingConv::X86_64_SysV: Out << "x86_64_sysvcc"; break;
315 case CallingConv::X86_64_Win64: Out << "x86_64_win64cc"; break;
Michael Kupersteine31b4862013-12-15 10:01:20 +0000316 case CallingConv::SPIR_FUNC: Out << "spir_func"; break;
317 case CallingConv::SPIR_KERNEL: Out << "spir_kernel"; break;
Micah Villmow857186d2012-09-13 15:11:12 +0000318 }
319}
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000320
Daniel Dunbardb0b70a2008-10-28 19:33:02 +0000321// PrintEscapedString - Print each character of the specified string, escaping
322// it if it is not printable or if it is an escape char.
Chris Lattner9380b812010-07-07 23:16:37 +0000323static void PrintEscapedString(StringRef Name, raw_ostream &Out) {
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000324 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
325 unsigned char C = Name[i];
Nick Lewycky5aa592a2009-03-15 06:39:52 +0000326 if (isprint(C) && C != '\\' && C != '"')
Daniel Dunbardb0b70a2008-10-28 19:33:02 +0000327 Out << C;
328 else
329 Out << '\\' << hexdigit(C >> 4) << hexdigit(C & 0x0F);
330 }
331}
332
Chris Lattner3eee99c2008-08-19 04:36:02 +0000333enum PrefixType {
334 GlobalPrefix,
David Majnemerdad0a642014-06-27 18:19:56 +0000335 ComdatPrefix,
Chris Lattner3eee99c2008-08-19 04:36:02 +0000336 LabelPrefix,
Daniel Dunbar389529a2008-10-14 23:28:09 +0000337 LocalPrefix,
338 NoPrefix
Chris Lattner3eee99c2008-08-19 04:36:02 +0000339};
340
341/// PrintLLVMName - Turn the specified name into an 'LLVM name', which is either
342/// prefixed with % (if the string only contains simple characters) or is
343/// surrounded with ""'s (if it has special chars in it). Print it out.
Benjamin Kramer92d89982010-07-14 22:38:02 +0000344static void PrintLLVMName(raw_ostream &OS, StringRef Name, PrefixType Prefix) {
Jay Foadf7adb322011-04-24 14:30:00 +0000345 assert(!Name.empty() && "Cannot get empty name!");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000346 switch (Prefix) {
Daniel Dunbar389529a2008-10-14 23:28:09 +0000347 case NoPrefix: break;
Chris Lattner1508d3f2008-08-19 05:16:28 +0000348 case GlobalPrefix: OS << '@'; break;
David Majnemerdad0a642014-06-27 18:19:56 +0000349 case ComdatPrefix: OS << '$'; break;
Chris Lattner1508d3f2008-08-19 05:16:28 +0000350 case LabelPrefix: break;
351 case LocalPrefix: OS << '%'; break;
Nick Lewycky063699a2009-03-19 06:31:22 +0000352 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000353
Chris Lattner3eee99c2008-08-19 04:36:02 +0000354 // Scan the name to see if it needs quotes first.
Guy Benyei83c74e92013-02-12 21:21:59 +0000355 bool NeedsQuotes = isdigit(static_cast<unsigned char>(Name[0]));
Chris Lattner3eee99c2008-08-19 04:36:02 +0000356 if (!NeedsQuotes) {
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000357 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
Aaron Ballmaned9b0a92012-07-16 16:18:18 +0000358 // By making this unsigned, the value passed in to isalnum will always be
359 // in the range 0-255. This is important when building with MSVC because
360 // its implementation will assert. This situation can arise when dealing
361 // with UTF-8 multibyte characters.
362 unsigned char C = Name[i];
Guy Benyei83c74e92013-02-12 21:21:59 +0000363 if (!isalnum(static_cast<unsigned char>(C)) && C != '-' && C != '.' &&
364 C != '_') {
Chris Lattner3eee99c2008-08-19 04:36:02 +0000365 NeedsQuotes = true;
366 break;
367 }
368 }
369 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000370
Chris Lattner3eee99c2008-08-19 04:36:02 +0000371 // If we didn't need any quotes, just write out the name in one blast.
372 if (!NeedsQuotes) {
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000373 OS << Name;
Chris Lattner3eee99c2008-08-19 04:36:02 +0000374 return;
375 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000376
Chris Lattner3eee99c2008-08-19 04:36:02 +0000377 // Okay, we need quotes. Output the quotes and escape any scary characters as
378 // needed.
379 OS << '"';
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000380 PrintEscapedString(Name, OS);
Chris Lattner3eee99c2008-08-19 04:36:02 +0000381 OS << '"';
382}
383
384/// PrintLLVMName - Turn the specified name into an 'LLVM name', which is either
385/// prefixed with % (if the string only contains simple characters) or is
386/// surrounded with ""'s (if it has special chars in it). Print it out.
Dan Gohman12dad632009-08-12 20:56:03 +0000387static void PrintLLVMName(raw_ostream &OS, const Value *V) {
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000388 PrintLLVMName(OS, V->getName(),
Chris Lattner3eee99c2008-08-19 04:36:02 +0000389 isa<GlobalValue>(V) ? GlobalPrefix : LocalPrefix);
390}
391
Chris Lattner0f578952009-02-28 20:25:14 +0000392
Benjamin Kramerba05a782015-03-17 19:53:41 +0000393namespace {
394class TypePrinting {
395 TypePrinting(const TypePrinting &) = delete;
396 void operator=(const TypePrinting&) = delete;
397public:
398
399 /// NamedTypes - The named types that are used by the current module.
400 TypeFinder NamedTypes;
401
402 /// NumberedTypes - The numbered types, along with their value.
403 DenseMap<StructType*, unsigned> NumberedTypes;
404
Benjamin Kramerdd0ff852015-04-11 15:32:26 +0000405 TypePrinting() = default;
Benjamin Kramerba05a782015-03-17 19:53:41 +0000406
407 void incorporateTypes(const Module &M);
408
409 void print(Type *Ty, raw_ostream &OS);
410
411 void printStructBody(StructType *Ty, raw_ostream &OS);
412};
413} // namespace
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000414
415void TypePrinting::incorporateTypes(const Module &M) {
Bill Wendling8555a372012-08-03 00:30:35 +0000416 NamedTypes.run(M, false);
Andrew Trickec4b6e72011-09-30 19:48:58 +0000417
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000418 // The list of struct types we got back includes all the struct types, split
419 // the unnamed ones out to a numbering and remove the anonymous structs.
420 unsigned NextNumber = 0;
Andrew Trickec4b6e72011-09-30 19:48:58 +0000421
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000422 std::vector<StructType*>::iterator NextToUse = NamedTypes.begin(), I, E;
423 for (I = NamedTypes.begin(), E = NamedTypes.end(); I != E; ++I) {
424 StructType *STy = *I;
Andrew Trickec4b6e72011-09-30 19:48:58 +0000425
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000426 // Ignore anonymous types.
Chris Lattner01beceb2011-08-12 18:07:07 +0000427 if (STy->isLiteral())
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000428 continue;
Andrew Trickec4b6e72011-09-30 19:48:58 +0000429
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000430 if (STy->getName().empty())
431 NumberedTypes[STy] = NextNumber++;
432 else
433 *NextToUse++ = STy;
434 }
Andrew Trickec4b6e72011-09-30 19:48:58 +0000435
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000436 NamedTypes.erase(NextToUse, NamedTypes.end());
437}
438
439
Chris Lattner40959d02009-02-28 20:49:40 +0000440/// CalcTypeName - Write the specified type to the specified raw_ostream, making
441/// use of type names or up references to shorten the type name where possible.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000442void TypePrinting::print(Type *Ty, raw_ostream &OS) {
Chris Lattner0f578952009-02-28 20:25:14 +0000443 switch (Ty->getTypeID()) {
Rafael Espindolaba7df702013-12-07 02:27:52 +0000444 case Type::VoidTyID: OS << "void"; return;
445 case Type::HalfTyID: OS << "half"; return;
446 case Type::FloatTyID: OS << "float"; return;
447 case Type::DoubleTyID: OS << "double"; return;
448 case Type::X86_FP80TyID: OS << "x86_fp80"; return;
449 case Type::FP128TyID: OS << "fp128"; return;
450 case Type::PPC_FP128TyID: OS << "ppc_fp128"; return;
451 case Type::LabelTyID: OS << "label"; return;
452 case Type::MetadataTyID: OS << "metadata"; return;
453 case Type::X86_MMXTyID: OS << "x86_mmx"; return;
Chris Lattner68318b12009-02-28 21:18:43 +0000454 case Type::IntegerTyID:
Chris Lattner06a23212009-02-28 21:27:31 +0000455 OS << 'i' << cast<IntegerType>(Ty)->getBitWidth();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000456 return;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000457
Chris Lattner07d88292009-02-28 20:35:42 +0000458 case Type::FunctionTyID: {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000459 FunctionType *FTy = cast<FunctionType>(Ty);
460 print(FTy->getReturnType(), OS);
Chris Lattner06a23212009-02-28 21:27:31 +0000461 OS << " (";
Chris Lattner07d88292009-02-28 20:35:42 +0000462 for (FunctionType::param_iterator I = FTy->param_begin(),
463 E = FTy->param_end(); I != E; ++I) {
464 if (I != FTy->param_begin())
Chris Lattner06a23212009-02-28 21:27:31 +0000465 OS << ", ";
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000466 print(*I, OS);
Chris Lattner0f578952009-02-28 20:25:14 +0000467 }
Chris Lattner07d88292009-02-28 20:35:42 +0000468 if (FTy->isVarArg()) {
Chris Lattner06a23212009-02-28 21:27:31 +0000469 if (FTy->getNumParams()) OS << ", ";
470 OS << "...";
Chris Lattner0f578952009-02-28 20:25:14 +0000471 }
Chris Lattner06a23212009-02-28 21:27:31 +0000472 OS << ')';
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000473 return;
Chris Lattner07d88292009-02-28 20:35:42 +0000474 }
475 case Type::StructTyID: {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000476 StructType *STy = cast<StructType>(Ty);
Andrew Trickec4b6e72011-09-30 19:48:58 +0000477
Chris Lattner01beceb2011-08-12 18:07:07 +0000478 if (STy->isLiteral())
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000479 return printStructBody(STy, OS);
480
481 if (!STy->getName().empty())
482 return PrintLLVMName(OS, STy->getName(), LocalPrefix);
Andrew Trickec4b6e72011-09-30 19:48:58 +0000483
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000484 DenseMap<StructType*, unsigned>::iterator I = NumberedTypes.find(STy);
485 if (I != NumberedTypes.end())
486 OS << '%' << I->second;
487 else // Not enumerated, print the hex address.
Benjamin Kramer4e8b4632011-11-02 17:24:36 +0000488 OS << "%\"type " << STy << '\"';
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000489 return;
Chris Lattner07d88292009-02-28 20:35:42 +0000490 }
491 case Type::PointerTyID: {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000492 PointerType *PTy = cast<PointerType>(Ty);
493 print(PTy->getElementType(), OS);
Chris Lattner07d88292009-02-28 20:35:42 +0000494 if (unsigned AddressSpace = PTy->getAddressSpace())
Chris Lattner06a23212009-02-28 21:27:31 +0000495 OS << " addrspace(" << AddressSpace << ')';
496 OS << '*';
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000497 return;
Chris Lattner07d88292009-02-28 20:35:42 +0000498 }
499 case Type::ArrayTyID: {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000500 ArrayType *ATy = cast<ArrayType>(Ty);
Chris Lattner06a23212009-02-28 21:27:31 +0000501 OS << '[' << ATy->getNumElements() << " x ";
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000502 print(ATy->getElementType(), OS);
Chris Lattner06a23212009-02-28 21:27:31 +0000503 OS << ']';
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000504 return;
Chris Lattner07d88292009-02-28 20:35:42 +0000505 }
506 case Type::VectorTyID: {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000507 VectorType *PTy = cast<VectorType>(Ty);
Chris Lattner06a23212009-02-28 21:27:31 +0000508 OS << "<" << PTy->getNumElements() << " x ";
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000509 print(PTy->getElementType(), OS);
Chris Lattner06a23212009-02-28 21:27:31 +0000510 OS << '>';
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000511 return;
Chris Lattner07d88292009-02-28 20:35:42 +0000512 }
Chris Lattner0f578952009-02-28 20:25:14 +0000513 }
Rafael Espindolaba7df702013-12-07 02:27:52 +0000514 llvm_unreachable("Invalid TypeID");
Chris Lattner0f578952009-02-28 20:25:14 +0000515}
516
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000517void TypePrinting::printStructBody(StructType *STy, raw_ostream &OS) {
518 if (STy->isOpaque()) {
519 OS << "opaque";
520 return;
Chris Lattner0f578952009-02-28 20:25:14 +0000521 }
Andrew Trickec4b6e72011-09-30 19:48:58 +0000522
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000523 if (STy->isPacked())
524 OS << '<';
Andrew Trickec4b6e72011-09-30 19:48:58 +0000525
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000526 if (STy->getNumElements() == 0) {
527 OS << "{}";
528 } else {
529 StructType::element_iterator I = STy->element_begin();
530 OS << "{ ";
531 print(*I++, OS);
532 for (StructType::element_iterator E = STy->element_end(); I != E; ++I) {
533 OS << ", ";
534 print(*I, OS);
Chris Lattner3243ea12009-03-01 00:03:38 +0000535 }
Andrew Trickec4b6e72011-09-30 19:48:58 +0000536
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000537 OS << " }";
Chris Lattner92c5c122009-02-28 23:20:19 +0000538 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000539 if (STy->isPacked())
540 OS << '>';
Chris Lattner92c5c122009-02-28 23:20:19 +0000541}
542
Benjamin Kramerba05a782015-03-17 19:53:41 +0000543namespace {
Chris Lattner3eee99c2008-08-19 04:36:02 +0000544//===----------------------------------------------------------------------===//
545// SlotTracker Class: Enumerate slot numbers for unnamed values
546//===----------------------------------------------------------------------===//
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000547/// This class provides computation of slot numbers for LLVM Assembly writing.
Chris Lattner393b7cd2008-08-17 04:17:45 +0000548///
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000549class SlotTracker {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000550public:
Devang Patel983c6b12009-07-08 21:44:25 +0000551 /// ValueMap - A mapping of Values to slot numbers.
Chris Lattnera204d412008-08-17 17:25:25 +0000552 typedef DenseMap<const Value*, unsigned> ValueMap;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000553
554private:
Devang Patel983c6b12009-07-08 21:44:25 +0000555 /// TheModule - The module for which we are holding slot numbers.
Chris Lattner393b7cd2008-08-17 04:17:45 +0000556 const Module* TheModule;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000557
Devang Patel983c6b12009-07-08 21:44:25 +0000558 /// TheFunction - The function for which we are holding slot numbers.
Chris Lattner393b7cd2008-08-17 04:17:45 +0000559 const Function* TheFunction;
560 bool FunctionProcessed;
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000561 bool ShouldInitializeAllMetadata;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000562
Jay Foaddbb221d2011-07-11 07:28:49 +0000563 /// mMap - The slot map for the module level data.
Chris Lattner393b7cd2008-08-17 04:17:45 +0000564 ValueMap mMap;
565 unsigned mNext;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000566
Jay Foaddbb221d2011-07-11 07:28:49 +0000567 /// fMap - The slot map for the function level data.
Chris Lattner393b7cd2008-08-17 04:17:45 +0000568 ValueMap fMap;
569 unsigned fNext;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000570
Devang Patel983c6b12009-07-08 21:44:25 +0000571 /// mdnMap - Map for MDNodes.
Chris Lattnercf4a76e2009-12-31 02:20:11 +0000572 DenseMap<const MDNode*, unsigned> mdnMap;
Devang Patel983c6b12009-07-08 21:44:25 +0000573 unsigned mdnNext;
Bill Wendling829b4782013-02-11 08:43:33 +0000574
575 /// asMap - The slot map for attribute sets.
576 DenseMap<AttributeSet, unsigned> asMap;
577 unsigned asNext;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000578public:
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000579 /// Construct from a module.
580 ///
581 /// If \c ShouldInitializeAllMetadata, initializes all metadata in all
582 /// functions, giving correct numbering for metadata referenced only from
583 /// within a function (even if no functions have been initialized).
584 explicit SlotTracker(const Module *M,
585 bool ShouldInitializeAllMetadata = false);
Chris Lattner393b7cd2008-08-17 04:17:45 +0000586 /// Construct from a function, starting out in incorp state.
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000587 ///
588 /// If \c ShouldInitializeAllMetadata, initializes all metadata in all
589 /// functions, giving correct numbering for metadata referenced only from
590 /// within a function (even if no functions have been initialized).
591 explicit SlotTracker(const Function *F,
592 bool ShouldInitializeAllMetadata = false);
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000593
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000594 /// Return the slot number of the specified value in it's type
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000595 /// plane. If something is not in the SlotTracker, return -1.
Chris Lattner5e043322007-01-11 03:54:27 +0000596 int getLocalSlot(const Value *V);
597 int getGlobalSlot(const GlobalValue *V);
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000598 int getMetadataSlot(const MDNode *N);
Bill Wendling829b4782013-02-11 08:43:33 +0000599 int getAttributeGroupSlot(AttributeSet AS);
Reid Spencer8beac692004-06-09 15:26:53 +0000600
Misha Brukmanb1c93172005-04-21 23:48:37 +0000601 /// If you'd like to deal with a function instead of just a module, use
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000602 /// this method to get its data into the SlotTracker.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000603 void incorporateFunction(const Function *F) {
604 TheFunction = F;
Reid Spencerb0ac8c42004-08-16 07:46:33 +0000605 FunctionProcessed = false;
606 }
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000607
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +0000608 const Function *getFunction() const { return TheFunction; }
609
Misha Brukmanb1c93172005-04-21 23:48:37 +0000610 /// After calling incorporateFunction, use this method to remove the
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000611 /// most recently incorporated function from the SlotTracker. This
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000612 /// will reset the state of the machine back to just the module contents.
613 void purgeFunction();
614
Devang Patel983c6b12009-07-08 21:44:25 +0000615 /// MDNode map iterators.
Chris Lattnercf4a76e2009-12-31 02:20:11 +0000616 typedef DenseMap<const MDNode*, unsigned>::iterator mdn_iterator;
617 mdn_iterator mdn_begin() { return mdnMap.begin(); }
618 mdn_iterator mdn_end() { return mdnMap.end(); }
619 unsigned mdn_size() const { return mdnMap.size(); }
620 bool mdn_empty() const { return mdnMap.empty(); }
Devang Patel983c6b12009-07-08 21:44:25 +0000621
Bill Wendling829b4782013-02-11 08:43:33 +0000622 /// AttributeSet map iterators.
623 typedef DenseMap<AttributeSet, unsigned>::iterator as_iterator;
624 as_iterator as_begin() { return asMap.begin(); }
625 as_iterator as_end() { return asMap.end(); }
626 unsigned as_size() const { return asMap.size(); }
627 bool as_empty() const { return asMap.empty(); }
628
Reid Spencer56010e42004-05-26 21:56:09 +0000629 /// This function does the actual initialization.
630 inline void initialize();
631
Devang Patel983c6b12009-07-08 21:44:25 +0000632 // Implementation Details
633private:
Chris Lattnerea862a32007-01-09 07:55:49 +0000634 /// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
635 void CreateModuleSlot(const GlobalValue *V);
Devang Patel983c6b12009-07-08 21:44:25 +0000636
637 /// CreateMetadataSlot - Insert the specified MDNode* into the slot table.
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000638 void CreateMetadataSlot(const MDNode *N);
Devang Patel983c6b12009-07-08 21:44:25 +0000639
Chris Lattnerea862a32007-01-09 07:55:49 +0000640 /// CreateFunctionSlot - Insert the specified Value* into the slot table.
641 void CreateFunctionSlot(const Value *V);
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000642
Bill Wendling829b4782013-02-11 08:43:33 +0000643 /// \brief Insert the specified AttributeSet into the slot table.
644 void CreateAttributeSetSlot(AttributeSet AS);
645
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000646 /// Add all of the module level global variables (and their initializers)
647 /// and function declarations, but not the contents of those functions.
648 void processModule();
649
Devang Patel983c6b12009-07-08 21:44:25 +0000650 /// Add all of the functions arguments, basic blocks, and instructions.
Reid Spencer56010e42004-05-26 21:56:09 +0000651 void processFunction();
652
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000653 /// Add all of the metadata from a function.
654 void processFunctionMetadata(const Function &F);
655
Duncan P. N. Exon Smith20b76ac2015-03-14 19:48:31 +0000656 /// Add all of the metadata from an instruction.
657 void processInstructionMetadata(const Instruction &I);
658
Aaron Ballmanf9a18972015-02-15 22:54:22 +0000659 SlotTracker(const SlotTracker &) = delete;
660 void operator=(const SlotTracker &) = delete;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000661};
Benjamin Kramerba05a782015-03-17 19:53:41 +0000662} // namespace
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000663
Benjamin Kramerba05a782015-03-17 19:53:41 +0000664static SlotTracker *createSlotTracker(const Module *M) {
Daniel Maleaded9f932013-05-08 20:38:31 +0000665 return new SlotTracker(M);
666}
Chris Lattner3eee99c2008-08-19 04:36:02 +0000667
668static SlotTracker *createSlotTracker(const Value *V) {
669 if (const Argument *FA = dyn_cast<Argument>(V))
670 return new SlotTracker(FA->getParent());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000671
Chris Lattner3eee99c2008-08-19 04:36:02 +0000672 if (const Instruction *I = dyn_cast<Instruction>(V))
Andrew Trick2f0cbf62011-09-30 19:50:40 +0000673 if (I->getParent())
674 return new SlotTracker(I->getParent()->getParent());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000675
Chris Lattner3eee99c2008-08-19 04:36:02 +0000676 if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
677 return new SlotTracker(BB->getParent());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000678
Chris Lattner3eee99c2008-08-19 04:36:02 +0000679 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
680 return new SlotTracker(GV->getParent());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000681
Chris Lattner3eee99c2008-08-19 04:36:02 +0000682 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V))
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000683 return new SlotTracker(GA->getParent());
684
Chris Lattner3eee99c2008-08-19 04:36:02 +0000685 if (const Function *Func = dyn_cast<Function>(V))
686 return new SlotTracker(Func);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000687
Craig Topperc6207612014-04-09 06:08:46 +0000688 return nullptr;
Chris Lattner3eee99c2008-08-19 04:36:02 +0000689}
690
691#if 0
David Greenec7f9b122010-01-05 01:29:26 +0000692#define ST_DEBUG(X) dbgs() << X
Chris Lattner3eee99c2008-08-19 04:36:02 +0000693#else
Chris Lattner604e3512008-08-19 04:47:09 +0000694#define ST_DEBUG(X)
Chris Lattner3eee99c2008-08-19 04:36:02 +0000695#endif
696
697// Module level constructor. Causes the contents of the Module (sans functions)
698// to be added to the slot table.
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000699SlotTracker::SlotTracker(const Module *M, bool ShouldInitializeAllMetadata)
700 : TheModule(M), TheFunction(nullptr), FunctionProcessed(false),
701 ShouldInitializeAllMetadata(ShouldInitializeAllMetadata), mNext(0),
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000702 fNext(0), mdnNext(0), asNext(0) {}
Chris Lattner3eee99c2008-08-19 04:36:02 +0000703
704// Function level constructor. Causes the contents of the Module and the one
705// function provided to be added to the slot table.
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000706SlotTracker::SlotTracker(const Function *F, bool ShouldInitializeAllMetadata)
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000707 : TheModule(F ? F->getParent() : nullptr), TheFunction(F),
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000708 FunctionProcessed(false),
709 ShouldInitializeAllMetadata(ShouldInitializeAllMetadata), mNext(0),
710 fNext(0), mdnNext(0), asNext(0) {}
Chris Lattner3eee99c2008-08-19 04:36:02 +0000711
712inline void SlotTracker::initialize() {
713 if (TheModule) {
714 processModule();
Craig Topperc6207612014-04-09 06:08:46 +0000715 TheModule = nullptr; ///< Prevent re-processing next time we're called.
Chris Lattner3eee99c2008-08-19 04:36:02 +0000716 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000717
Chris Lattner3eee99c2008-08-19 04:36:02 +0000718 if (TheFunction && !FunctionProcessed)
719 processFunction();
720}
721
722// Iterate through all the global variables, functions, and global
723// variable initializers and create slots for them.
724void SlotTracker::processModule() {
Chris Lattner604e3512008-08-19 04:47:09 +0000725 ST_DEBUG("begin processModule!\n");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000726
Chris Lattner3eee99c2008-08-19 04:36:02 +0000727 // Add all of the unnamed global variables to the value table.
Rafael Espindola5d8a1552015-06-17 17:33:37 +0000728 for (const GlobalVariable &Var : TheModule->globals()) {
729 if (!Var.hasName())
730 CreateModuleSlot(&Var);
Devang Patel983c6b12009-07-08 21:44:25 +0000731 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000732
Rafael Espindola54fc2982015-06-17 17:53:31 +0000733 for (const GlobalAlias &A : TheModule->aliases()) {
734 if (!A.hasName())
735 CreateModuleSlot(&A);
736 }
737
Devang Patel23e68302009-07-29 22:04:47 +0000738 // Add metadata used by named metadata.
Rafael Espindola5d8a1552015-06-17 17:33:37 +0000739 for (const NamedMDNode &NMD : TheModule->named_metadata()) {
740 for (unsigned i = 0, e = NMD.getNumOperands(); i != e; ++i)
741 CreateMetadataSlot(NMD.getOperand(i));
Devang Patel23e68302009-07-29 22:04:47 +0000742 }
743
Rafael Espindola5d8a1552015-06-17 17:33:37 +0000744 for (const Function &F : *TheModule) {
745 if (!F.hasName())
Bill Wendling829b4782013-02-11 08:43:33 +0000746 // Add all the unnamed functions to the table.
Rafael Espindola5d8a1552015-06-17 17:33:37 +0000747 CreateModuleSlot(&F);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000748
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000749 if (ShouldInitializeAllMetadata)
Rafael Espindola5d8a1552015-06-17 17:33:37 +0000750 processFunctionMetadata(F);
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000751
Bill Wendling829b4782013-02-11 08:43:33 +0000752 // Add all the function attributes to the table.
Bill Wendling90bc19c2013-02-20 07:21:42 +0000753 // FIXME: Add attributes of other objects?
Rafael Espindola5d8a1552015-06-17 17:33:37 +0000754 AttributeSet FnAttrs = F.getAttributes().getFnAttributes();
Bill Wendling829b4782013-02-11 08:43:33 +0000755 if (FnAttrs.hasAttributes(AttributeSet::FunctionIndex))
756 CreateAttributeSetSlot(FnAttrs);
757 }
758
Chris Lattner604e3512008-08-19 04:47:09 +0000759 ST_DEBUG("end processModule!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000760}
761
Chris Lattner3eee99c2008-08-19 04:36:02 +0000762// Process the arguments, basic blocks, and instructions of a function.
763void SlotTracker::processFunction() {
Chris Lattner604e3512008-08-19 04:47:09 +0000764 ST_DEBUG("begin processFunction!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000765 fNext = 0;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000766
Chris Lattner3eee99c2008-08-19 04:36:02 +0000767 // Add all the function arguments with no names.
768 for(Function::const_arg_iterator AI = TheFunction->arg_begin(),
769 AE = TheFunction->arg_end(); AI != AE; ++AI)
770 if (!AI->hasName())
771 CreateFunctionSlot(AI);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000772
Chris Lattner604e3512008-08-19 04:47:09 +0000773 ST_DEBUG("Inserting Instructions:\n");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000774
Chris Lattner3eee99c2008-08-19 04:36:02 +0000775 // Add all of the basic blocks and instructions with no names.
Duncan P. N. Exon Smith27f33ee2015-03-14 19:44:01 +0000776 for (auto &BB : *TheFunction) {
777 if (!BB.hasName())
778 CreateFunctionSlot(&BB);
Andrew Trickec4b6e72011-09-30 19:48:58 +0000779
Duncan P. N. Exon Smith3d4cd752015-04-24 22:04:41 +0000780 processFunctionMetadata(*TheFunction);
781
Duncan P. N. Exon Smith27f33ee2015-03-14 19:44:01 +0000782 for (auto &I : BB) {
783 if (!I.getType()->isVoidTy() && !I.hasName())
784 CreateFunctionSlot(&I);
Andrew Trickec4b6e72011-09-30 19:48:58 +0000785
Duncan P. N. Exon Smith20b76ac2015-03-14 19:48:31 +0000786 // We allow direct calls to any llvm.foo function here, because the
787 // target may not be linked into the optimizer.
788 if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
Bill Wendlinga0323742013-02-22 09:09:42 +0000789 // Add all the call attributes to the table.
790 AttributeSet Attrs = CI->getAttributes().getFnAttributes();
791 if (Attrs.hasAttributes(AttributeSet::FunctionIndex))
792 CreateAttributeSetSlot(Attrs);
Duncan P. N. Exon Smith27f33ee2015-03-14 19:44:01 +0000793 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
Bill Wendlinga0323742013-02-22 09:09:42 +0000794 // Add all the call attributes to the table.
795 AttributeSet Attrs = II->getAttributes().getFnAttributes();
796 if (Attrs.hasAttributes(AttributeSet::FunctionIndex))
797 CreateAttributeSetSlot(Attrs);
Chris Lattner58aff8f2010-05-10 20:53:17 +0000798 }
Devang Patel983c6b12009-07-08 21:44:25 +0000799 }
Chris Lattner3eee99c2008-08-19 04:36:02 +0000800 }
Devang Pateldec23fd2009-09-16 20:21:17 +0000801
Chris Lattner3eee99c2008-08-19 04:36:02 +0000802 FunctionProcessed = true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000803
Chris Lattner604e3512008-08-19 04:47:09 +0000804 ST_DEBUG("end processFunction!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000805}
806
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000807void SlotTracker::processFunctionMetadata(const Function &F) {
Duncan P. N. Exon Smith3d4cd752015-04-24 22:04:41 +0000808 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
809 for (auto &BB : F) {
810 F.getAllMetadata(MDs);
811 for (auto &MD : MDs)
812 CreateMetadataSlot(MD.second);
813
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000814 for (auto &I : BB)
815 processInstructionMetadata(I);
Duncan P. N. Exon Smith3d4cd752015-04-24 22:04:41 +0000816 }
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000817}
818
Duncan P. N. Exon Smith20b76ac2015-03-14 19:48:31 +0000819void SlotTracker::processInstructionMetadata(const Instruction &I) {
820 // Process metadata used directly by intrinsics.
821 if (const CallInst *CI = dyn_cast<CallInst>(&I))
822 if (Function *F = CI->getCalledFunction())
823 if (F->isIntrinsic())
824 for (auto &Op : I.operands())
825 if (auto *V = dyn_cast_or_null<MetadataAsValue>(Op))
826 if (MDNode *N = dyn_cast<MDNode>(V->getMetadata()))
827 CreateMetadataSlot(N);
828
829 // Process metadata attached to this instruction.
830 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
831 I.getAllMetadata(MDs);
832 for (auto &MD : MDs)
833 CreateMetadataSlot(MD.second);
834}
835
Chris Lattner3eee99c2008-08-19 04:36:02 +0000836/// Clean up after incorporating a function. This is the only way to get out of
837/// the function incorporation state that affects get*Slot/Create*Slot. Function
838/// incorporation state is indicated by TheFunction != 0.
839void SlotTracker::purgeFunction() {
Chris Lattner604e3512008-08-19 04:47:09 +0000840 ST_DEBUG("begin purgeFunction!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000841 fMap.clear(); // Simply discard the function level map
Craig Topperc6207612014-04-09 06:08:46 +0000842 TheFunction = nullptr;
Chris Lattner3eee99c2008-08-19 04:36:02 +0000843 FunctionProcessed = false;
Chris Lattner604e3512008-08-19 04:47:09 +0000844 ST_DEBUG("end purgeFunction!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000845}
846
847/// getGlobalSlot - Get the slot number of a global value.
848int SlotTracker::getGlobalSlot(const GlobalValue *V) {
849 // Check for uninitialized state and do lazy initialization.
850 initialize();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000851
Jay Foaddbb221d2011-07-11 07:28:49 +0000852 // Find the value in the module map
Chris Lattner3eee99c2008-08-19 04:36:02 +0000853 ValueMap::iterator MI = mMap.find(V);
Dan Gohman1dd27572008-10-01 19:58:59 +0000854 return MI == mMap.end() ? -1 : (int)MI->second;
Chris Lattner3eee99c2008-08-19 04:36:02 +0000855}
856
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000857/// getMetadataSlot - Get the slot number of a MDNode.
858int SlotTracker::getMetadataSlot(const MDNode *N) {
Devang Patel983c6b12009-07-08 21:44:25 +0000859 // Check for uninitialized state and do lazy initialization.
860 initialize();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000861
Jay Foaddbb221d2011-07-11 07:28:49 +0000862 // Find the MDNode in the module map
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000863 mdn_iterator MI = mdnMap.find(N);
Devang Patel983c6b12009-07-08 21:44:25 +0000864 return MI == mdnMap.end() ? -1 : (int)MI->second;
865}
866
Chris Lattner3eee99c2008-08-19 04:36:02 +0000867
868/// getLocalSlot - Get the slot number for a value that is local to a function.
869int SlotTracker::getLocalSlot(const Value *V) {
870 assert(!isa<Constant>(V) && "Can't get a constant or global slot with this!");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000871
Chris Lattner3eee99c2008-08-19 04:36:02 +0000872 // Check for uninitialized state and do lazy initialization.
873 initialize();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000874
Chris Lattner3eee99c2008-08-19 04:36:02 +0000875 ValueMap::iterator FI = fMap.find(V);
Dan Gohman1dd27572008-10-01 19:58:59 +0000876 return FI == fMap.end() ? -1 : (int)FI->second;
Chris Lattner3eee99c2008-08-19 04:36:02 +0000877}
878
Bill Wendling829b4782013-02-11 08:43:33 +0000879int SlotTracker::getAttributeGroupSlot(AttributeSet AS) {
880 // Check for uninitialized state and do lazy initialization.
881 initialize();
882
883 // Find the AttributeSet in the module map.
884 as_iterator AI = asMap.find(AS);
885 return AI == asMap.end() ? -1 : (int)AI->second;
886}
Chris Lattner3eee99c2008-08-19 04:36:02 +0000887
888/// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
889void SlotTracker::CreateModuleSlot(const GlobalValue *V) {
890 assert(V && "Can't insert a null Value into SlotTracker!");
Chris Lattner031560c2009-12-29 07:25:48 +0000891 assert(!V->getType()->isVoidTy() && "Doesn't need a slot!");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000892 assert(!V->hasName() && "Doesn't need a slot!");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000893
Chris Lattner3eee99c2008-08-19 04:36:02 +0000894 unsigned DestSlot = mNext++;
895 mMap[V] = DestSlot;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000896
Chris Lattner604e3512008-08-19 04:47:09 +0000897 ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" <<
Chris Lattner3eee99c2008-08-19 04:36:02 +0000898 DestSlot << " [");
899 // G = Global, F = Function, A = Alias, o = other
Chris Lattner604e3512008-08-19 04:47:09 +0000900 ST_DEBUG((isa<GlobalVariable>(V) ? 'G' :
Chris Lattner3eee99c2008-08-19 04:36:02 +0000901 (isa<Function>(V) ? 'F' :
902 (isa<GlobalAlias>(V) ? 'A' : 'o'))) << "]\n");
903}
904
Chris Lattner3eee99c2008-08-19 04:36:02 +0000905/// CreateSlot - Create a new slot for the specified value if it has no name.
906void SlotTracker::CreateFunctionSlot(const Value *V) {
Chris Lattner031560c2009-12-29 07:25:48 +0000907 assert(!V->getType()->isVoidTy() && !V->hasName() && "Doesn't need a slot!");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000908
Chris Lattner3eee99c2008-08-19 04:36:02 +0000909 unsigned DestSlot = fNext++;
910 fMap[V] = DestSlot;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000911
Chris Lattner3eee99c2008-08-19 04:36:02 +0000912 // G = Global, F = Function, o = other
Chris Lattner604e3512008-08-19 04:47:09 +0000913 ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" <<
Chris Lattner3eee99c2008-08-19 04:36:02 +0000914 DestSlot << " [o]\n");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000915}
Chris Lattner3eee99c2008-08-19 04:36:02 +0000916
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000917/// CreateModuleSlot - Insert the specified MDNode* into the slot table.
918void SlotTracker::CreateMetadataSlot(const MDNode *N) {
919 assert(N && "Can't insert a null Value into SlotTracker!");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000920
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000921 unsigned DestSlot = mdnNext;
922 if (!mdnMap.insert(std::make_pair(N, DestSlot)).second)
923 return;
924 ++mdnNext;
Devang Patel983c6b12009-07-08 21:44:25 +0000925
Chris Lattner0d50bdd2009-12-31 02:27:30 +0000926 // Recursively add any MDNodes referenced by operands.
927 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
928 if (const MDNode *Op = dyn_cast_or_null<MDNode>(N->getOperand(i)))
929 CreateMetadataSlot(Op);
Devang Patel983c6b12009-07-08 21:44:25 +0000930}
Chris Lattner3eee99c2008-08-19 04:36:02 +0000931
Bill Wendling829b4782013-02-11 08:43:33 +0000932void SlotTracker::CreateAttributeSetSlot(AttributeSet AS) {
933 assert(AS.hasAttributes(AttributeSet::FunctionIndex) &&
934 "Doesn't need a slot!");
935
936 as_iterator I = asMap.find(AS);
937 if (I != asMap.end())
938 return;
939
940 unsigned DestSlot = asNext++;
941 asMap[AS] = DestSlot;
942}
943
Chris Lattner3eee99c2008-08-19 04:36:02 +0000944//===----------------------------------------------------------------------===//
945// AsmWriter Implementation
946//===----------------------------------------------------------------------===//
Chris Lattner7f8845a2002-07-23 18:07:49 +0000947
Dan Gohman12dad632009-08-12 20:56:03 +0000948static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
Dan Gohman8061d9e2009-08-13 15:27:57 +0000949 TypePrinting *TypePrinter,
Dan Gohmana2489d12010-07-20 23:55:01 +0000950 SlotTracker *Machine,
951 const Module *Context);
Reid Spencer58d30f22004-07-04 11:50:43 +0000952
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000953static void WriteAsOperandInternal(raw_ostream &Out, const Metadata *MD,
954 TypePrinting *TypePrinter,
955 SlotTracker *Machine, const Module *Context,
956 bool FromValue = false);
957
Chris Lattnerfc9f1c92006-12-06 06:40:49 +0000958static const char *getPredicateText(unsigned predicate) {
Reid Spencer812a1be2006-12-04 05:19:18 +0000959 const char * pred = "unknown";
960 switch (predicate) {
Chris Lattnerbddea6a2009-12-31 02:13:35 +0000961 case FCmpInst::FCMP_FALSE: pred = "false"; break;
962 case FCmpInst::FCMP_OEQ: pred = "oeq"; break;
963 case FCmpInst::FCMP_OGT: pred = "ogt"; break;
964 case FCmpInst::FCMP_OGE: pred = "oge"; break;
965 case FCmpInst::FCMP_OLT: pred = "olt"; break;
966 case FCmpInst::FCMP_OLE: pred = "ole"; break;
967 case FCmpInst::FCMP_ONE: pred = "one"; break;
968 case FCmpInst::FCMP_ORD: pred = "ord"; break;
969 case FCmpInst::FCMP_UNO: pred = "uno"; break;
970 case FCmpInst::FCMP_UEQ: pred = "ueq"; break;
971 case FCmpInst::FCMP_UGT: pred = "ugt"; break;
972 case FCmpInst::FCMP_UGE: pred = "uge"; break;
973 case FCmpInst::FCMP_ULT: pred = "ult"; break;
974 case FCmpInst::FCMP_ULE: pred = "ule"; break;
975 case FCmpInst::FCMP_UNE: pred = "une"; break;
976 case FCmpInst::FCMP_TRUE: pred = "true"; break;
977 case ICmpInst::ICMP_EQ: pred = "eq"; break;
978 case ICmpInst::ICMP_NE: pred = "ne"; break;
979 case ICmpInst::ICMP_SGT: pred = "sgt"; break;
980 case ICmpInst::ICMP_SGE: pred = "sge"; break;
981 case ICmpInst::ICMP_SLT: pred = "slt"; break;
982 case ICmpInst::ICMP_SLE: pred = "sle"; break;
983 case ICmpInst::ICMP_UGT: pred = "ugt"; break;
984 case ICmpInst::ICMP_UGE: pred = "uge"; break;
985 case ICmpInst::ICMP_ULT: pred = "ult"; break;
986 case ICmpInst::ICMP_ULE: pred = "ule"; break;
Reid Spencer812a1be2006-12-04 05:19:18 +0000987 }
988 return pred;
989}
990
Eli Friedmanc9a551e2011-07-28 21:48:00 +0000991static void writeAtomicRMWOperation(raw_ostream &Out,
992 AtomicRMWInst::BinOp Op) {
993 switch (Op) {
994 default: Out << " <unknown operation " << Op << ">"; break;
995 case AtomicRMWInst::Xchg: Out << " xchg"; break;
996 case AtomicRMWInst::Add: Out << " add"; break;
997 case AtomicRMWInst::Sub: Out << " sub"; break;
998 case AtomicRMWInst::And: Out << " and"; break;
999 case AtomicRMWInst::Nand: Out << " nand"; break;
1000 case AtomicRMWInst::Or: Out << " or"; break;
1001 case AtomicRMWInst::Xor: Out << " xor"; break;
1002 case AtomicRMWInst::Max: Out << " max"; break;
1003 case AtomicRMWInst::Min: Out << " min"; break;
1004 case AtomicRMWInst::UMax: Out << " umax"; break;
1005 case AtomicRMWInst::UMin: Out << " umin"; break;
1006 }
1007}
Devang Patel983c6b12009-07-08 21:44:25 +00001008
Dan Gohman12dad632009-08-12 20:56:03 +00001009static void WriteOptimizationInfo(raw_ostream &Out, const User *U) {
Michael Ilseman92053172012-11-27 00:42:44 +00001010 if (const FPMathOperator *FPO = dyn_cast<const FPMathOperator>(U)) {
1011 // Unsafe algebra implies all the others, no need to write them all out
1012 if (FPO->hasUnsafeAlgebra())
1013 Out << " fast";
1014 else {
1015 if (FPO->hasNoNaNs())
1016 Out << " nnan";
1017 if (FPO->hasNoInfs())
1018 Out << " ninf";
1019 if (FPO->hasNoSignedZeros())
1020 Out << " nsz";
1021 if (FPO->hasAllowReciprocal())
1022 Out << " arcp";
1023 }
1024 }
1025
Dan Gohman0ebd6962009-07-20 21:19:07 +00001026 if (const OverflowingBinaryOperator *OBO =
1027 dyn_cast<OverflowingBinaryOperator>(U)) {
Dan Gohman16f54152009-08-20 17:11:38 +00001028 if (OBO->hasNoUnsignedWrap())
Dan Gohman9c7f8082009-07-27 16:11:46 +00001029 Out << " nuw";
Dan Gohman16f54152009-08-20 17:11:38 +00001030 if (OBO->hasNoSignedWrap())
Dan Gohman9c7f8082009-07-27 16:11:46 +00001031 Out << " nsw";
Chris Lattner35315d02011-02-06 21:44:57 +00001032 } else if (const PossiblyExactOperator *Div =
1033 dyn_cast<PossiblyExactOperator>(U)) {
Dan Gohman0ebd6962009-07-20 21:19:07 +00001034 if (Div->isExact())
Dan Gohman9c7f8082009-07-27 16:11:46 +00001035 Out << " exact";
Dan Gohman1639c392009-07-27 21:53:46 +00001036 } else if (const GEPOperator *GEP = dyn_cast<GEPOperator>(U)) {
1037 if (GEP->isInBounds())
1038 Out << " inbounds";
Dan Gohman0ebd6962009-07-20 21:19:07 +00001039 }
1040}
1041
Dan Gohmanefb8dbb2010-07-14 20:57:55 +00001042static void WriteConstantInternal(raw_ostream &Out, const Constant *CV,
1043 TypePrinting &TypePrinter,
Dan Gohmana2489d12010-07-20 23:55:01 +00001044 SlotTracker *Machine,
1045 const Module *Context) {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001046 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00001047 if (CI->getType()->isIntegerTy(1)) {
Reid Spencercddc9df2007-01-12 04:24:46 +00001048 Out << (CI->getZExtValue() ? "true" : "false");
Chris Lattner17f71652008-08-17 07:19:36 +00001049 return;
1050 }
1051 Out << CI->getValue();
1052 return;
1053 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001054
Chris Lattner17f71652008-08-17 07:19:36 +00001055 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Tobias Grosser6b31d172012-05-24 15:59:06 +00001056 if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEsingle ||
Dan Gohman518cda42011-12-17 00:04:22 +00001057 &CFP->getValueAPF().getSemantics() == &APFloat::IEEEdouble) {
Dale Johannesen028084e2007-09-12 03:30:33 +00001058 // We would like to output the FP constant value in exponential notation,
1059 // but we cannot do this if doing so will lose precision. Check here to
1060 // make sure that we only output it in exponential format if we can parse
1061 // the value back and get the same value.
1062 //
Dale Johannesen1f864982009-01-21 20:32:55 +00001063 bool ignored;
Dan Gohman518cda42011-12-17 00:04:22 +00001064 bool isHalf = &CFP->getValueAPF().getSemantics()==&APFloat::IEEEhalf;
Dale Johannesen028084e2007-09-12 03:30:33 +00001065 bool isDouble = &CFP->getValueAPF().getSemantics()==&APFloat::IEEEdouble;
NAKAMURA Takumi35d19c02012-02-16 08:12:24 +00001066 bool isInf = CFP->getValueAPF().isInfinity();
1067 bool isNaN = CFP->getValueAPF().isNaN();
1068 if (!isHalf && !isInf && !isNaN) {
Dan Gohman518cda42011-12-17 00:04:22 +00001069 double Val = isDouble ? CFP->getValueAPF().convertToDouble() :
1070 CFP->getValueAPF().convertToFloat();
1071 SmallString<128> StrVal;
1072 raw_svector_ostream(StrVal) << Val;
Chris Lattner1e194682002-04-18 18:53:13 +00001073
Dan Gohman518cda42011-12-17 00:04:22 +00001074 // Check to make sure that the stringized number is not some string like
1075 // "Inf" or NaN, that atof will accept, but the lexer will not. Check
1076 // that the string matches the "[-+]?[0-9]" regex.
1077 //
1078 if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
1079 ((StrVal[0] == '-' || StrVal[0] == '+') &&
1080 (StrVal[1] >= '0' && StrVal[1] <= '9'))) {
1081 // Reparse stringized version!
NAKAMURA Takumiaec41232012-02-16 04:19:15 +00001082 if (APFloat(APFloat::IEEEdouble, StrVal).convertToDouble() == Val) {
Yaron Keren09fb7c62015-03-10 07:33:23 +00001083 Out << StrVal;
Dan Gohman518cda42011-12-17 00:04:22 +00001084 return;
1085 }
Dale Johannesen028084e2007-09-12 03:30:33 +00001086 }
Chris Lattner1e194682002-04-18 18:53:13 +00001087 }
Dale Johannesen028084e2007-09-12 03:30:33 +00001088 // Otherwise we could not reparse it to exactly the same value, so we must
Dale Johannesen1f864982009-01-21 20:32:55 +00001089 // output the string in hexadecimal format! Note that loading and storing
1090 // floating point types changes the bits of NaNs on some hosts, notably
1091 // x86, so we must not use these types.
Benjamin Kramer86c77412014-03-15 18:47:07 +00001092 static_assert(sizeof(double) == sizeof(uint64_t),
1093 "assuming that double is 64 bits!");
Chris Lattner5505eed2008-11-10 04:30:26 +00001094 char Buffer[40];
Dale Johannesen1f864982009-01-21 20:32:55 +00001095 APFloat apf = CFP->getValueAPF();
Dan Gohman518cda42011-12-17 00:04:22 +00001096 // Halves and floats are represented in ASCII IR as double, convert.
Dale Johannesen1f864982009-01-21 20:32:55 +00001097 if (!isDouble)
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001098 apf.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven,
Dale Johannesen1f864982009-01-21 20:32:55 +00001099 &ignored);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001100 Out << "0x" <<
1101 utohex_buffer(uint64_t(apf.bitcastToAPInt().getZExtValue()),
Dale Johannesen1f864982009-01-21 20:32:55 +00001102 Buffer+40);
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001103 return;
1104 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001105
Tobias Grosser6b31d172012-05-24 15:59:06 +00001106 // Either half, or some form of long double.
1107 // These appear as a magic letter identifying the type, then a
1108 // fixed number of hex digits.
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001109 Out << "0x";
Tobias Grosser6b31d172012-05-24 15:59:06 +00001110 // Bit position, in the current word, of the next nibble to print.
1111 int shiftcount;
1112
Dale Johannesen93eefa02009-03-23 21:16:53 +00001113 if (&CFP->getValueAPF().getSemantics() == &APFloat::x87DoubleExtended) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001114 Out << 'K';
Dale Johannesen93eefa02009-03-23 21:16:53 +00001115 // api needed to prevent premature destruction
1116 APInt api = CFP->getValueAPF().bitcastToAPInt();
1117 const uint64_t* p = api.getRawData();
1118 uint64_t word = p[1];
Tobias Grosser6b31d172012-05-24 15:59:06 +00001119 shiftcount = 12;
Dale Johannesen93eefa02009-03-23 21:16:53 +00001120 int width = api.getBitWidth();
1121 for (int j=0; j<width; j+=4, shiftcount-=4) {
1122 unsigned int nibble = (word>>shiftcount) & 15;
1123 if (nibble < 10)
1124 Out << (unsigned char)(nibble + '0');
1125 else
1126 Out << (unsigned char)(nibble - 10 + 'A');
1127 if (shiftcount == 0 && j+4 < width) {
1128 word = *p;
1129 shiftcount = 64;
1130 if (width-j-4 < 64)
1131 shiftcount = width-j-4;
1132 }
1133 }
1134 return;
Tobias Grosser6b31d172012-05-24 15:59:06 +00001135 } else if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEquad) {
1136 shiftcount = 60;
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001137 Out << 'L';
Tobias Grosser6b31d172012-05-24 15:59:06 +00001138 } else if (&CFP->getValueAPF().getSemantics() == &APFloat::PPCDoubleDouble) {
1139 shiftcount = 60;
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001140 Out << 'M';
Tobias Grosser6b31d172012-05-24 15:59:06 +00001141 } else if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEhalf) {
1142 shiftcount = 12;
1143 Out << 'H';
1144 } else
Torok Edwinfbcc6632009-07-14 16:55:14 +00001145 llvm_unreachable("Unsupported floating point type");
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001146 // api needed to prevent premature destruction
Dale Johannesen54306fe2008-10-09 18:53:47 +00001147 APInt api = CFP->getValueAPF().bitcastToAPInt();
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001148 const uint64_t* p = api.getRawData();
1149 uint64_t word = *p;
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001150 int width = api.getBitWidth();
1151 for (int j=0; j<width; j+=4, shiftcount-=4) {
1152 unsigned int nibble = (word>>shiftcount) & 15;
1153 if (nibble < 10)
1154 Out << (unsigned char)(nibble + '0');
Dale Johannesen028084e2007-09-12 03:30:33 +00001155 else
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001156 Out << (unsigned char)(nibble - 10 + 'A');
1157 if (shiftcount == 0 && j+4 < width) {
1158 word = *(++p);
1159 shiftcount = 64;
1160 if (width-j-4 < 64)
1161 shiftcount = width-j-4;
Dale Johannesen028084e2007-09-12 03:30:33 +00001162 }
1163 }
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001164 return;
1165 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001166
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001167 if (isa<ConstantAggregateZero>(CV)) {
Chris Lattner76b2ff42004-02-15 05:55:15 +00001168 Out << "zeroinitializer";
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001169 return;
1170 }
Andrew Trickec4b6e72011-09-30 19:48:58 +00001171
Chris Lattner214cc702009-10-28 03:38:12 +00001172 if (const BlockAddress *BA = dyn_cast<BlockAddress>(CV)) {
1173 Out << "blockaddress(";
Dan Gohmana2489d12010-07-20 23:55:01 +00001174 WriteAsOperandInternal(Out, BA->getFunction(), &TypePrinter, Machine,
1175 Context);
Chris Lattner214cc702009-10-28 03:38:12 +00001176 Out << ", ";
Dan Gohmana2489d12010-07-20 23:55:01 +00001177 WriteAsOperandInternal(Out, BA->getBasicBlock(), &TypePrinter, Machine,
1178 Context);
Chris Lattner214cc702009-10-28 03:38:12 +00001179 Out << ")";
1180 return;
1181 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001182
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001183 if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001184 Type *ETy = CA->getType()->getElementType();
Chris Lattnercf9e8f62012-02-05 02:29:43 +00001185 Out << '[';
1186 TypePrinter.print(ETy, Out);
1187 Out << ' ';
1188 WriteAsOperandInternal(Out, CA->getOperand(0),
1189 &TypePrinter, Machine,
1190 Context);
1191 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
1192 Out << ", ";
Chris Lattner9c181f62012-01-31 03:15:40 +00001193 TypePrinter.print(ETy, Out);
1194 Out << ' ';
Chris Lattnercf9e8f62012-02-05 02:29:43 +00001195 WriteAsOperandInternal(Out, CA->getOperand(i), &TypePrinter, Machine,
Chris Lattner9c181f62012-01-31 03:15:40 +00001196 Context);
Chris Lattnerd84bb632002-04-16 21:36:08 +00001197 }
Chris Lattnercf9e8f62012-02-05 02:29:43 +00001198 Out << ']';
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001199 return;
1200 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001201
Chris Lattnerfa775002012-01-26 02:32:04 +00001202 if (const ConstantDataArray *CA = dyn_cast<ConstantDataArray>(CV)) {
1203 // As a special case, print the array as a string if it is an array of
1204 // i8 with ConstantInt values.
1205 if (CA->isString()) {
1206 Out << "c\"";
1207 PrintEscapedString(CA->getAsString(), Out);
1208 Out << '"';
1209 return;
1210 }
1211
1212 Type *ETy = CA->getType()->getElementType();
1213 Out << '[';
Chris Lattner9c181f62012-01-31 03:15:40 +00001214 TypePrinter.print(ETy, Out);
1215 Out << ' ';
1216 WriteAsOperandInternal(Out, CA->getElementAsConstant(0),
1217 &TypePrinter, Machine,
1218 Context);
1219 for (unsigned i = 1, e = CA->getNumElements(); i != e; ++i) {
1220 Out << ", ";
Chris Lattnerfa775002012-01-26 02:32:04 +00001221 TypePrinter.print(ETy, Out);
1222 Out << ' ';
Chris Lattner9c181f62012-01-31 03:15:40 +00001223 WriteAsOperandInternal(Out, CA->getElementAsConstant(i), &TypePrinter,
1224 Machine, Context);
Chris Lattnerfa775002012-01-26 02:32:04 +00001225 }
Chris Lattner9c181f62012-01-31 03:15:40 +00001226 Out << ']';
Chris Lattnerfa775002012-01-26 02:32:04 +00001227 return;
1228 }
1229
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001230
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001231 if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
Andrew Lenharth0d124b82007-01-08 18:21:30 +00001232 if (CS->getType()->isPacked())
1233 Out << '<';
Misha Brukman21bbdb92004-06-04 21:11:51 +00001234 Out << '{';
Jim Laskey3bb78742006-02-25 12:27:03 +00001235 unsigned N = CS->getNumOperands();
1236 if (N) {
Chris Lattner604e3512008-08-19 04:47:09 +00001237 Out << ' ';
Chris Lattnere101c442009-02-28 21:26:53 +00001238 TypePrinter.print(CS->getOperand(0)->getType(), Out);
Dan Gohman81313fd2008-09-14 17:21:12 +00001239 Out << ' ';
Chris Lattnerd84bb632002-04-16 21:36:08 +00001240
Dan Gohmana2489d12010-07-20 23:55:01 +00001241 WriteAsOperandInternal(Out, CS->getOperand(0), &TypePrinter, Machine,
1242 Context);
Chris Lattnerd84bb632002-04-16 21:36:08 +00001243
Jim Laskey3bb78742006-02-25 12:27:03 +00001244 for (unsigned i = 1; i < N; i++) {
Chris Lattnerd84bb632002-04-16 21:36:08 +00001245 Out << ", ";
Chris Lattnere101c442009-02-28 21:26:53 +00001246 TypePrinter.print(CS->getOperand(i)->getType(), Out);
Dan Gohman81313fd2008-09-14 17:21:12 +00001247 Out << ' ';
Chris Lattnerd84bb632002-04-16 21:36:08 +00001248
Dan Gohmana2489d12010-07-20 23:55:01 +00001249 WriteAsOperandInternal(Out, CS->getOperand(i), &TypePrinter, Machine,
1250 Context);
Chris Lattnerd84bb632002-04-16 21:36:08 +00001251 }
Dan Gohman81313fd2008-09-14 17:21:12 +00001252 Out << ' ';
Chris Lattnerd84bb632002-04-16 21:36:08 +00001253 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001254
Dan Gohman81313fd2008-09-14 17:21:12 +00001255 Out << '}';
Andrew Lenharth0d124b82007-01-08 18:21:30 +00001256 if (CS->getType()->isPacked())
1257 Out << '>';
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001258 return;
1259 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001260
Chris Lattnerfa775002012-01-26 02:32:04 +00001261 if (isa<ConstantVector>(CV) || isa<ConstantDataVector>(CV)) {
1262 Type *ETy = CV->getType()->getVectorElementType();
Dan Gohman1262a252009-02-11 00:25:25 +00001263 Out << '<';
Chris Lattnere101c442009-02-28 21:26:53 +00001264 TypePrinter.print(ETy, Out);
Dan Gohman81313fd2008-09-14 17:21:12 +00001265 Out << ' ';
Chris Lattnerfa775002012-01-26 02:32:04 +00001266 WriteAsOperandInternal(Out, CV->getAggregateElement(0U), &TypePrinter,
1267 Machine, Context);
1268 for (unsigned i = 1, e = CV->getType()->getVectorNumElements(); i != e;++i){
Chris Lattner585297e82008-08-19 05:26:17 +00001269 Out << ", ";
Chris Lattnere101c442009-02-28 21:26:53 +00001270 TypePrinter.print(ETy, Out);
Dan Gohman81313fd2008-09-14 17:21:12 +00001271 Out << ' ';
Chris Lattnerfa775002012-01-26 02:32:04 +00001272 WriteAsOperandInternal(Out, CV->getAggregateElement(i), &TypePrinter,
1273 Machine, Context);
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001274 }
Dan Gohman1262a252009-02-11 00:25:25 +00001275 Out << '>';
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001276 return;
1277 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001278
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001279 if (isa<ConstantPointerNull>(CV)) {
Chris Lattnerd84bb632002-04-16 21:36:08 +00001280 Out << "null";
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001281 return;
1282 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001283
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001284 if (isa<UndefValue>(CV)) {
Chris Lattner5e0b9f22004-10-16 18:08:06 +00001285 Out << "undef";
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001286 return;
1287 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001288
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001289 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Reid Spencer812a1be2006-12-04 05:19:18 +00001290 Out << CE->getOpcodeName();
Dan Gohman9c7f8082009-07-27 16:11:46 +00001291 WriteOptimizationInfo(Out, CE);
Reid Spencer812a1be2006-12-04 05:19:18 +00001292 if (CE->isCompare())
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001293 Out << ' ' << getPredicateText(CE->getPredicate());
Reid Spencer812a1be2006-12-04 05:19:18 +00001294 Out << " (";
Misha Brukmanb1c93172005-04-21 23:48:37 +00001295
David Blaikief72d05b2015-03-13 18:20:45 +00001296 if (const GEPOperator *GEP = dyn_cast<GEPOperator>(CE)) {
1297 TypePrinter.print(
1298 cast<PointerType>(GEP->getPointerOperandType()->getScalarType())
1299 ->getElementType(),
1300 Out);
1301 Out << ", ";
1302 }
1303
Vikram S. Adveb952b542002-07-14 23:14:45 +00001304 for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) {
Chris Lattnere101c442009-02-28 21:26:53 +00001305 TypePrinter.print((*OI)->getType(), Out);
Dan Gohman81313fd2008-09-14 17:21:12 +00001306 Out << ' ';
Dan Gohmana2489d12010-07-20 23:55:01 +00001307 WriteAsOperandInternal(Out, *OI, &TypePrinter, Machine, Context);
Vikram S. Adveb952b542002-07-14 23:14:45 +00001308 if (OI+1 != CE->op_end())
Chris Lattner3cd8c562002-07-30 18:54:25 +00001309 Out << ", ";
Vikram S. Adveb952b542002-07-14 23:14:45 +00001310 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001311
Dan Gohmana76f0f72008-05-31 19:12:39 +00001312 if (CE->hasIndices()) {
Jay Foad0091fe82011-04-13 15:22:40 +00001313 ArrayRef<unsigned> Indices = CE->getIndices();
Dan Gohmana76f0f72008-05-31 19:12:39 +00001314 for (unsigned i = 0, e = Indices.size(); i != e; ++i)
1315 Out << ", " << Indices[i];
1316 }
1317
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001318 if (CE->isCast()) {
Chris Lattner83b396b2002-08-15 19:37:43 +00001319 Out << " to ";
Chris Lattnere101c442009-02-28 21:26:53 +00001320 TypePrinter.print(CE->getType(), Out);
Chris Lattner83b396b2002-08-15 19:37:43 +00001321 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001322
Misha Brukman21bbdb92004-06-04 21:11:51 +00001323 Out << ')';
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001324 return;
Chris Lattnerd84bb632002-04-16 21:36:08 +00001325 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001326
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001327 Out << "<placeholder or erroneous Constant>";
Chris Lattnerd84bb632002-04-16 21:36:08 +00001328}
1329
Duncan P. N. Exon Smitha6de6a42015-01-12 23:45:31 +00001330static void writeMDTuple(raw_ostream &Out, const MDTuple *Node,
1331 TypePrinting *TypePrinter, SlotTracker *Machine,
1332 const Module *Context) {
Chris Lattnercdec5812009-12-31 02:31:59 +00001333 Out << "!{";
1334 for (unsigned mi = 0, me = Node->getNumOperands(); mi != me; ++mi) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001335 const Metadata *MD = Node->getOperand(mi);
1336 if (!MD)
Chris Lattnercdec5812009-12-31 02:31:59 +00001337 Out << "null";
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001338 else if (auto *MDV = dyn_cast<ValueAsMetadata>(MD)) {
1339 Value *V = MDV->getValue();
Chris Lattnercdec5812009-12-31 02:31:59 +00001340 TypePrinter->print(V->getType(), Out);
1341 Out << ' ';
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001342 WriteAsOperandInternal(Out, V, TypePrinter, Machine, Context);
1343 } else {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001344 WriteAsOperandInternal(Out, MD, TypePrinter, Machine, Context);
Chris Lattnercdec5812009-12-31 02:31:59 +00001345 }
1346 if (mi + 1 != me)
1347 Out << ", ";
1348 }
Andrew Trickec4b6e72011-09-30 19:48:58 +00001349
Chris Lattnercdec5812009-12-31 02:31:59 +00001350 Out << "}";
1351}
1352
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +00001353namespace {
1354struct FieldSeparator {
1355 bool Skip;
Duncan P. N. Exon Smith70ab3d22015-02-21 01:02:18 +00001356 const char *Sep;
1357 FieldSeparator(const char *Sep = ", ") : Skip(true), Sep(Sep) {}
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +00001358};
1359raw_ostream &operator<<(raw_ostream &OS, FieldSeparator &FS) {
1360 if (FS.Skip) {
1361 FS.Skip = false;
1362 return OS;
1363 }
Duncan P. N. Exon Smith70ab3d22015-02-21 01:02:18 +00001364 return OS << FS.Sep;
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +00001365}
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001366struct MDFieldPrinter {
1367 raw_ostream &Out;
1368 FieldSeparator FS;
1369 TypePrinting *TypePrinter;
1370 SlotTracker *Machine;
1371 const Module *Context;
1372
1373 explicit MDFieldPrinter(raw_ostream &Out)
1374 : Out(Out), TypePrinter(nullptr), Machine(nullptr), Context(nullptr) {}
1375 MDFieldPrinter(raw_ostream &Out, TypePrinting *TypePrinter,
1376 SlotTracker *Machine, const Module *Context)
1377 : Out(Out), TypePrinter(TypePrinter), Machine(Machine), Context(Context) {
1378 }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001379 void printTag(const DINode *N);
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001380 void printString(StringRef Name, StringRef Value,
1381 bool ShouldSkipEmpty = true);
1382 void printMetadata(StringRef Name, const Metadata *MD,
1383 bool ShouldSkipNull = true);
1384 template <class IntTy>
1385 void printInt(StringRef Name, IntTy Int, bool ShouldSkipZero = true);
1386 void printBool(StringRef Name, bool Value);
1387 void printDIFlags(StringRef Name, unsigned Flags);
1388 template <class IntTy, class Stringifier>
1389 void printDwarfEnum(StringRef Name, IntTy Value, Stringifier toString,
1390 bool ShouldSkipZero = true);
1391};
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +00001392} // end namespace
1393
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001394void MDFieldPrinter::printTag(const DINode *N) {
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001395 Out << FS << "tag: ";
1396 if (const char *Tag = dwarf::TagString(N->getTag()))
1397 Out << Tag;
1398 else
1399 Out << N->getTag();
1400}
1401
1402void MDFieldPrinter::printString(StringRef Name, StringRef Value,
1403 bool ShouldSkipEmpty) {
1404 if (ShouldSkipEmpty && Value.empty())
1405 return;
1406
1407 Out << FS << Name << ": \"";
1408 PrintEscapedString(Value, Out);
1409 Out << "\"";
1410}
1411
Duncan P. N. Exon Smith61a09332015-02-06 22:27:22 +00001412static void writeMetadataAsOperand(raw_ostream &Out, const Metadata *MD,
1413 TypePrinting *TypePrinter,
1414 SlotTracker *Machine,
1415 const Module *Context) {
1416 if (!MD) {
1417 Out << "null";
1418 return;
1419 }
1420 WriteAsOperandInternal(Out, MD, TypePrinter, Machine, Context);
1421}
1422
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001423void MDFieldPrinter::printMetadata(StringRef Name, const Metadata *MD,
1424 bool ShouldSkipNull) {
1425 if (ShouldSkipNull && !MD)
Duncan P. N. Exon Smith79cf9702015-02-28 22:16:56 +00001426 return;
1427
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001428 Out << FS << Name << ": ";
1429 writeMetadataAsOperand(Out, MD, TypePrinter, Machine, Context);
1430}
1431
1432template <class IntTy>
1433void MDFieldPrinter::printInt(StringRef Name, IntTy Int, bool ShouldSkipZero) {
1434 if (ShouldSkipZero && !Int)
1435 return;
1436
1437 Out << FS << Name << ": " << Int;
1438}
1439
1440void MDFieldPrinter::printBool(StringRef Name, bool Value) {
1441 Out << FS << Name << ": " << (Value ? "true" : "false");
1442}
1443
1444void MDFieldPrinter::printDIFlags(StringRef Name, unsigned Flags) {
1445 if (!Flags)
1446 return;
1447
1448 Out << FS << Name << ": ";
1449
1450 SmallVector<unsigned, 8> SplitFlags;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001451 unsigned Extra = DINode::splitFlags(Flags, SplitFlags);
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001452
1453 FieldSeparator FlagsFS(" | ");
1454 for (unsigned F : SplitFlags) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001455 const char *StringF = DINode::getFlagString(F);
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001456 assert(StringF && "Expected valid flag");
1457 Out << FlagsFS << StringF;
1458 }
1459 if (Extra || SplitFlags.empty())
1460 Out << FlagsFS << Extra;
1461}
1462
1463template <class IntTy, class Stringifier>
1464void MDFieldPrinter::printDwarfEnum(StringRef Name, IntTy Value,
1465 Stringifier toString, bool ShouldSkipZero) {
1466 if (!Value)
1467 return;
1468
1469 Out << FS << Name << ": ";
1470 if (const char *S = toString(Value))
1471 Out << S;
1472 else
1473 Out << Value;
Duncan P. N. Exon Smith79cf9702015-02-28 22:16:56 +00001474}
1475
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001476static void writeGenericDINode(raw_ostream &Out, const GenericDINode *N,
1477 TypePrinting *TypePrinter, SlotTracker *Machine,
1478 const Module *Context) {
1479 Out << "!GenericDINode(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001480 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1481 Printer.printTag(N);
1482 Printer.printString("header", N->getHeader());
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00001483 if (N->getNumDwarfOperands()) {
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001484 Out << Printer.FS << "operands: {";
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00001485 FieldSeparator IFS;
1486 for (auto &I : N->dwarf_operands()) {
1487 Out << IFS;
Duncan P. N. Exon Smith61a09332015-02-06 22:27:22 +00001488 writeMetadataAsOperand(Out, I, TypePrinter, Machine, Context);
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00001489 }
1490 Out << "}";
1491 }
1492 Out << ")";
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +00001493}
1494
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001495static void writeDILocation(raw_ostream &Out, const DILocation *DL,
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +00001496 TypePrinting *TypePrinter, SlotTracker *Machine,
1497 const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001498 Out << "!DILocation(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001499 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
Duncan P. N. Exon Smith503cf3b2015-01-14 22:14:26 +00001500 // Always output the line, since 0 is a relevant and important value for it.
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001501 Printer.printInt("line", DL->getLine(), /* ShouldSkipZero */ false);
1502 Printer.printInt("column", DL->getColumn());
1503 Printer.printMetadata("scope", DL->getRawScope(), /* ShouldSkipNull */ false);
1504 Printer.printMetadata("inlinedAt", DL->getRawInlinedAt());
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +00001505 Out << ")";
1506}
1507
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001508static void writeDISubrange(raw_ostream &Out, const DISubrange *N,
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00001509 TypePrinting *, SlotTracker *, const Module *) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001510 Out << "!DISubrange(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001511 MDFieldPrinter Printer(Out);
1512 Printer.printInt("count", N->getCount(), /* ShouldSkipZero */ false);
Duncan P. N. Exon Smith5dcf6212015-04-07 00:39:59 +00001513 Printer.printInt("lowerBound", N->getLowerBound());
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00001514 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001515}
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00001516
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001517static void writeDIEnumerator(raw_ostream &Out, const DIEnumerator *N,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001518 TypePrinting *, SlotTracker *, const Module *) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001519 Out << "!DIEnumerator(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001520 MDFieldPrinter Printer(Out);
1521 Printer.printString("name", N->getName(), /* ShouldSkipEmpty */ false);
1522 Printer.printInt("value", N->getValue(), /* ShouldSkipZero */ false);
Duncan P. N. Exon Smith87754762015-02-13 01:14:11 +00001523 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001524}
Duncan P. N. Exon Smith87754762015-02-13 01:14:11 +00001525
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001526static void writeDIBasicType(raw_ostream &Out, const DIBasicType *N,
Duncan P. N. Exon Smith09e03f32015-02-13 01:14:58 +00001527 TypePrinting *, SlotTracker *, const Module *) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001528 Out << "!DIBasicType(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001529 MDFieldPrinter Printer(Out);
Duncan P. N. Exon Smith16d182a2015-02-28 23:21:38 +00001530 if (N->getTag() != dwarf::DW_TAG_base_type)
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001531 Printer.printTag(N);
1532 Printer.printString("name", N->getName());
1533 Printer.printInt("size", N->getSizeInBits());
1534 Printer.printInt("align", N->getAlignInBits());
1535 Printer.printDwarfEnum("encoding", N->getEncoding(),
1536 dwarf::AttributeEncodingString);
Duncan P. N. Exon Smith09e03f32015-02-13 01:14:58 +00001537 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001538}
Duncan P. N. Exon Smith09e03f32015-02-13 01:14:58 +00001539
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001540static void writeDIDerivedType(raw_ostream &Out, const DIDerivedType *N,
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001541 TypePrinting *TypePrinter, SlotTracker *Machine,
1542 const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001543 Out << "!DIDerivedType(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001544 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1545 Printer.printTag(N);
1546 Printer.printString("name", N->getName());
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001547 Printer.printMetadata("scope", N->getRawScope());
1548 Printer.printMetadata("file", N->getRawFile());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001549 Printer.printInt("line", N->getLine());
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001550 Printer.printMetadata("baseType", N->getRawBaseType(),
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001551 /* ShouldSkipNull */ false);
1552 Printer.printInt("size", N->getSizeInBits());
1553 Printer.printInt("align", N->getAlignInBits());
1554 Printer.printInt("offset", N->getOffsetInBits());
1555 Printer.printDIFlags("flags", N->getFlags());
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001556 Printer.printMetadata("extraData", N->getRawExtraData());
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001557 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001558}
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001559
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001560static void writeDICompositeType(raw_ostream &Out, const DICompositeType *N,
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001561 TypePrinting *TypePrinter,
1562 SlotTracker *Machine, const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001563 Out << "!DICompositeType(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001564 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1565 Printer.printTag(N);
1566 Printer.printString("name", N->getName());
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001567 Printer.printMetadata("scope", N->getRawScope());
1568 Printer.printMetadata("file", N->getRawFile());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001569 Printer.printInt("line", N->getLine());
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001570 Printer.printMetadata("baseType", N->getRawBaseType());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001571 Printer.printInt("size", N->getSizeInBits());
1572 Printer.printInt("align", N->getAlignInBits());
1573 Printer.printInt("offset", N->getOffsetInBits());
1574 Printer.printDIFlags("flags", N->getFlags());
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001575 Printer.printMetadata("elements", N->getRawElements());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001576 Printer.printDwarfEnum("runtimeLang", N->getRuntimeLang(),
1577 dwarf::LanguageString);
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001578 Printer.printMetadata("vtableHolder", N->getRawVTableHolder());
1579 Printer.printMetadata("templateParams", N->getRawTemplateParams());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001580 Printer.printString("identifier", N->getIdentifier());
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001581 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001582}
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001583
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001584static void writeDISubroutineType(raw_ostream &Out, const DISubroutineType *N,
Duncan P. N. Exon Smith54e2bc62015-02-13 01:22:59 +00001585 TypePrinting *TypePrinter,
1586 SlotTracker *Machine, const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001587 Out << "!DISubroutineType(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001588 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1589 Printer.printDIFlags("flags", N->getFlags());
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001590 Printer.printMetadata("types", N->getRawTypeArray(),
1591 /* ShouldSkipNull */ false);
Duncan P. N. Exon Smith54e2bc62015-02-13 01:22:59 +00001592 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001593}
Duncan P. N. Exon Smithf14b9c72015-02-13 01:19:14 +00001594
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001595static void writeDIFile(raw_ostream &Out, const DIFile *N, TypePrinting *,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001596 SlotTracker *, const Module *) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001597 Out << "!DIFile(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001598 MDFieldPrinter Printer(Out);
1599 Printer.printString("filename", N->getFilename(),
1600 /* ShouldSkipEmpty */ false);
1601 Printer.printString("directory", N->getDirectory(),
1602 /* ShouldSkipEmpty */ false);
Duncan P. N. Exon Smithf14b9c72015-02-13 01:19:14 +00001603 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001604}
Duncan P. N. Exon Smithf14b9c72015-02-13 01:19:14 +00001605
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001606static void writeDICompileUnit(raw_ostream &Out, const DICompileUnit *N,
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00001607 TypePrinting *TypePrinter, SlotTracker *Machine,
1608 const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001609 Out << "!DICompileUnit(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001610 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1611 Printer.printDwarfEnum("language", N->getSourceLanguage(),
1612 dwarf::LanguageString, /* ShouldSkipZero */ false);
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001613 Printer.printMetadata("file", N->getRawFile(), /* ShouldSkipNull */ false);
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001614 Printer.printString("producer", N->getProducer());
1615 Printer.printBool("isOptimized", N->isOptimized());
1616 Printer.printString("flags", N->getFlags());
1617 Printer.printInt("runtimeVersion", N->getRuntimeVersion(),
1618 /* ShouldSkipZero */ false);
1619 Printer.printString("splitDebugFilename", N->getSplitDebugFilename());
1620 Printer.printInt("emissionKind", N->getEmissionKind(),
1621 /* ShouldSkipZero */ false);
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001622 Printer.printMetadata("enums", N->getRawEnumTypes());
1623 Printer.printMetadata("retainedTypes", N->getRawRetainedTypes());
1624 Printer.printMetadata("subprograms", N->getRawSubprograms());
1625 Printer.printMetadata("globals", N->getRawGlobalVariables());
1626 Printer.printMetadata("imports", N->getRawImportedEntities());
Adrian Prantl1f599f92015-05-21 20:37:30 +00001627 Printer.printInt("dwoId", N->getDWOId());
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00001628 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001629}
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00001630
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001631static void writeDISubprogram(raw_ostream &Out, const DISubprogram *N,
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00001632 TypePrinting *TypePrinter, SlotTracker *Machine,
1633 const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001634 Out << "!DISubprogram(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001635 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1636 Printer.printString("name", N->getName());
1637 Printer.printString("linkageName", N->getLinkageName());
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001638 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
1639 Printer.printMetadata("file", N->getRawFile());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001640 Printer.printInt("line", N->getLine());
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001641 Printer.printMetadata("type", N->getRawType());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001642 Printer.printBool("isLocal", N->isLocalToUnit());
1643 Printer.printBool("isDefinition", N->isDefinition());
1644 Printer.printInt("scopeLine", N->getScopeLine());
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001645 Printer.printMetadata("containingType", N->getRawContainingType());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001646 Printer.printDwarfEnum("virtuality", N->getVirtuality(),
1647 dwarf::VirtualityString);
1648 Printer.printInt("virtualIndex", N->getVirtualIndex());
1649 Printer.printDIFlags("flags", N->getFlags());
1650 Printer.printBool("isOptimized", N->isOptimized());
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001651 Printer.printMetadata("function", N->getRawFunction());
1652 Printer.printMetadata("templateParams", N->getRawTemplateParams());
1653 Printer.printMetadata("declaration", N->getRawDeclaration());
1654 Printer.printMetadata("variables", N->getRawVariables());
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00001655 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001656}
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00001657
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001658static void writeDILexicalBlock(raw_ostream &Out, const DILexicalBlock *N,
1659 TypePrinting *TypePrinter, SlotTracker *Machine,
1660 const Module *Context) {
1661 Out << "!DILexicalBlock(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001662 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
Duncan P. N. Exon Smith0e202b92015-03-30 16:37:48 +00001663 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
1664 Printer.printMetadata("file", N->getRawFile());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001665 Printer.printInt("line", N->getLine());
1666 Printer.printInt("column", N->getColumn());
Duncan P. N. Exon Smitha96d4092015-02-13 01:29:28 +00001667 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001668}
Duncan P. N. Exon Smitha96d4092015-02-13 01:29:28 +00001669
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001670static void writeDILexicalBlockFile(raw_ostream &Out,
1671 const DILexicalBlockFile *N,
Duncan P. N. Exon Smith06a07022015-02-13 01:30:42 +00001672 TypePrinting *TypePrinter,
1673 SlotTracker *Machine,
1674 const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001675 Out << "!DILexicalBlockFile(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001676 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
Duncan P. N. Exon Smith0e202b92015-03-30 16:37:48 +00001677 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
1678 Printer.printMetadata("file", N->getRawFile());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001679 Printer.printInt("discriminator", N->getDiscriminator(),
1680 /* ShouldSkipZero */ false);
Duncan P. N. Exon Smith06a07022015-02-13 01:30:42 +00001681 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001682}
Duncan P. N. Exon Smith06a07022015-02-13 01:30:42 +00001683
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001684static void writeDINamespace(raw_ostream &Out, const DINamespace *N,
Duncan P. N. Exon Smithe1460002015-02-13 01:32:09 +00001685 TypePrinting *TypePrinter, SlotTracker *Machine,
1686 const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001687 Out << "!DINamespace(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001688 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1689 Printer.printString("name", N->getName());
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00001690 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
1691 Printer.printMetadata("file", N->getRawFile());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001692 Printer.printInt("line", N->getLine());
Duncan P. N. Exon Smithe1460002015-02-13 01:32:09 +00001693 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001694}
Duncan P. N. Exon Smithe1460002015-02-13 01:32:09 +00001695
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001696static void writeDITemplateTypeParameter(raw_ostream &Out,
1697 const DITemplateTypeParameter *N,
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00001698 TypePrinting *TypePrinter,
1699 SlotTracker *Machine,
1700 const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001701 Out << "!DITemplateTypeParameter(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001702 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1703 Printer.printString("name", N->getName());
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001704 Printer.printMetadata("type", N->getRawType(), /* ShouldSkipNull */ false);
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00001705 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001706}
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00001707
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001708static void writeDITemplateValueParameter(raw_ostream &Out,
1709 const DITemplateValueParameter *N,
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00001710 TypePrinting *TypePrinter,
1711 SlotTracker *Machine,
1712 const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001713 Out << "!DITemplateValueParameter(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001714 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
Duncan P. N. Exon Smith16d182a2015-02-28 23:21:38 +00001715 if (N->getTag() != dwarf::DW_TAG_template_value_parameter)
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001716 Printer.printTag(N);
1717 Printer.printString("name", N->getName());
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001718 Printer.printMetadata("type", N->getRawType());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001719 Printer.printMetadata("value", N->getValue(), /* ShouldSkipNull */ false);
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00001720 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001721}
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00001722
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001723static void writeDIGlobalVariable(raw_ostream &Out, const DIGlobalVariable *N,
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00001724 TypePrinting *TypePrinter,
1725 SlotTracker *Machine, const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001726 Out << "!DIGlobalVariable(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001727 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1728 Printer.printString("name", N->getName());
1729 Printer.printString("linkageName", N->getLinkageName());
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00001730 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
1731 Printer.printMetadata("file", N->getRawFile());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001732 Printer.printInt("line", N->getLine());
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00001733 Printer.printMetadata("type", N->getRawType());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001734 Printer.printBool("isLocal", N->isLocalToUnit());
1735 Printer.printBool("isDefinition", N->isDefinition());
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00001736 Printer.printMetadata("variable", N->getRawVariable());
1737 Printer.printMetadata("declaration", N->getRawStaticDataMemberDeclaration());
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00001738 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001739}
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00001740
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001741static void writeDILocalVariable(raw_ostream &Out, const DILocalVariable *N,
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00001742 TypePrinting *TypePrinter,
1743 SlotTracker *Machine, const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001744 Out << "!DILocalVariable(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001745 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1746 Printer.printTag(N);
1747 Printer.printString("name", N->getName());
1748 Printer.printInt("arg", N->getArg(),
1749 /* ShouldSkipZero */
1750 N->getTag() == dwarf::DW_TAG_auto_variable);
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00001751 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
1752 Printer.printMetadata("file", N->getRawFile());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001753 Printer.printInt("line", N->getLine());
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00001754 Printer.printMetadata("type", N->getRawType());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001755 Printer.printDIFlags("flags", N->getFlags());
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00001756 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001757}
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00001758
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001759static void writeDIExpression(raw_ostream &Out, const DIExpression *N,
Duncan P. N. Exon Smith0c5c0122015-02-13 01:42:09 +00001760 TypePrinting *TypePrinter, SlotTracker *Machine,
1761 const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001762 Out << "!DIExpression(";
Duncan P. N. Exon Smith0c5c0122015-02-13 01:42:09 +00001763 FieldSeparator FS;
1764 if (N->isValid()) {
1765 for (auto I = N->expr_op_begin(), E = N->expr_op_end(); I != E; ++I) {
1766 const char *OpStr = dwarf::OperationEncodingString(I->getOp());
1767 assert(OpStr && "Expected valid opcode");
1768
1769 Out << FS << OpStr;
1770 for (unsigned A = 0, AE = I->getNumArgs(); A != AE; ++A)
1771 Out << FS << I->getArg(A);
1772 }
1773 } else {
1774 for (const auto &I : N->getElements())
1775 Out << FS << I;
1776 }
1777 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001778}
Duncan P. N. Exon Smith0c5c0122015-02-13 01:42:09 +00001779
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001780static void writeDIObjCProperty(raw_ostream &Out, const DIObjCProperty *N,
Duncan P. N. Exon Smithd45ce962015-02-13 01:43:22 +00001781 TypePrinting *TypePrinter, SlotTracker *Machine,
1782 const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001783 Out << "!DIObjCProperty(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001784 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1785 Printer.printString("name", N->getName());
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00001786 Printer.printMetadata("file", N->getRawFile());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001787 Printer.printInt("line", N->getLine());
1788 Printer.printString("setter", N->getSetterName());
1789 Printer.printString("getter", N->getGetterName());
1790 Printer.printInt("attributes", N->getAttributes());
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00001791 Printer.printMetadata("type", N->getRawType());
Duncan P. N. Exon Smithd45ce962015-02-13 01:43:22 +00001792 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001793}
Duncan P. N. Exon Smithd45ce962015-02-13 01:43:22 +00001794
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001795static void writeDIImportedEntity(raw_ostream &Out, const DIImportedEntity *N,
Duncan P. N. Exon Smith1c931162015-02-13 01:46:02 +00001796 TypePrinting *TypePrinter,
1797 SlotTracker *Machine, const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001798 Out << "!DIImportedEntity(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001799 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1800 Printer.printTag(N);
1801 Printer.printString("name", N->getName());
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00001802 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
1803 Printer.printMetadata("entity", N->getRawEntity());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001804 Printer.printInt("line", N->getLine());
Duncan P. N. Exon Smith1c931162015-02-13 01:46:02 +00001805 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001806}
1807
Duncan P. N. Exon Smith1c931162015-02-13 01:46:02 +00001808
Duncan P. N. Exon Smitha6de6a42015-01-12 23:45:31 +00001809static void WriteMDNodeBodyInternal(raw_ostream &Out, const MDNode *Node,
1810 TypePrinting *TypePrinter,
1811 SlotTracker *Machine,
1812 const Module *Context) {
Duncan P. N. Exon Smith2bc00f42015-01-19 23:13:14 +00001813 if (Node->isDistinct())
Duncan P. N. Exon Smitha6de6a42015-01-12 23:45:31 +00001814 Out << "distinct ";
Duncan P. N. Exon Smith3d510662015-03-16 21:21:10 +00001815 else if (Node->isTemporary())
1816 Out << "<temporary!> "; // Handle broken code.
Duncan P. N. Exon Smitha6de6a42015-01-12 23:45:31 +00001817
Duncan P. N. Exon Smith2bc00f42015-01-19 23:13:14 +00001818 switch (Node->getMetadataID()) {
Duncan P. N. Exon Smitha6de6a42015-01-12 23:45:31 +00001819 default:
1820 llvm_unreachable("Expected uniquable MDNode");
Duncan P. N. Exon Smith2bc00f42015-01-19 23:13:14 +00001821#define HANDLE_MDNODE_LEAF(CLASS) \
Duncan P. N. Exon Smitha6de6a42015-01-12 23:45:31 +00001822 case Metadata::CLASS##Kind: \
Duncan P. N. Exon Smith2bc00f42015-01-19 23:13:14 +00001823 write##CLASS(Out, cast<CLASS>(Node), TypePrinter, Machine, Context); \
Duncan P. N. Exon Smitha6de6a42015-01-12 23:45:31 +00001824 break;
1825#include "llvm/IR/Metadata.def"
1826 }
1827}
1828
Chandler Carruthd48cdbf2014-01-09 02:29:41 +00001829// Full implementation of printing a Value as an operand with support for
1830// TypePrinting, etc.
Dan Gohman12dad632009-08-12 20:56:03 +00001831static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
Dan Gohman8061d9e2009-08-13 15:27:57 +00001832 TypePrinting *TypePrinter,
Dan Gohmana2489d12010-07-20 23:55:01 +00001833 SlotTracker *Machine,
1834 const Module *Context) {
Chris Lattner033935d2008-08-17 04:40:13 +00001835 if (V->hasName()) {
1836 PrintLLVMName(Out, V);
1837 return;
1838 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001839
Chris Lattner033935d2008-08-17 04:40:13 +00001840 const Constant *CV = dyn_cast<Constant>(V);
1841 if (CV && !isa<GlobalValue>(CV)) {
Dan Gohman8061d9e2009-08-13 15:27:57 +00001842 assert(TypePrinter && "Constants require TypePrinting!");
Dan Gohmana2489d12010-07-20 23:55:01 +00001843 WriteConstantInternal(Out, CV, *TypePrinter, Machine, Context);
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001844 return;
1845 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001846
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001847 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
Chris Lattner033935d2008-08-17 04:40:13 +00001848 Out << "asm ";
1849 if (IA->hasSideEffects())
1850 Out << "sideeffect ";
Dale Johannesen1cfb9582009-10-21 23:28:00 +00001851 if (IA->isAlignStack())
1852 Out << "alignstack ";
Chad Rosierd8c76102012-09-05 19:00:49 +00001853 // We don't emit the AD_ATT dialect as it's the assumed default.
1854 if (IA->getDialect() == InlineAsm::AD_Intel)
1855 Out << "inteldialect ";
Chris Lattner033935d2008-08-17 04:40:13 +00001856 Out << '"';
1857 PrintEscapedString(IA->getAsmString(), Out);
1858 Out << "\", \"";
1859 PrintEscapedString(IA->getConstraintString(), Out);
1860 Out << '"';
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001861 return;
1862 }
Devang Patel7428d8a2009-07-22 17:43:22 +00001863
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001864 if (auto *MD = dyn_cast<MetadataAsValue>(V)) {
1865 WriteAsOperandInternal(Out, MD->getMetadata(), TypePrinter, Machine,
1866 Context, /* FromValue */ true);
Devang Patel7428d8a2009-07-22 17:43:22 +00001867 return;
1868 }
1869
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001870 char Prefix = '%';
1871 int Slot;
Chris Lattner5b82a0a2011-08-03 06:15:41 +00001872 // If we have a SlotTracker, use it.
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001873 if (Machine) {
1874 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1875 Slot = Machine->getGlobalSlot(GV);
1876 Prefix = '@';
1877 } else {
1878 Slot = Machine->getLocalSlot(V);
Andrew Trickec4b6e72011-09-30 19:48:58 +00001879
Chris Lattner5b82a0a2011-08-03 06:15:41 +00001880 // If the local value didn't succeed, then we may be referring to a value
1881 // from a different function. Translate it, as this can happen when using
1882 // address of blocks.
1883 if (Slot == -1)
1884 if ((Machine = createSlotTracker(V))) {
1885 Slot = Machine->getLocalSlot(V);
1886 delete Machine;
1887 }
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001888 }
Chris Lattner5b82a0a2011-08-03 06:15:41 +00001889 } else if ((Machine = createSlotTracker(V))) {
1890 // Otherwise, create one to get the # and then destroy it.
1891 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1892 Slot = Machine->getGlobalSlot(GV);
1893 Prefix = '@';
Chris Lattnera2d810d2006-01-25 22:26:05 +00001894 } else {
Chris Lattner5b82a0a2011-08-03 06:15:41 +00001895 Slot = Machine->getLocalSlot(V);
Chris Lattnerd84bb632002-04-16 21:36:08 +00001896 }
Chris Lattner5b82a0a2011-08-03 06:15:41 +00001897 delete Machine;
Craig Topperc6207612014-04-09 06:08:46 +00001898 Machine = nullptr;
Chris Lattner5b82a0a2011-08-03 06:15:41 +00001899 } else {
1900 Slot = -1;
Chris Lattnerd84bb632002-04-16 21:36:08 +00001901 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001902
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001903 if (Slot != -1)
1904 Out << Prefix << Slot;
1905 else
1906 Out << "<badref>";
Chris Lattnerd84bb632002-04-16 21:36:08 +00001907}
1908
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001909static void WriteAsOperandInternal(raw_ostream &Out, const Metadata *MD,
1910 TypePrinting *TypePrinter,
1911 SlotTracker *Machine, const Module *Context,
1912 bool FromValue) {
1913 if (const MDNode *N = dyn_cast<MDNode>(MD)) {
1914 if (!Machine)
1915 Machine = new SlotTracker(Context);
1916 int Slot = Machine->getMetadataSlot(N);
1917 if (Slot == -1)
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +00001918 // Give the pointer value instead of "badref", since this comes up all
1919 // the time when debugging.
1920 Out << "<" << N << ">";
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001921 else
1922 Out << '!' << Slot;
1923 return;
1924 }
1925
1926 if (const MDString *MDS = dyn_cast<MDString>(MD)) {
1927 Out << "!\"";
1928 PrintEscapedString(MDS->getString(), Out);
1929 Out << '"';
1930 return;
1931 }
1932
1933 auto *V = cast<ValueAsMetadata>(MD);
1934 assert(TypePrinter && "TypePrinter required for metadata values");
1935 assert((FromValue || !isa<LocalAsMetadata>(V)) &&
1936 "Unexpected function-local metadata outside of value argument");
1937
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001938 TypePrinter->print(V->getValue()->getType(), Out);
1939 Out << ' ';
1940 WriteAsOperandInternal(Out, V->getValue(), TypePrinter, Machine, Context);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001941}
1942
Benjamin Kramerba05a782015-03-17 19:53:41 +00001943namespace {
1944class AssemblyWriter {
1945 formatted_raw_ostream &Out;
1946 const Module *TheModule;
1947 std::unique_ptr<SlotTracker> ModuleSlotTracker;
1948 SlotTracker &Machine;
1949 TypePrinting TypePrinter;
1950 AssemblyAnnotationWriter *AnnotationWriter;
1951 SetVector<const Comdat *> Comdats;
Duncan P. N. Exon Smith89010d82015-04-15 01:36:30 +00001952 bool ShouldPreserveUseListOrder;
Benjamin Kramerba05a782015-03-17 19:53:41 +00001953 UseListOrderStack UseListOrders;
Duncan P. N. Exon Smithe30f10e2015-04-24 21:03:05 +00001954 SmallVector<StringRef, 8> MDNames;
Benjamin Kramerba05a782015-03-17 19:53:41 +00001955
1956public:
1957 /// Construct an AssemblyWriter with an external SlotTracker
Duncan P. N. Exon Smith89010d82015-04-15 01:36:30 +00001958 AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac, const Module *M,
1959 AssemblyAnnotationWriter *AAW,
1960 bool ShouldPreserveUseListOrder = false);
Benjamin Kramerba05a782015-03-17 19:53:41 +00001961
1962 /// Construct an AssemblyWriter with an internally allocated SlotTracker
1963 AssemblyWriter(formatted_raw_ostream &o, const Module *M,
Duncan P. N. Exon Smith89010d82015-04-15 01:36:30 +00001964 AssemblyAnnotationWriter *AAW,
1965 bool ShouldPreserveUseListOrder = false);
Benjamin Kramerba05a782015-03-17 19:53:41 +00001966
1967 void printMDNodeBody(const MDNode *MD);
1968 void printNamedMDNode(const NamedMDNode *NMD);
1969
1970 void printModule(const Module *M);
1971
1972 void writeOperand(const Value *Op, bool PrintType);
1973 void writeParamOperand(const Value *Operand, AttributeSet Attrs,unsigned Idx);
1974 void writeAtomic(AtomicOrdering Ordering, SynchronizationScope SynchScope);
1975 void writeAtomicCmpXchg(AtomicOrdering SuccessOrdering,
1976 AtomicOrdering FailureOrdering,
1977 SynchronizationScope SynchScope);
1978
1979 void writeAllMDNodes();
1980 void writeMDNode(unsigned Slot, const MDNode *Node);
1981 void writeAllAttributeGroups();
1982
1983 void printTypeIdentities();
1984 void printGlobal(const GlobalVariable *GV);
1985 void printAlias(const GlobalAlias *GV);
1986 void printComdat(const Comdat *C);
1987 void printFunction(const Function *F);
1988 void printArgument(const Argument *FA, AttributeSet Attrs, unsigned Idx);
1989 void printBasicBlock(const BasicBlock *BB);
1990 void printInstructionLine(const Instruction &I);
1991 void printInstruction(const Instruction &I);
1992
1993 void printUseListOrder(const UseListOrder &Order);
1994 void printUseLists(const Function *F);
1995
1996private:
1997 void init();
1998
Duncan P. N. Exon Smith86979272015-04-24 20:59:52 +00001999 /// \brief Print out metadata attachments.
2000 void printMetadataAttachments(
Duncan P. N. Exon Smithd3e4c2a2015-04-24 21:06:21 +00002001 const SmallVectorImpl<std::pair<unsigned, MDNode *>> &MDs,
2002 StringRef Separator);
Duncan P. N. Exon Smith86979272015-04-24 20:59:52 +00002003
Benjamin Kramerba05a782015-03-17 19:53:41 +00002004 // printInfoComment - Print a little comment after the instruction indicating
2005 // which slot it occupies.
2006 void printInfoComment(const Value &V);
Igor Laevsky2aa8caf2015-05-05 13:20:42 +00002007
2008 // printGCRelocateComment - print comment after call to the gc.relocate
2009 // intrinsic indicating base and derived pointer names.
2010 void printGCRelocateComment(const Value &V);
Benjamin Kramerba05a782015-03-17 19:53:41 +00002011};
2012} // namespace
2013
Daniel Maleaded9f932013-05-08 20:38:31 +00002014void AssemblyWriter::init() {
David Majnemerdad0a642014-06-27 18:19:56 +00002015 if (!TheModule)
2016 return;
2017 TypePrinter.incorporateTypes(*TheModule);
2018 for (const Function &F : *TheModule)
2019 if (const Comdat *C = F.getComdat())
2020 Comdats.insert(C);
2021 for (const GlobalVariable &GV : TheModule->globals())
2022 if (const Comdat *C = GV.getComdat())
2023 Comdats.insert(C);
Daniel Maleaded9f932013-05-08 20:38:31 +00002024}
Chris Lattner2e9fee42001-07-12 23:35:26 +00002025
Daniel Maleaded9f932013-05-08 20:38:31 +00002026AssemblyWriter::AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac,
Duncan P. N. Exon Smith89010d82015-04-15 01:36:30 +00002027 const Module *M, AssemblyAnnotationWriter *AAW,
2028 bool ShouldPreserveUseListOrder)
2029 : Out(o), TheModule(M), Machine(Mac), AnnotationWriter(AAW),
2030 ShouldPreserveUseListOrder(ShouldPreserveUseListOrder) {
Daniel Maleaded9f932013-05-08 20:38:31 +00002031 init();
2032}
Chris Lattner2f7c9632001-06-06 20:29:01 +00002033
Daniel Maleaded9f932013-05-08 20:38:31 +00002034AssemblyWriter::AssemblyWriter(formatted_raw_ostream &o, const Module *M,
Duncan P. N. Exon Smith89010d82015-04-15 01:36:30 +00002035 AssemblyAnnotationWriter *AAW,
2036 bool ShouldPreserveUseListOrder)
2037 : Out(o), TheModule(M), ModuleSlotTracker(createSlotTracker(M)),
2038 Machine(*ModuleSlotTracker), AnnotationWriter(AAW),
2039 ShouldPreserveUseListOrder(ShouldPreserveUseListOrder) {
Daniel Maleaded9f932013-05-08 20:38:31 +00002040 init();
2041}
Andrew Trickec4b6e72011-09-30 19:48:58 +00002042
Chris Lattner78e2e8b2006-12-06 06:24:27 +00002043void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType) {
Craig Topperc6207612014-04-09 06:08:46 +00002044 if (!Operand) {
Chris Lattner08f7d0c2005-02-24 16:58:29 +00002045 Out << "<null operand!>";
Chris Lattnerb419a1e2009-12-31 02:33:14 +00002046 return;
Chris Lattner08f7d0c2005-02-24 16:58:29 +00002047 }
Chris Lattnerb419a1e2009-12-31 02:33:14 +00002048 if (PrintType) {
2049 TypePrinter.print(Operand->getType(), Out);
2050 Out << ' ';
2051 }
Dan Gohmana2489d12010-07-20 23:55:01 +00002052 WriteAsOperandInternal(Out, Operand, &TypePrinter, &Machine, TheModule);
Chris Lattner2f7c9632001-06-06 20:29:01 +00002053}
2054
Eli Friedmanfee02c62011-07-25 23:16:38 +00002055void AssemblyWriter::writeAtomic(AtomicOrdering Ordering,
2056 SynchronizationScope SynchScope) {
2057 if (Ordering == NotAtomic)
2058 return;
2059
2060 switch (SynchScope) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00002061 case SingleThread: Out << " singlethread"; break;
2062 case CrossThread: break;
2063 }
2064
2065 switch (Ordering) {
2066 default: Out << " <bad ordering " << int(Ordering) << ">"; break;
2067 case Unordered: Out << " unordered"; break;
2068 case Monotonic: Out << " monotonic"; break;
2069 case Acquire: Out << " acquire"; break;
2070 case Release: Out << " release"; break;
2071 case AcquireRelease: Out << " acq_rel"; break;
2072 case SequentiallyConsistent: Out << " seq_cst"; break;
2073 }
2074}
2075
Tim Northovere94a5182014-03-11 10:48:52 +00002076void AssemblyWriter::writeAtomicCmpXchg(AtomicOrdering SuccessOrdering,
2077 AtomicOrdering FailureOrdering,
2078 SynchronizationScope SynchScope) {
2079 assert(SuccessOrdering != NotAtomic && FailureOrdering != NotAtomic);
2080
2081 switch (SynchScope) {
2082 case SingleThread: Out << " singlethread"; break;
2083 case CrossThread: break;
2084 }
2085
2086 switch (SuccessOrdering) {
2087 default: Out << " <bad ordering " << int(SuccessOrdering) << ">"; break;
2088 case Unordered: Out << " unordered"; break;
2089 case Monotonic: Out << " monotonic"; break;
2090 case Acquire: Out << " acquire"; break;
2091 case Release: Out << " release"; break;
2092 case AcquireRelease: Out << " acq_rel"; break;
2093 case SequentiallyConsistent: Out << " seq_cst"; break;
2094 }
2095
2096 switch (FailureOrdering) {
2097 default: Out << " <bad ordering " << int(FailureOrdering) << ">"; break;
2098 case Unordered: Out << " unordered"; break;
2099 case Monotonic: Out << " monotonic"; break;
2100 case Acquire: Out << " acquire"; break;
2101 case Release: Out << " release"; break;
2102 case AcquireRelease: Out << " acq_rel"; break;
2103 case SequentiallyConsistent: Out << " seq_cst"; break;
2104 }
2105}
2106
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002107void AssemblyWriter::writeParamOperand(const Value *Operand,
Bill Wendling749a43d2012-12-30 13:50:49 +00002108 AttributeSet Attrs, unsigned Idx) {
Craig Topperc6207612014-04-09 06:08:46 +00002109 if (!Operand) {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00002110 Out << "<null operand!>";
Chris Lattnerb419a1e2009-12-31 02:33:14 +00002111 return;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00002112 }
Chris Lattnerb419a1e2009-12-31 02:33:14 +00002113
2114 // Print the type
2115 TypePrinter.print(Operand->getType(), Out);
2116 // Print parameter attributes list
Bill Wendling749a43d2012-12-30 13:50:49 +00002117 if (Attrs.hasAttributes(Idx))
2118 Out << ' ' << Attrs.getAsString(Idx);
Chris Lattnerb419a1e2009-12-31 02:33:14 +00002119 Out << ' ';
2120 // Print the operand
Dan Gohmana2489d12010-07-20 23:55:01 +00002121 WriteAsOperandInternal(Out, Operand, &TypePrinter, &Machine, TheModule);
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00002122}
Chris Lattner2f7c9632001-06-06 20:29:01 +00002123
Chris Lattner7bfee412001-10-29 16:05:51 +00002124void AssemblyWriter::printModule(const Module *M) {
Bill Wendling829b4782013-02-11 08:43:33 +00002125 Machine.initialize();
2126
Duncan P. N. Exon Smith89010d82015-04-15 01:36:30 +00002127 if (ShouldPreserveUseListOrder)
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +00002128 UseListOrders = predictUseListOrder(M);
2129
Chris Lattner4d8689e2005-03-02 23:12:40 +00002130 if (!M->getModuleIdentifier().empty() &&
Misha Brukmanb1c93172005-04-21 23:48:37 +00002131 // Don't print the ID if it will start a new line (which would
Chris Lattner4d8689e2005-03-02 23:12:40 +00002132 // require a comment char before it).
2133 M->getModuleIdentifier().find('\n') == std::string::npos)
2134 Out << "; ModuleID = '" << M->getModuleIdentifier() << "'\n";
2135
Rafael Espindolaf863ee22014-02-25 20:01:08 +00002136 const std::string &DL = M->getDataLayoutStr();
2137 if (!DL.empty())
2138 Out << "target datalayout = \"" << DL << "\"\n";
Reid Spencer48f98c82004-07-25 21:44:54 +00002139 if (!M->getTargetTriple().empty())
Reid Spencerffec7df2004-07-25 21:29:43 +00002140 Out << "target triple = \"" << M->getTargetTriple() << "\"\n";
Misha Brukmanb1c93172005-04-21 23:48:37 +00002141
Chris Lattnereef2fe72006-01-24 04:13:11 +00002142 if (!M->getModuleInlineAsm().empty()) {
Dan Gohman69273e62009-08-12 23:54:22 +00002143 Out << '\n';
Benjamin Kramerbbd05a22015-06-07 13:59:33 +00002144
2145 // Split the string into lines, to make it easier to read the .ll file.
Benjamin Kramerf1cfc422015-06-08 18:58:57 +00002146 StringRef Asm = M->getModuleInlineAsm();
Benjamin Kramerbbd05a22015-06-07 13:59:33 +00002147 do {
2148 StringRef Front;
2149 std::tie(Front, Asm) = Asm.split('\n');
2150
Chris Lattnerefaf35d2006-01-24 00:45:30 +00002151 // We found a newline, print the portion of the asm string from the
2152 // last newline up to this newline.
2153 Out << "module asm \"";
Benjamin Kramerbbd05a22015-06-07 13:59:33 +00002154 PrintEscapedString(Front, Out);
Chris Lattnerefaf35d2006-01-24 00:45:30 +00002155 Out << "\"\n";
Benjamin Kramerbbd05a22015-06-07 13:59:33 +00002156 } while (!Asm.empty());
Chris Lattner6ed87bd2006-01-23 23:03:36 +00002157 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002158
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002159 printTypeIdentities();
Misha Brukmanb1c93172005-04-21 23:48:37 +00002160
David Majnemerdad0a642014-06-27 18:19:56 +00002161 // Output all comdats.
2162 if (!Comdats.empty())
2163 Out << '\n';
2164 for (const Comdat *C : Comdats) {
2165 printComdat(C);
2166 if (C != Comdats.back())
2167 Out << '\n';
2168 }
2169
Dan Gohman69273e62009-08-12 23:54:22 +00002170 // Output all globals.
2171 if (!M->global_empty()) Out << '\n';
Yaron Kerenf3cf9d12015-06-13 17:50:47 +00002172 for (const GlobalVariable &GV : M->globals()) {
2173 printGlobal(&GV); Out << '\n';
Duncan Sands66fc0e62012-09-12 09:55:51 +00002174 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002175
Chris Lattnerd2747052007-04-26 02:24:10 +00002176 // Output all aliases.
2177 if (!M->alias_empty()) Out << "\n";
Yaron Kerenf3cf9d12015-06-13 17:50:47 +00002178 for (const GlobalAlias &GA : M->aliases())
2179 printAlias(&GA);
Chris Lattnerfee714f2001-09-07 16:36:04 +00002180
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +00002181 // Output global use-lists.
2182 printUseLists(nullptr);
2183
Chris Lattner2cdd49d2004-09-14 05:06:58 +00002184 // Output all of the functions.
Yaron Kerenf3cf9d12015-06-13 17:50:47 +00002185 for (const Function &F : *M)
2186 printFunction(&F);
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +00002187 assert(UseListOrders.empty() && "All use-lists should have been consumed");
Devang Patel983c6b12009-07-08 21:44:25 +00002188
Bill Wendling829b4782013-02-11 08:43:33 +00002189 // Output all attribute groups.
Bill Wendling04945972013-04-29 23:48:06 +00002190 if (!Machine.as_empty()) {
Bill Wendling829b4782013-02-11 08:43:33 +00002191 Out << '\n';
2192 writeAllAttributeGroups();
2193 }
2194
Devang Patel23e68302009-07-29 22:04:47 +00002195 // Output named metadata.
Dan Gohman69273e62009-08-12 23:54:22 +00002196 if (!M->named_metadata_empty()) Out << '\n';
Andrew Trickec4b6e72011-09-30 19:48:58 +00002197
Yaron Kerenf3cf9d12015-06-13 17:50:47 +00002198 for (const NamedMDNode &Node : M->named_metadata())
2199 printNamedMDNode(&Node);
Devang Patel23e68302009-07-29 22:04:47 +00002200
2201 // Output metadata.
Chris Lattnercf4a76e2009-12-31 02:20:11 +00002202 if (!Machine.mdn_empty()) {
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002203 Out << '\n';
2204 writeAllMDNodes();
2205 }
Chris Lattnerfee714f2001-09-07 16:36:04 +00002206}
2207
Filipe Cabecinhas62431b12015-06-02 21:25:08 +00002208static void printMetadataIdentifier(StringRef Name,
2209 formatted_raw_ostream &Out) {
Nick Lewycky5bcbd732011-06-15 06:37:58 +00002210 if (Name.empty()) {
2211 Out << "<empty name> ";
2212 } else {
Filipe Cabecinhas1ac0d2d2015-06-02 21:25:00 +00002213 if (isalpha(static_cast<unsigned char>(Name[0])) || Name[0] == '-' ||
2214 Name[0] == '$' || Name[0] == '.' || Name[0] == '_')
Nick Lewycky5bcbd732011-06-15 06:37:58 +00002215 Out << Name[0];
2216 else
2217 Out << '\\' << hexdigit(Name[0] >> 4) << hexdigit(Name[0] & 0x0F);
2218 for (unsigned i = 1, e = Name.size(); i != e; ++i) {
2219 unsigned char C = Name[i];
Guy Benyei83c74e92013-02-12 21:21:59 +00002220 if (isalnum(static_cast<unsigned char>(C)) || C == '-' || C == '$' ||
2221 C == '.' || C == '_')
Nick Lewycky5bcbd732011-06-15 06:37:58 +00002222 Out << C;
2223 else
2224 Out << '\\' << hexdigit(C >> 4) << hexdigit(C & 0x0F);
2225 }
2226 }
Filipe Cabecinhas62431b12015-06-02 21:25:08 +00002227}
2228
2229void AssemblyWriter::printNamedMDNode(const NamedMDNode *NMD) {
2230 Out << '!';
2231 printMetadataIdentifier(NMD->getName(), Out);
Nick Lewycky5bcbd732011-06-15 06:37:58 +00002232 Out << " = !{";
Chris Lattner2c48c642009-12-31 01:54:05 +00002233 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
Filipe Cabecinhas1ac0d2d2015-06-02 21:25:00 +00002234 if (i)
2235 Out << ", ";
Dan Gohman0a54da22010-09-09 20:53:58 +00002236 int Slot = Machine.getMetadataSlot(NMD->getOperand(i));
2237 if (Slot == -1)
2238 Out << "<badref>";
2239 else
2240 Out << '!' << Slot;
Chris Lattner2c48c642009-12-31 01:54:05 +00002241 }
2242 Out << "}\n";
2243}
2244
Dan Gohmane2745262009-08-12 17:23:50 +00002245static void PrintLinkage(GlobalValue::LinkageTypes LT,
2246 formatted_raw_ostream &Out) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00002247 switch (LT) {
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00002248 case GlobalValue::ExternalLinkage: break;
2249 case GlobalValue::PrivateLinkage: Out << "private "; break;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00002250 case GlobalValue::InternalLinkage: Out << "internal "; break;
2251 case GlobalValue::LinkOnceAnyLinkage: Out << "linkonce "; break;
2252 case GlobalValue::LinkOnceODRLinkage: Out << "linkonce_odr "; break;
2253 case GlobalValue::WeakAnyLinkage: Out << "weak "; break;
2254 case GlobalValue::WeakODRLinkage: Out << "weak_odr "; break;
2255 case GlobalValue::CommonLinkage: Out << "common "; break;
2256 case GlobalValue::AppendingLinkage: Out << "appending "; break;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00002257 case GlobalValue::ExternalWeakLinkage: Out << "extern_weak "; break;
Chris Lattner184f1be2009-04-13 05:44:34 +00002258 case GlobalValue::AvailableExternallyLinkage:
2259 Out << "available_externally ";
2260 break;
Chris Lattner3a36d2c2008-08-19 05:06:27 +00002261 }
2262}
Duncan Sands12da8ce2009-03-07 15:45:40 +00002263
Chris Lattner3a36d2c2008-08-19 05:06:27 +00002264static void PrintVisibility(GlobalValue::VisibilityTypes Vis,
Dan Gohmane2745262009-08-12 17:23:50 +00002265 formatted_raw_ostream &Out) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00002266 switch (Vis) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00002267 case GlobalValue::DefaultVisibility: break;
2268 case GlobalValue::HiddenVisibility: Out << "hidden "; break;
2269 case GlobalValue::ProtectedVisibility: Out << "protected "; break;
2270 }
2271}
2272
Nico Rieck7157bb72014-01-14 15:22:47 +00002273static void PrintDLLStorageClass(GlobalValue::DLLStorageClassTypes SCT,
2274 formatted_raw_ostream &Out) {
2275 switch (SCT) {
2276 case GlobalValue::DefaultStorageClass: break;
2277 case GlobalValue::DLLImportStorageClass: Out << "dllimport "; break;
2278 case GlobalValue::DLLExportStorageClass: Out << "dllexport "; break;
2279 }
2280}
2281
Hans Wennborgcbe34b42012-06-23 11:37:03 +00002282static void PrintThreadLocalModel(GlobalVariable::ThreadLocalMode TLM,
2283 formatted_raw_ostream &Out) {
2284 switch (TLM) {
2285 case GlobalVariable::NotThreadLocal:
2286 break;
2287 case GlobalVariable::GeneralDynamicTLSModel:
2288 Out << "thread_local ";
2289 break;
2290 case GlobalVariable::LocalDynamicTLSModel:
2291 Out << "thread_local(localdynamic) ";
2292 break;
2293 case GlobalVariable::InitialExecTLSModel:
2294 Out << "thread_local(initialexec) ";
2295 break;
2296 case GlobalVariable::LocalExecTLSModel:
2297 Out << "thread_local(localexec) ";
2298 break;
2299 }
2300}
2301
Rafael Espindola83a362c2015-01-06 22:55:16 +00002302static void maybePrintComdat(formatted_raw_ostream &Out,
2303 const GlobalObject &GO) {
2304 const Comdat *C = GO.getComdat();
2305 if (!C)
2306 return;
2307
2308 if (isa<GlobalVariable>(GO))
2309 Out << ',';
2310 Out << " comdat";
2311
2312 if (GO.getName() == C->getName())
2313 return;
2314
2315 Out << '(';
2316 PrintLLVMName(Out, C->getName(), ComdatPrefix);
2317 Out << ')';
2318}
2319
Chris Lattner7bfee412001-10-29 16:05:51 +00002320void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Dan Gohman5ded1422010-01-29 23:12:36 +00002321 if (GV->isMaterializable())
2322 Out << "; Materializable\n";
2323
Dan Gohmana2489d12010-07-20 23:55:01 +00002324 WriteAsOperandInternal(Out, GV, &TypePrinter, &Machine, GV->getParent());
Dan Gohman466876b2009-08-12 23:32:33 +00002325 Out << " = ";
Chris Lattner37798642001-09-18 04:01:05 +00002326
Chris Lattner1508d3f2008-08-19 05:16:28 +00002327 if (!GV->hasInitializer() && GV->hasExternalLinkage())
2328 Out << "external ";
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002329
Chris Lattner1508d3f2008-08-19 05:16:28 +00002330 PrintLinkage(GV->getLinkage(), Out);
2331 PrintVisibility(GV->getVisibility(), Out);
Nico Rieck7157bb72014-01-14 15:22:47 +00002332 PrintDLLStorageClass(GV->getDLLStorageClass(), Out);
Hans Wennborgcbe34b42012-06-23 11:37:03 +00002333 PrintThreadLocalModel(GV->getThreadLocalMode(), Out);
Rafael Espindola42a4c9f2014-06-06 01:20:28 +00002334 if (GV->hasUnnamedAddr())
2335 Out << "unnamed_addr ";
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +00002336
Chris Lattnerac161bf2009-01-02 07:01:27 +00002337 if (unsigned AddressSpace = GV->getType()->getAddressSpace())
2338 Out << "addrspace(" << AddressSpace << ") ";
Michael Gottesman27e7ef32013-02-05 05:57:38 +00002339 if (GV->isExternallyInitialized()) Out << "externally_initialized ";
Misha Brukmana6619a92004-06-21 21:53:56 +00002340 Out << (GV->isConstant() ? "constant " : "global ");
Chris Lattnere101c442009-02-28 21:26:53 +00002341 TypePrinter.print(GV->getType()->getElementType(), Out);
Chris Lattner37798642001-09-18 04:01:05 +00002342
Dan Gohman81313fd2008-09-14 17:21:12 +00002343 if (GV->hasInitializer()) {
2344 Out << ' ';
Devang Patel983c6b12009-07-08 21:44:25 +00002345 writeOperand(GV->getInitializer(), false);
Dan Gohman81313fd2008-09-14 17:21:12 +00002346 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002347
Chris Lattner9380b812010-07-07 23:16:37 +00002348 if (GV->hasSection()) {
2349 Out << ", section \"";
2350 PrintEscapedString(GV->getSection(), Out);
2351 Out << '"';
2352 }
Rafael Espindola83a362c2015-01-06 22:55:16 +00002353 maybePrintComdat(Out, *GV);
Chris Lattner4b96c542005-11-12 00:10:19 +00002354 if (GV->getAlignment())
Chris Lattnerf8a974d2005-11-06 06:48:53 +00002355 Out << ", align " << GV->getAlignment();
Anton Korobeynikova97b6942007-04-25 14:27:10 +00002356
Chris Lattner113f4f42002-06-25 16:13:24 +00002357 printInfoComment(*GV);
Chris Lattnerda975502001-09-10 07:58:01 +00002358}
2359
Anton Korobeynikova97b6942007-04-25 14:27:10 +00002360void AssemblyWriter::printAlias(const GlobalAlias *GA) {
Dan Gohman5ded1422010-01-29 23:12:36 +00002361 if (GA->isMaterializable())
2362 Out << "; Materializable\n";
2363
Rafael Espindola54fc2982015-06-17 17:53:31 +00002364 WriteAsOperandInternal(Out, GA, &TypePrinter, &Machine, GA->getParent());
2365 Out << " = ";
2366
Rafael Espindola464fe022014-07-30 22:51:54 +00002367 PrintLinkage(GA->getLinkage(), Out);
Chris Lattner3a36d2c2008-08-19 05:06:27 +00002368 PrintVisibility(GA->getVisibility(), Out);
Nico Rieck7157bb72014-01-14 15:22:47 +00002369 PrintDLLStorageClass(GA->getDLLStorageClass(), Out);
Rafael Espindola59f7eba2014-05-28 18:15:43 +00002370 PrintThreadLocalModel(GA->getThreadLocalMode(), Out);
Rafael Espindola42a4c9f2014-06-06 01:20:28 +00002371 if (GA->hasUnnamedAddr())
2372 Out << "unnamed_addr ";
Anton Korobeynikova97b6942007-04-25 14:27:10 +00002373
2374 Out << "alias ";
2375
Anton Korobeynikov546ea7e2007-04-29 18:02:48 +00002376 const Constant *Aliasee = GA->getAliasee();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002377
Craig Topperc6207612014-04-09 06:08:46 +00002378 if (!Aliasee) {
Rafael Espindola64c1e182014-06-03 02:41:57 +00002379 TypePrinter.print(GA->getType(), Out);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002380 Out << " <<NULL ALIASEE>>";
Jay Foad92c19132011-08-01 12:48:54 +00002381 } else {
Jay Foad26db79d2011-08-01 12:29:14 +00002382 writeOperand(Aliasee, !isa<ConstantExpr>(Aliasee));
Jay Foad92c19132011-08-01 12:48:54 +00002383 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002384
Anton Korobeynikova97b6942007-04-25 14:27:10 +00002385 printInfoComment(*GA);
Chris Lattner1508d3f2008-08-19 05:16:28 +00002386 Out << '\n';
Anton Korobeynikova97b6942007-04-25 14:27:10 +00002387}
2388
David Majnemerdad0a642014-06-27 18:19:56 +00002389void AssemblyWriter::printComdat(const Comdat *C) {
2390 C->print(Out);
2391}
2392
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002393void AssemblyWriter::printTypeIdentities() {
2394 if (TypePrinter.NumberedTypes.empty() &&
2395 TypePrinter.NamedTypes.empty())
2396 return;
Andrew Trickec4b6e72011-09-30 19:48:58 +00002397
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002398 Out << '\n';
Andrew Trickec4b6e72011-09-30 19:48:58 +00002399
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002400 // We know all the numbers that each type is used and we know that it is a
2401 // dense assignment. Convert the map to an index table.
2402 std::vector<StructType*> NumberedTypes(TypePrinter.NumberedTypes.size());
Andrew Trickec4b6e72011-09-30 19:48:58 +00002403 for (DenseMap<StructType*, unsigned>::iterator I =
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002404 TypePrinter.NumberedTypes.begin(), E = TypePrinter.NumberedTypes.end();
2405 I != E; ++I) {
2406 assert(I->second < NumberedTypes.size() && "Didn't get a dense numbering?");
2407 NumberedTypes[I->second] = I->first;
2408 }
Andrew Trickec4b6e72011-09-30 19:48:58 +00002409
Chris Lattner3243ea12009-03-01 00:03:38 +00002410 // Emit all numbered types.
2411 for (unsigned i = 0, e = NumberedTypes.size(); i != e; ++i) {
Dan Gohman466876b2009-08-12 23:32:33 +00002412 Out << '%' << i << " = type ";
Andrew Trickec4b6e72011-09-30 19:48:58 +00002413
Chris Lattner3243ea12009-03-01 00:03:38 +00002414 // Make sure we print out at least one level of the type structure, so
2415 // that we do not get %2 = type %2
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002416 TypePrinter.printStructBody(NumberedTypes[i], Out);
Dan Gohman69273e62009-08-12 23:54:22 +00002417 Out << '\n';
Chris Lattner3243ea12009-03-01 00:03:38 +00002418 }
Andrew Trickec4b6e72011-09-30 19:48:58 +00002419
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002420 for (unsigned i = 0, e = TypePrinter.NamedTypes.size(); i != e; ++i) {
2421 PrintLLVMName(Out, TypePrinter.NamedTypes[i]->getName(), LocalPrefix);
Chris Lattner1508d3f2008-08-19 05:16:28 +00002422 Out << " = type ";
Reid Spencere7e96712004-05-25 08:53:40 +00002423
2424 // Make sure we print out at least one level of the type structure, so
2425 // that we do not get %FILE = type %FILE
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002426 TypePrinter.printStructBody(TypePrinter.NamedTypes[i], Out);
Chris Lattner3a36d2c2008-08-19 05:06:27 +00002427 Out << '\n';
Reid Spencere7e96712004-05-25 08:53:40 +00002428 }
Reid Spencer32af9e82007-01-06 07:24:44 +00002429}
2430
Misha Brukmanc566ca362004-03-02 00:22:19 +00002431/// printFunction - Print all aspects of a function.
2432///
Chris Lattner113f4f42002-06-25 16:13:24 +00002433void AssemblyWriter::printFunction(const Function *F) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00002434 // Print out the return type and name.
2435 Out << '\n';
Chris Lattner379a8d22003-04-16 20:28:45 +00002436
Misha Brukmana6619a92004-06-21 21:53:56 +00002437 if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00002438
Dan Gohman5ded1422010-01-29 23:12:36 +00002439 if (F->isMaterializable())
2440 Out << "; Materializable\n";
2441
Bill Wendling847a5c32013-04-16 20:55:47 +00002442 const AttributeSet &Attrs = F->getAttributes();
Bill Wendling04945972013-04-29 23:48:06 +00002443 if (Attrs.hasAttributes(AttributeSet::FunctionIndex)) {
Bill Wendling847a5c32013-04-16 20:55:47 +00002444 AttributeSet AS = Attrs.getFnAttributes();
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +00002445 std::string AttrStr;
2446
2447 unsigned Idx = 0;
2448 for (unsigned E = AS.getNumSlots(); Idx != E; ++Idx)
2449 if (AS.getSlotIndex(Idx) == AttributeSet::FunctionIndex)
2450 break;
2451
2452 for (AttributeSet::iterator I = AS.begin(Idx), E = AS.end(Idx);
2453 I != E; ++I) {
2454 Attribute Attr = *I;
2455 if (!Attr.isStringAttribute()) {
2456 if (!AttrStr.empty()) AttrStr += ' ';
2457 AttrStr += Attr.getAsString();
2458 }
2459 }
2460
Bill Wendling847a5c32013-04-16 20:55:47 +00002461 if (!AttrStr.empty())
2462 Out << "; Function Attrs: " << AttrStr << '\n';
2463 }
2464
Reid Spencer5301e7c2007-01-30 20:08:39 +00002465 if (F->isDeclaration())
Chris Lattner10f03a62007-08-19 22:15:26 +00002466 Out << "declare ";
2467 else
Reid Spencer7ce2d2a2006-12-29 20:29:48 +00002468 Out << "define ";
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002469
Chris Lattner3a36d2c2008-08-19 05:06:27 +00002470 PrintLinkage(F->getLinkage(), Out);
2471 PrintVisibility(F->getVisibility(), Out);
Nico Rieck7157bb72014-01-14 15:22:47 +00002472 PrintDLLStorageClass(F->getDLLStorageClass(), Out);
Chris Lattner379a8d22003-04-16 20:28:45 +00002473
Chris Lattnerf7b6d312005-05-06 20:26:43 +00002474 // Print the calling convention.
Micah Villmow857186d2012-09-13 15:11:12 +00002475 if (F->getCallingConv() != CallingConv::C) {
2476 PrintCallingConv(F->getCallingConv(), Out);
2477 Out << " ";
Chris Lattnerf7b6d312005-05-06 20:26:43 +00002478 }
2479
Chris Lattner229907c2011-07-18 04:54:35 +00002480 FunctionType *FT = F->getFunctionType();
Bill Wendling658d24d2013-01-18 21:53:16 +00002481 if (Attrs.hasAttributes(AttributeSet::ReturnIndex))
2482 Out << Attrs.getAsString(AttributeSet::ReturnIndex) << ' ';
Chris Lattnere101c442009-02-28 21:26:53 +00002483 TypePrinter.print(F->getReturnType(), Out);
Chris Lattner585297e82008-08-19 05:26:17 +00002484 Out << ' ';
Dan Gohmana2489d12010-07-20 23:55:01 +00002485 WriteAsOperandInternal(Out, F, &TypePrinter, &Machine, F->getParent());
Misha Brukmana6619a92004-06-21 21:53:56 +00002486 Out << '(';
Reid Spencer16f2f7f2004-05-26 07:18:52 +00002487 Machine.incorporateFunction(F);
Chris Lattnerfee714f2001-09-07 16:36:04 +00002488
Chris Lattner7bfee412001-10-29 16:05:51 +00002489 // Loop over the arguments, printing them...
Chris Lattnerfee714f2001-09-07 16:36:04 +00002490
Reid Spencer8c4914c2006-12-31 05:24:50 +00002491 unsigned Idx = 1;
Chris Lattner82738fe2007-04-18 00:57:22 +00002492 if (!F->isDeclaration()) {
2493 // If this isn't a declaration, print the argument names as well.
2494 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
2495 I != E; ++I) {
2496 // Insert commas as we go... the first arg doesn't get a comma
2497 if (I != F->arg_begin()) Out << ", ";
Bill Wendling749a43d2012-12-30 13:50:49 +00002498 printArgument(I, Attrs, Idx);
Chris Lattner82738fe2007-04-18 00:57:22 +00002499 Idx++;
2500 }
2501 } else {
2502 // Otherwise, print the types from the function type.
2503 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
2504 // Insert commas as we go... the first arg doesn't get a comma
2505 if (i) Out << ", ";
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002506
Chris Lattner82738fe2007-04-18 00:57:22 +00002507 // Output type...
Chris Lattnere101c442009-02-28 21:26:53 +00002508 TypePrinter.print(FT->getParamType(i), Out);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002509
Bill Wendling749a43d2012-12-30 13:50:49 +00002510 if (Attrs.hasAttributes(i+1))
2511 Out << ' ' << Attrs.getAsString(i+1);
Chris Lattner82738fe2007-04-18 00:57:22 +00002512 }
Reid Spencer8c4914c2006-12-31 05:24:50 +00002513 }
Chris Lattnerfee714f2001-09-07 16:36:04 +00002514
2515 // Finish printing arguments...
Chris Lattner113f4f42002-06-25 16:13:24 +00002516 if (FT->isVarArg()) {
Misha Brukmana6619a92004-06-21 21:53:56 +00002517 if (FT->getNumParams()) Out << ", ";
2518 Out << "..."; // Output varargs portion of signature!
Chris Lattnerfee714f2001-09-07 16:36:04 +00002519 }
Misha Brukmana6619a92004-06-21 21:53:56 +00002520 Out << ')';
Rafael Espindola563eb4b2011-01-25 19:09:56 +00002521 if (F->hasUnnamedAddr())
2522 Out << " unnamed_addr";
Bill Wendling04945972013-04-29 23:48:06 +00002523 if (Attrs.hasAttributes(AttributeSet::FunctionIndex))
2524 Out << " #" << Machine.getAttributeGroupSlot(Attrs.getFnAttributes());
Chris Lattner9380b812010-07-07 23:16:37 +00002525 if (F->hasSection()) {
2526 Out << " section \"";
2527 PrintEscapedString(F->getSection(), Out);
2528 Out << '"';
2529 }
Rafael Espindola83a362c2015-01-06 22:55:16 +00002530 maybePrintComdat(Out, *F);
Chris Lattnerf8a974d2005-11-06 06:48:53 +00002531 if (F->getAlignment())
2532 Out << " align " << F->getAlignment();
Gordon Henriksend930f912008-08-17 18:44:35 +00002533 if (F->hasGC())
2534 Out << " gc \"" << F->getGC() << '"';
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00002535 if (F->hasPrefixData()) {
2536 Out << " prefix ";
2537 writeOperand(F->getPrefixData(), true);
2538 }
Peter Collingbourne51d2de72014-12-03 02:08:38 +00002539 if (F->hasPrologueData()) {
2540 Out << " prologue ";
2541 writeOperand(F->getPrologueData(), true);
2542 }
2543
Duncan P. N. Exon Smith3d4cd752015-04-24 22:04:41 +00002544 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
2545 F->getAllMetadata(MDs);
2546 printMetadataAttachments(MDs, " ");
2547
Reid Spencer5301e7c2007-01-30 20:08:39 +00002548 if (F->isDeclaration()) {
Chris Lattnerfb483622010-09-02 22:52:10 +00002549 Out << '\n';
Chris Lattnerb2f02e52002-05-06 03:00:40 +00002550 } else {
Chris Lattnerfb483622010-09-02 22:52:10 +00002551 Out << " {";
2552 // Output all of the function's basic blocks.
Chris Lattner113f4f42002-06-25 16:13:24 +00002553 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
2554 printBasicBlock(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +00002555
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +00002556 // Output the function's use-lists.
2557 printUseLists(F);
2558
Misha Brukmana6619a92004-06-21 21:53:56 +00002559 Out << "}\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +00002560 }
2561
Reid Spencer16f2f7f2004-05-26 07:18:52 +00002562 Machine.purgeFunction();
Chris Lattner2f7c9632001-06-06 20:29:01 +00002563}
2564
Misha Brukmanc566ca362004-03-02 00:22:19 +00002565/// printArgument - This member is called for every argument that is passed into
2566/// the function. Simply print it out
2567///
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002568void AssemblyWriter::printArgument(const Argument *Arg,
Bill Wendling749a43d2012-12-30 13:50:49 +00002569 AttributeSet Attrs, unsigned Idx) {
Chris Lattner2f7c9632001-06-06 20:29:01 +00002570 // Output type...
Chris Lattnere101c442009-02-28 21:26:53 +00002571 TypePrinter.print(Arg->getType(), Out);
Misha Brukmanb1c93172005-04-21 23:48:37 +00002572
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00002573 // Output parameter attributes list
Bill Wendling749a43d2012-12-30 13:50:49 +00002574 if (Attrs.hasAttributes(Idx))
2575 Out << ' ' << Attrs.getAsString(Idx);
Reid Spencer8c4914c2006-12-31 05:24:50 +00002576
Chris Lattner2f7c9632001-06-06 20:29:01 +00002577 // Output name, if available...
Chris Lattner033935d2008-08-17 04:40:13 +00002578 if (Arg->hasName()) {
2579 Out << ' ';
2580 PrintLLVMName(Out, Arg);
2581 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00002582}
2583
Misha Brukmanc566ca362004-03-02 00:22:19 +00002584/// printBasicBlock - This member is called for each basic block in a method.
2585///
Chris Lattner7bfee412001-10-29 16:05:51 +00002586void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Nick Lewycky4d43d3c2008-04-25 16:53:59 +00002587 if (BB->hasName()) { // Print out the label if it exists...
Chris Lattner033935d2008-08-17 04:40:13 +00002588 Out << "\n";
Daniel Dunbare03eecb2009-07-25 23:55:21 +00002589 PrintLLVMName(Out, BB->getName(), LabelPrefix);
Chris Lattner033935d2008-08-17 04:40:13 +00002590 Out << ':';
Nick Lewycky4d43d3c2008-04-25 16:53:59 +00002591 } else if (!BB->use_empty()) { // Don't print block # of no uses...
Bill Wendling35a9c3c2011-04-10 23:18:04 +00002592 Out << "\n; <label>:";
Chris Lattner5e043322007-01-11 03:54:27 +00002593 int Slot = Machine.getLocalSlot(BB);
Chris Lattner757ee0b2004-06-09 19:41:19 +00002594 if (Slot != -1)
Misha Brukmana6619a92004-06-21 21:53:56 +00002595 Out << Slot;
Chris Lattner757ee0b2004-06-09 19:41:19 +00002596 else
Misha Brukmana6619a92004-06-21 21:53:56 +00002597 Out << "<badref>";
Chris Lattner58185f22002-10-02 19:38:55 +00002598 }
Chris Lattner2447ef52003-11-20 00:09:43 +00002599
Craig Topperc6207612014-04-09 06:08:46 +00002600 if (!BB->getParent()) {
Chris Lattneree97b8b2009-08-17 15:48:08 +00002601 Out.PadToColumn(50);
Dan Gohmane2745262009-08-12 17:23:50 +00002602 Out << "; Error: Block without parent!";
2603 } else if (BB != &BB->getParent()->getEntryBlock()) { // Not the entry block?
Chris Lattnerfb483622010-09-02 22:52:10 +00002604 // Output predecessors for the block.
Chris Lattneree97b8b2009-08-17 15:48:08 +00002605 Out.PadToColumn(50);
Dan Gohmane2745262009-08-12 17:23:50 +00002606 Out << ";";
Gabor Greif6c6b2fd2010-03-25 23:25:28 +00002607 const_pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002608
Chris Lattnerff834c02008-04-22 02:45:44 +00002609 if (PI == PE) {
2610 Out << " No predecessors!";
2611 } else {
Dan Gohman81313fd2008-09-14 17:21:12 +00002612 Out << " preds = ";
Chris Lattnerff834c02008-04-22 02:45:44 +00002613 writeOperand(*PI, false);
2614 for (++PI; PI != PE; ++PI) {
Dan Gohman81313fd2008-09-14 17:21:12 +00002615 Out << ", ";
Chris Lattner78e2e8b2006-12-06 06:24:27 +00002616 writeOperand(*PI, false);
Chris Lattner00211f12003-11-16 22:59:57 +00002617 }
Chris Lattner58185f22002-10-02 19:38:55 +00002618 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00002619 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00002620
Chris Lattnerff834c02008-04-22 02:45:44 +00002621 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +00002622
Misha Brukmana6619a92004-06-21 21:53:56 +00002623 if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00002624
Chris Lattnerfee714f2001-09-07 16:36:04 +00002625 // Output all of the instructions in the basic block...
Dan Gohmana4f709e2009-07-13 18:27:59 +00002626 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
Daniel Maleaded9f932013-05-08 20:38:31 +00002627 printInstructionLine(*I);
Dan Gohmana4f709e2009-07-13 18:27:59 +00002628 }
Chris Lattner96cdd272004-03-08 18:51:45 +00002629
Misha Brukmana6619a92004-06-21 21:53:56 +00002630 if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, Out);
Chris Lattner2f7c9632001-06-06 20:29:01 +00002631}
2632
Daniel Maleaded9f932013-05-08 20:38:31 +00002633/// printInstructionLine - Print an instruction and a newline character.
2634void AssemblyWriter::printInstructionLine(const Instruction &I) {
2635 printInstruction(I);
2636 Out << '\n';
2637}
2638
Igor Laevsky2aa8caf2015-05-05 13:20:42 +00002639/// printGCRelocateComment - print comment after call to the gc.relocate
2640/// intrinsic indicating base and derived pointer names.
2641void AssemblyWriter::printGCRelocateComment(const Value &V) {
2642 assert(isGCRelocate(&V));
2643 GCRelocateOperands GCOps(cast<Instruction>(&V));
2644
2645 Out << " ; (";
Sanjoy Das499d7032015-05-06 02:36:26 +00002646 writeOperand(GCOps.getBasePtr(), false);
Igor Laevsky2aa8caf2015-05-05 13:20:42 +00002647 Out << ", ";
Sanjoy Das499d7032015-05-06 02:36:26 +00002648 writeOperand(GCOps.getDerivedPtr(), false);
Igor Laevsky2aa8caf2015-05-05 13:20:42 +00002649 Out << ")";
2650}
2651
Misha Brukmanc566ca362004-03-02 00:22:19 +00002652/// printInfoComment - Print a little comment after the instruction indicating
2653/// which slot it occupies.
2654///
Chris Lattner113f4f42002-06-25 16:13:24 +00002655void AssemblyWriter::printInfoComment(const Value &V) {
Igor Laevsky2aa8caf2015-05-05 13:20:42 +00002656 if (isGCRelocate(&V))
2657 printGCRelocateComment(V);
2658
Bill Wendling847a5c32013-04-16 20:55:47 +00002659 if (AnnotationWriter)
Dan Gohmane34890f2010-02-10 20:41:46 +00002660 AnnotationWriter->printInfoComment(V, Out);
Chris Lattner862e3382001-10-13 06:42:36 +00002661}
2662
Reid Spencere7141c82006-08-28 01:02:49 +00002663// This member is called for each Instruction in a function..
Chris Lattner113f4f42002-06-25 16:13:24 +00002664void AssemblyWriter::printInstruction(const Instruction &I) {
Misha Brukmana6619a92004-06-21 21:53:56 +00002665 if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00002666
Dan Gohman466876b2009-08-12 23:32:33 +00002667 // Print out indentation for an instruction.
Dan Gohmanb4583b12009-08-13 01:41:52 +00002668 Out << " ";
Chris Lattner2f7c9632001-06-06 20:29:01 +00002669
2670 // Print out name if it exists...
Chris Lattner033935d2008-08-17 04:40:13 +00002671 if (I.hasName()) {
2672 PrintLLVMName(Out, &I);
2673 Out << " = ";
Chris Lattner031560c2009-12-29 07:25:48 +00002674 } else if (!I.getType()->isVoidTy()) {
Chris Lattnerb25e5ea2008-08-29 17:19:30 +00002675 // Print out the def slot taken.
2676 int SlotNum = Machine.getLocalSlot(&I);
2677 if (SlotNum == -1)
2678 Out << "<badref> = ";
2679 else
2680 Out << '%' << SlotNum << " = ";
Chris Lattner033935d2008-08-17 04:40:13 +00002681 }
Andrew Trickec4b6e72011-09-30 19:48:58 +00002682
Reid Kleckner5772b772014-04-24 20:14:34 +00002683 if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
2684 if (CI->isMustTailCall())
2685 Out << "musttail ";
2686 else if (CI->isTailCall())
2687 Out << "tail ";
2688 }
Chris Lattner504f9242003-09-08 17:45:59 +00002689
Chris Lattner2f7c9632001-06-06 20:29:01 +00002690 // Print out the opcode...
Misha Brukmana6619a92004-06-21 21:53:56 +00002691 Out << I.getOpcodeName();
Chris Lattner2f7c9632001-06-06 20:29:01 +00002692
Eli Friedman02e737b2011-08-12 22:50:01 +00002693 // If this is an atomic load or store, print out the atomic marker.
2694 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isAtomic()) ||
2695 (isa<StoreInst>(I) && cast<StoreInst>(I).isAtomic()))
2696 Out << " atomic";
2697
Tim Northover420a2162014-06-13 14:24:07 +00002698 if (isa<AtomicCmpXchgInst>(I) && cast<AtomicCmpXchgInst>(I).isWeak())
2699 Out << " weak";
2700
Eli Friedman02e737b2011-08-12 22:50:01 +00002701 // If this is a volatile operation, print out the volatile marker.
2702 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) ||
2703 (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile()) ||
2704 (isa<AtomicCmpXchgInst>(I) && cast<AtomicCmpXchgInst>(I).isVolatile()) ||
2705 (isa<AtomicRMWInst>(I) && cast<AtomicRMWInst>(I).isVolatile()))
2706 Out << " volatile";
2707
Dan Gohman9c7f8082009-07-27 16:11:46 +00002708 // Print out optimization information.
2709 WriteOptimizationInfo(Out, &I);
2710
Reid Spencer45e52392006-12-03 06:27:29 +00002711 // Print out the compare instruction predicates
Nate Begemand2195702008-05-12 19:01:56 +00002712 if (const CmpInst *CI = dyn_cast<CmpInst>(&I))
Chris Lattner82ff9232008-08-23 22:52:27 +00002713 Out << ' ' << getPredicateText(CI->getPredicate());
Reid Spencer45e52392006-12-03 06:27:29 +00002714
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002715 // Print out the atomicrmw operation
2716 if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(&I))
2717 writeAtomicRMWOperation(Out, RMWI->getOperation());
2718
Chris Lattner2f7c9632001-06-06 20:29:01 +00002719 // Print out the type of the operands...
Craig Topperc6207612014-04-09 06:08:46 +00002720 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : nullptr;
Chris Lattner2f7c9632001-06-06 20:29:01 +00002721
2722 // Special case conditional branches to swizzle the condition out to the front
Gabor Greifcab008f2009-02-09 15:45:06 +00002723 if (isa<BranchInst>(I) && cast<BranchInst>(I).isConditional()) {
David Blaikieb78e9e52013-02-11 01:16:51 +00002724 const BranchInst &BI(cast<BranchInst>(I));
Dan Gohman81313fd2008-09-14 17:21:12 +00002725 Out << ' ';
Gabor Greifcab008f2009-02-09 15:45:06 +00002726 writeOperand(BI.getCondition(), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00002727 Out << ", ";
Gabor Greifcab008f2009-02-09 15:45:06 +00002728 writeOperand(BI.getSuccessor(0), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00002729 Out << ", ";
Gabor Greifcab008f2009-02-09 15:45:06 +00002730 writeOperand(BI.getSuccessor(1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00002731
Chris Lattner8d48df22002-04-13 18:34:38 +00002732 } else if (isa<SwitchInst>(I)) {
David Blaikieb78e9e52013-02-11 01:16:51 +00002733 const SwitchInst& SI(cast<SwitchInst>(I));
Chris Lattner3ed871f2009-10-27 19:13:16 +00002734 // Special case switch instruction to get formatting nice and correct.
Dan Gohman81313fd2008-09-14 17:21:12 +00002735 Out << ' ';
Eli Friedman95031ed2011-09-29 20:21:17 +00002736 writeOperand(SI.getCondition(), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00002737 Out << ", ";
Eli Friedman95031ed2011-09-29 20:21:17 +00002738 writeOperand(SI.getDefaultDest(), true);
Chris Lattner82ff9232008-08-23 22:52:27 +00002739 Out << " [";
David Blaikieb78e9e52013-02-11 01:16:51 +00002740 for (SwitchInst::ConstCaseIt i = SI.case_begin(), e = SI.case_end();
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00002741 i != e; ++i) {
Dan Gohmanb4583b12009-08-13 01:41:52 +00002742 Out << "\n ";
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00002743 writeOperand(i.getCaseValue(), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00002744 Out << ", ";
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00002745 writeOperand(i.getCaseSuccessor(), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00002746 }
Dan Gohmanb4583b12009-08-13 01:41:52 +00002747 Out << "\n ]";
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00002748 } else if (isa<IndirectBrInst>(I)) {
2749 // Special case indirectbr instruction to get formatting nice and correct.
Chris Lattner3ed871f2009-10-27 19:13:16 +00002750 Out << ' ';
2751 writeOperand(Operand, true);
Dan Gohman43c57402009-10-30 02:01:10 +00002752 Out << ", [";
Andrew Trickec4b6e72011-09-30 19:48:58 +00002753
Chris Lattner3ed871f2009-10-27 19:13:16 +00002754 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) {
2755 if (i != 1)
2756 Out << ", ";
2757 writeOperand(I.getOperand(i), true);
2758 }
2759 Out << ']';
Jay Foad372ad642011-06-20 14:18:48 +00002760 } else if (const PHINode *PN = dyn_cast<PHINode>(&I)) {
Misha Brukmana6619a92004-06-21 21:53:56 +00002761 Out << ' ';
Chris Lattnere101c442009-02-28 21:26:53 +00002762 TypePrinter.print(I.getType(), Out);
Misha Brukmana6619a92004-06-21 21:53:56 +00002763 Out << ' ';
Chris Lattner2f7c9632001-06-06 20:29:01 +00002764
Jay Foad372ad642011-06-20 14:18:48 +00002765 for (unsigned op = 0, Eop = PN->getNumIncomingValues(); op < Eop; ++op) {
Misha Brukmana6619a92004-06-21 21:53:56 +00002766 if (op) Out << ", ";
Dan Gohman81313fd2008-09-14 17:21:12 +00002767 Out << "[ ";
Jay Foad372ad642011-06-20 14:18:48 +00002768 writeOperand(PN->getIncomingValue(op), false); Out << ", ";
2769 writeOperand(PN->getIncomingBlock(op), false); Out << " ]";
Chris Lattner931ef3b2001-06-11 15:04:20 +00002770 }
Dan Gohmana76f0f72008-05-31 19:12:39 +00002771 } else if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(&I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00002772 Out << ' ';
Dan Gohmana76f0f72008-05-31 19:12:39 +00002773 writeOperand(I.getOperand(0), true);
2774 for (const unsigned *i = EVI->idx_begin(), *e = EVI->idx_end(); i != e; ++i)
2775 Out << ", " << *i;
2776 } else if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(&I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00002777 Out << ' ';
2778 writeOperand(I.getOperand(0), true); Out << ", ";
Dan Gohmana76f0f72008-05-31 19:12:39 +00002779 writeOperand(I.getOperand(1), true);
2780 for (const unsigned *i = IVI->idx_begin(), *e = IVI->idx_end(); i != e; ++i)
2781 Out << ", " << *i;
Bill Wendlingfae14752011-08-12 20:24:12 +00002782 } else if (const LandingPadInst *LPI = dyn_cast<LandingPadInst>(&I)) {
2783 Out << ' ';
2784 TypePrinter.print(I.getType(), Out);
2785 Out << " personality ";
2786 writeOperand(I.getOperand(0), true); Out << '\n';
2787
2788 if (LPI->isCleanup())
2789 Out << " cleanup";
2790
2791 for (unsigned i = 0, e = LPI->getNumClauses(); i != e; ++i) {
2792 if (i != 0 || LPI->isCleanup()) Out << "\n";
2793 if (LPI->isCatch(i))
2794 Out << " catch ";
2795 else
2796 Out << " filter ";
2797
2798 writeOperand(LPI->getClause(i), true);
2799 }
Devang Patel59643e52008-02-23 00:35:18 +00002800 } else if (isa<ReturnInst>(I) && !Operand) {
2801 Out << " void";
Chris Lattnerf7b6d312005-05-06 20:26:43 +00002802 } else if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
2803 // Print the calling convention being used.
Micah Villmow857186d2012-09-13 15:11:12 +00002804 if (CI->getCallingConv() != CallingConv::C) {
2805 Out << " ";
2806 PrintCallingConv(CI->getCallingConv(), Out);
Chris Lattnerf7b6d312005-05-06 20:26:43 +00002807 }
2808
Gabor Greifc9a92512010-06-23 13:09:06 +00002809 Operand = CI->getCalledValue();
David Blaikie23af6482015-04-16 23:24:18 +00002810 FunctionType *FTy = cast<FunctionType>(CI->getFunctionType());
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002811 Type *RetTy = FTy->getReturnType();
Bill Wendlinge94d8432012-12-07 23:16:57 +00002812 const AttributeSet &PAL = CI->getAttributes();
Chris Lattner2f2d9472001-11-06 21:28:12 +00002813
Bill Wendling658d24d2013-01-18 21:53:16 +00002814 if (PAL.hasAttributes(AttributeSet::ReturnIndex))
2815 Out << ' ' << PAL.getAsString(AttributeSet::ReturnIndex);
Devang Patel221fe422008-09-29 20:49:50 +00002816
Chris Lattner463d6a52003-08-05 15:34:45 +00002817 // If possible, print out the short form of the call instruction. We can
Chris Lattner6915f8f2002-04-07 22:49:37 +00002818 // only do this if the first argument is a pointer to a nonvararg function,
Chris Lattner463d6a52003-08-05 15:34:45 +00002819 // and if the return type is not a pointer to a function.
Chris Lattner2f2d9472001-11-06 21:28:12 +00002820 //
Dan Gohman81313fd2008-09-14 17:21:12 +00002821 Out << ' ';
David Blaikie23af6482015-04-16 23:24:18 +00002822 TypePrinter.print(FTy->isVarArg() ? FTy : RetTy, Out);
2823 Out << ' ';
2824 writeOperand(Operand, false);
Misha Brukmana6619a92004-06-21 21:53:56 +00002825 Out << '(';
Gabor Greifc9a92512010-06-23 13:09:06 +00002826 for (unsigned op = 0, Eop = CI->getNumArgOperands(); op < Eop; ++op) {
2827 if (op > 0)
Dan Gohman81313fd2008-09-14 17:21:12 +00002828 Out << ", ";
Bill Wendling749a43d2012-12-30 13:50:49 +00002829 writeParamOperand(CI->getArgOperand(op), PAL, op + 1);
Chris Lattner2f7c9632001-06-06 20:29:01 +00002830 }
Reid Kleckner83498642014-08-26 00:33:28 +00002831
2832 // Emit an ellipsis if this is a musttail call in a vararg function. This
2833 // is only to aid readability, musttail calls forward varargs by default.
2834 if (CI->isMustTailCall() && CI->getParent() &&
2835 CI->getParent()->getParent() &&
2836 CI->getParent()->getParent()->isVarArg())
2837 Out << ", ...";
2838
Dan Gohman81313fd2008-09-14 17:21:12 +00002839 Out << ')';
Bill Wendling698e84f2012-12-30 10:32:01 +00002840 if (PAL.hasAttributes(AttributeSet::FunctionIndex))
Bill Wendlinga0323742013-02-22 09:09:42 +00002841 Out << " #" << Machine.getAttributeGroupSlot(PAL.getFnAttributes());
Chris Lattner113f4f42002-06-25 16:13:24 +00002842 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
Gabor Greifa2fbc0a2010-03-24 13:21:49 +00002843 Operand = II->getCalledValue();
David Blaikie445e3fb2015-04-24 19:32:54 +00002844 FunctionType *FTy = cast<FunctionType>(II->getFunctionType());
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002845 Type *RetTy = FTy->getReturnType();
Bill Wendlinge94d8432012-12-07 23:16:57 +00002846 const AttributeSet &PAL = II->getAttributes();
Chris Lattner463d6a52003-08-05 15:34:45 +00002847
Chris Lattnerf7b6d312005-05-06 20:26:43 +00002848 // Print the calling convention being used.
Micah Villmow857186d2012-09-13 15:11:12 +00002849 if (II->getCallingConv() != CallingConv::C) {
2850 Out << " ";
2851 PrintCallingConv(II->getCallingConv(), Out);
Chris Lattnerf7b6d312005-05-06 20:26:43 +00002852 }
2853
Bill Wendling658d24d2013-01-18 21:53:16 +00002854 if (PAL.hasAttributes(AttributeSet::ReturnIndex))
2855 Out << ' ' << PAL.getAsString(AttributeSet::ReturnIndex);
Devang Patel221fe422008-09-29 20:49:50 +00002856
Chris Lattner463d6a52003-08-05 15:34:45 +00002857 // If possible, print out the short form of the invoke instruction. We can
2858 // only do this if the first argument is a pointer to a nonvararg function,
2859 // and if the return type is not a pointer to a function.
2860 //
Dan Gohmanc7e00ba2008-10-15 18:02:08 +00002861 Out << ' ';
David Blaikie445e3fb2015-04-24 19:32:54 +00002862 TypePrinter.print(FTy->isVarArg() ? FTy : RetTy, Out);
2863 Out << ' ';
2864 writeOperand(Operand, false);
Misha Brukmana6619a92004-06-21 21:53:56 +00002865 Out << '(';
Gabor Greifc9a92512010-06-23 13:09:06 +00002866 for (unsigned op = 0, Eop = II->getNumArgOperands(); op < Eop; ++op) {
Gabor Greifa2fbc0a2010-03-24 13:21:49 +00002867 if (op)
Dan Gohman81313fd2008-09-14 17:21:12 +00002868 Out << ", ";
Bill Wendling749a43d2012-12-30 13:50:49 +00002869 writeParamOperand(II->getArgOperand(op), PAL, op + 1);
Chris Lattner862e3382001-10-13 06:42:36 +00002870 }
2871
Dan Gohman81313fd2008-09-14 17:21:12 +00002872 Out << ')';
Bill Wendling698e84f2012-12-30 10:32:01 +00002873 if (PAL.hasAttributes(AttributeSet::FunctionIndex))
Bill Wendlinga0323742013-02-22 09:09:42 +00002874 Out << " #" << Machine.getAttributeGroupSlot(PAL.getFnAttributes());
Devang Patela05633e2008-09-26 22:53:05 +00002875
Dan Gohmanb4583b12009-08-13 01:41:52 +00002876 Out << "\n to ";
Chris Lattner862e3382001-10-13 06:42:36 +00002877 writeOperand(II->getNormalDest(), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00002878 Out << " unwind ";
Chris Lattnerfae8ab32004-02-08 21:44:31 +00002879 writeOperand(II->getUnwindDest(), true);
Chris Lattner862e3382001-10-13 06:42:36 +00002880
Victor Hernandez8acf2952009-10-23 21:09:37 +00002881 } else if (const AllocaInst *AI = dyn_cast<AllocaInst>(&I)) {
Misha Brukmana6619a92004-06-21 21:53:56 +00002882 Out << ' ';
Reid Kleckner20844032014-01-25 01:24:06 +00002883 if (AI->isUsedWithInAlloca())
David Majnemerc4ab61c2014-03-09 06:41:58 +00002884 Out << "inalloca ";
2885 TypePrinter.print(AI->getAllocatedType(), Out);
Duncan P. N. Exon Smith720762e2015-03-13 19:30:44 +00002886
2887 // Explicitly write the array size if the code is broken, if it's an array
2888 // allocation, or if the type is not canonical for scalar allocations. The
2889 // latter case prevents the type from mutating when round-tripping through
2890 // assembly.
2891 if (!AI->getArraySize() || AI->isArrayAllocation() ||
2892 !AI->getArraySize()->getType()->isIntegerTy(32)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00002893 Out << ", ";
Chris Lattner8d48df22002-04-13 18:34:38 +00002894 writeOperand(AI->getArraySize(), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00002895 }
Nate Begeman848622f2005-11-05 09:21:28 +00002896 if (AI->getAlignment()) {
Chris Lattner7aeee3a2005-11-05 21:20:34 +00002897 Out << ", align " << AI->getAlignment();
Nate Begeman848622f2005-11-05 09:21:28 +00002898 }
Chris Lattner862e3382001-10-13 06:42:36 +00002899 } else if (isa<CastInst>(I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00002900 if (Operand) {
2901 Out << ' ';
2902 writeOperand(Operand, true); // Work with broken code
2903 }
Misha Brukmana6619a92004-06-21 21:53:56 +00002904 Out << " to ";
Chris Lattnere101c442009-02-28 21:26:53 +00002905 TypePrinter.print(I.getType(), Out);
Chris Lattner5b337482003-10-18 05:57:43 +00002906 } else if (isa<VAArgInst>(I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00002907 if (Operand) {
2908 Out << ' ';
2909 writeOperand(Operand, true); // Work with broken code
2910 }
Misha Brukmana6619a92004-06-21 21:53:56 +00002911 Out << ", ";
Chris Lattnere101c442009-02-28 21:26:53 +00002912 TypePrinter.print(I.getType(), Out);
2913 } else if (Operand) { // Print the normal way.
David Blaikiea79ac142015-02-27 21:17:42 +00002914 if (const auto *GEP = dyn_cast<GetElementPtrInst>(&I)) {
David Blaikie79e6c742015-02-27 19:29:02 +00002915 Out << ' ';
2916 TypePrinter.print(GEP->getSourceElementType(), Out);
2917 Out << ',';
David Blaikiea79ac142015-02-27 21:17:42 +00002918 } else if (const auto *LI = dyn_cast<LoadInst>(&I)) {
2919 Out << ' ';
2920 TypePrinter.print(LI->getType(), Out);
Benjamin Kramer257ed692015-03-02 15:24:41 +00002921 Out << ',';
David Blaikie79e6c742015-02-27 19:29:02 +00002922 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00002923
Misha Brukmanb1c93172005-04-21 23:48:37 +00002924 // PrintAllTypes - Instructions who have operands of all the same type
Chris Lattner2f7c9632001-06-06 20:29:01 +00002925 // omit the type from all but the first operand. If the instruction has
2926 // different type operands (for example br), then they are all printed.
2927 bool PrintAllTypes = false;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002928 Type *TheType = Operand->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +00002929
Reid Spencer0cdd04f2007-02-02 13:54:55 +00002930 // Select, Store and ShuffleVector always print all types.
Devang Patelce556d92008-03-04 22:05:14 +00002931 if (isa<SelectInst>(I) || isa<StoreInst>(I) || isa<ShuffleVectorInst>(I)
2932 || isa<ReturnInst>(I)) {
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00002933 PrintAllTypes = true;
2934 } else {
2935 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
2936 Operand = I.getOperand(i);
Nuno Lopes04fe2f02009-01-15 18:40:57 +00002937 // note that Operand shouldn't be null, but the test helps make dump()
2938 // more tolerant of malformed IR
Nuno Lopes0971e772009-01-14 17:51:41 +00002939 if (Operand && Operand->getType() != TheType) {
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00002940 PrintAllTypes = true; // We have differing types! Print them all!
2941 break;
2942 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00002943 }
2944 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00002945
Chris Lattner7bfee412001-10-29 16:05:51 +00002946 if (!PrintAllTypes) {
Misha Brukmana6619a92004-06-21 21:53:56 +00002947 Out << ' ';
Chris Lattnere101c442009-02-28 21:26:53 +00002948 TypePrinter.print(TheType, Out);
Chris Lattner7bfee412001-10-29 16:05:51 +00002949 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00002950
Dan Gohman81313fd2008-09-14 17:21:12 +00002951 Out << ' ';
Chris Lattner113f4f42002-06-25 16:13:24 +00002952 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
Dan Gohman81313fd2008-09-14 17:21:12 +00002953 if (i) Out << ", ";
Chris Lattner113f4f42002-06-25 16:13:24 +00002954 writeOperand(I.getOperand(i), PrintAllTypes);
Chris Lattner2f7c9632001-06-06 20:29:01 +00002955 }
2956 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002957
Eli Friedman59b66882011-08-09 23:02:53 +00002958 // Print atomic ordering/alignment for memory operations
2959 if (const LoadInst *LI = dyn_cast<LoadInst>(&I)) {
2960 if (LI->isAtomic())
2961 writeAtomic(LI->getOrdering(), LI->getSynchScope());
2962 if (LI->getAlignment())
2963 Out << ", align " << LI->getAlignment();
2964 } else if (const StoreInst *SI = dyn_cast<StoreInst>(&I)) {
2965 if (SI->isAtomic())
2966 writeAtomic(SI->getOrdering(), SI->getSynchScope());
2967 if (SI->getAlignment())
2968 Out << ", align " << SI->getAlignment();
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002969 } else if (const AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(&I)) {
Tim Northovere94a5182014-03-11 10:48:52 +00002970 writeAtomicCmpXchg(CXI->getSuccessOrdering(), CXI->getFailureOrdering(),
2971 CXI->getSynchScope());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002972 } else if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(&I)) {
2973 writeAtomic(RMWI->getOrdering(), RMWI->getSynchScope());
Eli Friedmanfee02c62011-07-25 23:16:38 +00002974 } else if (const FenceInst *FI = dyn_cast<FenceInst>(&I)) {
2975 writeAtomic(FI->getOrdering(), FI->getSynchScope());
Christopher Lamb84485702007-04-22 19:24:39 +00002976 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00002977
Chris Lattnerc9558df2009-12-28 20:10:43 +00002978 // Print Metadata info.
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00002979 SmallVector<std::pair<unsigned, MDNode *>, 4> InstMD;
Nick Lewyckyba8ec9a2010-02-25 06:53:04 +00002980 I.getAllMetadata(InstMD);
Duncan P. N. Exon Smithd3e4c2a2015-04-24 21:06:21 +00002981 printMetadataAttachments(InstMD, ", ");
Duncan P. N. Exon Smith86979272015-04-24 20:59:52 +00002982
2983 // Print a nice comment.
Chris Lattner862e3382001-10-13 06:42:36 +00002984 printInfoComment(I);
Chris Lattner2f7c9632001-06-06 20:29:01 +00002985}
2986
Duncan P. N. Exon Smith86979272015-04-24 20:59:52 +00002987void AssemblyWriter::printMetadataAttachments(
Duncan P. N. Exon Smithd3e4c2a2015-04-24 21:06:21 +00002988 const SmallVectorImpl<std::pair<unsigned, MDNode *>> &MDs,
2989 StringRef Separator) {
Duncan P. N. Exon Smith86979272015-04-24 20:59:52 +00002990 if (MDs.empty())
2991 return;
2992
Duncan P. N. Exon Smithe30f10e2015-04-24 21:03:05 +00002993 if (MDNames.empty())
2994 TheModule->getMDKindNames(MDNames);
2995
Duncan P. N. Exon Smith86979272015-04-24 20:59:52 +00002996 for (const auto &I : MDs) {
2997 unsigned Kind = I.first;
Duncan P. N. Exon Smithd3e4c2a2015-04-24 21:06:21 +00002998 Out << Separator;
Filipe Cabecinhas62431b12015-06-02 21:25:08 +00002999 if (Kind < MDNames.size()) {
3000 Out << "!";
3001 printMetadataIdentifier(MDNames[Kind], Out);
3002 } else
Duncan P. N. Exon Smithd3e4c2a2015-04-24 21:06:21 +00003003 Out << "!<unknown kind #" << Kind << ">";
Duncan P. N. Exon Smith86979272015-04-24 20:59:52 +00003004 Out << ' ';
3005 WriteAsOperandInternal(Out, I.second, &TypePrinter, &Machine, TheModule);
3006 }
3007}
3008
Daniel Maleaded9f932013-05-08 20:38:31 +00003009void AssemblyWriter::writeMDNode(unsigned Slot, const MDNode *Node) {
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +00003010 Out << '!' << Slot << " = ";
Daniel Maleaded9f932013-05-08 20:38:31 +00003011 printMDNodeBody(Node);
Duncan P. N. Exon Smith738889f2015-02-25 22:46:38 +00003012 Out << "\n";
Daniel Maleaded9f932013-05-08 20:38:31 +00003013}
3014
Chris Lattnerbddea6a2009-12-31 02:13:35 +00003015void AssemblyWriter::writeAllMDNodes() {
3016 SmallVector<const MDNode *, 16> Nodes;
Chris Lattnercf4a76e2009-12-31 02:20:11 +00003017 Nodes.resize(Machine.mdn_size());
3018 for (SlotTracker::mdn_iterator I = Machine.mdn_begin(), E = Machine.mdn_end();
3019 I != E; ++I)
Chris Lattnerbddea6a2009-12-31 02:13:35 +00003020 Nodes[I->second] = cast<MDNode>(I->first);
Andrew Trickec4b6e72011-09-30 19:48:58 +00003021
Chris Lattnerbddea6a2009-12-31 02:13:35 +00003022 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Daniel Maleaded9f932013-05-08 20:38:31 +00003023 writeMDNode(i, Nodes[i]);
Chris Lattnerbddea6a2009-12-31 02:13:35 +00003024 }
3025}
3026
3027void AssemblyWriter::printMDNodeBody(const MDNode *Node) {
Dan Gohmana2489d12010-07-20 23:55:01 +00003028 WriteMDNodeBodyInternal(Out, Node, &TypePrinter, &Machine, TheModule);
Chris Lattnerbddea6a2009-12-31 02:13:35 +00003029}
Chris Lattner2f7c9632001-06-06 20:29:01 +00003030
Bill Wendling829b4782013-02-11 08:43:33 +00003031void AssemblyWriter::writeAllAttributeGroups() {
3032 std::vector<std::pair<AttributeSet, unsigned> > asVec;
3033 asVec.resize(Machine.as_size());
3034
3035 for (SlotTracker::as_iterator I = Machine.as_begin(), E = Machine.as_end();
3036 I != E; ++I)
3037 asVec[I->second] = *I;
3038
3039 for (std::vector<std::pair<AttributeSet, unsigned> >::iterator
3040 I = asVec.begin(), E = asVec.end(); I != E; ++I)
3041 Out << "attributes #" << I->second << " = { "
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +00003042 << I->first.getAsString(AttributeSet::FunctionIndex, true) << " }\n";
Bill Wendling829b4782013-02-11 08:43:33 +00003043}
3044
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +00003045void AssemblyWriter::printUseListOrder(const UseListOrder &Order) {
3046 bool IsInFunction = Machine.getFunction();
3047 if (IsInFunction)
3048 Out << " ";
3049
3050 Out << "uselistorder";
3051 if (const BasicBlock *BB =
3052 IsInFunction ? nullptr : dyn_cast<BasicBlock>(Order.V)) {
3053 Out << "_bb ";
3054 writeOperand(BB->getParent(), false);
3055 Out << ", ";
3056 writeOperand(BB, false);
3057 } else {
3058 Out << " ";
3059 writeOperand(Order.V, true);
3060 }
3061 Out << ", { ";
3062
3063 assert(Order.Shuffle.size() >= 2 && "Shuffle too small");
3064 Out << Order.Shuffle[0];
3065 for (unsigned I = 1, E = Order.Shuffle.size(); I != E; ++I)
3066 Out << ", " << Order.Shuffle[I];
3067 Out << " }\n";
3068}
3069
3070void AssemblyWriter::printUseLists(const Function *F) {
3071 auto hasMore =
3072 [&]() { return !UseListOrders.empty() && UseListOrders.back().F == F; };
3073 if (!hasMore())
3074 // Nothing to do.
3075 return;
3076
3077 Out << "\n; uselistorder directives\n";
3078 while (hasMore()) {
3079 printUseListOrder(UseListOrders.back());
3080 UseListOrders.pop_back();
3081 }
3082}
3083
Chris Lattner2f7c9632001-06-06 20:29:01 +00003084//===----------------------------------------------------------------------===//
3085// External Interface declarations
3086//===----------------------------------------------------------------------===//
3087
Daniel Berlind8d60462015-04-13 22:36:38 +00003088void Function::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW) const {
3089 SlotTracker SlotTable(this->getParent());
3090 formatted_raw_ostream OS(ROS);
3091 AssemblyWriter W(OS, SlotTable, this->getParent(), AAW);
3092 W.printFunction(this);
3093}
3094
Duncan P. N. Exon Smithc4f0a322015-04-15 02:12:41 +00003095void Module::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW,
3096 bool ShouldPreserveUseListOrder) const {
Chris Lattnere36fd8a2008-08-19 04:26:57 +00003097 SlotTracker SlotTable(this);
Dan Gohmane2745262009-08-12 17:23:50 +00003098 formatted_raw_ostream OS(ROS);
Duncan P. N. Exon Smithc4f0a322015-04-15 02:12:41 +00003099 AssemblyWriter W(OS, SlotTable, this, AAW, ShouldPreserveUseListOrder);
Chris Lattnerefb5e392009-12-31 02:23:35 +00003100 W.printModule(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00003101}
3102
Rafael Espindola69927782014-04-23 12:23:05 +00003103void NamedMDNode::print(raw_ostream &ROS) const {
Dan Gohman2637cc12010-07-21 23:38:33 +00003104 SlotTracker SlotTable(getParent());
3105 formatted_raw_ostream OS(ROS);
Rafael Espindola69927782014-04-23 12:23:05 +00003106 AssemblyWriter W(OS, SlotTable, getParent(), nullptr);
Dan Gohman2637cc12010-07-21 23:38:33 +00003107 W.printNamedMDNode(this);
3108}
3109
David Majnemerdad0a642014-06-27 18:19:56 +00003110void Comdat::print(raw_ostream &ROS) const {
3111 PrintLLVMName(ROS, getName(), ComdatPrefix);
3112 ROS << " = comdat ";
3113
3114 switch (getSelectionKind()) {
3115 case Comdat::Any:
3116 ROS << "any";
3117 break;
3118 case Comdat::ExactMatch:
3119 ROS << "exactmatch";
3120 break;
3121 case Comdat::Largest:
3122 ROS << "largest";
3123 break;
3124 case Comdat::NoDuplicates:
3125 ROS << "noduplicates";
3126 break;
3127 case Comdat::SameSize:
3128 ROS << "samesize";
3129 break;
3130 }
3131
3132 ROS << '\n';
3133}
3134
Chris Lattner2ee62d22009-02-28 21:11:05 +00003135void Type::print(raw_ostream &OS) const {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003136 TypePrinting TP;
3137 TP.print(const_cast<Type*>(this), OS);
Andrew Trickec4b6e72011-09-30 19:48:58 +00003138
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003139 // If the type is a named struct type, print the body as well.
3140 if (StructType *STy = dyn_cast<StructType>(const_cast<Type*>(this)))
Chris Lattner01beceb2011-08-12 18:07:07 +00003141 if (!STy->isLiteral()) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003142 OS << " = type ";
3143 TP.printStructBody(STy, OS);
3144 }
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00003145}
3146
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +00003147static bool isReferencingMDNode(const Instruction &I) {
3148 if (const auto *CI = dyn_cast<CallInst>(&I))
3149 if (Function *F = CI->getCalledFunction())
3150 if (F->isIntrinsic())
3151 for (auto &Op : I.operands())
3152 if (auto *V = dyn_cast_or_null<MetadataAsValue>(Op))
3153 if (isa<MDNode>(V->getMetadata()))
3154 return true;
3155 return false;
3156}
3157
David Blaikieb38ac1f2014-04-05 23:33:25 +00003158void Value::print(raw_ostream &ROS) const {
Dan Gohman12dad632009-08-12 20:56:03 +00003159 formatted_raw_ostream OS(ROS);
Chris Lattner0c19df42008-08-23 22:23:09 +00003160 if (const Instruction *I = dyn_cast<Instruction>(this)) {
David Blaikieb38ac1f2014-04-05 23:33:25 +00003161 const Function *F = I->getParent() ? I->getParent()->getParent() : nullptr;
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +00003162 SlotTracker SlotTable(
3163 F,
3164 /* ShouldInitializeAllMetadata */ isReferencingMDNode(*I));
David Blaikieb38ac1f2014-04-05 23:33:25 +00003165 AssemblyWriter W(OS, SlotTable, getModuleFromVal(I), nullptr);
Chris Lattnerefb5e392009-12-31 02:23:35 +00003166 W.printInstruction(*I);
Chris Lattner0c19df42008-08-23 22:23:09 +00003167 } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(this)) {
3168 SlotTracker SlotTable(BB->getParent());
David Blaikieb38ac1f2014-04-05 23:33:25 +00003169 AssemblyWriter W(OS, SlotTable, getModuleFromVal(BB), nullptr);
Chris Lattnerefb5e392009-12-31 02:23:35 +00003170 W.printBasicBlock(BB);
Chris Lattner0c19df42008-08-23 22:23:09 +00003171 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(this)) {
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +00003172 SlotTracker SlotTable(GV->getParent(),
3173 /* ShouldInitializeAllMetadata */ isa<Function>(GV));
David Blaikieb38ac1f2014-04-05 23:33:25 +00003174 AssemblyWriter W(OS, SlotTable, GV->getParent(), nullptr);
Chris Lattnerefb5e392009-12-31 02:23:35 +00003175 if (const GlobalVariable *V = dyn_cast<GlobalVariable>(GV))
3176 W.printGlobal(V);
3177 else if (const Function *F = dyn_cast<Function>(GV))
3178 W.printFunction(F);
3179 else
3180 W.printAlias(cast<GlobalAlias>(GV));
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00003181 } else if (const MetadataAsValue *V = dyn_cast<MetadataAsValue>(this)) {
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +00003182 V->getMetadata()->print(ROS, getModuleFromVal(V));
Chris Lattner0c19df42008-08-23 22:23:09 +00003183 } else if (const Constant *C = dyn_cast<Constant>(this)) {
Chris Lattner92c5c122009-02-28 23:20:19 +00003184 TypePrinting TypePrinter;
Chris Lattnere101c442009-02-28 21:26:53 +00003185 TypePrinter.print(C->getType(), OS);
Chris Lattner2ee62d22009-02-28 21:11:05 +00003186 OS << ' ';
David Blaikieb38ac1f2014-04-05 23:33:25 +00003187 WriteConstantInternal(OS, C, TypePrinter, nullptr, nullptr);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00003188 } else if (isa<InlineAsm>(this) || isa<Argument>(this)) {
Chandler Carruthd48cdbf2014-01-09 02:29:41 +00003189 this->printAsOperand(OS);
Chris Lattner0c19df42008-08-23 22:23:09 +00003190 } else {
Nick Lewyckyad1b3d12014-05-09 00:49:03 +00003191 llvm_unreachable("Unknown value to print out!");
Chris Lattner0c19df42008-08-23 22:23:09 +00003192 }
3193}
3194
Chandler Carruthd48cdbf2014-01-09 02:29:41 +00003195void Value::printAsOperand(raw_ostream &O, bool PrintType, const Module *M) const {
3196 // Fast path: Don't construct and populate a TypePrinting object if we
3197 // won't be needing any types printed.
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +00003198 bool IsMetadata = isa<MetadataAsValue>(this);
3199 if (!PrintType && ((!isa<Constant>(this) && !IsMetadata) || hasName() ||
3200 isa<GlobalValue>(this))) {
Craig Topperc6207612014-04-09 06:08:46 +00003201 WriteAsOperandInternal(O, this, nullptr, nullptr, M);
Chandler Carruthd48cdbf2014-01-09 02:29:41 +00003202 return;
3203 }
3204
3205 if (!M)
3206 M = getModuleFromVal(this);
3207
3208 TypePrinting TypePrinter;
3209 if (M)
3210 TypePrinter.incorporateTypes(*M);
3211 if (PrintType) {
3212 TypePrinter.print(getType(), O);
3213 O << ' ';
3214 }
3215
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +00003216 SlotTracker Machine(M, /* ShouldInitializeAllMetadata */ IsMetadata);
3217 WriteAsOperandInternal(O, this, &TypePrinter, &Machine, M);
Chandler Carruthd48cdbf2014-01-09 02:29:41 +00003218}
3219
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +00003220static void printMetadataImpl(raw_ostream &ROS, const Metadata &MD,
3221 const Module *M, bool OnlyAsOperand) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00003222 formatted_raw_ostream OS(ROS);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00003223
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +00003224 auto *N = dyn_cast<MDNode>(&MD);
3225 TypePrinting TypePrinter;
3226 SlotTracker Machine(M, /* ShouldInitializeAllMetadata */ N);
3227 if (M)
3228 TypePrinter.incorporateTypes(*M);
3229
3230 WriteAsOperandInternal(OS, &MD, &TypePrinter, &Machine, M,
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00003231 /* FromValue */ true);
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +00003232 if (OnlyAsOperand || !N)
3233 return;
3234
3235 OS << " = ";
3236 WriteMDNodeBodyInternal(OS, N, &TypePrinter, &Machine, M);
3237}
3238
3239void Metadata::printAsOperand(raw_ostream &OS, const Module *M) const {
3240 printMetadataImpl(OS, *this, M, /* OnlyAsOperand */ true);
3241}
3242
3243void Metadata::print(raw_ostream &OS, const Module *M) const {
3244 printMetadataImpl(OS, *this, M, /* OnlyAsOperand */ false);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00003245}
3246
Chris Lattnerdab942552008-08-25 17:03:15 +00003247// Value::dump - allow easy printing of Values from the debugger.
Duncan P. N. Exon Smith89c1eaa2015-02-25 22:08:21 +00003248LLVM_DUMP_METHOD
David Greenec7f9b122010-01-05 01:29:26 +00003249void Value::dump() const { print(dbgs()); dbgs() << '\n'; }
Reid Spencer52641832004-05-25 18:14:38 +00003250
Chris Lattnerdab942552008-08-25 17:03:15 +00003251// Type::dump - allow easy printing of Types from the debugger.
Duncan P. N. Exon Smith89c1eaa2015-02-25 22:08:21 +00003252LLVM_DUMP_METHOD
Justin Bognerc0087f32014-08-12 03:24:59 +00003253void Type::dump() const { print(dbgs()); dbgs() << '\n'; }
Chris Lattner24025262009-02-28 21:05:51 +00003254
Chris Lattnerdab942552008-08-25 17:03:15 +00003255// Module::dump() - Allow printing of Modules from the debugger.
Duncan P. N. Exon Smith89c1eaa2015-02-25 22:08:21 +00003256LLVM_DUMP_METHOD
Craig Topperc6207612014-04-09 06:08:46 +00003257void Module::dump() const { print(dbgs(), nullptr); }
Bill Wendling3681d112011-12-09 23:18:34 +00003258
David Majnemerdad0a642014-06-27 18:19:56 +00003259// \brief Allow printing of Comdats from the debugger.
Duncan P. N. Exon Smith89c1eaa2015-02-25 22:08:21 +00003260LLVM_DUMP_METHOD
David Majnemerdad0a642014-06-27 18:19:56 +00003261void Comdat::dump() const { print(dbgs()); }
3262
Bill Wendling3681d112011-12-09 23:18:34 +00003263// NamedMDNode::dump() - Allow printing of NamedMDNodes from the debugger.
Duncan P. N. Exon Smith89c1eaa2015-02-25 22:08:21 +00003264LLVM_DUMP_METHOD
Rafael Espindola69927782014-04-23 12:23:05 +00003265void NamedMDNode::dump() const { print(dbgs()); }
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00003266
Duncan P. N. Exon Smith89c1eaa2015-02-25 22:08:21 +00003267LLVM_DUMP_METHOD
Duncan P. N. Exon Smitha66dc8f2015-03-15 06:53:32 +00003268void Metadata::dump() const { dump(nullptr); }
3269
3270LLVM_DUMP_METHOD
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +00003271void Metadata::dump(const Module *M) const {
3272 print(dbgs(), M);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00003273 dbgs() << '\n';
3274}