LLVM  10.0.0svn
AsmWriter.cpp
Go to the documentation of this file.
1 //===- AsmWriter.cpp - Printing LLVM as an assembly file ------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This library implements `print` family of functions in classes like
10 // Module, Function, Value, etc. In-memory representation of those classes is
11 // converted to IR strings.
12 //
13 // Note that these routines must be extremely tolerant of various errors in the
14 // LLVM code, because it can be used for debugging transformations.
15 //
16 //===----------------------------------------------------------------------===//
17 
18 #include "llvm/ADT/APFloat.h"
19 #include "llvm/ADT/APInt.h"
20 #include "llvm/ADT/ArrayRef.h"
21 #include "llvm/ADT/DenseMap.h"
22 #include "llvm/ADT/None.h"
23 #include "llvm/ADT/Optional.h"
24 #include "llvm/ADT/STLExtras.h"
25 #include "llvm/ADT/SetVector.h"
26 #include "llvm/ADT/SmallString.h"
27 #include "llvm/ADT/SmallVector.h"
28 #include "llvm/ADT/StringExtras.h"
29 #include "llvm/ADT/StringRef.h"
32 #include "llvm/Config/llvm-config.h"
33 #include "llvm/IR/Argument.h"
35 #include "llvm/IR/Attributes.h"
36 #include "llvm/IR/BasicBlock.h"
37 #include "llvm/IR/CFG.h"
38 #include "llvm/IR/CallingConv.h"
39 #include "llvm/IR/Comdat.h"
40 #include "llvm/IR/Constant.h"
41 #include "llvm/IR/Constants.h"
43 #include "llvm/IR/DerivedTypes.h"
44 #include "llvm/IR/Function.h"
45 #include "llvm/IR/GlobalAlias.h"
46 #include "llvm/IR/GlobalIFunc.h"
48 #include "llvm/IR/GlobalObject.h"
49 #include "llvm/IR/GlobalValue.h"
50 #include "llvm/IR/GlobalVariable.h"
52 #include "llvm/IR/InlineAsm.h"
53 #include "llvm/IR/InstrTypes.h"
54 #include "llvm/IR/Instruction.h"
55 #include "llvm/IR/Instructions.h"
56 #include "llvm/IR/LLVMContext.h"
57 #include "llvm/IR/Metadata.h"
58 #include "llvm/IR/Module.h"
61 #include "llvm/IR/Operator.h"
62 #include "llvm/IR/Statepoint.h"
63 #include "llvm/IR/Type.h"
64 #include "llvm/IR/TypeFinder.h"
65 #include "llvm/IR/Use.h"
66 #include "llvm/IR/UseListOrder.h"
67 #include "llvm/IR/User.h"
68 #include "llvm/IR/Value.h"
70 #include "llvm/Support/Casting.h"
71 #include "llvm/Support/Compiler.h"
72 #include "llvm/Support/Debug.h"
74 #include "llvm/Support/Format.h"
77 #include <algorithm>
78 #include <cassert>
79 #include <cctype>
80 #include <cstddef>
81 #include <cstdint>
82 #include <iterator>
83 #include <memory>
84 #include <string>
85 #include <tuple>
86 #include <utility>
87 #include <vector>
88 
89 using namespace llvm;
90 
91 // Make virtual table appear in this compilation unit.
93 
94 //===----------------------------------------------------------------------===//
95 // Helper Functions
96 //===----------------------------------------------------------------------===//
97 
98 namespace {
99 
100 struct OrderMap {
102 
103  unsigned size() const { return IDs.size(); }
104  std::pair<unsigned, bool> &operator[](const Value *V) { return IDs[V]; }
105 
106  std::pair<unsigned, bool> lookup(const Value *V) const {
107  return IDs.lookup(V);
108  }
109 
110  void index(const Value *V) {
111  // Explicitly sequence get-size and insert-value operations to avoid UB.
112  unsigned ID = IDs.size() + 1;
113  IDs[V].first = ID;
114  }
115 };
116 
117 } // end anonymous namespace
118 
119 static void orderValue(const Value *V, OrderMap &OM) {
120  if (OM.lookup(V).first)
121  return;
122 
123  if (const Constant *C = dyn_cast<Constant>(V))
124  if (C->getNumOperands() && !isa<GlobalValue>(C))
125  for (const Value *Op : C->operands())
126  if (!isa<BasicBlock>(Op) && !isa<GlobalValue>(Op))
127  orderValue(Op, OM);
128 
129  // Note: we cannot cache this lookup above, since inserting into the map
130  // changes the map's size, and thus affects the other IDs.
131  OM.index(V);
132 }
133 
134 static OrderMap orderModule(const Module *M) {
135  // This needs to match the order used by ValueEnumerator::ValueEnumerator()
136  // and ValueEnumerator::incorporateFunction().
137  OrderMap OM;
138 
139  for (const GlobalVariable &G : M->globals()) {
140  if (G.hasInitializer())
141  if (!isa<GlobalValue>(G.getInitializer()))
142  orderValue(G.getInitializer(), OM);
143  orderValue(&G, OM);
144  }
145  for (const GlobalAlias &A : M->aliases()) {
146  if (!isa<GlobalValue>(A.getAliasee()))
147  orderValue(A.getAliasee(), OM);
148  orderValue(&A, OM);
149  }
150  for (const GlobalIFunc &I : M->ifuncs()) {
151  if (!isa<GlobalValue>(I.getResolver()))
152  orderValue(I.getResolver(), OM);
153  orderValue(&I, OM);
154  }
155  for (const Function &F : *M) {
156  for (const Use &U : F.operands())
157  if (!isa<GlobalValue>(U.get()))
158  orderValue(U.get(), OM);
159 
160  orderValue(&F, OM);
161 
162  if (F.isDeclaration())
163  continue;
164 
165  for (const Argument &A : F.args())
166  orderValue(&A, OM);
167  for (const BasicBlock &BB : F) {
168  orderValue(&BB, OM);
169  for (const Instruction &I : BB) {
170  for (const Value *Op : I.operands())
171  if ((isa<Constant>(*Op) && !isa<GlobalValue>(*Op)) ||
172  isa<InlineAsm>(*Op))
173  orderValue(Op, OM);
174  orderValue(&I, OM);
175  }
176  }
177  }
178  return OM;
179 }
180 
181 static void predictValueUseListOrderImpl(const Value *V, const Function *F,
182  unsigned ID, const OrderMap &OM,
183  UseListOrderStack &Stack) {
184  // Predict use-list order for this one.
185  using Entry = std::pair<const Use *, unsigned>;
187  for (const Use &U : V->uses())
188  // Check if this user will be serialized.
189  if (OM.lookup(U.getUser()).first)
190  List.push_back(std::make_pair(&U, List.size()));
191 
192  if (List.size() < 2)
193  // We may have lost some users.
194  return;
195 
196  bool GetsReversed =
197  !isa<GlobalVariable>(V) && !isa<Function>(V) && !isa<BasicBlock>(V);
198  if (auto *BA = dyn_cast<BlockAddress>(V))
199  ID = OM.lookup(BA->getBasicBlock()).first;
200  llvm::sort(List, [&](const Entry &L, const Entry &R) {
201  const Use *LU = L.first;
202  const Use *RU = R.first;
203  if (LU == RU)
204  return false;
205 
206  auto LID = OM.lookup(LU->getUser()).first;
207  auto RID = OM.lookup(RU->getUser()).first;
208 
209  // If ID is 4, then expect: 7 6 5 1 2 3.
210  if (LID < RID) {
211  if (GetsReversed)
212  if (RID <= ID)
213  return true;
214  return false;
215  }
216  if (RID < LID) {
217  if (GetsReversed)
218  if (LID <= ID)
219  return false;
220  return true;
221  }
222 
223  // LID and RID are equal, so we have different operands of the same user.
224  // Assume operands are added in order for all instructions.
225  if (GetsReversed)
226  if (LID <= ID)
227  return LU->getOperandNo() < RU->getOperandNo();
228  return LU->getOperandNo() > RU->getOperandNo();
229  });
230 
231  if (std::is_sorted(
232  List.begin(), List.end(),
233  [](const Entry &L, const Entry &R) { return L.second < R.second; }))
234  // Order is already correct.
235  return;
236 
237  // Store the shuffle.
238  Stack.emplace_back(V, F, List.size());
239  assert(List.size() == Stack.back().Shuffle.size() && "Wrong size");
240  for (size_t I = 0, E = List.size(); I != E; ++I)
241  Stack.back().Shuffle[I] = List[I].second;
242 }
243 
244 static void predictValueUseListOrder(const Value *V, const Function *F,
245  OrderMap &OM, UseListOrderStack &Stack) {
246  auto &IDPair = OM[V];
247  assert(IDPair.first && "Unmapped value");
248  if (IDPair.second)
249  // Already predicted.
250  return;
251 
252  // Do the actual prediction.
253  IDPair.second = true;
254  if (!V->use_empty() && std::next(V->use_begin()) != V->use_end())
255  predictValueUseListOrderImpl(V, F, IDPair.first, OM, Stack);
256 
257  // Recursive descent into constants.
258  if (const Constant *C = dyn_cast<Constant>(V))
259  if (C->getNumOperands()) // Visit GlobalValues.
260  for (const Value *Op : C->operands())
261  if (isa<Constant>(Op)) // Visit GlobalValues.
262  predictValueUseListOrder(Op, F, OM, Stack);
263 }
264 
266  OrderMap OM = orderModule(M);
267 
268  // Use-list orders need to be serialized after all the users have been added
269  // to a value, or else the shuffles will be incomplete. Store them per
270  // function in a stack.
271  //
272  // Aside from function order, the order of values doesn't matter much here.
273  UseListOrderStack Stack;
274 
275  // We want to visit the functions backward now so we can list function-local
276  // constants in the last Function they're used in. Module-level constants
277  // have already been visited above.
278  for (const Function &F : make_range(M->rbegin(), M->rend())) {
279  if (F.isDeclaration())
280  continue;
281  for (const BasicBlock &BB : F)
282  predictValueUseListOrder(&BB, &F, OM, Stack);
283  for (const Argument &A : F.args())
284  predictValueUseListOrder(&A, &F, OM, Stack);
285  for (const BasicBlock &BB : F)
286  for (const Instruction &I : BB)
287  for (const Value *Op : I.operands())
288  if (isa<Constant>(*Op) || isa<InlineAsm>(*Op)) // Visit GlobalValues.
289  predictValueUseListOrder(Op, &F, OM, Stack);
290  for (const BasicBlock &BB : F)
291  for (const Instruction &I : BB)
292  predictValueUseListOrder(&I, &F, OM, Stack);
293  }
294 
295  // Visit globals last.
296  for (const GlobalVariable &G : M->globals())
297  predictValueUseListOrder(&G, nullptr, OM, Stack);
298  for (const Function &F : *M)
299  predictValueUseListOrder(&F, nullptr, OM, Stack);
300  for (const GlobalAlias &A : M->aliases())
301  predictValueUseListOrder(&A, nullptr, OM, Stack);
302  for (const GlobalIFunc &I : M->ifuncs())
303  predictValueUseListOrder(&I, nullptr, OM, Stack);
304  for (const GlobalVariable &G : M->globals())
305  if (G.hasInitializer())
306  predictValueUseListOrder(G.getInitializer(), nullptr, OM, Stack);
307  for (const GlobalAlias &A : M->aliases())
308  predictValueUseListOrder(A.getAliasee(), nullptr, OM, Stack);
309  for (const GlobalIFunc &I : M->ifuncs())
310  predictValueUseListOrder(I.getResolver(), nullptr, OM, Stack);
311  for (const Function &F : *M)
312  for (const Use &U : F.operands())
313  predictValueUseListOrder(U.get(), nullptr, OM, Stack);
314 
315  return Stack;
316 }
317 
318 static const Module *getModuleFromVal(const Value *V) {
319  if (const Argument *MA = dyn_cast<Argument>(V))
320  return MA->getParent() ? MA->getParent()->getParent() : nullptr;
321 
322  if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
323  return BB->getParent() ? BB->getParent()->getParent() : nullptr;
324 
325  if (const Instruction *I = dyn_cast<Instruction>(V)) {
326  const Function *M = I->getParent() ? I->getParent()->getParent() : nullptr;
327  return M ? M->getParent() : nullptr;
328  }
329 
330  if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
331  return GV->getParent();
332 
333  if (const auto *MAV = dyn_cast<MetadataAsValue>(V)) {
334  for (const User *U : MAV->users())
335  if (isa<Instruction>(U))
336  if (const Module *M = getModuleFromVal(U))
337  return M;
338  return nullptr;
339  }
340 
341  return nullptr;
342 }
343 
344 static void PrintCallingConv(unsigned cc, raw_ostream &Out) {
345  switch (cc) {
346  default: Out << "cc" << cc; break;
347  case CallingConv::Fast: Out << "fastcc"; break;
348  case CallingConv::Cold: Out << "coldcc"; break;
349  case CallingConv::WebKit_JS: Out << "webkit_jscc"; break;
350  case CallingConv::AnyReg: Out << "anyregcc"; break;
351  case CallingConv::PreserveMost: Out << "preserve_mostcc"; break;
352  case CallingConv::PreserveAll: Out << "preserve_allcc"; break;
353  case CallingConv::CXX_FAST_TLS: Out << "cxx_fast_tlscc"; break;
354  case CallingConv::GHC: Out << "ghccc"; break;
355  case CallingConv::Tail: Out << "tailcc"; break;
356  case CallingConv::X86_StdCall: Out << "x86_stdcallcc"; break;
357  case CallingConv::X86_FastCall: Out << "x86_fastcallcc"; break;
358  case CallingConv::X86_ThisCall: Out << "x86_thiscallcc"; break;
359  case CallingConv::X86_RegCall: Out << "x86_regcallcc"; break;
360  case CallingConv::X86_VectorCall:Out << "x86_vectorcallcc"; break;
361  case CallingConv::Intel_OCL_BI: Out << "intel_ocl_bicc"; break;
362  case CallingConv::ARM_APCS: Out << "arm_apcscc"; break;
363  case CallingConv::ARM_AAPCS: Out << "arm_aapcscc"; break;
364  case CallingConv::ARM_AAPCS_VFP: Out << "arm_aapcs_vfpcc"; break;
365  case CallingConv::AArch64_VectorCall: Out << "aarch64_vector_pcs"; break;
366  case CallingConv::MSP430_INTR: Out << "msp430_intrcc"; break;
367  case CallingConv::AVR_INTR: Out << "avr_intrcc "; break;
368  case CallingConv::AVR_SIGNAL: Out << "avr_signalcc "; break;
369  case CallingConv::PTX_Kernel: Out << "ptx_kernel"; break;
370  case CallingConv::PTX_Device: Out << "ptx_device"; break;
371  case CallingConv::X86_64_SysV: Out << "x86_64_sysvcc"; break;
372  case CallingConv::Win64: Out << "win64cc"; break;
373  case CallingConv::SPIR_FUNC: Out << "spir_func"; break;
374  case CallingConv::SPIR_KERNEL: Out << "spir_kernel"; break;
375  case CallingConv::Swift: Out << "swiftcc"; break;
376  case CallingConv::X86_INTR: Out << "x86_intrcc"; break;
377  case CallingConv::HHVM: Out << "hhvmcc"; break;
378  case CallingConv::HHVM_C: Out << "hhvm_ccc"; break;
379  case CallingConv::AMDGPU_VS: Out << "amdgpu_vs"; break;
380  case CallingConv::AMDGPU_LS: Out << "amdgpu_ls"; break;
381  case CallingConv::AMDGPU_HS: Out << "amdgpu_hs"; break;
382  case CallingConv::AMDGPU_ES: Out << "amdgpu_es"; break;
383  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;
386  case CallingConv::AMDGPU_KERNEL: Out << "amdgpu_kernel"; break;
387  }
388 }
389 
396 };
397 
399  assert(!Name.empty() && "Cannot get empty name!");
400 
401  // Scan the name to see if it needs quotes first.
402  bool NeedsQuotes = isdigit(static_cast<unsigned char>(Name[0]));
403  if (!NeedsQuotes) {
404  for (unsigned i = 0, e = Name.size(); i != e; ++i) {
405  // 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];
410  if (!isalnum(static_cast<unsigned char>(C)) && C != '-' && C != '.' &&
411  C != '_') {
412  NeedsQuotes = true;
413  break;
414  }
415  }
416  }
417 
418  // If we didn't need any quotes, just write out the name in one blast.
419  if (!NeedsQuotes) {
420  OS << Name;
421  return;
422  }
423 
424  // Okay, we need quotes. Output the quotes and escape any scary characters as
425  // needed.
426  OS << '"';
427  printEscapedString(Name, OS);
428  OS << '"';
429 }
430 
431 /// 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.
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.
456 static void PrintLLVMName(raw_ostream &OS, const Value *V) {
457  PrintLLVMName(OS, V->getName(),
458  isa<GlobalValue>(V) ? GlobalPrefix : LocalPrefix);
459 }
460 
461 namespace {
462 
463 class TypePrinting {
464 public:
465  TypePrinting(const Module *M = nullptr) : DeferredM(M) {}
466 
467  TypePrinting(const TypePrinting &) = delete;
468  TypePrinting &operator=(const TypePrinting &) = delete;
469 
470  /// 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();
477 
478  void print(Type *Ty, raw_ostream &OS);
479 
480  void printStructBody(StructType *Ty, raw_ostream &OS);
481 
482 private:
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.
492 
493  std::vector<StructType *> NumberedTypes;
494 };
495 
496 } // end anonymous namespace
497 
498 TypeFinder &TypePrinting::getNamedTypes() {
499  incorporateTypes();
500  return NamedTypes;
501 }
502 
503 std::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 
521 bool TypePrinting::empty() {
522  incorporateTypes();
523  return NamedTypes.empty() && Type2Number.empty();
524 }
525 
526 void TypePrinting::incorporateTypes() {
527  if (!DeferredM)
528  return;
529 
530  NamedTypes.run(*DeferredM, false);
531  DeferredM = nullptr;
532 
533  // 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;
536 
537  std::vector<StructType*>::iterator NextToUse = NamedTypes.begin(), I, E;
538  for (I = NamedTypes.begin(), E = NamedTypes.end(); I != E; ++I) {
539  StructType *STy = *I;
540 
541  // Ignore anonymous types.
542  if (STy->isLiteral())
543  continue;
544 
545  if (STy->getName().empty())
546  Type2Number[STy] = NextNumber++;
547  else
548  *NextToUse++ = STy;
549  }
550 
551  NamedTypes.erase(NextToUse, NamedTypes.end());
552 }
553 
554 /// 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.
556 void TypePrinting::print(Type *Ty, raw_ostream &OS) {
557  switch (Ty->getTypeID()) {
558  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;
568  case Type::TokenTyID: OS << "token"; return;
569  case Type::IntegerTyID:
570  OS << 'i' << cast<IntegerType>(Ty)->getBitWidth();
571  return;
572 
573  case Type::FunctionTyID: {
574  FunctionType *FTy = cast<FunctionType>(Ty);
575  print(FTy->getReturnType(), OS);
576  OS << " (";
578  E = FTy->param_end(); I != E; ++I) {
579  if (I != FTy->param_begin())
580  OS << ", ";
581  print(*I, OS);
582  }
583  if (FTy->isVarArg()) {
584  if (FTy->getNumParams()) OS << ", ";
585  OS << "...";
586  }
587  OS << ')';
588  return;
589  }
590  case Type::StructTyID: {
591  StructType *STy = cast<StructType>(Ty);
592 
593  if (STy->isLiteral())
594  return printStructBody(STy, OS);
595 
596  if (!STy->getName().empty())
597  return PrintLLVMName(OS, STy->getName(), LocalPrefix);
598 
599  incorporateTypes();
600  const auto I = Type2Number.find(STy);
601  if (I != Type2Number.end())
602  OS << '%' << I->second;
603  else // Not enumerated, print the hex address.
604  OS << "%\"type " << STy << '\"';
605  return;
606  }
607  case Type::PointerTyID: {
608  PointerType *PTy = cast<PointerType>(Ty);
609  print(PTy->getElementType(), OS);
610  if (unsigned AddressSpace = PTy->getAddressSpace())
611  OS << " addrspace(" << AddressSpace << ')';
612  OS << '*';
613  return;
614  }
615  case Type::ArrayTyID: {
616  ArrayType *ATy = cast<ArrayType>(Ty);
617  OS << '[' << ATy->getNumElements() << " x ";
618  print(ATy->getElementType(), OS);
619  OS << ']';
620  return;
621  }
622  case Type::VectorTyID: {
623  VectorType *PTy = cast<VectorType>(Ty);
624  OS << "<";
625  if (PTy->isScalable())
626  OS << "vscale x ";
627  OS << PTy->getNumElements() << " x ";
628  print(PTy->getElementType(), OS);
629  OS << '>';
630  return;
631  }
632  }
633  llvm_unreachable("Invalid TypeID");
634 }
635 
636 void TypePrinting::printStructBody(StructType *STy, raw_ostream &OS) {
637  if (STy->isOpaque()) {
638  OS << "opaque";
639  return;
640  }
641 
642  if (STy->isPacked())
643  OS << '<';
644 
645  if (STy->getNumElements() == 0) {
646  OS << "{}";
647  } else {
648  StructType::element_iterator I = STy->element_begin();
649  OS << "{ ";
650  print(*I++, OS);
651  for (StructType::element_iterator E = STy->element_end(); I != E; ++I) {
652  OS << ", ";
653  print(*I, OS);
654  }
655 
656  OS << " }";
657  }
658  if (STy->isPacked())
659  OS << '>';
660 }
661 
662 namespace llvm {
663 
664 //===----------------------------------------------------------------------===//
665 // SlotTracker Class: Enumerate slot numbers for unnamed values
666 //===----------------------------------------------------------------------===//
667 /// This class provides computation of slot numbers for LLVM Assembly writing.
668 ///
669 class SlotTracker {
670 public:
671  /// ValueMap - A mapping of Values to slot numbers.
672  using ValueMap = DenseMap<const Value *, unsigned>;
673 
674 private:
675  /// TheModule - The module for which we are holding slot numbers.
676  const Module* TheModule;
677 
678  /// TheFunction - The function for which we are holding slot numbers.
679  const Function* TheFunction = nullptr;
680  bool FunctionProcessed = false;
681  bool ShouldInitializeAllMetadata;
682 
683  /// The summary index for which we are holding slot numbers.
684  const ModuleSummaryIndex *TheIndex = nullptr;
685 
686  /// mMap - The slot map for the module level data.
687  ValueMap mMap;
688  unsigned mNext = 0;
689 
690  /// fMap - The slot map for the function level data.
691  ValueMap fMap;
692  unsigned fNext = 0;
693 
694  /// mdnMap - Map for MDNodes.
695  DenseMap<const MDNode*, unsigned> mdnMap;
696  unsigned mdnNext = 0;
697 
698  /// asMap - The slot map for attribute sets.
699  DenseMap<AttributeSet, unsigned> asMap;
700  unsigned asNext = 0;
701 
702  /// ModulePathMap - The slot map for Module paths used in the summary index.
703  StringMap<unsigned> ModulePathMap;
704  unsigned ModulePathNext = 0;
705 
706  /// GUIDMap - The slot map for GUIDs used in the summary index.
707  DenseMap<GlobalValue::GUID, unsigned> GUIDMap;
708  unsigned GUIDNext = 0;
709 
710  /// TypeIdMap - The slot map for type ids used in the summary index.
711  StringMap<unsigned> TypeIdMap;
712  unsigned TypeIdNext = 0;
713 
714 public:
715  /// Construct from a module.
716  ///
717  /// If \c ShouldInitializeAllMetadata, initializes all metadata in all
718  /// functions, giving correct numbering for metadata referenced only from
719  /// within a function (even if no functions have been initialized).
720  explicit SlotTracker(const Module *M,
721  bool ShouldInitializeAllMetadata = false);
722 
723  /// Construct from a function, starting out in incorp state.
724  ///
725  /// If \c ShouldInitializeAllMetadata, initializes all metadata in all
726  /// functions, giving correct numbering for metadata referenced only from
727  /// within a function (even if no functions have been initialized).
728  explicit SlotTracker(const Function *F,
729  bool ShouldInitializeAllMetadata = false);
730 
731  /// Construct from a module summary index.
732  explicit SlotTracker(const ModuleSummaryIndex *Index);
733 
734  SlotTracker(const SlotTracker &) = delete;
735  SlotTracker &operator=(const SlotTracker &) = delete;
736 
737  /// Return the slot number of the specified value in it's type
738  /// plane. If something is not in the SlotTracker, return -1.
739  int getLocalSlot(const Value *V);
740  int getGlobalSlot(const GlobalValue *V);
741  int getMetadataSlot(const MDNode *N);
742  int getAttributeGroupSlot(AttributeSet AS);
743  int getModulePathSlot(StringRef Path);
744  int getGUIDSlot(GlobalValue::GUID GUID);
745  int getTypeIdSlot(StringRef Id);
746 
747  /// If you'd like to deal with a function instead of just a module, use
748  /// this method to get its data into the SlotTracker.
749  void incorporateFunction(const Function *F) {
750  TheFunction = F;
751  FunctionProcessed = false;
752  }
753 
754  const Function *getFunction() const { return TheFunction; }
755 
756  /// After calling incorporateFunction, use this method to remove the
757  /// most recently incorporated function from the SlotTracker. This
758  /// will reset the state of the machine back to just the module contents.
759  void purgeFunction();
760 
761  /// MDNode map iterators.
762  using mdn_iterator = DenseMap<const MDNode*, unsigned>::iterator;
763 
764  mdn_iterator mdn_begin() { return mdnMap.begin(); }
765  mdn_iterator mdn_end() { return mdnMap.end(); }
766  unsigned mdn_size() const { return mdnMap.size(); }
767  bool mdn_empty() const { return mdnMap.empty(); }
768 
769  /// AttributeSet map iterators.
770  using as_iterator = DenseMap<AttributeSet, unsigned>::iterator;
771 
772  as_iterator as_begin() { return asMap.begin(); }
773  as_iterator as_end() { return asMap.end(); }
774  unsigned as_size() const { return asMap.size(); }
775  bool as_empty() const { return asMap.empty(); }
776 
777  /// GUID map iterators.
778  using guid_iterator = DenseMap<GlobalValue::GUID, unsigned>::iterator;
779 
780  /// These functions do the actual initialization.
781  inline void initializeIfNeeded();
782  void initializeIndexIfNeeded();
783 
784  // Implementation Details
785 private:
786  /// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
787  void CreateModuleSlot(const GlobalValue *V);
788 
789  /// CreateMetadataSlot - Insert the specified MDNode* into the slot table.
790  void CreateMetadataSlot(const MDNode *N);
791 
792  /// CreateFunctionSlot - Insert the specified Value* into the slot table.
793  void CreateFunctionSlot(const Value *V);
794 
795  /// Insert the specified AttributeSet into the slot table.
796  void CreateAttributeSetSlot(AttributeSet AS);
797 
798  inline void CreateModulePathSlot(StringRef Path);
799  void CreateGUIDSlot(GlobalValue::GUID GUID);
800  void CreateTypeIdSlot(StringRef Id);
801 
802  /// Add all of the module level global variables (and their initializers)
803  /// and function declarations, but not the contents of those functions.
804  void processModule();
805  void processIndex();
806 
807  /// Add all of the functions arguments, basic blocks, and instructions.
808  void processFunction();
809 
810  /// Add the metadata directly attached to a GlobalObject.
811  void processGlobalObjectMetadata(const GlobalObject &GO);
812 
813  /// Add all of the metadata from a function.
814  void processFunctionMetadata(const Function &F);
815 
816  /// Add all of the metadata from an instruction.
817  void processInstructionMetadata(const Instruction &I);
818 };
819 
820 } // end namespace llvm
821 
822 ModuleSlotTracker::ModuleSlotTracker(SlotTracker &Machine, const Module *M,
823  const Function *F)
824  : M(M), F(F), Machine(&Machine) {}
825 
826 ModuleSlotTracker::ModuleSlotTracker(const Module *M,
827  bool ShouldInitializeAllMetadata)
828  : ShouldCreateStorage(M),
829  ShouldInitializeAllMetadata(ShouldInitializeAllMetadata), M(M) {}
830 
831 ModuleSlotTracker::~ModuleSlotTracker() = default;
832 
833 SlotTracker *ModuleSlotTracker::getMachine() {
834  if (!ShouldCreateStorage)
835  return Machine;
836 
837  ShouldCreateStorage = false;
838  MachineStorage =
839  std::make_unique<SlotTracker>(M, ShouldInitializeAllMetadata);
840  Machine = MachineStorage.get();
841  return Machine;
842 }
843 
844 void ModuleSlotTracker::incorporateFunction(const Function &F) {
845  // Using getMachine() may lazily create the slot tracker.
846  if (!getMachine())
847  return;
848 
849  // Nothing to do if this is the right function already.
850  if (this->F == &F)
851  return;
852  if (this->F)
853  Machine->purgeFunction();
854  Machine->incorporateFunction(&F);
855  this->F = &F;
856 }
857 
858 int ModuleSlotTracker::getLocalSlot(const Value *V) {
859  assert(F && "No function incorporated");
860  return Machine->getLocalSlot(V);
861 }
862 
863 static SlotTracker *createSlotTracker(const Value *V) {
864  if (const Argument *FA = dyn_cast<Argument>(V))
865  return new SlotTracker(FA->getParent());
866 
867  if (const Instruction *I = dyn_cast<Instruction>(V))
868  if (I->getParent())
869  return new SlotTracker(I->getParent()->getParent());
870 
871  if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
872  return new SlotTracker(BB->getParent());
873 
874  if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
875  return new SlotTracker(GV->getParent());
876 
877  if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V))
878  return new SlotTracker(GA->getParent());
879 
880  if (const GlobalIFunc *GIF = dyn_cast<GlobalIFunc>(V))
881  return new SlotTracker(GIF->getParent());
882 
883  if (const Function *Func = dyn_cast<Function>(V))
884  return new SlotTracker(Func);
885 
886  return nullptr;
887 }
888 
889 #if 0
890 #define ST_DEBUG(X) dbgs() << X
891 #else
892 #define ST_DEBUG(X)
893 #endif
894 
895 // Module level constructor. Causes the contents of the Module (sans functions)
896 // to be added to the slot table.
897 SlotTracker::SlotTracker(const Module *M, bool ShouldInitializeAllMetadata)
898  : TheModule(M), ShouldInitializeAllMetadata(ShouldInitializeAllMetadata) {}
899 
900 // Function level constructor. Causes the contents of the Module and the one
901 // function provided to be added to the slot table.
902 SlotTracker::SlotTracker(const Function *F, bool ShouldInitializeAllMetadata)
903  : TheModule(F ? F->getParent() : nullptr), TheFunction(F),
904  ShouldInitializeAllMetadata(ShouldInitializeAllMetadata) {}
905 
906 SlotTracker::SlotTracker(const ModuleSummaryIndex *Index)
907  : TheModule(nullptr), ShouldInitializeAllMetadata(false), TheIndex(Index) {}
908 
909 inline void SlotTracker::initializeIfNeeded() {
910  if (TheModule) {
911  processModule();
912  TheModule = nullptr; ///< Prevent re-processing next time we're called.
913  }
914 
915  if (TheFunction && !FunctionProcessed)
916  processFunction();
917 }
918 
920  if (!TheIndex)
921  return;
922  processIndex();
923  TheIndex = nullptr; ///< Prevent re-processing next time we're called.
924 }
925 
926 // Iterate through all the global variables, functions, and global
927 // variable initializers and create slots for them.
928 void SlotTracker::processModule() {
929  ST_DEBUG("begin processModule!\n");
930 
931  // Add all of the unnamed global variables to the value table.
932  for (const GlobalVariable &Var : TheModule->globals()) {
933  if (!Var.hasName())
934  CreateModuleSlot(&Var);
935  processGlobalObjectMetadata(Var);
936  auto Attrs = Var.getAttributes();
937  if (Attrs.hasAttributes())
938  CreateAttributeSetSlot(Attrs);
939  }
940 
941  for (const GlobalAlias &A : TheModule->aliases()) {
942  if (!A.hasName())
943  CreateModuleSlot(&A);
944  }
945 
946  for (const GlobalIFunc &I : TheModule->ifuncs()) {
947  if (!I.hasName())
948  CreateModuleSlot(&I);
949  }
950 
951  // Add metadata used by named metadata.
952  for (const NamedMDNode &NMD : TheModule->named_metadata()) {
953  for (unsigned i = 0, e = NMD.getNumOperands(); i != e; ++i)
954  CreateMetadataSlot(NMD.getOperand(i));
955  }
956 
957  for (const Function &F : *TheModule) {
958  if (!F.hasName())
959  // Add all the unnamed functions to the table.
960  CreateModuleSlot(&F);
961 
962  if (ShouldInitializeAllMetadata)
963  processFunctionMetadata(F);
964 
965  // Add all the function attributes to the table.
966  // FIXME: Add attributes of other objects?
967  AttributeSet FnAttrs = F.getAttributes().getFnAttributes();
968  if (FnAttrs.hasAttributes())
969  CreateAttributeSetSlot(FnAttrs);
970  }
971 
972  ST_DEBUG("end processModule!\n");
973 }
974 
975 // Process the arguments, basic blocks, and instructions of a function.
976 void SlotTracker::processFunction() {
977  ST_DEBUG("begin processFunction!\n");
978  fNext = 0;
979 
980  // Process function metadata if it wasn't hit at the module-level.
981  if (!ShouldInitializeAllMetadata)
982  processFunctionMetadata(*TheFunction);
983 
984  // Add all the function arguments with no names.
985  for(Function::const_arg_iterator AI = TheFunction->arg_begin(),
986  AE = TheFunction->arg_end(); AI != AE; ++AI)
987  if (!AI->hasName())
988  CreateFunctionSlot(&*AI);
989 
990  ST_DEBUG("Inserting Instructions:\n");
991 
992  // Add all of the basic blocks and instructions with no names.
993  for (auto &BB : *TheFunction) {
994  if (!BB.hasName())
995  CreateFunctionSlot(&BB);
996 
997  for (auto &I : BB) {
998  if (!I.getType()->isVoidTy() && !I.hasName())
999  CreateFunctionSlot(&I);
1000 
1001  // We allow direct calls to any llvm.foo function here, because the
1002  // target may not be linked into the optimizer.
1003  if (const auto *Call = dyn_cast<CallBase>(&I)) {
1004  // Add all the call attributes to the table.
1005  AttributeSet Attrs = Call->getAttributes().getFnAttributes();
1006  if (Attrs.hasAttributes())
1007  CreateAttributeSetSlot(Attrs);
1008  }
1009  }
1010  }
1011 
1012  FunctionProcessed = true;
1013 
1014  ST_DEBUG("end processFunction!\n");
1015 }
1016 
1017 // Iterate through all the GUID in the index and create slots for them.
1018 void SlotTracker::processIndex() {
1019  ST_DEBUG("begin processIndex!\n");
1020  assert(TheIndex);
1021 
1022  // The first block of slots are just the module ids, which start at 0 and are
1023  // assigned consecutively. Since the StringMap iteration order isn't
1024  // guaranteed, use a std::map to order by module ID before assigning slots.
1025  std::map<uint64_t, StringRef> ModuleIdToPathMap;
1026  for (auto &ModPath : TheIndex->modulePaths())
1027  ModuleIdToPathMap[ModPath.second.first] = ModPath.first();
1028  for (auto &ModPair : ModuleIdToPathMap)
1029  CreateModulePathSlot(ModPair.second);
1030 
1031  // Start numbering the GUIDs after the module ids.
1032  GUIDNext = ModulePathNext;
1033 
1034  for (auto &GlobalList : *TheIndex)
1035  CreateGUIDSlot(GlobalList.first);
1036 
1037  // Start numbering the TypeIds after the GUIDs.
1038  TypeIdNext = GUIDNext;
1039 
1040  for (auto TidIter = TheIndex->typeIds().begin();
1041  TidIter != TheIndex->typeIds().end(); TidIter++)
1042  CreateTypeIdSlot(TidIter->second.first);
1043 
1044  for (auto &TId : TheIndex->typeIdCompatibleVtableMap())
1045  CreateGUIDSlot(GlobalValue::getGUID(TId.first));
1046 
1047  ST_DEBUG("end processIndex!\n");
1048 }
1049 
1050 void SlotTracker::processGlobalObjectMetadata(const GlobalObject &GO) {
1052  GO.getAllMetadata(MDs);
1053  for (auto &MD : MDs)
1054  CreateMetadataSlot(MD.second);
1055 }
1056 
1057 void SlotTracker::processFunctionMetadata(const Function &F) {
1058  processGlobalObjectMetadata(F);
1059  for (auto &BB : F) {
1060  for (auto &I : BB)
1061  processInstructionMetadata(I);
1062  }
1063 }
1064 
1065 void SlotTracker::processInstructionMetadata(const Instruction &I) {
1066  // Process metadata used directly by intrinsics.
1067  if (const CallInst *CI = dyn_cast<CallInst>(&I))
1068  if (Function *F = CI->getCalledFunction())
1069  if (F->isIntrinsic())
1070  for (auto &Op : I.operands())
1071  if (auto *V = dyn_cast_or_null<MetadataAsValue>(Op))
1072  if (MDNode *N = dyn_cast<MDNode>(V->getMetadata()))
1073  CreateMetadataSlot(N);
1074 
1075  // Process metadata attached to this instruction.
1077  I.getAllMetadata(MDs);
1078  for (auto &MD : MDs)
1079  CreateMetadataSlot(MD.second);
1080 }
1081 
1082 /// Clean up after incorporating a function. This is the only way to get out of
1083 /// the function incorporation state that affects get*Slot/Create*Slot. Function
1084 /// incorporation state is indicated by TheFunction != 0.
1086  ST_DEBUG("begin purgeFunction!\n");
1087  fMap.clear(); // Simply discard the function level map
1088  TheFunction = nullptr;
1089  FunctionProcessed = false;
1090  ST_DEBUG("end purgeFunction!\n");
1091 }
1092 
1093 /// getGlobalSlot - Get the slot number of a global value.
1095  // Check for uninitialized state and do lazy initialization.
1096  initializeIfNeeded();
1097 
1098  // Find the value in the module map
1099  ValueMap::iterator MI = mMap.find(V);
1100  return MI == mMap.end() ? -1 : (int)MI->second;
1101 }
1102 
1103 /// getMetadataSlot - Get the slot number of a MDNode.
1105  // Check for uninitialized state and do lazy initialization.
1106  initializeIfNeeded();
1107 
1108  // Find the MDNode in the module map
1109  mdn_iterator MI = mdnMap.find(N);
1110  return MI == mdnMap.end() ? -1 : (int)MI->second;
1111 }
1112 
1113 /// getLocalSlot - Get the slot number for a value that is local to a function.
1115  assert(!isa<Constant>(V) && "Can't get a constant or global slot with this!");
1116 
1117  // Check for uninitialized state and do lazy initialization.
1118  initializeIfNeeded();
1119 
1120  ValueMap::iterator FI = fMap.find(V);
1121  return FI == fMap.end() ? -1 : (int)FI->second;
1122 }
1123 
1125  // Check for uninitialized state and do lazy initialization.
1126  initializeIfNeeded();
1127 
1128  // Find the AttributeSet in the module map.
1129  as_iterator AI = asMap.find(AS);
1130  return AI == asMap.end() ? -1 : (int)AI->second;
1131 }
1132 
1134  // Check for uninitialized state and do lazy initialization.
1135  initializeIndexIfNeeded();
1136 
1137  // Find the Module path in the map
1138  auto I = ModulePathMap.find(Path);
1139  return I == ModulePathMap.end() ? -1 : (int)I->second;
1140 }
1141 
1143  // Check for uninitialized state and do lazy initialization.
1144  initializeIndexIfNeeded();
1145 
1146  // Find the GUID in the map
1147  guid_iterator I = GUIDMap.find(GUID);
1148  return I == GUIDMap.end() ? -1 : (int)I->second;
1149 }
1150 
1152  // Check for uninitialized state and do lazy initialization.
1153  initializeIndexIfNeeded();
1154 
1155  // Find the TypeId string in the map
1156  auto I = TypeIdMap.find(Id);
1157  return I == TypeIdMap.end() ? -1 : (int)I->second;
1158 }
1159 
1160 /// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
1161 void SlotTracker::CreateModuleSlot(const GlobalValue *V) {
1162  assert(V && "Can't insert a null Value into SlotTracker!");
1163  assert(!V->getType()->isVoidTy() && "Doesn't need a slot!");
1164  assert(!V->hasName() && "Doesn't need a slot!");
1165 
1166  unsigned DestSlot = mNext++;
1167  mMap[V] = DestSlot;
1168 
1169  ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" <<
1170  DestSlot << " [");
1171  // G = Global, F = Function, A = Alias, I = IFunc, o = other
1172  ST_DEBUG((isa<GlobalVariable>(V) ? 'G' :
1173  (isa<Function>(V) ? 'F' :
1174  (isa<GlobalAlias>(V) ? 'A' :
1175  (isa<GlobalIFunc>(V) ? 'I' : 'o')))) << "]\n");
1176 }
1177 
1178 /// CreateSlot - Create a new slot for the specified value if it has no name.
1179 void SlotTracker::CreateFunctionSlot(const Value *V) {
1180  assert(!V->getType()->isVoidTy() && !V->hasName() && "Doesn't need a slot!");
1181 
1182  unsigned DestSlot = fNext++;
1183  fMap[V] = DestSlot;
1184 
1185  // G = Global, F = Function, o = other
1186  ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" <<
1187  DestSlot << " [o]\n");
1188 }
1189 
1190 /// CreateModuleSlot - Insert the specified MDNode* into the slot table.
1191 void SlotTracker::CreateMetadataSlot(const MDNode *N) {
1192  assert(N && "Can't insert a null Value into SlotTracker!");
1193 
1194  // Don't make slots for DIExpressions. We just print them inline everywhere.
1195  if (isa<DIExpression>(N))
1196  return;
1197 
1198  unsigned DestSlot = mdnNext;
1199  if (!mdnMap.insert(std::make_pair(N, DestSlot)).second)
1200  return;
1201  ++mdnNext;
1202 
1203  // Recursively add any MDNodes referenced by operands.
1204  for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1205  if (const MDNode *Op = dyn_cast_or_null<MDNode>(N->getOperand(i)))
1206  CreateMetadataSlot(Op);
1207 }
1208 
1209 void SlotTracker::CreateAttributeSetSlot(AttributeSet AS) {
1210  assert(AS.hasAttributes() && "Doesn't need a slot!");
1211 
1212  as_iterator I = asMap.find(AS);
1213  if (I != asMap.end())
1214  return;
1215 
1216  unsigned DestSlot = asNext++;
1217  asMap[AS] = DestSlot;
1218 }
1219 
1220 /// Create a new slot for the specified Module
1221 void SlotTracker::CreateModulePathSlot(StringRef Path) {
1222  ModulePathMap[Path] = ModulePathNext++;
1223 }
1224 
1225 /// Create a new slot for the specified GUID
1226 void SlotTracker::CreateGUIDSlot(GlobalValue::GUID GUID) {
1227  GUIDMap[GUID] = GUIDNext++;
1228 }
1229 
1230 /// Create a new slot for the specified Id
1231 void SlotTracker::CreateTypeIdSlot(StringRef Id) {
1232  TypeIdMap[Id] = TypeIdNext++;
1233 }
1234 
1235 //===----------------------------------------------------------------------===//
1236 // AsmWriter Implementation
1237 //===----------------------------------------------------------------------===//
1238 
1239 static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
1240  TypePrinting *TypePrinter,
1242  const Module *Context);
1243 
1244 static void WriteAsOperandInternal(raw_ostream &Out, const Metadata *MD,
1245  TypePrinting *TypePrinter,
1246  SlotTracker *Machine, const Module *Context,
1247  bool FromValue = false);
1248 
1249 static void WriteOptimizationInfo(raw_ostream &Out, const User *U) {
1250  if (const FPMathOperator *FPO = dyn_cast<const FPMathOperator>(U)) {
1251  // 'Fast' is an abbreviation for all fast-math-flags.
1252  if (FPO->isFast())
1253  Out << " fast";
1254  else {
1255  if (FPO->hasAllowReassoc())
1256  Out << " reassoc";
1257  if (FPO->hasNoNaNs())
1258  Out << " nnan";
1259  if (FPO->hasNoInfs())
1260  Out << " ninf";
1261  if (FPO->hasNoSignedZeros())
1262  Out << " nsz";
1263  if (FPO->hasAllowReciprocal())
1264  Out << " arcp";
1265  if (FPO->hasAllowContract())
1266  Out << " contract";
1267  if (FPO->hasApproxFunc())
1268  Out << " afn";
1269  }
1270  }
1271 
1272  if (const OverflowingBinaryOperator *OBO =
1273  dyn_cast<OverflowingBinaryOperator>(U)) {
1274  if (OBO->hasNoUnsignedWrap())
1275  Out << " nuw";
1276  if (OBO->hasNoSignedWrap())
1277  Out << " nsw";
1278  } else if (const PossiblyExactOperator *Div =
1279  dyn_cast<PossiblyExactOperator>(U)) {
1280  if (Div->isExact())
1281  Out << " exact";
1282  } else if (const GEPOperator *GEP = dyn_cast<GEPOperator>(U)) {
1283  if (GEP->isInBounds())
1284  Out << " inbounds";
1285  }
1286 }
1287 
1288 static void WriteConstantInternal(raw_ostream &Out, const Constant *CV,
1289  TypePrinting &TypePrinter,
1291  const Module *Context) {
1292  if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
1293  if (CI->getType()->isIntegerTy(1)) {
1294  Out << (CI->getZExtValue() ? "true" : "false");
1295  return;
1296  }
1297  Out << CI->getValue();
1298  return;
1299  }
1300 
1301  if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
1302  const APFloat &APF = CFP->getValueAPF();
1303  if (&APF.getSemantics() == &APFloat::IEEEsingle() ||
1304  &APF.getSemantics() == &APFloat::IEEEdouble()) {
1305  // We would like to output the FP constant value in exponential notation,
1306  // but we cannot do this if doing so will lose precision. Check here to
1307  // make sure that we only output it in exponential format if we can parse
1308  // the value back and get the same value.
1309  //
1310  bool ignored;
1311  bool isDouble = &APF.getSemantics() == &APFloat::IEEEdouble();
1312  bool isInf = APF.isInfinity();
1313  bool isNaN = APF.isNaN();
1314  if (!isInf && !isNaN) {
1315  double Val = isDouble ? APF.convertToDouble() : APF.convertToFloat();
1317  APF.toString(StrVal, 6, 0, false);
1318  // Check to make sure that the stringized number is not some string like
1319  // "Inf" or NaN, that atof will accept, but the lexer will not. Check
1320  // that the string matches the "[-+]?[0-9]" regex.
1321  //
1322  assert(((StrVal[0] >= '0' && StrVal[0] <= '9') ||
1323  ((StrVal[0] == '-' || StrVal[0] == '+') &&
1324  (StrVal[1] >= '0' && StrVal[1] <= '9'))) &&
1325  "[-+]?[0-9] regex does not match!");
1326  // Reparse stringized version!
1327  if (APFloat(APFloat::IEEEdouble(), StrVal).convertToDouble() == Val) {
1328  Out << StrVal;
1329  return;
1330  }
1331  }
1332  // Otherwise we could not reparse it to exactly the same value, so we must
1333  // output the string in hexadecimal format! Note that loading and storing
1334  // floating point types changes the bits of NaNs on some hosts, notably
1335  // x86, so we must not use these types.
1336  static_assert(sizeof(double) == sizeof(uint64_t),
1337  "assuming that double is 64 bits!");
1338  APFloat apf = APF;
1339  // Floats are represented in ASCII IR as double, convert.
1340  if (!isDouble)
1342  &ignored);
1343  Out << format_hex(apf.bitcastToAPInt().getZExtValue(), 0, /*Upper=*/true);
1344  return;
1345  }
1346 
1347  // Either half, or some form of long double.
1348  // These appear as a magic letter identifying the type, then a
1349  // fixed number of hex digits.
1350  Out << "0x";
1351  APInt API = APF.bitcastToAPInt();
1352  if (&APF.getSemantics() == &APFloat::x87DoubleExtended()) {
1353  Out << 'K';
1354  Out << format_hex_no_prefix(API.getHiBits(16).getZExtValue(), 4,
1355  /*Upper=*/true);
1356  Out << format_hex_no_prefix(API.getLoBits(64).getZExtValue(), 16,
1357  /*Upper=*/true);
1358  return;
1359  } else if (&APF.getSemantics() == &APFloat::IEEEquad()) {
1360  Out << 'L';
1361  Out << format_hex_no_prefix(API.getLoBits(64).getZExtValue(), 16,
1362  /*Upper=*/true);
1363  Out << format_hex_no_prefix(API.getHiBits(64).getZExtValue(), 16,
1364  /*Upper=*/true);
1365  } else if (&APF.getSemantics() == &APFloat::PPCDoubleDouble()) {
1366  Out << 'M';
1367  Out << format_hex_no_prefix(API.getLoBits(64).getZExtValue(), 16,
1368  /*Upper=*/true);
1369  Out << format_hex_no_prefix(API.getHiBits(64).getZExtValue(), 16,
1370  /*Upper=*/true);
1371  } else if (&APF.getSemantics() == &APFloat::IEEEhalf()) {
1372  Out << 'H';
1373  Out << format_hex_no_prefix(API.getZExtValue(), 4,
1374  /*Upper=*/true);
1375  } else
1376  llvm_unreachable("Unsupported floating point type");
1377  return;
1378  }
1379 
1380  if (isa<ConstantAggregateZero>(CV)) {
1381  Out << "zeroinitializer";
1382  return;
1383  }
1384 
1385  if (const BlockAddress *BA = dyn_cast<BlockAddress>(CV)) {
1386  Out << "blockaddress(";
1387  WriteAsOperandInternal(Out, BA->getFunction(), &TypePrinter, Machine,
1388  Context);
1389  Out << ", ";
1390  WriteAsOperandInternal(Out, BA->getBasicBlock(), &TypePrinter, Machine,
1391  Context);
1392  Out << ")";
1393  return;
1394  }
1395 
1396  if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
1397  Type *ETy = CA->getType()->getElementType();
1398  Out << '[';
1399  TypePrinter.print(ETy, Out);
1400  Out << ' ';
1401  WriteAsOperandInternal(Out, CA->getOperand(0),
1402  &TypePrinter, Machine,
1403  Context);
1404  for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
1405  Out << ", ";
1406  TypePrinter.print(ETy, Out);
1407  Out << ' ';
1408  WriteAsOperandInternal(Out, CA->getOperand(i), &TypePrinter, Machine,
1409  Context);
1410  }
1411  Out << ']';
1412  return;
1413  }
1414 
1415  if (const ConstantDataArray *CA = dyn_cast<ConstantDataArray>(CV)) {
1416  // As a special case, print the array as a string if it is an array of
1417  // i8 with ConstantInt values.
1418  if (CA->isString()) {
1419  Out << "c\"";
1420  printEscapedString(CA->getAsString(), Out);
1421  Out << '"';
1422  return;
1423  }
1424 
1425  Type *ETy = CA->getType()->getElementType();
1426  Out << '[';
1427  TypePrinter.print(ETy, Out);
1428  Out << ' ';
1429  WriteAsOperandInternal(Out, CA->getElementAsConstant(0),
1430  &TypePrinter, Machine,
1431  Context);
1432  for (unsigned i = 1, e = CA->getNumElements(); i != e; ++i) {
1433  Out << ", ";
1434  TypePrinter.print(ETy, Out);
1435  Out << ' ';
1436  WriteAsOperandInternal(Out, CA->getElementAsConstant(i), &TypePrinter,
1437  Machine, Context);
1438  }
1439  Out << ']';
1440  return;
1441  }
1442 
1443  if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
1444  if (CS->getType()->isPacked())
1445  Out << '<';
1446  Out << '{';
1447  unsigned N = CS->getNumOperands();
1448  if (N) {
1449  Out << ' ';
1450  TypePrinter.print(CS->getOperand(0)->getType(), Out);
1451  Out << ' ';
1452 
1453  WriteAsOperandInternal(Out, CS->getOperand(0), &TypePrinter, Machine,
1454  Context);
1455 
1456  for (unsigned i = 1; i < N; i++) {
1457  Out << ", ";
1458  TypePrinter.print(CS->getOperand(i)->getType(), Out);
1459  Out << ' ';
1460 
1461  WriteAsOperandInternal(Out, CS->getOperand(i), &TypePrinter, Machine,
1462  Context);
1463  }
1464  Out << ' ';
1465  }
1466 
1467  Out << '}';
1468  if (CS->getType()->isPacked())
1469  Out << '>';
1470  return;
1471  }
1472 
1473  if (isa<ConstantVector>(CV) || isa<ConstantDataVector>(CV)) {
1474  Type *ETy = CV->getType()->getVectorElementType();
1475  Out << '<';
1476  TypePrinter.print(ETy, Out);
1477  Out << ' ';
1478  WriteAsOperandInternal(Out, CV->getAggregateElement(0U), &TypePrinter,
1479  Machine, Context);
1480  for (unsigned i = 1, e = CV->getType()->getVectorNumElements(); i != e;++i){
1481  Out << ", ";
1482  TypePrinter.print(ETy, Out);
1483  Out << ' ';
1484  WriteAsOperandInternal(Out, CV->getAggregateElement(i), &TypePrinter,
1485  Machine, Context);
1486  }
1487  Out << '>';
1488  return;
1489  }
1490 
1491  if (isa<ConstantPointerNull>(CV)) {
1492  Out << "null";
1493  return;
1494  }
1495 
1496  if (isa<ConstantTokenNone>(CV)) {
1497  Out << "none";
1498  return;
1499  }
1500 
1501  if (isa<UndefValue>(CV)) {
1502  Out << "undef";
1503  return;
1504  }
1505 
1506  if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
1507  Out << CE->getOpcodeName();
1508  WriteOptimizationInfo(Out, CE);
1509  if (CE->isCompare())
1510  Out << ' ' << CmpInst::getPredicateName(
1511  static_cast<CmpInst::Predicate>(CE->getPredicate()));
1512  Out << " (";
1513 
1514  Optional<unsigned> InRangeOp;
1515  if (const GEPOperator *GEP = dyn_cast<GEPOperator>(CE)) {
1516  TypePrinter.print(GEP->getSourceElementType(), Out);
1517  Out << ", ";
1518  InRangeOp = GEP->getInRangeIndex();
1519  if (InRangeOp)
1520  ++*InRangeOp;
1521  }
1522 
1523  for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) {
1524  if (InRangeOp && unsigned(OI - CE->op_begin()) == *InRangeOp)
1525  Out << "inrange ";
1526  TypePrinter.print((*OI)->getType(), Out);
1527  Out << ' ';
1528  WriteAsOperandInternal(Out, *OI, &TypePrinter, Machine, Context);
1529  if (OI+1 != CE->op_end())
1530  Out << ", ";
1531  }
1532 
1533  if (CE->hasIndices()) {
1534  ArrayRef<unsigned> Indices = CE->getIndices();
1535  for (unsigned i = 0, e = Indices.size(); i != e; ++i)
1536  Out << ", " << Indices[i];
1537  }
1538 
1539  if (CE->isCast()) {
1540  Out << " to ";
1541  TypePrinter.print(CE->getType(), Out);
1542  }
1543 
1544  Out << ')';
1545  return;
1546  }
1547 
1548  Out << "<placeholder or erroneous Constant>";
1549 }
1550 
1551 static void writeMDTuple(raw_ostream &Out, const MDTuple *Node,
1552  TypePrinting *TypePrinter, SlotTracker *Machine,
1553  const Module *Context) {
1554  Out << "!{";
1555  for (unsigned mi = 0, me = Node->getNumOperands(); mi != me; ++mi) {
1556  const Metadata *MD = Node->getOperand(mi);
1557  if (!MD)
1558  Out << "null";
1559  else if (auto *MDV = dyn_cast<ValueAsMetadata>(MD)) {
1560  Value *V = MDV->getValue();
1561  TypePrinter->print(V->getType(), Out);
1562  Out << ' ';
1563  WriteAsOperandInternal(Out, V, TypePrinter, Machine, Context);
1564  } else {
1565  WriteAsOperandInternal(Out, MD, TypePrinter, Machine, Context);
1566  }
1567  if (mi + 1 != me)
1568  Out << ", ";
1569  }
1570 
1571  Out << "}";
1572 }
1573 
1574 namespace {
1575 
1576 struct FieldSeparator {
1577  bool Skip = true;
1578  const char *Sep;
1579 
1580  FieldSeparator(const char *Sep = ", ") : Sep(Sep) {}
1581 };
1582 
1583 raw_ostream &operator<<(raw_ostream &OS, FieldSeparator &FS) {
1584  if (FS.Skip) {
1585  FS.Skip = false;
1586  return OS;
1587  }
1588  return OS << FS.Sep;
1589 }
1590 
1591 struct MDFieldPrinter {
1592  raw_ostream &Out;
1593  FieldSeparator FS;
1594  TypePrinting *TypePrinter = nullptr;
1595  SlotTracker *Machine = nullptr;
1596  const Module *Context = nullptr;
1597 
1598  explicit MDFieldPrinter(raw_ostream &Out) : Out(Out) {}
1599  MDFieldPrinter(raw_ostream &Out, TypePrinting *TypePrinter,
1600  SlotTracker *Machine, const Module *Context)
1601  : Out(Out), TypePrinter(TypePrinter), Machine(Machine), Context(Context) {
1602  }
1603 
1604  void printTag(const DINode *N);
1605  void printMacinfoType(const DIMacroNode *N);
1606  void printChecksum(const DIFile::ChecksumInfo<StringRef> &N);
1607  void printString(StringRef Name, StringRef Value,
1608  bool ShouldSkipEmpty = true);
1609  void printMetadata(StringRef Name, const Metadata *MD,
1610  bool ShouldSkipNull = true);
1611  template <class IntTy>
1612  void printInt(StringRef Name, IntTy Int, bool ShouldSkipZero = true);
1613  void printBool(StringRef Name, bool Value, Optional<bool> Default = None);
1614  void printDIFlags(StringRef Name, DINode::DIFlags Flags);
1615  void printDISPFlags(StringRef Name, DISubprogram::DISPFlags Flags);
1616  template <class IntTy, class Stringifier>
1617  void printDwarfEnum(StringRef Name, IntTy Value, Stringifier toString,
1618  bool ShouldSkipZero = true);
1619  void printEmissionKind(StringRef Name, DICompileUnit::DebugEmissionKind EK);
1620  void printNameTableKind(StringRef Name,
1621  DICompileUnit::DebugNameTableKind NTK);
1622 };
1623 
1624 } // end anonymous namespace
1625 
1626 void MDFieldPrinter::printTag(const DINode *N) {
1627  Out << FS << "tag: ";
1628  auto Tag = dwarf::TagString(N->getTag());
1629  if (!Tag.empty())
1630  Out << Tag;
1631  else
1632  Out << N->getTag();
1633 }
1634 
1635 void MDFieldPrinter::printMacinfoType(const DIMacroNode *N) {
1636  Out << FS << "type: ";
1637  auto Type = dwarf::MacinfoString(N->getMacinfoType());
1638  if (!Type.empty())
1639  Out << Type;
1640  else
1641  Out << N->getMacinfoType();
1642 }
1643 
1644 void MDFieldPrinter::printChecksum(
1645  const DIFile::ChecksumInfo<StringRef> &Checksum) {
1646  Out << FS << "checksumkind: " << Checksum.getKindAsString();
1647  printString("checksum", Checksum.Value, /* ShouldSkipEmpty */ false);
1648 }
1649 
1650 void MDFieldPrinter::printString(StringRef Name, StringRef Value,
1651  bool ShouldSkipEmpty) {
1652  if (ShouldSkipEmpty && Value.empty())
1653  return;
1654 
1655  Out << FS << Name << ": \"";
1656  printEscapedString(Value, Out);
1657  Out << "\"";
1658 }
1659 
1660 static void writeMetadataAsOperand(raw_ostream &Out, const Metadata *MD,
1661  TypePrinting *TypePrinter,
1662  SlotTracker *Machine,
1663  const Module *Context) {
1664  if (!MD) {
1665  Out << "null";
1666  return;
1667  }
1668  WriteAsOperandInternal(Out, MD, TypePrinter, Machine, Context);
1669 }
1670 
1671 void MDFieldPrinter::printMetadata(StringRef Name, const Metadata *MD,
1672  bool ShouldSkipNull) {
1673  if (ShouldSkipNull && !MD)
1674  return;
1675 
1676  Out << FS << Name << ": ";
1677  writeMetadataAsOperand(Out, MD, TypePrinter, Machine, Context);
1678 }
1679 
1680 template <class IntTy>
1681 void MDFieldPrinter::printInt(StringRef Name, IntTy Int, bool ShouldSkipZero) {
1682  if (ShouldSkipZero && !Int)
1683  return;
1684 
1685  Out << FS << Name << ": " << Int;
1686 }
1687 
1688 void MDFieldPrinter::printBool(StringRef Name, bool Value,
1689  Optional<bool> Default) {
1690  if (Default && Value == *Default)
1691  return;
1692  Out << FS << Name << ": " << (Value ? "true" : "false");
1693 }
1694 
1695 void MDFieldPrinter::printDIFlags(StringRef Name, DINode::DIFlags Flags) {
1696  if (!Flags)
1697  return;
1698 
1699  Out << FS << Name << ": ";
1700 
1701  SmallVector<DINode::DIFlags, 8> SplitFlags;
1702  auto Extra = DINode::splitFlags(Flags, SplitFlags);
1703 
1704  FieldSeparator FlagsFS(" | ");
1705  for (auto F : SplitFlags) {
1706  auto StringF = DINode::getFlagString(F);
1707  assert(!StringF.empty() && "Expected valid flag");
1708  Out << FlagsFS << StringF;
1709  }
1710  if (Extra || SplitFlags.empty())
1711  Out << FlagsFS << Extra;
1712 }
1713 
1714 void MDFieldPrinter::printDISPFlags(StringRef Name,
1715  DISubprogram::DISPFlags Flags) {
1716  // Always print this field, because no flags in the IR at all will be
1717  // interpreted as old-style isDefinition: true.
1718  Out << FS << Name << ": ";
1719 
1720  if (!Flags) {
1721  Out << 0;
1722  return;
1723  }
1724 
1725  SmallVector<DISubprogram::DISPFlags, 8> SplitFlags;
1726  auto Extra = DISubprogram::splitFlags(Flags, SplitFlags);
1727 
1728  FieldSeparator FlagsFS(" | ");
1729  for (auto F : SplitFlags) {
1730  auto StringF = DISubprogram::getFlagString(F);
1731  assert(!StringF.empty() && "Expected valid flag");
1732  Out << FlagsFS << StringF;
1733  }
1734  if (Extra || SplitFlags.empty())
1735  Out << FlagsFS << Extra;
1736 }
1737 
1738 void MDFieldPrinter::printEmissionKind(StringRef Name,
1739  DICompileUnit::DebugEmissionKind EK) {
1740  Out << FS << Name << ": " << DICompileUnit::emissionKindString(EK);
1741 }
1742 
1743 void MDFieldPrinter::printNameTableKind(StringRef Name,
1744  DICompileUnit::DebugNameTableKind NTK) {
1745  if (NTK == DICompileUnit::DebugNameTableKind::Default)
1746  return;
1747  Out << FS << Name << ": " << DICompileUnit::nameTableKindString(NTK);
1748 }
1749 
1750 template <class IntTy, class Stringifier>
1751 void MDFieldPrinter::printDwarfEnum(StringRef Name, IntTy Value,
1752  Stringifier toString, bool ShouldSkipZero) {
1753  if (!Value)
1754  return;
1755 
1756  Out << FS << Name << ": ";
1757  auto S = toString(Value);
1758  if (!S.empty())
1759  Out << S;
1760  else
1761  Out << Value;
1762 }
1763 
1764 static void writeGenericDINode(raw_ostream &Out, const GenericDINode *N,
1765  TypePrinting *TypePrinter, SlotTracker *Machine,
1766  const Module *Context) {
1767  Out << "!GenericDINode(";
1768  MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1769  Printer.printTag(N);
1770  Printer.printString("header", N->getHeader());
1771  if (N->getNumDwarfOperands()) {
1772  Out << Printer.FS << "operands: {";
1773  FieldSeparator IFS;
1774  for (auto &I : N->dwarf_operands()) {
1775  Out << IFS;
1776  writeMetadataAsOperand(Out, I, TypePrinter, Machine, Context);
1777  }
1778  Out << "}";
1779  }
1780  Out << ")";
1781 }
1782 
1783 static void writeDILocation(raw_ostream &Out, const DILocation *DL,
1784  TypePrinting *TypePrinter, SlotTracker *Machine,
1785  const Module *Context) {
1786  Out << "!DILocation(";
1787  MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1788  // Always output the line, since 0 is a relevant and important value for it.
1789  Printer.printInt("line", DL->getLine(), /* ShouldSkipZero */ false);
1790  Printer.printInt("column", DL->getColumn());
1791  Printer.printMetadata("scope", DL->getRawScope(), /* ShouldSkipNull */ false);
1792  Printer.printMetadata("inlinedAt", DL->getRawInlinedAt());
1793  Printer.printBool("isImplicitCode", DL->isImplicitCode(),
1794  /* Default */ false);
1795  Out << ")";
1796 }
1797 
1798 static void writeDISubrange(raw_ostream &Out, const DISubrange *N,
1799  TypePrinting *TypePrinter, SlotTracker *Machine,
1800  const Module *Context) {
1801  Out << "!DISubrange(";
1802  MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1803  if (auto *CE = N->getCount().dyn_cast<ConstantInt*>())
1804  Printer.printInt("count", CE->getSExtValue(), /* ShouldSkipZero */ false);
1805  else
1806  Printer.printMetadata("count", N->getCount().dyn_cast<DIVariable*>(),
1807  /*ShouldSkipNull */ false);
1808  Printer.printInt("lowerBound", N->getLowerBound());
1809  Out << ")";
1810 }
1811 
1812 static void writeDIEnumerator(raw_ostream &Out, const DIEnumerator *N,
1813  TypePrinting *, SlotTracker *, const Module *) {
1814  Out << "!DIEnumerator(";
1815  MDFieldPrinter Printer(Out);
1816  Printer.printString("name", N->getName(), /* ShouldSkipEmpty */ false);
1817  if (N->isUnsigned()) {
1818  auto Value = static_cast<uint64_t>(N->getValue());
1819  Printer.printInt("value", Value, /* ShouldSkipZero */ false);
1820  Printer.printBool("isUnsigned", true);
1821  } else {
1822  Printer.printInt("value", N->getValue(), /* ShouldSkipZero */ false);
1823  }
1824  Out << ")";
1825 }
1826 
1827 static void writeDIBasicType(raw_ostream &Out, const DIBasicType *N,
1828  TypePrinting *, SlotTracker *, const Module *) {
1829  Out << "!DIBasicType(";
1830  MDFieldPrinter Printer(Out);
1831  if (N->getTag() != dwarf::DW_TAG_base_type)
1832  Printer.printTag(N);
1833  Printer.printString("name", N->getName());
1834  Printer.printInt("size", N->getSizeInBits());
1835  Printer.printInt("align", N->getAlignInBits());
1836  Printer.printDwarfEnum("encoding", N->getEncoding(),
1837  dwarf::AttributeEncodingString);
1838  Printer.printDIFlags("flags", N->getFlags());
1839  Out << ")";
1840 }
1841 
1842 static void writeDIDerivedType(raw_ostream &Out, const DIDerivedType *N,
1843  TypePrinting *TypePrinter, SlotTracker *Machine,
1844  const Module *Context) {
1845  Out << "!DIDerivedType(";
1846  MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1847  Printer.printTag(N);
1848  Printer.printString("name", N->getName());
1849  Printer.printMetadata("scope", N->getRawScope());
1850  Printer.printMetadata("file", N->getRawFile());
1851  Printer.printInt("line", N->getLine());
1852  Printer.printMetadata("baseType", N->getRawBaseType(),
1853  /* ShouldSkipNull */ false);
1854  Printer.printInt("size", N->getSizeInBits());
1855  Printer.printInt("align", N->getAlignInBits());
1856  Printer.printInt("offset", N->getOffsetInBits());
1857  Printer.printDIFlags("flags", N->getFlags());
1858  Printer.printMetadata("extraData", N->getRawExtraData());
1859  if (const auto &DWARFAddressSpace = N->getDWARFAddressSpace())
1860  Printer.printInt("dwarfAddressSpace", *DWARFAddressSpace,
1861  /* ShouldSkipZero */ false);
1862  Out << ")";
1863 }
1864 
1865 static void writeDICompositeType(raw_ostream &Out, const DICompositeType *N,
1866  TypePrinting *TypePrinter,
1867  SlotTracker *Machine, const Module *Context) {
1868  Out << "!DICompositeType(";
1869  MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1870  Printer.printTag(N);
1871  Printer.printString("name", N->getName());
1872  Printer.printMetadata("scope", N->getRawScope());
1873  Printer.printMetadata("file", N->getRawFile());
1874  Printer.printInt("line", N->getLine());
1875  Printer.printMetadata("baseType", N->getRawBaseType());
1876  Printer.printInt("size", N->getSizeInBits());
1877  Printer.printInt("align", N->getAlignInBits());
1878  Printer.printInt("offset", N->getOffsetInBits());
1879  Printer.printDIFlags("flags", N->getFlags());
1880  Printer.printMetadata("elements", N->getRawElements());
1881  Printer.printDwarfEnum("runtimeLang", N->getRuntimeLang(),
1882  dwarf::LanguageString);
1883  Printer.printMetadata("vtableHolder", N->getRawVTableHolder());
1884  Printer.printMetadata("templateParams", N->getRawTemplateParams());
1885  Printer.printString("identifier", N->getIdentifier());
1886  Printer.printMetadata("discriminator", N->getRawDiscriminator());
1887  Out << ")";
1888 }
1889 
1890 static void writeDISubroutineType(raw_ostream &Out, const DISubroutineType *N,
1891  TypePrinting *TypePrinter,
1892  SlotTracker *Machine, const Module *Context) {
1893  Out << "!DISubroutineType(";
1894  MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1895  Printer.printDIFlags("flags", N->getFlags());
1896  Printer.printDwarfEnum("cc", N->getCC(), dwarf::ConventionString);
1897  Printer.printMetadata("types", N->getRawTypeArray(),
1898  /* ShouldSkipNull */ false);
1899  Out << ")";
1900 }
1901 
1902 static void writeDIFile(raw_ostream &Out, const DIFile *N, TypePrinting *,
1903  SlotTracker *, const Module *) {
1904  Out << "!DIFile(";
1905  MDFieldPrinter Printer(Out);
1906  Printer.printString("filename", N->getFilename(),
1907  /* ShouldSkipEmpty */ false);
1908  Printer.printString("directory", N->getDirectory(),
1909  /* ShouldSkipEmpty */ false);
1910  // Print all values for checksum together, or not at all.
1911  if (N->getChecksum())
1912  Printer.printChecksum(*N->getChecksum());
1913  Printer.printString("source", N->getSource().getValueOr(StringRef()),
1914  /* ShouldSkipEmpty */ true);
1915  Out << ")";
1916 }
1917 
1918 static void writeDICompileUnit(raw_ostream &Out, const DICompileUnit *N,
1919  TypePrinting *TypePrinter, SlotTracker *Machine,
1920  const Module *Context) {
1921  Out << "!DICompileUnit(";
1922  MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1923  Printer.printDwarfEnum("language", N->getSourceLanguage(),
1924  dwarf::LanguageString, /* ShouldSkipZero */ false);
1925  Printer.printMetadata("file", N->getRawFile(), /* ShouldSkipNull */ false);
1926  Printer.printString("producer", N->getProducer());
1927  Printer.printBool("isOptimized", N->isOptimized());
1928  Printer.printString("flags", N->getFlags());
1929  Printer.printInt("runtimeVersion", N->getRuntimeVersion(),
1930  /* ShouldSkipZero */ false);
1931  Printer.printString("splitDebugFilename", N->getSplitDebugFilename());
1932  Printer.printEmissionKind("emissionKind", N->getEmissionKind());
1933  Printer.printMetadata("enums", N->getRawEnumTypes());
1934  Printer.printMetadata("retainedTypes", N->getRawRetainedTypes());
1935  Printer.printMetadata("globals", N->getRawGlobalVariables());
1936  Printer.printMetadata("imports", N->getRawImportedEntities());
1937  Printer.printMetadata("macros", N->getRawMacros());
1938  Printer.printInt("dwoId", N->getDWOId());
1939  Printer.printBool("splitDebugInlining", N->getSplitDebugInlining(), true);
1940  Printer.printBool("debugInfoForProfiling", N->getDebugInfoForProfiling(),
1941  false);
1942  Printer.printNameTableKind("nameTableKind", N->getNameTableKind());
1943  Printer.printBool("rangesBaseAddress", N->getRangesBaseAddress(), false);
1944  Out << ")";
1945 }
1946 
1947 static void writeDISubprogram(raw_ostream &Out, const DISubprogram *N,
1948  TypePrinting *TypePrinter, SlotTracker *Machine,
1949  const Module *Context) {
1950  Out << "!DISubprogram(";
1951  MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1952  Printer.printString("name", N->getName());
1953  Printer.printString("linkageName", N->getLinkageName());
1954  Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
1955  Printer.printMetadata("file", N->getRawFile());
1956  Printer.printInt("line", N->getLine());
1957  Printer.printMetadata("type", N->getRawType());
1958  Printer.printInt("scopeLine", N->getScopeLine());
1959  Printer.printMetadata("containingType", N->getRawContainingType());
1960  if (N->getVirtuality() != dwarf::DW_VIRTUALITY_none ||
1961  N->getVirtualIndex() != 0)
1962  Printer.printInt("virtualIndex", N->getVirtualIndex(), false);
1963  Printer.printInt("thisAdjustment", N->getThisAdjustment());
1964  Printer.printDIFlags("flags", N->getFlags());
1965  Printer.printDISPFlags("spFlags", N->getSPFlags());
1966  Printer.printMetadata("unit", N->getRawUnit());
1967  Printer.printMetadata("templateParams", N->getRawTemplateParams());
1968  Printer.printMetadata("declaration", N->getRawDeclaration());
1969  Printer.printMetadata("retainedNodes", N->getRawRetainedNodes());
1970  Printer.printMetadata("thrownTypes", N->getRawThrownTypes());
1971  Out << ")";
1972 }
1973 
1974 static void writeDILexicalBlock(raw_ostream &Out, const DILexicalBlock *N,
1975  TypePrinting *TypePrinter, SlotTracker *Machine,
1976  const Module *Context) {
1977  Out << "!DILexicalBlock(";
1978  MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1979  Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
1980  Printer.printMetadata("file", N->getRawFile());
1981  Printer.printInt("line", N->getLine());
1982  Printer.printInt("column", N->getColumn());
1983  Out << ")";
1984 }
1985 
1986 static void writeDILexicalBlockFile(raw_ostream &Out,
1987  const DILexicalBlockFile *N,
1988  TypePrinting *TypePrinter,
1989  SlotTracker *Machine,
1990  const Module *Context) {
1991  Out << "!DILexicalBlockFile(";
1992  MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1993  Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
1994  Printer.printMetadata("file", N->getRawFile());
1995  Printer.printInt("discriminator", N->getDiscriminator(),
1996  /* ShouldSkipZero */ false);
1997  Out << ")";
1998 }
1999 
2000 static void writeDINamespace(raw_ostream &Out, const DINamespace *N,
2001  TypePrinting *TypePrinter, SlotTracker *Machine,
2002  const Module *Context) {
2003  Out << "!DINamespace(";
2004  MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
2005  Printer.printString("name", N->getName());
2006  Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
2007  Printer.printBool("exportSymbols", N->getExportSymbols(), false);
2008  Out << ")";
2009 }
2010 
2011 static void writeDICommonBlock(raw_ostream &Out, const DICommonBlock *N,
2012  TypePrinting *TypePrinter, SlotTracker *Machine,
2013  const Module *Context) {
2014  Out << "!DICommonBlock(";
2015  MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
2016  Printer.printMetadata("scope", N->getRawScope(), false);
2017  Printer.printMetadata("declaration", N->getRawDecl(), false);
2018  Printer.printString("name", N->getName());
2019  Printer.printMetadata("file", N->getRawFile());
2020  Printer.printInt("line", N->getLineNo());
2021  Out << ")";
2022 }
2023 
2024 static void writeDIMacro(raw_ostream &Out, const DIMacro *N,
2025  TypePrinting *TypePrinter, SlotTracker *Machine,
2026  const Module *Context) {
2027  Out << "!DIMacro(";
2028  MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
2029  Printer.printMacinfoType(N);
2030  Printer.printInt("line", N->getLine());
2031  Printer.printString("name", N->getName());
2032  Printer.printString("value", N->getValue());
2033  Out << ")";
2034 }
2035 
2036 static void writeDIMacroFile(raw_ostream &Out, const DIMacroFile *N,
2037  TypePrinting *TypePrinter, SlotTracker *Machine,
2038  const Module *Context) {
2039  Out << "!DIMacroFile(";
2040  MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
2041  Printer.printInt("line", N->getLine());
2042  Printer.printMetadata("file", N->getRawFile(), /* ShouldSkipNull */ false);
2043  Printer.printMetadata("nodes", N->getRawElements());
2044  Out << ")";
2045 }
2046 
2047 static void writeDIModule(raw_ostream &Out, const DIModule *N,
2048  TypePrinting *TypePrinter, SlotTracker *Machine,
2049  const Module *Context) {
2050  Out << "!DIModule(";
2051  MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
2052  Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
2053  Printer.printString("name", N->getName());
2054  Printer.printString("configMacros", N->getConfigurationMacros());
2055  Printer.printString("includePath", N->getIncludePath());
2056  Printer.printString("isysroot", N->getISysRoot());
2057  Out << ")";
2058 }
2059 
2060 
2061 static void writeDITemplateTypeParameter(raw_ostream &Out,
2062  const DITemplateTypeParameter *N,
2063  TypePrinting *TypePrinter,
2064  SlotTracker *Machine,
2065  const Module *Context) {
2066  Out << "!DITemplateTypeParameter(";
2067  MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
2068  Printer.printString("name", N->getName());
2069  Printer.printMetadata("type", N->getRawType(), /* ShouldSkipNull */ false);
2070  Out << ")";
2071 }
2072 
2073 static void writeDITemplateValueParameter(raw_ostream &Out,
2074  const DITemplateValueParameter *N,
2075  TypePrinting *TypePrinter,
2076  SlotTracker *Machine,
2077  const Module *Context) {
2078  Out << "!DITemplateValueParameter(";
2079  MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
2080  if (N->getTag() != dwarf::DW_TAG_template_value_parameter)
2081  Printer.printTag(N);
2082  Printer.printString("name", N->getName());
2083  Printer.printMetadata("type", N->getRawType());
2084  Printer.printMetadata("value", N->getValue(), /* ShouldSkipNull */ false);
2085  Out << ")";
2086 }
2087 
2088 static void writeDIGlobalVariable(raw_ostream &Out, const DIGlobalVariable *N,
2089  TypePrinting *TypePrinter,
2090  SlotTracker *Machine, const Module *Context) {
2091  Out << "!DIGlobalVariable(";
2092  MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
2093  Printer.printString("name", N->getName());
2094  Printer.printString("linkageName", N->getLinkageName());
2095  Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
2096  Printer.printMetadata("file", N->getRawFile());
2097  Printer.printInt("line", N->getLine());
2098  Printer.printMetadata("type", N->getRawType());
2099  Printer.printBool("isLocal", N->isLocalToUnit());
2100  Printer.printBool("isDefinition", N->isDefinition());
2101  Printer.printMetadata("declaration", N->getRawStaticDataMemberDeclaration());
2102  Printer.printMetadata("templateParams", N->getRawTemplateParams());
2103  Printer.printInt("align", N->getAlignInBits());
2104  Out << ")";
2105 }
2106 
2107 static void writeDILocalVariable(raw_ostream &Out, const DILocalVariable *N,
2108  TypePrinting *TypePrinter,
2109  SlotTracker *Machine, const Module *Context) {
2110  Out << "!DILocalVariable(";
2111  MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
2112  Printer.printString("name", N->getName());
2113  Printer.printInt("arg", N->getArg());
2114  Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
2115  Printer.printMetadata("file", N->getRawFile());
2116  Printer.printInt("line", N->getLine());
2117  Printer.printMetadata("type", N->getRawType());
2118  Printer.printDIFlags("flags", N->getFlags());
2119  Printer.printInt("align", N->getAlignInBits());
2120  Out << ")";
2121 }
2122 
2123 static void writeDILabel(raw_ostream &Out, const DILabel *N,
2124  TypePrinting *TypePrinter,
2125  SlotTracker *Machine, const Module *Context) {
2126  Out << "!DILabel(";
2127  MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
2128  Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
2129  Printer.printString("name", N->getName());
2130  Printer.printMetadata("file", N->getRawFile());
2131  Printer.printInt("line", N->getLine());
2132  Out << ")";
2133 }
2134 
2135 static void writeDIExpression(raw_ostream &Out, const DIExpression *N,
2136  TypePrinting *TypePrinter, SlotTracker *Machine,
2137  const Module *Context) {
2138  Out << "!DIExpression(";
2139  FieldSeparator FS;
2140  if (N->isValid()) {
2141  for (auto I = N->expr_op_begin(), E = N->expr_op_end(); I != E; ++I) {
2142  auto OpStr = dwarf::OperationEncodingString(I->getOp());
2143  assert(!OpStr.empty() && "Expected valid opcode");
2144 
2145  Out << FS << OpStr;
2146  if (I->getOp() == dwarf::DW_OP_LLVM_convert) {
2147  Out << FS << I->getArg(0);
2148  Out << FS << dwarf::AttributeEncodingString(I->getArg(1));
2149  } else {
2150  for (unsigned A = 0, AE = I->getNumArgs(); A != AE; ++A)
2151  Out << FS << I->getArg(A);
2152  }
2153  }
2154  } else {
2155  for (const auto &I : N->getElements())
2156  Out << FS << I;
2157  }
2158  Out << ")";
2159 }
2160 
2161 static void writeDIGlobalVariableExpression(raw_ostream &Out,
2162  const DIGlobalVariableExpression *N,
2163  TypePrinting *TypePrinter,
2164  SlotTracker *Machine,
2165  const Module *Context) {
2166  Out << "!DIGlobalVariableExpression(";
2167  MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
2168  Printer.printMetadata("var", N->getVariable());
2169  Printer.printMetadata("expr", N->getExpression());
2170  Out << ")";
2171 }
2172 
2173 static void writeDIObjCProperty(raw_ostream &Out, const DIObjCProperty *N,
2174  TypePrinting *TypePrinter, SlotTracker *Machine,
2175  const Module *Context) {
2176  Out << "!DIObjCProperty(";
2177  MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
2178  Printer.printString("name", N->getName());
2179  Printer.printMetadata("file", N->getRawFile());
2180  Printer.printInt("line", N->getLine());
2181  Printer.printString("setter", N->getSetterName());
2182  Printer.printString("getter", N->getGetterName());
2183  Printer.printInt("attributes", N->getAttributes());
2184  Printer.printMetadata("type", N->getRawType());
2185  Out << ")";
2186 }
2187 
2188 static void writeDIImportedEntity(raw_ostream &Out, const DIImportedEntity *N,
2189  TypePrinting *TypePrinter,
2190  SlotTracker *Machine, const Module *Context) {
2191  Out << "!DIImportedEntity(";
2192  MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
2193  Printer.printTag(N);
2194  Printer.printString("name", N->getName());
2195  Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
2196  Printer.printMetadata("entity", N->getRawEntity());
2197  Printer.printMetadata("file", N->getRawFile());
2198  Printer.printInt("line", N->getLine());
2199  Out << ")";
2200 }
2201 
2202 static void WriteMDNodeBodyInternal(raw_ostream &Out, const MDNode *Node,
2203  TypePrinting *TypePrinter,
2204  SlotTracker *Machine,
2205  const Module *Context) {
2206  if (Node->isDistinct())
2207  Out << "distinct ";
2208  else if (Node->isTemporary())
2209  Out << "<temporary!> "; // Handle broken code.
2210 
2211  switch (Node->getMetadataID()) {
2212  default:
2213  llvm_unreachable("Expected uniquable MDNode");
2214 #define HANDLE_MDNODE_LEAF(CLASS) \
2215  case Metadata::CLASS##Kind: \
2216  write##CLASS(Out, cast<CLASS>(Node), TypePrinter, Machine, Context); \
2217  break;
2218 #include "llvm/IR/Metadata.def"
2219  }
2220 }
2221 
2222 // Full implementation of printing a Value as an operand with support for
2223 // TypePrinting, etc.
2224 static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
2225  TypePrinting *TypePrinter,
2226  SlotTracker *Machine,
2227  const Module *Context) {
2228  if (V->hasName()) {
2229  PrintLLVMName(Out, V);
2230  return;
2231  }
2232 
2233  const Constant *CV = dyn_cast<Constant>(V);
2234  if (CV && !isa<GlobalValue>(CV)) {
2235  assert(TypePrinter && "Constants require TypePrinting!");
2236  WriteConstantInternal(Out, CV, *TypePrinter, Machine, Context);
2237  return;
2238  }
2239 
2240  if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
2241  Out << "asm ";
2242  if (IA->hasSideEffects())
2243  Out << "sideeffect ";
2244  if (IA->isAlignStack())
2245  Out << "alignstack ";
2246  // We don't emit the AD_ATT dialect as it's the assumed default.
2247  if (IA->getDialect() == InlineAsm::AD_Intel)
2248  Out << "inteldialect ";
2249  Out << '"';
2250  printEscapedString(IA->getAsmString(), Out);
2251  Out << "\", \"";
2252  printEscapedString(IA->getConstraintString(), Out);
2253  Out << '"';
2254  return;
2255  }
2256 
2257  if (auto *MD = dyn_cast<MetadataAsValue>(V)) {
2258  WriteAsOperandInternal(Out, MD->getMetadata(), TypePrinter, Machine,
2259  Context, /* FromValue */ true);
2260  return;
2261  }
2262 
2263  char Prefix = '%';
2264  int Slot;
2265  // If we have a SlotTracker, use it.
2266  if (Machine) {
2267  if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
2268  Slot = Machine->getGlobalSlot(GV);
2269  Prefix = '@';
2270  } else {
2271  Slot = Machine->getLocalSlot(V);
2272 
2273  // If the local value didn't succeed, then we may be referring to a value
2274  // from a different function. Translate it, as this can happen when using
2275  // address of blocks.
2276  if (Slot == -1)
2277  if ((Machine = createSlotTracker(V))) {
2278  Slot = Machine->getLocalSlot(V);
2279  delete Machine;
2280  }
2281  }
2282  } else if ((Machine = createSlotTracker(V))) {
2283  // Otherwise, create one to get the # and then destroy it.
2284  if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
2285  Slot = Machine->getGlobalSlot(GV);
2286  Prefix = '@';
2287  } else {
2288  Slot = Machine->getLocalSlot(V);
2289  }
2290  delete Machine;
2291  Machine = nullptr;
2292  } else {
2293  Slot = -1;
2294  }
2295 
2296  if (Slot != -1)
2297  Out << Prefix << Slot;
2298  else
2299  Out << "<badref>";
2300 }
2301 
2302 static void WriteAsOperandInternal(raw_ostream &Out, const Metadata *MD,
2303  TypePrinting *TypePrinter,
2304  SlotTracker *Machine, const Module *Context,
2305  bool FromValue) {
2306  // Write DIExpressions inline when used as a value. Improves readability of
2307  // debug info intrinsics.
2308  if (const DIExpression *Expr = dyn_cast<DIExpression>(MD)) {
2309  writeDIExpression(Out, Expr, TypePrinter, Machine, Context);
2310  return;
2311  }
2312 
2313  if (const MDNode *N = dyn_cast<MDNode>(MD)) {
2314  std::unique_ptr<SlotTracker> MachineStorage;
2315  if (!Machine) {
2316  MachineStorage = std::make_unique<SlotTracker>(Context);
2317  Machine = MachineStorage.get();
2318  }
2319  int Slot = Machine->getMetadataSlot(N);
2320  if (Slot == -1) {
2321  if (const DILocation *Loc = dyn_cast<DILocation>(N)) {
2322  writeDILocation(Out, Loc, TypePrinter, Machine, Context);
2323  return;
2324  }
2325  // Give the pointer value instead of "badref", since this comes up all
2326  // the time when debugging.
2327  Out << "<" << N << ">";
2328  } else
2329  Out << '!' << Slot;
2330  return;
2331  }
2332 
2333  if (const MDString *MDS = dyn_cast<MDString>(MD)) {
2334  Out << "!\"";
2335  printEscapedString(MDS->getString(), Out);
2336  Out << '"';
2337  return;
2338  }
2339 
2340  auto *V = cast<ValueAsMetadata>(MD);
2341  assert(TypePrinter && "TypePrinter required for metadata values");
2342  assert((FromValue || !isa<LocalAsMetadata>(V)) &&
2343  "Unexpected function-local metadata outside of value argument");
2344 
2345  TypePrinter->print(V->getValue()->getType(), Out);
2346  Out << ' ';
2347  WriteAsOperandInternal(Out, V->getValue(), TypePrinter, Machine, Context);
2348 }
2349 
2350 namespace {
2351 
2352 class AssemblyWriter {
2353  formatted_raw_ostream &Out;
2354  const Module *TheModule = nullptr;
2355  const ModuleSummaryIndex *TheIndex = nullptr;
2356  std::unique_ptr<SlotTracker> SlotTrackerStorage;
2358  TypePrinting TypePrinter;
2359  AssemblyAnnotationWriter *AnnotationWriter = nullptr;
2360  SetVector<const Comdat *> Comdats;
2361  bool IsForDebug;
2362  bool ShouldPreserveUseListOrder;
2363  UseListOrderStack UseListOrders;
2364  SmallVector<StringRef, 8> MDNames;
2365  /// Synchronization scope names registered with LLVMContext.
2368 
2369 public:
2370  /// Construct an AssemblyWriter with an external SlotTracker
2371  AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac, const Module *M,
2372  AssemblyAnnotationWriter *AAW, bool IsForDebug,
2373  bool ShouldPreserveUseListOrder = false);
2374 
2375  AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac,
2376  const ModuleSummaryIndex *Index, bool IsForDebug);
2377 
2378  void printMDNodeBody(const MDNode *MD);
2379  void printNamedMDNode(const NamedMDNode *NMD);
2380 
2381  void printModule(const Module *M);
2382 
2383  void writeOperand(const Value *Op, bool PrintType);
2384  void writeParamOperand(const Value *Operand, AttributeSet Attrs);
2385  void writeOperandBundles(const CallBase *Call);
2386  void writeSyncScope(const LLVMContext &Context,
2387  SyncScope::ID SSID);
2388  void writeAtomic(const LLVMContext &Context,
2389  AtomicOrdering Ordering,
2390  SyncScope::ID SSID);
2391  void writeAtomicCmpXchg(const LLVMContext &Context,
2392  AtomicOrdering SuccessOrdering,
2393  AtomicOrdering FailureOrdering,
2394  SyncScope::ID SSID);
2395 
2396  void writeAllMDNodes();
2397  void writeMDNode(unsigned Slot, const MDNode *Node);
2398  void writeAllAttributeGroups();
2399 
2400  void printTypeIdentities();
2401  void printGlobal(const GlobalVariable *GV);
2402  void printIndirectSymbol(const GlobalIndirectSymbol *GIS);
2403  void printComdat(const Comdat *C);
2404  void printFunction(const Function *F);
2405  void printArgument(const Argument *FA, AttributeSet Attrs);
2406  void printBasicBlock(const BasicBlock *BB);
2407  void printInstructionLine(const Instruction &I);
2408  void printInstruction(const Instruction &I);
2409 
2410  void printUseListOrder(const UseListOrder &Order);
2411  void printUseLists(const Function *F);
2412 
2413  void printModuleSummaryIndex();
2414  void printSummaryInfo(unsigned Slot, const ValueInfo &VI);
2415  void printSummary(const GlobalValueSummary &Summary);
2416  void printAliasSummary(const AliasSummary *AS);
2417  void printGlobalVarSummary(const GlobalVarSummary *GS);
2418  void printFunctionSummary(const FunctionSummary *FS);
2419  void printTypeIdSummary(const TypeIdSummary &TIS);
2420  void printTypeIdCompatibleVtableSummary(const TypeIdCompatibleVtableInfo &TI);
2421  void printTypeTestResolution(const TypeTestResolution &TTRes);
2422  void printArgs(const std::vector<uint64_t> &Args);
2423  void printWPDRes(const WholeProgramDevirtResolution &WPDRes);
2424  void printTypeIdInfo(const FunctionSummary::TypeIdInfo &TIDInfo);
2425  void printVFuncId(const FunctionSummary::VFuncId VFId);
2426  void
2427  printNonConstVCalls(const std::vector<FunctionSummary::VFuncId> VCallList,
2428  const char *Tag);
2429  void
2430  printConstVCalls(const std::vector<FunctionSummary::ConstVCall> VCallList,
2431  const char *Tag);
2432 
2433 private:
2434  /// Print out metadata attachments.
2435  void printMetadataAttachments(
2436  const SmallVectorImpl<std::pair<unsigned, MDNode *>> &MDs,
2437  StringRef Separator);
2438 
2439  // printInfoComment - Print a little comment after the instruction indicating
2440  // which slot it occupies.
2441  void printInfoComment(const Value &V);
2442 
2443  // printGCRelocateComment - print comment after call to the gc.relocate
2444  // intrinsic indicating base and derived pointer names.
2445  void printGCRelocateComment(const GCRelocateInst &Relocate);
2446 };
2447 
2448 } // end anonymous namespace
2449 
2450 AssemblyWriter::AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac,
2451  const Module *M, AssemblyAnnotationWriter *AAW,
2452  bool IsForDebug, bool ShouldPreserveUseListOrder)
2453  : Out(o), TheModule(M), Machine(Mac), TypePrinter(M), AnnotationWriter(AAW),
2454  IsForDebug(IsForDebug),
2455  ShouldPreserveUseListOrder(ShouldPreserveUseListOrder) {
2456  if (!TheModule)
2457  return;
2458  for (const GlobalObject &GO : TheModule->global_objects())
2459  if (const Comdat *C = GO.getComdat())
2460  Comdats.insert(C);
2461 }
2462 
2463 AssemblyWriter::AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac,
2464  const ModuleSummaryIndex *Index, bool IsForDebug)
2465  : Out(o), TheIndex(Index), Machine(Mac), TypePrinter(/*Module=*/nullptr),
2466  IsForDebug(IsForDebug), ShouldPreserveUseListOrder(false) {}
2467 
2468 void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType) {
2469  if (!Operand) {
2470  Out << "<null operand!>";
2471  return;
2472  }
2473  if (PrintType) {
2474  TypePrinter.print(Operand->getType(), Out);
2475  Out << ' ';
2476  }
2477  WriteAsOperandInternal(Out, Operand, &TypePrinter, &Machine, TheModule);
2478 }
2479 
2480 void AssemblyWriter::writeSyncScope(const LLVMContext &Context,
2481  SyncScope::ID SSID) {
2482  switch (SSID) {
2483  case SyncScope::System: {
2484  break;
2485  }
2486  default: {
2487  if (SSNs.empty())
2488  Context.getSyncScopeNames(SSNs);
2489 
2490  Out << " syncscope(\"";
2491  printEscapedString(SSNs[SSID], Out);
2492  Out << "\")";
2493  break;
2494  }
2495  }
2496 }
2497 
2498 void AssemblyWriter::writeAtomic(const LLVMContext &Context,
2499  AtomicOrdering Ordering,
2500  SyncScope::ID SSID) {
2501  if (Ordering == AtomicOrdering::NotAtomic)
2502  return;
2503 
2504  writeSyncScope(Context, SSID);
2505  Out << " " << toIRString(Ordering);
2506 }
2507 
2508 void AssemblyWriter::writeAtomicCmpXchg(const LLVMContext &Context,
2509  AtomicOrdering SuccessOrdering,
2510  AtomicOrdering FailureOrdering,
2511  SyncScope::ID SSID) {
2512  assert(SuccessOrdering != AtomicOrdering::NotAtomic &&
2513  FailureOrdering != AtomicOrdering::NotAtomic);
2514 
2515  writeSyncScope(Context, SSID);
2516  Out << " " << toIRString(SuccessOrdering);
2517  Out << " " << toIRString(FailureOrdering);
2518 }
2519 
2520 void AssemblyWriter::writeParamOperand(const Value *Operand,
2521  AttributeSet Attrs) {
2522  if (!Operand) {
2523  Out << "<null operand!>";
2524  return;
2525  }
2526 
2527  // Print the type
2528  TypePrinter.print(Operand->getType(), Out);
2529  // Print parameter attributes list
2530  if (Attrs.hasAttributes())
2531  Out << ' ' << Attrs.getAsString();
2532  Out << ' ';
2533  // Print the operand
2534  WriteAsOperandInternal(Out, Operand, &TypePrinter, &Machine, TheModule);
2535 }
2536 
2537 void AssemblyWriter::writeOperandBundles(const CallBase *Call) {
2538  if (!Call->hasOperandBundles())
2539  return;
2540 
2541  Out << " [ ";
2542 
2543  bool FirstBundle = true;
2544  for (unsigned i = 0, e = Call->getNumOperandBundles(); i != e; ++i) {
2545  OperandBundleUse BU = Call->getOperandBundleAt(i);
2546 
2547  if (!FirstBundle)
2548  Out << ", ";
2549  FirstBundle = false;
2550 
2551  Out << '"';
2552  printEscapedString(BU.getTagName(), Out);
2553  Out << '"';
2554 
2555  Out << '(';
2556 
2557  bool FirstInput = true;
2558  for (const auto &Input : BU.Inputs) {
2559  if (!FirstInput)
2560  Out << ", ";
2561  FirstInput = false;
2562 
2563  TypePrinter.print(Input->getType(), Out);
2564  Out << " ";
2565  WriteAsOperandInternal(Out, Input, &TypePrinter, &Machine, TheModule);
2566  }
2567 
2568  Out << ')';
2569  }
2570 
2571  Out << " ]";
2572 }
2573 
2574 void AssemblyWriter::printModule(const Module *M) {
2575  Machine.initializeIfNeeded();
2576 
2577  if (ShouldPreserveUseListOrder)
2578  UseListOrders = predictUseListOrder(M);
2579 
2580  if (!M->getModuleIdentifier().empty() &&
2581  // Don't print the ID if it will start a new line (which would
2582  // require a comment char before it).
2583  M->getModuleIdentifier().find('\n') == std::string::npos)
2584  Out << "; ModuleID = '" << M->getModuleIdentifier() << "'\n";
2585 
2586  if (!M->getSourceFileName().empty()) {
2587  Out << "source_filename = \"";
2589  Out << "\"\n";
2590  }
2591 
2592  const std::string &DL = M->getDataLayoutStr();
2593  if (!DL.empty())
2594  Out << "target datalayout = \"" << DL << "\"\n";
2595  if (!M->getTargetTriple().empty())
2596  Out << "target triple = \"" << M->getTargetTriple() << "\"\n";
2597 
2598  if (!M->getModuleInlineAsm().empty()) {
2599  Out << '\n';
2600 
2601  // Split the string into lines, to make it easier to read the .ll file.
2603  do {
2604  StringRef Front;
2605  std::tie(Front, Asm) = Asm.split('\n');
2606 
2607  // We found a newline, print the portion of the asm string from the
2608  // last newline up to this newline.
2609  Out << "module asm \"";
2610  printEscapedString(Front, Out);
2611  Out << "\"\n";
2612  } while (!Asm.empty());
2613  }
2614 
2615  printTypeIdentities();
2616 
2617  // Output all comdats.
2618  if (!Comdats.empty())
2619  Out << '\n';
2620  for (const Comdat *C : Comdats) {
2621  printComdat(C);
2622  if (C != Comdats.back())
2623  Out << '\n';
2624  }
2625 
2626  // Output all globals.
2627  if (!M->global_empty()) Out << '\n';
2628  for (const GlobalVariable &GV : M->globals()) {
2629  printGlobal(&GV); Out << '\n';
2630  }
2631 
2632  // Output all aliases.
2633  if (!M->alias_empty()) Out << "\n";
2634  for (const GlobalAlias &GA : M->aliases())
2635  printIndirectSymbol(&GA);
2636 
2637  // Output all ifuncs.
2638  if (!M->ifunc_empty()) Out << "\n";
2639  for (const GlobalIFunc &GI : M->ifuncs())
2640  printIndirectSymbol(&GI);
2641 
2642  // Output global use-lists.
2643  printUseLists(nullptr);
2644 
2645  // Output all of the functions.
2646  for (const Function &F : *M)
2647  printFunction(&F);
2648  assert(UseListOrders.empty() && "All use-lists should have been consumed");
2649 
2650  // Output all attribute groups.
2651  if (!Machine.as_empty()) {
2652  Out << '\n';
2653  writeAllAttributeGroups();
2654  }
2655 
2656  // Output named metadata.
2657  if (!M->named_metadata_empty()) Out << '\n';
2658 
2659  for (const NamedMDNode &Node : M->named_metadata())
2660  printNamedMDNode(&Node);
2661 
2662  // Output metadata.
2663  if (!Machine.mdn_empty()) {
2664  Out << '\n';
2665  writeAllMDNodes();
2666  }
2667 }
2668 
2669 void AssemblyWriter::printModuleSummaryIndex() {
2670  assert(TheIndex);
2671  Machine.initializeIndexIfNeeded();
2672 
2673  Out << "\n";
2674 
2675  // Print module path entries. To print in order, add paths to a vector
2676  // indexed by module slot.
2677  std::vector<std::pair<std::string, ModuleHash>> moduleVec;
2678  std::string RegularLTOModuleName = "[Regular LTO]";
2679  moduleVec.resize(TheIndex->modulePaths().size());
2680  for (auto &ModPath : TheIndex->modulePaths())
2681  moduleVec[Machine.getModulePathSlot(ModPath.first())] = std::make_pair(
2682  // A module id of -1 is a special entry for a regular LTO module created
2683  // during the thin link.
2684  ModPath.second.first == -1u ? RegularLTOModuleName
2685  : (std::string)ModPath.first(),
2686  ModPath.second.second);
2687 
2688  unsigned i = 0;
2689  for (auto &ModPair : moduleVec) {
2690  Out << "^" << i++ << " = module: (";
2691  Out << "path: \"";
2692  printEscapedString(ModPair.first, Out);
2693  Out << "\", hash: (";
2694  FieldSeparator FS;
2695  for (auto Hash : ModPair.second)
2696  Out << FS << Hash;
2697  Out << "))\n";
2698  }
2699 
2700  // FIXME: Change AliasSummary to hold a ValueInfo instead of summary pointer
2701  // for aliasee (then update BitcodeWriter.cpp and remove get/setAliaseeGUID).
2702  for (auto &GlobalList : *TheIndex) {
2703  auto GUID = GlobalList.first;
2704  for (auto &Summary : GlobalList.second.SummaryList)
2705  SummaryToGUIDMap[Summary.get()] = GUID;
2706  }
2707 
2708  // Print the global value summary entries.
2709  for (auto &GlobalList : *TheIndex) {
2710  auto GUID = GlobalList.first;
2711  auto VI = TheIndex->getValueInfo(GlobalList);
2712  printSummaryInfo(Machine.getGUIDSlot(GUID), VI);
2713  }
2714 
2715  // Print the TypeIdMap entries.
2716  for (auto TidIter = TheIndex->typeIds().begin();
2717  TidIter != TheIndex->typeIds().end(); TidIter++) {
2718  Out << "^" << Machine.getTypeIdSlot(TidIter->second.first)
2719  << " = typeid: (name: \"" << TidIter->second.first << "\"";
2720  printTypeIdSummary(TidIter->second.second);
2721  Out << ") ; guid = " << TidIter->first << "\n";
2722  }
2723 
2724  // Print the TypeIdCompatibleVtableMap entries.
2725  for (auto &TId : TheIndex->typeIdCompatibleVtableMap()) {
2726  auto GUID = GlobalValue::getGUID(TId.first);
2727  Out << "^" << Machine.getGUIDSlot(GUID)
2728  << " = typeidCompatibleVTable: (name: \"" << TId.first << "\"";
2729  printTypeIdCompatibleVtableSummary(TId.second);
2730  Out << ") ; guid = " << GUID << "\n";
2731  }
2732 }
2733 
2734 static const char *
2736  switch (K) {
2738  return "indir";
2740  return "singleImpl";
2742  return "branchFunnel";
2743  }
2744  llvm_unreachable("invalid WholeProgramDevirtResolution kind");
2745 }
2746 
2749  switch (K) {
2751  return "indir";
2753  return "uniformRetVal";
2755  return "uniqueRetVal";
2757  return "virtualConstProp";
2758  }
2759  llvm_unreachable("invalid WholeProgramDevirtResolution::ByArg kind");
2760 }
2761 
2763  switch (K) {
2765  return "unsat";
2767  return "byteArray";
2769  return "inline";
2771  return "single";
2773  return "allOnes";
2774  }
2775  llvm_unreachable("invalid TypeTestResolution kind");
2776 }
2777 
2778 void AssemblyWriter::printTypeTestResolution(const TypeTestResolution &TTRes) {
2779  Out << "typeTestRes: (kind: " << getTTResKindName(TTRes.TheKind)
2780  << ", sizeM1BitWidth: " << TTRes.SizeM1BitWidth;
2781 
2782  // The following fields are only used if the target does not support the use
2783  // of absolute symbols to store constants. Print only if non-zero.
2784  if (TTRes.AlignLog2)
2785  Out << ", alignLog2: " << TTRes.AlignLog2;
2786  if (TTRes.SizeM1)
2787  Out << ", sizeM1: " << TTRes.SizeM1;
2788  if (TTRes.BitMask)
2789  // BitMask is uint8_t which causes it to print the corresponding char.
2790  Out << ", bitMask: " << (unsigned)TTRes.BitMask;
2791  if (TTRes.InlineBits)
2792  Out << ", inlineBits: " << TTRes.InlineBits;
2793 
2794  Out << ")";
2795 }
2796 
2797 void AssemblyWriter::printTypeIdSummary(const TypeIdSummary &TIS) {
2798  Out << ", summary: (";
2799  printTypeTestResolution(TIS.TTRes);
2800  if (!TIS.WPDRes.empty()) {
2801  Out << ", wpdResolutions: (";
2802  FieldSeparator FS;
2803  for (auto &WPDRes : TIS.WPDRes) {
2804  Out << FS;
2805  Out << "(offset: " << WPDRes.first << ", ";
2806  printWPDRes(WPDRes.second);
2807  Out << ")";
2808  }
2809  Out << ")";
2810  }
2811  Out << ")";
2812 }
2813 
2814 void AssemblyWriter::printTypeIdCompatibleVtableSummary(
2815  const TypeIdCompatibleVtableInfo &TI) {
2816  Out << ", summary: (";
2817  FieldSeparator FS;
2818  for (auto &P : TI) {
2819  Out << FS;
2820  Out << "(offset: " << P.AddressPointOffset << ", ";
2821  Out << "^" << Machine.getGUIDSlot(P.VTableVI.getGUID());
2822  Out << ")";
2823  }
2824  Out << ")";
2825 }
2826 
2827 void AssemblyWriter::printArgs(const std::vector<uint64_t> &Args) {
2828  Out << "args: (";
2829  FieldSeparator FS;
2830  for (auto arg : Args) {
2831  Out << FS;
2832  Out << arg;
2833  }
2834  Out << ")";
2835 }
2836 
2837 void AssemblyWriter::printWPDRes(const WholeProgramDevirtResolution &WPDRes) {
2838  Out << "wpdRes: (kind: ";
2839  Out << getWholeProgDevirtResKindName(WPDRes.TheKind);
2840 
2842  Out << ", singleImplName: \"" << WPDRes.SingleImplName << "\"";
2843 
2844  if (!WPDRes.ResByArg.empty()) {
2845  Out << ", resByArg: (";
2846  FieldSeparator FS;
2847  for (auto &ResByArg : WPDRes.ResByArg) {
2848  Out << FS;
2849  printArgs(ResByArg.first);
2850  Out << ", byArg: (kind: ";
2851  Out << getWholeProgDevirtResByArgKindName(ResByArg.second.TheKind);
2852  if (ResByArg.second.TheKind ==
2854  ResByArg.second.TheKind ==
2856  Out << ", info: " << ResByArg.second.Info;
2857 
2858  // The following fields are only used if the target does not support the
2859  // use of absolute symbols to store constants. Print only if non-zero.
2860  if (ResByArg.second.Byte || ResByArg.second.Bit)
2861  Out << ", byte: " << ResByArg.second.Byte
2862  << ", bit: " << ResByArg.second.Bit;
2863 
2864  Out << ")";
2865  }
2866  Out << ")";
2867  }
2868  Out << ")";
2869 }
2870 
2872  switch (SK) {
2874  return "alias";
2876  return "function";
2878  return "variable";
2879  }
2880  llvm_unreachable("invalid summary kind");
2881 }
2882 
2883 void AssemblyWriter::printAliasSummary(const AliasSummary *AS) {
2884  Out << ", aliasee: ";
2885  // The indexes emitted for distributed backends may not include the
2886  // aliasee summary (only if it is being imported directly). Handle
2887  // that case by just emitting "null" as the aliasee.
2888  if (AS->hasAliasee())
2889  Out << "^" << Machine.getGUIDSlot(SummaryToGUIDMap[&AS->getAliasee()]);
2890  else
2891  Out << "null";
2892 }
2893 
2894 void AssemblyWriter::printGlobalVarSummary(const GlobalVarSummary *GS) {
2895  Out << ", varFlags: (readonly: " << GS->VarFlags.MaybeReadOnly << ", "
2896  << "writeonly: " << GS->VarFlags.MaybeWriteOnly << ")";
2897 
2898  auto VTableFuncs = GS->vTableFuncs();
2899  if (!VTableFuncs.empty()) {
2900  Out << ", vTableFuncs: (";
2901  FieldSeparator FS;
2902  for (auto &P : VTableFuncs) {
2903  Out << FS;
2904  Out << "(virtFunc: ^" << Machine.getGUIDSlot(P.FuncVI.getGUID())
2905  << ", offset: " << P.VTableOffset;
2906  Out << ")";
2907  }
2908  Out << ")";
2909  }
2910 }
2911 
2913  switch (LT) {
2915  return "external";
2917  return "private";
2919  return "internal";
2921  return "linkonce";
2923  return "linkonce_odr";
2925  return "weak";
2927  return "weak_odr";
2929  return "common";
2931  return "appending";
2933  return "extern_weak";
2935  return "available_externally";
2936  }
2937  llvm_unreachable("invalid linkage");
2938 }
2939 
2940 // When printing the linkage types in IR where the ExternalLinkage is
2941 // not printed, and other linkage types are expected to be printed with
2942 // a space after the name.
2944  if (LT == GlobalValue::ExternalLinkage)
2945  return "";
2946  return getLinkageName(LT) + " ";
2947 }
2948 
2949 void AssemblyWriter::printFunctionSummary(const FunctionSummary *FS) {
2950  Out << ", insts: " << FS->instCount();
2951 
2952  FunctionSummary::FFlags FFlags = FS->fflags();
2953  if (FFlags.ReadNone | FFlags.ReadOnly | FFlags.NoRecurse |
2954  FFlags.ReturnDoesNotAlias) {
2955  Out << ", funcFlags: (";
2956  Out << "readNone: " << FFlags.ReadNone;
2957  Out << ", readOnly: " << FFlags.ReadOnly;
2958  Out << ", noRecurse: " << FFlags.NoRecurse;
2959  Out << ", returnDoesNotAlias: " << FFlags.ReturnDoesNotAlias;
2960  Out << ", noInline: " << FFlags.NoInline;
2961  Out << ")";
2962  }
2963  if (!FS->calls().empty()) {
2964  Out << ", calls: (";
2965  FieldSeparator IFS;
2966  for (auto &Call : FS->calls()) {
2967  Out << IFS;
2968  Out << "(callee: ^" << Machine.getGUIDSlot(Call.first.getGUID());
2969  if (Call.second.getHotness() != CalleeInfo::HotnessType::Unknown)
2970  Out << ", hotness: " << getHotnessName(Call.second.getHotness());
2971  else if (Call.second.RelBlockFreq)
2972  Out << ", relbf: " << Call.second.RelBlockFreq;
2973  Out << ")";
2974  }
2975  Out << ")";
2976  }
2977 
2978  if (const auto *TIdInfo = FS->getTypeIdInfo())
2979  printTypeIdInfo(*TIdInfo);
2980 }
2981 
2982 void AssemblyWriter::printTypeIdInfo(
2983  const FunctionSummary::TypeIdInfo &TIDInfo) {
2984  Out << ", typeIdInfo: (";
2985  FieldSeparator TIDFS;
2986  if (!TIDInfo.TypeTests.empty()) {
2987  Out << TIDFS;
2988  Out << "typeTests: (";
2989  FieldSeparator FS;
2990  for (auto &GUID : TIDInfo.TypeTests) {
2991  auto TidIter = TheIndex->typeIds().equal_range(GUID);
2992  if (TidIter.first == TidIter.second) {
2993  Out << FS;
2994  Out << GUID;
2995  continue;
2996  }
2997  // Print all type id that correspond to this GUID.
2998  for (auto It = TidIter.first; It != TidIter.second; ++It) {
2999  Out << FS;
3000  auto Slot = Machine.getTypeIdSlot(It->second.first);
3001  assert(Slot != -1);
3002  Out << "^" << Slot;
3003  }
3004  }
3005  Out << ")";
3006  }
3007  if (!TIDInfo.TypeTestAssumeVCalls.empty()) {
3008  Out << TIDFS;
3009  printNonConstVCalls(TIDInfo.TypeTestAssumeVCalls, "typeTestAssumeVCalls");
3010  }
3011  if (!TIDInfo.TypeCheckedLoadVCalls.empty()) {
3012  Out << TIDFS;
3013  printNonConstVCalls(TIDInfo.TypeCheckedLoadVCalls, "typeCheckedLoadVCalls");
3014  }
3015  if (!TIDInfo.TypeTestAssumeConstVCalls.empty()) {
3016  Out << TIDFS;
3017  printConstVCalls(TIDInfo.TypeTestAssumeConstVCalls,
3018  "typeTestAssumeConstVCalls");
3019  }
3020  if (!TIDInfo.TypeCheckedLoadConstVCalls.empty()) {
3021  Out << TIDFS;
3022  printConstVCalls(TIDInfo.TypeCheckedLoadConstVCalls,
3023  "typeCheckedLoadConstVCalls");
3024  }
3025  Out << ")";
3026 }
3027 
3028 void AssemblyWriter::printVFuncId(const FunctionSummary::VFuncId VFId) {
3029  auto TidIter = TheIndex->typeIds().equal_range(VFId.GUID);
3030  if (TidIter.first == TidIter.second) {
3031  Out << "vFuncId: (";
3032  Out << "guid: " << VFId.GUID;
3033  Out << ", offset: " << VFId.Offset;
3034  Out << ")";
3035  return;
3036  }
3037  // Print all type id that correspond to this GUID.
3038  FieldSeparator FS;
3039  for (auto It = TidIter.first; It != TidIter.second; ++It) {
3040  Out << FS;
3041  Out << "vFuncId: (";
3042  auto Slot = Machine.getTypeIdSlot(It->second.first);
3043  assert(Slot != -1);
3044  Out << "^" << Slot;
3045  Out << ", offset: " << VFId.Offset;
3046  Out << ")";
3047  }
3048 }
3049 
3050 void AssemblyWriter::printNonConstVCalls(
3051  const std::vector<FunctionSummary::VFuncId> VCallList, const char *Tag) {
3052  Out << Tag << ": (";
3053  FieldSeparator FS;
3054  for (auto &VFuncId : VCallList) {
3055  Out << FS;
3056  printVFuncId(VFuncId);
3057  }
3058  Out << ")";
3059 }
3060 
3061 void AssemblyWriter::printConstVCalls(
3062  const std::vector<FunctionSummary::ConstVCall> VCallList, const char *Tag) {
3063  Out << Tag << ": (";
3064  FieldSeparator FS;
3065  for (auto &ConstVCall : VCallList) {
3066  Out << FS;
3067  Out << "(";
3068  printVFuncId(ConstVCall.VFunc);
3069  if (!ConstVCall.Args.empty()) {
3070  Out << ", ";
3071  printArgs(ConstVCall.Args);
3072  }
3073  Out << ")";
3074  }
3075  Out << ")";
3076 }
3077 
3078 void AssemblyWriter::printSummary(const GlobalValueSummary &Summary) {
3079  GlobalValueSummary::GVFlags GVFlags = Summary.flags();
3081  Out << getSummaryKindName(Summary.getSummaryKind()) << ": ";
3082  Out << "(module: ^" << Machine.getModulePathSlot(Summary.modulePath())
3083  << ", flags: (";
3084  Out << "linkage: " << getLinkageName(LT);
3085  Out << ", notEligibleToImport: " << GVFlags.NotEligibleToImport;
3086  Out << ", live: " << GVFlags.Live;
3087  Out << ", dsoLocal: " << GVFlags.DSOLocal;
3088  Out << ", canAutoHide: " << GVFlags.CanAutoHide;
3089  Out << ")";
3090 
3092  printAliasSummary(cast<AliasSummary>(&Summary));
3093  else if (Summary.getSummaryKind() == GlobalValueSummary::FunctionKind)
3094  printFunctionSummary(cast<FunctionSummary>(&Summary));
3095  else
3096  printGlobalVarSummary(cast<GlobalVarSummary>(&Summary));
3097 
3098  auto RefList = Summary.refs();
3099  if (!RefList.empty()) {
3100  Out << ", refs: (";
3101  FieldSeparator FS;
3102  for (auto &Ref : RefList) {
3103  Out << FS;
3104  if (Ref.isReadOnly())
3105  Out << "readonly ";
3106  else if (Ref.isWriteOnly())
3107  Out << "writeonly ";
3108  Out << "^" << Machine.getGUIDSlot(Ref.getGUID());
3109  }
3110  Out << ")";
3111  }
3112 
3113  Out << ")";
3114 }
3115 
3116 void AssemblyWriter::printSummaryInfo(unsigned Slot, const ValueInfo &VI) {
3117  Out << "^" << Slot << " = gv: (";
3118  if (!VI.name().empty())
3119  Out << "name: \"" << VI.name() << "\"";
3120  else
3121  Out << "guid: " << VI.getGUID();
3122  if (!VI.getSummaryList().empty()) {
3123  Out << ", summaries: (";
3124  FieldSeparator FS;
3125  for (auto &Summary : VI.getSummaryList()) {
3126  Out << FS;
3127  printSummary(*Summary);
3128  }
3129  Out << ")";
3130  }
3131  Out << ")";
3132  if (!VI.name().empty())
3133  Out << " ; guid = " << VI.getGUID();
3134  Out << "\n";
3135 }
3136 
3138  formatted_raw_ostream &Out) {
3139  if (Name.empty()) {
3140  Out << "<empty name> ";
3141  } else {
3142  if (isalpha(static_cast<unsigned char>(Name[0])) || Name[0] == '-' ||
3143  Name[0] == '$' || Name[0] == '.' || Name[0] == '_')
3144  Out << Name[0];
3145  else
3146  Out << '\\' << hexdigit(Name[0] >> 4) << hexdigit(Name[0] & 0x0F);
3147  for (unsigned i = 1, e = Name.size(); i != e; ++i) {
3148  unsigned char C = Name[i];
3149  if (isalnum(static_cast<unsigned char>(C)) || C == '-' || C == '$' ||
3150  C == '.' || C == '_')
3151  Out << C;
3152  else
3153  Out << '\\' << hexdigit(C >> 4) << hexdigit(C & 0x0F);
3154  }
3155  }
3156 }
3157 
3158 void AssemblyWriter::printNamedMDNode(const NamedMDNode *NMD) {
3159  Out << '!';
3160  printMetadataIdentifier(NMD->getName(), Out);
3161  Out << " = !{";
3162  for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
3163  if (i)
3164  Out << ", ";
3165 
3166  // Write DIExpressions inline.
3167  // FIXME: Ban DIExpressions in NamedMDNodes, they will serve no purpose.
3168  MDNode *Op = NMD->getOperand(i);
3169  if (auto *Expr = dyn_cast<DIExpression>(Op)) {
3170  writeDIExpression(Out, Expr, nullptr, nullptr, nullptr);
3171  continue;
3172  }
3173 
3174  int Slot = Machine.getMetadataSlot(Op);
3175  if (Slot == -1)
3176  Out << "<badref>";
3177  else
3178  Out << '!' << Slot;
3179  }
3180  Out << "}\n";
3181 }
3182 
3184  formatted_raw_ostream &Out) {
3185  switch (Vis) {
3186  case GlobalValue::DefaultVisibility: break;
3187  case GlobalValue::HiddenVisibility: Out << "hidden "; break;
3188  case GlobalValue::ProtectedVisibility: Out << "protected "; break;
3189  }
3190 }
3191 
3192 static void PrintDSOLocation(const GlobalValue &GV,
3193  formatted_raw_ostream &Out) {
3194  // GVs with local linkage or non default visibility are implicitly dso_local,
3195  // so we don't print it.
3196  bool Implicit = GV.hasLocalLinkage() ||
3198  if (GV.isDSOLocal() && !Implicit)
3199  Out << "dso_local ";
3200 }
3201 
3203  formatted_raw_ostream &Out) {
3204  switch (SCT) {
3206  case GlobalValue::DLLImportStorageClass: Out << "dllimport "; break;
3207  case GlobalValue::DLLExportStorageClass: Out << "dllexport "; break;
3208  }
3209 }
3210 
3212  formatted_raw_ostream &Out) {
3213  switch (TLM) {
3215  break;
3217  Out << "thread_local ";
3218  break;
3220  Out << "thread_local(localdynamic) ";
3221  break;
3223  Out << "thread_local(initialexec) ";
3224  break;
3226  Out << "thread_local(localexec) ";
3227  break;
3228  }
3229 }
3230 
3232  switch (UA) {
3234  return "";
3236  return "local_unnamed_addr";
3238  return "unnamed_addr";
3239  }
3240  llvm_unreachable("Unknown UnnamedAddr");
3241 }
3242 
3244  const GlobalObject &GO) {
3245  const Comdat *C = GO.getComdat();
3246  if (!C)
3247  return;
3248 
3249  if (isa<GlobalVariable>(GO))
3250  Out << ',';
3251  Out << " comdat";
3252 
3253  if (GO.getName() == C->getName())
3254  return;
3255 
3256  Out << '(';
3257  PrintLLVMName(Out, C->getName(), ComdatPrefix);
3258  Out << ')';
3259 }
3260 
3261 void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
3262  if (GV->isMaterializable())
3263  Out << "; Materializable\n";
3264 
3265  WriteAsOperandInternal(Out, GV, &TypePrinter, &Machine, GV->getParent());
3266  Out << " = ";
3267 
3268  if (!GV->hasInitializer() && GV->hasExternalLinkage())
3269  Out << "external ";
3270 
3271  Out << getLinkageNameWithSpace(GV->getLinkage());
3272  PrintDSOLocation(*GV, Out);
3273  PrintVisibility(GV->getVisibility(), Out);
3277  if (!UA.empty())
3278  Out << UA << ' ';
3279 
3280  if (unsigned AddressSpace = GV->getType()->getAddressSpace())
3281  Out << "addrspace(" << AddressSpace << ") ";
3282  if (GV->isExternallyInitialized()) Out << "externally_initialized ";
3283  Out << (GV->isConstant() ? "constant " : "global ");
3284  TypePrinter.print(GV->getValueType(), Out);
3285 
3286  if (GV->hasInitializer()) {
3287  Out << ' ';
3288  writeOperand(GV->getInitializer(), false);
3289  }
3290 
3291  if (GV->hasSection()) {
3292  Out << ", section \"";
3293  printEscapedString(GV->getSection(), Out);
3294  Out << '"';
3295  }
3296  if (GV->hasPartition()) {
3297  Out << ", partition \"";
3298  printEscapedString(GV->getPartition(), Out);
3299  Out << '"';
3300  }
3301 
3302  maybePrintComdat(Out, *GV);
3303  if (GV->getAlignment())
3304  Out << ", align " << GV->getAlignment();
3305 
3307  GV->getAllMetadata(MDs);
3308  printMetadataAttachments(MDs, ", ");
3309 
3310  auto Attrs = GV->getAttributes();
3311  if (Attrs.hasAttributes())
3312  Out << " #" << Machine.getAttributeGroupSlot(Attrs);
3313 
3314  printInfoComment(*GV);
3315 }
3316 
3317 void AssemblyWriter::printIndirectSymbol(const GlobalIndirectSymbol *GIS) {
3318  if (GIS->isMaterializable())
3319  Out << "; Materializable\n";
3320 
3321  WriteAsOperandInternal(Out, GIS, &TypePrinter, &Machine, GIS->getParent());
3322  Out << " = ";
3323 
3324  Out << getLinkageNameWithSpace(GIS->getLinkage());
3325  PrintDSOLocation(*GIS, Out);
3326  PrintVisibility(GIS->getVisibility(), Out);
3330  if (!UA.empty())
3331  Out << UA << ' ';
3332 
3333  if (isa<GlobalAlias>(GIS))
3334  Out << "alias ";
3335  else if (isa<GlobalIFunc>(GIS))
3336  Out << "ifunc ";
3337  else
3338  llvm_unreachable("Not an alias or ifunc!");
3339 
3340  TypePrinter.print(GIS->getValueType(), Out);
3341 
3342  Out << ", ";
3343 
3344  const Constant *IS = GIS->getIndirectSymbol();
3345 
3346  if (!IS) {
3347  TypePrinter.print(GIS->getType(), Out);
3348  Out << " <<NULL ALIASEE>>";
3349  } else {
3350  writeOperand(IS, !isa<ConstantExpr>(IS));
3351  }
3352 
3353  if (GIS->hasPartition()) {
3354  Out << ", partition \"";
3355  printEscapedString(GIS->getPartition(), Out);
3356  Out << '"';
3357  }
3358 
3359  printInfoComment(*GIS);
3360  Out << '\n';
3361 }
3362 
3363 void AssemblyWriter::printComdat(const Comdat *C) {
3364  C->print(Out);
3365 }
3366 
3367 void AssemblyWriter::printTypeIdentities() {
3368  if (TypePrinter.empty())
3369  return;
3370 
3371  Out << '\n';
3372 
3373  // Emit all numbered types.
3374  auto &NumberedTypes = TypePrinter.getNumberedTypes();
3375  for (unsigned I = 0, E = NumberedTypes.size(); I != E; ++I) {
3376  Out << '%' << I << " = type ";
3377 
3378  // Make sure we print out at least one level of the type structure, so
3379  // that we do not get %2 = type %2
3380  TypePrinter.printStructBody(NumberedTypes[I], Out);
3381  Out << '\n';
3382  }
3383 
3384  auto &NamedTypes = TypePrinter.getNamedTypes();
3385  for (unsigned I = 0, E = NamedTypes.size(); I != E; ++I) {
3386  PrintLLVMName(Out, NamedTypes[I]->getName(), LocalPrefix);
3387  Out << " = type ";
3388 
3389  // Make sure we print out at least one level of the type structure, so
3390  // that we do not get %FILE = type %FILE
3391  TypePrinter.printStructBody(NamedTypes[I], Out);
3392  Out << '\n';
3393  }
3394 }
3395 
3396 /// printFunction - Print all aspects of a function.
3397 void AssemblyWriter::printFunction(const Function *F) {
3398  // Print out the return type and name.
3399  Out << '\n';
3400 
3401  if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, Out);
3402 
3403  if (F->isMaterializable())
3404  Out << "; Materializable\n";
3405 
3406  const AttributeList &Attrs = F->getAttributes();
3408  AttributeSet AS = Attrs.getFnAttributes();
3409  std::string AttrStr;
3410 
3411  for (const Attribute &Attr : AS) {
3412  if (!Attr.isStringAttribute()) {
3413  if (!AttrStr.empty()) AttrStr += ' ';
3414  AttrStr += Attr.getAsString();
3415  }
3416  }
3417 
3418  if (!AttrStr.empty())
3419  Out << "; Function Attrs: " << AttrStr << '\n';
3420  }
3421 
3422  Machine.incorporateFunction(F);
3423 
3424  if (F->isDeclaration()) {
3425  Out << "declare";
3427  F->getAllMetadata(MDs);
3428  printMetadataAttachments(MDs, " ");
3429  Out << ' ';
3430  } else
3431  Out << "define ";
3432 
3433  Out << getLinkageNameWithSpace(F->getLinkage());
3434  PrintDSOLocation(*F, Out);
3435  PrintVisibility(F->getVisibility(), Out);
3437 
3438  // Print the calling convention.
3439  if (F->getCallingConv() != CallingConv::C) {
3440  PrintCallingConv(F->getCallingConv(), Out);
3441  Out << " ";
3442  }
3443 
3444  FunctionType *FT = F->getFunctionType();
3446  Out << Attrs.getAsString(AttributeList::ReturnIndex) << ' ';
3447  TypePrinter.print(F->getReturnType(), Out);
3448  Out << ' ';
3449  WriteAsOperandInternal(Out, F, &TypePrinter, &Machine, F->getParent());
3450  Out << '(';
3451 
3452  // Loop over the arguments, printing them...
3453  if (F->isDeclaration() && !IsForDebug) {
3454  // We're only interested in the type here - don't print argument names.
3455  for (unsigned I = 0, E = FT->getNumParams(); I != E; ++I) {
3456  // Insert commas as we go... the first arg doesn't get a comma
3457  if (I)
3458  Out << ", ";
3459  // Output type...
3460  TypePrinter.print(FT->getParamType(I), Out);
3461 
3462  AttributeSet ArgAttrs = Attrs.getParamAttributes(I);
3463  if (ArgAttrs.hasAttributes())
3464  Out << ' ' << ArgAttrs.getAsString();
3465  }
3466  } else {
3467  // The arguments are meaningful here, print them in detail.
3468  for (const Argument &Arg : F->args()) {
3469  // Insert commas as we go... the first arg doesn't get a comma
3470  if (Arg.getArgNo() != 0)
3471  Out << ", ";
3472  printArgument(&Arg, Attrs.getParamAttributes(Arg.getArgNo()));
3473  }
3474  }
3475 
3476  // Finish printing arguments...
3477  if (FT->isVarArg()) {
3478  if (FT->getNumParams()) Out << ", ";
3479  Out << "..."; // Output varargs portion of signature!
3480  }
3481  Out << ')';
3483  if (!UA.empty())
3484  Out << ' ' << UA;
3485  // We print the function address space if it is non-zero or if we are writing
3486  // a module with a non-zero program address space or if there is no valid
3487  // Module* so that the file can be parsed without the datalayout string.
3488  const Module *Mod = F->getParent();
3489  if (F->getAddressSpace() != 0 || !Mod ||
3490  Mod->getDataLayout().getProgramAddressSpace() != 0)
3491  Out << " addrspace(" << F->getAddressSpace() << ")";
3493  Out << " #" << Machine.getAttributeGroupSlot(Attrs.getFnAttributes());
3494  if (F->hasSection()) {
3495  Out << " section \"";
3496  printEscapedString(F->getSection(), Out);
3497  Out << '"';
3498  }
3499  if (F->hasPartition()) {
3500  Out << " partition \"";
3501  printEscapedString(F->getPartition(), Out);
3502  Out << '"';
3503  }
3504  maybePrintComdat(Out, *F);
3505  if (F->getAlignment())
3506  Out << " align " << F->getAlignment();
3507  if (F->hasGC())
3508  Out << " gc \"" << F->getGC() << '"';
3509  if (F->hasPrefixData()) {
3510  Out << " prefix ";
3511  writeOperand(F->getPrefixData(), true);
3512  }
3513  if (F->hasPrologueData()) {
3514  Out << " prologue ";
3515  writeOperand(F->getPrologueData(), true);
3516  }
3517  if (F->hasPersonalityFn()) {
3518  Out << " personality ";
3519  writeOperand(F->getPersonalityFn(), /*PrintType=*/true);
3520  }
3521 
3522  if (F->isDeclaration()) {
3523  Out << '\n';
3524  } else {
3526  F->getAllMetadata(MDs);
3527  printMetadataAttachments(MDs, " ");
3528 
3529  Out << " {";
3530  // Output all of the function's basic blocks.
3531  for (const BasicBlock &BB : *F)
3532  printBasicBlock(&BB);
3533 
3534  // Output the function's use-lists.
3535  printUseLists(F);
3536 
3537  Out << "}\n";
3538  }
3539 
3540  Machine.purgeFunction();
3541 }
3542 
3543 /// printArgument - This member is called for every argument that is passed into
3544 /// the function. Simply print it out
3545 void AssemblyWriter::printArgument(const Argument *Arg, AttributeSet Attrs) {
3546  // Output type...
3547  TypePrinter.print(Arg->getType(), Out);
3548 
3549  // Output parameter attributes list
3550  if (Attrs.hasAttributes())
3551  Out << ' ' << Attrs.getAsString();
3552 
3553  // Output name, if available...
3554  if (Arg->hasName()) {
3555  Out << ' ';
3556  PrintLLVMName(Out, Arg);
3557  } else {
3558  int Slot = Machine.getLocalSlot(Arg);
3559  assert(Slot != -1 && "expect argument in function here");
3560  Out << " %" << Slot;
3561  }
3562 }
3563 
3564 /// printBasicBlock - This member is called for each basic block in a method.
3565 void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
3566  bool IsEntryBlock = BB == &BB->getParent()->getEntryBlock();
3567  if (BB->hasName()) { // Print out the label if it exists...
3568  Out << "\n";
3569  PrintLLVMName(Out, BB->getName(), LabelPrefix);
3570  Out << ':';
3571  } else if (!IsEntryBlock) {
3572  Out << "\n";
3573  int Slot = Machine.getLocalSlot(BB);
3574  if (Slot != -1)
3575  Out << Slot << ":";
3576  else
3577  Out << "<badref>:";
3578  }
3579 
3580  if (!BB->getParent()) {
3581  Out.PadToColumn(50);
3582  Out << "; Error: Block without parent!";
3583  } else if (!IsEntryBlock) {
3584  // Output predecessors for the block.
3585  Out.PadToColumn(50);
3586  Out << ";";
3587  const_pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
3588 
3589  if (PI == PE) {
3590  Out << " No predecessors!";
3591  } else {
3592  Out << " preds = ";
3593  writeOperand(*PI, false);
3594  for (++PI; PI != PE; ++PI) {
3595  Out << ", ";
3596  writeOperand(*PI, false);
3597  }
3598  }
3599  }
3600 
3601  Out << "\n";
3602 
3603  if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, Out);
3604 
3605  // Output all of the instructions in the basic block...
3606  for (const Instruction &I : *BB) {
3607  printInstructionLine(I);
3608  }
3609 
3610  if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, Out);
3611 }
3612 
3613 /// printInstructionLine - Print an instruction and a newline character.
3614 void AssemblyWriter::printInstructionLine(const Instruction &I) {
3615  printInstruction(I);
3616  Out << '\n';
3617 }
3618 
3619 /// printGCRelocateComment - print comment after call to the gc.relocate
3620 /// intrinsic indicating base and derived pointer names.
3621 void AssemblyWriter::printGCRelocateComment(const GCRelocateInst &Relocate) {
3622  Out << " ; (";
3623  writeOperand(Relocate.getBasePtr(), false);
3624  Out << ", ";
3625  writeOperand(Relocate.getDerivedPtr(), false);
3626  Out << ")";
3627 }
3628 
3629 /// printInfoComment - Print a little comment after the instruction indicating
3630 /// which slot it occupies.
3631 void AssemblyWriter::printInfoComment(const Value &V) {
3632  if (const auto *Relocate = dyn_cast<GCRelocateInst>(&V))
3633  printGCRelocateComment(*Relocate);
3634 
3635  if (AnnotationWriter)
3636  AnnotationWriter->printInfoComment(V, Out);
3637 }
3638 
3639 static void maybePrintCallAddrSpace(const Value *Operand, const Instruction *I,
3640  raw_ostream &Out) {
3641  // We print the address space of the call if it is non-zero.
3642  unsigned CallAddrSpace = Operand->getType()->getPointerAddressSpace();
3643  bool PrintAddrSpace = CallAddrSpace != 0;
3644  if (!PrintAddrSpace) {
3645  const Module *Mod = getModuleFromVal(I);
3646  // We also print it if it is zero but not equal to the program address space
3647  // or if we can't find a valid Module* to make it possible to parse
3648  // the resulting file even without a datalayout string.
3649  if (!Mod || Mod->getDataLayout().getProgramAddressSpace() != 0)
3650  PrintAddrSpace = true;
3651  }
3652  if (PrintAddrSpace)
3653  Out << " addrspace(" << CallAddrSpace << ")";
3654 }
3655 
3656 // This member is called for each Instruction in a function..
3657 void AssemblyWriter::printInstruction(const Instruction &I) {
3658  if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, Out);
3659 
3660  // Print out indentation for an instruction.
3661  Out << " ";
3662 
3663  // Print out name if it exists...
3664  if (I.hasName()) {
3665  PrintLLVMName(Out, &I);
3666  Out << " = ";
3667  } else if (!I.getType()->isVoidTy()) {
3668  // Print out the def slot taken.
3669  int SlotNum = Machine.getLocalSlot(&I);
3670  if (SlotNum == -1)
3671  Out << "<badref> = ";
3672  else
3673  Out << '%' << SlotNum << " = ";
3674  }
3675 
3676  if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
3677  if (CI->isMustTailCall())
3678  Out << "musttail ";
3679  else if (CI->isTailCall())
3680  Out << "tail ";
3681  else if (CI->isNoTailCall())
3682  Out << "notail ";
3683  }
3684 
3685  // Print out the opcode...
3686  Out << I.getOpcodeName();
3687 
3688  // If this is an atomic load or store, print out the atomic marker.
3689  if ((isa<LoadInst>(I) && cast<LoadInst>(I).isAtomic()) ||
3690  (isa<StoreInst>(I) && cast<StoreInst>(I).isAtomic()))
3691  Out << " atomic";
3692 
3693  if (isa<AtomicCmpXchgInst>(I) && cast<AtomicCmpXchgInst>(I).isWeak())
3694  Out << " weak";
3695 
3696  // If this is a volatile operation, print out the volatile marker.
3697  if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) ||
3698  (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile()) ||
3699  (isa<AtomicCmpXchgInst>(I) && cast<AtomicCmpXchgInst>(I).isVolatile()) ||
3700  (isa<AtomicRMWInst>(I) && cast<AtomicRMWInst>(I).isVolatile()))
3701  Out << " volatile";
3702 
3703  // Print out optimization information.
3704  WriteOptimizationInfo(Out, &I);
3705 
3706  // Print out the compare instruction predicates
3707  if (const CmpInst *CI = dyn_cast<CmpInst>(&I))
3708  Out << ' ' << CmpInst::getPredicateName(CI->getPredicate());
3709 
3710  // Print out the atomicrmw operation
3711  if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(&I))
3712  Out << ' ' << AtomicRMWInst::getOperationName(RMWI->getOperation());
3713 
3714  // Print out the type of the operands...
3715  const Value *Operand = I.getNumOperands() ? I.getOperand(0) : nullptr;
3716 
3717  // Special case conditional branches to swizzle the condition out to the front
3718  if (isa<BranchInst>(I) && cast<BranchInst>(I).isConditional()) {
3719  const BranchInst &BI(cast<BranchInst>(I));
3720  Out << ' ';
3721  writeOperand(BI.getCondition(), true);
3722  Out << ", ";
3723  writeOperand(BI.getSuccessor(0), true);
3724  Out << ", ";
3725  writeOperand(BI.getSuccessor(1), true);
3726 
3727  } else if (isa<SwitchInst>(I)) {
3728  const SwitchInst& SI(cast<SwitchInst>(I));
3729  // Special case switch instruction to get formatting nice and correct.
3730  Out << ' ';
3731  writeOperand(SI.getCondition(), true);
3732  Out << ", ";
3733  writeOperand(SI.getDefaultDest(), true);
3734  Out << " [";
3735  for (auto Case : SI.cases()) {
3736  Out << "\n ";
3737  writeOperand(Case.getCaseValue(), true);
3738  Out << ", ";
3739  writeOperand(Case.getCaseSuccessor(), true);
3740  }
3741  Out << "\n ]";
3742  } else if (isa<IndirectBrInst>(I)) {
3743  // Special case indirectbr instruction to get formatting nice and correct.
3744  Out << ' ';
3745  writeOperand(Operand, true);
3746  Out << ", [";
3747 
3748  for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) {
3749  if (i != 1)
3750  Out << ", ";
3751  writeOperand(I.getOperand(i), true);
3752  }
3753  Out << ']';
3754  } else if (const PHINode *PN = dyn_cast<PHINode>(&I)) {
3755  Out << ' ';
3756  TypePrinter.print(I.getType(), Out);
3757  Out << ' ';
3758 
3759  for (unsigned op = 0, Eop = PN->getNumIncomingValues(); op < Eop; ++op) {
3760  if (op) Out << ", ";
3761  Out << "[ ";
3762  writeOperand(PN->getIncomingValue(op), false); Out << ", ";
3763  writeOperand(PN->getIncomingBlock(op), false); Out << " ]";
3764  }
3765  } else if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(&I)) {
3766  Out << ' ';
3767  writeOperand(I.getOperand(0), true);
3768  for (const unsigned *i = EVI->idx_begin(), *e = EVI->idx_end(); i != e; ++i)
3769  Out << ", " << *i;
3770  } else if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(&I)) {
3771  Out << ' ';
3772  writeOperand(I.getOperand(0), true); Out << ", ";
3773  writeOperand(I.getOperand(1), true);
3774  for (const unsigned *i = IVI->idx_begin(), *e = IVI->idx_end(); i != e; ++i)
3775  Out << ", " << *i;
3776  } else if (const LandingPadInst *LPI = dyn_cast<LandingPadInst>(&I)) {
3777  Out << ' ';
3778  TypePrinter.print(I.getType(), Out);
3779  if (LPI->isCleanup() || LPI->getNumClauses() != 0)
3780  Out << '\n';
3781 
3782  if (LPI->isCleanup())
3783  Out << " cleanup";
3784 
3785  for (unsigned i = 0, e = LPI->getNumClauses(); i != e; ++i) {
3786  if (i != 0 || LPI->isCleanup()) Out << "\n";
3787  if (LPI->isCatch(i))
3788  Out << " catch ";
3789  else
3790  Out << " filter ";
3791 
3792  writeOperand(LPI->getClause(i), true);
3793  }
3794  } else if (const auto *CatchSwitch = dyn_cast<CatchSwitchInst>(&I)) {
3795  Out << " within ";
3796  writeOperand(CatchSwitch->getParentPad(), /*PrintType=*/false);
3797  Out << " [";
3798  unsigned Op = 0;
3799  for (const BasicBlock *PadBB : CatchSwitch->handlers()) {
3800  if (Op > 0)
3801  Out << ", ";
3802  writeOperand(PadBB, /*PrintType=*/true);
3803  ++Op;
3804  }
3805  Out << "] unwind ";
3806  if (const BasicBlock *UnwindDest = CatchSwitch->getUnwindDest())
3807  writeOperand(UnwindDest, /*PrintType=*/true);
3808  else
3809  Out << "to caller";
3810  } else if (const auto *FPI = dyn_cast<FuncletPadInst>(&I)) {
3811  Out << " within ";
3812  writeOperand(FPI->getParentPad(), /*PrintType=*/false);
3813  Out << " [";
3814  for (unsigned Op = 0, NumOps = FPI->getNumArgOperands(); Op < NumOps;
3815  ++Op) {
3816  if (Op > 0)
3817  Out << ", ";
3818  writeOperand(FPI->getArgOperand(Op), /*PrintType=*/true);
3819  }
3820  Out << ']';
3821  } else if (isa<ReturnInst>(I) && !Operand) {
3822  Out << " void";
3823  } else if (const auto *CRI = dyn_cast<CatchReturnInst>(&I)) {
3824  Out << " from ";
3825  writeOperand(CRI->getOperand(0), /*PrintType=*/false);
3826 
3827  Out << " to ";
3828  writeOperand(CRI->getOperand(1), /*PrintType=*/true);
3829  } else if (const auto *CRI = dyn_cast<CleanupReturnInst>(&I)) {
3830  Out << " from ";
3831  writeOperand(CRI->getOperand(0), /*PrintType=*/false);
3832 
3833  Out << " unwind ";
3834  if (CRI->hasUnwindDest())
3835  writeOperand(CRI->getOperand(1), /*PrintType=*/true);
3836  else
3837  Out << "to caller";
3838  } else if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
3839  // Print the calling convention being used.
3840  if (CI->getCallingConv() != CallingConv::C) {
3841  Out << " ";
3842  PrintCallingConv(CI->getCallingConv(), Out);
3843  }
3844 
3845  Operand = CI->getCalledValue();
3846  FunctionType *FTy = CI->getFunctionType();
3847  Type *RetTy = FTy->getReturnType();
3848  const AttributeList &PAL = CI->getAttributes();
3849 
3851  Out << ' ' << PAL.getAsString(AttributeList::ReturnIndex);
3852 
3853  // Only print addrspace(N) if necessary:
3854  maybePrintCallAddrSpace(Operand, &I, Out);
3855 
3856  // If possible, print out the short form of the call instruction. We can
3857  // only do this if the first argument is a pointer to a nonvararg function,
3858  // and if the return type is not a pointer to a function.
3859  //
3860  Out << ' ';
3861  TypePrinter.print(FTy->isVarArg() ? FTy : RetTy, Out);
3862  Out << ' ';
3863  writeOperand(Operand, false);
3864  Out << '(';
3865  for (unsigned op = 0, Eop = CI->getNumArgOperands(); op < Eop; ++op) {
3866  if (op > 0)
3867  Out << ", ";
3868  writeParamOperand(CI->getArgOperand(op), PAL.getParamAttributes(op));
3869  }
3870 
3871  // Emit an ellipsis if this is a musttail call in a vararg function. This
3872  // is only to aid readability, musttail calls forward varargs by default.
3873  if (CI->isMustTailCall() && CI->getParent() &&
3874  CI->getParent()->getParent() &&
3875  CI->getParent()->getParent()->isVarArg())
3876  Out << ", ...";
3877 
3878  Out << ')';
3880  Out << " #" << Machine.getAttributeGroupSlot(PAL.getFnAttributes());
3881 
3882  writeOperandBundles(CI);
3883  } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
3884  Operand = II->getCalledValue();
3885  FunctionType *FTy = II->getFunctionType();
3886  Type *RetTy = FTy->getReturnType();
3887  const AttributeList &PAL = II->getAttributes();
3888 
3889  // Print the calling convention being used.
3890  if (II->getCallingConv() != CallingConv::C) {
3891  Out << " ";
3892  PrintCallingConv(II->getCallingConv(), Out);
3893  }
3894 
3896  Out << ' ' << PAL.getAsString(AttributeList::ReturnIndex);
3897 
3898  // Only print addrspace(N) if necessary:
3899  maybePrintCallAddrSpace(Operand, &I, Out);
3900 
3901  // If possible, print out the short form of the invoke instruction. We can
3902  // only do this if the first argument is a pointer to a nonvararg function,
3903  // and if the return type is not a pointer to a function.
3904  //
3905  Out << ' ';
3906  TypePrinter.print(FTy->isVarArg() ? FTy : RetTy, Out);
3907  Out << ' ';
3908  writeOperand(Operand, false);
3909  Out << '(';
3910  for (unsigned op = 0, Eop = II->getNumArgOperands(); op < Eop; ++op) {
3911  if (op)
3912  Out << ", ";
3913  writeParamOperand(II->getArgOperand(op), PAL.getParamAttributes(op));
3914  }
3915 
3916  Out << ')';
3918  Out << " #" << Machine.getAttributeGroupSlot(PAL.getFnAttributes());
3919 
3920  writeOperandBundles(II);
3921 
3922  Out << "\n to ";
3923  writeOperand(II->getNormalDest(), true);
3924  Out << " unwind ";
3925  writeOperand(II->getUnwindDest(), true);
3926  } else if (const CallBrInst *CBI = dyn_cast<CallBrInst>(&I)) {
3927  Operand = CBI->getCalledValue();
3928  FunctionType *FTy = CBI->getFunctionType();
3929  Type *RetTy = FTy->getReturnType();
3930  const AttributeList &PAL = CBI->getAttributes();
3931 
3932  // Print the calling convention being used.
3933  if (CBI->getCallingConv() != CallingConv::C) {
3934  Out << " ";
3935  PrintCallingConv(CBI->getCallingConv(), Out);
3936  }
3937 
3939  Out << ' ' << PAL.getAsString(AttributeList::ReturnIndex);
3940 
3941  // If possible, print out the short form of the callbr instruction. We can
3942  // only do this if the first argument is a pointer to a nonvararg function,
3943  // and if the return type is not a pointer to a function.
3944  //
3945  Out << ' ';
3946  TypePrinter.print(FTy->isVarArg() ? FTy : RetTy, Out);
3947  Out << ' ';
3948  writeOperand(Operand, false);
3949  Out << '(';
3950  for (unsigned op = 0, Eop = CBI->getNumArgOperands(); op < Eop; ++op) {
3951  if (op)
3952  Out << ", ";
3953  writeParamOperand(CBI->getArgOperand(op), PAL.getParamAttributes(op));
3954  }
3955 
3956  Out << ')';
3958  Out << " #" << Machine.getAttributeGroupSlot(PAL.getFnAttributes());
3959 
3960  writeOperandBundles(CBI);
3961 
3962  Out << "\n to ";
3963  writeOperand(CBI->getDefaultDest(), true);
3964  Out << " [";
3965  for (unsigned i = 0, e = CBI->getNumIndirectDests(); i != e; ++i) {
3966  if (i != 0)
3967  Out << ", ";
3968  writeOperand(CBI->getIndirectDest(i), true);
3969  }
3970  Out << ']';
3971  } else if (const AllocaInst *AI = dyn_cast<AllocaInst>(&I)) {
3972  Out << ' ';
3973  if (AI->isUsedWithInAlloca())
3974  Out << "inalloca ";
3975  if (AI->isSwiftError())
3976  Out << "swifterror ";
3977  TypePrinter.print(AI->getAllocatedType(), Out);
3978 
3979  // Explicitly write the array size if the code is broken, if it's an array
3980  // allocation, or if the type is not canonical for scalar allocations. The
3981  // latter case prevents the type from mutating when round-tripping through
3982  // assembly.
3983  if (!AI->getArraySize() || AI->isArrayAllocation() ||
3984  !AI->getArraySize()->getType()->isIntegerTy(32)) {
3985  Out << ", ";
3986  writeOperand(AI->getArraySize(), true);
3987  }
3988  if (AI->getAlignment()) {
3989  Out << ", align " << AI->getAlignment();
3990  }
3991 
3992  unsigned AddrSpace = AI->getType()->getAddressSpace();
3993  if (AddrSpace != 0) {
3994  Out << ", addrspace(" << AddrSpace << ')';
3995  }
3996  } else if (isa<CastInst>(I)) {
3997  if (Operand) {
3998  Out << ' ';
3999  writeOperand(Operand, true); // Work with broken code
4000  }
4001  Out << " to ";
4002  TypePrinter.print(I.getType(), Out);
4003  } else if (isa<VAArgInst>(I)) {
4004  if (Operand) {
4005  Out << ' ';
4006  writeOperand(Operand, true); // Work with broken code
4007  }
4008  Out << ", ";
4009  TypePrinter.print(I.getType(), Out);
4010  } else if (Operand) { // Print the normal way.
4011  if (const auto *GEP = dyn_cast<GetElementPtrInst>(&I)) {
4012  Out << ' ';
4013  TypePrinter.print(GEP->getSourceElementType(), Out);
4014  Out << ',';
4015  } else if (const auto *LI = dyn_cast<LoadInst>(&I)) {
4016  Out << ' ';
4017  TypePrinter.print(LI->getType(), Out);
4018  Out << ',';
4019  }
4020 
4021  // PrintAllTypes - Instructions who have operands of all the same type
4022  // omit the type from all but the first operand. If the instruction has
4023  // different type operands (for example br), then they are all printed.
4024  bool PrintAllTypes = false;
4025  Type *TheType = Operand->getType();
4026 
4027  // Select, Store and ShuffleVector always print all types.
4028  if (isa<SelectInst>(I) || isa<StoreInst>(I) || isa<ShuffleVectorInst>(I)
4029  || isa<ReturnInst>(I)) {
4030  PrintAllTypes = true;
4031  } else {
4032  for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
4033  Operand = I.getOperand(i);
4034  // note that Operand shouldn't be null, but the test helps make dump()
4035  // more tolerant of malformed IR
4036  if (Operand && Operand->getType() != TheType) {
4037  PrintAllTypes = true; // We have differing types! Print them all!
4038  break;
4039  }
4040  }
4041  }
4042 
4043  if (!PrintAllTypes) {
4044  Out << ' ';
4045  TypePrinter.print(TheType, Out);
4046  }
4047 
4048  Out << ' ';
4049  for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
4050  if (i) Out << ", ";
4051  writeOperand(I.getOperand(i), PrintAllTypes);
4052  }
4053  }
4054 
4055  // Print atomic ordering/alignment for memory operations
4056  if (const LoadInst *LI = dyn_cast<LoadInst>(&I)) {
4057  if (LI->isAtomic())
4058  writeAtomic(LI->getContext(), LI->getOrdering(), LI->getSyncScopeID());
4059  if (LI->getAlignment())
4060  Out << ", align " << LI->getAlignment();
4061  } else if (const StoreInst *SI = dyn_cast<StoreInst>(&I)) {
4062  if (SI->isAtomic())
4063  writeAtomic(SI->getContext(), SI->getOrdering(), SI->getSyncScopeID());
4064  if (SI->getAlignment())
4065  Out << ", align " << SI->getAlignment();
4066  } else if (const AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(&I)) {
4067  writeAtomicCmpXchg(CXI->getContext(), CXI->getSuccessOrdering(),
4068  CXI->getFailureOrdering(), CXI->getSyncScopeID());
4069  } else if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(&I)) {
4070  writeAtomic(RMWI->getContext(), RMWI->getOrdering(),
4071  RMWI->getSyncScopeID());
4072  } else if (const FenceInst *FI = dyn_cast<FenceInst>(&I)) {
4073  writeAtomic(FI->getContext(), FI->getOrdering(), FI->getSyncScopeID());
4074  }
4075 
4076  // Print Metadata info.
4078  I.getAllMetadata(InstMD);
4079  printMetadataAttachments(InstMD, ", ");
4080 
4081  // Print a nice comment.
4082  printInfoComment(I);
4083 }
4084 
4085 void AssemblyWriter::printMetadataAttachments(
4086  const SmallVectorImpl<std::pair<unsigned, MDNode *>> &MDs,
4087  StringRef Separator) {
4088  if (MDs.empty())
4089  return;
4090 
4091  if (MDNames.empty())
4092  MDs[0].second->getContext().getMDKindNames(MDNames);
4093 
4094  for (const auto &I : MDs) {
4095  unsigned Kind = I.first;
4096  Out << Separator;
4097  if (Kind < MDNames.size()) {
4098  Out << "!";
4099  printMetadataIdentifier(MDNames[Kind], Out);
4100  } else
4101  Out << "!<unknown kind #" << Kind << ">";
4102  Out << ' ';
4103  WriteAsOperandInternal(Out, I.second, &TypePrinter, &Machine, TheModule);
4104  }
4105 }
4106 
4107 void AssemblyWriter::writeMDNode(unsigned Slot, const MDNode *Node) {
4108  Out << '!' << Slot << " = ";
4109  printMDNodeBody(Node);
4110  Out << "\n";
4111 }
4112 
4113 void AssemblyWriter::writeAllMDNodes() {
4115  Nodes.resize(Machine.mdn_size());
4116  for (SlotTracker::mdn_iterator I = Machine.mdn_begin(), E = Machine.mdn_end();
4117  I != E; ++I)
4118  Nodes[I->second] = cast<MDNode>(I->first);
4119 
4120  for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
4121  writeMDNode(i, Nodes[i]);
4122  }
4123 }
4124 
4125 void AssemblyWriter::printMDNodeBody(const MDNode *Node) {
4126  WriteMDNodeBodyInternal(Out, Node, &TypePrinter, &Machine, TheModule);
4127 }
4128 
4129 void AssemblyWriter::writeAllAttributeGroups() {
4130  std::vector<std::pair<AttributeSet, unsigned>> asVec;
4131  asVec.resize(Machine.as_size());
4132 
4133  for (SlotTracker::as_iterator I = Machine.as_begin(), E = Machine.as_end();
4134  I != E; ++I)
4135  asVec[I->second] = *I;
4136 
4137  for (const auto &I : asVec)
4138  Out << "attributes #" << I.second << " = { "
4139  << I.first.getAsString(true) << " }\n";
4140 }
4141 
4142 void AssemblyWriter::printUseListOrder(const UseListOrder &Order) {
4143  bool IsInFunction = Machine.getFunction();
4144  if (IsInFunction)
4145  Out << " ";
4146 
4147  Out << "uselistorder";
4148  if (const BasicBlock *BB =
4149  IsInFunction ? nullptr : dyn_cast<BasicBlock>(Order.V)) {
4150  Out << "_bb ";
4151  writeOperand(BB->getParent(), false);
4152  Out << ", ";
4153  writeOperand(BB, false);
4154  } else {
4155  Out << " ";
4156  writeOperand(Order.V, true);
4157  }
4158  Out << ", { ";
4159 
4160  assert(Order.Shuffle.size() >= 2 && "Shuffle too small");
4161  Out << Order.Shuffle[0];
4162  for (unsigned I = 1, E = Order.Shuffle.size(); I != E; ++I)
4163  Out << ", " << Order.Shuffle[I];
4164  Out << " }\n";
4165 }
4166 
4167 void AssemblyWriter::printUseLists(const Function *F) {
4168  auto hasMore =
4169  [&]() { return !UseListOrders.empty() && UseListOrders.back().F == F; };
4170  if (!hasMore())
4171  // Nothing to do.
4172  return;
4173 
4174  Out << "\n; uselistorder directives\n";
4175  while (hasMore()) {
4176  printUseListOrder(UseListOrders.back());
4177  UseListOrders.pop_back();
4178  }
4179 }
4180 
4181 //===----------------------------------------------------------------------===//
4182 // External Interface declarations
4183 //===----------------------------------------------------------------------===//
4184 
4186  bool ShouldPreserveUseListOrder,
4187  bool IsForDebug) const {
4188  SlotTracker SlotTable(this->getParent());
4189  formatted_raw_ostream OS(ROS);
4190  AssemblyWriter W(OS, SlotTable, this->getParent(), AAW,
4191  IsForDebug,
4192  ShouldPreserveUseListOrder);
4193  W.printFunction(this);
4194 }
4195 
4197  bool ShouldPreserveUseListOrder, bool IsForDebug) const {
4198  SlotTracker SlotTable(this);
4199  formatted_raw_ostream OS(ROS);
4200  AssemblyWriter W(OS, SlotTable, this, AAW, IsForDebug,
4201  ShouldPreserveUseListOrder);
4202  W.printModule(this);
4203 }
4204 
4205 void NamedMDNode::print(raw_ostream &ROS, bool IsForDebug) const {
4206  SlotTracker SlotTable(getParent());
4207  formatted_raw_ostream OS(ROS);
4208  AssemblyWriter W(OS, SlotTable, getParent(), nullptr, IsForDebug);
4209  W.printNamedMDNode(this);
4210 }
4211 
4213  bool IsForDebug) const {
4214  Optional<SlotTracker> LocalST;
4215  SlotTracker *SlotTable;
4216  if (auto *ST = MST.getMachine())
4217  SlotTable = ST;
4218  else {
4219  LocalST.emplace(getParent());
4220  SlotTable = &*LocalST;
4221  }
4222 
4223  formatted_raw_ostream OS(ROS);
4224  AssemblyWriter W(OS, *SlotTable, getParent(), nullptr, IsForDebug);
4225  W.printNamedMDNode(this);
4226 }
4227 
4228 void Comdat::print(raw_ostream &ROS, bool /*IsForDebug*/) const {
4230  ROS << " = comdat ";
4231 
4232  switch (getSelectionKind()) {
4233  case Comdat::Any:
4234  ROS << "any";
4235  break;
4236  case Comdat::ExactMatch:
4237  ROS << "exactmatch";
4238  break;
4239  case Comdat::Largest:
4240  ROS << "largest";
4241  break;
4242  case Comdat::NoDuplicates:
4243  ROS << "noduplicates";
4244  break;
4245  case Comdat::SameSize:
4246  ROS << "samesize";
4247  break;
4248  }
4249 
4250  ROS << '\n';
4251 }
4252 
4253 void Type::print(raw_ostream &OS, bool /*IsForDebug*/, bool NoDetails) const {
4254  TypePrinting TP;
4255  TP.print(const_cast<Type*>(this), OS);
4256 
4257  if (NoDetails)
4258  return;
4259 
4260  // If the type is a named struct type, print the body as well.
4261  if (StructType *STy = dyn_cast<StructType>(const_cast<Type*>(this)))
4262  if (!STy->isLiteral()) {
4263  OS << " = type ";
4264  TP.printStructBody(STy, OS);
4265  }
4266 }
4267 
4268 static bool isReferencingMDNode(const Instruction &I) {
4269  if (const auto *CI = dyn_cast<CallInst>(&I))
4270  if (Function *F = CI->getCalledFunction())
4271  if (F->isIntrinsic())
4272  for (auto &Op : I.operands())
4273  if (auto *V = dyn_cast_or_null<MetadataAsValue>(Op))
4274  if (isa<MDNode>(V->getMetadata()))
4275  return true;
4276  return false;
4277 }
4278 
4279 void Value::print(raw_ostream &ROS, bool IsForDebug) const {
4280  bool ShouldInitializeAllMetadata = false;
4281  if (auto *I = dyn_cast<Instruction>(this))
4282  ShouldInitializeAllMetadata = isReferencingMDNode(*I);
4283  else if (isa<Function>(this) || isa<MetadataAsValue>(this))
4284  ShouldInitializeAllMetadata = true;
4285 
4286  ModuleSlotTracker MST(getModuleFromVal(this), ShouldInitializeAllMetadata);
4287  print(ROS, MST, IsForDebug);
4288 }
4289 
4291  bool IsForDebug) const {
4292  formatted_raw_ostream OS(ROS);
4293  SlotTracker EmptySlotTable(static_cast<const Module *>(nullptr));
4294  SlotTracker &SlotTable =
4295  MST.getMachine() ? *MST.getMachine() : EmptySlotTable;
4296  auto incorporateFunction = [&](const Function *F) {
4297  if (F)
4298  MST.incorporateFunction(*F);
4299  };
4300 
4301  if (const Instruction *I = dyn_cast<Instruction>(this)) {
4302  incorporateFunction(I->getParent() ? I->getParent()->getParent() : nullptr);
4303  AssemblyWriter W(OS, SlotTable, getModuleFromVal(I), nullptr, IsForDebug);
4304  W.printInstruction(*I);
4305  } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(this)) {
4306  incorporateFunction(BB->getParent());
4307  AssemblyWriter W(OS, SlotTable, getModuleFromVal(BB), nullptr, IsForDebug);
4308  W.printBasicBlock(BB);
4309  } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(this)) {
4310  AssemblyWriter W(OS, SlotTable, GV->getParent(), nullptr, IsForDebug);
4311  if (const GlobalVariable *V = dyn_cast<GlobalVariable>(GV))
4312  W.printGlobal(V);
4313  else if (const Function *F = dyn_cast<Function>(GV))
4314  W.printFunction(F);
4315  else
4316  W.printIndirectSymbol(cast<GlobalIndirectSymbol>(GV));
4317  } else if (const MetadataAsValue *V = dyn_cast<MetadataAsValue>(this)) {
4318  V->getMetadata()->print(ROS, MST, getModuleFromVal(V));
4319  } else if (const Constant *C = dyn_cast<Constant>(this)) {
4320  TypePrinting TypePrinter;
4321  TypePrinter.print(C->getType(), OS);
4322  OS << ' ';
4323  WriteConstantInternal(OS, C, TypePrinter, MST.getMachine(), nullptr);
4324  } else if (isa<InlineAsm>(this) || isa<Argument>(this)) {
4325  this->printAsOperand(OS, /* PrintType */ true, MST);
4326  } else {
4327  llvm_unreachable("Unknown value to print out!");
4328  }
4329 }
4330 
4331 /// Print without a type, skipping the TypePrinting object.
4332 ///
4333 /// \return \c true iff printing was successful.
4334 static bool printWithoutType(const Value &V, raw_ostream &O,
4335  SlotTracker *Machine, const Module *M) {
4336  if (V.hasName() || isa<GlobalValue>(V) ||
4337  (!isa<Constant>(V) && !isa<MetadataAsValue>(V))) {
4338  WriteAsOperandInternal(O, &V, nullptr, Machine, M);
4339  return true;
4340  }
4341  return false;
4342 }
4343 
4344 static void printAsOperandImpl(const Value &V, raw_ostream &O, bool PrintType,
4345  ModuleSlotTracker &MST) {
4346  TypePrinting TypePrinter(MST.getModule());
4347  if (PrintType) {
4348  TypePrinter.print(V.getType(), O);
4349  O << ' ';
4350  }
4351 
4352  WriteAsOperandInternal(O, &V, &TypePrinter, MST.getMachine(),
4353  MST.getModule());
4354 }
4355 
4356 void Value::printAsOperand(raw_ostream &O, bool PrintType,
4357  const Module *M) const {
4358  if (!M)
4359  M = getModuleFromVal(this);
4360 
4361  if (!PrintType)
4362  if (printWithoutType(*this, O, nullptr, M))
4363  return;
4364 
4366  M, /* ShouldInitializeAllMetadata */ isa<MetadataAsValue>(this));
4367  ModuleSlotTracker MST(Machine, M);
4368  printAsOperandImpl(*this, O, PrintType, MST);
4369 }
4370 
4371 void Value::printAsOperand(raw_ostream &O, bool PrintType,
4372  ModuleSlotTracker &MST) const {
4373  if (!PrintType)
4374  if (printWithoutType(*this, O, MST.getMachine(), MST.getModule()))
4375  return;
4376 
4377  printAsOperandImpl(*this, O, PrintType, MST);
4378 }
4379 
4380 static void printMetadataImpl(raw_ostream &ROS, const Metadata &MD,
4381  ModuleSlotTracker &MST, const Module *M,
4382  bool OnlyAsOperand) {
4383  formatted_raw_ostream OS(ROS);
4384 
4385  TypePrinting TypePrinter(M);
4386 
4387  WriteAsOperandInternal(OS, &MD, &TypePrinter, MST.getMachine(), M,
4388  /* FromValue */ true);
4389 
4390  auto *N = dyn_cast<MDNode>(&MD);
4391  if (OnlyAsOperand || !N || isa<DIExpression>(MD))
4392  return;
4393 
4394  OS << " = ";
4395  WriteMDNodeBodyInternal(OS, N, &TypePrinter, MST.getMachine(), M);
4396 }
4397 
4398 void Metadata::printAsOperand(raw_ostream &OS, const Module *M) const {
4399  ModuleSlotTracker MST(M, isa<MDNode>(this));
4400  printMetadataImpl(OS, *this, MST, M, /* OnlyAsOperand */ true);
4401 }
4402 
4404  const Module *M) const {
4405  printMetadataImpl(OS, *this, MST, M, /* OnlyAsOperand */ true);
4406 }
4407 
4409  bool /*IsForDebug*/) const {
4410  ModuleSlotTracker MST(M, isa<MDNode>(this));
4411  printMetadataImpl(OS, *this, MST, M, /* OnlyAsOperand */ false);
4412 }
4413 
4415  const Module *M, bool /*IsForDebug*/) const {
4416  printMetadataImpl(OS, *this, MST, M, /* OnlyAsOperand */ false);
4417 }
4418 
4419 void ModuleSummaryIndex::print(raw_ostream &ROS, bool IsForDebug) const {
4420  SlotTracker SlotTable(this);
4421  formatted_raw_ostream OS(ROS);
4422  AssemblyWriter W(OS, SlotTable, this, IsForDebug);
4423  W.printModuleSummaryIndex();
4424 }
4425 
4426 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
4427 // Value::dump - allow easy printing of Values from the debugger.
4429 void Value::dump() const { print(dbgs(), /*IsForDebug=*/true); dbgs() << '\n'; }
4430 
4431 // Type::dump - allow easy printing of Types from the debugger.
4433 void Type::dump() const { print(dbgs(), /*IsForDebug=*/true); dbgs() << '\n'; }
4434 
4435 // Module::dump() - Allow printing of Modules from the debugger.
4437 void Module::dump() const {
4438  print(dbgs(), nullptr,
4439  /*ShouldPreserveUseListOrder=*/false, /*IsForDebug=*/true);
4440 }
4441 
4442 // Allow printing of Comdats from the debugger.
4444 void Comdat::dump() const { print(dbgs(), /*IsForDebug=*/true); }
4445 
4446 // NamedMDNode::dump() - Allow printing of NamedMDNodes from the debugger.
4448 void NamedMDNode::dump() const { print(dbgs(), /*IsForDebug=*/true); }
4449 
4451 void Metadata::dump() const { dump(nullptr); }
4452 
4454 void Metadata::dump(const Module *M) const {
4455  print(dbgs(), M, /*IsForDebug=*/true);
4456  dbgs() << '\n';
4457 }
4458 
4459 // Allow printing of ModuleSummaryIndex from the debugger.
4461 void ModuleSummaryIndex::dump() const { print(dbgs(), /*IsForDebug=*/true); }
4462 #endif
static unsigned getBitWidth(Type *Ty, const DataLayout &DL)
Returns the bitwidth of the given scalar or pointer type.
Type * getVectorElementType() const
Definition: Type.h:375
AttributeSet getAttributes() const
Return the attribute set for this global.
uint64_t CallInst * C
bool hasOperandBundles() const
Return true if this User has any operand bundles.
Definition: InstrTypes.h:1733
static const fltSemantics & IEEEquad() LLVM_READNONE
Definition: APFloat.cpp:161
StringRef getSection() const
Get the custom section of this global if it has one.
Definition: GlobalObject.h:108
static SlotTracker * createSlotTracker(const Value *V)
Definition: AsmWriter.cpp:863
bool isIntrinsic() const
isIntrinsic - Returns true if the function&#39;s name starts with "llvm.".
Definition: Function.h:198
unsigned getAlignment() const
Definition: GlobalObject.h:73
use_iterator use_end()
Definition: Value.h:366
7: Labels
Definition: Type.h:64
X86_FastCall - &#39;fast&#39; analog of X86_StdCall.
Definition: CallingConv.h:96
const std::string & getTargetTriple() const
Get the target triple which is a string describing the target host.
Definition: Module.h:240
std::vector< TypeIdOffsetVtableInfo > TypeIdCompatibleVtableInfo
List of vtable definitions decorated by a particular type identifier, and their corresponding offsets...
ThreadLocalMode getThreadLocalMode() const
Definition: GlobalValue.h:258
static void WriteOptimizationInfo(raw_ostream &Out, const User *U)
Definition: AsmWriter.cpp:1249
This class is the base class for the comparison instructions.
Definition: InstrTypes.h:722
unsigned Live
In per-module summary, indicate that the global value must be considered a live root for index-based ...
PTX_Kernel - Call to a PTX kernel.
Definition: CallingConv.h:119
iterator_range< use_iterator > uses()
Definition: Value.h:374
static std::string getLinkageName(GlobalValue::LinkageTypes LT)
Definition: AsmWriter.cpp:2912
bool hasLocalLinkage() const
Definition: GlobalValue.h:445
This instruction extracts a struct member or array element value from an aggregate value...
Special purpose, only applies to global arrays.
Definition: GlobalValue.h:54
uint64_t getZExtValue() const
Get zero extended value.
Definition: APInt.h:1571
iterator_range< CaseIt > cases()
Iteration adapter for range-for loops.
This class represents an incoming formal argument to a Function.
Definition: Argument.h:29
void purgeFunction()
After calling incorporateFunction, use this method to remove the most recently incorporated function ...
Definition: AsmWriter.cpp:1085
LLVMContext & Context
static bool printWithoutType(const Value &V, raw_ostream &O, SlotTracker *Machine, const Module *M)
Print without a type, skipping the TypePrinting object.
Definition: AsmWriter.cpp:4334
Atomic ordering constants.
Calling convention used by HipHop Virtual Machine (HHVM) to perform calls to and from translation cac...
Definition: CallingConv.h:168
MDNode * getOperand(unsigned i) const
Definition: Metadata.cpp:1080
DenseMap< GlobalValue::GUID, unsigned >::iterator guid_iterator
GUID map iterators.
Definition: AsmWriter.cpp:778
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
uint64_t GUID
Declare a type to represent a global unique identifier for a global value.
Definition: GlobalValue.h:502
This class represents lattice values for constants.
Definition: AllocatorList.h:23
ARM_APCS - ARM Procedure Calling Standard calling convention (obsolete, but still used on some target...
Definition: CallingConv.h:100
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
Definition: Compiler.h:484
Type * getParamType(unsigned i) const
Parameter type accessors.
Definition: DerivedTypes.h:140
unsigned Linkage
The linkage type of the associated global value.
const GlobalValueSummary & getAliasee() const
Calling convention used for Mesa/AMDPAL pixel shaders.
Definition: CallingConv.h:199
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
2: 32-bit floating point type
Definition: Type.h:59
An instruction for ordering other memory operations.
Definition: Instructions.h:462
Same, but only replaced by something equivalent.
Definition: GlobalValue.h:53
static StringRef getUnnamedAddrEncoding(GlobalVariable::UnnamedAddr UA)
Definition: AsmWriter.cpp:3231
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
An instruction that atomically checks whether a specified value is in a memory location, and, if it is, stores a new value there.
Definition: Instructions.h:538
FormattedNumber format_hex(uint64_t N, unsigned Width, bool Upper=false)
format_hex - Output N as a fixed width hexadecimal.
Definition: Format.h:186
Global variable summary information to aid decisions and implementation of importing.
formatted_raw_ostream - A raw_ostream that wraps another one and keeps track of line and column posit...
Unsatisfiable type (i.e. no global has this type metadata)
bool isMaterializable() const
If this function&#39;s Module is being lazily streamed in functions from disk or some other source...
Definition: Globals.cpp:44
Available for inspection, not emission.
Definition: GlobalValue.h:49
Intel_OCL_BI - Calling conventions for Intel OpenCL built-ins.
Definition: CallingConv.h:144
PrefixType
Definition: AsmWriter.cpp:390
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
ArrayRef< ValueInfo > refs() const
Return the list of values referenced by this global value definition.
This class represents a function call, abstracting a target machine&#39;s calling convention.
This file contains the declarations for metadata subclasses.
void emplace(ArgTypes &&... Args)
Create a new object by constructing it in place with the given arguments.
Definition: Optional.h:237
MSVC calling convention that passes vectors and vector aggregates in SSE registers.
Definition: CallingConv.h:162
Value * getCondition() const
float convertToFloat() const
Definition: APFloat.h:1113
This file contains the declaration of the Comdat class, which represents a single COMDAT in LLVM...
Like Internal, but omit from symbol table.
Definition: GlobalValue.h:56
static void PrintDLLStorageClass(GlobalValue::DLLStorageClassTypes SCT, formatted_raw_ostream &Out)
Definition: AsmWriter.cpp:3202
bool global_empty() const
Definition: Module.h:585
Externally visible function.
Definition: GlobalValue.h:48
void printEscapedString(StringRef Name, raw_ostream &Out)
Print each character of the specified string, escaping it if it is not printable or if it is an escap...
std::vector< VFuncId > TypeTestAssumeVCalls
List of virtual calls made by this function using (respectively) llvm.assume(llvm.type.test) or llvm.type.checked.load intrinsics that do not have all constant integer arguments.
bool hasPrologueData() const
Check whether this function has prologue data.
Definition: Function.h:750
The data referenced by the COMDAT must be the same size.
Definition: Comdat.h:38
const std::string & getDataLayoutStr() const
Get the data layout string for the module&#39;s target platform.
Definition: Module.h:231
unsigned second
BasicBlock * getSuccessor(unsigned i) const
13: Structures
Definition: Type.h:73
Metadata node.
Definition: Metadata.h:863
int getGlobalSlot(const GlobalValue *V)
getGlobalSlot - Get the slot number of a global value.
Definition: AsmWriter.cpp:1094
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1100
F(f)
4: 80-bit floating point type (X87)
Definition: Type.h:61
const fltSemantics & getSemantics() const
Definition: APFloat.h:1170
const MDOperand & getOperand(unsigned I) const
Definition: Metadata.h:1068
unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
Definition: DerivedTypes.h:621
param_iterator param_end() const
Definition: DerivedTypes.h:134
An instruction for reading from memory.
Definition: Instructions.h:169
Manage lifetime of a slot tracker for printing IR.
1: 16-bit floating point type
Definition: Type.h:58
bool hasExternalWeakLinkage() const
Definition: GlobalValue.h:446
an instruction that atomically reads a memory location, combines it with another value, and then stores the result back.
Definition: Instructions.h:701
Hexagon Common GEP
Value * getCondition() const
Kind
Specifies which kind of type check we should emit for this byte array.
#define op(i)
OperandBundleUse getOperandBundleAt(unsigned Index) const
Return the operand bundle at a specific index.
Definition: InstrTypes.h:1777
static void writeDILocation(raw_ostream &Out, const DILocation *DL, TypePrinting *TypePrinter, SlotTracker *Machine, const Module *Context)
Definition: AsmWriter.cpp:1783
15: Pointers
Definition: Type.h:75
This defines the Use class.
void getAllMetadata(SmallVectorImpl< std::pair< unsigned, MDNode *>> &MDs) const
Appends all attachments for the global to MDs, sorting by attachment ID.
Definition: Metadata.cpp:1413
12: Functions
Definition: Type.h:72
int getMetadataSlot(const MDNode *N)
getMetadataSlot - Get the slot number of a MDNode.
Definition: AsmWriter.cpp:1104
std::map< uint64_t, WholeProgramDevirtResolution > WPDRes
Mapping from byte offset to whole-program devirt resolution for that (typeid, byte offset) pair...
static void PrintCallingConv(unsigned cc, raw_ostream &Out)
Definition: AsmWriter.cpp:344
Tentative definitions.
Definition: GlobalValue.h:58
void dump() const
Dump to stderr (for debugging).
Definition: AsmWriter.cpp:4461
static void PrintDSOLocation(const GlobalValue &GV, formatted_raw_ostream &Out)
Definition: AsmWriter.cpp:3192
Constant * getPrologueData() const
Get the prologue data associated with this function.
Definition: Function.cpp:1485
TypeTestResolution TTRes
SlotTracker * getMachine()
Lazily creates a slot tracker.
Definition: AsmWriter.cpp:833
static void predictValueUseListOrder(const Value *V, const Function *F, OrderMap &OM, UseListOrderStack &Stack)
Definition: AsmWriter.cpp:244
struct llvm::GlobalVarSummary::GVarFlags VarFlags
const TypeIdInfo * getTypeIdInfo() const
void print(raw_ostream &OS, bool IsForDebug=false) const
Print to an output stream.
Definition: AsmWriter.cpp:4419
static std::string getLinkageNameWithSpace(GlobalValue::LinkageTypes LT)
Definition: AsmWriter.cpp:2943
void dump() const
Support for debugging, callable in GDB: V->dump()
Definition: AsmWriter.cpp:4429
The address of a basic block.
Definition: Constants.h:839
unsigned NotEligibleToImport
Indicate if the global value cannot be imported (e.g.
GlobalValue::GUID getGUID() const
A tuple of MDNodes.
Definition: Metadata.h:1325
bool hasPrefixData() const
Check whether this function has prefix data.
Definition: Function.h:741
APInt getLoBits(unsigned numBits) const
Compute an APInt containing numBits lowbits from this APInt.
Definition: APInt.cpp:570
std::vector< unsigned > Shuffle
Definition: UseListOrder.h:29
static OrderMap orderModule(const Module *M)
Definition: AsmWriter.cpp:134
static void printMetadataImpl(raw_ostream &ROS, const Metadata &MD, ModuleSlotTracker &MST, const Module *M, bool OnlyAsOperand)
Definition: AsmWriter.cpp:4380
const DataLayout & getDataLayout() const
Get the data layout for the module&#39;s target platform.
Definition: Module.cpp:369
const Value * V
Definition: UseListOrder.h:27
TypeID getTypeID() const
Return the type id for the type.
Definition: Type.h:138
bool isDSOLocal() const
Definition: GlobalValue.h:283
X86_StdCall - stdcall is the calling conventions mostly used by the Win32 API.
Definition: CallingConv.h:91
When retpoline mitigation is enabled, use a branch funnel that is defined in the merged module...
Calling convention used for Mesa vertex shaders, or AMDPAL last shader stage before rasterization (ve...
Definition: CallingConv.h:193
Class to represent struct types.
Definition: DerivedTypes.h:238
A Use represents the edge between a Value definition and its users.
Definition: Use.h:55
bool isConstant() const
If the value is a global constant, its value is immutable throughout the runtime execution of the pro...
Value * getDerivedPtr() const
Definition: Statepoint.h:387
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:41
std::vector< ConstVCall > TypeCheckedLoadConstVCalls
const Module * getModule() const
UnnamedAddr getUnnamedAddr() const
Definition: GlobalValue.h:216
The access may reference the value stored in memory.
static StringRef getName(Value *V)
This file contains the simple types necessary to represent the attributes associated with functions a...
Just do a regular virtual call.
The linker may choose any COMDAT.
Definition: Comdat.h:34
unsigned getNumOperands() const
Definition: Metadata.cpp:1076
int getTypeIdSlot(StringRef Id)
Definition: AsmWriter.cpp:1151
The C convention as specified in the x86-64 supplement to the System V ABI, used on most non-Windows ...
Definition: CallingConv.h:148
bool hasAttributes(unsigned Index) const
Return true if attribute exists at the given index.
APInt getHiBits(unsigned numBits) const
Compute an APInt containing numBits highbits from this APInt.
Definition: APInt.cpp:565
uint64_t getNumElements() const
For scalable vectors, this will return the minimum number of elements in the vector.
Definition: DerivedTypes.h:398
static void printMetadataIdentifier(StringRef Name, formatted_raw_ostream &Out)
Definition: AsmWriter.cpp:3137
static const uint16_t * lookup(unsigned opcode, unsigned domain, ArrayRef< uint16_t[3]> Table)
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:140
This file implements a class to represent arbitrary precision integral constant values and operations...
Constant * getPrefixData() const
Get the prefix data associated with this function.
Definition: Function.cpp:1475
AtomicOrdering
Atomic ordering for LLVM&#39;s memory model.
enum llvm::WholeProgramDevirtResolution::Kind TheKind
void dump() const
Definition: AsmWriter.cpp:4444
static StringRef getOperationName(BinOp Op)
bool hasExternalLinkage() const
Definition: GlobalValue.h:431
Class to represent function types.
Definition: DerivedTypes.h:108
bool isInfinity() const
Definition: APFloat.h:1159
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:245
reverse_iterator rend()
Definition: Module.h:604
opStatus convert(const fltSemantics &ToSemantics, roundingMode RM, bool *losesInfo)
Definition: APFloat.cpp:4483
void toString(SmallVectorImpl< char > &Str, unsigned FormatPrecision=0, unsigned FormatMaxPadding=3, bool TruncateZero=true) const
Definition: APFloat.h:1182
void print(raw_ostream &OS, AssemblyAnnotationWriter *AAW=nullptr, bool ShouldPreserveUseListOrder=false, bool IsForDebug=false) const
Print the function to an output stream with an optional AssemblyAnnotationWriter. ...
Definition: AsmWriter.cpp:4185
Class to represent array types.
Definition: DerivedTypes.h:408
AttributeSet getParamAttributes(unsigned ArgNo) const
The attributes for the argument or parameter at the given index are returned.
VisibilityTypes
An enumeration for the kinds of visibility of global values.
Definition: GlobalValue.h:62
int getModulePathSlot(StringRef Path)
Definition: AsmWriter.cpp:1133
bool isVarArg() const
Definition: DerivedTypes.h:128
LLVM_NODISCARD size_t size() const
size - Get the string size.
Definition: StringRef.h:144
const std::string & getGC() const
Definition: Function.cpp:478
AttributeList getAttributes() const
Return the attribute list for this Function.
Definition: Function.h:223
An instruction for storing to memory.
Definition: Instructions.h:325
bool hasPersonalityFn() const
Check whether this function has a personality function.
Definition: Function.h:732
Calling convention used for Mesa/AMDPAL geometry shaders.
Definition: CallingConv.h:196
COFF::MachineTypes Machine
Definition: COFFYAML.cpp:365
Calling convention used for AMDPAL vertex shader if tessellation is in use.
Definition: CallingConv.h:220
LinkageTypes getLinkage() const
Definition: GlobalValue.h:460
static const fltSemantics & IEEEdouble() LLVM_READNONE
Definition: APFloat.cpp:158
char hexdigit(unsigned X, bool LowerCase=false)
hexdigit - Return the hexadecimal character for the given number X (which should be less than 16)...
Definition: StringExtras.h:36
static void PrintVisibility(GlobalValue::VisibilityTypes Vis, formatted_raw_ostream &Out)
Definition: AsmWriter.cpp:3183
unsigned getNumOperandBundles() const
Return the number of operand bundles associated with this User.
Definition: InstrTypes.h:1728
Debug location.
constexpr char Attrs[]
Key for Kernel::Metadata::mAttrs.
Structure to hold a use-list order.
Definition: UseListOrder.h:26
Class to hold module path string table and global value map, and encapsulate methods for operating on...
ArrayRef< std::unique_ptr< GlobalValueSummary > > getSummaryList() const
Value * getOperand(unsigned i) const
Definition: User.h:169
int getLocalSlot(const Value *V)
Return the slot number of the specified value in it&#39;s type plane.
Definition: AsmWriter.cpp:1114
Class to represent pointers.
Definition: DerivedTypes.h:575
const std::string & getSourceFileName() const
Get the module&#39;s original source file name.
Definition: Module.h:221
Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
Definition: Constants.cpp:359
AttributeSet getAttributes(unsigned Index) const
The attributes for the specified index are returned.
11: Arbitrary bit width integers
Definition: Type.h:71
bool alias_empty() const
Definition: Module.h:625
bool isVoidTy() const
Return true if this is &#39;void&#39;.
Definition: Type.h:141
ExternalWeak linkage description.
Definition: GlobalValue.h:57
const BasicBlock & getEntryBlock() const
Definition: Function.h:664
This class provides computation of slot numbers for LLVM Assembly writing.
Definition: AsmWriter.cpp:669
C - The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
0: type with no size
Definition: Type.h:57
#define P(N)
Same, but only replaced by something equivalent.
Definition: GlobalValue.h:51
Type * getReturnType() const
Returns the type of the ret val.
Definition: Function.h:168
No other Module may specify this COMDAT.
Definition: Comdat.h:37
StringRef getName() const
Definition: Comdat.cpp:26
const char * toIRString(AtomicOrdering ao)
String used by LLVM IR to represent atomic ordering.
The landingpad instruction holds all of the information necessary to generate correct exception handl...
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
* if(!EatIfPresent(lltok::kw_thread_local)) return false
ParseOptionalThreadLocal := /*empty.
void print(raw_ostream &OS, bool IsForDebug=false) const
Definition: AsmWriter.cpp:4228
DLLStorageClassTypes getDLLStorageClass() const
Definition: GlobalValue.h:262
VisibilityTypes getVisibility() const
Definition: GlobalValue.h:236
An array constant whose element type is a simple 1/2/4/8-byte integer or float/double, and whose elements are just simple data values (i.e.
Definition: Constants.h:689
static void predictValueUseListOrderImpl(const Value *V, const Function *F, unsigned ID, const OrderMap &OM, UseListOrderStack &Stack)
Definition: AsmWriter.cpp:181
bool hasName() const
Definition: Value.h:251
LLVM Basic Block Representation.
Definition: BasicBlock.h:57
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:64
Conditional or Unconditional Branch instruction.
ARM_AAPCS - ARM Architecture Procedure Calling Standard calling convention (aka EABI).
Definition: CallingConv.h:104
const char * getOpcodeName() const
Definition: Instruction.h:127
bool isNaN() const
Definition: APFloat.h:1160
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This is an important base class in LLVM.
Definition: Constant.h:41
SummaryKind getSummaryKind() const
Which kind of summary subclass this is.
ArrayRef< Use > Inputs
Definition: InstrTypes.h:1001
void dump() const
Dump the module to stderr (for debugging).
Definition: AsmWriter.cpp:4437
static StringRef getPredicateName(Predicate P)
This file contains the declarations for the subclasses of Constant, which represent the different fla...
unsigned instCount() const
Get the instruction count recorded for this function.
10: Tokens
Definition: Type.h:67
void getSyncScopeNames(SmallVectorImpl< StringRef > &SSNs) const
getSyncScopeNames - Populates client supplied SmallVector with synchronization scope names registered...
void print(raw_ostream &ROS, bool IsForDebug=false) const
Definition: AsmWriter.cpp:4205
std::vector< UseListOrder > UseListOrderStack
Definition: UseListOrder.h:39
unsigned getNumParams() const
Return the number of fixed parameters this function type requires.
Definition: DerivedTypes.h:144
ConstantFP - Floating Point Values [float, double].
Definition: Constants.h:263
double convertToDouble() const
Definition: APFloat.h:1112
Interval::pred_iterator pred_begin(Interval *I)
pred_begin/pred_end - define methods so that Intervals may be used just like BasicBlocks can with the...
Definition: Interval.h:112
BasicBlock * getDefaultDest() const
A udiv or sdiv instruction, which can be marked as "exact", indicating that no bits are destroyed...
Definition: Operator.h:119
static void WriteAsOperandInternal(raw_ostream &Out, const Value *V, TypePrinting *TypePrinter, SlotTracker *Machine, const Module *Context)
Definition: AsmWriter.cpp:2224
param_iterator param_begin() const
Definition: DerivedTypes.h:133
This file declares a class to represent arbitrary precision floating point values and provide a varie...
Single element (last example in "Short Inline Bit Vectors")
StringRef name() const
Fast - This calling convention attempts to make calls as fast as possible (e.g.
Definition: CallingConv.h:42
unsigned getAddressSpace() const
Definition: Globals.cpp:111
Utility class for integer operators which may exhibit overflow - Add, Sub, Mul, and Shl...
Definition: Operator.h:66
void print(raw_ostream &O, bool IsForDebug=false) const
Implement operator<< on Value.
Definition: AsmWriter.cpp:4279
StringRef getName() const
Return the name for this struct type if it has an identity.
Definition: Type.cpp:504
constexpr double e
Definition: MathExtras.h:57
DLLStorageClassTypes
Storage classes of global values for PE targets.
Definition: GlobalValue.h:69
6: 128-bit floating point type (two 64-bits, PowerPC)
Definition: Type.h:63
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
static void printAsOperandImpl(const Value &V, raw_ostream &O, bool PrintType, ModuleSlotTracker &MST)
Definition: AsmWriter.cpp:4344
Interval::pred_iterator pred_end(Interval *I)
Definition: Interval.h:115
op_range operands()
Definition: User.h:237
unsigned getAddressSpace() const
Return the address space of the Pointer type.
Definition: DerivedTypes.h:603
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
static const fltSemantics & x87DoubleExtended() LLVM_READNONE
Definition: APFloat.cpp:164
bool isMaterializable() const
Definition: Function.h:178
static const Module * getModuleFromVal(const Value *V)
Definition: AsmWriter.cpp:318
The data referenced by the COMDAT must be the same.
Definition: Comdat.h:35
Metadata wrapper in the Value hierarchy.
Definition: Metadata.h:173
All type identifier related information.
const Function * getFunction() const
Return the function this instruction belongs to.
Definition: Instruction.cpp:59
std::string getAsString(unsigned Index, bool InAttrGrp=false) const
Return the attributes at the index as a string.
Function and variable summary information to aid decisions and implementation of importing.
void incorporateFunction(const Function &F)
Incorporate the given function.
Definition: AsmWriter.cpp:844
static bool isAtomic(Instruction *I)
size_t size() const
Definition: SmallVector.h:52
static void PrintThreadLocalModel(GlobalVariable::ThreadLocalMode TLM, formatted_raw_ostream &Out)
Definition: AsmWriter.cpp:3211
const std::string & getModuleIdentifier() const
Get the module identifier which is, essentially, the name of the module.
Definition: Module.h:210
GVFlags flags() const
Get the flags for this GlobalValue (see struct GVFlags).
void printAsOperand(raw_ostream &O, bool PrintType=true, const Module *M=nullptr) const
Print the name of this Value out to the specified raw_ostream.
Definition: AsmWriter.cpp:4356
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Used for AVR interrupt routines.
Definition: CallingConv.h:181
CallBr instruction, tracking function calls that may not return control but instead transfer it to a ...
unsigned first
An "identifier" for a virtual function.
void dump() const
Definition: AsmWriter.cpp:4433
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1095
Struct that holds a reference to a particular GUID in a global value summary.
static void PrintLLVMName(raw_ostream &OS, StringRef Name, PrefixType Prefix)
Turn the specified name into an &#39;LLVM name&#39;, which is either prefixed with % (if the string only cont...
Definition: AsmWriter.cpp:434
constexpr bool empty(const T &RangeOrContainer)
Test whether RangeOrContainer is empty. Similar to C++17 std::empty.
Definition: STLExtras.h:197
reverse_iterator rbegin()
Definition: Module.h:602
void print(raw_ostream &OS, AssemblyAnnotationWriter *AAW, bool ShouldPreserveUseListOrder=false, bool IsForDebug=false) const
Print the module to an output stream with an optional AssemblyAnnotationWriter.
Definition: AsmWriter.cpp:4196
14: Arrays
Definition: Type.h:74
StringRef getPartition() const
Definition: Globals.cpp:188
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
void print(raw_ostream &OS, const Module *M=nullptr, bool IsForDebug=false) const
Print.
Definition: AsmWriter.cpp:4408
std::vector< VFuncId > TypeCheckedLoadVCalls
static const fltSemantics & IEEEsingle() LLVM_READNONE
Definition: APFloat.cpp:155
PTX_Device - Call to a PTX device function.
Definition: CallingConv.h:123
unsigned getNumOperands() const
Definition: User.h:191
std::string getAsString(bool InAttrGrp=false) const
Definition: Attributes.cpp:698
This is the shared class of boolean and integer constants.
Definition: Constants.h:83
auto size(R &&Range, typename std::enable_if< std::is_same< typename std::iterator_traits< decltype(Range.begin())>::iterator_category, std::random_access_iterator_tag >::value, void >::type *=nullptr) -> decltype(std::distance(Range.begin(), Range.end()))
Get the size of a range.
Definition: STLExtras.h:1146
bool isScalable() const
Returns whether or not this is a scalable vector (meaning the total element count is a multiple of th...
Definition: DerivedTypes.h:550
static const fltSemantics & IEEEhalf() LLVM_READNONE
Definition: APFloat.cpp:152
const char * getHotnessName(CalleeInfo::HotnessType HT)
16: SIMD &#39;packed&#39; format, or other vector type
Definition: Type.h:76
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
Definition: Function.h:212
bool hasSection() const
Check if this global has a custom object file section.
Definition: GlobalObject.h:100
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:837
Keep one copy of function when linking (inline)
Definition: GlobalValue.h:50
Utility class for floating point operations which can have information about relaxed accuracy require...
Definition: Operator.h:245
Module.h This file contains the declarations for the Module class.
static void WriteConstantInternal(raw_ostream &Out, const Constant *CV, TypePrinting &TypePrinter, SlotTracker *Machine, const Module *Context)
Definition: AsmWriter.cpp:1288
LLVM_NODISCARD std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Definition: StringRef.h:710
Type::subtype_iterator param_iterator
Definition: DerivedTypes.h:131
AddressSpace
Definition: NVPTXBaseInfo.h:21
Single implementation devirtualization.
const DataFlowGraph & G
Definition: RDFGraph.cpp:202
Type * getReturnType() const
Definition: DerivedTypes.h:129
void printLLVMNameWithoutPrefix(raw_ostream &OS, StringRef Name)
Print out a name of an LLVM value without any prefixes.
Definition: AsmWriter.cpp:398
unsigned getProgramAddressSpace() const
Definition: DataLayout.h:287
Alias summary information.
Calling convention used for Mesa/AMDPAL compute shaders.
Definition: CallingConv.h:202
The linker will choose the largest COMDAT.
Definition: Comdat.h:36
static bool isReferencingMDNode(const Instruction &I)
Definition: AsmWriter.cpp:4268
ArrayRef< VirtFuncOffset > vTableFuncs() const
DWARF expression.
void getAllMetadata(SmallVectorImpl< std::pair< unsigned, MDNode *>> &MDs) const
Get all metadata attached to this Instruction.
Definition: Instruction.h:260
enum llvm::TypeTestResolution::Kind TheKind
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
8: Metadata
Definition: Type.h:65
The access may modify the value stored in memory.
unsigned getVectorNumElements() const
Definition: DerivedTypes.h:566
FunctionType * getFunctionType() const
Returns the FunctionType for me.
Definition: Function.h:163
bool isLiteral() const
Return true if this type is uniqued by structural equivalence, false if it is a struct definition...
Definition: DerivedTypes.h:302
Class to represent vector types.
Definition: DerivedTypes.h:432
This file contains constants used for implementing Dwarf debug support.
ConstantArray - Constant Array Declarations.
Definition: Constants.h:413
Class for arbitrary precision integers.
Definition: APInt.h:69
SPIR_KERNEL - Calling convention for SPIR kernel functions.
Definition: CallingConv.h:141
static bool isWeak(const MCSymbolELF &Sym)
LinkageTypes
An enumeration for the kinds of linkage for global values.
Definition: GlobalValue.h:47
StringRef getName() const
Definition: Metadata.cpp:1097
MSP430_INTR - Calling convention used for MSP430 interrupt routines.
Definition: CallingConv.h:110
Synchronized with respect to all concurrently executing threads.
Definition: LLVMContext.h:54
A lightweight accessor for an operand bundle meant to be passed around by value.
Definition: InstrTypes.h:1000
iterator_range< user_iterator > users()
Definition: Value.h:419
GUID getGUID() const
Return a 64-bit global unique ID constructed from global value name (i.e.
Definition: GlobalValue.h:510
SummaryKind
Sububclass discriminator (for dyn_cast<> et al.)
Calling convention used for AMDPAL shader stage before geometry shader if geometry is in use...
Definition: CallingConv.h:225
static const char * getWholeProgDevirtResByArgKindName(WholeProgramDevirtResolution::ByArg::Kind K)
Definition: AsmWriter.cpp:2747
void initializeIndexIfNeeded()
Definition: AsmWriter.cpp:919
Register calling convention used for parameters transfer optimization.
Definition: CallingConv.h:208
FFlags fflags() const
Get function summary flags.
SPIR_FUNC - Calling convention for SPIR non-kernel device functions.
Definition: CallingConv.h:133
const Comdat * getComdat() const
Definition: GlobalObject.h:119
static const fltSemantics & PPCDoubleDouble() LLVM_READNONE
Definition: APFloat.cpp:170
use_iterator use_begin()
Definition: Value.h:358
#define ST_DEBUG(X)
Definition: AsmWriter.cpp:892
Test a byte array (first example)
This file provides various utilities for inspecting and working with the control flow graph in LLVM I...
Function to be accessible from DLL.
Definition: GlobalValue.h:72
bool hasPartition() const
Definition: GlobalValue.h:287
bool hasGC() const
hasGC/getGC/setGC/clearGC - The name of the garbage collection algorithm to use during code generatio...
Definition: Function.h:351
X86_ThisCall - Similar to X86_StdCall.
Definition: CallingConv.h:115
std::map< std::vector< uint64_t >, ByArg > ResByArg
Resolutions for calls with all constant integer arguments (excluding the first argument, "this"), where the key is the argument vector.
static const char * getTTResKindName(TypeTestResolution::Kind K)
Definition: AsmWriter.cpp:2762
The C convention as implemented on Windows/x86-64 and AArch64.
Definition: CallingConv.h:158
bool ifunc_empty() const
Definition: Module.h:643
StringRef modulePath() const
Get the path to the module containing this function.
void print(raw_ostream &O, bool IsForDebug=false, bool NoDetails=false) const
Print the current type.
Definition: AsmWriter.cpp:4253
Calling convention used for AVR signal routines.
Definition: CallingConv.h:184
static void writeDIExpression(raw_ostream &Out, const DIExpression *N, TypePrinting *TypePrinter, SlotTracker *Machine, const Module *Context)
Definition: AsmWriter.cpp:2135
All-ones bit vector ("Eliminating Bit Vector Checks for All-Ones Bit Vectors") ...
unsigned DSOLocal
Indicates that the linker resolved the symbol to a definition from within the same linkage unit...
static void orderValue(const Value *V, OrderMap &OM)
Definition: AsmWriter.cpp:119
StringRef getName() const
Return a constant reference to the value&#39;s name.
Definition: Value.cpp:214
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:106
unsigned CanAutoHide
In the per-module summary, indicates that the global value is linkonce_odr and global unnamed addr (s...
FormattedNumber format_hex_no_prefix(uint64_t N, unsigned Width, bool Upper=false)
format_hex_no_prefix - Output N as a fixed width hexadecimal.
Definition: Format.h:199
const NodeList & List
Definition: RDFGraph.cpp:201
HHVM calling convention for invoking C/C++ helpers.
Definition: CallingConv.h:171
#define I(x, y, z)
Definition: MD5.cpp:58
Tail - This calling convention attemps to make calls as fast as possible while guaranteeing that tail...
Definition: CallingConv.h:81
#define N
void dump() const
User-friendly dump.
Definition: AsmWriter.cpp:4451
StringRef getTagName() const
Return the tag of this operand bundle as a string.
Definition: InstrTypes.h:1019
static void maybePrintComdat(formatted_raw_ostream &Out, const GlobalObject &GO)
Definition: AsmWriter.cpp:3243
static const char * getSummaryKindName(GlobalValueSummary::SummaryKind SK)
Definition: AsmWriter.cpp:2871
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:332
ArrayRef< EdgeTy > calls() const
Return the list of <CalleeValueInfo, CalleeInfo> pairs.
Type * getValueType() const
Definition: GlobalValue.h:279
int getGUIDSlot(GlobalValue::GUID GUID)
Definition: AsmWriter.cpp:1142
Keep one copy of named function when linking (weak)
Definition: GlobalValue.h:52
static void WriteMDNodeBodyInternal(raw_ostream &Out, const MDNode *Node, TypePrinting *TypePrinter, SlotTracker *Machine, const Module *Context)
Definition: AsmWriter.cpp:2202
Rename collisions when linking (static functions).
Definition: GlobalValue.h:55
const Constant * getIndirectSymbol() const
Flags specific to function summaries.
iterator_range< ifunc_iterator > ifuncs()
Definition: Module.h:645
Inlined bit vector ("Short Inline Bit Vectors")
unsigned SizeM1BitWidth
Range of size-1 expressed as a bit width.
Function summary information to aid decisions and implementation of importing.
Function to be imported from DLL.
Definition: GlobalValue.h:71
ValueT lookup(const_arg_type_t< KeyT > Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
Definition: DenseMap.h:185
const std::string & getModuleInlineAsm() const
Get any module-scope inline assembly blocks.
Definition: Module.h:248
This file defines passes to print out IR in various granularities.
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition: Globals.cpp:231
3: 64-bit floating point type
Definition: Type.h:60
Multiway switch.
void printAsOperand(raw_ostream &OS, const Module *M=nullptr) const
Print as operand.
Definition: AsmWriter.cpp:4398
ModuleSummaryIndex.h This file contains the declarations the classes that hold the module index and s...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
bool hasAttributes() const
Return true if attributes exists in this set.
Definition: Attributes.h:274
Calling convention used for Mesa/AMDPAL hull shaders (= tessellation control shaders).
Definition: CallingConv.h:212
Represents calls to the gc.relocate intrinsic.
Definition: Statepoint.h:360
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:575
LLVM Value Representation.
Definition: Value.h:73
Constant * getPersonalityFn() const
Get the personality function associated with this function.
Definition: Function.cpp:1465
ARM_AAPCS_VFP - Same as ARM_AAPCS, but uses hard floating point ABI.
Definition: CallingConv.h:107
A vector that has set insertion semantics.
Definition: SetVector.h:40
bool hasInitializer() const
Definitions have initializers, declarations don&#39;t.
static const Function * getParent(const Value *V)
AttributeSet getFnAttributes() const
The function attributes are returned.
Group flags (Linkage, NotEligibleToImport, etc.) as a bitfield.
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
Invoke instruction.
bool hasDefaultVisibility() const
Definition: GlobalValue.h:237
Type * getElementType() const