blob: a5dc623e1a30fe2a0bf442bd4bb6ed817f466715 [file] [log] [blame]
Eugene Zelenkof1934002017-06-19 22:05:08 +00001//===- AsmWriter.cpp - Printing LLVM as an assembly file ------------------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
Vlad Tsyrklevich1cd32402018-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 Lattner00950542001-06-06 20:29:01 +000013//
Chris Lattner02b93992002-04-12 18:21:53 +000014// Note that these routines must be extremely tolerant of various errors in the
Chris Lattner8f77dae2003-05-08 02:44:12 +000015// LLVM code, because it can be used for debugging transformations.
Chris Lattner02b93992002-04-12 18:21:53 +000016//
Chris Lattner00950542001-06-06 20:29:01 +000017//===----------------------------------------------------------------------===//
18
Eugene Zelenkof1934002017-06-19 22:05:08 +000019#include "llvm/ADT/APFloat.h"
20#include "llvm/ADT/APInt.h"
21#include "llvm/ADT/ArrayRef.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000022#include "llvm/ADT/DenseMap.h"
Eugene Zelenkof1934002017-06-19 22:05:08 +000023#include "llvm/ADT/None.h"
24#include "llvm/ADT/Optional.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000025#include "llvm/ADT/STLExtras.h"
Benjamin Kramer93920f02015-03-17 19:53:41 +000026#include "llvm/ADT/SetVector.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000027#include "llvm/ADT/SmallString.h"
Eugene Zelenkof1934002017-06-19 22:05:08 +000028#include "llvm/ADT/SmallVector.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000029#include "llvm/ADT/StringExtras.h"
Eugene Zelenkof1934002017-06-19 22:05:08 +000030#include "llvm/ADT/StringRef.h"
31#include "llvm/ADT/iterator_range.h"
Zachary Turner19ca2b02017-06-07 03:48:56 +000032#include "llvm/BinaryFormat/Dwarf.h"
Nico Weber0f38c602018-04-30 14:59:11 +000033#include "llvm/Config/llvm-config.h"
Eugene Zelenkof1934002017-06-19 22:05:08 +000034#include "llvm/IR/Argument.h"
Chandler Carruthbc65a8d2014-01-07 12:34:26 +000035#include "llvm/IR/AssemblyAnnotationWriter.h"
Reid Kleckner7dde8e82017-04-10 23:31:05 +000036#include "llvm/IR/Attributes.h"
Eugene Zelenkof1934002017-06-19 22:05:08 +000037#include "llvm/IR/BasicBlock.h"
Chandler Carruth03e36d72014-03-04 11:45:46 +000038#include "llvm/IR/CFG.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000039#include "llvm/IR/CallingConv.h"
Eugene Zelenkof1934002017-06-19 22:05:08 +000040#include "llvm/IR/Comdat.h"
41#include "llvm/IR/Constant.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000042#include "llvm/IR/Constants.h"
Eugene Zelenkof1934002017-06-19 22:05:08 +000043#include "llvm/IR/DebugInfoMetadata.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000044#include "llvm/IR/DerivedTypes.h"
Eugene Zelenkof1934002017-06-19 22:05:08 +000045#include "llvm/IR/Function.h"
46#include "llvm/IR/GlobalAlias.h"
47#include "llvm/IR/GlobalIFunc.h"
48#include "llvm/IR/GlobalIndirectSymbol.h"
49#include "llvm/IR/GlobalObject.h"
50#include "llvm/IR/GlobalValue.h"
51#include "llvm/IR/GlobalVariable.h"
Chandler Carruth8a5351f2014-01-12 11:10:32 +000052#include "llvm/IR/IRPrintingPasses.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000053#include "llvm/IR/InlineAsm.h"
Eugene Zelenkof1934002017-06-19 22:05:08 +000054#include "llvm/IR/InstrTypes.h"
55#include "llvm/IR/Instruction.h"
56#include "llvm/IR/Instructions.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000057#include "llvm/IR/LLVMContext.h"
Eugene Zelenkof1934002017-06-19 22:05:08 +000058#include "llvm/IR/Metadata.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000059#include "llvm/IR/Module.h"
Duncan P. N. Exon Smith9a61a422015-06-26 22:04:20 +000060#include "llvm/IR/ModuleSlotTracker.h"
Teresa Johnsona9a21472018-05-26 02:34:13 +000061#include "llvm/IR/ModuleSummaryIndex.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000062#include "llvm/IR/Operator.h"
Igor Laevskya5f2faf2015-05-05 13:20:42 +000063#include "llvm/IR/Statepoint.h"
Eugene Zelenkof1934002017-06-19 22:05:08 +000064#include "llvm/IR/Type.h"
Chandler Carruth4068e1a2013-01-07 15:43:51 +000065#include "llvm/IR/TypeFinder.h"
Eugene Zelenkof1934002017-06-19 22:05:08 +000066#include "llvm/IR/Use.h"
Benjamin Kramer93920f02015-03-17 19:53:41 +000067#include "llvm/IR/UseListOrder.h"
Eugene Zelenkof1934002017-06-19 22:05:08 +000068#include "llvm/IR/User.h"
69#include "llvm/IR/Value.h"
70#include "llvm/Support/AtomicOrdering.h"
71#include "llvm/Support/Casting.h"
72#include "llvm/Support/Compiler.h"
David Greened865e022010-01-05 01:29:26 +000073#include "llvm/Support/Debug.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000074#include "llvm/Support/ErrorHandling.h"
Justin Bognerd8def4a2015-12-04 02:14:34 +000075#include "llvm/Support/Format.h"
Dan Gohman683e9222009-08-12 17:23:50 +000076#include "llvm/Support/FormattedStream.h"
Benjamin Kramer1bfcd1f2015-03-23 19:32:43 +000077#include "llvm/Support/raw_ostream.h"
Chris Lattner007377f2001-09-07 16:36:04 +000078#include <algorithm>
Eugene Zelenkof1934002017-06-19 22:05:08 +000079#include <cassert>
Reid Spencer4ad513c2007-05-22 19:27:35 +000080#include <cctype>
Eugene Zelenkof1934002017-06-19 22:05:08 +000081#include <cstddef>
82#include <cstdint>
83#include <iterator>
84#include <memory>
85#include <string>
86#include <tuple>
87#include <utility>
88#include <vector>
89
Chris Lattner31f84992003-11-21 20:23:48 +000090using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000091
Reid Spenceredd5d9e2005-05-15 16:13:11 +000092// Make virtual table appear in this compilation unit.
Eugene Zelenkof1934002017-06-19 22:05:08 +000093AssemblyAnnotationWriter::~AssemblyAnnotationWriter() = default;
Reid Spenceredd5d9e2005-05-15 16:13:11 +000094
Chris Lattner6ab910b2008-08-19 04:36:02 +000095//===----------------------------------------------------------------------===//
96// Helper Functions
97//===----------------------------------------------------------------------===//
98
Duncan P. N. Exon Smith78388182014-08-19 21:30:15 +000099namespace {
Eugene Zelenkof1934002017-06-19 22:05:08 +0000100
Duncan P. N. Exon Smith78388182014-08-19 21:30:15 +0000101struct OrderMap {
102 DenseMap<const Value *, std::pair<unsigned, bool>> IDs;
103
104 unsigned size() const { return IDs.size(); }
105 std::pair<unsigned, bool> &operator[](const Value *V) { return IDs[V]; }
Eugene Zelenkof1934002017-06-19 22:05:08 +0000106
Duncan P. N. Exon Smith78388182014-08-19 21:30:15 +0000107 std::pair<unsigned, bool> lookup(const Value *V) const {
108 return IDs.lookup(V);
109 }
Eugene Zelenkof1934002017-06-19 22:05:08 +0000110
Duncan P. N. Exon Smith78388182014-08-19 21:30:15 +0000111 void index(const Value *V) {
112 // Explicitly sequence get-size and insert-value operations to avoid UB.
113 unsigned ID = IDs.size() + 1;
114 IDs[V].first = ID;
115 }
116};
Eugene Zelenkof1934002017-06-19 22:05:08 +0000117
118} // end anonymous namespace
Duncan P. N. Exon Smith78388182014-08-19 21:30:15 +0000119
120static void orderValue(const Value *V, OrderMap &OM) {
121 if (OM.lookup(V).first)
122 return;
123
124 if (const Constant *C = dyn_cast<Constant>(V))
125 if (C->getNumOperands() && !isa<GlobalValue>(C))
126 for (const Value *Op : C->operands())
127 if (!isa<BasicBlock>(Op) && !isa<GlobalValue>(Op))
128 orderValue(Op, OM);
129
130 // Note: we cannot cache this lookup above, since inserting into the map
131 // changes the map's size, and thus affects the other IDs.
132 OM.index(V);
133}
134
135static OrderMap orderModule(const Module *M) {
136 // This needs to match the order used by ValueEnumerator::ValueEnumerator()
137 // and ValueEnumerator::incorporateFunction().
138 OrderMap OM;
139
140 for (const GlobalVariable &G : M->globals()) {
141 if (G.hasInitializer())
142 if (!isa<GlobalValue>(G.getInitializer()))
143 orderValue(G.getInitializer(), OM);
144 orderValue(&G, OM);
145 }
146 for (const GlobalAlias &A : M->aliases()) {
147 if (!isa<GlobalValue>(A.getAliasee()))
148 orderValue(A.getAliasee(), OM);
149 orderValue(&A, OM);
150 }
Dmitry Polukhinba492232016-04-07 12:32:19 +0000151 for (const GlobalIFunc &I : M->ifuncs()) {
152 if (!isa<GlobalValue>(I.getResolver()))
153 orderValue(I.getResolver(), OM);
154 orderValue(&I, OM);
155 }
Duncan P. N. Exon Smith78388182014-08-19 21:30:15 +0000156 for (const Function &F : *M) {
Vedant Kumaref112d42015-12-19 08:52:49 +0000157 for (const Use &U : F.operands())
158 if (!isa<GlobalValue>(U.get()))
159 orderValue(U.get(), OM);
David Majnemercc714e22015-06-17 20:52:32 +0000160
Duncan P. N. Exon Smith78388182014-08-19 21:30:15 +0000161 orderValue(&F, OM);
162
163 if (F.isDeclaration())
164 continue;
165
166 for (const Argument &A : F.args())
167 orderValue(&A, OM);
168 for (const BasicBlock &BB : F) {
169 orderValue(&BB, OM);
170 for (const Instruction &I : BB) {
171 for (const Value *Op : I.operands())
172 if ((isa<Constant>(*Op) && !isa<GlobalValue>(*Op)) ||
173 isa<InlineAsm>(*Op))
174 orderValue(Op, OM);
175 orderValue(&I, OM);
176 }
177 }
178 }
179 return OM;
180}
181
182static void predictValueUseListOrderImpl(const Value *V, const Function *F,
183 unsigned ID, const OrderMap &OM,
184 UseListOrderStack &Stack) {
185 // Predict use-list order for this one.
Eugene Zelenkof1934002017-06-19 22:05:08 +0000186 using Entry = std::pair<const Use *, unsigned>;
Duncan P. N. Exon Smith78388182014-08-19 21:30:15 +0000187 SmallVector<Entry, 64> List;
188 for (const Use &U : V->uses())
189 // Check if this user will be serialized.
190 if (OM.lookup(U.getUser()).first)
191 List.push_back(std::make_pair(&U, List.size()));
192
193 if (List.size() < 2)
194 // We may have lost some users.
195 return;
196
197 bool GetsReversed =
198 !isa<GlobalVariable>(V) && !isa<Function>(V) && !isa<BasicBlock>(V);
199 if (auto *BA = dyn_cast<BlockAddress>(V))
200 ID = OM.lookup(BA->getBasicBlock()).first;
Fangrui Song3b35e172018-09-27 02:13:45 +0000201 llvm::sort(List, [&](const Entry &L, const Entry &R) {
Duncan P. N. Exon Smith78388182014-08-19 21:30:15 +0000202 const Use *LU = L.first;
203 const Use *RU = R.first;
204 if (LU == RU)
205 return false;
206
207 auto LID = OM.lookup(LU->getUser()).first;
208 auto RID = OM.lookup(RU->getUser()).first;
209
210 // If ID is 4, then expect: 7 6 5 1 2 3.
211 if (LID < RID) {
212 if (GetsReversed)
213 if (RID <= ID)
214 return true;
215 return false;
216 }
217 if (RID < LID) {
218 if (GetsReversed)
219 if (LID <= ID)
220 return false;
221 return true;
222 }
223
224 // LID and RID are equal, so we have different operands of the same user.
225 // Assume operands are added in order for all instructions.
226 if (GetsReversed)
227 if (LID <= ID)
228 return LU->getOperandNo() < RU->getOperandNo();
229 return LU->getOperandNo() > RU->getOperandNo();
230 });
231
232 if (std::is_sorted(
233 List.begin(), List.end(),
234 [](const Entry &L, const Entry &R) { return L.second < R.second; }))
235 // Order is already correct.
236 return;
237
238 // Store the shuffle.
239 Stack.emplace_back(V, F, List.size());
240 assert(List.size() == Stack.back().Shuffle.size() && "Wrong size");
241 for (size_t I = 0, E = List.size(); I != E; ++I)
242 Stack.back().Shuffle[I] = List[I].second;
243}
244
245static void predictValueUseListOrder(const Value *V, const Function *F,
246 OrderMap &OM, UseListOrderStack &Stack) {
247 auto &IDPair = OM[V];
248 assert(IDPair.first && "Unmapped value");
249 if (IDPair.second)
250 // Already predicted.
251 return;
252
253 // Do the actual prediction.
254 IDPair.second = true;
255 if (!V->use_empty() && std::next(V->use_begin()) != V->use_end())
256 predictValueUseListOrderImpl(V, F, IDPair.first, OM, Stack);
257
258 // Recursive descent into constants.
259 if (const Constant *C = dyn_cast<Constant>(V))
260 if (C->getNumOperands()) // Visit GlobalValues.
261 for (const Value *Op : C->operands())
262 if (isa<Constant>(Op)) // Visit GlobalValues.
263 predictValueUseListOrder(Op, F, OM, Stack);
264}
265
266static UseListOrderStack predictUseListOrder(const Module *M) {
267 OrderMap OM = orderModule(M);
268
269 // Use-list orders need to be serialized after all the users have been added
270 // to a value, or else the shuffles will be incomplete. Store them per
271 // function in a stack.
272 //
273 // Aside from function order, the order of values doesn't matter much here.
274 UseListOrderStack Stack;
275
276 // We want to visit the functions backward now so we can list function-local
277 // constants in the last Function they're used in. Module-level constants
278 // have already been visited above.
Pete Cooper9f2f1652015-07-24 21:13:43 +0000279 for (const Function &F : make_range(M->rbegin(), M->rend())) {
Duncan P. N. Exon Smith78388182014-08-19 21:30:15 +0000280 if (F.isDeclaration())
281 continue;
282 for (const BasicBlock &BB : F)
283 predictValueUseListOrder(&BB, &F, OM, Stack);
284 for (const Argument &A : F.args())
285 predictValueUseListOrder(&A, &F, OM, Stack);
286 for (const BasicBlock &BB : F)
287 for (const Instruction &I : BB)
288 for (const Value *Op : I.operands())
289 if (isa<Constant>(*Op) || isa<InlineAsm>(*Op)) // Visit GlobalValues.
290 predictValueUseListOrder(Op, &F, OM, Stack);
291 for (const BasicBlock &BB : F)
292 for (const Instruction &I : BB)
293 predictValueUseListOrder(&I, &F, OM, Stack);
294 }
295
296 // Visit globals last.
297 for (const GlobalVariable &G : M->globals())
298 predictValueUseListOrder(&G, nullptr, OM, Stack);
299 for (const Function &F : *M)
300 predictValueUseListOrder(&F, nullptr, OM, Stack);
301 for (const GlobalAlias &A : M->aliases())
302 predictValueUseListOrder(&A, nullptr, OM, Stack);
Dmitry Polukhinba492232016-04-07 12:32:19 +0000303 for (const GlobalIFunc &I : M->ifuncs())
304 predictValueUseListOrder(&I, nullptr, OM, Stack);
Duncan P. N. Exon Smith78388182014-08-19 21:30:15 +0000305 for (const GlobalVariable &G : M->globals())
306 if (G.hasInitializer())
307 predictValueUseListOrder(G.getInitializer(), nullptr, OM, Stack);
308 for (const GlobalAlias &A : M->aliases())
309 predictValueUseListOrder(A.getAliasee(), nullptr, OM, Stack);
Dmitry Polukhinba492232016-04-07 12:32:19 +0000310 for (const GlobalIFunc &I : M->ifuncs())
311 predictValueUseListOrder(I.getResolver(), nullptr, OM, Stack);
Duncan P. N. Exon Smith78388182014-08-19 21:30:15 +0000312 for (const Function &F : *M)
Vedant Kumaref112d42015-12-19 08:52:49 +0000313 for (const Use &U : F.operands())
314 predictValueUseListOrder(U.get(), nullptr, OM, Stack);
Duncan P. N. Exon Smith78388182014-08-19 21:30:15 +0000315
316 return Stack;
317}
318
Chris Lattner6ab910b2008-08-19 04:36:02 +0000319static const Module *getModuleFromVal(const Value *V) {
320 if (const Argument *MA = dyn_cast<Argument>(V))
Craig Topperec0f0bc2014-04-09 06:08:46 +0000321 return MA->getParent() ? MA->getParent()->getParent() : nullptr;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000322
Chris Lattner6ab910b2008-08-19 04:36:02 +0000323 if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
Craig Topperec0f0bc2014-04-09 06:08:46 +0000324 return BB->getParent() ? BB->getParent()->getParent() : nullptr;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000325
Chris Lattner6ab910b2008-08-19 04:36:02 +0000326 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Craig Topperec0f0bc2014-04-09 06:08:46 +0000327 const Function *M = I->getParent() ? I->getParent()->getParent() : nullptr;
328 return M ? M->getParent() : nullptr;
Chris Lattner6ab910b2008-08-19 04:36:02 +0000329 }
Andrew Trick18801ec2011-09-30 19:48:58 +0000330
Chris Lattner6ab910b2008-08-19 04:36:02 +0000331 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
332 return GV->getParent();
Duncan P. N. Exon Smithf9f505e2015-03-14 20:19:36 +0000333
334 if (const auto *MAV = dyn_cast<MetadataAsValue>(V)) {
335 for (const User *U : MAV->users())
336 if (isa<Instruction>(U))
337 if (const Module *M = getModuleFromVal(U))
338 return M;
339 return nullptr;
340 }
341
Craig Topperec0f0bc2014-04-09 06:08:46 +0000342 return nullptr;
Chris Lattner6ab910b2008-08-19 04:36:02 +0000343}
344
Bill Wendling7ab6c762013-02-20 07:21:42 +0000345static void PrintCallingConv(unsigned cc, raw_ostream &Out) {
Micah Villmowd3766df2012-09-13 15:11:12 +0000346 switch (cc) {
Bill Wendling7ab6c762013-02-20 07:21:42 +0000347 default: Out << "cc" << cc; break;
348 case CallingConv::Fast: Out << "fastcc"; break;
349 case CallingConv::Cold: Out << "coldcc"; break;
Andrew Trick5a349802013-11-11 22:40:22 +0000350 case CallingConv::WebKit_JS: Out << "webkit_jscc"; break;
351 case CallingConv::AnyReg: Out << "anyregcc"; break;
Juergen Ributzkaceaf8292014-01-17 19:47:03 +0000352 case CallingConv::PreserveMost: Out << "preserve_mostcc"; break;
353 case CallingConv::PreserveAll: Out << "preserve_allcc"; break;
Manman Rencd2103d2015-12-04 17:40:13 +0000354 case CallingConv::CXX_FAST_TLS: Out << "cxx_fast_tlscc"; break;
Reid Kleckner03c735b2014-12-01 21:04:44 +0000355 case CallingConv::GHC: Out << "ghccc"; break;
Bill Wendling7ab6c762013-02-20 07:21:42 +0000356 case CallingConv::X86_StdCall: Out << "x86_stdcallcc"; break;
357 case CallingConv::X86_FastCall: Out << "x86_fastcallcc"; break;
358 case CallingConv::X86_ThisCall: Out << "x86_thiscallcc"; break;
Oren Ben Simhon4b6c3392016-10-13 07:53:43 +0000359 case CallingConv::X86_RegCall: Out << "x86_regcallcc"; break;
Reid Klecknerd5de3272014-10-28 01:29:26 +0000360 case CallingConv::X86_VectorCall:Out << "x86_vectorcallcc"; break;
Bill Wendling7ab6c762013-02-20 07:21:42 +0000361 case CallingConv::Intel_OCL_BI: Out << "intel_ocl_bicc"; break;
362 case CallingConv::ARM_APCS: Out << "arm_apcscc"; break;
363 case CallingConv::ARM_AAPCS: Out << "arm_aapcscc"; break;
364 case CallingConv::ARM_AAPCS_VFP: Out << "arm_aapcs_vfpcc"; break;
Sander de Smalen15889d42018-09-12 08:54:06 +0000365 case CallingConv::AArch64_VectorCall: Out << "aarch64_vector_pcs"; break;
Bill Wendling7ab6c762013-02-20 07:21:42 +0000366 case CallingConv::MSP430_INTR: Out << "msp430_intrcc"; break;
Dylan McKayca039022016-03-03 10:08:02 +0000367 case CallingConv::AVR_INTR: Out << "avr_intrcc "; break;
368 case CallingConv::AVR_SIGNAL: Out << "avr_signalcc "; break;
Bill Wendling7ab6c762013-02-20 07:21:42 +0000369 case CallingConv::PTX_Kernel: Out << "ptx_kernel"; break;
370 case CallingConv::PTX_Device: Out << "ptx_device"; break;
Charles Davisac226bb2013-07-12 06:02:35 +0000371 case CallingConv::X86_64_SysV: Out << "x86_64_sysvcc"; break;
Martin Storsjo6c132cb2017-07-17 20:05:19 +0000372 case CallingConv::Win64: Out << "win64cc"; break;
Michael Kupersteinef94f7e2013-12-15 10:01:20 +0000373 case CallingConv::SPIR_FUNC: Out << "spir_func"; break;
374 case CallingConv::SPIR_KERNEL: Out << "spir_kernel"; break;
Manman Ren1f7638e2016-04-05 22:41:47 +0000375 case CallingConv::Swift: Out << "swiftcc"; break;
Amjad Aboud98891742015-12-21 14:07:14 +0000376 case CallingConv::X86_INTR: Out << "x86_intrcc"; break;
Maksim Panchenko3b3752c2015-09-29 22:09:16 +0000377 case CallingConv::HHVM: Out << "hhvmcc"; break;
378 case CallingConv::HHVM_C: Out << "hhvm_ccc"; break;
Nicolai Haehnleea7a0c042016-04-06 19:40:20 +0000379 case CallingConv::AMDGPU_VS: Out << "amdgpu_vs"; break;
Tim Renouf8ba98f92017-09-29 09:51:22 +0000380 case CallingConv::AMDGPU_LS: Out << "amdgpu_ls"; break;
Marek Olsaka2057042017-05-02 15:41:10 +0000381 case CallingConv::AMDGPU_HS: Out << "amdgpu_hs"; break;
Tim Renouf8ba98f92017-09-29 09:51:22 +0000382 case CallingConv::AMDGPU_ES: Out << "amdgpu_es"; break;
Nicolai Haehnleea7a0c042016-04-06 19:40:20 +0000383 case CallingConv::AMDGPU_GS: Out << "amdgpu_gs"; break;
384 case CallingConv::AMDGPU_PS: Out << "amdgpu_ps"; break;
385 case CallingConv::AMDGPU_CS: Out << "amdgpu_cs"; break;
Nikolay Haustovac1dd292016-05-06 09:07:29 +0000386 case CallingConv::AMDGPU_KERNEL: Out << "amdgpu_kernel"; break;
Micah Villmowd3766df2012-09-13 15:11:12 +0000387 }
388}
Michael Ilseman407a6162012-11-15 22:34:00 +0000389
Chris Lattner6ab910b2008-08-19 04:36:02 +0000390enum PrefixType {
391 GlobalPrefix,
David Majnemerc8a11692014-06-27 18:19:56 +0000392 ComdatPrefix,
Chris Lattner6ab910b2008-08-19 04:36:02 +0000393 LabelPrefix,
Daniel Dunbarcad35802008-10-14 23:28:09 +0000394 LocalPrefix,
395 NoPrefix
Chris Lattner6ab910b2008-08-19 04:36:02 +0000396};
397
Alex Lorenz476706e2015-07-21 16:50:35 +0000398void llvm::printLLVMNameWithoutPrefix(raw_ostream &OS, StringRef Name) {
Jay Foad61717b32011-04-24 14:30:00 +0000399 assert(!Name.empty() && "Cannot get empty name!");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000400
Chris Lattner6ab910b2008-08-19 04:36:02 +0000401 // Scan the name to see if it needs quotes first.
Guy Benyei87d0b9e2013-02-12 21:21:59 +0000402 bool NeedsQuotes = isdigit(static_cast<unsigned char>(Name[0]));
Chris Lattner6ab910b2008-08-19 04:36:02 +0000403 if (!NeedsQuotes) {
Daniel Dunbar03d76512009-07-25 23:55:21 +0000404 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
Aaron Ballman09dab822012-07-16 16:18:18 +0000405 // By making this unsigned, the value passed in to isalnum will always be
406 // in the range 0-255. This is important when building with MSVC because
407 // its implementation will assert. This situation can arise when dealing
408 // with UTF-8 multibyte characters.
409 unsigned char C = Name[i];
Guy Benyei87d0b9e2013-02-12 21:21:59 +0000410 if (!isalnum(static_cast<unsigned char>(C)) && C != '-' && C != '.' &&
411 C != '_') {
Chris Lattner6ab910b2008-08-19 04:36:02 +0000412 NeedsQuotes = true;
413 break;
414 }
415 }
416 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000417
Chris Lattner6ab910b2008-08-19 04:36:02 +0000418 // If we didn't need any quotes, just write out the name in one blast.
419 if (!NeedsQuotes) {
Daniel Dunbar03d76512009-07-25 23:55:21 +0000420 OS << Name;
Chris Lattner6ab910b2008-08-19 04:36:02 +0000421 return;
422 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000423
Chris Lattner6ab910b2008-08-19 04:36:02 +0000424 // Okay, we need quotes. Output the quotes and escape any scary characters as
425 // needed.
426 OS << '"';
Jonas Devlieghere7eeba252018-05-31 17:01:42 +0000427 printEscapedString(Name, OS);
Chris Lattner6ab910b2008-08-19 04:36:02 +0000428 OS << '"';
429}
430
Alex Lorenz476706e2015-07-21 16:50:35 +0000431/// Turn the specified name into an 'LLVM name', which is either prefixed with %
432/// (if the string only contains simple characters) or is surrounded with ""'s
433/// (if it has special chars in it). Print it out.
434static void PrintLLVMName(raw_ostream &OS, StringRef Name, PrefixType Prefix) {
435 switch (Prefix) {
436 case NoPrefix:
437 break;
438 case GlobalPrefix:
439 OS << '@';
440 break;
441 case ComdatPrefix:
442 OS << '$';
443 break;
444 case LabelPrefix:
445 break;
446 case LocalPrefix:
447 OS << '%';
448 break;
449 }
450 printLLVMNameWithoutPrefix(OS, Name);
451}
452
453/// Turn the specified name into an 'LLVM name', which is either prefixed with %
454/// (if the string only contains simple characters) or is surrounded with ""'s
455/// (if it has special chars in it). Print it out.
Dan Gohman1220e102009-08-12 20:56:03 +0000456static void PrintLLVMName(raw_ostream &OS, const Value *V) {
Daniel Dunbara279bc32009-09-20 02:20:51 +0000457 PrintLLVMName(OS, V->getName(),
Chris Lattner6ab910b2008-08-19 04:36:02 +0000458 isa<GlobalValue>(V) ? GlobalPrefix : LocalPrefix);
459}
460
Benjamin Kramer93920f02015-03-17 19:53:41 +0000461namespace {
Benjamin Kramer93920f02015-03-17 19:53:41 +0000462
Eugene Zelenkof1934002017-06-19 22:05:08 +0000463class TypePrinting {
464public:
Roman Tereshin238a3382018-03-22 21:29:07 +0000465 TypePrinting(const Module *M = nullptr) : DeferredM(M) {}
Benjamin Kramer93920f02015-03-17 19:53:41 +0000466
Eugene Zelenkof1934002017-06-19 22:05:08 +0000467 TypePrinting(const TypePrinting &) = delete;
468 TypePrinting &operator=(const TypePrinting &) = delete;
Benjamin Kramer93920f02015-03-17 19:53:41 +0000469
Roman Tereshin238a3382018-03-22 21:29:07 +0000470 /// The named types that are used by the current module.
471 TypeFinder &getNamedTypes();
472
473 /// The numbered types, number to type mapping.
474 std::vector<StructType *> &getNumberedTypes();
475
476 bool empty();
Benjamin Kramer93920f02015-03-17 19:53:41 +0000477
478 void print(Type *Ty, raw_ostream &OS);
479
480 void printStructBody(StructType *Ty, raw_ostream &OS);
Roman Tereshin238a3382018-03-22 21:29:07 +0000481
482private:
483 void incorporateTypes();
484
485 /// A module to process lazily when needed. Set to nullptr as soon as used.
486 const Module *DeferredM;
487
488 TypeFinder NamedTypes;
489
490 // The numbered types, along with their value.
491 DenseMap<StructType *, unsigned> Type2Number;
492
493 std::vector<StructType *> NumberedTypes;
Benjamin Kramer93920f02015-03-17 19:53:41 +0000494};
Eugene Zelenkof1934002017-06-19 22:05:08 +0000495
496} // end anonymous namespace
Chris Lattner1afcace2011-07-09 17:41:24 +0000497
Roman Tereshin238a3382018-03-22 21:29:07 +0000498TypeFinder &TypePrinting::getNamedTypes() {
499 incorporateTypes();
500 return NamedTypes;
501}
502
503std::vector<StructType *> &TypePrinting::getNumberedTypes() {
504 incorporateTypes();
505
506 // We know all the numbers that each type is used and we know that it is a
507 // dense assignment. Convert the map to an index table, if it's not done
508 // already (judging from the sizes):
509 if (NumberedTypes.size() == Type2Number.size())
510 return NumberedTypes;
511
512 NumberedTypes.resize(Type2Number.size());
513 for (const auto &P : Type2Number) {
514 assert(P.second < NumberedTypes.size() && "Didn't get a dense numbering?");
515 assert(!NumberedTypes[P.second] && "Didn't get a unique numbering?");
516 NumberedTypes[P.second] = P.first;
517 }
518 return NumberedTypes;
519}
520
521bool TypePrinting::empty() {
522 incorporateTypes();
523 return NamedTypes.empty() && Type2Number.empty();
524}
525
526void TypePrinting::incorporateTypes() {
527 if (!DeferredM)
528 return;
529
530 NamedTypes.run(*DeferredM, false);
531 DeferredM = nullptr;
Andrew Trick18801ec2011-09-30 19:48:58 +0000532
Chris Lattner1afcace2011-07-09 17:41:24 +0000533 // The list of struct types we got back includes all the struct types, split
534 // the unnamed ones out to a numbering and remove the anonymous structs.
535 unsigned NextNumber = 0;
Andrew Trick18801ec2011-09-30 19:48:58 +0000536
Chris Lattner1afcace2011-07-09 17:41:24 +0000537 std::vector<StructType*>::iterator NextToUse = NamedTypes.begin(), I, E;
538 for (I = NamedTypes.begin(), E = NamedTypes.end(); I != E; ++I) {
539 StructType *STy = *I;
Andrew Trick18801ec2011-09-30 19:48:58 +0000540
Chris Lattner1afcace2011-07-09 17:41:24 +0000541 // Ignore anonymous types.
Chris Lattnerc4d0e9f2011-08-12 18:07:07 +0000542 if (STy->isLiteral())
Chris Lattner1afcace2011-07-09 17:41:24 +0000543 continue;
Andrew Trick18801ec2011-09-30 19:48:58 +0000544
Chris Lattner1afcace2011-07-09 17:41:24 +0000545 if (STy->getName().empty())
Roman Tereshin238a3382018-03-22 21:29:07 +0000546 Type2Number[STy] = NextNumber++;
Chris Lattner1afcace2011-07-09 17:41:24 +0000547 else
548 *NextToUse++ = STy;
549 }
Andrew Trick18801ec2011-09-30 19:48:58 +0000550
Chris Lattner1afcace2011-07-09 17:41:24 +0000551 NamedTypes.erase(NextToUse, NamedTypes.end());
552}
553
Roman Tereshin238a3382018-03-22 21:29:07 +0000554/// Write the specified type to the specified raw_ostream, making use of type
555/// names or up references to shorten the type name where possible.
Chris Lattner1afcace2011-07-09 17:41:24 +0000556void TypePrinting::print(Type *Ty, raw_ostream &OS) {
Chris Lattner9cc34462009-02-28 20:25:14 +0000557 switch (Ty->getTypeID()) {
Rafael Espindola954b6b82013-12-07 02:27:52 +0000558 case Type::VoidTyID: OS << "void"; return;
559 case Type::HalfTyID: OS << "half"; return;
560 case Type::FloatTyID: OS << "float"; return;
561 case Type::DoubleTyID: OS << "double"; return;
562 case Type::X86_FP80TyID: OS << "x86_fp80"; return;
563 case Type::FP128TyID: OS << "fp128"; return;
564 case Type::PPC_FP128TyID: OS << "ppc_fp128"; return;
565 case Type::LabelTyID: OS << "label"; return;
566 case Type::MetadataTyID: OS << "metadata"; return;
567 case Type::X86_MMXTyID: OS << "x86_mmx"; return;
David Majnemer2dacece2015-08-14 05:09:07 +0000568 case Type::TokenTyID: OS << "token"; return;
Chris Lattner583ffd82009-02-28 21:18:43 +0000569 case Type::IntegerTyID:
Chris Lattner30794262009-02-28 21:27:31 +0000570 OS << 'i' << cast<IntegerType>(Ty)->getBitWidth();
Chris Lattner1afcace2011-07-09 17:41:24 +0000571 return;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000572
Chris Lattner36942d72009-02-28 20:35:42 +0000573 case Type::FunctionTyID: {
Chris Lattner1afcace2011-07-09 17:41:24 +0000574 FunctionType *FTy = cast<FunctionType>(Ty);
575 print(FTy->getReturnType(), OS);
Chris Lattner30794262009-02-28 21:27:31 +0000576 OS << " (";
Chris Lattner36942d72009-02-28 20:35:42 +0000577 for (FunctionType::param_iterator I = FTy->param_begin(),
578 E = FTy->param_end(); I != E; ++I) {
579 if (I != FTy->param_begin())
Chris Lattner30794262009-02-28 21:27:31 +0000580 OS << ", ";
Chris Lattner1afcace2011-07-09 17:41:24 +0000581 print(*I, OS);
Chris Lattner9cc34462009-02-28 20:25:14 +0000582 }
Chris Lattner36942d72009-02-28 20:35:42 +0000583 if (FTy->isVarArg()) {
Chris Lattner30794262009-02-28 21:27:31 +0000584 if (FTy->getNumParams()) OS << ", ";
585 OS << "...";
Chris Lattner9cc34462009-02-28 20:25:14 +0000586 }
Chris Lattner30794262009-02-28 21:27:31 +0000587 OS << ')';
Chris Lattner1afcace2011-07-09 17:41:24 +0000588 return;
Chris Lattner36942d72009-02-28 20:35:42 +0000589 }
590 case Type::StructTyID: {
Chris Lattner1afcace2011-07-09 17:41:24 +0000591 StructType *STy = cast<StructType>(Ty);
Andrew Trick18801ec2011-09-30 19:48:58 +0000592
Chris Lattnerc4d0e9f2011-08-12 18:07:07 +0000593 if (STy->isLiteral())
Chris Lattner1afcace2011-07-09 17:41:24 +0000594 return printStructBody(STy, OS);
595
596 if (!STy->getName().empty())
597 return PrintLLVMName(OS, STy->getName(), LocalPrefix);
Andrew Trick18801ec2011-09-30 19:48:58 +0000598
Roman Tereshin238a3382018-03-22 21:29:07 +0000599 incorporateTypes();
600 const auto I = Type2Number.find(STy);
601 if (I != Type2Number.end())
Chris Lattner1afcace2011-07-09 17:41:24 +0000602 OS << '%' << I->second;
603 else // Not enumerated, print the hex address.
Benjamin Kramer5a832642011-11-02 17:24:36 +0000604 OS << "%\"type " << STy << '\"';
Chris Lattner1afcace2011-07-09 17:41:24 +0000605 return;
Chris Lattner36942d72009-02-28 20:35:42 +0000606 }
607 case Type::PointerTyID: {
Chris Lattner1afcace2011-07-09 17:41:24 +0000608 PointerType *PTy = cast<PointerType>(Ty);
609 print(PTy->getElementType(), OS);
Chris Lattner36942d72009-02-28 20:35:42 +0000610 if (unsigned AddressSpace = PTy->getAddressSpace())
Chris Lattner30794262009-02-28 21:27:31 +0000611 OS << " addrspace(" << AddressSpace << ')';
612 OS << '*';
Chris Lattner1afcace2011-07-09 17:41:24 +0000613 return;
Chris Lattner36942d72009-02-28 20:35:42 +0000614 }
615 case Type::ArrayTyID: {
Chris Lattner1afcace2011-07-09 17:41:24 +0000616 ArrayType *ATy = cast<ArrayType>(Ty);
Chris Lattner30794262009-02-28 21:27:31 +0000617 OS << '[' << ATy->getNumElements() << " x ";
Chris Lattner1afcace2011-07-09 17:41:24 +0000618 print(ATy->getElementType(), OS);
Chris Lattner30794262009-02-28 21:27:31 +0000619 OS << ']';
Chris Lattner1afcace2011-07-09 17:41:24 +0000620 return;
Chris Lattner36942d72009-02-28 20:35:42 +0000621 }
622 case Type::VectorTyID: {
Chris Lattner1afcace2011-07-09 17:41:24 +0000623 VectorType *PTy = cast<VectorType>(Ty);
Chris Lattner30794262009-02-28 21:27:31 +0000624 OS << "<" << PTy->getNumElements() << " x ";
Chris Lattner1afcace2011-07-09 17:41:24 +0000625 print(PTy->getElementType(), OS);
Chris Lattner30794262009-02-28 21:27:31 +0000626 OS << '>';
Chris Lattner1afcace2011-07-09 17:41:24 +0000627 return;
Chris Lattner36942d72009-02-28 20:35:42 +0000628 }
Chris Lattner9cc34462009-02-28 20:25:14 +0000629 }
Rafael Espindola954b6b82013-12-07 02:27:52 +0000630 llvm_unreachable("Invalid TypeID");
Chris Lattner9cc34462009-02-28 20:25:14 +0000631}
632
Chris Lattner1afcace2011-07-09 17:41:24 +0000633void TypePrinting::printStructBody(StructType *STy, raw_ostream &OS) {
634 if (STy->isOpaque()) {
635 OS << "opaque";
636 return;
Chris Lattner9cc34462009-02-28 20:25:14 +0000637 }
Andrew Trick18801ec2011-09-30 19:48:58 +0000638
Chris Lattner1afcace2011-07-09 17:41:24 +0000639 if (STy->isPacked())
640 OS << '<';
Andrew Trick18801ec2011-09-30 19:48:58 +0000641
Chris Lattner1afcace2011-07-09 17:41:24 +0000642 if (STy->getNumElements() == 0) {
643 OS << "{}";
644 } else {
645 StructType::element_iterator I = STy->element_begin();
646 OS << "{ ";
647 print(*I++, OS);
648 for (StructType::element_iterator E = STy->element_end(); I != E; ++I) {
649 OS << ", ";
650 print(*I, OS);
Chris Lattner413fd232009-03-01 00:03:38 +0000651 }
Andrew Trick18801ec2011-09-30 19:48:58 +0000652
Chris Lattner1afcace2011-07-09 17:41:24 +0000653 OS << " }";
Chris Lattnere9fa33e2009-02-28 23:20:19 +0000654 }
Chris Lattner1afcace2011-07-09 17:41:24 +0000655 if (STy->isPacked())
656 OS << '>';
Chris Lattnere9fa33e2009-02-28 23:20:19 +0000657}
658
Duncan P. N. Exon Smith9a61a422015-06-26 22:04:20 +0000659namespace llvm {
Eugene Zelenkof1934002017-06-19 22:05:08 +0000660
Chris Lattner6ab910b2008-08-19 04:36:02 +0000661//===----------------------------------------------------------------------===//
662// SlotTracker Class: Enumerate slot numbers for unnamed values
663//===----------------------------------------------------------------------===//
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000664/// This class provides computation of slot numbers for LLVM Assembly writing.
Chris Lattner45d4c732008-08-17 04:17:45 +0000665///
Chris Lattner0d9574a2008-08-19 04:26:57 +0000666class SlotTracker {
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000667public:
Devang Patel320671d2009-07-08 21:44:25 +0000668 /// ValueMap - A mapping of Values to slot numbers.
Eugene Zelenkof1934002017-06-19 22:05:08 +0000669 using ValueMap = DenseMap<const Value *, unsigned>;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000670
671private:
Devang Patel320671d2009-07-08 21:44:25 +0000672 /// TheModule - The module for which we are holding slot numbers.
Chris Lattner45d4c732008-08-17 04:17:45 +0000673 const Module* TheModule;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000674
Devang Patel320671d2009-07-08 21:44:25 +0000675 /// TheFunction - The function for which we are holding slot numbers.
Eugene Zelenkof1934002017-06-19 22:05:08 +0000676 const Function* TheFunction = nullptr;
677 bool FunctionProcessed = false;
Duncan P. N. Exon Smithf9f505e2015-03-14 20:19:36 +0000678 bool ShouldInitializeAllMetadata;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000679
Teresa Johnsona9a21472018-05-26 02:34:13 +0000680 /// The summary index for which we are holding slot numbers.
681 const ModuleSummaryIndex *TheIndex = nullptr;
682
Jay Foad7f0ce342011-07-11 07:28:49 +0000683 /// mMap - The slot map for the module level data.
Chris Lattner45d4c732008-08-17 04:17:45 +0000684 ValueMap mMap;
Eugene Zelenkof1934002017-06-19 22:05:08 +0000685 unsigned mNext = 0;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000686
Jay Foad7f0ce342011-07-11 07:28:49 +0000687 /// fMap - The slot map for the function level data.
Chris Lattner45d4c732008-08-17 04:17:45 +0000688 ValueMap fMap;
Eugene Zelenkof1934002017-06-19 22:05:08 +0000689 unsigned fNext = 0;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000690
Devang Patel320671d2009-07-08 21:44:25 +0000691 /// mdnMap - Map for MDNodes.
Chris Lattner307c9892009-12-31 02:20:11 +0000692 DenseMap<const MDNode*, unsigned> mdnMap;
Eugene Zelenkof1934002017-06-19 22:05:08 +0000693 unsigned mdnNext = 0;
Bill Wendlingb29ce262013-02-11 08:43:33 +0000694
695 /// asMap - The slot map for attribute sets.
Reid Kleckner06090402017-04-12 00:38:00 +0000696 DenseMap<AttributeSet, unsigned> asMap;
Eugene Zelenkof1934002017-06-19 22:05:08 +0000697 unsigned asNext = 0;
698
Teresa Johnson807ab9582018-07-02 22:09:23 +0000699 /// ModulePathMap - The slot map for Module paths used in the summary index.
700 StringMap<unsigned> ModulePathMap;
701 unsigned ModulePathNext = 0;
702
Teresa Johnsona9a21472018-05-26 02:34:13 +0000703 /// GUIDMap - The slot map for GUIDs used in the summary index.
704 DenseMap<GlobalValue::GUID, unsigned> GUIDMap;
705 unsigned GUIDNext = 0;
706
Teresa Johnson0d4dfd22018-09-25 20:14:40 +0000707 /// TypeIdMap - The slot map for type ids used in the summary index.
708 StringMap<unsigned> TypeIdMap;
709 unsigned TypeIdNext = 0;
710
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000711public:
Duncan P. N. Exon Smithf9f505e2015-03-14 20:19:36 +0000712 /// Construct from a module.
713 ///
714 /// If \c ShouldInitializeAllMetadata, initializes all metadata in all
715 /// functions, giving correct numbering for metadata referenced only from
716 /// within a function (even if no functions have been initialized).
717 explicit SlotTracker(const Module *M,
718 bool ShouldInitializeAllMetadata = false);
Eugene Zelenkof1934002017-06-19 22:05:08 +0000719
Chris Lattner45d4c732008-08-17 04:17:45 +0000720 /// Construct from a function, starting out in incorp state.
Duncan P. N. Exon Smithf9f505e2015-03-14 20:19:36 +0000721 ///
722 /// If \c ShouldInitializeAllMetadata, initializes all metadata in all
723 /// functions, giving correct numbering for metadata referenced only from
724 /// within a function (even if no functions have been initialized).
725 explicit SlotTracker(const Function *F,
726 bool ShouldInitializeAllMetadata = false);
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000727
Teresa Johnsona9a21472018-05-26 02:34:13 +0000728 /// Construct from a module summary index.
729 explicit SlotTracker(const ModuleSummaryIndex *Index);
730
Eugene Zelenkof1934002017-06-19 22:05:08 +0000731 SlotTracker(const SlotTracker &) = delete;
732 SlotTracker &operator=(const SlotTracker &) = delete;
733
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000734 /// Return the slot number of the specified value in it's type
Chris Lattner0d9574a2008-08-19 04:26:57 +0000735 /// plane. If something is not in the SlotTracker, return -1.
Chris Lattner22379bc2007-01-11 03:54:27 +0000736 int getLocalSlot(const Value *V);
737 int getGlobalSlot(const GlobalValue *V);
Duncan P. N. Exon Smith5bf8ade2014-11-11 21:30:22 +0000738 int getMetadataSlot(const MDNode *N);
Reid Kleckner06090402017-04-12 00:38:00 +0000739 int getAttributeGroupSlot(AttributeSet AS);
Teresa Johnson807ab9582018-07-02 22:09:23 +0000740 int getModulePathSlot(StringRef Path);
Teresa Johnsona9a21472018-05-26 02:34:13 +0000741 int getGUIDSlot(GlobalValue::GUID GUID);
Teresa Johnson0d4dfd22018-09-25 20:14:40 +0000742 int getTypeIdSlot(StringRef Id);
Reid Spencerfc621e22004-06-09 15:26:53 +0000743
Misha Brukmanfd939082005-04-21 23:48:37 +0000744 /// If you'd like to deal with a function instead of just a module, use
Chris Lattner0d9574a2008-08-19 04:26:57 +0000745 /// this method to get its data into the SlotTracker.
Misha Brukmanfd939082005-04-21 23:48:37 +0000746 void incorporateFunction(const Function *F) {
747 TheFunction = F;
Reid Spencer28531c72004-08-16 07:46:33 +0000748 FunctionProcessed = false;
749 }
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000750
Duncan P. N. Exon Smith78388182014-08-19 21:30:15 +0000751 const Function *getFunction() const { return TheFunction; }
752
Misha Brukmanfd939082005-04-21 23:48:37 +0000753 /// After calling incorporateFunction, use this method to remove the
Chris Lattner0d9574a2008-08-19 04:26:57 +0000754 /// most recently incorporated function from the SlotTracker. This
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000755 /// will reset the state of the machine back to just the module contents.
756 void purgeFunction();
757
Devang Patel320671d2009-07-08 21:44:25 +0000758 /// MDNode map iterators.
Eugene Zelenkof1934002017-06-19 22:05:08 +0000759 using mdn_iterator = DenseMap<const MDNode*, unsigned>::iterator;
760
Chris Lattner307c9892009-12-31 02:20:11 +0000761 mdn_iterator mdn_begin() { return mdnMap.begin(); }
762 mdn_iterator mdn_end() { return mdnMap.end(); }
763 unsigned mdn_size() const { return mdnMap.size(); }
764 bool mdn_empty() const { return mdnMap.empty(); }
Devang Patel320671d2009-07-08 21:44:25 +0000765
Reid Kleckner06090402017-04-12 00:38:00 +0000766 /// AttributeSet map iterators.
Eugene Zelenkof1934002017-06-19 22:05:08 +0000767 using as_iterator = DenseMap<AttributeSet, unsigned>::iterator;
768
Bill Wendlingb29ce262013-02-11 08:43:33 +0000769 as_iterator as_begin() { return asMap.begin(); }
770 as_iterator as_end() { return asMap.end(); }
771 unsigned as_size() const { return asMap.size(); }
772 bool as_empty() const { return asMap.empty(); }
773
Teresa Johnsona9a21472018-05-26 02:34:13 +0000774 /// GUID map iterators.
775 using guid_iterator = DenseMap<GlobalValue::GUID, unsigned>::iterator;
776
777 /// These functions do the actual initialization.
Teresa Johnsona9961f32018-07-03 15:52:57 +0000778 inline void initializeIfNeeded();
779 void initializeIndexIfNeeded();
Reid Spencerb03de0c2004-05-26 21:56:09 +0000780
Devang Patel320671d2009-07-08 21:44:25 +0000781 // Implementation Details
782private:
Chris Lattner9446bbe2007-01-09 07:55:49 +0000783 /// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
784 void CreateModuleSlot(const GlobalValue *V);
Devang Patel320671d2009-07-08 21:44:25 +0000785
786 /// CreateMetadataSlot - Insert the specified MDNode* into the slot table.
Duncan P. N. Exon Smith5bf8ade2014-11-11 21:30:22 +0000787 void CreateMetadataSlot(const MDNode *N);
Devang Patel320671d2009-07-08 21:44:25 +0000788
Chris Lattner9446bbe2007-01-09 07:55:49 +0000789 /// CreateFunctionSlot - Insert the specified Value* into the slot table.
790 void CreateFunctionSlot(const Value *V);
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000791
Adrian Prantl26b584c2018-05-01 15:54:18 +0000792 /// Insert the specified AttributeSet into the slot table.
Reid Kleckner06090402017-04-12 00:38:00 +0000793 void CreateAttributeSetSlot(AttributeSet AS);
Bill Wendlingb29ce262013-02-11 08:43:33 +0000794
Teresa Johnson807ab9582018-07-02 22:09:23 +0000795 inline void CreateModulePathSlot(StringRef Path);
Teresa Johnsona9a21472018-05-26 02:34:13 +0000796 void CreateGUIDSlot(GlobalValue::GUID GUID);
Teresa Johnson0d4dfd22018-09-25 20:14:40 +0000797 void CreateTypeIdSlot(StringRef Id);
Teresa Johnsona9a21472018-05-26 02:34:13 +0000798
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000799 /// Add all of the module level global variables (and their initializers)
800 /// and function declarations, but not the contents of those functions.
801 void processModule();
Teresa Johnsona9a21472018-05-26 02:34:13 +0000802 void processIndex();
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000803
Devang Patel320671d2009-07-08 21:44:25 +0000804 /// Add all of the functions arguments, basic blocks, and instructions.
Reid Spencerb03de0c2004-05-26 21:56:09 +0000805 void processFunction();
806
Peter Collingbourne6aef9f92016-05-31 23:01:54 +0000807 /// Add the metadata directly attached to a GlobalObject.
808 void processGlobalObjectMetadata(const GlobalObject &GO);
809
Duncan P. N. Exon Smithf9f505e2015-03-14 20:19:36 +0000810 /// Add all of the metadata from a function.
811 void processFunctionMetadata(const Function &F);
812
Duncan P. N. Exon Smith81ebfb12015-03-14 19:48:31 +0000813 /// Add all of the metadata from an instruction.
814 void processInstructionMetadata(const Instruction &I);
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000815};
Eugene Zelenkof1934002017-06-19 22:05:08 +0000816
817} // end namespace llvm
Duncan P. N. Exon Smith9a61a422015-06-26 22:04:20 +0000818
819ModuleSlotTracker::ModuleSlotTracker(SlotTracker &Machine, const Module *M,
820 const Function *F)
821 : M(M), F(F), Machine(&Machine) {}
822
Duncan P. N. Exon Smith8266df72015-06-26 22:28:47 +0000823ModuleSlotTracker::ModuleSlotTracker(const Module *M,
824 bool ShouldInitializeAllMetadata)
Duncan P. N. Exon Smith1487a2e2016-04-20 19:05:59 +0000825 : ShouldCreateStorage(M),
826 ShouldInitializeAllMetadata(ShouldInitializeAllMetadata), M(M) {}
Duncan P. N. Exon Smith9a61a422015-06-26 22:04:20 +0000827
Eugene Zelenkof1934002017-06-19 22:05:08 +0000828ModuleSlotTracker::~ModuleSlotTracker() = default;
Duncan P. N. Exon Smith9a61a422015-06-26 22:04:20 +0000829
Duncan P. N. Exon Smith1487a2e2016-04-20 19:05:59 +0000830SlotTracker *ModuleSlotTracker::getMachine() {
831 if (!ShouldCreateStorage)
832 return Machine;
833
834 ShouldCreateStorage = false;
835 MachineStorage =
836 llvm::make_unique<SlotTracker>(M, ShouldInitializeAllMetadata);
837 Machine = MachineStorage.get();
838 return Machine;
839}
840
Duncan P. N. Exon Smith9a61a422015-06-26 22:04:20 +0000841void ModuleSlotTracker::incorporateFunction(const Function &F) {
Duncan P. N. Exon Smith1487a2e2016-04-20 19:05:59 +0000842 // Using getMachine() may lazily create the slot tracker.
843 if (!getMachine())
Duncan P. N. Exon Smith9a61a422015-06-26 22:04:20 +0000844 return;
845
846 // Nothing to do if this is the right function already.
847 if (this->F == &F)
848 return;
849 if (this->F)
850 Machine->purgeFunction();
851 Machine->incorporateFunction(&F);
852 this->F = &F;
853}
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000854
Alex Lorenz9adb2122015-07-27 22:31:04 +0000855int ModuleSlotTracker::getLocalSlot(const Value *V) {
856 assert(F && "No function incorporated");
857 return Machine->getLocalSlot(V);
858}
859
Chris Lattner6ab910b2008-08-19 04:36:02 +0000860static SlotTracker *createSlotTracker(const Value *V) {
861 if (const Argument *FA = dyn_cast<Argument>(V))
862 return new SlotTracker(FA->getParent());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000863
Chris Lattner6ab910b2008-08-19 04:36:02 +0000864 if (const Instruction *I = dyn_cast<Instruction>(V))
Andrew Trick62e05902011-09-30 19:50:40 +0000865 if (I->getParent())
866 return new SlotTracker(I->getParent()->getParent());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000867
Chris Lattner6ab910b2008-08-19 04:36:02 +0000868 if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
869 return new SlotTracker(BB->getParent());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000870
Chris Lattner6ab910b2008-08-19 04:36:02 +0000871 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
872 return new SlotTracker(GV->getParent());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000873
Chris Lattner6ab910b2008-08-19 04:36:02 +0000874 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V))
Daniel Dunbara279bc32009-09-20 02:20:51 +0000875 return new SlotTracker(GA->getParent());
876
Dmitry Polukhinba492232016-04-07 12:32:19 +0000877 if (const GlobalIFunc *GIF = dyn_cast<GlobalIFunc>(V))
878 return new SlotTracker(GIF->getParent());
879
Chris Lattner6ab910b2008-08-19 04:36:02 +0000880 if (const Function *Func = dyn_cast<Function>(V))
881 return new SlotTracker(Func);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000882
Craig Topperec0f0bc2014-04-09 06:08:46 +0000883 return nullptr;
Chris Lattner6ab910b2008-08-19 04:36:02 +0000884}
885
886#if 0
David Greened865e022010-01-05 01:29:26 +0000887#define ST_DEBUG(X) dbgs() << X
Chris Lattner6ab910b2008-08-19 04:36:02 +0000888#else
Chris Lattner24233032008-08-19 04:47:09 +0000889#define ST_DEBUG(X)
Chris Lattner6ab910b2008-08-19 04:36:02 +0000890#endif
891
892// Module level constructor. Causes the contents of the Module (sans functions)
893// to be added to the slot table.
Duncan P. N. Exon Smithf9f505e2015-03-14 20:19:36 +0000894SlotTracker::SlotTracker(const Module *M, bool ShouldInitializeAllMetadata)
Eugene Zelenkof1934002017-06-19 22:05:08 +0000895 : TheModule(M), ShouldInitializeAllMetadata(ShouldInitializeAllMetadata) {}
Chris Lattner6ab910b2008-08-19 04:36:02 +0000896
897// Function level constructor. Causes the contents of the Module and the one
898// function provided to be added to the slot table.
Duncan P. N. Exon Smithf9f505e2015-03-14 20:19:36 +0000899SlotTracker::SlotTracker(const Function *F, bool ShouldInitializeAllMetadata)
Duncan P. N. Exon Smithdad20b22014-12-09 18:38:53 +0000900 : TheModule(F ? F->getParent() : nullptr), TheFunction(F),
Eugene Zelenkof1934002017-06-19 22:05:08 +0000901 ShouldInitializeAllMetadata(ShouldInitializeAllMetadata) {}
Chris Lattner6ab910b2008-08-19 04:36:02 +0000902
Teresa Johnsona9a21472018-05-26 02:34:13 +0000903SlotTracker::SlotTracker(const ModuleSummaryIndex *Index)
904 : TheModule(nullptr), ShouldInitializeAllMetadata(false), TheIndex(Index) {}
905
Teresa Johnsona9961f32018-07-03 15:52:57 +0000906inline void SlotTracker::initializeIfNeeded() {
Chris Lattner6ab910b2008-08-19 04:36:02 +0000907 if (TheModule) {
908 processModule();
Craig Topperec0f0bc2014-04-09 06:08:46 +0000909 TheModule = nullptr; ///< Prevent re-processing next time we're called.
Chris Lattner6ab910b2008-08-19 04:36:02 +0000910 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000911
Chris Lattner6ab910b2008-08-19 04:36:02 +0000912 if (TheFunction && !FunctionProcessed)
913 processFunction();
914}
915
Teresa Johnsona9961f32018-07-03 15:52:57 +0000916void SlotTracker::initializeIndexIfNeeded() {
Teresa Johnsona9a21472018-05-26 02:34:13 +0000917 if (!TheIndex)
918 return;
919 processIndex();
920 TheIndex = nullptr; ///< Prevent re-processing next time we're called.
921}
922
Chris Lattner6ab910b2008-08-19 04:36:02 +0000923// Iterate through all the global variables, functions, and global
924// variable initializers and create slots for them.
925void SlotTracker::processModule() {
Chris Lattner24233032008-08-19 04:47:09 +0000926 ST_DEBUG("begin processModule!\n");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000927
Chris Lattner6ab910b2008-08-19 04:36:02 +0000928 // Add all of the unnamed global variables to the value table.
Rafael Espindolab200e2f2015-06-17 17:33:37 +0000929 for (const GlobalVariable &Var : TheModule->globals()) {
930 if (!Var.hasName())
931 CreateModuleSlot(&Var);
Peter Collingbourne6aef9f92016-05-31 23:01:54 +0000932 processGlobalObjectMetadata(Var);
Javed Absara8ddcaa2017-05-11 12:28:08 +0000933 auto Attrs = Var.getAttributes();
934 if (Attrs.hasAttributes())
935 CreateAttributeSetSlot(Attrs);
Devang Patel320671d2009-07-08 21:44:25 +0000936 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000937
Rafael Espindola43e53492015-06-17 17:53:31 +0000938 for (const GlobalAlias &A : TheModule->aliases()) {
939 if (!A.hasName())
940 CreateModuleSlot(&A);
941 }
942
Dmitry Polukhinba492232016-04-07 12:32:19 +0000943 for (const GlobalIFunc &I : TheModule->ifuncs()) {
944 if (!I.hasName())
945 CreateModuleSlot(&I);
946 }
947
Devang Patel37c4a2d2009-07-29 22:04:47 +0000948 // Add metadata used by named metadata.
Rafael Espindolab200e2f2015-06-17 17:33:37 +0000949 for (const NamedMDNode &NMD : TheModule->named_metadata()) {
950 for (unsigned i = 0, e = NMD.getNumOperands(); i != e; ++i)
951 CreateMetadataSlot(NMD.getOperand(i));
Devang Patel37c4a2d2009-07-29 22:04:47 +0000952 }
953
Rafael Espindolab200e2f2015-06-17 17:33:37 +0000954 for (const Function &F : *TheModule) {
955 if (!F.hasName())
Bill Wendlingb29ce262013-02-11 08:43:33 +0000956 // Add all the unnamed functions to the table.
Rafael Espindolab200e2f2015-06-17 17:33:37 +0000957 CreateModuleSlot(&F);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000958
Duncan P. N. Exon Smithf9f505e2015-03-14 20:19:36 +0000959 if (ShouldInitializeAllMetadata)
Rafael Espindolab200e2f2015-06-17 17:33:37 +0000960 processFunctionMetadata(F);
Duncan P. N. Exon Smithf9f505e2015-03-14 20:19:36 +0000961
Bill Wendlingb29ce262013-02-11 08:43:33 +0000962 // Add all the function attributes to the table.
Bill Wendling7ab6c762013-02-20 07:21:42 +0000963 // FIXME: Add attributes of other objects?
Reid Kleckner06090402017-04-12 00:38:00 +0000964 AttributeSet FnAttrs = F.getAttributes().getFnAttributes();
965 if (FnAttrs.hasAttributes())
Bill Wendlingb29ce262013-02-11 08:43:33 +0000966 CreateAttributeSetSlot(FnAttrs);
967 }
968
Chris Lattner24233032008-08-19 04:47:09 +0000969 ST_DEBUG("end processModule!\n");
Chris Lattner6ab910b2008-08-19 04:36:02 +0000970}
971
Chris Lattner6ab910b2008-08-19 04:36:02 +0000972// Process the arguments, basic blocks, and instructions of a function.
973void SlotTracker::processFunction() {
Chris Lattner24233032008-08-19 04:47:09 +0000974 ST_DEBUG("begin processFunction!\n");
Chris Lattner6ab910b2008-08-19 04:36:02 +0000975 fNext = 0;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000976
Duncan P. N. Exon Smith812e1e42015-09-11 01:34:59 +0000977 // Process function metadata if it wasn't hit at the module-level.
978 if (!ShouldInitializeAllMetadata)
979 processFunctionMetadata(*TheFunction);
980
Chris Lattner6ab910b2008-08-19 04:36:02 +0000981 // Add all the function arguments with no names.
982 for(Function::const_arg_iterator AI = TheFunction->arg_begin(),
983 AE = TheFunction->arg_end(); AI != AE; ++AI)
984 if (!AI->hasName())
Duncan P. N. Exon Smitheac30952015-10-08 23:49:46 +0000985 CreateFunctionSlot(&*AI);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000986
Chris Lattner24233032008-08-19 04:47:09 +0000987 ST_DEBUG("Inserting Instructions:\n");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000988
Chris Lattner6ab910b2008-08-19 04:36:02 +0000989 // Add all of the basic blocks and instructions with no names.
Duncan P. N. Exon Smithff0e5a62015-03-14 19:44:01 +0000990 for (auto &BB : *TheFunction) {
991 if (!BB.hasName())
992 CreateFunctionSlot(&BB);
Andrew Trick18801ec2011-09-30 19:48:58 +0000993
Duncan P. N. Exon Smithff0e5a62015-03-14 19:44:01 +0000994 for (auto &I : BB) {
995 if (!I.getType()->isVoidTy() && !I.hasName())
996 CreateFunctionSlot(&I);
Andrew Trick18801ec2011-09-30 19:48:58 +0000997
Duncan P. N. Exon Smith81ebfb12015-03-14 19:48:31 +0000998 // We allow direct calls to any llvm.foo function here, because the
999 // target may not be linked into the optimizer.
Chandler Carruth9f32dc92019-01-07 07:31:49 +00001000 if (const auto *Call = dyn_cast<CallBase>(&I)) {
Bill Wendling351b7a12013-02-22 09:09:42 +00001001 // Add all the call attributes to the table.
Chandler Carruth9f32dc92019-01-07 07:31:49 +00001002 AttributeSet Attrs = Call->getAttributes().getFnAttributes();
Reid Kleckner06090402017-04-12 00:38:00 +00001003 if (Attrs.hasAttributes())
Bill Wendling351b7a12013-02-22 09:09:42 +00001004 CreateAttributeSetSlot(Attrs);
Chris Lattnerfd450c02010-05-10 20:53:17 +00001005 }
Devang Patel320671d2009-07-08 21:44:25 +00001006 }
Chris Lattner6ab910b2008-08-19 04:36:02 +00001007 }
Devang Patel43215782009-09-16 20:21:17 +00001008
Chris Lattner6ab910b2008-08-19 04:36:02 +00001009 FunctionProcessed = true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001010
Chris Lattner24233032008-08-19 04:47:09 +00001011 ST_DEBUG("end processFunction!\n");
Chris Lattner6ab910b2008-08-19 04:36:02 +00001012}
1013
Teresa Johnsona9a21472018-05-26 02:34:13 +00001014// Iterate through all the GUID in the index and create slots for them.
1015void SlotTracker::processIndex() {
1016 ST_DEBUG("begin processIndex!\n");
1017 assert(TheIndex);
1018
1019 // The first block of slots are just the module ids, which start at 0 and are
Teresa Johnson807ab9582018-07-02 22:09:23 +00001020 // assigned consecutively. Since the StringMap iteration order isn't
1021 // guaranteed, use a std::map to order by module ID before assigning slots.
1022 std::map<uint64_t, StringRef> ModuleIdToPathMap;
1023 for (auto &ModPath : TheIndex->modulePaths())
1024 ModuleIdToPathMap[ModPath.second.first] = ModPath.first();
1025 for (auto &ModPair : ModuleIdToPathMap)
1026 CreateModulePathSlot(ModPair.second);
1027
1028 // Start numbering the GUIDs after the module ids.
1029 GUIDNext = ModulePathNext;
Teresa Johnsona9a21472018-05-26 02:34:13 +00001030
1031 for (auto &GlobalList : *TheIndex)
1032 CreateGUIDSlot(GlobalList.first);
1033
Teresa Johnson0d4dfd22018-09-25 20:14:40 +00001034 // Start numbering the TypeIds after the GUIDs.
1035 TypeIdNext = GUIDNext;
1036
1037 for (auto TidIter = TheIndex->typeIds().begin();
1038 TidIter != TheIndex->typeIds().end(); TidIter++)
1039 CreateTypeIdSlot(TidIter->second.first);
Teresa Johnsona9a21472018-05-26 02:34:13 +00001040
1041 ST_DEBUG("end processIndex!\n");
1042}
1043
Peter Collingbourne6aef9f92016-05-31 23:01:54 +00001044void SlotTracker::processGlobalObjectMetadata(const GlobalObject &GO) {
Duncan P. N. Exon Smithae321142015-04-24 22:04:41 +00001045 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
Peter Collingbourne6aef9f92016-05-31 23:01:54 +00001046 GO.getAllMetadata(MDs);
Duncan P. N. Exon Smith812e1e42015-09-11 01:34:59 +00001047 for (auto &MD : MDs)
1048 CreateMetadataSlot(MD.second);
Peter Collingbourne6aef9f92016-05-31 23:01:54 +00001049}
Duncan P. N. Exon Smithae321142015-04-24 22:04:41 +00001050
Peter Collingbourne6aef9f92016-05-31 23:01:54 +00001051void SlotTracker::processFunctionMetadata(const Function &F) {
1052 processGlobalObjectMetadata(F);
Duncan P. N. Exon Smith812e1e42015-09-11 01:34:59 +00001053 for (auto &BB : F) {
Duncan P. N. Exon Smithf9f505e2015-03-14 20:19:36 +00001054 for (auto &I : BB)
1055 processInstructionMetadata(I);
Duncan P. N. Exon Smithae321142015-04-24 22:04:41 +00001056 }
Duncan P. N. Exon Smithf9f505e2015-03-14 20:19:36 +00001057}
1058
Duncan P. N. Exon Smith81ebfb12015-03-14 19:48:31 +00001059void SlotTracker::processInstructionMetadata(const Instruction &I) {
1060 // Process metadata used directly by intrinsics.
1061 if (const CallInst *CI = dyn_cast<CallInst>(&I))
1062 if (Function *F = CI->getCalledFunction())
Justin Lebar24dbd382016-07-28 23:58:15 +00001063 if (F->isIntrinsic())
Duncan P. N. Exon Smith81ebfb12015-03-14 19:48:31 +00001064 for (auto &Op : I.operands())
1065 if (auto *V = dyn_cast_or_null<MetadataAsValue>(Op))
1066 if (MDNode *N = dyn_cast<MDNode>(V->getMetadata()))
1067 CreateMetadataSlot(N);
1068
1069 // Process metadata attached to this instruction.
1070 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
1071 I.getAllMetadata(MDs);
1072 for (auto &MD : MDs)
1073 CreateMetadataSlot(MD.second);
1074}
1075
Chris Lattner6ab910b2008-08-19 04:36:02 +00001076/// Clean up after incorporating a function. This is the only way to get out of
1077/// the function incorporation state that affects get*Slot/Create*Slot. Function
1078/// incorporation state is indicated by TheFunction != 0.
1079void SlotTracker::purgeFunction() {
Chris Lattner24233032008-08-19 04:47:09 +00001080 ST_DEBUG("begin purgeFunction!\n");
Chris Lattner6ab910b2008-08-19 04:36:02 +00001081 fMap.clear(); // Simply discard the function level map
Craig Topperec0f0bc2014-04-09 06:08:46 +00001082 TheFunction = nullptr;
Chris Lattner6ab910b2008-08-19 04:36:02 +00001083 FunctionProcessed = false;
Chris Lattner24233032008-08-19 04:47:09 +00001084 ST_DEBUG("end purgeFunction!\n");
Chris Lattner6ab910b2008-08-19 04:36:02 +00001085}
1086
1087/// getGlobalSlot - Get the slot number of a global value.
1088int SlotTracker::getGlobalSlot(const GlobalValue *V) {
1089 // Check for uninitialized state and do lazy initialization.
Teresa Johnsona9961f32018-07-03 15:52:57 +00001090 initializeIfNeeded();
Daniel Dunbara279bc32009-09-20 02:20:51 +00001091
Jay Foad7f0ce342011-07-11 07:28:49 +00001092 // Find the value in the module map
Chris Lattner6ab910b2008-08-19 04:36:02 +00001093 ValueMap::iterator MI = mMap.find(V);
Dan Gohmanaeaf2452008-10-01 19:58:59 +00001094 return MI == mMap.end() ? -1 : (int)MI->second;
Chris Lattner6ab910b2008-08-19 04:36:02 +00001095}
1096
Duncan P. N. Exon Smith5bf8ade2014-11-11 21:30:22 +00001097/// getMetadataSlot - Get the slot number of a MDNode.
1098int SlotTracker::getMetadataSlot(const MDNode *N) {
Devang Patel320671d2009-07-08 21:44:25 +00001099 // Check for uninitialized state and do lazy initialization.
Teresa Johnsona9961f32018-07-03 15:52:57 +00001100 initializeIfNeeded();
Daniel Dunbara279bc32009-09-20 02:20:51 +00001101
Jay Foad7f0ce342011-07-11 07:28:49 +00001102 // Find the MDNode in the module map
Duncan P. N. Exon Smith5bf8ade2014-11-11 21:30:22 +00001103 mdn_iterator MI = mdnMap.find(N);
Devang Patel320671d2009-07-08 21:44:25 +00001104 return MI == mdnMap.end() ? -1 : (int)MI->second;
1105}
1106
Chris Lattner6ab910b2008-08-19 04:36:02 +00001107/// getLocalSlot - Get the slot number for a value that is local to a function.
1108int SlotTracker::getLocalSlot(const Value *V) {
1109 assert(!isa<Constant>(V) && "Can't get a constant or global slot with this!");
Daniel Dunbara279bc32009-09-20 02:20:51 +00001110
Chris Lattner6ab910b2008-08-19 04:36:02 +00001111 // Check for uninitialized state and do lazy initialization.
Teresa Johnsona9961f32018-07-03 15:52:57 +00001112 initializeIfNeeded();
Daniel Dunbara279bc32009-09-20 02:20:51 +00001113
Chris Lattner6ab910b2008-08-19 04:36:02 +00001114 ValueMap::iterator FI = fMap.find(V);
Dan Gohmanaeaf2452008-10-01 19:58:59 +00001115 return FI == fMap.end() ? -1 : (int)FI->second;
Chris Lattner6ab910b2008-08-19 04:36:02 +00001116}
1117
Reid Kleckner06090402017-04-12 00:38:00 +00001118int SlotTracker::getAttributeGroupSlot(AttributeSet AS) {
Bill Wendlingb29ce262013-02-11 08:43:33 +00001119 // Check for uninitialized state and do lazy initialization.
Teresa Johnsona9961f32018-07-03 15:52:57 +00001120 initializeIfNeeded();
Bill Wendlingb29ce262013-02-11 08:43:33 +00001121
Reid Kleckner06090402017-04-12 00:38:00 +00001122 // Find the AttributeSet in the module map.
Bill Wendlingb29ce262013-02-11 08:43:33 +00001123 as_iterator AI = asMap.find(AS);
1124 return AI == asMap.end() ? -1 : (int)AI->second;
1125}
Chris Lattner6ab910b2008-08-19 04:36:02 +00001126
Teresa Johnson807ab9582018-07-02 22:09:23 +00001127int SlotTracker::getModulePathSlot(StringRef Path) {
1128 // Check for uninitialized state and do lazy initialization.
Teresa Johnsona9961f32018-07-03 15:52:57 +00001129 initializeIndexIfNeeded();
Teresa Johnson807ab9582018-07-02 22:09:23 +00001130
1131 // Find the Module path in the map
1132 auto I = ModulePathMap.find(Path);
1133 return I == ModulePathMap.end() ? -1 : (int)I->second;
1134}
1135
Teresa Johnsona9a21472018-05-26 02:34:13 +00001136int SlotTracker::getGUIDSlot(GlobalValue::GUID GUID) {
1137 // Check for uninitialized state and do lazy initialization.
Teresa Johnsona9961f32018-07-03 15:52:57 +00001138 initializeIndexIfNeeded();
Teresa Johnsona9a21472018-05-26 02:34:13 +00001139
1140 // Find the GUID in the map
1141 guid_iterator I = GUIDMap.find(GUID);
1142 return I == GUIDMap.end() ? -1 : (int)I->second;
1143}
1144
Teresa Johnson0d4dfd22018-09-25 20:14:40 +00001145int SlotTracker::getTypeIdSlot(StringRef Id) {
1146 // Check for uninitialized state and do lazy initialization.
1147 initializeIndexIfNeeded();
1148
1149 // Find the TypeId string in the map
1150 auto I = TypeIdMap.find(Id);
1151 return I == TypeIdMap.end() ? -1 : (int)I->second;
1152}
1153
Chris Lattner6ab910b2008-08-19 04:36:02 +00001154/// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
1155void SlotTracker::CreateModuleSlot(const GlobalValue *V) {
1156 assert(V && "Can't insert a null Value into SlotTracker!");
Chris Lattner4ee93c42009-12-29 07:25:48 +00001157 assert(!V->getType()->isVoidTy() && "Doesn't need a slot!");
Chris Lattner6ab910b2008-08-19 04:36:02 +00001158 assert(!V->hasName() && "Doesn't need a slot!");
Daniel Dunbara279bc32009-09-20 02:20:51 +00001159
Chris Lattner6ab910b2008-08-19 04:36:02 +00001160 unsigned DestSlot = mNext++;
1161 mMap[V] = DestSlot;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001162
Chris Lattner24233032008-08-19 04:47:09 +00001163 ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" <<
Chris Lattner6ab910b2008-08-19 04:36:02 +00001164 DestSlot << " [");
Dmitry Polukhinba492232016-04-07 12:32:19 +00001165 // G = Global, F = Function, A = Alias, I = IFunc, o = other
Chris Lattner24233032008-08-19 04:47:09 +00001166 ST_DEBUG((isa<GlobalVariable>(V) ? 'G' :
Chris Lattner6ab910b2008-08-19 04:36:02 +00001167 (isa<Function>(V) ? 'F' :
Dmitry Polukhinba492232016-04-07 12:32:19 +00001168 (isa<GlobalAlias>(V) ? 'A' :
1169 (isa<GlobalIFunc>(V) ? 'I' : 'o')))) << "]\n");
Chris Lattner6ab910b2008-08-19 04:36:02 +00001170}
1171
Chris Lattner6ab910b2008-08-19 04:36:02 +00001172/// CreateSlot - Create a new slot for the specified value if it has no name.
1173void SlotTracker::CreateFunctionSlot(const Value *V) {
Chris Lattner4ee93c42009-12-29 07:25:48 +00001174 assert(!V->getType()->isVoidTy() && !V->hasName() && "Doesn't need a slot!");
Daniel Dunbara279bc32009-09-20 02:20:51 +00001175
Chris Lattner6ab910b2008-08-19 04:36:02 +00001176 unsigned DestSlot = fNext++;
1177 fMap[V] = DestSlot;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001178
Chris Lattner6ab910b2008-08-19 04:36:02 +00001179 // G = Global, F = Function, o = other
Chris Lattner24233032008-08-19 04:47:09 +00001180 ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" <<
Chris Lattner6ab910b2008-08-19 04:36:02 +00001181 DestSlot << " [o]\n");
Daniel Dunbara279bc32009-09-20 02:20:51 +00001182}
Chris Lattner6ab910b2008-08-19 04:36:02 +00001183
Duncan P. N. Exon Smith5bf8ade2014-11-11 21:30:22 +00001184/// CreateModuleSlot - Insert the specified MDNode* into the slot table.
1185void SlotTracker::CreateMetadataSlot(const MDNode *N) {
1186 assert(N && "Can't insert a null Value into SlotTracker!");
Daniel Dunbara279bc32009-09-20 02:20:51 +00001187
Reid Klecknera5b2af02017-08-23 20:31:27 +00001188 // Don't make slots for DIExpressions. We just print them inline everywhere.
1189 if (isa<DIExpression>(N))
1190 return;
1191
Duncan P. N. Exon Smithdad20b22014-12-09 18:38:53 +00001192 unsigned DestSlot = mdnNext;
1193 if (!mdnMap.insert(std::make_pair(N, DestSlot)).second)
1194 return;
1195 ++mdnNext;
Devang Patel320671d2009-07-08 21:44:25 +00001196
Chris Lattner2b4b1e22009-12-31 02:27:30 +00001197 // Recursively add any MDNodes referenced by operands.
1198 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1199 if (const MDNode *Op = dyn_cast_or_null<MDNode>(N->getOperand(i)))
1200 CreateMetadataSlot(Op);
Devang Patel320671d2009-07-08 21:44:25 +00001201}
Chris Lattner6ab910b2008-08-19 04:36:02 +00001202
Reid Kleckner06090402017-04-12 00:38:00 +00001203void SlotTracker::CreateAttributeSetSlot(AttributeSet AS) {
1204 assert(AS.hasAttributes() && "Doesn't need a slot!");
Bill Wendlingb29ce262013-02-11 08:43:33 +00001205
1206 as_iterator I = asMap.find(AS);
1207 if (I != asMap.end())
1208 return;
1209
1210 unsigned DestSlot = asNext++;
1211 asMap[AS] = DestSlot;
1212}
1213
Teresa Johnson807ab9582018-07-02 22:09:23 +00001214/// Create a new slot for the specified Module
1215void SlotTracker::CreateModulePathSlot(StringRef Path) {
1216 ModulePathMap[Path] = ModulePathNext++;
1217}
1218
Teresa Johnsona9a21472018-05-26 02:34:13 +00001219/// Create a new slot for the specified GUID
1220void SlotTracker::CreateGUIDSlot(GlobalValue::GUID GUID) {
1221 GUIDMap[GUID] = GUIDNext++;
1222}
1223
Teresa Johnson0d4dfd22018-09-25 20:14:40 +00001224/// Create a new slot for the specified Id
1225void SlotTracker::CreateTypeIdSlot(StringRef Id) {
1226 TypeIdMap[Id] = TypeIdNext++;
1227}
1228
Chris Lattner6ab910b2008-08-19 04:36:02 +00001229//===----------------------------------------------------------------------===//
1230// AsmWriter Implementation
1231//===----------------------------------------------------------------------===//
Chris Lattnerf082b802002-07-23 18:07:49 +00001232
Dan Gohman1220e102009-08-12 20:56:03 +00001233static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
Dan Gohmand6c0f652009-08-13 15:27:57 +00001234 TypePrinting *TypePrinter,
Dan Gohman3bdfbf52010-07-20 23:55:01 +00001235 SlotTracker *Machine,
1236 const Module *Context);
Reid Spencer0e25e1c2004-07-04 11:50:43 +00001237
Duncan P. N. Exon Smithdad20b22014-12-09 18:38:53 +00001238static void WriteAsOperandInternal(raw_ostream &Out, const Metadata *MD,
1239 TypePrinting *TypePrinter,
1240 SlotTracker *Machine, const Module *Context,
1241 bool FromValue = false);
1242
Dan Gohman1220e102009-08-12 20:56:03 +00001243static void WriteOptimizationInfo(raw_ostream &Out, const User *U) {
Michael Ilseman15c13d32012-11-27 00:42:44 +00001244 if (const FPMathOperator *FPO = dyn_cast<const FPMathOperator>(U)) {
Sanjay Patel00e900a2017-11-06 16:27:15 +00001245 // 'Fast' is an abbreviation for all fast-math-flags.
1246 if (FPO->isFast())
Michael Ilseman15c13d32012-11-27 00:42:44 +00001247 Out << " fast";
1248 else {
Sanjay Patel00e900a2017-11-06 16:27:15 +00001249 if (FPO->hasAllowReassoc())
1250 Out << " reassoc";
Michael Ilseman15c13d32012-11-27 00:42:44 +00001251 if (FPO->hasNoNaNs())
1252 Out << " nnan";
1253 if (FPO->hasNoInfs())
1254 Out << " ninf";
1255 if (FPO->hasNoSignedZeros())
1256 Out << " nsz";
1257 if (FPO->hasAllowReciprocal())
1258 Out << " arcp";
Adam Nemet5c57c112017-03-28 20:11:52 +00001259 if (FPO->hasAllowContract())
1260 Out << " contract";
Sanjay Patel00e900a2017-11-06 16:27:15 +00001261 if (FPO->hasApproxFunc())
1262 Out << " afn";
Michael Ilseman15c13d32012-11-27 00:42:44 +00001263 }
1264 }
1265
Dan Gohman1224c382009-07-20 21:19:07 +00001266 if (const OverflowingBinaryOperator *OBO =
1267 dyn_cast<OverflowingBinaryOperator>(U)) {
Dan Gohman5078f842009-08-20 17:11:38 +00001268 if (OBO->hasNoUnsignedWrap())
Dan Gohman59858cf2009-07-27 16:11:46 +00001269 Out << " nuw";
Dan Gohman5078f842009-08-20 17:11:38 +00001270 if (OBO->hasNoSignedWrap())
Dan Gohman59858cf2009-07-27 16:11:46 +00001271 Out << " nsw";
Chris Lattner35bda892011-02-06 21:44:57 +00001272 } else if (const PossiblyExactOperator *Div =
1273 dyn_cast<PossiblyExactOperator>(U)) {
Dan Gohman1224c382009-07-20 21:19:07 +00001274 if (Div->isExact())
Dan Gohman59858cf2009-07-27 16:11:46 +00001275 Out << " exact";
Dan Gohmandd8004d2009-07-27 21:53:46 +00001276 } else if (const GEPOperator *GEP = dyn_cast<GEPOperator>(U)) {
1277 if (GEP->isInBounds())
1278 Out << " inbounds";
Dan Gohman1224c382009-07-20 21:19:07 +00001279 }
1280}
1281
Dan Gohman40cf12f2010-07-14 20:57:55 +00001282static void WriteConstantInternal(raw_ostream &Out, const Constant *CV,
1283 TypePrinting &TypePrinter,
Dan Gohman3bdfbf52010-07-20 23:55:01 +00001284 SlotTracker *Machine,
1285 const Module *Context) {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001286 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001287 if (CI->getType()->isIntegerTy(1)) {
Reid Spencer579dca12007-01-12 04:24:46 +00001288 Out << (CI->getZExtValue() ? "true" : "false");
Chris Lattnerfad86b02008-08-17 07:19:36 +00001289 return;
1290 }
1291 Out << CI->getValue();
1292 return;
1293 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001294
Chris Lattnerfad86b02008-08-17 07:19:36 +00001295 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Serguei Katkovc849a0c2017-04-21 02:52:17 +00001296 const APFloat &APF = CFP->getValueAPF();
1297 if (&APF.getSemantics() == &APFloat::IEEEsingle() ||
1298 &APF.getSemantics() == &APFloat::IEEEdouble()) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00001299 // We would like to output the FP constant value in exponential notation,
1300 // but we cannot do this if doing so will lose precision. Check here to
1301 // make sure that we only output it in exponential format if we can parse
1302 // the value back and get the same value.
1303 //
Dale Johannesen541ed9f2009-01-21 20:32:55 +00001304 bool ignored;
Serguei Katkovc849a0c2017-04-21 02:52:17 +00001305 bool isDouble = &APF.getSemantics() == &APFloat::IEEEdouble();
1306 bool isInf = APF.isInfinity();
1307 bool isNaN = APF.isNaN();
Justin Bogner2cb46212015-12-04 01:14:24 +00001308 if (!isInf && !isNaN) {
Serguei Katkovc849a0c2017-04-21 02:52:17 +00001309 double Val = isDouble ? APF.convertToDouble() : APF.convertToFloat();
Dan Gohmance163392011-12-17 00:04:22 +00001310 SmallString<128> StrVal;
Serguei Katkovc849a0c2017-04-21 02:52:17 +00001311 APF.toString(StrVal, 6, 0, false);
Dan Gohmance163392011-12-17 00:04:22 +00001312 // Check to make sure that the stringized number is not some string like
1313 // "Inf" or NaN, that atof will accept, but the lexer will not. Check
1314 // that the string matches the "[-+]?[0-9]" regex.
1315 //
Serguei Katkov9bb6c3a2017-04-21 06:14:38 +00001316 assert(((StrVal[0] >= '0' && StrVal[0] <= '9') ||
1317 ((StrVal[0] == '-' || StrVal[0] == '+') &&
1318 (StrVal[1] >= '0' && StrVal[1] <= '9'))) &&
1319 "[-+]?[0-9] regex does not match!");
Serguei Katkovc849a0c2017-04-21 02:52:17 +00001320 // Reparse stringized version!
1321 if (APFloat(APFloat::IEEEdouble(), StrVal).convertToDouble() == Val) {
1322 Out << StrVal;
1323 return;
Dale Johannesen9d5f4562007-09-12 03:30:33 +00001324 }
Chris Lattner66e810b2002-04-18 18:53:13 +00001325 }
Dale Johannesen9d5f4562007-09-12 03:30:33 +00001326 // Otherwise we could not reparse it to exactly the same value, so we must
Dale Johannesen541ed9f2009-01-21 20:32:55 +00001327 // output the string in hexadecimal format! Note that loading and storing
1328 // floating point types changes the bits of NaNs on some hosts, notably
1329 // x86, so we must not use these types.
Benjamin Kramer571832b2014-03-15 18:47:07 +00001330 static_assert(sizeof(double) == sizeof(uint64_t),
1331 "assuming that double is 64 bits!");
Serguei Katkovc849a0c2017-04-21 02:52:17 +00001332 APFloat apf = APF;
Justin Bogner2cb46212015-12-04 01:14:24 +00001333 // Floats are represented in ASCII IR as double, convert.
Dale Johannesen541ed9f2009-01-21 20:32:55 +00001334 if (!isDouble)
Stephan Bergmann20a600c2016-12-14 11:57:17 +00001335 apf.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven,
Dale Johannesen541ed9f2009-01-21 20:32:55 +00001336 &ignored);
Justin Bognerd8def4a2015-12-04 02:14:34 +00001337 Out << format_hex(apf.bitcastToAPInt().getZExtValue(), 0, /*Upper=*/true);
Chris Lattnercfb5a202008-08-19 05:06:27 +00001338 return;
1339 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001340
Tobias Grosser057beb82012-05-24 15:59:06 +00001341 // Either half, or some form of long double.
1342 // These appear as a magic letter identifying the type, then a
1343 // fixed number of hex digits.
Chris Lattnercfb5a202008-08-19 05:06:27 +00001344 Out << "0x";
Serguei Katkovc849a0c2017-04-21 02:52:17 +00001345 APInt API = APF.bitcastToAPInt();
1346 if (&APF.getSemantics() == &APFloat::x87DoubleExtended()) {
Chris Lattnercfb5a202008-08-19 05:06:27 +00001347 Out << 'K';
Justin Bognerd8def4a2015-12-04 02:14:34 +00001348 Out << format_hex_no_prefix(API.getHiBits(16).getZExtValue(), 4,
1349 /*Upper=*/true);
1350 Out << format_hex_no_prefix(API.getLoBits(64).getZExtValue(), 16,
1351 /*Upper=*/true);
Dale Johannesen1b25cb22009-03-23 21:16:53 +00001352 return;
Serguei Katkovc849a0c2017-04-21 02:52:17 +00001353 } else if (&APF.getSemantics() == &APFloat::IEEEquad()) {
Chris Lattnercfb5a202008-08-19 05:06:27 +00001354 Out << 'L';
Justin Bognerd8def4a2015-12-04 02:14:34 +00001355 Out << format_hex_no_prefix(API.getLoBits(64).getZExtValue(), 16,
1356 /*Upper=*/true);
1357 Out << format_hex_no_prefix(API.getHiBits(64).getZExtValue(), 16,
1358 /*Upper=*/true);
Serguei Katkovc849a0c2017-04-21 02:52:17 +00001359 } else if (&APF.getSemantics() == &APFloat::PPCDoubleDouble()) {
Chris Lattnercfb5a202008-08-19 05:06:27 +00001360 Out << 'M';
Justin Bognerd8def4a2015-12-04 02:14:34 +00001361 Out << format_hex_no_prefix(API.getLoBits(64).getZExtValue(), 16,
1362 /*Upper=*/true);
1363 Out << format_hex_no_prefix(API.getHiBits(64).getZExtValue(), 16,
1364 /*Upper=*/true);
Serguei Katkovc849a0c2017-04-21 02:52:17 +00001365 } else if (&APF.getSemantics() == &APFloat::IEEEhalf()) {
Tobias Grosser057beb82012-05-24 15:59:06 +00001366 Out << 'H';
Justin Bognerd8def4a2015-12-04 02:14:34 +00001367 Out << format_hex_no_prefix(API.getZExtValue(), 4,
1368 /*Upper=*/true);
Tobias Grosser057beb82012-05-24 15:59:06 +00001369 } else
Torok Edwinc23197a2009-07-14 16:55:14 +00001370 llvm_unreachable("Unsupported floating point type");
Chris Lattnercfb5a202008-08-19 05:06:27 +00001371 return;
1372 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001373
Chris Lattnercfb5a202008-08-19 05:06:27 +00001374 if (isa<ConstantAggregateZero>(CV)) {
Chris Lattnerde512b52004-02-15 05:55:15 +00001375 Out << "zeroinitializer";
Chris Lattnercfb5a202008-08-19 05:06:27 +00001376 return;
1377 }
Andrew Trick18801ec2011-09-30 19:48:58 +00001378
Chris Lattner73050e12009-10-28 03:38:12 +00001379 if (const BlockAddress *BA = dyn_cast<BlockAddress>(CV)) {
1380 Out << "blockaddress(";
Dan Gohman3bdfbf52010-07-20 23:55:01 +00001381 WriteAsOperandInternal(Out, BA->getFunction(), &TypePrinter, Machine,
1382 Context);
Chris Lattner73050e12009-10-28 03:38:12 +00001383 Out << ", ";
Dan Gohman3bdfbf52010-07-20 23:55:01 +00001384 WriteAsOperandInternal(Out, BA->getBasicBlock(), &TypePrinter, Machine,
1385 Context);
Chris Lattner73050e12009-10-28 03:38:12 +00001386 Out << ")";
1387 return;
1388 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001389
Chris Lattnercfb5a202008-08-19 05:06:27 +00001390 if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
Chris Lattner1afcace2011-07-09 17:41:24 +00001391 Type *ETy = CA->getType()->getElementType();
Chris Lattner18c7f802012-02-05 02:29:43 +00001392 Out << '[';
1393 TypePrinter.print(ETy, Out);
1394 Out << ' ';
1395 WriteAsOperandInternal(Out, CA->getOperand(0),
1396 &TypePrinter, Machine,
1397 Context);
1398 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
1399 Out << ", ";
Chris Lattner8b10b692012-01-31 03:15:40 +00001400 TypePrinter.print(ETy, Out);
1401 Out << ' ';
Chris Lattner18c7f802012-02-05 02:29:43 +00001402 WriteAsOperandInternal(Out, CA->getOperand(i), &TypePrinter, Machine,
Chris Lattner8b10b692012-01-31 03:15:40 +00001403 Context);
Chris Lattner7a716ad2002-04-16 21:36:08 +00001404 }
Chris Lattner18c7f802012-02-05 02:29:43 +00001405 Out << ']';
Chris Lattnercfb5a202008-08-19 05:06:27 +00001406 return;
1407 }
Michael Ilseman407a6162012-11-15 22:34:00 +00001408
Chris Lattnerd59ae902012-01-26 02:32:04 +00001409 if (const ConstantDataArray *CA = dyn_cast<ConstantDataArray>(CV)) {
1410 // As a special case, print the array as a string if it is an array of
1411 // i8 with ConstantInt values.
1412 if (CA->isString()) {
1413 Out << "c\"";
Jonas Devlieghere7eeba252018-05-31 17:01:42 +00001414 printEscapedString(CA->getAsString(), Out);
Chris Lattnerd59ae902012-01-26 02:32:04 +00001415 Out << '"';
1416 return;
1417 }
1418
1419 Type *ETy = CA->getType()->getElementType();
1420 Out << '[';
Chris Lattner8b10b692012-01-31 03:15:40 +00001421 TypePrinter.print(ETy, Out);
1422 Out << ' ';
1423 WriteAsOperandInternal(Out, CA->getElementAsConstant(0),
1424 &TypePrinter, Machine,
1425 Context);
1426 for (unsigned i = 1, e = CA->getNumElements(); i != e; ++i) {
1427 Out << ", ";
Chris Lattnerd59ae902012-01-26 02:32:04 +00001428 TypePrinter.print(ETy, Out);
1429 Out << ' ';
Chris Lattner8b10b692012-01-31 03:15:40 +00001430 WriteAsOperandInternal(Out, CA->getElementAsConstant(i), &TypePrinter,
1431 Machine, Context);
Chris Lattnerd59ae902012-01-26 02:32:04 +00001432 }
Chris Lattner8b10b692012-01-31 03:15:40 +00001433 Out << ']';
Chris Lattnerd59ae902012-01-26 02:32:04 +00001434 return;
1435 }
1436
Chris Lattnercfb5a202008-08-19 05:06:27 +00001437 if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
Andrew Lenharth43f344a2007-01-08 18:21:30 +00001438 if (CS->getType()->isPacked())
1439 Out << '<';
Misha Brukman40c732c2004-06-04 21:11:51 +00001440 Out << '{';
Jim Laskeya3f332b2006-02-25 12:27:03 +00001441 unsigned N = CS->getNumOperands();
1442 if (N) {
Chris Lattner24233032008-08-19 04:47:09 +00001443 Out << ' ';
Chris Lattner0f7364b2009-02-28 21:26:53 +00001444 TypePrinter.print(CS->getOperand(0)->getType(), Out);
Dan Gohman8dae1382008-09-14 17:21:12 +00001445 Out << ' ';
Chris Lattner7a716ad2002-04-16 21:36:08 +00001446
Dan Gohman3bdfbf52010-07-20 23:55:01 +00001447 WriteAsOperandInternal(Out, CS->getOperand(0), &TypePrinter, Machine,
1448 Context);
Chris Lattner7a716ad2002-04-16 21:36:08 +00001449
Jim Laskeya3f332b2006-02-25 12:27:03 +00001450 for (unsigned i = 1; i < N; i++) {
Chris Lattner7a716ad2002-04-16 21:36:08 +00001451 Out << ", ";
Chris Lattner0f7364b2009-02-28 21:26:53 +00001452 TypePrinter.print(CS->getOperand(i)->getType(), Out);
Dan Gohman8dae1382008-09-14 17:21:12 +00001453 Out << ' ';
Chris Lattner7a716ad2002-04-16 21:36:08 +00001454
Dan Gohman3bdfbf52010-07-20 23:55:01 +00001455 WriteAsOperandInternal(Out, CS->getOperand(i), &TypePrinter, Machine,
1456 Context);
Chris Lattner7a716ad2002-04-16 21:36:08 +00001457 }
Dan Gohman8dae1382008-09-14 17:21:12 +00001458 Out << ' ';
Chris Lattner7a716ad2002-04-16 21:36:08 +00001459 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001460
Dan Gohman8dae1382008-09-14 17:21:12 +00001461 Out << '}';
Andrew Lenharth43f344a2007-01-08 18:21:30 +00001462 if (CS->getType()->isPacked())
1463 Out << '>';
Chris Lattnercfb5a202008-08-19 05:06:27 +00001464 return;
1465 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001466
Chris Lattnerd59ae902012-01-26 02:32:04 +00001467 if (isa<ConstantVector>(CV) || isa<ConstantDataVector>(CV)) {
1468 Type *ETy = CV->getType()->getVectorElementType();
Dan Gohman7dfa07f2009-02-11 00:25:25 +00001469 Out << '<';
Chris Lattner0f7364b2009-02-28 21:26:53 +00001470 TypePrinter.print(ETy, Out);
Dan Gohman8dae1382008-09-14 17:21:12 +00001471 Out << ' ';
Chris Lattnerd59ae902012-01-26 02:32:04 +00001472 WriteAsOperandInternal(Out, CV->getAggregateElement(0U), &TypePrinter,
1473 Machine, Context);
1474 for (unsigned i = 1, e = CV->getType()->getVectorNumElements(); i != e;++i){
Chris Lattner4667b712008-08-19 05:26:17 +00001475 Out << ", ";
Chris Lattner0f7364b2009-02-28 21:26:53 +00001476 TypePrinter.print(ETy, Out);
Dan Gohman8dae1382008-09-14 17:21:12 +00001477 Out << ' ';
Chris Lattnerd59ae902012-01-26 02:32:04 +00001478 WriteAsOperandInternal(Out, CV->getAggregateElement(i), &TypePrinter,
1479 Machine, Context);
Chris Lattnercfb5a202008-08-19 05:06:27 +00001480 }
Dan Gohman7dfa07f2009-02-11 00:25:25 +00001481 Out << '>';
Chris Lattnercfb5a202008-08-19 05:06:27 +00001482 return;
1483 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001484
Chris Lattnercfb5a202008-08-19 05:06:27 +00001485 if (isa<ConstantPointerNull>(CV)) {
Chris Lattner7a716ad2002-04-16 21:36:08 +00001486 Out << "null";
Chris Lattnercfb5a202008-08-19 05:06:27 +00001487 return;
1488 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001489
David Majnemer83fc12a2015-11-11 21:57:16 +00001490 if (isa<ConstantTokenNone>(CV)) {
1491 Out << "none";
1492 return;
1493 }
1494
Chris Lattnercfb5a202008-08-19 05:06:27 +00001495 if (isa<UndefValue>(CV)) {
Chris Lattnerb976e662004-10-16 18:08:06 +00001496 Out << "undef";
Chris Lattnercfb5a202008-08-19 05:06:27 +00001497 return;
1498 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001499
Chris Lattnercfb5a202008-08-19 05:06:27 +00001500 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Reid Spencer81dfeb32006-12-04 05:19:18 +00001501 Out << CE->getOpcodeName();
Dan Gohman59858cf2009-07-27 16:11:46 +00001502 WriteOptimizationInfo(Out, CE);
Reid Spencer81dfeb32006-12-04 05:19:18 +00001503 if (CE->isCompare())
Tim Northover3ed44cd2016-08-17 20:25:25 +00001504 Out << ' ' << CmpInst::getPredicateName(
1505 static_cast<CmpInst::Predicate>(CE->getPredicate()));
Reid Spencer81dfeb32006-12-04 05:19:18 +00001506 Out << " (";
Misha Brukmanfd939082005-04-21 23:48:37 +00001507
Peter Collingbourneca668e12016-11-10 22:34:55 +00001508 Optional<unsigned> InRangeOp;
David Blaikie5a70dd12015-03-13 18:20:45 +00001509 if (const GEPOperator *GEP = dyn_cast<GEPOperator>(CE)) {
David Blaikie0552dc72015-07-29 20:26:23 +00001510 TypePrinter.print(GEP->getSourceElementType(), Out);
David Blaikie5a70dd12015-03-13 18:20:45 +00001511 Out << ", ";
Peter Collingbourneca668e12016-11-10 22:34:55 +00001512 InRangeOp = GEP->getInRangeIndex();
1513 if (InRangeOp)
1514 ++*InRangeOp;
David Blaikie5a70dd12015-03-13 18:20:45 +00001515 }
1516
Vikram S. Adveb4dbb442002-07-14 23:14:45 +00001517 for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) {
Zachary Turner005ec662016-11-11 23:58:11 +00001518 if (InRangeOp && unsigned(OI - CE->op_begin()) == *InRangeOp)
Peter Collingbourneca668e12016-11-10 22:34:55 +00001519 Out << "inrange ";
Chris Lattner0f7364b2009-02-28 21:26:53 +00001520 TypePrinter.print((*OI)->getType(), Out);
Dan Gohman8dae1382008-09-14 17:21:12 +00001521 Out << ' ';
Dan Gohman3bdfbf52010-07-20 23:55:01 +00001522 WriteAsOperandInternal(Out, *OI, &TypePrinter, Machine, Context);
Vikram S. Adveb4dbb442002-07-14 23:14:45 +00001523 if (OI+1 != CE->op_end())
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001524 Out << ", ";
Vikram S. Adveb4dbb442002-07-14 23:14:45 +00001525 }
Misha Brukmanfd939082005-04-21 23:48:37 +00001526
Dan Gohman995be7d2008-05-31 19:12:39 +00001527 if (CE->hasIndices()) {
Jay Foadd30aa5a2011-04-13 15:22:40 +00001528 ArrayRef<unsigned> Indices = CE->getIndices();
Dan Gohman995be7d2008-05-31 19:12:39 +00001529 for (unsigned i = 0, e = Indices.size(); i != e; ++i)
1530 Out << ", " << Indices[i];
1531 }
1532
Reid Spencer3da59db2006-11-27 01:05:10 +00001533 if (CE->isCast()) {
Chris Lattner95586b82002-08-15 19:37:43 +00001534 Out << " to ";
Chris Lattner0f7364b2009-02-28 21:26:53 +00001535 TypePrinter.print(CE->getType(), Out);
Chris Lattner95586b82002-08-15 19:37:43 +00001536 }
Reid Spencer3da59db2006-11-27 01:05:10 +00001537
Misha Brukman40c732c2004-06-04 21:11:51 +00001538 Out << ')';
Chris Lattnercfb5a202008-08-19 05:06:27 +00001539 return;
Chris Lattner7a716ad2002-04-16 21:36:08 +00001540 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001541
Chris Lattnercfb5a202008-08-19 05:06:27 +00001542 Out << "<placeholder or erroneous Constant>";
Chris Lattner7a716ad2002-04-16 21:36:08 +00001543}
1544
Duncan P. N. Exon Smith120c1862015-01-12 23:45:31 +00001545static void writeMDTuple(raw_ostream &Out, const MDTuple *Node,
1546 TypePrinting *TypePrinter, SlotTracker *Machine,
1547 const Module *Context) {
Chris Lattner85b19122009-12-31 02:31:59 +00001548 Out << "!{";
1549 for (unsigned mi = 0, me = Node->getNumOperands(); mi != me; ++mi) {
Duncan P. N. Exon Smithdad20b22014-12-09 18:38:53 +00001550 const Metadata *MD = Node->getOperand(mi);
1551 if (!MD)
Chris Lattner85b19122009-12-31 02:31:59 +00001552 Out << "null";
Duncan P. N. Exon Smithdad20b22014-12-09 18:38:53 +00001553 else if (auto *MDV = dyn_cast<ValueAsMetadata>(MD)) {
1554 Value *V = MDV->getValue();
Chris Lattner85b19122009-12-31 02:31:59 +00001555 TypePrinter->print(V->getType(), Out);
1556 Out << ' ';
Duncan P. N. Exon Smithdad20b22014-12-09 18:38:53 +00001557 WriteAsOperandInternal(Out, V, TypePrinter, Machine, Context);
1558 } else {
Duncan P. N. Exon Smithdad20b22014-12-09 18:38:53 +00001559 WriteAsOperandInternal(Out, MD, TypePrinter, Machine, Context);
Chris Lattner85b19122009-12-31 02:31:59 +00001560 }
1561 if (mi + 1 != me)
1562 Out << ", ";
1563 }
Andrew Trick18801ec2011-09-30 19:48:58 +00001564
Chris Lattner85b19122009-12-31 02:31:59 +00001565 Out << "}";
1566}
1567
Duncan P. N. Exon Smithd6409622015-01-13 20:44:56 +00001568namespace {
Eugene Zelenkof1934002017-06-19 22:05:08 +00001569
Duncan P. N. Exon Smithd6409622015-01-13 20:44:56 +00001570struct FieldSeparator {
Eugene Zelenkof1934002017-06-19 22:05:08 +00001571 bool Skip = true;
Duncan P. N. Exon Smith9f8d4032015-02-21 01:02:18 +00001572 const char *Sep;
Eugene Zelenkof1934002017-06-19 22:05:08 +00001573
1574 FieldSeparator(const char *Sep = ", ") : Sep(Sep) {}
Duncan P. N. Exon Smithd6409622015-01-13 20:44:56 +00001575};
Eugene Zelenkof1934002017-06-19 22:05:08 +00001576
Duncan P. N. Exon Smithd6409622015-01-13 20:44:56 +00001577raw_ostream &operator<<(raw_ostream &OS, FieldSeparator &FS) {
1578 if (FS.Skip) {
1579 FS.Skip = false;
1580 return OS;
1581 }
Duncan P. N. Exon Smith9f8d4032015-02-21 01:02:18 +00001582 return OS << FS.Sep;
Duncan P. N. Exon Smithd6409622015-01-13 20:44:56 +00001583}
Eugene Zelenkof1934002017-06-19 22:05:08 +00001584
Duncan P. N. Exon Smith50b75d52015-03-27 00:17:42 +00001585struct MDFieldPrinter {
1586 raw_ostream &Out;
1587 FieldSeparator FS;
Eugene Zelenkof1934002017-06-19 22:05:08 +00001588 TypePrinting *TypePrinter = nullptr;
1589 SlotTracker *Machine = nullptr;
1590 const Module *Context = nullptr;
Duncan P. N. Exon Smith50b75d52015-03-27 00:17:42 +00001591
Eugene Zelenkof1934002017-06-19 22:05:08 +00001592 explicit MDFieldPrinter(raw_ostream &Out) : Out(Out) {}
Duncan P. N. Exon Smith50b75d52015-03-27 00:17:42 +00001593 MDFieldPrinter(raw_ostream &Out, TypePrinting *TypePrinter,
1594 SlotTracker *Machine, const Module *Context)
1595 : Out(Out), TypePrinter(TypePrinter), Machine(Machine), Context(Context) {
1596 }
Eugene Zelenkof1934002017-06-19 22:05:08 +00001597
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001598 void printTag(const DINode *N);
Amjad Aboud7db39802015-12-10 12:56:35 +00001599 void printMacinfoType(const DIMacroNode *N);
Scott Linder48632522018-02-12 19:45:54 +00001600 void printChecksum(const DIFile::ChecksumInfo<StringRef> &N);
Duncan P. N. Exon Smith50b75d52015-03-27 00:17:42 +00001601 void printString(StringRef Name, StringRef Value,
1602 bool ShouldSkipEmpty = true);
1603 void printMetadata(StringRef Name, const Metadata *MD,
1604 bool ShouldSkipNull = true);
1605 template <class IntTy>
1606 void printInt(StringRef Name, IntTy Int, bool ShouldSkipZero = true);
David Blaikiebf471b72016-08-24 18:29:49 +00001607 void printBool(StringRef Name, bool Value, Optional<bool> Default = None);
Leny Kholodovd9478f82016-09-06 10:46:28 +00001608 void printDIFlags(StringRef Name, DINode::DIFlags Flags);
Paul Robinsonccefd882018-11-28 21:14:32 +00001609 void printDISPFlags(StringRef Name, DISubprogram::DISPFlags Flags);
Duncan P. N. Exon Smith50b75d52015-03-27 00:17:42 +00001610 template <class IntTy, class Stringifier>
1611 void printDwarfEnum(StringRef Name, IntTy Value, Stringifier toString,
1612 bool ShouldSkipZero = true);
Adrian Prantl39bb84a2016-03-31 23:56:58 +00001613 void printEmissionKind(StringRef Name, DICompileUnit::DebugEmissionKind EK);
David Blaikiecf8a4a52018-08-16 21:29:55 +00001614 void printNameTableKind(StringRef Name,
1615 DICompileUnit::DebugNameTableKind NTK);
Duncan P. N. Exon Smith50b75d52015-03-27 00:17:42 +00001616};
Eugene Zelenkof1934002017-06-19 22:05:08 +00001617
1618} // end anonymous namespace
Duncan P. N. Exon Smithd6409622015-01-13 20:44:56 +00001619
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001620void MDFieldPrinter::printTag(const DINode *N) {
Duncan P. N. Exon Smith50b75d52015-03-27 00:17:42 +00001621 Out << FS << "tag: ";
Mehdi Aminif9bb6bc2016-10-05 05:59:29 +00001622 auto Tag = dwarf::TagString(N->getTag());
1623 if (!Tag.empty())
Duncan P. N. Exon Smith50b75d52015-03-27 00:17:42 +00001624 Out << Tag;
1625 else
1626 Out << N->getTag();
1627}
1628
Amjad Aboud7db39802015-12-10 12:56:35 +00001629void MDFieldPrinter::printMacinfoType(const DIMacroNode *N) {
1630 Out << FS << "type: ";
Mehdi Aminif9bb6bc2016-10-05 05:59:29 +00001631 auto Type = dwarf::MacinfoString(N->getMacinfoType());
1632 if (!Type.empty())
Amjad Aboud7db39802015-12-10 12:56:35 +00001633 Out << Type;
1634 else
1635 Out << N->getMacinfoType();
1636}
1637
Scott Linder48632522018-02-12 19:45:54 +00001638void MDFieldPrinter::printChecksum(
1639 const DIFile::ChecksumInfo<StringRef> &Checksum) {
1640 Out << FS << "checksumkind: " << Checksum.getKindAsString();
1641 printString("checksum", Checksum.Value, /* ShouldSkipEmpty */ false);
Amjad Aboud4e2e80b2016-12-25 10:12:09 +00001642}
1643
Duncan P. N. Exon Smith50b75d52015-03-27 00:17:42 +00001644void MDFieldPrinter::printString(StringRef Name, StringRef Value,
1645 bool ShouldSkipEmpty) {
1646 if (ShouldSkipEmpty && Value.empty())
1647 return;
1648
1649 Out << FS << Name << ": \"";
Jonas Devlieghere7eeba252018-05-31 17:01:42 +00001650 printEscapedString(Value, Out);
Duncan P. N. Exon Smith50b75d52015-03-27 00:17:42 +00001651 Out << "\"";
1652}
1653
Duncan P. N. Exon Smith8b07f1b2015-02-06 22:27:22 +00001654static void writeMetadataAsOperand(raw_ostream &Out, const Metadata *MD,
1655 TypePrinting *TypePrinter,
1656 SlotTracker *Machine,
1657 const Module *Context) {
1658 if (!MD) {
1659 Out << "null";
1660 return;
1661 }
1662 WriteAsOperandInternal(Out, MD, TypePrinter, Machine, Context);
1663}
1664
Duncan P. N. Exon Smith50b75d52015-03-27 00:17:42 +00001665void MDFieldPrinter::printMetadata(StringRef Name, const Metadata *MD,
1666 bool ShouldSkipNull) {
1667 if (ShouldSkipNull && !MD)
Duncan P. N. Exon Smith99732912015-02-28 22:16:56 +00001668 return;
1669
Duncan P. N. Exon Smith50b75d52015-03-27 00:17:42 +00001670 Out << FS << Name << ": ";
1671 writeMetadataAsOperand(Out, MD, TypePrinter, Machine, Context);
1672}
1673
1674template <class IntTy>
1675void MDFieldPrinter::printInt(StringRef Name, IntTy Int, bool ShouldSkipZero) {
1676 if (ShouldSkipZero && !Int)
1677 return;
1678
1679 Out << FS << Name << ": " << Int;
1680}
1681
David Blaikiebf471b72016-08-24 18:29:49 +00001682void MDFieldPrinter::printBool(StringRef Name, bool Value,
1683 Optional<bool> Default) {
1684 if (Default && Value == *Default)
1685 return;
Duncan P. N. Exon Smith50b75d52015-03-27 00:17:42 +00001686 Out << FS << Name << ": " << (Value ? "true" : "false");
1687}
1688
Leny Kholodovd9478f82016-09-06 10:46:28 +00001689void MDFieldPrinter::printDIFlags(StringRef Name, DINode::DIFlags Flags) {
Duncan P. N. Exon Smith50b75d52015-03-27 00:17:42 +00001690 if (!Flags)
1691 return;
1692
1693 Out << FS << Name << ": ";
1694
Leny Kholodovd9478f82016-09-06 10:46:28 +00001695 SmallVector<DINode::DIFlags, 8> SplitFlags;
1696 auto Extra = DINode::splitFlags(Flags, SplitFlags);
Duncan P. N. Exon Smith50b75d52015-03-27 00:17:42 +00001697
1698 FieldSeparator FlagsFS(" | ");
Leny Kholodovd9478f82016-09-06 10:46:28 +00001699 for (auto F : SplitFlags) {
Mehdi Aminiafe556b2016-10-01 05:57:50 +00001700 auto StringF = DINode::getFlagString(F);
1701 assert(!StringF.empty() && "Expected valid flag");
Duncan P. N. Exon Smith50b75d52015-03-27 00:17:42 +00001702 Out << FlagsFS << StringF;
1703 }
1704 if (Extra || SplitFlags.empty())
1705 Out << FlagsFS << Extra;
1706}
1707
Paul Robinsonccefd882018-11-28 21:14:32 +00001708void MDFieldPrinter::printDISPFlags(StringRef Name,
1709 DISubprogram::DISPFlags Flags) {
1710 // Always print this field, because no flags in the IR at all will be
1711 // interpreted as old-style isDefinition: true.
1712 Out << FS << Name << ": ";
1713
1714 if (!Flags) {
1715 Out << 0;
1716 return;
1717 }
1718
1719 SmallVector<DISubprogram::DISPFlags, 8> SplitFlags;
1720 auto Extra = DISubprogram::splitFlags(Flags, SplitFlags);
1721
1722 FieldSeparator FlagsFS(" | ");
1723 for (auto F : SplitFlags) {
1724 auto StringF = DISubprogram::getFlagString(F);
1725 assert(!StringF.empty() && "Expected valid flag");
1726 Out << FlagsFS << StringF;
1727 }
1728 if (Extra || SplitFlags.empty())
1729 Out << FlagsFS << Extra;
1730}
1731
Adrian Prantl39bb84a2016-03-31 23:56:58 +00001732void MDFieldPrinter::printEmissionKind(StringRef Name,
1733 DICompileUnit::DebugEmissionKind EK) {
Fangrui Songcfc86d42018-07-06 19:26:00 +00001734 Out << FS << Name << ": " << DICompileUnit::emissionKindString(EK);
Adrian Prantl39bb84a2016-03-31 23:56:58 +00001735}
1736
David Blaikiecf8a4a52018-08-16 21:29:55 +00001737void MDFieldPrinter::printNameTableKind(StringRef Name,
1738 DICompileUnit::DebugNameTableKind NTK) {
1739 if (NTK == DICompileUnit::DebugNameTableKind::Default)
1740 return;
1741 Out << FS << Name << ": " << DICompileUnit::nameTableKindString(NTK);
1742}
1743
Duncan P. N. Exon Smith50b75d52015-03-27 00:17:42 +00001744template <class IntTy, class Stringifier>
1745void MDFieldPrinter::printDwarfEnum(StringRef Name, IntTy Value,
1746 Stringifier toString, bool ShouldSkipZero) {
1747 if (!Value)
1748 return;
1749
1750 Out << FS << Name << ": ";
Mehdi Aminif9bb6bc2016-10-05 05:59:29 +00001751 auto S = toString(Value);
1752 if (!S.empty())
Duncan P. N. Exon Smith50b75d52015-03-27 00:17:42 +00001753 Out << S;
1754 else
1755 Out << Value;
Duncan P. N. Exon Smith99732912015-02-28 22:16:56 +00001756}
1757
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001758static void writeGenericDINode(raw_ostream &Out, const GenericDINode *N,
1759 TypePrinting *TypePrinter, SlotTracker *Machine,
1760 const Module *Context) {
1761 Out << "!GenericDINode(";
Duncan P. N. Exon Smith50b75d52015-03-27 00:17:42 +00001762 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1763 Printer.printTag(N);
1764 Printer.printString("header", N->getHeader());
Duncan P. N. Exon Smith6adbfa32015-02-03 21:54:14 +00001765 if (N->getNumDwarfOperands()) {
Duncan P. N. Exon Smith50b75d52015-03-27 00:17:42 +00001766 Out << Printer.FS << "operands: {";
Duncan P. N. Exon Smith6adbfa32015-02-03 21:54:14 +00001767 FieldSeparator IFS;
1768 for (auto &I : N->dwarf_operands()) {
1769 Out << IFS;
Duncan P. N. Exon Smith8b07f1b2015-02-06 22:27:22 +00001770 writeMetadataAsOperand(Out, I, TypePrinter, Machine, Context);
Duncan P. N. Exon Smith6adbfa32015-02-03 21:54:14 +00001771 }
1772 Out << "}";
1773 }
1774 Out << ")";
Duncan P. N. Exon Smith0a9f9212015-01-20 00:01:43 +00001775}
1776
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001777static void writeDILocation(raw_ostream &Out, const DILocation *DL,
Duncan P. N. Exon Smithd6409622015-01-13 20:44:56 +00001778 TypePrinting *TypePrinter, SlotTracker *Machine,
1779 const Module *Context) {
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001780 Out << "!DILocation(";
Duncan P. N. Exon Smith50b75d52015-03-27 00:17:42 +00001781 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
Duncan P. N. Exon Smithbf13dd02015-01-14 22:14:26 +00001782 // Always output the line, since 0 is a relevant and important value for it.
Duncan P. N. Exon Smith50b75d52015-03-27 00:17:42 +00001783 Printer.printInt("line", DL->getLine(), /* ShouldSkipZero */ false);
1784 Printer.printInt("column", DL->getColumn());
1785 Printer.printMetadata("scope", DL->getRawScope(), /* ShouldSkipNull */ false);
1786 Printer.printMetadata("inlinedAt", DL->getRawInlinedAt());
Calixte Denizet44db1d12018-09-20 08:53:06 +00001787 Printer.printBool("isImplicitCode", DL->isImplicitCode(),
1788 /* Default */ false);
Duncan P. N. Exon Smithd6409622015-01-13 20:44:56 +00001789 Out << ")";
1790}
1791
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001792static void writeDISubrange(raw_ostream &Out, const DISubrange *N,
Sander de Smalen959cee72018-01-24 09:56:07 +00001793 TypePrinting *TypePrinter, SlotTracker *Machine,
1794 const Module *Context) {
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001795 Out << "!DISubrange(";
Sander de Smalen959cee72018-01-24 09:56:07 +00001796 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1797 if (auto *CE = N->getCount().dyn_cast<ConstantInt*>())
1798 Printer.printInt("count", CE->getSExtValue(), /* ShouldSkipZero */ false);
1799 else
1800 Printer.printMetadata("count", N->getCount().dyn_cast<DIVariable*>(),
1801 /*ShouldSkipNull */ false);
Duncan P. N. Exon Smith329f8212015-04-07 00:39:59 +00001802 Printer.printInt("lowerBound", N->getLowerBound());
Duncan P. N. Exon Smithb984c492015-02-13 01:10:38 +00001803 Out << ")";
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001804}
Duncan P. N. Exon Smithb984c492015-02-13 01:10:38 +00001805
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001806static void writeDIEnumerator(raw_ostream &Out, const DIEnumerator *N,
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001807 TypePrinting *, SlotTracker *, const Module *) {
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001808 Out << "!DIEnumerator(";
Duncan P. N. Exon Smith50b75d52015-03-27 00:17:42 +00001809 MDFieldPrinter Printer(Out);
1810 Printer.printString("name", N->getName(), /* ShouldSkipEmpty */ false);
Momchil Velikov0c69bf42018-02-12 16:10:09 +00001811 if (N->isUnsigned()) {
1812 auto Value = static_cast<uint64_t>(N->getValue());
1813 Printer.printInt("value", Value, /* ShouldSkipZero */ false);
1814 Printer.printBool("isUnsigned", true);
1815 } else {
1816 Printer.printInt("value", N->getValue(), /* ShouldSkipZero */ false);
1817 }
Duncan P. N. Exon Smithaa7c9432015-02-13 01:14:11 +00001818 Out << ")";
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001819}
Duncan P. N. Exon Smithaa7c9432015-02-13 01:14:11 +00001820
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001821static void writeDIBasicType(raw_ostream &Out, const DIBasicType *N,
Duncan P. N. Exon Smith57b4c152015-02-13 01:14:58 +00001822 TypePrinting *, SlotTracker *, const Module *) {
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001823 Out << "!DIBasicType(";
Duncan P. N. Exon Smith50b75d52015-03-27 00:17:42 +00001824 MDFieldPrinter Printer(Out);
Duncan P. N. Exon Smith9b18dbf2015-02-28 23:21:38 +00001825 if (N->getTag() != dwarf::DW_TAG_base_type)
Duncan P. N. Exon Smith50b75d52015-03-27 00:17:42 +00001826 Printer.printTag(N);
1827 Printer.printString("name", N->getName());
1828 Printer.printInt("size", N->getSizeInBits());
1829 Printer.printInt("align", N->getAlignInBits());
1830 Printer.printDwarfEnum("encoding", N->getEncoding(),
1831 dwarf::AttributeEncodingString);
Adrian Prantlc4d19092018-08-14 19:35:34 +00001832 Printer.printDIFlags("flags", N->getFlags());
Duncan P. N. Exon Smith57b4c152015-02-13 01:14:58 +00001833 Out << ")";
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001834}
Duncan P. N. Exon Smith57b4c152015-02-13 01:14:58 +00001835
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001836static void writeDIDerivedType(raw_ostream &Out, const DIDerivedType *N,
Duncan P. N. Exon Smithdacf0002015-02-13 01:20:38 +00001837 TypePrinting *TypePrinter, SlotTracker *Machine,
1838 const Module *Context) {
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001839 Out << "!DIDerivedType(";
Duncan P. N. Exon Smith50b75d52015-03-27 00:17:42 +00001840 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1841 Printer.printTag(N);
1842 Printer.printString("name", N->getName());
Duncan P. N. Exon Smithb9433702015-03-27 23:05:04 +00001843 Printer.printMetadata("scope", N->getRawScope());
1844 Printer.printMetadata("file", N->getRawFile());
Duncan P. N. Exon Smith50b75d52015-03-27 00:17:42 +00001845 Printer.printInt("line", N->getLine());
Duncan P. N. Exon Smithb9433702015-03-27 23:05:04 +00001846 Printer.printMetadata("baseType", N->getRawBaseType(),
Duncan P. N. Exon Smith50b75d52015-03-27 00:17:42 +00001847 /* ShouldSkipNull */ false);
1848 Printer.printInt("size", N->getSizeInBits());
1849 Printer.printInt("align", N->getAlignInBits());
1850 Printer.printInt("offset", N->getOffsetInBits());
1851 Printer.printDIFlags("flags", N->getFlags());
Duncan P. N. Exon Smithb9433702015-03-27 23:05:04 +00001852 Printer.printMetadata("extraData", N->getRawExtraData());
Konstantin Zhuravlyov2cee5cc2017-03-08 23:55:44 +00001853 if (const auto &DWARFAddressSpace = N->getDWARFAddressSpace())
1854 Printer.printInt("dwarfAddressSpace", *DWARFAddressSpace,
1855 /* ShouldSkipZero */ false);
Duncan P. N. Exon Smithdacf0002015-02-13 01:20:38 +00001856 Out << ")";
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001857}
Duncan P. N. Exon Smithdacf0002015-02-13 01:20:38 +00001858
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001859static void writeDICompositeType(raw_ostream &Out, const DICompositeType *N,
Duncan P. N. Exon Smithdacf0002015-02-13 01:20:38 +00001860 TypePrinting *TypePrinter,
1861 SlotTracker *Machine, const Module *Context) {
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001862 Out << "!DICompositeType(";
Duncan P. N. Exon Smith50b75d52015-03-27 00:17:42 +00001863 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1864 Printer.printTag(N);
1865 Printer.printString("name", N->getName());
Duncan P. N. Exon Smithb9433702015-03-27 23:05:04 +00001866 Printer.printMetadata("scope", N->getRawScope());
1867 Printer.printMetadata("file", N->getRawFile());
Duncan P. N. Exon Smith50b75d52015-03-27 00:17:42 +00001868 Printer.printInt("line", N->getLine());
Duncan P. N. Exon Smithb9433702015-03-27 23:05:04 +00001869 Printer.printMetadata("baseType", N->getRawBaseType());
Duncan P. N. Exon Smith50b75d52015-03-27 00:17:42 +00001870 Printer.printInt("size", N->getSizeInBits());
1871 Printer.printInt("align", N->getAlignInBits());
1872 Printer.printInt("offset", N->getOffsetInBits());
1873 Printer.printDIFlags("flags", N->getFlags());
Duncan P. N. Exon Smithb9433702015-03-27 23:05:04 +00001874 Printer.printMetadata("elements", N->getRawElements());
Duncan P. N. Exon Smith50b75d52015-03-27 00:17:42 +00001875 Printer.printDwarfEnum("runtimeLang", N->getRuntimeLang(),
1876 dwarf::LanguageString);
Duncan P. N. Exon Smithb9433702015-03-27 23:05:04 +00001877 Printer.printMetadata("vtableHolder", N->getRawVTableHolder());
1878 Printer.printMetadata("templateParams", N->getRawTemplateParams());
Duncan P. N. Exon Smith50b75d52015-03-27 00:17:42 +00001879 Printer.printString("identifier", N->getIdentifier());
Adrian Prantl04aa6502018-02-06 23:45:59 +00001880 Printer.printMetadata("discriminator", N->getRawDiscriminator());
Duncan P. N. Exon Smithdacf0002015-02-13 01:20:38 +00001881 Out << ")";
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001882}
Duncan P. N. Exon Smithdacf0002015-02-13 01:20:38 +00001883
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001884static void writeDISubroutineType(raw_ostream &Out, const DISubroutineType *N,
Duncan P. N. Exon Smith1c092c02015-02-13 01:22:59 +00001885 TypePrinting *TypePrinter,
1886 SlotTracker *Machine, const Module *Context) {
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001887 Out << "!DISubroutineType(";
Duncan P. N. Exon Smith50b75d52015-03-27 00:17:42 +00001888 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1889 Printer.printDIFlags("flags", N->getFlags());
Reid Kleckner3d3aca22016-06-08 20:34:29 +00001890 Printer.printDwarfEnum("cc", N->getCC(), dwarf::ConventionString);
Duncan P. N. Exon Smithb9433702015-03-27 23:05:04 +00001891 Printer.printMetadata("types", N->getRawTypeArray(),
1892 /* ShouldSkipNull */ false);
Duncan P. N. Exon Smith1c092c02015-02-13 01:22:59 +00001893 Out << ")";
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001894}
Duncan P. N. Exon Smith192d9c32015-02-13 01:19:14 +00001895
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001896static void writeDIFile(raw_ostream &Out, const DIFile *N, TypePrinting *,
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001897 SlotTracker *, const Module *) {
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001898 Out << "!DIFile(";
Duncan P. N. Exon Smith50b75d52015-03-27 00:17:42 +00001899 MDFieldPrinter Printer(Out);
1900 Printer.printString("filename", N->getFilename(),
1901 /* ShouldSkipEmpty */ false);
1902 Printer.printString("directory", N->getDirectory(),
1903 /* ShouldSkipEmpty */ false);
Scott Linder48632522018-02-12 19:45:54 +00001904 // Print all values for checksum together, or not at all.
1905 if (N->getChecksum())
1906 Printer.printChecksum(*N->getChecksum());
Scott Linder5e4b5152018-02-23 23:01:06 +00001907 Printer.printString("source", N->getSource().getValueOr(StringRef()),
1908 /* ShouldSkipEmpty */ true);
Duncan P. N. Exon Smith192d9c32015-02-13 01:19:14 +00001909 Out << ")";
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001910}
Duncan P. N. Exon Smith192d9c32015-02-13 01:19:14 +00001911
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001912static void writeDICompileUnit(raw_ostream &Out, const DICompileUnit *N,
Duncan P. N. Exon Smith37742c32015-02-13 01:25:10 +00001913 TypePrinting *TypePrinter, SlotTracker *Machine,
1914 const Module *Context) {
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001915 Out << "!DICompileUnit(";
Duncan P. N. Exon Smith50b75d52015-03-27 00:17:42 +00001916 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1917 Printer.printDwarfEnum("language", N->getSourceLanguage(),
1918 dwarf::LanguageString, /* ShouldSkipZero */ false);
Duncan P. N. Exon Smithb9433702015-03-27 23:05:04 +00001919 Printer.printMetadata("file", N->getRawFile(), /* ShouldSkipNull */ false);
Duncan P. N. Exon Smith50b75d52015-03-27 00:17:42 +00001920 Printer.printString("producer", N->getProducer());
1921 Printer.printBool("isOptimized", N->isOptimized());
1922 Printer.printString("flags", N->getFlags());
1923 Printer.printInt("runtimeVersion", N->getRuntimeVersion(),
1924 /* ShouldSkipZero */ false);
1925 Printer.printString("splitDebugFilename", N->getSplitDebugFilename());
Adrian Prantl39bb84a2016-03-31 23:56:58 +00001926 Printer.printEmissionKind("emissionKind", N->getEmissionKind());
Duncan P. N. Exon Smithb9433702015-03-27 23:05:04 +00001927 Printer.printMetadata("enums", N->getRawEnumTypes());
1928 Printer.printMetadata("retainedTypes", N->getRawRetainedTypes());
Duncan P. N. Exon Smithb9433702015-03-27 23:05:04 +00001929 Printer.printMetadata("globals", N->getRawGlobalVariables());
1930 Printer.printMetadata("imports", N->getRawImportedEntities());
Amjad Aboud7db39802015-12-10 12:56:35 +00001931 Printer.printMetadata("macros", N->getRawMacros());
Adrian Prantl849c7602015-05-21 20:37:30 +00001932 Printer.printInt("dwoId", N->getDWOId());
David Blaikiebf471b72016-08-24 18:29:49 +00001933 Printer.printBool("splitDebugInlining", N->getSplitDebugInlining(), true);
Dehao Chenfe462302017-02-01 22:45:09 +00001934 Printer.printBool("debugInfoForProfiling", N->getDebugInfoForProfiling(),
1935 false);
David Blaikiecf8a4a52018-08-16 21:29:55 +00001936 Printer.printNameTableKind("nameTableKind", N->getNameTableKind());
David Blaikieecc582a2018-11-13 20:08:10 +00001937 Printer.printBool("rangesBaseAddress", N->getRangesBaseAddress(), false);
Duncan P. N. Exon Smith37742c32015-02-13 01:25:10 +00001938 Out << ")";
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001939}
Duncan P. N. Exon Smith37742c32015-02-13 01:25:10 +00001940
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001941static void writeDISubprogram(raw_ostream &Out, const DISubprogram *N,
Duncan P. N. Exon Smithed356a92015-02-13 01:26:47 +00001942 TypePrinting *TypePrinter, SlotTracker *Machine,
1943 const Module *Context) {
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001944 Out << "!DISubprogram(";
Duncan P. N. Exon Smith50b75d52015-03-27 00:17:42 +00001945 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1946 Printer.printString("name", N->getName());
1947 Printer.printString("linkageName", N->getLinkageName());
Duncan P. N. Exon Smithbd38c8d2015-03-30 16:19:15 +00001948 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
1949 Printer.printMetadata("file", N->getRawFile());
Duncan P. N. Exon Smith50b75d52015-03-27 00:17:42 +00001950 Printer.printInt("line", N->getLine());
Duncan P. N. Exon Smithbd38c8d2015-03-30 16:19:15 +00001951 Printer.printMetadata("type", N->getRawType());
Duncan P. N. Exon Smith50b75d52015-03-27 00:17:42 +00001952 Printer.printInt("scopeLine", N->getScopeLine());
Duncan P. N. Exon Smithbd38c8d2015-03-30 16:19:15 +00001953 Printer.printMetadata("containingType", N->getRawContainingType());
Peter Collingbourne4c97b942016-03-17 23:58:03 +00001954 if (N->getVirtuality() != dwarf::DW_VIRTUALITY_none ||
1955 N->getVirtualIndex() != 0)
1956 Printer.printInt("virtualIndex", N->getVirtualIndex(), false);
Reid Klecknerbd79db22016-07-01 02:41:21 +00001957 Printer.printInt("thisAdjustment", N->getThisAdjustment());
Duncan P. N. Exon Smith50b75d52015-03-27 00:17:42 +00001958 Printer.printDIFlags("flags", N->getFlags());
Paul Robinsonccefd882018-11-28 21:14:32 +00001959 Printer.printDISPFlags("spFlags", N->getSPFlags());
Adrian Prantl4eeaa0d2016-04-15 15:57:41 +00001960 Printer.printMetadata("unit", N->getRawUnit());
Duncan P. N. Exon Smithbd38c8d2015-03-30 16:19:15 +00001961 Printer.printMetadata("templateParams", N->getRawTemplateParams());
1962 Printer.printMetadata("declaration", N->getRawDeclaration());
Shiva Chena8a13bc2018-05-09 02:40:45 +00001963 Printer.printMetadata("retainedNodes", N->getRawRetainedNodes());
Adrian Prantl1bf62972017-04-26 22:56:44 +00001964 Printer.printMetadata("thrownTypes", N->getRawThrownTypes());
Duncan P. N. Exon Smithed356a92015-02-13 01:26:47 +00001965 Out << ")";
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001966}
Duncan P. N. Exon Smithed356a92015-02-13 01:26:47 +00001967
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001968static void writeDILexicalBlock(raw_ostream &Out, const DILexicalBlock *N,
1969 TypePrinting *TypePrinter, SlotTracker *Machine,
1970 const Module *Context) {
1971 Out << "!DILexicalBlock(";
Duncan P. N. Exon Smith50b75d52015-03-27 00:17:42 +00001972 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
Duncan P. N. Exon Smithd3ec0ca2015-03-30 16:37:48 +00001973 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
1974 Printer.printMetadata("file", N->getRawFile());
Duncan P. N. Exon Smith50b75d52015-03-27 00:17:42 +00001975 Printer.printInt("line", N->getLine());
1976 Printer.printInt("column", N->getColumn());
Duncan P. N. Exon Smithc7be07e2015-02-13 01:29:28 +00001977 Out << ")";
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001978}
Duncan P. N. Exon Smithc7be07e2015-02-13 01:29:28 +00001979
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001980static void writeDILexicalBlockFile(raw_ostream &Out,
1981 const DILexicalBlockFile *N,
Duncan P. N. Exon Smith246f0932015-02-13 01:30:42 +00001982 TypePrinting *TypePrinter,
1983 SlotTracker *Machine,
1984 const Module *Context) {
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001985 Out << "!DILexicalBlockFile(";
Duncan P. N. Exon Smith50b75d52015-03-27 00:17:42 +00001986 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
Duncan P. N. Exon Smithd3ec0ca2015-03-30 16:37:48 +00001987 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
1988 Printer.printMetadata("file", N->getRawFile());
Duncan P. N. Exon Smith50b75d52015-03-27 00:17:42 +00001989 Printer.printInt("discriminator", N->getDiscriminator(),
1990 /* ShouldSkipZero */ false);
Duncan P. N. Exon Smith246f0932015-02-13 01:30:42 +00001991 Out << ")";
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00001992}
Duncan P. N. Exon Smith246f0932015-02-13 01:30:42 +00001993
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001994static void writeDINamespace(raw_ostream &Out, const DINamespace *N,
Duncan P. N. Exon Smith7bd3d1d2015-02-13 01:32:09 +00001995 TypePrinting *TypePrinter, SlotTracker *Machine,
1996 const Module *Context) {
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001997 Out << "!DINamespace(";
Duncan P. N. Exon Smith50b75d52015-03-27 00:17:42 +00001998 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1999 Printer.printString("name", N->getName());
Duncan P. N. Exon Smith73802572015-03-30 17:21:38 +00002000 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
Adrian Prantl60a7c432016-11-03 19:42:02 +00002001 Printer.printBool("exportSymbols", N->getExportSymbols(), false);
Duncan P. N. Exon Smith7bd3d1d2015-02-13 01:32:09 +00002002 Out << ")";
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002003}
Duncan P. N. Exon Smith7bd3d1d2015-02-13 01:32:09 +00002004
Amjad Aboud7db39802015-12-10 12:56:35 +00002005static void writeDIMacro(raw_ostream &Out, const DIMacro *N,
2006 TypePrinting *TypePrinter, SlotTracker *Machine,
2007 const Module *Context) {
2008 Out << "!DIMacro(";
2009 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
2010 Printer.printMacinfoType(N);
2011 Printer.printInt("line", N->getLine());
2012 Printer.printString("name", N->getName());
2013 Printer.printString("value", N->getValue());
2014 Out << ")";
2015}
2016
2017static void writeDIMacroFile(raw_ostream &Out, const DIMacroFile *N,
2018 TypePrinting *TypePrinter, SlotTracker *Machine,
2019 const Module *Context) {
2020 Out << "!DIMacroFile(";
2021 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
2022 Printer.printInt("line", N->getLine());
2023 Printer.printMetadata("file", N->getRawFile(), /* ShouldSkipNull */ false);
2024 Printer.printMetadata("nodes", N->getRawElements());
2025 Out << ")";
2026}
2027
Adrian Prantl71776472015-06-29 23:03:47 +00002028static void writeDIModule(raw_ostream &Out, const DIModule *N,
2029 TypePrinting *TypePrinter, SlotTracker *Machine,
2030 const Module *Context) {
2031 Out << "!DIModule(";
2032 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
2033 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
2034 Printer.printString("name", N->getName());
2035 Printer.printString("configMacros", N->getConfigurationMacros());
2036 Printer.printString("includePath", N->getIncludePath());
2037 Printer.printString("isysroot", N->getISysRoot());
2038 Out << ")";
2039}
2040
2041
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002042static void writeDITemplateTypeParameter(raw_ostream &Out,
2043 const DITemplateTypeParameter *N,
Duncan P. N. Exon Smith8921bbc2015-02-13 01:34:32 +00002044 TypePrinting *TypePrinter,
2045 SlotTracker *Machine,
2046 const Module *Context) {
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002047 Out << "!DITemplateTypeParameter(";
Duncan P. N. Exon Smith50b75d52015-03-27 00:17:42 +00002048 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
2049 Printer.printString("name", N->getName());
Duncan P. N. Exon Smithe009b6f2015-04-06 19:03:45 +00002050 Printer.printMetadata("type", N->getRawType(), /* ShouldSkipNull */ false);
Duncan P. N. Exon Smith8921bbc2015-02-13 01:34:32 +00002051 Out << ")";
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002052}
Duncan P. N. Exon Smith8921bbc2015-02-13 01:34:32 +00002053
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002054static void writeDITemplateValueParameter(raw_ostream &Out,
2055 const DITemplateValueParameter *N,
Duncan P. N. Exon Smith8921bbc2015-02-13 01:34:32 +00002056 TypePrinting *TypePrinter,
2057 SlotTracker *Machine,
2058 const Module *Context) {
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002059 Out << "!DITemplateValueParameter(";
Duncan P. N. Exon Smith50b75d52015-03-27 00:17:42 +00002060 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
Duncan P. N. Exon Smith9b18dbf2015-02-28 23:21:38 +00002061 if (N->getTag() != dwarf::DW_TAG_template_value_parameter)
Duncan P. N. Exon Smith50b75d52015-03-27 00:17:42 +00002062 Printer.printTag(N);
2063 Printer.printString("name", N->getName());
Duncan P. N. Exon Smithe009b6f2015-04-06 19:03:45 +00002064 Printer.printMetadata("type", N->getRawType());
Duncan P. N. Exon Smith50b75d52015-03-27 00:17:42 +00002065 Printer.printMetadata("value", N->getValue(), /* ShouldSkipNull */ false);
Duncan P. N. Exon Smith8921bbc2015-02-13 01:34:32 +00002066 Out << ")";
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002067}
Duncan P. N. Exon Smith8921bbc2015-02-13 01:34:32 +00002068
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002069static void writeDIGlobalVariable(raw_ostream &Out, const DIGlobalVariable *N,
Duncan P. N. Exon Smithfbc547d2015-02-13 01:35:40 +00002070 TypePrinting *TypePrinter,
2071 SlotTracker *Machine, const Module *Context) {
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002072 Out << "!DIGlobalVariable(";
Duncan P. N. Exon Smith50b75d52015-03-27 00:17:42 +00002073 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
2074 Printer.printString("name", N->getName());
2075 Printer.printString("linkageName", N->getLinkageName());
Duncan P. N. Exon Smitha9902da2015-03-27 17:29:58 +00002076 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
2077 Printer.printMetadata("file", N->getRawFile());
Duncan P. N. Exon Smith50b75d52015-03-27 00:17:42 +00002078 Printer.printInt("line", N->getLine());
Duncan P. N. Exon Smitha9902da2015-03-27 17:29:58 +00002079 Printer.printMetadata("type", N->getRawType());
Duncan P. N. Exon Smith50b75d52015-03-27 00:17:42 +00002080 Printer.printBool("isLocal", N->isLocalToUnit());
2081 Printer.printBool("isDefinition", N->isDefinition());
Duncan P. N. Exon Smitha9902da2015-03-27 17:29:58 +00002082 Printer.printMetadata("declaration", N->getRawStaticDataMemberDeclaration());
Matthew Vossfff44e62018-10-03 18:44:53 +00002083 Printer.printMetadata("templateParams", N->getRawTemplateParams());
Victor Leschuke69c4592016-10-20 00:13:12 +00002084 Printer.printInt("align", N->getAlignInBits());
Duncan P. N. Exon Smithfbc547d2015-02-13 01:35:40 +00002085 Out << ")";
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002086}
Duncan P. N. Exon Smithfbc547d2015-02-13 01:35:40 +00002087
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002088static void writeDILocalVariable(raw_ostream &Out, const DILocalVariable *N,
Duncan P. N. Exon Smitha342d822015-02-13 01:39:44 +00002089 TypePrinting *TypePrinter,
2090 SlotTracker *Machine, const Module *Context) {
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002091 Out << "!DILocalVariable(";
Duncan P. N. Exon Smith50b75d52015-03-27 00:17:42 +00002092 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
Duncan P. N. Exon Smith50b75d52015-03-27 00:17:42 +00002093 Printer.printString("name", N->getName());
Duncan P. N. Exon Smithbf2040f2015-07-31 18:58:39 +00002094 Printer.printInt("arg", N->getArg());
Duncan P. N. Exon Smitha9902da2015-03-27 17:29:58 +00002095 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
2096 Printer.printMetadata("file", N->getRawFile());
Duncan P. N. Exon Smith50b75d52015-03-27 00:17:42 +00002097 Printer.printInt("line", N->getLine());
Duncan P. N. Exon Smitha9902da2015-03-27 17:29:58 +00002098 Printer.printMetadata("type", N->getRawType());
Duncan P. N. Exon Smith50b75d52015-03-27 00:17:42 +00002099 Printer.printDIFlags("flags", N->getFlags());
Victor Leschuke69c4592016-10-20 00:13:12 +00002100 Printer.printInt("align", N->getAlignInBits());
Duncan P. N. Exon Smitha342d822015-02-13 01:39:44 +00002101 Out << ")";
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002102}
Duncan P. N. Exon Smitha342d822015-02-13 01:39:44 +00002103
Shiva Chena8a13bc2018-05-09 02:40:45 +00002104static void writeDILabel(raw_ostream &Out, const DILabel *N,
2105 TypePrinting *TypePrinter,
2106 SlotTracker *Machine, const Module *Context) {
2107 Out << "!DILabel(";
2108 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
2109 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
2110 Printer.printString("name", N->getName());
2111 Printer.printMetadata("file", N->getRawFile());
2112 Printer.printInt("line", N->getLine());
2113 Out << ")";
2114}
2115
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002116static void writeDIExpression(raw_ostream &Out, const DIExpression *N,
Duncan P. N. Exon Smitha034e072015-02-13 01:42:09 +00002117 TypePrinting *TypePrinter, SlotTracker *Machine,
2118 const Module *Context) {
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002119 Out << "!DIExpression(";
Duncan P. N. Exon Smitha034e072015-02-13 01:42:09 +00002120 FieldSeparator FS;
2121 if (N->isValid()) {
2122 for (auto I = N->expr_op_begin(), E = N->expr_op_end(); I != E; ++I) {
Mehdi Aminif9bb6bc2016-10-05 05:59:29 +00002123 auto OpStr = dwarf::OperationEncodingString(I->getOp());
2124 assert(!OpStr.empty() && "Expected valid opcode");
Duncan P. N. Exon Smitha034e072015-02-13 01:42:09 +00002125
2126 Out << FS << OpStr;
2127 for (unsigned A = 0, AE = I->getNumArgs(); A != AE; ++A)
2128 Out << FS << I->getArg(A);
2129 }
2130 } else {
2131 for (const auto &I : N->getElements())
2132 Out << FS << I;
2133 }
2134 Out << ")";
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002135}
Duncan P. N. Exon Smitha034e072015-02-13 01:42:09 +00002136
Adrian Prantl7b500b42016-12-20 02:09:43 +00002137static void writeDIGlobalVariableExpression(raw_ostream &Out,
2138 const DIGlobalVariableExpression *N,
2139 TypePrinting *TypePrinter,
2140 SlotTracker *Machine,
2141 const Module *Context) {
2142 Out << "!DIGlobalVariableExpression(";
2143 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
2144 Printer.printMetadata("var", N->getVariable());
2145 Printer.printMetadata("expr", N->getExpression());
2146 Out << ")";
2147}
2148
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002149static void writeDIObjCProperty(raw_ostream &Out, const DIObjCProperty *N,
Duncan P. N. Exon Smith3bfa8d02015-02-13 01:43:22 +00002150 TypePrinting *TypePrinter, SlotTracker *Machine,
2151 const Module *Context) {
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002152 Out << "!DIObjCProperty(";
Duncan P. N. Exon Smith50b75d52015-03-27 00:17:42 +00002153 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
2154 Printer.printString("name", N->getName());
Duncan P. N. Exon Smith73802572015-03-30 17:21:38 +00002155 Printer.printMetadata("file", N->getRawFile());
Duncan P. N. Exon Smith50b75d52015-03-27 00:17:42 +00002156 Printer.printInt("line", N->getLine());
2157 Printer.printString("setter", N->getSetterName());
2158 Printer.printString("getter", N->getGetterName());
2159 Printer.printInt("attributes", N->getAttributes());
Duncan P. N. Exon Smith73802572015-03-30 17:21:38 +00002160 Printer.printMetadata("type", N->getRawType());
Duncan P. N. Exon Smith3bfa8d02015-02-13 01:43:22 +00002161 Out << ")";
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002162}
Duncan P. N. Exon Smith3bfa8d02015-02-13 01:43:22 +00002163
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002164static void writeDIImportedEntity(raw_ostream &Out, const DIImportedEntity *N,
Duncan P. N. Exon Smith6a390dc2015-02-13 01:46:02 +00002165 TypePrinting *TypePrinter,
2166 SlotTracker *Machine, const Module *Context) {
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00002167 Out << "!DIImportedEntity(";
Duncan P. N. Exon Smith50b75d52015-03-27 00:17:42 +00002168 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
2169 Printer.printTag(N);
2170 Printer.printString("name", N->getName());
Duncan P. N. Exon Smith73802572015-03-30 17:21:38 +00002171 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
2172 Printer.printMetadata("entity", N->getRawEntity());
Adrian Prantl9563b5a2017-07-19 00:09:54 +00002173 Printer.printMetadata("file", N->getRawFile());
Duncan P. N. Exon Smith50b75d52015-03-27 00:17:42 +00002174 Printer.printInt("line", N->getLine());
Duncan P. N. Exon Smith6a390dc2015-02-13 01:46:02 +00002175 Out << ")";
Duncan P. N. Exon Smith14fcfef2015-02-10 00:52:32 +00002176}
2177
Duncan P. N. Exon Smith120c1862015-01-12 23:45:31 +00002178static void WriteMDNodeBodyInternal(raw_ostream &Out, const MDNode *Node,
2179 TypePrinting *TypePrinter,
2180 SlotTracker *Machine,
2181 const Module *Context) {
Duncan P. N. Exon Smithb0617862015-01-19 23:13:14 +00002182 if (Node->isDistinct())
Duncan P. N. Exon Smith120c1862015-01-12 23:45:31 +00002183 Out << "distinct ";
Duncan P. N. Exon Smithbee74a32015-03-16 21:21:10 +00002184 else if (Node->isTemporary())
2185 Out << "<temporary!> "; // Handle broken code.
Duncan P. N. Exon Smith120c1862015-01-12 23:45:31 +00002186
Duncan P. N. Exon Smithb0617862015-01-19 23:13:14 +00002187 switch (Node->getMetadataID()) {
Duncan P. N. Exon Smith120c1862015-01-12 23:45:31 +00002188 default:
2189 llvm_unreachable("Expected uniquable MDNode");
Duncan P. N. Exon Smithb0617862015-01-19 23:13:14 +00002190#define HANDLE_MDNODE_LEAF(CLASS) \
Duncan P. N. Exon Smith120c1862015-01-12 23:45:31 +00002191 case Metadata::CLASS##Kind: \
Duncan P. N. Exon Smithb0617862015-01-19 23:13:14 +00002192 write##CLASS(Out, cast<CLASS>(Node), TypePrinter, Machine, Context); \
Duncan P. N. Exon Smith120c1862015-01-12 23:45:31 +00002193 break;
2194#include "llvm/IR/Metadata.def"
2195 }
2196}
2197
Chandler Carruth560e3952014-01-09 02:29:41 +00002198// Full implementation of printing a Value as an operand with support for
2199// TypePrinting, etc.
Dan Gohman1220e102009-08-12 20:56:03 +00002200static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
Dan Gohmand6c0f652009-08-13 15:27:57 +00002201 TypePrinting *TypePrinter,
Dan Gohman3bdfbf52010-07-20 23:55:01 +00002202 SlotTracker *Machine,
2203 const Module *Context) {
Chris Lattnerc97536e2008-08-17 04:40:13 +00002204 if (V->hasName()) {
2205 PrintLLVMName(Out, V);
2206 return;
2207 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002208
Chris Lattnerc97536e2008-08-17 04:40:13 +00002209 const Constant *CV = dyn_cast<Constant>(V);
2210 if (CV && !isa<GlobalValue>(CV)) {
Dan Gohmand6c0f652009-08-13 15:27:57 +00002211 assert(TypePrinter && "Constants require TypePrinting!");
Dan Gohman3bdfbf52010-07-20 23:55:01 +00002212 WriteConstantInternal(Out, CV, *TypePrinter, Machine, Context);
Chris Lattnercfb5a202008-08-19 05:06:27 +00002213 return;
2214 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002215
Chris Lattnercfb5a202008-08-19 05:06:27 +00002216 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
Chris Lattnerc97536e2008-08-17 04:40:13 +00002217 Out << "asm ";
2218 if (IA->hasSideEffects())
2219 Out << "sideeffect ";
Dale Johannesen8ba2d5b2009-10-21 23:28:00 +00002220 if (IA->isAlignStack())
2221 Out << "alignstack ";
Chad Rosier581600b2012-09-05 19:00:49 +00002222 // We don't emit the AD_ATT dialect as it's the assumed default.
2223 if (IA->getDialect() == InlineAsm::AD_Intel)
2224 Out << "inteldialect ";
Chris Lattnerc97536e2008-08-17 04:40:13 +00002225 Out << '"';
Jonas Devlieghere7eeba252018-05-31 17:01:42 +00002226 printEscapedString(IA->getAsmString(), Out);
Chris Lattnerc97536e2008-08-17 04:40:13 +00002227 Out << "\", \"";
Jonas Devlieghere7eeba252018-05-31 17:01:42 +00002228 printEscapedString(IA->getConstraintString(), Out);
Chris Lattnerc97536e2008-08-17 04:40:13 +00002229 Out << '"';
Chris Lattnercfb5a202008-08-19 05:06:27 +00002230 return;
2231 }
Devang Patele54abc92009-07-22 17:43:22 +00002232
Duncan P. N. Exon Smithdad20b22014-12-09 18:38:53 +00002233 if (auto *MD = dyn_cast<MetadataAsValue>(V)) {
2234 WriteAsOperandInternal(Out, MD->getMetadata(), TypePrinter, Machine,
2235 Context, /* FromValue */ true);
Devang Patele54abc92009-07-22 17:43:22 +00002236 return;
2237 }
2238
Chris Lattnercfb5a202008-08-19 05:06:27 +00002239 char Prefix = '%';
2240 int Slot;
Chris Lattnerfb5179a2011-08-03 06:15:41 +00002241 // If we have a SlotTracker, use it.
Chris Lattnercfb5a202008-08-19 05:06:27 +00002242 if (Machine) {
2243 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
2244 Slot = Machine->getGlobalSlot(GV);
2245 Prefix = '@';
2246 } else {
2247 Slot = Machine->getLocalSlot(V);
Andrew Trick18801ec2011-09-30 19:48:58 +00002248
Chris Lattnerfb5179a2011-08-03 06:15:41 +00002249 // If the local value didn't succeed, then we may be referring to a value
2250 // from a different function. Translate it, as this can happen when using
2251 // address of blocks.
2252 if (Slot == -1)
2253 if ((Machine = createSlotTracker(V))) {
2254 Slot = Machine->getLocalSlot(V);
2255 delete Machine;
2256 }
Chris Lattnercfb5a202008-08-19 05:06:27 +00002257 }
Chris Lattnerfb5179a2011-08-03 06:15:41 +00002258 } else if ((Machine = createSlotTracker(V))) {
2259 // Otherwise, create one to get the # and then destroy it.
2260 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
2261 Slot = Machine->getGlobalSlot(GV);
2262 Prefix = '@';
Chris Lattner80cd1152006-01-25 22:26:05 +00002263 } else {
Chris Lattnerfb5179a2011-08-03 06:15:41 +00002264 Slot = Machine->getLocalSlot(V);
Chris Lattner7a716ad2002-04-16 21:36:08 +00002265 }
Chris Lattnerfb5179a2011-08-03 06:15:41 +00002266 delete Machine;
Craig Topperec0f0bc2014-04-09 06:08:46 +00002267 Machine = nullptr;
Chris Lattnerfb5179a2011-08-03 06:15:41 +00002268 } else {
2269 Slot = -1;
Chris Lattner7a716ad2002-04-16 21:36:08 +00002270 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002271
Chris Lattnercfb5a202008-08-19 05:06:27 +00002272 if (Slot != -1)
2273 Out << Prefix << Slot;
2274 else
2275 Out << "<badref>";
Chris Lattner7a716ad2002-04-16 21:36:08 +00002276}
2277
Duncan P. N. Exon Smithdad20b22014-12-09 18:38:53 +00002278static void WriteAsOperandInternal(raw_ostream &Out, const Metadata *MD,
2279 TypePrinting *TypePrinter,
2280 SlotTracker *Machine, const Module *Context,
2281 bool FromValue) {
Reid Klecknera5b2af02017-08-23 20:31:27 +00002282 // Write DIExpressions inline when used as a value. Improves readability of
2283 // debug info intrinsics.
2284 if (const DIExpression *Expr = dyn_cast<DIExpression>(MD)) {
2285 writeDIExpression(Out, Expr, TypePrinter, Machine, Context);
2286 return;
2287 }
2288
Duncan P. N. Exon Smithdad20b22014-12-09 18:38:53 +00002289 if (const MDNode *N = dyn_cast<MDNode>(MD)) {
Duncan P. N. Exon Smith4f95bcc2015-06-27 00:15:32 +00002290 std::unique_ptr<SlotTracker> MachineStorage;
2291 if (!Machine) {
2292 MachineStorage = make_unique<SlotTracker>(Context);
2293 Machine = MachineStorage.get();
2294 }
Duncan P. N. Exon Smithdad20b22014-12-09 18:38:53 +00002295 int Slot = Machine->getMetadataSlot(N);
Daniel Sandersa3836a52018-12-13 14:25:27 +00002296 if (Slot == -1) {
2297 if (const DILocation *Loc = dyn_cast<DILocation>(N)) {
2298 writeDILocation(Out, Loc, TypePrinter, Machine, Context);
2299 return;
2300 }
Duncan P. N. Exon Smith2ebd1ef2014-12-16 07:09:37 +00002301 // Give the pointer value instead of "badref", since this comes up all
2302 // the time when debugging.
2303 Out << "<" << N << ">";
Daniel Sandersa3836a52018-12-13 14:25:27 +00002304 } else
Duncan P. N. Exon Smithdad20b22014-12-09 18:38:53 +00002305 Out << '!' << Slot;
2306 return;
2307 }
2308
2309 if (const MDString *MDS = dyn_cast<MDString>(MD)) {
2310 Out << "!\"";
Jonas Devlieghere7eeba252018-05-31 17:01:42 +00002311 printEscapedString(MDS->getString(), Out);
Duncan P. N. Exon Smithdad20b22014-12-09 18:38:53 +00002312 Out << '"';
2313 return;
2314 }
2315
2316 auto *V = cast<ValueAsMetadata>(MD);
2317 assert(TypePrinter && "TypePrinter required for metadata values");
2318 assert((FromValue || !isa<LocalAsMetadata>(V)) &&
2319 "Unexpected function-local metadata outside of value argument");
2320
Duncan P. N. Exon Smithdad20b22014-12-09 18:38:53 +00002321 TypePrinter->print(V->getValue()->getType(), Out);
2322 Out << ' ';
2323 WriteAsOperandInternal(Out, V->getValue(), TypePrinter, Machine, Context);
Duncan P. N. Exon Smithdad20b22014-12-09 18:38:53 +00002324}
2325
Benjamin Kramer93920f02015-03-17 19:53:41 +00002326namespace {
Eugene Zelenkof1934002017-06-19 22:05:08 +00002327
Benjamin Kramer93920f02015-03-17 19:53:41 +00002328class AssemblyWriter {
2329 formatted_raw_ostream &Out;
Teresa Johnsona9a21472018-05-26 02:34:13 +00002330 const Module *TheModule = nullptr;
2331 const ModuleSummaryIndex *TheIndex = nullptr;
Duncan P. N. Exon Smith9a61a422015-06-26 22:04:20 +00002332 std::unique_ptr<SlotTracker> SlotTrackerStorage;
Benjamin Kramer93920f02015-03-17 19:53:41 +00002333 SlotTracker &Machine;
2334 TypePrinting TypePrinter;
Teresa Johnsona9a21472018-05-26 02:34:13 +00002335 AssemblyAnnotationWriter *AnnotationWriter = nullptr;
Benjamin Kramer93920f02015-03-17 19:53:41 +00002336 SetVector<const Comdat *> Comdats;
Justin Bogner0adaaa32015-09-27 22:38:50 +00002337 bool IsForDebug;
Duncan P. N. Exon Smithc9d5dea2015-04-15 01:36:30 +00002338 bool ShouldPreserveUseListOrder;
Benjamin Kramer93920f02015-03-17 19:53:41 +00002339 UseListOrderStack UseListOrders;
Duncan P. N. Exon Smithc8be2442015-04-24 21:03:05 +00002340 SmallVector<StringRef, 8> MDNames;
Konstantin Zhuravlyov8f856852017-07-11 22:23:00 +00002341 /// Synchronization scope names registered with LLVMContext.
2342 SmallVector<StringRef, 8> SSNs;
Teresa Johnsona9a21472018-05-26 02:34:13 +00002343 DenseMap<const GlobalValueSummary *, GlobalValue::GUID> SummaryToGUIDMap;
Benjamin Kramer93920f02015-03-17 19:53:41 +00002344
2345public:
2346 /// Construct an AssemblyWriter with an external SlotTracker
Duncan P. N. Exon Smithc9d5dea2015-04-15 01:36:30 +00002347 AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac, const Module *M,
Justin Bogner0adaaa32015-09-27 22:38:50 +00002348 AssemblyAnnotationWriter *AAW, bool IsForDebug,
Duncan P. N. Exon Smithc9d5dea2015-04-15 01:36:30 +00002349 bool ShouldPreserveUseListOrder = false);
Benjamin Kramer93920f02015-03-17 19:53:41 +00002350
Teresa Johnsona9a21472018-05-26 02:34:13 +00002351 AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac,
2352 const ModuleSummaryIndex *Index, bool IsForDebug);
2353
Benjamin Kramer93920f02015-03-17 19:53:41 +00002354 void printMDNodeBody(const MDNode *MD);
2355 void printNamedMDNode(const NamedMDNode *NMD);
2356
2357 void printModule(const Module *M);
2358
2359 void writeOperand(const Value *Op, bool PrintType);
Reid Kleckner331b9af2017-04-28 18:37:16 +00002360 void writeParamOperand(const Value *Operand, AttributeSet Attrs);
Chandler Carruth9f32dc92019-01-07 07:31:49 +00002361 void writeOperandBundles(const CallBase *Call);
Konstantin Zhuravlyov8f856852017-07-11 22:23:00 +00002362 void writeSyncScope(const LLVMContext &Context,
2363 SyncScope::ID SSID);
2364 void writeAtomic(const LLVMContext &Context,
2365 AtomicOrdering Ordering,
2366 SyncScope::ID SSID);
2367 void writeAtomicCmpXchg(const LLVMContext &Context,
2368 AtomicOrdering SuccessOrdering,
Benjamin Kramer93920f02015-03-17 19:53:41 +00002369 AtomicOrdering FailureOrdering,
Konstantin Zhuravlyov8f856852017-07-11 22:23:00 +00002370 SyncScope::ID SSID);
Benjamin Kramer93920f02015-03-17 19:53:41 +00002371
2372 void writeAllMDNodes();
2373 void writeMDNode(unsigned Slot, const MDNode *Node);
2374 void writeAllAttributeGroups();
2375
2376 void printTypeIdentities();
2377 void printGlobal(const GlobalVariable *GV);
Dmitry Polukhin51a06a22016-04-05 08:47:51 +00002378 void printIndirectSymbol(const GlobalIndirectSymbol *GIS);
Benjamin Kramer93920f02015-03-17 19:53:41 +00002379 void printComdat(const Comdat *C);
2380 void printFunction(const Function *F);
Reid Kleckner331b9af2017-04-28 18:37:16 +00002381 void printArgument(const Argument *FA, AttributeSet Attrs);
Benjamin Kramer93920f02015-03-17 19:53:41 +00002382 void printBasicBlock(const BasicBlock *BB);
2383 void printInstructionLine(const Instruction &I);
2384 void printInstruction(const Instruction &I);
2385
2386 void printUseListOrder(const UseListOrder &Order);
2387 void printUseLists(const Function *F);
2388
Teresa Johnsona9a21472018-05-26 02:34:13 +00002389 void printModuleSummaryIndex();
2390 void printSummaryInfo(unsigned Slot, const ValueInfo &VI);
2391 void printSummary(const GlobalValueSummary &Summary);
2392 void printAliasSummary(const AliasSummary *AS);
2393 void printGlobalVarSummary(const GlobalVarSummary *GS);
2394 void printFunctionSummary(const FunctionSummary *FS);
2395 void printTypeIdSummary(const TypeIdSummary &TIS);
2396 void printTypeTestResolution(const TypeTestResolution &TTRes);
2397 void printArgs(const std::vector<uint64_t> &Args);
2398 void printWPDRes(const WholeProgramDevirtResolution &WPDRes);
2399 void printTypeIdInfo(const FunctionSummary::TypeIdInfo &TIDInfo);
2400 void printVFuncId(const FunctionSummary::VFuncId VFId);
2401 void
2402 printNonConstVCalls(const std::vector<FunctionSummary::VFuncId> VCallList,
2403 const char *Tag);
2404 void
2405 printConstVCalls(const std::vector<FunctionSummary::ConstVCall> VCallList,
2406 const char *Tag);
2407
Benjamin Kramer93920f02015-03-17 19:53:41 +00002408private:
Adrian Prantl26b584c2018-05-01 15:54:18 +00002409 /// Print out metadata attachments.
Duncan P. N. Exon Smith61317d52015-04-24 20:59:52 +00002410 void printMetadataAttachments(
Duncan P. N. Exon Smith3be41472015-04-24 21:06:21 +00002411 const SmallVectorImpl<std::pair<unsigned, MDNode *>> &MDs,
2412 StringRef Separator);
Duncan P. N. Exon Smith61317d52015-04-24 20:59:52 +00002413
Benjamin Kramer93920f02015-03-17 19:53:41 +00002414 // printInfoComment - Print a little comment after the instruction indicating
2415 // which slot it occupies.
2416 void printInfoComment(const Value &V);
Igor Laevskya5f2faf2015-05-05 13:20:42 +00002417
2418 // printGCRelocateComment - print comment after call to the gc.relocate
2419 // intrinsic indicating base and derived pointer names.
Manuel Jacob397864c2016-01-05 04:03:00 +00002420 void printGCRelocateComment(const GCRelocateInst &Relocate);
Benjamin Kramer93920f02015-03-17 19:53:41 +00002421};
Eugene Zelenkof1934002017-06-19 22:05:08 +00002422
2423} // end anonymous namespace
Benjamin Kramer93920f02015-03-17 19:53:41 +00002424
Justin Bogner8559c492015-09-02 22:46:15 +00002425AssemblyWriter::AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac,
2426 const Module *M, AssemblyAnnotationWriter *AAW,
Justin Bogner0adaaa32015-09-27 22:38:50 +00002427 bool IsForDebug, bool ShouldPreserveUseListOrder)
Roman Tereshin238a3382018-03-22 21:29:07 +00002428 : Out(o), TheModule(M), Machine(Mac), TypePrinter(M), AnnotationWriter(AAW),
Justin Bogner0adaaa32015-09-27 22:38:50 +00002429 IsForDebug(IsForDebug),
Justin Bogner8559c492015-09-02 22:46:15 +00002430 ShouldPreserveUseListOrder(ShouldPreserveUseListOrder) {
David Majnemerc8a11692014-06-27 18:19:56 +00002431 if (!TheModule)
2432 return;
Peter Collingbourne27725842016-06-22 20:29:42 +00002433 for (const GlobalObject &GO : TheModule->global_objects())
2434 if (const Comdat *C = GO.getComdat())
David Majnemerc8a11692014-06-27 18:19:56 +00002435 Comdats.insert(C);
Daniel Malead0fef322013-05-08 20:38:31 +00002436}
Chris Lattnerd8c2e422001-07-12 23:35:26 +00002437
Teresa Johnsona9a21472018-05-26 02:34:13 +00002438AssemblyWriter::AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac,
2439 const ModuleSummaryIndex *Index, bool IsForDebug)
2440 : Out(o), TheIndex(Index), Machine(Mac), TypePrinter(/*Module=*/nullptr),
2441 IsForDebug(IsForDebug), ShouldPreserveUseListOrder(false) {}
2442
Chris Lattner2fcfdb72006-12-06 06:24:27 +00002443void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType) {
Craig Topperec0f0bc2014-04-09 06:08:46 +00002444 if (!Operand) {
Chris Lattneraab18202005-02-24 16:58:29 +00002445 Out << "<null operand!>";
Chris Lattnerc65b72c2009-12-31 02:33:14 +00002446 return;
Chris Lattneraab18202005-02-24 16:58:29 +00002447 }
Chris Lattnerc65b72c2009-12-31 02:33:14 +00002448 if (PrintType) {
2449 TypePrinter.print(Operand->getType(), Out);
2450 Out << ' ';
2451 }
Dan Gohman3bdfbf52010-07-20 23:55:01 +00002452 WriteAsOperandInternal(Out, Operand, &TypePrinter, &Machine, TheModule);
Chris Lattner00950542001-06-06 20:29:01 +00002453}
2454
Konstantin Zhuravlyov8f856852017-07-11 22:23:00 +00002455void AssemblyWriter::writeSyncScope(const LLVMContext &Context,
2456 SyncScope::ID SSID) {
2457 switch (SSID) {
2458 case SyncScope::System: {
2459 break;
2460 }
2461 default: {
2462 if (SSNs.empty())
2463 Context.getSyncScopeNames(SSNs);
2464
2465 Out << " syncscope(\"";
Jonas Devlieghere7eeba252018-05-31 17:01:42 +00002466 printEscapedString(SSNs[SSID], Out);
Konstantin Zhuravlyov8f856852017-07-11 22:23:00 +00002467 Out << "\")";
2468 break;
2469 }
2470 }
2471}
2472
2473void AssemblyWriter::writeAtomic(const LLVMContext &Context,
2474 AtomicOrdering Ordering,
2475 SyncScope::ID SSID) {
JF Bastienb36d1a82016-04-06 21:19:33 +00002476 if (Ordering == AtomicOrdering::NotAtomic)
Eli Friedman47f35132011-07-25 23:16:38 +00002477 return;
2478
Konstantin Zhuravlyov8f856852017-07-11 22:23:00 +00002479 writeSyncScope(Context, SSID);
JF Bastienb36d1a82016-04-06 21:19:33 +00002480 Out << " " << toIRString(Ordering);
Eli Friedman47f35132011-07-25 23:16:38 +00002481}
2482
Konstantin Zhuravlyov8f856852017-07-11 22:23:00 +00002483void AssemblyWriter::writeAtomicCmpXchg(const LLVMContext &Context,
2484 AtomicOrdering SuccessOrdering,
Tim Northoverca396e32014-03-11 10:48:52 +00002485 AtomicOrdering FailureOrdering,
Konstantin Zhuravlyov8f856852017-07-11 22:23:00 +00002486 SyncScope::ID SSID) {
JF Bastienb36d1a82016-04-06 21:19:33 +00002487 assert(SuccessOrdering != AtomicOrdering::NotAtomic &&
2488 FailureOrdering != AtomicOrdering::NotAtomic);
Tim Northoverca396e32014-03-11 10:48:52 +00002489
Konstantin Zhuravlyov8f856852017-07-11 22:23:00 +00002490 writeSyncScope(Context, SSID);
JF Bastienb36d1a82016-04-06 21:19:33 +00002491 Out << " " << toIRString(SuccessOrdering);
2492 Out << " " << toIRString(FailureOrdering);
Tim Northoverca396e32014-03-11 10:48:52 +00002493}
2494
Daniel Dunbara279bc32009-09-20 02:20:51 +00002495void AssemblyWriter::writeParamOperand(const Value *Operand,
Reid Kleckner331b9af2017-04-28 18:37:16 +00002496 AttributeSet Attrs) {
Craig Topperec0f0bc2014-04-09 06:08:46 +00002497 if (!Operand) {
Duncan Sandsdc024672007-11-27 13:23:08 +00002498 Out << "<null operand!>";
Chris Lattnerc65b72c2009-12-31 02:33:14 +00002499 return;
Duncan Sandsdc024672007-11-27 13:23:08 +00002500 }
Chris Lattnerc65b72c2009-12-31 02:33:14 +00002501
2502 // Print the type
2503 TypePrinter.print(Operand->getType(), Out);
2504 // Print parameter attributes list
Reid Kleckner331b9af2017-04-28 18:37:16 +00002505 if (Attrs.hasAttributes())
2506 Out << ' ' << Attrs.getAsString();
Chris Lattnerc65b72c2009-12-31 02:33:14 +00002507 Out << ' ';
2508 // Print the operand
Dan Gohman3bdfbf52010-07-20 23:55:01 +00002509 WriteAsOperandInternal(Out, Operand, &TypePrinter, &Machine, TheModule);
Duncan Sandsdc024672007-11-27 13:23:08 +00002510}
Chris Lattner00950542001-06-06 20:29:01 +00002511
Chandler Carruth9f32dc92019-01-07 07:31:49 +00002512void AssemblyWriter::writeOperandBundles(const CallBase *Call) {
2513 if (!Call->hasOperandBundles())
Sanjoy Dasf70eb722015-09-24 23:34:52 +00002514 return;
2515
2516 Out << " [ ";
2517
2518 bool FirstBundle = true;
Chandler Carruth9f32dc92019-01-07 07:31:49 +00002519 for (unsigned i = 0, e = Call->getNumOperandBundles(); i != e; ++i) {
2520 OperandBundleUse BU = Call->getOperandBundleAt(i);
Sanjoy Dasf70eb722015-09-24 23:34:52 +00002521
2522 if (!FirstBundle)
2523 Out << ", ";
2524 FirstBundle = false;
2525
2526 Out << '"';
Jonas Devlieghere7eeba252018-05-31 17:01:42 +00002527 printEscapedString(BU.getTagName(), Out);
Sanjoy Dasf70eb722015-09-24 23:34:52 +00002528 Out << '"';
2529
2530 Out << '(';
2531
2532 bool FirstInput = true;
2533 for (const auto &Input : BU.Inputs) {
2534 if (!FirstInput)
2535 Out << ", ";
2536 FirstInput = false;
2537
2538 TypePrinter.print(Input->getType(), Out);
2539 Out << " ";
2540 WriteAsOperandInternal(Out, Input, &TypePrinter, &Machine, TheModule);
2541 }
2542
2543 Out << ')';
2544 }
2545
2546 Out << " ]";
2547}
2548
Chris Lattnerc1824992001-10-29 16:05:51 +00002549void AssemblyWriter::printModule(const Module *M) {
Teresa Johnsona9961f32018-07-03 15:52:57 +00002550 Machine.initializeIfNeeded();
Bill Wendlingb29ce262013-02-11 08:43:33 +00002551
Duncan P. N. Exon Smithc9d5dea2015-04-15 01:36:30 +00002552 if (ShouldPreserveUseListOrder)
Duncan P. N. Exon Smith78388182014-08-19 21:30:15 +00002553 UseListOrders = predictUseListOrder(M);
2554
Chris Lattner31ab1b32005-03-02 23:12:40 +00002555 if (!M->getModuleIdentifier().empty() &&
Misha Brukmanfd939082005-04-21 23:48:37 +00002556 // Don't print the ID if it will start a new line (which would
Chris Lattner31ab1b32005-03-02 23:12:40 +00002557 // require a comment char before it).
2558 M->getModuleIdentifier().find('\n') == std::string::npos)
2559 Out << "; ModuleID = '" << M->getModuleIdentifier() << "'\n";
2560
Teresa Johnson2dec5fc2016-03-30 18:15:08 +00002561 if (!M->getSourceFileName().empty()) {
Teresa Johnsond47ed922016-03-30 22:17:28 +00002562 Out << "source_filename = \"";
Jonas Devlieghere7eeba252018-05-31 17:01:42 +00002563 printEscapedString(M->getSourceFileName(), Out);
Teresa Johnsond47ed922016-03-30 22:17:28 +00002564 Out << "\"\n";
Teresa Johnson2dec5fc2016-03-30 18:15:08 +00002565 }
2566
Rafael Espindolaaab87fe2014-02-25 20:01:08 +00002567 const std::string &DL = M->getDataLayoutStr();
2568 if (!DL.empty())
2569 Out << "target datalayout = \"" << DL << "\"\n";
Reid Spencercddc86f2004-07-25 21:44:54 +00002570 if (!M->getTargetTriple().empty())
Reid Spencerc9a1f0d2004-07-25 21:29:43 +00002571 Out << "target triple = \"" << M->getTargetTriple() << "\"\n";
Misha Brukmanfd939082005-04-21 23:48:37 +00002572
Chris Lattnercc041ba2006-01-24 04:13:11 +00002573 if (!M->getModuleInlineAsm().empty()) {
Dan Gohman9bf0b9b2009-08-12 23:54:22 +00002574 Out << '\n';
Benjamin Krameraa847942015-06-07 13:59:33 +00002575
2576 // Split the string into lines, to make it easier to read the .ll file.
Benjamin Kramer9ebaf8c2015-06-08 18:58:57 +00002577 StringRef Asm = M->getModuleInlineAsm();
Benjamin Krameraa847942015-06-07 13:59:33 +00002578 do {
2579 StringRef Front;
2580 std::tie(Front, Asm) = Asm.split('\n');
2581
Chris Lattner42a162e2006-01-24 00:45:30 +00002582 // We found a newline, print the portion of the asm string from the
2583 // last newline up to this newline.
2584 Out << "module asm \"";
Jonas Devlieghere7eeba252018-05-31 17:01:42 +00002585 printEscapedString(Front, Out);
Chris Lattner42a162e2006-01-24 00:45:30 +00002586 Out << "\"\n";
Benjamin Krameraa847942015-06-07 13:59:33 +00002587 } while (!Asm.empty());
Chris Lattner18365502006-01-23 23:03:36 +00002588 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002589
Chris Lattner1afcace2011-07-09 17:41:24 +00002590 printTypeIdentities();
Misha Brukmanfd939082005-04-21 23:48:37 +00002591
David Majnemerc8a11692014-06-27 18:19:56 +00002592 // Output all comdats.
2593 if (!Comdats.empty())
2594 Out << '\n';
2595 for (const Comdat *C : Comdats) {
2596 printComdat(C);
2597 if (C != Comdats.back())
2598 Out << '\n';
2599 }
2600
Dan Gohman9bf0b9b2009-08-12 23:54:22 +00002601 // Output all globals.
2602 if (!M->global_empty()) Out << '\n';
Yaron Keren09eacc12015-06-13 17:50:47 +00002603 for (const GlobalVariable &GV : M->globals()) {
2604 printGlobal(&GV); Out << '\n';
Duncan Sands79da6ef2012-09-12 09:55:51 +00002605 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002606
Chris Lattner69dacfc2007-04-26 02:24:10 +00002607 // Output all aliases.
2608 if (!M->alias_empty()) Out << "\n";
Yaron Keren09eacc12015-06-13 17:50:47 +00002609 for (const GlobalAlias &GA : M->aliases())
Dmitry Polukhin51a06a22016-04-05 08:47:51 +00002610 printIndirectSymbol(&GA);
Chris Lattner007377f2001-09-07 16:36:04 +00002611
Dmitry Polukhinba492232016-04-07 12:32:19 +00002612 // Output all ifuncs.
2613 if (!M->ifunc_empty()) Out << "\n";
2614 for (const GlobalIFunc &GI : M->ifuncs())
2615 printIndirectSymbol(&GI);
2616
Duncan P. N. Exon Smith78388182014-08-19 21:30:15 +00002617 // Output global use-lists.
2618 printUseLists(nullptr);
2619
Chris Lattner44da7d72004-09-14 05:06:58 +00002620 // Output all of the functions.
Yaron Keren09eacc12015-06-13 17:50:47 +00002621 for (const Function &F : *M)
2622 printFunction(&F);
Duncan P. N. Exon Smith78388182014-08-19 21:30:15 +00002623 assert(UseListOrders.empty() && "All use-lists should have been consumed");
Devang Patel320671d2009-07-08 21:44:25 +00002624
Bill Wendlingb29ce262013-02-11 08:43:33 +00002625 // Output all attribute groups.
Bill Wendling725dae52013-04-29 23:48:06 +00002626 if (!Machine.as_empty()) {
Bill Wendlingb29ce262013-02-11 08:43:33 +00002627 Out << '\n';
2628 writeAllAttributeGroups();
2629 }
2630
Devang Patel37c4a2d2009-07-29 22:04:47 +00002631 // Output named metadata.
Dan Gohman9bf0b9b2009-08-12 23:54:22 +00002632 if (!M->named_metadata_empty()) Out << '\n';
Andrew Trick18801ec2011-09-30 19:48:58 +00002633
Yaron Keren09eacc12015-06-13 17:50:47 +00002634 for (const NamedMDNode &Node : M->named_metadata())
2635 printNamedMDNode(&Node);
Devang Patel37c4a2d2009-07-29 22:04:47 +00002636
2637 // Output metadata.
Chris Lattner307c9892009-12-31 02:20:11 +00002638 if (!Machine.mdn_empty()) {
Chris Lattner6e6b1802009-12-31 02:13:35 +00002639 Out << '\n';
2640 writeAllMDNodes();
2641 }
Chris Lattner007377f2001-09-07 16:36:04 +00002642}
2643
Teresa Johnsona9a21472018-05-26 02:34:13 +00002644void AssemblyWriter::printModuleSummaryIndex() {
2645 assert(TheIndex);
Teresa Johnsona9961f32018-07-03 15:52:57 +00002646 Machine.initializeIndexIfNeeded();
Teresa Johnsona9a21472018-05-26 02:34:13 +00002647
2648 Out << "\n";
2649
Teresa Johnson807ab9582018-07-02 22:09:23 +00002650 // Print module path entries. To print in order, add paths to a vector
2651 // indexed by module slot.
Teresa Johnson601e1a72018-05-26 02:53:52 +00002652 std::vector<std::pair<std::string, ModuleHash>> moduleVec;
Teresa Johnsona9a21472018-05-26 02:34:13 +00002653 std::string RegularLTOModuleName = "[Regular LTO]";
2654 moduleVec.resize(TheIndex->modulePaths().size());
Teresa Johnson807ab9582018-07-02 22:09:23 +00002655 for (auto &ModPath : TheIndex->modulePaths())
2656 moduleVec[Machine.getModulePathSlot(ModPath.first())] = std::make_pair(
2657 // A module id of -1 is a special entry for a regular LTO module created
2658 // during the thin link.
2659 ModPath.second.first == -1u ? RegularLTOModuleName
2660 : (std::string)ModPath.first(),
2661 ModPath.second.second);
2662
Teresa Johnsona9a21472018-05-26 02:34:13 +00002663 unsigned i = 0;
2664 for (auto &ModPair : moduleVec) {
2665 Out << "^" << i++ << " = module: (";
Andrew Ngf6e61652018-07-12 14:40:21 +00002666 Out << "path: \"";
2667 printEscapedString(ModPair.first, Out);
2668 Out << "\", hash: (";
Teresa Johnsona9a21472018-05-26 02:34:13 +00002669 FieldSeparator FS;
2670 for (auto Hash : ModPair.second)
2671 Out << FS << Hash;
2672 Out << "))\n";
2673 }
2674
2675 // FIXME: Change AliasSummary to hold a ValueInfo instead of summary pointer
2676 // for aliasee (then update BitcodeWriter.cpp and remove get/setAliaseeGUID).
2677 for (auto &GlobalList : *TheIndex) {
2678 auto GUID = GlobalList.first;
2679 for (auto &Summary : GlobalList.second.SummaryList)
2680 SummaryToGUIDMap[Summary.get()] = GUID;
2681 }
2682
2683 // Print the global value summary entries.
2684 for (auto &GlobalList : *TheIndex) {
2685 auto GUID = GlobalList.first;
2686 auto VI = TheIndex->getValueInfo(GlobalList);
2687 printSummaryInfo(Machine.getGUIDSlot(GUID), VI);
2688 }
2689
2690 // Print the TypeIdMap entries.
Teresa Johnson0d4dfd22018-09-25 20:14:40 +00002691 for (auto TidIter = TheIndex->typeIds().begin();
2692 TidIter != TheIndex->typeIds().end(); TidIter++) {
2693 Out << "^" << Machine.getTypeIdSlot(TidIter->second.first)
2694 << " = typeid: (name: \"" << TidIter->second.first << "\"";
2695 printTypeIdSummary(TidIter->second.second);
2696 Out << ") ; guid = " << TidIter->first << "\n";
Teresa Johnsona9a21472018-05-26 02:34:13 +00002697 }
2698}
2699
2700static const char *
2701getWholeProgDevirtResKindName(WholeProgramDevirtResolution::Kind K) {
2702 switch (K) {
2703 case WholeProgramDevirtResolution::Indir:
2704 return "indir";
2705 case WholeProgramDevirtResolution::SingleImpl:
2706 return "singleImpl";
2707 case WholeProgramDevirtResolution::BranchFunnel:
2708 return "branchFunnel";
2709 }
2710 llvm_unreachable("invalid WholeProgramDevirtResolution kind");
2711}
2712
2713static const char *getWholeProgDevirtResByArgKindName(
2714 WholeProgramDevirtResolution::ByArg::Kind K) {
2715 switch (K) {
2716 case WholeProgramDevirtResolution::ByArg::Indir:
2717 return "indir";
2718 case WholeProgramDevirtResolution::ByArg::UniformRetVal:
2719 return "uniformRetVal";
2720 case WholeProgramDevirtResolution::ByArg::UniqueRetVal:
2721 return "uniqueRetVal";
2722 case WholeProgramDevirtResolution::ByArg::VirtualConstProp:
2723 return "virtualConstProp";
2724 }
2725 llvm_unreachable("invalid WholeProgramDevirtResolution::ByArg kind");
2726}
2727
2728static const char *getTTResKindName(TypeTestResolution::Kind K) {
2729 switch (K) {
2730 case TypeTestResolution::Unsat:
2731 return "unsat";
2732 case TypeTestResolution::ByteArray:
2733 return "byteArray";
2734 case TypeTestResolution::Inline:
2735 return "inline";
2736 case TypeTestResolution::Single:
2737 return "single";
2738 case TypeTestResolution::AllOnes:
2739 return "allOnes";
2740 }
2741 llvm_unreachable("invalid TypeTestResolution kind");
2742}
2743
2744void AssemblyWriter::printTypeTestResolution(const TypeTestResolution &TTRes) {
2745 Out << "typeTestRes: (kind: " << getTTResKindName(TTRes.TheKind)
2746 << ", sizeM1BitWidth: " << TTRes.SizeM1BitWidth;
2747
2748 // The following fields are only used if the target does not support the use
2749 // of absolute symbols to store constants. Print only if non-zero.
2750 if (TTRes.AlignLog2)
2751 Out << ", alignLog2: " << TTRes.AlignLog2;
2752 if (TTRes.SizeM1)
2753 Out << ", sizeM1: " << TTRes.SizeM1;
2754 if (TTRes.BitMask)
2755 // BitMask is uint8_t which causes it to print the corresponding char.
2756 Out << ", bitMask: " << (unsigned)TTRes.BitMask;
2757 if (TTRes.InlineBits)
2758 Out << ", inlineBits: " << TTRes.InlineBits;
2759
2760 Out << ")";
2761}
2762
2763void AssemblyWriter::printTypeIdSummary(const TypeIdSummary &TIS) {
2764 Out << ", summary: (";
2765 printTypeTestResolution(TIS.TTRes);
2766 if (!TIS.WPDRes.empty()) {
2767 Out << ", wpdResolutions: (";
2768 FieldSeparator FS;
2769 for (auto &WPDRes : TIS.WPDRes) {
2770 Out << FS;
2771 Out << "(offset: " << WPDRes.first << ", ";
2772 printWPDRes(WPDRes.second);
2773 Out << ")";
2774 }
2775 Out << ")";
2776 }
2777 Out << ")";
2778}
2779
2780void AssemblyWriter::printArgs(const std::vector<uint64_t> &Args) {
2781 Out << "args: (";
2782 FieldSeparator FS;
2783 for (auto arg : Args) {
2784 Out << FS;
2785 Out << arg;
2786 }
2787 Out << ")";
2788}
2789
2790void AssemblyWriter::printWPDRes(const WholeProgramDevirtResolution &WPDRes) {
2791 Out << "wpdRes: (kind: ";
2792 Out << getWholeProgDevirtResKindName(WPDRes.TheKind);
2793
2794 if (WPDRes.TheKind == WholeProgramDevirtResolution::SingleImpl)
2795 Out << ", singleImplName: \"" << WPDRes.SingleImplName << "\"";
2796
2797 if (!WPDRes.ResByArg.empty()) {
2798 Out << ", resByArg: (";
2799 FieldSeparator FS;
2800 for (auto &ResByArg : WPDRes.ResByArg) {
2801 Out << FS;
2802 printArgs(ResByArg.first);
2803 Out << ", byArg: (kind: ";
2804 Out << getWholeProgDevirtResByArgKindName(ResByArg.second.TheKind);
2805 if (ResByArg.second.TheKind ==
2806 WholeProgramDevirtResolution::ByArg::UniformRetVal ||
2807 ResByArg.second.TheKind ==
2808 WholeProgramDevirtResolution::ByArg::UniqueRetVal)
2809 Out << ", info: " << ResByArg.second.Info;
2810
2811 // The following fields are only used if the target does not support the
2812 // use of absolute symbols to store constants. Print only if non-zero.
2813 if (ResByArg.second.Byte || ResByArg.second.Bit)
2814 Out << ", byte: " << ResByArg.second.Byte
2815 << ", bit: " << ResByArg.second.Bit;
2816
2817 Out << ")";
2818 }
2819 Out << ")";
2820 }
2821 Out << ")";
2822}
2823
2824static const char *getSummaryKindName(GlobalValueSummary::SummaryKind SK) {
2825 switch (SK) {
2826 case GlobalValueSummary::AliasKind:
2827 return "alias";
2828 case GlobalValueSummary::FunctionKind:
2829 return "function";
2830 case GlobalValueSummary::GlobalVarKind:
2831 return "variable";
2832 }
2833 llvm_unreachable("invalid summary kind");
2834}
2835
2836void AssemblyWriter::printAliasSummary(const AliasSummary *AS) {
Teresa Johnson3aca69c2018-07-03 01:11:43 +00002837 Out << ", aliasee: ";
2838 // The indexes emitted for distributed backends may not include the
2839 // aliasee summary (only if it is being imported directly). Handle
2840 // that case by just emitting "null" as the aliasee.
2841 if (AS->hasAliasee())
2842 Out << "^" << Machine.getGUIDSlot(SummaryToGUIDMap[&AS->getAliasee()]);
2843 else
2844 Out << "null";
Teresa Johnsona9a21472018-05-26 02:34:13 +00002845}
2846
2847void AssemblyWriter::printGlobalVarSummary(const GlobalVarSummary *GS) {
Eugene Leviantae9f7732018-11-23 10:54:51 +00002848 Out << ", varFlags: (readonly: " << GS->VarFlags.ReadOnly << ")";
Teresa Johnsona9a21472018-05-26 02:34:13 +00002849}
2850
Teresa Johnsonf1f35aa2018-05-25 15:15:39 +00002851static std::string getLinkageName(GlobalValue::LinkageTypes LT) {
2852 switch (LT) {
2853 case GlobalValue::ExternalLinkage:
2854 return "external";
2855 case GlobalValue::PrivateLinkage:
2856 return "private";
2857 case GlobalValue::InternalLinkage:
2858 return "internal";
2859 case GlobalValue::LinkOnceAnyLinkage:
2860 return "linkonce";
2861 case GlobalValue::LinkOnceODRLinkage:
2862 return "linkonce_odr";
2863 case GlobalValue::WeakAnyLinkage:
2864 return "weak";
2865 case GlobalValue::WeakODRLinkage:
2866 return "weak_odr";
2867 case GlobalValue::CommonLinkage:
2868 return "common";
2869 case GlobalValue::AppendingLinkage:
2870 return "appending";
2871 case GlobalValue::ExternalWeakLinkage:
2872 return "extern_weak";
2873 case GlobalValue::AvailableExternallyLinkage:
2874 return "available_externally";
2875 }
2876 llvm_unreachable("invalid linkage");
2877}
2878
2879// When printing the linkage types in IR where the ExternalLinkage is
2880// not printed, and other linkage types are expected to be printed with
2881// a space after the name.
2882static std::string getLinkageNameWithSpace(GlobalValue::LinkageTypes LT) {
2883 if (LT == GlobalValue::ExternalLinkage)
2884 return "";
2885 return getLinkageName(LT) + " ";
2886}
2887
Teresa Johnsona9a21472018-05-26 02:34:13 +00002888void AssemblyWriter::printFunctionSummary(const FunctionSummary *FS) {
2889 Out << ", insts: " << FS->instCount();
2890
2891 FunctionSummary::FFlags FFlags = FS->fflags();
2892 if (FFlags.ReadNone | FFlags.ReadOnly | FFlags.NoRecurse |
2893 FFlags.ReturnDoesNotAlias) {
2894 Out << ", funcFlags: (";
2895 Out << "readNone: " << FFlags.ReadNone;
2896 Out << ", readOnly: " << FFlags.ReadOnly;
2897 Out << ", noRecurse: " << FFlags.NoRecurse;
2898 Out << ", returnDoesNotAlias: " << FFlags.ReturnDoesNotAlias;
Teresa Johnson645cd312018-11-06 19:41:35 +00002899 Out << ", noInline: " << FFlags.NoInline;
Teresa Johnsona9a21472018-05-26 02:34:13 +00002900 Out << ")";
2901 }
2902 if (!FS->calls().empty()) {
2903 Out << ", calls: (";
2904 FieldSeparator IFS;
2905 for (auto &Call : FS->calls()) {
2906 Out << IFS;
2907 Out << "(callee: ^" << Machine.getGUIDSlot(Call.first.getGUID());
2908 if (Call.second.getHotness() != CalleeInfo::HotnessType::Unknown)
2909 Out << ", hotness: " << getHotnessName(Call.second.getHotness());
2910 else if (Call.second.RelBlockFreq)
2911 Out << ", relbf: " << Call.second.RelBlockFreq;
2912 Out << ")";
2913 }
2914 Out << ")";
2915 }
2916
2917 if (const auto *TIdInfo = FS->getTypeIdInfo())
2918 printTypeIdInfo(*TIdInfo);
2919}
2920
2921void AssemblyWriter::printTypeIdInfo(
2922 const FunctionSummary::TypeIdInfo &TIDInfo) {
2923 Out << ", typeIdInfo: (";
2924 FieldSeparator TIDFS;
2925 if (!TIDInfo.TypeTests.empty()) {
2926 Out << TIDFS;
2927 Out << "typeTests: (";
2928 FieldSeparator FS;
2929 for (auto &GUID : TIDInfo.TypeTests) {
Teresa Johnson0d4dfd22018-09-25 20:14:40 +00002930 auto TidIter = TheIndex->typeIds().equal_range(GUID);
2931 if (TidIter.first == TidIter.second) {
2932 Out << FS;
Teresa Johnsona9a21472018-05-26 02:34:13 +00002933 Out << GUID;
Teresa Johnson0d4dfd22018-09-25 20:14:40 +00002934 continue;
2935 }
2936 // Print all type id that correspond to this GUID.
2937 for (auto It = TidIter.first; It != TidIter.second; ++It) {
2938 Out << FS;
2939 auto Slot = Machine.getTypeIdSlot(It->second.first);
2940 assert(Slot != -1);
2941 Out << "^" << Slot;
2942 }
Teresa Johnsona9a21472018-05-26 02:34:13 +00002943 }
2944 Out << ")";
2945 }
2946 if (!TIDInfo.TypeTestAssumeVCalls.empty()) {
2947 Out << TIDFS;
2948 printNonConstVCalls(TIDInfo.TypeTestAssumeVCalls, "typeTestAssumeVCalls");
2949 }
2950 if (!TIDInfo.TypeCheckedLoadVCalls.empty()) {
2951 Out << TIDFS;
2952 printNonConstVCalls(TIDInfo.TypeCheckedLoadVCalls, "typeCheckedLoadVCalls");
2953 }
2954 if (!TIDInfo.TypeTestAssumeConstVCalls.empty()) {
2955 Out << TIDFS;
2956 printConstVCalls(TIDInfo.TypeTestAssumeConstVCalls,
2957 "typeTestAssumeConstVCalls");
2958 }
2959 if (!TIDInfo.TypeCheckedLoadConstVCalls.empty()) {
2960 Out << TIDFS;
2961 printConstVCalls(TIDInfo.TypeCheckedLoadConstVCalls,
2962 "typeCheckedLoadConstVCalls");
2963 }
2964 Out << ")";
2965}
2966
2967void AssemblyWriter::printVFuncId(const FunctionSummary::VFuncId VFId) {
Teresa Johnson0d4dfd22018-09-25 20:14:40 +00002968 auto TidIter = TheIndex->typeIds().equal_range(VFId.GUID);
2969 if (TidIter.first == TidIter.second) {
2970 Out << "vFuncId: (";
Teresa Johnsona9a21472018-05-26 02:34:13 +00002971 Out << "guid: " << VFId.GUID;
Teresa Johnson0d4dfd22018-09-25 20:14:40 +00002972 Out << ", offset: " << VFId.Offset;
2973 Out << ")";
2974 return;
2975 }
2976 // Print all type id that correspond to this GUID.
2977 FieldSeparator FS;
2978 for (auto It = TidIter.first; It != TidIter.second; ++It) {
2979 Out << FS;
2980 Out << "vFuncId: (";
2981 auto Slot = Machine.getTypeIdSlot(It->second.first);
2982 assert(Slot != -1);
2983 Out << "^" << Slot;
2984 Out << ", offset: " << VFId.Offset;
2985 Out << ")";
2986 }
Teresa Johnsona9a21472018-05-26 02:34:13 +00002987}
2988
2989void AssemblyWriter::printNonConstVCalls(
2990 const std::vector<FunctionSummary::VFuncId> VCallList, const char *Tag) {
2991 Out << Tag << ": (";
2992 FieldSeparator FS;
2993 for (auto &VFuncId : VCallList) {
2994 Out << FS;
2995 printVFuncId(VFuncId);
2996 }
2997 Out << ")";
2998}
2999
3000void AssemblyWriter::printConstVCalls(
3001 const std::vector<FunctionSummary::ConstVCall> VCallList, const char *Tag) {
3002 Out << Tag << ": (";
3003 FieldSeparator FS;
3004 for (auto &ConstVCall : VCallList) {
3005 Out << FS;
Teresa Johnsond64c9dd2018-08-14 01:49:33 +00003006 Out << "(";
Teresa Johnsona9a21472018-05-26 02:34:13 +00003007 printVFuncId(ConstVCall.VFunc);
3008 if (!ConstVCall.Args.empty()) {
3009 Out << ", ";
3010 printArgs(ConstVCall.Args);
3011 }
Teresa Johnsond64c9dd2018-08-14 01:49:33 +00003012 Out << ")";
Teresa Johnsona9a21472018-05-26 02:34:13 +00003013 }
3014 Out << ")";
3015}
3016
3017void AssemblyWriter::printSummary(const GlobalValueSummary &Summary) {
3018 GlobalValueSummary::GVFlags GVFlags = Summary.flags();
3019 GlobalValue::LinkageTypes LT = (GlobalValue::LinkageTypes)GVFlags.Linkage;
3020 Out << getSummaryKindName(Summary.getSummaryKind()) << ": ";
Teresa Johnson807ab9582018-07-02 22:09:23 +00003021 Out << "(module: ^" << Machine.getModulePathSlot(Summary.modulePath())
Teresa Johnsona9a21472018-05-26 02:34:13 +00003022 << ", flags: (";
3023 Out << "linkage: " << getLinkageName(LT);
3024 Out << ", notEligibleToImport: " << GVFlags.NotEligibleToImport;
3025 Out << ", live: " << GVFlags.Live;
3026 Out << ", dsoLocal: " << GVFlags.DSOLocal;
3027 Out << ")";
3028
3029 if (Summary.getSummaryKind() == GlobalValueSummary::AliasKind)
3030 printAliasSummary(cast<AliasSummary>(&Summary));
3031 else if (Summary.getSummaryKind() == GlobalValueSummary::FunctionKind)
3032 printFunctionSummary(cast<FunctionSummary>(&Summary));
3033 else
3034 printGlobalVarSummary(cast<GlobalVarSummary>(&Summary));
3035
3036 auto RefList = Summary.refs();
3037 if (!RefList.empty()) {
3038 Out << ", refs: (";
3039 FieldSeparator FS;
3040 for (auto &Ref : RefList) {
3041 Out << FS;
Eugene Leviantae9f7732018-11-23 10:54:51 +00003042 if (Ref.isReadOnly())
3043 Out << "readonly ";
Teresa Johnsona9a21472018-05-26 02:34:13 +00003044 Out << "^" << Machine.getGUIDSlot(Ref.getGUID());
3045 }
3046 Out << ")";
3047 }
3048
3049 Out << ")";
3050}
3051
3052void AssemblyWriter::printSummaryInfo(unsigned Slot, const ValueInfo &VI) {
3053 Out << "^" << Slot << " = gv: (";
3054 if (!VI.name().empty())
3055 Out << "name: \"" << VI.name() << "\"";
3056 else
3057 Out << "guid: " << VI.getGUID();
3058 if (!VI.getSummaryList().empty()) {
3059 Out << ", summaries: (";
3060 FieldSeparator FS;
3061 for (auto &Summary : VI.getSummaryList()) {
3062 Out << FS;
3063 printSummary(*Summary);
3064 }
3065 Out << ")";
3066 }
3067 Out << ")";
3068 if (!VI.name().empty())
3069 Out << " ; guid = " << VI.getGUID();
3070 Out << "\n";
3071}
3072
Filipe Cabecinhas6af0f892015-06-02 21:25:08 +00003073static void printMetadataIdentifier(StringRef Name,
3074 formatted_raw_ostream &Out) {
Nick Lewycky9100a782011-06-15 06:37:58 +00003075 if (Name.empty()) {
3076 Out << "<empty name> ";
3077 } else {
Filipe Cabecinhas581e2552015-06-02 21:25:00 +00003078 if (isalpha(static_cast<unsigned char>(Name[0])) || Name[0] == '-' ||
3079 Name[0] == '$' || Name[0] == '.' || Name[0] == '_')
Nick Lewycky9100a782011-06-15 06:37:58 +00003080 Out << Name[0];
3081 else
3082 Out << '\\' << hexdigit(Name[0] >> 4) << hexdigit(Name[0] & 0x0F);
3083 for (unsigned i = 1, e = Name.size(); i != e; ++i) {
3084 unsigned char C = Name[i];
Guy Benyei87d0b9e2013-02-12 21:21:59 +00003085 if (isalnum(static_cast<unsigned char>(C)) || C == '-' || C == '$' ||
3086 C == '.' || C == '_')
Nick Lewycky9100a782011-06-15 06:37:58 +00003087 Out << C;
3088 else
3089 Out << '\\' << hexdigit(C >> 4) << hexdigit(C & 0x0F);
3090 }
3091 }
Filipe Cabecinhas6af0f892015-06-02 21:25:08 +00003092}
3093
3094void AssemblyWriter::printNamedMDNode(const NamedMDNode *NMD) {
3095 Out << '!';
3096 printMetadataIdentifier(NMD->getName(), Out);
Nick Lewycky9100a782011-06-15 06:37:58 +00003097 Out << " = !{";
Chris Lattnerfdb33562009-12-31 01:54:05 +00003098 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
Filipe Cabecinhas581e2552015-06-02 21:25:00 +00003099 if (i)
3100 Out << ", ";
Reid Klecknera5b2af02017-08-23 20:31:27 +00003101
3102 // Write DIExpressions inline.
3103 // FIXME: Ban DIExpressions in NamedMDNodes, they will serve no purpose.
3104 MDNode *Op = NMD->getOperand(i);
3105 if (auto *Expr = dyn_cast<DIExpression>(Op)) {
3106 writeDIExpression(Out, Expr, nullptr, nullptr, nullptr);
3107 continue;
3108 }
3109
3110 int Slot = Machine.getMetadataSlot(Op);
Dan Gohman3da076f2010-09-09 20:53:58 +00003111 if (Slot == -1)
3112 Out << "<badref>";
3113 else
3114 Out << '!' << Slot;
Chris Lattnerfdb33562009-12-31 01:54:05 +00003115 }
3116 Out << "}\n";
3117}
3118
Chris Lattnercfb5a202008-08-19 05:06:27 +00003119static void PrintVisibility(GlobalValue::VisibilityTypes Vis,
Dan Gohman683e9222009-08-12 17:23:50 +00003120 formatted_raw_ostream &Out) {
Chris Lattnercfb5a202008-08-19 05:06:27 +00003121 switch (Vis) {
Chris Lattnercfb5a202008-08-19 05:06:27 +00003122 case GlobalValue::DefaultVisibility: break;
3123 case GlobalValue::HiddenVisibility: Out << "hidden "; break;
3124 case GlobalValue::ProtectedVisibility: Out << "protected "; break;
3125 }
3126}
3127
Rafael Espindola1e1801c2018-01-11 22:15:05 +00003128static void PrintDSOLocation(const GlobalValue &GV,
3129 formatted_raw_ostream &Out) {
Rafael Espindola7e523c12018-01-18 02:08:23 +00003130 // GVs with local linkage or non default visibility are implicitly dso_local,
3131 // so we don't print it.
3132 bool Implicit = GV.hasLocalLinkage() ||
3133 (!GV.hasExternalWeakLinkage() && !GV.hasDefaultVisibility());
3134 if (GV.isDSOLocal() && !Implicit)
Sean Fertile509132b2017-10-26 15:00:26 +00003135 Out << "dso_local ";
3136}
3137
Nico Rieck38f68c52014-01-14 15:22:47 +00003138static void PrintDLLStorageClass(GlobalValue::DLLStorageClassTypes SCT,
3139 formatted_raw_ostream &Out) {
3140 switch (SCT) {
3141 case GlobalValue::DefaultStorageClass: break;
3142 case GlobalValue::DLLImportStorageClass: Out << "dllimport "; break;
3143 case GlobalValue::DLLExportStorageClass: Out << "dllexport "; break;
3144 }
3145}
3146
Hans Wennborgce718ff2012-06-23 11:37:03 +00003147static void PrintThreadLocalModel(GlobalVariable::ThreadLocalMode TLM,
3148 formatted_raw_ostream &Out) {
3149 switch (TLM) {
3150 case GlobalVariable::NotThreadLocal:
3151 break;
3152 case GlobalVariable::GeneralDynamicTLSModel:
3153 Out << "thread_local ";
3154 break;
3155 case GlobalVariable::LocalDynamicTLSModel:
3156 Out << "thread_local(localdynamic) ";
3157 break;
3158 case GlobalVariable::InitialExecTLSModel:
3159 Out << "thread_local(initialexec) ";
3160 break;
3161 case GlobalVariable::LocalExecTLSModel:
3162 Out << "thread_local(localexec) ";
3163 break;
3164 }
3165}
3166
Peter Collingbourne63b34cd2016-06-14 21:01:22 +00003167static StringRef getUnnamedAddrEncoding(GlobalVariable::UnnamedAddr UA) {
3168 switch (UA) {
3169 case GlobalVariable::UnnamedAddr::None:
3170 return "";
3171 case GlobalVariable::UnnamedAddr::Local:
3172 return "local_unnamed_addr";
3173 case GlobalVariable::UnnamedAddr::Global:
3174 return "unnamed_addr";
3175 }
Aaron Ballman0774f892016-06-15 15:27:53 +00003176 llvm_unreachable("Unknown UnnamedAddr");
Peter Collingbourne63b34cd2016-06-14 21:01:22 +00003177}
3178
Rafael Espindolaf907a262015-01-06 22:55:16 +00003179static void maybePrintComdat(formatted_raw_ostream &Out,
3180 const GlobalObject &GO) {
3181 const Comdat *C = GO.getComdat();
3182 if (!C)
3183 return;
3184
3185 if (isa<GlobalVariable>(GO))
3186 Out << ',';
3187 Out << " comdat";
3188
3189 if (GO.getName() == C->getName())
3190 return;
3191
3192 Out << '(';
3193 PrintLLVMName(Out, C->getName(), ComdatPrefix);
3194 Out << ')';
3195}
3196
Chris Lattnerc1824992001-10-29 16:05:51 +00003197void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Dan Gohman4483c7b2010-01-29 23:12:36 +00003198 if (GV->isMaterializable())
3199 Out << "; Materializable\n";
3200
Dan Gohman3bdfbf52010-07-20 23:55:01 +00003201 WriteAsOperandInternal(Out, GV, &TypePrinter, &Machine, GV->getParent());
Dan Gohman3845e502009-08-12 23:32:33 +00003202 Out << " = ";
Chris Lattnerd70684f2001-09-18 04:01:05 +00003203
Chris Lattner52b26de2008-08-19 05:16:28 +00003204 if (!GV->hasInitializer() && GV->hasExternalLinkage())
3205 Out << "external ";
Daniel Dunbara279bc32009-09-20 02:20:51 +00003206
Teresa Johnsonf1f35aa2018-05-25 15:15:39 +00003207 Out << getLinkageNameWithSpace(GV->getLinkage());
Rafael Espindola1e1801c2018-01-11 22:15:05 +00003208 PrintDSOLocation(*GV, Out);
Chris Lattner52b26de2008-08-19 05:16:28 +00003209 PrintVisibility(GV->getVisibility(), Out);
Nico Rieck38f68c52014-01-14 15:22:47 +00003210 PrintDLLStorageClass(GV->getDLLStorageClass(), Out);
Hans Wennborgce718ff2012-06-23 11:37:03 +00003211 PrintThreadLocalModel(GV->getThreadLocalMode(), Out);
Peter Collingbourne63b34cd2016-06-14 21:01:22 +00003212 StringRef UA = getUnnamedAddrEncoding(GV->getUnnamedAddr());
3213 if (!UA.empty())
3214 Out << UA << ' ';
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00003215
Chris Lattnerdf986172009-01-02 07:01:27 +00003216 if (unsigned AddressSpace = GV->getType()->getAddressSpace())
3217 Out << "addrspace(" << AddressSpace << ") ";
Michael Gottesmana2de37c2013-02-05 05:57:38 +00003218 if (GV->isExternallyInitialized()) Out << "externally_initialized ";
Misha Brukman0313e0b2004-06-21 21:53:56 +00003219 Out << (GV->isConstant() ? "constant " : "global ");
Manuel Jacob75e1cfb2016-01-16 20:30:46 +00003220 TypePrinter.print(GV->getValueType(), Out);
Chris Lattnerd70684f2001-09-18 04:01:05 +00003221
Dan Gohman8dae1382008-09-14 17:21:12 +00003222 if (GV->hasInitializer()) {
3223 Out << ' ';
Devang Patel320671d2009-07-08 21:44:25 +00003224 writeOperand(GV->getInitializer(), false);
Dan Gohman8dae1382008-09-14 17:21:12 +00003225 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003226
Chris Lattner8fff1262010-07-07 23:16:37 +00003227 if (GV->hasSection()) {
3228 Out << ", section \"";
Jonas Devlieghere7eeba252018-05-31 17:01:42 +00003229 printEscapedString(GV->getSection(), Out);
Chris Lattner8fff1262010-07-07 23:16:37 +00003230 Out << '"';
3231 }
Rafael Espindolaf907a262015-01-06 22:55:16 +00003232 maybePrintComdat(Out, *GV);
Chris Lattner60962db2005-11-12 00:10:19 +00003233 if (GV->getAlignment())
Chris Lattner30caa282005-11-06 06:48:53 +00003234 Out << ", align " << GV->getAlignment();
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00003235
Peter Collingbourne6aef9f92016-05-31 23:01:54 +00003236 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
3237 GV->getAllMetadata(MDs);
3238 printMetadataAttachments(MDs, ", ");
3239
Javed Absara8ddcaa2017-05-11 12:28:08 +00003240 auto Attrs = GV->getAttributes();
3241 if (Attrs.hasAttributes())
3242 Out << " #" << Machine.getAttributeGroupSlot(Attrs);
3243
Chris Lattner7e708292002-06-25 16:13:24 +00003244 printInfoComment(*GV);
Chris Lattner70cc3392001-09-10 07:58:01 +00003245}
3246
Dmitry Polukhin51a06a22016-04-05 08:47:51 +00003247void AssemblyWriter::printIndirectSymbol(const GlobalIndirectSymbol *GIS) {
3248 if (GIS->isMaterializable())
Dan Gohman4483c7b2010-01-29 23:12:36 +00003249 Out << "; Materializable\n";
3250
Dmitry Polukhin51a06a22016-04-05 08:47:51 +00003251 WriteAsOperandInternal(Out, GIS, &TypePrinter, &Machine, GIS->getParent());
Rafael Espindola43e53492015-06-17 17:53:31 +00003252 Out << " = ";
3253
Teresa Johnsonf1f35aa2018-05-25 15:15:39 +00003254 Out << getLinkageNameWithSpace(GIS->getLinkage());
Rafael Espindola1e1801c2018-01-11 22:15:05 +00003255 PrintDSOLocation(*GIS, Out);
Dmitry Polukhin51a06a22016-04-05 08:47:51 +00003256 PrintVisibility(GIS->getVisibility(), Out);
3257 PrintDLLStorageClass(GIS->getDLLStorageClass(), Out);
3258 PrintThreadLocalModel(GIS->getThreadLocalMode(), Out);
Peter Collingbourne63b34cd2016-06-14 21:01:22 +00003259 StringRef UA = getUnnamedAddrEncoding(GIS->getUnnamedAddr());
3260 if (!UA.empty())
3261 Out << UA << ' ';
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00003262
Dmitry Polukhin51a06a22016-04-05 08:47:51 +00003263 if (isa<GlobalAlias>(GIS))
3264 Out << "alias ";
Dmitry Polukhinba492232016-04-07 12:32:19 +00003265 else if (isa<GlobalIFunc>(GIS))
3266 Out << "ifunc ";
Dmitry Polukhin51a06a22016-04-05 08:47:51 +00003267 else
Dmitry Polukhinba492232016-04-07 12:32:19 +00003268 llvm_unreachable("Not an alias or ifunc!");
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00003269
Dmitry Polukhin51a06a22016-04-05 08:47:51 +00003270 TypePrinter.print(GIS->getValueType(), Out);
David Blaikie21f77df2015-09-11 03:22:04 +00003271
3272 Out << ", ";
3273
Dmitry Polukhin51a06a22016-04-05 08:47:51 +00003274 const Constant *IS = GIS->getIndirectSymbol();
Daniel Dunbara279bc32009-09-20 02:20:51 +00003275
Dmitry Polukhin51a06a22016-04-05 08:47:51 +00003276 if (!IS) {
3277 TypePrinter.print(GIS->getType(), Out);
Chris Lattner1afcace2011-07-09 17:41:24 +00003278 Out << " <<NULL ALIASEE>>";
Jay Foad5cd8ea22011-08-01 12:48:54 +00003279 } else {
Dmitry Polukhin51a06a22016-04-05 08:47:51 +00003280 writeOperand(IS, !isa<ConstantExpr>(IS));
Jay Foad5cd8ea22011-08-01 12:48:54 +00003281 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003282
Dmitry Polukhin51a06a22016-04-05 08:47:51 +00003283 printInfoComment(*GIS);
Chris Lattner52b26de2008-08-19 05:16:28 +00003284 Out << '\n';
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00003285}
3286
David Majnemerc8a11692014-06-27 18:19:56 +00003287void AssemblyWriter::printComdat(const Comdat *C) {
3288 C->print(Out);
3289}
3290
Chris Lattner1afcace2011-07-09 17:41:24 +00003291void AssemblyWriter::printTypeIdentities() {
Roman Tereshin238a3382018-03-22 21:29:07 +00003292 if (TypePrinter.empty())
Chris Lattner1afcace2011-07-09 17:41:24 +00003293 return;
Andrew Trick18801ec2011-09-30 19:48:58 +00003294
Chris Lattner1afcace2011-07-09 17:41:24 +00003295 Out << '\n';
Andrew Trick18801ec2011-09-30 19:48:58 +00003296
Chris Lattner413fd232009-03-01 00:03:38 +00003297 // Emit all numbered types.
Roman Tereshin238a3382018-03-22 21:29:07 +00003298 auto &NumberedTypes = TypePrinter.getNumberedTypes();
3299 for (unsigned I = 0, E = NumberedTypes.size(); I != E; ++I) {
3300 Out << '%' << I << " = type ";
Andrew Trick18801ec2011-09-30 19:48:58 +00003301
Chris Lattner413fd232009-03-01 00:03:38 +00003302 // Make sure we print out at least one level of the type structure, so
3303 // that we do not get %2 = type %2
Roman Tereshin238a3382018-03-22 21:29:07 +00003304 TypePrinter.printStructBody(NumberedTypes[I], Out);
Dan Gohman9bf0b9b2009-08-12 23:54:22 +00003305 Out << '\n';
Chris Lattner413fd232009-03-01 00:03:38 +00003306 }
Andrew Trick18801ec2011-09-30 19:48:58 +00003307
Roman Tereshin238a3382018-03-22 21:29:07 +00003308 auto &NamedTypes = TypePrinter.getNamedTypes();
3309 for (unsigned I = 0, E = NamedTypes.size(); I != E; ++I) {
3310 PrintLLVMName(Out, NamedTypes[I]->getName(), LocalPrefix);
Chris Lattner52b26de2008-08-19 05:16:28 +00003311 Out << " = type ";
Reid Spencer9231ac82004-05-25 08:53:40 +00003312
3313 // Make sure we print out at least one level of the type structure, so
3314 // that we do not get %FILE = type %FILE
Roman Tereshin238a3382018-03-22 21:29:07 +00003315 TypePrinter.printStructBody(NamedTypes[I], Out);
Chris Lattnercfb5a202008-08-19 05:06:27 +00003316 Out << '\n';
Reid Spencer9231ac82004-05-25 08:53:40 +00003317 }
Reid Spencer78d033e2007-01-06 07:24:44 +00003318}
3319
Misha Brukmanab5c6002004-03-02 00:22:19 +00003320/// printFunction - Print all aspects of a function.
Chris Lattner7e708292002-06-25 16:13:24 +00003321void AssemblyWriter::printFunction(const Function *F) {
Chris Lattnercfb5a202008-08-19 05:06:27 +00003322 // Print out the return type and name.
3323 Out << '\n';
Chris Lattner4ad02e72003-04-16 20:28:45 +00003324
Misha Brukman0313e0b2004-06-21 21:53:56 +00003325 if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, Out);
Chris Lattner95e5a2c2003-10-30 23:41:03 +00003326
Dan Gohman4483c7b2010-01-29 23:12:36 +00003327 if (F->isMaterializable())
3328 Out << "; Materializable\n";
3329
Reid Kleckner67077702017-03-21 16:57:19 +00003330 const AttributeList &Attrs = F->getAttributes();
3331 if (Attrs.hasAttributes(AttributeList::FunctionIndex)) {
Reid Kleckner06090402017-04-12 00:38:00 +00003332 AttributeSet AS = Attrs.getFnAttributes();
Rafael Espindolaaae02982013-05-01 13:07:03 +00003333 std::string AttrStr;
3334
Reid Kleckner06090402017-04-12 00:38:00 +00003335 for (const Attribute &Attr : AS) {
Rafael Espindolaaae02982013-05-01 13:07:03 +00003336 if (!Attr.isStringAttribute()) {
3337 if (!AttrStr.empty()) AttrStr += ' ';
3338 AttrStr += Attr.getAsString();
3339 }
3340 }
3341
Bill Wendling63405492013-04-16 20:55:47 +00003342 if (!AttrStr.empty())
3343 Out << "; Function Attrs: " << AttrStr << '\n';
3344 }
3345
Peter Collingbourne99e2e272016-06-21 23:42:48 +00003346 Machine.incorporateFunction(F);
3347
3348 if (F->isDeclaration()) {
3349 Out << "declare";
3350 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
3351 F->getAllMetadata(MDs);
3352 printMetadataAttachments(MDs, " ");
3353 Out << ' ';
3354 } else
Reid Spencerb951bc02006-12-29 20:29:48 +00003355 Out << "define ";
Daniel Dunbara279bc32009-09-20 02:20:51 +00003356
Teresa Johnsonf1f35aa2018-05-25 15:15:39 +00003357 Out << getLinkageNameWithSpace(F->getLinkage());
Rafael Espindola1e1801c2018-01-11 22:15:05 +00003358 PrintDSOLocation(*F, Out);
Chris Lattnercfb5a202008-08-19 05:06:27 +00003359 PrintVisibility(F->getVisibility(), Out);
Nico Rieck38f68c52014-01-14 15:22:47 +00003360 PrintDLLStorageClass(F->getDLLStorageClass(), Out);
Chris Lattner4ad02e72003-04-16 20:28:45 +00003361
Chris Lattnerd5118982005-05-06 20:26:43 +00003362 // Print the calling convention.
Micah Villmowd3766df2012-09-13 15:11:12 +00003363 if (F->getCallingConv() != CallingConv::C) {
3364 PrintCallingConv(F->getCallingConv(), Out);
3365 Out << " ";
Chris Lattnerd5118982005-05-06 20:26:43 +00003366 }
3367
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003368 FunctionType *FT = F->getFunctionType();
Reid Kleckner67077702017-03-21 16:57:19 +00003369 if (Attrs.hasAttributes(AttributeList::ReturnIndex))
3370 Out << Attrs.getAsString(AttributeList::ReturnIndex) << ' ';
Chris Lattner0f7364b2009-02-28 21:26:53 +00003371 TypePrinter.print(F->getReturnType(), Out);
Chris Lattner4667b712008-08-19 05:26:17 +00003372 Out << ' ';
Dan Gohman3bdfbf52010-07-20 23:55:01 +00003373 WriteAsOperandInternal(Out, F, &TypePrinter, &Machine, F->getParent());
Misha Brukman0313e0b2004-06-21 21:53:56 +00003374 Out << '(';
Chris Lattner007377f2001-09-07 16:36:04 +00003375
Chris Lattnerc1824992001-10-29 16:05:51 +00003376 // Loop over the arguments, printing them...
Justin Bogner0adaaa32015-09-27 22:38:50 +00003377 if (F->isDeclaration() && !IsForDebug) {
Justin Bogner984b15d2015-09-02 17:54:41 +00003378 // We're only interested in the type here - don't print argument names.
3379 for (unsigned I = 0, E = FT->getNumParams(); I != E; ++I) {
Chris Lattner8dcd2f12007-04-18 00:57:22 +00003380 // Insert commas as we go... the first arg doesn't get a comma
Justin Bogner984b15d2015-09-02 17:54:41 +00003381 if (I)
3382 Out << ", ";
3383 // Output type...
3384 TypePrinter.print(FT->getParamType(I), Out);
3385
Reid Kleckner331b9af2017-04-28 18:37:16 +00003386 AttributeSet ArgAttrs = Attrs.getParamAttributes(I);
3387 if (ArgAttrs.hasAttributes())
3388 Out << ' ' << ArgAttrs.getAsString();
Chris Lattner8dcd2f12007-04-18 00:57:22 +00003389 }
3390 } else {
Justin Bogner984b15d2015-09-02 17:54:41 +00003391 // The arguments are meaningful here, print them in detail.
Justin Bogner984b15d2015-09-02 17:54:41 +00003392 for (const Argument &Arg : F->args()) {
Chris Lattner8dcd2f12007-04-18 00:57:22 +00003393 // Insert commas as we go... the first arg doesn't get a comma
Reid Kleckner331b9af2017-04-28 18:37:16 +00003394 if (Arg.getArgNo() != 0)
Justin Bogner984b15d2015-09-02 17:54:41 +00003395 Out << ", ";
Reid Kleckner331b9af2017-04-28 18:37:16 +00003396 printArgument(&Arg, Attrs.getParamAttributes(Arg.getArgNo()));
Chris Lattner8dcd2f12007-04-18 00:57:22 +00003397 }
Reid Spencerbd5db8e2006-12-31 05:24:50 +00003398 }
Chris Lattner007377f2001-09-07 16:36:04 +00003399
3400 // Finish printing arguments...
Chris Lattner7e708292002-06-25 16:13:24 +00003401 if (FT->isVarArg()) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00003402 if (FT->getNumParams()) Out << ", ";
3403 Out << "..."; // Output varargs portion of signature!
Chris Lattner007377f2001-09-07 16:36:04 +00003404 }
Misha Brukman0313e0b2004-06-21 21:53:56 +00003405 Out << ')';
Peter Collingbourne63b34cd2016-06-14 21:01:22 +00003406 StringRef UA = getUnnamedAddrEncoding(F->getUnnamedAddr());
3407 if (!UA.empty())
3408 Out << ' ' << UA;
Alexander Richardson47ff67b2018-08-23 09:25:17 +00003409 // We print the function address space if it is non-zero or if we are writing
3410 // a module with a non-zero program address space or if there is no valid
3411 // Module* so that the file can be parsed without the datalayout string.
3412 const Module *Mod = F->getParent();
3413 if (F->getAddressSpace() != 0 || !Mod ||
3414 Mod->getDataLayout().getProgramAddressSpace() != 0)
3415 Out << " addrspace(" << F->getAddressSpace() << ")";
Reid Kleckner67077702017-03-21 16:57:19 +00003416 if (Attrs.hasAttributes(AttributeList::FunctionIndex))
Bill Wendling725dae52013-04-29 23:48:06 +00003417 Out << " #" << Machine.getAttributeGroupSlot(Attrs.getFnAttributes());
Chris Lattner8fff1262010-07-07 23:16:37 +00003418 if (F->hasSection()) {
3419 Out << " section \"";
Jonas Devlieghere7eeba252018-05-31 17:01:42 +00003420 printEscapedString(F->getSection(), Out);
Chris Lattner8fff1262010-07-07 23:16:37 +00003421 Out << '"';
3422 }
Rafael Espindolaf907a262015-01-06 22:55:16 +00003423 maybePrintComdat(Out, *F);
Chris Lattner30caa282005-11-06 06:48:53 +00003424 if (F->getAlignment())
3425 Out << " align " << F->getAlignment();
Gordon Henriksen5eca0752008-08-17 18:44:35 +00003426 if (F->hasGC())
3427 Out << " gc \"" << F->getGC() << '"';
Peter Collingbourne1e3037f2013-09-16 01:08:15 +00003428 if (F->hasPrefixData()) {
3429 Out << " prefix ";
3430 writeOperand(F->getPrefixData(), true);
3431 }
Peter Collingbournebb660fc2014-12-03 02:08:38 +00003432 if (F->hasPrologueData()) {
3433 Out << " prologue ";
3434 writeOperand(F->getPrologueData(), true);
3435 }
David Majnemercc714e22015-06-17 20:52:32 +00003436 if (F->hasPersonalityFn()) {
3437 Out << " personality ";
3438 writeOperand(F->getPersonalityFn(), /*PrintType=*/true);
3439 }
Peter Collingbournebb660fc2014-12-03 02:08:38 +00003440
Reid Spencer5cbf9852007-01-30 20:08:39 +00003441 if (F->isDeclaration()) {
Chris Lattner91fb4072010-09-02 22:52:10 +00003442 Out << '\n';
Chris Lattner03e2acb2002-05-06 03:00:40 +00003443 } else {
Peter Collingbourne99e2e272016-06-21 23:42:48 +00003444 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
3445 F->getAllMetadata(MDs);
3446 printMetadataAttachments(MDs, " ");
3447
Chris Lattner91fb4072010-09-02 22:52:10 +00003448 Out << " {";
3449 // Output all of the function's basic blocks.
Benjamin Kramere96e21f2016-06-26 14:10:56 +00003450 for (const BasicBlock &BB : *F)
3451 printBasicBlock(&BB);
Chris Lattner007377f2001-09-07 16:36:04 +00003452
Duncan P. N. Exon Smith78388182014-08-19 21:30:15 +00003453 // Output the function's use-lists.
3454 printUseLists(F);
3455
Misha Brukman0313e0b2004-06-21 21:53:56 +00003456 Out << "}\n";
Chris Lattner007377f2001-09-07 16:36:04 +00003457 }
3458
Reid Spencer0d1b77e2004-05-26 07:18:52 +00003459 Machine.purgeFunction();
Chris Lattner00950542001-06-06 20:29:01 +00003460}
3461
Misha Brukmanab5c6002004-03-02 00:22:19 +00003462/// printArgument - This member is called for every argument that is passed into
3463/// the function. Simply print it out
Reid Kleckner331b9af2017-04-28 18:37:16 +00003464void AssemblyWriter::printArgument(const Argument *Arg, AttributeSet Attrs) {
Chris Lattner00950542001-06-06 20:29:01 +00003465 // Output type...
Chris Lattner0f7364b2009-02-28 21:26:53 +00003466 TypePrinter.print(Arg->getType(), Out);
Misha Brukmanfd939082005-04-21 23:48:37 +00003467
Duncan Sandsdc024672007-11-27 13:23:08 +00003468 // Output parameter attributes list
Reid Kleckner331b9af2017-04-28 18:37:16 +00003469 if (Attrs.hasAttributes())
3470 Out << ' ' << Attrs.getAsString();
Reid Spencerbd5db8e2006-12-31 05:24:50 +00003471
Chris Lattner00950542001-06-06 20:29:01 +00003472 // Output name, if available...
Chris Lattnerc97536e2008-08-17 04:40:13 +00003473 if (Arg->hasName()) {
3474 Out << ' ';
3475 PrintLLVMName(Out, Arg);
3476 }
Chris Lattner00950542001-06-06 20:29:01 +00003477}
3478
Misha Brukmanab5c6002004-03-02 00:22:19 +00003479/// printBasicBlock - This member is called for each basic block in a method.
Chris Lattnerc1824992001-10-29 16:05:51 +00003480void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Nick Lewycky280a6e62008-04-25 16:53:59 +00003481 if (BB->hasName()) { // Print out the label if it exists...
Chris Lattnerc97536e2008-08-17 04:40:13 +00003482 Out << "\n";
Daniel Dunbar03d76512009-07-25 23:55:21 +00003483 PrintLLVMName(Out, BB->getName(), LabelPrefix);
Chris Lattnerc97536e2008-08-17 04:40:13 +00003484 Out << ':';
Nick Lewycky280a6e62008-04-25 16:53:59 +00003485 } else if (!BB->use_empty()) { // Don't print block # of no uses...
Bill Wendling5d7a5a42011-04-10 23:18:04 +00003486 Out << "\n; <label>:";
Chris Lattner22379bc2007-01-11 03:54:27 +00003487 int Slot = Machine.getLocalSlot(BB);
Chris Lattner69566452004-06-09 19:41:19 +00003488 if (Slot != -1)
Evgeniy Stepanovca14b572016-01-27 21:53:08 +00003489 Out << Slot << ":";
Chris Lattner69566452004-06-09 19:41:19 +00003490 else
Misha Brukman0313e0b2004-06-21 21:53:56 +00003491 Out << "<badref>";
Chris Lattner061269b2002-10-02 19:38:55 +00003492 }
Chris Lattner4e4d8622003-11-20 00:09:43 +00003493
Craig Topperec0f0bc2014-04-09 06:08:46 +00003494 if (!BB->getParent()) {
Chris Lattner8f4b1ec2009-08-17 15:48:08 +00003495 Out.PadToColumn(50);
Dan Gohman683e9222009-08-12 17:23:50 +00003496 Out << "; Error: Block without parent!";
3497 } else if (BB != &BB->getParent()->getEntryBlock()) { // Not the entry block?
Chris Lattner91fb4072010-09-02 22:52:10 +00003498 // Output predecessors for the block.
Chris Lattner8f4b1ec2009-08-17 15:48:08 +00003499 Out.PadToColumn(50);
Dan Gohman683e9222009-08-12 17:23:50 +00003500 Out << ";";
Gabor Greif44424642010-03-25 23:25:28 +00003501 const_pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
Daniel Dunbara279bc32009-09-20 02:20:51 +00003502
Chris Lattnereb411292008-04-22 02:45:44 +00003503 if (PI == PE) {
3504 Out << " No predecessors!";
3505 } else {
Dan Gohman8dae1382008-09-14 17:21:12 +00003506 Out << " preds = ";
Chris Lattnereb411292008-04-22 02:45:44 +00003507 writeOperand(*PI, false);
3508 for (++PI; PI != PE; ++PI) {
Dan Gohman8dae1382008-09-14 17:21:12 +00003509 Out << ", ";
Chris Lattner2fcfdb72006-12-06 06:24:27 +00003510 writeOperand(*PI, false);
Chris Lattner40efcec2003-11-16 22:59:57 +00003511 }
Chris Lattner061269b2002-10-02 19:38:55 +00003512 }
Chris Lattner00950542001-06-06 20:29:01 +00003513 }
Misha Brukmanfd939082005-04-21 23:48:37 +00003514
Chris Lattnereb411292008-04-22 02:45:44 +00003515 Out << "\n";
Chris Lattner00950542001-06-06 20:29:01 +00003516
Misha Brukman0313e0b2004-06-21 21:53:56 +00003517 if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, Out);
Chris Lattner95e5a2c2003-10-30 23:41:03 +00003518
Chris Lattner007377f2001-09-07 16:36:04 +00003519 // Output all of the instructions in the basic block...
Benjamin Kramere96e21f2016-06-26 14:10:56 +00003520 for (const Instruction &I : *BB) {
3521 printInstructionLine(I);
Dan Gohmanbeca6892009-07-13 18:27:59 +00003522 }
Chris Lattner9f717ef2004-03-08 18:51:45 +00003523
Misha Brukman0313e0b2004-06-21 21:53:56 +00003524 if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, Out);
Chris Lattner00950542001-06-06 20:29:01 +00003525}
3526
Daniel Malead0fef322013-05-08 20:38:31 +00003527/// printInstructionLine - Print an instruction and a newline character.
3528void AssemblyWriter::printInstructionLine(const Instruction &I) {
3529 printInstruction(I);
3530 Out << '\n';
3531}
3532
Igor Laevskya5f2faf2015-05-05 13:20:42 +00003533/// printGCRelocateComment - print comment after call to the gc.relocate
3534/// intrinsic indicating base and derived pointer names.
Manuel Jacob397864c2016-01-05 04:03:00 +00003535void AssemblyWriter::printGCRelocateComment(const GCRelocateInst &Relocate) {
Igor Laevskya5f2faf2015-05-05 13:20:42 +00003536 Out << " ; (";
Manuel Jacob397864c2016-01-05 04:03:00 +00003537 writeOperand(Relocate.getBasePtr(), false);
Igor Laevskya5f2faf2015-05-05 13:20:42 +00003538 Out << ", ";
Manuel Jacob397864c2016-01-05 04:03:00 +00003539 writeOperand(Relocate.getDerivedPtr(), false);
Igor Laevskya5f2faf2015-05-05 13:20:42 +00003540 Out << ")";
3541}
3542
Misha Brukmanab5c6002004-03-02 00:22:19 +00003543/// printInfoComment - Print a little comment after the instruction indicating
3544/// which slot it occupies.
Chris Lattner7e708292002-06-25 16:13:24 +00003545void AssemblyWriter::printInfoComment(const Value &V) {
Manuel Jacob397864c2016-01-05 04:03:00 +00003546 if (const auto *Relocate = dyn_cast<GCRelocateInst>(&V))
3547 printGCRelocateComment(*Relocate);
Igor Laevskya5f2faf2015-05-05 13:20:42 +00003548
Bill Wendling63405492013-04-16 20:55:47 +00003549 if (AnnotationWriter)
Dan Gohman7a5666e2010-02-10 20:41:46 +00003550 AnnotationWriter->printInfoComment(V, Out);
Chris Lattnere02fa852001-10-13 06:42:36 +00003551}
3552
Alexander Richardson47ff67b2018-08-23 09:25:17 +00003553static void maybePrintCallAddrSpace(const Value *Operand, const Instruction *I,
3554 raw_ostream &Out) {
3555 // We print the address space of the call if it is non-zero.
3556 unsigned CallAddrSpace = Operand->getType()->getPointerAddressSpace();
3557 bool PrintAddrSpace = CallAddrSpace != 0;
3558 if (!PrintAddrSpace) {
3559 const Module *Mod = getModuleFromVal(I);
3560 // We also print it if it is zero but not equal to the program address space
3561 // or if we can't find a valid Module* to make it possible to parse
3562 // the resulting file even without a datalayout string.
3563 if (!Mod || Mod->getDataLayout().getProgramAddressSpace() != 0)
3564 PrintAddrSpace = true;
3565 }
3566 if (PrintAddrSpace)
3567 Out << " addrspace(" << CallAddrSpace << ")";
3568}
3569
Reid Spencer3a9ec242006-08-28 01:02:49 +00003570// This member is called for each Instruction in a function..
Chris Lattner7e708292002-06-25 16:13:24 +00003571void AssemblyWriter::printInstruction(const Instruction &I) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00003572 if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, Out);
Chris Lattner95e5a2c2003-10-30 23:41:03 +00003573
Dan Gohman3845e502009-08-12 23:32:33 +00003574 // Print out indentation for an instruction.
Dan Gohman01889ca2009-08-13 01:41:52 +00003575 Out << " ";
Chris Lattner00950542001-06-06 20:29:01 +00003576
3577 // Print out name if it exists...
Chris Lattnerc97536e2008-08-17 04:40:13 +00003578 if (I.hasName()) {
3579 PrintLLVMName(Out, &I);
3580 Out << " = ";
Chris Lattner4ee93c42009-12-29 07:25:48 +00003581 } else if (!I.getType()->isVoidTy()) {
Chris Lattner828db8a2008-08-29 17:19:30 +00003582 // Print out the def slot taken.
3583 int SlotNum = Machine.getLocalSlot(&I);
3584 if (SlotNum == -1)
3585 Out << "<badref> = ";
3586 else
3587 Out << '%' << SlotNum << " = ";
Chris Lattnerc97536e2008-08-17 04:40:13 +00003588 }
Andrew Trick18801ec2011-09-30 19:48:58 +00003589
Reid Kleckner710c1a42014-04-24 20:14:34 +00003590 if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
3591 if (CI->isMustTailCall())
3592 Out << "musttail ";
3593 else if (CI->isTailCall())
3594 Out << "tail ";
Akira Hatanakac35973b2015-11-06 23:55:38 +00003595 else if (CI->isNoTailCall())
3596 Out << "notail ";
Reid Kleckner710c1a42014-04-24 20:14:34 +00003597 }
Chris Lattnere5e475e2003-09-08 17:45:59 +00003598
Chris Lattner00950542001-06-06 20:29:01 +00003599 // Print out the opcode...
Misha Brukman0313e0b2004-06-21 21:53:56 +00003600 Out << I.getOpcodeName();
Chris Lattner00950542001-06-06 20:29:01 +00003601
Eli Friedmanf03bb262011-08-12 22:50:01 +00003602 // If this is an atomic load or store, print out the atomic marker.
3603 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isAtomic()) ||
3604 (isa<StoreInst>(I) && cast<StoreInst>(I).isAtomic()))
3605 Out << " atomic";
3606
Tim Northover8f2a85e2014-06-13 14:24:07 +00003607 if (isa<AtomicCmpXchgInst>(I) && cast<AtomicCmpXchgInst>(I).isWeak())
3608 Out << " weak";
3609
Eli Friedmanf03bb262011-08-12 22:50:01 +00003610 // If this is a volatile operation, print out the volatile marker.
3611 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) ||
3612 (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile()) ||
3613 (isa<AtomicCmpXchgInst>(I) && cast<AtomicCmpXchgInst>(I).isVolatile()) ||
3614 (isa<AtomicRMWInst>(I) && cast<AtomicRMWInst>(I).isVolatile()))
3615 Out << " volatile";
3616
Dan Gohman59858cf2009-07-27 16:11:46 +00003617 // Print out optimization information.
3618 WriteOptimizationInfo(Out, &I);
3619
Reid Spencer74f16422006-12-03 06:27:29 +00003620 // Print out the compare instruction predicates
Nate Begemanac80ade2008-05-12 19:01:56 +00003621 if (const CmpInst *CI = dyn_cast<CmpInst>(&I))
Tim Northover3ed44cd2016-08-17 20:25:25 +00003622 Out << ' ' << CmpInst::getPredicateName(CI->getPredicate());
Reid Spencer74f16422006-12-03 06:27:29 +00003623
Eli Friedmanff030482011-07-28 21:48:00 +00003624 // Print out the atomicrmw operation
3625 if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(&I))
Matt Arsenault2c2c51b2018-10-02 23:44:11 +00003626 Out << ' ' << AtomicRMWInst::getOperationName(RMWI->getOperation());
Eli Friedmanff030482011-07-28 21:48:00 +00003627
Chris Lattner00950542001-06-06 20:29:01 +00003628 // Print out the type of the operands...
Craig Topperec0f0bc2014-04-09 06:08:46 +00003629 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : nullptr;
Chris Lattner00950542001-06-06 20:29:01 +00003630
3631 // Special case conditional branches to swizzle the condition out to the front
Gabor Greifccd27fb2009-02-09 15:45:06 +00003632 if (isa<BranchInst>(I) && cast<BranchInst>(I).isConditional()) {
David Blaikief12b3792013-02-11 01:16:51 +00003633 const BranchInst &BI(cast<BranchInst>(I));
Dan Gohman8dae1382008-09-14 17:21:12 +00003634 Out << ' ';
Gabor Greifccd27fb2009-02-09 15:45:06 +00003635 writeOperand(BI.getCondition(), true);
Dan Gohman8dae1382008-09-14 17:21:12 +00003636 Out << ", ";
Gabor Greifccd27fb2009-02-09 15:45:06 +00003637 writeOperand(BI.getSuccessor(0), true);
Dan Gohman8dae1382008-09-14 17:21:12 +00003638 Out << ", ";
Gabor Greifccd27fb2009-02-09 15:45:06 +00003639 writeOperand(BI.getSuccessor(1), true);
Chris Lattner00950542001-06-06 20:29:01 +00003640
Chris Lattner94dc1f22002-04-13 18:34:38 +00003641 } else if (isa<SwitchInst>(I)) {
David Blaikief12b3792013-02-11 01:16:51 +00003642 const SwitchInst& SI(cast<SwitchInst>(I));
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003643 // Special case switch instruction to get formatting nice and correct.
Dan Gohman8dae1382008-09-14 17:21:12 +00003644 Out << ' ';
Eli Friedmanbb5a7442011-09-29 20:21:17 +00003645 writeOperand(SI.getCondition(), true);
Dan Gohman8dae1382008-09-14 17:21:12 +00003646 Out << ", ";
Eli Friedmanbb5a7442011-09-29 20:21:17 +00003647 writeOperand(SI.getDefaultDest(), true);
Chris Lattnerab49ee72008-08-23 22:52:27 +00003648 Out << " [";
Chandler Carruthddfada22017-04-12 07:27:28 +00003649 for (auto Case : SI.cases()) {
Dan Gohman01889ca2009-08-13 01:41:52 +00003650 Out << "\n ";
Chandler Carruthddfada22017-04-12 07:27:28 +00003651 writeOperand(Case.getCaseValue(), true);
Dan Gohman8dae1382008-09-14 17:21:12 +00003652 Out << ", ";
Chandler Carruthddfada22017-04-12 07:27:28 +00003653 writeOperand(Case.getCaseSuccessor(), true);
Chris Lattner00950542001-06-06 20:29:01 +00003654 }
Dan Gohman01889ca2009-08-13 01:41:52 +00003655 Out << "\n ]";
Chris Lattnerab21db72009-10-28 00:19:10 +00003656 } else if (isa<IndirectBrInst>(I)) {
3657 // Special case indirectbr instruction to get formatting nice and correct.
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003658 Out << ' ';
3659 writeOperand(Operand, true);
Dan Gohman0ed1f422009-10-30 02:01:10 +00003660 Out << ", [";
Andrew Trick18801ec2011-09-30 19:48:58 +00003661
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003662 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) {
3663 if (i != 1)
3664 Out << ", ";
3665 writeOperand(I.getOperand(i), true);
3666 }
3667 Out << ']';
Jay Foadc1371202011-06-20 14:18:48 +00003668 } else if (const PHINode *PN = dyn_cast<PHINode>(&I)) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00003669 Out << ' ';
Chris Lattner0f7364b2009-02-28 21:26:53 +00003670 TypePrinter.print(I.getType(), Out);
Misha Brukman0313e0b2004-06-21 21:53:56 +00003671 Out << ' ';
Chris Lattner00950542001-06-06 20:29:01 +00003672
Jay Foadc1371202011-06-20 14:18:48 +00003673 for (unsigned op = 0, Eop = PN->getNumIncomingValues(); op < Eop; ++op) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00003674 if (op) Out << ", ";
Dan Gohman8dae1382008-09-14 17:21:12 +00003675 Out << "[ ";
Jay Foadc1371202011-06-20 14:18:48 +00003676 writeOperand(PN->getIncomingValue(op), false); Out << ", ";
3677 writeOperand(PN->getIncomingBlock(op), false); Out << " ]";
Chris Lattnerc24d2082001-06-11 15:04:20 +00003678 }
Dan Gohman995be7d2008-05-31 19:12:39 +00003679 } else if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(&I)) {
Dan Gohman8dae1382008-09-14 17:21:12 +00003680 Out << ' ';
Dan Gohman995be7d2008-05-31 19:12:39 +00003681 writeOperand(I.getOperand(0), true);
3682 for (const unsigned *i = EVI->idx_begin(), *e = EVI->idx_end(); i != e; ++i)
3683 Out << ", " << *i;
3684 } else if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(&I)) {
Dan Gohman8dae1382008-09-14 17:21:12 +00003685 Out << ' ';
3686 writeOperand(I.getOperand(0), true); Out << ", ";
Dan Gohman995be7d2008-05-31 19:12:39 +00003687 writeOperand(I.getOperand(1), true);
3688 for (const unsigned *i = IVI->idx_begin(), *e = IVI->idx_end(); i != e; ++i)
3689 Out << ", " << *i;
Bill Wendlinge6e88262011-08-12 20:24:12 +00003690 } else if (const LandingPadInst *LPI = dyn_cast<LandingPadInst>(&I)) {
3691 Out << ' ';
3692 TypePrinter.print(I.getType(), Out);
David Majnemercc714e22015-06-17 20:52:32 +00003693 if (LPI->isCleanup() || LPI->getNumClauses() != 0)
3694 Out << '\n';
Bill Wendlinge6e88262011-08-12 20:24:12 +00003695
3696 if (LPI->isCleanup())
3697 Out << " cleanup";
3698
3699 for (unsigned i = 0, e = LPI->getNumClauses(); i != e; ++i) {
3700 if (i != 0 || LPI->isCleanup()) Out << "\n";
3701 if (LPI->isCatch(i))
3702 Out << " catch ";
3703 else
3704 Out << " filter ";
3705
3706 writeOperand(LPI->getClause(i), true);
3707 }
David Majnemer8cec2f22015-12-12 05:38:55 +00003708 } else if (const auto *CatchSwitch = dyn_cast<CatchSwitchInst>(&I)) {
3709 Out << " within ";
3710 writeOperand(CatchSwitch->getParentPad(), /*PrintType=*/false);
David Majnemer4a45f082015-07-31 17:58:14 +00003711 Out << " [";
David Majnemer8cec2f22015-12-12 05:38:55 +00003712 unsigned Op = 0;
3713 for (const BasicBlock *PadBB : CatchSwitch->handlers()) {
3714 if (Op > 0)
3715 Out << ", ";
3716 writeOperand(PadBB, /*PrintType=*/true);
3717 ++Op;
3718 }
3719 Out << "] unwind ";
3720 if (const BasicBlock *UnwindDest = CatchSwitch->getUnwindDest())
3721 writeOperand(UnwindDest, /*PrintType=*/true);
3722 else
3723 Out << "to caller";
3724 } else if (const auto *FPI = dyn_cast<FuncletPadInst>(&I)) {
3725 Out << " within ";
3726 writeOperand(FPI->getParentPad(), /*PrintType=*/false);
3727 Out << " [";
3728 for (unsigned Op = 0, NumOps = FPI->getNumArgOperands(); Op < NumOps;
David Majnemer4a45f082015-07-31 17:58:14 +00003729 ++Op) {
3730 if (Op > 0)
3731 Out << ", ";
David Majnemer8cec2f22015-12-12 05:38:55 +00003732 writeOperand(FPI->getArgOperand(Op), /*PrintType=*/true);
David Majnemer4a45f082015-07-31 17:58:14 +00003733 }
David Majnemer8cec2f22015-12-12 05:38:55 +00003734 Out << ']';
Devang Patel57ef4f42008-02-23 00:35:18 +00003735 } else if (isa<ReturnInst>(I) && !Operand) {
3736 Out << " void";
David Majnemerde17e772015-08-15 02:46:08 +00003737 } else if (const auto *CRI = dyn_cast<CatchReturnInst>(&I)) {
David Majnemer8cec2f22015-12-12 05:38:55 +00003738 Out << " from ";
3739 writeOperand(CRI->getOperand(0), /*PrintType=*/false);
David Majnemerde17e772015-08-15 02:46:08 +00003740
3741 Out << " to ";
David Majnemer8cec2f22015-12-12 05:38:55 +00003742 writeOperand(CRI->getOperand(1), /*PrintType=*/true);
David Majnemer4a45f082015-07-31 17:58:14 +00003743 } else if (const auto *CRI = dyn_cast<CleanupReturnInst>(&I)) {
David Majnemer8cec2f22015-12-12 05:38:55 +00003744 Out << " from ";
3745 writeOperand(CRI->getOperand(0), /*PrintType=*/false);
David Majnemer4a45f082015-07-31 17:58:14 +00003746
3747 Out << " unwind ";
3748 if (CRI->hasUnwindDest())
David Majnemer8cec2f22015-12-12 05:38:55 +00003749 writeOperand(CRI->getOperand(1), /*PrintType=*/true);
Joseph Tremoulet226889e2015-09-03 09:09:43 +00003750 else
3751 Out << "to caller";
Chris Lattnerd5118982005-05-06 20:26:43 +00003752 } else if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
3753 // Print the calling convention being used.
Micah Villmowd3766df2012-09-13 15:11:12 +00003754 if (CI->getCallingConv() != CallingConv::C) {
3755 Out << " ";
3756 PrintCallingConv(CI->getCallingConv(), Out);
Chris Lattnerd5118982005-05-06 20:26:43 +00003757 }
3758
Gabor Greif7bbdf0c2010-06-23 13:09:06 +00003759 Operand = CI->getCalledValue();
Ahmed Bougacha9cccec82016-12-21 23:26:13 +00003760 FunctionType *FTy = CI->getFunctionType();
Chris Lattner1afcace2011-07-09 17:41:24 +00003761 Type *RetTy = FTy->getReturnType();
Reid Kleckner67077702017-03-21 16:57:19 +00003762 const AttributeList &PAL = CI->getAttributes();
Chris Lattner268de042001-11-06 21:28:12 +00003763
Reid Kleckner67077702017-03-21 16:57:19 +00003764 if (PAL.hasAttributes(AttributeList::ReturnIndex))
3765 Out << ' ' << PAL.getAsString(AttributeList::ReturnIndex);
Devang Patel652203f2008-09-29 20:49:50 +00003766
Alexander Richardson47ff67b2018-08-23 09:25:17 +00003767 // Only print addrspace(N) if necessary:
3768 maybePrintCallAddrSpace(Operand, &I, Out);
3769
Chris Lattner7a012292003-08-05 15:34:45 +00003770 // If possible, print out the short form of the call instruction. We can
Chris Lattnerb5794002002-04-07 22:49:37 +00003771 // only do this if the first argument is a pointer to a nonvararg function,
Chris Lattner7a012292003-08-05 15:34:45 +00003772 // and if the return type is not a pointer to a function.
Chris Lattner268de042001-11-06 21:28:12 +00003773 //
Dan Gohman8dae1382008-09-14 17:21:12 +00003774 Out << ' ';
David Blaikie32b845d2015-04-16 23:24:18 +00003775 TypePrinter.print(FTy->isVarArg() ? FTy : RetTy, Out);
3776 Out << ' ';
3777 writeOperand(Operand, false);
Misha Brukman0313e0b2004-06-21 21:53:56 +00003778 Out << '(';
Gabor Greif7bbdf0c2010-06-23 13:09:06 +00003779 for (unsigned op = 0, Eop = CI->getNumArgOperands(); op < Eop; ++op) {
3780 if (op > 0)
Dan Gohman8dae1382008-09-14 17:21:12 +00003781 Out << ", ";
Reid Kleckner331b9af2017-04-28 18:37:16 +00003782 writeParamOperand(CI->getArgOperand(op), PAL.getParamAttributes(op));
Chris Lattner00950542001-06-06 20:29:01 +00003783 }
Reid Kleckner44b3a0b2014-08-26 00:33:28 +00003784
3785 // Emit an ellipsis if this is a musttail call in a vararg function. This
3786 // is only to aid readability, musttail calls forward varargs by default.
3787 if (CI->isMustTailCall() && CI->getParent() &&
3788 CI->getParent()->getParent() &&
3789 CI->getParent()->getParent()->isVarArg())
3790 Out << ", ...";
3791
Dan Gohman8dae1382008-09-14 17:21:12 +00003792 Out << ')';
Reid Kleckner67077702017-03-21 16:57:19 +00003793 if (PAL.hasAttributes(AttributeList::FunctionIndex))
Bill Wendling351b7a12013-02-22 09:09:42 +00003794 Out << " #" << Machine.getAttributeGroupSlot(PAL.getFnAttributes());
Sanjoy Dasf70eb722015-09-24 23:34:52 +00003795
3796 writeOperandBundles(CI);
Chris Lattner7e708292002-06-25 16:13:24 +00003797 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
Gabor Greifc9f75002010-03-24 13:21:49 +00003798 Operand = II->getCalledValue();
Ahmed Bougacha9cccec82016-12-21 23:26:13 +00003799 FunctionType *FTy = II->getFunctionType();
Chris Lattner1afcace2011-07-09 17:41:24 +00003800 Type *RetTy = FTy->getReturnType();
Reid Kleckner67077702017-03-21 16:57:19 +00003801 const AttributeList &PAL = II->getAttributes();
Chris Lattner7a012292003-08-05 15:34:45 +00003802
Chris Lattnerd5118982005-05-06 20:26:43 +00003803 // Print the calling convention being used.
Micah Villmowd3766df2012-09-13 15:11:12 +00003804 if (II->getCallingConv() != CallingConv::C) {
3805 Out << " ";
3806 PrintCallingConv(II->getCallingConv(), Out);
Chris Lattnerd5118982005-05-06 20:26:43 +00003807 }
3808
Reid Kleckner67077702017-03-21 16:57:19 +00003809 if (PAL.hasAttributes(AttributeList::ReturnIndex))
3810 Out << ' ' << PAL.getAsString(AttributeList::ReturnIndex);
Devang Patel652203f2008-09-29 20:49:50 +00003811
Alexander Richardson47ff67b2018-08-23 09:25:17 +00003812 // Only print addrspace(N) if necessary:
3813 maybePrintCallAddrSpace(Operand, &I, Out);
3814
Chris Lattner7a012292003-08-05 15:34:45 +00003815 // If possible, print out the short form of the invoke instruction. We can
3816 // only do this if the first argument is a pointer to a nonvararg function,
3817 // and if the return type is not a pointer to a function.
3818 //
Dan Gohman2b6c3d92008-10-15 18:02:08 +00003819 Out << ' ';
David Blaikiee41f3842015-04-24 19:32:54 +00003820 TypePrinter.print(FTy->isVarArg() ? FTy : RetTy, Out);
3821 Out << ' ';
3822 writeOperand(Operand, false);
Misha Brukman0313e0b2004-06-21 21:53:56 +00003823 Out << '(';
Gabor Greif7bbdf0c2010-06-23 13:09:06 +00003824 for (unsigned op = 0, Eop = II->getNumArgOperands(); op < Eop; ++op) {
Gabor Greifc9f75002010-03-24 13:21:49 +00003825 if (op)
Dan Gohman8dae1382008-09-14 17:21:12 +00003826 Out << ", ";
Reid Kleckner331b9af2017-04-28 18:37:16 +00003827 writeParamOperand(II->getArgOperand(op), PAL.getParamAttributes(op));
Chris Lattnere02fa852001-10-13 06:42:36 +00003828 }
3829
Dan Gohman8dae1382008-09-14 17:21:12 +00003830 Out << ')';
Reid Kleckner67077702017-03-21 16:57:19 +00003831 if (PAL.hasAttributes(AttributeList::FunctionIndex))
Bill Wendling351b7a12013-02-22 09:09:42 +00003832 Out << " #" << Machine.getAttributeGroupSlot(PAL.getFnAttributes());
Devang Patel19c87462008-09-26 22:53:05 +00003833
Sanjoy Dasf70eb722015-09-24 23:34:52 +00003834 writeOperandBundles(II);
3835
Dan Gohman01889ca2009-08-13 01:41:52 +00003836 Out << "\n to ";
Chris Lattnere02fa852001-10-13 06:42:36 +00003837 writeOperand(II->getNormalDest(), true);
Dan Gohman8dae1382008-09-14 17:21:12 +00003838 Out << " unwind ";
Chris Lattneraeb2a1d2004-02-08 21:44:31 +00003839 writeOperand(II->getUnwindDest(), true);
Victor Hernandez7b929da2009-10-23 21:09:37 +00003840 } else if (const AllocaInst *AI = dyn_cast<AllocaInst>(&I)) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00003841 Out << ' ';
Reid Kleckner1d167022014-01-25 01:24:06 +00003842 if (AI->isUsedWithInAlloca())
David Majnemer39a09d22014-03-09 06:41:58 +00003843 Out << "inalloca ";
Manman Ren4bda8822016-04-01 21:41:15 +00003844 if (AI->isSwiftError())
3845 Out << "swifterror ";
David Majnemer39a09d22014-03-09 06:41:58 +00003846 TypePrinter.print(AI->getAllocatedType(), Out);
Duncan P. N. Exon Smith95ff6562015-03-13 19:30:44 +00003847
3848 // Explicitly write the array size if the code is broken, if it's an array
3849 // allocation, or if the type is not canonical for scalar allocations. The
3850 // latter case prevents the type from mutating when round-tripping through
3851 // assembly.
3852 if (!AI->getArraySize() || AI->isArrayAllocation() ||
3853 !AI->getArraySize()->getType()->isIntegerTy(32)) {
Dan Gohman8dae1382008-09-14 17:21:12 +00003854 Out << ", ";
Chris Lattner94dc1f22002-04-13 18:34:38 +00003855 writeOperand(AI->getArraySize(), true);
Chris Lattner00950542001-06-06 20:29:01 +00003856 }
Nate Begeman14b05292005-11-05 09:21:28 +00003857 if (AI->getAlignment()) {
Chris Lattner9fad0b92005-11-05 21:20:34 +00003858 Out << ", align " << AI->getAlignment();
Nate Begeman14b05292005-11-05 09:21:28 +00003859 }
Matt Arsenaulte0b3c332017-04-10 22:27:50 +00003860
3861 unsigned AddrSpace = AI->getType()->getAddressSpace();
3862 if (AddrSpace != 0) {
3863 Out << ", addrspace(" << AddrSpace << ')';
3864 }
Chris Lattnere02fa852001-10-13 06:42:36 +00003865 } else if (isa<CastInst>(I)) {
Dan Gohman8dae1382008-09-14 17:21:12 +00003866 if (Operand) {
3867 Out << ' ';
3868 writeOperand(Operand, true); // Work with broken code
3869 }
Misha Brukman0313e0b2004-06-21 21:53:56 +00003870 Out << " to ";
Chris Lattner0f7364b2009-02-28 21:26:53 +00003871 TypePrinter.print(I.getType(), Out);
Chris Lattner4d45bd02003-10-18 05:57:43 +00003872 } else if (isa<VAArgInst>(I)) {
Dan Gohman8dae1382008-09-14 17:21:12 +00003873 if (Operand) {
3874 Out << ' ';
3875 writeOperand(Operand, true); // Work with broken code
3876 }
Misha Brukman0313e0b2004-06-21 21:53:56 +00003877 Out << ", ";
Chris Lattner0f7364b2009-02-28 21:26:53 +00003878 TypePrinter.print(I.getType(), Out);
3879 } else if (Operand) { // Print the normal way.
David Blaikie7c9c6ed2015-02-27 21:17:42 +00003880 if (const auto *GEP = dyn_cast<GetElementPtrInst>(&I)) {
David Blaikie198d8ba2015-02-27 19:29:02 +00003881 Out << ' ';
3882 TypePrinter.print(GEP->getSourceElementType(), Out);
3883 Out << ',';
David Blaikie7c9c6ed2015-02-27 21:17:42 +00003884 } else if (const auto *LI = dyn_cast<LoadInst>(&I)) {
3885 Out << ' ';
3886 TypePrinter.print(LI->getType(), Out);
Benjamin Kramer6cc6b412015-03-02 15:24:41 +00003887 Out << ',';
David Blaikie198d8ba2015-02-27 19:29:02 +00003888 }
Chris Lattner00950542001-06-06 20:29:01 +00003889
Misha Brukmanfd939082005-04-21 23:48:37 +00003890 // PrintAllTypes - Instructions who have operands of all the same type
Chris Lattner00950542001-06-06 20:29:01 +00003891 // omit the type from all but the first operand. If the instruction has
3892 // different type operands (for example br), then they are all printed.
3893 bool PrintAllTypes = false;
Chris Lattner1afcace2011-07-09 17:41:24 +00003894 Type *TheType = Operand->getType();
Chris Lattner00950542001-06-06 20:29:01 +00003895
Reid Spencerebe57e32007-02-02 13:54:55 +00003896 // Select, Store and ShuffleVector always print all types.
Devang Patel64947682008-03-04 22:05:14 +00003897 if (isa<SelectInst>(I) || isa<StoreInst>(I) || isa<ShuffleVectorInst>(I)
3898 || isa<ReturnInst>(I)) {
Chris Lattnerffd9bf42003-04-16 20:20:02 +00003899 PrintAllTypes = true;
3900 } else {
3901 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
3902 Operand = I.getOperand(i);
Nuno Lopes6ad2b2a2009-01-15 18:40:57 +00003903 // note that Operand shouldn't be null, but the test helps make dump()
3904 // more tolerant of malformed IR
Nuno Lopesa8c78a92009-01-14 17:51:41 +00003905 if (Operand && Operand->getType() != TheType) {
Chris Lattnerffd9bf42003-04-16 20:20:02 +00003906 PrintAllTypes = true; // We have differing types! Print them all!
3907 break;
3908 }
Chris Lattner00950542001-06-06 20:29:01 +00003909 }
3910 }
Misha Brukmanfd939082005-04-21 23:48:37 +00003911
Chris Lattnerc1824992001-10-29 16:05:51 +00003912 if (!PrintAllTypes) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00003913 Out << ' ';
Chris Lattner0f7364b2009-02-28 21:26:53 +00003914 TypePrinter.print(TheType, Out);
Chris Lattnerc1824992001-10-29 16:05:51 +00003915 }
Chris Lattner00950542001-06-06 20:29:01 +00003916
Dan Gohman8dae1382008-09-14 17:21:12 +00003917 Out << ' ';
Chris Lattner7e708292002-06-25 16:13:24 +00003918 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
Dan Gohman8dae1382008-09-14 17:21:12 +00003919 if (i) Out << ", ";
Chris Lattner7e708292002-06-25 16:13:24 +00003920 writeOperand(I.getOperand(i), PrintAllTypes);
Chris Lattner00950542001-06-06 20:29:01 +00003921 }
3922 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003923
Eli Friedman21006d42011-08-09 23:02:53 +00003924 // Print atomic ordering/alignment for memory operations
3925 if (const LoadInst *LI = dyn_cast<LoadInst>(&I)) {
3926 if (LI->isAtomic())
Konstantin Zhuravlyov8f856852017-07-11 22:23:00 +00003927 writeAtomic(LI->getContext(), LI->getOrdering(), LI->getSyncScopeID());
Eli Friedman21006d42011-08-09 23:02:53 +00003928 if (LI->getAlignment())
3929 Out << ", align " << LI->getAlignment();
3930 } else if (const StoreInst *SI = dyn_cast<StoreInst>(&I)) {
3931 if (SI->isAtomic())
Konstantin Zhuravlyov8f856852017-07-11 22:23:00 +00003932 writeAtomic(SI->getContext(), SI->getOrdering(), SI->getSyncScopeID());
Eli Friedman21006d42011-08-09 23:02:53 +00003933 if (SI->getAlignment())
3934 Out << ", align " << SI->getAlignment();
Eli Friedmanff030482011-07-28 21:48:00 +00003935 } else if (const AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(&I)) {
Konstantin Zhuravlyov8f856852017-07-11 22:23:00 +00003936 writeAtomicCmpXchg(CXI->getContext(), CXI->getSuccessOrdering(),
3937 CXI->getFailureOrdering(), CXI->getSyncScopeID());
Eli Friedmanff030482011-07-28 21:48:00 +00003938 } else if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(&I)) {
Konstantin Zhuravlyov8f856852017-07-11 22:23:00 +00003939 writeAtomic(RMWI->getContext(), RMWI->getOrdering(),
3940 RMWI->getSyncScopeID());
Eli Friedman47f35132011-07-25 23:16:38 +00003941 } else if (const FenceInst *FI = dyn_cast<FenceInst>(&I)) {
Konstantin Zhuravlyov8f856852017-07-11 22:23:00 +00003942 writeAtomic(FI->getContext(), FI->getOrdering(), FI->getSyncScopeID());
Christopher Lamb43c7f372007-04-22 19:24:39 +00003943 }
Chris Lattner00950542001-06-06 20:29:01 +00003944
Chris Lattner7d05c462009-12-28 20:10:43 +00003945 // Print Metadata info.
Duncan P. N. Exon Smith5bf8ade2014-11-11 21:30:22 +00003946 SmallVector<std::pair<unsigned, MDNode *>, 4> InstMD;
Nick Lewyckyfa0c54e2010-02-25 06:53:04 +00003947 I.getAllMetadata(InstMD);
Duncan P. N. Exon Smith3be41472015-04-24 21:06:21 +00003948 printMetadataAttachments(InstMD, ", ");
Duncan P. N. Exon Smith61317d52015-04-24 20:59:52 +00003949
3950 // Print a nice comment.
Chris Lattnere02fa852001-10-13 06:42:36 +00003951 printInfoComment(I);
Chris Lattner00950542001-06-06 20:29:01 +00003952}
3953
Duncan P. N. Exon Smith61317d52015-04-24 20:59:52 +00003954void AssemblyWriter::printMetadataAttachments(
Duncan P. N. Exon Smith3be41472015-04-24 21:06:21 +00003955 const SmallVectorImpl<std::pair<unsigned, MDNode *>> &MDs,
3956 StringRef Separator) {
Duncan P. N. Exon Smith61317d52015-04-24 20:59:52 +00003957 if (MDs.empty())
3958 return;
3959
Duncan P. N. Exon Smithc8be2442015-04-24 21:03:05 +00003960 if (MDNames.empty())
Mehdi Aminif4526752016-01-07 20:14:30 +00003961 MDs[0].second->getContext().getMDKindNames(MDNames);
Duncan P. N. Exon Smithc8be2442015-04-24 21:03:05 +00003962
Duncan P. N. Exon Smith61317d52015-04-24 20:59:52 +00003963 for (const auto &I : MDs) {
3964 unsigned Kind = I.first;
Duncan P. N. Exon Smith3be41472015-04-24 21:06:21 +00003965 Out << Separator;
Filipe Cabecinhas6af0f892015-06-02 21:25:08 +00003966 if (Kind < MDNames.size()) {
3967 Out << "!";
3968 printMetadataIdentifier(MDNames[Kind], Out);
3969 } else
Duncan P. N. Exon Smith3be41472015-04-24 21:06:21 +00003970 Out << "!<unknown kind #" << Kind << ">";
Duncan P. N. Exon Smith61317d52015-04-24 20:59:52 +00003971 Out << ' ';
3972 WriteAsOperandInternal(Out, I.second, &TypePrinter, &Machine, TheModule);
3973 }
3974}
3975
Daniel Malead0fef322013-05-08 20:38:31 +00003976void AssemblyWriter::writeMDNode(unsigned Slot, const MDNode *Node) {
Duncan P. N. Exon Smith1ef70ff2014-12-15 19:07:53 +00003977 Out << '!' << Slot << " = ";
Daniel Malead0fef322013-05-08 20:38:31 +00003978 printMDNodeBody(Node);
Duncan P. N. Exon Smith344ea292015-02-25 22:46:38 +00003979 Out << "\n";
Daniel Malead0fef322013-05-08 20:38:31 +00003980}
3981
Chris Lattner6e6b1802009-12-31 02:13:35 +00003982void AssemblyWriter::writeAllMDNodes() {
3983 SmallVector<const MDNode *, 16> Nodes;
Chris Lattner307c9892009-12-31 02:20:11 +00003984 Nodes.resize(Machine.mdn_size());
3985 for (SlotTracker::mdn_iterator I = Machine.mdn_begin(), E = Machine.mdn_end();
3986 I != E; ++I)
Chris Lattner6e6b1802009-12-31 02:13:35 +00003987 Nodes[I->second] = cast<MDNode>(I->first);
Andrew Trick18801ec2011-09-30 19:48:58 +00003988
Chris Lattner6e6b1802009-12-31 02:13:35 +00003989 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Daniel Malead0fef322013-05-08 20:38:31 +00003990 writeMDNode(i, Nodes[i]);
Chris Lattner6e6b1802009-12-31 02:13:35 +00003991 }
3992}
3993
3994void AssemblyWriter::printMDNodeBody(const MDNode *Node) {
Dan Gohman3bdfbf52010-07-20 23:55:01 +00003995 WriteMDNodeBodyInternal(Out, Node, &TypePrinter, &Machine, TheModule);
Chris Lattner6e6b1802009-12-31 02:13:35 +00003996}
Chris Lattner00950542001-06-06 20:29:01 +00003997
Bill Wendlingb29ce262013-02-11 08:43:33 +00003998void AssemblyWriter::writeAllAttributeGroups() {
Reid Kleckner06090402017-04-12 00:38:00 +00003999 std::vector<std::pair<AttributeSet, unsigned>> asVec;
Bill Wendlingb29ce262013-02-11 08:43:33 +00004000 asVec.resize(Machine.as_size());
4001
4002 for (SlotTracker::as_iterator I = Machine.as_begin(), E = Machine.as_end();
4003 I != E; ++I)
4004 asVec[I->second] = *I;
4005
Benjamin Kramere96e21f2016-06-26 14:10:56 +00004006 for (const auto &I : asVec)
4007 Out << "attributes #" << I.second << " = { "
Reid Kleckner06090402017-04-12 00:38:00 +00004008 << I.first.getAsString(true) << " }\n";
Bill Wendlingb29ce262013-02-11 08:43:33 +00004009}
4010
Duncan P. N. Exon Smith78388182014-08-19 21:30:15 +00004011void AssemblyWriter::printUseListOrder(const UseListOrder &Order) {
4012 bool IsInFunction = Machine.getFunction();
4013 if (IsInFunction)
4014 Out << " ";
4015
4016 Out << "uselistorder";
4017 if (const BasicBlock *BB =
4018 IsInFunction ? nullptr : dyn_cast<BasicBlock>(Order.V)) {
4019 Out << "_bb ";
4020 writeOperand(BB->getParent(), false);
4021 Out << ", ";
4022 writeOperand(BB, false);
4023 } else {
4024 Out << " ";
4025 writeOperand(Order.V, true);
4026 }
4027 Out << ", { ";
4028
4029 assert(Order.Shuffle.size() >= 2 && "Shuffle too small");
4030 Out << Order.Shuffle[0];
4031 for (unsigned I = 1, E = Order.Shuffle.size(); I != E; ++I)
4032 Out << ", " << Order.Shuffle[I];
4033 Out << " }\n";
4034}
4035
4036void AssemblyWriter::printUseLists(const Function *F) {
4037 auto hasMore =
4038 [&]() { return !UseListOrders.empty() && UseListOrders.back().F == F; };
4039 if (!hasMore())
4040 // Nothing to do.
4041 return;
4042
4043 Out << "\n; uselistorder directives\n";
4044 while (hasMore()) {
4045 printUseListOrder(UseListOrders.back());
4046 UseListOrders.pop_back();
4047 }
4048}
4049
Chris Lattner00950542001-06-06 20:29:01 +00004050//===----------------------------------------------------------------------===//
4051// External Interface declarations
4052//===----------------------------------------------------------------------===//
4053
George Burgess IV02e0bb62016-02-02 22:46:49 +00004054void Function::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW,
4055 bool ShouldPreserveUseListOrder,
4056 bool IsForDebug) const {
4057 SlotTracker SlotTable(this->getParent());
4058 formatted_raw_ostream OS(ROS);
4059 AssemblyWriter W(OS, SlotTable, this->getParent(), AAW,
4060 IsForDebug,
4061 ShouldPreserveUseListOrder);
4062 W.printFunction(this);
4063}
4064
Duncan P. N. Exon Smith8b376eb2015-04-15 02:12:41 +00004065void Module::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW,
Justin Bogner0adaaa32015-09-27 22:38:50 +00004066 bool ShouldPreserveUseListOrder, bool IsForDebug) const {
Chris Lattner0d9574a2008-08-19 04:26:57 +00004067 SlotTracker SlotTable(this);
Dan Gohman683e9222009-08-12 17:23:50 +00004068 formatted_raw_ostream OS(ROS);
Justin Bogner0adaaa32015-09-27 22:38:50 +00004069 AssemblyWriter W(OS, SlotTable, this, AAW, IsForDebug,
4070 ShouldPreserveUseListOrder);
Chris Lattnerbd72b322009-12-31 02:23:35 +00004071 W.printModule(this);
Chris Lattner00950542001-06-06 20:29:01 +00004072}
4073
Justin Bogner0adaaa32015-09-27 22:38:50 +00004074void NamedMDNode::print(raw_ostream &ROS, bool IsForDebug) const {
Dan Gohman17aa92c2010-07-21 23:38:33 +00004075 SlotTracker SlotTable(getParent());
4076 formatted_raw_ostream OS(ROS);
Justin Bogner0adaaa32015-09-27 22:38:50 +00004077 AssemblyWriter W(OS, SlotTable, getParent(), nullptr, IsForDebug);
Dan Gohman17aa92c2010-07-21 23:38:33 +00004078 W.printNamedMDNode(this);
4079}
4080
Duncan P. N. Exon Smith4ca56a62016-04-20 17:27:44 +00004081void NamedMDNode::print(raw_ostream &ROS, ModuleSlotTracker &MST,
4082 bool IsForDebug) const {
4083 Optional<SlotTracker> LocalST;
4084 SlotTracker *SlotTable;
4085 if (auto *ST = MST.getMachine())
4086 SlotTable = ST;
4087 else {
4088 LocalST.emplace(getParent());
4089 SlotTable = &*LocalST;
4090 }
4091
4092 formatted_raw_ostream OS(ROS);
4093 AssemblyWriter W(OS, *SlotTable, getParent(), nullptr, IsForDebug);
4094 W.printNamedMDNode(this);
4095}
4096
Justin Bogner0adaaa32015-09-27 22:38:50 +00004097void Comdat::print(raw_ostream &ROS, bool /*IsForDebug*/) const {
David Majnemerc8a11692014-06-27 18:19:56 +00004098 PrintLLVMName(ROS, getName(), ComdatPrefix);
4099 ROS << " = comdat ";
4100
4101 switch (getSelectionKind()) {
4102 case Comdat::Any:
4103 ROS << "any";
4104 break;
4105 case Comdat::ExactMatch:
4106 ROS << "exactmatch";
4107 break;
4108 case Comdat::Largest:
4109 ROS << "largest";
4110 break;
4111 case Comdat::NoDuplicates:
4112 ROS << "noduplicates";
4113 break;
4114 case Comdat::SameSize:
4115 ROS << "samesize";
4116 break;
4117 }
4118
4119 ROS << '\n';
4120}
4121
Quentin Colombetdbefd772016-03-07 22:32:42 +00004122void Type::print(raw_ostream &OS, bool /*IsForDebug*/, bool NoDetails) const {
Chris Lattner1afcace2011-07-09 17:41:24 +00004123 TypePrinting TP;
4124 TP.print(const_cast<Type*>(this), OS);
Andrew Trick18801ec2011-09-30 19:48:58 +00004125
Quentin Colombetdbefd772016-03-07 22:32:42 +00004126 if (NoDetails)
4127 return;
4128
Chris Lattner1afcace2011-07-09 17:41:24 +00004129 // If the type is a named struct type, print the body as well.
4130 if (StructType *STy = dyn_cast<StructType>(const_cast<Type*>(this)))
Chris Lattnerc4d0e9f2011-08-12 18:07:07 +00004131 if (!STy->isLiteral()) {
Chris Lattner1afcace2011-07-09 17:41:24 +00004132 OS << " = type ";
4133 TP.printStructBody(STy, OS);
4134 }
Chris Lattner75cf7cf2002-04-08 22:03:40 +00004135}
4136
Duncan P. N. Exon Smithf9f505e2015-03-14 20:19:36 +00004137static bool isReferencingMDNode(const Instruction &I) {
4138 if (const auto *CI = dyn_cast<CallInst>(&I))
4139 if (Function *F = CI->getCalledFunction())
Justin Lebar24dbd382016-07-28 23:58:15 +00004140 if (F->isIntrinsic())
Duncan P. N. Exon Smithf9f505e2015-03-14 20:19:36 +00004141 for (auto &Op : I.operands())
4142 if (auto *V = dyn_cast_or_null<MetadataAsValue>(Op))
4143 if (isa<MDNode>(V->getMetadata()))
4144 return true;
4145 return false;
4146}
4147
Justin Bogner0adaaa32015-09-27 22:38:50 +00004148void Value::print(raw_ostream &ROS, bool IsForDebug) const {
Duncan P. N. Exon Smith2a5fda92015-06-27 00:38:26 +00004149 bool ShouldInitializeAllMetadata = false;
4150 if (auto *I = dyn_cast<Instruction>(this))
4151 ShouldInitializeAllMetadata = isReferencingMDNode(*I);
4152 else if (isa<Function>(this) || isa<MetadataAsValue>(this))
4153 ShouldInitializeAllMetadata = true;
4154
4155 ModuleSlotTracker MST(getModuleFromVal(this), ShouldInitializeAllMetadata);
Justin Bogner0adaaa32015-09-27 22:38:50 +00004156 print(ROS, MST, IsForDebug);
Duncan P. N. Exon Smith2a5fda92015-06-27 00:38:26 +00004157}
4158
Justin Bogner0adaaa32015-09-27 22:38:50 +00004159void Value::print(raw_ostream &ROS, ModuleSlotTracker &MST,
4160 bool IsForDebug) const {
Dan Gohman1220e102009-08-12 20:56:03 +00004161 formatted_raw_ostream OS(ROS);
Duncan P. N. Exon Smith2a5fda92015-06-27 00:38:26 +00004162 SlotTracker EmptySlotTable(static_cast<const Module *>(nullptr));
4163 SlotTracker &SlotTable =
4164 MST.getMachine() ? *MST.getMachine() : EmptySlotTable;
4165 auto incorporateFunction = [&](const Function *F) {
4166 if (F)
4167 MST.incorporateFunction(*F);
4168 };
4169
Chris Lattner944fac72008-08-23 22:23:09 +00004170 if (const Instruction *I = dyn_cast<Instruction>(this)) {
Duncan P. N. Exon Smith2a5fda92015-06-27 00:38:26 +00004171 incorporateFunction(I->getParent() ? I->getParent()->getParent() : nullptr);
Justin Bogner0adaaa32015-09-27 22:38:50 +00004172 AssemblyWriter W(OS, SlotTable, getModuleFromVal(I), nullptr, IsForDebug);
Chris Lattnerbd72b322009-12-31 02:23:35 +00004173 W.printInstruction(*I);
Chris Lattner944fac72008-08-23 22:23:09 +00004174 } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(this)) {
Duncan P. N. Exon Smith2a5fda92015-06-27 00:38:26 +00004175 incorporateFunction(BB->getParent());
Justin Bogner0adaaa32015-09-27 22:38:50 +00004176 AssemblyWriter W(OS, SlotTable, getModuleFromVal(BB), nullptr, IsForDebug);
Chris Lattnerbd72b322009-12-31 02:23:35 +00004177 W.printBasicBlock(BB);
Chris Lattner944fac72008-08-23 22:23:09 +00004178 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(this)) {
Justin Bogner0adaaa32015-09-27 22:38:50 +00004179 AssemblyWriter W(OS, SlotTable, GV->getParent(), nullptr, IsForDebug);
Chris Lattnerbd72b322009-12-31 02:23:35 +00004180 if (const GlobalVariable *V = dyn_cast<GlobalVariable>(GV))
4181 W.printGlobal(V);
4182 else if (const Function *F = dyn_cast<Function>(GV))
4183 W.printFunction(F);
4184 else
Dmitry Polukhin51a06a22016-04-05 08:47:51 +00004185 W.printIndirectSymbol(cast<GlobalIndirectSymbol>(GV));
Duncan P. N. Exon Smithdad20b22014-12-09 18:38:53 +00004186 } else if (const MetadataAsValue *V = dyn_cast<MetadataAsValue>(this)) {
Duncan P. N. Exon Smith2a5fda92015-06-27 00:38:26 +00004187 V->getMetadata()->print(ROS, MST, getModuleFromVal(V));
Chris Lattner944fac72008-08-23 22:23:09 +00004188 } else if (const Constant *C = dyn_cast<Constant>(this)) {
Chris Lattnere9fa33e2009-02-28 23:20:19 +00004189 TypePrinting TypePrinter;
Chris Lattner0f7364b2009-02-28 21:26:53 +00004190 TypePrinter.print(C->getType(), OS);
Chris Lattner6d4306e2009-02-28 21:11:05 +00004191 OS << ' ';
Duncan P. N. Exon Smith2a5fda92015-06-27 00:38:26 +00004192 WriteConstantInternal(OS, C, TypePrinter, MST.getMachine(), nullptr);
Duncan P. N. Exon Smithdad20b22014-12-09 18:38:53 +00004193 } else if (isa<InlineAsm>(this) || isa<Argument>(this)) {
Duncan P. N. Exon Smith2a5fda92015-06-27 00:38:26 +00004194 this->printAsOperand(OS, /* PrintType */ true, MST);
Chris Lattner944fac72008-08-23 22:23:09 +00004195 } else {
Nick Lewyckye5849112014-05-09 00:49:03 +00004196 llvm_unreachable("Unknown value to print out!");
Chris Lattner944fac72008-08-23 22:23:09 +00004197 }
4198}
4199
Duncan P. N. Exon Smith9a61a422015-06-26 22:04:20 +00004200/// Print without a type, skipping the TypePrinting object.
4201///
Benjamin Kramerd3c712e2015-08-08 18:27:36 +00004202/// \return \c true iff printing was successful.
Duncan P. N. Exon Smith9a61a422015-06-26 22:04:20 +00004203static bool printWithoutType(const Value &V, raw_ostream &O,
4204 SlotTracker *Machine, const Module *M) {
4205 if (V.hasName() || isa<GlobalValue>(V) ||
4206 (!isa<Constant>(V) && !isa<MetadataAsValue>(V))) {
4207 WriteAsOperandInternal(O, &V, nullptr, Machine, M);
4208 return true;
Chandler Carruth560e3952014-01-09 02:29:41 +00004209 }
Duncan P. N. Exon Smith9a61a422015-06-26 22:04:20 +00004210 return false;
4211}
Chandler Carruth560e3952014-01-09 02:29:41 +00004212
Duncan P. N. Exon Smith9a61a422015-06-26 22:04:20 +00004213static void printAsOperandImpl(const Value &V, raw_ostream &O, bool PrintType,
4214 ModuleSlotTracker &MST) {
Roman Tereshin238a3382018-03-22 21:29:07 +00004215 TypePrinting TypePrinter(MST.getModule());
Chandler Carruth560e3952014-01-09 02:29:41 +00004216 if (PrintType) {
Duncan P. N. Exon Smith9a61a422015-06-26 22:04:20 +00004217 TypePrinter.print(V.getType(), O);
Chandler Carruth560e3952014-01-09 02:29:41 +00004218 O << ' ';
4219 }
4220
Duncan P. N. Exon Smith9a61a422015-06-26 22:04:20 +00004221 WriteAsOperandInternal(O, &V, &TypePrinter, MST.getMachine(),
4222 MST.getModule());
4223}
4224
4225void Value::printAsOperand(raw_ostream &O, bool PrintType,
4226 const Module *M) const {
4227 if (!M)
4228 M = getModuleFromVal(this);
4229
4230 if (!PrintType)
4231 if (printWithoutType(*this, O, nullptr, M))
4232 return;
4233
4234 SlotTracker Machine(
4235 M, /* ShouldInitializeAllMetadata */ isa<MetadataAsValue>(this));
4236 ModuleSlotTracker MST(Machine, M);
4237 printAsOperandImpl(*this, O, PrintType, MST);
4238}
4239
4240void Value::printAsOperand(raw_ostream &O, bool PrintType,
4241 ModuleSlotTracker &MST) const {
4242 if (!PrintType)
4243 if (printWithoutType(*this, O, MST.getMachine(), MST.getModule()))
4244 return;
4245
4246 printAsOperandImpl(*this, O, PrintType, MST);
Chandler Carruth560e3952014-01-09 02:29:41 +00004247}
4248
Duncan P. N. Exon Smithf9f505e2015-03-14 20:19:36 +00004249static void printMetadataImpl(raw_ostream &ROS, const Metadata &MD,
Duncan P. N. Exon Smith8266df72015-06-26 22:28:47 +00004250 ModuleSlotTracker &MST, const Module *M,
4251 bool OnlyAsOperand) {
Duncan P. N. Exon Smithdad20b22014-12-09 18:38:53 +00004252 formatted_raw_ostream OS(ROS);
Duncan P. N. Exon Smithdad20b22014-12-09 18:38:53 +00004253
Roman Tereshin238a3382018-03-22 21:29:07 +00004254 TypePrinting TypePrinter(M);
Duncan P. N. Exon Smithf9f505e2015-03-14 20:19:36 +00004255
Duncan P. N. Exon Smith8266df72015-06-26 22:28:47 +00004256 WriteAsOperandInternal(OS, &MD, &TypePrinter, MST.getMachine(), M,
Duncan P. N. Exon Smithdad20b22014-12-09 18:38:53 +00004257 /* FromValue */ true);
Duncan P. N. Exon Smith8266df72015-06-26 22:28:47 +00004258
4259 auto *N = dyn_cast<MDNode>(&MD);
Reid Klecknerb5b98232017-08-30 20:40:36 +00004260 if (OnlyAsOperand || !N || isa<DIExpression>(MD))
Duncan P. N. Exon Smithf9f505e2015-03-14 20:19:36 +00004261 return;
4262
4263 OS << " = ";
Duncan P. N. Exon Smith8266df72015-06-26 22:28:47 +00004264 WriteMDNodeBodyInternal(OS, N, &TypePrinter, MST.getMachine(), M);
Duncan P. N. Exon Smithf9f505e2015-03-14 20:19:36 +00004265}
4266
4267void Metadata::printAsOperand(raw_ostream &OS, const Module *M) const {
Duncan P. N. Exon Smith8266df72015-06-26 22:28:47 +00004268 ModuleSlotTracker MST(M, isa<MDNode>(this));
4269 printMetadataImpl(OS, *this, MST, M, /* OnlyAsOperand */ true);
4270}
4271
4272void Metadata::printAsOperand(raw_ostream &OS, ModuleSlotTracker &MST,
4273 const Module *M) const {
4274 printMetadataImpl(OS, *this, MST, M, /* OnlyAsOperand */ true);
Duncan P. N. Exon Smithf9f505e2015-03-14 20:19:36 +00004275}
4276
Justin Bogner0adaaa32015-09-27 22:38:50 +00004277void Metadata::print(raw_ostream &OS, const Module *M,
4278 bool /*IsForDebug*/) const {
Duncan P. N. Exon Smith8266df72015-06-26 22:28:47 +00004279 ModuleSlotTracker MST(M, isa<MDNode>(this));
4280 printMetadataImpl(OS, *this, MST, M, /* OnlyAsOperand */ false);
Duncan P. N. Exon Smithdad20b22014-12-09 18:38:53 +00004281}
4282
Duncan P. N. Exon Smith2a5fda92015-06-27 00:38:26 +00004283void Metadata::print(raw_ostream &OS, ModuleSlotTracker &MST,
Justin Bogner0adaaa32015-09-27 22:38:50 +00004284 const Module *M, bool /*IsForDebug*/) const {
Duncan P. N. Exon Smith2a5fda92015-06-27 00:38:26 +00004285 printMetadataImpl(OS, *this, MST, M, /* OnlyAsOperand */ false);
4286}
4287
Teresa Johnsona9a21472018-05-26 02:34:13 +00004288void ModuleSummaryIndex::print(raw_ostream &ROS, bool IsForDebug) const {
4289 SlotTracker SlotTable(this);
4290 formatted_raw_ostream OS(ROS);
4291 AssemblyWriter W(OS, SlotTable, this, IsForDebug);
4292 W.printModuleSummaryIndex();
4293}
4294
Aaron Ballman1d03d382017-10-15 14:32:27 +00004295#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Chris Lattner7059e532008-08-25 17:03:15 +00004296// Value::dump - allow easy printing of Values from the debugger.
Duncan P. N. Exon Smith3a955f32015-02-25 22:08:21 +00004297LLVM_DUMP_METHOD
Justin Bogner0adaaa32015-09-27 22:38:50 +00004298void Value::dump() const { print(dbgs(), /*IsForDebug=*/true); dbgs() << '\n'; }
Reid Spencerfa452c02004-05-25 18:14:38 +00004299
Chris Lattner7059e532008-08-25 17:03:15 +00004300// Type::dump - allow easy printing of Types from the debugger.
Duncan P. N. Exon Smith3a955f32015-02-25 22:08:21 +00004301LLVM_DUMP_METHOD
Justin Bogner0adaaa32015-09-27 22:38:50 +00004302void Type::dump() const { print(dbgs(), /*IsForDebug=*/true); dbgs() << '\n'; }
Chris Lattnerc2871372009-02-28 21:05:51 +00004303
Chris Lattner7059e532008-08-25 17:03:15 +00004304// Module::dump() - Allow printing of Modules from the debugger.
Duncan P. N. Exon Smith3a955f32015-02-25 22:08:21 +00004305LLVM_DUMP_METHOD
Justin Bogner0adaaa32015-09-27 22:38:50 +00004306void Module::dump() const {
4307 print(dbgs(), nullptr,
4308 /*ShouldPreserveUseListOrder=*/false, /*IsForDebug=*/true);
4309}
Bill Wendlingf4374e42011-12-09 23:18:34 +00004310
Adrian Prantl26b584c2018-05-01 15:54:18 +00004311// Allow printing of Comdats from the debugger.
Duncan P. N. Exon Smith3a955f32015-02-25 22:08:21 +00004312LLVM_DUMP_METHOD
Justin Bogner0adaaa32015-09-27 22:38:50 +00004313void Comdat::dump() const { print(dbgs(), /*IsForDebug=*/true); }
David Majnemerc8a11692014-06-27 18:19:56 +00004314
Bill Wendlingf4374e42011-12-09 23:18:34 +00004315// NamedMDNode::dump() - Allow printing of NamedMDNodes from the debugger.
Duncan P. N. Exon Smith3a955f32015-02-25 22:08:21 +00004316LLVM_DUMP_METHOD
Justin Bogner0adaaa32015-09-27 22:38:50 +00004317void NamedMDNode::dump() const { print(dbgs(), /*IsForDebug=*/true); }
Duncan P. N. Exon Smithdad20b22014-12-09 18:38:53 +00004318
Duncan P. N. Exon Smith3a955f32015-02-25 22:08:21 +00004319LLVM_DUMP_METHOD
Duncan P. N. Exon Smith9ca35802015-03-15 06:53:32 +00004320void Metadata::dump() const { dump(nullptr); }
4321
4322LLVM_DUMP_METHOD
Duncan P. N. Exon Smithf9f505e2015-03-14 20:19:36 +00004323void Metadata::dump(const Module *M) const {
Justin Bogner0adaaa32015-09-27 22:38:50 +00004324 print(dbgs(), M, /*IsForDebug=*/true);
Duncan P. N. Exon Smithdad20b22014-12-09 18:38:53 +00004325 dbgs() << '\n';
4326}
Teresa Johnsona9a21472018-05-26 02:34:13 +00004327
4328// Allow printing of ModuleSummaryIndex from the debugger.
4329LLVM_DUMP_METHOD
4330void ModuleSummaryIndex::dump() const { print(dbgs(), /*IsForDebug=*/true); }
Matthias Braun88d20752017-01-28 02:02:38 +00004331#endif