blob: 699b79c8725ee30b81c749f18b4cd794fa3202a7 [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);
Adrian Prantl55f42622018-08-14 19:35:34 +00001790 Printer.printDIFlags("flags", N->getFlags());
Duncan P. N. Exon Smith09e03f32015-02-13 01:14:58 +00001791 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001792}
Duncan P. N. Exon Smith09e03f32015-02-13 01:14:58 +00001793
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001794static void writeDIDerivedType(raw_ostream &Out, const DIDerivedType *N,
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001795 TypePrinting *TypePrinter, SlotTracker *Machine,
1796 const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001797 Out << "!DIDerivedType(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001798 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1799 Printer.printTag(N);
1800 Printer.printString("name", N->getName());
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001801 Printer.printMetadata("scope", N->getRawScope());
1802 Printer.printMetadata("file", N->getRawFile());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001803 Printer.printInt("line", N->getLine());
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001804 Printer.printMetadata("baseType", N->getRawBaseType(),
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001805 /* ShouldSkipNull */ false);
1806 Printer.printInt("size", N->getSizeInBits());
1807 Printer.printInt("align", N->getAlignInBits());
1808 Printer.printInt("offset", N->getOffsetInBits());
1809 Printer.printDIFlags("flags", N->getFlags());
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001810 Printer.printMetadata("extraData", N->getRawExtraData());
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +00001811 if (const auto &DWARFAddressSpace = N->getDWARFAddressSpace())
1812 Printer.printInt("dwarfAddressSpace", *DWARFAddressSpace,
1813 /* ShouldSkipZero */ false);
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001814 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001815}
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001816
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001817static void writeDICompositeType(raw_ostream &Out, const DICompositeType *N,
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001818 TypePrinting *TypePrinter,
1819 SlotTracker *Machine, const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001820 Out << "!DICompositeType(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001821 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1822 Printer.printTag(N);
1823 Printer.printString("name", N->getName());
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001824 Printer.printMetadata("scope", N->getRawScope());
1825 Printer.printMetadata("file", N->getRawFile());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001826 Printer.printInt("line", N->getLine());
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001827 Printer.printMetadata("baseType", N->getRawBaseType());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001828 Printer.printInt("size", N->getSizeInBits());
1829 Printer.printInt("align", N->getAlignInBits());
1830 Printer.printInt("offset", N->getOffsetInBits());
1831 Printer.printDIFlags("flags", N->getFlags());
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001832 Printer.printMetadata("elements", N->getRawElements());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001833 Printer.printDwarfEnum("runtimeLang", N->getRuntimeLang(),
1834 dwarf::LanguageString);
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001835 Printer.printMetadata("vtableHolder", N->getRawVTableHolder());
1836 Printer.printMetadata("templateParams", N->getRawTemplateParams());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001837 Printer.printString("identifier", N->getIdentifier());
Adrian Prantl8c599212018-02-06 23:45:59 +00001838 Printer.printMetadata("discriminator", N->getRawDiscriminator());
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001839 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001840}
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00001841
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001842static void writeDISubroutineType(raw_ostream &Out, const DISubroutineType *N,
Duncan P. N. Exon Smith54e2bc62015-02-13 01:22:59 +00001843 TypePrinting *TypePrinter,
1844 SlotTracker *Machine, const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001845 Out << "!DISubroutineType(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001846 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1847 Printer.printDIFlags("flags", N->getFlags());
Reid Klecknerde3d8b52016-06-08 20:34:29 +00001848 Printer.printDwarfEnum("cc", N->getCC(), dwarf::ConventionString);
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001849 Printer.printMetadata("types", N->getRawTypeArray(),
1850 /* ShouldSkipNull */ false);
Duncan P. N. Exon Smith54e2bc62015-02-13 01:22:59 +00001851 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001852}
Duncan P. N. Exon Smithf14b9c72015-02-13 01:19:14 +00001853
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001854static void writeDIFile(raw_ostream &Out, const DIFile *N, TypePrinting *,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001855 SlotTracker *, const Module *) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001856 Out << "!DIFile(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001857 MDFieldPrinter Printer(Out);
1858 Printer.printString("filename", N->getFilename(),
1859 /* ShouldSkipEmpty */ false);
1860 Printer.printString("directory", N->getDirectory(),
1861 /* ShouldSkipEmpty */ false);
Scott Linder71603842018-02-12 19:45:54 +00001862 // Print all values for checksum together, or not at all.
1863 if (N->getChecksum())
1864 Printer.printChecksum(*N->getChecksum());
Scott Linder16c7bda2018-02-23 23:01:06 +00001865 Printer.printString("source", N->getSource().getValueOr(StringRef()),
1866 /* ShouldSkipEmpty */ true);
Duncan P. N. Exon Smithf14b9c72015-02-13 01:19:14 +00001867 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001868}
Duncan P. N. Exon Smithf14b9c72015-02-13 01:19:14 +00001869
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001870static void writeDICompileUnit(raw_ostream &Out, const DICompileUnit *N,
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00001871 TypePrinting *TypePrinter, SlotTracker *Machine,
1872 const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001873 Out << "!DICompileUnit(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001874 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1875 Printer.printDwarfEnum("language", N->getSourceLanguage(),
1876 dwarf::LanguageString, /* ShouldSkipZero */ false);
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001877 Printer.printMetadata("file", N->getRawFile(), /* ShouldSkipNull */ false);
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001878 Printer.printString("producer", N->getProducer());
1879 Printer.printBool("isOptimized", N->isOptimized());
1880 Printer.printString("flags", N->getFlags());
1881 Printer.printInt("runtimeVersion", N->getRuntimeVersion(),
1882 /* ShouldSkipZero */ false);
1883 Printer.printString("splitDebugFilename", N->getSplitDebugFilename());
Adrian Prantlb939a252016-03-31 23:56:58 +00001884 Printer.printEmissionKind("emissionKind", N->getEmissionKind());
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001885 Printer.printMetadata("enums", N->getRawEnumTypes());
1886 Printer.printMetadata("retainedTypes", N->getRawRetainedTypes());
Duncan P. N. Exon Smith53855f02015-03-27 23:05:04 +00001887 Printer.printMetadata("globals", N->getRawGlobalVariables());
1888 Printer.printMetadata("imports", N->getRawImportedEntities());
Amjad Abouda9bcf162015-12-10 12:56:35 +00001889 Printer.printMetadata("macros", N->getRawMacros());
Adrian Prantl1f599f92015-05-21 20:37:30 +00001890 Printer.printInt("dwoId", N->getDWOId());
David Blaikiea01f2952016-08-24 18:29:49 +00001891 Printer.printBool("splitDebugInlining", N->getSplitDebugInlining(), true);
Dehao Chen0944a8c2017-02-01 22:45:09 +00001892 Printer.printBool("debugInfoForProfiling", N->getDebugInfoForProfiling(),
1893 false);
Peter Collingbourneb52e2362017-09-12 21:50:41 +00001894 Printer.printBool("gnuPubnames", N->getGnuPubnames(), false);
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00001895 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001896}
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00001897
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001898static void writeDISubprogram(raw_ostream &Out, const DISubprogram *N,
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00001899 TypePrinting *TypePrinter, SlotTracker *Machine,
1900 const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001901 Out << "!DISubprogram(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001902 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1903 Printer.printString("name", N->getName());
1904 Printer.printString("linkageName", N->getLinkageName());
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001905 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
1906 Printer.printMetadata("file", N->getRawFile());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001907 Printer.printInt("line", N->getLine());
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001908 Printer.printMetadata("type", N->getRawType());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001909 Printer.printBool("isLocal", N->isLocalToUnit());
1910 Printer.printBool("isDefinition", N->isDefinition());
1911 Printer.printInt("scopeLine", N->getScopeLine());
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001912 Printer.printMetadata("containingType", N->getRawContainingType());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001913 Printer.printDwarfEnum("virtuality", N->getVirtuality(),
1914 dwarf::VirtualityString);
Peter Collingbournea1f86252016-03-17 23:58:03 +00001915 if (N->getVirtuality() != dwarf::DW_VIRTUALITY_none ||
1916 N->getVirtualIndex() != 0)
1917 Printer.printInt("virtualIndex", N->getVirtualIndex(), false);
Reid Klecknerb5af11d2016-07-01 02:41:21 +00001918 Printer.printInt("thisAdjustment", N->getThisAdjustment());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001919 Printer.printDIFlags("flags", N->getFlags());
1920 Printer.printBool("isOptimized", N->isOptimized());
Adrian Prantl75819ae2016-04-15 15:57:41 +00001921 Printer.printMetadata("unit", N->getRawUnit());
Duncan P. N. Exon Smith869db502015-03-30 16:19:15 +00001922 Printer.printMetadata("templateParams", N->getRawTemplateParams());
1923 Printer.printMetadata("declaration", N->getRawDeclaration());
Shiva Chen2c864552018-05-09 02:40:45 +00001924 Printer.printMetadata("retainedNodes", N->getRawRetainedNodes());
Adrian Prantl1d12b882017-04-26 22:56:44 +00001925 Printer.printMetadata("thrownTypes", N->getRawThrownTypes());
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00001926 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001927}
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00001928
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001929static void writeDILexicalBlock(raw_ostream &Out, const DILexicalBlock *N,
1930 TypePrinting *TypePrinter, SlotTracker *Machine,
1931 const Module *Context) {
1932 Out << "!DILexicalBlock(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001933 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
Duncan P. N. Exon Smith0e202b92015-03-30 16:37:48 +00001934 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
1935 Printer.printMetadata("file", N->getRawFile());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001936 Printer.printInt("line", N->getLine());
1937 Printer.printInt("column", N->getColumn());
Duncan P. N. Exon Smitha96d4092015-02-13 01:29:28 +00001938 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001939}
Duncan P. N. Exon Smitha96d4092015-02-13 01:29:28 +00001940
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001941static void writeDILexicalBlockFile(raw_ostream &Out,
1942 const DILexicalBlockFile *N,
Duncan P. N. Exon Smith06a07022015-02-13 01:30:42 +00001943 TypePrinting *TypePrinter,
1944 SlotTracker *Machine,
1945 const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001946 Out << "!DILexicalBlockFile(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001947 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
Duncan P. N. Exon Smith0e202b92015-03-30 16:37:48 +00001948 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
1949 Printer.printMetadata("file", N->getRawFile());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001950 Printer.printInt("discriminator", N->getDiscriminator(),
1951 /* ShouldSkipZero */ false);
Duncan P. N. Exon Smith06a07022015-02-13 01:30:42 +00001952 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001953}
Duncan P. N. Exon Smith06a07022015-02-13 01:30:42 +00001954
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001955static void writeDINamespace(raw_ostream &Out, const DINamespace *N,
Duncan P. N. Exon Smithe1460002015-02-13 01:32:09 +00001956 TypePrinting *TypePrinter, SlotTracker *Machine,
1957 const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001958 Out << "!DINamespace(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00001959 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1960 Printer.printString("name", N->getName());
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00001961 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
Adrian Prantldbfda632016-11-03 19:42:02 +00001962 Printer.printBool("exportSymbols", N->getExportSymbols(), false);
Duncan P. N. Exon Smithe1460002015-02-13 01:32:09 +00001963 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00001964}
Duncan P. N. Exon Smithe1460002015-02-13 01:32:09 +00001965
Amjad Abouda9bcf162015-12-10 12:56:35 +00001966static void writeDIMacro(raw_ostream &Out, const DIMacro *N,
1967 TypePrinting *TypePrinter, SlotTracker *Machine,
1968 const Module *Context) {
1969 Out << "!DIMacro(";
1970 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1971 Printer.printMacinfoType(N);
1972 Printer.printInt("line", N->getLine());
1973 Printer.printString("name", N->getName());
1974 Printer.printString("value", N->getValue());
1975 Out << ")";
1976}
1977
1978static void writeDIMacroFile(raw_ostream &Out, const DIMacroFile *N,
1979 TypePrinting *TypePrinter, SlotTracker *Machine,
1980 const Module *Context) {
1981 Out << "!DIMacroFile(";
1982 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1983 Printer.printInt("line", N->getLine());
1984 Printer.printMetadata("file", N->getRawFile(), /* ShouldSkipNull */ false);
1985 Printer.printMetadata("nodes", N->getRawElements());
1986 Out << ")";
1987}
1988
Adrian Prantlab1243f2015-06-29 23:03:47 +00001989static void writeDIModule(raw_ostream &Out, const DIModule *N,
1990 TypePrinting *TypePrinter, SlotTracker *Machine,
1991 const Module *Context) {
1992 Out << "!DIModule(";
1993 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1994 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
1995 Printer.printString("name", N->getName());
1996 Printer.printString("configMacros", N->getConfigurationMacros());
1997 Printer.printString("includePath", N->getIncludePath());
1998 Printer.printString("isysroot", N->getISysRoot());
1999 Out << ")";
2000}
2001
2002
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002003static void writeDITemplateTypeParameter(raw_ostream &Out,
2004 const DITemplateTypeParameter *N,
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00002005 TypePrinting *TypePrinter,
2006 SlotTracker *Machine,
2007 const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002008 Out << "!DITemplateTypeParameter(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00002009 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
2010 Printer.printString("name", N->getName());
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00002011 Printer.printMetadata("type", N->getRawType(), /* ShouldSkipNull */ false);
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00002012 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002013}
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00002014
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002015static void writeDITemplateValueParameter(raw_ostream &Out,
2016 const DITemplateValueParameter *N,
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00002017 TypePrinting *TypePrinter,
2018 SlotTracker *Machine,
2019 const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002020 Out << "!DITemplateValueParameter(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00002021 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
Duncan P. N. Exon Smith16d182a2015-02-28 23:21:38 +00002022 if (N->getTag() != dwarf::DW_TAG_template_value_parameter)
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00002023 Printer.printTag(N);
2024 Printer.printString("name", N->getName());
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +00002025 Printer.printMetadata("type", N->getRawType());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00002026 Printer.printMetadata("value", N->getValue(), /* ShouldSkipNull */ false);
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00002027 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002028}
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00002029
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002030static void writeDIGlobalVariable(raw_ostream &Out, const DIGlobalVariable *N,
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00002031 TypePrinting *TypePrinter,
2032 SlotTracker *Machine, const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002033 Out << "!DIGlobalVariable(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00002034 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
2035 Printer.printString("name", N->getName());
2036 Printer.printString("linkageName", N->getLinkageName());
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00002037 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
2038 Printer.printMetadata("file", N->getRawFile());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00002039 Printer.printInt("line", N->getLine());
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00002040 Printer.printMetadata("type", N->getRawType());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00002041 Printer.printBool("isLocal", N->isLocalToUnit());
2042 Printer.printBool("isDefinition", N->isDefinition());
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00002043 Printer.printMetadata("declaration", N->getRawStaticDataMemberDeclaration());
Victor Leschuk2ede1262016-10-20 00:13:12 +00002044 Printer.printInt("align", N->getAlignInBits());
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00002045 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002046}
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00002047
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002048static void writeDILocalVariable(raw_ostream &Out, const DILocalVariable *N,
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00002049 TypePrinting *TypePrinter,
2050 SlotTracker *Machine, const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002051 Out << "!DILocalVariable(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00002052 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00002053 Printer.printString("name", N->getName());
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00002054 Printer.printInt("arg", N->getArg());
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00002055 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
2056 Printer.printMetadata("file", N->getRawFile());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00002057 Printer.printInt("line", N->getLine());
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00002058 Printer.printMetadata("type", N->getRawType());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00002059 Printer.printDIFlags("flags", N->getFlags());
Victor Leschuk2ede1262016-10-20 00:13:12 +00002060 Printer.printInt("align", N->getAlignInBits());
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00002061 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002062}
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00002063
Shiva Chen2c864552018-05-09 02:40:45 +00002064static void writeDILabel(raw_ostream &Out, const DILabel *N,
2065 TypePrinting *TypePrinter,
2066 SlotTracker *Machine, const Module *Context) {
2067 Out << "!DILabel(";
2068 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
2069 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
2070 Printer.printString("name", N->getName());
2071 Printer.printMetadata("file", N->getRawFile());
2072 Printer.printInt("line", N->getLine());
2073 Out << ")";
2074}
2075
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002076static void writeDIExpression(raw_ostream &Out, const DIExpression *N,
Duncan P. N. Exon Smith0c5c0122015-02-13 01:42:09 +00002077 TypePrinting *TypePrinter, SlotTracker *Machine,
2078 const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002079 Out << "!DIExpression(";
Duncan P. N. Exon Smith0c5c0122015-02-13 01:42:09 +00002080 FieldSeparator FS;
2081 if (N->isValid()) {
2082 for (auto I = N->expr_op_begin(), E = N->expr_op_end(); I != E; ++I) {
Mehdi Amini149f6ea2016-10-05 05:59:29 +00002083 auto OpStr = dwarf::OperationEncodingString(I->getOp());
2084 assert(!OpStr.empty() && "Expected valid opcode");
Duncan P. N. Exon Smith0c5c0122015-02-13 01:42:09 +00002085
2086 Out << FS << OpStr;
2087 for (unsigned A = 0, AE = I->getNumArgs(); A != AE; ++A)
2088 Out << FS << I->getArg(A);
2089 }
2090 } else {
2091 for (const auto &I : N->getElements())
2092 Out << FS << I;
2093 }
2094 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002095}
Duncan P. N. Exon Smith0c5c0122015-02-13 01:42:09 +00002096
Adrian Prantlbceaaa92016-12-20 02:09:43 +00002097static void writeDIGlobalVariableExpression(raw_ostream &Out,
2098 const DIGlobalVariableExpression *N,
2099 TypePrinting *TypePrinter,
2100 SlotTracker *Machine,
2101 const Module *Context) {
2102 Out << "!DIGlobalVariableExpression(";
2103 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
2104 Printer.printMetadata("var", N->getVariable());
2105 Printer.printMetadata("expr", N->getExpression());
2106 Out << ")";
2107}
2108
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002109static void writeDIObjCProperty(raw_ostream &Out, const DIObjCProperty *N,
Duncan P. N. Exon Smithd45ce962015-02-13 01:43:22 +00002110 TypePrinting *TypePrinter, SlotTracker *Machine,
2111 const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002112 Out << "!DIObjCProperty(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00002113 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
2114 Printer.printString("name", N->getName());
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00002115 Printer.printMetadata("file", N->getRawFile());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00002116 Printer.printInt("line", N->getLine());
2117 Printer.printString("setter", N->getSetterName());
2118 Printer.printString("getter", N->getGetterName());
2119 Printer.printInt("attributes", N->getAttributes());
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00002120 Printer.printMetadata("type", N->getRawType());
Duncan P. N. Exon Smithd45ce962015-02-13 01:43:22 +00002121 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002122}
Duncan P. N. Exon Smithd45ce962015-02-13 01:43:22 +00002123
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002124static void writeDIImportedEntity(raw_ostream &Out, const DIImportedEntity *N,
Duncan P. N. Exon Smith1c931162015-02-13 01:46:02 +00002125 TypePrinting *TypePrinter,
2126 SlotTracker *Machine, const Module *Context) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002127 Out << "!DIImportedEntity(";
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00002128 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
2129 Printer.printTag(N);
2130 Printer.printString("name", N->getName());
Duncan P. N. Exon Smithf9b47752015-03-30 17:21:38 +00002131 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
2132 Printer.printMetadata("entity", N->getRawEntity());
Adrian Prantld63bfd22017-07-19 00:09:54 +00002133 Printer.printMetadata("file", N->getRawFile());
Duncan P. N. Exon Smith6d267f02015-03-27 00:17:42 +00002134 Printer.printInt("line", N->getLine());
Duncan P. N. Exon Smith1c931162015-02-13 01:46:02 +00002135 Out << ")";
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +00002136}
2137
Duncan P. N. Exon Smitha6de6a42015-01-12 23:45:31 +00002138static void WriteMDNodeBodyInternal(raw_ostream &Out, const MDNode *Node,
2139 TypePrinting *TypePrinter,
2140 SlotTracker *Machine,
2141 const Module *Context) {
Duncan P. N. Exon Smith2bc00f42015-01-19 23:13:14 +00002142 if (Node->isDistinct())
Duncan P. N. Exon Smitha6de6a42015-01-12 23:45:31 +00002143 Out << "distinct ";
Duncan P. N. Exon Smith3d510662015-03-16 21:21:10 +00002144 else if (Node->isTemporary())
2145 Out << "<temporary!> "; // Handle broken code.
Duncan P. N. Exon Smitha6de6a42015-01-12 23:45:31 +00002146
Duncan P. N. Exon Smith2bc00f42015-01-19 23:13:14 +00002147 switch (Node->getMetadataID()) {
Duncan P. N. Exon Smitha6de6a42015-01-12 23:45:31 +00002148 default:
2149 llvm_unreachable("Expected uniquable MDNode");
Duncan P. N. Exon Smith2bc00f42015-01-19 23:13:14 +00002150#define HANDLE_MDNODE_LEAF(CLASS) \
Duncan P. N. Exon Smitha6de6a42015-01-12 23:45:31 +00002151 case Metadata::CLASS##Kind: \
Duncan P. N. Exon Smith2bc00f42015-01-19 23:13:14 +00002152 write##CLASS(Out, cast<CLASS>(Node), TypePrinter, Machine, Context); \
Duncan P. N. Exon Smitha6de6a42015-01-12 23:45:31 +00002153 break;
2154#include "llvm/IR/Metadata.def"
2155 }
2156}
2157
Chandler Carruthd48cdbf2014-01-09 02:29:41 +00002158// Full implementation of printing a Value as an operand with support for
2159// TypePrinting, etc.
Dan Gohman12dad632009-08-12 20:56:03 +00002160static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
Dan Gohman8061d9e2009-08-13 15:27:57 +00002161 TypePrinting *TypePrinter,
Dan Gohmana2489d12010-07-20 23:55:01 +00002162 SlotTracker *Machine,
2163 const Module *Context) {
Chris Lattner033935d2008-08-17 04:40:13 +00002164 if (V->hasName()) {
2165 PrintLLVMName(Out, V);
2166 return;
2167 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002168
Chris Lattner033935d2008-08-17 04:40:13 +00002169 const Constant *CV = dyn_cast<Constant>(V);
2170 if (CV && !isa<GlobalValue>(CV)) {
Dan Gohman8061d9e2009-08-13 15:27:57 +00002171 assert(TypePrinter && "Constants require TypePrinting!");
Dan Gohmana2489d12010-07-20 23:55:01 +00002172 WriteConstantInternal(Out, CV, *TypePrinter, Machine, Context);
Chris Lattner3a36d2c2008-08-19 05:06:27 +00002173 return;
2174 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002175
Chris Lattner3a36d2c2008-08-19 05:06:27 +00002176 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
Chris Lattner033935d2008-08-17 04:40:13 +00002177 Out << "asm ";
2178 if (IA->hasSideEffects())
2179 Out << "sideeffect ";
Dale Johannesen1cfb9582009-10-21 23:28:00 +00002180 if (IA->isAlignStack())
2181 Out << "alignstack ";
Chad Rosierd8c76102012-09-05 19:00:49 +00002182 // We don't emit the AD_ATT dialect as it's the assumed default.
2183 if (IA->getDialect() == InlineAsm::AD_Intel)
2184 Out << "inteldialect ";
Chris Lattner033935d2008-08-17 04:40:13 +00002185 Out << '"';
Jonas Devlieghere745918ff2018-05-31 17:01:42 +00002186 printEscapedString(IA->getAsmString(), Out);
Chris Lattner033935d2008-08-17 04:40:13 +00002187 Out << "\", \"";
Jonas Devlieghere745918ff2018-05-31 17:01:42 +00002188 printEscapedString(IA->getConstraintString(), Out);
Chris Lattner033935d2008-08-17 04:40:13 +00002189 Out << '"';
Chris Lattner3a36d2c2008-08-19 05:06:27 +00002190 return;
2191 }
Devang Patel7428d8a2009-07-22 17:43:22 +00002192
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002193 if (auto *MD = dyn_cast<MetadataAsValue>(V)) {
2194 WriteAsOperandInternal(Out, MD->getMetadata(), TypePrinter, Machine,
2195 Context, /* FromValue */ true);
Devang Patel7428d8a2009-07-22 17:43:22 +00002196 return;
2197 }
2198
Chris Lattner3a36d2c2008-08-19 05:06:27 +00002199 char Prefix = '%';
2200 int Slot;
Chris Lattner5b82a0a2011-08-03 06:15:41 +00002201 // If we have a SlotTracker, use it.
Chris Lattner3a36d2c2008-08-19 05:06:27 +00002202 if (Machine) {
2203 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
2204 Slot = Machine->getGlobalSlot(GV);
2205 Prefix = '@';
2206 } else {
2207 Slot = Machine->getLocalSlot(V);
Andrew Trickec4b6e72011-09-30 19:48:58 +00002208
Chris Lattner5b82a0a2011-08-03 06:15:41 +00002209 // If the local value didn't succeed, then we may be referring to a value
2210 // from a different function. Translate it, as this can happen when using
2211 // address of blocks.
2212 if (Slot == -1)
2213 if ((Machine = createSlotTracker(V))) {
2214 Slot = Machine->getLocalSlot(V);
2215 delete Machine;
2216 }
Chris Lattner3a36d2c2008-08-19 05:06:27 +00002217 }
Chris Lattner5b82a0a2011-08-03 06:15:41 +00002218 } else if ((Machine = createSlotTracker(V))) {
2219 // Otherwise, create one to get the # and then destroy it.
2220 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
2221 Slot = Machine->getGlobalSlot(GV);
2222 Prefix = '@';
Chris Lattnera2d810d2006-01-25 22:26:05 +00002223 } else {
Chris Lattner5b82a0a2011-08-03 06:15:41 +00002224 Slot = Machine->getLocalSlot(V);
Chris Lattnerd84bb632002-04-16 21:36:08 +00002225 }
Chris Lattner5b82a0a2011-08-03 06:15:41 +00002226 delete Machine;
Craig Topperc6207612014-04-09 06:08:46 +00002227 Machine = nullptr;
Chris Lattner5b82a0a2011-08-03 06:15:41 +00002228 } else {
2229 Slot = -1;
Chris Lattnerd84bb632002-04-16 21:36:08 +00002230 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002231
Chris Lattner3a36d2c2008-08-19 05:06:27 +00002232 if (Slot != -1)
2233 Out << Prefix << Slot;
2234 else
2235 Out << "<badref>";
Chris Lattnerd84bb632002-04-16 21:36:08 +00002236}
2237
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002238static void WriteAsOperandInternal(raw_ostream &Out, const Metadata *MD,
2239 TypePrinting *TypePrinter,
2240 SlotTracker *Machine, const Module *Context,
2241 bool FromValue) {
Reid Kleckner6d353342017-08-23 20:31:27 +00002242 // Write DIExpressions inline when used as a value. Improves readability of
2243 // debug info intrinsics.
2244 if (const DIExpression *Expr = dyn_cast<DIExpression>(MD)) {
2245 writeDIExpression(Out, Expr, TypePrinter, Machine, Context);
2246 return;
2247 }
2248
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002249 if (const MDNode *N = dyn_cast<MDNode>(MD)) {
Duncan P. N. Exon Smithf8b3ad62015-06-27 00:15:32 +00002250 std::unique_ptr<SlotTracker> MachineStorage;
2251 if (!Machine) {
2252 MachineStorage = make_unique<SlotTracker>(Context);
2253 Machine = MachineStorage.get();
2254 }
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002255 int Slot = Machine->getMetadataSlot(N);
2256 if (Slot == -1)
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +00002257 // Give the pointer value instead of "badref", since this comes up all
2258 // the time when debugging.
2259 Out << "<" << N << ">";
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002260 else
2261 Out << '!' << Slot;
2262 return;
2263 }
2264
2265 if (const MDString *MDS = dyn_cast<MDString>(MD)) {
2266 Out << "!\"";
Jonas Devlieghere745918ff2018-05-31 17:01:42 +00002267 printEscapedString(MDS->getString(), Out);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002268 Out << '"';
2269 return;
2270 }
2271
2272 auto *V = cast<ValueAsMetadata>(MD);
2273 assert(TypePrinter && "TypePrinter required for metadata values");
2274 assert((FromValue || !isa<LocalAsMetadata>(V)) &&
2275 "Unexpected function-local metadata outside of value argument");
2276
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002277 TypePrinter->print(V->getValue()->getType(), Out);
2278 Out << ' ';
2279 WriteAsOperandInternal(Out, V->getValue(), TypePrinter, Machine, Context);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002280}
2281
Benjamin Kramerba05a782015-03-17 19:53:41 +00002282namespace {
Eugene Zelenkode6cce22017-06-19 22:05:08 +00002283
Benjamin Kramerba05a782015-03-17 19:53:41 +00002284class AssemblyWriter {
2285 formatted_raw_ostream &Out;
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00002286 const Module *TheModule = nullptr;
2287 const ModuleSummaryIndex *TheIndex = nullptr;
Duncan P. N. Exon Smith326921542015-06-26 22:04:20 +00002288 std::unique_ptr<SlotTracker> SlotTrackerStorage;
Benjamin Kramerba05a782015-03-17 19:53:41 +00002289 SlotTracker &Machine;
2290 TypePrinting TypePrinter;
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00002291 AssemblyAnnotationWriter *AnnotationWriter = nullptr;
Benjamin Kramerba05a782015-03-17 19:53:41 +00002292 SetVector<const Comdat *> Comdats;
Justin Bognerd7d1a722015-09-27 22:38:50 +00002293 bool IsForDebug;
Duncan P. N. Exon Smith89010d82015-04-15 01:36:30 +00002294 bool ShouldPreserveUseListOrder;
Benjamin Kramerba05a782015-03-17 19:53:41 +00002295 UseListOrderStack UseListOrders;
Duncan P. N. Exon Smithe30f10e2015-04-24 21:03:05 +00002296 SmallVector<StringRef, 8> MDNames;
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00002297 /// Synchronization scope names registered with LLVMContext.
2298 SmallVector<StringRef, 8> SSNs;
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00002299 DenseMap<const GlobalValueSummary *, GlobalValue::GUID> SummaryToGUIDMap;
Benjamin Kramerba05a782015-03-17 19:53:41 +00002300
2301public:
2302 /// Construct an AssemblyWriter with an external SlotTracker
Duncan P. N. Exon Smith89010d82015-04-15 01:36:30 +00002303 AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac, const Module *M,
Justin Bognerd7d1a722015-09-27 22:38:50 +00002304 AssemblyAnnotationWriter *AAW, bool IsForDebug,
Duncan P. N. Exon Smith89010d82015-04-15 01:36:30 +00002305 bool ShouldPreserveUseListOrder = false);
Benjamin Kramerba05a782015-03-17 19:53:41 +00002306
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00002307 AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac,
2308 const ModuleSummaryIndex *Index, bool IsForDebug);
2309
Benjamin Kramerba05a782015-03-17 19:53:41 +00002310 void printMDNodeBody(const MDNode *MD);
2311 void printNamedMDNode(const NamedMDNode *NMD);
2312
2313 void printModule(const Module *M);
2314
2315 void writeOperand(const Value *Op, bool PrintType);
Reid Kleckner6652a522017-04-28 18:37:16 +00002316 void writeParamOperand(const Value *Operand, AttributeSet Attrs);
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002317 void writeOperandBundles(ImmutableCallSite CS);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00002318 void writeSyncScope(const LLVMContext &Context,
2319 SyncScope::ID SSID);
2320 void writeAtomic(const LLVMContext &Context,
2321 AtomicOrdering Ordering,
2322 SyncScope::ID SSID);
2323 void writeAtomicCmpXchg(const LLVMContext &Context,
2324 AtomicOrdering SuccessOrdering,
Benjamin Kramerba05a782015-03-17 19:53:41 +00002325 AtomicOrdering FailureOrdering,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00002326 SyncScope::ID SSID);
Benjamin Kramerba05a782015-03-17 19:53:41 +00002327
2328 void writeAllMDNodes();
2329 void writeMDNode(unsigned Slot, const MDNode *Node);
2330 void writeAllAttributeGroups();
2331
2332 void printTypeIdentities();
2333 void printGlobal(const GlobalVariable *GV);
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +00002334 void printIndirectSymbol(const GlobalIndirectSymbol *GIS);
Benjamin Kramerba05a782015-03-17 19:53:41 +00002335 void printComdat(const Comdat *C);
2336 void printFunction(const Function *F);
Reid Kleckner6652a522017-04-28 18:37:16 +00002337 void printArgument(const Argument *FA, AttributeSet Attrs);
Benjamin Kramerba05a782015-03-17 19:53:41 +00002338 void printBasicBlock(const BasicBlock *BB);
2339 void printInstructionLine(const Instruction &I);
2340 void printInstruction(const Instruction &I);
2341
2342 void printUseListOrder(const UseListOrder &Order);
2343 void printUseLists(const Function *F);
2344
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00002345 void printModuleSummaryIndex();
2346 void printSummaryInfo(unsigned Slot, const ValueInfo &VI);
2347 void printSummary(const GlobalValueSummary &Summary);
2348 void printAliasSummary(const AliasSummary *AS);
2349 void printGlobalVarSummary(const GlobalVarSummary *GS);
2350 void printFunctionSummary(const FunctionSummary *FS);
2351 void printTypeIdSummary(const TypeIdSummary &TIS);
2352 void printTypeTestResolution(const TypeTestResolution &TTRes);
2353 void printArgs(const std::vector<uint64_t> &Args);
2354 void printWPDRes(const WholeProgramDevirtResolution &WPDRes);
2355 void printTypeIdInfo(const FunctionSummary::TypeIdInfo &TIDInfo);
2356 void printVFuncId(const FunctionSummary::VFuncId VFId);
2357 void
2358 printNonConstVCalls(const std::vector<FunctionSummary::VFuncId> VCallList,
2359 const char *Tag);
2360 void
2361 printConstVCalls(const std::vector<FunctionSummary::ConstVCall> VCallList,
2362 const char *Tag);
2363
Benjamin Kramerba05a782015-03-17 19:53:41 +00002364private:
Adrian Prantl5f8f34e42018-05-01 15:54:18 +00002365 /// Print out metadata attachments.
Duncan P. N. Exon Smith86979272015-04-24 20:59:52 +00002366 void printMetadataAttachments(
Duncan P. N. Exon Smithd3e4c2a2015-04-24 21:06:21 +00002367 const SmallVectorImpl<std::pair<unsigned, MDNode *>> &MDs,
2368 StringRef Separator);
Duncan P. N. Exon Smith86979272015-04-24 20:59:52 +00002369
Benjamin Kramerba05a782015-03-17 19:53:41 +00002370 // printInfoComment - Print a little comment after the instruction indicating
2371 // which slot it occupies.
2372 void printInfoComment(const Value &V);
Igor Laevsky2aa8caf2015-05-05 13:20:42 +00002373
2374 // printGCRelocateComment - print comment after call to the gc.relocate
2375 // intrinsic indicating base and derived pointer names.
Manuel Jacob83eefa62016-01-05 04:03:00 +00002376 void printGCRelocateComment(const GCRelocateInst &Relocate);
Benjamin Kramerba05a782015-03-17 19:53:41 +00002377};
Eugene Zelenkode6cce22017-06-19 22:05:08 +00002378
2379} // end anonymous namespace
Benjamin Kramerba05a782015-03-17 19:53:41 +00002380
Justin Bogner0ffea9d2015-09-02 22:46:15 +00002381AssemblyWriter::AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac,
2382 const Module *M, AssemblyAnnotationWriter *AAW,
Justin Bognerd7d1a722015-09-27 22:38:50 +00002383 bool IsForDebug, bool ShouldPreserveUseListOrder)
Roman Tereshind96de6f2018-03-22 21:29:07 +00002384 : Out(o), TheModule(M), Machine(Mac), TypePrinter(M), AnnotationWriter(AAW),
Justin Bognerd7d1a722015-09-27 22:38:50 +00002385 IsForDebug(IsForDebug),
Justin Bogner0ffea9d2015-09-02 22:46:15 +00002386 ShouldPreserveUseListOrder(ShouldPreserveUseListOrder) {
David Majnemerdad0a642014-06-27 18:19:56 +00002387 if (!TheModule)
2388 return;
Peter Collingbourne6d88fde2016-06-22 20:29:42 +00002389 for (const GlobalObject &GO : TheModule->global_objects())
2390 if (const Comdat *C = GO.getComdat())
David Majnemerdad0a642014-06-27 18:19:56 +00002391 Comdats.insert(C);
Daniel Maleaded9f932013-05-08 20:38:31 +00002392}
Chris Lattner2e9fee42001-07-12 23:35:26 +00002393
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00002394AssemblyWriter::AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac,
2395 const ModuleSummaryIndex *Index, bool IsForDebug)
2396 : Out(o), TheIndex(Index), Machine(Mac), TypePrinter(/*Module=*/nullptr),
2397 IsForDebug(IsForDebug), ShouldPreserveUseListOrder(false) {}
2398
Chris Lattner78e2e8b2006-12-06 06:24:27 +00002399void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType) {
Craig Topperc6207612014-04-09 06:08:46 +00002400 if (!Operand) {
Chris Lattner08f7d0c2005-02-24 16:58:29 +00002401 Out << "<null operand!>";
Chris Lattnerb419a1e2009-12-31 02:33:14 +00002402 return;
Chris Lattner08f7d0c2005-02-24 16:58:29 +00002403 }
Chris Lattnerb419a1e2009-12-31 02:33:14 +00002404 if (PrintType) {
2405 TypePrinter.print(Operand->getType(), Out);
2406 Out << ' ';
2407 }
Dan Gohmana2489d12010-07-20 23:55:01 +00002408 WriteAsOperandInternal(Out, Operand, &TypePrinter, &Machine, TheModule);
Chris Lattner2f7c9632001-06-06 20:29:01 +00002409}
2410
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00002411void AssemblyWriter::writeSyncScope(const LLVMContext &Context,
2412 SyncScope::ID SSID) {
2413 switch (SSID) {
2414 case SyncScope::System: {
2415 break;
2416 }
2417 default: {
2418 if (SSNs.empty())
2419 Context.getSyncScopeNames(SSNs);
2420
2421 Out << " syncscope(\"";
Jonas Devlieghere745918ff2018-05-31 17:01:42 +00002422 printEscapedString(SSNs[SSID], Out);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00002423 Out << "\")";
2424 break;
2425 }
2426 }
2427}
2428
2429void AssemblyWriter::writeAtomic(const LLVMContext &Context,
2430 AtomicOrdering Ordering,
2431 SyncScope::ID SSID) {
JF Bastien800f87a2016-04-06 21:19:33 +00002432 if (Ordering == AtomicOrdering::NotAtomic)
Eli Friedmanfee02c62011-07-25 23:16:38 +00002433 return;
2434
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00002435 writeSyncScope(Context, SSID);
JF Bastien800f87a2016-04-06 21:19:33 +00002436 Out << " " << toIRString(Ordering);
Eli Friedmanfee02c62011-07-25 23:16:38 +00002437}
2438
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00002439void AssemblyWriter::writeAtomicCmpXchg(const LLVMContext &Context,
2440 AtomicOrdering SuccessOrdering,
Tim Northovere94a5182014-03-11 10:48:52 +00002441 AtomicOrdering FailureOrdering,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00002442 SyncScope::ID SSID) {
JF Bastien800f87a2016-04-06 21:19:33 +00002443 assert(SuccessOrdering != AtomicOrdering::NotAtomic &&
2444 FailureOrdering != AtomicOrdering::NotAtomic);
Tim Northovere94a5182014-03-11 10:48:52 +00002445
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00002446 writeSyncScope(Context, SSID);
JF Bastien800f87a2016-04-06 21:19:33 +00002447 Out << " " << toIRString(SuccessOrdering);
2448 Out << " " << toIRString(FailureOrdering);
Tim Northovere94a5182014-03-11 10:48:52 +00002449}
2450
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002451void AssemblyWriter::writeParamOperand(const Value *Operand,
Reid Kleckner6652a522017-04-28 18:37:16 +00002452 AttributeSet Attrs) {
Craig Topperc6207612014-04-09 06:08:46 +00002453 if (!Operand) {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00002454 Out << "<null operand!>";
Chris Lattnerb419a1e2009-12-31 02:33:14 +00002455 return;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00002456 }
Chris Lattnerb419a1e2009-12-31 02:33:14 +00002457
2458 // Print the type
2459 TypePrinter.print(Operand->getType(), Out);
2460 // Print parameter attributes list
Reid Kleckner6652a522017-04-28 18:37:16 +00002461 if (Attrs.hasAttributes())
2462 Out << ' ' << Attrs.getAsString();
Chris Lattnerb419a1e2009-12-31 02:33:14 +00002463 Out << ' ';
2464 // Print the operand
Dan Gohmana2489d12010-07-20 23:55:01 +00002465 WriteAsOperandInternal(Out, Operand, &TypePrinter, &Machine, TheModule);
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00002466}
Chris Lattner2f7c9632001-06-06 20:29:01 +00002467
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002468void AssemblyWriter::writeOperandBundles(ImmutableCallSite CS) {
2469 if (!CS.hasOperandBundles())
2470 return;
2471
2472 Out << " [ ";
2473
2474 bool FirstBundle = true;
2475 for (unsigned i = 0, e = CS.getNumOperandBundles(); i != e; ++i) {
Sanjoy Das54c3ca62015-11-07 01:56:04 +00002476 OperandBundleUse BU = CS.getOperandBundleAt(i);
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002477
2478 if (!FirstBundle)
2479 Out << ", ";
2480 FirstBundle = false;
2481
2482 Out << '"';
Jonas Devlieghere745918ff2018-05-31 17:01:42 +00002483 printEscapedString(BU.getTagName(), Out);
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002484 Out << '"';
2485
2486 Out << '(';
2487
2488 bool FirstInput = true;
2489 for (const auto &Input : BU.Inputs) {
2490 if (!FirstInput)
2491 Out << ", ";
2492 FirstInput = false;
2493
2494 TypePrinter.print(Input->getType(), Out);
2495 Out << " ";
2496 WriteAsOperandInternal(Out, Input, &TypePrinter, &Machine, TheModule);
2497 }
2498
2499 Out << ')';
2500 }
2501
2502 Out << " ]";
2503}
2504
Chris Lattner7bfee412001-10-29 16:05:51 +00002505void AssemblyWriter::printModule(const Module *M) {
Teresa Johnsonfc801f52018-07-03 15:52:57 +00002506 Machine.initializeIfNeeded();
Bill Wendling829b4782013-02-11 08:43:33 +00002507
Duncan P. N. Exon Smith89010d82015-04-15 01:36:30 +00002508 if (ShouldPreserveUseListOrder)
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +00002509 UseListOrders = predictUseListOrder(M);
2510
Chris Lattner4d8689e2005-03-02 23:12:40 +00002511 if (!M->getModuleIdentifier().empty() &&
Misha Brukmanb1c93172005-04-21 23:48:37 +00002512 // Don't print the ID if it will start a new line (which would
Chris Lattner4d8689e2005-03-02 23:12:40 +00002513 // require a comment char before it).
2514 M->getModuleIdentifier().find('\n') == std::string::npos)
2515 Out << "; ModuleID = '" << M->getModuleIdentifier() << "'\n";
2516
Teresa Johnson83c517c2016-03-30 18:15:08 +00002517 if (!M->getSourceFileName().empty()) {
Teresa Johnsond8d94652016-03-30 22:17:28 +00002518 Out << "source_filename = \"";
Jonas Devlieghere745918ff2018-05-31 17:01:42 +00002519 printEscapedString(M->getSourceFileName(), Out);
Teresa Johnsond8d94652016-03-30 22:17:28 +00002520 Out << "\"\n";
Teresa Johnson83c517c2016-03-30 18:15:08 +00002521 }
2522
Rafael Espindolaf863ee22014-02-25 20:01:08 +00002523 const std::string &DL = M->getDataLayoutStr();
2524 if (!DL.empty())
2525 Out << "target datalayout = \"" << DL << "\"\n";
Reid Spencer48f98c82004-07-25 21:44:54 +00002526 if (!M->getTargetTriple().empty())
Reid Spencerffec7df2004-07-25 21:29:43 +00002527 Out << "target triple = \"" << M->getTargetTriple() << "\"\n";
Misha Brukmanb1c93172005-04-21 23:48:37 +00002528
Chris Lattnereef2fe72006-01-24 04:13:11 +00002529 if (!M->getModuleInlineAsm().empty()) {
Dan Gohman69273e62009-08-12 23:54:22 +00002530 Out << '\n';
Benjamin Kramerbbd05a22015-06-07 13:59:33 +00002531
2532 // Split the string into lines, to make it easier to read the .ll file.
Benjamin Kramerf1cfc422015-06-08 18:58:57 +00002533 StringRef Asm = M->getModuleInlineAsm();
Benjamin Kramerbbd05a22015-06-07 13:59:33 +00002534 do {
2535 StringRef Front;
2536 std::tie(Front, Asm) = Asm.split('\n');
2537
Chris Lattnerefaf35d2006-01-24 00:45:30 +00002538 // We found a newline, print the portion of the asm string from the
2539 // last newline up to this newline.
2540 Out << "module asm \"";
Jonas Devlieghere745918ff2018-05-31 17:01:42 +00002541 printEscapedString(Front, Out);
Chris Lattnerefaf35d2006-01-24 00:45:30 +00002542 Out << "\"\n";
Benjamin Kramerbbd05a22015-06-07 13:59:33 +00002543 } while (!Asm.empty());
Chris Lattner6ed87bd2006-01-23 23:03:36 +00002544 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002545
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002546 printTypeIdentities();
Misha Brukmanb1c93172005-04-21 23:48:37 +00002547
David Majnemerdad0a642014-06-27 18:19:56 +00002548 // Output all comdats.
2549 if (!Comdats.empty())
2550 Out << '\n';
2551 for (const Comdat *C : Comdats) {
2552 printComdat(C);
2553 if (C != Comdats.back())
2554 Out << '\n';
2555 }
2556
Dan Gohman69273e62009-08-12 23:54:22 +00002557 // Output all globals.
2558 if (!M->global_empty()) Out << '\n';
Yaron Kerenf3cf9d12015-06-13 17:50:47 +00002559 for (const GlobalVariable &GV : M->globals()) {
2560 printGlobal(&GV); Out << '\n';
Duncan Sands66fc0e62012-09-12 09:55:51 +00002561 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002562
Chris Lattnerd2747052007-04-26 02:24:10 +00002563 // Output all aliases.
2564 if (!M->alias_empty()) Out << "\n";
Yaron Kerenf3cf9d12015-06-13 17:50:47 +00002565 for (const GlobalAlias &GA : M->aliases())
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +00002566 printIndirectSymbol(&GA);
Chris Lattnerfee714f2001-09-07 16:36:04 +00002567
Dmitry Polukhina1feff72016-04-07 12:32:19 +00002568 // Output all ifuncs.
2569 if (!M->ifunc_empty()) Out << "\n";
2570 for (const GlobalIFunc &GI : M->ifuncs())
2571 printIndirectSymbol(&GI);
2572
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +00002573 // Output global use-lists.
2574 printUseLists(nullptr);
2575
Chris Lattner2cdd49d2004-09-14 05:06:58 +00002576 // Output all of the functions.
Yaron Kerenf3cf9d12015-06-13 17:50:47 +00002577 for (const Function &F : *M)
2578 printFunction(&F);
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +00002579 assert(UseListOrders.empty() && "All use-lists should have been consumed");
Devang Patel983c6b12009-07-08 21:44:25 +00002580
Bill Wendling829b4782013-02-11 08:43:33 +00002581 // Output all attribute groups.
Bill Wendling04945972013-04-29 23:48:06 +00002582 if (!Machine.as_empty()) {
Bill Wendling829b4782013-02-11 08:43:33 +00002583 Out << '\n';
2584 writeAllAttributeGroups();
2585 }
2586
Devang Patel23e68302009-07-29 22:04:47 +00002587 // Output named metadata.
Dan Gohman69273e62009-08-12 23:54:22 +00002588 if (!M->named_metadata_empty()) Out << '\n';
Andrew Trickec4b6e72011-09-30 19:48:58 +00002589
Yaron Kerenf3cf9d12015-06-13 17:50:47 +00002590 for (const NamedMDNode &Node : M->named_metadata())
2591 printNamedMDNode(&Node);
Devang Patel23e68302009-07-29 22:04:47 +00002592
2593 // Output metadata.
Chris Lattnercf4a76e2009-12-31 02:20:11 +00002594 if (!Machine.mdn_empty()) {
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002595 Out << '\n';
2596 writeAllMDNodes();
2597 }
Chris Lattnerfee714f2001-09-07 16:36:04 +00002598}
2599
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00002600void AssemblyWriter::printModuleSummaryIndex() {
2601 assert(TheIndex);
Teresa Johnsonfc801f52018-07-03 15:52:57 +00002602 Machine.initializeIndexIfNeeded();
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00002603
2604 Out << "\n";
2605
Teresa Johnson8fc76662018-07-02 22:09:23 +00002606 // Print module path entries. To print in order, add paths to a vector
2607 // indexed by module slot.
Teresa Johnson724e7a12018-05-26 02:53:52 +00002608 std::vector<std::pair<std::string, ModuleHash>> moduleVec;
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00002609 std::string RegularLTOModuleName = "[Regular LTO]";
2610 moduleVec.resize(TheIndex->modulePaths().size());
Teresa Johnson8fc76662018-07-02 22:09:23 +00002611 for (auto &ModPath : TheIndex->modulePaths())
2612 moduleVec[Machine.getModulePathSlot(ModPath.first())] = std::make_pair(
2613 // A module id of -1 is a special entry for a regular LTO module created
2614 // during the thin link.
2615 ModPath.second.first == -1u ? RegularLTOModuleName
2616 : (std::string)ModPath.first(),
2617 ModPath.second.second);
2618
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00002619 unsigned i = 0;
2620 for (auto &ModPair : moduleVec) {
2621 Out << "^" << i++ << " = module: (";
Andrew Ngac305e12018-07-12 14:40:21 +00002622 Out << "path: \"";
2623 printEscapedString(ModPair.first, Out);
2624 Out << "\", hash: (";
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00002625 FieldSeparator FS;
2626 for (auto Hash : ModPair.second)
2627 Out << FS << Hash;
2628 Out << "))\n";
2629 }
2630
2631 // FIXME: Change AliasSummary to hold a ValueInfo instead of summary pointer
2632 // for aliasee (then update BitcodeWriter.cpp and remove get/setAliaseeGUID).
2633 for (auto &GlobalList : *TheIndex) {
2634 auto GUID = GlobalList.first;
2635 for (auto &Summary : GlobalList.second.SummaryList)
2636 SummaryToGUIDMap[Summary.get()] = GUID;
2637 }
2638
2639 // Print the global value summary entries.
2640 for (auto &GlobalList : *TheIndex) {
2641 auto GUID = GlobalList.first;
2642 auto VI = TheIndex->getValueInfo(GlobalList);
2643 printSummaryInfo(Machine.getGUIDSlot(GUID), VI);
2644 }
2645
2646 // Print the TypeIdMap entries.
2647 for (auto &TId : TheIndex->typeIds()) {
2648 auto GUID = GlobalValue::getGUID(TId.first);
2649 Out << "^" << Machine.getGUIDSlot(GUID) << " = typeid: (name: \""
2650 << TId.first << "\"";
2651 printTypeIdSummary(TId.second);
2652 Out << ") ; guid = " << GUID << "\n";
2653 }
2654}
2655
2656static const char *
2657getWholeProgDevirtResKindName(WholeProgramDevirtResolution::Kind K) {
2658 switch (K) {
2659 case WholeProgramDevirtResolution::Indir:
2660 return "indir";
2661 case WholeProgramDevirtResolution::SingleImpl:
2662 return "singleImpl";
2663 case WholeProgramDevirtResolution::BranchFunnel:
2664 return "branchFunnel";
2665 }
2666 llvm_unreachable("invalid WholeProgramDevirtResolution kind");
2667}
2668
2669static const char *getWholeProgDevirtResByArgKindName(
2670 WholeProgramDevirtResolution::ByArg::Kind K) {
2671 switch (K) {
2672 case WholeProgramDevirtResolution::ByArg::Indir:
2673 return "indir";
2674 case WholeProgramDevirtResolution::ByArg::UniformRetVal:
2675 return "uniformRetVal";
2676 case WholeProgramDevirtResolution::ByArg::UniqueRetVal:
2677 return "uniqueRetVal";
2678 case WholeProgramDevirtResolution::ByArg::VirtualConstProp:
2679 return "virtualConstProp";
2680 }
2681 llvm_unreachable("invalid WholeProgramDevirtResolution::ByArg kind");
2682}
2683
2684static const char *getTTResKindName(TypeTestResolution::Kind K) {
2685 switch (K) {
2686 case TypeTestResolution::Unsat:
2687 return "unsat";
2688 case TypeTestResolution::ByteArray:
2689 return "byteArray";
2690 case TypeTestResolution::Inline:
2691 return "inline";
2692 case TypeTestResolution::Single:
2693 return "single";
2694 case TypeTestResolution::AllOnes:
2695 return "allOnes";
2696 }
2697 llvm_unreachable("invalid TypeTestResolution kind");
2698}
2699
2700void AssemblyWriter::printTypeTestResolution(const TypeTestResolution &TTRes) {
2701 Out << "typeTestRes: (kind: " << getTTResKindName(TTRes.TheKind)
2702 << ", sizeM1BitWidth: " << TTRes.SizeM1BitWidth;
2703
2704 // The following fields are only used if the target does not support the use
2705 // of absolute symbols to store constants. Print only if non-zero.
2706 if (TTRes.AlignLog2)
2707 Out << ", alignLog2: " << TTRes.AlignLog2;
2708 if (TTRes.SizeM1)
2709 Out << ", sizeM1: " << TTRes.SizeM1;
2710 if (TTRes.BitMask)
2711 // BitMask is uint8_t which causes it to print the corresponding char.
2712 Out << ", bitMask: " << (unsigned)TTRes.BitMask;
2713 if (TTRes.InlineBits)
2714 Out << ", inlineBits: " << TTRes.InlineBits;
2715
2716 Out << ")";
2717}
2718
2719void AssemblyWriter::printTypeIdSummary(const TypeIdSummary &TIS) {
2720 Out << ", summary: (";
2721 printTypeTestResolution(TIS.TTRes);
2722 if (!TIS.WPDRes.empty()) {
2723 Out << ", wpdResolutions: (";
2724 FieldSeparator FS;
2725 for (auto &WPDRes : TIS.WPDRes) {
2726 Out << FS;
2727 Out << "(offset: " << WPDRes.first << ", ";
2728 printWPDRes(WPDRes.second);
2729 Out << ")";
2730 }
2731 Out << ")";
2732 }
2733 Out << ")";
2734}
2735
2736void AssemblyWriter::printArgs(const std::vector<uint64_t> &Args) {
2737 Out << "args: (";
2738 FieldSeparator FS;
2739 for (auto arg : Args) {
2740 Out << FS;
2741 Out << arg;
2742 }
2743 Out << ")";
2744}
2745
2746void AssemblyWriter::printWPDRes(const WholeProgramDevirtResolution &WPDRes) {
2747 Out << "wpdRes: (kind: ";
2748 Out << getWholeProgDevirtResKindName(WPDRes.TheKind);
2749
2750 if (WPDRes.TheKind == WholeProgramDevirtResolution::SingleImpl)
2751 Out << ", singleImplName: \"" << WPDRes.SingleImplName << "\"";
2752
2753 if (!WPDRes.ResByArg.empty()) {
2754 Out << ", resByArg: (";
2755 FieldSeparator FS;
2756 for (auto &ResByArg : WPDRes.ResByArg) {
2757 Out << FS;
2758 printArgs(ResByArg.first);
2759 Out << ", byArg: (kind: ";
2760 Out << getWholeProgDevirtResByArgKindName(ResByArg.second.TheKind);
2761 if (ResByArg.second.TheKind ==
2762 WholeProgramDevirtResolution::ByArg::UniformRetVal ||
2763 ResByArg.second.TheKind ==
2764 WholeProgramDevirtResolution::ByArg::UniqueRetVal)
2765 Out << ", info: " << ResByArg.second.Info;
2766
2767 // The following fields are only used if the target does not support the
2768 // use of absolute symbols to store constants. Print only if non-zero.
2769 if (ResByArg.second.Byte || ResByArg.second.Bit)
2770 Out << ", byte: " << ResByArg.second.Byte
2771 << ", bit: " << ResByArg.second.Bit;
2772
2773 Out << ")";
2774 }
2775 Out << ")";
2776 }
2777 Out << ")";
2778}
2779
2780static const char *getSummaryKindName(GlobalValueSummary::SummaryKind SK) {
2781 switch (SK) {
2782 case GlobalValueSummary::AliasKind:
2783 return "alias";
2784 case GlobalValueSummary::FunctionKind:
2785 return "function";
2786 case GlobalValueSummary::GlobalVarKind:
2787 return "variable";
2788 }
2789 llvm_unreachable("invalid summary kind");
2790}
2791
2792void AssemblyWriter::printAliasSummary(const AliasSummary *AS) {
Teresa Johnsonf8182f12018-07-03 01:11:43 +00002793 Out << ", aliasee: ";
2794 // The indexes emitted for distributed backends may not include the
2795 // aliasee summary (only if it is being imported directly). Handle
2796 // that case by just emitting "null" as the aliasee.
2797 if (AS->hasAliasee())
2798 Out << "^" << Machine.getGUIDSlot(SummaryToGUIDMap[&AS->getAliasee()]);
2799 else
2800 Out << "null";
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00002801}
2802
2803void AssemblyWriter::printGlobalVarSummary(const GlobalVarSummary *GS) {
2804 // Nothing for now
2805}
2806
Teresa Johnsonc80c8732018-05-25 15:15:39 +00002807static std::string getLinkageName(GlobalValue::LinkageTypes LT) {
2808 switch (LT) {
2809 case GlobalValue::ExternalLinkage:
2810 return "external";
2811 case GlobalValue::PrivateLinkage:
2812 return "private";
2813 case GlobalValue::InternalLinkage:
2814 return "internal";
2815 case GlobalValue::LinkOnceAnyLinkage:
2816 return "linkonce";
2817 case GlobalValue::LinkOnceODRLinkage:
2818 return "linkonce_odr";
2819 case GlobalValue::WeakAnyLinkage:
2820 return "weak";
2821 case GlobalValue::WeakODRLinkage:
2822 return "weak_odr";
2823 case GlobalValue::CommonLinkage:
2824 return "common";
2825 case GlobalValue::AppendingLinkage:
2826 return "appending";
2827 case GlobalValue::ExternalWeakLinkage:
2828 return "extern_weak";
2829 case GlobalValue::AvailableExternallyLinkage:
2830 return "available_externally";
2831 }
2832 llvm_unreachable("invalid linkage");
2833}
2834
2835// When printing the linkage types in IR where the ExternalLinkage is
2836// not printed, and other linkage types are expected to be printed with
2837// a space after the name.
2838static std::string getLinkageNameWithSpace(GlobalValue::LinkageTypes LT) {
2839 if (LT == GlobalValue::ExternalLinkage)
2840 return "";
2841 return getLinkageName(LT) + " ";
2842}
2843
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00002844static const char *getHotnessName(CalleeInfo::HotnessType HT) {
2845 switch (HT) {
2846 case CalleeInfo::HotnessType::Unknown:
2847 return "unknown";
2848 case CalleeInfo::HotnessType::Cold:
2849 return "cold";
2850 case CalleeInfo::HotnessType::None:
2851 return "none";
2852 case CalleeInfo::HotnessType::Hot:
2853 return "hot";
2854 case CalleeInfo::HotnessType::Critical:
2855 return "critical";
2856 }
2857 llvm_unreachable("invalid hotness");
2858}
2859
2860void AssemblyWriter::printFunctionSummary(const FunctionSummary *FS) {
2861 Out << ", insts: " << FS->instCount();
2862
2863 FunctionSummary::FFlags FFlags = FS->fflags();
2864 if (FFlags.ReadNone | FFlags.ReadOnly | FFlags.NoRecurse |
2865 FFlags.ReturnDoesNotAlias) {
2866 Out << ", funcFlags: (";
2867 Out << "readNone: " << FFlags.ReadNone;
2868 Out << ", readOnly: " << FFlags.ReadOnly;
2869 Out << ", noRecurse: " << FFlags.NoRecurse;
2870 Out << ", returnDoesNotAlias: " << FFlags.ReturnDoesNotAlias;
2871 Out << ")";
2872 }
2873 if (!FS->calls().empty()) {
2874 Out << ", calls: (";
2875 FieldSeparator IFS;
2876 for (auto &Call : FS->calls()) {
2877 Out << IFS;
2878 Out << "(callee: ^" << Machine.getGUIDSlot(Call.first.getGUID());
2879 if (Call.second.getHotness() != CalleeInfo::HotnessType::Unknown)
2880 Out << ", hotness: " << getHotnessName(Call.second.getHotness());
2881 else if (Call.second.RelBlockFreq)
2882 Out << ", relbf: " << Call.second.RelBlockFreq;
2883 Out << ")";
2884 }
2885 Out << ")";
2886 }
2887
2888 if (const auto *TIdInfo = FS->getTypeIdInfo())
2889 printTypeIdInfo(*TIdInfo);
2890}
2891
2892void AssemblyWriter::printTypeIdInfo(
2893 const FunctionSummary::TypeIdInfo &TIDInfo) {
2894 Out << ", typeIdInfo: (";
2895 FieldSeparator TIDFS;
2896 if (!TIDInfo.TypeTests.empty()) {
2897 Out << TIDFS;
2898 Out << "typeTests: (";
2899 FieldSeparator FS;
2900 for (auto &GUID : TIDInfo.TypeTests) {
2901 Out << FS;
2902 auto Slot = Machine.getGUIDSlot(GUID);
2903 if (Slot != -1)
2904 Out << "^" << Slot;
2905 else
2906 Out << GUID;
2907 }
2908 Out << ")";
2909 }
2910 if (!TIDInfo.TypeTestAssumeVCalls.empty()) {
2911 Out << TIDFS;
2912 printNonConstVCalls(TIDInfo.TypeTestAssumeVCalls, "typeTestAssumeVCalls");
2913 }
2914 if (!TIDInfo.TypeCheckedLoadVCalls.empty()) {
2915 Out << TIDFS;
2916 printNonConstVCalls(TIDInfo.TypeCheckedLoadVCalls, "typeCheckedLoadVCalls");
2917 }
2918 if (!TIDInfo.TypeTestAssumeConstVCalls.empty()) {
2919 Out << TIDFS;
2920 printConstVCalls(TIDInfo.TypeTestAssumeConstVCalls,
2921 "typeTestAssumeConstVCalls");
2922 }
2923 if (!TIDInfo.TypeCheckedLoadConstVCalls.empty()) {
2924 Out << TIDFS;
2925 printConstVCalls(TIDInfo.TypeCheckedLoadConstVCalls,
2926 "typeCheckedLoadConstVCalls");
2927 }
2928 Out << ")";
2929}
2930
2931void AssemblyWriter::printVFuncId(const FunctionSummary::VFuncId VFId) {
2932 Out << "vFuncId: (";
2933 auto Slot = Machine.getGUIDSlot(VFId.GUID);
2934 if (Slot != -1)
2935 Out << "^" << Slot;
2936 else
2937 Out << "guid: " << VFId.GUID;
2938 Out << ", offset: " << VFId.Offset;
2939 Out << ")";
2940}
2941
2942void AssemblyWriter::printNonConstVCalls(
2943 const std::vector<FunctionSummary::VFuncId> VCallList, const char *Tag) {
2944 Out << Tag << ": (";
2945 FieldSeparator FS;
2946 for (auto &VFuncId : VCallList) {
2947 Out << FS;
2948 printVFuncId(VFuncId);
2949 }
2950 Out << ")";
2951}
2952
2953void AssemblyWriter::printConstVCalls(
2954 const std::vector<FunctionSummary::ConstVCall> VCallList, const char *Tag) {
2955 Out << Tag << ": (";
2956 FieldSeparator FS;
2957 for (auto &ConstVCall : VCallList) {
2958 Out << FS;
Teresa Johnsonc7816802018-08-14 01:49:33 +00002959 Out << "(";
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00002960 printVFuncId(ConstVCall.VFunc);
2961 if (!ConstVCall.Args.empty()) {
2962 Out << ", ";
2963 printArgs(ConstVCall.Args);
2964 }
Teresa Johnsonc7816802018-08-14 01:49:33 +00002965 Out << ")";
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00002966 }
2967 Out << ")";
2968}
2969
2970void AssemblyWriter::printSummary(const GlobalValueSummary &Summary) {
2971 GlobalValueSummary::GVFlags GVFlags = Summary.flags();
2972 GlobalValue::LinkageTypes LT = (GlobalValue::LinkageTypes)GVFlags.Linkage;
2973 Out << getSummaryKindName(Summary.getSummaryKind()) << ": ";
Teresa Johnson8fc76662018-07-02 22:09:23 +00002974 Out << "(module: ^" << Machine.getModulePathSlot(Summary.modulePath())
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00002975 << ", flags: (";
2976 Out << "linkage: " << getLinkageName(LT);
2977 Out << ", notEligibleToImport: " << GVFlags.NotEligibleToImport;
2978 Out << ", live: " << GVFlags.Live;
2979 Out << ", dsoLocal: " << GVFlags.DSOLocal;
2980 Out << ")";
2981
2982 if (Summary.getSummaryKind() == GlobalValueSummary::AliasKind)
2983 printAliasSummary(cast<AliasSummary>(&Summary));
2984 else if (Summary.getSummaryKind() == GlobalValueSummary::FunctionKind)
2985 printFunctionSummary(cast<FunctionSummary>(&Summary));
2986 else
2987 printGlobalVarSummary(cast<GlobalVarSummary>(&Summary));
2988
2989 auto RefList = Summary.refs();
2990 if (!RefList.empty()) {
2991 Out << ", refs: (";
2992 FieldSeparator FS;
2993 for (auto &Ref : RefList) {
2994 Out << FS;
2995 Out << "^" << Machine.getGUIDSlot(Ref.getGUID());
2996 }
2997 Out << ")";
2998 }
2999
3000 Out << ")";
3001}
3002
3003void AssemblyWriter::printSummaryInfo(unsigned Slot, const ValueInfo &VI) {
3004 Out << "^" << Slot << " = gv: (";
3005 if (!VI.name().empty())
3006 Out << "name: \"" << VI.name() << "\"";
3007 else
3008 Out << "guid: " << VI.getGUID();
3009 if (!VI.getSummaryList().empty()) {
3010 Out << ", summaries: (";
3011 FieldSeparator FS;
3012 for (auto &Summary : VI.getSummaryList()) {
3013 Out << FS;
3014 printSummary(*Summary);
3015 }
3016 Out << ")";
3017 }
3018 Out << ")";
3019 if (!VI.name().empty())
3020 Out << " ; guid = " << VI.getGUID();
3021 Out << "\n";
3022}
3023
Filipe Cabecinhas62431b12015-06-02 21:25:08 +00003024static void printMetadataIdentifier(StringRef Name,
3025 formatted_raw_ostream &Out) {
Nick Lewycky5bcbd732011-06-15 06:37:58 +00003026 if (Name.empty()) {
3027 Out << "<empty name> ";
3028 } else {
Filipe Cabecinhas1ac0d2d2015-06-02 21:25:00 +00003029 if (isalpha(static_cast<unsigned char>(Name[0])) || Name[0] == '-' ||
3030 Name[0] == '$' || Name[0] == '.' || Name[0] == '_')
Nick Lewycky5bcbd732011-06-15 06:37:58 +00003031 Out << Name[0];
3032 else
3033 Out << '\\' << hexdigit(Name[0] >> 4) << hexdigit(Name[0] & 0x0F);
3034 for (unsigned i = 1, e = Name.size(); i != e; ++i) {
3035 unsigned char C = Name[i];
Guy Benyei83c74e92013-02-12 21:21:59 +00003036 if (isalnum(static_cast<unsigned char>(C)) || C == '-' || C == '$' ||
3037 C == '.' || C == '_')
Nick Lewycky5bcbd732011-06-15 06:37:58 +00003038 Out << C;
3039 else
3040 Out << '\\' << hexdigit(C >> 4) << hexdigit(C & 0x0F);
3041 }
3042 }
Filipe Cabecinhas62431b12015-06-02 21:25:08 +00003043}
3044
3045void AssemblyWriter::printNamedMDNode(const NamedMDNode *NMD) {
3046 Out << '!';
3047 printMetadataIdentifier(NMD->getName(), Out);
Nick Lewycky5bcbd732011-06-15 06:37:58 +00003048 Out << " = !{";
Chris Lattner2c48c642009-12-31 01:54:05 +00003049 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
Filipe Cabecinhas1ac0d2d2015-06-02 21:25:00 +00003050 if (i)
3051 Out << ", ";
Reid Kleckner6d353342017-08-23 20:31:27 +00003052
3053 // Write DIExpressions inline.
3054 // FIXME: Ban DIExpressions in NamedMDNodes, they will serve no purpose.
3055 MDNode *Op = NMD->getOperand(i);
3056 if (auto *Expr = dyn_cast<DIExpression>(Op)) {
3057 writeDIExpression(Out, Expr, nullptr, nullptr, nullptr);
3058 continue;
3059 }
3060
3061 int Slot = Machine.getMetadataSlot(Op);
Dan Gohman0a54da22010-09-09 20:53:58 +00003062 if (Slot == -1)
3063 Out << "<badref>";
3064 else
3065 Out << '!' << Slot;
Chris Lattner2c48c642009-12-31 01:54:05 +00003066 }
3067 Out << "}\n";
3068}
3069
Chris Lattner3a36d2c2008-08-19 05:06:27 +00003070static void PrintVisibility(GlobalValue::VisibilityTypes Vis,
Dan Gohmane2745262009-08-12 17:23:50 +00003071 formatted_raw_ostream &Out) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00003072 switch (Vis) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00003073 case GlobalValue::DefaultVisibility: break;
3074 case GlobalValue::HiddenVisibility: Out << "hidden "; break;
3075 case GlobalValue::ProtectedVisibility: Out << "protected "; break;
3076 }
3077}
3078
Rafael Espindolae4b02312018-01-11 22:15:05 +00003079static void PrintDSOLocation(const GlobalValue &GV,
3080 formatted_raw_ostream &Out) {
Rafael Espindola9fbc0402018-01-18 02:08:23 +00003081 // GVs with local linkage or non default visibility are implicitly dso_local,
3082 // so we don't print it.
3083 bool Implicit = GV.hasLocalLinkage() ||
3084 (!GV.hasExternalWeakLinkage() && !GV.hasDefaultVisibility());
3085 if (GV.isDSOLocal() && !Implicit)
Sean Fertilec70d28b2017-10-26 15:00:26 +00003086 Out << "dso_local ";
3087}
3088
Nico Rieck7157bb72014-01-14 15:22:47 +00003089static void PrintDLLStorageClass(GlobalValue::DLLStorageClassTypes SCT,
3090 formatted_raw_ostream &Out) {
3091 switch (SCT) {
3092 case GlobalValue::DefaultStorageClass: break;
3093 case GlobalValue::DLLImportStorageClass: Out << "dllimport "; break;
3094 case GlobalValue::DLLExportStorageClass: Out << "dllexport "; break;
3095 }
3096}
3097
Hans Wennborgcbe34b42012-06-23 11:37:03 +00003098static void PrintThreadLocalModel(GlobalVariable::ThreadLocalMode TLM,
3099 formatted_raw_ostream &Out) {
3100 switch (TLM) {
3101 case GlobalVariable::NotThreadLocal:
3102 break;
3103 case GlobalVariable::GeneralDynamicTLSModel:
3104 Out << "thread_local ";
3105 break;
3106 case GlobalVariable::LocalDynamicTLSModel:
3107 Out << "thread_local(localdynamic) ";
3108 break;
3109 case GlobalVariable::InitialExecTLSModel:
3110 Out << "thread_local(initialexec) ";
3111 break;
3112 case GlobalVariable::LocalExecTLSModel:
3113 Out << "thread_local(localexec) ";
3114 break;
3115 }
3116}
3117
Peter Collingbourne96efdd62016-06-14 21:01:22 +00003118static StringRef getUnnamedAddrEncoding(GlobalVariable::UnnamedAddr UA) {
3119 switch (UA) {
3120 case GlobalVariable::UnnamedAddr::None:
3121 return "";
3122 case GlobalVariable::UnnamedAddr::Local:
3123 return "local_unnamed_addr";
3124 case GlobalVariable::UnnamedAddr::Global:
3125 return "unnamed_addr";
3126 }
Aaron Ballman2b9fa3f2016-06-15 15:27:53 +00003127 llvm_unreachable("Unknown UnnamedAddr");
Peter Collingbourne96efdd62016-06-14 21:01:22 +00003128}
3129
Rafael Espindola83a362c2015-01-06 22:55:16 +00003130static void maybePrintComdat(formatted_raw_ostream &Out,
3131 const GlobalObject &GO) {
3132 const Comdat *C = GO.getComdat();
3133 if (!C)
3134 return;
3135
3136 if (isa<GlobalVariable>(GO))
3137 Out << ',';
3138 Out << " comdat";
3139
3140 if (GO.getName() == C->getName())
3141 return;
3142
3143 Out << '(';
3144 PrintLLVMName(Out, C->getName(), ComdatPrefix);
3145 Out << ')';
3146}
3147
Chris Lattner7bfee412001-10-29 16:05:51 +00003148void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Dan Gohman5ded1422010-01-29 23:12:36 +00003149 if (GV->isMaterializable())
3150 Out << "; Materializable\n";
3151
Dan Gohmana2489d12010-07-20 23:55:01 +00003152 WriteAsOperandInternal(Out, GV, &TypePrinter, &Machine, GV->getParent());
Dan Gohman466876b2009-08-12 23:32:33 +00003153 Out << " = ";
Chris Lattner37798642001-09-18 04:01:05 +00003154
Chris Lattner1508d3f2008-08-19 05:16:28 +00003155 if (!GV->hasInitializer() && GV->hasExternalLinkage())
3156 Out << "external ";
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003157
Teresa Johnsonc80c8732018-05-25 15:15:39 +00003158 Out << getLinkageNameWithSpace(GV->getLinkage());
Rafael Espindolae4b02312018-01-11 22:15:05 +00003159 PrintDSOLocation(*GV, Out);
Chris Lattner1508d3f2008-08-19 05:16:28 +00003160 PrintVisibility(GV->getVisibility(), Out);
Nico Rieck7157bb72014-01-14 15:22:47 +00003161 PrintDLLStorageClass(GV->getDLLStorageClass(), Out);
Hans Wennborgcbe34b42012-06-23 11:37:03 +00003162 PrintThreadLocalModel(GV->getThreadLocalMode(), Out);
Peter Collingbourne96efdd62016-06-14 21:01:22 +00003163 StringRef UA = getUnnamedAddrEncoding(GV->getUnnamedAddr());
3164 if (!UA.empty())
3165 Out << UA << ' ';
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +00003166
Chris Lattnerac161bf2009-01-02 07:01:27 +00003167 if (unsigned AddressSpace = GV->getType()->getAddressSpace())
3168 Out << "addrspace(" << AddressSpace << ") ";
Michael Gottesman27e7ef32013-02-05 05:57:38 +00003169 if (GV->isExternallyInitialized()) Out << "externally_initialized ";
Misha Brukmana6619a92004-06-21 21:53:56 +00003170 Out << (GV->isConstant() ? "constant " : "global ");
Manuel Jacob5f6eaac2016-01-16 20:30:46 +00003171 TypePrinter.print(GV->getValueType(), Out);
Chris Lattner37798642001-09-18 04:01:05 +00003172
Dan Gohman81313fd2008-09-14 17:21:12 +00003173 if (GV->hasInitializer()) {
3174 Out << ' ';
Devang Patel983c6b12009-07-08 21:44:25 +00003175 writeOperand(GV->getInitializer(), false);
Dan Gohman81313fd2008-09-14 17:21:12 +00003176 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003177
Chris Lattner9380b812010-07-07 23:16:37 +00003178 if (GV->hasSection()) {
3179 Out << ", section \"";
Jonas Devlieghere745918ff2018-05-31 17:01:42 +00003180 printEscapedString(GV->getSection(), Out);
Chris Lattner9380b812010-07-07 23:16:37 +00003181 Out << '"';
3182 }
Rafael Espindola83a362c2015-01-06 22:55:16 +00003183 maybePrintComdat(Out, *GV);
Chris Lattner4b96c542005-11-12 00:10:19 +00003184 if (GV->getAlignment())
Chris Lattnerf8a974d2005-11-06 06:48:53 +00003185 Out << ", align " << GV->getAlignment();
Anton Korobeynikova97b6942007-04-25 14:27:10 +00003186
Peter Collingbournecceae7f2016-05-31 23:01:54 +00003187 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
3188 GV->getAllMetadata(MDs);
3189 printMetadataAttachments(MDs, ", ");
3190
Javed Absarf3d79042017-05-11 12:28:08 +00003191 auto Attrs = GV->getAttributes();
3192 if (Attrs.hasAttributes())
3193 Out << " #" << Machine.getAttributeGroupSlot(Attrs);
3194
Chris Lattner113f4f42002-06-25 16:13:24 +00003195 printInfoComment(*GV);
Chris Lattnerda975502001-09-10 07:58:01 +00003196}
3197
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +00003198void AssemblyWriter::printIndirectSymbol(const GlobalIndirectSymbol *GIS) {
3199 if (GIS->isMaterializable())
Dan Gohman5ded1422010-01-29 23:12:36 +00003200 Out << "; Materializable\n";
3201
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +00003202 WriteAsOperandInternal(Out, GIS, &TypePrinter, &Machine, GIS->getParent());
Rafael Espindola54fc2982015-06-17 17:53:31 +00003203 Out << " = ";
3204
Teresa Johnsonc80c8732018-05-25 15:15:39 +00003205 Out << getLinkageNameWithSpace(GIS->getLinkage());
Rafael Espindolae4b02312018-01-11 22:15:05 +00003206 PrintDSOLocation(*GIS, Out);
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +00003207 PrintVisibility(GIS->getVisibility(), Out);
3208 PrintDLLStorageClass(GIS->getDLLStorageClass(), Out);
3209 PrintThreadLocalModel(GIS->getThreadLocalMode(), Out);
Peter Collingbourne96efdd62016-06-14 21:01:22 +00003210 StringRef UA = getUnnamedAddrEncoding(GIS->getUnnamedAddr());
3211 if (!UA.empty())
3212 Out << UA << ' ';
Anton Korobeynikova97b6942007-04-25 14:27:10 +00003213
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +00003214 if (isa<GlobalAlias>(GIS))
3215 Out << "alias ";
Dmitry Polukhina1feff72016-04-07 12:32:19 +00003216 else if (isa<GlobalIFunc>(GIS))
3217 Out << "ifunc ";
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +00003218 else
Dmitry Polukhina1feff72016-04-07 12:32:19 +00003219 llvm_unreachable("Not an alias or ifunc!");
Anton Korobeynikova97b6942007-04-25 14:27:10 +00003220
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +00003221 TypePrinter.print(GIS->getValueType(), Out);
David Blaikie2f408302015-09-11 03:22:04 +00003222
3223 Out << ", ";
3224
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +00003225 const Constant *IS = GIS->getIndirectSymbol();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003226
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +00003227 if (!IS) {
3228 TypePrinter.print(GIS->getType(), Out);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003229 Out << " <<NULL ALIASEE>>";
Jay Foad92c19132011-08-01 12:48:54 +00003230 } else {
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +00003231 writeOperand(IS, !isa<ConstantExpr>(IS));
Jay Foad92c19132011-08-01 12:48:54 +00003232 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003233
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +00003234 printInfoComment(*GIS);
Chris Lattner1508d3f2008-08-19 05:16:28 +00003235 Out << '\n';
Anton Korobeynikova97b6942007-04-25 14:27:10 +00003236}
3237
David Majnemerdad0a642014-06-27 18:19:56 +00003238void AssemblyWriter::printComdat(const Comdat *C) {
3239 C->print(Out);
3240}
3241
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003242void AssemblyWriter::printTypeIdentities() {
Roman Tereshind96de6f2018-03-22 21:29:07 +00003243 if (TypePrinter.empty())
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003244 return;
Andrew Trickec4b6e72011-09-30 19:48:58 +00003245
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003246 Out << '\n';
Andrew Trickec4b6e72011-09-30 19:48:58 +00003247
Chris Lattner3243ea12009-03-01 00:03:38 +00003248 // Emit all numbered types.
Roman Tereshind96de6f2018-03-22 21:29:07 +00003249 auto &NumberedTypes = TypePrinter.getNumberedTypes();
3250 for (unsigned I = 0, E = NumberedTypes.size(); I != E; ++I) {
3251 Out << '%' << I << " = type ";
Andrew Trickec4b6e72011-09-30 19:48:58 +00003252
Chris Lattner3243ea12009-03-01 00:03:38 +00003253 // Make sure we print out at least one level of the type structure, so
3254 // that we do not get %2 = type %2
Roman Tereshind96de6f2018-03-22 21:29:07 +00003255 TypePrinter.printStructBody(NumberedTypes[I], Out);
Dan Gohman69273e62009-08-12 23:54:22 +00003256 Out << '\n';
Chris Lattner3243ea12009-03-01 00:03:38 +00003257 }
Andrew Trickec4b6e72011-09-30 19:48:58 +00003258
Roman Tereshind96de6f2018-03-22 21:29:07 +00003259 auto &NamedTypes = TypePrinter.getNamedTypes();
3260 for (unsigned I = 0, E = NamedTypes.size(); I != E; ++I) {
3261 PrintLLVMName(Out, NamedTypes[I]->getName(), LocalPrefix);
Chris Lattner1508d3f2008-08-19 05:16:28 +00003262 Out << " = type ";
Reid Spencere7e96712004-05-25 08:53:40 +00003263
3264 // Make sure we print out at least one level of the type structure, so
3265 // that we do not get %FILE = type %FILE
Roman Tereshind96de6f2018-03-22 21:29:07 +00003266 TypePrinter.printStructBody(NamedTypes[I], Out);
Chris Lattner3a36d2c2008-08-19 05:06:27 +00003267 Out << '\n';
Reid Spencere7e96712004-05-25 08:53:40 +00003268 }
Reid Spencer32af9e82007-01-06 07:24:44 +00003269}
3270
Misha Brukmanc566ca362004-03-02 00:22:19 +00003271/// printFunction - Print all aspects of a function.
Chris Lattner113f4f42002-06-25 16:13:24 +00003272void AssemblyWriter::printFunction(const Function *F) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00003273 // Print out the return type and name.
3274 Out << '\n';
Chris Lattner379a8d22003-04-16 20:28:45 +00003275
Misha Brukmana6619a92004-06-21 21:53:56 +00003276 if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00003277
Dan Gohman5ded1422010-01-29 23:12:36 +00003278 if (F->isMaterializable())
3279 Out << "; Materializable\n";
3280
Reid Klecknerb5180542017-03-21 16:57:19 +00003281 const AttributeList &Attrs = F->getAttributes();
3282 if (Attrs.hasAttributes(AttributeList::FunctionIndex)) {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00003283 AttributeSet AS = Attrs.getFnAttributes();
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +00003284 std::string AttrStr;
3285
Reid Klecknerc2cb5602017-04-12 00:38:00 +00003286 for (const Attribute &Attr : AS) {
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +00003287 if (!Attr.isStringAttribute()) {
3288 if (!AttrStr.empty()) AttrStr += ' ';
3289 AttrStr += Attr.getAsString();
3290 }
3291 }
3292
Bill Wendling847a5c32013-04-16 20:55:47 +00003293 if (!AttrStr.empty())
3294 Out << "; Function Attrs: " << AttrStr << '\n';
3295 }
3296
Peter Collingbourne21521892016-06-21 23:42:48 +00003297 Machine.incorporateFunction(F);
3298
3299 if (F->isDeclaration()) {
3300 Out << "declare";
3301 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
3302 F->getAllMetadata(MDs);
3303 printMetadataAttachments(MDs, " ");
3304 Out << ' ';
3305 } else
Reid Spencer7ce2d2a2006-12-29 20:29:48 +00003306 Out << "define ";
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003307
Teresa Johnsonc80c8732018-05-25 15:15:39 +00003308 Out << getLinkageNameWithSpace(F->getLinkage());
Rafael Espindolae4b02312018-01-11 22:15:05 +00003309 PrintDSOLocation(*F, Out);
Chris Lattner3a36d2c2008-08-19 05:06:27 +00003310 PrintVisibility(F->getVisibility(), Out);
Nico Rieck7157bb72014-01-14 15:22:47 +00003311 PrintDLLStorageClass(F->getDLLStorageClass(), Out);
Chris Lattner379a8d22003-04-16 20:28:45 +00003312
Chris Lattnerf7b6d312005-05-06 20:26:43 +00003313 // Print the calling convention.
Micah Villmow857186d2012-09-13 15:11:12 +00003314 if (F->getCallingConv() != CallingConv::C) {
3315 PrintCallingConv(F->getCallingConv(), Out);
3316 Out << " ";
Chris Lattnerf7b6d312005-05-06 20:26:43 +00003317 }
3318
Chris Lattner229907c2011-07-18 04:54:35 +00003319 FunctionType *FT = F->getFunctionType();
Reid Klecknerb5180542017-03-21 16:57:19 +00003320 if (Attrs.hasAttributes(AttributeList::ReturnIndex))
3321 Out << Attrs.getAsString(AttributeList::ReturnIndex) << ' ';
Chris Lattnere101c442009-02-28 21:26:53 +00003322 TypePrinter.print(F->getReturnType(), Out);
Chris Lattner585297e82008-08-19 05:26:17 +00003323 Out << ' ';
Dan Gohmana2489d12010-07-20 23:55:01 +00003324 WriteAsOperandInternal(Out, F, &TypePrinter, &Machine, F->getParent());
Misha Brukmana6619a92004-06-21 21:53:56 +00003325 Out << '(';
Chris Lattnerfee714f2001-09-07 16:36:04 +00003326
Chris Lattner7bfee412001-10-29 16:05:51 +00003327 // Loop over the arguments, printing them...
Justin Bognerd7d1a722015-09-27 22:38:50 +00003328 if (F->isDeclaration() && !IsForDebug) {
Justin Bogner58e08232015-09-02 17:54:41 +00003329 // We're only interested in the type here - don't print argument names.
3330 for (unsigned I = 0, E = FT->getNumParams(); I != E; ++I) {
Chris Lattner82738fe2007-04-18 00:57:22 +00003331 // Insert commas as we go... the first arg doesn't get a comma
Justin Bogner58e08232015-09-02 17:54:41 +00003332 if (I)
3333 Out << ", ";
3334 // Output type...
3335 TypePrinter.print(FT->getParamType(I), Out);
3336
Reid Kleckner6652a522017-04-28 18:37:16 +00003337 AttributeSet ArgAttrs = Attrs.getParamAttributes(I);
3338 if (ArgAttrs.hasAttributes())
3339 Out << ' ' << ArgAttrs.getAsString();
Chris Lattner82738fe2007-04-18 00:57:22 +00003340 }
3341 } else {
Justin Bogner58e08232015-09-02 17:54:41 +00003342 // The arguments are meaningful here, print them in detail.
Justin Bogner58e08232015-09-02 17:54:41 +00003343 for (const Argument &Arg : F->args()) {
Chris Lattner82738fe2007-04-18 00:57:22 +00003344 // Insert commas as we go... the first arg doesn't get a comma
Reid Kleckner6652a522017-04-28 18:37:16 +00003345 if (Arg.getArgNo() != 0)
Justin Bogner58e08232015-09-02 17:54:41 +00003346 Out << ", ";
Reid Kleckner6652a522017-04-28 18:37:16 +00003347 printArgument(&Arg, Attrs.getParamAttributes(Arg.getArgNo()));
Chris Lattner82738fe2007-04-18 00:57:22 +00003348 }
Reid Spencer8c4914c2006-12-31 05:24:50 +00003349 }
Chris Lattnerfee714f2001-09-07 16:36:04 +00003350
3351 // Finish printing arguments...
Chris Lattner113f4f42002-06-25 16:13:24 +00003352 if (FT->isVarArg()) {
Misha Brukmana6619a92004-06-21 21:53:56 +00003353 if (FT->getNumParams()) Out << ", ";
3354 Out << "..."; // Output varargs portion of signature!
Chris Lattnerfee714f2001-09-07 16:36:04 +00003355 }
Misha Brukmana6619a92004-06-21 21:53:56 +00003356 Out << ')';
Peter Collingbourne96efdd62016-06-14 21:01:22 +00003357 StringRef UA = getUnnamedAddrEncoding(F->getUnnamedAddr());
3358 if (!UA.empty())
3359 Out << ' ' << UA;
Reid Klecknerb5180542017-03-21 16:57:19 +00003360 if (Attrs.hasAttributes(AttributeList::FunctionIndex))
Bill Wendling04945972013-04-29 23:48:06 +00003361 Out << " #" << Machine.getAttributeGroupSlot(Attrs.getFnAttributes());
Chris Lattner9380b812010-07-07 23:16:37 +00003362 if (F->hasSection()) {
3363 Out << " section \"";
Jonas Devlieghere745918ff2018-05-31 17:01:42 +00003364 printEscapedString(F->getSection(), Out);
Chris Lattner9380b812010-07-07 23:16:37 +00003365 Out << '"';
3366 }
Rafael Espindola83a362c2015-01-06 22:55:16 +00003367 maybePrintComdat(Out, *F);
Chris Lattnerf8a974d2005-11-06 06:48:53 +00003368 if (F->getAlignment())
3369 Out << " align " << F->getAlignment();
Gordon Henriksend930f912008-08-17 18:44:35 +00003370 if (F->hasGC())
3371 Out << " gc \"" << F->getGC() << '"';
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00003372 if (F->hasPrefixData()) {
3373 Out << " prefix ";
3374 writeOperand(F->getPrefixData(), true);
3375 }
Peter Collingbourne51d2de72014-12-03 02:08:38 +00003376 if (F->hasPrologueData()) {
3377 Out << " prologue ";
3378 writeOperand(F->getPrologueData(), true);
3379 }
David Majnemer7fddecc2015-06-17 20:52:32 +00003380 if (F->hasPersonalityFn()) {
3381 Out << " personality ";
3382 writeOperand(F->getPersonalityFn(), /*PrintType=*/true);
3383 }
Peter Collingbourne51d2de72014-12-03 02:08:38 +00003384
Reid Spencer5301e7c2007-01-30 20:08:39 +00003385 if (F->isDeclaration()) {
Chris Lattnerfb483622010-09-02 22:52:10 +00003386 Out << '\n';
Chris Lattnerb2f02e52002-05-06 03:00:40 +00003387 } else {
Peter Collingbourne21521892016-06-21 23:42:48 +00003388 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
3389 F->getAllMetadata(MDs);
3390 printMetadataAttachments(MDs, " ");
3391
Chris Lattnerfb483622010-09-02 22:52:10 +00003392 Out << " {";
3393 // Output all of the function's basic blocks.
Benjamin Krameraf28e7d2016-06-26 14:10:56 +00003394 for (const BasicBlock &BB : *F)
3395 printBasicBlock(&BB);
Chris Lattnerfee714f2001-09-07 16:36:04 +00003396
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +00003397 // Output the function's use-lists.
3398 printUseLists(F);
3399
Misha Brukmana6619a92004-06-21 21:53:56 +00003400 Out << "}\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +00003401 }
3402
Reid Spencer16f2f7f2004-05-26 07:18:52 +00003403 Machine.purgeFunction();
Chris Lattner2f7c9632001-06-06 20:29:01 +00003404}
3405
Misha Brukmanc566ca362004-03-02 00:22:19 +00003406/// printArgument - This member is called for every argument that is passed into
3407/// the function. Simply print it out
Reid Kleckner6652a522017-04-28 18:37:16 +00003408void AssemblyWriter::printArgument(const Argument *Arg, AttributeSet Attrs) {
Chris Lattner2f7c9632001-06-06 20:29:01 +00003409 // Output type...
Chris Lattnere101c442009-02-28 21:26:53 +00003410 TypePrinter.print(Arg->getType(), Out);
Misha Brukmanb1c93172005-04-21 23:48:37 +00003411
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00003412 // Output parameter attributes list
Reid Kleckner6652a522017-04-28 18:37:16 +00003413 if (Attrs.hasAttributes())
3414 Out << ' ' << Attrs.getAsString();
Reid Spencer8c4914c2006-12-31 05:24:50 +00003415
Chris Lattner2f7c9632001-06-06 20:29:01 +00003416 // Output name, if available...
Chris Lattner033935d2008-08-17 04:40:13 +00003417 if (Arg->hasName()) {
3418 Out << ' ';
3419 PrintLLVMName(Out, Arg);
3420 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00003421}
3422
Misha Brukmanc566ca362004-03-02 00:22:19 +00003423/// printBasicBlock - This member is called for each basic block in a method.
Chris Lattner7bfee412001-10-29 16:05:51 +00003424void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Nick Lewycky4d43d3c2008-04-25 16:53:59 +00003425 if (BB->hasName()) { // Print out the label if it exists...
Chris Lattner033935d2008-08-17 04:40:13 +00003426 Out << "\n";
Daniel Dunbare03eecb2009-07-25 23:55:21 +00003427 PrintLLVMName(Out, BB->getName(), LabelPrefix);
Chris Lattner033935d2008-08-17 04:40:13 +00003428 Out << ':';
Nick Lewycky4d43d3c2008-04-25 16:53:59 +00003429 } else if (!BB->use_empty()) { // Don't print block # of no uses...
Bill Wendling35a9c3c2011-04-10 23:18:04 +00003430 Out << "\n; <label>:";
Chris Lattner5e043322007-01-11 03:54:27 +00003431 int Slot = Machine.getLocalSlot(BB);
Chris Lattner757ee0b2004-06-09 19:41:19 +00003432 if (Slot != -1)
Evgeniy Stepanove257f0f2016-01-27 21:53:08 +00003433 Out << Slot << ":";
Chris Lattner757ee0b2004-06-09 19:41:19 +00003434 else
Misha Brukmana6619a92004-06-21 21:53:56 +00003435 Out << "<badref>";
Chris Lattner58185f22002-10-02 19:38:55 +00003436 }
Chris Lattner2447ef52003-11-20 00:09:43 +00003437
Craig Topperc6207612014-04-09 06:08:46 +00003438 if (!BB->getParent()) {
Chris Lattneree97b8b2009-08-17 15:48:08 +00003439 Out.PadToColumn(50);
Dan Gohmane2745262009-08-12 17:23:50 +00003440 Out << "; Error: Block without parent!";
3441 } else if (BB != &BB->getParent()->getEntryBlock()) { // Not the entry block?
Chris Lattnerfb483622010-09-02 22:52:10 +00003442 // Output predecessors for the block.
Chris Lattneree97b8b2009-08-17 15:48:08 +00003443 Out.PadToColumn(50);
Dan Gohmane2745262009-08-12 17:23:50 +00003444 Out << ";";
Gabor Greif6c6b2fd2010-03-25 23:25:28 +00003445 const_pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003446
Chris Lattnerff834c02008-04-22 02:45:44 +00003447 if (PI == PE) {
3448 Out << " No predecessors!";
3449 } else {
Dan Gohman81313fd2008-09-14 17:21:12 +00003450 Out << " preds = ";
Chris Lattnerff834c02008-04-22 02:45:44 +00003451 writeOperand(*PI, false);
3452 for (++PI; PI != PE; ++PI) {
Dan Gohman81313fd2008-09-14 17:21:12 +00003453 Out << ", ";
Chris Lattner78e2e8b2006-12-06 06:24:27 +00003454 writeOperand(*PI, false);
Chris Lattner00211f12003-11-16 22:59:57 +00003455 }
Chris Lattner58185f22002-10-02 19:38:55 +00003456 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00003457 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00003458
Chris Lattnerff834c02008-04-22 02:45:44 +00003459 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +00003460
Misha Brukmana6619a92004-06-21 21:53:56 +00003461 if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00003462
Chris Lattnerfee714f2001-09-07 16:36:04 +00003463 // Output all of the instructions in the basic block...
Benjamin Krameraf28e7d2016-06-26 14:10:56 +00003464 for (const Instruction &I : *BB) {
3465 printInstructionLine(I);
Dan Gohmana4f709e2009-07-13 18:27:59 +00003466 }
Chris Lattner96cdd272004-03-08 18:51:45 +00003467
Misha Brukmana6619a92004-06-21 21:53:56 +00003468 if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, Out);
Chris Lattner2f7c9632001-06-06 20:29:01 +00003469}
3470
Daniel Maleaded9f932013-05-08 20:38:31 +00003471/// printInstructionLine - Print an instruction and a newline character.
3472void AssemblyWriter::printInstructionLine(const Instruction &I) {
3473 printInstruction(I);
3474 Out << '\n';
3475}
3476
Igor Laevsky2aa8caf2015-05-05 13:20:42 +00003477/// printGCRelocateComment - print comment after call to the gc.relocate
3478/// intrinsic indicating base and derived pointer names.
Manuel Jacob83eefa62016-01-05 04:03:00 +00003479void AssemblyWriter::printGCRelocateComment(const GCRelocateInst &Relocate) {
Igor Laevsky2aa8caf2015-05-05 13:20:42 +00003480 Out << " ; (";
Manuel Jacob83eefa62016-01-05 04:03:00 +00003481 writeOperand(Relocate.getBasePtr(), false);
Igor Laevsky2aa8caf2015-05-05 13:20:42 +00003482 Out << ", ";
Manuel Jacob83eefa62016-01-05 04:03:00 +00003483 writeOperand(Relocate.getDerivedPtr(), false);
Igor Laevsky2aa8caf2015-05-05 13:20:42 +00003484 Out << ")";
3485}
3486
Misha Brukmanc566ca362004-03-02 00:22:19 +00003487/// printInfoComment - Print a little comment after the instruction indicating
3488/// which slot it occupies.
Chris Lattner113f4f42002-06-25 16:13:24 +00003489void AssemblyWriter::printInfoComment(const Value &V) {
Manuel Jacob83eefa62016-01-05 04:03:00 +00003490 if (const auto *Relocate = dyn_cast<GCRelocateInst>(&V))
3491 printGCRelocateComment(*Relocate);
Igor Laevsky2aa8caf2015-05-05 13:20:42 +00003492
Bill Wendling847a5c32013-04-16 20:55:47 +00003493 if (AnnotationWriter)
Dan Gohmane34890f2010-02-10 20:41:46 +00003494 AnnotationWriter->printInfoComment(V, Out);
Chris Lattner862e3382001-10-13 06:42:36 +00003495}
3496
Reid Spencere7141c82006-08-28 01:02:49 +00003497// This member is called for each Instruction in a function..
Chris Lattner113f4f42002-06-25 16:13:24 +00003498void AssemblyWriter::printInstruction(const Instruction &I) {
Misha Brukmana6619a92004-06-21 21:53:56 +00003499 if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00003500
Dan Gohman466876b2009-08-12 23:32:33 +00003501 // Print out indentation for an instruction.
Dan Gohmanb4583b12009-08-13 01:41:52 +00003502 Out << " ";
Chris Lattner2f7c9632001-06-06 20:29:01 +00003503
3504 // Print out name if it exists...
Chris Lattner033935d2008-08-17 04:40:13 +00003505 if (I.hasName()) {
3506 PrintLLVMName(Out, &I);
3507 Out << " = ";
Chris Lattner031560c2009-12-29 07:25:48 +00003508 } else if (!I.getType()->isVoidTy()) {
Chris Lattnerb25e5ea2008-08-29 17:19:30 +00003509 // Print out the def slot taken.
3510 int SlotNum = Machine.getLocalSlot(&I);
3511 if (SlotNum == -1)
3512 Out << "<badref> = ";
3513 else
3514 Out << '%' << SlotNum << " = ";
Chris Lattner033935d2008-08-17 04:40:13 +00003515 }
Andrew Trickec4b6e72011-09-30 19:48:58 +00003516
Reid Kleckner5772b772014-04-24 20:14:34 +00003517 if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
3518 if (CI->isMustTailCall())
3519 Out << "musttail ";
3520 else if (CI->isTailCall())
3521 Out << "tail ";
Akira Hatanaka5cfcce122015-11-06 23:55:38 +00003522 else if (CI->isNoTailCall())
3523 Out << "notail ";
Reid Kleckner5772b772014-04-24 20:14:34 +00003524 }
Chris Lattner504f9242003-09-08 17:45:59 +00003525
Chris Lattner2f7c9632001-06-06 20:29:01 +00003526 // Print out the opcode...
Misha Brukmana6619a92004-06-21 21:53:56 +00003527 Out << I.getOpcodeName();
Chris Lattner2f7c9632001-06-06 20:29:01 +00003528
Eli Friedman02e737b2011-08-12 22:50:01 +00003529 // If this is an atomic load or store, print out the atomic marker.
3530 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isAtomic()) ||
3531 (isa<StoreInst>(I) && cast<StoreInst>(I).isAtomic()))
3532 Out << " atomic";
3533
Tim Northover420a2162014-06-13 14:24:07 +00003534 if (isa<AtomicCmpXchgInst>(I) && cast<AtomicCmpXchgInst>(I).isWeak())
3535 Out << " weak";
3536
Eli Friedman02e737b2011-08-12 22:50:01 +00003537 // If this is a volatile operation, print out the volatile marker.
3538 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) ||
3539 (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile()) ||
3540 (isa<AtomicCmpXchgInst>(I) && cast<AtomicCmpXchgInst>(I).isVolatile()) ||
3541 (isa<AtomicRMWInst>(I) && cast<AtomicRMWInst>(I).isVolatile()))
3542 Out << " volatile";
3543
Dan Gohman9c7f8082009-07-27 16:11:46 +00003544 // Print out optimization information.
3545 WriteOptimizationInfo(Out, &I);
3546
Reid Spencer45e52392006-12-03 06:27:29 +00003547 // Print out the compare instruction predicates
Nate Begemand2195702008-05-12 19:01:56 +00003548 if (const CmpInst *CI = dyn_cast<CmpInst>(&I))
Tim Northoverde3aea0412016-08-17 20:25:25 +00003549 Out << ' ' << CmpInst::getPredicateName(CI->getPredicate());
Reid Spencer45e52392006-12-03 06:27:29 +00003550
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003551 // Print out the atomicrmw operation
3552 if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(&I))
3553 writeAtomicRMWOperation(Out, RMWI->getOperation());
3554
Chris Lattner2f7c9632001-06-06 20:29:01 +00003555 // Print out the type of the operands...
Craig Topperc6207612014-04-09 06:08:46 +00003556 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : nullptr;
Chris Lattner2f7c9632001-06-06 20:29:01 +00003557
3558 // Special case conditional branches to swizzle the condition out to the front
Gabor Greifcab008f2009-02-09 15:45:06 +00003559 if (isa<BranchInst>(I) && cast<BranchInst>(I).isConditional()) {
David Blaikieb78e9e52013-02-11 01:16:51 +00003560 const BranchInst &BI(cast<BranchInst>(I));
Dan Gohman81313fd2008-09-14 17:21:12 +00003561 Out << ' ';
Gabor Greifcab008f2009-02-09 15:45:06 +00003562 writeOperand(BI.getCondition(), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00003563 Out << ", ";
Gabor Greifcab008f2009-02-09 15:45:06 +00003564 writeOperand(BI.getSuccessor(0), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00003565 Out << ", ";
Gabor Greifcab008f2009-02-09 15:45:06 +00003566 writeOperand(BI.getSuccessor(1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00003567
Chris Lattner8d48df22002-04-13 18:34:38 +00003568 } else if (isa<SwitchInst>(I)) {
David Blaikieb78e9e52013-02-11 01:16:51 +00003569 const SwitchInst& SI(cast<SwitchInst>(I));
Chris Lattner3ed871f2009-10-27 19:13:16 +00003570 // Special case switch instruction to get formatting nice and correct.
Dan Gohman81313fd2008-09-14 17:21:12 +00003571 Out << ' ';
Eli Friedman95031ed2011-09-29 20:21:17 +00003572 writeOperand(SI.getCondition(), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00003573 Out << ", ";
Eli Friedman95031ed2011-09-29 20:21:17 +00003574 writeOperand(SI.getDefaultDest(), true);
Chris Lattner82ff9232008-08-23 22:52:27 +00003575 Out << " [";
Chandler Carruth927d8e62017-04-12 07:27:28 +00003576 for (auto Case : SI.cases()) {
Dan Gohmanb4583b12009-08-13 01:41:52 +00003577 Out << "\n ";
Chandler Carruth927d8e62017-04-12 07:27:28 +00003578 writeOperand(Case.getCaseValue(), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00003579 Out << ", ";
Chandler Carruth927d8e62017-04-12 07:27:28 +00003580 writeOperand(Case.getCaseSuccessor(), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00003581 }
Dan Gohmanb4583b12009-08-13 01:41:52 +00003582 Out << "\n ]";
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003583 } else if (isa<IndirectBrInst>(I)) {
3584 // Special case indirectbr instruction to get formatting nice and correct.
Chris Lattner3ed871f2009-10-27 19:13:16 +00003585 Out << ' ';
3586 writeOperand(Operand, true);
Dan Gohman43c57402009-10-30 02:01:10 +00003587 Out << ", [";
Andrew Trickec4b6e72011-09-30 19:48:58 +00003588
Chris Lattner3ed871f2009-10-27 19:13:16 +00003589 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) {
3590 if (i != 1)
3591 Out << ", ";
3592 writeOperand(I.getOperand(i), true);
3593 }
3594 Out << ']';
Jay Foad372ad642011-06-20 14:18:48 +00003595 } else if (const PHINode *PN = dyn_cast<PHINode>(&I)) {
Misha Brukmana6619a92004-06-21 21:53:56 +00003596 Out << ' ';
Chris Lattnere101c442009-02-28 21:26:53 +00003597 TypePrinter.print(I.getType(), Out);
Misha Brukmana6619a92004-06-21 21:53:56 +00003598 Out << ' ';
Chris Lattner2f7c9632001-06-06 20:29:01 +00003599
Jay Foad372ad642011-06-20 14:18:48 +00003600 for (unsigned op = 0, Eop = PN->getNumIncomingValues(); op < Eop; ++op) {
Misha Brukmana6619a92004-06-21 21:53:56 +00003601 if (op) Out << ", ";
Dan Gohman81313fd2008-09-14 17:21:12 +00003602 Out << "[ ";
Jay Foad372ad642011-06-20 14:18:48 +00003603 writeOperand(PN->getIncomingValue(op), false); Out << ", ";
3604 writeOperand(PN->getIncomingBlock(op), false); Out << " ]";
Chris Lattner931ef3b2001-06-11 15:04:20 +00003605 }
Dan Gohmana76f0f72008-05-31 19:12:39 +00003606 } else if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(&I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00003607 Out << ' ';
Dan Gohmana76f0f72008-05-31 19:12:39 +00003608 writeOperand(I.getOperand(0), true);
3609 for (const unsigned *i = EVI->idx_begin(), *e = EVI->idx_end(); i != e; ++i)
3610 Out << ", " << *i;
3611 } else if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(&I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00003612 Out << ' ';
3613 writeOperand(I.getOperand(0), true); Out << ", ";
Dan Gohmana76f0f72008-05-31 19:12:39 +00003614 writeOperand(I.getOperand(1), true);
3615 for (const unsigned *i = IVI->idx_begin(), *e = IVI->idx_end(); i != e; ++i)
3616 Out << ", " << *i;
Bill Wendlingfae14752011-08-12 20:24:12 +00003617 } else if (const LandingPadInst *LPI = dyn_cast<LandingPadInst>(&I)) {
3618 Out << ' ';
3619 TypePrinter.print(I.getType(), Out);
David Majnemer7fddecc2015-06-17 20:52:32 +00003620 if (LPI->isCleanup() || LPI->getNumClauses() != 0)
3621 Out << '\n';
Bill Wendlingfae14752011-08-12 20:24:12 +00003622
3623 if (LPI->isCleanup())
3624 Out << " cleanup";
3625
3626 for (unsigned i = 0, e = LPI->getNumClauses(); i != e; ++i) {
3627 if (i != 0 || LPI->isCleanup()) Out << "\n";
3628 if (LPI->isCatch(i))
3629 Out << " catch ";
3630 else
3631 Out << " filter ";
3632
3633 writeOperand(LPI->getClause(i), true);
3634 }
David Majnemer8a1c45d2015-12-12 05:38:55 +00003635 } else if (const auto *CatchSwitch = dyn_cast<CatchSwitchInst>(&I)) {
3636 Out << " within ";
3637 writeOperand(CatchSwitch->getParentPad(), /*PrintType=*/false);
David Majnemer654e1302015-07-31 17:58:14 +00003638 Out << " [";
David Majnemer8a1c45d2015-12-12 05:38:55 +00003639 unsigned Op = 0;
3640 for (const BasicBlock *PadBB : CatchSwitch->handlers()) {
3641 if (Op > 0)
3642 Out << ", ";
3643 writeOperand(PadBB, /*PrintType=*/true);
3644 ++Op;
3645 }
3646 Out << "] unwind ";
3647 if (const BasicBlock *UnwindDest = CatchSwitch->getUnwindDest())
3648 writeOperand(UnwindDest, /*PrintType=*/true);
3649 else
3650 Out << "to caller";
3651 } else if (const auto *FPI = dyn_cast<FuncletPadInst>(&I)) {
3652 Out << " within ";
3653 writeOperand(FPI->getParentPad(), /*PrintType=*/false);
3654 Out << " [";
3655 for (unsigned Op = 0, NumOps = FPI->getNumArgOperands(); Op < NumOps;
David Majnemer654e1302015-07-31 17:58:14 +00003656 ++Op) {
3657 if (Op > 0)
3658 Out << ", ";
David Majnemer8a1c45d2015-12-12 05:38:55 +00003659 writeOperand(FPI->getArgOperand(Op), /*PrintType=*/true);
David Majnemer654e1302015-07-31 17:58:14 +00003660 }
David Majnemer8a1c45d2015-12-12 05:38:55 +00003661 Out << ']';
Devang Patel59643e52008-02-23 00:35:18 +00003662 } else if (isa<ReturnInst>(I) && !Operand) {
3663 Out << " void";
David Majnemer0bc0eef2015-08-15 02:46:08 +00003664 } else if (const auto *CRI = dyn_cast<CatchReturnInst>(&I)) {
David Majnemer8a1c45d2015-12-12 05:38:55 +00003665 Out << " from ";
3666 writeOperand(CRI->getOperand(0), /*PrintType=*/false);
David Majnemer0bc0eef2015-08-15 02:46:08 +00003667
3668 Out << " to ";
David Majnemer8a1c45d2015-12-12 05:38:55 +00003669 writeOperand(CRI->getOperand(1), /*PrintType=*/true);
David Majnemer654e1302015-07-31 17:58:14 +00003670 } else if (const auto *CRI = dyn_cast<CleanupReturnInst>(&I)) {
David Majnemer8a1c45d2015-12-12 05:38:55 +00003671 Out << " from ";
3672 writeOperand(CRI->getOperand(0), /*PrintType=*/false);
David Majnemer654e1302015-07-31 17:58:14 +00003673
3674 Out << " unwind ";
3675 if (CRI->hasUnwindDest())
David Majnemer8a1c45d2015-12-12 05:38:55 +00003676 writeOperand(CRI->getOperand(1), /*PrintType=*/true);
Joseph Tremoulet9ce71f72015-09-03 09:09:43 +00003677 else
3678 Out << "to caller";
Chris Lattnerf7b6d312005-05-06 20:26:43 +00003679 } else if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
3680 // Print the calling convention being used.
Micah Villmow857186d2012-09-13 15:11:12 +00003681 if (CI->getCallingConv() != CallingConv::C) {
3682 Out << " ";
3683 PrintCallingConv(CI->getCallingConv(), Out);
Chris Lattnerf7b6d312005-05-06 20:26:43 +00003684 }
3685
Gabor Greifc9a92512010-06-23 13:09:06 +00003686 Operand = CI->getCalledValue();
Ahmed Bougachaaa9fe532016-12-21 23:26:13 +00003687 FunctionType *FTy = CI->getFunctionType();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003688 Type *RetTy = FTy->getReturnType();
Reid Klecknerb5180542017-03-21 16:57:19 +00003689 const AttributeList &PAL = CI->getAttributes();
Chris Lattner2f2d9472001-11-06 21:28:12 +00003690
Reid Klecknerb5180542017-03-21 16:57:19 +00003691 if (PAL.hasAttributes(AttributeList::ReturnIndex))
3692 Out << ' ' << PAL.getAsString(AttributeList::ReturnIndex);
Devang Patel221fe422008-09-29 20:49:50 +00003693
Chris Lattner463d6a52003-08-05 15:34:45 +00003694 // If possible, print out the short form of the call instruction. We can
Chris Lattner6915f8f2002-04-07 22:49:37 +00003695 // only do this if the first argument is a pointer to a nonvararg function,
Chris Lattner463d6a52003-08-05 15:34:45 +00003696 // and if the return type is not a pointer to a function.
Chris Lattner2f2d9472001-11-06 21:28:12 +00003697 //
Dan Gohman81313fd2008-09-14 17:21:12 +00003698 Out << ' ';
David Blaikie23af6482015-04-16 23:24:18 +00003699 TypePrinter.print(FTy->isVarArg() ? FTy : RetTy, Out);
3700 Out << ' ';
3701 writeOperand(Operand, false);
Misha Brukmana6619a92004-06-21 21:53:56 +00003702 Out << '(';
Gabor Greifc9a92512010-06-23 13:09:06 +00003703 for (unsigned op = 0, Eop = CI->getNumArgOperands(); op < Eop; ++op) {
3704 if (op > 0)
Dan Gohman81313fd2008-09-14 17:21:12 +00003705 Out << ", ";
Reid Kleckner6652a522017-04-28 18:37:16 +00003706 writeParamOperand(CI->getArgOperand(op), PAL.getParamAttributes(op));
Chris Lattner2f7c9632001-06-06 20:29:01 +00003707 }
Reid Kleckner83498642014-08-26 00:33:28 +00003708
3709 // Emit an ellipsis if this is a musttail call in a vararg function. This
3710 // is only to aid readability, musttail calls forward varargs by default.
3711 if (CI->isMustTailCall() && CI->getParent() &&
3712 CI->getParent()->getParent() &&
3713 CI->getParent()->getParent()->isVarArg())
3714 Out << ", ...";
3715
Dan Gohman81313fd2008-09-14 17:21:12 +00003716 Out << ')';
Reid Klecknerb5180542017-03-21 16:57:19 +00003717 if (PAL.hasAttributes(AttributeList::FunctionIndex))
Bill Wendlinga0323742013-02-22 09:09:42 +00003718 Out << " #" << Machine.getAttributeGroupSlot(PAL.getFnAttributes());
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00003719
3720 writeOperandBundles(CI);
Chris Lattner113f4f42002-06-25 16:13:24 +00003721 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
Gabor Greifa2fbc0a2010-03-24 13:21:49 +00003722 Operand = II->getCalledValue();
Ahmed Bougachaaa9fe532016-12-21 23:26:13 +00003723 FunctionType *FTy = II->getFunctionType();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003724 Type *RetTy = FTy->getReturnType();
Reid Klecknerb5180542017-03-21 16:57:19 +00003725 const AttributeList &PAL = II->getAttributes();
Chris Lattner463d6a52003-08-05 15:34:45 +00003726
Chris Lattnerf7b6d312005-05-06 20:26:43 +00003727 // Print the calling convention being used.
Micah Villmow857186d2012-09-13 15:11:12 +00003728 if (II->getCallingConv() != CallingConv::C) {
3729 Out << " ";
3730 PrintCallingConv(II->getCallingConv(), Out);
Chris Lattnerf7b6d312005-05-06 20:26:43 +00003731 }
3732
Reid Klecknerb5180542017-03-21 16:57:19 +00003733 if (PAL.hasAttributes(AttributeList::ReturnIndex))
3734 Out << ' ' << PAL.getAsString(AttributeList::ReturnIndex);
Devang Patel221fe422008-09-29 20:49:50 +00003735
Chris Lattner463d6a52003-08-05 15:34:45 +00003736 // If possible, print out the short form of the invoke instruction. We can
3737 // only do this if the first argument is a pointer to a nonvararg function,
3738 // and if the return type is not a pointer to a function.
3739 //
Dan Gohmanc7e00ba2008-10-15 18:02:08 +00003740 Out << ' ';
David Blaikie445e3fb2015-04-24 19:32:54 +00003741 TypePrinter.print(FTy->isVarArg() ? FTy : RetTy, Out);
3742 Out << ' ';
3743 writeOperand(Operand, false);
Misha Brukmana6619a92004-06-21 21:53:56 +00003744 Out << '(';
Gabor Greifc9a92512010-06-23 13:09:06 +00003745 for (unsigned op = 0, Eop = II->getNumArgOperands(); op < Eop; ++op) {
Gabor Greifa2fbc0a2010-03-24 13:21:49 +00003746 if (op)
Dan Gohman81313fd2008-09-14 17:21:12 +00003747 Out << ", ";
Reid Kleckner6652a522017-04-28 18:37:16 +00003748 writeParamOperand(II->getArgOperand(op), PAL.getParamAttributes(op));
Chris Lattner862e3382001-10-13 06:42:36 +00003749 }
3750
Dan Gohman81313fd2008-09-14 17:21:12 +00003751 Out << ')';
Reid Klecknerb5180542017-03-21 16:57:19 +00003752 if (PAL.hasAttributes(AttributeList::FunctionIndex))
Bill Wendlinga0323742013-02-22 09:09:42 +00003753 Out << " #" << Machine.getAttributeGroupSlot(PAL.getFnAttributes());
Devang Patela05633e2008-09-26 22:53:05 +00003754
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00003755 writeOperandBundles(II);
3756
Dan Gohmanb4583b12009-08-13 01:41:52 +00003757 Out << "\n to ";
Chris Lattner862e3382001-10-13 06:42:36 +00003758 writeOperand(II->getNormalDest(), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00003759 Out << " unwind ";
Chris Lattnerfae8ab32004-02-08 21:44:31 +00003760 writeOperand(II->getUnwindDest(), true);
Victor Hernandez8acf2952009-10-23 21:09:37 +00003761 } else if (const AllocaInst *AI = dyn_cast<AllocaInst>(&I)) {
Misha Brukmana6619a92004-06-21 21:53:56 +00003762 Out << ' ';
Reid Kleckner20844032014-01-25 01:24:06 +00003763 if (AI->isUsedWithInAlloca())
David Majnemerc4ab61c2014-03-09 06:41:58 +00003764 Out << "inalloca ";
Manman Ren9bfd0d02016-04-01 21:41:15 +00003765 if (AI->isSwiftError())
3766 Out << "swifterror ";
David Majnemerc4ab61c2014-03-09 06:41:58 +00003767 TypePrinter.print(AI->getAllocatedType(), Out);
Duncan P. N. Exon Smith720762e2015-03-13 19:30:44 +00003768
3769 // Explicitly write the array size if the code is broken, if it's an array
3770 // allocation, or if the type is not canonical for scalar allocations. The
3771 // latter case prevents the type from mutating when round-tripping through
3772 // assembly.
3773 if (!AI->getArraySize() || AI->isArrayAllocation() ||
3774 !AI->getArraySize()->getType()->isIntegerTy(32)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00003775 Out << ", ";
Chris Lattner8d48df22002-04-13 18:34:38 +00003776 writeOperand(AI->getArraySize(), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00003777 }
Nate Begeman848622f2005-11-05 09:21:28 +00003778 if (AI->getAlignment()) {
Chris Lattner7aeee3a2005-11-05 21:20:34 +00003779 Out << ", align " << AI->getAlignment();
Nate Begeman848622f2005-11-05 09:21:28 +00003780 }
Matt Arsenault3c1fc762017-04-10 22:27:50 +00003781
3782 unsigned AddrSpace = AI->getType()->getAddressSpace();
3783 if (AddrSpace != 0) {
3784 Out << ", addrspace(" << AddrSpace << ')';
3785 }
Chris Lattner862e3382001-10-13 06:42:36 +00003786 } else if (isa<CastInst>(I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00003787 if (Operand) {
3788 Out << ' ';
3789 writeOperand(Operand, true); // Work with broken code
3790 }
Misha Brukmana6619a92004-06-21 21:53:56 +00003791 Out << " to ";
Chris Lattnere101c442009-02-28 21:26:53 +00003792 TypePrinter.print(I.getType(), Out);
Chris Lattner5b337482003-10-18 05:57:43 +00003793 } else if (isa<VAArgInst>(I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00003794 if (Operand) {
3795 Out << ' ';
3796 writeOperand(Operand, true); // Work with broken code
3797 }
Misha Brukmana6619a92004-06-21 21:53:56 +00003798 Out << ", ";
Chris Lattnere101c442009-02-28 21:26:53 +00003799 TypePrinter.print(I.getType(), Out);
3800 } else if (Operand) { // Print the normal way.
David Blaikiea79ac142015-02-27 21:17:42 +00003801 if (const auto *GEP = dyn_cast<GetElementPtrInst>(&I)) {
David Blaikie79e6c742015-02-27 19:29:02 +00003802 Out << ' ';
3803 TypePrinter.print(GEP->getSourceElementType(), Out);
3804 Out << ',';
David Blaikiea79ac142015-02-27 21:17:42 +00003805 } else if (const auto *LI = dyn_cast<LoadInst>(&I)) {
3806 Out << ' ';
3807 TypePrinter.print(LI->getType(), Out);
Benjamin Kramer257ed692015-03-02 15:24:41 +00003808 Out << ',';
David Blaikie79e6c742015-02-27 19:29:02 +00003809 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00003810
Misha Brukmanb1c93172005-04-21 23:48:37 +00003811 // PrintAllTypes - Instructions who have operands of all the same type
Chris Lattner2f7c9632001-06-06 20:29:01 +00003812 // omit the type from all but the first operand. If the instruction has
3813 // different type operands (for example br), then they are all printed.
3814 bool PrintAllTypes = false;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003815 Type *TheType = Operand->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +00003816
Reid Spencer0cdd04f2007-02-02 13:54:55 +00003817 // Select, Store and ShuffleVector always print all types.
Devang Patelce556d92008-03-04 22:05:14 +00003818 if (isa<SelectInst>(I) || isa<StoreInst>(I) || isa<ShuffleVectorInst>(I)
3819 || isa<ReturnInst>(I)) {
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00003820 PrintAllTypes = true;
3821 } else {
3822 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
3823 Operand = I.getOperand(i);
Nuno Lopes04fe2f02009-01-15 18:40:57 +00003824 // note that Operand shouldn't be null, but the test helps make dump()
3825 // more tolerant of malformed IR
Nuno Lopes0971e772009-01-14 17:51:41 +00003826 if (Operand && Operand->getType() != TheType) {
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00003827 PrintAllTypes = true; // We have differing types! Print them all!
3828 break;
3829 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00003830 }
3831 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00003832
Chris Lattner7bfee412001-10-29 16:05:51 +00003833 if (!PrintAllTypes) {
Misha Brukmana6619a92004-06-21 21:53:56 +00003834 Out << ' ';
Chris Lattnere101c442009-02-28 21:26:53 +00003835 TypePrinter.print(TheType, Out);
Chris Lattner7bfee412001-10-29 16:05:51 +00003836 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00003837
Dan Gohman81313fd2008-09-14 17:21:12 +00003838 Out << ' ';
Chris Lattner113f4f42002-06-25 16:13:24 +00003839 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
Dan Gohman81313fd2008-09-14 17:21:12 +00003840 if (i) Out << ", ";
Chris Lattner113f4f42002-06-25 16:13:24 +00003841 writeOperand(I.getOperand(i), PrintAllTypes);
Chris Lattner2f7c9632001-06-06 20:29:01 +00003842 }
3843 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003844
Eli Friedman59b66882011-08-09 23:02:53 +00003845 // Print atomic ordering/alignment for memory operations
3846 if (const LoadInst *LI = dyn_cast<LoadInst>(&I)) {
3847 if (LI->isAtomic())
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003848 writeAtomic(LI->getContext(), LI->getOrdering(), LI->getSyncScopeID());
Eli Friedman59b66882011-08-09 23:02:53 +00003849 if (LI->getAlignment())
3850 Out << ", align " << LI->getAlignment();
3851 } else if (const StoreInst *SI = dyn_cast<StoreInst>(&I)) {
3852 if (SI->isAtomic())
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003853 writeAtomic(SI->getContext(), SI->getOrdering(), SI->getSyncScopeID());
Eli Friedman59b66882011-08-09 23:02:53 +00003854 if (SI->getAlignment())
3855 Out << ", align " << SI->getAlignment();
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003856 } else if (const AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(&I)) {
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003857 writeAtomicCmpXchg(CXI->getContext(), CXI->getSuccessOrdering(),
3858 CXI->getFailureOrdering(), CXI->getSyncScopeID());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003859 } else if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(&I)) {
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003860 writeAtomic(RMWI->getContext(), RMWI->getOrdering(),
3861 RMWI->getSyncScopeID());
Eli Friedmanfee02c62011-07-25 23:16:38 +00003862 } else if (const FenceInst *FI = dyn_cast<FenceInst>(&I)) {
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003863 writeAtomic(FI->getContext(), FI->getOrdering(), FI->getSyncScopeID());
Christopher Lamb84485702007-04-22 19:24:39 +00003864 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00003865
Chris Lattnerc9558df2009-12-28 20:10:43 +00003866 // Print Metadata info.
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00003867 SmallVector<std::pair<unsigned, MDNode *>, 4> InstMD;
Nick Lewyckyba8ec9a2010-02-25 06:53:04 +00003868 I.getAllMetadata(InstMD);
Duncan P. N. Exon Smithd3e4c2a2015-04-24 21:06:21 +00003869 printMetadataAttachments(InstMD, ", ");
Duncan P. N. Exon Smith86979272015-04-24 20:59:52 +00003870
3871 // Print a nice comment.
Chris Lattner862e3382001-10-13 06:42:36 +00003872 printInfoComment(I);
Chris Lattner2f7c9632001-06-06 20:29:01 +00003873}
3874
Duncan P. N. Exon Smith86979272015-04-24 20:59:52 +00003875void AssemblyWriter::printMetadataAttachments(
Duncan P. N. Exon Smithd3e4c2a2015-04-24 21:06:21 +00003876 const SmallVectorImpl<std::pair<unsigned, MDNode *>> &MDs,
3877 StringRef Separator) {
Duncan P. N. Exon Smith86979272015-04-24 20:59:52 +00003878 if (MDs.empty())
3879 return;
3880
Duncan P. N. Exon Smithe30f10e2015-04-24 21:03:05 +00003881 if (MDNames.empty())
Mehdi Aminib9b50aa2016-01-07 20:14:30 +00003882 MDs[0].second->getContext().getMDKindNames(MDNames);
Duncan P. N. Exon Smithe30f10e2015-04-24 21:03:05 +00003883
Duncan P. N. Exon Smith86979272015-04-24 20:59:52 +00003884 for (const auto &I : MDs) {
3885 unsigned Kind = I.first;
Duncan P. N. Exon Smithd3e4c2a2015-04-24 21:06:21 +00003886 Out << Separator;
Filipe Cabecinhas62431b12015-06-02 21:25:08 +00003887 if (Kind < MDNames.size()) {
3888 Out << "!";
3889 printMetadataIdentifier(MDNames[Kind], Out);
3890 } else
Duncan P. N. Exon Smithd3e4c2a2015-04-24 21:06:21 +00003891 Out << "!<unknown kind #" << Kind << ">";
Duncan P. N. Exon Smith86979272015-04-24 20:59:52 +00003892 Out << ' ';
3893 WriteAsOperandInternal(Out, I.second, &TypePrinter, &Machine, TheModule);
3894 }
3895}
3896
Daniel Maleaded9f932013-05-08 20:38:31 +00003897void AssemblyWriter::writeMDNode(unsigned Slot, const MDNode *Node) {
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +00003898 Out << '!' << Slot << " = ";
Daniel Maleaded9f932013-05-08 20:38:31 +00003899 printMDNodeBody(Node);
Duncan P. N. Exon Smith738889f2015-02-25 22:46:38 +00003900 Out << "\n";
Daniel Maleaded9f932013-05-08 20:38:31 +00003901}
3902
Chris Lattnerbddea6a2009-12-31 02:13:35 +00003903void AssemblyWriter::writeAllMDNodes() {
3904 SmallVector<const MDNode *, 16> Nodes;
Chris Lattnercf4a76e2009-12-31 02:20:11 +00003905 Nodes.resize(Machine.mdn_size());
3906 for (SlotTracker::mdn_iterator I = Machine.mdn_begin(), E = Machine.mdn_end();
3907 I != E; ++I)
Chris Lattnerbddea6a2009-12-31 02:13:35 +00003908 Nodes[I->second] = cast<MDNode>(I->first);
Andrew Trickec4b6e72011-09-30 19:48:58 +00003909
Chris Lattnerbddea6a2009-12-31 02:13:35 +00003910 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Daniel Maleaded9f932013-05-08 20:38:31 +00003911 writeMDNode(i, Nodes[i]);
Chris Lattnerbddea6a2009-12-31 02:13:35 +00003912 }
3913}
3914
3915void AssemblyWriter::printMDNodeBody(const MDNode *Node) {
Dan Gohmana2489d12010-07-20 23:55:01 +00003916 WriteMDNodeBodyInternal(Out, Node, &TypePrinter, &Machine, TheModule);
Chris Lattnerbddea6a2009-12-31 02:13:35 +00003917}
Chris Lattner2f7c9632001-06-06 20:29:01 +00003918
Bill Wendling829b4782013-02-11 08:43:33 +00003919void AssemblyWriter::writeAllAttributeGroups() {
Reid Klecknerc2cb5602017-04-12 00:38:00 +00003920 std::vector<std::pair<AttributeSet, unsigned>> asVec;
Bill Wendling829b4782013-02-11 08:43:33 +00003921 asVec.resize(Machine.as_size());
3922
3923 for (SlotTracker::as_iterator I = Machine.as_begin(), E = Machine.as_end();
3924 I != E; ++I)
3925 asVec[I->second] = *I;
3926
Benjamin Krameraf28e7d2016-06-26 14:10:56 +00003927 for (const auto &I : asVec)
3928 Out << "attributes #" << I.second << " = { "
Reid Klecknerc2cb5602017-04-12 00:38:00 +00003929 << I.first.getAsString(true) << " }\n";
Bill Wendling829b4782013-02-11 08:43:33 +00003930}
3931
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +00003932void AssemblyWriter::printUseListOrder(const UseListOrder &Order) {
3933 bool IsInFunction = Machine.getFunction();
3934 if (IsInFunction)
3935 Out << " ";
3936
3937 Out << "uselistorder";
3938 if (const BasicBlock *BB =
3939 IsInFunction ? nullptr : dyn_cast<BasicBlock>(Order.V)) {
3940 Out << "_bb ";
3941 writeOperand(BB->getParent(), false);
3942 Out << ", ";
3943 writeOperand(BB, false);
3944 } else {
3945 Out << " ";
3946 writeOperand(Order.V, true);
3947 }
3948 Out << ", { ";
3949
3950 assert(Order.Shuffle.size() >= 2 && "Shuffle too small");
3951 Out << Order.Shuffle[0];
3952 for (unsigned I = 1, E = Order.Shuffle.size(); I != E; ++I)
3953 Out << ", " << Order.Shuffle[I];
3954 Out << " }\n";
3955}
3956
3957void AssemblyWriter::printUseLists(const Function *F) {
3958 auto hasMore =
3959 [&]() { return !UseListOrders.empty() && UseListOrders.back().F == F; };
3960 if (!hasMore())
3961 // Nothing to do.
3962 return;
3963
3964 Out << "\n; uselistorder directives\n";
3965 while (hasMore()) {
3966 printUseListOrder(UseListOrders.back());
3967 UseListOrders.pop_back();
3968 }
3969}
3970
Chris Lattner2f7c9632001-06-06 20:29:01 +00003971//===----------------------------------------------------------------------===//
3972// External Interface declarations
3973//===----------------------------------------------------------------------===//
3974
George Burgess IVe1100f52016-02-02 22:46:49 +00003975void Function::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW,
3976 bool ShouldPreserveUseListOrder,
3977 bool IsForDebug) const {
3978 SlotTracker SlotTable(this->getParent());
3979 formatted_raw_ostream OS(ROS);
3980 AssemblyWriter W(OS, SlotTable, this->getParent(), AAW,
3981 IsForDebug,
3982 ShouldPreserveUseListOrder);
3983 W.printFunction(this);
3984}
3985
Duncan P. N. Exon Smithc4f0a322015-04-15 02:12:41 +00003986void Module::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW,
Justin Bognerd7d1a722015-09-27 22:38:50 +00003987 bool ShouldPreserveUseListOrder, bool IsForDebug) const {
Chris Lattnere36fd8a2008-08-19 04:26:57 +00003988 SlotTracker SlotTable(this);
Dan Gohmane2745262009-08-12 17:23:50 +00003989 formatted_raw_ostream OS(ROS);
Justin Bognerd7d1a722015-09-27 22:38:50 +00003990 AssemblyWriter W(OS, SlotTable, this, AAW, IsForDebug,
3991 ShouldPreserveUseListOrder);
Chris Lattnerefb5e392009-12-31 02:23:35 +00003992 W.printModule(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00003993}
3994
Justin Bognerd7d1a722015-09-27 22:38:50 +00003995void NamedMDNode::print(raw_ostream &ROS, bool IsForDebug) const {
Dan Gohman2637cc12010-07-21 23:38:33 +00003996 SlotTracker SlotTable(getParent());
3997 formatted_raw_ostream OS(ROS);
Justin Bognerd7d1a722015-09-27 22:38:50 +00003998 AssemblyWriter W(OS, SlotTable, getParent(), nullptr, IsForDebug);
Dan Gohman2637cc12010-07-21 23:38:33 +00003999 W.printNamedMDNode(this);
4000}
4001
Duncan P. N. Exon Smith0ecff952016-04-20 17:27:44 +00004002void NamedMDNode::print(raw_ostream &ROS, ModuleSlotTracker &MST,
4003 bool IsForDebug) const {
4004 Optional<SlotTracker> LocalST;
4005 SlotTracker *SlotTable;
4006 if (auto *ST = MST.getMachine())
4007 SlotTable = ST;
4008 else {
4009 LocalST.emplace(getParent());
4010 SlotTable = &*LocalST;
4011 }
4012
4013 formatted_raw_ostream OS(ROS);
4014 AssemblyWriter W(OS, *SlotTable, getParent(), nullptr, IsForDebug);
4015 W.printNamedMDNode(this);
4016}
4017
Justin Bognerd7d1a722015-09-27 22:38:50 +00004018void Comdat::print(raw_ostream &ROS, bool /*IsForDebug*/) const {
David Majnemerdad0a642014-06-27 18:19:56 +00004019 PrintLLVMName(ROS, getName(), ComdatPrefix);
4020 ROS << " = comdat ";
4021
4022 switch (getSelectionKind()) {
4023 case Comdat::Any:
4024 ROS << "any";
4025 break;
4026 case Comdat::ExactMatch:
4027 ROS << "exactmatch";
4028 break;
4029 case Comdat::Largest:
4030 ROS << "largest";
4031 break;
4032 case Comdat::NoDuplicates:
4033 ROS << "noduplicates";
4034 break;
4035 case Comdat::SameSize:
4036 ROS << "samesize";
4037 break;
4038 }
4039
4040 ROS << '\n';
4041}
4042
Quentin Colombet774b1ef2016-03-07 22:32:42 +00004043void Type::print(raw_ostream &OS, bool /*IsForDebug*/, bool NoDetails) const {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004044 TypePrinting TP;
4045 TP.print(const_cast<Type*>(this), OS);
Andrew Trickec4b6e72011-09-30 19:48:58 +00004046
Quentin Colombet774b1ef2016-03-07 22:32:42 +00004047 if (NoDetails)
4048 return;
4049
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004050 // If the type is a named struct type, print the body as well.
4051 if (StructType *STy = dyn_cast<StructType>(const_cast<Type*>(this)))
Chris Lattner01beceb2011-08-12 18:07:07 +00004052 if (!STy->isLiteral()) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004053 OS << " = type ";
4054 TP.printStructBody(STy, OS);
4055 }
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00004056}
4057
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +00004058static bool isReferencingMDNode(const Instruction &I) {
4059 if (const auto *CI = dyn_cast<CallInst>(&I))
4060 if (Function *F = CI->getCalledFunction())
Justin Lebar9cbc3012016-07-28 23:58:15 +00004061 if (F->isIntrinsic())
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +00004062 for (auto &Op : I.operands())
4063 if (auto *V = dyn_cast_or_null<MetadataAsValue>(Op))
4064 if (isa<MDNode>(V->getMetadata()))
4065 return true;
4066 return false;
4067}
4068
Justin Bognerd7d1a722015-09-27 22:38:50 +00004069void Value::print(raw_ostream &ROS, bool IsForDebug) const {
Duncan P. N. Exon Smith1f8a99a2015-06-27 00:38:26 +00004070 bool ShouldInitializeAllMetadata = false;
4071 if (auto *I = dyn_cast<Instruction>(this))
4072 ShouldInitializeAllMetadata = isReferencingMDNode(*I);
4073 else if (isa<Function>(this) || isa<MetadataAsValue>(this))
4074 ShouldInitializeAllMetadata = true;
4075
4076 ModuleSlotTracker MST(getModuleFromVal(this), ShouldInitializeAllMetadata);
Justin Bognerd7d1a722015-09-27 22:38:50 +00004077 print(ROS, MST, IsForDebug);
Duncan P. N. Exon Smith1f8a99a2015-06-27 00:38:26 +00004078}
4079
Justin Bognerd7d1a722015-09-27 22:38:50 +00004080void Value::print(raw_ostream &ROS, ModuleSlotTracker &MST,
4081 bool IsForDebug) const {
Dan Gohman12dad632009-08-12 20:56:03 +00004082 formatted_raw_ostream OS(ROS);
Duncan P. N. Exon Smith1f8a99a2015-06-27 00:38:26 +00004083 SlotTracker EmptySlotTable(static_cast<const Module *>(nullptr));
4084 SlotTracker &SlotTable =
4085 MST.getMachine() ? *MST.getMachine() : EmptySlotTable;
4086 auto incorporateFunction = [&](const Function *F) {
4087 if (F)
4088 MST.incorporateFunction(*F);
4089 };
4090
Chris Lattner0c19df42008-08-23 22:23:09 +00004091 if (const Instruction *I = dyn_cast<Instruction>(this)) {
Duncan P. N. Exon Smith1f8a99a2015-06-27 00:38:26 +00004092 incorporateFunction(I->getParent() ? I->getParent()->getParent() : nullptr);
Justin Bognerd7d1a722015-09-27 22:38:50 +00004093 AssemblyWriter W(OS, SlotTable, getModuleFromVal(I), nullptr, IsForDebug);
Chris Lattnerefb5e392009-12-31 02:23:35 +00004094 W.printInstruction(*I);
Chris Lattner0c19df42008-08-23 22:23:09 +00004095 } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(this)) {
Duncan P. N. Exon Smith1f8a99a2015-06-27 00:38:26 +00004096 incorporateFunction(BB->getParent());
Justin Bognerd7d1a722015-09-27 22:38:50 +00004097 AssemblyWriter W(OS, SlotTable, getModuleFromVal(BB), nullptr, IsForDebug);
Chris Lattnerefb5e392009-12-31 02:23:35 +00004098 W.printBasicBlock(BB);
Chris Lattner0c19df42008-08-23 22:23:09 +00004099 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(this)) {
Justin Bognerd7d1a722015-09-27 22:38:50 +00004100 AssemblyWriter W(OS, SlotTable, GV->getParent(), nullptr, IsForDebug);
Chris Lattnerefb5e392009-12-31 02:23:35 +00004101 if (const GlobalVariable *V = dyn_cast<GlobalVariable>(GV))
4102 W.printGlobal(V);
4103 else if (const Function *F = dyn_cast<Function>(GV))
4104 W.printFunction(F);
4105 else
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +00004106 W.printIndirectSymbol(cast<GlobalIndirectSymbol>(GV));
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00004107 } else if (const MetadataAsValue *V = dyn_cast<MetadataAsValue>(this)) {
Duncan P. N. Exon Smith1f8a99a2015-06-27 00:38:26 +00004108 V->getMetadata()->print(ROS, MST, getModuleFromVal(V));
Chris Lattner0c19df42008-08-23 22:23:09 +00004109 } else if (const Constant *C = dyn_cast<Constant>(this)) {
Chris Lattner92c5c122009-02-28 23:20:19 +00004110 TypePrinting TypePrinter;
Chris Lattnere101c442009-02-28 21:26:53 +00004111 TypePrinter.print(C->getType(), OS);
Chris Lattner2ee62d22009-02-28 21:11:05 +00004112 OS << ' ';
Duncan P. N. Exon Smith1f8a99a2015-06-27 00:38:26 +00004113 WriteConstantInternal(OS, C, TypePrinter, MST.getMachine(), nullptr);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00004114 } else if (isa<InlineAsm>(this) || isa<Argument>(this)) {
Duncan P. N. Exon Smith1f8a99a2015-06-27 00:38:26 +00004115 this->printAsOperand(OS, /* PrintType */ true, MST);
Chris Lattner0c19df42008-08-23 22:23:09 +00004116 } else {
Nick Lewyckyad1b3d12014-05-09 00:49:03 +00004117 llvm_unreachable("Unknown value to print out!");
Chris Lattner0c19df42008-08-23 22:23:09 +00004118 }
4119}
4120
Duncan P. N. Exon Smith326921542015-06-26 22:04:20 +00004121/// Print without a type, skipping the TypePrinting object.
4122///
Benjamin Kramerdf005cb2015-08-08 18:27:36 +00004123/// \return \c true iff printing was successful.
Duncan P. N. Exon Smith326921542015-06-26 22:04:20 +00004124static bool printWithoutType(const Value &V, raw_ostream &O,
4125 SlotTracker *Machine, const Module *M) {
4126 if (V.hasName() || isa<GlobalValue>(V) ||
4127 (!isa<Constant>(V) && !isa<MetadataAsValue>(V))) {
4128 WriteAsOperandInternal(O, &V, nullptr, Machine, M);
4129 return true;
Chandler Carruthd48cdbf2014-01-09 02:29:41 +00004130 }
Duncan P. N. Exon Smith326921542015-06-26 22:04:20 +00004131 return false;
4132}
Chandler Carruthd48cdbf2014-01-09 02:29:41 +00004133
Duncan P. N. Exon Smith326921542015-06-26 22:04:20 +00004134static void printAsOperandImpl(const Value &V, raw_ostream &O, bool PrintType,
4135 ModuleSlotTracker &MST) {
Roman Tereshind96de6f2018-03-22 21:29:07 +00004136 TypePrinting TypePrinter(MST.getModule());
Chandler Carruthd48cdbf2014-01-09 02:29:41 +00004137 if (PrintType) {
Duncan P. N. Exon Smith326921542015-06-26 22:04:20 +00004138 TypePrinter.print(V.getType(), O);
Chandler Carruthd48cdbf2014-01-09 02:29:41 +00004139 O << ' ';
4140 }
4141
Duncan P. N. Exon Smith326921542015-06-26 22:04:20 +00004142 WriteAsOperandInternal(O, &V, &TypePrinter, MST.getMachine(),
4143 MST.getModule());
4144}
4145
4146void Value::printAsOperand(raw_ostream &O, bool PrintType,
4147 const Module *M) const {
4148 if (!M)
4149 M = getModuleFromVal(this);
4150
4151 if (!PrintType)
4152 if (printWithoutType(*this, O, nullptr, M))
4153 return;
4154
4155 SlotTracker Machine(
4156 M, /* ShouldInitializeAllMetadata */ isa<MetadataAsValue>(this));
4157 ModuleSlotTracker MST(Machine, M);
4158 printAsOperandImpl(*this, O, PrintType, MST);
4159}
4160
4161void Value::printAsOperand(raw_ostream &O, bool PrintType,
4162 ModuleSlotTracker &MST) const {
4163 if (!PrintType)
4164 if (printWithoutType(*this, O, MST.getMachine(), MST.getModule()))
4165 return;
4166
4167 printAsOperandImpl(*this, O, PrintType, MST);
Chandler Carruthd48cdbf2014-01-09 02:29:41 +00004168}
4169
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +00004170static void printMetadataImpl(raw_ostream &ROS, const Metadata &MD,
Duncan P. N. Exon Smith6529ed42015-06-26 22:28:47 +00004171 ModuleSlotTracker &MST, const Module *M,
4172 bool OnlyAsOperand) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00004173 formatted_raw_ostream OS(ROS);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00004174
Roman Tereshind96de6f2018-03-22 21:29:07 +00004175 TypePrinting TypePrinter(M);
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +00004176
Duncan P. N. Exon Smith6529ed42015-06-26 22:28:47 +00004177 WriteAsOperandInternal(OS, &MD, &TypePrinter, MST.getMachine(), M,
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00004178 /* FromValue */ true);
Duncan P. N. Exon Smith6529ed42015-06-26 22:28:47 +00004179
4180 auto *N = dyn_cast<MDNode>(&MD);
Reid Klecknerb95b4272017-08-30 20:40:36 +00004181 if (OnlyAsOperand || !N || isa<DIExpression>(MD))
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +00004182 return;
4183
4184 OS << " = ";
Duncan P. N. Exon Smith6529ed42015-06-26 22:28:47 +00004185 WriteMDNodeBodyInternal(OS, N, &TypePrinter, MST.getMachine(), M);
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +00004186}
4187
4188void Metadata::printAsOperand(raw_ostream &OS, const Module *M) const {
Duncan P. N. Exon Smith6529ed42015-06-26 22:28:47 +00004189 ModuleSlotTracker MST(M, isa<MDNode>(this));
4190 printMetadataImpl(OS, *this, MST, M, /* OnlyAsOperand */ true);
4191}
4192
4193void Metadata::printAsOperand(raw_ostream &OS, ModuleSlotTracker &MST,
4194 const Module *M) const {
4195 printMetadataImpl(OS, *this, MST, M, /* OnlyAsOperand */ true);
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +00004196}
4197
Justin Bognerd7d1a722015-09-27 22:38:50 +00004198void Metadata::print(raw_ostream &OS, const Module *M,
4199 bool /*IsForDebug*/) const {
Duncan P. N. Exon Smith6529ed42015-06-26 22:28:47 +00004200 ModuleSlotTracker MST(M, isa<MDNode>(this));
4201 printMetadataImpl(OS, *this, MST, M, /* OnlyAsOperand */ false);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00004202}
4203
Duncan P. N. Exon Smith1f8a99a2015-06-27 00:38:26 +00004204void Metadata::print(raw_ostream &OS, ModuleSlotTracker &MST,
Justin Bognerd7d1a722015-09-27 22:38:50 +00004205 const Module *M, bool /*IsForDebug*/) const {
Duncan P. N. Exon Smith1f8a99a2015-06-27 00:38:26 +00004206 printMetadataImpl(OS, *this, MST, M, /* OnlyAsOperand */ false);
4207}
4208
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00004209void ModuleSummaryIndex::print(raw_ostream &ROS, bool IsForDebug) const {
4210 SlotTracker SlotTable(this);
4211 formatted_raw_ostream OS(ROS);
4212 AssemblyWriter W(OS, SlotTable, this, IsForDebug);
4213 W.printModuleSummaryIndex();
4214}
4215
Aaron Ballman615eb472017-10-15 14:32:27 +00004216#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Chris Lattnerdab942552008-08-25 17:03:15 +00004217// Value::dump - allow easy printing of Values from the debugger.
Duncan P. N. Exon Smith89c1eaa2015-02-25 22:08:21 +00004218LLVM_DUMP_METHOD
Justin Bognerd7d1a722015-09-27 22:38:50 +00004219void Value::dump() const { print(dbgs(), /*IsForDebug=*/true); dbgs() << '\n'; }
Reid Spencer52641832004-05-25 18:14:38 +00004220
Chris Lattnerdab942552008-08-25 17:03:15 +00004221// Type::dump - allow easy printing of Types from the debugger.
Duncan P. N. Exon Smith89c1eaa2015-02-25 22:08:21 +00004222LLVM_DUMP_METHOD
Justin Bognerd7d1a722015-09-27 22:38:50 +00004223void Type::dump() const { print(dbgs(), /*IsForDebug=*/true); dbgs() << '\n'; }
Chris Lattner24025262009-02-28 21:05:51 +00004224
Chris Lattnerdab942552008-08-25 17:03:15 +00004225// Module::dump() - Allow printing of Modules from the debugger.
Duncan P. N. Exon Smith89c1eaa2015-02-25 22:08:21 +00004226LLVM_DUMP_METHOD
Justin Bognerd7d1a722015-09-27 22:38:50 +00004227void Module::dump() const {
4228 print(dbgs(), nullptr,
4229 /*ShouldPreserveUseListOrder=*/false, /*IsForDebug=*/true);
4230}
Bill Wendling3681d112011-12-09 23:18:34 +00004231
Adrian Prantl5f8f34e42018-05-01 15:54:18 +00004232// Allow printing of Comdats from the debugger.
Duncan P. N. Exon Smith89c1eaa2015-02-25 22:08:21 +00004233LLVM_DUMP_METHOD
Justin Bognerd7d1a722015-09-27 22:38:50 +00004234void Comdat::dump() const { print(dbgs(), /*IsForDebug=*/true); }
David Majnemerdad0a642014-06-27 18:19:56 +00004235
Bill Wendling3681d112011-12-09 23:18:34 +00004236// NamedMDNode::dump() - Allow printing of NamedMDNodes from the debugger.
Duncan P. N. Exon Smith89c1eaa2015-02-25 22:08:21 +00004237LLVM_DUMP_METHOD
Justin Bognerd7d1a722015-09-27 22:38:50 +00004238void NamedMDNode::dump() const { print(dbgs(), /*IsForDebug=*/true); }
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00004239
Duncan P. N. Exon Smith89c1eaa2015-02-25 22:08:21 +00004240LLVM_DUMP_METHOD
Duncan P. N. Exon Smitha66dc8f2015-03-15 06:53:32 +00004241void Metadata::dump() const { dump(nullptr); }
4242
4243LLVM_DUMP_METHOD
Duncan P. N. Exon Smithd6d70e72015-03-14 20:19:36 +00004244void Metadata::dump(const Module *M) const {
Justin Bognerd7d1a722015-09-27 22:38:50 +00004245 print(dbgs(), M, /*IsForDebug=*/true);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00004246 dbgs() << '\n';
4247}
Teresa Johnson08d5b4e2018-05-26 02:34:13 +00004248
4249// Allow printing of ModuleSummaryIndex from the debugger.
4250LLVM_DUMP_METHOD
4251void ModuleSummaryIndex::dump() const { print(dbgs(), /*IsForDebug=*/true); }
Matthias Braun8c209aa2017-01-28 02:02:38 +00004252#endif