blob: 8830c9d5feb4c7aca55b0bbcf8e5d64c57974f91 [file] [log] [blame]
Chris Lattnerf7e79482002-04-07 22:31:46 +00001//===-- AsmWriter.cpp - Printing LLVM as an assembly file -----------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
10// This library implements the functionality defined in llvm/Assembly/Writer.h
11//
Chris Lattner189088e2002-04-12 18:21:53 +000012// Note that these routines must be extremely tolerant of various errors in the
Chris Lattnerf70da102003-05-08 02:44:12 +000013// LLVM code, because it can be used for debugging transformations.
Chris Lattner189088e2002-04-12 18:21:53 +000014//
Chris Lattner2f7c9632001-06-06 20:29:01 +000015//===----------------------------------------------------------------------===//
16
Daniel Maleafddddbe2013-05-23 22:34:33 +000017#include "AsmWriter.h"
18
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +000019#include "llvm/Assembly/Writer.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/ADT/DenseMap.h"
21#include "llvm/ADT/STLExtras.h"
22#include "llvm/ADT/SmallString.h"
23#include "llvm/ADT/StringExtras.h"
Chris Lattnerad074cb2010-09-02 23:09:42 +000024#include "llvm/Assembly/AssemblyAnnotationWriter.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000025#include "llvm/Assembly/PrintModulePass.h"
Bill Wendling5cb50c52012-06-28 00:41:44 +000026#include "llvm/DebugInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000027#include "llvm/IR/CallingConv.h"
28#include "llvm/IR/Constants.h"
29#include "llvm/IR/DerivedTypes.h"
30#include "llvm/IR/InlineAsm.h"
31#include "llvm/IR/IntrinsicInst.h"
32#include "llvm/IR/LLVMContext.h"
33#include "llvm/IR/Module.h"
34#include "llvm/IR/Operator.h"
Chandler Carruthdcb603f2013-01-07 15:43:51 +000035#include "llvm/IR/TypeFinder.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000036#include "llvm/IR/ValueSymbolTable.h"
Bill Wendlingdfc91892006-11-28 02:09:03 +000037#include "llvm/Support/CFG.h"
David Greenec7f9b122010-01-05 01:29:26 +000038#include "llvm/Support/Debug.h"
Devang Patel711ab5b2009-09-30 20:16:54 +000039#include "llvm/Support/Dwarf.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000040#include "llvm/Support/ErrorHandling.h"
Dan Gohmane2745262009-08-12 17:23:50 +000041#include "llvm/Support/FormattedStream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000042#include "llvm/Support/MathExtras.h"
Daniel Maleafddddbe2013-05-23 22:34:33 +000043
Chris Lattnerfee714f2001-09-07 16:36:04 +000044#include <algorithm>
Reid Spencerbdf03b42007-05-22 19:27:35 +000045#include <cctype>
Chris Lattner189d19f2003-11-21 20:23:48 +000046using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000047
Reid Spencer294715b2005-05-15 16:13:11 +000048// Make virtual table appear in this compilation unit.
49AssemblyAnnotationWriter::~AssemblyAnnotationWriter() {}
50
Chris Lattner3eee99c2008-08-19 04:36:02 +000051//===----------------------------------------------------------------------===//
52// Helper Functions
53//===----------------------------------------------------------------------===//
54
55static const Module *getModuleFromVal(const Value *V) {
56 if (const Argument *MA = dyn_cast<Argument>(V))
57 return MA->getParent() ? MA->getParent()->getParent() : 0;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +000058
Chris Lattner3eee99c2008-08-19 04:36:02 +000059 if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
60 return BB->getParent() ? BB->getParent()->getParent() : 0;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +000061
Chris Lattner3eee99c2008-08-19 04:36:02 +000062 if (const Instruction *I = dyn_cast<Instruction>(V)) {
63 const Function *M = I->getParent() ? I->getParent()->getParent() : 0;
64 return M ? M->getParent() : 0;
65 }
Andrew Trickec4b6e72011-09-30 19:48:58 +000066
Chris Lattner3eee99c2008-08-19 04:36:02 +000067 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
68 return GV->getParent();
69 return 0;
70}
71
Bill Wendling90bc19c2013-02-20 07:21:42 +000072static void PrintCallingConv(unsigned cc, raw_ostream &Out) {
Micah Villmow857186d2012-09-13 15:11:12 +000073 switch (cc) {
Bill Wendling90bc19c2013-02-20 07:21:42 +000074 default: Out << "cc" << cc; break;
75 case CallingConv::Fast: Out << "fastcc"; break;
76 case CallingConv::Cold: Out << "coldcc"; break;
Andrew Trick5ae6ed82013-11-11 22:40:22 +000077 case CallingConv::WebKit_JS: Out << "webkit_jscc"; break;
78 case CallingConv::AnyReg: Out << "anyregcc"; break;
Bill Wendling90bc19c2013-02-20 07:21:42 +000079 case CallingConv::X86_StdCall: Out << "x86_stdcallcc"; break;
80 case CallingConv::X86_FastCall: Out << "x86_fastcallcc"; break;
81 case CallingConv::X86_ThisCall: Out << "x86_thiscallcc"; break;
82 case CallingConv::Intel_OCL_BI: Out << "intel_ocl_bicc"; break;
83 case CallingConv::ARM_APCS: Out << "arm_apcscc"; break;
84 case CallingConv::ARM_AAPCS: Out << "arm_aapcscc"; break;
85 case CallingConv::ARM_AAPCS_VFP: Out << "arm_aapcs_vfpcc"; break;
86 case CallingConv::MSP430_INTR: Out << "msp430_intrcc"; break;
87 case CallingConv::PTX_Kernel: Out << "ptx_kernel"; break;
88 case CallingConv::PTX_Device: Out << "ptx_device"; break;
Charles Davise8f297c2013-07-12 06:02:35 +000089 case CallingConv::X86_64_SysV: Out << "x86_64_sysvcc"; break;
90 case CallingConv::X86_64_Win64: Out << "x86_64_win64cc"; break;
Micah Villmow857186d2012-09-13 15:11:12 +000091 }
92}
Michael Ilseman26ee2b82012-11-15 22:34:00 +000093
Daniel Dunbardb0b70a2008-10-28 19:33:02 +000094// PrintEscapedString - Print each character of the specified string, escaping
95// it if it is not printable or if it is an escape char.
Chris Lattner9380b812010-07-07 23:16:37 +000096static void PrintEscapedString(StringRef Name, raw_ostream &Out) {
Daniel Dunbare03eecb2009-07-25 23:55:21 +000097 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
98 unsigned char C = Name[i];
Nick Lewycky5aa592a2009-03-15 06:39:52 +000099 if (isprint(C) && C != '\\' && C != '"')
Daniel Dunbardb0b70a2008-10-28 19:33:02 +0000100 Out << C;
101 else
102 Out << '\\' << hexdigit(C >> 4) << hexdigit(C & 0x0F);
103 }
104}
105
Chris Lattner3eee99c2008-08-19 04:36:02 +0000106enum PrefixType {
107 GlobalPrefix,
108 LabelPrefix,
Daniel Dunbar389529a2008-10-14 23:28:09 +0000109 LocalPrefix,
110 NoPrefix
Chris Lattner3eee99c2008-08-19 04:36:02 +0000111};
112
113/// PrintLLVMName - Turn the specified name into an 'LLVM name', which is either
114/// prefixed with % (if the string only contains simple characters) or is
115/// surrounded with ""'s (if it has special chars in it). Print it out.
Benjamin Kramer92d89982010-07-14 22:38:02 +0000116static void PrintLLVMName(raw_ostream &OS, StringRef Name, PrefixType Prefix) {
Jay Foadf7adb322011-04-24 14:30:00 +0000117 assert(!Name.empty() && "Cannot get empty name!");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000118 switch (Prefix) {
Daniel Dunbar389529a2008-10-14 23:28:09 +0000119 case NoPrefix: break;
Chris Lattner1508d3f2008-08-19 05:16:28 +0000120 case GlobalPrefix: OS << '@'; break;
121 case LabelPrefix: break;
122 case LocalPrefix: OS << '%'; break;
Nick Lewycky063699a2009-03-19 06:31:22 +0000123 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000124
Chris Lattner3eee99c2008-08-19 04:36:02 +0000125 // Scan the name to see if it needs quotes first.
Guy Benyei83c74e92013-02-12 21:21:59 +0000126 bool NeedsQuotes = isdigit(static_cast<unsigned char>(Name[0]));
Chris Lattner3eee99c2008-08-19 04:36:02 +0000127 if (!NeedsQuotes) {
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000128 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
Aaron Ballmaned9b0a92012-07-16 16:18:18 +0000129 // By making this unsigned, the value passed in to isalnum will always be
130 // in the range 0-255. This is important when building with MSVC because
131 // its implementation will assert. This situation can arise when dealing
132 // with UTF-8 multibyte characters.
133 unsigned char C = Name[i];
Guy Benyei83c74e92013-02-12 21:21:59 +0000134 if (!isalnum(static_cast<unsigned char>(C)) && C != '-' && C != '.' &&
135 C != '_') {
Chris Lattner3eee99c2008-08-19 04:36:02 +0000136 NeedsQuotes = true;
137 break;
138 }
139 }
140 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000141
Chris Lattner3eee99c2008-08-19 04:36:02 +0000142 // If we didn't need any quotes, just write out the name in one blast.
143 if (!NeedsQuotes) {
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000144 OS << Name;
Chris Lattner3eee99c2008-08-19 04:36:02 +0000145 return;
146 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000147
Chris Lattner3eee99c2008-08-19 04:36:02 +0000148 // Okay, we need quotes. Output the quotes and escape any scary characters as
149 // needed.
150 OS << '"';
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000151 PrintEscapedString(Name, OS);
Chris Lattner3eee99c2008-08-19 04:36:02 +0000152 OS << '"';
153}
154
155/// PrintLLVMName - Turn the specified name into an 'LLVM name', which is either
156/// prefixed with % (if the string only contains simple characters) or is
157/// surrounded with ""'s (if it has special chars in it). Print it out.
Dan Gohman12dad632009-08-12 20:56:03 +0000158static void PrintLLVMName(raw_ostream &OS, const Value *V) {
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000159 PrintLLVMName(OS, V->getName(),
Chris Lattner3eee99c2008-08-19 04:36:02 +0000160 isa<GlobalValue>(V) ? GlobalPrefix : LocalPrefix);
161}
162
Chris Lattner0f578952009-02-28 20:25:14 +0000163
Daniel Maleaded9f932013-05-08 20:38:31 +0000164namespace llvm {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000165
166void TypePrinting::incorporateTypes(const Module &M) {
Bill Wendling8555a372012-08-03 00:30:35 +0000167 NamedTypes.run(M, false);
Andrew Trickec4b6e72011-09-30 19:48:58 +0000168
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000169 // The list of struct types we got back includes all the struct types, split
170 // the unnamed ones out to a numbering and remove the anonymous structs.
171 unsigned NextNumber = 0;
Andrew Trickec4b6e72011-09-30 19:48:58 +0000172
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000173 std::vector<StructType*>::iterator NextToUse = NamedTypes.begin(), I, E;
174 for (I = NamedTypes.begin(), E = NamedTypes.end(); I != E; ++I) {
175 StructType *STy = *I;
Andrew Trickec4b6e72011-09-30 19:48:58 +0000176
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000177 // Ignore anonymous types.
Chris Lattner01beceb2011-08-12 18:07:07 +0000178 if (STy->isLiteral())
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000179 continue;
Andrew Trickec4b6e72011-09-30 19:48:58 +0000180
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000181 if (STy->getName().empty())
182 NumberedTypes[STy] = NextNumber++;
183 else
184 *NextToUse++ = STy;
185 }
Andrew Trickec4b6e72011-09-30 19:48:58 +0000186
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000187 NamedTypes.erase(NextToUse, NamedTypes.end());
188}
189
190
Chris Lattner40959d02009-02-28 20:49:40 +0000191/// CalcTypeName - Write the specified type to the specified raw_ostream, making
192/// use of type names or up references to shorten the type name where possible.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000193void TypePrinting::print(Type *Ty, raw_ostream &OS) {
Chris Lattner0f578952009-02-28 20:25:14 +0000194 switch (Ty->getTypeID()) {
Chris Lattner06a23212009-02-28 21:27:31 +0000195 case Type::VoidTyID: OS << "void"; break;
Dan Gohman518cda42011-12-17 00:04:22 +0000196 case Type::HalfTyID: OS << "half"; break;
Chris Lattner06a23212009-02-28 21:27:31 +0000197 case Type::FloatTyID: OS << "float"; break;
198 case Type::DoubleTyID: OS << "double"; break;
199 case Type::X86_FP80TyID: OS << "x86_fp80"; break;
200 case Type::FP128TyID: OS << "fp128"; break;
201 case Type::PPC_FP128TyID: OS << "ppc_fp128"; break;
202 case Type::LabelTyID: OS << "label"; break;
Nick Lewyckyadbc2842009-05-30 05:06:04 +0000203 case Type::MetadataTyID: OS << "metadata"; break;
Dale Johannesenbaa5d042010-09-10 20:55:01 +0000204 case Type::X86_MMXTyID: OS << "x86_mmx"; break;
Chris Lattner68318b12009-02-28 21:18:43 +0000205 case Type::IntegerTyID:
Chris Lattner06a23212009-02-28 21:27:31 +0000206 OS << 'i' << cast<IntegerType>(Ty)->getBitWidth();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000207 return;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000208
Chris Lattner07d88292009-02-28 20:35:42 +0000209 case Type::FunctionTyID: {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000210 FunctionType *FTy = cast<FunctionType>(Ty);
211 print(FTy->getReturnType(), OS);
Chris Lattner06a23212009-02-28 21:27:31 +0000212 OS << " (";
Chris Lattner07d88292009-02-28 20:35:42 +0000213 for (FunctionType::param_iterator I = FTy->param_begin(),
214 E = FTy->param_end(); I != E; ++I) {
215 if (I != FTy->param_begin())
Chris Lattner06a23212009-02-28 21:27:31 +0000216 OS << ", ";
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000217 print(*I, OS);
Chris Lattner0f578952009-02-28 20:25:14 +0000218 }
Chris Lattner07d88292009-02-28 20:35:42 +0000219 if (FTy->isVarArg()) {
Chris Lattner06a23212009-02-28 21:27:31 +0000220 if (FTy->getNumParams()) OS << ", ";
221 OS << "...";
Chris Lattner0f578952009-02-28 20:25:14 +0000222 }
Chris Lattner06a23212009-02-28 21:27:31 +0000223 OS << ')';
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000224 return;
Chris Lattner07d88292009-02-28 20:35:42 +0000225 }
226 case Type::StructTyID: {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000227 StructType *STy = cast<StructType>(Ty);
Andrew Trickec4b6e72011-09-30 19:48:58 +0000228
Chris Lattner01beceb2011-08-12 18:07:07 +0000229 if (STy->isLiteral())
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000230 return printStructBody(STy, OS);
231
232 if (!STy->getName().empty())
233 return PrintLLVMName(OS, STy->getName(), LocalPrefix);
Andrew Trickec4b6e72011-09-30 19:48:58 +0000234
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000235 DenseMap<StructType*, unsigned>::iterator I = NumberedTypes.find(STy);
236 if (I != NumberedTypes.end())
237 OS << '%' << I->second;
238 else // Not enumerated, print the hex address.
Benjamin Kramer4e8b4632011-11-02 17:24:36 +0000239 OS << "%\"type " << STy << '\"';
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000240 return;
Chris Lattner07d88292009-02-28 20:35:42 +0000241 }
242 case Type::PointerTyID: {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000243 PointerType *PTy = cast<PointerType>(Ty);
244 print(PTy->getElementType(), OS);
Chris Lattner07d88292009-02-28 20:35:42 +0000245 if (unsigned AddressSpace = PTy->getAddressSpace())
Chris Lattner06a23212009-02-28 21:27:31 +0000246 OS << " addrspace(" << AddressSpace << ')';
247 OS << '*';
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000248 return;
Chris Lattner07d88292009-02-28 20:35:42 +0000249 }
250 case Type::ArrayTyID: {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000251 ArrayType *ATy = cast<ArrayType>(Ty);
Chris Lattner06a23212009-02-28 21:27:31 +0000252 OS << '[' << ATy->getNumElements() << " x ";
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000253 print(ATy->getElementType(), OS);
Chris Lattner06a23212009-02-28 21:27:31 +0000254 OS << ']';
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000255 return;
Chris Lattner07d88292009-02-28 20:35:42 +0000256 }
257 case Type::VectorTyID: {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000258 VectorType *PTy = cast<VectorType>(Ty);
Chris Lattner06a23212009-02-28 21:27:31 +0000259 OS << "<" << PTy->getNumElements() << " x ";
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000260 print(PTy->getElementType(), OS);
Chris Lattner06a23212009-02-28 21:27:31 +0000261 OS << '>';
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000262 return;
Chris Lattner07d88292009-02-28 20:35:42 +0000263 }
Chris Lattner07d88292009-02-28 20:35:42 +0000264 default:
Chris Lattner06a23212009-02-28 21:27:31 +0000265 OS << "<unrecognized-type>";
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000266 return;
Chris Lattner0f578952009-02-28 20:25:14 +0000267 }
Chris Lattner0f578952009-02-28 20:25:14 +0000268}
269
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000270void TypePrinting::printStructBody(StructType *STy, raw_ostream &OS) {
271 if (STy->isOpaque()) {
272 OS << "opaque";
273 return;
Chris Lattner0f578952009-02-28 20:25:14 +0000274 }
Andrew Trickec4b6e72011-09-30 19:48:58 +0000275
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000276 if (STy->isPacked())
277 OS << '<';
Andrew Trickec4b6e72011-09-30 19:48:58 +0000278
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000279 if (STy->getNumElements() == 0) {
280 OS << "{}";
281 } else {
282 StructType::element_iterator I = STy->element_begin();
283 OS << "{ ";
284 print(*I++, OS);
285 for (StructType::element_iterator E = STy->element_end(); I != E; ++I) {
286 OS << ", ";
287 print(*I, OS);
Chris Lattner3243ea12009-03-01 00:03:38 +0000288 }
Andrew Trickec4b6e72011-09-30 19:48:58 +0000289
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000290 OS << " }";
Chris Lattner92c5c122009-02-28 23:20:19 +0000291 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000292 if (STy->isPacked())
293 OS << '>';
Chris Lattner92c5c122009-02-28 23:20:19 +0000294}
295
Chris Lattner3eee99c2008-08-19 04:36:02 +0000296//===----------------------------------------------------------------------===//
297// SlotTracker Class: Enumerate slot numbers for unnamed values
298//===----------------------------------------------------------------------===//
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000299/// This class provides computation of slot numbers for LLVM Assembly writing.
Chris Lattner393b7cd2008-08-17 04:17:45 +0000300///
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000301class SlotTracker {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000302public:
Devang Patel983c6b12009-07-08 21:44:25 +0000303 /// ValueMap - A mapping of Values to slot numbers.
Chris Lattnera204d412008-08-17 17:25:25 +0000304 typedef DenseMap<const Value*, unsigned> ValueMap;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000305
306private:
Devang Patel983c6b12009-07-08 21:44:25 +0000307 /// TheModule - The module for which we are holding slot numbers.
Chris Lattner393b7cd2008-08-17 04:17:45 +0000308 const Module* TheModule;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000309
Devang Patel983c6b12009-07-08 21:44:25 +0000310 /// TheFunction - The function for which we are holding slot numbers.
Chris Lattner393b7cd2008-08-17 04:17:45 +0000311 const Function* TheFunction;
312 bool FunctionProcessed;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000313
Jay Foaddbb221d2011-07-11 07:28:49 +0000314 /// mMap - The slot map for the module level data.
Chris Lattner393b7cd2008-08-17 04:17:45 +0000315 ValueMap mMap;
316 unsigned mNext;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000317
Jay Foaddbb221d2011-07-11 07:28:49 +0000318 /// fMap - The slot map for the function level data.
Chris Lattner393b7cd2008-08-17 04:17:45 +0000319 ValueMap fMap;
320 unsigned fNext;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000321
Devang Patel983c6b12009-07-08 21:44:25 +0000322 /// mdnMap - Map for MDNodes.
Chris Lattnercf4a76e2009-12-31 02:20:11 +0000323 DenseMap<const MDNode*, unsigned> mdnMap;
Devang Patel983c6b12009-07-08 21:44:25 +0000324 unsigned mdnNext;
Bill Wendling829b4782013-02-11 08:43:33 +0000325
326 /// asMap - The slot map for attribute sets.
327 DenseMap<AttributeSet, unsigned> asMap;
328 unsigned asNext;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000329public:
Chris Lattner393b7cd2008-08-17 04:17:45 +0000330 /// Construct from a module
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000331 explicit SlotTracker(const Module *M);
Chris Lattner393b7cd2008-08-17 04:17:45 +0000332 /// Construct from a function, starting out in incorp state.
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000333 explicit SlotTracker(const Function *F);
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000334
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000335 /// Return the slot number of the specified value in it's type
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000336 /// plane. If something is not in the SlotTracker, return -1.
Chris Lattner5e043322007-01-11 03:54:27 +0000337 int getLocalSlot(const Value *V);
338 int getGlobalSlot(const GlobalValue *V);
Devang Patel983c6b12009-07-08 21:44:25 +0000339 int getMetadataSlot(const MDNode *N);
Bill Wendling829b4782013-02-11 08:43:33 +0000340 int getAttributeGroupSlot(AttributeSet AS);
Reid Spencer8beac692004-06-09 15:26:53 +0000341
Misha Brukmanb1c93172005-04-21 23:48:37 +0000342 /// If you'd like to deal with a function instead of just a module, use
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000343 /// this method to get its data into the SlotTracker.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000344 void incorporateFunction(const Function *F) {
345 TheFunction = F;
Reid Spencerb0ac8c42004-08-16 07:46:33 +0000346 FunctionProcessed = false;
347 }
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000348
Misha Brukmanb1c93172005-04-21 23:48:37 +0000349 /// After calling incorporateFunction, use this method to remove the
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000350 /// most recently incorporated function from the SlotTracker. This
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000351 /// will reset the state of the machine back to just the module contents.
352 void purgeFunction();
353
Devang Patel983c6b12009-07-08 21:44:25 +0000354 /// MDNode map iterators.
Chris Lattnercf4a76e2009-12-31 02:20:11 +0000355 typedef DenseMap<const MDNode*, unsigned>::iterator mdn_iterator;
356 mdn_iterator mdn_begin() { return mdnMap.begin(); }
357 mdn_iterator mdn_end() { return mdnMap.end(); }
358 unsigned mdn_size() const { return mdnMap.size(); }
359 bool mdn_empty() const { return mdnMap.empty(); }
Devang Patel983c6b12009-07-08 21:44:25 +0000360
Bill Wendling829b4782013-02-11 08:43:33 +0000361 /// AttributeSet map iterators.
362 typedef DenseMap<AttributeSet, unsigned>::iterator as_iterator;
363 as_iterator as_begin() { return asMap.begin(); }
364 as_iterator as_end() { return asMap.end(); }
365 unsigned as_size() const { return asMap.size(); }
366 bool as_empty() const { return asMap.empty(); }
367
Reid Spencer56010e42004-05-26 21:56:09 +0000368 /// This function does the actual initialization.
369 inline void initialize();
370
Devang Patel983c6b12009-07-08 21:44:25 +0000371 // Implementation Details
372private:
Chris Lattnerea862a32007-01-09 07:55:49 +0000373 /// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
374 void CreateModuleSlot(const GlobalValue *V);
Devang Patel983c6b12009-07-08 21:44:25 +0000375
376 /// CreateMetadataSlot - Insert the specified MDNode* into the slot table.
377 void CreateMetadataSlot(const MDNode *N);
378
Chris Lattnerea862a32007-01-09 07:55:49 +0000379 /// CreateFunctionSlot - Insert the specified Value* into the slot table.
380 void CreateFunctionSlot(const Value *V);
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000381
Bill Wendling829b4782013-02-11 08:43:33 +0000382 /// \brief Insert the specified AttributeSet into the slot table.
383 void CreateAttributeSetSlot(AttributeSet AS);
384
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000385 /// Add all of the module level global variables (and their initializers)
386 /// and function declarations, but not the contents of those functions.
387 void processModule();
388
Devang Patel983c6b12009-07-08 21:44:25 +0000389 /// Add all of the functions arguments, basic blocks, and instructions.
Reid Spencer56010e42004-05-26 21:56:09 +0000390 void processFunction();
391
Craig Toppera60c0f12012-09-15 17:09:36 +0000392 SlotTracker(const SlotTracker &) LLVM_DELETED_FUNCTION;
393 void operator=(const SlotTracker &) LLVM_DELETED_FUNCTION;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000394};
395
Daniel Maleaded9f932013-05-08 20:38:31 +0000396SlotTracker *createSlotTracker(const Module *M) {
397 return new SlotTracker(M);
398}
Chris Lattner3eee99c2008-08-19 04:36:02 +0000399
400static SlotTracker *createSlotTracker(const Value *V) {
401 if (const Argument *FA = dyn_cast<Argument>(V))
402 return new SlotTracker(FA->getParent());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000403
Chris Lattner3eee99c2008-08-19 04:36:02 +0000404 if (const Instruction *I = dyn_cast<Instruction>(V))
Andrew Trick2f0cbf62011-09-30 19:50:40 +0000405 if (I->getParent())
406 return new SlotTracker(I->getParent()->getParent());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000407
Chris Lattner3eee99c2008-08-19 04:36:02 +0000408 if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
409 return new SlotTracker(BB->getParent());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000410
Chris Lattner3eee99c2008-08-19 04:36:02 +0000411 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
412 return new SlotTracker(GV->getParent());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000413
Chris Lattner3eee99c2008-08-19 04:36:02 +0000414 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V))
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000415 return new SlotTracker(GA->getParent());
416
Chris Lattner3eee99c2008-08-19 04:36:02 +0000417 if (const Function *Func = dyn_cast<Function>(V))
418 return new SlotTracker(Func);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000419
Dan Gohmana2489d12010-07-20 23:55:01 +0000420 if (const MDNode *MD = dyn_cast<MDNode>(V)) {
421 if (!MD->isFunctionLocal())
422 return new SlotTracker(MD->getFunction());
423
Dale Johannesen7b1a7ed2010-01-13 00:00:24 +0000424 return new SlotTracker((Function *)0);
Dan Gohmana2489d12010-07-20 23:55:01 +0000425 }
Dale Johannesen7b1a7ed2010-01-13 00:00:24 +0000426
Chris Lattner3eee99c2008-08-19 04:36:02 +0000427 return 0;
428}
429
430#if 0
David Greenec7f9b122010-01-05 01:29:26 +0000431#define ST_DEBUG(X) dbgs() << X
Chris Lattner3eee99c2008-08-19 04:36:02 +0000432#else
Chris Lattner604e3512008-08-19 04:47:09 +0000433#define ST_DEBUG(X)
Chris Lattner3eee99c2008-08-19 04:36:02 +0000434#endif
435
436// Module level constructor. Causes the contents of the Module (sans functions)
437// to be added to the slot table.
438SlotTracker::SlotTracker(const Module *M)
Andrew Trickec4b6e72011-09-30 19:48:58 +0000439 : TheModule(M), TheFunction(0), FunctionProcessed(false),
Bill Wendling829b4782013-02-11 08:43:33 +0000440 mNext(0), fNext(0), mdnNext(0), asNext(0) {
Chris Lattner3eee99c2008-08-19 04:36:02 +0000441}
442
443// Function level constructor. Causes the contents of the Module and the one
444// function provided to be added to the slot table.
445SlotTracker::SlotTracker(const Function *F)
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000446 : TheModule(F ? F->getParent() : 0), TheFunction(F), FunctionProcessed(false),
Bill Wendling829b4782013-02-11 08:43:33 +0000447 mNext(0), fNext(0), mdnNext(0), asNext(0) {
Chris Lattner3eee99c2008-08-19 04:36:02 +0000448}
449
450inline void SlotTracker::initialize() {
451 if (TheModule) {
452 processModule();
453 TheModule = 0; ///< Prevent re-processing next time we're called.
454 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000455
Chris Lattner3eee99c2008-08-19 04:36:02 +0000456 if (TheFunction && !FunctionProcessed)
457 processFunction();
458}
459
460// Iterate through all the global variables, functions, and global
461// variable initializers and create slots for them.
462void SlotTracker::processModule() {
Chris Lattner604e3512008-08-19 04:47:09 +0000463 ST_DEBUG("begin processModule!\n");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000464
Chris Lattner3eee99c2008-08-19 04:36:02 +0000465 // Add all of the unnamed global variables to the value table.
466 for (Module::const_global_iterator I = TheModule->global_begin(),
Devang Patel983c6b12009-07-08 21:44:25 +0000467 E = TheModule->global_end(); I != E; ++I) {
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000468 if (!I->hasName())
Chris Lattner3eee99c2008-08-19 04:36:02 +0000469 CreateModuleSlot(I);
Devang Patel983c6b12009-07-08 21:44:25 +0000470 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000471
Devang Patel23e68302009-07-29 22:04:47 +0000472 // Add metadata used by named metadata.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000473 for (Module::const_named_metadata_iterator
Devang Patel23e68302009-07-29 22:04:47 +0000474 I = TheModule->named_metadata_begin(),
475 E = TheModule->named_metadata_end(); I != E; ++I) {
476 const NamedMDNode *NMD = I;
Dan Gohman093cb792010-07-21 18:54:18 +0000477 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
478 CreateMetadataSlot(NMD->getOperand(i));
Devang Patel23e68302009-07-29 22:04:47 +0000479 }
480
Chris Lattner3eee99c2008-08-19 04:36:02 +0000481 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
Bill Wendling829b4782013-02-11 08:43:33 +0000482 I != E; ++I) {
Chris Lattner3eee99c2008-08-19 04:36:02 +0000483 if (!I->hasName())
Bill Wendling829b4782013-02-11 08:43:33 +0000484 // Add all the unnamed functions to the table.
Chris Lattner3eee99c2008-08-19 04:36:02 +0000485 CreateModuleSlot(I);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000486
Bill Wendling829b4782013-02-11 08:43:33 +0000487 // Add all the function attributes to the table.
Bill Wendling90bc19c2013-02-20 07:21:42 +0000488 // FIXME: Add attributes of other objects?
Bill Wendling829b4782013-02-11 08:43:33 +0000489 AttributeSet FnAttrs = I->getAttributes().getFnAttributes();
490 if (FnAttrs.hasAttributes(AttributeSet::FunctionIndex))
491 CreateAttributeSetSlot(FnAttrs);
492 }
493
Chris Lattner604e3512008-08-19 04:47:09 +0000494 ST_DEBUG("end processModule!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000495}
496
Chris Lattner3eee99c2008-08-19 04:36:02 +0000497// Process the arguments, basic blocks, and instructions of a function.
498void SlotTracker::processFunction() {
Chris Lattner604e3512008-08-19 04:47:09 +0000499 ST_DEBUG("begin processFunction!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000500 fNext = 0;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000501
Chris Lattner3eee99c2008-08-19 04:36:02 +0000502 // Add all the function arguments with no names.
503 for(Function::const_arg_iterator AI = TheFunction->arg_begin(),
504 AE = TheFunction->arg_end(); AI != AE; ++AI)
505 if (!AI->hasName())
506 CreateFunctionSlot(AI);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000507
Chris Lattner604e3512008-08-19 04:47:09 +0000508 ST_DEBUG("Inserting Instructions:\n");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000509
Chris Lattnerbddea6a2009-12-31 02:13:35 +0000510 SmallVector<std::pair<unsigned, MDNode*>, 4> MDForInst;
Devang Pateldec23fd2009-09-16 20:21:17 +0000511
Chris Lattner3eee99c2008-08-19 04:36:02 +0000512 // Add all of the basic blocks and instructions with no names.
513 for (Function::const_iterator BB = TheFunction->begin(),
514 E = TheFunction->end(); BB != E; ++BB) {
515 if (!BB->hasName())
516 CreateFunctionSlot(BB);
Andrew Trickec4b6e72011-09-30 19:48:58 +0000517
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000518 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E;
Devang Patel983c6b12009-07-08 21:44:25 +0000519 ++I) {
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000520 if (!I->getType()->isVoidTy() && !I->hasName())
Chris Lattner3eee99c2008-08-19 04:36:02 +0000521 CreateFunctionSlot(I);
Andrew Trickec4b6e72011-09-30 19:48:58 +0000522
Chris Lattner58aff8f2010-05-10 20:53:17 +0000523 // Intrinsics can directly use metadata. We allow direct calls to any
524 // llvm.foo function here, because the target may not be linked into the
525 // optimizer.
526 if (const CallInst *CI = dyn_cast<CallInst>(I)) {
527 if (Function *F = CI->getCalledFunction())
Matt Arsenaulta68c9ad2013-12-05 06:05:43 +0000528 if (F->isIntrinsic())
Chris Lattner58aff8f2010-05-10 20:53:17 +0000529 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
530 if (MDNode *N = dyn_cast_or_null<MDNode>(I->getOperand(i)))
531 CreateMetadataSlot(N);
Bill Wendling6da216f2013-02-20 00:04:41 +0000532
Bill Wendlinga0323742013-02-22 09:09:42 +0000533 // Add all the call attributes to the table.
534 AttributeSet Attrs = CI->getAttributes().getFnAttributes();
535 if (Attrs.hasAttributes(AttributeSet::FunctionIndex))
536 CreateAttributeSetSlot(Attrs);
537 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(I)) {
538 // Add all the call attributes to the table.
539 AttributeSet Attrs = II->getAttributes().getFnAttributes();
540 if (Attrs.hasAttributes(AttributeSet::FunctionIndex))
541 CreateAttributeSetSlot(Attrs);
Chris Lattner58aff8f2010-05-10 20:53:17 +0000542 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000543
Devang Pateldec23fd2009-09-16 20:21:17 +0000544 // Process metadata attached with this instruction.
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000545 I->getAllMetadata(MDForInst);
546 for (unsigned i = 0, e = MDForInst.size(); i != e; ++i)
547 CreateMetadataSlot(MDForInst[i].second);
Chris Lattnerbddea6a2009-12-31 02:13:35 +0000548 MDForInst.clear();
Devang Patel983c6b12009-07-08 21:44:25 +0000549 }
Chris Lattner3eee99c2008-08-19 04:36:02 +0000550 }
Devang Pateldec23fd2009-09-16 20:21:17 +0000551
Chris Lattner3eee99c2008-08-19 04:36:02 +0000552 FunctionProcessed = true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000553
Chris Lattner604e3512008-08-19 04:47:09 +0000554 ST_DEBUG("end processFunction!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000555}
556
557/// Clean up after incorporating a function. This is the only way to get out of
558/// the function incorporation state that affects get*Slot/Create*Slot. Function
559/// incorporation state is indicated by TheFunction != 0.
560void SlotTracker::purgeFunction() {
Chris Lattner604e3512008-08-19 04:47:09 +0000561 ST_DEBUG("begin purgeFunction!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000562 fMap.clear(); // Simply discard the function level map
563 TheFunction = 0;
564 FunctionProcessed = false;
Chris Lattner604e3512008-08-19 04:47:09 +0000565 ST_DEBUG("end purgeFunction!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000566}
567
568/// getGlobalSlot - Get the slot number of a global value.
569int SlotTracker::getGlobalSlot(const GlobalValue *V) {
570 // Check for uninitialized state and do lazy initialization.
571 initialize();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000572
Jay Foaddbb221d2011-07-11 07:28:49 +0000573 // Find the value in the module map
Chris Lattner3eee99c2008-08-19 04:36:02 +0000574 ValueMap::iterator MI = mMap.find(V);
Dan Gohman1dd27572008-10-01 19:58:59 +0000575 return MI == mMap.end() ? -1 : (int)MI->second;
Chris Lattner3eee99c2008-08-19 04:36:02 +0000576}
577
Chris Lattnercf4a76e2009-12-31 02:20:11 +0000578/// getMetadataSlot - Get the slot number of a MDNode.
Devang Patel983c6b12009-07-08 21:44:25 +0000579int SlotTracker::getMetadataSlot(const MDNode *N) {
580 // Check for uninitialized state and do lazy initialization.
581 initialize();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000582
Jay Foaddbb221d2011-07-11 07:28:49 +0000583 // Find the MDNode in the module map
Chris Lattnercf4a76e2009-12-31 02:20:11 +0000584 mdn_iterator MI = mdnMap.find(N);
Devang Patel983c6b12009-07-08 21:44:25 +0000585 return MI == mdnMap.end() ? -1 : (int)MI->second;
586}
587
Chris Lattner3eee99c2008-08-19 04:36:02 +0000588
589/// getLocalSlot - Get the slot number for a value that is local to a function.
590int SlotTracker::getLocalSlot(const Value *V) {
591 assert(!isa<Constant>(V) && "Can't get a constant or global slot with this!");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000592
Chris Lattner3eee99c2008-08-19 04:36:02 +0000593 // Check for uninitialized state and do lazy initialization.
594 initialize();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000595
Chris Lattner3eee99c2008-08-19 04:36:02 +0000596 ValueMap::iterator FI = fMap.find(V);
Dan Gohman1dd27572008-10-01 19:58:59 +0000597 return FI == fMap.end() ? -1 : (int)FI->second;
Chris Lattner3eee99c2008-08-19 04:36:02 +0000598}
599
Bill Wendling829b4782013-02-11 08:43:33 +0000600int SlotTracker::getAttributeGroupSlot(AttributeSet AS) {
601 // Check for uninitialized state and do lazy initialization.
602 initialize();
603
604 // Find the AttributeSet in the module map.
605 as_iterator AI = asMap.find(AS);
606 return AI == asMap.end() ? -1 : (int)AI->second;
607}
Chris Lattner3eee99c2008-08-19 04:36:02 +0000608
609/// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
610void SlotTracker::CreateModuleSlot(const GlobalValue *V) {
611 assert(V && "Can't insert a null Value into SlotTracker!");
Chris Lattner031560c2009-12-29 07:25:48 +0000612 assert(!V->getType()->isVoidTy() && "Doesn't need a slot!");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000613 assert(!V->hasName() && "Doesn't need a slot!");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000614
Chris Lattner3eee99c2008-08-19 04:36:02 +0000615 unsigned DestSlot = mNext++;
616 mMap[V] = DestSlot;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000617
Chris Lattner604e3512008-08-19 04:47:09 +0000618 ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" <<
Chris Lattner3eee99c2008-08-19 04:36:02 +0000619 DestSlot << " [");
620 // G = Global, F = Function, A = Alias, o = other
Chris Lattner604e3512008-08-19 04:47:09 +0000621 ST_DEBUG((isa<GlobalVariable>(V) ? 'G' :
Chris Lattner3eee99c2008-08-19 04:36:02 +0000622 (isa<Function>(V) ? 'F' :
623 (isa<GlobalAlias>(V) ? 'A' : 'o'))) << "]\n");
624}
625
Chris Lattner3eee99c2008-08-19 04:36:02 +0000626/// CreateSlot - Create a new slot for the specified value if it has no name.
627void SlotTracker::CreateFunctionSlot(const Value *V) {
Chris Lattner031560c2009-12-29 07:25:48 +0000628 assert(!V->getType()->isVoidTy() && !V->hasName() && "Doesn't need a slot!");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000629
Chris Lattner3eee99c2008-08-19 04:36:02 +0000630 unsigned DestSlot = fNext++;
631 fMap[V] = DestSlot;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000632
Chris Lattner3eee99c2008-08-19 04:36:02 +0000633 // G = Global, F = Function, o = other
Chris Lattner604e3512008-08-19 04:47:09 +0000634 ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" <<
Chris Lattner3eee99c2008-08-19 04:36:02 +0000635 DestSlot << " [o]\n");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000636}
Chris Lattner3eee99c2008-08-19 04:36:02 +0000637
Devang Patel983c6b12009-07-08 21:44:25 +0000638/// CreateModuleSlot - Insert the specified MDNode* into the slot table.
639void SlotTracker::CreateMetadataSlot(const MDNode *N) {
640 assert(N && "Can't insert a null Value into SlotTracker!");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000641
Chris Lattner0d50bdd2009-12-31 02:27:30 +0000642 // Don't insert if N is a function-local metadata, these are always printed
643 // inline.
Dan Gohmana2489d12010-07-20 23:55:01 +0000644 if (!N->isFunctionLocal()) {
645 mdn_iterator I = mdnMap.find(N);
646 if (I != mdnMap.end())
647 return;
Victor Hernandez4d633542009-12-04 20:07:10 +0000648
Dan Gohmana2489d12010-07-20 23:55:01 +0000649 unsigned DestSlot = mdnNext++;
650 mdnMap[N] = DestSlot;
651 }
Devang Patel983c6b12009-07-08 21:44:25 +0000652
Chris Lattner0d50bdd2009-12-31 02:27:30 +0000653 // Recursively add any MDNodes referenced by operands.
654 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
655 if (const MDNode *Op = dyn_cast_or_null<MDNode>(N->getOperand(i)))
656 CreateMetadataSlot(Op);
Devang Patel983c6b12009-07-08 21:44:25 +0000657}
Chris Lattner3eee99c2008-08-19 04:36:02 +0000658
Bill Wendling829b4782013-02-11 08:43:33 +0000659void SlotTracker::CreateAttributeSetSlot(AttributeSet AS) {
660 assert(AS.hasAttributes(AttributeSet::FunctionIndex) &&
661 "Doesn't need a slot!");
662
663 as_iterator I = asMap.find(AS);
664 if (I != asMap.end())
665 return;
666
667 unsigned DestSlot = asNext++;
668 asMap[AS] = DestSlot;
669}
670
Chris Lattner3eee99c2008-08-19 04:36:02 +0000671//===----------------------------------------------------------------------===//
672// AsmWriter Implementation
673//===----------------------------------------------------------------------===//
Chris Lattner7f8845a2002-07-23 18:07:49 +0000674
Dan Gohman12dad632009-08-12 20:56:03 +0000675static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
Dan Gohman8061d9e2009-08-13 15:27:57 +0000676 TypePrinting *TypePrinter,
Dan Gohmana2489d12010-07-20 23:55:01 +0000677 SlotTracker *Machine,
678 const Module *Context);
Reid Spencer58d30f22004-07-04 11:50:43 +0000679
Chris Lattner033935d2008-08-17 04:40:13 +0000680
Chris Lattnerb86620e2001-10-29 16:37:48 +0000681
Chris Lattnerfc9f1c92006-12-06 06:40:49 +0000682static const char *getPredicateText(unsigned predicate) {
Reid Spencer812a1be2006-12-04 05:19:18 +0000683 const char * pred = "unknown";
684 switch (predicate) {
Chris Lattnerbddea6a2009-12-31 02:13:35 +0000685 case FCmpInst::FCMP_FALSE: pred = "false"; break;
686 case FCmpInst::FCMP_OEQ: pred = "oeq"; break;
687 case FCmpInst::FCMP_OGT: pred = "ogt"; break;
688 case FCmpInst::FCMP_OGE: pred = "oge"; break;
689 case FCmpInst::FCMP_OLT: pred = "olt"; break;
690 case FCmpInst::FCMP_OLE: pred = "ole"; break;
691 case FCmpInst::FCMP_ONE: pred = "one"; break;
692 case FCmpInst::FCMP_ORD: pred = "ord"; break;
693 case FCmpInst::FCMP_UNO: pred = "uno"; break;
694 case FCmpInst::FCMP_UEQ: pred = "ueq"; break;
695 case FCmpInst::FCMP_UGT: pred = "ugt"; break;
696 case FCmpInst::FCMP_UGE: pred = "uge"; break;
697 case FCmpInst::FCMP_ULT: pred = "ult"; break;
698 case FCmpInst::FCMP_ULE: pred = "ule"; break;
699 case FCmpInst::FCMP_UNE: pred = "une"; break;
700 case FCmpInst::FCMP_TRUE: pred = "true"; break;
701 case ICmpInst::ICMP_EQ: pred = "eq"; break;
702 case ICmpInst::ICMP_NE: pred = "ne"; break;
703 case ICmpInst::ICMP_SGT: pred = "sgt"; break;
704 case ICmpInst::ICMP_SGE: pred = "sge"; break;
705 case ICmpInst::ICMP_SLT: pred = "slt"; break;
706 case ICmpInst::ICMP_SLE: pred = "sle"; break;
707 case ICmpInst::ICMP_UGT: pred = "ugt"; break;
708 case ICmpInst::ICMP_UGE: pred = "uge"; break;
709 case ICmpInst::ICMP_ULT: pred = "ult"; break;
710 case ICmpInst::ICMP_ULE: pred = "ule"; break;
Reid Spencer812a1be2006-12-04 05:19:18 +0000711 }
712 return pred;
713}
714
Eli Friedmanc9a551e2011-07-28 21:48:00 +0000715static void writeAtomicRMWOperation(raw_ostream &Out,
716 AtomicRMWInst::BinOp Op) {
717 switch (Op) {
718 default: Out << " <unknown operation " << Op << ">"; break;
719 case AtomicRMWInst::Xchg: Out << " xchg"; break;
720 case AtomicRMWInst::Add: Out << " add"; break;
721 case AtomicRMWInst::Sub: Out << " sub"; break;
722 case AtomicRMWInst::And: Out << " and"; break;
723 case AtomicRMWInst::Nand: Out << " nand"; break;
724 case AtomicRMWInst::Or: Out << " or"; break;
725 case AtomicRMWInst::Xor: Out << " xor"; break;
726 case AtomicRMWInst::Max: Out << " max"; break;
727 case AtomicRMWInst::Min: Out << " min"; break;
728 case AtomicRMWInst::UMax: Out << " umax"; break;
729 case AtomicRMWInst::UMin: Out << " umin"; break;
730 }
731}
Devang Patel983c6b12009-07-08 21:44:25 +0000732
Dan Gohman12dad632009-08-12 20:56:03 +0000733static void WriteOptimizationInfo(raw_ostream &Out, const User *U) {
Michael Ilseman92053172012-11-27 00:42:44 +0000734 if (const FPMathOperator *FPO = dyn_cast<const FPMathOperator>(U)) {
735 // Unsafe algebra implies all the others, no need to write them all out
736 if (FPO->hasUnsafeAlgebra())
737 Out << " fast";
738 else {
739 if (FPO->hasNoNaNs())
740 Out << " nnan";
741 if (FPO->hasNoInfs())
742 Out << " ninf";
743 if (FPO->hasNoSignedZeros())
744 Out << " nsz";
745 if (FPO->hasAllowReciprocal())
746 Out << " arcp";
747 }
748 }
749
Dan Gohman0ebd6962009-07-20 21:19:07 +0000750 if (const OverflowingBinaryOperator *OBO =
751 dyn_cast<OverflowingBinaryOperator>(U)) {
Dan Gohman16f54152009-08-20 17:11:38 +0000752 if (OBO->hasNoUnsignedWrap())
Dan Gohman9c7f8082009-07-27 16:11:46 +0000753 Out << " nuw";
Dan Gohman16f54152009-08-20 17:11:38 +0000754 if (OBO->hasNoSignedWrap())
Dan Gohman9c7f8082009-07-27 16:11:46 +0000755 Out << " nsw";
Chris Lattner35315d02011-02-06 21:44:57 +0000756 } else if (const PossiblyExactOperator *Div =
757 dyn_cast<PossiblyExactOperator>(U)) {
Dan Gohman0ebd6962009-07-20 21:19:07 +0000758 if (Div->isExact())
Dan Gohman9c7f8082009-07-27 16:11:46 +0000759 Out << " exact";
Dan Gohman1639c392009-07-27 21:53:46 +0000760 } else if (const GEPOperator *GEP = dyn_cast<GEPOperator>(U)) {
761 if (GEP->isInBounds())
762 Out << " inbounds";
Dan Gohman0ebd6962009-07-20 21:19:07 +0000763 }
764}
765
Dan Gohmanefb8dbb2010-07-14 20:57:55 +0000766static void WriteConstantInternal(raw_ostream &Out, const Constant *CV,
767 TypePrinting &TypePrinter,
Dan Gohmana2489d12010-07-20 23:55:01 +0000768 SlotTracker *Machine,
769 const Module *Context) {
Zhou Sheng75b871f2007-01-11 12:24:14 +0000770 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Duncan Sands9dff9be2010-02-15 16:12:20 +0000771 if (CI->getType()->isIntegerTy(1)) {
Reid Spencercddc9df2007-01-12 04:24:46 +0000772 Out << (CI->getZExtValue() ? "true" : "false");
Chris Lattner17f71652008-08-17 07:19:36 +0000773 return;
774 }
775 Out << CI->getValue();
776 return;
777 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000778
Chris Lattner17f71652008-08-17 07:19:36 +0000779 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Tobias Grosser6b31d172012-05-24 15:59:06 +0000780 if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEsingle ||
Dan Gohman518cda42011-12-17 00:04:22 +0000781 &CFP->getValueAPF().getSemantics() == &APFloat::IEEEdouble) {
Dale Johannesen028084e2007-09-12 03:30:33 +0000782 // We would like to output the FP constant value in exponential notation,
783 // but we cannot do this if doing so will lose precision. Check here to
784 // make sure that we only output it in exponential format if we can parse
785 // the value back and get the same value.
786 //
Dale Johannesen1f864982009-01-21 20:32:55 +0000787 bool ignored;
Dan Gohman518cda42011-12-17 00:04:22 +0000788 bool isHalf = &CFP->getValueAPF().getSemantics()==&APFloat::IEEEhalf;
Dale Johannesen028084e2007-09-12 03:30:33 +0000789 bool isDouble = &CFP->getValueAPF().getSemantics()==&APFloat::IEEEdouble;
NAKAMURA Takumi35d19c02012-02-16 08:12:24 +0000790 bool isInf = CFP->getValueAPF().isInfinity();
791 bool isNaN = CFP->getValueAPF().isNaN();
792 if (!isHalf && !isInf && !isNaN) {
Dan Gohman518cda42011-12-17 00:04:22 +0000793 double Val = isDouble ? CFP->getValueAPF().convertToDouble() :
794 CFP->getValueAPF().convertToFloat();
795 SmallString<128> StrVal;
796 raw_svector_ostream(StrVal) << Val;
Chris Lattner1e194682002-04-18 18:53:13 +0000797
Dan Gohman518cda42011-12-17 00:04:22 +0000798 // Check to make sure that the stringized number is not some string like
799 // "Inf" or NaN, that atof will accept, but the lexer will not. Check
800 // that the string matches the "[-+]?[0-9]" regex.
801 //
802 if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
803 ((StrVal[0] == '-' || StrVal[0] == '+') &&
804 (StrVal[1] >= '0' && StrVal[1] <= '9'))) {
805 // Reparse stringized version!
NAKAMURA Takumiaec41232012-02-16 04:19:15 +0000806 if (APFloat(APFloat::IEEEdouble, StrVal).convertToDouble() == Val) {
Dan Gohman518cda42011-12-17 00:04:22 +0000807 Out << StrVal.str();
808 return;
809 }
Dale Johannesen028084e2007-09-12 03:30:33 +0000810 }
Chris Lattner1e194682002-04-18 18:53:13 +0000811 }
Dale Johannesen028084e2007-09-12 03:30:33 +0000812 // Otherwise we could not reparse it to exactly the same value, so we must
Dale Johannesen1f864982009-01-21 20:32:55 +0000813 // output the string in hexadecimal format! Note that loading and storing
814 // floating point types changes the bits of NaNs on some hosts, notably
815 // x86, so we must not use these types.
Dale Johannesen028084e2007-09-12 03:30:33 +0000816 assert(sizeof(double) == sizeof(uint64_t) &&
817 "assuming that double is 64 bits!");
Chris Lattner5505eed2008-11-10 04:30:26 +0000818 char Buffer[40];
Dale Johannesen1f864982009-01-21 20:32:55 +0000819 APFloat apf = CFP->getValueAPF();
Dan Gohman518cda42011-12-17 00:04:22 +0000820 // Halves and floats are represented in ASCII IR as double, convert.
Dale Johannesen1f864982009-01-21 20:32:55 +0000821 if (!isDouble)
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000822 apf.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven,
Dale Johannesen1f864982009-01-21 20:32:55 +0000823 &ignored);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000824 Out << "0x" <<
825 utohex_buffer(uint64_t(apf.bitcastToAPInt().getZExtValue()),
Dale Johannesen1f864982009-01-21 20:32:55 +0000826 Buffer+40);
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000827 return;
828 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000829
Tobias Grosser6b31d172012-05-24 15:59:06 +0000830 // Either half, or some form of long double.
831 // These appear as a magic letter identifying the type, then a
832 // fixed number of hex digits.
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000833 Out << "0x";
Tobias Grosser6b31d172012-05-24 15:59:06 +0000834 // Bit position, in the current word, of the next nibble to print.
835 int shiftcount;
836
Dale Johannesen93eefa02009-03-23 21:16:53 +0000837 if (&CFP->getValueAPF().getSemantics() == &APFloat::x87DoubleExtended) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000838 Out << 'K';
Dale Johannesen93eefa02009-03-23 21:16:53 +0000839 // api needed to prevent premature destruction
840 APInt api = CFP->getValueAPF().bitcastToAPInt();
841 const uint64_t* p = api.getRawData();
842 uint64_t word = p[1];
Tobias Grosser6b31d172012-05-24 15:59:06 +0000843 shiftcount = 12;
Dale Johannesen93eefa02009-03-23 21:16:53 +0000844 int width = api.getBitWidth();
845 for (int j=0; j<width; j+=4, shiftcount-=4) {
846 unsigned int nibble = (word>>shiftcount) & 15;
847 if (nibble < 10)
848 Out << (unsigned char)(nibble + '0');
849 else
850 Out << (unsigned char)(nibble - 10 + 'A');
851 if (shiftcount == 0 && j+4 < width) {
852 word = *p;
853 shiftcount = 64;
854 if (width-j-4 < 64)
855 shiftcount = width-j-4;
856 }
857 }
858 return;
Tobias Grosser6b31d172012-05-24 15:59:06 +0000859 } else if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEquad) {
860 shiftcount = 60;
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000861 Out << 'L';
Tobias Grosser6b31d172012-05-24 15:59:06 +0000862 } else if (&CFP->getValueAPF().getSemantics() == &APFloat::PPCDoubleDouble) {
863 shiftcount = 60;
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000864 Out << 'M';
Tobias Grosser6b31d172012-05-24 15:59:06 +0000865 } else if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEhalf) {
866 shiftcount = 12;
867 Out << 'H';
868 } else
Torok Edwinfbcc6632009-07-14 16:55:14 +0000869 llvm_unreachable("Unsupported floating point type");
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000870 // api needed to prevent premature destruction
Dale Johannesen54306fe2008-10-09 18:53:47 +0000871 APInt api = CFP->getValueAPF().bitcastToAPInt();
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000872 const uint64_t* p = api.getRawData();
873 uint64_t word = *p;
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000874 int width = api.getBitWidth();
875 for (int j=0; j<width; j+=4, shiftcount-=4) {
876 unsigned int nibble = (word>>shiftcount) & 15;
877 if (nibble < 10)
878 Out << (unsigned char)(nibble + '0');
Dale Johannesen028084e2007-09-12 03:30:33 +0000879 else
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000880 Out << (unsigned char)(nibble - 10 + 'A');
881 if (shiftcount == 0 && j+4 < width) {
882 word = *(++p);
883 shiftcount = 64;
884 if (width-j-4 < 64)
885 shiftcount = width-j-4;
Dale Johannesen028084e2007-09-12 03:30:33 +0000886 }
887 }
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000888 return;
889 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000890
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000891 if (isa<ConstantAggregateZero>(CV)) {
Chris Lattner76b2ff42004-02-15 05:55:15 +0000892 Out << "zeroinitializer";
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000893 return;
894 }
Andrew Trickec4b6e72011-09-30 19:48:58 +0000895
Chris Lattner214cc702009-10-28 03:38:12 +0000896 if (const BlockAddress *BA = dyn_cast<BlockAddress>(CV)) {
897 Out << "blockaddress(";
Dan Gohmana2489d12010-07-20 23:55:01 +0000898 WriteAsOperandInternal(Out, BA->getFunction(), &TypePrinter, Machine,
899 Context);
Chris Lattner214cc702009-10-28 03:38:12 +0000900 Out << ", ";
Dan Gohmana2489d12010-07-20 23:55:01 +0000901 WriteAsOperandInternal(Out, BA->getBasicBlock(), &TypePrinter, Machine,
902 Context);
Chris Lattner214cc702009-10-28 03:38:12 +0000903 Out << ")";
904 return;
905 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000906
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000907 if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000908 Type *ETy = CA->getType()->getElementType();
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000909 Out << '[';
910 TypePrinter.print(ETy, Out);
911 Out << ' ';
912 WriteAsOperandInternal(Out, CA->getOperand(0),
913 &TypePrinter, Machine,
914 Context);
915 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
916 Out << ", ";
Chris Lattner9c181f62012-01-31 03:15:40 +0000917 TypePrinter.print(ETy, Out);
918 Out << ' ';
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000919 WriteAsOperandInternal(Out, CA->getOperand(i), &TypePrinter, Machine,
Chris Lattner9c181f62012-01-31 03:15:40 +0000920 Context);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000921 }
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000922 Out << ']';
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000923 return;
924 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000925
Chris Lattnerfa775002012-01-26 02:32:04 +0000926 if (const ConstantDataArray *CA = dyn_cast<ConstantDataArray>(CV)) {
927 // As a special case, print the array as a string if it is an array of
928 // i8 with ConstantInt values.
929 if (CA->isString()) {
930 Out << "c\"";
931 PrintEscapedString(CA->getAsString(), Out);
932 Out << '"';
933 return;
934 }
935
936 Type *ETy = CA->getType()->getElementType();
937 Out << '[';
Chris Lattner9c181f62012-01-31 03:15:40 +0000938 TypePrinter.print(ETy, Out);
939 Out << ' ';
940 WriteAsOperandInternal(Out, CA->getElementAsConstant(0),
941 &TypePrinter, Machine,
942 Context);
943 for (unsigned i = 1, e = CA->getNumElements(); i != e; ++i) {
944 Out << ", ";
Chris Lattnerfa775002012-01-26 02:32:04 +0000945 TypePrinter.print(ETy, Out);
946 Out << ' ';
Chris Lattner9c181f62012-01-31 03:15:40 +0000947 WriteAsOperandInternal(Out, CA->getElementAsConstant(i), &TypePrinter,
948 Machine, Context);
Chris Lattnerfa775002012-01-26 02:32:04 +0000949 }
Chris Lattner9c181f62012-01-31 03:15:40 +0000950 Out << ']';
Chris Lattnerfa775002012-01-26 02:32:04 +0000951 return;
952 }
953
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000954
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000955 if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
Andrew Lenharth0d124b82007-01-08 18:21:30 +0000956 if (CS->getType()->isPacked())
957 Out << '<';
Misha Brukman21bbdb92004-06-04 21:11:51 +0000958 Out << '{';
Jim Laskey3bb78742006-02-25 12:27:03 +0000959 unsigned N = CS->getNumOperands();
960 if (N) {
Chris Lattner604e3512008-08-19 04:47:09 +0000961 Out << ' ';
Chris Lattnere101c442009-02-28 21:26:53 +0000962 TypePrinter.print(CS->getOperand(0)->getType(), Out);
Dan Gohman81313fd2008-09-14 17:21:12 +0000963 Out << ' ';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000964
Dan Gohmana2489d12010-07-20 23:55:01 +0000965 WriteAsOperandInternal(Out, CS->getOperand(0), &TypePrinter, Machine,
966 Context);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000967
Jim Laskey3bb78742006-02-25 12:27:03 +0000968 for (unsigned i = 1; i < N; i++) {
Chris Lattnerd84bb632002-04-16 21:36:08 +0000969 Out << ", ";
Chris Lattnere101c442009-02-28 21:26:53 +0000970 TypePrinter.print(CS->getOperand(i)->getType(), Out);
Dan Gohman81313fd2008-09-14 17:21:12 +0000971 Out << ' ';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000972
Dan Gohmana2489d12010-07-20 23:55:01 +0000973 WriteAsOperandInternal(Out, CS->getOperand(i), &TypePrinter, Machine,
974 Context);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000975 }
Dan Gohman81313fd2008-09-14 17:21:12 +0000976 Out << ' ';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000977 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000978
Dan Gohman81313fd2008-09-14 17:21:12 +0000979 Out << '}';
Andrew Lenharth0d124b82007-01-08 18:21:30 +0000980 if (CS->getType()->isPacked())
981 Out << '>';
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000982 return;
983 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000984
Chris Lattnerfa775002012-01-26 02:32:04 +0000985 if (isa<ConstantVector>(CV) || isa<ConstantDataVector>(CV)) {
986 Type *ETy = CV->getType()->getVectorElementType();
Dan Gohman1262a252009-02-11 00:25:25 +0000987 Out << '<';
Chris Lattnere101c442009-02-28 21:26:53 +0000988 TypePrinter.print(ETy, Out);
Dan Gohman81313fd2008-09-14 17:21:12 +0000989 Out << ' ';
Chris Lattnerfa775002012-01-26 02:32:04 +0000990 WriteAsOperandInternal(Out, CV->getAggregateElement(0U), &TypePrinter,
991 Machine, Context);
992 for (unsigned i = 1, e = CV->getType()->getVectorNumElements(); i != e;++i){
Chris Lattner585297e82008-08-19 05:26:17 +0000993 Out << ", ";
Chris Lattnere101c442009-02-28 21:26:53 +0000994 TypePrinter.print(ETy, Out);
Dan Gohman81313fd2008-09-14 17:21:12 +0000995 Out << ' ';
Chris Lattnerfa775002012-01-26 02:32:04 +0000996 WriteAsOperandInternal(Out, CV->getAggregateElement(i), &TypePrinter,
997 Machine, Context);
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000998 }
Dan Gohman1262a252009-02-11 00:25:25 +0000999 Out << '>';
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001000 return;
1001 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001002
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001003 if (isa<ConstantPointerNull>(CV)) {
Chris Lattnerd84bb632002-04-16 21:36:08 +00001004 Out << "null";
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001005 return;
1006 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001007
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001008 if (isa<UndefValue>(CV)) {
Chris Lattner5e0b9f22004-10-16 18:08:06 +00001009 Out << "undef";
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001010 return;
1011 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001012
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001013 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Reid Spencer812a1be2006-12-04 05:19:18 +00001014 Out << CE->getOpcodeName();
Dan Gohman9c7f8082009-07-27 16:11:46 +00001015 WriteOptimizationInfo(Out, CE);
Reid Spencer812a1be2006-12-04 05:19:18 +00001016 if (CE->isCompare())
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001017 Out << ' ' << getPredicateText(CE->getPredicate());
Reid Spencer812a1be2006-12-04 05:19:18 +00001018 Out << " (";
Misha Brukmanb1c93172005-04-21 23:48:37 +00001019
Vikram S. Adveb952b542002-07-14 23:14:45 +00001020 for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) {
Chris Lattnere101c442009-02-28 21:26:53 +00001021 TypePrinter.print((*OI)->getType(), Out);
Dan Gohman81313fd2008-09-14 17:21:12 +00001022 Out << ' ';
Dan Gohmana2489d12010-07-20 23:55:01 +00001023 WriteAsOperandInternal(Out, *OI, &TypePrinter, Machine, Context);
Vikram S. Adveb952b542002-07-14 23:14:45 +00001024 if (OI+1 != CE->op_end())
Chris Lattner3cd8c562002-07-30 18:54:25 +00001025 Out << ", ";
Vikram S. Adveb952b542002-07-14 23:14:45 +00001026 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001027
Dan Gohmana76f0f72008-05-31 19:12:39 +00001028 if (CE->hasIndices()) {
Jay Foad0091fe82011-04-13 15:22:40 +00001029 ArrayRef<unsigned> Indices = CE->getIndices();
Dan Gohmana76f0f72008-05-31 19:12:39 +00001030 for (unsigned i = 0, e = Indices.size(); i != e; ++i)
1031 Out << ", " << Indices[i];
1032 }
1033
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001034 if (CE->isCast()) {
Chris Lattner83b396b2002-08-15 19:37:43 +00001035 Out << " to ";
Chris Lattnere101c442009-02-28 21:26:53 +00001036 TypePrinter.print(CE->getType(), Out);
Chris Lattner83b396b2002-08-15 19:37:43 +00001037 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001038
Misha Brukman21bbdb92004-06-04 21:11:51 +00001039 Out << ')';
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001040 return;
Chris Lattnerd84bb632002-04-16 21:36:08 +00001041 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001042
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001043 Out << "<placeholder or erroneous Constant>";
Chris Lattnerd84bb632002-04-16 21:36:08 +00001044}
1045
Chris Lattnercdec5812009-12-31 02:31:59 +00001046static void WriteMDNodeBodyInternal(raw_ostream &Out, const MDNode *Node,
1047 TypePrinting *TypePrinter,
Dan Gohmana2489d12010-07-20 23:55:01 +00001048 SlotTracker *Machine,
1049 const Module *Context) {
Chris Lattnercdec5812009-12-31 02:31:59 +00001050 Out << "!{";
1051 for (unsigned mi = 0, me = Node->getNumOperands(); mi != me; ++mi) {
1052 const Value *V = Node->getOperand(mi);
1053 if (V == 0)
1054 Out << "null";
1055 else {
1056 TypePrinter->print(V->getType(), Out);
1057 Out << ' ';
Andrew Trickec4b6e72011-09-30 19:48:58 +00001058 WriteAsOperandInternal(Out, Node->getOperand(mi),
Dan Gohmana2489d12010-07-20 23:55:01 +00001059 TypePrinter, Machine, Context);
Chris Lattnercdec5812009-12-31 02:31:59 +00001060 }
1061 if (mi + 1 != me)
1062 Out << ", ";
1063 }
Andrew Trickec4b6e72011-09-30 19:48:58 +00001064
Chris Lattnercdec5812009-12-31 02:31:59 +00001065 Out << "}";
1066}
1067
Chris Lattnerd84bb632002-04-16 21:36:08 +00001068
Misha Brukmanc566ca362004-03-02 00:22:19 +00001069/// WriteAsOperand - Write the name of the specified value out to the specified
1070/// ostream. This can be useful when you just want to print int %reg126, not
1071/// the whole instruction that generated it.
1072///
Dan Gohman12dad632009-08-12 20:56:03 +00001073static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
Dan Gohman8061d9e2009-08-13 15:27:57 +00001074 TypePrinting *TypePrinter,
Dan Gohmana2489d12010-07-20 23:55:01 +00001075 SlotTracker *Machine,
1076 const Module *Context) {
Chris Lattner033935d2008-08-17 04:40:13 +00001077 if (V->hasName()) {
1078 PrintLLVMName(Out, V);
1079 return;
1080 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001081
Chris Lattner033935d2008-08-17 04:40:13 +00001082 const Constant *CV = dyn_cast<Constant>(V);
1083 if (CV && !isa<GlobalValue>(CV)) {
Dan Gohman8061d9e2009-08-13 15:27:57 +00001084 assert(TypePrinter && "Constants require TypePrinting!");
Dan Gohmana2489d12010-07-20 23:55:01 +00001085 WriteConstantInternal(Out, CV, *TypePrinter, Machine, Context);
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001086 return;
1087 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001088
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001089 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
Chris Lattner033935d2008-08-17 04:40:13 +00001090 Out << "asm ";
1091 if (IA->hasSideEffects())
1092 Out << "sideeffect ";
Dale Johannesen1cfb9582009-10-21 23:28:00 +00001093 if (IA->isAlignStack())
1094 Out << "alignstack ";
Chad Rosierd8c76102012-09-05 19:00:49 +00001095 // We don't emit the AD_ATT dialect as it's the assumed default.
1096 if (IA->getDialect() == InlineAsm::AD_Intel)
1097 Out << "inteldialect ";
Chris Lattner033935d2008-08-17 04:40:13 +00001098 Out << '"';
1099 PrintEscapedString(IA->getAsmString(), Out);
1100 Out << "\", \"";
1101 PrintEscapedString(IA->getConstraintString(), Out);
1102 Out << '"';
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001103 return;
1104 }
Devang Patel7428d8a2009-07-22 17:43:22 +00001105
Devang Patele059ba6e2009-07-23 01:07:34 +00001106 if (const MDNode *N = dyn_cast<MDNode>(V)) {
Victor Hernandez0471abd2009-12-18 20:09:14 +00001107 if (N->isFunctionLocal()) {
Victor Hernandezb7176a12009-12-04 01:35:02 +00001108 // Print metadata inline, not via slot reference number.
Dan Gohmana2489d12010-07-20 23:55:01 +00001109 WriteMDNodeBodyInternal(Out, N, TypePrinter, Machine, Context);
Victor Hernandezb7176a12009-12-04 01:35:02 +00001110 return;
1111 }
Andrew Trickec4b6e72011-09-30 19:48:58 +00001112
Dan Gohmana2489d12010-07-20 23:55:01 +00001113 if (!Machine) {
1114 if (N->isFunctionLocal())
1115 Machine = new SlotTracker(N->getFunction());
1116 else
1117 Machine = new SlotTracker(Context);
1118 }
Dan Gohman0a54da22010-09-09 20:53:58 +00001119 int Slot = Machine->getMetadataSlot(N);
1120 if (Slot == -1)
1121 Out << "<badref>";
1122 else
1123 Out << '!' << Slot;
Devang Patele059ba6e2009-07-23 01:07:34 +00001124 return;
1125 }
1126
Devang Patel7428d8a2009-07-22 17:43:22 +00001127 if (const MDString *MDS = dyn_cast<MDString>(V)) {
Devang Patel7428d8a2009-07-22 17:43:22 +00001128 Out << "!\"";
Daniel Dunbare03eecb2009-07-25 23:55:21 +00001129 PrintEscapedString(MDS->getString(), Out);
Devang Patel7428d8a2009-07-22 17:43:22 +00001130 Out << '"';
1131 return;
1132 }
1133
Evan Cheng00e87d12009-11-16 07:10:36 +00001134 if (V->getValueID() == Value::PseudoSourceValueVal ||
1135 V->getValueID() == Value::FixedStackPseudoSourceValueVal) {
Dan Gohmanc0353bf2009-09-23 01:33:16 +00001136 V->print(Out);
1137 return;
1138 }
1139
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001140 char Prefix = '%';
1141 int Slot;
Chris Lattner5b82a0a2011-08-03 06:15:41 +00001142 // If we have a SlotTracker, use it.
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001143 if (Machine) {
1144 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1145 Slot = Machine->getGlobalSlot(GV);
1146 Prefix = '@';
1147 } else {
1148 Slot = Machine->getLocalSlot(V);
Andrew Trickec4b6e72011-09-30 19:48:58 +00001149
Chris Lattner5b82a0a2011-08-03 06:15:41 +00001150 // If the local value didn't succeed, then we may be referring to a value
1151 // from a different function. Translate it, as this can happen when using
1152 // address of blocks.
1153 if (Slot == -1)
1154 if ((Machine = createSlotTracker(V))) {
1155 Slot = Machine->getLocalSlot(V);
1156 delete Machine;
1157 }
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001158 }
Chris Lattner5b82a0a2011-08-03 06:15:41 +00001159 } else if ((Machine = createSlotTracker(V))) {
1160 // Otherwise, create one to get the # and then destroy it.
1161 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1162 Slot = Machine->getGlobalSlot(GV);
1163 Prefix = '@';
Chris Lattnera2d810d2006-01-25 22:26:05 +00001164 } else {
Chris Lattner5b82a0a2011-08-03 06:15:41 +00001165 Slot = Machine->getLocalSlot(V);
Chris Lattnerd84bb632002-04-16 21:36:08 +00001166 }
Chris Lattner5b82a0a2011-08-03 06:15:41 +00001167 delete Machine;
1168 Machine = 0;
1169 } else {
1170 Slot = -1;
Chris Lattnerd84bb632002-04-16 21:36:08 +00001171 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001172
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001173 if (Slot != -1)
1174 Out << Prefix << Slot;
1175 else
1176 Out << "<badref>";
Chris Lattnerd84bb632002-04-16 21:36:08 +00001177}
1178
Daniel Maleaded9f932013-05-08 20:38:31 +00001179void WriteAsOperand(raw_ostream &Out, const Value *V,
1180 bool PrintType, const Module *Context) {
Dan Gohman8061d9e2009-08-13 15:27:57 +00001181
1182 // Fast path: Don't construct and populate a TypePrinting object if we
1183 // won't be needing any types printed.
Dan Gohman08097122009-08-13 23:07:11 +00001184 if (!PrintType &&
Dan Gohmana2489d12010-07-20 23:55:01 +00001185 ((!isa<Constant>(V) && !isa<MDNode>(V)) ||
1186 V->hasName() || isa<GlobalValue>(V))) {
1187 WriteAsOperandInternal(Out, V, 0, 0, Context);
Dan Gohman8061d9e2009-08-13 15:27:57 +00001188 return;
1189 }
1190
Chris Lattner5a9f63e2002-07-10 16:48:17 +00001191 if (Context == 0) Context = getModuleFromVal(V);
Chris Lattnerb86620e2001-10-29 16:37:48 +00001192
Chris Lattner92c5c122009-02-28 23:20:19 +00001193 TypePrinting TypePrinter;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001194 if (Context)
1195 TypePrinter.incorporateTypes(*Context);
Dan Gohman81313fd2008-09-14 17:21:12 +00001196 if (PrintType) {
Chris Lattnere101c442009-02-28 21:26:53 +00001197 TypePrinter.print(V->getType(), Out);
Dan Gohman81313fd2008-09-14 17:21:12 +00001198 Out << ' ';
1199 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001200
Dan Gohmana2489d12010-07-20 23:55:01 +00001201 WriteAsOperandInternal(Out, V, &TypePrinter, 0, Context);
Chris Lattner5e5abe32001-07-20 19:15:21 +00001202}
1203
Daniel Maleaded9f932013-05-08 20:38:31 +00001204void AssemblyWriter::init() {
1205 if (TheModule)
1206 TypePrinter.incorporateTypes(*TheModule);
1207}
Chris Lattner2e9fee42001-07-12 23:35:26 +00001208
Andrew Trickec4b6e72011-09-30 19:48:58 +00001209
Daniel Maleaded9f932013-05-08 20:38:31 +00001210AssemblyWriter::AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac,
1211 const Module *M,
1212 AssemblyAnnotationWriter *AAW)
1213 : Out(o), TheModule(M), Machine(Mac), AnnotationWriter(AAW) {
1214 init();
1215}
Chris Lattner2f7c9632001-06-06 20:29:01 +00001216
Daniel Maleaded9f932013-05-08 20:38:31 +00001217AssemblyWriter::AssemblyWriter(formatted_raw_ostream &o, const Module *M,
1218 AssemblyAnnotationWriter *AAW)
1219 : Out(o), TheModule(M), ModuleSlotTracker(createSlotTracker(M)),
1220 Machine(*ModuleSlotTracker), AnnotationWriter(AAW) {
1221 init();
1222}
Andrew Trickec4b6e72011-09-30 19:48:58 +00001223
Daniel Maleaded9f932013-05-08 20:38:31 +00001224AssemblyWriter::~AssemblyWriter() { }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001225
Chris Lattner78e2e8b2006-12-06 06:24:27 +00001226void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType) {
1227 if (Operand == 0) {
Chris Lattner08f7d0c2005-02-24 16:58:29 +00001228 Out << "<null operand!>";
Chris Lattnerb419a1e2009-12-31 02:33:14 +00001229 return;
Chris Lattner08f7d0c2005-02-24 16:58:29 +00001230 }
Chris Lattnerb419a1e2009-12-31 02:33:14 +00001231 if (PrintType) {
1232 TypePrinter.print(Operand->getType(), Out);
1233 Out << ' ';
1234 }
Dan Gohmana2489d12010-07-20 23:55:01 +00001235 WriteAsOperandInternal(Out, Operand, &TypePrinter, &Machine, TheModule);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001236}
1237
Eli Friedmanfee02c62011-07-25 23:16:38 +00001238void AssemblyWriter::writeAtomic(AtomicOrdering Ordering,
1239 SynchronizationScope SynchScope) {
1240 if (Ordering == NotAtomic)
1241 return;
1242
1243 switch (SynchScope) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001244 case SingleThread: Out << " singlethread"; break;
1245 case CrossThread: break;
1246 }
1247
1248 switch (Ordering) {
1249 default: Out << " <bad ordering " << int(Ordering) << ">"; break;
1250 case Unordered: Out << " unordered"; break;
1251 case Monotonic: Out << " monotonic"; break;
1252 case Acquire: Out << " acquire"; break;
1253 case Release: Out << " release"; break;
1254 case AcquireRelease: Out << " acq_rel"; break;
1255 case SequentiallyConsistent: Out << " seq_cst"; break;
1256 }
1257}
1258
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001259void AssemblyWriter::writeParamOperand(const Value *Operand,
Bill Wendling749a43d2012-12-30 13:50:49 +00001260 AttributeSet Attrs, unsigned Idx) {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00001261 if (Operand == 0) {
1262 Out << "<null operand!>";
Chris Lattnerb419a1e2009-12-31 02:33:14 +00001263 return;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00001264 }
Chris Lattnerb419a1e2009-12-31 02:33:14 +00001265
1266 // Print the type
1267 TypePrinter.print(Operand->getType(), Out);
1268 // Print parameter attributes list
Bill Wendling749a43d2012-12-30 13:50:49 +00001269 if (Attrs.hasAttributes(Idx))
1270 Out << ' ' << Attrs.getAsString(Idx);
Chris Lattnerb419a1e2009-12-31 02:33:14 +00001271 Out << ' ';
1272 // Print the operand
Dan Gohmana2489d12010-07-20 23:55:01 +00001273 WriteAsOperandInternal(Out, Operand, &TypePrinter, &Machine, TheModule);
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00001274}
Chris Lattner2f7c9632001-06-06 20:29:01 +00001275
Chris Lattner7bfee412001-10-29 16:05:51 +00001276void AssemblyWriter::printModule(const Module *M) {
Bill Wendling829b4782013-02-11 08:43:33 +00001277 Machine.initialize();
1278
Chris Lattner4d8689e2005-03-02 23:12:40 +00001279 if (!M->getModuleIdentifier().empty() &&
Misha Brukmanb1c93172005-04-21 23:48:37 +00001280 // Don't print the ID if it will start a new line (which would
Chris Lattner4d8689e2005-03-02 23:12:40 +00001281 // require a comment char before it).
1282 M->getModuleIdentifier().find('\n') == std::string::npos)
1283 Out << "; ModuleID = '" << M->getModuleIdentifier() << "'\n";
1284
Owen Andersone2237542006-10-18 02:21:12 +00001285 if (!M->getDataLayout().empty())
Chris Lattner04897162006-10-22 06:06:56 +00001286 Out << "target datalayout = \"" << M->getDataLayout() << "\"\n";
Reid Spencer48f98c82004-07-25 21:44:54 +00001287 if (!M->getTargetTriple().empty())
Reid Spencerffec7df2004-07-25 21:29:43 +00001288 Out << "target triple = \"" << M->getTargetTriple() << "\"\n";
Misha Brukmanb1c93172005-04-21 23:48:37 +00001289
Chris Lattnereef2fe72006-01-24 04:13:11 +00001290 if (!M->getModuleInlineAsm().empty()) {
Chris Lattnerefaf35d2006-01-24 00:45:30 +00001291 // Split the string into lines, to make it easier to read the .ll file.
Chris Lattnereef2fe72006-01-24 04:13:11 +00001292 std::string Asm = M->getModuleInlineAsm();
Chris Lattnerefaf35d2006-01-24 00:45:30 +00001293 size_t CurPos = 0;
1294 size_t NewLine = Asm.find_first_of('\n', CurPos);
Dan Gohman69273e62009-08-12 23:54:22 +00001295 Out << '\n';
Chris Lattnerefaf35d2006-01-24 00:45:30 +00001296 while (NewLine != std::string::npos) {
1297 // We found a newline, print the portion of the asm string from the
1298 // last newline up to this newline.
1299 Out << "module asm \"";
1300 PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.begin()+NewLine),
1301 Out);
1302 Out << "\"\n";
1303 CurPos = NewLine+1;
1304 NewLine = Asm.find_first_of('\n', CurPos);
1305 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +00001306 std::string rest(Asm.begin()+CurPos, Asm.end());
1307 if (!rest.empty()) {
1308 Out << "module asm \"";
1309 PrintEscapedString(rest, Out);
1310 Out << "\"\n";
1311 }
Chris Lattner6ed87bd2006-01-23 23:03:36 +00001312 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001313
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001314 printTypeIdentities();
Misha Brukmanb1c93172005-04-21 23:48:37 +00001315
Dan Gohman69273e62009-08-12 23:54:22 +00001316 // Output all globals.
1317 if (!M->global_empty()) Out << '\n';
Chris Lattner54932b02006-12-06 04:41:52 +00001318 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
Duncan Sands66fc0e62012-09-12 09:55:51 +00001319 I != E; ++I) {
1320 printGlobal(I); Out << '\n';
1321 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001322
Chris Lattnerd2747052007-04-26 02:24:10 +00001323 // Output all aliases.
1324 if (!M->alias_empty()) Out << "\n";
1325 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
1326 I != E; ++I)
1327 printAlias(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +00001328
Chris Lattner2cdd49d2004-09-14 05:06:58 +00001329 // Output all of the functions.
Chris Lattner113f4f42002-06-25 16:13:24 +00001330 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
1331 printFunction(I);
Devang Patel983c6b12009-07-08 21:44:25 +00001332
Bill Wendling829b4782013-02-11 08:43:33 +00001333 // Output all attribute groups.
Bill Wendling04945972013-04-29 23:48:06 +00001334 if (!Machine.as_empty()) {
Bill Wendling829b4782013-02-11 08:43:33 +00001335 Out << '\n';
1336 writeAllAttributeGroups();
1337 }
1338
Devang Patel23e68302009-07-29 22:04:47 +00001339 // Output named metadata.
Dan Gohman69273e62009-08-12 23:54:22 +00001340 if (!M->named_metadata_empty()) Out << '\n';
Andrew Trickec4b6e72011-09-30 19:48:58 +00001341
Devang Patel23e68302009-07-29 22:04:47 +00001342 for (Module::const_named_metadata_iterator I = M->named_metadata_begin(),
Chris Lattnerbddea6a2009-12-31 02:13:35 +00001343 E = M->named_metadata_end(); I != E; ++I)
Chris Lattner2c48c642009-12-31 01:54:05 +00001344 printNamedMDNode(I);
Devang Patel23e68302009-07-29 22:04:47 +00001345
1346 // Output metadata.
Chris Lattnercf4a76e2009-12-31 02:20:11 +00001347 if (!Machine.mdn_empty()) {
Chris Lattnerbddea6a2009-12-31 02:13:35 +00001348 Out << '\n';
1349 writeAllMDNodes();
1350 }
Chris Lattnerfee714f2001-09-07 16:36:04 +00001351}
1352
Chris Lattner2c48c642009-12-31 01:54:05 +00001353void AssemblyWriter::printNamedMDNode(const NamedMDNode *NMD) {
Nick Lewycky5bcbd732011-06-15 06:37:58 +00001354 Out << '!';
1355 StringRef Name = NMD->getName();
1356 if (Name.empty()) {
1357 Out << "<empty name> ";
1358 } else {
Guy Benyei83c74e92013-02-12 21:21:59 +00001359 if (isalpha(static_cast<unsigned char>(Name[0])) ||
1360 Name[0] == '-' || Name[0] == '$' ||
Nick Lewycky5bcbd732011-06-15 06:37:58 +00001361 Name[0] == '.' || Name[0] == '_')
1362 Out << Name[0];
1363 else
1364 Out << '\\' << hexdigit(Name[0] >> 4) << hexdigit(Name[0] & 0x0F);
1365 for (unsigned i = 1, e = Name.size(); i != e; ++i) {
1366 unsigned char C = Name[i];
Guy Benyei83c74e92013-02-12 21:21:59 +00001367 if (isalnum(static_cast<unsigned char>(C)) || C == '-' || C == '$' ||
1368 C == '.' || C == '_')
Nick Lewycky5bcbd732011-06-15 06:37:58 +00001369 Out << C;
1370 else
1371 Out << '\\' << hexdigit(C >> 4) << hexdigit(C & 0x0F);
1372 }
1373 }
1374 Out << " = !{";
Chris Lattner2c48c642009-12-31 01:54:05 +00001375 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1376 if (i) Out << ", ";
Dan Gohman0a54da22010-09-09 20:53:58 +00001377 int Slot = Machine.getMetadataSlot(NMD->getOperand(i));
1378 if (Slot == -1)
1379 Out << "<badref>";
1380 else
1381 Out << '!' << Slot;
Chris Lattner2c48c642009-12-31 01:54:05 +00001382 }
1383 Out << "}\n";
1384}
1385
1386
Dan Gohmane2745262009-08-12 17:23:50 +00001387static void PrintLinkage(GlobalValue::LinkageTypes LT,
1388 formatted_raw_ostream &Out) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001389 switch (LT) {
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001390 case GlobalValue::ExternalLinkage: break;
1391 case GlobalValue::PrivateLinkage: Out << "private "; break;
1392 case GlobalValue::LinkerPrivateLinkage: Out << "linker_private "; break;
Bill Wendling03bcd6e2010-07-01 21:55:59 +00001393 case GlobalValue::LinkerPrivateWeakLinkage:
1394 Out << "linker_private_weak ";
1395 break;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001396 case GlobalValue::InternalLinkage: Out << "internal "; break;
1397 case GlobalValue::LinkOnceAnyLinkage: Out << "linkonce "; break;
1398 case GlobalValue::LinkOnceODRLinkage: Out << "linkonce_odr "; break;
1399 case GlobalValue::WeakAnyLinkage: Out << "weak "; break;
1400 case GlobalValue::WeakODRLinkage: Out << "weak_odr "; break;
1401 case GlobalValue::CommonLinkage: Out << "common "; break;
1402 case GlobalValue::AppendingLinkage: Out << "appending "; break;
1403 case GlobalValue::DLLImportLinkage: Out << "dllimport "; break;
1404 case GlobalValue::DLLExportLinkage: Out << "dllexport "; break;
1405 case GlobalValue::ExternalWeakLinkage: Out << "extern_weak "; break;
Chris Lattner184f1be2009-04-13 05:44:34 +00001406 case GlobalValue::AvailableExternallyLinkage:
1407 Out << "available_externally ";
1408 break;
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001409 }
1410}
Duncan Sands12da8ce2009-03-07 15:45:40 +00001411
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001412
1413static void PrintVisibility(GlobalValue::VisibilityTypes Vis,
Dan Gohmane2745262009-08-12 17:23:50 +00001414 formatted_raw_ostream &Out) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001415 switch (Vis) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001416 case GlobalValue::DefaultVisibility: break;
1417 case GlobalValue::HiddenVisibility: Out << "hidden "; break;
1418 case GlobalValue::ProtectedVisibility: Out << "protected "; break;
1419 }
1420}
1421
Hans Wennborgcbe34b42012-06-23 11:37:03 +00001422static void PrintThreadLocalModel(GlobalVariable::ThreadLocalMode TLM,
1423 formatted_raw_ostream &Out) {
1424 switch (TLM) {
1425 case GlobalVariable::NotThreadLocal:
1426 break;
1427 case GlobalVariable::GeneralDynamicTLSModel:
1428 Out << "thread_local ";
1429 break;
1430 case GlobalVariable::LocalDynamicTLSModel:
1431 Out << "thread_local(localdynamic) ";
1432 break;
1433 case GlobalVariable::InitialExecTLSModel:
1434 Out << "thread_local(initialexec) ";
1435 break;
1436 case GlobalVariable::LocalExecTLSModel:
1437 Out << "thread_local(localexec) ";
1438 break;
1439 }
1440}
1441
Chris Lattner7bfee412001-10-29 16:05:51 +00001442void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Dan Gohman5ded1422010-01-29 23:12:36 +00001443 if (GV->isMaterializable())
1444 Out << "; Materializable\n";
1445
Dan Gohmana2489d12010-07-20 23:55:01 +00001446 WriteAsOperandInternal(Out, GV, &TypePrinter, &Machine, GV->getParent());
Dan Gohman466876b2009-08-12 23:32:33 +00001447 Out << " = ";
Chris Lattner37798642001-09-18 04:01:05 +00001448
Chris Lattner1508d3f2008-08-19 05:16:28 +00001449 if (!GV->hasInitializer() && GV->hasExternalLinkage())
1450 Out << "external ";
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001451
Chris Lattner1508d3f2008-08-19 05:16:28 +00001452 PrintLinkage(GV->getLinkage(), Out);
1453 PrintVisibility(GV->getVisibility(), Out);
Hans Wennborgcbe34b42012-06-23 11:37:03 +00001454 PrintThreadLocalModel(GV->getThreadLocalMode(), Out);
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +00001455
Chris Lattnerac161bf2009-01-02 07:01:27 +00001456 if (unsigned AddressSpace = GV->getType()->getAddressSpace())
1457 Out << "addrspace(" << AddressSpace << ") ";
Rafael Espindola45e6c192011-01-08 16:42:36 +00001458 if (GV->hasUnnamedAddr()) Out << "unnamed_addr ";
Michael Gottesman27e7ef32013-02-05 05:57:38 +00001459 if (GV->isExternallyInitialized()) Out << "externally_initialized ";
Misha Brukmana6619a92004-06-21 21:53:56 +00001460 Out << (GV->isConstant() ? "constant " : "global ");
Chris Lattnere101c442009-02-28 21:26:53 +00001461 TypePrinter.print(GV->getType()->getElementType(), Out);
Chris Lattner37798642001-09-18 04:01:05 +00001462
Dan Gohman81313fd2008-09-14 17:21:12 +00001463 if (GV->hasInitializer()) {
1464 Out << ' ';
Devang Patel983c6b12009-07-08 21:44:25 +00001465 writeOperand(GV->getInitializer(), false);
Dan Gohman81313fd2008-09-14 17:21:12 +00001466 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001467
Chris Lattner9380b812010-07-07 23:16:37 +00001468 if (GV->hasSection()) {
1469 Out << ", section \"";
1470 PrintEscapedString(GV->getSection(), Out);
1471 Out << '"';
1472 }
Chris Lattner4b96c542005-11-12 00:10:19 +00001473 if (GV->getAlignment())
Chris Lattnerf8a974d2005-11-06 06:48:53 +00001474 Out << ", align " << GV->getAlignment();
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001475
Chris Lattner113f4f42002-06-25 16:13:24 +00001476 printInfoComment(*GV);
Chris Lattnerda975502001-09-10 07:58:01 +00001477}
1478
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001479void AssemblyWriter::printAlias(const GlobalAlias *GA) {
Dan Gohman5ded1422010-01-29 23:12:36 +00001480 if (GA->isMaterializable())
1481 Out << "; Materializable\n";
1482
Dale Johannesen83e468a2008-06-03 18:14:29 +00001483 // Don't crash when dumping partially built GA
1484 if (!GA->hasName())
1485 Out << "<<nameless>> = ";
Chris Lattner033935d2008-08-17 04:40:13 +00001486 else {
1487 PrintLLVMName(Out, GA);
1488 Out << " = ";
1489 }
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001490 PrintVisibility(GA->getVisibility(), Out);
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001491
1492 Out << "alias ";
1493
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001494 PrintLinkage(GA->getLinkage(), Out);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001495
Anton Korobeynikov546ea7e2007-04-29 18:02:48 +00001496 const Constant *Aliasee = GA->getAliasee();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001497
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001498 if (Aliasee == 0) {
1499 TypePrinter.print(GA->getType(), Out);
1500 Out << " <<NULL ALIASEE>>";
Jay Foad92c19132011-08-01 12:48:54 +00001501 } else {
Jay Foad26db79d2011-08-01 12:29:14 +00001502 writeOperand(Aliasee, !isa<ConstantExpr>(Aliasee));
Jay Foad92c19132011-08-01 12:48:54 +00001503 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001504
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001505 printInfoComment(*GA);
Chris Lattner1508d3f2008-08-19 05:16:28 +00001506 Out << '\n';
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001507}
1508
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001509void AssemblyWriter::printTypeIdentities() {
1510 if (TypePrinter.NumberedTypes.empty() &&
1511 TypePrinter.NamedTypes.empty())
1512 return;
Andrew Trickec4b6e72011-09-30 19:48:58 +00001513
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001514 Out << '\n';
Andrew Trickec4b6e72011-09-30 19:48:58 +00001515
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001516 // We know all the numbers that each type is used and we know that it is a
1517 // dense assignment. Convert the map to an index table.
1518 std::vector<StructType*> NumberedTypes(TypePrinter.NumberedTypes.size());
Andrew Trickec4b6e72011-09-30 19:48:58 +00001519 for (DenseMap<StructType*, unsigned>::iterator I =
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001520 TypePrinter.NumberedTypes.begin(), E = TypePrinter.NumberedTypes.end();
1521 I != E; ++I) {
1522 assert(I->second < NumberedTypes.size() && "Didn't get a dense numbering?");
1523 NumberedTypes[I->second] = I->first;
1524 }
Andrew Trickec4b6e72011-09-30 19:48:58 +00001525
Chris Lattner3243ea12009-03-01 00:03:38 +00001526 // Emit all numbered types.
1527 for (unsigned i = 0, e = NumberedTypes.size(); i != e; ++i) {
Dan Gohman466876b2009-08-12 23:32:33 +00001528 Out << '%' << i << " = type ";
Andrew Trickec4b6e72011-09-30 19:48:58 +00001529
Chris Lattner3243ea12009-03-01 00:03:38 +00001530 // Make sure we print out at least one level of the type structure, so
1531 // that we do not get %2 = type %2
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001532 TypePrinter.printStructBody(NumberedTypes[i], Out);
Dan Gohman69273e62009-08-12 23:54:22 +00001533 Out << '\n';
Chris Lattner3243ea12009-03-01 00:03:38 +00001534 }
Andrew Trickec4b6e72011-09-30 19:48:58 +00001535
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001536 for (unsigned i = 0, e = TypePrinter.NamedTypes.size(); i != e; ++i) {
1537 PrintLLVMName(Out, TypePrinter.NamedTypes[i]->getName(), LocalPrefix);
Chris Lattner1508d3f2008-08-19 05:16:28 +00001538 Out << " = type ";
Reid Spencere7e96712004-05-25 08:53:40 +00001539
1540 // Make sure we print out at least one level of the type structure, so
1541 // that we do not get %FILE = type %FILE
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001542 TypePrinter.printStructBody(TypePrinter.NamedTypes[i], Out);
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001543 Out << '\n';
Reid Spencere7e96712004-05-25 08:53:40 +00001544 }
Reid Spencer32af9e82007-01-06 07:24:44 +00001545}
1546
Misha Brukmanc566ca362004-03-02 00:22:19 +00001547/// printFunction - Print all aspects of a function.
1548///
Chris Lattner113f4f42002-06-25 16:13:24 +00001549void AssemblyWriter::printFunction(const Function *F) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001550 // Print out the return type and name.
1551 Out << '\n';
Chris Lattner379a8d22003-04-16 20:28:45 +00001552
Misha Brukmana6619a92004-06-21 21:53:56 +00001553 if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001554
Dan Gohman5ded1422010-01-29 23:12:36 +00001555 if (F->isMaterializable())
1556 Out << "; Materializable\n";
1557
Bill Wendling847a5c32013-04-16 20:55:47 +00001558 const AttributeSet &Attrs = F->getAttributes();
Bill Wendling04945972013-04-29 23:48:06 +00001559 if (Attrs.hasAttributes(AttributeSet::FunctionIndex)) {
Bill Wendling847a5c32013-04-16 20:55:47 +00001560 AttributeSet AS = Attrs.getFnAttributes();
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +00001561 std::string AttrStr;
1562
1563 unsigned Idx = 0;
1564 for (unsigned E = AS.getNumSlots(); Idx != E; ++Idx)
1565 if (AS.getSlotIndex(Idx) == AttributeSet::FunctionIndex)
1566 break;
1567
1568 for (AttributeSet::iterator I = AS.begin(Idx), E = AS.end(Idx);
1569 I != E; ++I) {
1570 Attribute Attr = *I;
1571 if (!Attr.isStringAttribute()) {
1572 if (!AttrStr.empty()) AttrStr += ' ';
1573 AttrStr += Attr.getAsString();
1574 }
1575 }
1576
Bill Wendling847a5c32013-04-16 20:55:47 +00001577 if (!AttrStr.empty())
1578 Out << "; Function Attrs: " << AttrStr << '\n';
1579 }
1580
Reid Spencer5301e7c2007-01-30 20:08:39 +00001581 if (F->isDeclaration())
Chris Lattner10f03a62007-08-19 22:15:26 +00001582 Out << "declare ";
1583 else
Reid Spencer7ce2d2a2006-12-29 20:29:48 +00001584 Out << "define ";
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001585
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001586 PrintLinkage(F->getLinkage(), Out);
1587 PrintVisibility(F->getVisibility(), Out);
Chris Lattner379a8d22003-04-16 20:28:45 +00001588
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001589 // Print the calling convention.
Micah Villmow857186d2012-09-13 15:11:12 +00001590 if (F->getCallingConv() != CallingConv::C) {
1591 PrintCallingConv(F->getCallingConv(), Out);
1592 Out << " ";
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001593 }
1594
Chris Lattner229907c2011-07-18 04:54:35 +00001595 FunctionType *FT = F->getFunctionType();
Bill Wendling658d24d2013-01-18 21:53:16 +00001596 if (Attrs.hasAttributes(AttributeSet::ReturnIndex))
1597 Out << Attrs.getAsString(AttributeSet::ReturnIndex) << ' ';
Chris Lattnere101c442009-02-28 21:26:53 +00001598 TypePrinter.print(F->getReturnType(), Out);
Chris Lattner585297e82008-08-19 05:26:17 +00001599 Out << ' ';
Dan Gohmana2489d12010-07-20 23:55:01 +00001600 WriteAsOperandInternal(Out, F, &TypePrinter, &Machine, F->getParent());
Misha Brukmana6619a92004-06-21 21:53:56 +00001601 Out << '(';
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001602 Machine.incorporateFunction(F);
Chris Lattnerfee714f2001-09-07 16:36:04 +00001603
Chris Lattner7bfee412001-10-29 16:05:51 +00001604 // Loop over the arguments, printing them...
Chris Lattnerfee714f2001-09-07 16:36:04 +00001605
Reid Spencer8c4914c2006-12-31 05:24:50 +00001606 unsigned Idx = 1;
Chris Lattner82738fe2007-04-18 00:57:22 +00001607 if (!F->isDeclaration()) {
1608 // If this isn't a declaration, print the argument names as well.
1609 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
1610 I != E; ++I) {
1611 // Insert commas as we go... the first arg doesn't get a comma
1612 if (I != F->arg_begin()) Out << ", ";
Bill Wendling749a43d2012-12-30 13:50:49 +00001613 printArgument(I, Attrs, Idx);
Chris Lattner82738fe2007-04-18 00:57:22 +00001614 Idx++;
1615 }
1616 } else {
1617 // Otherwise, print the types from the function type.
1618 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
1619 // Insert commas as we go... the first arg doesn't get a comma
1620 if (i) Out << ", ";
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001621
Chris Lattner82738fe2007-04-18 00:57:22 +00001622 // Output type...
Chris Lattnere101c442009-02-28 21:26:53 +00001623 TypePrinter.print(FT->getParamType(i), Out);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001624
Bill Wendling749a43d2012-12-30 13:50:49 +00001625 if (Attrs.hasAttributes(i+1))
1626 Out << ' ' << Attrs.getAsString(i+1);
Chris Lattner82738fe2007-04-18 00:57:22 +00001627 }
Reid Spencer8c4914c2006-12-31 05:24:50 +00001628 }
Chris Lattnerfee714f2001-09-07 16:36:04 +00001629
1630 // Finish printing arguments...
Chris Lattner113f4f42002-06-25 16:13:24 +00001631 if (FT->isVarArg()) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001632 if (FT->getNumParams()) Out << ", ";
1633 Out << "..."; // Output varargs portion of signature!
Chris Lattnerfee714f2001-09-07 16:36:04 +00001634 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001635 Out << ')';
Rafael Espindola563eb4b2011-01-25 19:09:56 +00001636 if (F->hasUnnamedAddr())
1637 Out << " unnamed_addr";
Bill Wendling04945972013-04-29 23:48:06 +00001638 if (Attrs.hasAttributes(AttributeSet::FunctionIndex))
1639 Out << " #" << Machine.getAttributeGroupSlot(Attrs.getFnAttributes());
Chris Lattner9380b812010-07-07 23:16:37 +00001640 if (F->hasSection()) {
1641 Out << " section \"";
1642 PrintEscapedString(F->getSection(), Out);
1643 Out << '"';
1644 }
Chris Lattnerf8a974d2005-11-06 06:48:53 +00001645 if (F->getAlignment())
1646 Out << " align " << F->getAlignment();
Gordon Henriksend930f912008-08-17 18:44:35 +00001647 if (F->hasGC())
1648 Out << " gc \"" << F->getGC() << '"';
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00001649 if (F->hasPrefixData()) {
1650 Out << " prefix ";
1651 writeOperand(F->getPrefixData(), true);
1652 }
Reid Spencer5301e7c2007-01-30 20:08:39 +00001653 if (F->isDeclaration()) {
Chris Lattnerfb483622010-09-02 22:52:10 +00001654 Out << '\n';
Chris Lattnerb2f02e52002-05-06 03:00:40 +00001655 } else {
Chris Lattnerfb483622010-09-02 22:52:10 +00001656 Out << " {";
1657 // Output all of the function's basic blocks.
Chris Lattner113f4f42002-06-25 16:13:24 +00001658 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
1659 printBasicBlock(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +00001660
Misha Brukmana6619a92004-06-21 21:53:56 +00001661 Out << "}\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +00001662 }
1663
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001664 Machine.purgeFunction();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001665}
1666
Misha Brukmanc566ca362004-03-02 00:22:19 +00001667/// printArgument - This member is called for every argument that is passed into
1668/// the function. Simply print it out
1669///
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001670void AssemblyWriter::printArgument(const Argument *Arg,
Bill Wendling749a43d2012-12-30 13:50:49 +00001671 AttributeSet Attrs, unsigned Idx) {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001672 // Output type...
Chris Lattnere101c442009-02-28 21:26:53 +00001673 TypePrinter.print(Arg->getType(), Out);
Misha Brukmanb1c93172005-04-21 23:48:37 +00001674
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00001675 // Output parameter attributes list
Bill Wendling749a43d2012-12-30 13:50:49 +00001676 if (Attrs.hasAttributes(Idx))
1677 Out << ' ' << Attrs.getAsString(Idx);
Reid Spencer8c4914c2006-12-31 05:24:50 +00001678
Chris Lattner2f7c9632001-06-06 20:29:01 +00001679 // Output name, if available...
Chris Lattner033935d2008-08-17 04:40:13 +00001680 if (Arg->hasName()) {
1681 Out << ' ';
1682 PrintLLVMName(Out, Arg);
1683 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001684}
1685
Misha Brukmanc566ca362004-03-02 00:22:19 +00001686/// printBasicBlock - This member is called for each basic block in a method.
1687///
Chris Lattner7bfee412001-10-29 16:05:51 +00001688void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Nick Lewycky4d43d3c2008-04-25 16:53:59 +00001689 if (BB->hasName()) { // Print out the label if it exists...
Chris Lattner033935d2008-08-17 04:40:13 +00001690 Out << "\n";
Daniel Dunbare03eecb2009-07-25 23:55:21 +00001691 PrintLLVMName(Out, BB->getName(), LabelPrefix);
Chris Lattner033935d2008-08-17 04:40:13 +00001692 Out << ':';
Nick Lewycky4d43d3c2008-04-25 16:53:59 +00001693 } else if (!BB->use_empty()) { // Don't print block # of no uses...
Bill Wendling35a9c3c2011-04-10 23:18:04 +00001694 Out << "\n; <label>:";
Chris Lattner5e043322007-01-11 03:54:27 +00001695 int Slot = Machine.getLocalSlot(BB);
Chris Lattner757ee0b2004-06-09 19:41:19 +00001696 if (Slot != -1)
Misha Brukmana6619a92004-06-21 21:53:56 +00001697 Out << Slot;
Chris Lattner757ee0b2004-06-09 19:41:19 +00001698 else
Misha Brukmana6619a92004-06-21 21:53:56 +00001699 Out << "<badref>";
Chris Lattner58185f22002-10-02 19:38:55 +00001700 }
Chris Lattner2447ef52003-11-20 00:09:43 +00001701
Dan Gohmane2745262009-08-12 17:23:50 +00001702 if (BB->getParent() == 0) {
Chris Lattneree97b8b2009-08-17 15:48:08 +00001703 Out.PadToColumn(50);
Dan Gohmane2745262009-08-12 17:23:50 +00001704 Out << "; Error: Block without parent!";
1705 } else if (BB != &BB->getParent()->getEntryBlock()) { // Not the entry block?
Chris Lattnerfb483622010-09-02 22:52:10 +00001706 // Output predecessors for the block.
Chris Lattneree97b8b2009-08-17 15:48:08 +00001707 Out.PadToColumn(50);
Dan Gohmane2745262009-08-12 17:23:50 +00001708 Out << ";";
Gabor Greif6c6b2fd2010-03-25 23:25:28 +00001709 const_pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001710
Chris Lattnerff834c02008-04-22 02:45:44 +00001711 if (PI == PE) {
1712 Out << " No predecessors!";
1713 } else {
Dan Gohman81313fd2008-09-14 17:21:12 +00001714 Out << " preds = ";
Chris Lattnerff834c02008-04-22 02:45:44 +00001715 writeOperand(*PI, false);
1716 for (++PI; PI != PE; ++PI) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001717 Out << ", ";
Chris Lattner78e2e8b2006-12-06 06:24:27 +00001718 writeOperand(*PI, false);
Chris Lattner00211f12003-11-16 22:59:57 +00001719 }
Chris Lattner58185f22002-10-02 19:38:55 +00001720 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001721 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001722
Chris Lattnerff834c02008-04-22 02:45:44 +00001723 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001724
Misha Brukmana6619a92004-06-21 21:53:56 +00001725 if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001726
Chris Lattnerfee714f2001-09-07 16:36:04 +00001727 // Output all of the instructions in the basic block...
Dan Gohmana4f709e2009-07-13 18:27:59 +00001728 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
Daniel Maleaded9f932013-05-08 20:38:31 +00001729 printInstructionLine(*I);
Dan Gohmana4f709e2009-07-13 18:27:59 +00001730 }
Chris Lattner96cdd272004-03-08 18:51:45 +00001731
Misha Brukmana6619a92004-06-21 21:53:56 +00001732 if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, Out);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001733}
1734
Daniel Maleaded9f932013-05-08 20:38:31 +00001735/// printInstructionLine - Print an instruction and a newline character.
1736void AssemblyWriter::printInstructionLine(const Instruction &I) {
1737 printInstruction(I);
1738 Out << '\n';
1739}
1740
Misha Brukmanc566ca362004-03-02 00:22:19 +00001741/// printInfoComment - Print a little comment after the instruction indicating
1742/// which slot it occupies.
1743///
Chris Lattner113f4f42002-06-25 16:13:24 +00001744void AssemblyWriter::printInfoComment(const Value &V) {
Bill Wendling847a5c32013-04-16 20:55:47 +00001745 if (AnnotationWriter)
Dan Gohmane34890f2010-02-10 20:41:46 +00001746 AnnotationWriter->printInfoComment(V, Out);
Chris Lattner862e3382001-10-13 06:42:36 +00001747}
1748
Reid Spencere7141c82006-08-28 01:02:49 +00001749// This member is called for each Instruction in a function..
Chris Lattner113f4f42002-06-25 16:13:24 +00001750void AssemblyWriter::printInstruction(const Instruction &I) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001751 if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001752
Dan Gohman466876b2009-08-12 23:32:33 +00001753 // Print out indentation for an instruction.
Dan Gohmanb4583b12009-08-13 01:41:52 +00001754 Out << " ";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001755
1756 // Print out name if it exists...
Chris Lattner033935d2008-08-17 04:40:13 +00001757 if (I.hasName()) {
1758 PrintLLVMName(Out, &I);
1759 Out << " = ";
Chris Lattner031560c2009-12-29 07:25:48 +00001760 } else if (!I.getType()->isVoidTy()) {
Chris Lattnerb25e5ea2008-08-29 17:19:30 +00001761 // Print out the def slot taken.
1762 int SlotNum = Machine.getLocalSlot(&I);
1763 if (SlotNum == -1)
1764 Out << "<badref> = ";
1765 else
1766 Out << '%' << SlotNum << " = ";
Chris Lattner033935d2008-08-17 04:40:13 +00001767 }
Andrew Trickec4b6e72011-09-30 19:48:58 +00001768
Eli Friedman59b66882011-08-09 23:02:53 +00001769 if (isa<CallInst>(I) && cast<CallInst>(I).isTailCall())
Chris Lattner06038452005-05-06 05:51:46 +00001770 Out << "tail ";
Chris Lattner504f9242003-09-08 17:45:59 +00001771
Chris Lattner2f7c9632001-06-06 20:29:01 +00001772 // Print out the opcode...
Misha Brukmana6619a92004-06-21 21:53:56 +00001773 Out << I.getOpcodeName();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001774
Eli Friedman02e737b2011-08-12 22:50:01 +00001775 // If this is an atomic load or store, print out the atomic marker.
1776 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isAtomic()) ||
1777 (isa<StoreInst>(I) && cast<StoreInst>(I).isAtomic()))
1778 Out << " atomic";
1779
1780 // If this is a volatile operation, print out the volatile marker.
1781 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) ||
1782 (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile()) ||
1783 (isa<AtomicCmpXchgInst>(I) && cast<AtomicCmpXchgInst>(I).isVolatile()) ||
1784 (isa<AtomicRMWInst>(I) && cast<AtomicRMWInst>(I).isVolatile()))
1785 Out << " volatile";
1786
Dan Gohman9c7f8082009-07-27 16:11:46 +00001787 // Print out optimization information.
1788 WriteOptimizationInfo(Out, &I);
1789
Reid Spencer45e52392006-12-03 06:27:29 +00001790 // Print out the compare instruction predicates
Nate Begemand2195702008-05-12 19:01:56 +00001791 if (const CmpInst *CI = dyn_cast<CmpInst>(&I))
Chris Lattner82ff9232008-08-23 22:52:27 +00001792 Out << ' ' << getPredicateText(CI->getPredicate());
Reid Spencer45e52392006-12-03 06:27:29 +00001793
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001794 // Print out the atomicrmw operation
1795 if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(&I))
1796 writeAtomicRMWOperation(Out, RMWI->getOperation());
1797
Chris Lattner2f7c9632001-06-06 20:29:01 +00001798 // Print out the type of the operands...
Chris Lattner113f4f42002-06-25 16:13:24 +00001799 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001800
1801 // Special case conditional branches to swizzle the condition out to the front
Gabor Greifcab008f2009-02-09 15:45:06 +00001802 if (isa<BranchInst>(I) && cast<BranchInst>(I).isConditional()) {
David Blaikieb78e9e52013-02-11 01:16:51 +00001803 const BranchInst &BI(cast<BranchInst>(I));
Dan Gohman81313fd2008-09-14 17:21:12 +00001804 Out << ' ';
Gabor Greifcab008f2009-02-09 15:45:06 +00001805 writeOperand(BI.getCondition(), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00001806 Out << ", ";
Gabor Greifcab008f2009-02-09 15:45:06 +00001807 writeOperand(BI.getSuccessor(0), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00001808 Out << ", ";
Gabor Greifcab008f2009-02-09 15:45:06 +00001809 writeOperand(BI.getSuccessor(1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001810
Chris Lattner8d48df22002-04-13 18:34:38 +00001811 } else if (isa<SwitchInst>(I)) {
David Blaikieb78e9e52013-02-11 01:16:51 +00001812 const SwitchInst& SI(cast<SwitchInst>(I));
Chris Lattner3ed871f2009-10-27 19:13:16 +00001813 // Special case switch instruction to get formatting nice and correct.
Dan Gohman81313fd2008-09-14 17:21:12 +00001814 Out << ' ';
Eli Friedman95031ed2011-09-29 20:21:17 +00001815 writeOperand(SI.getCondition(), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00001816 Out << ", ";
Eli Friedman95031ed2011-09-29 20:21:17 +00001817 writeOperand(SI.getDefaultDest(), true);
Chris Lattner82ff9232008-08-23 22:52:27 +00001818 Out << " [";
David Blaikieb78e9e52013-02-11 01:16:51 +00001819 for (SwitchInst::ConstCaseIt i = SI.case_begin(), e = SI.case_end();
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00001820 i != e; ++i) {
Dan Gohmanb4583b12009-08-13 01:41:52 +00001821 Out << "\n ";
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00001822 writeOperand(i.getCaseValue(), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00001823 Out << ", ";
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00001824 writeOperand(i.getCaseSuccessor(), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001825 }
Dan Gohmanb4583b12009-08-13 01:41:52 +00001826 Out << "\n ]";
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00001827 } else if (isa<IndirectBrInst>(I)) {
1828 // Special case indirectbr instruction to get formatting nice and correct.
Chris Lattner3ed871f2009-10-27 19:13:16 +00001829 Out << ' ';
1830 writeOperand(Operand, true);
Dan Gohman43c57402009-10-30 02:01:10 +00001831 Out << ", [";
Andrew Trickec4b6e72011-09-30 19:48:58 +00001832
Chris Lattner3ed871f2009-10-27 19:13:16 +00001833 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) {
1834 if (i != 1)
1835 Out << ", ";
1836 writeOperand(I.getOperand(i), true);
1837 }
1838 Out << ']';
Jay Foad372ad642011-06-20 14:18:48 +00001839 } else if (const PHINode *PN = dyn_cast<PHINode>(&I)) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001840 Out << ' ';
Chris Lattnere101c442009-02-28 21:26:53 +00001841 TypePrinter.print(I.getType(), Out);
Misha Brukmana6619a92004-06-21 21:53:56 +00001842 Out << ' ';
Chris Lattner2f7c9632001-06-06 20:29:01 +00001843
Jay Foad372ad642011-06-20 14:18:48 +00001844 for (unsigned op = 0, Eop = PN->getNumIncomingValues(); op < Eop; ++op) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001845 if (op) Out << ", ";
Dan Gohman81313fd2008-09-14 17:21:12 +00001846 Out << "[ ";
Jay Foad372ad642011-06-20 14:18:48 +00001847 writeOperand(PN->getIncomingValue(op), false); Out << ", ";
1848 writeOperand(PN->getIncomingBlock(op), false); Out << " ]";
Chris Lattner931ef3b2001-06-11 15:04:20 +00001849 }
Dan Gohmana76f0f72008-05-31 19:12:39 +00001850 } else if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(&I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001851 Out << ' ';
Dan Gohmana76f0f72008-05-31 19:12:39 +00001852 writeOperand(I.getOperand(0), true);
1853 for (const unsigned *i = EVI->idx_begin(), *e = EVI->idx_end(); i != e; ++i)
1854 Out << ", " << *i;
1855 } else if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(&I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001856 Out << ' ';
1857 writeOperand(I.getOperand(0), true); Out << ", ";
Dan Gohmana76f0f72008-05-31 19:12:39 +00001858 writeOperand(I.getOperand(1), true);
1859 for (const unsigned *i = IVI->idx_begin(), *e = IVI->idx_end(); i != e; ++i)
1860 Out << ", " << *i;
Bill Wendlingfae14752011-08-12 20:24:12 +00001861 } else if (const LandingPadInst *LPI = dyn_cast<LandingPadInst>(&I)) {
1862 Out << ' ';
1863 TypePrinter.print(I.getType(), Out);
1864 Out << " personality ";
1865 writeOperand(I.getOperand(0), true); Out << '\n';
1866
1867 if (LPI->isCleanup())
1868 Out << " cleanup";
1869
1870 for (unsigned i = 0, e = LPI->getNumClauses(); i != e; ++i) {
1871 if (i != 0 || LPI->isCleanup()) Out << "\n";
1872 if (LPI->isCatch(i))
1873 Out << " catch ";
1874 else
1875 Out << " filter ";
1876
1877 writeOperand(LPI->getClause(i), true);
1878 }
Devang Patel59643e52008-02-23 00:35:18 +00001879 } else if (isa<ReturnInst>(I) && !Operand) {
1880 Out << " void";
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001881 } else if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
1882 // Print the calling convention being used.
Micah Villmow857186d2012-09-13 15:11:12 +00001883 if (CI->getCallingConv() != CallingConv::C) {
1884 Out << " ";
1885 PrintCallingConv(CI->getCallingConv(), Out);
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001886 }
1887
Gabor Greifc9a92512010-06-23 13:09:06 +00001888 Operand = CI->getCalledValue();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001889 PointerType *PTy = cast<PointerType>(Operand->getType());
1890 FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1891 Type *RetTy = FTy->getReturnType();
Bill Wendlinge94d8432012-12-07 23:16:57 +00001892 const AttributeSet &PAL = CI->getAttributes();
Chris Lattner2f2d9472001-11-06 21:28:12 +00001893
Bill Wendling658d24d2013-01-18 21:53:16 +00001894 if (PAL.hasAttributes(AttributeSet::ReturnIndex))
1895 Out << ' ' << PAL.getAsString(AttributeSet::ReturnIndex);
Devang Patel221fe422008-09-29 20:49:50 +00001896
Chris Lattner463d6a52003-08-05 15:34:45 +00001897 // If possible, print out the short form of the call instruction. We can
Chris Lattner6915f8f2002-04-07 22:49:37 +00001898 // only do this if the first argument is a pointer to a nonvararg function,
Chris Lattner463d6a52003-08-05 15:34:45 +00001899 // and if the return type is not a pointer to a function.
Chris Lattner2f2d9472001-11-06 21:28:12 +00001900 //
Dan Gohman81313fd2008-09-14 17:21:12 +00001901 Out << ' ';
Chris Lattner463d6a52003-08-05 15:34:45 +00001902 if (!FTy->isVarArg() &&
Duncan Sands19d0b472010-02-16 11:11:14 +00001903 (!RetTy->isPointerTy() ||
1904 !cast<PointerType>(RetTy)->getElementType()->isFunctionTy())) {
Chris Lattnere101c442009-02-28 21:26:53 +00001905 TypePrinter.print(RetTy, Out);
Dan Gohman81313fd2008-09-14 17:21:12 +00001906 Out << ' ';
Chris Lattner2f2d9472001-11-06 21:28:12 +00001907 writeOperand(Operand, false);
1908 } else {
1909 writeOperand(Operand, true);
1910 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001911 Out << '(';
Gabor Greifc9a92512010-06-23 13:09:06 +00001912 for (unsigned op = 0, Eop = CI->getNumArgOperands(); op < Eop; ++op) {
1913 if (op > 0)
Dan Gohman81313fd2008-09-14 17:21:12 +00001914 Out << ", ";
Bill Wendling749a43d2012-12-30 13:50:49 +00001915 writeParamOperand(CI->getArgOperand(op), PAL, op + 1);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001916 }
Dan Gohman81313fd2008-09-14 17:21:12 +00001917 Out << ')';
Bill Wendling698e84f2012-12-30 10:32:01 +00001918 if (PAL.hasAttributes(AttributeSet::FunctionIndex))
Bill Wendlinga0323742013-02-22 09:09:42 +00001919 Out << " #" << Machine.getAttributeGroupSlot(PAL.getFnAttributes());
Chris Lattner113f4f42002-06-25 16:13:24 +00001920 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
Gabor Greifa2fbc0a2010-03-24 13:21:49 +00001921 Operand = II->getCalledValue();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001922 PointerType *PTy = cast<PointerType>(Operand->getType());
1923 FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1924 Type *RetTy = FTy->getReturnType();
Bill Wendlinge94d8432012-12-07 23:16:57 +00001925 const AttributeSet &PAL = II->getAttributes();
Chris Lattner463d6a52003-08-05 15:34:45 +00001926
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001927 // Print the calling convention being used.
Micah Villmow857186d2012-09-13 15:11:12 +00001928 if (II->getCallingConv() != CallingConv::C) {
1929 Out << " ";
1930 PrintCallingConv(II->getCallingConv(), Out);
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001931 }
1932
Bill Wendling658d24d2013-01-18 21:53:16 +00001933 if (PAL.hasAttributes(AttributeSet::ReturnIndex))
1934 Out << ' ' << PAL.getAsString(AttributeSet::ReturnIndex);
Devang Patel221fe422008-09-29 20:49:50 +00001935
Chris Lattner463d6a52003-08-05 15:34:45 +00001936 // If possible, print out the short form of the invoke instruction. We can
1937 // only do this if the first argument is a pointer to a nonvararg function,
1938 // and if the return type is not a pointer to a function.
1939 //
Dan Gohmanc7e00ba2008-10-15 18:02:08 +00001940 Out << ' ';
Chris Lattner463d6a52003-08-05 15:34:45 +00001941 if (!FTy->isVarArg() &&
Duncan Sands19d0b472010-02-16 11:11:14 +00001942 (!RetTy->isPointerTy() ||
1943 !cast<PointerType>(RetTy)->getElementType()->isFunctionTy())) {
Chris Lattnere101c442009-02-28 21:26:53 +00001944 TypePrinter.print(RetTy, Out);
Dan Gohmanc7e00ba2008-10-15 18:02:08 +00001945 Out << ' ';
Chris Lattner463d6a52003-08-05 15:34:45 +00001946 writeOperand(Operand, false);
1947 } else {
1948 writeOperand(Operand, true);
1949 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001950 Out << '(';
Gabor Greifc9a92512010-06-23 13:09:06 +00001951 for (unsigned op = 0, Eop = II->getNumArgOperands(); op < Eop; ++op) {
Gabor Greifa2fbc0a2010-03-24 13:21:49 +00001952 if (op)
Dan Gohman81313fd2008-09-14 17:21:12 +00001953 Out << ", ";
Bill Wendling749a43d2012-12-30 13:50:49 +00001954 writeParamOperand(II->getArgOperand(op), PAL, op + 1);
Chris Lattner862e3382001-10-13 06:42:36 +00001955 }
1956
Dan Gohman81313fd2008-09-14 17:21:12 +00001957 Out << ')';
Bill Wendling698e84f2012-12-30 10:32:01 +00001958 if (PAL.hasAttributes(AttributeSet::FunctionIndex))
Bill Wendlinga0323742013-02-22 09:09:42 +00001959 Out << " #" << Machine.getAttributeGroupSlot(PAL.getFnAttributes());
Devang Patela05633e2008-09-26 22:53:05 +00001960
Dan Gohmanb4583b12009-08-13 01:41:52 +00001961 Out << "\n to ";
Chris Lattner862e3382001-10-13 06:42:36 +00001962 writeOperand(II->getNormalDest(), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00001963 Out << " unwind ";
Chris Lattnerfae8ab32004-02-08 21:44:31 +00001964 writeOperand(II->getUnwindDest(), true);
Chris Lattner862e3382001-10-13 06:42:36 +00001965
Victor Hernandez8acf2952009-10-23 21:09:37 +00001966 } else if (const AllocaInst *AI = dyn_cast<AllocaInst>(&I)) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001967 Out << ' ';
Dan Gohmanb2f426c2013-02-08 22:01:47 +00001968 TypePrinter.print(AI->getAllocatedType(), Out);
Dan Gohmanb53e26c2009-07-31 18:23:24 +00001969 if (!AI->getArraySize() || AI->isArrayAllocation()) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001970 Out << ", ";
Chris Lattner8d48df22002-04-13 18:34:38 +00001971 writeOperand(AI->getArraySize(), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001972 }
Nate Begeman848622f2005-11-05 09:21:28 +00001973 if (AI->getAlignment()) {
Chris Lattner7aeee3a2005-11-05 21:20:34 +00001974 Out << ", align " << AI->getAlignment();
Nate Begeman848622f2005-11-05 09:21:28 +00001975 }
Chris Lattner862e3382001-10-13 06:42:36 +00001976 } else if (isa<CastInst>(I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001977 if (Operand) {
1978 Out << ' ';
1979 writeOperand(Operand, true); // Work with broken code
1980 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001981 Out << " to ";
Chris Lattnere101c442009-02-28 21:26:53 +00001982 TypePrinter.print(I.getType(), Out);
Chris Lattner5b337482003-10-18 05:57:43 +00001983 } else if (isa<VAArgInst>(I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001984 if (Operand) {
1985 Out << ' ';
1986 writeOperand(Operand, true); // Work with broken code
1987 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001988 Out << ", ";
Chris Lattnere101c442009-02-28 21:26:53 +00001989 TypePrinter.print(I.getType(), Out);
1990 } else if (Operand) { // Print the normal way.
Chris Lattner2f7c9632001-06-06 20:29:01 +00001991
Misha Brukmanb1c93172005-04-21 23:48:37 +00001992 // PrintAllTypes - Instructions who have operands of all the same type
Chris Lattner2f7c9632001-06-06 20:29:01 +00001993 // omit the type from all but the first operand. If the instruction has
1994 // different type operands (for example br), then they are all printed.
1995 bool PrintAllTypes = false;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001996 Type *TheType = Operand->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001997
Reid Spencer0cdd04f2007-02-02 13:54:55 +00001998 // Select, Store and ShuffleVector always print all types.
Devang Patelce556d92008-03-04 22:05:14 +00001999 if (isa<SelectInst>(I) || isa<StoreInst>(I) || isa<ShuffleVectorInst>(I)
2000 || isa<ReturnInst>(I)) {
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00002001 PrintAllTypes = true;
2002 } else {
2003 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
2004 Operand = I.getOperand(i);
Nuno Lopes04fe2f02009-01-15 18:40:57 +00002005 // note that Operand shouldn't be null, but the test helps make dump()
2006 // more tolerant of malformed IR
Nuno Lopes0971e772009-01-14 17:51:41 +00002007 if (Operand && Operand->getType() != TheType) {
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00002008 PrintAllTypes = true; // We have differing types! Print them all!
2009 break;
2010 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00002011 }
2012 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00002013
Chris Lattner7bfee412001-10-29 16:05:51 +00002014 if (!PrintAllTypes) {
Misha Brukmana6619a92004-06-21 21:53:56 +00002015 Out << ' ';
Chris Lattnere101c442009-02-28 21:26:53 +00002016 TypePrinter.print(TheType, Out);
Chris Lattner7bfee412001-10-29 16:05:51 +00002017 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00002018
Dan Gohman81313fd2008-09-14 17:21:12 +00002019 Out << ' ';
Chris Lattner113f4f42002-06-25 16:13:24 +00002020 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
Dan Gohman81313fd2008-09-14 17:21:12 +00002021 if (i) Out << ", ";
Chris Lattner113f4f42002-06-25 16:13:24 +00002022 writeOperand(I.getOperand(i), PrintAllTypes);
Chris Lattner2f7c9632001-06-06 20:29:01 +00002023 }
2024 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002025
Eli Friedman59b66882011-08-09 23:02:53 +00002026 // Print atomic ordering/alignment for memory operations
2027 if (const LoadInst *LI = dyn_cast<LoadInst>(&I)) {
2028 if (LI->isAtomic())
2029 writeAtomic(LI->getOrdering(), LI->getSynchScope());
2030 if (LI->getAlignment())
2031 Out << ", align " << LI->getAlignment();
2032 } else if (const StoreInst *SI = dyn_cast<StoreInst>(&I)) {
2033 if (SI->isAtomic())
2034 writeAtomic(SI->getOrdering(), SI->getSynchScope());
2035 if (SI->getAlignment())
2036 Out << ", align " << SI->getAlignment();
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002037 } else if (const AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(&I)) {
2038 writeAtomic(CXI->getOrdering(), CXI->getSynchScope());
2039 } else if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(&I)) {
2040 writeAtomic(RMWI->getOrdering(), RMWI->getSynchScope());
Eli Friedmanfee02c62011-07-25 23:16:38 +00002041 } else if (const FenceInst *FI = dyn_cast<FenceInst>(&I)) {
2042 writeAtomic(FI->getOrdering(), FI->getSynchScope());
Christopher Lamb84485702007-04-22 19:24:39 +00002043 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00002044
Chris Lattnerc9558df2009-12-28 20:10:43 +00002045 // Print Metadata info.
Nick Lewyckyba8ec9a2010-02-25 06:53:04 +00002046 SmallVector<std::pair<unsigned, MDNode*>, 4> InstMD;
2047 I.getAllMetadata(InstMD);
Erick Tryzelaar72a37132010-03-02 05:32:52 +00002048 if (!InstMD.empty()) {
2049 SmallVector<StringRef, 8> MDNames;
2050 I.getType()->getContext().getMDKindNames(MDNames);
2051 for (unsigned i = 0, e = InstMD.size(); i != e; ++i) {
2052 unsigned Kind = InstMD[i].first;
2053 if (Kind < MDNames.size()) {
2054 Out << ", !" << MDNames[Kind];
Daniel Maleaded9f932013-05-08 20:38:31 +00002055 } else {
2056 Out << ", !<unknown kind #" << Kind << ">";
2057 }
Dan Gohmana2489d12010-07-20 23:55:01 +00002058 Out << ' ';
2059 WriteAsOperandInternal(Out, InstMD[i].second, &TypePrinter, &Machine,
2060 TheModule);
Nick Lewyckyba8ec9a2010-02-25 06:53:04 +00002061 }
Devang Patelbcdb0252009-10-07 16:37:55 +00002062 }
Chris Lattner862e3382001-10-13 06:42:36 +00002063 printInfoComment(I);
Chris Lattner2f7c9632001-06-06 20:29:01 +00002064}
2065
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002066static void WriteMDNodeComment(const MDNode *Node,
Duncan Sands41b4a6b2010-07-12 08:16:59 +00002067 formatted_raw_ostream &Out) {
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002068 if (Node->getNumOperands() < 1)
2069 return;
Bill Wendling5cb50c52012-06-28 00:41:44 +00002070
2071 Value *Op = Node->getOperand(0);
2072 if (!Op || !isa<ConstantInt>(Op) || cast<ConstantInt>(Op)->getBitWidth() < 32)
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002073 return;
Andrew Trickec4b6e72011-09-30 19:48:58 +00002074
Bill Wendling5cb50c52012-06-28 00:41:44 +00002075 DIDescriptor Desc(Node);
David Blaikiedc69ebb2013-03-11 23:39:23 +00002076 if (!Desc.Verify())
Bill Wendling5cb50c52012-06-28 00:41:44 +00002077 return;
2078
2079 unsigned Tag = Desc.getTag();
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002080 Out.PadToColumn(50);
Bill Wendlinga0bc1082012-07-03 20:01:02 +00002081 if (dwarf::TagString(Tag)) {
2082 Out << "; ";
2083 Desc.print(Out);
2084 } else if (Tag == dwarf::DW_TAG_user_base) {
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002085 Out << "; [ DW_TAG_user_base ]";
Bill Wendlinga0bc1082012-07-03 20:01:02 +00002086 }
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002087}
2088
Daniel Maleaded9f932013-05-08 20:38:31 +00002089void AssemblyWriter::writeMDNode(unsigned Slot, const MDNode *Node) {
2090 Out << '!' << Slot << " = metadata ";
2091 printMDNodeBody(Node);
2092}
2093
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002094void AssemblyWriter::writeAllMDNodes() {
2095 SmallVector<const MDNode *, 16> Nodes;
Chris Lattnercf4a76e2009-12-31 02:20:11 +00002096 Nodes.resize(Machine.mdn_size());
2097 for (SlotTracker::mdn_iterator I = Machine.mdn_begin(), E = Machine.mdn_end();
2098 I != E; ++I)
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002099 Nodes[I->second] = cast<MDNode>(I->first);
Andrew Trickec4b6e72011-09-30 19:48:58 +00002100
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002101 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Daniel Maleaded9f932013-05-08 20:38:31 +00002102 writeMDNode(i, Nodes[i]);
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002103 }
2104}
2105
2106void AssemblyWriter::printMDNodeBody(const MDNode *Node) {
Dan Gohmana2489d12010-07-20 23:55:01 +00002107 WriteMDNodeBodyInternal(Out, Node, &TypePrinter, &Machine, TheModule);
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002108 WriteMDNodeComment(Node, Out);
2109 Out << "\n";
2110}
Chris Lattner2f7c9632001-06-06 20:29:01 +00002111
Bill Wendling829b4782013-02-11 08:43:33 +00002112void AssemblyWriter::writeAllAttributeGroups() {
2113 std::vector<std::pair<AttributeSet, unsigned> > asVec;
2114 asVec.resize(Machine.as_size());
2115
2116 for (SlotTracker::as_iterator I = Machine.as_begin(), E = Machine.as_end();
2117 I != E; ++I)
2118 asVec[I->second] = *I;
2119
2120 for (std::vector<std::pair<AttributeSet, unsigned> >::iterator
2121 I = asVec.begin(), E = asVec.end(); I != E; ++I)
2122 Out << "attributes #" << I->second << " = { "
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +00002123 << I->first.getAsString(AttributeSet::FunctionIndex, true) << " }\n";
Bill Wendling829b4782013-02-11 08:43:33 +00002124}
2125
Daniel Maleaded9f932013-05-08 20:38:31 +00002126} // namespace llvm
2127
Chris Lattner2f7c9632001-06-06 20:29:01 +00002128//===----------------------------------------------------------------------===//
2129// External Interface declarations
2130//===----------------------------------------------------------------------===//
2131
Dan Gohmane2745262009-08-12 17:23:50 +00002132void Module::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW) const {
Chris Lattnere36fd8a2008-08-19 04:26:57 +00002133 SlotTracker SlotTable(this);
Dan Gohmane2745262009-08-12 17:23:50 +00002134 formatted_raw_ostream OS(ROS);
Chris Lattner0c19df42008-08-23 22:23:09 +00002135 AssemblyWriter W(OS, SlotTable, this, AAW);
Chris Lattnerefb5e392009-12-31 02:23:35 +00002136 W.printModule(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00002137}
2138
Dan Gohman2637cc12010-07-21 23:38:33 +00002139void NamedMDNode::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW) const {
2140 SlotTracker SlotTable(getParent());
2141 formatted_raw_ostream OS(ROS);
2142 AssemblyWriter W(OS, SlotTable, getParent(), AAW);
2143 W.printNamedMDNode(this);
2144}
2145
Chris Lattner2ee62d22009-02-28 21:11:05 +00002146void Type::print(raw_ostream &OS) const {
2147 if (this == 0) {
2148 OS << "<null Type>";
2149 return;
2150 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002151 TypePrinting TP;
2152 TP.print(const_cast<Type*>(this), OS);
Andrew Trickec4b6e72011-09-30 19:48:58 +00002153
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002154 // If the type is a named struct type, print the body as well.
2155 if (StructType *STy = dyn_cast<StructType>(const_cast<Type*>(this)))
Chris Lattner01beceb2011-08-12 18:07:07 +00002156 if (!STy->isLiteral()) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002157 OS << " = type ";
2158 TP.printStructBody(STy, OS);
2159 }
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00002160}
2161
Dan Gohmane2745262009-08-12 17:23:50 +00002162void Value::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW) const {
Chris Lattner0c19df42008-08-23 22:23:09 +00002163 if (this == 0) {
Dan Gohman12dad632009-08-12 20:56:03 +00002164 ROS << "printing a <null> value\n";
Chris Lattner0c19df42008-08-23 22:23:09 +00002165 return;
2166 }
Dan Gohman12dad632009-08-12 20:56:03 +00002167 formatted_raw_ostream OS(ROS);
Chris Lattner0c19df42008-08-23 22:23:09 +00002168 if (const Instruction *I = dyn_cast<Instruction>(this)) {
2169 const Function *F = I->getParent() ? I->getParent()->getParent() : 0;
2170 SlotTracker SlotTable(F);
Chris Lattnerd5bace72009-12-31 08:23:09 +00002171 AssemblyWriter W(OS, SlotTable, getModuleFromVal(I), AAW);
Chris Lattnerefb5e392009-12-31 02:23:35 +00002172 W.printInstruction(*I);
Chris Lattner0c19df42008-08-23 22:23:09 +00002173 } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(this)) {
2174 SlotTracker SlotTable(BB->getParent());
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002175 AssemblyWriter W(OS, SlotTable, getModuleFromVal(BB), AAW);
Chris Lattnerefb5e392009-12-31 02:23:35 +00002176 W.printBasicBlock(BB);
Chris Lattner0c19df42008-08-23 22:23:09 +00002177 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(this)) {
2178 SlotTracker SlotTable(GV->getParent());
Dan Gohman6c7a4852009-04-20 16:10:33 +00002179 AssemblyWriter W(OS, SlotTable, GV->getParent(), AAW);
Chris Lattnerefb5e392009-12-31 02:23:35 +00002180 if (const GlobalVariable *V = dyn_cast<GlobalVariable>(GV))
2181 W.printGlobal(V);
2182 else if (const Function *F = dyn_cast<Function>(GV))
2183 W.printFunction(F);
2184 else
2185 W.printAlias(cast<GlobalAlias>(GV));
Devang Patel5546b982009-07-01 20:59:15 +00002186 } else if (const MDNode *N = dyn_cast<MDNode>(this)) {
Victor Hernandeze5f2af72010-01-20 04:45:57 +00002187 const Function *F = N->getFunction();
Victor Hernandez61e6e822010-01-14 01:47:37 +00002188 SlotTracker SlotTable(F);
Dan Gohman15132172010-07-14 21:12:44 +00002189 AssemblyWriter W(OS, SlotTable, F ? F->getParent() : 0, AAW);
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002190 W.printMDNodeBody(N);
Chris Lattner0c19df42008-08-23 22:23:09 +00002191 } else if (const Constant *C = dyn_cast<Constant>(this)) {
Chris Lattner92c5c122009-02-28 23:20:19 +00002192 TypePrinting TypePrinter;
Chris Lattnere101c442009-02-28 21:26:53 +00002193 TypePrinter.print(C->getType(), OS);
Chris Lattner2ee62d22009-02-28 21:11:05 +00002194 OS << ' ';
Dan Gohmana2489d12010-07-20 23:55:01 +00002195 WriteConstantInternal(OS, C, TypePrinter, 0, 0);
Chris Lattner31fcc282009-12-31 01:41:14 +00002196 } else if (isa<InlineAsm>(this) || isa<MDString>(this) ||
2197 isa<Argument>(this)) {
Chris Lattner0c19df42008-08-23 22:23:09 +00002198 WriteAsOperand(OS, this, true, 0);
2199 } else {
Dan Gohmanc0353bf2009-09-23 01:33:16 +00002200 // Otherwise we don't know what it is. Call the virtual function to
2201 // allow a subclass to print itself.
2202 printCustom(OS);
Chris Lattner0c19df42008-08-23 22:23:09 +00002203 }
2204}
2205
Dan Gohmanc0353bf2009-09-23 01:33:16 +00002206// Value::printCustom - subclasses should override this to implement printing.
2207void Value::printCustom(raw_ostream &OS) const {
2208 llvm_unreachable("Unknown value to print out!");
2209}
2210
Chris Lattnerdab942552008-08-25 17:03:15 +00002211// Value::dump - allow easy printing of Values from the debugger.
David Greenec7f9b122010-01-05 01:29:26 +00002212void Value::dump() const { print(dbgs()); dbgs() << '\n'; }
Reid Spencer52641832004-05-25 18:14:38 +00002213
Chris Lattnerdab942552008-08-25 17:03:15 +00002214// Type::dump - allow easy printing of Types from the debugger.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002215void Type::dump() const { print(dbgs()); }
Chris Lattner24025262009-02-28 21:05:51 +00002216
Chris Lattnerdab942552008-08-25 17:03:15 +00002217// Module::dump() - Allow printing of Modules from the debugger.
David Greenec7f9b122010-01-05 01:29:26 +00002218void Module::dump() const { print(dbgs(), 0); }
Bill Wendling3681d112011-12-09 23:18:34 +00002219
2220// NamedMDNode::dump() - Allow printing of NamedMDNodes from the debugger.
2221void NamedMDNode::dump() const { print(dbgs(), 0); }