1894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===-- AsmWriter.cpp - Printing LLVM as an assembly file -----------------===//
2894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
3894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//                     The LLVM Compiler Infrastructure
4894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
5894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This file is distributed under the University of Illinois Open Source
6894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// License. See LICENSE.TXT for details.
7894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
8894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
9894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
10894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This library implements the functionality defined in llvm/Assembly/Writer.h
11894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
12894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Note that these routines must be extremely tolerant of various errors in the
13894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// LLVM code, because it can be used for debugging transformations.
14894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
15894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
16894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
17894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Assembly/Writer.h"
18894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Assembly/PrintModulePass.h"
1919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#include "llvm/Assembly/AssemblyAnnotationWriter.h"
20894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/LLVMContext.h"
21894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/CallingConv.h"
22894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Constants.h"
23894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/DerivedTypes.h"
24894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/InlineAsm.h"
25894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/IntrinsicInst.h"
26894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Operator.h"
27894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Module.h"
28894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/ValueSymbolTable.h"
2919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#include "llvm/ADT/DenseMap.h"
30894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/ADT/SmallString.h"
31894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/ADT/StringExtras.h"
32894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/ADT/STLExtras.h"
33894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Support/CFG.h"
34894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Support/Debug.h"
35894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Support/Dwarf.h"
36894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Support/ErrorHandling.h"
37894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Support/MathExtras.h"
38894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Support/FormattedStream.h"
39894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include <algorithm>
40894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include <cctype>
41894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanusing namespace llvm;
42894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
43894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Make virtual table appear in this compilation unit.
44894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanAssemblyAnnotationWriter::~AssemblyAnnotationWriter() {}
45894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
46894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
47894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Helper Functions
48894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
49894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
50894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic const Module *getModuleFromVal(const Value *V) {
51894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (const Argument *MA = dyn_cast<Argument>(V))
52894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return MA->getParent() ? MA->getParent()->getParent() : 0;
53894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
54894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
55894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return BB->getParent() ? BB->getParent()->getParent() : 0;
56894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
57894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (const Instruction *I = dyn_cast<Instruction>(V)) {
58894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    const Function *M = I->getParent() ? I->getParent()->getParent() : 0;
59894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return M ? M->getParent() : 0;
60894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
6119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
62894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
63894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return GV->getParent();
64894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return 0;
65894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
66894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
67894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// PrintEscapedString - Print each character of the specified string, escaping
68894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// it if it is not printable or if it is an escape char.
69894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic void PrintEscapedString(StringRef Name, raw_ostream &Out) {
70894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 0, e = Name.size(); i != e; ++i) {
71894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    unsigned char C = Name[i];
72894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (isprint(C) && C != '\\' && C != '"')
73894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << C;
74894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else
75894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << '\\' << hexdigit(C >> 4) << hexdigit(C & 0x0F);
76894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
77894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
78894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
79894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanenum PrefixType {
80894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  GlobalPrefix,
81894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  LabelPrefix,
82894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  LocalPrefix,
83894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  NoPrefix
84894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman};
85894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
86894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// PrintLLVMName - Turn the specified name into an 'LLVM name', which is either
87894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// prefixed with % (if the string only contains simple characters) or is
88894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// surrounded with ""'s (if it has special chars in it).  Print it out.
89894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic void PrintLLVMName(raw_ostream &OS, StringRef Name, PrefixType Prefix) {
9019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  assert(!Name.empty() && "Cannot get empty name!");
91894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  switch (Prefix) {
92894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  default: llvm_unreachable("Bad prefix!");
93894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case NoPrefix: break;
94894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case GlobalPrefix: OS << '@'; break;
95894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case LabelPrefix:  break;
96894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case LocalPrefix:  OS << '%'; break;
97894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
98894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
99894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Scan the name to see if it needs quotes first.
100894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool NeedsQuotes = isdigit(Name[0]);
101894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!NeedsQuotes) {
102894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (unsigned i = 0, e = Name.size(); i != e; ++i) {
103894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      char C = Name[i];
104894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (!isalnum(C) && C != '-' && C != '.' && C != '_') {
105894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        NeedsQuotes = true;
106894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        break;
107894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
108894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
109894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
110894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
111894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If we didn't need any quotes, just write out the name in one blast.
112894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!NeedsQuotes) {
113894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    OS << Name;
114894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return;
115894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
116894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
117894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Okay, we need quotes.  Output the quotes and escape any scary characters as
118894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // needed.
119894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  OS << '"';
120894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  PrintEscapedString(Name, OS);
121894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  OS << '"';
122894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
123894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
124894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// PrintLLVMName - Turn the specified name into an 'LLVM name', which is either
125894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// prefixed with % (if the string only contains simple characters) or is
126894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// surrounded with ""'s (if it has special chars in it).  Print it out.
127894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic void PrintLLVMName(raw_ostream &OS, const Value *V) {
128894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  PrintLLVMName(OS, V->getName(),
129894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                isa<GlobalValue>(V) ? GlobalPrefix : LocalPrefix);
130894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
131894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
132894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
133894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// TypePrinting Class: Type printing machinery
134894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
135894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
13619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// TypePrinting - Type printing machinery.
13719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumannamespace {
13819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanclass TypePrinting {
13919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  TypePrinting(const TypePrinting &);   // DO NOT IMPLEMENT
14019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  void operator=(const TypePrinting&);  // DO NOT IMPLEMENT
14119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanpublic:
142894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
14319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  /// NamedTypes - The named types that are used by the current module.
14419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  std::vector<StructType*> NamedTypes;
145894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
14619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  /// NumberedTypes - The numbered types, along with their value.
14719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  DenseMap<StructType*, unsigned> NumberedTypes;
148894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
149894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
15019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  TypePrinting() {}
15119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  ~TypePrinting() {}
152894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
15319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  void incorporateTypes(const Module &M);
154894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
15519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  void print(Type *Ty, raw_ostream &OS);
156894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
15719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  void printStructBody(StructType *Ty, raw_ostream &OS);
15819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman};
15919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman} // end anonymous namespace.
160894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
161894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
16219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanvoid TypePrinting::incorporateTypes(const Module &M) {
16319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  M.findUsedStructTypes(NamedTypes);
16419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
16519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // The list of struct types we got back includes all the struct types, split
16619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // the unnamed ones out to a numbering and remove the anonymous structs.
16719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned NextNumber = 0;
16819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
16919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  std::vector<StructType*>::iterator NextToUse = NamedTypes.begin(), I, E;
17019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  for (I = NamedTypes.begin(), E = NamedTypes.end(); I != E; ++I) {
17119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    StructType *STy = *I;
17219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
17319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Ignore anonymous types.
17419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (STy->isLiteral())
17519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      continue;
17619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
17719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (STy->getName().empty())
17819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      NumberedTypes[STy] = NextNumber++;
17919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    else
18019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      *NextToUse++ = STy;
181894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
182894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
18319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  NamedTypes.erase(NextToUse, NamedTypes.end());
18419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
18519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
186894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
18719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// CalcTypeName - Write the specified type to the specified raw_ostream, making
18819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// use of type names or up references to shorten the type name where possible.
18919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanvoid TypePrinting::print(Type *Ty, raw_ostream &OS) {
190894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  switch (Ty->getTypeID()) {
191894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Type::VoidTyID:      OS << "void"; break;
192894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Type::FloatTyID:     OS << "float"; break;
193894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Type::DoubleTyID:    OS << "double"; break;
194894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Type::X86_FP80TyID:  OS << "x86_fp80"; break;
195894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Type::FP128TyID:     OS << "fp128"; break;
196894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Type::PPC_FP128TyID: OS << "ppc_fp128"; break;
197894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Type::LabelTyID:     OS << "label"; break;
198894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Type::MetadataTyID:  OS << "metadata"; break;
19919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case Type::X86_MMXTyID:   OS << "x86_mmx"; break;
200894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Type::IntegerTyID:
201894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    OS << 'i' << cast<IntegerType>(Ty)->getBitWidth();
20219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return;
203894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
204894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Type::FunctionTyID: {
20519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    FunctionType *FTy = cast<FunctionType>(Ty);
20619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    print(FTy->getReturnType(), OS);
207894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    OS << " (";
208894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (FunctionType::param_iterator I = FTy->param_begin(),
209894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         E = FTy->param_end(); I != E; ++I) {
210894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (I != FTy->param_begin())
211894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        OS << ", ";
21219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      print(*I, OS);
213894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
214894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (FTy->isVarArg()) {
215894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (FTy->getNumParams()) OS << ", ";
216894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      OS << "...";
217894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
218894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    OS << ')';
21919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return;
220894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
221894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Type::StructTyID: {
22219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    StructType *STy = cast<StructType>(Ty);
22319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
22419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (STy->isLiteral())
22519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return printStructBody(STy, OS);
22619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
22719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (!STy->getName().empty())
22819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return PrintLLVMName(OS, STy->getName(), LocalPrefix);
22919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
23019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    DenseMap<StructType*, unsigned>::iterator I = NumberedTypes.find(STy);
23119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (I != NumberedTypes.end())
23219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      OS << '%' << I->second;
23319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    else  // Not enumerated, print the hex address.
23419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      OS << "%\"type 0x" << STy << '\"';
23519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return;
236894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
237894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Type::PointerTyID: {
23819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    PointerType *PTy = cast<PointerType>(Ty);
23919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    print(PTy->getElementType(), OS);
240894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (unsigned AddressSpace = PTy->getAddressSpace())
241894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      OS << " addrspace(" << AddressSpace << ')';
242894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    OS << '*';
24319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return;
244894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
245894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Type::ArrayTyID: {
24619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    ArrayType *ATy = cast<ArrayType>(Ty);
247894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    OS << '[' << ATy->getNumElements() << " x ";
24819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    print(ATy->getElementType(), OS);
249894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    OS << ']';
25019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return;
251894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
252894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Type::VectorTyID: {
25319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    VectorType *PTy = cast<VectorType>(Ty);
254894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    OS << "<" << PTy->getNumElements() << " x ";
25519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    print(PTy->getElementType(), OS);
256894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    OS << '>';
25719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return;
258894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
259894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  default:
260894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    OS << "<unrecognized-type>";
26119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return;
262894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
263894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
264894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
26519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanvoid TypePrinting::printStructBody(StructType *STy, raw_ostream &OS) {
26619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (STy->isOpaque()) {
26719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    OS << "opaque";
26819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return;
269894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
270894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
27119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (STy->isPacked())
27219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    OS << '<';
273894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
27419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (STy->getNumElements() == 0) {
27519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    OS << "{}";
27619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  } else {
27719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    StructType::element_iterator I = STy->element_begin();
27819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    OS << "{ ";
27919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    print(*I++, OS);
28019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    for (StructType::element_iterator E = STy->element_end(); I != E; ++I) {
28119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      OS << ", ";
28219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      print(*I, OS);
283894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
284894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
28519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    OS << " }";
286894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
28719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (STy->isPacked())
28819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    OS << '>';
289894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
290894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
291894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
292894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
293894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
294894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// SlotTracker Class: Enumerate slot numbers for unnamed values
295894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
296894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
297894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumannamespace {
298894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
299894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// This class provides computation of slot numbers for LLVM Assembly writing.
300894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///
301894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass SlotTracker {
302894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanpublic:
303894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// ValueMap - A mapping of Values to slot numbers.
304894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  typedef DenseMap<const Value*, unsigned> ValueMap;
305894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
306894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanprivate:
307894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// TheModule - The module for which we are holding slot numbers.
308894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const Module* TheModule;
309894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
310894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// TheFunction - The function for which we are holding slot numbers.
311894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const Function* TheFunction;
312894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool FunctionProcessed;
313894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
31419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  /// mMap - The slot map for the module level data.
315894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ValueMap mMap;
316894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned mNext;
317894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
31819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  /// fMap - The slot map for the function level data.
319894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ValueMap fMap;
320894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned fNext;
321894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
322894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// mdnMap - Map for MDNodes.
323894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DenseMap<const MDNode*, unsigned> mdnMap;
324894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned mdnNext;
325894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanpublic:
326894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// Construct from a module
327894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  explicit SlotTracker(const Module *M);
328894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// Construct from a function, starting out in incorp state.
329894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  explicit SlotTracker(const Function *F);
330894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
331894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// Return the slot number of the specified value in it's type
332894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// plane.  If something is not in the SlotTracker, return -1.
333894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  int getLocalSlot(const Value *V);
334894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  int getGlobalSlot(const GlobalValue *V);
335894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  int getMetadataSlot(const MDNode *N);
336894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
337894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// If you'd like to deal with a function instead of just a module, use
338894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// this method to get its data into the SlotTracker.
339894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void incorporateFunction(const Function *F) {
340894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    TheFunction = F;
341894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    FunctionProcessed = false;
342894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
343894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
344894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// After calling incorporateFunction, use this method to remove the
345894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// most recently incorporated function from the SlotTracker. This
346894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// will reset the state of the machine back to just the module contents.
347894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void purgeFunction();
348894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
349894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// MDNode map iterators.
350894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  typedef DenseMap<const MDNode*, unsigned>::iterator mdn_iterator;
351894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  mdn_iterator mdn_begin() { return mdnMap.begin(); }
352894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  mdn_iterator mdn_end() { return mdnMap.end(); }
353894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned mdn_size() const { return mdnMap.size(); }
354894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool mdn_empty() const { return mdnMap.empty(); }
355894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
356894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// This function does the actual initialization.
357894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  inline void initialize();
358894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
359894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Implementation Details
360894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanprivate:
361894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
362894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void CreateModuleSlot(const GlobalValue *V);
363894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
364894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// CreateMetadataSlot - Insert the specified MDNode* into the slot table.
365894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void CreateMetadataSlot(const MDNode *N);
366894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
367894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// CreateFunctionSlot - Insert the specified Value* into the slot table.
368894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void CreateFunctionSlot(const Value *V);
369894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
370894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// Add all of the module level global variables (and their initializers)
371894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// and function declarations, but not the contents of those functions.
372894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void processModule();
373894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
374894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// Add all of the functions arguments, basic blocks, and instructions.
375894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void processFunction();
376894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
377894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SlotTracker(const SlotTracker &);  // DO NOT IMPLEMENT
378894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void operator=(const SlotTracker &);  // DO NOT IMPLEMENT
379894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman};
380894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
381894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}  // end anonymous namespace
382894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
383894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
384894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic SlotTracker *createSlotTracker(const Value *V) {
385894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (const Argument *FA = dyn_cast<Argument>(V))
386894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return new SlotTracker(FA->getParent());
387894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
388894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (const Instruction *I = dyn_cast<Instruction>(V))
38919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (I->getParent())
39019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return new SlotTracker(I->getParent()->getParent());
391894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
392894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
393894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return new SlotTracker(BB->getParent());
394894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
395894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
396894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return new SlotTracker(GV->getParent());
397894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
398894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V))
399894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return new SlotTracker(GA->getParent());
400894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
401894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (const Function *Func = dyn_cast<Function>(V))
402894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return new SlotTracker(Func);
403894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
404894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (const MDNode *MD = dyn_cast<MDNode>(V)) {
405894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!MD->isFunctionLocal())
406894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return new SlotTracker(MD->getFunction());
407894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
408894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return new SlotTracker((Function *)0);
409894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
410894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
411894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return 0;
412894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
413894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
414894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#if 0
415894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#define ST_DEBUG(X) dbgs() << X
416894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#else
417894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#define ST_DEBUG(X)
418894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif
419894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
420894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Module level constructor. Causes the contents of the Module (sans functions)
421894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// to be added to the slot table.
422894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSlotTracker::SlotTracker(const Module *M)
42319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  : TheModule(M), TheFunction(0), FunctionProcessed(false),
424894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    mNext(0), fNext(0),  mdnNext(0) {
425894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
426894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
427894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Function level constructor. Causes the contents of the Module and the one
428894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// function provided to be added to the slot table.
429894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSlotTracker::SlotTracker(const Function *F)
430894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  : TheModule(F ? F->getParent() : 0), TheFunction(F), FunctionProcessed(false),
431894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    mNext(0), fNext(0), mdnNext(0) {
432894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
433894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
434894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumaninline void SlotTracker::initialize() {
435894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (TheModule) {
436894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    processModule();
437894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    TheModule = 0; ///< Prevent re-processing next time we're called.
438894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
439894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
440894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (TheFunction && !FunctionProcessed)
441894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    processFunction();
442894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
443894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
444894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Iterate through all the global variables, functions, and global
445894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// variable initializers and create slots for them.
446894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid SlotTracker::processModule() {
447894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ST_DEBUG("begin processModule!\n");
448894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
449894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Add all of the unnamed global variables to the value table.
450894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (Module::const_global_iterator I = TheModule->global_begin(),
451894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         E = TheModule->global_end(); I != E; ++I) {
452894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!I->hasName())
453894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      CreateModuleSlot(I);
454894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
455894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
456894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Add metadata used by named metadata.
457894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (Module::const_named_metadata_iterator
458894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         I = TheModule->named_metadata_begin(),
459894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         E = TheModule->named_metadata_end(); I != E; ++I) {
460894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    const NamedMDNode *NMD = I;
461894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
462894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      CreateMetadataSlot(NMD->getOperand(i));
463894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
464894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
465894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Add all the unnamed functions to the table.
466894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
467894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman       I != E; ++I)
468894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!I->hasName())
469894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      CreateModuleSlot(I);
470894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
471894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ST_DEBUG("end processModule!\n");
472894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
473894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
474894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Process the arguments, basic blocks, and instructions  of a function.
475894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid SlotTracker::processFunction() {
476894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ST_DEBUG("begin processFunction!\n");
477894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  fNext = 0;
478894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
479894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Add all the function arguments with no names.
480894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for(Function::const_arg_iterator AI = TheFunction->arg_begin(),
481894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      AE = TheFunction->arg_end(); AI != AE; ++AI)
482894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!AI->hasName())
483894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      CreateFunctionSlot(AI);
484894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
485894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ST_DEBUG("Inserting Instructions:\n");
486894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
487894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SmallVector<std::pair<unsigned, MDNode*>, 4> MDForInst;
488894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
489894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Add all of the basic blocks and instructions with no names.
490894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (Function::const_iterator BB = TheFunction->begin(),
491894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman       E = TheFunction->end(); BB != E; ++BB) {
492894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!BB->hasName())
493894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      CreateFunctionSlot(BB);
49419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
495894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E;
496894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         ++I) {
497894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (!I->getType()->isVoidTy() && !I->hasName())
498894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        CreateFunctionSlot(I);
49919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
500894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Intrinsics can directly use metadata.  We allow direct calls to any
501894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // llvm.foo function here, because the target may not be linked into the
502894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // optimizer.
503894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (const CallInst *CI = dyn_cast<CallInst>(I)) {
504894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (Function *F = CI->getCalledFunction())
505894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          if (F->getName().startswith("llvm."))
506894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
507894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman              if (MDNode *N = dyn_cast_or_null<MDNode>(I->getOperand(i)))
508894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                CreateMetadataSlot(N);
509894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
510894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
511894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Process metadata attached with this instruction.
512894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      I->getAllMetadata(MDForInst);
513894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      for (unsigned i = 0, e = MDForInst.size(); i != e; ++i)
514894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        CreateMetadataSlot(MDForInst[i].second);
515894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      MDForInst.clear();
516894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
517894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
518894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
519894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  FunctionProcessed = true;
520894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
521894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ST_DEBUG("end processFunction!\n");
522894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
523894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
524894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// Clean up after incorporating a function. This is the only way to get out of
525894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// the function incorporation state that affects get*Slot/Create*Slot. Function
526894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// incorporation state is indicated by TheFunction != 0.
527894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid SlotTracker::purgeFunction() {
528894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ST_DEBUG("begin purgeFunction!\n");
529894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  fMap.clear(); // Simply discard the function level map
530894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  TheFunction = 0;
531894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  FunctionProcessed = false;
532894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ST_DEBUG("end purgeFunction!\n");
533894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
534894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
535894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// getGlobalSlot - Get the slot number of a global value.
536894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanint SlotTracker::getGlobalSlot(const GlobalValue *V) {
537894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Check for uninitialized state and do lazy initialization.
538894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  initialize();
539894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
54019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Find the value in the module map
541894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ValueMap::iterator MI = mMap.find(V);
542894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return MI == mMap.end() ? -1 : (int)MI->second;
543894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
544894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
545894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// getMetadataSlot - Get the slot number of a MDNode.
546894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanint SlotTracker::getMetadataSlot(const MDNode *N) {
547894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Check for uninitialized state and do lazy initialization.
548894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  initialize();
549894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
55019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Find the MDNode in the module map
551894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  mdn_iterator MI = mdnMap.find(N);
552894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return MI == mdnMap.end() ? -1 : (int)MI->second;
553894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
554894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
555894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
556894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// getLocalSlot - Get the slot number for a value that is local to a function.
557894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanint SlotTracker::getLocalSlot(const Value *V) {
558894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(!isa<Constant>(V) && "Can't get a constant or global slot with this!");
559894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
560894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Check for uninitialized state and do lazy initialization.
561894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  initialize();
562894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
563894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ValueMap::iterator FI = fMap.find(V);
564894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return FI == fMap.end() ? -1 : (int)FI->second;
565894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
566894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
567894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
568894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
569894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid SlotTracker::CreateModuleSlot(const GlobalValue *V) {
570894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(V && "Can't insert a null Value into SlotTracker!");
571894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(!V->getType()->isVoidTy() && "Doesn't need a slot!");
572894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(!V->hasName() && "Doesn't need a slot!");
573894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
574894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned DestSlot = mNext++;
575894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  mMap[V] = DestSlot;
576894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
577894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ST_DEBUG("  Inserting value [" << V->getType() << "] = " << V << " slot=" <<
578894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman           DestSlot << " [");
579894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // G = Global, F = Function, A = Alias, o = other
580894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ST_DEBUG((isa<GlobalVariable>(V) ? 'G' :
581894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            (isa<Function>(V) ? 'F' :
582894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman             (isa<GlobalAlias>(V) ? 'A' : 'o'))) << "]\n");
583894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
584894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
585894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// CreateSlot - Create a new slot for the specified value if it has no name.
586894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid SlotTracker::CreateFunctionSlot(const Value *V) {
587894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(!V->getType()->isVoidTy() && !V->hasName() && "Doesn't need a slot!");
588894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
589894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned DestSlot = fNext++;
590894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  fMap[V] = DestSlot;
591894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
592894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // G = Global, F = Function, o = other
593894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ST_DEBUG("  Inserting value [" << V->getType() << "] = " << V << " slot=" <<
594894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman           DestSlot << " [o]\n");
595894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
596894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
597894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// CreateModuleSlot - Insert the specified MDNode* into the slot table.
598894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid SlotTracker::CreateMetadataSlot(const MDNode *N) {
599894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(N && "Can't insert a null Value into SlotTracker!");
600894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
601894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Don't insert if N is a function-local metadata, these are always printed
602894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // inline.
603894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!N->isFunctionLocal()) {
604894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    mdn_iterator I = mdnMap.find(N);
605894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (I != mdnMap.end())
606894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return;
607894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
608894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    unsigned DestSlot = mdnNext++;
609894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    mdnMap[N] = DestSlot;
610894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
611894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
612894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Recursively add any MDNodes referenced by operands.
613894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
614894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (const MDNode *Op = dyn_cast_or_null<MDNode>(N->getOperand(i)))
615894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      CreateMetadataSlot(Op);
616894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
617894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
618894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
619894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// AsmWriter Implementation
620894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
621894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
622894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
623894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                   TypePrinting *TypePrinter,
624894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                   SlotTracker *Machine,
625894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                   const Module *Context);
626894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
627894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
628894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
629894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic const char *getPredicateText(unsigned predicate) {
630894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const char * pred = "unknown";
631894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  switch (predicate) {
632894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case FCmpInst::FCMP_FALSE: pred = "false"; break;
633894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case FCmpInst::FCMP_OEQ:   pred = "oeq"; break;
634894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case FCmpInst::FCMP_OGT:   pred = "ogt"; break;
635894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case FCmpInst::FCMP_OGE:   pred = "oge"; break;
636894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case FCmpInst::FCMP_OLT:   pred = "olt"; break;
637894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case FCmpInst::FCMP_OLE:   pred = "ole"; break;
638894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case FCmpInst::FCMP_ONE:   pred = "one"; break;
639894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case FCmpInst::FCMP_ORD:   pred = "ord"; break;
640894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case FCmpInst::FCMP_UNO:   pred = "uno"; break;
641894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case FCmpInst::FCMP_UEQ:   pred = "ueq"; break;
642894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case FCmpInst::FCMP_UGT:   pred = "ugt"; break;
643894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case FCmpInst::FCMP_UGE:   pred = "uge"; break;
644894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case FCmpInst::FCMP_ULT:   pred = "ult"; break;
645894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case FCmpInst::FCMP_ULE:   pred = "ule"; break;
646894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case FCmpInst::FCMP_UNE:   pred = "une"; break;
647894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case FCmpInst::FCMP_TRUE:  pred = "true"; break;
648894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ICmpInst::ICMP_EQ:    pred = "eq"; break;
649894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ICmpInst::ICMP_NE:    pred = "ne"; break;
650894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ICmpInst::ICMP_SGT:   pred = "sgt"; break;
651894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ICmpInst::ICMP_SGE:   pred = "sge"; break;
652894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ICmpInst::ICMP_SLT:   pred = "slt"; break;
653894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ICmpInst::ICMP_SLE:   pred = "sle"; break;
654894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ICmpInst::ICMP_UGT:   pred = "ugt"; break;
655894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ICmpInst::ICMP_UGE:   pred = "uge"; break;
656894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ICmpInst::ICMP_ULT:   pred = "ult"; break;
657894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ICmpInst::ICMP_ULE:   pred = "ule"; break;
658894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
659894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return pred;
660894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
661894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
66219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic void writeAtomicRMWOperation(raw_ostream &Out,
66319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                    AtomicRMWInst::BinOp Op) {
66419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  switch (Op) {
66519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  default: Out << " <unknown operation " << Op << ">"; break;
66619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case AtomicRMWInst::Xchg: Out << " xchg"; break;
66719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case AtomicRMWInst::Add:  Out << " add"; break;
66819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case AtomicRMWInst::Sub:  Out << " sub"; break;
66919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case AtomicRMWInst::And:  Out << " and"; break;
67019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case AtomicRMWInst::Nand: Out << " nand"; break;
67119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case AtomicRMWInst::Or:   Out << " or"; break;
67219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case AtomicRMWInst::Xor:  Out << " xor"; break;
67319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case AtomicRMWInst::Max:  Out << " max"; break;
67419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case AtomicRMWInst::Min:  Out << " min"; break;
67519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case AtomicRMWInst::UMax: Out << " umax"; break;
67619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case AtomicRMWInst::UMin: Out << " umin"; break;
67719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
67819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
679894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
680894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic void WriteOptimizationInfo(raw_ostream &Out, const User *U) {
681894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (const OverflowingBinaryOperator *OBO =
682894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        dyn_cast<OverflowingBinaryOperator>(U)) {
683894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (OBO->hasNoUnsignedWrap())
684894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << " nuw";
685894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (OBO->hasNoSignedWrap())
686894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << " nsw";
68719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  } else if (const PossiblyExactOperator *Div =
68819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman               dyn_cast<PossiblyExactOperator>(U)) {
689894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Div->isExact())
690894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << " exact";
691894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (const GEPOperator *GEP = dyn_cast<GEPOperator>(U)) {
692894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (GEP->isInBounds())
693894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << " inbounds";
694894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
695894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
696894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
697894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic void WriteConstantInternal(raw_ostream &Out, const Constant *CV,
698894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                  TypePrinting &TypePrinter,
699894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                  SlotTracker *Machine,
700894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                  const Module *Context) {
701894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
702894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (CI->getType()->isIntegerTy(1)) {
703894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << (CI->getZExtValue() ? "true" : "false");
704894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return;
705894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
706894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << CI->getValue();
707894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return;
708894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
709894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
710894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
711894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEdouble ||
712894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        &CFP->getValueAPF().getSemantics() == &APFloat::IEEEsingle) {
713894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // We would like to output the FP constant value in exponential notation,
714894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // but we cannot do this if doing so will lose precision.  Check here to
715894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // make sure that we only output it in exponential format if we can parse
716894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // the value back and get the same value.
717894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      //
718894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      bool ignored;
719894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      bool isDouble = &CFP->getValueAPF().getSemantics()==&APFloat::IEEEdouble;
720894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      double Val = isDouble ? CFP->getValueAPF().convertToDouble() :
721894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                              CFP->getValueAPF().convertToFloat();
722894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      SmallString<128> StrVal;
723894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      raw_svector_ostream(StrVal) << Val;
724894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
725894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Check to make sure that the stringized number is not some string like
726894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // "Inf" or NaN, that atof will accept, but the lexer will not.  Check
727894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // that the string matches the "[-+]?[0-9]" regex.
728894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      //
729894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
730894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          ((StrVal[0] == '-' || StrVal[0] == '+') &&
731894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman           (StrVal[1] >= '0' && StrVal[1] <= '9'))) {
732894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // Reparse stringized version!
733894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (atof(StrVal.c_str()) == Val) {
734894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          Out << StrVal.str();
735894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          return;
736894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        }
737894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
738894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Otherwise we could not reparse it to exactly the same value, so we must
739894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // output the string in hexadecimal format!  Note that loading and storing
740894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // floating point types changes the bits of NaNs on some hosts, notably
741894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // x86, so we must not use these types.
742894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      assert(sizeof(double) == sizeof(uint64_t) &&
743894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman             "assuming that double is 64 bits!");
744894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      char Buffer[40];
745894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      APFloat apf = CFP->getValueAPF();
746894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Floats are represented in ASCII IR as double, convert.
747894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (!isDouble)
748894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        apf.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven,
749894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                          &ignored);
750894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << "0x" <<
751894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman              utohex_buffer(uint64_t(apf.bitcastToAPInt().getZExtValue()),
752894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                            Buffer+40);
753894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return;
754894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
755894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
756894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Some form of long double.  These appear as a magic letter identifying
757894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // the type, then a fixed number of hex digits.
758894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "0x";
759894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (&CFP->getValueAPF().getSemantics() == &APFloat::x87DoubleExtended) {
760894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << 'K';
761894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // api needed to prevent premature destruction
762894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      APInt api = CFP->getValueAPF().bitcastToAPInt();
763894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      const uint64_t* p = api.getRawData();
764894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      uint64_t word = p[1];
765894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      int shiftcount=12;
766894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      int width = api.getBitWidth();
767894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      for (int j=0; j<width; j+=4, shiftcount-=4) {
768894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        unsigned int nibble = (word>>shiftcount) & 15;
769894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (nibble < 10)
770894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          Out << (unsigned char)(nibble + '0');
771894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        else
772894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          Out << (unsigned char)(nibble - 10 + 'A');
773894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (shiftcount == 0 && j+4 < width) {
774894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          word = *p;
775894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          shiftcount = 64;
776894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          if (width-j-4 < 64)
777894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            shiftcount = width-j-4;
778894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        }
779894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
780894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return;
781894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    } else if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEquad)
782894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << 'L';
783894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else if (&CFP->getValueAPF().getSemantics() == &APFloat::PPCDoubleDouble)
784894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << 'M';
785894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else
786894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      llvm_unreachable("Unsupported floating point type");
787894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // api needed to prevent premature destruction
788894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    APInt api = CFP->getValueAPF().bitcastToAPInt();
789894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    const uint64_t* p = api.getRawData();
790894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    uint64_t word = *p;
791894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    int shiftcount=60;
792894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    int width = api.getBitWidth();
793894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (int j=0; j<width; j+=4, shiftcount-=4) {
794894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      unsigned int nibble = (word>>shiftcount) & 15;
795894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (nibble < 10)
796894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Out << (unsigned char)(nibble + '0');
797894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      else
798894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Out << (unsigned char)(nibble - 10 + 'A');
799894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (shiftcount == 0 && j+4 < width) {
800894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        word = *(++p);
801894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        shiftcount = 64;
802894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (width-j-4 < 64)
803894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          shiftcount = width-j-4;
804894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
805894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
806894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return;
807894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
808894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
809894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (isa<ConstantAggregateZero>(CV)) {
810894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "zeroinitializer";
811894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return;
812894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
81319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
814894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (const BlockAddress *BA = dyn_cast<BlockAddress>(CV)) {
815894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "blockaddress(";
816894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    WriteAsOperandInternal(Out, BA->getFunction(), &TypePrinter, Machine,
817894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                           Context);
818894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << ", ";
819894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    WriteAsOperandInternal(Out, BA->getBasicBlock(), &TypePrinter, Machine,
820894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                           Context);
821894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << ")";
822894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return;
823894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
824894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
825894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
826894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // As a special case, print the array as a string if it is an array of
827894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // i8 with ConstantInt values.
828894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    //
82919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Type *ETy = CA->getType()->getElementType();
830894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (CA->isString()) {
831894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << "c\"";
832894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      PrintEscapedString(CA->getAsString(), Out);
833894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << '"';
834894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    } else {                // Cannot output in string format...
835894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << '[';
836894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (CA->getNumOperands()) {
837894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        TypePrinter.print(ETy, Out);
838894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Out << ' ';
839894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        WriteAsOperandInternal(Out, CA->getOperand(0),
840894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                               &TypePrinter, Machine,
841894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                               Context);
842894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
843894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          Out << ", ";
844894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          TypePrinter.print(ETy, Out);
845894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          Out << ' ';
846894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          WriteAsOperandInternal(Out, CA->getOperand(i), &TypePrinter, Machine,
847894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                 Context);
848894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        }
849894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
850894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << ']';
851894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
852894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return;
853894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
854894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
855894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
856894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (CS->getType()->isPacked())
857894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << '<';
858894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << '{';
859894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    unsigned N = CS->getNumOperands();
860894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (N) {
861894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << ' ';
862894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      TypePrinter.print(CS->getOperand(0)->getType(), Out);
863894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << ' ';
864894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
865894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      WriteAsOperandInternal(Out, CS->getOperand(0), &TypePrinter, Machine,
866894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                             Context);
867894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
868894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      for (unsigned i = 1; i < N; i++) {
869894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Out << ", ";
870894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        TypePrinter.print(CS->getOperand(i)->getType(), Out);
871894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Out << ' ';
872894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
873894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        WriteAsOperandInternal(Out, CS->getOperand(i), &TypePrinter, Machine,
874894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                               Context);
875894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
876894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << ' ';
877894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
878894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
879894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << '}';
880894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (CS->getType()->isPacked())
881894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << '>';
882894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return;
883894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
884894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
885894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
88619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Type *ETy = CP->getType()->getElementType();
887894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    assert(CP->getNumOperands() > 0 &&
888894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman           "Number of operands for a PackedConst must be > 0");
889894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << '<';
890894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    TypePrinter.print(ETy, Out);
891894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << ' ';
892894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    WriteAsOperandInternal(Out, CP->getOperand(0), &TypePrinter, Machine,
893894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                           Context);
894894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (unsigned i = 1, e = CP->getNumOperands(); i != e; ++i) {
895894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << ", ";
896894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      TypePrinter.print(ETy, Out);
897894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << ' ';
898894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      WriteAsOperandInternal(Out, CP->getOperand(i), &TypePrinter, Machine,
899894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                             Context);
900894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
901894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << '>';
902894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return;
903894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
904894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
905894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (isa<ConstantPointerNull>(CV)) {
906894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "null";
907894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return;
908894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
909894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
910894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (isa<UndefValue>(CV)) {
911894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "undef";
912894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return;
913894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
914894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
915894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
916894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << CE->getOpcodeName();
917894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    WriteOptimizationInfo(Out, CE);
918894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (CE->isCompare())
919894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << ' ' << getPredicateText(CE->getPredicate());
920894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << " (";
921894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
922894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) {
923894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      TypePrinter.print((*OI)->getType(), Out);
924894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << ' ';
925894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      WriteAsOperandInternal(Out, *OI, &TypePrinter, Machine, Context);
926894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (OI+1 != CE->op_end())
927894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Out << ", ";
928894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
929894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
930894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (CE->hasIndices()) {
93119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      ArrayRef<unsigned> Indices = CE->getIndices();
932894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      for (unsigned i = 0, e = Indices.size(); i != e; ++i)
933894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Out << ", " << Indices[i];
934894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
935894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
936894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (CE->isCast()) {
937894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << " to ";
938894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      TypePrinter.print(CE->getType(), Out);
939894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
940894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
941894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << ')';
942894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return;
943894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
944894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
945894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << "<placeholder or erroneous Constant>";
946894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
947894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
948894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic void WriteMDNodeBodyInternal(raw_ostream &Out, const MDNode *Node,
949894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                    TypePrinting *TypePrinter,
950894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                    SlotTracker *Machine,
951894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                    const Module *Context) {
952894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << "!{";
953894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned mi = 0, me = Node->getNumOperands(); mi != me; ++mi) {
954894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    const Value *V = Node->getOperand(mi);
955894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (V == 0)
956894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << "null";
957894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else {
958894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      TypePrinter->print(V->getType(), Out);
959894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << ' ';
96019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      WriteAsOperandInternal(Out, Node->getOperand(mi),
961894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                             TypePrinter, Machine, Context);
962894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
963894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (mi + 1 != me)
964894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << ", ";
965894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
96619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
967894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << "}";
968894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
969894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
970894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
971894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// WriteAsOperand - Write the name of the specified value out to the specified
972894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// ostream.  This can be useful when you just want to print int %reg126, not
973894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// the whole instruction that generated it.
974894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///
975894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
976894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                   TypePrinting *TypePrinter,
977894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                   SlotTracker *Machine,
978894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                   const Module *Context) {
979894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (V->hasName()) {
980894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PrintLLVMName(Out, V);
981894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return;
982894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
983894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
984894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const Constant *CV = dyn_cast<Constant>(V);
985894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (CV && !isa<GlobalValue>(CV)) {
986894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    assert(TypePrinter && "Constants require TypePrinting!");
987894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    WriteConstantInternal(Out, CV, *TypePrinter, Machine, Context);
988894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return;
989894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
990894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
991894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
992894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "asm ";
993894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (IA->hasSideEffects())
994894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << "sideeffect ";
995894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (IA->isAlignStack())
996894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << "alignstack ";
997894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << '"';
998894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PrintEscapedString(IA->getAsmString(), Out);
999894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "\", \"";
1000894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PrintEscapedString(IA->getConstraintString(), Out);
1001894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << '"';
1002894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return;
1003894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1004894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1005894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (const MDNode *N = dyn_cast<MDNode>(V)) {
1006894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (N->isFunctionLocal()) {
1007894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Print metadata inline, not via slot reference number.
1008894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      WriteMDNodeBodyInternal(Out, N, TypePrinter, Machine, Context);
1009894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return;
1010894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
101119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1012894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!Machine) {
1013894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (N->isFunctionLocal())
1014894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Machine = new SlotTracker(N->getFunction());
1015894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      else
1016894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Machine = new SlotTracker(Context);
1017894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
101819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    int Slot = Machine->getMetadataSlot(N);
101919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (Slot == -1)
102019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Out << "<badref>";
102119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    else
102219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Out << '!' << Slot;
1023894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return;
1024894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1025894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1026894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (const MDString *MDS = dyn_cast<MDString>(V)) {
1027894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "!\"";
1028894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PrintEscapedString(MDS->getString(), Out);
1029894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << '"';
1030894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return;
1031894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1032894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1033894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (V->getValueID() == Value::PseudoSourceValueVal ||
1034894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      V->getValueID() == Value::FixedStackPseudoSourceValueVal) {
1035894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    V->print(Out);
1036894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return;
1037894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1038894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1039894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  char Prefix = '%';
1040894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  int Slot;
104119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // If we have a SlotTracker, use it.
1042894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Machine) {
1043894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1044894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Slot = Machine->getGlobalSlot(GV);
1045894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Prefix = '@';
1046894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    } else {
1047894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Slot = Machine->getLocalSlot(V);
104819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
104919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      // If the local value didn't succeed, then we may be referring to a value
105019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      // from a different function.  Translate it, as this can happen when using
105119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      // address of blocks.
105219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (Slot == -1)
105319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        if ((Machine = createSlotTracker(V))) {
105419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          Slot = Machine->getLocalSlot(V);
105519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          delete Machine;
105619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        }
1057894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
105819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  } else if ((Machine = createSlotTracker(V))) {
105919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Otherwise, create one to get the # and then destroy it.
106019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
106119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Slot = Machine->getGlobalSlot(GV);
106219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Prefix = '@';
1063894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    } else {
106419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Slot = Machine->getLocalSlot(V);
1065894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
106619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    delete Machine;
106719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Machine = 0;
106819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  } else {
106919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Slot = -1;
1070894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1071894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1072894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Slot != -1)
1073894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << Prefix << Slot;
1074894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  else
1075894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "<badref>";
1076894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1077894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1078894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid llvm::WriteAsOperand(raw_ostream &Out, const Value *V,
1079894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                          bool PrintType, const Module *Context) {
1080894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1081894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Fast path: Don't construct and populate a TypePrinting object if we
1082894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // won't be needing any types printed.
1083894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!PrintType &&
1084894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      ((!isa<Constant>(V) && !isa<MDNode>(V)) ||
1085894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman       V->hasName() || isa<GlobalValue>(V))) {
1086894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    WriteAsOperandInternal(Out, V, 0, 0, Context);
1087894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return;
1088894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1089894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1090894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Context == 0) Context = getModuleFromVal(V);
1091894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1092894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  TypePrinting TypePrinter;
109319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Context)
109419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    TypePrinter.incorporateTypes(*Context);
1095894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (PrintType) {
1096894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    TypePrinter.print(V->getType(), Out);
1097894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << ' ';
1098894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1099894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1100894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  WriteAsOperandInternal(Out, V, &TypePrinter, 0, Context);
1101894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1102894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1103894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumannamespace {
1104894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1105894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass AssemblyWriter {
1106894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  formatted_raw_ostream &Out;
1107894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SlotTracker &Machine;
1108894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const Module *TheModule;
1109894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  TypePrinting TypePrinter;
1110894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  AssemblyAnnotationWriter *AnnotationWriter;
111119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1112894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanpublic:
1113894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  inline AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac,
1114894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                        const Module *M,
1115894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                        AssemblyAnnotationWriter *AAW)
1116894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    : Out(o), Machine(Mac), TheModule(M), AnnotationWriter(AAW) {
111719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (M)
111819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      TypePrinter.incorporateTypes(*M);
1119894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1120894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1121894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void printMDNodeBody(const MDNode *MD);
1122894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void printNamedMDNode(const NamedMDNode *NMD);
112319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1124894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void printModule(const Module *M);
1125894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1126894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void writeOperand(const Value *Op, bool PrintType);
1127894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void writeParamOperand(const Value *Operand, Attributes Attrs);
112819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  void writeAtomic(AtomicOrdering Ordering, SynchronizationScope SynchScope);
1129894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1130894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void writeAllMDNodes();
1131894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
113219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  void printTypeIdentities();
1133894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void printGlobal(const GlobalVariable *GV);
1134894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void printAlias(const GlobalAlias *GV);
1135894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void printFunction(const Function *F);
1136894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void printArgument(const Argument *FA, Attributes Attrs);
1137894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void printBasicBlock(const BasicBlock *BB);
1138894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void printInstruction(const Instruction &I);
1139894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1140894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanprivate:
1141894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // printInfoComment - Print a little comment after the instruction indicating
1142894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // which slot it occupies.
1143894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void printInfoComment(const Value &V);
1144894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman};
1145894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}  // end of anonymous namespace
1146894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1147894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid AssemblyWriter::writeOperand(const Value *Operand, bool PrintType) {
1148894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Operand == 0) {
1149894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "<null operand!>";
1150894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return;
1151894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1152894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (PrintType) {
1153894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    TypePrinter.print(Operand->getType(), Out);
1154894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << ' ';
1155894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1156894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  WriteAsOperandInternal(Out, Operand, &TypePrinter, &Machine, TheModule);
1157894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1158894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
115919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanvoid AssemblyWriter::writeAtomic(AtomicOrdering Ordering,
116019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                 SynchronizationScope SynchScope) {
116119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Ordering == NotAtomic)
116219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return;
116319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
116419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  switch (SynchScope) {
116519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  default: Out << " <bad scope " << int(SynchScope) << ">"; break;
116619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case SingleThread: Out << " singlethread"; break;
116719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case CrossThread: break;
116819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
116919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
117019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  switch (Ordering) {
117119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  default: Out << " <bad ordering " << int(Ordering) << ">"; break;
117219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case Unordered: Out << " unordered"; break;
117319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case Monotonic: Out << " monotonic"; break;
117419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case Acquire: Out << " acquire"; break;
117519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case Release: Out << " release"; break;
117619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case AcquireRelease: Out << " acq_rel"; break;
117719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case SequentiallyConsistent: Out << " seq_cst"; break;
117819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
117919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
118019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1181894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid AssemblyWriter::writeParamOperand(const Value *Operand,
1182894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                       Attributes Attrs) {
1183894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Operand == 0) {
1184894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "<null operand!>";
1185894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return;
1186894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1187894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1188894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Print the type
1189894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  TypePrinter.print(Operand->getType(), Out);
1190894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Print parameter attributes list
1191894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Attrs != Attribute::None)
1192894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << ' ' << Attribute::getAsString(Attrs);
1193894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << ' ';
1194894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Print the operand
1195894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  WriteAsOperandInternal(Out, Operand, &TypePrinter, &Machine, TheModule);
1196894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1197894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1198894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid AssemblyWriter::printModule(const Module *M) {
1199894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!M->getModuleIdentifier().empty() &&
1200894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Don't print the ID if it will start a new line (which would
1201894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // require a comment char before it).
1202894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      M->getModuleIdentifier().find('\n') == std::string::npos)
1203894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "; ModuleID = '" << M->getModuleIdentifier() << "'\n";
1204894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1205894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!M->getDataLayout().empty())
1206894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "target datalayout = \"" << M->getDataLayout() << "\"\n";
1207894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!M->getTargetTriple().empty())
1208894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "target triple = \"" << M->getTargetTriple() << "\"\n";
1209894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1210894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!M->getModuleInlineAsm().empty()) {
1211894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Split the string into lines, to make it easier to read the .ll file.
1212894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    std::string Asm = M->getModuleInlineAsm();
1213894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    size_t CurPos = 0;
1214894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    size_t NewLine = Asm.find_first_of('\n', CurPos);
1215894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << '\n';
1216894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    while (NewLine != std::string::npos) {
1217894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // We found a newline, print the portion of the asm string from the
1218894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // last newline up to this newline.
1219894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << "module asm \"";
1220894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.begin()+NewLine),
1221894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                         Out);
1222894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << "\"\n";
1223894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      CurPos = NewLine+1;
1224894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      NewLine = Asm.find_first_of('\n', CurPos);
1225894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
122619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    std::string rest(Asm.begin()+CurPos, Asm.end());
122719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (!rest.empty()) {
122819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Out << "module asm \"";
122919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      PrintEscapedString(rest, Out);
123019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Out << "\"\n";
123119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
1232894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1233894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1234894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Loop over the dependent libraries and emit them.
1235894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Module::lib_iterator LI = M->lib_begin();
1236894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Module::lib_iterator LE = M->lib_end();
1237894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (LI != LE) {
1238894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << '\n';
1239894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "deplibs = [ ";
1240894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    while (LI != LE) {
1241894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << '"' << *LI << '"';
1242894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      ++LI;
1243894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (LI != LE)
1244894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Out << ", ";
1245894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
1246894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << " ]";
1247894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1248894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
124919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  printTypeIdentities();
1250894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1251894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Output all globals.
1252894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!M->global_empty()) Out << '\n';
1253894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
1254894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman       I != E; ++I)
1255894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    printGlobal(I);
1256894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1257894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Output all aliases.
1258894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!M->alias_empty()) Out << "\n";
1259894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
1260894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman       I != E; ++I)
1261894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    printAlias(I);
1262894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1263894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Output all of the functions.
1264894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
1265894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    printFunction(I);
1266894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1267894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Output named metadata.
1268894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!M->named_metadata_empty()) Out << '\n';
126919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1270894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (Module::const_named_metadata_iterator I = M->named_metadata_begin(),
1271894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman       E = M->named_metadata_end(); I != E; ++I)
1272894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    printNamedMDNode(I);
1273894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1274894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Output metadata.
1275894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!Machine.mdn_empty()) {
1276894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << '\n';
1277894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    writeAllMDNodes();
1278894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1279894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1280894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1281894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid AssemblyWriter::printNamedMDNode(const NamedMDNode *NMD) {
128219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Out << '!';
128319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  StringRef Name = NMD->getName();
128419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Name.empty()) {
128519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Out << "<empty name> ";
128619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  } else {
128719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (isalpha(Name[0]) || Name[0] == '-' || Name[0] == '$' ||
128819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        Name[0] == '.' || Name[0] == '_')
128919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Out << Name[0];
129019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    else
129119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Out << '\\' << hexdigit(Name[0] >> 4) << hexdigit(Name[0] & 0x0F);
129219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    for (unsigned i = 1, e = Name.size(); i != e; ++i) {
129319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      unsigned char C = Name[i];
129419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (isalnum(C) || C == '-' || C == '$' || C == '.' || C == '_')
129519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        Out << C;
129619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      else
129719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        Out << '\\' << hexdigit(C >> 4) << hexdigit(C & 0x0F);
129819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
129919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
130019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Out << " = !{";
1301894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1302894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (i) Out << ", ";
130319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    int Slot = Machine.getMetadataSlot(NMD->getOperand(i));
130419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (Slot == -1)
130519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Out << "<badref>";
130619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    else
130719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Out << '!' << Slot;
1308894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1309894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << "}\n";
1310894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1311894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1312894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1313894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic void PrintLinkage(GlobalValue::LinkageTypes LT,
1314894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                         formatted_raw_ostream &Out) {
1315894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  switch (LT) {
1316894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case GlobalValue::ExternalLinkage: break;
1317894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case GlobalValue::PrivateLinkage:       Out << "private ";        break;
1318894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case GlobalValue::LinkerPrivateLinkage: Out << "linker_private "; break;
1319894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case GlobalValue::LinkerPrivateWeakLinkage:
1320894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "linker_private_weak ";
1321894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
132219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case GlobalValue::LinkerPrivateWeakDefAutoLinkage:
132319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Out << "linker_private_weak_def_auto ";
132419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    break;
1325894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case GlobalValue::InternalLinkage:      Out << "internal ";       break;
1326894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case GlobalValue::LinkOnceAnyLinkage:   Out << "linkonce ";       break;
1327894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case GlobalValue::LinkOnceODRLinkage:   Out << "linkonce_odr ";   break;
1328894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case GlobalValue::WeakAnyLinkage:       Out << "weak ";           break;
1329894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case GlobalValue::WeakODRLinkage:       Out << "weak_odr ";       break;
1330894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case GlobalValue::CommonLinkage:        Out << "common ";         break;
1331894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case GlobalValue::AppendingLinkage:     Out << "appending ";      break;
1332894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case GlobalValue::DLLImportLinkage:     Out << "dllimport ";      break;
1333894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case GlobalValue::DLLExportLinkage:     Out << "dllexport ";      break;
1334894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case GlobalValue::ExternalWeakLinkage:  Out << "extern_weak ";    break;
1335894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case GlobalValue::AvailableExternallyLinkage:
1336894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "available_externally ";
1337894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
1338894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1339894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1340894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1341894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1342894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic void PrintVisibility(GlobalValue::VisibilityTypes Vis,
1343894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                            formatted_raw_ostream &Out) {
1344894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  switch (Vis) {
1345894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case GlobalValue::DefaultVisibility: break;
1346894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case GlobalValue::HiddenVisibility:    Out << "hidden "; break;
1347894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case GlobalValue::ProtectedVisibility: Out << "protected "; break;
1348894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1349894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1350894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1351894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid AssemblyWriter::printGlobal(const GlobalVariable *GV) {
1352894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (GV->isMaterializable())
1353894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "; Materializable\n";
1354894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1355894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  WriteAsOperandInternal(Out, GV, &TypePrinter, &Machine, GV->getParent());
1356894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << " = ";
1357894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1358894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!GV->hasInitializer() && GV->hasExternalLinkage())
1359894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "external ";
1360894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1361894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  PrintLinkage(GV->getLinkage(), Out);
1362894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  PrintVisibility(GV->getVisibility(), Out);
1363894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1364894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (GV->isThreadLocal()) Out << "thread_local ";
1365894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (unsigned AddressSpace = GV->getType()->getAddressSpace())
1366894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "addrspace(" << AddressSpace << ") ";
136719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (GV->hasUnnamedAddr()) Out << "unnamed_addr ";
1368894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << (GV->isConstant() ? "constant " : "global ");
1369894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  TypePrinter.print(GV->getType()->getElementType(), Out);
1370894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1371894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (GV->hasInitializer()) {
1372894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << ' ';
1373894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    writeOperand(GV->getInitializer(), false);
1374894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1375894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1376894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (GV->hasSection()) {
1377894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << ", section \"";
1378894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PrintEscapedString(GV->getSection(), Out);
1379894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << '"';
1380894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1381894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (GV->getAlignment())
1382894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << ", align " << GV->getAlignment();
1383894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1384894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  printInfoComment(*GV);
1385894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << '\n';
1386894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1387894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1388894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid AssemblyWriter::printAlias(const GlobalAlias *GA) {
1389894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (GA->isMaterializable())
1390894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "; Materializable\n";
1391894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1392894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Don't crash when dumping partially built GA
1393894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!GA->hasName())
1394894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "<<nameless>> = ";
1395894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  else {
1396894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PrintLLVMName(Out, GA);
1397894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << " = ";
1398894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1399894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  PrintVisibility(GA->getVisibility(), Out);
1400894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1401894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << "alias ";
1402894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1403894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  PrintLinkage(GA->getLinkage(), Out);
1404894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1405894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const Constant *Aliasee = GA->getAliasee();
1406894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
140719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Aliasee == 0) {
1408894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    TypePrinter.print(GA->getType(), Out);
140919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Out << " <<NULL ALIASEE>>";
1410894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else {
141119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    writeOperand(Aliasee, !isa<ConstantExpr>(Aliasee));
1412894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1413894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1414894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  printInfoComment(*GA);
1415894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << '\n';
1416894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1417894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
141819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanvoid AssemblyWriter::printTypeIdentities() {
141919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (TypePrinter.NumberedTypes.empty() &&
142019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      TypePrinter.NamedTypes.empty())
142119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return;
142219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
142319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Out << '\n';
142419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
142519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // We know all the numbers that each type is used and we know that it is a
142619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // dense assignment.  Convert the map to an index table.
142719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  std::vector<StructType*> NumberedTypes(TypePrinter.NumberedTypes.size());
142819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  for (DenseMap<StructType*, unsigned>::iterator I =
142919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman       TypePrinter.NumberedTypes.begin(), E = TypePrinter.NumberedTypes.end();
143019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman       I != E; ++I) {
143119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    assert(I->second < NumberedTypes.size() && "Didn't get a dense numbering?");
143219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    NumberedTypes[I->second] = I->first;
143319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
143419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1435894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Emit all numbered types.
1436894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 0, e = NumberedTypes.size(); i != e; ++i) {
1437894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << '%' << i << " = type ";
1438894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1439894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Make sure we print out at least one level of the type structure, so
1440894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // that we do not get %2 = type %2
144119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    TypePrinter.printStructBody(NumberedTypes[i], Out);
1442894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << '\n';
1443894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1444894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
144519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  for (unsigned i = 0, e = TypePrinter.NamedTypes.size(); i != e; ++i) {
144619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    PrintLLVMName(Out, TypePrinter.NamedTypes[i]->getName(), LocalPrefix);
1447894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << " = type ";
1448894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1449894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Make sure we print out at least one level of the type structure, so
1450894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // that we do not get %FILE = type %FILE
145119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    TypePrinter.printStructBody(TypePrinter.NamedTypes[i], Out);
1452894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << '\n';
1453894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1454894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1455894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1456894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// printFunction - Print all aspects of a function.
1457894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///
1458894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid AssemblyWriter::printFunction(const Function *F) {
1459894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Print out the return type and name.
1460894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << '\n';
1461894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1462894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, Out);
1463894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1464894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (F->isMaterializable())
1465894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "; Materializable\n";
1466894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1467894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (F->isDeclaration())
1468894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "declare ";
1469894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  else
1470894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "define ";
1471894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1472894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  PrintLinkage(F->getLinkage(), Out);
1473894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  PrintVisibility(F->getVisibility(), Out);
1474894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1475894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Print the calling convention.
1476894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  switch (F->getCallingConv()) {
1477894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case CallingConv::C: break;   // default
1478894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case CallingConv::Fast:         Out << "fastcc "; break;
1479894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case CallingConv::Cold:         Out << "coldcc "; break;
1480894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case CallingConv::X86_StdCall:  Out << "x86_stdcallcc "; break;
1481894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case CallingConv::X86_FastCall: Out << "x86_fastcallcc "; break;
1482894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case CallingConv::X86_ThisCall: Out << "x86_thiscallcc "; break;
1483894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case CallingConv::ARM_APCS:     Out << "arm_apcscc "; break;
1484894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case CallingConv::ARM_AAPCS:    Out << "arm_aapcscc "; break;
1485894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case CallingConv::ARM_AAPCS_VFP:Out << "arm_aapcs_vfpcc "; break;
1486894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case CallingConv::MSP430_INTR:  Out << "msp430_intrcc "; break;
148719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case CallingConv::PTX_Kernel:   Out << "ptx_kernel "; break;
148819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case CallingConv::PTX_Device:   Out << "ptx_device "; break;
1489894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  default: Out << "cc" << F->getCallingConv() << " "; break;
1490894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1491894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
149219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  FunctionType *FT = F->getFunctionType();
1493894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const AttrListPtr &Attrs = F->getAttributes();
1494894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Attributes RetAttrs = Attrs.getRetAttributes();
1495894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (RetAttrs != Attribute::None)
1496894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out <<  Attribute::getAsString(Attrs.getRetAttributes()) << ' ';
1497894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  TypePrinter.print(F->getReturnType(), Out);
1498894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << ' ';
1499894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  WriteAsOperandInternal(Out, F, &TypePrinter, &Machine, F->getParent());
1500894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << '(';
1501894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Machine.incorporateFunction(F);
1502894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1503894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Loop over the arguments, printing them...
1504894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1505894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned Idx = 1;
1506894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!F->isDeclaration()) {
1507894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If this isn't a declaration, print the argument names as well.
1508894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
1509894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         I != E; ++I) {
1510894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Insert commas as we go... the first arg doesn't get a comma
1511894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (I != F->arg_begin()) Out << ", ";
1512894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      printArgument(I, Attrs.getParamAttributes(Idx));
1513894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Idx++;
1514894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
1515894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else {
1516894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Otherwise, print the types from the function type.
1517894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
1518894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Insert commas as we go... the first arg doesn't get a comma
1519894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (i) Out << ", ";
1520894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1521894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Output type...
1522894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      TypePrinter.print(FT->getParamType(i), Out);
1523894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1524894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Attributes ArgAttrs = Attrs.getParamAttributes(i+1);
1525894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (ArgAttrs != Attribute::None)
1526894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Out << ' ' << Attribute::getAsString(ArgAttrs);
1527894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
1528894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1529894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1530894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Finish printing arguments...
1531894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (FT->isVarArg()) {
1532894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (FT->getNumParams()) Out << ", ";
1533894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "...";  // Output varargs portion of signature!
1534894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1535894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << ')';
153619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (F->hasUnnamedAddr())
153719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Out << " unnamed_addr";
1538894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Attributes FnAttrs = Attrs.getFnAttributes();
1539894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (FnAttrs != Attribute::None)
1540894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << ' ' << Attribute::getAsString(Attrs.getFnAttributes());
1541894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (F->hasSection()) {
1542894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << " section \"";
1543894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PrintEscapedString(F->getSection(), Out);
1544894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << '"';
1545894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1546894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (F->getAlignment())
1547894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << " align " << F->getAlignment();
154819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (F->hasGC())
154919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Out << " gc \"" << F->getGC() << '"';
1550894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (F->isDeclaration()) {
155119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Out << '\n';
1552894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else {
1553894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << " {";
155419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Output all of the function's basic blocks.
1555894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
1556894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      printBasicBlock(I);
1557894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1558894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "}\n";
1559894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1560894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1561894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Machine.purgeFunction();
1562894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1563894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1564894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// printArgument - This member is called for every argument that is passed into
1565894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// the function.  Simply print it out
1566894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///
1567894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid AssemblyWriter::printArgument(const Argument *Arg,
1568894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                   Attributes Attrs) {
1569894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Output type...
1570894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  TypePrinter.print(Arg->getType(), Out);
1571894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1572894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Output parameter attributes list
1573894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Attrs != Attribute::None)
1574894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << ' ' << Attribute::getAsString(Attrs);
1575894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1576894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Output name, if available...
1577894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Arg->hasName()) {
1578894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << ' ';
1579894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PrintLLVMName(Out, Arg);
1580894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1581894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1582894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1583894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// printBasicBlock - This member is called for each basic block in a method.
1584894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///
1585894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
1586894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (BB->hasName()) {              // Print out the label if it exists...
1587894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "\n";
1588894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PrintLLVMName(Out, BB->getName(), LabelPrefix);
1589894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << ':';
1590894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (!BB->use_empty()) {      // Don't print block # of no uses...
1591894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "\n; <label>:";
1592894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    int Slot = Machine.getLocalSlot(BB);
1593894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Slot != -1)
1594894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << Slot;
1595894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else
1596894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << "<badref>";
1597894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1598894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1599894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (BB->getParent() == 0) {
1600894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out.PadToColumn(50);
1601894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "; Error: Block without parent!";
1602894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (BB != &BB->getParent()->getEntryBlock()) {  // Not the entry block?
160319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Output predecessors for the block.
1604894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out.PadToColumn(50);
1605894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << ";";
1606894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    const_pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
1607894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1608894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (PI == PE) {
1609894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << " No predecessors!";
1610894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    } else {
1611894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << " preds = ";
1612894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      writeOperand(*PI, false);
1613894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      for (++PI; PI != PE; ++PI) {
1614894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Out << ", ";
1615894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        writeOperand(*PI, false);
1616894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
1617894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
1618894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1619894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1620894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << "\n";
1621894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1622894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, Out);
1623894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1624894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Output all of the instructions in the basic block...
1625894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
1626894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    printInstruction(*I);
1627894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << '\n';
1628894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1629894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1630894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, Out);
1631894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1632894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1633894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// printInfoComment - Print a little comment after the instruction indicating
1634894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// which slot it occupies.
1635894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///
1636894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid AssemblyWriter::printInfoComment(const Value &V) {
1637894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (AnnotationWriter) {
1638894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    AnnotationWriter->printInfoComment(V, Out);
1639894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return;
1640894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1641894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1642894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1643894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This member is called for each Instruction in a function..
1644894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid AssemblyWriter::printInstruction(const Instruction &I) {
1645894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, Out);
1646894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1647894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Print out indentation for an instruction.
1648894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << "  ";
1649894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1650894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Print out name if it exists...
1651894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (I.hasName()) {
1652894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PrintLLVMName(Out, &I);
1653894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << " = ";
1654894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (!I.getType()->isVoidTy()) {
1655894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Print out the def slot taken.
1656894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    int SlotNum = Machine.getLocalSlot(&I);
1657894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (SlotNum == -1)
1658894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << "<badref> = ";
1659894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else
1660894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << '%' << SlotNum << " = ";
1661894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1662894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
166319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (isa<CallInst>(I) && cast<CallInst>(I).isTailCall())
1664894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "tail ";
1665894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1666894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Print out the opcode...
1667894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << I.getOpcodeName();
1668894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
166919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // If this is an atomic load or store, print out the atomic marker.
167019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if ((isa<LoadInst>(I)  && cast<LoadInst>(I).isAtomic()) ||
167119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      (isa<StoreInst>(I) && cast<StoreInst>(I).isAtomic()))
167219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Out << " atomic";
167319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
167419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // If this is a volatile operation, print out the volatile marker.
167519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if ((isa<LoadInst>(I)  && cast<LoadInst>(I).isVolatile()) ||
167619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile()) ||
167719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      (isa<AtomicCmpXchgInst>(I) && cast<AtomicCmpXchgInst>(I).isVolatile()) ||
167819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      (isa<AtomicRMWInst>(I) && cast<AtomicRMWInst>(I).isVolatile()))
167919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Out << " volatile";
168019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1681894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Print out optimization information.
1682894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  WriteOptimizationInfo(Out, &I);
1683894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1684894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Print out the compare instruction predicates
1685894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (const CmpInst *CI = dyn_cast<CmpInst>(&I))
1686894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << ' ' << getPredicateText(CI->getPredicate());
1687894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
168819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Print out the atomicrmw operation
168919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(&I))
169019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    writeAtomicRMWOperation(Out, RMWI->getOperation());
169119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1692894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Print out the type of the operands...
1693894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0;
1694894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1695894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Special case conditional branches to swizzle the condition out to the front
1696894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (isa<BranchInst>(I) && cast<BranchInst>(I).isConditional()) {
1697894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    BranchInst &BI(cast<BranchInst>(I));
1698894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << ' ';
1699894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    writeOperand(BI.getCondition(), true);
1700894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << ", ";
1701894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    writeOperand(BI.getSuccessor(0), true);
1702894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << ", ";
1703894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    writeOperand(BI.getSuccessor(1), true);
1704894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1705894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (isa<SwitchInst>(I)) {
170619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SwitchInst& SI(cast<SwitchInst>(I));
1707894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Special case switch instruction to get formatting nice and correct.
1708894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << ' ';
170919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    writeOperand(SI.getCondition(), true);
1710894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << ", ";
171119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    writeOperand(SI.getDefaultDest(), true);
1712894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << " [";
171319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Skip the first item since that's the default case.
171419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    unsigned NumCases = SI.getNumCases();
171519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    for (unsigned i = 1; i < NumCases; ++i) {
1716894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << "\n    ";
171719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      writeOperand(SI.getCaseValue(i), true);
1718894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << ", ";
171919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      writeOperand(SI.getSuccessor(i), true);
1720894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
1721894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "\n  ]";
1722894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (isa<IndirectBrInst>(I)) {
1723894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Special case indirectbr instruction to get formatting nice and correct.
1724894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << ' ';
1725894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    writeOperand(Operand, true);
1726894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << ", [";
172719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1728894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) {
1729894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (i != 1)
1730894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Out << ", ";
1731894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      writeOperand(I.getOperand(i), true);
1732894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
1733894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << ']';
173419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  } else if (const PHINode *PN = dyn_cast<PHINode>(&I)) {
1735894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << ' ';
1736894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    TypePrinter.print(I.getType(), Out);
1737894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << ' ';
1738894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
173919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    for (unsigned op = 0, Eop = PN->getNumIncomingValues(); op < Eop; ++op) {
1740894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (op) Out << ", ";
1741894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << "[ ";
174219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      writeOperand(PN->getIncomingValue(op), false); Out << ", ";
174319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      writeOperand(PN->getIncomingBlock(op), false); Out << " ]";
1744894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
1745894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(&I)) {
1746894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << ' ';
1747894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    writeOperand(I.getOperand(0), true);
1748894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (const unsigned *i = EVI->idx_begin(), *e = EVI->idx_end(); i != e; ++i)
1749894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << ", " << *i;
1750894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(&I)) {
1751894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << ' ';
1752894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    writeOperand(I.getOperand(0), true); Out << ", ";
1753894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    writeOperand(I.getOperand(1), true);
1754894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (const unsigned *i = IVI->idx_begin(), *e = IVI->idx_end(); i != e; ++i)
1755894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << ", " << *i;
175619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  } else if (const LandingPadInst *LPI = dyn_cast<LandingPadInst>(&I)) {
175719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Out << ' ';
175819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    TypePrinter.print(I.getType(), Out);
175919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Out << " personality ";
176019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    writeOperand(I.getOperand(0), true); Out << '\n';
176119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
176219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (LPI->isCleanup())
176319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Out << "          cleanup";
176419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
176519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    for (unsigned i = 0, e = LPI->getNumClauses(); i != e; ++i) {
176619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (i != 0 || LPI->isCleanup()) Out << "\n";
176719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (LPI->isCatch(i))
176819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        Out << "          catch ";
176919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      else
177019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        Out << "          filter ";
177119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
177219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      writeOperand(LPI->getClause(i), true);
177319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
1774894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (isa<ReturnInst>(I) && !Operand) {
1775894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << " void";
1776894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
1777894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Print the calling convention being used.
1778894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    switch (CI->getCallingConv()) {
1779894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case CallingConv::C: break;   // default
1780894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case CallingConv::Fast:  Out << " fastcc"; break;
1781894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case CallingConv::Cold:  Out << " coldcc"; break;
1782894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case CallingConv::X86_StdCall:  Out << " x86_stdcallcc"; break;
1783894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case CallingConv::X86_FastCall: Out << " x86_fastcallcc"; break;
1784894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case CallingConv::X86_ThisCall: Out << " x86_thiscallcc"; break;
1785894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case CallingConv::ARM_APCS:     Out << " arm_apcscc "; break;
1786894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case CallingConv::ARM_AAPCS:    Out << " arm_aapcscc "; break;
1787894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case CallingConv::ARM_AAPCS_VFP:Out << " arm_aapcs_vfpcc "; break;
1788894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case CallingConv::MSP430_INTR:  Out << " msp430_intrcc "; break;
178919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case CallingConv::PTX_Kernel:   Out << " ptx_kernel"; break;
179019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case CallingConv::PTX_Device:   Out << " ptx_device"; break;
1791894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    default: Out << " cc" << CI->getCallingConv(); break;
1792894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
1793894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1794894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Operand = CI->getCalledValue();
179519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    PointerType *PTy = cast<PointerType>(Operand->getType());
179619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
179719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Type *RetTy = FTy->getReturnType();
1798894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    const AttrListPtr &PAL = CI->getAttributes();
1799894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1800894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (PAL.getRetAttributes() != Attribute::None)
1801894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << ' ' << Attribute::getAsString(PAL.getRetAttributes());
1802894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1803894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If possible, print out the short form of the call instruction.  We can
1804894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // only do this if the first argument is a pointer to a nonvararg function,
1805894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // and if the return type is not a pointer to a function.
1806894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    //
1807894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << ' ';
1808894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!FTy->isVarArg() &&
1809894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        (!RetTy->isPointerTy() ||
1810894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         !cast<PointerType>(RetTy)->getElementType()->isFunctionTy())) {
1811894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      TypePrinter.print(RetTy, Out);
1812894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << ' ';
1813894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      writeOperand(Operand, false);
1814894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    } else {
1815894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      writeOperand(Operand, true);
1816894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
1817894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << '(';
1818894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (unsigned op = 0, Eop = CI->getNumArgOperands(); op < Eop; ++op) {
1819894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (op > 0)
1820894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Out << ", ";
1821894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      writeParamOperand(CI->getArgOperand(op), PAL.getParamAttributes(op + 1));
1822894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
1823894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << ')';
1824894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (PAL.getFnAttributes() != Attribute::None)
1825894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << ' ' << Attribute::getAsString(PAL.getFnAttributes());
182619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
182719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Operand = II->getCalledValue();
182819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    PointerType *PTy = cast<PointerType>(Operand->getType());
182919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
183019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Type *RetTy = FTy->getReturnType();
183119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    const AttrListPtr &PAL = II->getAttributes();
183219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
183319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Print the calling convention being used.
183419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    switch (II->getCallingConv()) {
183519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case CallingConv::C: break;   // default
183619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case CallingConv::Fast:  Out << " fastcc"; break;
183719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case CallingConv::Cold:  Out << " coldcc"; break;
183819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case CallingConv::X86_StdCall:  Out << " x86_stdcallcc"; break;
183919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case CallingConv::X86_FastCall: Out << " x86_fastcallcc"; break;
184019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case CallingConv::X86_ThisCall: Out << " x86_thiscallcc"; break;
184119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case CallingConv::ARM_APCS:     Out << " arm_apcscc "; break;
184219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case CallingConv::ARM_AAPCS:    Out << " arm_aapcscc "; break;
184319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case CallingConv::ARM_AAPCS_VFP:Out << " arm_aapcs_vfpcc "; break;
184419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case CallingConv::MSP430_INTR:  Out << " msp430_intrcc "; break;
184519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case CallingConv::PTX_Kernel:   Out << " ptx_kernel"; break;
184619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case CallingConv::PTX_Device:   Out << " ptx_device"; break;
184719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    default: Out << " cc" << II->getCallingConv(); break;
184819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
184919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
185019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (PAL.getRetAttributes() != Attribute::None)
185119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Out << ' ' << Attribute::getAsString(PAL.getRetAttributes());
185219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
185319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // If possible, print out the short form of the invoke instruction. We can
185419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // only do this if the first argument is a pointer to a nonvararg function,
185519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // and if the return type is not a pointer to a function.
185619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    //
185719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Out << ' ';
185819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (!FTy->isVarArg() &&
185919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        (!RetTy->isPointerTy() ||
186019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman         !cast<PointerType>(RetTy)->getElementType()->isFunctionTy())) {
186119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      TypePrinter.print(RetTy, Out);
186219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Out << ' ';
186319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      writeOperand(Operand, false);
186419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    } else {
186519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      writeOperand(Operand, true);
186619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
186719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Out << '(';
186819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    for (unsigned op = 0, Eop = II->getNumArgOperands(); op < Eop; ++op) {
186919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (op)
187019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        Out << ", ";
187119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      writeParamOperand(II->getArgOperand(op), PAL.getParamAttributes(op + 1));
187219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
187319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
187419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Out << ')';
187519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (PAL.getFnAttributes() != Attribute::None)
187619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Out << ' ' << Attribute::getAsString(PAL.getFnAttributes());
187719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
187819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Out << "\n          to ";
187919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    writeOperand(II->getNormalDest(), true);
188019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Out << " unwind ";
188119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    writeOperand(II->getUnwindDest(), true);
188219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1883894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (const AllocaInst *AI = dyn_cast<AllocaInst>(&I)) {
1884894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << ' ';
1885894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    TypePrinter.print(AI->getType()->getElementType(), Out);
1886894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!AI->getArraySize() || AI->isArrayAllocation()) {
1887894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << ", ";
1888894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      writeOperand(AI->getArraySize(), true);
1889894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
1890894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (AI->getAlignment()) {
1891894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << ", align " << AI->getAlignment();
1892894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
1893894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (isa<CastInst>(I)) {
1894894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Operand) {
1895894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << ' ';
1896894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      writeOperand(Operand, true);   // Work with broken code
1897894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
1898894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << " to ";
1899894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    TypePrinter.print(I.getType(), Out);
190019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  } else if (isa<VAArgInst>(I)) {
190119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (Operand) {
190219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Out << ' ';
190319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      writeOperand(Operand, true);   // Work with broken code
190419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
190519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Out << ", ";
190619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    TypePrinter.print(I.getType(), Out);
1907894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (Operand) {   // Print the normal way.
1908894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1909894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // PrintAllTypes - Instructions who have operands of all the same type
1910894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // omit the type from all but the first operand.  If the instruction has
1911894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // different type operands (for example br), then they are all printed.
1912894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    bool PrintAllTypes = false;
191319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Type *TheType = Operand->getType();
1914894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1915894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Select, Store and ShuffleVector always print all types.
1916894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (isa<SelectInst>(I) || isa<StoreInst>(I) || isa<ShuffleVectorInst>(I)
1917894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        || isa<ReturnInst>(I)) {
1918894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      PrintAllTypes = true;
1919894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    } else {
1920894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
1921894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Operand = I.getOperand(i);
1922894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // note that Operand shouldn't be null, but the test helps make dump()
1923894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // more tolerant of malformed IR
1924894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (Operand && Operand->getType() != TheType) {
1925894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          PrintAllTypes = true;    // We have differing types!  Print them all!
1926894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          break;
1927894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        }
1928894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
1929894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
1930894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1931894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!PrintAllTypes) {
1932894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << ' ';
1933894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      TypePrinter.print(TheType, Out);
1934894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
1935894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1936894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << ' ';
1937894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
1938894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (i) Out << ", ";
1939894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      writeOperand(I.getOperand(i), PrintAllTypes);
1940894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
1941894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1942894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
194319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Print atomic ordering/alignment for memory operations
194419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (const LoadInst *LI = dyn_cast<LoadInst>(&I)) {
194519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (LI->isAtomic())
194619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      writeAtomic(LI->getOrdering(), LI->getSynchScope());
194719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (LI->getAlignment())
194819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Out << ", align " << LI->getAlignment();
194919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  } else if (const StoreInst *SI = dyn_cast<StoreInst>(&I)) {
195019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (SI->isAtomic())
195119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      writeAtomic(SI->getOrdering(), SI->getSynchScope());
195219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (SI->getAlignment())
195319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Out << ", align " << SI->getAlignment();
195419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  } else if (const AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(&I)) {
195519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    writeAtomic(CXI->getOrdering(), CXI->getSynchScope());
195619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  } else if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(&I)) {
195719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    writeAtomic(RMWI->getOrdering(), RMWI->getSynchScope());
195819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  } else if (const FenceInst *FI = dyn_cast<FenceInst>(&I)) {
195919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    writeAtomic(FI->getOrdering(), FI->getSynchScope());
1960894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1961894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1962894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Print Metadata info.
1963894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SmallVector<std::pair<unsigned, MDNode*>, 4> InstMD;
1964894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  I.getAllMetadata(InstMD);
1965894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!InstMD.empty()) {
1966894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SmallVector<StringRef, 8> MDNames;
1967894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    I.getType()->getContext().getMDKindNames(MDNames);
1968894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (unsigned i = 0, e = InstMD.size(); i != e; ++i) {
1969894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      unsigned Kind = InstMD[i].first;
1970894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman       if (Kind < MDNames.size()) {
1971894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         Out << ", !" << MDNames[Kind];
1972894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      } else {
1973894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Out << ", !<unknown kind #" << Kind << ">";
1974894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
1975894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << ' ';
1976894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      WriteAsOperandInternal(Out, InstMD[i].second, &TypePrinter, &Machine,
1977894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                             TheModule);
1978894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
1979894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1980894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  printInfoComment(I);
1981894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1982894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1983894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic void WriteMDNodeComment(const MDNode *Node,
1984894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                               formatted_raw_ostream &Out) {
1985894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Node->getNumOperands() < 1)
1986894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return;
1987894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ConstantInt *CI = dyn_cast_or_null<ConstantInt>(Node->getOperand(0));
1988894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!CI) return;
1989894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  APInt Val = CI->getValue();
1990894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  APInt Tag = Val & ~APInt(Val.getBitWidth(), LLVMDebugVersionMask);
1991894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Val.ult(LLVMDebugVersion))
1992894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return;
199319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1994894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out.PadToColumn(50);
199519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Tag == dwarf::DW_TAG_user_base)
1996894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << "; [ DW_TAG_user_base ]";
1997894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  else if (Tag.isIntN(32)) {
1998894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (const char *TagName = dwarf::TagString(Tag.getZExtValue()))
1999894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Out << "; [ " << TagName << " ]";
2000894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
2001894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
2002894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2003894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid AssemblyWriter::writeAllMDNodes() {
2004894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SmallVector<const MDNode *, 16> Nodes;
2005894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Nodes.resize(Machine.mdn_size());
2006894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (SlotTracker::mdn_iterator I = Machine.mdn_begin(), E = Machine.mdn_end();
2007894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman       I != E; ++I)
2008894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Nodes[I->second] = cast<MDNode>(I->first);
200919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
2010894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
2011894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Out << '!' << i << " = metadata ";
2012894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    printMDNodeBody(Nodes[i]);
2013894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
2014894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
2015894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2016894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid AssemblyWriter::printMDNodeBody(const MDNode *Node) {
2017894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  WriteMDNodeBodyInternal(Out, Node, &TypePrinter, &Machine, TheModule);
2018894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  WriteMDNodeComment(Node, Out);
2019894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Out << "\n";
2020894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
2021894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2022894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
2023894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//                       External Interface declarations
2024894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
2025894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2026894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid Module::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW) const {
2027894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SlotTracker SlotTable(this);
2028894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  formatted_raw_ostream OS(ROS);
2029894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  AssemblyWriter W(OS, SlotTable, this, AAW);
2030894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  W.printModule(this);
2031894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
2032894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2033894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid NamedMDNode::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW) const {
2034894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SlotTracker SlotTable(getParent());
2035894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  formatted_raw_ostream OS(ROS);
2036894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  AssemblyWriter W(OS, SlotTable, getParent(), AAW);
2037894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  W.printNamedMDNode(this);
2038894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
2039894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2040894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid Type::print(raw_ostream &OS) const {
204119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  TypePrinting TP;
204219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  TP.print(const_cast<Type*>(this), OS);
204319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
204419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // If the type is a named struct type, print the body as well.
204519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (StructType *STy = dyn_cast<StructType>(const_cast<Type*>(this)))
204619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (!STy->isLiteral()) {
204719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      OS << " = type ";
204819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      TP.printStructBody(STy, OS);
204919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
2050894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
2051894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2052894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid Value::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW) const {
2053894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  formatted_raw_ostream OS(ROS);
2054894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (const Instruction *I = dyn_cast<Instruction>(this)) {
2055894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    const Function *F = I->getParent() ? I->getParent()->getParent() : 0;
2056894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SlotTracker SlotTable(F);
2057894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    AssemblyWriter W(OS, SlotTable, getModuleFromVal(I), AAW);
2058894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    W.printInstruction(*I);
2059894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(this)) {
2060894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SlotTracker SlotTable(BB->getParent());
2061894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    AssemblyWriter W(OS, SlotTable, getModuleFromVal(BB), AAW);
2062894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    W.printBasicBlock(BB);
2063894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(this)) {
2064894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SlotTracker SlotTable(GV->getParent());
2065894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    AssemblyWriter W(OS, SlotTable, GV->getParent(), AAW);
2066894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (const GlobalVariable *V = dyn_cast<GlobalVariable>(GV))
2067894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      W.printGlobal(V);
2068894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else if (const Function *F = dyn_cast<Function>(GV))
2069894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      W.printFunction(F);
2070894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else
2071894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      W.printAlias(cast<GlobalAlias>(GV));
2072894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (const MDNode *N = dyn_cast<MDNode>(this)) {
2073894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    const Function *F = N->getFunction();
2074894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SlotTracker SlotTable(F);
2075894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    AssemblyWriter W(OS, SlotTable, F ? F->getParent() : 0, AAW);
2076894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    W.printMDNodeBody(N);
2077894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (const Constant *C = dyn_cast<Constant>(this)) {
2078894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    TypePrinting TypePrinter;
2079894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    TypePrinter.print(C->getType(), OS);
2080894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    OS << ' ';
2081894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    WriteConstantInternal(OS, C, TypePrinter, 0, 0);
208219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  } else if (isa<InlineAsm>(this) || isa<MDString>(this) ||
2083894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman             isa<Argument>(this)) {
2084894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    WriteAsOperand(OS, this, true, 0);
2085894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else {
2086894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Otherwise we don't know what it is. Call the virtual function to
2087894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // allow a subclass to print itself.
2088894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    printCustom(OS);
2089894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
2090894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
2091894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2092894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Value::printCustom - subclasses should override this to implement printing.
2093894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid Value::printCustom(raw_ostream &OS) const {
2094894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  llvm_unreachable("Unknown value to print out!");
2095894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
2096894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2097894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Value::dump - allow easy printing of Values from the debugger.
2098894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid Value::dump() const { print(dbgs()); dbgs() << '\n'; }
2099894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2100894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Type::dump - allow easy printing of Types from the debugger.
210119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanvoid Type::dump() const { print(dbgs()); }
2102894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
2103894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Module::dump() - Allow printing of Modules from the debugger.
2104894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid Module::dump() const { print(dbgs(), 0); }
2105