blob: f275305cd99fb7ea23a6b2242b0a50d9898bf9ad [file] [log] [blame]
Chris Lattnerf7e79482002-04-07 22:31:46 +00001//===-- AsmWriter.cpp - Printing LLVM as an assembly file -----------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
10// This library implements the functionality defined in llvm/Assembly/Writer.h
11//
Chris Lattner189088e2002-04-12 18:21:53 +000012// Note that these routines must be extremely tolerant of various errors in the
Chris Lattnerf70da102003-05-08 02:44:12 +000013// LLVM code, because it can be used for debugging transformations.
Chris Lattner189088e2002-04-12 18:21:53 +000014//
Chris Lattner2f7c9632001-06-06 20:29:01 +000015//===----------------------------------------------------------------------===//
16
Daniel Maleafddddbe2013-05-23 22:34:33 +000017#include "AsmWriter.h"
18
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +000019#include "llvm/Assembly/Writer.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/ADT/DenseMap.h"
21#include "llvm/ADT/STLExtras.h"
22#include "llvm/ADT/SmallString.h"
23#include "llvm/ADT/StringExtras.h"
Chris Lattnerad074cb2010-09-02 23:09:42 +000024#include "llvm/Assembly/AssemblyAnnotationWriter.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000025#include "llvm/Assembly/PrintModulePass.h"
Bill Wendling5cb50c52012-06-28 00:41:44 +000026#include "llvm/DebugInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000027#include "llvm/IR/CallingConv.h"
28#include "llvm/IR/Constants.h"
29#include "llvm/IR/DerivedTypes.h"
30#include "llvm/IR/InlineAsm.h"
31#include "llvm/IR/IntrinsicInst.h"
32#include "llvm/IR/LLVMContext.h"
33#include "llvm/IR/Module.h"
34#include "llvm/IR/Operator.h"
Chandler Carruthdcb603f2013-01-07 15:43:51 +000035#include "llvm/IR/TypeFinder.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000036#include "llvm/IR/ValueSymbolTable.h"
Bill Wendlingdfc91892006-11-28 02:09:03 +000037#include "llvm/Support/CFG.h"
David Greenec7f9b122010-01-05 01:29:26 +000038#include "llvm/Support/Debug.h"
Devang Patel711ab5b2009-09-30 20:16:54 +000039#include "llvm/Support/Dwarf.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000040#include "llvm/Support/ErrorHandling.h"
Dan Gohmane2745262009-08-12 17:23:50 +000041#include "llvm/Support/FormattedStream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000042#include "llvm/Support/MathExtras.h"
Daniel Maleafddddbe2013-05-23 22:34:33 +000043
Chris Lattnerfee714f2001-09-07 16:36:04 +000044#include <algorithm>
Reid Spencerbdf03b42007-05-22 19:27:35 +000045#include <cctype>
Chris Lattner189d19f2003-11-21 20:23:48 +000046using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000047
Reid Spencer294715b2005-05-15 16:13:11 +000048// Make virtual table appear in this compilation unit.
49AssemblyAnnotationWriter::~AssemblyAnnotationWriter() {}
50
Chris Lattner3eee99c2008-08-19 04:36:02 +000051//===----------------------------------------------------------------------===//
52// Helper Functions
53//===----------------------------------------------------------------------===//
54
55static const Module *getModuleFromVal(const Value *V) {
56 if (const Argument *MA = dyn_cast<Argument>(V))
57 return MA->getParent() ? MA->getParent()->getParent() : 0;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +000058
Chris Lattner3eee99c2008-08-19 04:36:02 +000059 if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
60 return BB->getParent() ? BB->getParent()->getParent() : 0;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +000061
Chris Lattner3eee99c2008-08-19 04:36:02 +000062 if (const Instruction *I = dyn_cast<Instruction>(V)) {
63 const Function *M = I->getParent() ? I->getParent()->getParent() : 0;
64 return M ? M->getParent() : 0;
65 }
Andrew Trickec4b6e72011-09-30 19:48:58 +000066
Chris Lattner3eee99c2008-08-19 04:36:02 +000067 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
68 return GV->getParent();
69 return 0;
70}
71
Bill Wendling90bc19c2013-02-20 07:21:42 +000072static void PrintCallingConv(unsigned cc, raw_ostream &Out) {
Micah Villmow857186d2012-09-13 15:11:12 +000073 switch (cc) {
Bill Wendling90bc19c2013-02-20 07:21:42 +000074 default: Out << "cc" << cc; break;
75 case CallingConv::Fast: Out << "fastcc"; break;
76 case CallingConv::Cold: Out << "coldcc"; break;
77 case CallingConv::X86_StdCall: Out << "x86_stdcallcc"; break;
78 case CallingConv::X86_FastCall: Out << "x86_fastcallcc"; break;
79 case CallingConv::X86_ThisCall: Out << "x86_thiscallcc"; break;
80 case CallingConv::Intel_OCL_BI: Out << "intel_ocl_bicc"; break;
81 case CallingConv::ARM_APCS: Out << "arm_apcscc"; break;
82 case CallingConv::ARM_AAPCS: Out << "arm_aapcscc"; break;
83 case CallingConv::ARM_AAPCS_VFP: Out << "arm_aapcs_vfpcc"; break;
84 case CallingConv::MSP430_INTR: Out << "msp430_intrcc"; break;
85 case CallingConv::PTX_Kernel: Out << "ptx_kernel"; break;
86 case CallingConv::PTX_Device: Out << "ptx_device"; break;
Charles Davise8f297c2013-07-12 06:02:35 +000087 case CallingConv::X86_64_SysV: Out << "x86_64_sysvcc"; break;
88 case CallingConv::X86_64_Win64: Out << "x86_64_win64cc"; break;
Micah Villmow857186d2012-09-13 15:11:12 +000089 }
90}
Michael Ilseman26ee2b82012-11-15 22:34:00 +000091
Daniel Dunbardb0b70a2008-10-28 19:33:02 +000092// PrintEscapedString - Print each character of the specified string, escaping
93// it if it is not printable or if it is an escape char.
Chris Lattner9380b812010-07-07 23:16:37 +000094static void PrintEscapedString(StringRef Name, raw_ostream &Out) {
Daniel Dunbare03eecb2009-07-25 23:55:21 +000095 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
96 unsigned char C = Name[i];
Nick Lewycky5aa592a2009-03-15 06:39:52 +000097 if (isprint(C) && C != '\\' && C != '"')
Daniel Dunbardb0b70a2008-10-28 19:33:02 +000098 Out << C;
99 else
100 Out << '\\' << hexdigit(C >> 4) << hexdigit(C & 0x0F);
101 }
102}
103
Chris Lattner3eee99c2008-08-19 04:36:02 +0000104enum PrefixType {
105 GlobalPrefix,
106 LabelPrefix,
Daniel Dunbar389529a2008-10-14 23:28:09 +0000107 LocalPrefix,
108 NoPrefix
Chris Lattner3eee99c2008-08-19 04:36:02 +0000109};
110
111/// PrintLLVMName - Turn the specified name into an 'LLVM name', which is either
112/// prefixed with % (if the string only contains simple characters) or is
113/// surrounded with ""'s (if it has special chars in it). Print it out.
Benjamin Kramer92d89982010-07-14 22:38:02 +0000114static void PrintLLVMName(raw_ostream &OS, StringRef Name, PrefixType Prefix) {
Jay Foadf7adb322011-04-24 14:30:00 +0000115 assert(!Name.empty() && "Cannot get empty name!");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000116 switch (Prefix) {
Daniel Dunbar389529a2008-10-14 23:28:09 +0000117 case NoPrefix: break;
Chris Lattner1508d3f2008-08-19 05:16:28 +0000118 case GlobalPrefix: OS << '@'; break;
119 case LabelPrefix: break;
120 case LocalPrefix: OS << '%'; break;
Nick Lewycky063699a2009-03-19 06:31:22 +0000121 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000122
Chris Lattner3eee99c2008-08-19 04:36:02 +0000123 // Scan the name to see if it needs quotes first.
Guy Benyei83c74e92013-02-12 21:21:59 +0000124 bool NeedsQuotes = isdigit(static_cast<unsigned char>(Name[0]));
Chris Lattner3eee99c2008-08-19 04:36:02 +0000125 if (!NeedsQuotes) {
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000126 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
Aaron Ballmaned9b0a92012-07-16 16:18:18 +0000127 // By making this unsigned, the value passed in to isalnum will always be
128 // in the range 0-255. This is important when building with MSVC because
129 // its implementation will assert. This situation can arise when dealing
130 // with UTF-8 multibyte characters.
131 unsigned char C = Name[i];
Guy Benyei83c74e92013-02-12 21:21:59 +0000132 if (!isalnum(static_cast<unsigned char>(C)) && C != '-' && C != '.' &&
133 C != '_') {
Chris Lattner3eee99c2008-08-19 04:36:02 +0000134 NeedsQuotes = true;
135 break;
136 }
137 }
138 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000139
Chris Lattner3eee99c2008-08-19 04:36:02 +0000140 // If we didn't need any quotes, just write out the name in one blast.
141 if (!NeedsQuotes) {
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000142 OS << Name;
Chris Lattner3eee99c2008-08-19 04:36:02 +0000143 return;
144 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000145
Chris Lattner3eee99c2008-08-19 04:36:02 +0000146 // Okay, we need quotes. Output the quotes and escape any scary characters as
147 // needed.
148 OS << '"';
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000149 PrintEscapedString(Name, OS);
Chris Lattner3eee99c2008-08-19 04:36:02 +0000150 OS << '"';
151}
152
153/// PrintLLVMName - Turn the specified name into an 'LLVM name', which is either
154/// prefixed with % (if the string only contains simple characters) or is
155/// surrounded with ""'s (if it has special chars in it). Print it out.
Dan Gohman12dad632009-08-12 20:56:03 +0000156static void PrintLLVMName(raw_ostream &OS, const Value *V) {
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000157 PrintLLVMName(OS, V->getName(),
Chris Lattner3eee99c2008-08-19 04:36:02 +0000158 isa<GlobalValue>(V) ? GlobalPrefix : LocalPrefix);
159}
160
Chris Lattner0f578952009-02-28 20:25:14 +0000161
Daniel Maleaded9f932013-05-08 20:38:31 +0000162namespace llvm {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000163
164void TypePrinting::incorporateTypes(const Module &M) {
Bill Wendling8555a372012-08-03 00:30:35 +0000165 NamedTypes.run(M, false);
Andrew Trickec4b6e72011-09-30 19:48:58 +0000166
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000167 // The list of struct types we got back includes all the struct types, split
168 // the unnamed ones out to a numbering and remove the anonymous structs.
169 unsigned NextNumber = 0;
Andrew Trickec4b6e72011-09-30 19:48:58 +0000170
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000171 std::vector<StructType*>::iterator NextToUse = NamedTypes.begin(), I, E;
172 for (I = NamedTypes.begin(), E = NamedTypes.end(); I != E; ++I) {
173 StructType *STy = *I;
Andrew Trickec4b6e72011-09-30 19:48:58 +0000174
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000175 // Ignore anonymous types.
Chris Lattner01beceb2011-08-12 18:07:07 +0000176 if (STy->isLiteral())
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000177 continue;
Andrew Trickec4b6e72011-09-30 19:48:58 +0000178
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000179 if (STy->getName().empty())
180 NumberedTypes[STy] = NextNumber++;
181 else
182 *NextToUse++ = STy;
183 }
Andrew Trickec4b6e72011-09-30 19:48:58 +0000184
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000185 NamedTypes.erase(NextToUse, NamedTypes.end());
186}
187
188
Chris Lattner40959d02009-02-28 20:49:40 +0000189/// CalcTypeName - Write the specified type to the specified raw_ostream, making
190/// use of type names or up references to shorten the type name where possible.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000191void TypePrinting::print(Type *Ty, raw_ostream &OS) {
Chris Lattner0f578952009-02-28 20:25:14 +0000192 switch (Ty->getTypeID()) {
Chris Lattner06a23212009-02-28 21:27:31 +0000193 case Type::VoidTyID: OS << "void"; break;
Dan Gohman518cda42011-12-17 00:04:22 +0000194 case Type::HalfTyID: OS << "half"; break;
Chris Lattner06a23212009-02-28 21:27:31 +0000195 case Type::FloatTyID: OS << "float"; break;
196 case Type::DoubleTyID: OS << "double"; break;
197 case Type::X86_FP80TyID: OS << "x86_fp80"; break;
198 case Type::FP128TyID: OS << "fp128"; break;
199 case Type::PPC_FP128TyID: OS << "ppc_fp128"; break;
200 case Type::LabelTyID: OS << "label"; break;
Nick Lewyckyadbc2842009-05-30 05:06:04 +0000201 case Type::MetadataTyID: OS << "metadata"; break;
Dale Johannesenbaa5d042010-09-10 20:55:01 +0000202 case Type::X86_MMXTyID: OS << "x86_mmx"; break;
Chris Lattner68318b12009-02-28 21:18:43 +0000203 case Type::IntegerTyID:
Chris Lattner06a23212009-02-28 21:27:31 +0000204 OS << 'i' << cast<IntegerType>(Ty)->getBitWidth();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000205 return;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000206
Chris Lattner07d88292009-02-28 20:35:42 +0000207 case Type::FunctionTyID: {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000208 FunctionType *FTy = cast<FunctionType>(Ty);
209 print(FTy->getReturnType(), OS);
Chris Lattner06a23212009-02-28 21:27:31 +0000210 OS << " (";
Chris Lattner07d88292009-02-28 20:35:42 +0000211 for (FunctionType::param_iterator I = FTy->param_begin(),
212 E = FTy->param_end(); I != E; ++I) {
213 if (I != FTy->param_begin())
Chris Lattner06a23212009-02-28 21:27:31 +0000214 OS << ", ";
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000215 print(*I, OS);
Chris Lattner0f578952009-02-28 20:25:14 +0000216 }
Chris Lattner07d88292009-02-28 20:35:42 +0000217 if (FTy->isVarArg()) {
Chris Lattner06a23212009-02-28 21:27:31 +0000218 if (FTy->getNumParams()) OS << ", ";
219 OS << "...";
Chris Lattner0f578952009-02-28 20:25:14 +0000220 }
Chris Lattner06a23212009-02-28 21:27:31 +0000221 OS << ')';
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000222 return;
Chris Lattner07d88292009-02-28 20:35:42 +0000223 }
224 case Type::StructTyID: {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000225 StructType *STy = cast<StructType>(Ty);
Andrew Trickec4b6e72011-09-30 19:48:58 +0000226
Chris Lattner01beceb2011-08-12 18:07:07 +0000227 if (STy->isLiteral())
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000228 return printStructBody(STy, OS);
229
230 if (!STy->getName().empty())
231 return PrintLLVMName(OS, STy->getName(), LocalPrefix);
Andrew Trickec4b6e72011-09-30 19:48:58 +0000232
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000233 DenseMap<StructType*, unsigned>::iterator I = NumberedTypes.find(STy);
234 if (I != NumberedTypes.end())
235 OS << '%' << I->second;
236 else // Not enumerated, print the hex address.
Benjamin Kramer4e8b4632011-11-02 17:24:36 +0000237 OS << "%\"type " << STy << '\"';
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000238 return;
Chris Lattner07d88292009-02-28 20:35:42 +0000239 }
240 case Type::PointerTyID: {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000241 PointerType *PTy = cast<PointerType>(Ty);
242 print(PTy->getElementType(), OS);
Chris Lattner07d88292009-02-28 20:35:42 +0000243 if (unsigned AddressSpace = PTy->getAddressSpace())
Chris Lattner06a23212009-02-28 21:27:31 +0000244 OS << " addrspace(" << AddressSpace << ')';
245 OS << '*';
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000246 return;
Chris Lattner07d88292009-02-28 20:35:42 +0000247 }
248 case Type::ArrayTyID: {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000249 ArrayType *ATy = cast<ArrayType>(Ty);
Chris Lattner06a23212009-02-28 21:27:31 +0000250 OS << '[' << ATy->getNumElements() << " x ";
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000251 print(ATy->getElementType(), OS);
Chris Lattner06a23212009-02-28 21:27:31 +0000252 OS << ']';
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000253 return;
Chris Lattner07d88292009-02-28 20:35:42 +0000254 }
255 case Type::VectorTyID: {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000256 VectorType *PTy = cast<VectorType>(Ty);
Chris Lattner06a23212009-02-28 21:27:31 +0000257 OS << "<" << PTy->getNumElements() << " x ";
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000258 print(PTy->getElementType(), OS);
Chris Lattner06a23212009-02-28 21:27:31 +0000259 OS << '>';
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000260 return;
Chris Lattner07d88292009-02-28 20:35:42 +0000261 }
Chris Lattner07d88292009-02-28 20:35:42 +0000262 default:
Chris Lattner06a23212009-02-28 21:27:31 +0000263 OS << "<unrecognized-type>";
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000264 return;
Chris Lattner0f578952009-02-28 20:25:14 +0000265 }
Chris Lattner0f578952009-02-28 20:25:14 +0000266}
267
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000268void TypePrinting::printStructBody(StructType *STy, raw_ostream &OS) {
269 if (STy->isOpaque()) {
270 OS << "opaque";
271 return;
Chris Lattner0f578952009-02-28 20:25:14 +0000272 }
Andrew Trickec4b6e72011-09-30 19:48:58 +0000273
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000274 if (STy->isPacked())
275 OS << '<';
Andrew Trickec4b6e72011-09-30 19:48:58 +0000276
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000277 if (STy->getNumElements() == 0) {
278 OS << "{}";
279 } else {
280 StructType::element_iterator I = STy->element_begin();
281 OS << "{ ";
282 print(*I++, OS);
283 for (StructType::element_iterator E = STy->element_end(); I != E; ++I) {
284 OS << ", ";
285 print(*I, OS);
Chris Lattner3243ea12009-03-01 00:03:38 +0000286 }
Andrew Trickec4b6e72011-09-30 19:48:58 +0000287
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000288 OS << " }";
Chris Lattner92c5c122009-02-28 23:20:19 +0000289 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000290 if (STy->isPacked())
291 OS << '>';
Chris Lattner92c5c122009-02-28 23:20:19 +0000292}
293
Chris Lattner3eee99c2008-08-19 04:36:02 +0000294//===----------------------------------------------------------------------===//
295// SlotTracker Class: Enumerate slot numbers for unnamed values
296//===----------------------------------------------------------------------===//
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000297/// This class provides computation of slot numbers for LLVM Assembly writing.
Chris Lattner393b7cd2008-08-17 04:17:45 +0000298///
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000299class SlotTracker {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000300public:
Devang Patel983c6b12009-07-08 21:44:25 +0000301 /// ValueMap - A mapping of Values to slot numbers.
Chris Lattnera204d412008-08-17 17:25:25 +0000302 typedef DenseMap<const Value*, unsigned> ValueMap;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000303
304private:
Devang Patel983c6b12009-07-08 21:44:25 +0000305 /// TheModule - The module for which we are holding slot numbers.
Chris Lattner393b7cd2008-08-17 04:17:45 +0000306 const Module* TheModule;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000307
Devang Patel983c6b12009-07-08 21:44:25 +0000308 /// TheFunction - The function for which we are holding slot numbers.
Chris Lattner393b7cd2008-08-17 04:17:45 +0000309 const Function* TheFunction;
310 bool FunctionProcessed;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000311
Jay Foaddbb221d2011-07-11 07:28:49 +0000312 /// mMap - The slot map for the module level data.
Chris Lattner393b7cd2008-08-17 04:17:45 +0000313 ValueMap mMap;
314 unsigned mNext;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000315
Jay Foaddbb221d2011-07-11 07:28:49 +0000316 /// fMap - The slot map for the function level data.
Chris Lattner393b7cd2008-08-17 04:17:45 +0000317 ValueMap fMap;
318 unsigned fNext;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000319
Devang Patel983c6b12009-07-08 21:44:25 +0000320 /// mdnMap - Map for MDNodes.
Chris Lattnercf4a76e2009-12-31 02:20:11 +0000321 DenseMap<const MDNode*, unsigned> mdnMap;
Devang Patel983c6b12009-07-08 21:44:25 +0000322 unsigned mdnNext;
Bill Wendling829b4782013-02-11 08:43:33 +0000323
324 /// asMap - The slot map for attribute sets.
325 DenseMap<AttributeSet, unsigned> asMap;
326 unsigned asNext;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000327public:
Chris Lattner393b7cd2008-08-17 04:17:45 +0000328 /// Construct from a module
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000329 explicit SlotTracker(const Module *M);
Chris Lattner393b7cd2008-08-17 04:17:45 +0000330 /// Construct from a function, starting out in incorp state.
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000331 explicit SlotTracker(const Function *F);
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000332
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000333 /// Return the slot number of the specified value in it's type
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000334 /// plane. If something is not in the SlotTracker, return -1.
Chris Lattner5e043322007-01-11 03:54:27 +0000335 int getLocalSlot(const Value *V);
336 int getGlobalSlot(const GlobalValue *V);
Devang Patel983c6b12009-07-08 21:44:25 +0000337 int getMetadataSlot(const MDNode *N);
Bill Wendling829b4782013-02-11 08:43:33 +0000338 int getAttributeGroupSlot(AttributeSet AS);
Reid Spencer8beac692004-06-09 15:26:53 +0000339
Misha Brukmanb1c93172005-04-21 23:48:37 +0000340 /// If you'd like to deal with a function instead of just a module, use
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000341 /// this method to get its data into the SlotTracker.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000342 void incorporateFunction(const Function *F) {
343 TheFunction = F;
Reid Spencerb0ac8c42004-08-16 07:46:33 +0000344 FunctionProcessed = false;
345 }
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000346
Misha Brukmanb1c93172005-04-21 23:48:37 +0000347 /// After calling incorporateFunction, use this method to remove the
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000348 /// most recently incorporated function from the SlotTracker. This
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000349 /// will reset the state of the machine back to just the module contents.
350 void purgeFunction();
351
Devang Patel983c6b12009-07-08 21:44:25 +0000352 /// MDNode map iterators.
Chris Lattnercf4a76e2009-12-31 02:20:11 +0000353 typedef DenseMap<const MDNode*, unsigned>::iterator mdn_iterator;
354 mdn_iterator mdn_begin() { return mdnMap.begin(); }
355 mdn_iterator mdn_end() { return mdnMap.end(); }
356 unsigned mdn_size() const { return mdnMap.size(); }
357 bool mdn_empty() const { return mdnMap.empty(); }
Devang Patel983c6b12009-07-08 21:44:25 +0000358
Bill Wendling829b4782013-02-11 08:43:33 +0000359 /// AttributeSet map iterators.
360 typedef DenseMap<AttributeSet, unsigned>::iterator as_iterator;
361 as_iterator as_begin() { return asMap.begin(); }
362 as_iterator as_end() { return asMap.end(); }
363 unsigned as_size() const { return asMap.size(); }
364 bool as_empty() const { return asMap.empty(); }
365
Reid Spencer56010e42004-05-26 21:56:09 +0000366 /// This function does the actual initialization.
367 inline void initialize();
368
Devang Patel983c6b12009-07-08 21:44:25 +0000369 // Implementation Details
370private:
Chris Lattnerea862a32007-01-09 07:55:49 +0000371 /// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
372 void CreateModuleSlot(const GlobalValue *V);
Devang Patel983c6b12009-07-08 21:44:25 +0000373
374 /// CreateMetadataSlot - Insert the specified MDNode* into the slot table.
375 void CreateMetadataSlot(const MDNode *N);
376
Chris Lattnerea862a32007-01-09 07:55:49 +0000377 /// CreateFunctionSlot - Insert the specified Value* into the slot table.
378 void CreateFunctionSlot(const Value *V);
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000379
Bill Wendling829b4782013-02-11 08:43:33 +0000380 /// \brief Insert the specified AttributeSet into the slot table.
381 void CreateAttributeSetSlot(AttributeSet AS);
382
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000383 /// Add all of the module level global variables (and their initializers)
384 /// and function declarations, but not the contents of those functions.
385 void processModule();
386
Devang Patel983c6b12009-07-08 21:44:25 +0000387 /// Add all of the functions arguments, basic blocks, and instructions.
Reid Spencer56010e42004-05-26 21:56:09 +0000388 void processFunction();
389
Craig Toppera60c0f12012-09-15 17:09:36 +0000390 SlotTracker(const SlotTracker &) LLVM_DELETED_FUNCTION;
391 void operator=(const SlotTracker &) LLVM_DELETED_FUNCTION;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000392};
393
Daniel Maleaded9f932013-05-08 20:38:31 +0000394SlotTracker *createSlotTracker(const Module *M) {
395 return new SlotTracker(M);
396}
Chris Lattner3eee99c2008-08-19 04:36:02 +0000397
398static SlotTracker *createSlotTracker(const Value *V) {
399 if (const Argument *FA = dyn_cast<Argument>(V))
400 return new SlotTracker(FA->getParent());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000401
Chris Lattner3eee99c2008-08-19 04:36:02 +0000402 if (const Instruction *I = dyn_cast<Instruction>(V))
Andrew Trick2f0cbf62011-09-30 19:50:40 +0000403 if (I->getParent())
404 return new SlotTracker(I->getParent()->getParent());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000405
Chris Lattner3eee99c2008-08-19 04:36:02 +0000406 if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
407 return new SlotTracker(BB->getParent());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000408
Chris Lattner3eee99c2008-08-19 04:36:02 +0000409 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
410 return new SlotTracker(GV->getParent());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000411
Chris Lattner3eee99c2008-08-19 04:36:02 +0000412 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V))
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000413 return new SlotTracker(GA->getParent());
414
Chris Lattner3eee99c2008-08-19 04:36:02 +0000415 if (const Function *Func = dyn_cast<Function>(V))
416 return new SlotTracker(Func);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000417
Dan Gohmana2489d12010-07-20 23:55:01 +0000418 if (const MDNode *MD = dyn_cast<MDNode>(V)) {
419 if (!MD->isFunctionLocal())
420 return new SlotTracker(MD->getFunction());
421
Dale Johannesen7b1a7ed2010-01-13 00:00:24 +0000422 return new SlotTracker((Function *)0);
Dan Gohmana2489d12010-07-20 23:55:01 +0000423 }
Dale Johannesen7b1a7ed2010-01-13 00:00:24 +0000424
Chris Lattner3eee99c2008-08-19 04:36:02 +0000425 return 0;
426}
427
428#if 0
David Greenec7f9b122010-01-05 01:29:26 +0000429#define ST_DEBUG(X) dbgs() << X
Chris Lattner3eee99c2008-08-19 04:36:02 +0000430#else
Chris Lattner604e3512008-08-19 04:47:09 +0000431#define ST_DEBUG(X)
Chris Lattner3eee99c2008-08-19 04:36:02 +0000432#endif
433
434// Module level constructor. Causes the contents of the Module (sans functions)
435// to be added to the slot table.
436SlotTracker::SlotTracker(const Module *M)
Andrew Trickec4b6e72011-09-30 19:48:58 +0000437 : TheModule(M), TheFunction(0), FunctionProcessed(false),
Bill Wendling829b4782013-02-11 08:43:33 +0000438 mNext(0), fNext(0), mdnNext(0), asNext(0) {
Chris Lattner3eee99c2008-08-19 04:36:02 +0000439}
440
441// Function level constructor. Causes the contents of the Module and the one
442// function provided to be added to the slot table.
443SlotTracker::SlotTracker(const Function *F)
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000444 : TheModule(F ? F->getParent() : 0), TheFunction(F), FunctionProcessed(false),
Bill Wendling829b4782013-02-11 08:43:33 +0000445 mNext(0), fNext(0), mdnNext(0), asNext(0) {
Chris Lattner3eee99c2008-08-19 04:36:02 +0000446}
447
448inline void SlotTracker::initialize() {
449 if (TheModule) {
450 processModule();
451 TheModule = 0; ///< Prevent re-processing next time we're called.
452 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000453
Chris Lattner3eee99c2008-08-19 04:36:02 +0000454 if (TheFunction && !FunctionProcessed)
455 processFunction();
456}
457
458// Iterate through all the global variables, functions, and global
459// variable initializers and create slots for them.
460void SlotTracker::processModule() {
Chris Lattner604e3512008-08-19 04:47:09 +0000461 ST_DEBUG("begin processModule!\n");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000462
Chris Lattner3eee99c2008-08-19 04:36:02 +0000463 // Add all of the unnamed global variables to the value table.
464 for (Module::const_global_iterator I = TheModule->global_begin(),
Devang Patel983c6b12009-07-08 21:44:25 +0000465 E = TheModule->global_end(); I != E; ++I) {
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000466 if (!I->hasName())
Chris Lattner3eee99c2008-08-19 04:36:02 +0000467 CreateModuleSlot(I);
Devang Patel983c6b12009-07-08 21:44:25 +0000468 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000469
Devang Patel23e68302009-07-29 22:04:47 +0000470 // Add metadata used by named metadata.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000471 for (Module::const_named_metadata_iterator
Devang Patel23e68302009-07-29 22:04:47 +0000472 I = TheModule->named_metadata_begin(),
473 E = TheModule->named_metadata_end(); I != E; ++I) {
474 const NamedMDNode *NMD = I;
Dan Gohman093cb792010-07-21 18:54:18 +0000475 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
476 CreateMetadataSlot(NMD->getOperand(i));
Devang Patel23e68302009-07-29 22:04:47 +0000477 }
478
Chris Lattner3eee99c2008-08-19 04:36:02 +0000479 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
Bill Wendling829b4782013-02-11 08:43:33 +0000480 I != E; ++I) {
Chris Lattner3eee99c2008-08-19 04:36:02 +0000481 if (!I->hasName())
Bill Wendling829b4782013-02-11 08:43:33 +0000482 // Add all the unnamed functions to the table.
Chris Lattner3eee99c2008-08-19 04:36:02 +0000483 CreateModuleSlot(I);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000484
Bill Wendling829b4782013-02-11 08:43:33 +0000485 // Add all the function attributes to the table.
Bill Wendling90bc19c2013-02-20 07:21:42 +0000486 // FIXME: Add attributes of other objects?
Bill Wendling829b4782013-02-11 08:43:33 +0000487 AttributeSet FnAttrs = I->getAttributes().getFnAttributes();
488 if (FnAttrs.hasAttributes(AttributeSet::FunctionIndex))
489 CreateAttributeSetSlot(FnAttrs);
490 }
491
Chris Lattner604e3512008-08-19 04:47:09 +0000492 ST_DEBUG("end processModule!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000493}
494
Chris Lattner3eee99c2008-08-19 04:36:02 +0000495// Process the arguments, basic blocks, and instructions of a function.
496void SlotTracker::processFunction() {
Chris Lattner604e3512008-08-19 04:47:09 +0000497 ST_DEBUG("begin processFunction!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000498 fNext = 0;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000499
Chris Lattner3eee99c2008-08-19 04:36:02 +0000500 // Add all the function arguments with no names.
501 for(Function::const_arg_iterator AI = TheFunction->arg_begin(),
502 AE = TheFunction->arg_end(); AI != AE; ++AI)
503 if (!AI->hasName())
504 CreateFunctionSlot(AI);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000505
Chris Lattner604e3512008-08-19 04:47:09 +0000506 ST_DEBUG("Inserting Instructions:\n");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000507
Chris Lattnerbddea6a2009-12-31 02:13:35 +0000508 SmallVector<std::pair<unsigned, MDNode*>, 4> MDForInst;
Devang Pateldec23fd2009-09-16 20:21:17 +0000509
Chris Lattner3eee99c2008-08-19 04:36:02 +0000510 // Add all of the basic blocks and instructions with no names.
511 for (Function::const_iterator BB = TheFunction->begin(),
512 E = TheFunction->end(); BB != E; ++BB) {
513 if (!BB->hasName())
514 CreateFunctionSlot(BB);
Andrew Trickec4b6e72011-09-30 19:48:58 +0000515
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000516 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E;
Devang Patel983c6b12009-07-08 21:44:25 +0000517 ++I) {
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000518 if (!I->getType()->isVoidTy() && !I->hasName())
Chris Lattner3eee99c2008-08-19 04:36:02 +0000519 CreateFunctionSlot(I);
Andrew Trickec4b6e72011-09-30 19:48:58 +0000520
Chris Lattner58aff8f2010-05-10 20:53:17 +0000521 // Intrinsics can directly use metadata. We allow direct calls to any
522 // llvm.foo function here, because the target may not be linked into the
523 // optimizer.
524 if (const CallInst *CI = dyn_cast<CallInst>(I)) {
525 if (Function *F = CI->getCalledFunction())
526 if (F->getName().startswith("llvm."))
527 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
528 if (MDNode *N = dyn_cast_or_null<MDNode>(I->getOperand(i)))
529 CreateMetadataSlot(N);
Bill Wendling6da216f2013-02-20 00:04:41 +0000530
Bill Wendlinga0323742013-02-22 09:09:42 +0000531 // Add all the call attributes to the table.
532 AttributeSet Attrs = CI->getAttributes().getFnAttributes();
533 if (Attrs.hasAttributes(AttributeSet::FunctionIndex))
534 CreateAttributeSetSlot(Attrs);
535 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(I)) {
536 // Add all the call attributes to the table.
537 AttributeSet Attrs = II->getAttributes().getFnAttributes();
538 if (Attrs.hasAttributes(AttributeSet::FunctionIndex))
539 CreateAttributeSetSlot(Attrs);
Chris Lattner58aff8f2010-05-10 20:53:17 +0000540 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000541
Devang Pateldec23fd2009-09-16 20:21:17 +0000542 // Process metadata attached with this instruction.
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000543 I->getAllMetadata(MDForInst);
544 for (unsigned i = 0, e = MDForInst.size(); i != e; ++i)
545 CreateMetadataSlot(MDForInst[i].second);
Chris Lattnerbddea6a2009-12-31 02:13:35 +0000546 MDForInst.clear();
Devang Patel983c6b12009-07-08 21:44:25 +0000547 }
Chris Lattner3eee99c2008-08-19 04:36:02 +0000548 }
Devang Pateldec23fd2009-09-16 20:21:17 +0000549
Chris Lattner3eee99c2008-08-19 04:36:02 +0000550 FunctionProcessed = true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000551
Chris Lattner604e3512008-08-19 04:47:09 +0000552 ST_DEBUG("end processFunction!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000553}
554
555/// Clean up after incorporating a function. This is the only way to get out of
556/// the function incorporation state that affects get*Slot/Create*Slot. Function
557/// incorporation state is indicated by TheFunction != 0.
558void SlotTracker::purgeFunction() {
Chris Lattner604e3512008-08-19 04:47:09 +0000559 ST_DEBUG("begin purgeFunction!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000560 fMap.clear(); // Simply discard the function level map
561 TheFunction = 0;
562 FunctionProcessed = false;
Chris Lattner604e3512008-08-19 04:47:09 +0000563 ST_DEBUG("end purgeFunction!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000564}
565
566/// getGlobalSlot - Get the slot number of a global value.
567int SlotTracker::getGlobalSlot(const GlobalValue *V) {
568 // Check for uninitialized state and do lazy initialization.
569 initialize();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000570
Jay Foaddbb221d2011-07-11 07:28:49 +0000571 // Find the value in the module map
Chris Lattner3eee99c2008-08-19 04:36:02 +0000572 ValueMap::iterator MI = mMap.find(V);
Dan Gohman1dd27572008-10-01 19:58:59 +0000573 return MI == mMap.end() ? -1 : (int)MI->second;
Chris Lattner3eee99c2008-08-19 04:36:02 +0000574}
575
Chris Lattnercf4a76e2009-12-31 02:20:11 +0000576/// getMetadataSlot - Get the slot number of a MDNode.
Devang Patel983c6b12009-07-08 21:44:25 +0000577int SlotTracker::getMetadataSlot(const MDNode *N) {
578 // Check for uninitialized state and do lazy initialization.
579 initialize();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000580
Jay Foaddbb221d2011-07-11 07:28:49 +0000581 // Find the MDNode in the module map
Chris Lattnercf4a76e2009-12-31 02:20:11 +0000582 mdn_iterator MI = mdnMap.find(N);
Devang Patel983c6b12009-07-08 21:44:25 +0000583 return MI == mdnMap.end() ? -1 : (int)MI->second;
584}
585
Chris Lattner3eee99c2008-08-19 04:36:02 +0000586
587/// getLocalSlot - Get the slot number for a value that is local to a function.
588int SlotTracker::getLocalSlot(const Value *V) {
589 assert(!isa<Constant>(V) && "Can't get a constant or global slot with this!");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000590
Chris Lattner3eee99c2008-08-19 04:36:02 +0000591 // Check for uninitialized state and do lazy initialization.
592 initialize();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000593
Chris Lattner3eee99c2008-08-19 04:36:02 +0000594 ValueMap::iterator FI = fMap.find(V);
Dan Gohman1dd27572008-10-01 19:58:59 +0000595 return FI == fMap.end() ? -1 : (int)FI->second;
Chris Lattner3eee99c2008-08-19 04:36:02 +0000596}
597
Bill Wendling829b4782013-02-11 08:43:33 +0000598int SlotTracker::getAttributeGroupSlot(AttributeSet AS) {
599 // Check for uninitialized state and do lazy initialization.
600 initialize();
601
602 // Find the AttributeSet in the module map.
603 as_iterator AI = asMap.find(AS);
604 return AI == asMap.end() ? -1 : (int)AI->second;
605}
Chris Lattner3eee99c2008-08-19 04:36:02 +0000606
607/// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
608void SlotTracker::CreateModuleSlot(const GlobalValue *V) {
609 assert(V && "Can't insert a null Value into SlotTracker!");
Chris Lattner031560c2009-12-29 07:25:48 +0000610 assert(!V->getType()->isVoidTy() && "Doesn't need a slot!");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000611 assert(!V->hasName() && "Doesn't need a slot!");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000612
Chris Lattner3eee99c2008-08-19 04:36:02 +0000613 unsigned DestSlot = mNext++;
614 mMap[V] = DestSlot;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000615
Chris Lattner604e3512008-08-19 04:47:09 +0000616 ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" <<
Chris Lattner3eee99c2008-08-19 04:36:02 +0000617 DestSlot << " [");
618 // G = Global, F = Function, A = Alias, o = other
Chris Lattner604e3512008-08-19 04:47:09 +0000619 ST_DEBUG((isa<GlobalVariable>(V) ? 'G' :
Chris Lattner3eee99c2008-08-19 04:36:02 +0000620 (isa<Function>(V) ? 'F' :
621 (isa<GlobalAlias>(V) ? 'A' : 'o'))) << "]\n");
622}
623
Chris Lattner3eee99c2008-08-19 04:36:02 +0000624/// CreateSlot - Create a new slot for the specified value if it has no name.
625void SlotTracker::CreateFunctionSlot(const Value *V) {
Chris Lattner031560c2009-12-29 07:25:48 +0000626 assert(!V->getType()->isVoidTy() && !V->hasName() && "Doesn't need a slot!");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000627
Chris Lattner3eee99c2008-08-19 04:36:02 +0000628 unsigned DestSlot = fNext++;
629 fMap[V] = DestSlot;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000630
Chris Lattner3eee99c2008-08-19 04:36:02 +0000631 // G = Global, F = Function, o = other
Chris Lattner604e3512008-08-19 04:47:09 +0000632 ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" <<
Chris Lattner3eee99c2008-08-19 04:36:02 +0000633 DestSlot << " [o]\n");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000634}
Chris Lattner3eee99c2008-08-19 04:36:02 +0000635
Devang Patel983c6b12009-07-08 21:44:25 +0000636/// CreateModuleSlot - Insert the specified MDNode* into the slot table.
637void SlotTracker::CreateMetadataSlot(const MDNode *N) {
638 assert(N && "Can't insert a null Value into SlotTracker!");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000639
Chris Lattner0d50bdd2009-12-31 02:27:30 +0000640 // Don't insert if N is a function-local metadata, these are always printed
641 // inline.
Dan Gohmana2489d12010-07-20 23:55:01 +0000642 if (!N->isFunctionLocal()) {
643 mdn_iterator I = mdnMap.find(N);
644 if (I != mdnMap.end())
645 return;
Victor Hernandez4d633542009-12-04 20:07:10 +0000646
Dan Gohmana2489d12010-07-20 23:55:01 +0000647 unsigned DestSlot = mdnNext++;
648 mdnMap[N] = DestSlot;
649 }
Devang Patel983c6b12009-07-08 21:44:25 +0000650
Chris Lattner0d50bdd2009-12-31 02:27:30 +0000651 // Recursively add any MDNodes referenced by operands.
652 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
653 if (const MDNode *Op = dyn_cast_or_null<MDNode>(N->getOperand(i)))
654 CreateMetadataSlot(Op);
Devang Patel983c6b12009-07-08 21:44:25 +0000655}
Chris Lattner3eee99c2008-08-19 04:36:02 +0000656
Bill Wendling829b4782013-02-11 08:43:33 +0000657void SlotTracker::CreateAttributeSetSlot(AttributeSet AS) {
658 assert(AS.hasAttributes(AttributeSet::FunctionIndex) &&
659 "Doesn't need a slot!");
660
661 as_iterator I = asMap.find(AS);
662 if (I != asMap.end())
663 return;
664
665 unsigned DestSlot = asNext++;
666 asMap[AS] = DestSlot;
667}
668
Chris Lattner3eee99c2008-08-19 04:36:02 +0000669//===----------------------------------------------------------------------===//
670// AsmWriter Implementation
671//===----------------------------------------------------------------------===//
Chris Lattner7f8845a2002-07-23 18:07:49 +0000672
Dan Gohman12dad632009-08-12 20:56:03 +0000673static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
Dan Gohman8061d9e2009-08-13 15:27:57 +0000674 TypePrinting *TypePrinter,
Dan Gohmana2489d12010-07-20 23:55:01 +0000675 SlotTracker *Machine,
676 const Module *Context);
Reid Spencer58d30f22004-07-04 11:50:43 +0000677
Chris Lattner033935d2008-08-17 04:40:13 +0000678
Chris Lattnerb86620e2001-10-29 16:37:48 +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.
Dale Johannesen028084e2007-09-12 03:30:33 +0000814 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);
1051 if (V == 0)
1052 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
Chris Lattnerd84bb632002-04-16 21:36:08 +00001066
Misha Brukmanc566ca362004-03-02 00:22:19 +00001067/// WriteAsOperand - Write the name of the specified value out to the specified
1068/// ostream. This can be useful when you just want to print int %reg126, not
1069/// the whole instruction that generated it.
1070///
Dan Gohman12dad632009-08-12 20:56:03 +00001071static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
Dan Gohman8061d9e2009-08-13 15:27:57 +00001072 TypePrinting *TypePrinter,
Dan Gohmana2489d12010-07-20 23:55:01 +00001073 SlotTracker *Machine,
1074 const Module *Context) {
Chris Lattner033935d2008-08-17 04:40:13 +00001075 if (V->hasName()) {
1076 PrintLLVMName(Out, V);
1077 return;
1078 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001079
Chris Lattner033935d2008-08-17 04:40:13 +00001080 const Constant *CV = dyn_cast<Constant>(V);
1081 if (CV && !isa<GlobalValue>(CV)) {
Dan Gohman8061d9e2009-08-13 15:27:57 +00001082 assert(TypePrinter && "Constants require TypePrinting!");
Dan Gohmana2489d12010-07-20 23:55:01 +00001083 WriteConstantInternal(Out, CV, *TypePrinter, Machine, Context);
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001084 return;
1085 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001086
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001087 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
Chris Lattner033935d2008-08-17 04:40:13 +00001088 Out << "asm ";
1089 if (IA->hasSideEffects())
1090 Out << "sideeffect ";
Dale Johannesen1cfb9582009-10-21 23:28:00 +00001091 if (IA->isAlignStack())
1092 Out << "alignstack ";
Chad Rosierd8c76102012-09-05 19:00:49 +00001093 // We don't emit the AD_ATT dialect as it's the assumed default.
1094 if (IA->getDialect() == InlineAsm::AD_Intel)
1095 Out << "inteldialect ";
Chris Lattner033935d2008-08-17 04:40:13 +00001096 Out << '"';
1097 PrintEscapedString(IA->getAsmString(), Out);
1098 Out << "\", \"";
1099 PrintEscapedString(IA->getConstraintString(), Out);
1100 Out << '"';
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001101 return;
1102 }
Devang Patel7428d8a2009-07-22 17:43:22 +00001103
Devang Patele059ba6e2009-07-23 01:07:34 +00001104 if (const MDNode *N = dyn_cast<MDNode>(V)) {
Victor Hernandez0471abd2009-12-18 20:09:14 +00001105 if (N->isFunctionLocal()) {
Victor Hernandezb7176a12009-12-04 01:35:02 +00001106 // Print metadata inline, not via slot reference number.
Dan Gohmana2489d12010-07-20 23:55:01 +00001107 WriteMDNodeBodyInternal(Out, N, TypePrinter, Machine, Context);
Victor Hernandezb7176a12009-12-04 01:35:02 +00001108 return;
1109 }
Andrew Trickec4b6e72011-09-30 19:48:58 +00001110
Dan Gohmana2489d12010-07-20 23:55:01 +00001111 if (!Machine) {
1112 if (N->isFunctionLocal())
1113 Machine = new SlotTracker(N->getFunction());
1114 else
1115 Machine = new SlotTracker(Context);
1116 }
Dan Gohman0a54da22010-09-09 20:53:58 +00001117 int Slot = Machine->getMetadataSlot(N);
1118 if (Slot == -1)
1119 Out << "<badref>";
1120 else
1121 Out << '!' << Slot;
Devang Patele059ba6e2009-07-23 01:07:34 +00001122 return;
1123 }
1124
Devang Patel7428d8a2009-07-22 17:43:22 +00001125 if (const MDString *MDS = dyn_cast<MDString>(V)) {
Devang Patel7428d8a2009-07-22 17:43:22 +00001126 Out << "!\"";
Daniel Dunbare03eecb2009-07-25 23:55:21 +00001127 PrintEscapedString(MDS->getString(), Out);
Devang Patel7428d8a2009-07-22 17:43:22 +00001128 Out << '"';
1129 return;
1130 }
1131
Evan Cheng00e87d12009-11-16 07:10:36 +00001132 if (V->getValueID() == Value::PseudoSourceValueVal ||
1133 V->getValueID() == Value::FixedStackPseudoSourceValueVal) {
Dan Gohmanc0353bf2009-09-23 01:33:16 +00001134 V->print(Out);
1135 return;
1136 }
1137
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001138 char Prefix = '%';
1139 int Slot;
Chris Lattner5b82a0a2011-08-03 06:15:41 +00001140 // If we have a SlotTracker, use it.
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001141 if (Machine) {
1142 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1143 Slot = Machine->getGlobalSlot(GV);
1144 Prefix = '@';
1145 } else {
1146 Slot = Machine->getLocalSlot(V);
Andrew Trickec4b6e72011-09-30 19:48:58 +00001147
Chris Lattner5b82a0a2011-08-03 06:15:41 +00001148 // If the local value didn't succeed, then we may be referring to a value
1149 // from a different function. Translate it, as this can happen when using
1150 // address of blocks.
1151 if (Slot == -1)
1152 if ((Machine = createSlotTracker(V))) {
1153 Slot = Machine->getLocalSlot(V);
1154 delete Machine;
1155 }
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001156 }
Chris Lattner5b82a0a2011-08-03 06:15:41 +00001157 } else if ((Machine = createSlotTracker(V))) {
1158 // Otherwise, create one to get the # and then destroy it.
1159 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1160 Slot = Machine->getGlobalSlot(GV);
1161 Prefix = '@';
Chris Lattnera2d810d2006-01-25 22:26:05 +00001162 } else {
Chris Lattner5b82a0a2011-08-03 06:15:41 +00001163 Slot = Machine->getLocalSlot(V);
Chris Lattnerd84bb632002-04-16 21:36:08 +00001164 }
Chris Lattner5b82a0a2011-08-03 06:15:41 +00001165 delete Machine;
1166 Machine = 0;
1167 } else {
1168 Slot = -1;
Chris Lattnerd84bb632002-04-16 21:36:08 +00001169 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001170
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001171 if (Slot != -1)
1172 Out << Prefix << Slot;
1173 else
1174 Out << "<badref>";
Chris Lattnerd84bb632002-04-16 21:36:08 +00001175}
1176
Daniel Maleaded9f932013-05-08 20:38:31 +00001177void WriteAsOperand(raw_ostream &Out, const Value *V,
1178 bool PrintType, const Module *Context) {
Dan Gohman8061d9e2009-08-13 15:27:57 +00001179
1180 // Fast path: Don't construct and populate a TypePrinting object if we
1181 // won't be needing any types printed.
Dan Gohman08097122009-08-13 23:07:11 +00001182 if (!PrintType &&
Dan Gohmana2489d12010-07-20 23:55:01 +00001183 ((!isa<Constant>(V) && !isa<MDNode>(V)) ||
1184 V->hasName() || isa<GlobalValue>(V))) {
1185 WriteAsOperandInternal(Out, V, 0, 0, Context);
Dan Gohman8061d9e2009-08-13 15:27:57 +00001186 return;
1187 }
1188
Chris Lattner5a9f63e2002-07-10 16:48:17 +00001189 if (Context == 0) Context = getModuleFromVal(V);
Chris Lattnerb86620e2001-10-29 16:37:48 +00001190
Chris Lattner92c5c122009-02-28 23:20:19 +00001191 TypePrinting TypePrinter;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001192 if (Context)
1193 TypePrinter.incorporateTypes(*Context);
Dan Gohman81313fd2008-09-14 17:21:12 +00001194 if (PrintType) {
Chris Lattnere101c442009-02-28 21:26:53 +00001195 TypePrinter.print(V->getType(), Out);
Dan Gohman81313fd2008-09-14 17:21:12 +00001196 Out << ' ';
1197 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001198
Dan Gohmana2489d12010-07-20 23:55:01 +00001199 WriteAsOperandInternal(Out, V, &TypePrinter, 0, Context);
Chris Lattner5e5abe32001-07-20 19:15:21 +00001200}
1201
Daniel Maleaded9f932013-05-08 20:38:31 +00001202void AssemblyWriter::init() {
1203 if (TheModule)
1204 TypePrinter.incorporateTypes(*TheModule);
1205}
Chris Lattner2e9fee42001-07-12 23:35:26 +00001206
Andrew Trickec4b6e72011-09-30 19:48:58 +00001207
Daniel Maleaded9f932013-05-08 20:38:31 +00001208AssemblyWriter::AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac,
1209 const Module *M,
1210 AssemblyAnnotationWriter *AAW)
1211 : Out(o), TheModule(M), Machine(Mac), AnnotationWriter(AAW) {
1212 init();
1213}
Chris Lattner2f7c9632001-06-06 20:29:01 +00001214
Daniel Maleaded9f932013-05-08 20:38:31 +00001215AssemblyWriter::AssemblyWriter(formatted_raw_ostream &o, const Module *M,
1216 AssemblyAnnotationWriter *AAW)
1217 : Out(o), TheModule(M), ModuleSlotTracker(createSlotTracker(M)),
1218 Machine(*ModuleSlotTracker), AnnotationWriter(AAW) {
1219 init();
1220}
Andrew Trickec4b6e72011-09-30 19:48:58 +00001221
Daniel Maleaded9f932013-05-08 20:38:31 +00001222AssemblyWriter::~AssemblyWriter() { }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001223
Chris Lattner78e2e8b2006-12-06 06:24:27 +00001224void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType) {
1225 if (Operand == 0) {
Chris Lattner08f7d0c2005-02-24 16:58:29 +00001226 Out << "<null operand!>";
Chris Lattnerb419a1e2009-12-31 02:33:14 +00001227 return;
Chris Lattner08f7d0c2005-02-24 16:58:29 +00001228 }
Chris Lattnerb419a1e2009-12-31 02:33:14 +00001229 if (PrintType) {
1230 TypePrinter.print(Operand->getType(), Out);
1231 Out << ' ';
1232 }
Dan Gohmana2489d12010-07-20 23:55:01 +00001233 WriteAsOperandInternal(Out, Operand, &TypePrinter, &Machine, TheModule);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001234}
1235
Eli Friedmanfee02c62011-07-25 23:16:38 +00001236void AssemblyWriter::writeAtomic(AtomicOrdering Ordering,
1237 SynchronizationScope SynchScope) {
1238 if (Ordering == NotAtomic)
1239 return;
1240
1241 switch (SynchScope) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001242 case SingleThread: Out << " singlethread"; break;
1243 case CrossThread: break;
1244 }
1245
1246 switch (Ordering) {
1247 default: Out << " <bad ordering " << int(Ordering) << ">"; break;
1248 case Unordered: Out << " unordered"; break;
1249 case Monotonic: Out << " monotonic"; break;
1250 case Acquire: Out << " acquire"; break;
1251 case Release: Out << " release"; break;
1252 case AcquireRelease: Out << " acq_rel"; break;
1253 case SequentiallyConsistent: Out << " seq_cst"; break;
1254 }
1255}
1256
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001257void AssemblyWriter::writeParamOperand(const Value *Operand,
Bill Wendling749a43d2012-12-30 13:50:49 +00001258 AttributeSet Attrs, unsigned Idx) {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00001259 if (Operand == 0) {
1260 Out << "<null operand!>";
Chris Lattnerb419a1e2009-12-31 02:33:14 +00001261 return;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00001262 }
Chris Lattnerb419a1e2009-12-31 02:33:14 +00001263
1264 // Print the type
1265 TypePrinter.print(Operand->getType(), Out);
1266 // Print parameter attributes list
Bill Wendling749a43d2012-12-30 13:50:49 +00001267 if (Attrs.hasAttributes(Idx))
1268 Out << ' ' << Attrs.getAsString(Idx);
Chris Lattnerb419a1e2009-12-31 02:33:14 +00001269 Out << ' ';
1270 // Print the operand
Dan Gohmana2489d12010-07-20 23:55:01 +00001271 WriteAsOperandInternal(Out, Operand, &TypePrinter, &Machine, TheModule);
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00001272}
Chris Lattner2f7c9632001-06-06 20:29:01 +00001273
Chris Lattner7bfee412001-10-29 16:05:51 +00001274void AssemblyWriter::printModule(const Module *M) {
Bill Wendling829b4782013-02-11 08:43:33 +00001275 Machine.initialize();
1276
Chris Lattner4d8689e2005-03-02 23:12:40 +00001277 if (!M->getModuleIdentifier().empty() &&
Misha Brukmanb1c93172005-04-21 23:48:37 +00001278 // Don't print the ID if it will start a new line (which would
Chris Lattner4d8689e2005-03-02 23:12:40 +00001279 // require a comment char before it).
1280 M->getModuleIdentifier().find('\n') == std::string::npos)
1281 Out << "; ModuleID = '" << M->getModuleIdentifier() << "'\n";
1282
Owen Andersone2237542006-10-18 02:21:12 +00001283 if (!M->getDataLayout().empty())
Chris Lattner04897162006-10-22 06:06:56 +00001284 Out << "target datalayout = \"" << M->getDataLayout() << "\"\n";
Reid Spencer48f98c82004-07-25 21:44:54 +00001285 if (!M->getTargetTriple().empty())
Reid Spencerffec7df2004-07-25 21:29:43 +00001286 Out << "target triple = \"" << M->getTargetTriple() << "\"\n";
Misha Brukmanb1c93172005-04-21 23:48:37 +00001287
Chris Lattnereef2fe72006-01-24 04:13:11 +00001288 if (!M->getModuleInlineAsm().empty()) {
Chris Lattnerefaf35d2006-01-24 00:45:30 +00001289 // Split the string into lines, to make it easier to read the .ll file.
Chris Lattnereef2fe72006-01-24 04:13:11 +00001290 std::string Asm = M->getModuleInlineAsm();
Chris Lattnerefaf35d2006-01-24 00:45:30 +00001291 size_t CurPos = 0;
1292 size_t NewLine = Asm.find_first_of('\n', CurPos);
Dan Gohman69273e62009-08-12 23:54:22 +00001293 Out << '\n';
Chris Lattnerefaf35d2006-01-24 00:45:30 +00001294 while (NewLine != std::string::npos) {
1295 // We found a newline, print the portion of the asm string from the
1296 // last newline up to this newline.
1297 Out << "module asm \"";
1298 PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.begin()+NewLine),
1299 Out);
1300 Out << "\"\n";
1301 CurPos = NewLine+1;
1302 NewLine = Asm.find_first_of('\n', CurPos);
1303 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +00001304 std::string rest(Asm.begin()+CurPos, Asm.end());
1305 if (!rest.empty()) {
1306 Out << "module asm \"";
1307 PrintEscapedString(rest, Out);
1308 Out << "\"\n";
1309 }
Chris Lattner6ed87bd2006-01-23 23:03:36 +00001310 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001311
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001312 printTypeIdentities();
Misha Brukmanb1c93172005-04-21 23:48:37 +00001313
Dan Gohman69273e62009-08-12 23:54:22 +00001314 // Output all globals.
1315 if (!M->global_empty()) Out << '\n';
Chris Lattner54932b02006-12-06 04:41:52 +00001316 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
Duncan Sands66fc0e62012-09-12 09:55:51 +00001317 I != E; ++I) {
1318 printGlobal(I); Out << '\n';
1319 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001320
Chris Lattnerd2747052007-04-26 02:24:10 +00001321 // Output all aliases.
1322 if (!M->alias_empty()) Out << "\n";
1323 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
1324 I != E; ++I)
1325 printAlias(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +00001326
Chris Lattner2cdd49d2004-09-14 05:06:58 +00001327 // Output all of the functions.
Chris Lattner113f4f42002-06-25 16:13:24 +00001328 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
1329 printFunction(I);
Devang Patel983c6b12009-07-08 21:44:25 +00001330
Bill Wendling829b4782013-02-11 08:43:33 +00001331 // Output all attribute groups.
Bill Wendling04945972013-04-29 23:48:06 +00001332 if (!Machine.as_empty()) {
Bill Wendling829b4782013-02-11 08:43:33 +00001333 Out << '\n';
1334 writeAllAttributeGroups();
1335 }
1336
Devang Patel23e68302009-07-29 22:04:47 +00001337 // Output named metadata.
Dan Gohman69273e62009-08-12 23:54:22 +00001338 if (!M->named_metadata_empty()) Out << '\n';
Andrew Trickec4b6e72011-09-30 19:48:58 +00001339
Devang Patel23e68302009-07-29 22:04:47 +00001340 for (Module::const_named_metadata_iterator I = M->named_metadata_begin(),
Chris Lattnerbddea6a2009-12-31 02:13:35 +00001341 E = M->named_metadata_end(); I != E; ++I)
Chris Lattner2c48c642009-12-31 01:54:05 +00001342 printNamedMDNode(I);
Devang Patel23e68302009-07-29 22:04:47 +00001343
1344 // Output metadata.
Chris Lattnercf4a76e2009-12-31 02:20:11 +00001345 if (!Machine.mdn_empty()) {
Chris Lattnerbddea6a2009-12-31 02:13:35 +00001346 Out << '\n';
1347 writeAllMDNodes();
1348 }
Chris Lattnerfee714f2001-09-07 16:36:04 +00001349}
1350
Chris Lattner2c48c642009-12-31 01:54:05 +00001351void AssemblyWriter::printNamedMDNode(const NamedMDNode *NMD) {
Nick Lewycky5bcbd732011-06-15 06:37:58 +00001352 Out << '!';
1353 StringRef Name = NMD->getName();
1354 if (Name.empty()) {
1355 Out << "<empty name> ";
1356 } else {
Guy Benyei83c74e92013-02-12 21:21:59 +00001357 if (isalpha(static_cast<unsigned char>(Name[0])) ||
1358 Name[0] == '-' || Name[0] == '$' ||
Nick Lewycky5bcbd732011-06-15 06:37:58 +00001359 Name[0] == '.' || Name[0] == '_')
1360 Out << Name[0];
1361 else
1362 Out << '\\' << hexdigit(Name[0] >> 4) << hexdigit(Name[0] & 0x0F);
1363 for (unsigned i = 1, e = Name.size(); i != e; ++i) {
1364 unsigned char C = Name[i];
Guy Benyei83c74e92013-02-12 21:21:59 +00001365 if (isalnum(static_cast<unsigned char>(C)) || C == '-' || C == '$' ||
1366 C == '.' || C == '_')
Nick Lewycky5bcbd732011-06-15 06:37:58 +00001367 Out << C;
1368 else
1369 Out << '\\' << hexdigit(C >> 4) << hexdigit(C & 0x0F);
1370 }
1371 }
1372 Out << " = !{";
Chris Lattner2c48c642009-12-31 01:54:05 +00001373 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1374 if (i) Out << ", ";
Dan Gohman0a54da22010-09-09 20:53:58 +00001375 int Slot = Machine.getMetadataSlot(NMD->getOperand(i));
1376 if (Slot == -1)
1377 Out << "<badref>";
1378 else
1379 Out << '!' << Slot;
Chris Lattner2c48c642009-12-31 01:54:05 +00001380 }
1381 Out << "}\n";
1382}
1383
1384
Dan Gohmane2745262009-08-12 17:23:50 +00001385static void PrintLinkage(GlobalValue::LinkageTypes LT,
1386 formatted_raw_ostream &Out) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001387 switch (LT) {
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001388 case GlobalValue::ExternalLinkage: break;
1389 case GlobalValue::PrivateLinkage: Out << "private "; break;
1390 case GlobalValue::LinkerPrivateLinkage: Out << "linker_private "; break;
Bill Wendling03bcd6e2010-07-01 21:55:59 +00001391 case GlobalValue::LinkerPrivateWeakLinkage:
1392 Out << "linker_private_weak ";
1393 break;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001394 case GlobalValue::InternalLinkage: Out << "internal "; break;
1395 case GlobalValue::LinkOnceAnyLinkage: Out << "linkonce "; break;
1396 case GlobalValue::LinkOnceODRLinkage: Out << "linkonce_odr "; break;
Bill Wendling34bc34e2012-08-17 18:33:14 +00001397 case GlobalValue::LinkOnceODRAutoHideLinkage:
1398 Out << "linkonce_odr_auto_hide ";
1399 break;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001400 case GlobalValue::WeakAnyLinkage: Out << "weak "; break;
1401 case GlobalValue::WeakODRLinkage: Out << "weak_odr "; break;
1402 case GlobalValue::CommonLinkage: Out << "common "; break;
1403 case GlobalValue::AppendingLinkage: Out << "appending "; break;
1404 case GlobalValue::DLLImportLinkage: Out << "dllimport "; break;
1405 case GlobalValue::DLLExportLinkage: Out << "dllexport "; break;
1406 case GlobalValue::ExternalWeakLinkage: Out << "extern_weak "; break;
Chris Lattner184f1be2009-04-13 05:44:34 +00001407 case GlobalValue::AvailableExternallyLinkage:
1408 Out << "available_externally ";
1409 break;
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001410 }
1411}
Duncan Sands12da8ce2009-03-07 15:45:40 +00001412
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001413
1414static void PrintVisibility(GlobalValue::VisibilityTypes Vis,
Dan Gohmane2745262009-08-12 17:23:50 +00001415 formatted_raw_ostream &Out) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001416 switch (Vis) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001417 case GlobalValue::DefaultVisibility: break;
1418 case GlobalValue::HiddenVisibility: Out << "hidden "; break;
1419 case GlobalValue::ProtectedVisibility: Out << "protected "; break;
1420 }
1421}
1422
Hans Wennborgcbe34b42012-06-23 11:37:03 +00001423static void PrintThreadLocalModel(GlobalVariable::ThreadLocalMode TLM,
1424 formatted_raw_ostream &Out) {
1425 switch (TLM) {
1426 case GlobalVariable::NotThreadLocal:
1427 break;
1428 case GlobalVariable::GeneralDynamicTLSModel:
1429 Out << "thread_local ";
1430 break;
1431 case GlobalVariable::LocalDynamicTLSModel:
1432 Out << "thread_local(localdynamic) ";
1433 break;
1434 case GlobalVariable::InitialExecTLSModel:
1435 Out << "thread_local(initialexec) ";
1436 break;
1437 case GlobalVariable::LocalExecTLSModel:
1438 Out << "thread_local(localexec) ";
1439 break;
1440 }
1441}
1442
Chris Lattner7bfee412001-10-29 16:05:51 +00001443void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Dan Gohman5ded1422010-01-29 23:12:36 +00001444 if (GV->isMaterializable())
1445 Out << "; Materializable\n";
1446
Dan Gohmana2489d12010-07-20 23:55:01 +00001447 WriteAsOperandInternal(Out, GV, &TypePrinter, &Machine, GV->getParent());
Dan Gohman466876b2009-08-12 23:32:33 +00001448 Out << " = ";
Chris Lattner37798642001-09-18 04:01:05 +00001449
Chris Lattner1508d3f2008-08-19 05:16:28 +00001450 if (!GV->hasInitializer() && GV->hasExternalLinkage())
1451 Out << "external ";
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001452
Chris Lattner1508d3f2008-08-19 05:16:28 +00001453 PrintLinkage(GV->getLinkage(), Out);
1454 PrintVisibility(GV->getVisibility(), Out);
Hans Wennborgcbe34b42012-06-23 11:37:03 +00001455 PrintThreadLocalModel(GV->getThreadLocalMode(), Out);
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +00001456
Chris Lattnerac161bf2009-01-02 07:01:27 +00001457 if (unsigned AddressSpace = GV->getType()->getAddressSpace())
1458 Out << "addrspace(" << AddressSpace << ") ";
Rafael Espindola45e6c192011-01-08 16:42:36 +00001459 if (GV->hasUnnamedAddr()) Out << "unnamed_addr ";
Michael Gottesman27e7ef32013-02-05 05:57:38 +00001460 if (GV->isExternallyInitialized()) Out << "externally_initialized ";
Misha Brukmana6619a92004-06-21 21:53:56 +00001461 Out << (GV->isConstant() ? "constant " : "global ");
Chris Lattnere101c442009-02-28 21:26:53 +00001462 TypePrinter.print(GV->getType()->getElementType(), Out);
Chris Lattner37798642001-09-18 04:01:05 +00001463
Dan Gohman81313fd2008-09-14 17:21:12 +00001464 if (GV->hasInitializer()) {
1465 Out << ' ';
Devang Patel983c6b12009-07-08 21:44:25 +00001466 writeOperand(GV->getInitializer(), false);
Dan Gohman81313fd2008-09-14 17:21:12 +00001467 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001468
Chris Lattner9380b812010-07-07 23:16:37 +00001469 if (GV->hasSection()) {
1470 Out << ", section \"";
1471 PrintEscapedString(GV->getSection(), Out);
1472 Out << '"';
1473 }
Chris Lattner4b96c542005-11-12 00:10:19 +00001474 if (GV->getAlignment())
Chris Lattnerf8a974d2005-11-06 06:48:53 +00001475 Out << ", align " << GV->getAlignment();
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001476
Chris Lattner113f4f42002-06-25 16:13:24 +00001477 printInfoComment(*GV);
Chris Lattnerda975502001-09-10 07:58:01 +00001478}
1479
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001480void AssemblyWriter::printAlias(const GlobalAlias *GA) {
Dan Gohman5ded1422010-01-29 23:12:36 +00001481 if (GA->isMaterializable())
1482 Out << "; Materializable\n";
1483
Dale Johannesen83e468a2008-06-03 18:14:29 +00001484 // Don't crash when dumping partially built GA
1485 if (!GA->hasName())
1486 Out << "<<nameless>> = ";
Chris Lattner033935d2008-08-17 04:40:13 +00001487 else {
1488 PrintLLVMName(Out, GA);
1489 Out << " = ";
1490 }
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001491 PrintVisibility(GA->getVisibility(), 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
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001499 if (Aliasee == 0) {
1500 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);
Chris Lattner379a8d22003-04-16 20:28:45 +00001589
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001590 // Print the calling convention.
Micah Villmow857186d2012-09-13 15:11:12 +00001591 if (F->getCallingConv() != CallingConv::C) {
1592 PrintCallingConv(F->getCallingConv(), Out);
1593 Out << " ";
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001594 }
1595
Chris Lattner229907c2011-07-18 04:54:35 +00001596 FunctionType *FT = F->getFunctionType();
Bill Wendling658d24d2013-01-18 21:53:16 +00001597 if (Attrs.hasAttributes(AttributeSet::ReturnIndex))
1598 Out << Attrs.getAsString(AttributeSet::ReturnIndex) << ' ';
Chris Lattnere101c442009-02-28 21:26:53 +00001599 TypePrinter.print(F->getReturnType(), Out);
Chris Lattner585297e82008-08-19 05:26:17 +00001600 Out << ' ';
Dan Gohmana2489d12010-07-20 23:55:01 +00001601 WriteAsOperandInternal(Out, F, &TypePrinter, &Machine, F->getParent());
Misha Brukmana6619a92004-06-21 21:53:56 +00001602 Out << '(';
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001603 Machine.incorporateFunction(F);
Chris Lattnerfee714f2001-09-07 16:36:04 +00001604
Chris Lattner7bfee412001-10-29 16:05:51 +00001605 // Loop over the arguments, printing them...
Chris Lattnerfee714f2001-09-07 16:36:04 +00001606
Reid Spencer8c4914c2006-12-31 05:24:50 +00001607 unsigned Idx = 1;
Chris Lattner82738fe2007-04-18 00:57:22 +00001608 if (!F->isDeclaration()) {
1609 // If this isn't a declaration, print the argument names as well.
1610 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
1611 I != E; ++I) {
1612 // Insert commas as we go... the first arg doesn't get a comma
1613 if (I != F->arg_begin()) Out << ", ";
Bill Wendling749a43d2012-12-30 13:50:49 +00001614 printArgument(I, Attrs, Idx);
Chris Lattner82738fe2007-04-18 00:57:22 +00001615 Idx++;
1616 }
1617 } else {
1618 // Otherwise, print the types from the function type.
1619 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
1620 // Insert commas as we go... the first arg doesn't get a comma
1621 if (i) Out << ", ";
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001622
Chris Lattner82738fe2007-04-18 00:57:22 +00001623 // Output type...
Chris Lattnere101c442009-02-28 21:26:53 +00001624 TypePrinter.print(FT->getParamType(i), Out);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001625
Bill Wendling749a43d2012-12-30 13:50:49 +00001626 if (Attrs.hasAttributes(i+1))
1627 Out << ' ' << Attrs.getAsString(i+1);
Chris Lattner82738fe2007-04-18 00:57:22 +00001628 }
Reid Spencer8c4914c2006-12-31 05:24:50 +00001629 }
Chris Lattnerfee714f2001-09-07 16:36:04 +00001630
1631 // Finish printing arguments...
Chris Lattner113f4f42002-06-25 16:13:24 +00001632 if (FT->isVarArg()) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001633 if (FT->getNumParams()) Out << ", ";
1634 Out << "..."; // Output varargs portion of signature!
Chris Lattnerfee714f2001-09-07 16:36:04 +00001635 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001636 Out << ')';
Rafael Espindola563eb4b2011-01-25 19:09:56 +00001637 if (F->hasUnnamedAddr())
1638 Out << " unnamed_addr";
Bill Wendling04945972013-04-29 23:48:06 +00001639 if (Attrs.hasAttributes(AttributeSet::FunctionIndex))
1640 Out << " #" << Machine.getAttributeGroupSlot(Attrs.getFnAttributes());
Chris Lattner9380b812010-07-07 23:16:37 +00001641 if (F->hasSection()) {
1642 Out << " section \"";
1643 PrintEscapedString(F->getSection(), Out);
1644 Out << '"';
1645 }
Chris Lattnerf8a974d2005-11-06 06:48:53 +00001646 if (F->getAlignment())
1647 Out << " align " << F->getAlignment();
Gordon Henriksend930f912008-08-17 18:44:35 +00001648 if (F->hasGC())
1649 Out << " gc \"" << F->getGC() << '"';
Reid Spencer5301e7c2007-01-30 20:08:39 +00001650 if (F->isDeclaration()) {
Chris Lattnerfb483622010-09-02 22:52:10 +00001651 Out << '\n';
Chris Lattnerb2f02e52002-05-06 03:00:40 +00001652 } else {
Chris Lattnerfb483622010-09-02 22:52:10 +00001653 Out << " {";
1654 // Output all of the function's basic blocks.
Chris Lattner113f4f42002-06-25 16:13:24 +00001655 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
1656 printBasicBlock(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +00001657
Misha Brukmana6619a92004-06-21 21:53:56 +00001658 Out << "}\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +00001659 }
1660
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001661 Machine.purgeFunction();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001662}
1663
Misha Brukmanc566ca362004-03-02 00:22:19 +00001664/// printArgument - This member is called for every argument that is passed into
1665/// the function. Simply print it out
1666///
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001667void AssemblyWriter::printArgument(const Argument *Arg,
Bill Wendling749a43d2012-12-30 13:50:49 +00001668 AttributeSet Attrs, unsigned Idx) {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001669 // Output type...
Chris Lattnere101c442009-02-28 21:26:53 +00001670 TypePrinter.print(Arg->getType(), Out);
Misha Brukmanb1c93172005-04-21 23:48:37 +00001671
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00001672 // Output parameter attributes list
Bill Wendling749a43d2012-12-30 13:50:49 +00001673 if (Attrs.hasAttributes(Idx))
1674 Out << ' ' << Attrs.getAsString(Idx);
Reid Spencer8c4914c2006-12-31 05:24:50 +00001675
Chris Lattner2f7c9632001-06-06 20:29:01 +00001676 // Output name, if available...
Chris Lattner033935d2008-08-17 04:40:13 +00001677 if (Arg->hasName()) {
1678 Out << ' ';
1679 PrintLLVMName(Out, Arg);
1680 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001681}
1682
Misha Brukmanc566ca362004-03-02 00:22:19 +00001683/// printBasicBlock - This member is called for each basic block in a method.
1684///
Chris Lattner7bfee412001-10-29 16:05:51 +00001685void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Nick Lewycky4d43d3c2008-04-25 16:53:59 +00001686 if (BB->hasName()) { // Print out the label if it exists...
Chris Lattner033935d2008-08-17 04:40:13 +00001687 Out << "\n";
Daniel Dunbare03eecb2009-07-25 23:55:21 +00001688 PrintLLVMName(Out, BB->getName(), LabelPrefix);
Chris Lattner033935d2008-08-17 04:40:13 +00001689 Out << ':';
Nick Lewycky4d43d3c2008-04-25 16:53:59 +00001690 } else if (!BB->use_empty()) { // Don't print block # of no uses...
Bill Wendling35a9c3c2011-04-10 23:18:04 +00001691 Out << "\n; <label>:";
Chris Lattner5e043322007-01-11 03:54:27 +00001692 int Slot = Machine.getLocalSlot(BB);
Chris Lattner757ee0b2004-06-09 19:41:19 +00001693 if (Slot != -1)
Misha Brukmana6619a92004-06-21 21:53:56 +00001694 Out << Slot;
Chris Lattner757ee0b2004-06-09 19:41:19 +00001695 else
Misha Brukmana6619a92004-06-21 21:53:56 +00001696 Out << "<badref>";
Chris Lattner58185f22002-10-02 19:38:55 +00001697 }
Chris Lattner2447ef52003-11-20 00:09:43 +00001698
Dan Gohmane2745262009-08-12 17:23:50 +00001699 if (BB->getParent() == 0) {
Chris Lattneree97b8b2009-08-17 15:48:08 +00001700 Out.PadToColumn(50);
Dan Gohmane2745262009-08-12 17:23:50 +00001701 Out << "; Error: Block without parent!";
1702 } else if (BB != &BB->getParent()->getEntryBlock()) { // Not the entry block?
Chris Lattnerfb483622010-09-02 22:52:10 +00001703 // Output predecessors for the block.
Chris Lattneree97b8b2009-08-17 15:48:08 +00001704 Out.PadToColumn(50);
Dan Gohmane2745262009-08-12 17:23:50 +00001705 Out << ";";
Gabor Greif6c6b2fd2010-03-25 23:25:28 +00001706 const_pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001707
Chris Lattnerff834c02008-04-22 02:45:44 +00001708 if (PI == PE) {
1709 Out << " No predecessors!";
1710 } else {
Dan Gohman81313fd2008-09-14 17:21:12 +00001711 Out << " preds = ";
Chris Lattnerff834c02008-04-22 02:45:44 +00001712 writeOperand(*PI, false);
1713 for (++PI; PI != PE; ++PI) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001714 Out << ", ";
Chris Lattner78e2e8b2006-12-06 06:24:27 +00001715 writeOperand(*PI, false);
Chris Lattner00211f12003-11-16 22:59:57 +00001716 }
Chris Lattner58185f22002-10-02 19:38:55 +00001717 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001718 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001719
Chris Lattnerff834c02008-04-22 02:45:44 +00001720 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001721
Misha Brukmana6619a92004-06-21 21:53:56 +00001722 if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001723
Chris Lattnerfee714f2001-09-07 16:36:04 +00001724 // Output all of the instructions in the basic block...
Dan Gohmana4f709e2009-07-13 18:27:59 +00001725 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
Daniel Maleaded9f932013-05-08 20:38:31 +00001726 printInstructionLine(*I);
Dan Gohmana4f709e2009-07-13 18:27:59 +00001727 }
Chris Lattner96cdd272004-03-08 18:51:45 +00001728
Misha Brukmana6619a92004-06-21 21:53:56 +00001729 if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, Out);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001730}
1731
Daniel Maleaded9f932013-05-08 20:38:31 +00001732/// printInstructionLine - Print an instruction and a newline character.
1733void AssemblyWriter::printInstructionLine(const Instruction &I) {
1734 printInstruction(I);
1735 Out << '\n';
1736}
1737
Misha Brukmanc566ca362004-03-02 00:22:19 +00001738/// printInfoComment - Print a little comment after the instruction indicating
1739/// which slot it occupies.
1740///
Chris Lattner113f4f42002-06-25 16:13:24 +00001741void AssemblyWriter::printInfoComment(const Value &V) {
Bill Wendling847a5c32013-04-16 20:55:47 +00001742 if (AnnotationWriter)
Dan Gohmane34890f2010-02-10 20:41:46 +00001743 AnnotationWriter->printInfoComment(V, Out);
Chris Lattner862e3382001-10-13 06:42:36 +00001744}
1745
Reid Spencere7141c82006-08-28 01:02:49 +00001746// This member is called for each Instruction in a function..
Chris Lattner113f4f42002-06-25 16:13:24 +00001747void AssemblyWriter::printInstruction(const Instruction &I) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001748 if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001749
Dan Gohman466876b2009-08-12 23:32:33 +00001750 // Print out indentation for an instruction.
Dan Gohmanb4583b12009-08-13 01:41:52 +00001751 Out << " ";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001752
1753 // Print out name if it exists...
Chris Lattner033935d2008-08-17 04:40:13 +00001754 if (I.hasName()) {
1755 PrintLLVMName(Out, &I);
1756 Out << " = ";
Chris Lattner031560c2009-12-29 07:25:48 +00001757 } else if (!I.getType()->isVoidTy()) {
Chris Lattnerb25e5ea2008-08-29 17:19:30 +00001758 // Print out the def slot taken.
1759 int SlotNum = Machine.getLocalSlot(&I);
1760 if (SlotNum == -1)
1761 Out << "<badref> = ";
1762 else
1763 Out << '%' << SlotNum << " = ";
Chris Lattner033935d2008-08-17 04:40:13 +00001764 }
Andrew Trickec4b6e72011-09-30 19:48:58 +00001765
Eli Friedman59b66882011-08-09 23:02:53 +00001766 if (isa<CallInst>(I) && cast<CallInst>(I).isTailCall())
Chris Lattner06038452005-05-06 05:51:46 +00001767 Out << "tail ";
Chris Lattner504f9242003-09-08 17:45:59 +00001768
Chris Lattner2f7c9632001-06-06 20:29:01 +00001769 // Print out the opcode...
Misha Brukmana6619a92004-06-21 21:53:56 +00001770 Out << I.getOpcodeName();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001771
Eli Friedman02e737b2011-08-12 22:50:01 +00001772 // If this is an atomic load or store, print out the atomic marker.
1773 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isAtomic()) ||
1774 (isa<StoreInst>(I) && cast<StoreInst>(I).isAtomic()))
1775 Out << " atomic";
1776
1777 // If this is a volatile operation, print out the volatile marker.
1778 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) ||
1779 (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile()) ||
1780 (isa<AtomicCmpXchgInst>(I) && cast<AtomicCmpXchgInst>(I).isVolatile()) ||
1781 (isa<AtomicRMWInst>(I) && cast<AtomicRMWInst>(I).isVolatile()))
1782 Out << " volatile";
1783
Dan Gohman9c7f8082009-07-27 16:11:46 +00001784 // Print out optimization information.
1785 WriteOptimizationInfo(Out, &I);
1786
Reid Spencer45e52392006-12-03 06:27:29 +00001787 // Print out the compare instruction predicates
Nate Begemand2195702008-05-12 19:01:56 +00001788 if (const CmpInst *CI = dyn_cast<CmpInst>(&I))
Chris Lattner82ff9232008-08-23 22:52:27 +00001789 Out << ' ' << getPredicateText(CI->getPredicate());
Reid Spencer45e52392006-12-03 06:27:29 +00001790
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001791 // Print out the atomicrmw operation
1792 if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(&I))
1793 writeAtomicRMWOperation(Out, RMWI->getOperation());
1794
Chris Lattner2f7c9632001-06-06 20:29:01 +00001795 // Print out the type of the operands...
Chris Lattner113f4f42002-06-25 16:13:24 +00001796 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001797
1798 // Special case conditional branches to swizzle the condition out to the front
Gabor Greifcab008f2009-02-09 15:45:06 +00001799 if (isa<BranchInst>(I) && cast<BranchInst>(I).isConditional()) {
David Blaikieb78e9e52013-02-11 01:16:51 +00001800 const BranchInst &BI(cast<BranchInst>(I));
Dan Gohman81313fd2008-09-14 17:21:12 +00001801 Out << ' ';
Gabor Greifcab008f2009-02-09 15:45:06 +00001802 writeOperand(BI.getCondition(), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00001803 Out << ", ";
Gabor Greifcab008f2009-02-09 15:45:06 +00001804 writeOperand(BI.getSuccessor(0), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00001805 Out << ", ";
Gabor Greifcab008f2009-02-09 15:45:06 +00001806 writeOperand(BI.getSuccessor(1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001807
Chris Lattner8d48df22002-04-13 18:34:38 +00001808 } else if (isa<SwitchInst>(I)) {
David Blaikieb78e9e52013-02-11 01:16:51 +00001809 const SwitchInst& SI(cast<SwitchInst>(I));
Chris Lattner3ed871f2009-10-27 19:13:16 +00001810 // Special case switch instruction to get formatting nice and correct.
Dan Gohman81313fd2008-09-14 17:21:12 +00001811 Out << ' ';
Eli Friedman95031ed2011-09-29 20:21:17 +00001812 writeOperand(SI.getCondition(), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00001813 Out << ", ";
Eli Friedman95031ed2011-09-29 20:21:17 +00001814 writeOperand(SI.getDefaultDest(), true);
Chris Lattner82ff9232008-08-23 22:52:27 +00001815 Out << " [";
David Blaikieb78e9e52013-02-11 01:16:51 +00001816 for (SwitchInst::ConstCaseIt i = SI.case_begin(), e = SI.case_end();
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00001817 i != e; ++i) {
Dan Gohmanb4583b12009-08-13 01:41:52 +00001818 Out << "\n ";
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00001819 writeOperand(i.getCaseValue(), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00001820 Out << ", ";
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00001821 writeOperand(i.getCaseSuccessor(), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001822 }
Dan Gohmanb4583b12009-08-13 01:41:52 +00001823 Out << "\n ]";
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00001824 } else if (isa<IndirectBrInst>(I)) {
1825 // Special case indirectbr instruction to get formatting nice and correct.
Chris Lattner3ed871f2009-10-27 19:13:16 +00001826 Out << ' ';
1827 writeOperand(Operand, true);
Dan Gohman43c57402009-10-30 02:01:10 +00001828 Out << ", [";
Andrew Trickec4b6e72011-09-30 19:48:58 +00001829
Chris Lattner3ed871f2009-10-27 19:13:16 +00001830 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) {
1831 if (i != 1)
1832 Out << ", ";
1833 writeOperand(I.getOperand(i), true);
1834 }
1835 Out << ']';
Jay Foad372ad642011-06-20 14:18:48 +00001836 } else if (const PHINode *PN = dyn_cast<PHINode>(&I)) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001837 Out << ' ';
Chris Lattnere101c442009-02-28 21:26:53 +00001838 TypePrinter.print(I.getType(), Out);
Misha Brukmana6619a92004-06-21 21:53:56 +00001839 Out << ' ';
Chris Lattner2f7c9632001-06-06 20:29:01 +00001840
Jay Foad372ad642011-06-20 14:18:48 +00001841 for (unsigned op = 0, Eop = PN->getNumIncomingValues(); op < Eop; ++op) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001842 if (op) Out << ", ";
Dan Gohman81313fd2008-09-14 17:21:12 +00001843 Out << "[ ";
Jay Foad372ad642011-06-20 14:18:48 +00001844 writeOperand(PN->getIncomingValue(op), false); Out << ", ";
1845 writeOperand(PN->getIncomingBlock(op), false); Out << " ]";
Chris Lattner931ef3b2001-06-11 15:04:20 +00001846 }
Dan Gohmana76f0f72008-05-31 19:12:39 +00001847 } else if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(&I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001848 Out << ' ';
Dan Gohmana76f0f72008-05-31 19:12:39 +00001849 writeOperand(I.getOperand(0), true);
1850 for (const unsigned *i = EVI->idx_begin(), *e = EVI->idx_end(); i != e; ++i)
1851 Out << ", " << *i;
1852 } else if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(&I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001853 Out << ' ';
1854 writeOperand(I.getOperand(0), true); Out << ", ";
Dan Gohmana76f0f72008-05-31 19:12:39 +00001855 writeOperand(I.getOperand(1), true);
1856 for (const unsigned *i = IVI->idx_begin(), *e = IVI->idx_end(); i != e; ++i)
1857 Out << ", " << *i;
Bill Wendlingfae14752011-08-12 20:24:12 +00001858 } else if (const LandingPadInst *LPI = dyn_cast<LandingPadInst>(&I)) {
1859 Out << ' ';
1860 TypePrinter.print(I.getType(), Out);
1861 Out << " personality ";
1862 writeOperand(I.getOperand(0), true); Out << '\n';
1863
1864 if (LPI->isCleanup())
1865 Out << " cleanup";
1866
1867 for (unsigned i = 0, e = LPI->getNumClauses(); i != e; ++i) {
1868 if (i != 0 || LPI->isCleanup()) Out << "\n";
1869 if (LPI->isCatch(i))
1870 Out << " catch ";
1871 else
1872 Out << " filter ";
1873
1874 writeOperand(LPI->getClause(i), true);
1875 }
Devang Patel59643e52008-02-23 00:35:18 +00001876 } else if (isa<ReturnInst>(I) && !Operand) {
1877 Out << " void";
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001878 } else if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
1879 // Print the calling convention being used.
Micah Villmow857186d2012-09-13 15:11:12 +00001880 if (CI->getCallingConv() != CallingConv::C) {
1881 Out << " ";
1882 PrintCallingConv(CI->getCallingConv(), Out);
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001883 }
1884
Gabor Greifc9a92512010-06-23 13:09:06 +00001885 Operand = CI->getCalledValue();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001886 PointerType *PTy = cast<PointerType>(Operand->getType());
1887 FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1888 Type *RetTy = FTy->getReturnType();
Bill Wendlinge94d8432012-12-07 23:16:57 +00001889 const AttributeSet &PAL = CI->getAttributes();
Chris Lattner2f2d9472001-11-06 21:28:12 +00001890
Bill Wendling658d24d2013-01-18 21:53:16 +00001891 if (PAL.hasAttributes(AttributeSet::ReturnIndex))
1892 Out << ' ' << PAL.getAsString(AttributeSet::ReturnIndex);
Devang Patel221fe422008-09-29 20:49:50 +00001893
Chris Lattner463d6a52003-08-05 15:34:45 +00001894 // If possible, print out the short form of the call instruction. We can
Chris Lattner6915f8f2002-04-07 22:49:37 +00001895 // only do this if the first argument is a pointer to a nonvararg function,
Chris Lattner463d6a52003-08-05 15:34:45 +00001896 // and if the return type is not a pointer to a function.
Chris Lattner2f2d9472001-11-06 21:28:12 +00001897 //
Dan Gohman81313fd2008-09-14 17:21:12 +00001898 Out << ' ';
Chris Lattner463d6a52003-08-05 15:34:45 +00001899 if (!FTy->isVarArg() &&
Duncan Sands19d0b472010-02-16 11:11:14 +00001900 (!RetTy->isPointerTy() ||
1901 !cast<PointerType>(RetTy)->getElementType()->isFunctionTy())) {
Chris Lattnere101c442009-02-28 21:26:53 +00001902 TypePrinter.print(RetTy, Out);
Dan Gohman81313fd2008-09-14 17:21:12 +00001903 Out << ' ';
Chris Lattner2f2d9472001-11-06 21:28:12 +00001904 writeOperand(Operand, false);
1905 } else {
1906 writeOperand(Operand, true);
1907 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001908 Out << '(';
Gabor Greifc9a92512010-06-23 13:09:06 +00001909 for (unsigned op = 0, Eop = CI->getNumArgOperands(); op < Eop; ++op) {
1910 if (op > 0)
Dan Gohman81313fd2008-09-14 17:21:12 +00001911 Out << ", ";
Bill Wendling749a43d2012-12-30 13:50:49 +00001912 writeParamOperand(CI->getArgOperand(op), PAL, op + 1);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001913 }
Dan Gohman81313fd2008-09-14 17:21:12 +00001914 Out << ')';
Bill Wendling698e84f2012-12-30 10:32:01 +00001915 if (PAL.hasAttributes(AttributeSet::FunctionIndex))
Bill Wendlinga0323742013-02-22 09:09:42 +00001916 Out << " #" << Machine.getAttributeGroupSlot(PAL.getFnAttributes());
Chris Lattner113f4f42002-06-25 16:13:24 +00001917 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
Gabor Greifa2fbc0a2010-03-24 13:21:49 +00001918 Operand = II->getCalledValue();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001919 PointerType *PTy = cast<PointerType>(Operand->getType());
1920 FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1921 Type *RetTy = FTy->getReturnType();
Bill Wendlinge94d8432012-12-07 23:16:57 +00001922 const AttributeSet &PAL = II->getAttributes();
Chris Lattner463d6a52003-08-05 15:34:45 +00001923
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001924 // Print the calling convention being used.
Micah Villmow857186d2012-09-13 15:11:12 +00001925 if (II->getCallingConv() != CallingConv::C) {
1926 Out << " ";
1927 PrintCallingConv(II->getCallingConv(), Out);
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001928 }
1929
Bill Wendling658d24d2013-01-18 21:53:16 +00001930 if (PAL.hasAttributes(AttributeSet::ReturnIndex))
1931 Out << ' ' << PAL.getAsString(AttributeSet::ReturnIndex);
Devang Patel221fe422008-09-29 20:49:50 +00001932
Chris Lattner463d6a52003-08-05 15:34:45 +00001933 // If possible, print out the short form of the invoke instruction. We can
1934 // only do this if the first argument is a pointer to a nonvararg function,
1935 // and if the return type is not a pointer to a function.
1936 //
Dan Gohmanc7e00ba2008-10-15 18:02:08 +00001937 Out << ' ';
Chris Lattner463d6a52003-08-05 15:34:45 +00001938 if (!FTy->isVarArg() &&
Duncan Sands19d0b472010-02-16 11:11:14 +00001939 (!RetTy->isPointerTy() ||
1940 !cast<PointerType>(RetTy)->getElementType()->isFunctionTy())) {
Chris Lattnere101c442009-02-28 21:26:53 +00001941 TypePrinter.print(RetTy, Out);
Dan Gohmanc7e00ba2008-10-15 18:02:08 +00001942 Out << ' ';
Chris Lattner463d6a52003-08-05 15:34:45 +00001943 writeOperand(Operand, false);
1944 } else {
1945 writeOperand(Operand, true);
1946 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001947 Out << '(';
Gabor Greifc9a92512010-06-23 13:09:06 +00001948 for (unsigned op = 0, Eop = II->getNumArgOperands(); op < Eop; ++op) {
Gabor Greifa2fbc0a2010-03-24 13:21:49 +00001949 if (op)
Dan Gohman81313fd2008-09-14 17:21:12 +00001950 Out << ", ";
Bill Wendling749a43d2012-12-30 13:50:49 +00001951 writeParamOperand(II->getArgOperand(op), PAL, op + 1);
Chris Lattner862e3382001-10-13 06:42:36 +00001952 }
1953
Dan Gohman81313fd2008-09-14 17:21:12 +00001954 Out << ')';
Bill Wendling698e84f2012-12-30 10:32:01 +00001955 if (PAL.hasAttributes(AttributeSet::FunctionIndex))
Bill Wendlinga0323742013-02-22 09:09:42 +00001956 Out << " #" << Machine.getAttributeGroupSlot(PAL.getFnAttributes());
Devang Patela05633e2008-09-26 22:53:05 +00001957
Dan Gohmanb4583b12009-08-13 01:41:52 +00001958 Out << "\n to ";
Chris Lattner862e3382001-10-13 06:42:36 +00001959 writeOperand(II->getNormalDest(), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00001960 Out << " unwind ";
Chris Lattnerfae8ab32004-02-08 21:44:31 +00001961 writeOperand(II->getUnwindDest(), true);
Chris Lattner862e3382001-10-13 06:42:36 +00001962
Victor Hernandez8acf2952009-10-23 21:09:37 +00001963 } else if (const AllocaInst *AI = dyn_cast<AllocaInst>(&I)) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001964 Out << ' ';
Dan Gohmanb2f426c2013-02-08 22:01:47 +00001965 TypePrinter.print(AI->getAllocatedType(), Out);
Dan Gohmanb53e26c2009-07-31 18:23:24 +00001966 if (!AI->getArraySize() || AI->isArrayAllocation()) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001967 Out << ", ";
Chris Lattner8d48df22002-04-13 18:34:38 +00001968 writeOperand(AI->getArraySize(), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001969 }
Nate Begeman848622f2005-11-05 09:21:28 +00001970 if (AI->getAlignment()) {
Chris Lattner7aeee3a2005-11-05 21:20:34 +00001971 Out << ", align " << AI->getAlignment();
Nate Begeman848622f2005-11-05 09:21:28 +00001972 }
Chris Lattner862e3382001-10-13 06:42:36 +00001973 } else if (isa<CastInst>(I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001974 if (Operand) {
1975 Out << ' ';
1976 writeOperand(Operand, true); // Work with broken code
1977 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001978 Out << " to ";
Chris Lattnere101c442009-02-28 21:26:53 +00001979 TypePrinter.print(I.getType(), Out);
Chris Lattner5b337482003-10-18 05:57:43 +00001980 } else if (isa<VAArgInst>(I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001981 if (Operand) {
1982 Out << ' ';
1983 writeOperand(Operand, true); // Work with broken code
1984 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001985 Out << ", ";
Chris Lattnere101c442009-02-28 21:26:53 +00001986 TypePrinter.print(I.getType(), Out);
1987 } else if (Operand) { // Print the normal way.
Chris Lattner2f7c9632001-06-06 20:29:01 +00001988
Misha Brukmanb1c93172005-04-21 23:48:37 +00001989 // PrintAllTypes - Instructions who have operands of all the same type
Chris Lattner2f7c9632001-06-06 20:29:01 +00001990 // omit the type from all but the first operand. If the instruction has
1991 // different type operands (for example br), then they are all printed.
1992 bool PrintAllTypes = false;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001993 Type *TheType = Operand->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001994
Reid Spencer0cdd04f2007-02-02 13:54:55 +00001995 // Select, Store and ShuffleVector always print all types.
Devang Patelce556d92008-03-04 22:05:14 +00001996 if (isa<SelectInst>(I) || isa<StoreInst>(I) || isa<ShuffleVectorInst>(I)
1997 || isa<ReturnInst>(I)) {
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00001998 PrintAllTypes = true;
1999 } else {
2000 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
2001 Operand = I.getOperand(i);
Nuno Lopes04fe2f02009-01-15 18:40:57 +00002002 // note that Operand shouldn't be null, but the test helps make dump()
2003 // more tolerant of malformed IR
Nuno Lopes0971e772009-01-14 17:51:41 +00002004 if (Operand && Operand->getType() != TheType) {
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00002005 PrintAllTypes = true; // We have differing types! Print them all!
2006 break;
2007 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00002008 }
2009 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00002010
Chris Lattner7bfee412001-10-29 16:05:51 +00002011 if (!PrintAllTypes) {
Misha Brukmana6619a92004-06-21 21:53:56 +00002012 Out << ' ';
Chris Lattnere101c442009-02-28 21:26:53 +00002013 TypePrinter.print(TheType, Out);
Chris Lattner7bfee412001-10-29 16:05:51 +00002014 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00002015
Dan Gohman81313fd2008-09-14 17:21:12 +00002016 Out << ' ';
Chris Lattner113f4f42002-06-25 16:13:24 +00002017 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
Dan Gohman81313fd2008-09-14 17:21:12 +00002018 if (i) Out << ", ";
Chris Lattner113f4f42002-06-25 16:13:24 +00002019 writeOperand(I.getOperand(i), PrintAllTypes);
Chris Lattner2f7c9632001-06-06 20:29:01 +00002020 }
2021 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002022
Eli Friedman59b66882011-08-09 23:02:53 +00002023 // Print atomic ordering/alignment for memory operations
2024 if (const LoadInst *LI = dyn_cast<LoadInst>(&I)) {
2025 if (LI->isAtomic())
2026 writeAtomic(LI->getOrdering(), LI->getSynchScope());
2027 if (LI->getAlignment())
2028 Out << ", align " << LI->getAlignment();
2029 } else if (const StoreInst *SI = dyn_cast<StoreInst>(&I)) {
2030 if (SI->isAtomic())
2031 writeAtomic(SI->getOrdering(), SI->getSynchScope());
2032 if (SI->getAlignment())
2033 Out << ", align " << SI->getAlignment();
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002034 } else if (const AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(&I)) {
2035 writeAtomic(CXI->getOrdering(), CXI->getSynchScope());
2036 } else if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(&I)) {
2037 writeAtomic(RMWI->getOrdering(), RMWI->getSynchScope());
Eli Friedmanfee02c62011-07-25 23:16:38 +00002038 } else if (const FenceInst *FI = dyn_cast<FenceInst>(&I)) {
2039 writeAtomic(FI->getOrdering(), FI->getSynchScope());
Christopher Lamb84485702007-04-22 19:24:39 +00002040 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00002041
Chris Lattnerc9558df2009-12-28 20:10:43 +00002042 // Print Metadata info.
Nick Lewyckyba8ec9a2010-02-25 06:53:04 +00002043 SmallVector<std::pair<unsigned, MDNode*>, 4> InstMD;
2044 I.getAllMetadata(InstMD);
Erick Tryzelaar72a37132010-03-02 05:32:52 +00002045 if (!InstMD.empty()) {
2046 SmallVector<StringRef, 8> MDNames;
2047 I.getType()->getContext().getMDKindNames(MDNames);
2048 for (unsigned i = 0, e = InstMD.size(); i != e; ++i) {
2049 unsigned Kind = InstMD[i].first;
2050 if (Kind < MDNames.size()) {
2051 Out << ", !" << MDNames[Kind];
Daniel Maleaded9f932013-05-08 20:38:31 +00002052 } else {
2053 Out << ", !<unknown kind #" << Kind << ">";
2054 }
Dan Gohmana2489d12010-07-20 23:55:01 +00002055 Out << ' ';
2056 WriteAsOperandInternal(Out, InstMD[i].second, &TypePrinter, &Machine,
2057 TheModule);
Nick Lewyckyba8ec9a2010-02-25 06:53:04 +00002058 }
Devang Patelbcdb0252009-10-07 16:37:55 +00002059 }
Chris Lattner862e3382001-10-13 06:42:36 +00002060 printInfoComment(I);
Chris Lattner2f7c9632001-06-06 20:29:01 +00002061}
2062
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002063static void WriteMDNodeComment(const MDNode *Node,
Duncan Sands41b4a6b2010-07-12 08:16:59 +00002064 formatted_raw_ostream &Out) {
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002065 if (Node->getNumOperands() < 1)
2066 return;
Bill Wendling5cb50c52012-06-28 00:41:44 +00002067
2068 Value *Op = Node->getOperand(0);
2069 if (!Op || !isa<ConstantInt>(Op) || cast<ConstantInt>(Op)->getBitWidth() < 32)
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002070 return;
Andrew Trickec4b6e72011-09-30 19:48:58 +00002071
Bill Wendling5cb50c52012-06-28 00:41:44 +00002072 DIDescriptor Desc(Node);
David Blaikiedc69ebb2013-03-11 23:39:23 +00002073 if (!Desc.Verify())
Bill Wendling5cb50c52012-06-28 00:41:44 +00002074 return;
2075
2076 unsigned Tag = Desc.getTag();
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002077 Out.PadToColumn(50);
Bill Wendlinga0bc1082012-07-03 20:01:02 +00002078 if (dwarf::TagString(Tag)) {
2079 Out << "; ";
2080 Desc.print(Out);
2081 } else if (Tag == dwarf::DW_TAG_user_base) {
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002082 Out << "; [ DW_TAG_user_base ]";
Bill Wendlinga0bc1082012-07-03 20:01:02 +00002083 }
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002084}
2085
Daniel Maleaded9f932013-05-08 20:38:31 +00002086void AssemblyWriter::writeMDNode(unsigned Slot, const MDNode *Node) {
2087 Out << '!' << Slot << " = metadata ";
2088 printMDNodeBody(Node);
2089}
2090
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002091void AssemblyWriter::writeAllMDNodes() {
2092 SmallVector<const MDNode *, 16> Nodes;
Chris Lattnercf4a76e2009-12-31 02:20:11 +00002093 Nodes.resize(Machine.mdn_size());
2094 for (SlotTracker::mdn_iterator I = Machine.mdn_begin(), E = Machine.mdn_end();
2095 I != E; ++I)
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002096 Nodes[I->second] = cast<MDNode>(I->first);
Andrew Trickec4b6e72011-09-30 19:48:58 +00002097
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002098 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Daniel Maleaded9f932013-05-08 20:38:31 +00002099 writeMDNode(i, Nodes[i]);
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002100 }
2101}
2102
2103void AssemblyWriter::printMDNodeBody(const MDNode *Node) {
Dan Gohmana2489d12010-07-20 23:55:01 +00002104 WriteMDNodeBodyInternal(Out, Node, &TypePrinter, &Machine, TheModule);
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002105 WriteMDNodeComment(Node, Out);
2106 Out << "\n";
2107}
Chris Lattner2f7c9632001-06-06 20:29:01 +00002108
Bill Wendling829b4782013-02-11 08:43:33 +00002109void AssemblyWriter::writeAllAttributeGroups() {
2110 std::vector<std::pair<AttributeSet, unsigned> > asVec;
2111 asVec.resize(Machine.as_size());
2112
2113 for (SlotTracker::as_iterator I = Machine.as_begin(), E = Machine.as_end();
2114 I != E; ++I)
2115 asVec[I->second] = *I;
2116
2117 for (std::vector<std::pair<AttributeSet, unsigned> >::iterator
2118 I = asVec.begin(), E = asVec.end(); I != E; ++I)
2119 Out << "attributes #" << I->second << " = { "
Rafael Espindolacbf5a7a2013-05-01 13:07:03 +00002120 << I->first.getAsString(AttributeSet::FunctionIndex, true) << " }\n";
Bill Wendling829b4782013-02-11 08:43:33 +00002121}
2122
Daniel Maleaded9f932013-05-08 20:38:31 +00002123} // namespace llvm
2124
Chris Lattner2f7c9632001-06-06 20:29:01 +00002125//===----------------------------------------------------------------------===//
2126// External Interface declarations
2127//===----------------------------------------------------------------------===//
2128
Dan Gohmane2745262009-08-12 17:23:50 +00002129void Module::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW) const {
Chris Lattnere36fd8a2008-08-19 04:26:57 +00002130 SlotTracker SlotTable(this);
Dan Gohmane2745262009-08-12 17:23:50 +00002131 formatted_raw_ostream OS(ROS);
Chris Lattner0c19df42008-08-23 22:23:09 +00002132 AssemblyWriter W(OS, SlotTable, this, AAW);
Chris Lattnerefb5e392009-12-31 02:23:35 +00002133 W.printModule(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00002134}
2135
Dan Gohman2637cc12010-07-21 23:38:33 +00002136void NamedMDNode::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW) const {
2137 SlotTracker SlotTable(getParent());
2138 formatted_raw_ostream OS(ROS);
2139 AssemblyWriter W(OS, SlotTable, getParent(), AAW);
2140 W.printNamedMDNode(this);
2141}
2142
Chris Lattner2ee62d22009-02-28 21:11:05 +00002143void Type::print(raw_ostream &OS) const {
2144 if (this == 0) {
2145 OS << "<null Type>";
2146 return;
2147 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002148 TypePrinting TP;
2149 TP.print(const_cast<Type*>(this), OS);
Andrew Trickec4b6e72011-09-30 19:48:58 +00002150
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002151 // If the type is a named struct type, print the body as well.
2152 if (StructType *STy = dyn_cast<StructType>(const_cast<Type*>(this)))
Chris Lattner01beceb2011-08-12 18:07:07 +00002153 if (!STy->isLiteral()) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002154 OS << " = type ";
2155 TP.printStructBody(STy, OS);
2156 }
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00002157}
2158
Dan Gohmane2745262009-08-12 17:23:50 +00002159void Value::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW) const {
Chris Lattner0c19df42008-08-23 22:23:09 +00002160 if (this == 0) {
Dan Gohman12dad632009-08-12 20:56:03 +00002161 ROS << "printing a <null> value\n";
Chris Lattner0c19df42008-08-23 22:23:09 +00002162 return;
2163 }
Dan Gohman12dad632009-08-12 20:56:03 +00002164 formatted_raw_ostream OS(ROS);
Chris Lattner0c19df42008-08-23 22:23:09 +00002165 if (const Instruction *I = dyn_cast<Instruction>(this)) {
2166 const Function *F = I->getParent() ? I->getParent()->getParent() : 0;
2167 SlotTracker SlotTable(F);
Chris Lattnerd5bace72009-12-31 08:23:09 +00002168 AssemblyWriter W(OS, SlotTable, getModuleFromVal(I), AAW);
Chris Lattnerefb5e392009-12-31 02:23:35 +00002169 W.printInstruction(*I);
Chris Lattner0c19df42008-08-23 22:23:09 +00002170 } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(this)) {
2171 SlotTracker SlotTable(BB->getParent());
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002172 AssemblyWriter W(OS, SlotTable, getModuleFromVal(BB), AAW);
Chris Lattnerefb5e392009-12-31 02:23:35 +00002173 W.printBasicBlock(BB);
Chris Lattner0c19df42008-08-23 22:23:09 +00002174 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(this)) {
2175 SlotTracker SlotTable(GV->getParent());
Dan Gohman6c7a4852009-04-20 16:10:33 +00002176 AssemblyWriter W(OS, SlotTable, GV->getParent(), AAW);
Chris Lattnerefb5e392009-12-31 02:23:35 +00002177 if (const GlobalVariable *V = dyn_cast<GlobalVariable>(GV))
2178 W.printGlobal(V);
2179 else if (const Function *F = dyn_cast<Function>(GV))
2180 W.printFunction(F);
2181 else
2182 W.printAlias(cast<GlobalAlias>(GV));
Devang Patel5546b982009-07-01 20:59:15 +00002183 } else if (const MDNode *N = dyn_cast<MDNode>(this)) {
Victor Hernandeze5f2af72010-01-20 04:45:57 +00002184 const Function *F = N->getFunction();
Victor Hernandez61e6e822010-01-14 01:47:37 +00002185 SlotTracker SlotTable(F);
Dan Gohman15132172010-07-14 21:12:44 +00002186 AssemblyWriter W(OS, SlotTable, F ? F->getParent() : 0, AAW);
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002187 W.printMDNodeBody(N);
Chris Lattner0c19df42008-08-23 22:23:09 +00002188 } else if (const Constant *C = dyn_cast<Constant>(this)) {
Chris Lattner92c5c122009-02-28 23:20:19 +00002189 TypePrinting TypePrinter;
Chris Lattnere101c442009-02-28 21:26:53 +00002190 TypePrinter.print(C->getType(), OS);
Chris Lattner2ee62d22009-02-28 21:11:05 +00002191 OS << ' ';
Dan Gohmana2489d12010-07-20 23:55:01 +00002192 WriteConstantInternal(OS, C, TypePrinter, 0, 0);
Chris Lattner31fcc282009-12-31 01:41:14 +00002193 } else if (isa<InlineAsm>(this) || isa<MDString>(this) ||
2194 isa<Argument>(this)) {
Chris Lattner0c19df42008-08-23 22:23:09 +00002195 WriteAsOperand(OS, this, true, 0);
2196 } else {
Dan Gohmanc0353bf2009-09-23 01:33:16 +00002197 // Otherwise we don't know what it is. Call the virtual function to
2198 // allow a subclass to print itself.
2199 printCustom(OS);
Chris Lattner0c19df42008-08-23 22:23:09 +00002200 }
2201}
2202
Dan Gohmanc0353bf2009-09-23 01:33:16 +00002203// Value::printCustom - subclasses should override this to implement printing.
2204void Value::printCustom(raw_ostream &OS) const {
2205 llvm_unreachable("Unknown value to print out!");
2206}
2207
Chris Lattnerdab942552008-08-25 17:03:15 +00002208// Value::dump - allow easy printing of Values from the debugger.
David Greenec7f9b122010-01-05 01:29:26 +00002209void Value::dump() const { print(dbgs()); dbgs() << '\n'; }
Reid Spencer52641832004-05-25 18:14:38 +00002210
Chris Lattnerdab942552008-08-25 17:03:15 +00002211// Type::dump - allow easy printing of Types from the debugger.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002212void Type::dump() const { print(dbgs()); }
Chris Lattner24025262009-02-28 21:05:51 +00002213
Chris Lattnerdab942552008-08-25 17:03:15 +00002214// Module::dump() - Allow printing of Modules from the debugger.
David Greenec7f9b122010-01-05 01:29:26 +00002215void Module::dump() const { print(dbgs(), 0); }
Bill Wendling3681d112011-12-09 23:18:34 +00002216
2217// NamedMDNode::dump() - Allow printing of NamedMDNodes from the debugger.
2218void NamedMDNode::dump() const { print(dbgs(), 0); }