blob: fc73a61f9f7f1131da4d779010d194a994b4ef81 [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"
Chandler Carruth9aca9182014-01-07 12:34:26 +000022#include "llvm/IR/AssemblyAnnotationWriter.h"
Chandler Carruth1305dc32014-03-04 11:45:46 +000023#include "llvm/IR/CFG.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000024#include "llvm/IR/CallingConv.h"
25#include "llvm/IR/Constants.h"
Chandler Carruth9a4c9e52014-03-06 00:46:21 +000026#include "llvm/IR/DebugInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000027#include "llvm/IR/DerivedTypes.h"
Chandler Carruthb8ddc702014-01-12 11:10:32 +000028#include "llvm/IR/IRPrintingPasses.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000029#include "llvm/IR/InlineAsm.h"
30#include "llvm/IR/IntrinsicInst.h"
31#include "llvm/IR/LLVMContext.h"
32#include "llvm/IR/Module.h"
33#include "llvm/IR/Operator.h"
Chandler Carruthdcb603f2013-01-07 15:43:51 +000034#include "llvm/IR/TypeFinder.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000035#include "llvm/IR/ValueSymbolTable.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))
Craig Topperc6207612014-04-09 06:08:46 +000054 return MA->getParent() ? MA->getParent()->getParent() : nullptr;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +000055
Chris Lattner3eee99c2008-08-19 04:36:02 +000056 if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
Craig Topperc6207612014-04-09 06:08:46 +000057 return BB->getParent() ? BB->getParent()->getParent() : nullptr;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +000058
Chris Lattner3eee99c2008-08-19 04:36:02 +000059 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Craig Topperc6207612014-04-09 06:08:46 +000060 const Function *M = I->getParent() ? I->getParent()->getParent() : nullptr;
61 return M ? M->getParent() : nullptr;
Chris Lattner3eee99c2008-08-19 04:36:02 +000062 }
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();
Craig Topperc6207612014-04-09 06:08:46 +000066 return nullptr;
Chris Lattner3eee99c2008-08-19 04:36:02 +000067}
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
Craig Topperc6207612014-04-09 06:08:46 +0000423 return new SlotTracker((Function *)nullptr);
Dan Gohmana2489d12010-07-20 23:55:01 +0000424 }
Dale Johannesen7b1a7ed2010-01-13 00:00:24 +0000425
Craig Topperc6207612014-04-09 06:08:46 +0000426 return nullptr;
Chris Lattner3eee99c2008-08-19 04:36:02 +0000427}
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)
Craig Topperc6207612014-04-09 06:08:46 +0000438 : TheModule(M), TheFunction(nullptr), 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)
Craig Topperc6207612014-04-09 06:08:46 +0000445 : TheModule(F ? F->getParent() : nullptr), TheFunction(F),
446 FunctionProcessed(false), 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();
Craig Topperc6207612014-04-09 06:08:46 +0000452 TheModule = nullptr; ///< Prevent re-processing next time we're called.
Chris Lattner3eee99c2008-08-19 04:36:02 +0000453 }
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
Craig Topperc6207612014-04-09 06:08:46 +0000562 TheFunction = nullptr;
Chris Lattner3eee99c2008-08-19 04:36:02 +0000563 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.
Benjamin Kramer86c77412014-03-15 18:47:07 +0000813 static_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);
Craig Topperc6207612014-04-09 06:08:46 +00001050 if (!V)
Chris Lattnercdec5812009-12-31 02:31:59 +00001051 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
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001128 char Prefix = '%';
1129 int Slot;
Chris Lattner5b82a0a2011-08-03 06:15:41 +00001130 // If we have a SlotTracker, use it.
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001131 if (Machine) {
1132 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1133 Slot = Machine->getGlobalSlot(GV);
1134 Prefix = '@';
1135 } else {
1136 Slot = Machine->getLocalSlot(V);
Andrew Trickec4b6e72011-09-30 19:48:58 +00001137
Chris Lattner5b82a0a2011-08-03 06:15:41 +00001138 // If the local value didn't succeed, then we may be referring to a value
1139 // from a different function. Translate it, as this can happen when using
1140 // address of blocks.
1141 if (Slot == -1)
1142 if ((Machine = createSlotTracker(V))) {
1143 Slot = Machine->getLocalSlot(V);
1144 delete Machine;
1145 }
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001146 }
Chris Lattner5b82a0a2011-08-03 06:15:41 +00001147 } else if ((Machine = createSlotTracker(V))) {
1148 // Otherwise, create one to get the # and then destroy it.
1149 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1150 Slot = Machine->getGlobalSlot(GV);
1151 Prefix = '@';
Chris Lattnera2d810d2006-01-25 22:26:05 +00001152 } else {
Chris Lattner5b82a0a2011-08-03 06:15:41 +00001153 Slot = Machine->getLocalSlot(V);
Chris Lattnerd84bb632002-04-16 21:36:08 +00001154 }
Chris Lattner5b82a0a2011-08-03 06:15:41 +00001155 delete Machine;
Craig Topperc6207612014-04-09 06:08:46 +00001156 Machine = nullptr;
Chris Lattner5b82a0a2011-08-03 06:15:41 +00001157 } else {
1158 Slot = -1;
Chris Lattnerd84bb632002-04-16 21:36:08 +00001159 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001160
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001161 if (Slot != -1)
1162 Out << Prefix << Slot;
1163 else
1164 Out << "<badref>";
Chris Lattnerd84bb632002-04-16 21:36:08 +00001165}
1166
Daniel Maleaded9f932013-05-08 20:38:31 +00001167void AssemblyWriter::init() {
1168 if (TheModule)
1169 TypePrinter.incorporateTypes(*TheModule);
1170}
Chris Lattner2e9fee42001-07-12 23:35:26 +00001171
Andrew Trickec4b6e72011-09-30 19:48:58 +00001172
Daniel Maleaded9f932013-05-08 20:38:31 +00001173AssemblyWriter::AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac,
1174 const Module *M,
1175 AssemblyAnnotationWriter *AAW)
1176 : Out(o), TheModule(M), Machine(Mac), AnnotationWriter(AAW) {
1177 init();
1178}
Chris Lattner2f7c9632001-06-06 20:29:01 +00001179
Daniel Maleaded9f932013-05-08 20:38:31 +00001180AssemblyWriter::AssemblyWriter(formatted_raw_ostream &o, const Module *M,
1181 AssemblyAnnotationWriter *AAW)
1182 : Out(o), TheModule(M), ModuleSlotTracker(createSlotTracker(M)),
1183 Machine(*ModuleSlotTracker), AnnotationWriter(AAW) {
1184 init();
1185}
Andrew Trickec4b6e72011-09-30 19:48:58 +00001186
Daniel Maleaded9f932013-05-08 20:38:31 +00001187AssemblyWriter::~AssemblyWriter() { }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001188
Chris Lattner78e2e8b2006-12-06 06:24:27 +00001189void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType) {
Craig Topperc6207612014-04-09 06:08:46 +00001190 if (!Operand) {
Chris Lattner08f7d0c2005-02-24 16:58:29 +00001191 Out << "<null operand!>";
Chris Lattnerb419a1e2009-12-31 02:33:14 +00001192 return;
Chris Lattner08f7d0c2005-02-24 16:58:29 +00001193 }
Chris Lattnerb419a1e2009-12-31 02:33:14 +00001194 if (PrintType) {
1195 TypePrinter.print(Operand->getType(), Out);
1196 Out << ' ';
1197 }
Dan Gohmana2489d12010-07-20 23:55:01 +00001198 WriteAsOperandInternal(Out, Operand, &TypePrinter, &Machine, TheModule);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001199}
1200
Eli Friedmanfee02c62011-07-25 23:16:38 +00001201void AssemblyWriter::writeAtomic(AtomicOrdering Ordering,
1202 SynchronizationScope SynchScope) {
1203 if (Ordering == NotAtomic)
1204 return;
1205
1206 switch (SynchScope) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001207 case SingleThread: Out << " singlethread"; break;
1208 case CrossThread: break;
1209 }
1210
1211 switch (Ordering) {
1212 default: Out << " <bad ordering " << int(Ordering) << ">"; break;
1213 case Unordered: Out << " unordered"; break;
1214 case Monotonic: Out << " monotonic"; break;
1215 case Acquire: Out << " acquire"; break;
1216 case Release: Out << " release"; break;
1217 case AcquireRelease: Out << " acq_rel"; break;
1218 case SequentiallyConsistent: Out << " seq_cst"; break;
1219 }
1220}
1221
Tim Northovere94a5182014-03-11 10:48:52 +00001222void AssemblyWriter::writeAtomicCmpXchg(AtomicOrdering SuccessOrdering,
1223 AtomicOrdering FailureOrdering,
1224 SynchronizationScope SynchScope) {
1225 assert(SuccessOrdering != NotAtomic && FailureOrdering != NotAtomic);
1226
1227 switch (SynchScope) {
1228 case SingleThread: Out << " singlethread"; break;
1229 case CrossThread: break;
1230 }
1231
1232 switch (SuccessOrdering) {
1233 default: Out << " <bad ordering " << int(SuccessOrdering) << ">"; break;
1234 case Unordered: Out << " unordered"; break;
1235 case Monotonic: Out << " monotonic"; break;
1236 case Acquire: Out << " acquire"; break;
1237 case Release: Out << " release"; break;
1238 case AcquireRelease: Out << " acq_rel"; break;
1239 case SequentiallyConsistent: Out << " seq_cst"; break;
1240 }
1241
1242 switch (FailureOrdering) {
1243 default: Out << " <bad ordering " << int(FailureOrdering) << ">"; break;
1244 case Unordered: Out << " unordered"; break;
1245 case Monotonic: Out << " monotonic"; break;
1246 case Acquire: Out << " acquire"; break;
1247 case Release: Out << " release"; break;
1248 case AcquireRelease: Out << " acq_rel"; break;
1249 case SequentiallyConsistent: Out << " seq_cst"; break;
1250 }
1251}
1252
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001253void AssemblyWriter::writeParamOperand(const Value *Operand,
Bill Wendling749a43d2012-12-30 13:50:49 +00001254 AttributeSet Attrs, unsigned Idx) {
Craig Topperc6207612014-04-09 06:08:46 +00001255 if (!Operand) {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00001256 Out << "<null operand!>";
Chris Lattnerb419a1e2009-12-31 02:33:14 +00001257 return;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00001258 }
Chris Lattnerb419a1e2009-12-31 02:33:14 +00001259
1260 // Print the type
1261 TypePrinter.print(Operand->getType(), Out);
1262 // Print parameter attributes list
Bill Wendling749a43d2012-12-30 13:50:49 +00001263 if (Attrs.hasAttributes(Idx))
1264 Out << ' ' << Attrs.getAsString(Idx);
Chris Lattnerb419a1e2009-12-31 02:33:14 +00001265 Out << ' ';
1266 // Print the operand
Dan Gohmana2489d12010-07-20 23:55:01 +00001267 WriteAsOperandInternal(Out, Operand, &TypePrinter, &Machine, TheModule);
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00001268}
Chris Lattner2f7c9632001-06-06 20:29:01 +00001269
Chris Lattner7bfee412001-10-29 16:05:51 +00001270void AssemblyWriter::printModule(const Module *M) {
Bill Wendling829b4782013-02-11 08:43:33 +00001271 Machine.initialize();
1272
Chris Lattner4d8689e2005-03-02 23:12:40 +00001273 if (!M->getModuleIdentifier().empty() &&
Misha Brukmanb1c93172005-04-21 23:48:37 +00001274 // Don't print the ID if it will start a new line (which would
Chris Lattner4d8689e2005-03-02 23:12:40 +00001275 // require a comment char before it).
1276 M->getModuleIdentifier().find('\n') == std::string::npos)
1277 Out << "; ModuleID = '" << M->getModuleIdentifier() << "'\n";
1278
Rafael Espindolaf863ee22014-02-25 20:01:08 +00001279 const std::string &DL = M->getDataLayoutStr();
1280 if (!DL.empty())
1281 Out << "target datalayout = \"" << DL << "\"\n";
Reid Spencer48f98c82004-07-25 21:44:54 +00001282 if (!M->getTargetTriple().empty())
Reid Spencerffec7df2004-07-25 21:29:43 +00001283 Out << "target triple = \"" << M->getTargetTriple() << "\"\n";
Misha Brukmanb1c93172005-04-21 23:48:37 +00001284
Chris Lattnereef2fe72006-01-24 04:13:11 +00001285 if (!M->getModuleInlineAsm().empty()) {
Chris Lattnerefaf35d2006-01-24 00:45:30 +00001286 // Split the string into lines, to make it easier to read the .ll file.
Chris Lattnereef2fe72006-01-24 04:13:11 +00001287 std::string Asm = M->getModuleInlineAsm();
Chris Lattnerefaf35d2006-01-24 00:45:30 +00001288 size_t CurPos = 0;
1289 size_t NewLine = Asm.find_first_of('\n', CurPos);
Dan Gohman69273e62009-08-12 23:54:22 +00001290 Out << '\n';
Chris Lattnerefaf35d2006-01-24 00:45:30 +00001291 while (NewLine != std::string::npos) {
1292 // We found a newline, print the portion of the asm string from the
1293 // last newline up to this newline.
1294 Out << "module asm \"";
1295 PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.begin()+NewLine),
1296 Out);
1297 Out << "\"\n";
1298 CurPos = NewLine+1;
1299 NewLine = Asm.find_first_of('\n', CurPos);
1300 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +00001301 std::string rest(Asm.begin()+CurPos, Asm.end());
1302 if (!rest.empty()) {
1303 Out << "module asm \"";
1304 PrintEscapedString(rest, Out);
1305 Out << "\"\n";
1306 }
Chris Lattner6ed87bd2006-01-23 23:03:36 +00001307 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001308
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001309 printTypeIdentities();
Misha Brukmanb1c93172005-04-21 23:48:37 +00001310
Dan Gohman69273e62009-08-12 23:54:22 +00001311 // Output all globals.
1312 if (!M->global_empty()) Out << '\n';
Chris Lattner54932b02006-12-06 04:41:52 +00001313 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
Duncan Sands66fc0e62012-09-12 09:55:51 +00001314 I != E; ++I) {
1315 printGlobal(I); Out << '\n';
1316 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001317
Chris Lattnerd2747052007-04-26 02:24:10 +00001318 // Output all aliases.
1319 if (!M->alias_empty()) Out << "\n";
1320 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
1321 I != E; ++I)
1322 printAlias(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +00001323
Chris Lattner2cdd49d2004-09-14 05:06:58 +00001324 // Output all of the functions.
Chris Lattner113f4f42002-06-25 16:13:24 +00001325 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
1326 printFunction(I);
Devang Patel983c6b12009-07-08 21:44:25 +00001327
Bill Wendling829b4782013-02-11 08:43:33 +00001328 // Output all attribute groups.
Bill Wendling04945972013-04-29 23:48:06 +00001329 if (!Machine.as_empty()) {
Bill Wendling829b4782013-02-11 08:43:33 +00001330 Out << '\n';
1331 writeAllAttributeGroups();
1332 }
1333
Devang Patel23e68302009-07-29 22:04:47 +00001334 // Output named metadata.
Dan Gohman69273e62009-08-12 23:54:22 +00001335 if (!M->named_metadata_empty()) Out << '\n';
Andrew Trickec4b6e72011-09-30 19:48:58 +00001336
Devang Patel23e68302009-07-29 22:04:47 +00001337 for (Module::const_named_metadata_iterator I = M->named_metadata_begin(),
Chris Lattnerbddea6a2009-12-31 02:13:35 +00001338 E = M->named_metadata_end(); I != E; ++I)
Chris Lattner2c48c642009-12-31 01:54:05 +00001339 printNamedMDNode(I);
Devang Patel23e68302009-07-29 22:04:47 +00001340
1341 // Output metadata.
Chris Lattnercf4a76e2009-12-31 02:20:11 +00001342 if (!Machine.mdn_empty()) {
Chris Lattnerbddea6a2009-12-31 02:13:35 +00001343 Out << '\n';
1344 writeAllMDNodes();
1345 }
Chris Lattnerfee714f2001-09-07 16:36:04 +00001346}
1347
Chris Lattner2c48c642009-12-31 01:54:05 +00001348void AssemblyWriter::printNamedMDNode(const NamedMDNode *NMD) {
Nick Lewycky5bcbd732011-06-15 06:37:58 +00001349 Out << '!';
1350 StringRef Name = NMD->getName();
1351 if (Name.empty()) {
1352 Out << "<empty name> ";
1353 } else {
Guy Benyei83c74e92013-02-12 21:21:59 +00001354 if (isalpha(static_cast<unsigned char>(Name[0])) ||
1355 Name[0] == '-' || Name[0] == '$' ||
Nick Lewycky5bcbd732011-06-15 06:37:58 +00001356 Name[0] == '.' || Name[0] == '_')
1357 Out << Name[0];
1358 else
1359 Out << '\\' << hexdigit(Name[0] >> 4) << hexdigit(Name[0] & 0x0F);
1360 for (unsigned i = 1, e = Name.size(); i != e; ++i) {
1361 unsigned char C = Name[i];
Guy Benyei83c74e92013-02-12 21:21:59 +00001362 if (isalnum(static_cast<unsigned char>(C)) || C == '-' || C == '$' ||
1363 C == '.' || C == '_')
Nick Lewycky5bcbd732011-06-15 06:37:58 +00001364 Out << C;
1365 else
1366 Out << '\\' << hexdigit(C >> 4) << hexdigit(C & 0x0F);
1367 }
1368 }
1369 Out << " = !{";
Chris Lattner2c48c642009-12-31 01:54:05 +00001370 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1371 if (i) Out << ", ";
Dan Gohman0a54da22010-09-09 20:53:58 +00001372 int Slot = Machine.getMetadataSlot(NMD->getOperand(i));
1373 if (Slot == -1)
1374 Out << "<badref>";
1375 else
1376 Out << '!' << Slot;
Chris Lattner2c48c642009-12-31 01:54:05 +00001377 }
1378 Out << "}\n";
1379}
1380
1381
Dan Gohmane2745262009-08-12 17:23:50 +00001382static void PrintLinkage(GlobalValue::LinkageTypes LT,
1383 formatted_raw_ostream &Out) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001384 switch (LT) {
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001385 case GlobalValue::ExternalLinkage: break;
1386 case GlobalValue::PrivateLinkage: Out << "private "; break;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001387 case GlobalValue::InternalLinkage: Out << "internal "; break;
1388 case GlobalValue::LinkOnceAnyLinkage: Out << "linkonce "; break;
1389 case GlobalValue::LinkOnceODRLinkage: Out << "linkonce_odr "; break;
1390 case GlobalValue::WeakAnyLinkage: Out << "weak "; break;
1391 case GlobalValue::WeakODRLinkage: Out << "weak_odr "; break;
1392 case GlobalValue::CommonLinkage: Out << "common "; break;
1393 case GlobalValue::AppendingLinkage: Out << "appending "; break;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001394 case GlobalValue::ExternalWeakLinkage: Out << "extern_weak "; break;
Chris Lattner184f1be2009-04-13 05:44:34 +00001395 case GlobalValue::AvailableExternallyLinkage:
1396 Out << "available_externally ";
1397 break;
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001398 }
1399}
Duncan Sands12da8ce2009-03-07 15:45:40 +00001400
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001401
1402static void PrintVisibility(GlobalValue::VisibilityTypes Vis,
Dan Gohmane2745262009-08-12 17:23:50 +00001403 formatted_raw_ostream &Out) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001404 switch (Vis) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001405 case GlobalValue::DefaultVisibility: break;
1406 case GlobalValue::HiddenVisibility: Out << "hidden "; break;
1407 case GlobalValue::ProtectedVisibility: Out << "protected "; break;
1408 }
1409}
1410
Nico Rieck7157bb72014-01-14 15:22:47 +00001411static void PrintDLLStorageClass(GlobalValue::DLLStorageClassTypes SCT,
1412 formatted_raw_ostream &Out) {
1413 switch (SCT) {
1414 case GlobalValue::DefaultStorageClass: break;
1415 case GlobalValue::DLLImportStorageClass: Out << "dllimport "; break;
1416 case GlobalValue::DLLExportStorageClass: Out << "dllexport "; break;
1417 }
1418}
1419
Hans Wennborgcbe34b42012-06-23 11:37:03 +00001420static void PrintThreadLocalModel(GlobalVariable::ThreadLocalMode TLM,
1421 formatted_raw_ostream &Out) {
1422 switch (TLM) {
1423 case GlobalVariable::NotThreadLocal:
1424 break;
1425 case GlobalVariable::GeneralDynamicTLSModel:
1426 Out << "thread_local ";
1427 break;
1428 case GlobalVariable::LocalDynamicTLSModel:
1429 Out << "thread_local(localdynamic) ";
1430 break;
1431 case GlobalVariable::InitialExecTLSModel:
1432 Out << "thread_local(initialexec) ";
1433 break;
1434 case GlobalVariable::LocalExecTLSModel:
1435 Out << "thread_local(localexec) ";
1436 break;
1437 }
1438}
1439
Chris Lattner7bfee412001-10-29 16:05:51 +00001440void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Dan Gohman5ded1422010-01-29 23:12:36 +00001441 if (GV->isMaterializable())
1442 Out << "; Materializable\n";
1443
Dan Gohmana2489d12010-07-20 23:55:01 +00001444 WriteAsOperandInternal(Out, GV, &TypePrinter, &Machine, GV->getParent());
Dan Gohman466876b2009-08-12 23:32:33 +00001445 Out << " = ";
Chris Lattner37798642001-09-18 04:01:05 +00001446
Chris Lattner1508d3f2008-08-19 05:16:28 +00001447 if (!GV->hasInitializer() && GV->hasExternalLinkage())
1448 Out << "external ";
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001449
Chris Lattner1508d3f2008-08-19 05:16:28 +00001450 PrintLinkage(GV->getLinkage(), Out);
1451 PrintVisibility(GV->getVisibility(), Out);
Nico Rieck7157bb72014-01-14 15:22:47 +00001452 PrintDLLStorageClass(GV->getDLLStorageClass(), Out);
Hans Wennborgcbe34b42012-06-23 11:37:03 +00001453 PrintThreadLocalModel(GV->getThreadLocalMode(), Out);
Rafael Espindola42a4c9f2014-06-06 01:20:28 +00001454 if (GV->hasUnnamedAddr())
1455 Out << "unnamed_addr ";
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +00001456
Chris Lattnerac161bf2009-01-02 07:01:27 +00001457 if (unsigned AddressSpace = GV->getType()->getAddressSpace())
1458 Out << "addrspace(" << AddressSpace << ") ";
Michael Gottesman27e7ef32013-02-05 05:57:38 +00001459 if (GV->isExternallyInitialized()) Out << "externally_initialized ";
Misha Brukmana6619a92004-06-21 21:53:56 +00001460 Out << (GV->isConstant() ? "constant " : "global ");
Chris Lattnere101c442009-02-28 21:26:53 +00001461 TypePrinter.print(GV->getType()->getElementType(), Out);
Chris Lattner37798642001-09-18 04:01:05 +00001462
Dan Gohman81313fd2008-09-14 17:21:12 +00001463 if (GV->hasInitializer()) {
1464 Out << ' ';
Devang Patel983c6b12009-07-08 21:44:25 +00001465 writeOperand(GV->getInitializer(), false);
Dan Gohman81313fd2008-09-14 17:21:12 +00001466 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001467
Chris Lattner9380b812010-07-07 23:16:37 +00001468 if (GV->hasSection()) {
1469 Out << ", section \"";
1470 PrintEscapedString(GV->getSection(), Out);
1471 Out << '"';
1472 }
Chris Lattner4b96c542005-11-12 00:10:19 +00001473 if (GV->getAlignment())
Chris Lattnerf8a974d2005-11-06 06:48:53 +00001474 Out << ", align " << GV->getAlignment();
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001475
Chris Lattner113f4f42002-06-25 16:13:24 +00001476 printInfoComment(*GV);
Chris Lattnerda975502001-09-10 07:58:01 +00001477}
1478
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001479void AssemblyWriter::printAlias(const GlobalAlias *GA) {
Dan Gohman5ded1422010-01-29 23:12:36 +00001480 if (GA->isMaterializable())
1481 Out << "; Materializable\n";
1482
Dale Johannesen83e468a2008-06-03 18:14:29 +00001483 // Don't crash when dumping partially built GA
1484 if (!GA->hasName())
1485 Out << "<<nameless>> = ";
Chris Lattner033935d2008-08-17 04:40:13 +00001486 else {
1487 PrintLLVMName(Out, GA);
1488 Out << " = ";
1489 }
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001490 PrintVisibility(GA->getVisibility(), Out);
Nico Rieck7157bb72014-01-14 15:22:47 +00001491 PrintDLLStorageClass(GA->getDLLStorageClass(), Out);
Rafael Espindola59f7eba2014-05-28 18:15:43 +00001492 PrintThreadLocalModel(GA->getThreadLocalMode(), Out);
Rafael Espindola42a4c9f2014-06-06 01:20:28 +00001493 if (GA->hasUnnamedAddr())
1494 Out << "unnamed_addr ";
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001495
1496 Out << "alias ";
1497
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001498 PrintLinkage(GA->getLinkage(), Out);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001499
Anton Korobeynikov546ea7e2007-04-29 18:02:48 +00001500 const Constant *Aliasee = GA->getAliasee();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001501
Craig Topperc6207612014-04-09 06:08:46 +00001502 if (!Aliasee) {
Rafael Espindola64c1e182014-06-03 02:41:57 +00001503 TypePrinter.print(GA->getType(), Out);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001504 Out << " <<NULL ALIASEE>>";
Jay Foad92c19132011-08-01 12:48:54 +00001505 } else {
Jay Foad26db79d2011-08-01 12:29:14 +00001506 writeOperand(Aliasee, !isa<ConstantExpr>(Aliasee));
Jay Foad92c19132011-08-01 12:48:54 +00001507 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001508
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001509 printInfoComment(*GA);
Chris Lattner1508d3f2008-08-19 05:16:28 +00001510 Out << '\n';
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001511}
1512
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001513void AssemblyWriter::printTypeIdentities() {
1514 if (TypePrinter.NumberedTypes.empty() &&
1515 TypePrinter.NamedTypes.empty())
1516 return;
Andrew Trickec4b6e72011-09-30 19:48:58 +00001517
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001518 Out << '\n';
Andrew Trickec4b6e72011-09-30 19:48:58 +00001519
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001520 // We know all the numbers that each type is used and we know that it is a
1521 // dense assignment. Convert the map to an index table.
1522 std::vector<StructType*> NumberedTypes(TypePrinter.NumberedTypes.size());
Andrew Trickec4b6e72011-09-30 19:48:58 +00001523 for (DenseMap<StructType*, unsigned>::iterator I =
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001524 TypePrinter.NumberedTypes.begin(), E = TypePrinter.NumberedTypes.end();
1525 I != E; ++I) {
1526 assert(I->second < NumberedTypes.size() && "Didn't get a dense numbering?");
1527 NumberedTypes[I->second] = I->first;
1528 }
Andrew Trickec4b6e72011-09-30 19:48:58 +00001529
Chris Lattner3243ea12009-03-01 00:03:38 +00001530 // Emit all numbered types.
1531 for (unsigned i = 0, e = NumberedTypes.size(); i != e; ++i) {
Dan Gohman466876b2009-08-12 23:32:33 +00001532 Out << '%' << i << " = type ";
Andrew Trickec4b6e72011-09-30 19:48:58 +00001533
Chris Lattner3243ea12009-03-01 00:03:38 +00001534 // Make sure we print out at least one level of the type structure, so
1535 // that we do not get %2 = type %2
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001536 TypePrinter.printStructBody(NumberedTypes[i], Out);
Dan Gohman69273e62009-08-12 23:54:22 +00001537 Out << '\n';
Chris Lattner3243ea12009-03-01 00:03:38 +00001538 }
Andrew Trickec4b6e72011-09-30 19:48:58 +00001539
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001540 for (unsigned i = 0, e = TypePrinter.NamedTypes.size(); i != e; ++i) {
1541 PrintLLVMName(Out, TypePrinter.NamedTypes[i]->getName(), LocalPrefix);
Chris Lattner1508d3f2008-08-19 05:16:28 +00001542 Out << " = type ";
Reid Spencere7e96712004-05-25 08:53:40 +00001543
1544 // Make sure we print out at least one level of the type structure, so
1545 // that we do not get %FILE = type %FILE
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001546 TypePrinter.printStructBody(TypePrinter.NamedTypes[i], Out);
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001547 Out << '\n';
Reid Spencere7e96712004-05-25 08:53:40 +00001548 }
Reid Spencer32af9e82007-01-06 07:24:44 +00001549}
1550
Misha Brukmanc566ca362004-03-02 00:22:19 +00001551/// printFunction - Print all aspects of a function.
1552///
Chris Lattner113f4f42002-06-25 16:13:24 +00001553void AssemblyWriter::printFunction(const Function *F) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001554 // Print out the return type and name.
1555 Out << '\n';
Chris Lattner379a8d22003-04-16 20:28:45 +00001556
Misha Brukmana6619a92004-06-21 21:53:56 +00001557 if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001558
Dan Gohman5ded1422010-01-29 23:12:36 +00001559 if (F->isMaterializable())
1560 Out << "; Materializable\n";
1561
Bill Wendling847a5c32013-04-16 20:55:47 +00001562 const AttributeSet &Attrs = F->getAttributes();
Bill Wendling04945972013-04-29 23:48:06 +00001563 if (Attrs.hasAttributes(AttributeSet::FunctionIndex)) {
Bill Wendling847a5c32013-04-16 20:55:47 +00001564 AttributeSet AS = Attrs.getFnAttributes();
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +00001565 std::string AttrStr;
1566
1567 unsigned Idx = 0;
1568 for (unsigned E = AS.getNumSlots(); Idx != E; ++Idx)
1569 if (AS.getSlotIndex(Idx) == AttributeSet::FunctionIndex)
1570 break;
1571
1572 for (AttributeSet::iterator I = AS.begin(Idx), E = AS.end(Idx);
1573 I != E; ++I) {
1574 Attribute Attr = *I;
1575 if (!Attr.isStringAttribute()) {
1576 if (!AttrStr.empty()) AttrStr += ' ';
1577 AttrStr += Attr.getAsString();
1578 }
1579 }
1580
Bill Wendling847a5c32013-04-16 20:55:47 +00001581 if (!AttrStr.empty())
1582 Out << "; Function Attrs: " << AttrStr << '\n';
1583 }
1584
Reid Spencer5301e7c2007-01-30 20:08:39 +00001585 if (F->isDeclaration())
Chris Lattner10f03a62007-08-19 22:15:26 +00001586 Out << "declare ";
1587 else
Reid Spencer7ce2d2a2006-12-29 20:29:48 +00001588 Out << "define ";
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001589
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001590 PrintLinkage(F->getLinkage(), Out);
1591 PrintVisibility(F->getVisibility(), Out);
Nico Rieck7157bb72014-01-14 15:22:47 +00001592 PrintDLLStorageClass(F->getDLLStorageClass(), Out);
Chris Lattner379a8d22003-04-16 20:28:45 +00001593
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001594 // Print the calling convention.
Micah Villmow857186d2012-09-13 15:11:12 +00001595 if (F->getCallingConv() != CallingConv::C) {
1596 PrintCallingConv(F->getCallingConv(), Out);
1597 Out << " ";
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001598 }
1599
Chris Lattner229907c2011-07-18 04:54:35 +00001600 FunctionType *FT = F->getFunctionType();
Bill Wendling658d24d2013-01-18 21:53:16 +00001601 if (Attrs.hasAttributes(AttributeSet::ReturnIndex))
1602 Out << Attrs.getAsString(AttributeSet::ReturnIndex) << ' ';
Chris Lattnere101c442009-02-28 21:26:53 +00001603 TypePrinter.print(F->getReturnType(), Out);
Chris Lattner585297e82008-08-19 05:26:17 +00001604 Out << ' ';
Dan Gohmana2489d12010-07-20 23:55:01 +00001605 WriteAsOperandInternal(Out, F, &TypePrinter, &Machine, F->getParent());
Misha Brukmana6619a92004-06-21 21:53:56 +00001606 Out << '(';
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001607 Machine.incorporateFunction(F);
Chris Lattnerfee714f2001-09-07 16:36:04 +00001608
Chris Lattner7bfee412001-10-29 16:05:51 +00001609 // Loop over the arguments, printing them...
Chris Lattnerfee714f2001-09-07 16:36:04 +00001610
Reid Spencer8c4914c2006-12-31 05:24:50 +00001611 unsigned Idx = 1;
Chris Lattner82738fe2007-04-18 00:57:22 +00001612 if (!F->isDeclaration()) {
1613 // If this isn't a declaration, print the argument names as well.
1614 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
1615 I != E; ++I) {
1616 // Insert commas as we go... the first arg doesn't get a comma
1617 if (I != F->arg_begin()) Out << ", ";
Bill Wendling749a43d2012-12-30 13:50:49 +00001618 printArgument(I, Attrs, Idx);
Chris Lattner82738fe2007-04-18 00:57:22 +00001619 Idx++;
1620 }
1621 } else {
1622 // Otherwise, print the types from the function type.
1623 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
1624 // Insert commas as we go... the first arg doesn't get a comma
1625 if (i) Out << ", ";
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001626
Chris Lattner82738fe2007-04-18 00:57:22 +00001627 // Output type...
Chris Lattnere101c442009-02-28 21:26:53 +00001628 TypePrinter.print(FT->getParamType(i), Out);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001629
Bill Wendling749a43d2012-12-30 13:50:49 +00001630 if (Attrs.hasAttributes(i+1))
1631 Out << ' ' << Attrs.getAsString(i+1);
Chris Lattner82738fe2007-04-18 00:57:22 +00001632 }
Reid Spencer8c4914c2006-12-31 05:24:50 +00001633 }
Chris Lattnerfee714f2001-09-07 16:36:04 +00001634
1635 // Finish printing arguments...
Chris Lattner113f4f42002-06-25 16:13:24 +00001636 if (FT->isVarArg()) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001637 if (FT->getNumParams()) Out << ", ";
1638 Out << "..."; // Output varargs portion of signature!
Chris Lattnerfee714f2001-09-07 16:36:04 +00001639 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001640 Out << ')';
Rafael Espindola563eb4b2011-01-25 19:09:56 +00001641 if (F->hasUnnamedAddr())
1642 Out << " unnamed_addr";
Bill Wendling04945972013-04-29 23:48:06 +00001643 if (Attrs.hasAttributes(AttributeSet::FunctionIndex))
1644 Out << " #" << Machine.getAttributeGroupSlot(Attrs.getFnAttributes());
Chris Lattner9380b812010-07-07 23:16:37 +00001645 if (F->hasSection()) {
1646 Out << " section \"";
1647 PrintEscapedString(F->getSection(), Out);
1648 Out << '"';
1649 }
Chris Lattnerf8a974d2005-11-06 06:48:53 +00001650 if (F->getAlignment())
1651 Out << " align " << F->getAlignment();
Gordon Henriksend930f912008-08-17 18:44:35 +00001652 if (F->hasGC())
1653 Out << " gc \"" << F->getGC() << '"';
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00001654 if (F->hasPrefixData()) {
1655 Out << " prefix ";
1656 writeOperand(F->getPrefixData(), true);
1657 }
Reid Spencer5301e7c2007-01-30 20:08:39 +00001658 if (F->isDeclaration()) {
Chris Lattnerfb483622010-09-02 22:52:10 +00001659 Out << '\n';
Chris Lattnerb2f02e52002-05-06 03:00:40 +00001660 } else {
Chris Lattnerfb483622010-09-02 22:52:10 +00001661 Out << " {";
1662 // Output all of the function's basic blocks.
Chris Lattner113f4f42002-06-25 16:13:24 +00001663 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
1664 printBasicBlock(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +00001665
Misha Brukmana6619a92004-06-21 21:53:56 +00001666 Out << "}\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +00001667 }
1668
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001669 Machine.purgeFunction();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001670}
1671
Misha Brukmanc566ca362004-03-02 00:22:19 +00001672/// printArgument - This member is called for every argument that is passed into
1673/// the function. Simply print it out
1674///
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001675void AssemblyWriter::printArgument(const Argument *Arg,
Bill Wendling749a43d2012-12-30 13:50:49 +00001676 AttributeSet Attrs, unsigned Idx) {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001677 // Output type...
Chris Lattnere101c442009-02-28 21:26:53 +00001678 TypePrinter.print(Arg->getType(), Out);
Misha Brukmanb1c93172005-04-21 23:48:37 +00001679
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00001680 // Output parameter attributes list
Bill Wendling749a43d2012-12-30 13:50:49 +00001681 if (Attrs.hasAttributes(Idx))
1682 Out << ' ' << Attrs.getAsString(Idx);
Reid Spencer8c4914c2006-12-31 05:24:50 +00001683
Chris Lattner2f7c9632001-06-06 20:29:01 +00001684 // Output name, if available...
Chris Lattner033935d2008-08-17 04:40:13 +00001685 if (Arg->hasName()) {
1686 Out << ' ';
1687 PrintLLVMName(Out, Arg);
1688 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001689}
1690
Misha Brukmanc566ca362004-03-02 00:22:19 +00001691/// printBasicBlock - This member is called for each basic block in a method.
1692///
Chris Lattner7bfee412001-10-29 16:05:51 +00001693void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Nick Lewycky4d43d3c2008-04-25 16:53:59 +00001694 if (BB->hasName()) { // Print out the label if it exists...
Chris Lattner033935d2008-08-17 04:40:13 +00001695 Out << "\n";
Daniel Dunbare03eecb2009-07-25 23:55:21 +00001696 PrintLLVMName(Out, BB->getName(), LabelPrefix);
Chris Lattner033935d2008-08-17 04:40:13 +00001697 Out << ':';
Nick Lewycky4d43d3c2008-04-25 16:53:59 +00001698 } else if (!BB->use_empty()) { // Don't print block # of no uses...
Bill Wendling35a9c3c2011-04-10 23:18:04 +00001699 Out << "\n; <label>:";
Chris Lattner5e043322007-01-11 03:54:27 +00001700 int Slot = Machine.getLocalSlot(BB);
Chris Lattner757ee0b2004-06-09 19:41:19 +00001701 if (Slot != -1)
Misha Brukmana6619a92004-06-21 21:53:56 +00001702 Out << Slot;
Chris Lattner757ee0b2004-06-09 19:41:19 +00001703 else
Misha Brukmana6619a92004-06-21 21:53:56 +00001704 Out << "<badref>";
Chris Lattner58185f22002-10-02 19:38:55 +00001705 }
Chris Lattner2447ef52003-11-20 00:09:43 +00001706
Craig Topperc6207612014-04-09 06:08:46 +00001707 if (!BB->getParent()) {
Chris Lattneree97b8b2009-08-17 15:48:08 +00001708 Out.PadToColumn(50);
Dan Gohmane2745262009-08-12 17:23:50 +00001709 Out << "; Error: Block without parent!";
1710 } else if (BB != &BB->getParent()->getEntryBlock()) { // Not the entry block?
Chris Lattnerfb483622010-09-02 22:52:10 +00001711 // Output predecessors for the block.
Chris Lattneree97b8b2009-08-17 15:48:08 +00001712 Out.PadToColumn(50);
Dan Gohmane2745262009-08-12 17:23:50 +00001713 Out << ";";
Gabor Greif6c6b2fd2010-03-25 23:25:28 +00001714 const_pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001715
Chris Lattnerff834c02008-04-22 02:45:44 +00001716 if (PI == PE) {
1717 Out << " No predecessors!";
1718 } else {
Dan Gohman81313fd2008-09-14 17:21:12 +00001719 Out << " preds = ";
Chris Lattnerff834c02008-04-22 02:45:44 +00001720 writeOperand(*PI, false);
1721 for (++PI; PI != PE; ++PI) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001722 Out << ", ";
Chris Lattner78e2e8b2006-12-06 06:24:27 +00001723 writeOperand(*PI, false);
Chris Lattner00211f12003-11-16 22:59:57 +00001724 }
Chris Lattner58185f22002-10-02 19:38:55 +00001725 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001726 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001727
Chris Lattnerff834c02008-04-22 02:45:44 +00001728 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001729
Misha Brukmana6619a92004-06-21 21:53:56 +00001730 if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001731
Chris Lattnerfee714f2001-09-07 16:36:04 +00001732 // Output all of the instructions in the basic block...
Dan Gohmana4f709e2009-07-13 18:27:59 +00001733 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
Daniel Maleaded9f932013-05-08 20:38:31 +00001734 printInstructionLine(*I);
Dan Gohmana4f709e2009-07-13 18:27:59 +00001735 }
Chris Lattner96cdd272004-03-08 18:51:45 +00001736
Misha Brukmana6619a92004-06-21 21:53:56 +00001737 if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, Out);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001738}
1739
Daniel Maleaded9f932013-05-08 20:38:31 +00001740/// printInstructionLine - Print an instruction and a newline character.
1741void AssemblyWriter::printInstructionLine(const Instruction &I) {
1742 printInstruction(I);
1743 Out << '\n';
1744}
1745
Misha Brukmanc566ca362004-03-02 00:22:19 +00001746/// printInfoComment - Print a little comment after the instruction indicating
1747/// which slot it occupies.
1748///
Chris Lattner113f4f42002-06-25 16:13:24 +00001749void AssemblyWriter::printInfoComment(const Value &V) {
Bill Wendling847a5c32013-04-16 20:55:47 +00001750 if (AnnotationWriter)
Dan Gohmane34890f2010-02-10 20:41:46 +00001751 AnnotationWriter->printInfoComment(V, Out);
Chris Lattner862e3382001-10-13 06:42:36 +00001752}
1753
Reid Spencere7141c82006-08-28 01:02:49 +00001754// This member is called for each Instruction in a function..
Chris Lattner113f4f42002-06-25 16:13:24 +00001755void AssemblyWriter::printInstruction(const Instruction &I) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001756 if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001757
Dan Gohman466876b2009-08-12 23:32:33 +00001758 // Print out indentation for an instruction.
Dan Gohmanb4583b12009-08-13 01:41:52 +00001759 Out << " ";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001760
1761 // Print out name if it exists...
Chris Lattner033935d2008-08-17 04:40:13 +00001762 if (I.hasName()) {
1763 PrintLLVMName(Out, &I);
1764 Out << " = ";
Chris Lattner031560c2009-12-29 07:25:48 +00001765 } else if (!I.getType()->isVoidTy()) {
Chris Lattnerb25e5ea2008-08-29 17:19:30 +00001766 // Print out the def slot taken.
1767 int SlotNum = Machine.getLocalSlot(&I);
1768 if (SlotNum == -1)
1769 Out << "<badref> = ";
1770 else
1771 Out << '%' << SlotNum << " = ";
Chris Lattner033935d2008-08-17 04:40:13 +00001772 }
Andrew Trickec4b6e72011-09-30 19:48:58 +00001773
Reid Kleckner5772b772014-04-24 20:14:34 +00001774 if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
1775 if (CI->isMustTailCall())
1776 Out << "musttail ";
1777 else if (CI->isTailCall())
1778 Out << "tail ";
1779 }
Chris Lattner504f9242003-09-08 17:45:59 +00001780
Chris Lattner2f7c9632001-06-06 20:29:01 +00001781 // Print out the opcode...
Misha Brukmana6619a92004-06-21 21:53:56 +00001782 Out << I.getOpcodeName();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001783
Eli Friedman02e737b2011-08-12 22:50:01 +00001784 // If this is an atomic load or store, print out the atomic marker.
1785 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isAtomic()) ||
1786 (isa<StoreInst>(I) && cast<StoreInst>(I).isAtomic()))
1787 Out << " atomic";
1788
1789 // If this is a volatile operation, print out the volatile marker.
1790 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) ||
1791 (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile()) ||
1792 (isa<AtomicCmpXchgInst>(I) && cast<AtomicCmpXchgInst>(I).isVolatile()) ||
1793 (isa<AtomicRMWInst>(I) && cast<AtomicRMWInst>(I).isVolatile()))
1794 Out << " volatile";
1795
Dan Gohman9c7f8082009-07-27 16:11:46 +00001796 // Print out optimization information.
1797 WriteOptimizationInfo(Out, &I);
1798
Reid Spencer45e52392006-12-03 06:27:29 +00001799 // Print out the compare instruction predicates
Nate Begemand2195702008-05-12 19:01:56 +00001800 if (const CmpInst *CI = dyn_cast<CmpInst>(&I))
Chris Lattner82ff9232008-08-23 22:52:27 +00001801 Out << ' ' << getPredicateText(CI->getPredicate());
Reid Spencer45e52392006-12-03 06:27:29 +00001802
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001803 // Print out the atomicrmw operation
1804 if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(&I))
1805 writeAtomicRMWOperation(Out, RMWI->getOperation());
1806
Chris Lattner2f7c9632001-06-06 20:29:01 +00001807 // Print out the type of the operands...
Craig Topperc6207612014-04-09 06:08:46 +00001808 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : nullptr;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001809
1810 // Special case conditional branches to swizzle the condition out to the front
Gabor Greifcab008f2009-02-09 15:45:06 +00001811 if (isa<BranchInst>(I) && cast<BranchInst>(I).isConditional()) {
David Blaikieb78e9e52013-02-11 01:16:51 +00001812 const BranchInst &BI(cast<BranchInst>(I));
Dan Gohman81313fd2008-09-14 17:21:12 +00001813 Out << ' ';
Gabor Greifcab008f2009-02-09 15:45:06 +00001814 writeOperand(BI.getCondition(), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00001815 Out << ", ";
Gabor Greifcab008f2009-02-09 15:45:06 +00001816 writeOperand(BI.getSuccessor(0), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00001817 Out << ", ";
Gabor Greifcab008f2009-02-09 15:45:06 +00001818 writeOperand(BI.getSuccessor(1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001819
Chris Lattner8d48df22002-04-13 18:34:38 +00001820 } else if (isa<SwitchInst>(I)) {
David Blaikieb78e9e52013-02-11 01:16:51 +00001821 const SwitchInst& SI(cast<SwitchInst>(I));
Chris Lattner3ed871f2009-10-27 19:13:16 +00001822 // Special case switch instruction to get formatting nice and correct.
Dan Gohman81313fd2008-09-14 17:21:12 +00001823 Out << ' ';
Eli Friedman95031ed2011-09-29 20:21:17 +00001824 writeOperand(SI.getCondition(), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00001825 Out << ", ";
Eli Friedman95031ed2011-09-29 20:21:17 +00001826 writeOperand(SI.getDefaultDest(), true);
Chris Lattner82ff9232008-08-23 22:52:27 +00001827 Out << " [";
David Blaikieb78e9e52013-02-11 01:16:51 +00001828 for (SwitchInst::ConstCaseIt i = SI.case_begin(), e = SI.case_end();
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00001829 i != e; ++i) {
Dan Gohmanb4583b12009-08-13 01:41:52 +00001830 Out << "\n ";
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00001831 writeOperand(i.getCaseValue(), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00001832 Out << ", ";
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00001833 writeOperand(i.getCaseSuccessor(), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001834 }
Dan Gohmanb4583b12009-08-13 01:41:52 +00001835 Out << "\n ]";
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00001836 } else if (isa<IndirectBrInst>(I)) {
1837 // Special case indirectbr instruction to get formatting nice and correct.
Chris Lattner3ed871f2009-10-27 19:13:16 +00001838 Out << ' ';
1839 writeOperand(Operand, true);
Dan Gohman43c57402009-10-30 02:01:10 +00001840 Out << ", [";
Andrew Trickec4b6e72011-09-30 19:48:58 +00001841
Chris Lattner3ed871f2009-10-27 19:13:16 +00001842 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) {
1843 if (i != 1)
1844 Out << ", ";
1845 writeOperand(I.getOperand(i), true);
1846 }
1847 Out << ']';
Jay Foad372ad642011-06-20 14:18:48 +00001848 } else if (const PHINode *PN = dyn_cast<PHINode>(&I)) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001849 Out << ' ';
Chris Lattnere101c442009-02-28 21:26:53 +00001850 TypePrinter.print(I.getType(), Out);
Misha Brukmana6619a92004-06-21 21:53:56 +00001851 Out << ' ';
Chris Lattner2f7c9632001-06-06 20:29:01 +00001852
Jay Foad372ad642011-06-20 14:18:48 +00001853 for (unsigned op = 0, Eop = PN->getNumIncomingValues(); op < Eop; ++op) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001854 if (op) Out << ", ";
Dan Gohman81313fd2008-09-14 17:21:12 +00001855 Out << "[ ";
Jay Foad372ad642011-06-20 14:18:48 +00001856 writeOperand(PN->getIncomingValue(op), false); Out << ", ";
1857 writeOperand(PN->getIncomingBlock(op), false); Out << " ]";
Chris Lattner931ef3b2001-06-11 15:04:20 +00001858 }
Dan Gohmana76f0f72008-05-31 19:12:39 +00001859 } else if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(&I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001860 Out << ' ';
Dan Gohmana76f0f72008-05-31 19:12:39 +00001861 writeOperand(I.getOperand(0), true);
1862 for (const unsigned *i = EVI->idx_begin(), *e = EVI->idx_end(); i != e; ++i)
1863 Out << ", " << *i;
1864 } else if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(&I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001865 Out << ' ';
1866 writeOperand(I.getOperand(0), true); Out << ", ";
Dan Gohmana76f0f72008-05-31 19:12:39 +00001867 writeOperand(I.getOperand(1), true);
1868 for (const unsigned *i = IVI->idx_begin(), *e = IVI->idx_end(); i != e; ++i)
1869 Out << ", " << *i;
Bill Wendlingfae14752011-08-12 20:24:12 +00001870 } else if (const LandingPadInst *LPI = dyn_cast<LandingPadInst>(&I)) {
1871 Out << ' ';
1872 TypePrinter.print(I.getType(), Out);
1873 Out << " personality ";
1874 writeOperand(I.getOperand(0), true); Out << '\n';
1875
1876 if (LPI->isCleanup())
1877 Out << " cleanup";
1878
1879 for (unsigned i = 0, e = LPI->getNumClauses(); i != e; ++i) {
1880 if (i != 0 || LPI->isCleanup()) Out << "\n";
1881 if (LPI->isCatch(i))
1882 Out << " catch ";
1883 else
1884 Out << " filter ";
1885
1886 writeOperand(LPI->getClause(i), true);
1887 }
Devang Patel59643e52008-02-23 00:35:18 +00001888 } else if (isa<ReturnInst>(I) && !Operand) {
1889 Out << " void";
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001890 } else if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
1891 // Print the calling convention being used.
Micah Villmow857186d2012-09-13 15:11:12 +00001892 if (CI->getCallingConv() != CallingConv::C) {
1893 Out << " ";
1894 PrintCallingConv(CI->getCallingConv(), Out);
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001895 }
1896
Gabor Greifc9a92512010-06-23 13:09:06 +00001897 Operand = CI->getCalledValue();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001898 PointerType *PTy = cast<PointerType>(Operand->getType());
1899 FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1900 Type *RetTy = FTy->getReturnType();
Bill Wendlinge94d8432012-12-07 23:16:57 +00001901 const AttributeSet &PAL = CI->getAttributes();
Chris Lattner2f2d9472001-11-06 21:28:12 +00001902
Bill Wendling658d24d2013-01-18 21:53:16 +00001903 if (PAL.hasAttributes(AttributeSet::ReturnIndex))
1904 Out << ' ' << PAL.getAsString(AttributeSet::ReturnIndex);
Devang Patel221fe422008-09-29 20:49:50 +00001905
Chris Lattner463d6a52003-08-05 15:34:45 +00001906 // If possible, print out the short form of the call instruction. We can
Chris Lattner6915f8f2002-04-07 22:49:37 +00001907 // only do this if the first argument is a pointer to a nonvararg function,
Chris Lattner463d6a52003-08-05 15:34:45 +00001908 // and if the return type is not a pointer to a function.
Chris Lattner2f2d9472001-11-06 21:28:12 +00001909 //
Dan Gohman81313fd2008-09-14 17:21:12 +00001910 Out << ' ';
Chris Lattner463d6a52003-08-05 15:34:45 +00001911 if (!FTy->isVarArg() &&
Duncan Sands19d0b472010-02-16 11:11:14 +00001912 (!RetTy->isPointerTy() ||
1913 !cast<PointerType>(RetTy)->getElementType()->isFunctionTy())) {
Chris Lattnere101c442009-02-28 21:26:53 +00001914 TypePrinter.print(RetTy, Out);
Dan Gohman81313fd2008-09-14 17:21:12 +00001915 Out << ' ';
Chris Lattner2f2d9472001-11-06 21:28:12 +00001916 writeOperand(Operand, false);
1917 } else {
1918 writeOperand(Operand, true);
1919 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001920 Out << '(';
Gabor Greifc9a92512010-06-23 13:09:06 +00001921 for (unsigned op = 0, Eop = CI->getNumArgOperands(); op < Eop; ++op) {
1922 if (op > 0)
Dan Gohman81313fd2008-09-14 17:21:12 +00001923 Out << ", ";
Bill Wendling749a43d2012-12-30 13:50:49 +00001924 writeParamOperand(CI->getArgOperand(op), PAL, op + 1);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001925 }
Dan Gohman81313fd2008-09-14 17:21:12 +00001926 Out << ')';
Bill Wendling698e84f2012-12-30 10:32:01 +00001927 if (PAL.hasAttributes(AttributeSet::FunctionIndex))
Bill Wendlinga0323742013-02-22 09:09:42 +00001928 Out << " #" << Machine.getAttributeGroupSlot(PAL.getFnAttributes());
Chris Lattner113f4f42002-06-25 16:13:24 +00001929 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
Gabor Greifa2fbc0a2010-03-24 13:21:49 +00001930 Operand = II->getCalledValue();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001931 PointerType *PTy = cast<PointerType>(Operand->getType());
1932 FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1933 Type *RetTy = FTy->getReturnType();
Bill Wendlinge94d8432012-12-07 23:16:57 +00001934 const AttributeSet &PAL = II->getAttributes();
Chris Lattner463d6a52003-08-05 15:34:45 +00001935
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001936 // Print the calling convention being used.
Micah Villmow857186d2012-09-13 15:11:12 +00001937 if (II->getCallingConv() != CallingConv::C) {
1938 Out << " ";
1939 PrintCallingConv(II->getCallingConv(), Out);
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001940 }
1941
Bill Wendling658d24d2013-01-18 21:53:16 +00001942 if (PAL.hasAttributes(AttributeSet::ReturnIndex))
1943 Out << ' ' << PAL.getAsString(AttributeSet::ReturnIndex);
Devang Patel221fe422008-09-29 20:49:50 +00001944
Chris Lattner463d6a52003-08-05 15:34:45 +00001945 // If possible, print out the short form of the invoke instruction. We can
1946 // only do this if the first argument is a pointer to a nonvararg function,
1947 // and if the return type is not a pointer to a function.
1948 //
Dan Gohmanc7e00ba2008-10-15 18:02:08 +00001949 Out << ' ';
Chris Lattner463d6a52003-08-05 15:34:45 +00001950 if (!FTy->isVarArg() &&
Duncan Sands19d0b472010-02-16 11:11:14 +00001951 (!RetTy->isPointerTy() ||
1952 !cast<PointerType>(RetTy)->getElementType()->isFunctionTy())) {
Chris Lattnere101c442009-02-28 21:26:53 +00001953 TypePrinter.print(RetTy, Out);
Dan Gohmanc7e00ba2008-10-15 18:02:08 +00001954 Out << ' ';
Chris Lattner463d6a52003-08-05 15:34:45 +00001955 writeOperand(Operand, false);
1956 } else {
1957 writeOperand(Operand, true);
1958 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001959 Out << '(';
Gabor Greifc9a92512010-06-23 13:09:06 +00001960 for (unsigned op = 0, Eop = II->getNumArgOperands(); op < Eop; ++op) {
Gabor Greifa2fbc0a2010-03-24 13:21:49 +00001961 if (op)
Dan Gohman81313fd2008-09-14 17:21:12 +00001962 Out << ", ";
Bill Wendling749a43d2012-12-30 13:50:49 +00001963 writeParamOperand(II->getArgOperand(op), PAL, op + 1);
Chris Lattner862e3382001-10-13 06:42:36 +00001964 }
1965
Dan Gohman81313fd2008-09-14 17:21:12 +00001966 Out << ')';
Bill Wendling698e84f2012-12-30 10:32:01 +00001967 if (PAL.hasAttributes(AttributeSet::FunctionIndex))
Bill Wendlinga0323742013-02-22 09:09:42 +00001968 Out << " #" << Machine.getAttributeGroupSlot(PAL.getFnAttributes());
Devang Patela05633e2008-09-26 22:53:05 +00001969
Dan Gohmanb4583b12009-08-13 01:41:52 +00001970 Out << "\n to ";
Chris Lattner862e3382001-10-13 06:42:36 +00001971 writeOperand(II->getNormalDest(), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00001972 Out << " unwind ";
Chris Lattnerfae8ab32004-02-08 21:44:31 +00001973 writeOperand(II->getUnwindDest(), true);
Chris Lattner862e3382001-10-13 06:42:36 +00001974
Victor Hernandez8acf2952009-10-23 21:09:37 +00001975 } else if (const AllocaInst *AI = dyn_cast<AllocaInst>(&I)) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001976 Out << ' ';
Reid Kleckner20844032014-01-25 01:24:06 +00001977 if (AI->isUsedWithInAlloca())
David Majnemerc4ab61c2014-03-09 06:41:58 +00001978 Out << "inalloca ";
1979 TypePrinter.print(AI->getAllocatedType(), Out);
Dan Gohmanb53e26c2009-07-31 18:23:24 +00001980 if (!AI->getArraySize() || AI->isArrayAllocation()) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001981 Out << ", ";
Chris Lattner8d48df22002-04-13 18:34:38 +00001982 writeOperand(AI->getArraySize(), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001983 }
Nate Begeman848622f2005-11-05 09:21:28 +00001984 if (AI->getAlignment()) {
Chris Lattner7aeee3a2005-11-05 21:20:34 +00001985 Out << ", align " << AI->getAlignment();
Nate Begeman848622f2005-11-05 09:21:28 +00001986 }
Chris Lattner862e3382001-10-13 06:42:36 +00001987 } else if (isa<CastInst>(I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001988 if (Operand) {
1989 Out << ' ';
1990 writeOperand(Operand, true); // Work with broken code
1991 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001992 Out << " to ";
Chris Lattnere101c442009-02-28 21:26:53 +00001993 TypePrinter.print(I.getType(), Out);
Chris Lattner5b337482003-10-18 05:57:43 +00001994 } else if (isa<VAArgInst>(I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001995 if (Operand) {
1996 Out << ' ';
1997 writeOperand(Operand, true); // Work with broken code
1998 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001999 Out << ", ";
Chris Lattnere101c442009-02-28 21:26:53 +00002000 TypePrinter.print(I.getType(), Out);
2001 } else if (Operand) { // Print the normal way.
Chris Lattner2f7c9632001-06-06 20:29:01 +00002002
Misha Brukmanb1c93172005-04-21 23:48:37 +00002003 // PrintAllTypes - Instructions who have operands of all the same type
Chris Lattner2f7c9632001-06-06 20:29:01 +00002004 // omit the type from all but the first operand. If the instruction has
2005 // different type operands (for example br), then they are all printed.
2006 bool PrintAllTypes = false;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002007 Type *TheType = Operand->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +00002008
Reid Spencer0cdd04f2007-02-02 13:54:55 +00002009 // Select, Store and ShuffleVector always print all types.
Devang Patelce556d92008-03-04 22:05:14 +00002010 if (isa<SelectInst>(I) || isa<StoreInst>(I) || isa<ShuffleVectorInst>(I)
2011 || isa<ReturnInst>(I)) {
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00002012 PrintAllTypes = true;
2013 } else {
2014 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
2015 Operand = I.getOperand(i);
Nuno Lopes04fe2f02009-01-15 18:40:57 +00002016 // note that Operand shouldn't be null, but the test helps make dump()
2017 // more tolerant of malformed IR
Nuno Lopes0971e772009-01-14 17:51:41 +00002018 if (Operand && Operand->getType() != TheType) {
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00002019 PrintAllTypes = true; // We have differing types! Print them all!
2020 break;
2021 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00002022 }
2023 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00002024
Chris Lattner7bfee412001-10-29 16:05:51 +00002025 if (!PrintAllTypes) {
Misha Brukmana6619a92004-06-21 21:53:56 +00002026 Out << ' ';
Chris Lattnere101c442009-02-28 21:26:53 +00002027 TypePrinter.print(TheType, Out);
Chris Lattner7bfee412001-10-29 16:05:51 +00002028 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00002029
Dan Gohman81313fd2008-09-14 17:21:12 +00002030 Out << ' ';
Chris Lattner113f4f42002-06-25 16:13:24 +00002031 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
Dan Gohman81313fd2008-09-14 17:21:12 +00002032 if (i) Out << ", ";
Chris Lattner113f4f42002-06-25 16:13:24 +00002033 writeOperand(I.getOperand(i), PrintAllTypes);
Chris Lattner2f7c9632001-06-06 20:29:01 +00002034 }
2035 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002036
Eli Friedman59b66882011-08-09 23:02:53 +00002037 // Print atomic ordering/alignment for memory operations
2038 if (const LoadInst *LI = dyn_cast<LoadInst>(&I)) {
2039 if (LI->isAtomic())
2040 writeAtomic(LI->getOrdering(), LI->getSynchScope());
2041 if (LI->getAlignment())
2042 Out << ", align " << LI->getAlignment();
2043 } else if (const StoreInst *SI = dyn_cast<StoreInst>(&I)) {
2044 if (SI->isAtomic())
2045 writeAtomic(SI->getOrdering(), SI->getSynchScope());
2046 if (SI->getAlignment())
2047 Out << ", align " << SI->getAlignment();
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002048 } else if (const AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(&I)) {
Tim Northovere94a5182014-03-11 10:48:52 +00002049 writeAtomicCmpXchg(CXI->getSuccessOrdering(), CXI->getFailureOrdering(),
2050 CXI->getSynchScope());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002051 } else if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(&I)) {
2052 writeAtomic(RMWI->getOrdering(), RMWI->getSynchScope());
Eli Friedmanfee02c62011-07-25 23:16:38 +00002053 } else if (const FenceInst *FI = dyn_cast<FenceInst>(&I)) {
2054 writeAtomic(FI->getOrdering(), FI->getSynchScope());
Christopher Lamb84485702007-04-22 19:24:39 +00002055 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00002056
Chris Lattnerc9558df2009-12-28 20:10:43 +00002057 // Print Metadata info.
Nick Lewyckyba8ec9a2010-02-25 06:53:04 +00002058 SmallVector<std::pair<unsigned, MDNode*>, 4> InstMD;
2059 I.getAllMetadata(InstMD);
Erick Tryzelaar72a37132010-03-02 05:32:52 +00002060 if (!InstMD.empty()) {
2061 SmallVector<StringRef, 8> MDNames;
2062 I.getType()->getContext().getMDKindNames(MDNames);
2063 for (unsigned i = 0, e = InstMD.size(); i != e; ++i) {
2064 unsigned Kind = InstMD[i].first;
2065 if (Kind < MDNames.size()) {
2066 Out << ", !" << MDNames[Kind];
Daniel Maleaded9f932013-05-08 20:38:31 +00002067 } else {
2068 Out << ", !<unknown kind #" << Kind << ">";
2069 }
Dan Gohmana2489d12010-07-20 23:55:01 +00002070 Out << ' ';
2071 WriteAsOperandInternal(Out, InstMD[i].second, &TypePrinter, &Machine,
2072 TheModule);
Nick Lewyckyba8ec9a2010-02-25 06:53:04 +00002073 }
Devang Patelbcdb0252009-10-07 16:37:55 +00002074 }
Chris Lattner862e3382001-10-13 06:42:36 +00002075 printInfoComment(I);
Chris Lattner2f7c9632001-06-06 20:29:01 +00002076}
2077
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002078static void WriteMDNodeComment(const MDNode *Node,
Duncan Sands41b4a6b2010-07-12 08:16:59 +00002079 formatted_raw_ostream &Out) {
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002080 if (Node->getNumOperands() < 1)
2081 return;
Bill Wendling5cb50c52012-06-28 00:41:44 +00002082
2083 Value *Op = Node->getOperand(0);
2084 if (!Op || !isa<ConstantInt>(Op) || cast<ConstantInt>(Op)->getBitWidth() < 32)
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002085 return;
Andrew Trickec4b6e72011-09-30 19:48:58 +00002086
Bill Wendling5cb50c52012-06-28 00:41:44 +00002087 DIDescriptor Desc(Node);
David Blaikiedc69ebb2013-03-11 23:39:23 +00002088 if (!Desc.Verify())
Bill Wendling5cb50c52012-06-28 00:41:44 +00002089 return;
2090
2091 unsigned Tag = Desc.getTag();
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002092 Out.PadToColumn(50);
Bill Wendlinga0bc1082012-07-03 20:01:02 +00002093 if (dwarf::TagString(Tag)) {
2094 Out << "; ";
2095 Desc.print(Out);
2096 } else if (Tag == dwarf::DW_TAG_user_base) {
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002097 Out << "; [ DW_TAG_user_base ]";
Bill Wendlinga0bc1082012-07-03 20:01:02 +00002098 }
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002099}
2100
Daniel Maleaded9f932013-05-08 20:38:31 +00002101void AssemblyWriter::writeMDNode(unsigned Slot, const MDNode *Node) {
2102 Out << '!' << Slot << " = metadata ";
2103 printMDNodeBody(Node);
2104}
2105
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002106void AssemblyWriter::writeAllMDNodes() {
2107 SmallVector<const MDNode *, 16> Nodes;
Chris Lattnercf4a76e2009-12-31 02:20:11 +00002108 Nodes.resize(Machine.mdn_size());
2109 for (SlotTracker::mdn_iterator I = Machine.mdn_begin(), E = Machine.mdn_end();
2110 I != E; ++I)
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002111 Nodes[I->second] = cast<MDNode>(I->first);
Andrew Trickec4b6e72011-09-30 19:48:58 +00002112
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002113 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Daniel Maleaded9f932013-05-08 20:38:31 +00002114 writeMDNode(i, Nodes[i]);
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002115 }
2116}
2117
2118void AssemblyWriter::printMDNodeBody(const MDNode *Node) {
Dan Gohmana2489d12010-07-20 23:55:01 +00002119 WriteMDNodeBodyInternal(Out, Node, &TypePrinter, &Machine, TheModule);
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002120 WriteMDNodeComment(Node, Out);
2121 Out << "\n";
2122}
Chris Lattner2f7c9632001-06-06 20:29:01 +00002123
Bill Wendling829b4782013-02-11 08:43:33 +00002124void AssemblyWriter::writeAllAttributeGroups() {
2125 std::vector<std::pair<AttributeSet, unsigned> > asVec;
2126 asVec.resize(Machine.as_size());
2127
2128 for (SlotTracker::as_iterator I = Machine.as_begin(), E = Machine.as_end();
2129 I != E; ++I)
2130 asVec[I->second] = *I;
2131
2132 for (std::vector<std::pair<AttributeSet, unsigned> >::iterator
2133 I = asVec.begin(), E = asVec.end(); I != E; ++I)
2134 Out << "attributes #" << I->second << " = { "
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +00002135 << I->first.getAsString(AttributeSet::FunctionIndex, true) << " }\n";
Bill Wendling829b4782013-02-11 08:43:33 +00002136}
2137
Daniel Maleaded9f932013-05-08 20:38:31 +00002138} // namespace llvm
2139
Chris Lattner2f7c9632001-06-06 20:29:01 +00002140//===----------------------------------------------------------------------===//
2141// External Interface declarations
2142//===----------------------------------------------------------------------===//
2143
Dan Gohmane2745262009-08-12 17:23:50 +00002144void Module::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW) const {
Chris Lattnere36fd8a2008-08-19 04:26:57 +00002145 SlotTracker SlotTable(this);
Dan Gohmane2745262009-08-12 17:23:50 +00002146 formatted_raw_ostream OS(ROS);
Chris Lattner0c19df42008-08-23 22:23:09 +00002147 AssemblyWriter W(OS, SlotTable, this, AAW);
Chris Lattnerefb5e392009-12-31 02:23:35 +00002148 W.printModule(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00002149}
2150
Rafael Espindola69927782014-04-23 12:23:05 +00002151void NamedMDNode::print(raw_ostream &ROS) const {
Dan Gohman2637cc12010-07-21 23:38:33 +00002152 SlotTracker SlotTable(getParent());
2153 formatted_raw_ostream OS(ROS);
Rafael Espindola69927782014-04-23 12:23:05 +00002154 AssemblyWriter W(OS, SlotTable, getParent(), nullptr);
Dan Gohman2637cc12010-07-21 23:38:33 +00002155 W.printNamedMDNode(this);
2156}
2157
Chris Lattner2ee62d22009-02-28 21:11:05 +00002158void Type::print(raw_ostream &OS) const {
Craig Topperc6207612014-04-09 06:08:46 +00002159 if (!this) {
Chris Lattner2ee62d22009-02-28 21:11:05 +00002160 OS << "<null Type>";
2161 return;
2162 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002163 TypePrinting TP;
2164 TP.print(const_cast<Type*>(this), OS);
Andrew Trickec4b6e72011-09-30 19:48:58 +00002165
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002166 // If the type is a named struct type, print the body as well.
2167 if (StructType *STy = dyn_cast<StructType>(const_cast<Type*>(this)))
Chris Lattner01beceb2011-08-12 18:07:07 +00002168 if (!STy->isLiteral()) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002169 OS << " = type ";
2170 TP.printStructBody(STy, OS);
2171 }
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00002172}
2173
David Blaikieb38ac1f2014-04-05 23:33:25 +00002174void Value::print(raw_ostream &ROS) const {
2175 if (!this) {
Dan Gohman12dad632009-08-12 20:56:03 +00002176 ROS << "printing a <null> value\n";
Chris Lattner0c19df42008-08-23 22:23:09 +00002177 return;
2178 }
Dan Gohman12dad632009-08-12 20:56:03 +00002179 formatted_raw_ostream OS(ROS);
Chris Lattner0c19df42008-08-23 22:23:09 +00002180 if (const Instruction *I = dyn_cast<Instruction>(this)) {
David Blaikieb38ac1f2014-04-05 23:33:25 +00002181 const Function *F = I->getParent() ? I->getParent()->getParent() : nullptr;
Chris Lattner0c19df42008-08-23 22:23:09 +00002182 SlotTracker SlotTable(F);
David Blaikieb38ac1f2014-04-05 23:33:25 +00002183 AssemblyWriter W(OS, SlotTable, getModuleFromVal(I), nullptr);
Chris Lattnerefb5e392009-12-31 02:23:35 +00002184 W.printInstruction(*I);
Chris Lattner0c19df42008-08-23 22:23:09 +00002185 } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(this)) {
2186 SlotTracker SlotTable(BB->getParent());
David Blaikieb38ac1f2014-04-05 23:33:25 +00002187 AssemblyWriter W(OS, SlotTable, getModuleFromVal(BB), nullptr);
Chris Lattnerefb5e392009-12-31 02:23:35 +00002188 W.printBasicBlock(BB);
Chris Lattner0c19df42008-08-23 22:23:09 +00002189 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(this)) {
2190 SlotTracker SlotTable(GV->getParent());
David Blaikieb38ac1f2014-04-05 23:33:25 +00002191 AssemblyWriter W(OS, SlotTable, GV->getParent(), nullptr);
Chris Lattnerefb5e392009-12-31 02:23:35 +00002192 if (const GlobalVariable *V = dyn_cast<GlobalVariable>(GV))
2193 W.printGlobal(V);
2194 else if (const Function *F = dyn_cast<Function>(GV))
2195 W.printFunction(F);
2196 else
2197 W.printAlias(cast<GlobalAlias>(GV));
Devang Patel5546b982009-07-01 20:59:15 +00002198 } else if (const MDNode *N = dyn_cast<MDNode>(this)) {
Victor Hernandeze5f2af72010-01-20 04:45:57 +00002199 const Function *F = N->getFunction();
Victor Hernandez61e6e822010-01-14 01:47:37 +00002200 SlotTracker SlotTable(F);
David Blaikieb38ac1f2014-04-05 23:33:25 +00002201 AssemblyWriter W(OS, SlotTable, F ? F->getParent() : nullptr, nullptr);
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002202 W.printMDNodeBody(N);
Chris Lattner0c19df42008-08-23 22:23:09 +00002203 } else if (const Constant *C = dyn_cast<Constant>(this)) {
Chris Lattner92c5c122009-02-28 23:20:19 +00002204 TypePrinting TypePrinter;
Chris Lattnere101c442009-02-28 21:26:53 +00002205 TypePrinter.print(C->getType(), OS);
Chris Lattner2ee62d22009-02-28 21:11:05 +00002206 OS << ' ';
David Blaikieb38ac1f2014-04-05 23:33:25 +00002207 WriteConstantInternal(OS, C, TypePrinter, nullptr, nullptr);
Chris Lattner31fcc282009-12-31 01:41:14 +00002208 } else if (isa<InlineAsm>(this) || isa<MDString>(this) ||
2209 isa<Argument>(this)) {
Chandler Carruthd48cdbf2014-01-09 02:29:41 +00002210 this->printAsOperand(OS);
Chris Lattner0c19df42008-08-23 22:23:09 +00002211 } else {
Nick Lewyckyad1b3d12014-05-09 00:49:03 +00002212 llvm_unreachable("Unknown value to print out!");
Chris Lattner0c19df42008-08-23 22:23:09 +00002213 }
2214}
2215
Chandler Carruthd48cdbf2014-01-09 02:29:41 +00002216void Value::printAsOperand(raw_ostream &O, bool PrintType, const Module *M) const {
2217 // Fast path: Don't construct and populate a TypePrinting object if we
2218 // won't be needing any types printed.
2219 if (!PrintType &&
2220 ((!isa<Constant>(this) && !isa<MDNode>(this)) ||
2221 hasName() || isa<GlobalValue>(this))) {
Craig Topperc6207612014-04-09 06:08:46 +00002222 WriteAsOperandInternal(O, this, nullptr, nullptr, M);
Chandler Carruthd48cdbf2014-01-09 02:29:41 +00002223 return;
2224 }
2225
2226 if (!M)
2227 M = getModuleFromVal(this);
2228
2229 TypePrinting TypePrinter;
2230 if (M)
2231 TypePrinter.incorporateTypes(*M);
2232 if (PrintType) {
2233 TypePrinter.print(getType(), O);
2234 O << ' ';
2235 }
2236
Craig Topperc6207612014-04-09 06:08:46 +00002237 WriteAsOperandInternal(O, this, &TypePrinter, nullptr, M);
Chandler Carruthd48cdbf2014-01-09 02:29:41 +00002238}
2239
Chris Lattnerdab942552008-08-25 17:03:15 +00002240// Value::dump - allow easy printing of Values from the debugger.
David Greenec7f9b122010-01-05 01:29:26 +00002241void Value::dump() const { print(dbgs()); dbgs() << '\n'; }
Reid Spencer52641832004-05-25 18:14:38 +00002242
Chris Lattnerdab942552008-08-25 17:03:15 +00002243// Type::dump - allow easy printing of Types from the debugger.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002244void Type::dump() const { print(dbgs()); }
Chris Lattner24025262009-02-28 21:05:51 +00002245
Chris Lattnerdab942552008-08-25 17:03:15 +00002246// Module::dump() - Allow printing of Modules from the debugger.
Craig Topperc6207612014-04-09 06:08:46 +00002247void Module::dump() const { print(dbgs(), nullptr); }
Bill Wendling3681d112011-12-09 23:18:34 +00002248
2249// NamedMDNode::dump() - Allow printing of NamedMDNodes from the debugger.
Rafael Espindola69927782014-04-23 12:23:05 +00002250void NamedMDNode::dump() const { print(dbgs()); }