blob: c94620372a23ea51cd2b6555d12677dd7d8a9b70 [file] [log] [blame]
Chris Lattner8da78af2002-04-07 22:31:46 +00001//===-- AsmWriter.cpp - Printing LLVM as an assembly file -----------------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
10// This library implements the functionality defined in llvm/Assembly/Writer.h
11//
Chris Lattner02b93992002-04-12 18:21:53 +000012// Note that these routines must be extremely tolerant of various errors in the
Chris Lattner8f77dae2003-05-08 02:44:12 +000013// LLVM code, because it can be used for debugging transformations.
Chris Lattner02b93992002-04-12 18:21:53 +000014//
Chris Lattner00950542001-06-06 20:29:01 +000015//===----------------------------------------------------------------------===//
16
Chris Lattner75cf7cf2002-04-08 22:03:40 +000017#include "llvm/Assembly/Writer.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000018#include "llvm/ADT/DenseMap.h"
19#include "llvm/ADT/STLExtras.h"
20#include "llvm/ADT/SmallString.h"
21#include "llvm/ADT/StringExtras.h"
Chris Lattner1dbb3872010-09-02 23:09:42 +000022#include "llvm/Assembly/AssemblyAnnotationWriter.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000023#include "llvm/Assembly/PrintModulePass.h"
Bill Wendling58a6cf22012-06-28 00:41:44 +000024#include "llvm/DebugInfo.h"
Daniel Malead0fef322013-05-08 20:38:31 +000025#include "llvm/IR/AsmWriter.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000026#include "llvm/IR/CallingConv.h"
27#include "llvm/IR/Constants.h"
28#include "llvm/IR/DerivedTypes.h"
29#include "llvm/IR/InlineAsm.h"
30#include "llvm/IR/IntrinsicInst.h"
31#include "llvm/IR/LLVMContext.h"
32#include "llvm/IR/Module.h"
33#include "llvm/IR/Operator.h"
Chandler Carruth4068e1a2013-01-07 15:43:51 +000034#include "llvm/IR/TypeFinder.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000035#include "llvm/IR/ValueSymbolTable.h"
Bill Wendling8f487662006-11-28 02:09:03 +000036#include "llvm/Support/CFG.h"
David Greened865e022010-01-05 01:29:26 +000037#include "llvm/Support/Debug.h"
Devang Patel2d5988d2009-09-30 20:16:54 +000038#include "llvm/Support/Dwarf.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000039#include "llvm/Support/ErrorHandling.h"
Dan Gohman683e9222009-08-12 17:23:50 +000040#include "llvm/Support/FormattedStream.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000041#include "llvm/Support/MathExtras.h"
Chris Lattner007377f2001-09-07 16:36:04 +000042#include <algorithm>
Reid Spencer4ad513c2007-05-22 19:27:35 +000043#include <cctype>
Chris Lattner31f84992003-11-21 20:23:48 +000044using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000045
Reid Spenceredd5d9e2005-05-15 16:13:11 +000046// Make virtual table appear in this compilation unit.
47AssemblyAnnotationWriter::~AssemblyAnnotationWriter() {}
48
Chris Lattner6ab910b2008-08-19 04:36:02 +000049//===----------------------------------------------------------------------===//
50// Helper Functions
51//===----------------------------------------------------------------------===//
52
53static const Module *getModuleFromVal(const Value *V) {
54 if (const Argument *MA = dyn_cast<Argument>(V))
55 return MA->getParent() ? MA->getParent()->getParent() : 0;
Daniel Dunbara279bc32009-09-20 02:20:51 +000056
Chris Lattner6ab910b2008-08-19 04:36:02 +000057 if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
58 return BB->getParent() ? BB->getParent()->getParent() : 0;
Daniel Dunbara279bc32009-09-20 02:20:51 +000059
Chris Lattner6ab910b2008-08-19 04:36:02 +000060 if (const Instruction *I = dyn_cast<Instruction>(V)) {
61 const Function *M = I->getParent() ? I->getParent()->getParent() : 0;
62 return M ? M->getParent() : 0;
63 }
Andrew Trick18801ec2011-09-30 19:48:58 +000064
Chris Lattner6ab910b2008-08-19 04:36:02 +000065 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
66 return GV->getParent();
67 return 0;
68}
69
Bill Wendling7ab6c762013-02-20 07:21:42 +000070static void PrintCallingConv(unsigned cc, raw_ostream &Out) {
Micah Villmowd3766df2012-09-13 15:11:12 +000071 switch (cc) {
Bill Wendling7ab6c762013-02-20 07:21:42 +000072 default: Out << "cc" << cc; break;
73 case CallingConv::Fast: Out << "fastcc"; break;
74 case CallingConv::Cold: Out << "coldcc"; break;
75 case CallingConv::X86_StdCall: Out << "x86_stdcallcc"; break;
76 case CallingConv::X86_FastCall: Out << "x86_fastcallcc"; break;
77 case CallingConv::X86_ThisCall: Out << "x86_thiscallcc"; break;
78 case CallingConv::Intel_OCL_BI: Out << "intel_ocl_bicc"; break;
79 case CallingConv::ARM_APCS: Out << "arm_apcscc"; break;
80 case CallingConv::ARM_AAPCS: Out << "arm_aapcscc"; break;
81 case CallingConv::ARM_AAPCS_VFP: Out << "arm_aapcs_vfpcc"; break;
82 case CallingConv::MSP430_INTR: Out << "msp430_intrcc"; break;
83 case CallingConv::PTX_Kernel: Out << "ptx_kernel"; break;
84 case CallingConv::PTX_Device: Out << "ptx_device"; break;
Micah Villmowd3766df2012-09-13 15:11:12 +000085 }
86}
Michael Ilseman407a6162012-11-15 22:34:00 +000087
Daniel Dunbare9da1332008-10-28 19:33:02 +000088// PrintEscapedString - Print each character of the specified string, escaping
89// it if it is not printable or if it is an escape char.
Chris Lattner8fff1262010-07-07 23:16:37 +000090static void PrintEscapedString(StringRef Name, raw_ostream &Out) {
Daniel Dunbar03d76512009-07-25 23:55:21 +000091 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
92 unsigned char C = Name[i];
Nick Lewycky34a40862009-03-15 06:39:52 +000093 if (isprint(C) && C != '\\' && C != '"')
Daniel Dunbare9da1332008-10-28 19:33:02 +000094 Out << C;
95 else
96 Out << '\\' << hexdigit(C >> 4) << hexdigit(C & 0x0F);
97 }
98}
99
Chris Lattner6ab910b2008-08-19 04:36:02 +0000100enum PrefixType {
101 GlobalPrefix,
102 LabelPrefix,
Daniel Dunbarcad35802008-10-14 23:28:09 +0000103 LocalPrefix,
104 NoPrefix
Chris Lattner6ab910b2008-08-19 04:36:02 +0000105};
106
107/// PrintLLVMName - Turn the specified name into an 'LLVM name', which is either
108/// prefixed with % (if the string only contains simple characters) or is
109/// surrounded with ""'s (if it has special chars in it). Print it out.
Benjamin Kramer38e59892010-07-14 22:38:02 +0000110static void PrintLLVMName(raw_ostream &OS, StringRef Name, PrefixType Prefix) {
Jay Foad61717b32011-04-24 14:30:00 +0000111 assert(!Name.empty() && "Cannot get empty name!");
Chris Lattner6ab910b2008-08-19 04:36:02 +0000112 switch (Prefix) {
Daniel Dunbarcad35802008-10-14 23:28:09 +0000113 case NoPrefix: break;
Chris Lattner52b26de2008-08-19 05:16:28 +0000114 case GlobalPrefix: OS << '@'; break;
115 case LabelPrefix: break;
116 case LocalPrefix: OS << '%'; break;
Nick Lewycky04234832009-03-19 06:31:22 +0000117 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000118
Chris Lattner6ab910b2008-08-19 04:36:02 +0000119 // Scan the name to see if it needs quotes first.
Guy Benyei87d0b9e2013-02-12 21:21:59 +0000120 bool NeedsQuotes = isdigit(static_cast<unsigned char>(Name[0]));
Chris Lattner6ab910b2008-08-19 04:36:02 +0000121 if (!NeedsQuotes) {
Daniel Dunbar03d76512009-07-25 23:55:21 +0000122 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
Aaron Ballman09dab822012-07-16 16:18:18 +0000123 // By making this unsigned, the value passed in to isalnum will always be
124 // in the range 0-255. This is important when building with MSVC because
125 // its implementation will assert. This situation can arise when dealing
126 // with UTF-8 multibyte characters.
127 unsigned char C = Name[i];
Guy Benyei87d0b9e2013-02-12 21:21:59 +0000128 if (!isalnum(static_cast<unsigned char>(C)) && C != '-' && C != '.' &&
129 C != '_') {
Chris Lattner6ab910b2008-08-19 04:36:02 +0000130 NeedsQuotes = true;
131 break;
132 }
133 }
134 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000135
Chris Lattner6ab910b2008-08-19 04:36:02 +0000136 // If we didn't need any quotes, just write out the name in one blast.
137 if (!NeedsQuotes) {
Daniel Dunbar03d76512009-07-25 23:55:21 +0000138 OS << Name;
Chris Lattner6ab910b2008-08-19 04:36:02 +0000139 return;
140 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000141
Chris Lattner6ab910b2008-08-19 04:36:02 +0000142 // Okay, we need quotes. Output the quotes and escape any scary characters as
143 // needed.
144 OS << '"';
Daniel Dunbar03d76512009-07-25 23:55:21 +0000145 PrintEscapedString(Name, OS);
Chris Lattner6ab910b2008-08-19 04:36:02 +0000146 OS << '"';
147}
148
149/// PrintLLVMName - Turn the specified name into an 'LLVM name', which is either
150/// prefixed with % (if the string only contains simple characters) or is
151/// surrounded with ""'s (if it has special chars in it). Print it out.
Dan Gohman1220e102009-08-12 20:56:03 +0000152static void PrintLLVMName(raw_ostream &OS, const Value *V) {
Daniel Dunbara279bc32009-09-20 02:20:51 +0000153 PrintLLVMName(OS, V->getName(),
Chris Lattner6ab910b2008-08-19 04:36:02 +0000154 isa<GlobalValue>(V) ? GlobalPrefix : LocalPrefix);
155}
156
Chris Lattner9cc34462009-02-28 20:25:14 +0000157
Daniel Malead0fef322013-05-08 20:38:31 +0000158namespace llvm {
Chris Lattner1afcace2011-07-09 17:41:24 +0000159
160void TypePrinting::incorporateTypes(const Module &M) {
Bill Wendling573e9732012-08-03 00:30:35 +0000161 NamedTypes.run(M, false);
Andrew Trick18801ec2011-09-30 19:48:58 +0000162
Chris Lattner1afcace2011-07-09 17:41:24 +0000163 // The list of struct types we got back includes all the struct types, split
164 // the unnamed ones out to a numbering and remove the anonymous structs.
165 unsigned NextNumber = 0;
Andrew Trick18801ec2011-09-30 19:48:58 +0000166
Chris Lattner1afcace2011-07-09 17:41:24 +0000167 std::vector<StructType*>::iterator NextToUse = NamedTypes.begin(), I, E;
168 for (I = NamedTypes.begin(), E = NamedTypes.end(); I != E; ++I) {
169 StructType *STy = *I;
Andrew Trick18801ec2011-09-30 19:48:58 +0000170
Chris Lattner1afcace2011-07-09 17:41:24 +0000171 // Ignore anonymous types.
Chris Lattnerc4d0e9f2011-08-12 18:07:07 +0000172 if (STy->isLiteral())
Chris Lattner1afcace2011-07-09 17:41:24 +0000173 continue;
Andrew Trick18801ec2011-09-30 19:48:58 +0000174
Chris Lattner1afcace2011-07-09 17:41:24 +0000175 if (STy->getName().empty())
176 NumberedTypes[STy] = NextNumber++;
177 else
178 *NextToUse++ = STy;
179 }
Andrew Trick18801ec2011-09-30 19:48:58 +0000180
Chris Lattner1afcace2011-07-09 17:41:24 +0000181 NamedTypes.erase(NextToUse, NamedTypes.end());
182}
183
184
Chris Lattner534361e2009-02-28 20:49:40 +0000185/// CalcTypeName - Write the specified type to the specified raw_ostream, making
186/// use of type names or up references to shorten the type name where possible.
Chris Lattner1afcace2011-07-09 17:41:24 +0000187void TypePrinting::print(Type *Ty, raw_ostream &OS) {
Chris Lattner9cc34462009-02-28 20:25:14 +0000188 switch (Ty->getTypeID()) {
Chris Lattner30794262009-02-28 21:27:31 +0000189 case Type::VoidTyID: OS << "void"; break;
Dan Gohmance163392011-12-17 00:04:22 +0000190 case Type::HalfTyID: OS << "half"; break;
Chris Lattner30794262009-02-28 21:27:31 +0000191 case Type::FloatTyID: OS << "float"; break;
192 case Type::DoubleTyID: OS << "double"; break;
193 case Type::X86_FP80TyID: OS << "x86_fp80"; break;
194 case Type::FP128TyID: OS << "fp128"; break;
195 case Type::PPC_FP128TyID: OS << "ppc_fp128"; break;
196 case Type::LabelTyID: OS << "label"; break;
Nick Lewycky7a0370f2009-05-30 05:06:04 +0000197 case Type::MetadataTyID: OS << "metadata"; break;
Dale Johannesenbb811a22010-09-10 20:55:01 +0000198 case Type::X86_MMXTyID: OS << "x86_mmx"; break;
Chris Lattner583ffd82009-02-28 21:18:43 +0000199 case Type::IntegerTyID:
Chris Lattner30794262009-02-28 21:27:31 +0000200 OS << 'i' << cast<IntegerType>(Ty)->getBitWidth();
Chris Lattner1afcace2011-07-09 17:41:24 +0000201 return;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000202
Chris Lattner36942d72009-02-28 20:35:42 +0000203 case Type::FunctionTyID: {
Chris Lattner1afcace2011-07-09 17:41:24 +0000204 FunctionType *FTy = cast<FunctionType>(Ty);
205 print(FTy->getReturnType(), OS);
Chris Lattner30794262009-02-28 21:27:31 +0000206 OS << " (";
Chris Lattner36942d72009-02-28 20:35:42 +0000207 for (FunctionType::param_iterator I = FTy->param_begin(),
208 E = FTy->param_end(); I != E; ++I) {
209 if (I != FTy->param_begin())
Chris Lattner30794262009-02-28 21:27:31 +0000210 OS << ", ";
Chris Lattner1afcace2011-07-09 17:41:24 +0000211 print(*I, OS);
Chris Lattner9cc34462009-02-28 20:25:14 +0000212 }
Chris Lattner36942d72009-02-28 20:35:42 +0000213 if (FTy->isVarArg()) {
Chris Lattner30794262009-02-28 21:27:31 +0000214 if (FTy->getNumParams()) OS << ", ";
215 OS << "...";
Chris Lattner9cc34462009-02-28 20:25:14 +0000216 }
Chris Lattner30794262009-02-28 21:27:31 +0000217 OS << ')';
Chris Lattner1afcace2011-07-09 17:41:24 +0000218 return;
Chris Lattner36942d72009-02-28 20:35:42 +0000219 }
220 case Type::StructTyID: {
Chris Lattner1afcace2011-07-09 17:41:24 +0000221 StructType *STy = cast<StructType>(Ty);
Andrew Trick18801ec2011-09-30 19:48:58 +0000222
Chris Lattnerc4d0e9f2011-08-12 18:07:07 +0000223 if (STy->isLiteral())
Chris Lattner1afcace2011-07-09 17:41:24 +0000224 return printStructBody(STy, OS);
225
226 if (!STy->getName().empty())
227 return PrintLLVMName(OS, STy->getName(), LocalPrefix);
Andrew Trick18801ec2011-09-30 19:48:58 +0000228
Chris Lattner1afcace2011-07-09 17:41:24 +0000229 DenseMap<StructType*, unsigned>::iterator I = NumberedTypes.find(STy);
230 if (I != NumberedTypes.end())
231 OS << '%' << I->second;
232 else // Not enumerated, print the hex address.
Benjamin Kramer5a832642011-11-02 17:24:36 +0000233 OS << "%\"type " << STy << '\"';
Chris Lattner1afcace2011-07-09 17:41:24 +0000234 return;
Chris Lattner36942d72009-02-28 20:35:42 +0000235 }
236 case Type::PointerTyID: {
Chris Lattner1afcace2011-07-09 17:41:24 +0000237 PointerType *PTy = cast<PointerType>(Ty);
238 print(PTy->getElementType(), OS);
Chris Lattner36942d72009-02-28 20:35:42 +0000239 if (unsigned AddressSpace = PTy->getAddressSpace())
Chris Lattner30794262009-02-28 21:27:31 +0000240 OS << " addrspace(" << AddressSpace << ')';
241 OS << '*';
Chris Lattner1afcace2011-07-09 17:41:24 +0000242 return;
Chris Lattner36942d72009-02-28 20:35:42 +0000243 }
244 case Type::ArrayTyID: {
Chris Lattner1afcace2011-07-09 17:41:24 +0000245 ArrayType *ATy = cast<ArrayType>(Ty);
Chris Lattner30794262009-02-28 21:27:31 +0000246 OS << '[' << ATy->getNumElements() << " x ";
Chris Lattner1afcace2011-07-09 17:41:24 +0000247 print(ATy->getElementType(), OS);
Chris Lattner30794262009-02-28 21:27:31 +0000248 OS << ']';
Chris Lattner1afcace2011-07-09 17:41:24 +0000249 return;
Chris Lattner36942d72009-02-28 20:35:42 +0000250 }
251 case Type::VectorTyID: {
Chris Lattner1afcace2011-07-09 17:41:24 +0000252 VectorType *PTy = cast<VectorType>(Ty);
Chris Lattner30794262009-02-28 21:27:31 +0000253 OS << "<" << PTy->getNumElements() << " x ";
Chris Lattner1afcace2011-07-09 17:41:24 +0000254 print(PTy->getElementType(), OS);
Chris Lattner30794262009-02-28 21:27:31 +0000255 OS << '>';
Chris Lattner1afcace2011-07-09 17:41:24 +0000256 return;
Chris Lattner36942d72009-02-28 20:35:42 +0000257 }
Chris Lattner36942d72009-02-28 20:35:42 +0000258 default:
Chris Lattner30794262009-02-28 21:27:31 +0000259 OS << "<unrecognized-type>";
Chris Lattner1afcace2011-07-09 17:41:24 +0000260 return;
Chris Lattner9cc34462009-02-28 20:25:14 +0000261 }
Chris Lattner9cc34462009-02-28 20:25:14 +0000262}
263
Chris Lattner1afcace2011-07-09 17:41:24 +0000264void TypePrinting::printStructBody(StructType *STy, raw_ostream &OS) {
265 if (STy->isOpaque()) {
266 OS << "opaque";
267 return;
Chris Lattner9cc34462009-02-28 20:25:14 +0000268 }
Andrew Trick18801ec2011-09-30 19:48:58 +0000269
Chris Lattner1afcace2011-07-09 17:41:24 +0000270 if (STy->isPacked())
271 OS << '<';
Andrew Trick18801ec2011-09-30 19:48:58 +0000272
Chris Lattner1afcace2011-07-09 17:41:24 +0000273 if (STy->getNumElements() == 0) {
274 OS << "{}";
275 } else {
276 StructType::element_iterator I = STy->element_begin();
277 OS << "{ ";
278 print(*I++, OS);
279 for (StructType::element_iterator E = STy->element_end(); I != E; ++I) {
280 OS << ", ";
281 print(*I, OS);
Chris Lattner413fd232009-03-01 00:03:38 +0000282 }
Andrew Trick18801ec2011-09-30 19:48:58 +0000283
Chris Lattner1afcace2011-07-09 17:41:24 +0000284 OS << " }";
Chris Lattnere9fa33e2009-02-28 23:20:19 +0000285 }
Chris Lattner1afcace2011-07-09 17:41:24 +0000286 if (STy->isPacked())
287 OS << '>';
Chris Lattnere9fa33e2009-02-28 23:20:19 +0000288}
289
Chris Lattner6ab910b2008-08-19 04:36:02 +0000290//===----------------------------------------------------------------------===//
291// SlotTracker Class: Enumerate slot numbers for unnamed values
292//===----------------------------------------------------------------------===//
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000293/// This class provides computation of slot numbers for LLVM Assembly writing.
Chris Lattner45d4c732008-08-17 04:17:45 +0000294///
Chris Lattner0d9574a2008-08-19 04:26:57 +0000295class SlotTracker {
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000296public:
Devang Patel320671d2009-07-08 21:44:25 +0000297 /// ValueMap - A mapping of Values to slot numbers.
Chris Lattner92255072008-08-17 17:25:25 +0000298 typedef DenseMap<const Value*, unsigned> ValueMap;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000299
300private:
Devang Patel320671d2009-07-08 21:44:25 +0000301 /// TheModule - The module for which we are holding slot numbers.
Chris Lattner45d4c732008-08-17 04:17:45 +0000302 const Module* TheModule;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000303
Devang Patel320671d2009-07-08 21:44:25 +0000304 /// TheFunction - The function for which we are holding slot numbers.
Chris Lattner45d4c732008-08-17 04:17:45 +0000305 const Function* TheFunction;
306 bool FunctionProcessed;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000307
Jay Foad7f0ce342011-07-11 07:28:49 +0000308 /// mMap - The slot map for the module level data.
Chris Lattner45d4c732008-08-17 04:17:45 +0000309 ValueMap mMap;
310 unsigned mNext;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000311
Jay Foad7f0ce342011-07-11 07:28:49 +0000312 /// fMap - The slot map for the function level data.
Chris Lattner45d4c732008-08-17 04:17:45 +0000313 ValueMap fMap;
314 unsigned fNext;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000315
Devang Patel320671d2009-07-08 21:44:25 +0000316 /// mdnMap - Map for MDNodes.
Chris Lattner307c9892009-12-31 02:20:11 +0000317 DenseMap<const MDNode*, unsigned> mdnMap;
Devang Patel320671d2009-07-08 21:44:25 +0000318 unsigned mdnNext;
Bill Wendlingb29ce262013-02-11 08:43:33 +0000319
320 /// asMap - The slot map for attribute sets.
321 DenseMap<AttributeSet, unsigned> asMap;
322 unsigned asNext;
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000323public:
Chris Lattner45d4c732008-08-17 04:17:45 +0000324 /// Construct from a module
Chris Lattner0d9574a2008-08-19 04:26:57 +0000325 explicit SlotTracker(const Module *M);
Chris Lattner45d4c732008-08-17 04:17:45 +0000326 /// Construct from a function, starting out in incorp state.
Chris Lattner0d9574a2008-08-19 04:26:57 +0000327 explicit SlotTracker(const Function *F);
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000328
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000329 /// Return the slot number of the specified value in it's type
Chris Lattner0d9574a2008-08-19 04:26:57 +0000330 /// plane. If something is not in the SlotTracker, return -1.
Chris Lattner22379bc2007-01-11 03:54:27 +0000331 int getLocalSlot(const Value *V);
332 int getGlobalSlot(const GlobalValue *V);
Devang Patel320671d2009-07-08 21:44:25 +0000333 int getMetadataSlot(const MDNode *N);
Bill Wendlingb29ce262013-02-11 08:43:33 +0000334 int getAttributeGroupSlot(AttributeSet AS);
Reid Spencerfc621e22004-06-09 15:26:53 +0000335
Misha Brukmanfd939082005-04-21 23:48:37 +0000336 /// If you'd like to deal with a function instead of just a module, use
Chris Lattner0d9574a2008-08-19 04:26:57 +0000337 /// this method to get its data into the SlotTracker.
Misha Brukmanfd939082005-04-21 23:48:37 +0000338 void incorporateFunction(const Function *F) {
339 TheFunction = F;
Reid Spencer28531c72004-08-16 07:46:33 +0000340 FunctionProcessed = false;
341 }
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000342
Misha Brukmanfd939082005-04-21 23:48:37 +0000343 /// After calling incorporateFunction, use this method to remove the
Chris Lattner0d9574a2008-08-19 04:26:57 +0000344 /// most recently incorporated function from the SlotTracker. This
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000345 /// will reset the state of the machine back to just the module contents.
346 void purgeFunction();
347
Devang Patel320671d2009-07-08 21:44:25 +0000348 /// MDNode map iterators.
Chris Lattner307c9892009-12-31 02:20:11 +0000349 typedef DenseMap<const MDNode*, unsigned>::iterator mdn_iterator;
350 mdn_iterator mdn_begin() { return mdnMap.begin(); }
351 mdn_iterator mdn_end() { return mdnMap.end(); }
352 unsigned mdn_size() const { return mdnMap.size(); }
353 bool mdn_empty() const { return mdnMap.empty(); }
Devang Patel320671d2009-07-08 21:44:25 +0000354
Bill Wendlingb29ce262013-02-11 08:43:33 +0000355 /// AttributeSet map iterators.
356 typedef DenseMap<AttributeSet, unsigned>::iterator as_iterator;
357 as_iterator as_begin() { return asMap.begin(); }
358 as_iterator as_end() { return asMap.end(); }
359 unsigned as_size() const { return asMap.size(); }
360 bool as_empty() const { return asMap.empty(); }
361
Reid Spencerb03de0c2004-05-26 21:56:09 +0000362 /// This function does the actual initialization.
363 inline void initialize();
364
Devang Patel320671d2009-07-08 21:44:25 +0000365 // Implementation Details
366private:
Chris Lattner9446bbe2007-01-09 07:55:49 +0000367 /// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
368 void CreateModuleSlot(const GlobalValue *V);
Devang Patel320671d2009-07-08 21:44:25 +0000369
370 /// CreateMetadataSlot - Insert the specified MDNode* into the slot table.
371 void CreateMetadataSlot(const MDNode *N);
372
Chris Lattner9446bbe2007-01-09 07:55:49 +0000373 /// CreateFunctionSlot - Insert the specified Value* into the slot table.
374 void CreateFunctionSlot(const Value *V);
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000375
Bill Wendlingb29ce262013-02-11 08:43:33 +0000376 /// \brief Insert the specified AttributeSet into the slot table.
377 void CreateAttributeSetSlot(AttributeSet AS);
378
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000379 /// Add all of the module level global variables (and their initializers)
380 /// and function declarations, but not the contents of those functions.
381 void processModule();
382
Devang Patel320671d2009-07-08 21:44:25 +0000383 /// Add all of the functions arguments, basic blocks, and instructions.
Reid Spencerb03de0c2004-05-26 21:56:09 +0000384 void processFunction();
385
Craig Topper86a1c322012-09-15 17:09:36 +0000386 SlotTracker(const SlotTracker &) LLVM_DELETED_FUNCTION;
387 void operator=(const SlotTracker &) LLVM_DELETED_FUNCTION;
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000388};
389
Daniel Malead0fef322013-05-08 20:38:31 +0000390SlotTracker *createSlotTracker(const Module *M) {
391 return new SlotTracker(M);
392}
Chris Lattner6ab910b2008-08-19 04:36:02 +0000393
394static SlotTracker *createSlotTracker(const Value *V) {
395 if (const Argument *FA = dyn_cast<Argument>(V))
396 return new SlotTracker(FA->getParent());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000397
Chris Lattner6ab910b2008-08-19 04:36:02 +0000398 if (const Instruction *I = dyn_cast<Instruction>(V))
Andrew Trick62e05902011-09-30 19:50:40 +0000399 if (I->getParent())
400 return new SlotTracker(I->getParent()->getParent());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000401
Chris Lattner6ab910b2008-08-19 04:36:02 +0000402 if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
403 return new SlotTracker(BB->getParent());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000404
Chris Lattner6ab910b2008-08-19 04:36:02 +0000405 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
406 return new SlotTracker(GV->getParent());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000407
Chris Lattner6ab910b2008-08-19 04:36:02 +0000408 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V))
Daniel Dunbara279bc32009-09-20 02:20:51 +0000409 return new SlotTracker(GA->getParent());
410
Chris Lattner6ab910b2008-08-19 04:36:02 +0000411 if (const Function *Func = dyn_cast<Function>(V))
412 return new SlotTracker(Func);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000413
Dan Gohman3bdfbf52010-07-20 23:55:01 +0000414 if (const MDNode *MD = dyn_cast<MDNode>(V)) {
415 if (!MD->isFunctionLocal())
416 return new SlotTracker(MD->getFunction());
417
Dale Johannesen5f72a5e2010-01-13 00:00:24 +0000418 return new SlotTracker((Function *)0);
Dan Gohman3bdfbf52010-07-20 23:55:01 +0000419 }
Dale Johannesen5f72a5e2010-01-13 00:00:24 +0000420
Chris Lattner6ab910b2008-08-19 04:36:02 +0000421 return 0;
422}
423
424#if 0
David Greened865e022010-01-05 01:29:26 +0000425#define ST_DEBUG(X) dbgs() << X
Chris Lattner6ab910b2008-08-19 04:36:02 +0000426#else
Chris Lattner24233032008-08-19 04:47:09 +0000427#define ST_DEBUG(X)
Chris Lattner6ab910b2008-08-19 04:36:02 +0000428#endif
429
430// Module level constructor. Causes the contents of the Module (sans functions)
431// to be added to the slot table.
432SlotTracker::SlotTracker(const Module *M)
Andrew Trick18801ec2011-09-30 19:48:58 +0000433 : TheModule(M), TheFunction(0), FunctionProcessed(false),
Bill Wendlingb29ce262013-02-11 08:43:33 +0000434 mNext(0), fNext(0), mdnNext(0), asNext(0) {
Chris Lattner6ab910b2008-08-19 04:36:02 +0000435}
436
437// Function level constructor. Causes the contents of the Module and the one
438// function provided to be added to the slot table.
439SlotTracker::SlotTracker(const Function *F)
Chris Lattnercfb5a202008-08-19 05:06:27 +0000440 : TheModule(F ? F->getParent() : 0), TheFunction(F), FunctionProcessed(false),
Bill Wendlingb29ce262013-02-11 08:43:33 +0000441 mNext(0), fNext(0), mdnNext(0), asNext(0) {
Chris Lattner6ab910b2008-08-19 04:36:02 +0000442}
443
444inline void SlotTracker::initialize() {
445 if (TheModule) {
446 processModule();
447 TheModule = 0; ///< Prevent re-processing next time we're called.
448 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000449
Chris Lattner6ab910b2008-08-19 04:36:02 +0000450 if (TheFunction && !FunctionProcessed)
451 processFunction();
452}
453
454// Iterate through all the global variables, functions, and global
455// variable initializers and create slots for them.
456void SlotTracker::processModule() {
Chris Lattner24233032008-08-19 04:47:09 +0000457 ST_DEBUG("begin processModule!\n");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000458
Chris Lattner6ab910b2008-08-19 04:36:02 +0000459 // Add all of the unnamed global variables to the value table.
460 for (Module::const_global_iterator I = TheModule->global_begin(),
Devang Patel320671d2009-07-08 21:44:25 +0000461 E = TheModule->global_end(); I != E; ++I) {
Daniel Dunbara279bc32009-09-20 02:20:51 +0000462 if (!I->hasName())
Chris Lattner6ab910b2008-08-19 04:36:02 +0000463 CreateModuleSlot(I);
Devang Patel320671d2009-07-08 21:44:25 +0000464 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000465
Devang Patel37c4a2d2009-07-29 22:04:47 +0000466 // Add metadata used by named metadata.
Daniel Dunbara279bc32009-09-20 02:20:51 +0000467 for (Module::const_named_metadata_iterator
Devang Patel37c4a2d2009-07-29 22:04:47 +0000468 I = TheModule->named_metadata_begin(),
469 E = TheModule->named_metadata_end(); I != E; ++I) {
470 const NamedMDNode *NMD = I;
Dan Gohman872814a2010-07-21 18:54:18 +0000471 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
472 CreateMetadataSlot(NMD->getOperand(i));
Devang Patel37c4a2d2009-07-29 22:04:47 +0000473 }
474
Chris Lattner6ab910b2008-08-19 04:36:02 +0000475 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
Bill Wendlingb29ce262013-02-11 08:43:33 +0000476 I != E; ++I) {
Chris Lattner6ab910b2008-08-19 04:36:02 +0000477 if (!I->hasName())
Bill Wendlingb29ce262013-02-11 08:43:33 +0000478 // Add all the unnamed functions to the table.
Chris Lattner6ab910b2008-08-19 04:36:02 +0000479 CreateModuleSlot(I);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000480
Bill Wendlingb29ce262013-02-11 08:43:33 +0000481 // Add all the function attributes to the table.
Bill Wendling7ab6c762013-02-20 07:21:42 +0000482 // FIXME: Add attributes of other objects?
Bill Wendlingb29ce262013-02-11 08:43:33 +0000483 AttributeSet FnAttrs = I->getAttributes().getFnAttributes();
484 if (FnAttrs.hasAttributes(AttributeSet::FunctionIndex))
485 CreateAttributeSetSlot(FnAttrs);
486 }
487
Chris Lattner24233032008-08-19 04:47:09 +0000488 ST_DEBUG("end processModule!\n");
Chris Lattner6ab910b2008-08-19 04:36:02 +0000489}
490
Chris Lattner6ab910b2008-08-19 04:36:02 +0000491// Process the arguments, basic blocks, and instructions of a function.
492void SlotTracker::processFunction() {
Chris Lattner24233032008-08-19 04:47:09 +0000493 ST_DEBUG("begin processFunction!\n");
Chris Lattner6ab910b2008-08-19 04:36:02 +0000494 fNext = 0;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000495
Chris Lattner6ab910b2008-08-19 04:36:02 +0000496 // Add all the function arguments with no names.
497 for(Function::const_arg_iterator AI = TheFunction->arg_begin(),
498 AE = TheFunction->arg_end(); AI != AE; ++AI)
499 if (!AI->hasName())
500 CreateFunctionSlot(AI);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000501
Chris Lattner24233032008-08-19 04:47:09 +0000502 ST_DEBUG("Inserting Instructions:\n");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000503
Chris Lattner6e6b1802009-12-31 02:13:35 +0000504 SmallVector<std::pair<unsigned, MDNode*>, 4> MDForInst;
Devang Patel43215782009-09-16 20:21:17 +0000505
Chris Lattner6ab910b2008-08-19 04:36:02 +0000506 // Add all of the basic blocks and instructions with no names.
507 for (Function::const_iterator BB = TheFunction->begin(),
508 E = TheFunction->end(); BB != E; ++BB) {
509 if (!BB->hasName())
510 CreateFunctionSlot(BB);
Andrew Trick18801ec2011-09-30 19:48:58 +0000511
Daniel Dunbara279bc32009-09-20 02:20:51 +0000512 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E;
Devang Patel320671d2009-07-08 21:44:25 +0000513 ++I) {
Chris Lattner3990b122009-12-28 23:41:32 +0000514 if (!I->getType()->isVoidTy() && !I->hasName())
Chris Lattner6ab910b2008-08-19 04:36:02 +0000515 CreateFunctionSlot(I);
Andrew Trick18801ec2011-09-30 19:48:58 +0000516
Chris Lattnerfd450c02010-05-10 20:53:17 +0000517 // Intrinsics can directly use metadata. We allow direct calls to any
518 // llvm.foo function here, because the target may not be linked into the
519 // optimizer.
520 if (const CallInst *CI = dyn_cast<CallInst>(I)) {
521 if (Function *F = CI->getCalledFunction())
522 if (F->getName().startswith("llvm."))
523 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
524 if (MDNode *N = dyn_cast_or_null<MDNode>(I->getOperand(i)))
525 CreateMetadataSlot(N);
Bill Wendling2bb471f2013-02-20 00:04:41 +0000526
Bill Wendling351b7a12013-02-22 09:09:42 +0000527 // Add all the call attributes to the table.
528 AttributeSet Attrs = CI->getAttributes().getFnAttributes();
529 if (Attrs.hasAttributes(AttributeSet::FunctionIndex))
530 CreateAttributeSetSlot(Attrs);
531 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(I)) {
532 // Add all the call attributes to the table.
533 AttributeSet Attrs = II->getAttributes().getFnAttributes();
534 if (Attrs.hasAttributes(AttributeSet::FunctionIndex))
535 CreateAttributeSetSlot(Attrs);
Chris Lattnerfd450c02010-05-10 20:53:17 +0000536 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000537
Devang Patel43215782009-09-16 20:21:17 +0000538 // Process metadata attached with this instruction.
Chris Lattner3990b122009-12-28 23:41:32 +0000539 I->getAllMetadata(MDForInst);
540 for (unsigned i = 0, e = MDForInst.size(); i != e; ++i)
541 CreateMetadataSlot(MDForInst[i].second);
Chris Lattner6e6b1802009-12-31 02:13:35 +0000542 MDForInst.clear();
Devang Patel320671d2009-07-08 21:44:25 +0000543 }
Chris Lattner6ab910b2008-08-19 04:36:02 +0000544 }
Devang Patel43215782009-09-16 20:21:17 +0000545
Chris Lattner6ab910b2008-08-19 04:36:02 +0000546 FunctionProcessed = true;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000547
Chris Lattner24233032008-08-19 04:47:09 +0000548 ST_DEBUG("end processFunction!\n");
Chris Lattner6ab910b2008-08-19 04:36:02 +0000549}
550
551/// Clean up after incorporating a function. This is the only way to get out of
552/// the function incorporation state that affects get*Slot/Create*Slot. Function
553/// incorporation state is indicated by TheFunction != 0.
554void SlotTracker::purgeFunction() {
Chris Lattner24233032008-08-19 04:47:09 +0000555 ST_DEBUG("begin purgeFunction!\n");
Chris Lattner6ab910b2008-08-19 04:36:02 +0000556 fMap.clear(); // Simply discard the function level map
557 TheFunction = 0;
558 FunctionProcessed = false;
Chris Lattner24233032008-08-19 04:47:09 +0000559 ST_DEBUG("end purgeFunction!\n");
Chris Lattner6ab910b2008-08-19 04:36:02 +0000560}
561
562/// getGlobalSlot - Get the slot number of a global value.
563int SlotTracker::getGlobalSlot(const GlobalValue *V) {
564 // Check for uninitialized state and do lazy initialization.
565 initialize();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000566
Jay Foad7f0ce342011-07-11 07:28:49 +0000567 // Find the value in the module map
Chris Lattner6ab910b2008-08-19 04:36:02 +0000568 ValueMap::iterator MI = mMap.find(V);
Dan Gohmanaeaf2452008-10-01 19:58:59 +0000569 return MI == mMap.end() ? -1 : (int)MI->second;
Chris Lattner6ab910b2008-08-19 04:36:02 +0000570}
571
Chris Lattner307c9892009-12-31 02:20:11 +0000572/// getMetadataSlot - Get the slot number of a MDNode.
Devang Patel320671d2009-07-08 21:44:25 +0000573int SlotTracker::getMetadataSlot(const MDNode *N) {
574 // Check for uninitialized state and do lazy initialization.
575 initialize();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000576
Jay Foad7f0ce342011-07-11 07:28:49 +0000577 // Find the MDNode in the module map
Chris Lattner307c9892009-12-31 02:20:11 +0000578 mdn_iterator MI = mdnMap.find(N);
Devang Patel320671d2009-07-08 21:44:25 +0000579 return MI == mdnMap.end() ? -1 : (int)MI->second;
580}
581
Chris Lattner6ab910b2008-08-19 04:36:02 +0000582
583/// getLocalSlot - Get the slot number for a value that is local to a function.
584int SlotTracker::getLocalSlot(const Value *V) {
585 assert(!isa<Constant>(V) && "Can't get a constant or global slot with this!");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000586
Chris Lattner6ab910b2008-08-19 04:36:02 +0000587 // Check for uninitialized state and do lazy initialization.
588 initialize();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000589
Chris Lattner6ab910b2008-08-19 04:36:02 +0000590 ValueMap::iterator FI = fMap.find(V);
Dan Gohmanaeaf2452008-10-01 19:58:59 +0000591 return FI == fMap.end() ? -1 : (int)FI->second;
Chris Lattner6ab910b2008-08-19 04:36:02 +0000592}
593
Bill Wendlingb29ce262013-02-11 08:43:33 +0000594int SlotTracker::getAttributeGroupSlot(AttributeSet AS) {
595 // Check for uninitialized state and do lazy initialization.
596 initialize();
597
598 // Find the AttributeSet in the module map.
599 as_iterator AI = asMap.find(AS);
600 return AI == asMap.end() ? -1 : (int)AI->second;
601}
Chris Lattner6ab910b2008-08-19 04:36:02 +0000602
603/// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
604void SlotTracker::CreateModuleSlot(const GlobalValue *V) {
605 assert(V && "Can't insert a null Value into SlotTracker!");
Chris Lattner4ee93c42009-12-29 07:25:48 +0000606 assert(!V->getType()->isVoidTy() && "Doesn't need a slot!");
Chris Lattner6ab910b2008-08-19 04:36:02 +0000607 assert(!V->hasName() && "Doesn't need a slot!");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000608
Chris Lattner6ab910b2008-08-19 04:36:02 +0000609 unsigned DestSlot = mNext++;
610 mMap[V] = DestSlot;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000611
Chris Lattner24233032008-08-19 04:47:09 +0000612 ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" <<
Chris Lattner6ab910b2008-08-19 04:36:02 +0000613 DestSlot << " [");
614 // G = Global, F = Function, A = Alias, o = other
Chris Lattner24233032008-08-19 04:47:09 +0000615 ST_DEBUG((isa<GlobalVariable>(V) ? 'G' :
Chris Lattner6ab910b2008-08-19 04:36:02 +0000616 (isa<Function>(V) ? 'F' :
617 (isa<GlobalAlias>(V) ? 'A' : 'o'))) << "]\n");
618}
619
Chris Lattner6ab910b2008-08-19 04:36:02 +0000620/// CreateSlot - Create a new slot for the specified value if it has no name.
621void SlotTracker::CreateFunctionSlot(const Value *V) {
Chris Lattner4ee93c42009-12-29 07:25:48 +0000622 assert(!V->getType()->isVoidTy() && !V->hasName() && "Doesn't need a slot!");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000623
Chris Lattner6ab910b2008-08-19 04:36:02 +0000624 unsigned DestSlot = fNext++;
625 fMap[V] = DestSlot;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000626
Chris Lattner6ab910b2008-08-19 04:36:02 +0000627 // G = Global, F = Function, o = other
Chris Lattner24233032008-08-19 04:47:09 +0000628 ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" <<
Chris Lattner6ab910b2008-08-19 04:36:02 +0000629 DestSlot << " [o]\n");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000630}
Chris Lattner6ab910b2008-08-19 04:36:02 +0000631
Devang Patel320671d2009-07-08 21:44:25 +0000632/// CreateModuleSlot - Insert the specified MDNode* into the slot table.
633void SlotTracker::CreateMetadataSlot(const MDNode *N) {
634 assert(N && "Can't insert a null Value into SlotTracker!");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000635
Chris Lattner2b4b1e22009-12-31 02:27:30 +0000636 // Don't insert if N is a function-local metadata, these are always printed
637 // inline.
Dan Gohman3bdfbf52010-07-20 23:55:01 +0000638 if (!N->isFunctionLocal()) {
639 mdn_iterator I = mdnMap.find(N);
640 if (I != mdnMap.end())
641 return;
Victor Hernandezff7707e2009-12-04 20:07:10 +0000642
Dan Gohman3bdfbf52010-07-20 23:55:01 +0000643 unsigned DestSlot = mdnNext++;
644 mdnMap[N] = DestSlot;
645 }
Devang Patel320671d2009-07-08 21:44:25 +0000646
Chris Lattner2b4b1e22009-12-31 02:27:30 +0000647 // Recursively add any MDNodes referenced by operands.
648 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
649 if (const MDNode *Op = dyn_cast_or_null<MDNode>(N->getOperand(i)))
650 CreateMetadataSlot(Op);
Devang Patel320671d2009-07-08 21:44:25 +0000651}
Chris Lattner6ab910b2008-08-19 04:36:02 +0000652
Bill Wendlingb29ce262013-02-11 08:43:33 +0000653void SlotTracker::CreateAttributeSetSlot(AttributeSet AS) {
654 assert(AS.hasAttributes(AttributeSet::FunctionIndex) &&
655 "Doesn't need a slot!");
656
657 as_iterator I = asMap.find(AS);
658 if (I != asMap.end())
659 return;
660
661 unsigned DestSlot = asNext++;
662 asMap[AS] = DestSlot;
663}
664
Chris Lattner6ab910b2008-08-19 04:36:02 +0000665//===----------------------------------------------------------------------===//
666// AsmWriter Implementation
667//===----------------------------------------------------------------------===//
Chris Lattnerf082b802002-07-23 18:07:49 +0000668
Dan Gohman1220e102009-08-12 20:56:03 +0000669static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
Dan Gohmand6c0f652009-08-13 15:27:57 +0000670 TypePrinting *TypePrinter,
Dan Gohman3bdfbf52010-07-20 23:55:01 +0000671 SlotTracker *Machine,
672 const Module *Context);
Reid Spencer0e25e1c2004-07-04 11:50:43 +0000673
Chris Lattnerc97536e2008-08-17 04:40:13 +0000674
Chris Lattner207b5bc2001-10-29 16:37:48 +0000675
Chris Lattner82c4bc72006-12-06 06:40:49 +0000676static const char *getPredicateText(unsigned predicate) {
Reid Spencer81dfeb32006-12-04 05:19:18 +0000677 const char * pred = "unknown";
678 switch (predicate) {
Chris Lattner6e6b1802009-12-31 02:13:35 +0000679 case FCmpInst::FCMP_FALSE: pred = "false"; break;
680 case FCmpInst::FCMP_OEQ: pred = "oeq"; break;
681 case FCmpInst::FCMP_OGT: pred = "ogt"; break;
682 case FCmpInst::FCMP_OGE: pred = "oge"; break;
683 case FCmpInst::FCMP_OLT: pred = "olt"; break;
684 case FCmpInst::FCMP_OLE: pred = "ole"; break;
685 case FCmpInst::FCMP_ONE: pred = "one"; break;
686 case FCmpInst::FCMP_ORD: pred = "ord"; break;
687 case FCmpInst::FCMP_UNO: pred = "uno"; break;
688 case FCmpInst::FCMP_UEQ: pred = "ueq"; break;
689 case FCmpInst::FCMP_UGT: pred = "ugt"; break;
690 case FCmpInst::FCMP_UGE: pred = "uge"; break;
691 case FCmpInst::FCMP_ULT: pred = "ult"; break;
692 case FCmpInst::FCMP_ULE: pred = "ule"; break;
693 case FCmpInst::FCMP_UNE: pred = "une"; break;
694 case FCmpInst::FCMP_TRUE: pred = "true"; break;
695 case ICmpInst::ICMP_EQ: pred = "eq"; break;
696 case ICmpInst::ICMP_NE: pred = "ne"; break;
697 case ICmpInst::ICMP_SGT: pred = "sgt"; break;
698 case ICmpInst::ICMP_SGE: pred = "sge"; break;
699 case ICmpInst::ICMP_SLT: pred = "slt"; break;
700 case ICmpInst::ICMP_SLE: pred = "sle"; break;
701 case ICmpInst::ICMP_UGT: pred = "ugt"; break;
702 case ICmpInst::ICMP_UGE: pred = "uge"; break;
703 case ICmpInst::ICMP_ULT: pred = "ult"; break;
704 case ICmpInst::ICMP_ULE: pred = "ule"; break;
Reid Spencer81dfeb32006-12-04 05:19:18 +0000705 }
706 return pred;
707}
708
Eli Friedmanff030482011-07-28 21:48:00 +0000709static void writeAtomicRMWOperation(raw_ostream &Out,
710 AtomicRMWInst::BinOp Op) {
711 switch (Op) {
712 default: Out << " <unknown operation " << Op << ">"; break;
713 case AtomicRMWInst::Xchg: Out << " xchg"; break;
714 case AtomicRMWInst::Add: Out << " add"; break;
715 case AtomicRMWInst::Sub: Out << " sub"; break;
716 case AtomicRMWInst::And: Out << " and"; break;
717 case AtomicRMWInst::Nand: Out << " nand"; break;
718 case AtomicRMWInst::Or: Out << " or"; break;
719 case AtomicRMWInst::Xor: Out << " xor"; break;
720 case AtomicRMWInst::Max: Out << " max"; break;
721 case AtomicRMWInst::Min: Out << " min"; break;
722 case AtomicRMWInst::UMax: Out << " umax"; break;
723 case AtomicRMWInst::UMin: Out << " umin"; break;
724 }
725}
Devang Patel320671d2009-07-08 21:44:25 +0000726
Dan Gohman1220e102009-08-12 20:56:03 +0000727static void WriteOptimizationInfo(raw_ostream &Out, const User *U) {
Michael Ilseman15c13d32012-11-27 00:42:44 +0000728 if (const FPMathOperator *FPO = dyn_cast<const FPMathOperator>(U)) {
729 // Unsafe algebra implies all the others, no need to write them all out
730 if (FPO->hasUnsafeAlgebra())
731 Out << " fast";
732 else {
733 if (FPO->hasNoNaNs())
734 Out << " nnan";
735 if (FPO->hasNoInfs())
736 Out << " ninf";
737 if (FPO->hasNoSignedZeros())
738 Out << " nsz";
739 if (FPO->hasAllowReciprocal())
740 Out << " arcp";
741 }
742 }
743
Dan Gohman1224c382009-07-20 21:19:07 +0000744 if (const OverflowingBinaryOperator *OBO =
745 dyn_cast<OverflowingBinaryOperator>(U)) {
Dan Gohman5078f842009-08-20 17:11:38 +0000746 if (OBO->hasNoUnsignedWrap())
Dan Gohman59858cf2009-07-27 16:11:46 +0000747 Out << " nuw";
Dan Gohman5078f842009-08-20 17:11:38 +0000748 if (OBO->hasNoSignedWrap())
Dan Gohman59858cf2009-07-27 16:11:46 +0000749 Out << " nsw";
Chris Lattner35bda892011-02-06 21:44:57 +0000750 } else if (const PossiblyExactOperator *Div =
751 dyn_cast<PossiblyExactOperator>(U)) {
Dan Gohman1224c382009-07-20 21:19:07 +0000752 if (Div->isExact())
Dan Gohman59858cf2009-07-27 16:11:46 +0000753 Out << " exact";
Dan Gohmandd8004d2009-07-27 21:53:46 +0000754 } else if (const GEPOperator *GEP = dyn_cast<GEPOperator>(U)) {
755 if (GEP->isInBounds())
756 Out << " inbounds";
Dan Gohman1224c382009-07-20 21:19:07 +0000757 }
758}
759
Dan Gohman40cf12f2010-07-14 20:57:55 +0000760static void WriteConstantInternal(raw_ostream &Out, const Constant *CV,
761 TypePrinting &TypePrinter,
Dan Gohman3bdfbf52010-07-20 23:55:01 +0000762 SlotTracker *Machine,
763 const Module *Context) {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +0000764 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000765 if (CI->getType()->isIntegerTy(1)) {
Reid Spencer579dca12007-01-12 04:24:46 +0000766 Out << (CI->getZExtValue() ? "true" : "false");
Chris Lattnerfad86b02008-08-17 07:19:36 +0000767 return;
768 }
769 Out << CI->getValue();
770 return;
771 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000772
Chris Lattnerfad86b02008-08-17 07:19:36 +0000773 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Tobias Grosser057beb82012-05-24 15:59:06 +0000774 if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEsingle ||
Dan Gohmance163392011-12-17 00:04:22 +0000775 &CFP->getValueAPF().getSemantics() == &APFloat::IEEEdouble) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000776 // We would like to output the FP constant value in exponential notation,
777 // but we cannot do this if doing so will lose precision. Check here to
778 // make sure that we only output it in exponential format if we can parse
779 // the value back and get the same value.
780 //
Dale Johannesen541ed9f2009-01-21 20:32:55 +0000781 bool ignored;
Dan Gohmance163392011-12-17 00:04:22 +0000782 bool isHalf = &CFP->getValueAPF().getSemantics()==&APFloat::IEEEhalf;
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000783 bool isDouble = &CFP->getValueAPF().getSemantics()==&APFloat::IEEEdouble;
NAKAMURA Takumi22bed5d2012-02-16 08:12:24 +0000784 bool isInf = CFP->getValueAPF().isInfinity();
785 bool isNaN = CFP->getValueAPF().isNaN();
786 if (!isHalf && !isInf && !isNaN) {
Dan Gohmance163392011-12-17 00:04:22 +0000787 double Val = isDouble ? CFP->getValueAPF().convertToDouble() :
788 CFP->getValueAPF().convertToFloat();
789 SmallString<128> StrVal;
790 raw_svector_ostream(StrVal) << Val;
Chris Lattner66e810b2002-04-18 18:53:13 +0000791
Dan Gohmance163392011-12-17 00:04:22 +0000792 // Check to make sure that the stringized number is not some string like
793 // "Inf" or NaN, that atof will accept, but the lexer will not. Check
794 // that the string matches the "[-+]?[0-9]" regex.
795 //
796 if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
797 ((StrVal[0] == '-' || StrVal[0] == '+') &&
798 (StrVal[1] >= '0' && StrVal[1] <= '9'))) {
799 // Reparse stringized version!
NAKAMURA Takumic8782a12012-02-16 04:19:15 +0000800 if (APFloat(APFloat::IEEEdouble, StrVal).convertToDouble() == Val) {
Dan Gohmance163392011-12-17 00:04:22 +0000801 Out << StrVal.str();
802 return;
803 }
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000804 }
Chris Lattner66e810b2002-04-18 18:53:13 +0000805 }
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000806 // Otherwise we could not reparse it to exactly the same value, so we must
Dale Johannesen541ed9f2009-01-21 20:32:55 +0000807 // output the string in hexadecimal format! Note that loading and storing
808 // floating point types changes the bits of NaNs on some hosts, notably
809 // x86, so we must not use these types.
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000810 assert(sizeof(double) == sizeof(uint64_t) &&
811 "assuming that double is 64 bits!");
Chris Lattnerc6a13462008-11-10 04:30:26 +0000812 char Buffer[40];
Dale Johannesen541ed9f2009-01-21 20:32:55 +0000813 APFloat apf = CFP->getValueAPF();
Dan Gohmance163392011-12-17 00:04:22 +0000814 // Halves and floats are represented in ASCII IR as double, convert.
Dale Johannesen541ed9f2009-01-21 20:32:55 +0000815 if (!isDouble)
Daniel Dunbara279bc32009-09-20 02:20:51 +0000816 apf.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven,
Dale Johannesen541ed9f2009-01-21 20:32:55 +0000817 &ignored);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000818 Out << "0x" <<
819 utohex_buffer(uint64_t(apf.bitcastToAPInt().getZExtValue()),
Dale Johannesen541ed9f2009-01-21 20:32:55 +0000820 Buffer+40);
Chris Lattnercfb5a202008-08-19 05:06:27 +0000821 return;
822 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000823
Tobias Grosser057beb82012-05-24 15:59:06 +0000824 // Either half, or some form of long double.
825 // These appear as a magic letter identifying the type, then a
826 // fixed number of hex digits.
Chris Lattnercfb5a202008-08-19 05:06:27 +0000827 Out << "0x";
Tobias Grosser057beb82012-05-24 15:59:06 +0000828 // Bit position, in the current word, of the next nibble to print.
829 int shiftcount;
830
Dale Johannesen1b25cb22009-03-23 21:16:53 +0000831 if (&CFP->getValueAPF().getSemantics() == &APFloat::x87DoubleExtended) {
Chris Lattnercfb5a202008-08-19 05:06:27 +0000832 Out << 'K';
Dale Johannesen1b25cb22009-03-23 21:16:53 +0000833 // api needed to prevent premature destruction
834 APInt api = CFP->getValueAPF().bitcastToAPInt();
835 const uint64_t* p = api.getRawData();
836 uint64_t word = p[1];
Tobias Grosser057beb82012-05-24 15:59:06 +0000837 shiftcount = 12;
Dale Johannesen1b25cb22009-03-23 21:16:53 +0000838 int width = api.getBitWidth();
839 for (int j=0; j<width; j+=4, shiftcount-=4) {
840 unsigned int nibble = (word>>shiftcount) & 15;
841 if (nibble < 10)
842 Out << (unsigned char)(nibble + '0');
843 else
844 Out << (unsigned char)(nibble - 10 + 'A');
845 if (shiftcount == 0 && j+4 < width) {
846 word = *p;
847 shiftcount = 64;
848 if (width-j-4 < 64)
849 shiftcount = width-j-4;
850 }
851 }
852 return;
Tobias Grosser057beb82012-05-24 15:59:06 +0000853 } else if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEquad) {
854 shiftcount = 60;
Chris Lattnercfb5a202008-08-19 05:06:27 +0000855 Out << 'L';
Tobias Grosser057beb82012-05-24 15:59:06 +0000856 } else if (&CFP->getValueAPF().getSemantics() == &APFloat::PPCDoubleDouble) {
857 shiftcount = 60;
Chris Lattnercfb5a202008-08-19 05:06:27 +0000858 Out << 'M';
Tobias Grosser057beb82012-05-24 15:59:06 +0000859 } else if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEhalf) {
860 shiftcount = 12;
861 Out << 'H';
862 } else
Torok Edwinc23197a2009-07-14 16:55:14 +0000863 llvm_unreachable("Unsupported floating point type");
Chris Lattnercfb5a202008-08-19 05:06:27 +0000864 // api needed to prevent premature destruction
Dale Johannesen7111b022008-10-09 18:53:47 +0000865 APInt api = CFP->getValueAPF().bitcastToAPInt();
Chris Lattnercfb5a202008-08-19 05:06:27 +0000866 const uint64_t* p = api.getRawData();
867 uint64_t word = *p;
Chris Lattnercfb5a202008-08-19 05:06:27 +0000868 int width = api.getBitWidth();
869 for (int j=0; j<width; j+=4, shiftcount-=4) {
870 unsigned int nibble = (word>>shiftcount) & 15;
871 if (nibble < 10)
872 Out << (unsigned char)(nibble + '0');
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000873 else
Chris Lattnercfb5a202008-08-19 05:06:27 +0000874 Out << (unsigned char)(nibble - 10 + 'A');
875 if (shiftcount == 0 && j+4 < width) {
876 word = *(++p);
877 shiftcount = 64;
878 if (width-j-4 < 64)
879 shiftcount = width-j-4;
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000880 }
881 }
Chris Lattnercfb5a202008-08-19 05:06:27 +0000882 return;
883 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000884
Chris Lattnercfb5a202008-08-19 05:06:27 +0000885 if (isa<ConstantAggregateZero>(CV)) {
Chris Lattnerde512b52004-02-15 05:55:15 +0000886 Out << "zeroinitializer";
Chris Lattnercfb5a202008-08-19 05:06:27 +0000887 return;
888 }
Andrew Trick18801ec2011-09-30 19:48:58 +0000889
Chris Lattner73050e12009-10-28 03:38:12 +0000890 if (const BlockAddress *BA = dyn_cast<BlockAddress>(CV)) {
891 Out << "blockaddress(";
Dan Gohman3bdfbf52010-07-20 23:55:01 +0000892 WriteAsOperandInternal(Out, BA->getFunction(), &TypePrinter, Machine,
893 Context);
Chris Lattner73050e12009-10-28 03:38:12 +0000894 Out << ", ";
Dan Gohman3bdfbf52010-07-20 23:55:01 +0000895 WriteAsOperandInternal(Out, BA->getBasicBlock(), &TypePrinter, Machine,
896 Context);
Chris Lattner73050e12009-10-28 03:38:12 +0000897 Out << ")";
898 return;
899 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000900
Chris Lattnercfb5a202008-08-19 05:06:27 +0000901 if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
Chris Lattner1afcace2011-07-09 17:41:24 +0000902 Type *ETy = CA->getType()->getElementType();
Chris Lattner18c7f802012-02-05 02:29:43 +0000903 Out << '[';
904 TypePrinter.print(ETy, Out);
905 Out << ' ';
906 WriteAsOperandInternal(Out, CA->getOperand(0),
907 &TypePrinter, Machine,
908 Context);
909 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
910 Out << ", ";
Chris Lattner8b10b692012-01-31 03:15:40 +0000911 TypePrinter.print(ETy, Out);
912 Out << ' ';
Chris Lattner18c7f802012-02-05 02:29:43 +0000913 WriteAsOperandInternal(Out, CA->getOperand(i), &TypePrinter, Machine,
Chris Lattner8b10b692012-01-31 03:15:40 +0000914 Context);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000915 }
Chris Lattner18c7f802012-02-05 02:29:43 +0000916 Out << ']';
Chris Lattnercfb5a202008-08-19 05:06:27 +0000917 return;
918 }
Michael Ilseman407a6162012-11-15 22:34:00 +0000919
Chris Lattnerd59ae902012-01-26 02:32:04 +0000920 if (const ConstantDataArray *CA = dyn_cast<ConstantDataArray>(CV)) {
921 // As a special case, print the array as a string if it is an array of
922 // i8 with ConstantInt values.
923 if (CA->isString()) {
924 Out << "c\"";
925 PrintEscapedString(CA->getAsString(), Out);
926 Out << '"';
927 return;
928 }
929
930 Type *ETy = CA->getType()->getElementType();
931 Out << '[';
Chris Lattner8b10b692012-01-31 03:15:40 +0000932 TypePrinter.print(ETy, Out);
933 Out << ' ';
934 WriteAsOperandInternal(Out, CA->getElementAsConstant(0),
935 &TypePrinter, Machine,
936 Context);
937 for (unsigned i = 1, e = CA->getNumElements(); i != e; ++i) {
938 Out << ", ";
Chris Lattnerd59ae902012-01-26 02:32:04 +0000939 TypePrinter.print(ETy, Out);
940 Out << ' ';
Chris Lattner8b10b692012-01-31 03:15:40 +0000941 WriteAsOperandInternal(Out, CA->getElementAsConstant(i), &TypePrinter,
942 Machine, Context);
Chris Lattnerd59ae902012-01-26 02:32:04 +0000943 }
Chris Lattner8b10b692012-01-31 03:15:40 +0000944 Out << ']';
Chris Lattnerd59ae902012-01-26 02:32:04 +0000945 return;
946 }
947
Daniel Dunbara279bc32009-09-20 02:20:51 +0000948
Chris Lattnercfb5a202008-08-19 05:06:27 +0000949 if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
Andrew Lenharth43f344a2007-01-08 18:21:30 +0000950 if (CS->getType()->isPacked())
951 Out << '<';
Misha Brukman40c732c2004-06-04 21:11:51 +0000952 Out << '{';
Jim Laskeya3f332b2006-02-25 12:27:03 +0000953 unsigned N = CS->getNumOperands();
954 if (N) {
Chris Lattner24233032008-08-19 04:47:09 +0000955 Out << ' ';
Chris Lattner0f7364b2009-02-28 21:26:53 +0000956 TypePrinter.print(CS->getOperand(0)->getType(), Out);
Dan Gohman8dae1382008-09-14 17:21:12 +0000957 Out << ' ';
Chris Lattner7a716ad2002-04-16 21:36:08 +0000958
Dan Gohman3bdfbf52010-07-20 23:55:01 +0000959 WriteAsOperandInternal(Out, CS->getOperand(0), &TypePrinter, Machine,
960 Context);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000961
Jim Laskeya3f332b2006-02-25 12:27:03 +0000962 for (unsigned i = 1; i < N; i++) {
Chris Lattner7a716ad2002-04-16 21:36:08 +0000963 Out << ", ";
Chris Lattner0f7364b2009-02-28 21:26:53 +0000964 TypePrinter.print(CS->getOperand(i)->getType(), Out);
Dan Gohman8dae1382008-09-14 17:21:12 +0000965 Out << ' ';
Chris Lattner7a716ad2002-04-16 21:36:08 +0000966
Dan Gohman3bdfbf52010-07-20 23:55:01 +0000967 WriteAsOperandInternal(Out, CS->getOperand(i), &TypePrinter, Machine,
968 Context);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000969 }
Dan Gohman8dae1382008-09-14 17:21:12 +0000970 Out << ' ';
Chris Lattner7a716ad2002-04-16 21:36:08 +0000971 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000972
Dan Gohman8dae1382008-09-14 17:21:12 +0000973 Out << '}';
Andrew Lenharth43f344a2007-01-08 18:21:30 +0000974 if (CS->getType()->isPacked())
975 Out << '>';
Chris Lattnercfb5a202008-08-19 05:06:27 +0000976 return;
977 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000978
Chris Lattnerd59ae902012-01-26 02:32:04 +0000979 if (isa<ConstantVector>(CV) || isa<ConstantDataVector>(CV)) {
980 Type *ETy = CV->getType()->getVectorElementType();
Dan Gohman7dfa07f2009-02-11 00:25:25 +0000981 Out << '<';
Chris Lattner0f7364b2009-02-28 21:26:53 +0000982 TypePrinter.print(ETy, Out);
Dan Gohman8dae1382008-09-14 17:21:12 +0000983 Out << ' ';
Chris Lattnerd59ae902012-01-26 02:32:04 +0000984 WriteAsOperandInternal(Out, CV->getAggregateElement(0U), &TypePrinter,
985 Machine, Context);
986 for (unsigned i = 1, e = CV->getType()->getVectorNumElements(); i != e;++i){
Chris Lattner4667b712008-08-19 05:26:17 +0000987 Out << ", ";
Chris Lattner0f7364b2009-02-28 21:26:53 +0000988 TypePrinter.print(ETy, Out);
Dan Gohman8dae1382008-09-14 17:21:12 +0000989 Out << ' ';
Chris Lattnerd59ae902012-01-26 02:32:04 +0000990 WriteAsOperandInternal(Out, CV->getAggregateElement(i), &TypePrinter,
991 Machine, Context);
Chris Lattnercfb5a202008-08-19 05:06:27 +0000992 }
Dan Gohman7dfa07f2009-02-11 00:25:25 +0000993 Out << '>';
Chris Lattnercfb5a202008-08-19 05:06:27 +0000994 return;
995 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000996
Chris Lattnercfb5a202008-08-19 05:06:27 +0000997 if (isa<ConstantPointerNull>(CV)) {
Chris Lattner7a716ad2002-04-16 21:36:08 +0000998 Out << "null";
Chris Lattnercfb5a202008-08-19 05:06:27 +0000999 return;
1000 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001001
Chris Lattnercfb5a202008-08-19 05:06:27 +00001002 if (isa<UndefValue>(CV)) {
Chris Lattnerb976e662004-10-16 18:08:06 +00001003 Out << "undef";
Chris Lattnercfb5a202008-08-19 05:06:27 +00001004 return;
1005 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001006
Chris Lattnercfb5a202008-08-19 05:06:27 +00001007 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Reid Spencer81dfeb32006-12-04 05:19:18 +00001008 Out << CE->getOpcodeName();
Dan Gohman59858cf2009-07-27 16:11:46 +00001009 WriteOptimizationInfo(Out, CE);
Reid Spencer81dfeb32006-12-04 05:19:18 +00001010 if (CE->isCompare())
Chris Lattnercfb5a202008-08-19 05:06:27 +00001011 Out << ' ' << getPredicateText(CE->getPredicate());
Reid Spencer81dfeb32006-12-04 05:19:18 +00001012 Out << " (";
Misha Brukmanfd939082005-04-21 23:48:37 +00001013
Vikram S. Adveb4dbb442002-07-14 23:14:45 +00001014 for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) {
Chris Lattner0f7364b2009-02-28 21:26:53 +00001015 TypePrinter.print((*OI)->getType(), Out);
Dan Gohman8dae1382008-09-14 17:21:12 +00001016 Out << ' ';
Dan Gohman3bdfbf52010-07-20 23:55:01 +00001017 WriteAsOperandInternal(Out, *OI, &TypePrinter, Machine, Context);
Vikram S. Adveb4dbb442002-07-14 23:14:45 +00001018 if (OI+1 != CE->op_end())
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001019 Out << ", ";
Vikram S. Adveb4dbb442002-07-14 23:14:45 +00001020 }
Misha Brukmanfd939082005-04-21 23:48:37 +00001021
Dan Gohman995be7d2008-05-31 19:12:39 +00001022 if (CE->hasIndices()) {
Jay Foadd30aa5a2011-04-13 15:22:40 +00001023 ArrayRef<unsigned> Indices = CE->getIndices();
Dan Gohman995be7d2008-05-31 19:12:39 +00001024 for (unsigned i = 0, e = Indices.size(); i != e; ++i)
1025 Out << ", " << Indices[i];
1026 }
1027
Reid Spencer3da59db2006-11-27 01:05:10 +00001028 if (CE->isCast()) {
Chris Lattner95586b82002-08-15 19:37:43 +00001029 Out << " to ";
Chris Lattner0f7364b2009-02-28 21:26:53 +00001030 TypePrinter.print(CE->getType(), Out);
Chris Lattner95586b82002-08-15 19:37:43 +00001031 }
Reid Spencer3da59db2006-11-27 01:05:10 +00001032
Misha Brukman40c732c2004-06-04 21:11:51 +00001033 Out << ')';
Chris Lattnercfb5a202008-08-19 05:06:27 +00001034 return;
Chris Lattner7a716ad2002-04-16 21:36:08 +00001035 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001036
Chris Lattnercfb5a202008-08-19 05:06:27 +00001037 Out << "<placeholder or erroneous Constant>";
Chris Lattner7a716ad2002-04-16 21:36:08 +00001038}
1039
Chris Lattner85b19122009-12-31 02:31:59 +00001040static void WriteMDNodeBodyInternal(raw_ostream &Out, const MDNode *Node,
1041 TypePrinting *TypePrinter,
Dan Gohman3bdfbf52010-07-20 23:55:01 +00001042 SlotTracker *Machine,
1043 const Module *Context) {
Chris Lattner85b19122009-12-31 02:31:59 +00001044 Out << "!{";
1045 for (unsigned mi = 0, me = Node->getNumOperands(); mi != me; ++mi) {
1046 const Value *V = Node->getOperand(mi);
1047 if (V == 0)
1048 Out << "null";
1049 else {
1050 TypePrinter->print(V->getType(), Out);
1051 Out << ' ';
Andrew Trick18801ec2011-09-30 19:48:58 +00001052 WriteAsOperandInternal(Out, Node->getOperand(mi),
Dan Gohman3bdfbf52010-07-20 23:55:01 +00001053 TypePrinter, Machine, Context);
Chris Lattner85b19122009-12-31 02:31:59 +00001054 }
1055 if (mi + 1 != me)
1056 Out << ", ";
1057 }
Andrew Trick18801ec2011-09-30 19:48:58 +00001058
Chris Lattner85b19122009-12-31 02:31:59 +00001059 Out << "}";
1060}
1061
Chris Lattner7a716ad2002-04-16 21:36:08 +00001062
Misha Brukmanab5c6002004-03-02 00:22:19 +00001063/// WriteAsOperand - Write the name of the specified value out to the specified
1064/// ostream. This can be useful when you just want to print int %reg126, not
1065/// the whole instruction that generated it.
1066///
Dan Gohman1220e102009-08-12 20:56:03 +00001067static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
Dan Gohmand6c0f652009-08-13 15:27:57 +00001068 TypePrinting *TypePrinter,
Dan Gohman3bdfbf52010-07-20 23:55:01 +00001069 SlotTracker *Machine,
1070 const Module *Context) {
Chris Lattnerc97536e2008-08-17 04:40:13 +00001071 if (V->hasName()) {
1072 PrintLLVMName(Out, V);
1073 return;
1074 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001075
Chris Lattnerc97536e2008-08-17 04:40:13 +00001076 const Constant *CV = dyn_cast<Constant>(V);
1077 if (CV && !isa<GlobalValue>(CV)) {
Dan Gohmand6c0f652009-08-13 15:27:57 +00001078 assert(TypePrinter && "Constants require TypePrinting!");
Dan Gohman3bdfbf52010-07-20 23:55:01 +00001079 WriteConstantInternal(Out, CV, *TypePrinter, Machine, Context);
Chris Lattnercfb5a202008-08-19 05:06:27 +00001080 return;
1081 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001082
Chris Lattnercfb5a202008-08-19 05:06:27 +00001083 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
Chris Lattnerc97536e2008-08-17 04:40:13 +00001084 Out << "asm ";
1085 if (IA->hasSideEffects())
1086 Out << "sideeffect ";
Dale Johannesen8ba2d5b2009-10-21 23:28:00 +00001087 if (IA->isAlignStack())
1088 Out << "alignstack ";
Chad Rosier581600b2012-09-05 19:00:49 +00001089 // We don't emit the AD_ATT dialect as it's the assumed default.
1090 if (IA->getDialect() == InlineAsm::AD_Intel)
1091 Out << "inteldialect ";
Chris Lattnerc97536e2008-08-17 04:40:13 +00001092 Out << '"';
1093 PrintEscapedString(IA->getAsmString(), Out);
1094 Out << "\", \"";
1095 PrintEscapedString(IA->getConstraintString(), Out);
1096 Out << '"';
Chris Lattnercfb5a202008-08-19 05:06:27 +00001097 return;
1098 }
Devang Patele54abc92009-07-22 17:43:22 +00001099
Devang Patel104cf9e2009-07-23 01:07:34 +00001100 if (const MDNode *N = dyn_cast<MDNode>(V)) {
Victor Hernandez5d301622009-12-18 20:09:14 +00001101 if (N->isFunctionLocal()) {
Victor Hernandez97e24502009-12-04 01:35:02 +00001102 // Print metadata inline, not via slot reference number.
Dan Gohman3bdfbf52010-07-20 23:55:01 +00001103 WriteMDNodeBodyInternal(Out, N, TypePrinter, Machine, Context);
Victor Hernandez97e24502009-12-04 01:35:02 +00001104 return;
1105 }
Andrew Trick18801ec2011-09-30 19:48:58 +00001106
Dan Gohman3bdfbf52010-07-20 23:55:01 +00001107 if (!Machine) {
1108 if (N->isFunctionLocal())
1109 Machine = new SlotTracker(N->getFunction());
1110 else
1111 Machine = new SlotTracker(Context);
1112 }
Dan Gohman3da076f2010-09-09 20:53:58 +00001113 int Slot = Machine->getMetadataSlot(N);
1114 if (Slot == -1)
1115 Out << "<badref>";
1116 else
1117 Out << '!' << Slot;
Devang Patel104cf9e2009-07-23 01:07:34 +00001118 return;
1119 }
1120
Devang Patele54abc92009-07-22 17:43:22 +00001121 if (const MDString *MDS = dyn_cast<MDString>(V)) {
Devang Patele54abc92009-07-22 17:43:22 +00001122 Out << "!\"";
Daniel Dunbar03d76512009-07-25 23:55:21 +00001123 PrintEscapedString(MDS->getString(), Out);
Devang Patele54abc92009-07-22 17:43:22 +00001124 Out << '"';
1125 return;
1126 }
1127
Evan Cheng746d5462009-11-16 07:10:36 +00001128 if (V->getValueID() == Value::PseudoSourceValueVal ||
1129 V->getValueID() == Value::FixedStackPseudoSourceValueVal) {
Dan Gohmancd26ec52009-09-23 01:33:16 +00001130 V->print(Out);
1131 return;
1132 }
1133
Chris Lattnercfb5a202008-08-19 05:06:27 +00001134 char Prefix = '%';
1135 int Slot;
Chris Lattnerfb5179a2011-08-03 06:15:41 +00001136 // If we have a SlotTracker, use it.
Chris Lattnercfb5a202008-08-19 05:06:27 +00001137 if (Machine) {
1138 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1139 Slot = Machine->getGlobalSlot(GV);
1140 Prefix = '@';
1141 } else {
1142 Slot = Machine->getLocalSlot(V);
Andrew Trick18801ec2011-09-30 19:48:58 +00001143
Chris Lattnerfb5179a2011-08-03 06:15:41 +00001144 // If the local value didn't succeed, then we may be referring to a value
1145 // from a different function. Translate it, as this can happen when using
1146 // address of blocks.
1147 if (Slot == -1)
1148 if ((Machine = createSlotTracker(V))) {
1149 Slot = Machine->getLocalSlot(V);
1150 delete Machine;
1151 }
Chris Lattnercfb5a202008-08-19 05:06:27 +00001152 }
Chris Lattnerfb5179a2011-08-03 06:15:41 +00001153 } else if ((Machine = createSlotTracker(V))) {
1154 // Otherwise, create one to get the # and then destroy it.
1155 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1156 Slot = Machine->getGlobalSlot(GV);
1157 Prefix = '@';
Chris Lattner80cd1152006-01-25 22:26:05 +00001158 } else {
Chris Lattnerfb5179a2011-08-03 06:15:41 +00001159 Slot = Machine->getLocalSlot(V);
Chris Lattner7a716ad2002-04-16 21:36:08 +00001160 }
Chris Lattnerfb5179a2011-08-03 06:15:41 +00001161 delete Machine;
1162 Machine = 0;
1163 } else {
1164 Slot = -1;
Chris Lattner7a716ad2002-04-16 21:36:08 +00001165 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001166
Chris Lattnercfb5a202008-08-19 05:06:27 +00001167 if (Slot != -1)
1168 Out << Prefix << Slot;
1169 else
1170 Out << "<badref>";
Chris Lattner7a716ad2002-04-16 21:36:08 +00001171}
1172
Daniel Malead0fef322013-05-08 20:38:31 +00001173void WriteAsOperand(raw_ostream &Out, const Value *V,
1174 bool PrintType, const Module *Context) {
Dan Gohmand6c0f652009-08-13 15:27:57 +00001175
1176 // Fast path: Don't construct and populate a TypePrinting object if we
1177 // won't be needing any types printed.
Dan Gohman009fc9e2009-08-13 23:07:11 +00001178 if (!PrintType &&
Dan Gohman3bdfbf52010-07-20 23:55:01 +00001179 ((!isa<Constant>(V) && !isa<MDNode>(V)) ||
1180 V->hasName() || isa<GlobalValue>(V))) {
1181 WriteAsOperandInternal(Out, V, 0, 0, Context);
Dan Gohmand6c0f652009-08-13 15:27:57 +00001182 return;
1183 }
1184
Chris Lattner607dc682002-07-10 16:48:17 +00001185 if (Context == 0) Context = getModuleFromVal(V);
Chris Lattner207b5bc2001-10-29 16:37:48 +00001186
Chris Lattnere9fa33e2009-02-28 23:20:19 +00001187 TypePrinting TypePrinter;
Chris Lattner1afcace2011-07-09 17:41:24 +00001188 if (Context)
1189 TypePrinter.incorporateTypes(*Context);
Dan Gohman8dae1382008-09-14 17:21:12 +00001190 if (PrintType) {
Chris Lattner0f7364b2009-02-28 21:26:53 +00001191 TypePrinter.print(V->getType(), Out);
Dan Gohman8dae1382008-09-14 17:21:12 +00001192 Out << ' ';
1193 }
Misha Brukmanfd939082005-04-21 23:48:37 +00001194
Dan Gohman3bdfbf52010-07-20 23:55:01 +00001195 WriteAsOperandInternal(Out, V, &TypePrinter, 0, Context);
Chris Lattner622f7402001-07-20 19:15:21 +00001196}
1197
Daniel Malead0fef322013-05-08 20:38:31 +00001198void AssemblyWriter::init() {
1199 if (TheModule)
1200 TypePrinter.incorporateTypes(*TheModule);
1201}
Chris Lattnerd8c2e422001-07-12 23:35:26 +00001202
Andrew Trick18801ec2011-09-30 19:48:58 +00001203
Daniel Malead0fef322013-05-08 20:38:31 +00001204AssemblyWriter::AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac,
1205 const Module *M,
1206 AssemblyAnnotationWriter *AAW)
1207 : Out(o), TheModule(M), Machine(Mac), AnnotationWriter(AAW) {
1208 init();
1209}
Chris Lattner00950542001-06-06 20:29:01 +00001210
Daniel Malead0fef322013-05-08 20:38:31 +00001211AssemblyWriter::AssemblyWriter(formatted_raw_ostream &o, const Module *M,
1212 AssemblyAnnotationWriter *AAW)
1213 : Out(o), TheModule(M), ModuleSlotTracker(createSlotTracker(M)),
1214 Machine(*ModuleSlotTracker), AnnotationWriter(AAW) {
1215 init();
1216}
Andrew Trick18801ec2011-09-30 19:48:58 +00001217
Daniel Malead0fef322013-05-08 20:38:31 +00001218AssemblyWriter::~AssemblyWriter() { }
Chris Lattner00950542001-06-06 20:29:01 +00001219
Chris Lattner2fcfdb72006-12-06 06:24:27 +00001220void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType) {
1221 if (Operand == 0) {
Chris Lattneraab18202005-02-24 16:58:29 +00001222 Out << "<null operand!>";
Chris Lattnerc65b72c2009-12-31 02:33:14 +00001223 return;
Chris Lattneraab18202005-02-24 16:58:29 +00001224 }
Chris Lattnerc65b72c2009-12-31 02:33:14 +00001225 if (PrintType) {
1226 TypePrinter.print(Operand->getType(), Out);
1227 Out << ' ';
1228 }
Dan Gohman3bdfbf52010-07-20 23:55:01 +00001229 WriteAsOperandInternal(Out, Operand, &TypePrinter, &Machine, TheModule);
Chris Lattner00950542001-06-06 20:29:01 +00001230}
1231
Eli Friedman47f35132011-07-25 23:16:38 +00001232void AssemblyWriter::writeAtomic(AtomicOrdering Ordering,
1233 SynchronizationScope SynchScope) {
1234 if (Ordering == NotAtomic)
1235 return;
1236
1237 switch (SynchScope) {
Eli Friedman47f35132011-07-25 23:16:38 +00001238 case SingleThread: Out << " singlethread"; break;
1239 case CrossThread: break;
1240 }
1241
1242 switch (Ordering) {
1243 default: Out << " <bad ordering " << int(Ordering) << ">"; break;
1244 case Unordered: Out << " unordered"; break;
1245 case Monotonic: Out << " monotonic"; break;
1246 case Acquire: Out << " acquire"; break;
1247 case Release: Out << " release"; break;
1248 case AcquireRelease: Out << " acq_rel"; break;
1249 case SequentiallyConsistent: Out << " seq_cst"; break;
1250 }
1251}
1252
Daniel Dunbara279bc32009-09-20 02:20:51 +00001253void AssemblyWriter::writeParamOperand(const Value *Operand,
Bill Wendling94e94b32012-12-30 13:50:49 +00001254 AttributeSet Attrs, unsigned Idx) {
Duncan Sandsdc024672007-11-27 13:23:08 +00001255 if (Operand == 0) {
1256 Out << "<null operand!>";
Chris Lattnerc65b72c2009-12-31 02:33:14 +00001257 return;
Duncan Sandsdc024672007-11-27 13:23:08 +00001258 }
Chris Lattnerc65b72c2009-12-31 02:33:14 +00001259
1260 // Print the type
1261 TypePrinter.print(Operand->getType(), Out);
1262 // Print parameter attributes list
Bill Wendling94e94b32012-12-30 13:50:49 +00001263 if (Attrs.hasAttributes(Idx))
1264 Out << ' ' << Attrs.getAsString(Idx);
Chris Lattnerc65b72c2009-12-31 02:33:14 +00001265 Out << ' ';
1266 // Print the operand
Dan Gohman3bdfbf52010-07-20 23:55:01 +00001267 WriteAsOperandInternal(Out, Operand, &TypePrinter, &Machine, TheModule);
Duncan Sandsdc024672007-11-27 13:23:08 +00001268}
Chris Lattner00950542001-06-06 20:29:01 +00001269
Chris Lattnerc1824992001-10-29 16:05:51 +00001270void AssemblyWriter::printModule(const Module *M) {
Bill Wendlingb29ce262013-02-11 08:43:33 +00001271 Machine.initialize();
1272
Chris Lattner31ab1b32005-03-02 23:12:40 +00001273 if (!M->getModuleIdentifier().empty() &&
Misha Brukmanfd939082005-04-21 23:48:37 +00001274 // Don't print the ID if it will start a new line (which would
Chris Lattner31ab1b32005-03-02 23:12:40 +00001275 // require a comment char before it).
1276 M->getModuleIdentifier().find('\n') == std::string::npos)
1277 Out << "; ModuleID = '" << M->getModuleIdentifier() << "'\n";
1278
Owen Andersoncf7ff2b2006-10-18 02:21:12 +00001279 if (!M->getDataLayout().empty())
Chris Lattnerd2f9e602006-10-22 06:06:56 +00001280 Out << "target datalayout = \"" << M->getDataLayout() << "\"\n";
Reid Spencercddc86f2004-07-25 21:44:54 +00001281 if (!M->getTargetTriple().empty())
Reid Spencerc9a1f0d2004-07-25 21:29:43 +00001282 Out << "target triple = \"" << M->getTargetTriple() << "\"\n";
Misha Brukmanfd939082005-04-21 23:48:37 +00001283
Chris Lattnercc041ba2006-01-24 04:13:11 +00001284 if (!M->getModuleInlineAsm().empty()) {
Chris Lattner42a162e2006-01-24 00:45:30 +00001285 // Split the string into lines, to make it easier to read the .ll file.
Chris Lattnercc041ba2006-01-24 04:13:11 +00001286 std::string Asm = M->getModuleInlineAsm();
Chris Lattner42a162e2006-01-24 00:45:30 +00001287 size_t CurPos = 0;
1288 size_t NewLine = Asm.find_first_of('\n', CurPos);
Dan Gohman9bf0b9b2009-08-12 23:54:22 +00001289 Out << '\n';
Chris Lattner42a162e2006-01-24 00:45:30 +00001290 while (NewLine != std::string::npos) {
1291 // We found a newline, print the portion of the asm string from the
1292 // last newline up to this newline.
1293 Out << "module asm \"";
1294 PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.begin()+NewLine),
1295 Out);
1296 Out << "\"\n";
1297 CurPos = NewLine+1;
1298 NewLine = Asm.find_first_of('\n', CurPos);
1299 }
Rafael Espindola38c4e532011-03-02 04:14:42 +00001300 std::string rest(Asm.begin()+CurPos, Asm.end());
1301 if (!rest.empty()) {
1302 Out << "module asm \"";
1303 PrintEscapedString(rest, Out);
1304 Out << "\"\n";
1305 }
Chris Lattner18365502006-01-23 23:03:36 +00001306 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001307
Chris Lattner1afcace2011-07-09 17:41:24 +00001308 printTypeIdentities();
Misha Brukmanfd939082005-04-21 23:48:37 +00001309
Dan Gohman9bf0b9b2009-08-12 23:54:22 +00001310 // Output all globals.
1311 if (!M->global_empty()) Out << '\n';
Chris Lattnerd6d826c2006-12-06 04:41:52 +00001312 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
Duncan Sands79da6ef2012-09-12 09:55:51 +00001313 I != E; ++I) {
1314 printGlobal(I); Out << '\n';
1315 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001316
Chris Lattner69dacfc2007-04-26 02:24:10 +00001317 // Output all aliases.
1318 if (!M->alias_empty()) Out << "\n";
1319 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
1320 I != E; ++I)
1321 printAlias(I);
Chris Lattner007377f2001-09-07 16:36:04 +00001322
Chris Lattner44da7d72004-09-14 05:06:58 +00001323 // Output all of the functions.
Chris Lattner7e708292002-06-25 16:13:24 +00001324 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
1325 printFunction(I);
Devang Patel320671d2009-07-08 21:44:25 +00001326
Bill Wendlingb29ce262013-02-11 08:43:33 +00001327 // Output all attribute groups.
Bill Wendling725dae52013-04-29 23:48:06 +00001328 if (!Machine.as_empty()) {
Bill Wendlingb29ce262013-02-11 08:43:33 +00001329 Out << '\n';
1330 writeAllAttributeGroups();
1331 }
1332
Devang Patel37c4a2d2009-07-29 22:04:47 +00001333 // Output named metadata.
Dan Gohman9bf0b9b2009-08-12 23:54:22 +00001334 if (!M->named_metadata_empty()) Out << '\n';
Andrew Trick18801ec2011-09-30 19:48:58 +00001335
Devang Patel37c4a2d2009-07-29 22:04:47 +00001336 for (Module::const_named_metadata_iterator I = M->named_metadata_begin(),
Chris Lattner6e6b1802009-12-31 02:13:35 +00001337 E = M->named_metadata_end(); I != E; ++I)
Chris Lattnerfdb33562009-12-31 01:54:05 +00001338 printNamedMDNode(I);
Devang Patel37c4a2d2009-07-29 22:04:47 +00001339
1340 // Output metadata.
Chris Lattner307c9892009-12-31 02:20:11 +00001341 if (!Machine.mdn_empty()) {
Chris Lattner6e6b1802009-12-31 02:13:35 +00001342 Out << '\n';
1343 writeAllMDNodes();
1344 }
Chris Lattner007377f2001-09-07 16:36:04 +00001345}
1346
Chris Lattnerfdb33562009-12-31 01:54:05 +00001347void AssemblyWriter::printNamedMDNode(const NamedMDNode *NMD) {
Nick Lewycky9100a782011-06-15 06:37:58 +00001348 Out << '!';
1349 StringRef Name = NMD->getName();
1350 if (Name.empty()) {
1351 Out << "<empty name> ";
1352 } else {
Guy Benyei87d0b9e2013-02-12 21:21:59 +00001353 if (isalpha(static_cast<unsigned char>(Name[0])) ||
1354 Name[0] == '-' || Name[0] == '$' ||
Nick Lewycky9100a782011-06-15 06:37:58 +00001355 Name[0] == '.' || Name[0] == '_')
1356 Out << Name[0];
1357 else
1358 Out << '\\' << hexdigit(Name[0] >> 4) << hexdigit(Name[0] & 0x0F);
1359 for (unsigned i = 1, e = Name.size(); i != e; ++i) {
1360 unsigned char C = Name[i];
Guy Benyei87d0b9e2013-02-12 21:21:59 +00001361 if (isalnum(static_cast<unsigned char>(C)) || C == '-' || C == '$' ||
1362 C == '.' || C == '_')
Nick Lewycky9100a782011-06-15 06:37:58 +00001363 Out << C;
1364 else
1365 Out << '\\' << hexdigit(C >> 4) << hexdigit(C & 0x0F);
1366 }
1367 }
1368 Out << " = !{";
Chris Lattnerfdb33562009-12-31 01:54:05 +00001369 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1370 if (i) Out << ", ";
Dan Gohman3da076f2010-09-09 20:53:58 +00001371 int Slot = Machine.getMetadataSlot(NMD->getOperand(i));
1372 if (Slot == -1)
1373 Out << "<badref>";
1374 else
1375 Out << '!' << Slot;
Chris Lattnerfdb33562009-12-31 01:54:05 +00001376 }
1377 Out << "}\n";
1378}
1379
1380
Dan Gohman683e9222009-08-12 17:23:50 +00001381static void PrintLinkage(GlobalValue::LinkageTypes LT,
1382 formatted_raw_ostream &Out) {
Chris Lattnercfb5a202008-08-19 05:06:27 +00001383 switch (LT) {
Bill Wendling3d10a5a2009-07-20 01:03:30 +00001384 case GlobalValue::ExternalLinkage: break;
1385 case GlobalValue::PrivateLinkage: Out << "private "; break;
1386 case GlobalValue::LinkerPrivateLinkage: Out << "linker_private "; break;
Bill Wendling5e721d72010-07-01 21:55:59 +00001387 case GlobalValue::LinkerPrivateWeakLinkage:
1388 Out << "linker_private_weak ";
1389 break;
Bill Wendling3d10a5a2009-07-20 01:03:30 +00001390 case GlobalValue::InternalLinkage: Out << "internal "; break;
1391 case GlobalValue::LinkOnceAnyLinkage: Out << "linkonce "; break;
1392 case GlobalValue::LinkOnceODRLinkage: Out << "linkonce_odr "; break;
Bill Wendling32811be2012-08-17 18:33:14 +00001393 case GlobalValue::LinkOnceODRAutoHideLinkage:
1394 Out << "linkonce_odr_auto_hide ";
1395 break;
Bill Wendling3d10a5a2009-07-20 01:03:30 +00001396 case GlobalValue::WeakAnyLinkage: Out << "weak "; break;
1397 case GlobalValue::WeakODRLinkage: Out << "weak_odr "; break;
1398 case GlobalValue::CommonLinkage: Out << "common "; break;
1399 case GlobalValue::AppendingLinkage: Out << "appending "; break;
1400 case GlobalValue::DLLImportLinkage: Out << "dllimport "; break;
1401 case GlobalValue::DLLExportLinkage: Out << "dllexport "; break;
1402 case GlobalValue::ExternalWeakLinkage: Out << "extern_weak "; break;
Chris Lattner266c7bb2009-04-13 05:44:34 +00001403 case GlobalValue::AvailableExternallyLinkage:
1404 Out << "available_externally ";
1405 break;
Chris Lattnercfb5a202008-08-19 05:06:27 +00001406 }
1407}
Duncan Sands667d4b82009-03-07 15:45:40 +00001408
Chris Lattnercfb5a202008-08-19 05:06:27 +00001409
1410static void PrintVisibility(GlobalValue::VisibilityTypes Vis,
Dan Gohman683e9222009-08-12 17:23:50 +00001411 formatted_raw_ostream &Out) {
Chris Lattnercfb5a202008-08-19 05:06:27 +00001412 switch (Vis) {
Chris Lattnercfb5a202008-08-19 05:06:27 +00001413 case GlobalValue::DefaultVisibility: break;
1414 case GlobalValue::HiddenVisibility: Out << "hidden "; break;
1415 case GlobalValue::ProtectedVisibility: Out << "protected "; break;
1416 }
1417}
1418
Hans Wennborgce718ff2012-06-23 11:37:03 +00001419static void PrintThreadLocalModel(GlobalVariable::ThreadLocalMode TLM,
1420 formatted_raw_ostream &Out) {
1421 switch (TLM) {
1422 case GlobalVariable::NotThreadLocal:
1423 break;
1424 case GlobalVariable::GeneralDynamicTLSModel:
1425 Out << "thread_local ";
1426 break;
1427 case GlobalVariable::LocalDynamicTLSModel:
1428 Out << "thread_local(localdynamic) ";
1429 break;
1430 case GlobalVariable::InitialExecTLSModel:
1431 Out << "thread_local(initialexec) ";
1432 break;
1433 case GlobalVariable::LocalExecTLSModel:
1434 Out << "thread_local(localexec) ";
1435 break;
1436 }
1437}
1438
Chris Lattnerc1824992001-10-29 16:05:51 +00001439void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Dan Gohman4483c7b2010-01-29 23:12:36 +00001440 if (GV->isMaterializable())
1441 Out << "; Materializable\n";
1442
Dan Gohman3bdfbf52010-07-20 23:55:01 +00001443 WriteAsOperandInternal(Out, GV, &TypePrinter, &Machine, GV->getParent());
Dan Gohman3845e502009-08-12 23:32:33 +00001444 Out << " = ";
Chris Lattnerd70684f2001-09-18 04:01:05 +00001445
Chris Lattner52b26de2008-08-19 05:16:28 +00001446 if (!GV->hasInitializer() && GV->hasExternalLinkage())
1447 Out << "external ";
Daniel Dunbara279bc32009-09-20 02:20:51 +00001448
Chris Lattner52b26de2008-08-19 05:16:28 +00001449 PrintLinkage(GV->getLinkage(), Out);
1450 PrintVisibility(GV->getVisibility(), Out);
Hans Wennborgce718ff2012-06-23 11:37:03 +00001451 PrintThreadLocalModel(GV->getThreadLocalMode(), Out);
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00001452
Chris Lattnerdf986172009-01-02 07:01:27 +00001453 if (unsigned AddressSpace = GV->getType()->getAddressSpace())
1454 Out << "addrspace(" << AddressSpace << ") ";
Rafael Espindolabea46262011-01-08 16:42:36 +00001455 if (GV->hasUnnamedAddr()) Out << "unnamed_addr ";
Michael Gottesmana2de37c2013-02-05 05:57:38 +00001456 if (GV->isExternallyInitialized()) Out << "externally_initialized ";
Misha Brukman0313e0b2004-06-21 21:53:56 +00001457 Out << (GV->isConstant() ? "constant " : "global ");
Chris Lattner0f7364b2009-02-28 21:26:53 +00001458 TypePrinter.print(GV->getType()->getElementType(), Out);
Chris Lattnerd70684f2001-09-18 04:01:05 +00001459
Dan Gohman8dae1382008-09-14 17:21:12 +00001460 if (GV->hasInitializer()) {
1461 Out << ' ';
Devang Patel320671d2009-07-08 21:44:25 +00001462 writeOperand(GV->getInitializer(), false);
Dan Gohman8dae1382008-09-14 17:21:12 +00001463 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001464
Chris Lattner8fff1262010-07-07 23:16:37 +00001465 if (GV->hasSection()) {
1466 Out << ", section \"";
1467 PrintEscapedString(GV->getSection(), Out);
1468 Out << '"';
1469 }
Chris Lattner60962db2005-11-12 00:10:19 +00001470 if (GV->getAlignment())
Chris Lattner30caa282005-11-06 06:48:53 +00001471 Out << ", align " << GV->getAlignment();
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001472
Chris Lattner7e708292002-06-25 16:13:24 +00001473 printInfoComment(*GV);
Chris Lattner70cc3392001-09-10 07:58:01 +00001474}
1475
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001476void AssemblyWriter::printAlias(const GlobalAlias *GA) {
Dan Gohman4483c7b2010-01-29 23:12:36 +00001477 if (GA->isMaterializable())
1478 Out << "; Materializable\n";
1479
Dale Johannesen24f07dc2008-06-03 18:14:29 +00001480 // Don't crash when dumping partially built GA
1481 if (!GA->hasName())
1482 Out << "<<nameless>> = ";
Chris Lattnerc97536e2008-08-17 04:40:13 +00001483 else {
1484 PrintLLVMName(Out, GA);
1485 Out << " = ";
1486 }
Chris Lattnercfb5a202008-08-19 05:06:27 +00001487 PrintVisibility(GA->getVisibility(), Out);
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001488
1489 Out << "alias ";
1490
Chris Lattnercfb5a202008-08-19 05:06:27 +00001491 PrintLinkage(GA->getLinkage(), Out);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001492
Anton Korobeynikovc6c98af2007-04-29 18:02:48 +00001493 const Constant *Aliasee = GA->getAliasee();
Daniel Dunbara279bc32009-09-20 02:20:51 +00001494
Chris Lattner1afcace2011-07-09 17:41:24 +00001495 if (Aliasee == 0) {
1496 TypePrinter.print(GA->getType(), Out);
1497 Out << " <<NULL ALIASEE>>";
Jay Foad5cd8ea22011-08-01 12:48:54 +00001498 } else {
Jay Foad8d948652011-08-01 12:29:14 +00001499 writeOperand(Aliasee, !isa<ConstantExpr>(Aliasee));
Jay Foad5cd8ea22011-08-01 12:48:54 +00001500 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001501
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001502 printInfoComment(*GA);
Chris Lattner52b26de2008-08-19 05:16:28 +00001503 Out << '\n';
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001504}
1505
Chris Lattner1afcace2011-07-09 17:41:24 +00001506void AssemblyWriter::printTypeIdentities() {
1507 if (TypePrinter.NumberedTypes.empty() &&
1508 TypePrinter.NamedTypes.empty())
1509 return;
Andrew Trick18801ec2011-09-30 19:48:58 +00001510
Chris Lattner1afcace2011-07-09 17:41:24 +00001511 Out << '\n';
Andrew Trick18801ec2011-09-30 19:48:58 +00001512
Chris Lattner1afcace2011-07-09 17:41:24 +00001513 // We know all the numbers that each type is used and we know that it is a
1514 // dense assignment. Convert the map to an index table.
1515 std::vector<StructType*> NumberedTypes(TypePrinter.NumberedTypes.size());
Andrew Trick18801ec2011-09-30 19:48:58 +00001516 for (DenseMap<StructType*, unsigned>::iterator I =
Chris Lattner1afcace2011-07-09 17:41:24 +00001517 TypePrinter.NumberedTypes.begin(), E = TypePrinter.NumberedTypes.end();
1518 I != E; ++I) {
1519 assert(I->second < NumberedTypes.size() && "Didn't get a dense numbering?");
1520 NumberedTypes[I->second] = I->first;
1521 }
Andrew Trick18801ec2011-09-30 19:48:58 +00001522
Chris Lattner413fd232009-03-01 00:03:38 +00001523 // Emit all numbered types.
1524 for (unsigned i = 0, e = NumberedTypes.size(); i != e; ++i) {
Dan Gohman3845e502009-08-12 23:32:33 +00001525 Out << '%' << i << " = type ";
Andrew Trick18801ec2011-09-30 19:48:58 +00001526
Chris Lattner413fd232009-03-01 00:03:38 +00001527 // Make sure we print out at least one level of the type structure, so
1528 // that we do not get %2 = type %2
Chris Lattner1afcace2011-07-09 17:41:24 +00001529 TypePrinter.printStructBody(NumberedTypes[i], Out);
Dan Gohman9bf0b9b2009-08-12 23:54:22 +00001530 Out << '\n';
Chris Lattner413fd232009-03-01 00:03:38 +00001531 }
Andrew Trick18801ec2011-09-30 19:48:58 +00001532
Chris Lattner1afcace2011-07-09 17:41:24 +00001533 for (unsigned i = 0, e = TypePrinter.NamedTypes.size(); i != e; ++i) {
1534 PrintLLVMName(Out, TypePrinter.NamedTypes[i]->getName(), LocalPrefix);
Chris Lattner52b26de2008-08-19 05:16:28 +00001535 Out << " = type ";
Reid Spencer9231ac82004-05-25 08:53:40 +00001536
1537 // Make sure we print out at least one level of the type structure, so
1538 // that we do not get %FILE = type %FILE
Chris Lattner1afcace2011-07-09 17:41:24 +00001539 TypePrinter.printStructBody(TypePrinter.NamedTypes[i], Out);
Chris Lattnercfb5a202008-08-19 05:06:27 +00001540 Out << '\n';
Reid Spencer9231ac82004-05-25 08:53:40 +00001541 }
Reid Spencer78d033e2007-01-06 07:24:44 +00001542}
1543
Misha Brukmanab5c6002004-03-02 00:22:19 +00001544/// printFunction - Print all aspects of a function.
1545///
Chris Lattner7e708292002-06-25 16:13:24 +00001546void AssemblyWriter::printFunction(const Function *F) {
Chris Lattnercfb5a202008-08-19 05:06:27 +00001547 // Print out the return type and name.
1548 Out << '\n';
Chris Lattner4ad02e72003-04-16 20:28:45 +00001549
Misha Brukman0313e0b2004-06-21 21:53:56 +00001550 if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, Out);
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001551
Dan Gohman4483c7b2010-01-29 23:12:36 +00001552 if (F->isMaterializable())
1553 Out << "; Materializable\n";
1554
Bill Wendling63405492013-04-16 20:55:47 +00001555 const AttributeSet &Attrs = F->getAttributes();
Bill Wendling725dae52013-04-29 23:48:06 +00001556 if (Attrs.hasAttributes(AttributeSet::FunctionIndex)) {
Bill Wendling63405492013-04-16 20:55:47 +00001557 AttributeSet AS = Attrs.getFnAttributes();
Rafael Espindolaaae02982013-05-01 13:07:03 +00001558 std::string AttrStr;
1559
1560 unsigned Idx = 0;
1561 for (unsigned E = AS.getNumSlots(); Idx != E; ++Idx)
1562 if (AS.getSlotIndex(Idx) == AttributeSet::FunctionIndex)
1563 break;
1564
1565 for (AttributeSet::iterator I = AS.begin(Idx), E = AS.end(Idx);
1566 I != E; ++I) {
1567 Attribute Attr = *I;
1568 if (!Attr.isStringAttribute()) {
1569 if (!AttrStr.empty()) AttrStr += ' ';
1570 AttrStr += Attr.getAsString();
1571 }
1572 }
1573
Bill Wendling63405492013-04-16 20:55:47 +00001574 if (!AttrStr.empty())
1575 Out << "; Function Attrs: " << AttrStr << '\n';
1576 }
1577
Reid Spencer5cbf9852007-01-30 20:08:39 +00001578 if (F->isDeclaration())
Chris Lattner3aa60662007-08-19 22:15:26 +00001579 Out << "declare ";
1580 else
Reid Spencerb951bc02006-12-29 20:29:48 +00001581 Out << "define ";
Daniel Dunbara279bc32009-09-20 02:20:51 +00001582
Chris Lattnercfb5a202008-08-19 05:06:27 +00001583 PrintLinkage(F->getLinkage(), Out);
1584 PrintVisibility(F->getVisibility(), Out);
Chris Lattner4ad02e72003-04-16 20:28:45 +00001585
Chris Lattnerd5118982005-05-06 20:26:43 +00001586 // Print the calling convention.
Micah Villmowd3766df2012-09-13 15:11:12 +00001587 if (F->getCallingConv() != CallingConv::C) {
1588 PrintCallingConv(F->getCallingConv(), Out);
1589 Out << " ";
Chris Lattnerd5118982005-05-06 20:26:43 +00001590 }
1591
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001592 FunctionType *FT = F->getFunctionType();
Bill Wendling1b0c54f2013-01-18 21:53:16 +00001593 if (Attrs.hasAttributes(AttributeSet::ReturnIndex))
1594 Out << Attrs.getAsString(AttributeSet::ReturnIndex) << ' ';
Chris Lattner0f7364b2009-02-28 21:26:53 +00001595 TypePrinter.print(F->getReturnType(), Out);
Chris Lattner4667b712008-08-19 05:26:17 +00001596 Out << ' ';
Dan Gohman3bdfbf52010-07-20 23:55:01 +00001597 WriteAsOperandInternal(Out, F, &TypePrinter, &Machine, F->getParent());
Misha Brukman0313e0b2004-06-21 21:53:56 +00001598 Out << '(';
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001599 Machine.incorporateFunction(F);
Chris Lattner007377f2001-09-07 16:36:04 +00001600
Chris Lattnerc1824992001-10-29 16:05:51 +00001601 // Loop over the arguments, printing them...
Chris Lattner007377f2001-09-07 16:36:04 +00001602
Reid Spencerbd5db8e2006-12-31 05:24:50 +00001603 unsigned Idx = 1;
Chris Lattner8dcd2f12007-04-18 00:57:22 +00001604 if (!F->isDeclaration()) {
1605 // If this isn't a declaration, print the argument names as well.
1606 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
1607 I != E; ++I) {
1608 // Insert commas as we go... the first arg doesn't get a comma
1609 if (I != F->arg_begin()) Out << ", ";
Bill Wendling94e94b32012-12-30 13:50:49 +00001610 printArgument(I, Attrs, Idx);
Chris Lattner8dcd2f12007-04-18 00:57:22 +00001611 Idx++;
1612 }
1613 } else {
1614 // Otherwise, print the types from the function type.
1615 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
1616 // Insert commas as we go... the first arg doesn't get a comma
1617 if (i) Out << ", ";
Daniel Dunbara279bc32009-09-20 02:20:51 +00001618
Chris Lattner8dcd2f12007-04-18 00:57:22 +00001619 // Output type...
Chris Lattner0f7364b2009-02-28 21:26:53 +00001620 TypePrinter.print(FT->getParamType(i), Out);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001621
Bill Wendling94e94b32012-12-30 13:50:49 +00001622 if (Attrs.hasAttributes(i+1))
1623 Out << ' ' << Attrs.getAsString(i+1);
Chris Lattner8dcd2f12007-04-18 00:57:22 +00001624 }
Reid Spencerbd5db8e2006-12-31 05:24:50 +00001625 }
Chris Lattner007377f2001-09-07 16:36:04 +00001626
1627 // Finish printing arguments...
Chris Lattner7e708292002-06-25 16:13:24 +00001628 if (FT->isVarArg()) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001629 if (FT->getNumParams()) Out << ", ";
1630 Out << "..."; // Output varargs portion of signature!
Chris Lattner007377f2001-09-07 16:36:04 +00001631 }
Misha Brukman0313e0b2004-06-21 21:53:56 +00001632 Out << ')';
Rafael Espindola3971df52011-01-25 19:09:56 +00001633 if (F->hasUnnamedAddr())
1634 Out << " unnamed_addr";
Bill Wendling725dae52013-04-29 23:48:06 +00001635 if (Attrs.hasAttributes(AttributeSet::FunctionIndex))
1636 Out << " #" << Machine.getAttributeGroupSlot(Attrs.getFnAttributes());
Chris Lattner8fff1262010-07-07 23:16:37 +00001637 if (F->hasSection()) {
1638 Out << " section \"";
1639 PrintEscapedString(F->getSection(), Out);
1640 Out << '"';
1641 }
Chris Lattner30caa282005-11-06 06:48:53 +00001642 if (F->getAlignment())
1643 Out << " align " << F->getAlignment();
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001644 if (F->hasGC())
1645 Out << " gc \"" << F->getGC() << '"';
Reid Spencer5cbf9852007-01-30 20:08:39 +00001646 if (F->isDeclaration()) {
Chris Lattner91fb4072010-09-02 22:52:10 +00001647 Out << '\n';
Chris Lattner03e2acb2002-05-06 03:00:40 +00001648 } else {
Chris Lattner91fb4072010-09-02 22:52:10 +00001649 Out << " {";
1650 // Output all of the function's basic blocks.
Chris Lattner7e708292002-06-25 16:13:24 +00001651 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
1652 printBasicBlock(I);
Chris Lattner007377f2001-09-07 16:36:04 +00001653
Misha Brukman0313e0b2004-06-21 21:53:56 +00001654 Out << "}\n";
Chris Lattner007377f2001-09-07 16:36:04 +00001655 }
1656
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001657 Machine.purgeFunction();
Chris Lattner00950542001-06-06 20:29:01 +00001658}
1659
Misha Brukmanab5c6002004-03-02 00:22:19 +00001660/// printArgument - This member is called for every argument that is passed into
1661/// the function. Simply print it out
1662///
Daniel Dunbara279bc32009-09-20 02:20:51 +00001663void AssemblyWriter::printArgument(const Argument *Arg,
Bill Wendling94e94b32012-12-30 13:50:49 +00001664 AttributeSet Attrs, unsigned Idx) {
Chris Lattner00950542001-06-06 20:29:01 +00001665 // Output type...
Chris Lattner0f7364b2009-02-28 21:26:53 +00001666 TypePrinter.print(Arg->getType(), Out);
Misha Brukmanfd939082005-04-21 23:48:37 +00001667
Duncan Sandsdc024672007-11-27 13:23:08 +00001668 // Output parameter attributes list
Bill Wendling94e94b32012-12-30 13:50:49 +00001669 if (Attrs.hasAttributes(Idx))
1670 Out << ' ' << Attrs.getAsString(Idx);
Reid Spencerbd5db8e2006-12-31 05:24:50 +00001671
Chris Lattner00950542001-06-06 20:29:01 +00001672 // Output name, if available...
Chris Lattnerc97536e2008-08-17 04:40:13 +00001673 if (Arg->hasName()) {
1674 Out << ' ';
1675 PrintLLVMName(Out, Arg);
1676 }
Chris Lattner00950542001-06-06 20:29:01 +00001677}
1678
Misha Brukmanab5c6002004-03-02 00:22:19 +00001679/// printBasicBlock - This member is called for each basic block in a method.
1680///
Chris Lattnerc1824992001-10-29 16:05:51 +00001681void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Nick Lewycky280a6e62008-04-25 16:53:59 +00001682 if (BB->hasName()) { // Print out the label if it exists...
Chris Lattnerc97536e2008-08-17 04:40:13 +00001683 Out << "\n";
Daniel Dunbar03d76512009-07-25 23:55:21 +00001684 PrintLLVMName(Out, BB->getName(), LabelPrefix);
Chris Lattnerc97536e2008-08-17 04:40:13 +00001685 Out << ':';
Nick Lewycky280a6e62008-04-25 16:53:59 +00001686 } else if (!BB->use_empty()) { // Don't print block # of no uses...
Bill Wendling5d7a5a42011-04-10 23:18:04 +00001687 Out << "\n; <label>:";
Chris Lattner22379bc2007-01-11 03:54:27 +00001688 int Slot = Machine.getLocalSlot(BB);
Chris Lattner69566452004-06-09 19:41:19 +00001689 if (Slot != -1)
Misha Brukman0313e0b2004-06-21 21:53:56 +00001690 Out << Slot;
Chris Lattner69566452004-06-09 19:41:19 +00001691 else
Misha Brukman0313e0b2004-06-21 21:53:56 +00001692 Out << "<badref>";
Chris Lattner061269b2002-10-02 19:38:55 +00001693 }
Chris Lattner4e4d8622003-11-20 00:09:43 +00001694
Dan Gohman683e9222009-08-12 17:23:50 +00001695 if (BB->getParent() == 0) {
Chris Lattner8f4b1ec2009-08-17 15:48:08 +00001696 Out.PadToColumn(50);
Dan Gohman683e9222009-08-12 17:23:50 +00001697 Out << "; Error: Block without parent!";
1698 } else if (BB != &BB->getParent()->getEntryBlock()) { // Not the entry block?
Chris Lattner91fb4072010-09-02 22:52:10 +00001699 // Output predecessors for the block.
Chris Lattner8f4b1ec2009-08-17 15:48:08 +00001700 Out.PadToColumn(50);
Dan Gohman683e9222009-08-12 17:23:50 +00001701 Out << ";";
Gabor Greif44424642010-03-25 23:25:28 +00001702 const_pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001703
Chris Lattnereb411292008-04-22 02:45:44 +00001704 if (PI == PE) {
1705 Out << " No predecessors!";
1706 } else {
Dan Gohman8dae1382008-09-14 17:21:12 +00001707 Out << " preds = ";
Chris Lattnereb411292008-04-22 02:45:44 +00001708 writeOperand(*PI, false);
1709 for (++PI; PI != PE; ++PI) {
Dan Gohman8dae1382008-09-14 17:21:12 +00001710 Out << ", ";
Chris Lattner2fcfdb72006-12-06 06:24:27 +00001711 writeOperand(*PI, false);
Chris Lattner40efcec2003-11-16 22:59:57 +00001712 }
Chris Lattner061269b2002-10-02 19:38:55 +00001713 }
Chris Lattner00950542001-06-06 20:29:01 +00001714 }
Misha Brukmanfd939082005-04-21 23:48:37 +00001715
Chris Lattnereb411292008-04-22 02:45:44 +00001716 Out << "\n";
Chris Lattner00950542001-06-06 20:29:01 +00001717
Misha Brukman0313e0b2004-06-21 21:53:56 +00001718 if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, Out);
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001719
Chris Lattner007377f2001-09-07 16:36:04 +00001720 // Output all of the instructions in the basic block...
Dan Gohmanbeca6892009-07-13 18:27:59 +00001721 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
Daniel Malead0fef322013-05-08 20:38:31 +00001722 printInstructionLine(*I);
Dan Gohmanbeca6892009-07-13 18:27:59 +00001723 }
Chris Lattner9f717ef2004-03-08 18:51:45 +00001724
Misha Brukman0313e0b2004-06-21 21:53:56 +00001725 if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, Out);
Chris Lattner00950542001-06-06 20:29:01 +00001726}
1727
Daniel Malead0fef322013-05-08 20:38:31 +00001728/// printInstructionLine - Print an instruction and a newline character.
1729void AssemblyWriter::printInstructionLine(const Instruction &I) {
1730 printInstruction(I);
1731 Out << '\n';
1732}
1733
Misha Brukmanab5c6002004-03-02 00:22:19 +00001734/// printInfoComment - Print a little comment after the instruction indicating
1735/// which slot it occupies.
1736///
Chris Lattner7e708292002-06-25 16:13:24 +00001737void AssemblyWriter::printInfoComment(const Value &V) {
Bill Wendling63405492013-04-16 20:55:47 +00001738 if (AnnotationWriter)
Dan Gohman7a5666e2010-02-10 20:41:46 +00001739 AnnotationWriter->printInfoComment(V, Out);
Chris Lattnere02fa852001-10-13 06:42:36 +00001740}
1741
Reid Spencer3a9ec242006-08-28 01:02:49 +00001742// This member is called for each Instruction in a function..
Chris Lattner7e708292002-06-25 16:13:24 +00001743void AssemblyWriter::printInstruction(const Instruction &I) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001744 if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, Out);
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001745
Dan Gohman3845e502009-08-12 23:32:33 +00001746 // Print out indentation for an instruction.
Dan Gohman01889ca2009-08-13 01:41:52 +00001747 Out << " ";
Chris Lattner00950542001-06-06 20:29:01 +00001748
1749 // Print out name if it exists...
Chris Lattnerc97536e2008-08-17 04:40:13 +00001750 if (I.hasName()) {
1751 PrintLLVMName(Out, &I);
1752 Out << " = ";
Chris Lattner4ee93c42009-12-29 07:25:48 +00001753 } else if (!I.getType()->isVoidTy()) {
Chris Lattner828db8a2008-08-29 17:19:30 +00001754 // Print out the def slot taken.
1755 int SlotNum = Machine.getLocalSlot(&I);
1756 if (SlotNum == -1)
1757 Out << "<badref> = ";
1758 else
1759 Out << '%' << SlotNum << " = ";
Chris Lattnerc97536e2008-08-17 04:40:13 +00001760 }
Andrew Trick18801ec2011-09-30 19:48:58 +00001761
Eli Friedman21006d42011-08-09 23:02:53 +00001762 if (isa<CallInst>(I) && cast<CallInst>(I).isTailCall())
Chris Lattnerddb6db42005-05-06 05:51:46 +00001763 Out << "tail ";
Chris Lattnere5e475e2003-09-08 17:45:59 +00001764
Chris Lattner00950542001-06-06 20:29:01 +00001765 // Print out the opcode...
Misha Brukman0313e0b2004-06-21 21:53:56 +00001766 Out << I.getOpcodeName();
Chris Lattner00950542001-06-06 20:29:01 +00001767
Eli Friedmanf03bb262011-08-12 22:50:01 +00001768 // If this is an atomic load or store, print out the atomic marker.
1769 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isAtomic()) ||
1770 (isa<StoreInst>(I) && cast<StoreInst>(I).isAtomic()))
1771 Out << " atomic";
1772
1773 // If this is a volatile operation, print out the volatile marker.
1774 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) ||
1775 (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile()) ||
1776 (isa<AtomicCmpXchgInst>(I) && cast<AtomicCmpXchgInst>(I).isVolatile()) ||
1777 (isa<AtomicRMWInst>(I) && cast<AtomicRMWInst>(I).isVolatile()))
1778 Out << " volatile";
1779
Dan Gohman59858cf2009-07-27 16:11:46 +00001780 // Print out optimization information.
1781 WriteOptimizationInfo(Out, &I);
1782
Reid Spencer74f16422006-12-03 06:27:29 +00001783 // Print out the compare instruction predicates
Nate Begemanac80ade2008-05-12 19:01:56 +00001784 if (const CmpInst *CI = dyn_cast<CmpInst>(&I))
Chris Lattnerab49ee72008-08-23 22:52:27 +00001785 Out << ' ' << getPredicateText(CI->getPredicate());
Reid Spencer74f16422006-12-03 06:27:29 +00001786
Eli Friedmanff030482011-07-28 21:48:00 +00001787 // Print out the atomicrmw operation
1788 if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(&I))
1789 writeAtomicRMWOperation(Out, RMWI->getOperation());
1790
Chris Lattner00950542001-06-06 20:29:01 +00001791 // Print out the type of the operands...
Chris Lattner7e708292002-06-25 16:13:24 +00001792 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0;
Chris Lattner00950542001-06-06 20:29:01 +00001793
1794 // Special case conditional branches to swizzle the condition out to the front
Gabor Greifccd27fb2009-02-09 15:45:06 +00001795 if (isa<BranchInst>(I) && cast<BranchInst>(I).isConditional()) {
David Blaikief12b3792013-02-11 01:16:51 +00001796 const BranchInst &BI(cast<BranchInst>(I));
Dan Gohman8dae1382008-09-14 17:21:12 +00001797 Out << ' ';
Gabor Greifccd27fb2009-02-09 15:45:06 +00001798 writeOperand(BI.getCondition(), true);
Dan Gohman8dae1382008-09-14 17:21:12 +00001799 Out << ", ";
Gabor Greifccd27fb2009-02-09 15:45:06 +00001800 writeOperand(BI.getSuccessor(0), true);
Dan Gohman8dae1382008-09-14 17:21:12 +00001801 Out << ", ";
Gabor Greifccd27fb2009-02-09 15:45:06 +00001802 writeOperand(BI.getSuccessor(1), true);
Chris Lattner00950542001-06-06 20:29:01 +00001803
Chris Lattner94dc1f22002-04-13 18:34:38 +00001804 } else if (isa<SwitchInst>(I)) {
David Blaikief12b3792013-02-11 01:16:51 +00001805 const SwitchInst& SI(cast<SwitchInst>(I));
Chris Lattnerf9be95f2009-10-27 19:13:16 +00001806 // Special case switch instruction to get formatting nice and correct.
Dan Gohman8dae1382008-09-14 17:21:12 +00001807 Out << ' ';
Eli Friedmanbb5a7442011-09-29 20:21:17 +00001808 writeOperand(SI.getCondition(), true);
Dan Gohman8dae1382008-09-14 17:21:12 +00001809 Out << ", ";
Eli Friedmanbb5a7442011-09-29 20:21:17 +00001810 writeOperand(SI.getDefaultDest(), true);
Chris Lattnerab49ee72008-08-23 22:52:27 +00001811 Out << " [";
David Blaikief12b3792013-02-11 01:16:51 +00001812 for (SwitchInst::ConstCaseIt i = SI.case_begin(), e = SI.case_end();
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00001813 i != e; ++i) {
Dan Gohman01889ca2009-08-13 01:41:52 +00001814 Out << "\n ";
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00001815 writeOperand(i.getCaseValue(), true);
Dan Gohman8dae1382008-09-14 17:21:12 +00001816 Out << ", ";
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00001817 writeOperand(i.getCaseSuccessor(), true);
Chris Lattner00950542001-06-06 20:29:01 +00001818 }
Dan Gohman01889ca2009-08-13 01:41:52 +00001819 Out << "\n ]";
Chris Lattnerab21db72009-10-28 00:19:10 +00001820 } else if (isa<IndirectBrInst>(I)) {
1821 // Special case indirectbr instruction to get formatting nice and correct.
Chris Lattnerf9be95f2009-10-27 19:13:16 +00001822 Out << ' ';
1823 writeOperand(Operand, true);
Dan Gohman0ed1f422009-10-30 02:01:10 +00001824 Out << ", [";
Andrew Trick18801ec2011-09-30 19:48:58 +00001825
Chris Lattnerf9be95f2009-10-27 19:13:16 +00001826 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) {
1827 if (i != 1)
1828 Out << ", ";
1829 writeOperand(I.getOperand(i), true);
1830 }
1831 Out << ']';
Jay Foadc1371202011-06-20 14:18:48 +00001832 } else if (const PHINode *PN = dyn_cast<PHINode>(&I)) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001833 Out << ' ';
Chris Lattner0f7364b2009-02-28 21:26:53 +00001834 TypePrinter.print(I.getType(), Out);
Misha Brukman0313e0b2004-06-21 21:53:56 +00001835 Out << ' ';
Chris Lattner00950542001-06-06 20:29:01 +00001836
Jay Foadc1371202011-06-20 14:18:48 +00001837 for (unsigned op = 0, Eop = PN->getNumIncomingValues(); op < Eop; ++op) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001838 if (op) Out << ", ";
Dan Gohman8dae1382008-09-14 17:21:12 +00001839 Out << "[ ";
Jay Foadc1371202011-06-20 14:18:48 +00001840 writeOperand(PN->getIncomingValue(op), false); Out << ", ";
1841 writeOperand(PN->getIncomingBlock(op), false); Out << " ]";
Chris Lattnerc24d2082001-06-11 15:04:20 +00001842 }
Dan Gohman995be7d2008-05-31 19:12:39 +00001843 } else if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(&I)) {
Dan Gohman8dae1382008-09-14 17:21:12 +00001844 Out << ' ';
Dan Gohman995be7d2008-05-31 19:12:39 +00001845 writeOperand(I.getOperand(0), true);
1846 for (const unsigned *i = EVI->idx_begin(), *e = EVI->idx_end(); i != e; ++i)
1847 Out << ", " << *i;
1848 } else if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(&I)) {
Dan Gohman8dae1382008-09-14 17:21:12 +00001849 Out << ' ';
1850 writeOperand(I.getOperand(0), true); Out << ", ";
Dan Gohman995be7d2008-05-31 19:12:39 +00001851 writeOperand(I.getOperand(1), true);
1852 for (const unsigned *i = IVI->idx_begin(), *e = IVI->idx_end(); i != e; ++i)
1853 Out << ", " << *i;
Bill Wendlinge6e88262011-08-12 20:24:12 +00001854 } else if (const LandingPadInst *LPI = dyn_cast<LandingPadInst>(&I)) {
1855 Out << ' ';
1856 TypePrinter.print(I.getType(), Out);
1857 Out << " personality ";
1858 writeOperand(I.getOperand(0), true); Out << '\n';
1859
1860 if (LPI->isCleanup())
1861 Out << " cleanup";
1862
1863 for (unsigned i = 0, e = LPI->getNumClauses(); i != e; ++i) {
1864 if (i != 0 || LPI->isCleanup()) Out << "\n";
1865 if (LPI->isCatch(i))
1866 Out << " catch ";
1867 else
1868 Out << " filter ";
1869
1870 writeOperand(LPI->getClause(i), true);
1871 }
Devang Patel57ef4f42008-02-23 00:35:18 +00001872 } else if (isa<ReturnInst>(I) && !Operand) {
1873 Out << " void";
Chris Lattnerd5118982005-05-06 20:26:43 +00001874 } else if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
1875 // Print the calling convention being used.
Micah Villmowd3766df2012-09-13 15:11:12 +00001876 if (CI->getCallingConv() != CallingConv::C) {
1877 Out << " ";
1878 PrintCallingConv(CI->getCallingConv(), Out);
Chris Lattnerd5118982005-05-06 20:26:43 +00001879 }
1880
Gabor Greif7bbdf0c2010-06-23 13:09:06 +00001881 Operand = CI->getCalledValue();
Chris Lattner1afcace2011-07-09 17:41:24 +00001882 PointerType *PTy = cast<PointerType>(Operand->getType());
1883 FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1884 Type *RetTy = FTy->getReturnType();
Bill Wendling99faa3b2012-12-07 23:16:57 +00001885 const AttributeSet &PAL = CI->getAttributes();
Chris Lattner268de042001-11-06 21:28:12 +00001886
Bill Wendling1b0c54f2013-01-18 21:53:16 +00001887 if (PAL.hasAttributes(AttributeSet::ReturnIndex))
1888 Out << ' ' << PAL.getAsString(AttributeSet::ReturnIndex);
Devang Patel652203f2008-09-29 20:49:50 +00001889
Chris Lattner7a012292003-08-05 15:34:45 +00001890 // If possible, print out the short form of the call instruction. We can
Chris Lattnerb5794002002-04-07 22:49:37 +00001891 // only do this if the first argument is a pointer to a nonvararg function,
Chris Lattner7a012292003-08-05 15:34:45 +00001892 // and if the return type is not a pointer to a function.
Chris Lattner268de042001-11-06 21:28:12 +00001893 //
Dan Gohman8dae1382008-09-14 17:21:12 +00001894 Out << ' ';
Chris Lattner7a012292003-08-05 15:34:45 +00001895 if (!FTy->isVarArg() &&
Duncan Sands1df98592010-02-16 11:11:14 +00001896 (!RetTy->isPointerTy() ||
1897 !cast<PointerType>(RetTy)->getElementType()->isFunctionTy())) {
Chris Lattner0f7364b2009-02-28 21:26:53 +00001898 TypePrinter.print(RetTy, Out);
Dan Gohman8dae1382008-09-14 17:21:12 +00001899 Out << ' ';
Chris Lattner268de042001-11-06 21:28:12 +00001900 writeOperand(Operand, false);
1901 } else {
1902 writeOperand(Operand, true);
1903 }
Misha Brukman0313e0b2004-06-21 21:53:56 +00001904 Out << '(';
Gabor Greif7bbdf0c2010-06-23 13:09:06 +00001905 for (unsigned op = 0, Eop = CI->getNumArgOperands(); op < Eop; ++op) {
1906 if (op > 0)
Dan Gohman8dae1382008-09-14 17:21:12 +00001907 Out << ", ";
Bill Wendling94e94b32012-12-30 13:50:49 +00001908 writeParamOperand(CI->getArgOperand(op), PAL, op + 1);
Chris Lattner00950542001-06-06 20:29:01 +00001909 }
Dan Gohman8dae1382008-09-14 17:21:12 +00001910 Out << ')';
Bill Wendling831737d2012-12-30 10:32:01 +00001911 if (PAL.hasAttributes(AttributeSet::FunctionIndex))
Bill Wendling351b7a12013-02-22 09:09:42 +00001912 Out << " #" << Machine.getAttributeGroupSlot(PAL.getFnAttributes());
Chris Lattner7e708292002-06-25 16:13:24 +00001913 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
Gabor Greifc9f75002010-03-24 13:21:49 +00001914 Operand = II->getCalledValue();
Chris Lattner1afcace2011-07-09 17:41:24 +00001915 PointerType *PTy = cast<PointerType>(Operand->getType());
1916 FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1917 Type *RetTy = FTy->getReturnType();
Bill Wendling99faa3b2012-12-07 23:16:57 +00001918 const AttributeSet &PAL = II->getAttributes();
Chris Lattner7a012292003-08-05 15:34:45 +00001919
Chris Lattnerd5118982005-05-06 20:26:43 +00001920 // Print the calling convention being used.
Micah Villmowd3766df2012-09-13 15:11:12 +00001921 if (II->getCallingConv() != CallingConv::C) {
1922 Out << " ";
1923 PrintCallingConv(II->getCallingConv(), Out);
Chris Lattnerd5118982005-05-06 20:26:43 +00001924 }
1925
Bill Wendling1b0c54f2013-01-18 21:53:16 +00001926 if (PAL.hasAttributes(AttributeSet::ReturnIndex))
1927 Out << ' ' << PAL.getAsString(AttributeSet::ReturnIndex);
Devang Patel652203f2008-09-29 20:49:50 +00001928
Chris Lattner7a012292003-08-05 15:34:45 +00001929 // If possible, print out the short form of the invoke instruction. We can
1930 // only do this if the first argument is a pointer to a nonvararg function,
1931 // and if the return type is not a pointer to a function.
1932 //
Dan Gohman2b6c3d92008-10-15 18:02:08 +00001933 Out << ' ';
Chris Lattner7a012292003-08-05 15:34:45 +00001934 if (!FTy->isVarArg() &&
Duncan Sands1df98592010-02-16 11:11:14 +00001935 (!RetTy->isPointerTy() ||
1936 !cast<PointerType>(RetTy)->getElementType()->isFunctionTy())) {
Chris Lattner0f7364b2009-02-28 21:26:53 +00001937 TypePrinter.print(RetTy, Out);
Dan Gohman2b6c3d92008-10-15 18:02:08 +00001938 Out << ' ';
Chris Lattner7a012292003-08-05 15:34:45 +00001939 writeOperand(Operand, false);
1940 } else {
1941 writeOperand(Operand, true);
1942 }
Misha Brukman0313e0b2004-06-21 21:53:56 +00001943 Out << '(';
Gabor Greif7bbdf0c2010-06-23 13:09:06 +00001944 for (unsigned op = 0, Eop = II->getNumArgOperands(); op < Eop; ++op) {
Gabor Greifc9f75002010-03-24 13:21:49 +00001945 if (op)
Dan Gohman8dae1382008-09-14 17:21:12 +00001946 Out << ", ";
Bill Wendling94e94b32012-12-30 13:50:49 +00001947 writeParamOperand(II->getArgOperand(op), PAL, op + 1);
Chris Lattnere02fa852001-10-13 06:42:36 +00001948 }
1949
Dan Gohman8dae1382008-09-14 17:21:12 +00001950 Out << ')';
Bill Wendling831737d2012-12-30 10:32:01 +00001951 if (PAL.hasAttributes(AttributeSet::FunctionIndex))
Bill Wendling351b7a12013-02-22 09:09:42 +00001952 Out << " #" << Machine.getAttributeGroupSlot(PAL.getFnAttributes());
Devang Patel19c87462008-09-26 22:53:05 +00001953
Dan Gohman01889ca2009-08-13 01:41:52 +00001954 Out << "\n to ";
Chris Lattnere02fa852001-10-13 06:42:36 +00001955 writeOperand(II->getNormalDest(), true);
Dan Gohman8dae1382008-09-14 17:21:12 +00001956 Out << " unwind ";
Chris Lattneraeb2a1d2004-02-08 21:44:31 +00001957 writeOperand(II->getUnwindDest(), true);
Chris Lattnere02fa852001-10-13 06:42:36 +00001958
Victor Hernandez7b929da2009-10-23 21:09:37 +00001959 } else if (const AllocaInst *AI = dyn_cast<AllocaInst>(&I)) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001960 Out << ' ';
Dan Gohmand3da6d52013-02-08 22:01:47 +00001961 TypePrinter.print(AI->getAllocatedType(), Out);
Dan Gohman69bff072009-07-31 18:23:24 +00001962 if (!AI->getArraySize() || AI->isArrayAllocation()) {
Dan Gohman8dae1382008-09-14 17:21:12 +00001963 Out << ", ";
Chris Lattner94dc1f22002-04-13 18:34:38 +00001964 writeOperand(AI->getArraySize(), true);
Chris Lattner00950542001-06-06 20:29:01 +00001965 }
Nate Begeman14b05292005-11-05 09:21:28 +00001966 if (AI->getAlignment()) {
Chris Lattner9fad0b92005-11-05 21:20:34 +00001967 Out << ", align " << AI->getAlignment();
Nate Begeman14b05292005-11-05 09:21:28 +00001968 }
Chris Lattnere02fa852001-10-13 06:42:36 +00001969 } else if (isa<CastInst>(I)) {
Dan Gohman8dae1382008-09-14 17:21:12 +00001970 if (Operand) {
1971 Out << ' ';
1972 writeOperand(Operand, true); // Work with broken code
1973 }
Misha Brukman0313e0b2004-06-21 21:53:56 +00001974 Out << " to ";
Chris Lattner0f7364b2009-02-28 21:26:53 +00001975 TypePrinter.print(I.getType(), Out);
Chris Lattner4d45bd02003-10-18 05:57:43 +00001976 } else if (isa<VAArgInst>(I)) {
Dan Gohman8dae1382008-09-14 17:21:12 +00001977 if (Operand) {
1978 Out << ' ';
1979 writeOperand(Operand, true); // Work with broken code
1980 }
Misha Brukman0313e0b2004-06-21 21:53:56 +00001981 Out << ", ";
Chris Lattner0f7364b2009-02-28 21:26:53 +00001982 TypePrinter.print(I.getType(), Out);
1983 } else if (Operand) { // Print the normal way.
Chris Lattner00950542001-06-06 20:29:01 +00001984
Misha Brukmanfd939082005-04-21 23:48:37 +00001985 // PrintAllTypes - Instructions who have operands of all the same type
Chris Lattner00950542001-06-06 20:29:01 +00001986 // omit the type from all but the first operand. If the instruction has
1987 // different type operands (for example br), then they are all printed.
1988 bool PrintAllTypes = false;
Chris Lattner1afcace2011-07-09 17:41:24 +00001989 Type *TheType = Operand->getType();
Chris Lattner00950542001-06-06 20:29:01 +00001990
Reid Spencerebe57e32007-02-02 13:54:55 +00001991 // Select, Store and ShuffleVector always print all types.
Devang Patel64947682008-03-04 22:05:14 +00001992 if (isa<SelectInst>(I) || isa<StoreInst>(I) || isa<ShuffleVectorInst>(I)
1993 || isa<ReturnInst>(I)) {
Chris Lattnerffd9bf42003-04-16 20:20:02 +00001994 PrintAllTypes = true;
1995 } else {
1996 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
1997 Operand = I.getOperand(i);
Nuno Lopes6ad2b2a2009-01-15 18:40:57 +00001998 // note that Operand shouldn't be null, but the test helps make dump()
1999 // more tolerant of malformed IR
Nuno Lopesa8c78a92009-01-14 17:51:41 +00002000 if (Operand && Operand->getType() != TheType) {
Chris Lattnerffd9bf42003-04-16 20:20:02 +00002001 PrintAllTypes = true; // We have differing types! Print them all!
2002 break;
2003 }
Chris Lattner00950542001-06-06 20:29:01 +00002004 }
2005 }
Misha Brukmanfd939082005-04-21 23:48:37 +00002006
Chris Lattnerc1824992001-10-29 16:05:51 +00002007 if (!PrintAllTypes) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00002008 Out << ' ';
Chris Lattner0f7364b2009-02-28 21:26:53 +00002009 TypePrinter.print(TheType, Out);
Chris Lattnerc1824992001-10-29 16:05:51 +00002010 }
Chris Lattner00950542001-06-06 20:29:01 +00002011
Dan Gohman8dae1382008-09-14 17:21:12 +00002012 Out << ' ';
Chris Lattner7e708292002-06-25 16:13:24 +00002013 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
Dan Gohman8dae1382008-09-14 17:21:12 +00002014 if (i) Out << ", ";
Chris Lattner7e708292002-06-25 16:13:24 +00002015 writeOperand(I.getOperand(i), PrintAllTypes);
Chris Lattner00950542001-06-06 20:29:01 +00002016 }
2017 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002018
Eli Friedman21006d42011-08-09 23:02:53 +00002019 // Print atomic ordering/alignment for memory operations
2020 if (const LoadInst *LI = dyn_cast<LoadInst>(&I)) {
2021 if (LI->isAtomic())
2022 writeAtomic(LI->getOrdering(), LI->getSynchScope());
2023 if (LI->getAlignment())
2024 Out << ", align " << LI->getAlignment();
2025 } else if (const StoreInst *SI = dyn_cast<StoreInst>(&I)) {
2026 if (SI->isAtomic())
2027 writeAtomic(SI->getOrdering(), SI->getSynchScope());
2028 if (SI->getAlignment())
2029 Out << ", align " << SI->getAlignment();
Eli Friedmanff030482011-07-28 21:48:00 +00002030 } else if (const AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(&I)) {
2031 writeAtomic(CXI->getOrdering(), CXI->getSynchScope());
2032 } else if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(&I)) {
2033 writeAtomic(RMWI->getOrdering(), RMWI->getSynchScope());
Eli Friedman47f35132011-07-25 23:16:38 +00002034 } else if (const FenceInst *FI = dyn_cast<FenceInst>(&I)) {
2035 writeAtomic(FI->getOrdering(), FI->getSynchScope());
Christopher Lamb43c7f372007-04-22 19:24:39 +00002036 }
Chris Lattner00950542001-06-06 20:29:01 +00002037
Chris Lattner7d05c462009-12-28 20:10:43 +00002038 // Print Metadata info.
Nick Lewyckyfa0c54e2010-02-25 06:53:04 +00002039 SmallVector<std::pair<unsigned, MDNode*>, 4> InstMD;
2040 I.getAllMetadata(InstMD);
Erick Tryzelaarf3d5c912010-03-02 05:32:52 +00002041 if (!InstMD.empty()) {
2042 SmallVector<StringRef, 8> MDNames;
2043 I.getType()->getContext().getMDKindNames(MDNames);
2044 for (unsigned i = 0, e = InstMD.size(); i != e; ++i) {
2045 unsigned Kind = InstMD[i].first;
2046 if (Kind < MDNames.size()) {
2047 Out << ", !" << MDNames[Kind];
Daniel Malead0fef322013-05-08 20:38:31 +00002048 } else {
2049 Out << ", !<unknown kind #" << Kind << ">";
2050 }
Dan Gohman3bdfbf52010-07-20 23:55:01 +00002051 Out << ' ';
2052 WriteAsOperandInternal(Out, InstMD[i].second, &TypePrinter, &Machine,
2053 TheModule);
Nick Lewyckyfa0c54e2010-02-25 06:53:04 +00002054 }
Devang Patel7f93f4d2009-10-07 16:37:55 +00002055 }
Chris Lattnere02fa852001-10-13 06:42:36 +00002056 printInfoComment(I);
Chris Lattner00950542001-06-06 20:29:01 +00002057}
2058
Chris Lattner6e6b1802009-12-31 02:13:35 +00002059static void WriteMDNodeComment(const MDNode *Node,
Duncan Sands34727662010-07-12 08:16:59 +00002060 formatted_raw_ostream &Out) {
Chris Lattner6e6b1802009-12-31 02:13:35 +00002061 if (Node->getNumOperands() < 1)
2062 return;
Bill Wendling58a6cf22012-06-28 00:41:44 +00002063
2064 Value *Op = Node->getOperand(0);
2065 if (!Op || !isa<ConstantInt>(Op) || cast<ConstantInt>(Op)->getBitWidth() < 32)
Chris Lattner6e6b1802009-12-31 02:13:35 +00002066 return;
Andrew Trick18801ec2011-09-30 19:48:58 +00002067
Bill Wendling58a6cf22012-06-28 00:41:44 +00002068 DIDescriptor Desc(Node);
David Blaikiec0ec8a42013-03-11 23:39:23 +00002069 if (!Desc.Verify())
Bill Wendling58a6cf22012-06-28 00:41:44 +00002070 return;
2071
2072 unsigned Tag = Desc.getTag();
Chris Lattner6e6b1802009-12-31 02:13:35 +00002073 Out.PadToColumn(50);
Bill Wendling86b032b2012-07-03 20:01:02 +00002074 if (dwarf::TagString(Tag)) {
2075 Out << "; ";
2076 Desc.print(Out);
2077 } else if (Tag == dwarf::DW_TAG_user_base) {
Chris Lattner6e6b1802009-12-31 02:13:35 +00002078 Out << "; [ DW_TAG_user_base ]";
Bill Wendling86b032b2012-07-03 20:01:02 +00002079 }
Chris Lattner6e6b1802009-12-31 02:13:35 +00002080}
2081
Daniel Malead0fef322013-05-08 20:38:31 +00002082void AssemblyWriter::writeMDNode(unsigned Slot, const MDNode *Node) {
2083 Out << '!' << Slot << " = metadata ";
2084 printMDNodeBody(Node);
2085}
2086
Chris Lattner6e6b1802009-12-31 02:13:35 +00002087void AssemblyWriter::writeAllMDNodes() {
2088 SmallVector<const MDNode *, 16> Nodes;
Chris Lattner307c9892009-12-31 02:20:11 +00002089 Nodes.resize(Machine.mdn_size());
2090 for (SlotTracker::mdn_iterator I = Machine.mdn_begin(), E = Machine.mdn_end();
2091 I != E; ++I)
Chris Lattner6e6b1802009-12-31 02:13:35 +00002092 Nodes[I->second] = cast<MDNode>(I->first);
Andrew Trick18801ec2011-09-30 19:48:58 +00002093
Chris Lattner6e6b1802009-12-31 02:13:35 +00002094 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Daniel Malead0fef322013-05-08 20:38:31 +00002095 writeMDNode(i, Nodes[i]);
Chris Lattner6e6b1802009-12-31 02:13:35 +00002096 }
2097}
2098
2099void AssemblyWriter::printMDNodeBody(const MDNode *Node) {
Dan Gohman3bdfbf52010-07-20 23:55:01 +00002100 WriteMDNodeBodyInternal(Out, Node, &TypePrinter, &Machine, TheModule);
Chris Lattner6e6b1802009-12-31 02:13:35 +00002101 WriteMDNodeComment(Node, Out);
2102 Out << "\n";
2103}
Chris Lattner00950542001-06-06 20:29:01 +00002104
Bill Wendlingb29ce262013-02-11 08:43:33 +00002105void AssemblyWriter::writeAllAttributeGroups() {
2106 std::vector<std::pair<AttributeSet, unsigned> > asVec;
2107 asVec.resize(Machine.as_size());
2108
2109 for (SlotTracker::as_iterator I = Machine.as_begin(), E = Machine.as_end();
2110 I != E; ++I)
2111 asVec[I->second] = *I;
2112
2113 for (std::vector<std::pair<AttributeSet, unsigned> >::iterator
2114 I = asVec.begin(), E = asVec.end(); I != E; ++I)
2115 Out << "attributes #" << I->second << " = { "
Rafael Espindolaaae02982013-05-01 13:07:03 +00002116 << I->first.getAsString(AttributeSet::FunctionIndex, true) << " }\n";
Bill Wendlingb29ce262013-02-11 08:43:33 +00002117}
2118
Daniel Malead0fef322013-05-08 20:38:31 +00002119} // namespace llvm
2120
Chris Lattner00950542001-06-06 20:29:01 +00002121//===----------------------------------------------------------------------===//
2122// External Interface declarations
2123//===----------------------------------------------------------------------===//
2124
Dan Gohman683e9222009-08-12 17:23:50 +00002125void Module::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW) const {
Chris Lattner0d9574a2008-08-19 04:26:57 +00002126 SlotTracker SlotTable(this);
Dan Gohman683e9222009-08-12 17:23:50 +00002127 formatted_raw_ostream OS(ROS);
Chris Lattner944fac72008-08-23 22:23:09 +00002128 AssemblyWriter W(OS, SlotTable, this, AAW);
Chris Lattnerbd72b322009-12-31 02:23:35 +00002129 W.printModule(this);
Chris Lattner00950542001-06-06 20:29:01 +00002130}
2131
Dan Gohman17aa92c2010-07-21 23:38:33 +00002132void NamedMDNode::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW) const {
2133 SlotTracker SlotTable(getParent());
2134 formatted_raw_ostream OS(ROS);
2135 AssemblyWriter W(OS, SlotTable, getParent(), AAW);
2136 W.printNamedMDNode(this);
2137}
2138
Chris Lattner6d4306e2009-02-28 21:11:05 +00002139void Type::print(raw_ostream &OS) const {
2140 if (this == 0) {
2141 OS << "<null Type>";
2142 return;
2143 }
Chris Lattner1afcace2011-07-09 17:41:24 +00002144 TypePrinting TP;
2145 TP.print(const_cast<Type*>(this), OS);
Andrew Trick18801ec2011-09-30 19:48:58 +00002146
Chris Lattner1afcace2011-07-09 17:41:24 +00002147 // If the type is a named struct type, print the body as well.
2148 if (StructType *STy = dyn_cast<StructType>(const_cast<Type*>(this)))
Chris Lattnerc4d0e9f2011-08-12 18:07:07 +00002149 if (!STy->isLiteral()) {
Chris Lattner1afcace2011-07-09 17:41:24 +00002150 OS << " = type ";
2151 TP.printStructBody(STy, OS);
2152 }
Chris Lattner75cf7cf2002-04-08 22:03:40 +00002153}
2154
Dan Gohman683e9222009-08-12 17:23:50 +00002155void Value::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW) const {
Chris Lattner944fac72008-08-23 22:23:09 +00002156 if (this == 0) {
Dan Gohman1220e102009-08-12 20:56:03 +00002157 ROS << "printing a <null> value\n";
Chris Lattner944fac72008-08-23 22:23:09 +00002158 return;
2159 }
Dan Gohman1220e102009-08-12 20:56:03 +00002160 formatted_raw_ostream OS(ROS);
Chris Lattner944fac72008-08-23 22:23:09 +00002161 if (const Instruction *I = dyn_cast<Instruction>(this)) {
2162 const Function *F = I->getParent() ? I->getParent()->getParent() : 0;
2163 SlotTracker SlotTable(F);
Chris Lattnerdbe85bf2009-12-31 08:23:09 +00002164 AssemblyWriter W(OS, SlotTable, getModuleFromVal(I), AAW);
Chris Lattnerbd72b322009-12-31 02:23:35 +00002165 W.printInstruction(*I);
Chris Lattner944fac72008-08-23 22:23:09 +00002166 } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(this)) {
2167 SlotTracker SlotTable(BB->getParent());
Chris Lattner6e6b1802009-12-31 02:13:35 +00002168 AssemblyWriter W(OS, SlotTable, getModuleFromVal(BB), AAW);
Chris Lattnerbd72b322009-12-31 02:23:35 +00002169 W.printBasicBlock(BB);
Chris Lattner944fac72008-08-23 22:23:09 +00002170 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(this)) {
2171 SlotTracker SlotTable(GV->getParent());
Dan Gohmanba0941f2009-04-20 16:10:33 +00002172 AssemblyWriter W(OS, SlotTable, GV->getParent(), AAW);
Chris Lattnerbd72b322009-12-31 02:23:35 +00002173 if (const GlobalVariable *V = dyn_cast<GlobalVariable>(GV))
2174 W.printGlobal(V);
2175 else if (const Function *F = dyn_cast<Function>(GV))
2176 W.printFunction(F);
2177 else
2178 W.printAlias(cast<GlobalAlias>(GV));
Devang Patelfcd65ae2009-07-01 20:59:15 +00002179 } else if (const MDNode *N = dyn_cast<MDNode>(this)) {
Victor Hernandez8fffff52010-01-20 04:45:57 +00002180 const Function *F = N->getFunction();
Victor Hernandez559588b2010-01-14 01:47:37 +00002181 SlotTracker SlotTable(F);
Dan Gohman79b78a42010-07-14 21:12:44 +00002182 AssemblyWriter W(OS, SlotTable, F ? F->getParent() : 0, AAW);
Chris Lattner6e6b1802009-12-31 02:13:35 +00002183 W.printMDNodeBody(N);
Chris Lattner944fac72008-08-23 22:23:09 +00002184 } else if (const Constant *C = dyn_cast<Constant>(this)) {
Chris Lattnere9fa33e2009-02-28 23:20:19 +00002185 TypePrinting TypePrinter;
Chris Lattner0f7364b2009-02-28 21:26:53 +00002186 TypePrinter.print(C->getType(), OS);
Chris Lattner6d4306e2009-02-28 21:11:05 +00002187 OS << ' ';
Dan Gohman3bdfbf52010-07-20 23:55:01 +00002188 WriteConstantInternal(OS, C, TypePrinter, 0, 0);
Chris Lattner4a3d3a52009-12-31 01:41:14 +00002189 } else if (isa<InlineAsm>(this) || isa<MDString>(this) ||
2190 isa<Argument>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00002191 WriteAsOperand(OS, this, true, 0);
2192 } else {
Dan Gohmancd26ec52009-09-23 01:33:16 +00002193 // Otherwise we don't know what it is. Call the virtual function to
2194 // allow a subclass to print itself.
2195 printCustom(OS);
Chris Lattner944fac72008-08-23 22:23:09 +00002196 }
2197}
2198
Dan Gohmancd26ec52009-09-23 01:33:16 +00002199// Value::printCustom - subclasses should override this to implement printing.
2200void Value::printCustom(raw_ostream &OS) const {
2201 llvm_unreachable("Unknown value to print out!");
2202}
2203
Chris Lattner7059e532008-08-25 17:03:15 +00002204// Value::dump - allow easy printing of Values from the debugger.
David Greened865e022010-01-05 01:29:26 +00002205void Value::dump() const { print(dbgs()); dbgs() << '\n'; }
Reid Spencerfa452c02004-05-25 18:14:38 +00002206
Chris Lattner7059e532008-08-25 17:03:15 +00002207// Type::dump - allow easy printing of Types from the debugger.
Chris Lattner1afcace2011-07-09 17:41:24 +00002208void Type::dump() const { print(dbgs()); }
Chris Lattnerc2871372009-02-28 21:05:51 +00002209
Chris Lattner7059e532008-08-25 17:03:15 +00002210// Module::dump() - Allow printing of Modules from the debugger.
David Greened865e022010-01-05 01:29:26 +00002211void Module::dump() const { print(dbgs(), 0); }
Bill Wendlingf4374e42011-12-09 23:18:34 +00002212
2213// NamedMDNode::dump() - Allow printing of NamedMDNodes from the debugger.
2214void NamedMDNode::dump() const { print(dbgs(), 0); }