blob: e19c7c20a5d3a1ca2674ff42161fa230a8670929 [file] [log] [blame]
Eugene Zelenkode6cce22017-06-19 22:05:08 +00001//===- AsmWriter.cpp - Printing LLVM as an assembly file ------------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
Vlad Tsyrklevich3b59a8a2018-04-26 17:34:51 +000010// This library implements `print` family of functions in classes like
11// Module, Function, Value, etc. In-memory representation of those classes is
12// converted to IR strings.
Chris Lattner2f7c9632001-06-06 20:29:01 +000013//
Chris Lattner189088e2002-04-12 18:21:53 +000014// Note that these routines must be extremely tolerant of various errors in the
Chris Lattnerf70da102003-05-08 02:44:12 +000015// LLVM code, because it can be used for debugging transformations.
Chris Lattner189088e2002-04-12 18:21:53 +000016//
Chris Lattner2f7c9632001-06-06 20:29:01 +000017//===----------------------------------------------------------------------===//
18
Eugene Zelenkode6cce22017-06-19 22:05:08 +000019#include "llvm/ADT/APFloat.h"
20#include "llvm/ADT/APInt.h"
21#include "llvm/ADT/ArrayRef.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000022#include "llvm/ADT/DenseMap.h"
Eugene Zelenkode6cce22017-06-19 22:05:08 +000023#include "llvm/ADT/None.h"
24#include "llvm/ADT/Optional.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000025#include "llvm/ADT/STLExtras.h"
Benjamin Kramerba05a782015-03-17 19:53:41 +000026#include "llvm/ADT/SetVector.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000027#include "llvm/ADT/SmallString.h"
Eugene Zelenkode6cce22017-06-19 22:05:08 +000028#include "llvm/ADT/SmallVector.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000029#include "llvm/ADT/StringExtras.h"
Eugene Zelenkode6cce22017-06-19 22:05:08 +000030#include "llvm/ADT/StringRef.h"
31#include "llvm/ADT/iterator_range.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000032#include "llvm/BinaryFormat/Dwarf.h"
Nico Weber432a3882018-04-30 14:59:11 +000033#include "llvm/Config/llvm-config.h"
Eugene Zelenkode6cce22017-06-19 22:05:08 +000034#include "llvm/IR/Argument.h"
Chandler Carruth9aca9182014-01-07 12:34:26 +000035#include "llvm/IR/AssemblyAnnotationWriter.h"
Reid Klecknereb9dd5b2017-04-10 23:31:05 +000036#include "llvm/IR/Attributes.h"
Eugene Zelenkode6cce22017-06-19 22:05:08 +000037#include "llvm/IR/BasicBlock.h"
Chandler Carruth1305dc32014-03-04 11:45:46 +000038#include "llvm/IR/CFG.h"
Eugene Zelenkode6cce22017-06-19 22:05:08 +000039#include "llvm/IR/CallSite.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000040#include "llvm/IR/CallingConv.h"
Eugene Zelenkode6cce22017-06-19 22:05:08 +000041#include "llvm/IR/Comdat.h"
42#include "llvm/IR/Constant.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000043#include "llvm/IR/Constants.h"
Eugene Zelenkode6cce22017-06-19 22:05:08 +000044#include "llvm/IR/DebugInfoMetadata.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000045#include "llvm/IR/DerivedTypes.h"
Eugene Zelenkode6cce22017-06-19 22:05:08 +000046#include "llvm/IR/Function.h"
47#include "llvm/IR/GlobalAlias.h"
48#include "llvm/IR/GlobalIFunc.h"
49#include "llvm/IR/GlobalIndirectSymbol.h"
50#include "llvm/IR/GlobalObject.h"
51#include "llvm/IR/GlobalValue.h"
52#include "llvm/IR/GlobalVariable.h"
Chandler Carruthb8ddc702014-01-12 11:10:32 +000053#include "llvm/IR/IRPrintingPasses.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000054#include "llvm/IR/InlineAsm.h"
Eugene Zelenkode6cce22017-06-19 22:05:08 +000055#include "llvm/IR/InstrTypes.h"
56#include "llvm/IR/Instruction.h"
57#include "llvm/IR/Instructions.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000058#include "llvm/IR/LLVMContext.h"
Eugene Zelenkode6cce22017-06-19 22:05:08 +000059#include "llvm/IR/Metadata.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000060#include "llvm/IR/Module.h"
Duncan P. N. Exon Smith326921542015-06-26 22:04:20 +000061#include "llvm/IR/ModuleSlotTracker.h"
Teresa Johnson08d5b4e2018-05-26 02:34:13 +000062#include "llvm/IR/ModuleSummaryIndex.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000063#include "llvm/IR/Operator.h"
Igor Laevsky2aa8caf2015-05-05 13:20:42 +000064#include "llvm/IR/Statepoint.h"
Eugene Zelenkode6cce22017-06-19 22:05:08 +000065#include "llvm/IR/Type.h"
Chandler Carruthdcb603f2013-01-07 15:43:51 +000066#include "llvm/IR/TypeFinder.h"
Eugene Zelenkode6cce22017-06-19 22:05:08 +000067#include "llvm/IR/Use.h"
Benjamin Kramerba05a782015-03-17 19:53:41 +000068#include "llvm/IR/UseListOrder.h"
Eugene Zelenkode6cce22017-06-19 22:05:08 +000069#include "llvm/IR/User.h"
70#include "llvm/IR/Value.h"
71#include "llvm/Support/AtomicOrdering.h"
72#include "llvm/Support/Casting.h"
73#include "llvm/Support/Compiler.h"
David Greenec7f9b122010-01-05 01:29:26 +000074#include "llvm/Support/Debug.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000075#include "llvm/Support/ErrorHandling.h"
Justin Bogner93289572015-12-04 02:14:34 +000076#include "llvm/Support/Format.h"
Dan Gohmane2745262009-08-12 17:23:50 +000077#include "llvm/Support/FormattedStream.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000078#include "llvm/Support/raw_ostream.h"
Chris Lattnerfee714f2001-09-07 16:36:04 +000079#include <algorithm>
Eugene Zelenkode6cce22017-06-19 22:05:08 +000080#include <cassert>
Reid Spencerbdf03b42007-05-22 19:27:35 +000081#include <cctype>
Eugene Zelenkode6cce22017-06-19 22:05:08 +000082#include <cstddef>
83#include <cstdint>
84#include <iterator>
85#include <memory>
86#include <string>
87#include <tuple>
88#include <utility>
89#include <vector>
90
Chris Lattner189d19f2003-11-21 20:23:48 +000091using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000092
Reid Spencer294715b2005-05-15 16:13:11 +000093// Make virtual table appear in this compilation unit.
Eugene Zelenkode6cce22017-06-19 22:05:08 +000094AssemblyAnnotationWriter::~AssemblyAnnotationWriter() = default;
Reid Spencer294715b2005-05-15 16:13:11 +000095
Chris Lattner3eee99c2008-08-19 04:36:02 +000096//===----------------------------------------------------------------------===//
97// Helper Functions
98//===----------------------------------------------------------------------===//
99
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +0000100namespace {
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000101
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +0000102struct OrderMap {
103 DenseMap<const Value *, std::pair<unsigned, bool>> IDs;
104
105 unsigned size() const { return IDs.size(); }
106 std::pair<unsigned, bool> &operator[](const Value *V) { return IDs[V]; }
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000107
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +0000108 std::pair<unsigned, bool> lookup(const Value *V) const {
109 return IDs.lookup(V);
110 }
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000111
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +0000112 void index(const Value *V) {
113 // Explicitly sequence get-size and insert-value operations to avoid UB.
114 unsigned ID = IDs.size() + 1;
115 IDs[V].first = ID;
116 }
117};
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000118
119} // end anonymous namespace
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +0000120
121static void orderValue(const Value *V, OrderMap &OM) {
122 if (OM.lookup(V).first)
123 return;
124
125 if (const Constant *C = dyn_cast<Constant>(V))
126 if (C->getNumOperands() && !isa<GlobalValue>(C))
127 for (const Value *Op : C->operands())
128 if (!isa<BasicBlock>(Op) && !isa<GlobalValue>(Op))
129 orderValue(Op, OM);
130
131 // Note: we cannot cache this lookup above, since inserting into the map
132 // changes the map's size, and thus affects the other IDs.
133 OM.index(V);
134}
135
136static OrderMap orderModule(const Module *M) {
137 // This needs to match the order used by ValueEnumerator::ValueEnumerator()
138 // and ValueEnumerator::incorporateFunction().
139 OrderMap OM;
140
141 for (const GlobalVariable &G : M->globals()) {
142 if (G.hasInitializer())
143 if (!isa<GlobalValue>(G.getInitializer()))
144 orderValue(G.getInitializer(), OM);
145 orderValue(&G, OM);
146 }
147 for (const GlobalAlias &A : M->aliases()) {
148 if (!isa<GlobalValue>(A.getAliasee()))
149 orderValue(A.getAliasee(), OM);
150 orderValue(&A, OM);
151 }
Dmitry Polukhina1feff72016-04-07 12:32:19 +0000152 for (const GlobalIFunc &I : M->ifuncs()) {
153 if (!isa<GlobalValue>(I.getResolver()))
154 orderValue(I.getResolver(), OM);
155 orderValue(&I, OM);
156 }
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +0000157 for (const Function &F : *M) {
Vedant Kumar3a63fb32015-12-19 08:52:49 +0000158 for (const Use &U : F.operands())
159 if (!isa<GlobalValue>(U.get()))
160 orderValue(U.get(), OM);
David Majnemer7fddecc2015-06-17 20:52:32 +0000161
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +0000162 orderValue(&F, OM);
163
164 if (F.isDeclaration())
165 continue;
166
167 for (const Argument &A : F.args())
168 orderValue(&A, OM);
169 for (const BasicBlock &BB : F) {
170 orderValue(&BB, OM);
171 for (const Instruction &I : BB) {
172 for (const Value *Op : I.operands())
173 if ((isa<Constant>(*Op) && !isa<GlobalValue>(*Op)) ||
174 isa<InlineAsm>(*Op))
175 orderValue(Op, OM);
176 orderValue(&I, OM);
177 }
178 }
179 }
180 return OM;
181}
182
183static void predictValueUseListOrderImpl(const Value *V, const Function *F,
184 unsigned ID, const OrderMap &OM,
185 UseListOrderStack &Stack) {
186 // Predict use-list order for this one.
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000187 using Entry = std::pair<const Use *, unsigned>;
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +0000188 SmallVector<Entry, 64> List;
189 for (const Use &U : V->uses())
190 // Check if this user will be serialized.
191 if (OM.lookup(U.getUser()).first)
192 List.push_back(std::make_pair(&U, List.size()));
193
194 if (List.size() < 2)
195 // We may have lost some users.
196 return;
197
198 bool GetsReversed =
199 !isa<GlobalVariable>(V) && !isa<Function>(V) && !isa<BasicBlock>(V);
200 if (auto *BA = dyn_cast<BlockAddress>(V))
201 ID = OM.lookup(BA->getBasicBlock()).first;
Mandeep Singh Grangf3555652018-04-05 21:52:24 +0000202 llvm::sort(List.begin(), List.end(), [&](const Entry &L, const Entry &R) {
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +0000203 const Use *LU = L.first;
204 const Use *RU = R.first;
205 if (LU == RU)
206 return false;
207
208 auto LID = OM.lookup(LU->getUser()).first;
209 auto RID = OM.lookup(RU->getUser()).first;
210
211 // If ID is 4, then expect: 7 6 5 1 2 3.
212 if (LID < RID) {
213 if (GetsReversed)
214 if (RID <= ID)
215 return true;
216 return false;
217 }
218 if (RID < LID) {
219 if (GetsReversed)
220 if (LID <= ID)
221 return false;
222 return true;
223 }
224
225 // LID and RID are equal, so we have different operands of the same user.
226 // Assume operands are added in order for all instructions.
227 if (GetsReversed)
228 if (LID <= ID)
229 return LU->getOperandNo() < RU->getOperandNo();
230 return LU->getOperandNo() > RU->getOperandNo();
231 });
232
233 if (std::is_sorted(
234 List.begin(), List.end(),
235 [](const Entry &L, const Entry &R) { return L.second < R.second; }))
236 // Order is already correct.
237 return;
238
239 // Store the shuffle.
240 Stack.emplace_back(V, F, List.size());
241 assert(List.size() == Stack.back().Shuffle.size() && "Wrong size");
242 for (size_t I = 0, E = List.size(); I != E; ++I)
243 Stack.back().Shuffle[I] = List[I].second;
244}
245
246static void predictValueUseListOrder(const Value *V, const Function *F,
247 OrderMap &OM, UseListOrderStack &Stack) {
248 auto &IDPair = OM[V];
249 assert(IDPair.first && "Unmapped value");
250 if (IDPair.second)
251 // Already predicted.
252 return;
253
254 // Do the actual prediction.
255 IDPair.second = true;
256 if (!V->use_empty() && std::next(V->use_begin()) != V->use_end())
257 predictValueUseListOrderImpl(V, F, IDPair.first, OM, Stack);
258
259 // Recursive descent into constants.
260 if (const Constant *C = dyn_cast<Constant>(V))
261 if (C->getNumOperands()) // Visit GlobalValues.
262 for (const Value *Op : C->operands())
263 if (isa<Constant>(Op)) // Visit GlobalValues.
264 predictValueUseListOrder(Op, F, OM, Stack);
265}
266
267static UseListOrderStack predictUseListOrder(const Module *M) {
268 OrderMap OM = orderModule(M);
269
270 // Use-list orders need to be serialized after all the users have been added
271 // to a value, or else the shuffles will be incomplete. Store them per
272 // function in a stack.
273 //
274 // Aside from function order, the order of values doesn't matter much here.
275 UseListOrderStack Stack;
276
277 // We want to visit the functions backward now so we can list function-local
278 // constants in the last Function they're used in. Module-level constants
279 // have already been visited above.
Pete Cooper7679afd2015-07-24 21:13:43 +0000280 for (const Function &F : make_range(M->rbegin(), M->rend())) {
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +0000281 if (F.isDeclaration())
282 continue;
283 for (const BasicBlock &BB : F)
284 predictValueUseListOrder(&BB, &F, OM, Stack);
285 for (const Argument &A : F.args())
286 predictValueUseListOrder(&A, &F, OM, Stack);
287 for (const BasicBlock &BB : F)
288 for (const Instruction &I : BB)
289 for (const Value *Op : I.operands())
290 if (isa<Constant>(*Op) || isa<InlineAsm>(*Op)) // Visit GlobalValues.
291 predictValueUseListOrder(Op, &F, OM, Stack);
292 for (const BasicBlock &BB : F)
293 for (const Instruction &I : BB)
294 predictValueUseListOrder(&I, &F, OM, Stack);
295 }
296
297 // Visit globals last.
298 for (const GlobalVariable &G : M->globals())
299 predictValueUseListOrder(&G, nullptr, OM, Stack);
300 for (const Function &F : *M)
301 predictValueUseListOrder(&F, nullptr, OM, Stack);
302 for (const GlobalAlias &A : M->aliases())
303 predictValueUseListOrder(&A, nullptr, OM, Stack);
Dmitry Polukhina1feff72016-04-07 12:32:19 +0000304 for (const GlobalIFunc &I : M->ifuncs())
305 predictValueUseListOrder(&I, nullptr, OM, Stack);
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +0000306 for (const GlobalVariable &G : M->globals())
307 if (G.hasInitializer())
308 predictValueUseListOrder(G.getInitializer(), nullptr, OM, Stack);
309 for (const GlobalAlias &A : M->aliases())
310 predictValueUseListOrder(A.getAliasee(), nullptr, OM, Stack);
Dmitry Polukhina1feff72016-04-07 12:32:19 +0000311 for (const GlobalIFunc &I : M->ifuncs())
312 predictValueUseListOrder(I.getResolver(), nullptr, OM, Stack);
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +0000313 for (const Function &F : *M)
Vedant Kumar3a63fb32015-12-19 08:52:49 +0000314 for (const Use &U : F.operands())
315 predictValueUseListOrder(U.get(), nullptr, OM, Stack);
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +0000316
317 return Stack;
318}
319
Chris Lattner3eee99c2008-08-19 04:36:02 +0000320static const Module *getModuleFromVal(const Value *V) {
321 if (const Argument *MA = dyn_cast<Argument>(V))
Craig Topperc6207612014-04-09 06:08:46 +0000322 return MA->getParent() ? MA->getParent()->getParent() : nullptr;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000323
Chris Lattner3eee99c2008-08-19 04:36:02 +0000324 if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
Craig Topperc6207612014-04-09 06:08:46 +0000325 return BB->getParent() ? BB->getParent()->getParent() : nullptr;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000326
Chris Lattner3eee99c2008-08-19 04:36:02 +0000327 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Craig Topperc6207612014-04-09 06:08:46 +0000328 const Function *M = I->getParent() ? I->getParent()->getParent() : nullptr;
329 return M ? M->getParent() : nullptr;
Chris Lattner3eee99c2008-08-19 04:36:02 +0000330 }
Andrew Trickec4b6e72011-09-30 19:48:58 +0000331
Chris Lattner3eee99c2008-08-19 04:36:02 +0000332 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
333 return GV->getParent();
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000334
335 if (const auto *MAV = dyn_cast<MetadataAsValue>(V)) {
336 for (const User *U : MAV->users())
337 if (isa<Instruction>(U))
338 if (const Module *M = getModuleFromVal(U))
339 return M;
340 return nullptr;
341 }
342
Craig Topperc6207612014-04-09 06:08:46 +0000343 return nullptr;
Chris Lattner3eee99c2008-08-19 04:36:02 +0000344}
345
Bill Wendling90bc19c2013-02-20 07:21:42 +0000346static void PrintCallingConv(unsigned cc, raw_ostream &Out) {
Micah Villmow857186d2012-09-13 15:11:12 +0000347 switch (cc) {
Bill Wendling90bc19c2013-02-20 07:21:42 +0000348 default: Out << "cc" << cc; break;
349 case CallingConv::Fast: Out << "fastcc"; break;
350 case CallingConv::Cold: Out << "coldcc"; break;
Andrew Trick5ae6ed82013-11-11 22:40:22 +0000351 case CallingConv::WebKit_JS: Out << "webkit_jscc"; break;
352 case CallingConv::AnyReg: Out << "anyregcc"; break;
Juergen Ributzkae6250132014-01-17 19:47:03 +0000353 case CallingConv::PreserveMost: Out << "preserve_mostcc"; break;
354 case CallingConv::PreserveAll: Out << "preserve_allcc"; break;
Manman Ren19c7bbe2015-12-04 17:40:13 +0000355 case CallingConv::CXX_FAST_TLS: Out << "cxx_fast_tlscc"; break;
Reid Kleckner35fc3632014-12-01 21:04:44 +0000356 case CallingConv::GHC: Out << "ghccc"; break;
Bill Wendling90bc19c2013-02-20 07:21:42 +0000357 case CallingConv::X86_StdCall: Out << "x86_stdcallcc"; break;
358 case CallingConv::X86_FastCall: Out << "x86_fastcallcc"; break;
359 case CallingConv::X86_ThisCall: Out << "x86_thiscallcc"; break;
Oren Ben Simhon92ccbf22016-10-13 07:53:43 +0000360 case CallingConv::X86_RegCall: Out << "x86_regcallcc"; break;
Reid Kleckner9ccce992014-10-28 01:29:26 +0000361 case CallingConv::X86_VectorCall:Out << "x86_vectorcallcc"; break;
Bill Wendling90bc19c2013-02-20 07:21:42 +0000362 case CallingConv::Intel_OCL_BI: Out << "intel_ocl_bicc"; break;
363 case CallingConv::ARM_APCS: Out << "arm_apcscc"; break;
364 case CallingConv::ARM_AAPCS: Out << "arm_aapcscc"; break;
365 case CallingConv::ARM_AAPCS_VFP: Out << "arm_aapcs_vfpcc"; break;
366 case CallingConv::MSP430_INTR: Out << "msp430_intrcc"; break;
Dylan McKay4fd0d4a2016-03-03 10:08:02 +0000367 case CallingConv::AVR_INTR: Out << "avr_intrcc "; break;
368 case CallingConv::AVR_SIGNAL: Out << "avr_signalcc "; break;
Bill Wendling90bc19c2013-02-20 07:21:42 +0000369 case CallingConv::PTX_Kernel: Out << "ptx_kernel"; break;
370 case CallingConv::PTX_Device: Out << "ptx_device"; break;
Charles Davise8f297c2013-07-12 06:02:35 +0000371 case CallingConv::X86_64_SysV: Out << "x86_64_sysvcc"; break;
Martin Storsjo2f24e932017-07-17 20:05:19 +0000372 case CallingConv::Win64: Out << "win64cc"; break;
Michael Kupersteine31b4862013-12-15 10:01:20 +0000373 case CallingConv::SPIR_FUNC: Out << "spir_func"; break;
374 case CallingConv::SPIR_KERNEL: Out << "spir_kernel"; break;
Manman Renf8bdd882016-04-05 22:41:47 +0000375 case CallingConv::Swift: Out << "swiftcc"; break;
Amjad Aboud60b5e1b2015-12-21 14:07:14 +0000376 case CallingConv::X86_INTR: Out << "x86_intrcc"; break;
Maksim Panchenkocce239c2015-09-29 22:09:16 +0000377 case CallingConv::HHVM: Out << "hhvmcc"; break;
378 case CallingConv::HHVM_C: Out << "hhvm_ccc"; break;
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000379 case CallingConv::AMDGPU_VS: Out << "amdgpu_vs"; break;
Tim Renoufef1ae8f2017-09-29 09:51:22 +0000380 case CallingConv::AMDGPU_LS: Out << "amdgpu_ls"; break;
Marek Olsaka302a7362017-05-02 15:41:10 +0000381 case CallingConv::AMDGPU_HS: Out << "amdgpu_hs"; break;
Tim Renoufef1ae8f2017-09-29 09:51:22 +0000382 case CallingConv::AMDGPU_ES: Out << "amdgpu_es"; break;
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000383 case CallingConv::AMDGPU_GS: Out << "amdgpu_gs"; break;
384 case CallingConv::AMDGPU_PS: Out << "amdgpu_ps"; break;
385 case CallingConv::AMDGPU_CS: Out << "amdgpu_cs"; break;
Nikolay Haustov1f7732a2016-05-06 09:07:29 +0000386 case CallingConv::AMDGPU_KERNEL: Out << "amdgpu_kernel"; break;
Micah Villmow857186d2012-09-13 15:11:12 +0000387 }
388}
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000389
Chris Lattner3eee99c2008-08-19 04:36:02 +0000390enum PrefixType {
391 GlobalPrefix,
David Majnemerdad0a642014-06-27 18:19:56 +0000392 ComdatPrefix,
Chris Lattner3eee99c2008-08-19 04:36:02 +0000393 LabelPrefix,
Daniel Dunbar389529a2008-10-14 23:28:09 +0000394 LocalPrefix,
395 NoPrefix
Chris Lattner3eee99c2008-08-19 04:36:02 +0000396};
397
Alex Lorenz2b7f2652015-07-21 16:50:35 +0000398void llvm::printLLVMNameWithoutPrefix(raw_ostream &OS, StringRef Name) {
Jay Foadf7adb322011-04-24 14:30:00 +0000399 assert(!Name.empty() && "Cannot get empty name!");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000400
Chris Lattner3eee99c2008-08-19 04:36:02 +0000401 // Scan the name to see if it needs quotes first.
Guy Benyei83c74e92013-02-12 21:21:59 +0000402 bool NeedsQuotes = isdigit(static_cast<unsigned char>(Name[0]));
Chris Lattner3eee99c2008-08-19 04:36:02 +0000403 if (!NeedsQuotes) {
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000404 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
Aaron Ballmaned9b0a92012-07-16 16:18:18 +0000405 // By making this unsigned, the value passed in to isalnum will always be
406 // in the range 0-255. This is important when building with MSVC because
407 // its implementation will assert. This situation can arise when dealing
408 // with UTF-8 multibyte characters.
409 unsigned char C = Name[i];
Guy Benyei83c74e92013-02-12 21:21:59 +0000410 if (!isalnum(static_cast<unsigned char>(C)) && C != '-' && C != '.' &&
411 C != '_') {
Chris Lattner3eee99c2008-08-19 04:36:02 +0000412 NeedsQuotes = true;
413 break;
414 }
415 }
416 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000417
Chris Lattner3eee99c2008-08-19 04:36:02 +0000418 // If we didn't need any quotes, just write out the name in one blast.
419 if (!NeedsQuotes) {
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000420 OS << Name;
Chris Lattner3eee99c2008-08-19 04:36:02 +0000421 return;
422 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000423
Chris Lattner3eee99c2008-08-19 04:36:02 +0000424 // Okay, we need quotes. Output the quotes and escape any scary characters as
425 // needed.
426 OS << '"';
Jonas Devlieghere745918ff2018-05-31 17:01:42 +0000427 printEscapedString(Name, OS);
Chris Lattner3eee99c2008-08-19 04:36:02 +0000428 OS << '"';
429}
430
Alex Lorenz2b7f2652015-07-21 16:50:35 +0000431/// Turn the specified name into an 'LLVM name', which is either prefixed with %
432/// (if the string only contains simple characters) or is surrounded with ""'s
433/// (if it has special chars in it). Print it out.
434static void PrintLLVMName(raw_ostream &OS, StringRef Name, PrefixType Prefix) {
435 switch (Prefix) {
436 case NoPrefix:
437 break;
438 case GlobalPrefix:
439 OS << '@';
440 break;
441 case ComdatPrefix:
442 OS << '$';
443 break;
444 case LabelPrefix:
445 break;
446 case LocalPrefix:
447 OS << '%';
448 break;
449 }
450 printLLVMNameWithoutPrefix(OS, Name);
451}
452
453/// Turn the specified name into an 'LLVM name', which is either prefixed with %
454/// (if the string only contains simple characters) or is surrounded with ""'s
455/// (if it has special chars in it). Print it out.
Dan Gohman12dad632009-08-12 20:56:03 +0000456static void PrintLLVMName(raw_ostream &OS, const Value *V) {
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000457 PrintLLVMName(OS, V->getName(),
Chris Lattner3eee99c2008-08-19 04:36:02 +0000458 isa<GlobalValue>(V) ? GlobalPrefix : LocalPrefix);
459}
460
Benjamin Kramerba05a782015-03-17 19:53:41 +0000461namespace {
Benjamin Kramerba05a782015-03-17 19:53:41 +0000462
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000463class TypePrinting {
464public:
Roman Tereshind96de6f2018-03-22 21:29:07 +0000465 TypePrinting(const Module *M = nullptr) : DeferredM(M) {}
Benjamin Kramerba05a782015-03-17 19:53:41 +0000466
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000467 TypePrinting(const TypePrinting &) = delete;
468 TypePrinting &operator=(const TypePrinting &) = delete;
Benjamin Kramerba05a782015-03-17 19:53:41 +0000469
Roman Tereshind96de6f2018-03-22 21:29:07 +0000470 /// The named types that are used by the current module.
471 TypeFinder &getNamedTypes();
472
473 /// The numbered types, number to type mapping.
474 std::vector<StructType *> &getNumberedTypes();
475
476 bool empty();
Benjamin Kramerba05a782015-03-17 19:53:41 +0000477
478 void print(Type *Ty, raw_ostream &OS);
479
480 void printStructBody(StructType *Ty, raw_ostream &OS);
Roman Tereshind96de6f2018-03-22 21:29:07 +0000481
482private:
483 void incorporateTypes();
484
485 /// A module to process lazily when needed. Set to nullptr as soon as used.
486 const Module *DeferredM;
487
488 TypeFinder NamedTypes;
489
490 // The numbered types, along with their value.
491 DenseMap<StructType *, unsigned> Type2Number;
492
493 std::vector<StructType *> NumberedTypes;
Benjamin Kramerba05a782015-03-17 19:53:41 +0000494};
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000495
496} // end anonymous namespace
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000497
Roman Tereshind96de6f2018-03-22 21:29:07 +0000498TypeFinder &TypePrinting::getNamedTypes() {
499 incorporateTypes();
500 return NamedTypes;
501}
502
503std::vector<StructType *> &TypePrinting::getNumberedTypes() {
504 incorporateTypes();
505
506 // We know all the numbers that each type is used and we know that it is a
507 // dense assignment. Convert the map to an index table, if it's not done
508 // already (judging from the sizes):
509 if (NumberedTypes.size() == Type2Number.size())
510 return NumberedTypes;
511
512 NumberedTypes.resize(Type2Number.size());
513 for (const auto &P : Type2Number) {
514 assert(P.second < NumberedTypes.size() && "Didn't get a dense numbering?");
515 assert(!NumberedTypes[P.second] && "Didn't get a unique numbering?");
516 NumberedTypes[P.second] = P.first;
517 }
518 return NumberedTypes;
519}
520
521bool TypePrinting::empty() {
522 incorporateTypes();
523 return NamedTypes.empty() && Type2Number.empty();
524}
525
526void TypePrinting::incorporateTypes() {
527 if (!DeferredM)
528 return;
529
530 NamedTypes.run(*DeferredM, false);
531 DeferredM = nullptr;
Andrew Trickec4b6e72011-09-30 19:48:58 +0000532
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000533 // The list of struct types we got back includes all the struct types, split
534 // the unnamed ones out to a numbering and remove the anonymous structs.
535 unsigned NextNumber = 0;
Andrew Trickec4b6e72011-09-30 19:48:58 +0000536
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000537 std::vector<StructType*>::iterator NextToUse = NamedTypes.begin(), I, E;
538 for (I = NamedTypes.begin(), E = NamedTypes.end(); I != E; ++I) {
539 StructType *STy = *I;
Andrew Trickec4b6e72011-09-30 19:48:58 +0000540
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000541 // Ignore anonymous types.
Chris Lattner01beceb2011-08-12 18:07:07 +0000542 if (STy->isLiteral())
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000543 continue;
Andrew Trickec4b6e72011-09-30 19:48:58 +0000544
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000545 if (STy->getName().empty())
Roman Tereshind96de6f2018-03-22 21:29:07 +0000546 Type2Number[STy] = NextNumber++;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000547 else
548 *NextToUse++ = STy;
549 }
Andrew Trickec4b6e72011-09-30 19:48:58 +0000550
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000551 NamedTypes.erase(NextToUse, NamedTypes.end());
552}
553
Roman Tereshind96de6f2018-03-22 21:29:07 +0000554/// Write the specified type to the specified raw_ostream, making use of type
555/// names or up references to shorten the type name where possible.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000556void TypePrinting::print(Type *Ty, raw_ostream &OS) {
Chris Lattner0f578952009-02-28 20:25:14 +0000557 switch (Ty->getTypeID()) {
Rafael Espindolaba7df702013-12-07 02:27:52 +0000558 case Type::VoidTyID: OS << "void"; return;
559 case Type::HalfTyID: OS << "half"; return;
560 case Type::FloatTyID: OS << "float"; return;
561 case Type::DoubleTyID: OS << "double"; return;
562 case Type::X86_FP80TyID: OS << "x86_fp80"; return;
563 case Type::FP128TyID: OS << "fp128"; return;
564 case Type::PPC_FP128TyID: OS << "ppc_fp128"; return;
565 case Type::LabelTyID: OS << "label"; return;
566 case Type::MetadataTyID: OS << "metadata"; return;
567 case Type::X86_MMXTyID: OS << "x86_mmx"; return;
David Majnemerb611e3f2015-08-14 05:09:07 +0000568 case Type::TokenTyID: OS << "token"; return;
Chris Lattner68318b12009-02-28 21:18:43 +0000569 case Type::IntegerTyID:
Chris Lattner06a23212009-02-28 21:27:31 +0000570 OS << 'i' << cast<IntegerType>(Ty)->getBitWidth();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000571 return;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000572
Chris Lattner07d88292009-02-28 20:35:42 +0000573 case Type::FunctionTyID: {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000574 FunctionType *FTy = cast<FunctionType>(Ty);
575 print(FTy->getReturnType(), OS);
Chris Lattner06a23212009-02-28 21:27:31 +0000576 OS << " (";
Chris Lattner07d88292009-02-28 20:35:42 +0000577 for (FunctionType::param_iterator I = FTy->param_begin(),
578 E = FTy->param_end(); I != E; ++I) {
579 if (I != FTy->param_begin())
Chris Lattner06a23212009-02-28 21:27:31 +0000580 OS << ", ";
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000581 print(*I, OS);
Chris Lattner0f578952009-02-28 20:25:14 +0000582 }
Chris Lattner07d88292009-02-28 20:35:42 +0000583 if (FTy->isVarArg()) {
Chris Lattner06a23212009-02-28 21:27:31 +0000584 if (FTy->getNumParams()) OS << ", ";
585 OS << "...";
Chris Lattner0f578952009-02-28 20:25:14 +0000586 }
Chris Lattner06a23212009-02-28 21:27:31 +0000587 OS << ')';
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000588 return;
Chris Lattner07d88292009-02-28 20:35:42 +0000589 }
590 case Type::StructTyID: {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000591 StructType *STy = cast<StructType>(Ty);
Andrew Trickec4b6e72011-09-30 19:48:58 +0000592
Chris Lattner01beceb2011-08-12 18:07:07 +0000593 if (STy->isLiteral())
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000594 return printStructBody(STy, OS);
595
596 if (!STy->getName().empty())
597 return PrintLLVMName(OS, STy->getName(), LocalPrefix);
Andrew Trickec4b6e72011-09-30 19:48:58 +0000598
Roman Tereshind96de6f2018-03-22 21:29:07 +0000599 incorporateTypes();
600 const auto I = Type2Number.find(STy);
601 if (I != Type2Number.end())
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000602 OS << '%' << I->second;
603 else // Not enumerated, print the hex address.
Benjamin Kramer4e8b4632011-11-02 17:24:36 +0000604 OS << "%\"type " << STy << '\"';
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000605 return;
Chris Lattner07d88292009-02-28 20:35:42 +0000606 }
607 case Type::PointerTyID: {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000608 PointerType *PTy = cast<PointerType>(Ty);
609 print(PTy->getElementType(), OS);
Chris Lattner07d88292009-02-28 20:35:42 +0000610 if (unsigned AddressSpace = PTy->getAddressSpace())
Chris Lattner06a23212009-02-28 21:27:31 +0000611 OS << " addrspace(" << AddressSpace << ')';
612 OS << '*';
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000613 return;
Chris Lattner07d88292009-02-28 20:35:42 +0000614 }
615 case Type::ArrayTyID: {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000616 ArrayType *ATy = cast<ArrayType>(Ty);
Chris Lattner06a23212009-02-28 21:27:31 +0000617 OS << '[' << ATy->getNumElements() << " x ";
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000618 print(ATy->getElementType(), OS);
Chris Lattner06a23212009-02-28 21:27:31 +0000619 OS << ']';
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000620 return;
Chris Lattner07d88292009-02-28 20:35:42 +0000621 }
622 case Type::VectorTyID: {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000623 VectorType *PTy = cast<VectorType>(Ty);
Chris Lattner06a23212009-02-28 21:27:31 +0000624 OS << "<" << PTy->getNumElements() << " x ";
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000625 print(PTy->getElementType(), OS);
Chris Lattner06a23212009-02-28 21:27:31 +0000626 OS << '>';
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000627 return;
Chris Lattner07d88292009-02-28 20:35:42 +0000628 }
Chris Lattner0f578952009-02-28 20:25:14 +0000629 }
Rafael Espindolaba7df702013-12-07 02:27:52 +0000630 llvm_unreachable("Invalid TypeID");
Chris Lattner0f578952009-02-28 20:25:14 +0000631}
632
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000633void TypePrinting::printStructBody(StructType *STy, raw_ostream &OS) {
634 if (STy->isOpaque()) {
635 OS << "opaque";
636 return;
Chris Lattner0f578952009-02-28 20:25:14 +0000637 }
Andrew Trickec4b6e72011-09-30 19:48:58 +0000638
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000639 if (STy->isPacked())
640 OS << '<';
Andrew Trickec4b6e72011-09-30 19:48:58 +0000641
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000642 if (STy->getNumElements() == 0) {
643 OS << "{}";
644 } else {
645 StructType::element_iterator I = STy->element_begin();
646 OS << "{ ";
647 print(*I++, OS);
648 for (StructType::element_iterator E = STy->element_end(); I != E; ++I) {
649 OS << ", ";
650 print(*I, OS);
Chris Lattner3243ea12009-03-01 00:03:38 +0000651 }
Andrew Trickec4b6e72011-09-30 19:48:58 +0000652
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000653 OS << " }";
Chris Lattner92c5c122009-02-28 23:20:19 +0000654 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000655 if (STy->isPacked())
656 OS << '>';
Chris Lattner92c5c122009-02-28 23:20:19 +0000657}
658
Duncan P. N. Exon Smith326921542015-06-26 22:04:20 +0000659namespace llvm {
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000660
Chris Lattner3eee99c2008-08-19 04:36:02 +0000661//===----------------------------------------------------------------------===//
662// SlotTracker Class: Enumerate slot numbers for unnamed values
663//===----------------------------------------------------------------------===//
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000664/// This class provides computation of slot numbers for LLVM Assembly writing.
Chris Lattner393b7cd2008-08-17 04:17:45 +0000665///
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000666class SlotTracker {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000667public:
Devang Patel983c6b12009-07-08 21:44:25 +0000668 /// ValueMap - A mapping of Values to slot numbers.
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000669 using ValueMap = DenseMap<const Value *, unsigned>;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000670
671private:
Devang Patel983c6b12009-07-08 21:44:25 +0000672 /// TheModule - The module for which we are holding slot numbers.
Chris Lattner393b7cd2008-08-17 04:17:45 +0000673 const Module* TheModule;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000674
Devang Patel983c6b12009-07-08 21:44:25 +0000675 /// TheFunction - The function for which we are holding slot numbers.
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000676 const Function* TheFunction = nullptr;
677 bool FunctionProcessed = false;
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000678 bool ShouldInitializeAllMetadata;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000679
Teresa Johnson08d5b4e2018-05-26 02:34:13 +0000680 /// The summary index for which we are holding slot numbers.
681 const ModuleSummaryIndex *TheIndex = nullptr;
682
Jay Foaddbb221d2011-07-11 07:28:49 +0000683 /// mMap - The slot map for the module level data.
Chris Lattner393b7cd2008-08-17 04:17:45 +0000684 ValueMap mMap;
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000685 unsigned mNext = 0;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000686
Jay Foaddbb221d2011-07-11 07:28:49 +0000687 /// fMap - The slot map for the function level data.
Chris Lattner393b7cd2008-08-17 04:17:45 +0000688 ValueMap fMap;
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000689 unsigned fNext = 0;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000690
Devang Patel983c6b12009-07-08 21:44:25 +0000691 /// mdnMap - Map for MDNodes.
Chris Lattnercf4a76e2009-12-31 02:20:11 +0000692 DenseMap<const MDNode*, unsigned> mdnMap;
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000693 unsigned mdnNext = 0;
Bill Wendling829b4782013-02-11 08:43:33 +0000694
695 /// asMap - The slot map for attribute sets.
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000696 DenseMap<AttributeSet, unsigned> asMap;
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000697 unsigned asNext = 0;
698
Teresa Johnson8fc76662018-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 Johnson08d5b4e2018-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
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000707public:
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000708 /// Construct from a module.
709 ///
710 /// If \c ShouldInitializeAllMetadata, initializes all metadata in all
711 /// functions, giving correct numbering for metadata referenced only from
712 /// within a function (even if no functions have been initialized).
713 explicit SlotTracker(const Module *M,
714 bool ShouldInitializeAllMetadata = false);
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000715
Chris Lattner393b7cd2008-08-17 04:17:45 +0000716 /// Construct from a function, starting out in incorp state.
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000717 ///
718 /// If \c ShouldInitializeAllMetadata, initializes all metadata in all
719 /// functions, giving correct numbering for metadata referenced only from
720 /// within a function (even if no functions have been initialized).
721 explicit SlotTracker(const Function *F,
722 bool ShouldInitializeAllMetadata = false);
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000723
Teresa Johnson08d5b4e2018-05-26 02:34:13 +0000724 /// Construct from a module summary index.
725 explicit SlotTracker(const ModuleSummaryIndex *Index);
726
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000727 SlotTracker(const SlotTracker &) = delete;
728 SlotTracker &operator=(const SlotTracker &) = delete;
729
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000730 /// Return the slot number of the specified value in it's type
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000731 /// plane. If something is not in the SlotTracker, return -1.
Chris Lattner5e043322007-01-11 03:54:27 +0000732 int getLocalSlot(const Value *V);
733 int getGlobalSlot(const GlobalValue *V);
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000734 int getMetadataSlot(const MDNode *N);
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000735 int getAttributeGroupSlot(AttributeSet AS);
Teresa Johnson8fc76662018-07-02 22:09:23 +0000736 int getModulePathSlot(StringRef Path);
Teresa Johnson08d5b4e2018-05-26 02:34:13 +0000737 int getGUIDSlot(GlobalValue::GUID GUID);
Reid Spencer8beac692004-06-09 15:26:53 +0000738
Misha Brukmanb1c93172005-04-21 23:48:37 +0000739 /// If you'd like to deal with a function instead of just a module, use
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000740 /// this method to get its data into the SlotTracker.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000741 void incorporateFunction(const Function *F) {
742 TheFunction = F;
Reid Spencerb0ac8c42004-08-16 07:46:33 +0000743 FunctionProcessed = false;
744 }
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000745
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +0000746 const Function *getFunction() const { return TheFunction; }
747
Misha Brukmanb1c93172005-04-21 23:48:37 +0000748 /// After calling incorporateFunction, use this method to remove the
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000749 /// most recently incorporated function from the SlotTracker. This
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000750 /// will reset the state of the machine back to just the module contents.
751 void purgeFunction();
752
Devang Patel983c6b12009-07-08 21:44:25 +0000753 /// MDNode map iterators.
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000754 using mdn_iterator = DenseMap<const MDNode*, unsigned>::iterator;
755
Chris Lattnercf4a76e2009-12-31 02:20:11 +0000756 mdn_iterator mdn_begin() { return mdnMap.begin(); }
757 mdn_iterator mdn_end() { return mdnMap.end(); }
758 unsigned mdn_size() const { return mdnMap.size(); }
759 bool mdn_empty() const { return mdnMap.empty(); }
Devang Patel983c6b12009-07-08 21:44:25 +0000760
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000761 /// AttributeSet map iterators.
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000762 using as_iterator = DenseMap<AttributeSet, unsigned>::iterator;
763
Bill Wendling829b4782013-02-11 08:43:33 +0000764 as_iterator as_begin() { return asMap.begin(); }
765 as_iterator as_end() { return asMap.end(); }
766 unsigned as_size() const { return asMap.size(); }
767 bool as_empty() const { return asMap.empty(); }
768
Teresa Johnson08d5b4e2018-05-26 02:34:13 +0000769 /// GUID map iterators.
770 using guid_iterator = DenseMap<GlobalValue::GUID, unsigned>::iterator;
771
772 /// These functions do the actual initialization.
Teresa Johnsonfc801f52018-07-03 15:52:57 +0000773 inline void initializeIfNeeded();
774 void initializeIndexIfNeeded();
Reid Spencer56010e42004-05-26 21:56:09 +0000775
Devang Patel983c6b12009-07-08 21:44:25 +0000776 // Implementation Details
777private:
Chris Lattnerea862a32007-01-09 07:55:49 +0000778 /// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
779 void CreateModuleSlot(const GlobalValue *V);
Devang Patel983c6b12009-07-08 21:44:25 +0000780
781 /// CreateMetadataSlot - Insert the specified MDNode* into the slot table.
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000782 void CreateMetadataSlot(const MDNode *N);
Devang Patel983c6b12009-07-08 21:44:25 +0000783
Chris Lattnerea862a32007-01-09 07:55:49 +0000784 /// CreateFunctionSlot - Insert the specified Value* into the slot table.
785 void CreateFunctionSlot(const Value *V);
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000786
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000787 /// Insert the specified AttributeSet into the slot table.
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000788 void CreateAttributeSetSlot(AttributeSet AS);
Bill Wendling829b4782013-02-11 08:43:33 +0000789
Teresa Johnson8fc76662018-07-02 22:09:23 +0000790 inline void CreateModulePathSlot(StringRef Path);
Teresa Johnson08d5b4e2018-05-26 02:34:13 +0000791 void CreateGUIDSlot(GlobalValue::GUID GUID);
792
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000793 /// Add all of the module level global variables (and their initializers)
794 /// and function declarations, but not the contents of those functions.
795 void processModule();
Teresa Johnson08d5b4e2018-05-26 02:34:13 +0000796 void processIndex();
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000797
Devang Patel983c6b12009-07-08 21:44:25 +0000798 /// Add all of the functions arguments, basic blocks, and instructions.
Reid Spencer56010e42004-05-26 21:56:09 +0000799 void processFunction();
800
Peter Collingbournecceae7f2016-05-31 23:01:54 +0000801 /// Add the metadata directly attached to a GlobalObject.
802 void processGlobalObjectMetadata(const GlobalObject &GO);
803
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000804 /// Add all of the metadata from a function.
805 void processFunctionMetadata(const Function &F);
806
Duncan P. N. Exon Smith20b76ac2015-03-14 19:48:31 +0000807 /// Add all of the metadata from an instruction.
808 void processInstructionMetadata(const Instruction &I);
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000809};
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000810
811} // end namespace llvm
Duncan P. N. Exon Smith326921542015-06-26 22:04:20 +0000812
813ModuleSlotTracker::ModuleSlotTracker(SlotTracker &Machine, const Module *M,
814 const Function *F)
815 : M(M), F(F), Machine(&Machine) {}
816
Duncan P. N. Exon Smith6529ed42015-06-26 22:28:47 +0000817ModuleSlotTracker::ModuleSlotTracker(const Module *M,
818 bool ShouldInitializeAllMetadata)
Duncan P. N. Exon Smithaf0fdc22016-04-20 19:05:59 +0000819 : ShouldCreateStorage(M),
820 ShouldInitializeAllMetadata(ShouldInitializeAllMetadata), M(M) {}
Duncan P. N. Exon Smith326921542015-06-26 22:04:20 +0000821
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000822ModuleSlotTracker::~ModuleSlotTracker() = default;
Duncan P. N. Exon Smith326921542015-06-26 22:04:20 +0000823
Duncan P. N. Exon Smithaf0fdc22016-04-20 19:05:59 +0000824SlotTracker *ModuleSlotTracker::getMachine() {
825 if (!ShouldCreateStorage)
826 return Machine;
827
828 ShouldCreateStorage = false;
829 MachineStorage =
830 llvm::make_unique<SlotTracker>(M, ShouldInitializeAllMetadata);
831 Machine = MachineStorage.get();
832 return Machine;
833}
834
Duncan P. N. Exon Smith326921542015-06-26 22:04:20 +0000835void ModuleSlotTracker::incorporateFunction(const Function &F) {
Duncan P. N. Exon Smithaf0fdc22016-04-20 19:05:59 +0000836 // Using getMachine() may lazily create the slot tracker.
837 if (!getMachine())
Duncan P. N. Exon Smith326921542015-06-26 22:04:20 +0000838 return;
839
840 // Nothing to do if this is the right function already.
841 if (this->F == &F)
842 return;
843 if (this->F)
844 Machine->purgeFunction();
845 Machine->incorporateFunction(&F);
846 this->F = &F;
847}
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000848
Alex Lorenz991a6242015-07-27 22:31:04 +0000849int ModuleSlotTracker::getLocalSlot(const Value *V) {
850 assert(F && "No function incorporated");
851 return Machine->getLocalSlot(V);
852}
853
Chris Lattner3eee99c2008-08-19 04:36:02 +0000854static SlotTracker *createSlotTracker(const Value *V) {
855 if (const Argument *FA = dyn_cast<Argument>(V))
856 return new SlotTracker(FA->getParent());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000857
Chris Lattner3eee99c2008-08-19 04:36:02 +0000858 if (const Instruction *I = dyn_cast<Instruction>(V))
Andrew Trick2f0cbf62011-09-30 19:50:40 +0000859 if (I->getParent())
860 return new SlotTracker(I->getParent()->getParent());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000861
Chris Lattner3eee99c2008-08-19 04:36:02 +0000862 if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
863 return new SlotTracker(BB->getParent());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000864
Chris Lattner3eee99c2008-08-19 04:36:02 +0000865 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
866 return new SlotTracker(GV->getParent());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000867
Chris Lattner3eee99c2008-08-19 04:36:02 +0000868 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V))
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000869 return new SlotTracker(GA->getParent());
870
Dmitry Polukhina1feff72016-04-07 12:32:19 +0000871 if (const GlobalIFunc *GIF = dyn_cast<GlobalIFunc>(V))
872 return new SlotTracker(GIF->getParent());
873
Chris Lattner3eee99c2008-08-19 04:36:02 +0000874 if (const Function *Func = dyn_cast<Function>(V))
875 return new SlotTracker(Func);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000876
Craig Topperc6207612014-04-09 06:08:46 +0000877 return nullptr;
Chris Lattner3eee99c2008-08-19 04:36:02 +0000878}
879
880#if 0
David Greenec7f9b122010-01-05 01:29:26 +0000881#define ST_DEBUG(X) dbgs() << X
Chris Lattner3eee99c2008-08-19 04:36:02 +0000882#else
Chris Lattner604e3512008-08-19 04:47:09 +0000883#define ST_DEBUG(X)
Chris Lattner3eee99c2008-08-19 04:36:02 +0000884#endif
885
886// Module level constructor. Causes the contents of the Module (sans functions)
887// to be added to the slot table.
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000888SlotTracker::SlotTracker(const Module *M, bool ShouldInitializeAllMetadata)
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000889 : TheModule(M), ShouldInitializeAllMetadata(ShouldInitializeAllMetadata) {}
Chris Lattner3eee99c2008-08-19 04:36:02 +0000890
891// Function level constructor. Causes the contents of the Module and the one
892// function provided to be added to the slot table.
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000893SlotTracker::SlotTracker(const Function *F, bool ShouldInitializeAllMetadata)
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000894 : TheModule(F ? F->getParent() : nullptr), TheFunction(F),
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000895 ShouldInitializeAllMetadata(ShouldInitializeAllMetadata) {}
Chris Lattner3eee99c2008-08-19 04:36:02 +0000896
Teresa Johnson08d5b4e2018-05-26 02:34:13 +0000897SlotTracker::SlotTracker(const ModuleSummaryIndex *Index)
898 : TheModule(nullptr), ShouldInitializeAllMetadata(false), TheIndex(Index) {}
899
Teresa Johnsonfc801f52018-07-03 15:52:57 +0000900inline void SlotTracker::initializeIfNeeded() {
Chris Lattner3eee99c2008-08-19 04:36:02 +0000901 if (TheModule) {
902 processModule();
Craig Topperc6207612014-04-09 06:08:46 +0000903 TheModule = nullptr; ///< Prevent re-processing next time we're called.
Chris Lattner3eee99c2008-08-19 04:36:02 +0000904 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000905
Chris Lattner3eee99c2008-08-19 04:36:02 +0000906 if (TheFunction && !FunctionProcessed)
907 processFunction();
908}
909
Teresa Johnsonfc801f52018-07-03 15:52:57 +0000910void SlotTracker::initializeIndexIfNeeded() {
Teresa Johnson08d5b4e2018-05-26 02:34:13 +0000911 if (!TheIndex)
912 return;
913 processIndex();
914 TheIndex = nullptr; ///< Prevent re-processing next time we're called.
915}
916
Chris Lattner3eee99c2008-08-19 04:36:02 +0000917// Iterate through all the global variables, functions, and global
918// variable initializers and create slots for them.
919void SlotTracker::processModule() {
Chris Lattner604e3512008-08-19 04:47:09 +0000920 ST_DEBUG("begin processModule!\n");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000921
Chris Lattner3eee99c2008-08-19 04:36:02 +0000922 // Add all of the unnamed global variables to the value table.
Rafael Espindola5d8a1552015-06-17 17:33:37 +0000923 for (const GlobalVariable &Var : TheModule->globals()) {
924 if (!Var.hasName())
925 CreateModuleSlot(&Var);
Peter Collingbournecceae7f2016-05-31 23:01:54 +0000926 processGlobalObjectMetadata(Var);
Javed Absarf3d79042017-05-11 12:28:08 +0000927 auto Attrs = Var.getAttributes();
928 if (Attrs.hasAttributes())
929 CreateAttributeSetSlot(Attrs);
Devang Patel983c6b12009-07-08 21:44:25 +0000930 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000931
Rafael Espindola54fc2982015-06-17 17:53:31 +0000932 for (const GlobalAlias &A : TheModule->aliases()) {
933 if (!A.hasName())
934 CreateModuleSlot(&A);
935 }
936
Dmitry Polukhina1feff72016-04-07 12:32:19 +0000937 for (const GlobalIFunc &I : TheModule->ifuncs()) {
938 if (!I.hasName())
939 CreateModuleSlot(&I);
940 }
941
Devang Patel23e68302009-07-29 22:04:47 +0000942 // Add metadata used by named metadata.
Rafael Espindola5d8a1552015-06-17 17:33:37 +0000943 for (const NamedMDNode &NMD : TheModule->named_metadata()) {
944 for (unsigned i = 0, e = NMD.getNumOperands(); i != e; ++i)
945 CreateMetadataSlot(NMD.getOperand(i));
Devang Patel23e68302009-07-29 22:04:47 +0000946 }
947
Rafael Espindola5d8a1552015-06-17 17:33:37 +0000948 for (const Function &F : *TheModule) {
949 if (!F.hasName())
Bill Wendling829b4782013-02-11 08:43:33 +0000950 // Add all the unnamed functions to the table.
Rafael Espindola5d8a1552015-06-17 17:33:37 +0000951 CreateModuleSlot(&F);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000952
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000953 if (ShouldInitializeAllMetadata)
Rafael Espindola5d8a1552015-06-17 17:33:37 +0000954 processFunctionMetadata(F);
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +0000955
Bill Wendling829b4782013-02-11 08:43:33 +0000956 // Add all the function attributes to the table.
Bill Wendling90bc19c2013-02-20 07:21:42 +0000957 // FIXME: Add attributes of other objects?
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000958 AttributeSet FnAttrs = F.getAttributes().getFnAttributes();
959 if (FnAttrs.hasAttributes())
Bill Wendling829b4782013-02-11 08:43:33 +0000960 CreateAttributeSetSlot(FnAttrs);
961 }
962
Chris Lattner604e3512008-08-19 04:47:09 +0000963 ST_DEBUG("end processModule!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000964}
965
Chris Lattner3eee99c2008-08-19 04:36:02 +0000966// Process the arguments, basic blocks, and instructions of a function.
967void SlotTracker::processFunction() {
Chris Lattner604e3512008-08-19 04:47:09 +0000968 ST_DEBUG("begin processFunction!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000969 fNext = 0;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000970
Duncan P. N. Exon Smithadc54562015-09-11 01:34:59 +0000971 // Process function metadata if it wasn't hit at the module-level.
972 if (!ShouldInitializeAllMetadata)
973 processFunctionMetadata(*TheFunction);
974
Chris Lattner3eee99c2008-08-19 04:36:02 +0000975 // Add all the function arguments with no names.
976 for(Function::const_arg_iterator AI = TheFunction->arg_begin(),
977 AE = TheFunction->arg_end(); AI != AE; ++AI)
978 if (!AI->hasName())
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +0000979 CreateFunctionSlot(&*AI);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000980
Chris Lattner604e3512008-08-19 04:47:09 +0000981 ST_DEBUG("Inserting Instructions:\n");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000982
Chris Lattner3eee99c2008-08-19 04:36:02 +0000983 // Add all of the basic blocks and instructions with no names.
Duncan P. N. Exon Smith27f33ee2015-03-14 19:44:01 +0000984 for (auto &BB : *TheFunction) {
985 if (!BB.hasName())
986 CreateFunctionSlot(&BB);
Andrew Trickec4b6e72011-09-30 19:48:58 +0000987
Duncan P. N. Exon Smith27f33ee2015-03-14 19:44:01 +0000988 for (auto &I : BB) {
989 if (!I.getType()->isVoidTy() && !I.hasName())
990 CreateFunctionSlot(&I);
Andrew Trickec4b6e72011-09-30 19:48:58 +0000991
Duncan P. N. Exon Smith20b76ac2015-03-14 19:48:31 +0000992 // We allow direct calls to any llvm.foo function here, because the
993 // target may not be linked into the optimizer.
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000994 if (auto CS = ImmutableCallSite(&I)) {
Bill Wendlinga0323742013-02-22 09:09:42 +0000995 // Add all the call attributes to the table.
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000996 AttributeSet Attrs = CS.getAttributes().getFnAttributes();
997 if (Attrs.hasAttributes())
Bill Wendlinga0323742013-02-22 09:09:42 +0000998 CreateAttributeSetSlot(Attrs);
Chris Lattner58aff8f2010-05-10 20:53:17 +0000999 }
Devang Patel983c6b12009-07-08 21:44:25 +00001000 }
Chris Lattner3eee99c2008-08-19 04:36:02 +00001001 }
Devang Pateldec23fd2009-09-16 20:21:17 +00001002
Chris Lattner3eee99c2008-08-19 04:36:02 +00001003 FunctionProcessed = true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001004
Chris Lattner604e3512008-08-19 04:47:09 +00001005 ST_DEBUG("end processFunction!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +00001006}
1007
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00001008// Iterate through all the GUID in the index and create slots for them.
1009void SlotTracker::processIndex() {
1010 ST_DEBUG("begin processIndex!\n");
1011 assert(TheIndex);
1012
1013 // The first block of slots are just the module ids, which start at 0 and are
Teresa Johnson8fc76662018-07-02 22:09:23 +00001014 // assigned consecutively. Since the StringMap iteration order isn't
1015 // guaranteed, use a std::map to order by module ID before assigning slots.
1016 std::map<uint64_t, StringRef> ModuleIdToPathMap;
1017 for (auto &ModPath : TheIndex->modulePaths())
1018 ModuleIdToPathMap[ModPath.second.first] = ModPath.first();
1019 for (auto &ModPair : ModuleIdToPathMap)
1020 CreateModulePathSlot(ModPair.second);
1021
1022 // Start numbering the GUIDs after the module ids.
1023 GUIDNext = ModulePathNext;
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00001024
1025 for (auto &GlobalList : *TheIndex)
1026 CreateGUIDSlot(GlobalList.first);
1027
1028 for (auto &TId : TheIndex->typeIds())
1029 CreateGUIDSlot(GlobalValue::getGUID(TId.first));
1030
1031 ST_DEBUG("end processIndex!\n");
1032}
1033
Peter Collingbournecceae7f2016-05-31 23:01:54 +00001034void SlotTracker::processGlobalObjectMetadata(const GlobalObject &GO) {
Duncan P. N. Exon Smith3d4cd752015-04-24 22:04:41 +00001035 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
Peter Collingbournecceae7f2016-05-31 23:01:54 +00001036 GO.getAllMetadata(MDs);
Duncan P. N. Exon Smithadc54562015-09-11 01:34:59 +00001037 for (auto &MD : MDs)
1038 CreateMetadataSlot(MD.second);
Peter Collingbournecceae7f2016-05-31 23:01:54 +00001039}
Duncan P. N. Exon Smith3d4cd752015-04-24 22:04:41 +00001040
Peter Collingbournecceae7f2016-05-31 23:01:54 +00001041void SlotTracker::processFunctionMetadata(const Function &F) {
1042 processGlobalObjectMetadata(F);
Duncan P. N. Exon Smithadc54562015-09-11 01:34:59 +00001043 for (auto &BB : F) {
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +00001044 for (auto &I : BB)
1045 processInstructionMetadata(I);
Duncan P. N. Exon Smith3d4cd752015-04-24 22:04:41 +00001046 }
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +00001047}
1048
Duncan P. N. Exon Smith20b76ac2015-03-14 19:48:31 +00001049void SlotTracker::processInstructionMetadata(const Instruction &I) {
1050 // Process metadata used directly by intrinsics.
1051 if (const CallInst *CI = dyn_cast<CallInst>(&I))
1052 if (Function *F = CI->getCalledFunction())
Justin Lebar9cbc3012016-07-28 23:58:15 +00001053 if (F->isIntrinsic())
Duncan P. N. Exon Smith20b76ac2015-03-14 19:48:31 +00001054 for (auto &Op : I.operands())
1055 if (auto *V = dyn_cast_or_null<MetadataAsValue>(Op))
1056 if (MDNode *N = dyn_cast<MDNode>(V->getMetadata()))
1057 CreateMetadataSlot(N);
1058
1059 // Process metadata attached to this instruction.
1060 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
1061 I.getAllMetadata(MDs);
1062 for (auto &MD : MDs)
1063 CreateMetadataSlot(MD.second);
1064}
1065
Chris Lattner3eee99c2008-08-19 04:36:02 +00001066/// Clean up after incorporating a function. This is the only way to get out of
1067/// the function incorporation state that affects get*Slot/Create*Slot. Function
1068/// incorporation state is indicated by TheFunction != 0.
1069void SlotTracker::purgeFunction() {
Chris Lattner604e3512008-08-19 04:47:09 +00001070 ST_DEBUG("begin purgeFunction!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +00001071 fMap.clear(); // Simply discard the function level map
Craig Topperc6207612014-04-09 06:08:46 +00001072 TheFunction = nullptr;
Chris Lattner3eee99c2008-08-19 04:36:02 +00001073 FunctionProcessed = false;
Chris Lattner604e3512008-08-19 04:47:09 +00001074 ST_DEBUG("end purgeFunction!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +00001075}
1076
1077/// getGlobalSlot - Get the slot number of a global value.
1078int SlotTracker::getGlobalSlot(const GlobalValue *V) {
1079 // Check for uninitialized state and do lazy initialization.
Teresa Johnsonfc801f52018-07-03 15:52:57 +00001080 initializeIfNeeded();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001081
Jay Foaddbb221d2011-07-11 07:28:49 +00001082 // Find the value in the module map
Chris Lattner3eee99c2008-08-19 04:36:02 +00001083 ValueMap::iterator MI = mMap.find(V);
Dan Gohman1dd27572008-10-01 19:58:59 +00001084 return MI == mMap.end() ? -1 : (int)MI->second;
Chris Lattner3eee99c2008-08-19 04:36:02 +00001085}
1086
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00001087/// getMetadataSlot - Get the slot number of a MDNode.
1088int SlotTracker::getMetadataSlot(const MDNode *N) {
Devang Patel983c6b12009-07-08 21:44:25 +00001089 // Check for uninitialized state and do lazy initialization.
Teresa Johnsonfc801f52018-07-03 15:52:57 +00001090 initializeIfNeeded();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001091
Jay Foaddbb221d2011-07-11 07:28:49 +00001092 // Find the MDNode in the module map
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00001093 mdn_iterator MI = mdnMap.find(N);
Devang Patel983c6b12009-07-08 21:44:25 +00001094 return MI == mdnMap.end() ? -1 : (int)MI->second;
1095}
1096
Chris Lattner3eee99c2008-08-19 04:36:02 +00001097/// getLocalSlot - Get the slot number for a value that is local to a function.
1098int SlotTracker::getLocalSlot(const Value *V) {
1099 assert(!isa<Constant>(V) && "Can't get a constant or global slot with this!");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001100
Chris Lattner3eee99c2008-08-19 04:36:02 +00001101 // Check for uninitialized state and do lazy initialization.
Teresa Johnsonfc801f52018-07-03 15:52:57 +00001102 initializeIfNeeded();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001103
Chris Lattner3eee99c2008-08-19 04:36:02 +00001104 ValueMap::iterator FI = fMap.find(V);
Dan Gohman1dd27572008-10-01 19:58:59 +00001105 return FI == fMap.end() ? -1 : (int)FI->second;
Chris Lattner3eee99c2008-08-19 04:36:02 +00001106}
1107
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001108int SlotTracker::getAttributeGroupSlot(AttributeSet AS) {
Bill Wendling829b4782013-02-11 08:43:33 +00001109 // Check for uninitialized state and do lazy initialization.
Teresa Johnsonfc801f52018-07-03 15:52:57 +00001110 initializeIfNeeded();
Bill Wendling829b4782013-02-11 08:43:33 +00001111
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001112 // Find the AttributeSet in the module map.
Bill Wendling829b4782013-02-11 08:43:33 +00001113 as_iterator AI = asMap.find(AS);
1114 return AI == asMap.end() ? -1 : (int)AI->second;
1115}
Chris Lattner3eee99c2008-08-19 04:36:02 +00001116
Teresa Johnson8fc76662018-07-02 22:09:23 +00001117int SlotTracker::getModulePathSlot(StringRef Path) {
1118 // Check for uninitialized state and do lazy initialization.
Teresa Johnsonfc801f52018-07-03 15:52:57 +00001119 initializeIndexIfNeeded();
Teresa Johnson8fc76662018-07-02 22:09:23 +00001120
1121 // Find the Module path in the map
1122 auto I = ModulePathMap.find(Path);
1123 return I == ModulePathMap.end() ? -1 : (int)I->second;
1124}
1125
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00001126int SlotTracker::getGUIDSlot(GlobalValue::GUID GUID) {
1127 // Check for uninitialized state and do lazy initialization.
Teresa Johnsonfc801f52018-07-03 15:52:57 +00001128 initializeIndexIfNeeded();
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00001129
1130 // Find the GUID in the map
1131 guid_iterator I = GUIDMap.find(GUID);
1132 return I == GUIDMap.end() ? -1 : (int)I->second;
1133}
1134
Chris Lattner3eee99c2008-08-19 04:36:02 +00001135/// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
1136void SlotTracker::CreateModuleSlot(const GlobalValue *V) {
1137 assert(V && "Can't insert a null Value into SlotTracker!");
Chris Lattner031560c2009-12-29 07:25:48 +00001138 assert(!V->getType()->isVoidTy() && "Doesn't need a slot!");
Chris Lattner3eee99c2008-08-19 04:36:02 +00001139 assert(!V->hasName() && "Doesn't need a slot!");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001140
Chris Lattner3eee99c2008-08-19 04:36:02 +00001141 unsigned DestSlot = mNext++;
1142 mMap[V] = DestSlot;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001143
Chris Lattner604e3512008-08-19 04:47:09 +00001144 ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" <<
Chris Lattner3eee99c2008-08-19 04:36:02 +00001145 DestSlot << " [");
Dmitry Polukhina1feff72016-04-07 12:32:19 +00001146 // G = Global, F = Function, A = Alias, I = IFunc, o = other
Chris Lattner604e3512008-08-19 04:47:09 +00001147 ST_DEBUG((isa<GlobalVariable>(V) ? 'G' :
Chris Lattner3eee99c2008-08-19 04:36:02 +00001148 (isa<Function>(V) ? 'F' :
Dmitry Polukhina1feff72016-04-07 12:32:19 +00001149 (isa<GlobalAlias>(V) ? 'A' :
1150 (isa<GlobalIFunc>(V) ? 'I' : 'o')))) << "]\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +00001151}
1152
Chris Lattner3eee99c2008-08-19 04:36:02 +00001153/// CreateSlot - Create a new slot for the specified value if it has no name.
1154void SlotTracker::CreateFunctionSlot(const Value *V) {
Chris Lattner031560c2009-12-29 07:25:48 +00001155 assert(!V->getType()->isVoidTy() && !V->hasName() && "Doesn't need a slot!");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001156
Chris Lattner3eee99c2008-08-19 04:36:02 +00001157 unsigned DestSlot = fNext++;
1158 fMap[V] = DestSlot;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001159
Chris Lattner3eee99c2008-08-19 04:36:02 +00001160 // G = Global, F = Function, o = other
Chris Lattner604e3512008-08-19 04:47:09 +00001161 ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" <<
Chris Lattner3eee99c2008-08-19 04:36:02 +00001162 DestSlot << " [o]\n");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001163}
Chris Lattner3eee99c2008-08-19 04:36:02 +00001164
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00001165/// CreateModuleSlot - Insert the specified MDNode* into the slot table.
1166void SlotTracker::CreateMetadataSlot(const MDNode *N) {
1167 assert(N && "Can't insert a null Value into SlotTracker!");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001168
Reid Kleckner6d353342017-08-23 20:31:27 +00001169 // Don't make slots for DIExpressions. We just print them inline everywhere.
1170 if (isa<DIExpression>(N))
1171 return;
1172
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001173 unsigned DestSlot = mdnNext;
1174 if (!mdnMap.insert(std::make_pair(N, DestSlot)).second)
1175 return;
1176 ++mdnNext;
Devang Patel983c6b12009-07-08 21:44:25 +00001177
Chris Lattner0d50bdd2009-12-31 02:27:30 +00001178 // Recursively add any MDNodes referenced by operands.
1179 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1180 if (const MDNode *Op = dyn_cast_or_null<MDNode>(N->getOperand(i)))
1181 CreateMetadataSlot(Op);
Devang Patel983c6b12009-07-08 21:44:25 +00001182}
Chris Lattner3eee99c2008-08-19 04:36:02 +00001183
Reid Klecknerc2cb5602017-04-12 00:38:00 +00001184void SlotTracker::CreateAttributeSetSlot(AttributeSet AS) {
1185 assert(AS.hasAttributes() && "Doesn't need a slot!");
Bill Wendling829b4782013-02-11 08:43:33 +00001186
1187 as_iterator I = asMap.find(AS);
1188 if (I != asMap.end())
1189 return;
1190
1191 unsigned DestSlot = asNext++;
1192 asMap[AS] = DestSlot;
1193}
1194
Teresa Johnson8fc76662018-07-02 22:09:23 +00001195/// Create a new slot for the specified Module
1196void SlotTracker::CreateModulePathSlot(StringRef Path) {
1197 ModulePathMap[Path] = ModulePathNext++;
1198}
1199
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00001200/// Create a new slot for the specified GUID
1201void SlotTracker::CreateGUIDSlot(GlobalValue::GUID GUID) {
1202 GUIDMap[GUID] = GUIDNext++;
1203}
1204
Chris Lattner3eee99c2008-08-19 04:36:02 +00001205//===----------------------------------------------------------------------===//
1206// AsmWriter Implementation
1207//===----------------------------------------------------------------------===//
Chris Lattner7f8845a2002-07-23 18:07:49 +00001208
Dan Gohman12dad632009-08-12 20:56:03 +00001209static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
Dan Gohman8061d9e2009-08-13 15:27:57 +00001210 TypePrinting *TypePrinter,
Dan Gohmana2489d12010-07-20 23:55:01 +00001211 SlotTracker *Machine,
1212 const Module *Context);
Reid Spencer58d30f22004-07-04 11:50:43 +00001213
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001214static void WriteAsOperandInternal(raw_ostream &Out, const Metadata *MD,
1215 TypePrinting *TypePrinter,
1216 SlotTracker *Machine, const Module *Context,
1217 bool FromValue = false);
1218
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001219static void writeAtomicRMWOperation(raw_ostream &Out,
1220 AtomicRMWInst::BinOp Op) {
1221 switch (Op) {
1222 default: Out << " <unknown operation " << Op << ">"; break;
1223 case AtomicRMWInst::Xchg: Out << " xchg"; break;
1224 case AtomicRMWInst::Add: Out << " add"; break;
1225 case AtomicRMWInst::Sub: Out << " sub"; break;
1226 case AtomicRMWInst::And: Out << " and"; break;
1227 case AtomicRMWInst::Nand: Out << " nand"; break;
1228 case AtomicRMWInst::Or: Out << " or"; break;
1229 case AtomicRMWInst::Xor: Out << " xor"; break;
1230 case AtomicRMWInst::Max: Out << " max"; break;
1231 case AtomicRMWInst::Min: Out << " min"; break;
1232 case AtomicRMWInst::UMax: Out << " umax"; break;
1233 case AtomicRMWInst::UMin: Out << " umin"; break;
1234 }
1235}
Devang Patel983c6b12009-07-08 21:44:25 +00001236
Dan Gohman12dad632009-08-12 20:56:03 +00001237static void WriteOptimizationInfo(raw_ostream &Out, const User *U) {
Michael Ilseman92053172012-11-27 00:42:44 +00001238 if (const FPMathOperator *FPO = dyn_cast<const FPMathOperator>(U)) {
Sanjay Patel629c4112017-11-06 16:27:15 +00001239 // 'Fast' is an abbreviation for all fast-math-flags.
1240 if (FPO->isFast())
Michael Ilseman92053172012-11-27 00:42:44 +00001241 Out << " fast";
1242 else {
Sanjay Patel629c4112017-11-06 16:27:15 +00001243 if (FPO->hasAllowReassoc())
1244 Out << " reassoc";
Michael Ilseman92053172012-11-27 00:42:44 +00001245 if (FPO->hasNoNaNs())
1246 Out << " nnan";
1247 if (FPO->hasNoInfs())
1248 Out << " ninf";
1249 if (FPO->hasNoSignedZeros())
1250 Out << " nsz";
1251 if (FPO->hasAllowReciprocal())
1252 Out << " arcp";
Adam Nemetcd847a82017-03-28 20:11:52 +00001253 if (FPO->hasAllowContract())
1254 Out << " contract";
Sanjay Patel629c4112017-11-06 16:27:15 +00001255 if (FPO->hasApproxFunc())
1256 Out << " afn";
Michael Ilseman92053172012-11-27 00:42:44 +00001257 }
1258 }
1259
Dan Gohman0ebd6962009-07-20 21:19:07 +00001260 if (const OverflowingBinaryOperator *OBO =
1261 dyn_cast<OverflowingBinaryOperator>(U)) {
Dan Gohman16f54152009-08-20 17:11:38 +00001262 if (OBO->hasNoUnsignedWrap())
Dan Gohman9c7f8082009-07-27 16:11:46 +00001263 Out << " nuw";
Dan Gohman16f54152009-08-20 17:11:38 +00001264 if (OBO->hasNoSignedWrap())
Dan Gohman9c7f8082009-07-27 16:11:46 +00001265 Out << " nsw";
Chris Lattner35315d02011-02-06 21:44:57 +00001266 } else if (const PossiblyExactOperator *Div =
1267 dyn_cast<PossiblyExactOperator>(U)) {
Dan Gohman0ebd6962009-07-20 21:19:07 +00001268 if (Div->isExact())
Dan Gohman9c7f8082009-07-27 16:11:46 +00001269 Out << " exact";
Dan Gohman1639c392009-07-27 21:53:46 +00001270 } else if (const GEPOperator *GEP = dyn_cast<GEPOperator>(U)) {
1271 if (GEP->isInBounds())
1272 Out << " inbounds";
Dan Gohman0ebd6962009-07-20 21:19:07 +00001273 }
1274}
1275
Dan Gohmanefb8dbb2010-07-14 20:57:55 +00001276static void WriteConstantInternal(raw_ostream &Out, const Constant *CV,
1277 TypePrinting &TypePrinter,
Dan Gohmana2489d12010-07-20 23:55:01 +00001278 SlotTracker *Machine,
1279 const Module *Context) {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001280 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00001281 if (CI->getType()->isIntegerTy(1)) {
Reid Spencercddc9df2007-01-12 04:24:46 +00001282 Out << (CI->getZExtValue() ? "true" : "false");
Chris Lattner17f71652008-08-17 07:19:36 +00001283 return;
1284 }
1285 Out << CI->getValue();
1286 return;
1287 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001288
Chris Lattner17f71652008-08-17 07:19:36 +00001289 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Serguei Katkov3a46eb42017-04-21 02:52:17 +00001290 const APFloat &APF = CFP->getValueAPF();
1291 if (&APF.getSemantics() == &APFloat::IEEEsingle() ||
1292 &APF.getSemantics() == &APFloat::IEEEdouble()) {
Dale Johannesen028084e2007-09-12 03:30:33 +00001293 // We would like to output the FP constant value in exponential notation,
1294 // but we cannot do this if doing so will lose precision. Check here to
1295 // make sure that we only output it in exponential format if we can parse
1296 // the value back and get the same value.
1297 //
Dale Johannesen1f864982009-01-21 20:32:55 +00001298 bool ignored;
Serguei Katkov3a46eb42017-04-21 02:52:17 +00001299 bool isDouble = &APF.getSemantics() == &APFloat::IEEEdouble();
1300 bool isInf = APF.isInfinity();
1301 bool isNaN = APF.isNaN();
Justin Bognerb609b6b2015-12-04 01:14:24 +00001302 if (!isInf && !isNaN) {
Serguei Katkov3a46eb42017-04-21 02:52:17 +00001303 double Val = isDouble ? APF.convertToDouble() : APF.convertToFloat();
Dan Gohman518cda42011-12-17 00:04:22 +00001304 SmallString<128> StrVal;
Serguei Katkov3a46eb42017-04-21 02:52:17 +00001305 APF.toString(StrVal, 6, 0, false);
Dan Gohman518cda42011-12-17 00:04:22 +00001306 // Check to make sure that the stringized number is not some string like
1307 // "Inf" or NaN, that atof will accept, but the lexer will not. Check
1308 // that the string matches the "[-+]?[0-9]" regex.
1309 //
Serguei Katkovc9a752c2017-04-21 06:14:38 +00001310 assert(((StrVal[0] >= '0' && StrVal[0] <= '9') ||
1311 ((StrVal[0] == '-' || StrVal[0] == '+') &&
1312 (StrVal[1] >= '0' && StrVal[1] <= '9'))) &&
1313 "[-+]?[0-9] regex does not match!");
Serguei Katkov3a46eb42017-04-21 02:52:17 +00001314 // Reparse stringized version!
1315 if (APFloat(APFloat::IEEEdouble(), StrVal).convertToDouble() == Val) {
1316 Out << StrVal;
1317 return;
Dale Johannesen028084e2007-09-12 03:30:33 +00001318 }
Chris Lattner1e194682002-04-18 18:53:13 +00001319 }
Dale Johannesen028084e2007-09-12 03:30:33 +00001320 // Otherwise we could not reparse it to exactly the same value, so we must
Dale Johannesen1f864982009-01-21 20:32:55 +00001321 // output the string in hexadecimal format! Note that loading and storing
1322 // floating point types changes the bits of NaNs on some hosts, notably
1323 // x86, so we must not use these types.
Benjamin Kramer86c77412014-03-15 18:47:07 +00001324 static_assert(sizeof(double) == sizeof(uint64_t),
1325 "assuming that double is 64 bits!");
Serguei Katkov3a46eb42017-04-21 02:52:17 +00001326 APFloat apf = APF;
Justin Bognerb609b6b2015-12-04 01:14:24 +00001327 // Floats are represented in ASCII IR as double, convert.
Dale Johannesen1f864982009-01-21 20:32:55 +00001328 if (!isDouble)
Stephan Bergmann17c7f702016-12-14 11:57:17 +00001329 apf.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven,
Dale Johannesen1f864982009-01-21 20:32:55 +00001330 &ignored);
Justin Bogner93289572015-12-04 02:14:34 +00001331 Out << format_hex(apf.bitcastToAPInt().getZExtValue(), 0, /*Upper=*/true);
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001332 return;
1333 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001334
Tobias Grosser6b31d172012-05-24 15:59:06 +00001335 // Either half, or some form of long double.
1336 // These appear as a magic letter identifying the type, then a
1337 // fixed number of hex digits.
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001338 Out << "0x";
Serguei Katkov3a46eb42017-04-21 02:52:17 +00001339 APInt API = APF.bitcastToAPInt();
1340 if (&APF.getSemantics() == &APFloat::x87DoubleExtended()) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001341 Out << 'K';
Justin Bogner93289572015-12-04 02:14:34 +00001342 Out << format_hex_no_prefix(API.getHiBits(16).getZExtValue(), 4,
1343 /*Upper=*/true);
1344 Out << format_hex_no_prefix(API.getLoBits(64).getZExtValue(), 16,
1345 /*Upper=*/true);
Dale Johannesen93eefa02009-03-23 21:16:53 +00001346 return;
Serguei Katkov3a46eb42017-04-21 02:52:17 +00001347 } else if (&APF.getSemantics() == &APFloat::IEEEquad()) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001348 Out << 'L';
Justin Bogner93289572015-12-04 02:14:34 +00001349 Out << format_hex_no_prefix(API.getLoBits(64).getZExtValue(), 16,
1350 /*Upper=*/true);
1351 Out << format_hex_no_prefix(API.getHiBits(64).getZExtValue(), 16,
1352 /*Upper=*/true);
Serguei Katkov3a46eb42017-04-21 02:52:17 +00001353 } else if (&APF.getSemantics() == &APFloat::PPCDoubleDouble()) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001354 Out << 'M';
Justin Bogner93289572015-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 Katkov3a46eb42017-04-21 02:52:17 +00001359 } else if (&APF.getSemantics() == &APFloat::IEEEhalf()) {
Tobias Grosser6b31d172012-05-24 15:59:06 +00001360 Out << 'H';
Justin Bogner93289572015-12-04 02:14:34 +00001361 Out << format_hex_no_prefix(API.getZExtValue(), 4,
1362 /*Upper=*/true);
Tobias Grosser6b31d172012-05-24 15:59:06 +00001363 } else
Torok Edwinfbcc6632009-07-14 16:55:14 +00001364 llvm_unreachable("Unsupported floating point type");
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001365 return;
1366 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001367
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001368 if (isa<ConstantAggregateZero>(CV)) {
Chris Lattner76b2ff42004-02-15 05:55:15 +00001369 Out << "zeroinitializer";
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001370 return;
1371 }
Andrew Trickec4b6e72011-09-30 19:48:58 +00001372
Chris Lattner214cc702009-10-28 03:38:12 +00001373 if (const BlockAddress *BA = dyn_cast<BlockAddress>(CV)) {
1374 Out << "blockaddress(";
Dan Gohmana2489d12010-07-20 23:55:01 +00001375 WriteAsOperandInternal(Out, BA->getFunction(), &TypePrinter, Machine,
1376 Context);
Chris Lattner214cc702009-10-28 03:38:12 +00001377 Out << ", ";
Dan Gohmana2489d12010-07-20 23:55:01 +00001378 WriteAsOperandInternal(Out, BA->getBasicBlock(), &TypePrinter, Machine,
1379 Context);
Chris Lattner214cc702009-10-28 03:38:12 +00001380 Out << ")";
1381 return;
1382 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001383
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001384 if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001385 Type *ETy = CA->getType()->getElementType();
Chris Lattnercf9e8f62012-02-05 02:29:43 +00001386 Out << '[';
1387 TypePrinter.print(ETy, Out);
1388 Out << ' ';
1389 WriteAsOperandInternal(Out, CA->getOperand(0),
1390 &TypePrinter, Machine,
1391 Context);
1392 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
1393 Out << ", ";
Chris Lattner9c181f62012-01-31 03:15:40 +00001394 TypePrinter.print(ETy, Out);
1395 Out << ' ';
Chris Lattnercf9e8f62012-02-05 02:29:43 +00001396 WriteAsOperandInternal(Out, CA->getOperand(i), &TypePrinter, Machine,
Chris Lattner9c181f62012-01-31 03:15:40 +00001397 Context);
Chris Lattnerd84bb632002-04-16 21:36:08 +00001398 }
Chris Lattnercf9e8f62012-02-05 02:29:43 +00001399 Out << ']';
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001400 return;
1401 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001402
Chris Lattnerfa775002012-01-26 02:32:04 +00001403 if (const ConstantDataArray *CA = dyn_cast<ConstantDataArray>(CV)) {
1404 // As a special case, print the array as a string if it is an array of
1405 // i8 with ConstantInt values.
1406 if (CA->isString()) {
1407 Out << "c\"";
Jonas Devlieghere745918ff2018-05-31 17:01:42 +00001408 printEscapedString(CA->getAsString(), Out);
Chris Lattnerfa775002012-01-26 02:32:04 +00001409 Out << '"';
1410 return;
1411 }
1412
1413 Type *ETy = CA->getType()->getElementType();
1414 Out << '[';
Chris Lattner9c181f62012-01-31 03:15:40 +00001415 TypePrinter.print(ETy, Out);
1416 Out << ' ';
1417 WriteAsOperandInternal(Out, CA->getElementAsConstant(0),
1418 &TypePrinter, Machine,
1419 Context);
1420 for (unsigned i = 1, e = CA->getNumElements(); i != e; ++i) {
1421 Out << ", ";
Chris Lattnerfa775002012-01-26 02:32:04 +00001422 TypePrinter.print(ETy, Out);
1423 Out << ' ';
Chris Lattner9c181f62012-01-31 03:15:40 +00001424 WriteAsOperandInternal(Out, CA->getElementAsConstant(i), &TypePrinter,
1425 Machine, Context);
Chris Lattnerfa775002012-01-26 02:32:04 +00001426 }
Chris Lattner9c181f62012-01-31 03:15:40 +00001427 Out << ']';
Chris Lattnerfa775002012-01-26 02:32:04 +00001428 return;
1429 }
1430
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001431 if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
Andrew Lenharth0d124b82007-01-08 18:21:30 +00001432 if (CS->getType()->isPacked())
1433 Out << '<';
Misha Brukman21bbdb92004-06-04 21:11:51 +00001434 Out << '{';
Jim Laskey3bb78742006-02-25 12:27:03 +00001435 unsigned N = CS->getNumOperands();
1436 if (N) {
Chris Lattner604e3512008-08-19 04:47:09 +00001437 Out << ' ';
Chris Lattnere101c442009-02-28 21:26:53 +00001438 TypePrinter.print(CS->getOperand(0)->getType(), Out);
Dan Gohman81313fd2008-09-14 17:21:12 +00001439 Out << ' ';
Chris Lattnerd84bb632002-04-16 21:36:08 +00001440
Dan Gohmana2489d12010-07-20 23:55:01 +00001441 WriteAsOperandInternal(Out, CS->getOperand(0), &TypePrinter, Machine,
1442 Context);
Chris Lattnerd84bb632002-04-16 21:36:08 +00001443
Jim Laskey3bb78742006-02-25 12:27:03 +00001444 for (unsigned i = 1; i < N; i++) {
Chris Lattnerd84bb632002-04-16 21:36:08 +00001445 Out << ", ";
Chris Lattnere101c442009-02-28 21:26:53 +00001446 TypePrinter.print(CS->getOperand(i)->getType(), Out);
Dan Gohman81313fd2008-09-14 17:21:12 +00001447 Out << ' ';
Chris Lattnerd84bb632002-04-16 21:36:08 +00001448
Dan Gohmana2489d12010-07-20 23:55:01 +00001449 WriteAsOperandInternal(Out, CS->getOperand(i), &TypePrinter, Machine,
1450 Context);
Chris Lattnerd84bb632002-04-16 21:36:08 +00001451 }
Dan Gohman81313fd2008-09-14 17:21:12 +00001452 Out << ' ';
Chris Lattnerd84bb632002-04-16 21:36:08 +00001453 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001454
Dan Gohman81313fd2008-09-14 17:21:12 +00001455 Out << '}';
Andrew Lenharth0d124b82007-01-08 18:21:30 +00001456 if (CS->getType()->isPacked())
1457 Out << '>';
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001458 return;
1459 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001460
Chris Lattnerfa775002012-01-26 02:32:04 +00001461 if (isa<ConstantVector>(CV) || isa<ConstantDataVector>(CV)) {
1462 Type *ETy = CV->getType()->getVectorElementType();
Dan Gohman1262a252009-02-11 00:25:25 +00001463 Out << '<';
Chris Lattnere101c442009-02-28 21:26:53 +00001464 TypePrinter.print(ETy, Out);
Dan Gohman81313fd2008-09-14 17:21:12 +00001465 Out << ' ';
Chris Lattnerfa775002012-01-26 02:32:04 +00001466 WriteAsOperandInternal(Out, CV->getAggregateElement(0U), &TypePrinter,
1467 Machine, Context);
1468 for (unsigned i = 1, e = CV->getType()->getVectorNumElements(); i != e;++i){
Chris Lattner585297e82008-08-19 05:26:17 +00001469 Out << ", ";
Chris Lattnere101c442009-02-28 21:26:53 +00001470 TypePrinter.print(ETy, Out);
Dan Gohman81313fd2008-09-14 17:21:12 +00001471 Out << ' ';
Chris Lattnerfa775002012-01-26 02:32:04 +00001472 WriteAsOperandInternal(Out, CV->getAggregateElement(i), &TypePrinter,
1473 Machine, Context);
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001474 }
Dan Gohman1262a252009-02-11 00:25:25 +00001475 Out << '>';
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001476 return;
1477 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001478
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001479 if (isa<ConstantPointerNull>(CV)) {
Chris Lattnerd84bb632002-04-16 21:36:08 +00001480 Out << "null";
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001481 return;
1482 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001483
David Majnemerf0f224d2015-11-11 21:57:16 +00001484 if (isa<ConstantTokenNone>(CV)) {
1485 Out << "none";
1486 return;
1487 }
1488
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001489 if (isa<UndefValue>(CV)) {
Chris Lattner5e0b9f22004-10-16 18:08:06 +00001490 Out << "undef";
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001491 return;
1492 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001493
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001494 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Reid Spencer812a1be2006-12-04 05:19:18 +00001495 Out << CE->getOpcodeName();
Dan Gohman9c7f8082009-07-27 16:11:46 +00001496 WriteOptimizationInfo(Out, CE);
Reid Spencer812a1be2006-12-04 05:19:18 +00001497 if (CE->isCompare())
Tim Northoverde3aea0412016-08-17 20:25:25 +00001498 Out << ' ' << CmpInst::getPredicateName(
1499 static_cast<CmpInst::Predicate>(CE->getPredicate()));
Reid Spencer812a1be2006-12-04 05:19:18 +00001500 Out << " (";
Misha Brukmanb1c93172005-04-21 23:48:37 +00001501
Peter Collingbourned93620b2016-11-10 22:34:55 +00001502 Optional<unsigned> InRangeOp;
David Blaikief72d05b2015-03-13 18:20:45 +00001503 if (const GEPOperator *GEP = dyn_cast<GEPOperator>(CE)) {
David Blaikied06654f2015-07-29 20:26:23 +00001504 TypePrinter.print(GEP->getSourceElementType(), Out);
David Blaikief72d05b2015-03-13 18:20:45 +00001505 Out << ", ";
Peter Collingbourned93620b2016-11-10 22:34:55 +00001506 InRangeOp = GEP->getInRangeIndex();
1507 if (InRangeOp)
1508 ++*InRangeOp;
David Blaikief72d05b2015-03-13 18:20:45 +00001509 }
1510
Vikram S. Adveb952b542002-07-14 23:14:45 +00001511 for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) {
Zachary Turner804d5022016-11-11 23:58:11 +00001512 if (InRangeOp && unsigned(OI - CE->op_begin()) == *InRangeOp)
Peter Collingbourned93620b2016-11-10 22:34:55 +00001513 Out << "inrange ";
Chris Lattnere101c442009-02-28 21:26:53 +00001514 TypePrinter.print((*OI)->getType(), Out);
Dan Gohman81313fd2008-09-14 17:21:12 +00001515 Out << ' ';
Dan Gohmana2489d12010-07-20 23:55:01 +00001516 WriteAsOperandInternal(Out, *OI, &TypePrinter, Machine, Context);
Vikram S. Adveb952b542002-07-14 23:14:45 +00001517 if (OI+1 != CE->op_end())
Chris Lattner3cd8c562002-07-30 18:54:25 +00001518 Out << ", ";
Vikram S. Adveb952b542002-07-14 23:14:45 +00001519 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001520
Dan Gohmana76f0f72008-05-31 19:12:39 +00001521 if (CE->hasIndices()) {
Jay Foad0091fe82011-04-13 15:22:40 +00001522 ArrayRef<unsigned> Indices = CE->getIndices();
Dan Gohmana76f0f72008-05-31 19:12:39 +00001523 for (unsigned i = 0, e = Indices.size(); i != e; ++i)
1524 Out << ", " << Indices[i];
1525 }
1526
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001527 if (CE->isCast()) {
Chris Lattner83b396b2002-08-15 19:37:43 +00001528 Out << " to ";
Chris Lattnere101c442009-02-28 21:26:53 +00001529 TypePrinter.print(CE->getType(), Out);
Chris Lattner83b396b2002-08-15 19:37:43 +00001530 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001531
Misha Brukman21bbdb92004-06-04 21:11:51 +00001532 Out << ')';
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001533 return;
Chris Lattnerd84bb632002-04-16 21:36:08 +00001534 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001535
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001536 Out << "<placeholder or erroneous Constant>";
Chris Lattnerd84bb632002-04-16 21:36:08 +00001537}
1538
Duncan P. N. Exon Smitha6de6a42015-01-12 23:45:31 +00001539static void writeMDTuple(raw_ostream &Out, const MDTuple *Node,
1540 TypePrinting *TypePrinter, SlotTracker *Machine,
1541 const Module *Context) {
Chris Lattnercdec5812009-12-31 02:31:59 +00001542 Out << "!{";
1543 for (unsigned mi = 0, me = Node->getNumOperands(); mi != me; ++mi) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001544 const Metadata *MD = Node->getOperand(mi);
1545 if (!MD)
Chris Lattnercdec5812009-12-31 02:31:59 +00001546 Out << "null";
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001547 else if (auto *MDV = dyn_cast<ValueAsMetadata>(MD)) {
1548 Value *V = MDV->getValue();
Chris Lattnercdec5812009-12-31 02:31:59 +00001549 TypePrinter->print(V->getType(), Out);
1550 Out << ' ';
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001551 WriteAsOperandInternal(Out, V, TypePrinter, Machine, Context);
1552 } else {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001553 WriteAsOperandInternal(Out, MD, TypePrinter, Machine, Context);
Chris Lattnercdec5812009-12-31 02:31:59 +00001554 }
1555 if (mi + 1 != me)
1556 Out << ", ";
1557 }
Andrew Trickec4b6e72011-09-30 19:48:58 +00001558
Chris Lattnercdec5812009-12-31 02:31:59 +00001559 Out << "}";
1560}
1561
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +00001562namespace {
Eugene Zelenkode6cce22017-06-19 22:05:08 +00001563
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +00001564struct FieldSeparator {
Eugene Zelenkode6cce22017-06-19 22:05:08 +00001565 bool Skip = true;
Duncan P. N. Exon Smith70ab3d22015-02-21 01:02:18 +00001566 const char *Sep;
Eugene Zelenkode6cce22017-06-19 22:05:08 +00001567
1568 FieldSeparator(const char *Sep = ", ") : Sep(Sep) {}
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +00001569};
Eugene Zelenkode6cce22017-06-19 22:05:08 +00001570
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +00001571raw_ostream &operator<<(raw_ostream &OS, FieldSeparator &FS) {
1572 if (FS.Skip) {
1573 FS.Skip = false;
1574 return OS;
1575 }
Duncan P. N. Exon Smith70ab3d22015-02-21 01:02:18 +00001576 return OS << FS.Sep;
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +00001577}
Eugene Zelenkode6cce22017-06-19 22:05:08 +00001578
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001579struct MDFieldPrinter {
1580 raw_ostream &Out;
1581 FieldSeparator FS;
Eugene Zelenkode6cce22017-06-19 22:05:08 +00001582 TypePrinting *TypePrinter = nullptr;
1583 SlotTracker *Machine = nullptr;
1584 const Module *Context = nullptr;
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001585
Eugene Zelenkode6cce22017-06-19 22:05:08 +00001586 explicit MDFieldPrinter(raw_ostream &Out) : Out(Out) {}
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001587 MDFieldPrinter(raw_ostream &Out, TypePrinting *TypePrinter,
1588 SlotTracker *Machine, const Module *Context)
1589 : Out(Out), TypePrinter(TypePrinter), Machine(Machine), Context(Context) {
1590 }
Eugene Zelenkode6cce22017-06-19 22:05:08 +00001591
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001592 void printTag(const DINode *N);
Amjad Abouda9bcf162015-12-10 12:56:35 +00001593 void printMacinfoType(const DIMacroNode *N);
Scott Linder71603842018-02-12 19:45:54 +00001594 void printChecksum(const DIFile::ChecksumInfo<StringRef> &N);
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001595 void printString(StringRef Name, StringRef Value,
1596 bool ShouldSkipEmpty = true);
1597 void printMetadata(StringRef Name, const Metadata *MD,
1598 bool ShouldSkipNull = true);
1599 template <class IntTy>
1600 void printInt(StringRef Name, IntTy Int, bool ShouldSkipZero = true);
David Blaikiea01f2952016-08-24 18:29:49 +00001601 void printBool(StringRef Name, bool Value, Optional<bool> Default = None);
Leny Kholodov5fcc4182016-09-06 10:46:28 +00001602 void printDIFlags(StringRef Name, DINode::DIFlags Flags);
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001603 template <class IntTy, class Stringifier>
1604 void printDwarfEnum(StringRef Name, IntTy Value, Stringifier toString,
1605 bool ShouldSkipZero = true);
Adrian Prantlb939a252016-03-31 23:56:58 +00001606 void printEmissionKind(StringRef Name, DICompileUnit::DebugEmissionKind EK);
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001607};
Eugene Zelenkode6cce22017-06-19 22:05:08 +00001608
1609} // end anonymous namespace
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +00001610
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001611void MDFieldPrinter::printTag(const DINode *N) {
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001612 Out << FS << "tag: ";
Mehdi Amini149f6ea2016-10-05 05:59:29 +00001613 auto Tag = dwarf::TagString(N->getTag());
1614 if (!Tag.empty())
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001615 Out << Tag;
1616 else
1617 Out << N->getTag();
1618}
1619
Amjad Abouda9bcf162015-12-10 12:56:35 +00001620void MDFieldPrinter::printMacinfoType(const DIMacroNode *N) {
1621 Out << FS << "type: ";
Mehdi Amini149f6ea2016-10-05 05:59:29 +00001622 auto Type = dwarf::MacinfoString(N->getMacinfoType());
1623 if (!Type.empty())
Amjad Abouda9bcf162015-12-10 12:56:35 +00001624 Out << Type;
1625 else
1626 Out << N->getMacinfoType();
1627}
1628
Scott Linder71603842018-02-12 19:45:54 +00001629void MDFieldPrinter::printChecksum(
1630 const DIFile::ChecksumInfo<StringRef> &Checksum) {
1631 Out << FS << "checksumkind: " << Checksum.getKindAsString();
1632 printString("checksum", Checksum.Value, /* ShouldSkipEmpty */ false);
Amjad Aboud7faeecc2016-12-25 10:12:09 +00001633}
1634
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001635void MDFieldPrinter::printString(StringRef Name, StringRef Value,
1636 bool ShouldSkipEmpty) {
1637 if (ShouldSkipEmpty && Value.empty())
1638 return;
1639
1640 Out << FS << Name << ": \"";
Jonas Devlieghere745918ff2018-05-31 17:01:42 +00001641 printEscapedString(Value, Out);
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001642 Out << "\"";
1643}
1644
Duncan P. N. Exon Smith61a09332015-02-06 22:27:22 +00001645static void writeMetadataAsOperand(raw_ostream &Out, const Metadata *MD,
1646 TypePrinting *TypePrinter,
1647 SlotTracker *Machine,
1648 const Module *Context) {
1649 if (!MD) {
1650 Out << "null";
1651 return;
1652 }
1653 WriteAsOperandInternal(Out, MD, TypePrinter, Machine, Context);
1654}
1655
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001656void MDFieldPrinter::printMetadata(StringRef Name, const Metadata *MD,
1657 bool ShouldSkipNull) {
1658 if (ShouldSkipNull && !MD)
Duncan P. N. Exon Smith79cf9702015-02-28 22:16:56 +00001659 return;
1660
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001661 Out << FS << Name << ": ";
1662 writeMetadataAsOperand(Out, MD, TypePrinter, Machine, Context);
1663}
1664
1665template <class IntTy>
1666void MDFieldPrinter::printInt(StringRef Name, IntTy Int, bool ShouldSkipZero) {
1667 if (ShouldSkipZero && !Int)
1668 return;
1669
1670 Out << FS << Name << ": " << Int;
1671}
1672
David Blaikiea01f2952016-08-24 18:29:49 +00001673void MDFieldPrinter::printBool(StringRef Name, bool Value,
1674 Optional<bool> Default) {
1675 if (Default && Value == *Default)
1676 return;
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001677 Out << FS << Name << ": " << (Value ? "true" : "false");
1678}
1679
Leny Kholodov5fcc4182016-09-06 10:46:28 +00001680void MDFieldPrinter::printDIFlags(StringRef Name, DINode::DIFlags Flags) {
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001681 if (!Flags)
1682 return;
1683
1684 Out << FS << Name << ": ";
1685
Leny Kholodov5fcc4182016-09-06 10:46:28 +00001686 SmallVector<DINode::DIFlags, 8> SplitFlags;
1687 auto Extra = DINode::splitFlags(Flags, SplitFlags);
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001688
1689 FieldSeparator FlagsFS(" | ");
Leny Kholodov5fcc4182016-09-06 10:46:28 +00001690 for (auto F : SplitFlags) {
Mehdi Aminif42ec792016-10-01 05:57:50 +00001691 auto StringF = DINode::getFlagString(F);
1692 assert(!StringF.empty() && "Expected valid flag");
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001693 Out << FlagsFS << StringF;
1694 }
1695 if (Extra || SplitFlags.empty())
1696 Out << FlagsFS << Extra;
1697}
1698
Adrian Prantlb939a252016-03-31 23:56:58 +00001699void MDFieldPrinter::printEmissionKind(StringRef Name,
1700 DICompileUnit::DebugEmissionKind EK) {
Fangrui Song3c1b5db2018-07-06 19:26:00 +00001701 Out << FS << Name << ": " << DICompileUnit::emissionKindString(EK);
Adrian Prantlb939a252016-03-31 23:56:58 +00001702}
1703
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001704template <class IntTy, class Stringifier>
1705void MDFieldPrinter::printDwarfEnum(StringRef Name, IntTy Value,
1706 Stringifier toString, bool ShouldSkipZero) {
1707 if (!Value)
1708 return;
1709
1710 Out << FS << Name << ": ";
Mehdi Amini149f6ea2016-10-05 05:59:29 +00001711 auto S = toString(Value);
1712 if (!S.empty())
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001713 Out << S;
1714 else
1715 Out << Value;
Duncan P. N. Exon Smith79cf9702015-02-28 22:16:56 +00001716}
1717
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001718static void writeGenericDINode(raw_ostream &Out, const GenericDINode *N,
1719 TypePrinting *TypePrinter, SlotTracker *Machine,
1720 const Module *Context) {
1721 Out << "!GenericDINode(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001722 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1723 Printer.printTag(N);
1724 Printer.printString("header", N->getHeader());
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00001725 if (N->getNumDwarfOperands()) {
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001726 Out << Printer.FS << "operands: {";
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00001727 FieldSeparator IFS;
1728 for (auto &I : N->dwarf_operands()) {
1729 Out << IFS;
Duncan P. N. Exon Smith61a09332015-02-06 22:27:22 +00001730 writeMetadataAsOperand(Out, I, TypePrinter, Machine, Context);
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00001731 }
1732 Out << "}";
1733 }
1734 Out << ")";
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +00001735}
1736
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001737static void writeDILocation(raw_ostream &Out, const DILocation *DL,
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +00001738 TypePrinting *TypePrinter, SlotTracker *Machine,
1739 const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001740 Out << "!DILocation(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001741 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
Duncan P. N. Exon Smith503cf3b2015-01-14 22:14:26 +00001742 // Always output the line, since 0 is a relevant and important value for it.
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001743 Printer.printInt("line", DL->getLine(), /* ShouldSkipZero */ false);
1744 Printer.printInt("column", DL->getColumn());
1745 Printer.printMetadata("scope", DL->getRawScope(), /* ShouldSkipNull */ false);
1746 Printer.printMetadata("inlinedAt", DL->getRawInlinedAt());
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +00001747 Out << ")";
1748}
1749
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001750static void writeDISubrange(raw_ostream &Out, const DISubrange *N,
Sander de Smalenfdf40912018-01-24 09:56:07 +00001751 TypePrinting *TypePrinter, SlotTracker *Machine,
1752 const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001753 Out << "!DISubrange(";
Sander de Smalenfdf40912018-01-24 09:56:07 +00001754 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1755 if (auto *CE = N->getCount().dyn_cast<ConstantInt*>())
1756 Printer.printInt("count", CE->getSExtValue(), /* ShouldSkipZero */ false);
1757 else
1758 Printer.printMetadata("count", N->getCount().dyn_cast<DIVariable*>(),
1759 /*ShouldSkipNull */ false);
Duncan P. N. Exon Smith5dcf6212015-04-07 00:39:59 +00001760 Printer.printInt("lowerBound", N->getLowerBound());
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00001761 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001762}
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00001763
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001764static void writeDIEnumerator(raw_ostream &Out, const DIEnumerator *N,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001765 TypePrinting *, SlotTracker *, const Module *) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001766 Out << "!DIEnumerator(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001767 MDFieldPrinter Printer(Out);
1768 Printer.printString("name", N->getName(), /* ShouldSkipEmpty */ false);
Momchil Velikov08dc66e2018-02-12 16:10:09 +00001769 if (N->isUnsigned()) {
1770 auto Value = static_cast<uint64_t>(N->getValue());
1771 Printer.printInt("value", Value, /* ShouldSkipZero */ false);
1772 Printer.printBool("isUnsigned", true);
1773 } else {
1774 Printer.printInt("value", N->getValue(), /* ShouldSkipZero */ false);
1775 }
Duncan P. N. Exon Smith87754762015-02-13 01:14:11 +00001776 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001777}
Duncan P. N. Exon Smith87754762015-02-13 01:14:11 +00001778
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001779static void writeDIBasicType(raw_ostream &Out, const DIBasicType *N,
Duncan P. N. Exon Smith09e03f32015-02-13 01:14:58 +00001780 TypePrinting *, SlotTracker *, const Module *) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001781 Out << "!DIBasicType(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001782 MDFieldPrinter Printer(Out);
Duncan P. N. Exon Smith16d182a2015-02-28 23:21:38 +00001783 if (N->getTag() != dwarf::DW_TAG_base_type)
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001784 Printer.printTag(N);
1785 Printer.printString("name", N->getName());
1786 Printer.printInt("size", N->getSizeInBits());
1787 Printer.printInt("align", N->getAlignInBits());
1788 Printer.printDwarfEnum("encoding", N->getEncoding(),
1789 dwarf::AttributeEncodingString);
Duncan P. N. Exon Smith09e03f32015-02-13 01:14:58 +00001790 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001791}
Duncan P. N. Exon Smith09e03f32015-02-13 01:14:58 +00001792
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001793static void writeDIDerivedType(raw_ostream &Out, const DIDerivedType *N,
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001794 TypePrinting *TypePrinter, SlotTracker *Machine,
1795 const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001796 Out << "!DIDerivedType(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001797 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1798 Printer.printTag(N);
1799 Printer.printString("name", N->getName());
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001800 Printer.printMetadata("scope", N->getRawScope());
1801 Printer.printMetadata("file", N->getRawFile());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001802 Printer.printInt("line", N->getLine());
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001803 Printer.printMetadata("baseType", N->getRawBaseType(),
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001804 /* ShouldSkipNull */ false);
1805 Printer.printInt("size", N->getSizeInBits());
1806 Printer.printInt("align", N->getAlignInBits());
1807 Printer.printInt("offset", N->getOffsetInBits());
1808 Printer.printDIFlags("flags", N->getFlags());
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001809 Printer.printMetadata("extraData", N->getRawExtraData());
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +00001810 if (const auto &DWARFAddressSpace = N->getDWARFAddressSpace())
1811 Printer.printInt("dwarfAddressSpace", *DWARFAddressSpace,
1812 /* ShouldSkipZero */ false);
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001813 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001814}
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001815
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001816static void writeDICompositeType(raw_ostream &Out, const DICompositeType *N,
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001817 TypePrinting *TypePrinter,
1818 SlotTracker *Machine, const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001819 Out << "!DICompositeType(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001820 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1821 Printer.printTag(N);
1822 Printer.printString("name", N->getName());
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001823 Printer.printMetadata("scope", N->getRawScope());
1824 Printer.printMetadata("file", N->getRawFile());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001825 Printer.printInt("line", N->getLine());
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001826 Printer.printMetadata("baseType", N->getRawBaseType());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001827 Printer.printInt("size", N->getSizeInBits());
1828 Printer.printInt("align", N->getAlignInBits());
1829 Printer.printInt("offset", N->getOffsetInBits());
1830 Printer.printDIFlags("flags", N->getFlags());
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001831 Printer.printMetadata("elements", N->getRawElements());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001832 Printer.printDwarfEnum("runtimeLang", N->getRuntimeLang(),
1833 dwarf::LanguageString);
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001834 Printer.printMetadata("vtableHolder", N->getRawVTableHolder());
1835 Printer.printMetadata("templateParams", N->getRawTemplateParams());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001836 Printer.printString("identifier", N->getIdentifier());
Adrian Prantl8c599212018-02-06 23:45:59 +00001837 Printer.printMetadata("discriminator", N->getRawDiscriminator());
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001838 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001839}
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001840
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001841static void writeDISubroutineType(raw_ostream &Out, const DISubroutineType *N,
Duncan P. N. Exon Smith54e2bc62015-02-13 01:22:59 +00001842 TypePrinting *TypePrinter,
1843 SlotTracker *Machine, const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001844 Out << "!DISubroutineType(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001845 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1846 Printer.printDIFlags("flags", N->getFlags());
Reid Klecknerde3d8b52016-06-08 20:34:29 +00001847 Printer.printDwarfEnum("cc", N->getCC(), dwarf::ConventionString);
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001848 Printer.printMetadata("types", N->getRawTypeArray(),
1849 /* ShouldSkipNull */ false);
Duncan P. N. Exon Smith54e2bc62015-02-13 01:22:59 +00001850 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001851}
Duncan P. N. Exon Smithf14b9c72015-02-13 01:19:14 +00001852
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001853static void writeDIFile(raw_ostream &Out, const DIFile *N, TypePrinting *,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001854 SlotTracker *, const Module *) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001855 Out << "!DIFile(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001856 MDFieldPrinter Printer(Out);
1857 Printer.printString("filename", N->getFilename(),
1858 /* ShouldSkipEmpty */ false);
1859 Printer.printString("directory", N->getDirectory(),
1860 /* ShouldSkipEmpty */ false);
Scott Linder71603842018-02-12 19:45:54 +00001861 // Print all values for checksum together, or not at all.
1862 if (N->getChecksum())
1863 Printer.printChecksum(*N->getChecksum());
Scott Linder16c7bda2018-02-23 23:01:06 +00001864 Printer.printString("source", N->getSource().getValueOr(StringRef()),
1865 /* ShouldSkipEmpty */ true);
Duncan P. N. Exon Smithf14b9c72015-02-13 01:19:14 +00001866 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001867}
Duncan P. N. Exon Smithf14b9c72015-02-13 01:19:14 +00001868
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001869static void writeDICompileUnit(raw_ostream &Out, const DICompileUnit *N,
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00001870 TypePrinting *TypePrinter, SlotTracker *Machine,
1871 const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001872 Out << "!DICompileUnit(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001873 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1874 Printer.printDwarfEnum("language", N->getSourceLanguage(),
1875 dwarf::LanguageString, /* ShouldSkipZero */ false);
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001876 Printer.printMetadata("file", N->getRawFile(), /* ShouldSkipNull */ false);
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001877 Printer.printString("producer", N->getProducer());
1878 Printer.printBool("isOptimized", N->isOptimized());
1879 Printer.printString("flags", N->getFlags());
1880 Printer.printInt("runtimeVersion", N->getRuntimeVersion(),
1881 /* ShouldSkipZero */ false);
1882 Printer.printString("splitDebugFilename", N->getSplitDebugFilename());
Adrian Prantlb939a252016-03-31 23:56:58 +00001883 Printer.printEmissionKind("emissionKind", N->getEmissionKind());
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001884 Printer.printMetadata("enums", N->getRawEnumTypes());
1885 Printer.printMetadata("retainedTypes", N->getRawRetainedTypes());
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001886 Printer.printMetadata("globals", N->getRawGlobalVariables());
1887 Printer.printMetadata("imports", N->getRawImportedEntities());
Amjad Abouda9bcf162015-12-10 12:56:35 +00001888 Printer.printMetadata("macros", N->getRawMacros());
Adrian Prantl1f599f92015-05-21 20:37:30 +00001889 Printer.printInt("dwoId", N->getDWOId());
David Blaikiea01f2952016-08-24 18:29:49 +00001890 Printer.printBool("splitDebugInlining", N->getSplitDebugInlining(), true);
Dehao Chen0944a8c2017-02-01 22:45:09 +00001891 Printer.printBool("debugInfoForProfiling", N->getDebugInfoForProfiling(),
1892 false);
Peter Collingbourneb52e2362017-09-12 21:50:41 +00001893 Printer.printBool("gnuPubnames", N->getGnuPubnames(), false);
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00001894 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001895}
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00001896
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001897static void writeDISubprogram(raw_ostream &Out, const DISubprogram *N,
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00001898 TypePrinting *TypePrinter, SlotTracker *Machine,
1899 const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001900 Out << "!DISubprogram(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001901 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1902 Printer.printString("name", N->getName());
1903 Printer.printString("linkageName", N->getLinkageName());
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001904 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
1905 Printer.printMetadata("file", N->getRawFile());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001906 Printer.printInt("line", N->getLine());
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001907 Printer.printMetadata("type", N->getRawType());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001908 Printer.printBool("isLocal", N->isLocalToUnit());
1909 Printer.printBool("isDefinition", N->isDefinition());
1910 Printer.printInt("scopeLine", N->getScopeLine());
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001911 Printer.printMetadata("containingType", N->getRawContainingType());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001912 Printer.printDwarfEnum("virtuality", N->getVirtuality(),
1913 dwarf::VirtualityString);
Peter Collingbournea1f86252016-03-17 23:58:03 +00001914 if (N->getVirtuality() != dwarf::DW_VIRTUALITY_none ||
1915 N->getVirtualIndex() != 0)
1916 Printer.printInt("virtualIndex", N->getVirtualIndex(), false);
Reid Klecknerb5af11d2016-07-01 02:41:21 +00001917 Printer.printInt("thisAdjustment", N->getThisAdjustment());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001918 Printer.printDIFlags("flags", N->getFlags());
1919 Printer.printBool("isOptimized", N->isOptimized());
Adrian Prantl75819ae2016-04-15 15:57:41 +00001920 Printer.printMetadata("unit", N->getRawUnit());
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001921 Printer.printMetadata("templateParams", N->getRawTemplateParams());
1922 Printer.printMetadata("declaration", N->getRawDeclaration());
Shiva Chen2c864552018-05-09 02:40:45 +00001923 Printer.printMetadata("retainedNodes", N->getRawRetainedNodes());
Adrian Prantl1d12b882017-04-26 22:56:44 +00001924 Printer.printMetadata("thrownTypes", N->getRawThrownTypes());
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00001925 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001926}
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00001927
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001928static void writeDILexicalBlock(raw_ostream &Out, const DILexicalBlock *N,
1929 TypePrinting *TypePrinter, SlotTracker *Machine,
1930 const Module *Context) {
1931 Out << "!DILexicalBlock(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001932 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
Duncan P. N. Exon Smith0e202b92015-03-30 16:37:48 +00001933 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
1934 Printer.printMetadata("file", N->getRawFile());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001935 Printer.printInt("line", N->getLine());
1936 Printer.printInt("column", N->getColumn());
Duncan P. N. Exon Smitha96d4092015-02-13 01:29:28 +00001937 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001938}
Duncan P. N. Exon Smitha96d4092015-02-13 01:29:28 +00001939
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001940static void writeDILexicalBlockFile(raw_ostream &Out,
1941 const DILexicalBlockFile *N,
Duncan P. N. Exon Smith06a07022015-02-13 01:30:42 +00001942 TypePrinting *TypePrinter,
1943 SlotTracker *Machine,
1944 const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001945 Out << "!DILexicalBlockFile(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001946 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
Duncan P. N. Exon Smith0e202b92015-03-30 16:37:48 +00001947 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
1948 Printer.printMetadata("file", N->getRawFile());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001949 Printer.printInt("discriminator", N->getDiscriminator(),
1950 /* ShouldSkipZero */ false);
Duncan P. N. Exon Smith06a07022015-02-13 01:30:42 +00001951 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001952}
Duncan P. N. Exon Smith06a07022015-02-13 01:30:42 +00001953
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001954static void writeDINamespace(raw_ostream &Out, const DINamespace *N,
Duncan P. N. Exon Smithe1460002015-02-13 01:32:09 +00001955 TypePrinting *TypePrinter, SlotTracker *Machine,
1956 const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001957 Out << "!DINamespace(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001958 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1959 Printer.printString("name", N->getName());
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00001960 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
Adrian Prantldbfda632016-11-03 19:42:02 +00001961 Printer.printBool("exportSymbols", N->getExportSymbols(), false);
Duncan P. N. Exon Smithe1460002015-02-13 01:32:09 +00001962 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001963}
Duncan P. N. Exon Smithe1460002015-02-13 01:32:09 +00001964
Amjad Abouda9bcf162015-12-10 12:56:35 +00001965static void writeDIMacro(raw_ostream &Out, const DIMacro *N,
1966 TypePrinting *TypePrinter, SlotTracker *Machine,
1967 const Module *Context) {
1968 Out << "!DIMacro(";
1969 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1970 Printer.printMacinfoType(N);
1971 Printer.printInt("line", N->getLine());
1972 Printer.printString("name", N->getName());
1973 Printer.printString("value", N->getValue());
1974 Out << ")";
1975}
1976
1977static void writeDIMacroFile(raw_ostream &Out, const DIMacroFile *N,
1978 TypePrinting *TypePrinter, SlotTracker *Machine,
1979 const Module *Context) {
1980 Out << "!DIMacroFile(";
1981 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1982 Printer.printInt("line", N->getLine());
1983 Printer.printMetadata("file", N->getRawFile(), /* ShouldSkipNull */ false);
1984 Printer.printMetadata("nodes", N->getRawElements());
1985 Out << ")";
1986}
1987
Adrian Prantlab1243f2015-06-29 23:03:47 +00001988static void writeDIModule(raw_ostream &Out, const DIModule *N,
1989 TypePrinting *TypePrinter, SlotTracker *Machine,
1990 const Module *Context) {
1991 Out << "!DIModule(";
1992 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1993 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
1994 Printer.printString("name", N->getName());
1995 Printer.printString("configMacros", N->getConfigurationMacros());
1996 Printer.printString("includePath", N->getIncludePath());
1997 Printer.printString("isysroot", N->getISysRoot());
1998 Out << ")";
1999}
2000
2001
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002002static void writeDITemplateTypeParameter(raw_ostream &Out,
2003 const DITemplateTypeParameter *N,
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00002004 TypePrinting *TypePrinter,
2005 SlotTracker *Machine,
2006 const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002007 Out << "!DITemplateTypeParameter(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00002008 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
2009 Printer.printString("name", N->getName());
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00002010 Printer.printMetadata("type", N->getRawType(), /* ShouldSkipNull */ false);
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00002011 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002012}
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00002013
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002014static void writeDITemplateValueParameter(raw_ostream &Out,
2015 const DITemplateValueParameter *N,
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00002016 TypePrinting *TypePrinter,
2017 SlotTracker *Machine,
2018 const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002019 Out << "!DITemplateValueParameter(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00002020 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
Duncan P. N. Exon Smith16d182a2015-02-28 23:21:38 +00002021 if (N->getTag() != dwarf::DW_TAG_template_value_parameter)
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00002022 Printer.printTag(N);
2023 Printer.printString("name", N->getName());
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00002024 Printer.printMetadata("type", N->getRawType());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00002025 Printer.printMetadata("value", N->getValue(), /* ShouldSkipNull */ false);
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00002026 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002027}
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00002028
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002029static void writeDIGlobalVariable(raw_ostream &Out, const DIGlobalVariable *N,
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00002030 TypePrinting *TypePrinter,
2031 SlotTracker *Machine, const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002032 Out << "!DIGlobalVariable(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00002033 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
2034 Printer.printString("name", N->getName());
2035 Printer.printString("linkageName", N->getLinkageName());
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00002036 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
2037 Printer.printMetadata("file", N->getRawFile());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00002038 Printer.printInt("line", N->getLine());
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00002039 Printer.printMetadata("type", N->getRawType());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00002040 Printer.printBool("isLocal", N->isLocalToUnit());
2041 Printer.printBool("isDefinition", N->isDefinition());
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00002042 Printer.printMetadata("declaration", N->getRawStaticDataMemberDeclaration());
Victor Leschuk2ede1262016-10-20 00:13:12 +00002043 Printer.printInt("align", N->getAlignInBits());
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00002044 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002045}
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00002046
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002047static void writeDILocalVariable(raw_ostream &Out, const DILocalVariable *N,
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00002048 TypePrinting *TypePrinter,
2049 SlotTracker *Machine, const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002050 Out << "!DILocalVariable(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00002051 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00002052 Printer.printString("name", N->getName());
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00002053 Printer.printInt("arg", N->getArg());
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00002054 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
2055 Printer.printMetadata("file", N->getRawFile());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00002056 Printer.printInt("line", N->getLine());
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00002057 Printer.printMetadata("type", N->getRawType());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00002058 Printer.printDIFlags("flags", N->getFlags());
Victor Leschuk2ede1262016-10-20 00:13:12 +00002059 Printer.printInt("align", N->getAlignInBits());
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00002060 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002061}
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00002062
Shiva Chen2c864552018-05-09 02:40:45 +00002063static void writeDILabel(raw_ostream &Out, const DILabel *N,
2064 TypePrinting *TypePrinter,
2065 SlotTracker *Machine, const Module *Context) {
2066 Out << "!DILabel(";
2067 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
2068 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
2069 Printer.printString("name", N->getName());
2070 Printer.printMetadata("file", N->getRawFile());
2071 Printer.printInt("line", N->getLine());
2072 Out << ")";
2073}
2074
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002075static void writeDIExpression(raw_ostream &Out, const DIExpression *N,
Duncan P. N. Exon Smith0c5c0122015-02-13 01:42:09 +00002076 TypePrinting *TypePrinter, SlotTracker *Machine,
2077 const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002078 Out << "!DIExpression(";
Duncan P. N. Exon Smith0c5c0122015-02-13 01:42:09 +00002079 FieldSeparator FS;
2080 if (N->isValid()) {
2081 for (auto I = N->expr_op_begin(), E = N->expr_op_end(); I != E; ++I) {
Mehdi Amini149f6ea2016-10-05 05:59:29 +00002082 auto OpStr = dwarf::OperationEncodingString(I->getOp());
2083 assert(!OpStr.empty() && "Expected valid opcode");
Duncan P. N. Exon Smith0c5c0122015-02-13 01:42:09 +00002084
2085 Out << FS << OpStr;
2086 for (unsigned A = 0, AE = I->getNumArgs(); A != AE; ++A)
2087 Out << FS << I->getArg(A);
2088 }
2089 } else {
2090 for (const auto &I : N->getElements())
2091 Out << FS << I;
2092 }
2093 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002094}
Duncan P. N. Exon Smith0c5c0122015-02-13 01:42:09 +00002095
Adrian Prantlbceaaa92016-12-20 02:09:43 +00002096static void writeDIGlobalVariableExpression(raw_ostream &Out,
2097 const DIGlobalVariableExpression *N,
2098 TypePrinting *TypePrinter,
2099 SlotTracker *Machine,
2100 const Module *Context) {
2101 Out << "!DIGlobalVariableExpression(";
2102 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
2103 Printer.printMetadata("var", N->getVariable());
2104 Printer.printMetadata("expr", N->getExpression());
2105 Out << ")";
2106}
2107
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002108static void writeDIObjCProperty(raw_ostream &Out, const DIObjCProperty *N,
Duncan P. N. Exon Smithd45ce962015-02-13 01:43:22 +00002109 TypePrinting *TypePrinter, SlotTracker *Machine,
2110 const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002111 Out << "!DIObjCProperty(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00002112 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
2113 Printer.printString("name", N->getName());
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00002114 Printer.printMetadata("file", N->getRawFile());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00002115 Printer.printInt("line", N->getLine());
2116 Printer.printString("setter", N->getSetterName());
2117 Printer.printString("getter", N->getGetterName());
2118 Printer.printInt("attributes", N->getAttributes());
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00002119 Printer.printMetadata("type", N->getRawType());
Duncan P. N. Exon Smithd45ce962015-02-13 01:43:22 +00002120 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002121}
Duncan P. N. Exon Smithd45ce962015-02-13 01:43:22 +00002122
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002123static void writeDIImportedEntity(raw_ostream &Out, const DIImportedEntity *N,
Duncan P. N. Exon Smith1c931162015-02-13 01:46:02 +00002124 TypePrinting *TypePrinter,
2125 SlotTracker *Machine, const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002126 Out << "!DIImportedEntity(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00002127 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
2128 Printer.printTag(N);
2129 Printer.printString("name", N->getName());
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00002130 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
2131 Printer.printMetadata("entity", N->getRawEntity());
Adrian Prantld63bfd22017-07-19 00:09:54 +00002132 Printer.printMetadata("file", N->getRawFile());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00002133 Printer.printInt("line", N->getLine());
Duncan P. N. Exon Smith1c931162015-02-13 01:46:02 +00002134 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002135}
2136
Duncan P. N. Exon Smitha6de6a42015-01-12 23:45:31 +00002137static void WriteMDNodeBodyInternal(raw_ostream &Out, const MDNode *Node,
2138 TypePrinting *TypePrinter,
2139 SlotTracker *Machine,
2140 const Module *Context) {
Duncan P. N. Exon Smith2bc00f42015-01-19 23:13:14 +00002141 if (Node->isDistinct())
Duncan P. N. Exon Smitha6de6a42015-01-12 23:45:31 +00002142 Out << "distinct ";
Duncan P. N. Exon Smith3d510662015-03-16 21:21:10 +00002143 else if (Node->isTemporary())
2144 Out << "<temporary!> "; // Handle broken code.
Duncan P. N. Exon Smitha6de6a42015-01-12 23:45:31 +00002145
Duncan P. N. Exon Smith2bc00f42015-01-19 23:13:14 +00002146 switch (Node->getMetadataID()) {
Duncan P. N. Exon Smitha6de6a42015-01-12 23:45:31 +00002147 default:
2148 llvm_unreachable("Expected uniquable MDNode");
Duncan P. N. Exon Smith2bc00f42015-01-19 23:13:14 +00002149#define HANDLE_MDNODE_LEAF(CLASS) \
Duncan P. N. Exon Smitha6de6a42015-01-12 23:45:31 +00002150 case Metadata::CLASS##Kind: \
Duncan P. N. Exon Smith2bc00f42015-01-19 23:13:14 +00002151 write##CLASS(Out, cast<CLASS>(Node), TypePrinter, Machine, Context); \
Duncan P. N. Exon Smitha6de6a42015-01-12 23:45:31 +00002152 break;
2153#include "llvm/IR/Metadata.def"
2154 }
2155}
2156
Chandler Carruthd48cdbf2014-01-09 02:29:41 +00002157// Full implementation of printing a Value as an operand with support for
2158// TypePrinting, etc.
Dan Gohman12dad632009-08-12 20:56:03 +00002159static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
Dan Gohman8061d9e2009-08-13 15:27:57 +00002160 TypePrinting *TypePrinter,
Dan Gohmana2489d12010-07-20 23:55:01 +00002161 SlotTracker *Machine,
2162 const Module *Context) {
Chris Lattner033935d2008-08-17 04:40:13 +00002163 if (V->hasName()) {
2164 PrintLLVMName(Out, V);
2165 return;
2166 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002167
Chris Lattner033935d2008-08-17 04:40:13 +00002168 const Constant *CV = dyn_cast<Constant>(V);
2169 if (CV && !isa<GlobalValue>(CV)) {
Dan Gohman8061d9e2009-08-13 15:27:57 +00002170 assert(TypePrinter && "Constants require TypePrinting!");
Dan Gohmana2489d12010-07-20 23:55:01 +00002171 WriteConstantInternal(Out, CV, *TypePrinter, Machine, Context);
Chris Lattner3a36d2c2008-08-19 05:06:27 +00002172 return;
2173 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002174
Chris Lattner3a36d2c2008-08-19 05:06:27 +00002175 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
Chris Lattner033935d2008-08-17 04:40:13 +00002176 Out << "asm ";
2177 if (IA->hasSideEffects())
2178 Out << "sideeffect ";
Dale Johannesen1cfb9582009-10-21 23:28:00 +00002179 if (IA->isAlignStack())
2180 Out << "alignstack ";
Chad Rosierd8c76102012-09-05 19:00:49 +00002181 // We don't emit the AD_ATT dialect as it's the assumed default.
2182 if (IA->getDialect() == InlineAsm::AD_Intel)
2183 Out << "inteldialect ";
Chris Lattner033935d2008-08-17 04:40:13 +00002184 Out << '"';
Jonas Devlieghere745918ff2018-05-31 17:01:42 +00002185 printEscapedString(IA->getAsmString(), Out);
Chris Lattner033935d2008-08-17 04:40:13 +00002186 Out << "\", \"";
Jonas Devlieghere745918ff2018-05-31 17:01:42 +00002187 printEscapedString(IA->getConstraintString(), Out);
Chris Lattner033935d2008-08-17 04:40:13 +00002188 Out << '"';
Chris Lattner3a36d2c2008-08-19 05:06:27 +00002189 return;
2190 }
Devang Patel7428d8a2009-07-22 17:43:22 +00002191
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002192 if (auto *MD = dyn_cast<MetadataAsValue>(V)) {
2193 WriteAsOperandInternal(Out, MD->getMetadata(), TypePrinter, Machine,
2194 Context, /* FromValue */ true);
Devang Patel7428d8a2009-07-22 17:43:22 +00002195 return;
2196 }
2197
Chris Lattner3a36d2c2008-08-19 05:06:27 +00002198 char Prefix = '%';
2199 int Slot;
Chris Lattner5b82a0a2011-08-03 06:15:41 +00002200 // If we have a SlotTracker, use it.
Chris Lattner3a36d2c2008-08-19 05:06:27 +00002201 if (Machine) {
2202 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
2203 Slot = Machine->getGlobalSlot(GV);
2204 Prefix = '@';
2205 } else {
2206 Slot = Machine->getLocalSlot(V);
Andrew Trickec4b6e72011-09-30 19:48:58 +00002207
Chris Lattner5b82a0a2011-08-03 06:15:41 +00002208 // If the local value didn't succeed, then we may be referring to a value
2209 // from a different function. Translate it, as this can happen when using
2210 // address of blocks.
2211 if (Slot == -1)
2212 if ((Machine = createSlotTracker(V))) {
2213 Slot = Machine->getLocalSlot(V);
2214 delete Machine;
2215 }
Chris Lattner3a36d2c2008-08-19 05:06:27 +00002216 }
Chris Lattner5b82a0a2011-08-03 06:15:41 +00002217 } else if ((Machine = createSlotTracker(V))) {
2218 // Otherwise, create one to get the # and then destroy it.
2219 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
2220 Slot = Machine->getGlobalSlot(GV);
2221 Prefix = '@';
Chris Lattnera2d810d2006-01-25 22:26:05 +00002222 } else {
Chris Lattner5b82a0a2011-08-03 06:15:41 +00002223 Slot = Machine->getLocalSlot(V);
Chris Lattnerd84bb632002-04-16 21:36:08 +00002224 }
Chris Lattner5b82a0a2011-08-03 06:15:41 +00002225 delete Machine;
Craig Topperc6207612014-04-09 06:08:46 +00002226 Machine = nullptr;
Chris Lattner5b82a0a2011-08-03 06:15:41 +00002227 } else {
2228 Slot = -1;
Chris Lattnerd84bb632002-04-16 21:36:08 +00002229 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002230
Chris Lattner3a36d2c2008-08-19 05:06:27 +00002231 if (Slot != -1)
2232 Out << Prefix << Slot;
2233 else
2234 Out << "<badref>";
Chris Lattnerd84bb632002-04-16 21:36:08 +00002235}
2236
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002237static void WriteAsOperandInternal(raw_ostream &Out, const Metadata *MD,
2238 TypePrinting *TypePrinter,
2239 SlotTracker *Machine, const Module *Context,
2240 bool FromValue) {
Reid Kleckner6d353342017-08-23 20:31:27 +00002241 // Write DIExpressions inline when used as a value. Improves readability of
2242 // debug info intrinsics.
2243 if (const DIExpression *Expr = dyn_cast<DIExpression>(MD)) {
2244 writeDIExpression(Out, Expr, TypePrinter, Machine, Context);
2245 return;
2246 }
2247
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002248 if (const MDNode *N = dyn_cast<MDNode>(MD)) {
Duncan P. N. Exon Smithf8b3ad62015-06-27 00:15:32 +00002249 std::unique_ptr<SlotTracker> MachineStorage;
2250 if (!Machine) {
2251 MachineStorage = make_unique<SlotTracker>(Context);
2252 Machine = MachineStorage.get();
2253 }
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002254 int Slot = Machine->getMetadataSlot(N);
2255 if (Slot == -1)
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +00002256 // Give the pointer value instead of "badref", since this comes up all
2257 // the time when debugging.
2258 Out << "<" << N << ">";
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002259 else
2260 Out << '!' << Slot;
2261 return;
2262 }
2263
2264 if (const MDString *MDS = dyn_cast<MDString>(MD)) {
2265 Out << "!\"";
Jonas Devlieghere745918ff2018-05-31 17:01:42 +00002266 printEscapedString(MDS->getString(), Out);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002267 Out << '"';
2268 return;
2269 }
2270
2271 auto *V = cast<ValueAsMetadata>(MD);
2272 assert(TypePrinter && "TypePrinter required for metadata values");
2273 assert((FromValue || !isa<LocalAsMetadata>(V)) &&
2274 "Unexpected function-local metadata outside of value argument");
2275
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002276 TypePrinter->print(V->getValue()->getType(), Out);
2277 Out << ' ';
2278 WriteAsOperandInternal(Out, V->getValue(), TypePrinter, Machine, Context);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002279}
2280
Benjamin Kramerba05a782015-03-17 19:53:41 +00002281namespace {
Eugene Zelenkode6cce22017-06-19 22:05:08 +00002282
Benjamin Kramerba05a782015-03-17 19:53:41 +00002283class AssemblyWriter {
2284 formatted_raw_ostream &Out;
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00002285 const Module *TheModule = nullptr;
2286 const ModuleSummaryIndex *TheIndex = nullptr;
Duncan P. N. Exon Smith326921542015-06-26 22:04:20 +00002287 std::unique_ptr<SlotTracker> SlotTrackerStorage;
Benjamin Kramerba05a782015-03-17 19:53:41 +00002288 SlotTracker &Machine;
2289 TypePrinting TypePrinter;
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00002290 AssemblyAnnotationWriter *AnnotationWriter = nullptr;
Benjamin Kramerba05a782015-03-17 19:53:41 +00002291 SetVector<const Comdat *> Comdats;
Justin Bognerd7d1a722015-09-27 22:38:50 +00002292 bool IsForDebug;
Duncan P. N. Exon Smith89010d82015-04-15 01:36:30 +00002293 bool ShouldPreserveUseListOrder;
Benjamin Kramerba05a782015-03-17 19:53:41 +00002294 UseListOrderStack UseListOrders;
Duncan P. N. Exon Smithe30f10e2015-04-24 21:03:05 +00002295 SmallVector<StringRef, 8> MDNames;
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00002296 /// Synchronization scope names registered with LLVMContext.
2297 SmallVector<StringRef, 8> SSNs;
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00002298 DenseMap<const GlobalValueSummary *, GlobalValue::GUID> SummaryToGUIDMap;
Benjamin Kramerba05a782015-03-17 19:53:41 +00002299
2300public:
2301 /// Construct an AssemblyWriter with an external SlotTracker
Duncan P. N. Exon Smith89010d82015-04-15 01:36:30 +00002302 AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac, const Module *M,
Justin Bognerd7d1a722015-09-27 22:38:50 +00002303 AssemblyAnnotationWriter *AAW, bool IsForDebug,
Duncan P. N. Exon Smith89010d82015-04-15 01:36:30 +00002304 bool ShouldPreserveUseListOrder = false);
Benjamin Kramerba05a782015-03-17 19:53:41 +00002305
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00002306 AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac,
2307 const ModuleSummaryIndex *Index, bool IsForDebug);
2308
Benjamin Kramerba05a782015-03-17 19:53:41 +00002309 void printMDNodeBody(const MDNode *MD);
2310 void printNamedMDNode(const NamedMDNode *NMD);
2311
2312 void printModule(const Module *M);
2313
2314 void writeOperand(const Value *Op, bool PrintType);
Reid Kleckner6652a522017-04-28 18:37:16 +00002315 void writeParamOperand(const Value *Operand, AttributeSet Attrs);
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002316 void writeOperandBundles(ImmutableCallSite CS);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00002317 void writeSyncScope(const LLVMContext &Context,
2318 SyncScope::ID SSID);
2319 void writeAtomic(const LLVMContext &Context,
2320 AtomicOrdering Ordering,
2321 SyncScope::ID SSID);
2322 void writeAtomicCmpXchg(const LLVMContext &Context,
2323 AtomicOrdering SuccessOrdering,
Benjamin Kramerba05a782015-03-17 19:53:41 +00002324 AtomicOrdering FailureOrdering,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00002325 SyncScope::ID SSID);
Benjamin Kramerba05a782015-03-17 19:53:41 +00002326
2327 void writeAllMDNodes();
2328 void writeMDNode(unsigned Slot, const MDNode *Node);
2329 void writeAllAttributeGroups();
2330
2331 void printTypeIdentities();
2332 void printGlobal(const GlobalVariable *GV);
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +00002333 void printIndirectSymbol(const GlobalIndirectSymbol *GIS);
Benjamin Kramerba05a782015-03-17 19:53:41 +00002334 void printComdat(const Comdat *C);
2335 void printFunction(const Function *F);
Reid Kleckner6652a522017-04-28 18:37:16 +00002336 void printArgument(const Argument *FA, AttributeSet Attrs);
Benjamin Kramerba05a782015-03-17 19:53:41 +00002337 void printBasicBlock(const BasicBlock *BB);
2338 void printInstructionLine(const Instruction &I);
2339 void printInstruction(const Instruction &I);
2340
2341 void printUseListOrder(const UseListOrder &Order);
2342 void printUseLists(const Function *F);
2343
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00002344 void printModuleSummaryIndex();
2345 void printSummaryInfo(unsigned Slot, const ValueInfo &VI);
2346 void printSummary(const GlobalValueSummary &Summary);
2347 void printAliasSummary(const AliasSummary *AS);
2348 void printGlobalVarSummary(const GlobalVarSummary *GS);
2349 void printFunctionSummary(const FunctionSummary *FS);
2350 void printTypeIdSummary(const TypeIdSummary &TIS);
2351 void printTypeTestResolution(const TypeTestResolution &TTRes);
2352 void printArgs(const std::vector<uint64_t> &Args);
2353 void printWPDRes(const WholeProgramDevirtResolution &WPDRes);
2354 void printTypeIdInfo(const FunctionSummary::TypeIdInfo &TIDInfo);
2355 void printVFuncId(const FunctionSummary::VFuncId VFId);
2356 void
2357 printNonConstVCalls(const std::vector<FunctionSummary::VFuncId> VCallList,
2358 const char *Tag);
2359 void
2360 printConstVCalls(const std::vector<FunctionSummary::ConstVCall> VCallList,
2361 const char *Tag);
2362
Benjamin Kramerba05a782015-03-17 19:53:41 +00002363private:
Adrian Prantl5f8f34e42018-05-01 15:54:18 +00002364 /// Print out metadata attachments.
Duncan P. N. Exon Smith86979272015-04-24 20:59:52 +00002365 void printMetadataAttachments(
Duncan P. N. Exon Smithd3e4c2a2015-04-24 21:06:21 +00002366 const SmallVectorImpl<std::pair<unsigned, MDNode *>> &MDs,
2367 StringRef Separator);
Duncan P. N. Exon Smith86979272015-04-24 20:59:52 +00002368
Benjamin Kramerba05a782015-03-17 19:53:41 +00002369 // printInfoComment - Print a little comment after the instruction indicating
2370 // which slot it occupies.
2371 void printInfoComment(const Value &V);
Igor Laevsky2aa8caf2015-05-05 13:20:42 +00002372
2373 // printGCRelocateComment - print comment after call to the gc.relocate
2374 // intrinsic indicating base and derived pointer names.
Manuel Jacob83eefa62016-01-05 04:03:00 +00002375 void printGCRelocateComment(const GCRelocateInst &Relocate);
Benjamin Kramerba05a782015-03-17 19:53:41 +00002376};
Eugene Zelenkode6cce22017-06-19 22:05:08 +00002377
2378} // end anonymous namespace
Benjamin Kramerba05a782015-03-17 19:53:41 +00002379
Justin Bogner0ffea9d2015-09-02 22:46:15 +00002380AssemblyWriter::AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac,
2381 const Module *M, AssemblyAnnotationWriter *AAW,
Justin Bognerd7d1a722015-09-27 22:38:50 +00002382 bool IsForDebug, bool ShouldPreserveUseListOrder)
Roman Tereshind96de6f2018-03-22 21:29:07 +00002383 : Out(o), TheModule(M), Machine(Mac), TypePrinter(M), AnnotationWriter(AAW),
Justin Bognerd7d1a722015-09-27 22:38:50 +00002384 IsForDebug(IsForDebug),
Justin Bogner0ffea9d2015-09-02 22:46:15 +00002385 ShouldPreserveUseListOrder(ShouldPreserveUseListOrder) {
David Majnemerdad0a642014-06-27 18:19:56 +00002386 if (!TheModule)
2387 return;
Peter Collingbourne6d88fde2016-06-22 20:29:42 +00002388 for (const GlobalObject &GO : TheModule->global_objects())
2389 if (const Comdat *C = GO.getComdat())
David Majnemerdad0a642014-06-27 18:19:56 +00002390 Comdats.insert(C);
Daniel Maleaded9f932013-05-08 20:38:31 +00002391}
Chris Lattner2e9fee42001-07-12 23:35:26 +00002392
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00002393AssemblyWriter::AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac,
2394 const ModuleSummaryIndex *Index, bool IsForDebug)
2395 : Out(o), TheIndex(Index), Machine(Mac), TypePrinter(/*Module=*/nullptr),
2396 IsForDebug(IsForDebug), ShouldPreserveUseListOrder(false) {}
2397
Chris Lattner78e2e8b2006-12-06 06:24:27 +00002398void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType) {
Craig Topperc6207612014-04-09 06:08:46 +00002399 if (!Operand) {
Chris Lattner08f7d0c2005-02-24 16:58:29 +00002400 Out << "<null operand!>";
Chris Lattnerb419a1e2009-12-31 02:33:14 +00002401 return;
Chris Lattner08f7d0c2005-02-24 16:58:29 +00002402 }
Chris Lattnerb419a1e2009-12-31 02:33:14 +00002403 if (PrintType) {
2404 TypePrinter.print(Operand->getType(), Out);
2405 Out << ' ';
2406 }
Dan Gohmana2489d12010-07-20 23:55:01 +00002407 WriteAsOperandInternal(Out, Operand, &TypePrinter, &Machine, TheModule);
Chris Lattner2f7c9632001-06-06 20:29:01 +00002408}
2409
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00002410void AssemblyWriter::writeSyncScope(const LLVMContext &Context,
2411 SyncScope::ID SSID) {
2412 switch (SSID) {
2413 case SyncScope::System: {
2414 break;
2415 }
2416 default: {
2417 if (SSNs.empty())
2418 Context.getSyncScopeNames(SSNs);
2419
2420 Out << " syncscope(\"";
Jonas Devlieghere745918ff2018-05-31 17:01:42 +00002421 printEscapedString(SSNs[SSID], Out);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00002422 Out << "\")";
2423 break;
2424 }
2425 }
2426}
2427
2428void AssemblyWriter::writeAtomic(const LLVMContext &Context,
2429 AtomicOrdering Ordering,
2430 SyncScope::ID SSID) {
JF Bastien800f87a2016-04-06 21:19:33 +00002431 if (Ordering == AtomicOrdering::NotAtomic)
Eli Friedmanfee02c62011-07-25 23:16:38 +00002432 return;
2433
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00002434 writeSyncScope(Context, SSID);
JF Bastien800f87a2016-04-06 21:19:33 +00002435 Out << " " << toIRString(Ordering);
Eli Friedmanfee02c62011-07-25 23:16:38 +00002436}
2437
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00002438void AssemblyWriter::writeAtomicCmpXchg(const LLVMContext &Context,
2439 AtomicOrdering SuccessOrdering,
Tim Northovere94a5182014-03-11 10:48:52 +00002440 AtomicOrdering FailureOrdering,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00002441 SyncScope::ID SSID) {
JF Bastien800f87a2016-04-06 21:19:33 +00002442 assert(SuccessOrdering != AtomicOrdering::NotAtomic &&
2443 FailureOrdering != AtomicOrdering::NotAtomic);
Tim Northovere94a5182014-03-11 10:48:52 +00002444
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00002445 writeSyncScope(Context, SSID);
JF Bastien800f87a2016-04-06 21:19:33 +00002446 Out << " " << toIRString(SuccessOrdering);
2447 Out << " " << toIRString(FailureOrdering);
Tim Northovere94a5182014-03-11 10:48:52 +00002448}
2449
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002450void AssemblyWriter::writeParamOperand(const Value *Operand,
Reid Kleckner6652a522017-04-28 18:37:16 +00002451 AttributeSet Attrs) {
Craig Topperc6207612014-04-09 06:08:46 +00002452 if (!Operand) {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00002453 Out << "<null operand!>";
Chris Lattnerb419a1e2009-12-31 02:33:14 +00002454 return;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00002455 }
Chris Lattnerb419a1e2009-12-31 02:33:14 +00002456
2457 // Print the type
2458 TypePrinter.print(Operand->getType(), Out);
2459 // Print parameter attributes list
Reid Kleckner6652a522017-04-28 18:37:16 +00002460 if (Attrs.hasAttributes())
2461 Out << ' ' << Attrs.getAsString();
Chris Lattnerb419a1e2009-12-31 02:33:14 +00002462 Out << ' ';
2463 // Print the operand
Dan Gohmana2489d12010-07-20 23:55:01 +00002464 WriteAsOperandInternal(Out, Operand, &TypePrinter, &Machine, TheModule);
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00002465}
Chris Lattner2f7c9632001-06-06 20:29:01 +00002466
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002467void AssemblyWriter::writeOperandBundles(ImmutableCallSite CS) {
2468 if (!CS.hasOperandBundles())
2469 return;
2470
2471 Out << " [ ";
2472
2473 bool FirstBundle = true;
2474 for (unsigned i = 0, e = CS.getNumOperandBundles(); i != e; ++i) {
Sanjoy Das54c3ca62015-11-07 01:56:04 +00002475 OperandBundleUse BU = CS.getOperandBundleAt(i);
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002476
2477 if (!FirstBundle)
2478 Out << ", ";
2479 FirstBundle = false;
2480
2481 Out << '"';
Jonas Devlieghere745918ff2018-05-31 17:01:42 +00002482 printEscapedString(BU.getTagName(), Out);
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002483 Out << '"';
2484
2485 Out << '(';
2486
2487 bool FirstInput = true;
2488 for (const auto &Input : BU.Inputs) {
2489 if (!FirstInput)
2490 Out << ", ";
2491 FirstInput = false;
2492
2493 TypePrinter.print(Input->getType(), Out);
2494 Out << " ";
2495 WriteAsOperandInternal(Out, Input, &TypePrinter, &Machine, TheModule);
2496 }
2497
2498 Out << ')';
2499 }
2500
2501 Out << " ]";
2502}
2503
Chris Lattner7bfee412001-10-29 16:05:51 +00002504void AssemblyWriter::printModule(const Module *M) {
Teresa Johnsonfc801f52018-07-03 15:52:57 +00002505 Machine.initializeIfNeeded();
Bill Wendling829b4782013-02-11 08:43:33 +00002506
Duncan P. N. Exon Smith89010d82015-04-15 01:36:30 +00002507 if (ShouldPreserveUseListOrder)
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +00002508 UseListOrders = predictUseListOrder(M);
2509
Chris Lattner4d8689e2005-03-02 23:12:40 +00002510 if (!M->getModuleIdentifier().empty() &&
Misha Brukmanb1c93172005-04-21 23:48:37 +00002511 // Don't print the ID if it will start a new line (which would
Chris Lattner4d8689e2005-03-02 23:12:40 +00002512 // require a comment char before it).
2513 M->getModuleIdentifier().find('\n') == std::string::npos)
2514 Out << "; ModuleID = '" << M->getModuleIdentifier() << "'\n";
2515
Teresa Johnson83c517c2016-03-30 18:15:08 +00002516 if (!M->getSourceFileName().empty()) {
Teresa Johnsond8d94652016-03-30 22:17:28 +00002517 Out << "source_filename = \"";
Jonas Devlieghere745918ff2018-05-31 17:01:42 +00002518 printEscapedString(M->getSourceFileName(), Out);
Teresa Johnsond8d94652016-03-30 22:17:28 +00002519 Out << "\"\n";
Teresa Johnson83c517c2016-03-30 18:15:08 +00002520 }
2521
Rafael Espindolaf863ee22014-02-25 20:01:08 +00002522 const std::string &DL = M->getDataLayoutStr();
2523 if (!DL.empty())
2524 Out << "target datalayout = \"" << DL << "\"\n";
Reid Spencer48f98c82004-07-25 21:44:54 +00002525 if (!M->getTargetTriple().empty())
Reid Spencerffec7df2004-07-25 21:29:43 +00002526 Out << "target triple = \"" << M->getTargetTriple() << "\"\n";
Misha Brukmanb1c93172005-04-21 23:48:37 +00002527
Chris Lattnereef2fe72006-01-24 04:13:11 +00002528 if (!M->getModuleInlineAsm().empty()) {
Dan Gohman69273e62009-08-12 23:54:22 +00002529 Out << '\n';
Benjamin Kramerbbd05a22015-06-07 13:59:33 +00002530
2531 // Split the string into lines, to make it easier to read the .ll file.
Benjamin Kramerf1cfc422015-06-08 18:58:57 +00002532 StringRef Asm = M->getModuleInlineAsm();
Benjamin Kramerbbd05a22015-06-07 13:59:33 +00002533 do {
2534 StringRef Front;
2535 std::tie(Front, Asm) = Asm.split('\n');
2536
Chris Lattnerefaf35d2006-01-24 00:45:30 +00002537 // We found a newline, print the portion of the asm string from the
2538 // last newline up to this newline.
2539 Out << "module asm \"";
Jonas Devlieghere745918ff2018-05-31 17:01:42 +00002540 printEscapedString(Front, Out);
Chris Lattnerefaf35d2006-01-24 00:45:30 +00002541 Out << "\"\n";
Benjamin Kramerbbd05a22015-06-07 13:59:33 +00002542 } while (!Asm.empty());
Chris Lattner6ed87bd2006-01-23 23:03:36 +00002543 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002544
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002545 printTypeIdentities();
Misha Brukmanb1c93172005-04-21 23:48:37 +00002546
David Majnemerdad0a642014-06-27 18:19:56 +00002547 // Output all comdats.
2548 if (!Comdats.empty())
2549 Out << '\n';
2550 for (const Comdat *C : Comdats) {
2551 printComdat(C);
2552 if (C != Comdats.back())
2553 Out << '\n';
2554 }
2555
Dan Gohman69273e62009-08-12 23:54:22 +00002556 // Output all globals.
2557 if (!M->global_empty()) Out << '\n';
Yaron Kerenf3cf9d12015-06-13 17:50:47 +00002558 for (const GlobalVariable &GV : M->globals()) {
2559 printGlobal(&GV); Out << '\n';
Duncan Sands66fc0e62012-09-12 09:55:51 +00002560 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002561
Chris Lattnerd2747052007-04-26 02:24:10 +00002562 // Output all aliases.
2563 if (!M->alias_empty()) Out << "\n";
Yaron Kerenf3cf9d12015-06-13 17:50:47 +00002564 for (const GlobalAlias &GA : M->aliases())
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +00002565 printIndirectSymbol(&GA);
Chris Lattnerfee714f2001-09-07 16:36:04 +00002566
Dmitry Polukhina1feff72016-04-07 12:32:19 +00002567 // Output all ifuncs.
2568 if (!M->ifunc_empty()) Out << "\n";
2569 for (const GlobalIFunc &GI : M->ifuncs())
2570 printIndirectSymbol(&GI);
2571
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +00002572 // Output global use-lists.
2573 printUseLists(nullptr);
2574
Chris Lattner2cdd49d2004-09-14 05:06:58 +00002575 // Output all of the functions.
Yaron Kerenf3cf9d12015-06-13 17:50:47 +00002576 for (const Function &F : *M)
2577 printFunction(&F);
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +00002578 assert(UseListOrders.empty() && "All use-lists should have been consumed");
Devang Patel983c6b12009-07-08 21:44:25 +00002579
Bill Wendling829b4782013-02-11 08:43:33 +00002580 // Output all attribute groups.
Bill Wendling04945972013-04-29 23:48:06 +00002581 if (!Machine.as_empty()) {
Bill Wendling829b4782013-02-11 08:43:33 +00002582 Out << '\n';
2583 writeAllAttributeGroups();
2584 }
2585
Devang Patel23e68302009-07-29 22:04:47 +00002586 // Output named metadata.
Dan Gohman69273e62009-08-12 23:54:22 +00002587 if (!M->named_metadata_empty()) Out << '\n';
Andrew Trickec4b6e72011-09-30 19:48:58 +00002588
Yaron Kerenf3cf9d12015-06-13 17:50:47 +00002589 for (const NamedMDNode &Node : M->named_metadata())
2590 printNamedMDNode(&Node);
Devang Patel23e68302009-07-29 22:04:47 +00002591
2592 // Output metadata.
Chris Lattnercf4a76e2009-12-31 02:20:11 +00002593 if (!Machine.mdn_empty()) {
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002594 Out << '\n';
2595 writeAllMDNodes();
2596 }
Chris Lattnerfee714f2001-09-07 16:36:04 +00002597}
2598
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00002599void AssemblyWriter::printModuleSummaryIndex() {
2600 assert(TheIndex);
Teresa Johnsonfc801f52018-07-03 15:52:57 +00002601 Machine.initializeIndexIfNeeded();
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00002602
2603 Out << "\n";
2604
Teresa Johnson8fc76662018-07-02 22:09:23 +00002605 // Print module path entries. To print in order, add paths to a vector
2606 // indexed by module slot.
Teresa Johnson724e7a12018-05-26 02:53:52 +00002607 std::vector<std::pair<std::string, ModuleHash>> moduleVec;
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00002608 std::string RegularLTOModuleName = "[Regular LTO]";
2609 moduleVec.resize(TheIndex->modulePaths().size());
Teresa Johnson8fc76662018-07-02 22:09:23 +00002610 for (auto &ModPath : TheIndex->modulePaths())
2611 moduleVec[Machine.getModulePathSlot(ModPath.first())] = std::make_pair(
2612 // A module id of -1 is a special entry for a regular LTO module created
2613 // during the thin link.
2614 ModPath.second.first == -1u ? RegularLTOModuleName
2615 : (std::string)ModPath.first(),
2616 ModPath.second.second);
2617
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00002618 unsigned i = 0;
2619 for (auto &ModPair : moduleVec) {
2620 Out << "^" << i++ << " = module: (";
Andrew Ngac305e12018-07-12 14:40:21 +00002621 Out << "path: \"";
2622 printEscapedString(ModPair.first, Out);
2623 Out << "\", hash: (";
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00002624 FieldSeparator FS;
2625 for (auto Hash : ModPair.second)
2626 Out << FS << Hash;
2627 Out << "))\n";
2628 }
2629
2630 // FIXME: Change AliasSummary to hold a ValueInfo instead of summary pointer
2631 // for aliasee (then update BitcodeWriter.cpp and remove get/setAliaseeGUID).
2632 for (auto &GlobalList : *TheIndex) {
2633 auto GUID = GlobalList.first;
2634 for (auto &Summary : GlobalList.second.SummaryList)
2635 SummaryToGUIDMap[Summary.get()] = GUID;
2636 }
2637
2638 // Print the global value summary entries.
2639 for (auto &GlobalList : *TheIndex) {
2640 auto GUID = GlobalList.first;
2641 auto VI = TheIndex->getValueInfo(GlobalList);
2642 printSummaryInfo(Machine.getGUIDSlot(GUID), VI);
2643 }
2644
2645 // Print the TypeIdMap entries.
2646 for (auto &TId : TheIndex->typeIds()) {
2647 auto GUID = GlobalValue::getGUID(TId.first);
2648 Out << "^" << Machine.getGUIDSlot(GUID) << " = typeid: (name: \""
2649 << TId.first << "\"";
2650 printTypeIdSummary(TId.second);
2651 Out << ") ; guid = " << GUID << "\n";
2652 }
2653}
2654
2655static const char *
2656getWholeProgDevirtResKindName(WholeProgramDevirtResolution::Kind K) {
2657 switch (K) {
2658 case WholeProgramDevirtResolution::Indir:
2659 return "indir";
2660 case WholeProgramDevirtResolution::SingleImpl:
2661 return "singleImpl";
2662 case WholeProgramDevirtResolution::BranchFunnel:
2663 return "branchFunnel";
2664 }
2665 llvm_unreachable("invalid WholeProgramDevirtResolution kind");
2666}
2667
2668static const char *getWholeProgDevirtResByArgKindName(
2669 WholeProgramDevirtResolution::ByArg::Kind K) {
2670 switch (K) {
2671 case WholeProgramDevirtResolution::ByArg::Indir:
2672 return "indir";
2673 case WholeProgramDevirtResolution::ByArg::UniformRetVal:
2674 return "uniformRetVal";
2675 case WholeProgramDevirtResolution::ByArg::UniqueRetVal:
2676 return "uniqueRetVal";
2677 case WholeProgramDevirtResolution::ByArg::VirtualConstProp:
2678 return "virtualConstProp";
2679 }
2680 llvm_unreachable("invalid WholeProgramDevirtResolution::ByArg kind");
2681}
2682
2683static const char *getTTResKindName(TypeTestResolution::Kind K) {
2684 switch (K) {
2685 case TypeTestResolution::Unsat:
2686 return "unsat";
2687 case TypeTestResolution::ByteArray:
2688 return "byteArray";
2689 case TypeTestResolution::Inline:
2690 return "inline";
2691 case TypeTestResolution::Single:
2692 return "single";
2693 case TypeTestResolution::AllOnes:
2694 return "allOnes";
2695 }
2696 llvm_unreachable("invalid TypeTestResolution kind");
2697}
2698
2699void AssemblyWriter::printTypeTestResolution(const TypeTestResolution &TTRes) {
2700 Out << "typeTestRes: (kind: " << getTTResKindName(TTRes.TheKind)
2701 << ", sizeM1BitWidth: " << TTRes.SizeM1BitWidth;
2702
2703 // The following fields are only used if the target does not support the use
2704 // of absolute symbols to store constants. Print only if non-zero.
2705 if (TTRes.AlignLog2)
2706 Out << ", alignLog2: " << TTRes.AlignLog2;
2707 if (TTRes.SizeM1)
2708 Out << ", sizeM1: " << TTRes.SizeM1;
2709 if (TTRes.BitMask)
2710 // BitMask is uint8_t which causes it to print the corresponding char.
2711 Out << ", bitMask: " << (unsigned)TTRes.BitMask;
2712 if (TTRes.InlineBits)
2713 Out << ", inlineBits: " << TTRes.InlineBits;
2714
2715 Out << ")";
2716}
2717
2718void AssemblyWriter::printTypeIdSummary(const TypeIdSummary &TIS) {
2719 Out << ", summary: (";
2720 printTypeTestResolution(TIS.TTRes);
2721 if (!TIS.WPDRes.empty()) {
2722 Out << ", wpdResolutions: (";
2723 FieldSeparator FS;
2724 for (auto &WPDRes : TIS.WPDRes) {
2725 Out << FS;
2726 Out << "(offset: " << WPDRes.first << ", ";
2727 printWPDRes(WPDRes.second);
2728 Out << ")";
2729 }
2730 Out << ")";
2731 }
2732 Out << ")";
2733}
2734
2735void AssemblyWriter::printArgs(const std::vector<uint64_t> &Args) {
2736 Out << "args: (";
2737 FieldSeparator FS;
2738 for (auto arg : Args) {
2739 Out << FS;
2740 Out << arg;
2741 }
2742 Out << ")";
2743}
2744
2745void AssemblyWriter::printWPDRes(const WholeProgramDevirtResolution &WPDRes) {
2746 Out << "wpdRes: (kind: ";
2747 Out << getWholeProgDevirtResKindName(WPDRes.TheKind);
2748
2749 if (WPDRes.TheKind == WholeProgramDevirtResolution::SingleImpl)
2750 Out << ", singleImplName: \"" << WPDRes.SingleImplName << "\"";
2751
2752 if (!WPDRes.ResByArg.empty()) {
2753 Out << ", resByArg: (";
2754 FieldSeparator FS;
2755 for (auto &ResByArg : WPDRes.ResByArg) {
2756 Out << FS;
2757 printArgs(ResByArg.first);
2758 Out << ", byArg: (kind: ";
2759 Out << getWholeProgDevirtResByArgKindName(ResByArg.second.TheKind);
2760 if (ResByArg.second.TheKind ==
2761 WholeProgramDevirtResolution::ByArg::UniformRetVal ||
2762 ResByArg.second.TheKind ==
2763 WholeProgramDevirtResolution::ByArg::UniqueRetVal)
2764 Out << ", info: " << ResByArg.second.Info;
2765
2766 // The following fields are only used if the target does not support the
2767 // use of absolute symbols to store constants. Print only if non-zero.
2768 if (ResByArg.second.Byte || ResByArg.second.Bit)
2769 Out << ", byte: " << ResByArg.second.Byte
2770 << ", bit: " << ResByArg.second.Bit;
2771
2772 Out << ")";
2773 }
2774 Out << ")";
2775 }
2776 Out << ")";
2777}
2778
2779static const char *getSummaryKindName(GlobalValueSummary::SummaryKind SK) {
2780 switch (SK) {
2781 case GlobalValueSummary::AliasKind:
2782 return "alias";
2783 case GlobalValueSummary::FunctionKind:
2784 return "function";
2785 case GlobalValueSummary::GlobalVarKind:
2786 return "variable";
2787 }
2788 llvm_unreachable("invalid summary kind");
2789}
2790
2791void AssemblyWriter::printAliasSummary(const AliasSummary *AS) {
Teresa Johnsonf8182f12018-07-03 01:11:43 +00002792 Out << ", aliasee: ";
2793 // The indexes emitted for distributed backends may not include the
2794 // aliasee summary (only if it is being imported directly). Handle
2795 // that case by just emitting "null" as the aliasee.
2796 if (AS->hasAliasee())
2797 Out << "^" << Machine.getGUIDSlot(SummaryToGUIDMap[&AS->getAliasee()]);
2798 else
2799 Out << "null";
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00002800}
2801
2802void AssemblyWriter::printGlobalVarSummary(const GlobalVarSummary *GS) {
2803 // Nothing for now
2804}
2805
Teresa Johnsonc80c8732018-05-25 15:15:39 +00002806static std::string getLinkageName(GlobalValue::LinkageTypes LT) {
2807 switch (LT) {
2808 case GlobalValue::ExternalLinkage:
2809 return "external";
2810 case GlobalValue::PrivateLinkage:
2811 return "private";
2812 case GlobalValue::InternalLinkage:
2813 return "internal";
2814 case GlobalValue::LinkOnceAnyLinkage:
2815 return "linkonce";
2816 case GlobalValue::LinkOnceODRLinkage:
2817 return "linkonce_odr";
2818 case GlobalValue::WeakAnyLinkage:
2819 return "weak";
2820 case GlobalValue::WeakODRLinkage:
2821 return "weak_odr";
2822 case GlobalValue::CommonLinkage:
2823 return "common";
2824 case GlobalValue::AppendingLinkage:
2825 return "appending";
2826 case GlobalValue::ExternalWeakLinkage:
2827 return "extern_weak";
2828 case GlobalValue::AvailableExternallyLinkage:
2829 return "available_externally";
2830 }
2831 llvm_unreachable("invalid linkage");
2832}
2833
2834// When printing the linkage types in IR where the ExternalLinkage is
2835// not printed, and other linkage types are expected to be printed with
2836// a space after the name.
2837static std::string getLinkageNameWithSpace(GlobalValue::LinkageTypes LT) {
2838 if (LT == GlobalValue::ExternalLinkage)
2839 return "";
2840 return getLinkageName(LT) + " ";
2841}
2842
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00002843static const char *getHotnessName(CalleeInfo::HotnessType HT) {
2844 switch (HT) {
2845 case CalleeInfo::HotnessType::Unknown:
2846 return "unknown";
2847 case CalleeInfo::HotnessType::Cold:
2848 return "cold";
2849 case CalleeInfo::HotnessType::None:
2850 return "none";
2851 case CalleeInfo::HotnessType::Hot:
2852 return "hot";
2853 case CalleeInfo::HotnessType::Critical:
2854 return "critical";
2855 }
2856 llvm_unreachable("invalid hotness");
2857}
2858
2859void AssemblyWriter::printFunctionSummary(const FunctionSummary *FS) {
2860 Out << ", insts: " << FS->instCount();
2861
2862 FunctionSummary::FFlags FFlags = FS->fflags();
2863 if (FFlags.ReadNone | FFlags.ReadOnly | FFlags.NoRecurse |
2864 FFlags.ReturnDoesNotAlias) {
2865 Out << ", funcFlags: (";
2866 Out << "readNone: " << FFlags.ReadNone;
2867 Out << ", readOnly: " << FFlags.ReadOnly;
2868 Out << ", noRecurse: " << FFlags.NoRecurse;
2869 Out << ", returnDoesNotAlias: " << FFlags.ReturnDoesNotAlias;
2870 Out << ")";
2871 }
2872 if (!FS->calls().empty()) {
2873 Out << ", calls: (";
2874 FieldSeparator IFS;
2875 for (auto &Call : FS->calls()) {
2876 Out << IFS;
2877 Out << "(callee: ^" << Machine.getGUIDSlot(Call.first.getGUID());
2878 if (Call.second.getHotness() != CalleeInfo::HotnessType::Unknown)
2879 Out << ", hotness: " << getHotnessName(Call.second.getHotness());
2880 else if (Call.second.RelBlockFreq)
2881 Out << ", relbf: " << Call.second.RelBlockFreq;
2882 Out << ")";
2883 }
2884 Out << ")";
2885 }
2886
2887 if (const auto *TIdInfo = FS->getTypeIdInfo())
2888 printTypeIdInfo(*TIdInfo);
2889}
2890
2891void AssemblyWriter::printTypeIdInfo(
2892 const FunctionSummary::TypeIdInfo &TIDInfo) {
2893 Out << ", typeIdInfo: (";
2894 FieldSeparator TIDFS;
2895 if (!TIDInfo.TypeTests.empty()) {
2896 Out << TIDFS;
2897 Out << "typeTests: (";
2898 FieldSeparator FS;
2899 for (auto &GUID : TIDInfo.TypeTests) {
2900 Out << FS;
2901 auto Slot = Machine.getGUIDSlot(GUID);
2902 if (Slot != -1)
2903 Out << "^" << Slot;
2904 else
2905 Out << GUID;
2906 }
2907 Out << ")";
2908 }
2909 if (!TIDInfo.TypeTestAssumeVCalls.empty()) {
2910 Out << TIDFS;
2911 printNonConstVCalls(TIDInfo.TypeTestAssumeVCalls, "typeTestAssumeVCalls");
2912 }
2913 if (!TIDInfo.TypeCheckedLoadVCalls.empty()) {
2914 Out << TIDFS;
2915 printNonConstVCalls(TIDInfo.TypeCheckedLoadVCalls, "typeCheckedLoadVCalls");
2916 }
2917 if (!TIDInfo.TypeTestAssumeConstVCalls.empty()) {
2918 Out << TIDFS;
2919 printConstVCalls(TIDInfo.TypeTestAssumeConstVCalls,
2920 "typeTestAssumeConstVCalls");
2921 }
2922 if (!TIDInfo.TypeCheckedLoadConstVCalls.empty()) {
2923 Out << TIDFS;
2924 printConstVCalls(TIDInfo.TypeCheckedLoadConstVCalls,
2925 "typeCheckedLoadConstVCalls");
2926 }
2927 Out << ")";
2928}
2929
2930void AssemblyWriter::printVFuncId(const FunctionSummary::VFuncId VFId) {
2931 Out << "vFuncId: (";
2932 auto Slot = Machine.getGUIDSlot(VFId.GUID);
2933 if (Slot != -1)
2934 Out << "^" << Slot;
2935 else
2936 Out << "guid: " << VFId.GUID;
2937 Out << ", offset: " << VFId.Offset;
2938 Out << ")";
2939}
2940
2941void AssemblyWriter::printNonConstVCalls(
2942 const std::vector<FunctionSummary::VFuncId> VCallList, const char *Tag) {
2943 Out << Tag << ": (";
2944 FieldSeparator FS;
2945 for (auto &VFuncId : VCallList) {
2946 Out << FS;
2947 printVFuncId(VFuncId);
2948 }
2949 Out << ")";
2950}
2951
2952void AssemblyWriter::printConstVCalls(
2953 const std::vector<FunctionSummary::ConstVCall> VCallList, const char *Tag) {
2954 Out << Tag << ": (";
2955 FieldSeparator FS;
2956 for (auto &ConstVCall : VCallList) {
2957 Out << FS;
Teresa Johnsonc7816802018-08-14 01:49:33 +00002958 Out << "(";
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00002959 printVFuncId(ConstVCall.VFunc);
2960 if (!ConstVCall.Args.empty()) {
2961 Out << ", ";
2962 printArgs(ConstVCall.Args);
2963 }
Teresa Johnsonc7816802018-08-14 01:49:33 +00002964 Out << ")";
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00002965 }
2966 Out << ")";
2967}
2968
2969void AssemblyWriter::printSummary(const GlobalValueSummary &Summary) {
2970 GlobalValueSummary::GVFlags GVFlags = Summary.flags();
2971 GlobalValue::LinkageTypes LT = (GlobalValue::LinkageTypes)GVFlags.Linkage;
2972 Out << getSummaryKindName(Summary.getSummaryKind()) << ": ";
Teresa Johnson8fc76662018-07-02 22:09:23 +00002973 Out << "(module: ^" << Machine.getModulePathSlot(Summary.modulePath())
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00002974 << ", flags: (";
2975 Out << "linkage: " << getLinkageName(LT);
2976 Out << ", notEligibleToImport: " << GVFlags.NotEligibleToImport;
2977 Out << ", live: " << GVFlags.Live;
2978 Out << ", dsoLocal: " << GVFlags.DSOLocal;
2979 Out << ")";
2980
2981 if (Summary.getSummaryKind() == GlobalValueSummary::AliasKind)
2982 printAliasSummary(cast<AliasSummary>(&Summary));
2983 else if (Summary.getSummaryKind() == GlobalValueSummary::FunctionKind)
2984 printFunctionSummary(cast<FunctionSummary>(&Summary));
2985 else
2986 printGlobalVarSummary(cast<GlobalVarSummary>(&Summary));
2987
2988 auto RefList = Summary.refs();
2989 if (!RefList.empty()) {
2990 Out << ", refs: (";
2991 FieldSeparator FS;
2992 for (auto &Ref : RefList) {
2993 Out << FS;
2994 Out << "^" << Machine.getGUIDSlot(Ref.getGUID());
2995 }
2996 Out << ")";
2997 }
2998
2999 Out << ")";
3000}
3001
3002void AssemblyWriter::printSummaryInfo(unsigned Slot, const ValueInfo &VI) {
3003 Out << "^" << Slot << " = gv: (";
3004 if (!VI.name().empty())
3005 Out << "name: \"" << VI.name() << "\"";
3006 else
3007 Out << "guid: " << VI.getGUID();
3008 if (!VI.getSummaryList().empty()) {
3009 Out << ", summaries: (";
3010 FieldSeparator FS;
3011 for (auto &Summary : VI.getSummaryList()) {
3012 Out << FS;
3013 printSummary(*Summary);
3014 }
3015 Out << ")";
3016 }
3017 Out << ")";
3018 if (!VI.name().empty())
3019 Out << " ; guid = " << VI.getGUID();
3020 Out << "\n";
3021}
3022
Filipe Cabecinhas62431b12015-06-02 21:25:08 +00003023static void printMetadataIdentifier(StringRef Name,
3024 formatted_raw_ostream &Out) {
Nick Lewycky5bcbd732011-06-15 06:37:58 +00003025 if (Name.empty()) {
3026 Out << "<empty name> ";
3027 } else {
Filipe Cabecinhas1ac0d2d2015-06-02 21:25:00 +00003028 if (isalpha(static_cast<unsigned char>(Name[0])) || Name[0] == '-' ||
3029 Name[0] == '$' || Name[0] == '.' || Name[0] == '_')
Nick Lewycky5bcbd732011-06-15 06:37:58 +00003030 Out << Name[0];
3031 else
3032 Out << '\\' << hexdigit(Name[0] >> 4) << hexdigit(Name[0] & 0x0F);
3033 for (unsigned i = 1, e = Name.size(); i != e; ++i) {
3034 unsigned char C = Name[i];
Guy Benyei83c74e92013-02-12 21:21:59 +00003035 if (isalnum(static_cast<unsigned char>(C)) || C == '-' || C == '$' ||
3036 C == '.' || C == '_')
Nick Lewycky5bcbd732011-06-15 06:37:58 +00003037 Out << C;
3038 else
3039 Out << '\\' << hexdigit(C >> 4) << hexdigit(C & 0x0F);
3040 }
3041 }
Filipe Cabecinhas62431b12015-06-02 21:25:08 +00003042}
3043
3044void AssemblyWriter::printNamedMDNode(const NamedMDNode *NMD) {
3045 Out << '!';
3046 printMetadataIdentifier(NMD->getName(), Out);
Nick Lewycky5bcbd732011-06-15 06:37:58 +00003047 Out << " = !{";
Chris Lattner2c48c642009-12-31 01:54:05 +00003048 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
Filipe Cabecinhas1ac0d2d2015-06-02 21:25:00 +00003049 if (i)
3050 Out << ", ";
Reid Kleckner6d353342017-08-23 20:31:27 +00003051
3052 // Write DIExpressions inline.
3053 // FIXME: Ban DIExpressions in NamedMDNodes, they will serve no purpose.
3054 MDNode *Op = NMD->getOperand(i);
3055 if (auto *Expr = dyn_cast<DIExpression>(Op)) {
3056 writeDIExpression(Out, Expr, nullptr, nullptr, nullptr);
3057 continue;
3058 }
3059
3060 int Slot = Machine.getMetadataSlot(Op);
Dan Gohman0a54da22010-09-09 20:53:58 +00003061 if (Slot == -1)
3062 Out << "<badref>";
3063 else
3064 Out << '!' << Slot;
Chris Lattner2c48c642009-12-31 01:54:05 +00003065 }
3066 Out << "}\n";
3067}
3068
Chris Lattner3a36d2c2008-08-19 05:06:27 +00003069static void PrintVisibility(GlobalValue::VisibilityTypes Vis,
Dan Gohmane2745262009-08-12 17:23:50 +00003070 formatted_raw_ostream &Out) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00003071 switch (Vis) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00003072 case GlobalValue::DefaultVisibility: break;
3073 case GlobalValue::HiddenVisibility: Out << "hidden "; break;
3074 case GlobalValue::ProtectedVisibility: Out << "protected "; break;
3075 }
3076}
3077
Rafael Espindolae4b02312018-01-11 22:15:05 +00003078static void PrintDSOLocation(const GlobalValue &GV,
3079 formatted_raw_ostream &Out) {
Rafael Espindola9fbc0402018-01-18 02:08:23 +00003080 // GVs with local linkage or non default visibility are implicitly dso_local,
3081 // so we don't print it.
3082 bool Implicit = GV.hasLocalLinkage() ||
3083 (!GV.hasExternalWeakLinkage() && !GV.hasDefaultVisibility());
3084 if (GV.isDSOLocal() && !Implicit)
Sean Fertilec70d28b2017-10-26 15:00:26 +00003085 Out << "dso_local ";
3086}
3087
Nico Rieck7157bb72014-01-14 15:22:47 +00003088static void PrintDLLStorageClass(GlobalValue::DLLStorageClassTypes SCT,
3089 formatted_raw_ostream &Out) {
3090 switch (SCT) {
3091 case GlobalValue::DefaultStorageClass: break;
3092 case GlobalValue::DLLImportStorageClass: Out << "dllimport "; break;
3093 case GlobalValue::DLLExportStorageClass: Out << "dllexport "; break;
3094 }
3095}
3096
Hans Wennborgcbe34b42012-06-23 11:37:03 +00003097static void PrintThreadLocalModel(GlobalVariable::ThreadLocalMode TLM,
3098 formatted_raw_ostream &Out) {
3099 switch (TLM) {
3100 case GlobalVariable::NotThreadLocal:
3101 break;
3102 case GlobalVariable::GeneralDynamicTLSModel:
3103 Out << "thread_local ";
3104 break;
3105 case GlobalVariable::LocalDynamicTLSModel:
3106 Out << "thread_local(localdynamic) ";
3107 break;
3108 case GlobalVariable::InitialExecTLSModel:
3109 Out << "thread_local(initialexec) ";
3110 break;
3111 case GlobalVariable::LocalExecTLSModel:
3112 Out << "thread_local(localexec) ";
3113 break;
3114 }
3115}
3116
Peter Collingbourne96efdd62016-06-14 21:01:22 +00003117static StringRef getUnnamedAddrEncoding(GlobalVariable::UnnamedAddr UA) {
3118 switch (UA) {
3119 case GlobalVariable::UnnamedAddr::None:
3120 return "";
3121 case GlobalVariable::UnnamedAddr::Local:
3122 return "local_unnamed_addr";
3123 case GlobalVariable::UnnamedAddr::Global:
3124 return "unnamed_addr";
3125 }
Aaron Ballman2b9fa3f2016-06-15 15:27:53 +00003126 llvm_unreachable("Unknown UnnamedAddr");
Peter Collingbourne96efdd62016-06-14 21:01:22 +00003127}
3128
Rafael Espindola83a362c2015-01-06 22:55:16 +00003129static void maybePrintComdat(formatted_raw_ostream &Out,
3130 const GlobalObject &GO) {
3131 const Comdat *C = GO.getComdat();
3132 if (!C)
3133 return;
3134
3135 if (isa<GlobalVariable>(GO))
3136 Out << ',';
3137 Out << " comdat";
3138
3139 if (GO.getName() == C->getName())
3140 return;
3141
3142 Out << '(';
3143 PrintLLVMName(Out, C->getName(), ComdatPrefix);
3144 Out << ')';
3145}
3146
Chris Lattner7bfee412001-10-29 16:05:51 +00003147void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Dan Gohman5ded1422010-01-29 23:12:36 +00003148 if (GV->isMaterializable())
3149 Out << "; Materializable\n";
3150
Dan Gohmana2489d12010-07-20 23:55:01 +00003151 WriteAsOperandInternal(Out, GV, &TypePrinter, &Machine, GV->getParent());
Dan Gohman466876b2009-08-12 23:32:33 +00003152 Out << " = ";
Chris Lattner37798642001-09-18 04:01:05 +00003153
Chris Lattner1508d3f2008-08-19 05:16:28 +00003154 if (!GV->hasInitializer() && GV->hasExternalLinkage())
3155 Out << "external ";
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003156
Teresa Johnsonc80c8732018-05-25 15:15:39 +00003157 Out << getLinkageNameWithSpace(GV->getLinkage());
Rafael Espindolae4b02312018-01-11 22:15:05 +00003158 PrintDSOLocation(*GV, Out);
Chris Lattner1508d3f2008-08-19 05:16:28 +00003159 PrintVisibility(GV->getVisibility(), Out);
Nico Rieck7157bb72014-01-14 15:22:47 +00003160 PrintDLLStorageClass(GV->getDLLStorageClass(), Out);
Hans Wennborgcbe34b42012-06-23 11:37:03 +00003161 PrintThreadLocalModel(GV->getThreadLocalMode(), Out);
Peter Collingbourne96efdd62016-06-14 21:01:22 +00003162 StringRef UA = getUnnamedAddrEncoding(GV->getUnnamedAddr());
3163 if (!UA.empty())
3164 Out << UA << ' ';
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +00003165
Chris Lattnerac161bf2009-01-02 07:01:27 +00003166 if (unsigned AddressSpace = GV->getType()->getAddressSpace())
3167 Out << "addrspace(" << AddressSpace << ") ";
Michael Gottesman27e7ef32013-02-05 05:57:38 +00003168 if (GV->isExternallyInitialized()) Out << "externally_initialized ";
Misha Brukmana6619a92004-06-21 21:53:56 +00003169 Out << (GV->isConstant() ? "constant " : "global ");
Manuel Jacob5f6eaac2016-01-16 20:30:46 +00003170 TypePrinter.print(GV->getValueType(), Out);
Chris Lattner37798642001-09-18 04:01:05 +00003171
Dan Gohman81313fd2008-09-14 17:21:12 +00003172 if (GV->hasInitializer()) {
3173 Out << ' ';
Devang Patel983c6b12009-07-08 21:44:25 +00003174 writeOperand(GV->getInitializer(), false);
Dan Gohman81313fd2008-09-14 17:21:12 +00003175 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003176
Chris Lattner9380b812010-07-07 23:16:37 +00003177 if (GV->hasSection()) {
3178 Out << ", section \"";
Jonas Devlieghere745918ff2018-05-31 17:01:42 +00003179 printEscapedString(GV->getSection(), Out);
Chris Lattner9380b812010-07-07 23:16:37 +00003180 Out << '"';
3181 }
Rafael Espindola83a362c2015-01-06 22:55:16 +00003182 maybePrintComdat(Out, *GV);
Chris Lattner4b96c542005-11-12 00:10:19 +00003183 if (GV->getAlignment())
Chris Lattnerf8a974d2005-11-06 06:48:53 +00003184 Out << ", align " << GV->getAlignment();
Anton Korobeynikova97b6942007-04-25 14:27:10 +00003185
Peter Collingbournecceae7f2016-05-31 23:01:54 +00003186 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
3187 GV->getAllMetadata(MDs);
3188 printMetadataAttachments(MDs, ", ");
3189
Javed Absarf3d79042017-05-11 12:28:08 +00003190 auto Attrs = GV->getAttributes();
3191 if (Attrs.hasAttributes())
3192 Out << " #" << Machine.getAttributeGroupSlot(Attrs);
3193
Chris Lattner113f4f42002-06-25 16:13:24 +00003194 printInfoComment(*GV);
Chris Lattnerda975502001-09-10 07:58:01 +00003195}
3196
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +00003197void AssemblyWriter::printIndirectSymbol(const GlobalIndirectSymbol *GIS) {
3198 if (GIS->isMaterializable())
Dan Gohman5ded1422010-01-29 23:12:36 +00003199 Out << "; Materializable\n";
3200
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +00003201 WriteAsOperandInternal(Out, GIS, &TypePrinter, &Machine, GIS->getParent());
Rafael Espindola54fc2982015-06-17 17:53:31 +00003202 Out << " = ";
3203
Teresa Johnsonc80c8732018-05-25 15:15:39 +00003204 Out << getLinkageNameWithSpace(GIS->getLinkage());
Rafael Espindolae4b02312018-01-11 22:15:05 +00003205 PrintDSOLocation(*GIS, Out);
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +00003206 PrintVisibility(GIS->getVisibility(), Out);
3207 PrintDLLStorageClass(GIS->getDLLStorageClass(), Out);
3208 PrintThreadLocalModel(GIS->getThreadLocalMode(), Out);
Peter Collingbourne96efdd62016-06-14 21:01:22 +00003209 StringRef UA = getUnnamedAddrEncoding(GIS->getUnnamedAddr());
3210 if (!UA.empty())
3211 Out << UA << ' ';
Anton Korobeynikova97b6942007-04-25 14:27:10 +00003212
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +00003213 if (isa<GlobalAlias>(GIS))
3214 Out << "alias ";
Dmitry Polukhina1feff72016-04-07 12:32:19 +00003215 else if (isa<GlobalIFunc>(GIS))
3216 Out << "ifunc ";
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +00003217 else
Dmitry Polukhina1feff72016-04-07 12:32:19 +00003218 llvm_unreachable("Not an alias or ifunc!");
Anton Korobeynikova97b6942007-04-25 14:27:10 +00003219
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +00003220 TypePrinter.print(GIS->getValueType(), Out);
David Blaikie2f408302015-09-11 03:22:04 +00003221
3222 Out << ", ";
3223
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +00003224 const Constant *IS = GIS->getIndirectSymbol();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003225
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +00003226 if (!IS) {
3227 TypePrinter.print(GIS->getType(), Out);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003228 Out << " <<NULL ALIASEE>>";
Jay Foad92c19132011-08-01 12:48:54 +00003229 } else {
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +00003230 writeOperand(IS, !isa<ConstantExpr>(IS));
Jay Foad92c19132011-08-01 12:48:54 +00003231 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003232
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +00003233 printInfoComment(*GIS);
Chris Lattner1508d3f2008-08-19 05:16:28 +00003234 Out << '\n';
Anton Korobeynikova97b6942007-04-25 14:27:10 +00003235}
3236
David Majnemerdad0a642014-06-27 18:19:56 +00003237void AssemblyWriter::printComdat(const Comdat *C) {
3238 C->print(Out);
3239}
3240
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003241void AssemblyWriter::printTypeIdentities() {
Roman Tereshind96de6f2018-03-22 21:29:07 +00003242 if (TypePrinter.empty())
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003243 return;
Andrew Trickec4b6e72011-09-30 19:48:58 +00003244
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003245 Out << '\n';
Andrew Trickec4b6e72011-09-30 19:48:58 +00003246
Chris Lattner3243ea12009-03-01 00:03:38 +00003247 // Emit all numbered types.
Roman Tereshind96de6f2018-03-22 21:29:07 +00003248 auto &NumberedTypes = TypePrinter.getNumberedTypes();
3249 for (unsigned I = 0, E = NumberedTypes.size(); I != E; ++I) {
3250 Out << '%' << I << " = type ";
Andrew Trickec4b6e72011-09-30 19:48:58 +00003251
Chris Lattner3243ea12009-03-01 00:03:38 +00003252 // Make sure we print out at least one level of the type structure, so
3253 // that we do not get %2 = type %2
Roman Tereshind96de6f2018-03-22 21:29:07 +00003254 TypePrinter.printStructBody(NumberedTypes[I], Out);
Dan Gohman69273e62009-08-12 23:54:22 +00003255 Out << '\n';
Chris Lattner3243ea12009-03-01 00:03:38 +00003256 }
Andrew Trickec4b6e72011-09-30 19:48:58 +00003257
Roman Tereshind96de6f2018-03-22 21:29:07 +00003258 auto &NamedTypes = TypePrinter.getNamedTypes();
3259 for (unsigned I = 0, E = NamedTypes.size(); I != E; ++I) {
3260 PrintLLVMName(Out, NamedTypes[I]->getName(), LocalPrefix);
Chris Lattner1508d3f2008-08-19 05:16:28 +00003261 Out << " = type ";
Reid Spencere7e96712004-05-25 08:53:40 +00003262
3263 // Make sure we print out at least one level of the type structure, so
3264 // that we do not get %FILE = type %FILE
Roman Tereshind96de6f2018-03-22 21:29:07 +00003265 TypePrinter.printStructBody(NamedTypes[I], Out);
Chris Lattner3a36d2c2008-08-19 05:06:27 +00003266 Out << '\n';
Reid Spencere7e96712004-05-25 08:53:40 +00003267 }
Reid Spencer32af9e82007-01-06 07:24:44 +00003268}
3269
Misha Brukmanc566ca362004-03-02 00:22:19 +00003270/// printFunction - Print all aspects of a function.
Chris Lattner113f4f42002-06-25 16:13:24 +00003271void AssemblyWriter::printFunction(const Function *F) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00003272 // Print out the return type and name.
3273 Out << '\n';
Chris Lattner379a8d22003-04-16 20:28:45 +00003274
Misha Brukmana6619a92004-06-21 21:53:56 +00003275 if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00003276
Dan Gohman5ded1422010-01-29 23:12:36 +00003277 if (F->isMaterializable())
3278 Out << "; Materializable\n";
3279
Reid Klecknerb5180542017-03-21 16:57:19 +00003280 const AttributeList &Attrs = F->getAttributes();
3281 if (Attrs.hasAttributes(AttributeList::FunctionIndex)) {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00003282 AttributeSet AS = Attrs.getFnAttributes();
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +00003283 std::string AttrStr;
3284
Reid Klecknerc2cb5602017-04-12 00:38:00 +00003285 for (const Attribute &Attr : AS) {
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +00003286 if (!Attr.isStringAttribute()) {
3287 if (!AttrStr.empty()) AttrStr += ' ';
3288 AttrStr += Attr.getAsString();
3289 }
3290 }
3291
Bill Wendling847a5c32013-04-16 20:55:47 +00003292 if (!AttrStr.empty())
3293 Out << "; Function Attrs: " << AttrStr << '\n';
3294 }
3295
Peter Collingbourne21521892016-06-21 23:42:48 +00003296 Machine.incorporateFunction(F);
3297
3298 if (F->isDeclaration()) {
3299 Out << "declare";
3300 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
3301 F->getAllMetadata(MDs);
3302 printMetadataAttachments(MDs, " ");
3303 Out << ' ';
3304 } else
Reid Spencer7ce2d2a2006-12-29 20:29:48 +00003305 Out << "define ";
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003306
Teresa Johnsonc80c8732018-05-25 15:15:39 +00003307 Out << getLinkageNameWithSpace(F->getLinkage());
Rafael Espindolae4b02312018-01-11 22:15:05 +00003308 PrintDSOLocation(*F, Out);
Chris Lattner3a36d2c2008-08-19 05:06:27 +00003309 PrintVisibility(F->getVisibility(), Out);
Nico Rieck7157bb72014-01-14 15:22:47 +00003310 PrintDLLStorageClass(F->getDLLStorageClass(), Out);
Chris Lattner379a8d22003-04-16 20:28:45 +00003311
Chris Lattnerf7b6d312005-05-06 20:26:43 +00003312 // Print the calling convention.
Micah Villmow857186d2012-09-13 15:11:12 +00003313 if (F->getCallingConv() != CallingConv::C) {
3314 PrintCallingConv(F->getCallingConv(), Out);
3315 Out << " ";
Chris Lattnerf7b6d312005-05-06 20:26:43 +00003316 }
3317
Chris Lattner229907c2011-07-18 04:54:35 +00003318 FunctionType *FT = F->getFunctionType();
Reid Klecknerb5180542017-03-21 16:57:19 +00003319 if (Attrs.hasAttributes(AttributeList::ReturnIndex))
3320 Out << Attrs.getAsString(AttributeList::ReturnIndex) << ' ';
Chris Lattnere101c442009-02-28 21:26:53 +00003321 TypePrinter.print(F->getReturnType(), Out);
Chris Lattner585297e82008-08-19 05:26:17 +00003322 Out << ' ';
Dan Gohmana2489d12010-07-20 23:55:01 +00003323 WriteAsOperandInternal(Out, F, &TypePrinter, &Machine, F->getParent());
Misha Brukmana6619a92004-06-21 21:53:56 +00003324 Out << '(';
Chris Lattnerfee714f2001-09-07 16:36:04 +00003325
Chris Lattner7bfee412001-10-29 16:05:51 +00003326 // Loop over the arguments, printing them...
Justin Bognerd7d1a722015-09-27 22:38:50 +00003327 if (F->isDeclaration() && !IsForDebug) {
Justin Bogner58e08232015-09-02 17:54:41 +00003328 // We're only interested in the type here - don't print argument names.
3329 for (unsigned I = 0, E = FT->getNumParams(); I != E; ++I) {
Chris Lattner82738fe2007-04-18 00:57:22 +00003330 // Insert commas as we go... the first arg doesn't get a comma
Justin Bogner58e08232015-09-02 17:54:41 +00003331 if (I)
3332 Out << ", ";
3333 // Output type...
3334 TypePrinter.print(FT->getParamType(I), Out);
3335
Reid Kleckner6652a522017-04-28 18:37:16 +00003336 AttributeSet ArgAttrs = Attrs.getParamAttributes(I);
3337 if (ArgAttrs.hasAttributes())
3338 Out << ' ' << ArgAttrs.getAsString();
Chris Lattner82738fe2007-04-18 00:57:22 +00003339 }
3340 } else {
Justin Bogner58e08232015-09-02 17:54:41 +00003341 // The arguments are meaningful here, print them in detail.
Justin Bogner58e08232015-09-02 17:54:41 +00003342 for (const Argument &Arg : F->args()) {
Chris Lattner82738fe2007-04-18 00:57:22 +00003343 // Insert commas as we go... the first arg doesn't get a comma
Reid Kleckner6652a522017-04-28 18:37:16 +00003344 if (Arg.getArgNo() != 0)
Justin Bogner58e08232015-09-02 17:54:41 +00003345 Out << ", ";
Reid Kleckner6652a522017-04-28 18:37:16 +00003346 printArgument(&Arg, Attrs.getParamAttributes(Arg.getArgNo()));
Chris Lattner82738fe2007-04-18 00:57:22 +00003347 }
Reid Spencer8c4914c2006-12-31 05:24:50 +00003348 }
Chris Lattnerfee714f2001-09-07 16:36:04 +00003349
3350 // Finish printing arguments...
Chris Lattner113f4f42002-06-25 16:13:24 +00003351 if (FT->isVarArg()) {
Misha Brukmana6619a92004-06-21 21:53:56 +00003352 if (FT->getNumParams()) Out << ", ";
3353 Out << "..."; // Output varargs portion of signature!
Chris Lattnerfee714f2001-09-07 16:36:04 +00003354 }
Misha Brukmana6619a92004-06-21 21:53:56 +00003355 Out << ')';
Peter Collingbourne96efdd62016-06-14 21:01:22 +00003356 StringRef UA = getUnnamedAddrEncoding(F->getUnnamedAddr());
3357 if (!UA.empty())
3358 Out << ' ' << UA;
Reid Klecknerb5180542017-03-21 16:57:19 +00003359 if (Attrs.hasAttributes(AttributeList::FunctionIndex))
Bill Wendling04945972013-04-29 23:48:06 +00003360 Out << " #" << Machine.getAttributeGroupSlot(Attrs.getFnAttributes());
Chris Lattner9380b812010-07-07 23:16:37 +00003361 if (F->hasSection()) {
3362 Out << " section \"";
Jonas Devlieghere745918ff2018-05-31 17:01:42 +00003363 printEscapedString(F->getSection(), Out);
Chris Lattner9380b812010-07-07 23:16:37 +00003364 Out << '"';
3365 }
Rafael Espindola83a362c2015-01-06 22:55:16 +00003366 maybePrintComdat(Out, *F);
Chris Lattnerf8a974d2005-11-06 06:48:53 +00003367 if (F->getAlignment())
3368 Out << " align " << F->getAlignment();
Gordon Henriksend930f912008-08-17 18:44:35 +00003369 if (F->hasGC())
3370 Out << " gc \"" << F->getGC() << '"';
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00003371 if (F->hasPrefixData()) {
3372 Out << " prefix ";
3373 writeOperand(F->getPrefixData(), true);
3374 }
Peter Collingbourne51d2de72014-12-03 02:08:38 +00003375 if (F->hasPrologueData()) {
3376 Out << " prologue ";
3377 writeOperand(F->getPrologueData(), true);
3378 }
David Majnemer7fddecc2015-06-17 20:52:32 +00003379 if (F->hasPersonalityFn()) {
3380 Out << " personality ";
3381 writeOperand(F->getPersonalityFn(), /*PrintType=*/true);
3382 }
Peter Collingbourne51d2de72014-12-03 02:08:38 +00003383
Reid Spencer5301e7c2007-01-30 20:08:39 +00003384 if (F->isDeclaration()) {
Chris Lattnerfb483622010-09-02 22:52:10 +00003385 Out << '\n';
Chris Lattnerb2f02e52002-05-06 03:00:40 +00003386 } else {
Peter Collingbourne21521892016-06-21 23:42:48 +00003387 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
3388 F->getAllMetadata(MDs);
3389 printMetadataAttachments(MDs, " ");
3390
Chris Lattnerfb483622010-09-02 22:52:10 +00003391 Out << " {";
3392 // Output all of the function's basic blocks.
Benjamin Krameraf28e7d2016-06-26 14:10:56 +00003393 for (const BasicBlock &BB : *F)
3394 printBasicBlock(&BB);
Chris Lattnerfee714f2001-09-07 16:36:04 +00003395
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +00003396 // Output the function's use-lists.
3397 printUseLists(F);
3398
Misha Brukmana6619a92004-06-21 21:53:56 +00003399 Out << "}\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +00003400 }
3401
Reid Spencer16f2f7f2004-05-26 07:18:52 +00003402 Machine.purgeFunction();
Chris Lattner2f7c9632001-06-06 20:29:01 +00003403}
3404
Misha Brukmanc566ca362004-03-02 00:22:19 +00003405/// printArgument - This member is called for every argument that is passed into
3406/// the function. Simply print it out
Reid Kleckner6652a522017-04-28 18:37:16 +00003407void AssemblyWriter::printArgument(const Argument *Arg, AttributeSet Attrs) {
Chris Lattner2f7c9632001-06-06 20:29:01 +00003408 // Output type...
Chris Lattnere101c442009-02-28 21:26:53 +00003409 TypePrinter.print(Arg->getType(), Out);
Misha Brukmanb1c93172005-04-21 23:48:37 +00003410
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00003411 // Output parameter attributes list
Reid Kleckner6652a522017-04-28 18:37:16 +00003412 if (Attrs.hasAttributes())
3413 Out << ' ' << Attrs.getAsString();
Reid Spencer8c4914c2006-12-31 05:24:50 +00003414
Chris Lattner2f7c9632001-06-06 20:29:01 +00003415 // Output name, if available...
Chris Lattner033935d2008-08-17 04:40:13 +00003416 if (Arg->hasName()) {
3417 Out << ' ';
3418 PrintLLVMName(Out, Arg);
3419 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00003420}
3421
Misha Brukmanc566ca362004-03-02 00:22:19 +00003422/// printBasicBlock - This member is called for each basic block in a method.
Chris Lattner7bfee412001-10-29 16:05:51 +00003423void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Nick Lewycky4d43d3c2008-04-25 16:53:59 +00003424 if (BB->hasName()) { // Print out the label if it exists...
Chris Lattner033935d2008-08-17 04:40:13 +00003425 Out << "\n";
Daniel Dunbare03eecb2009-07-25 23:55:21 +00003426 PrintLLVMName(Out, BB->getName(), LabelPrefix);
Chris Lattner033935d2008-08-17 04:40:13 +00003427 Out << ':';
Nick Lewycky4d43d3c2008-04-25 16:53:59 +00003428 } else if (!BB->use_empty()) { // Don't print block # of no uses...
Bill Wendling35a9c3c2011-04-10 23:18:04 +00003429 Out << "\n; <label>:";
Chris Lattner5e043322007-01-11 03:54:27 +00003430 int Slot = Machine.getLocalSlot(BB);
Chris Lattner757ee0b2004-06-09 19:41:19 +00003431 if (Slot != -1)
Evgeniy Stepanove257f0f2016-01-27 21:53:08 +00003432 Out << Slot << ":";
Chris Lattner757ee0b2004-06-09 19:41:19 +00003433 else
Misha Brukmana6619a92004-06-21 21:53:56 +00003434 Out << "<badref>";
Chris Lattner58185f22002-10-02 19:38:55 +00003435 }
Chris Lattner2447ef52003-11-20 00:09:43 +00003436
Craig Topperc6207612014-04-09 06:08:46 +00003437 if (!BB->getParent()) {
Chris Lattneree97b8b2009-08-17 15:48:08 +00003438 Out.PadToColumn(50);
Dan Gohmane2745262009-08-12 17:23:50 +00003439 Out << "; Error: Block without parent!";
3440 } else if (BB != &BB->getParent()->getEntryBlock()) { // Not the entry block?
Chris Lattnerfb483622010-09-02 22:52:10 +00003441 // Output predecessors for the block.
Chris Lattneree97b8b2009-08-17 15:48:08 +00003442 Out.PadToColumn(50);
Dan Gohmane2745262009-08-12 17:23:50 +00003443 Out << ";";
Gabor Greif6c6b2fd2010-03-25 23:25:28 +00003444 const_pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003445
Chris Lattnerff834c02008-04-22 02:45:44 +00003446 if (PI == PE) {
3447 Out << " No predecessors!";
3448 } else {
Dan Gohman81313fd2008-09-14 17:21:12 +00003449 Out << " preds = ";
Chris Lattnerff834c02008-04-22 02:45:44 +00003450 writeOperand(*PI, false);
3451 for (++PI; PI != PE; ++PI) {
Dan Gohman81313fd2008-09-14 17:21:12 +00003452 Out << ", ";
Chris Lattner78e2e8b2006-12-06 06:24:27 +00003453 writeOperand(*PI, false);
Chris Lattner00211f12003-11-16 22:59:57 +00003454 }
Chris Lattner58185f22002-10-02 19:38:55 +00003455 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00003456 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00003457
Chris Lattnerff834c02008-04-22 02:45:44 +00003458 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +00003459
Misha Brukmana6619a92004-06-21 21:53:56 +00003460 if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00003461
Chris Lattnerfee714f2001-09-07 16:36:04 +00003462 // Output all of the instructions in the basic block...
Benjamin Krameraf28e7d2016-06-26 14:10:56 +00003463 for (const Instruction &I : *BB) {
3464 printInstructionLine(I);
Dan Gohmana4f709e2009-07-13 18:27:59 +00003465 }
Chris Lattner96cdd272004-03-08 18:51:45 +00003466
Misha Brukmana6619a92004-06-21 21:53:56 +00003467 if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, Out);
Chris Lattner2f7c9632001-06-06 20:29:01 +00003468}
3469
Daniel Maleaded9f932013-05-08 20:38:31 +00003470/// printInstructionLine - Print an instruction and a newline character.
3471void AssemblyWriter::printInstructionLine(const Instruction &I) {
3472 printInstruction(I);
3473 Out << '\n';
3474}
3475
Igor Laevsky2aa8caf2015-05-05 13:20:42 +00003476/// printGCRelocateComment - print comment after call to the gc.relocate
3477/// intrinsic indicating base and derived pointer names.
Manuel Jacob83eefa62016-01-05 04:03:00 +00003478void AssemblyWriter::printGCRelocateComment(const GCRelocateInst &Relocate) {
Igor Laevsky2aa8caf2015-05-05 13:20:42 +00003479 Out << " ; (";
Manuel Jacob83eefa62016-01-05 04:03:00 +00003480 writeOperand(Relocate.getBasePtr(), false);
Igor Laevsky2aa8caf2015-05-05 13:20:42 +00003481 Out << ", ";
Manuel Jacob83eefa62016-01-05 04:03:00 +00003482 writeOperand(Relocate.getDerivedPtr(), false);
Igor Laevsky2aa8caf2015-05-05 13:20:42 +00003483 Out << ")";
3484}
3485
Misha Brukmanc566ca362004-03-02 00:22:19 +00003486/// printInfoComment - Print a little comment after the instruction indicating
3487/// which slot it occupies.
Chris Lattner113f4f42002-06-25 16:13:24 +00003488void AssemblyWriter::printInfoComment(const Value &V) {
Manuel Jacob83eefa62016-01-05 04:03:00 +00003489 if (const auto *Relocate = dyn_cast<GCRelocateInst>(&V))
3490 printGCRelocateComment(*Relocate);
Igor Laevsky2aa8caf2015-05-05 13:20:42 +00003491
Bill Wendling847a5c32013-04-16 20:55:47 +00003492 if (AnnotationWriter)
Dan Gohmane34890f2010-02-10 20:41:46 +00003493 AnnotationWriter->printInfoComment(V, Out);
Chris Lattner862e3382001-10-13 06:42:36 +00003494}
3495
Reid Spencere7141c82006-08-28 01:02:49 +00003496// This member is called for each Instruction in a function..
Chris Lattner113f4f42002-06-25 16:13:24 +00003497void AssemblyWriter::printInstruction(const Instruction &I) {
Misha Brukmana6619a92004-06-21 21:53:56 +00003498 if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00003499
Dan Gohman466876b2009-08-12 23:32:33 +00003500 // Print out indentation for an instruction.
Dan Gohmanb4583b12009-08-13 01:41:52 +00003501 Out << " ";
Chris Lattner2f7c9632001-06-06 20:29:01 +00003502
3503 // Print out name if it exists...
Chris Lattner033935d2008-08-17 04:40:13 +00003504 if (I.hasName()) {
3505 PrintLLVMName(Out, &I);
3506 Out << " = ";
Chris Lattner031560c2009-12-29 07:25:48 +00003507 } else if (!I.getType()->isVoidTy()) {
Chris Lattnerb25e5ea2008-08-29 17:19:30 +00003508 // Print out the def slot taken.
3509 int SlotNum = Machine.getLocalSlot(&I);
3510 if (SlotNum == -1)
3511 Out << "<badref> = ";
3512 else
3513 Out << '%' << SlotNum << " = ";
Chris Lattner033935d2008-08-17 04:40:13 +00003514 }
Andrew Trickec4b6e72011-09-30 19:48:58 +00003515
Reid Kleckner5772b772014-04-24 20:14:34 +00003516 if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
3517 if (CI->isMustTailCall())
3518 Out << "musttail ";
3519 else if (CI->isTailCall())
3520 Out << "tail ";
Akira Hatanaka5cfcce122015-11-06 23:55:38 +00003521 else if (CI->isNoTailCall())
3522 Out << "notail ";
Reid Kleckner5772b772014-04-24 20:14:34 +00003523 }
Chris Lattner504f9242003-09-08 17:45:59 +00003524
Chris Lattner2f7c9632001-06-06 20:29:01 +00003525 // Print out the opcode...
Misha Brukmana6619a92004-06-21 21:53:56 +00003526 Out << I.getOpcodeName();
Chris Lattner2f7c9632001-06-06 20:29:01 +00003527
Eli Friedman02e737b2011-08-12 22:50:01 +00003528 // If this is an atomic load or store, print out the atomic marker.
3529 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isAtomic()) ||
3530 (isa<StoreInst>(I) && cast<StoreInst>(I).isAtomic()))
3531 Out << " atomic";
3532
Tim Northover420a2162014-06-13 14:24:07 +00003533 if (isa<AtomicCmpXchgInst>(I) && cast<AtomicCmpXchgInst>(I).isWeak())
3534 Out << " weak";
3535
Eli Friedman02e737b2011-08-12 22:50:01 +00003536 // If this is a volatile operation, print out the volatile marker.
3537 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) ||
3538 (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile()) ||
3539 (isa<AtomicCmpXchgInst>(I) && cast<AtomicCmpXchgInst>(I).isVolatile()) ||
3540 (isa<AtomicRMWInst>(I) && cast<AtomicRMWInst>(I).isVolatile()))
3541 Out << " volatile";
3542
Dan Gohman9c7f8082009-07-27 16:11:46 +00003543 // Print out optimization information.
3544 WriteOptimizationInfo(Out, &I);
3545
Reid Spencer45e52392006-12-03 06:27:29 +00003546 // Print out the compare instruction predicates
Nate Begemand2195702008-05-12 19:01:56 +00003547 if (const CmpInst *CI = dyn_cast<CmpInst>(&I))
Tim Northoverde3aea0412016-08-17 20:25:25 +00003548 Out << ' ' << CmpInst::getPredicateName(CI->getPredicate());
Reid Spencer45e52392006-12-03 06:27:29 +00003549
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003550 // Print out the atomicrmw operation
3551 if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(&I))
3552 writeAtomicRMWOperation(Out, RMWI->getOperation());
3553
Chris Lattner2f7c9632001-06-06 20:29:01 +00003554 // Print out the type of the operands...
Craig Topperc6207612014-04-09 06:08:46 +00003555 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : nullptr;
Chris Lattner2f7c9632001-06-06 20:29:01 +00003556
3557 // Special case conditional branches to swizzle the condition out to the front
Gabor Greifcab008f2009-02-09 15:45:06 +00003558 if (isa<BranchInst>(I) && cast<BranchInst>(I).isConditional()) {
David Blaikieb78e9e52013-02-11 01:16:51 +00003559 const BranchInst &BI(cast<BranchInst>(I));
Dan Gohman81313fd2008-09-14 17:21:12 +00003560 Out << ' ';
Gabor Greifcab008f2009-02-09 15:45:06 +00003561 writeOperand(BI.getCondition(), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00003562 Out << ", ";
Gabor Greifcab008f2009-02-09 15:45:06 +00003563 writeOperand(BI.getSuccessor(0), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00003564 Out << ", ";
Gabor Greifcab008f2009-02-09 15:45:06 +00003565 writeOperand(BI.getSuccessor(1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00003566
Chris Lattner8d48df22002-04-13 18:34:38 +00003567 } else if (isa<SwitchInst>(I)) {
David Blaikieb78e9e52013-02-11 01:16:51 +00003568 const SwitchInst& SI(cast<SwitchInst>(I));
Chris Lattner3ed871f2009-10-27 19:13:16 +00003569 // Special case switch instruction to get formatting nice and correct.
Dan Gohman81313fd2008-09-14 17:21:12 +00003570 Out << ' ';
Eli Friedman95031ed2011-09-29 20:21:17 +00003571 writeOperand(SI.getCondition(), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00003572 Out << ", ";
Eli Friedman95031ed2011-09-29 20:21:17 +00003573 writeOperand(SI.getDefaultDest(), true);
Chris Lattner82ff9232008-08-23 22:52:27 +00003574 Out << " [";
Chandler Carruth927d8e62017-04-12 07:27:28 +00003575 for (auto Case : SI.cases()) {
Dan Gohmanb4583b12009-08-13 01:41:52 +00003576 Out << "\n ";
Chandler Carruth927d8e62017-04-12 07:27:28 +00003577 writeOperand(Case.getCaseValue(), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00003578 Out << ", ";
Chandler Carruth927d8e62017-04-12 07:27:28 +00003579 writeOperand(Case.getCaseSuccessor(), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00003580 }
Dan Gohmanb4583b12009-08-13 01:41:52 +00003581 Out << "\n ]";
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003582 } else if (isa<IndirectBrInst>(I)) {
3583 // Special case indirectbr instruction to get formatting nice and correct.
Chris Lattner3ed871f2009-10-27 19:13:16 +00003584 Out << ' ';
3585 writeOperand(Operand, true);
Dan Gohman43c57402009-10-30 02:01:10 +00003586 Out << ", [";
Andrew Trickec4b6e72011-09-30 19:48:58 +00003587
Chris Lattner3ed871f2009-10-27 19:13:16 +00003588 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) {
3589 if (i != 1)
3590 Out << ", ";
3591 writeOperand(I.getOperand(i), true);
3592 }
3593 Out << ']';
Jay Foad372ad642011-06-20 14:18:48 +00003594 } else if (const PHINode *PN = dyn_cast<PHINode>(&I)) {
Misha Brukmana6619a92004-06-21 21:53:56 +00003595 Out << ' ';
Chris Lattnere101c442009-02-28 21:26:53 +00003596 TypePrinter.print(I.getType(), Out);
Misha Brukmana6619a92004-06-21 21:53:56 +00003597 Out << ' ';
Chris Lattner2f7c9632001-06-06 20:29:01 +00003598
Jay Foad372ad642011-06-20 14:18:48 +00003599 for (unsigned op = 0, Eop = PN->getNumIncomingValues(); op < Eop; ++op) {
Misha Brukmana6619a92004-06-21 21:53:56 +00003600 if (op) Out << ", ";
Dan Gohman81313fd2008-09-14 17:21:12 +00003601 Out << "[ ";
Jay Foad372ad642011-06-20 14:18:48 +00003602 writeOperand(PN->getIncomingValue(op), false); Out << ", ";
3603 writeOperand(PN->getIncomingBlock(op), false); Out << " ]";
Chris Lattner931ef3b2001-06-11 15:04:20 +00003604 }
Dan Gohmana76f0f72008-05-31 19:12:39 +00003605 } else if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(&I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00003606 Out << ' ';
Dan Gohmana76f0f72008-05-31 19:12:39 +00003607 writeOperand(I.getOperand(0), true);
3608 for (const unsigned *i = EVI->idx_begin(), *e = EVI->idx_end(); i != e; ++i)
3609 Out << ", " << *i;
3610 } else if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(&I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00003611 Out << ' ';
3612 writeOperand(I.getOperand(0), true); Out << ", ";
Dan Gohmana76f0f72008-05-31 19:12:39 +00003613 writeOperand(I.getOperand(1), true);
3614 for (const unsigned *i = IVI->idx_begin(), *e = IVI->idx_end(); i != e; ++i)
3615 Out << ", " << *i;
Bill Wendlingfae14752011-08-12 20:24:12 +00003616 } else if (const LandingPadInst *LPI = dyn_cast<LandingPadInst>(&I)) {
3617 Out << ' ';
3618 TypePrinter.print(I.getType(), Out);
David Majnemer7fddecc2015-06-17 20:52:32 +00003619 if (LPI->isCleanup() || LPI->getNumClauses() != 0)
3620 Out << '\n';
Bill Wendlingfae14752011-08-12 20:24:12 +00003621
3622 if (LPI->isCleanup())
3623 Out << " cleanup";
3624
3625 for (unsigned i = 0, e = LPI->getNumClauses(); i != e; ++i) {
3626 if (i != 0 || LPI->isCleanup()) Out << "\n";
3627 if (LPI->isCatch(i))
3628 Out << " catch ";
3629 else
3630 Out << " filter ";
3631
3632 writeOperand(LPI->getClause(i), true);
3633 }
David Majnemer8a1c45d2015-12-12 05:38:55 +00003634 } else if (const auto *CatchSwitch = dyn_cast<CatchSwitchInst>(&I)) {
3635 Out << " within ";
3636 writeOperand(CatchSwitch->getParentPad(), /*PrintType=*/false);
David Majnemer654e1302015-07-31 17:58:14 +00003637 Out << " [";
David Majnemer8a1c45d2015-12-12 05:38:55 +00003638 unsigned Op = 0;
3639 for (const BasicBlock *PadBB : CatchSwitch->handlers()) {
3640 if (Op > 0)
3641 Out << ", ";
3642 writeOperand(PadBB, /*PrintType=*/true);
3643 ++Op;
3644 }
3645 Out << "] unwind ";
3646 if (const BasicBlock *UnwindDest = CatchSwitch->getUnwindDest())
3647 writeOperand(UnwindDest, /*PrintType=*/true);
3648 else
3649 Out << "to caller";
3650 } else if (const auto *FPI = dyn_cast<FuncletPadInst>(&I)) {
3651 Out << " within ";
3652 writeOperand(FPI->getParentPad(), /*PrintType=*/false);
3653 Out << " [";
3654 for (unsigned Op = 0, NumOps = FPI->getNumArgOperands(); Op < NumOps;
David Majnemer654e1302015-07-31 17:58:14 +00003655 ++Op) {
3656 if (Op > 0)
3657 Out << ", ";
David Majnemer8a1c45d2015-12-12 05:38:55 +00003658 writeOperand(FPI->getArgOperand(Op), /*PrintType=*/true);
David Majnemer654e1302015-07-31 17:58:14 +00003659 }
David Majnemer8a1c45d2015-12-12 05:38:55 +00003660 Out << ']';
Devang Patel59643e52008-02-23 00:35:18 +00003661 } else if (isa<ReturnInst>(I) && !Operand) {
3662 Out << " void";
David Majnemer0bc0eef2015-08-15 02:46:08 +00003663 } else if (const auto *CRI = dyn_cast<CatchReturnInst>(&I)) {
David Majnemer8a1c45d2015-12-12 05:38:55 +00003664 Out << " from ";
3665 writeOperand(CRI->getOperand(0), /*PrintType=*/false);
David Majnemer0bc0eef2015-08-15 02:46:08 +00003666
3667 Out << " to ";
David Majnemer8a1c45d2015-12-12 05:38:55 +00003668 writeOperand(CRI->getOperand(1), /*PrintType=*/true);
David Majnemer654e1302015-07-31 17:58:14 +00003669 } else if (const auto *CRI = dyn_cast<CleanupReturnInst>(&I)) {
David Majnemer8a1c45d2015-12-12 05:38:55 +00003670 Out << " from ";
3671 writeOperand(CRI->getOperand(0), /*PrintType=*/false);
David Majnemer654e1302015-07-31 17:58:14 +00003672
3673 Out << " unwind ";
3674 if (CRI->hasUnwindDest())
David Majnemer8a1c45d2015-12-12 05:38:55 +00003675 writeOperand(CRI->getOperand(1), /*PrintType=*/true);
Joseph Tremoulet9ce71f72015-09-03 09:09:43 +00003676 else
3677 Out << "to caller";
Chris Lattnerf7b6d312005-05-06 20:26:43 +00003678 } else if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
3679 // Print the calling convention being used.
Micah Villmow857186d2012-09-13 15:11:12 +00003680 if (CI->getCallingConv() != CallingConv::C) {
3681 Out << " ";
3682 PrintCallingConv(CI->getCallingConv(), Out);
Chris Lattnerf7b6d312005-05-06 20:26:43 +00003683 }
3684
Gabor Greifc9a92512010-06-23 13:09:06 +00003685 Operand = CI->getCalledValue();
Ahmed Bougachaaa9fe532016-12-21 23:26:13 +00003686 FunctionType *FTy = CI->getFunctionType();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003687 Type *RetTy = FTy->getReturnType();
Reid Klecknerb5180542017-03-21 16:57:19 +00003688 const AttributeList &PAL = CI->getAttributes();
Chris Lattner2f2d9472001-11-06 21:28:12 +00003689
Reid Klecknerb5180542017-03-21 16:57:19 +00003690 if (PAL.hasAttributes(AttributeList::ReturnIndex))
3691 Out << ' ' << PAL.getAsString(AttributeList::ReturnIndex);
Devang Patel221fe422008-09-29 20:49:50 +00003692
Chris Lattner463d6a52003-08-05 15:34:45 +00003693 // If possible, print out the short form of the call instruction. We can
Chris Lattner6915f8f2002-04-07 22:49:37 +00003694 // only do this if the first argument is a pointer to a nonvararg function,
Chris Lattner463d6a52003-08-05 15:34:45 +00003695 // and if the return type is not a pointer to a function.
Chris Lattner2f2d9472001-11-06 21:28:12 +00003696 //
Dan Gohman81313fd2008-09-14 17:21:12 +00003697 Out << ' ';
David Blaikie23af6482015-04-16 23:24:18 +00003698 TypePrinter.print(FTy->isVarArg() ? FTy : RetTy, Out);
3699 Out << ' ';
3700 writeOperand(Operand, false);
Misha Brukmana6619a92004-06-21 21:53:56 +00003701 Out << '(';
Gabor Greifc9a92512010-06-23 13:09:06 +00003702 for (unsigned op = 0, Eop = CI->getNumArgOperands(); op < Eop; ++op) {
3703 if (op > 0)
Dan Gohman81313fd2008-09-14 17:21:12 +00003704 Out << ", ";
Reid Kleckner6652a522017-04-28 18:37:16 +00003705 writeParamOperand(CI->getArgOperand(op), PAL.getParamAttributes(op));
Chris Lattner2f7c9632001-06-06 20:29:01 +00003706 }
Reid Kleckner83498642014-08-26 00:33:28 +00003707
3708 // Emit an ellipsis if this is a musttail call in a vararg function. This
3709 // is only to aid readability, musttail calls forward varargs by default.
3710 if (CI->isMustTailCall() && CI->getParent() &&
3711 CI->getParent()->getParent() &&
3712 CI->getParent()->getParent()->isVarArg())
3713 Out << ", ...";
3714
Dan Gohman81313fd2008-09-14 17:21:12 +00003715 Out << ')';
Reid Klecknerb5180542017-03-21 16:57:19 +00003716 if (PAL.hasAttributes(AttributeList::FunctionIndex))
Bill Wendlinga0323742013-02-22 09:09:42 +00003717 Out << " #" << Machine.getAttributeGroupSlot(PAL.getFnAttributes());
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00003718
3719 writeOperandBundles(CI);
Chris Lattner113f4f42002-06-25 16:13:24 +00003720 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
Gabor Greifa2fbc0a2010-03-24 13:21:49 +00003721 Operand = II->getCalledValue();
Ahmed Bougachaaa9fe532016-12-21 23:26:13 +00003722 FunctionType *FTy = II->getFunctionType();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003723 Type *RetTy = FTy->getReturnType();
Reid Klecknerb5180542017-03-21 16:57:19 +00003724 const AttributeList &PAL = II->getAttributes();
Chris Lattner463d6a52003-08-05 15:34:45 +00003725
Chris Lattnerf7b6d312005-05-06 20:26:43 +00003726 // Print the calling convention being used.
Micah Villmow857186d2012-09-13 15:11:12 +00003727 if (II->getCallingConv() != CallingConv::C) {
3728 Out << " ";
3729 PrintCallingConv(II->getCallingConv(), Out);
Chris Lattnerf7b6d312005-05-06 20:26:43 +00003730 }
3731
Reid Klecknerb5180542017-03-21 16:57:19 +00003732 if (PAL.hasAttributes(AttributeList::ReturnIndex))
3733 Out << ' ' << PAL.getAsString(AttributeList::ReturnIndex);
Devang Patel221fe422008-09-29 20:49:50 +00003734
Chris Lattner463d6a52003-08-05 15:34:45 +00003735 // If possible, print out the short form of the invoke instruction. We can
3736 // only do this if the first argument is a pointer to a nonvararg function,
3737 // and if the return type is not a pointer to a function.
3738 //
Dan Gohmanc7e00ba2008-10-15 18:02:08 +00003739 Out << ' ';
David Blaikie445e3fb2015-04-24 19:32:54 +00003740 TypePrinter.print(FTy->isVarArg() ? FTy : RetTy, Out);
3741 Out << ' ';
3742 writeOperand(Operand, false);
Misha Brukmana6619a92004-06-21 21:53:56 +00003743 Out << '(';
Gabor Greifc9a92512010-06-23 13:09:06 +00003744 for (unsigned op = 0, Eop = II->getNumArgOperands(); op < Eop; ++op) {
Gabor Greifa2fbc0a2010-03-24 13:21:49 +00003745 if (op)
Dan Gohman81313fd2008-09-14 17:21:12 +00003746 Out << ", ";
Reid Kleckner6652a522017-04-28 18:37:16 +00003747 writeParamOperand(II->getArgOperand(op), PAL.getParamAttributes(op));
Chris Lattner862e3382001-10-13 06:42:36 +00003748 }
3749
Dan Gohman81313fd2008-09-14 17:21:12 +00003750 Out << ')';
Reid Klecknerb5180542017-03-21 16:57:19 +00003751 if (PAL.hasAttributes(AttributeList::FunctionIndex))
Bill Wendlinga0323742013-02-22 09:09:42 +00003752 Out << " #" << Machine.getAttributeGroupSlot(PAL.getFnAttributes());
Devang Patela05633e2008-09-26 22:53:05 +00003753
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00003754 writeOperandBundles(II);
3755
Dan Gohmanb4583b12009-08-13 01:41:52 +00003756 Out << "\n to ";
Chris Lattner862e3382001-10-13 06:42:36 +00003757 writeOperand(II->getNormalDest(), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00003758 Out << " unwind ";
Chris Lattnerfae8ab32004-02-08 21:44:31 +00003759 writeOperand(II->getUnwindDest(), true);
Victor Hernandez8acf2952009-10-23 21:09:37 +00003760 } else if (const AllocaInst *AI = dyn_cast<AllocaInst>(&I)) {
Misha Brukmana6619a92004-06-21 21:53:56 +00003761 Out << ' ';
Reid Kleckner20844032014-01-25 01:24:06 +00003762 if (AI->isUsedWithInAlloca())
David Majnemerc4ab61c2014-03-09 06:41:58 +00003763 Out << "inalloca ";
Manman Ren9bfd0d02016-04-01 21:41:15 +00003764 if (AI->isSwiftError())
3765 Out << "swifterror ";
David Majnemerc4ab61c2014-03-09 06:41:58 +00003766 TypePrinter.print(AI->getAllocatedType(), Out);
Duncan P. N. Exon Smith720762e2015-03-13 19:30:44 +00003767
3768 // Explicitly write the array size if the code is broken, if it's an array
3769 // allocation, or if the type is not canonical for scalar allocations. The
3770 // latter case prevents the type from mutating when round-tripping through
3771 // assembly.
3772 if (!AI->getArraySize() || AI->isArrayAllocation() ||
3773 !AI->getArraySize()->getType()->isIntegerTy(32)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00003774 Out << ", ";
Chris Lattner8d48df22002-04-13 18:34:38 +00003775 writeOperand(AI->getArraySize(), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00003776 }
Nate Begeman848622f2005-11-05 09:21:28 +00003777 if (AI->getAlignment()) {
Chris Lattner7aeee3a2005-11-05 21:20:34 +00003778 Out << ", align " << AI->getAlignment();
Nate Begeman848622f2005-11-05 09:21:28 +00003779 }
Matt Arsenault3c1fc762017-04-10 22:27:50 +00003780
3781 unsigned AddrSpace = AI->getType()->getAddressSpace();
3782 if (AddrSpace != 0) {
3783 Out << ", addrspace(" << AddrSpace << ')';
3784 }
Chris Lattner862e3382001-10-13 06:42:36 +00003785 } else if (isa<CastInst>(I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00003786 if (Operand) {
3787 Out << ' ';
3788 writeOperand(Operand, true); // Work with broken code
3789 }
Misha Brukmana6619a92004-06-21 21:53:56 +00003790 Out << " to ";
Chris Lattnere101c442009-02-28 21:26:53 +00003791 TypePrinter.print(I.getType(), Out);
Chris Lattner5b337482003-10-18 05:57:43 +00003792 } else if (isa<VAArgInst>(I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00003793 if (Operand) {
3794 Out << ' ';
3795 writeOperand(Operand, true); // Work with broken code
3796 }
Misha Brukmana6619a92004-06-21 21:53:56 +00003797 Out << ", ";
Chris Lattnere101c442009-02-28 21:26:53 +00003798 TypePrinter.print(I.getType(), Out);
3799 } else if (Operand) { // Print the normal way.
David Blaikiea79ac142015-02-27 21:17:42 +00003800 if (const auto *GEP = dyn_cast<GetElementPtrInst>(&I)) {
David Blaikie79e6c742015-02-27 19:29:02 +00003801 Out << ' ';
3802 TypePrinter.print(GEP->getSourceElementType(), Out);
3803 Out << ',';
David Blaikiea79ac142015-02-27 21:17:42 +00003804 } else if (const auto *LI = dyn_cast<LoadInst>(&I)) {
3805 Out << ' ';
3806 TypePrinter.print(LI->getType(), Out);
Benjamin Kramer257ed692015-03-02 15:24:41 +00003807 Out << ',';
David Blaikie79e6c742015-02-27 19:29:02 +00003808 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00003809
Misha Brukmanb1c93172005-04-21 23:48:37 +00003810 // PrintAllTypes - Instructions who have operands of all the same type
Chris Lattner2f7c9632001-06-06 20:29:01 +00003811 // omit the type from all but the first operand. If the instruction has
3812 // different type operands (for example br), then they are all printed.
3813 bool PrintAllTypes = false;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003814 Type *TheType = Operand->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +00003815
Reid Spencer0cdd04f2007-02-02 13:54:55 +00003816 // Select, Store and ShuffleVector always print all types.
Devang Patelce556d92008-03-04 22:05:14 +00003817 if (isa<SelectInst>(I) || isa<StoreInst>(I) || isa<ShuffleVectorInst>(I)
3818 || isa<ReturnInst>(I)) {
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00003819 PrintAllTypes = true;
3820 } else {
3821 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
3822 Operand = I.getOperand(i);
Nuno Lopes04fe2f02009-01-15 18:40:57 +00003823 // note that Operand shouldn't be null, but the test helps make dump()
3824 // more tolerant of malformed IR
Nuno Lopes0971e772009-01-14 17:51:41 +00003825 if (Operand && Operand->getType() != TheType) {
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00003826 PrintAllTypes = true; // We have differing types! Print them all!
3827 break;
3828 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00003829 }
3830 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00003831
Chris Lattner7bfee412001-10-29 16:05:51 +00003832 if (!PrintAllTypes) {
Misha Brukmana6619a92004-06-21 21:53:56 +00003833 Out << ' ';
Chris Lattnere101c442009-02-28 21:26:53 +00003834 TypePrinter.print(TheType, Out);
Chris Lattner7bfee412001-10-29 16:05:51 +00003835 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00003836
Dan Gohman81313fd2008-09-14 17:21:12 +00003837 Out << ' ';
Chris Lattner113f4f42002-06-25 16:13:24 +00003838 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
Dan Gohman81313fd2008-09-14 17:21:12 +00003839 if (i) Out << ", ";
Chris Lattner113f4f42002-06-25 16:13:24 +00003840 writeOperand(I.getOperand(i), PrintAllTypes);
Chris Lattner2f7c9632001-06-06 20:29:01 +00003841 }
3842 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003843
Eli Friedman59b66882011-08-09 23:02:53 +00003844 // Print atomic ordering/alignment for memory operations
3845 if (const LoadInst *LI = dyn_cast<LoadInst>(&I)) {
3846 if (LI->isAtomic())
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003847 writeAtomic(LI->getContext(), LI->getOrdering(), LI->getSyncScopeID());
Eli Friedman59b66882011-08-09 23:02:53 +00003848 if (LI->getAlignment())
3849 Out << ", align " << LI->getAlignment();
3850 } else if (const StoreInst *SI = dyn_cast<StoreInst>(&I)) {
3851 if (SI->isAtomic())
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003852 writeAtomic(SI->getContext(), SI->getOrdering(), SI->getSyncScopeID());
Eli Friedman59b66882011-08-09 23:02:53 +00003853 if (SI->getAlignment())
3854 Out << ", align " << SI->getAlignment();
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003855 } else if (const AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(&I)) {
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003856 writeAtomicCmpXchg(CXI->getContext(), CXI->getSuccessOrdering(),
3857 CXI->getFailureOrdering(), CXI->getSyncScopeID());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003858 } else if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(&I)) {
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003859 writeAtomic(RMWI->getContext(), RMWI->getOrdering(),
3860 RMWI->getSyncScopeID());
Eli Friedmanfee02c62011-07-25 23:16:38 +00003861 } else if (const FenceInst *FI = dyn_cast<FenceInst>(&I)) {
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003862 writeAtomic(FI->getContext(), FI->getOrdering(), FI->getSyncScopeID());
Christopher Lamb84485702007-04-22 19:24:39 +00003863 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00003864
Chris Lattnerc9558df2009-12-28 20:10:43 +00003865 // Print Metadata info.
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00003866 SmallVector<std::pair<unsigned, MDNode *>, 4> InstMD;
Nick Lewyckyba8ec9a2010-02-25 06:53:04 +00003867 I.getAllMetadata(InstMD);
Duncan P. N. Exon Smithd3e4c2a2015-04-24 21:06:21 +00003868 printMetadataAttachments(InstMD, ", ");
Duncan P. N. Exon Smith86979272015-04-24 20:59:52 +00003869
3870 // Print a nice comment.
Chris Lattner862e3382001-10-13 06:42:36 +00003871 printInfoComment(I);
Chris Lattner2f7c9632001-06-06 20:29:01 +00003872}
3873
Duncan P. N. Exon Smith86979272015-04-24 20:59:52 +00003874void AssemblyWriter::printMetadataAttachments(
Duncan P. N. Exon Smithd3e4c2a2015-04-24 21:06:21 +00003875 const SmallVectorImpl<std::pair<unsigned, MDNode *>> &MDs,
3876 StringRef Separator) {
Duncan P. N. Exon Smith86979272015-04-24 20:59:52 +00003877 if (MDs.empty())
3878 return;
3879
Duncan P. N. Exon Smithe30f10e2015-04-24 21:03:05 +00003880 if (MDNames.empty())
Mehdi Aminib9b50aa2016-01-07 20:14:30 +00003881 MDs[0].second->getContext().getMDKindNames(MDNames);
Duncan P. N. Exon Smithe30f10e2015-04-24 21:03:05 +00003882
Duncan P. N. Exon Smith86979272015-04-24 20:59:52 +00003883 for (const auto &I : MDs) {
3884 unsigned Kind = I.first;
Duncan P. N. Exon Smithd3e4c2a2015-04-24 21:06:21 +00003885 Out << Separator;
Filipe Cabecinhas62431b12015-06-02 21:25:08 +00003886 if (Kind < MDNames.size()) {
3887 Out << "!";
3888 printMetadataIdentifier(MDNames[Kind], Out);
3889 } else
Duncan P. N. Exon Smithd3e4c2a2015-04-24 21:06:21 +00003890 Out << "!<unknown kind #" << Kind << ">";
Duncan P. N. Exon Smith86979272015-04-24 20:59:52 +00003891 Out << ' ';
3892 WriteAsOperandInternal(Out, I.second, &TypePrinter, &Machine, TheModule);
3893 }
3894}
3895
Daniel Maleaded9f932013-05-08 20:38:31 +00003896void AssemblyWriter::writeMDNode(unsigned Slot, const MDNode *Node) {
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +00003897 Out << '!' << Slot << " = ";
Daniel Maleaded9f932013-05-08 20:38:31 +00003898 printMDNodeBody(Node);
Duncan P. N. Exon Smith738889f2015-02-25 22:46:38 +00003899 Out << "\n";
Daniel Maleaded9f932013-05-08 20:38:31 +00003900}
3901
Chris Lattnerbddea6a2009-12-31 02:13:35 +00003902void AssemblyWriter::writeAllMDNodes() {
3903 SmallVector<const MDNode *, 16> Nodes;
Chris Lattnercf4a76e2009-12-31 02:20:11 +00003904 Nodes.resize(Machine.mdn_size());
3905 for (SlotTracker::mdn_iterator I = Machine.mdn_begin(), E = Machine.mdn_end();
3906 I != E; ++I)
Chris Lattnerbddea6a2009-12-31 02:13:35 +00003907 Nodes[I->second] = cast<MDNode>(I->first);
Andrew Trickec4b6e72011-09-30 19:48:58 +00003908
Chris Lattnerbddea6a2009-12-31 02:13:35 +00003909 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Daniel Maleaded9f932013-05-08 20:38:31 +00003910 writeMDNode(i, Nodes[i]);
Chris Lattnerbddea6a2009-12-31 02:13:35 +00003911 }
3912}
3913
3914void AssemblyWriter::printMDNodeBody(const MDNode *Node) {
Dan Gohmana2489d12010-07-20 23:55:01 +00003915 WriteMDNodeBodyInternal(Out, Node, &TypePrinter, &Machine, TheModule);
Chris Lattnerbddea6a2009-12-31 02:13:35 +00003916}
Chris Lattner2f7c9632001-06-06 20:29:01 +00003917
Bill Wendling829b4782013-02-11 08:43:33 +00003918void AssemblyWriter::writeAllAttributeGroups() {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00003919 std::vector<std::pair<AttributeSet, unsigned>> asVec;
Bill Wendling829b4782013-02-11 08:43:33 +00003920 asVec.resize(Machine.as_size());
3921
3922 for (SlotTracker::as_iterator I = Machine.as_begin(), E = Machine.as_end();
3923 I != E; ++I)
3924 asVec[I->second] = *I;
3925
Benjamin Krameraf28e7d2016-06-26 14:10:56 +00003926 for (const auto &I : asVec)
3927 Out << "attributes #" << I.second << " = { "
Reid Klecknerc2cb5602017-04-12 00:38:00 +00003928 << I.first.getAsString(true) << " }\n";
Bill Wendling829b4782013-02-11 08:43:33 +00003929}
3930
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +00003931void AssemblyWriter::printUseListOrder(const UseListOrder &Order) {
3932 bool IsInFunction = Machine.getFunction();
3933 if (IsInFunction)
3934 Out << " ";
3935
3936 Out << "uselistorder";
3937 if (const BasicBlock *BB =
3938 IsInFunction ? nullptr : dyn_cast<BasicBlock>(Order.V)) {
3939 Out << "_bb ";
3940 writeOperand(BB->getParent(), false);
3941 Out << ", ";
3942 writeOperand(BB, false);
3943 } else {
3944 Out << " ";
3945 writeOperand(Order.V, true);
3946 }
3947 Out << ", { ";
3948
3949 assert(Order.Shuffle.size() >= 2 && "Shuffle too small");
3950 Out << Order.Shuffle[0];
3951 for (unsigned I = 1, E = Order.Shuffle.size(); I != E; ++I)
3952 Out << ", " << Order.Shuffle[I];
3953 Out << " }\n";
3954}
3955
3956void AssemblyWriter::printUseLists(const Function *F) {
3957 auto hasMore =
3958 [&]() { return !UseListOrders.empty() && UseListOrders.back().F == F; };
3959 if (!hasMore())
3960 // Nothing to do.
3961 return;
3962
3963 Out << "\n; uselistorder directives\n";
3964 while (hasMore()) {
3965 printUseListOrder(UseListOrders.back());
3966 UseListOrders.pop_back();
3967 }
3968}
3969
Chris Lattner2f7c9632001-06-06 20:29:01 +00003970//===----------------------------------------------------------------------===//
3971// External Interface declarations
3972//===----------------------------------------------------------------------===//
3973
George Burgess IVe1100f52016-02-02 22:46:49 +00003974void Function::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW,
3975 bool ShouldPreserveUseListOrder,
3976 bool IsForDebug) const {
3977 SlotTracker SlotTable(this->getParent());
3978 formatted_raw_ostream OS(ROS);
3979 AssemblyWriter W(OS, SlotTable, this->getParent(), AAW,
3980 IsForDebug,
3981 ShouldPreserveUseListOrder);
3982 W.printFunction(this);
3983}
3984
Duncan P. N. Exon Smithc4f0a322015-04-15 02:12:41 +00003985void Module::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW,
Justin Bognerd7d1a722015-09-27 22:38:50 +00003986 bool ShouldPreserveUseListOrder, bool IsForDebug) const {
Chris Lattnere36fd8a2008-08-19 04:26:57 +00003987 SlotTracker SlotTable(this);
Dan Gohmane2745262009-08-12 17:23:50 +00003988 formatted_raw_ostream OS(ROS);
Justin Bognerd7d1a722015-09-27 22:38:50 +00003989 AssemblyWriter W(OS, SlotTable, this, AAW, IsForDebug,
3990 ShouldPreserveUseListOrder);
Chris Lattnerefb5e392009-12-31 02:23:35 +00003991 W.printModule(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00003992}
3993
Justin Bognerd7d1a722015-09-27 22:38:50 +00003994void NamedMDNode::print(raw_ostream &ROS, bool IsForDebug) const {
Dan Gohman2637cc12010-07-21 23:38:33 +00003995 SlotTracker SlotTable(getParent());
3996 formatted_raw_ostream OS(ROS);
Justin Bognerd7d1a722015-09-27 22:38:50 +00003997 AssemblyWriter W(OS, SlotTable, getParent(), nullptr, IsForDebug);
Dan Gohman2637cc12010-07-21 23:38:33 +00003998 W.printNamedMDNode(this);
3999}
4000
Duncan P. N. Exon Smith0ecff952016-04-20 17:27:44 +00004001void NamedMDNode::print(raw_ostream &ROS, ModuleSlotTracker &MST,
4002 bool IsForDebug) const {
4003 Optional<SlotTracker> LocalST;
4004 SlotTracker *SlotTable;
4005 if (auto *ST = MST.getMachine())
4006 SlotTable = ST;
4007 else {
4008 LocalST.emplace(getParent());
4009 SlotTable = &*LocalST;
4010 }
4011
4012 formatted_raw_ostream OS(ROS);
4013 AssemblyWriter W(OS, *SlotTable, getParent(), nullptr, IsForDebug);
4014 W.printNamedMDNode(this);
4015}
4016
Justin Bognerd7d1a722015-09-27 22:38:50 +00004017void Comdat::print(raw_ostream &ROS, bool /*IsForDebug*/) const {
David Majnemerdad0a642014-06-27 18:19:56 +00004018 PrintLLVMName(ROS, getName(), ComdatPrefix);
4019 ROS << " = comdat ";
4020
4021 switch (getSelectionKind()) {
4022 case Comdat::Any:
4023 ROS << "any";
4024 break;
4025 case Comdat::ExactMatch:
4026 ROS << "exactmatch";
4027 break;
4028 case Comdat::Largest:
4029 ROS << "largest";
4030 break;
4031 case Comdat::NoDuplicates:
4032 ROS << "noduplicates";
4033 break;
4034 case Comdat::SameSize:
4035 ROS << "samesize";
4036 break;
4037 }
4038
4039 ROS << '\n';
4040}
4041
Quentin Colombet774b1ef2016-03-07 22:32:42 +00004042void Type::print(raw_ostream &OS, bool /*IsForDebug*/, bool NoDetails) const {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004043 TypePrinting TP;
4044 TP.print(const_cast<Type*>(this), OS);
Andrew Trickec4b6e72011-09-30 19:48:58 +00004045
Quentin Colombet774b1ef2016-03-07 22:32:42 +00004046 if (NoDetails)
4047 return;
4048
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004049 // If the type is a named struct type, print the body as well.
4050 if (StructType *STy = dyn_cast<StructType>(const_cast<Type*>(this)))
Chris Lattner01beceb2011-08-12 18:07:07 +00004051 if (!STy->isLiteral()) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004052 OS << " = type ";
4053 TP.printStructBody(STy, OS);
4054 }
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00004055}
4056
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +00004057static bool isReferencingMDNode(const Instruction &I) {
4058 if (const auto *CI = dyn_cast<CallInst>(&I))
4059 if (Function *F = CI->getCalledFunction())
Justin Lebar9cbc3012016-07-28 23:58:15 +00004060 if (F->isIntrinsic())
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +00004061 for (auto &Op : I.operands())
4062 if (auto *V = dyn_cast_or_null<MetadataAsValue>(Op))
4063 if (isa<MDNode>(V->getMetadata()))
4064 return true;
4065 return false;
4066}
4067
Justin Bognerd7d1a722015-09-27 22:38:50 +00004068void Value::print(raw_ostream &ROS, bool IsForDebug) const {
Duncan P. N. Exon Smith1f8a99a2015-06-27 00:38:26 +00004069 bool ShouldInitializeAllMetadata = false;
4070 if (auto *I = dyn_cast<Instruction>(this))
4071 ShouldInitializeAllMetadata = isReferencingMDNode(*I);
4072 else if (isa<Function>(this) || isa<MetadataAsValue>(this))
4073 ShouldInitializeAllMetadata = true;
4074
4075 ModuleSlotTracker MST(getModuleFromVal(this), ShouldInitializeAllMetadata);
Justin Bognerd7d1a722015-09-27 22:38:50 +00004076 print(ROS, MST, IsForDebug);
Duncan P. N. Exon Smith1f8a99a2015-06-27 00:38:26 +00004077}
4078
Justin Bognerd7d1a722015-09-27 22:38:50 +00004079void Value::print(raw_ostream &ROS, ModuleSlotTracker &MST,
4080 bool IsForDebug) const {
Dan Gohman12dad632009-08-12 20:56:03 +00004081 formatted_raw_ostream OS(ROS);
Duncan P. N. Exon Smith1f8a99a2015-06-27 00:38:26 +00004082 SlotTracker EmptySlotTable(static_cast<const Module *>(nullptr));
4083 SlotTracker &SlotTable =
4084 MST.getMachine() ? *MST.getMachine() : EmptySlotTable;
4085 auto incorporateFunction = [&](const Function *F) {
4086 if (F)
4087 MST.incorporateFunction(*F);
4088 };
4089
Chris Lattner0c19df42008-08-23 22:23:09 +00004090 if (const Instruction *I = dyn_cast<Instruction>(this)) {
Duncan P. N. Exon Smith1f8a99a2015-06-27 00:38:26 +00004091 incorporateFunction(I->getParent() ? I->getParent()->getParent() : nullptr);
Justin Bognerd7d1a722015-09-27 22:38:50 +00004092 AssemblyWriter W(OS, SlotTable, getModuleFromVal(I), nullptr, IsForDebug);
Chris Lattnerefb5e392009-12-31 02:23:35 +00004093 W.printInstruction(*I);
Chris Lattner0c19df42008-08-23 22:23:09 +00004094 } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(this)) {
Duncan P. N. Exon Smith1f8a99a2015-06-27 00:38:26 +00004095 incorporateFunction(BB->getParent());
Justin Bognerd7d1a722015-09-27 22:38:50 +00004096 AssemblyWriter W(OS, SlotTable, getModuleFromVal(BB), nullptr, IsForDebug);
Chris Lattnerefb5e392009-12-31 02:23:35 +00004097 W.printBasicBlock(BB);
Chris Lattner0c19df42008-08-23 22:23:09 +00004098 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(this)) {
Justin Bognerd7d1a722015-09-27 22:38:50 +00004099 AssemblyWriter W(OS, SlotTable, GV->getParent(), nullptr, IsForDebug);
Chris Lattnerefb5e392009-12-31 02:23:35 +00004100 if (const GlobalVariable *V = dyn_cast<GlobalVariable>(GV))
4101 W.printGlobal(V);
4102 else if (const Function *F = dyn_cast<Function>(GV))
4103 W.printFunction(F);
4104 else
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +00004105 W.printIndirectSymbol(cast<GlobalIndirectSymbol>(GV));
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00004106 } else if (const MetadataAsValue *V = dyn_cast<MetadataAsValue>(this)) {
Duncan P. N. Exon Smith1f8a99a2015-06-27 00:38:26 +00004107 V->getMetadata()->print(ROS, MST, getModuleFromVal(V));
Chris Lattner0c19df42008-08-23 22:23:09 +00004108 } else if (const Constant *C = dyn_cast<Constant>(this)) {
Chris Lattner92c5c122009-02-28 23:20:19 +00004109 TypePrinting TypePrinter;
Chris Lattnere101c442009-02-28 21:26:53 +00004110 TypePrinter.print(C->getType(), OS);
Chris Lattner2ee62d22009-02-28 21:11:05 +00004111 OS << ' ';
Duncan P. N. Exon Smith1f8a99a2015-06-27 00:38:26 +00004112 WriteConstantInternal(OS, C, TypePrinter, MST.getMachine(), nullptr);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00004113 } else if (isa<InlineAsm>(this) || isa<Argument>(this)) {
Duncan P. N. Exon Smith1f8a99a2015-06-27 00:38:26 +00004114 this->printAsOperand(OS, /* PrintType */ true, MST);
Chris Lattner0c19df42008-08-23 22:23:09 +00004115 } else {
Nick Lewyckyad1b3d12014-05-09 00:49:03 +00004116 llvm_unreachable("Unknown value to print out!");
Chris Lattner0c19df42008-08-23 22:23:09 +00004117 }
4118}
4119
Duncan P. N. Exon Smith326921542015-06-26 22:04:20 +00004120/// Print without a type, skipping the TypePrinting object.
4121///
Benjamin Kramerdf005cb2015-08-08 18:27:36 +00004122/// \return \c true iff printing was successful.
Duncan P. N. Exon Smith326921542015-06-26 22:04:20 +00004123static bool printWithoutType(const Value &V, raw_ostream &O,
4124 SlotTracker *Machine, const Module *M) {
4125 if (V.hasName() || isa<GlobalValue>(V) ||
4126 (!isa<Constant>(V) && !isa<MetadataAsValue>(V))) {
4127 WriteAsOperandInternal(O, &V, nullptr, Machine, M);
4128 return true;
Chandler Carruthd48cdbf2014-01-09 02:29:41 +00004129 }
Duncan P. N. Exon Smith326921542015-06-26 22:04:20 +00004130 return false;
4131}
Chandler Carruthd48cdbf2014-01-09 02:29:41 +00004132
Duncan P. N. Exon Smith326921542015-06-26 22:04:20 +00004133static void printAsOperandImpl(const Value &V, raw_ostream &O, bool PrintType,
4134 ModuleSlotTracker &MST) {
Roman Tereshind96de6f2018-03-22 21:29:07 +00004135 TypePrinting TypePrinter(MST.getModule());
Chandler Carruthd48cdbf2014-01-09 02:29:41 +00004136 if (PrintType) {
Duncan P. N. Exon Smith326921542015-06-26 22:04:20 +00004137 TypePrinter.print(V.getType(), O);
Chandler Carruthd48cdbf2014-01-09 02:29:41 +00004138 O << ' ';
4139 }
4140
Duncan P. N. Exon Smith326921542015-06-26 22:04:20 +00004141 WriteAsOperandInternal(O, &V, &TypePrinter, MST.getMachine(),
4142 MST.getModule());
4143}
4144
4145void Value::printAsOperand(raw_ostream &O, bool PrintType,
4146 const Module *M) const {
4147 if (!M)
4148 M = getModuleFromVal(this);
4149
4150 if (!PrintType)
4151 if (printWithoutType(*this, O, nullptr, M))
4152 return;
4153
4154 SlotTracker Machine(
4155 M, /* ShouldInitializeAllMetadata */ isa<MetadataAsValue>(this));
4156 ModuleSlotTracker MST(Machine, M);
4157 printAsOperandImpl(*this, O, PrintType, MST);
4158}
4159
4160void Value::printAsOperand(raw_ostream &O, bool PrintType,
4161 ModuleSlotTracker &MST) const {
4162 if (!PrintType)
4163 if (printWithoutType(*this, O, MST.getMachine(), MST.getModule()))
4164 return;
4165
4166 printAsOperandImpl(*this, O, PrintType, MST);
Chandler Carruthd48cdbf2014-01-09 02:29:41 +00004167}
4168
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +00004169static void printMetadataImpl(raw_ostream &ROS, const Metadata &MD,
Duncan P. N. Exon Smith6529ed42015-06-26 22:28:47 +00004170 ModuleSlotTracker &MST, const Module *M,
4171 bool OnlyAsOperand) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00004172 formatted_raw_ostream OS(ROS);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00004173
Roman Tereshind96de6f2018-03-22 21:29:07 +00004174 TypePrinting TypePrinter(M);
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +00004175
Duncan P. N. Exon Smith6529ed42015-06-26 22:28:47 +00004176 WriteAsOperandInternal(OS, &MD, &TypePrinter, MST.getMachine(), M,
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00004177 /* FromValue */ true);
Duncan P. N. Exon Smith6529ed42015-06-26 22:28:47 +00004178
4179 auto *N = dyn_cast<MDNode>(&MD);
Reid Klecknerb95b4272017-08-30 20:40:36 +00004180 if (OnlyAsOperand || !N || isa<DIExpression>(MD))
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +00004181 return;
4182
4183 OS << " = ";
Duncan P. N. Exon Smith6529ed42015-06-26 22:28:47 +00004184 WriteMDNodeBodyInternal(OS, N, &TypePrinter, MST.getMachine(), M);
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +00004185}
4186
4187void Metadata::printAsOperand(raw_ostream &OS, const Module *M) const {
Duncan P. N. Exon Smith6529ed42015-06-26 22:28:47 +00004188 ModuleSlotTracker MST(M, isa<MDNode>(this));
4189 printMetadataImpl(OS, *this, MST, M, /* OnlyAsOperand */ true);
4190}
4191
4192void Metadata::printAsOperand(raw_ostream &OS, ModuleSlotTracker &MST,
4193 const Module *M) const {
4194 printMetadataImpl(OS, *this, MST, M, /* OnlyAsOperand */ true);
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +00004195}
4196
Justin Bognerd7d1a722015-09-27 22:38:50 +00004197void Metadata::print(raw_ostream &OS, const Module *M,
4198 bool /*IsForDebug*/) const {
Duncan P. N. Exon Smith6529ed42015-06-26 22:28:47 +00004199 ModuleSlotTracker MST(M, isa<MDNode>(this));
4200 printMetadataImpl(OS, *this, MST, M, /* OnlyAsOperand */ false);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00004201}
4202
Duncan P. N. Exon Smith1f8a99a2015-06-27 00:38:26 +00004203void Metadata::print(raw_ostream &OS, ModuleSlotTracker &MST,
Justin Bognerd7d1a722015-09-27 22:38:50 +00004204 const Module *M, bool /*IsForDebug*/) const {
Duncan P. N. Exon Smith1f8a99a2015-06-27 00:38:26 +00004205 printMetadataImpl(OS, *this, MST, M, /* OnlyAsOperand */ false);
4206}
4207
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00004208void ModuleSummaryIndex::print(raw_ostream &ROS, bool IsForDebug) const {
4209 SlotTracker SlotTable(this);
4210 formatted_raw_ostream OS(ROS);
4211 AssemblyWriter W(OS, SlotTable, this, IsForDebug);
4212 W.printModuleSummaryIndex();
4213}
4214
Aaron Ballman615eb472017-10-15 14:32:27 +00004215#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Chris Lattnerdab942552008-08-25 17:03:15 +00004216// Value::dump - allow easy printing of Values from the debugger.
Duncan P. N. Exon Smith89c1eaa2015-02-25 22:08:21 +00004217LLVM_DUMP_METHOD
Justin Bognerd7d1a722015-09-27 22:38:50 +00004218void Value::dump() const { print(dbgs(), /*IsForDebug=*/true); dbgs() << '\n'; }
Reid Spencer52641832004-05-25 18:14:38 +00004219
Chris Lattnerdab942552008-08-25 17:03:15 +00004220// Type::dump - allow easy printing of Types from the debugger.
Duncan P. N. Exon Smith89c1eaa2015-02-25 22:08:21 +00004221LLVM_DUMP_METHOD
Justin Bognerd7d1a722015-09-27 22:38:50 +00004222void Type::dump() const { print(dbgs(), /*IsForDebug=*/true); dbgs() << '\n'; }
Chris Lattner24025262009-02-28 21:05:51 +00004223
Chris Lattnerdab942552008-08-25 17:03:15 +00004224// Module::dump() - Allow printing of Modules from the debugger.
Duncan P. N. Exon Smith89c1eaa2015-02-25 22:08:21 +00004225LLVM_DUMP_METHOD
Justin Bognerd7d1a722015-09-27 22:38:50 +00004226void Module::dump() const {
4227 print(dbgs(), nullptr,
4228 /*ShouldPreserveUseListOrder=*/false, /*IsForDebug=*/true);
4229}
Bill Wendling3681d112011-12-09 23:18:34 +00004230
Adrian Prantl5f8f34e42018-05-01 15:54:18 +00004231// Allow printing of Comdats from the debugger.
Duncan P. N. Exon Smith89c1eaa2015-02-25 22:08:21 +00004232LLVM_DUMP_METHOD
Justin Bognerd7d1a722015-09-27 22:38:50 +00004233void Comdat::dump() const { print(dbgs(), /*IsForDebug=*/true); }
David Majnemerdad0a642014-06-27 18:19:56 +00004234
Bill Wendling3681d112011-12-09 23:18:34 +00004235// NamedMDNode::dump() - Allow printing of NamedMDNodes from the debugger.
Duncan P. N. Exon Smith89c1eaa2015-02-25 22:08:21 +00004236LLVM_DUMP_METHOD
Justin Bognerd7d1a722015-09-27 22:38:50 +00004237void NamedMDNode::dump() const { print(dbgs(), /*IsForDebug=*/true); }
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00004238
Duncan P. N. Exon Smith89c1eaa2015-02-25 22:08:21 +00004239LLVM_DUMP_METHOD
Duncan P. N. Exon Smitha66dc8f2015-03-15 06:53:32 +00004240void Metadata::dump() const { dump(nullptr); }
4241
4242LLVM_DUMP_METHOD
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +00004243void Metadata::dump(const Module *M) const {
Justin Bognerd7d1a722015-09-27 22:38:50 +00004244 print(dbgs(), M, /*IsForDebug=*/true);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00004245 dbgs() << '\n';
4246}
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00004247
4248// Allow printing of ModuleSummaryIndex from the debugger.
4249LLVM_DUMP_METHOD
4250void ModuleSummaryIndex::dump() const { print(dbgs(), /*IsForDebug=*/true); }
Matthias Braun8c209aa2017-01-28 02:02:38 +00004251#endif