blob: 1a8e3002b3b31f5362721d7268f87b0325ce20a6 [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;
Sander de Smalen4dbc5122018-09-12 08:54:06 +0000366 case CallingConv::AArch64_VectorCall: Out << "aarch64_vector_pcs"; break;
Bill Wendling90bc19c2013-02-20 07:21:42 +0000367 case CallingConv::MSP430_INTR: Out << "msp430_intrcc"; break;
Dylan McKay4fd0d4a2016-03-03 10:08:02 +0000368 case CallingConv::AVR_INTR: Out << "avr_intrcc "; break;
369 case CallingConv::AVR_SIGNAL: Out << "avr_signalcc "; break;
Bill Wendling90bc19c2013-02-20 07:21:42 +0000370 case CallingConv::PTX_Kernel: Out << "ptx_kernel"; break;
371 case CallingConv::PTX_Device: Out << "ptx_device"; break;
Charles Davise8f297c2013-07-12 06:02:35 +0000372 case CallingConv::X86_64_SysV: Out << "x86_64_sysvcc"; break;
Martin Storsjo2f24e932017-07-17 20:05:19 +0000373 case CallingConv::Win64: Out << "win64cc"; break;
Michael Kupersteine31b4862013-12-15 10:01:20 +0000374 case CallingConv::SPIR_FUNC: Out << "spir_func"; break;
375 case CallingConv::SPIR_KERNEL: Out << "spir_kernel"; break;
Manman Renf8bdd882016-04-05 22:41:47 +0000376 case CallingConv::Swift: Out << "swiftcc"; break;
Amjad Aboud60b5e1b2015-12-21 14:07:14 +0000377 case CallingConv::X86_INTR: Out << "x86_intrcc"; break;
Maksim Panchenkocce239c2015-09-29 22:09:16 +0000378 case CallingConv::HHVM: Out << "hhvmcc"; break;
379 case CallingConv::HHVM_C: Out << "hhvm_ccc"; break;
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000380 case CallingConv::AMDGPU_VS: Out << "amdgpu_vs"; break;
Tim Renoufef1ae8f2017-09-29 09:51:22 +0000381 case CallingConv::AMDGPU_LS: Out << "amdgpu_ls"; break;
Marek Olsaka302a7362017-05-02 15:41:10 +0000382 case CallingConv::AMDGPU_HS: Out << "amdgpu_hs"; break;
Tim Renoufef1ae8f2017-09-29 09:51:22 +0000383 case CallingConv::AMDGPU_ES: Out << "amdgpu_es"; break;
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000384 case CallingConv::AMDGPU_GS: Out << "amdgpu_gs"; break;
385 case CallingConv::AMDGPU_PS: Out << "amdgpu_ps"; break;
386 case CallingConv::AMDGPU_CS: Out << "amdgpu_cs"; break;
Nikolay Haustov1f7732a2016-05-06 09:07:29 +0000387 case CallingConv::AMDGPU_KERNEL: Out << "amdgpu_kernel"; break;
Micah Villmow857186d2012-09-13 15:11:12 +0000388 }
389}
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000390
Chris Lattner3eee99c2008-08-19 04:36:02 +0000391enum PrefixType {
392 GlobalPrefix,
David Majnemerdad0a642014-06-27 18:19:56 +0000393 ComdatPrefix,
Chris Lattner3eee99c2008-08-19 04:36:02 +0000394 LabelPrefix,
Daniel Dunbar389529a2008-10-14 23:28:09 +0000395 LocalPrefix,
396 NoPrefix
Chris Lattner3eee99c2008-08-19 04:36:02 +0000397};
398
Alex Lorenz2b7f2652015-07-21 16:50:35 +0000399void llvm::printLLVMNameWithoutPrefix(raw_ostream &OS, StringRef Name) {
Jay Foadf7adb322011-04-24 14:30:00 +0000400 assert(!Name.empty() && "Cannot get empty name!");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000401
Chris Lattner3eee99c2008-08-19 04:36:02 +0000402 // Scan the name to see if it needs quotes first.
Guy Benyei83c74e92013-02-12 21:21:59 +0000403 bool NeedsQuotes = isdigit(static_cast<unsigned char>(Name[0]));
Chris Lattner3eee99c2008-08-19 04:36:02 +0000404 if (!NeedsQuotes) {
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000405 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
Aaron Ballmaned9b0a92012-07-16 16:18:18 +0000406 // By making this unsigned, the value passed in to isalnum will always be
407 // in the range 0-255. This is important when building with MSVC because
408 // its implementation will assert. This situation can arise when dealing
409 // with UTF-8 multibyte characters.
410 unsigned char C = Name[i];
Guy Benyei83c74e92013-02-12 21:21:59 +0000411 if (!isalnum(static_cast<unsigned char>(C)) && C != '-' && C != '.' &&
412 C != '_') {
Chris Lattner3eee99c2008-08-19 04:36:02 +0000413 NeedsQuotes = true;
414 break;
415 }
416 }
417 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000418
Chris Lattner3eee99c2008-08-19 04:36:02 +0000419 // If we didn't need any quotes, just write out the name in one blast.
420 if (!NeedsQuotes) {
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000421 OS << Name;
Chris Lattner3eee99c2008-08-19 04:36:02 +0000422 return;
423 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000424
Chris Lattner3eee99c2008-08-19 04:36:02 +0000425 // Okay, we need quotes. Output the quotes and escape any scary characters as
426 // needed.
427 OS << '"';
Jonas Devlieghere745918ff2018-05-31 17:01:42 +0000428 printEscapedString(Name, OS);
Chris Lattner3eee99c2008-08-19 04:36:02 +0000429 OS << '"';
430}
431
Alex Lorenz2b7f2652015-07-21 16:50:35 +0000432/// Turn the specified name into an 'LLVM name', which is either prefixed with %
433/// (if the string only contains simple characters) or is surrounded with ""'s
434/// (if it has special chars in it). Print it out.
435static void PrintLLVMName(raw_ostream &OS, StringRef Name, PrefixType Prefix) {
436 switch (Prefix) {
437 case NoPrefix:
438 break;
439 case GlobalPrefix:
440 OS << '@';
441 break;
442 case ComdatPrefix:
443 OS << '$';
444 break;
445 case LabelPrefix:
446 break;
447 case LocalPrefix:
448 OS << '%';
449 break;
450 }
451 printLLVMNameWithoutPrefix(OS, Name);
452}
453
454/// Turn the specified name into an 'LLVM name', which is either prefixed with %
455/// (if the string only contains simple characters) or is surrounded with ""'s
456/// (if it has special chars in it). Print it out.
Dan Gohman12dad632009-08-12 20:56:03 +0000457static void PrintLLVMName(raw_ostream &OS, const Value *V) {
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000458 PrintLLVMName(OS, V->getName(),
Chris Lattner3eee99c2008-08-19 04:36:02 +0000459 isa<GlobalValue>(V) ? GlobalPrefix : LocalPrefix);
460}
461
Benjamin Kramerba05a782015-03-17 19:53:41 +0000462namespace {
Benjamin Kramerba05a782015-03-17 19:53:41 +0000463
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000464class TypePrinting {
465public:
Roman Tereshind96de6f2018-03-22 21:29:07 +0000466 TypePrinting(const Module *M = nullptr) : DeferredM(M) {}
Benjamin Kramerba05a782015-03-17 19:53:41 +0000467
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000468 TypePrinting(const TypePrinting &) = delete;
469 TypePrinting &operator=(const TypePrinting &) = delete;
Benjamin Kramerba05a782015-03-17 19:53:41 +0000470
Roman Tereshind96de6f2018-03-22 21:29:07 +0000471 /// The named types that are used by the current module.
472 TypeFinder &getNamedTypes();
473
474 /// The numbered types, number to type mapping.
475 std::vector<StructType *> &getNumberedTypes();
476
477 bool empty();
Benjamin Kramerba05a782015-03-17 19:53:41 +0000478
479 void print(Type *Ty, raw_ostream &OS);
480
481 void printStructBody(StructType *Ty, raw_ostream &OS);
Roman Tereshind96de6f2018-03-22 21:29:07 +0000482
483private:
484 void incorporateTypes();
485
486 /// A module to process lazily when needed. Set to nullptr as soon as used.
487 const Module *DeferredM;
488
489 TypeFinder NamedTypes;
490
491 // The numbered types, along with their value.
492 DenseMap<StructType *, unsigned> Type2Number;
493
494 std::vector<StructType *> NumberedTypes;
Benjamin Kramerba05a782015-03-17 19:53:41 +0000495};
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000496
497} // end anonymous namespace
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000498
Roman Tereshind96de6f2018-03-22 21:29:07 +0000499TypeFinder &TypePrinting::getNamedTypes() {
500 incorporateTypes();
501 return NamedTypes;
502}
503
504std::vector<StructType *> &TypePrinting::getNumberedTypes() {
505 incorporateTypes();
506
507 // We know all the numbers that each type is used and we know that it is a
508 // dense assignment. Convert the map to an index table, if it's not done
509 // already (judging from the sizes):
510 if (NumberedTypes.size() == Type2Number.size())
511 return NumberedTypes;
512
513 NumberedTypes.resize(Type2Number.size());
514 for (const auto &P : Type2Number) {
515 assert(P.second < NumberedTypes.size() && "Didn't get a dense numbering?");
516 assert(!NumberedTypes[P.second] && "Didn't get a unique numbering?");
517 NumberedTypes[P.second] = P.first;
518 }
519 return NumberedTypes;
520}
521
522bool TypePrinting::empty() {
523 incorporateTypes();
524 return NamedTypes.empty() && Type2Number.empty();
525}
526
527void TypePrinting::incorporateTypes() {
528 if (!DeferredM)
529 return;
530
531 NamedTypes.run(*DeferredM, false);
532 DeferredM = nullptr;
Andrew Trickec4b6e72011-09-30 19:48:58 +0000533
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000534 // The list of struct types we got back includes all the struct types, split
535 // the unnamed ones out to a numbering and remove the anonymous structs.
536 unsigned NextNumber = 0;
Andrew Trickec4b6e72011-09-30 19:48:58 +0000537
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000538 std::vector<StructType*>::iterator NextToUse = NamedTypes.begin(), I, E;
539 for (I = NamedTypes.begin(), E = NamedTypes.end(); I != E; ++I) {
540 StructType *STy = *I;
Andrew Trickec4b6e72011-09-30 19:48:58 +0000541
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000542 // Ignore anonymous types.
Chris Lattner01beceb2011-08-12 18:07:07 +0000543 if (STy->isLiteral())
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000544 continue;
Andrew Trickec4b6e72011-09-30 19:48:58 +0000545
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000546 if (STy->getName().empty())
Roman Tereshind96de6f2018-03-22 21:29:07 +0000547 Type2Number[STy] = NextNumber++;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000548 else
549 *NextToUse++ = STy;
550 }
Andrew Trickec4b6e72011-09-30 19:48:58 +0000551
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000552 NamedTypes.erase(NextToUse, NamedTypes.end());
553}
554
Roman Tereshind96de6f2018-03-22 21:29:07 +0000555/// Write the specified type to the specified raw_ostream, making use of type
556/// names or up references to shorten the type name where possible.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000557void TypePrinting::print(Type *Ty, raw_ostream &OS) {
Chris Lattner0f578952009-02-28 20:25:14 +0000558 switch (Ty->getTypeID()) {
Rafael Espindolaba7df702013-12-07 02:27:52 +0000559 case Type::VoidTyID: OS << "void"; return;
560 case Type::HalfTyID: OS << "half"; return;
561 case Type::FloatTyID: OS << "float"; return;
562 case Type::DoubleTyID: OS << "double"; return;
563 case Type::X86_FP80TyID: OS << "x86_fp80"; return;
564 case Type::FP128TyID: OS << "fp128"; return;
565 case Type::PPC_FP128TyID: OS << "ppc_fp128"; return;
566 case Type::LabelTyID: OS << "label"; return;
567 case Type::MetadataTyID: OS << "metadata"; return;
568 case Type::X86_MMXTyID: OS << "x86_mmx"; return;
David Majnemerb611e3f2015-08-14 05:09:07 +0000569 case Type::TokenTyID: OS << "token"; return;
Chris Lattner68318b12009-02-28 21:18:43 +0000570 case Type::IntegerTyID:
Chris Lattner06a23212009-02-28 21:27:31 +0000571 OS << 'i' << cast<IntegerType>(Ty)->getBitWidth();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000572 return;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000573
Chris Lattner07d88292009-02-28 20:35:42 +0000574 case Type::FunctionTyID: {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000575 FunctionType *FTy = cast<FunctionType>(Ty);
576 print(FTy->getReturnType(), OS);
Chris Lattner06a23212009-02-28 21:27:31 +0000577 OS << " (";
Chris Lattner07d88292009-02-28 20:35:42 +0000578 for (FunctionType::param_iterator I = FTy->param_begin(),
579 E = FTy->param_end(); I != E; ++I) {
580 if (I != FTy->param_begin())
Chris Lattner06a23212009-02-28 21:27:31 +0000581 OS << ", ";
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000582 print(*I, OS);
Chris Lattner0f578952009-02-28 20:25:14 +0000583 }
Chris Lattner07d88292009-02-28 20:35:42 +0000584 if (FTy->isVarArg()) {
Chris Lattner06a23212009-02-28 21:27:31 +0000585 if (FTy->getNumParams()) OS << ", ";
586 OS << "...";
Chris Lattner0f578952009-02-28 20:25:14 +0000587 }
Chris Lattner06a23212009-02-28 21:27:31 +0000588 OS << ')';
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000589 return;
Chris Lattner07d88292009-02-28 20:35:42 +0000590 }
591 case Type::StructTyID: {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000592 StructType *STy = cast<StructType>(Ty);
Andrew Trickec4b6e72011-09-30 19:48:58 +0000593
Chris Lattner01beceb2011-08-12 18:07:07 +0000594 if (STy->isLiteral())
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000595 return printStructBody(STy, OS);
596
597 if (!STy->getName().empty())
598 return PrintLLVMName(OS, STy->getName(), LocalPrefix);
Andrew Trickec4b6e72011-09-30 19:48:58 +0000599
Roman Tereshind96de6f2018-03-22 21:29:07 +0000600 incorporateTypes();
601 const auto I = Type2Number.find(STy);
602 if (I != Type2Number.end())
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000603 OS << '%' << I->second;
604 else // Not enumerated, print the hex address.
Benjamin Kramer4e8b4632011-11-02 17:24:36 +0000605 OS << "%\"type " << STy << '\"';
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000606 return;
Chris Lattner07d88292009-02-28 20:35:42 +0000607 }
608 case Type::PointerTyID: {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000609 PointerType *PTy = cast<PointerType>(Ty);
610 print(PTy->getElementType(), OS);
Chris Lattner07d88292009-02-28 20:35:42 +0000611 if (unsigned AddressSpace = PTy->getAddressSpace())
Chris Lattner06a23212009-02-28 21:27:31 +0000612 OS << " addrspace(" << AddressSpace << ')';
613 OS << '*';
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000614 return;
Chris Lattner07d88292009-02-28 20:35:42 +0000615 }
616 case Type::ArrayTyID: {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000617 ArrayType *ATy = cast<ArrayType>(Ty);
Chris Lattner06a23212009-02-28 21:27:31 +0000618 OS << '[' << ATy->getNumElements() << " x ";
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000619 print(ATy->getElementType(), OS);
Chris Lattner06a23212009-02-28 21:27:31 +0000620 OS << ']';
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000621 return;
Chris Lattner07d88292009-02-28 20:35:42 +0000622 }
623 case Type::VectorTyID: {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000624 VectorType *PTy = cast<VectorType>(Ty);
Chris Lattner06a23212009-02-28 21:27:31 +0000625 OS << "<" << PTy->getNumElements() << " x ";
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000626 print(PTy->getElementType(), OS);
Chris Lattner06a23212009-02-28 21:27:31 +0000627 OS << '>';
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000628 return;
Chris Lattner07d88292009-02-28 20:35:42 +0000629 }
Chris Lattner0f578952009-02-28 20:25:14 +0000630 }
Rafael Espindolaba7df702013-12-07 02:27:52 +0000631 llvm_unreachable("Invalid TypeID");
Chris Lattner0f578952009-02-28 20:25:14 +0000632}
633
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000634void TypePrinting::printStructBody(StructType *STy, raw_ostream &OS) {
635 if (STy->isOpaque()) {
636 OS << "opaque";
637 return;
Chris Lattner0f578952009-02-28 20:25:14 +0000638 }
Andrew Trickec4b6e72011-09-30 19:48:58 +0000639
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000640 if (STy->isPacked())
641 OS << '<';
Andrew Trickec4b6e72011-09-30 19:48:58 +0000642
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000643 if (STy->getNumElements() == 0) {
644 OS << "{}";
645 } else {
646 StructType::element_iterator I = STy->element_begin();
647 OS << "{ ";
648 print(*I++, OS);
649 for (StructType::element_iterator E = STy->element_end(); I != E; ++I) {
650 OS << ", ";
651 print(*I, OS);
Chris Lattner3243ea12009-03-01 00:03:38 +0000652 }
Andrew Trickec4b6e72011-09-30 19:48:58 +0000653
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000654 OS << " }";
Chris Lattner92c5c122009-02-28 23:20:19 +0000655 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000656 if (STy->isPacked())
657 OS << '>';
Chris Lattner92c5c122009-02-28 23:20:19 +0000658}
659
Duncan P. N. Exon Smith326921542015-06-26 22:04:20 +0000660namespace llvm {
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000661
Chris Lattner3eee99c2008-08-19 04:36:02 +0000662//===----------------------------------------------------------------------===//
663// SlotTracker Class: Enumerate slot numbers for unnamed values
664//===----------------------------------------------------------------------===//
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000665/// This class provides computation of slot numbers for LLVM Assembly writing.
Chris Lattner393b7cd2008-08-17 04:17:45 +0000666///
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000667class SlotTracker {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000668public:
Devang Patel983c6b12009-07-08 21:44:25 +0000669 /// ValueMap - A mapping of Values to slot numbers.
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000670 using ValueMap = DenseMap<const Value *, unsigned>;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000671
672private:
Devang Patel983c6b12009-07-08 21:44:25 +0000673 /// TheModule - The module for which we are holding slot numbers.
Chris Lattner393b7cd2008-08-17 04:17:45 +0000674 const Module* TheModule;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000675
Devang Patel983c6b12009-07-08 21:44:25 +0000676 /// TheFunction - The function for which we are holding slot numbers.
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000677 const Function* TheFunction = nullptr;
678 bool FunctionProcessed = false;
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000679 bool ShouldInitializeAllMetadata;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000680
Teresa Johnson08d5b4e2018-05-26 02:34:13 +0000681 /// The summary index for which we are holding slot numbers.
682 const ModuleSummaryIndex *TheIndex = nullptr;
683
Jay Foaddbb221d2011-07-11 07:28:49 +0000684 /// mMap - The slot map for the module level data.
Chris Lattner393b7cd2008-08-17 04:17:45 +0000685 ValueMap mMap;
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000686 unsigned mNext = 0;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000687
Jay Foaddbb221d2011-07-11 07:28:49 +0000688 /// fMap - The slot map for the function level data.
Chris Lattner393b7cd2008-08-17 04:17:45 +0000689 ValueMap fMap;
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000690 unsigned fNext = 0;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000691
Devang Patel983c6b12009-07-08 21:44:25 +0000692 /// mdnMap - Map for MDNodes.
Chris Lattnercf4a76e2009-12-31 02:20:11 +0000693 DenseMap<const MDNode*, unsigned> mdnMap;
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000694 unsigned mdnNext = 0;
Bill Wendling829b4782013-02-11 08:43:33 +0000695
696 /// asMap - The slot map for attribute sets.
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000697 DenseMap<AttributeSet, unsigned> asMap;
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000698 unsigned asNext = 0;
699
Teresa Johnson8fc76662018-07-02 22:09:23 +0000700 /// ModulePathMap - The slot map for Module paths used in the summary index.
701 StringMap<unsigned> ModulePathMap;
702 unsigned ModulePathNext = 0;
703
Teresa Johnson08d5b4e2018-05-26 02:34:13 +0000704 /// GUIDMap - The slot map for GUIDs used in the summary index.
705 DenseMap<GlobalValue::GUID, unsigned> GUIDMap;
706 unsigned GUIDNext = 0;
707
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000708public:
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000709 /// Construct from a module.
710 ///
711 /// If \c ShouldInitializeAllMetadata, initializes all metadata in all
712 /// functions, giving correct numbering for metadata referenced only from
713 /// within a function (even if no functions have been initialized).
714 explicit SlotTracker(const Module *M,
715 bool ShouldInitializeAllMetadata = false);
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000716
Chris Lattner393b7cd2008-08-17 04:17:45 +0000717 /// Construct from a function, starting out in incorp state.
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000718 ///
719 /// If \c ShouldInitializeAllMetadata, initializes all metadata in all
720 /// functions, giving correct numbering for metadata referenced only from
721 /// within a function (even if no functions have been initialized).
722 explicit SlotTracker(const Function *F,
723 bool ShouldInitializeAllMetadata = false);
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000724
Teresa Johnson08d5b4e2018-05-26 02:34:13 +0000725 /// Construct from a module summary index.
726 explicit SlotTracker(const ModuleSummaryIndex *Index);
727
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000728 SlotTracker(const SlotTracker &) = delete;
729 SlotTracker &operator=(const SlotTracker &) = delete;
730
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000731 /// Return the slot number of the specified value in it's type
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000732 /// plane. If something is not in the SlotTracker, return -1.
Chris Lattner5e043322007-01-11 03:54:27 +0000733 int getLocalSlot(const Value *V);
734 int getGlobalSlot(const GlobalValue *V);
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000735 int getMetadataSlot(const MDNode *N);
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000736 int getAttributeGroupSlot(AttributeSet AS);
Teresa Johnson8fc76662018-07-02 22:09:23 +0000737 int getModulePathSlot(StringRef Path);
Teresa Johnson08d5b4e2018-05-26 02:34:13 +0000738 int getGUIDSlot(GlobalValue::GUID GUID);
Reid Spencer8beac692004-06-09 15:26:53 +0000739
Misha Brukmanb1c93172005-04-21 23:48:37 +0000740 /// If you'd like to deal with a function instead of just a module, use
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000741 /// this method to get its data into the SlotTracker.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000742 void incorporateFunction(const Function *F) {
743 TheFunction = F;
Reid Spencerb0ac8c42004-08-16 07:46:33 +0000744 FunctionProcessed = false;
745 }
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000746
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +0000747 const Function *getFunction() const { return TheFunction; }
748
Misha Brukmanb1c93172005-04-21 23:48:37 +0000749 /// After calling incorporateFunction, use this method to remove the
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000750 /// most recently incorporated function from the SlotTracker. This
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000751 /// will reset the state of the machine back to just the module contents.
752 void purgeFunction();
753
Devang Patel983c6b12009-07-08 21:44:25 +0000754 /// MDNode map iterators.
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000755 using mdn_iterator = DenseMap<const MDNode*, unsigned>::iterator;
756
Chris Lattnercf4a76e2009-12-31 02:20:11 +0000757 mdn_iterator mdn_begin() { return mdnMap.begin(); }
758 mdn_iterator mdn_end() { return mdnMap.end(); }
759 unsigned mdn_size() const { return mdnMap.size(); }
760 bool mdn_empty() const { return mdnMap.empty(); }
Devang Patel983c6b12009-07-08 21:44:25 +0000761
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000762 /// AttributeSet map iterators.
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000763 using as_iterator = DenseMap<AttributeSet, unsigned>::iterator;
764
Bill Wendling829b4782013-02-11 08:43:33 +0000765 as_iterator as_begin() { return asMap.begin(); }
766 as_iterator as_end() { return asMap.end(); }
767 unsigned as_size() const { return asMap.size(); }
768 bool as_empty() const { return asMap.empty(); }
769
Teresa Johnson08d5b4e2018-05-26 02:34:13 +0000770 /// GUID map iterators.
771 using guid_iterator = DenseMap<GlobalValue::GUID, unsigned>::iterator;
772
773 /// These functions do the actual initialization.
Teresa Johnsonfc801f52018-07-03 15:52:57 +0000774 inline void initializeIfNeeded();
775 void initializeIndexIfNeeded();
Reid Spencer56010e42004-05-26 21:56:09 +0000776
Devang Patel983c6b12009-07-08 21:44:25 +0000777 // Implementation Details
778private:
Chris Lattnerea862a32007-01-09 07:55:49 +0000779 /// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
780 void CreateModuleSlot(const GlobalValue *V);
Devang Patel983c6b12009-07-08 21:44:25 +0000781
782 /// CreateMetadataSlot - Insert the specified MDNode* into the slot table.
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000783 void CreateMetadataSlot(const MDNode *N);
Devang Patel983c6b12009-07-08 21:44:25 +0000784
Chris Lattnerea862a32007-01-09 07:55:49 +0000785 /// CreateFunctionSlot - Insert the specified Value* into the slot table.
786 void CreateFunctionSlot(const Value *V);
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000787
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000788 /// Insert the specified AttributeSet into the slot table.
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000789 void CreateAttributeSetSlot(AttributeSet AS);
Bill Wendling829b4782013-02-11 08:43:33 +0000790
Teresa Johnson8fc76662018-07-02 22:09:23 +0000791 inline void CreateModulePathSlot(StringRef Path);
Teresa Johnson08d5b4e2018-05-26 02:34:13 +0000792 void CreateGUIDSlot(GlobalValue::GUID GUID);
793
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000794 /// Add all of the module level global variables (and their initializers)
795 /// and function declarations, but not the contents of those functions.
796 void processModule();
Teresa Johnson08d5b4e2018-05-26 02:34:13 +0000797 void processIndex();
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000798
Devang Patel983c6b12009-07-08 21:44:25 +0000799 /// Add all of the functions arguments, basic blocks, and instructions.
Reid Spencer56010e42004-05-26 21:56:09 +0000800 void processFunction();
801
Peter Collingbournecceae7f2016-05-31 23:01:54 +0000802 /// Add the metadata directly attached to a GlobalObject.
803 void processGlobalObjectMetadata(const GlobalObject &GO);
804
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000805 /// Add all of the metadata from a function.
806 void processFunctionMetadata(const Function &F);
807
Duncan P. N. Exon Smith20b76ac2015-03-14 19:48:31 +0000808 /// Add all of the metadata from an instruction.
809 void processInstructionMetadata(const Instruction &I);
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000810};
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000811
812} // end namespace llvm
Duncan P. N. Exon Smith326921542015-06-26 22:04:20 +0000813
814ModuleSlotTracker::ModuleSlotTracker(SlotTracker &Machine, const Module *M,
815 const Function *F)
816 : M(M), F(F), Machine(&Machine) {}
817
Duncan P. N. Exon Smith6529ed42015-06-26 22:28:47 +0000818ModuleSlotTracker::ModuleSlotTracker(const Module *M,
819 bool ShouldInitializeAllMetadata)
Duncan P. N. Exon Smithaf0fdc22016-04-20 19:05:59 +0000820 : ShouldCreateStorage(M),
821 ShouldInitializeAllMetadata(ShouldInitializeAllMetadata), M(M) {}
Duncan P. N. Exon Smith326921542015-06-26 22:04:20 +0000822
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000823ModuleSlotTracker::~ModuleSlotTracker() = default;
Duncan P. N. Exon Smith326921542015-06-26 22:04:20 +0000824
Duncan P. N. Exon Smithaf0fdc22016-04-20 19:05:59 +0000825SlotTracker *ModuleSlotTracker::getMachine() {
826 if (!ShouldCreateStorage)
827 return Machine;
828
829 ShouldCreateStorage = false;
830 MachineStorage =
831 llvm::make_unique<SlotTracker>(M, ShouldInitializeAllMetadata);
832 Machine = MachineStorage.get();
833 return Machine;
834}
835
Duncan P. N. Exon Smith326921542015-06-26 22:04:20 +0000836void ModuleSlotTracker::incorporateFunction(const Function &F) {
Duncan P. N. Exon Smithaf0fdc22016-04-20 19:05:59 +0000837 // Using getMachine() may lazily create the slot tracker.
838 if (!getMachine())
Duncan P. N. Exon Smith326921542015-06-26 22:04:20 +0000839 return;
840
841 // Nothing to do if this is the right function already.
842 if (this->F == &F)
843 return;
844 if (this->F)
845 Machine->purgeFunction();
846 Machine->incorporateFunction(&F);
847 this->F = &F;
848}
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000849
Alex Lorenz991a6242015-07-27 22:31:04 +0000850int ModuleSlotTracker::getLocalSlot(const Value *V) {
851 assert(F && "No function incorporated");
852 return Machine->getLocalSlot(V);
853}
854
Chris Lattner3eee99c2008-08-19 04:36:02 +0000855static SlotTracker *createSlotTracker(const Value *V) {
856 if (const Argument *FA = dyn_cast<Argument>(V))
857 return new SlotTracker(FA->getParent());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000858
Chris Lattner3eee99c2008-08-19 04:36:02 +0000859 if (const Instruction *I = dyn_cast<Instruction>(V))
Andrew Trick2f0cbf62011-09-30 19:50:40 +0000860 if (I->getParent())
861 return new SlotTracker(I->getParent()->getParent());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000862
Chris Lattner3eee99c2008-08-19 04:36:02 +0000863 if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
864 return new SlotTracker(BB->getParent());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000865
Chris Lattner3eee99c2008-08-19 04:36:02 +0000866 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
867 return new SlotTracker(GV->getParent());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000868
Chris Lattner3eee99c2008-08-19 04:36:02 +0000869 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V))
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000870 return new SlotTracker(GA->getParent());
871
Dmitry Polukhina1feff72016-04-07 12:32:19 +0000872 if (const GlobalIFunc *GIF = dyn_cast<GlobalIFunc>(V))
873 return new SlotTracker(GIF->getParent());
874
Chris Lattner3eee99c2008-08-19 04:36:02 +0000875 if (const Function *Func = dyn_cast<Function>(V))
876 return new SlotTracker(Func);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000877
Craig Topperc6207612014-04-09 06:08:46 +0000878 return nullptr;
Chris Lattner3eee99c2008-08-19 04:36:02 +0000879}
880
881#if 0
David Greenec7f9b122010-01-05 01:29:26 +0000882#define ST_DEBUG(X) dbgs() << X
Chris Lattner3eee99c2008-08-19 04:36:02 +0000883#else
Chris Lattner604e3512008-08-19 04:47:09 +0000884#define ST_DEBUG(X)
Chris Lattner3eee99c2008-08-19 04:36:02 +0000885#endif
886
887// Module level constructor. Causes the contents of the Module (sans functions)
888// to be added to the slot table.
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000889SlotTracker::SlotTracker(const Module *M, bool ShouldInitializeAllMetadata)
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000890 : TheModule(M), ShouldInitializeAllMetadata(ShouldInitializeAllMetadata) {}
Chris Lattner3eee99c2008-08-19 04:36:02 +0000891
892// Function level constructor. Causes the contents of the Module and the one
893// function provided to be added to the slot table.
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000894SlotTracker::SlotTracker(const Function *F, bool ShouldInitializeAllMetadata)
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000895 : TheModule(F ? F->getParent() : nullptr), TheFunction(F),
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000896 ShouldInitializeAllMetadata(ShouldInitializeAllMetadata) {}
Chris Lattner3eee99c2008-08-19 04:36:02 +0000897
Teresa Johnson08d5b4e2018-05-26 02:34:13 +0000898SlotTracker::SlotTracker(const ModuleSummaryIndex *Index)
899 : TheModule(nullptr), ShouldInitializeAllMetadata(false), TheIndex(Index) {}
900
Teresa Johnsonfc801f52018-07-03 15:52:57 +0000901inline void SlotTracker::initializeIfNeeded() {
Chris Lattner3eee99c2008-08-19 04:36:02 +0000902 if (TheModule) {
903 processModule();
Craig Topperc6207612014-04-09 06:08:46 +0000904 TheModule = nullptr; ///< Prevent re-processing next time we're called.
Chris Lattner3eee99c2008-08-19 04:36:02 +0000905 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000906
Chris Lattner3eee99c2008-08-19 04:36:02 +0000907 if (TheFunction && !FunctionProcessed)
908 processFunction();
909}
910
Teresa Johnsonfc801f52018-07-03 15:52:57 +0000911void SlotTracker::initializeIndexIfNeeded() {
Teresa Johnson08d5b4e2018-05-26 02:34:13 +0000912 if (!TheIndex)
913 return;
914 processIndex();
915 TheIndex = nullptr; ///< Prevent re-processing next time we're called.
916}
917
Chris Lattner3eee99c2008-08-19 04:36:02 +0000918// Iterate through all the global variables, functions, and global
919// variable initializers and create slots for them.
920void SlotTracker::processModule() {
Chris Lattner604e3512008-08-19 04:47:09 +0000921 ST_DEBUG("begin processModule!\n");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000922
Chris Lattner3eee99c2008-08-19 04:36:02 +0000923 // Add all of the unnamed global variables to the value table.
Rafael Espindola5d8a1552015-06-17 17:33:37 +0000924 for (const GlobalVariable &Var : TheModule->globals()) {
925 if (!Var.hasName())
926 CreateModuleSlot(&Var);
Peter Collingbournecceae7f2016-05-31 23:01:54 +0000927 processGlobalObjectMetadata(Var);
Javed Absarf3d79042017-05-11 12:28:08 +0000928 auto Attrs = Var.getAttributes();
929 if (Attrs.hasAttributes())
930 CreateAttributeSetSlot(Attrs);
Devang Patel983c6b12009-07-08 21:44:25 +0000931 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000932
Rafael Espindola54fc2982015-06-17 17:53:31 +0000933 for (const GlobalAlias &A : TheModule->aliases()) {
934 if (!A.hasName())
935 CreateModuleSlot(&A);
936 }
937
Dmitry Polukhina1feff72016-04-07 12:32:19 +0000938 for (const GlobalIFunc &I : TheModule->ifuncs()) {
939 if (!I.hasName())
940 CreateModuleSlot(&I);
941 }
942
Devang Patel23e68302009-07-29 22:04:47 +0000943 // Add metadata used by named metadata.
Rafael Espindola5d8a1552015-06-17 17:33:37 +0000944 for (const NamedMDNode &NMD : TheModule->named_metadata()) {
945 for (unsigned i = 0, e = NMD.getNumOperands(); i != e; ++i)
946 CreateMetadataSlot(NMD.getOperand(i));
Devang Patel23e68302009-07-29 22:04:47 +0000947 }
948
Rafael Espindola5d8a1552015-06-17 17:33:37 +0000949 for (const Function &F : *TheModule) {
950 if (!F.hasName())
Bill Wendling829b4782013-02-11 08:43:33 +0000951 // Add all the unnamed functions to the table.
Rafael Espindola5d8a1552015-06-17 17:33:37 +0000952 CreateModuleSlot(&F);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000953
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000954 if (ShouldInitializeAllMetadata)
Rafael Espindola5d8a1552015-06-17 17:33:37 +0000955 processFunctionMetadata(F);
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000956
Bill Wendling829b4782013-02-11 08:43:33 +0000957 // Add all the function attributes to the table.
Bill Wendling90bc19c2013-02-20 07:21:42 +0000958 // FIXME: Add attributes of other objects?
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000959 AttributeSet FnAttrs = F.getAttributes().getFnAttributes();
960 if (FnAttrs.hasAttributes())
Bill Wendling829b4782013-02-11 08:43:33 +0000961 CreateAttributeSetSlot(FnAttrs);
962 }
963
Chris Lattner604e3512008-08-19 04:47:09 +0000964 ST_DEBUG("end processModule!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000965}
966
Chris Lattner3eee99c2008-08-19 04:36:02 +0000967// Process the arguments, basic blocks, and instructions of a function.
968void SlotTracker::processFunction() {
Chris Lattner604e3512008-08-19 04:47:09 +0000969 ST_DEBUG("begin processFunction!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000970 fNext = 0;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000971
Duncan P. N. Exon Smithadc54562015-09-11 01:34:59 +0000972 // Process function metadata if it wasn't hit at the module-level.
973 if (!ShouldInitializeAllMetadata)
974 processFunctionMetadata(*TheFunction);
975
Chris Lattner3eee99c2008-08-19 04:36:02 +0000976 // Add all the function arguments with no names.
977 for(Function::const_arg_iterator AI = TheFunction->arg_begin(),
978 AE = TheFunction->arg_end(); AI != AE; ++AI)
979 if (!AI->hasName())
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +0000980 CreateFunctionSlot(&*AI);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000981
Chris Lattner604e3512008-08-19 04:47:09 +0000982 ST_DEBUG("Inserting Instructions:\n");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000983
Chris Lattner3eee99c2008-08-19 04:36:02 +0000984 // Add all of the basic blocks and instructions with no names.
Duncan P. N. Exon Smith27f33ee2015-03-14 19:44:01 +0000985 for (auto &BB : *TheFunction) {
986 if (!BB.hasName())
987 CreateFunctionSlot(&BB);
Andrew Trickec4b6e72011-09-30 19:48:58 +0000988
Duncan P. N. Exon Smith27f33ee2015-03-14 19:44:01 +0000989 for (auto &I : BB) {
990 if (!I.getType()->isVoidTy() && !I.hasName())
991 CreateFunctionSlot(&I);
Andrew Trickec4b6e72011-09-30 19:48:58 +0000992
Duncan P. N. Exon Smith20b76ac2015-03-14 19:48:31 +0000993 // We allow direct calls to any llvm.foo function here, because the
994 // target may not be linked into the optimizer.
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000995 if (auto CS = ImmutableCallSite(&I)) {
Bill Wendlinga0323742013-02-22 09:09:42 +0000996 // Add all the call attributes to the table.
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000997 AttributeSet Attrs = CS.getAttributes().getFnAttributes();
998 if (Attrs.hasAttributes())
Bill Wendlinga0323742013-02-22 09:09:42 +0000999 CreateAttributeSetSlot(Attrs);
Chris Lattner58aff8f2010-05-10 20:53:17 +00001000 }
Devang Patel983c6b12009-07-08 21:44:25 +00001001 }
Chris Lattner3eee99c2008-08-19 04:36:02 +00001002 }
Devang Pateldec23fd2009-09-16 20:21:17 +00001003
Chris Lattner3eee99c2008-08-19 04:36:02 +00001004 FunctionProcessed = true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001005
Chris Lattner604e3512008-08-19 04:47:09 +00001006 ST_DEBUG("end processFunction!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +00001007}
1008
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00001009// Iterate through all the GUID in the index and create slots for them.
1010void SlotTracker::processIndex() {
1011 ST_DEBUG("begin processIndex!\n");
1012 assert(TheIndex);
1013
1014 // The first block of slots are just the module ids, which start at 0 and are
Teresa Johnson8fc76662018-07-02 22:09:23 +00001015 // assigned consecutively. Since the StringMap iteration order isn't
1016 // guaranteed, use a std::map to order by module ID before assigning slots.
1017 std::map<uint64_t, StringRef> ModuleIdToPathMap;
1018 for (auto &ModPath : TheIndex->modulePaths())
1019 ModuleIdToPathMap[ModPath.second.first] = ModPath.first();
1020 for (auto &ModPair : ModuleIdToPathMap)
1021 CreateModulePathSlot(ModPair.second);
1022
1023 // Start numbering the GUIDs after the module ids.
1024 GUIDNext = ModulePathNext;
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00001025
1026 for (auto &GlobalList : *TheIndex)
1027 CreateGUIDSlot(GlobalList.first);
1028
1029 for (auto &TId : TheIndex->typeIds())
1030 CreateGUIDSlot(GlobalValue::getGUID(TId.first));
1031
1032 ST_DEBUG("end processIndex!\n");
1033}
1034
Peter Collingbournecceae7f2016-05-31 23:01:54 +00001035void SlotTracker::processGlobalObjectMetadata(const GlobalObject &GO) {
Duncan P. N. Exon Smith3d4cd752015-04-24 22:04:41 +00001036 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
Peter Collingbournecceae7f2016-05-31 23:01:54 +00001037 GO.getAllMetadata(MDs);
Duncan P. N. Exon Smithadc54562015-09-11 01:34:59 +00001038 for (auto &MD : MDs)
1039 CreateMetadataSlot(MD.second);
Peter Collingbournecceae7f2016-05-31 23:01:54 +00001040}
Duncan P. N. Exon Smith3d4cd752015-04-24 22:04:41 +00001041
Peter Collingbournecceae7f2016-05-31 23:01:54 +00001042void SlotTracker::processFunctionMetadata(const Function &F) {
1043 processGlobalObjectMetadata(F);
Duncan P. N. Exon Smithadc54562015-09-11 01:34:59 +00001044 for (auto &BB : F) {
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +00001045 for (auto &I : BB)
1046 processInstructionMetadata(I);
Duncan P. N. Exon Smith3d4cd752015-04-24 22:04:41 +00001047 }
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +00001048}
1049
Duncan P. N. Exon Smith20b76ac2015-03-14 19:48:31 +00001050void SlotTracker::processInstructionMetadata(const Instruction &I) {
1051 // Process metadata used directly by intrinsics.
1052 if (const CallInst *CI = dyn_cast<CallInst>(&I))
1053 if (Function *F = CI->getCalledFunction())
Justin Lebar9cbc3012016-07-28 23:58:15 +00001054 if (F->isIntrinsic())
Duncan P. N. Exon Smith20b76ac2015-03-14 19:48:31 +00001055 for (auto &Op : I.operands())
1056 if (auto *V = dyn_cast_or_null<MetadataAsValue>(Op))
1057 if (MDNode *N = dyn_cast<MDNode>(V->getMetadata()))
1058 CreateMetadataSlot(N);
1059
1060 // Process metadata attached to this instruction.
1061 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
1062 I.getAllMetadata(MDs);
1063 for (auto &MD : MDs)
1064 CreateMetadataSlot(MD.second);
1065}
1066
Chris Lattner3eee99c2008-08-19 04:36:02 +00001067/// Clean up after incorporating a function. This is the only way to get out of
1068/// the function incorporation state that affects get*Slot/Create*Slot. Function
1069/// incorporation state is indicated by TheFunction != 0.
1070void SlotTracker::purgeFunction() {
Chris Lattner604e3512008-08-19 04:47:09 +00001071 ST_DEBUG("begin purgeFunction!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +00001072 fMap.clear(); // Simply discard the function level map
Craig Topperc6207612014-04-09 06:08:46 +00001073 TheFunction = nullptr;
Chris Lattner3eee99c2008-08-19 04:36:02 +00001074 FunctionProcessed = false;
Chris Lattner604e3512008-08-19 04:47:09 +00001075 ST_DEBUG("end purgeFunction!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +00001076}
1077
1078/// getGlobalSlot - Get the slot number of a global value.
1079int SlotTracker::getGlobalSlot(const GlobalValue *V) {
1080 // Check for uninitialized state and do lazy initialization.
Teresa Johnsonfc801f52018-07-03 15:52:57 +00001081 initializeIfNeeded();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001082
Jay Foaddbb221d2011-07-11 07:28:49 +00001083 // Find the value in the module map
Chris Lattner3eee99c2008-08-19 04:36:02 +00001084 ValueMap::iterator MI = mMap.find(V);
Dan Gohman1dd27572008-10-01 19:58:59 +00001085 return MI == mMap.end() ? -1 : (int)MI->second;
Chris Lattner3eee99c2008-08-19 04:36:02 +00001086}
1087
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00001088/// getMetadataSlot - Get the slot number of a MDNode.
1089int SlotTracker::getMetadataSlot(const MDNode *N) {
Devang Patel983c6b12009-07-08 21:44:25 +00001090 // Check for uninitialized state and do lazy initialization.
Teresa Johnsonfc801f52018-07-03 15:52:57 +00001091 initializeIfNeeded();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001092
Jay Foaddbb221d2011-07-11 07:28:49 +00001093 // Find the MDNode in the module map
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00001094 mdn_iterator MI = mdnMap.find(N);
Devang Patel983c6b12009-07-08 21:44:25 +00001095 return MI == mdnMap.end() ? -1 : (int)MI->second;
1096}
1097
Chris Lattner3eee99c2008-08-19 04:36:02 +00001098/// getLocalSlot - Get the slot number for a value that is local to a function.
1099int SlotTracker::getLocalSlot(const Value *V) {
1100 assert(!isa<Constant>(V) && "Can't get a constant or global slot with this!");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001101
Chris Lattner3eee99c2008-08-19 04:36:02 +00001102 // Check for uninitialized state and do lazy initialization.
Teresa Johnsonfc801f52018-07-03 15:52:57 +00001103 initializeIfNeeded();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001104
Chris Lattner3eee99c2008-08-19 04:36:02 +00001105 ValueMap::iterator FI = fMap.find(V);
Dan Gohman1dd27572008-10-01 19:58:59 +00001106 return FI == fMap.end() ? -1 : (int)FI->second;
Chris Lattner3eee99c2008-08-19 04:36:02 +00001107}
1108
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001109int SlotTracker::getAttributeGroupSlot(AttributeSet AS) {
Bill Wendling829b4782013-02-11 08:43:33 +00001110 // Check for uninitialized state and do lazy initialization.
Teresa Johnsonfc801f52018-07-03 15:52:57 +00001111 initializeIfNeeded();
Bill Wendling829b4782013-02-11 08:43:33 +00001112
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001113 // Find the AttributeSet in the module map.
Bill Wendling829b4782013-02-11 08:43:33 +00001114 as_iterator AI = asMap.find(AS);
1115 return AI == asMap.end() ? -1 : (int)AI->second;
1116}
Chris Lattner3eee99c2008-08-19 04:36:02 +00001117
Teresa Johnson8fc76662018-07-02 22:09:23 +00001118int SlotTracker::getModulePathSlot(StringRef Path) {
1119 // Check for uninitialized state and do lazy initialization.
Teresa Johnsonfc801f52018-07-03 15:52:57 +00001120 initializeIndexIfNeeded();
Teresa Johnson8fc76662018-07-02 22:09:23 +00001121
1122 // Find the Module path in the map
1123 auto I = ModulePathMap.find(Path);
1124 return I == ModulePathMap.end() ? -1 : (int)I->second;
1125}
1126
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00001127int SlotTracker::getGUIDSlot(GlobalValue::GUID GUID) {
1128 // Check for uninitialized state and do lazy initialization.
Teresa Johnsonfc801f52018-07-03 15:52:57 +00001129 initializeIndexIfNeeded();
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00001130
1131 // Find the GUID in the map
1132 guid_iterator I = GUIDMap.find(GUID);
1133 return I == GUIDMap.end() ? -1 : (int)I->second;
1134}
1135
Chris Lattner3eee99c2008-08-19 04:36:02 +00001136/// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
1137void SlotTracker::CreateModuleSlot(const GlobalValue *V) {
1138 assert(V && "Can't insert a null Value into SlotTracker!");
Chris Lattner031560c2009-12-29 07:25:48 +00001139 assert(!V->getType()->isVoidTy() && "Doesn't need a slot!");
Chris Lattner3eee99c2008-08-19 04:36:02 +00001140 assert(!V->hasName() && "Doesn't need a slot!");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001141
Chris Lattner3eee99c2008-08-19 04:36:02 +00001142 unsigned DestSlot = mNext++;
1143 mMap[V] = DestSlot;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001144
Chris Lattner604e3512008-08-19 04:47:09 +00001145 ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" <<
Chris Lattner3eee99c2008-08-19 04:36:02 +00001146 DestSlot << " [");
Dmitry Polukhina1feff72016-04-07 12:32:19 +00001147 // G = Global, F = Function, A = Alias, I = IFunc, o = other
Chris Lattner604e3512008-08-19 04:47:09 +00001148 ST_DEBUG((isa<GlobalVariable>(V) ? 'G' :
Chris Lattner3eee99c2008-08-19 04:36:02 +00001149 (isa<Function>(V) ? 'F' :
Dmitry Polukhina1feff72016-04-07 12:32:19 +00001150 (isa<GlobalAlias>(V) ? 'A' :
1151 (isa<GlobalIFunc>(V) ? 'I' : 'o')))) << "]\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +00001152}
1153
Chris Lattner3eee99c2008-08-19 04:36:02 +00001154/// CreateSlot - Create a new slot for the specified value if it has no name.
1155void SlotTracker::CreateFunctionSlot(const Value *V) {
Chris Lattner031560c2009-12-29 07:25:48 +00001156 assert(!V->getType()->isVoidTy() && !V->hasName() && "Doesn't need a slot!");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001157
Chris Lattner3eee99c2008-08-19 04:36:02 +00001158 unsigned DestSlot = fNext++;
1159 fMap[V] = DestSlot;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001160
Chris Lattner3eee99c2008-08-19 04:36:02 +00001161 // G = Global, F = Function, o = other
Chris Lattner604e3512008-08-19 04:47:09 +00001162 ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" <<
Chris Lattner3eee99c2008-08-19 04:36:02 +00001163 DestSlot << " [o]\n");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001164}
Chris Lattner3eee99c2008-08-19 04:36:02 +00001165
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00001166/// CreateModuleSlot - Insert the specified MDNode* into the slot table.
1167void SlotTracker::CreateMetadataSlot(const MDNode *N) {
1168 assert(N && "Can't insert a null Value into SlotTracker!");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001169
Reid Kleckner6d353342017-08-23 20:31:27 +00001170 // Don't make slots for DIExpressions. We just print them inline everywhere.
1171 if (isa<DIExpression>(N))
1172 return;
1173
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001174 unsigned DestSlot = mdnNext;
1175 if (!mdnMap.insert(std::make_pair(N, DestSlot)).second)
1176 return;
1177 ++mdnNext;
Devang Patel983c6b12009-07-08 21:44:25 +00001178
Chris Lattner0d50bdd2009-12-31 02:27:30 +00001179 // Recursively add any MDNodes referenced by operands.
1180 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1181 if (const MDNode *Op = dyn_cast_or_null<MDNode>(N->getOperand(i)))
1182 CreateMetadataSlot(Op);
Devang Patel983c6b12009-07-08 21:44:25 +00001183}
Chris Lattner3eee99c2008-08-19 04:36:02 +00001184
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001185void SlotTracker::CreateAttributeSetSlot(AttributeSet AS) {
1186 assert(AS.hasAttributes() && "Doesn't need a slot!");
Bill Wendling829b4782013-02-11 08:43:33 +00001187
1188 as_iterator I = asMap.find(AS);
1189 if (I != asMap.end())
1190 return;
1191
1192 unsigned DestSlot = asNext++;
1193 asMap[AS] = DestSlot;
1194}
1195
Teresa Johnson8fc76662018-07-02 22:09:23 +00001196/// Create a new slot for the specified Module
1197void SlotTracker::CreateModulePathSlot(StringRef Path) {
1198 ModulePathMap[Path] = ModulePathNext++;
1199}
1200
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00001201/// Create a new slot for the specified GUID
1202void SlotTracker::CreateGUIDSlot(GlobalValue::GUID GUID) {
1203 GUIDMap[GUID] = GUIDNext++;
1204}
1205
Chris Lattner3eee99c2008-08-19 04:36:02 +00001206//===----------------------------------------------------------------------===//
1207// AsmWriter Implementation
1208//===----------------------------------------------------------------------===//
Chris Lattner7f8845a2002-07-23 18:07:49 +00001209
Dan Gohman12dad632009-08-12 20:56:03 +00001210static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
Dan Gohman8061d9e2009-08-13 15:27:57 +00001211 TypePrinting *TypePrinter,
Dan Gohmana2489d12010-07-20 23:55:01 +00001212 SlotTracker *Machine,
1213 const Module *Context);
Reid Spencer58d30f22004-07-04 11:50:43 +00001214
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001215static void WriteAsOperandInternal(raw_ostream &Out, const Metadata *MD,
1216 TypePrinting *TypePrinter,
1217 SlotTracker *Machine, const Module *Context,
1218 bool FromValue = false);
1219
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001220static void writeAtomicRMWOperation(raw_ostream &Out,
1221 AtomicRMWInst::BinOp Op) {
1222 switch (Op) {
1223 default: Out << " <unknown operation " << Op << ">"; break;
1224 case AtomicRMWInst::Xchg: Out << " xchg"; break;
1225 case AtomicRMWInst::Add: Out << " add"; break;
1226 case AtomicRMWInst::Sub: Out << " sub"; break;
1227 case AtomicRMWInst::And: Out << " and"; break;
1228 case AtomicRMWInst::Nand: Out << " nand"; break;
1229 case AtomicRMWInst::Or: Out << " or"; break;
1230 case AtomicRMWInst::Xor: Out << " xor"; break;
1231 case AtomicRMWInst::Max: Out << " max"; break;
1232 case AtomicRMWInst::Min: Out << " min"; break;
1233 case AtomicRMWInst::UMax: Out << " umax"; break;
1234 case AtomicRMWInst::UMin: Out << " umin"; break;
1235 }
1236}
Devang Patel983c6b12009-07-08 21:44:25 +00001237
Dan Gohman12dad632009-08-12 20:56:03 +00001238static void WriteOptimizationInfo(raw_ostream &Out, const User *U) {
Michael Ilseman92053172012-11-27 00:42:44 +00001239 if (const FPMathOperator *FPO = dyn_cast<const FPMathOperator>(U)) {
Sanjay Patel629c4112017-11-06 16:27:15 +00001240 // 'Fast' is an abbreviation for all fast-math-flags.
1241 if (FPO->isFast())
Michael Ilseman92053172012-11-27 00:42:44 +00001242 Out << " fast";
1243 else {
Sanjay Patel629c4112017-11-06 16:27:15 +00001244 if (FPO->hasAllowReassoc())
1245 Out << " reassoc";
Michael Ilseman92053172012-11-27 00:42:44 +00001246 if (FPO->hasNoNaNs())
1247 Out << " nnan";
1248 if (FPO->hasNoInfs())
1249 Out << " ninf";
1250 if (FPO->hasNoSignedZeros())
1251 Out << " nsz";
1252 if (FPO->hasAllowReciprocal())
1253 Out << " arcp";
Adam Nemetcd847a82017-03-28 20:11:52 +00001254 if (FPO->hasAllowContract())
1255 Out << " contract";
Sanjay Patel629c4112017-11-06 16:27:15 +00001256 if (FPO->hasApproxFunc())
1257 Out << " afn";
Michael Ilseman92053172012-11-27 00:42:44 +00001258 }
1259 }
1260
Dan Gohman0ebd6962009-07-20 21:19:07 +00001261 if (const OverflowingBinaryOperator *OBO =
1262 dyn_cast<OverflowingBinaryOperator>(U)) {
Dan Gohman16f54152009-08-20 17:11:38 +00001263 if (OBO->hasNoUnsignedWrap())
Dan Gohman9c7f8082009-07-27 16:11:46 +00001264 Out << " nuw";
Dan Gohman16f54152009-08-20 17:11:38 +00001265 if (OBO->hasNoSignedWrap())
Dan Gohman9c7f8082009-07-27 16:11:46 +00001266 Out << " nsw";
Chris Lattner35315d02011-02-06 21:44:57 +00001267 } else if (const PossiblyExactOperator *Div =
1268 dyn_cast<PossiblyExactOperator>(U)) {
Dan Gohman0ebd6962009-07-20 21:19:07 +00001269 if (Div->isExact())
Dan Gohman9c7f8082009-07-27 16:11:46 +00001270 Out << " exact";
Dan Gohman1639c392009-07-27 21:53:46 +00001271 } else if (const GEPOperator *GEP = dyn_cast<GEPOperator>(U)) {
1272 if (GEP->isInBounds())
1273 Out << " inbounds";
Dan Gohman0ebd6962009-07-20 21:19:07 +00001274 }
1275}
1276
Dan Gohmanefb8dbb2010-07-14 20:57:55 +00001277static void WriteConstantInternal(raw_ostream &Out, const Constant *CV,
1278 TypePrinting &TypePrinter,
Dan Gohmana2489d12010-07-20 23:55:01 +00001279 SlotTracker *Machine,
1280 const Module *Context) {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001281 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00001282 if (CI->getType()->isIntegerTy(1)) {
Reid Spencercddc9df2007-01-12 04:24:46 +00001283 Out << (CI->getZExtValue() ? "true" : "false");
Chris Lattner17f71652008-08-17 07:19:36 +00001284 return;
1285 }
1286 Out << CI->getValue();
1287 return;
1288 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001289
Chris Lattner17f71652008-08-17 07:19:36 +00001290 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Serguei Katkov3a46eb42017-04-21 02:52:17 +00001291 const APFloat &APF = CFP->getValueAPF();
1292 if (&APF.getSemantics() == &APFloat::IEEEsingle() ||
1293 &APF.getSemantics() == &APFloat::IEEEdouble()) {
Dale Johannesen028084e2007-09-12 03:30:33 +00001294 // We would like to output the FP constant value in exponential notation,
1295 // but we cannot do this if doing so will lose precision. Check here to
1296 // make sure that we only output it in exponential format if we can parse
1297 // the value back and get the same value.
1298 //
Dale Johannesen1f864982009-01-21 20:32:55 +00001299 bool ignored;
Serguei Katkov3a46eb42017-04-21 02:52:17 +00001300 bool isDouble = &APF.getSemantics() == &APFloat::IEEEdouble();
1301 bool isInf = APF.isInfinity();
1302 bool isNaN = APF.isNaN();
Justin Bognerb609b6b2015-12-04 01:14:24 +00001303 if (!isInf && !isNaN) {
Serguei Katkov3a46eb42017-04-21 02:52:17 +00001304 double Val = isDouble ? APF.convertToDouble() : APF.convertToFloat();
Dan Gohman518cda42011-12-17 00:04:22 +00001305 SmallString<128> StrVal;
Serguei Katkov3a46eb42017-04-21 02:52:17 +00001306 APF.toString(StrVal, 6, 0, false);
Dan Gohman518cda42011-12-17 00:04:22 +00001307 // Check to make sure that the stringized number is not some string like
1308 // "Inf" or NaN, that atof will accept, but the lexer will not. Check
1309 // that the string matches the "[-+]?[0-9]" regex.
1310 //
Serguei Katkovc9a752c2017-04-21 06:14:38 +00001311 assert(((StrVal[0] >= '0' && StrVal[0] <= '9') ||
1312 ((StrVal[0] == '-' || StrVal[0] == '+') &&
1313 (StrVal[1] >= '0' && StrVal[1] <= '9'))) &&
1314 "[-+]?[0-9] regex does not match!");
Serguei Katkov3a46eb42017-04-21 02:52:17 +00001315 // Reparse stringized version!
1316 if (APFloat(APFloat::IEEEdouble(), StrVal).convertToDouble() == Val) {
1317 Out << StrVal;
1318 return;
Dale Johannesen028084e2007-09-12 03:30:33 +00001319 }
Chris Lattner1e194682002-04-18 18:53:13 +00001320 }
Dale Johannesen028084e2007-09-12 03:30:33 +00001321 // Otherwise we could not reparse it to exactly the same value, so we must
Dale Johannesen1f864982009-01-21 20:32:55 +00001322 // output the string in hexadecimal format! Note that loading and storing
1323 // floating point types changes the bits of NaNs on some hosts, notably
1324 // x86, so we must not use these types.
Benjamin Kramer86c77412014-03-15 18:47:07 +00001325 static_assert(sizeof(double) == sizeof(uint64_t),
1326 "assuming that double is 64 bits!");
Serguei Katkov3a46eb42017-04-21 02:52:17 +00001327 APFloat apf = APF;
Justin Bognerb609b6b2015-12-04 01:14:24 +00001328 // Floats are represented in ASCII IR as double, convert.
Dale Johannesen1f864982009-01-21 20:32:55 +00001329 if (!isDouble)
Stephan Bergmann17c7f702016-12-14 11:57:17 +00001330 apf.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven,
Dale Johannesen1f864982009-01-21 20:32:55 +00001331 &ignored);
Justin Bogner93289572015-12-04 02:14:34 +00001332 Out << format_hex(apf.bitcastToAPInt().getZExtValue(), 0, /*Upper=*/true);
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001333 return;
1334 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001335
Tobias Grosser6b31d172012-05-24 15:59:06 +00001336 // Either half, or some form of long double.
1337 // These appear as a magic letter identifying the type, then a
1338 // fixed number of hex digits.
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001339 Out << "0x";
Serguei Katkov3a46eb42017-04-21 02:52:17 +00001340 APInt API = APF.bitcastToAPInt();
1341 if (&APF.getSemantics() == &APFloat::x87DoubleExtended()) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001342 Out << 'K';
Justin Bogner93289572015-12-04 02:14:34 +00001343 Out << format_hex_no_prefix(API.getHiBits(16).getZExtValue(), 4,
1344 /*Upper=*/true);
1345 Out << format_hex_no_prefix(API.getLoBits(64).getZExtValue(), 16,
1346 /*Upper=*/true);
Dale Johannesen93eefa02009-03-23 21:16:53 +00001347 return;
Serguei Katkov3a46eb42017-04-21 02:52:17 +00001348 } else if (&APF.getSemantics() == &APFloat::IEEEquad()) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001349 Out << 'L';
Justin Bogner93289572015-12-04 02:14:34 +00001350 Out << format_hex_no_prefix(API.getLoBits(64).getZExtValue(), 16,
1351 /*Upper=*/true);
1352 Out << format_hex_no_prefix(API.getHiBits(64).getZExtValue(), 16,
1353 /*Upper=*/true);
Serguei Katkov3a46eb42017-04-21 02:52:17 +00001354 } else if (&APF.getSemantics() == &APFloat::PPCDoubleDouble()) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001355 Out << 'M';
Justin Bogner93289572015-12-04 02:14:34 +00001356 Out << format_hex_no_prefix(API.getLoBits(64).getZExtValue(), 16,
1357 /*Upper=*/true);
1358 Out << format_hex_no_prefix(API.getHiBits(64).getZExtValue(), 16,
1359 /*Upper=*/true);
Serguei Katkov3a46eb42017-04-21 02:52:17 +00001360 } else if (&APF.getSemantics() == &APFloat::IEEEhalf()) {
Tobias Grosser6b31d172012-05-24 15:59:06 +00001361 Out << 'H';
Justin Bogner93289572015-12-04 02:14:34 +00001362 Out << format_hex_no_prefix(API.getZExtValue(), 4,
1363 /*Upper=*/true);
Tobias Grosser6b31d172012-05-24 15:59:06 +00001364 } else
Torok Edwinfbcc6632009-07-14 16:55:14 +00001365 llvm_unreachable("Unsupported floating point type");
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001366 return;
1367 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001368
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001369 if (isa<ConstantAggregateZero>(CV)) {
Chris Lattner76b2ff42004-02-15 05:55:15 +00001370 Out << "zeroinitializer";
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001371 return;
1372 }
Andrew Trickec4b6e72011-09-30 19:48:58 +00001373
Chris Lattner214cc702009-10-28 03:38:12 +00001374 if (const BlockAddress *BA = dyn_cast<BlockAddress>(CV)) {
1375 Out << "blockaddress(";
Dan Gohmana2489d12010-07-20 23:55:01 +00001376 WriteAsOperandInternal(Out, BA->getFunction(), &TypePrinter, Machine,
1377 Context);
Chris Lattner214cc702009-10-28 03:38:12 +00001378 Out << ", ";
Dan Gohmana2489d12010-07-20 23:55:01 +00001379 WriteAsOperandInternal(Out, BA->getBasicBlock(), &TypePrinter, Machine,
1380 Context);
Chris Lattner214cc702009-10-28 03:38:12 +00001381 Out << ")";
1382 return;
1383 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001384
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001385 if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001386 Type *ETy = CA->getType()->getElementType();
Chris Lattnercf9e8f62012-02-05 02:29:43 +00001387 Out << '[';
1388 TypePrinter.print(ETy, Out);
1389 Out << ' ';
1390 WriteAsOperandInternal(Out, CA->getOperand(0),
1391 &TypePrinter, Machine,
1392 Context);
1393 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
1394 Out << ", ";
Chris Lattner9c181f62012-01-31 03:15:40 +00001395 TypePrinter.print(ETy, Out);
1396 Out << ' ';
Chris Lattnercf9e8f62012-02-05 02:29:43 +00001397 WriteAsOperandInternal(Out, CA->getOperand(i), &TypePrinter, Machine,
Chris Lattner9c181f62012-01-31 03:15:40 +00001398 Context);
Chris Lattnerd84bb632002-04-16 21:36:08 +00001399 }
Chris Lattnercf9e8f62012-02-05 02:29:43 +00001400 Out << ']';
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001401 return;
1402 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001403
Chris Lattnerfa775002012-01-26 02:32:04 +00001404 if (const ConstantDataArray *CA = dyn_cast<ConstantDataArray>(CV)) {
1405 // As a special case, print the array as a string if it is an array of
1406 // i8 with ConstantInt values.
1407 if (CA->isString()) {
1408 Out << "c\"";
Jonas Devlieghere745918ff2018-05-31 17:01:42 +00001409 printEscapedString(CA->getAsString(), Out);
Chris Lattnerfa775002012-01-26 02:32:04 +00001410 Out << '"';
1411 return;
1412 }
1413
1414 Type *ETy = CA->getType()->getElementType();
1415 Out << '[';
Chris Lattner9c181f62012-01-31 03:15:40 +00001416 TypePrinter.print(ETy, Out);
1417 Out << ' ';
1418 WriteAsOperandInternal(Out, CA->getElementAsConstant(0),
1419 &TypePrinter, Machine,
1420 Context);
1421 for (unsigned i = 1, e = CA->getNumElements(); i != e; ++i) {
1422 Out << ", ";
Chris Lattnerfa775002012-01-26 02:32:04 +00001423 TypePrinter.print(ETy, Out);
1424 Out << ' ';
Chris Lattner9c181f62012-01-31 03:15:40 +00001425 WriteAsOperandInternal(Out, CA->getElementAsConstant(i), &TypePrinter,
1426 Machine, Context);
Chris Lattnerfa775002012-01-26 02:32:04 +00001427 }
Chris Lattner9c181f62012-01-31 03:15:40 +00001428 Out << ']';
Chris Lattnerfa775002012-01-26 02:32:04 +00001429 return;
1430 }
1431
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001432 if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
Andrew Lenharth0d124b82007-01-08 18:21:30 +00001433 if (CS->getType()->isPacked())
1434 Out << '<';
Misha Brukman21bbdb92004-06-04 21:11:51 +00001435 Out << '{';
Jim Laskey3bb78742006-02-25 12:27:03 +00001436 unsigned N = CS->getNumOperands();
1437 if (N) {
Chris Lattner604e3512008-08-19 04:47:09 +00001438 Out << ' ';
Chris Lattnere101c442009-02-28 21:26:53 +00001439 TypePrinter.print(CS->getOperand(0)->getType(), Out);
Dan Gohman81313fd2008-09-14 17:21:12 +00001440 Out << ' ';
Chris Lattnerd84bb632002-04-16 21:36:08 +00001441
Dan Gohmana2489d12010-07-20 23:55:01 +00001442 WriteAsOperandInternal(Out, CS->getOperand(0), &TypePrinter, Machine,
1443 Context);
Chris Lattnerd84bb632002-04-16 21:36:08 +00001444
Jim Laskey3bb78742006-02-25 12:27:03 +00001445 for (unsigned i = 1; i < N; i++) {
Chris Lattnerd84bb632002-04-16 21:36:08 +00001446 Out << ", ";
Chris Lattnere101c442009-02-28 21:26:53 +00001447 TypePrinter.print(CS->getOperand(i)->getType(), Out);
Dan Gohman81313fd2008-09-14 17:21:12 +00001448 Out << ' ';
Chris Lattnerd84bb632002-04-16 21:36:08 +00001449
Dan Gohmana2489d12010-07-20 23:55:01 +00001450 WriteAsOperandInternal(Out, CS->getOperand(i), &TypePrinter, Machine,
1451 Context);
Chris Lattnerd84bb632002-04-16 21:36:08 +00001452 }
Dan Gohman81313fd2008-09-14 17:21:12 +00001453 Out << ' ';
Chris Lattnerd84bb632002-04-16 21:36:08 +00001454 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001455
Dan Gohman81313fd2008-09-14 17:21:12 +00001456 Out << '}';
Andrew Lenharth0d124b82007-01-08 18:21:30 +00001457 if (CS->getType()->isPacked())
1458 Out << '>';
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001459 return;
1460 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001461
Chris Lattnerfa775002012-01-26 02:32:04 +00001462 if (isa<ConstantVector>(CV) || isa<ConstantDataVector>(CV)) {
1463 Type *ETy = CV->getType()->getVectorElementType();
Dan Gohman1262a252009-02-11 00:25:25 +00001464 Out << '<';
Chris Lattnere101c442009-02-28 21:26:53 +00001465 TypePrinter.print(ETy, Out);
Dan Gohman81313fd2008-09-14 17:21:12 +00001466 Out << ' ';
Chris Lattnerfa775002012-01-26 02:32:04 +00001467 WriteAsOperandInternal(Out, CV->getAggregateElement(0U), &TypePrinter,
1468 Machine, Context);
1469 for (unsigned i = 1, e = CV->getType()->getVectorNumElements(); i != e;++i){
Chris Lattner585297e82008-08-19 05:26:17 +00001470 Out << ", ";
Chris Lattnere101c442009-02-28 21:26:53 +00001471 TypePrinter.print(ETy, Out);
Dan Gohman81313fd2008-09-14 17:21:12 +00001472 Out << ' ';
Chris Lattnerfa775002012-01-26 02:32:04 +00001473 WriteAsOperandInternal(Out, CV->getAggregateElement(i), &TypePrinter,
1474 Machine, Context);
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001475 }
Dan Gohman1262a252009-02-11 00:25:25 +00001476 Out << '>';
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001477 return;
1478 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001479
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001480 if (isa<ConstantPointerNull>(CV)) {
Chris Lattnerd84bb632002-04-16 21:36:08 +00001481 Out << "null";
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001482 return;
1483 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001484
David Majnemerf0f224d2015-11-11 21:57:16 +00001485 if (isa<ConstantTokenNone>(CV)) {
1486 Out << "none";
1487 return;
1488 }
1489
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001490 if (isa<UndefValue>(CV)) {
Chris Lattner5e0b9f22004-10-16 18:08:06 +00001491 Out << "undef";
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001492 return;
1493 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001494
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001495 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Reid Spencer812a1be2006-12-04 05:19:18 +00001496 Out << CE->getOpcodeName();
Dan Gohman9c7f8082009-07-27 16:11:46 +00001497 WriteOptimizationInfo(Out, CE);
Reid Spencer812a1be2006-12-04 05:19:18 +00001498 if (CE->isCompare())
Tim Northoverde3aea0412016-08-17 20:25:25 +00001499 Out << ' ' << CmpInst::getPredicateName(
1500 static_cast<CmpInst::Predicate>(CE->getPredicate()));
Reid Spencer812a1be2006-12-04 05:19:18 +00001501 Out << " (";
Misha Brukmanb1c93172005-04-21 23:48:37 +00001502
Peter Collingbourned93620b2016-11-10 22:34:55 +00001503 Optional<unsigned> InRangeOp;
David Blaikief72d05b2015-03-13 18:20:45 +00001504 if (const GEPOperator *GEP = dyn_cast<GEPOperator>(CE)) {
David Blaikied06654f2015-07-29 20:26:23 +00001505 TypePrinter.print(GEP->getSourceElementType(), Out);
David Blaikief72d05b2015-03-13 18:20:45 +00001506 Out << ", ";
Peter Collingbourned93620b2016-11-10 22:34:55 +00001507 InRangeOp = GEP->getInRangeIndex();
1508 if (InRangeOp)
1509 ++*InRangeOp;
David Blaikief72d05b2015-03-13 18:20:45 +00001510 }
1511
Vikram S. Adveb952b542002-07-14 23:14:45 +00001512 for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) {
Zachary Turner804d5022016-11-11 23:58:11 +00001513 if (InRangeOp && unsigned(OI - CE->op_begin()) == *InRangeOp)
Peter Collingbourned93620b2016-11-10 22:34:55 +00001514 Out << "inrange ";
Chris Lattnere101c442009-02-28 21:26:53 +00001515 TypePrinter.print((*OI)->getType(), Out);
Dan Gohman81313fd2008-09-14 17:21:12 +00001516 Out << ' ';
Dan Gohmana2489d12010-07-20 23:55:01 +00001517 WriteAsOperandInternal(Out, *OI, &TypePrinter, Machine, Context);
Vikram S. Adveb952b542002-07-14 23:14:45 +00001518 if (OI+1 != CE->op_end())
Chris Lattner3cd8c562002-07-30 18:54:25 +00001519 Out << ", ";
Vikram S. Adveb952b542002-07-14 23:14:45 +00001520 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001521
Dan Gohmana76f0f72008-05-31 19:12:39 +00001522 if (CE->hasIndices()) {
Jay Foad0091fe82011-04-13 15:22:40 +00001523 ArrayRef<unsigned> Indices = CE->getIndices();
Dan Gohmana76f0f72008-05-31 19:12:39 +00001524 for (unsigned i = 0, e = Indices.size(); i != e; ++i)
1525 Out << ", " << Indices[i];
1526 }
1527
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001528 if (CE->isCast()) {
Chris Lattner83b396b2002-08-15 19:37:43 +00001529 Out << " to ";
Chris Lattnere101c442009-02-28 21:26:53 +00001530 TypePrinter.print(CE->getType(), Out);
Chris Lattner83b396b2002-08-15 19:37:43 +00001531 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001532
Misha Brukman21bbdb92004-06-04 21:11:51 +00001533 Out << ')';
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001534 return;
Chris Lattnerd84bb632002-04-16 21:36:08 +00001535 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001536
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001537 Out << "<placeholder or erroneous Constant>";
Chris Lattnerd84bb632002-04-16 21:36:08 +00001538}
1539
Duncan P. N. Exon Smitha6de6a42015-01-12 23:45:31 +00001540static void writeMDTuple(raw_ostream &Out, const MDTuple *Node,
1541 TypePrinting *TypePrinter, SlotTracker *Machine,
1542 const Module *Context) {
Chris Lattnercdec5812009-12-31 02:31:59 +00001543 Out << "!{";
1544 for (unsigned mi = 0, me = Node->getNumOperands(); mi != me; ++mi) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001545 const Metadata *MD = Node->getOperand(mi);
1546 if (!MD)
Chris Lattnercdec5812009-12-31 02:31:59 +00001547 Out << "null";
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001548 else if (auto *MDV = dyn_cast<ValueAsMetadata>(MD)) {
1549 Value *V = MDV->getValue();
Chris Lattnercdec5812009-12-31 02:31:59 +00001550 TypePrinter->print(V->getType(), Out);
1551 Out << ' ';
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001552 WriteAsOperandInternal(Out, V, TypePrinter, Machine, Context);
1553 } else {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001554 WriteAsOperandInternal(Out, MD, TypePrinter, Machine, Context);
Chris Lattnercdec5812009-12-31 02:31:59 +00001555 }
1556 if (mi + 1 != me)
1557 Out << ", ";
1558 }
Andrew Trickec4b6e72011-09-30 19:48:58 +00001559
Chris Lattnercdec5812009-12-31 02:31:59 +00001560 Out << "}";
1561}
1562
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +00001563namespace {
Eugene Zelenkode6cce22017-06-19 22:05:08 +00001564
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +00001565struct FieldSeparator {
Eugene Zelenkode6cce22017-06-19 22:05:08 +00001566 bool Skip = true;
Duncan P. N. Exon Smith70ab3d22015-02-21 01:02:18 +00001567 const char *Sep;
Eugene Zelenkode6cce22017-06-19 22:05:08 +00001568
1569 FieldSeparator(const char *Sep = ", ") : Sep(Sep) {}
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +00001570};
Eugene Zelenkode6cce22017-06-19 22:05:08 +00001571
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +00001572raw_ostream &operator<<(raw_ostream &OS, FieldSeparator &FS) {
1573 if (FS.Skip) {
1574 FS.Skip = false;
1575 return OS;
1576 }
Duncan P. N. Exon Smith70ab3d22015-02-21 01:02:18 +00001577 return OS << FS.Sep;
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +00001578}
Eugene Zelenkode6cce22017-06-19 22:05:08 +00001579
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001580struct MDFieldPrinter {
1581 raw_ostream &Out;
1582 FieldSeparator FS;
Eugene Zelenkode6cce22017-06-19 22:05:08 +00001583 TypePrinting *TypePrinter = nullptr;
1584 SlotTracker *Machine = nullptr;
1585 const Module *Context = nullptr;
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001586
Eugene Zelenkode6cce22017-06-19 22:05:08 +00001587 explicit MDFieldPrinter(raw_ostream &Out) : Out(Out) {}
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001588 MDFieldPrinter(raw_ostream &Out, TypePrinting *TypePrinter,
1589 SlotTracker *Machine, const Module *Context)
1590 : Out(Out), TypePrinter(TypePrinter), Machine(Machine), Context(Context) {
1591 }
Eugene Zelenkode6cce22017-06-19 22:05:08 +00001592
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001593 void printTag(const DINode *N);
Amjad Abouda9bcf162015-12-10 12:56:35 +00001594 void printMacinfoType(const DIMacroNode *N);
Scott Linder71603842018-02-12 19:45:54 +00001595 void printChecksum(const DIFile::ChecksumInfo<StringRef> &N);
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001596 void printString(StringRef Name, StringRef Value,
1597 bool ShouldSkipEmpty = true);
1598 void printMetadata(StringRef Name, const Metadata *MD,
1599 bool ShouldSkipNull = true);
1600 template <class IntTy>
1601 void printInt(StringRef Name, IntTy Int, bool ShouldSkipZero = true);
David Blaikiea01f2952016-08-24 18:29:49 +00001602 void printBool(StringRef Name, bool Value, Optional<bool> Default = None);
Leny Kholodov5fcc4182016-09-06 10:46:28 +00001603 void printDIFlags(StringRef Name, DINode::DIFlags Flags);
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001604 template <class IntTy, class Stringifier>
1605 void printDwarfEnum(StringRef Name, IntTy Value, Stringifier toString,
1606 bool ShouldSkipZero = true);
Adrian Prantlb939a252016-03-31 23:56:58 +00001607 void printEmissionKind(StringRef Name, DICompileUnit::DebugEmissionKind EK);
David Blaikie66cf14d2018-08-16 21:29:55 +00001608 void printNameTableKind(StringRef Name,
1609 DICompileUnit::DebugNameTableKind NTK);
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001610};
Eugene Zelenkode6cce22017-06-19 22:05:08 +00001611
1612} // end anonymous namespace
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +00001613
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001614void MDFieldPrinter::printTag(const DINode *N) {
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001615 Out << FS << "tag: ";
Mehdi Amini149f6ea2016-10-05 05:59:29 +00001616 auto Tag = dwarf::TagString(N->getTag());
1617 if (!Tag.empty())
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001618 Out << Tag;
1619 else
1620 Out << N->getTag();
1621}
1622
Amjad Abouda9bcf162015-12-10 12:56:35 +00001623void MDFieldPrinter::printMacinfoType(const DIMacroNode *N) {
1624 Out << FS << "type: ";
Mehdi Amini149f6ea2016-10-05 05:59:29 +00001625 auto Type = dwarf::MacinfoString(N->getMacinfoType());
1626 if (!Type.empty())
Amjad Abouda9bcf162015-12-10 12:56:35 +00001627 Out << Type;
1628 else
1629 Out << N->getMacinfoType();
1630}
1631
Scott Linder71603842018-02-12 19:45:54 +00001632void MDFieldPrinter::printChecksum(
1633 const DIFile::ChecksumInfo<StringRef> &Checksum) {
1634 Out << FS << "checksumkind: " << Checksum.getKindAsString();
1635 printString("checksum", Checksum.Value, /* ShouldSkipEmpty */ false);
Amjad Aboud7faeecc2016-12-25 10:12:09 +00001636}
1637
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001638void MDFieldPrinter::printString(StringRef Name, StringRef Value,
1639 bool ShouldSkipEmpty) {
1640 if (ShouldSkipEmpty && Value.empty())
1641 return;
1642
1643 Out << FS << Name << ": \"";
Jonas Devlieghere745918ff2018-05-31 17:01:42 +00001644 printEscapedString(Value, Out);
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001645 Out << "\"";
1646}
1647
Duncan P. N. Exon Smith61a09332015-02-06 22:27:22 +00001648static void writeMetadataAsOperand(raw_ostream &Out, const Metadata *MD,
1649 TypePrinting *TypePrinter,
1650 SlotTracker *Machine,
1651 const Module *Context) {
1652 if (!MD) {
1653 Out << "null";
1654 return;
1655 }
1656 WriteAsOperandInternal(Out, MD, TypePrinter, Machine, Context);
1657}
1658
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001659void MDFieldPrinter::printMetadata(StringRef Name, const Metadata *MD,
1660 bool ShouldSkipNull) {
1661 if (ShouldSkipNull && !MD)
Duncan P. N. Exon Smith79cf9702015-02-28 22:16:56 +00001662 return;
1663
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001664 Out << FS << Name << ": ";
1665 writeMetadataAsOperand(Out, MD, TypePrinter, Machine, Context);
1666}
1667
1668template <class IntTy>
1669void MDFieldPrinter::printInt(StringRef Name, IntTy Int, bool ShouldSkipZero) {
1670 if (ShouldSkipZero && !Int)
1671 return;
1672
1673 Out << FS << Name << ": " << Int;
1674}
1675
David Blaikiea01f2952016-08-24 18:29:49 +00001676void MDFieldPrinter::printBool(StringRef Name, bool Value,
1677 Optional<bool> Default) {
1678 if (Default && Value == *Default)
1679 return;
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001680 Out << FS << Name << ": " << (Value ? "true" : "false");
1681}
1682
Leny Kholodov5fcc4182016-09-06 10:46:28 +00001683void MDFieldPrinter::printDIFlags(StringRef Name, DINode::DIFlags Flags) {
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001684 if (!Flags)
1685 return;
1686
1687 Out << FS << Name << ": ";
1688
Leny Kholodov5fcc4182016-09-06 10:46:28 +00001689 SmallVector<DINode::DIFlags, 8> SplitFlags;
1690 auto Extra = DINode::splitFlags(Flags, SplitFlags);
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001691
1692 FieldSeparator FlagsFS(" | ");
Leny Kholodov5fcc4182016-09-06 10:46:28 +00001693 for (auto F : SplitFlags) {
Mehdi Aminif42ec792016-10-01 05:57:50 +00001694 auto StringF = DINode::getFlagString(F);
1695 assert(!StringF.empty() && "Expected valid flag");
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001696 Out << FlagsFS << StringF;
1697 }
1698 if (Extra || SplitFlags.empty())
1699 Out << FlagsFS << Extra;
1700}
1701
Adrian Prantlb939a252016-03-31 23:56:58 +00001702void MDFieldPrinter::printEmissionKind(StringRef Name,
1703 DICompileUnit::DebugEmissionKind EK) {
Fangrui Song3c1b5db2018-07-06 19:26:00 +00001704 Out << FS << Name << ": " << DICompileUnit::emissionKindString(EK);
Adrian Prantlb939a252016-03-31 23:56:58 +00001705}
1706
David Blaikie66cf14d2018-08-16 21:29:55 +00001707void MDFieldPrinter::printNameTableKind(StringRef Name,
1708 DICompileUnit::DebugNameTableKind NTK) {
1709 if (NTK == DICompileUnit::DebugNameTableKind::Default)
1710 return;
1711 Out << FS << Name << ": " << DICompileUnit::nameTableKindString(NTK);
1712}
1713
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001714template <class IntTy, class Stringifier>
1715void MDFieldPrinter::printDwarfEnum(StringRef Name, IntTy Value,
1716 Stringifier toString, bool ShouldSkipZero) {
1717 if (!Value)
1718 return;
1719
1720 Out << FS << Name << ": ";
Mehdi Amini149f6ea2016-10-05 05:59:29 +00001721 auto S = toString(Value);
1722 if (!S.empty())
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001723 Out << S;
1724 else
1725 Out << Value;
Duncan P. N. Exon Smith79cf9702015-02-28 22:16:56 +00001726}
1727
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001728static void writeGenericDINode(raw_ostream &Out, const GenericDINode *N,
1729 TypePrinting *TypePrinter, SlotTracker *Machine,
1730 const Module *Context) {
1731 Out << "!GenericDINode(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001732 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1733 Printer.printTag(N);
1734 Printer.printString("header", N->getHeader());
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00001735 if (N->getNumDwarfOperands()) {
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001736 Out << Printer.FS << "operands: {";
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00001737 FieldSeparator IFS;
1738 for (auto &I : N->dwarf_operands()) {
1739 Out << IFS;
Duncan P. N. Exon Smith61a09332015-02-06 22:27:22 +00001740 writeMetadataAsOperand(Out, I, TypePrinter, Machine, Context);
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00001741 }
1742 Out << "}";
1743 }
1744 Out << ")";
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +00001745}
1746
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001747static void writeDILocation(raw_ostream &Out, const DILocation *DL,
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +00001748 TypePrinting *TypePrinter, SlotTracker *Machine,
1749 const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001750 Out << "!DILocation(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001751 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
Duncan P. N. Exon Smith503cf3b2015-01-14 22:14:26 +00001752 // Always output the line, since 0 is a relevant and important value for it.
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001753 Printer.printInt("line", DL->getLine(), /* ShouldSkipZero */ false);
1754 Printer.printInt("column", DL->getColumn());
1755 Printer.printMetadata("scope", DL->getRawScope(), /* ShouldSkipNull */ false);
1756 Printer.printMetadata("inlinedAt", DL->getRawInlinedAt());
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +00001757 Out << ")";
1758}
1759
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001760static void writeDISubrange(raw_ostream &Out, const DISubrange *N,
Sander de Smalenfdf40912018-01-24 09:56:07 +00001761 TypePrinting *TypePrinter, SlotTracker *Machine,
1762 const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001763 Out << "!DISubrange(";
Sander de Smalenfdf40912018-01-24 09:56:07 +00001764 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1765 if (auto *CE = N->getCount().dyn_cast<ConstantInt*>())
1766 Printer.printInt("count", CE->getSExtValue(), /* ShouldSkipZero */ false);
1767 else
1768 Printer.printMetadata("count", N->getCount().dyn_cast<DIVariable*>(),
1769 /*ShouldSkipNull */ false);
Duncan P. N. Exon Smith5dcf6212015-04-07 00:39:59 +00001770 Printer.printInt("lowerBound", N->getLowerBound());
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00001771 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001772}
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00001773
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001774static void writeDIEnumerator(raw_ostream &Out, const DIEnumerator *N,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001775 TypePrinting *, SlotTracker *, const Module *) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001776 Out << "!DIEnumerator(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001777 MDFieldPrinter Printer(Out);
1778 Printer.printString("name", N->getName(), /* ShouldSkipEmpty */ false);
Momchil Velikov08dc66e2018-02-12 16:10:09 +00001779 if (N->isUnsigned()) {
1780 auto Value = static_cast<uint64_t>(N->getValue());
1781 Printer.printInt("value", Value, /* ShouldSkipZero */ false);
1782 Printer.printBool("isUnsigned", true);
1783 } else {
1784 Printer.printInt("value", N->getValue(), /* ShouldSkipZero */ false);
1785 }
Duncan P. N. Exon Smith87754762015-02-13 01:14:11 +00001786 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001787}
Duncan P. N. Exon Smith87754762015-02-13 01:14:11 +00001788
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001789static void writeDIBasicType(raw_ostream &Out, const DIBasicType *N,
Duncan P. N. Exon Smith09e03f32015-02-13 01:14:58 +00001790 TypePrinting *, SlotTracker *, const Module *) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001791 Out << "!DIBasicType(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001792 MDFieldPrinter Printer(Out);
Duncan P. N. Exon Smith16d182a2015-02-28 23:21:38 +00001793 if (N->getTag() != dwarf::DW_TAG_base_type)
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001794 Printer.printTag(N);
1795 Printer.printString("name", N->getName());
1796 Printer.printInt("size", N->getSizeInBits());
1797 Printer.printInt("align", N->getAlignInBits());
1798 Printer.printDwarfEnum("encoding", N->getEncoding(),
1799 dwarf::AttributeEncodingString);
Adrian Prantl55f42622018-08-14 19:35:34 +00001800 Printer.printDIFlags("flags", N->getFlags());
Duncan P. N. Exon Smith09e03f32015-02-13 01:14:58 +00001801 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001802}
Duncan P. N. Exon Smith09e03f32015-02-13 01:14:58 +00001803
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001804static void writeDIDerivedType(raw_ostream &Out, const DIDerivedType *N,
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001805 TypePrinting *TypePrinter, SlotTracker *Machine,
1806 const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001807 Out << "!DIDerivedType(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001808 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1809 Printer.printTag(N);
1810 Printer.printString("name", N->getName());
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001811 Printer.printMetadata("scope", N->getRawScope());
1812 Printer.printMetadata("file", N->getRawFile());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001813 Printer.printInt("line", N->getLine());
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001814 Printer.printMetadata("baseType", N->getRawBaseType(),
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001815 /* ShouldSkipNull */ false);
1816 Printer.printInt("size", N->getSizeInBits());
1817 Printer.printInt("align", N->getAlignInBits());
1818 Printer.printInt("offset", N->getOffsetInBits());
1819 Printer.printDIFlags("flags", N->getFlags());
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001820 Printer.printMetadata("extraData", N->getRawExtraData());
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +00001821 if (const auto &DWARFAddressSpace = N->getDWARFAddressSpace())
1822 Printer.printInt("dwarfAddressSpace", *DWARFAddressSpace,
1823 /* ShouldSkipZero */ false);
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001824 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001825}
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001826
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001827static void writeDICompositeType(raw_ostream &Out, const DICompositeType *N,
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001828 TypePrinting *TypePrinter,
1829 SlotTracker *Machine, const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001830 Out << "!DICompositeType(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001831 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1832 Printer.printTag(N);
1833 Printer.printString("name", N->getName());
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001834 Printer.printMetadata("scope", N->getRawScope());
1835 Printer.printMetadata("file", N->getRawFile());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001836 Printer.printInt("line", N->getLine());
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001837 Printer.printMetadata("baseType", N->getRawBaseType());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001838 Printer.printInt("size", N->getSizeInBits());
1839 Printer.printInt("align", N->getAlignInBits());
1840 Printer.printInt("offset", N->getOffsetInBits());
1841 Printer.printDIFlags("flags", N->getFlags());
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001842 Printer.printMetadata("elements", N->getRawElements());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001843 Printer.printDwarfEnum("runtimeLang", N->getRuntimeLang(),
1844 dwarf::LanguageString);
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001845 Printer.printMetadata("vtableHolder", N->getRawVTableHolder());
1846 Printer.printMetadata("templateParams", N->getRawTemplateParams());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001847 Printer.printString("identifier", N->getIdentifier());
Adrian Prantl8c599212018-02-06 23:45:59 +00001848 Printer.printMetadata("discriminator", N->getRawDiscriminator());
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001849 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001850}
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001851
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001852static void writeDISubroutineType(raw_ostream &Out, const DISubroutineType *N,
Duncan P. N. Exon Smith54e2bc62015-02-13 01:22:59 +00001853 TypePrinting *TypePrinter,
1854 SlotTracker *Machine, const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001855 Out << "!DISubroutineType(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001856 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1857 Printer.printDIFlags("flags", N->getFlags());
Reid Klecknerde3d8b52016-06-08 20:34:29 +00001858 Printer.printDwarfEnum("cc", N->getCC(), dwarf::ConventionString);
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001859 Printer.printMetadata("types", N->getRawTypeArray(),
1860 /* ShouldSkipNull */ false);
Duncan P. N. Exon Smith54e2bc62015-02-13 01:22:59 +00001861 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001862}
Duncan P. N. Exon Smithf14b9c72015-02-13 01:19:14 +00001863
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001864static void writeDIFile(raw_ostream &Out, const DIFile *N, TypePrinting *,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001865 SlotTracker *, const Module *) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001866 Out << "!DIFile(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001867 MDFieldPrinter Printer(Out);
1868 Printer.printString("filename", N->getFilename(),
1869 /* ShouldSkipEmpty */ false);
1870 Printer.printString("directory", N->getDirectory(),
1871 /* ShouldSkipEmpty */ false);
Scott Linder71603842018-02-12 19:45:54 +00001872 // Print all values for checksum together, or not at all.
1873 if (N->getChecksum())
1874 Printer.printChecksum(*N->getChecksum());
Scott Linder16c7bda2018-02-23 23:01:06 +00001875 Printer.printString("source", N->getSource().getValueOr(StringRef()),
1876 /* ShouldSkipEmpty */ true);
Duncan P. N. Exon Smithf14b9c72015-02-13 01:19:14 +00001877 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001878}
Duncan P. N. Exon Smithf14b9c72015-02-13 01:19:14 +00001879
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001880static void writeDICompileUnit(raw_ostream &Out, const DICompileUnit *N,
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00001881 TypePrinting *TypePrinter, SlotTracker *Machine,
1882 const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001883 Out << "!DICompileUnit(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001884 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1885 Printer.printDwarfEnum("language", N->getSourceLanguage(),
1886 dwarf::LanguageString, /* ShouldSkipZero */ false);
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001887 Printer.printMetadata("file", N->getRawFile(), /* ShouldSkipNull */ false);
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001888 Printer.printString("producer", N->getProducer());
1889 Printer.printBool("isOptimized", N->isOptimized());
1890 Printer.printString("flags", N->getFlags());
1891 Printer.printInt("runtimeVersion", N->getRuntimeVersion(),
1892 /* ShouldSkipZero */ false);
1893 Printer.printString("splitDebugFilename", N->getSplitDebugFilename());
Adrian Prantlb939a252016-03-31 23:56:58 +00001894 Printer.printEmissionKind("emissionKind", N->getEmissionKind());
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001895 Printer.printMetadata("enums", N->getRawEnumTypes());
1896 Printer.printMetadata("retainedTypes", N->getRawRetainedTypes());
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001897 Printer.printMetadata("globals", N->getRawGlobalVariables());
1898 Printer.printMetadata("imports", N->getRawImportedEntities());
Amjad Abouda9bcf162015-12-10 12:56:35 +00001899 Printer.printMetadata("macros", N->getRawMacros());
Adrian Prantl1f599f92015-05-21 20:37:30 +00001900 Printer.printInt("dwoId", N->getDWOId());
David Blaikiea01f2952016-08-24 18:29:49 +00001901 Printer.printBool("splitDebugInlining", N->getSplitDebugInlining(), true);
Dehao Chen0944a8c2017-02-01 22:45:09 +00001902 Printer.printBool("debugInfoForProfiling", N->getDebugInfoForProfiling(),
1903 false);
David Blaikie66cf14d2018-08-16 21:29:55 +00001904 Printer.printNameTableKind("nameTableKind", N->getNameTableKind());
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00001905 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001906}
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00001907
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001908static void writeDISubprogram(raw_ostream &Out, const DISubprogram *N,
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00001909 TypePrinting *TypePrinter, SlotTracker *Machine,
1910 const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001911 Out << "!DISubprogram(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001912 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1913 Printer.printString("name", N->getName());
1914 Printer.printString("linkageName", N->getLinkageName());
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001915 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
1916 Printer.printMetadata("file", N->getRawFile());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001917 Printer.printInt("line", N->getLine());
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001918 Printer.printMetadata("type", N->getRawType());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001919 Printer.printBool("isLocal", N->isLocalToUnit());
1920 Printer.printBool("isDefinition", N->isDefinition());
1921 Printer.printInt("scopeLine", N->getScopeLine());
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001922 Printer.printMetadata("containingType", N->getRawContainingType());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001923 Printer.printDwarfEnum("virtuality", N->getVirtuality(),
1924 dwarf::VirtualityString);
Peter Collingbournea1f86252016-03-17 23:58:03 +00001925 if (N->getVirtuality() != dwarf::DW_VIRTUALITY_none ||
1926 N->getVirtualIndex() != 0)
1927 Printer.printInt("virtualIndex", N->getVirtualIndex(), false);
Reid Klecknerb5af11d2016-07-01 02:41:21 +00001928 Printer.printInt("thisAdjustment", N->getThisAdjustment());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001929 Printer.printDIFlags("flags", N->getFlags());
1930 Printer.printBool("isOptimized", N->isOptimized());
Adrian Prantl75819ae2016-04-15 15:57:41 +00001931 Printer.printMetadata("unit", N->getRawUnit());
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001932 Printer.printMetadata("templateParams", N->getRawTemplateParams());
1933 Printer.printMetadata("declaration", N->getRawDeclaration());
Shiva Chen2c864552018-05-09 02:40:45 +00001934 Printer.printMetadata("retainedNodes", N->getRawRetainedNodes());
Adrian Prantl1d12b882017-04-26 22:56:44 +00001935 Printer.printMetadata("thrownTypes", N->getRawThrownTypes());
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00001936 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001937}
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00001938
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001939static void writeDILexicalBlock(raw_ostream &Out, const DILexicalBlock *N,
1940 TypePrinting *TypePrinter, SlotTracker *Machine,
1941 const Module *Context) {
1942 Out << "!DILexicalBlock(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001943 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
Duncan P. N. Exon Smith0e202b92015-03-30 16:37:48 +00001944 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
1945 Printer.printMetadata("file", N->getRawFile());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001946 Printer.printInt("line", N->getLine());
1947 Printer.printInt("column", N->getColumn());
Duncan P. N. Exon Smitha96d4092015-02-13 01:29:28 +00001948 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001949}
Duncan P. N. Exon Smitha96d4092015-02-13 01:29:28 +00001950
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001951static void writeDILexicalBlockFile(raw_ostream &Out,
1952 const DILexicalBlockFile *N,
Duncan P. N. Exon Smith06a07022015-02-13 01:30:42 +00001953 TypePrinting *TypePrinter,
1954 SlotTracker *Machine,
1955 const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001956 Out << "!DILexicalBlockFile(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001957 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
Duncan P. N. Exon Smith0e202b92015-03-30 16:37:48 +00001958 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
1959 Printer.printMetadata("file", N->getRawFile());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001960 Printer.printInt("discriminator", N->getDiscriminator(),
1961 /* ShouldSkipZero */ false);
Duncan P. N. Exon Smith06a07022015-02-13 01:30:42 +00001962 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001963}
Duncan P. N. Exon Smith06a07022015-02-13 01:30:42 +00001964
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001965static void writeDINamespace(raw_ostream &Out, const DINamespace *N,
Duncan P. N. Exon Smithe1460002015-02-13 01:32:09 +00001966 TypePrinting *TypePrinter, SlotTracker *Machine,
1967 const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001968 Out << "!DINamespace(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001969 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1970 Printer.printString("name", N->getName());
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00001971 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
Adrian Prantldbfda632016-11-03 19:42:02 +00001972 Printer.printBool("exportSymbols", N->getExportSymbols(), false);
Duncan P. N. Exon Smithe1460002015-02-13 01:32:09 +00001973 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001974}
Duncan P. N. Exon Smithe1460002015-02-13 01:32:09 +00001975
Amjad Abouda9bcf162015-12-10 12:56:35 +00001976static void writeDIMacro(raw_ostream &Out, const DIMacro *N,
1977 TypePrinting *TypePrinter, SlotTracker *Machine,
1978 const Module *Context) {
1979 Out << "!DIMacro(";
1980 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1981 Printer.printMacinfoType(N);
1982 Printer.printInt("line", N->getLine());
1983 Printer.printString("name", N->getName());
1984 Printer.printString("value", N->getValue());
1985 Out << ")";
1986}
1987
1988static void writeDIMacroFile(raw_ostream &Out, const DIMacroFile *N,
1989 TypePrinting *TypePrinter, SlotTracker *Machine,
1990 const Module *Context) {
1991 Out << "!DIMacroFile(";
1992 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1993 Printer.printInt("line", N->getLine());
1994 Printer.printMetadata("file", N->getRawFile(), /* ShouldSkipNull */ false);
1995 Printer.printMetadata("nodes", N->getRawElements());
1996 Out << ")";
1997}
1998
Adrian Prantlab1243f2015-06-29 23:03:47 +00001999static void writeDIModule(raw_ostream &Out, const DIModule *N,
2000 TypePrinting *TypePrinter, SlotTracker *Machine,
2001 const Module *Context) {
2002 Out << "!DIModule(";
2003 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
2004 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
2005 Printer.printString("name", N->getName());
2006 Printer.printString("configMacros", N->getConfigurationMacros());
2007 Printer.printString("includePath", N->getIncludePath());
2008 Printer.printString("isysroot", N->getISysRoot());
2009 Out << ")";
2010}
2011
2012
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002013static void writeDITemplateTypeParameter(raw_ostream &Out,
2014 const DITemplateTypeParameter *N,
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00002015 TypePrinting *TypePrinter,
2016 SlotTracker *Machine,
2017 const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002018 Out << "!DITemplateTypeParameter(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00002019 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
2020 Printer.printString("name", N->getName());
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00002021 Printer.printMetadata("type", N->getRawType(), /* ShouldSkipNull */ false);
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00002022 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002023}
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00002024
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002025static void writeDITemplateValueParameter(raw_ostream &Out,
2026 const DITemplateValueParameter *N,
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00002027 TypePrinting *TypePrinter,
2028 SlotTracker *Machine,
2029 const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002030 Out << "!DITemplateValueParameter(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00002031 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
Duncan P. N. Exon Smith16d182a2015-02-28 23:21:38 +00002032 if (N->getTag() != dwarf::DW_TAG_template_value_parameter)
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00002033 Printer.printTag(N);
2034 Printer.printString("name", N->getName());
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00002035 Printer.printMetadata("type", N->getRawType());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00002036 Printer.printMetadata("value", N->getValue(), /* ShouldSkipNull */ false);
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00002037 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002038}
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00002039
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002040static void writeDIGlobalVariable(raw_ostream &Out, const DIGlobalVariable *N,
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00002041 TypePrinting *TypePrinter,
2042 SlotTracker *Machine, const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002043 Out << "!DIGlobalVariable(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00002044 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
2045 Printer.printString("name", N->getName());
2046 Printer.printString("linkageName", N->getLinkageName());
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00002047 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
2048 Printer.printMetadata("file", N->getRawFile());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00002049 Printer.printInt("line", N->getLine());
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00002050 Printer.printMetadata("type", N->getRawType());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00002051 Printer.printBool("isLocal", N->isLocalToUnit());
2052 Printer.printBool("isDefinition", N->isDefinition());
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00002053 Printer.printMetadata("declaration", N->getRawStaticDataMemberDeclaration());
Victor Leschuk2ede1262016-10-20 00:13:12 +00002054 Printer.printInt("align", N->getAlignInBits());
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00002055 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002056}
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00002057
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002058static void writeDILocalVariable(raw_ostream &Out, const DILocalVariable *N,
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00002059 TypePrinting *TypePrinter,
2060 SlotTracker *Machine, const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002061 Out << "!DILocalVariable(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00002062 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00002063 Printer.printString("name", N->getName());
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00002064 Printer.printInt("arg", N->getArg());
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00002065 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
2066 Printer.printMetadata("file", N->getRawFile());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00002067 Printer.printInt("line", N->getLine());
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00002068 Printer.printMetadata("type", N->getRawType());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00002069 Printer.printDIFlags("flags", N->getFlags());
Victor Leschuk2ede1262016-10-20 00:13:12 +00002070 Printer.printInt("align", N->getAlignInBits());
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00002071 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002072}
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00002073
Shiva Chen2c864552018-05-09 02:40:45 +00002074static void writeDILabel(raw_ostream &Out, const DILabel *N,
2075 TypePrinting *TypePrinter,
2076 SlotTracker *Machine, const Module *Context) {
2077 Out << "!DILabel(";
2078 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
2079 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
2080 Printer.printString("name", N->getName());
2081 Printer.printMetadata("file", N->getRawFile());
2082 Printer.printInt("line", N->getLine());
2083 Out << ")";
2084}
2085
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002086static void writeDIExpression(raw_ostream &Out, const DIExpression *N,
Duncan P. N. Exon Smith0c5c0122015-02-13 01:42:09 +00002087 TypePrinting *TypePrinter, SlotTracker *Machine,
2088 const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002089 Out << "!DIExpression(";
Duncan P. N. Exon Smith0c5c0122015-02-13 01:42:09 +00002090 FieldSeparator FS;
2091 if (N->isValid()) {
2092 for (auto I = N->expr_op_begin(), E = N->expr_op_end(); I != E; ++I) {
Mehdi Amini149f6ea2016-10-05 05:59:29 +00002093 auto OpStr = dwarf::OperationEncodingString(I->getOp());
2094 assert(!OpStr.empty() && "Expected valid opcode");
Duncan P. N. Exon Smith0c5c0122015-02-13 01:42:09 +00002095
2096 Out << FS << OpStr;
2097 for (unsigned A = 0, AE = I->getNumArgs(); A != AE; ++A)
2098 Out << FS << I->getArg(A);
2099 }
2100 } else {
2101 for (const auto &I : N->getElements())
2102 Out << FS << I;
2103 }
2104 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002105}
Duncan P. N. Exon Smith0c5c0122015-02-13 01:42:09 +00002106
Adrian Prantlbceaaa92016-12-20 02:09:43 +00002107static void writeDIGlobalVariableExpression(raw_ostream &Out,
2108 const DIGlobalVariableExpression *N,
2109 TypePrinting *TypePrinter,
2110 SlotTracker *Machine,
2111 const Module *Context) {
2112 Out << "!DIGlobalVariableExpression(";
2113 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
2114 Printer.printMetadata("var", N->getVariable());
2115 Printer.printMetadata("expr", N->getExpression());
2116 Out << ")";
2117}
2118
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002119static void writeDIObjCProperty(raw_ostream &Out, const DIObjCProperty *N,
Duncan P. N. Exon Smithd45ce962015-02-13 01:43:22 +00002120 TypePrinting *TypePrinter, SlotTracker *Machine,
2121 const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002122 Out << "!DIObjCProperty(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00002123 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
2124 Printer.printString("name", N->getName());
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00002125 Printer.printMetadata("file", N->getRawFile());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00002126 Printer.printInt("line", N->getLine());
2127 Printer.printString("setter", N->getSetterName());
2128 Printer.printString("getter", N->getGetterName());
2129 Printer.printInt("attributes", N->getAttributes());
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00002130 Printer.printMetadata("type", N->getRawType());
Duncan P. N. Exon Smithd45ce962015-02-13 01:43:22 +00002131 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002132}
Duncan P. N. Exon Smithd45ce962015-02-13 01:43:22 +00002133
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002134static void writeDIImportedEntity(raw_ostream &Out, const DIImportedEntity *N,
Duncan P. N. Exon Smith1c931162015-02-13 01:46:02 +00002135 TypePrinting *TypePrinter,
2136 SlotTracker *Machine, const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002137 Out << "!DIImportedEntity(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00002138 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
2139 Printer.printTag(N);
2140 Printer.printString("name", N->getName());
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00002141 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
2142 Printer.printMetadata("entity", N->getRawEntity());
Adrian Prantld63bfd22017-07-19 00:09:54 +00002143 Printer.printMetadata("file", N->getRawFile());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00002144 Printer.printInt("line", N->getLine());
Duncan P. N. Exon Smith1c931162015-02-13 01:46:02 +00002145 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002146}
2147
Duncan P. N. Exon Smitha6de6a42015-01-12 23:45:31 +00002148static void WriteMDNodeBodyInternal(raw_ostream &Out, const MDNode *Node,
2149 TypePrinting *TypePrinter,
2150 SlotTracker *Machine,
2151 const Module *Context) {
Duncan P. N. Exon Smith2bc00f42015-01-19 23:13:14 +00002152 if (Node->isDistinct())
Duncan P. N. Exon Smitha6de6a42015-01-12 23:45:31 +00002153 Out << "distinct ";
Duncan P. N. Exon Smith3d510662015-03-16 21:21:10 +00002154 else if (Node->isTemporary())
2155 Out << "<temporary!> "; // Handle broken code.
Duncan P. N. Exon Smitha6de6a42015-01-12 23:45:31 +00002156
Duncan P. N. Exon Smith2bc00f42015-01-19 23:13:14 +00002157 switch (Node->getMetadataID()) {
Duncan P. N. Exon Smitha6de6a42015-01-12 23:45:31 +00002158 default:
2159 llvm_unreachable("Expected uniquable MDNode");
Duncan P. N. Exon Smith2bc00f42015-01-19 23:13:14 +00002160#define HANDLE_MDNODE_LEAF(CLASS) \
Duncan P. N. Exon Smitha6de6a42015-01-12 23:45:31 +00002161 case Metadata::CLASS##Kind: \
Duncan P. N. Exon Smith2bc00f42015-01-19 23:13:14 +00002162 write##CLASS(Out, cast<CLASS>(Node), TypePrinter, Machine, Context); \
Duncan P. N. Exon Smitha6de6a42015-01-12 23:45:31 +00002163 break;
2164#include "llvm/IR/Metadata.def"
2165 }
2166}
2167
Chandler Carruthd48cdbf2014-01-09 02:29:41 +00002168// Full implementation of printing a Value as an operand with support for
2169// TypePrinting, etc.
Dan Gohman12dad632009-08-12 20:56:03 +00002170static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
Dan Gohman8061d9e2009-08-13 15:27:57 +00002171 TypePrinting *TypePrinter,
Dan Gohmana2489d12010-07-20 23:55:01 +00002172 SlotTracker *Machine,
2173 const Module *Context) {
Chris Lattner033935d2008-08-17 04:40:13 +00002174 if (V->hasName()) {
2175 PrintLLVMName(Out, V);
2176 return;
2177 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002178
Chris Lattner033935d2008-08-17 04:40:13 +00002179 const Constant *CV = dyn_cast<Constant>(V);
2180 if (CV && !isa<GlobalValue>(CV)) {
Dan Gohman8061d9e2009-08-13 15:27:57 +00002181 assert(TypePrinter && "Constants require TypePrinting!");
Dan Gohmana2489d12010-07-20 23:55:01 +00002182 WriteConstantInternal(Out, CV, *TypePrinter, Machine, Context);
Chris Lattner3a36d2c2008-08-19 05:06:27 +00002183 return;
2184 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002185
Chris Lattner3a36d2c2008-08-19 05:06:27 +00002186 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
Chris Lattner033935d2008-08-17 04:40:13 +00002187 Out << "asm ";
2188 if (IA->hasSideEffects())
2189 Out << "sideeffect ";
Dale Johannesen1cfb9582009-10-21 23:28:00 +00002190 if (IA->isAlignStack())
2191 Out << "alignstack ";
Chad Rosierd8c76102012-09-05 19:00:49 +00002192 // We don't emit the AD_ATT dialect as it's the assumed default.
2193 if (IA->getDialect() == InlineAsm::AD_Intel)
2194 Out << "inteldialect ";
Chris Lattner033935d2008-08-17 04:40:13 +00002195 Out << '"';
Jonas Devlieghere745918ff2018-05-31 17:01:42 +00002196 printEscapedString(IA->getAsmString(), Out);
Chris Lattner033935d2008-08-17 04:40:13 +00002197 Out << "\", \"";
Jonas Devlieghere745918ff2018-05-31 17:01:42 +00002198 printEscapedString(IA->getConstraintString(), Out);
Chris Lattner033935d2008-08-17 04:40:13 +00002199 Out << '"';
Chris Lattner3a36d2c2008-08-19 05:06:27 +00002200 return;
2201 }
Devang Patel7428d8a2009-07-22 17:43:22 +00002202
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002203 if (auto *MD = dyn_cast<MetadataAsValue>(V)) {
2204 WriteAsOperandInternal(Out, MD->getMetadata(), TypePrinter, Machine,
2205 Context, /* FromValue */ true);
Devang Patel7428d8a2009-07-22 17:43:22 +00002206 return;
2207 }
2208
Chris Lattner3a36d2c2008-08-19 05:06:27 +00002209 char Prefix = '%';
2210 int Slot;
Chris Lattner5b82a0a2011-08-03 06:15:41 +00002211 // If we have a SlotTracker, use it.
Chris Lattner3a36d2c2008-08-19 05:06:27 +00002212 if (Machine) {
2213 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
2214 Slot = Machine->getGlobalSlot(GV);
2215 Prefix = '@';
2216 } else {
2217 Slot = Machine->getLocalSlot(V);
Andrew Trickec4b6e72011-09-30 19:48:58 +00002218
Chris Lattner5b82a0a2011-08-03 06:15:41 +00002219 // If the local value didn't succeed, then we may be referring to a value
2220 // from a different function. Translate it, as this can happen when using
2221 // address of blocks.
2222 if (Slot == -1)
2223 if ((Machine = createSlotTracker(V))) {
2224 Slot = Machine->getLocalSlot(V);
2225 delete Machine;
2226 }
Chris Lattner3a36d2c2008-08-19 05:06:27 +00002227 }
Chris Lattner5b82a0a2011-08-03 06:15:41 +00002228 } else if ((Machine = createSlotTracker(V))) {
2229 // Otherwise, create one to get the # and then destroy it.
2230 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
2231 Slot = Machine->getGlobalSlot(GV);
2232 Prefix = '@';
Chris Lattnera2d810d2006-01-25 22:26:05 +00002233 } else {
Chris Lattner5b82a0a2011-08-03 06:15:41 +00002234 Slot = Machine->getLocalSlot(V);
Chris Lattnerd84bb632002-04-16 21:36:08 +00002235 }
Chris Lattner5b82a0a2011-08-03 06:15:41 +00002236 delete Machine;
Craig Topperc6207612014-04-09 06:08:46 +00002237 Machine = nullptr;
Chris Lattner5b82a0a2011-08-03 06:15:41 +00002238 } else {
2239 Slot = -1;
Chris Lattnerd84bb632002-04-16 21:36:08 +00002240 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002241
Chris Lattner3a36d2c2008-08-19 05:06:27 +00002242 if (Slot != -1)
2243 Out << Prefix << Slot;
2244 else
2245 Out << "<badref>";
Chris Lattnerd84bb632002-04-16 21:36:08 +00002246}
2247
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002248static void WriteAsOperandInternal(raw_ostream &Out, const Metadata *MD,
2249 TypePrinting *TypePrinter,
2250 SlotTracker *Machine, const Module *Context,
2251 bool FromValue) {
Reid Kleckner6d353342017-08-23 20:31:27 +00002252 // Write DIExpressions inline when used as a value. Improves readability of
2253 // debug info intrinsics.
2254 if (const DIExpression *Expr = dyn_cast<DIExpression>(MD)) {
2255 writeDIExpression(Out, Expr, TypePrinter, Machine, Context);
2256 return;
2257 }
2258
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002259 if (const MDNode *N = dyn_cast<MDNode>(MD)) {
Duncan P. N. Exon Smithf8b3ad62015-06-27 00:15:32 +00002260 std::unique_ptr<SlotTracker> MachineStorage;
2261 if (!Machine) {
2262 MachineStorage = make_unique<SlotTracker>(Context);
2263 Machine = MachineStorage.get();
2264 }
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002265 int Slot = Machine->getMetadataSlot(N);
2266 if (Slot == -1)
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +00002267 // Give the pointer value instead of "badref", since this comes up all
2268 // the time when debugging.
2269 Out << "<" << N << ">";
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002270 else
2271 Out << '!' << Slot;
2272 return;
2273 }
2274
2275 if (const MDString *MDS = dyn_cast<MDString>(MD)) {
2276 Out << "!\"";
Jonas Devlieghere745918ff2018-05-31 17:01:42 +00002277 printEscapedString(MDS->getString(), Out);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002278 Out << '"';
2279 return;
2280 }
2281
2282 auto *V = cast<ValueAsMetadata>(MD);
2283 assert(TypePrinter && "TypePrinter required for metadata values");
2284 assert((FromValue || !isa<LocalAsMetadata>(V)) &&
2285 "Unexpected function-local metadata outside of value argument");
2286
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002287 TypePrinter->print(V->getValue()->getType(), Out);
2288 Out << ' ';
2289 WriteAsOperandInternal(Out, V->getValue(), TypePrinter, Machine, Context);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002290}
2291
Benjamin Kramerba05a782015-03-17 19:53:41 +00002292namespace {
Eugene Zelenkode6cce22017-06-19 22:05:08 +00002293
Benjamin Kramerba05a782015-03-17 19:53:41 +00002294class AssemblyWriter {
2295 formatted_raw_ostream &Out;
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00002296 const Module *TheModule = nullptr;
2297 const ModuleSummaryIndex *TheIndex = nullptr;
Duncan P. N. Exon Smith326921542015-06-26 22:04:20 +00002298 std::unique_ptr<SlotTracker> SlotTrackerStorage;
Benjamin Kramerba05a782015-03-17 19:53:41 +00002299 SlotTracker &Machine;
2300 TypePrinting TypePrinter;
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00002301 AssemblyAnnotationWriter *AnnotationWriter = nullptr;
Benjamin Kramerba05a782015-03-17 19:53:41 +00002302 SetVector<const Comdat *> Comdats;
Justin Bognerd7d1a722015-09-27 22:38:50 +00002303 bool IsForDebug;
Duncan P. N. Exon Smith89010d82015-04-15 01:36:30 +00002304 bool ShouldPreserveUseListOrder;
Benjamin Kramerba05a782015-03-17 19:53:41 +00002305 UseListOrderStack UseListOrders;
Duncan P. N. Exon Smithe30f10e2015-04-24 21:03:05 +00002306 SmallVector<StringRef, 8> MDNames;
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00002307 /// Synchronization scope names registered with LLVMContext.
2308 SmallVector<StringRef, 8> SSNs;
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00002309 DenseMap<const GlobalValueSummary *, GlobalValue::GUID> SummaryToGUIDMap;
Benjamin Kramerba05a782015-03-17 19:53:41 +00002310
2311public:
2312 /// Construct an AssemblyWriter with an external SlotTracker
Duncan P. N. Exon Smith89010d82015-04-15 01:36:30 +00002313 AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac, const Module *M,
Justin Bognerd7d1a722015-09-27 22:38:50 +00002314 AssemblyAnnotationWriter *AAW, bool IsForDebug,
Duncan P. N. Exon Smith89010d82015-04-15 01:36:30 +00002315 bool ShouldPreserveUseListOrder = false);
Benjamin Kramerba05a782015-03-17 19:53:41 +00002316
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00002317 AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac,
2318 const ModuleSummaryIndex *Index, bool IsForDebug);
2319
Benjamin Kramerba05a782015-03-17 19:53:41 +00002320 void printMDNodeBody(const MDNode *MD);
2321 void printNamedMDNode(const NamedMDNode *NMD);
2322
2323 void printModule(const Module *M);
2324
2325 void writeOperand(const Value *Op, bool PrintType);
Reid Kleckner6652a522017-04-28 18:37:16 +00002326 void writeParamOperand(const Value *Operand, AttributeSet Attrs);
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002327 void writeOperandBundles(ImmutableCallSite CS);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00002328 void writeSyncScope(const LLVMContext &Context,
2329 SyncScope::ID SSID);
2330 void writeAtomic(const LLVMContext &Context,
2331 AtomicOrdering Ordering,
2332 SyncScope::ID SSID);
2333 void writeAtomicCmpXchg(const LLVMContext &Context,
2334 AtomicOrdering SuccessOrdering,
Benjamin Kramerba05a782015-03-17 19:53:41 +00002335 AtomicOrdering FailureOrdering,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00002336 SyncScope::ID SSID);
Benjamin Kramerba05a782015-03-17 19:53:41 +00002337
2338 void writeAllMDNodes();
2339 void writeMDNode(unsigned Slot, const MDNode *Node);
2340 void writeAllAttributeGroups();
2341
2342 void printTypeIdentities();
2343 void printGlobal(const GlobalVariable *GV);
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +00002344 void printIndirectSymbol(const GlobalIndirectSymbol *GIS);
Benjamin Kramerba05a782015-03-17 19:53:41 +00002345 void printComdat(const Comdat *C);
2346 void printFunction(const Function *F);
Reid Kleckner6652a522017-04-28 18:37:16 +00002347 void printArgument(const Argument *FA, AttributeSet Attrs);
Benjamin Kramerba05a782015-03-17 19:53:41 +00002348 void printBasicBlock(const BasicBlock *BB);
2349 void printInstructionLine(const Instruction &I);
2350 void printInstruction(const Instruction &I);
2351
2352 void printUseListOrder(const UseListOrder &Order);
2353 void printUseLists(const Function *F);
2354
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00002355 void printModuleSummaryIndex();
2356 void printSummaryInfo(unsigned Slot, const ValueInfo &VI);
2357 void printSummary(const GlobalValueSummary &Summary);
2358 void printAliasSummary(const AliasSummary *AS);
2359 void printGlobalVarSummary(const GlobalVarSummary *GS);
2360 void printFunctionSummary(const FunctionSummary *FS);
2361 void printTypeIdSummary(const TypeIdSummary &TIS);
2362 void printTypeTestResolution(const TypeTestResolution &TTRes);
2363 void printArgs(const std::vector<uint64_t> &Args);
2364 void printWPDRes(const WholeProgramDevirtResolution &WPDRes);
2365 void printTypeIdInfo(const FunctionSummary::TypeIdInfo &TIDInfo);
2366 void printVFuncId(const FunctionSummary::VFuncId VFId);
2367 void
2368 printNonConstVCalls(const std::vector<FunctionSummary::VFuncId> VCallList,
2369 const char *Tag);
2370 void
2371 printConstVCalls(const std::vector<FunctionSummary::ConstVCall> VCallList,
2372 const char *Tag);
2373
Benjamin Kramerba05a782015-03-17 19:53:41 +00002374private:
Adrian Prantl5f8f34e42018-05-01 15:54:18 +00002375 /// Print out metadata attachments.
Duncan P. N. Exon Smith86979272015-04-24 20:59:52 +00002376 void printMetadataAttachments(
Duncan P. N. Exon Smithd3e4c2a2015-04-24 21:06:21 +00002377 const SmallVectorImpl<std::pair<unsigned, MDNode *>> &MDs,
2378 StringRef Separator);
Duncan P. N. Exon Smith86979272015-04-24 20:59:52 +00002379
Benjamin Kramerba05a782015-03-17 19:53:41 +00002380 // printInfoComment - Print a little comment after the instruction indicating
2381 // which slot it occupies.
2382 void printInfoComment(const Value &V);
Igor Laevsky2aa8caf2015-05-05 13:20:42 +00002383
2384 // printGCRelocateComment - print comment after call to the gc.relocate
2385 // intrinsic indicating base and derived pointer names.
Manuel Jacob83eefa62016-01-05 04:03:00 +00002386 void printGCRelocateComment(const GCRelocateInst &Relocate);
Benjamin Kramerba05a782015-03-17 19:53:41 +00002387};
Eugene Zelenkode6cce22017-06-19 22:05:08 +00002388
2389} // end anonymous namespace
Benjamin Kramerba05a782015-03-17 19:53:41 +00002390
Justin Bogner0ffea9d2015-09-02 22:46:15 +00002391AssemblyWriter::AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac,
2392 const Module *M, AssemblyAnnotationWriter *AAW,
Justin Bognerd7d1a722015-09-27 22:38:50 +00002393 bool IsForDebug, bool ShouldPreserveUseListOrder)
Roman Tereshind96de6f2018-03-22 21:29:07 +00002394 : Out(o), TheModule(M), Machine(Mac), TypePrinter(M), AnnotationWriter(AAW),
Justin Bognerd7d1a722015-09-27 22:38:50 +00002395 IsForDebug(IsForDebug),
Justin Bogner0ffea9d2015-09-02 22:46:15 +00002396 ShouldPreserveUseListOrder(ShouldPreserveUseListOrder) {
David Majnemerdad0a642014-06-27 18:19:56 +00002397 if (!TheModule)
2398 return;
Peter Collingbourne6d88fde2016-06-22 20:29:42 +00002399 for (const GlobalObject &GO : TheModule->global_objects())
2400 if (const Comdat *C = GO.getComdat())
David Majnemerdad0a642014-06-27 18:19:56 +00002401 Comdats.insert(C);
Daniel Maleaded9f932013-05-08 20:38:31 +00002402}
Chris Lattner2e9fee42001-07-12 23:35:26 +00002403
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00002404AssemblyWriter::AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac,
2405 const ModuleSummaryIndex *Index, bool IsForDebug)
2406 : Out(o), TheIndex(Index), Machine(Mac), TypePrinter(/*Module=*/nullptr),
2407 IsForDebug(IsForDebug), ShouldPreserveUseListOrder(false) {}
2408
Chris Lattner78e2e8b2006-12-06 06:24:27 +00002409void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType) {
Craig Topperc6207612014-04-09 06:08:46 +00002410 if (!Operand) {
Chris Lattner08f7d0c2005-02-24 16:58:29 +00002411 Out << "<null operand!>";
Chris Lattnerb419a1e2009-12-31 02:33:14 +00002412 return;
Chris Lattner08f7d0c2005-02-24 16:58:29 +00002413 }
Chris Lattnerb419a1e2009-12-31 02:33:14 +00002414 if (PrintType) {
2415 TypePrinter.print(Operand->getType(), Out);
2416 Out << ' ';
2417 }
Dan Gohmana2489d12010-07-20 23:55:01 +00002418 WriteAsOperandInternal(Out, Operand, &TypePrinter, &Machine, TheModule);
Chris Lattner2f7c9632001-06-06 20:29:01 +00002419}
2420
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00002421void AssemblyWriter::writeSyncScope(const LLVMContext &Context,
2422 SyncScope::ID SSID) {
2423 switch (SSID) {
2424 case SyncScope::System: {
2425 break;
2426 }
2427 default: {
2428 if (SSNs.empty())
2429 Context.getSyncScopeNames(SSNs);
2430
2431 Out << " syncscope(\"";
Jonas Devlieghere745918ff2018-05-31 17:01:42 +00002432 printEscapedString(SSNs[SSID], Out);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00002433 Out << "\")";
2434 break;
2435 }
2436 }
2437}
2438
2439void AssemblyWriter::writeAtomic(const LLVMContext &Context,
2440 AtomicOrdering Ordering,
2441 SyncScope::ID SSID) {
JF Bastien800f87a2016-04-06 21:19:33 +00002442 if (Ordering == AtomicOrdering::NotAtomic)
Eli Friedmanfee02c62011-07-25 23:16:38 +00002443 return;
2444
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00002445 writeSyncScope(Context, SSID);
JF Bastien800f87a2016-04-06 21:19:33 +00002446 Out << " " << toIRString(Ordering);
Eli Friedmanfee02c62011-07-25 23:16:38 +00002447}
2448
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00002449void AssemblyWriter::writeAtomicCmpXchg(const LLVMContext &Context,
2450 AtomicOrdering SuccessOrdering,
Tim Northovere94a5182014-03-11 10:48:52 +00002451 AtomicOrdering FailureOrdering,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00002452 SyncScope::ID SSID) {
JF Bastien800f87a2016-04-06 21:19:33 +00002453 assert(SuccessOrdering != AtomicOrdering::NotAtomic &&
2454 FailureOrdering != AtomicOrdering::NotAtomic);
Tim Northovere94a5182014-03-11 10:48:52 +00002455
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00002456 writeSyncScope(Context, SSID);
JF Bastien800f87a2016-04-06 21:19:33 +00002457 Out << " " << toIRString(SuccessOrdering);
2458 Out << " " << toIRString(FailureOrdering);
Tim Northovere94a5182014-03-11 10:48:52 +00002459}
2460
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002461void AssemblyWriter::writeParamOperand(const Value *Operand,
Reid Kleckner6652a522017-04-28 18:37:16 +00002462 AttributeSet Attrs) {
Craig Topperc6207612014-04-09 06:08:46 +00002463 if (!Operand) {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00002464 Out << "<null operand!>";
Chris Lattnerb419a1e2009-12-31 02:33:14 +00002465 return;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00002466 }
Chris Lattnerb419a1e2009-12-31 02:33:14 +00002467
2468 // Print the type
2469 TypePrinter.print(Operand->getType(), Out);
2470 // Print parameter attributes list
Reid Kleckner6652a522017-04-28 18:37:16 +00002471 if (Attrs.hasAttributes())
2472 Out << ' ' << Attrs.getAsString();
Chris Lattnerb419a1e2009-12-31 02:33:14 +00002473 Out << ' ';
2474 // Print the operand
Dan Gohmana2489d12010-07-20 23:55:01 +00002475 WriteAsOperandInternal(Out, Operand, &TypePrinter, &Machine, TheModule);
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00002476}
Chris Lattner2f7c9632001-06-06 20:29:01 +00002477
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002478void AssemblyWriter::writeOperandBundles(ImmutableCallSite CS) {
2479 if (!CS.hasOperandBundles())
2480 return;
2481
2482 Out << " [ ";
2483
2484 bool FirstBundle = true;
2485 for (unsigned i = 0, e = CS.getNumOperandBundles(); i != e; ++i) {
Sanjoy Das54c3ca62015-11-07 01:56:04 +00002486 OperandBundleUse BU = CS.getOperandBundleAt(i);
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002487
2488 if (!FirstBundle)
2489 Out << ", ";
2490 FirstBundle = false;
2491
2492 Out << '"';
Jonas Devlieghere745918ff2018-05-31 17:01:42 +00002493 printEscapedString(BU.getTagName(), Out);
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002494 Out << '"';
2495
2496 Out << '(';
2497
2498 bool FirstInput = true;
2499 for (const auto &Input : BU.Inputs) {
2500 if (!FirstInput)
2501 Out << ", ";
2502 FirstInput = false;
2503
2504 TypePrinter.print(Input->getType(), Out);
2505 Out << " ";
2506 WriteAsOperandInternal(Out, Input, &TypePrinter, &Machine, TheModule);
2507 }
2508
2509 Out << ')';
2510 }
2511
2512 Out << " ]";
2513}
2514
Chris Lattner7bfee412001-10-29 16:05:51 +00002515void AssemblyWriter::printModule(const Module *M) {
Teresa Johnsonfc801f52018-07-03 15:52:57 +00002516 Machine.initializeIfNeeded();
Bill Wendling829b4782013-02-11 08:43:33 +00002517
Duncan P. N. Exon Smith89010d82015-04-15 01:36:30 +00002518 if (ShouldPreserveUseListOrder)
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +00002519 UseListOrders = predictUseListOrder(M);
2520
Chris Lattner4d8689e2005-03-02 23:12:40 +00002521 if (!M->getModuleIdentifier().empty() &&
Misha Brukmanb1c93172005-04-21 23:48:37 +00002522 // Don't print the ID if it will start a new line (which would
Chris Lattner4d8689e2005-03-02 23:12:40 +00002523 // require a comment char before it).
2524 M->getModuleIdentifier().find('\n') == std::string::npos)
2525 Out << "; ModuleID = '" << M->getModuleIdentifier() << "'\n";
2526
Teresa Johnson83c517c2016-03-30 18:15:08 +00002527 if (!M->getSourceFileName().empty()) {
Teresa Johnsond8d94652016-03-30 22:17:28 +00002528 Out << "source_filename = \"";
Jonas Devlieghere745918ff2018-05-31 17:01:42 +00002529 printEscapedString(M->getSourceFileName(), Out);
Teresa Johnsond8d94652016-03-30 22:17:28 +00002530 Out << "\"\n";
Teresa Johnson83c517c2016-03-30 18:15:08 +00002531 }
2532
Rafael Espindolaf863ee22014-02-25 20:01:08 +00002533 const std::string &DL = M->getDataLayoutStr();
2534 if (!DL.empty())
2535 Out << "target datalayout = \"" << DL << "\"\n";
Reid Spencer48f98c82004-07-25 21:44:54 +00002536 if (!M->getTargetTriple().empty())
Reid Spencerffec7df2004-07-25 21:29:43 +00002537 Out << "target triple = \"" << M->getTargetTriple() << "\"\n";
Misha Brukmanb1c93172005-04-21 23:48:37 +00002538
Chris Lattnereef2fe72006-01-24 04:13:11 +00002539 if (!M->getModuleInlineAsm().empty()) {
Dan Gohman69273e62009-08-12 23:54:22 +00002540 Out << '\n';
Benjamin Kramerbbd05a22015-06-07 13:59:33 +00002541
2542 // Split the string into lines, to make it easier to read the .ll file.
Benjamin Kramerf1cfc422015-06-08 18:58:57 +00002543 StringRef Asm = M->getModuleInlineAsm();
Benjamin Kramerbbd05a22015-06-07 13:59:33 +00002544 do {
2545 StringRef Front;
2546 std::tie(Front, Asm) = Asm.split('\n');
2547
Chris Lattnerefaf35d2006-01-24 00:45:30 +00002548 // We found a newline, print the portion of the asm string from the
2549 // last newline up to this newline.
2550 Out << "module asm \"";
Jonas Devlieghere745918ff2018-05-31 17:01:42 +00002551 printEscapedString(Front, Out);
Chris Lattnerefaf35d2006-01-24 00:45:30 +00002552 Out << "\"\n";
Benjamin Kramerbbd05a22015-06-07 13:59:33 +00002553 } while (!Asm.empty());
Chris Lattner6ed87bd2006-01-23 23:03:36 +00002554 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002555
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002556 printTypeIdentities();
Misha Brukmanb1c93172005-04-21 23:48:37 +00002557
David Majnemerdad0a642014-06-27 18:19:56 +00002558 // Output all comdats.
2559 if (!Comdats.empty())
2560 Out << '\n';
2561 for (const Comdat *C : Comdats) {
2562 printComdat(C);
2563 if (C != Comdats.back())
2564 Out << '\n';
2565 }
2566
Dan Gohman69273e62009-08-12 23:54:22 +00002567 // Output all globals.
2568 if (!M->global_empty()) Out << '\n';
Yaron Kerenf3cf9d12015-06-13 17:50:47 +00002569 for (const GlobalVariable &GV : M->globals()) {
2570 printGlobal(&GV); Out << '\n';
Duncan Sands66fc0e62012-09-12 09:55:51 +00002571 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002572
Chris Lattnerd2747052007-04-26 02:24:10 +00002573 // Output all aliases.
2574 if (!M->alias_empty()) Out << "\n";
Yaron Kerenf3cf9d12015-06-13 17:50:47 +00002575 for (const GlobalAlias &GA : M->aliases())
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +00002576 printIndirectSymbol(&GA);
Chris Lattnerfee714f2001-09-07 16:36:04 +00002577
Dmitry Polukhina1feff72016-04-07 12:32:19 +00002578 // Output all ifuncs.
2579 if (!M->ifunc_empty()) Out << "\n";
2580 for (const GlobalIFunc &GI : M->ifuncs())
2581 printIndirectSymbol(&GI);
2582
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +00002583 // Output global use-lists.
2584 printUseLists(nullptr);
2585
Chris Lattner2cdd49d2004-09-14 05:06:58 +00002586 // Output all of the functions.
Yaron Kerenf3cf9d12015-06-13 17:50:47 +00002587 for (const Function &F : *M)
2588 printFunction(&F);
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +00002589 assert(UseListOrders.empty() && "All use-lists should have been consumed");
Devang Patel983c6b12009-07-08 21:44:25 +00002590
Bill Wendling829b4782013-02-11 08:43:33 +00002591 // Output all attribute groups.
Bill Wendling04945972013-04-29 23:48:06 +00002592 if (!Machine.as_empty()) {
Bill Wendling829b4782013-02-11 08:43:33 +00002593 Out << '\n';
2594 writeAllAttributeGroups();
2595 }
2596
Devang Patel23e68302009-07-29 22:04:47 +00002597 // Output named metadata.
Dan Gohman69273e62009-08-12 23:54:22 +00002598 if (!M->named_metadata_empty()) Out << '\n';
Andrew Trickec4b6e72011-09-30 19:48:58 +00002599
Yaron Kerenf3cf9d12015-06-13 17:50:47 +00002600 for (const NamedMDNode &Node : M->named_metadata())
2601 printNamedMDNode(&Node);
Devang Patel23e68302009-07-29 22:04:47 +00002602
2603 // Output metadata.
Chris Lattnercf4a76e2009-12-31 02:20:11 +00002604 if (!Machine.mdn_empty()) {
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002605 Out << '\n';
2606 writeAllMDNodes();
2607 }
Chris Lattnerfee714f2001-09-07 16:36:04 +00002608}
2609
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00002610void AssemblyWriter::printModuleSummaryIndex() {
2611 assert(TheIndex);
Teresa Johnsonfc801f52018-07-03 15:52:57 +00002612 Machine.initializeIndexIfNeeded();
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00002613
2614 Out << "\n";
2615
Teresa Johnson8fc76662018-07-02 22:09:23 +00002616 // Print module path entries. To print in order, add paths to a vector
2617 // indexed by module slot.
Teresa Johnson724e7a12018-05-26 02:53:52 +00002618 std::vector<std::pair<std::string, ModuleHash>> moduleVec;
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00002619 std::string RegularLTOModuleName = "[Regular LTO]";
2620 moduleVec.resize(TheIndex->modulePaths().size());
Teresa Johnson8fc76662018-07-02 22:09:23 +00002621 for (auto &ModPath : TheIndex->modulePaths())
2622 moduleVec[Machine.getModulePathSlot(ModPath.first())] = std::make_pair(
2623 // A module id of -1 is a special entry for a regular LTO module created
2624 // during the thin link.
2625 ModPath.second.first == -1u ? RegularLTOModuleName
2626 : (std::string)ModPath.first(),
2627 ModPath.second.second);
2628
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00002629 unsigned i = 0;
2630 for (auto &ModPair : moduleVec) {
2631 Out << "^" << i++ << " = module: (";
Andrew Ngac305e12018-07-12 14:40:21 +00002632 Out << "path: \"";
2633 printEscapedString(ModPair.first, Out);
2634 Out << "\", hash: (";
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00002635 FieldSeparator FS;
2636 for (auto Hash : ModPair.second)
2637 Out << FS << Hash;
2638 Out << "))\n";
2639 }
2640
2641 // FIXME: Change AliasSummary to hold a ValueInfo instead of summary pointer
2642 // for aliasee (then update BitcodeWriter.cpp and remove get/setAliaseeGUID).
2643 for (auto &GlobalList : *TheIndex) {
2644 auto GUID = GlobalList.first;
2645 for (auto &Summary : GlobalList.second.SummaryList)
2646 SummaryToGUIDMap[Summary.get()] = GUID;
2647 }
2648
2649 // Print the global value summary entries.
2650 for (auto &GlobalList : *TheIndex) {
2651 auto GUID = GlobalList.first;
2652 auto VI = TheIndex->getValueInfo(GlobalList);
2653 printSummaryInfo(Machine.getGUIDSlot(GUID), VI);
2654 }
2655
2656 // Print the TypeIdMap entries.
2657 for (auto &TId : TheIndex->typeIds()) {
2658 auto GUID = GlobalValue::getGUID(TId.first);
2659 Out << "^" << Machine.getGUIDSlot(GUID) << " = typeid: (name: \""
2660 << TId.first << "\"";
2661 printTypeIdSummary(TId.second);
2662 Out << ") ; guid = " << GUID << "\n";
2663 }
2664}
2665
2666static const char *
2667getWholeProgDevirtResKindName(WholeProgramDevirtResolution::Kind K) {
2668 switch (K) {
2669 case WholeProgramDevirtResolution::Indir:
2670 return "indir";
2671 case WholeProgramDevirtResolution::SingleImpl:
2672 return "singleImpl";
2673 case WholeProgramDevirtResolution::BranchFunnel:
2674 return "branchFunnel";
2675 }
2676 llvm_unreachable("invalid WholeProgramDevirtResolution kind");
2677}
2678
2679static const char *getWholeProgDevirtResByArgKindName(
2680 WholeProgramDevirtResolution::ByArg::Kind K) {
2681 switch (K) {
2682 case WholeProgramDevirtResolution::ByArg::Indir:
2683 return "indir";
2684 case WholeProgramDevirtResolution::ByArg::UniformRetVal:
2685 return "uniformRetVal";
2686 case WholeProgramDevirtResolution::ByArg::UniqueRetVal:
2687 return "uniqueRetVal";
2688 case WholeProgramDevirtResolution::ByArg::VirtualConstProp:
2689 return "virtualConstProp";
2690 }
2691 llvm_unreachable("invalid WholeProgramDevirtResolution::ByArg kind");
2692}
2693
2694static const char *getTTResKindName(TypeTestResolution::Kind K) {
2695 switch (K) {
2696 case TypeTestResolution::Unsat:
2697 return "unsat";
2698 case TypeTestResolution::ByteArray:
2699 return "byteArray";
2700 case TypeTestResolution::Inline:
2701 return "inline";
2702 case TypeTestResolution::Single:
2703 return "single";
2704 case TypeTestResolution::AllOnes:
2705 return "allOnes";
2706 }
2707 llvm_unreachable("invalid TypeTestResolution kind");
2708}
2709
2710void AssemblyWriter::printTypeTestResolution(const TypeTestResolution &TTRes) {
2711 Out << "typeTestRes: (kind: " << getTTResKindName(TTRes.TheKind)
2712 << ", sizeM1BitWidth: " << TTRes.SizeM1BitWidth;
2713
2714 // The following fields are only used if the target does not support the use
2715 // of absolute symbols to store constants. Print only if non-zero.
2716 if (TTRes.AlignLog2)
2717 Out << ", alignLog2: " << TTRes.AlignLog2;
2718 if (TTRes.SizeM1)
2719 Out << ", sizeM1: " << TTRes.SizeM1;
2720 if (TTRes.BitMask)
2721 // BitMask is uint8_t which causes it to print the corresponding char.
2722 Out << ", bitMask: " << (unsigned)TTRes.BitMask;
2723 if (TTRes.InlineBits)
2724 Out << ", inlineBits: " << TTRes.InlineBits;
2725
2726 Out << ")";
2727}
2728
2729void AssemblyWriter::printTypeIdSummary(const TypeIdSummary &TIS) {
2730 Out << ", summary: (";
2731 printTypeTestResolution(TIS.TTRes);
2732 if (!TIS.WPDRes.empty()) {
2733 Out << ", wpdResolutions: (";
2734 FieldSeparator FS;
2735 for (auto &WPDRes : TIS.WPDRes) {
2736 Out << FS;
2737 Out << "(offset: " << WPDRes.first << ", ";
2738 printWPDRes(WPDRes.second);
2739 Out << ")";
2740 }
2741 Out << ")";
2742 }
2743 Out << ")";
2744}
2745
2746void AssemblyWriter::printArgs(const std::vector<uint64_t> &Args) {
2747 Out << "args: (";
2748 FieldSeparator FS;
2749 for (auto arg : Args) {
2750 Out << FS;
2751 Out << arg;
2752 }
2753 Out << ")";
2754}
2755
2756void AssemblyWriter::printWPDRes(const WholeProgramDevirtResolution &WPDRes) {
2757 Out << "wpdRes: (kind: ";
2758 Out << getWholeProgDevirtResKindName(WPDRes.TheKind);
2759
2760 if (WPDRes.TheKind == WholeProgramDevirtResolution::SingleImpl)
2761 Out << ", singleImplName: \"" << WPDRes.SingleImplName << "\"";
2762
2763 if (!WPDRes.ResByArg.empty()) {
2764 Out << ", resByArg: (";
2765 FieldSeparator FS;
2766 for (auto &ResByArg : WPDRes.ResByArg) {
2767 Out << FS;
2768 printArgs(ResByArg.first);
2769 Out << ", byArg: (kind: ";
2770 Out << getWholeProgDevirtResByArgKindName(ResByArg.second.TheKind);
2771 if (ResByArg.second.TheKind ==
2772 WholeProgramDevirtResolution::ByArg::UniformRetVal ||
2773 ResByArg.second.TheKind ==
2774 WholeProgramDevirtResolution::ByArg::UniqueRetVal)
2775 Out << ", info: " << ResByArg.second.Info;
2776
2777 // The following fields are only used if the target does not support the
2778 // use of absolute symbols to store constants. Print only if non-zero.
2779 if (ResByArg.second.Byte || ResByArg.second.Bit)
2780 Out << ", byte: " << ResByArg.second.Byte
2781 << ", bit: " << ResByArg.second.Bit;
2782
2783 Out << ")";
2784 }
2785 Out << ")";
2786 }
2787 Out << ")";
2788}
2789
2790static const char *getSummaryKindName(GlobalValueSummary::SummaryKind SK) {
2791 switch (SK) {
2792 case GlobalValueSummary::AliasKind:
2793 return "alias";
2794 case GlobalValueSummary::FunctionKind:
2795 return "function";
2796 case GlobalValueSummary::GlobalVarKind:
2797 return "variable";
2798 }
2799 llvm_unreachable("invalid summary kind");
2800}
2801
2802void AssemblyWriter::printAliasSummary(const AliasSummary *AS) {
Teresa Johnsonf8182f12018-07-03 01:11:43 +00002803 Out << ", aliasee: ";
2804 // The indexes emitted for distributed backends may not include the
2805 // aliasee summary (only if it is being imported directly). Handle
2806 // that case by just emitting "null" as the aliasee.
2807 if (AS->hasAliasee())
2808 Out << "^" << Machine.getGUIDSlot(SummaryToGUIDMap[&AS->getAliasee()]);
2809 else
2810 Out << "null";
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00002811}
2812
2813void AssemblyWriter::printGlobalVarSummary(const GlobalVarSummary *GS) {
2814 // Nothing for now
2815}
2816
Teresa Johnsonc80c8732018-05-25 15:15:39 +00002817static std::string getLinkageName(GlobalValue::LinkageTypes LT) {
2818 switch (LT) {
2819 case GlobalValue::ExternalLinkage:
2820 return "external";
2821 case GlobalValue::PrivateLinkage:
2822 return "private";
2823 case GlobalValue::InternalLinkage:
2824 return "internal";
2825 case GlobalValue::LinkOnceAnyLinkage:
2826 return "linkonce";
2827 case GlobalValue::LinkOnceODRLinkage:
2828 return "linkonce_odr";
2829 case GlobalValue::WeakAnyLinkage:
2830 return "weak";
2831 case GlobalValue::WeakODRLinkage:
2832 return "weak_odr";
2833 case GlobalValue::CommonLinkage:
2834 return "common";
2835 case GlobalValue::AppendingLinkage:
2836 return "appending";
2837 case GlobalValue::ExternalWeakLinkage:
2838 return "extern_weak";
2839 case GlobalValue::AvailableExternallyLinkage:
2840 return "available_externally";
2841 }
2842 llvm_unreachable("invalid linkage");
2843}
2844
2845// When printing the linkage types in IR where the ExternalLinkage is
2846// not printed, and other linkage types are expected to be printed with
2847// a space after the name.
2848static std::string getLinkageNameWithSpace(GlobalValue::LinkageTypes LT) {
2849 if (LT == GlobalValue::ExternalLinkage)
2850 return "";
2851 return getLinkageName(LT) + " ";
2852}
2853
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00002854void AssemblyWriter::printFunctionSummary(const FunctionSummary *FS) {
2855 Out << ", insts: " << FS->instCount();
2856
2857 FunctionSummary::FFlags FFlags = FS->fflags();
2858 if (FFlags.ReadNone | FFlags.ReadOnly | FFlags.NoRecurse |
2859 FFlags.ReturnDoesNotAlias) {
2860 Out << ", funcFlags: (";
2861 Out << "readNone: " << FFlags.ReadNone;
2862 Out << ", readOnly: " << FFlags.ReadOnly;
2863 Out << ", noRecurse: " << FFlags.NoRecurse;
2864 Out << ", returnDoesNotAlias: " << FFlags.ReturnDoesNotAlias;
2865 Out << ")";
2866 }
2867 if (!FS->calls().empty()) {
2868 Out << ", calls: (";
2869 FieldSeparator IFS;
2870 for (auto &Call : FS->calls()) {
2871 Out << IFS;
2872 Out << "(callee: ^" << Machine.getGUIDSlot(Call.first.getGUID());
2873 if (Call.second.getHotness() != CalleeInfo::HotnessType::Unknown)
2874 Out << ", hotness: " << getHotnessName(Call.second.getHotness());
2875 else if (Call.second.RelBlockFreq)
2876 Out << ", relbf: " << Call.second.RelBlockFreq;
2877 Out << ")";
2878 }
2879 Out << ")";
2880 }
2881
2882 if (const auto *TIdInfo = FS->getTypeIdInfo())
2883 printTypeIdInfo(*TIdInfo);
2884}
2885
2886void AssemblyWriter::printTypeIdInfo(
2887 const FunctionSummary::TypeIdInfo &TIDInfo) {
2888 Out << ", typeIdInfo: (";
2889 FieldSeparator TIDFS;
2890 if (!TIDInfo.TypeTests.empty()) {
2891 Out << TIDFS;
2892 Out << "typeTests: (";
2893 FieldSeparator FS;
2894 for (auto &GUID : TIDInfo.TypeTests) {
2895 Out << FS;
2896 auto Slot = Machine.getGUIDSlot(GUID);
2897 if (Slot != -1)
2898 Out << "^" << Slot;
2899 else
2900 Out << GUID;
2901 }
2902 Out << ")";
2903 }
2904 if (!TIDInfo.TypeTestAssumeVCalls.empty()) {
2905 Out << TIDFS;
2906 printNonConstVCalls(TIDInfo.TypeTestAssumeVCalls, "typeTestAssumeVCalls");
2907 }
2908 if (!TIDInfo.TypeCheckedLoadVCalls.empty()) {
2909 Out << TIDFS;
2910 printNonConstVCalls(TIDInfo.TypeCheckedLoadVCalls, "typeCheckedLoadVCalls");
2911 }
2912 if (!TIDInfo.TypeTestAssumeConstVCalls.empty()) {
2913 Out << TIDFS;
2914 printConstVCalls(TIDInfo.TypeTestAssumeConstVCalls,
2915 "typeTestAssumeConstVCalls");
2916 }
2917 if (!TIDInfo.TypeCheckedLoadConstVCalls.empty()) {
2918 Out << TIDFS;
2919 printConstVCalls(TIDInfo.TypeCheckedLoadConstVCalls,
2920 "typeCheckedLoadConstVCalls");
2921 }
2922 Out << ")";
2923}
2924
2925void AssemblyWriter::printVFuncId(const FunctionSummary::VFuncId VFId) {
2926 Out << "vFuncId: (";
2927 auto Slot = Machine.getGUIDSlot(VFId.GUID);
2928 if (Slot != -1)
2929 Out << "^" << Slot;
2930 else
2931 Out << "guid: " << VFId.GUID;
2932 Out << ", offset: " << VFId.Offset;
2933 Out << ")";
2934}
2935
2936void AssemblyWriter::printNonConstVCalls(
2937 const std::vector<FunctionSummary::VFuncId> VCallList, const char *Tag) {
2938 Out << Tag << ": (";
2939 FieldSeparator FS;
2940 for (auto &VFuncId : VCallList) {
2941 Out << FS;
2942 printVFuncId(VFuncId);
2943 }
2944 Out << ")";
2945}
2946
2947void AssemblyWriter::printConstVCalls(
2948 const std::vector<FunctionSummary::ConstVCall> VCallList, const char *Tag) {
2949 Out << Tag << ": (";
2950 FieldSeparator FS;
2951 for (auto &ConstVCall : VCallList) {
2952 Out << FS;
Teresa Johnsonc7816802018-08-14 01:49:33 +00002953 Out << "(";
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00002954 printVFuncId(ConstVCall.VFunc);
2955 if (!ConstVCall.Args.empty()) {
2956 Out << ", ";
2957 printArgs(ConstVCall.Args);
2958 }
Teresa Johnsonc7816802018-08-14 01:49:33 +00002959 Out << ")";
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00002960 }
2961 Out << ")";
2962}
2963
2964void AssemblyWriter::printSummary(const GlobalValueSummary &Summary) {
2965 GlobalValueSummary::GVFlags GVFlags = Summary.flags();
2966 GlobalValue::LinkageTypes LT = (GlobalValue::LinkageTypes)GVFlags.Linkage;
2967 Out << getSummaryKindName(Summary.getSummaryKind()) << ": ";
Teresa Johnson8fc76662018-07-02 22:09:23 +00002968 Out << "(module: ^" << Machine.getModulePathSlot(Summary.modulePath())
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00002969 << ", flags: (";
2970 Out << "linkage: " << getLinkageName(LT);
2971 Out << ", notEligibleToImport: " << GVFlags.NotEligibleToImport;
2972 Out << ", live: " << GVFlags.Live;
2973 Out << ", dsoLocal: " << GVFlags.DSOLocal;
2974 Out << ")";
2975
2976 if (Summary.getSummaryKind() == GlobalValueSummary::AliasKind)
2977 printAliasSummary(cast<AliasSummary>(&Summary));
2978 else if (Summary.getSummaryKind() == GlobalValueSummary::FunctionKind)
2979 printFunctionSummary(cast<FunctionSummary>(&Summary));
2980 else
2981 printGlobalVarSummary(cast<GlobalVarSummary>(&Summary));
2982
2983 auto RefList = Summary.refs();
2984 if (!RefList.empty()) {
2985 Out << ", refs: (";
2986 FieldSeparator FS;
2987 for (auto &Ref : RefList) {
2988 Out << FS;
2989 Out << "^" << Machine.getGUIDSlot(Ref.getGUID());
2990 }
2991 Out << ")";
2992 }
2993
2994 Out << ")";
2995}
2996
2997void AssemblyWriter::printSummaryInfo(unsigned Slot, const ValueInfo &VI) {
2998 Out << "^" << Slot << " = gv: (";
2999 if (!VI.name().empty())
3000 Out << "name: \"" << VI.name() << "\"";
3001 else
3002 Out << "guid: " << VI.getGUID();
3003 if (!VI.getSummaryList().empty()) {
3004 Out << ", summaries: (";
3005 FieldSeparator FS;
3006 for (auto &Summary : VI.getSummaryList()) {
3007 Out << FS;
3008 printSummary(*Summary);
3009 }
3010 Out << ")";
3011 }
3012 Out << ")";
3013 if (!VI.name().empty())
3014 Out << " ; guid = " << VI.getGUID();
3015 Out << "\n";
3016}
3017
Filipe Cabecinhas62431b12015-06-02 21:25:08 +00003018static void printMetadataIdentifier(StringRef Name,
3019 formatted_raw_ostream &Out) {
Nick Lewycky5bcbd732011-06-15 06:37:58 +00003020 if (Name.empty()) {
3021 Out << "<empty name> ";
3022 } else {
Filipe Cabecinhas1ac0d2d2015-06-02 21:25:00 +00003023 if (isalpha(static_cast<unsigned char>(Name[0])) || Name[0] == '-' ||
3024 Name[0] == '$' || Name[0] == '.' || Name[0] == '_')
Nick Lewycky5bcbd732011-06-15 06:37:58 +00003025 Out << Name[0];
3026 else
3027 Out << '\\' << hexdigit(Name[0] >> 4) << hexdigit(Name[0] & 0x0F);
3028 for (unsigned i = 1, e = Name.size(); i != e; ++i) {
3029 unsigned char C = Name[i];
Guy Benyei83c74e92013-02-12 21:21:59 +00003030 if (isalnum(static_cast<unsigned char>(C)) || C == '-' || C == '$' ||
3031 C == '.' || C == '_')
Nick Lewycky5bcbd732011-06-15 06:37:58 +00003032 Out << C;
3033 else
3034 Out << '\\' << hexdigit(C >> 4) << hexdigit(C & 0x0F);
3035 }
3036 }
Filipe Cabecinhas62431b12015-06-02 21:25:08 +00003037}
3038
3039void AssemblyWriter::printNamedMDNode(const NamedMDNode *NMD) {
3040 Out << '!';
3041 printMetadataIdentifier(NMD->getName(), Out);
Nick Lewycky5bcbd732011-06-15 06:37:58 +00003042 Out << " = !{";
Chris Lattner2c48c642009-12-31 01:54:05 +00003043 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
Filipe Cabecinhas1ac0d2d2015-06-02 21:25:00 +00003044 if (i)
3045 Out << ", ";
Reid Kleckner6d353342017-08-23 20:31:27 +00003046
3047 // Write DIExpressions inline.
3048 // FIXME: Ban DIExpressions in NamedMDNodes, they will serve no purpose.
3049 MDNode *Op = NMD->getOperand(i);
3050 if (auto *Expr = dyn_cast<DIExpression>(Op)) {
3051 writeDIExpression(Out, Expr, nullptr, nullptr, nullptr);
3052 continue;
3053 }
3054
3055 int Slot = Machine.getMetadataSlot(Op);
Dan Gohman0a54da22010-09-09 20:53:58 +00003056 if (Slot == -1)
3057 Out << "<badref>";
3058 else
3059 Out << '!' << Slot;
Chris Lattner2c48c642009-12-31 01:54:05 +00003060 }
3061 Out << "}\n";
3062}
3063
Chris Lattner3a36d2c2008-08-19 05:06:27 +00003064static void PrintVisibility(GlobalValue::VisibilityTypes Vis,
Dan Gohmane2745262009-08-12 17:23:50 +00003065 formatted_raw_ostream &Out) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00003066 switch (Vis) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00003067 case GlobalValue::DefaultVisibility: break;
3068 case GlobalValue::HiddenVisibility: Out << "hidden "; break;
3069 case GlobalValue::ProtectedVisibility: Out << "protected "; break;
3070 }
3071}
3072
Rafael Espindolae4b02312018-01-11 22:15:05 +00003073static void PrintDSOLocation(const GlobalValue &GV,
3074 formatted_raw_ostream &Out) {
Rafael Espindola9fbc0402018-01-18 02:08:23 +00003075 // GVs with local linkage or non default visibility are implicitly dso_local,
3076 // so we don't print it.
3077 bool Implicit = GV.hasLocalLinkage() ||
3078 (!GV.hasExternalWeakLinkage() && !GV.hasDefaultVisibility());
3079 if (GV.isDSOLocal() && !Implicit)
Sean Fertilec70d28b2017-10-26 15:00:26 +00003080 Out << "dso_local ";
3081}
3082
Nico Rieck7157bb72014-01-14 15:22:47 +00003083static void PrintDLLStorageClass(GlobalValue::DLLStorageClassTypes SCT,
3084 formatted_raw_ostream &Out) {
3085 switch (SCT) {
3086 case GlobalValue::DefaultStorageClass: break;
3087 case GlobalValue::DLLImportStorageClass: Out << "dllimport "; break;
3088 case GlobalValue::DLLExportStorageClass: Out << "dllexport "; break;
3089 }
3090}
3091
Hans Wennborgcbe34b42012-06-23 11:37:03 +00003092static void PrintThreadLocalModel(GlobalVariable::ThreadLocalMode TLM,
3093 formatted_raw_ostream &Out) {
3094 switch (TLM) {
3095 case GlobalVariable::NotThreadLocal:
3096 break;
3097 case GlobalVariable::GeneralDynamicTLSModel:
3098 Out << "thread_local ";
3099 break;
3100 case GlobalVariable::LocalDynamicTLSModel:
3101 Out << "thread_local(localdynamic) ";
3102 break;
3103 case GlobalVariable::InitialExecTLSModel:
3104 Out << "thread_local(initialexec) ";
3105 break;
3106 case GlobalVariable::LocalExecTLSModel:
3107 Out << "thread_local(localexec) ";
3108 break;
3109 }
3110}
3111
Peter Collingbourne96efdd62016-06-14 21:01:22 +00003112static StringRef getUnnamedAddrEncoding(GlobalVariable::UnnamedAddr UA) {
3113 switch (UA) {
3114 case GlobalVariable::UnnamedAddr::None:
3115 return "";
3116 case GlobalVariable::UnnamedAddr::Local:
3117 return "local_unnamed_addr";
3118 case GlobalVariable::UnnamedAddr::Global:
3119 return "unnamed_addr";
3120 }
Aaron Ballman2b9fa3f2016-06-15 15:27:53 +00003121 llvm_unreachable("Unknown UnnamedAddr");
Peter Collingbourne96efdd62016-06-14 21:01:22 +00003122}
3123
Rafael Espindola83a362c2015-01-06 22:55:16 +00003124static void maybePrintComdat(formatted_raw_ostream &Out,
3125 const GlobalObject &GO) {
3126 const Comdat *C = GO.getComdat();
3127 if (!C)
3128 return;
3129
3130 if (isa<GlobalVariable>(GO))
3131 Out << ',';
3132 Out << " comdat";
3133
3134 if (GO.getName() == C->getName())
3135 return;
3136
3137 Out << '(';
3138 PrintLLVMName(Out, C->getName(), ComdatPrefix);
3139 Out << ')';
3140}
3141
Chris Lattner7bfee412001-10-29 16:05:51 +00003142void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Dan Gohman5ded1422010-01-29 23:12:36 +00003143 if (GV->isMaterializable())
3144 Out << "; Materializable\n";
3145
Dan Gohmana2489d12010-07-20 23:55:01 +00003146 WriteAsOperandInternal(Out, GV, &TypePrinter, &Machine, GV->getParent());
Dan Gohman466876b2009-08-12 23:32:33 +00003147 Out << " = ";
Chris Lattner37798642001-09-18 04:01:05 +00003148
Chris Lattner1508d3f2008-08-19 05:16:28 +00003149 if (!GV->hasInitializer() && GV->hasExternalLinkage())
3150 Out << "external ";
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003151
Teresa Johnsonc80c8732018-05-25 15:15:39 +00003152 Out << getLinkageNameWithSpace(GV->getLinkage());
Rafael Espindolae4b02312018-01-11 22:15:05 +00003153 PrintDSOLocation(*GV, Out);
Chris Lattner1508d3f2008-08-19 05:16:28 +00003154 PrintVisibility(GV->getVisibility(), Out);
Nico Rieck7157bb72014-01-14 15:22:47 +00003155 PrintDLLStorageClass(GV->getDLLStorageClass(), Out);
Hans Wennborgcbe34b42012-06-23 11:37:03 +00003156 PrintThreadLocalModel(GV->getThreadLocalMode(), Out);
Peter Collingbourne96efdd62016-06-14 21:01:22 +00003157 StringRef UA = getUnnamedAddrEncoding(GV->getUnnamedAddr());
3158 if (!UA.empty())
3159 Out << UA << ' ';
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +00003160
Chris Lattnerac161bf2009-01-02 07:01:27 +00003161 if (unsigned AddressSpace = GV->getType()->getAddressSpace())
3162 Out << "addrspace(" << AddressSpace << ") ";
Michael Gottesman27e7ef32013-02-05 05:57:38 +00003163 if (GV->isExternallyInitialized()) Out << "externally_initialized ";
Misha Brukmana6619a92004-06-21 21:53:56 +00003164 Out << (GV->isConstant() ? "constant " : "global ");
Manuel Jacob5f6eaac2016-01-16 20:30:46 +00003165 TypePrinter.print(GV->getValueType(), Out);
Chris Lattner37798642001-09-18 04:01:05 +00003166
Dan Gohman81313fd2008-09-14 17:21:12 +00003167 if (GV->hasInitializer()) {
3168 Out << ' ';
Devang Patel983c6b12009-07-08 21:44:25 +00003169 writeOperand(GV->getInitializer(), false);
Dan Gohman81313fd2008-09-14 17:21:12 +00003170 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003171
Chris Lattner9380b812010-07-07 23:16:37 +00003172 if (GV->hasSection()) {
3173 Out << ", section \"";
Jonas Devlieghere745918ff2018-05-31 17:01:42 +00003174 printEscapedString(GV->getSection(), Out);
Chris Lattner9380b812010-07-07 23:16:37 +00003175 Out << '"';
3176 }
Rafael Espindola83a362c2015-01-06 22:55:16 +00003177 maybePrintComdat(Out, *GV);
Chris Lattner4b96c542005-11-12 00:10:19 +00003178 if (GV->getAlignment())
Chris Lattnerf8a974d2005-11-06 06:48:53 +00003179 Out << ", align " << GV->getAlignment();
Anton Korobeynikova97b6942007-04-25 14:27:10 +00003180
Peter Collingbournecceae7f2016-05-31 23:01:54 +00003181 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
3182 GV->getAllMetadata(MDs);
3183 printMetadataAttachments(MDs, ", ");
3184
Javed Absarf3d79042017-05-11 12:28:08 +00003185 auto Attrs = GV->getAttributes();
3186 if (Attrs.hasAttributes())
3187 Out << " #" << Machine.getAttributeGroupSlot(Attrs);
3188
Chris Lattner113f4f42002-06-25 16:13:24 +00003189 printInfoComment(*GV);
Chris Lattnerda975502001-09-10 07:58:01 +00003190}
3191
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +00003192void AssemblyWriter::printIndirectSymbol(const GlobalIndirectSymbol *GIS) {
3193 if (GIS->isMaterializable())
Dan Gohman5ded1422010-01-29 23:12:36 +00003194 Out << "; Materializable\n";
3195
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +00003196 WriteAsOperandInternal(Out, GIS, &TypePrinter, &Machine, GIS->getParent());
Rafael Espindola54fc2982015-06-17 17:53:31 +00003197 Out << " = ";
3198
Teresa Johnsonc80c8732018-05-25 15:15:39 +00003199 Out << getLinkageNameWithSpace(GIS->getLinkage());
Rafael Espindolae4b02312018-01-11 22:15:05 +00003200 PrintDSOLocation(*GIS, Out);
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +00003201 PrintVisibility(GIS->getVisibility(), Out);
3202 PrintDLLStorageClass(GIS->getDLLStorageClass(), Out);
3203 PrintThreadLocalModel(GIS->getThreadLocalMode(), Out);
Peter Collingbourne96efdd62016-06-14 21:01:22 +00003204 StringRef UA = getUnnamedAddrEncoding(GIS->getUnnamedAddr());
3205 if (!UA.empty())
3206 Out << UA << ' ';
Anton Korobeynikova97b6942007-04-25 14:27:10 +00003207
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +00003208 if (isa<GlobalAlias>(GIS))
3209 Out << "alias ";
Dmitry Polukhina1feff72016-04-07 12:32:19 +00003210 else if (isa<GlobalIFunc>(GIS))
3211 Out << "ifunc ";
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +00003212 else
Dmitry Polukhina1feff72016-04-07 12:32:19 +00003213 llvm_unreachable("Not an alias or ifunc!");
Anton Korobeynikova97b6942007-04-25 14:27:10 +00003214
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +00003215 TypePrinter.print(GIS->getValueType(), Out);
David Blaikie2f408302015-09-11 03:22:04 +00003216
3217 Out << ", ";
3218
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +00003219 const Constant *IS = GIS->getIndirectSymbol();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003220
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +00003221 if (!IS) {
3222 TypePrinter.print(GIS->getType(), Out);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003223 Out << " <<NULL ALIASEE>>";
Jay Foad92c19132011-08-01 12:48:54 +00003224 } else {
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +00003225 writeOperand(IS, !isa<ConstantExpr>(IS));
Jay Foad92c19132011-08-01 12:48:54 +00003226 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003227
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +00003228 printInfoComment(*GIS);
Chris Lattner1508d3f2008-08-19 05:16:28 +00003229 Out << '\n';
Anton Korobeynikova97b6942007-04-25 14:27:10 +00003230}
3231
David Majnemerdad0a642014-06-27 18:19:56 +00003232void AssemblyWriter::printComdat(const Comdat *C) {
3233 C->print(Out);
3234}
3235
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003236void AssemblyWriter::printTypeIdentities() {
Roman Tereshind96de6f2018-03-22 21:29:07 +00003237 if (TypePrinter.empty())
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003238 return;
Andrew Trickec4b6e72011-09-30 19:48:58 +00003239
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003240 Out << '\n';
Andrew Trickec4b6e72011-09-30 19:48:58 +00003241
Chris Lattner3243ea12009-03-01 00:03:38 +00003242 // Emit all numbered types.
Roman Tereshind96de6f2018-03-22 21:29:07 +00003243 auto &NumberedTypes = TypePrinter.getNumberedTypes();
3244 for (unsigned I = 0, E = NumberedTypes.size(); I != E; ++I) {
3245 Out << '%' << I << " = type ";
Andrew Trickec4b6e72011-09-30 19:48:58 +00003246
Chris Lattner3243ea12009-03-01 00:03:38 +00003247 // Make sure we print out at least one level of the type structure, so
3248 // that we do not get %2 = type %2
Roman Tereshind96de6f2018-03-22 21:29:07 +00003249 TypePrinter.printStructBody(NumberedTypes[I], Out);
Dan Gohman69273e62009-08-12 23:54:22 +00003250 Out << '\n';
Chris Lattner3243ea12009-03-01 00:03:38 +00003251 }
Andrew Trickec4b6e72011-09-30 19:48:58 +00003252
Roman Tereshind96de6f2018-03-22 21:29:07 +00003253 auto &NamedTypes = TypePrinter.getNamedTypes();
3254 for (unsigned I = 0, E = NamedTypes.size(); I != E; ++I) {
3255 PrintLLVMName(Out, NamedTypes[I]->getName(), LocalPrefix);
Chris Lattner1508d3f2008-08-19 05:16:28 +00003256 Out << " = type ";
Reid Spencere7e96712004-05-25 08:53:40 +00003257
3258 // Make sure we print out at least one level of the type structure, so
3259 // that we do not get %FILE = type %FILE
Roman Tereshind96de6f2018-03-22 21:29:07 +00003260 TypePrinter.printStructBody(NamedTypes[I], Out);
Chris Lattner3a36d2c2008-08-19 05:06:27 +00003261 Out << '\n';
Reid Spencere7e96712004-05-25 08:53:40 +00003262 }
Reid Spencer32af9e82007-01-06 07:24:44 +00003263}
3264
Misha Brukmanc566ca362004-03-02 00:22:19 +00003265/// printFunction - Print all aspects of a function.
Chris Lattner113f4f42002-06-25 16:13:24 +00003266void AssemblyWriter::printFunction(const Function *F) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00003267 // Print out the return type and name.
3268 Out << '\n';
Chris Lattner379a8d22003-04-16 20:28:45 +00003269
Misha Brukmana6619a92004-06-21 21:53:56 +00003270 if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00003271
Dan Gohman5ded1422010-01-29 23:12:36 +00003272 if (F->isMaterializable())
3273 Out << "; Materializable\n";
3274
Reid Klecknerb5180542017-03-21 16:57:19 +00003275 const AttributeList &Attrs = F->getAttributes();
3276 if (Attrs.hasAttributes(AttributeList::FunctionIndex)) {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00003277 AttributeSet AS = Attrs.getFnAttributes();
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +00003278 std::string AttrStr;
3279
Reid Klecknerc2cb5602017-04-12 00:38:00 +00003280 for (const Attribute &Attr : AS) {
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +00003281 if (!Attr.isStringAttribute()) {
3282 if (!AttrStr.empty()) AttrStr += ' ';
3283 AttrStr += Attr.getAsString();
3284 }
3285 }
3286
Bill Wendling847a5c32013-04-16 20:55:47 +00003287 if (!AttrStr.empty())
3288 Out << "; Function Attrs: " << AttrStr << '\n';
3289 }
3290
Peter Collingbourne21521892016-06-21 23:42:48 +00003291 Machine.incorporateFunction(F);
3292
3293 if (F->isDeclaration()) {
3294 Out << "declare";
3295 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
3296 F->getAllMetadata(MDs);
3297 printMetadataAttachments(MDs, " ");
3298 Out << ' ';
3299 } else
Reid Spencer7ce2d2a2006-12-29 20:29:48 +00003300 Out << "define ";
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003301
Teresa Johnsonc80c8732018-05-25 15:15:39 +00003302 Out << getLinkageNameWithSpace(F->getLinkage());
Rafael Espindolae4b02312018-01-11 22:15:05 +00003303 PrintDSOLocation(*F, Out);
Chris Lattner3a36d2c2008-08-19 05:06:27 +00003304 PrintVisibility(F->getVisibility(), Out);
Nico Rieck7157bb72014-01-14 15:22:47 +00003305 PrintDLLStorageClass(F->getDLLStorageClass(), Out);
Chris Lattner379a8d22003-04-16 20:28:45 +00003306
Chris Lattnerf7b6d312005-05-06 20:26:43 +00003307 // Print the calling convention.
Micah Villmow857186d2012-09-13 15:11:12 +00003308 if (F->getCallingConv() != CallingConv::C) {
3309 PrintCallingConv(F->getCallingConv(), Out);
3310 Out << " ";
Chris Lattnerf7b6d312005-05-06 20:26:43 +00003311 }
3312
Chris Lattner229907c2011-07-18 04:54:35 +00003313 FunctionType *FT = F->getFunctionType();
Reid Klecknerb5180542017-03-21 16:57:19 +00003314 if (Attrs.hasAttributes(AttributeList::ReturnIndex))
3315 Out << Attrs.getAsString(AttributeList::ReturnIndex) << ' ';
Chris Lattnere101c442009-02-28 21:26:53 +00003316 TypePrinter.print(F->getReturnType(), Out);
Chris Lattner585297e82008-08-19 05:26:17 +00003317 Out << ' ';
Dan Gohmana2489d12010-07-20 23:55:01 +00003318 WriteAsOperandInternal(Out, F, &TypePrinter, &Machine, F->getParent());
Misha Brukmana6619a92004-06-21 21:53:56 +00003319 Out << '(';
Chris Lattnerfee714f2001-09-07 16:36:04 +00003320
Chris Lattner7bfee412001-10-29 16:05:51 +00003321 // Loop over the arguments, printing them...
Justin Bognerd7d1a722015-09-27 22:38:50 +00003322 if (F->isDeclaration() && !IsForDebug) {
Justin Bogner58e08232015-09-02 17:54:41 +00003323 // We're only interested in the type here - don't print argument names.
3324 for (unsigned I = 0, E = FT->getNumParams(); I != E; ++I) {
Chris Lattner82738fe2007-04-18 00:57:22 +00003325 // Insert commas as we go... the first arg doesn't get a comma
Justin Bogner58e08232015-09-02 17:54:41 +00003326 if (I)
3327 Out << ", ";
3328 // Output type...
3329 TypePrinter.print(FT->getParamType(I), Out);
3330
Reid Kleckner6652a522017-04-28 18:37:16 +00003331 AttributeSet ArgAttrs = Attrs.getParamAttributes(I);
3332 if (ArgAttrs.hasAttributes())
3333 Out << ' ' << ArgAttrs.getAsString();
Chris Lattner82738fe2007-04-18 00:57:22 +00003334 }
3335 } else {
Justin Bogner58e08232015-09-02 17:54:41 +00003336 // The arguments are meaningful here, print them in detail.
Justin Bogner58e08232015-09-02 17:54:41 +00003337 for (const Argument &Arg : F->args()) {
Chris Lattner82738fe2007-04-18 00:57:22 +00003338 // Insert commas as we go... the first arg doesn't get a comma
Reid Kleckner6652a522017-04-28 18:37:16 +00003339 if (Arg.getArgNo() != 0)
Justin Bogner58e08232015-09-02 17:54:41 +00003340 Out << ", ";
Reid Kleckner6652a522017-04-28 18:37:16 +00003341 printArgument(&Arg, Attrs.getParamAttributes(Arg.getArgNo()));
Chris Lattner82738fe2007-04-18 00:57:22 +00003342 }
Reid Spencer8c4914c2006-12-31 05:24:50 +00003343 }
Chris Lattnerfee714f2001-09-07 16:36:04 +00003344
3345 // Finish printing arguments...
Chris Lattner113f4f42002-06-25 16:13:24 +00003346 if (FT->isVarArg()) {
Misha Brukmana6619a92004-06-21 21:53:56 +00003347 if (FT->getNumParams()) Out << ", ";
3348 Out << "..."; // Output varargs portion of signature!
Chris Lattnerfee714f2001-09-07 16:36:04 +00003349 }
Misha Brukmana6619a92004-06-21 21:53:56 +00003350 Out << ')';
Peter Collingbourne96efdd62016-06-14 21:01:22 +00003351 StringRef UA = getUnnamedAddrEncoding(F->getUnnamedAddr());
3352 if (!UA.empty())
3353 Out << ' ' << UA;
Alexander Richardson6bcf2ba2018-08-23 09:25:17 +00003354 // We print the function address space if it is non-zero or if we are writing
3355 // a module with a non-zero program address space or if there is no valid
3356 // Module* so that the file can be parsed without the datalayout string.
3357 const Module *Mod = F->getParent();
3358 if (F->getAddressSpace() != 0 || !Mod ||
3359 Mod->getDataLayout().getProgramAddressSpace() != 0)
3360 Out << " addrspace(" << F->getAddressSpace() << ")";
Reid Klecknerb5180542017-03-21 16:57:19 +00003361 if (Attrs.hasAttributes(AttributeList::FunctionIndex))
Bill Wendling04945972013-04-29 23:48:06 +00003362 Out << " #" << Machine.getAttributeGroupSlot(Attrs.getFnAttributes());
Chris Lattner9380b812010-07-07 23:16:37 +00003363 if (F->hasSection()) {
3364 Out << " section \"";
Jonas Devlieghere745918ff2018-05-31 17:01:42 +00003365 printEscapedString(F->getSection(), Out);
Chris Lattner9380b812010-07-07 23:16:37 +00003366 Out << '"';
3367 }
Rafael Espindola83a362c2015-01-06 22:55:16 +00003368 maybePrintComdat(Out, *F);
Chris Lattnerf8a974d2005-11-06 06:48:53 +00003369 if (F->getAlignment())
3370 Out << " align " << F->getAlignment();
Gordon Henriksend930f912008-08-17 18:44:35 +00003371 if (F->hasGC())
3372 Out << " gc \"" << F->getGC() << '"';
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00003373 if (F->hasPrefixData()) {
3374 Out << " prefix ";
3375 writeOperand(F->getPrefixData(), true);
3376 }
Peter Collingbourne51d2de72014-12-03 02:08:38 +00003377 if (F->hasPrologueData()) {
3378 Out << " prologue ";
3379 writeOperand(F->getPrologueData(), true);
3380 }
David Majnemer7fddecc2015-06-17 20:52:32 +00003381 if (F->hasPersonalityFn()) {
3382 Out << " personality ";
3383 writeOperand(F->getPersonalityFn(), /*PrintType=*/true);
3384 }
Peter Collingbourne51d2de72014-12-03 02:08:38 +00003385
Reid Spencer5301e7c2007-01-30 20:08:39 +00003386 if (F->isDeclaration()) {
Chris Lattnerfb483622010-09-02 22:52:10 +00003387 Out << '\n';
Chris Lattnerb2f02e52002-05-06 03:00:40 +00003388 } else {
Peter Collingbourne21521892016-06-21 23:42:48 +00003389 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
3390 F->getAllMetadata(MDs);
3391 printMetadataAttachments(MDs, " ");
3392
Chris Lattnerfb483622010-09-02 22:52:10 +00003393 Out << " {";
3394 // Output all of the function's basic blocks.
Benjamin Krameraf28e7d2016-06-26 14:10:56 +00003395 for (const BasicBlock &BB : *F)
3396 printBasicBlock(&BB);
Chris Lattnerfee714f2001-09-07 16:36:04 +00003397
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +00003398 // Output the function's use-lists.
3399 printUseLists(F);
3400
Misha Brukmana6619a92004-06-21 21:53:56 +00003401 Out << "}\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +00003402 }
3403
Reid Spencer16f2f7f2004-05-26 07:18:52 +00003404 Machine.purgeFunction();
Chris Lattner2f7c9632001-06-06 20:29:01 +00003405}
3406
Misha Brukmanc566ca362004-03-02 00:22:19 +00003407/// printArgument - This member is called for every argument that is passed into
3408/// the function. Simply print it out
Reid Kleckner6652a522017-04-28 18:37:16 +00003409void AssemblyWriter::printArgument(const Argument *Arg, AttributeSet Attrs) {
Chris Lattner2f7c9632001-06-06 20:29:01 +00003410 // Output type...
Chris Lattnere101c442009-02-28 21:26:53 +00003411 TypePrinter.print(Arg->getType(), Out);
Misha Brukmanb1c93172005-04-21 23:48:37 +00003412
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00003413 // Output parameter attributes list
Reid Kleckner6652a522017-04-28 18:37:16 +00003414 if (Attrs.hasAttributes())
3415 Out << ' ' << Attrs.getAsString();
Reid Spencer8c4914c2006-12-31 05:24:50 +00003416
Chris Lattner2f7c9632001-06-06 20:29:01 +00003417 // Output name, if available...
Chris Lattner033935d2008-08-17 04:40:13 +00003418 if (Arg->hasName()) {
3419 Out << ' ';
3420 PrintLLVMName(Out, Arg);
3421 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00003422}
3423
Misha Brukmanc566ca362004-03-02 00:22:19 +00003424/// printBasicBlock - This member is called for each basic block in a method.
Chris Lattner7bfee412001-10-29 16:05:51 +00003425void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Nick Lewycky4d43d3c2008-04-25 16:53:59 +00003426 if (BB->hasName()) { // Print out the label if it exists...
Chris Lattner033935d2008-08-17 04:40:13 +00003427 Out << "\n";
Daniel Dunbare03eecb2009-07-25 23:55:21 +00003428 PrintLLVMName(Out, BB->getName(), LabelPrefix);
Chris Lattner033935d2008-08-17 04:40:13 +00003429 Out << ':';
Nick Lewycky4d43d3c2008-04-25 16:53:59 +00003430 } else if (!BB->use_empty()) { // Don't print block # of no uses...
Bill Wendling35a9c3c2011-04-10 23:18:04 +00003431 Out << "\n; <label>:";
Chris Lattner5e043322007-01-11 03:54:27 +00003432 int Slot = Machine.getLocalSlot(BB);
Chris Lattner757ee0b2004-06-09 19:41:19 +00003433 if (Slot != -1)
Evgeniy Stepanove257f0f2016-01-27 21:53:08 +00003434 Out << Slot << ":";
Chris Lattner757ee0b2004-06-09 19:41:19 +00003435 else
Misha Brukmana6619a92004-06-21 21:53:56 +00003436 Out << "<badref>";
Chris Lattner58185f22002-10-02 19:38:55 +00003437 }
Chris Lattner2447ef52003-11-20 00:09:43 +00003438
Craig Topperc6207612014-04-09 06:08:46 +00003439 if (!BB->getParent()) {
Chris Lattneree97b8b2009-08-17 15:48:08 +00003440 Out.PadToColumn(50);
Dan Gohmane2745262009-08-12 17:23:50 +00003441 Out << "; Error: Block without parent!";
3442 } else if (BB != &BB->getParent()->getEntryBlock()) { // Not the entry block?
Chris Lattnerfb483622010-09-02 22:52:10 +00003443 // Output predecessors for the block.
Chris Lattneree97b8b2009-08-17 15:48:08 +00003444 Out.PadToColumn(50);
Dan Gohmane2745262009-08-12 17:23:50 +00003445 Out << ";";
Gabor Greif6c6b2fd2010-03-25 23:25:28 +00003446 const_pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003447
Chris Lattnerff834c02008-04-22 02:45:44 +00003448 if (PI == PE) {
3449 Out << " No predecessors!";
3450 } else {
Dan Gohman81313fd2008-09-14 17:21:12 +00003451 Out << " preds = ";
Chris Lattnerff834c02008-04-22 02:45:44 +00003452 writeOperand(*PI, false);
3453 for (++PI; PI != PE; ++PI) {
Dan Gohman81313fd2008-09-14 17:21:12 +00003454 Out << ", ";
Chris Lattner78e2e8b2006-12-06 06:24:27 +00003455 writeOperand(*PI, false);
Chris Lattner00211f12003-11-16 22:59:57 +00003456 }
Chris Lattner58185f22002-10-02 19:38:55 +00003457 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00003458 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00003459
Chris Lattnerff834c02008-04-22 02:45:44 +00003460 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +00003461
Misha Brukmana6619a92004-06-21 21:53:56 +00003462 if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00003463
Chris Lattnerfee714f2001-09-07 16:36:04 +00003464 // Output all of the instructions in the basic block...
Benjamin Krameraf28e7d2016-06-26 14:10:56 +00003465 for (const Instruction &I : *BB) {
3466 printInstructionLine(I);
Dan Gohmana4f709e2009-07-13 18:27:59 +00003467 }
Chris Lattner96cdd272004-03-08 18:51:45 +00003468
Misha Brukmana6619a92004-06-21 21:53:56 +00003469 if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, Out);
Chris Lattner2f7c9632001-06-06 20:29:01 +00003470}
3471
Daniel Maleaded9f932013-05-08 20:38:31 +00003472/// printInstructionLine - Print an instruction and a newline character.
3473void AssemblyWriter::printInstructionLine(const Instruction &I) {
3474 printInstruction(I);
3475 Out << '\n';
3476}
3477
Igor Laevsky2aa8caf2015-05-05 13:20:42 +00003478/// printGCRelocateComment - print comment after call to the gc.relocate
3479/// intrinsic indicating base and derived pointer names.
Manuel Jacob83eefa62016-01-05 04:03:00 +00003480void AssemblyWriter::printGCRelocateComment(const GCRelocateInst &Relocate) {
Igor Laevsky2aa8caf2015-05-05 13:20:42 +00003481 Out << " ; (";
Manuel Jacob83eefa62016-01-05 04:03:00 +00003482 writeOperand(Relocate.getBasePtr(), false);
Igor Laevsky2aa8caf2015-05-05 13:20:42 +00003483 Out << ", ";
Manuel Jacob83eefa62016-01-05 04:03:00 +00003484 writeOperand(Relocate.getDerivedPtr(), false);
Igor Laevsky2aa8caf2015-05-05 13:20:42 +00003485 Out << ")";
3486}
3487
Misha Brukmanc566ca362004-03-02 00:22:19 +00003488/// printInfoComment - Print a little comment after the instruction indicating
3489/// which slot it occupies.
Chris Lattner113f4f42002-06-25 16:13:24 +00003490void AssemblyWriter::printInfoComment(const Value &V) {
Manuel Jacob83eefa62016-01-05 04:03:00 +00003491 if (const auto *Relocate = dyn_cast<GCRelocateInst>(&V))
3492 printGCRelocateComment(*Relocate);
Igor Laevsky2aa8caf2015-05-05 13:20:42 +00003493
Bill Wendling847a5c32013-04-16 20:55:47 +00003494 if (AnnotationWriter)
Dan Gohmane34890f2010-02-10 20:41:46 +00003495 AnnotationWriter->printInfoComment(V, Out);
Chris Lattner862e3382001-10-13 06:42:36 +00003496}
3497
Alexander Richardson6bcf2ba2018-08-23 09:25:17 +00003498static void maybePrintCallAddrSpace(const Value *Operand, const Instruction *I,
3499 raw_ostream &Out) {
3500 // We print the address space of the call if it is non-zero.
3501 unsigned CallAddrSpace = Operand->getType()->getPointerAddressSpace();
3502 bool PrintAddrSpace = CallAddrSpace != 0;
3503 if (!PrintAddrSpace) {
3504 const Module *Mod = getModuleFromVal(I);
3505 // We also print it if it is zero but not equal to the program address space
3506 // or if we can't find a valid Module* to make it possible to parse
3507 // the resulting file even without a datalayout string.
3508 if (!Mod || Mod->getDataLayout().getProgramAddressSpace() != 0)
3509 PrintAddrSpace = true;
3510 }
3511 if (PrintAddrSpace)
3512 Out << " addrspace(" << CallAddrSpace << ")";
3513}
3514
Reid Spencere7141c82006-08-28 01:02:49 +00003515// This member is called for each Instruction in a function..
Chris Lattner113f4f42002-06-25 16:13:24 +00003516void AssemblyWriter::printInstruction(const Instruction &I) {
Misha Brukmana6619a92004-06-21 21:53:56 +00003517 if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00003518
Dan Gohman466876b2009-08-12 23:32:33 +00003519 // Print out indentation for an instruction.
Dan Gohmanb4583b12009-08-13 01:41:52 +00003520 Out << " ";
Chris Lattner2f7c9632001-06-06 20:29:01 +00003521
3522 // Print out name if it exists...
Chris Lattner033935d2008-08-17 04:40:13 +00003523 if (I.hasName()) {
3524 PrintLLVMName(Out, &I);
3525 Out << " = ";
Chris Lattner031560c2009-12-29 07:25:48 +00003526 } else if (!I.getType()->isVoidTy()) {
Chris Lattnerb25e5ea2008-08-29 17:19:30 +00003527 // Print out the def slot taken.
3528 int SlotNum = Machine.getLocalSlot(&I);
3529 if (SlotNum == -1)
3530 Out << "<badref> = ";
3531 else
3532 Out << '%' << SlotNum << " = ";
Chris Lattner033935d2008-08-17 04:40:13 +00003533 }
Andrew Trickec4b6e72011-09-30 19:48:58 +00003534
Reid Kleckner5772b772014-04-24 20:14:34 +00003535 if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
3536 if (CI->isMustTailCall())
3537 Out << "musttail ";
3538 else if (CI->isTailCall())
3539 Out << "tail ";
Akira Hatanaka5cfcce122015-11-06 23:55:38 +00003540 else if (CI->isNoTailCall())
3541 Out << "notail ";
Reid Kleckner5772b772014-04-24 20:14:34 +00003542 }
Chris Lattner504f9242003-09-08 17:45:59 +00003543
Chris Lattner2f7c9632001-06-06 20:29:01 +00003544 // Print out the opcode...
Misha Brukmana6619a92004-06-21 21:53:56 +00003545 Out << I.getOpcodeName();
Chris Lattner2f7c9632001-06-06 20:29:01 +00003546
Eli Friedman02e737b2011-08-12 22:50:01 +00003547 // If this is an atomic load or store, print out the atomic marker.
3548 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isAtomic()) ||
3549 (isa<StoreInst>(I) && cast<StoreInst>(I).isAtomic()))
3550 Out << " atomic";
3551
Tim Northover420a2162014-06-13 14:24:07 +00003552 if (isa<AtomicCmpXchgInst>(I) && cast<AtomicCmpXchgInst>(I).isWeak())
3553 Out << " weak";
3554
Eli Friedman02e737b2011-08-12 22:50:01 +00003555 // If this is a volatile operation, print out the volatile marker.
3556 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) ||
3557 (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile()) ||
3558 (isa<AtomicCmpXchgInst>(I) && cast<AtomicCmpXchgInst>(I).isVolatile()) ||
3559 (isa<AtomicRMWInst>(I) && cast<AtomicRMWInst>(I).isVolatile()))
3560 Out << " volatile";
3561
Dan Gohman9c7f8082009-07-27 16:11:46 +00003562 // Print out optimization information.
3563 WriteOptimizationInfo(Out, &I);
3564
Reid Spencer45e52392006-12-03 06:27:29 +00003565 // Print out the compare instruction predicates
Nate Begemand2195702008-05-12 19:01:56 +00003566 if (const CmpInst *CI = dyn_cast<CmpInst>(&I))
Tim Northoverde3aea0412016-08-17 20:25:25 +00003567 Out << ' ' << CmpInst::getPredicateName(CI->getPredicate());
Reid Spencer45e52392006-12-03 06:27:29 +00003568
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003569 // Print out the atomicrmw operation
3570 if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(&I))
3571 writeAtomicRMWOperation(Out, RMWI->getOperation());
3572
Chris Lattner2f7c9632001-06-06 20:29:01 +00003573 // Print out the type of the operands...
Craig Topperc6207612014-04-09 06:08:46 +00003574 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : nullptr;
Chris Lattner2f7c9632001-06-06 20:29:01 +00003575
3576 // Special case conditional branches to swizzle the condition out to the front
Gabor Greifcab008f2009-02-09 15:45:06 +00003577 if (isa<BranchInst>(I) && cast<BranchInst>(I).isConditional()) {
David Blaikieb78e9e52013-02-11 01:16:51 +00003578 const BranchInst &BI(cast<BranchInst>(I));
Dan Gohman81313fd2008-09-14 17:21:12 +00003579 Out << ' ';
Gabor Greifcab008f2009-02-09 15:45:06 +00003580 writeOperand(BI.getCondition(), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00003581 Out << ", ";
Gabor Greifcab008f2009-02-09 15:45:06 +00003582 writeOperand(BI.getSuccessor(0), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00003583 Out << ", ";
Gabor Greifcab008f2009-02-09 15:45:06 +00003584 writeOperand(BI.getSuccessor(1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00003585
Chris Lattner8d48df22002-04-13 18:34:38 +00003586 } else if (isa<SwitchInst>(I)) {
David Blaikieb78e9e52013-02-11 01:16:51 +00003587 const SwitchInst& SI(cast<SwitchInst>(I));
Chris Lattner3ed871f2009-10-27 19:13:16 +00003588 // Special case switch instruction to get formatting nice and correct.
Dan Gohman81313fd2008-09-14 17:21:12 +00003589 Out << ' ';
Eli Friedman95031ed2011-09-29 20:21:17 +00003590 writeOperand(SI.getCondition(), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00003591 Out << ", ";
Eli Friedman95031ed2011-09-29 20:21:17 +00003592 writeOperand(SI.getDefaultDest(), true);
Chris Lattner82ff9232008-08-23 22:52:27 +00003593 Out << " [";
Chandler Carruth927d8e62017-04-12 07:27:28 +00003594 for (auto Case : SI.cases()) {
Dan Gohmanb4583b12009-08-13 01:41:52 +00003595 Out << "\n ";
Chandler Carruth927d8e62017-04-12 07:27:28 +00003596 writeOperand(Case.getCaseValue(), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00003597 Out << ", ";
Chandler Carruth927d8e62017-04-12 07:27:28 +00003598 writeOperand(Case.getCaseSuccessor(), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00003599 }
Dan Gohmanb4583b12009-08-13 01:41:52 +00003600 Out << "\n ]";
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003601 } else if (isa<IndirectBrInst>(I)) {
3602 // Special case indirectbr instruction to get formatting nice and correct.
Chris Lattner3ed871f2009-10-27 19:13:16 +00003603 Out << ' ';
3604 writeOperand(Operand, true);
Dan Gohman43c57402009-10-30 02:01:10 +00003605 Out << ", [";
Andrew Trickec4b6e72011-09-30 19:48:58 +00003606
Chris Lattner3ed871f2009-10-27 19:13:16 +00003607 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) {
3608 if (i != 1)
3609 Out << ", ";
3610 writeOperand(I.getOperand(i), true);
3611 }
3612 Out << ']';
Jay Foad372ad642011-06-20 14:18:48 +00003613 } else if (const PHINode *PN = dyn_cast<PHINode>(&I)) {
Misha Brukmana6619a92004-06-21 21:53:56 +00003614 Out << ' ';
Chris Lattnere101c442009-02-28 21:26:53 +00003615 TypePrinter.print(I.getType(), Out);
Misha Brukmana6619a92004-06-21 21:53:56 +00003616 Out << ' ';
Chris Lattner2f7c9632001-06-06 20:29:01 +00003617
Jay Foad372ad642011-06-20 14:18:48 +00003618 for (unsigned op = 0, Eop = PN->getNumIncomingValues(); op < Eop; ++op) {
Misha Brukmana6619a92004-06-21 21:53:56 +00003619 if (op) Out << ", ";
Dan Gohman81313fd2008-09-14 17:21:12 +00003620 Out << "[ ";
Jay Foad372ad642011-06-20 14:18:48 +00003621 writeOperand(PN->getIncomingValue(op), false); Out << ", ";
3622 writeOperand(PN->getIncomingBlock(op), false); Out << " ]";
Chris Lattner931ef3b2001-06-11 15:04:20 +00003623 }
Dan Gohmana76f0f72008-05-31 19:12:39 +00003624 } else if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(&I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00003625 Out << ' ';
Dan Gohmana76f0f72008-05-31 19:12:39 +00003626 writeOperand(I.getOperand(0), true);
3627 for (const unsigned *i = EVI->idx_begin(), *e = EVI->idx_end(); i != e; ++i)
3628 Out << ", " << *i;
3629 } else if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(&I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00003630 Out << ' ';
3631 writeOperand(I.getOperand(0), true); Out << ", ";
Dan Gohmana76f0f72008-05-31 19:12:39 +00003632 writeOperand(I.getOperand(1), true);
3633 for (const unsigned *i = IVI->idx_begin(), *e = IVI->idx_end(); i != e; ++i)
3634 Out << ", " << *i;
Bill Wendlingfae14752011-08-12 20:24:12 +00003635 } else if (const LandingPadInst *LPI = dyn_cast<LandingPadInst>(&I)) {
3636 Out << ' ';
3637 TypePrinter.print(I.getType(), Out);
David Majnemer7fddecc2015-06-17 20:52:32 +00003638 if (LPI->isCleanup() || LPI->getNumClauses() != 0)
3639 Out << '\n';
Bill Wendlingfae14752011-08-12 20:24:12 +00003640
3641 if (LPI->isCleanup())
3642 Out << " cleanup";
3643
3644 for (unsigned i = 0, e = LPI->getNumClauses(); i != e; ++i) {
3645 if (i != 0 || LPI->isCleanup()) Out << "\n";
3646 if (LPI->isCatch(i))
3647 Out << " catch ";
3648 else
3649 Out << " filter ";
3650
3651 writeOperand(LPI->getClause(i), true);
3652 }
David Majnemer8a1c45d2015-12-12 05:38:55 +00003653 } else if (const auto *CatchSwitch = dyn_cast<CatchSwitchInst>(&I)) {
3654 Out << " within ";
3655 writeOperand(CatchSwitch->getParentPad(), /*PrintType=*/false);
David Majnemer654e1302015-07-31 17:58:14 +00003656 Out << " [";
David Majnemer8a1c45d2015-12-12 05:38:55 +00003657 unsigned Op = 0;
3658 for (const BasicBlock *PadBB : CatchSwitch->handlers()) {
3659 if (Op > 0)
3660 Out << ", ";
3661 writeOperand(PadBB, /*PrintType=*/true);
3662 ++Op;
3663 }
3664 Out << "] unwind ";
3665 if (const BasicBlock *UnwindDest = CatchSwitch->getUnwindDest())
3666 writeOperand(UnwindDest, /*PrintType=*/true);
3667 else
3668 Out << "to caller";
3669 } else if (const auto *FPI = dyn_cast<FuncletPadInst>(&I)) {
3670 Out << " within ";
3671 writeOperand(FPI->getParentPad(), /*PrintType=*/false);
3672 Out << " [";
3673 for (unsigned Op = 0, NumOps = FPI->getNumArgOperands(); Op < NumOps;
David Majnemer654e1302015-07-31 17:58:14 +00003674 ++Op) {
3675 if (Op > 0)
3676 Out << ", ";
David Majnemer8a1c45d2015-12-12 05:38:55 +00003677 writeOperand(FPI->getArgOperand(Op), /*PrintType=*/true);
David Majnemer654e1302015-07-31 17:58:14 +00003678 }
David Majnemer8a1c45d2015-12-12 05:38:55 +00003679 Out << ']';
Devang Patel59643e52008-02-23 00:35:18 +00003680 } else if (isa<ReturnInst>(I) && !Operand) {
3681 Out << " void";
David Majnemer0bc0eef2015-08-15 02:46:08 +00003682 } else if (const auto *CRI = dyn_cast<CatchReturnInst>(&I)) {
David Majnemer8a1c45d2015-12-12 05:38:55 +00003683 Out << " from ";
3684 writeOperand(CRI->getOperand(0), /*PrintType=*/false);
David Majnemer0bc0eef2015-08-15 02:46:08 +00003685
3686 Out << " to ";
David Majnemer8a1c45d2015-12-12 05:38:55 +00003687 writeOperand(CRI->getOperand(1), /*PrintType=*/true);
David Majnemer654e1302015-07-31 17:58:14 +00003688 } else if (const auto *CRI = dyn_cast<CleanupReturnInst>(&I)) {
David Majnemer8a1c45d2015-12-12 05:38:55 +00003689 Out << " from ";
3690 writeOperand(CRI->getOperand(0), /*PrintType=*/false);
David Majnemer654e1302015-07-31 17:58:14 +00003691
3692 Out << " unwind ";
3693 if (CRI->hasUnwindDest())
David Majnemer8a1c45d2015-12-12 05:38:55 +00003694 writeOperand(CRI->getOperand(1), /*PrintType=*/true);
Joseph Tremoulet9ce71f72015-09-03 09:09:43 +00003695 else
3696 Out << "to caller";
Chris Lattnerf7b6d312005-05-06 20:26:43 +00003697 } else if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
3698 // Print the calling convention being used.
Micah Villmow857186d2012-09-13 15:11:12 +00003699 if (CI->getCallingConv() != CallingConv::C) {
3700 Out << " ";
3701 PrintCallingConv(CI->getCallingConv(), Out);
Chris Lattnerf7b6d312005-05-06 20:26:43 +00003702 }
3703
Gabor Greifc9a92512010-06-23 13:09:06 +00003704 Operand = CI->getCalledValue();
Ahmed Bougachaaa9fe532016-12-21 23:26:13 +00003705 FunctionType *FTy = CI->getFunctionType();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003706 Type *RetTy = FTy->getReturnType();
Reid Klecknerb5180542017-03-21 16:57:19 +00003707 const AttributeList &PAL = CI->getAttributes();
Chris Lattner2f2d9472001-11-06 21:28:12 +00003708
Reid Klecknerb5180542017-03-21 16:57:19 +00003709 if (PAL.hasAttributes(AttributeList::ReturnIndex))
3710 Out << ' ' << PAL.getAsString(AttributeList::ReturnIndex);
Devang Patel221fe422008-09-29 20:49:50 +00003711
Alexander Richardson6bcf2ba2018-08-23 09:25:17 +00003712 // Only print addrspace(N) if necessary:
3713 maybePrintCallAddrSpace(Operand, &I, Out);
3714
Chris Lattner463d6a52003-08-05 15:34:45 +00003715 // If possible, print out the short form of the call instruction. We can
Chris Lattner6915f8f2002-04-07 22:49:37 +00003716 // only do this if the first argument is a pointer to a nonvararg function,
Chris Lattner463d6a52003-08-05 15:34:45 +00003717 // and if the return type is not a pointer to a function.
Chris Lattner2f2d9472001-11-06 21:28:12 +00003718 //
Dan Gohman81313fd2008-09-14 17:21:12 +00003719 Out << ' ';
David Blaikie23af6482015-04-16 23:24:18 +00003720 TypePrinter.print(FTy->isVarArg() ? FTy : RetTy, Out);
3721 Out << ' ';
3722 writeOperand(Operand, false);
Misha Brukmana6619a92004-06-21 21:53:56 +00003723 Out << '(';
Gabor Greifc9a92512010-06-23 13:09:06 +00003724 for (unsigned op = 0, Eop = CI->getNumArgOperands(); op < Eop; ++op) {
3725 if (op > 0)
Dan Gohman81313fd2008-09-14 17:21:12 +00003726 Out << ", ";
Reid Kleckner6652a522017-04-28 18:37:16 +00003727 writeParamOperand(CI->getArgOperand(op), PAL.getParamAttributes(op));
Chris Lattner2f7c9632001-06-06 20:29:01 +00003728 }
Reid Kleckner83498642014-08-26 00:33:28 +00003729
3730 // Emit an ellipsis if this is a musttail call in a vararg function. This
3731 // is only to aid readability, musttail calls forward varargs by default.
3732 if (CI->isMustTailCall() && CI->getParent() &&
3733 CI->getParent()->getParent() &&
3734 CI->getParent()->getParent()->isVarArg())
3735 Out << ", ...";
3736
Dan Gohman81313fd2008-09-14 17:21:12 +00003737 Out << ')';
Reid Klecknerb5180542017-03-21 16:57:19 +00003738 if (PAL.hasAttributes(AttributeList::FunctionIndex))
Bill Wendlinga0323742013-02-22 09:09:42 +00003739 Out << " #" << Machine.getAttributeGroupSlot(PAL.getFnAttributes());
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00003740
3741 writeOperandBundles(CI);
Chris Lattner113f4f42002-06-25 16:13:24 +00003742 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
Gabor Greifa2fbc0a2010-03-24 13:21:49 +00003743 Operand = II->getCalledValue();
Ahmed Bougachaaa9fe532016-12-21 23:26:13 +00003744 FunctionType *FTy = II->getFunctionType();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003745 Type *RetTy = FTy->getReturnType();
Reid Klecknerb5180542017-03-21 16:57:19 +00003746 const AttributeList &PAL = II->getAttributes();
Chris Lattner463d6a52003-08-05 15:34:45 +00003747
Chris Lattnerf7b6d312005-05-06 20:26:43 +00003748 // Print the calling convention being used.
Micah Villmow857186d2012-09-13 15:11:12 +00003749 if (II->getCallingConv() != CallingConv::C) {
3750 Out << " ";
3751 PrintCallingConv(II->getCallingConv(), Out);
Chris Lattnerf7b6d312005-05-06 20:26:43 +00003752 }
3753
Reid Klecknerb5180542017-03-21 16:57:19 +00003754 if (PAL.hasAttributes(AttributeList::ReturnIndex))
3755 Out << ' ' << PAL.getAsString(AttributeList::ReturnIndex);
Devang Patel221fe422008-09-29 20:49:50 +00003756
Alexander Richardson6bcf2ba2018-08-23 09:25:17 +00003757 // Only print addrspace(N) if necessary:
3758 maybePrintCallAddrSpace(Operand, &I, Out);
3759
Chris Lattner463d6a52003-08-05 15:34:45 +00003760 // If possible, print out the short form of the invoke instruction. We can
3761 // only do this if the first argument is a pointer to a nonvararg function,
3762 // and if the return type is not a pointer to a function.
3763 //
Dan Gohmanc7e00ba2008-10-15 18:02:08 +00003764 Out << ' ';
David Blaikie445e3fb2015-04-24 19:32:54 +00003765 TypePrinter.print(FTy->isVarArg() ? FTy : RetTy, Out);
3766 Out << ' ';
3767 writeOperand(Operand, false);
Misha Brukmana6619a92004-06-21 21:53:56 +00003768 Out << '(';
Gabor Greifc9a92512010-06-23 13:09:06 +00003769 for (unsigned op = 0, Eop = II->getNumArgOperands(); op < Eop; ++op) {
Gabor Greifa2fbc0a2010-03-24 13:21:49 +00003770 if (op)
Dan Gohman81313fd2008-09-14 17:21:12 +00003771 Out << ", ";
Reid Kleckner6652a522017-04-28 18:37:16 +00003772 writeParamOperand(II->getArgOperand(op), PAL.getParamAttributes(op));
Chris Lattner862e3382001-10-13 06:42:36 +00003773 }
3774
Dan Gohman81313fd2008-09-14 17:21:12 +00003775 Out << ')';
Reid Klecknerb5180542017-03-21 16:57:19 +00003776 if (PAL.hasAttributes(AttributeList::FunctionIndex))
Bill Wendlinga0323742013-02-22 09:09:42 +00003777 Out << " #" << Machine.getAttributeGroupSlot(PAL.getFnAttributes());
Devang Patela05633e2008-09-26 22:53:05 +00003778
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00003779 writeOperandBundles(II);
3780
Dan Gohmanb4583b12009-08-13 01:41:52 +00003781 Out << "\n to ";
Chris Lattner862e3382001-10-13 06:42:36 +00003782 writeOperand(II->getNormalDest(), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00003783 Out << " unwind ";
Chris Lattnerfae8ab32004-02-08 21:44:31 +00003784 writeOperand(II->getUnwindDest(), true);
Victor Hernandez8acf2952009-10-23 21:09:37 +00003785 } else if (const AllocaInst *AI = dyn_cast<AllocaInst>(&I)) {
Misha Brukmana6619a92004-06-21 21:53:56 +00003786 Out << ' ';
Reid Kleckner20844032014-01-25 01:24:06 +00003787 if (AI->isUsedWithInAlloca())
David Majnemerc4ab61c2014-03-09 06:41:58 +00003788 Out << "inalloca ";
Manman Ren9bfd0d02016-04-01 21:41:15 +00003789 if (AI->isSwiftError())
3790 Out << "swifterror ";
David Majnemerc4ab61c2014-03-09 06:41:58 +00003791 TypePrinter.print(AI->getAllocatedType(), Out);
Duncan P. N. Exon Smith720762e2015-03-13 19:30:44 +00003792
3793 // Explicitly write the array size if the code is broken, if it's an array
3794 // allocation, or if the type is not canonical for scalar allocations. The
3795 // latter case prevents the type from mutating when round-tripping through
3796 // assembly.
3797 if (!AI->getArraySize() || AI->isArrayAllocation() ||
3798 !AI->getArraySize()->getType()->isIntegerTy(32)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00003799 Out << ", ";
Chris Lattner8d48df22002-04-13 18:34:38 +00003800 writeOperand(AI->getArraySize(), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00003801 }
Nate Begeman848622f2005-11-05 09:21:28 +00003802 if (AI->getAlignment()) {
Chris Lattner7aeee3a2005-11-05 21:20:34 +00003803 Out << ", align " << AI->getAlignment();
Nate Begeman848622f2005-11-05 09:21:28 +00003804 }
Matt Arsenault3c1fc762017-04-10 22:27:50 +00003805
3806 unsigned AddrSpace = AI->getType()->getAddressSpace();
3807 if (AddrSpace != 0) {
3808 Out << ", addrspace(" << AddrSpace << ')';
3809 }
Chris Lattner862e3382001-10-13 06:42:36 +00003810 } else if (isa<CastInst>(I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00003811 if (Operand) {
3812 Out << ' ';
3813 writeOperand(Operand, true); // Work with broken code
3814 }
Misha Brukmana6619a92004-06-21 21:53:56 +00003815 Out << " to ";
Chris Lattnere101c442009-02-28 21:26:53 +00003816 TypePrinter.print(I.getType(), Out);
Chris Lattner5b337482003-10-18 05:57:43 +00003817 } else if (isa<VAArgInst>(I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00003818 if (Operand) {
3819 Out << ' ';
3820 writeOperand(Operand, true); // Work with broken code
3821 }
Misha Brukmana6619a92004-06-21 21:53:56 +00003822 Out << ", ";
Chris Lattnere101c442009-02-28 21:26:53 +00003823 TypePrinter.print(I.getType(), Out);
3824 } else if (Operand) { // Print the normal way.
David Blaikiea79ac142015-02-27 21:17:42 +00003825 if (const auto *GEP = dyn_cast<GetElementPtrInst>(&I)) {
David Blaikie79e6c742015-02-27 19:29:02 +00003826 Out << ' ';
3827 TypePrinter.print(GEP->getSourceElementType(), Out);
3828 Out << ',';
David Blaikiea79ac142015-02-27 21:17:42 +00003829 } else if (const auto *LI = dyn_cast<LoadInst>(&I)) {
3830 Out << ' ';
3831 TypePrinter.print(LI->getType(), Out);
Benjamin Kramer257ed692015-03-02 15:24:41 +00003832 Out << ',';
David Blaikie79e6c742015-02-27 19:29:02 +00003833 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00003834
Misha Brukmanb1c93172005-04-21 23:48:37 +00003835 // PrintAllTypes - Instructions who have operands of all the same type
Chris Lattner2f7c9632001-06-06 20:29:01 +00003836 // omit the type from all but the first operand. If the instruction has
3837 // different type operands (for example br), then they are all printed.
3838 bool PrintAllTypes = false;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003839 Type *TheType = Operand->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +00003840
Reid Spencer0cdd04f2007-02-02 13:54:55 +00003841 // Select, Store and ShuffleVector always print all types.
Devang Patelce556d92008-03-04 22:05:14 +00003842 if (isa<SelectInst>(I) || isa<StoreInst>(I) || isa<ShuffleVectorInst>(I)
3843 || isa<ReturnInst>(I)) {
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00003844 PrintAllTypes = true;
3845 } else {
3846 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
3847 Operand = I.getOperand(i);
Nuno Lopes04fe2f02009-01-15 18:40:57 +00003848 // note that Operand shouldn't be null, but the test helps make dump()
3849 // more tolerant of malformed IR
Nuno Lopes0971e772009-01-14 17:51:41 +00003850 if (Operand && Operand->getType() != TheType) {
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00003851 PrintAllTypes = true; // We have differing types! Print them all!
3852 break;
3853 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00003854 }
3855 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00003856
Chris Lattner7bfee412001-10-29 16:05:51 +00003857 if (!PrintAllTypes) {
Misha Brukmana6619a92004-06-21 21:53:56 +00003858 Out << ' ';
Chris Lattnere101c442009-02-28 21:26:53 +00003859 TypePrinter.print(TheType, Out);
Chris Lattner7bfee412001-10-29 16:05:51 +00003860 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00003861
Dan Gohman81313fd2008-09-14 17:21:12 +00003862 Out << ' ';
Chris Lattner113f4f42002-06-25 16:13:24 +00003863 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
Dan Gohman81313fd2008-09-14 17:21:12 +00003864 if (i) Out << ", ";
Chris Lattner113f4f42002-06-25 16:13:24 +00003865 writeOperand(I.getOperand(i), PrintAllTypes);
Chris Lattner2f7c9632001-06-06 20:29:01 +00003866 }
3867 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003868
Eli Friedman59b66882011-08-09 23:02:53 +00003869 // Print atomic ordering/alignment for memory operations
3870 if (const LoadInst *LI = dyn_cast<LoadInst>(&I)) {
3871 if (LI->isAtomic())
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003872 writeAtomic(LI->getContext(), LI->getOrdering(), LI->getSyncScopeID());
Eli Friedman59b66882011-08-09 23:02:53 +00003873 if (LI->getAlignment())
3874 Out << ", align " << LI->getAlignment();
3875 } else if (const StoreInst *SI = dyn_cast<StoreInst>(&I)) {
3876 if (SI->isAtomic())
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003877 writeAtomic(SI->getContext(), SI->getOrdering(), SI->getSyncScopeID());
Eli Friedman59b66882011-08-09 23:02:53 +00003878 if (SI->getAlignment())
3879 Out << ", align " << SI->getAlignment();
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003880 } else if (const AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(&I)) {
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003881 writeAtomicCmpXchg(CXI->getContext(), CXI->getSuccessOrdering(),
3882 CXI->getFailureOrdering(), CXI->getSyncScopeID());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003883 } else if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(&I)) {
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003884 writeAtomic(RMWI->getContext(), RMWI->getOrdering(),
3885 RMWI->getSyncScopeID());
Eli Friedmanfee02c62011-07-25 23:16:38 +00003886 } else if (const FenceInst *FI = dyn_cast<FenceInst>(&I)) {
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003887 writeAtomic(FI->getContext(), FI->getOrdering(), FI->getSyncScopeID());
Christopher Lamb84485702007-04-22 19:24:39 +00003888 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00003889
Chris Lattnerc9558df2009-12-28 20:10:43 +00003890 // Print Metadata info.
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00003891 SmallVector<std::pair<unsigned, MDNode *>, 4> InstMD;
Nick Lewyckyba8ec9a2010-02-25 06:53:04 +00003892 I.getAllMetadata(InstMD);
Duncan P. N. Exon Smithd3e4c2a2015-04-24 21:06:21 +00003893 printMetadataAttachments(InstMD, ", ");
Duncan P. N. Exon Smith86979272015-04-24 20:59:52 +00003894
3895 // Print a nice comment.
Chris Lattner862e3382001-10-13 06:42:36 +00003896 printInfoComment(I);
Chris Lattner2f7c9632001-06-06 20:29:01 +00003897}
3898
Duncan P. N. Exon Smith86979272015-04-24 20:59:52 +00003899void AssemblyWriter::printMetadataAttachments(
Duncan P. N. Exon Smithd3e4c2a2015-04-24 21:06:21 +00003900 const SmallVectorImpl<std::pair<unsigned, MDNode *>> &MDs,
3901 StringRef Separator) {
Duncan P. N. Exon Smith86979272015-04-24 20:59:52 +00003902 if (MDs.empty())
3903 return;
3904
Duncan P. N. Exon Smithe30f10e2015-04-24 21:03:05 +00003905 if (MDNames.empty())
Mehdi Aminib9b50aa2016-01-07 20:14:30 +00003906 MDs[0].second->getContext().getMDKindNames(MDNames);
Duncan P. N. Exon Smithe30f10e2015-04-24 21:03:05 +00003907
Duncan P. N. Exon Smith86979272015-04-24 20:59:52 +00003908 for (const auto &I : MDs) {
3909 unsigned Kind = I.first;
Duncan P. N. Exon Smithd3e4c2a2015-04-24 21:06:21 +00003910 Out << Separator;
Filipe Cabecinhas62431b12015-06-02 21:25:08 +00003911 if (Kind < MDNames.size()) {
3912 Out << "!";
3913 printMetadataIdentifier(MDNames[Kind], Out);
3914 } else
Duncan P. N. Exon Smithd3e4c2a2015-04-24 21:06:21 +00003915 Out << "!<unknown kind #" << Kind << ">";
Duncan P. N. Exon Smith86979272015-04-24 20:59:52 +00003916 Out << ' ';
3917 WriteAsOperandInternal(Out, I.second, &TypePrinter, &Machine, TheModule);
3918 }
3919}
3920
Daniel Maleaded9f932013-05-08 20:38:31 +00003921void AssemblyWriter::writeMDNode(unsigned Slot, const MDNode *Node) {
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +00003922 Out << '!' << Slot << " = ";
Daniel Maleaded9f932013-05-08 20:38:31 +00003923 printMDNodeBody(Node);
Duncan P. N. Exon Smith738889f2015-02-25 22:46:38 +00003924 Out << "\n";
Daniel Maleaded9f932013-05-08 20:38:31 +00003925}
3926
Chris Lattnerbddea6a2009-12-31 02:13:35 +00003927void AssemblyWriter::writeAllMDNodes() {
3928 SmallVector<const MDNode *, 16> Nodes;
Chris Lattnercf4a76e2009-12-31 02:20:11 +00003929 Nodes.resize(Machine.mdn_size());
3930 for (SlotTracker::mdn_iterator I = Machine.mdn_begin(), E = Machine.mdn_end();
3931 I != E; ++I)
Chris Lattnerbddea6a2009-12-31 02:13:35 +00003932 Nodes[I->second] = cast<MDNode>(I->first);
Andrew Trickec4b6e72011-09-30 19:48:58 +00003933
Chris Lattnerbddea6a2009-12-31 02:13:35 +00003934 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Daniel Maleaded9f932013-05-08 20:38:31 +00003935 writeMDNode(i, Nodes[i]);
Chris Lattnerbddea6a2009-12-31 02:13:35 +00003936 }
3937}
3938
3939void AssemblyWriter::printMDNodeBody(const MDNode *Node) {
Dan Gohmana2489d12010-07-20 23:55:01 +00003940 WriteMDNodeBodyInternal(Out, Node, &TypePrinter, &Machine, TheModule);
Chris Lattnerbddea6a2009-12-31 02:13:35 +00003941}
Chris Lattner2f7c9632001-06-06 20:29:01 +00003942
Bill Wendling829b4782013-02-11 08:43:33 +00003943void AssemblyWriter::writeAllAttributeGroups() {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00003944 std::vector<std::pair<AttributeSet, unsigned>> asVec;
Bill Wendling829b4782013-02-11 08:43:33 +00003945 asVec.resize(Machine.as_size());
3946
3947 for (SlotTracker::as_iterator I = Machine.as_begin(), E = Machine.as_end();
3948 I != E; ++I)
3949 asVec[I->second] = *I;
3950
Benjamin Krameraf28e7d2016-06-26 14:10:56 +00003951 for (const auto &I : asVec)
3952 Out << "attributes #" << I.second << " = { "
Reid Klecknerc2cb5602017-04-12 00:38:00 +00003953 << I.first.getAsString(true) << " }\n";
Bill Wendling829b4782013-02-11 08:43:33 +00003954}
3955
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +00003956void AssemblyWriter::printUseListOrder(const UseListOrder &Order) {
3957 bool IsInFunction = Machine.getFunction();
3958 if (IsInFunction)
3959 Out << " ";
3960
3961 Out << "uselistorder";
3962 if (const BasicBlock *BB =
3963 IsInFunction ? nullptr : dyn_cast<BasicBlock>(Order.V)) {
3964 Out << "_bb ";
3965 writeOperand(BB->getParent(), false);
3966 Out << ", ";
3967 writeOperand(BB, false);
3968 } else {
3969 Out << " ";
3970 writeOperand(Order.V, true);
3971 }
3972 Out << ", { ";
3973
3974 assert(Order.Shuffle.size() >= 2 && "Shuffle too small");
3975 Out << Order.Shuffle[0];
3976 for (unsigned I = 1, E = Order.Shuffle.size(); I != E; ++I)
3977 Out << ", " << Order.Shuffle[I];
3978 Out << " }\n";
3979}
3980
3981void AssemblyWriter::printUseLists(const Function *F) {
3982 auto hasMore =
3983 [&]() { return !UseListOrders.empty() && UseListOrders.back().F == F; };
3984 if (!hasMore())
3985 // Nothing to do.
3986 return;
3987
3988 Out << "\n; uselistorder directives\n";
3989 while (hasMore()) {
3990 printUseListOrder(UseListOrders.back());
3991 UseListOrders.pop_back();
3992 }
3993}
3994
Chris Lattner2f7c9632001-06-06 20:29:01 +00003995//===----------------------------------------------------------------------===//
3996// External Interface declarations
3997//===----------------------------------------------------------------------===//
3998
George Burgess IVe1100f52016-02-02 22:46:49 +00003999void Function::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW,
4000 bool ShouldPreserveUseListOrder,
4001 bool IsForDebug) const {
4002 SlotTracker SlotTable(this->getParent());
4003 formatted_raw_ostream OS(ROS);
4004 AssemblyWriter W(OS, SlotTable, this->getParent(), AAW,
4005 IsForDebug,
4006 ShouldPreserveUseListOrder);
4007 W.printFunction(this);
4008}
4009
Duncan P. N. Exon Smithc4f0a322015-04-15 02:12:41 +00004010void Module::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW,
Justin Bognerd7d1a722015-09-27 22:38:50 +00004011 bool ShouldPreserveUseListOrder, bool IsForDebug) const {
Chris Lattnere36fd8a2008-08-19 04:26:57 +00004012 SlotTracker SlotTable(this);
Dan Gohmane2745262009-08-12 17:23:50 +00004013 formatted_raw_ostream OS(ROS);
Justin Bognerd7d1a722015-09-27 22:38:50 +00004014 AssemblyWriter W(OS, SlotTable, this, AAW, IsForDebug,
4015 ShouldPreserveUseListOrder);
Chris Lattnerefb5e392009-12-31 02:23:35 +00004016 W.printModule(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00004017}
4018
Justin Bognerd7d1a722015-09-27 22:38:50 +00004019void NamedMDNode::print(raw_ostream &ROS, bool IsForDebug) const {
Dan Gohman2637cc12010-07-21 23:38:33 +00004020 SlotTracker SlotTable(getParent());
4021 formatted_raw_ostream OS(ROS);
Justin Bognerd7d1a722015-09-27 22:38:50 +00004022 AssemblyWriter W(OS, SlotTable, getParent(), nullptr, IsForDebug);
Dan Gohman2637cc12010-07-21 23:38:33 +00004023 W.printNamedMDNode(this);
4024}
4025
Duncan P. N. Exon Smith0ecff952016-04-20 17:27:44 +00004026void NamedMDNode::print(raw_ostream &ROS, ModuleSlotTracker &MST,
4027 bool IsForDebug) const {
4028 Optional<SlotTracker> LocalST;
4029 SlotTracker *SlotTable;
4030 if (auto *ST = MST.getMachine())
4031 SlotTable = ST;
4032 else {
4033 LocalST.emplace(getParent());
4034 SlotTable = &*LocalST;
4035 }
4036
4037 formatted_raw_ostream OS(ROS);
4038 AssemblyWriter W(OS, *SlotTable, getParent(), nullptr, IsForDebug);
4039 W.printNamedMDNode(this);
4040}
4041
Justin Bognerd7d1a722015-09-27 22:38:50 +00004042void Comdat::print(raw_ostream &ROS, bool /*IsForDebug*/) const {
David Majnemerdad0a642014-06-27 18:19:56 +00004043 PrintLLVMName(ROS, getName(), ComdatPrefix);
4044 ROS << " = comdat ";
4045
4046 switch (getSelectionKind()) {
4047 case Comdat::Any:
4048 ROS << "any";
4049 break;
4050 case Comdat::ExactMatch:
4051 ROS << "exactmatch";
4052 break;
4053 case Comdat::Largest:
4054 ROS << "largest";
4055 break;
4056 case Comdat::NoDuplicates:
4057 ROS << "noduplicates";
4058 break;
4059 case Comdat::SameSize:
4060 ROS << "samesize";
4061 break;
4062 }
4063
4064 ROS << '\n';
4065}
4066
Quentin Colombet774b1ef2016-03-07 22:32:42 +00004067void Type::print(raw_ostream &OS, bool /*IsForDebug*/, bool NoDetails) const {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004068 TypePrinting TP;
4069 TP.print(const_cast<Type*>(this), OS);
Andrew Trickec4b6e72011-09-30 19:48:58 +00004070
Quentin Colombet774b1ef2016-03-07 22:32:42 +00004071 if (NoDetails)
4072 return;
4073
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004074 // If the type is a named struct type, print the body as well.
4075 if (StructType *STy = dyn_cast<StructType>(const_cast<Type*>(this)))
Chris Lattner01beceb2011-08-12 18:07:07 +00004076 if (!STy->isLiteral()) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004077 OS << " = type ";
4078 TP.printStructBody(STy, OS);
4079 }
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00004080}
4081
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +00004082static bool isReferencingMDNode(const Instruction &I) {
4083 if (const auto *CI = dyn_cast<CallInst>(&I))
4084 if (Function *F = CI->getCalledFunction())
Justin Lebar9cbc3012016-07-28 23:58:15 +00004085 if (F->isIntrinsic())
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +00004086 for (auto &Op : I.operands())
4087 if (auto *V = dyn_cast_or_null<MetadataAsValue>(Op))
4088 if (isa<MDNode>(V->getMetadata()))
4089 return true;
4090 return false;
4091}
4092
Justin Bognerd7d1a722015-09-27 22:38:50 +00004093void Value::print(raw_ostream &ROS, bool IsForDebug) const {
Duncan P. N. Exon Smith1f8a99a2015-06-27 00:38:26 +00004094 bool ShouldInitializeAllMetadata = false;
4095 if (auto *I = dyn_cast<Instruction>(this))
4096 ShouldInitializeAllMetadata = isReferencingMDNode(*I);
4097 else if (isa<Function>(this) || isa<MetadataAsValue>(this))
4098 ShouldInitializeAllMetadata = true;
4099
4100 ModuleSlotTracker MST(getModuleFromVal(this), ShouldInitializeAllMetadata);
Justin Bognerd7d1a722015-09-27 22:38:50 +00004101 print(ROS, MST, IsForDebug);
Duncan P. N. Exon Smith1f8a99a2015-06-27 00:38:26 +00004102}
4103
Justin Bognerd7d1a722015-09-27 22:38:50 +00004104void Value::print(raw_ostream &ROS, ModuleSlotTracker &MST,
4105 bool IsForDebug) const {
Dan Gohman12dad632009-08-12 20:56:03 +00004106 formatted_raw_ostream OS(ROS);
Duncan P. N. Exon Smith1f8a99a2015-06-27 00:38:26 +00004107 SlotTracker EmptySlotTable(static_cast<const Module *>(nullptr));
4108 SlotTracker &SlotTable =
4109 MST.getMachine() ? *MST.getMachine() : EmptySlotTable;
4110 auto incorporateFunction = [&](const Function *F) {
4111 if (F)
4112 MST.incorporateFunction(*F);
4113 };
4114
Chris Lattner0c19df42008-08-23 22:23:09 +00004115 if (const Instruction *I = dyn_cast<Instruction>(this)) {
Duncan P. N. Exon Smith1f8a99a2015-06-27 00:38:26 +00004116 incorporateFunction(I->getParent() ? I->getParent()->getParent() : nullptr);
Justin Bognerd7d1a722015-09-27 22:38:50 +00004117 AssemblyWriter W(OS, SlotTable, getModuleFromVal(I), nullptr, IsForDebug);
Chris Lattnerefb5e392009-12-31 02:23:35 +00004118 W.printInstruction(*I);
Chris Lattner0c19df42008-08-23 22:23:09 +00004119 } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(this)) {
Duncan P. N. Exon Smith1f8a99a2015-06-27 00:38:26 +00004120 incorporateFunction(BB->getParent());
Justin Bognerd7d1a722015-09-27 22:38:50 +00004121 AssemblyWriter W(OS, SlotTable, getModuleFromVal(BB), nullptr, IsForDebug);
Chris Lattnerefb5e392009-12-31 02:23:35 +00004122 W.printBasicBlock(BB);
Chris Lattner0c19df42008-08-23 22:23:09 +00004123 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(this)) {
Justin Bognerd7d1a722015-09-27 22:38:50 +00004124 AssemblyWriter W(OS, SlotTable, GV->getParent(), nullptr, IsForDebug);
Chris Lattnerefb5e392009-12-31 02:23:35 +00004125 if (const GlobalVariable *V = dyn_cast<GlobalVariable>(GV))
4126 W.printGlobal(V);
4127 else if (const Function *F = dyn_cast<Function>(GV))
4128 W.printFunction(F);
4129 else
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +00004130 W.printIndirectSymbol(cast<GlobalIndirectSymbol>(GV));
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00004131 } else if (const MetadataAsValue *V = dyn_cast<MetadataAsValue>(this)) {
Duncan P. N. Exon Smith1f8a99a2015-06-27 00:38:26 +00004132 V->getMetadata()->print(ROS, MST, getModuleFromVal(V));
Chris Lattner0c19df42008-08-23 22:23:09 +00004133 } else if (const Constant *C = dyn_cast<Constant>(this)) {
Chris Lattner92c5c122009-02-28 23:20:19 +00004134 TypePrinting TypePrinter;
Chris Lattnere101c442009-02-28 21:26:53 +00004135 TypePrinter.print(C->getType(), OS);
Chris Lattner2ee62d22009-02-28 21:11:05 +00004136 OS << ' ';
Duncan P. N. Exon Smith1f8a99a2015-06-27 00:38:26 +00004137 WriteConstantInternal(OS, C, TypePrinter, MST.getMachine(), nullptr);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00004138 } else if (isa<InlineAsm>(this) || isa<Argument>(this)) {
Duncan P. N. Exon Smith1f8a99a2015-06-27 00:38:26 +00004139 this->printAsOperand(OS, /* PrintType */ true, MST);
Chris Lattner0c19df42008-08-23 22:23:09 +00004140 } else {
Nick Lewyckyad1b3d12014-05-09 00:49:03 +00004141 llvm_unreachable("Unknown value to print out!");
Chris Lattner0c19df42008-08-23 22:23:09 +00004142 }
4143}
4144
Duncan P. N. Exon Smith326921542015-06-26 22:04:20 +00004145/// Print without a type, skipping the TypePrinting object.
4146///
Benjamin Kramerdf005cb2015-08-08 18:27:36 +00004147/// \return \c true iff printing was successful.
Duncan P. N. Exon Smith326921542015-06-26 22:04:20 +00004148static bool printWithoutType(const Value &V, raw_ostream &O,
4149 SlotTracker *Machine, const Module *M) {
4150 if (V.hasName() || isa<GlobalValue>(V) ||
4151 (!isa<Constant>(V) && !isa<MetadataAsValue>(V))) {
4152 WriteAsOperandInternal(O, &V, nullptr, Machine, M);
4153 return true;
Chandler Carruthd48cdbf2014-01-09 02:29:41 +00004154 }
Duncan P. N. Exon Smith326921542015-06-26 22:04:20 +00004155 return false;
4156}
Chandler Carruthd48cdbf2014-01-09 02:29:41 +00004157
Duncan P. N. Exon Smith326921542015-06-26 22:04:20 +00004158static void printAsOperandImpl(const Value &V, raw_ostream &O, bool PrintType,
4159 ModuleSlotTracker &MST) {
Roman Tereshind96de6f2018-03-22 21:29:07 +00004160 TypePrinting TypePrinter(MST.getModule());
Chandler Carruthd48cdbf2014-01-09 02:29:41 +00004161 if (PrintType) {
Duncan P. N. Exon Smith326921542015-06-26 22:04:20 +00004162 TypePrinter.print(V.getType(), O);
Chandler Carruthd48cdbf2014-01-09 02:29:41 +00004163 O << ' ';
4164 }
4165
Duncan P. N. Exon Smith326921542015-06-26 22:04:20 +00004166 WriteAsOperandInternal(O, &V, &TypePrinter, MST.getMachine(),
4167 MST.getModule());
4168}
4169
4170void Value::printAsOperand(raw_ostream &O, bool PrintType,
4171 const Module *M) const {
4172 if (!M)
4173 M = getModuleFromVal(this);
4174
4175 if (!PrintType)
4176 if (printWithoutType(*this, O, nullptr, M))
4177 return;
4178
4179 SlotTracker Machine(
4180 M, /* ShouldInitializeAllMetadata */ isa<MetadataAsValue>(this));
4181 ModuleSlotTracker MST(Machine, M);
4182 printAsOperandImpl(*this, O, PrintType, MST);
4183}
4184
4185void Value::printAsOperand(raw_ostream &O, bool PrintType,
4186 ModuleSlotTracker &MST) const {
4187 if (!PrintType)
4188 if (printWithoutType(*this, O, MST.getMachine(), MST.getModule()))
4189 return;
4190
4191 printAsOperandImpl(*this, O, PrintType, MST);
Chandler Carruthd48cdbf2014-01-09 02:29:41 +00004192}
4193
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +00004194static void printMetadataImpl(raw_ostream &ROS, const Metadata &MD,
Duncan P. N. Exon Smith6529ed42015-06-26 22:28:47 +00004195 ModuleSlotTracker &MST, const Module *M,
4196 bool OnlyAsOperand) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00004197 formatted_raw_ostream OS(ROS);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00004198
Roman Tereshind96de6f2018-03-22 21:29:07 +00004199 TypePrinting TypePrinter(M);
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +00004200
Duncan P. N. Exon Smith6529ed42015-06-26 22:28:47 +00004201 WriteAsOperandInternal(OS, &MD, &TypePrinter, MST.getMachine(), M,
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00004202 /* FromValue */ true);
Duncan P. N. Exon Smith6529ed42015-06-26 22:28:47 +00004203
4204 auto *N = dyn_cast<MDNode>(&MD);
Reid Klecknerb95b4272017-08-30 20:40:36 +00004205 if (OnlyAsOperand || !N || isa<DIExpression>(MD))
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +00004206 return;
4207
4208 OS << " = ";
Duncan P. N. Exon Smith6529ed42015-06-26 22:28:47 +00004209 WriteMDNodeBodyInternal(OS, N, &TypePrinter, MST.getMachine(), M);
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +00004210}
4211
4212void Metadata::printAsOperand(raw_ostream &OS, const Module *M) const {
Duncan P. N. Exon Smith6529ed42015-06-26 22:28:47 +00004213 ModuleSlotTracker MST(M, isa<MDNode>(this));
4214 printMetadataImpl(OS, *this, MST, M, /* OnlyAsOperand */ true);
4215}
4216
4217void Metadata::printAsOperand(raw_ostream &OS, ModuleSlotTracker &MST,
4218 const Module *M) const {
4219 printMetadataImpl(OS, *this, MST, M, /* OnlyAsOperand */ true);
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +00004220}
4221
Justin Bognerd7d1a722015-09-27 22:38:50 +00004222void Metadata::print(raw_ostream &OS, const Module *M,
4223 bool /*IsForDebug*/) const {
Duncan P. N. Exon Smith6529ed42015-06-26 22:28:47 +00004224 ModuleSlotTracker MST(M, isa<MDNode>(this));
4225 printMetadataImpl(OS, *this, MST, M, /* OnlyAsOperand */ false);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00004226}
4227
Duncan P. N. Exon Smith1f8a99a2015-06-27 00:38:26 +00004228void Metadata::print(raw_ostream &OS, ModuleSlotTracker &MST,
Justin Bognerd7d1a722015-09-27 22:38:50 +00004229 const Module *M, bool /*IsForDebug*/) const {
Duncan P. N. Exon Smith1f8a99a2015-06-27 00:38:26 +00004230 printMetadataImpl(OS, *this, MST, M, /* OnlyAsOperand */ false);
4231}
4232
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00004233void ModuleSummaryIndex::print(raw_ostream &ROS, bool IsForDebug) const {
4234 SlotTracker SlotTable(this);
4235 formatted_raw_ostream OS(ROS);
4236 AssemblyWriter W(OS, SlotTable, this, IsForDebug);
4237 W.printModuleSummaryIndex();
4238}
4239
Aaron Ballman615eb472017-10-15 14:32:27 +00004240#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Chris Lattnerdab942552008-08-25 17:03:15 +00004241// Value::dump - allow easy printing of Values from the debugger.
Duncan P. N. Exon Smith89c1eaa2015-02-25 22:08:21 +00004242LLVM_DUMP_METHOD
Justin Bognerd7d1a722015-09-27 22:38:50 +00004243void Value::dump() const { print(dbgs(), /*IsForDebug=*/true); dbgs() << '\n'; }
Reid Spencer52641832004-05-25 18:14:38 +00004244
Chris Lattnerdab942552008-08-25 17:03:15 +00004245// Type::dump - allow easy printing of Types from the debugger.
Duncan P. N. Exon Smith89c1eaa2015-02-25 22:08:21 +00004246LLVM_DUMP_METHOD
Justin Bognerd7d1a722015-09-27 22:38:50 +00004247void Type::dump() const { print(dbgs(), /*IsForDebug=*/true); dbgs() << '\n'; }
Chris Lattner24025262009-02-28 21:05:51 +00004248
Chris Lattnerdab942552008-08-25 17:03:15 +00004249// Module::dump() - Allow printing of Modules from the debugger.
Duncan P. N. Exon Smith89c1eaa2015-02-25 22:08:21 +00004250LLVM_DUMP_METHOD
Justin Bognerd7d1a722015-09-27 22:38:50 +00004251void Module::dump() const {
4252 print(dbgs(), nullptr,
4253 /*ShouldPreserveUseListOrder=*/false, /*IsForDebug=*/true);
4254}
Bill Wendling3681d112011-12-09 23:18:34 +00004255
Adrian Prantl5f8f34e42018-05-01 15:54:18 +00004256// Allow printing of Comdats from the debugger.
Duncan P. N. Exon Smith89c1eaa2015-02-25 22:08:21 +00004257LLVM_DUMP_METHOD
Justin Bognerd7d1a722015-09-27 22:38:50 +00004258void Comdat::dump() const { print(dbgs(), /*IsForDebug=*/true); }
David Majnemerdad0a642014-06-27 18:19:56 +00004259
Bill Wendling3681d112011-12-09 23:18:34 +00004260// NamedMDNode::dump() - Allow printing of NamedMDNodes from the debugger.
Duncan P. N. Exon Smith89c1eaa2015-02-25 22:08:21 +00004261LLVM_DUMP_METHOD
Justin Bognerd7d1a722015-09-27 22:38:50 +00004262void NamedMDNode::dump() const { print(dbgs(), /*IsForDebug=*/true); }
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00004263
Duncan P. N. Exon Smith89c1eaa2015-02-25 22:08:21 +00004264LLVM_DUMP_METHOD
Duncan P. N. Exon Smitha66dc8f2015-03-15 06:53:32 +00004265void Metadata::dump() const { dump(nullptr); }
4266
4267LLVM_DUMP_METHOD
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +00004268void Metadata::dump(const Module *M) const {
Justin Bognerd7d1a722015-09-27 22:38:50 +00004269 print(dbgs(), M, /*IsForDebug=*/true);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00004270 dbgs() << '\n';
4271}
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00004272
4273// Allow printing of ModuleSummaryIndex from the debugger.
4274LLVM_DUMP_METHOD
4275void ModuleSummaryIndex::dump() const { print(dbgs(), /*IsForDebug=*/true); }
Matthias Braun8c209aa2017-01-28 02:02:38 +00004276#endif