Chris Lattner | 8da78af | 2002-04-07 22:31:46 +0000 | [diff] [blame] | 1 | //===-- AsmWriter.cpp - Printing LLVM as an assembly file -----------------===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 9 | // |
| 10 | // This library implements the functionality defined in llvm/Assembly/Writer.h |
| 11 | // |
Chris Lattner | 02b9399 | 2002-04-12 18:21:53 +0000 | [diff] [blame] | 12 | // Note that these routines must be extremely tolerant of various errors in the |
Chris Lattner | 8f77dae | 2003-05-08 02:44:12 +0000 | [diff] [blame] | 13 | // LLVM code, because it can be used for debugging transformations. |
Chris Lattner | 02b9399 | 2002-04-12 18:21:53 +0000 | [diff] [blame] | 14 | // |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 15 | //===----------------------------------------------------------------------===// |
| 16 | |
Chris Lattner | 75cf7cf | 2002-04-08 22:03:40 +0000 | [diff] [blame] | 17 | #include "llvm/Assembly/Writer.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/DenseMap.h" |
| 19 | #include "llvm/ADT/STLExtras.h" |
| 20 | #include "llvm/ADT/SmallString.h" |
| 21 | #include "llvm/ADT/StringExtras.h" |
Chris Lattner | 1dbb387 | 2010-09-02 23:09:42 +0000 | [diff] [blame] | 22 | #include "llvm/Assembly/AssemblyAnnotationWriter.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 23 | #include "llvm/Assembly/PrintModulePass.h" |
Bill Wendling | 58a6cf2 | 2012-06-28 00:41:44 +0000 | [diff] [blame] | 24 | #include "llvm/DebugInfo.h" |
Daniel Malea | d0fef32 | 2013-05-08 20:38:31 +0000 | [diff] [blame^] | 25 | #include "llvm/IR/AsmWriter.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 26 | #include "llvm/IR/CallingConv.h" |
| 27 | #include "llvm/IR/Constants.h" |
| 28 | #include "llvm/IR/DerivedTypes.h" |
| 29 | #include "llvm/IR/InlineAsm.h" |
| 30 | #include "llvm/IR/IntrinsicInst.h" |
| 31 | #include "llvm/IR/LLVMContext.h" |
| 32 | #include "llvm/IR/Module.h" |
| 33 | #include "llvm/IR/Operator.h" |
Chandler Carruth | 4068e1a | 2013-01-07 15:43:51 +0000 | [diff] [blame] | 34 | #include "llvm/IR/TypeFinder.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 35 | #include "llvm/IR/ValueSymbolTable.h" |
Bill Wendling | 8f48766 | 2006-11-28 02:09:03 +0000 | [diff] [blame] | 36 | #include "llvm/Support/CFG.h" |
David Greene | d865e02 | 2010-01-05 01:29:26 +0000 | [diff] [blame] | 37 | #include "llvm/Support/Debug.h" |
Devang Patel | 2d5988d | 2009-09-30 20:16:54 +0000 | [diff] [blame] | 38 | #include "llvm/Support/Dwarf.h" |
Torok Edwin | ab7c09b | 2009-07-08 18:01:40 +0000 | [diff] [blame] | 39 | #include "llvm/Support/ErrorHandling.h" |
Dan Gohman | 683e922 | 2009-08-12 17:23:50 +0000 | [diff] [blame] | 40 | #include "llvm/Support/FormattedStream.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 41 | #include "llvm/Support/MathExtras.h" |
Chris Lattner | 007377f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 42 | #include <algorithm> |
Reid Spencer | 4ad513c | 2007-05-22 19:27:35 +0000 | [diff] [blame] | 43 | #include <cctype> |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 44 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 45 | |
Reid Spencer | edd5d9e | 2005-05-15 16:13:11 +0000 | [diff] [blame] | 46 | // Make virtual table appear in this compilation unit. |
| 47 | AssemblyAnnotationWriter::~AssemblyAnnotationWriter() {} |
| 48 | |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 49 | //===----------------------------------------------------------------------===// |
| 50 | // Helper Functions |
| 51 | //===----------------------------------------------------------------------===// |
| 52 | |
| 53 | static const Module *getModuleFromVal(const Value *V) { |
| 54 | if (const Argument *MA = dyn_cast<Argument>(V)) |
| 55 | return MA->getParent() ? MA->getParent()->getParent() : 0; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 56 | |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 57 | if (const BasicBlock *BB = dyn_cast<BasicBlock>(V)) |
| 58 | return BB->getParent() ? BB->getParent()->getParent() : 0; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 59 | |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 60 | if (const Instruction *I = dyn_cast<Instruction>(V)) { |
| 61 | const Function *M = I->getParent() ? I->getParent()->getParent() : 0; |
| 62 | return M ? M->getParent() : 0; |
| 63 | } |
Andrew Trick | 18801ec | 2011-09-30 19:48:58 +0000 | [diff] [blame] | 64 | |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 65 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) |
| 66 | return GV->getParent(); |
| 67 | return 0; |
| 68 | } |
| 69 | |
Bill Wendling | 7ab6c76 | 2013-02-20 07:21:42 +0000 | [diff] [blame] | 70 | static void PrintCallingConv(unsigned cc, raw_ostream &Out) { |
Micah Villmow | d3766df | 2012-09-13 15:11:12 +0000 | [diff] [blame] | 71 | switch (cc) { |
Bill Wendling | 7ab6c76 | 2013-02-20 07:21:42 +0000 | [diff] [blame] | 72 | default: Out << "cc" << cc; break; |
| 73 | case CallingConv::Fast: Out << "fastcc"; break; |
| 74 | case CallingConv::Cold: Out << "coldcc"; break; |
| 75 | case CallingConv::X86_StdCall: Out << "x86_stdcallcc"; break; |
| 76 | case CallingConv::X86_FastCall: Out << "x86_fastcallcc"; break; |
| 77 | case CallingConv::X86_ThisCall: Out << "x86_thiscallcc"; break; |
| 78 | case CallingConv::Intel_OCL_BI: Out << "intel_ocl_bicc"; break; |
| 79 | case CallingConv::ARM_APCS: Out << "arm_apcscc"; break; |
| 80 | case CallingConv::ARM_AAPCS: Out << "arm_aapcscc"; break; |
| 81 | case CallingConv::ARM_AAPCS_VFP: Out << "arm_aapcs_vfpcc"; break; |
| 82 | case CallingConv::MSP430_INTR: Out << "msp430_intrcc"; break; |
| 83 | case CallingConv::PTX_Kernel: Out << "ptx_kernel"; break; |
| 84 | case CallingConv::PTX_Device: Out << "ptx_device"; break; |
Micah Villmow | d3766df | 2012-09-13 15:11:12 +0000 | [diff] [blame] | 85 | } |
| 86 | } |
Michael Ilseman | 407a616 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 87 | |
Daniel Dunbar | e9da133 | 2008-10-28 19:33:02 +0000 | [diff] [blame] | 88 | // PrintEscapedString - Print each character of the specified string, escaping |
| 89 | // it if it is not printable or if it is an escape char. |
Chris Lattner | 8fff126 | 2010-07-07 23:16:37 +0000 | [diff] [blame] | 90 | static void PrintEscapedString(StringRef Name, raw_ostream &Out) { |
Daniel Dunbar | 03d7651 | 2009-07-25 23:55:21 +0000 | [diff] [blame] | 91 | for (unsigned i = 0, e = Name.size(); i != e; ++i) { |
| 92 | unsigned char C = Name[i]; |
Nick Lewycky | 34a4086 | 2009-03-15 06:39:52 +0000 | [diff] [blame] | 93 | if (isprint(C) && C != '\\' && C != '"') |
Daniel Dunbar | e9da133 | 2008-10-28 19:33:02 +0000 | [diff] [blame] | 94 | Out << C; |
| 95 | else |
| 96 | Out << '\\' << hexdigit(C >> 4) << hexdigit(C & 0x0F); |
| 97 | } |
| 98 | } |
| 99 | |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 100 | enum PrefixType { |
| 101 | GlobalPrefix, |
| 102 | LabelPrefix, |
Daniel Dunbar | cad3580 | 2008-10-14 23:28:09 +0000 | [diff] [blame] | 103 | LocalPrefix, |
| 104 | NoPrefix |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | /// PrintLLVMName - Turn the specified name into an 'LLVM name', which is either |
| 108 | /// prefixed with % (if the string only contains simple characters) or is |
| 109 | /// surrounded with ""'s (if it has special chars in it). Print it out. |
Benjamin Kramer | 38e5989 | 2010-07-14 22:38:02 +0000 | [diff] [blame] | 110 | static void PrintLLVMName(raw_ostream &OS, StringRef Name, PrefixType Prefix) { |
Jay Foad | 61717b3 | 2011-04-24 14:30:00 +0000 | [diff] [blame] | 111 | assert(!Name.empty() && "Cannot get empty name!"); |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 112 | switch (Prefix) { |
Daniel Dunbar | cad3580 | 2008-10-14 23:28:09 +0000 | [diff] [blame] | 113 | case NoPrefix: break; |
Chris Lattner | 52b26de | 2008-08-19 05:16:28 +0000 | [diff] [blame] | 114 | case GlobalPrefix: OS << '@'; break; |
| 115 | case LabelPrefix: break; |
| 116 | case LocalPrefix: OS << '%'; break; |
Nick Lewycky | 0423483 | 2009-03-19 06:31:22 +0000 | [diff] [blame] | 117 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 118 | |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 119 | // Scan the name to see if it needs quotes first. |
Guy Benyei | 87d0b9e | 2013-02-12 21:21:59 +0000 | [diff] [blame] | 120 | bool NeedsQuotes = isdigit(static_cast<unsigned char>(Name[0])); |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 121 | if (!NeedsQuotes) { |
Daniel Dunbar | 03d7651 | 2009-07-25 23:55:21 +0000 | [diff] [blame] | 122 | for (unsigned i = 0, e = Name.size(); i != e; ++i) { |
Aaron Ballman | 09dab82 | 2012-07-16 16:18:18 +0000 | [diff] [blame] | 123 | // By making this unsigned, the value passed in to isalnum will always be |
| 124 | // in the range 0-255. This is important when building with MSVC because |
| 125 | // its implementation will assert. This situation can arise when dealing |
| 126 | // with UTF-8 multibyte characters. |
| 127 | unsigned char C = Name[i]; |
Guy Benyei | 87d0b9e | 2013-02-12 21:21:59 +0000 | [diff] [blame] | 128 | if (!isalnum(static_cast<unsigned char>(C)) && C != '-' && C != '.' && |
| 129 | C != '_') { |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 130 | NeedsQuotes = true; |
| 131 | break; |
| 132 | } |
| 133 | } |
| 134 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 135 | |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 136 | // If we didn't need any quotes, just write out the name in one blast. |
| 137 | if (!NeedsQuotes) { |
Daniel Dunbar | 03d7651 | 2009-07-25 23:55:21 +0000 | [diff] [blame] | 138 | OS << Name; |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 139 | return; |
| 140 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 141 | |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 142 | // Okay, we need quotes. Output the quotes and escape any scary characters as |
| 143 | // needed. |
| 144 | OS << '"'; |
Daniel Dunbar | 03d7651 | 2009-07-25 23:55:21 +0000 | [diff] [blame] | 145 | PrintEscapedString(Name, OS); |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 146 | OS << '"'; |
| 147 | } |
| 148 | |
| 149 | /// PrintLLVMName - Turn the specified name into an 'LLVM name', which is either |
| 150 | /// prefixed with % (if the string only contains simple characters) or is |
| 151 | /// surrounded with ""'s (if it has special chars in it). Print it out. |
Dan Gohman | 1220e10 | 2009-08-12 20:56:03 +0000 | [diff] [blame] | 152 | static void PrintLLVMName(raw_ostream &OS, const Value *V) { |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 153 | PrintLLVMName(OS, V->getName(), |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 154 | isa<GlobalValue>(V) ? GlobalPrefix : LocalPrefix); |
| 155 | } |
| 156 | |
Chris Lattner | 9cc3446 | 2009-02-28 20:25:14 +0000 | [diff] [blame] | 157 | |
Daniel Malea | d0fef32 | 2013-05-08 20:38:31 +0000 | [diff] [blame^] | 158 | namespace llvm { |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 159 | |
| 160 | void TypePrinting::incorporateTypes(const Module &M) { |
Bill Wendling | 573e973 | 2012-08-03 00:30:35 +0000 | [diff] [blame] | 161 | NamedTypes.run(M, false); |
Andrew Trick | 18801ec | 2011-09-30 19:48:58 +0000 | [diff] [blame] | 162 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 163 | // The list of struct types we got back includes all the struct types, split |
| 164 | // the unnamed ones out to a numbering and remove the anonymous structs. |
| 165 | unsigned NextNumber = 0; |
Andrew Trick | 18801ec | 2011-09-30 19:48:58 +0000 | [diff] [blame] | 166 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 167 | std::vector<StructType*>::iterator NextToUse = NamedTypes.begin(), I, E; |
| 168 | for (I = NamedTypes.begin(), E = NamedTypes.end(); I != E; ++I) { |
| 169 | StructType *STy = *I; |
Andrew Trick | 18801ec | 2011-09-30 19:48:58 +0000 | [diff] [blame] | 170 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 171 | // Ignore anonymous types. |
Chris Lattner | c4d0e9f | 2011-08-12 18:07:07 +0000 | [diff] [blame] | 172 | if (STy->isLiteral()) |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 173 | continue; |
Andrew Trick | 18801ec | 2011-09-30 19:48:58 +0000 | [diff] [blame] | 174 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 175 | if (STy->getName().empty()) |
| 176 | NumberedTypes[STy] = NextNumber++; |
| 177 | else |
| 178 | *NextToUse++ = STy; |
| 179 | } |
Andrew Trick | 18801ec | 2011-09-30 19:48:58 +0000 | [diff] [blame] | 180 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 181 | NamedTypes.erase(NextToUse, NamedTypes.end()); |
| 182 | } |
| 183 | |
| 184 | |
Chris Lattner | 534361e | 2009-02-28 20:49:40 +0000 | [diff] [blame] | 185 | /// CalcTypeName - Write the specified type to the specified raw_ostream, making |
| 186 | /// use of type names or up references to shorten the type name where possible. |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 187 | void TypePrinting::print(Type *Ty, raw_ostream &OS) { |
Chris Lattner | 9cc3446 | 2009-02-28 20:25:14 +0000 | [diff] [blame] | 188 | switch (Ty->getTypeID()) { |
Chris Lattner | 3079426 | 2009-02-28 21:27:31 +0000 | [diff] [blame] | 189 | case Type::VoidTyID: OS << "void"; break; |
Dan Gohman | ce16339 | 2011-12-17 00:04:22 +0000 | [diff] [blame] | 190 | case Type::HalfTyID: OS << "half"; break; |
Chris Lattner | 3079426 | 2009-02-28 21:27:31 +0000 | [diff] [blame] | 191 | case Type::FloatTyID: OS << "float"; break; |
| 192 | case Type::DoubleTyID: OS << "double"; break; |
| 193 | case Type::X86_FP80TyID: OS << "x86_fp80"; break; |
| 194 | case Type::FP128TyID: OS << "fp128"; break; |
| 195 | case Type::PPC_FP128TyID: OS << "ppc_fp128"; break; |
| 196 | case Type::LabelTyID: OS << "label"; break; |
Nick Lewycky | 7a0370f | 2009-05-30 05:06:04 +0000 | [diff] [blame] | 197 | case Type::MetadataTyID: OS << "metadata"; break; |
Dale Johannesen | bb811a2 | 2010-09-10 20:55:01 +0000 | [diff] [blame] | 198 | case Type::X86_MMXTyID: OS << "x86_mmx"; break; |
Chris Lattner | 583ffd8 | 2009-02-28 21:18:43 +0000 | [diff] [blame] | 199 | case Type::IntegerTyID: |
Chris Lattner | 3079426 | 2009-02-28 21:27:31 +0000 | [diff] [blame] | 200 | OS << 'i' << cast<IntegerType>(Ty)->getBitWidth(); |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 201 | return; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 202 | |
Chris Lattner | 36942d7 | 2009-02-28 20:35:42 +0000 | [diff] [blame] | 203 | case Type::FunctionTyID: { |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 204 | FunctionType *FTy = cast<FunctionType>(Ty); |
| 205 | print(FTy->getReturnType(), OS); |
Chris Lattner | 3079426 | 2009-02-28 21:27:31 +0000 | [diff] [blame] | 206 | OS << " ("; |
Chris Lattner | 36942d7 | 2009-02-28 20:35:42 +0000 | [diff] [blame] | 207 | for (FunctionType::param_iterator I = FTy->param_begin(), |
| 208 | E = FTy->param_end(); I != E; ++I) { |
| 209 | if (I != FTy->param_begin()) |
Chris Lattner | 3079426 | 2009-02-28 21:27:31 +0000 | [diff] [blame] | 210 | OS << ", "; |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 211 | print(*I, OS); |
Chris Lattner | 9cc3446 | 2009-02-28 20:25:14 +0000 | [diff] [blame] | 212 | } |
Chris Lattner | 36942d7 | 2009-02-28 20:35:42 +0000 | [diff] [blame] | 213 | if (FTy->isVarArg()) { |
Chris Lattner | 3079426 | 2009-02-28 21:27:31 +0000 | [diff] [blame] | 214 | if (FTy->getNumParams()) OS << ", "; |
| 215 | OS << "..."; |
Chris Lattner | 9cc3446 | 2009-02-28 20:25:14 +0000 | [diff] [blame] | 216 | } |
Chris Lattner | 3079426 | 2009-02-28 21:27:31 +0000 | [diff] [blame] | 217 | OS << ')'; |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 218 | return; |
Chris Lattner | 36942d7 | 2009-02-28 20:35:42 +0000 | [diff] [blame] | 219 | } |
| 220 | case Type::StructTyID: { |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 221 | StructType *STy = cast<StructType>(Ty); |
Andrew Trick | 18801ec | 2011-09-30 19:48:58 +0000 | [diff] [blame] | 222 | |
Chris Lattner | c4d0e9f | 2011-08-12 18:07:07 +0000 | [diff] [blame] | 223 | if (STy->isLiteral()) |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 224 | return printStructBody(STy, OS); |
| 225 | |
| 226 | if (!STy->getName().empty()) |
| 227 | return PrintLLVMName(OS, STy->getName(), LocalPrefix); |
Andrew Trick | 18801ec | 2011-09-30 19:48:58 +0000 | [diff] [blame] | 228 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 229 | DenseMap<StructType*, unsigned>::iterator I = NumberedTypes.find(STy); |
| 230 | if (I != NumberedTypes.end()) |
| 231 | OS << '%' << I->second; |
| 232 | else // Not enumerated, print the hex address. |
Benjamin Kramer | 5a83264 | 2011-11-02 17:24:36 +0000 | [diff] [blame] | 233 | OS << "%\"type " << STy << '\"'; |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 234 | return; |
Chris Lattner | 36942d7 | 2009-02-28 20:35:42 +0000 | [diff] [blame] | 235 | } |
| 236 | case Type::PointerTyID: { |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 237 | PointerType *PTy = cast<PointerType>(Ty); |
| 238 | print(PTy->getElementType(), OS); |
Chris Lattner | 36942d7 | 2009-02-28 20:35:42 +0000 | [diff] [blame] | 239 | if (unsigned AddressSpace = PTy->getAddressSpace()) |
Chris Lattner | 3079426 | 2009-02-28 21:27:31 +0000 | [diff] [blame] | 240 | OS << " addrspace(" << AddressSpace << ')'; |
| 241 | OS << '*'; |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 242 | return; |
Chris Lattner | 36942d7 | 2009-02-28 20:35:42 +0000 | [diff] [blame] | 243 | } |
| 244 | case Type::ArrayTyID: { |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 245 | ArrayType *ATy = cast<ArrayType>(Ty); |
Chris Lattner | 3079426 | 2009-02-28 21:27:31 +0000 | [diff] [blame] | 246 | OS << '[' << ATy->getNumElements() << " x "; |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 247 | print(ATy->getElementType(), OS); |
Chris Lattner | 3079426 | 2009-02-28 21:27:31 +0000 | [diff] [blame] | 248 | OS << ']'; |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 249 | return; |
Chris Lattner | 36942d7 | 2009-02-28 20:35:42 +0000 | [diff] [blame] | 250 | } |
| 251 | case Type::VectorTyID: { |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 252 | VectorType *PTy = cast<VectorType>(Ty); |
Chris Lattner | 3079426 | 2009-02-28 21:27:31 +0000 | [diff] [blame] | 253 | OS << "<" << PTy->getNumElements() << " x "; |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 254 | print(PTy->getElementType(), OS); |
Chris Lattner | 3079426 | 2009-02-28 21:27:31 +0000 | [diff] [blame] | 255 | OS << '>'; |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 256 | return; |
Chris Lattner | 36942d7 | 2009-02-28 20:35:42 +0000 | [diff] [blame] | 257 | } |
Chris Lattner | 36942d7 | 2009-02-28 20:35:42 +0000 | [diff] [blame] | 258 | default: |
Chris Lattner | 3079426 | 2009-02-28 21:27:31 +0000 | [diff] [blame] | 259 | OS << "<unrecognized-type>"; |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 260 | return; |
Chris Lattner | 9cc3446 | 2009-02-28 20:25:14 +0000 | [diff] [blame] | 261 | } |
Chris Lattner | 9cc3446 | 2009-02-28 20:25:14 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 264 | void TypePrinting::printStructBody(StructType *STy, raw_ostream &OS) { |
| 265 | if (STy->isOpaque()) { |
| 266 | OS << "opaque"; |
| 267 | return; |
Chris Lattner | 9cc3446 | 2009-02-28 20:25:14 +0000 | [diff] [blame] | 268 | } |
Andrew Trick | 18801ec | 2011-09-30 19:48:58 +0000 | [diff] [blame] | 269 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 270 | if (STy->isPacked()) |
| 271 | OS << '<'; |
Andrew Trick | 18801ec | 2011-09-30 19:48:58 +0000 | [diff] [blame] | 272 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 273 | if (STy->getNumElements() == 0) { |
| 274 | OS << "{}"; |
| 275 | } else { |
| 276 | StructType::element_iterator I = STy->element_begin(); |
| 277 | OS << "{ "; |
| 278 | print(*I++, OS); |
| 279 | for (StructType::element_iterator E = STy->element_end(); I != E; ++I) { |
| 280 | OS << ", "; |
| 281 | print(*I, OS); |
Chris Lattner | 413fd23 | 2009-03-01 00:03:38 +0000 | [diff] [blame] | 282 | } |
Andrew Trick | 18801ec | 2011-09-30 19:48:58 +0000 | [diff] [blame] | 283 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 284 | OS << " }"; |
Chris Lattner | e9fa33e | 2009-02-28 23:20:19 +0000 | [diff] [blame] | 285 | } |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 286 | if (STy->isPacked()) |
| 287 | OS << '>'; |
Chris Lattner | e9fa33e | 2009-02-28 23:20:19 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 290 | //===----------------------------------------------------------------------===// |
| 291 | // SlotTracker Class: Enumerate slot numbers for unnamed values |
| 292 | //===----------------------------------------------------------------------===// |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 293 | /// This class provides computation of slot numbers for LLVM Assembly writing. |
Chris Lattner | 45d4c73 | 2008-08-17 04:17:45 +0000 | [diff] [blame] | 294 | /// |
Chris Lattner | 0d9574a | 2008-08-19 04:26:57 +0000 | [diff] [blame] | 295 | class SlotTracker { |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 296 | public: |
Devang Patel | 320671d | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 297 | /// ValueMap - A mapping of Values to slot numbers. |
Chris Lattner | 9225507 | 2008-08-17 17:25:25 +0000 | [diff] [blame] | 298 | typedef DenseMap<const Value*, unsigned> ValueMap; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 299 | |
| 300 | private: |
Devang Patel | 320671d | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 301 | /// TheModule - The module for which we are holding slot numbers. |
Chris Lattner | 45d4c73 | 2008-08-17 04:17:45 +0000 | [diff] [blame] | 302 | const Module* TheModule; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 303 | |
Devang Patel | 320671d | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 304 | /// TheFunction - The function for which we are holding slot numbers. |
Chris Lattner | 45d4c73 | 2008-08-17 04:17:45 +0000 | [diff] [blame] | 305 | const Function* TheFunction; |
| 306 | bool FunctionProcessed; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 307 | |
Jay Foad | 7f0ce34 | 2011-07-11 07:28:49 +0000 | [diff] [blame] | 308 | /// mMap - The slot map for the module level data. |
Chris Lattner | 45d4c73 | 2008-08-17 04:17:45 +0000 | [diff] [blame] | 309 | ValueMap mMap; |
| 310 | unsigned mNext; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 311 | |
Jay Foad | 7f0ce34 | 2011-07-11 07:28:49 +0000 | [diff] [blame] | 312 | /// fMap - The slot map for the function level data. |
Chris Lattner | 45d4c73 | 2008-08-17 04:17:45 +0000 | [diff] [blame] | 313 | ValueMap fMap; |
| 314 | unsigned fNext; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 315 | |
Devang Patel | 320671d | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 316 | /// mdnMap - Map for MDNodes. |
Chris Lattner | 307c989 | 2009-12-31 02:20:11 +0000 | [diff] [blame] | 317 | DenseMap<const MDNode*, unsigned> mdnMap; |
Devang Patel | 320671d | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 318 | unsigned mdnNext; |
Bill Wendling | b29ce26 | 2013-02-11 08:43:33 +0000 | [diff] [blame] | 319 | |
| 320 | /// asMap - The slot map for attribute sets. |
| 321 | DenseMap<AttributeSet, unsigned> asMap; |
| 322 | unsigned asNext; |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 323 | public: |
Chris Lattner | 45d4c73 | 2008-08-17 04:17:45 +0000 | [diff] [blame] | 324 | /// Construct from a module |
Chris Lattner | 0d9574a | 2008-08-19 04:26:57 +0000 | [diff] [blame] | 325 | explicit SlotTracker(const Module *M); |
Chris Lattner | 45d4c73 | 2008-08-17 04:17:45 +0000 | [diff] [blame] | 326 | /// Construct from a function, starting out in incorp state. |
Chris Lattner | 0d9574a | 2008-08-19 04:26:57 +0000 | [diff] [blame] | 327 | explicit SlotTracker(const Function *F); |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 328 | |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 329 | /// Return the slot number of the specified value in it's type |
Chris Lattner | 0d9574a | 2008-08-19 04:26:57 +0000 | [diff] [blame] | 330 | /// plane. If something is not in the SlotTracker, return -1. |
Chris Lattner | 22379bc | 2007-01-11 03:54:27 +0000 | [diff] [blame] | 331 | int getLocalSlot(const Value *V); |
| 332 | int getGlobalSlot(const GlobalValue *V); |
Devang Patel | 320671d | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 333 | int getMetadataSlot(const MDNode *N); |
Bill Wendling | b29ce26 | 2013-02-11 08:43:33 +0000 | [diff] [blame] | 334 | int getAttributeGroupSlot(AttributeSet AS); |
Reid Spencer | fc621e2 | 2004-06-09 15:26:53 +0000 | [diff] [blame] | 335 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 336 | /// If you'd like to deal with a function instead of just a module, use |
Chris Lattner | 0d9574a | 2008-08-19 04:26:57 +0000 | [diff] [blame] | 337 | /// this method to get its data into the SlotTracker. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 338 | void incorporateFunction(const Function *F) { |
| 339 | TheFunction = F; |
Reid Spencer | 28531c7 | 2004-08-16 07:46:33 +0000 | [diff] [blame] | 340 | FunctionProcessed = false; |
| 341 | } |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 342 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 343 | /// After calling incorporateFunction, use this method to remove the |
Chris Lattner | 0d9574a | 2008-08-19 04:26:57 +0000 | [diff] [blame] | 344 | /// most recently incorporated function from the SlotTracker. This |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 345 | /// will reset the state of the machine back to just the module contents. |
| 346 | void purgeFunction(); |
| 347 | |
Devang Patel | 320671d | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 348 | /// MDNode map iterators. |
Chris Lattner | 307c989 | 2009-12-31 02:20:11 +0000 | [diff] [blame] | 349 | typedef DenseMap<const MDNode*, unsigned>::iterator mdn_iterator; |
| 350 | mdn_iterator mdn_begin() { return mdnMap.begin(); } |
| 351 | mdn_iterator mdn_end() { return mdnMap.end(); } |
| 352 | unsigned mdn_size() const { return mdnMap.size(); } |
| 353 | bool mdn_empty() const { return mdnMap.empty(); } |
Devang Patel | 320671d | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 354 | |
Bill Wendling | b29ce26 | 2013-02-11 08:43:33 +0000 | [diff] [blame] | 355 | /// AttributeSet map iterators. |
| 356 | typedef DenseMap<AttributeSet, unsigned>::iterator as_iterator; |
| 357 | as_iterator as_begin() { return asMap.begin(); } |
| 358 | as_iterator as_end() { return asMap.end(); } |
| 359 | unsigned as_size() const { return asMap.size(); } |
| 360 | bool as_empty() const { return asMap.empty(); } |
| 361 | |
Reid Spencer | b03de0c | 2004-05-26 21:56:09 +0000 | [diff] [blame] | 362 | /// This function does the actual initialization. |
| 363 | inline void initialize(); |
| 364 | |
Devang Patel | 320671d | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 365 | // Implementation Details |
| 366 | private: |
Chris Lattner | 9446bbe | 2007-01-09 07:55:49 +0000 | [diff] [blame] | 367 | /// CreateModuleSlot - Insert the specified GlobalValue* into the slot table. |
| 368 | void CreateModuleSlot(const GlobalValue *V); |
Devang Patel | 320671d | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 369 | |
| 370 | /// CreateMetadataSlot - Insert the specified MDNode* into the slot table. |
| 371 | void CreateMetadataSlot(const MDNode *N); |
| 372 | |
Chris Lattner | 9446bbe | 2007-01-09 07:55:49 +0000 | [diff] [blame] | 373 | /// CreateFunctionSlot - Insert the specified Value* into the slot table. |
| 374 | void CreateFunctionSlot(const Value *V); |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 375 | |
Bill Wendling | b29ce26 | 2013-02-11 08:43:33 +0000 | [diff] [blame] | 376 | /// \brief Insert the specified AttributeSet into the slot table. |
| 377 | void CreateAttributeSetSlot(AttributeSet AS); |
| 378 | |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 379 | /// Add all of the module level global variables (and their initializers) |
| 380 | /// and function declarations, but not the contents of those functions. |
| 381 | void processModule(); |
| 382 | |
Devang Patel | 320671d | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 383 | /// Add all of the functions arguments, basic blocks, and instructions. |
Reid Spencer | b03de0c | 2004-05-26 21:56:09 +0000 | [diff] [blame] | 384 | void processFunction(); |
| 385 | |
Craig Topper | 86a1c32 | 2012-09-15 17:09:36 +0000 | [diff] [blame] | 386 | SlotTracker(const SlotTracker &) LLVM_DELETED_FUNCTION; |
| 387 | void operator=(const SlotTracker &) LLVM_DELETED_FUNCTION; |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 388 | }; |
| 389 | |
Daniel Malea | d0fef32 | 2013-05-08 20:38:31 +0000 | [diff] [blame^] | 390 | SlotTracker *createSlotTracker(const Module *M) { |
| 391 | return new SlotTracker(M); |
| 392 | } |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 393 | |
| 394 | static SlotTracker *createSlotTracker(const Value *V) { |
| 395 | if (const Argument *FA = dyn_cast<Argument>(V)) |
| 396 | return new SlotTracker(FA->getParent()); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 397 | |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 398 | if (const Instruction *I = dyn_cast<Instruction>(V)) |
Andrew Trick | 62e0590 | 2011-09-30 19:50:40 +0000 | [diff] [blame] | 399 | if (I->getParent()) |
| 400 | return new SlotTracker(I->getParent()->getParent()); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 401 | |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 402 | if (const BasicBlock *BB = dyn_cast<BasicBlock>(V)) |
| 403 | return new SlotTracker(BB->getParent()); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 404 | |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 405 | if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) |
| 406 | return new SlotTracker(GV->getParent()); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 407 | |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 408 | if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 409 | return new SlotTracker(GA->getParent()); |
| 410 | |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 411 | if (const Function *Func = dyn_cast<Function>(V)) |
| 412 | return new SlotTracker(Func); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 413 | |
Dan Gohman | 3bdfbf5 | 2010-07-20 23:55:01 +0000 | [diff] [blame] | 414 | if (const MDNode *MD = dyn_cast<MDNode>(V)) { |
| 415 | if (!MD->isFunctionLocal()) |
| 416 | return new SlotTracker(MD->getFunction()); |
| 417 | |
Dale Johannesen | 5f72a5e | 2010-01-13 00:00:24 +0000 | [diff] [blame] | 418 | return new SlotTracker((Function *)0); |
Dan Gohman | 3bdfbf5 | 2010-07-20 23:55:01 +0000 | [diff] [blame] | 419 | } |
Dale Johannesen | 5f72a5e | 2010-01-13 00:00:24 +0000 | [diff] [blame] | 420 | |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 421 | return 0; |
| 422 | } |
| 423 | |
| 424 | #if 0 |
David Greene | d865e02 | 2010-01-05 01:29:26 +0000 | [diff] [blame] | 425 | #define ST_DEBUG(X) dbgs() << X |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 426 | #else |
Chris Lattner | 2423303 | 2008-08-19 04:47:09 +0000 | [diff] [blame] | 427 | #define ST_DEBUG(X) |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 428 | #endif |
| 429 | |
| 430 | // Module level constructor. Causes the contents of the Module (sans functions) |
| 431 | // to be added to the slot table. |
| 432 | SlotTracker::SlotTracker(const Module *M) |
Andrew Trick | 18801ec | 2011-09-30 19:48:58 +0000 | [diff] [blame] | 433 | : TheModule(M), TheFunction(0), FunctionProcessed(false), |
Bill Wendling | b29ce26 | 2013-02-11 08:43:33 +0000 | [diff] [blame] | 434 | mNext(0), fNext(0), mdnNext(0), asNext(0) { |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | // Function level constructor. Causes the contents of the Module and the one |
| 438 | // function provided to be added to the slot table. |
| 439 | SlotTracker::SlotTracker(const Function *F) |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 440 | : TheModule(F ? F->getParent() : 0), TheFunction(F), FunctionProcessed(false), |
Bill Wendling | b29ce26 | 2013-02-11 08:43:33 +0000 | [diff] [blame] | 441 | mNext(0), fNext(0), mdnNext(0), asNext(0) { |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | inline void SlotTracker::initialize() { |
| 445 | if (TheModule) { |
| 446 | processModule(); |
| 447 | TheModule = 0; ///< Prevent re-processing next time we're called. |
| 448 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 449 | |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 450 | if (TheFunction && !FunctionProcessed) |
| 451 | processFunction(); |
| 452 | } |
| 453 | |
| 454 | // Iterate through all the global variables, functions, and global |
| 455 | // variable initializers and create slots for them. |
| 456 | void SlotTracker::processModule() { |
Chris Lattner | 2423303 | 2008-08-19 04:47:09 +0000 | [diff] [blame] | 457 | ST_DEBUG("begin processModule!\n"); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 458 | |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 459 | // Add all of the unnamed global variables to the value table. |
| 460 | for (Module::const_global_iterator I = TheModule->global_begin(), |
Devang Patel | 320671d | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 461 | E = TheModule->global_end(); I != E; ++I) { |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 462 | if (!I->hasName()) |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 463 | CreateModuleSlot(I); |
Devang Patel | 320671d | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 464 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 465 | |
Devang Patel | 37c4a2d | 2009-07-29 22:04:47 +0000 | [diff] [blame] | 466 | // Add metadata used by named metadata. |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 467 | for (Module::const_named_metadata_iterator |
Devang Patel | 37c4a2d | 2009-07-29 22:04:47 +0000 | [diff] [blame] | 468 | I = TheModule->named_metadata_begin(), |
| 469 | E = TheModule->named_metadata_end(); I != E; ++I) { |
| 470 | const NamedMDNode *NMD = I; |
Dan Gohman | 872814a | 2010-07-21 18:54:18 +0000 | [diff] [blame] | 471 | for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) |
| 472 | CreateMetadataSlot(NMD->getOperand(i)); |
Devang Patel | 37c4a2d | 2009-07-29 22:04:47 +0000 | [diff] [blame] | 473 | } |
| 474 | |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 475 | for (Module::const_iterator I = TheModule->begin(), E = TheModule->end(); |
Bill Wendling | b29ce26 | 2013-02-11 08:43:33 +0000 | [diff] [blame] | 476 | I != E; ++I) { |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 477 | if (!I->hasName()) |
Bill Wendling | b29ce26 | 2013-02-11 08:43:33 +0000 | [diff] [blame] | 478 | // Add all the unnamed functions to the table. |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 479 | CreateModuleSlot(I); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 480 | |
Bill Wendling | b29ce26 | 2013-02-11 08:43:33 +0000 | [diff] [blame] | 481 | // Add all the function attributes to the table. |
Bill Wendling | 7ab6c76 | 2013-02-20 07:21:42 +0000 | [diff] [blame] | 482 | // FIXME: Add attributes of other objects? |
Bill Wendling | b29ce26 | 2013-02-11 08:43:33 +0000 | [diff] [blame] | 483 | AttributeSet FnAttrs = I->getAttributes().getFnAttributes(); |
| 484 | if (FnAttrs.hasAttributes(AttributeSet::FunctionIndex)) |
| 485 | CreateAttributeSetSlot(FnAttrs); |
| 486 | } |
| 487 | |
Chris Lattner | 2423303 | 2008-08-19 04:47:09 +0000 | [diff] [blame] | 488 | ST_DEBUG("end processModule!\n"); |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 489 | } |
| 490 | |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 491 | // Process the arguments, basic blocks, and instructions of a function. |
| 492 | void SlotTracker::processFunction() { |
Chris Lattner | 2423303 | 2008-08-19 04:47:09 +0000 | [diff] [blame] | 493 | ST_DEBUG("begin processFunction!\n"); |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 494 | fNext = 0; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 495 | |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 496 | // Add all the function arguments with no names. |
| 497 | for(Function::const_arg_iterator AI = TheFunction->arg_begin(), |
| 498 | AE = TheFunction->arg_end(); AI != AE; ++AI) |
| 499 | if (!AI->hasName()) |
| 500 | CreateFunctionSlot(AI); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 501 | |
Chris Lattner | 2423303 | 2008-08-19 04:47:09 +0000 | [diff] [blame] | 502 | ST_DEBUG("Inserting Instructions:\n"); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 503 | |
Chris Lattner | 6e6b180 | 2009-12-31 02:13:35 +0000 | [diff] [blame] | 504 | SmallVector<std::pair<unsigned, MDNode*>, 4> MDForInst; |
Devang Patel | 4321578 | 2009-09-16 20:21:17 +0000 | [diff] [blame] | 505 | |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 506 | // Add all of the basic blocks and instructions with no names. |
| 507 | for (Function::const_iterator BB = TheFunction->begin(), |
| 508 | E = TheFunction->end(); BB != E; ++BB) { |
| 509 | if (!BB->hasName()) |
| 510 | CreateFunctionSlot(BB); |
Andrew Trick | 18801ec | 2011-09-30 19:48:58 +0000 | [diff] [blame] | 511 | |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 512 | for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; |
Devang Patel | 320671d | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 513 | ++I) { |
Chris Lattner | 3990b12 | 2009-12-28 23:41:32 +0000 | [diff] [blame] | 514 | if (!I->getType()->isVoidTy() && !I->hasName()) |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 515 | CreateFunctionSlot(I); |
Andrew Trick | 18801ec | 2011-09-30 19:48:58 +0000 | [diff] [blame] | 516 | |
Chris Lattner | fd450c0 | 2010-05-10 20:53:17 +0000 | [diff] [blame] | 517 | // Intrinsics can directly use metadata. We allow direct calls to any |
| 518 | // llvm.foo function here, because the target may not be linked into the |
| 519 | // optimizer. |
| 520 | if (const CallInst *CI = dyn_cast<CallInst>(I)) { |
| 521 | if (Function *F = CI->getCalledFunction()) |
| 522 | if (F->getName().startswith("llvm.")) |
| 523 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) |
| 524 | if (MDNode *N = dyn_cast_or_null<MDNode>(I->getOperand(i))) |
| 525 | CreateMetadataSlot(N); |
Bill Wendling | 2bb471f | 2013-02-20 00:04:41 +0000 | [diff] [blame] | 526 | |
Bill Wendling | 351b7a1 | 2013-02-22 09:09:42 +0000 | [diff] [blame] | 527 | // Add all the call attributes to the table. |
| 528 | AttributeSet Attrs = CI->getAttributes().getFnAttributes(); |
| 529 | if (Attrs.hasAttributes(AttributeSet::FunctionIndex)) |
| 530 | CreateAttributeSetSlot(Attrs); |
| 531 | } else if (const InvokeInst *II = dyn_cast<InvokeInst>(I)) { |
| 532 | // Add all the call attributes to the table. |
| 533 | AttributeSet Attrs = II->getAttributes().getFnAttributes(); |
| 534 | if (Attrs.hasAttributes(AttributeSet::FunctionIndex)) |
| 535 | CreateAttributeSetSlot(Attrs); |
Chris Lattner | fd450c0 | 2010-05-10 20:53:17 +0000 | [diff] [blame] | 536 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 537 | |
Devang Patel | 4321578 | 2009-09-16 20:21:17 +0000 | [diff] [blame] | 538 | // Process metadata attached with this instruction. |
Chris Lattner | 3990b12 | 2009-12-28 23:41:32 +0000 | [diff] [blame] | 539 | I->getAllMetadata(MDForInst); |
| 540 | for (unsigned i = 0, e = MDForInst.size(); i != e; ++i) |
| 541 | CreateMetadataSlot(MDForInst[i].second); |
Chris Lattner | 6e6b180 | 2009-12-31 02:13:35 +0000 | [diff] [blame] | 542 | MDForInst.clear(); |
Devang Patel | 320671d | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 543 | } |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 544 | } |
Devang Patel | 4321578 | 2009-09-16 20:21:17 +0000 | [diff] [blame] | 545 | |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 546 | FunctionProcessed = true; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 547 | |
Chris Lattner | 2423303 | 2008-08-19 04:47:09 +0000 | [diff] [blame] | 548 | ST_DEBUG("end processFunction!\n"); |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | /// Clean up after incorporating a function. This is the only way to get out of |
| 552 | /// the function incorporation state that affects get*Slot/Create*Slot. Function |
| 553 | /// incorporation state is indicated by TheFunction != 0. |
| 554 | void SlotTracker::purgeFunction() { |
Chris Lattner | 2423303 | 2008-08-19 04:47:09 +0000 | [diff] [blame] | 555 | ST_DEBUG("begin purgeFunction!\n"); |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 556 | fMap.clear(); // Simply discard the function level map |
| 557 | TheFunction = 0; |
| 558 | FunctionProcessed = false; |
Chris Lattner | 2423303 | 2008-08-19 04:47:09 +0000 | [diff] [blame] | 559 | ST_DEBUG("end purgeFunction!\n"); |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | /// getGlobalSlot - Get the slot number of a global value. |
| 563 | int SlotTracker::getGlobalSlot(const GlobalValue *V) { |
| 564 | // Check for uninitialized state and do lazy initialization. |
| 565 | initialize(); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 566 | |
Jay Foad | 7f0ce34 | 2011-07-11 07:28:49 +0000 | [diff] [blame] | 567 | // Find the value in the module map |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 568 | ValueMap::iterator MI = mMap.find(V); |
Dan Gohman | aeaf245 | 2008-10-01 19:58:59 +0000 | [diff] [blame] | 569 | return MI == mMap.end() ? -1 : (int)MI->second; |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 570 | } |
| 571 | |
Chris Lattner | 307c989 | 2009-12-31 02:20:11 +0000 | [diff] [blame] | 572 | /// getMetadataSlot - Get the slot number of a MDNode. |
Devang Patel | 320671d | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 573 | int SlotTracker::getMetadataSlot(const MDNode *N) { |
| 574 | // Check for uninitialized state and do lazy initialization. |
| 575 | initialize(); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 576 | |
Jay Foad | 7f0ce34 | 2011-07-11 07:28:49 +0000 | [diff] [blame] | 577 | // Find the MDNode in the module map |
Chris Lattner | 307c989 | 2009-12-31 02:20:11 +0000 | [diff] [blame] | 578 | mdn_iterator MI = mdnMap.find(N); |
Devang Patel | 320671d | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 579 | return MI == mdnMap.end() ? -1 : (int)MI->second; |
| 580 | } |
| 581 | |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 582 | |
| 583 | /// getLocalSlot - Get the slot number for a value that is local to a function. |
| 584 | int SlotTracker::getLocalSlot(const Value *V) { |
| 585 | assert(!isa<Constant>(V) && "Can't get a constant or global slot with this!"); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 586 | |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 587 | // Check for uninitialized state and do lazy initialization. |
| 588 | initialize(); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 589 | |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 590 | ValueMap::iterator FI = fMap.find(V); |
Dan Gohman | aeaf245 | 2008-10-01 19:58:59 +0000 | [diff] [blame] | 591 | return FI == fMap.end() ? -1 : (int)FI->second; |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 592 | } |
| 593 | |
Bill Wendling | b29ce26 | 2013-02-11 08:43:33 +0000 | [diff] [blame] | 594 | int SlotTracker::getAttributeGroupSlot(AttributeSet AS) { |
| 595 | // Check for uninitialized state and do lazy initialization. |
| 596 | initialize(); |
| 597 | |
| 598 | // Find the AttributeSet in the module map. |
| 599 | as_iterator AI = asMap.find(AS); |
| 600 | return AI == asMap.end() ? -1 : (int)AI->second; |
| 601 | } |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 602 | |
| 603 | /// CreateModuleSlot - Insert the specified GlobalValue* into the slot table. |
| 604 | void SlotTracker::CreateModuleSlot(const GlobalValue *V) { |
| 605 | assert(V && "Can't insert a null Value into SlotTracker!"); |
Chris Lattner | 4ee93c4 | 2009-12-29 07:25:48 +0000 | [diff] [blame] | 606 | assert(!V->getType()->isVoidTy() && "Doesn't need a slot!"); |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 607 | assert(!V->hasName() && "Doesn't need a slot!"); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 608 | |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 609 | unsigned DestSlot = mNext++; |
| 610 | mMap[V] = DestSlot; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 611 | |
Chris Lattner | 2423303 | 2008-08-19 04:47:09 +0000 | [diff] [blame] | 612 | ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" << |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 613 | DestSlot << " ["); |
| 614 | // G = Global, F = Function, A = Alias, o = other |
Chris Lattner | 2423303 | 2008-08-19 04:47:09 +0000 | [diff] [blame] | 615 | ST_DEBUG((isa<GlobalVariable>(V) ? 'G' : |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 616 | (isa<Function>(V) ? 'F' : |
| 617 | (isa<GlobalAlias>(V) ? 'A' : 'o'))) << "]\n"); |
| 618 | } |
| 619 | |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 620 | /// CreateSlot - Create a new slot for the specified value if it has no name. |
| 621 | void SlotTracker::CreateFunctionSlot(const Value *V) { |
Chris Lattner | 4ee93c4 | 2009-12-29 07:25:48 +0000 | [diff] [blame] | 622 | assert(!V->getType()->isVoidTy() && !V->hasName() && "Doesn't need a slot!"); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 623 | |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 624 | unsigned DestSlot = fNext++; |
| 625 | fMap[V] = DestSlot; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 626 | |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 627 | // G = Global, F = Function, o = other |
Chris Lattner | 2423303 | 2008-08-19 04:47:09 +0000 | [diff] [blame] | 628 | ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" << |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 629 | DestSlot << " [o]\n"); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 630 | } |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 631 | |
Devang Patel | 320671d | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 632 | /// CreateModuleSlot - Insert the specified MDNode* into the slot table. |
| 633 | void SlotTracker::CreateMetadataSlot(const MDNode *N) { |
| 634 | assert(N && "Can't insert a null Value into SlotTracker!"); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 635 | |
Chris Lattner | 2b4b1e2 | 2009-12-31 02:27:30 +0000 | [diff] [blame] | 636 | // Don't insert if N is a function-local metadata, these are always printed |
| 637 | // inline. |
Dan Gohman | 3bdfbf5 | 2010-07-20 23:55:01 +0000 | [diff] [blame] | 638 | if (!N->isFunctionLocal()) { |
| 639 | mdn_iterator I = mdnMap.find(N); |
| 640 | if (I != mdnMap.end()) |
| 641 | return; |
Victor Hernandez | ff7707e | 2009-12-04 20:07:10 +0000 | [diff] [blame] | 642 | |
Dan Gohman | 3bdfbf5 | 2010-07-20 23:55:01 +0000 | [diff] [blame] | 643 | unsigned DestSlot = mdnNext++; |
| 644 | mdnMap[N] = DestSlot; |
| 645 | } |
Devang Patel | 320671d | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 646 | |
Chris Lattner | 2b4b1e2 | 2009-12-31 02:27:30 +0000 | [diff] [blame] | 647 | // Recursively add any MDNodes referenced by operands. |
| 648 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) |
| 649 | if (const MDNode *Op = dyn_cast_or_null<MDNode>(N->getOperand(i))) |
| 650 | CreateMetadataSlot(Op); |
Devang Patel | 320671d | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 651 | } |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 652 | |
Bill Wendling | b29ce26 | 2013-02-11 08:43:33 +0000 | [diff] [blame] | 653 | void SlotTracker::CreateAttributeSetSlot(AttributeSet AS) { |
| 654 | assert(AS.hasAttributes(AttributeSet::FunctionIndex) && |
| 655 | "Doesn't need a slot!"); |
| 656 | |
| 657 | as_iterator I = asMap.find(AS); |
| 658 | if (I != asMap.end()) |
| 659 | return; |
| 660 | |
| 661 | unsigned DestSlot = asNext++; |
| 662 | asMap[AS] = DestSlot; |
| 663 | } |
| 664 | |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 665 | //===----------------------------------------------------------------------===// |
| 666 | // AsmWriter Implementation |
| 667 | //===----------------------------------------------------------------------===// |
Chris Lattner | f082b80 | 2002-07-23 18:07:49 +0000 | [diff] [blame] | 668 | |
Dan Gohman | 1220e10 | 2009-08-12 20:56:03 +0000 | [diff] [blame] | 669 | static void WriteAsOperandInternal(raw_ostream &Out, const Value *V, |
Dan Gohman | d6c0f65 | 2009-08-13 15:27:57 +0000 | [diff] [blame] | 670 | TypePrinting *TypePrinter, |
Dan Gohman | 3bdfbf5 | 2010-07-20 23:55:01 +0000 | [diff] [blame] | 671 | SlotTracker *Machine, |
| 672 | const Module *Context); |
Reid Spencer | 0e25e1c | 2004-07-04 11:50:43 +0000 | [diff] [blame] | 673 | |
Chris Lattner | c97536e | 2008-08-17 04:40:13 +0000 | [diff] [blame] | 674 | |
Chris Lattner | 207b5bc | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 675 | |
Chris Lattner | 82c4bc7 | 2006-12-06 06:40:49 +0000 | [diff] [blame] | 676 | static const char *getPredicateText(unsigned predicate) { |
Reid Spencer | 81dfeb3 | 2006-12-04 05:19:18 +0000 | [diff] [blame] | 677 | const char * pred = "unknown"; |
| 678 | switch (predicate) { |
Chris Lattner | 6e6b180 | 2009-12-31 02:13:35 +0000 | [diff] [blame] | 679 | case FCmpInst::FCMP_FALSE: pred = "false"; break; |
| 680 | case FCmpInst::FCMP_OEQ: pred = "oeq"; break; |
| 681 | case FCmpInst::FCMP_OGT: pred = "ogt"; break; |
| 682 | case FCmpInst::FCMP_OGE: pred = "oge"; break; |
| 683 | case FCmpInst::FCMP_OLT: pred = "olt"; break; |
| 684 | case FCmpInst::FCMP_OLE: pred = "ole"; break; |
| 685 | case FCmpInst::FCMP_ONE: pred = "one"; break; |
| 686 | case FCmpInst::FCMP_ORD: pred = "ord"; break; |
| 687 | case FCmpInst::FCMP_UNO: pred = "uno"; break; |
| 688 | case FCmpInst::FCMP_UEQ: pred = "ueq"; break; |
| 689 | case FCmpInst::FCMP_UGT: pred = "ugt"; break; |
| 690 | case FCmpInst::FCMP_UGE: pred = "uge"; break; |
| 691 | case FCmpInst::FCMP_ULT: pred = "ult"; break; |
| 692 | case FCmpInst::FCMP_ULE: pred = "ule"; break; |
| 693 | case FCmpInst::FCMP_UNE: pred = "une"; break; |
| 694 | case FCmpInst::FCMP_TRUE: pred = "true"; break; |
| 695 | case ICmpInst::ICMP_EQ: pred = "eq"; break; |
| 696 | case ICmpInst::ICMP_NE: pred = "ne"; break; |
| 697 | case ICmpInst::ICMP_SGT: pred = "sgt"; break; |
| 698 | case ICmpInst::ICMP_SGE: pred = "sge"; break; |
| 699 | case ICmpInst::ICMP_SLT: pred = "slt"; break; |
| 700 | case ICmpInst::ICMP_SLE: pred = "sle"; break; |
| 701 | case ICmpInst::ICMP_UGT: pred = "ugt"; break; |
| 702 | case ICmpInst::ICMP_UGE: pred = "uge"; break; |
| 703 | case ICmpInst::ICMP_ULT: pred = "ult"; break; |
| 704 | case ICmpInst::ICMP_ULE: pred = "ule"; break; |
Reid Spencer | 81dfeb3 | 2006-12-04 05:19:18 +0000 | [diff] [blame] | 705 | } |
| 706 | return pred; |
| 707 | } |
| 708 | |
Eli Friedman | ff03048 | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 709 | static void writeAtomicRMWOperation(raw_ostream &Out, |
| 710 | AtomicRMWInst::BinOp Op) { |
| 711 | switch (Op) { |
| 712 | default: Out << " <unknown operation " << Op << ">"; break; |
| 713 | case AtomicRMWInst::Xchg: Out << " xchg"; break; |
| 714 | case AtomicRMWInst::Add: Out << " add"; break; |
| 715 | case AtomicRMWInst::Sub: Out << " sub"; break; |
| 716 | case AtomicRMWInst::And: Out << " and"; break; |
| 717 | case AtomicRMWInst::Nand: Out << " nand"; break; |
| 718 | case AtomicRMWInst::Or: Out << " or"; break; |
| 719 | case AtomicRMWInst::Xor: Out << " xor"; break; |
| 720 | case AtomicRMWInst::Max: Out << " max"; break; |
| 721 | case AtomicRMWInst::Min: Out << " min"; break; |
| 722 | case AtomicRMWInst::UMax: Out << " umax"; break; |
| 723 | case AtomicRMWInst::UMin: Out << " umin"; break; |
| 724 | } |
| 725 | } |
Devang Patel | 320671d | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 726 | |
Dan Gohman | 1220e10 | 2009-08-12 20:56:03 +0000 | [diff] [blame] | 727 | static void WriteOptimizationInfo(raw_ostream &Out, const User *U) { |
Michael Ilseman | 15c13d3 | 2012-11-27 00:42:44 +0000 | [diff] [blame] | 728 | if (const FPMathOperator *FPO = dyn_cast<const FPMathOperator>(U)) { |
| 729 | // Unsafe algebra implies all the others, no need to write them all out |
| 730 | if (FPO->hasUnsafeAlgebra()) |
| 731 | Out << " fast"; |
| 732 | else { |
| 733 | if (FPO->hasNoNaNs()) |
| 734 | Out << " nnan"; |
| 735 | if (FPO->hasNoInfs()) |
| 736 | Out << " ninf"; |
| 737 | if (FPO->hasNoSignedZeros()) |
| 738 | Out << " nsz"; |
| 739 | if (FPO->hasAllowReciprocal()) |
| 740 | Out << " arcp"; |
| 741 | } |
| 742 | } |
| 743 | |
Dan Gohman | 1224c38 | 2009-07-20 21:19:07 +0000 | [diff] [blame] | 744 | if (const OverflowingBinaryOperator *OBO = |
| 745 | dyn_cast<OverflowingBinaryOperator>(U)) { |
Dan Gohman | 5078f84 | 2009-08-20 17:11:38 +0000 | [diff] [blame] | 746 | if (OBO->hasNoUnsignedWrap()) |
Dan Gohman | 59858cf | 2009-07-27 16:11:46 +0000 | [diff] [blame] | 747 | Out << " nuw"; |
Dan Gohman | 5078f84 | 2009-08-20 17:11:38 +0000 | [diff] [blame] | 748 | if (OBO->hasNoSignedWrap()) |
Dan Gohman | 59858cf | 2009-07-27 16:11:46 +0000 | [diff] [blame] | 749 | Out << " nsw"; |
Chris Lattner | 35bda89 | 2011-02-06 21:44:57 +0000 | [diff] [blame] | 750 | } else if (const PossiblyExactOperator *Div = |
| 751 | dyn_cast<PossiblyExactOperator>(U)) { |
Dan Gohman | 1224c38 | 2009-07-20 21:19:07 +0000 | [diff] [blame] | 752 | if (Div->isExact()) |
Dan Gohman | 59858cf | 2009-07-27 16:11:46 +0000 | [diff] [blame] | 753 | Out << " exact"; |
Dan Gohman | dd8004d | 2009-07-27 21:53:46 +0000 | [diff] [blame] | 754 | } else if (const GEPOperator *GEP = dyn_cast<GEPOperator>(U)) { |
| 755 | if (GEP->isInBounds()) |
| 756 | Out << " inbounds"; |
Dan Gohman | 1224c38 | 2009-07-20 21:19:07 +0000 | [diff] [blame] | 757 | } |
| 758 | } |
| 759 | |
Dan Gohman | 40cf12f | 2010-07-14 20:57:55 +0000 | [diff] [blame] | 760 | static void WriteConstantInternal(raw_ostream &Out, const Constant *CV, |
| 761 | TypePrinting &TypePrinter, |
Dan Gohman | 3bdfbf5 | 2010-07-20 23:55:01 +0000 | [diff] [blame] | 762 | SlotTracker *Machine, |
| 763 | const Module *Context) { |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 764 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) { |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 765 | if (CI->getType()->isIntegerTy(1)) { |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 766 | Out << (CI->getZExtValue() ? "true" : "false"); |
Chris Lattner | fad86b0 | 2008-08-17 07:19:36 +0000 | [diff] [blame] | 767 | return; |
| 768 | } |
| 769 | Out << CI->getValue(); |
| 770 | return; |
| 771 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 772 | |
Chris Lattner | fad86b0 | 2008-08-17 07:19:36 +0000 | [diff] [blame] | 773 | if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) { |
Tobias Grosser | 057beb8 | 2012-05-24 15:59:06 +0000 | [diff] [blame] | 774 | if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEsingle || |
Dan Gohman | ce16339 | 2011-12-17 00:04:22 +0000 | [diff] [blame] | 775 | &CFP->getValueAPF().getSemantics() == &APFloat::IEEEdouble) { |
Dale Johannesen | 9d5f456 | 2007-09-12 03:30:33 +0000 | [diff] [blame] | 776 | // We would like to output the FP constant value in exponential notation, |
| 777 | // but we cannot do this if doing so will lose precision. Check here to |
| 778 | // make sure that we only output it in exponential format if we can parse |
| 779 | // the value back and get the same value. |
| 780 | // |
Dale Johannesen | 541ed9f | 2009-01-21 20:32:55 +0000 | [diff] [blame] | 781 | bool ignored; |
Dan Gohman | ce16339 | 2011-12-17 00:04:22 +0000 | [diff] [blame] | 782 | bool isHalf = &CFP->getValueAPF().getSemantics()==&APFloat::IEEEhalf; |
Dale Johannesen | 9d5f456 | 2007-09-12 03:30:33 +0000 | [diff] [blame] | 783 | bool isDouble = &CFP->getValueAPF().getSemantics()==&APFloat::IEEEdouble; |
NAKAMURA Takumi | 22bed5d | 2012-02-16 08:12:24 +0000 | [diff] [blame] | 784 | bool isInf = CFP->getValueAPF().isInfinity(); |
| 785 | bool isNaN = CFP->getValueAPF().isNaN(); |
| 786 | if (!isHalf && !isInf && !isNaN) { |
Dan Gohman | ce16339 | 2011-12-17 00:04:22 +0000 | [diff] [blame] | 787 | double Val = isDouble ? CFP->getValueAPF().convertToDouble() : |
| 788 | CFP->getValueAPF().convertToFloat(); |
| 789 | SmallString<128> StrVal; |
| 790 | raw_svector_ostream(StrVal) << Val; |
Chris Lattner | 66e810b | 2002-04-18 18:53:13 +0000 | [diff] [blame] | 791 | |
Dan Gohman | ce16339 | 2011-12-17 00:04:22 +0000 | [diff] [blame] | 792 | // Check to make sure that the stringized number is not some string like |
| 793 | // "Inf" or NaN, that atof will accept, but the lexer will not. Check |
| 794 | // that the string matches the "[-+]?[0-9]" regex. |
| 795 | // |
| 796 | if ((StrVal[0] >= '0' && StrVal[0] <= '9') || |
| 797 | ((StrVal[0] == '-' || StrVal[0] == '+') && |
| 798 | (StrVal[1] >= '0' && StrVal[1] <= '9'))) { |
| 799 | // Reparse stringized version! |
NAKAMURA Takumi | c8782a1 | 2012-02-16 04:19:15 +0000 | [diff] [blame] | 800 | if (APFloat(APFloat::IEEEdouble, StrVal).convertToDouble() == Val) { |
Dan Gohman | ce16339 | 2011-12-17 00:04:22 +0000 | [diff] [blame] | 801 | Out << StrVal.str(); |
| 802 | return; |
| 803 | } |
Dale Johannesen | 9d5f456 | 2007-09-12 03:30:33 +0000 | [diff] [blame] | 804 | } |
Chris Lattner | 66e810b | 2002-04-18 18:53:13 +0000 | [diff] [blame] | 805 | } |
Dale Johannesen | 9d5f456 | 2007-09-12 03:30:33 +0000 | [diff] [blame] | 806 | // Otherwise we could not reparse it to exactly the same value, so we must |
Dale Johannesen | 541ed9f | 2009-01-21 20:32:55 +0000 | [diff] [blame] | 807 | // output the string in hexadecimal format! Note that loading and storing |
| 808 | // floating point types changes the bits of NaNs on some hosts, notably |
| 809 | // x86, so we must not use these types. |
Dale Johannesen | 9d5f456 | 2007-09-12 03:30:33 +0000 | [diff] [blame] | 810 | assert(sizeof(double) == sizeof(uint64_t) && |
| 811 | "assuming that double is 64 bits!"); |
Chris Lattner | c6a1346 | 2008-11-10 04:30:26 +0000 | [diff] [blame] | 812 | char Buffer[40]; |
Dale Johannesen | 541ed9f | 2009-01-21 20:32:55 +0000 | [diff] [blame] | 813 | APFloat apf = CFP->getValueAPF(); |
Dan Gohman | ce16339 | 2011-12-17 00:04:22 +0000 | [diff] [blame] | 814 | // Halves and floats are represented in ASCII IR as double, convert. |
Dale Johannesen | 541ed9f | 2009-01-21 20:32:55 +0000 | [diff] [blame] | 815 | if (!isDouble) |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 816 | apf.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, |
Dale Johannesen | 541ed9f | 2009-01-21 20:32:55 +0000 | [diff] [blame] | 817 | &ignored); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 818 | Out << "0x" << |
| 819 | utohex_buffer(uint64_t(apf.bitcastToAPInt().getZExtValue()), |
Dale Johannesen | 541ed9f | 2009-01-21 20:32:55 +0000 | [diff] [blame] | 820 | Buffer+40); |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 821 | return; |
| 822 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 823 | |
Tobias Grosser | 057beb8 | 2012-05-24 15:59:06 +0000 | [diff] [blame] | 824 | // Either half, or some form of long double. |
| 825 | // These appear as a magic letter identifying the type, then a |
| 826 | // fixed number of hex digits. |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 827 | Out << "0x"; |
Tobias Grosser | 057beb8 | 2012-05-24 15:59:06 +0000 | [diff] [blame] | 828 | // Bit position, in the current word, of the next nibble to print. |
| 829 | int shiftcount; |
| 830 | |
Dale Johannesen | 1b25cb2 | 2009-03-23 21:16:53 +0000 | [diff] [blame] | 831 | if (&CFP->getValueAPF().getSemantics() == &APFloat::x87DoubleExtended) { |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 832 | Out << 'K'; |
Dale Johannesen | 1b25cb2 | 2009-03-23 21:16:53 +0000 | [diff] [blame] | 833 | // api needed to prevent premature destruction |
| 834 | APInt api = CFP->getValueAPF().bitcastToAPInt(); |
| 835 | const uint64_t* p = api.getRawData(); |
| 836 | uint64_t word = p[1]; |
Tobias Grosser | 057beb8 | 2012-05-24 15:59:06 +0000 | [diff] [blame] | 837 | shiftcount = 12; |
Dale Johannesen | 1b25cb2 | 2009-03-23 21:16:53 +0000 | [diff] [blame] | 838 | int width = api.getBitWidth(); |
| 839 | for (int j=0; j<width; j+=4, shiftcount-=4) { |
| 840 | unsigned int nibble = (word>>shiftcount) & 15; |
| 841 | if (nibble < 10) |
| 842 | Out << (unsigned char)(nibble + '0'); |
| 843 | else |
| 844 | Out << (unsigned char)(nibble - 10 + 'A'); |
| 845 | if (shiftcount == 0 && j+4 < width) { |
| 846 | word = *p; |
| 847 | shiftcount = 64; |
| 848 | if (width-j-4 < 64) |
| 849 | shiftcount = width-j-4; |
| 850 | } |
| 851 | } |
| 852 | return; |
Tobias Grosser | 057beb8 | 2012-05-24 15:59:06 +0000 | [diff] [blame] | 853 | } else if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEquad) { |
| 854 | shiftcount = 60; |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 855 | Out << 'L'; |
Tobias Grosser | 057beb8 | 2012-05-24 15:59:06 +0000 | [diff] [blame] | 856 | } else if (&CFP->getValueAPF().getSemantics() == &APFloat::PPCDoubleDouble) { |
| 857 | shiftcount = 60; |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 858 | Out << 'M'; |
Tobias Grosser | 057beb8 | 2012-05-24 15:59:06 +0000 | [diff] [blame] | 859 | } else if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEhalf) { |
| 860 | shiftcount = 12; |
| 861 | Out << 'H'; |
| 862 | } else |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 863 | llvm_unreachable("Unsupported floating point type"); |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 864 | // api needed to prevent premature destruction |
Dale Johannesen | 7111b02 | 2008-10-09 18:53:47 +0000 | [diff] [blame] | 865 | APInt api = CFP->getValueAPF().bitcastToAPInt(); |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 866 | const uint64_t* p = api.getRawData(); |
| 867 | uint64_t word = *p; |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 868 | int width = api.getBitWidth(); |
| 869 | for (int j=0; j<width; j+=4, shiftcount-=4) { |
| 870 | unsigned int nibble = (word>>shiftcount) & 15; |
| 871 | if (nibble < 10) |
| 872 | Out << (unsigned char)(nibble + '0'); |
Dale Johannesen | 9d5f456 | 2007-09-12 03:30:33 +0000 | [diff] [blame] | 873 | else |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 874 | Out << (unsigned char)(nibble - 10 + 'A'); |
| 875 | if (shiftcount == 0 && j+4 < width) { |
| 876 | word = *(++p); |
| 877 | shiftcount = 64; |
| 878 | if (width-j-4 < 64) |
| 879 | shiftcount = width-j-4; |
Dale Johannesen | 9d5f456 | 2007-09-12 03:30:33 +0000 | [diff] [blame] | 880 | } |
| 881 | } |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 882 | return; |
| 883 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 884 | |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 885 | if (isa<ConstantAggregateZero>(CV)) { |
Chris Lattner | de512b5 | 2004-02-15 05:55:15 +0000 | [diff] [blame] | 886 | Out << "zeroinitializer"; |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 887 | return; |
| 888 | } |
Andrew Trick | 18801ec | 2011-09-30 19:48:58 +0000 | [diff] [blame] | 889 | |
Chris Lattner | 73050e1 | 2009-10-28 03:38:12 +0000 | [diff] [blame] | 890 | if (const BlockAddress *BA = dyn_cast<BlockAddress>(CV)) { |
| 891 | Out << "blockaddress("; |
Dan Gohman | 3bdfbf5 | 2010-07-20 23:55:01 +0000 | [diff] [blame] | 892 | WriteAsOperandInternal(Out, BA->getFunction(), &TypePrinter, Machine, |
| 893 | Context); |
Chris Lattner | 73050e1 | 2009-10-28 03:38:12 +0000 | [diff] [blame] | 894 | Out << ", "; |
Dan Gohman | 3bdfbf5 | 2010-07-20 23:55:01 +0000 | [diff] [blame] | 895 | WriteAsOperandInternal(Out, BA->getBasicBlock(), &TypePrinter, Machine, |
| 896 | Context); |
Chris Lattner | 73050e1 | 2009-10-28 03:38:12 +0000 | [diff] [blame] | 897 | Out << ")"; |
| 898 | return; |
| 899 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 900 | |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 901 | if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) { |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 902 | Type *ETy = CA->getType()->getElementType(); |
Chris Lattner | 18c7f80 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 903 | Out << '['; |
| 904 | TypePrinter.print(ETy, Out); |
| 905 | Out << ' '; |
| 906 | WriteAsOperandInternal(Out, CA->getOperand(0), |
| 907 | &TypePrinter, Machine, |
| 908 | Context); |
| 909 | for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) { |
| 910 | Out << ", "; |
Chris Lattner | 8b10b69 | 2012-01-31 03:15:40 +0000 | [diff] [blame] | 911 | TypePrinter.print(ETy, Out); |
| 912 | Out << ' '; |
Chris Lattner | 18c7f80 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 913 | WriteAsOperandInternal(Out, CA->getOperand(i), &TypePrinter, Machine, |
Chris Lattner | 8b10b69 | 2012-01-31 03:15:40 +0000 | [diff] [blame] | 914 | Context); |
Chris Lattner | 7a716ad | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 915 | } |
Chris Lattner | 18c7f80 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 916 | Out << ']'; |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 917 | return; |
| 918 | } |
Michael Ilseman | 407a616 | 2012-11-15 22:34:00 +0000 | [diff] [blame] | 919 | |
Chris Lattner | d59ae90 | 2012-01-26 02:32:04 +0000 | [diff] [blame] | 920 | if (const ConstantDataArray *CA = dyn_cast<ConstantDataArray>(CV)) { |
| 921 | // As a special case, print the array as a string if it is an array of |
| 922 | // i8 with ConstantInt values. |
| 923 | if (CA->isString()) { |
| 924 | Out << "c\""; |
| 925 | PrintEscapedString(CA->getAsString(), Out); |
| 926 | Out << '"'; |
| 927 | return; |
| 928 | } |
| 929 | |
| 930 | Type *ETy = CA->getType()->getElementType(); |
| 931 | Out << '['; |
Chris Lattner | 8b10b69 | 2012-01-31 03:15:40 +0000 | [diff] [blame] | 932 | TypePrinter.print(ETy, Out); |
| 933 | Out << ' '; |
| 934 | WriteAsOperandInternal(Out, CA->getElementAsConstant(0), |
| 935 | &TypePrinter, Machine, |
| 936 | Context); |
| 937 | for (unsigned i = 1, e = CA->getNumElements(); i != e; ++i) { |
| 938 | Out << ", "; |
Chris Lattner | d59ae90 | 2012-01-26 02:32:04 +0000 | [diff] [blame] | 939 | TypePrinter.print(ETy, Out); |
| 940 | Out << ' '; |
Chris Lattner | 8b10b69 | 2012-01-31 03:15:40 +0000 | [diff] [blame] | 941 | WriteAsOperandInternal(Out, CA->getElementAsConstant(i), &TypePrinter, |
| 942 | Machine, Context); |
Chris Lattner | d59ae90 | 2012-01-26 02:32:04 +0000 | [diff] [blame] | 943 | } |
Chris Lattner | 8b10b69 | 2012-01-31 03:15:40 +0000 | [diff] [blame] | 944 | Out << ']'; |
Chris Lattner | d59ae90 | 2012-01-26 02:32:04 +0000 | [diff] [blame] | 945 | return; |
| 946 | } |
| 947 | |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 948 | |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 949 | if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) { |
Andrew Lenharth | 43f344a | 2007-01-08 18:21:30 +0000 | [diff] [blame] | 950 | if (CS->getType()->isPacked()) |
| 951 | Out << '<'; |
Misha Brukman | 40c732c | 2004-06-04 21:11:51 +0000 | [diff] [blame] | 952 | Out << '{'; |
Jim Laskey | a3f332b | 2006-02-25 12:27:03 +0000 | [diff] [blame] | 953 | unsigned N = CS->getNumOperands(); |
| 954 | if (N) { |
Chris Lattner | 2423303 | 2008-08-19 04:47:09 +0000 | [diff] [blame] | 955 | Out << ' '; |
Chris Lattner | 0f7364b | 2009-02-28 21:26:53 +0000 | [diff] [blame] | 956 | TypePrinter.print(CS->getOperand(0)->getType(), Out); |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 957 | Out << ' '; |
Chris Lattner | 7a716ad | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 958 | |
Dan Gohman | 3bdfbf5 | 2010-07-20 23:55:01 +0000 | [diff] [blame] | 959 | WriteAsOperandInternal(Out, CS->getOperand(0), &TypePrinter, Machine, |
| 960 | Context); |
Chris Lattner | 7a716ad | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 961 | |
Jim Laskey | a3f332b | 2006-02-25 12:27:03 +0000 | [diff] [blame] | 962 | for (unsigned i = 1; i < N; i++) { |
Chris Lattner | 7a716ad | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 963 | Out << ", "; |
Chris Lattner | 0f7364b | 2009-02-28 21:26:53 +0000 | [diff] [blame] | 964 | TypePrinter.print(CS->getOperand(i)->getType(), Out); |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 965 | Out << ' '; |
Chris Lattner | 7a716ad | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 966 | |
Dan Gohman | 3bdfbf5 | 2010-07-20 23:55:01 +0000 | [diff] [blame] | 967 | WriteAsOperandInternal(Out, CS->getOperand(i), &TypePrinter, Machine, |
| 968 | Context); |
Chris Lattner | 7a716ad | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 969 | } |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 970 | Out << ' '; |
Chris Lattner | 7a716ad | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 971 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 972 | |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 973 | Out << '}'; |
Andrew Lenharth | 43f344a | 2007-01-08 18:21:30 +0000 | [diff] [blame] | 974 | if (CS->getType()->isPacked()) |
| 975 | Out << '>'; |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 976 | return; |
| 977 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 978 | |
Chris Lattner | d59ae90 | 2012-01-26 02:32:04 +0000 | [diff] [blame] | 979 | if (isa<ConstantVector>(CV) || isa<ConstantDataVector>(CV)) { |
| 980 | Type *ETy = CV->getType()->getVectorElementType(); |
Dan Gohman | 7dfa07f | 2009-02-11 00:25:25 +0000 | [diff] [blame] | 981 | Out << '<'; |
Chris Lattner | 0f7364b | 2009-02-28 21:26:53 +0000 | [diff] [blame] | 982 | TypePrinter.print(ETy, Out); |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 983 | Out << ' '; |
Chris Lattner | d59ae90 | 2012-01-26 02:32:04 +0000 | [diff] [blame] | 984 | WriteAsOperandInternal(Out, CV->getAggregateElement(0U), &TypePrinter, |
| 985 | Machine, Context); |
| 986 | for (unsigned i = 1, e = CV->getType()->getVectorNumElements(); i != e;++i){ |
Chris Lattner | 4667b71 | 2008-08-19 05:26:17 +0000 | [diff] [blame] | 987 | Out << ", "; |
Chris Lattner | 0f7364b | 2009-02-28 21:26:53 +0000 | [diff] [blame] | 988 | TypePrinter.print(ETy, Out); |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 989 | Out << ' '; |
Chris Lattner | d59ae90 | 2012-01-26 02:32:04 +0000 | [diff] [blame] | 990 | WriteAsOperandInternal(Out, CV->getAggregateElement(i), &TypePrinter, |
| 991 | Machine, Context); |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 992 | } |
Dan Gohman | 7dfa07f | 2009-02-11 00:25:25 +0000 | [diff] [blame] | 993 | Out << '>'; |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 994 | return; |
| 995 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 996 | |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 997 | if (isa<ConstantPointerNull>(CV)) { |
Chris Lattner | 7a716ad | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 998 | Out << "null"; |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 999 | return; |
| 1000 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1001 | |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1002 | if (isa<UndefValue>(CV)) { |
Chris Lattner | b976e66 | 2004-10-16 18:08:06 +0000 | [diff] [blame] | 1003 | Out << "undef"; |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1004 | return; |
| 1005 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1006 | |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1007 | if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) { |
Reid Spencer | 81dfeb3 | 2006-12-04 05:19:18 +0000 | [diff] [blame] | 1008 | Out << CE->getOpcodeName(); |
Dan Gohman | 59858cf | 2009-07-27 16:11:46 +0000 | [diff] [blame] | 1009 | WriteOptimizationInfo(Out, CE); |
Reid Spencer | 81dfeb3 | 2006-12-04 05:19:18 +0000 | [diff] [blame] | 1010 | if (CE->isCompare()) |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1011 | Out << ' ' << getPredicateText(CE->getPredicate()); |
Reid Spencer | 81dfeb3 | 2006-12-04 05:19:18 +0000 | [diff] [blame] | 1012 | Out << " ("; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1013 | |
Vikram S. Adve | b4dbb44 | 2002-07-14 23:14:45 +0000 | [diff] [blame] | 1014 | for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) { |
Chris Lattner | 0f7364b | 2009-02-28 21:26:53 +0000 | [diff] [blame] | 1015 | TypePrinter.print((*OI)->getType(), Out); |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1016 | Out << ' '; |
Dan Gohman | 3bdfbf5 | 2010-07-20 23:55:01 +0000 | [diff] [blame] | 1017 | WriteAsOperandInternal(Out, *OI, &TypePrinter, Machine, Context); |
Vikram S. Adve | b4dbb44 | 2002-07-14 23:14:45 +0000 | [diff] [blame] | 1018 | if (OI+1 != CE->op_end()) |
Chris Lattner | c188eeb | 2002-07-30 18:54:25 +0000 | [diff] [blame] | 1019 | Out << ", "; |
Vikram S. Adve | b4dbb44 | 2002-07-14 23:14:45 +0000 | [diff] [blame] | 1020 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1021 | |
Dan Gohman | 995be7d | 2008-05-31 19:12:39 +0000 | [diff] [blame] | 1022 | if (CE->hasIndices()) { |
Jay Foad | d30aa5a | 2011-04-13 15:22:40 +0000 | [diff] [blame] | 1023 | ArrayRef<unsigned> Indices = CE->getIndices(); |
Dan Gohman | 995be7d | 2008-05-31 19:12:39 +0000 | [diff] [blame] | 1024 | for (unsigned i = 0, e = Indices.size(); i != e; ++i) |
| 1025 | Out << ", " << Indices[i]; |
| 1026 | } |
| 1027 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1028 | if (CE->isCast()) { |
Chris Lattner | 95586b8 | 2002-08-15 19:37:43 +0000 | [diff] [blame] | 1029 | Out << " to "; |
Chris Lattner | 0f7364b | 2009-02-28 21:26:53 +0000 | [diff] [blame] | 1030 | TypePrinter.print(CE->getType(), Out); |
Chris Lattner | 95586b8 | 2002-08-15 19:37:43 +0000 | [diff] [blame] | 1031 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1032 | |
Misha Brukman | 40c732c | 2004-06-04 21:11:51 +0000 | [diff] [blame] | 1033 | Out << ')'; |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1034 | return; |
Chris Lattner | 7a716ad | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 1035 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1036 | |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1037 | Out << "<placeholder or erroneous Constant>"; |
Chris Lattner | 7a716ad | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 1038 | } |
| 1039 | |
Chris Lattner | 85b1912 | 2009-12-31 02:31:59 +0000 | [diff] [blame] | 1040 | static void WriteMDNodeBodyInternal(raw_ostream &Out, const MDNode *Node, |
| 1041 | TypePrinting *TypePrinter, |
Dan Gohman | 3bdfbf5 | 2010-07-20 23:55:01 +0000 | [diff] [blame] | 1042 | SlotTracker *Machine, |
| 1043 | const Module *Context) { |
Chris Lattner | 85b1912 | 2009-12-31 02:31:59 +0000 | [diff] [blame] | 1044 | Out << "!{"; |
| 1045 | for (unsigned mi = 0, me = Node->getNumOperands(); mi != me; ++mi) { |
| 1046 | const Value *V = Node->getOperand(mi); |
| 1047 | if (V == 0) |
| 1048 | Out << "null"; |
| 1049 | else { |
| 1050 | TypePrinter->print(V->getType(), Out); |
| 1051 | Out << ' '; |
Andrew Trick | 18801ec | 2011-09-30 19:48:58 +0000 | [diff] [blame] | 1052 | WriteAsOperandInternal(Out, Node->getOperand(mi), |
Dan Gohman | 3bdfbf5 | 2010-07-20 23:55:01 +0000 | [diff] [blame] | 1053 | TypePrinter, Machine, Context); |
Chris Lattner | 85b1912 | 2009-12-31 02:31:59 +0000 | [diff] [blame] | 1054 | } |
| 1055 | if (mi + 1 != me) |
| 1056 | Out << ", "; |
| 1057 | } |
Andrew Trick | 18801ec | 2011-09-30 19:48:58 +0000 | [diff] [blame] | 1058 | |
Chris Lattner | 85b1912 | 2009-12-31 02:31:59 +0000 | [diff] [blame] | 1059 | Out << "}"; |
| 1060 | } |
| 1061 | |
Chris Lattner | 7a716ad | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 1062 | |
Misha Brukman | ab5c600 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 1063 | /// WriteAsOperand - Write the name of the specified value out to the specified |
| 1064 | /// ostream. This can be useful when you just want to print int %reg126, not |
| 1065 | /// the whole instruction that generated it. |
| 1066 | /// |
Dan Gohman | 1220e10 | 2009-08-12 20:56:03 +0000 | [diff] [blame] | 1067 | static void WriteAsOperandInternal(raw_ostream &Out, const Value *V, |
Dan Gohman | d6c0f65 | 2009-08-13 15:27:57 +0000 | [diff] [blame] | 1068 | TypePrinting *TypePrinter, |
Dan Gohman | 3bdfbf5 | 2010-07-20 23:55:01 +0000 | [diff] [blame] | 1069 | SlotTracker *Machine, |
| 1070 | const Module *Context) { |
Chris Lattner | c97536e | 2008-08-17 04:40:13 +0000 | [diff] [blame] | 1071 | if (V->hasName()) { |
| 1072 | PrintLLVMName(Out, V); |
| 1073 | return; |
| 1074 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1075 | |
Chris Lattner | c97536e | 2008-08-17 04:40:13 +0000 | [diff] [blame] | 1076 | const Constant *CV = dyn_cast<Constant>(V); |
| 1077 | if (CV && !isa<GlobalValue>(CV)) { |
Dan Gohman | d6c0f65 | 2009-08-13 15:27:57 +0000 | [diff] [blame] | 1078 | assert(TypePrinter && "Constants require TypePrinting!"); |
Dan Gohman | 3bdfbf5 | 2010-07-20 23:55:01 +0000 | [diff] [blame] | 1079 | WriteConstantInternal(Out, CV, *TypePrinter, Machine, Context); |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1080 | return; |
| 1081 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1082 | |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1083 | if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) { |
Chris Lattner | c97536e | 2008-08-17 04:40:13 +0000 | [diff] [blame] | 1084 | Out << "asm "; |
| 1085 | if (IA->hasSideEffects()) |
| 1086 | Out << "sideeffect "; |
Dale Johannesen | 8ba2d5b | 2009-10-21 23:28:00 +0000 | [diff] [blame] | 1087 | if (IA->isAlignStack()) |
| 1088 | Out << "alignstack "; |
Chad Rosier | 581600b | 2012-09-05 19:00:49 +0000 | [diff] [blame] | 1089 | // We don't emit the AD_ATT dialect as it's the assumed default. |
| 1090 | if (IA->getDialect() == InlineAsm::AD_Intel) |
| 1091 | Out << "inteldialect "; |
Chris Lattner | c97536e | 2008-08-17 04:40:13 +0000 | [diff] [blame] | 1092 | Out << '"'; |
| 1093 | PrintEscapedString(IA->getAsmString(), Out); |
| 1094 | Out << "\", \""; |
| 1095 | PrintEscapedString(IA->getConstraintString(), Out); |
| 1096 | Out << '"'; |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1097 | return; |
| 1098 | } |
Devang Patel | e54abc9 | 2009-07-22 17:43:22 +0000 | [diff] [blame] | 1099 | |
Devang Patel | 104cf9e | 2009-07-23 01:07:34 +0000 | [diff] [blame] | 1100 | if (const MDNode *N = dyn_cast<MDNode>(V)) { |
Victor Hernandez | 5d30162 | 2009-12-18 20:09:14 +0000 | [diff] [blame] | 1101 | if (N->isFunctionLocal()) { |
Victor Hernandez | 97e2450 | 2009-12-04 01:35:02 +0000 | [diff] [blame] | 1102 | // Print metadata inline, not via slot reference number. |
Dan Gohman | 3bdfbf5 | 2010-07-20 23:55:01 +0000 | [diff] [blame] | 1103 | WriteMDNodeBodyInternal(Out, N, TypePrinter, Machine, Context); |
Victor Hernandez | 97e2450 | 2009-12-04 01:35:02 +0000 | [diff] [blame] | 1104 | return; |
| 1105 | } |
Andrew Trick | 18801ec | 2011-09-30 19:48:58 +0000 | [diff] [blame] | 1106 | |
Dan Gohman | 3bdfbf5 | 2010-07-20 23:55:01 +0000 | [diff] [blame] | 1107 | if (!Machine) { |
| 1108 | if (N->isFunctionLocal()) |
| 1109 | Machine = new SlotTracker(N->getFunction()); |
| 1110 | else |
| 1111 | Machine = new SlotTracker(Context); |
| 1112 | } |
Dan Gohman | 3da076f | 2010-09-09 20:53:58 +0000 | [diff] [blame] | 1113 | int Slot = Machine->getMetadataSlot(N); |
| 1114 | if (Slot == -1) |
| 1115 | Out << "<badref>"; |
| 1116 | else |
| 1117 | Out << '!' << Slot; |
Devang Patel | 104cf9e | 2009-07-23 01:07:34 +0000 | [diff] [blame] | 1118 | return; |
| 1119 | } |
| 1120 | |
Devang Patel | e54abc9 | 2009-07-22 17:43:22 +0000 | [diff] [blame] | 1121 | if (const MDString *MDS = dyn_cast<MDString>(V)) { |
Devang Patel | e54abc9 | 2009-07-22 17:43:22 +0000 | [diff] [blame] | 1122 | Out << "!\""; |
Daniel Dunbar | 03d7651 | 2009-07-25 23:55:21 +0000 | [diff] [blame] | 1123 | PrintEscapedString(MDS->getString(), Out); |
Devang Patel | e54abc9 | 2009-07-22 17:43:22 +0000 | [diff] [blame] | 1124 | Out << '"'; |
| 1125 | return; |
| 1126 | } |
| 1127 | |
Evan Cheng | 746d546 | 2009-11-16 07:10:36 +0000 | [diff] [blame] | 1128 | if (V->getValueID() == Value::PseudoSourceValueVal || |
| 1129 | V->getValueID() == Value::FixedStackPseudoSourceValueVal) { |
Dan Gohman | cd26ec5 | 2009-09-23 01:33:16 +0000 | [diff] [blame] | 1130 | V->print(Out); |
| 1131 | return; |
| 1132 | } |
| 1133 | |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1134 | char Prefix = '%'; |
| 1135 | int Slot; |
Chris Lattner | fb5179a | 2011-08-03 06:15:41 +0000 | [diff] [blame] | 1136 | // If we have a SlotTracker, use it. |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1137 | if (Machine) { |
| 1138 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) { |
| 1139 | Slot = Machine->getGlobalSlot(GV); |
| 1140 | Prefix = '@'; |
| 1141 | } else { |
| 1142 | Slot = Machine->getLocalSlot(V); |
Andrew Trick | 18801ec | 2011-09-30 19:48:58 +0000 | [diff] [blame] | 1143 | |
Chris Lattner | fb5179a | 2011-08-03 06:15:41 +0000 | [diff] [blame] | 1144 | // If the local value didn't succeed, then we may be referring to a value |
| 1145 | // from a different function. Translate it, as this can happen when using |
| 1146 | // address of blocks. |
| 1147 | if (Slot == -1) |
| 1148 | if ((Machine = createSlotTracker(V))) { |
| 1149 | Slot = Machine->getLocalSlot(V); |
| 1150 | delete Machine; |
| 1151 | } |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1152 | } |
Chris Lattner | fb5179a | 2011-08-03 06:15:41 +0000 | [diff] [blame] | 1153 | } else if ((Machine = createSlotTracker(V))) { |
| 1154 | // Otherwise, create one to get the # and then destroy it. |
| 1155 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) { |
| 1156 | Slot = Machine->getGlobalSlot(GV); |
| 1157 | Prefix = '@'; |
Chris Lattner | 80cd115 | 2006-01-25 22:26:05 +0000 | [diff] [blame] | 1158 | } else { |
Chris Lattner | fb5179a | 2011-08-03 06:15:41 +0000 | [diff] [blame] | 1159 | Slot = Machine->getLocalSlot(V); |
Chris Lattner | 7a716ad | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 1160 | } |
Chris Lattner | fb5179a | 2011-08-03 06:15:41 +0000 | [diff] [blame] | 1161 | delete Machine; |
| 1162 | Machine = 0; |
| 1163 | } else { |
| 1164 | Slot = -1; |
Chris Lattner | 7a716ad | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 1165 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1166 | |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1167 | if (Slot != -1) |
| 1168 | Out << Prefix << Slot; |
| 1169 | else |
| 1170 | Out << "<badref>"; |
Chris Lattner | 7a716ad | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 1171 | } |
| 1172 | |
Daniel Malea | d0fef32 | 2013-05-08 20:38:31 +0000 | [diff] [blame^] | 1173 | void WriteAsOperand(raw_ostream &Out, const Value *V, |
| 1174 | bool PrintType, const Module *Context) { |
Dan Gohman | d6c0f65 | 2009-08-13 15:27:57 +0000 | [diff] [blame] | 1175 | |
| 1176 | // Fast path: Don't construct and populate a TypePrinting object if we |
| 1177 | // won't be needing any types printed. |
Dan Gohman | 009fc9e | 2009-08-13 23:07:11 +0000 | [diff] [blame] | 1178 | if (!PrintType && |
Dan Gohman | 3bdfbf5 | 2010-07-20 23:55:01 +0000 | [diff] [blame] | 1179 | ((!isa<Constant>(V) && !isa<MDNode>(V)) || |
| 1180 | V->hasName() || isa<GlobalValue>(V))) { |
| 1181 | WriteAsOperandInternal(Out, V, 0, 0, Context); |
Dan Gohman | d6c0f65 | 2009-08-13 15:27:57 +0000 | [diff] [blame] | 1182 | return; |
| 1183 | } |
| 1184 | |
Chris Lattner | 607dc68 | 2002-07-10 16:48:17 +0000 | [diff] [blame] | 1185 | if (Context == 0) Context = getModuleFromVal(V); |
Chris Lattner | 207b5bc | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 1186 | |
Chris Lattner | e9fa33e | 2009-02-28 23:20:19 +0000 | [diff] [blame] | 1187 | TypePrinting TypePrinter; |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1188 | if (Context) |
| 1189 | TypePrinter.incorporateTypes(*Context); |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1190 | if (PrintType) { |
Chris Lattner | 0f7364b | 2009-02-28 21:26:53 +0000 | [diff] [blame] | 1191 | TypePrinter.print(V->getType(), Out); |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1192 | Out << ' '; |
| 1193 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1194 | |
Dan Gohman | 3bdfbf5 | 2010-07-20 23:55:01 +0000 | [diff] [blame] | 1195 | WriteAsOperandInternal(Out, V, &TypePrinter, 0, Context); |
Chris Lattner | 622f740 | 2001-07-20 19:15:21 +0000 | [diff] [blame] | 1196 | } |
| 1197 | |
Daniel Malea | d0fef32 | 2013-05-08 20:38:31 +0000 | [diff] [blame^] | 1198 | void AssemblyWriter::init() { |
| 1199 | if (TheModule) |
| 1200 | TypePrinter.incorporateTypes(*TheModule); |
| 1201 | } |
Chris Lattner | d8c2e42 | 2001-07-12 23:35:26 +0000 | [diff] [blame] | 1202 | |
Andrew Trick | 18801ec | 2011-09-30 19:48:58 +0000 | [diff] [blame] | 1203 | |
Daniel Malea | d0fef32 | 2013-05-08 20:38:31 +0000 | [diff] [blame^] | 1204 | AssemblyWriter::AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac, |
| 1205 | const Module *M, |
| 1206 | AssemblyAnnotationWriter *AAW) |
| 1207 | : Out(o), TheModule(M), Machine(Mac), AnnotationWriter(AAW) { |
| 1208 | init(); |
| 1209 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1210 | |
Daniel Malea | d0fef32 | 2013-05-08 20:38:31 +0000 | [diff] [blame^] | 1211 | AssemblyWriter::AssemblyWriter(formatted_raw_ostream &o, const Module *M, |
| 1212 | AssemblyAnnotationWriter *AAW) |
| 1213 | : Out(o), TheModule(M), ModuleSlotTracker(createSlotTracker(M)), |
| 1214 | Machine(*ModuleSlotTracker), AnnotationWriter(AAW) { |
| 1215 | init(); |
| 1216 | } |
Andrew Trick | 18801ec | 2011-09-30 19:48:58 +0000 | [diff] [blame] | 1217 | |
Daniel Malea | d0fef32 | 2013-05-08 20:38:31 +0000 | [diff] [blame^] | 1218 | AssemblyWriter::~AssemblyWriter() { } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1219 | |
Chris Lattner | 2fcfdb7 | 2006-12-06 06:24:27 +0000 | [diff] [blame] | 1220 | void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType) { |
| 1221 | if (Operand == 0) { |
Chris Lattner | aab1820 | 2005-02-24 16:58:29 +0000 | [diff] [blame] | 1222 | Out << "<null operand!>"; |
Chris Lattner | c65b72c | 2009-12-31 02:33:14 +0000 | [diff] [blame] | 1223 | return; |
Chris Lattner | aab1820 | 2005-02-24 16:58:29 +0000 | [diff] [blame] | 1224 | } |
Chris Lattner | c65b72c | 2009-12-31 02:33:14 +0000 | [diff] [blame] | 1225 | if (PrintType) { |
| 1226 | TypePrinter.print(Operand->getType(), Out); |
| 1227 | Out << ' '; |
| 1228 | } |
Dan Gohman | 3bdfbf5 | 2010-07-20 23:55:01 +0000 | [diff] [blame] | 1229 | WriteAsOperandInternal(Out, Operand, &TypePrinter, &Machine, TheModule); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1230 | } |
| 1231 | |
Eli Friedman | 47f3513 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 1232 | void AssemblyWriter::writeAtomic(AtomicOrdering Ordering, |
| 1233 | SynchronizationScope SynchScope) { |
| 1234 | if (Ordering == NotAtomic) |
| 1235 | return; |
| 1236 | |
| 1237 | switch (SynchScope) { |
Eli Friedman | 47f3513 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 1238 | case SingleThread: Out << " singlethread"; break; |
| 1239 | case CrossThread: break; |
| 1240 | } |
| 1241 | |
| 1242 | switch (Ordering) { |
| 1243 | default: Out << " <bad ordering " << int(Ordering) << ">"; break; |
| 1244 | case Unordered: Out << " unordered"; break; |
| 1245 | case Monotonic: Out << " monotonic"; break; |
| 1246 | case Acquire: Out << " acquire"; break; |
| 1247 | case Release: Out << " release"; break; |
| 1248 | case AcquireRelease: Out << " acq_rel"; break; |
| 1249 | case SequentiallyConsistent: Out << " seq_cst"; break; |
| 1250 | } |
| 1251 | } |
| 1252 | |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1253 | void AssemblyWriter::writeParamOperand(const Value *Operand, |
Bill Wendling | 94e94b3 | 2012-12-30 13:50:49 +0000 | [diff] [blame] | 1254 | AttributeSet Attrs, unsigned Idx) { |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 1255 | if (Operand == 0) { |
| 1256 | Out << "<null operand!>"; |
Chris Lattner | c65b72c | 2009-12-31 02:33:14 +0000 | [diff] [blame] | 1257 | return; |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 1258 | } |
Chris Lattner | c65b72c | 2009-12-31 02:33:14 +0000 | [diff] [blame] | 1259 | |
| 1260 | // Print the type |
| 1261 | TypePrinter.print(Operand->getType(), Out); |
| 1262 | // Print parameter attributes list |
Bill Wendling | 94e94b3 | 2012-12-30 13:50:49 +0000 | [diff] [blame] | 1263 | if (Attrs.hasAttributes(Idx)) |
| 1264 | Out << ' ' << Attrs.getAsString(Idx); |
Chris Lattner | c65b72c | 2009-12-31 02:33:14 +0000 | [diff] [blame] | 1265 | Out << ' '; |
| 1266 | // Print the operand |
Dan Gohman | 3bdfbf5 | 2010-07-20 23:55:01 +0000 | [diff] [blame] | 1267 | WriteAsOperandInternal(Out, Operand, &TypePrinter, &Machine, TheModule); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 1268 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1269 | |
Chris Lattner | c182499 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 1270 | void AssemblyWriter::printModule(const Module *M) { |
Bill Wendling | b29ce26 | 2013-02-11 08:43:33 +0000 | [diff] [blame] | 1271 | Machine.initialize(); |
| 1272 | |
Chris Lattner | 31ab1b3 | 2005-03-02 23:12:40 +0000 | [diff] [blame] | 1273 | if (!M->getModuleIdentifier().empty() && |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1274 | // Don't print the ID if it will start a new line (which would |
Chris Lattner | 31ab1b3 | 2005-03-02 23:12:40 +0000 | [diff] [blame] | 1275 | // require a comment char before it). |
| 1276 | M->getModuleIdentifier().find('\n') == std::string::npos) |
| 1277 | Out << "; ModuleID = '" << M->getModuleIdentifier() << "'\n"; |
| 1278 | |
Owen Anderson | cf7ff2b | 2006-10-18 02:21:12 +0000 | [diff] [blame] | 1279 | if (!M->getDataLayout().empty()) |
Chris Lattner | d2f9e60 | 2006-10-22 06:06:56 +0000 | [diff] [blame] | 1280 | Out << "target datalayout = \"" << M->getDataLayout() << "\"\n"; |
Reid Spencer | cddc86f | 2004-07-25 21:44:54 +0000 | [diff] [blame] | 1281 | if (!M->getTargetTriple().empty()) |
Reid Spencer | c9a1f0d | 2004-07-25 21:29:43 +0000 | [diff] [blame] | 1282 | Out << "target triple = \"" << M->getTargetTriple() << "\"\n"; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1283 | |
Chris Lattner | cc041ba | 2006-01-24 04:13:11 +0000 | [diff] [blame] | 1284 | if (!M->getModuleInlineAsm().empty()) { |
Chris Lattner | 42a162e | 2006-01-24 00:45:30 +0000 | [diff] [blame] | 1285 | // Split the string into lines, to make it easier to read the .ll file. |
Chris Lattner | cc041ba | 2006-01-24 04:13:11 +0000 | [diff] [blame] | 1286 | std::string Asm = M->getModuleInlineAsm(); |
Chris Lattner | 42a162e | 2006-01-24 00:45:30 +0000 | [diff] [blame] | 1287 | size_t CurPos = 0; |
| 1288 | size_t NewLine = Asm.find_first_of('\n', CurPos); |
Dan Gohman | 9bf0b9b | 2009-08-12 23:54:22 +0000 | [diff] [blame] | 1289 | Out << '\n'; |
Chris Lattner | 42a162e | 2006-01-24 00:45:30 +0000 | [diff] [blame] | 1290 | while (NewLine != std::string::npos) { |
| 1291 | // We found a newline, print the portion of the asm string from the |
| 1292 | // last newline up to this newline. |
| 1293 | Out << "module asm \""; |
| 1294 | PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.begin()+NewLine), |
| 1295 | Out); |
| 1296 | Out << "\"\n"; |
| 1297 | CurPos = NewLine+1; |
| 1298 | NewLine = Asm.find_first_of('\n', CurPos); |
| 1299 | } |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 1300 | std::string rest(Asm.begin()+CurPos, Asm.end()); |
| 1301 | if (!rest.empty()) { |
| 1302 | Out << "module asm \""; |
| 1303 | PrintEscapedString(rest, Out); |
| 1304 | Out << "\"\n"; |
| 1305 | } |
Chris Lattner | 1836550 | 2006-01-23 23:03:36 +0000 | [diff] [blame] | 1306 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1307 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1308 | printTypeIdentities(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1309 | |
Dan Gohman | 9bf0b9b | 2009-08-12 23:54:22 +0000 | [diff] [blame] | 1310 | // Output all globals. |
| 1311 | if (!M->global_empty()) Out << '\n'; |
Chris Lattner | d6d826c | 2006-12-06 04:41:52 +0000 | [diff] [blame] | 1312 | for (Module::const_global_iterator I = M->global_begin(), E = M->global_end(); |
Duncan Sands | 79da6ef | 2012-09-12 09:55:51 +0000 | [diff] [blame] | 1313 | I != E; ++I) { |
| 1314 | printGlobal(I); Out << '\n'; |
| 1315 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1316 | |
Chris Lattner | 69dacfc | 2007-04-26 02:24:10 +0000 | [diff] [blame] | 1317 | // Output all aliases. |
| 1318 | if (!M->alias_empty()) Out << "\n"; |
| 1319 | for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end(); |
| 1320 | I != E; ++I) |
| 1321 | printAlias(I); |
Chris Lattner | 007377f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 1322 | |
Chris Lattner | 44da7d7 | 2004-09-14 05:06:58 +0000 | [diff] [blame] | 1323 | // Output all of the functions. |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1324 | for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) |
| 1325 | printFunction(I); |
Devang Patel | 320671d | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 1326 | |
Bill Wendling | b29ce26 | 2013-02-11 08:43:33 +0000 | [diff] [blame] | 1327 | // Output all attribute groups. |
Bill Wendling | 725dae5 | 2013-04-29 23:48:06 +0000 | [diff] [blame] | 1328 | if (!Machine.as_empty()) { |
Bill Wendling | b29ce26 | 2013-02-11 08:43:33 +0000 | [diff] [blame] | 1329 | Out << '\n'; |
| 1330 | writeAllAttributeGroups(); |
| 1331 | } |
| 1332 | |
Devang Patel | 37c4a2d | 2009-07-29 22:04:47 +0000 | [diff] [blame] | 1333 | // Output named metadata. |
Dan Gohman | 9bf0b9b | 2009-08-12 23:54:22 +0000 | [diff] [blame] | 1334 | if (!M->named_metadata_empty()) Out << '\n'; |
Andrew Trick | 18801ec | 2011-09-30 19:48:58 +0000 | [diff] [blame] | 1335 | |
Devang Patel | 37c4a2d | 2009-07-29 22:04:47 +0000 | [diff] [blame] | 1336 | for (Module::const_named_metadata_iterator I = M->named_metadata_begin(), |
Chris Lattner | 6e6b180 | 2009-12-31 02:13:35 +0000 | [diff] [blame] | 1337 | E = M->named_metadata_end(); I != E; ++I) |
Chris Lattner | fdb3356 | 2009-12-31 01:54:05 +0000 | [diff] [blame] | 1338 | printNamedMDNode(I); |
Devang Patel | 37c4a2d | 2009-07-29 22:04:47 +0000 | [diff] [blame] | 1339 | |
| 1340 | // Output metadata. |
Chris Lattner | 307c989 | 2009-12-31 02:20:11 +0000 | [diff] [blame] | 1341 | if (!Machine.mdn_empty()) { |
Chris Lattner | 6e6b180 | 2009-12-31 02:13:35 +0000 | [diff] [blame] | 1342 | Out << '\n'; |
| 1343 | writeAllMDNodes(); |
| 1344 | } |
Chris Lattner | 007377f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 1345 | } |
| 1346 | |
Chris Lattner | fdb3356 | 2009-12-31 01:54:05 +0000 | [diff] [blame] | 1347 | void AssemblyWriter::printNamedMDNode(const NamedMDNode *NMD) { |
Nick Lewycky | 9100a78 | 2011-06-15 06:37:58 +0000 | [diff] [blame] | 1348 | Out << '!'; |
| 1349 | StringRef Name = NMD->getName(); |
| 1350 | if (Name.empty()) { |
| 1351 | Out << "<empty name> "; |
| 1352 | } else { |
Guy Benyei | 87d0b9e | 2013-02-12 21:21:59 +0000 | [diff] [blame] | 1353 | if (isalpha(static_cast<unsigned char>(Name[0])) || |
| 1354 | Name[0] == '-' || Name[0] == '$' || |
Nick Lewycky | 9100a78 | 2011-06-15 06:37:58 +0000 | [diff] [blame] | 1355 | Name[0] == '.' || Name[0] == '_') |
| 1356 | Out << Name[0]; |
| 1357 | else |
| 1358 | Out << '\\' << hexdigit(Name[0] >> 4) << hexdigit(Name[0] & 0x0F); |
| 1359 | for (unsigned i = 1, e = Name.size(); i != e; ++i) { |
| 1360 | unsigned char C = Name[i]; |
Guy Benyei | 87d0b9e | 2013-02-12 21:21:59 +0000 | [diff] [blame] | 1361 | if (isalnum(static_cast<unsigned char>(C)) || C == '-' || C == '$' || |
| 1362 | C == '.' || C == '_') |
Nick Lewycky | 9100a78 | 2011-06-15 06:37:58 +0000 | [diff] [blame] | 1363 | Out << C; |
| 1364 | else |
| 1365 | Out << '\\' << hexdigit(C >> 4) << hexdigit(C & 0x0F); |
| 1366 | } |
| 1367 | } |
| 1368 | Out << " = !{"; |
Chris Lattner | fdb3356 | 2009-12-31 01:54:05 +0000 | [diff] [blame] | 1369 | for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) { |
| 1370 | if (i) Out << ", "; |
Dan Gohman | 3da076f | 2010-09-09 20:53:58 +0000 | [diff] [blame] | 1371 | int Slot = Machine.getMetadataSlot(NMD->getOperand(i)); |
| 1372 | if (Slot == -1) |
| 1373 | Out << "<badref>"; |
| 1374 | else |
| 1375 | Out << '!' << Slot; |
Chris Lattner | fdb3356 | 2009-12-31 01:54:05 +0000 | [diff] [blame] | 1376 | } |
| 1377 | Out << "}\n"; |
| 1378 | } |
| 1379 | |
| 1380 | |
Dan Gohman | 683e922 | 2009-08-12 17:23:50 +0000 | [diff] [blame] | 1381 | static void PrintLinkage(GlobalValue::LinkageTypes LT, |
| 1382 | formatted_raw_ostream &Out) { |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1383 | switch (LT) { |
Bill Wendling | 3d10a5a | 2009-07-20 01:03:30 +0000 | [diff] [blame] | 1384 | case GlobalValue::ExternalLinkage: break; |
| 1385 | case GlobalValue::PrivateLinkage: Out << "private "; break; |
| 1386 | case GlobalValue::LinkerPrivateLinkage: Out << "linker_private "; break; |
Bill Wendling | 5e721d7 | 2010-07-01 21:55:59 +0000 | [diff] [blame] | 1387 | case GlobalValue::LinkerPrivateWeakLinkage: |
| 1388 | Out << "linker_private_weak "; |
| 1389 | break; |
Bill Wendling | 3d10a5a | 2009-07-20 01:03:30 +0000 | [diff] [blame] | 1390 | case GlobalValue::InternalLinkage: Out << "internal "; break; |
| 1391 | case GlobalValue::LinkOnceAnyLinkage: Out << "linkonce "; break; |
| 1392 | case GlobalValue::LinkOnceODRLinkage: Out << "linkonce_odr "; break; |
Bill Wendling | 32811be | 2012-08-17 18:33:14 +0000 | [diff] [blame] | 1393 | case GlobalValue::LinkOnceODRAutoHideLinkage: |
| 1394 | Out << "linkonce_odr_auto_hide "; |
| 1395 | break; |
Bill Wendling | 3d10a5a | 2009-07-20 01:03:30 +0000 | [diff] [blame] | 1396 | case GlobalValue::WeakAnyLinkage: Out << "weak "; break; |
| 1397 | case GlobalValue::WeakODRLinkage: Out << "weak_odr "; break; |
| 1398 | case GlobalValue::CommonLinkage: Out << "common "; break; |
| 1399 | case GlobalValue::AppendingLinkage: Out << "appending "; break; |
| 1400 | case GlobalValue::DLLImportLinkage: Out << "dllimport "; break; |
| 1401 | case GlobalValue::DLLExportLinkage: Out << "dllexport "; break; |
| 1402 | case GlobalValue::ExternalWeakLinkage: Out << "extern_weak "; break; |
Chris Lattner | 266c7bb | 2009-04-13 05:44:34 +0000 | [diff] [blame] | 1403 | case GlobalValue::AvailableExternallyLinkage: |
| 1404 | Out << "available_externally "; |
| 1405 | break; |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1406 | } |
| 1407 | } |
Duncan Sands | 667d4b8 | 2009-03-07 15:45:40 +0000 | [diff] [blame] | 1408 | |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1409 | |
| 1410 | static void PrintVisibility(GlobalValue::VisibilityTypes Vis, |
Dan Gohman | 683e922 | 2009-08-12 17:23:50 +0000 | [diff] [blame] | 1411 | formatted_raw_ostream &Out) { |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1412 | switch (Vis) { |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1413 | case GlobalValue::DefaultVisibility: break; |
| 1414 | case GlobalValue::HiddenVisibility: Out << "hidden "; break; |
| 1415 | case GlobalValue::ProtectedVisibility: Out << "protected "; break; |
| 1416 | } |
| 1417 | } |
| 1418 | |
Hans Wennborg | ce718ff | 2012-06-23 11:37:03 +0000 | [diff] [blame] | 1419 | static void PrintThreadLocalModel(GlobalVariable::ThreadLocalMode TLM, |
| 1420 | formatted_raw_ostream &Out) { |
| 1421 | switch (TLM) { |
| 1422 | case GlobalVariable::NotThreadLocal: |
| 1423 | break; |
| 1424 | case GlobalVariable::GeneralDynamicTLSModel: |
| 1425 | Out << "thread_local "; |
| 1426 | break; |
| 1427 | case GlobalVariable::LocalDynamicTLSModel: |
| 1428 | Out << "thread_local(localdynamic) "; |
| 1429 | break; |
| 1430 | case GlobalVariable::InitialExecTLSModel: |
| 1431 | Out << "thread_local(initialexec) "; |
| 1432 | break; |
| 1433 | case GlobalVariable::LocalExecTLSModel: |
| 1434 | Out << "thread_local(localexec) "; |
| 1435 | break; |
| 1436 | } |
| 1437 | } |
| 1438 | |
Chris Lattner | c182499 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 1439 | void AssemblyWriter::printGlobal(const GlobalVariable *GV) { |
Dan Gohman | 4483c7b | 2010-01-29 23:12:36 +0000 | [diff] [blame] | 1440 | if (GV->isMaterializable()) |
| 1441 | Out << "; Materializable\n"; |
| 1442 | |
Dan Gohman | 3bdfbf5 | 2010-07-20 23:55:01 +0000 | [diff] [blame] | 1443 | WriteAsOperandInternal(Out, GV, &TypePrinter, &Machine, GV->getParent()); |
Dan Gohman | 3845e50 | 2009-08-12 23:32:33 +0000 | [diff] [blame] | 1444 | Out << " = "; |
Chris Lattner | d70684f | 2001-09-18 04:01:05 +0000 | [diff] [blame] | 1445 | |
Chris Lattner | 52b26de | 2008-08-19 05:16:28 +0000 | [diff] [blame] | 1446 | if (!GV->hasInitializer() && GV->hasExternalLinkage()) |
| 1447 | Out << "external "; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1448 | |
Chris Lattner | 52b26de | 2008-08-19 05:16:28 +0000 | [diff] [blame] | 1449 | PrintLinkage(GV->getLinkage(), Out); |
| 1450 | PrintVisibility(GV->getVisibility(), Out); |
Hans Wennborg | ce718ff | 2012-06-23 11:37:03 +0000 | [diff] [blame] | 1451 | PrintThreadLocalModel(GV->getThreadLocalMode(), Out); |
Lauro Ramos Venancio | c763552 | 2007-04-12 18:32:50 +0000 | [diff] [blame] | 1452 | |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1453 | if (unsigned AddressSpace = GV->getType()->getAddressSpace()) |
| 1454 | Out << "addrspace(" << AddressSpace << ") "; |
Rafael Espindola | bea4626 | 2011-01-08 16:42:36 +0000 | [diff] [blame] | 1455 | if (GV->hasUnnamedAddr()) Out << "unnamed_addr "; |
Michael Gottesman | a2de37c | 2013-02-05 05:57:38 +0000 | [diff] [blame] | 1456 | if (GV->isExternallyInitialized()) Out << "externally_initialized "; |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1457 | Out << (GV->isConstant() ? "constant " : "global "); |
Chris Lattner | 0f7364b | 2009-02-28 21:26:53 +0000 | [diff] [blame] | 1458 | TypePrinter.print(GV->getType()->getElementType(), Out); |
Chris Lattner | d70684f | 2001-09-18 04:01:05 +0000 | [diff] [blame] | 1459 | |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1460 | if (GV->hasInitializer()) { |
| 1461 | Out << ' '; |
Devang Patel | 320671d | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 1462 | writeOperand(GV->getInitializer(), false); |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1463 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1464 | |
Chris Lattner | 8fff126 | 2010-07-07 23:16:37 +0000 | [diff] [blame] | 1465 | if (GV->hasSection()) { |
| 1466 | Out << ", section \""; |
| 1467 | PrintEscapedString(GV->getSection(), Out); |
| 1468 | Out << '"'; |
| 1469 | } |
Chris Lattner | 60962db | 2005-11-12 00:10:19 +0000 | [diff] [blame] | 1470 | if (GV->getAlignment()) |
Chris Lattner | 30caa28 | 2005-11-06 06:48:53 +0000 | [diff] [blame] | 1471 | Out << ", align " << GV->getAlignment(); |
Anton Korobeynikov | 8b0a8c8 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 1472 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1473 | printInfoComment(*GV); |
Chris Lattner | 70cc339 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 1474 | } |
| 1475 | |
Anton Korobeynikov | 8b0a8c8 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 1476 | void AssemblyWriter::printAlias(const GlobalAlias *GA) { |
Dan Gohman | 4483c7b | 2010-01-29 23:12:36 +0000 | [diff] [blame] | 1477 | if (GA->isMaterializable()) |
| 1478 | Out << "; Materializable\n"; |
| 1479 | |
Dale Johannesen | 24f07dc | 2008-06-03 18:14:29 +0000 | [diff] [blame] | 1480 | // Don't crash when dumping partially built GA |
| 1481 | if (!GA->hasName()) |
| 1482 | Out << "<<nameless>> = "; |
Chris Lattner | c97536e | 2008-08-17 04:40:13 +0000 | [diff] [blame] | 1483 | else { |
| 1484 | PrintLLVMName(Out, GA); |
| 1485 | Out << " = "; |
| 1486 | } |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1487 | PrintVisibility(GA->getVisibility(), Out); |
Anton Korobeynikov | 8b0a8c8 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 1488 | |
| 1489 | Out << "alias "; |
| 1490 | |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1491 | PrintLinkage(GA->getLinkage(), Out); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1492 | |
Anton Korobeynikov | c6c98af | 2007-04-29 18:02:48 +0000 | [diff] [blame] | 1493 | const Constant *Aliasee = GA->getAliasee(); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1494 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1495 | if (Aliasee == 0) { |
| 1496 | TypePrinter.print(GA->getType(), Out); |
| 1497 | Out << " <<NULL ALIASEE>>"; |
Jay Foad | 5cd8ea2 | 2011-08-01 12:48:54 +0000 | [diff] [blame] | 1498 | } else { |
Jay Foad | 8d94865 | 2011-08-01 12:29:14 +0000 | [diff] [blame] | 1499 | writeOperand(Aliasee, !isa<ConstantExpr>(Aliasee)); |
Jay Foad | 5cd8ea2 | 2011-08-01 12:48:54 +0000 | [diff] [blame] | 1500 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1501 | |
Anton Korobeynikov | 8b0a8c8 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 1502 | printInfoComment(*GA); |
Chris Lattner | 52b26de | 2008-08-19 05:16:28 +0000 | [diff] [blame] | 1503 | Out << '\n'; |
Anton Korobeynikov | 8b0a8c8 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 1504 | } |
| 1505 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1506 | void AssemblyWriter::printTypeIdentities() { |
| 1507 | if (TypePrinter.NumberedTypes.empty() && |
| 1508 | TypePrinter.NamedTypes.empty()) |
| 1509 | return; |
Andrew Trick | 18801ec | 2011-09-30 19:48:58 +0000 | [diff] [blame] | 1510 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1511 | Out << '\n'; |
Andrew Trick | 18801ec | 2011-09-30 19:48:58 +0000 | [diff] [blame] | 1512 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1513 | // We know all the numbers that each type is used and we know that it is a |
| 1514 | // dense assignment. Convert the map to an index table. |
| 1515 | std::vector<StructType*> NumberedTypes(TypePrinter.NumberedTypes.size()); |
Andrew Trick | 18801ec | 2011-09-30 19:48:58 +0000 | [diff] [blame] | 1516 | for (DenseMap<StructType*, unsigned>::iterator I = |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1517 | TypePrinter.NumberedTypes.begin(), E = TypePrinter.NumberedTypes.end(); |
| 1518 | I != E; ++I) { |
| 1519 | assert(I->second < NumberedTypes.size() && "Didn't get a dense numbering?"); |
| 1520 | NumberedTypes[I->second] = I->first; |
| 1521 | } |
Andrew Trick | 18801ec | 2011-09-30 19:48:58 +0000 | [diff] [blame] | 1522 | |
Chris Lattner | 413fd23 | 2009-03-01 00:03:38 +0000 | [diff] [blame] | 1523 | // Emit all numbered types. |
| 1524 | for (unsigned i = 0, e = NumberedTypes.size(); i != e; ++i) { |
Dan Gohman | 3845e50 | 2009-08-12 23:32:33 +0000 | [diff] [blame] | 1525 | Out << '%' << i << " = type "; |
Andrew Trick | 18801ec | 2011-09-30 19:48:58 +0000 | [diff] [blame] | 1526 | |
Chris Lattner | 413fd23 | 2009-03-01 00:03:38 +0000 | [diff] [blame] | 1527 | // Make sure we print out at least one level of the type structure, so |
| 1528 | // that we do not get %2 = type %2 |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1529 | TypePrinter.printStructBody(NumberedTypes[i], Out); |
Dan Gohman | 9bf0b9b | 2009-08-12 23:54:22 +0000 | [diff] [blame] | 1530 | Out << '\n'; |
Chris Lattner | 413fd23 | 2009-03-01 00:03:38 +0000 | [diff] [blame] | 1531 | } |
Andrew Trick | 18801ec | 2011-09-30 19:48:58 +0000 | [diff] [blame] | 1532 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1533 | for (unsigned i = 0, e = TypePrinter.NamedTypes.size(); i != e; ++i) { |
| 1534 | PrintLLVMName(Out, TypePrinter.NamedTypes[i]->getName(), LocalPrefix); |
Chris Lattner | 52b26de | 2008-08-19 05:16:28 +0000 | [diff] [blame] | 1535 | Out << " = type "; |
Reid Spencer | 9231ac8 | 2004-05-25 08:53:40 +0000 | [diff] [blame] | 1536 | |
| 1537 | // Make sure we print out at least one level of the type structure, so |
| 1538 | // that we do not get %FILE = type %FILE |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1539 | TypePrinter.printStructBody(TypePrinter.NamedTypes[i], Out); |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1540 | Out << '\n'; |
Reid Spencer | 9231ac8 | 2004-05-25 08:53:40 +0000 | [diff] [blame] | 1541 | } |
Reid Spencer | 78d033e | 2007-01-06 07:24:44 +0000 | [diff] [blame] | 1542 | } |
| 1543 | |
Misha Brukman | ab5c600 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 1544 | /// printFunction - Print all aspects of a function. |
| 1545 | /// |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1546 | void AssemblyWriter::printFunction(const Function *F) { |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1547 | // Print out the return type and name. |
| 1548 | Out << '\n'; |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 1549 | |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1550 | if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, Out); |
Chris Lattner | 95e5a2c | 2003-10-30 23:41:03 +0000 | [diff] [blame] | 1551 | |
Dan Gohman | 4483c7b | 2010-01-29 23:12:36 +0000 | [diff] [blame] | 1552 | if (F->isMaterializable()) |
| 1553 | Out << "; Materializable\n"; |
| 1554 | |
Bill Wendling | 6340549 | 2013-04-16 20:55:47 +0000 | [diff] [blame] | 1555 | const AttributeSet &Attrs = F->getAttributes(); |
Bill Wendling | 725dae5 | 2013-04-29 23:48:06 +0000 | [diff] [blame] | 1556 | if (Attrs.hasAttributes(AttributeSet::FunctionIndex)) { |
Bill Wendling | 6340549 | 2013-04-16 20:55:47 +0000 | [diff] [blame] | 1557 | AttributeSet AS = Attrs.getFnAttributes(); |
Rafael Espindola | aae0298 | 2013-05-01 13:07:03 +0000 | [diff] [blame] | 1558 | std::string AttrStr; |
| 1559 | |
| 1560 | unsigned Idx = 0; |
| 1561 | for (unsigned E = AS.getNumSlots(); Idx != E; ++Idx) |
| 1562 | if (AS.getSlotIndex(Idx) == AttributeSet::FunctionIndex) |
| 1563 | break; |
| 1564 | |
| 1565 | for (AttributeSet::iterator I = AS.begin(Idx), E = AS.end(Idx); |
| 1566 | I != E; ++I) { |
| 1567 | Attribute Attr = *I; |
| 1568 | if (!Attr.isStringAttribute()) { |
| 1569 | if (!AttrStr.empty()) AttrStr += ' '; |
| 1570 | AttrStr += Attr.getAsString(); |
| 1571 | } |
| 1572 | } |
| 1573 | |
Bill Wendling | 6340549 | 2013-04-16 20:55:47 +0000 | [diff] [blame] | 1574 | if (!AttrStr.empty()) |
| 1575 | Out << "; Function Attrs: " << AttrStr << '\n'; |
| 1576 | } |
| 1577 | |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 1578 | if (F->isDeclaration()) |
Chris Lattner | 3aa6066 | 2007-08-19 22:15:26 +0000 | [diff] [blame] | 1579 | Out << "declare "; |
| 1580 | else |
Reid Spencer | b951bc0 | 2006-12-29 20:29:48 +0000 | [diff] [blame] | 1581 | Out << "define "; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1582 | |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1583 | PrintLinkage(F->getLinkage(), Out); |
| 1584 | PrintVisibility(F->getVisibility(), Out); |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 1585 | |
Chris Lattner | d511898 | 2005-05-06 20:26:43 +0000 | [diff] [blame] | 1586 | // Print the calling convention. |
Micah Villmow | d3766df | 2012-09-13 15:11:12 +0000 | [diff] [blame] | 1587 | if (F->getCallingConv() != CallingConv::C) { |
| 1588 | PrintCallingConv(F->getCallingConv(), Out); |
| 1589 | Out << " "; |
Chris Lattner | d511898 | 2005-05-06 20:26:43 +0000 | [diff] [blame] | 1590 | } |
| 1591 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1592 | FunctionType *FT = F->getFunctionType(); |
Bill Wendling | 1b0c54f | 2013-01-18 21:53:16 +0000 | [diff] [blame] | 1593 | if (Attrs.hasAttributes(AttributeSet::ReturnIndex)) |
| 1594 | Out << Attrs.getAsString(AttributeSet::ReturnIndex) << ' '; |
Chris Lattner | 0f7364b | 2009-02-28 21:26:53 +0000 | [diff] [blame] | 1595 | TypePrinter.print(F->getReturnType(), Out); |
Chris Lattner | 4667b71 | 2008-08-19 05:26:17 +0000 | [diff] [blame] | 1596 | Out << ' '; |
Dan Gohman | 3bdfbf5 | 2010-07-20 23:55:01 +0000 | [diff] [blame] | 1597 | WriteAsOperandInternal(Out, F, &TypePrinter, &Machine, F->getParent()); |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1598 | Out << '('; |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1599 | Machine.incorporateFunction(F); |
Chris Lattner | 007377f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 1600 | |
Chris Lattner | c182499 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 1601 | // Loop over the arguments, printing them... |
Chris Lattner | 007377f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 1602 | |
Reid Spencer | bd5db8e | 2006-12-31 05:24:50 +0000 | [diff] [blame] | 1603 | unsigned Idx = 1; |
Chris Lattner | 8dcd2f1 | 2007-04-18 00:57:22 +0000 | [diff] [blame] | 1604 | if (!F->isDeclaration()) { |
| 1605 | // If this isn't a declaration, print the argument names as well. |
| 1606 | for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end(); |
| 1607 | I != E; ++I) { |
| 1608 | // Insert commas as we go... the first arg doesn't get a comma |
| 1609 | if (I != F->arg_begin()) Out << ", "; |
Bill Wendling | 94e94b3 | 2012-12-30 13:50:49 +0000 | [diff] [blame] | 1610 | printArgument(I, Attrs, Idx); |
Chris Lattner | 8dcd2f1 | 2007-04-18 00:57:22 +0000 | [diff] [blame] | 1611 | Idx++; |
| 1612 | } |
| 1613 | } else { |
| 1614 | // Otherwise, print the types from the function type. |
| 1615 | for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) { |
| 1616 | // Insert commas as we go... the first arg doesn't get a comma |
| 1617 | if (i) Out << ", "; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1618 | |
Chris Lattner | 8dcd2f1 | 2007-04-18 00:57:22 +0000 | [diff] [blame] | 1619 | // Output type... |
Chris Lattner | 0f7364b | 2009-02-28 21:26:53 +0000 | [diff] [blame] | 1620 | TypePrinter.print(FT->getParamType(i), Out); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1621 | |
Bill Wendling | 94e94b3 | 2012-12-30 13:50:49 +0000 | [diff] [blame] | 1622 | if (Attrs.hasAttributes(i+1)) |
| 1623 | Out << ' ' << Attrs.getAsString(i+1); |
Chris Lattner | 8dcd2f1 | 2007-04-18 00:57:22 +0000 | [diff] [blame] | 1624 | } |
Reid Spencer | bd5db8e | 2006-12-31 05:24:50 +0000 | [diff] [blame] | 1625 | } |
Chris Lattner | 007377f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 1626 | |
| 1627 | // Finish printing arguments... |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1628 | if (FT->isVarArg()) { |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1629 | if (FT->getNumParams()) Out << ", "; |
| 1630 | Out << "..."; // Output varargs portion of signature! |
Chris Lattner | 007377f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 1631 | } |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1632 | Out << ')'; |
Rafael Espindola | 3971df5 | 2011-01-25 19:09:56 +0000 | [diff] [blame] | 1633 | if (F->hasUnnamedAddr()) |
| 1634 | Out << " unnamed_addr"; |
Bill Wendling | 725dae5 | 2013-04-29 23:48:06 +0000 | [diff] [blame] | 1635 | if (Attrs.hasAttributes(AttributeSet::FunctionIndex)) |
| 1636 | Out << " #" << Machine.getAttributeGroupSlot(Attrs.getFnAttributes()); |
Chris Lattner | 8fff126 | 2010-07-07 23:16:37 +0000 | [diff] [blame] | 1637 | if (F->hasSection()) { |
| 1638 | Out << " section \""; |
| 1639 | PrintEscapedString(F->getSection(), Out); |
| 1640 | Out << '"'; |
| 1641 | } |
Chris Lattner | 30caa28 | 2005-11-06 06:48:53 +0000 | [diff] [blame] | 1642 | if (F->getAlignment()) |
| 1643 | Out << " align " << F->getAlignment(); |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 1644 | if (F->hasGC()) |
| 1645 | Out << " gc \"" << F->getGC() << '"'; |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 1646 | if (F->isDeclaration()) { |
Chris Lattner | 91fb407 | 2010-09-02 22:52:10 +0000 | [diff] [blame] | 1647 | Out << '\n'; |
Chris Lattner | 03e2acb | 2002-05-06 03:00:40 +0000 | [diff] [blame] | 1648 | } else { |
Chris Lattner | 91fb407 | 2010-09-02 22:52:10 +0000 | [diff] [blame] | 1649 | Out << " {"; |
| 1650 | // Output all of the function's basic blocks. |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1651 | for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I) |
| 1652 | printBasicBlock(I); |
Chris Lattner | 007377f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 1653 | |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1654 | Out << "}\n"; |
Chris Lattner | 007377f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 1655 | } |
| 1656 | |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1657 | Machine.purgeFunction(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1658 | } |
| 1659 | |
Misha Brukman | ab5c600 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 1660 | /// printArgument - This member is called for every argument that is passed into |
| 1661 | /// the function. Simply print it out |
| 1662 | /// |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1663 | void AssemblyWriter::printArgument(const Argument *Arg, |
Bill Wendling | 94e94b3 | 2012-12-30 13:50:49 +0000 | [diff] [blame] | 1664 | AttributeSet Attrs, unsigned Idx) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1665 | // Output type... |
Chris Lattner | 0f7364b | 2009-02-28 21:26:53 +0000 | [diff] [blame] | 1666 | TypePrinter.print(Arg->getType(), Out); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1667 | |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 1668 | // Output parameter attributes list |
Bill Wendling | 94e94b3 | 2012-12-30 13:50:49 +0000 | [diff] [blame] | 1669 | if (Attrs.hasAttributes(Idx)) |
| 1670 | Out << ' ' << Attrs.getAsString(Idx); |
Reid Spencer | bd5db8e | 2006-12-31 05:24:50 +0000 | [diff] [blame] | 1671 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1672 | // Output name, if available... |
Chris Lattner | c97536e | 2008-08-17 04:40:13 +0000 | [diff] [blame] | 1673 | if (Arg->hasName()) { |
| 1674 | Out << ' '; |
| 1675 | PrintLLVMName(Out, Arg); |
| 1676 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1677 | } |
| 1678 | |
Misha Brukman | ab5c600 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 1679 | /// printBasicBlock - This member is called for each basic block in a method. |
| 1680 | /// |
Chris Lattner | c182499 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 1681 | void AssemblyWriter::printBasicBlock(const BasicBlock *BB) { |
Nick Lewycky | 280a6e6 | 2008-04-25 16:53:59 +0000 | [diff] [blame] | 1682 | if (BB->hasName()) { // Print out the label if it exists... |
Chris Lattner | c97536e | 2008-08-17 04:40:13 +0000 | [diff] [blame] | 1683 | Out << "\n"; |
Daniel Dunbar | 03d7651 | 2009-07-25 23:55:21 +0000 | [diff] [blame] | 1684 | PrintLLVMName(Out, BB->getName(), LabelPrefix); |
Chris Lattner | c97536e | 2008-08-17 04:40:13 +0000 | [diff] [blame] | 1685 | Out << ':'; |
Nick Lewycky | 280a6e6 | 2008-04-25 16:53:59 +0000 | [diff] [blame] | 1686 | } else if (!BB->use_empty()) { // Don't print block # of no uses... |
Bill Wendling | 5d7a5a4 | 2011-04-10 23:18:04 +0000 | [diff] [blame] | 1687 | Out << "\n; <label>:"; |
Chris Lattner | 22379bc | 2007-01-11 03:54:27 +0000 | [diff] [blame] | 1688 | int Slot = Machine.getLocalSlot(BB); |
Chris Lattner | 6956645 | 2004-06-09 19:41:19 +0000 | [diff] [blame] | 1689 | if (Slot != -1) |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1690 | Out << Slot; |
Chris Lattner | 6956645 | 2004-06-09 19:41:19 +0000 | [diff] [blame] | 1691 | else |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1692 | Out << "<badref>"; |
Chris Lattner | 061269b | 2002-10-02 19:38:55 +0000 | [diff] [blame] | 1693 | } |
Chris Lattner | 4e4d862 | 2003-11-20 00:09:43 +0000 | [diff] [blame] | 1694 | |
Dan Gohman | 683e922 | 2009-08-12 17:23:50 +0000 | [diff] [blame] | 1695 | if (BB->getParent() == 0) { |
Chris Lattner | 8f4b1ec | 2009-08-17 15:48:08 +0000 | [diff] [blame] | 1696 | Out.PadToColumn(50); |
Dan Gohman | 683e922 | 2009-08-12 17:23:50 +0000 | [diff] [blame] | 1697 | Out << "; Error: Block without parent!"; |
| 1698 | } else if (BB != &BB->getParent()->getEntryBlock()) { // Not the entry block? |
Chris Lattner | 91fb407 | 2010-09-02 22:52:10 +0000 | [diff] [blame] | 1699 | // Output predecessors for the block. |
Chris Lattner | 8f4b1ec | 2009-08-17 15:48:08 +0000 | [diff] [blame] | 1700 | Out.PadToColumn(50); |
Dan Gohman | 683e922 | 2009-08-12 17:23:50 +0000 | [diff] [blame] | 1701 | Out << ";"; |
Gabor Greif | 4442464 | 2010-03-25 23:25:28 +0000 | [diff] [blame] | 1702 | const_pred_iterator PI = pred_begin(BB), PE = pred_end(BB); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1703 | |
Chris Lattner | eb41129 | 2008-04-22 02:45:44 +0000 | [diff] [blame] | 1704 | if (PI == PE) { |
| 1705 | Out << " No predecessors!"; |
| 1706 | } else { |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1707 | Out << " preds = "; |
Chris Lattner | eb41129 | 2008-04-22 02:45:44 +0000 | [diff] [blame] | 1708 | writeOperand(*PI, false); |
| 1709 | for (++PI; PI != PE; ++PI) { |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1710 | Out << ", "; |
Chris Lattner | 2fcfdb7 | 2006-12-06 06:24:27 +0000 | [diff] [blame] | 1711 | writeOperand(*PI, false); |
Chris Lattner | 40efcec | 2003-11-16 22:59:57 +0000 | [diff] [blame] | 1712 | } |
Chris Lattner | 061269b | 2002-10-02 19:38:55 +0000 | [diff] [blame] | 1713 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1714 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1715 | |
Chris Lattner | eb41129 | 2008-04-22 02:45:44 +0000 | [diff] [blame] | 1716 | Out << "\n"; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1717 | |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1718 | if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, Out); |
Chris Lattner | 95e5a2c | 2003-10-30 23:41:03 +0000 | [diff] [blame] | 1719 | |
Chris Lattner | 007377f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 1720 | // Output all of the instructions in the basic block... |
Dan Gohman | beca689 | 2009-07-13 18:27:59 +0000 | [diff] [blame] | 1721 | for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I) { |
Daniel Malea | d0fef32 | 2013-05-08 20:38:31 +0000 | [diff] [blame^] | 1722 | printInstructionLine(*I); |
Dan Gohman | beca689 | 2009-07-13 18:27:59 +0000 | [diff] [blame] | 1723 | } |
Chris Lattner | 9f717ef | 2004-03-08 18:51:45 +0000 | [diff] [blame] | 1724 | |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1725 | if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, Out); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1726 | } |
| 1727 | |
Daniel Malea | d0fef32 | 2013-05-08 20:38:31 +0000 | [diff] [blame^] | 1728 | /// printInstructionLine - Print an instruction and a newline character. |
| 1729 | void AssemblyWriter::printInstructionLine(const Instruction &I) { |
| 1730 | printInstruction(I); |
| 1731 | Out << '\n'; |
| 1732 | } |
| 1733 | |
Misha Brukman | ab5c600 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 1734 | /// printInfoComment - Print a little comment after the instruction indicating |
| 1735 | /// which slot it occupies. |
| 1736 | /// |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1737 | void AssemblyWriter::printInfoComment(const Value &V) { |
Bill Wendling | 6340549 | 2013-04-16 20:55:47 +0000 | [diff] [blame] | 1738 | if (AnnotationWriter) |
Dan Gohman | 7a5666e | 2010-02-10 20:41:46 +0000 | [diff] [blame] | 1739 | AnnotationWriter->printInfoComment(V, Out); |
Chris Lattner | e02fa85 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 1740 | } |
| 1741 | |
Reid Spencer | 3a9ec24 | 2006-08-28 01:02:49 +0000 | [diff] [blame] | 1742 | // This member is called for each Instruction in a function.. |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1743 | void AssemblyWriter::printInstruction(const Instruction &I) { |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1744 | if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, Out); |
Chris Lattner | 95e5a2c | 2003-10-30 23:41:03 +0000 | [diff] [blame] | 1745 | |
Dan Gohman | 3845e50 | 2009-08-12 23:32:33 +0000 | [diff] [blame] | 1746 | // Print out indentation for an instruction. |
Dan Gohman | 01889ca | 2009-08-13 01:41:52 +0000 | [diff] [blame] | 1747 | Out << " "; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1748 | |
| 1749 | // Print out name if it exists... |
Chris Lattner | c97536e | 2008-08-17 04:40:13 +0000 | [diff] [blame] | 1750 | if (I.hasName()) { |
| 1751 | PrintLLVMName(Out, &I); |
| 1752 | Out << " = "; |
Chris Lattner | 4ee93c4 | 2009-12-29 07:25:48 +0000 | [diff] [blame] | 1753 | } else if (!I.getType()->isVoidTy()) { |
Chris Lattner | 828db8a | 2008-08-29 17:19:30 +0000 | [diff] [blame] | 1754 | // Print out the def slot taken. |
| 1755 | int SlotNum = Machine.getLocalSlot(&I); |
| 1756 | if (SlotNum == -1) |
| 1757 | Out << "<badref> = "; |
| 1758 | else |
| 1759 | Out << '%' << SlotNum << " = "; |
Chris Lattner | c97536e | 2008-08-17 04:40:13 +0000 | [diff] [blame] | 1760 | } |
Andrew Trick | 18801ec | 2011-09-30 19:48:58 +0000 | [diff] [blame] | 1761 | |
Eli Friedman | 21006d4 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1762 | if (isa<CallInst>(I) && cast<CallInst>(I).isTailCall()) |
Chris Lattner | ddb6db4 | 2005-05-06 05:51:46 +0000 | [diff] [blame] | 1763 | Out << "tail "; |
Chris Lattner | e5e475e | 2003-09-08 17:45:59 +0000 | [diff] [blame] | 1764 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1765 | // Print out the opcode... |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1766 | Out << I.getOpcodeName(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1767 | |
Eli Friedman | f03bb26 | 2011-08-12 22:50:01 +0000 | [diff] [blame] | 1768 | // If this is an atomic load or store, print out the atomic marker. |
| 1769 | if ((isa<LoadInst>(I) && cast<LoadInst>(I).isAtomic()) || |
| 1770 | (isa<StoreInst>(I) && cast<StoreInst>(I).isAtomic())) |
| 1771 | Out << " atomic"; |
| 1772 | |
| 1773 | // If this is a volatile operation, print out the volatile marker. |
| 1774 | if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) || |
| 1775 | (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile()) || |
| 1776 | (isa<AtomicCmpXchgInst>(I) && cast<AtomicCmpXchgInst>(I).isVolatile()) || |
| 1777 | (isa<AtomicRMWInst>(I) && cast<AtomicRMWInst>(I).isVolatile())) |
| 1778 | Out << " volatile"; |
| 1779 | |
Dan Gohman | 59858cf | 2009-07-27 16:11:46 +0000 | [diff] [blame] | 1780 | // Print out optimization information. |
| 1781 | WriteOptimizationInfo(Out, &I); |
| 1782 | |
Reid Spencer | 74f1642 | 2006-12-03 06:27:29 +0000 | [diff] [blame] | 1783 | // Print out the compare instruction predicates |
Nate Begeman | ac80ade | 2008-05-12 19:01:56 +0000 | [diff] [blame] | 1784 | if (const CmpInst *CI = dyn_cast<CmpInst>(&I)) |
Chris Lattner | ab49ee7 | 2008-08-23 22:52:27 +0000 | [diff] [blame] | 1785 | Out << ' ' << getPredicateText(CI->getPredicate()); |
Reid Spencer | 74f1642 | 2006-12-03 06:27:29 +0000 | [diff] [blame] | 1786 | |
Eli Friedman | ff03048 | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1787 | // Print out the atomicrmw operation |
| 1788 | if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(&I)) |
| 1789 | writeAtomicRMWOperation(Out, RMWI->getOperation()); |
| 1790 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1791 | // Print out the type of the operands... |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1792 | const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1793 | |
| 1794 | // Special case conditional branches to swizzle the condition out to the front |
Gabor Greif | ccd27fb | 2009-02-09 15:45:06 +0000 | [diff] [blame] | 1795 | if (isa<BranchInst>(I) && cast<BranchInst>(I).isConditional()) { |
David Blaikie | f12b379 | 2013-02-11 01:16:51 +0000 | [diff] [blame] | 1796 | const BranchInst &BI(cast<BranchInst>(I)); |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1797 | Out << ' '; |
Gabor Greif | ccd27fb | 2009-02-09 15:45:06 +0000 | [diff] [blame] | 1798 | writeOperand(BI.getCondition(), true); |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1799 | Out << ", "; |
Gabor Greif | ccd27fb | 2009-02-09 15:45:06 +0000 | [diff] [blame] | 1800 | writeOperand(BI.getSuccessor(0), true); |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1801 | Out << ", "; |
Gabor Greif | ccd27fb | 2009-02-09 15:45:06 +0000 | [diff] [blame] | 1802 | writeOperand(BI.getSuccessor(1), true); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1803 | |
Chris Lattner | 94dc1f2 | 2002-04-13 18:34:38 +0000 | [diff] [blame] | 1804 | } else if (isa<SwitchInst>(I)) { |
David Blaikie | f12b379 | 2013-02-11 01:16:51 +0000 | [diff] [blame] | 1805 | const SwitchInst& SI(cast<SwitchInst>(I)); |
Chris Lattner | f9be95f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 1806 | // Special case switch instruction to get formatting nice and correct. |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1807 | Out << ' '; |
Eli Friedman | bb5a744 | 2011-09-29 20:21:17 +0000 | [diff] [blame] | 1808 | writeOperand(SI.getCondition(), true); |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1809 | Out << ", "; |
Eli Friedman | bb5a744 | 2011-09-29 20:21:17 +0000 | [diff] [blame] | 1810 | writeOperand(SI.getDefaultDest(), true); |
Chris Lattner | ab49ee7 | 2008-08-23 22:52:27 +0000 | [diff] [blame] | 1811 | Out << " ["; |
David Blaikie | f12b379 | 2013-02-11 01:16:51 +0000 | [diff] [blame] | 1812 | for (SwitchInst::ConstCaseIt i = SI.case_begin(), e = SI.case_end(); |
Stepan Dyatkovskiy | c10fa6c | 2012-03-08 07:06:20 +0000 | [diff] [blame] | 1813 | i != e; ++i) { |
Dan Gohman | 01889ca | 2009-08-13 01:41:52 +0000 | [diff] [blame] | 1814 | Out << "\n "; |
Stepan Dyatkovskiy | c10fa6c | 2012-03-08 07:06:20 +0000 | [diff] [blame] | 1815 | writeOperand(i.getCaseValue(), true); |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1816 | Out << ", "; |
Stepan Dyatkovskiy | c10fa6c | 2012-03-08 07:06:20 +0000 | [diff] [blame] | 1817 | writeOperand(i.getCaseSuccessor(), true); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1818 | } |
Dan Gohman | 01889ca | 2009-08-13 01:41:52 +0000 | [diff] [blame] | 1819 | Out << "\n ]"; |
Chris Lattner | ab21db7 | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 1820 | } else if (isa<IndirectBrInst>(I)) { |
| 1821 | // Special case indirectbr instruction to get formatting nice and correct. |
Chris Lattner | f9be95f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 1822 | Out << ' '; |
| 1823 | writeOperand(Operand, true); |
Dan Gohman | 0ed1f42 | 2009-10-30 02:01:10 +0000 | [diff] [blame] | 1824 | Out << ", ["; |
Andrew Trick | 18801ec | 2011-09-30 19:48:58 +0000 | [diff] [blame] | 1825 | |
Chris Lattner | f9be95f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 1826 | for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) { |
| 1827 | if (i != 1) |
| 1828 | Out << ", "; |
| 1829 | writeOperand(I.getOperand(i), true); |
| 1830 | } |
| 1831 | Out << ']'; |
Jay Foad | c137120 | 2011-06-20 14:18:48 +0000 | [diff] [blame] | 1832 | } else if (const PHINode *PN = dyn_cast<PHINode>(&I)) { |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1833 | Out << ' '; |
Chris Lattner | 0f7364b | 2009-02-28 21:26:53 +0000 | [diff] [blame] | 1834 | TypePrinter.print(I.getType(), Out); |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1835 | Out << ' '; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1836 | |
Jay Foad | c137120 | 2011-06-20 14:18:48 +0000 | [diff] [blame] | 1837 | for (unsigned op = 0, Eop = PN->getNumIncomingValues(); op < Eop; ++op) { |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1838 | if (op) Out << ", "; |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1839 | Out << "[ "; |
Jay Foad | c137120 | 2011-06-20 14:18:48 +0000 | [diff] [blame] | 1840 | writeOperand(PN->getIncomingValue(op), false); Out << ", "; |
| 1841 | writeOperand(PN->getIncomingBlock(op), false); Out << " ]"; |
Chris Lattner | c24d208 | 2001-06-11 15:04:20 +0000 | [diff] [blame] | 1842 | } |
Dan Gohman | 995be7d | 2008-05-31 19:12:39 +0000 | [diff] [blame] | 1843 | } else if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(&I)) { |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1844 | Out << ' '; |
Dan Gohman | 995be7d | 2008-05-31 19:12:39 +0000 | [diff] [blame] | 1845 | writeOperand(I.getOperand(0), true); |
| 1846 | for (const unsigned *i = EVI->idx_begin(), *e = EVI->idx_end(); i != e; ++i) |
| 1847 | Out << ", " << *i; |
| 1848 | } else if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(&I)) { |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1849 | Out << ' '; |
| 1850 | writeOperand(I.getOperand(0), true); Out << ", "; |
Dan Gohman | 995be7d | 2008-05-31 19:12:39 +0000 | [diff] [blame] | 1851 | writeOperand(I.getOperand(1), true); |
| 1852 | for (const unsigned *i = IVI->idx_begin(), *e = IVI->idx_end(); i != e; ++i) |
| 1853 | Out << ", " << *i; |
Bill Wendling | e6e8826 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 1854 | } else if (const LandingPadInst *LPI = dyn_cast<LandingPadInst>(&I)) { |
| 1855 | Out << ' '; |
| 1856 | TypePrinter.print(I.getType(), Out); |
| 1857 | Out << " personality "; |
| 1858 | writeOperand(I.getOperand(0), true); Out << '\n'; |
| 1859 | |
| 1860 | if (LPI->isCleanup()) |
| 1861 | Out << " cleanup"; |
| 1862 | |
| 1863 | for (unsigned i = 0, e = LPI->getNumClauses(); i != e; ++i) { |
| 1864 | if (i != 0 || LPI->isCleanup()) Out << "\n"; |
| 1865 | if (LPI->isCatch(i)) |
| 1866 | Out << " catch "; |
| 1867 | else |
| 1868 | Out << " filter "; |
| 1869 | |
| 1870 | writeOperand(LPI->getClause(i), true); |
| 1871 | } |
Devang Patel | 57ef4f4 | 2008-02-23 00:35:18 +0000 | [diff] [blame] | 1872 | } else if (isa<ReturnInst>(I) && !Operand) { |
| 1873 | Out << " void"; |
Chris Lattner | d511898 | 2005-05-06 20:26:43 +0000 | [diff] [blame] | 1874 | } else if (const CallInst *CI = dyn_cast<CallInst>(&I)) { |
| 1875 | // Print the calling convention being used. |
Micah Villmow | d3766df | 2012-09-13 15:11:12 +0000 | [diff] [blame] | 1876 | if (CI->getCallingConv() != CallingConv::C) { |
| 1877 | Out << " "; |
| 1878 | PrintCallingConv(CI->getCallingConv(), Out); |
Chris Lattner | d511898 | 2005-05-06 20:26:43 +0000 | [diff] [blame] | 1879 | } |
| 1880 | |
Gabor Greif | 7bbdf0c | 2010-06-23 13:09:06 +0000 | [diff] [blame] | 1881 | Operand = CI->getCalledValue(); |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1882 | PointerType *PTy = cast<PointerType>(Operand->getType()); |
| 1883 | FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); |
| 1884 | Type *RetTy = FTy->getReturnType(); |
Bill Wendling | 99faa3b | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 1885 | const AttributeSet &PAL = CI->getAttributes(); |
Chris Lattner | 268de04 | 2001-11-06 21:28:12 +0000 | [diff] [blame] | 1886 | |
Bill Wendling | 1b0c54f | 2013-01-18 21:53:16 +0000 | [diff] [blame] | 1887 | if (PAL.hasAttributes(AttributeSet::ReturnIndex)) |
| 1888 | Out << ' ' << PAL.getAsString(AttributeSet::ReturnIndex); |
Devang Patel | 652203f | 2008-09-29 20:49:50 +0000 | [diff] [blame] | 1889 | |
Chris Lattner | 7a01229 | 2003-08-05 15:34:45 +0000 | [diff] [blame] | 1890 | // If possible, print out the short form of the call instruction. We can |
Chris Lattner | b579400 | 2002-04-07 22:49:37 +0000 | [diff] [blame] | 1891 | // only do this if the first argument is a pointer to a nonvararg function, |
Chris Lattner | 7a01229 | 2003-08-05 15:34:45 +0000 | [diff] [blame] | 1892 | // and if the return type is not a pointer to a function. |
Chris Lattner | 268de04 | 2001-11-06 21:28:12 +0000 | [diff] [blame] | 1893 | // |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1894 | Out << ' '; |
Chris Lattner | 7a01229 | 2003-08-05 15:34:45 +0000 | [diff] [blame] | 1895 | if (!FTy->isVarArg() && |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1896 | (!RetTy->isPointerTy() || |
| 1897 | !cast<PointerType>(RetTy)->getElementType()->isFunctionTy())) { |
Chris Lattner | 0f7364b | 2009-02-28 21:26:53 +0000 | [diff] [blame] | 1898 | TypePrinter.print(RetTy, Out); |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1899 | Out << ' '; |
Chris Lattner | 268de04 | 2001-11-06 21:28:12 +0000 | [diff] [blame] | 1900 | writeOperand(Operand, false); |
| 1901 | } else { |
| 1902 | writeOperand(Operand, true); |
| 1903 | } |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1904 | Out << '('; |
Gabor Greif | 7bbdf0c | 2010-06-23 13:09:06 +0000 | [diff] [blame] | 1905 | for (unsigned op = 0, Eop = CI->getNumArgOperands(); op < Eop; ++op) { |
| 1906 | if (op > 0) |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1907 | Out << ", "; |
Bill Wendling | 94e94b3 | 2012-12-30 13:50:49 +0000 | [diff] [blame] | 1908 | writeParamOperand(CI->getArgOperand(op), PAL, op + 1); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1909 | } |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1910 | Out << ')'; |
Bill Wendling | 831737d | 2012-12-30 10:32:01 +0000 | [diff] [blame] | 1911 | if (PAL.hasAttributes(AttributeSet::FunctionIndex)) |
Bill Wendling | 351b7a1 | 2013-02-22 09:09:42 +0000 | [diff] [blame] | 1912 | Out << " #" << Machine.getAttributeGroupSlot(PAL.getFnAttributes()); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1913 | } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) { |
Gabor Greif | c9f7500 | 2010-03-24 13:21:49 +0000 | [diff] [blame] | 1914 | Operand = II->getCalledValue(); |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1915 | PointerType *PTy = cast<PointerType>(Operand->getType()); |
| 1916 | FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); |
| 1917 | Type *RetTy = FTy->getReturnType(); |
Bill Wendling | 99faa3b | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 1918 | const AttributeSet &PAL = II->getAttributes(); |
Chris Lattner | 7a01229 | 2003-08-05 15:34:45 +0000 | [diff] [blame] | 1919 | |
Chris Lattner | d511898 | 2005-05-06 20:26:43 +0000 | [diff] [blame] | 1920 | // Print the calling convention being used. |
Micah Villmow | d3766df | 2012-09-13 15:11:12 +0000 | [diff] [blame] | 1921 | if (II->getCallingConv() != CallingConv::C) { |
| 1922 | Out << " "; |
| 1923 | PrintCallingConv(II->getCallingConv(), Out); |
Chris Lattner | d511898 | 2005-05-06 20:26:43 +0000 | [diff] [blame] | 1924 | } |
| 1925 | |
Bill Wendling | 1b0c54f | 2013-01-18 21:53:16 +0000 | [diff] [blame] | 1926 | if (PAL.hasAttributes(AttributeSet::ReturnIndex)) |
| 1927 | Out << ' ' << PAL.getAsString(AttributeSet::ReturnIndex); |
Devang Patel | 652203f | 2008-09-29 20:49:50 +0000 | [diff] [blame] | 1928 | |
Chris Lattner | 7a01229 | 2003-08-05 15:34:45 +0000 | [diff] [blame] | 1929 | // If possible, print out the short form of the invoke instruction. We can |
| 1930 | // only do this if the first argument is a pointer to a nonvararg function, |
| 1931 | // and if the return type is not a pointer to a function. |
| 1932 | // |
Dan Gohman | 2b6c3d9 | 2008-10-15 18:02:08 +0000 | [diff] [blame] | 1933 | Out << ' '; |
Chris Lattner | 7a01229 | 2003-08-05 15:34:45 +0000 | [diff] [blame] | 1934 | if (!FTy->isVarArg() && |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1935 | (!RetTy->isPointerTy() || |
| 1936 | !cast<PointerType>(RetTy)->getElementType()->isFunctionTy())) { |
Chris Lattner | 0f7364b | 2009-02-28 21:26:53 +0000 | [diff] [blame] | 1937 | TypePrinter.print(RetTy, Out); |
Dan Gohman | 2b6c3d9 | 2008-10-15 18:02:08 +0000 | [diff] [blame] | 1938 | Out << ' '; |
Chris Lattner | 7a01229 | 2003-08-05 15:34:45 +0000 | [diff] [blame] | 1939 | writeOperand(Operand, false); |
| 1940 | } else { |
| 1941 | writeOperand(Operand, true); |
| 1942 | } |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1943 | Out << '('; |
Gabor Greif | 7bbdf0c | 2010-06-23 13:09:06 +0000 | [diff] [blame] | 1944 | for (unsigned op = 0, Eop = II->getNumArgOperands(); op < Eop; ++op) { |
Gabor Greif | c9f7500 | 2010-03-24 13:21:49 +0000 | [diff] [blame] | 1945 | if (op) |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1946 | Out << ", "; |
Bill Wendling | 94e94b3 | 2012-12-30 13:50:49 +0000 | [diff] [blame] | 1947 | writeParamOperand(II->getArgOperand(op), PAL, op + 1); |
Chris Lattner | e02fa85 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 1948 | } |
| 1949 | |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1950 | Out << ')'; |
Bill Wendling | 831737d | 2012-12-30 10:32:01 +0000 | [diff] [blame] | 1951 | if (PAL.hasAttributes(AttributeSet::FunctionIndex)) |
Bill Wendling | 351b7a1 | 2013-02-22 09:09:42 +0000 | [diff] [blame] | 1952 | Out << " #" << Machine.getAttributeGroupSlot(PAL.getFnAttributes()); |
Devang Patel | 19c8746 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 1953 | |
Dan Gohman | 01889ca | 2009-08-13 01:41:52 +0000 | [diff] [blame] | 1954 | Out << "\n to "; |
Chris Lattner | e02fa85 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 1955 | writeOperand(II->getNormalDest(), true); |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1956 | Out << " unwind "; |
Chris Lattner | aeb2a1d | 2004-02-08 21:44:31 +0000 | [diff] [blame] | 1957 | writeOperand(II->getUnwindDest(), true); |
Chris Lattner | e02fa85 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 1958 | |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1959 | } else if (const AllocaInst *AI = dyn_cast<AllocaInst>(&I)) { |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1960 | Out << ' '; |
Dan Gohman | d3da6d5 | 2013-02-08 22:01:47 +0000 | [diff] [blame] | 1961 | TypePrinter.print(AI->getAllocatedType(), Out); |
Dan Gohman | 69bff07 | 2009-07-31 18:23:24 +0000 | [diff] [blame] | 1962 | if (!AI->getArraySize() || AI->isArrayAllocation()) { |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1963 | Out << ", "; |
Chris Lattner | 94dc1f2 | 2002-04-13 18:34:38 +0000 | [diff] [blame] | 1964 | writeOperand(AI->getArraySize(), true); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1965 | } |
Nate Begeman | 14b0529 | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 1966 | if (AI->getAlignment()) { |
Chris Lattner | 9fad0b9 | 2005-11-05 21:20:34 +0000 | [diff] [blame] | 1967 | Out << ", align " << AI->getAlignment(); |
Nate Begeman | 14b0529 | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 1968 | } |
Chris Lattner | e02fa85 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 1969 | } else if (isa<CastInst>(I)) { |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1970 | if (Operand) { |
| 1971 | Out << ' '; |
| 1972 | writeOperand(Operand, true); // Work with broken code |
| 1973 | } |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1974 | Out << " to "; |
Chris Lattner | 0f7364b | 2009-02-28 21:26:53 +0000 | [diff] [blame] | 1975 | TypePrinter.print(I.getType(), Out); |
Chris Lattner | 4d45bd0 | 2003-10-18 05:57:43 +0000 | [diff] [blame] | 1976 | } else if (isa<VAArgInst>(I)) { |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1977 | if (Operand) { |
| 1978 | Out << ' '; |
| 1979 | writeOperand(Operand, true); // Work with broken code |
| 1980 | } |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1981 | Out << ", "; |
Chris Lattner | 0f7364b | 2009-02-28 21:26:53 +0000 | [diff] [blame] | 1982 | TypePrinter.print(I.getType(), Out); |
| 1983 | } else if (Operand) { // Print the normal way. |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1984 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1985 | // PrintAllTypes - Instructions who have operands of all the same type |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1986 | // omit the type from all but the first operand. If the instruction has |
| 1987 | // different type operands (for example br), then they are all printed. |
| 1988 | bool PrintAllTypes = false; |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1989 | Type *TheType = Operand->getType(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1990 | |
Reid Spencer | ebe57e3 | 2007-02-02 13:54:55 +0000 | [diff] [blame] | 1991 | // Select, Store and ShuffleVector always print all types. |
Devang Patel | 6494768 | 2008-03-04 22:05:14 +0000 | [diff] [blame] | 1992 | if (isa<SelectInst>(I) || isa<StoreInst>(I) || isa<ShuffleVectorInst>(I) |
| 1993 | || isa<ReturnInst>(I)) { |
Chris Lattner | ffd9bf4 | 2003-04-16 20:20:02 +0000 | [diff] [blame] | 1994 | PrintAllTypes = true; |
| 1995 | } else { |
| 1996 | for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) { |
| 1997 | Operand = I.getOperand(i); |
Nuno Lopes | 6ad2b2a | 2009-01-15 18:40:57 +0000 | [diff] [blame] | 1998 | // note that Operand shouldn't be null, but the test helps make dump() |
| 1999 | // more tolerant of malformed IR |
Nuno Lopes | a8c78a9 | 2009-01-14 17:51:41 +0000 | [diff] [blame] | 2000 | if (Operand && Operand->getType() != TheType) { |
Chris Lattner | ffd9bf4 | 2003-04-16 20:20:02 +0000 | [diff] [blame] | 2001 | PrintAllTypes = true; // We have differing types! Print them all! |
| 2002 | break; |
| 2003 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 2004 | } |
| 2005 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2006 | |
Chris Lattner | c182499 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 2007 | if (!PrintAllTypes) { |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 2008 | Out << ' '; |
Chris Lattner | 0f7364b | 2009-02-28 21:26:53 +0000 | [diff] [blame] | 2009 | TypePrinter.print(TheType, Out); |
Chris Lattner | c182499 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 2010 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 2011 | |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 2012 | Out << ' '; |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2013 | for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) { |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 2014 | if (i) Out << ", "; |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2015 | writeOperand(I.getOperand(i), PrintAllTypes); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 2016 | } |
| 2017 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2018 | |
Eli Friedman | 21006d4 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 2019 | // Print atomic ordering/alignment for memory operations |
| 2020 | if (const LoadInst *LI = dyn_cast<LoadInst>(&I)) { |
| 2021 | if (LI->isAtomic()) |
| 2022 | writeAtomic(LI->getOrdering(), LI->getSynchScope()); |
| 2023 | if (LI->getAlignment()) |
| 2024 | Out << ", align " << LI->getAlignment(); |
| 2025 | } else if (const StoreInst *SI = dyn_cast<StoreInst>(&I)) { |
| 2026 | if (SI->isAtomic()) |
| 2027 | writeAtomic(SI->getOrdering(), SI->getSynchScope()); |
| 2028 | if (SI->getAlignment()) |
| 2029 | Out << ", align " << SI->getAlignment(); |
Eli Friedman | ff03048 | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 2030 | } else if (const AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(&I)) { |
| 2031 | writeAtomic(CXI->getOrdering(), CXI->getSynchScope()); |
| 2032 | } else if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(&I)) { |
| 2033 | writeAtomic(RMWI->getOrdering(), RMWI->getSynchScope()); |
Eli Friedman | 47f3513 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 2034 | } else if (const FenceInst *FI = dyn_cast<FenceInst>(&I)) { |
| 2035 | writeAtomic(FI->getOrdering(), FI->getSynchScope()); |
Christopher Lamb | 43c7f37 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 2036 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 2037 | |
Chris Lattner | 7d05c46 | 2009-12-28 20:10:43 +0000 | [diff] [blame] | 2038 | // Print Metadata info. |
Nick Lewycky | fa0c54e | 2010-02-25 06:53:04 +0000 | [diff] [blame] | 2039 | SmallVector<std::pair<unsigned, MDNode*>, 4> InstMD; |
| 2040 | I.getAllMetadata(InstMD); |
Erick Tryzelaar | f3d5c91 | 2010-03-02 05:32:52 +0000 | [diff] [blame] | 2041 | if (!InstMD.empty()) { |
| 2042 | SmallVector<StringRef, 8> MDNames; |
| 2043 | I.getType()->getContext().getMDKindNames(MDNames); |
| 2044 | for (unsigned i = 0, e = InstMD.size(); i != e; ++i) { |
| 2045 | unsigned Kind = InstMD[i].first; |
| 2046 | if (Kind < MDNames.size()) { |
| 2047 | Out << ", !" << MDNames[Kind]; |
Daniel Malea | d0fef32 | 2013-05-08 20:38:31 +0000 | [diff] [blame^] | 2048 | } else { |
| 2049 | Out << ", !<unknown kind #" << Kind << ">"; |
| 2050 | } |
Dan Gohman | 3bdfbf5 | 2010-07-20 23:55:01 +0000 | [diff] [blame] | 2051 | Out << ' '; |
| 2052 | WriteAsOperandInternal(Out, InstMD[i].second, &TypePrinter, &Machine, |
| 2053 | TheModule); |
Nick Lewycky | fa0c54e | 2010-02-25 06:53:04 +0000 | [diff] [blame] | 2054 | } |
Devang Patel | 7f93f4d | 2009-10-07 16:37:55 +0000 | [diff] [blame] | 2055 | } |
Chris Lattner | e02fa85 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 2056 | printInfoComment(I); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 2057 | } |
| 2058 | |
Chris Lattner | 6e6b180 | 2009-12-31 02:13:35 +0000 | [diff] [blame] | 2059 | static void WriteMDNodeComment(const MDNode *Node, |
Duncan Sands | 3472766 | 2010-07-12 08:16:59 +0000 | [diff] [blame] | 2060 | formatted_raw_ostream &Out) { |
Chris Lattner | 6e6b180 | 2009-12-31 02:13:35 +0000 | [diff] [blame] | 2061 | if (Node->getNumOperands() < 1) |
| 2062 | return; |
Bill Wendling | 58a6cf2 | 2012-06-28 00:41:44 +0000 | [diff] [blame] | 2063 | |
| 2064 | Value *Op = Node->getOperand(0); |
| 2065 | if (!Op || !isa<ConstantInt>(Op) || cast<ConstantInt>(Op)->getBitWidth() < 32) |
Chris Lattner | 6e6b180 | 2009-12-31 02:13:35 +0000 | [diff] [blame] | 2066 | return; |
Andrew Trick | 18801ec | 2011-09-30 19:48:58 +0000 | [diff] [blame] | 2067 | |
Bill Wendling | 58a6cf2 | 2012-06-28 00:41:44 +0000 | [diff] [blame] | 2068 | DIDescriptor Desc(Node); |
David Blaikie | c0ec8a4 | 2013-03-11 23:39:23 +0000 | [diff] [blame] | 2069 | if (!Desc.Verify()) |
Bill Wendling | 58a6cf2 | 2012-06-28 00:41:44 +0000 | [diff] [blame] | 2070 | return; |
| 2071 | |
| 2072 | unsigned Tag = Desc.getTag(); |
Chris Lattner | 6e6b180 | 2009-12-31 02:13:35 +0000 | [diff] [blame] | 2073 | Out.PadToColumn(50); |
Bill Wendling | 86b032b | 2012-07-03 20:01:02 +0000 | [diff] [blame] | 2074 | if (dwarf::TagString(Tag)) { |
| 2075 | Out << "; "; |
| 2076 | Desc.print(Out); |
| 2077 | } else if (Tag == dwarf::DW_TAG_user_base) { |
Chris Lattner | 6e6b180 | 2009-12-31 02:13:35 +0000 | [diff] [blame] | 2078 | Out << "; [ DW_TAG_user_base ]"; |
Bill Wendling | 86b032b | 2012-07-03 20:01:02 +0000 | [diff] [blame] | 2079 | } |
Chris Lattner | 6e6b180 | 2009-12-31 02:13:35 +0000 | [diff] [blame] | 2080 | } |
| 2081 | |
Daniel Malea | d0fef32 | 2013-05-08 20:38:31 +0000 | [diff] [blame^] | 2082 | void AssemblyWriter::writeMDNode(unsigned Slot, const MDNode *Node) { |
| 2083 | Out << '!' << Slot << " = metadata "; |
| 2084 | printMDNodeBody(Node); |
| 2085 | } |
| 2086 | |
Chris Lattner | 6e6b180 | 2009-12-31 02:13:35 +0000 | [diff] [blame] | 2087 | void AssemblyWriter::writeAllMDNodes() { |
| 2088 | SmallVector<const MDNode *, 16> Nodes; |
Chris Lattner | 307c989 | 2009-12-31 02:20:11 +0000 | [diff] [blame] | 2089 | Nodes.resize(Machine.mdn_size()); |
| 2090 | for (SlotTracker::mdn_iterator I = Machine.mdn_begin(), E = Machine.mdn_end(); |
| 2091 | I != E; ++I) |
Chris Lattner | 6e6b180 | 2009-12-31 02:13:35 +0000 | [diff] [blame] | 2092 | Nodes[I->second] = cast<MDNode>(I->first); |
Andrew Trick | 18801ec | 2011-09-30 19:48:58 +0000 | [diff] [blame] | 2093 | |
Chris Lattner | 6e6b180 | 2009-12-31 02:13:35 +0000 | [diff] [blame] | 2094 | for (unsigned i = 0, e = Nodes.size(); i != e; ++i) { |
Daniel Malea | d0fef32 | 2013-05-08 20:38:31 +0000 | [diff] [blame^] | 2095 | writeMDNode(i, Nodes[i]); |
Chris Lattner | 6e6b180 | 2009-12-31 02:13:35 +0000 | [diff] [blame] | 2096 | } |
| 2097 | } |
| 2098 | |
| 2099 | void AssemblyWriter::printMDNodeBody(const MDNode *Node) { |
Dan Gohman | 3bdfbf5 | 2010-07-20 23:55:01 +0000 | [diff] [blame] | 2100 | WriteMDNodeBodyInternal(Out, Node, &TypePrinter, &Machine, TheModule); |
Chris Lattner | 6e6b180 | 2009-12-31 02:13:35 +0000 | [diff] [blame] | 2101 | WriteMDNodeComment(Node, Out); |
| 2102 | Out << "\n"; |
| 2103 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 2104 | |
Bill Wendling | b29ce26 | 2013-02-11 08:43:33 +0000 | [diff] [blame] | 2105 | void AssemblyWriter::writeAllAttributeGroups() { |
| 2106 | std::vector<std::pair<AttributeSet, unsigned> > asVec; |
| 2107 | asVec.resize(Machine.as_size()); |
| 2108 | |
| 2109 | for (SlotTracker::as_iterator I = Machine.as_begin(), E = Machine.as_end(); |
| 2110 | I != E; ++I) |
| 2111 | asVec[I->second] = *I; |
| 2112 | |
| 2113 | for (std::vector<std::pair<AttributeSet, unsigned> >::iterator |
| 2114 | I = asVec.begin(), E = asVec.end(); I != E; ++I) |
| 2115 | Out << "attributes #" << I->second << " = { " |
Rafael Espindola | aae0298 | 2013-05-01 13:07:03 +0000 | [diff] [blame] | 2116 | << I->first.getAsString(AttributeSet::FunctionIndex, true) << " }\n"; |
Bill Wendling | b29ce26 | 2013-02-11 08:43:33 +0000 | [diff] [blame] | 2117 | } |
| 2118 | |
Daniel Malea | d0fef32 | 2013-05-08 20:38:31 +0000 | [diff] [blame^] | 2119 | } // namespace llvm |
| 2120 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 2121 | //===----------------------------------------------------------------------===// |
| 2122 | // External Interface declarations |
| 2123 | //===----------------------------------------------------------------------===// |
| 2124 | |
Dan Gohman | 683e922 | 2009-08-12 17:23:50 +0000 | [diff] [blame] | 2125 | void Module::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW) const { |
Chris Lattner | 0d9574a | 2008-08-19 04:26:57 +0000 | [diff] [blame] | 2126 | SlotTracker SlotTable(this); |
Dan Gohman | 683e922 | 2009-08-12 17:23:50 +0000 | [diff] [blame] | 2127 | formatted_raw_ostream OS(ROS); |
Chris Lattner | 944fac7 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 2128 | AssemblyWriter W(OS, SlotTable, this, AAW); |
Chris Lattner | bd72b32 | 2009-12-31 02:23:35 +0000 | [diff] [blame] | 2129 | W.printModule(this); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 2130 | } |
| 2131 | |
Dan Gohman | 17aa92c | 2010-07-21 23:38:33 +0000 | [diff] [blame] | 2132 | void NamedMDNode::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW) const { |
| 2133 | SlotTracker SlotTable(getParent()); |
| 2134 | formatted_raw_ostream OS(ROS); |
| 2135 | AssemblyWriter W(OS, SlotTable, getParent(), AAW); |
| 2136 | W.printNamedMDNode(this); |
| 2137 | } |
| 2138 | |
Chris Lattner | 6d4306e | 2009-02-28 21:11:05 +0000 | [diff] [blame] | 2139 | void Type::print(raw_ostream &OS) const { |
| 2140 | if (this == 0) { |
| 2141 | OS << "<null Type>"; |
| 2142 | return; |
| 2143 | } |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 2144 | TypePrinting TP; |
| 2145 | TP.print(const_cast<Type*>(this), OS); |
Andrew Trick | 18801ec | 2011-09-30 19:48:58 +0000 | [diff] [blame] | 2146 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 2147 | // If the type is a named struct type, print the body as well. |
| 2148 | if (StructType *STy = dyn_cast<StructType>(const_cast<Type*>(this))) |
Chris Lattner | c4d0e9f | 2011-08-12 18:07:07 +0000 | [diff] [blame] | 2149 | if (!STy->isLiteral()) { |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 2150 | OS << " = type "; |
| 2151 | TP.printStructBody(STy, OS); |
| 2152 | } |
Chris Lattner | 75cf7cf | 2002-04-08 22:03:40 +0000 | [diff] [blame] | 2153 | } |
| 2154 | |
Dan Gohman | 683e922 | 2009-08-12 17:23:50 +0000 | [diff] [blame] | 2155 | void Value::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW) const { |
Chris Lattner | 944fac7 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 2156 | if (this == 0) { |
Dan Gohman | 1220e10 | 2009-08-12 20:56:03 +0000 | [diff] [blame] | 2157 | ROS << "printing a <null> value\n"; |
Chris Lattner | 944fac7 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 2158 | return; |
| 2159 | } |
Dan Gohman | 1220e10 | 2009-08-12 20:56:03 +0000 | [diff] [blame] | 2160 | formatted_raw_ostream OS(ROS); |
Chris Lattner | 944fac7 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 2161 | if (const Instruction *I = dyn_cast<Instruction>(this)) { |
| 2162 | const Function *F = I->getParent() ? I->getParent()->getParent() : 0; |
| 2163 | SlotTracker SlotTable(F); |
Chris Lattner | dbe85bf | 2009-12-31 08:23:09 +0000 | [diff] [blame] | 2164 | AssemblyWriter W(OS, SlotTable, getModuleFromVal(I), AAW); |
Chris Lattner | bd72b32 | 2009-12-31 02:23:35 +0000 | [diff] [blame] | 2165 | W.printInstruction(*I); |
Chris Lattner | 944fac7 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 2166 | } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(this)) { |
| 2167 | SlotTracker SlotTable(BB->getParent()); |
Chris Lattner | 6e6b180 | 2009-12-31 02:13:35 +0000 | [diff] [blame] | 2168 | AssemblyWriter W(OS, SlotTable, getModuleFromVal(BB), AAW); |
Chris Lattner | bd72b32 | 2009-12-31 02:23:35 +0000 | [diff] [blame] | 2169 | W.printBasicBlock(BB); |
Chris Lattner | 944fac7 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 2170 | } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(this)) { |
| 2171 | SlotTracker SlotTable(GV->getParent()); |
Dan Gohman | ba0941f | 2009-04-20 16:10:33 +0000 | [diff] [blame] | 2172 | AssemblyWriter W(OS, SlotTable, GV->getParent(), AAW); |
Chris Lattner | bd72b32 | 2009-12-31 02:23:35 +0000 | [diff] [blame] | 2173 | if (const GlobalVariable *V = dyn_cast<GlobalVariable>(GV)) |
| 2174 | W.printGlobal(V); |
| 2175 | else if (const Function *F = dyn_cast<Function>(GV)) |
| 2176 | W.printFunction(F); |
| 2177 | else |
| 2178 | W.printAlias(cast<GlobalAlias>(GV)); |
Devang Patel | fcd65ae | 2009-07-01 20:59:15 +0000 | [diff] [blame] | 2179 | } else if (const MDNode *N = dyn_cast<MDNode>(this)) { |
Victor Hernandez | 8fffff5 | 2010-01-20 04:45:57 +0000 | [diff] [blame] | 2180 | const Function *F = N->getFunction(); |
Victor Hernandez | 559588b | 2010-01-14 01:47:37 +0000 | [diff] [blame] | 2181 | SlotTracker SlotTable(F); |
Dan Gohman | 79b78a4 | 2010-07-14 21:12:44 +0000 | [diff] [blame] | 2182 | AssemblyWriter W(OS, SlotTable, F ? F->getParent() : 0, AAW); |
Chris Lattner | 6e6b180 | 2009-12-31 02:13:35 +0000 | [diff] [blame] | 2183 | W.printMDNodeBody(N); |
Chris Lattner | 944fac7 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 2184 | } else if (const Constant *C = dyn_cast<Constant>(this)) { |
Chris Lattner | e9fa33e | 2009-02-28 23:20:19 +0000 | [diff] [blame] | 2185 | TypePrinting TypePrinter; |
Chris Lattner | 0f7364b | 2009-02-28 21:26:53 +0000 | [diff] [blame] | 2186 | TypePrinter.print(C->getType(), OS); |
Chris Lattner | 6d4306e | 2009-02-28 21:11:05 +0000 | [diff] [blame] | 2187 | OS << ' '; |
Dan Gohman | 3bdfbf5 | 2010-07-20 23:55:01 +0000 | [diff] [blame] | 2188 | WriteConstantInternal(OS, C, TypePrinter, 0, 0); |
Chris Lattner | 4a3d3a5 | 2009-12-31 01:41:14 +0000 | [diff] [blame] | 2189 | } else if (isa<InlineAsm>(this) || isa<MDString>(this) || |
| 2190 | isa<Argument>(this)) { |
Chris Lattner | 944fac7 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 2191 | WriteAsOperand(OS, this, true, 0); |
| 2192 | } else { |
Dan Gohman | cd26ec5 | 2009-09-23 01:33:16 +0000 | [diff] [blame] | 2193 | // Otherwise we don't know what it is. Call the virtual function to |
| 2194 | // allow a subclass to print itself. |
| 2195 | printCustom(OS); |
Chris Lattner | 944fac7 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 2196 | } |
| 2197 | } |
| 2198 | |
Dan Gohman | cd26ec5 | 2009-09-23 01:33:16 +0000 | [diff] [blame] | 2199 | // Value::printCustom - subclasses should override this to implement printing. |
| 2200 | void Value::printCustom(raw_ostream &OS) const { |
| 2201 | llvm_unreachable("Unknown value to print out!"); |
| 2202 | } |
| 2203 | |
Chris Lattner | 7059e53 | 2008-08-25 17:03:15 +0000 | [diff] [blame] | 2204 | // Value::dump - allow easy printing of Values from the debugger. |
David Greene | d865e02 | 2010-01-05 01:29:26 +0000 | [diff] [blame] | 2205 | void Value::dump() const { print(dbgs()); dbgs() << '\n'; } |
Reid Spencer | fa452c0 | 2004-05-25 18:14:38 +0000 | [diff] [blame] | 2206 | |
Chris Lattner | 7059e53 | 2008-08-25 17:03:15 +0000 | [diff] [blame] | 2207 | // Type::dump - allow easy printing of Types from the debugger. |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 2208 | void Type::dump() const { print(dbgs()); } |
Chris Lattner | c287137 | 2009-02-28 21:05:51 +0000 | [diff] [blame] | 2209 | |
Chris Lattner | 7059e53 | 2008-08-25 17:03:15 +0000 | [diff] [blame] | 2210 | // Module::dump() - Allow printing of Modules from the debugger. |
David Greene | d865e02 | 2010-01-05 01:29:26 +0000 | [diff] [blame] | 2211 | void Module::dump() const { print(dbgs(), 0); } |
Bill Wendling | f4374e4 | 2011-12-09 23:18:34 +0000 | [diff] [blame] | 2212 | |
| 2213 | // NamedMDNode::dump() - Allow printing of NamedMDNodes from the debugger. |
| 2214 | void NamedMDNode::dump() const { print(dbgs(), 0); } |