blob: e6cd418c321bce00d1766e71379e83f3dbcce7fd [file] [log] [blame]
Chris Lattnerf7e79482002-04-07 22:31:46 +00001//===-- AsmWriter.cpp - Printing LLVM as an assembly file -----------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
10// This library implements the functionality defined in llvm/Assembly/Writer.h
11//
Chris Lattner189088e2002-04-12 18:21:53 +000012// Note that these routines must be extremely tolerant of various errors in the
Chris Lattnerf70da102003-05-08 02:44:12 +000013// LLVM code, because it can be used for debugging transformations.
Chris Lattner189088e2002-04-12 18:21:53 +000014//
Chris Lattner2f7c9632001-06-06 20:29:01 +000015//===----------------------------------------------------------------------===//
16
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +000017#include "llvm/Assembly/Writer.h"
Chris Lattner7f8845a2002-07-23 18:07:49 +000018#include "llvm/Assembly/PrintModulePass.h"
Chris Lattnerad074cb2010-09-02 23:09:42 +000019#include "llvm/Assembly/AssemblyAnnotationWriter.h"
Erick Tryzelaar72a37132010-03-02 05:32:52 +000020#include "llvm/LLVMContext.h"
Chris Lattnerf7b6d312005-05-06 20:26:43 +000021#include "llvm/CallingConv.h"
Chris Lattnerc70b3f62004-01-20 19:50:34 +000022#include "llvm/Constants.h"
Chris Lattner913d18f2002-04-29 18:46:50 +000023#include "llvm/DerivedTypes.h"
Chris Lattner8bbcda22006-01-25 18:57:27 +000024#include "llvm/InlineAsm.h"
Chris Lattner2f2aa2b2009-12-28 23:41:32 +000025#include "llvm/IntrinsicInst.h"
Dan Gohman0ebd6962009-07-20 21:19:07 +000026#include "llvm/Operator.h"
Chris Lattnerc70b3f62004-01-20 19:50:34 +000027#include "llvm/Module.h"
Reid Spencer3aaaa0b2007-02-05 20:47:22 +000028#include "llvm/ValueSymbolTable.h"
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000029#include "llvm/ADT/DenseMap.h"
Benjamin Kramerb17c5862010-01-29 14:42:22 +000030#include "llvm/ADT/SmallString.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000031#include "llvm/ADT/StringExtras.h"
32#include "llvm/ADT/STLExtras.h"
Bill Wendlingdfc91892006-11-28 02:09:03 +000033#include "llvm/Support/CFG.h"
David Greenec7f9b122010-01-05 01:29:26 +000034#include "llvm/Support/Debug.h"
Devang Patel711ab5b2009-09-30 20:16:54 +000035#include "llvm/Support/Dwarf.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000036#include "llvm/Support/ErrorHandling.h"
Jim Laskeyb74c6662005-08-17 19:34:49 +000037#include "llvm/Support/MathExtras.h"
Dan Gohmane2745262009-08-12 17:23:50 +000038#include "llvm/Support/FormattedStream.h"
Chris Lattnerfee714f2001-09-07 16:36:04 +000039#include <algorithm>
Reid Spencerbdf03b42007-05-22 19:27:35 +000040#include <cctype>
Chris Lattner189d19f2003-11-21 20:23:48 +000041using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000042
Reid Spencer294715b2005-05-15 16:13:11 +000043// Make virtual table appear in this compilation unit.
44AssemblyAnnotationWriter::~AssemblyAnnotationWriter() {}
45
Chris Lattner3eee99c2008-08-19 04:36:02 +000046//===----------------------------------------------------------------------===//
47// Helper Functions
48//===----------------------------------------------------------------------===//
49
50static const Module *getModuleFromVal(const Value *V) {
51 if (const Argument *MA = dyn_cast<Argument>(V))
52 return MA->getParent() ? MA->getParent()->getParent() : 0;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +000053
Chris Lattner3eee99c2008-08-19 04:36:02 +000054 if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
55 return BB->getParent() ? BB->getParent()->getParent() : 0;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +000056
Chris Lattner3eee99c2008-08-19 04:36:02 +000057 if (const Instruction *I = dyn_cast<Instruction>(V)) {
58 const Function *M = I->getParent() ? I->getParent()->getParent() : 0;
59 return M ? M->getParent() : 0;
60 }
Chris Lattner31fcc282009-12-31 01:41:14 +000061
Chris Lattner3eee99c2008-08-19 04:36:02 +000062 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
63 return GV->getParent();
64 return 0;
65}
66
Daniel Dunbardb0b70a2008-10-28 19:33:02 +000067// PrintEscapedString - Print each character of the specified string, escaping
68// it if it is not printable or if it is an escape char.
Chris Lattner9380b812010-07-07 23:16:37 +000069static void PrintEscapedString(StringRef Name, raw_ostream &Out) {
Daniel Dunbare03eecb2009-07-25 23:55:21 +000070 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
71 unsigned char C = Name[i];
Nick Lewycky5aa592a2009-03-15 06:39:52 +000072 if (isprint(C) && C != '\\' && C != '"')
Daniel Dunbardb0b70a2008-10-28 19:33:02 +000073 Out << C;
74 else
75 Out << '\\' << hexdigit(C >> 4) << hexdigit(C & 0x0F);
76 }
77}
78
Chris Lattner3eee99c2008-08-19 04:36:02 +000079enum PrefixType {
80 GlobalPrefix,
81 LabelPrefix,
Daniel Dunbar389529a2008-10-14 23:28:09 +000082 LocalPrefix,
83 NoPrefix
Chris Lattner3eee99c2008-08-19 04:36:02 +000084};
85
86/// PrintLLVMName - Turn the specified name into an 'LLVM name', which is either
87/// prefixed with % (if the string only contains simple characters) or is
88/// surrounded with ""'s (if it has special chars in it). Print it out.
Benjamin Kramer92d89982010-07-14 22:38:02 +000089static void PrintLLVMName(raw_ostream &OS, StringRef Name, PrefixType Prefix) {
Jay Foadf7adb322011-04-24 14:30:00 +000090 assert(!Name.empty() && "Cannot get empty name!");
Chris Lattner3eee99c2008-08-19 04:36:02 +000091 switch (Prefix) {
Torok Edwinfbcc6632009-07-14 16:55:14 +000092 default: llvm_unreachable("Bad prefix!");
Daniel Dunbar389529a2008-10-14 23:28:09 +000093 case NoPrefix: break;
Chris Lattner1508d3f2008-08-19 05:16:28 +000094 case GlobalPrefix: OS << '@'; break;
95 case LabelPrefix: break;
96 case LocalPrefix: OS << '%'; break;
Nick Lewycky063699a2009-03-19 06:31:22 +000097 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +000098
Chris Lattner3eee99c2008-08-19 04:36:02 +000099 // Scan the name to see if it needs quotes first.
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000100 bool NeedsQuotes = isdigit(Name[0]);
Chris Lattner3eee99c2008-08-19 04:36:02 +0000101 if (!NeedsQuotes) {
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000102 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
103 char C = Name[i];
Chris Lattner3eee99c2008-08-19 04:36:02 +0000104 if (!isalnum(C) && C != '-' && C != '.' && C != '_') {
105 NeedsQuotes = true;
106 break;
107 }
108 }
109 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000110
Chris Lattner3eee99c2008-08-19 04:36:02 +0000111 // If we didn't need any quotes, just write out the name in one blast.
112 if (!NeedsQuotes) {
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000113 OS << Name;
Chris Lattner3eee99c2008-08-19 04:36:02 +0000114 return;
115 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000116
Chris Lattner3eee99c2008-08-19 04:36:02 +0000117 // Okay, we need quotes. Output the quotes and escape any scary characters as
118 // needed.
119 OS << '"';
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000120 PrintEscapedString(Name, OS);
Chris Lattner3eee99c2008-08-19 04:36:02 +0000121 OS << '"';
122}
123
124/// PrintLLVMName - Turn the specified name into an 'LLVM name', which is either
125/// prefixed with % (if the string only contains simple characters) or is
126/// surrounded with ""'s (if it has special chars in it). Print it out.
Dan Gohman12dad632009-08-12 20:56:03 +0000127static void PrintLLVMName(raw_ostream &OS, const Value *V) {
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000128 PrintLLVMName(OS, V->getName(),
Chris Lattner3eee99c2008-08-19 04:36:02 +0000129 isa<GlobalValue>(V) ? GlobalPrefix : LocalPrefix);
130}
131
Chris Lattner0f578952009-02-28 20:25:14 +0000132//===----------------------------------------------------------------------===//
133// TypePrinting Class: Type printing machinery
134//===----------------------------------------------------------------------===//
135
Chris Lattner3a16c712011-06-18 21:23:04 +0000136/// TypePrinting - Type printing machinery.
137namespace {
138class TypePrinting {
Chris Lattner3a16c712011-06-18 21:23:04 +0000139 TypePrinting(const TypePrinting &); // DO NOT IMPLEMENT
140 void operator=(const TypePrinting&); // DO NOT IMPLEMENT
141public:
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000142
143 /// NamedTypes - The named types that are used by the current module.
144 std::vector<StructType*> NamedTypes;
145
146 /// NumberedTypes - The numbered types, along with their value.
147 DenseMap<StructType*, unsigned> NumberedTypes;
148
149
Chris Lattner3a16c712011-06-18 21:23:04 +0000150 TypePrinting() {}
151 ~TypePrinting() {}
152
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000153 void incorporateTypes(const Module &M);
Chris Lattner3a16c712011-06-18 21:23:04 +0000154
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000155 void print(Type *Ty, raw_ostream &OS);
Chris Lattner3a16c712011-06-18 21:23:04 +0000156
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000157 void printStructBody(StructType *Ty, raw_ostream &OS);
Chris Lattner3a16c712011-06-18 21:23:04 +0000158};
159} // end anonymous namespace.
Chris Lattner3ad46822009-02-28 22:34:45 +0000160
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000161
162void TypePrinting::incorporateTypes(const Module &M) {
163 M.findUsedStructTypes(NamedTypes);
164
165 // The list of struct types we got back includes all the struct types, split
166 // the unnamed ones out to a numbering and remove the anonymous structs.
167 unsigned NextNumber = 0;
168
169 std::vector<StructType*>::iterator NextToUse = NamedTypes.begin(), I, E;
170 for (I = NamedTypes.begin(), E = NamedTypes.end(); I != E; ++I) {
171 StructType *STy = *I;
172
173 // Ignore anonymous types.
174 if (STy->isAnonymous())
175 continue;
176
177 if (STy->getName().empty())
178 NumberedTypes[STy] = NextNumber++;
179 else
180 *NextToUse++ = STy;
181 }
182
183 NamedTypes.erase(NextToUse, NamedTypes.end());
184}
185
186
Chris Lattner40959d02009-02-28 20:49:40 +0000187/// CalcTypeName - Write the specified type to the specified raw_ostream, making
188/// use of type names or up references to shorten the type name where possible.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000189void TypePrinting::print(Type *Ty, raw_ostream &OS) {
Chris Lattner0f578952009-02-28 20:25:14 +0000190 switch (Ty->getTypeID()) {
Chris Lattner06a23212009-02-28 21:27:31 +0000191 case Type::VoidTyID: OS << "void"; break;
192 case Type::FloatTyID: OS << "float"; break;
193 case Type::DoubleTyID: OS << "double"; break;
194 case Type::X86_FP80TyID: OS << "x86_fp80"; break;
195 case Type::FP128TyID: OS << "fp128"; break;
196 case Type::PPC_FP128TyID: OS << "ppc_fp128"; break;
197 case Type::LabelTyID: OS << "label"; break;
Nick Lewyckyadbc2842009-05-30 05:06:04 +0000198 case Type::MetadataTyID: OS << "metadata"; break;
Dale Johannesenbaa5d042010-09-10 20:55:01 +0000199 case Type::X86_MMXTyID: OS << "x86_mmx"; break;
Chris Lattner68318b12009-02-28 21:18:43 +0000200 case Type::IntegerTyID:
Chris Lattner06a23212009-02-28 21:27:31 +0000201 OS << 'i' << cast<IntegerType>(Ty)->getBitWidth();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000202 return;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000203
Chris Lattner07d88292009-02-28 20:35:42 +0000204 case Type::FunctionTyID: {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000205 FunctionType *FTy = cast<FunctionType>(Ty);
206 print(FTy->getReturnType(), OS);
Chris Lattner06a23212009-02-28 21:27:31 +0000207 OS << " (";
Chris Lattner07d88292009-02-28 20:35:42 +0000208 for (FunctionType::param_iterator I = FTy->param_begin(),
209 E = FTy->param_end(); I != E; ++I) {
210 if (I != FTy->param_begin())
Chris Lattner06a23212009-02-28 21:27:31 +0000211 OS << ", ";
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000212 print(*I, OS);
Chris Lattner0f578952009-02-28 20:25:14 +0000213 }
Chris Lattner07d88292009-02-28 20:35:42 +0000214 if (FTy->isVarArg()) {
Chris Lattner06a23212009-02-28 21:27:31 +0000215 if (FTy->getNumParams()) OS << ", ";
216 OS << "...";
Chris Lattner0f578952009-02-28 20:25:14 +0000217 }
Chris Lattner06a23212009-02-28 21:27:31 +0000218 OS << ')';
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000219 return;
Chris Lattner07d88292009-02-28 20:35:42 +0000220 }
221 case Type::StructTyID: {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000222 StructType *STy = cast<StructType>(Ty);
223
224 if (STy->isAnonymous())
225 return printStructBody(STy, OS);
226
227 if (!STy->getName().empty())
228 return PrintLLVMName(OS, STy->getName(), LocalPrefix);
229
230 DenseMap<StructType*, unsigned>::iterator I = NumberedTypes.find(STy);
231 if (I != NumberedTypes.end())
232 OS << '%' << I->second;
233 else // Not enumerated, print the hex address.
234 OS << "%\"type 0x" << STy << '\"';
235 return;
Chris Lattner07d88292009-02-28 20:35:42 +0000236 }
237 case Type::PointerTyID: {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000238 PointerType *PTy = cast<PointerType>(Ty);
239 print(PTy->getElementType(), OS);
Chris Lattner07d88292009-02-28 20:35:42 +0000240 if (unsigned AddressSpace = PTy->getAddressSpace())
Chris Lattner06a23212009-02-28 21:27:31 +0000241 OS << " addrspace(" << AddressSpace << ')';
242 OS << '*';
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000243 return;
Chris Lattner07d88292009-02-28 20:35:42 +0000244 }
245 case Type::ArrayTyID: {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000246 ArrayType *ATy = cast<ArrayType>(Ty);
Chris Lattner06a23212009-02-28 21:27:31 +0000247 OS << '[' << ATy->getNumElements() << " x ";
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000248 print(ATy->getElementType(), OS);
Chris Lattner06a23212009-02-28 21:27:31 +0000249 OS << ']';
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000250 return;
Chris Lattner07d88292009-02-28 20:35:42 +0000251 }
252 case Type::VectorTyID: {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000253 VectorType *PTy = cast<VectorType>(Ty);
Chris Lattner06a23212009-02-28 21:27:31 +0000254 OS << "<" << PTy->getNumElements() << " x ";
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000255 print(PTy->getElementType(), OS);
Chris Lattner06a23212009-02-28 21:27:31 +0000256 OS << '>';
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000257 return;
Chris Lattner07d88292009-02-28 20:35:42 +0000258 }
Chris Lattner07d88292009-02-28 20:35:42 +0000259 default:
Chris Lattner06a23212009-02-28 21:27:31 +0000260 OS << "<unrecognized-type>";
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000261 return;
Chris Lattner0f578952009-02-28 20:25:14 +0000262 }
Chris Lattner0f578952009-02-28 20:25:14 +0000263}
264
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000265void TypePrinting::printStructBody(StructType *STy, raw_ostream &OS) {
266 if (STy->isOpaque()) {
267 OS << "opaque";
268 return;
Chris Lattner0f578952009-02-28 20:25:14 +0000269 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000270
271 if (STy->isPacked())
272 OS << '<';
273
274 if (STy->getNumElements() == 0) {
275 OS << "{}";
276 } else {
277 StructType::element_iterator I = STy->element_begin();
278 OS << "{ ";
279 print(*I++, OS);
280 for (StructType::element_iterator E = STy->element_end(); I != E; ++I) {
281 OS << ", ";
282 print(*I, OS);
Chris Lattner3243ea12009-03-01 00:03:38 +0000283 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000284
285 OS << " }";
Chris Lattner92c5c122009-02-28 23:20:19 +0000286 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000287 if (STy->isPacked())
288 OS << '>';
Chris Lattner92c5c122009-02-28 23:20:19 +0000289}
290
Chris Lattner0f578952009-02-28 20:25:14 +0000291
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000292
Chris Lattner3eee99c2008-08-19 04:36:02 +0000293//===----------------------------------------------------------------------===//
294// SlotTracker Class: Enumerate slot numbers for unnamed values
295//===----------------------------------------------------------------------===//
296
Chris Lattner3ee58762008-08-19 04:28:07 +0000297namespace {
298
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000299/// This class provides computation of slot numbers for LLVM Assembly writing.
Chris Lattner393b7cd2008-08-17 04:17:45 +0000300///
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000301class SlotTracker {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000302public:
Devang Patel983c6b12009-07-08 21:44:25 +0000303 /// ValueMap - A mapping of Values to slot numbers.
Chris Lattnera204d412008-08-17 17:25:25 +0000304 typedef DenseMap<const Value*, unsigned> ValueMap;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000305
306private:
Devang Patel983c6b12009-07-08 21:44:25 +0000307 /// TheModule - The module for which we are holding slot numbers.
Chris Lattner393b7cd2008-08-17 04:17:45 +0000308 const Module* TheModule;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000309
Devang Patel983c6b12009-07-08 21:44:25 +0000310 /// TheFunction - The function for which we are holding slot numbers.
Chris Lattner393b7cd2008-08-17 04:17:45 +0000311 const Function* TheFunction;
312 bool FunctionProcessed;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000313
Jay Foaddbb221d2011-07-11 07:28:49 +0000314 /// mMap - The slot map for the module level data.
Chris Lattner393b7cd2008-08-17 04:17:45 +0000315 ValueMap mMap;
316 unsigned mNext;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000317
Jay Foaddbb221d2011-07-11 07:28:49 +0000318 /// fMap - The slot map for the function level data.
Chris Lattner393b7cd2008-08-17 04:17:45 +0000319 ValueMap fMap;
320 unsigned fNext;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000321
Devang Patel983c6b12009-07-08 21:44:25 +0000322 /// mdnMap - Map for MDNodes.
Chris Lattnercf4a76e2009-12-31 02:20:11 +0000323 DenseMap<const MDNode*, unsigned> mdnMap;
Devang Patel983c6b12009-07-08 21:44:25 +0000324 unsigned mdnNext;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000325public:
Chris Lattner393b7cd2008-08-17 04:17:45 +0000326 /// Construct from a module
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000327 explicit SlotTracker(const Module *M);
Chris Lattner393b7cd2008-08-17 04:17:45 +0000328 /// Construct from a function, starting out in incorp state.
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000329 explicit SlotTracker(const Function *F);
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000330
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000331 /// Return the slot number of the specified value in it's type
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000332 /// plane. If something is not in the SlotTracker, return -1.
Chris Lattner5e043322007-01-11 03:54:27 +0000333 int getLocalSlot(const Value *V);
334 int getGlobalSlot(const GlobalValue *V);
Devang Patel983c6b12009-07-08 21:44:25 +0000335 int getMetadataSlot(const MDNode *N);
Reid Spencer8beac692004-06-09 15:26:53 +0000336
Misha Brukmanb1c93172005-04-21 23:48:37 +0000337 /// If you'd like to deal with a function instead of just a module, use
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000338 /// this method to get its data into the SlotTracker.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000339 void incorporateFunction(const Function *F) {
340 TheFunction = F;
Reid Spencerb0ac8c42004-08-16 07:46:33 +0000341 FunctionProcessed = false;
342 }
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000343
Misha Brukmanb1c93172005-04-21 23:48:37 +0000344 /// After calling incorporateFunction, use this method to remove the
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000345 /// most recently incorporated function from the SlotTracker. This
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000346 /// will reset the state of the machine back to just the module contents.
347 void purgeFunction();
348
Devang Patel983c6b12009-07-08 21:44:25 +0000349 /// MDNode map iterators.
Chris Lattnercf4a76e2009-12-31 02:20:11 +0000350 typedef DenseMap<const MDNode*, unsigned>::iterator mdn_iterator;
351 mdn_iterator mdn_begin() { return mdnMap.begin(); }
352 mdn_iterator mdn_end() { return mdnMap.end(); }
353 unsigned mdn_size() const { return mdnMap.size(); }
354 bool mdn_empty() const { return mdnMap.empty(); }
Devang Patel983c6b12009-07-08 21:44:25 +0000355
Reid Spencer56010e42004-05-26 21:56:09 +0000356 /// This function does the actual initialization.
357 inline void initialize();
358
Devang Patel983c6b12009-07-08 21:44:25 +0000359 // Implementation Details
360private:
Chris Lattnerea862a32007-01-09 07:55:49 +0000361 /// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
362 void CreateModuleSlot(const GlobalValue *V);
Devang Patel983c6b12009-07-08 21:44:25 +0000363
364 /// CreateMetadataSlot - Insert the specified MDNode* into the slot table.
365 void CreateMetadataSlot(const MDNode *N);
366
Chris Lattnerea862a32007-01-09 07:55:49 +0000367 /// CreateFunctionSlot - Insert the specified Value* into the slot table.
368 void CreateFunctionSlot(const Value *V);
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000369
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000370 /// Add all of the module level global variables (and their initializers)
371 /// and function declarations, but not the contents of those functions.
372 void processModule();
373
Devang Patel983c6b12009-07-08 21:44:25 +0000374 /// Add all of the functions arguments, basic blocks, and instructions.
Reid Spencer56010e42004-05-26 21:56:09 +0000375 void processFunction();
376
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000377 SlotTracker(const SlotTracker &); // DO NOT IMPLEMENT
378 void operator=(const SlotTracker &); // DO NOT IMPLEMENT
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000379};
380
Chris Lattner3ee58762008-08-19 04:28:07 +0000381} // end anonymous namespace
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000382
Chris Lattner3eee99c2008-08-19 04:36:02 +0000383
384static SlotTracker *createSlotTracker(const Value *V) {
385 if (const Argument *FA = dyn_cast<Argument>(V))
386 return new SlotTracker(FA->getParent());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000387
Chris Lattner3eee99c2008-08-19 04:36:02 +0000388 if (const Instruction *I = dyn_cast<Instruction>(V))
389 return new SlotTracker(I->getParent()->getParent());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000390
Chris Lattner3eee99c2008-08-19 04:36:02 +0000391 if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
392 return new SlotTracker(BB->getParent());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000393
Chris Lattner3eee99c2008-08-19 04:36:02 +0000394 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
395 return new SlotTracker(GV->getParent());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000396
Chris Lattner3eee99c2008-08-19 04:36:02 +0000397 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V))
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000398 return new SlotTracker(GA->getParent());
399
Chris Lattner3eee99c2008-08-19 04:36:02 +0000400 if (const Function *Func = dyn_cast<Function>(V))
401 return new SlotTracker(Func);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000402
Dan Gohmana2489d12010-07-20 23:55:01 +0000403 if (const MDNode *MD = dyn_cast<MDNode>(V)) {
404 if (!MD->isFunctionLocal())
405 return new SlotTracker(MD->getFunction());
406
Dale Johannesen7b1a7ed2010-01-13 00:00:24 +0000407 return new SlotTracker((Function *)0);
Dan Gohmana2489d12010-07-20 23:55:01 +0000408 }
Dale Johannesen7b1a7ed2010-01-13 00:00:24 +0000409
Chris Lattner3eee99c2008-08-19 04:36:02 +0000410 return 0;
411}
412
413#if 0
David Greenec7f9b122010-01-05 01:29:26 +0000414#define ST_DEBUG(X) dbgs() << X
Chris Lattner3eee99c2008-08-19 04:36:02 +0000415#else
Chris Lattner604e3512008-08-19 04:47:09 +0000416#define ST_DEBUG(X)
Chris Lattner3eee99c2008-08-19 04:36:02 +0000417#endif
418
419// Module level constructor. Causes the contents of the Module (sans functions)
420// to be added to the slot table.
421SlotTracker::SlotTracker(const Module *M)
Chris Lattnerbddea6a2009-12-31 02:13:35 +0000422 : TheModule(M), TheFunction(0), FunctionProcessed(false),
Chris Lattnerbe2de792009-12-31 01:36:50 +0000423 mNext(0), fNext(0), mdnNext(0) {
Chris Lattner3eee99c2008-08-19 04:36:02 +0000424}
425
426// Function level constructor. Causes the contents of the Module and the one
427// function provided to be added to the slot table.
428SlotTracker::SlotTracker(const Function *F)
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000429 : TheModule(F ? F->getParent() : 0), TheFunction(F), FunctionProcessed(false),
Chris Lattnerbddea6a2009-12-31 02:13:35 +0000430 mNext(0), fNext(0), mdnNext(0) {
Chris Lattner3eee99c2008-08-19 04:36:02 +0000431}
432
433inline void SlotTracker::initialize() {
434 if (TheModule) {
435 processModule();
436 TheModule = 0; ///< Prevent re-processing next time we're called.
437 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000438
Chris Lattner3eee99c2008-08-19 04:36:02 +0000439 if (TheFunction && !FunctionProcessed)
440 processFunction();
441}
442
443// Iterate through all the global variables, functions, and global
444// variable initializers and create slots for them.
445void SlotTracker::processModule() {
Chris Lattner604e3512008-08-19 04:47:09 +0000446 ST_DEBUG("begin processModule!\n");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000447
Chris Lattner3eee99c2008-08-19 04:36:02 +0000448 // Add all of the unnamed global variables to the value table.
449 for (Module::const_global_iterator I = TheModule->global_begin(),
Devang Patel983c6b12009-07-08 21:44:25 +0000450 E = TheModule->global_end(); I != E; ++I) {
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000451 if (!I->hasName())
Chris Lattner3eee99c2008-08-19 04:36:02 +0000452 CreateModuleSlot(I);
Devang Patel983c6b12009-07-08 21:44:25 +0000453 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000454
Devang Patel23e68302009-07-29 22:04:47 +0000455 // Add metadata used by named metadata.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000456 for (Module::const_named_metadata_iterator
Devang Patel23e68302009-07-29 22:04:47 +0000457 I = TheModule->named_metadata_begin(),
458 E = TheModule->named_metadata_end(); I != E; ++I) {
459 const NamedMDNode *NMD = I;
Dan Gohman093cb792010-07-21 18:54:18 +0000460 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
461 CreateMetadataSlot(NMD->getOperand(i));
Devang Patel23e68302009-07-29 22:04:47 +0000462 }
463
Chris Lattner3eee99c2008-08-19 04:36:02 +0000464 // Add all the unnamed functions to the table.
465 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
466 I != E; ++I)
467 if (!I->hasName())
468 CreateModuleSlot(I);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000469
Chris Lattner604e3512008-08-19 04:47:09 +0000470 ST_DEBUG("end processModule!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000471}
472
Chris Lattner3eee99c2008-08-19 04:36:02 +0000473// Process the arguments, basic blocks, and instructions of a function.
474void SlotTracker::processFunction() {
Chris Lattner604e3512008-08-19 04:47:09 +0000475 ST_DEBUG("begin processFunction!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000476 fNext = 0;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000477
Chris Lattner3eee99c2008-08-19 04:36:02 +0000478 // Add all the function arguments with no names.
479 for(Function::const_arg_iterator AI = TheFunction->arg_begin(),
480 AE = TheFunction->arg_end(); AI != AE; ++AI)
481 if (!AI->hasName())
482 CreateFunctionSlot(AI);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000483
Chris Lattner604e3512008-08-19 04:47:09 +0000484 ST_DEBUG("Inserting Instructions:\n");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000485
Chris Lattnerbddea6a2009-12-31 02:13:35 +0000486 SmallVector<std::pair<unsigned, MDNode*>, 4> MDForInst;
Devang Pateldec23fd2009-09-16 20:21:17 +0000487
Chris Lattner3eee99c2008-08-19 04:36:02 +0000488 // Add all of the basic blocks and instructions with no names.
489 for (Function::const_iterator BB = TheFunction->begin(),
490 E = TheFunction->end(); BB != E; ++BB) {
491 if (!BB->hasName())
492 CreateFunctionSlot(BB);
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000493
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000494 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E;
Devang Patel983c6b12009-07-08 21:44:25 +0000495 ++I) {
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000496 if (!I->getType()->isVoidTy() && !I->hasName())
Chris Lattner3eee99c2008-08-19 04:36:02 +0000497 CreateFunctionSlot(I);
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000498
Chris Lattner58aff8f2010-05-10 20:53:17 +0000499 // Intrinsics can directly use metadata. We allow direct calls to any
500 // llvm.foo function here, because the target may not be linked into the
501 // optimizer.
502 if (const CallInst *CI = dyn_cast<CallInst>(I)) {
503 if (Function *F = CI->getCalledFunction())
504 if (F->getName().startswith("llvm."))
505 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
506 if (MDNode *N = dyn_cast_or_null<MDNode>(I->getOperand(i)))
507 CreateMetadataSlot(N);
508 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000509
Devang Pateldec23fd2009-09-16 20:21:17 +0000510 // Process metadata attached with this instruction.
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000511 I->getAllMetadata(MDForInst);
512 for (unsigned i = 0, e = MDForInst.size(); i != e; ++i)
513 CreateMetadataSlot(MDForInst[i].second);
Chris Lattnerbddea6a2009-12-31 02:13:35 +0000514 MDForInst.clear();
Devang Patel983c6b12009-07-08 21:44:25 +0000515 }
Chris Lattner3eee99c2008-08-19 04:36:02 +0000516 }
Devang Pateldec23fd2009-09-16 20:21:17 +0000517
Chris Lattner3eee99c2008-08-19 04:36:02 +0000518 FunctionProcessed = true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000519
Chris Lattner604e3512008-08-19 04:47:09 +0000520 ST_DEBUG("end processFunction!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000521}
522
523/// Clean up after incorporating a function. This is the only way to get out of
524/// the function incorporation state that affects get*Slot/Create*Slot. Function
525/// incorporation state is indicated by TheFunction != 0.
526void SlotTracker::purgeFunction() {
Chris Lattner604e3512008-08-19 04:47:09 +0000527 ST_DEBUG("begin purgeFunction!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000528 fMap.clear(); // Simply discard the function level map
529 TheFunction = 0;
530 FunctionProcessed = false;
Chris Lattner604e3512008-08-19 04:47:09 +0000531 ST_DEBUG("end purgeFunction!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000532}
533
534/// getGlobalSlot - Get the slot number of a global value.
535int SlotTracker::getGlobalSlot(const GlobalValue *V) {
536 // Check for uninitialized state and do lazy initialization.
537 initialize();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000538
Jay Foaddbb221d2011-07-11 07:28:49 +0000539 // Find the value in the module map
Chris Lattner3eee99c2008-08-19 04:36:02 +0000540 ValueMap::iterator MI = mMap.find(V);
Dan Gohman1dd27572008-10-01 19:58:59 +0000541 return MI == mMap.end() ? -1 : (int)MI->second;
Chris Lattner3eee99c2008-08-19 04:36:02 +0000542}
543
Chris Lattnercf4a76e2009-12-31 02:20:11 +0000544/// getMetadataSlot - Get the slot number of a MDNode.
Devang Patel983c6b12009-07-08 21:44:25 +0000545int SlotTracker::getMetadataSlot(const MDNode *N) {
546 // Check for uninitialized state and do lazy initialization.
547 initialize();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000548
Jay Foaddbb221d2011-07-11 07:28:49 +0000549 // Find the MDNode in the module map
Chris Lattnercf4a76e2009-12-31 02:20:11 +0000550 mdn_iterator MI = mdnMap.find(N);
Devang Patel983c6b12009-07-08 21:44:25 +0000551 return MI == mdnMap.end() ? -1 : (int)MI->second;
552}
553
Chris Lattner3eee99c2008-08-19 04:36:02 +0000554
555/// getLocalSlot - Get the slot number for a value that is local to a function.
556int SlotTracker::getLocalSlot(const Value *V) {
557 assert(!isa<Constant>(V) && "Can't get a constant or global slot with this!");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000558
Chris Lattner3eee99c2008-08-19 04:36:02 +0000559 // Check for uninitialized state and do lazy initialization.
560 initialize();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000561
Chris Lattner3eee99c2008-08-19 04:36:02 +0000562 ValueMap::iterator FI = fMap.find(V);
Dan Gohman1dd27572008-10-01 19:58:59 +0000563 return FI == fMap.end() ? -1 : (int)FI->second;
Chris Lattner3eee99c2008-08-19 04:36:02 +0000564}
565
566
567/// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
568void SlotTracker::CreateModuleSlot(const GlobalValue *V) {
569 assert(V && "Can't insert a null Value into SlotTracker!");
Chris Lattner031560c2009-12-29 07:25:48 +0000570 assert(!V->getType()->isVoidTy() && "Doesn't need a slot!");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000571 assert(!V->hasName() && "Doesn't need a slot!");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000572
Chris Lattner3eee99c2008-08-19 04:36:02 +0000573 unsigned DestSlot = mNext++;
574 mMap[V] = DestSlot;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000575
Chris Lattner604e3512008-08-19 04:47:09 +0000576 ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" <<
Chris Lattner3eee99c2008-08-19 04:36:02 +0000577 DestSlot << " [");
578 // G = Global, F = Function, A = Alias, o = other
Chris Lattner604e3512008-08-19 04:47:09 +0000579 ST_DEBUG((isa<GlobalVariable>(V) ? 'G' :
Chris Lattner3eee99c2008-08-19 04:36:02 +0000580 (isa<Function>(V) ? 'F' :
581 (isa<GlobalAlias>(V) ? 'A' : 'o'))) << "]\n");
582}
583
Chris Lattner3eee99c2008-08-19 04:36:02 +0000584/// CreateSlot - Create a new slot for the specified value if it has no name.
585void SlotTracker::CreateFunctionSlot(const Value *V) {
Chris Lattner031560c2009-12-29 07:25:48 +0000586 assert(!V->getType()->isVoidTy() && !V->hasName() && "Doesn't need a slot!");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000587
Chris Lattner3eee99c2008-08-19 04:36:02 +0000588 unsigned DestSlot = fNext++;
589 fMap[V] = DestSlot;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000590
Chris Lattner3eee99c2008-08-19 04:36:02 +0000591 // G = Global, F = Function, o = other
Chris Lattner604e3512008-08-19 04:47:09 +0000592 ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" <<
Chris Lattner3eee99c2008-08-19 04:36:02 +0000593 DestSlot << " [o]\n");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000594}
Chris Lattner3eee99c2008-08-19 04:36:02 +0000595
Devang Patel983c6b12009-07-08 21:44:25 +0000596/// CreateModuleSlot - Insert the specified MDNode* into the slot table.
597void SlotTracker::CreateMetadataSlot(const MDNode *N) {
598 assert(N && "Can't insert a null Value into SlotTracker!");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000599
Chris Lattner0d50bdd2009-12-31 02:27:30 +0000600 // Don't insert if N is a function-local metadata, these are always printed
601 // inline.
Dan Gohmana2489d12010-07-20 23:55:01 +0000602 if (!N->isFunctionLocal()) {
603 mdn_iterator I = mdnMap.find(N);
604 if (I != mdnMap.end())
605 return;
Victor Hernandez4d633542009-12-04 20:07:10 +0000606
Dan Gohmana2489d12010-07-20 23:55:01 +0000607 unsigned DestSlot = mdnNext++;
608 mdnMap[N] = DestSlot;
609 }
Devang Patel983c6b12009-07-08 21:44:25 +0000610
Chris Lattner0d50bdd2009-12-31 02:27:30 +0000611 // Recursively add any MDNodes referenced by operands.
612 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
613 if (const MDNode *Op = dyn_cast_or_null<MDNode>(N->getOperand(i)))
614 CreateMetadataSlot(Op);
Devang Patel983c6b12009-07-08 21:44:25 +0000615}
Chris Lattner3eee99c2008-08-19 04:36:02 +0000616
617//===----------------------------------------------------------------------===//
618// AsmWriter Implementation
619//===----------------------------------------------------------------------===//
Chris Lattner7f8845a2002-07-23 18:07:49 +0000620
Dan Gohman12dad632009-08-12 20:56:03 +0000621static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
Dan Gohman8061d9e2009-08-13 15:27:57 +0000622 TypePrinting *TypePrinter,
Dan Gohmana2489d12010-07-20 23:55:01 +0000623 SlotTracker *Machine,
624 const Module *Context);
Reid Spencer58d30f22004-07-04 11:50:43 +0000625
Chris Lattner033935d2008-08-17 04:40:13 +0000626
Chris Lattnerb86620e2001-10-29 16:37:48 +0000627
Chris Lattnerfc9f1c92006-12-06 06:40:49 +0000628static const char *getPredicateText(unsigned predicate) {
Reid Spencer812a1be2006-12-04 05:19:18 +0000629 const char * pred = "unknown";
630 switch (predicate) {
Chris Lattnerbddea6a2009-12-31 02:13:35 +0000631 case FCmpInst::FCMP_FALSE: pred = "false"; break;
632 case FCmpInst::FCMP_OEQ: pred = "oeq"; break;
633 case FCmpInst::FCMP_OGT: pred = "ogt"; break;
634 case FCmpInst::FCMP_OGE: pred = "oge"; break;
635 case FCmpInst::FCMP_OLT: pred = "olt"; break;
636 case FCmpInst::FCMP_OLE: pred = "ole"; break;
637 case FCmpInst::FCMP_ONE: pred = "one"; break;
638 case FCmpInst::FCMP_ORD: pred = "ord"; break;
639 case FCmpInst::FCMP_UNO: pred = "uno"; break;
640 case FCmpInst::FCMP_UEQ: pred = "ueq"; break;
641 case FCmpInst::FCMP_UGT: pred = "ugt"; break;
642 case FCmpInst::FCMP_UGE: pred = "uge"; break;
643 case FCmpInst::FCMP_ULT: pred = "ult"; break;
644 case FCmpInst::FCMP_ULE: pred = "ule"; break;
645 case FCmpInst::FCMP_UNE: pred = "une"; break;
646 case FCmpInst::FCMP_TRUE: pred = "true"; break;
647 case ICmpInst::ICMP_EQ: pred = "eq"; break;
648 case ICmpInst::ICMP_NE: pred = "ne"; break;
649 case ICmpInst::ICMP_SGT: pred = "sgt"; break;
650 case ICmpInst::ICMP_SGE: pred = "sge"; break;
651 case ICmpInst::ICMP_SLT: pred = "slt"; break;
652 case ICmpInst::ICMP_SLE: pred = "sle"; break;
653 case ICmpInst::ICMP_UGT: pred = "ugt"; break;
654 case ICmpInst::ICMP_UGE: pred = "uge"; break;
655 case ICmpInst::ICMP_ULT: pred = "ult"; break;
656 case ICmpInst::ICMP_ULE: pred = "ule"; break;
Reid Spencer812a1be2006-12-04 05:19:18 +0000657 }
658 return pred;
659}
660
Devang Patel983c6b12009-07-08 21:44:25 +0000661
Dan Gohman12dad632009-08-12 20:56:03 +0000662static void WriteOptimizationInfo(raw_ostream &Out, const User *U) {
Dan Gohman0ebd6962009-07-20 21:19:07 +0000663 if (const OverflowingBinaryOperator *OBO =
664 dyn_cast<OverflowingBinaryOperator>(U)) {
Dan Gohman16f54152009-08-20 17:11:38 +0000665 if (OBO->hasNoUnsignedWrap())
Dan Gohman9c7f8082009-07-27 16:11:46 +0000666 Out << " nuw";
Dan Gohman16f54152009-08-20 17:11:38 +0000667 if (OBO->hasNoSignedWrap())
Dan Gohman9c7f8082009-07-27 16:11:46 +0000668 Out << " nsw";
Chris Lattner35315d02011-02-06 21:44:57 +0000669 } else if (const PossiblyExactOperator *Div =
670 dyn_cast<PossiblyExactOperator>(U)) {
Dan Gohman0ebd6962009-07-20 21:19:07 +0000671 if (Div->isExact())
Dan Gohman9c7f8082009-07-27 16:11:46 +0000672 Out << " exact";
Dan Gohman1639c392009-07-27 21:53:46 +0000673 } else if (const GEPOperator *GEP = dyn_cast<GEPOperator>(U)) {
674 if (GEP->isInBounds())
675 Out << " inbounds";
Dan Gohman0ebd6962009-07-20 21:19:07 +0000676 }
677}
678
Dan Gohmanefb8dbb2010-07-14 20:57:55 +0000679static void WriteConstantInternal(raw_ostream &Out, const Constant *CV,
680 TypePrinting &TypePrinter,
Dan Gohmana2489d12010-07-20 23:55:01 +0000681 SlotTracker *Machine,
682 const Module *Context) {
Zhou Sheng75b871f2007-01-11 12:24:14 +0000683 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Duncan Sands9dff9be2010-02-15 16:12:20 +0000684 if (CI->getType()->isIntegerTy(1)) {
Reid Spencercddc9df2007-01-12 04:24:46 +0000685 Out << (CI->getZExtValue() ? "true" : "false");
Chris Lattner17f71652008-08-17 07:19:36 +0000686 return;
687 }
688 Out << CI->getValue();
689 return;
690 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000691
Chris Lattner17f71652008-08-17 07:19:36 +0000692 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Dale Johannesen028084e2007-09-12 03:30:33 +0000693 if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEdouble ||
694 &CFP->getValueAPF().getSemantics() == &APFloat::IEEEsingle) {
695 // We would like to output the FP constant value in exponential notation,
696 // but we cannot do this if doing so will lose precision. Check here to
697 // make sure that we only output it in exponential format if we can parse
698 // the value back and get the same value.
699 //
Dale Johannesen1f864982009-01-21 20:32:55 +0000700 bool ignored;
Dale Johannesen028084e2007-09-12 03:30:33 +0000701 bool isDouble = &CFP->getValueAPF().getSemantics()==&APFloat::IEEEdouble;
Chris Lattner17f71652008-08-17 07:19:36 +0000702 double Val = isDouble ? CFP->getValueAPF().convertToDouble() :
703 CFP->getValueAPF().convertToFloat();
Benjamin Kramerb17c5862010-01-29 14:42:22 +0000704 SmallString<128> StrVal;
705 raw_svector_ostream(StrVal) << Val;
Chris Lattner1e194682002-04-18 18:53:13 +0000706
Dale Johannesen028084e2007-09-12 03:30:33 +0000707 // Check to make sure that the stringized number is not some string like
708 // "Inf" or NaN, that atof will accept, but the lexer will not. Check
709 // that the string matches the "[-+]?[0-9]" regex.
710 //
711 if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
712 ((StrVal[0] == '-' || StrVal[0] == '+') &&
713 (StrVal[1] >= '0' && StrVal[1] <= '9'))) {
714 // Reparse stringized version!
715 if (atof(StrVal.c_str()) == Val) {
Benjamin Kramerb17c5862010-01-29 14:42:22 +0000716 Out << StrVal.str();
Dale Johannesen028084e2007-09-12 03:30:33 +0000717 return;
718 }
Chris Lattner1e194682002-04-18 18:53:13 +0000719 }
Dale Johannesen028084e2007-09-12 03:30:33 +0000720 // Otherwise we could not reparse it to exactly the same value, so we must
Dale Johannesen1f864982009-01-21 20:32:55 +0000721 // output the string in hexadecimal format! Note that loading and storing
722 // floating point types changes the bits of NaNs on some hosts, notably
723 // x86, so we must not use these types.
Dale Johannesen028084e2007-09-12 03:30:33 +0000724 assert(sizeof(double) == sizeof(uint64_t) &&
725 "assuming that double is 64 bits!");
Chris Lattner5505eed2008-11-10 04:30:26 +0000726 char Buffer[40];
Dale Johannesen1f864982009-01-21 20:32:55 +0000727 APFloat apf = CFP->getValueAPF();
728 // Floats are represented in ASCII IR as double, convert.
729 if (!isDouble)
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000730 apf.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven,
Dale Johannesen1f864982009-01-21 20:32:55 +0000731 &ignored);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000732 Out << "0x" <<
733 utohex_buffer(uint64_t(apf.bitcastToAPInt().getZExtValue()),
Dale Johannesen1f864982009-01-21 20:32:55 +0000734 Buffer+40);
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000735 return;
736 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000737
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000738 // Some form of long double. These appear as a magic letter identifying
739 // the type, then a fixed number of hex digits.
740 Out << "0x";
Dale Johannesen93eefa02009-03-23 21:16:53 +0000741 if (&CFP->getValueAPF().getSemantics() == &APFloat::x87DoubleExtended) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000742 Out << 'K';
Dale Johannesen93eefa02009-03-23 21:16:53 +0000743 // api needed to prevent premature destruction
744 APInt api = CFP->getValueAPF().bitcastToAPInt();
745 const uint64_t* p = api.getRawData();
746 uint64_t word = p[1];
747 int shiftcount=12;
748 int width = api.getBitWidth();
749 for (int j=0; j<width; j+=4, shiftcount-=4) {
750 unsigned int nibble = (word>>shiftcount) & 15;
751 if (nibble < 10)
752 Out << (unsigned char)(nibble + '0');
753 else
754 Out << (unsigned char)(nibble - 10 + 'A');
755 if (shiftcount == 0 && j+4 < width) {
756 word = *p;
757 shiftcount = 64;
758 if (width-j-4 < 64)
759 shiftcount = width-j-4;
760 }
761 }
762 return;
763 } else if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEquad)
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000764 Out << 'L';
765 else if (&CFP->getValueAPF().getSemantics() == &APFloat::PPCDoubleDouble)
766 Out << 'M';
767 else
Torok Edwinfbcc6632009-07-14 16:55:14 +0000768 llvm_unreachable("Unsupported floating point type");
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000769 // api needed to prevent premature destruction
Dale Johannesen54306fe2008-10-09 18:53:47 +0000770 APInt api = CFP->getValueAPF().bitcastToAPInt();
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000771 const uint64_t* p = api.getRawData();
772 uint64_t word = *p;
773 int shiftcount=60;
774 int width = api.getBitWidth();
775 for (int j=0; j<width; j+=4, shiftcount-=4) {
776 unsigned int nibble = (word>>shiftcount) & 15;
777 if (nibble < 10)
778 Out << (unsigned char)(nibble + '0');
Dale Johannesen028084e2007-09-12 03:30:33 +0000779 else
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000780 Out << (unsigned char)(nibble - 10 + 'A');
781 if (shiftcount == 0 && j+4 < width) {
782 word = *(++p);
783 shiftcount = 64;
784 if (width-j-4 < 64)
785 shiftcount = width-j-4;
Dale Johannesen028084e2007-09-12 03:30:33 +0000786 }
787 }
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000788 return;
789 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000790
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000791 if (isa<ConstantAggregateZero>(CV)) {
Chris Lattner76b2ff42004-02-15 05:55:15 +0000792 Out << "zeroinitializer";
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000793 return;
794 }
Chris Lattner214cc702009-10-28 03:38:12 +0000795
796 if (const BlockAddress *BA = dyn_cast<BlockAddress>(CV)) {
797 Out << "blockaddress(";
Dan Gohmana2489d12010-07-20 23:55:01 +0000798 WriteAsOperandInternal(Out, BA->getFunction(), &TypePrinter, Machine,
799 Context);
Chris Lattner214cc702009-10-28 03:38:12 +0000800 Out << ", ";
Dan Gohmana2489d12010-07-20 23:55:01 +0000801 WriteAsOperandInternal(Out, BA->getBasicBlock(), &TypePrinter, Machine,
802 Context);
Chris Lattner214cc702009-10-28 03:38:12 +0000803 Out << ")";
804 return;
805 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000806
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000807 if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
Chris Lattner1e194682002-04-18 18:53:13 +0000808 // As a special case, print the array as a string if it is an array of
Dan Gohmane9bc2ba2008-05-12 16:34:30 +0000809 // i8 with ConstantInt values.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000810 //
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000811 Type *ETy = CA->getType()->getElementType();
Chris Lattner6ed87bd2006-01-23 23:03:36 +0000812 if (CA->isString()) {
Chris Lattner1e194682002-04-18 18:53:13 +0000813 Out << "c\"";
Chris Lattner6ed87bd2006-01-23 23:03:36 +0000814 PrintEscapedString(CA->getAsString(), Out);
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000815 Out << '"';
Chris Lattner1e194682002-04-18 18:53:13 +0000816 } else { // Cannot output in string format...
Misha Brukman21bbdb92004-06-04 21:11:51 +0000817 Out << '[';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000818 if (CA->getNumOperands()) {
Chris Lattnere101c442009-02-28 21:26:53 +0000819 TypePrinter.print(ETy, Out);
Dan Gohman81313fd2008-09-14 17:21:12 +0000820 Out << ' ';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000821 WriteAsOperandInternal(Out, CA->getOperand(0),
Dan Gohmana2489d12010-07-20 23:55:01 +0000822 &TypePrinter, Machine,
823 Context);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000824 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
825 Out << ", ";
Chris Lattnere101c442009-02-28 21:26:53 +0000826 TypePrinter.print(ETy, Out);
Dan Gohman81313fd2008-09-14 17:21:12 +0000827 Out << ' ';
Dan Gohmana2489d12010-07-20 23:55:01 +0000828 WriteAsOperandInternal(Out, CA->getOperand(i), &TypePrinter, Machine,
829 Context);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000830 }
831 }
Dan Gohman81313fd2008-09-14 17:21:12 +0000832 Out << ']';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000833 }
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000834 return;
835 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000836
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000837 if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
Andrew Lenharth0d124b82007-01-08 18:21:30 +0000838 if (CS->getType()->isPacked())
839 Out << '<';
Misha Brukman21bbdb92004-06-04 21:11:51 +0000840 Out << '{';
Jim Laskey3bb78742006-02-25 12:27:03 +0000841 unsigned N = CS->getNumOperands();
842 if (N) {
Chris Lattner604e3512008-08-19 04:47:09 +0000843 Out << ' ';
Chris Lattnere101c442009-02-28 21:26:53 +0000844 TypePrinter.print(CS->getOperand(0)->getType(), Out);
Dan Gohman81313fd2008-09-14 17:21:12 +0000845 Out << ' ';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000846
Dan Gohmana2489d12010-07-20 23:55:01 +0000847 WriteAsOperandInternal(Out, CS->getOperand(0), &TypePrinter, Machine,
848 Context);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000849
Jim Laskey3bb78742006-02-25 12:27:03 +0000850 for (unsigned i = 1; i < N; i++) {
Chris Lattnerd84bb632002-04-16 21:36:08 +0000851 Out << ", ";
Chris Lattnere101c442009-02-28 21:26:53 +0000852 TypePrinter.print(CS->getOperand(i)->getType(), Out);
Dan Gohman81313fd2008-09-14 17:21:12 +0000853 Out << ' ';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000854
Dan Gohmana2489d12010-07-20 23:55:01 +0000855 WriteAsOperandInternal(Out, CS->getOperand(i), &TypePrinter, Machine,
856 Context);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000857 }
Dan Gohman81313fd2008-09-14 17:21:12 +0000858 Out << ' ';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000859 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000860
Dan Gohman81313fd2008-09-14 17:21:12 +0000861 Out << '}';
Andrew Lenharth0d124b82007-01-08 18:21:30 +0000862 if (CS->getType()->isPacked())
863 Out << '>';
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000864 return;
865 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000866
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000867 if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000868 Type *ETy = CP->getType()->getElementType();
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000869 assert(CP->getNumOperands() > 0 &&
870 "Number of operands for a PackedConst must be > 0");
Dan Gohman1262a252009-02-11 00:25:25 +0000871 Out << '<';
Chris Lattnere101c442009-02-28 21:26:53 +0000872 TypePrinter.print(ETy, Out);
Dan Gohman81313fd2008-09-14 17:21:12 +0000873 Out << ' ';
Dan Gohmana2489d12010-07-20 23:55:01 +0000874 WriteAsOperandInternal(Out, CP->getOperand(0), &TypePrinter, Machine,
875 Context);
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000876 for (unsigned i = 1, e = CP->getNumOperands(); i != e; ++i) {
Chris Lattner585297e82008-08-19 05:26:17 +0000877 Out << ", ";
Chris Lattnere101c442009-02-28 21:26:53 +0000878 TypePrinter.print(ETy, Out);
Dan Gohman81313fd2008-09-14 17:21:12 +0000879 Out << ' ';
Dan Gohmana2489d12010-07-20 23:55:01 +0000880 WriteAsOperandInternal(Out, CP->getOperand(i), &TypePrinter, Machine,
881 Context);
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000882 }
Dan Gohman1262a252009-02-11 00:25:25 +0000883 Out << '>';
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000884 return;
885 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000886
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000887 if (isa<ConstantPointerNull>(CV)) {
Chris Lattnerd84bb632002-04-16 21:36:08 +0000888 Out << "null";
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000889 return;
890 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000891
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000892 if (isa<UndefValue>(CV)) {
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000893 Out << "undef";
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000894 return;
895 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000896
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000897 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Reid Spencer812a1be2006-12-04 05:19:18 +0000898 Out << CE->getOpcodeName();
Dan Gohman9c7f8082009-07-27 16:11:46 +0000899 WriteOptimizationInfo(Out, CE);
Reid Spencer812a1be2006-12-04 05:19:18 +0000900 if (CE->isCompare())
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000901 Out << ' ' << getPredicateText(CE->getPredicate());
Reid Spencer812a1be2006-12-04 05:19:18 +0000902 Out << " (";
Misha Brukmanb1c93172005-04-21 23:48:37 +0000903
Vikram S. Adveb952b542002-07-14 23:14:45 +0000904 for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) {
Chris Lattnere101c442009-02-28 21:26:53 +0000905 TypePrinter.print((*OI)->getType(), Out);
Dan Gohman81313fd2008-09-14 17:21:12 +0000906 Out << ' ';
Dan Gohmana2489d12010-07-20 23:55:01 +0000907 WriteAsOperandInternal(Out, *OI, &TypePrinter, Machine, Context);
Vikram S. Adveb952b542002-07-14 23:14:45 +0000908 if (OI+1 != CE->op_end())
Chris Lattner3cd8c562002-07-30 18:54:25 +0000909 Out << ", ";
Vikram S. Adveb952b542002-07-14 23:14:45 +0000910 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000911
Dan Gohmana76f0f72008-05-31 19:12:39 +0000912 if (CE->hasIndices()) {
Jay Foad0091fe82011-04-13 15:22:40 +0000913 ArrayRef<unsigned> Indices = CE->getIndices();
Dan Gohmana76f0f72008-05-31 19:12:39 +0000914 for (unsigned i = 0, e = Indices.size(); i != e; ++i)
915 Out << ", " << Indices[i];
916 }
917
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000918 if (CE->isCast()) {
Chris Lattner83b396b2002-08-15 19:37:43 +0000919 Out << " to ";
Chris Lattnere101c442009-02-28 21:26:53 +0000920 TypePrinter.print(CE->getType(), Out);
Chris Lattner83b396b2002-08-15 19:37:43 +0000921 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000922
Misha Brukman21bbdb92004-06-04 21:11:51 +0000923 Out << ')';
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000924 return;
Chris Lattnerd84bb632002-04-16 21:36:08 +0000925 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000926
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000927 Out << "<placeholder or erroneous Constant>";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000928}
929
Chris Lattnercdec5812009-12-31 02:31:59 +0000930static void WriteMDNodeBodyInternal(raw_ostream &Out, const MDNode *Node,
931 TypePrinting *TypePrinter,
Dan Gohmana2489d12010-07-20 23:55:01 +0000932 SlotTracker *Machine,
933 const Module *Context) {
Chris Lattnercdec5812009-12-31 02:31:59 +0000934 Out << "!{";
935 for (unsigned mi = 0, me = Node->getNumOperands(); mi != me; ++mi) {
936 const Value *V = Node->getOperand(mi);
937 if (V == 0)
938 Out << "null";
939 else {
940 TypePrinter->print(V->getType(), Out);
941 Out << ' ';
942 WriteAsOperandInternal(Out, Node->getOperand(mi),
Dan Gohmana2489d12010-07-20 23:55:01 +0000943 TypePrinter, Machine, Context);
Chris Lattnercdec5812009-12-31 02:31:59 +0000944 }
945 if (mi + 1 != me)
946 Out << ", ";
947 }
948
949 Out << "}";
950}
951
Chris Lattnerd84bb632002-04-16 21:36:08 +0000952
Misha Brukmanc566ca362004-03-02 00:22:19 +0000953/// WriteAsOperand - Write the name of the specified value out to the specified
954/// ostream. This can be useful when you just want to print int %reg126, not
955/// the whole instruction that generated it.
956///
Dan Gohman12dad632009-08-12 20:56:03 +0000957static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
Dan Gohman8061d9e2009-08-13 15:27:57 +0000958 TypePrinting *TypePrinter,
Dan Gohmana2489d12010-07-20 23:55:01 +0000959 SlotTracker *Machine,
960 const Module *Context) {
Chris Lattner033935d2008-08-17 04:40:13 +0000961 if (V->hasName()) {
962 PrintLLVMName(Out, V);
963 return;
964 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000965
Chris Lattner033935d2008-08-17 04:40:13 +0000966 const Constant *CV = dyn_cast<Constant>(V);
967 if (CV && !isa<GlobalValue>(CV)) {
Dan Gohman8061d9e2009-08-13 15:27:57 +0000968 assert(TypePrinter && "Constants require TypePrinting!");
Dan Gohmana2489d12010-07-20 23:55:01 +0000969 WriteConstantInternal(Out, CV, *TypePrinter, Machine, Context);
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000970 return;
971 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000972
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000973 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
Chris Lattner033935d2008-08-17 04:40:13 +0000974 Out << "asm ";
975 if (IA->hasSideEffects())
976 Out << "sideeffect ";
Dale Johannesen1cfb9582009-10-21 23:28:00 +0000977 if (IA->isAlignStack())
978 Out << "alignstack ";
Chris Lattner033935d2008-08-17 04:40:13 +0000979 Out << '"';
980 PrintEscapedString(IA->getAsmString(), Out);
981 Out << "\", \"";
982 PrintEscapedString(IA->getConstraintString(), Out);
983 Out << '"';
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000984 return;
985 }
Devang Patel7428d8a2009-07-22 17:43:22 +0000986
Devang Patele059ba6e2009-07-23 01:07:34 +0000987 if (const MDNode *N = dyn_cast<MDNode>(V)) {
Victor Hernandez0471abd2009-12-18 20:09:14 +0000988 if (N->isFunctionLocal()) {
Victor Hernandezb7176a12009-12-04 01:35:02 +0000989 // Print metadata inline, not via slot reference number.
Dan Gohmana2489d12010-07-20 23:55:01 +0000990 WriteMDNodeBodyInternal(Out, N, TypePrinter, Machine, Context);
Victor Hernandezb7176a12009-12-04 01:35:02 +0000991 return;
992 }
993
Dan Gohmana2489d12010-07-20 23:55:01 +0000994 if (!Machine) {
995 if (N->isFunctionLocal())
996 Machine = new SlotTracker(N->getFunction());
997 else
998 Machine = new SlotTracker(Context);
999 }
Dan Gohman0a54da22010-09-09 20:53:58 +00001000 int Slot = Machine->getMetadataSlot(N);
1001 if (Slot == -1)
1002 Out << "<badref>";
1003 else
1004 Out << '!' << Slot;
Devang Patele059ba6e2009-07-23 01:07:34 +00001005 return;
1006 }
1007
Devang Patel7428d8a2009-07-22 17:43:22 +00001008 if (const MDString *MDS = dyn_cast<MDString>(V)) {
Devang Patel7428d8a2009-07-22 17:43:22 +00001009 Out << "!\"";
Daniel Dunbare03eecb2009-07-25 23:55:21 +00001010 PrintEscapedString(MDS->getString(), Out);
Devang Patel7428d8a2009-07-22 17:43:22 +00001011 Out << '"';
1012 return;
1013 }
1014
Evan Cheng00e87d12009-11-16 07:10:36 +00001015 if (V->getValueID() == Value::PseudoSourceValueVal ||
1016 V->getValueID() == Value::FixedStackPseudoSourceValueVal) {
Dan Gohmanc0353bf2009-09-23 01:33:16 +00001017 V->print(Out);
1018 return;
1019 }
1020
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001021 char Prefix = '%';
1022 int Slot;
1023 if (Machine) {
1024 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1025 Slot = Machine->getGlobalSlot(GV);
1026 Prefix = '@';
1027 } else {
1028 Slot = Machine->getLocalSlot(V);
1029 }
Chris Lattner033935d2008-08-17 04:40:13 +00001030 } else {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001031 Machine = createSlotTracker(V);
Chris Lattner033935d2008-08-17 04:40:13 +00001032 if (Machine) {
1033 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1034 Slot = Machine->getGlobalSlot(GV);
1035 Prefix = '@';
1036 } else {
1037 Slot = Machine->getLocalSlot(V);
1038 }
Dan Gohman8061d9e2009-08-13 15:27:57 +00001039 delete Machine;
Chris Lattnera2d810d2006-01-25 22:26:05 +00001040 } else {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001041 Slot = -1;
Chris Lattnerd84bb632002-04-16 21:36:08 +00001042 }
1043 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001044
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001045 if (Slot != -1)
1046 Out << Prefix << Slot;
1047 else
1048 Out << "<badref>";
Chris Lattnerd84bb632002-04-16 21:36:08 +00001049}
1050
Dan Gohman12dad632009-08-12 20:56:03 +00001051void llvm::WriteAsOperand(raw_ostream &Out, const Value *V,
1052 bool PrintType, const Module *Context) {
Dan Gohman8061d9e2009-08-13 15:27:57 +00001053
1054 // Fast path: Don't construct and populate a TypePrinting object if we
1055 // won't be needing any types printed.
Dan Gohman08097122009-08-13 23:07:11 +00001056 if (!PrintType &&
Dan Gohmana2489d12010-07-20 23:55:01 +00001057 ((!isa<Constant>(V) && !isa<MDNode>(V)) ||
1058 V->hasName() || isa<GlobalValue>(V))) {
1059 WriteAsOperandInternal(Out, V, 0, 0, Context);
Dan Gohman8061d9e2009-08-13 15:27:57 +00001060 return;
1061 }
1062
Chris Lattner5a9f63e2002-07-10 16:48:17 +00001063 if (Context == 0) Context = getModuleFromVal(V);
Chris Lattnerb86620e2001-10-29 16:37:48 +00001064
Chris Lattner92c5c122009-02-28 23:20:19 +00001065 TypePrinting TypePrinter;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001066 if (Context)
1067 TypePrinter.incorporateTypes(*Context);
Dan Gohman81313fd2008-09-14 17:21:12 +00001068 if (PrintType) {
Chris Lattnere101c442009-02-28 21:26:53 +00001069 TypePrinter.print(V->getType(), Out);
Dan Gohman81313fd2008-09-14 17:21:12 +00001070 Out << ' ';
1071 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001072
Dan Gohmana2489d12010-07-20 23:55:01 +00001073 WriteAsOperandInternal(Out, V, &TypePrinter, 0, Context);
Chris Lattner5e5abe32001-07-20 19:15:21 +00001074}
1075
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001076namespace {
Chris Lattner2e9fee42001-07-12 23:35:26 +00001077
Chris Lattnerfee714f2001-09-07 16:36:04 +00001078class AssemblyWriter {
Dan Gohmane2745262009-08-12 17:23:50 +00001079 formatted_raw_ostream &Out;
Chris Lattnere36fd8a2008-08-19 04:26:57 +00001080 SlotTracker &Machine;
Chris Lattner7bfee412001-10-29 16:05:51 +00001081 const Module *TheModule;
Chris Lattner0f578952009-02-28 20:25:14 +00001082 TypePrinting TypePrinter;
Chris Lattner8339f7d2003-10-30 23:41:03 +00001083 AssemblyAnnotationWriter *AnnotationWriter;
Chris Lattnerc9558df2009-12-28 20:10:43 +00001084
Chris Lattner2f7c9632001-06-06 20:29:01 +00001085public:
Dan Gohmane2745262009-08-12 17:23:50 +00001086 inline AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac,
1087 const Module *M,
Chris Lattner8339f7d2003-10-30 23:41:03 +00001088 AssemblyAnnotationWriter *AAW)
Devang Patel59c0c132009-09-28 18:31:56 +00001089 : Out(o), Machine(Mac), TheModule(M), AnnotationWriter(AAW) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001090 if (M)
1091 TypePrinter.incorporateTypes(*M);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001092 }
1093
Chris Lattnerbddea6a2009-12-31 02:13:35 +00001094 void printMDNodeBody(const MDNode *MD);
Chris Lattner2c48c642009-12-31 01:54:05 +00001095 void printNamedMDNode(const NamedMDNode *NMD);
1096
Chris Lattnerefb5e392009-12-31 02:23:35 +00001097 void printModule(const Module *M);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001098
Chris Lattner78e2e8b2006-12-06 06:24:27 +00001099 void writeOperand(const Value *Op, bool PrintType);
Devang Patelba3fa6c2008-09-23 23:03:40 +00001100 void writeParamOperand(const Value *Operand, Attributes Attrs);
Eli Friedmanfee02c62011-07-25 23:16:38 +00001101 void writeAtomic(AtomicOrdering Ordering, SynchronizationScope SynchScope);
Chris Lattner1e194682002-04-18 18:53:13 +00001102
Chris Lattnerbddea6a2009-12-31 02:13:35 +00001103 void writeAllMDNodes();
1104
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001105 void printTypeIdentities();
Chris Lattner7bfee412001-10-29 16:05:51 +00001106 void printGlobal(const GlobalVariable *GV);
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001107 void printAlias(const GlobalAlias *GV);
Chris Lattner57698e22002-03-26 18:01:55 +00001108 void printFunction(const Function *F);
Devang Patelba3fa6c2008-09-23 23:03:40 +00001109 void printArgument(const Argument *FA, Attributes Attrs);
Chris Lattner7bfee412001-10-29 16:05:51 +00001110 void printBasicBlock(const BasicBlock *BB);
Chris Lattner113f4f42002-06-25 16:13:24 +00001111 void printInstruction(const Instruction &I);
Chris Lattnerd816b532002-04-13 20:53:41 +00001112
Dan Gohmanbd11c412010-02-10 20:42:57 +00001113private:
Chris Lattner862e3382001-10-13 06:42:36 +00001114 // printInfoComment - Print a little comment after the instruction indicating
1115 // which slot it occupies.
Chris Lattner113f4f42002-06-25 16:13:24 +00001116 void printInfoComment(const Value &V);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001117};
Chris Lattner3243ea12009-03-01 00:03:38 +00001118} // end of anonymous namespace
Chris Lattner2f7c9632001-06-06 20:29:01 +00001119
Chris Lattner78e2e8b2006-12-06 06:24:27 +00001120void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType) {
1121 if (Operand == 0) {
Chris Lattner08f7d0c2005-02-24 16:58:29 +00001122 Out << "<null operand!>";
Chris Lattnerb419a1e2009-12-31 02:33:14 +00001123 return;
Chris Lattner08f7d0c2005-02-24 16:58:29 +00001124 }
Chris Lattnerb419a1e2009-12-31 02:33:14 +00001125 if (PrintType) {
1126 TypePrinter.print(Operand->getType(), Out);
1127 Out << ' ';
1128 }
Dan Gohmana2489d12010-07-20 23:55:01 +00001129 WriteAsOperandInternal(Out, Operand, &TypePrinter, &Machine, TheModule);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001130}
1131
Eli Friedmanfee02c62011-07-25 23:16:38 +00001132void AssemblyWriter::writeAtomic(AtomicOrdering Ordering,
1133 SynchronizationScope SynchScope) {
1134 if (Ordering == NotAtomic)
1135 return;
1136
1137 switch (SynchScope) {
1138 default: Out << " <bad scope " << int(SynchScope) << ">"; break;
1139 case SingleThread: Out << " singlethread"; break;
1140 case CrossThread: break;
1141 }
1142
1143 switch (Ordering) {
1144 default: Out << " <bad ordering " << int(Ordering) << ">"; break;
1145 case Unordered: Out << " unordered"; break;
1146 case Monotonic: Out << " monotonic"; break;
1147 case Acquire: Out << " acquire"; break;
1148 case Release: Out << " release"; break;
1149 case AcquireRelease: Out << " acq_rel"; break;
1150 case SequentiallyConsistent: Out << " seq_cst"; break;
1151 }
1152}
1153
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001154void AssemblyWriter::writeParamOperand(const Value *Operand,
Devang Patelba3fa6c2008-09-23 23:03:40 +00001155 Attributes Attrs) {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00001156 if (Operand == 0) {
1157 Out << "<null operand!>";
Chris Lattnerb419a1e2009-12-31 02:33:14 +00001158 return;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00001159 }
Chris Lattnerb419a1e2009-12-31 02:33:14 +00001160
1161 // Print the type
1162 TypePrinter.print(Operand->getType(), Out);
1163 // Print parameter attributes list
1164 if (Attrs != Attribute::None)
1165 Out << ' ' << Attribute::getAsString(Attrs);
1166 Out << ' ';
1167 // Print the operand
Dan Gohmana2489d12010-07-20 23:55:01 +00001168 WriteAsOperandInternal(Out, Operand, &TypePrinter, &Machine, TheModule);
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00001169}
Chris Lattner2f7c9632001-06-06 20:29:01 +00001170
Chris Lattner7bfee412001-10-29 16:05:51 +00001171void AssemblyWriter::printModule(const Module *M) {
Chris Lattner4d8689e2005-03-02 23:12:40 +00001172 if (!M->getModuleIdentifier().empty() &&
Misha Brukmanb1c93172005-04-21 23:48:37 +00001173 // Don't print the ID if it will start a new line (which would
Chris Lattner4d8689e2005-03-02 23:12:40 +00001174 // require a comment char before it).
1175 M->getModuleIdentifier().find('\n') == std::string::npos)
1176 Out << "; ModuleID = '" << M->getModuleIdentifier() << "'\n";
1177
Owen Andersone2237542006-10-18 02:21:12 +00001178 if (!M->getDataLayout().empty())
Chris Lattner04897162006-10-22 06:06:56 +00001179 Out << "target datalayout = \"" << M->getDataLayout() << "\"\n";
Reid Spencer48f98c82004-07-25 21:44:54 +00001180 if (!M->getTargetTriple().empty())
Reid Spencerffec7df2004-07-25 21:29:43 +00001181 Out << "target triple = \"" << M->getTargetTriple() << "\"\n";
Misha Brukmanb1c93172005-04-21 23:48:37 +00001182
Chris Lattnereef2fe72006-01-24 04:13:11 +00001183 if (!M->getModuleInlineAsm().empty()) {
Chris Lattnerefaf35d2006-01-24 00:45:30 +00001184 // Split the string into lines, to make it easier to read the .ll file.
Chris Lattnereef2fe72006-01-24 04:13:11 +00001185 std::string Asm = M->getModuleInlineAsm();
Chris Lattnerefaf35d2006-01-24 00:45:30 +00001186 size_t CurPos = 0;
1187 size_t NewLine = Asm.find_first_of('\n', CurPos);
Dan Gohman69273e62009-08-12 23:54:22 +00001188 Out << '\n';
Chris Lattnerefaf35d2006-01-24 00:45:30 +00001189 while (NewLine != std::string::npos) {
1190 // We found a newline, print the portion of the asm string from the
1191 // last newline up to this newline.
1192 Out << "module asm \"";
1193 PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.begin()+NewLine),
1194 Out);
1195 Out << "\"\n";
1196 CurPos = NewLine+1;
1197 NewLine = Asm.find_first_of('\n', CurPos);
1198 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +00001199 std::string rest(Asm.begin()+CurPos, Asm.end());
1200 if (!rest.empty()) {
1201 Out << "module asm \"";
1202 PrintEscapedString(rest, Out);
1203 Out << "\"\n";
1204 }
Chris Lattner6ed87bd2006-01-23 23:03:36 +00001205 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001206
Chris Lattner2cdd49d2004-09-14 05:06:58 +00001207 // Loop over the dependent libraries and emit them.
Chris Lattner730cfe42004-09-14 04:51:44 +00001208 Module::lib_iterator LI = M->lib_begin();
1209 Module::lib_iterator LE = M->lib_end();
Reid Spencer48f98c82004-07-25 21:44:54 +00001210 if (LI != LE) {
Dan Gohman69273e62009-08-12 23:54:22 +00001211 Out << '\n';
Chris Lattner730cfe42004-09-14 04:51:44 +00001212 Out << "deplibs = [ ";
1213 while (LI != LE) {
Chris Lattner2cdd49d2004-09-14 05:06:58 +00001214 Out << '"' << *LI << '"';
Reid Spencerffec7df2004-07-25 21:29:43 +00001215 ++LI;
Chris Lattner730cfe42004-09-14 04:51:44 +00001216 if (LI != LE)
1217 Out << ", ";
Reid Spencerffec7df2004-07-25 21:29:43 +00001218 }
Dan Gohman69273e62009-08-12 23:54:22 +00001219 Out << " ]";
Reid Spencercc5ff642004-07-25 18:08:18 +00001220 }
Reid Spencerb9e08772004-09-13 23:44:23 +00001221
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001222 printTypeIdentities();
Misha Brukmanb1c93172005-04-21 23:48:37 +00001223
Dan Gohman69273e62009-08-12 23:54:22 +00001224 // Output all globals.
1225 if (!M->global_empty()) Out << '\n';
Chris Lattner54932b02006-12-06 04:41:52 +00001226 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
1227 I != E; ++I)
Chris Lattner113f4f42002-06-25 16:13:24 +00001228 printGlobal(I);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001229
Chris Lattnerd2747052007-04-26 02:24:10 +00001230 // Output all aliases.
1231 if (!M->alias_empty()) Out << "\n";
1232 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
1233 I != E; ++I)
1234 printAlias(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +00001235
Chris Lattner2cdd49d2004-09-14 05:06:58 +00001236 // Output all of the functions.
Chris Lattner113f4f42002-06-25 16:13:24 +00001237 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
1238 printFunction(I);
Devang Patel983c6b12009-07-08 21:44:25 +00001239
Devang Patel23e68302009-07-29 22:04:47 +00001240 // Output named metadata.
Dan Gohman69273e62009-08-12 23:54:22 +00001241 if (!M->named_metadata_empty()) Out << '\n';
Chris Lattner2c48c642009-12-31 01:54:05 +00001242
Devang Patel23e68302009-07-29 22:04:47 +00001243 for (Module::const_named_metadata_iterator I = M->named_metadata_begin(),
Chris Lattnerbddea6a2009-12-31 02:13:35 +00001244 E = M->named_metadata_end(); I != E; ++I)
Chris Lattner2c48c642009-12-31 01:54:05 +00001245 printNamedMDNode(I);
Devang Patel23e68302009-07-29 22:04:47 +00001246
1247 // Output metadata.
Chris Lattnercf4a76e2009-12-31 02:20:11 +00001248 if (!Machine.mdn_empty()) {
Chris Lattnerbddea6a2009-12-31 02:13:35 +00001249 Out << '\n';
1250 writeAllMDNodes();
1251 }
Chris Lattnerfee714f2001-09-07 16:36:04 +00001252}
1253
Chris Lattner2c48c642009-12-31 01:54:05 +00001254void AssemblyWriter::printNamedMDNode(const NamedMDNode *NMD) {
Nick Lewycky5bcbd732011-06-15 06:37:58 +00001255 Out << '!';
1256 StringRef Name = NMD->getName();
1257 if (Name.empty()) {
1258 Out << "<empty name> ";
1259 } else {
1260 if (isalpha(Name[0]) || Name[0] == '-' || Name[0] == '$' ||
1261 Name[0] == '.' || Name[0] == '_')
1262 Out << Name[0];
1263 else
1264 Out << '\\' << hexdigit(Name[0] >> 4) << hexdigit(Name[0] & 0x0F);
1265 for (unsigned i = 1, e = Name.size(); i != e; ++i) {
1266 unsigned char C = Name[i];
1267 if (isalnum(C) || C == '-' || C == '$' || C == '.' || C == '_')
1268 Out << C;
1269 else
1270 Out << '\\' << hexdigit(C >> 4) << hexdigit(C & 0x0F);
1271 }
1272 }
1273 Out << " = !{";
Chris Lattner2c48c642009-12-31 01:54:05 +00001274 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1275 if (i) Out << ", ";
Dan Gohman0a54da22010-09-09 20:53:58 +00001276 int Slot = Machine.getMetadataSlot(NMD->getOperand(i));
1277 if (Slot == -1)
1278 Out << "<badref>";
1279 else
1280 Out << '!' << Slot;
Chris Lattner2c48c642009-12-31 01:54:05 +00001281 }
1282 Out << "}\n";
1283}
1284
1285
Dan Gohmane2745262009-08-12 17:23:50 +00001286static void PrintLinkage(GlobalValue::LinkageTypes LT,
1287 formatted_raw_ostream &Out) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001288 switch (LT) {
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001289 case GlobalValue::ExternalLinkage: break;
1290 case GlobalValue::PrivateLinkage: Out << "private "; break;
1291 case GlobalValue::LinkerPrivateLinkage: Out << "linker_private "; break;
Bill Wendling03bcd6e2010-07-01 21:55:59 +00001292 case GlobalValue::LinkerPrivateWeakLinkage:
1293 Out << "linker_private_weak ";
1294 break;
Bill Wendling578ee402010-08-20 22:05:50 +00001295 case GlobalValue::LinkerPrivateWeakDefAutoLinkage:
1296 Out << "linker_private_weak_def_auto ";
1297 break;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001298 case GlobalValue::InternalLinkage: Out << "internal "; break;
1299 case GlobalValue::LinkOnceAnyLinkage: Out << "linkonce "; break;
1300 case GlobalValue::LinkOnceODRLinkage: Out << "linkonce_odr "; break;
1301 case GlobalValue::WeakAnyLinkage: Out << "weak "; break;
1302 case GlobalValue::WeakODRLinkage: Out << "weak_odr "; break;
1303 case GlobalValue::CommonLinkage: Out << "common "; break;
1304 case GlobalValue::AppendingLinkage: Out << "appending "; break;
1305 case GlobalValue::DLLImportLinkage: Out << "dllimport "; break;
1306 case GlobalValue::DLLExportLinkage: Out << "dllexport "; break;
1307 case GlobalValue::ExternalWeakLinkage: Out << "extern_weak "; break;
Chris Lattner184f1be2009-04-13 05:44:34 +00001308 case GlobalValue::AvailableExternallyLinkage:
1309 Out << "available_externally ";
1310 break;
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001311 }
1312}
Duncan Sands12da8ce2009-03-07 15:45:40 +00001313
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001314
1315static void PrintVisibility(GlobalValue::VisibilityTypes Vis,
Dan Gohmane2745262009-08-12 17:23:50 +00001316 formatted_raw_ostream &Out) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001317 switch (Vis) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001318 case GlobalValue::DefaultVisibility: break;
1319 case GlobalValue::HiddenVisibility: Out << "hidden "; break;
1320 case GlobalValue::ProtectedVisibility: Out << "protected "; break;
1321 }
1322}
1323
Chris Lattner7bfee412001-10-29 16:05:51 +00001324void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Dan Gohman5ded1422010-01-29 23:12:36 +00001325 if (GV->isMaterializable())
1326 Out << "; Materializable\n";
1327
Dan Gohmana2489d12010-07-20 23:55:01 +00001328 WriteAsOperandInternal(Out, GV, &TypePrinter, &Machine, GV->getParent());
Dan Gohman466876b2009-08-12 23:32:33 +00001329 Out << " = ";
Chris Lattner37798642001-09-18 04:01:05 +00001330
Chris Lattner1508d3f2008-08-19 05:16:28 +00001331 if (!GV->hasInitializer() && GV->hasExternalLinkage())
1332 Out << "external ";
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001333
Chris Lattner1508d3f2008-08-19 05:16:28 +00001334 PrintLinkage(GV->getLinkage(), Out);
1335 PrintVisibility(GV->getVisibility(), Out);
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +00001336
1337 if (GV->isThreadLocal()) Out << "thread_local ";
Chris Lattnerac161bf2009-01-02 07:01:27 +00001338 if (unsigned AddressSpace = GV->getType()->getAddressSpace())
1339 Out << "addrspace(" << AddressSpace << ") ";
Rafael Espindola45e6c192011-01-08 16:42:36 +00001340 if (GV->hasUnnamedAddr()) Out << "unnamed_addr ";
Misha Brukmana6619a92004-06-21 21:53:56 +00001341 Out << (GV->isConstant() ? "constant " : "global ");
Chris Lattnere101c442009-02-28 21:26:53 +00001342 TypePrinter.print(GV->getType()->getElementType(), Out);
Chris Lattner37798642001-09-18 04:01:05 +00001343
Dan Gohman81313fd2008-09-14 17:21:12 +00001344 if (GV->hasInitializer()) {
1345 Out << ' ';
Devang Patel983c6b12009-07-08 21:44:25 +00001346 writeOperand(GV->getInitializer(), false);
Dan Gohman81313fd2008-09-14 17:21:12 +00001347 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001348
Chris Lattner9380b812010-07-07 23:16:37 +00001349 if (GV->hasSection()) {
1350 Out << ", section \"";
1351 PrintEscapedString(GV->getSection(), Out);
1352 Out << '"';
1353 }
Chris Lattner4b96c542005-11-12 00:10:19 +00001354 if (GV->getAlignment())
Chris Lattnerf8a974d2005-11-06 06:48:53 +00001355 Out << ", align " << GV->getAlignment();
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001356
Chris Lattner113f4f42002-06-25 16:13:24 +00001357 printInfoComment(*GV);
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001358 Out << '\n';
Chris Lattnerda975502001-09-10 07:58:01 +00001359}
1360
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001361void AssemblyWriter::printAlias(const GlobalAlias *GA) {
Dan Gohman5ded1422010-01-29 23:12:36 +00001362 if (GA->isMaterializable())
1363 Out << "; Materializable\n";
1364
Dale Johannesen83e468a2008-06-03 18:14:29 +00001365 // Don't crash when dumping partially built GA
1366 if (!GA->hasName())
1367 Out << "<<nameless>> = ";
Chris Lattner033935d2008-08-17 04:40:13 +00001368 else {
1369 PrintLLVMName(Out, GA);
1370 Out << " = ";
1371 }
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001372 PrintVisibility(GA->getVisibility(), Out);
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001373
1374 Out << "alias ";
1375
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001376 PrintLinkage(GA->getLinkage(), Out);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001377
Anton Korobeynikov546ea7e2007-04-29 18:02:48 +00001378 const Constant *Aliasee = GA->getAliasee();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001379
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001380 if (Aliasee == 0) {
1381 TypePrinter.print(GA->getType(), Out);
1382 Out << " <<NULL ALIASEE>>";
1383 } else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Aliasee)) {
Chris Lattnere101c442009-02-28 21:26:53 +00001384 TypePrinter.print(GV->getType(), Out);
Chris Lattner033935d2008-08-17 04:40:13 +00001385 Out << ' ';
1386 PrintLLVMName(Out, GV);
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001387 } else if (const Function *F = dyn_cast<Function>(Aliasee)) {
Chris Lattnere101c442009-02-28 21:26:53 +00001388 TypePrinter.print(F->getFunctionType(), Out);
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001389 Out << "* ";
1390
Dan Gohmana2489d12010-07-20 23:55:01 +00001391 WriteAsOperandInternal(Out, F, &TypePrinter, &Machine, F->getParent());
Anton Korobeynikov72d5d422008-03-22 08:17:17 +00001392 } else if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(Aliasee)) {
Chris Lattnere101c442009-02-28 21:26:53 +00001393 TypePrinter.print(GA->getType(), Out);
1394 Out << ' ';
Chris Lattner033935d2008-08-17 04:40:13 +00001395 PrintLLVMName(Out, GA);
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +00001396 } else {
Chris Lattnercde89e42009-04-25 21:23:19 +00001397 const ConstantExpr *CE = cast<ConstantExpr>(Aliasee);
1398 // The only valid GEP is an all zero GEP.
1399 assert((CE->getOpcode() == Instruction::BitCast ||
1400 CE->getOpcode() == Instruction::GetElementPtr) &&
1401 "Unsupported aliasee");
1402 writeOperand(CE, false);
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +00001403 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001404
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001405 printInfoComment(*GA);
Chris Lattner1508d3f2008-08-19 05:16:28 +00001406 Out << '\n';
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001407}
1408
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001409void AssemblyWriter::printTypeIdentities() {
1410 if (TypePrinter.NumberedTypes.empty() &&
1411 TypePrinter.NamedTypes.empty())
1412 return;
1413
1414 Out << '\n';
1415
1416 // We know all the numbers that each type is used and we know that it is a
1417 // dense assignment. Convert the map to an index table.
1418 std::vector<StructType*> NumberedTypes(TypePrinter.NumberedTypes.size());
1419 for (DenseMap<StructType*, unsigned>::iterator I =
1420 TypePrinter.NumberedTypes.begin(), E = TypePrinter.NumberedTypes.end();
1421 I != E; ++I) {
1422 assert(I->second < NumberedTypes.size() && "Didn't get a dense numbering?");
1423 NumberedTypes[I->second] = I->first;
1424 }
1425
Chris Lattner3243ea12009-03-01 00:03:38 +00001426 // Emit all numbered types.
1427 for (unsigned i = 0, e = NumberedTypes.size(); i != e; ++i) {
Dan Gohman466876b2009-08-12 23:32:33 +00001428 Out << '%' << i << " = type ";
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001429
Chris Lattner3243ea12009-03-01 00:03:38 +00001430 // Make sure we print out at least one level of the type structure, so
1431 // that we do not get %2 = type %2
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001432 TypePrinter.printStructBody(NumberedTypes[i], Out);
Dan Gohman69273e62009-08-12 23:54:22 +00001433 Out << '\n';
Chris Lattner3243ea12009-03-01 00:03:38 +00001434 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001435
1436 for (unsigned i = 0, e = TypePrinter.NamedTypes.size(); i != e; ++i) {
1437 PrintLLVMName(Out, TypePrinter.NamedTypes[i]->getName(), LocalPrefix);
Chris Lattner1508d3f2008-08-19 05:16:28 +00001438 Out << " = type ";
Reid Spencere7e96712004-05-25 08:53:40 +00001439
1440 // Make sure we print out at least one level of the type structure, so
1441 // that we do not get %FILE = type %FILE
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001442 TypePrinter.printStructBody(TypePrinter.NamedTypes[i], Out);
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001443 Out << '\n';
Reid Spencere7e96712004-05-25 08:53:40 +00001444 }
Reid Spencer32af9e82007-01-06 07:24:44 +00001445}
1446
Misha Brukmanc566ca362004-03-02 00:22:19 +00001447/// printFunction - Print all aspects of a function.
1448///
Chris Lattner113f4f42002-06-25 16:13:24 +00001449void AssemblyWriter::printFunction(const Function *F) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001450 // Print out the return type and name.
1451 Out << '\n';
Chris Lattner379a8d22003-04-16 20:28:45 +00001452
Misha Brukmana6619a92004-06-21 21:53:56 +00001453 if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001454
Dan Gohman5ded1422010-01-29 23:12:36 +00001455 if (F->isMaterializable())
1456 Out << "; Materializable\n";
1457
Reid Spencer5301e7c2007-01-30 20:08:39 +00001458 if (F->isDeclaration())
Chris Lattner10f03a62007-08-19 22:15:26 +00001459 Out << "declare ";
1460 else
Reid Spencer7ce2d2a2006-12-29 20:29:48 +00001461 Out << "define ";
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001462
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001463 PrintLinkage(F->getLinkage(), Out);
1464 PrintVisibility(F->getVisibility(), Out);
Chris Lattner379a8d22003-04-16 20:28:45 +00001465
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001466 // Print the calling convention.
1467 switch (F->getCallingConv()) {
1468 case CallingConv::C: break; // default
Anton Korobeynikov3c5b3df2006-09-20 22:03:51 +00001469 case CallingConv::Fast: Out << "fastcc "; break;
1470 case CallingConv::Cold: Out << "coldcc "; break;
1471 case CallingConv::X86_StdCall: Out << "x86_stdcallcc "; break;
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001472 case CallingConv::X86_FastCall: Out << "x86_fastcallcc "; break;
Anton Korobeynikov8f35fab2010-05-16 09:08:45 +00001473 case CallingConv::X86_ThisCall: Out << "x86_thiscallcc "; break;
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001474 case CallingConv::ARM_APCS: Out << "arm_apcscc "; break;
1475 case CallingConv::ARM_AAPCS: Out << "arm_aapcscc "; break;
1476 case CallingConv::ARM_AAPCS_VFP:Out << "arm_aapcs_vfpcc "; break;
Anton Korobeynikov27a0ecf2009-12-07 02:27:35 +00001477 case CallingConv::MSP430_INTR: Out << "msp430_intrcc "; break;
Justin Holewinski0cfa7372011-03-07 14:32:30 +00001478 case CallingConv::PTX_Kernel: Out << "ptx_kernel "; break;
1479 case CallingConv::PTX_Device: Out << "ptx_device "; break;
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001480 default: Out << "cc" << F->getCallingConv() << " "; break;
1481 }
1482
Chris Lattner229907c2011-07-18 04:54:35 +00001483 FunctionType *FT = F->getFunctionType();
Devang Patel4c758ea2008-09-25 21:00:45 +00001484 const AttrListPtr &Attrs = F->getAttributes();
Devang Patel221fe422008-09-29 20:49:50 +00001485 Attributes RetAttrs = Attrs.getRetAttributes();
1486 if (RetAttrs != Attribute::None)
1487 Out << Attribute::getAsString(Attrs.getRetAttributes()) << ' ';
Chris Lattnere101c442009-02-28 21:26:53 +00001488 TypePrinter.print(F->getReturnType(), Out);
Chris Lattner585297e82008-08-19 05:26:17 +00001489 Out << ' ';
Dan Gohmana2489d12010-07-20 23:55:01 +00001490 WriteAsOperandInternal(Out, F, &TypePrinter, &Machine, F->getParent());
Misha Brukmana6619a92004-06-21 21:53:56 +00001491 Out << '(';
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001492 Machine.incorporateFunction(F);
Chris Lattnerfee714f2001-09-07 16:36:04 +00001493
Chris Lattner7bfee412001-10-29 16:05:51 +00001494 // Loop over the arguments, printing them...
Chris Lattnerfee714f2001-09-07 16:36:04 +00001495
Reid Spencer8c4914c2006-12-31 05:24:50 +00001496 unsigned Idx = 1;
Chris Lattner82738fe2007-04-18 00:57:22 +00001497 if (!F->isDeclaration()) {
1498 // If this isn't a declaration, print the argument names as well.
1499 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
1500 I != E; ++I) {
1501 // Insert commas as we go... the first arg doesn't get a comma
1502 if (I != F->arg_begin()) Out << ", ";
Devang Patela05633e2008-09-26 22:53:05 +00001503 printArgument(I, Attrs.getParamAttributes(Idx));
Chris Lattner82738fe2007-04-18 00:57:22 +00001504 Idx++;
1505 }
1506 } else {
1507 // Otherwise, print the types from the function type.
1508 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
1509 // Insert commas as we go... the first arg doesn't get a comma
1510 if (i) Out << ", ";
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001511
Chris Lattner82738fe2007-04-18 00:57:22 +00001512 // Output type...
Chris Lattnere101c442009-02-28 21:26:53 +00001513 TypePrinter.print(FT->getParamType(i), Out);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001514
Devang Patela05633e2008-09-26 22:53:05 +00001515 Attributes ArgAttrs = Attrs.getParamAttributes(i+1);
Devang Patel4c758ea2008-09-25 21:00:45 +00001516 if (ArgAttrs != Attribute::None)
1517 Out << ' ' << Attribute::getAsString(ArgAttrs);
Chris Lattner82738fe2007-04-18 00:57:22 +00001518 }
Reid Spencer8c4914c2006-12-31 05:24:50 +00001519 }
Chris Lattnerfee714f2001-09-07 16:36:04 +00001520
1521 // Finish printing arguments...
Chris Lattner113f4f42002-06-25 16:13:24 +00001522 if (FT->isVarArg()) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001523 if (FT->getNumParams()) Out << ", ";
1524 Out << "..."; // Output varargs portion of signature!
Chris Lattnerfee714f2001-09-07 16:36:04 +00001525 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001526 Out << ')';
Rafael Espindola563eb4b2011-01-25 19:09:56 +00001527 if (F->hasUnnamedAddr())
1528 Out << " unnamed_addr";
Devang Patela05633e2008-09-26 22:53:05 +00001529 Attributes FnAttrs = Attrs.getFnAttributes();
1530 if (FnAttrs != Attribute::None)
1531 Out << ' ' << Attribute::getAsString(Attrs.getFnAttributes());
Chris Lattner9380b812010-07-07 23:16:37 +00001532 if (F->hasSection()) {
1533 Out << " section \"";
1534 PrintEscapedString(F->getSection(), Out);
1535 Out << '"';
1536 }
Chris Lattnerf8a974d2005-11-06 06:48:53 +00001537 if (F->getAlignment())
1538 Out << " align " << F->getAlignment();
Gordon Henriksend930f912008-08-17 18:44:35 +00001539 if (F->hasGC())
1540 Out << " gc \"" << F->getGC() << '"';
Reid Spencer5301e7c2007-01-30 20:08:39 +00001541 if (F->isDeclaration()) {
Chris Lattnerfb483622010-09-02 22:52:10 +00001542 Out << '\n';
Chris Lattnerb2f02e52002-05-06 03:00:40 +00001543 } else {
Chris Lattnerfb483622010-09-02 22:52:10 +00001544 Out << " {";
1545 // Output all of the function's basic blocks.
Chris Lattner113f4f42002-06-25 16:13:24 +00001546 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
1547 printBasicBlock(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +00001548
Misha Brukmana6619a92004-06-21 21:53:56 +00001549 Out << "}\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +00001550 }
1551
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001552 Machine.purgeFunction();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001553}
1554
Misha Brukmanc566ca362004-03-02 00:22:19 +00001555/// printArgument - This member is called for every argument that is passed into
1556/// the function. Simply print it out
1557///
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001558void AssemblyWriter::printArgument(const Argument *Arg,
Devang Patelba3fa6c2008-09-23 23:03:40 +00001559 Attributes Attrs) {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001560 // Output type...
Chris Lattnere101c442009-02-28 21:26:53 +00001561 TypePrinter.print(Arg->getType(), Out);
Misha Brukmanb1c93172005-04-21 23:48:37 +00001562
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00001563 // Output parameter attributes list
Devang Patel4c758ea2008-09-25 21:00:45 +00001564 if (Attrs != Attribute::None)
1565 Out << ' ' << Attribute::getAsString(Attrs);
Reid Spencer8c4914c2006-12-31 05:24:50 +00001566
Chris Lattner2f7c9632001-06-06 20:29:01 +00001567 // Output name, if available...
Chris Lattner033935d2008-08-17 04:40:13 +00001568 if (Arg->hasName()) {
1569 Out << ' ';
1570 PrintLLVMName(Out, Arg);
1571 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001572}
1573
Misha Brukmanc566ca362004-03-02 00:22:19 +00001574/// printBasicBlock - This member is called for each basic block in a method.
1575///
Chris Lattner7bfee412001-10-29 16:05:51 +00001576void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Nick Lewycky4d43d3c2008-04-25 16:53:59 +00001577 if (BB->hasName()) { // Print out the label if it exists...
Chris Lattner033935d2008-08-17 04:40:13 +00001578 Out << "\n";
Daniel Dunbare03eecb2009-07-25 23:55:21 +00001579 PrintLLVMName(Out, BB->getName(), LabelPrefix);
Chris Lattner033935d2008-08-17 04:40:13 +00001580 Out << ':';
Nick Lewycky4d43d3c2008-04-25 16:53:59 +00001581 } else if (!BB->use_empty()) { // Don't print block # of no uses...
Bill Wendling35a9c3c2011-04-10 23:18:04 +00001582 Out << "\n; <label>:";
Chris Lattner5e043322007-01-11 03:54:27 +00001583 int Slot = Machine.getLocalSlot(BB);
Chris Lattner757ee0b2004-06-09 19:41:19 +00001584 if (Slot != -1)
Misha Brukmana6619a92004-06-21 21:53:56 +00001585 Out << Slot;
Chris Lattner757ee0b2004-06-09 19:41:19 +00001586 else
Misha Brukmana6619a92004-06-21 21:53:56 +00001587 Out << "<badref>";
Chris Lattner58185f22002-10-02 19:38:55 +00001588 }
Chris Lattner2447ef52003-11-20 00:09:43 +00001589
Dan Gohmane2745262009-08-12 17:23:50 +00001590 if (BB->getParent() == 0) {
Chris Lattneree97b8b2009-08-17 15:48:08 +00001591 Out.PadToColumn(50);
Dan Gohmane2745262009-08-12 17:23:50 +00001592 Out << "; Error: Block without parent!";
1593 } else if (BB != &BB->getParent()->getEntryBlock()) { // Not the entry block?
Chris Lattnerfb483622010-09-02 22:52:10 +00001594 // Output predecessors for the block.
Chris Lattneree97b8b2009-08-17 15:48:08 +00001595 Out.PadToColumn(50);
Dan Gohmane2745262009-08-12 17:23:50 +00001596 Out << ";";
Gabor Greif6c6b2fd2010-03-25 23:25:28 +00001597 const_pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001598
Chris Lattnerff834c02008-04-22 02:45:44 +00001599 if (PI == PE) {
1600 Out << " No predecessors!";
1601 } else {
Dan Gohman81313fd2008-09-14 17:21:12 +00001602 Out << " preds = ";
Chris Lattnerff834c02008-04-22 02:45:44 +00001603 writeOperand(*PI, false);
1604 for (++PI; PI != PE; ++PI) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001605 Out << ", ";
Chris Lattner78e2e8b2006-12-06 06:24:27 +00001606 writeOperand(*PI, false);
Chris Lattner00211f12003-11-16 22:59:57 +00001607 }
Chris Lattner58185f22002-10-02 19:38:55 +00001608 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001609 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001610
Chris Lattnerff834c02008-04-22 02:45:44 +00001611 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001612
Misha Brukmana6619a92004-06-21 21:53:56 +00001613 if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001614
Chris Lattnerfee714f2001-09-07 16:36:04 +00001615 // Output all of the instructions in the basic block...
Dan Gohmana4f709e2009-07-13 18:27:59 +00001616 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
Chris Lattner113f4f42002-06-25 16:13:24 +00001617 printInstruction(*I);
Dan Gohmana4f709e2009-07-13 18:27:59 +00001618 Out << '\n';
1619 }
Chris Lattner96cdd272004-03-08 18:51:45 +00001620
Misha Brukmana6619a92004-06-21 21:53:56 +00001621 if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, Out);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001622}
1623
Misha Brukmanc566ca362004-03-02 00:22:19 +00001624/// printInfoComment - Print a little comment after the instruction indicating
1625/// which slot it occupies.
1626///
Chris Lattner113f4f42002-06-25 16:13:24 +00001627void AssemblyWriter::printInfoComment(const Value &V) {
Dan Gohmane34890f2010-02-10 20:41:46 +00001628 if (AnnotationWriter) {
1629 AnnotationWriter->printInfoComment(V, Out);
1630 return;
1631 }
Chris Lattner862e3382001-10-13 06:42:36 +00001632}
1633
Reid Spencere7141c82006-08-28 01:02:49 +00001634// This member is called for each Instruction in a function..
Chris Lattner113f4f42002-06-25 16:13:24 +00001635void AssemblyWriter::printInstruction(const Instruction &I) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001636 if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001637
Dan Gohman466876b2009-08-12 23:32:33 +00001638 // Print out indentation for an instruction.
Dan Gohmanb4583b12009-08-13 01:41:52 +00001639 Out << " ";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001640
1641 // Print out name if it exists...
Chris Lattner033935d2008-08-17 04:40:13 +00001642 if (I.hasName()) {
1643 PrintLLVMName(Out, &I);
1644 Out << " = ";
Chris Lattner031560c2009-12-29 07:25:48 +00001645 } else if (!I.getType()->isVoidTy()) {
Chris Lattnerb25e5ea2008-08-29 17:19:30 +00001646 // Print out the def slot taken.
1647 int SlotNum = Machine.getLocalSlot(&I);
1648 if (SlotNum == -1)
1649 Out << "<badref> = ";
1650 else
1651 Out << '%' << SlotNum << " = ";
Chris Lattner033935d2008-08-17 04:40:13 +00001652 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001653
Chris Lattner06038452005-05-06 05:51:46 +00001654 // If this is a volatile load or store, print out the volatile marker.
Chris Lattner504f9242003-09-08 17:45:59 +00001655 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) ||
Chris Lattner06038452005-05-06 05:51:46 +00001656 (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile())) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001657 Out << "volatile ";
Chris Lattner06038452005-05-06 05:51:46 +00001658 } else if (isa<CallInst>(I) && cast<CallInst>(I).isTailCall()) {
1659 // If this is a call, check if it's a tail call.
1660 Out << "tail ";
1661 }
Chris Lattner504f9242003-09-08 17:45:59 +00001662
Chris Lattner2f7c9632001-06-06 20:29:01 +00001663 // Print out the opcode...
Misha Brukmana6619a92004-06-21 21:53:56 +00001664 Out << I.getOpcodeName();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001665
Dan Gohman9c7f8082009-07-27 16:11:46 +00001666 // Print out optimization information.
1667 WriteOptimizationInfo(Out, &I);
1668
Reid Spencer45e52392006-12-03 06:27:29 +00001669 // Print out the compare instruction predicates
Nate Begemand2195702008-05-12 19:01:56 +00001670 if (const CmpInst *CI = dyn_cast<CmpInst>(&I))
Chris Lattner82ff9232008-08-23 22:52:27 +00001671 Out << ' ' << getPredicateText(CI->getPredicate());
Reid Spencer45e52392006-12-03 06:27:29 +00001672
Chris Lattner2f7c9632001-06-06 20:29:01 +00001673 // Print out the type of the operands...
Chris Lattner113f4f42002-06-25 16:13:24 +00001674 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001675
1676 // Special case conditional branches to swizzle the condition out to the front
Gabor Greifcab008f2009-02-09 15:45:06 +00001677 if (isa<BranchInst>(I) && cast<BranchInst>(I).isConditional()) {
1678 BranchInst &BI(cast<BranchInst>(I));
Dan Gohman81313fd2008-09-14 17:21:12 +00001679 Out << ' ';
Gabor Greifcab008f2009-02-09 15:45:06 +00001680 writeOperand(BI.getCondition(), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00001681 Out << ", ";
Gabor Greifcab008f2009-02-09 15:45:06 +00001682 writeOperand(BI.getSuccessor(0), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00001683 Out << ", ";
Gabor Greifcab008f2009-02-09 15:45:06 +00001684 writeOperand(BI.getSuccessor(1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001685
Chris Lattner8d48df22002-04-13 18:34:38 +00001686 } else if (isa<SwitchInst>(I)) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00001687 // Special case switch instruction to get formatting nice and correct.
Dan Gohman81313fd2008-09-14 17:21:12 +00001688 Out << ' ';
Chris Lattner82ff9232008-08-23 22:52:27 +00001689 writeOperand(Operand , true);
Dan Gohman81313fd2008-09-14 17:21:12 +00001690 Out << ", ";
Chris Lattner82ff9232008-08-23 22:52:27 +00001691 writeOperand(I.getOperand(1), true);
1692 Out << " [";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001693
Chris Lattner113f4f42002-06-25 16:13:24 +00001694 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; op += 2) {
Dan Gohmanb4583b12009-08-13 01:41:52 +00001695 Out << "\n ";
Chris Lattner82ff9232008-08-23 22:52:27 +00001696 writeOperand(I.getOperand(op ), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00001697 Out << ", ";
Chris Lattner113f4f42002-06-25 16:13:24 +00001698 writeOperand(I.getOperand(op+1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001699 }
Dan Gohmanb4583b12009-08-13 01:41:52 +00001700 Out << "\n ]";
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00001701 } else if (isa<IndirectBrInst>(I)) {
1702 // Special case indirectbr instruction to get formatting nice and correct.
Chris Lattner3ed871f2009-10-27 19:13:16 +00001703 Out << ' ';
1704 writeOperand(Operand, true);
Dan Gohman43c57402009-10-30 02:01:10 +00001705 Out << ", [";
Chris Lattner3ed871f2009-10-27 19:13:16 +00001706
1707 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) {
1708 if (i != 1)
1709 Out << ", ";
1710 writeOperand(I.getOperand(i), true);
1711 }
1712 Out << ']';
Bill Wendling6c923bb2011-07-27 20:18:04 +00001713 } else if (isa<ResumeInst>(I)) {
1714 Out << ' ';
1715 writeOperand(Operand, true);
Jay Foad372ad642011-06-20 14:18:48 +00001716 } else if (const PHINode *PN = dyn_cast<PHINode>(&I)) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001717 Out << ' ';
Chris Lattnere101c442009-02-28 21:26:53 +00001718 TypePrinter.print(I.getType(), Out);
Misha Brukmana6619a92004-06-21 21:53:56 +00001719 Out << ' ';
Chris Lattner2f7c9632001-06-06 20:29:01 +00001720
Jay Foad372ad642011-06-20 14:18:48 +00001721 for (unsigned op = 0, Eop = PN->getNumIncomingValues(); op < Eop; ++op) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001722 if (op) Out << ", ";
Dan Gohman81313fd2008-09-14 17:21:12 +00001723 Out << "[ ";
Jay Foad372ad642011-06-20 14:18:48 +00001724 writeOperand(PN->getIncomingValue(op), false); Out << ", ";
1725 writeOperand(PN->getIncomingBlock(op), false); Out << " ]";
Chris Lattner931ef3b2001-06-11 15:04:20 +00001726 }
Dan Gohmana76f0f72008-05-31 19:12:39 +00001727 } else if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(&I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001728 Out << ' ';
Dan Gohmana76f0f72008-05-31 19:12:39 +00001729 writeOperand(I.getOperand(0), true);
1730 for (const unsigned *i = EVI->idx_begin(), *e = EVI->idx_end(); i != e; ++i)
1731 Out << ", " << *i;
1732 } else if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(&I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001733 Out << ' ';
1734 writeOperand(I.getOperand(0), true); Out << ", ";
Dan Gohmana76f0f72008-05-31 19:12:39 +00001735 writeOperand(I.getOperand(1), true);
1736 for (const unsigned *i = IVI->idx_begin(), *e = IVI->idx_end(); i != e; ++i)
1737 Out << ", " << *i;
Bill Wendling6c923bb2011-07-27 20:18:04 +00001738 } else if (const LandingPadInst *LPI = dyn_cast<LandingPadInst>(&I)) {
1739 Out << ' ';
1740 TypePrinter.print(I.getType(), Out);
1741 Out << " personality ";
1742 writeOperand(LPI->getPersonalityFn(), true); Out << '\n';
1743
1744 if (LPI->isCleanup())
1745 Out << " cleanup";
1746
1747 for (unsigned i = 0, e = LPI->getNumClauses(); i != e; ) {
1748 if (i != 0 || LPI->isCleanup()) Out << "\n";
1749
1750 SmallVector<const Value*, 8> Vals;
1751 LandingPadInst::ClauseType CT = LPI->getClauseType(i);
1752 for (; i != e && LPI->getClauseType(i) == CT; ++i)
1753 Vals.push_back(LPI->getClauseValue(i));
1754
1755 if (CT == LandingPadInst::Catch)
1756 Out << " catch ";
1757 else
1758 Out << " filter ";
1759
1760 for (unsigned II = 0, IE = Vals.size(); II != IE; ++II) {
1761 if (II != 0) Out << ", ";
1762 writeOperand(Vals[II], true);
1763 }
1764 }
Devang Patel59643e52008-02-23 00:35:18 +00001765 } else if (isa<ReturnInst>(I) && !Operand) {
1766 Out << " void";
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001767 } else if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
1768 // Print the calling convention being used.
1769 switch (CI->getCallingConv()) {
1770 case CallingConv::C: break; // default
Chris Lattner29d20852006-05-19 21:58:52 +00001771 case CallingConv::Fast: Out << " fastcc"; break;
1772 case CallingConv::Cold: Out << " coldcc"; break;
Chris Lattnerf5270372007-11-18 18:32:16 +00001773 case CallingConv::X86_StdCall: Out << " x86_stdcallcc"; break;
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001774 case CallingConv::X86_FastCall: Out << " x86_fastcallcc"; break;
Anton Korobeynikov8f35fab2010-05-16 09:08:45 +00001775 case CallingConv::X86_ThisCall: Out << " x86_thiscallcc"; break;
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001776 case CallingConv::ARM_APCS: Out << " arm_apcscc "; break;
1777 case CallingConv::ARM_AAPCS: Out << " arm_aapcscc "; break;
1778 case CallingConv::ARM_AAPCS_VFP:Out << " arm_aapcs_vfpcc "; break;
Anton Korobeynikov27a0ecf2009-12-07 02:27:35 +00001779 case CallingConv::MSP430_INTR: Out << " msp430_intrcc "; break;
Che-Liang Chiou29947902010-09-25 07:46:17 +00001780 case CallingConv::PTX_Kernel: Out << " ptx_kernel"; break;
1781 case CallingConv::PTX_Device: Out << " ptx_device"; break;
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001782 default: Out << " cc" << CI->getCallingConv(); break;
1783 }
1784
Gabor Greifc9a92512010-06-23 13:09:06 +00001785 Operand = CI->getCalledValue();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001786 PointerType *PTy = cast<PointerType>(Operand->getType());
1787 FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1788 Type *RetTy = FTy->getReturnType();
Devang Patel4c758ea2008-09-25 21:00:45 +00001789 const AttrListPtr &PAL = CI->getAttributes();
Chris Lattner2f2d9472001-11-06 21:28:12 +00001790
Devang Patel221fe422008-09-29 20:49:50 +00001791 if (PAL.getRetAttributes() != Attribute::None)
1792 Out << ' ' << Attribute::getAsString(PAL.getRetAttributes());
1793
Chris Lattner463d6a52003-08-05 15:34:45 +00001794 // If possible, print out the short form of the call instruction. We can
Chris Lattner6915f8f2002-04-07 22:49:37 +00001795 // only do this if the first argument is a pointer to a nonvararg function,
Chris Lattner463d6a52003-08-05 15:34:45 +00001796 // and if the return type is not a pointer to a function.
Chris Lattner2f2d9472001-11-06 21:28:12 +00001797 //
Dan Gohman81313fd2008-09-14 17:21:12 +00001798 Out << ' ';
Chris Lattner463d6a52003-08-05 15:34:45 +00001799 if (!FTy->isVarArg() &&
Duncan Sands19d0b472010-02-16 11:11:14 +00001800 (!RetTy->isPointerTy() ||
1801 !cast<PointerType>(RetTy)->getElementType()->isFunctionTy())) {
Chris Lattnere101c442009-02-28 21:26:53 +00001802 TypePrinter.print(RetTy, Out);
Dan Gohman81313fd2008-09-14 17:21:12 +00001803 Out << ' ';
Chris Lattner2f2d9472001-11-06 21:28:12 +00001804 writeOperand(Operand, false);
1805 } else {
1806 writeOperand(Operand, true);
1807 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001808 Out << '(';
Gabor Greifc9a92512010-06-23 13:09:06 +00001809 for (unsigned op = 0, Eop = CI->getNumArgOperands(); op < Eop; ++op) {
1810 if (op > 0)
Dan Gohman81313fd2008-09-14 17:21:12 +00001811 Out << ", ";
Gabor Greifc9a92512010-06-23 13:09:06 +00001812 writeParamOperand(CI->getArgOperand(op), PAL.getParamAttributes(op + 1));
Chris Lattner2f7c9632001-06-06 20:29:01 +00001813 }
Dan Gohman81313fd2008-09-14 17:21:12 +00001814 Out << ')';
Devang Patela05633e2008-09-26 22:53:05 +00001815 if (PAL.getFnAttributes() != Attribute::None)
1816 Out << ' ' << Attribute::getAsString(PAL.getFnAttributes());
Chris Lattner113f4f42002-06-25 16:13:24 +00001817 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
Gabor Greifa2fbc0a2010-03-24 13:21:49 +00001818 Operand = II->getCalledValue();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001819 PointerType *PTy = cast<PointerType>(Operand->getType());
1820 FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1821 Type *RetTy = FTy->getReturnType();
Devang Patel4c758ea2008-09-25 21:00:45 +00001822 const AttrListPtr &PAL = II->getAttributes();
Chris Lattner463d6a52003-08-05 15:34:45 +00001823
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001824 // Print the calling convention being used.
1825 switch (II->getCallingConv()) {
1826 case CallingConv::C: break; // default
Chris Lattner29d20852006-05-19 21:58:52 +00001827 case CallingConv::Fast: Out << " fastcc"; break;
1828 case CallingConv::Cold: Out << " coldcc"; break;
Dan Gohman81313fd2008-09-14 17:21:12 +00001829 case CallingConv::X86_StdCall: Out << " x86_stdcallcc"; break;
1830 case CallingConv::X86_FastCall: Out << " x86_fastcallcc"; break;
Anton Korobeynikov8f35fab2010-05-16 09:08:45 +00001831 case CallingConv::X86_ThisCall: Out << " x86_thiscallcc"; break;
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001832 case CallingConv::ARM_APCS: Out << " arm_apcscc "; break;
1833 case CallingConv::ARM_AAPCS: Out << " arm_aapcscc "; break;
1834 case CallingConv::ARM_AAPCS_VFP:Out << " arm_aapcs_vfpcc "; break;
Anton Korobeynikov27a0ecf2009-12-07 02:27:35 +00001835 case CallingConv::MSP430_INTR: Out << " msp430_intrcc "; break;
Che-Liang Chiou29947902010-09-25 07:46:17 +00001836 case CallingConv::PTX_Kernel: Out << " ptx_kernel"; break;
1837 case CallingConv::PTX_Device: Out << " ptx_device"; break;
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001838 default: Out << " cc" << II->getCallingConv(); break;
1839 }
1840
Devang Patel221fe422008-09-29 20:49:50 +00001841 if (PAL.getRetAttributes() != Attribute::None)
1842 Out << ' ' << Attribute::getAsString(PAL.getRetAttributes());
1843
Chris Lattner463d6a52003-08-05 15:34:45 +00001844 // If possible, print out the short form of the invoke instruction. We can
1845 // only do this if the first argument is a pointer to a nonvararg function,
1846 // and if the return type is not a pointer to a function.
1847 //
Dan Gohmanc7e00ba2008-10-15 18:02:08 +00001848 Out << ' ';
Chris Lattner463d6a52003-08-05 15:34:45 +00001849 if (!FTy->isVarArg() &&
Duncan Sands19d0b472010-02-16 11:11:14 +00001850 (!RetTy->isPointerTy() ||
1851 !cast<PointerType>(RetTy)->getElementType()->isFunctionTy())) {
Chris Lattnere101c442009-02-28 21:26:53 +00001852 TypePrinter.print(RetTy, Out);
Dan Gohmanc7e00ba2008-10-15 18:02:08 +00001853 Out << ' ';
Chris Lattner463d6a52003-08-05 15:34:45 +00001854 writeOperand(Operand, false);
1855 } else {
1856 writeOperand(Operand, true);
1857 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001858 Out << '(';
Gabor Greifc9a92512010-06-23 13:09:06 +00001859 for (unsigned op = 0, Eop = II->getNumArgOperands(); op < Eop; ++op) {
Gabor Greifa2fbc0a2010-03-24 13:21:49 +00001860 if (op)
Dan Gohman81313fd2008-09-14 17:21:12 +00001861 Out << ", ";
Gabor Greifc9a92512010-06-23 13:09:06 +00001862 writeParamOperand(II->getArgOperand(op), PAL.getParamAttributes(op + 1));
Chris Lattner862e3382001-10-13 06:42:36 +00001863 }
1864
Dan Gohman81313fd2008-09-14 17:21:12 +00001865 Out << ')';
Devang Patela05633e2008-09-26 22:53:05 +00001866 if (PAL.getFnAttributes() != Attribute::None)
1867 Out << ' ' << Attribute::getAsString(PAL.getFnAttributes());
1868
Dan Gohmanb4583b12009-08-13 01:41:52 +00001869 Out << "\n to ";
Chris Lattner862e3382001-10-13 06:42:36 +00001870 writeOperand(II->getNormalDest(), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00001871 Out << " unwind ";
Chris Lattnerfae8ab32004-02-08 21:44:31 +00001872 writeOperand(II->getUnwindDest(), true);
Chris Lattner862e3382001-10-13 06:42:36 +00001873
Victor Hernandez8acf2952009-10-23 21:09:37 +00001874 } else if (const AllocaInst *AI = dyn_cast<AllocaInst>(&I)) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001875 Out << ' ';
Chris Lattnere101c442009-02-28 21:26:53 +00001876 TypePrinter.print(AI->getType()->getElementType(), Out);
Dan Gohmanb53e26c2009-07-31 18:23:24 +00001877 if (!AI->getArraySize() || AI->isArrayAllocation()) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001878 Out << ", ";
Chris Lattner8d48df22002-04-13 18:34:38 +00001879 writeOperand(AI->getArraySize(), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001880 }
Nate Begeman848622f2005-11-05 09:21:28 +00001881 if (AI->getAlignment()) {
Chris Lattner7aeee3a2005-11-05 21:20:34 +00001882 Out << ", align " << AI->getAlignment();
Nate Begeman848622f2005-11-05 09:21:28 +00001883 }
Chris Lattner862e3382001-10-13 06:42:36 +00001884 } else if (isa<CastInst>(I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001885 if (Operand) {
1886 Out << ' ';
1887 writeOperand(Operand, true); // Work with broken code
1888 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001889 Out << " to ";
Chris Lattnere101c442009-02-28 21:26:53 +00001890 TypePrinter.print(I.getType(), Out);
Chris Lattner5b337482003-10-18 05:57:43 +00001891 } else if (isa<VAArgInst>(I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001892 if (Operand) {
1893 Out << ' ';
1894 writeOperand(Operand, true); // Work with broken code
1895 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001896 Out << ", ";
Chris Lattnere101c442009-02-28 21:26:53 +00001897 TypePrinter.print(I.getType(), Out);
1898 } else if (Operand) { // Print the normal way.
Chris Lattner2f7c9632001-06-06 20:29:01 +00001899
Misha Brukmanb1c93172005-04-21 23:48:37 +00001900 // PrintAllTypes - Instructions who have operands of all the same type
Chris Lattner2f7c9632001-06-06 20:29:01 +00001901 // omit the type from all but the first operand. If the instruction has
1902 // different type operands (for example br), then they are all printed.
1903 bool PrintAllTypes = false;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001904 Type *TheType = Operand->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001905
Reid Spencer0cdd04f2007-02-02 13:54:55 +00001906 // Select, Store and ShuffleVector always print all types.
Devang Patelce556d92008-03-04 22:05:14 +00001907 if (isa<SelectInst>(I) || isa<StoreInst>(I) || isa<ShuffleVectorInst>(I)
1908 || isa<ReturnInst>(I)) {
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00001909 PrintAllTypes = true;
1910 } else {
1911 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
1912 Operand = I.getOperand(i);
Nuno Lopes04fe2f02009-01-15 18:40:57 +00001913 // note that Operand shouldn't be null, but the test helps make dump()
1914 // more tolerant of malformed IR
Nuno Lopes0971e772009-01-14 17:51:41 +00001915 if (Operand && Operand->getType() != TheType) {
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00001916 PrintAllTypes = true; // We have differing types! Print them all!
1917 break;
1918 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001919 }
1920 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001921
Chris Lattner7bfee412001-10-29 16:05:51 +00001922 if (!PrintAllTypes) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001923 Out << ' ';
Chris Lattnere101c442009-02-28 21:26:53 +00001924 TypePrinter.print(TheType, Out);
Chris Lattner7bfee412001-10-29 16:05:51 +00001925 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001926
Dan Gohman81313fd2008-09-14 17:21:12 +00001927 Out << ' ';
Chris Lattner113f4f42002-06-25 16:13:24 +00001928 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001929 if (i) Out << ", ";
Chris Lattner113f4f42002-06-25 16:13:24 +00001930 writeOperand(I.getOperand(i), PrintAllTypes);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001931 }
1932 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001933
Chris Lattnerc9558df2009-12-28 20:10:43 +00001934 // Print post operand alignment for load/store.
Christopher Lamb84485702007-04-22 19:24:39 +00001935 if (isa<LoadInst>(I) && cast<LoadInst>(I).getAlignment()) {
1936 Out << ", align " << cast<LoadInst>(I).getAlignment();
1937 } else if (isa<StoreInst>(I) && cast<StoreInst>(I).getAlignment()) {
1938 Out << ", align " << cast<StoreInst>(I).getAlignment();
Eli Friedmanfee02c62011-07-25 23:16:38 +00001939 } else if (const FenceInst *FI = dyn_cast<FenceInst>(&I)) {
1940 writeAtomic(FI->getOrdering(), FI->getSynchScope());
Christopher Lamb84485702007-04-22 19:24:39 +00001941 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001942
Chris Lattnerc9558df2009-12-28 20:10:43 +00001943 // Print Metadata info.
Nick Lewyckyba8ec9a2010-02-25 06:53:04 +00001944 SmallVector<std::pair<unsigned, MDNode*>, 4> InstMD;
1945 I.getAllMetadata(InstMD);
Erick Tryzelaar72a37132010-03-02 05:32:52 +00001946 if (!InstMD.empty()) {
1947 SmallVector<StringRef, 8> MDNames;
1948 I.getType()->getContext().getMDKindNames(MDNames);
1949 for (unsigned i = 0, e = InstMD.size(); i != e; ++i) {
1950 unsigned Kind = InstMD[i].first;
1951 if (Kind < MDNames.size()) {
1952 Out << ", !" << MDNames[Kind];
1953 } else {
1954 Out << ", !<unknown kind #" << Kind << ">";
1955 }
Dan Gohmana2489d12010-07-20 23:55:01 +00001956 Out << ' ';
1957 WriteAsOperandInternal(Out, InstMD[i].second, &TypePrinter, &Machine,
1958 TheModule);
Nick Lewyckyba8ec9a2010-02-25 06:53:04 +00001959 }
Devang Patelbcdb0252009-10-07 16:37:55 +00001960 }
Chris Lattner862e3382001-10-13 06:42:36 +00001961 printInfoComment(I);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001962}
1963
Chris Lattnerbddea6a2009-12-31 02:13:35 +00001964static void WriteMDNodeComment(const MDNode *Node,
Duncan Sands41b4a6b2010-07-12 08:16:59 +00001965 formatted_raw_ostream &Out) {
Chris Lattnerbddea6a2009-12-31 02:13:35 +00001966 if (Node->getNumOperands() < 1)
1967 return;
1968 ConstantInt *CI = dyn_cast_or_null<ConstantInt>(Node->getOperand(0));
1969 if (!CI) return;
Dan Gohman2fb68302010-05-07 22:15:24 +00001970 APInt Val = CI->getValue();
1971 APInt Tag = Val & ~APInt(Val.getBitWidth(), LLVMDebugVersionMask);
1972 if (Val.ult(LLVMDebugVersion))
Chris Lattnerbddea6a2009-12-31 02:13:35 +00001973 return;
1974
1975 Out.PadToColumn(50);
Devang Patel0ca3d1e2011-02-18 23:13:40 +00001976 if (Tag == dwarf::DW_TAG_user_base)
Chris Lattnerbddea6a2009-12-31 02:13:35 +00001977 Out << "; [ DW_TAG_user_base ]";
Dan Gohman2fb68302010-05-07 22:15:24 +00001978 else if (Tag.isIntN(32)) {
1979 if (const char *TagName = dwarf::TagString(Tag.getZExtValue()))
1980 Out << "; [ " << TagName << " ]";
1981 }
Chris Lattnerbddea6a2009-12-31 02:13:35 +00001982}
1983
1984void AssemblyWriter::writeAllMDNodes() {
1985 SmallVector<const MDNode *, 16> Nodes;
Chris Lattnercf4a76e2009-12-31 02:20:11 +00001986 Nodes.resize(Machine.mdn_size());
1987 for (SlotTracker::mdn_iterator I = Machine.mdn_begin(), E = Machine.mdn_end();
1988 I != E; ++I)
Chris Lattnerbddea6a2009-12-31 02:13:35 +00001989 Nodes[I->second] = cast<MDNode>(I->first);
1990
1991 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
1992 Out << '!' << i << " = metadata ";
Chris Lattner0d50bdd2009-12-31 02:27:30 +00001993 printMDNodeBody(Nodes[i]);
Chris Lattnerbddea6a2009-12-31 02:13:35 +00001994 }
1995}
1996
1997void AssemblyWriter::printMDNodeBody(const MDNode *Node) {
Dan Gohmana2489d12010-07-20 23:55:01 +00001998 WriteMDNodeBodyInternal(Out, Node, &TypePrinter, &Machine, TheModule);
Chris Lattnerbddea6a2009-12-31 02:13:35 +00001999 WriteMDNodeComment(Node, Out);
2000 Out << "\n";
2001}
Chris Lattner2f7c9632001-06-06 20:29:01 +00002002
2003//===----------------------------------------------------------------------===//
2004// External Interface declarations
2005//===----------------------------------------------------------------------===//
2006
Dan Gohmane2745262009-08-12 17:23:50 +00002007void Module::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW) const {
Chris Lattnere36fd8a2008-08-19 04:26:57 +00002008 SlotTracker SlotTable(this);
Dan Gohmane2745262009-08-12 17:23:50 +00002009 formatted_raw_ostream OS(ROS);
Chris Lattner0c19df42008-08-23 22:23:09 +00002010 AssemblyWriter W(OS, SlotTable, this, AAW);
Chris Lattnerefb5e392009-12-31 02:23:35 +00002011 W.printModule(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00002012}
2013
Dan Gohman2637cc12010-07-21 23:38:33 +00002014void NamedMDNode::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW) const {
2015 SlotTracker SlotTable(getParent());
2016 formatted_raw_ostream OS(ROS);
2017 AssemblyWriter W(OS, SlotTable, getParent(), AAW);
2018 W.printNamedMDNode(this);
2019}
2020
Chris Lattner2ee62d22009-02-28 21:11:05 +00002021void Type::print(raw_ostream &OS) const {
2022 if (this == 0) {
2023 OS << "<null Type>";
2024 return;
2025 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002026 TypePrinting TP;
2027 TP.print(const_cast<Type*>(this), OS);
2028
2029 // If the type is a named struct type, print the body as well.
2030 if (StructType *STy = dyn_cast<StructType>(const_cast<Type*>(this)))
2031 if (!STy->isAnonymous()) {
2032 OS << " = type ";
2033 TP.printStructBody(STy, OS);
2034 }
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00002035}
2036
Dan Gohmane2745262009-08-12 17:23:50 +00002037void Value::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW) const {
Chris Lattner0c19df42008-08-23 22:23:09 +00002038 if (this == 0) {
Dan Gohman12dad632009-08-12 20:56:03 +00002039 ROS << "printing a <null> value\n";
Chris Lattner0c19df42008-08-23 22:23:09 +00002040 return;
2041 }
Dan Gohman12dad632009-08-12 20:56:03 +00002042 formatted_raw_ostream OS(ROS);
Chris Lattner0c19df42008-08-23 22:23:09 +00002043 if (const Instruction *I = dyn_cast<Instruction>(this)) {
2044 const Function *F = I->getParent() ? I->getParent()->getParent() : 0;
2045 SlotTracker SlotTable(F);
Chris Lattnerd5bace72009-12-31 08:23:09 +00002046 AssemblyWriter W(OS, SlotTable, getModuleFromVal(I), AAW);
Chris Lattnerefb5e392009-12-31 02:23:35 +00002047 W.printInstruction(*I);
Chris Lattner0c19df42008-08-23 22:23:09 +00002048 } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(this)) {
2049 SlotTracker SlotTable(BB->getParent());
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002050 AssemblyWriter W(OS, SlotTable, getModuleFromVal(BB), AAW);
Chris Lattnerefb5e392009-12-31 02:23:35 +00002051 W.printBasicBlock(BB);
Chris Lattner0c19df42008-08-23 22:23:09 +00002052 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(this)) {
2053 SlotTracker SlotTable(GV->getParent());
Dan Gohman6c7a4852009-04-20 16:10:33 +00002054 AssemblyWriter W(OS, SlotTable, GV->getParent(), AAW);
Chris Lattnerefb5e392009-12-31 02:23:35 +00002055 if (const GlobalVariable *V = dyn_cast<GlobalVariable>(GV))
2056 W.printGlobal(V);
2057 else if (const Function *F = dyn_cast<Function>(GV))
2058 W.printFunction(F);
2059 else
2060 W.printAlias(cast<GlobalAlias>(GV));
Devang Patel5546b982009-07-01 20:59:15 +00002061 } else if (const MDNode *N = dyn_cast<MDNode>(this)) {
Victor Hernandeze5f2af72010-01-20 04:45:57 +00002062 const Function *F = N->getFunction();
Victor Hernandez61e6e822010-01-14 01:47:37 +00002063 SlotTracker SlotTable(F);
Dan Gohman15132172010-07-14 21:12:44 +00002064 AssemblyWriter W(OS, SlotTable, F ? F->getParent() : 0, AAW);
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002065 W.printMDNodeBody(N);
Chris Lattner0c19df42008-08-23 22:23:09 +00002066 } else if (const Constant *C = dyn_cast<Constant>(this)) {
Chris Lattner92c5c122009-02-28 23:20:19 +00002067 TypePrinting TypePrinter;
Chris Lattnere101c442009-02-28 21:26:53 +00002068 TypePrinter.print(C->getType(), OS);
Chris Lattner2ee62d22009-02-28 21:11:05 +00002069 OS << ' ';
Dan Gohmana2489d12010-07-20 23:55:01 +00002070 WriteConstantInternal(OS, C, TypePrinter, 0, 0);
Chris Lattner31fcc282009-12-31 01:41:14 +00002071 } else if (isa<InlineAsm>(this) || isa<MDString>(this) ||
2072 isa<Argument>(this)) {
Chris Lattner0c19df42008-08-23 22:23:09 +00002073 WriteAsOperand(OS, this, true, 0);
2074 } else {
Dan Gohmanc0353bf2009-09-23 01:33:16 +00002075 // Otherwise we don't know what it is. Call the virtual function to
2076 // allow a subclass to print itself.
2077 printCustom(OS);
Chris Lattner0c19df42008-08-23 22:23:09 +00002078 }
2079}
2080
Dan Gohmanc0353bf2009-09-23 01:33:16 +00002081// Value::printCustom - subclasses should override this to implement printing.
2082void Value::printCustom(raw_ostream &OS) const {
2083 llvm_unreachable("Unknown value to print out!");
2084}
2085
Chris Lattnerdab942552008-08-25 17:03:15 +00002086// Value::dump - allow easy printing of Values from the debugger.
David Greenec7f9b122010-01-05 01:29:26 +00002087void Value::dump() const { print(dbgs()); dbgs() << '\n'; }
Reid Spencer52641832004-05-25 18:14:38 +00002088
Chris Lattnerdab942552008-08-25 17:03:15 +00002089// Type::dump - allow easy printing of Types from the debugger.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002090void Type::dump() const { print(dbgs()); }
Chris Lattner24025262009-02-28 21:05:51 +00002091
Chris Lattnerdab942552008-08-25 17:03:15 +00002092// Module::dump() - Allow printing of Modules from the debugger.
David Greenec7f9b122010-01-05 01:29:26 +00002093void Module::dump() const { print(dbgs(), 0); }