blob: 7659a6b580dd15b772824bcd6746ff9460cef258 [file] [log] [blame]
Eugene Zelenkode6cce22017-06-19 22:05:08 +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//
Vlad Tsyrklevich3b59a8a2018-04-26 17:34:51 +000010// This library implements `print` family of functions in classes like
11// Module, Function, Value, etc. In-memory representation of those classes is
12// converted to IR strings.
Chris Lattner2f7c9632001-06-06 20:29:01 +000013//
Chris Lattner189088e2002-04-12 18:21:53 +000014// Note that these routines must be extremely tolerant of various errors in the
Chris Lattnerf70da102003-05-08 02:44:12 +000015// LLVM code, because it can be used for debugging transformations.
Chris Lattner189088e2002-04-12 18:21:53 +000016//
Chris Lattner2f7c9632001-06-06 20:29:01 +000017//===----------------------------------------------------------------------===//
18
Eugene Zelenkode6cce22017-06-19 22:05:08 +000019#include "llvm/ADT/APFloat.h"
20#include "llvm/ADT/APInt.h"
21#include "llvm/ADT/ArrayRef.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000022#include "llvm/ADT/DenseMap.h"
Eugene Zelenkode6cce22017-06-19 22:05:08 +000023#include "llvm/ADT/None.h"
24#include "llvm/ADT/Optional.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000025#include "llvm/ADT/STLExtras.h"
Benjamin Kramerba05a782015-03-17 19:53:41 +000026#include "llvm/ADT/SetVector.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000027#include "llvm/ADT/SmallString.h"
Eugene Zelenkode6cce22017-06-19 22:05:08 +000028#include "llvm/ADT/SmallVector.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000029#include "llvm/ADT/StringExtras.h"
Eugene Zelenkode6cce22017-06-19 22:05:08 +000030#include "llvm/ADT/StringRef.h"
31#include "llvm/ADT/iterator_range.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000032#include "llvm/BinaryFormat/Dwarf.h"
Nico Weber432a3882018-04-30 14:59:11 +000033#include "llvm/Config/llvm-config.h"
Eugene Zelenkode6cce22017-06-19 22:05:08 +000034#include "llvm/IR/Argument.h"
Chandler Carruth9aca9182014-01-07 12:34:26 +000035#include "llvm/IR/AssemblyAnnotationWriter.h"
Reid Klecknereb9dd5b2017-04-10 23:31:05 +000036#include "llvm/IR/Attributes.h"
Eugene Zelenkode6cce22017-06-19 22:05:08 +000037#include "llvm/IR/BasicBlock.h"
Chandler Carruth1305dc32014-03-04 11:45:46 +000038#include "llvm/IR/CFG.h"
Eugene Zelenkode6cce22017-06-19 22:05:08 +000039#include "llvm/IR/CallSite.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000040#include "llvm/IR/CallingConv.h"
Eugene Zelenkode6cce22017-06-19 22:05:08 +000041#include "llvm/IR/Comdat.h"
42#include "llvm/IR/Constant.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000043#include "llvm/IR/Constants.h"
Eugene Zelenkode6cce22017-06-19 22:05:08 +000044#include "llvm/IR/DebugInfoMetadata.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000045#include "llvm/IR/DerivedTypes.h"
Eugene Zelenkode6cce22017-06-19 22:05:08 +000046#include "llvm/IR/Function.h"
47#include "llvm/IR/GlobalAlias.h"
48#include "llvm/IR/GlobalIFunc.h"
49#include "llvm/IR/GlobalIndirectSymbol.h"
50#include "llvm/IR/GlobalObject.h"
51#include "llvm/IR/GlobalValue.h"
52#include "llvm/IR/GlobalVariable.h"
Chandler Carruthb8ddc702014-01-12 11:10:32 +000053#include "llvm/IR/IRPrintingPasses.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000054#include "llvm/IR/InlineAsm.h"
Eugene Zelenkode6cce22017-06-19 22:05:08 +000055#include "llvm/IR/InstrTypes.h"
56#include "llvm/IR/Instruction.h"
57#include "llvm/IR/Instructions.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000058#include "llvm/IR/LLVMContext.h"
Eugene Zelenkode6cce22017-06-19 22:05:08 +000059#include "llvm/IR/Metadata.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000060#include "llvm/IR/Module.h"
Duncan P. N. Exon Smith326921542015-06-26 22:04:20 +000061#include "llvm/IR/ModuleSlotTracker.h"
Teresa Johnson08d5b4e2018-05-26 02:34:13 +000062#include "llvm/IR/ModuleSummaryIndex.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000063#include "llvm/IR/Operator.h"
Igor Laevsky2aa8caf2015-05-05 13:20:42 +000064#include "llvm/IR/Statepoint.h"
Eugene Zelenkode6cce22017-06-19 22:05:08 +000065#include "llvm/IR/Type.h"
Chandler Carruthdcb603f2013-01-07 15:43:51 +000066#include "llvm/IR/TypeFinder.h"
Eugene Zelenkode6cce22017-06-19 22:05:08 +000067#include "llvm/IR/Use.h"
Benjamin Kramerba05a782015-03-17 19:53:41 +000068#include "llvm/IR/UseListOrder.h"
Eugene Zelenkode6cce22017-06-19 22:05:08 +000069#include "llvm/IR/User.h"
70#include "llvm/IR/Value.h"
71#include "llvm/Support/AtomicOrdering.h"
72#include "llvm/Support/Casting.h"
73#include "llvm/Support/Compiler.h"
David Greenec7f9b122010-01-05 01:29:26 +000074#include "llvm/Support/Debug.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000075#include "llvm/Support/ErrorHandling.h"
Justin Bogner93289572015-12-04 02:14:34 +000076#include "llvm/Support/Format.h"
Dan Gohmane2745262009-08-12 17:23:50 +000077#include "llvm/Support/FormattedStream.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000078#include "llvm/Support/raw_ostream.h"
Chris Lattnerfee714f2001-09-07 16:36:04 +000079#include <algorithm>
Eugene Zelenkode6cce22017-06-19 22:05:08 +000080#include <cassert>
Reid Spencerbdf03b42007-05-22 19:27:35 +000081#include <cctype>
Eugene Zelenkode6cce22017-06-19 22:05:08 +000082#include <cstddef>
83#include <cstdint>
84#include <iterator>
85#include <memory>
86#include <string>
87#include <tuple>
88#include <utility>
89#include <vector>
90
Chris Lattner189d19f2003-11-21 20:23:48 +000091using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000092
Reid Spencer294715b2005-05-15 16:13:11 +000093// Make virtual table appear in this compilation unit.
Eugene Zelenkode6cce22017-06-19 22:05:08 +000094AssemblyAnnotationWriter::~AssemblyAnnotationWriter() = default;
Reid Spencer294715b2005-05-15 16:13:11 +000095
Chris Lattner3eee99c2008-08-19 04:36:02 +000096//===----------------------------------------------------------------------===//
97// Helper Functions
98//===----------------------------------------------------------------------===//
99
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +0000100namespace {
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000101
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +0000102struct OrderMap {
103 DenseMap<const Value *, std::pair<unsigned, bool>> IDs;
104
105 unsigned size() const { return IDs.size(); }
106 std::pair<unsigned, bool> &operator[](const Value *V) { return IDs[V]; }
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000107
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +0000108 std::pair<unsigned, bool> lookup(const Value *V) const {
109 return IDs.lookup(V);
110 }
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000111
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +0000112 void index(const Value *V) {
113 // Explicitly sequence get-size and insert-value operations to avoid UB.
114 unsigned ID = IDs.size() + 1;
115 IDs[V].first = ID;
116 }
117};
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000118
119} // end anonymous namespace
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +0000120
121static void orderValue(const Value *V, OrderMap &OM) {
122 if (OM.lookup(V).first)
123 return;
124
125 if (const Constant *C = dyn_cast<Constant>(V))
126 if (C->getNumOperands() && !isa<GlobalValue>(C))
127 for (const Value *Op : C->operands())
128 if (!isa<BasicBlock>(Op) && !isa<GlobalValue>(Op))
129 orderValue(Op, OM);
130
131 // Note: we cannot cache this lookup above, since inserting into the map
132 // changes the map's size, and thus affects the other IDs.
133 OM.index(V);
134}
135
136static OrderMap orderModule(const Module *M) {
137 // This needs to match the order used by ValueEnumerator::ValueEnumerator()
138 // and ValueEnumerator::incorporateFunction().
139 OrderMap OM;
140
141 for (const GlobalVariable &G : M->globals()) {
142 if (G.hasInitializer())
143 if (!isa<GlobalValue>(G.getInitializer()))
144 orderValue(G.getInitializer(), OM);
145 orderValue(&G, OM);
146 }
147 for (const GlobalAlias &A : M->aliases()) {
148 if (!isa<GlobalValue>(A.getAliasee()))
149 orderValue(A.getAliasee(), OM);
150 orderValue(&A, OM);
151 }
Dmitry Polukhina1feff72016-04-07 12:32:19 +0000152 for (const GlobalIFunc &I : M->ifuncs()) {
153 if (!isa<GlobalValue>(I.getResolver()))
154 orderValue(I.getResolver(), OM);
155 orderValue(&I, OM);
156 }
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +0000157 for (const Function &F : *M) {
Vedant Kumar3a63fb32015-12-19 08:52:49 +0000158 for (const Use &U : F.operands())
159 if (!isa<GlobalValue>(U.get()))
160 orderValue(U.get(), OM);
David Majnemer7fddecc2015-06-17 20:52:32 +0000161
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +0000162 orderValue(&F, OM);
163
164 if (F.isDeclaration())
165 continue;
166
167 for (const Argument &A : F.args())
168 orderValue(&A, OM);
169 for (const BasicBlock &BB : F) {
170 orderValue(&BB, OM);
171 for (const Instruction &I : BB) {
172 for (const Value *Op : I.operands())
173 if ((isa<Constant>(*Op) && !isa<GlobalValue>(*Op)) ||
174 isa<InlineAsm>(*Op))
175 orderValue(Op, OM);
176 orderValue(&I, OM);
177 }
178 }
179 }
180 return OM;
181}
182
183static void predictValueUseListOrderImpl(const Value *V, const Function *F,
184 unsigned ID, const OrderMap &OM,
185 UseListOrderStack &Stack) {
186 // Predict use-list order for this one.
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000187 using Entry = std::pair<const Use *, unsigned>;
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +0000188 SmallVector<Entry, 64> List;
189 for (const Use &U : V->uses())
190 // Check if this user will be serialized.
191 if (OM.lookup(U.getUser()).first)
192 List.push_back(std::make_pair(&U, List.size()));
193
194 if (List.size() < 2)
195 // We may have lost some users.
196 return;
197
198 bool GetsReversed =
199 !isa<GlobalVariable>(V) && !isa<Function>(V) && !isa<BasicBlock>(V);
200 if (auto *BA = dyn_cast<BlockAddress>(V))
201 ID = OM.lookup(BA->getBasicBlock()).first;
Mandeep Singh Grangf3555652018-04-05 21:52:24 +0000202 llvm::sort(List.begin(), List.end(), [&](const Entry &L, const Entry &R) {
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +0000203 const Use *LU = L.first;
204 const Use *RU = R.first;
205 if (LU == RU)
206 return false;
207
208 auto LID = OM.lookup(LU->getUser()).first;
209 auto RID = OM.lookup(RU->getUser()).first;
210
211 // If ID is 4, then expect: 7 6 5 1 2 3.
212 if (LID < RID) {
213 if (GetsReversed)
214 if (RID <= ID)
215 return true;
216 return false;
217 }
218 if (RID < LID) {
219 if (GetsReversed)
220 if (LID <= ID)
221 return false;
222 return true;
223 }
224
225 // LID and RID are equal, so we have different operands of the same user.
226 // Assume operands are added in order for all instructions.
227 if (GetsReversed)
228 if (LID <= ID)
229 return LU->getOperandNo() < RU->getOperandNo();
230 return LU->getOperandNo() > RU->getOperandNo();
231 });
232
233 if (std::is_sorted(
234 List.begin(), List.end(),
235 [](const Entry &L, const Entry &R) { return L.second < R.second; }))
236 // Order is already correct.
237 return;
238
239 // Store the shuffle.
240 Stack.emplace_back(V, F, List.size());
241 assert(List.size() == Stack.back().Shuffle.size() && "Wrong size");
242 for (size_t I = 0, E = List.size(); I != E; ++I)
243 Stack.back().Shuffle[I] = List[I].second;
244}
245
246static void predictValueUseListOrder(const Value *V, const Function *F,
247 OrderMap &OM, UseListOrderStack &Stack) {
248 auto &IDPair = OM[V];
249 assert(IDPair.first && "Unmapped value");
250 if (IDPair.second)
251 // Already predicted.
252 return;
253
254 // Do the actual prediction.
255 IDPair.second = true;
256 if (!V->use_empty() && std::next(V->use_begin()) != V->use_end())
257 predictValueUseListOrderImpl(V, F, IDPair.first, OM, Stack);
258
259 // Recursive descent into constants.
260 if (const Constant *C = dyn_cast<Constant>(V))
261 if (C->getNumOperands()) // Visit GlobalValues.
262 for (const Value *Op : C->operands())
263 if (isa<Constant>(Op)) // Visit GlobalValues.
264 predictValueUseListOrder(Op, F, OM, Stack);
265}
266
267static UseListOrderStack predictUseListOrder(const Module *M) {
268 OrderMap OM = orderModule(M);
269
270 // Use-list orders need to be serialized after all the users have been added
271 // to a value, or else the shuffles will be incomplete. Store them per
272 // function in a stack.
273 //
274 // Aside from function order, the order of values doesn't matter much here.
275 UseListOrderStack Stack;
276
277 // We want to visit the functions backward now so we can list function-local
278 // constants in the last Function they're used in. Module-level constants
279 // have already been visited above.
Pete Cooper7679afd2015-07-24 21:13:43 +0000280 for (const Function &F : make_range(M->rbegin(), M->rend())) {
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +0000281 if (F.isDeclaration())
282 continue;
283 for (const BasicBlock &BB : F)
284 predictValueUseListOrder(&BB, &F, OM, Stack);
285 for (const Argument &A : F.args())
286 predictValueUseListOrder(&A, &F, OM, Stack);
287 for (const BasicBlock &BB : F)
288 for (const Instruction &I : BB)
289 for (const Value *Op : I.operands())
290 if (isa<Constant>(*Op) || isa<InlineAsm>(*Op)) // Visit GlobalValues.
291 predictValueUseListOrder(Op, &F, OM, Stack);
292 for (const BasicBlock &BB : F)
293 for (const Instruction &I : BB)
294 predictValueUseListOrder(&I, &F, OM, Stack);
295 }
296
297 // Visit globals last.
298 for (const GlobalVariable &G : M->globals())
299 predictValueUseListOrder(&G, nullptr, OM, Stack);
300 for (const Function &F : *M)
301 predictValueUseListOrder(&F, nullptr, OM, Stack);
302 for (const GlobalAlias &A : M->aliases())
303 predictValueUseListOrder(&A, nullptr, OM, Stack);
Dmitry Polukhina1feff72016-04-07 12:32:19 +0000304 for (const GlobalIFunc &I : M->ifuncs())
305 predictValueUseListOrder(&I, nullptr, OM, Stack);
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +0000306 for (const GlobalVariable &G : M->globals())
307 if (G.hasInitializer())
308 predictValueUseListOrder(G.getInitializer(), nullptr, OM, Stack);
309 for (const GlobalAlias &A : M->aliases())
310 predictValueUseListOrder(A.getAliasee(), nullptr, OM, Stack);
Dmitry Polukhina1feff72016-04-07 12:32:19 +0000311 for (const GlobalIFunc &I : M->ifuncs())
312 predictValueUseListOrder(I.getResolver(), nullptr, OM, Stack);
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +0000313 for (const Function &F : *M)
Vedant Kumar3a63fb32015-12-19 08:52:49 +0000314 for (const Use &U : F.operands())
315 predictValueUseListOrder(U.get(), nullptr, OM, Stack);
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +0000316
317 return Stack;
318}
319
Chris Lattner3eee99c2008-08-19 04:36:02 +0000320static const Module *getModuleFromVal(const Value *V) {
321 if (const Argument *MA = dyn_cast<Argument>(V))
Craig Topperc6207612014-04-09 06:08:46 +0000322 return MA->getParent() ? MA->getParent()->getParent() : nullptr;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000323
Chris Lattner3eee99c2008-08-19 04:36:02 +0000324 if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
Craig Topperc6207612014-04-09 06:08:46 +0000325 return BB->getParent() ? BB->getParent()->getParent() : nullptr;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000326
Chris Lattner3eee99c2008-08-19 04:36:02 +0000327 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Craig Topperc6207612014-04-09 06:08:46 +0000328 const Function *M = I->getParent() ? I->getParent()->getParent() : nullptr;
329 return M ? M->getParent() : nullptr;
Chris Lattner3eee99c2008-08-19 04:36:02 +0000330 }
Andrew Trickec4b6e72011-09-30 19:48:58 +0000331
Chris Lattner3eee99c2008-08-19 04:36:02 +0000332 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
333 return GV->getParent();
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000334
335 if (const auto *MAV = dyn_cast<MetadataAsValue>(V)) {
336 for (const User *U : MAV->users())
337 if (isa<Instruction>(U))
338 if (const Module *M = getModuleFromVal(U))
339 return M;
340 return nullptr;
341 }
342
Craig Topperc6207612014-04-09 06:08:46 +0000343 return nullptr;
Chris Lattner3eee99c2008-08-19 04:36:02 +0000344}
345
Bill Wendling90bc19c2013-02-20 07:21:42 +0000346static void PrintCallingConv(unsigned cc, raw_ostream &Out) {
Micah Villmow857186d2012-09-13 15:11:12 +0000347 switch (cc) {
Bill Wendling90bc19c2013-02-20 07:21:42 +0000348 default: Out << "cc" << cc; break;
349 case CallingConv::Fast: Out << "fastcc"; break;
350 case CallingConv::Cold: Out << "coldcc"; break;
Andrew Trick5ae6ed82013-11-11 22:40:22 +0000351 case CallingConv::WebKit_JS: Out << "webkit_jscc"; break;
352 case CallingConv::AnyReg: Out << "anyregcc"; break;
Juergen Ributzkae6250132014-01-17 19:47:03 +0000353 case CallingConv::PreserveMost: Out << "preserve_mostcc"; break;
354 case CallingConv::PreserveAll: Out << "preserve_allcc"; break;
Manman Ren19c7bbe2015-12-04 17:40:13 +0000355 case CallingConv::CXX_FAST_TLS: Out << "cxx_fast_tlscc"; break;
Reid Kleckner35fc3632014-12-01 21:04:44 +0000356 case CallingConv::GHC: Out << "ghccc"; break;
Bill Wendling90bc19c2013-02-20 07:21:42 +0000357 case CallingConv::X86_StdCall: Out << "x86_stdcallcc"; break;
358 case CallingConv::X86_FastCall: Out << "x86_fastcallcc"; break;
359 case CallingConv::X86_ThisCall: Out << "x86_thiscallcc"; break;
Oren Ben Simhon92ccbf22016-10-13 07:53:43 +0000360 case CallingConv::X86_RegCall: Out << "x86_regcallcc"; break;
Reid Kleckner9ccce992014-10-28 01:29:26 +0000361 case CallingConv::X86_VectorCall:Out << "x86_vectorcallcc"; break;
Bill Wendling90bc19c2013-02-20 07:21:42 +0000362 case CallingConv::Intel_OCL_BI: Out << "intel_ocl_bicc"; break;
363 case CallingConv::ARM_APCS: Out << "arm_apcscc"; break;
364 case CallingConv::ARM_AAPCS: Out << "arm_aapcscc"; break;
365 case CallingConv::ARM_AAPCS_VFP: Out << "arm_aapcs_vfpcc"; break;
366 case CallingConv::MSP430_INTR: Out << "msp430_intrcc"; break;
Dylan McKay4fd0d4a2016-03-03 10:08:02 +0000367 case CallingConv::AVR_INTR: Out << "avr_intrcc "; break;
368 case CallingConv::AVR_SIGNAL: Out << "avr_signalcc "; break;
Bill Wendling90bc19c2013-02-20 07:21:42 +0000369 case CallingConv::PTX_Kernel: Out << "ptx_kernel"; break;
370 case CallingConv::PTX_Device: Out << "ptx_device"; break;
Charles Davise8f297c2013-07-12 06:02:35 +0000371 case CallingConv::X86_64_SysV: Out << "x86_64_sysvcc"; break;
Martin Storsjo2f24e932017-07-17 20:05:19 +0000372 case CallingConv::Win64: Out << "win64cc"; break;
Michael Kupersteine31b4862013-12-15 10:01:20 +0000373 case CallingConv::SPIR_FUNC: Out << "spir_func"; break;
374 case CallingConv::SPIR_KERNEL: Out << "spir_kernel"; break;
Manman Renf8bdd882016-04-05 22:41:47 +0000375 case CallingConv::Swift: Out << "swiftcc"; break;
Amjad Aboud60b5e1b2015-12-21 14:07:14 +0000376 case CallingConv::X86_INTR: Out << "x86_intrcc"; break;
Maksim Panchenkocce239c2015-09-29 22:09:16 +0000377 case CallingConv::HHVM: Out << "hhvmcc"; break;
378 case CallingConv::HHVM_C: Out << "hhvm_ccc"; break;
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000379 case CallingConv::AMDGPU_VS: Out << "amdgpu_vs"; break;
Tim Renoufef1ae8f2017-09-29 09:51:22 +0000380 case CallingConv::AMDGPU_LS: Out << "amdgpu_ls"; break;
Marek Olsaka302a7362017-05-02 15:41:10 +0000381 case CallingConv::AMDGPU_HS: Out << "amdgpu_hs"; break;
Tim Renoufef1ae8f2017-09-29 09:51:22 +0000382 case CallingConv::AMDGPU_ES: Out << "amdgpu_es"; break;
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000383 case CallingConv::AMDGPU_GS: Out << "amdgpu_gs"; break;
384 case CallingConv::AMDGPU_PS: Out << "amdgpu_ps"; break;
385 case CallingConv::AMDGPU_CS: Out << "amdgpu_cs"; break;
Nikolay Haustov1f7732a2016-05-06 09:07:29 +0000386 case CallingConv::AMDGPU_KERNEL: Out << "amdgpu_kernel"; break;
Micah Villmow857186d2012-09-13 15:11:12 +0000387 }
388}
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000389
Chris Lattner3eee99c2008-08-19 04:36:02 +0000390enum PrefixType {
391 GlobalPrefix,
David Majnemerdad0a642014-06-27 18:19:56 +0000392 ComdatPrefix,
Chris Lattner3eee99c2008-08-19 04:36:02 +0000393 LabelPrefix,
Daniel Dunbar389529a2008-10-14 23:28:09 +0000394 LocalPrefix,
395 NoPrefix
Chris Lattner3eee99c2008-08-19 04:36:02 +0000396};
397
Alex Lorenz2b7f2652015-07-21 16:50:35 +0000398void llvm::printLLVMNameWithoutPrefix(raw_ostream &OS, StringRef Name) {
Jay Foadf7adb322011-04-24 14:30:00 +0000399 assert(!Name.empty() && "Cannot get empty name!");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000400
Chris Lattner3eee99c2008-08-19 04:36:02 +0000401 // Scan the name to see if it needs quotes first.
Guy Benyei83c74e92013-02-12 21:21:59 +0000402 bool NeedsQuotes = isdigit(static_cast<unsigned char>(Name[0]));
Chris Lattner3eee99c2008-08-19 04:36:02 +0000403 if (!NeedsQuotes) {
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000404 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
Aaron Ballmaned9b0a92012-07-16 16:18:18 +0000405 // By making this unsigned, the value passed in to isalnum will always be
406 // in the range 0-255. This is important when building with MSVC because
407 // its implementation will assert. This situation can arise when dealing
408 // with UTF-8 multibyte characters.
409 unsigned char C = Name[i];
Guy Benyei83c74e92013-02-12 21:21:59 +0000410 if (!isalnum(static_cast<unsigned char>(C)) && C != '-' && C != '.' &&
411 C != '_') {
Chris Lattner3eee99c2008-08-19 04:36:02 +0000412 NeedsQuotes = true;
413 break;
414 }
415 }
416 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000417
Chris Lattner3eee99c2008-08-19 04:36:02 +0000418 // If we didn't need any quotes, just write out the name in one blast.
419 if (!NeedsQuotes) {
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000420 OS << Name;
Chris Lattner3eee99c2008-08-19 04:36:02 +0000421 return;
422 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000423
Chris Lattner3eee99c2008-08-19 04:36:02 +0000424 // Okay, we need quotes. Output the quotes and escape any scary characters as
425 // needed.
426 OS << '"';
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000427 PrintEscapedString(Name, OS);
Chris Lattner3eee99c2008-08-19 04:36:02 +0000428 OS << '"';
429}
430
Alex Lorenz2b7f2652015-07-21 16:50:35 +0000431/// Turn the specified name into an 'LLVM name', which is either prefixed with %
432/// (if the string only contains simple characters) or is surrounded with ""'s
433/// (if it has special chars in it). Print it out.
434static void PrintLLVMName(raw_ostream &OS, StringRef Name, PrefixType Prefix) {
435 switch (Prefix) {
436 case NoPrefix:
437 break;
438 case GlobalPrefix:
439 OS << '@';
440 break;
441 case ComdatPrefix:
442 OS << '$';
443 break;
444 case LabelPrefix:
445 break;
446 case LocalPrefix:
447 OS << '%';
448 break;
449 }
450 printLLVMNameWithoutPrefix(OS, Name);
451}
452
453/// Turn the specified name into an 'LLVM name', which is either prefixed with %
454/// (if the string only contains simple characters) or is surrounded with ""'s
455/// (if it has special chars in it). Print it out.
Dan Gohman12dad632009-08-12 20:56:03 +0000456static void PrintLLVMName(raw_ostream &OS, const Value *V) {
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000457 PrintLLVMName(OS, V->getName(),
Chris Lattner3eee99c2008-08-19 04:36:02 +0000458 isa<GlobalValue>(V) ? GlobalPrefix : LocalPrefix);
459}
460
Benjamin Kramerba05a782015-03-17 19:53:41 +0000461namespace {
Benjamin Kramerba05a782015-03-17 19:53:41 +0000462
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000463class TypePrinting {
464public:
Roman Tereshind96de6f2018-03-22 21:29:07 +0000465 TypePrinting(const Module *M = nullptr) : DeferredM(M) {}
Benjamin Kramerba05a782015-03-17 19:53:41 +0000466
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000467 TypePrinting(const TypePrinting &) = delete;
468 TypePrinting &operator=(const TypePrinting &) = delete;
Benjamin Kramerba05a782015-03-17 19:53:41 +0000469
Roman Tereshind96de6f2018-03-22 21:29:07 +0000470 /// The named types that are used by the current module.
471 TypeFinder &getNamedTypes();
472
473 /// The numbered types, number to type mapping.
474 std::vector<StructType *> &getNumberedTypes();
475
476 bool empty();
Benjamin Kramerba05a782015-03-17 19:53:41 +0000477
478 void print(Type *Ty, raw_ostream &OS);
479
480 void printStructBody(StructType *Ty, raw_ostream &OS);
Roman Tereshind96de6f2018-03-22 21:29:07 +0000481
482private:
483 void incorporateTypes();
484
485 /// A module to process lazily when needed. Set to nullptr as soon as used.
486 const Module *DeferredM;
487
488 TypeFinder NamedTypes;
489
490 // The numbered types, along with their value.
491 DenseMap<StructType *, unsigned> Type2Number;
492
493 std::vector<StructType *> NumberedTypes;
Benjamin Kramerba05a782015-03-17 19:53:41 +0000494};
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000495
496} // end anonymous namespace
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000497
Roman Tereshind96de6f2018-03-22 21:29:07 +0000498TypeFinder &TypePrinting::getNamedTypes() {
499 incorporateTypes();
500 return NamedTypes;
501}
502
503std::vector<StructType *> &TypePrinting::getNumberedTypes() {
504 incorporateTypes();
505
506 // We know all the numbers that each type is used and we know that it is a
507 // dense assignment. Convert the map to an index table, if it's not done
508 // already (judging from the sizes):
509 if (NumberedTypes.size() == Type2Number.size())
510 return NumberedTypes;
511
512 NumberedTypes.resize(Type2Number.size());
513 for (const auto &P : Type2Number) {
514 assert(P.second < NumberedTypes.size() && "Didn't get a dense numbering?");
515 assert(!NumberedTypes[P.second] && "Didn't get a unique numbering?");
516 NumberedTypes[P.second] = P.first;
517 }
518 return NumberedTypes;
519}
520
521bool TypePrinting::empty() {
522 incorporateTypes();
523 return NamedTypes.empty() && Type2Number.empty();
524}
525
526void TypePrinting::incorporateTypes() {
527 if (!DeferredM)
528 return;
529
530 NamedTypes.run(*DeferredM, false);
531 DeferredM = nullptr;
Andrew Trickec4b6e72011-09-30 19:48:58 +0000532
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000533 // The list of struct types we got back includes all the struct types, split
534 // the unnamed ones out to a numbering and remove the anonymous structs.
535 unsigned NextNumber = 0;
Andrew Trickec4b6e72011-09-30 19:48:58 +0000536
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000537 std::vector<StructType*>::iterator NextToUse = NamedTypes.begin(), I, E;
538 for (I = NamedTypes.begin(), E = NamedTypes.end(); I != E; ++I) {
539 StructType *STy = *I;
Andrew Trickec4b6e72011-09-30 19:48:58 +0000540
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000541 // Ignore anonymous types.
Chris Lattner01beceb2011-08-12 18:07:07 +0000542 if (STy->isLiteral())
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000543 continue;
Andrew Trickec4b6e72011-09-30 19:48:58 +0000544
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000545 if (STy->getName().empty())
Roman Tereshind96de6f2018-03-22 21:29:07 +0000546 Type2Number[STy] = NextNumber++;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000547 else
548 *NextToUse++ = STy;
549 }
Andrew Trickec4b6e72011-09-30 19:48:58 +0000550
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000551 NamedTypes.erase(NextToUse, NamedTypes.end());
552}
553
Roman Tereshind96de6f2018-03-22 21:29:07 +0000554/// Write the specified type to the specified raw_ostream, making use of type
555/// names or up references to shorten the type name where possible.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000556void TypePrinting::print(Type *Ty, raw_ostream &OS) {
Chris Lattner0f578952009-02-28 20:25:14 +0000557 switch (Ty->getTypeID()) {
Rafael Espindolaba7df702013-12-07 02:27:52 +0000558 case Type::VoidTyID: OS << "void"; return;
559 case Type::HalfTyID: OS << "half"; return;
560 case Type::FloatTyID: OS << "float"; return;
561 case Type::DoubleTyID: OS << "double"; return;
562 case Type::X86_FP80TyID: OS << "x86_fp80"; return;
563 case Type::FP128TyID: OS << "fp128"; return;
564 case Type::PPC_FP128TyID: OS << "ppc_fp128"; return;
565 case Type::LabelTyID: OS << "label"; return;
566 case Type::MetadataTyID: OS << "metadata"; return;
567 case Type::X86_MMXTyID: OS << "x86_mmx"; return;
David Majnemerb611e3f2015-08-14 05:09:07 +0000568 case Type::TokenTyID: OS << "token"; return;
Chris Lattner68318b12009-02-28 21:18:43 +0000569 case Type::IntegerTyID:
Chris Lattner06a23212009-02-28 21:27:31 +0000570 OS << 'i' << cast<IntegerType>(Ty)->getBitWidth();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000571 return;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000572
Chris Lattner07d88292009-02-28 20:35:42 +0000573 case Type::FunctionTyID: {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000574 FunctionType *FTy = cast<FunctionType>(Ty);
575 print(FTy->getReturnType(), OS);
Chris Lattner06a23212009-02-28 21:27:31 +0000576 OS << " (";
Chris Lattner07d88292009-02-28 20:35:42 +0000577 for (FunctionType::param_iterator I = FTy->param_begin(),
578 E = FTy->param_end(); I != E; ++I) {
579 if (I != FTy->param_begin())
Chris Lattner06a23212009-02-28 21:27:31 +0000580 OS << ", ";
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000581 print(*I, OS);
Chris Lattner0f578952009-02-28 20:25:14 +0000582 }
Chris Lattner07d88292009-02-28 20:35:42 +0000583 if (FTy->isVarArg()) {
Chris Lattner06a23212009-02-28 21:27:31 +0000584 if (FTy->getNumParams()) OS << ", ";
585 OS << "...";
Chris Lattner0f578952009-02-28 20:25:14 +0000586 }
Chris Lattner06a23212009-02-28 21:27:31 +0000587 OS << ')';
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000588 return;
Chris Lattner07d88292009-02-28 20:35:42 +0000589 }
590 case Type::StructTyID: {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000591 StructType *STy = cast<StructType>(Ty);
Andrew Trickec4b6e72011-09-30 19:48:58 +0000592
Chris Lattner01beceb2011-08-12 18:07:07 +0000593 if (STy->isLiteral())
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000594 return printStructBody(STy, OS);
595
596 if (!STy->getName().empty())
597 return PrintLLVMName(OS, STy->getName(), LocalPrefix);
Andrew Trickec4b6e72011-09-30 19:48:58 +0000598
Roman Tereshind96de6f2018-03-22 21:29:07 +0000599 incorporateTypes();
600 const auto I = Type2Number.find(STy);
601 if (I != Type2Number.end())
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000602 OS << '%' << I->second;
603 else // Not enumerated, print the hex address.
Benjamin Kramer4e8b4632011-11-02 17:24:36 +0000604 OS << "%\"type " << STy << '\"';
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000605 return;
Chris Lattner07d88292009-02-28 20:35:42 +0000606 }
607 case Type::PointerTyID: {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000608 PointerType *PTy = cast<PointerType>(Ty);
609 print(PTy->getElementType(), OS);
Chris Lattner07d88292009-02-28 20:35:42 +0000610 if (unsigned AddressSpace = PTy->getAddressSpace())
Chris Lattner06a23212009-02-28 21:27:31 +0000611 OS << " addrspace(" << AddressSpace << ')';
612 OS << '*';
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000613 return;
Chris Lattner07d88292009-02-28 20:35:42 +0000614 }
615 case Type::ArrayTyID: {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000616 ArrayType *ATy = cast<ArrayType>(Ty);
Chris Lattner06a23212009-02-28 21:27:31 +0000617 OS << '[' << ATy->getNumElements() << " x ";
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000618 print(ATy->getElementType(), OS);
Chris Lattner06a23212009-02-28 21:27:31 +0000619 OS << ']';
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000620 return;
Chris Lattner07d88292009-02-28 20:35:42 +0000621 }
622 case Type::VectorTyID: {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000623 VectorType *PTy = cast<VectorType>(Ty);
Chris Lattner06a23212009-02-28 21:27:31 +0000624 OS << "<" << PTy->getNumElements() << " x ";
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000625 print(PTy->getElementType(), OS);
Chris Lattner06a23212009-02-28 21:27:31 +0000626 OS << '>';
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000627 return;
Chris Lattner07d88292009-02-28 20:35:42 +0000628 }
Chris Lattner0f578952009-02-28 20:25:14 +0000629 }
Rafael Espindolaba7df702013-12-07 02:27:52 +0000630 llvm_unreachable("Invalid TypeID");
Chris Lattner0f578952009-02-28 20:25:14 +0000631}
632
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000633void TypePrinting::printStructBody(StructType *STy, raw_ostream &OS) {
634 if (STy->isOpaque()) {
635 OS << "opaque";
636 return;
Chris Lattner0f578952009-02-28 20:25:14 +0000637 }
Andrew Trickec4b6e72011-09-30 19:48:58 +0000638
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000639 if (STy->isPacked())
640 OS << '<';
Andrew Trickec4b6e72011-09-30 19:48:58 +0000641
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000642 if (STy->getNumElements() == 0) {
643 OS << "{}";
644 } else {
645 StructType::element_iterator I = STy->element_begin();
646 OS << "{ ";
647 print(*I++, OS);
648 for (StructType::element_iterator E = STy->element_end(); I != E; ++I) {
649 OS << ", ";
650 print(*I, OS);
Chris Lattner3243ea12009-03-01 00:03:38 +0000651 }
Andrew Trickec4b6e72011-09-30 19:48:58 +0000652
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000653 OS << " }";
Chris Lattner92c5c122009-02-28 23:20:19 +0000654 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000655 if (STy->isPacked())
656 OS << '>';
Chris Lattner92c5c122009-02-28 23:20:19 +0000657}
658
Duncan P. N. Exon Smith326921542015-06-26 22:04:20 +0000659namespace llvm {
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000660
Chris Lattner3eee99c2008-08-19 04:36:02 +0000661//===----------------------------------------------------------------------===//
662// SlotTracker Class: Enumerate slot numbers for unnamed values
663//===----------------------------------------------------------------------===//
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000664/// This class provides computation of slot numbers for LLVM Assembly writing.
Chris Lattner393b7cd2008-08-17 04:17:45 +0000665///
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000666class SlotTracker {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000667public:
Devang Patel983c6b12009-07-08 21:44:25 +0000668 /// ValueMap - A mapping of Values to slot numbers.
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000669 using ValueMap = DenseMap<const Value *, unsigned>;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000670
671private:
Devang Patel983c6b12009-07-08 21:44:25 +0000672 /// TheModule - The module for which we are holding slot numbers.
Chris Lattner393b7cd2008-08-17 04:17:45 +0000673 const Module* TheModule;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000674
Devang Patel983c6b12009-07-08 21:44:25 +0000675 /// TheFunction - The function for which we are holding slot numbers.
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000676 const Function* TheFunction = nullptr;
677 bool FunctionProcessed = false;
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000678 bool ShouldInitializeAllMetadata;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000679
Teresa Johnson08d5b4e2018-05-26 02:34:13 +0000680 /// The summary index for which we are holding slot numbers.
681 const ModuleSummaryIndex *TheIndex = nullptr;
682
Jay Foaddbb221d2011-07-11 07:28:49 +0000683 /// mMap - The slot map for the module level data.
Chris Lattner393b7cd2008-08-17 04:17:45 +0000684 ValueMap mMap;
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000685 unsigned mNext = 0;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000686
Jay Foaddbb221d2011-07-11 07:28:49 +0000687 /// fMap - The slot map for the function level data.
Chris Lattner393b7cd2008-08-17 04:17:45 +0000688 ValueMap fMap;
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000689 unsigned fNext = 0;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000690
Devang Patel983c6b12009-07-08 21:44:25 +0000691 /// mdnMap - Map for MDNodes.
Chris Lattnercf4a76e2009-12-31 02:20:11 +0000692 DenseMap<const MDNode*, unsigned> mdnMap;
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000693 unsigned mdnNext = 0;
Bill Wendling829b4782013-02-11 08:43:33 +0000694
695 /// asMap - The slot map for attribute sets.
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000696 DenseMap<AttributeSet, unsigned> asMap;
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000697 unsigned asNext = 0;
698
Teresa Johnson08d5b4e2018-05-26 02:34:13 +0000699 /// GUIDMap - The slot map for GUIDs used in the summary index.
700 DenseMap<GlobalValue::GUID, unsigned> GUIDMap;
701 unsigned GUIDNext = 0;
702
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000703public:
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000704 /// Construct from a module.
705 ///
706 /// If \c ShouldInitializeAllMetadata, initializes all metadata in all
707 /// functions, giving correct numbering for metadata referenced only from
708 /// within a function (even if no functions have been initialized).
709 explicit SlotTracker(const Module *M,
710 bool ShouldInitializeAllMetadata = false);
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000711
Chris Lattner393b7cd2008-08-17 04:17:45 +0000712 /// Construct from a function, starting out in incorp state.
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000713 ///
714 /// If \c ShouldInitializeAllMetadata, initializes all metadata in all
715 /// functions, giving correct numbering for metadata referenced only from
716 /// within a function (even if no functions have been initialized).
717 explicit SlotTracker(const Function *F,
718 bool ShouldInitializeAllMetadata = false);
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000719
Teresa Johnson08d5b4e2018-05-26 02:34:13 +0000720 /// Construct from a module summary index.
721 explicit SlotTracker(const ModuleSummaryIndex *Index);
722
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000723 SlotTracker(const SlotTracker &) = delete;
724 SlotTracker &operator=(const SlotTracker &) = delete;
725
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000726 /// Return the slot number of the specified value in it's type
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000727 /// plane. If something is not in the SlotTracker, return -1.
Chris Lattner5e043322007-01-11 03:54:27 +0000728 int getLocalSlot(const Value *V);
729 int getGlobalSlot(const GlobalValue *V);
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000730 int getMetadataSlot(const MDNode *N);
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000731 int getAttributeGroupSlot(AttributeSet AS);
Teresa Johnson08d5b4e2018-05-26 02:34:13 +0000732 int getGUIDSlot(GlobalValue::GUID GUID);
Reid Spencer8beac692004-06-09 15:26:53 +0000733
Misha Brukmanb1c93172005-04-21 23:48:37 +0000734 /// If you'd like to deal with a function instead of just a module, use
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000735 /// this method to get its data into the SlotTracker.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000736 void incorporateFunction(const Function *F) {
737 TheFunction = F;
Reid Spencerb0ac8c42004-08-16 07:46:33 +0000738 FunctionProcessed = false;
739 }
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000740
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +0000741 const Function *getFunction() const { return TheFunction; }
742
Misha Brukmanb1c93172005-04-21 23:48:37 +0000743 /// After calling incorporateFunction, use this method to remove the
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000744 /// most recently incorporated function from the SlotTracker. This
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000745 /// will reset the state of the machine back to just the module contents.
746 void purgeFunction();
747
Devang Patel983c6b12009-07-08 21:44:25 +0000748 /// MDNode map iterators.
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000749 using mdn_iterator = DenseMap<const MDNode*, unsigned>::iterator;
750
Chris Lattnercf4a76e2009-12-31 02:20:11 +0000751 mdn_iterator mdn_begin() { return mdnMap.begin(); }
752 mdn_iterator mdn_end() { return mdnMap.end(); }
753 unsigned mdn_size() const { return mdnMap.size(); }
754 bool mdn_empty() const { return mdnMap.empty(); }
Devang Patel983c6b12009-07-08 21:44:25 +0000755
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000756 /// AttributeSet map iterators.
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000757 using as_iterator = DenseMap<AttributeSet, unsigned>::iterator;
758
Bill Wendling829b4782013-02-11 08:43:33 +0000759 as_iterator as_begin() { return asMap.begin(); }
760 as_iterator as_end() { return asMap.end(); }
761 unsigned as_size() const { return asMap.size(); }
762 bool as_empty() const { return asMap.empty(); }
763
Teresa Johnson08d5b4e2018-05-26 02:34:13 +0000764 /// GUID map iterators.
765 using guid_iterator = DenseMap<GlobalValue::GUID, unsigned>::iterator;
766
767 /// These functions do the actual initialization.
Reid Spencer56010e42004-05-26 21:56:09 +0000768 inline void initialize();
Teresa Johnson08d5b4e2018-05-26 02:34:13 +0000769 void initializeIndex();
Reid Spencer56010e42004-05-26 21:56:09 +0000770
Devang Patel983c6b12009-07-08 21:44:25 +0000771 // Implementation Details
772private:
Chris Lattnerea862a32007-01-09 07:55:49 +0000773 /// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
774 void CreateModuleSlot(const GlobalValue *V);
Devang Patel983c6b12009-07-08 21:44:25 +0000775
776 /// CreateMetadataSlot - Insert the specified MDNode* into the slot table.
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000777 void CreateMetadataSlot(const MDNode *N);
Devang Patel983c6b12009-07-08 21:44:25 +0000778
Chris Lattnerea862a32007-01-09 07:55:49 +0000779 /// CreateFunctionSlot - Insert the specified Value* into the slot table.
780 void CreateFunctionSlot(const Value *V);
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000781
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000782 /// Insert the specified AttributeSet into the slot table.
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000783 void CreateAttributeSetSlot(AttributeSet AS);
Bill Wendling829b4782013-02-11 08:43:33 +0000784
Teresa Johnson08d5b4e2018-05-26 02:34:13 +0000785 void CreateGUIDSlot(GlobalValue::GUID GUID);
786
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000787 /// Add all of the module level global variables (and their initializers)
788 /// and function declarations, but not the contents of those functions.
789 void processModule();
Teresa Johnson08d5b4e2018-05-26 02:34:13 +0000790 void processIndex();
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000791
Devang Patel983c6b12009-07-08 21:44:25 +0000792 /// Add all of the functions arguments, basic blocks, and instructions.
Reid Spencer56010e42004-05-26 21:56:09 +0000793 void processFunction();
794
Peter Collingbournecceae7f2016-05-31 23:01:54 +0000795 /// Add the metadata directly attached to a GlobalObject.
796 void processGlobalObjectMetadata(const GlobalObject &GO);
797
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000798 /// Add all of the metadata from a function.
799 void processFunctionMetadata(const Function &F);
800
Duncan P. N. Exon Smith20b76ac2015-03-14 19:48:31 +0000801 /// Add all of the metadata from an instruction.
802 void processInstructionMetadata(const Instruction &I);
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000803};
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000804
805} // end namespace llvm
Duncan P. N. Exon Smith326921542015-06-26 22:04:20 +0000806
807ModuleSlotTracker::ModuleSlotTracker(SlotTracker &Machine, const Module *M,
808 const Function *F)
809 : M(M), F(F), Machine(&Machine) {}
810
Duncan P. N. Exon Smith6529ed42015-06-26 22:28:47 +0000811ModuleSlotTracker::ModuleSlotTracker(const Module *M,
812 bool ShouldInitializeAllMetadata)
Duncan P. N. Exon Smithaf0fdc22016-04-20 19:05:59 +0000813 : ShouldCreateStorage(M),
814 ShouldInitializeAllMetadata(ShouldInitializeAllMetadata), M(M) {}
Duncan P. N. Exon Smith326921542015-06-26 22:04:20 +0000815
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000816ModuleSlotTracker::~ModuleSlotTracker() = default;
Duncan P. N. Exon Smith326921542015-06-26 22:04:20 +0000817
Duncan P. N. Exon Smithaf0fdc22016-04-20 19:05:59 +0000818SlotTracker *ModuleSlotTracker::getMachine() {
819 if (!ShouldCreateStorage)
820 return Machine;
821
822 ShouldCreateStorage = false;
823 MachineStorage =
824 llvm::make_unique<SlotTracker>(M, ShouldInitializeAllMetadata);
825 Machine = MachineStorage.get();
826 return Machine;
827}
828
Duncan P. N. Exon Smith326921542015-06-26 22:04:20 +0000829void ModuleSlotTracker::incorporateFunction(const Function &F) {
Duncan P. N. Exon Smithaf0fdc22016-04-20 19:05:59 +0000830 // Using getMachine() may lazily create the slot tracker.
831 if (!getMachine())
Duncan P. N. Exon Smith326921542015-06-26 22:04:20 +0000832 return;
833
834 // Nothing to do if this is the right function already.
835 if (this->F == &F)
836 return;
837 if (this->F)
838 Machine->purgeFunction();
839 Machine->incorporateFunction(&F);
840 this->F = &F;
841}
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000842
Alex Lorenz991a6242015-07-27 22:31:04 +0000843int ModuleSlotTracker::getLocalSlot(const Value *V) {
844 assert(F && "No function incorporated");
845 return Machine->getLocalSlot(V);
846}
847
Chris Lattner3eee99c2008-08-19 04:36:02 +0000848static SlotTracker *createSlotTracker(const Value *V) {
849 if (const Argument *FA = dyn_cast<Argument>(V))
850 return new SlotTracker(FA->getParent());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000851
Chris Lattner3eee99c2008-08-19 04:36:02 +0000852 if (const Instruction *I = dyn_cast<Instruction>(V))
Andrew Trick2f0cbf62011-09-30 19:50:40 +0000853 if (I->getParent())
854 return new SlotTracker(I->getParent()->getParent());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000855
Chris Lattner3eee99c2008-08-19 04:36:02 +0000856 if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
857 return new SlotTracker(BB->getParent());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000858
Chris Lattner3eee99c2008-08-19 04:36:02 +0000859 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
860 return new SlotTracker(GV->getParent());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000861
Chris Lattner3eee99c2008-08-19 04:36:02 +0000862 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V))
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000863 return new SlotTracker(GA->getParent());
864
Dmitry Polukhina1feff72016-04-07 12:32:19 +0000865 if (const GlobalIFunc *GIF = dyn_cast<GlobalIFunc>(V))
866 return new SlotTracker(GIF->getParent());
867
Chris Lattner3eee99c2008-08-19 04:36:02 +0000868 if (const Function *Func = dyn_cast<Function>(V))
869 return new SlotTracker(Func);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000870
Craig Topperc6207612014-04-09 06:08:46 +0000871 return nullptr;
Chris Lattner3eee99c2008-08-19 04:36:02 +0000872}
873
874#if 0
David Greenec7f9b122010-01-05 01:29:26 +0000875#define ST_DEBUG(X) dbgs() << X
Chris Lattner3eee99c2008-08-19 04:36:02 +0000876#else
Chris Lattner604e3512008-08-19 04:47:09 +0000877#define ST_DEBUG(X)
Chris Lattner3eee99c2008-08-19 04:36:02 +0000878#endif
879
880// Module level constructor. Causes the contents of the Module (sans functions)
881// to be added to the slot table.
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000882SlotTracker::SlotTracker(const Module *M, bool ShouldInitializeAllMetadata)
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000883 : TheModule(M), ShouldInitializeAllMetadata(ShouldInitializeAllMetadata) {}
Chris Lattner3eee99c2008-08-19 04:36:02 +0000884
885// Function level constructor. Causes the contents of the Module and the one
886// function provided to be added to the slot table.
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000887SlotTracker::SlotTracker(const Function *F, bool ShouldInitializeAllMetadata)
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000888 : TheModule(F ? F->getParent() : nullptr), TheFunction(F),
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000889 ShouldInitializeAllMetadata(ShouldInitializeAllMetadata) {}
Chris Lattner3eee99c2008-08-19 04:36:02 +0000890
Teresa Johnson08d5b4e2018-05-26 02:34:13 +0000891SlotTracker::SlotTracker(const ModuleSummaryIndex *Index)
892 : TheModule(nullptr), ShouldInitializeAllMetadata(false), TheIndex(Index) {}
893
Chris Lattner3eee99c2008-08-19 04:36:02 +0000894inline void SlotTracker::initialize() {
895 if (TheModule) {
896 processModule();
Craig Topperc6207612014-04-09 06:08:46 +0000897 TheModule = nullptr; ///< Prevent re-processing next time we're called.
Chris Lattner3eee99c2008-08-19 04:36:02 +0000898 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000899
Chris Lattner3eee99c2008-08-19 04:36:02 +0000900 if (TheFunction && !FunctionProcessed)
901 processFunction();
902}
903
Teresa Johnson08d5b4e2018-05-26 02:34:13 +0000904void SlotTracker::initializeIndex() {
905 if (!TheIndex)
906 return;
907 processIndex();
908 TheIndex = nullptr; ///< Prevent re-processing next time we're called.
909}
910
Chris Lattner3eee99c2008-08-19 04:36:02 +0000911// Iterate through all the global variables, functions, and global
912// variable initializers and create slots for them.
913void SlotTracker::processModule() {
Chris Lattner604e3512008-08-19 04:47:09 +0000914 ST_DEBUG("begin processModule!\n");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000915
Chris Lattner3eee99c2008-08-19 04:36:02 +0000916 // Add all of the unnamed global variables to the value table.
Rafael Espindola5d8a1552015-06-17 17:33:37 +0000917 for (const GlobalVariable &Var : TheModule->globals()) {
918 if (!Var.hasName())
919 CreateModuleSlot(&Var);
Peter Collingbournecceae7f2016-05-31 23:01:54 +0000920 processGlobalObjectMetadata(Var);
Javed Absarf3d79042017-05-11 12:28:08 +0000921 auto Attrs = Var.getAttributes();
922 if (Attrs.hasAttributes())
923 CreateAttributeSetSlot(Attrs);
Devang Patel983c6b12009-07-08 21:44:25 +0000924 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000925
Rafael Espindola54fc2982015-06-17 17:53:31 +0000926 for (const GlobalAlias &A : TheModule->aliases()) {
927 if (!A.hasName())
928 CreateModuleSlot(&A);
929 }
930
Dmitry Polukhina1feff72016-04-07 12:32:19 +0000931 for (const GlobalIFunc &I : TheModule->ifuncs()) {
932 if (!I.hasName())
933 CreateModuleSlot(&I);
934 }
935
Devang Patel23e68302009-07-29 22:04:47 +0000936 // Add metadata used by named metadata.
Rafael Espindola5d8a1552015-06-17 17:33:37 +0000937 for (const NamedMDNode &NMD : TheModule->named_metadata()) {
938 for (unsigned i = 0, e = NMD.getNumOperands(); i != e; ++i)
939 CreateMetadataSlot(NMD.getOperand(i));
Devang Patel23e68302009-07-29 22:04:47 +0000940 }
941
Rafael Espindola5d8a1552015-06-17 17:33:37 +0000942 for (const Function &F : *TheModule) {
943 if (!F.hasName())
Bill Wendling829b4782013-02-11 08:43:33 +0000944 // Add all the unnamed functions to the table.
Rafael Espindola5d8a1552015-06-17 17:33:37 +0000945 CreateModuleSlot(&F);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000946
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000947 if (ShouldInitializeAllMetadata)
Rafael Espindola5d8a1552015-06-17 17:33:37 +0000948 processFunctionMetadata(F);
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000949
Bill Wendling829b4782013-02-11 08:43:33 +0000950 // Add all the function attributes to the table.
Bill Wendling90bc19c2013-02-20 07:21:42 +0000951 // FIXME: Add attributes of other objects?
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000952 AttributeSet FnAttrs = F.getAttributes().getFnAttributes();
953 if (FnAttrs.hasAttributes())
Bill Wendling829b4782013-02-11 08:43:33 +0000954 CreateAttributeSetSlot(FnAttrs);
955 }
956
Chris Lattner604e3512008-08-19 04:47:09 +0000957 ST_DEBUG("end processModule!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000958}
959
Chris Lattner3eee99c2008-08-19 04:36:02 +0000960// Process the arguments, basic blocks, and instructions of a function.
961void SlotTracker::processFunction() {
Chris Lattner604e3512008-08-19 04:47:09 +0000962 ST_DEBUG("begin processFunction!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000963 fNext = 0;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000964
Duncan P. N. Exon Smithadc54562015-09-11 01:34:59 +0000965 // Process function metadata if it wasn't hit at the module-level.
966 if (!ShouldInitializeAllMetadata)
967 processFunctionMetadata(*TheFunction);
968
Chris Lattner3eee99c2008-08-19 04:36:02 +0000969 // Add all the function arguments with no names.
970 for(Function::const_arg_iterator AI = TheFunction->arg_begin(),
971 AE = TheFunction->arg_end(); AI != AE; ++AI)
972 if (!AI->hasName())
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +0000973 CreateFunctionSlot(&*AI);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000974
Chris Lattner604e3512008-08-19 04:47:09 +0000975 ST_DEBUG("Inserting Instructions:\n");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000976
Chris Lattner3eee99c2008-08-19 04:36:02 +0000977 // Add all of the basic blocks and instructions with no names.
Duncan P. N. Exon Smith27f33ee2015-03-14 19:44:01 +0000978 for (auto &BB : *TheFunction) {
979 if (!BB.hasName())
980 CreateFunctionSlot(&BB);
Andrew Trickec4b6e72011-09-30 19:48:58 +0000981
Duncan P. N. Exon Smith27f33ee2015-03-14 19:44:01 +0000982 for (auto &I : BB) {
983 if (!I.getType()->isVoidTy() && !I.hasName())
984 CreateFunctionSlot(&I);
Andrew Trickec4b6e72011-09-30 19:48:58 +0000985
Duncan P. N. Exon Smith20b76ac2015-03-14 19:48:31 +0000986 // We allow direct calls to any llvm.foo function here, because the
987 // target may not be linked into the optimizer.
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000988 if (auto CS = ImmutableCallSite(&I)) {
Bill Wendlinga0323742013-02-22 09:09:42 +0000989 // Add all the call attributes to the table.
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000990 AttributeSet Attrs = CS.getAttributes().getFnAttributes();
991 if (Attrs.hasAttributes())
Bill Wendlinga0323742013-02-22 09:09:42 +0000992 CreateAttributeSetSlot(Attrs);
Chris Lattner58aff8f2010-05-10 20:53:17 +0000993 }
Devang Patel983c6b12009-07-08 21:44:25 +0000994 }
Chris Lattner3eee99c2008-08-19 04:36:02 +0000995 }
Devang Pateldec23fd2009-09-16 20:21:17 +0000996
Chris Lattner3eee99c2008-08-19 04:36:02 +0000997 FunctionProcessed = true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000998
Chris Lattner604e3512008-08-19 04:47:09 +0000999 ST_DEBUG("end processFunction!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +00001000}
1001
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00001002// Iterate through all the GUID in the index and create slots for them.
1003void SlotTracker::processIndex() {
1004 ST_DEBUG("begin processIndex!\n");
1005 assert(TheIndex);
1006
1007 // The first block of slots are just the module ids, which start at 0 and are
1008 // assigned consecutively. Start numbering the GUIDs after the module ids.
1009 GUIDNext = TheIndex->modulePaths().size();
1010
1011 for (auto &GlobalList : *TheIndex)
1012 CreateGUIDSlot(GlobalList.first);
1013
1014 for (auto &TId : TheIndex->typeIds())
1015 CreateGUIDSlot(GlobalValue::getGUID(TId.first));
1016
1017 ST_DEBUG("end processIndex!\n");
1018}
1019
Peter Collingbournecceae7f2016-05-31 23:01:54 +00001020void SlotTracker::processGlobalObjectMetadata(const GlobalObject &GO) {
Duncan P. N. Exon Smith3d4cd752015-04-24 22:04:41 +00001021 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
Peter Collingbournecceae7f2016-05-31 23:01:54 +00001022 GO.getAllMetadata(MDs);
Duncan P. N. Exon Smithadc54562015-09-11 01:34:59 +00001023 for (auto &MD : MDs)
1024 CreateMetadataSlot(MD.second);
Peter Collingbournecceae7f2016-05-31 23:01:54 +00001025}
Duncan P. N. Exon Smith3d4cd752015-04-24 22:04:41 +00001026
Peter Collingbournecceae7f2016-05-31 23:01:54 +00001027void SlotTracker::processFunctionMetadata(const Function &F) {
1028 processGlobalObjectMetadata(F);
Duncan P. N. Exon Smithadc54562015-09-11 01:34:59 +00001029 for (auto &BB : F) {
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +00001030 for (auto &I : BB)
1031 processInstructionMetadata(I);
Duncan P. N. Exon Smith3d4cd752015-04-24 22:04:41 +00001032 }
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +00001033}
1034
Duncan P. N. Exon Smith20b76ac2015-03-14 19:48:31 +00001035void SlotTracker::processInstructionMetadata(const Instruction &I) {
1036 // Process metadata used directly by intrinsics.
1037 if (const CallInst *CI = dyn_cast<CallInst>(&I))
1038 if (Function *F = CI->getCalledFunction())
Justin Lebar9cbc3012016-07-28 23:58:15 +00001039 if (F->isIntrinsic())
Duncan P. N. Exon Smith20b76ac2015-03-14 19:48:31 +00001040 for (auto &Op : I.operands())
1041 if (auto *V = dyn_cast_or_null<MetadataAsValue>(Op))
1042 if (MDNode *N = dyn_cast<MDNode>(V->getMetadata()))
1043 CreateMetadataSlot(N);
1044
1045 // Process metadata attached to this instruction.
1046 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
1047 I.getAllMetadata(MDs);
1048 for (auto &MD : MDs)
1049 CreateMetadataSlot(MD.second);
1050}
1051
Chris Lattner3eee99c2008-08-19 04:36:02 +00001052/// Clean up after incorporating a function. This is the only way to get out of
1053/// the function incorporation state that affects get*Slot/Create*Slot. Function
1054/// incorporation state is indicated by TheFunction != 0.
1055void SlotTracker::purgeFunction() {
Chris Lattner604e3512008-08-19 04:47:09 +00001056 ST_DEBUG("begin purgeFunction!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +00001057 fMap.clear(); // Simply discard the function level map
Craig Topperc6207612014-04-09 06:08:46 +00001058 TheFunction = nullptr;
Chris Lattner3eee99c2008-08-19 04:36:02 +00001059 FunctionProcessed = false;
Chris Lattner604e3512008-08-19 04:47:09 +00001060 ST_DEBUG("end purgeFunction!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +00001061}
1062
1063/// getGlobalSlot - Get the slot number of a global value.
1064int SlotTracker::getGlobalSlot(const GlobalValue *V) {
1065 // Check for uninitialized state and do lazy initialization.
1066 initialize();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001067
Jay Foaddbb221d2011-07-11 07:28:49 +00001068 // Find the value in the module map
Chris Lattner3eee99c2008-08-19 04:36:02 +00001069 ValueMap::iterator MI = mMap.find(V);
Dan Gohman1dd27572008-10-01 19:58:59 +00001070 return MI == mMap.end() ? -1 : (int)MI->second;
Chris Lattner3eee99c2008-08-19 04:36:02 +00001071}
1072
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00001073/// getMetadataSlot - Get the slot number of a MDNode.
1074int SlotTracker::getMetadataSlot(const MDNode *N) {
Devang Patel983c6b12009-07-08 21:44:25 +00001075 // Check for uninitialized state and do lazy initialization.
1076 initialize();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001077
Jay Foaddbb221d2011-07-11 07:28:49 +00001078 // Find the MDNode in the module map
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00001079 mdn_iterator MI = mdnMap.find(N);
Devang Patel983c6b12009-07-08 21:44:25 +00001080 return MI == mdnMap.end() ? -1 : (int)MI->second;
1081}
1082
Chris Lattner3eee99c2008-08-19 04:36:02 +00001083/// getLocalSlot - Get the slot number for a value that is local to a function.
1084int SlotTracker::getLocalSlot(const Value *V) {
1085 assert(!isa<Constant>(V) && "Can't get a constant or global slot with this!");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001086
Chris Lattner3eee99c2008-08-19 04:36:02 +00001087 // Check for uninitialized state and do lazy initialization.
1088 initialize();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001089
Chris Lattner3eee99c2008-08-19 04:36:02 +00001090 ValueMap::iterator FI = fMap.find(V);
Dan Gohman1dd27572008-10-01 19:58:59 +00001091 return FI == fMap.end() ? -1 : (int)FI->second;
Chris Lattner3eee99c2008-08-19 04:36:02 +00001092}
1093
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001094int SlotTracker::getAttributeGroupSlot(AttributeSet AS) {
Bill Wendling829b4782013-02-11 08:43:33 +00001095 // Check for uninitialized state and do lazy initialization.
1096 initialize();
1097
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001098 // Find the AttributeSet in the module map.
Bill Wendling829b4782013-02-11 08:43:33 +00001099 as_iterator AI = asMap.find(AS);
1100 return AI == asMap.end() ? -1 : (int)AI->second;
1101}
Chris Lattner3eee99c2008-08-19 04:36:02 +00001102
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00001103int SlotTracker::getGUIDSlot(GlobalValue::GUID GUID) {
1104 // Check for uninitialized state and do lazy initialization.
1105 initializeIndex();
1106
1107 // Find the GUID in the map
1108 guid_iterator I = GUIDMap.find(GUID);
1109 return I == GUIDMap.end() ? -1 : (int)I->second;
1110}
1111
Chris Lattner3eee99c2008-08-19 04:36:02 +00001112/// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
1113void SlotTracker::CreateModuleSlot(const GlobalValue *V) {
1114 assert(V && "Can't insert a null Value into SlotTracker!");
Chris Lattner031560c2009-12-29 07:25:48 +00001115 assert(!V->getType()->isVoidTy() && "Doesn't need a slot!");
Chris Lattner3eee99c2008-08-19 04:36:02 +00001116 assert(!V->hasName() && "Doesn't need a slot!");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001117
Chris Lattner3eee99c2008-08-19 04:36:02 +00001118 unsigned DestSlot = mNext++;
1119 mMap[V] = DestSlot;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001120
Chris Lattner604e3512008-08-19 04:47:09 +00001121 ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" <<
Chris Lattner3eee99c2008-08-19 04:36:02 +00001122 DestSlot << " [");
Dmitry Polukhina1feff72016-04-07 12:32:19 +00001123 // G = Global, F = Function, A = Alias, I = IFunc, o = other
Chris Lattner604e3512008-08-19 04:47:09 +00001124 ST_DEBUG((isa<GlobalVariable>(V) ? 'G' :
Chris Lattner3eee99c2008-08-19 04:36:02 +00001125 (isa<Function>(V) ? 'F' :
Dmitry Polukhina1feff72016-04-07 12:32:19 +00001126 (isa<GlobalAlias>(V) ? 'A' :
1127 (isa<GlobalIFunc>(V) ? 'I' : 'o')))) << "]\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +00001128}
1129
Chris Lattner3eee99c2008-08-19 04:36:02 +00001130/// CreateSlot - Create a new slot for the specified value if it has no name.
1131void SlotTracker::CreateFunctionSlot(const Value *V) {
Chris Lattner031560c2009-12-29 07:25:48 +00001132 assert(!V->getType()->isVoidTy() && !V->hasName() && "Doesn't need a slot!");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001133
Chris Lattner3eee99c2008-08-19 04:36:02 +00001134 unsigned DestSlot = fNext++;
1135 fMap[V] = DestSlot;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001136
Chris Lattner3eee99c2008-08-19 04:36:02 +00001137 // G = Global, F = Function, o = other
Chris Lattner604e3512008-08-19 04:47:09 +00001138 ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" <<
Chris Lattner3eee99c2008-08-19 04:36:02 +00001139 DestSlot << " [o]\n");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001140}
Chris Lattner3eee99c2008-08-19 04:36:02 +00001141
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00001142/// CreateModuleSlot - Insert the specified MDNode* into the slot table.
1143void SlotTracker::CreateMetadataSlot(const MDNode *N) {
1144 assert(N && "Can't insert a null Value into SlotTracker!");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001145
Reid Kleckner6d353342017-08-23 20:31:27 +00001146 // Don't make slots for DIExpressions. We just print them inline everywhere.
1147 if (isa<DIExpression>(N))
1148 return;
1149
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001150 unsigned DestSlot = mdnNext;
1151 if (!mdnMap.insert(std::make_pair(N, DestSlot)).second)
1152 return;
1153 ++mdnNext;
Devang Patel983c6b12009-07-08 21:44:25 +00001154
Chris Lattner0d50bdd2009-12-31 02:27:30 +00001155 // Recursively add any MDNodes referenced by operands.
1156 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1157 if (const MDNode *Op = dyn_cast_or_null<MDNode>(N->getOperand(i)))
1158 CreateMetadataSlot(Op);
Devang Patel983c6b12009-07-08 21:44:25 +00001159}
Chris Lattner3eee99c2008-08-19 04:36:02 +00001160
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001161void SlotTracker::CreateAttributeSetSlot(AttributeSet AS) {
1162 assert(AS.hasAttributes() && "Doesn't need a slot!");
Bill Wendling829b4782013-02-11 08:43:33 +00001163
1164 as_iterator I = asMap.find(AS);
1165 if (I != asMap.end())
1166 return;
1167
1168 unsigned DestSlot = asNext++;
1169 asMap[AS] = DestSlot;
1170}
1171
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00001172/// Create a new slot for the specified GUID
1173void SlotTracker::CreateGUIDSlot(GlobalValue::GUID GUID) {
1174 GUIDMap[GUID] = GUIDNext++;
1175}
1176
Chris Lattner3eee99c2008-08-19 04:36:02 +00001177//===----------------------------------------------------------------------===//
1178// AsmWriter Implementation
1179//===----------------------------------------------------------------------===//
Chris Lattner7f8845a2002-07-23 18:07:49 +00001180
Dan Gohman12dad632009-08-12 20:56:03 +00001181static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
Dan Gohman8061d9e2009-08-13 15:27:57 +00001182 TypePrinting *TypePrinter,
Dan Gohmana2489d12010-07-20 23:55:01 +00001183 SlotTracker *Machine,
1184 const Module *Context);
Reid Spencer58d30f22004-07-04 11:50:43 +00001185
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001186static void WriteAsOperandInternal(raw_ostream &Out, const Metadata *MD,
1187 TypePrinting *TypePrinter,
1188 SlotTracker *Machine, const Module *Context,
1189 bool FromValue = false);
1190
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001191static void writeAtomicRMWOperation(raw_ostream &Out,
1192 AtomicRMWInst::BinOp Op) {
1193 switch (Op) {
1194 default: Out << " <unknown operation " << Op << ">"; break;
1195 case AtomicRMWInst::Xchg: Out << " xchg"; break;
1196 case AtomicRMWInst::Add: Out << " add"; break;
1197 case AtomicRMWInst::Sub: Out << " sub"; break;
1198 case AtomicRMWInst::And: Out << " and"; break;
1199 case AtomicRMWInst::Nand: Out << " nand"; break;
1200 case AtomicRMWInst::Or: Out << " or"; break;
1201 case AtomicRMWInst::Xor: Out << " xor"; break;
1202 case AtomicRMWInst::Max: Out << " max"; break;
1203 case AtomicRMWInst::Min: Out << " min"; break;
1204 case AtomicRMWInst::UMax: Out << " umax"; break;
1205 case AtomicRMWInst::UMin: Out << " umin"; break;
1206 }
1207}
Devang Patel983c6b12009-07-08 21:44:25 +00001208
Dan Gohman12dad632009-08-12 20:56:03 +00001209static void WriteOptimizationInfo(raw_ostream &Out, const User *U) {
Michael Ilseman92053172012-11-27 00:42:44 +00001210 if (const FPMathOperator *FPO = dyn_cast<const FPMathOperator>(U)) {
Sanjay Patel629c4112017-11-06 16:27:15 +00001211 // 'Fast' is an abbreviation for all fast-math-flags.
1212 if (FPO->isFast())
Michael Ilseman92053172012-11-27 00:42:44 +00001213 Out << " fast";
1214 else {
Sanjay Patel629c4112017-11-06 16:27:15 +00001215 if (FPO->hasAllowReassoc())
1216 Out << " reassoc";
Michael Ilseman92053172012-11-27 00:42:44 +00001217 if (FPO->hasNoNaNs())
1218 Out << " nnan";
1219 if (FPO->hasNoInfs())
1220 Out << " ninf";
1221 if (FPO->hasNoSignedZeros())
1222 Out << " nsz";
1223 if (FPO->hasAllowReciprocal())
1224 Out << " arcp";
Adam Nemetcd847a82017-03-28 20:11:52 +00001225 if (FPO->hasAllowContract())
1226 Out << " contract";
Sanjay Patel629c4112017-11-06 16:27:15 +00001227 if (FPO->hasApproxFunc())
1228 Out << " afn";
Michael Ilseman92053172012-11-27 00:42:44 +00001229 }
1230 }
1231
Dan Gohman0ebd6962009-07-20 21:19:07 +00001232 if (const OverflowingBinaryOperator *OBO =
1233 dyn_cast<OverflowingBinaryOperator>(U)) {
Dan Gohman16f54152009-08-20 17:11:38 +00001234 if (OBO->hasNoUnsignedWrap())
Dan Gohman9c7f8082009-07-27 16:11:46 +00001235 Out << " nuw";
Dan Gohman16f54152009-08-20 17:11:38 +00001236 if (OBO->hasNoSignedWrap())
Dan Gohman9c7f8082009-07-27 16:11:46 +00001237 Out << " nsw";
Chris Lattner35315d02011-02-06 21:44:57 +00001238 } else if (const PossiblyExactOperator *Div =
1239 dyn_cast<PossiblyExactOperator>(U)) {
Dan Gohman0ebd6962009-07-20 21:19:07 +00001240 if (Div->isExact())
Dan Gohman9c7f8082009-07-27 16:11:46 +00001241 Out << " exact";
Dan Gohman1639c392009-07-27 21:53:46 +00001242 } else if (const GEPOperator *GEP = dyn_cast<GEPOperator>(U)) {
1243 if (GEP->isInBounds())
1244 Out << " inbounds";
Dan Gohman0ebd6962009-07-20 21:19:07 +00001245 }
1246}
1247
Dan Gohmanefb8dbb2010-07-14 20:57:55 +00001248static void WriteConstantInternal(raw_ostream &Out, const Constant *CV,
1249 TypePrinting &TypePrinter,
Dan Gohmana2489d12010-07-20 23:55:01 +00001250 SlotTracker *Machine,
1251 const Module *Context) {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001252 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00001253 if (CI->getType()->isIntegerTy(1)) {
Reid Spencercddc9df2007-01-12 04:24:46 +00001254 Out << (CI->getZExtValue() ? "true" : "false");
Chris Lattner17f71652008-08-17 07:19:36 +00001255 return;
1256 }
1257 Out << CI->getValue();
1258 return;
1259 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001260
Chris Lattner17f71652008-08-17 07:19:36 +00001261 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Serguei Katkov3a46eb42017-04-21 02:52:17 +00001262 const APFloat &APF = CFP->getValueAPF();
1263 if (&APF.getSemantics() == &APFloat::IEEEsingle() ||
1264 &APF.getSemantics() == &APFloat::IEEEdouble()) {
Dale Johannesen028084e2007-09-12 03:30:33 +00001265 // We would like to output the FP constant value in exponential notation,
1266 // but we cannot do this if doing so will lose precision. Check here to
1267 // make sure that we only output it in exponential format if we can parse
1268 // the value back and get the same value.
1269 //
Dale Johannesen1f864982009-01-21 20:32:55 +00001270 bool ignored;
Serguei Katkov3a46eb42017-04-21 02:52:17 +00001271 bool isDouble = &APF.getSemantics() == &APFloat::IEEEdouble();
1272 bool isInf = APF.isInfinity();
1273 bool isNaN = APF.isNaN();
Justin Bognerb609b6b2015-12-04 01:14:24 +00001274 if (!isInf && !isNaN) {
Serguei Katkov3a46eb42017-04-21 02:52:17 +00001275 double Val = isDouble ? APF.convertToDouble() : APF.convertToFloat();
Dan Gohman518cda42011-12-17 00:04:22 +00001276 SmallString<128> StrVal;
Serguei Katkov3a46eb42017-04-21 02:52:17 +00001277 APF.toString(StrVal, 6, 0, false);
Dan Gohman518cda42011-12-17 00:04:22 +00001278 // Check to make sure that the stringized number is not some string like
1279 // "Inf" or NaN, that atof will accept, but the lexer will not. Check
1280 // that the string matches the "[-+]?[0-9]" regex.
1281 //
Serguei Katkovc9a752c2017-04-21 06:14:38 +00001282 assert(((StrVal[0] >= '0' && StrVal[0] <= '9') ||
1283 ((StrVal[0] == '-' || StrVal[0] == '+') &&
1284 (StrVal[1] >= '0' && StrVal[1] <= '9'))) &&
1285 "[-+]?[0-9] regex does not match!");
Serguei Katkov3a46eb42017-04-21 02:52:17 +00001286 // Reparse stringized version!
1287 if (APFloat(APFloat::IEEEdouble(), StrVal).convertToDouble() == Val) {
1288 Out << StrVal;
1289 return;
Dale Johannesen028084e2007-09-12 03:30:33 +00001290 }
Chris Lattner1e194682002-04-18 18:53:13 +00001291 }
Dale Johannesen028084e2007-09-12 03:30:33 +00001292 // Otherwise we could not reparse it to exactly the same value, so we must
Dale Johannesen1f864982009-01-21 20:32:55 +00001293 // output the string in hexadecimal format! Note that loading and storing
1294 // floating point types changes the bits of NaNs on some hosts, notably
1295 // x86, so we must not use these types.
Benjamin Kramer86c77412014-03-15 18:47:07 +00001296 static_assert(sizeof(double) == sizeof(uint64_t),
1297 "assuming that double is 64 bits!");
Serguei Katkov3a46eb42017-04-21 02:52:17 +00001298 APFloat apf = APF;
Justin Bognerb609b6b2015-12-04 01:14:24 +00001299 // Floats are represented in ASCII IR as double, convert.
Dale Johannesen1f864982009-01-21 20:32:55 +00001300 if (!isDouble)
Stephan Bergmann17c7f702016-12-14 11:57:17 +00001301 apf.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven,
Dale Johannesen1f864982009-01-21 20:32:55 +00001302 &ignored);
Justin Bogner93289572015-12-04 02:14:34 +00001303 Out << format_hex(apf.bitcastToAPInt().getZExtValue(), 0, /*Upper=*/true);
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001304 return;
1305 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001306
Tobias Grosser6b31d172012-05-24 15:59:06 +00001307 // Either half, or some form of long double.
1308 // These appear as a magic letter identifying the type, then a
1309 // fixed number of hex digits.
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001310 Out << "0x";
Serguei Katkov3a46eb42017-04-21 02:52:17 +00001311 APInt API = APF.bitcastToAPInt();
1312 if (&APF.getSemantics() == &APFloat::x87DoubleExtended()) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001313 Out << 'K';
Justin Bogner93289572015-12-04 02:14:34 +00001314 Out << format_hex_no_prefix(API.getHiBits(16).getZExtValue(), 4,
1315 /*Upper=*/true);
1316 Out << format_hex_no_prefix(API.getLoBits(64).getZExtValue(), 16,
1317 /*Upper=*/true);
Dale Johannesen93eefa02009-03-23 21:16:53 +00001318 return;
Serguei Katkov3a46eb42017-04-21 02:52:17 +00001319 } else if (&APF.getSemantics() == &APFloat::IEEEquad()) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001320 Out << 'L';
Justin Bogner93289572015-12-04 02:14:34 +00001321 Out << format_hex_no_prefix(API.getLoBits(64).getZExtValue(), 16,
1322 /*Upper=*/true);
1323 Out << format_hex_no_prefix(API.getHiBits(64).getZExtValue(), 16,
1324 /*Upper=*/true);
Serguei Katkov3a46eb42017-04-21 02:52:17 +00001325 } else if (&APF.getSemantics() == &APFloat::PPCDoubleDouble()) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001326 Out << 'M';
Justin Bogner93289572015-12-04 02:14:34 +00001327 Out << format_hex_no_prefix(API.getLoBits(64).getZExtValue(), 16,
1328 /*Upper=*/true);
1329 Out << format_hex_no_prefix(API.getHiBits(64).getZExtValue(), 16,
1330 /*Upper=*/true);
Serguei Katkov3a46eb42017-04-21 02:52:17 +00001331 } else if (&APF.getSemantics() == &APFloat::IEEEhalf()) {
Tobias Grosser6b31d172012-05-24 15:59:06 +00001332 Out << 'H';
Justin Bogner93289572015-12-04 02:14:34 +00001333 Out << format_hex_no_prefix(API.getZExtValue(), 4,
1334 /*Upper=*/true);
Tobias Grosser6b31d172012-05-24 15:59:06 +00001335 } else
Torok Edwinfbcc6632009-07-14 16:55:14 +00001336 llvm_unreachable("Unsupported floating point type");
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001337 return;
1338 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001339
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001340 if (isa<ConstantAggregateZero>(CV)) {
Chris Lattner76b2ff42004-02-15 05:55:15 +00001341 Out << "zeroinitializer";
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001342 return;
1343 }
Andrew Trickec4b6e72011-09-30 19:48:58 +00001344
Chris Lattner214cc702009-10-28 03:38:12 +00001345 if (const BlockAddress *BA = dyn_cast<BlockAddress>(CV)) {
1346 Out << "blockaddress(";
Dan Gohmana2489d12010-07-20 23:55:01 +00001347 WriteAsOperandInternal(Out, BA->getFunction(), &TypePrinter, Machine,
1348 Context);
Chris Lattner214cc702009-10-28 03:38:12 +00001349 Out << ", ";
Dan Gohmana2489d12010-07-20 23:55:01 +00001350 WriteAsOperandInternal(Out, BA->getBasicBlock(), &TypePrinter, Machine,
1351 Context);
Chris Lattner214cc702009-10-28 03:38:12 +00001352 Out << ")";
1353 return;
1354 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001355
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001356 if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001357 Type *ETy = CA->getType()->getElementType();
Chris Lattnercf9e8f62012-02-05 02:29:43 +00001358 Out << '[';
1359 TypePrinter.print(ETy, Out);
1360 Out << ' ';
1361 WriteAsOperandInternal(Out, CA->getOperand(0),
1362 &TypePrinter, Machine,
1363 Context);
1364 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
1365 Out << ", ";
Chris Lattner9c181f62012-01-31 03:15:40 +00001366 TypePrinter.print(ETy, Out);
1367 Out << ' ';
Chris Lattnercf9e8f62012-02-05 02:29:43 +00001368 WriteAsOperandInternal(Out, CA->getOperand(i), &TypePrinter, Machine,
Chris Lattner9c181f62012-01-31 03:15:40 +00001369 Context);
Chris Lattnerd84bb632002-04-16 21:36:08 +00001370 }
Chris Lattnercf9e8f62012-02-05 02:29:43 +00001371 Out << ']';
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001372 return;
1373 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001374
Chris Lattnerfa775002012-01-26 02:32:04 +00001375 if (const ConstantDataArray *CA = dyn_cast<ConstantDataArray>(CV)) {
1376 // As a special case, print the array as a string if it is an array of
1377 // i8 with ConstantInt values.
1378 if (CA->isString()) {
1379 Out << "c\"";
1380 PrintEscapedString(CA->getAsString(), Out);
1381 Out << '"';
1382 return;
1383 }
1384
1385 Type *ETy = CA->getType()->getElementType();
1386 Out << '[';
Chris Lattner9c181f62012-01-31 03:15:40 +00001387 TypePrinter.print(ETy, Out);
1388 Out << ' ';
1389 WriteAsOperandInternal(Out, CA->getElementAsConstant(0),
1390 &TypePrinter, Machine,
1391 Context);
1392 for (unsigned i = 1, e = CA->getNumElements(); i != e; ++i) {
1393 Out << ", ";
Chris Lattnerfa775002012-01-26 02:32:04 +00001394 TypePrinter.print(ETy, Out);
1395 Out << ' ';
Chris Lattner9c181f62012-01-31 03:15:40 +00001396 WriteAsOperandInternal(Out, CA->getElementAsConstant(i), &TypePrinter,
1397 Machine, Context);
Chris Lattnerfa775002012-01-26 02:32:04 +00001398 }
Chris Lattner9c181f62012-01-31 03:15:40 +00001399 Out << ']';
Chris Lattnerfa775002012-01-26 02:32:04 +00001400 return;
1401 }
1402
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001403 if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
Andrew Lenharth0d124b82007-01-08 18:21:30 +00001404 if (CS->getType()->isPacked())
1405 Out << '<';
Misha Brukman21bbdb92004-06-04 21:11:51 +00001406 Out << '{';
Jim Laskey3bb78742006-02-25 12:27:03 +00001407 unsigned N = CS->getNumOperands();
1408 if (N) {
Chris Lattner604e3512008-08-19 04:47:09 +00001409 Out << ' ';
Chris Lattnere101c442009-02-28 21:26:53 +00001410 TypePrinter.print(CS->getOperand(0)->getType(), Out);
Dan Gohman81313fd2008-09-14 17:21:12 +00001411 Out << ' ';
Chris Lattnerd84bb632002-04-16 21:36:08 +00001412
Dan Gohmana2489d12010-07-20 23:55:01 +00001413 WriteAsOperandInternal(Out, CS->getOperand(0), &TypePrinter, Machine,
1414 Context);
Chris Lattnerd84bb632002-04-16 21:36:08 +00001415
Jim Laskey3bb78742006-02-25 12:27:03 +00001416 for (unsigned i = 1; i < N; i++) {
Chris Lattnerd84bb632002-04-16 21:36:08 +00001417 Out << ", ";
Chris Lattnere101c442009-02-28 21:26:53 +00001418 TypePrinter.print(CS->getOperand(i)->getType(), Out);
Dan Gohman81313fd2008-09-14 17:21:12 +00001419 Out << ' ';
Chris Lattnerd84bb632002-04-16 21:36:08 +00001420
Dan Gohmana2489d12010-07-20 23:55:01 +00001421 WriteAsOperandInternal(Out, CS->getOperand(i), &TypePrinter, Machine,
1422 Context);
Chris Lattnerd84bb632002-04-16 21:36:08 +00001423 }
Dan Gohman81313fd2008-09-14 17:21:12 +00001424 Out << ' ';
Chris Lattnerd84bb632002-04-16 21:36:08 +00001425 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001426
Dan Gohman81313fd2008-09-14 17:21:12 +00001427 Out << '}';
Andrew Lenharth0d124b82007-01-08 18:21:30 +00001428 if (CS->getType()->isPacked())
1429 Out << '>';
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001430 return;
1431 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001432
Chris Lattnerfa775002012-01-26 02:32:04 +00001433 if (isa<ConstantVector>(CV) || isa<ConstantDataVector>(CV)) {
1434 Type *ETy = CV->getType()->getVectorElementType();
Dan Gohman1262a252009-02-11 00:25:25 +00001435 Out << '<';
Chris Lattnere101c442009-02-28 21:26:53 +00001436 TypePrinter.print(ETy, Out);
Dan Gohman81313fd2008-09-14 17:21:12 +00001437 Out << ' ';
Chris Lattnerfa775002012-01-26 02:32:04 +00001438 WriteAsOperandInternal(Out, CV->getAggregateElement(0U), &TypePrinter,
1439 Machine, Context);
1440 for (unsigned i = 1, e = CV->getType()->getVectorNumElements(); i != e;++i){
Chris Lattner585297e82008-08-19 05:26:17 +00001441 Out << ", ";
Chris Lattnere101c442009-02-28 21:26:53 +00001442 TypePrinter.print(ETy, Out);
Dan Gohman81313fd2008-09-14 17:21:12 +00001443 Out << ' ';
Chris Lattnerfa775002012-01-26 02:32:04 +00001444 WriteAsOperandInternal(Out, CV->getAggregateElement(i), &TypePrinter,
1445 Machine, Context);
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001446 }
Dan Gohman1262a252009-02-11 00:25:25 +00001447 Out << '>';
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001448 return;
1449 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001450
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001451 if (isa<ConstantPointerNull>(CV)) {
Chris Lattnerd84bb632002-04-16 21:36:08 +00001452 Out << "null";
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001453 return;
1454 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001455
David Majnemerf0f224d2015-11-11 21:57:16 +00001456 if (isa<ConstantTokenNone>(CV)) {
1457 Out << "none";
1458 return;
1459 }
1460
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001461 if (isa<UndefValue>(CV)) {
Chris Lattner5e0b9f22004-10-16 18:08:06 +00001462 Out << "undef";
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001463 return;
1464 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001465
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001466 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Reid Spencer812a1be2006-12-04 05:19:18 +00001467 Out << CE->getOpcodeName();
Dan Gohman9c7f8082009-07-27 16:11:46 +00001468 WriteOptimizationInfo(Out, CE);
Reid Spencer812a1be2006-12-04 05:19:18 +00001469 if (CE->isCompare())
Tim Northoverde3aea0412016-08-17 20:25:25 +00001470 Out << ' ' << CmpInst::getPredicateName(
1471 static_cast<CmpInst::Predicate>(CE->getPredicate()));
Reid Spencer812a1be2006-12-04 05:19:18 +00001472 Out << " (";
Misha Brukmanb1c93172005-04-21 23:48:37 +00001473
Peter Collingbourned93620b2016-11-10 22:34:55 +00001474 Optional<unsigned> InRangeOp;
David Blaikief72d05b2015-03-13 18:20:45 +00001475 if (const GEPOperator *GEP = dyn_cast<GEPOperator>(CE)) {
David Blaikied06654f2015-07-29 20:26:23 +00001476 TypePrinter.print(GEP->getSourceElementType(), Out);
David Blaikief72d05b2015-03-13 18:20:45 +00001477 Out << ", ";
Peter Collingbourned93620b2016-11-10 22:34:55 +00001478 InRangeOp = GEP->getInRangeIndex();
1479 if (InRangeOp)
1480 ++*InRangeOp;
David Blaikief72d05b2015-03-13 18:20:45 +00001481 }
1482
Vikram S. Adveb952b542002-07-14 23:14:45 +00001483 for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) {
Zachary Turner804d5022016-11-11 23:58:11 +00001484 if (InRangeOp && unsigned(OI - CE->op_begin()) == *InRangeOp)
Peter Collingbourned93620b2016-11-10 22:34:55 +00001485 Out << "inrange ";
Chris Lattnere101c442009-02-28 21:26:53 +00001486 TypePrinter.print((*OI)->getType(), Out);
Dan Gohman81313fd2008-09-14 17:21:12 +00001487 Out << ' ';
Dan Gohmana2489d12010-07-20 23:55:01 +00001488 WriteAsOperandInternal(Out, *OI, &TypePrinter, Machine, Context);
Vikram S. Adveb952b542002-07-14 23:14:45 +00001489 if (OI+1 != CE->op_end())
Chris Lattner3cd8c562002-07-30 18:54:25 +00001490 Out << ", ";
Vikram S. Adveb952b542002-07-14 23:14:45 +00001491 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001492
Dan Gohmana76f0f72008-05-31 19:12:39 +00001493 if (CE->hasIndices()) {
Jay Foad0091fe82011-04-13 15:22:40 +00001494 ArrayRef<unsigned> Indices = CE->getIndices();
Dan Gohmana76f0f72008-05-31 19:12:39 +00001495 for (unsigned i = 0, e = Indices.size(); i != e; ++i)
1496 Out << ", " << Indices[i];
1497 }
1498
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001499 if (CE->isCast()) {
Chris Lattner83b396b2002-08-15 19:37:43 +00001500 Out << " to ";
Chris Lattnere101c442009-02-28 21:26:53 +00001501 TypePrinter.print(CE->getType(), Out);
Chris Lattner83b396b2002-08-15 19:37:43 +00001502 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001503
Misha Brukman21bbdb92004-06-04 21:11:51 +00001504 Out << ')';
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001505 return;
Chris Lattnerd84bb632002-04-16 21:36:08 +00001506 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001507
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001508 Out << "<placeholder or erroneous Constant>";
Chris Lattnerd84bb632002-04-16 21:36:08 +00001509}
1510
Duncan P. N. Exon Smitha6de6a42015-01-12 23:45:31 +00001511static void writeMDTuple(raw_ostream &Out, const MDTuple *Node,
1512 TypePrinting *TypePrinter, SlotTracker *Machine,
1513 const Module *Context) {
Chris Lattnercdec5812009-12-31 02:31:59 +00001514 Out << "!{";
1515 for (unsigned mi = 0, me = Node->getNumOperands(); mi != me; ++mi) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001516 const Metadata *MD = Node->getOperand(mi);
1517 if (!MD)
Chris Lattnercdec5812009-12-31 02:31:59 +00001518 Out << "null";
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001519 else if (auto *MDV = dyn_cast<ValueAsMetadata>(MD)) {
1520 Value *V = MDV->getValue();
Chris Lattnercdec5812009-12-31 02:31:59 +00001521 TypePrinter->print(V->getType(), Out);
1522 Out << ' ';
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001523 WriteAsOperandInternal(Out, V, TypePrinter, Machine, Context);
1524 } else {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001525 WriteAsOperandInternal(Out, MD, TypePrinter, Machine, Context);
Chris Lattnercdec5812009-12-31 02:31:59 +00001526 }
1527 if (mi + 1 != me)
1528 Out << ", ";
1529 }
Andrew Trickec4b6e72011-09-30 19:48:58 +00001530
Chris Lattnercdec5812009-12-31 02:31:59 +00001531 Out << "}";
1532}
1533
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +00001534namespace {
Eugene Zelenkode6cce22017-06-19 22:05:08 +00001535
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +00001536struct FieldSeparator {
Eugene Zelenkode6cce22017-06-19 22:05:08 +00001537 bool Skip = true;
Duncan P. N. Exon Smith70ab3d22015-02-21 01:02:18 +00001538 const char *Sep;
Eugene Zelenkode6cce22017-06-19 22:05:08 +00001539
1540 FieldSeparator(const char *Sep = ", ") : Sep(Sep) {}
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +00001541};
Eugene Zelenkode6cce22017-06-19 22:05:08 +00001542
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +00001543raw_ostream &operator<<(raw_ostream &OS, FieldSeparator &FS) {
1544 if (FS.Skip) {
1545 FS.Skip = false;
1546 return OS;
1547 }
Duncan P. N. Exon Smith70ab3d22015-02-21 01:02:18 +00001548 return OS << FS.Sep;
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +00001549}
Eugene Zelenkode6cce22017-06-19 22:05:08 +00001550
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001551struct MDFieldPrinter {
1552 raw_ostream &Out;
1553 FieldSeparator FS;
Eugene Zelenkode6cce22017-06-19 22:05:08 +00001554 TypePrinting *TypePrinter = nullptr;
1555 SlotTracker *Machine = nullptr;
1556 const Module *Context = nullptr;
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001557
Eugene Zelenkode6cce22017-06-19 22:05:08 +00001558 explicit MDFieldPrinter(raw_ostream &Out) : Out(Out) {}
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001559 MDFieldPrinter(raw_ostream &Out, TypePrinting *TypePrinter,
1560 SlotTracker *Machine, const Module *Context)
1561 : Out(Out), TypePrinter(TypePrinter), Machine(Machine), Context(Context) {
1562 }
Eugene Zelenkode6cce22017-06-19 22:05:08 +00001563
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001564 void printTag(const DINode *N);
Amjad Abouda9bcf162015-12-10 12:56:35 +00001565 void printMacinfoType(const DIMacroNode *N);
Scott Linder71603842018-02-12 19:45:54 +00001566 void printChecksum(const DIFile::ChecksumInfo<StringRef> &N);
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001567 void printString(StringRef Name, StringRef Value,
1568 bool ShouldSkipEmpty = true);
1569 void printMetadata(StringRef Name, const Metadata *MD,
1570 bool ShouldSkipNull = true);
1571 template <class IntTy>
1572 void printInt(StringRef Name, IntTy Int, bool ShouldSkipZero = true);
David Blaikiea01f2952016-08-24 18:29:49 +00001573 void printBool(StringRef Name, bool Value, Optional<bool> Default = None);
Leny Kholodov5fcc4182016-09-06 10:46:28 +00001574 void printDIFlags(StringRef Name, DINode::DIFlags Flags);
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001575 template <class IntTy, class Stringifier>
1576 void printDwarfEnum(StringRef Name, IntTy Value, Stringifier toString,
1577 bool ShouldSkipZero = true);
Adrian Prantlb939a252016-03-31 23:56:58 +00001578 void printEmissionKind(StringRef Name, DICompileUnit::DebugEmissionKind EK);
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001579};
Eugene Zelenkode6cce22017-06-19 22:05:08 +00001580
1581} // end anonymous namespace
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +00001582
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001583void MDFieldPrinter::printTag(const DINode *N) {
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001584 Out << FS << "tag: ";
Mehdi Amini149f6ea2016-10-05 05:59:29 +00001585 auto Tag = dwarf::TagString(N->getTag());
1586 if (!Tag.empty())
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001587 Out << Tag;
1588 else
1589 Out << N->getTag();
1590}
1591
Amjad Abouda9bcf162015-12-10 12:56:35 +00001592void MDFieldPrinter::printMacinfoType(const DIMacroNode *N) {
1593 Out << FS << "type: ";
Mehdi Amini149f6ea2016-10-05 05:59:29 +00001594 auto Type = dwarf::MacinfoString(N->getMacinfoType());
1595 if (!Type.empty())
Amjad Abouda9bcf162015-12-10 12:56:35 +00001596 Out << Type;
1597 else
1598 Out << N->getMacinfoType();
1599}
1600
Scott Linder71603842018-02-12 19:45:54 +00001601void MDFieldPrinter::printChecksum(
1602 const DIFile::ChecksumInfo<StringRef> &Checksum) {
1603 Out << FS << "checksumkind: " << Checksum.getKindAsString();
1604 printString("checksum", Checksum.Value, /* ShouldSkipEmpty */ false);
Amjad Aboud7faeecc2016-12-25 10:12:09 +00001605}
1606
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001607void MDFieldPrinter::printString(StringRef Name, StringRef Value,
1608 bool ShouldSkipEmpty) {
1609 if (ShouldSkipEmpty && Value.empty())
1610 return;
1611
1612 Out << FS << Name << ": \"";
1613 PrintEscapedString(Value, Out);
1614 Out << "\"";
1615}
1616
Duncan P. N. Exon Smith61a09332015-02-06 22:27:22 +00001617static void writeMetadataAsOperand(raw_ostream &Out, const Metadata *MD,
1618 TypePrinting *TypePrinter,
1619 SlotTracker *Machine,
1620 const Module *Context) {
1621 if (!MD) {
1622 Out << "null";
1623 return;
1624 }
1625 WriteAsOperandInternal(Out, MD, TypePrinter, Machine, Context);
1626}
1627
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001628void MDFieldPrinter::printMetadata(StringRef Name, const Metadata *MD,
1629 bool ShouldSkipNull) {
1630 if (ShouldSkipNull && !MD)
Duncan P. N. Exon Smith79cf9702015-02-28 22:16:56 +00001631 return;
1632
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001633 Out << FS << Name << ": ";
1634 writeMetadataAsOperand(Out, MD, TypePrinter, Machine, Context);
1635}
1636
1637template <class IntTy>
1638void MDFieldPrinter::printInt(StringRef Name, IntTy Int, bool ShouldSkipZero) {
1639 if (ShouldSkipZero && !Int)
1640 return;
1641
1642 Out << FS << Name << ": " << Int;
1643}
1644
David Blaikiea01f2952016-08-24 18:29:49 +00001645void MDFieldPrinter::printBool(StringRef Name, bool Value,
1646 Optional<bool> Default) {
1647 if (Default && Value == *Default)
1648 return;
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001649 Out << FS << Name << ": " << (Value ? "true" : "false");
1650}
1651
Leny Kholodov5fcc4182016-09-06 10:46:28 +00001652void MDFieldPrinter::printDIFlags(StringRef Name, DINode::DIFlags Flags) {
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001653 if (!Flags)
1654 return;
1655
1656 Out << FS << Name << ": ";
1657
Leny Kholodov5fcc4182016-09-06 10:46:28 +00001658 SmallVector<DINode::DIFlags, 8> SplitFlags;
1659 auto Extra = DINode::splitFlags(Flags, SplitFlags);
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001660
1661 FieldSeparator FlagsFS(" | ");
Leny Kholodov5fcc4182016-09-06 10:46:28 +00001662 for (auto F : SplitFlags) {
Mehdi Aminif42ec792016-10-01 05:57:50 +00001663 auto StringF = DINode::getFlagString(F);
1664 assert(!StringF.empty() && "Expected valid flag");
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001665 Out << FlagsFS << StringF;
1666 }
1667 if (Extra || SplitFlags.empty())
1668 Out << FlagsFS << Extra;
1669}
1670
Adrian Prantlb939a252016-03-31 23:56:58 +00001671void MDFieldPrinter::printEmissionKind(StringRef Name,
1672 DICompileUnit::DebugEmissionKind EK) {
1673 Out << FS << Name << ": " << DICompileUnit::EmissionKindString(EK);
1674}
1675
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001676template <class IntTy, class Stringifier>
1677void MDFieldPrinter::printDwarfEnum(StringRef Name, IntTy Value,
1678 Stringifier toString, bool ShouldSkipZero) {
1679 if (!Value)
1680 return;
1681
1682 Out << FS << Name << ": ";
Mehdi Amini149f6ea2016-10-05 05:59:29 +00001683 auto S = toString(Value);
1684 if (!S.empty())
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001685 Out << S;
1686 else
1687 Out << Value;
Duncan P. N. Exon Smith79cf9702015-02-28 22:16:56 +00001688}
1689
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001690static void writeGenericDINode(raw_ostream &Out, const GenericDINode *N,
1691 TypePrinting *TypePrinter, SlotTracker *Machine,
1692 const Module *Context) {
1693 Out << "!GenericDINode(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001694 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1695 Printer.printTag(N);
1696 Printer.printString("header", N->getHeader());
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00001697 if (N->getNumDwarfOperands()) {
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001698 Out << Printer.FS << "operands: {";
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00001699 FieldSeparator IFS;
1700 for (auto &I : N->dwarf_operands()) {
1701 Out << IFS;
Duncan P. N. Exon Smith61a09332015-02-06 22:27:22 +00001702 writeMetadataAsOperand(Out, I, TypePrinter, Machine, Context);
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00001703 }
1704 Out << "}";
1705 }
1706 Out << ")";
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +00001707}
1708
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001709static void writeDILocation(raw_ostream &Out, const DILocation *DL,
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +00001710 TypePrinting *TypePrinter, SlotTracker *Machine,
1711 const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001712 Out << "!DILocation(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001713 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
Duncan P. N. Exon Smith503cf3b2015-01-14 22:14:26 +00001714 // Always output the line, since 0 is a relevant and important value for it.
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001715 Printer.printInt("line", DL->getLine(), /* ShouldSkipZero */ false);
1716 Printer.printInt("column", DL->getColumn());
1717 Printer.printMetadata("scope", DL->getRawScope(), /* ShouldSkipNull */ false);
1718 Printer.printMetadata("inlinedAt", DL->getRawInlinedAt());
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +00001719 Out << ")";
1720}
1721
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001722static void writeDISubrange(raw_ostream &Out, const DISubrange *N,
Sander de Smalenfdf40912018-01-24 09:56:07 +00001723 TypePrinting *TypePrinter, SlotTracker *Machine,
1724 const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001725 Out << "!DISubrange(";
Sander de Smalenfdf40912018-01-24 09:56:07 +00001726 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1727 if (auto *CE = N->getCount().dyn_cast<ConstantInt*>())
1728 Printer.printInt("count", CE->getSExtValue(), /* ShouldSkipZero */ false);
1729 else
1730 Printer.printMetadata("count", N->getCount().dyn_cast<DIVariable*>(),
1731 /*ShouldSkipNull */ false);
Duncan P. N. Exon Smith5dcf6212015-04-07 00:39:59 +00001732 Printer.printInt("lowerBound", N->getLowerBound());
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00001733 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001734}
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00001735
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001736static void writeDIEnumerator(raw_ostream &Out, const DIEnumerator *N,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001737 TypePrinting *, SlotTracker *, const Module *) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001738 Out << "!DIEnumerator(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001739 MDFieldPrinter Printer(Out);
1740 Printer.printString("name", N->getName(), /* ShouldSkipEmpty */ false);
Momchil Velikov08dc66e2018-02-12 16:10:09 +00001741 if (N->isUnsigned()) {
1742 auto Value = static_cast<uint64_t>(N->getValue());
1743 Printer.printInt("value", Value, /* ShouldSkipZero */ false);
1744 Printer.printBool("isUnsigned", true);
1745 } else {
1746 Printer.printInt("value", N->getValue(), /* ShouldSkipZero */ false);
1747 }
Duncan P. N. Exon Smith87754762015-02-13 01:14:11 +00001748 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001749}
Duncan P. N. Exon Smith87754762015-02-13 01:14:11 +00001750
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001751static void writeDIBasicType(raw_ostream &Out, const DIBasicType *N,
Duncan P. N. Exon Smith09e03f32015-02-13 01:14:58 +00001752 TypePrinting *, SlotTracker *, const Module *) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001753 Out << "!DIBasicType(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001754 MDFieldPrinter Printer(Out);
Duncan P. N. Exon Smith16d182a2015-02-28 23:21:38 +00001755 if (N->getTag() != dwarf::DW_TAG_base_type)
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001756 Printer.printTag(N);
1757 Printer.printString("name", N->getName());
1758 Printer.printInt("size", N->getSizeInBits());
1759 Printer.printInt("align", N->getAlignInBits());
1760 Printer.printDwarfEnum("encoding", N->getEncoding(),
1761 dwarf::AttributeEncodingString);
Duncan P. N. Exon Smith09e03f32015-02-13 01:14:58 +00001762 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001763}
Duncan P. N. Exon Smith09e03f32015-02-13 01:14:58 +00001764
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001765static void writeDIDerivedType(raw_ostream &Out, const DIDerivedType *N,
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001766 TypePrinting *TypePrinter, SlotTracker *Machine,
1767 const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001768 Out << "!DIDerivedType(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001769 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1770 Printer.printTag(N);
1771 Printer.printString("name", N->getName());
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001772 Printer.printMetadata("scope", N->getRawScope());
1773 Printer.printMetadata("file", N->getRawFile());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001774 Printer.printInt("line", N->getLine());
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001775 Printer.printMetadata("baseType", N->getRawBaseType(),
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001776 /* ShouldSkipNull */ false);
1777 Printer.printInt("size", N->getSizeInBits());
1778 Printer.printInt("align", N->getAlignInBits());
1779 Printer.printInt("offset", N->getOffsetInBits());
1780 Printer.printDIFlags("flags", N->getFlags());
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001781 Printer.printMetadata("extraData", N->getRawExtraData());
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +00001782 if (const auto &DWARFAddressSpace = N->getDWARFAddressSpace())
1783 Printer.printInt("dwarfAddressSpace", *DWARFAddressSpace,
1784 /* ShouldSkipZero */ false);
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001785 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001786}
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001787
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001788static void writeDICompositeType(raw_ostream &Out, const DICompositeType *N,
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001789 TypePrinting *TypePrinter,
1790 SlotTracker *Machine, const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001791 Out << "!DICompositeType(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001792 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1793 Printer.printTag(N);
1794 Printer.printString("name", N->getName());
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001795 Printer.printMetadata("scope", N->getRawScope());
1796 Printer.printMetadata("file", N->getRawFile());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001797 Printer.printInt("line", N->getLine());
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001798 Printer.printMetadata("baseType", N->getRawBaseType());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001799 Printer.printInt("size", N->getSizeInBits());
1800 Printer.printInt("align", N->getAlignInBits());
1801 Printer.printInt("offset", N->getOffsetInBits());
1802 Printer.printDIFlags("flags", N->getFlags());
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001803 Printer.printMetadata("elements", N->getRawElements());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001804 Printer.printDwarfEnum("runtimeLang", N->getRuntimeLang(),
1805 dwarf::LanguageString);
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001806 Printer.printMetadata("vtableHolder", N->getRawVTableHolder());
1807 Printer.printMetadata("templateParams", N->getRawTemplateParams());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001808 Printer.printString("identifier", N->getIdentifier());
Adrian Prantl8c599212018-02-06 23:45:59 +00001809 Printer.printMetadata("discriminator", N->getRawDiscriminator());
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001810 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001811}
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001812
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001813static void writeDISubroutineType(raw_ostream &Out, const DISubroutineType *N,
Duncan P. N. Exon Smith54e2bc62015-02-13 01:22:59 +00001814 TypePrinting *TypePrinter,
1815 SlotTracker *Machine, const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001816 Out << "!DISubroutineType(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001817 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1818 Printer.printDIFlags("flags", N->getFlags());
Reid Klecknerde3d8b52016-06-08 20:34:29 +00001819 Printer.printDwarfEnum("cc", N->getCC(), dwarf::ConventionString);
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001820 Printer.printMetadata("types", N->getRawTypeArray(),
1821 /* ShouldSkipNull */ false);
Duncan P. N. Exon Smith54e2bc62015-02-13 01:22:59 +00001822 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001823}
Duncan P. N. Exon Smithf14b9c72015-02-13 01:19:14 +00001824
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001825static void writeDIFile(raw_ostream &Out, const DIFile *N, TypePrinting *,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001826 SlotTracker *, const Module *) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001827 Out << "!DIFile(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001828 MDFieldPrinter Printer(Out);
1829 Printer.printString("filename", N->getFilename(),
1830 /* ShouldSkipEmpty */ false);
1831 Printer.printString("directory", N->getDirectory(),
1832 /* ShouldSkipEmpty */ false);
Scott Linder71603842018-02-12 19:45:54 +00001833 // Print all values for checksum together, or not at all.
1834 if (N->getChecksum())
1835 Printer.printChecksum(*N->getChecksum());
Scott Linder16c7bda2018-02-23 23:01:06 +00001836 Printer.printString("source", N->getSource().getValueOr(StringRef()),
1837 /* ShouldSkipEmpty */ true);
Duncan P. N. Exon Smithf14b9c72015-02-13 01:19:14 +00001838 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001839}
Duncan P. N. Exon Smithf14b9c72015-02-13 01:19:14 +00001840
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001841static void writeDICompileUnit(raw_ostream &Out, const DICompileUnit *N,
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00001842 TypePrinting *TypePrinter, SlotTracker *Machine,
1843 const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001844 Out << "!DICompileUnit(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001845 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1846 Printer.printDwarfEnum("language", N->getSourceLanguage(),
1847 dwarf::LanguageString, /* ShouldSkipZero */ false);
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001848 Printer.printMetadata("file", N->getRawFile(), /* ShouldSkipNull */ false);
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001849 Printer.printString("producer", N->getProducer());
1850 Printer.printBool("isOptimized", N->isOptimized());
1851 Printer.printString("flags", N->getFlags());
1852 Printer.printInt("runtimeVersion", N->getRuntimeVersion(),
1853 /* ShouldSkipZero */ false);
1854 Printer.printString("splitDebugFilename", N->getSplitDebugFilename());
Adrian Prantlb939a252016-03-31 23:56:58 +00001855 Printer.printEmissionKind("emissionKind", N->getEmissionKind());
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001856 Printer.printMetadata("enums", N->getRawEnumTypes());
1857 Printer.printMetadata("retainedTypes", N->getRawRetainedTypes());
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001858 Printer.printMetadata("globals", N->getRawGlobalVariables());
1859 Printer.printMetadata("imports", N->getRawImportedEntities());
Amjad Abouda9bcf162015-12-10 12:56:35 +00001860 Printer.printMetadata("macros", N->getRawMacros());
Adrian Prantl1f599f92015-05-21 20:37:30 +00001861 Printer.printInt("dwoId", N->getDWOId());
David Blaikiea01f2952016-08-24 18:29:49 +00001862 Printer.printBool("splitDebugInlining", N->getSplitDebugInlining(), true);
Dehao Chen0944a8c2017-02-01 22:45:09 +00001863 Printer.printBool("debugInfoForProfiling", N->getDebugInfoForProfiling(),
1864 false);
Peter Collingbourneb52e2362017-09-12 21:50:41 +00001865 Printer.printBool("gnuPubnames", N->getGnuPubnames(), false);
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00001866 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001867}
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00001868
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001869static void writeDISubprogram(raw_ostream &Out, const DISubprogram *N,
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00001870 TypePrinting *TypePrinter, SlotTracker *Machine,
1871 const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001872 Out << "!DISubprogram(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001873 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1874 Printer.printString("name", N->getName());
1875 Printer.printString("linkageName", N->getLinkageName());
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001876 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
1877 Printer.printMetadata("file", N->getRawFile());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001878 Printer.printInt("line", N->getLine());
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001879 Printer.printMetadata("type", N->getRawType());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001880 Printer.printBool("isLocal", N->isLocalToUnit());
1881 Printer.printBool("isDefinition", N->isDefinition());
1882 Printer.printInt("scopeLine", N->getScopeLine());
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001883 Printer.printMetadata("containingType", N->getRawContainingType());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001884 Printer.printDwarfEnum("virtuality", N->getVirtuality(),
1885 dwarf::VirtualityString);
Peter Collingbournea1f86252016-03-17 23:58:03 +00001886 if (N->getVirtuality() != dwarf::DW_VIRTUALITY_none ||
1887 N->getVirtualIndex() != 0)
1888 Printer.printInt("virtualIndex", N->getVirtualIndex(), false);
Reid Klecknerb5af11d2016-07-01 02:41:21 +00001889 Printer.printInt("thisAdjustment", N->getThisAdjustment());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001890 Printer.printDIFlags("flags", N->getFlags());
1891 Printer.printBool("isOptimized", N->isOptimized());
Adrian Prantl75819ae2016-04-15 15:57:41 +00001892 Printer.printMetadata("unit", N->getRawUnit());
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001893 Printer.printMetadata("templateParams", N->getRawTemplateParams());
1894 Printer.printMetadata("declaration", N->getRawDeclaration());
Shiva Chen2c864552018-05-09 02:40:45 +00001895 Printer.printMetadata("retainedNodes", N->getRawRetainedNodes());
Adrian Prantl1d12b882017-04-26 22:56:44 +00001896 Printer.printMetadata("thrownTypes", N->getRawThrownTypes());
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00001897 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001898}
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00001899
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001900static void writeDILexicalBlock(raw_ostream &Out, const DILexicalBlock *N,
1901 TypePrinting *TypePrinter, SlotTracker *Machine,
1902 const Module *Context) {
1903 Out << "!DILexicalBlock(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001904 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
Duncan P. N. Exon Smith0e202b92015-03-30 16:37:48 +00001905 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
1906 Printer.printMetadata("file", N->getRawFile());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001907 Printer.printInt("line", N->getLine());
1908 Printer.printInt("column", N->getColumn());
Duncan P. N. Exon Smitha96d4092015-02-13 01:29:28 +00001909 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001910}
Duncan P. N. Exon Smitha96d4092015-02-13 01:29:28 +00001911
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001912static void writeDILexicalBlockFile(raw_ostream &Out,
1913 const DILexicalBlockFile *N,
Duncan P. N. Exon Smith06a07022015-02-13 01:30:42 +00001914 TypePrinting *TypePrinter,
1915 SlotTracker *Machine,
1916 const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001917 Out << "!DILexicalBlockFile(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001918 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
Duncan P. N. Exon Smith0e202b92015-03-30 16:37:48 +00001919 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
1920 Printer.printMetadata("file", N->getRawFile());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001921 Printer.printInt("discriminator", N->getDiscriminator(),
1922 /* ShouldSkipZero */ false);
Duncan P. N. Exon Smith06a07022015-02-13 01:30:42 +00001923 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001924}
Duncan P. N. Exon Smith06a07022015-02-13 01:30:42 +00001925
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001926static void writeDINamespace(raw_ostream &Out, const DINamespace *N,
Duncan P. N. Exon Smithe1460002015-02-13 01:32:09 +00001927 TypePrinting *TypePrinter, SlotTracker *Machine,
1928 const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001929 Out << "!DINamespace(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001930 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1931 Printer.printString("name", N->getName());
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00001932 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
Adrian Prantldbfda632016-11-03 19:42:02 +00001933 Printer.printBool("exportSymbols", N->getExportSymbols(), false);
Duncan P. N. Exon Smithe1460002015-02-13 01:32:09 +00001934 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001935}
Duncan P. N. Exon Smithe1460002015-02-13 01:32:09 +00001936
Amjad Abouda9bcf162015-12-10 12:56:35 +00001937static void writeDIMacro(raw_ostream &Out, const DIMacro *N,
1938 TypePrinting *TypePrinter, SlotTracker *Machine,
1939 const Module *Context) {
1940 Out << "!DIMacro(";
1941 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1942 Printer.printMacinfoType(N);
1943 Printer.printInt("line", N->getLine());
1944 Printer.printString("name", N->getName());
1945 Printer.printString("value", N->getValue());
1946 Out << ")";
1947}
1948
1949static void writeDIMacroFile(raw_ostream &Out, const DIMacroFile *N,
1950 TypePrinting *TypePrinter, SlotTracker *Machine,
1951 const Module *Context) {
1952 Out << "!DIMacroFile(";
1953 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1954 Printer.printInt("line", N->getLine());
1955 Printer.printMetadata("file", N->getRawFile(), /* ShouldSkipNull */ false);
1956 Printer.printMetadata("nodes", N->getRawElements());
1957 Out << ")";
1958}
1959
Adrian Prantlab1243f2015-06-29 23:03:47 +00001960static void writeDIModule(raw_ostream &Out, const DIModule *N,
1961 TypePrinting *TypePrinter, SlotTracker *Machine,
1962 const Module *Context) {
1963 Out << "!DIModule(";
1964 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1965 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
1966 Printer.printString("name", N->getName());
1967 Printer.printString("configMacros", N->getConfigurationMacros());
1968 Printer.printString("includePath", N->getIncludePath());
1969 Printer.printString("isysroot", N->getISysRoot());
1970 Out << ")";
1971}
1972
1973
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001974static void writeDITemplateTypeParameter(raw_ostream &Out,
1975 const DITemplateTypeParameter *N,
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00001976 TypePrinting *TypePrinter,
1977 SlotTracker *Machine,
1978 const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001979 Out << "!DITemplateTypeParameter(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001980 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1981 Printer.printString("name", N->getName());
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001982 Printer.printMetadata("type", N->getRawType(), /* ShouldSkipNull */ false);
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00001983 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001984}
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00001985
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001986static void writeDITemplateValueParameter(raw_ostream &Out,
1987 const DITemplateValueParameter *N,
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00001988 TypePrinting *TypePrinter,
1989 SlotTracker *Machine,
1990 const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001991 Out << "!DITemplateValueParameter(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001992 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
Duncan P. N. Exon Smith16d182a2015-02-28 23:21:38 +00001993 if (N->getTag() != dwarf::DW_TAG_template_value_parameter)
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001994 Printer.printTag(N);
1995 Printer.printString("name", N->getName());
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00001996 Printer.printMetadata("type", N->getRawType());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001997 Printer.printMetadata("value", N->getValue(), /* ShouldSkipNull */ false);
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00001998 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001999}
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00002000
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002001static void writeDIGlobalVariable(raw_ostream &Out, const DIGlobalVariable *N,
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00002002 TypePrinting *TypePrinter,
2003 SlotTracker *Machine, const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002004 Out << "!DIGlobalVariable(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00002005 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
2006 Printer.printString("name", N->getName());
2007 Printer.printString("linkageName", N->getLinkageName());
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00002008 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
2009 Printer.printMetadata("file", N->getRawFile());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00002010 Printer.printInt("line", N->getLine());
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00002011 Printer.printMetadata("type", N->getRawType());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00002012 Printer.printBool("isLocal", N->isLocalToUnit());
2013 Printer.printBool("isDefinition", N->isDefinition());
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00002014 Printer.printMetadata("declaration", N->getRawStaticDataMemberDeclaration());
Victor Leschuk2ede1262016-10-20 00:13:12 +00002015 Printer.printInt("align", N->getAlignInBits());
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00002016 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002017}
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00002018
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002019static void writeDILocalVariable(raw_ostream &Out, const DILocalVariable *N,
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00002020 TypePrinting *TypePrinter,
2021 SlotTracker *Machine, const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002022 Out << "!DILocalVariable(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00002023 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00002024 Printer.printString("name", N->getName());
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00002025 Printer.printInt("arg", N->getArg());
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00002026 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
2027 Printer.printMetadata("file", N->getRawFile());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00002028 Printer.printInt("line", N->getLine());
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00002029 Printer.printMetadata("type", N->getRawType());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00002030 Printer.printDIFlags("flags", N->getFlags());
Victor Leschuk2ede1262016-10-20 00:13:12 +00002031 Printer.printInt("align", N->getAlignInBits());
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00002032 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002033}
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00002034
Shiva Chen2c864552018-05-09 02:40:45 +00002035static void writeDILabel(raw_ostream &Out, const DILabel *N,
2036 TypePrinting *TypePrinter,
2037 SlotTracker *Machine, const Module *Context) {
2038 Out << "!DILabel(";
2039 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
2040 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
2041 Printer.printString("name", N->getName());
2042 Printer.printMetadata("file", N->getRawFile());
2043 Printer.printInt("line", N->getLine());
2044 Out << ")";
2045}
2046
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002047static void writeDIExpression(raw_ostream &Out, const DIExpression *N,
Duncan P. N. Exon Smith0c5c0122015-02-13 01:42:09 +00002048 TypePrinting *TypePrinter, SlotTracker *Machine,
2049 const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002050 Out << "!DIExpression(";
Duncan P. N. Exon Smith0c5c0122015-02-13 01:42:09 +00002051 FieldSeparator FS;
2052 if (N->isValid()) {
2053 for (auto I = N->expr_op_begin(), E = N->expr_op_end(); I != E; ++I) {
Mehdi Amini149f6ea2016-10-05 05:59:29 +00002054 auto OpStr = dwarf::OperationEncodingString(I->getOp());
2055 assert(!OpStr.empty() && "Expected valid opcode");
Duncan P. N. Exon Smith0c5c0122015-02-13 01:42:09 +00002056
2057 Out << FS << OpStr;
2058 for (unsigned A = 0, AE = I->getNumArgs(); A != AE; ++A)
2059 Out << FS << I->getArg(A);
2060 }
2061 } else {
2062 for (const auto &I : N->getElements())
2063 Out << FS << I;
2064 }
2065 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002066}
Duncan P. N. Exon Smith0c5c0122015-02-13 01:42:09 +00002067
Adrian Prantlbceaaa92016-12-20 02:09:43 +00002068static void writeDIGlobalVariableExpression(raw_ostream &Out,
2069 const DIGlobalVariableExpression *N,
2070 TypePrinting *TypePrinter,
2071 SlotTracker *Machine,
2072 const Module *Context) {
2073 Out << "!DIGlobalVariableExpression(";
2074 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
2075 Printer.printMetadata("var", N->getVariable());
2076 Printer.printMetadata("expr", N->getExpression());
2077 Out << ")";
2078}
2079
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002080static void writeDIObjCProperty(raw_ostream &Out, const DIObjCProperty *N,
Duncan P. N. Exon Smithd45ce962015-02-13 01:43:22 +00002081 TypePrinting *TypePrinter, SlotTracker *Machine,
2082 const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002083 Out << "!DIObjCProperty(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00002084 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
2085 Printer.printString("name", N->getName());
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00002086 Printer.printMetadata("file", N->getRawFile());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00002087 Printer.printInt("line", N->getLine());
2088 Printer.printString("setter", N->getSetterName());
2089 Printer.printString("getter", N->getGetterName());
2090 Printer.printInt("attributes", N->getAttributes());
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00002091 Printer.printMetadata("type", N->getRawType());
Duncan P. N. Exon Smithd45ce962015-02-13 01:43:22 +00002092 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002093}
Duncan P. N. Exon Smithd45ce962015-02-13 01:43:22 +00002094
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002095static void writeDIImportedEntity(raw_ostream &Out, const DIImportedEntity *N,
Duncan P. N. Exon Smith1c931162015-02-13 01:46:02 +00002096 TypePrinting *TypePrinter,
2097 SlotTracker *Machine, const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002098 Out << "!DIImportedEntity(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00002099 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
2100 Printer.printTag(N);
2101 Printer.printString("name", N->getName());
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00002102 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
2103 Printer.printMetadata("entity", N->getRawEntity());
Adrian Prantld63bfd22017-07-19 00:09:54 +00002104 Printer.printMetadata("file", N->getRawFile());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00002105 Printer.printInt("line", N->getLine());
Duncan P. N. Exon Smith1c931162015-02-13 01:46:02 +00002106 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002107}
2108
Duncan P. N. Exon Smitha6de6a42015-01-12 23:45:31 +00002109static void WriteMDNodeBodyInternal(raw_ostream &Out, const MDNode *Node,
2110 TypePrinting *TypePrinter,
2111 SlotTracker *Machine,
2112 const Module *Context) {
Duncan P. N. Exon Smith2bc00f42015-01-19 23:13:14 +00002113 if (Node->isDistinct())
Duncan P. N. Exon Smitha6de6a42015-01-12 23:45:31 +00002114 Out << "distinct ";
Duncan P. N. Exon Smith3d510662015-03-16 21:21:10 +00002115 else if (Node->isTemporary())
2116 Out << "<temporary!> "; // Handle broken code.
Duncan P. N. Exon Smitha6de6a42015-01-12 23:45:31 +00002117
Duncan P. N. Exon Smith2bc00f42015-01-19 23:13:14 +00002118 switch (Node->getMetadataID()) {
Duncan P. N. Exon Smitha6de6a42015-01-12 23:45:31 +00002119 default:
2120 llvm_unreachable("Expected uniquable MDNode");
Duncan P. N. Exon Smith2bc00f42015-01-19 23:13:14 +00002121#define HANDLE_MDNODE_LEAF(CLASS) \
Duncan P. N. Exon Smitha6de6a42015-01-12 23:45:31 +00002122 case Metadata::CLASS##Kind: \
Duncan P. N. Exon Smith2bc00f42015-01-19 23:13:14 +00002123 write##CLASS(Out, cast<CLASS>(Node), TypePrinter, Machine, Context); \
Duncan P. N. Exon Smitha6de6a42015-01-12 23:45:31 +00002124 break;
2125#include "llvm/IR/Metadata.def"
2126 }
2127}
2128
Chandler Carruthd48cdbf2014-01-09 02:29:41 +00002129// Full implementation of printing a Value as an operand with support for
2130// TypePrinting, etc.
Dan Gohman12dad632009-08-12 20:56:03 +00002131static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
Dan Gohman8061d9e2009-08-13 15:27:57 +00002132 TypePrinting *TypePrinter,
Dan Gohmana2489d12010-07-20 23:55:01 +00002133 SlotTracker *Machine,
2134 const Module *Context) {
Chris Lattner033935d2008-08-17 04:40:13 +00002135 if (V->hasName()) {
2136 PrintLLVMName(Out, V);
2137 return;
2138 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002139
Chris Lattner033935d2008-08-17 04:40:13 +00002140 const Constant *CV = dyn_cast<Constant>(V);
2141 if (CV && !isa<GlobalValue>(CV)) {
Dan Gohman8061d9e2009-08-13 15:27:57 +00002142 assert(TypePrinter && "Constants require TypePrinting!");
Dan Gohmana2489d12010-07-20 23:55:01 +00002143 WriteConstantInternal(Out, CV, *TypePrinter, Machine, Context);
Chris Lattner3a36d2c2008-08-19 05:06:27 +00002144 return;
2145 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002146
Chris Lattner3a36d2c2008-08-19 05:06:27 +00002147 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
Chris Lattner033935d2008-08-17 04:40:13 +00002148 Out << "asm ";
2149 if (IA->hasSideEffects())
2150 Out << "sideeffect ";
Dale Johannesen1cfb9582009-10-21 23:28:00 +00002151 if (IA->isAlignStack())
2152 Out << "alignstack ";
Chad Rosierd8c76102012-09-05 19:00:49 +00002153 // We don't emit the AD_ATT dialect as it's the assumed default.
2154 if (IA->getDialect() == InlineAsm::AD_Intel)
2155 Out << "inteldialect ";
Chris Lattner033935d2008-08-17 04:40:13 +00002156 Out << '"';
2157 PrintEscapedString(IA->getAsmString(), Out);
2158 Out << "\", \"";
2159 PrintEscapedString(IA->getConstraintString(), Out);
2160 Out << '"';
Chris Lattner3a36d2c2008-08-19 05:06:27 +00002161 return;
2162 }
Devang Patel7428d8a2009-07-22 17:43:22 +00002163
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002164 if (auto *MD = dyn_cast<MetadataAsValue>(V)) {
2165 WriteAsOperandInternal(Out, MD->getMetadata(), TypePrinter, Machine,
2166 Context, /* FromValue */ true);
Devang Patel7428d8a2009-07-22 17:43:22 +00002167 return;
2168 }
2169
Chris Lattner3a36d2c2008-08-19 05:06:27 +00002170 char Prefix = '%';
2171 int Slot;
Chris Lattner5b82a0a2011-08-03 06:15:41 +00002172 // If we have a SlotTracker, use it.
Chris Lattner3a36d2c2008-08-19 05:06:27 +00002173 if (Machine) {
2174 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
2175 Slot = Machine->getGlobalSlot(GV);
2176 Prefix = '@';
2177 } else {
2178 Slot = Machine->getLocalSlot(V);
Andrew Trickec4b6e72011-09-30 19:48:58 +00002179
Chris Lattner5b82a0a2011-08-03 06:15:41 +00002180 // If the local value didn't succeed, then we may be referring to a value
2181 // from a different function. Translate it, as this can happen when using
2182 // address of blocks.
2183 if (Slot == -1)
2184 if ((Machine = createSlotTracker(V))) {
2185 Slot = Machine->getLocalSlot(V);
2186 delete Machine;
2187 }
Chris Lattner3a36d2c2008-08-19 05:06:27 +00002188 }
Chris Lattner5b82a0a2011-08-03 06:15:41 +00002189 } else if ((Machine = createSlotTracker(V))) {
2190 // Otherwise, create one to get the # and then destroy it.
2191 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
2192 Slot = Machine->getGlobalSlot(GV);
2193 Prefix = '@';
Chris Lattnera2d810d2006-01-25 22:26:05 +00002194 } else {
Chris Lattner5b82a0a2011-08-03 06:15:41 +00002195 Slot = Machine->getLocalSlot(V);
Chris Lattnerd84bb632002-04-16 21:36:08 +00002196 }
Chris Lattner5b82a0a2011-08-03 06:15:41 +00002197 delete Machine;
Craig Topperc6207612014-04-09 06:08:46 +00002198 Machine = nullptr;
Chris Lattner5b82a0a2011-08-03 06:15:41 +00002199 } else {
2200 Slot = -1;
Chris Lattnerd84bb632002-04-16 21:36:08 +00002201 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002202
Chris Lattner3a36d2c2008-08-19 05:06:27 +00002203 if (Slot != -1)
2204 Out << Prefix << Slot;
2205 else
2206 Out << "<badref>";
Chris Lattnerd84bb632002-04-16 21:36:08 +00002207}
2208
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002209static void WriteAsOperandInternal(raw_ostream &Out, const Metadata *MD,
2210 TypePrinting *TypePrinter,
2211 SlotTracker *Machine, const Module *Context,
2212 bool FromValue) {
Reid Kleckner6d353342017-08-23 20:31:27 +00002213 // Write DIExpressions inline when used as a value. Improves readability of
2214 // debug info intrinsics.
2215 if (const DIExpression *Expr = dyn_cast<DIExpression>(MD)) {
2216 writeDIExpression(Out, Expr, TypePrinter, Machine, Context);
2217 return;
2218 }
2219
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002220 if (const MDNode *N = dyn_cast<MDNode>(MD)) {
Duncan P. N. Exon Smithf8b3ad62015-06-27 00:15:32 +00002221 std::unique_ptr<SlotTracker> MachineStorage;
2222 if (!Machine) {
2223 MachineStorage = make_unique<SlotTracker>(Context);
2224 Machine = MachineStorage.get();
2225 }
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002226 int Slot = Machine->getMetadataSlot(N);
2227 if (Slot == -1)
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +00002228 // Give the pointer value instead of "badref", since this comes up all
2229 // the time when debugging.
2230 Out << "<" << N << ">";
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002231 else
2232 Out << '!' << Slot;
2233 return;
2234 }
2235
2236 if (const MDString *MDS = dyn_cast<MDString>(MD)) {
2237 Out << "!\"";
2238 PrintEscapedString(MDS->getString(), Out);
2239 Out << '"';
2240 return;
2241 }
2242
2243 auto *V = cast<ValueAsMetadata>(MD);
2244 assert(TypePrinter && "TypePrinter required for metadata values");
2245 assert((FromValue || !isa<LocalAsMetadata>(V)) &&
2246 "Unexpected function-local metadata outside of value argument");
2247
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002248 TypePrinter->print(V->getValue()->getType(), Out);
2249 Out << ' ';
2250 WriteAsOperandInternal(Out, V->getValue(), TypePrinter, Machine, Context);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002251}
2252
Benjamin Kramerba05a782015-03-17 19:53:41 +00002253namespace {
Eugene Zelenkode6cce22017-06-19 22:05:08 +00002254
Benjamin Kramerba05a782015-03-17 19:53:41 +00002255class AssemblyWriter {
2256 formatted_raw_ostream &Out;
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00002257 const Module *TheModule = nullptr;
2258 const ModuleSummaryIndex *TheIndex = nullptr;
Duncan P. N. Exon Smith326921542015-06-26 22:04:20 +00002259 std::unique_ptr<SlotTracker> SlotTrackerStorage;
Benjamin Kramerba05a782015-03-17 19:53:41 +00002260 SlotTracker &Machine;
2261 TypePrinting TypePrinter;
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00002262 AssemblyAnnotationWriter *AnnotationWriter = nullptr;
Benjamin Kramerba05a782015-03-17 19:53:41 +00002263 SetVector<const Comdat *> Comdats;
Justin Bognerd7d1a722015-09-27 22:38:50 +00002264 bool IsForDebug;
Duncan P. N. Exon Smith89010d82015-04-15 01:36:30 +00002265 bool ShouldPreserveUseListOrder;
Benjamin Kramerba05a782015-03-17 19:53:41 +00002266 UseListOrderStack UseListOrders;
Duncan P. N. Exon Smithe30f10e2015-04-24 21:03:05 +00002267 SmallVector<StringRef, 8> MDNames;
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00002268 /// Synchronization scope names registered with LLVMContext.
2269 SmallVector<StringRef, 8> SSNs;
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00002270 DenseMap<const GlobalValueSummary *, GlobalValue::GUID> SummaryToGUIDMap;
Benjamin Kramerba05a782015-03-17 19:53:41 +00002271
2272public:
2273 /// Construct an AssemblyWriter with an external SlotTracker
Duncan P. N. Exon Smith89010d82015-04-15 01:36:30 +00002274 AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac, const Module *M,
Justin Bognerd7d1a722015-09-27 22:38:50 +00002275 AssemblyAnnotationWriter *AAW, bool IsForDebug,
Duncan P. N. Exon Smith89010d82015-04-15 01:36:30 +00002276 bool ShouldPreserveUseListOrder = false);
Benjamin Kramerba05a782015-03-17 19:53:41 +00002277
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00002278 AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac,
2279 const ModuleSummaryIndex *Index, bool IsForDebug);
2280
Benjamin Kramerba05a782015-03-17 19:53:41 +00002281 void printMDNodeBody(const MDNode *MD);
2282 void printNamedMDNode(const NamedMDNode *NMD);
2283
2284 void printModule(const Module *M);
2285
2286 void writeOperand(const Value *Op, bool PrintType);
Reid Kleckner6652a522017-04-28 18:37:16 +00002287 void writeParamOperand(const Value *Operand, AttributeSet Attrs);
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002288 void writeOperandBundles(ImmutableCallSite CS);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00002289 void writeSyncScope(const LLVMContext &Context,
2290 SyncScope::ID SSID);
2291 void writeAtomic(const LLVMContext &Context,
2292 AtomicOrdering Ordering,
2293 SyncScope::ID SSID);
2294 void writeAtomicCmpXchg(const LLVMContext &Context,
2295 AtomicOrdering SuccessOrdering,
Benjamin Kramerba05a782015-03-17 19:53:41 +00002296 AtomicOrdering FailureOrdering,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00002297 SyncScope::ID SSID);
Benjamin Kramerba05a782015-03-17 19:53:41 +00002298
2299 void writeAllMDNodes();
2300 void writeMDNode(unsigned Slot, const MDNode *Node);
2301 void writeAllAttributeGroups();
2302
2303 void printTypeIdentities();
2304 void printGlobal(const GlobalVariable *GV);
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +00002305 void printIndirectSymbol(const GlobalIndirectSymbol *GIS);
Benjamin Kramerba05a782015-03-17 19:53:41 +00002306 void printComdat(const Comdat *C);
2307 void printFunction(const Function *F);
Reid Kleckner6652a522017-04-28 18:37:16 +00002308 void printArgument(const Argument *FA, AttributeSet Attrs);
Benjamin Kramerba05a782015-03-17 19:53:41 +00002309 void printBasicBlock(const BasicBlock *BB);
2310 void printInstructionLine(const Instruction &I);
2311 void printInstruction(const Instruction &I);
2312
2313 void printUseListOrder(const UseListOrder &Order);
2314 void printUseLists(const Function *F);
2315
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00002316 void printModuleSummaryIndex();
2317 void printSummaryInfo(unsigned Slot, const ValueInfo &VI);
2318 void printSummary(const GlobalValueSummary &Summary);
2319 void printAliasSummary(const AliasSummary *AS);
2320 void printGlobalVarSummary(const GlobalVarSummary *GS);
2321 void printFunctionSummary(const FunctionSummary *FS);
2322 void printTypeIdSummary(const TypeIdSummary &TIS);
2323 void printTypeTestResolution(const TypeTestResolution &TTRes);
2324 void printArgs(const std::vector<uint64_t> &Args);
2325 void printWPDRes(const WholeProgramDevirtResolution &WPDRes);
2326 void printTypeIdInfo(const FunctionSummary::TypeIdInfo &TIDInfo);
2327 void printVFuncId(const FunctionSummary::VFuncId VFId);
2328 void
2329 printNonConstVCalls(const std::vector<FunctionSummary::VFuncId> VCallList,
2330 const char *Tag);
2331 void
2332 printConstVCalls(const std::vector<FunctionSummary::ConstVCall> VCallList,
2333 const char *Tag);
2334
Benjamin Kramerba05a782015-03-17 19:53:41 +00002335private:
Adrian Prantl5f8f34e42018-05-01 15:54:18 +00002336 /// Print out metadata attachments.
Duncan P. N. Exon Smith86979272015-04-24 20:59:52 +00002337 void printMetadataAttachments(
Duncan P. N. Exon Smithd3e4c2a2015-04-24 21:06:21 +00002338 const SmallVectorImpl<std::pair<unsigned, MDNode *>> &MDs,
2339 StringRef Separator);
Duncan P. N. Exon Smith86979272015-04-24 20:59:52 +00002340
Benjamin Kramerba05a782015-03-17 19:53:41 +00002341 // printInfoComment - Print a little comment after the instruction indicating
2342 // which slot it occupies.
2343 void printInfoComment(const Value &V);
Igor Laevsky2aa8caf2015-05-05 13:20:42 +00002344
2345 // printGCRelocateComment - print comment after call to the gc.relocate
2346 // intrinsic indicating base and derived pointer names.
Manuel Jacob83eefa62016-01-05 04:03:00 +00002347 void printGCRelocateComment(const GCRelocateInst &Relocate);
Benjamin Kramerba05a782015-03-17 19:53:41 +00002348};
Eugene Zelenkode6cce22017-06-19 22:05:08 +00002349
2350} // end anonymous namespace
Benjamin Kramerba05a782015-03-17 19:53:41 +00002351
Justin Bogner0ffea9d2015-09-02 22:46:15 +00002352AssemblyWriter::AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac,
2353 const Module *M, AssemblyAnnotationWriter *AAW,
Justin Bognerd7d1a722015-09-27 22:38:50 +00002354 bool IsForDebug, bool ShouldPreserveUseListOrder)
Roman Tereshind96de6f2018-03-22 21:29:07 +00002355 : Out(o), TheModule(M), Machine(Mac), TypePrinter(M), AnnotationWriter(AAW),
Justin Bognerd7d1a722015-09-27 22:38:50 +00002356 IsForDebug(IsForDebug),
Justin Bogner0ffea9d2015-09-02 22:46:15 +00002357 ShouldPreserveUseListOrder(ShouldPreserveUseListOrder) {
David Majnemerdad0a642014-06-27 18:19:56 +00002358 if (!TheModule)
2359 return;
Peter Collingbourne6d88fde2016-06-22 20:29:42 +00002360 for (const GlobalObject &GO : TheModule->global_objects())
2361 if (const Comdat *C = GO.getComdat())
David Majnemerdad0a642014-06-27 18:19:56 +00002362 Comdats.insert(C);
Daniel Maleaded9f932013-05-08 20:38:31 +00002363}
Chris Lattner2e9fee42001-07-12 23:35:26 +00002364
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00002365AssemblyWriter::AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac,
2366 const ModuleSummaryIndex *Index, bool IsForDebug)
2367 : Out(o), TheIndex(Index), Machine(Mac), TypePrinter(/*Module=*/nullptr),
2368 IsForDebug(IsForDebug), ShouldPreserveUseListOrder(false) {}
2369
Chris Lattner78e2e8b2006-12-06 06:24:27 +00002370void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType) {
Craig Topperc6207612014-04-09 06:08:46 +00002371 if (!Operand) {
Chris Lattner08f7d0c2005-02-24 16:58:29 +00002372 Out << "<null operand!>";
Chris Lattnerb419a1e2009-12-31 02:33:14 +00002373 return;
Chris Lattner08f7d0c2005-02-24 16:58:29 +00002374 }
Chris Lattnerb419a1e2009-12-31 02:33:14 +00002375 if (PrintType) {
2376 TypePrinter.print(Operand->getType(), Out);
2377 Out << ' ';
2378 }
Dan Gohmana2489d12010-07-20 23:55:01 +00002379 WriteAsOperandInternal(Out, Operand, &TypePrinter, &Machine, TheModule);
Chris Lattner2f7c9632001-06-06 20:29:01 +00002380}
2381
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00002382void AssemblyWriter::writeSyncScope(const LLVMContext &Context,
2383 SyncScope::ID SSID) {
2384 switch (SSID) {
2385 case SyncScope::System: {
2386 break;
2387 }
2388 default: {
2389 if (SSNs.empty())
2390 Context.getSyncScopeNames(SSNs);
2391
2392 Out << " syncscope(\"";
2393 PrintEscapedString(SSNs[SSID], Out);
2394 Out << "\")";
2395 break;
2396 }
2397 }
2398}
2399
2400void AssemblyWriter::writeAtomic(const LLVMContext &Context,
2401 AtomicOrdering Ordering,
2402 SyncScope::ID SSID) {
JF Bastien800f87a2016-04-06 21:19:33 +00002403 if (Ordering == AtomicOrdering::NotAtomic)
Eli Friedmanfee02c62011-07-25 23:16:38 +00002404 return;
2405
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00002406 writeSyncScope(Context, SSID);
JF Bastien800f87a2016-04-06 21:19:33 +00002407 Out << " " << toIRString(Ordering);
Eli Friedmanfee02c62011-07-25 23:16:38 +00002408}
2409
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00002410void AssemblyWriter::writeAtomicCmpXchg(const LLVMContext &Context,
2411 AtomicOrdering SuccessOrdering,
Tim Northovere94a5182014-03-11 10:48:52 +00002412 AtomicOrdering FailureOrdering,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00002413 SyncScope::ID SSID) {
JF Bastien800f87a2016-04-06 21:19:33 +00002414 assert(SuccessOrdering != AtomicOrdering::NotAtomic &&
2415 FailureOrdering != AtomicOrdering::NotAtomic);
Tim Northovere94a5182014-03-11 10:48:52 +00002416
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00002417 writeSyncScope(Context, SSID);
JF Bastien800f87a2016-04-06 21:19:33 +00002418 Out << " " << toIRString(SuccessOrdering);
2419 Out << " " << toIRString(FailureOrdering);
Tim Northovere94a5182014-03-11 10:48:52 +00002420}
2421
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002422void AssemblyWriter::writeParamOperand(const Value *Operand,
Reid Kleckner6652a522017-04-28 18:37:16 +00002423 AttributeSet Attrs) {
Craig Topperc6207612014-04-09 06:08:46 +00002424 if (!Operand) {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00002425 Out << "<null operand!>";
Chris Lattnerb419a1e2009-12-31 02:33:14 +00002426 return;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00002427 }
Chris Lattnerb419a1e2009-12-31 02:33:14 +00002428
2429 // Print the type
2430 TypePrinter.print(Operand->getType(), Out);
2431 // Print parameter attributes list
Reid Kleckner6652a522017-04-28 18:37:16 +00002432 if (Attrs.hasAttributes())
2433 Out << ' ' << Attrs.getAsString();
Chris Lattnerb419a1e2009-12-31 02:33:14 +00002434 Out << ' ';
2435 // Print the operand
Dan Gohmana2489d12010-07-20 23:55:01 +00002436 WriteAsOperandInternal(Out, Operand, &TypePrinter, &Machine, TheModule);
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00002437}
Chris Lattner2f7c9632001-06-06 20:29:01 +00002438
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002439void AssemblyWriter::writeOperandBundles(ImmutableCallSite CS) {
2440 if (!CS.hasOperandBundles())
2441 return;
2442
2443 Out << " [ ";
2444
2445 bool FirstBundle = true;
2446 for (unsigned i = 0, e = CS.getNumOperandBundles(); i != e; ++i) {
Sanjoy Das54c3ca62015-11-07 01:56:04 +00002447 OperandBundleUse BU = CS.getOperandBundleAt(i);
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002448
2449 if (!FirstBundle)
2450 Out << ", ";
2451 FirstBundle = false;
2452
2453 Out << '"';
Sanjoy Dasb9ca6dc2015-11-10 20:13:15 +00002454 PrintEscapedString(BU.getTagName(), Out);
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002455 Out << '"';
2456
2457 Out << '(';
2458
2459 bool FirstInput = true;
2460 for (const auto &Input : BU.Inputs) {
2461 if (!FirstInput)
2462 Out << ", ";
2463 FirstInput = false;
2464
2465 TypePrinter.print(Input->getType(), Out);
2466 Out << " ";
2467 WriteAsOperandInternal(Out, Input, &TypePrinter, &Machine, TheModule);
2468 }
2469
2470 Out << ')';
2471 }
2472
2473 Out << " ]";
2474}
2475
Chris Lattner7bfee412001-10-29 16:05:51 +00002476void AssemblyWriter::printModule(const Module *M) {
Bill Wendling829b4782013-02-11 08:43:33 +00002477 Machine.initialize();
2478
Duncan P. N. Exon Smith89010d82015-04-15 01:36:30 +00002479 if (ShouldPreserveUseListOrder)
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +00002480 UseListOrders = predictUseListOrder(M);
2481
Chris Lattner4d8689e2005-03-02 23:12:40 +00002482 if (!M->getModuleIdentifier().empty() &&
Misha Brukmanb1c93172005-04-21 23:48:37 +00002483 // Don't print the ID if it will start a new line (which would
Chris Lattner4d8689e2005-03-02 23:12:40 +00002484 // require a comment char before it).
2485 M->getModuleIdentifier().find('\n') == std::string::npos)
2486 Out << "; ModuleID = '" << M->getModuleIdentifier() << "'\n";
2487
Teresa Johnson83c517c2016-03-30 18:15:08 +00002488 if (!M->getSourceFileName().empty()) {
Teresa Johnsond8d94652016-03-30 22:17:28 +00002489 Out << "source_filename = \"";
2490 PrintEscapedString(M->getSourceFileName(), Out);
2491 Out << "\"\n";
Teresa Johnson83c517c2016-03-30 18:15:08 +00002492 }
2493
Rafael Espindolaf863ee22014-02-25 20:01:08 +00002494 const std::string &DL = M->getDataLayoutStr();
2495 if (!DL.empty())
2496 Out << "target datalayout = \"" << DL << "\"\n";
Reid Spencer48f98c82004-07-25 21:44:54 +00002497 if (!M->getTargetTriple().empty())
Reid Spencerffec7df2004-07-25 21:29:43 +00002498 Out << "target triple = \"" << M->getTargetTriple() << "\"\n";
Misha Brukmanb1c93172005-04-21 23:48:37 +00002499
Chris Lattnereef2fe72006-01-24 04:13:11 +00002500 if (!M->getModuleInlineAsm().empty()) {
Dan Gohman69273e62009-08-12 23:54:22 +00002501 Out << '\n';
Benjamin Kramerbbd05a22015-06-07 13:59:33 +00002502
2503 // Split the string into lines, to make it easier to read the .ll file.
Benjamin Kramerf1cfc422015-06-08 18:58:57 +00002504 StringRef Asm = M->getModuleInlineAsm();
Benjamin Kramerbbd05a22015-06-07 13:59:33 +00002505 do {
2506 StringRef Front;
2507 std::tie(Front, Asm) = Asm.split('\n');
2508
Chris Lattnerefaf35d2006-01-24 00:45:30 +00002509 // We found a newline, print the portion of the asm string from the
2510 // last newline up to this newline.
2511 Out << "module asm \"";
Benjamin Kramerbbd05a22015-06-07 13:59:33 +00002512 PrintEscapedString(Front, Out);
Chris Lattnerefaf35d2006-01-24 00:45:30 +00002513 Out << "\"\n";
Benjamin Kramerbbd05a22015-06-07 13:59:33 +00002514 } while (!Asm.empty());
Chris Lattner6ed87bd2006-01-23 23:03:36 +00002515 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002516
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002517 printTypeIdentities();
Misha Brukmanb1c93172005-04-21 23:48:37 +00002518
David Majnemerdad0a642014-06-27 18:19:56 +00002519 // Output all comdats.
2520 if (!Comdats.empty())
2521 Out << '\n';
2522 for (const Comdat *C : Comdats) {
2523 printComdat(C);
2524 if (C != Comdats.back())
2525 Out << '\n';
2526 }
2527
Dan Gohman69273e62009-08-12 23:54:22 +00002528 // Output all globals.
2529 if (!M->global_empty()) Out << '\n';
Yaron Kerenf3cf9d12015-06-13 17:50:47 +00002530 for (const GlobalVariable &GV : M->globals()) {
2531 printGlobal(&GV); Out << '\n';
Duncan Sands66fc0e62012-09-12 09:55:51 +00002532 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002533
Chris Lattnerd2747052007-04-26 02:24:10 +00002534 // Output all aliases.
2535 if (!M->alias_empty()) Out << "\n";
Yaron Kerenf3cf9d12015-06-13 17:50:47 +00002536 for (const GlobalAlias &GA : M->aliases())
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +00002537 printIndirectSymbol(&GA);
Chris Lattnerfee714f2001-09-07 16:36:04 +00002538
Dmitry Polukhina1feff72016-04-07 12:32:19 +00002539 // Output all ifuncs.
2540 if (!M->ifunc_empty()) Out << "\n";
2541 for (const GlobalIFunc &GI : M->ifuncs())
2542 printIndirectSymbol(&GI);
2543
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +00002544 // Output global use-lists.
2545 printUseLists(nullptr);
2546
Chris Lattner2cdd49d2004-09-14 05:06:58 +00002547 // Output all of the functions.
Yaron Kerenf3cf9d12015-06-13 17:50:47 +00002548 for (const Function &F : *M)
2549 printFunction(&F);
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +00002550 assert(UseListOrders.empty() && "All use-lists should have been consumed");
Devang Patel983c6b12009-07-08 21:44:25 +00002551
Bill Wendling829b4782013-02-11 08:43:33 +00002552 // Output all attribute groups.
Bill Wendling04945972013-04-29 23:48:06 +00002553 if (!Machine.as_empty()) {
Bill Wendling829b4782013-02-11 08:43:33 +00002554 Out << '\n';
2555 writeAllAttributeGroups();
2556 }
2557
Devang Patel23e68302009-07-29 22:04:47 +00002558 // Output named metadata.
Dan Gohman69273e62009-08-12 23:54:22 +00002559 if (!M->named_metadata_empty()) Out << '\n';
Andrew Trickec4b6e72011-09-30 19:48:58 +00002560
Yaron Kerenf3cf9d12015-06-13 17:50:47 +00002561 for (const NamedMDNode &Node : M->named_metadata())
2562 printNamedMDNode(&Node);
Devang Patel23e68302009-07-29 22:04:47 +00002563
2564 // Output metadata.
Chris Lattnercf4a76e2009-12-31 02:20:11 +00002565 if (!Machine.mdn_empty()) {
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002566 Out << '\n';
2567 writeAllMDNodes();
2568 }
Chris Lattnerfee714f2001-09-07 16:36:04 +00002569}
2570
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00002571void AssemblyWriter::printModuleSummaryIndex() {
2572 assert(TheIndex);
2573 Machine.initializeIndex();
2574
2575 Out << "\n";
2576
2577 // Print module path entries, using the module id as the slot number. To
2578 // print in order, add paths to a vector indexed by module id.
2579 std::vector<std::pair<StringRef, ModuleHash>> moduleVec;
2580 std::string RegularLTOModuleName = "[Regular LTO]";
2581 moduleVec.resize(TheIndex->modulePaths().size());
2582 for (auto &ModPath : TheIndex->modulePaths()) {
2583 // A module id of -1 is a special entry for a regular LTO module created
2584 // during the thin link.
2585 if (ModPath.second.first == -1u)
2586 moduleVec[TheIndex->modulePaths().size() - 1] =
2587 std::make_pair(RegularLTOModuleName, ModPath.second.second);
2588 else {
2589 assert(ModPath.second.first < moduleVec.size());
2590 moduleVec[ModPath.second.first] =
2591 std::make_pair(ModPath.first(), ModPath.second.second);
2592 }
2593 }
2594 unsigned i = 0;
2595 for (auto &ModPair : moduleVec) {
2596 Out << "^" << i++ << " = module: (";
2597 Out << "path: \"" << ModPair.first << "\"";
2598 Out << ", hash: (";
2599 FieldSeparator FS;
2600 for (auto Hash : ModPair.second)
2601 Out << FS << Hash;
2602 Out << "))\n";
2603 }
2604
2605 // FIXME: Change AliasSummary to hold a ValueInfo instead of summary pointer
2606 // for aliasee (then update BitcodeWriter.cpp and remove get/setAliaseeGUID).
2607 for (auto &GlobalList : *TheIndex) {
2608 auto GUID = GlobalList.first;
2609 for (auto &Summary : GlobalList.second.SummaryList)
2610 SummaryToGUIDMap[Summary.get()] = GUID;
2611 }
2612
2613 // Print the global value summary entries.
2614 for (auto &GlobalList : *TheIndex) {
2615 auto GUID = GlobalList.first;
2616 auto VI = TheIndex->getValueInfo(GlobalList);
2617 printSummaryInfo(Machine.getGUIDSlot(GUID), VI);
2618 }
2619
2620 // Print the TypeIdMap entries.
2621 for (auto &TId : TheIndex->typeIds()) {
2622 auto GUID = GlobalValue::getGUID(TId.first);
2623 Out << "^" << Machine.getGUIDSlot(GUID) << " = typeid: (name: \""
2624 << TId.first << "\"";
2625 printTypeIdSummary(TId.second);
2626 Out << ") ; guid = " << GUID << "\n";
2627 }
2628}
2629
2630static const char *
2631getWholeProgDevirtResKindName(WholeProgramDevirtResolution::Kind K) {
2632 switch (K) {
2633 case WholeProgramDevirtResolution::Indir:
2634 return "indir";
2635 case WholeProgramDevirtResolution::SingleImpl:
2636 return "singleImpl";
2637 case WholeProgramDevirtResolution::BranchFunnel:
2638 return "branchFunnel";
2639 }
2640 llvm_unreachable("invalid WholeProgramDevirtResolution kind");
2641}
2642
2643static const char *getWholeProgDevirtResByArgKindName(
2644 WholeProgramDevirtResolution::ByArg::Kind K) {
2645 switch (K) {
2646 case WholeProgramDevirtResolution::ByArg::Indir:
2647 return "indir";
2648 case WholeProgramDevirtResolution::ByArg::UniformRetVal:
2649 return "uniformRetVal";
2650 case WholeProgramDevirtResolution::ByArg::UniqueRetVal:
2651 return "uniqueRetVal";
2652 case WholeProgramDevirtResolution::ByArg::VirtualConstProp:
2653 return "virtualConstProp";
2654 }
2655 llvm_unreachable("invalid WholeProgramDevirtResolution::ByArg kind");
2656}
2657
2658static const char *getTTResKindName(TypeTestResolution::Kind K) {
2659 switch (K) {
2660 case TypeTestResolution::Unsat:
2661 return "unsat";
2662 case TypeTestResolution::ByteArray:
2663 return "byteArray";
2664 case TypeTestResolution::Inline:
2665 return "inline";
2666 case TypeTestResolution::Single:
2667 return "single";
2668 case TypeTestResolution::AllOnes:
2669 return "allOnes";
2670 }
2671 llvm_unreachable("invalid TypeTestResolution kind");
2672}
2673
2674void AssemblyWriter::printTypeTestResolution(const TypeTestResolution &TTRes) {
2675 Out << "typeTestRes: (kind: " << getTTResKindName(TTRes.TheKind)
2676 << ", sizeM1BitWidth: " << TTRes.SizeM1BitWidth;
2677
2678 // The following fields are only used if the target does not support the use
2679 // of absolute symbols to store constants. Print only if non-zero.
2680 if (TTRes.AlignLog2)
2681 Out << ", alignLog2: " << TTRes.AlignLog2;
2682 if (TTRes.SizeM1)
2683 Out << ", sizeM1: " << TTRes.SizeM1;
2684 if (TTRes.BitMask)
2685 // BitMask is uint8_t which causes it to print the corresponding char.
2686 Out << ", bitMask: " << (unsigned)TTRes.BitMask;
2687 if (TTRes.InlineBits)
2688 Out << ", inlineBits: " << TTRes.InlineBits;
2689
2690 Out << ")";
2691}
2692
2693void AssemblyWriter::printTypeIdSummary(const TypeIdSummary &TIS) {
2694 Out << ", summary: (";
2695 printTypeTestResolution(TIS.TTRes);
2696 if (!TIS.WPDRes.empty()) {
2697 Out << ", wpdResolutions: (";
2698 FieldSeparator FS;
2699 for (auto &WPDRes : TIS.WPDRes) {
2700 Out << FS;
2701 Out << "(offset: " << WPDRes.first << ", ";
2702 printWPDRes(WPDRes.second);
2703 Out << ")";
2704 }
2705 Out << ")";
2706 }
2707 Out << ")";
2708}
2709
2710void AssemblyWriter::printArgs(const std::vector<uint64_t> &Args) {
2711 Out << "args: (";
2712 FieldSeparator FS;
2713 for (auto arg : Args) {
2714 Out << FS;
2715 Out << arg;
2716 }
2717 Out << ")";
2718}
2719
2720void AssemblyWriter::printWPDRes(const WholeProgramDevirtResolution &WPDRes) {
2721 Out << "wpdRes: (kind: ";
2722 Out << getWholeProgDevirtResKindName(WPDRes.TheKind);
2723
2724 if (WPDRes.TheKind == WholeProgramDevirtResolution::SingleImpl)
2725 Out << ", singleImplName: \"" << WPDRes.SingleImplName << "\"";
2726
2727 if (!WPDRes.ResByArg.empty()) {
2728 Out << ", resByArg: (";
2729 FieldSeparator FS;
2730 for (auto &ResByArg : WPDRes.ResByArg) {
2731 Out << FS;
2732 printArgs(ResByArg.first);
2733 Out << ", byArg: (kind: ";
2734 Out << getWholeProgDevirtResByArgKindName(ResByArg.second.TheKind);
2735 if (ResByArg.second.TheKind ==
2736 WholeProgramDevirtResolution::ByArg::UniformRetVal ||
2737 ResByArg.second.TheKind ==
2738 WholeProgramDevirtResolution::ByArg::UniqueRetVal)
2739 Out << ", info: " << ResByArg.second.Info;
2740
2741 // The following fields are only used if the target does not support the
2742 // use of absolute symbols to store constants. Print only if non-zero.
2743 if (ResByArg.second.Byte || ResByArg.second.Bit)
2744 Out << ", byte: " << ResByArg.second.Byte
2745 << ", bit: " << ResByArg.second.Bit;
2746
2747 Out << ")";
2748 }
2749 Out << ")";
2750 }
2751 Out << ")";
2752}
2753
2754static const char *getSummaryKindName(GlobalValueSummary::SummaryKind SK) {
2755 switch (SK) {
2756 case GlobalValueSummary::AliasKind:
2757 return "alias";
2758 case GlobalValueSummary::FunctionKind:
2759 return "function";
2760 case GlobalValueSummary::GlobalVarKind:
2761 return "variable";
2762 }
2763 llvm_unreachable("invalid summary kind");
2764}
2765
2766void AssemblyWriter::printAliasSummary(const AliasSummary *AS) {
2767 Out << ", aliasee: ^"
2768 << Machine.getGUIDSlot(SummaryToGUIDMap[&AS->getAliasee()]);
2769}
2770
2771void AssemblyWriter::printGlobalVarSummary(const GlobalVarSummary *GS) {
2772 // Nothing for now
2773}
2774
Teresa Johnsonc80c8732018-05-25 15:15:39 +00002775static std::string getLinkageName(GlobalValue::LinkageTypes LT) {
2776 switch (LT) {
2777 case GlobalValue::ExternalLinkage:
2778 return "external";
2779 case GlobalValue::PrivateLinkage:
2780 return "private";
2781 case GlobalValue::InternalLinkage:
2782 return "internal";
2783 case GlobalValue::LinkOnceAnyLinkage:
2784 return "linkonce";
2785 case GlobalValue::LinkOnceODRLinkage:
2786 return "linkonce_odr";
2787 case GlobalValue::WeakAnyLinkage:
2788 return "weak";
2789 case GlobalValue::WeakODRLinkage:
2790 return "weak_odr";
2791 case GlobalValue::CommonLinkage:
2792 return "common";
2793 case GlobalValue::AppendingLinkage:
2794 return "appending";
2795 case GlobalValue::ExternalWeakLinkage:
2796 return "extern_weak";
2797 case GlobalValue::AvailableExternallyLinkage:
2798 return "available_externally";
2799 }
2800 llvm_unreachable("invalid linkage");
2801}
2802
2803// When printing the linkage types in IR where the ExternalLinkage is
2804// not printed, and other linkage types are expected to be printed with
2805// a space after the name.
2806static std::string getLinkageNameWithSpace(GlobalValue::LinkageTypes LT) {
2807 if (LT == GlobalValue::ExternalLinkage)
2808 return "";
2809 return getLinkageName(LT) + " ";
2810}
2811
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00002812static const char *getHotnessName(CalleeInfo::HotnessType HT) {
2813 switch (HT) {
2814 case CalleeInfo::HotnessType::Unknown:
2815 return "unknown";
2816 case CalleeInfo::HotnessType::Cold:
2817 return "cold";
2818 case CalleeInfo::HotnessType::None:
2819 return "none";
2820 case CalleeInfo::HotnessType::Hot:
2821 return "hot";
2822 case CalleeInfo::HotnessType::Critical:
2823 return "critical";
2824 }
2825 llvm_unreachable("invalid hotness");
2826}
2827
2828void AssemblyWriter::printFunctionSummary(const FunctionSummary *FS) {
2829 Out << ", insts: " << FS->instCount();
2830
2831 FunctionSummary::FFlags FFlags = FS->fflags();
2832 if (FFlags.ReadNone | FFlags.ReadOnly | FFlags.NoRecurse |
2833 FFlags.ReturnDoesNotAlias) {
2834 Out << ", funcFlags: (";
2835 Out << "readNone: " << FFlags.ReadNone;
2836 Out << ", readOnly: " << FFlags.ReadOnly;
2837 Out << ", noRecurse: " << FFlags.NoRecurse;
2838 Out << ", returnDoesNotAlias: " << FFlags.ReturnDoesNotAlias;
2839 Out << ")";
2840 }
2841 if (!FS->calls().empty()) {
2842 Out << ", calls: (";
2843 FieldSeparator IFS;
2844 for (auto &Call : FS->calls()) {
2845 Out << IFS;
2846 Out << "(callee: ^" << Machine.getGUIDSlot(Call.first.getGUID());
2847 if (Call.second.getHotness() != CalleeInfo::HotnessType::Unknown)
2848 Out << ", hotness: " << getHotnessName(Call.second.getHotness());
2849 else if (Call.second.RelBlockFreq)
2850 Out << ", relbf: " << Call.second.RelBlockFreq;
2851 Out << ")";
2852 }
2853 Out << ")";
2854 }
2855
2856 if (const auto *TIdInfo = FS->getTypeIdInfo())
2857 printTypeIdInfo(*TIdInfo);
2858}
2859
2860void AssemblyWriter::printTypeIdInfo(
2861 const FunctionSummary::TypeIdInfo &TIDInfo) {
2862 Out << ", typeIdInfo: (";
2863 FieldSeparator TIDFS;
2864 if (!TIDInfo.TypeTests.empty()) {
2865 Out << TIDFS;
2866 Out << "typeTests: (";
2867 FieldSeparator FS;
2868 for (auto &GUID : TIDInfo.TypeTests) {
2869 Out << FS;
2870 auto Slot = Machine.getGUIDSlot(GUID);
2871 if (Slot != -1)
2872 Out << "^" << Slot;
2873 else
2874 Out << GUID;
2875 }
2876 Out << ")";
2877 }
2878 if (!TIDInfo.TypeTestAssumeVCalls.empty()) {
2879 Out << TIDFS;
2880 printNonConstVCalls(TIDInfo.TypeTestAssumeVCalls, "typeTestAssumeVCalls");
2881 }
2882 if (!TIDInfo.TypeCheckedLoadVCalls.empty()) {
2883 Out << TIDFS;
2884 printNonConstVCalls(TIDInfo.TypeCheckedLoadVCalls, "typeCheckedLoadVCalls");
2885 }
2886 if (!TIDInfo.TypeTestAssumeConstVCalls.empty()) {
2887 Out << TIDFS;
2888 printConstVCalls(TIDInfo.TypeTestAssumeConstVCalls,
2889 "typeTestAssumeConstVCalls");
2890 }
2891 if (!TIDInfo.TypeCheckedLoadConstVCalls.empty()) {
2892 Out << TIDFS;
2893 printConstVCalls(TIDInfo.TypeCheckedLoadConstVCalls,
2894 "typeCheckedLoadConstVCalls");
2895 }
2896 Out << ")";
2897}
2898
2899void AssemblyWriter::printVFuncId(const FunctionSummary::VFuncId VFId) {
2900 Out << "vFuncId: (";
2901 auto Slot = Machine.getGUIDSlot(VFId.GUID);
2902 if (Slot != -1)
2903 Out << "^" << Slot;
2904 else
2905 Out << "guid: " << VFId.GUID;
2906 Out << ", offset: " << VFId.Offset;
2907 Out << ")";
2908}
2909
2910void AssemblyWriter::printNonConstVCalls(
2911 const std::vector<FunctionSummary::VFuncId> VCallList, const char *Tag) {
2912 Out << Tag << ": (";
2913 FieldSeparator FS;
2914 for (auto &VFuncId : VCallList) {
2915 Out << FS;
2916 printVFuncId(VFuncId);
2917 }
2918 Out << ")";
2919}
2920
2921void AssemblyWriter::printConstVCalls(
2922 const std::vector<FunctionSummary::ConstVCall> VCallList, const char *Tag) {
2923 Out << Tag << ": (";
2924 FieldSeparator FS;
2925 for (auto &ConstVCall : VCallList) {
2926 Out << FS;
2927 printVFuncId(ConstVCall.VFunc);
2928 if (!ConstVCall.Args.empty()) {
2929 Out << ", ";
2930 printArgs(ConstVCall.Args);
2931 }
2932 }
2933 Out << ")";
2934}
2935
2936void AssemblyWriter::printSummary(const GlobalValueSummary &Summary) {
2937 GlobalValueSummary::GVFlags GVFlags = Summary.flags();
2938 GlobalValue::LinkageTypes LT = (GlobalValue::LinkageTypes)GVFlags.Linkage;
2939 Out << getSummaryKindName(Summary.getSummaryKind()) << ": ";
2940 Out << "(module: ^" << TheIndex->getModuleId(Summary.modulePath())
2941 << ", flags: (";
2942 Out << "linkage: " << getLinkageName(LT);
2943 Out << ", notEligibleToImport: " << GVFlags.NotEligibleToImport;
2944 Out << ", live: " << GVFlags.Live;
2945 Out << ", dsoLocal: " << GVFlags.DSOLocal;
2946 Out << ")";
2947
2948 if (Summary.getSummaryKind() == GlobalValueSummary::AliasKind)
2949 printAliasSummary(cast<AliasSummary>(&Summary));
2950 else if (Summary.getSummaryKind() == GlobalValueSummary::FunctionKind)
2951 printFunctionSummary(cast<FunctionSummary>(&Summary));
2952 else
2953 printGlobalVarSummary(cast<GlobalVarSummary>(&Summary));
2954
2955 auto RefList = Summary.refs();
2956 if (!RefList.empty()) {
2957 Out << ", refs: (";
2958 FieldSeparator FS;
2959 for (auto &Ref : RefList) {
2960 Out << FS;
2961 Out << "^" << Machine.getGUIDSlot(Ref.getGUID());
2962 }
2963 Out << ")";
2964 }
2965
2966 Out << ")";
2967}
2968
2969void AssemblyWriter::printSummaryInfo(unsigned Slot, const ValueInfo &VI) {
2970 Out << "^" << Slot << " = gv: (";
2971 if (!VI.name().empty())
2972 Out << "name: \"" << VI.name() << "\"";
2973 else
2974 Out << "guid: " << VI.getGUID();
2975 if (!VI.getSummaryList().empty()) {
2976 Out << ", summaries: (";
2977 FieldSeparator FS;
2978 for (auto &Summary : VI.getSummaryList()) {
2979 Out << FS;
2980 printSummary(*Summary);
2981 }
2982 Out << ")";
2983 }
2984 Out << ")";
2985 if (!VI.name().empty())
2986 Out << " ; guid = " << VI.getGUID();
2987 Out << "\n";
2988}
2989
Filipe Cabecinhas62431b12015-06-02 21:25:08 +00002990static void printMetadataIdentifier(StringRef Name,
2991 formatted_raw_ostream &Out) {
Nick Lewycky5bcbd732011-06-15 06:37:58 +00002992 if (Name.empty()) {
2993 Out << "<empty name> ";
2994 } else {
Filipe Cabecinhas1ac0d2d2015-06-02 21:25:00 +00002995 if (isalpha(static_cast<unsigned char>(Name[0])) || Name[0] == '-' ||
2996 Name[0] == '$' || Name[0] == '.' || Name[0] == '_')
Nick Lewycky5bcbd732011-06-15 06:37:58 +00002997 Out << Name[0];
2998 else
2999 Out << '\\' << hexdigit(Name[0] >> 4) << hexdigit(Name[0] & 0x0F);
3000 for (unsigned i = 1, e = Name.size(); i != e; ++i) {
3001 unsigned char C = Name[i];
Guy Benyei83c74e92013-02-12 21:21:59 +00003002 if (isalnum(static_cast<unsigned char>(C)) || C == '-' || C == '$' ||
3003 C == '.' || C == '_')
Nick Lewycky5bcbd732011-06-15 06:37:58 +00003004 Out << C;
3005 else
3006 Out << '\\' << hexdigit(C >> 4) << hexdigit(C & 0x0F);
3007 }
3008 }
Filipe Cabecinhas62431b12015-06-02 21:25:08 +00003009}
3010
3011void AssemblyWriter::printNamedMDNode(const NamedMDNode *NMD) {
3012 Out << '!';
3013 printMetadataIdentifier(NMD->getName(), Out);
Nick Lewycky5bcbd732011-06-15 06:37:58 +00003014 Out << " = !{";
Chris Lattner2c48c642009-12-31 01:54:05 +00003015 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
Filipe Cabecinhas1ac0d2d2015-06-02 21:25:00 +00003016 if (i)
3017 Out << ", ";
Reid Kleckner6d353342017-08-23 20:31:27 +00003018
3019 // Write DIExpressions inline.
3020 // FIXME: Ban DIExpressions in NamedMDNodes, they will serve no purpose.
3021 MDNode *Op = NMD->getOperand(i);
3022 if (auto *Expr = dyn_cast<DIExpression>(Op)) {
3023 writeDIExpression(Out, Expr, nullptr, nullptr, nullptr);
3024 continue;
3025 }
3026
3027 int Slot = Machine.getMetadataSlot(Op);
Dan Gohman0a54da22010-09-09 20:53:58 +00003028 if (Slot == -1)
3029 Out << "<badref>";
3030 else
3031 Out << '!' << Slot;
Chris Lattner2c48c642009-12-31 01:54:05 +00003032 }
3033 Out << "}\n";
3034}
3035
Chris Lattner3a36d2c2008-08-19 05:06:27 +00003036static void PrintVisibility(GlobalValue::VisibilityTypes Vis,
Dan Gohmane2745262009-08-12 17:23:50 +00003037 formatted_raw_ostream &Out) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00003038 switch (Vis) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00003039 case GlobalValue::DefaultVisibility: break;
3040 case GlobalValue::HiddenVisibility: Out << "hidden "; break;
3041 case GlobalValue::ProtectedVisibility: Out << "protected "; break;
3042 }
3043}
3044
Rafael Espindolae4b02312018-01-11 22:15:05 +00003045static void PrintDSOLocation(const GlobalValue &GV,
3046 formatted_raw_ostream &Out) {
Rafael Espindola9fbc0402018-01-18 02:08:23 +00003047 // GVs with local linkage or non default visibility are implicitly dso_local,
3048 // so we don't print it.
3049 bool Implicit = GV.hasLocalLinkage() ||
3050 (!GV.hasExternalWeakLinkage() && !GV.hasDefaultVisibility());
3051 if (GV.isDSOLocal() && !Implicit)
Sean Fertilec70d28b2017-10-26 15:00:26 +00003052 Out << "dso_local ";
3053}
3054
Nico Rieck7157bb72014-01-14 15:22:47 +00003055static void PrintDLLStorageClass(GlobalValue::DLLStorageClassTypes SCT,
3056 formatted_raw_ostream &Out) {
3057 switch (SCT) {
3058 case GlobalValue::DefaultStorageClass: break;
3059 case GlobalValue::DLLImportStorageClass: Out << "dllimport "; break;
3060 case GlobalValue::DLLExportStorageClass: Out << "dllexport "; break;
3061 }
3062}
3063
Hans Wennborgcbe34b42012-06-23 11:37:03 +00003064static void PrintThreadLocalModel(GlobalVariable::ThreadLocalMode TLM,
3065 formatted_raw_ostream &Out) {
3066 switch (TLM) {
3067 case GlobalVariable::NotThreadLocal:
3068 break;
3069 case GlobalVariable::GeneralDynamicTLSModel:
3070 Out << "thread_local ";
3071 break;
3072 case GlobalVariable::LocalDynamicTLSModel:
3073 Out << "thread_local(localdynamic) ";
3074 break;
3075 case GlobalVariable::InitialExecTLSModel:
3076 Out << "thread_local(initialexec) ";
3077 break;
3078 case GlobalVariable::LocalExecTLSModel:
3079 Out << "thread_local(localexec) ";
3080 break;
3081 }
3082}
3083
Peter Collingbourne96efdd62016-06-14 21:01:22 +00003084static StringRef getUnnamedAddrEncoding(GlobalVariable::UnnamedAddr UA) {
3085 switch (UA) {
3086 case GlobalVariable::UnnamedAddr::None:
3087 return "";
3088 case GlobalVariable::UnnamedAddr::Local:
3089 return "local_unnamed_addr";
3090 case GlobalVariable::UnnamedAddr::Global:
3091 return "unnamed_addr";
3092 }
Aaron Ballman2b9fa3f2016-06-15 15:27:53 +00003093 llvm_unreachable("Unknown UnnamedAddr");
Peter Collingbourne96efdd62016-06-14 21:01:22 +00003094}
3095
Rafael Espindola83a362c2015-01-06 22:55:16 +00003096static void maybePrintComdat(formatted_raw_ostream &Out,
3097 const GlobalObject &GO) {
3098 const Comdat *C = GO.getComdat();
3099 if (!C)
3100 return;
3101
3102 if (isa<GlobalVariable>(GO))
3103 Out << ',';
3104 Out << " comdat";
3105
3106 if (GO.getName() == C->getName())
3107 return;
3108
3109 Out << '(';
3110 PrintLLVMName(Out, C->getName(), ComdatPrefix);
3111 Out << ')';
3112}
3113
Chris Lattner7bfee412001-10-29 16:05:51 +00003114void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Dan Gohman5ded1422010-01-29 23:12:36 +00003115 if (GV->isMaterializable())
3116 Out << "; Materializable\n";
3117
Dan Gohmana2489d12010-07-20 23:55:01 +00003118 WriteAsOperandInternal(Out, GV, &TypePrinter, &Machine, GV->getParent());
Dan Gohman466876b2009-08-12 23:32:33 +00003119 Out << " = ";
Chris Lattner37798642001-09-18 04:01:05 +00003120
Chris Lattner1508d3f2008-08-19 05:16:28 +00003121 if (!GV->hasInitializer() && GV->hasExternalLinkage())
3122 Out << "external ";
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003123
Teresa Johnsonc80c8732018-05-25 15:15:39 +00003124 Out << getLinkageNameWithSpace(GV->getLinkage());
Rafael Espindolae4b02312018-01-11 22:15:05 +00003125 PrintDSOLocation(*GV, Out);
Chris Lattner1508d3f2008-08-19 05:16:28 +00003126 PrintVisibility(GV->getVisibility(), Out);
Nico Rieck7157bb72014-01-14 15:22:47 +00003127 PrintDLLStorageClass(GV->getDLLStorageClass(), Out);
Hans Wennborgcbe34b42012-06-23 11:37:03 +00003128 PrintThreadLocalModel(GV->getThreadLocalMode(), Out);
Peter Collingbourne96efdd62016-06-14 21:01:22 +00003129 StringRef UA = getUnnamedAddrEncoding(GV->getUnnamedAddr());
3130 if (!UA.empty())
3131 Out << UA << ' ';
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +00003132
Chris Lattnerac161bf2009-01-02 07:01:27 +00003133 if (unsigned AddressSpace = GV->getType()->getAddressSpace())
3134 Out << "addrspace(" << AddressSpace << ") ";
Michael Gottesman27e7ef32013-02-05 05:57:38 +00003135 if (GV->isExternallyInitialized()) Out << "externally_initialized ";
Misha Brukmana6619a92004-06-21 21:53:56 +00003136 Out << (GV->isConstant() ? "constant " : "global ");
Manuel Jacob5f6eaac2016-01-16 20:30:46 +00003137 TypePrinter.print(GV->getValueType(), Out);
Chris Lattner37798642001-09-18 04:01:05 +00003138
Dan Gohman81313fd2008-09-14 17:21:12 +00003139 if (GV->hasInitializer()) {
3140 Out << ' ';
Devang Patel983c6b12009-07-08 21:44:25 +00003141 writeOperand(GV->getInitializer(), false);
Dan Gohman81313fd2008-09-14 17:21:12 +00003142 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003143
Chris Lattner9380b812010-07-07 23:16:37 +00003144 if (GV->hasSection()) {
3145 Out << ", section \"";
3146 PrintEscapedString(GV->getSection(), Out);
3147 Out << '"';
3148 }
Rafael Espindola83a362c2015-01-06 22:55:16 +00003149 maybePrintComdat(Out, *GV);
Chris Lattner4b96c542005-11-12 00:10:19 +00003150 if (GV->getAlignment())
Chris Lattnerf8a974d2005-11-06 06:48:53 +00003151 Out << ", align " << GV->getAlignment();
Anton Korobeynikova97b6942007-04-25 14:27:10 +00003152
Peter Collingbournecceae7f2016-05-31 23:01:54 +00003153 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
3154 GV->getAllMetadata(MDs);
3155 printMetadataAttachments(MDs, ", ");
3156
Javed Absarf3d79042017-05-11 12:28:08 +00003157 auto Attrs = GV->getAttributes();
3158 if (Attrs.hasAttributes())
3159 Out << " #" << Machine.getAttributeGroupSlot(Attrs);
3160
Chris Lattner113f4f42002-06-25 16:13:24 +00003161 printInfoComment(*GV);
Chris Lattnerda975502001-09-10 07:58:01 +00003162}
3163
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +00003164void AssemblyWriter::printIndirectSymbol(const GlobalIndirectSymbol *GIS) {
3165 if (GIS->isMaterializable())
Dan Gohman5ded1422010-01-29 23:12:36 +00003166 Out << "; Materializable\n";
3167
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +00003168 WriteAsOperandInternal(Out, GIS, &TypePrinter, &Machine, GIS->getParent());
Rafael Espindola54fc2982015-06-17 17:53:31 +00003169 Out << " = ";
3170
Teresa Johnsonc80c8732018-05-25 15:15:39 +00003171 Out << getLinkageNameWithSpace(GIS->getLinkage());
Rafael Espindolae4b02312018-01-11 22:15:05 +00003172 PrintDSOLocation(*GIS, Out);
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +00003173 PrintVisibility(GIS->getVisibility(), Out);
3174 PrintDLLStorageClass(GIS->getDLLStorageClass(), Out);
3175 PrintThreadLocalModel(GIS->getThreadLocalMode(), Out);
Peter Collingbourne96efdd62016-06-14 21:01:22 +00003176 StringRef UA = getUnnamedAddrEncoding(GIS->getUnnamedAddr());
3177 if (!UA.empty())
3178 Out << UA << ' ';
Anton Korobeynikova97b6942007-04-25 14:27:10 +00003179
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +00003180 if (isa<GlobalAlias>(GIS))
3181 Out << "alias ";
Dmitry Polukhina1feff72016-04-07 12:32:19 +00003182 else if (isa<GlobalIFunc>(GIS))
3183 Out << "ifunc ";
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +00003184 else
Dmitry Polukhina1feff72016-04-07 12:32:19 +00003185 llvm_unreachable("Not an alias or ifunc!");
Anton Korobeynikova97b6942007-04-25 14:27:10 +00003186
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +00003187 TypePrinter.print(GIS->getValueType(), Out);
David Blaikie2f408302015-09-11 03:22:04 +00003188
3189 Out << ", ";
3190
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +00003191 const Constant *IS = GIS->getIndirectSymbol();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003192
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +00003193 if (!IS) {
3194 TypePrinter.print(GIS->getType(), Out);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003195 Out << " <<NULL ALIASEE>>";
Jay Foad92c19132011-08-01 12:48:54 +00003196 } else {
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +00003197 writeOperand(IS, !isa<ConstantExpr>(IS));
Jay Foad92c19132011-08-01 12:48:54 +00003198 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003199
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +00003200 printInfoComment(*GIS);
Chris Lattner1508d3f2008-08-19 05:16:28 +00003201 Out << '\n';
Anton Korobeynikova97b6942007-04-25 14:27:10 +00003202}
3203
David Majnemerdad0a642014-06-27 18:19:56 +00003204void AssemblyWriter::printComdat(const Comdat *C) {
3205 C->print(Out);
3206}
3207
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003208void AssemblyWriter::printTypeIdentities() {
Roman Tereshind96de6f2018-03-22 21:29:07 +00003209 if (TypePrinter.empty())
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003210 return;
Andrew Trickec4b6e72011-09-30 19:48:58 +00003211
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003212 Out << '\n';
Andrew Trickec4b6e72011-09-30 19:48:58 +00003213
Chris Lattner3243ea12009-03-01 00:03:38 +00003214 // Emit all numbered types.
Roman Tereshind96de6f2018-03-22 21:29:07 +00003215 auto &NumberedTypes = TypePrinter.getNumberedTypes();
3216 for (unsigned I = 0, E = NumberedTypes.size(); I != E; ++I) {
3217 Out << '%' << I << " = type ";
Andrew Trickec4b6e72011-09-30 19:48:58 +00003218
Chris Lattner3243ea12009-03-01 00:03:38 +00003219 // Make sure we print out at least one level of the type structure, so
3220 // that we do not get %2 = type %2
Roman Tereshind96de6f2018-03-22 21:29:07 +00003221 TypePrinter.printStructBody(NumberedTypes[I], Out);
Dan Gohman69273e62009-08-12 23:54:22 +00003222 Out << '\n';
Chris Lattner3243ea12009-03-01 00:03:38 +00003223 }
Andrew Trickec4b6e72011-09-30 19:48:58 +00003224
Roman Tereshind96de6f2018-03-22 21:29:07 +00003225 auto &NamedTypes = TypePrinter.getNamedTypes();
3226 for (unsigned I = 0, E = NamedTypes.size(); I != E; ++I) {
3227 PrintLLVMName(Out, NamedTypes[I]->getName(), LocalPrefix);
Chris Lattner1508d3f2008-08-19 05:16:28 +00003228 Out << " = type ";
Reid Spencere7e96712004-05-25 08:53:40 +00003229
3230 // Make sure we print out at least one level of the type structure, so
3231 // that we do not get %FILE = type %FILE
Roman Tereshind96de6f2018-03-22 21:29:07 +00003232 TypePrinter.printStructBody(NamedTypes[I], Out);
Chris Lattner3a36d2c2008-08-19 05:06:27 +00003233 Out << '\n';
Reid Spencere7e96712004-05-25 08:53:40 +00003234 }
Reid Spencer32af9e82007-01-06 07:24:44 +00003235}
3236
Misha Brukmanc566ca362004-03-02 00:22:19 +00003237/// printFunction - Print all aspects of a function.
Chris Lattner113f4f42002-06-25 16:13:24 +00003238void AssemblyWriter::printFunction(const Function *F) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00003239 // Print out the return type and name.
3240 Out << '\n';
Chris Lattner379a8d22003-04-16 20:28:45 +00003241
Misha Brukmana6619a92004-06-21 21:53:56 +00003242 if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00003243
Dan Gohman5ded1422010-01-29 23:12:36 +00003244 if (F->isMaterializable())
3245 Out << "; Materializable\n";
3246
Reid Klecknerb5180542017-03-21 16:57:19 +00003247 const AttributeList &Attrs = F->getAttributes();
3248 if (Attrs.hasAttributes(AttributeList::FunctionIndex)) {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00003249 AttributeSet AS = Attrs.getFnAttributes();
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +00003250 std::string AttrStr;
3251
Reid Klecknerc2cb5602017-04-12 00:38:00 +00003252 for (const Attribute &Attr : AS) {
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +00003253 if (!Attr.isStringAttribute()) {
3254 if (!AttrStr.empty()) AttrStr += ' ';
3255 AttrStr += Attr.getAsString();
3256 }
3257 }
3258
Bill Wendling847a5c32013-04-16 20:55:47 +00003259 if (!AttrStr.empty())
3260 Out << "; Function Attrs: " << AttrStr << '\n';
3261 }
3262
Peter Collingbourne21521892016-06-21 23:42:48 +00003263 Machine.incorporateFunction(F);
3264
3265 if (F->isDeclaration()) {
3266 Out << "declare";
3267 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
3268 F->getAllMetadata(MDs);
3269 printMetadataAttachments(MDs, " ");
3270 Out << ' ';
3271 } else
Reid Spencer7ce2d2a2006-12-29 20:29:48 +00003272 Out << "define ";
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003273
Teresa Johnsonc80c8732018-05-25 15:15:39 +00003274 Out << getLinkageNameWithSpace(F->getLinkage());
Rafael Espindolae4b02312018-01-11 22:15:05 +00003275 PrintDSOLocation(*F, Out);
Chris Lattner3a36d2c2008-08-19 05:06:27 +00003276 PrintVisibility(F->getVisibility(), Out);
Nico Rieck7157bb72014-01-14 15:22:47 +00003277 PrintDLLStorageClass(F->getDLLStorageClass(), Out);
Chris Lattner379a8d22003-04-16 20:28:45 +00003278
Chris Lattnerf7b6d312005-05-06 20:26:43 +00003279 // Print the calling convention.
Micah Villmow857186d2012-09-13 15:11:12 +00003280 if (F->getCallingConv() != CallingConv::C) {
3281 PrintCallingConv(F->getCallingConv(), Out);
3282 Out << " ";
Chris Lattnerf7b6d312005-05-06 20:26:43 +00003283 }
3284
Chris Lattner229907c2011-07-18 04:54:35 +00003285 FunctionType *FT = F->getFunctionType();
Reid Klecknerb5180542017-03-21 16:57:19 +00003286 if (Attrs.hasAttributes(AttributeList::ReturnIndex))
3287 Out << Attrs.getAsString(AttributeList::ReturnIndex) << ' ';
Chris Lattnere101c442009-02-28 21:26:53 +00003288 TypePrinter.print(F->getReturnType(), Out);
Chris Lattner585297e82008-08-19 05:26:17 +00003289 Out << ' ';
Dan Gohmana2489d12010-07-20 23:55:01 +00003290 WriteAsOperandInternal(Out, F, &TypePrinter, &Machine, F->getParent());
Misha Brukmana6619a92004-06-21 21:53:56 +00003291 Out << '(';
Chris Lattnerfee714f2001-09-07 16:36:04 +00003292
Chris Lattner7bfee412001-10-29 16:05:51 +00003293 // Loop over the arguments, printing them...
Justin Bognerd7d1a722015-09-27 22:38:50 +00003294 if (F->isDeclaration() && !IsForDebug) {
Justin Bogner58e08232015-09-02 17:54:41 +00003295 // We're only interested in the type here - don't print argument names.
3296 for (unsigned I = 0, E = FT->getNumParams(); I != E; ++I) {
Chris Lattner82738fe2007-04-18 00:57:22 +00003297 // Insert commas as we go... the first arg doesn't get a comma
Justin Bogner58e08232015-09-02 17:54:41 +00003298 if (I)
3299 Out << ", ";
3300 // Output type...
3301 TypePrinter.print(FT->getParamType(I), Out);
3302
Reid Kleckner6652a522017-04-28 18:37:16 +00003303 AttributeSet ArgAttrs = Attrs.getParamAttributes(I);
3304 if (ArgAttrs.hasAttributes())
3305 Out << ' ' << ArgAttrs.getAsString();
Chris Lattner82738fe2007-04-18 00:57:22 +00003306 }
3307 } else {
Justin Bogner58e08232015-09-02 17:54:41 +00003308 // The arguments are meaningful here, print them in detail.
Justin Bogner58e08232015-09-02 17:54:41 +00003309 for (const Argument &Arg : F->args()) {
Chris Lattner82738fe2007-04-18 00:57:22 +00003310 // Insert commas as we go... the first arg doesn't get a comma
Reid Kleckner6652a522017-04-28 18:37:16 +00003311 if (Arg.getArgNo() != 0)
Justin Bogner58e08232015-09-02 17:54:41 +00003312 Out << ", ";
Reid Kleckner6652a522017-04-28 18:37:16 +00003313 printArgument(&Arg, Attrs.getParamAttributes(Arg.getArgNo()));
Chris Lattner82738fe2007-04-18 00:57:22 +00003314 }
Reid Spencer8c4914c2006-12-31 05:24:50 +00003315 }
Chris Lattnerfee714f2001-09-07 16:36:04 +00003316
3317 // Finish printing arguments...
Chris Lattner113f4f42002-06-25 16:13:24 +00003318 if (FT->isVarArg()) {
Misha Brukmana6619a92004-06-21 21:53:56 +00003319 if (FT->getNumParams()) Out << ", ";
3320 Out << "..."; // Output varargs portion of signature!
Chris Lattnerfee714f2001-09-07 16:36:04 +00003321 }
Misha Brukmana6619a92004-06-21 21:53:56 +00003322 Out << ')';
Peter Collingbourne96efdd62016-06-14 21:01:22 +00003323 StringRef UA = getUnnamedAddrEncoding(F->getUnnamedAddr());
3324 if (!UA.empty())
3325 Out << ' ' << UA;
Reid Klecknerb5180542017-03-21 16:57:19 +00003326 if (Attrs.hasAttributes(AttributeList::FunctionIndex))
Bill Wendling04945972013-04-29 23:48:06 +00003327 Out << " #" << Machine.getAttributeGroupSlot(Attrs.getFnAttributes());
Chris Lattner9380b812010-07-07 23:16:37 +00003328 if (F->hasSection()) {
3329 Out << " section \"";
3330 PrintEscapedString(F->getSection(), Out);
3331 Out << '"';
3332 }
Rafael Espindola83a362c2015-01-06 22:55:16 +00003333 maybePrintComdat(Out, *F);
Chris Lattnerf8a974d2005-11-06 06:48:53 +00003334 if (F->getAlignment())
3335 Out << " align " << F->getAlignment();
Gordon Henriksend930f912008-08-17 18:44:35 +00003336 if (F->hasGC())
3337 Out << " gc \"" << F->getGC() << '"';
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00003338 if (F->hasPrefixData()) {
3339 Out << " prefix ";
3340 writeOperand(F->getPrefixData(), true);
3341 }
Peter Collingbourne51d2de72014-12-03 02:08:38 +00003342 if (F->hasPrologueData()) {
3343 Out << " prologue ";
3344 writeOperand(F->getPrologueData(), true);
3345 }
David Majnemer7fddecc2015-06-17 20:52:32 +00003346 if (F->hasPersonalityFn()) {
3347 Out << " personality ";
3348 writeOperand(F->getPersonalityFn(), /*PrintType=*/true);
3349 }
Peter Collingbourne51d2de72014-12-03 02:08:38 +00003350
Reid Spencer5301e7c2007-01-30 20:08:39 +00003351 if (F->isDeclaration()) {
Chris Lattnerfb483622010-09-02 22:52:10 +00003352 Out << '\n';
Chris Lattnerb2f02e52002-05-06 03:00:40 +00003353 } else {
Peter Collingbourne21521892016-06-21 23:42:48 +00003354 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
3355 F->getAllMetadata(MDs);
3356 printMetadataAttachments(MDs, " ");
3357
Chris Lattnerfb483622010-09-02 22:52:10 +00003358 Out << " {";
3359 // Output all of the function's basic blocks.
Benjamin Krameraf28e7d2016-06-26 14:10:56 +00003360 for (const BasicBlock &BB : *F)
3361 printBasicBlock(&BB);
Chris Lattnerfee714f2001-09-07 16:36:04 +00003362
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +00003363 // Output the function's use-lists.
3364 printUseLists(F);
3365
Misha Brukmana6619a92004-06-21 21:53:56 +00003366 Out << "}\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +00003367 }
3368
Reid Spencer16f2f7f2004-05-26 07:18:52 +00003369 Machine.purgeFunction();
Chris Lattner2f7c9632001-06-06 20:29:01 +00003370}
3371
Misha Brukmanc566ca362004-03-02 00:22:19 +00003372/// printArgument - This member is called for every argument that is passed into
3373/// the function. Simply print it out
Reid Kleckner6652a522017-04-28 18:37:16 +00003374void AssemblyWriter::printArgument(const Argument *Arg, AttributeSet Attrs) {
Chris Lattner2f7c9632001-06-06 20:29:01 +00003375 // Output type...
Chris Lattnere101c442009-02-28 21:26:53 +00003376 TypePrinter.print(Arg->getType(), Out);
Misha Brukmanb1c93172005-04-21 23:48:37 +00003377
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00003378 // Output parameter attributes list
Reid Kleckner6652a522017-04-28 18:37:16 +00003379 if (Attrs.hasAttributes())
3380 Out << ' ' << Attrs.getAsString();
Reid Spencer8c4914c2006-12-31 05:24:50 +00003381
Chris Lattner2f7c9632001-06-06 20:29:01 +00003382 // Output name, if available...
Chris Lattner033935d2008-08-17 04:40:13 +00003383 if (Arg->hasName()) {
3384 Out << ' ';
3385 PrintLLVMName(Out, Arg);
3386 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00003387}
3388
Misha Brukmanc566ca362004-03-02 00:22:19 +00003389/// printBasicBlock - This member is called for each basic block in a method.
Chris Lattner7bfee412001-10-29 16:05:51 +00003390void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Nick Lewycky4d43d3c2008-04-25 16:53:59 +00003391 if (BB->hasName()) { // Print out the label if it exists...
Chris Lattner033935d2008-08-17 04:40:13 +00003392 Out << "\n";
Daniel Dunbare03eecb2009-07-25 23:55:21 +00003393 PrintLLVMName(Out, BB->getName(), LabelPrefix);
Chris Lattner033935d2008-08-17 04:40:13 +00003394 Out << ':';
Nick Lewycky4d43d3c2008-04-25 16:53:59 +00003395 } else if (!BB->use_empty()) { // Don't print block # of no uses...
Bill Wendling35a9c3c2011-04-10 23:18:04 +00003396 Out << "\n; <label>:";
Chris Lattner5e043322007-01-11 03:54:27 +00003397 int Slot = Machine.getLocalSlot(BB);
Chris Lattner757ee0b2004-06-09 19:41:19 +00003398 if (Slot != -1)
Evgeniy Stepanove257f0f2016-01-27 21:53:08 +00003399 Out << Slot << ":";
Chris Lattner757ee0b2004-06-09 19:41:19 +00003400 else
Misha Brukmana6619a92004-06-21 21:53:56 +00003401 Out << "<badref>";
Chris Lattner58185f22002-10-02 19:38:55 +00003402 }
Chris Lattner2447ef52003-11-20 00:09:43 +00003403
Craig Topperc6207612014-04-09 06:08:46 +00003404 if (!BB->getParent()) {
Chris Lattneree97b8b2009-08-17 15:48:08 +00003405 Out.PadToColumn(50);
Dan Gohmane2745262009-08-12 17:23:50 +00003406 Out << "; Error: Block without parent!";
3407 } else if (BB != &BB->getParent()->getEntryBlock()) { // Not the entry block?
Chris Lattnerfb483622010-09-02 22:52:10 +00003408 // Output predecessors for the block.
Chris Lattneree97b8b2009-08-17 15:48:08 +00003409 Out.PadToColumn(50);
Dan Gohmane2745262009-08-12 17:23:50 +00003410 Out << ";";
Gabor Greif6c6b2fd2010-03-25 23:25:28 +00003411 const_pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003412
Chris Lattnerff834c02008-04-22 02:45:44 +00003413 if (PI == PE) {
3414 Out << " No predecessors!";
3415 } else {
Dan Gohman81313fd2008-09-14 17:21:12 +00003416 Out << " preds = ";
Chris Lattnerff834c02008-04-22 02:45:44 +00003417 writeOperand(*PI, false);
3418 for (++PI; PI != PE; ++PI) {
Dan Gohman81313fd2008-09-14 17:21:12 +00003419 Out << ", ";
Chris Lattner78e2e8b2006-12-06 06:24:27 +00003420 writeOperand(*PI, false);
Chris Lattner00211f12003-11-16 22:59:57 +00003421 }
Chris Lattner58185f22002-10-02 19:38:55 +00003422 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00003423 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00003424
Chris Lattnerff834c02008-04-22 02:45:44 +00003425 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +00003426
Misha Brukmana6619a92004-06-21 21:53:56 +00003427 if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00003428
Chris Lattnerfee714f2001-09-07 16:36:04 +00003429 // Output all of the instructions in the basic block...
Benjamin Krameraf28e7d2016-06-26 14:10:56 +00003430 for (const Instruction &I : *BB) {
3431 printInstructionLine(I);
Dan Gohmana4f709e2009-07-13 18:27:59 +00003432 }
Chris Lattner96cdd272004-03-08 18:51:45 +00003433
Misha Brukmana6619a92004-06-21 21:53:56 +00003434 if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, Out);
Chris Lattner2f7c9632001-06-06 20:29:01 +00003435}
3436
Daniel Maleaded9f932013-05-08 20:38:31 +00003437/// printInstructionLine - Print an instruction and a newline character.
3438void AssemblyWriter::printInstructionLine(const Instruction &I) {
3439 printInstruction(I);
3440 Out << '\n';
3441}
3442
Igor Laevsky2aa8caf2015-05-05 13:20:42 +00003443/// printGCRelocateComment - print comment after call to the gc.relocate
3444/// intrinsic indicating base and derived pointer names.
Manuel Jacob83eefa62016-01-05 04:03:00 +00003445void AssemblyWriter::printGCRelocateComment(const GCRelocateInst &Relocate) {
Igor Laevsky2aa8caf2015-05-05 13:20:42 +00003446 Out << " ; (";
Manuel Jacob83eefa62016-01-05 04:03:00 +00003447 writeOperand(Relocate.getBasePtr(), false);
Igor Laevsky2aa8caf2015-05-05 13:20:42 +00003448 Out << ", ";
Manuel Jacob83eefa62016-01-05 04:03:00 +00003449 writeOperand(Relocate.getDerivedPtr(), false);
Igor Laevsky2aa8caf2015-05-05 13:20:42 +00003450 Out << ")";
3451}
3452
Misha Brukmanc566ca362004-03-02 00:22:19 +00003453/// printInfoComment - Print a little comment after the instruction indicating
3454/// which slot it occupies.
Chris Lattner113f4f42002-06-25 16:13:24 +00003455void AssemblyWriter::printInfoComment(const Value &V) {
Manuel Jacob83eefa62016-01-05 04:03:00 +00003456 if (const auto *Relocate = dyn_cast<GCRelocateInst>(&V))
3457 printGCRelocateComment(*Relocate);
Igor Laevsky2aa8caf2015-05-05 13:20:42 +00003458
Bill Wendling847a5c32013-04-16 20:55:47 +00003459 if (AnnotationWriter)
Dan Gohmane34890f2010-02-10 20:41:46 +00003460 AnnotationWriter->printInfoComment(V, Out);
Chris Lattner862e3382001-10-13 06:42:36 +00003461}
3462
Reid Spencere7141c82006-08-28 01:02:49 +00003463// This member is called for each Instruction in a function..
Chris Lattner113f4f42002-06-25 16:13:24 +00003464void AssemblyWriter::printInstruction(const Instruction &I) {
Misha Brukmana6619a92004-06-21 21:53:56 +00003465 if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00003466
Dan Gohman466876b2009-08-12 23:32:33 +00003467 // Print out indentation for an instruction.
Dan Gohmanb4583b12009-08-13 01:41:52 +00003468 Out << " ";
Chris Lattner2f7c9632001-06-06 20:29:01 +00003469
3470 // Print out name if it exists...
Chris Lattner033935d2008-08-17 04:40:13 +00003471 if (I.hasName()) {
3472 PrintLLVMName(Out, &I);
3473 Out << " = ";
Chris Lattner031560c2009-12-29 07:25:48 +00003474 } else if (!I.getType()->isVoidTy()) {
Chris Lattnerb25e5ea2008-08-29 17:19:30 +00003475 // Print out the def slot taken.
3476 int SlotNum = Machine.getLocalSlot(&I);
3477 if (SlotNum == -1)
3478 Out << "<badref> = ";
3479 else
3480 Out << '%' << SlotNum << " = ";
Chris Lattner033935d2008-08-17 04:40:13 +00003481 }
Andrew Trickec4b6e72011-09-30 19:48:58 +00003482
Reid Kleckner5772b772014-04-24 20:14:34 +00003483 if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
3484 if (CI->isMustTailCall())
3485 Out << "musttail ";
3486 else if (CI->isTailCall())
3487 Out << "tail ";
Akira Hatanaka5cfcce122015-11-06 23:55:38 +00003488 else if (CI->isNoTailCall())
3489 Out << "notail ";
Reid Kleckner5772b772014-04-24 20:14:34 +00003490 }
Chris Lattner504f9242003-09-08 17:45:59 +00003491
Chris Lattner2f7c9632001-06-06 20:29:01 +00003492 // Print out the opcode...
Misha Brukmana6619a92004-06-21 21:53:56 +00003493 Out << I.getOpcodeName();
Chris Lattner2f7c9632001-06-06 20:29:01 +00003494
Eli Friedman02e737b2011-08-12 22:50:01 +00003495 // If this is an atomic load or store, print out the atomic marker.
3496 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isAtomic()) ||
3497 (isa<StoreInst>(I) && cast<StoreInst>(I).isAtomic()))
3498 Out << " atomic";
3499
Tim Northover420a2162014-06-13 14:24:07 +00003500 if (isa<AtomicCmpXchgInst>(I) && cast<AtomicCmpXchgInst>(I).isWeak())
3501 Out << " weak";
3502
Eli Friedman02e737b2011-08-12 22:50:01 +00003503 // If this is a volatile operation, print out the volatile marker.
3504 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) ||
3505 (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile()) ||
3506 (isa<AtomicCmpXchgInst>(I) && cast<AtomicCmpXchgInst>(I).isVolatile()) ||
3507 (isa<AtomicRMWInst>(I) && cast<AtomicRMWInst>(I).isVolatile()))
3508 Out << " volatile";
3509
Dan Gohman9c7f8082009-07-27 16:11:46 +00003510 // Print out optimization information.
3511 WriteOptimizationInfo(Out, &I);
3512
Reid Spencer45e52392006-12-03 06:27:29 +00003513 // Print out the compare instruction predicates
Nate Begemand2195702008-05-12 19:01:56 +00003514 if (const CmpInst *CI = dyn_cast<CmpInst>(&I))
Tim Northoverde3aea0412016-08-17 20:25:25 +00003515 Out << ' ' << CmpInst::getPredicateName(CI->getPredicate());
Reid Spencer45e52392006-12-03 06:27:29 +00003516
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003517 // Print out the atomicrmw operation
3518 if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(&I))
3519 writeAtomicRMWOperation(Out, RMWI->getOperation());
3520
Chris Lattner2f7c9632001-06-06 20:29:01 +00003521 // Print out the type of the operands...
Craig Topperc6207612014-04-09 06:08:46 +00003522 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : nullptr;
Chris Lattner2f7c9632001-06-06 20:29:01 +00003523
3524 // Special case conditional branches to swizzle the condition out to the front
Gabor Greifcab008f2009-02-09 15:45:06 +00003525 if (isa<BranchInst>(I) && cast<BranchInst>(I).isConditional()) {
David Blaikieb78e9e52013-02-11 01:16:51 +00003526 const BranchInst &BI(cast<BranchInst>(I));
Dan Gohman81313fd2008-09-14 17:21:12 +00003527 Out << ' ';
Gabor Greifcab008f2009-02-09 15:45:06 +00003528 writeOperand(BI.getCondition(), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00003529 Out << ", ";
Gabor Greifcab008f2009-02-09 15:45:06 +00003530 writeOperand(BI.getSuccessor(0), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00003531 Out << ", ";
Gabor Greifcab008f2009-02-09 15:45:06 +00003532 writeOperand(BI.getSuccessor(1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00003533
Chris Lattner8d48df22002-04-13 18:34:38 +00003534 } else if (isa<SwitchInst>(I)) {
David Blaikieb78e9e52013-02-11 01:16:51 +00003535 const SwitchInst& SI(cast<SwitchInst>(I));
Chris Lattner3ed871f2009-10-27 19:13:16 +00003536 // Special case switch instruction to get formatting nice and correct.
Dan Gohman81313fd2008-09-14 17:21:12 +00003537 Out << ' ';
Eli Friedman95031ed2011-09-29 20:21:17 +00003538 writeOperand(SI.getCondition(), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00003539 Out << ", ";
Eli Friedman95031ed2011-09-29 20:21:17 +00003540 writeOperand(SI.getDefaultDest(), true);
Chris Lattner82ff9232008-08-23 22:52:27 +00003541 Out << " [";
Chandler Carruth927d8e62017-04-12 07:27:28 +00003542 for (auto Case : SI.cases()) {
Dan Gohmanb4583b12009-08-13 01:41:52 +00003543 Out << "\n ";
Chandler Carruth927d8e62017-04-12 07:27:28 +00003544 writeOperand(Case.getCaseValue(), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00003545 Out << ", ";
Chandler Carruth927d8e62017-04-12 07:27:28 +00003546 writeOperand(Case.getCaseSuccessor(), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00003547 }
Dan Gohmanb4583b12009-08-13 01:41:52 +00003548 Out << "\n ]";
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003549 } else if (isa<IndirectBrInst>(I)) {
3550 // Special case indirectbr instruction to get formatting nice and correct.
Chris Lattner3ed871f2009-10-27 19:13:16 +00003551 Out << ' ';
3552 writeOperand(Operand, true);
Dan Gohman43c57402009-10-30 02:01:10 +00003553 Out << ", [";
Andrew Trickec4b6e72011-09-30 19:48:58 +00003554
Chris Lattner3ed871f2009-10-27 19:13:16 +00003555 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) {
3556 if (i != 1)
3557 Out << ", ";
3558 writeOperand(I.getOperand(i), true);
3559 }
3560 Out << ']';
Jay Foad372ad642011-06-20 14:18:48 +00003561 } else if (const PHINode *PN = dyn_cast<PHINode>(&I)) {
Misha Brukmana6619a92004-06-21 21:53:56 +00003562 Out << ' ';
Chris Lattnere101c442009-02-28 21:26:53 +00003563 TypePrinter.print(I.getType(), Out);
Misha Brukmana6619a92004-06-21 21:53:56 +00003564 Out << ' ';
Chris Lattner2f7c9632001-06-06 20:29:01 +00003565
Jay Foad372ad642011-06-20 14:18:48 +00003566 for (unsigned op = 0, Eop = PN->getNumIncomingValues(); op < Eop; ++op) {
Misha Brukmana6619a92004-06-21 21:53:56 +00003567 if (op) Out << ", ";
Dan Gohman81313fd2008-09-14 17:21:12 +00003568 Out << "[ ";
Jay Foad372ad642011-06-20 14:18:48 +00003569 writeOperand(PN->getIncomingValue(op), false); Out << ", ";
3570 writeOperand(PN->getIncomingBlock(op), false); Out << " ]";
Chris Lattner931ef3b2001-06-11 15:04:20 +00003571 }
Dan Gohmana76f0f72008-05-31 19:12:39 +00003572 } else if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(&I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00003573 Out << ' ';
Dan Gohmana76f0f72008-05-31 19:12:39 +00003574 writeOperand(I.getOperand(0), true);
3575 for (const unsigned *i = EVI->idx_begin(), *e = EVI->idx_end(); i != e; ++i)
3576 Out << ", " << *i;
3577 } else if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(&I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00003578 Out << ' ';
3579 writeOperand(I.getOperand(0), true); Out << ", ";
Dan Gohmana76f0f72008-05-31 19:12:39 +00003580 writeOperand(I.getOperand(1), true);
3581 for (const unsigned *i = IVI->idx_begin(), *e = IVI->idx_end(); i != e; ++i)
3582 Out << ", " << *i;
Bill Wendlingfae14752011-08-12 20:24:12 +00003583 } else if (const LandingPadInst *LPI = dyn_cast<LandingPadInst>(&I)) {
3584 Out << ' ';
3585 TypePrinter.print(I.getType(), Out);
David Majnemer7fddecc2015-06-17 20:52:32 +00003586 if (LPI->isCleanup() || LPI->getNumClauses() != 0)
3587 Out << '\n';
Bill Wendlingfae14752011-08-12 20:24:12 +00003588
3589 if (LPI->isCleanup())
3590 Out << " cleanup";
3591
3592 for (unsigned i = 0, e = LPI->getNumClauses(); i != e; ++i) {
3593 if (i != 0 || LPI->isCleanup()) Out << "\n";
3594 if (LPI->isCatch(i))
3595 Out << " catch ";
3596 else
3597 Out << " filter ";
3598
3599 writeOperand(LPI->getClause(i), true);
3600 }
David Majnemer8a1c45d2015-12-12 05:38:55 +00003601 } else if (const auto *CatchSwitch = dyn_cast<CatchSwitchInst>(&I)) {
3602 Out << " within ";
3603 writeOperand(CatchSwitch->getParentPad(), /*PrintType=*/false);
David Majnemer654e1302015-07-31 17:58:14 +00003604 Out << " [";
David Majnemer8a1c45d2015-12-12 05:38:55 +00003605 unsigned Op = 0;
3606 for (const BasicBlock *PadBB : CatchSwitch->handlers()) {
3607 if (Op > 0)
3608 Out << ", ";
3609 writeOperand(PadBB, /*PrintType=*/true);
3610 ++Op;
3611 }
3612 Out << "] unwind ";
3613 if (const BasicBlock *UnwindDest = CatchSwitch->getUnwindDest())
3614 writeOperand(UnwindDest, /*PrintType=*/true);
3615 else
3616 Out << "to caller";
3617 } else if (const auto *FPI = dyn_cast<FuncletPadInst>(&I)) {
3618 Out << " within ";
3619 writeOperand(FPI->getParentPad(), /*PrintType=*/false);
3620 Out << " [";
3621 for (unsigned Op = 0, NumOps = FPI->getNumArgOperands(); Op < NumOps;
David Majnemer654e1302015-07-31 17:58:14 +00003622 ++Op) {
3623 if (Op > 0)
3624 Out << ", ";
David Majnemer8a1c45d2015-12-12 05:38:55 +00003625 writeOperand(FPI->getArgOperand(Op), /*PrintType=*/true);
David Majnemer654e1302015-07-31 17:58:14 +00003626 }
David Majnemer8a1c45d2015-12-12 05:38:55 +00003627 Out << ']';
Devang Patel59643e52008-02-23 00:35:18 +00003628 } else if (isa<ReturnInst>(I) && !Operand) {
3629 Out << " void";
David Majnemer0bc0eef2015-08-15 02:46:08 +00003630 } else if (const auto *CRI = dyn_cast<CatchReturnInst>(&I)) {
David Majnemer8a1c45d2015-12-12 05:38:55 +00003631 Out << " from ";
3632 writeOperand(CRI->getOperand(0), /*PrintType=*/false);
David Majnemer0bc0eef2015-08-15 02:46:08 +00003633
3634 Out << " to ";
David Majnemer8a1c45d2015-12-12 05:38:55 +00003635 writeOperand(CRI->getOperand(1), /*PrintType=*/true);
David Majnemer654e1302015-07-31 17:58:14 +00003636 } else if (const auto *CRI = dyn_cast<CleanupReturnInst>(&I)) {
David Majnemer8a1c45d2015-12-12 05:38:55 +00003637 Out << " from ";
3638 writeOperand(CRI->getOperand(0), /*PrintType=*/false);
David Majnemer654e1302015-07-31 17:58:14 +00003639
3640 Out << " unwind ";
3641 if (CRI->hasUnwindDest())
David Majnemer8a1c45d2015-12-12 05:38:55 +00003642 writeOperand(CRI->getOperand(1), /*PrintType=*/true);
Joseph Tremoulet9ce71f72015-09-03 09:09:43 +00003643 else
3644 Out << "to caller";
Chris Lattnerf7b6d312005-05-06 20:26:43 +00003645 } else if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
3646 // Print the calling convention being used.
Micah Villmow857186d2012-09-13 15:11:12 +00003647 if (CI->getCallingConv() != CallingConv::C) {
3648 Out << " ";
3649 PrintCallingConv(CI->getCallingConv(), Out);
Chris Lattnerf7b6d312005-05-06 20:26:43 +00003650 }
3651
Gabor Greifc9a92512010-06-23 13:09:06 +00003652 Operand = CI->getCalledValue();
Ahmed Bougachaaa9fe532016-12-21 23:26:13 +00003653 FunctionType *FTy = CI->getFunctionType();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003654 Type *RetTy = FTy->getReturnType();
Reid Klecknerb5180542017-03-21 16:57:19 +00003655 const AttributeList &PAL = CI->getAttributes();
Chris Lattner2f2d9472001-11-06 21:28:12 +00003656
Reid Klecknerb5180542017-03-21 16:57:19 +00003657 if (PAL.hasAttributes(AttributeList::ReturnIndex))
3658 Out << ' ' << PAL.getAsString(AttributeList::ReturnIndex);
Devang Patel221fe422008-09-29 20:49:50 +00003659
Chris Lattner463d6a52003-08-05 15:34:45 +00003660 // If possible, print out the short form of the call instruction. We can
Chris Lattner6915f8f2002-04-07 22:49:37 +00003661 // only do this if the first argument is a pointer to a nonvararg function,
Chris Lattner463d6a52003-08-05 15:34:45 +00003662 // and if the return type is not a pointer to a function.
Chris Lattner2f2d9472001-11-06 21:28:12 +00003663 //
Dan Gohman81313fd2008-09-14 17:21:12 +00003664 Out << ' ';
David Blaikie23af6482015-04-16 23:24:18 +00003665 TypePrinter.print(FTy->isVarArg() ? FTy : RetTy, Out);
3666 Out << ' ';
3667 writeOperand(Operand, false);
Misha Brukmana6619a92004-06-21 21:53:56 +00003668 Out << '(';
Gabor Greifc9a92512010-06-23 13:09:06 +00003669 for (unsigned op = 0, Eop = CI->getNumArgOperands(); op < Eop; ++op) {
3670 if (op > 0)
Dan Gohman81313fd2008-09-14 17:21:12 +00003671 Out << ", ";
Reid Kleckner6652a522017-04-28 18:37:16 +00003672 writeParamOperand(CI->getArgOperand(op), PAL.getParamAttributes(op));
Chris Lattner2f7c9632001-06-06 20:29:01 +00003673 }
Reid Kleckner83498642014-08-26 00:33:28 +00003674
3675 // Emit an ellipsis if this is a musttail call in a vararg function. This
3676 // is only to aid readability, musttail calls forward varargs by default.
3677 if (CI->isMustTailCall() && CI->getParent() &&
3678 CI->getParent()->getParent() &&
3679 CI->getParent()->getParent()->isVarArg())
3680 Out << ", ...";
3681
Dan Gohman81313fd2008-09-14 17:21:12 +00003682 Out << ')';
Reid Klecknerb5180542017-03-21 16:57:19 +00003683 if (PAL.hasAttributes(AttributeList::FunctionIndex))
Bill Wendlinga0323742013-02-22 09:09:42 +00003684 Out << " #" << Machine.getAttributeGroupSlot(PAL.getFnAttributes());
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00003685
3686 writeOperandBundles(CI);
Chris Lattner113f4f42002-06-25 16:13:24 +00003687 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
Gabor Greifa2fbc0a2010-03-24 13:21:49 +00003688 Operand = II->getCalledValue();
Ahmed Bougachaaa9fe532016-12-21 23:26:13 +00003689 FunctionType *FTy = II->getFunctionType();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003690 Type *RetTy = FTy->getReturnType();
Reid Klecknerb5180542017-03-21 16:57:19 +00003691 const AttributeList &PAL = II->getAttributes();
Chris Lattner463d6a52003-08-05 15:34:45 +00003692
Chris Lattnerf7b6d312005-05-06 20:26:43 +00003693 // Print the calling convention being used.
Micah Villmow857186d2012-09-13 15:11:12 +00003694 if (II->getCallingConv() != CallingConv::C) {
3695 Out << " ";
3696 PrintCallingConv(II->getCallingConv(), Out);
Chris Lattnerf7b6d312005-05-06 20:26:43 +00003697 }
3698
Reid Klecknerb5180542017-03-21 16:57:19 +00003699 if (PAL.hasAttributes(AttributeList::ReturnIndex))
3700 Out << ' ' << PAL.getAsString(AttributeList::ReturnIndex);
Devang Patel221fe422008-09-29 20:49:50 +00003701
Chris Lattner463d6a52003-08-05 15:34:45 +00003702 // If possible, print out the short form of the invoke instruction. We can
3703 // only do this if the first argument is a pointer to a nonvararg function,
3704 // and if the return type is not a pointer to a function.
3705 //
Dan Gohmanc7e00ba2008-10-15 18:02:08 +00003706 Out << ' ';
David Blaikie445e3fb2015-04-24 19:32:54 +00003707 TypePrinter.print(FTy->isVarArg() ? FTy : RetTy, Out);
3708 Out << ' ';
3709 writeOperand(Operand, false);
Misha Brukmana6619a92004-06-21 21:53:56 +00003710 Out << '(';
Gabor Greifc9a92512010-06-23 13:09:06 +00003711 for (unsigned op = 0, Eop = II->getNumArgOperands(); op < Eop; ++op) {
Gabor Greifa2fbc0a2010-03-24 13:21:49 +00003712 if (op)
Dan Gohman81313fd2008-09-14 17:21:12 +00003713 Out << ", ";
Reid Kleckner6652a522017-04-28 18:37:16 +00003714 writeParamOperand(II->getArgOperand(op), PAL.getParamAttributes(op));
Chris Lattner862e3382001-10-13 06:42:36 +00003715 }
3716
Dan Gohman81313fd2008-09-14 17:21:12 +00003717 Out << ')';
Reid Klecknerb5180542017-03-21 16:57:19 +00003718 if (PAL.hasAttributes(AttributeList::FunctionIndex))
Bill Wendlinga0323742013-02-22 09:09:42 +00003719 Out << " #" << Machine.getAttributeGroupSlot(PAL.getFnAttributes());
Devang Patela05633e2008-09-26 22:53:05 +00003720
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00003721 writeOperandBundles(II);
3722
Dan Gohmanb4583b12009-08-13 01:41:52 +00003723 Out << "\n to ";
Chris Lattner862e3382001-10-13 06:42:36 +00003724 writeOperand(II->getNormalDest(), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00003725 Out << " unwind ";
Chris Lattnerfae8ab32004-02-08 21:44:31 +00003726 writeOperand(II->getUnwindDest(), true);
Victor Hernandez8acf2952009-10-23 21:09:37 +00003727 } else if (const AllocaInst *AI = dyn_cast<AllocaInst>(&I)) {
Misha Brukmana6619a92004-06-21 21:53:56 +00003728 Out << ' ';
Reid Kleckner20844032014-01-25 01:24:06 +00003729 if (AI->isUsedWithInAlloca())
David Majnemerc4ab61c2014-03-09 06:41:58 +00003730 Out << "inalloca ";
Manman Ren9bfd0d02016-04-01 21:41:15 +00003731 if (AI->isSwiftError())
3732 Out << "swifterror ";
David Majnemerc4ab61c2014-03-09 06:41:58 +00003733 TypePrinter.print(AI->getAllocatedType(), Out);
Duncan P. N. Exon Smith720762e2015-03-13 19:30:44 +00003734
3735 // Explicitly write the array size if the code is broken, if it's an array
3736 // allocation, or if the type is not canonical for scalar allocations. The
3737 // latter case prevents the type from mutating when round-tripping through
3738 // assembly.
3739 if (!AI->getArraySize() || AI->isArrayAllocation() ||
3740 !AI->getArraySize()->getType()->isIntegerTy(32)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00003741 Out << ", ";
Chris Lattner8d48df22002-04-13 18:34:38 +00003742 writeOperand(AI->getArraySize(), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00003743 }
Nate Begeman848622f2005-11-05 09:21:28 +00003744 if (AI->getAlignment()) {
Chris Lattner7aeee3a2005-11-05 21:20:34 +00003745 Out << ", align " << AI->getAlignment();
Nate Begeman848622f2005-11-05 09:21:28 +00003746 }
Matt Arsenault3c1fc762017-04-10 22:27:50 +00003747
3748 unsigned AddrSpace = AI->getType()->getAddressSpace();
3749 if (AddrSpace != 0) {
3750 Out << ", addrspace(" << AddrSpace << ')';
3751 }
Chris Lattner862e3382001-10-13 06:42:36 +00003752 } else if (isa<CastInst>(I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00003753 if (Operand) {
3754 Out << ' ';
3755 writeOperand(Operand, true); // Work with broken code
3756 }
Misha Brukmana6619a92004-06-21 21:53:56 +00003757 Out << " to ";
Chris Lattnere101c442009-02-28 21:26:53 +00003758 TypePrinter.print(I.getType(), Out);
Chris Lattner5b337482003-10-18 05:57:43 +00003759 } else if (isa<VAArgInst>(I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00003760 if (Operand) {
3761 Out << ' ';
3762 writeOperand(Operand, true); // Work with broken code
3763 }
Misha Brukmana6619a92004-06-21 21:53:56 +00003764 Out << ", ";
Chris Lattnere101c442009-02-28 21:26:53 +00003765 TypePrinter.print(I.getType(), Out);
3766 } else if (Operand) { // Print the normal way.
David Blaikiea79ac142015-02-27 21:17:42 +00003767 if (const auto *GEP = dyn_cast<GetElementPtrInst>(&I)) {
David Blaikie79e6c742015-02-27 19:29:02 +00003768 Out << ' ';
3769 TypePrinter.print(GEP->getSourceElementType(), Out);
3770 Out << ',';
David Blaikiea79ac142015-02-27 21:17:42 +00003771 } else if (const auto *LI = dyn_cast<LoadInst>(&I)) {
3772 Out << ' ';
3773 TypePrinter.print(LI->getType(), Out);
Benjamin Kramer257ed692015-03-02 15:24:41 +00003774 Out << ',';
David Blaikie79e6c742015-02-27 19:29:02 +00003775 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00003776
Misha Brukmanb1c93172005-04-21 23:48:37 +00003777 // PrintAllTypes - Instructions who have operands of all the same type
Chris Lattner2f7c9632001-06-06 20:29:01 +00003778 // omit the type from all but the first operand. If the instruction has
3779 // different type operands (for example br), then they are all printed.
3780 bool PrintAllTypes = false;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003781 Type *TheType = Operand->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +00003782
Reid Spencer0cdd04f2007-02-02 13:54:55 +00003783 // Select, Store and ShuffleVector always print all types.
Devang Patelce556d92008-03-04 22:05:14 +00003784 if (isa<SelectInst>(I) || isa<StoreInst>(I) || isa<ShuffleVectorInst>(I)
3785 || isa<ReturnInst>(I)) {
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00003786 PrintAllTypes = true;
3787 } else {
3788 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
3789 Operand = I.getOperand(i);
Nuno Lopes04fe2f02009-01-15 18:40:57 +00003790 // note that Operand shouldn't be null, but the test helps make dump()
3791 // more tolerant of malformed IR
Nuno Lopes0971e772009-01-14 17:51:41 +00003792 if (Operand && Operand->getType() != TheType) {
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00003793 PrintAllTypes = true; // We have differing types! Print them all!
3794 break;
3795 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00003796 }
3797 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00003798
Chris Lattner7bfee412001-10-29 16:05:51 +00003799 if (!PrintAllTypes) {
Misha Brukmana6619a92004-06-21 21:53:56 +00003800 Out << ' ';
Chris Lattnere101c442009-02-28 21:26:53 +00003801 TypePrinter.print(TheType, Out);
Chris Lattner7bfee412001-10-29 16:05:51 +00003802 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00003803
Dan Gohman81313fd2008-09-14 17:21:12 +00003804 Out << ' ';
Chris Lattner113f4f42002-06-25 16:13:24 +00003805 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
Dan Gohman81313fd2008-09-14 17:21:12 +00003806 if (i) Out << ", ";
Chris Lattner113f4f42002-06-25 16:13:24 +00003807 writeOperand(I.getOperand(i), PrintAllTypes);
Chris Lattner2f7c9632001-06-06 20:29:01 +00003808 }
3809 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003810
Eli Friedman59b66882011-08-09 23:02:53 +00003811 // Print atomic ordering/alignment for memory operations
3812 if (const LoadInst *LI = dyn_cast<LoadInst>(&I)) {
3813 if (LI->isAtomic())
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003814 writeAtomic(LI->getContext(), LI->getOrdering(), LI->getSyncScopeID());
Eli Friedman59b66882011-08-09 23:02:53 +00003815 if (LI->getAlignment())
3816 Out << ", align " << LI->getAlignment();
3817 } else if (const StoreInst *SI = dyn_cast<StoreInst>(&I)) {
3818 if (SI->isAtomic())
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003819 writeAtomic(SI->getContext(), SI->getOrdering(), SI->getSyncScopeID());
Eli Friedman59b66882011-08-09 23:02:53 +00003820 if (SI->getAlignment())
3821 Out << ", align " << SI->getAlignment();
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003822 } else if (const AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(&I)) {
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003823 writeAtomicCmpXchg(CXI->getContext(), CXI->getSuccessOrdering(),
3824 CXI->getFailureOrdering(), CXI->getSyncScopeID());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003825 } else if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(&I)) {
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003826 writeAtomic(RMWI->getContext(), RMWI->getOrdering(),
3827 RMWI->getSyncScopeID());
Eli Friedmanfee02c62011-07-25 23:16:38 +00003828 } else if (const FenceInst *FI = dyn_cast<FenceInst>(&I)) {
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003829 writeAtomic(FI->getContext(), FI->getOrdering(), FI->getSyncScopeID());
Christopher Lamb84485702007-04-22 19:24:39 +00003830 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00003831
Chris Lattnerc9558df2009-12-28 20:10:43 +00003832 // Print Metadata info.
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00003833 SmallVector<std::pair<unsigned, MDNode *>, 4> InstMD;
Nick Lewyckyba8ec9a2010-02-25 06:53:04 +00003834 I.getAllMetadata(InstMD);
Duncan P. N. Exon Smithd3e4c2a2015-04-24 21:06:21 +00003835 printMetadataAttachments(InstMD, ", ");
Duncan P. N. Exon Smith86979272015-04-24 20:59:52 +00003836
3837 // Print a nice comment.
Chris Lattner862e3382001-10-13 06:42:36 +00003838 printInfoComment(I);
Chris Lattner2f7c9632001-06-06 20:29:01 +00003839}
3840
Duncan P. N. Exon Smith86979272015-04-24 20:59:52 +00003841void AssemblyWriter::printMetadataAttachments(
Duncan P. N. Exon Smithd3e4c2a2015-04-24 21:06:21 +00003842 const SmallVectorImpl<std::pair<unsigned, MDNode *>> &MDs,
3843 StringRef Separator) {
Duncan P. N. Exon Smith86979272015-04-24 20:59:52 +00003844 if (MDs.empty())
3845 return;
3846
Duncan P. N. Exon Smithe30f10e2015-04-24 21:03:05 +00003847 if (MDNames.empty())
Mehdi Aminib9b50aa2016-01-07 20:14:30 +00003848 MDs[0].second->getContext().getMDKindNames(MDNames);
Duncan P. N. Exon Smithe30f10e2015-04-24 21:03:05 +00003849
Duncan P. N. Exon Smith86979272015-04-24 20:59:52 +00003850 for (const auto &I : MDs) {
3851 unsigned Kind = I.first;
Duncan P. N. Exon Smithd3e4c2a2015-04-24 21:06:21 +00003852 Out << Separator;
Filipe Cabecinhas62431b12015-06-02 21:25:08 +00003853 if (Kind < MDNames.size()) {
3854 Out << "!";
3855 printMetadataIdentifier(MDNames[Kind], Out);
3856 } else
Duncan P. N. Exon Smithd3e4c2a2015-04-24 21:06:21 +00003857 Out << "!<unknown kind #" << Kind << ">";
Duncan P. N. Exon Smith86979272015-04-24 20:59:52 +00003858 Out << ' ';
3859 WriteAsOperandInternal(Out, I.second, &TypePrinter, &Machine, TheModule);
3860 }
3861}
3862
Daniel Maleaded9f932013-05-08 20:38:31 +00003863void AssemblyWriter::writeMDNode(unsigned Slot, const MDNode *Node) {
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +00003864 Out << '!' << Slot << " = ";
Daniel Maleaded9f932013-05-08 20:38:31 +00003865 printMDNodeBody(Node);
Duncan P. N. Exon Smith738889f2015-02-25 22:46:38 +00003866 Out << "\n";
Daniel Maleaded9f932013-05-08 20:38:31 +00003867}
3868
Chris Lattnerbddea6a2009-12-31 02:13:35 +00003869void AssemblyWriter::writeAllMDNodes() {
3870 SmallVector<const MDNode *, 16> Nodes;
Chris Lattnercf4a76e2009-12-31 02:20:11 +00003871 Nodes.resize(Machine.mdn_size());
3872 for (SlotTracker::mdn_iterator I = Machine.mdn_begin(), E = Machine.mdn_end();
3873 I != E; ++I)
Chris Lattnerbddea6a2009-12-31 02:13:35 +00003874 Nodes[I->second] = cast<MDNode>(I->first);
Andrew Trickec4b6e72011-09-30 19:48:58 +00003875
Chris Lattnerbddea6a2009-12-31 02:13:35 +00003876 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Daniel Maleaded9f932013-05-08 20:38:31 +00003877 writeMDNode(i, Nodes[i]);
Chris Lattnerbddea6a2009-12-31 02:13:35 +00003878 }
3879}
3880
3881void AssemblyWriter::printMDNodeBody(const MDNode *Node) {
Dan Gohmana2489d12010-07-20 23:55:01 +00003882 WriteMDNodeBodyInternal(Out, Node, &TypePrinter, &Machine, TheModule);
Chris Lattnerbddea6a2009-12-31 02:13:35 +00003883}
Chris Lattner2f7c9632001-06-06 20:29:01 +00003884
Bill Wendling829b4782013-02-11 08:43:33 +00003885void AssemblyWriter::writeAllAttributeGroups() {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00003886 std::vector<std::pair<AttributeSet, unsigned>> asVec;
Bill Wendling829b4782013-02-11 08:43:33 +00003887 asVec.resize(Machine.as_size());
3888
3889 for (SlotTracker::as_iterator I = Machine.as_begin(), E = Machine.as_end();
3890 I != E; ++I)
3891 asVec[I->second] = *I;
3892
Benjamin Krameraf28e7d2016-06-26 14:10:56 +00003893 for (const auto &I : asVec)
3894 Out << "attributes #" << I.second << " = { "
Reid Klecknerc2cb5602017-04-12 00:38:00 +00003895 << I.first.getAsString(true) << " }\n";
Bill Wendling829b4782013-02-11 08:43:33 +00003896}
3897
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +00003898void AssemblyWriter::printUseListOrder(const UseListOrder &Order) {
3899 bool IsInFunction = Machine.getFunction();
3900 if (IsInFunction)
3901 Out << " ";
3902
3903 Out << "uselistorder";
3904 if (const BasicBlock *BB =
3905 IsInFunction ? nullptr : dyn_cast<BasicBlock>(Order.V)) {
3906 Out << "_bb ";
3907 writeOperand(BB->getParent(), false);
3908 Out << ", ";
3909 writeOperand(BB, false);
3910 } else {
3911 Out << " ";
3912 writeOperand(Order.V, true);
3913 }
3914 Out << ", { ";
3915
3916 assert(Order.Shuffle.size() >= 2 && "Shuffle too small");
3917 Out << Order.Shuffle[0];
3918 for (unsigned I = 1, E = Order.Shuffle.size(); I != E; ++I)
3919 Out << ", " << Order.Shuffle[I];
3920 Out << " }\n";
3921}
3922
3923void AssemblyWriter::printUseLists(const Function *F) {
3924 auto hasMore =
3925 [&]() { return !UseListOrders.empty() && UseListOrders.back().F == F; };
3926 if (!hasMore())
3927 // Nothing to do.
3928 return;
3929
3930 Out << "\n; uselistorder directives\n";
3931 while (hasMore()) {
3932 printUseListOrder(UseListOrders.back());
3933 UseListOrders.pop_back();
3934 }
3935}
3936
Chris Lattner2f7c9632001-06-06 20:29:01 +00003937//===----------------------------------------------------------------------===//
3938// External Interface declarations
3939//===----------------------------------------------------------------------===//
3940
George Burgess IVe1100f52016-02-02 22:46:49 +00003941void Function::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW,
3942 bool ShouldPreserveUseListOrder,
3943 bool IsForDebug) const {
3944 SlotTracker SlotTable(this->getParent());
3945 formatted_raw_ostream OS(ROS);
3946 AssemblyWriter W(OS, SlotTable, this->getParent(), AAW,
3947 IsForDebug,
3948 ShouldPreserveUseListOrder);
3949 W.printFunction(this);
3950}
3951
Duncan P. N. Exon Smithc4f0a322015-04-15 02:12:41 +00003952void Module::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW,
Justin Bognerd7d1a722015-09-27 22:38:50 +00003953 bool ShouldPreserveUseListOrder, bool IsForDebug) const {
Chris Lattnere36fd8a2008-08-19 04:26:57 +00003954 SlotTracker SlotTable(this);
Dan Gohmane2745262009-08-12 17:23:50 +00003955 formatted_raw_ostream OS(ROS);
Justin Bognerd7d1a722015-09-27 22:38:50 +00003956 AssemblyWriter W(OS, SlotTable, this, AAW, IsForDebug,
3957 ShouldPreserveUseListOrder);
Chris Lattnerefb5e392009-12-31 02:23:35 +00003958 W.printModule(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00003959}
3960
Justin Bognerd7d1a722015-09-27 22:38:50 +00003961void NamedMDNode::print(raw_ostream &ROS, bool IsForDebug) const {
Dan Gohman2637cc12010-07-21 23:38:33 +00003962 SlotTracker SlotTable(getParent());
3963 formatted_raw_ostream OS(ROS);
Justin Bognerd7d1a722015-09-27 22:38:50 +00003964 AssemblyWriter W(OS, SlotTable, getParent(), nullptr, IsForDebug);
Dan Gohman2637cc12010-07-21 23:38:33 +00003965 W.printNamedMDNode(this);
3966}
3967
Duncan P. N. Exon Smith0ecff952016-04-20 17:27:44 +00003968void NamedMDNode::print(raw_ostream &ROS, ModuleSlotTracker &MST,
3969 bool IsForDebug) const {
3970 Optional<SlotTracker> LocalST;
3971 SlotTracker *SlotTable;
3972 if (auto *ST = MST.getMachine())
3973 SlotTable = ST;
3974 else {
3975 LocalST.emplace(getParent());
3976 SlotTable = &*LocalST;
3977 }
3978
3979 formatted_raw_ostream OS(ROS);
3980 AssemblyWriter W(OS, *SlotTable, getParent(), nullptr, IsForDebug);
3981 W.printNamedMDNode(this);
3982}
3983
Justin Bognerd7d1a722015-09-27 22:38:50 +00003984void Comdat::print(raw_ostream &ROS, bool /*IsForDebug*/) const {
David Majnemerdad0a642014-06-27 18:19:56 +00003985 PrintLLVMName(ROS, getName(), ComdatPrefix);
3986 ROS << " = comdat ";
3987
3988 switch (getSelectionKind()) {
3989 case Comdat::Any:
3990 ROS << "any";
3991 break;
3992 case Comdat::ExactMatch:
3993 ROS << "exactmatch";
3994 break;
3995 case Comdat::Largest:
3996 ROS << "largest";
3997 break;
3998 case Comdat::NoDuplicates:
3999 ROS << "noduplicates";
4000 break;
4001 case Comdat::SameSize:
4002 ROS << "samesize";
4003 break;
4004 }
4005
4006 ROS << '\n';
4007}
4008
Quentin Colombet774b1ef2016-03-07 22:32:42 +00004009void Type::print(raw_ostream &OS, bool /*IsForDebug*/, bool NoDetails) const {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004010 TypePrinting TP;
4011 TP.print(const_cast<Type*>(this), OS);
Andrew Trickec4b6e72011-09-30 19:48:58 +00004012
Quentin Colombet774b1ef2016-03-07 22:32:42 +00004013 if (NoDetails)
4014 return;
4015
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004016 // If the type is a named struct type, print the body as well.
4017 if (StructType *STy = dyn_cast<StructType>(const_cast<Type*>(this)))
Chris Lattner01beceb2011-08-12 18:07:07 +00004018 if (!STy->isLiteral()) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004019 OS << " = type ";
4020 TP.printStructBody(STy, OS);
4021 }
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00004022}
4023
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +00004024static bool isReferencingMDNode(const Instruction &I) {
4025 if (const auto *CI = dyn_cast<CallInst>(&I))
4026 if (Function *F = CI->getCalledFunction())
Justin Lebar9cbc3012016-07-28 23:58:15 +00004027 if (F->isIntrinsic())
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +00004028 for (auto &Op : I.operands())
4029 if (auto *V = dyn_cast_or_null<MetadataAsValue>(Op))
4030 if (isa<MDNode>(V->getMetadata()))
4031 return true;
4032 return false;
4033}
4034
Justin Bognerd7d1a722015-09-27 22:38:50 +00004035void Value::print(raw_ostream &ROS, bool IsForDebug) const {
Duncan P. N. Exon Smith1f8a99a2015-06-27 00:38:26 +00004036 bool ShouldInitializeAllMetadata = false;
4037 if (auto *I = dyn_cast<Instruction>(this))
4038 ShouldInitializeAllMetadata = isReferencingMDNode(*I);
4039 else if (isa<Function>(this) || isa<MetadataAsValue>(this))
4040 ShouldInitializeAllMetadata = true;
4041
4042 ModuleSlotTracker MST(getModuleFromVal(this), ShouldInitializeAllMetadata);
Justin Bognerd7d1a722015-09-27 22:38:50 +00004043 print(ROS, MST, IsForDebug);
Duncan P. N. Exon Smith1f8a99a2015-06-27 00:38:26 +00004044}
4045
Justin Bognerd7d1a722015-09-27 22:38:50 +00004046void Value::print(raw_ostream &ROS, ModuleSlotTracker &MST,
4047 bool IsForDebug) const {
Dan Gohman12dad632009-08-12 20:56:03 +00004048 formatted_raw_ostream OS(ROS);
Duncan P. N. Exon Smith1f8a99a2015-06-27 00:38:26 +00004049 SlotTracker EmptySlotTable(static_cast<const Module *>(nullptr));
4050 SlotTracker &SlotTable =
4051 MST.getMachine() ? *MST.getMachine() : EmptySlotTable;
4052 auto incorporateFunction = [&](const Function *F) {
4053 if (F)
4054 MST.incorporateFunction(*F);
4055 };
4056
Chris Lattner0c19df42008-08-23 22:23:09 +00004057 if (const Instruction *I = dyn_cast<Instruction>(this)) {
Duncan P. N. Exon Smith1f8a99a2015-06-27 00:38:26 +00004058 incorporateFunction(I->getParent() ? I->getParent()->getParent() : nullptr);
Justin Bognerd7d1a722015-09-27 22:38:50 +00004059 AssemblyWriter W(OS, SlotTable, getModuleFromVal(I), nullptr, IsForDebug);
Chris Lattnerefb5e392009-12-31 02:23:35 +00004060 W.printInstruction(*I);
Chris Lattner0c19df42008-08-23 22:23:09 +00004061 } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(this)) {
Duncan P. N. Exon Smith1f8a99a2015-06-27 00:38:26 +00004062 incorporateFunction(BB->getParent());
Justin Bognerd7d1a722015-09-27 22:38:50 +00004063 AssemblyWriter W(OS, SlotTable, getModuleFromVal(BB), nullptr, IsForDebug);
Chris Lattnerefb5e392009-12-31 02:23:35 +00004064 W.printBasicBlock(BB);
Chris Lattner0c19df42008-08-23 22:23:09 +00004065 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(this)) {
Justin Bognerd7d1a722015-09-27 22:38:50 +00004066 AssemblyWriter W(OS, SlotTable, GV->getParent(), nullptr, IsForDebug);
Chris Lattnerefb5e392009-12-31 02:23:35 +00004067 if (const GlobalVariable *V = dyn_cast<GlobalVariable>(GV))
4068 W.printGlobal(V);
4069 else if (const Function *F = dyn_cast<Function>(GV))
4070 W.printFunction(F);
4071 else
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +00004072 W.printIndirectSymbol(cast<GlobalIndirectSymbol>(GV));
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00004073 } else if (const MetadataAsValue *V = dyn_cast<MetadataAsValue>(this)) {
Duncan P. N. Exon Smith1f8a99a2015-06-27 00:38:26 +00004074 V->getMetadata()->print(ROS, MST, getModuleFromVal(V));
Chris Lattner0c19df42008-08-23 22:23:09 +00004075 } else if (const Constant *C = dyn_cast<Constant>(this)) {
Chris Lattner92c5c122009-02-28 23:20:19 +00004076 TypePrinting TypePrinter;
Chris Lattnere101c442009-02-28 21:26:53 +00004077 TypePrinter.print(C->getType(), OS);
Chris Lattner2ee62d22009-02-28 21:11:05 +00004078 OS << ' ';
Duncan P. N. Exon Smith1f8a99a2015-06-27 00:38:26 +00004079 WriteConstantInternal(OS, C, TypePrinter, MST.getMachine(), nullptr);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00004080 } else if (isa<InlineAsm>(this) || isa<Argument>(this)) {
Duncan P. N. Exon Smith1f8a99a2015-06-27 00:38:26 +00004081 this->printAsOperand(OS, /* PrintType */ true, MST);
Chris Lattner0c19df42008-08-23 22:23:09 +00004082 } else {
Nick Lewyckyad1b3d12014-05-09 00:49:03 +00004083 llvm_unreachable("Unknown value to print out!");
Chris Lattner0c19df42008-08-23 22:23:09 +00004084 }
4085}
4086
Duncan P. N. Exon Smith326921542015-06-26 22:04:20 +00004087/// Print without a type, skipping the TypePrinting object.
4088///
Benjamin Kramerdf005cb2015-08-08 18:27:36 +00004089/// \return \c true iff printing was successful.
Duncan P. N. Exon Smith326921542015-06-26 22:04:20 +00004090static bool printWithoutType(const Value &V, raw_ostream &O,
4091 SlotTracker *Machine, const Module *M) {
4092 if (V.hasName() || isa<GlobalValue>(V) ||
4093 (!isa<Constant>(V) && !isa<MetadataAsValue>(V))) {
4094 WriteAsOperandInternal(O, &V, nullptr, Machine, M);
4095 return true;
Chandler Carruthd48cdbf2014-01-09 02:29:41 +00004096 }
Duncan P. N. Exon Smith326921542015-06-26 22:04:20 +00004097 return false;
4098}
Chandler Carruthd48cdbf2014-01-09 02:29:41 +00004099
Duncan P. N. Exon Smith326921542015-06-26 22:04:20 +00004100static void printAsOperandImpl(const Value &V, raw_ostream &O, bool PrintType,
4101 ModuleSlotTracker &MST) {
Roman Tereshind96de6f2018-03-22 21:29:07 +00004102 TypePrinting TypePrinter(MST.getModule());
Chandler Carruthd48cdbf2014-01-09 02:29:41 +00004103 if (PrintType) {
Duncan P. N. Exon Smith326921542015-06-26 22:04:20 +00004104 TypePrinter.print(V.getType(), O);
Chandler Carruthd48cdbf2014-01-09 02:29:41 +00004105 O << ' ';
4106 }
4107
Duncan P. N. Exon Smith326921542015-06-26 22:04:20 +00004108 WriteAsOperandInternal(O, &V, &TypePrinter, MST.getMachine(),
4109 MST.getModule());
4110}
4111
4112void Value::printAsOperand(raw_ostream &O, bool PrintType,
4113 const Module *M) const {
4114 if (!M)
4115 M = getModuleFromVal(this);
4116
4117 if (!PrintType)
4118 if (printWithoutType(*this, O, nullptr, M))
4119 return;
4120
4121 SlotTracker Machine(
4122 M, /* ShouldInitializeAllMetadata */ isa<MetadataAsValue>(this));
4123 ModuleSlotTracker MST(Machine, M);
4124 printAsOperandImpl(*this, O, PrintType, MST);
4125}
4126
4127void Value::printAsOperand(raw_ostream &O, bool PrintType,
4128 ModuleSlotTracker &MST) const {
4129 if (!PrintType)
4130 if (printWithoutType(*this, O, MST.getMachine(), MST.getModule()))
4131 return;
4132
4133 printAsOperandImpl(*this, O, PrintType, MST);
Chandler Carruthd48cdbf2014-01-09 02:29:41 +00004134}
4135
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +00004136static void printMetadataImpl(raw_ostream &ROS, const Metadata &MD,
Duncan P. N. Exon Smith6529ed42015-06-26 22:28:47 +00004137 ModuleSlotTracker &MST, const Module *M,
4138 bool OnlyAsOperand) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00004139 formatted_raw_ostream OS(ROS);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00004140
Roman Tereshind96de6f2018-03-22 21:29:07 +00004141 TypePrinting TypePrinter(M);
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +00004142
Duncan P. N. Exon Smith6529ed42015-06-26 22:28:47 +00004143 WriteAsOperandInternal(OS, &MD, &TypePrinter, MST.getMachine(), M,
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00004144 /* FromValue */ true);
Duncan P. N. Exon Smith6529ed42015-06-26 22:28:47 +00004145
4146 auto *N = dyn_cast<MDNode>(&MD);
Reid Klecknerb95b4272017-08-30 20:40:36 +00004147 if (OnlyAsOperand || !N || isa<DIExpression>(MD))
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +00004148 return;
4149
4150 OS << " = ";
Duncan P. N. Exon Smith6529ed42015-06-26 22:28:47 +00004151 WriteMDNodeBodyInternal(OS, N, &TypePrinter, MST.getMachine(), M);
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +00004152}
4153
4154void Metadata::printAsOperand(raw_ostream &OS, const Module *M) const {
Duncan P. N. Exon Smith6529ed42015-06-26 22:28:47 +00004155 ModuleSlotTracker MST(M, isa<MDNode>(this));
4156 printMetadataImpl(OS, *this, MST, M, /* OnlyAsOperand */ true);
4157}
4158
4159void Metadata::printAsOperand(raw_ostream &OS, ModuleSlotTracker &MST,
4160 const Module *M) const {
4161 printMetadataImpl(OS, *this, MST, M, /* OnlyAsOperand */ true);
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +00004162}
4163
Justin Bognerd7d1a722015-09-27 22:38:50 +00004164void Metadata::print(raw_ostream &OS, const Module *M,
4165 bool /*IsForDebug*/) const {
Duncan P. N. Exon Smith6529ed42015-06-26 22:28:47 +00004166 ModuleSlotTracker MST(M, isa<MDNode>(this));
4167 printMetadataImpl(OS, *this, MST, M, /* OnlyAsOperand */ false);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00004168}
4169
Duncan P. N. Exon Smith1f8a99a2015-06-27 00:38:26 +00004170void Metadata::print(raw_ostream &OS, ModuleSlotTracker &MST,
Justin Bognerd7d1a722015-09-27 22:38:50 +00004171 const Module *M, bool /*IsForDebug*/) const {
Duncan P. N. Exon Smith1f8a99a2015-06-27 00:38:26 +00004172 printMetadataImpl(OS, *this, MST, M, /* OnlyAsOperand */ false);
4173}
4174
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00004175void ModuleSummaryIndex::print(raw_ostream &ROS, bool IsForDebug) const {
4176 SlotTracker SlotTable(this);
4177 formatted_raw_ostream OS(ROS);
4178 AssemblyWriter W(OS, SlotTable, this, IsForDebug);
4179 W.printModuleSummaryIndex();
4180}
4181
Aaron Ballman615eb472017-10-15 14:32:27 +00004182#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Chris Lattnerdab942552008-08-25 17:03:15 +00004183// Value::dump - allow easy printing of Values from the debugger.
Duncan P. N. Exon Smith89c1eaa2015-02-25 22:08:21 +00004184LLVM_DUMP_METHOD
Justin Bognerd7d1a722015-09-27 22:38:50 +00004185void Value::dump() const { print(dbgs(), /*IsForDebug=*/true); dbgs() << '\n'; }
Reid Spencer52641832004-05-25 18:14:38 +00004186
Chris Lattnerdab942552008-08-25 17:03:15 +00004187// Type::dump - allow easy printing of Types from the debugger.
Duncan P. N. Exon Smith89c1eaa2015-02-25 22:08:21 +00004188LLVM_DUMP_METHOD
Justin Bognerd7d1a722015-09-27 22:38:50 +00004189void Type::dump() const { print(dbgs(), /*IsForDebug=*/true); dbgs() << '\n'; }
Chris Lattner24025262009-02-28 21:05:51 +00004190
Chris Lattnerdab942552008-08-25 17:03:15 +00004191// Module::dump() - Allow printing of Modules from the debugger.
Duncan P. N. Exon Smith89c1eaa2015-02-25 22:08:21 +00004192LLVM_DUMP_METHOD
Justin Bognerd7d1a722015-09-27 22:38:50 +00004193void Module::dump() const {
4194 print(dbgs(), nullptr,
4195 /*ShouldPreserveUseListOrder=*/false, /*IsForDebug=*/true);
4196}
Bill Wendling3681d112011-12-09 23:18:34 +00004197
Adrian Prantl5f8f34e42018-05-01 15:54:18 +00004198// Allow printing of Comdats from the debugger.
Duncan P. N. Exon Smith89c1eaa2015-02-25 22:08:21 +00004199LLVM_DUMP_METHOD
Justin Bognerd7d1a722015-09-27 22:38:50 +00004200void Comdat::dump() const { print(dbgs(), /*IsForDebug=*/true); }
David Majnemerdad0a642014-06-27 18:19:56 +00004201
Bill Wendling3681d112011-12-09 23:18:34 +00004202// NamedMDNode::dump() - Allow printing of NamedMDNodes from the debugger.
Duncan P. N. Exon Smith89c1eaa2015-02-25 22:08:21 +00004203LLVM_DUMP_METHOD
Justin Bognerd7d1a722015-09-27 22:38:50 +00004204void NamedMDNode::dump() const { print(dbgs(), /*IsForDebug=*/true); }
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00004205
Duncan P. N. Exon Smith89c1eaa2015-02-25 22:08:21 +00004206LLVM_DUMP_METHOD
Duncan P. N. Exon Smitha66dc8f2015-03-15 06:53:32 +00004207void Metadata::dump() const { dump(nullptr); }
4208
4209LLVM_DUMP_METHOD
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +00004210void Metadata::dump(const Module *M) const {
Justin Bognerd7d1a722015-09-27 22:38:50 +00004211 print(dbgs(), M, /*IsForDebug=*/true);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00004212 dbgs() << '\n';
4213}
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00004214
4215// Allow printing of ModuleSummaryIndex from the debugger.
4216LLVM_DUMP_METHOD
4217void ModuleSummaryIndex::dump() const { print(dbgs(), /*IsForDebug=*/true); }
Matthias Braun8c209aa2017-01-28 02:02:38 +00004218#endif