blob: 2b23278a88cbad422b108cf5f8affab4879bbe89 [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//
Chandler Carruth9aca9182014-01-07 12:34:26 +000010// This library implements the functionality defined in llvm/IR/Writer.h
Chris Lattner2f7c9632001-06-06 20:29:01 +000011//
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"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "llvm/ADT/DenseMap.h"
19#include "llvm/ADT/STLExtras.h"
20#include "llvm/ADT/SmallString.h"
21#include "llvm/ADT/StringExtras.h"
Bill Wendling5cb50c52012-06-28 00:41:44 +000022#include "llvm/DebugInfo.h"
Chandler Carruth9aca9182014-01-07 12:34:26 +000023#include "llvm/IR/AssemblyAnnotationWriter.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000024#include "llvm/IR/CallingConv.h"
25#include "llvm/IR/Constants.h"
26#include "llvm/IR/DerivedTypes.h"
Chandler Carruthb8ddc702014-01-12 11:10:32 +000027#include "llvm/IR/IRPrintingPasses.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000028#include "llvm/IR/InlineAsm.h"
29#include "llvm/IR/IntrinsicInst.h"
30#include "llvm/IR/LLVMContext.h"
31#include "llvm/IR/Module.h"
32#include "llvm/IR/Operator.h"
Chandler Carruthdcb603f2013-01-07 15:43:51 +000033#include "llvm/IR/TypeFinder.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000034#include "llvm/IR/ValueSymbolTable.h"
Bill Wendlingdfc91892006-11-28 02:09:03 +000035#include "llvm/Support/CFG.h"
David Greenec7f9b122010-01-05 01:29:26 +000036#include "llvm/Support/Debug.h"
Devang Patel711ab5b2009-09-30 20:16:54 +000037#include "llvm/Support/Dwarf.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000038#include "llvm/Support/ErrorHandling.h"
Dan Gohmane2745262009-08-12 17:23:50 +000039#include "llvm/Support/FormattedStream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000040#include "llvm/Support/MathExtras.h"
Chris Lattnerfee714f2001-09-07 16:36:04 +000041#include <algorithm>
Reid Spencerbdf03b42007-05-22 19:27:35 +000042#include <cctype>
Chris Lattner189d19f2003-11-21 20:23:48 +000043using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000044
Reid Spencer294715b2005-05-15 16:13:11 +000045// Make virtual table appear in this compilation unit.
46AssemblyAnnotationWriter::~AssemblyAnnotationWriter() {}
47
Chris Lattner3eee99c2008-08-19 04:36:02 +000048//===----------------------------------------------------------------------===//
49// Helper Functions
50//===----------------------------------------------------------------------===//
51
52static const Module *getModuleFromVal(const Value *V) {
53 if (const Argument *MA = dyn_cast<Argument>(V))
54 return MA->getParent() ? MA->getParent()->getParent() : 0;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +000055
Chris Lattner3eee99c2008-08-19 04:36:02 +000056 if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
57 return BB->getParent() ? BB->getParent()->getParent() : 0;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +000058
Chris Lattner3eee99c2008-08-19 04:36:02 +000059 if (const Instruction *I = dyn_cast<Instruction>(V)) {
60 const Function *M = I->getParent() ? I->getParent()->getParent() : 0;
61 return M ? M->getParent() : 0;
62 }
Andrew Trickec4b6e72011-09-30 19:48:58 +000063
Chris Lattner3eee99c2008-08-19 04:36:02 +000064 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
65 return GV->getParent();
66 return 0;
67}
68
Bill Wendling90bc19c2013-02-20 07:21:42 +000069static void PrintCallingConv(unsigned cc, raw_ostream &Out) {
Micah Villmow857186d2012-09-13 15:11:12 +000070 switch (cc) {
Bill Wendling90bc19c2013-02-20 07:21:42 +000071 default: Out << "cc" << cc; break;
72 case CallingConv::Fast: Out << "fastcc"; break;
73 case CallingConv::Cold: Out << "coldcc"; break;
Andrew Trick5ae6ed82013-11-11 22:40:22 +000074 case CallingConv::WebKit_JS: Out << "webkit_jscc"; break;
75 case CallingConv::AnyReg: Out << "anyregcc"; break;
Juergen Ributzkae6250132014-01-17 19:47:03 +000076 case CallingConv::PreserveMost: Out << "preserve_mostcc"; break;
77 case CallingConv::PreserveAll: Out << "preserve_allcc"; break;
Bill Wendling90bc19c2013-02-20 07:21:42 +000078 case CallingConv::X86_StdCall: Out << "x86_stdcallcc"; break;
79 case CallingConv::X86_FastCall: Out << "x86_fastcallcc"; break;
80 case CallingConv::X86_ThisCall: Out << "x86_thiscallcc"; break;
81 case CallingConv::Intel_OCL_BI: Out << "intel_ocl_bicc"; break;
82 case CallingConv::ARM_APCS: Out << "arm_apcscc"; break;
83 case CallingConv::ARM_AAPCS: Out << "arm_aapcscc"; break;
84 case CallingConv::ARM_AAPCS_VFP: Out << "arm_aapcs_vfpcc"; break;
85 case CallingConv::MSP430_INTR: Out << "msp430_intrcc"; break;
86 case CallingConv::PTX_Kernel: Out << "ptx_kernel"; break;
87 case CallingConv::PTX_Device: Out << "ptx_device"; break;
Charles Davise8f297c2013-07-12 06:02:35 +000088 case CallingConv::X86_64_SysV: Out << "x86_64_sysvcc"; break;
89 case CallingConv::X86_64_Win64: Out << "x86_64_win64cc"; break;
Michael Kupersteine31b4862013-12-15 10:01:20 +000090 case CallingConv::SPIR_FUNC: Out << "spir_func"; break;
91 case CallingConv::SPIR_KERNEL: Out << "spir_kernel"; break;
Micah Villmow857186d2012-09-13 15:11:12 +000092 }
93}
Michael Ilseman26ee2b82012-11-15 22:34:00 +000094
Daniel Dunbardb0b70a2008-10-28 19:33:02 +000095// PrintEscapedString - Print each character of the specified string, escaping
96// it if it is not printable or if it is an escape char.
Chris Lattner9380b812010-07-07 23:16:37 +000097static void PrintEscapedString(StringRef Name, raw_ostream &Out) {
Daniel Dunbare03eecb2009-07-25 23:55:21 +000098 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
99 unsigned char C = Name[i];
Nick Lewycky5aa592a2009-03-15 06:39:52 +0000100 if (isprint(C) && C != '\\' && C != '"')
Daniel Dunbardb0b70a2008-10-28 19:33:02 +0000101 Out << C;
102 else
103 Out << '\\' << hexdigit(C >> 4) << hexdigit(C & 0x0F);
104 }
105}
106
Chris Lattner3eee99c2008-08-19 04:36:02 +0000107enum PrefixType {
108 GlobalPrefix,
109 LabelPrefix,
Daniel Dunbar389529a2008-10-14 23:28:09 +0000110 LocalPrefix,
111 NoPrefix
Chris Lattner3eee99c2008-08-19 04:36:02 +0000112};
113
114/// PrintLLVMName - Turn the specified name into an 'LLVM name', which is either
115/// prefixed with % (if the string only contains simple characters) or is
116/// surrounded with ""'s (if it has special chars in it). Print it out.
Benjamin Kramer92d89982010-07-14 22:38:02 +0000117static void PrintLLVMName(raw_ostream &OS, StringRef Name, PrefixType Prefix) {
Jay Foadf7adb322011-04-24 14:30:00 +0000118 assert(!Name.empty() && "Cannot get empty name!");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000119 switch (Prefix) {
Daniel Dunbar389529a2008-10-14 23:28:09 +0000120 case NoPrefix: break;
Chris Lattner1508d3f2008-08-19 05:16:28 +0000121 case GlobalPrefix: OS << '@'; break;
122 case LabelPrefix: break;
123 case LocalPrefix: OS << '%'; break;
Nick Lewycky063699a2009-03-19 06:31:22 +0000124 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000125
Chris Lattner3eee99c2008-08-19 04:36:02 +0000126 // Scan the name to see if it needs quotes first.
Guy Benyei83c74e92013-02-12 21:21:59 +0000127 bool NeedsQuotes = isdigit(static_cast<unsigned char>(Name[0]));
Chris Lattner3eee99c2008-08-19 04:36:02 +0000128 if (!NeedsQuotes) {
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000129 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
Aaron Ballmaned9b0a92012-07-16 16:18:18 +0000130 // By making this unsigned, the value passed in to isalnum will always be
131 // in the range 0-255. This is important when building with MSVC because
132 // its implementation will assert. This situation can arise when dealing
133 // with UTF-8 multibyte characters.
134 unsigned char C = Name[i];
Guy Benyei83c74e92013-02-12 21:21:59 +0000135 if (!isalnum(static_cast<unsigned char>(C)) && C != '-' && C != '.' &&
136 C != '_') {
Chris Lattner3eee99c2008-08-19 04:36:02 +0000137 NeedsQuotes = true;
138 break;
139 }
140 }
141 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000142
Chris Lattner3eee99c2008-08-19 04:36:02 +0000143 // If we didn't need any quotes, just write out the name in one blast.
144 if (!NeedsQuotes) {
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000145 OS << Name;
Chris Lattner3eee99c2008-08-19 04:36:02 +0000146 return;
147 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000148
Chris Lattner3eee99c2008-08-19 04:36:02 +0000149 // Okay, we need quotes. Output the quotes and escape any scary characters as
150 // needed.
151 OS << '"';
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000152 PrintEscapedString(Name, OS);
Chris Lattner3eee99c2008-08-19 04:36:02 +0000153 OS << '"';
154}
155
156/// PrintLLVMName - Turn the specified name into an 'LLVM name', which is either
157/// prefixed with % (if the string only contains simple characters) or is
158/// surrounded with ""'s (if it has special chars in it). Print it out.
Dan Gohman12dad632009-08-12 20:56:03 +0000159static void PrintLLVMName(raw_ostream &OS, const Value *V) {
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000160 PrintLLVMName(OS, V->getName(),
Chris Lattner3eee99c2008-08-19 04:36:02 +0000161 isa<GlobalValue>(V) ? GlobalPrefix : LocalPrefix);
162}
163
Chris Lattner0f578952009-02-28 20:25:14 +0000164
Daniel Maleaded9f932013-05-08 20:38:31 +0000165namespace llvm {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000166
167void TypePrinting::incorporateTypes(const Module &M) {
Bill Wendling8555a372012-08-03 00:30:35 +0000168 NamedTypes.run(M, false);
Andrew Trickec4b6e72011-09-30 19:48:58 +0000169
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000170 // The list of struct types we got back includes all the struct types, split
171 // the unnamed ones out to a numbering and remove the anonymous structs.
172 unsigned NextNumber = 0;
Andrew Trickec4b6e72011-09-30 19:48:58 +0000173
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000174 std::vector<StructType*>::iterator NextToUse = NamedTypes.begin(), I, E;
175 for (I = NamedTypes.begin(), E = NamedTypes.end(); I != E; ++I) {
176 StructType *STy = *I;
Andrew Trickec4b6e72011-09-30 19:48:58 +0000177
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000178 // Ignore anonymous types.
Chris Lattner01beceb2011-08-12 18:07:07 +0000179 if (STy->isLiteral())
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000180 continue;
Andrew Trickec4b6e72011-09-30 19:48:58 +0000181
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000182 if (STy->getName().empty())
183 NumberedTypes[STy] = NextNumber++;
184 else
185 *NextToUse++ = STy;
186 }
Andrew Trickec4b6e72011-09-30 19:48:58 +0000187
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000188 NamedTypes.erase(NextToUse, NamedTypes.end());
189}
190
191
Chris Lattner40959d02009-02-28 20:49:40 +0000192/// CalcTypeName - Write the specified type to the specified raw_ostream, making
193/// use of type names or up references to shorten the type name where possible.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000194void TypePrinting::print(Type *Ty, raw_ostream &OS) {
Chris Lattner0f578952009-02-28 20:25:14 +0000195 switch (Ty->getTypeID()) {
Rafael Espindolaba7df702013-12-07 02:27:52 +0000196 case Type::VoidTyID: OS << "void"; return;
197 case Type::HalfTyID: OS << "half"; return;
198 case Type::FloatTyID: OS << "float"; return;
199 case Type::DoubleTyID: OS << "double"; return;
200 case Type::X86_FP80TyID: OS << "x86_fp80"; return;
201 case Type::FP128TyID: OS << "fp128"; return;
202 case Type::PPC_FP128TyID: OS << "ppc_fp128"; return;
203 case Type::LabelTyID: OS << "label"; return;
204 case Type::MetadataTyID: OS << "metadata"; return;
205 case Type::X86_MMXTyID: OS << "x86_mmx"; return;
Chris Lattner68318b12009-02-28 21:18:43 +0000206 case Type::IntegerTyID:
Chris Lattner06a23212009-02-28 21:27:31 +0000207 OS << 'i' << cast<IntegerType>(Ty)->getBitWidth();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000208 return;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000209
Chris Lattner07d88292009-02-28 20:35:42 +0000210 case Type::FunctionTyID: {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000211 FunctionType *FTy = cast<FunctionType>(Ty);
212 print(FTy->getReturnType(), OS);
Chris Lattner06a23212009-02-28 21:27:31 +0000213 OS << " (";
Chris Lattner07d88292009-02-28 20:35:42 +0000214 for (FunctionType::param_iterator I = FTy->param_begin(),
215 E = FTy->param_end(); I != E; ++I) {
216 if (I != FTy->param_begin())
Chris Lattner06a23212009-02-28 21:27:31 +0000217 OS << ", ";
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000218 print(*I, OS);
Chris Lattner0f578952009-02-28 20:25:14 +0000219 }
Chris Lattner07d88292009-02-28 20:35:42 +0000220 if (FTy->isVarArg()) {
Chris Lattner06a23212009-02-28 21:27:31 +0000221 if (FTy->getNumParams()) OS << ", ";
222 OS << "...";
Chris Lattner0f578952009-02-28 20:25:14 +0000223 }
Chris Lattner06a23212009-02-28 21:27:31 +0000224 OS << ')';
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000225 return;
Chris Lattner07d88292009-02-28 20:35:42 +0000226 }
227 case Type::StructTyID: {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000228 StructType *STy = cast<StructType>(Ty);
Andrew Trickec4b6e72011-09-30 19:48:58 +0000229
Chris Lattner01beceb2011-08-12 18:07:07 +0000230 if (STy->isLiteral())
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000231 return printStructBody(STy, OS);
232
233 if (!STy->getName().empty())
234 return PrintLLVMName(OS, STy->getName(), LocalPrefix);
Andrew Trickec4b6e72011-09-30 19:48:58 +0000235
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000236 DenseMap<StructType*, unsigned>::iterator I = NumberedTypes.find(STy);
237 if (I != NumberedTypes.end())
238 OS << '%' << I->second;
239 else // Not enumerated, print the hex address.
Benjamin Kramer4e8b4632011-11-02 17:24:36 +0000240 OS << "%\"type " << STy << '\"';
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000241 return;
Chris Lattner07d88292009-02-28 20:35:42 +0000242 }
243 case Type::PointerTyID: {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000244 PointerType *PTy = cast<PointerType>(Ty);
245 print(PTy->getElementType(), OS);
Chris Lattner07d88292009-02-28 20:35:42 +0000246 if (unsigned AddressSpace = PTy->getAddressSpace())
Chris Lattner06a23212009-02-28 21:27:31 +0000247 OS << " addrspace(" << AddressSpace << ')';
248 OS << '*';
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000249 return;
Chris Lattner07d88292009-02-28 20:35:42 +0000250 }
251 case Type::ArrayTyID: {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000252 ArrayType *ATy = cast<ArrayType>(Ty);
Chris Lattner06a23212009-02-28 21:27:31 +0000253 OS << '[' << ATy->getNumElements() << " x ";
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000254 print(ATy->getElementType(), OS);
Chris Lattner06a23212009-02-28 21:27:31 +0000255 OS << ']';
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000256 return;
Chris Lattner07d88292009-02-28 20:35:42 +0000257 }
258 case Type::VectorTyID: {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000259 VectorType *PTy = cast<VectorType>(Ty);
Chris Lattner06a23212009-02-28 21:27:31 +0000260 OS << "<" << PTy->getNumElements() << " x ";
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000261 print(PTy->getElementType(), OS);
Chris Lattner06a23212009-02-28 21:27:31 +0000262 OS << '>';
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000263 return;
Chris Lattner07d88292009-02-28 20:35:42 +0000264 }
Chris Lattner0f578952009-02-28 20:25:14 +0000265 }
Rafael Espindolaba7df702013-12-07 02:27:52 +0000266 llvm_unreachable("Invalid TypeID");
Chris Lattner0f578952009-02-28 20:25:14 +0000267}
268
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000269void TypePrinting::printStructBody(StructType *STy, raw_ostream &OS) {
270 if (STy->isOpaque()) {
271 OS << "opaque";
272 return;
Chris Lattner0f578952009-02-28 20:25:14 +0000273 }
Andrew Trickec4b6e72011-09-30 19:48:58 +0000274
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000275 if (STy->isPacked())
276 OS << '<';
Andrew Trickec4b6e72011-09-30 19:48:58 +0000277
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000278 if (STy->getNumElements() == 0) {
279 OS << "{}";
280 } else {
281 StructType::element_iterator I = STy->element_begin();
282 OS << "{ ";
283 print(*I++, OS);
284 for (StructType::element_iterator E = STy->element_end(); I != E; ++I) {
285 OS << ", ";
286 print(*I, OS);
Chris Lattner3243ea12009-03-01 00:03:38 +0000287 }
Andrew Trickec4b6e72011-09-30 19:48:58 +0000288
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000289 OS << " }";
Chris Lattner92c5c122009-02-28 23:20:19 +0000290 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000291 if (STy->isPacked())
292 OS << '>';
Chris Lattner92c5c122009-02-28 23:20:19 +0000293}
294
Chris Lattner3eee99c2008-08-19 04:36:02 +0000295//===----------------------------------------------------------------------===//
296// SlotTracker Class: Enumerate slot numbers for unnamed values
297//===----------------------------------------------------------------------===//
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000298/// This class provides computation of slot numbers for LLVM Assembly writing.
Chris Lattner393b7cd2008-08-17 04:17:45 +0000299///
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000300class SlotTracker {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000301public:
Devang Patel983c6b12009-07-08 21:44:25 +0000302 /// ValueMap - A mapping of Values to slot numbers.
Chris Lattnera204d412008-08-17 17:25:25 +0000303 typedef DenseMap<const Value*, unsigned> ValueMap;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000304
305private:
Devang Patel983c6b12009-07-08 21:44:25 +0000306 /// TheModule - The module for which we are holding slot numbers.
Chris Lattner393b7cd2008-08-17 04:17:45 +0000307 const Module* TheModule;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000308
Devang Patel983c6b12009-07-08 21:44:25 +0000309 /// TheFunction - The function for which we are holding slot numbers.
Chris Lattner393b7cd2008-08-17 04:17:45 +0000310 const Function* TheFunction;
311 bool FunctionProcessed;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000312
Jay Foaddbb221d2011-07-11 07:28:49 +0000313 /// mMap - The slot map for the module level data.
Chris Lattner393b7cd2008-08-17 04:17:45 +0000314 ValueMap mMap;
315 unsigned mNext;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000316
Jay Foaddbb221d2011-07-11 07:28:49 +0000317 /// fMap - The slot map for the function level data.
Chris Lattner393b7cd2008-08-17 04:17:45 +0000318 ValueMap fMap;
319 unsigned fNext;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000320
Devang Patel983c6b12009-07-08 21:44:25 +0000321 /// mdnMap - Map for MDNodes.
Chris Lattnercf4a76e2009-12-31 02:20:11 +0000322 DenseMap<const MDNode*, unsigned> mdnMap;
Devang Patel983c6b12009-07-08 21:44:25 +0000323 unsigned mdnNext;
Bill Wendling829b4782013-02-11 08:43:33 +0000324
325 /// asMap - The slot map for attribute sets.
326 DenseMap<AttributeSet, unsigned> asMap;
327 unsigned asNext;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000328public:
Chris Lattner393b7cd2008-08-17 04:17:45 +0000329 /// Construct from a module
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000330 explicit SlotTracker(const Module *M);
Chris Lattner393b7cd2008-08-17 04:17:45 +0000331 /// Construct from a function, starting out in incorp state.
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000332 explicit SlotTracker(const Function *F);
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000333
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000334 /// Return the slot number of the specified value in it's type
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000335 /// plane. If something is not in the SlotTracker, return -1.
Chris Lattner5e043322007-01-11 03:54:27 +0000336 int getLocalSlot(const Value *V);
337 int getGlobalSlot(const GlobalValue *V);
Devang Patel983c6b12009-07-08 21:44:25 +0000338 int getMetadataSlot(const MDNode *N);
Bill Wendling829b4782013-02-11 08:43:33 +0000339 int getAttributeGroupSlot(AttributeSet AS);
Reid Spencer8beac692004-06-09 15:26:53 +0000340
Misha Brukmanb1c93172005-04-21 23:48:37 +0000341 /// If you'd like to deal with a function instead of just a module, use
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000342 /// this method to get its data into the SlotTracker.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000343 void incorporateFunction(const Function *F) {
344 TheFunction = F;
Reid Spencerb0ac8c42004-08-16 07:46:33 +0000345 FunctionProcessed = false;
346 }
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000347
Misha Brukmanb1c93172005-04-21 23:48:37 +0000348 /// After calling incorporateFunction, use this method to remove the
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000349 /// most recently incorporated function from the SlotTracker. This
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000350 /// will reset the state of the machine back to just the module contents.
351 void purgeFunction();
352
Devang Patel983c6b12009-07-08 21:44:25 +0000353 /// MDNode map iterators.
Chris Lattnercf4a76e2009-12-31 02:20:11 +0000354 typedef DenseMap<const MDNode*, unsigned>::iterator mdn_iterator;
355 mdn_iterator mdn_begin() { return mdnMap.begin(); }
356 mdn_iterator mdn_end() { return mdnMap.end(); }
357 unsigned mdn_size() const { return mdnMap.size(); }
358 bool mdn_empty() const { return mdnMap.empty(); }
Devang Patel983c6b12009-07-08 21:44:25 +0000359
Bill Wendling829b4782013-02-11 08:43:33 +0000360 /// AttributeSet map iterators.
361 typedef DenseMap<AttributeSet, unsigned>::iterator as_iterator;
362 as_iterator as_begin() { return asMap.begin(); }
363 as_iterator as_end() { return asMap.end(); }
364 unsigned as_size() const { return asMap.size(); }
365 bool as_empty() const { return asMap.empty(); }
366
Reid Spencer56010e42004-05-26 21:56:09 +0000367 /// This function does the actual initialization.
368 inline void initialize();
369
Devang Patel983c6b12009-07-08 21:44:25 +0000370 // Implementation Details
371private:
Chris Lattnerea862a32007-01-09 07:55:49 +0000372 /// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
373 void CreateModuleSlot(const GlobalValue *V);
Devang Patel983c6b12009-07-08 21:44:25 +0000374
375 /// CreateMetadataSlot - Insert the specified MDNode* into the slot table.
376 void CreateMetadataSlot(const MDNode *N);
377
Chris Lattnerea862a32007-01-09 07:55:49 +0000378 /// CreateFunctionSlot - Insert the specified Value* into the slot table.
379 void CreateFunctionSlot(const Value *V);
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000380
Bill Wendling829b4782013-02-11 08:43:33 +0000381 /// \brief Insert the specified AttributeSet into the slot table.
382 void CreateAttributeSetSlot(AttributeSet AS);
383
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000384 /// Add all of the module level global variables (and their initializers)
385 /// and function declarations, but not the contents of those functions.
386 void processModule();
387
Devang Patel983c6b12009-07-08 21:44:25 +0000388 /// Add all of the functions arguments, basic blocks, and instructions.
Reid Spencer56010e42004-05-26 21:56:09 +0000389 void processFunction();
390
Craig Toppera60c0f12012-09-15 17:09:36 +0000391 SlotTracker(const SlotTracker &) LLVM_DELETED_FUNCTION;
392 void operator=(const SlotTracker &) LLVM_DELETED_FUNCTION;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000393};
394
Daniel Maleaded9f932013-05-08 20:38:31 +0000395SlotTracker *createSlotTracker(const Module *M) {
396 return new SlotTracker(M);
397}
Chris Lattner3eee99c2008-08-19 04:36:02 +0000398
399static SlotTracker *createSlotTracker(const Value *V) {
400 if (const Argument *FA = dyn_cast<Argument>(V))
401 return new SlotTracker(FA->getParent());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000402
Chris Lattner3eee99c2008-08-19 04:36:02 +0000403 if (const Instruction *I = dyn_cast<Instruction>(V))
Andrew Trick2f0cbf62011-09-30 19:50:40 +0000404 if (I->getParent())
405 return new SlotTracker(I->getParent()->getParent());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000406
Chris Lattner3eee99c2008-08-19 04:36:02 +0000407 if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
408 return new SlotTracker(BB->getParent());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000409
Chris Lattner3eee99c2008-08-19 04:36:02 +0000410 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
411 return new SlotTracker(GV->getParent());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000412
Chris Lattner3eee99c2008-08-19 04:36:02 +0000413 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V))
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000414 return new SlotTracker(GA->getParent());
415
Chris Lattner3eee99c2008-08-19 04:36:02 +0000416 if (const Function *Func = dyn_cast<Function>(V))
417 return new SlotTracker(Func);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000418
Dan Gohmana2489d12010-07-20 23:55:01 +0000419 if (const MDNode *MD = dyn_cast<MDNode>(V)) {
420 if (!MD->isFunctionLocal())
421 return new SlotTracker(MD->getFunction());
422
Dale Johannesen7b1a7ed2010-01-13 00:00:24 +0000423 return new SlotTracker((Function *)0);
Dan Gohmana2489d12010-07-20 23:55:01 +0000424 }
Dale Johannesen7b1a7ed2010-01-13 00:00:24 +0000425
Chris Lattner3eee99c2008-08-19 04:36:02 +0000426 return 0;
427}
428
429#if 0
David Greenec7f9b122010-01-05 01:29:26 +0000430#define ST_DEBUG(X) dbgs() << X
Chris Lattner3eee99c2008-08-19 04:36:02 +0000431#else
Chris Lattner604e3512008-08-19 04:47:09 +0000432#define ST_DEBUG(X)
Chris Lattner3eee99c2008-08-19 04:36:02 +0000433#endif
434
435// Module level constructor. Causes the contents of the Module (sans functions)
436// to be added to the slot table.
437SlotTracker::SlotTracker(const Module *M)
Andrew Trickec4b6e72011-09-30 19:48:58 +0000438 : TheModule(M), TheFunction(0), FunctionProcessed(false),
Bill Wendling829b4782013-02-11 08:43:33 +0000439 mNext(0), fNext(0), mdnNext(0), asNext(0) {
Chris Lattner3eee99c2008-08-19 04:36:02 +0000440}
441
442// Function level constructor. Causes the contents of the Module and the one
443// function provided to be added to the slot table.
444SlotTracker::SlotTracker(const Function *F)
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000445 : TheModule(F ? F->getParent() : 0), TheFunction(F), FunctionProcessed(false),
Bill Wendling829b4782013-02-11 08:43:33 +0000446 mNext(0), fNext(0), mdnNext(0), asNext(0) {
Chris Lattner3eee99c2008-08-19 04:36:02 +0000447}
448
449inline void SlotTracker::initialize() {
450 if (TheModule) {
451 processModule();
452 TheModule = 0; ///< Prevent re-processing next time we're called.
453 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000454
Chris Lattner3eee99c2008-08-19 04:36:02 +0000455 if (TheFunction && !FunctionProcessed)
456 processFunction();
457}
458
459// Iterate through all the global variables, functions, and global
460// variable initializers and create slots for them.
461void SlotTracker::processModule() {
Chris Lattner604e3512008-08-19 04:47:09 +0000462 ST_DEBUG("begin processModule!\n");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000463
Chris Lattner3eee99c2008-08-19 04:36:02 +0000464 // Add all of the unnamed global variables to the value table.
465 for (Module::const_global_iterator I = TheModule->global_begin(),
Devang Patel983c6b12009-07-08 21:44:25 +0000466 E = TheModule->global_end(); I != E; ++I) {
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000467 if (!I->hasName())
Chris Lattner3eee99c2008-08-19 04:36:02 +0000468 CreateModuleSlot(I);
Devang Patel983c6b12009-07-08 21:44:25 +0000469 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000470
Devang Patel23e68302009-07-29 22:04:47 +0000471 // Add metadata used by named metadata.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000472 for (Module::const_named_metadata_iterator
Devang Patel23e68302009-07-29 22:04:47 +0000473 I = TheModule->named_metadata_begin(),
474 E = TheModule->named_metadata_end(); I != E; ++I) {
475 const NamedMDNode *NMD = I;
Dan Gohman093cb792010-07-21 18:54:18 +0000476 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
477 CreateMetadataSlot(NMD->getOperand(i));
Devang Patel23e68302009-07-29 22:04:47 +0000478 }
479
Chris Lattner3eee99c2008-08-19 04:36:02 +0000480 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
Bill Wendling829b4782013-02-11 08:43:33 +0000481 I != E; ++I) {
Chris Lattner3eee99c2008-08-19 04:36:02 +0000482 if (!I->hasName())
Bill Wendling829b4782013-02-11 08:43:33 +0000483 // Add all the unnamed functions to the table.
Chris Lattner3eee99c2008-08-19 04:36:02 +0000484 CreateModuleSlot(I);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000485
Bill Wendling829b4782013-02-11 08:43:33 +0000486 // Add all the function attributes to the table.
Bill Wendling90bc19c2013-02-20 07:21:42 +0000487 // FIXME: Add attributes of other objects?
Bill Wendling829b4782013-02-11 08:43:33 +0000488 AttributeSet FnAttrs = I->getAttributes().getFnAttributes();
489 if (FnAttrs.hasAttributes(AttributeSet::FunctionIndex))
490 CreateAttributeSetSlot(FnAttrs);
491 }
492
Chris Lattner604e3512008-08-19 04:47:09 +0000493 ST_DEBUG("end processModule!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000494}
495
Chris Lattner3eee99c2008-08-19 04:36:02 +0000496// Process the arguments, basic blocks, and instructions of a function.
497void SlotTracker::processFunction() {
Chris Lattner604e3512008-08-19 04:47:09 +0000498 ST_DEBUG("begin processFunction!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000499 fNext = 0;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000500
Chris Lattner3eee99c2008-08-19 04:36:02 +0000501 // Add all the function arguments with no names.
502 for(Function::const_arg_iterator AI = TheFunction->arg_begin(),
503 AE = TheFunction->arg_end(); AI != AE; ++AI)
504 if (!AI->hasName())
505 CreateFunctionSlot(AI);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000506
Chris Lattner604e3512008-08-19 04:47:09 +0000507 ST_DEBUG("Inserting Instructions:\n");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000508
Chris Lattnerbddea6a2009-12-31 02:13:35 +0000509 SmallVector<std::pair<unsigned, MDNode*>, 4> MDForInst;
Devang Pateldec23fd2009-09-16 20:21:17 +0000510
Chris Lattner3eee99c2008-08-19 04:36:02 +0000511 // Add all of the basic blocks and instructions with no names.
512 for (Function::const_iterator BB = TheFunction->begin(),
513 E = TheFunction->end(); BB != E; ++BB) {
514 if (!BB->hasName())
515 CreateFunctionSlot(BB);
Andrew Trickec4b6e72011-09-30 19:48:58 +0000516
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000517 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E;
Devang Patel983c6b12009-07-08 21:44:25 +0000518 ++I) {
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000519 if (!I->getType()->isVoidTy() && !I->hasName())
Chris Lattner3eee99c2008-08-19 04:36:02 +0000520 CreateFunctionSlot(I);
Andrew Trickec4b6e72011-09-30 19:48:58 +0000521
Chris Lattner58aff8f2010-05-10 20:53:17 +0000522 // Intrinsics can directly use metadata. We allow direct calls to any
523 // llvm.foo function here, because the target may not be linked into the
524 // optimizer.
525 if (const CallInst *CI = dyn_cast<CallInst>(I)) {
526 if (Function *F = CI->getCalledFunction())
Matt Arsenaulta68c9ad2013-12-05 06:05:43 +0000527 if (F->isIntrinsic())
Chris Lattner58aff8f2010-05-10 20:53:17 +0000528 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
529 if (MDNode *N = dyn_cast_or_null<MDNode>(I->getOperand(i)))
530 CreateMetadataSlot(N);
Bill Wendling6da216f2013-02-20 00:04:41 +0000531
Bill Wendlinga0323742013-02-22 09:09:42 +0000532 // Add all the call attributes to the table.
533 AttributeSet Attrs = CI->getAttributes().getFnAttributes();
534 if (Attrs.hasAttributes(AttributeSet::FunctionIndex))
535 CreateAttributeSetSlot(Attrs);
536 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(I)) {
537 // Add all the call attributes to the table.
538 AttributeSet Attrs = II->getAttributes().getFnAttributes();
539 if (Attrs.hasAttributes(AttributeSet::FunctionIndex))
540 CreateAttributeSetSlot(Attrs);
Chris Lattner58aff8f2010-05-10 20:53:17 +0000541 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000542
Devang Pateldec23fd2009-09-16 20:21:17 +0000543 // Process metadata attached with this instruction.
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000544 I->getAllMetadata(MDForInst);
545 for (unsigned i = 0, e = MDForInst.size(); i != e; ++i)
546 CreateMetadataSlot(MDForInst[i].second);
Chris Lattnerbddea6a2009-12-31 02:13:35 +0000547 MDForInst.clear();
Devang Patel983c6b12009-07-08 21:44:25 +0000548 }
Chris Lattner3eee99c2008-08-19 04:36:02 +0000549 }
Devang Pateldec23fd2009-09-16 20:21:17 +0000550
Chris Lattner3eee99c2008-08-19 04:36:02 +0000551 FunctionProcessed = true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000552
Chris Lattner604e3512008-08-19 04:47:09 +0000553 ST_DEBUG("end processFunction!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000554}
555
556/// Clean up after incorporating a function. This is the only way to get out of
557/// the function incorporation state that affects get*Slot/Create*Slot. Function
558/// incorporation state is indicated by TheFunction != 0.
559void SlotTracker::purgeFunction() {
Chris Lattner604e3512008-08-19 04:47:09 +0000560 ST_DEBUG("begin purgeFunction!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000561 fMap.clear(); // Simply discard the function level map
562 TheFunction = 0;
563 FunctionProcessed = false;
Chris Lattner604e3512008-08-19 04:47:09 +0000564 ST_DEBUG("end purgeFunction!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000565}
566
567/// getGlobalSlot - Get the slot number of a global value.
568int SlotTracker::getGlobalSlot(const GlobalValue *V) {
569 // Check for uninitialized state and do lazy initialization.
570 initialize();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000571
Jay Foaddbb221d2011-07-11 07:28:49 +0000572 // Find the value in the module map
Chris Lattner3eee99c2008-08-19 04:36:02 +0000573 ValueMap::iterator MI = mMap.find(V);
Dan Gohman1dd27572008-10-01 19:58:59 +0000574 return MI == mMap.end() ? -1 : (int)MI->second;
Chris Lattner3eee99c2008-08-19 04:36:02 +0000575}
576
Chris Lattnercf4a76e2009-12-31 02:20:11 +0000577/// getMetadataSlot - Get the slot number of a MDNode.
Devang Patel983c6b12009-07-08 21:44:25 +0000578int SlotTracker::getMetadataSlot(const MDNode *N) {
579 // Check for uninitialized state and do lazy initialization.
580 initialize();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000581
Jay Foaddbb221d2011-07-11 07:28:49 +0000582 // Find the MDNode in the module map
Chris Lattnercf4a76e2009-12-31 02:20:11 +0000583 mdn_iterator MI = mdnMap.find(N);
Devang Patel983c6b12009-07-08 21:44:25 +0000584 return MI == mdnMap.end() ? -1 : (int)MI->second;
585}
586
Chris Lattner3eee99c2008-08-19 04:36:02 +0000587
588/// getLocalSlot - Get the slot number for a value that is local to a function.
589int SlotTracker::getLocalSlot(const Value *V) {
590 assert(!isa<Constant>(V) && "Can't get a constant or global slot with this!");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000591
Chris Lattner3eee99c2008-08-19 04:36:02 +0000592 // Check for uninitialized state and do lazy initialization.
593 initialize();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000594
Chris Lattner3eee99c2008-08-19 04:36:02 +0000595 ValueMap::iterator FI = fMap.find(V);
Dan Gohman1dd27572008-10-01 19:58:59 +0000596 return FI == fMap.end() ? -1 : (int)FI->second;
Chris Lattner3eee99c2008-08-19 04:36:02 +0000597}
598
Bill Wendling829b4782013-02-11 08:43:33 +0000599int SlotTracker::getAttributeGroupSlot(AttributeSet AS) {
600 // Check for uninitialized state and do lazy initialization.
601 initialize();
602
603 // Find the AttributeSet in the module map.
604 as_iterator AI = asMap.find(AS);
605 return AI == asMap.end() ? -1 : (int)AI->second;
606}
Chris Lattner3eee99c2008-08-19 04:36:02 +0000607
608/// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
609void SlotTracker::CreateModuleSlot(const GlobalValue *V) {
610 assert(V && "Can't insert a null Value into SlotTracker!");
Chris Lattner031560c2009-12-29 07:25:48 +0000611 assert(!V->getType()->isVoidTy() && "Doesn't need a slot!");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000612 assert(!V->hasName() && "Doesn't need a slot!");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000613
Chris Lattner3eee99c2008-08-19 04:36:02 +0000614 unsigned DestSlot = mNext++;
615 mMap[V] = DestSlot;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000616
Chris Lattner604e3512008-08-19 04:47:09 +0000617 ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" <<
Chris Lattner3eee99c2008-08-19 04:36:02 +0000618 DestSlot << " [");
619 // G = Global, F = Function, A = Alias, o = other
Chris Lattner604e3512008-08-19 04:47:09 +0000620 ST_DEBUG((isa<GlobalVariable>(V) ? 'G' :
Chris Lattner3eee99c2008-08-19 04:36:02 +0000621 (isa<Function>(V) ? 'F' :
622 (isa<GlobalAlias>(V) ? 'A' : 'o'))) << "]\n");
623}
624
Chris Lattner3eee99c2008-08-19 04:36:02 +0000625/// CreateSlot - Create a new slot for the specified value if it has no name.
626void SlotTracker::CreateFunctionSlot(const Value *V) {
Chris Lattner031560c2009-12-29 07:25:48 +0000627 assert(!V->getType()->isVoidTy() && !V->hasName() && "Doesn't need a slot!");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000628
Chris Lattner3eee99c2008-08-19 04:36:02 +0000629 unsigned DestSlot = fNext++;
630 fMap[V] = DestSlot;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000631
Chris Lattner3eee99c2008-08-19 04:36:02 +0000632 // G = Global, F = Function, o = other
Chris Lattner604e3512008-08-19 04:47:09 +0000633 ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" <<
Chris Lattner3eee99c2008-08-19 04:36:02 +0000634 DestSlot << " [o]\n");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000635}
Chris Lattner3eee99c2008-08-19 04:36:02 +0000636
Devang Patel983c6b12009-07-08 21:44:25 +0000637/// CreateModuleSlot - Insert the specified MDNode* into the slot table.
638void SlotTracker::CreateMetadataSlot(const MDNode *N) {
639 assert(N && "Can't insert a null Value into SlotTracker!");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000640
Chris Lattner0d50bdd2009-12-31 02:27:30 +0000641 // Don't insert if N is a function-local metadata, these are always printed
642 // inline.
Dan Gohmana2489d12010-07-20 23:55:01 +0000643 if (!N->isFunctionLocal()) {
644 mdn_iterator I = mdnMap.find(N);
645 if (I != mdnMap.end())
646 return;
Victor Hernandez4d633542009-12-04 20:07:10 +0000647
Dan Gohmana2489d12010-07-20 23:55:01 +0000648 unsigned DestSlot = mdnNext++;
649 mdnMap[N] = DestSlot;
650 }
Devang Patel983c6b12009-07-08 21:44:25 +0000651
Chris Lattner0d50bdd2009-12-31 02:27:30 +0000652 // Recursively add any MDNodes referenced by operands.
653 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
654 if (const MDNode *Op = dyn_cast_or_null<MDNode>(N->getOperand(i)))
655 CreateMetadataSlot(Op);
Devang Patel983c6b12009-07-08 21:44:25 +0000656}
Chris Lattner3eee99c2008-08-19 04:36:02 +0000657
Bill Wendling829b4782013-02-11 08:43:33 +0000658void SlotTracker::CreateAttributeSetSlot(AttributeSet AS) {
659 assert(AS.hasAttributes(AttributeSet::FunctionIndex) &&
660 "Doesn't need a slot!");
661
662 as_iterator I = asMap.find(AS);
663 if (I != asMap.end())
664 return;
665
666 unsigned DestSlot = asNext++;
667 asMap[AS] = DestSlot;
668}
669
Chris Lattner3eee99c2008-08-19 04:36:02 +0000670//===----------------------------------------------------------------------===//
671// AsmWriter Implementation
672//===----------------------------------------------------------------------===//
Chris Lattner7f8845a2002-07-23 18:07:49 +0000673
Dan Gohman12dad632009-08-12 20:56:03 +0000674static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
Dan Gohman8061d9e2009-08-13 15:27:57 +0000675 TypePrinting *TypePrinter,
Dan Gohmana2489d12010-07-20 23:55:01 +0000676 SlotTracker *Machine,
677 const Module *Context);
Reid Spencer58d30f22004-07-04 11:50:43 +0000678
Chris Lattnerfc9f1c92006-12-06 06:40:49 +0000679static const char *getPredicateText(unsigned predicate) {
Reid Spencer812a1be2006-12-04 05:19:18 +0000680 const char * pred = "unknown";
681 switch (predicate) {
Chris Lattnerbddea6a2009-12-31 02:13:35 +0000682 case FCmpInst::FCMP_FALSE: pred = "false"; break;
683 case FCmpInst::FCMP_OEQ: pred = "oeq"; break;
684 case FCmpInst::FCMP_OGT: pred = "ogt"; break;
685 case FCmpInst::FCMP_OGE: pred = "oge"; break;
686 case FCmpInst::FCMP_OLT: pred = "olt"; break;
687 case FCmpInst::FCMP_OLE: pred = "ole"; break;
688 case FCmpInst::FCMP_ONE: pred = "one"; break;
689 case FCmpInst::FCMP_ORD: pred = "ord"; break;
690 case FCmpInst::FCMP_UNO: pred = "uno"; break;
691 case FCmpInst::FCMP_UEQ: pred = "ueq"; break;
692 case FCmpInst::FCMP_UGT: pred = "ugt"; break;
693 case FCmpInst::FCMP_UGE: pred = "uge"; break;
694 case FCmpInst::FCMP_ULT: pred = "ult"; break;
695 case FCmpInst::FCMP_ULE: pred = "ule"; break;
696 case FCmpInst::FCMP_UNE: pred = "une"; break;
697 case FCmpInst::FCMP_TRUE: pred = "true"; break;
698 case ICmpInst::ICMP_EQ: pred = "eq"; break;
699 case ICmpInst::ICMP_NE: pred = "ne"; break;
700 case ICmpInst::ICMP_SGT: pred = "sgt"; break;
701 case ICmpInst::ICMP_SGE: pred = "sge"; break;
702 case ICmpInst::ICMP_SLT: pred = "slt"; break;
703 case ICmpInst::ICMP_SLE: pred = "sle"; break;
704 case ICmpInst::ICMP_UGT: pred = "ugt"; break;
705 case ICmpInst::ICMP_UGE: pred = "uge"; break;
706 case ICmpInst::ICMP_ULT: pred = "ult"; break;
707 case ICmpInst::ICMP_ULE: pred = "ule"; break;
Reid Spencer812a1be2006-12-04 05:19:18 +0000708 }
709 return pred;
710}
711
Eli Friedmanc9a551e2011-07-28 21:48:00 +0000712static void writeAtomicRMWOperation(raw_ostream &Out,
713 AtomicRMWInst::BinOp Op) {
714 switch (Op) {
715 default: Out << " <unknown operation " << Op << ">"; break;
716 case AtomicRMWInst::Xchg: Out << " xchg"; break;
717 case AtomicRMWInst::Add: Out << " add"; break;
718 case AtomicRMWInst::Sub: Out << " sub"; break;
719 case AtomicRMWInst::And: Out << " and"; break;
720 case AtomicRMWInst::Nand: Out << " nand"; break;
721 case AtomicRMWInst::Or: Out << " or"; break;
722 case AtomicRMWInst::Xor: Out << " xor"; break;
723 case AtomicRMWInst::Max: Out << " max"; break;
724 case AtomicRMWInst::Min: Out << " min"; break;
725 case AtomicRMWInst::UMax: Out << " umax"; break;
726 case AtomicRMWInst::UMin: Out << " umin"; break;
727 }
728}
Devang Patel983c6b12009-07-08 21:44:25 +0000729
Dan Gohman12dad632009-08-12 20:56:03 +0000730static void WriteOptimizationInfo(raw_ostream &Out, const User *U) {
Michael Ilseman92053172012-11-27 00:42:44 +0000731 if (const FPMathOperator *FPO = dyn_cast<const FPMathOperator>(U)) {
732 // Unsafe algebra implies all the others, no need to write them all out
733 if (FPO->hasUnsafeAlgebra())
734 Out << " fast";
735 else {
736 if (FPO->hasNoNaNs())
737 Out << " nnan";
738 if (FPO->hasNoInfs())
739 Out << " ninf";
740 if (FPO->hasNoSignedZeros())
741 Out << " nsz";
742 if (FPO->hasAllowReciprocal())
743 Out << " arcp";
744 }
745 }
746
Dan Gohman0ebd6962009-07-20 21:19:07 +0000747 if (const OverflowingBinaryOperator *OBO =
748 dyn_cast<OverflowingBinaryOperator>(U)) {
Dan Gohman16f54152009-08-20 17:11:38 +0000749 if (OBO->hasNoUnsignedWrap())
Dan Gohman9c7f8082009-07-27 16:11:46 +0000750 Out << " nuw";
Dan Gohman16f54152009-08-20 17:11:38 +0000751 if (OBO->hasNoSignedWrap())
Dan Gohman9c7f8082009-07-27 16:11:46 +0000752 Out << " nsw";
Chris Lattner35315d02011-02-06 21:44:57 +0000753 } else if (const PossiblyExactOperator *Div =
754 dyn_cast<PossiblyExactOperator>(U)) {
Dan Gohman0ebd6962009-07-20 21:19:07 +0000755 if (Div->isExact())
Dan Gohman9c7f8082009-07-27 16:11:46 +0000756 Out << " exact";
Dan Gohman1639c392009-07-27 21:53:46 +0000757 } else if (const GEPOperator *GEP = dyn_cast<GEPOperator>(U)) {
758 if (GEP->isInBounds())
759 Out << " inbounds";
Dan Gohman0ebd6962009-07-20 21:19:07 +0000760 }
761}
762
Dan Gohmanefb8dbb2010-07-14 20:57:55 +0000763static void WriteConstantInternal(raw_ostream &Out, const Constant *CV,
764 TypePrinting &TypePrinter,
Dan Gohmana2489d12010-07-20 23:55:01 +0000765 SlotTracker *Machine,
766 const Module *Context) {
Zhou Sheng75b871f2007-01-11 12:24:14 +0000767 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Duncan Sands9dff9be2010-02-15 16:12:20 +0000768 if (CI->getType()->isIntegerTy(1)) {
Reid Spencercddc9df2007-01-12 04:24:46 +0000769 Out << (CI->getZExtValue() ? "true" : "false");
Chris Lattner17f71652008-08-17 07:19:36 +0000770 return;
771 }
772 Out << CI->getValue();
773 return;
774 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000775
Chris Lattner17f71652008-08-17 07:19:36 +0000776 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Tobias Grosser6b31d172012-05-24 15:59:06 +0000777 if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEsingle ||
Dan Gohman518cda42011-12-17 00:04:22 +0000778 &CFP->getValueAPF().getSemantics() == &APFloat::IEEEdouble) {
Dale Johannesen028084e2007-09-12 03:30:33 +0000779 // We would like to output the FP constant value in exponential notation,
780 // but we cannot do this if doing so will lose precision. Check here to
781 // make sure that we only output it in exponential format if we can parse
782 // the value back and get the same value.
783 //
Dale Johannesen1f864982009-01-21 20:32:55 +0000784 bool ignored;
Dan Gohman518cda42011-12-17 00:04:22 +0000785 bool isHalf = &CFP->getValueAPF().getSemantics()==&APFloat::IEEEhalf;
Dale Johannesen028084e2007-09-12 03:30:33 +0000786 bool isDouble = &CFP->getValueAPF().getSemantics()==&APFloat::IEEEdouble;
NAKAMURA Takumi35d19c02012-02-16 08:12:24 +0000787 bool isInf = CFP->getValueAPF().isInfinity();
788 bool isNaN = CFP->getValueAPF().isNaN();
789 if (!isHalf && !isInf && !isNaN) {
Dan Gohman518cda42011-12-17 00:04:22 +0000790 double Val = isDouble ? CFP->getValueAPF().convertToDouble() :
791 CFP->getValueAPF().convertToFloat();
792 SmallString<128> StrVal;
793 raw_svector_ostream(StrVal) << Val;
Chris Lattner1e194682002-04-18 18:53:13 +0000794
Dan Gohman518cda42011-12-17 00:04:22 +0000795 // Check to make sure that the stringized number is not some string like
796 // "Inf" or NaN, that atof will accept, but the lexer will not. Check
797 // that the string matches the "[-+]?[0-9]" regex.
798 //
799 if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
800 ((StrVal[0] == '-' || StrVal[0] == '+') &&
801 (StrVal[1] >= '0' && StrVal[1] <= '9'))) {
802 // Reparse stringized version!
NAKAMURA Takumiaec41232012-02-16 04:19:15 +0000803 if (APFloat(APFloat::IEEEdouble, StrVal).convertToDouble() == Val) {
Dan Gohman518cda42011-12-17 00:04:22 +0000804 Out << StrVal.str();
805 return;
806 }
Dale Johannesen028084e2007-09-12 03:30:33 +0000807 }
Chris Lattner1e194682002-04-18 18:53:13 +0000808 }
Dale Johannesen028084e2007-09-12 03:30:33 +0000809 // Otherwise we could not reparse it to exactly the same value, so we must
Dale Johannesen1f864982009-01-21 20:32:55 +0000810 // output the string in hexadecimal format! Note that loading and storing
811 // floating point types changes the bits of NaNs on some hosts, notably
812 // x86, so we must not use these types.
Dale Johannesen028084e2007-09-12 03:30:33 +0000813 assert(sizeof(double) == sizeof(uint64_t) &&
814 "assuming that double is 64 bits!");
Chris Lattner5505eed2008-11-10 04:30:26 +0000815 char Buffer[40];
Dale Johannesen1f864982009-01-21 20:32:55 +0000816 APFloat apf = CFP->getValueAPF();
Dan Gohman518cda42011-12-17 00:04:22 +0000817 // Halves and floats are represented in ASCII IR as double, convert.
Dale Johannesen1f864982009-01-21 20:32:55 +0000818 if (!isDouble)
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000819 apf.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven,
Dale Johannesen1f864982009-01-21 20:32:55 +0000820 &ignored);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000821 Out << "0x" <<
822 utohex_buffer(uint64_t(apf.bitcastToAPInt().getZExtValue()),
Dale Johannesen1f864982009-01-21 20:32:55 +0000823 Buffer+40);
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000824 return;
825 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000826
Tobias Grosser6b31d172012-05-24 15:59:06 +0000827 // Either half, or some form of long double.
828 // These appear as a magic letter identifying the type, then a
829 // fixed number of hex digits.
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000830 Out << "0x";
Tobias Grosser6b31d172012-05-24 15:59:06 +0000831 // Bit position, in the current word, of the next nibble to print.
832 int shiftcount;
833
Dale Johannesen93eefa02009-03-23 21:16:53 +0000834 if (&CFP->getValueAPF().getSemantics() == &APFloat::x87DoubleExtended) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000835 Out << 'K';
Dale Johannesen93eefa02009-03-23 21:16:53 +0000836 // api needed to prevent premature destruction
837 APInt api = CFP->getValueAPF().bitcastToAPInt();
838 const uint64_t* p = api.getRawData();
839 uint64_t word = p[1];
Tobias Grosser6b31d172012-05-24 15:59:06 +0000840 shiftcount = 12;
Dale Johannesen93eefa02009-03-23 21:16:53 +0000841 int width = api.getBitWidth();
842 for (int j=0; j<width; j+=4, shiftcount-=4) {
843 unsigned int nibble = (word>>shiftcount) & 15;
844 if (nibble < 10)
845 Out << (unsigned char)(nibble + '0');
846 else
847 Out << (unsigned char)(nibble - 10 + 'A');
848 if (shiftcount == 0 && j+4 < width) {
849 word = *p;
850 shiftcount = 64;
851 if (width-j-4 < 64)
852 shiftcount = width-j-4;
853 }
854 }
855 return;
Tobias Grosser6b31d172012-05-24 15:59:06 +0000856 } else if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEquad) {
857 shiftcount = 60;
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000858 Out << 'L';
Tobias Grosser6b31d172012-05-24 15:59:06 +0000859 } else if (&CFP->getValueAPF().getSemantics() == &APFloat::PPCDoubleDouble) {
860 shiftcount = 60;
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000861 Out << 'M';
Tobias Grosser6b31d172012-05-24 15:59:06 +0000862 } else if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEhalf) {
863 shiftcount = 12;
864 Out << 'H';
865 } else
Torok Edwinfbcc6632009-07-14 16:55:14 +0000866 llvm_unreachable("Unsupported floating point type");
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000867 // api needed to prevent premature destruction
Dale Johannesen54306fe2008-10-09 18:53:47 +0000868 APInt api = CFP->getValueAPF().bitcastToAPInt();
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000869 const uint64_t* p = api.getRawData();
870 uint64_t word = *p;
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000871 int width = api.getBitWidth();
872 for (int j=0; j<width; j+=4, shiftcount-=4) {
873 unsigned int nibble = (word>>shiftcount) & 15;
874 if (nibble < 10)
875 Out << (unsigned char)(nibble + '0');
Dale Johannesen028084e2007-09-12 03:30:33 +0000876 else
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000877 Out << (unsigned char)(nibble - 10 + 'A');
878 if (shiftcount == 0 && j+4 < width) {
879 word = *(++p);
880 shiftcount = 64;
881 if (width-j-4 < 64)
882 shiftcount = width-j-4;
Dale Johannesen028084e2007-09-12 03:30:33 +0000883 }
884 }
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000885 return;
886 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000887
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000888 if (isa<ConstantAggregateZero>(CV)) {
Chris Lattner76b2ff42004-02-15 05:55:15 +0000889 Out << "zeroinitializer";
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000890 return;
891 }
Andrew Trickec4b6e72011-09-30 19:48:58 +0000892
Chris Lattner214cc702009-10-28 03:38:12 +0000893 if (const BlockAddress *BA = dyn_cast<BlockAddress>(CV)) {
894 Out << "blockaddress(";
Dan Gohmana2489d12010-07-20 23:55:01 +0000895 WriteAsOperandInternal(Out, BA->getFunction(), &TypePrinter, Machine,
896 Context);
Chris Lattner214cc702009-10-28 03:38:12 +0000897 Out << ", ";
Dan Gohmana2489d12010-07-20 23:55:01 +0000898 WriteAsOperandInternal(Out, BA->getBasicBlock(), &TypePrinter, Machine,
899 Context);
Chris Lattner214cc702009-10-28 03:38:12 +0000900 Out << ")";
901 return;
902 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000903
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000904 if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000905 Type *ETy = CA->getType()->getElementType();
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000906 Out << '[';
907 TypePrinter.print(ETy, Out);
908 Out << ' ';
909 WriteAsOperandInternal(Out, CA->getOperand(0),
910 &TypePrinter, Machine,
911 Context);
912 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
913 Out << ", ";
Chris Lattner9c181f62012-01-31 03:15:40 +0000914 TypePrinter.print(ETy, Out);
915 Out << ' ';
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000916 WriteAsOperandInternal(Out, CA->getOperand(i), &TypePrinter, Machine,
Chris Lattner9c181f62012-01-31 03:15:40 +0000917 Context);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000918 }
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000919 Out << ']';
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000920 return;
921 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000922
Chris Lattnerfa775002012-01-26 02:32:04 +0000923 if (const ConstantDataArray *CA = dyn_cast<ConstantDataArray>(CV)) {
924 // As a special case, print the array as a string if it is an array of
925 // i8 with ConstantInt values.
926 if (CA->isString()) {
927 Out << "c\"";
928 PrintEscapedString(CA->getAsString(), Out);
929 Out << '"';
930 return;
931 }
932
933 Type *ETy = CA->getType()->getElementType();
934 Out << '[';
Chris Lattner9c181f62012-01-31 03:15:40 +0000935 TypePrinter.print(ETy, Out);
936 Out << ' ';
937 WriteAsOperandInternal(Out, CA->getElementAsConstant(0),
938 &TypePrinter, Machine,
939 Context);
940 for (unsigned i = 1, e = CA->getNumElements(); i != e; ++i) {
941 Out << ", ";
Chris Lattnerfa775002012-01-26 02:32:04 +0000942 TypePrinter.print(ETy, Out);
943 Out << ' ';
Chris Lattner9c181f62012-01-31 03:15:40 +0000944 WriteAsOperandInternal(Out, CA->getElementAsConstant(i), &TypePrinter,
945 Machine, Context);
Chris Lattnerfa775002012-01-26 02:32:04 +0000946 }
Chris Lattner9c181f62012-01-31 03:15:40 +0000947 Out << ']';
Chris Lattnerfa775002012-01-26 02:32:04 +0000948 return;
949 }
950
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000951
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000952 if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
Andrew Lenharth0d124b82007-01-08 18:21:30 +0000953 if (CS->getType()->isPacked())
954 Out << '<';
Misha Brukman21bbdb92004-06-04 21:11:51 +0000955 Out << '{';
Jim Laskey3bb78742006-02-25 12:27:03 +0000956 unsigned N = CS->getNumOperands();
957 if (N) {
Chris Lattner604e3512008-08-19 04:47:09 +0000958 Out << ' ';
Chris Lattnere101c442009-02-28 21:26:53 +0000959 TypePrinter.print(CS->getOperand(0)->getType(), Out);
Dan Gohman81313fd2008-09-14 17:21:12 +0000960 Out << ' ';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000961
Dan Gohmana2489d12010-07-20 23:55:01 +0000962 WriteAsOperandInternal(Out, CS->getOperand(0), &TypePrinter, Machine,
963 Context);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000964
Jim Laskey3bb78742006-02-25 12:27:03 +0000965 for (unsigned i = 1; i < N; i++) {
Chris Lattnerd84bb632002-04-16 21:36:08 +0000966 Out << ", ";
Chris Lattnere101c442009-02-28 21:26:53 +0000967 TypePrinter.print(CS->getOperand(i)->getType(), Out);
Dan Gohman81313fd2008-09-14 17:21:12 +0000968 Out << ' ';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000969
Dan Gohmana2489d12010-07-20 23:55:01 +0000970 WriteAsOperandInternal(Out, CS->getOperand(i), &TypePrinter, Machine,
971 Context);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000972 }
Dan Gohman81313fd2008-09-14 17:21:12 +0000973 Out << ' ';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000974 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000975
Dan Gohman81313fd2008-09-14 17:21:12 +0000976 Out << '}';
Andrew Lenharth0d124b82007-01-08 18:21:30 +0000977 if (CS->getType()->isPacked())
978 Out << '>';
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000979 return;
980 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000981
Chris Lattnerfa775002012-01-26 02:32:04 +0000982 if (isa<ConstantVector>(CV) || isa<ConstantDataVector>(CV)) {
983 Type *ETy = CV->getType()->getVectorElementType();
Dan Gohman1262a252009-02-11 00:25:25 +0000984 Out << '<';
Chris Lattnere101c442009-02-28 21:26:53 +0000985 TypePrinter.print(ETy, Out);
Dan Gohman81313fd2008-09-14 17:21:12 +0000986 Out << ' ';
Chris Lattnerfa775002012-01-26 02:32:04 +0000987 WriteAsOperandInternal(Out, CV->getAggregateElement(0U), &TypePrinter,
988 Machine, Context);
989 for (unsigned i = 1, e = CV->getType()->getVectorNumElements(); i != e;++i){
Chris Lattner585297e82008-08-19 05:26:17 +0000990 Out << ", ";
Chris Lattnere101c442009-02-28 21:26:53 +0000991 TypePrinter.print(ETy, Out);
Dan Gohman81313fd2008-09-14 17:21:12 +0000992 Out << ' ';
Chris Lattnerfa775002012-01-26 02:32:04 +0000993 WriteAsOperandInternal(Out, CV->getAggregateElement(i), &TypePrinter,
994 Machine, Context);
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000995 }
Dan Gohman1262a252009-02-11 00:25:25 +0000996 Out << '>';
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000997 return;
998 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000999
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001000 if (isa<ConstantPointerNull>(CV)) {
Chris Lattnerd84bb632002-04-16 21:36:08 +00001001 Out << "null";
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001002 return;
1003 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001004
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001005 if (isa<UndefValue>(CV)) {
Chris Lattner5e0b9f22004-10-16 18:08:06 +00001006 Out << "undef";
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001007 return;
1008 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001009
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001010 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Reid Spencer812a1be2006-12-04 05:19:18 +00001011 Out << CE->getOpcodeName();
Dan Gohman9c7f8082009-07-27 16:11:46 +00001012 WriteOptimizationInfo(Out, CE);
Reid Spencer812a1be2006-12-04 05:19:18 +00001013 if (CE->isCompare())
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001014 Out << ' ' << getPredicateText(CE->getPredicate());
Reid Spencer812a1be2006-12-04 05:19:18 +00001015 Out << " (";
Misha Brukmanb1c93172005-04-21 23:48:37 +00001016
Vikram S. Adveb952b542002-07-14 23:14:45 +00001017 for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) {
Chris Lattnere101c442009-02-28 21:26:53 +00001018 TypePrinter.print((*OI)->getType(), Out);
Dan Gohman81313fd2008-09-14 17:21:12 +00001019 Out << ' ';
Dan Gohmana2489d12010-07-20 23:55:01 +00001020 WriteAsOperandInternal(Out, *OI, &TypePrinter, Machine, Context);
Vikram S. Adveb952b542002-07-14 23:14:45 +00001021 if (OI+1 != CE->op_end())
Chris Lattner3cd8c562002-07-30 18:54:25 +00001022 Out << ", ";
Vikram S. Adveb952b542002-07-14 23:14:45 +00001023 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001024
Dan Gohmana76f0f72008-05-31 19:12:39 +00001025 if (CE->hasIndices()) {
Jay Foad0091fe82011-04-13 15:22:40 +00001026 ArrayRef<unsigned> Indices = CE->getIndices();
Dan Gohmana76f0f72008-05-31 19:12:39 +00001027 for (unsigned i = 0, e = Indices.size(); i != e; ++i)
1028 Out << ", " << Indices[i];
1029 }
1030
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001031 if (CE->isCast()) {
Chris Lattner83b396b2002-08-15 19:37:43 +00001032 Out << " to ";
Chris Lattnere101c442009-02-28 21:26:53 +00001033 TypePrinter.print(CE->getType(), Out);
Chris Lattner83b396b2002-08-15 19:37:43 +00001034 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001035
Misha Brukman21bbdb92004-06-04 21:11:51 +00001036 Out << ')';
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001037 return;
Chris Lattnerd84bb632002-04-16 21:36:08 +00001038 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001039
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001040 Out << "<placeholder or erroneous Constant>";
Chris Lattnerd84bb632002-04-16 21:36:08 +00001041}
1042
Chris Lattnercdec5812009-12-31 02:31:59 +00001043static void WriteMDNodeBodyInternal(raw_ostream &Out, const MDNode *Node,
1044 TypePrinting *TypePrinter,
Dan Gohmana2489d12010-07-20 23:55:01 +00001045 SlotTracker *Machine,
1046 const Module *Context) {
Chris Lattnercdec5812009-12-31 02:31:59 +00001047 Out << "!{";
1048 for (unsigned mi = 0, me = Node->getNumOperands(); mi != me; ++mi) {
1049 const Value *V = Node->getOperand(mi);
1050 if (V == 0)
1051 Out << "null";
1052 else {
1053 TypePrinter->print(V->getType(), Out);
1054 Out << ' ';
Andrew Trickec4b6e72011-09-30 19:48:58 +00001055 WriteAsOperandInternal(Out, Node->getOperand(mi),
Dan Gohmana2489d12010-07-20 23:55:01 +00001056 TypePrinter, Machine, Context);
Chris Lattnercdec5812009-12-31 02:31:59 +00001057 }
1058 if (mi + 1 != me)
1059 Out << ", ";
1060 }
Andrew Trickec4b6e72011-09-30 19:48:58 +00001061
Chris Lattnercdec5812009-12-31 02:31:59 +00001062 Out << "}";
1063}
1064
Chandler Carruthd48cdbf2014-01-09 02:29:41 +00001065// Full implementation of printing a Value as an operand with support for
1066// TypePrinting, etc.
Dan Gohman12dad632009-08-12 20:56:03 +00001067static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
Dan Gohman8061d9e2009-08-13 15:27:57 +00001068 TypePrinting *TypePrinter,
Dan Gohmana2489d12010-07-20 23:55:01 +00001069 SlotTracker *Machine,
1070 const Module *Context) {
Chris Lattner033935d2008-08-17 04:40:13 +00001071 if (V->hasName()) {
1072 PrintLLVMName(Out, V);
1073 return;
1074 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001075
Chris Lattner033935d2008-08-17 04:40:13 +00001076 const Constant *CV = dyn_cast<Constant>(V);
1077 if (CV && !isa<GlobalValue>(CV)) {
Dan Gohman8061d9e2009-08-13 15:27:57 +00001078 assert(TypePrinter && "Constants require TypePrinting!");
Dan Gohmana2489d12010-07-20 23:55:01 +00001079 WriteConstantInternal(Out, CV, *TypePrinter, Machine, Context);
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001080 return;
1081 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001082
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001083 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
Chris Lattner033935d2008-08-17 04:40:13 +00001084 Out << "asm ";
1085 if (IA->hasSideEffects())
1086 Out << "sideeffect ";
Dale Johannesen1cfb9582009-10-21 23:28:00 +00001087 if (IA->isAlignStack())
1088 Out << "alignstack ";
Chad Rosierd8c76102012-09-05 19:00:49 +00001089 // We don't emit the AD_ATT dialect as it's the assumed default.
1090 if (IA->getDialect() == InlineAsm::AD_Intel)
1091 Out << "inteldialect ";
Chris Lattner033935d2008-08-17 04:40:13 +00001092 Out << '"';
1093 PrintEscapedString(IA->getAsmString(), Out);
1094 Out << "\", \"";
1095 PrintEscapedString(IA->getConstraintString(), Out);
1096 Out << '"';
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001097 return;
1098 }
Devang Patel7428d8a2009-07-22 17:43:22 +00001099
Devang Patele059ba6e2009-07-23 01:07:34 +00001100 if (const MDNode *N = dyn_cast<MDNode>(V)) {
Victor Hernandez0471abd2009-12-18 20:09:14 +00001101 if (N->isFunctionLocal()) {
Victor Hernandezb7176a12009-12-04 01:35:02 +00001102 // Print metadata inline, not via slot reference number.
Dan Gohmana2489d12010-07-20 23:55:01 +00001103 WriteMDNodeBodyInternal(Out, N, TypePrinter, Machine, Context);
Victor Hernandezb7176a12009-12-04 01:35:02 +00001104 return;
1105 }
Andrew Trickec4b6e72011-09-30 19:48:58 +00001106
Dan Gohmana2489d12010-07-20 23:55:01 +00001107 if (!Machine) {
1108 if (N->isFunctionLocal())
1109 Machine = new SlotTracker(N->getFunction());
1110 else
1111 Machine = new SlotTracker(Context);
1112 }
Dan Gohman0a54da22010-09-09 20:53:58 +00001113 int Slot = Machine->getMetadataSlot(N);
1114 if (Slot == -1)
1115 Out << "<badref>";
1116 else
1117 Out << '!' << Slot;
Devang Patele059ba6e2009-07-23 01:07:34 +00001118 return;
1119 }
1120
Devang Patel7428d8a2009-07-22 17:43:22 +00001121 if (const MDString *MDS = dyn_cast<MDString>(V)) {
Devang Patel7428d8a2009-07-22 17:43:22 +00001122 Out << "!\"";
Daniel Dunbare03eecb2009-07-25 23:55:21 +00001123 PrintEscapedString(MDS->getString(), Out);
Devang Patel7428d8a2009-07-22 17:43:22 +00001124 Out << '"';
1125 return;
1126 }
1127
Evan Cheng00e87d12009-11-16 07:10:36 +00001128 if (V->getValueID() == Value::PseudoSourceValueVal ||
1129 V->getValueID() == Value::FixedStackPseudoSourceValueVal) {
Dan Gohmanc0353bf2009-09-23 01:33:16 +00001130 V->print(Out);
1131 return;
1132 }
1133
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001134 char Prefix = '%';
1135 int Slot;
Chris Lattner5b82a0a2011-08-03 06:15:41 +00001136 // If we have a SlotTracker, use it.
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001137 if (Machine) {
1138 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1139 Slot = Machine->getGlobalSlot(GV);
1140 Prefix = '@';
1141 } else {
1142 Slot = Machine->getLocalSlot(V);
Andrew Trickec4b6e72011-09-30 19:48:58 +00001143
Chris Lattner5b82a0a2011-08-03 06:15:41 +00001144 // If the local value didn't succeed, then we may be referring to a value
1145 // from a different function. Translate it, as this can happen when using
1146 // address of blocks.
1147 if (Slot == -1)
1148 if ((Machine = createSlotTracker(V))) {
1149 Slot = Machine->getLocalSlot(V);
1150 delete Machine;
1151 }
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001152 }
Chris Lattner5b82a0a2011-08-03 06:15:41 +00001153 } else if ((Machine = createSlotTracker(V))) {
1154 // Otherwise, create one to get the # and then destroy it.
1155 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1156 Slot = Machine->getGlobalSlot(GV);
1157 Prefix = '@';
Chris Lattnera2d810d2006-01-25 22:26:05 +00001158 } else {
Chris Lattner5b82a0a2011-08-03 06:15:41 +00001159 Slot = Machine->getLocalSlot(V);
Chris Lattnerd84bb632002-04-16 21:36:08 +00001160 }
Chris Lattner5b82a0a2011-08-03 06:15:41 +00001161 delete Machine;
1162 Machine = 0;
1163 } else {
1164 Slot = -1;
Chris Lattnerd84bb632002-04-16 21:36:08 +00001165 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001166
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001167 if (Slot != -1)
1168 Out << Prefix << Slot;
1169 else
1170 Out << "<badref>";
Chris Lattnerd84bb632002-04-16 21:36:08 +00001171}
1172
Daniel Maleaded9f932013-05-08 20:38:31 +00001173void AssemblyWriter::init() {
1174 if (TheModule)
1175 TypePrinter.incorporateTypes(*TheModule);
1176}
Chris Lattner2e9fee42001-07-12 23:35:26 +00001177
Andrew Trickec4b6e72011-09-30 19:48:58 +00001178
Daniel Maleaded9f932013-05-08 20:38:31 +00001179AssemblyWriter::AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac,
1180 const Module *M,
1181 AssemblyAnnotationWriter *AAW)
1182 : Out(o), TheModule(M), Machine(Mac), AnnotationWriter(AAW) {
1183 init();
1184}
Chris Lattner2f7c9632001-06-06 20:29:01 +00001185
Daniel Maleaded9f932013-05-08 20:38:31 +00001186AssemblyWriter::AssemblyWriter(formatted_raw_ostream &o, const Module *M,
1187 AssemblyAnnotationWriter *AAW)
1188 : Out(o), TheModule(M), ModuleSlotTracker(createSlotTracker(M)),
1189 Machine(*ModuleSlotTracker), AnnotationWriter(AAW) {
1190 init();
1191}
Andrew Trickec4b6e72011-09-30 19:48:58 +00001192
Daniel Maleaded9f932013-05-08 20:38:31 +00001193AssemblyWriter::~AssemblyWriter() { }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001194
Chris Lattner78e2e8b2006-12-06 06:24:27 +00001195void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType) {
1196 if (Operand == 0) {
Chris Lattner08f7d0c2005-02-24 16:58:29 +00001197 Out << "<null operand!>";
Chris Lattnerb419a1e2009-12-31 02:33:14 +00001198 return;
Chris Lattner08f7d0c2005-02-24 16:58:29 +00001199 }
Chris Lattnerb419a1e2009-12-31 02:33:14 +00001200 if (PrintType) {
1201 TypePrinter.print(Operand->getType(), Out);
1202 Out << ' ';
1203 }
Dan Gohmana2489d12010-07-20 23:55:01 +00001204 WriteAsOperandInternal(Out, Operand, &TypePrinter, &Machine, TheModule);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001205}
1206
Eli Friedmanfee02c62011-07-25 23:16:38 +00001207void AssemblyWriter::writeAtomic(AtomicOrdering Ordering,
1208 SynchronizationScope SynchScope) {
1209 if (Ordering == NotAtomic)
1210 return;
1211
1212 switch (SynchScope) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001213 case SingleThread: Out << " singlethread"; break;
1214 case CrossThread: break;
1215 }
1216
1217 switch (Ordering) {
1218 default: Out << " <bad ordering " << int(Ordering) << ">"; break;
1219 case Unordered: Out << " unordered"; break;
1220 case Monotonic: Out << " monotonic"; break;
1221 case Acquire: Out << " acquire"; break;
1222 case Release: Out << " release"; break;
1223 case AcquireRelease: Out << " acq_rel"; break;
1224 case SequentiallyConsistent: Out << " seq_cst"; break;
1225 }
1226}
1227
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001228void AssemblyWriter::writeParamOperand(const Value *Operand,
Bill Wendling749a43d2012-12-30 13:50:49 +00001229 AttributeSet Attrs, unsigned Idx) {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00001230 if (Operand == 0) {
1231 Out << "<null operand!>";
Chris Lattnerb419a1e2009-12-31 02:33:14 +00001232 return;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00001233 }
Chris Lattnerb419a1e2009-12-31 02:33:14 +00001234
1235 // Print the type
1236 TypePrinter.print(Operand->getType(), Out);
1237 // Print parameter attributes list
Bill Wendling749a43d2012-12-30 13:50:49 +00001238 if (Attrs.hasAttributes(Idx))
1239 Out << ' ' << Attrs.getAsString(Idx);
Chris Lattnerb419a1e2009-12-31 02:33:14 +00001240 Out << ' ';
1241 // Print the operand
Dan Gohmana2489d12010-07-20 23:55:01 +00001242 WriteAsOperandInternal(Out, Operand, &TypePrinter, &Machine, TheModule);
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00001243}
Chris Lattner2f7c9632001-06-06 20:29:01 +00001244
Chris Lattner7bfee412001-10-29 16:05:51 +00001245void AssemblyWriter::printModule(const Module *M) {
Bill Wendling829b4782013-02-11 08:43:33 +00001246 Machine.initialize();
1247
Chris Lattner4d8689e2005-03-02 23:12:40 +00001248 if (!M->getModuleIdentifier().empty() &&
Misha Brukmanb1c93172005-04-21 23:48:37 +00001249 // Don't print the ID if it will start a new line (which would
Chris Lattner4d8689e2005-03-02 23:12:40 +00001250 // require a comment char before it).
1251 M->getModuleIdentifier().find('\n') == std::string::npos)
1252 Out << "; ModuleID = '" << M->getModuleIdentifier() << "'\n";
1253
Owen Andersone2237542006-10-18 02:21:12 +00001254 if (!M->getDataLayout().empty())
Chris Lattner04897162006-10-22 06:06:56 +00001255 Out << "target datalayout = \"" << M->getDataLayout() << "\"\n";
Reid Spencer48f98c82004-07-25 21:44:54 +00001256 if (!M->getTargetTriple().empty())
Reid Spencerffec7df2004-07-25 21:29:43 +00001257 Out << "target triple = \"" << M->getTargetTriple() << "\"\n";
Misha Brukmanb1c93172005-04-21 23:48:37 +00001258
Chris Lattnereef2fe72006-01-24 04:13:11 +00001259 if (!M->getModuleInlineAsm().empty()) {
Chris Lattnerefaf35d2006-01-24 00:45:30 +00001260 // Split the string into lines, to make it easier to read the .ll file.
Chris Lattnereef2fe72006-01-24 04:13:11 +00001261 std::string Asm = M->getModuleInlineAsm();
Chris Lattnerefaf35d2006-01-24 00:45:30 +00001262 size_t CurPos = 0;
1263 size_t NewLine = Asm.find_first_of('\n', CurPos);
Dan Gohman69273e62009-08-12 23:54:22 +00001264 Out << '\n';
Chris Lattnerefaf35d2006-01-24 00:45:30 +00001265 while (NewLine != std::string::npos) {
1266 // We found a newline, print the portion of the asm string from the
1267 // last newline up to this newline.
1268 Out << "module asm \"";
1269 PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.begin()+NewLine),
1270 Out);
1271 Out << "\"\n";
1272 CurPos = NewLine+1;
1273 NewLine = Asm.find_first_of('\n', CurPos);
1274 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +00001275 std::string rest(Asm.begin()+CurPos, Asm.end());
1276 if (!rest.empty()) {
1277 Out << "module asm \"";
1278 PrintEscapedString(rest, Out);
1279 Out << "\"\n";
1280 }
Chris Lattner6ed87bd2006-01-23 23:03:36 +00001281 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001282
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001283 printTypeIdentities();
Misha Brukmanb1c93172005-04-21 23:48:37 +00001284
Dan Gohman69273e62009-08-12 23:54:22 +00001285 // Output all globals.
1286 if (!M->global_empty()) Out << '\n';
Chris Lattner54932b02006-12-06 04:41:52 +00001287 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
Duncan Sands66fc0e62012-09-12 09:55:51 +00001288 I != E; ++I) {
1289 printGlobal(I); Out << '\n';
1290 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001291
Chris Lattnerd2747052007-04-26 02:24:10 +00001292 // Output all aliases.
1293 if (!M->alias_empty()) Out << "\n";
1294 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
1295 I != E; ++I)
1296 printAlias(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +00001297
Chris Lattner2cdd49d2004-09-14 05:06:58 +00001298 // Output all of the functions.
Chris Lattner113f4f42002-06-25 16:13:24 +00001299 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
1300 printFunction(I);
Devang Patel983c6b12009-07-08 21:44:25 +00001301
Bill Wendling829b4782013-02-11 08:43:33 +00001302 // Output all attribute groups.
Bill Wendling04945972013-04-29 23:48:06 +00001303 if (!Machine.as_empty()) {
Bill Wendling829b4782013-02-11 08:43:33 +00001304 Out << '\n';
1305 writeAllAttributeGroups();
1306 }
1307
Devang Patel23e68302009-07-29 22:04:47 +00001308 // Output named metadata.
Dan Gohman69273e62009-08-12 23:54:22 +00001309 if (!M->named_metadata_empty()) Out << '\n';
Andrew Trickec4b6e72011-09-30 19:48:58 +00001310
Devang Patel23e68302009-07-29 22:04:47 +00001311 for (Module::const_named_metadata_iterator I = M->named_metadata_begin(),
Chris Lattnerbddea6a2009-12-31 02:13:35 +00001312 E = M->named_metadata_end(); I != E; ++I)
Chris Lattner2c48c642009-12-31 01:54:05 +00001313 printNamedMDNode(I);
Devang Patel23e68302009-07-29 22:04:47 +00001314
1315 // Output metadata.
Chris Lattnercf4a76e2009-12-31 02:20:11 +00001316 if (!Machine.mdn_empty()) {
Chris Lattnerbddea6a2009-12-31 02:13:35 +00001317 Out << '\n';
1318 writeAllMDNodes();
1319 }
Chris Lattnerfee714f2001-09-07 16:36:04 +00001320}
1321
Chris Lattner2c48c642009-12-31 01:54:05 +00001322void AssemblyWriter::printNamedMDNode(const NamedMDNode *NMD) {
Nick Lewycky5bcbd732011-06-15 06:37:58 +00001323 Out << '!';
1324 StringRef Name = NMD->getName();
1325 if (Name.empty()) {
1326 Out << "<empty name> ";
1327 } else {
Guy Benyei83c74e92013-02-12 21:21:59 +00001328 if (isalpha(static_cast<unsigned char>(Name[0])) ||
1329 Name[0] == '-' || Name[0] == '$' ||
Nick Lewycky5bcbd732011-06-15 06:37:58 +00001330 Name[0] == '.' || Name[0] == '_')
1331 Out << Name[0];
1332 else
1333 Out << '\\' << hexdigit(Name[0] >> 4) << hexdigit(Name[0] & 0x0F);
1334 for (unsigned i = 1, e = Name.size(); i != e; ++i) {
1335 unsigned char C = Name[i];
Guy Benyei83c74e92013-02-12 21:21:59 +00001336 if (isalnum(static_cast<unsigned char>(C)) || C == '-' || C == '$' ||
1337 C == '.' || C == '_')
Nick Lewycky5bcbd732011-06-15 06:37:58 +00001338 Out << C;
1339 else
1340 Out << '\\' << hexdigit(C >> 4) << hexdigit(C & 0x0F);
1341 }
1342 }
1343 Out << " = !{";
Chris Lattner2c48c642009-12-31 01:54:05 +00001344 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1345 if (i) Out << ", ";
Dan Gohman0a54da22010-09-09 20:53:58 +00001346 int Slot = Machine.getMetadataSlot(NMD->getOperand(i));
1347 if (Slot == -1)
1348 Out << "<badref>";
1349 else
1350 Out << '!' << Slot;
Chris Lattner2c48c642009-12-31 01:54:05 +00001351 }
1352 Out << "}\n";
1353}
1354
1355
Dan Gohmane2745262009-08-12 17:23:50 +00001356static void PrintLinkage(GlobalValue::LinkageTypes LT,
1357 formatted_raw_ostream &Out) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001358 switch (LT) {
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001359 case GlobalValue::ExternalLinkage: break;
1360 case GlobalValue::PrivateLinkage: Out << "private "; break;
1361 case GlobalValue::LinkerPrivateLinkage: Out << "linker_private "; break;
Bill Wendling03bcd6e2010-07-01 21:55:59 +00001362 case GlobalValue::LinkerPrivateWeakLinkage:
1363 Out << "linker_private_weak ";
1364 break;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001365 case GlobalValue::InternalLinkage: Out << "internal "; break;
1366 case GlobalValue::LinkOnceAnyLinkage: Out << "linkonce "; break;
1367 case GlobalValue::LinkOnceODRLinkage: Out << "linkonce_odr "; break;
1368 case GlobalValue::WeakAnyLinkage: Out << "weak "; break;
1369 case GlobalValue::WeakODRLinkage: Out << "weak_odr "; break;
1370 case GlobalValue::CommonLinkage: Out << "common "; break;
1371 case GlobalValue::AppendingLinkage: Out << "appending "; break;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001372 case GlobalValue::ExternalWeakLinkage: Out << "extern_weak "; break;
Chris Lattner184f1be2009-04-13 05:44:34 +00001373 case GlobalValue::AvailableExternallyLinkage:
1374 Out << "available_externally ";
1375 break;
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001376 }
1377}
Duncan Sands12da8ce2009-03-07 15:45:40 +00001378
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001379
1380static void PrintVisibility(GlobalValue::VisibilityTypes Vis,
Dan Gohmane2745262009-08-12 17:23:50 +00001381 formatted_raw_ostream &Out) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001382 switch (Vis) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001383 case GlobalValue::DefaultVisibility: break;
1384 case GlobalValue::HiddenVisibility: Out << "hidden "; break;
1385 case GlobalValue::ProtectedVisibility: Out << "protected "; break;
1386 }
1387}
1388
Nico Rieck7157bb72014-01-14 15:22:47 +00001389static void PrintDLLStorageClass(GlobalValue::DLLStorageClassTypes SCT,
1390 formatted_raw_ostream &Out) {
1391 switch (SCT) {
1392 case GlobalValue::DefaultStorageClass: break;
1393 case GlobalValue::DLLImportStorageClass: Out << "dllimport "; break;
1394 case GlobalValue::DLLExportStorageClass: Out << "dllexport "; break;
1395 }
1396}
1397
Hans Wennborgcbe34b42012-06-23 11:37:03 +00001398static void PrintThreadLocalModel(GlobalVariable::ThreadLocalMode TLM,
1399 formatted_raw_ostream &Out) {
1400 switch (TLM) {
1401 case GlobalVariable::NotThreadLocal:
1402 break;
1403 case GlobalVariable::GeneralDynamicTLSModel:
1404 Out << "thread_local ";
1405 break;
1406 case GlobalVariable::LocalDynamicTLSModel:
1407 Out << "thread_local(localdynamic) ";
1408 break;
1409 case GlobalVariable::InitialExecTLSModel:
1410 Out << "thread_local(initialexec) ";
1411 break;
1412 case GlobalVariable::LocalExecTLSModel:
1413 Out << "thread_local(localexec) ";
1414 break;
1415 }
1416}
1417
Chris Lattner7bfee412001-10-29 16:05:51 +00001418void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Dan Gohman5ded1422010-01-29 23:12:36 +00001419 if (GV->isMaterializable())
1420 Out << "; Materializable\n";
1421
Dan Gohmana2489d12010-07-20 23:55:01 +00001422 WriteAsOperandInternal(Out, GV, &TypePrinter, &Machine, GV->getParent());
Dan Gohman466876b2009-08-12 23:32:33 +00001423 Out << " = ";
Chris Lattner37798642001-09-18 04:01:05 +00001424
Chris Lattner1508d3f2008-08-19 05:16:28 +00001425 if (!GV->hasInitializer() && GV->hasExternalLinkage())
1426 Out << "external ";
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001427
Chris Lattner1508d3f2008-08-19 05:16:28 +00001428 PrintLinkage(GV->getLinkage(), Out);
1429 PrintVisibility(GV->getVisibility(), Out);
Nico Rieck7157bb72014-01-14 15:22:47 +00001430 PrintDLLStorageClass(GV->getDLLStorageClass(), Out);
Hans Wennborgcbe34b42012-06-23 11:37:03 +00001431 PrintThreadLocalModel(GV->getThreadLocalMode(), Out);
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +00001432
Chris Lattnerac161bf2009-01-02 07:01:27 +00001433 if (unsigned AddressSpace = GV->getType()->getAddressSpace())
1434 Out << "addrspace(" << AddressSpace << ") ";
Rafael Espindola45e6c192011-01-08 16:42:36 +00001435 if (GV->hasUnnamedAddr()) Out << "unnamed_addr ";
Michael Gottesman27e7ef32013-02-05 05:57:38 +00001436 if (GV->isExternallyInitialized()) Out << "externally_initialized ";
Misha Brukmana6619a92004-06-21 21:53:56 +00001437 Out << (GV->isConstant() ? "constant " : "global ");
Chris Lattnere101c442009-02-28 21:26:53 +00001438 TypePrinter.print(GV->getType()->getElementType(), Out);
Chris Lattner37798642001-09-18 04:01:05 +00001439
Dan Gohman81313fd2008-09-14 17:21:12 +00001440 if (GV->hasInitializer()) {
1441 Out << ' ';
Devang Patel983c6b12009-07-08 21:44:25 +00001442 writeOperand(GV->getInitializer(), false);
Dan Gohman81313fd2008-09-14 17:21:12 +00001443 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001444
Chris Lattner9380b812010-07-07 23:16:37 +00001445 if (GV->hasSection()) {
1446 Out << ", section \"";
1447 PrintEscapedString(GV->getSection(), Out);
1448 Out << '"';
1449 }
Chris Lattner4b96c542005-11-12 00:10:19 +00001450 if (GV->getAlignment())
Chris Lattnerf8a974d2005-11-06 06:48:53 +00001451 Out << ", align " << GV->getAlignment();
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001452
Chris Lattner113f4f42002-06-25 16:13:24 +00001453 printInfoComment(*GV);
Chris Lattnerda975502001-09-10 07:58:01 +00001454}
1455
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001456void AssemblyWriter::printAlias(const GlobalAlias *GA) {
Dan Gohman5ded1422010-01-29 23:12:36 +00001457 if (GA->isMaterializable())
1458 Out << "; Materializable\n";
1459
Dale Johannesen83e468a2008-06-03 18:14:29 +00001460 // Don't crash when dumping partially built GA
1461 if (!GA->hasName())
1462 Out << "<<nameless>> = ";
Chris Lattner033935d2008-08-17 04:40:13 +00001463 else {
1464 PrintLLVMName(Out, GA);
1465 Out << " = ";
1466 }
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001467 PrintVisibility(GA->getVisibility(), Out);
Nico Rieck7157bb72014-01-14 15:22:47 +00001468 PrintDLLStorageClass(GA->getDLLStorageClass(), Out);
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001469
1470 Out << "alias ";
1471
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001472 PrintLinkage(GA->getLinkage(), Out);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001473
Anton Korobeynikov546ea7e2007-04-29 18:02:48 +00001474 const Constant *Aliasee = GA->getAliasee();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001475
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001476 if (Aliasee == 0) {
1477 TypePrinter.print(GA->getType(), Out);
1478 Out << " <<NULL ALIASEE>>";
Jay Foad92c19132011-08-01 12:48:54 +00001479 } else {
Jay Foad26db79d2011-08-01 12:29:14 +00001480 writeOperand(Aliasee, !isa<ConstantExpr>(Aliasee));
Jay Foad92c19132011-08-01 12:48:54 +00001481 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001482
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001483 printInfoComment(*GA);
Chris Lattner1508d3f2008-08-19 05:16:28 +00001484 Out << '\n';
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001485}
1486
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001487void AssemblyWriter::printTypeIdentities() {
1488 if (TypePrinter.NumberedTypes.empty() &&
1489 TypePrinter.NamedTypes.empty())
1490 return;
Andrew Trickec4b6e72011-09-30 19:48:58 +00001491
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001492 Out << '\n';
Andrew Trickec4b6e72011-09-30 19:48:58 +00001493
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001494 // We know all the numbers that each type is used and we know that it is a
1495 // dense assignment. Convert the map to an index table.
1496 std::vector<StructType*> NumberedTypes(TypePrinter.NumberedTypes.size());
Andrew Trickec4b6e72011-09-30 19:48:58 +00001497 for (DenseMap<StructType*, unsigned>::iterator I =
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001498 TypePrinter.NumberedTypes.begin(), E = TypePrinter.NumberedTypes.end();
1499 I != E; ++I) {
1500 assert(I->second < NumberedTypes.size() && "Didn't get a dense numbering?");
1501 NumberedTypes[I->second] = I->first;
1502 }
Andrew Trickec4b6e72011-09-30 19:48:58 +00001503
Chris Lattner3243ea12009-03-01 00:03:38 +00001504 // Emit all numbered types.
1505 for (unsigned i = 0, e = NumberedTypes.size(); i != e; ++i) {
Dan Gohman466876b2009-08-12 23:32:33 +00001506 Out << '%' << i << " = type ";
Andrew Trickec4b6e72011-09-30 19:48:58 +00001507
Chris Lattner3243ea12009-03-01 00:03:38 +00001508 // Make sure we print out at least one level of the type structure, so
1509 // that we do not get %2 = type %2
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001510 TypePrinter.printStructBody(NumberedTypes[i], Out);
Dan Gohman69273e62009-08-12 23:54:22 +00001511 Out << '\n';
Chris Lattner3243ea12009-03-01 00:03:38 +00001512 }
Andrew Trickec4b6e72011-09-30 19:48:58 +00001513
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001514 for (unsigned i = 0, e = TypePrinter.NamedTypes.size(); i != e; ++i) {
1515 PrintLLVMName(Out, TypePrinter.NamedTypes[i]->getName(), LocalPrefix);
Chris Lattner1508d3f2008-08-19 05:16:28 +00001516 Out << " = type ";
Reid Spencere7e96712004-05-25 08:53:40 +00001517
1518 // Make sure we print out at least one level of the type structure, so
1519 // that we do not get %FILE = type %FILE
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001520 TypePrinter.printStructBody(TypePrinter.NamedTypes[i], Out);
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001521 Out << '\n';
Reid Spencere7e96712004-05-25 08:53:40 +00001522 }
Reid Spencer32af9e82007-01-06 07:24:44 +00001523}
1524
Misha Brukmanc566ca362004-03-02 00:22:19 +00001525/// printFunction - Print all aspects of a function.
1526///
Chris Lattner113f4f42002-06-25 16:13:24 +00001527void AssemblyWriter::printFunction(const Function *F) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001528 // Print out the return type and name.
1529 Out << '\n';
Chris Lattner379a8d22003-04-16 20:28:45 +00001530
Misha Brukmana6619a92004-06-21 21:53:56 +00001531 if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001532
Dan Gohman5ded1422010-01-29 23:12:36 +00001533 if (F->isMaterializable())
1534 Out << "; Materializable\n";
1535
Bill Wendling847a5c32013-04-16 20:55:47 +00001536 const AttributeSet &Attrs = F->getAttributes();
Bill Wendling04945972013-04-29 23:48:06 +00001537 if (Attrs.hasAttributes(AttributeSet::FunctionIndex)) {
Bill Wendling847a5c32013-04-16 20:55:47 +00001538 AttributeSet AS = Attrs.getFnAttributes();
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +00001539 std::string AttrStr;
1540
1541 unsigned Idx = 0;
1542 for (unsigned E = AS.getNumSlots(); Idx != E; ++Idx)
1543 if (AS.getSlotIndex(Idx) == AttributeSet::FunctionIndex)
1544 break;
1545
1546 for (AttributeSet::iterator I = AS.begin(Idx), E = AS.end(Idx);
1547 I != E; ++I) {
1548 Attribute Attr = *I;
1549 if (!Attr.isStringAttribute()) {
1550 if (!AttrStr.empty()) AttrStr += ' ';
1551 AttrStr += Attr.getAsString();
1552 }
1553 }
1554
Bill Wendling847a5c32013-04-16 20:55:47 +00001555 if (!AttrStr.empty())
1556 Out << "; Function Attrs: " << AttrStr << '\n';
1557 }
1558
Reid Spencer5301e7c2007-01-30 20:08:39 +00001559 if (F->isDeclaration())
Chris Lattner10f03a62007-08-19 22:15:26 +00001560 Out << "declare ";
1561 else
Reid Spencer7ce2d2a2006-12-29 20:29:48 +00001562 Out << "define ";
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001563
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001564 PrintLinkage(F->getLinkage(), Out);
1565 PrintVisibility(F->getVisibility(), Out);
Nico Rieck7157bb72014-01-14 15:22:47 +00001566 PrintDLLStorageClass(F->getDLLStorageClass(), Out);
Chris Lattner379a8d22003-04-16 20:28:45 +00001567
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001568 // Print the calling convention.
Micah Villmow857186d2012-09-13 15:11:12 +00001569 if (F->getCallingConv() != CallingConv::C) {
1570 PrintCallingConv(F->getCallingConv(), Out);
1571 Out << " ";
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001572 }
1573
Chris Lattner229907c2011-07-18 04:54:35 +00001574 FunctionType *FT = F->getFunctionType();
Bill Wendling658d24d2013-01-18 21:53:16 +00001575 if (Attrs.hasAttributes(AttributeSet::ReturnIndex))
1576 Out << Attrs.getAsString(AttributeSet::ReturnIndex) << ' ';
Chris Lattnere101c442009-02-28 21:26:53 +00001577 TypePrinter.print(F->getReturnType(), Out);
Chris Lattner585297e82008-08-19 05:26:17 +00001578 Out << ' ';
Dan Gohmana2489d12010-07-20 23:55:01 +00001579 WriteAsOperandInternal(Out, F, &TypePrinter, &Machine, F->getParent());
Misha Brukmana6619a92004-06-21 21:53:56 +00001580 Out << '(';
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001581 Machine.incorporateFunction(F);
Chris Lattnerfee714f2001-09-07 16:36:04 +00001582
Chris Lattner7bfee412001-10-29 16:05:51 +00001583 // Loop over the arguments, printing them...
Chris Lattnerfee714f2001-09-07 16:36:04 +00001584
Reid Spencer8c4914c2006-12-31 05:24:50 +00001585 unsigned Idx = 1;
Chris Lattner82738fe2007-04-18 00:57:22 +00001586 if (!F->isDeclaration()) {
1587 // If this isn't a declaration, print the argument names as well.
1588 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
1589 I != E; ++I) {
1590 // Insert commas as we go... the first arg doesn't get a comma
1591 if (I != F->arg_begin()) Out << ", ";
Bill Wendling749a43d2012-12-30 13:50:49 +00001592 printArgument(I, Attrs, Idx);
Chris Lattner82738fe2007-04-18 00:57:22 +00001593 Idx++;
1594 }
1595 } else {
1596 // Otherwise, print the types from the function type.
1597 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
1598 // Insert commas as we go... the first arg doesn't get a comma
1599 if (i) Out << ", ";
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001600
Chris Lattner82738fe2007-04-18 00:57:22 +00001601 // Output type...
Chris Lattnere101c442009-02-28 21:26:53 +00001602 TypePrinter.print(FT->getParamType(i), Out);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001603
Bill Wendling749a43d2012-12-30 13:50:49 +00001604 if (Attrs.hasAttributes(i+1))
1605 Out << ' ' << Attrs.getAsString(i+1);
Chris Lattner82738fe2007-04-18 00:57:22 +00001606 }
Reid Spencer8c4914c2006-12-31 05:24:50 +00001607 }
Chris Lattnerfee714f2001-09-07 16:36:04 +00001608
1609 // Finish printing arguments...
Chris Lattner113f4f42002-06-25 16:13:24 +00001610 if (FT->isVarArg()) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001611 if (FT->getNumParams()) Out << ", ";
1612 Out << "..."; // Output varargs portion of signature!
Chris Lattnerfee714f2001-09-07 16:36:04 +00001613 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001614 Out << ')';
Rafael Espindola563eb4b2011-01-25 19:09:56 +00001615 if (F->hasUnnamedAddr())
1616 Out << " unnamed_addr";
Bill Wendling04945972013-04-29 23:48:06 +00001617 if (Attrs.hasAttributes(AttributeSet::FunctionIndex))
1618 Out << " #" << Machine.getAttributeGroupSlot(Attrs.getFnAttributes());
Chris Lattner9380b812010-07-07 23:16:37 +00001619 if (F->hasSection()) {
1620 Out << " section \"";
1621 PrintEscapedString(F->getSection(), Out);
1622 Out << '"';
1623 }
Chris Lattnerf8a974d2005-11-06 06:48:53 +00001624 if (F->getAlignment())
1625 Out << " align " << F->getAlignment();
Gordon Henriksend930f912008-08-17 18:44:35 +00001626 if (F->hasGC())
1627 Out << " gc \"" << F->getGC() << '"';
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00001628 if (F->hasPrefixData()) {
1629 Out << " prefix ";
1630 writeOperand(F->getPrefixData(), true);
1631 }
Reid Spencer5301e7c2007-01-30 20:08:39 +00001632 if (F->isDeclaration()) {
Chris Lattnerfb483622010-09-02 22:52:10 +00001633 Out << '\n';
Chris Lattnerb2f02e52002-05-06 03:00:40 +00001634 } else {
Chris Lattnerfb483622010-09-02 22:52:10 +00001635 Out << " {";
1636 // Output all of the function's basic blocks.
Chris Lattner113f4f42002-06-25 16:13:24 +00001637 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
1638 printBasicBlock(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +00001639
Misha Brukmana6619a92004-06-21 21:53:56 +00001640 Out << "}\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +00001641 }
1642
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001643 Machine.purgeFunction();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001644}
1645
Misha Brukmanc566ca362004-03-02 00:22:19 +00001646/// printArgument - This member is called for every argument that is passed into
1647/// the function. Simply print it out
1648///
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001649void AssemblyWriter::printArgument(const Argument *Arg,
Bill Wendling749a43d2012-12-30 13:50:49 +00001650 AttributeSet Attrs, unsigned Idx) {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001651 // Output type...
Chris Lattnere101c442009-02-28 21:26:53 +00001652 TypePrinter.print(Arg->getType(), Out);
Misha Brukmanb1c93172005-04-21 23:48:37 +00001653
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00001654 // Output parameter attributes list
Bill Wendling749a43d2012-12-30 13:50:49 +00001655 if (Attrs.hasAttributes(Idx))
1656 Out << ' ' << Attrs.getAsString(Idx);
Reid Spencer8c4914c2006-12-31 05:24:50 +00001657
Chris Lattner2f7c9632001-06-06 20:29:01 +00001658 // Output name, if available...
Chris Lattner033935d2008-08-17 04:40:13 +00001659 if (Arg->hasName()) {
1660 Out << ' ';
1661 PrintLLVMName(Out, Arg);
1662 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001663}
1664
Misha Brukmanc566ca362004-03-02 00:22:19 +00001665/// printBasicBlock - This member is called for each basic block in a method.
1666///
Chris Lattner7bfee412001-10-29 16:05:51 +00001667void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Nick Lewycky4d43d3c2008-04-25 16:53:59 +00001668 if (BB->hasName()) { // Print out the label if it exists...
Chris Lattner033935d2008-08-17 04:40:13 +00001669 Out << "\n";
Daniel Dunbare03eecb2009-07-25 23:55:21 +00001670 PrintLLVMName(Out, BB->getName(), LabelPrefix);
Chris Lattner033935d2008-08-17 04:40:13 +00001671 Out << ':';
Nick Lewycky4d43d3c2008-04-25 16:53:59 +00001672 } else if (!BB->use_empty()) { // Don't print block # of no uses...
Bill Wendling35a9c3c2011-04-10 23:18:04 +00001673 Out << "\n; <label>:";
Chris Lattner5e043322007-01-11 03:54:27 +00001674 int Slot = Machine.getLocalSlot(BB);
Chris Lattner757ee0b2004-06-09 19:41:19 +00001675 if (Slot != -1)
Misha Brukmana6619a92004-06-21 21:53:56 +00001676 Out << Slot;
Chris Lattner757ee0b2004-06-09 19:41:19 +00001677 else
Misha Brukmana6619a92004-06-21 21:53:56 +00001678 Out << "<badref>";
Chris Lattner58185f22002-10-02 19:38:55 +00001679 }
Chris Lattner2447ef52003-11-20 00:09:43 +00001680
Dan Gohmane2745262009-08-12 17:23:50 +00001681 if (BB->getParent() == 0) {
Chris Lattneree97b8b2009-08-17 15:48:08 +00001682 Out.PadToColumn(50);
Dan Gohmane2745262009-08-12 17:23:50 +00001683 Out << "; Error: Block without parent!";
1684 } else if (BB != &BB->getParent()->getEntryBlock()) { // Not the entry block?
Chris Lattnerfb483622010-09-02 22:52:10 +00001685 // Output predecessors for the block.
Chris Lattneree97b8b2009-08-17 15:48:08 +00001686 Out.PadToColumn(50);
Dan Gohmane2745262009-08-12 17:23:50 +00001687 Out << ";";
Gabor Greif6c6b2fd2010-03-25 23:25:28 +00001688 const_pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001689
Chris Lattnerff834c02008-04-22 02:45:44 +00001690 if (PI == PE) {
1691 Out << " No predecessors!";
1692 } else {
Dan Gohman81313fd2008-09-14 17:21:12 +00001693 Out << " preds = ";
Chris Lattnerff834c02008-04-22 02:45:44 +00001694 writeOperand(*PI, false);
1695 for (++PI; PI != PE; ++PI) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001696 Out << ", ";
Chris Lattner78e2e8b2006-12-06 06:24:27 +00001697 writeOperand(*PI, false);
Chris Lattner00211f12003-11-16 22:59:57 +00001698 }
Chris Lattner58185f22002-10-02 19:38:55 +00001699 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001700 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001701
Chris Lattnerff834c02008-04-22 02:45:44 +00001702 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001703
Misha Brukmana6619a92004-06-21 21:53:56 +00001704 if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001705
Chris Lattnerfee714f2001-09-07 16:36:04 +00001706 // Output all of the instructions in the basic block...
Dan Gohmana4f709e2009-07-13 18:27:59 +00001707 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
Daniel Maleaded9f932013-05-08 20:38:31 +00001708 printInstructionLine(*I);
Dan Gohmana4f709e2009-07-13 18:27:59 +00001709 }
Chris Lattner96cdd272004-03-08 18:51:45 +00001710
Misha Brukmana6619a92004-06-21 21:53:56 +00001711 if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, Out);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001712}
1713
Daniel Maleaded9f932013-05-08 20:38:31 +00001714/// printInstructionLine - Print an instruction and a newline character.
1715void AssemblyWriter::printInstructionLine(const Instruction &I) {
1716 printInstruction(I);
1717 Out << '\n';
1718}
1719
Misha Brukmanc566ca362004-03-02 00:22:19 +00001720/// printInfoComment - Print a little comment after the instruction indicating
1721/// which slot it occupies.
1722///
Chris Lattner113f4f42002-06-25 16:13:24 +00001723void AssemblyWriter::printInfoComment(const Value &V) {
Bill Wendling847a5c32013-04-16 20:55:47 +00001724 if (AnnotationWriter)
Dan Gohmane34890f2010-02-10 20:41:46 +00001725 AnnotationWriter->printInfoComment(V, Out);
Chris Lattner862e3382001-10-13 06:42:36 +00001726}
1727
Reid Spencere7141c82006-08-28 01:02:49 +00001728// This member is called for each Instruction in a function..
Chris Lattner113f4f42002-06-25 16:13:24 +00001729void AssemblyWriter::printInstruction(const Instruction &I) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001730 if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001731
Dan Gohman466876b2009-08-12 23:32:33 +00001732 // Print out indentation for an instruction.
Dan Gohmanb4583b12009-08-13 01:41:52 +00001733 Out << " ";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001734
1735 // Print out name if it exists...
Chris Lattner033935d2008-08-17 04:40:13 +00001736 if (I.hasName()) {
1737 PrintLLVMName(Out, &I);
1738 Out << " = ";
Chris Lattner031560c2009-12-29 07:25:48 +00001739 } else if (!I.getType()->isVoidTy()) {
Chris Lattnerb25e5ea2008-08-29 17:19:30 +00001740 // Print out the def slot taken.
1741 int SlotNum = Machine.getLocalSlot(&I);
1742 if (SlotNum == -1)
1743 Out << "<badref> = ";
1744 else
1745 Out << '%' << SlotNum << " = ";
Chris Lattner033935d2008-08-17 04:40:13 +00001746 }
Andrew Trickec4b6e72011-09-30 19:48:58 +00001747
Eli Friedman59b66882011-08-09 23:02:53 +00001748 if (isa<CallInst>(I) && cast<CallInst>(I).isTailCall())
Chris Lattner06038452005-05-06 05:51:46 +00001749 Out << "tail ";
Chris Lattner504f9242003-09-08 17:45:59 +00001750
Chris Lattner2f7c9632001-06-06 20:29:01 +00001751 // Print out the opcode...
Misha Brukmana6619a92004-06-21 21:53:56 +00001752 Out << I.getOpcodeName();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001753
Eli Friedman02e737b2011-08-12 22:50:01 +00001754 // If this is an atomic load or store, print out the atomic marker.
1755 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isAtomic()) ||
1756 (isa<StoreInst>(I) && cast<StoreInst>(I).isAtomic()))
1757 Out << " atomic";
1758
1759 // If this is a volatile operation, print out the volatile marker.
1760 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) ||
1761 (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile()) ||
1762 (isa<AtomicCmpXchgInst>(I) && cast<AtomicCmpXchgInst>(I).isVolatile()) ||
1763 (isa<AtomicRMWInst>(I) && cast<AtomicRMWInst>(I).isVolatile()))
1764 Out << " volatile";
1765
Dan Gohman9c7f8082009-07-27 16:11:46 +00001766 // Print out optimization information.
1767 WriteOptimizationInfo(Out, &I);
1768
Reid Spencer45e52392006-12-03 06:27:29 +00001769 // Print out the compare instruction predicates
Nate Begemand2195702008-05-12 19:01:56 +00001770 if (const CmpInst *CI = dyn_cast<CmpInst>(&I))
Chris Lattner82ff9232008-08-23 22:52:27 +00001771 Out << ' ' << getPredicateText(CI->getPredicate());
Reid Spencer45e52392006-12-03 06:27:29 +00001772
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001773 // Print out the atomicrmw operation
1774 if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(&I))
1775 writeAtomicRMWOperation(Out, RMWI->getOperation());
1776
Chris Lattner2f7c9632001-06-06 20:29:01 +00001777 // Print out the type of the operands...
Chris Lattner113f4f42002-06-25 16:13:24 +00001778 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001779
1780 // Special case conditional branches to swizzle the condition out to the front
Gabor Greifcab008f2009-02-09 15:45:06 +00001781 if (isa<BranchInst>(I) && cast<BranchInst>(I).isConditional()) {
David Blaikieb78e9e52013-02-11 01:16:51 +00001782 const BranchInst &BI(cast<BranchInst>(I));
Dan Gohman81313fd2008-09-14 17:21:12 +00001783 Out << ' ';
Gabor Greifcab008f2009-02-09 15:45:06 +00001784 writeOperand(BI.getCondition(), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00001785 Out << ", ";
Gabor Greifcab008f2009-02-09 15:45:06 +00001786 writeOperand(BI.getSuccessor(0), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00001787 Out << ", ";
Gabor Greifcab008f2009-02-09 15:45:06 +00001788 writeOperand(BI.getSuccessor(1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001789
Chris Lattner8d48df22002-04-13 18:34:38 +00001790 } else if (isa<SwitchInst>(I)) {
David Blaikieb78e9e52013-02-11 01:16:51 +00001791 const SwitchInst& SI(cast<SwitchInst>(I));
Chris Lattner3ed871f2009-10-27 19:13:16 +00001792 // Special case switch instruction to get formatting nice and correct.
Dan Gohman81313fd2008-09-14 17:21:12 +00001793 Out << ' ';
Eli Friedman95031ed2011-09-29 20:21:17 +00001794 writeOperand(SI.getCondition(), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00001795 Out << ", ";
Eli Friedman95031ed2011-09-29 20:21:17 +00001796 writeOperand(SI.getDefaultDest(), true);
Chris Lattner82ff9232008-08-23 22:52:27 +00001797 Out << " [";
David Blaikieb78e9e52013-02-11 01:16:51 +00001798 for (SwitchInst::ConstCaseIt i = SI.case_begin(), e = SI.case_end();
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00001799 i != e; ++i) {
Dan Gohmanb4583b12009-08-13 01:41:52 +00001800 Out << "\n ";
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00001801 writeOperand(i.getCaseValue(), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00001802 Out << ", ";
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00001803 writeOperand(i.getCaseSuccessor(), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001804 }
Dan Gohmanb4583b12009-08-13 01:41:52 +00001805 Out << "\n ]";
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00001806 } else if (isa<IndirectBrInst>(I)) {
1807 // Special case indirectbr instruction to get formatting nice and correct.
Chris Lattner3ed871f2009-10-27 19:13:16 +00001808 Out << ' ';
1809 writeOperand(Operand, true);
Dan Gohman43c57402009-10-30 02:01:10 +00001810 Out << ", [";
Andrew Trickec4b6e72011-09-30 19:48:58 +00001811
Chris Lattner3ed871f2009-10-27 19:13:16 +00001812 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) {
1813 if (i != 1)
1814 Out << ", ";
1815 writeOperand(I.getOperand(i), true);
1816 }
1817 Out << ']';
Jay Foad372ad642011-06-20 14:18:48 +00001818 } else if (const PHINode *PN = dyn_cast<PHINode>(&I)) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001819 Out << ' ';
Chris Lattnere101c442009-02-28 21:26:53 +00001820 TypePrinter.print(I.getType(), Out);
Misha Brukmana6619a92004-06-21 21:53:56 +00001821 Out << ' ';
Chris Lattner2f7c9632001-06-06 20:29:01 +00001822
Jay Foad372ad642011-06-20 14:18:48 +00001823 for (unsigned op = 0, Eop = PN->getNumIncomingValues(); op < Eop; ++op) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001824 if (op) Out << ", ";
Dan Gohman81313fd2008-09-14 17:21:12 +00001825 Out << "[ ";
Jay Foad372ad642011-06-20 14:18:48 +00001826 writeOperand(PN->getIncomingValue(op), false); Out << ", ";
1827 writeOperand(PN->getIncomingBlock(op), false); Out << " ]";
Chris Lattner931ef3b2001-06-11 15:04:20 +00001828 }
Dan Gohmana76f0f72008-05-31 19:12:39 +00001829 } else if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(&I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001830 Out << ' ';
Dan Gohmana76f0f72008-05-31 19:12:39 +00001831 writeOperand(I.getOperand(0), true);
1832 for (const unsigned *i = EVI->idx_begin(), *e = EVI->idx_end(); i != e; ++i)
1833 Out << ", " << *i;
1834 } else if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(&I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001835 Out << ' ';
1836 writeOperand(I.getOperand(0), true); Out << ", ";
Dan Gohmana76f0f72008-05-31 19:12:39 +00001837 writeOperand(I.getOperand(1), true);
1838 for (const unsigned *i = IVI->idx_begin(), *e = IVI->idx_end(); i != e; ++i)
1839 Out << ", " << *i;
Bill Wendlingfae14752011-08-12 20:24:12 +00001840 } else if (const LandingPadInst *LPI = dyn_cast<LandingPadInst>(&I)) {
1841 Out << ' ';
1842 TypePrinter.print(I.getType(), Out);
1843 Out << " personality ";
1844 writeOperand(I.getOperand(0), true); Out << '\n';
1845
1846 if (LPI->isCleanup())
1847 Out << " cleanup";
1848
1849 for (unsigned i = 0, e = LPI->getNumClauses(); i != e; ++i) {
1850 if (i != 0 || LPI->isCleanup()) Out << "\n";
1851 if (LPI->isCatch(i))
1852 Out << " catch ";
1853 else
1854 Out << " filter ";
1855
1856 writeOperand(LPI->getClause(i), true);
1857 }
Devang Patel59643e52008-02-23 00:35:18 +00001858 } else if (isa<ReturnInst>(I) && !Operand) {
1859 Out << " void";
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001860 } else if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
1861 // Print the calling convention being used.
Micah Villmow857186d2012-09-13 15:11:12 +00001862 if (CI->getCallingConv() != CallingConv::C) {
1863 Out << " ";
1864 PrintCallingConv(CI->getCallingConv(), Out);
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001865 }
1866
Gabor Greifc9a92512010-06-23 13:09:06 +00001867 Operand = CI->getCalledValue();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001868 PointerType *PTy = cast<PointerType>(Operand->getType());
1869 FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1870 Type *RetTy = FTy->getReturnType();
Bill Wendlinge94d8432012-12-07 23:16:57 +00001871 const AttributeSet &PAL = CI->getAttributes();
Chris Lattner2f2d9472001-11-06 21:28:12 +00001872
Bill Wendling658d24d2013-01-18 21:53:16 +00001873 if (PAL.hasAttributes(AttributeSet::ReturnIndex))
1874 Out << ' ' << PAL.getAsString(AttributeSet::ReturnIndex);
Devang Patel221fe422008-09-29 20:49:50 +00001875
Chris Lattner463d6a52003-08-05 15:34:45 +00001876 // If possible, print out the short form of the call instruction. We can
Chris Lattner6915f8f2002-04-07 22:49:37 +00001877 // only do this if the first argument is a pointer to a nonvararg function,
Chris Lattner463d6a52003-08-05 15:34:45 +00001878 // and if the return type is not a pointer to a function.
Chris Lattner2f2d9472001-11-06 21:28:12 +00001879 //
Dan Gohman81313fd2008-09-14 17:21:12 +00001880 Out << ' ';
Chris Lattner463d6a52003-08-05 15:34:45 +00001881 if (!FTy->isVarArg() &&
Duncan Sands19d0b472010-02-16 11:11:14 +00001882 (!RetTy->isPointerTy() ||
1883 !cast<PointerType>(RetTy)->getElementType()->isFunctionTy())) {
Chris Lattnere101c442009-02-28 21:26:53 +00001884 TypePrinter.print(RetTy, Out);
Dan Gohman81313fd2008-09-14 17:21:12 +00001885 Out << ' ';
Chris Lattner2f2d9472001-11-06 21:28:12 +00001886 writeOperand(Operand, false);
1887 } else {
1888 writeOperand(Operand, true);
1889 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001890 Out << '(';
Gabor Greifc9a92512010-06-23 13:09:06 +00001891 for (unsigned op = 0, Eop = CI->getNumArgOperands(); op < Eop; ++op) {
1892 if (op > 0)
Dan Gohman81313fd2008-09-14 17:21:12 +00001893 Out << ", ";
Bill Wendling749a43d2012-12-30 13:50:49 +00001894 writeParamOperand(CI->getArgOperand(op), PAL, op + 1);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001895 }
Dan Gohman81313fd2008-09-14 17:21:12 +00001896 Out << ')';
Bill Wendling698e84f2012-12-30 10:32:01 +00001897 if (PAL.hasAttributes(AttributeSet::FunctionIndex))
Bill Wendlinga0323742013-02-22 09:09:42 +00001898 Out << " #" << Machine.getAttributeGroupSlot(PAL.getFnAttributes());
Chris Lattner113f4f42002-06-25 16:13:24 +00001899 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
Gabor Greifa2fbc0a2010-03-24 13:21:49 +00001900 Operand = II->getCalledValue();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001901 PointerType *PTy = cast<PointerType>(Operand->getType());
1902 FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1903 Type *RetTy = FTy->getReturnType();
Bill Wendlinge94d8432012-12-07 23:16:57 +00001904 const AttributeSet &PAL = II->getAttributes();
Chris Lattner463d6a52003-08-05 15:34:45 +00001905
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001906 // Print the calling convention being used.
Micah Villmow857186d2012-09-13 15:11:12 +00001907 if (II->getCallingConv() != CallingConv::C) {
1908 Out << " ";
1909 PrintCallingConv(II->getCallingConv(), Out);
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001910 }
1911
Bill Wendling658d24d2013-01-18 21:53:16 +00001912 if (PAL.hasAttributes(AttributeSet::ReturnIndex))
1913 Out << ' ' << PAL.getAsString(AttributeSet::ReturnIndex);
Devang Patel221fe422008-09-29 20:49:50 +00001914
Chris Lattner463d6a52003-08-05 15:34:45 +00001915 // If possible, print out the short form of the invoke instruction. We can
1916 // only do this if the first argument is a pointer to a nonvararg function,
1917 // and if the return type is not a pointer to a function.
1918 //
Dan Gohmanc7e00ba2008-10-15 18:02:08 +00001919 Out << ' ';
Chris Lattner463d6a52003-08-05 15:34:45 +00001920 if (!FTy->isVarArg() &&
Duncan Sands19d0b472010-02-16 11:11:14 +00001921 (!RetTy->isPointerTy() ||
1922 !cast<PointerType>(RetTy)->getElementType()->isFunctionTy())) {
Chris Lattnere101c442009-02-28 21:26:53 +00001923 TypePrinter.print(RetTy, Out);
Dan Gohmanc7e00ba2008-10-15 18:02:08 +00001924 Out << ' ';
Chris Lattner463d6a52003-08-05 15:34:45 +00001925 writeOperand(Operand, false);
1926 } else {
1927 writeOperand(Operand, true);
1928 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001929 Out << '(';
Gabor Greifc9a92512010-06-23 13:09:06 +00001930 for (unsigned op = 0, Eop = II->getNumArgOperands(); op < Eop; ++op) {
Gabor Greifa2fbc0a2010-03-24 13:21:49 +00001931 if (op)
Dan Gohman81313fd2008-09-14 17:21:12 +00001932 Out << ", ";
Bill Wendling749a43d2012-12-30 13:50:49 +00001933 writeParamOperand(II->getArgOperand(op), PAL, op + 1);
Chris Lattner862e3382001-10-13 06:42:36 +00001934 }
1935
Dan Gohman81313fd2008-09-14 17:21:12 +00001936 Out << ')';
Bill Wendling698e84f2012-12-30 10:32:01 +00001937 if (PAL.hasAttributes(AttributeSet::FunctionIndex))
Bill Wendlinga0323742013-02-22 09:09:42 +00001938 Out << " #" << Machine.getAttributeGroupSlot(PAL.getFnAttributes());
Devang Patela05633e2008-09-26 22:53:05 +00001939
Dan Gohmanb4583b12009-08-13 01:41:52 +00001940 Out << "\n to ";
Chris Lattner862e3382001-10-13 06:42:36 +00001941 writeOperand(II->getNormalDest(), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00001942 Out << " unwind ";
Chris Lattnerfae8ab32004-02-08 21:44:31 +00001943 writeOperand(II->getUnwindDest(), true);
Chris Lattner862e3382001-10-13 06:42:36 +00001944
Victor Hernandez8acf2952009-10-23 21:09:37 +00001945 } else if (const AllocaInst *AI = dyn_cast<AllocaInst>(&I)) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001946 Out << ' ';
Dan Gohmanb2f426c2013-02-08 22:01:47 +00001947 TypePrinter.print(AI->getAllocatedType(), Out);
Reid Kleckner20844032014-01-25 01:24:06 +00001948 if (AI->isUsedWithInAlloca())
1949 Out << ", inalloca";
Dan Gohmanb53e26c2009-07-31 18:23:24 +00001950 if (!AI->getArraySize() || AI->isArrayAllocation()) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001951 Out << ", ";
Chris Lattner8d48df22002-04-13 18:34:38 +00001952 writeOperand(AI->getArraySize(), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001953 }
Nate Begeman848622f2005-11-05 09:21:28 +00001954 if (AI->getAlignment()) {
Chris Lattner7aeee3a2005-11-05 21:20:34 +00001955 Out << ", align " << AI->getAlignment();
Nate Begeman848622f2005-11-05 09:21:28 +00001956 }
Chris Lattner862e3382001-10-13 06:42:36 +00001957 } else if (isa<CastInst>(I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001958 if (Operand) {
1959 Out << ' ';
1960 writeOperand(Operand, true); // Work with broken code
1961 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001962 Out << " to ";
Chris Lattnere101c442009-02-28 21:26:53 +00001963 TypePrinter.print(I.getType(), Out);
Chris Lattner5b337482003-10-18 05:57:43 +00001964 } else if (isa<VAArgInst>(I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001965 if (Operand) {
1966 Out << ' ';
1967 writeOperand(Operand, true); // Work with broken code
1968 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001969 Out << ", ";
Chris Lattnere101c442009-02-28 21:26:53 +00001970 TypePrinter.print(I.getType(), Out);
1971 } else if (Operand) { // Print the normal way.
Chris Lattner2f7c9632001-06-06 20:29:01 +00001972
Misha Brukmanb1c93172005-04-21 23:48:37 +00001973 // PrintAllTypes - Instructions who have operands of all the same type
Chris Lattner2f7c9632001-06-06 20:29:01 +00001974 // omit the type from all but the first operand. If the instruction has
1975 // different type operands (for example br), then they are all printed.
1976 bool PrintAllTypes = false;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001977 Type *TheType = Operand->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001978
Reid Spencer0cdd04f2007-02-02 13:54:55 +00001979 // Select, Store and ShuffleVector always print all types.
Devang Patelce556d92008-03-04 22:05:14 +00001980 if (isa<SelectInst>(I) || isa<StoreInst>(I) || isa<ShuffleVectorInst>(I)
1981 || isa<ReturnInst>(I)) {
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00001982 PrintAllTypes = true;
1983 } else {
1984 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
1985 Operand = I.getOperand(i);
Nuno Lopes04fe2f02009-01-15 18:40:57 +00001986 // note that Operand shouldn't be null, but the test helps make dump()
1987 // more tolerant of malformed IR
Nuno Lopes0971e772009-01-14 17:51:41 +00001988 if (Operand && Operand->getType() != TheType) {
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00001989 PrintAllTypes = true; // We have differing types! Print them all!
1990 break;
1991 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001992 }
1993 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001994
Chris Lattner7bfee412001-10-29 16:05:51 +00001995 if (!PrintAllTypes) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001996 Out << ' ';
Chris Lattnere101c442009-02-28 21:26:53 +00001997 TypePrinter.print(TheType, Out);
Chris Lattner7bfee412001-10-29 16:05:51 +00001998 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001999
Dan Gohman81313fd2008-09-14 17:21:12 +00002000 Out << ' ';
Chris Lattner113f4f42002-06-25 16:13:24 +00002001 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
Dan Gohman81313fd2008-09-14 17:21:12 +00002002 if (i) Out << ", ";
Chris Lattner113f4f42002-06-25 16:13:24 +00002003 writeOperand(I.getOperand(i), PrintAllTypes);
Chris Lattner2f7c9632001-06-06 20:29:01 +00002004 }
2005 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002006
Eli Friedman59b66882011-08-09 23:02:53 +00002007 // Print atomic ordering/alignment for memory operations
2008 if (const LoadInst *LI = dyn_cast<LoadInst>(&I)) {
2009 if (LI->isAtomic())
2010 writeAtomic(LI->getOrdering(), LI->getSynchScope());
2011 if (LI->getAlignment())
2012 Out << ", align " << LI->getAlignment();
2013 } else if (const StoreInst *SI = dyn_cast<StoreInst>(&I)) {
2014 if (SI->isAtomic())
2015 writeAtomic(SI->getOrdering(), SI->getSynchScope());
2016 if (SI->getAlignment())
2017 Out << ", align " << SI->getAlignment();
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002018 } else if (const AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(&I)) {
2019 writeAtomic(CXI->getOrdering(), CXI->getSynchScope());
2020 } else if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(&I)) {
2021 writeAtomic(RMWI->getOrdering(), RMWI->getSynchScope());
Eli Friedmanfee02c62011-07-25 23:16:38 +00002022 } else if (const FenceInst *FI = dyn_cast<FenceInst>(&I)) {
2023 writeAtomic(FI->getOrdering(), FI->getSynchScope());
Christopher Lamb84485702007-04-22 19:24:39 +00002024 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00002025
Chris Lattnerc9558df2009-12-28 20:10:43 +00002026 // Print Metadata info.
Nick Lewyckyba8ec9a2010-02-25 06:53:04 +00002027 SmallVector<std::pair<unsigned, MDNode*>, 4> InstMD;
2028 I.getAllMetadata(InstMD);
Erick Tryzelaar72a37132010-03-02 05:32:52 +00002029 if (!InstMD.empty()) {
2030 SmallVector<StringRef, 8> MDNames;
2031 I.getType()->getContext().getMDKindNames(MDNames);
2032 for (unsigned i = 0, e = InstMD.size(); i != e; ++i) {
2033 unsigned Kind = InstMD[i].first;
2034 if (Kind < MDNames.size()) {
2035 Out << ", !" << MDNames[Kind];
Daniel Maleaded9f932013-05-08 20:38:31 +00002036 } else {
2037 Out << ", !<unknown kind #" << Kind << ">";
2038 }
Dan Gohmana2489d12010-07-20 23:55:01 +00002039 Out << ' ';
2040 WriteAsOperandInternal(Out, InstMD[i].second, &TypePrinter, &Machine,
2041 TheModule);
Nick Lewyckyba8ec9a2010-02-25 06:53:04 +00002042 }
Devang Patelbcdb0252009-10-07 16:37:55 +00002043 }
Chris Lattner862e3382001-10-13 06:42:36 +00002044 printInfoComment(I);
Chris Lattner2f7c9632001-06-06 20:29:01 +00002045}
2046
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002047static void WriteMDNodeComment(const MDNode *Node,
Duncan Sands41b4a6b2010-07-12 08:16:59 +00002048 formatted_raw_ostream &Out) {
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002049 if (Node->getNumOperands() < 1)
2050 return;
Bill Wendling5cb50c52012-06-28 00:41:44 +00002051
2052 Value *Op = Node->getOperand(0);
2053 if (!Op || !isa<ConstantInt>(Op) || cast<ConstantInt>(Op)->getBitWidth() < 32)
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002054 return;
Andrew Trickec4b6e72011-09-30 19:48:58 +00002055
Bill Wendling5cb50c52012-06-28 00:41:44 +00002056 DIDescriptor Desc(Node);
David Blaikiedc69ebb2013-03-11 23:39:23 +00002057 if (!Desc.Verify())
Bill Wendling5cb50c52012-06-28 00:41:44 +00002058 return;
2059
2060 unsigned Tag = Desc.getTag();
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002061 Out.PadToColumn(50);
Bill Wendlinga0bc1082012-07-03 20:01:02 +00002062 if (dwarf::TagString(Tag)) {
2063 Out << "; ";
2064 Desc.print(Out);
2065 } else if (Tag == dwarf::DW_TAG_user_base) {
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002066 Out << "; [ DW_TAG_user_base ]";
Bill Wendlinga0bc1082012-07-03 20:01:02 +00002067 }
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002068}
2069
Daniel Maleaded9f932013-05-08 20:38:31 +00002070void AssemblyWriter::writeMDNode(unsigned Slot, const MDNode *Node) {
2071 Out << '!' << Slot << " = metadata ";
2072 printMDNodeBody(Node);
2073}
2074
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002075void AssemblyWriter::writeAllMDNodes() {
2076 SmallVector<const MDNode *, 16> Nodes;
Chris Lattnercf4a76e2009-12-31 02:20:11 +00002077 Nodes.resize(Machine.mdn_size());
2078 for (SlotTracker::mdn_iterator I = Machine.mdn_begin(), E = Machine.mdn_end();
2079 I != E; ++I)
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002080 Nodes[I->second] = cast<MDNode>(I->first);
Andrew Trickec4b6e72011-09-30 19:48:58 +00002081
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002082 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Daniel Maleaded9f932013-05-08 20:38:31 +00002083 writeMDNode(i, Nodes[i]);
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002084 }
2085}
2086
2087void AssemblyWriter::printMDNodeBody(const MDNode *Node) {
Dan Gohmana2489d12010-07-20 23:55:01 +00002088 WriteMDNodeBodyInternal(Out, Node, &TypePrinter, &Machine, TheModule);
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002089 WriteMDNodeComment(Node, Out);
2090 Out << "\n";
2091}
Chris Lattner2f7c9632001-06-06 20:29:01 +00002092
Bill Wendling829b4782013-02-11 08:43:33 +00002093void AssemblyWriter::writeAllAttributeGroups() {
2094 std::vector<std::pair<AttributeSet, unsigned> > asVec;
2095 asVec.resize(Machine.as_size());
2096
2097 for (SlotTracker::as_iterator I = Machine.as_begin(), E = Machine.as_end();
2098 I != E; ++I)
2099 asVec[I->second] = *I;
2100
2101 for (std::vector<std::pair<AttributeSet, unsigned> >::iterator
2102 I = asVec.begin(), E = asVec.end(); I != E; ++I)
2103 Out << "attributes #" << I->second << " = { "
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +00002104 << I->first.getAsString(AttributeSet::FunctionIndex, true) << " }\n";
Bill Wendling829b4782013-02-11 08:43:33 +00002105}
2106
Daniel Maleaded9f932013-05-08 20:38:31 +00002107} // namespace llvm
2108
Chris Lattner2f7c9632001-06-06 20:29:01 +00002109//===----------------------------------------------------------------------===//
2110// External Interface declarations
2111//===----------------------------------------------------------------------===//
2112
Dan Gohmane2745262009-08-12 17:23:50 +00002113void Module::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW) const {
Chris Lattnere36fd8a2008-08-19 04:26:57 +00002114 SlotTracker SlotTable(this);
Dan Gohmane2745262009-08-12 17:23:50 +00002115 formatted_raw_ostream OS(ROS);
Chris Lattner0c19df42008-08-23 22:23:09 +00002116 AssemblyWriter W(OS, SlotTable, this, AAW);
Chris Lattnerefb5e392009-12-31 02:23:35 +00002117 W.printModule(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00002118}
2119
Dan Gohman2637cc12010-07-21 23:38:33 +00002120void NamedMDNode::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW) const {
2121 SlotTracker SlotTable(getParent());
2122 formatted_raw_ostream OS(ROS);
2123 AssemblyWriter W(OS, SlotTable, getParent(), AAW);
2124 W.printNamedMDNode(this);
2125}
2126
Chris Lattner2ee62d22009-02-28 21:11:05 +00002127void Type::print(raw_ostream &OS) const {
2128 if (this == 0) {
2129 OS << "<null Type>";
2130 return;
2131 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002132 TypePrinting TP;
2133 TP.print(const_cast<Type*>(this), OS);
Andrew Trickec4b6e72011-09-30 19:48:58 +00002134
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002135 // If the type is a named struct type, print the body as well.
2136 if (StructType *STy = dyn_cast<StructType>(const_cast<Type*>(this)))
Chris Lattner01beceb2011-08-12 18:07:07 +00002137 if (!STy->isLiteral()) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002138 OS << " = type ";
2139 TP.printStructBody(STy, OS);
2140 }
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00002141}
2142
Dan Gohmane2745262009-08-12 17:23:50 +00002143void Value::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW) const {
Chris Lattner0c19df42008-08-23 22:23:09 +00002144 if (this == 0) {
Dan Gohman12dad632009-08-12 20:56:03 +00002145 ROS << "printing a <null> value\n";
Chris Lattner0c19df42008-08-23 22:23:09 +00002146 return;
2147 }
Dan Gohman12dad632009-08-12 20:56:03 +00002148 formatted_raw_ostream OS(ROS);
Chris Lattner0c19df42008-08-23 22:23:09 +00002149 if (const Instruction *I = dyn_cast<Instruction>(this)) {
2150 const Function *F = I->getParent() ? I->getParent()->getParent() : 0;
2151 SlotTracker SlotTable(F);
Chris Lattnerd5bace72009-12-31 08:23:09 +00002152 AssemblyWriter W(OS, SlotTable, getModuleFromVal(I), AAW);
Chris Lattnerefb5e392009-12-31 02:23:35 +00002153 W.printInstruction(*I);
Chris Lattner0c19df42008-08-23 22:23:09 +00002154 } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(this)) {
2155 SlotTracker SlotTable(BB->getParent());
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002156 AssemblyWriter W(OS, SlotTable, getModuleFromVal(BB), AAW);
Chris Lattnerefb5e392009-12-31 02:23:35 +00002157 W.printBasicBlock(BB);
Chris Lattner0c19df42008-08-23 22:23:09 +00002158 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(this)) {
2159 SlotTracker SlotTable(GV->getParent());
Dan Gohman6c7a4852009-04-20 16:10:33 +00002160 AssemblyWriter W(OS, SlotTable, GV->getParent(), AAW);
Chris Lattnerefb5e392009-12-31 02:23:35 +00002161 if (const GlobalVariable *V = dyn_cast<GlobalVariable>(GV))
2162 W.printGlobal(V);
2163 else if (const Function *F = dyn_cast<Function>(GV))
2164 W.printFunction(F);
2165 else
2166 W.printAlias(cast<GlobalAlias>(GV));
Devang Patel5546b982009-07-01 20:59:15 +00002167 } else if (const MDNode *N = dyn_cast<MDNode>(this)) {
Victor Hernandeze5f2af72010-01-20 04:45:57 +00002168 const Function *F = N->getFunction();
Victor Hernandez61e6e822010-01-14 01:47:37 +00002169 SlotTracker SlotTable(F);
Dan Gohman15132172010-07-14 21:12:44 +00002170 AssemblyWriter W(OS, SlotTable, F ? F->getParent() : 0, AAW);
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002171 W.printMDNodeBody(N);
Chris Lattner0c19df42008-08-23 22:23:09 +00002172 } else if (const Constant *C = dyn_cast<Constant>(this)) {
Chris Lattner92c5c122009-02-28 23:20:19 +00002173 TypePrinting TypePrinter;
Chris Lattnere101c442009-02-28 21:26:53 +00002174 TypePrinter.print(C->getType(), OS);
Chris Lattner2ee62d22009-02-28 21:11:05 +00002175 OS << ' ';
Dan Gohmana2489d12010-07-20 23:55:01 +00002176 WriteConstantInternal(OS, C, TypePrinter, 0, 0);
Chris Lattner31fcc282009-12-31 01:41:14 +00002177 } else if (isa<InlineAsm>(this) || isa<MDString>(this) ||
2178 isa<Argument>(this)) {
Chandler Carruthd48cdbf2014-01-09 02:29:41 +00002179 this->printAsOperand(OS);
Chris Lattner0c19df42008-08-23 22:23:09 +00002180 } else {
Dan Gohmanc0353bf2009-09-23 01:33:16 +00002181 // Otherwise we don't know what it is. Call the virtual function to
2182 // allow a subclass to print itself.
2183 printCustom(OS);
Chris Lattner0c19df42008-08-23 22:23:09 +00002184 }
2185}
2186
Chandler Carruthd48cdbf2014-01-09 02:29:41 +00002187void Value::printAsOperand(raw_ostream &O, bool PrintType, const Module *M) const {
2188 // Fast path: Don't construct and populate a TypePrinting object if we
2189 // won't be needing any types printed.
2190 if (!PrintType &&
2191 ((!isa<Constant>(this) && !isa<MDNode>(this)) ||
2192 hasName() || isa<GlobalValue>(this))) {
2193 WriteAsOperandInternal(O, this, 0, 0, M);
2194 return;
2195 }
2196
2197 if (!M)
2198 M = getModuleFromVal(this);
2199
2200 TypePrinting TypePrinter;
2201 if (M)
2202 TypePrinter.incorporateTypes(*M);
2203 if (PrintType) {
2204 TypePrinter.print(getType(), O);
2205 O << ' ';
2206 }
2207
2208 WriteAsOperandInternal(O, this, &TypePrinter, 0, M);
2209}
2210
Dan Gohmanc0353bf2009-09-23 01:33:16 +00002211// Value::printCustom - subclasses should override this to implement printing.
2212void Value::printCustom(raw_ostream &OS) const {
2213 llvm_unreachable("Unknown value to print out!");
2214}
2215
Chris Lattnerdab942552008-08-25 17:03:15 +00002216// Value::dump - allow easy printing of Values from the debugger.
David Greenec7f9b122010-01-05 01:29:26 +00002217void Value::dump() const { print(dbgs()); dbgs() << '\n'; }
Reid Spencer52641832004-05-25 18:14:38 +00002218
Chris Lattnerdab942552008-08-25 17:03:15 +00002219// Type::dump - allow easy printing of Types from the debugger.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002220void Type::dump() const { print(dbgs()); }
Chris Lattner24025262009-02-28 21:05:51 +00002221
Chris Lattnerdab942552008-08-25 17:03:15 +00002222// Module::dump() - Allow printing of Modules from the debugger.
David Greenec7f9b122010-01-05 01:29:26 +00002223void Module::dump() const { print(dbgs(), 0); }
Bill Wendling3681d112011-12-09 23:18:34 +00002224
2225// NamedMDNode::dump() - Allow printing of NamedMDNodes from the debugger.
2226void NamedMDNode::dump() const { print(dbgs(), 0); }