blob: 3e716d13d3585904e0f8b9ed87f408eb5ffb6cbe [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;
Reid Kleckner1c843222014-01-31 17:41:22 +000081 case CallingConv::X86_CDeclMethod:Out << "x86_cdeclmethodcc"; break;
Bill Wendling90bc19c2013-02-20 07:21:42 +000082 case CallingConv::Intel_OCL_BI: Out << "intel_ocl_bicc"; break;
83 case CallingConv::ARM_APCS: Out << "arm_apcscc"; break;
84 case CallingConv::ARM_AAPCS: Out << "arm_aapcscc"; break;
85 case CallingConv::ARM_AAPCS_VFP: Out << "arm_aapcs_vfpcc"; break;
86 case CallingConv::MSP430_INTR: Out << "msp430_intrcc"; break;
87 case CallingConv::PTX_Kernel: Out << "ptx_kernel"; break;
88 case CallingConv::PTX_Device: Out << "ptx_device"; break;
Charles Davise8f297c2013-07-12 06:02:35 +000089 case CallingConv::X86_64_SysV: Out << "x86_64_sysvcc"; break;
90 case CallingConv::X86_64_Win64: Out << "x86_64_win64cc"; break;
Michael Kupersteine31b4862013-12-15 10:01:20 +000091 case CallingConv::SPIR_FUNC: Out << "spir_func"; break;
92 case CallingConv::SPIR_KERNEL: Out << "spir_kernel"; break;
Micah Villmow857186d2012-09-13 15:11:12 +000093 }
94}
Michael Ilseman26ee2b82012-11-15 22:34:00 +000095
Daniel Dunbardb0b70a2008-10-28 19:33:02 +000096// PrintEscapedString - Print each character of the specified string, escaping
97// it if it is not printable or if it is an escape char.
Chris Lattner9380b812010-07-07 23:16:37 +000098static void PrintEscapedString(StringRef Name, raw_ostream &Out) {
Daniel Dunbare03eecb2009-07-25 23:55:21 +000099 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
100 unsigned char C = Name[i];
Nick Lewycky5aa592a2009-03-15 06:39:52 +0000101 if (isprint(C) && C != '\\' && C != '"')
Daniel Dunbardb0b70a2008-10-28 19:33:02 +0000102 Out << C;
103 else
104 Out << '\\' << hexdigit(C >> 4) << hexdigit(C & 0x0F);
105 }
106}
107
Chris Lattner3eee99c2008-08-19 04:36:02 +0000108enum PrefixType {
109 GlobalPrefix,
110 LabelPrefix,
Daniel Dunbar389529a2008-10-14 23:28:09 +0000111 LocalPrefix,
112 NoPrefix
Chris Lattner3eee99c2008-08-19 04:36:02 +0000113};
114
115/// PrintLLVMName - Turn the specified name into an 'LLVM name', which is either
116/// prefixed with % (if the string only contains simple characters) or is
117/// surrounded with ""'s (if it has special chars in it). Print it out.
Benjamin Kramer92d89982010-07-14 22:38:02 +0000118static void PrintLLVMName(raw_ostream &OS, StringRef Name, PrefixType Prefix) {
Jay Foadf7adb322011-04-24 14:30:00 +0000119 assert(!Name.empty() && "Cannot get empty name!");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000120 switch (Prefix) {
Daniel Dunbar389529a2008-10-14 23:28:09 +0000121 case NoPrefix: break;
Chris Lattner1508d3f2008-08-19 05:16:28 +0000122 case GlobalPrefix: OS << '@'; break;
123 case LabelPrefix: break;
124 case LocalPrefix: OS << '%'; break;
Nick Lewycky063699a2009-03-19 06:31:22 +0000125 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000126
Chris Lattner3eee99c2008-08-19 04:36:02 +0000127 // Scan the name to see if it needs quotes first.
Guy Benyei83c74e92013-02-12 21:21:59 +0000128 bool NeedsQuotes = isdigit(static_cast<unsigned char>(Name[0]));
Chris Lattner3eee99c2008-08-19 04:36:02 +0000129 if (!NeedsQuotes) {
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000130 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
Aaron Ballmaned9b0a92012-07-16 16:18:18 +0000131 // By making this unsigned, the value passed in to isalnum will always be
132 // in the range 0-255. This is important when building with MSVC because
133 // its implementation will assert. This situation can arise when dealing
134 // with UTF-8 multibyte characters.
135 unsigned char C = Name[i];
Guy Benyei83c74e92013-02-12 21:21:59 +0000136 if (!isalnum(static_cast<unsigned char>(C)) && C != '-' && C != '.' &&
137 C != '_') {
Chris Lattner3eee99c2008-08-19 04:36:02 +0000138 NeedsQuotes = true;
139 break;
140 }
141 }
142 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000143
Chris Lattner3eee99c2008-08-19 04:36:02 +0000144 // If we didn't need any quotes, just write out the name in one blast.
145 if (!NeedsQuotes) {
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000146 OS << Name;
Chris Lattner3eee99c2008-08-19 04:36:02 +0000147 return;
148 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000149
Chris Lattner3eee99c2008-08-19 04:36:02 +0000150 // Okay, we need quotes. Output the quotes and escape any scary characters as
151 // needed.
152 OS << '"';
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000153 PrintEscapedString(Name, OS);
Chris Lattner3eee99c2008-08-19 04:36:02 +0000154 OS << '"';
155}
156
157/// PrintLLVMName - Turn the specified name into an 'LLVM name', which is either
158/// prefixed with % (if the string only contains simple characters) or is
159/// surrounded with ""'s (if it has special chars in it). Print it out.
Dan Gohman12dad632009-08-12 20:56:03 +0000160static void PrintLLVMName(raw_ostream &OS, const Value *V) {
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000161 PrintLLVMName(OS, V->getName(),
Chris Lattner3eee99c2008-08-19 04:36:02 +0000162 isa<GlobalValue>(V) ? GlobalPrefix : LocalPrefix);
163}
164
Chris Lattner0f578952009-02-28 20:25:14 +0000165
Daniel Maleaded9f932013-05-08 20:38:31 +0000166namespace llvm {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000167
168void TypePrinting::incorporateTypes(const Module &M) {
Bill Wendling8555a372012-08-03 00:30:35 +0000169 NamedTypes.run(M, false);
Andrew Trickec4b6e72011-09-30 19:48:58 +0000170
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000171 // The list of struct types we got back includes all the struct types, split
172 // the unnamed ones out to a numbering and remove the anonymous structs.
173 unsigned NextNumber = 0;
Andrew Trickec4b6e72011-09-30 19:48:58 +0000174
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000175 std::vector<StructType*>::iterator NextToUse = NamedTypes.begin(), I, E;
176 for (I = NamedTypes.begin(), E = NamedTypes.end(); I != E; ++I) {
177 StructType *STy = *I;
Andrew Trickec4b6e72011-09-30 19:48:58 +0000178
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000179 // Ignore anonymous types.
Chris Lattner01beceb2011-08-12 18:07:07 +0000180 if (STy->isLiteral())
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000181 continue;
Andrew Trickec4b6e72011-09-30 19:48:58 +0000182
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000183 if (STy->getName().empty())
184 NumberedTypes[STy] = NextNumber++;
185 else
186 *NextToUse++ = STy;
187 }
Andrew Trickec4b6e72011-09-30 19:48:58 +0000188
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000189 NamedTypes.erase(NextToUse, NamedTypes.end());
190}
191
192
Chris Lattner40959d02009-02-28 20:49:40 +0000193/// CalcTypeName - Write the specified type to the specified raw_ostream, making
194/// use of type names or up references to shorten the type name where possible.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000195void TypePrinting::print(Type *Ty, raw_ostream &OS) {
Chris Lattner0f578952009-02-28 20:25:14 +0000196 switch (Ty->getTypeID()) {
Rafael Espindolaba7df702013-12-07 02:27:52 +0000197 case Type::VoidTyID: OS << "void"; return;
198 case Type::HalfTyID: OS << "half"; return;
199 case Type::FloatTyID: OS << "float"; return;
200 case Type::DoubleTyID: OS << "double"; return;
201 case Type::X86_FP80TyID: OS << "x86_fp80"; return;
202 case Type::FP128TyID: OS << "fp128"; return;
203 case Type::PPC_FP128TyID: OS << "ppc_fp128"; return;
204 case Type::LabelTyID: OS << "label"; return;
205 case Type::MetadataTyID: OS << "metadata"; return;
206 case Type::X86_MMXTyID: OS << "x86_mmx"; return;
Chris Lattner68318b12009-02-28 21:18:43 +0000207 case Type::IntegerTyID:
Chris Lattner06a23212009-02-28 21:27:31 +0000208 OS << 'i' << cast<IntegerType>(Ty)->getBitWidth();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000209 return;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000210
Chris Lattner07d88292009-02-28 20:35:42 +0000211 case Type::FunctionTyID: {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000212 FunctionType *FTy = cast<FunctionType>(Ty);
213 print(FTy->getReturnType(), OS);
Chris Lattner06a23212009-02-28 21:27:31 +0000214 OS << " (";
Chris Lattner07d88292009-02-28 20:35:42 +0000215 for (FunctionType::param_iterator I = FTy->param_begin(),
216 E = FTy->param_end(); I != E; ++I) {
217 if (I != FTy->param_begin())
Chris Lattner06a23212009-02-28 21:27:31 +0000218 OS << ", ";
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000219 print(*I, OS);
Chris Lattner0f578952009-02-28 20:25:14 +0000220 }
Chris Lattner07d88292009-02-28 20:35:42 +0000221 if (FTy->isVarArg()) {
Chris Lattner06a23212009-02-28 21:27:31 +0000222 if (FTy->getNumParams()) OS << ", ";
223 OS << "...";
Chris Lattner0f578952009-02-28 20:25:14 +0000224 }
Chris Lattner06a23212009-02-28 21:27:31 +0000225 OS << ')';
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000226 return;
Chris Lattner07d88292009-02-28 20:35:42 +0000227 }
228 case Type::StructTyID: {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000229 StructType *STy = cast<StructType>(Ty);
Andrew Trickec4b6e72011-09-30 19:48:58 +0000230
Chris Lattner01beceb2011-08-12 18:07:07 +0000231 if (STy->isLiteral())
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000232 return printStructBody(STy, OS);
233
234 if (!STy->getName().empty())
235 return PrintLLVMName(OS, STy->getName(), LocalPrefix);
Andrew Trickec4b6e72011-09-30 19:48:58 +0000236
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000237 DenseMap<StructType*, unsigned>::iterator I = NumberedTypes.find(STy);
238 if (I != NumberedTypes.end())
239 OS << '%' << I->second;
240 else // Not enumerated, print the hex address.
Benjamin Kramer4e8b4632011-11-02 17:24:36 +0000241 OS << "%\"type " << STy << '\"';
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000242 return;
Chris Lattner07d88292009-02-28 20:35:42 +0000243 }
244 case Type::PointerTyID: {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000245 PointerType *PTy = cast<PointerType>(Ty);
246 print(PTy->getElementType(), OS);
Chris Lattner07d88292009-02-28 20:35:42 +0000247 if (unsigned AddressSpace = PTy->getAddressSpace())
Chris Lattner06a23212009-02-28 21:27:31 +0000248 OS << " addrspace(" << AddressSpace << ')';
249 OS << '*';
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000250 return;
Chris Lattner07d88292009-02-28 20:35:42 +0000251 }
252 case Type::ArrayTyID: {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000253 ArrayType *ATy = cast<ArrayType>(Ty);
Chris Lattner06a23212009-02-28 21:27:31 +0000254 OS << '[' << ATy->getNumElements() << " x ";
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000255 print(ATy->getElementType(), OS);
Chris Lattner06a23212009-02-28 21:27:31 +0000256 OS << ']';
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000257 return;
Chris Lattner07d88292009-02-28 20:35:42 +0000258 }
259 case Type::VectorTyID: {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000260 VectorType *PTy = cast<VectorType>(Ty);
Chris Lattner06a23212009-02-28 21:27:31 +0000261 OS << "<" << PTy->getNumElements() << " x ";
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000262 print(PTy->getElementType(), OS);
Chris Lattner06a23212009-02-28 21:27:31 +0000263 OS << '>';
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000264 return;
Chris Lattner07d88292009-02-28 20:35:42 +0000265 }
Chris Lattner0f578952009-02-28 20:25:14 +0000266 }
Rafael Espindolaba7df702013-12-07 02:27:52 +0000267 llvm_unreachable("Invalid TypeID");
Chris Lattner0f578952009-02-28 20:25:14 +0000268}
269
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000270void TypePrinting::printStructBody(StructType *STy, raw_ostream &OS) {
271 if (STy->isOpaque()) {
272 OS << "opaque";
273 return;
Chris Lattner0f578952009-02-28 20:25:14 +0000274 }
Andrew Trickec4b6e72011-09-30 19:48:58 +0000275
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000276 if (STy->isPacked())
277 OS << '<';
Andrew Trickec4b6e72011-09-30 19:48:58 +0000278
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000279 if (STy->getNumElements() == 0) {
280 OS << "{}";
281 } else {
282 StructType::element_iterator I = STy->element_begin();
283 OS << "{ ";
284 print(*I++, OS);
285 for (StructType::element_iterator E = STy->element_end(); I != E; ++I) {
286 OS << ", ";
287 print(*I, OS);
Chris Lattner3243ea12009-03-01 00:03:38 +0000288 }
Andrew Trickec4b6e72011-09-30 19:48:58 +0000289
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000290 OS << " }";
Chris Lattner92c5c122009-02-28 23:20:19 +0000291 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000292 if (STy->isPacked())
293 OS << '>';
Chris Lattner92c5c122009-02-28 23:20:19 +0000294}
295
Chris Lattner3eee99c2008-08-19 04:36:02 +0000296//===----------------------------------------------------------------------===//
297// SlotTracker Class: Enumerate slot numbers for unnamed values
298//===----------------------------------------------------------------------===//
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000299/// This class provides computation of slot numbers for LLVM Assembly writing.
Chris Lattner393b7cd2008-08-17 04:17:45 +0000300///
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000301class SlotTracker {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000302public:
Devang Patel983c6b12009-07-08 21:44:25 +0000303 /// ValueMap - A mapping of Values to slot numbers.
Chris Lattnera204d412008-08-17 17:25:25 +0000304 typedef DenseMap<const Value*, unsigned> ValueMap;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000305
306private:
Devang Patel983c6b12009-07-08 21:44:25 +0000307 /// TheModule - The module for which we are holding slot numbers.
Chris Lattner393b7cd2008-08-17 04:17:45 +0000308 const Module* TheModule;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000309
Devang Patel983c6b12009-07-08 21:44:25 +0000310 /// TheFunction - The function for which we are holding slot numbers.
Chris Lattner393b7cd2008-08-17 04:17:45 +0000311 const Function* TheFunction;
312 bool FunctionProcessed;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000313
Jay Foaddbb221d2011-07-11 07:28:49 +0000314 /// mMap - The slot map for the module level data.
Chris Lattner393b7cd2008-08-17 04:17:45 +0000315 ValueMap mMap;
316 unsigned mNext;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000317
Jay Foaddbb221d2011-07-11 07:28:49 +0000318 /// fMap - The slot map for the function level data.
Chris Lattner393b7cd2008-08-17 04:17:45 +0000319 ValueMap fMap;
320 unsigned fNext;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000321
Devang Patel983c6b12009-07-08 21:44:25 +0000322 /// mdnMap - Map for MDNodes.
Chris Lattnercf4a76e2009-12-31 02:20:11 +0000323 DenseMap<const MDNode*, unsigned> mdnMap;
Devang Patel983c6b12009-07-08 21:44:25 +0000324 unsigned mdnNext;
Bill Wendling829b4782013-02-11 08:43:33 +0000325
326 /// asMap - The slot map for attribute sets.
327 DenseMap<AttributeSet, unsigned> asMap;
328 unsigned asNext;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000329public:
Chris Lattner393b7cd2008-08-17 04:17:45 +0000330 /// Construct from a module
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000331 explicit SlotTracker(const Module *M);
Chris Lattner393b7cd2008-08-17 04:17:45 +0000332 /// Construct from a function, starting out in incorp state.
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000333 explicit SlotTracker(const Function *F);
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000334
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000335 /// Return the slot number of the specified value in it's type
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000336 /// plane. If something is not in the SlotTracker, return -1.
Chris Lattner5e043322007-01-11 03:54:27 +0000337 int getLocalSlot(const Value *V);
338 int getGlobalSlot(const GlobalValue *V);
Devang Patel983c6b12009-07-08 21:44:25 +0000339 int getMetadataSlot(const MDNode *N);
Bill Wendling829b4782013-02-11 08:43:33 +0000340 int getAttributeGroupSlot(AttributeSet AS);
Reid Spencer8beac692004-06-09 15:26:53 +0000341
Misha Brukmanb1c93172005-04-21 23:48:37 +0000342 /// If you'd like to deal with a function instead of just a module, use
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000343 /// this method to get its data into the SlotTracker.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000344 void incorporateFunction(const Function *F) {
345 TheFunction = F;
Reid Spencerb0ac8c42004-08-16 07:46:33 +0000346 FunctionProcessed = false;
347 }
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000348
Misha Brukmanb1c93172005-04-21 23:48:37 +0000349 /// After calling incorporateFunction, use this method to remove the
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000350 /// most recently incorporated function from the SlotTracker. This
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000351 /// will reset the state of the machine back to just the module contents.
352 void purgeFunction();
353
Devang Patel983c6b12009-07-08 21:44:25 +0000354 /// MDNode map iterators.
Chris Lattnercf4a76e2009-12-31 02:20:11 +0000355 typedef DenseMap<const MDNode*, unsigned>::iterator mdn_iterator;
356 mdn_iterator mdn_begin() { return mdnMap.begin(); }
357 mdn_iterator mdn_end() { return mdnMap.end(); }
358 unsigned mdn_size() const { return mdnMap.size(); }
359 bool mdn_empty() const { return mdnMap.empty(); }
Devang Patel983c6b12009-07-08 21:44:25 +0000360
Bill Wendling829b4782013-02-11 08:43:33 +0000361 /// AttributeSet map iterators.
362 typedef DenseMap<AttributeSet, unsigned>::iterator as_iterator;
363 as_iterator as_begin() { return asMap.begin(); }
364 as_iterator as_end() { return asMap.end(); }
365 unsigned as_size() const { return asMap.size(); }
366 bool as_empty() const { return asMap.empty(); }
367
Reid Spencer56010e42004-05-26 21:56:09 +0000368 /// This function does the actual initialization.
369 inline void initialize();
370
Devang Patel983c6b12009-07-08 21:44:25 +0000371 // Implementation Details
372private:
Chris Lattnerea862a32007-01-09 07:55:49 +0000373 /// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
374 void CreateModuleSlot(const GlobalValue *V);
Devang Patel983c6b12009-07-08 21:44:25 +0000375
376 /// CreateMetadataSlot - Insert the specified MDNode* into the slot table.
377 void CreateMetadataSlot(const MDNode *N);
378
Chris Lattnerea862a32007-01-09 07:55:49 +0000379 /// CreateFunctionSlot - Insert the specified Value* into the slot table.
380 void CreateFunctionSlot(const Value *V);
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000381
Bill Wendling829b4782013-02-11 08:43:33 +0000382 /// \brief Insert the specified AttributeSet into the slot table.
383 void CreateAttributeSetSlot(AttributeSet AS);
384
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000385 /// Add all of the module level global variables (and their initializers)
386 /// and function declarations, but not the contents of those functions.
387 void processModule();
388
Devang Patel983c6b12009-07-08 21:44:25 +0000389 /// Add all of the functions arguments, basic blocks, and instructions.
Reid Spencer56010e42004-05-26 21:56:09 +0000390 void processFunction();
391
Craig Toppera60c0f12012-09-15 17:09:36 +0000392 SlotTracker(const SlotTracker &) LLVM_DELETED_FUNCTION;
393 void operator=(const SlotTracker &) LLVM_DELETED_FUNCTION;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000394};
395
Daniel Maleaded9f932013-05-08 20:38:31 +0000396SlotTracker *createSlotTracker(const Module *M) {
397 return new SlotTracker(M);
398}
Chris Lattner3eee99c2008-08-19 04:36:02 +0000399
400static SlotTracker *createSlotTracker(const Value *V) {
401 if (const Argument *FA = dyn_cast<Argument>(V))
402 return new SlotTracker(FA->getParent());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000403
Chris Lattner3eee99c2008-08-19 04:36:02 +0000404 if (const Instruction *I = dyn_cast<Instruction>(V))
Andrew Trick2f0cbf62011-09-30 19:50:40 +0000405 if (I->getParent())
406 return new SlotTracker(I->getParent()->getParent());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000407
Chris Lattner3eee99c2008-08-19 04:36:02 +0000408 if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
409 return new SlotTracker(BB->getParent());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000410
Chris Lattner3eee99c2008-08-19 04:36:02 +0000411 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
412 return new SlotTracker(GV->getParent());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000413
Chris Lattner3eee99c2008-08-19 04:36:02 +0000414 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V))
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000415 return new SlotTracker(GA->getParent());
416
Chris Lattner3eee99c2008-08-19 04:36:02 +0000417 if (const Function *Func = dyn_cast<Function>(V))
418 return new SlotTracker(Func);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000419
Dan Gohmana2489d12010-07-20 23:55:01 +0000420 if (const MDNode *MD = dyn_cast<MDNode>(V)) {
421 if (!MD->isFunctionLocal())
422 return new SlotTracker(MD->getFunction());
423
Craig Topperc6207612014-04-09 06:08:46 +0000424 return new SlotTracker((Function *)nullptr);
Dan Gohmana2489d12010-07-20 23:55:01 +0000425 }
Dale Johannesen7b1a7ed2010-01-13 00:00:24 +0000426
Craig Topperc6207612014-04-09 06:08:46 +0000427 return nullptr;
Chris Lattner3eee99c2008-08-19 04:36:02 +0000428}
429
430#if 0
David Greenec7f9b122010-01-05 01:29:26 +0000431#define ST_DEBUG(X) dbgs() << X
Chris Lattner3eee99c2008-08-19 04:36:02 +0000432#else
Chris Lattner604e3512008-08-19 04:47:09 +0000433#define ST_DEBUG(X)
Chris Lattner3eee99c2008-08-19 04:36:02 +0000434#endif
435
436// Module level constructor. Causes the contents of the Module (sans functions)
437// to be added to the slot table.
438SlotTracker::SlotTracker(const Module *M)
Craig Topperc6207612014-04-09 06:08:46 +0000439 : TheModule(M), TheFunction(nullptr), FunctionProcessed(false),
Bill Wendling829b4782013-02-11 08:43:33 +0000440 mNext(0), fNext(0), mdnNext(0), asNext(0) {
Chris Lattner3eee99c2008-08-19 04:36:02 +0000441}
442
443// Function level constructor. Causes the contents of the Module and the one
444// function provided to be added to the slot table.
445SlotTracker::SlotTracker(const Function *F)
Craig Topperc6207612014-04-09 06:08:46 +0000446 : TheModule(F ? F->getParent() : nullptr), TheFunction(F),
447 FunctionProcessed(false), mNext(0), fNext(0), mdnNext(0), asNext(0) {
Chris Lattner3eee99c2008-08-19 04:36:02 +0000448}
449
450inline void SlotTracker::initialize() {
451 if (TheModule) {
452 processModule();
Craig Topperc6207612014-04-09 06:08:46 +0000453 TheModule = nullptr; ///< Prevent re-processing next time we're called.
Chris Lattner3eee99c2008-08-19 04:36:02 +0000454 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000455
Chris Lattner3eee99c2008-08-19 04:36:02 +0000456 if (TheFunction && !FunctionProcessed)
457 processFunction();
458}
459
460// Iterate through all the global variables, functions, and global
461// variable initializers and create slots for them.
462void SlotTracker::processModule() {
Chris Lattner604e3512008-08-19 04:47:09 +0000463 ST_DEBUG("begin processModule!\n");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000464
Chris Lattner3eee99c2008-08-19 04:36:02 +0000465 // Add all of the unnamed global variables to the value table.
466 for (Module::const_global_iterator I = TheModule->global_begin(),
Devang Patel983c6b12009-07-08 21:44:25 +0000467 E = TheModule->global_end(); I != E; ++I) {
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000468 if (!I->hasName())
Chris Lattner3eee99c2008-08-19 04:36:02 +0000469 CreateModuleSlot(I);
Devang Patel983c6b12009-07-08 21:44:25 +0000470 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000471
Devang Patel23e68302009-07-29 22:04:47 +0000472 // Add metadata used by named metadata.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000473 for (Module::const_named_metadata_iterator
Devang Patel23e68302009-07-29 22:04:47 +0000474 I = TheModule->named_metadata_begin(),
475 E = TheModule->named_metadata_end(); I != E; ++I) {
476 const NamedMDNode *NMD = I;
Dan Gohman093cb792010-07-21 18:54:18 +0000477 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
478 CreateMetadataSlot(NMD->getOperand(i));
Devang Patel23e68302009-07-29 22:04:47 +0000479 }
480
Chris Lattner3eee99c2008-08-19 04:36:02 +0000481 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
Bill Wendling829b4782013-02-11 08:43:33 +0000482 I != E; ++I) {
Chris Lattner3eee99c2008-08-19 04:36:02 +0000483 if (!I->hasName())
Bill Wendling829b4782013-02-11 08:43:33 +0000484 // Add all the unnamed functions to the table.
Chris Lattner3eee99c2008-08-19 04:36:02 +0000485 CreateModuleSlot(I);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000486
Bill Wendling829b4782013-02-11 08:43:33 +0000487 // Add all the function attributes to the table.
Bill Wendling90bc19c2013-02-20 07:21:42 +0000488 // FIXME: Add attributes of other objects?
Bill Wendling829b4782013-02-11 08:43:33 +0000489 AttributeSet FnAttrs = I->getAttributes().getFnAttributes();
490 if (FnAttrs.hasAttributes(AttributeSet::FunctionIndex))
491 CreateAttributeSetSlot(FnAttrs);
492 }
493
Chris Lattner604e3512008-08-19 04:47:09 +0000494 ST_DEBUG("end processModule!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000495}
496
Chris Lattner3eee99c2008-08-19 04:36:02 +0000497// Process the arguments, basic blocks, and instructions of a function.
498void SlotTracker::processFunction() {
Chris Lattner604e3512008-08-19 04:47:09 +0000499 ST_DEBUG("begin processFunction!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000500 fNext = 0;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000501
Chris Lattner3eee99c2008-08-19 04:36:02 +0000502 // Add all the function arguments with no names.
503 for(Function::const_arg_iterator AI = TheFunction->arg_begin(),
504 AE = TheFunction->arg_end(); AI != AE; ++AI)
505 if (!AI->hasName())
506 CreateFunctionSlot(AI);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000507
Chris Lattner604e3512008-08-19 04:47:09 +0000508 ST_DEBUG("Inserting Instructions:\n");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000509
Chris Lattnerbddea6a2009-12-31 02:13:35 +0000510 SmallVector<std::pair<unsigned, MDNode*>, 4> MDForInst;
Devang Pateldec23fd2009-09-16 20:21:17 +0000511
Chris Lattner3eee99c2008-08-19 04:36:02 +0000512 // Add all of the basic blocks and instructions with no names.
513 for (Function::const_iterator BB = TheFunction->begin(),
514 E = TheFunction->end(); BB != E; ++BB) {
515 if (!BB->hasName())
516 CreateFunctionSlot(BB);
Andrew Trickec4b6e72011-09-30 19:48:58 +0000517
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000518 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E;
Devang Patel983c6b12009-07-08 21:44:25 +0000519 ++I) {
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000520 if (!I->getType()->isVoidTy() && !I->hasName())
Chris Lattner3eee99c2008-08-19 04:36:02 +0000521 CreateFunctionSlot(I);
Andrew Trickec4b6e72011-09-30 19:48:58 +0000522
Chris Lattner58aff8f2010-05-10 20:53:17 +0000523 // Intrinsics can directly use metadata. We allow direct calls to any
524 // llvm.foo function here, because the target may not be linked into the
525 // optimizer.
526 if (const CallInst *CI = dyn_cast<CallInst>(I)) {
527 if (Function *F = CI->getCalledFunction())
Matt Arsenaulta68c9ad2013-12-05 06:05:43 +0000528 if (F->isIntrinsic())
Chris Lattner58aff8f2010-05-10 20:53:17 +0000529 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
530 if (MDNode *N = dyn_cast_or_null<MDNode>(I->getOperand(i)))
531 CreateMetadataSlot(N);
Bill Wendling6da216f2013-02-20 00:04:41 +0000532
Bill Wendlinga0323742013-02-22 09:09:42 +0000533 // Add all the call attributes to the table.
534 AttributeSet Attrs = CI->getAttributes().getFnAttributes();
535 if (Attrs.hasAttributes(AttributeSet::FunctionIndex))
536 CreateAttributeSetSlot(Attrs);
537 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(I)) {
538 // Add all the call attributes to the table.
539 AttributeSet Attrs = II->getAttributes().getFnAttributes();
540 if (Attrs.hasAttributes(AttributeSet::FunctionIndex))
541 CreateAttributeSetSlot(Attrs);
Chris Lattner58aff8f2010-05-10 20:53:17 +0000542 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000543
Devang Pateldec23fd2009-09-16 20:21:17 +0000544 // Process metadata attached with this instruction.
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000545 I->getAllMetadata(MDForInst);
546 for (unsigned i = 0, e = MDForInst.size(); i != e; ++i)
547 CreateMetadataSlot(MDForInst[i].second);
Chris Lattnerbddea6a2009-12-31 02:13:35 +0000548 MDForInst.clear();
Devang Patel983c6b12009-07-08 21:44:25 +0000549 }
Chris Lattner3eee99c2008-08-19 04:36:02 +0000550 }
Devang Pateldec23fd2009-09-16 20:21:17 +0000551
Chris Lattner3eee99c2008-08-19 04:36:02 +0000552 FunctionProcessed = true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000553
Chris Lattner604e3512008-08-19 04:47:09 +0000554 ST_DEBUG("end processFunction!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000555}
556
557/// Clean up after incorporating a function. This is the only way to get out of
558/// the function incorporation state that affects get*Slot/Create*Slot. Function
559/// incorporation state is indicated by TheFunction != 0.
560void SlotTracker::purgeFunction() {
Chris Lattner604e3512008-08-19 04:47:09 +0000561 ST_DEBUG("begin purgeFunction!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000562 fMap.clear(); // Simply discard the function level map
Craig Topperc6207612014-04-09 06:08:46 +0000563 TheFunction = nullptr;
Chris Lattner3eee99c2008-08-19 04:36:02 +0000564 FunctionProcessed = false;
Chris Lattner604e3512008-08-19 04:47:09 +0000565 ST_DEBUG("end purgeFunction!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000566}
567
568/// getGlobalSlot - Get the slot number of a global value.
569int SlotTracker::getGlobalSlot(const GlobalValue *V) {
570 // Check for uninitialized state and do lazy initialization.
571 initialize();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000572
Jay Foaddbb221d2011-07-11 07:28:49 +0000573 // Find the value in the module map
Chris Lattner3eee99c2008-08-19 04:36:02 +0000574 ValueMap::iterator MI = mMap.find(V);
Dan Gohman1dd27572008-10-01 19:58:59 +0000575 return MI == mMap.end() ? -1 : (int)MI->second;
Chris Lattner3eee99c2008-08-19 04:36:02 +0000576}
577
Chris Lattnercf4a76e2009-12-31 02:20:11 +0000578/// getMetadataSlot - Get the slot number of a MDNode.
Devang Patel983c6b12009-07-08 21:44:25 +0000579int SlotTracker::getMetadataSlot(const MDNode *N) {
580 // Check for uninitialized state and do lazy initialization.
581 initialize();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000582
Jay Foaddbb221d2011-07-11 07:28:49 +0000583 // Find the MDNode in the module map
Chris Lattnercf4a76e2009-12-31 02:20:11 +0000584 mdn_iterator MI = mdnMap.find(N);
Devang Patel983c6b12009-07-08 21:44:25 +0000585 return MI == mdnMap.end() ? -1 : (int)MI->second;
586}
587
Chris Lattner3eee99c2008-08-19 04:36:02 +0000588
589/// getLocalSlot - Get the slot number for a value that is local to a function.
590int SlotTracker::getLocalSlot(const Value *V) {
591 assert(!isa<Constant>(V) && "Can't get a constant or global slot with this!");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000592
Chris Lattner3eee99c2008-08-19 04:36:02 +0000593 // Check for uninitialized state and do lazy initialization.
594 initialize();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000595
Chris Lattner3eee99c2008-08-19 04:36:02 +0000596 ValueMap::iterator FI = fMap.find(V);
Dan Gohman1dd27572008-10-01 19:58:59 +0000597 return FI == fMap.end() ? -1 : (int)FI->second;
Chris Lattner3eee99c2008-08-19 04:36:02 +0000598}
599
Bill Wendling829b4782013-02-11 08:43:33 +0000600int SlotTracker::getAttributeGroupSlot(AttributeSet AS) {
601 // Check for uninitialized state and do lazy initialization.
602 initialize();
603
604 // Find the AttributeSet in the module map.
605 as_iterator AI = asMap.find(AS);
606 return AI == asMap.end() ? -1 : (int)AI->second;
607}
Chris Lattner3eee99c2008-08-19 04:36:02 +0000608
609/// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
610void SlotTracker::CreateModuleSlot(const GlobalValue *V) {
611 assert(V && "Can't insert a null Value into SlotTracker!");
Chris Lattner031560c2009-12-29 07:25:48 +0000612 assert(!V->getType()->isVoidTy() && "Doesn't need a slot!");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000613 assert(!V->hasName() && "Doesn't need a slot!");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000614
Chris Lattner3eee99c2008-08-19 04:36:02 +0000615 unsigned DestSlot = mNext++;
616 mMap[V] = DestSlot;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000617
Chris Lattner604e3512008-08-19 04:47:09 +0000618 ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" <<
Chris Lattner3eee99c2008-08-19 04:36:02 +0000619 DestSlot << " [");
620 // G = Global, F = Function, A = Alias, o = other
Chris Lattner604e3512008-08-19 04:47:09 +0000621 ST_DEBUG((isa<GlobalVariable>(V) ? 'G' :
Chris Lattner3eee99c2008-08-19 04:36:02 +0000622 (isa<Function>(V) ? 'F' :
623 (isa<GlobalAlias>(V) ? 'A' : 'o'))) << "]\n");
624}
625
Chris Lattner3eee99c2008-08-19 04:36:02 +0000626/// CreateSlot - Create a new slot for the specified value if it has no name.
627void SlotTracker::CreateFunctionSlot(const Value *V) {
Chris Lattner031560c2009-12-29 07:25:48 +0000628 assert(!V->getType()->isVoidTy() && !V->hasName() && "Doesn't need a slot!");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000629
Chris Lattner3eee99c2008-08-19 04:36:02 +0000630 unsigned DestSlot = fNext++;
631 fMap[V] = DestSlot;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000632
Chris Lattner3eee99c2008-08-19 04:36:02 +0000633 // G = Global, F = Function, o = other
Chris Lattner604e3512008-08-19 04:47:09 +0000634 ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" <<
Chris Lattner3eee99c2008-08-19 04:36:02 +0000635 DestSlot << " [o]\n");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000636}
Chris Lattner3eee99c2008-08-19 04:36:02 +0000637
Devang Patel983c6b12009-07-08 21:44:25 +0000638/// CreateModuleSlot - Insert the specified MDNode* into the slot table.
639void SlotTracker::CreateMetadataSlot(const MDNode *N) {
640 assert(N && "Can't insert a null Value into SlotTracker!");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000641
Chris Lattner0d50bdd2009-12-31 02:27:30 +0000642 // Don't insert if N is a function-local metadata, these are always printed
643 // inline.
Dan Gohmana2489d12010-07-20 23:55:01 +0000644 if (!N->isFunctionLocal()) {
645 mdn_iterator I = mdnMap.find(N);
646 if (I != mdnMap.end())
647 return;
Victor Hernandez4d633542009-12-04 20:07:10 +0000648
Dan Gohmana2489d12010-07-20 23:55:01 +0000649 unsigned DestSlot = mdnNext++;
650 mdnMap[N] = DestSlot;
651 }
Devang Patel983c6b12009-07-08 21:44:25 +0000652
Chris Lattner0d50bdd2009-12-31 02:27:30 +0000653 // Recursively add any MDNodes referenced by operands.
654 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
655 if (const MDNode *Op = dyn_cast_or_null<MDNode>(N->getOperand(i)))
656 CreateMetadataSlot(Op);
Devang Patel983c6b12009-07-08 21:44:25 +0000657}
Chris Lattner3eee99c2008-08-19 04:36:02 +0000658
Bill Wendling829b4782013-02-11 08:43:33 +0000659void SlotTracker::CreateAttributeSetSlot(AttributeSet AS) {
660 assert(AS.hasAttributes(AttributeSet::FunctionIndex) &&
661 "Doesn't need a slot!");
662
663 as_iterator I = asMap.find(AS);
664 if (I != asMap.end())
665 return;
666
667 unsigned DestSlot = asNext++;
668 asMap[AS] = DestSlot;
669}
670
Chris Lattner3eee99c2008-08-19 04:36:02 +0000671//===----------------------------------------------------------------------===//
672// AsmWriter Implementation
673//===----------------------------------------------------------------------===//
Chris Lattner7f8845a2002-07-23 18:07:49 +0000674
Dan Gohman12dad632009-08-12 20:56:03 +0000675static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
Dan Gohman8061d9e2009-08-13 15:27:57 +0000676 TypePrinting *TypePrinter,
Dan Gohmana2489d12010-07-20 23:55:01 +0000677 SlotTracker *Machine,
678 const Module *Context);
Reid Spencer58d30f22004-07-04 11:50:43 +0000679
Chris Lattnerfc9f1c92006-12-06 06:40:49 +0000680static const char *getPredicateText(unsigned predicate) {
Reid Spencer812a1be2006-12-04 05:19:18 +0000681 const char * pred = "unknown";
682 switch (predicate) {
Chris Lattnerbddea6a2009-12-31 02:13:35 +0000683 case FCmpInst::FCMP_FALSE: pred = "false"; break;
684 case FCmpInst::FCMP_OEQ: pred = "oeq"; break;
685 case FCmpInst::FCMP_OGT: pred = "ogt"; break;
686 case FCmpInst::FCMP_OGE: pred = "oge"; break;
687 case FCmpInst::FCMP_OLT: pred = "olt"; break;
688 case FCmpInst::FCMP_OLE: pred = "ole"; break;
689 case FCmpInst::FCMP_ONE: pred = "one"; break;
690 case FCmpInst::FCMP_ORD: pred = "ord"; break;
691 case FCmpInst::FCMP_UNO: pred = "uno"; break;
692 case FCmpInst::FCMP_UEQ: pred = "ueq"; break;
693 case FCmpInst::FCMP_UGT: pred = "ugt"; break;
694 case FCmpInst::FCMP_UGE: pred = "uge"; break;
695 case FCmpInst::FCMP_ULT: pred = "ult"; break;
696 case FCmpInst::FCMP_ULE: pred = "ule"; break;
697 case FCmpInst::FCMP_UNE: pred = "une"; break;
698 case FCmpInst::FCMP_TRUE: pred = "true"; break;
699 case ICmpInst::ICMP_EQ: pred = "eq"; break;
700 case ICmpInst::ICMP_NE: pred = "ne"; break;
701 case ICmpInst::ICMP_SGT: pred = "sgt"; break;
702 case ICmpInst::ICMP_SGE: pred = "sge"; break;
703 case ICmpInst::ICMP_SLT: pred = "slt"; break;
704 case ICmpInst::ICMP_SLE: pred = "sle"; break;
705 case ICmpInst::ICMP_UGT: pred = "ugt"; break;
706 case ICmpInst::ICMP_UGE: pred = "uge"; break;
707 case ICmpInst::ICMP_ULT: pred = "ult"; break;
708 case ICmpInst::ICMP_ULE: pred = "ule"; break;
Reid Spencer812a1be2006-12-04 05:19:18 +0000709 }
710 return pred;
711}
712
Eli Friedmanc9a551e2011-07-28 21:48:00 +0000713static void writeAtomicRMWOperation(raw_ostream &Out,
714 AtomicRMWInst::BinOp Op) {
715 switch (Op) {
716 default: Out << " <unknown operation " << Op << ">"; break;
717 case AtomicRMWInst::Xchg: Out << " xchg"; break;
718 case AtomicRMWInst::Add: Out << " add"; break;
719 case AtomicRMWInst::Sub: Out << " sub"; break;
720 case AtomicRMWInst::And: Out << " and"; break;
721 case AtomicRMWInst::Nand: Out << " nand"; break;
722 case AtomicRMWInst::Or: Out << " or"; break;
723 case AtomicRMWInst::Xor: Out << " xor"; break;
724 case AtomicRMWInst::Max: Out << " max"; break;
725 case AtomicRMWInst::Min: Out << " min"; break;
726 case AtomicRMWInst::UMax: Out << " umax"; break;
727 case AtomicRMWInst::UMin: Out << " umin"; break;
728 }
729}
Devang Patel983c6b12009-07-08 21:44:25 +0000730
Dan Gohman12dad632009-08-12 20:56:03 +0000731static void WriteOptimizationInfo(raw_ostream &Out, const User *U) {
Michael Ilseman92053172012-11-27 00:42:44 +0000732 if (const FPMathOperator *FPO = dyn_cast<const FPMathOperator>(U)) {
733 // Unsafe algebra implies all the others, no need to write them all out
734 if (FPO->hasUnsafeAlgebra())
735 Out << " fast";
736 else {
737 if (FPO->hasNoNaNs())
738 Out << " nnan";
739 if (FPO->hasNoInfs())
740 Out << " ninf";
741 if (FPO->hasNoSignedZeros())
742 Out << " nsz";
743 if (FPO->hasAllowReciprocal())
744 Out << " arcp";
745 }
746 }
747
Dan Gohman0ebd6962009-07-20 21:19:07 +0000748 if (const OverflowingBinaryOperator *OBO =
749 dyn_cast<OverflowingBinaryOperator>(U)) {
Dan Gohman16f54152009-08-20 17:11:38 +0000750 if (OBO->hasNoUnsignedWrap())
Dan Gohman9c7f8082009-07-27 16:11:46 +0000751 Out << " nuw";
Dan Gohman16f54152009-08-20 17:11:38 +0000752 if (OBO->hasNoSignedWrap())
Dan Gohman9c7f8082009-07-27 16:11:46 +0000753 Out << " nsw";
Chris Lattner35315d02011-02-06 21:44:57 +0000754 } else if (const PossiblyExactOperator *Div =
755 dyn_cast<PossiblyExactOperator>(U)) {
Dan Gohman0ebd6962009-07-20 21:19:07 +0000756 if (Div->isExact())
Dan Gohman9c7f8082009-07-27 16:11:46 +0000757 Out << " exact";
Dan Gohman1639c392009-07-27 21:53:46 +0000758 } else if (const GEPOperator *GEP = dyn_cast<GEPOperator>(U)) {
759 if (GEP->isInBounds())
760 Out << " inbounds";
Dan Gohman0ebd6962009-07-20 21:19:07 +0000761 }
762}
763
Dan Gohmanefb8dbb2010-07-14 20:57:55 +0000764static void WriteConstantInternal(raw_ostream &Out, const Constant *CV,
765 TypePrinting &TypePrinter,
Dan Gohmana2489d12010-07-20 23:55:01 +0000766 SlotTracker *Machine,
767 const Module *Context) {
Zhou Sheng75b871f2007-01-11 12:24:14 +0000768 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Duncan Sands9dff9be2010-02-15 16:12:20 +0000769 if (CI->getType()->isIntegerTy(1)) {
Reid Spencercddc9df2007-01-12 04:24:46 +0000770 Out << (CI->getZExtValue() ? "true" : "false");
Chris Lattner17f71652008-08-17 07:19:36 +0000771 return;
772 }
773 Out << CI->getValue();
774 return;
775 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000776
Chris Lattner17f71652008-08-17 07:19:36 +0000777 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Tobias Grosser6b31d172012-05-24 15:59:06 +0000778 if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEsingle ||
Dan Gohman518cda42011-12-17 00:04:22 +0000779 &CFP->getValueAPF().getSemantics() == &APFloat::IEEEdouble) {
Dale Johannesen028084e2007-09-12 03:30:33 +0000780 // We would like to output the FP constant value in exponential notation,
781 // but we cannot do this if doing so will lose precision. Check here to
782 // make sure that we only output it in exponential format if we can parse
783 // the value back and get the same value.
784 //
Dale Johannesen1f864982009-01-21 20:32:55 +0000785 bool ignored;
Dan Gohman518cda42011-12-17 00:04:22 +0000786 bool isHalf = &CFP->getValueAPF().getSemantics()==&APFloat::IEEEhalf;
Dale Johannesen028084e2007-09-12 03:30:33 +0000787 bool isDouble = &CFP->getValueAPF().getSemantics()==&APFloat::IEEEdouble;
NAKAMURA Takumi35d19c02012-02-16 08:12:24 +0000788 bool isInf = CFP->getValueAPF().isInfinity();
789 bool isNaN = CFP->getValueAPF().isNaN();
790 if (!isHalf && !isInf && !isNaN) {
Dan Gohman518cda42011-12-17 00:04:22 +0000791 double Val = isDouble ? CFP->getValueAPF().convertToDouble() :
792 CFP->getValueAPF().convertToFloat();
793 SmallString<128> StrVal;
794 raw_svector_ostream(StrVal) << Val;
Chris Lattner1e194682002-04-18 18:53:13 +0000795
Dan Gohman518cda42011-12-17 00:04:22 +0000796 // Check to make sure that the stringized number is not some string like
797 // "Inf" or NaN, that atof will accept, but the lexer will not. Check
798 // that the string matches the "[-+]?[0-9]" regex.
799 //
800 if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
801 ((StrVal[0] == '-' || StrVal[0] == '+') &&
802 (StrVal[1] >= '0' && StrVal[1] <= '9'))) {
803 // Reparse stringized version!
NAKAMURA Takumiaec41232012-02-16 04:19:15 +0000804 if (APFloat(APFloat::IEEEdouble, StrVal).convertToDouble() == Val) {
Dan Gohman518cda42011-12-17 00:04:22 +0000805 Out << StrVal.str();
806 return;
807 }
Dale Johannesen028084e2007-09-12 03:30:33 +0000808 }
Chris Lattner1e194682002-04-18 18:53:13 +0000809 }
Dale Johannesen028084e2007-09-12 03:30:33 +0000810 // Otherwise we could not reparse it to exactly the same value, so we must
Dale Johannesen1f864982009-01-21 20:32:55 +0000811 // output the string in hexadecimal format! Note that loading and storing
812 // floating point types changes the bits of NaNs on some hosts, notably
813 // x86, so we must not use these types.
Benjamin Kramer86c77412014-03-15 18:47:07 +0000814 static_assert(sizeof(double) == sizeof(uint64_t),
815 "assuming that double is 64 bits!");
Chris Lattner5505eed2008-11-10 04:30:26 +0000816 char Buffer[40];
Dale Johannesen1f864982009-01-21 20:32:55 +0000817 APFloat apf = CFP->getValueAPF();
Dan Gohman518cda42011-12-17 00:04:22 +0000818 // Halves and floats are represented in ASCII IR as double, convert.
Dale Johannesen1f864982009-01-21 20:32:55 +0000819 if (!isDouble)
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000820 apf.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven,
Dale Johannesen1f864982009-01-21 20:32:55 +0000821 &ignored);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000822 Out << "0x" <<
823 utohex_buffer(uint64_t(apf.bitcastToAPInt().getZExtValue()),
Dale Johannesen1f864982009-01-21 20:32:55 +0000824 Buffer+40);
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000825 return;
826 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000827
Tobias Grosser6b31d172012-05-24 15:59:06 +0000828 // Either half, or some form of long double.
829 // These appear as a magic letter identifying the type, then a
830 // fixed number of hex digits.
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000831 Out << "0x";
Tobias Grosser6b31d172012-05-24 15:59:06 +0000832 // Bit position, in the current word, of the next nibble to print.
833 int shiftcount;
834
Dale Johannesen93eefa02009-03-23 21:16:53 +0000835 if (&CFP->getValueAPF().getSemantics() == &APFloat::x87DoubleExtended) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000836 Out << 'K';
Dale Johannesen93eefa02009-03-23 21:16:53 +0000837 // api needed to prevent premature destruction
838 APInt api = CFP->getValueAPF().bitcastToAPInt();
839 const uint64_t* p = api.getRawData();
840 uint64_t word = p[1];
Tobias Grosser6b31d172012-05-24 15:59:06 +0000841 shiftcount = 12;
Dale Johannesen93eefa02009-03-23 21:16:53 +0000842 int width = api.getBitWidth();
843 for (int j=0; j<width; j+=4, shiftcount-=4) {
844 unsigned int nibble = (word>>shiftcount) & 15;
845 if (nibble < 10)
846 Out << (unsigned char)(nibble + '0');
847 else
848 Out << (unsigned char)(nibble - 10 + 'A');
849 if (shiftcount == 0 && j+4 < width) {
850 word = *p;
851 shiftcount = 64;
852 if (width-j-4 < 64)
853 shiftcount = width-j-4;
854 }
855 }
856 return;
Tobias Grosser6b31d172012-05-24 15:59:06 +0000857 } else if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEquad) {
858 shiftcount = 60;
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000859 Out << 'L';
Tobias Grosser6b31d172012-05-24 15:59:06 +0000860 } else if (&CFP->getValueAPF().getSemantics() == &APFloat::PPCDoubleDouble) {
861 shiftcount = 60;
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000862 Out << 'M';
Tobias Grosser6b31d172012-05-24 15:59:06 +0000863 } else if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEhalf) {
864 shiftcount = 12;
865 Out << 'H';
866 } else
Torok Edwinfbcc6632009-07-14 16:55:14 +0000867 llvm_unreachable("Unsupported floating point type");
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000868 // api needed to prevent premature destruction
Dale Johannesen54306fe2008-10-09 18:53:47 +0000869 APInt api = CFP->getValueAPF().bitcastToAPInt();
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000870 const uint64_t* p = api.getRawData();
871 uint64_t word = *p;
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000872 int width = api.getBitWidth();
873 for (int j=0; j<width; j+=4, shiftcount-=4) {
874 unsigned int nibble = (word>>shiftcount) & 15;
875 if (nibble < 10)
876 Out << (unsigned char)(nibble + '0');
Dale Johannesen028084e2007-09-12 03:30:33 +0000877 else
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000878 Out << (unsigned char)(nibble - 10 + 'A');
879 if (shiftcount == 0 && j+4 < width) {
880 word = *(++p);
881 shiftcount = 64;
882 if (width-j-4 < 64)
883 shiftcount = width-j-4;
Dale Johannesen028084e2007-09-12 03:30:33 +0000884 }
885 }
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000886 return;
887 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000888
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000889 if (isa<ConstantAggregateZero>(CV)) {
Chris Lattner76b2ff42004-02-15 05:55:15 +0000890 Out << "zeroinitializer";
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000891 return;
892 }
Andrew Trickec4b6e72011-09-30 19:48:58 +0000893
Chris Lattner214cc702009-10-28 03:38:12 +0000894 if (const BlockAddress *BA = dyn_cast<BlockAddress>(CV)) {
895 Out << "blockaddress(";
Dan Gohmana2489d12010-07-20 23:55:01 +0000896 WriteAsOperandInternal(Out, BA->getFunction(), &TypePrinter, Machine,
897 Context);
Chris Lattner214cc702009-10-28 03:38:12 +0000898 Out << ", ";
Dan Gohmana2489d12010-07-20 23:55:01 +0000899 WriteAsOperandInternal(Out, BA->getBasicBlock(), &TypePrinter, Machine,
900 Context);
Chris Lattner214cc702009-10-28 03:38:12 +0000901 Out << ")";
902 return;
903 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000904
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000905 if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000906 Type *ETy = CA->getType()->getElementType();
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000907 Out << '[';
908 TypePrinter.print(ETy, Out);
909 Out << ' ';
910 WriteAsOperandInternal(Out, CA->getOperand(0),
911 &TypePrinter, Machine,
912 Context);
913 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
914 Out << ", ";
Chris Lattner9c181f62012-01-31 03:15:40 +0000915 TypePrinter.print(ETy, Out);
916 Out << ' ';
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000917 WriteAsOperandInternal(Out, CA->getOperand(i), &TypePrinter, Machine,
Chris Lattner9c181f62012-01-31 03:15:40 +0000918 Context);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000919 }
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000920 Out << ']';
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000921 return;
922 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000923
Chris Lattnerfa775002012-01-26 02:32:04 +0000924 if (const ConstantDataArray *CA = dyn_cast<ConstantDataArray>(CV)) {
925 // As a special case, print the array as a string if it is an array of
926 // i8 with ConstantInt values.
927 if (CA->isString()) {
928 Out << "c\"";
929 PrintEscapedString(CA->getAsString(), Out);
930 Out << '"';
931 return;
932 }
933
934 Type *ETy = CA->getType()->getElementType();
935 Out << '[';
Chris Lattner9c181f62012-01-31 03:15:40 +0000936 TypePrinter.print(ETy, Out);
937 Out << ' ';
938 WriteAsOperandInternal(Out, CA->getElementAsConstant(0),
939 &TypePrinter, Machine,
940 Context);
941 for (unsigned i = 1, e = CA->getNumElements(); i != e; ++i) {
942 Out << ", ";
Chris Lattnerfa775002012-01-26 02:32:04 +0000943 TypePrinter.print(ETy, Out);
944 Out << ' ';
Chris Lattner9c181f62012-01-31 03:15:40 +0000945 WriteAsOperandInternal(Out, CA->getElementAsConstant(i), &TypePrinter,
946 Machine, Context);
Chris Lattnerfa775002012-01-26 02:32:04 +0000947 }
Chris Lattner9c181f62012-01-31 03:15:40 +0000948 Out << ']';
Chris Lattnerfa775002012-01-26 02:32:04 +0000949 return;
950 }
951
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000952
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000953 if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
Andrew Lenharth0d124b82007-01-08 18:21:30 +0000954 if (CS->getType()->isPacked())
955 Out << '<';
Misha Brukman21bbdb92004-06-04 21:11:51 +0000956 Out << '{';
Jim Laskey3bb78742006-02-25 12:27:03 +0000957 unsigned N = CS->getNumOperands();
958 if (N) {
Chris Lattner604e3512008-08-19 04:47:09 +0000959 Out << ' ';
Chris Lattnere101c442009-02-28 21:26:53 +0000960 TypePrinter.print(CS->getOperand(0)->getType(), Out);
Dan Gohman81313fd2008-09-14 17:21:12 +0000961 Out << ' ';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000962
Dan Gohmana2489d12010-07-20 23:55:01 +0000963 WriteAsOperandInternal(Out, CS->getOperand(0), &TypePrinter, Machine,
964 Context);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000965
Jim Laskey3bb78742006-02-25 12:27:03 +0000966 for (unsigned i = 1; i < N; i++) {
Chris Lattnerd84bb632002-04-16 21:36:08 +0000967 Out << ", ";
Chris Lattnere101c442009-02-28 21:26:53 +0000968 TypePrinter.print(CS->getOperand(i)->getType(), Out);
Dan Gohman81313fd2008-09-14 17:21:12 +0000969 Out << ' ';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000970
Dan Gohmana2489d12010-07-20 23:55:01 +0000971 WriteAsOperandInternal(Out, CS->getOperand(i), &TypePrinter, Machine,
972 Context);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000973 }
Dan Gohman81313fd2008-09-14 17:21:12 +0000974 Out << ' ';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000975 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000976
Dan Gohman81313fd2008-09-14 17:21:12 +0000977 Out << '}';
Andrew Lenharth0d124b82007-01-08 18:21:30 +0000978 if (CS->getType()->isPacked())
979 Out << '>';
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000980 return;
981 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000982
Chris Lattnerfa775002012-01-26 02:32:04 +0000983 if (isa<ConstantVector>(CV) || isa<ConstantDataVector>(CV)) {
984 Type *ETy = CV->getType()->getVectorElementType();
Dan Gohman1262a252009-02-11 00:25:25 +0000985 Out << '<';
Chris Lattnere101c442009-02-28 21:26:53 +0000986 TypePrinter.print(ETy, Out);
Dan Gohman81313fd2008-09-14 17:21:12 +0000987 Out << ' ';
Chris Lattnerfa775002012-01-26 02:32:04 +0000988 WriteAsOperandInternal(Out, CV->getAggregateElement(0U), &TypePrinter,
989 Machine, Context);
990 for (unsigned i = 1, e = CV->getType()->getVectorNumElements(); i != e;++i){
Chris Lattner585297e82008-08-19 05:26:17 +0000991 Out << ", ";
Chris Lattnere101c442009-02-28 21:26:53 +0000992 TypePrinter.print(ETy, Out);
Dan Gohman81313fd2008-09-14 17:21:12 +0000993 Out << ' ';
Chris Lattnerfa775002012-01-26 02:32:04 +0000994 WriteAsOperandInternal(Out, CV->getAggregateElement(i), &TypePrinter,
995 Machine, Context);
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000996 }
Dan Gohman1262a252009-02-11 00:25:25 +0000997 Out << '>';
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000998 return;
999 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001000
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001001 if (isa<ConstantPointerNull>(CV)) {
Chris Lattnerd84bb632002-04-16 21:36:08 +00001002 Out << "null";
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001003 return;
1004 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001005
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001006 if (isa<UndefValue>(CV)) {
Chris Lattner5e0b9f22004-10-16 18:08:06 +00001007 Out << "undef";
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001008 return;
1009 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001010
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001011 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Reid Spencer812a1be2006-12-04 05:19:18 +00001012 Out << CE->getOpcodeName();
Dan Gohman9c7f8082009-07-27 16:11:46 +00001013 WriteOptimizationInfo(Out, CE);
Reid Spencer812a1be2006-12-04 05:19:18 +00001014 if (CE->isCompare())
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001015 Out << ' ' << getPredicateText(CE->getPredicate());
Reid Spencer812a1be2006-12-04 05:19:18 +00001016 Out << " (";
Misha Brukmanb1c93172005-04-21 23:48:37 +00001017
Vikram S. Adveb952b542002-07-14 23:14:45 +00001018 for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) {
Chris Lattnere101c442009-02-28 21:26:53 +00001019 TypePrinter.print((*OI)->getType(), Out);
Dan Gohman81313fd2008-09-14 17:21:12 +00001020 Out << ' ';
Dan Gohmana2489d12010-07-20 23:55:01 +00001021 WriteAsOperandInternal(Out, *OI, &TypePrinter, Machine, Context);
Vikram S. Adveb952b542002-07-14 23:14:45 +00001022 if (OI+1 != CE->op_end())
Chris Lattner3cd8c562002-07-30 18:54:25 +00001023 Out << ", ";
Vikram S. Adveb952b542002-07-14 23:14:45 +00001024 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001025
Dan Gohmana76f0f72008-05-31 19:12:39 +00001026 if (CE->hasIndices()) {
Jay Foad0091fe82011-04-13 15:22:40 +00001027 ArrayRef<unsigned> Indices = CE->getIndices();
Dan Gohmana76f0f72008-05-31 19:12:39 +00001028 for (unsigned i = 0, e = Indices.size(); i != e; ++i)
1029 Out << ", " << Indices[i];
1030 }
1031
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001032 if (CE->isCast()) {
Chris Lattner83b396b2002-08-15 19:37:43 +00001033 Out << " to ";
Chris Lattnere101c442009-02-28 21:26:53 +00001034 TypePrinter.print(CE->getType(), Out);
Chris Lattner83b396b2002-08-15 19:37:43 +00001035 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001036
Misha Brukman21bbdb92004-06-04 21:11:51 +00001037 Out << ')';
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001038 return;
Chris Lattnerd84bb632002-04-16 21:36:08 +00001039 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001040
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001041 Out << "<placeholder or erroneous Constant>";
Chris Lattnerd84bb632002-04-16 21:36:08 +00001042}
1043
Chris Lattnercdec5812009-12-31 02:31:59 +00001044static void WriteMDNodeBodyInternal(raw_ostream &Out, const MDNode *Node,
1045 TypePrinting *TypePrinter,
Dan Gohmana2489d12010-07-20 23:55:01 +00001046 SlotTracker *Machine,
1047 const Module *Context) {
Chris Lattnercdec5812009-12-31 02:31:59 +00001048 Out << "!{";
1049 for (unsigned mi = 0, me = Node->getNumOperands(); mi != me; ++mi) {
1050 const Value *V = Node->getOperand(mi);
Craig Topperc6207612014-04-09 06:08:46 +00001051 if (!V)
Chris Lattnercdec5812009-12-31 02:31:59 +00001052 Out << "null";
1053 else {
1054 TypePrinter->print(V->getType(), Out);
1055 Out << ' ';
Andrew Trickec4b6e72011-09-30 19:48:58 +00001056 WriteAsOperandInternal(Out, Node->getOperand(mi),
Dan Gohmana2489d12010-07-20 23:55:01 +00001057 TypePrinter, Machine, Context);
Chris Lattnercdec5812009-12-31 02:31:59 +00001058 }
1059 if (mi + 1 != me)
1060 Out << ", ";
1061 }
Andrew Trickec4b6e72011-09-30 19:48:58 +00001062
Chris Lattnercdec5812009-12-31 02:31:59 +00001063 Out << "}";
1064}
1065
Chandler Carruthd48cdbf2014-01-09 02:29:41 +00001066// Full implementation of printing a Value as an operand with support for
1067// TypePrinting, etc.
Dan Gohman12dad632009-08-12 20:56:03 +00001068static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
Dan Gohman8061d9e2009-08-13 15:27:57 +00001069 TypePrinting *TypePrinter,
Dan Gohmana2489d12010-07-20 23:55:01 +00001070 SlotTracker *Machine,
1071 const Module *Context) {
Chris Lattner033935d2008-08-17 04:40:13 +00001072 if (V->hasName()) {
1073 PrintLLVMName(Out, V);
1074 return;
1075 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001076
Chris Lattner033935d2008-08-17 04:40:13 +00001077 const Constant *CV = dyn_cast<Constant>(V);
1078 if (CV && !isa<GlobalValue>(CV)) {
Dan Gohman8061d9e2009-08-13 15:27:57 +00001079 assert(TypePrinter && "Constants require TypePrinting!");
Dan Gohmana2489d12010-07-20 23:55:01 +00001080 WriteConstantInternal(Out, CV, *TypePrinter, Machine, Context);
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001081 return;
1082 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001083
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001084 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
Chris Lattner033935d2008-08-17 04:40:13 +00001085 Out << "asm ";
1086 if (IA->hasSideEffects())
1087 Out << "sideeffect ";
Dale Johannesen1cfb9582009-10-21 23:28:00 +00001088 if (IA->isAlignStack())
1089 Out << "alignstack ";
Chad Rosierd8c76102012-09-05 19:00:49 +00001090 // We don't emit the AD_ATT dialect as it's the assumed default.
1091 if (IA->getDialect() == InlineAsm::AD_Intel)
1092 Out << "inteldialect ";
Chris Lattner033935d2008-08-17 04:40:13 +00001093 Out << '"';
1094 PrintEscapedString(IA->getAsmString(), Out);
1095 Out << "\", \"";
1096 PrintEscapedString(IA->getConstraintString(), Out);
1097 Out << '"';
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001098 return;
1099 }
Devang Patel7428d8a2009-07-22 17:43:22 +00001100
Devang Patele059ba6e2009-07-23 01:07:34 +00001101 if (const MDNode *N = dyn_cast<MDNode>(V)) {
Victor Hernandez0471abd2009-12-18 20:09:14 +00001102 if (N->isFunctionLocal()) {
Victor Hernandezb7176a12009-12-04 01:35:02 +00001103 // Print metadata inline, not via slot reference number.
Dan Gohmana2489d12010-07-20 23:55:01 +00001104 WriteMDNodeBodyInternal(Out, N, TypePrinter, Machine, Context);
Victor Hernandezb7176a12009-12-04 01:35:02 +00001105 return;
1106 }
Andrew Trickec4b6e72011-09-30 19:48:58 +00001107
Dan Gohmana2489d12010-07-20 23:55:01 +00001108 if (!Machine) {
1109 if (N->isFunctionLocal())
1110 Machine = new SlotTracker(N->getFunction());
1111 else
1112 Machine = new SlotTracker(Context);
1113 }
Dan Gohman0a54da22010-09-09 20:53:58 +00001114 int Slot = Machine->getMetadataSlot(N);
1115 if (Slot == -1)
1116 Out << "<badref>";
1117 else
1118 Out << '!' << Slot;
Devang Patele059ba6e2009-07-23 01:07:34 +00001119 return;
1120 }
1121
Devang Patel7428d8a2009-07-22 17:43:22 +00001122 if (const MDString *MDS = dyn_cast<MDString>(V)) {
Devang Patel7428d8a2009-07-22 17:43:22 +00001123 Out << "!\"";
Daniel Dunbare03eecb2009-07-25 23:55:21 +00001124 PrintEscapedString(MDS->getString(), Out);
Devang Patel7428d8a2009-07-22 17:43:22 +00001125 Out << '"';
1126 return;
1127 }
1128
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001129 char Prefix = '%';
1130 int Slot;
Chris Lattner5b82a0a2011-08-03 06:15:41 +00001131 // If we have a SlotTracker, use it.
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001132 if (Machine) {
1133 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1134 Slot = Machine->getGlobalSlot(GV);
1135 Prefix = '@';
1136 } else {
1137 Slot = Machine->getLocalSlot(V);
Andrew Trickec4b6e72011-09-30 19:48:58 +00001138
Chris Lattner5b82a0a2011-08-03 06:15:41 +00001139 // If the local value didn't succeed, then we may be referring to a value
1140 // from a different function. Translate it, as this can happen when using
1141 // address of blocks.
1142 if (Slot == -1)
1143 if ((Machine = createSlotTracker(V))) {
1144 Slot = Machine->getLocalSlot(V);
1145 delete Machine;
1146 }
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001147 }
Chris Lattner5b82a0a2011-08-03 06:15:41 +00001148 } else if ((Machine = createSlotTracker(V))) {
1149 // Otherwise, create one to get the # and then destroy it.
1150 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1151 Slot = Machine->getGlobalSlot(GV);
1152 Prefix = '@';
Chris Lattnera2d810d2006-01-25 22:26:05 +00001153 } else {
Chris Lattner5b82a0a2011-08-03 06:15:41 +00001154 Slot = Machine->getLocalSlot(V);
Chris Lattnerd84bb632002-04-16 21:36:08 +00001155 }
Chris Lattner5b82a0a2011-08-03 06:15:41 +00001156 delete Machine;
Craig Topperc6207612014-04-09 06:08:46 +00001157 Machine = nullptr;
Chris Lattner5b82a0a2011-08-03 06:15:41 +00001158 } else {
1159 Slot = -1;
Chris Lattnerd84bb632002-04-16 21:36:08 +00001160 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001161
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001162 if (Slot != -1)
1163 Out << Prefix << Slot;
1164 else
1165 Out << "<badref>";
Chris Lattnerd84bb632002-04-16 21:36:08 +00001166}
1167
Daniel Maleaded9f932013-05-08 20:38:31 +00001168void AssemblyWriter::init() {
1169 if (TheModule)
1170 TypePrinter.incorporateTypes(*TheModule);
1171}
Chris Lattner2e9fee42001-07-12 23:35:26 +00001172
Andrew Trickec4b6e72011-09-30 19:48:58 +00001173
Daniel Maleaded9f932013-05-08 20:38:31 +00001174AssemblyWriter::AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac,
1175 const Module *M,
1176 AssemblyAnnotationWriter *AAW)
1177 : Out(o), TheModule(M), Machine(Mac), AnnotationWriter(AAW) {
1178 init();
1179}
Chris Lattner2f7c9632001-06-06 20:29:01 +00001180
Daniel Maleaded9f932013-05-08 20:38:31 +00001181AssemblyWriter::AssemblyWriter(formatted_raw_ostream &o, const Module *M,
1182 AssemblyAnnotationWriter *AAW)
1183 : Out(o), TheModule(M), ModuleSlotTracker(createSlotTracker(M)),
1184 Machine(*ModuleSlotTracker), AnnotationWriter(AAW) {
1185 init();
1186}
Andrew Trickec4b6e72011-09-30 19:48:58 +00001187
Daniel Maleaded9f932013-05-08 20:38:31 +00001188AssemblyWriter::~AssemblyWriter() { }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001189
Chris Lattner78e2e8b2006-12-06 06:24:27 +00001190void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType) {
Craig Topperc6207612014-04-09 06:08:46 +00001191 if (!Operand) {
Chris Lattner08f7d0c2005-02-24 16:58:29 +00001192 Out << "<null operand!>";
Chris Lattnerb419a1e2009-12-31 02:33:14 +00001193 return;
Chris Lattner08f7d0c2005-02-24 16:58:29 +00001194 }
Chris Lattnerb419a1e2009-12-31 02:33:14 +00001195 if (PrintType) {
1196 TypePrinter.print(Operand->getType(), Out);
1197 Out << ' ';
1198 }
Dan Gohmana2489d12010-07-20 23:55:01 +00001199 WriteAsOperandInternal(Out, Operand, &TypePrinter, &Machine, TheModule);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001200}
1201
Eli Friedmanfee02c62011-07-25 23:16:38 +00001202void AssemblyWriter::writeAtomic(AtomicOrdering Ordering,
1203 SynchronizationScope SynchScope) {
1204 if (Ordering == NotAtomic)
1205 return;
1206
1207 switch (SynchScope) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001208 case SingleThread: Out << " singlethread"; break;
1209 case CrossThread: break;
1210 }
1211
1212 switch (Ordering) {
1213 default: Out << " <bad ordering " << int(Ordering) << ">"; break;
1214 case Unordered: Out << " unordered"; break;
1215 case Monotonic: Out << " monotonic"; break;
1216 case Acquire: Out << " acquire"; break;
1217 case Release: Out << " release"; break;
1218 case AcquireRelease: Out << " acq_rel"; break;
1219 case SequentiallyConsistent: Out << " seq_cst"; break;
1220 }
1221}
1222
Tim Northovere94a5182014-03-11 10:48:52 +00001223void AssemblyWriter::writeAtomicCmpXchg(AtomicOrdering SuccessOrdering,
1224 AtomicOrdering FailureOrdering,
1225 SynchronizationScope SynchScope) {
1226 assert(SuccessOrdering != NotAtomic && FailureOrdering != NotAtomic);
1227
1228 switch (SynchScope) {
1229 case SingleThread: Out << " singlethread"; break;
1230 case CrossThread: break;
1231 }
1232
1233 switch (SuccessOrdering) {
1234 default: Out << " <bad ordering " << int(SuccessOrdering) << ">"; break;
1235 case Unordered: Out << " unordered"; break;
1236 case Monotonic: Out << " monotonic"; break;
1237 case Acquire: Out << " acquire"; break;
1238 case Release: Out << " release"; break;
1239 case AcquireRelease: Out << " acq_rel"; break;
1240 case SequentiallyConsistent: Out << " seq_cst"; break;
1241 }
1242
1243 switch (FailureOrdering) {
1244 default: Out << " <bad ordering " << int(FailureOrdering) << ">"; break;
1245 case Unordered: Out << " unordered"; break;
1246 case Monotonic: Out << " monotonic"; break;
1247 case Acquire: Out << " acquire"; break;
1248 case Release: Out << " release"; break;
1249 case AcquireRelease: Out << " acq_rel"; break;
1250 case SequentiallyConsistent: Out << " seq_cst"; break;
1251 }
1252}
1253
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001254void AssemblyWriter::writeParamOperand(const Value *Operand,
Bill Wendling749a43d2012-12-30 13:50:49 +00001255 AttributeSet Attrs, unsigned Idx) {
Craig Topperc6207612014-04-09 06:08:46 +00001256 if (!Operand) {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00001257 Out << "<null operand!>";
Chris Lattnerb419a1e2009-12-31 02:33:14 +00001258 return;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00001259 }
Chris Lattnerb419a1e2009-12-31 02:33:14 +00001260
1261 // Print the type
1262 TypePrinter.print(Operand->getType(), Out);
1263 // Print parameter attributes list
Bill Wendling749a43d2012-12-30 13:50:49 +00001264 if (Attrs.hasAttributes(Idx))
1265 Out << ' ' << Attrs.getAsString(Idx);
Chris Lattnerb419a1e2009-12-31 02:33:14 +00001266 Out << ' ';
1267 // Print the operand
Dan Gohmana2489d12010-07-20 23:55:01 +00001268 WriteAsOperandInternal(Out, Operand, &TypePrinter, &Machine, TheModule);
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00001269}
Chris Lattner2f7c9632001-06-06 20:29:01 +00001270
Chris Lattner7bfee412001-10-29 16:05:51 +00001271void AssemblyWriter::printModule(const Module *M) {
Bill Wendling829b4782013-02-11 08:43:33 +00001272 Machine.initialize();
1273
Chris Lattner4d8689e2005-03-02 23:12:40 +00001274 if (!M->getModuleIdentifier().empty() &&
Misha Brukmanb1c93172005-04-21 23:48:37 +00001275 // Don't print the ID if it will start a new line (which would
Chris Lattner4d8689e2005-03-02 23:12:40 +00001276 // require a comment char before it).
1277 M->getModuleIdentifier().find('\n') == std::string::npos)
1278 Out << "; ModuleID = '" << M->getModuleIdentifier() << "'\n";
1279
Rafael Espindolaf863ee22014-02-25 20:01:08 +00001280 const std::string &DL = M->getDataLayoutStr();
1281 if (!DL.empty())
1282 Out << "target datalayout = \"" << DL << "\"\n";
Reid Spencer48f98c82004-07-25 21:44:54 +00001283 if (!M->getTargetTriple().empty())
Reid Spencerffec7df2004-07-25 21:29:43 +00001284 Out << "target triple = \"" << M->getTargetTriple() << "\"\n";
Misha Brukmanb1c93172005-04-21 23:48:37 +00001285
Chris Lattnereef2fe72006-01-24 04:13:11 +00001286 if (!M->getModuleInlineAsm().empty()) {
Chris Lattnerefaf35d2006-01-24 00:45:30 +00001287 // Split the string into lines, to make it easier to read the .ll file.
Chris Lattnereef2fe72006-01-24 04:13:11 +00001288 std::string Asm = M->getModuleInlineAsm();
Chris Lattnerefaf35d2006-01-24 00:45:30 +00001289 size_t CurPos = 0;
1290 size_t NewLine = Asm.find_first_of('\n', CurPos);
Dan Gohman69273e62009-08-12 23:54:22 +00001291 Out << '\n';
Chris Lattnerefaf35d2006-01-24 00:45:30 +00001292 while (NewLine != std::string::npos) {
1293 // We found a newline, print the portion of the asm string from the
1294 // last newline up to this newline.
1295 Out << "module asm \"";
1296 PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.begin()+NewLine),
1297 Out);
1298 Out << "\"\n";
1299 CurPos = NewLine+1;
1300 NewLine = Asm.find_first_of('\n', CurPos);
1301 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +00001302 std::string rest(Asm.begin()+CurPos, Asm.end());
1303 if (!rest.empty()) {
1304 Out << "module asm \"";
1305 PrintEscapedString(rest, Out);
1306 Out << "\"\n";
1307 }
Chris Lattner6ed87bd2006-01-23 23:03:36 +00001308 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001309
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001310 printTypeIdentities();
Misha Brukmanb1c93172005-04-21 23:48:37 +00001311
Dan Gohman69273e62009-08-12 23:54:22 +00001312 // Output all globals.
1313 if (!M->global_empty()) Out << '\n';
Chris Lattner54932b02006-12-06 04:41:52 +00001314 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
Duncan Sands66fc0e62012-09-12 09:55:51 +00001315 I != E; ++I) {
1316 printGlobal(I); Out << '\n';
1317 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001318
Chris Lattnerd2747052007-04-26 02:24:10 +00001319 // Output all aliases.
1320 if (!M->alias_empty()) Out << "\n";
1321 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
1322 I != E; ++I)
1323 printAlias(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +00001324
Chris Lattner2cdd49d2004-09-14 05:06:58 +00001325 // Output all of the functions.
Chris Lattner113f4f42002-06-25 16:13:24 +00001326 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
1327 printFunction(I);
Devang Patel983c6b12009-07-08 21:44:25 +00001328
Bill Wendling829b4782013-02-11 08:43:33 +00001329 // Output all attribute groups.
Bill Wendling04945972013-04-29 23:48:06 +00001330 if (!Machine.as_empty()) {
Bill Wendling829b4782013-02-11 08:43:33 +00001331 Out << '\n';
1332 writeAllAttributeGroups();
1333 }
1334
Devang Patel23e68302009-07-29 22:04:47 +00001335 // Output named metadata.
Dan Gohman69273e62009-08-12 23:54:22 +00001336 if (!M->named_metadata_empty()) Out << '\n';
Andrew Trickec4b6e72011-09-30 19:48:58 +00001337
Devang Patel23e68302009-07-29 22:04:47 +00001338 for (Module::const_named_metadata_iterator I = M->named_metadata_begin(),
Chris Lattnerbddea6a2009-12-31 02:13:35 +00001339 E = M->named_metadata_end(); I != E; ++I)
Chris Lattner2c48c642009-12-31 01:54:05 +00001340 printNamedMDNode(I);
Devang Patel23e68302009-07-29 22:04:47 +00001341
1342 // Output metadata.
Chris Lattnercf4a76e2009-12-31 02:20:11 +00001343 if (!Machine.mdn_empty()) {
Chris Lattnerbddea6a2009-12-31 02:13:35 +00001344 Out << '\n';
1345 writeAllMDNodes();
1346 }
Chris Lattnerfee714f2001-09-07 16:36:04 +00001347}
1348
Chris Lattner2c48c642009-12-31 01:54:05 +00001349void AssemblyWriter::printNamedMDNode(const NamedMDNode *NMD) {
Nick Lewycky5bcbd732011-06-15 06:37:58 +00001350 Out << '!';
1351 StringRef Name = NMD->getName();
1352 if (Name.empty()) {
1353 Out << "<empty name> ";
1354 } else {
Guy Benyei83c74e92013-02-12 21:21:59 +00001355 if (isalpha(static_cast<unsigned char>(Name[0])) ||
1356 Name[0] == '-' || Name[0] == '$' ||
Nick Lewycky5bcbd732011-06-15 06:37:58 +00001357 Name[0] == '.' || Name[0] == '_')
1358 Out << Name[0];
1359 else
1360 Out << '\\' << hexdigit(Name[0] >> 4) << hexdigit(Name[0] & 0x0F);
1361 for (unsigned i = 1, e = Name.size(); i != e; ++i) {
1362 unsigned char C = Name[i];
Guy Benyei83c74e92013-02-12 21:21:59 +00001363 if (isalnum(static_cast<unsigned char>(C)) || C == '-' || C == '$' ||
1364 C == '.' || C == '_')
Nick Lewycky5bcbd732011-06-15 06:37:58 +00001365 Out << C;
1366 else
1367 Out << '\\' << hexdigit(C >> 4) << hexdigit(C & 0x0F);
1368 }
1369 }
1370 Out << " = !{";
Chris Lattner2c48c642009-12-31 01:54:05 +00001371 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1372 if (i) Out << ", ";
Dan Gohman0a54da22010-09-09 20:53:58 +00001373 int Slot = Machine.getMetadataSlot(NMD->getOperand(i));
1374 if (Slot == -1)
1375 Out << "<badref>";
1376 else
1377 Out << '!' << Slot;
Chris Lattner2c48c642009-12-31 01:54:05 +00001378 }
1379 Out << "}\n";
1380}
1381
1382
Dan Gohmane2745262009-08-12 17:23:50 +00001383static void PrintLinkage(GlobalValue::LinkageTypes LT,
1384 formatted_raw_ostream &Out) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001385 switch (LT) {
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001386 case GlobalValue::ExternalLinkage: break;
1387 case GlobalValue::PrivateLinkage: Out << "private "; break;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001388 case GlobalValue::InternalLinkage: Out << "internal "; break;
1389 case GlobalValue::LinkOnceAnyLinkage: Out << "linkonce "; break;
1390 case GlobalValue::LinkOnceODRLinkage: Out << "linkonce_odr "; break;
1391 case GlobalValue::WeakAnyLinkage: Out << "weak "; break;
1392 case GlobalValue::WeakODRLinkage: Out << "weak_odr "; break;
1393 case GlobalValue::CommonLinkage: Out << "common "; break;
1394 case GlobalValue::AppendingLinkage: Out << "appending "; break;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001395 case GlobalValue::ExternalWeakLinkage: Out << "extern_weak "; break;
Chris Lattner184f1be2009-04-13 05:44:34 +00001396 case GlobalValue::AvailableExternallyLinkage:
1397 Out << "available_externally ";
1398 break;
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001399 }
1400}
Duncan Sands12da8ce2009-03-07 15:45:40 +00001401
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001402
1403static void PrintVisibility(GlobalValue::VisibilityTypes Vis,
Dan Gohmane2745262009-08-12 17:23:50 +00001404 formatted_raw_ostream &Out) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001405 switch (Vis) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001406 case GlobalValue::DefaultVisibility: break;
1407 case GlobalValue::HiddenVisibility: Out << "hidden "; break;
1408 case GlobalValue::ProtectedVisibility: Out << "protected "; break;
1409 }
1410}
1411
Nico Rieck7157bb72014-01-14 15:22:47 +00001412static void PrintDLLStorageClass(GlobalValue::DLLStorageClassTypes SCT,
1413 formatted_raw_ostream &Out) {
1414 switch (SCT) {
1415 case GlobalValue::DefaultStorageClass: break;
1416 case GlobalValue::DLLImportStorageClass: Out << "dllimport "; break;
1417 case GlobalValue::DLLExportStorageClass: Out << "dllexport "; break;
1418 }
1419}
1420
Hans Wennborgcbe34b42012-06-23 11:37:03 +00001421static void PrintThreadLocalModel(GlobalVariable::ThreadLocalMode TLM,
1422 formatted_raw_ostream &Out) {
1423 switch (TLM) {
1424 case GlobalVariable::NotThreadLocal:
1425 break;
1426 case GlobalVariable::GeneralDynamicTLSModel:
1427 Out << "thread_local ";
1428 break;
1429 case GlobalVariable::LocalDynamicTLSModel:
1430 Out << "thread_local(localdynamic) ";
1431 break;
1432 case GlobalVariable::InitialExecTLSModel:
1433 Out << "thread_local(initialexec) ";
1434 break;
1435 case GlobalVariable::LocalExecTLSModel:
1436 Out << "thread_local(localexec) ";
1437 break;
1438 }
1439}
1440
Chris Lattner7bfee412001-10-29 16:05:51 +00001441void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Dan Gohman5ded1422010-01-29 23:12:36 +00001442 if (GV->isMaterializable())
1443 Out << "; Materializable\n";
1444
Dan Gohmana2489d12010-07-20 23:55:01 +00001445 WriteAsOperandInternal(Out, GV, &TypePrinter, &Machine, GV->getParent());
Dan Gohman466876b2009-08-12 23:32:33 +00001446 Out << " = ";
Chris Lattner37798642001-09-18 04:01:05 +00001447
Chris Lattner1508d3f2008-08-19 05:16:28 +00001448 if (!GV->hasInitializer() && GV->hasExternalLinkage())
1449 Out << "external ";
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001450
Chris Lattner1508d3f2008-08-19 05:16:28 +00001451 PrintLinkage(GV->getLinkage(), Out);
1452 PrintVisibility(GV->getVisibility(), Out);
Nico Rieck7157bb72014-01-14 15:22:47 +00001453 PrintDLLStorageClass(GV->getDLLStorageClass(), Out);
Hans Wennborgcbe34b42012-06-23 11:37:03 +00001454 PrintThreadLocalModel(GV->getThreadLocalMode(), Out);
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +00001455
Chris Lattnerac161bf2009-01-02 07:01:27 +00001456 if (unsigned AddressSpace = GV->getType()->getAddressSpace())
1457 Out << "addrspace(" << AddressSpace << ") ";
Rafael Espindola45e6c192011-01-08 16:42:36 +00001458 if (GV->hasUnnamedAddr()) Out << "unnamed_addr ";
Michael Gottesman27e7ef32013-02-05 05:57:38 +00001459 if (GV->isExternallyInitialized()) Out << "externally_initialized ";
Misha Brukmana6619a92004-06-21 21:53:56 +00001460 Out << (GV->isConstant() ? "constant " : "global ");
Chris Lattnere101c442009-02-28 21:26:53 +00001461 TypePrinter.print(GV->getType()->getElementType(), Out);
Chris Lattner37798642001-09-18 04:01:05 +00001462
Dan Gohman81313fd2008-09-14 17:21:12 +00001463 if (GV->hasInitializer()) {
1464 Out << ' ';
Devang Patel983c6b12009-07-08 21:44:25 +00001465 writeOperand(GV->getInitializer(), false);
Dan Gohman81313fd2008-09-14 17:21:12 +00001466 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001467
Chris Lattner9380b812010-07-07 23:16:37 +00001468 if (GV->hasSection()) {
1469 Out << ", section \"";
1470 PrintEscapedString(GV->getSection(), Out);
1471 Out << '"';
1472 }
Chris Lattner4b96c542005-11-12 00:10:19 +00001473 if (GV->getAlignment())
Chris Lattnerf8a974d2005-11-06 06:48:53 +00001474 Out << ", align " << GV->getAlignment();
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001475
Chris Lattner113f4f42002-06-25 16:13:24 +00001476 printInfoComment(*GV);
Chris Lattnerda975502001-09-10 07:58:01 +00001477}
1478
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001479void AssemblyWriter::printAlias(const GlobalAlias *GA) {
Dan Gohman5ded1422010-01-29 23:12:36 +00001480 if (GA->isMaterializable())
1481 Out << "; Materializable\n";
1482
Dale Johannesen83e468a2008-06-03 18:14:29 +00001483 // Don't crash when dumping partially built GA
1484 if (!GA->hasName())
1485 Out << "<<nameless>> = ";
Chris Lattner033935d2008-08-17 04:40:13 +00001486 else {
1487 PrintLLVMName(Out, GA);
1488 Out << " = ";
1489 }
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001490 PrintVisibility(GA->getVisibility(), Out);
Nico Rieck7157bb72014-01-14 15:22:47 +00001491 PrintDLLStorageClass(GA->getDLLStorageClass(), Out);
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001492
1493 Out << "alias ";
1494
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001495 PrintLinkage(GA->getLinkage(), Out);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001496
Anton Korobeynikov546ea7e2007-04-29 18:02:48 +00001497 const Constant *Aliasee = GA->getAliasee();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001498
Craig Topperc6207612014-04-09 06:08:46 +00001499 if (!Aliasee) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001500 TypePrinter.print(GA->getType(), Out);
1501 Out << " <<NULL ALIASEE>>";
Jay Foad92c19132011-08-01 12:48:54 +00001502 } else {
Jay Foad26db79d2011-08-01 12:29:14 +00001503 writeOperand(Aliasee, !isa<ConstantExpr>(Aliasee));
Jay Foad92c19132011-08-01 12:48:54 +00001504 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001505
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001506 printInfoComment(*GA);
Chris Lattner1508d3f2008-08-19 05:16:28 +00001507 Out << '\n';
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001508}
1509
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001510void AssemblyWriter::printTypeIdentities() {
1511 if (TypePrinter.NumberedTypes.empty() &&
1512 TypePrinter.NamedTypes.empty())
1513 return;
Andrew Trickec4b6e72011-09-30 19:48:58 +00001514
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001515 Out << '\n';
Andrew Trickec4b6e72011-09-30 19:48:58 +00001516
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001517 // We know all the numbers that each type is used and we know that it is a
1518 // dense assignment. Convert the map to an index table.
1519 std::vector<StructType*> NumberedTypes(TypePrinter.NumberedTypes.size());
Andrew Trickec4b6e72011-09-30 19:48:58 +00001520 for (DenseMap<StructType*, unsigned>::iterator I =
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001521 TypePrinter.NumberedTypes.begin(), E = TypePrinter.NumberedTypes.end();
1522 I != E; ++I) {
1523 assert(I->second < NumberedTypes.size() && "Didn't get a dense numbering?");
1524 NumberedTypes[I->second] = I->first;
1525 }
Andrew Trickec4b6e72011-09-30 19:48:58 +00001526
Chris Lattner3243ea12009-03-01 00:03:38 +00001527 // Emit all numbered types.
1528 for (unsigned i = 0, e = NumberedTypes.size(); i != e; ++i) {
Dan Gohman466876b2009-08-12 23:32:33 +00001529 Out << '%' << i << " = type ";
Andrew Trickec4b6e72011-09-30 19:48:58 +00001530
Chris Lattner3243ea12009-03-01 00:03:38 +00001531 // Make sure we print out at least one level of the type structure, so
1532 // that we do not get %2 = type %2
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001533 TypePrinter.printStructBody(NumberedTypes[i], Out);
Dan Gohman69273e62009-08-12 23:54:22 +00001534 Out << '\n';
Chris Lattner3243ea12009-03-01 00:03:38 +00001535 }
Andrew Trickec4b6e72011-09-30 19:48:58 +00001536
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001537 for (unsigned i = 0, e = TypePrinter.NamedTypes.size(); i != e; ++i) {
1538 PrintLLVMName(Out, TypePrinter.NamedTypes[i]->getName(), LocalPrefix);
Chris Lattner1508d3f2008-08-19 05:16:28 +00001539 Out << " = type ";
Reid Spencere7e96712004-05-25 08:53:40 +00001540
1541 // Make sure we print out at least one level of the type structure, so
1542 // that we do not get %FILE = type %FILE
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001543 TypePrinter.printStructBody(TypePrinter.NamedTypes[i], Out);
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001544 Out << '\n';
Reid Spencere7e96712004-05-25 08:53:40 +00001545 }
Reid Spencer32af9e82007-01-06 07:24:44 +00001546}
1547
Misha Brukmanc566ca362004-03-02 00:22:19 +00001548/// printFunction - Print all aspects of a function.
1549///
Chris Lattner113f4f42002-06-25 16:13:24 +00001550void AssemblyWriter::printFunction(const Function *F) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001551 // Print out the return type and name.
1552 Out << '\n';
Chris Lattner379a8d22003-04-16 20:28:45 +00001553
Misha Brukmana6619a92004-06-21 21:53:56 +00001554 if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001555
Dan Gohman5ded1422010-01-29 23:12:36 +00001556 if (F->isMaterializable())
1557 Out << "; Materializable\n";
1558
Bill Wendling847a5c32013-04-16 20:55:47 +00001559 const AttributeSet &Attrs = F->getAttributes();
Bill Wendling04945972013-04-29 23:48:06 +00001560 if (Attrs.hasAttributes(AttributeSet::FunctionIndex)) {
Bill Wendling847a5c32013-04-16 20:55:47 +00001561 AttributeSet AS = Attrs.getFnAttributes();
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +00001562 std::string AttrStr;
1563
1564 unsigned Idx = 0;
1565 for (unsigned E = AS.getNumSlots(); Idx != E; ++Idx)
1566 if (AS.getSlotIndex(Idx) == AttributeSet::FunctionIndex)
1567 break;
1568
1569 for (AttributeSet::iterator I = AS.begin(Idx), E = AS.end(Idx);
1570 I != E; ++I) {
1571 Attribute Attr = *I;
1572 if (!Attr.isStringAttribute()) {
1573 if (!AttrStr.empty()) AttrStr += ' ';
1574 AttrStr += Attr.getAsString();
1575 }
1576 }
1577
Bill Wendling847a5c32013-04-16 20:55:47 +00001578 if (!AttrStr.empty())
1579 Out << "; Function Attrs: " << AttrStr << '\n';
1580 }
1581
Reid Spencer5301e7c2007-01-30 20:08:39 +00001582 if (F->isDeclaration())
Chris Lattner10f03a62007-08-19 22:15:26 +00001583 Out << "declare ";
1584 else
Reid Spencer7ce2d2a2006-12-29 20:29:48 +00001585 Out << "define ";
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001586
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001587 PrintLinkage(F->getLinkage(), Out);
1588 PrintVisibility(F->getVisibility(), Out);
Nico Rieck7157bb72014-01-14 15:22:47 +00001589 PrintDLLStorageClass(F->getDLLStorageClass(), Out);
Chris Lattner379a8d22003-04-16 20:28:45 +00001590
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001591 // Print the calling convention.
Micah Villmow857186d2012-09-13 15:11:12 +00001592 if (F->getCallingConv() != CallingConv::C) {
1593 PrintCallingConv(F->getCallingConv(), Out);
1594 Out << " ";
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001595 }
1596
Chris Lattner229907c2011-07-18 04:54:35 +00001597 FunctionType *FT = F->getFunctionType();
Bill Wendling658d24d2013-01-18 21:53:16 +00001598 if (Attrs.hasAttributes(AttributeSet::ReturnIndex))
1599 Out << Attrs.getAsString(AttributeSet::ReturnIndex) << ' ';
Chris Lattnere101c442009-02-28 21:26:53 +00001600 TypePrinter.print(F->getReturnType(), Out);
Chris Lattner585297e82008-08-19 05:26:17 +00001601 Out << ' ';
Dan Gohmana2489d12010-07-20 23:55:01 +00001602 WriteAsOperandInternal(Out, F, &TypePrinter, &Machine, F->getParent());
Misha Brukmana6619a92004-06-21 21:53:56 +00001603 Out << '(';
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001604 Machine.incorporateFunction(F);
Chris Lattnerfee714f2001-09-07 16:36:04 +00001605
Chris Lattner7bfee412001-10-29 16:05:51 +00001606 // Loop over the arguments, printing them...
Chris Lattnerfee714f2001-09-07 16:36:04 +00001607
Reid Spencer8c4914c2006-12-31 05:24:50 +00001608 unsigned Idx = 1;
Chris Lattner82738fe2007-04-18 00:57:22 +00001609 if (!F->isDeclaration()) {
1610 // If this isn't a declaration, print the argument names as well.
1611 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
1612 I != E; ++I) {
1613 // Insert commas as we go... the first arg doesn't get a comma
1614 if (I != F->arg_begin()) Out << ", ";
Bill Wendling749a43d2012-12-30 13:50:49 +00001615 printArgument(I, Attrs, Idx);
Chris Lattner82738fe2007-04-18 00:57:22 +00001616 Idx++;
1617 }
1618 } else {
1619 // Otherwise, print the types from the function type.
1620 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
1621 // Insert commas as we go... the first arg doesn't get a comma
1622 if (i) Out << ", ";
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001623
Chris Lattner82738fe2007-04-18 00:57:22 +00001624 // Output type...
Chris Lattnere101c442009-02-28 21:26:53 +00001625 TypePrinter.print(FT->getParamType(i), Out);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001626
Bill Wendling749a43d2012-12-30 13:50:49 +00001627 if (Attrs.hasAttributes(i+1))
1628 Out << ' ' << Attrs.getAsString(i+1);
Chris Lattner82738fe2007-04-18 00:57:22 +00001629 }
Reid Spencer8c4914c2006-12-31 05:24:50 +00001630 }
Chris Lattnerfee714f2001-09-07 16:36:04 +00001631
1632 // Finish printing arguments...
Chris Lattner113f4f42002-06-25 16:13:24 +00001633 if (FT->isVarArg()) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001634 if (FT->getNumParams()) Out << ", ";
1635 Out << "..."; // Output varargs portion of signature!
Chris Lattnerfee714f2001-09-07 16:36:04 +00001636 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001637 Out << ')';
Rafael Espindola563eb4b2011-01-25 19:09:56 +00001638 if (F->hasUnnamedAddr())
1639 Out << " unnamed_addr";
Bill Wendling04945972013-04-29 23:48:06 +00001640 if (Attrs.hasAttributes(AttributeSet::FunctionIndex))
1641 Out << " #" << Machine.getAttributeGroupSlot(Attrs.getFnAttributes());
Chris Lattner9380b812010-07-07 23:16:37 +00001642 if (F->hasSection()) {
1643 Out << " section \"";
1644 PrintEscapedString(F->getSection(), Out);
1645 Out << '"';
1646 }
Chris Lattnerf8a974d2005-11-06 06:48:53 +00001647 if (F->getAlignment())
1648 Out << " align " << F->getAlignment();
Gordon Henriksend930f912008-08-17 18:44:35 +00001649 if (F->hasGC())
1650 Out << " gc \"" << F->getGC() << '"';
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00001651 if (F->hasPrefixData()) {
1652 Out << " prefix ";
1653 writeOperand(F->getPrefixData(), true);
1654 }
Reid Spencer5301e7c2007-01-30 20:08:39 +00001655 if (F->isDeclaration()) {
Chris Lattnerfb483622010-09-02 22:52:10 +00001656 Out << '\n';
Chris Lattnerb2f02e52002-05-06 03:00:40 +00001657 } else {
Chris Lattnerfb483622010-09-02 22:52:10 +00001658 Out << " {";
1659 // Output all of the function's basic blocks.
Chris Lattner113f4f42002-06-25 16:13:24 +00001660 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
1661 printBasicBlock(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +00001662
Misha Brukmana6619a92004-06-21 21:53:56 +00001663 Out << "}\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +00001664 }
1665
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001666 Machine.purgeFunction();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001667}
1668
Misha Brukmanc566ca362004-03-02 00:22:19 +00001669/// printArgument - This member is called for every argument that is passed into
1670/// the function. Simply print it out
1671///
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001672void AssemblyWriter::printArgument(const Argument *Arg,
Bill Wendling749a43d2012-12-30 13:50:49 +00001673 AttributeSet Attrs, unsigned Idx) {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001674 // Output type...
Chris Lattnere101c442009-02-28 21:26:53 +00001675 TypePrinter.print(Arg->getType(), Out);
Misha Brukmanb1c93172005-04-21 23:48:37 +00001676
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00001677 // Output parameter attributes list
Bill Wendling749a43d2012-12-30 13:50:49 +00001678 if (Attrs.hasAttributes(Idx))
1679 Out << ' ' << Attrs.getAsString(Idx);
Reid Spencer8c4914c2006-12-31 05:24:50 +00001680
Chris Lattner2f7c9632001-06-06 20:29:01 +00001681 // Output name, if available...
Chris Lattner033935d2008-08-17 04:40:13 +00001682 if (Arg->hasName()) {
1683 Out << ' ';
1684 PrintLLVMName(Out, Arg);
1685 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001686}
1687
Misha Brukmanc566ca362004-03-02 00:22:19 +00001688/// printBasicBlock - This member is called for each basic block in a method.
1689///
Chris Lattner7bfee412001-10-29 16:05:51 +00001690void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Nick Lewycky4d43d3c2008-04-25 16:53:59 +00001691 if (BB->hasName()) { // Print out the label if it exists...
Chris Lattner033935d2008-08-17 04:40:13 +00001692 Out << "\n";
Daniel Dunbare03eecb2009-07-25 23:55:21 +00001693 PrintLLVMName(Out, BB->getName(), LabelPrefix);
Chris Lattner033935d2008-08-17 04:40:13 +00001694 Out << ':';
Nick Lewycky4d43d3c2008-04-25 16:53:59 +00001695 } else if (!BB->use_empty()) { // Don't print block # of no uses...
Bill Wendling35a9c3c2011-04-10 23:18:04 +00001696 Out << "\n; <label>:";
Chris Lattner5e043322007-01-11 03:54:27 +00001697 int Slot = Machine.getLocalSlot(BB);
Chris Lattner757ee0b2004-06-09 19:41:19 +00001698 if (Slot != -1)
Misha Brukmana6619a92004-06-21 21:53:56 +00001699 Out << Slot;
Chris Lattner757ee0b2004-06-09 19:41:19 +00001700 else
Misha Brukmana6619a92004-06-21 21:53:56 +00001701 Out << "<badref>";
Chris Lattner58185f22002-10-02 19:38:55 +00001702 }
Chris Lattner2447ef52003-11-20 00:09:43 +00001703
Craig Topperc6207612014-04-09 06:08:46 +00001704 if (!BB->getParent()) {
Chris Lattneree97b8b2009-08-17 15:48:08 +00001705 Out.PadToColumn(50);
Dan Gohmane2745262009-08-12 17:23:50 +00001706 Out << "; Error: Block without parent!";
1707 } else if (BB != &BB->getParent()->getEntryBlock()) { // Not the entry block?
Chris Lattnerfb483622010-09-02 22:52:10 +00001708 // Output predecessors for the block.
Chris Lattneree97b8b2009-08-17 15:48:08 +00001709 Out.PadToColumn(50);
Dan Gohmane2745262009-08-12 17:23:50 +00001710 Out << ";";
Gabor Greif6c6b2fd2010-03-25 23:25:28 +00001711 const_pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001712
Chris Lattnerff834c02008-04-22 02:45:44 +00001713 if (PI == PE) {
1714 Out << " No predecessors!";
1715 } else {
Dan Gohman81313fd2008-09-14 17:21:12 +00001716 Out << " preds = ";
Chris Lattnerff834c02008-04-22 02:45:44 +00001717 writeOperand(*PI, false);
1718 for (++PI; PI != PE; ++PI) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001719 Out << ", ";
Chris Lattner78e2e8b2006-12-06 06:24:27 +00001720 writeOperand(*PI, false);
Chris Lattner00211f12003-11-16 22:59:57 +00001721 }
Chris Lattner58185f22002-10-02 19:38:55 +00001722 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001723 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001724
Chris Lattnerff834c02008-04-22 02:45:44 +00001725 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001726
Misha Brukmana6619a92004-06-21 21:53:56 +00001727 if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001728
Chris Lattnerfee714f2001-09-07 16:36:04 +00001729 // Output all of the instructions in the basic block...
Dan Gohmana4f709e2009-07-13 18:27:59 +00001730 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
Daniel Maleaded9f932013-05-08 20:38:31 +00001731 printInstructionLine(*I);
Dan Gohmana4f709e2009-07-13 18:27:59 +00001732 }
Chris Lattner96cdd272004-03-08 18:51:45 +00001733
Misha Brukmana6619a92004-06-21 21:53:56 +00001734 if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, Out);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001735}
1736
Daniel Maleaded9f932013-05-08 20:38:31 +00001737/// printInstructionLine - Print an instruction and a newline character.
1738void AssemblyWriter::printInstructionLine(const Instruction &I) {
1739 printInstruction(I);
1740 Out << '\n';
1741}
1742
Misha Brukmanc566ca362004-03-02 00:22:19 +00001743/// printInfoComment - Print a little comment after the instruction indicating
1744/// which slot it occupies.
1745///
Chris Lattner113f4f42002-06-25 16:13:24 +00001746void AssemblyWriter::printInfoComment(const Value &V) {
Bill Wendling847a5c32013-04-16 20:55:47 +00001747 if (AnnotationWriter)
Dan Gohmane34890f2010-02-10 20:41:46 +00001748 AnnotationWriter->printInfoComment(V, Out);
Chris Lattner862e3382001-10-13 06:42:36 +00001749}
1750
Reid Spencere7141c82006-08-28 01:02:49 +00001751// This member is called for each Instruction in a function..
Chris Lattner113f4f42002-06-25 16:13:24 +00001752void AssemblyWriter::printInstruction(const Instruction &I) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001753 if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001754
Dan Gohman466876b2009-08-12 23:32:33 +00001755 // Print out indentation for an instruction.
Dan Gohmanb4583b12009-08-13 01:41:52 +00001756 Out << " ";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001757
1758 // Print out name if it exists...
Chris Lattner033935d2008-08-17 04:40:13 +00001759 if (I.hasName()) {
1760 PrintLLVMName(Out, &I);
1761 Out << " = ";
Chris Lattner031560c2009-12-29 07:25:48 +00001762 } else if (!I.getType()->isVoidTy()) {
Chris Lattnerb25e5ea2008-08-29 17:19:30 +00001763 // Print out the def slot taken.
1764 int SlotNum = Machine.getLocalSlot(&I);
1765 if (SlotNum == -1)
1766 Out << "<badref> = ";
1767 else
1768 Out << '%' << SlotNum << " = ";
Chris Lattner033935d2008-08-17 04:40:13 +00001769 }
Andrew Trickec4b6e72011-09-30 19:48:58 +00001770
Reid Kleckner5772b772014-04-24 20:14:34 +00001771 if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
1772 if (CI->isMustTailCall())
1773 Out << "musttail ";
1774 else if (CI->isTailCall())
1775 Out << "tail ";
1776 }
Chris Lattner504f9242003-09-08 17:45:59 +00001777
Chris Lattner2f7c9632001-06-06 20:29:01 +00001778 // Print out the opcode...
Misha Brukmana6619a92004-06-21 21:53:56 +00001779 Out << I.getOpcodeName();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001780
Eli Friedman02e737b2011-08-12 22:50:01 +00001781 // If this is an atomic load or store, print out the atomic marker.
1782 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isAtomic()) ||
1783 (isa<StoreInst>(I) && cast<StoreInst>(I).isAtomic()))
1784 Out << " atomic";
1785
1786 // If this is a volatile operation, print out the volatile marker.
1787 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) ||
1788 (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile()) ||
1789 (isa<AtomicCmpXchgInst>(I) && cast<AtomicCmpXchgInst>(I).isVolatile()) ||
1790 (isa<AtomicRMWInst>(I) && cast<AtomicRMWInst>(I).isVolatile()))
1791 Out << " volatile";
1792
Dan Gohman9c7f8082009-07-27 16:11:46 +00001793 // Print out optimization information.
1794 WriteOptimizationInfo(Out, &I);
1795
Reid Spencer45e52392006-12-03 06:27:29 +00001796 // Print out the compare instruction predicates
Nate Begemand2195702008-05-12 19:01:56 +00001797 if (const CmpInst *CI = dyn_cast<CmpInst>(&I))
Chris Lattner82ff9232008-08-23 22:52:27 +00001798 Out << ' ' << getPredicateText(CI->getPredicate());
Reid Spencer45e52392006-12-03 06:27:29 +00001799
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001800 // Print out the atomicrmw operation
1801 if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(&I))
1802 writeAtomicRMWOperation(Out, RMWI->getOperation());
1803
Chris Lattner2f7c9632001-06-06 20:29:01 +00001804 // Print out the type of the operands...
Craig Topperc6207612014-04-09 06:08:46 +00001805 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : nullptr;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001806
1807 // Special case conditional branches to swizzle the condition out to the front
Gabor Greifcab008f2009-02-09 15:45:06 +00001808 if (isa<BranchInst>(I) && cast<BranchInst>(I).isConditional()) {
David Blaikieb78e9e52013-02-11 01:16:51 +00001809 const BranchInst &BI(cast<BranchInst>(I));
Dan Gohman81313fd2008-09-14 17:21:12 +00001810 Out << ' ';
Gabor Greifcab008f2009-02-09 15:45:06 +00001811 writeOperand(BI.getCondition(), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00001812 Out << ", ";
Gabor Greifcab008f2009-02-09 15:45:06 +00001813 writeOperand(BI.getSuccessor(0), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00001814 Out << ", ";
Gabor Greifcab008f2009-02-09 15:45:06 +00001815 writeOperand(BI.getSuccessor(1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001816
Chris Lattner8d48df22002-04-13 18:34:38 +00001817 } else if (isa<SwitchInst>(I)) {
David Blaikieb78e9e52013-02-11 01:16:51 +00001818 const SwitchInst& SI(cast<SwitchInst>(I));
Chris Lattner3ed871f2009-10-27 19:13:16 +00001819 // Special case switch instruction to get formatting nice and correct.
Dan Gohman81313fd2008-09-14 17:21:12 +00001820 Out << ' ';
Eli Friedman95031ed2011-09-29 20:21:17 +00001821 writeOperand(SI.getCondition(), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00001822 Out << ", ";
Eli Friedman95031ed2011-09-29 20:21:17 +00001823 writeOperand(SI.getDefaultDest(), true);
Chris Lattner82ff9232008-08-23 22:52:27 +00001824 Out << " [";
David Blaikieb78e9e52013-02-11 01:16:51 +00001825 for (SwitchInst::ConstCaseIt i = SI.case_begin(), e = SI.case_end();
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00001826 i != e; ++i) {
Dan Gohmanb4583b12009-08-13 01:41:52 +00001827 Out << "\n ";
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00001828 writeOperand(i.getCaseValue(), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00001829 Out << ", ";
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00001830 writeOperand(i.getCaseSuccessor(), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001831 }
Dan Gohmanb4583b12009-08-13 01:41:52 +00001832 Out << "\n ]";
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00001833 } else if (isa<IndirectBrInst>(I)) {
1834 // Special case indirectbr instruction to get formatting nice and correct.
Chris Lattner3ed871f2009-10-27 19:13:16 +00001835 Out << ' ';
1836 writeOperand(Operand, true);
Dan Gohman43c57402009-10-30 02:01:10 +00001837 Out << ", [";
Andrew Trickec4b6e72011-09-30 19:48:58 +00001838
Chris Lattner3ed871f2009-10-27 19:13:16 +00001839 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) {
1840 if (i != 1)
1841 Out << ", ";
1842 writeOperand(I.getOperand(i), true);
1843 }
1844 Out << ']';
Jay Foad372ad642011-06-20 14:18:48 +00001845 } else if (const PHINode *PN = dyn_cast<PHINode>(&I)) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001846 Out << ' ';
Chris Lattnere101c442009-02-28 21:26:53 +00001847 TypePrinter.print(I.getType(), Out);
Misha Brukmana6619a92004-06-21 21:53:56 +00001848 Out << ' ';
Chris Lattner2f7c9632001-06-06 20:29:01 +00001849
Jay Foad372ad642011-06-20 14:18:48 +00001850 for (unsigned op = 0, Eop = PN->getNumIncomingValues(); op < Eop; ++op) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001851 if (op) Out << ", ";
Dan Gohman81313fd2008-09-14 17:21:12 +00001852 Out << "[ ";
Jay Foad372ad642011-06-20 14:18:48 +00001853 writeOperand(PN->getIncomingValue(op), false); Out << ", ";
1854 writeOperand(PN->getIncomingBlock(op), false); Out << " ]";
Chris Lattner931ef3b2001-06-11 15:04:20 +00001855 }
Dan Gohmana76f0f72008-05-31 19:12:39 +00001856 } else if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(&I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001857 Out << ' ';
Dan Gohmana76f0f72008-05-31 19:12:39 +00001858 writeOperand(I.getOperand(0), true);
1859 for (const unsigned *i = EVI->idx_begin(), *e = EVI->idx_end(); i != e; ++i)
1860 Out << ", " << *i;
1861 } else if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(&I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001862 Out << ' ';
1863 writeOperand(I.getOperand(0), true); Out << ", ";
Dan Gohmana76f0f72008-05-31 19:12:39 +00001864 writeOperand(I.getOperand(1), true);
1865 for (const unsigned *i = IVI->idx_begin(), *e = IVI->idx_end(); i != e; ++i)
1866 Out << ", " << *i;
Bill Wendlingfae14752011-08-12 20:24:12 +00001867 } else if (const LandingPadInst *LPI = dyn_cast<LandingPadInst>(&I)) {
1868 Out << ' ';
1869 TypePrinter.print(I.getType(), Out);
1870 Out << " personality ";
1871 writeOperand(I.getOperand(0), true); Out << '\n';
1872
1873 if (LPI->isCleanup())
1874 Out << " cleanup";
1875
1876 for (unsigned i = 0, e = LPI->getNumClauses(); i != e; ++i) {
1877 if (i != 0 || LPI->isCleanup()) Out << "\n";
1878 if (LPI->isCatch(i))
1879 Out << " catch ";
1880 else
1881 Out << " filter ";
1882
1883 writeOperand(LPI->getClause(i), true);
1884 }
Devang Patel59643e52008-02-23 00:35:18 +00001885 } else if (isa<ReturnInst>(I) && !Operand) {
1886 Out << " void";
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001887 } else if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
1888 // Print the calling convention being used.
Micah Villmow857186d2012-09-13 15:11:12 +00001889 if (CI->getCallingConv() != CallingConv::C) {
1890 Out << " ";
1891 PrintCallingConv(CI->getCallingConv(), Out);
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001892 }
1893
Gabor Greifc9a92512010-06-23 13:09:06 +00001894 Operand = CI->getCalledValue();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001895 PointerType *PTy = cast<PointerType>(Operand->getType());
1896 FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1897 Type *RetTy = FTy->getReturnType();
Bill Wendlinge94d8432012-12-07 23:16:57 +00001898 const AttributeSet &PAL = CI->getAttributes();
Chris Lattner2f2d9472001-11-06 21:28:12 +00001899
Bill Wendling658d24d2013-01-18 21:53:16 +00001900 if (PAL.hasAttributes(AttributeSet::ReturnIndex))
1901 Out << ' ' << PAL.getAsString(AttributeSet::ReturnIndex);
Devang Patel221fe422008-09-29 20:49:50 +00001902
Chris Lattner463d6a52003-08-05 15:34:45 +00001903 // If possible, print out the short form of the call instruction. We can
Chris Lattner6915f8f2002-04-07 22:49:37 +00001904 // only do this if the first argument is a pointer to a nonvararg function,
Chris Lattner463d6a52003-08-05 15:34:45 +00001905 // and if the return type is not a pointer to a function.
Chris Lattner2f2d9472001-11-06 21:28:12 +00001906 //
Dan Gohman81313fd2008-09-14 17:21:12 +00001907 Out << ' ';
Chris Lattner463d6a52003-08-05 15:34:45 +00001908 if (!FTy->isVarArg() &&
Duncan Sands19d0b472010-02-16 11:11:14 +00001909 (!RetTy->isPointerTy() ||
1910 !cast<PointerType>(RetTy)->getElementType()->isFunctionTy())) {
Chris Lattnere101c442009-02-28 21:26:53 +00001911 TypePrinter.print(RetTy, Out);
Dan Gohman81313fd2008-09-14 17:21:12 +00001912 Out << ' ';
Chris Lattner2f2d9472001-11-06 21:28:12 +00001913 writeOperand(Operand, false);
1914 } else {
1915 writeOperand(Operand, true);
1916 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001917 Out << '(';
Gabor Greifc9a92512010-06-23 13:09:06 +00001918 for (unsigned op = 0, Eop = CI->getNumArgOperands(); op < Eop; ++op) {
1919 if (op > 0)
Dan Gohman81313fd2008-09-14 17:21:12 +00001920 Out << ", ";
Bill Wendling749a43d2012-12-30 13:50:49 +00001921 writeParamOperand(CI->getArgOperand(op), PAL, op + 1);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001922 }
Dan Gohman81313fd2008-09-14 17:21:12 +00001923 Out << ')';
Bill Wendling698e84f2012-12-30 10:32:01 +00001924 if (PAL.hasAttributes(AttributeSet::FunctionIndex))
Bill Wendlinga0323742013-02-22 09:09:42 +00001925 Out << " #" << Machine.getAttributeGroupSlot(PAL.getFnAttributes());
Chris Lattner113f4f42002-06-25 16:13:24 +00001926 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
Gabor Greifa2fbc0a2010-03-24 13:21:49 +00001927 Operand = II->getCalledValue();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001928 PointerType *PTy = cast<PointerType>(Operand->getType());
1929 FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1930 Type *RetTy = FTy->getReturnType();
Bill Wendlinge94d8432012-12-07 23:16:57 +00001931 const AttributeSet &PAL = II->getAttributes();
Chris Lattner463d6a52003-08-05 15:34:45 +00001932
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001933 // Print the calling convention being used.
Micah Villmow857186d2012-09-13 15:11:12 +00001934 if (II->getCallingConv() != CallingConv::C) {
1935 Out << " ";
1936 PrintCallingConv(II->getCallingConv(), Out);
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001937 }
1938
Bill Wendling658d24d2013-01-18 21:53:16 +00001939 if (PAL.hasAttributes(AttributeSet::ReturnIndex))
1940 Out << ' ' << PAL.getAsString(AttributeSet::ReturnIndex);
Devang Patel221fe422008-09-29 20:49:50 +00001941
Chris Lattner463d6a52003-08-05 15:34:45 +00001942 // If possible, print out the short form of the invoke instruction. We can
1943 // only do this if the first argument is a pointer to a nonvararg function,
1944 // and if the return type is not a pointer to a function.
1945 //
Dan Gohmanc7e00ba2008-10-15 18:02:08 +00001946 Out << ' ';
Chris Lattner463d6a52003-08-05 15:34:45 +00001947 if (!FTy->isVarArg() &&
Duncan Sands19d0b472010-02-16 11:11:14 +00001948 (!RetTy->isPointerTy() ||
1949 !cast<PointerType>(RetTy)->getElementType()->isFunctionTy())) {
Chris Lattnere101c442009-02-28 21:26:53 +00001950 TypePrinter.print(RetTy, Out);
Dan Gohmanc7e00ba2008-10-15 18:02:08 +00001951 Out << ' ';
Chris Lattner463d6a52003-08-05 15:34:45 +00001952 writeOperand(Operand, false);
1953 } else {
1954 writeOperand(Operand, true);
1955 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001956 Out << '(';
Gabor Greifc9a92512010-06-23 13:09:06 +00001957 for (unsigned op = 0, Eop = II->getNumArgOperands(); op < Eop; ++op) {
Gabor Greifa2fbc0a2010-03-24 13:21:49 +00001958 if (op)
Dan Gohman81313fd2008-09-14 17:21:12 +00001959 Out << ", ";
Bill Wendling749a43d2012-12-30 13:50:49 +00001960 writeParamOperand(II->getArgOperand(op), PAL, op + 1);
Chris Lattner862e3382001-10-13 06:42:36 +00001961 }
1962
Dan Gohman81313fd2008-09-14 17:21:12 +00001963 Out << ')';
Bill Wendling698e84f2012-12-30 10:32:01 +00001964 if (PAL.hasAttributes(AttributeSet::FunctionIndex))
Bill Wendlinga0323742013-02-22 09:09:42 +00001965 Out << " #" << Machine.getAttributeGroupSlot(PAL.getFnAttributes());
Devang Patela05633e2008-09-26 22:53:05 +00001966
Dan Gohmanb4583b12009-08-13 01:41:52 +00001967 Out << "\n to ";
Chris Lattner862e3382001-10-13 06:42:36 +00001968 writeOperand(II->getNormalDest(), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00001969 Out << " unwind ";
Chris Lattnerfae8ab32004-02-08 21:44:31 +00001970 writeOperand(II->getUnwindDest(), true);
Chris Lattner862e3382001-10-13 06:42:36 +00001971
Victor Hernandez8acf2952009-10-23 21:09:37 +00001972 } else if (const AllocaInst *AI = dyn_cast<AllocaInst>(&I)) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001973 Out << ' ';
Reid Kleckner20844032014-01-25 01:24:06 +00001974 if (AI->isUsedWithInAlloca())
David Majnemerc4ab61c2014-03-09 06:41:58 +00001975 Out << "inalloca ";
1976 TypePrinter.print(AI->getAllocatedType(), Out);
Dan Gohmanb53e26c2009-07-31 18:23:24 +00001977 if (!AI->getArraySize() || AI->isArrayAllocation()) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001978 Out << ", ";
Chris Lattner8d48df22002-04-13 18:34:38 +00001979 writeOperand(AI->getArraySize(), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001980 }
Nate Begeman848622f2005-11-05 09:21:28 +00001981 if (AI->getAlignment()) {
Chris Lattner7aeee3a2005-11-05 21:20:34 +00001982 Out << ", align " << AI->getAlignment();
Nate Begeman848622f2005-11-05 09:21:28 +00001983 }
Chris Lattner862e3382001-10-13 06:42:36 +00001984 } else if (isa<CastInst>(I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001985 if (Operand) {
1986 Out << ' ';
1987 writeOperand(Operand, true); // Work with broken code
1988 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001989 Out << " to ";
Chris Lattnere101c442009-02-28 21:26:53 +00001990 TypePrinter.print(I.getType(), Out);
Chris Lattner5b337482003-10-18 05:57:43 +00001991 } else if (isa<VAArgInst>(I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001992 if (Operand) {
1993 Out << ' ';
1994 writeOperand(Operand, true); // Work with broken code
1995 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001996 Out << ", ";
Chris Lattnere101c442009-02-28 21:26:53 +00001997 TypePrinter.print(I.getType(), Out);
1998 } else if (Operand) { // Print the normal way.
Chris Lattner2f7c9632001-06-06 20:29:01 +00001999
Misha Brukmanb1c93172005-04-21 23:48:37 +00002000 // PrintAllTypes - Instructions who have operands of all the same type
Chris Lattner2f7c9632001-06-06 20:29:01 +00002001 // omit the type from all but the first operand. If the instruction has
2002 // different type operands (for example br), then they are all printed.
2003 bool PrintAllTypes = false;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002004 Type *TheType = Operand->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +00002005
Reid Spencer0cdd04f2007-02-02 13:54:55 +00002006 // Select, Store and ShuffleVector always print all types.
Devang Patelce556d92008-03-04 22:05:14 +00002007 if (isa<SelectInst>(I) || isa<StoreInst>(I) || isa<ShuffleVectorInst>(I)
2008 || isa<ReturnInst>(I)) {
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00002009 PrintAllTypes = true;
2010 } else {
2011 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
2012 Operand = I.getOperand(i);
Nuno Lopes04fe2f02009-01-15 18:40:57 +00002013 // note that Operand shouldn't be null, but the test helps make dump()
2014 // more tolerant of malformed IR
Nuno Lopes0971e772009-01-14 17:51:41 +00002015 if (Operand && Operand->getType() != TheType) {
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00002016 PrintAllTypes = true; // We have differing types! Print them all!
2017 break;
2018 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00002019 }
2020 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00002021
Chris Lattner7bfee412001-10-29 16:05:51 +00002022 if (!PrintAllTypes) {
Misha Brukmana6619a92004-06-21 21:53:56 +00002023 Out << ' ';
Chris Lattnere101c442009-02-28 21:26:53 +00002024 TypePrinter.print(TheType, Out);
Chris Lattner7bfee412001-10-29 16:05:51 +00002025 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00002026
Dan Gohman81313fd2008-09-14 17:21:12 +00002027 Out << ' ';
Chris Lattner113f4f42002-06-25 16:13:24 +00002028 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
Dan Gohman81313fd2008-09-14 17:21:12 +00002029 if (i) Out << ", ";
Chris Lattner113f4f42002-06-25 16:13:24 +00002030 writeOperand(I.getOperand(i), PrintAllTypes);
Chris Lattner2f7c9632001-06-06 20:29:01 +00002031 }
2032 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002033
Eli Friedman59b66882011-08-09 23:02:53 +00002034 // Print atomic ordering/alignment for memory operations
2035 if (const LoadInst *LI = dyn_cast<LoadInst>(&I)) {
2036 if (LI->isAtomic())
2037 writeAtomic(LI->getOrdering(), LI->getSynchScope());
2038 if (LI->getAlignment())
2039 Out << ", align " << LI->getAlignment();
2040 } else if (const StoreInst *SI = dyn_cast<StoreInst>(&I)) {
2041 if (SI->isAtomic())
2042 writeAtomic(SI->getOrdering(), SI->getSynchScope());
2043 if (SI->getAlignment())
2044 Out << ", align " << SI->getAlignment();
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002045 } else if (const AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(&I)) {
Tim Northovere94a5182014-03-11 10:48:52 +00002046 writeAtomicCmpXchg(CXI->getSuccessOrdering(), CXI->getFailureOrdering(),
2047 CXI->getSynchScope());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002048 } else if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(&I)) {
2049 writeAtomic(RMWI->getOrdering(), RMWI->getSynchScope());
Eli Friedmanfee02c62011-07-25 23:16:38 +00002050 } else if (const FenceInst *FI = dyn_cast<FenceInst>(&I)) {
2051 writeAtomic(FI->getOrdering(), FI->getSynchScope());
Christopher Lamb84485702007-04-22 19:24:39 +00002052 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00002053
Chris Lattnerc9558df2009-12-28 20:10:43 +00002054 // Print Metadata info.
Nick Lewyckyba8ec9a2010-02-25 06:53:04 +00002055 SmallVector<std::pair<unsigned, MDNode*>, 4> InstMD;
2056 I.getAllMetadata(InstMD);
Erick Tryzelaar72a37132010-03-02 05:32:52 +00002057 if (!InstMD.empty()) {
2058 SmallVector<StringRef, 8> MDNames;
2059 I.getType()->getContext().getMDKindNames(MDNames);
2060 for (unsigned i = 0, e = InstMD.size(); i != e; ++i) {
2061 unsigned Kind = InstMD[i].first;
2062 if (Kind < MDNames.size()) {
2063 Out << ", !" << MDNames[Kind];
Daniel Maleaded9f932013-05-08 20:38:31 +00002064 } else {
2065 Out << ", !<unknown kind #" << Kind << ">";
2066 }
Dan Gohmana2489d12010-07-20 23:55:01 +00002067 Out << ' ';
2068 WriteAsOperandInternal(Out, InstMD[i].second, &TypePrinter, &Machine,
2069 TheModule);
Nick Lewyckyba8ec9a2010-02-25 06:53:04 +00002070 }
Devang Patelbcdb0252009-10-07 16:37:55 +00002071 }
Chris Lattner862e3382001-10-13 06:42:36 +00002072 printInfoComment(I);
Chris Lattner2f7c9632001-06-06 20:29:01 +00002073}
2074
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002075static void WriteMDNodeComment(const MDNode *Node,
Duncan Sands41b4a6b2010-07-12 08:16:59 +00002076 formatted_raw_ostream &Out) {
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002077 if (Node->getNumOperands() < 1)
2078 return;
Bill Wendling5cb50c52012-06-28 00:41:44 +00002079
2080 Value *Op = Node->getOperand(0);
2081 if (!Op || !isa<ConstantInt>(Op) || cast<ConstantInt>(Op)->getBitWidth() < 32)
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002082 return;
Andrew Trickec4b6e72011-09-30 19:48:58 +00002083
Bill Wendling5cb50c52012-06-28 00:41:44 +00002084 DIDescriptor Desc(Node);
David Blaikiedc69ebb2013-03-11 23:39:23 +00002085 if (!Desc.Verify())
Bill Wendling5cb50c52012-06-28 00:41:44 +00002086 return;
2087
2088 unsigned Tag = Desc.getTag();
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002089 Out.PadToColumn(50);
Bill Wendlinga0bc1082012-07-03 20:01:02 +00002090 if (dwarf::TagString(Tag)) {
2091 Out << "; ";
2092 Desc.print(Out);
2093 } else if (Tag == dwarf::DW_TAG_user_base) {
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002094 Out << "; [ DW_TAG_user_base ]";
Bill Wendlinga0bc1082012-07-03 20:01:02 +00002095 }
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002096}
2097
Daniel Maleaded9f932013-05-08 20:38:31 +00002098void AssemblyWriter::writeMDNode(unsigned Slot, const MDNode *Node) {
2099 Out << '!' << Slot << " = metadata ";
2100 printMDNodeBody(Node);
2101}
2102
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002103void AssemblyWriter::writeAllMDNodes() {
2104 SmallVector<const MDNode *, 16> Nodes;
Chris Lattnercf4a76e2009-12-31 02:20:11 +00002105 Nodes.resize(Machine.mdn_size());
2106 for (SlotTracker::mdn_iterator I = Machine.mdn_begin(), E = Machine.mdn_end();
2107 I != E; ++I)
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002108 Nodes[I->second] = cast<MDNode>(I->first);
Andrew Trickec4b6e72011-09-30 19:48:58 +00002109
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002110 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Daniel Maleaded9f932013-05-08 20:38:31 +00002111 writeMDNode(i, Nodes[i]);
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002112 }
2113}
2114
2115void AssemblyWriter::printMDNodeBody(const MDNode *Node) {
Dan Gohmana2489d12010-07-20 23:55:01 +00002116 WriteMDNodeBodyInternal(Out, Node, &TypePrinter, &Machine, TheModule);
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002117 WriteMDNodeComment(Node, Out);
2118 Out << "\n";
2119}
Chris Lattner2f7c9632001-06-06 20:29:01 +00002120
Bill Wendling829b4782013-02-11 08:43:33 +00002121void AssemblyWriter::writeAllAttributeGroups() {
2122 std::vector<std::pair<AttributeSet, unsigned> > asVec;
2123 asVec.resize(Machine.as_size());
2124
2125 for (SlotTracker::as_iterator I = Machine.as_begin(), E = Machine.as_end();
2126 I != E; ++I)
2127 asVec[I->second] = *I;
2128
2129 for (std::vector<std::pair<AttributeSet, unsigned> >::iterator
2130 I = asVec.begin(), E = asVec.end(); I != E; ++I)
2131 Out << "attributes #" << I->second << " = { "
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +00002132 << I->first.getAsString(AttributeSet::FunctionIndex, true) << " }\n";
Bill Wendling829b4782013-02-11 08:43:33 +00002133}
2134
Daniel Maleaded9f932013-05-08 20:38:31 +00002135} // namespace llvm
2136
Chris Lattner2f7c9632001-06-06 20:29:01 +00002137//===----------------------------------------------------------------------===//
2138// External Interface declarations
2139//===----------------------------------------------------------------------===//
2140
Dan Gohmane2745262009-08-12 17:23:50 +00002141void Module::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW) const {
Chris Lattnere36fd8a2008-08-19 04:26:57 +00002142 SlotTracker SlotTable(this);
Dan Gohmane2745262009-08-12 17:23:50 +00002143 formatted_raw_ostream OS(ROS);
Chris Lattner0c19df42008-08-23 22:23:09 +00002144 AssemblyWriter W(OS, SlotTable, this, AAW);
Chris Lattnerefb5e392009-12-31 02:23:35 +00002145 W.printModule(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00002146}
2147
Rafael Espindola69927782014-04-23 12:23:05 +00002148void NamedMDNode::print(raw_ostream &ROS) const {
Dan Gohman2637cc12010-07-21 23:38:33 +00002149 SlotTracker SlotTable(getParent());
2150 formatted_raw_ostream OS(ROS);
Rafael Espindola69927782014-04-23 12:23:05 +00002151 AssemblyWriter W(OS, SlotTable, getParent(), nullptr);
Dan Gohman2637cc12010-07-21 23:38:33 +00002152 W.printNamedMDNode(this);
2153}
2154
Chris Lattner2ee62d22009-02-28 21:11:05 +00002155void Type::print(raw_ostream &OS) const {
Craig Topperc6207612014-04-09 06:08:46 +00002156 if (!this) {
Chris Lattner2ee62d22009-02-28 21:11:05 +00002157 OS << "<null Type>";
2158 return;
2159 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002160 TypePrinting TP;
2161 TP.print(const_cast<Type*>(this), OS);
Andrew Trickec4b6e72011-09-30 19:48:58 +00002162
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002163 // If the type is a named struct type, print the body as well.
2164 if (StructType *STy = dyn_cast<StructType>(const_cast<Type*>(this)))
Chris Lattner01beceb2011-08-12 18:07:07 +00002165 if (!STy->isLiteral()) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002166 OS << " = type ";
2167 TP.printStructBody(STy, OS);
2168 }
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00002169}
2170
David Blaikieb38ac1f2014-04-05 23:33:25 +00002171void Value::print(raw_ostream &ROS) const {
2172 if (!this) {
Dan Gohman12dad632009-08-12 20:56:03 +00002173 ROS << "printing a <null> value\n";
Chris Lattner0c19df42008-08-23 22:23:09 +00002174 return;
2175 }
Dan Gohman12dad632009-08-12 20:56:03 +00002176 formatted_raw_ostream OS(ROS);
Chris Lattner0c19df42008-08-23 22:23:09 +00002177 if (const Instruction *I = dyn_cast<Instruction>(this)) {
David Blaikieb38ac1f2014-04-05 23:33:25 +00002178 const Function *F = I->getParent() ? I->getParent()->getParent() : nullptr;
Chris Lattner0c19df42008-08-23 22:23:09 +00002179 SlotTracker SlotTable(F);
David Blaikieb38ac1f2014-04-05 23:33:25 +00002180 AssemblyWriter W(OS, SlotTable, getModuleFromVal(I), nullptr);
Chris Lattnerefb5e392009-12-31 02:23:35 +00002181 W.printInstruction(*I);
Chris Lattner0c19df42008-08-23 22:23:09 +00002182 } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(this)) {
2183 SlotTracker SlotTable(BB->getParent());
David Blaikieb38ac1f2014-04-05 23:33:25 +00002184 AssemblyWriter W(OS, SlotTable, getModuleFromVal(BB), nullptr);
Chris Lattnerefb5e392009-12-31 02:23:35 +00002185 W.printBasicBlock(BB);
Chris Lattner0c19df42008-08-23 22:23:09 +00002186 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(this)) {
2187 SlotTracker SlotTable(GV->getParent());
David Blaikieb38ac1f2014-04-05 23:33:25 +00002188 AssemblyWriter W(OS, SlotTable, GV->getParent(), nullptr);
Chris Lattnerefb5e392009-12-31 02:23:35 +00002189 if (const GlobalVariable *V = dyn_cast<GlobalVariable>(GV))
2190 W.printGlobal(V);
2191 else if (const Function *F = dyn_cast<Function>(GV))
2192 W.printFunction(F);
2193 else
2194 W.printAlias(cast<GlobalAlias>(GV));
Devang Patel5546b982009-07-01 20:59:15 +00002195 } else if (const MDNode *N = dyn_cast<MDNode>(this)) {
Victor Hernandeze5f2af72010-01-20 04:45:57 +00002196 const Function *F = N->getFunction();
Victor Hernandez61e6e822010-01-14 01:47:37 +00002197 SlotTracker SlotTable(F);
David Blaikieb38ac1f2014-04-05 23:33:25 +00002198 AssemblyWriter W(OS, SlotTable, F ? F->getParent() : nullptr, nullptr);
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002199 W.printMDNodeBody(N);
Chris Lattner0c19df42008-08-23 22:23:09 +00002200 } else if (const Constant *C = dyn_cast<Constant>(this)) {
Chris Lattner92c5c122009-02-28 23:20:19 +00002201 TypePrinting TypePrinter;
Chris Lattnere101c442009-02-28 21:26:53 +00002202 TypePrinter.print(C->getType(), OS);
Chris Lattner2ee62d22009-02-28 21:11:05 +00002203 OS << ' ';
David Blaikieb38ac1f2014-04-05 23:33:25 +00002204 WriteConstantInternal(OS, C, TypePrinter, nullptr, nullptr);
Chris Lattner31fcc282009-12-31 01:41:14 +00002205 } else if (isa<InlineAsm>(this) || isa<MDString>(this) ||
2206 isa<Argument>(this)) {
Chandler Carruthd48cdbf2014-01-09 02:29:41 +00002207 this->printAsOperand(OS);
Chris Lattner0c19df42008-08-23 22:23:09 +00002208 } else {
Nick Lewyckyad1b3d12014-05-09 00:49:03 +00002209 llvm_unreachable("Unknown value to print out!");
Chris Lattner0c19df42008-08-23 22:23:09 +00002210 }
2211}
2212
Chandler Carruthd48cdbf2014-01-09 02:29:41 +00002213void Value::printAsOperand(raw_ostream &O, bool PrintType, const Module *M) const {
2214 // Fast path: Don't construct and populate a TypePrinting object if we
2215 // won't be needing any types printed.
2216 if (!PrintType &&
2217 ((!isa<Constant>(this) && !isa<MDNode>(this)) ||
2218 hasName() || isa<GlobalValue>(this))) {
Craig Topperc6207612014-04-09 06:08:46 +00002219 WriteAsOperandInternal(O, this, nullptr, nullptr, M);
Chandler Carruthd48cdbf2014-01-09 02:29:41 +00002220 return;
2221 }
2222
2223 if (!M)
2224 M = getModuleFromVal(this);
2225
2226 TypePrinting TypePrinter;
2227 if (M)
2228 TypePrinter.incorporateTypes(*M);
2229 if (PrintType) {
2230 TypePrinter.print(getType(), O);
2231 O << ' ';
2232 }
2233
Craig Topperc6207612014-04-09 06:08:46 +00002234 WriteAsOperandInternal(O, this, &TypePrinter, nullptr, M);
Chandler Carruthd48cdbf2014-01-09 02:29:41 +00002235}
2236
Chris Lattnerdab942552008-08-25 17:03:15 +00002237// Value::dump - allow easy printing of Values from the debugger.
David Greenec7f9b122010-01-05 01:29:26 +00002238void Value::dump() const { print(dbgs()); dbgs() << '\n'; }
Reid Spencer52641832004-05-25 18:14:38 +00002239
Chris Lattnerdab942552008-08-25 17:03:15 +00002240// Type::dump - allow easy printing of Types from the debugger.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002241void Type::dump() const { print(dbgs()); }
Chris Lattner24025262009-02-28 21:05:51 +00002242
Chris Lattnerdab942552008-08-25 17:03:15 +00002243// Module::dump() - Allow printing of Modules from the debugger.
Craig Topperc6207612014-04-09 06:08:46 +00002244void Module::dump() const { print(dbgs(), nullptr); }
Bill Wendling3681d112011-12-09 23:18:34 +00002245
2246// NamedMDNode::dump() - Allow printing of NamedMDNodes from the debugger.
Rafael Espindola69927782014-04-23 12:23:05 +00002247void NamedMDNode::dump() const { print(dbgs()); }