blob: b6d98b85735c7953883583e9e1ed72580190b46f [file] [log] [blame]
Chris Lattner8da78af2002-04-07 22:31:46 +00001//===-- AsmWriter.cpp - Printing LLVM as an assembly file -----------------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
10// This library implements the functionality defined in llvm/Assembly/Writer.h
11//
Chris Lattner02b93992002-04-12 18:21:53 +000012// Note that these routines must be extremely tolerant of various errors in the
Chris Lattner8f77dae2003-05-08 02:44:12 +000013// LLVM code, because it can be used for debugging transformations.
Chris Lattner02b93992002-04-12 18:21:53 +000014//
Chris Lattner00950542001-06-06 20:29:01 +000015//===----------------------------------------------------------------------===//
16
Chris Lattner75cf7cf2002-04-08 22:03:40 +000017#include "llvm/Assembly/Writer.h"
Chris Lattnerf082b802002-07-23 18:07:49 +000018#include "llvm/Assembly/PrintModulePass.h"
Chris Lattner95e5a2c2003-10-30 23:41:03 +000019#include "llvm/Assembly/AsmAnnotationWriter.h"
Chris Lattnerd5118982005-05-06 20:26:43 +000020#include "llvm/CallingConv.h"
Chris Lattnerf2d577b2004-01-20 19:50:34 +000021#include "llvm/Constants.h"
Chris Lattner3eb59c02002-04-29 18:46:50 +000022#include "llvm/DerivedTypes.h"
Chris Lattner863517a2006-01-25 18:57:27 +000023#include "llvm/InlineAsm.h"
Vikram S. Adveb4dbb442002-07-14 23:14:45 +000024#include "llvm/Instruction.h"
Misha Brukman44336292004-07-29 16:53:53 +000025#include "llvm/Instructions.h"
Dan Gohman1224c382009-07-20 21:19:07 +000026#include "llvm/Operator.h"
Devang Patel0a9f7b92009-07-28 21:49:47 +000027#include "llvm/Metadata.h"
Chris Lattnerf2d577b2004-01-20 19:50:34 +000028#include "llvm/Module.h"
Reid Spenceref9b9a72007-02-05 20:47:22 +000029#include "llvm/ValueSymbolTable.h"
Reid Spencer78d033e2007-01-06 07:24:44 +000030#include "llvm/TypeSymbolTable.h"
Chris Lattner413fd232009-03-01 00:03:38 +000031#include "llvm/ADT/DenseSet.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000032#include "llvm/ADT/StringExtras.h"
33#include "llvm/ADT/STLExtras.h"
Bill Wendling8f487662006-11-28 02:09:03 +000034#include "llvm/Support/CFG.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000035#include "llvm/Support/ErrorHandling.h"
Jim Laskeycb6682f2005-08-17 19:34:49 +000036#include "llvm/Support/MathExtras.h"
Dan Gohman683e9222009-08-12 17:23:50 +000037#include "llvm/Support/FormattedStream.h"
Chris Lattner007377f2001-09-07 16:36:04 +000038#include <algorithm>
Reid Spencer4ad513c2007-05-22 19:27:35 +000039#include <cctype>
Devang Patel923078c2009-07-01 19:21:12 +000040#include <map>
Chris Lattner31f84992003-11-21 20:23:48 +000041using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000042
Reid Spenceredd5d9e2005-05-15 16:13:11 +000043// Make virtual table appear in this compilation unit.
44AssemblyAnnotationWriter::~AssemblyAnnotationWriter() {}
45
Chris Lattner6ab910b2008-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;
53
54 if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
55 return BB->getParent() ? BB->getParent()->getParent() : 0;
56
57 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 }
61
62 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
63 return GV->getParent();
64 return 0;
65}
66
Daniel Dunbare9da1332008-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.
Dan Gohman683e9222009-08-12 17:23:50 +000069static void PrintEscapedString(const StringRef &Name,
Dan Gohman1220e102009-08-12 20:56:03 +000070 raw_ostream &Out) {
Daniel Dunbar03d76512009-07-25 23:55:21 +000071 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
72 unsigned char C = Name[i];
Nick Lewycky34a40862009-03-15 06:39:52 +000073 if (isprint(C) && C != '\\' && C != '"')
Daniel Dunbare9da1332008-10-28 19:33:02 +000074 Out << C;
75 else
76 Out << '\\' << hexdigit(C >> 4) << hexdigit(C & 0x0F);
77 }
78}
79
Chris Lattner6ab910b2008-08-19 04:36:02 +000080enum PrefixType {
81 GlobalPrefix,
82 LabelPrefix,
Daniel Dunbarcad35802008-10-14 23:28:09 +000083 LocalPrefix,
84 NoPrefix
Chris Lattner6ab910b2008-08-19 04:36:02 +000085};
86
87/// PrintLLVMName - Turn the specified name into an 'LLVM name', which is either
88/// prefixed with % (if the string only contains simple characters) or is
89/// surrounded with ""'s (if it has special chars in it). Print it out.
Dan Gohman1220e102009-08-12 20:56:03 +000090static void PrintLLVMName(raw_ostream &OS, const StringRef &Name,
Daniel Dunbar03d76512009-07-25 23:55:21 +000091 PrefixType Prefix) {
92 assert(Name.data() && "Cannot get empty name!");
Chris Lattner6ab910b2008-08-19 04:36:02 +000093 switch (Prefix) {
Torok Edwinc23197a2009-07-14 16:55:14 +000094 default: llvm_unreachable("Bad prefix!");
Daniel Dunbarcad35802008-10-14 23:28:09 +000095 case NoPrefix: break;
Chris Lattner52b26de2008-08-19 05:16:28 +000096 case GlobalPrefix: OS << '@'; break;
97 case LabelPrefix: break;
98 case LocalPrefix: OS << '%'; break;
Nick Lewycky04234832009-03-19 06:31:22 +000099 }
Chris Lattner6ab910b2008-08-19 04:36:02 +0000100
101 // Scan the name to see if it needs quotes first.
Daniel Dunbar03d76512009-07-25 23:55:21 +0000102 bool NeedsQuotes = isdigit(Name[0]);
Chris Lattner6ab910b2008-08-19 04:36:02 +0000103 if (!NeedsQuotes) {
Daniel Dunbar03d76512009-07-25 23:55:21 +0000104 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
105 char C = Name[i];
Chris Lattner6ab910b2008-08-19 04:36:02 +0000106 if (!isalnum(C) && C != '-' && C != '.' && C != '_') {
107 NeedsQuotes = true;
108 break;
109 }
110 }
111 }
112
113 // If we didn't need any quotes, just write out the name in one blast.
114 if (!NeedsQuotes) {
Daniel Dunbar03d76512009-07-25 23:55:21 +0000115 OS << Name;
Chris Lattner6ab910b2008-08-19 04:36:02 +0000116 return;
117 }
118
119 // Okay, we need quotes. Output the quotes and escape any scary characters as
120 // needed.
121 OS << '"';
Daniel Dunbar03d76512009-07-25 23:55:21 +0000122 PrintEscapedString(Name, OS);
Chris Lattner6ab910b2008-08-19 04:36:02 +0000123 OS << '"';
124}
125
126/// PrintLLVMName - Turn the specified name into an 'LLVM name', which is either
127/// prefixed with % (if the string only contains simple characters) or is
128/// surrounded with ""'s (if it has special chars in it). Print it out.
Dan Gohman1220e102009-08-12 20:56:03 +0000129static void PrintLLVMName(raw_ostream &OS, const Value *V) {
Daniel Dunbar03d76512009-07-25 23:55:21 +0000130 PrintLLVMName(OS, V->getName(),
Chris Lattner6ab910b2008-08-19 04:36:02 +0000131 isa<GlobalValue>(V) ? GlobalPrefix : LocalPrefix);
132}
133
Chris Lattner9cc34462009-02-28 20:25:14 +0000134//===----------------------------------------------------------------------===//
135// TypePrinting Class: Type printing machinery
136//===----------------------------------------------------------------------===//
137
Chris Lattner87185e82009-02-28 23:03:55 +0000138static DenseMap<const Type *, std::string> &getTypeNamesMap(void *M) {
139 return *static_cast<DenseMap<const Type *, std::string>*>(M);
Chris Lattnerd8030a72009-02-28 22:34:45 +0000140}
141
142void TypePrinting::clear() {
143 getTypeNamesMap(TypeNames).clear();
144}
Chris Lattner9cc34462009-02-28 20:25:14 +0000145
Chris Lattnere9fa33e2009-02-28 23:20:19 +0000146bool TypePrinting::hasTypeName(const Type *Ty) const {
147 return getTypeNamesMap(TypeNames).count(Ty);
148}
149
150void TypePrinting::addTypeName(const Type *Ty, const std::string &N) {
151 getTypeNamesMap(TypeNames).insert(std::make_pair(Ty, N));
152}
153
154
155TypePrinting::TypePrinting() {
Chris Lattner87185e82009-02-28 23:03:55 +0000156 TypeNames = new DenseMap<const Type *, std::string>();
Chris Lattner9cc34462009-02-28 20:25:14 +0000157}
158
Chris Lattnerd8030a72009-02-28 22:34:45 +0000159TypePrinting::~TypePrinting() {
160 delete &getTypeNamesMap(TypeNames);
161}
162
Chris Lattner534361e2009-02-28 20:49:40 +0000163/// CalcTypeName - Write the specified type to the specified raw_ostream, making
164/// use of type names or up references to shorten the type name where possible.
165void TypePrinting::CalcTypeName(const Type *Ty,
Chris Lattnerb840cac2009-02-28 20:34:19 +0000166 SmallVectorImpl<const Type *> &TypeStack,
Chris Lattnerd2b6cb02009-03-01 01:16:21 +0000167 raw_ostream &OS, bool IgnoreTopLevelName) {
Chris Lattner9cc34462009-02-28 20:25:14 +0000168 // Check to see if the type is named.
Chris Lattnerd2b6cb02009-03-01 01:16:21 +0000169 if (!IgnoreTopLevelName) {
Nick Lewycky04234832009-03-19 06:31:22 +0000170 DenseMap<const Type *, std::string> &TM = getTypeNamesMap(TypeNames);
Chris Lattnerd2b6cb02009-03-01 01:16:21 +0000171 DenseMap<const Type *, std::string>::iterator I = TM.find(Ty);
172 if (I != TM.end()) {
173 OS << I->second;
174 return;
175 }
Chris Lattner9cc34462009-02-28 20:25:14 +0000176 }
177
178 // Check to see if the Type is already on the stack...
179 unsigned Slot = 0, CurSize = TypeStack.size();
180 while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type
181
182 // This is another base case for the recursion. In this case, we know
183 // that we have looped back to a type that we have previously visited.
184 // Generate the appropriate upreference to handle this.
185 if (Slot < CurSize) {
Chris Lattner30794262009-02-28 21:27:31 +0000186 OS << '\\' << unsigned(CurSize-Slot); // Here's the upreference
Chris Lattner9cc34462009-02-28 20:25:14 +0000187 return;
188 }
189
190 TypeStack.push_back(Ty); // Recursive case: Add us to the stack..
191
192 switch (Ty->getTypeID()) {
Chris Lattner30794262009-02-28 21:27:31 +0000193 case Type::VoidTyID: OS << "void"; break;
194 case Type::FloatTyID: OS << "float"; break;
195 case Type::DoubleTyID: OS << "double"; break;
196 case Type::X86_FP80TyID: OS << "x86_fp80"; break;
197 case Type::FP128TyID: OS << "fp128"; break;
198 case Type::PPC_FP128TyID: OS << "ppc_fp128"; break;
199 case Type::LabelTyID: OS << "label"; break;
Nick Lewycky7a0370f2009-05-30 05:06:04 +0000200 case Type::MetadataTyID: OS << "metadata"; break;
Chris Lattner583ffd82009-02-28 21:18:43 +0000201 case Type::IntegerTyID:
Chris Lattner30794262009-02-28 21:27:31 +0000202 OS << 'i' << cast<IntegerType>(Ty)->getBitWidth();
Chris Lattner583ffd82009-02-28 21:18:43 +0000203 break;
204
Chris Lattner36942d72009-02-28 20:35:42 +0000205 case Type::FunctionTyID: {
206 const FunctionType *FTy = cast<FunctionType>(Ty);
Chris Lattner30794262009-02-28 21:27:31 +0000207 CalcTypeName(FTy->getReturnType(), TypeStack, OS);
208 OS << " (";
Chris Lattner36942d72009-02-28 20:35:42 +0000209 for (FunctionType::param_iterator I = FTy->param_begin(),
210 E = FTy->param_end(); I != E; ++I) {
211 if (I != FTy->param_begin())
Chris Lattner30794262009-02-28 21:27:31 +0000212 OS << ", ";
213 CalcTypeName(*I, TypeStack, OS);
Chris Lattner9cc34462009-02-28 20:25:14 +0000214 }
Chris Lattner36942d72009-02-28 20:35:42 +0000215 if (FTy->isVarArg()) {
Chris Lattner30794262009-02-28 21:27:31 +0000216 if (FTy->getNumParams()) OS << ", ";
217 OS << "...";
Chris Lattner9cc34462009-02-28 20:25:14 +0000218 }
Chris Lattner30794262009-02-28 21:27:31 +0000219 OS << ')';
Chris Lattner36942d72009-02-28 20:35:42 +0000220 break;
221 }
222 case Type::StructTyID: {
223 const StructType *STy = cast<StructType>(Ty);
224 if (STy->isPacked())
Chris Lattner30794262009-02-28 21:27:31 +0000225 OS << '<';
226 OS << "{ ";
Chris Lattner36942d72009-02-28 20:35:42 +0000227 for (StructType::element_iterator I = STy->element_begin(),
228 E = STy->element_end(); I != E; ++I) {
Chris Lattner30794262009-02-28 21:27:31 +0000229 CalcTypeName(*I, TypeStack, OS);
Chris Lattner36942d72009-02-28 20:35:42 +0000230 if (next(I) != STy->element_end())
Chris Lattner30794262009-02-28 21:27:31 +0000231 OS << ',';
232 OS << ' ';
Chris Lattner9cc34462009-02-28 20:25:14 +0000233 }
Chris Lattner30794262009-02-28 21:27:31 +0000234 OS << '}';
Chris Lattner36942d72009-02-28 20:35:42 +0000235 if (STy->isPacked())
Chris Lattner30794262009-02-28 21:27:31 +0000236 OS << '>';
Chris Lattner36942d72009-02-28 20:35:42 +0000237 break;
238 }
239 case Type::PointerTyID: {
240 const PointerType *PTy = cast<PointerType>(Ty);
Chris Lattner30794262009-02-28 21:27:31 +0000241 CalcTypeName(PTy->getElementType(), TypeStack, OS);
Chris Lattner36942d72009-02-28 20:35:42 +0000242 if (unsigned AddressSpace = PTy->getAddressSpace())
Chris Lattner30794262009-02-28 21:27:31 +0000243 OS << " addrspace(" << AddressSpace << ')';
244 OS << '*';
Chris Lattner36942d72009-02-28 20:35:42 +0000245 break;
246 }
247 case Type::ArrayTyID: {
248 const ArrayType *ATy = cast<ArrayType>(Ty);
Chris Lattner30794262009-02-28 21:27:31 +0000249 OS << '[' << ATy->getNumElements() << " x ";
250 CalcTypeName(ATy->getElementType(), TypeStack, OS);
251 OS << ']';
Chris Lattner36942d72009-02-28 20:35:42 +0000252 break;
253 }
254 case Type::VectorTyID: {
255 const VectorType *PTy = cast<VectorType>(Ty);
Chris Lattner30794262009-02-28 21:27:31 +0000256 OS << "<" << PTy->getNumElements() << " x ";
257 CalcTypeName(PTy->getElementType(), TypeStack, OS);
258 OS << '>';
Chris Lattner36942d72009-02-28 20:35:42 +0000259 break;
260 }
261 case Type::OpaqueTyID:
Chris Lattner30794262009-02-28 21:27:31 +0000262 OS << "opaque";
Chris Lattner36942d72009-02-28 20:35:42 +0000263 break;
264 default:
Chris Lattner30794262009-02-28 21:27:31 +0000265 OS << "<unrecognized-type>";
Chris Lattner36942d72009-02-28 20:35:42 +0000266 break;
Chris Lattner9cc34462009-02-28 20:25:14 +0000267 }
268
Chris Lattner534361e2009-02-28 20:49:40 +0000269 TypeStack.pop_back(); // Remove self from stack.
Chris Lattner9cc34462009-02-28 20:25:14 +0000270}
271
272/// printTypeInt - The internal guts of printing out a type that has a
273/// potentially named portion.
274///
Chris Lattnerd2b6cb02009-03-01 01:16:21 +0000275void TypePrinting::print(const Type *Ty, raw_ostream &OS,
276 bool IgnoreTopLevelName) {
Chris Lattner9cc34462009-02-28 20:25:14 +0000277 // Check to see if the type is named.
Chris Lattner87185e82009-02-28 23:03:55 +0000278 DenseMap<const Type*, std::string> &TM = getTypeNamesMap(TypeNames);
Chris Lattnerd2b6cb02009-03-01 01:16:21 +0000279 if (!IgnoreTopLevelName) {
280 DenseMap<const Type*, std::string>::iterator I = TM.find(Ty);
281 if (I != TM.end()) {
282 OS << I->second;
283 return;
284 }
Chris Lattner9cc34462009-02-28 20:25:14 +0000285 }
286
287 // Otherwise we have a type that has not been named but is a derived type.
288 // Carefully recurse the type hierarchy to print out any contained symbolic
289 // names.
Chris Lattnerb840cac2009-02-28 20:34:19 +0000290 SmallVector<const Type *, 16> TypeStack;
Chris Lattner9cc34462009-02-28 20:25:14 +0000291 std::string TypeName;
Chris Lattner534361e2009-02-28 20:49:40 +0000292
293 raw_string_ostream TypeOS(TypeName);
Chris Lattnerd2b6cb02009-03-01 01:16:21 +0000294 CalcTypeName(Ty, TypeStack, TypeOS, IgnoreTopLevelName);
Chris Lattner534361e2009-02-28 20:49:40 +0000295 OS << TypeOS.str();
296
297 // Cache type name for later use.
Chris Lattnerd2b6cb02009-03-01 01:16:21 +0000298 if (!IgnoreTopLevelName)
299 TM.insert(std::make_pair(Ty, TypeOS.str()));
Chris Lattner9cc34462009-02-28 20:25:14 +0000300}
301
Chris Lattner413fd232009-03-01 00:03:38 +0000302namespace {
303 class TypeFinder {
304 // To avoid walking constant expressions multiple times and other IR
305 // objects, we keep several helper maps.
306 DenseSet<const Value*> VisitedConstants;
307 DenseSet<const Type*> VisitedTypes;
308
309 TypePrinting &TP;
310 std::vector<const Type*> &NumberedTypes;
311 public:
312 TypeFinder(TypePrinting &tp, std::vector<const Type*> &numberedTypes)
313 : TP(tp), NumberedTypes(numberedTypes) {}
314
315 void Run(const Module &M) {
Chris Lattner88485862009-03-01 00:32:33 +0000316 // Get types from the type symbol table. This gets opaque types referened
317 // only through derived named types.
318 const TypeSymbolTable &ST = M.getTypeSymbolTable();
319 for (TypeSymbolTable::const_iterator TI = ST.begin(), E = ST.end();
320 TI != E; ++TI)
321 IncorporateType(TI->second);
322
Chris Lattner413fd232009-03-01 00:03:38 +0000323 // Get types from global variables.
324 for (Module::const_global_iterator I = M.global_begin(),
325 E = M.global_end(); I != E; ++I) {
326 IncorporateType(I->getType());
327 if (I->hasInitializer())
328 IncorporateValue(I->getInitializer());
329 }
330
331 // Get types from aliases.
332 for (Module::const_alias_iterator I = M.alias_begin(),
333 E = M.alias_end(); I != E; ++I) {
334 IncorporateType(I->getType());
335 IncorporateValue(I->getAliasee());
336 }
337
338 // Get types from functions.
339 for (Module::const_iterator FI = M.begin(), E = M.end(); FI != E; ++FI) {
340 IncorporateType(FI->getType());
341
342 for (Function::const_iterator BB = FI->begin(), E = FI->end();
343 BB != E;++BB)
344 for (BasicBlock::const_iterator II = BB->begin(),
345 E = BB->end(); II != E; ++II) {
346 const Instruction &I = *II;
347 // Incorporate the type of the instruction and all its operands.
348 IncorporateType(I.getType());
349 for (User::const_op_iterator OI = I.op_begin(), OE = I.op_end();
350 OI != OE; ++OI)
351 IncorporateValue(*OI);
352 }
353 }
354 }
355
356 private:
357 void IncorporateType(const Type *Ty) {
358 // Check to see if we're already visited this type.
Chris Lattner88485862009-03-01 00:32:33 +0000359 if (!VisitedTypes.insert(Ty).second)
Chris Lattner413fd232009-03-01 00:03:38 +0000360 return;
361
362 // If this is a structure or opaque type, add a name for the type.
Nick Lewycky21cc4462009-04-04 07:22:01 +0000363 if (((isa<StructType>(Ty) && cast<StructType>(Ty)->getNumElements())
364 || isa<OpaqueType>(Ty)) && !TP.hasTypeName(Ty)) {
Chris Lattner413fd232009-03-01 00:03:38 +0000365 TP.addTypeName(Ty, "%"+utostr(unsigned(NumberedTypes.size())));
366 NumberedTypes.push_back(Ty);
367 }
368
369 // Recursively walk all contained types.
370 for (Type::subtype_iterator I = Ty->subtype_begin(),
371 E = Ty->subtype_end(); I != E; ++I)
372 IncorporateType(*I);
373 }
374
375 /// IncorporateValue - This method is used to walk operand lists finding
376 /// types hiding in constant expressions and other operands that won't be
377 /// walked in other ways. GlobalValues, basic blocks, instructions, and
378 /// inst operands are all explicitly enumerated.
379 void IncorporateValue(const Value *V) {
380 if (V == 0 || !isa<Constant>(V) || isa<GlobalValue>(V)) return;
381
382 // Already visited?
383 if (!VisitedConstants.insert(V).second)
384 return;
385
386 // Check this type.
387 IncorporateType(V->getType());
388
389 // Look in operands for types.
390 const Constant *C = cast<Constant>(V);
391 for (Constant::const_op_iterator I = C->op_begin(),
392 E = C->op_end(); I != E;++I)
393 IncorporateValue(*I);
394 }
395 };
396} // end anonymous namespace
397
398
399/// AddModuleTypesToPrinter - Add all of the symbolic type names for types in
400/// the specified module to the TypePrinter and all numbered types to it and the
401/// NumberedTypes table.
402static void AddModuleTypesToPrinter(TypePrinting &TP,
403 std::vector<const Type*> &NumberedTypes,
404 const Module *M) {
Chris Lattnere9fa33e2009-02-28 23:20:19 +0000405 if (M == 0) return;
406
407 // If the module has a symbol table, take all global types and stuff their
408 // names into the TypeNames map.
409 const TypeSymbolTable &ST = M->getTypeSymbolTable();
410 for (TypeSymbolTable::const_iterator TI = ST.begin(), E = ST.end();
411 TI != E; ++TI) {
412 const Type *Ty = cast<Type>(TI->second);
413
414 // As a heuristic, don't insert pointer to primitive types, because
415 // they are used too often to have a single useful name.
416 if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
417 const Type *PETy = PTy->getElementType();
418 if ((PETy->isPrimitiveType() || PETy->isInteger()) &&
419 !isa<OpaqueType>(PETy))
420 continue;
421 }
422
423 // Likewise don't insert primitives either.
424 if (Ty->isInteger() || Ty->isPrimitiveType())
425 continue;
426
427 // Get the name as a string and insert it into TypeNames.
428 std::string NameStr;
Dan Gohman683e9222009-08-12 17:23:50 +0000429 raw_string_ostream NameROS(NameStr);
430 formatted_raw_ostream NameOS(NameROS);
Daniel Dunbar03d76512009-07-25 23:55:21 +0000431 PrintLLVMName(NameOS, TI->first, LocalPrefix);
Dan Gohman683e9222009-08-12 17:23:50 +0000432 NameOS.flush();
433 TP.addTypeName(Ty, NameStr);
Chris Lattnere9fa33e2009-02-28 23:20:19 +0000434 }
Chris Lattner413fd232009-03-01 00:03:38 +0000435
436 // Walk the entire module to find references to unnamed structure and opaque
437 // types. This is required for correctness by opaque types (because multiple
438 // uses of an unnamed opaque type needs to be referred to by the same ID) and
439 // it shrinks complex recursive structure types substantially in some cases.
440 TypeFinder(TP, NumberedTypes).Run(*M);
Chris Lattnere9fa33e2009-02-28 23:20:19 +0000441}
442
Chris Lattner9cc34462009-02-28 20:25:14 +0000443
Chris Lattner9cc34462009-02-28 20:25:14 +0000444/// WriteTypeSymbolic - This attempts to write the specified type as a symbolic
445/// type, iff there is an entry in the modules symbol table for the specified
Chris Lattnerc2871372009-02-28 21:05:51 +0000446/// type or one of it's component types.
Chris Lattner9cc34462009-02-28 20:25:14 +0000447///
Chris Lattnere9fa33e2009-02-28 23:20:19 +0000448void llvm::WriteTypeSymbolic(raw_ostream &OS, const Type *Ty, const Module *M) {
449 TypePrinting Printer;
Chris Lattner413fd232009-03-01 00:03:38 +0000450 std::vector<const Type*> NumberedTypes;
451 AddModuleTypesToPrinter(Printer, NumberedTypes, M);
Chris Lattnere9fa33e2009-02-28 23:20:19 +0000452 Printer.print(Ty, OS);
Chris Lattner9cc34462009-02-28 20:25:14 +0000453}
454
Chris Lattner6ab910b2008-08-19 04:36:02 +0000455//===----------------------------------------------------------------------===//
456// SlotTracker Class: Enumerate slot numbers for unnamed values
457//===----------------------------------------------------------------------===//
458
Chris Lattnerb64871a2008-08-19 04:28:07 +0000459namespace {
460
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000461/// This class provides computation of slot numbers for LLVM Assembly writing.
Chris Lattner45d4c732008-08-17 04:17:45 +0000462///
Chris Lattner0d9574a2008-08-19 04:26:57 +0000463class SlotTracker {
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000464public:
Devang Patel320671d2009-07-08 21:44:25 +0000465 /// ValueMap - A mapping of Values to slot numbers.
Chris Lattner92255072008-08-17 17:25:25 +0000466 typedef DenseMap<const Value*, unsigned> ValueMap;
Chris Lattner45d4c732008-08-17 04:17:45 +0000467
468private:
Devang Patel320671d2009-07-08 21:44:25 +0000469 /// TheModule - The module for which we are holding slot numbers.
Chris Lattner45d4c732008-08-17 04:17:45 +0000470 const Module* TheModule;
471
Devang Patel320671d2009-07-08 21:44:25 +0000472 /// TheFunction - The function for which we are holding slot numbers.
Chris Lattner45d4c732008-08-17 04:17:45 +0000473 const Function* TheFunction;
474 bool FunctionProcessed;
475
Devang Patel320671d2009-07-08 21:44:25 +0000476 /// TheMDNode - The MDNode for which we are holding slot numbers.
477 const MDNode *TheMDNode;
478
Devang Patelc29d5b32009-07-30 00:02:57 +0000479 /// TheNamedMDNode - The MDNode for which we are holding slot numbers.
480 const NamedMDNode *TheNamedMDNode;
481
Devang Patel320671d2009-07-08 21:44:25 +0000482 /// mMap - The TypePlanes map for the module level data.
Chris Lattner45d4c732008-08-17 04:17:45 +0000483 ValueMap mMap;
484 unsigned mNext;
485
Devang Patel320671d2009-07-08 21:44:25 +0000486 /// fMap - The TypePlanes map for the function level data.
Chris Lattner45d4c732008-08-17 04:17:45 +0000487 ValueMap fMap;
488 unsigned fNext;
489
Devang Patel320671d2009-07-08 21:44:25 +0000490 /// mdnMap - Map for MDNodes.
491 ValueMap mdnMap;
492 unsigned mdnNext;
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000493public:
Chris Lattner45d4c732008-08-17 04:17:45 +0000494 /// Construct from a module
Chris Lattner0d9574a2008-08-19 04:26:57 +0000495 explicit SlotTracker(const Module *M);
Chris Lattner45d4c732008-08-17 04:17:45 +0000496 /// Construct from a function, starting out in incorp state.
Chris Lattner0d9574a2008-08-19 04:26:57 +0000497 explicit SlotTracker(const Function *F);
Devang Patel320671d2009-07-08 21:44:25 +0000498 /// Construct from a mdnode.
499 explicit SlotTracker(const MDNode *N);
Devang Patelc29d5b32009-07-30 00:02:57 +0000500 /// Construct from a named mdnode.
501 explicit SlotTracker(const NamedMDNode *N);
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000502
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000503 /// Return the slot number of the specified value in it's type
Chris Lattner0d9574a2008-08-19 04:26:57 +0000504 /// plane. If something is not in the SlotTracker, return -1.
Chris Lattner22379bc2007-01-11 03:54:27 +0000505 int getLocalSlot(const Value *V);
506 int getGlobalSlot(const GlobalValue *V);
Devang Patel320671d2009-07-08 21:44:25 +0000507 int getMetadataSlot(const MDNode *N);
Reid Spencerfc621e22004-06-09 15:26:53 +0000508
Misha Brukmanfd939082005-04-21 23:48:37 +0000509 /// If you'd like to deal with a function instead of just a module, use
Chris Lattner0d9574a2008-08-19 04:26:57 +0000510 /// this method to get its data into the SlotTracker.
Misha Brukmanfd939082005-04-21 23:48:37 +0000511 void incorporateFunction(const Function *F) {
512 TheFunction = F;
Reid Spencer28531c72004-08-16 07:46:33 +0000513 FunctionProcessed = false;
514 }
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000515
Misha Brukmanfd939082005-04-21 23:48:37 +0000516 /// After calling incorporateFunction, use this method to remove the
Chris Lattner0d9574a2008-08-19 04:26:57 +0000517 /// most recently incorporated function from the SlotTracker. This
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000518 /// will reset the state of the machine back to just the module contents.
519 void purgeFunction();
520
Devang Patel320671d2009-07-08 21:44:25 +0000521 /// MDNode map iterators.
522 ValueMap::iterator mdnBegin() { return mdnMap.begin(); }
523 ValueMap::iterator mdnEnd() { return mdnMap.end(); }
Dan Gohman9bf0b9b2009-08-12 23:54:22 +0000524 unsigned mdnSize() const { return mdnMap.size(); }
525 bool mdnEmpty() const { return mdnMap.empty(); }
Devang Patel320671d2009-07-08 21:44:25 +0000526
Reid Spencerb03de0c2004-05-26 21:56:09 +0000527 /// This function does the actual initialization.
528 inline void initialize();
529
Devang Patel320671d2009-07-08 21:44:25 +0000530 // Implementation Details
531private:
Chris Lattner9446bbe2007-01-09 07:55:49 +0000532 /// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
533 void CreateModuleSlot(const GlobalValue *V);
Devang Patel320671d2009-07-08 21:44:25 +0000534
535 /// CreateMetadataSlot - Insert the specified MDNode* into the slot table.
536 void CreateMetadataSlot(const MDNode *N);
537
Chris Lattner9446bbe2007-01-09 07:55:49 +0000538 /// CreateFunctionSlot - Insert the specified Value* into the slot table.
539 void CreateFunctionSlot(const Value *V);
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000540
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000541 /// Add all of the module level global variables (and their initializers)
542 /// and function declarations, but not the contents of those functions.
543 void processModule();
544
Devang Patel320671d2009-07-08 21:44:25 +0000545 /// Add all of the functions arguments, basic blocks, and instructions.
Reid Spencerb03de0c2004-05-26 21:56:09 +0000546 void processFunction();
547
Devang Patel320671d2009-07-08 21:44:25 +0000548 /// Add all MDNode operands.
549 void processMDNode();
550
Devang Patelc29d5b32009-07-30 00:02:57 +0000551 /// Add all MDNode operands.
552 void processNamedMDNode();
553
Chris Lattner0d9574a2008-08-19 04:26:57 +0000554 SlotTracker(const SlotTracker &); // DO NOT IMPLEMENT
555 void operator=(const SlotTracker &); // DO NOT IMPLEMENT
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000556};
557
Chris Lattnerb64871a2008-08-19 04:28:07 +0000558} // end anonymous namespace
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000559
Chris Lattner6ab910b2008-08-19 04:36:02 +0000560
561static SlotTracker *createSlotTracker(const Value *V) {
562 if (const Argument *FA = dyn_cast<Argument>(V))
563 return new SlotTracker(FA->getParent());
564
565 if (const Instruction *I = dyn_cast<Instruction>(V))
566 return new SlotTracker(I->getParent()->getParent());
567
568 if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
569 return new SlotTracker(BB->getParent());
570
571 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
572 return new SlotTracker(GV->getParent());
573
574 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V))
575 return new SlotTracker(GA->getParent());
576
577 if (const Function *Func = dyn_cast<Function>(V))
578 return new SlotTracker(Func);
579
580 return 0;
581}
582
583#if 0
Dan Gohmanfd87a542009-07-25 01:43:01 +0000584#define ST_DEBUG(X) errs() << X
Chris Lattner6ab910b2008-08-19 04:36:02 +0000585#else
Chris Lattner24233032008-08-19 04:47:09 +0000586#define ST_DEBUG(X)
Chris Lattner6ab910b2008-08-19 04:36:02 +0000587#endif
588
589// Module level constructor. Causes the contents of the Module (sans functions)
590// to be added to the slot table.
591SlotTracker::SlotTracker(const Module *M)
Devang Patel320671d2009-07-08 21:44:25 +0000592 : TheModule(M), TheFunction(0), FunctionProcessed(false), TheMDNode(0),
Devang Patelc29d5b32009-07-30 00:02:57 +0000593 TheNamedMDNode(0), mNext(0), fNext(0), mdnNext(0) {
Chris Lattner6ab910b2008-08-19 04:36:02 +0000594}
595
596// Function level constructor. Causes the contents of the Module and the one
597// function provided to be added to the slot table.
598SlotTracker::SlotTracker(const Function *F)
Chris Lattnercfb5a202008-08-19 05:06:27 +0000599 : TheModule(F ? F->getParent() : 0), TheFunction(F), FunctionProcessed(false),
Devang Patelc29d5b32009-07-30 00:02:57 +0000600 TheMDNode(0), TheNamedMDNode(0), mNext(0), fNext(0), mdnNext(0) {
Devang Patel320671d2009-07-08 21:44:25 +0000601}
602
603// Constructor to handle single MDNode.
604SlotTracker::SlotTracker(const MDNode *C)
605 : TheModule(0), TheFunction(0), FunctionProcessed(false), TheMDNode(C),
Devang Patelc29d5b32009-07-30 00:02:57 +0000606 TheNamedMDNode(0), mNext(0), fNext(0), mdnNext(0) {
607}
608
609// Constructor to handle single NamedMDNode.
610SlotTracker::SlotTracker(const NamedMDNode *N)
611 : TheModule(0), TheFunction(0), FunctionProcessed(false), TheMDNode(0),
612 TheNamedMDNode(N), mNext(0), fNext(0), mdnNext(0) {
Chris Lattner6ab910b2008-08-19 04:36:02 +0000613}
614
615inline void SlotTracker::initialize() {
616 if (TheModule) {
617 processModule();
618 TheModule = 0; ///< Prevent re-processing next time we're called.
619 }
Chris Lattnercfb5a202008-08-19 05:06:27 +0000620
Chris Lattner6ab910b2008-08-19 04:36:02 +0000621 if (TheFunction && !FunctionProcessed)
622 processFunction();
Devang Patel320671d2009-07-08 21:44:25 +0000623
624 if (TheMDNode)
625 processMDNode();
Devang Patelc29d5b32009-07-30 00:02:57 +0000626
627 if (TheNamedMDNode)
628 processNamedMDNode();
Chris Lattner6ab910b2008-08-19 04:36:02 +0000629}
630
631// Iterate through all the global variables, functions, and global
632// variable initializers and create slots for them.
633void SlotTracker::processModule() {
Chris Lattner24233032008-08-19 04:47:09 +0000634 ST_DEBUG("begin processModule!\n");
Chris Lattner6ab910b2008-08-19 04:36:02 +0000635
636 // Add all of the unnamed global variables to the value table.
637 for (Module::const_global_iterator I = TheModule->global_begin(),
Devang Patel320671d2009-07-08 21:44:25 +0000638 E = TheModule->global_end(); I != E; ++I) {
Chris Lattner6ab910b2008-08-19 04:36:02 +0000639 if (!I->hasName())
640 CreateModuleSlot(I);
Devang Patel320671d2009-07-08 21:44:25 +0000641 if (I->hasInitializer()) {
642 if (MDNode *N = dyn_cast<MDNode>(I->getInitializer()))
643 CreateMetadataSlot(N);
644 }
645 }
Chris Lattner6ab910b2008-08-19 04:36:02 +0000646
Devang Patel37c4a2d2009-07-29 22:04:47 +0000647 // Add metadata used by named metadata.
648 for (Module::const_named_metadata_iterator
649 I = TheModule->named_metadata_begin(),
650 E = TheModule->named_metadata_end(); I != E; ++I) {
651 const NamedMDNode *NMD = I;
652 for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i) {
653 MDNode *MD = dyn_cast_or_null<MDNode>(NMD->getElement(i));
Devang Patele8861b82009-07-30 01:02:04 +0000654 if (MD)
655 CreateMetadataSlot(MD);
Devang Patel37c4a2d2009-07-29 22:04:47 +0000656 }
657 }
658
Chris Lattner6ab910b2008-08-19 04:36:02 +0000659 // Add all the unnamed functions to the table.
660 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
661 I != E; ++I)
662 if (!I->hasName())
663 CreateModuleSlot(I);
664
Chris Lattner24233032008-08-19 04:47:09 +0000665 ST_DEBUG("end processModule!\n");
Chris Lattner6ab910b2008-08-19 04:36:02 +0000666}
667
Chris Lattner6ab910b2008-08-19 04:36:02 +0000668// Process the arguments, basic blocks, and instructions of a function.
669void SlotTracker::processFunction() {
Chris Lattner24233032008-08-19 04:47:09 +0000670 ST_DEBUG("begin processFunction!\n");
Chris Lattner6ab910b2008-08-19 04:36:02 +0000671 fNext = 0;
672
673 // Add all the function arguments with no names.
674 for(Function::const_arg_iterator AI = TheFunction->arg_begin(),
675 AE = TheFunction->arg_end(); AI != AE; ++AI)
676 if (!AI->hasName())
677 CreateFunctionSlot(AI);
678
Chris Lattner24233032008-08-19 04:47:09 +0000679 ST_DEBUG("Inserting Instructions:\n");
Chris Lattner6ab910b2008-08-19 04:36:02 +0000680
681 // Add all of the basic blocks and instructions with no names.
682 for (Function::const_iterator BB = TheFunction->begin(),
683 E = TheFunction->end(); BB != E; ++BB) {
684 if (!BB->hasName())
685 CreateFunctionSlot(BB);
Devang Patel320671d2009-07-08 21:44:25 +0000686 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E;
687 ++I) {
Chris Lattner6ab910b2008-08-19 04:36:02 +0000688 if (I->getType() != Type::VoidTy && !I->hasName())
689 CreateFunctionSlot(I);
Devang Patel320671d2009-07-08 21:44:25 +0000690 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
691 if (MDNode *N = dyn_cast<MDNode>(I->getOperand(i)))
692 CreateMetadataSlot(N);
693 }
Chris Lattner6ab910b2008-08-19 04:36:02 +0000694 }
695
696 FunctionProcessed = true;
697
Chris Lattner24233032008-08-19 04:47:09 +0000698 ST_DEBUG("end processFunction!\n");
Chris Lattner6ab910b2008-08-19 04:36:02 +0000699}
700
Devang Patel320671d2009-07-08 21:44:25 +0000701/// processMDNode - Process TheMDNode.
702void SlotTracker::processMDNode() {
703 ST_DEBUG("begin processMDNode!\n");
704 mdnNext = 0;
705 CreateMetadataSlot(TheMDNode);
706 TheMDNode = 0;
707 ST_DEBUG("end processMDNode!\n");
708}
709
Devang Patelc29d5b32009-07-30 00:02:57 +0000710/// processNamedMDNode - Process TheNamedMDNode.
711void SlotTracker::processNamedMDNode() {
712 ST_DEBUG("begin processNamedMDNode!\n");
713 mdnNext = 0;
714 for (unsigned i = 0, e = TheNamedMDNode->getNumElements(); i != e; ++i) {
715 MDNode *MD = dyn_cast_or_null<MDNode>(TheNamedMDNode->getElement(i));
716 if (MD)
717 CreateMetadataSlot(MD);
718 }
719 TheNamedMDNode = 0;
720 ST_DEBUG("end processNamedMDNode!\n");
721}
722
Chris Lattner6ab910b2008-08-19 04:36:02 +0000723/// Clean up after incorporating a function. This is the only way to get out of
724/// the function incorporation state that affects get*Slot/Create*Slot. Function
725/// incorporation state is indicated by TheFunction != 0.
726void SlotTracker::purgeFunction() {
Chris Lattner24233032008-08-19 04:47:09 +0000727 ST_DEBUG("begin purgeFunction!\n");
Chris Lattner6ab910b2008-08-19 04:36:02 +0000728 fMap.clear(); // Simply discard the function level map
729 TheFunction = 0;
730 FunctionProcessed = false;
Chris Lattner24233032008-08-19 04:47:09 +0000731 ST_DEBUG("end purgeFunction!\n");
Chris Lattner6ab910b2008-08-19 04:36:02 +0000732}
733
734/// getGlobalSlot - Get the slot number of a global value.
735int SlotTracker::getGlobalSlot(const GlobalValue *V) {
736 // Check for uninitialized state and do lazy initialization.
737 initialize();
738
739 // Find the type plane in the module map
740 ValueMap::iterator MI = mMap.find(V);
Dan Gohmanaeaf2452008-10-01 19:58:59 +0000741 return MI == mMap.end() ? -1 : (int)MI->second;
Chris Lattner6ab910b2008-08-19 04:36:02 +0000742}
743
Devang Patel320671d2009-07-08 21:44:25 +0000744/// getGlobalSlot - Get the slot number of a MDNode.
745int SlotTracker::getMetadataSlot(const MDNode *N) {
746 // Check for uninitialized state and do lazy initialization.
747 initialize();
748
749 // Find the type plane in the module map
750 ValueMap::iterator MI = mdnMap.find(N);
751 return MI == mdnMap.end() ? -1 : (int)MI->second;
752}
753
Chris Lattner6ab910b2008-08-19 04:36:02 +0000754
755/// getLocalSlot - Get the slot number for a value that is local to a function.
756int SlotTracker::getLocalSlot(const Value *V) {
757 assert(!isa<Constant>(V) && "Can't get a constant or global slot with this!");
758
759 // Check for uninitialized state and do lazy initialization.
760 initialize();
761
762 ValueMap::iterator FI = fMap.find(V);
Dan Gohmanaeaf2452008-10-01 19:58:59 +0000763 return FI == fMap.end() ? -1 : (int)FI->second;
Chris Lattner6ab910b2008-08-19 04:36:02 +0000764}
765
766
767/// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
768void SlotTracker::CreateModuleSlot(const GlobalValue *V) {
769 assert(V && "Can't insert a null Value into SlotTracker!");
770 assert(V->getType() != Type::VoidTy && "Doesn't need a slot!");
771 assert(!V->hasName() && "Doesn't need a slot!");
772
773 unsigned DestSlot = mNext++;
774 mMap[V] = DestSlot;
775
Chris Lattner24233032008-08-19 04:47:09 +0000776 ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" <<
Chris Lattner6ab910b2008-08-19 04:36:02 +0000777 DestSlot << " [");
778 // G = Global, F = Function, A = Alias, o = other
Chris Lattner24233032008-08-19 04:47:09 +0000779 ST_DEBUG((isa<GlobalVariable>(V) ? 'G' :
Chris Lattner6ab910b2008-08-19 04:36:02 +0000780 (isa<Function>(V) ? 'F' :
781 (isa<GlobalAlias>(V) ? 'A' : 'o'))) << "]\n");
782}
783
Chris Lattner6ab910b2008-08-19 04:36:02 +0000784/// CreateSlot - Create a new slot for the specified value if it has no name.
785void SlotTracker::CreateFunctionSlot(const Value *V) {
786 assert(V->getType() != Type::VoidTy && !V->hasName() &&
787 "Doesn't need a slot!");
788
789 unsigned DestSlot = fNext++;
790 fMap[V] = DestSlot;
791
792 // G = Global, F = Function, o = other
Chris Lattner24233032008-08-19 04:47:09 +0000793 ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" <<
Chris Lattner6ab910b2008-08-19 04:36:02 +0000794 DestSlot << " [o]\n");
795}
796
Devang Patel320671d2009-07-08 21:44:25 +0000797/// CreateModuleSlot - Insert the specified MDNode* into the slot table.
798void SlotTracker::CreateMetadataSlot(const MDNode *N) {
799 assert(N && "Can't insert a null Value into SlotTracker!");
800
801 ValueMap::iterator I = mdnMap.find(N);
802 if (I != mdnMap.end())
803 return;
Chris Lattner6ab910b2008-08-19 04:36:02 +0000804
Devang Patel320671d2009-07-08 21:44:25 +0000805 unsigned DestSlot = mdnNext++;
806 mdnMap[N] = DestSlot;
807
808 for (MDNode::const_elem_iterator MDI = N->elem_begin(),
809 MDE = N->elem_end(); MDI != MDE; ++MDI) {
810 const Value *TV = *MDI;
811 if (TV)
812 if (const MDNode *N2 = dyn_cast<MDNode>(TV))
813 CreateMetadataSlot(N2);
814 }
815}
Chris Lattner6ab910b2008-08-19 04:36:02 +0000816
817//===----------------------------------------------------------------------===//
818// AsmWriter Implementation
819//===----------------------------------------------------------------------===//
Chris Lattnerf082b802002-07-23 18:07:49 +0000820
Dan Gohman1220e102009-08-12 20:56:03 +0000821static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
Dan Gohmand6c0f652009-08-13 15:27:57 +0000822 TypePrinting *TypePrinter,
Chris Lattner0d9574a2008-08-19 04:26:57 +0000823 SlotTracker *Machine);
Reid Spencer0e25e1c2004-07-04 11:50:43 +0000824
Chris Lattnerc97536e2008-08-17 04:40:13 +0000825
Chris Lattner207b5bc2001-10-29 16:37:48 +0000826
Chris Lattner82c4bc72006-12-06 06:40:49 +0000827static const char *getPredicateText(unsigned predicate) {
Reid Spencer81dfeb32006-12-04 05:19:18 +0000828 const char * pred = "unknown";
829 switch (predicate) {
830 case FCmpInst::FCMP_FALSE: pred = "false"; break;
831 case FCmpInst::FCMP_OEQ: pred = "oeq"; break;
832 case FCmpInst::FCMP_OGT: pred = "ogt"; break;
833 case FCmpInst::FCMP_OGE: pred = "oge"; break;
834 case FCmpInst::FCMP_OLT: pred = "olt"; break;
835 case FCmpInst::FCMP_OLE: pred = "ole"; break;
836 case FCmpInst::FCMP_ONE: pred = "one"; break;
837 case FCmpInst::FCMP_ORD: pred = "ord"; break;
838 case FCmpInst::FCMP_UNO: pred = "uno"; break;
839 case FCmpInst::FCMP_UEQ: pred = "ueq"; break;
840 case FCmpInst::FCMP_UGT: pred = "ugt"; break;
841 case FCmpInst::FCMP_UGE: pred = "uge"; break;
842 case FCmpInst::FCMP_ULT: pred = "ult"; break;
843 case FCmpInst::FCMP_ULE: pred = "ule"; break;
844 case FCmpInst::FCMP_UNE: pred = "une"; break;
845 case FCmpInst::FCMP_TRUE: pred = "true"; break;
846 case ICmpInst::ICMP_EQ: pred = "eq"; break;
847 case ICmpInst::ICMP_NE: pred = "ne"; break;
848 case ICmpInst::ICMP_SGT: pred = "sgt"; break;
849 case ICmpInst::ICMP_SGE: pred = "sge"; break;
850 case ICmpInst::ICMP_SLT: pred = "slt"; break;
851 case ICmpInst::ICMP_SLE: pred = "sle"; break;
852 case ICmpInst::ICMP_UGT: pred = "ugt"; break;
853 case ICmpInst::ICMP_UGE: pred = "uge"; break;
854 case ICmpInst::ICMP_ULT: pred = "ult"; break;
855 case ICmpInst::ICMP_ULE: pred = "ule"; break;
856 }
857 return pred;
858}
859
Dan Gohman683e9222009-08-12 17:23:50 +0000860static void WriteMDNodes(formatted_raw_ostream &Out, TypePrinting &TypePrinter,
Devang Patel320671d2009-07-08 21:44:25 +0000861 SlotTracker &Machine) {
862 SmallVector<const MDNode *, 16> Nodes;
863 Nodes.resize(Machine.mdnSize());
864 for (SlotTracker::ValueMap::iterator I =
865 Machine.mdnBegin(), E = Machine.mdnEnd(); I != E; ++I)
866 Nodes[I->second] = cast<MDNode>(I->first);
867
868 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Devang Patel2214c942009-07-08 21:57:07 +0000869 Out << '!' << i << " = metadata ";
Devang Patel320671d2009-07-08 21:44:25 +0000870 const MDNode *Node = Nodes[i];
871 Out << "!{";
872 for (MDNode::const_elem_iterator NI = Node->elem_begin(),
873 NE = Node->elem_end(); NI != NE;) {
874 const Value *V = *NI;
875 if (!V)
876 Out << "null";
877 else if (const MDNode *N = dyn_cast<MDNode>(V)) {
878 Out << "metadata ";
879 Out << '!' << Machine.getMetadataSlot(N);
880 }
881 else {
882 TypePrinter.print((*NI)->getType(), Out);
883 Out << ' ';
Dan Gohmand6c0f652009-08-13 15:27:57 +0000884 WriteAsOperandInternal(Out, *NI, &TypePrinter, &Machine);
Devang Patel320671d2009-07-08 21:44:25 +0000885 }
886 if (++NI != NE)
887 Out << ", ";
888 }
889 Out << "}\n";
890 }
891}
892
Dan Gohman1220e102009-08-12 20:56:03 +0000893static void WriteOptimizationInfo(raw_ostream &Out, const User *U) {
Dan Gohman1224c382009-07-20 21:19:07 +0000894 if (const OverflowingBinaryOperator *OBO =
895 dyn_cast<OverflowingBinaryOperator>(U)) {
896 if (OBO->hasNoUnsignedOverflow())
Dan Gohman59858cf2009-07-27 16:11:46 +0000897 Out << " nuw";
Dan Gohman1224c382009-07-20 21:19:07 +0000898 if (OBO->hasNoSignedOverflow())
Dan Gohman59858cf2009-07-27 16:11:46 +0000899 Out << " nsw";
Dan Gohman1224c382009-07-20 21:19:07 +0000900 } else if (const SDivOperator *Div = dyn_cast<SDivOperator>(U)) {
901 if (Div->isExact())
Dan Gohman59858cf2009-07-27 16:11:46 +0000902 Out << " exact";
Dan Gohmandd8004d2009-07-27 21:53:46 +0000903 } else if (const GEPOperator *GEP = dyn_cast<GEPOperator>(U)) {
904 if (GEP->isInBounds())
905 Out << " inbounds";
Dan Gohman1224c382009-07-20 21:19:07 +0000906 }
907}
908
Dan Gohman1220e102009-08-12 20:56:03 +0000909static void WriteConstantInt(raw_ostream &Out, const Constant *CV,
Chris Lattner9cc34462009-02-28 20:25:14 +0000910 TypePrinting &TypePrinter, SlotTracker *Machine) {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +0000911 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Chris Lattnerfad86b02008-08-17 07:19:36 +0000912 if (CI->getType() == Type::Int1Ty) {
Reid Spencer579dca12007-01-12 04:24:46 +0000913 Out << (CI->getZExtValue() ? "true" : "false");
Chris Lattnerfad86b02008-08-17 07:19:36 +0000914 return;
915 }
916 Out << CI->getValue();
917 return;
918 }
919
920 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000921 if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEdouble ||
922 &CFP->getValueAPF().getSemantics() == &APFloat::IEEEsingle) {
923 // We would like to output the FP constant value in exponential notation,
924 // but we cannot do this if doing so will lose precision. Check here to
925 // make sure that we only output it in exponential format if we can parse
926 // the value back and get the same value.
927 //
Dale Johannesen541ed9f2009-01-21 20:32:55 +0000928 bool ignored;
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000929 bool isDouble = &CFP->getValueAPF().getSemantics()==&APFloat::IEEEdouble;
Chris Lattnerfad86b02008-08-17 07:19:36 +0000930 double Val = isDouble ? CFP->getValueAPF().convertToDouble() :
931 CFP->getValueAPF().convertToFloat();
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000932 std::string StrVal = ftostr(CFP->getValueAPF());
Chris Lattner66e810b2002-04-18 18:53:13 +0000933
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000934 // Check to make sure that the stringized number is not some string like
935 // "Inf" or NaN, that atof will accept, but the lexer will not. Check
936 // that the string matches the "[-+]?[0-9]" regex.
937 //
938 if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
939 ((StrVal[0] == '-' || StrVal[0] == '+') &&
940 (StrVal[1] >= '0' && StrVal[1] <= '9'))) {
941 // Reparse stringized version!
942 if (atof(StrVal.c_str()) == Val) {
943 Out << StrVal;
944 return;
945 }
Chris Lattner66e810b2002-04-18 18:53:13 +0000946 }
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000947 // Otherwise we could not reparse it to exactly the same value, so we must
Dale Johannesen541ed9f2009-01-21 20:32:55 +0000948 // output the string in hexadecimal format! Note that loading and storing
949 // floating point types changes the bits of NaNs on some hosts, notably
950 // x86, so we must not use these types.
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000951 assert(sizeof(double) == sizeof(uint64_t) &&
952 "assuming that double is 64 bits!");
Chris Lattnerc6a13462008-11-10 04:30:26 +0000953 char Buffer[40];
Dale Johannesen541ed9f2009-01-21 20:32:55 +0000954 APFloat apf = CFP->getValueAPF();
955 // Floats are represented in ASCII IR as double, convert.
956 if (!isDouble)
957 apf.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven,
958 &ignored);
959 Out << "0x" <<
960 utohex_buffer(uint64_t(apf.bitcastToAPInt().getZExtValue()),
961 Buffer+40);
Chris Lattnercfb5a202008-08-19 05:06:27 +0000962 return;
963 }
964
965 // Some form of long double. These appear as a magic letter identifying
966 // the type, then a fixed number of hex digits.
967 Out << "0x";
Dale Johannesen1b25cb22009-03-23 21:16:53 +0000968 if (&CFP->getValueAPF().getSemantics() == &APFloat::x87DoubleExtended) {
Chris Lattnercfb5a202008-08-19 05:06:27 +0000969 Out << 'K';
Dale Johannesen1b25cb22009-03-23 21:16:53 +0000970 // api needed to prevent premature destruction
971 APInt api = CFP->getValueAPF().bitcastToAPInt();
972 const uint64_t* p = api.getRawData();
973 uint64_t word = p[1];
974 int shiftcount=12;
975 int width = api.getBitWidth();
976 for (int j=0; j<width; j+=4, shiftcount-=4) {
977 unsigned int nibble = (word>>shiftcount) & 15;
978 if (nibble < 10)
979 Out << (unsigned char)(nibble + '0');
980 else
981 Out << (unsigned char)(nibble - 10 + 'A');
982 if (shiftcount == 0 && j+4 < width) {
983 word = *p;
984 shiftcount = 64;
985 if (width-j-4 < 64)
986 shiftcount = width-j-4;
987 }
988 }
989 return;
990 } else if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEquad)
Chris Lattnercfb5a202008-08-19 05:06:27 +0000991 Out << 'L';
992 else if (&CFP->getValueAPF().getSemantics() == &APFloat::PPCDoubleDouble)
993 Out << 'M';
994 else
Torok Edwinc23197a2009-07-14 16:55:14 +0000995 llvm_unreachable("Unsupported floating point type");
Chris Lattnercfb5a202008-08-19 05:06:27 +0000996 // api needed to prevent premature destruction
Dale Johannesen7111b022008-10-09 18:53:47 +0000997 APInt api = CFP->getValueAPF().bitcastToAPInt();
Chris Lattnercfb5a202008-08-19 05:06:27 +0000998 const uint64_t* p = api.getRawData();
999 uint64_t word = *p;
1000 int shiftcount=60;
1001 int width = api.getBitWidth();
1002 for (int j=0; j<width; j+=4, shiftcount-=4) {
1003 unsigned int nibble = (word>>shiftcount) & 15;
1004 if (nibble < 10)
1005 Out << (unsigned char)(nibble + '0');
Dale Johannesen9d5f4562007-09-12 03:30:33 +00001006 else
Chris Lattnercfb5a202008-08-19 05:06:27 +00001007 Out << (unsigned char)(nibble - 10 + 'A');
1008 if (shiftcount == 0 && j+4 < width) {
1009 word = *(++p);
1010 shiftcount = 64;
1011 if (width-j-4 < 64)
1012 shiftcount = width-j-4;
Dale Johannesen9d5f4562007-09-12 03:30:33 +00001013 }
1014 }
Chris Lattnercfb5a202008-08-19 05:06:27 +00001015 return;
1016 }
1017
1018 if (isa<ConstantAggregateZero>(CV)) {
Chris Lattnerde512b52004-02-15 05:55:15 +00001019 Out << "zeroinitializer";
Chris Lattnercfb5a202008-08-19 05:06:27 +00001020 return;
1021 }
1022
1023 if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
Chris Lattner66e810b2002-04-18 18:53:13 +00001024 // As a special case, print the array as a string if it is an array of
Dan Gohman9b38d7d2008-05-12 16:34:30 +00001025 // i8 with ConstantInt values.
Misha Brukmanfd939082005-04-21 23:48:37 +00001026 //
Chris Lattner66e810b2002-04-18 18:53:13 +00001027 const Type *ETy = CA->getType()->getElementType();
Chris Lattner18365502006-01-23 23:03:36 +00001028 if (CA->isString()) {
Chris Lattner66e810b2002-04-18 18:53:13 +00001029 Out << "c\"";
Chris Lattner18365502006-01-23 23:03:36 +00001030 PrintEscapedString(CA->getAsString(), Out);
Chris Lattnercfb5a202008-08-19 05:06:27 +00001031 Out << '"';
Chris Lattner66e810b2002-04-18 18:53:13 +00001032 } else { // Cannot output in string format...
Misha Brukman40c732c2004-06-04 21:11:51 +00001033 Out << '[';
Chris Lattner7a716ad2002-04-16 21:36:08 +00001034 if (CA->getNumOperands()) {
Chris Lattner0f7364b2009-02-28 21:26:53 +00001035 TypePrinter.print(ETy, Out);
Dan Gohman8dae1382008-09-14 17:21:12 +00001036 Out << ' ';
Chris Lattner7a716ad2002-04-16 21:36:08 +00001037 WriteAsOperandInternal(Out, CA->getOperand(0),
Dan Gohmand6c0f652009-08-13 15:27:57 +00001038 &TypePrinter, Machine);
Chris Lattner7a716ad2002-04-16 21:36:08 +00001039 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
1040 Out << ", ";
Chris Lattner0f7364b2009-02-28 21:26:53 +00001041 TypePrinter.print(ETy, Out);
Dan Gohman8dae1382008-09-14 17:21:12 +00001042 Out << ' ';
Dan Gohmand6c0f652009-08-13 15:27:57 +00001043 WriteAsOperandInternal(Out, CA->getOperand(i), &TypePrinter, Machine);
Chris Lattner7a716ad2002-04-16 21:36:08 +00001044 }
1045 }
Dan Gohman8dae1382008-09-14 17:21:12 +00001046 Out << ']';
Chris Lattner7a716ad2002-04-16 21:36:08 +00001047 }
Chris Lattnercfb5a202008-08-19 05:06:27 +00001048 return;
1049 }
1050
1051 if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
Andrew Lenharth43f344a2007-01-08 18:21:30 +00001052 if (CS->getType()->isPacked())
1053 Out << '<';
Misha Brukman40c732c2004-06-04 21:11:51 +00001054 Out << '{';
Jim Laskeya3f332b2006-02-25 12:27:03 +00001055 unsigned N = CS->getNumOperands();
1056 if (N) {
Chris Lattner24233032008-08-19 04:47:09 +00001057 Out << ' ';
Chris Lattner0f7364b2009-02-28 21:26:53 +00001058 TypePrinter.print(CS->getOperand(0)->getType(), Out);
Dan Gohman8dae1382008-09-14 17:21:12 +00001059 Out << ' ';
Chris Lattner7a716ad2002-04-16 21:36:08 +00001060
Dan Gohmand6c0f652009-08-13 15:27:57 +00001061 WriteAsOperandInternal(Out, CS->getOperand(0), &TypePrinter, Machine);
Chris Lattner7a716ad2002-04-16 21:36:08 +00001062
Jim Laskeya3f332b2006-02-25 12:27:03 +00001063 for (unsigned i = 1; i < N; i++) {
Chris Lattner7a716ad2002-04-16 21:36:08 +00001064 Out << ", ";
Chris Lattner0f7364b2009-02-28 21:26:53 +00001065 TypePrinter.print(CS->getOperand(i)->getType(), Out);
Dan Gohman8dae1382008-09-14 17:21:12 +00001066 Out << ' ';
Chris Lattner7a716ad2002-04-16 21:36:08 +00001067
Dan Gohmand6c0f652009-08-13 15:27:57 +00001068 WriteAsOperandInternal(Out, CS->getOperand(i), &TypePrinter, Machine);
Chris Lattner7a716ad2002-04-16 21:36:08 +00001069 }
Dan Gohman8dae1382008-09-14 17:21:12 +00001070 Out << ' ';
Chris Lattner7a716ad2002-04-16 21:36:08 +00001071 }
Jim Laskeya3f332b2006-02-25 12:27:03 +00001072
Dan Gohman8dae1382008-09-14 17:21:12 +00001073 Out << '}';
Andrew Lenharth43f344a2007-01-08 18:21:30 +00001074 if (CS->getType()->isPacked())
1075 Out << '>';
Chris Lattnercfb5a202008-08-19 05:06:27 +00001076 return;
1077 }
1078
1079 if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
1080 const Type *ETy = CP->getType()->getElementType();
1081 assert(CP->getNumOperands() > 0 &&
1082 "Number of operands for a PackedConst must be > 0");
Dan Gohman7dfa07f2009-02-11 00:25:25 +00001083 Out << '<';
Chris Lattner0f7364b2009-02-28 21:26:53 +00001084 TypePrinter.print(ETy, Out);
Dan Gohman8dae1382008-09-14 17:21:12 +00001085 Out << ' ';
Dan Gohmand6c0f652009-08-13 15:27:57 +00001086 WriteAsOperandInternal(Out, CP->getOperand(0), &TypePrinter, Machine);
Chris Lattnercfb5a202008-08-19 05:06:27 +00001087 for (unsigned i = 1, e = CP->getNumOperands(); i != e; ++i) {
Chris Lattner4667b712008-08-19 05:26:17 +00001088 Out << ", ";
Chris Lattner0f7364b2009-02-28 21:26:53 +00001089 TypePrinter.print(ETy, Out);
Dan Gohman8dae1382008-09-14 17:21:12 +00001090 Out << ' ';
Dan Gohmand6c0f652009-08-13 15:27:57 +00001091 WriteAsOperandInternal(Out, CP->getOperand(i), &TypePrinter, Machine);
Chris Lattnercfb5a202008-08-19 05:06:27 +00001092 }
Dan Gohman7dfa07f2009-02-11 00:25:25 +00001093 Out << '>';
Chris Lattnercfb5a202008-08-19 05:06:27 +00001094 return;
1095 }
1096
1097 if (isa<ConstantPointerNull>(CV)) {
Chris Lattner7a716ad2002-04-16 21:36:08 +00001098 Out << "null";
Chris Lattnercfb5a202008-08-19 05:06:27 +00001099 return;
1100 }
1101
1102 if (isa<UndefValue>(CV)) {
Chris Lattnerb976e662004-10-16 18:08:06 +00001103 Out << "undef";
Chris Lattnercfb5a202008-08-19 05:06:27 +00001104 return;
1105 }
Nick Lewycky21cc4462009-04-04 07:22:01 +00001106
Devang Patel320671d2009-07-08 21:44:25 +00001107 if (const MDNode *Node = dyn_cast<MDNode>(CV)) {
1108 Out << "!" << Machine->getMetadataSlot(Node);
1109 return;
1110 }
1111
Chris Lattnercfb5a202008-08-19 05:06:27 +00001112 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Reid Spencer81dfeb32006-12-04 05:19:18 +00001113 Out << CE->getOpcodeName();
Dan Gohman59858cf2009-07-27 16:11:46 +00001114 WriteOptimizationInfo(Out, CE);
Reid Spencer81dfeb32006-12-04 05:19:18 +00001115 if (CE->isCompare())
Chris Lattnercfb5a202008-08-19 05:06:27 +00001116 Out << ' ' << getPredicateText(CE->getPredicate());
Reid Spencer81dfeb32006-12-04 05:19:18 +00001117 Out << " (";
Misha Brukmanfd939082005-04-21 23:48:37 +00001118
Vikram S. Adveb4dbb442002-07-14 23:14:45 +00001119 for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) {
Chris Lattner0f7364b2009-02-28 21:26:53 +00001120 TypePrinter.print((*OI)->getType(), Out);
Dan Gohman8dae1382008-09-14 17:21:12 +00001121 Out << ' ';
Dan Gohmand6c0f652009-08-13 15:27:57 +00001122 WriteAsOperandInternal(Out, *OI, &TypePrinter, Machine);
Vikram S. Adveb4dbb442002-07-14 23:14:45 +00001123 if (OI+1 != CE->op_end())
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001124 Out << ", ";
Vikram S. Adveb4dbb442002-07-14 23:14:45 +00001125 }
Misha Brukmanfd939082005-04-21 23:48:37 +00001126
Dan Gohman995be7d2008-05-31 19:12:39 +00001127 if (CE->hasIndices()) {
1128 const SmallVector<unsigned, 4> &Indices = CE->getIndices();
1129 for (unsigned i = 0, e = Indices.size(); i != e; ++i)
1130 Out << ", " << Indices[i];
1131 }
1132
Reid Spencer3da59db2006-11-27 01:05:10 +00001133 if (CE->isCast()) {
Chris Lattner95586b82002-08-15 19:37:43 +00001134 Out << " to ";
Chris Lattner0f7364b2009-02-28 21:26:53 +00001135 TypePrinter.print(CE->getType(), Out);
Chris Lattner95586b82002-08-15 19:37:43 +00001136 }
Reid Spencer3da59db2006-11-27 01:05:10 +00001137
Misha Brukman40c732c2004-06-04 21:11:51 +00001138 Out << ')';
Chris Lattnercfb5a202008-08-19 05:06:27 +00001139 return;
Chris Lattner7a716ad2002-04-16 21:36:08 +00001140 }
Chris Lattnercfb5a202008-08-19 05:06:27 +00001141
1142 Out << "<placeholder or erroneous Constant>";
Chris Lattner7a716ad2002-04-16 21:36:08 +00001143}
1144
1145
Misha Brukmanab5c6002004-03-02 00:22:19 +00001146/// WriteAsOperand - Write the name of the specified value out to the specified
1147/// ostream. This can be useful when you just want to print int %reg126, not
1148/// the whole instruction that generated it.
1149///
Dan Gohman1220e102009-08-12 20:56:03 +00001150static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
Dan Gohmand6c0f652009-08-13 15:27:57 +00001151 TypePrinting *TypePrinter,
Chris Lattner0d9574a2008-08-19 04:26:57 +00001152 SlotTracker *Machine) {
Chris Lattnerc97536e2008-08-17 04:40:13 +00001153 if (V->hasName()) {
1154 PrintLLVMName(Out, V);
1155 return;
1156 }
1157
1158 const Constant *CV = dyn_cast<Constant>(V);
1159 if (CV && !isa<GlobalValue>(CV)) {
Dan Gohmand6c0f652009-08-13 15:27:57 +00001160 assert(TypePrinter && "Constants require TypePrinting!");
1161 WriteConstantInt(Out, CV, *TypePrinter, Machine);
Chris Lattnercfb5a202008-08-19 05:06:27 +00001162 return;
1163 }
1164
1165 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
Chris Lattnerc97536e2008-08-17 04:40:13 +00001166 Out << "asm ";
1167 if (IA->hasSideEffects())
1168 Out << "sideeffect ";
1169 Out << '"';
1170 PrintEscapedString(IA->getAsmString(), Out);
1171 Out << "\", \"";
1172 PrintEscapedString(IA->getConstraintString(), Out);
1173 Out << '"';
Chris Lattnercfb5a202008-08-19 05:06:27 +00001174 return;
1175 }
Devang Patele54abc92009-07-22 17:43:22 +00001176
Devang Patel104cf9e2009-07-23 01:07:34 +00001177 if (const MDNode *N = dyn_cast<MDNode>(V)) {
1178 Out << '!' << Machine->getMetadataSlot(N);
1179 return;
1180 }
1181
Devang Patele54abc92009-07-22 17:43:22 +00001182 if (const MDString *MDS = dyn_cast<MDString>(V)) {
Devang Patele54abc92009-07-22 17:43:22 +00001183 Out << "!\"";
Daniel Dunbar03d76512009-07-25 23:55:21 +00001184 PrintEscapedString(MDS->getString(), Out);
Devang Patele54abc92009-07-22 17:43:22 +00001185 Out << '"';
1186 return;
1187 }
1188
Chris Lattnercfb5a202008-08-19 05:06:27 +00001189 char Prefix = '%';
1190 int Slot;
1191 if (Machine) {
1192 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1193 Slot = Machine->getGlobalSlot(GV);
1194 Prefix = '@';
1195 } else {
1196 Slot = Machine->getLocalSlot(V);
1197 }
Chris Lattnerc97536e2008-08-17 04:40:13 +00001198 } else {
Chris Lattnercfb5a202008-08-19 05:06:27 +00001199 Machine = createSlotTracker(V);
Chris Lattnerc97536e2008-08-17 04:40:13 +00001200 if (Machine) {
1201 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1202 Slot = Machine->getGlobalSlot(GV);
1203 Prefix = '@';
1204 } else {
1205 Slot = Machine->getLocalSlot(V);
1206 }
Dan Gohmand6c0f652009-08-13 15:27:57 +00001207 delete Machine;
Chris Lattner80cd1152006-01-25 22:26:05 +00001208 } else {
Chris Lattnercfb5a202008-08-19 05:06:27 +00001209 Slot = -1;
Chris Lattner7a716ad2002-04-16 21:36:08 +00001210 }
1211 }
Chris Lattnercfb5a202008-08-19 05:06:27 +00001212
1213 if (Slot != -1)
1214 Out << Prefix << Slot;
1215 else
1216 Out << "<badref>";
Chris Lattner7a716ad2002-04-16 21:36:08 +00001217}
1218
Misha Brukman9d0802e2004-03-01 19:48:13 +00001219/// WriteAsOperand - Write the name of the specified value out to the specified
1220/// ostream. This can be useful when you just want to print int %reg126, not
1221/// the whole instruction that generated it.
1222///
Chris Lattner24233032008-08-19 04:47:09 +00001223void llvm::WriteAsOperand(std::ostream &Out, const Value *V, bool PrintType,
1224 const Module *Context) {
Chris Lattner944fac72008-08-23 22:23:09 +00001225 raw_os_ostream OS(Out);
1226 WriteAsOperand(OS, V, PrintType, Context);
1227}
1228
Dan Gohman1220e102009-08-12 20:56:03 +00001229void llvm::WriteAsOperand(raw_ostream &Out, const Value *V,
1230 bool PrintType, const Module *Context) {
Dan Gohmand6c0f652009-08-13 15:27:57 +00001231
1232 // Fast path: Don't construct and populate a TypePrinting object if we
1233 // won't be needing any types printed.
1234 if (!PrintType && !isa<Constant>(V)) {
1235 WriteAsOperandInternal(Out, V, 0, 0);
1236 return;
1237 }
1238
Chris Lattner607dc682002-07-10 16:48:17 +00001239 if (Context == 0) Context = getModuleFromVal(V);
Chris Lattner207b5bc2001-10-29 16:37:48 +00001240
Chris Lattnere9fa33e2009-02-28 23:20:19 +00001241 TypePrinting TypePrinter;
Chris Lattner413fd232009-03-01 00:03:38 +00001242 std::vector<const Type*> NumberedTypes;
1243 AddModuleTypesToPrinter(TypePrinter, NumberedTypes, Context);
Dan Gohman8dae1382008-09-14 17:21:12 +00001244 if (PrintType) {
Chris Lattner0f7364b2009-02-28 21:26:53 +00001245 TypePrinter.print(V->getType(), Out);
Dan Gohman8dae1382008-09-14 17:21:12 +00001246 Out << ' ';
1247 }
Misha Brukmanfd939082005-04-21 23:48:37 +00001248
Dan Gohmand6c0f652009-08-13 15:27:57 +00001249 WriteAsOperandInternal(Out, V, &TypePrinter, 0);
Chris Lattner622f7402001-07-20 19:15:21 +00001250}
1251
Chris Lattnercfb5a202008-08-19 05:06:27 +00001252namespace {
Chris Lattnerd8c2e422001-07-12 23:35:26 +00001253
Chris Lattner007377f2001-09-07 16:36:04 +00001254class AssemblyWriter {
Dan Gohman683e9222009-08-12 17:23:50 +00001255 formatted_raw_ostream &Out;
Chris Lattner0d9574a2008-08-19 04:26:57 +00001256 SlotTracker &Machine;
Chris Lattnerc1824992001-10-29 16:05:51 +00001257 const Module *TheModule;
Chris Lattner9cc34462009-02-28 20:25:14 +00001258 TypePrinting TypePrinter;
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001259 AssemblyAnnotationWriter *AnnotationWriter;
Chris Lattner413fd232009-03-01 00:03:38 +00001260 std::vector<const Type*> NumberedTypes;
Devang Patel923078c2009-07-01 19:21:12 +00001261
1262 // Each MDNode is assigned unique MetadataIDNo.
1263 std::map<const MDNode *, unsigned> MDNodes;
1264 unsigned MetadataIDNo;
Chris Lattner00950542001-06-06 20:29:01 +00001265public:
Dan Gohman683e9222009-08-12 17:23:50 +00001266 inline AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac,
1267 const Module *M,
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001268 AssemblyAnnotationWriter *AAW)
Devang Patel923078c2009-07-01 19:21:12 +00001269 : Out(o), Machine(Mac), TheModule(M), AnnotationWriter(AAW), MetadataIDNo(0) {
Chris Lattner413fd232009-03-01 00:03:38 +00001270 AddModuleTypesToPrinter(TypePrinter, NumberedTypes, M);
Chris Lattner00950542001-06-06 20:29:01 +00001271 }
1272
Chris Lattner413fd232009-03-01 00:03:38 +00001273 void write(const Module *M) { printModule(M); }
Chris Lattner944fac72008-08-23 22:23:09 +00001274
1275 void write(const GlobalValue *G) {
1276 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(G))
1277 printGlobal(GV);
1278 else if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(G))
1279 printAlias(GA);
1280 else if (const Function *F = dyn_cast<Function>(G))
1281 printFunction(F);
1282 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001283 llvm_unreachable("Unknown global");
Chris Lattner944fac72008-08-23 22:23:09 +00001284 }
1285
Chris Lattnercfb5a202008-08-19 05:06:27 +00001286 void write(const BasicBlock *BB) { printBasicBlock(BB); }
1287 void write(const Instruction *I) { printInstruction(*I); }
Chris Lattner00950542001-06-06 20:29:01 +00001288
Chris Lattner2fcfdb72006-12-06 06:24:27 +00001289 void writeOperand(const Value *Op, bool PrintType);
Devang Pateleaf42ab2008-09-23 23:03:40 +00001290 void writeParamOperand(const Value *Operand, Attributes Attrs);
Chris Lattner66e810b2002-04-18 18:53:13 +00001291
Misha Brukman5cf1acf2004-04-28 15:31:21 +00001292 const Module* getModule() { return TheModule; }
1293
Misha Brukmanf771bea2004-11-15 19:30:05 +00001294private:
Chris Lattnerc1824992001-10-29 16:05:51 +00001295 void printModule(const Module *M);
Reid Spencer78d033e2007-01-06 07:24:44 +00001296 void printTypeSymbolTable(const TypeSymbolTable &ST);
Chris Lattnerc1824992001-10-29 16:05:51 +00001297 void printGlobal(const GlobalVariable *GV);
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001298 void printAlias(const GlobalAlias *GV);
Chris Lattner79df7c02002-03-26 18:01:55 +00001299 void printFunction(const Function *F);
Devang Pateleaf42ab2008-09-23 23:03:40 +00001300 void printArgument(const Argument *FA, Attributes Attrs);
Chris Lattnerc1824992001-10-29 16:05:51 +00001301 void printBasicBlock(const BasicBlock *BB);
Chris Lattner7e708292002-06-25 16:13:24 +00001302 void printInstruction(const Instruction &I);
Chris Lattner2761e9f2002-04-13 20:53:41 +00001303
Chris Lattnere02fa852001-10-13 06:42:36 +00001304 // printInfoComment - Print a little comment after the instruction indicating
1305 // which slot it occupies.
Chris Lattner7e708292002-06-25 16:13:24 +00001306 void printInfoComment(const Value &V);
Chris Lattner00950542001-06-06 20:29:01 +00001307};
Chris Lattner413fd232009-03-01 00:03:38 +00001308} // end of anonymous namespace
Chris Lattner00950542001-06-06 20:29:01 +00001309
Chris Lattner2761e9f2002-04-13 20:53:41 +00001310
Chris Lattner2fcfdb72006-12-06 06:24:27 +00001311void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType) {
1312 if (Operand == 0) {
Chris Lattneraab18202005-02-24 16:58:29 +00001313 Out << "<null operand!>";
Chris Lattner2fcfdb72006-12-06 06:24:27 +00001314 } else {
Chris Lattnercfb5a202008-08-19 05:06:27 +00001315 if (PrintType) {
Chris Lattner0f7364b2009-02-28 21:26:53 +00001316 TypePrinter.print(Operand->getType(), Out);
Dan Gohman8dae1382008-09-14 17:21:12 +00001317 Out << ' ';
Chris Lattnercfb5a202008-08-19 05:06:27 +00001318 }
Dan Gohmand6c0f652009-08-13 15:27:57 +00001319 WriteAsOperandInternal(Out, Operand, &TypePrinter, &Machine);
Chris Lattneraab18202005-02-24 16:58:29 +00001320 }
Chris Lattner00950542001-06-06 20:29:01 +00001321}
1322
Dale Johannesen0d51e7e2008-02-19 21:38:47 +00001323void AssemblyWriter::writeParamOperand(const Value *Operand,
Devang Pateleaf42ab2008-09-23 23:03:40 +00001324 Attributes Attrs) {
Duncan Sandsdc024672007-11-27 13:23:08 +00001325 if (Operand == 0) {
1326 Out << "<null operand!>";
1327 } else {
Duncan Sandsdc024672007-11-27 13:23:08 +00001328 // Print the type
Chris Lattner0f7364b2009-02-28 21:26:53 +00001329 TypePrinter.print(Operand->getType(), Out);
Duncan Sandsdc024672007-11-27 13:23:08 +00001330 // Print parameter attributes list
Devang Patel05988662008-09-25 21:00:45 +00001331 if (Attrs != Attribute::None)
1332 Out << ' ' << Attribute::getAsString(Attrs);
Dan Gohman8dae1382008-09-14 17:21:12 +00001333 Out << ' ';
Duncan Sandsdc024672007-11-27 13:23:08 +00001334 // Print the operand
Dan Gohmand6c0f652009-08-13 15:27:57 +00001335 WriteAsOperandInternal(Out, Operand, &TypePrinter, &Machine);
Duncan Sandsdc024672007-11-27 13:23:08 +00001336 }
1337}
Chris Lattner00950542001-06-06 20:29:01 +00001338
Chris Lattnerc1824992001-10-29 16:05:51 +00001339void AssemblyWriter::printModule(const Module *M) {
Chris Lattner31ab1b32005-03-02 23:12:40 +00001340 if (!M->getModuleIdentifier().empty() &&
Misha Brukmanfd939082005-04-21 23:48:37 +00001341 // Don't print the ID if it will start a new line (which would
Chris Lattner31ab1b32005-03-02 23:12:40 +00001342 // require a comment char before it).
1343 M->getModuleIdentifier().find('\n') == std::string::npos)
1344 Out << "; ModuleID = '" << M->getModuleIdentifier() << "'\n";
1345
Owen Andersoncf7ff2b2006-10-18 02:21:12 +00001346 if (!M->getDataLayout().empty())
Chris Lattnerd2f9e602006-10-22 06:06:56 +00001347 Out << "target datalayout = \"" << M->getDataLayout() << "\"\n";
Reid Spencercddc86f2004-07-25 21:44:54 +00001348 if (!M->getTargetTriple().empty())
Reid Spencerc9a1f0d2004-07-25 21:29:43 +00001349 Out << "target triple = \"" << M->getTargetTriple() << "\"\n";
Misha Brukmanfd939082005-04-21 23:48:37 +00001350
Chris Lattnercc041ba2006-01-24 04:13:11 +00001351 if (!M->getModuleInlineAsm().empty()) {
Chris Lattner42a162e2006-01-24 00:45:30 +00001352 // Split the string into lines, to make it easier to read the .ll file.
Chris Lattnercc041ba2006-01-24 04:13:11 +00001353 std::string Asm = M->getModuleInlineAsm();
Chris Lattner42a162e2006-01-24 00:45:30 +00001354 size_t CurPos = 0;
1355 size_t NewLine = Asm.find_first_of('\n', CurPos);
Dan Gohman9bf0b9b2009-08-12 23:54:22 +00001356 Out << '\n';
Chris Lattner42a162e2006-01-24 00:45:30 +00001357 while (NewLine != std::string::npos) {
1358 // We found a newline, print the portion of the asm string from the
1359 // last newline up to this newline.
1360 Out << "module asm \"";
1361 PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.begin()+NewLine),
1362 Out);
1363 Out << "\"\n";
1364 CurPos = NewLine+1;
1365 NewLine = Asm.find_first_of('\n', CurPos);
1366 }
Chris Lattner71cdba32006-01-24 00:40:17 +00001367 Out << "module asm \"";
Chris Lattner42a162e2006-01-24 00:45:30 +00001368 PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.end()), Out);
Chris Lattner18365502006-01-23 23:03:36 +00001369 Out << "\"\n";
1370 }
1371
Chris Lattner44da7d72004-09-14 05:06:58 +00001372 // Loop over the dependent libraries and emit them.
Chris Lattnercfe97b72004-09-14 04:51:44 +00001373 Module::lib_iterator LI = M->lib_begin();
1374 Module::lib_iterator LE = M->lib_end();
Reid Spencercddc86f2004-07-25 21:44:54 +00001375 if (LI != LE) {
Dan Gohman9bf0b9b2009-08-12 23:54:22 +00001376 Out << '\n';
Chris Lattnercfe97b72004-09-14 04:51:44 +00001377 Out << "deplibs = [ ";
1378 while (LI != LE) {
Chris Lattner44da7d72004-09-14 05:06:58 +00001379 Out << '"' << *LI << '"';
Reid Spencerc9a1f0d2004-07-25 21:29:43 +00001380 ++LI;
Chris Lattnercfe97b72004-09-14 04:51:44 +00001381 if (LI != LE)
1382 Out << ", ";
Reid Spencerc9a1f0d2004-07-25 21:29:43 +00001383 }
Dan Gohman9bf0b9b2009-08-12 23:54:22 +00001384 Out << " ]";
Reid Spencer83f6a772004-07-25 18:08:18 +00001385 }
Reid Spencere59eaf42004-09-13 23:44:23 +00001386
Chris Lattner413fd232009-03-01 00:03:38 +00001387 // Loop over the symbol table, emitting all id'd types.
Dan Gohman9bf0b9b2009-08-12 23:54:22 +00001388 if (!M->getTypeSymbolTable().empty() || !NumberedTypes.empty()) Out << '\n';
Reid Spencer78d033e2007-01-06 07:24:44 +00001389 printTypeSymbolTable(M->getTypeSymbolTable());
Misha Brukmanfd939082005-04-21 23:48:37 +00001390
Dan Gohman9bf0b9b2009-08-12 23:54:22 +00001391 // Output all globals.
1392 if (!M->global_empty()) Out << '\n';
Chris Lattnerd6d826c2006-12-06 04:41:52 +00001393 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
1394 I != E; ++I)
Chris Lattner7e708292002-06-25 16:13:24 +00001395 printGlobal(I);
Chris Lattner69dacfc2007-04-26 02:24:10 +00001396
1397 // Output all aliases.
1398 if (!M->alias_empty()) Out << "\n";
1399 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
1400 I != E; ++I)
1401 printAlias(I);
Chris Lattner007377f2001-09-07 16:36:04 +00001402
Chris Lattner44da7d72004-09-14 05:06:58 +00001403 // Output all of the functions.
Chris Lattner7e708292002-06-25 16:13:24 +00001404 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
1405 printFunction(I);
Devang Patel320671d2009-07-08 21:44:25 +00001406
Devang Patel37c4a2d2009-07-29 22:04:47 +00001407 // Output named metadata.
Dan Gohman9bf0b9b2009-08-12 23:54:22 +00001408 if (!M->named_metadata_empty()) Out << '\n';
Devang Patel37c4a2d2009-07-29 22:04:47 +00001409 for (Module::const_named_metadata_iterator I = M->named_metadata_begin(),
1410 E = M->named_metadata_end(); I != E; ++I) {
1411 const NamedMDNode *NMD = I;
1412 Out << "!" << NMD->getName() << " = !{";
1413 for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i) {
1414 if (i) Out << ", ";
Devang Patele8861b82009-07-30 01:02:04 +00001415 MDNode *MD = dyn_cast_or_null<MDNode>(NMD->getElement(i));
Devang Patel37c4a2d2009-07-29 22:04:47 +00001416 Out << '!' << Machine.getMetadataSlot(MD);
1417 }
1418 Out << "}\n";
1419 }
1420
1421 // Output metadata.
Dan Gohman9bf0b9b2009-08-12 23:54:22 +00001422 if (!Machine.mdnEmpty()) Out << '\n';
Devang Patel320671d2009-07-08 21:44:25 +00001423 WriteMDNodes(Out, TypePrinter, Machine);
Chris Lattner007377f2001-09-07 16:36:04 +00001424}
1425
Dan Gohman683e9222009-08-12 17:23:50 +00001426static void PrintLinkage(GlobalValue::LinkageTypes LT,
1427 formatted_raw_ostream &Out) {
Chris Lattnercfb5a202008-08-19 05:06:27 +00001428 switch (LT) {
Bill Wendling3d10a5a2009-07-20 01:03:30 +00001429 case GlobalValue::ExternalLinkage: break;
1430 case GlobalValue::PrivateLinkage: Out << "private "; break;
1431 case GlobalValue::LinkerPrivateLinkage: Out << "linker_private "; break;
1432 case GlobalValue::InternalLinkage: Out << "internal "; break;
1433 case GlobalValue::LinkOnceAnyLinkage: Out << "linkonce "; break;
1434 case GlobalValue::LinkOnceODRLinkage: Out << "linkonce_odr "; break;
1435 case GlobalValue::WeakAnyLinkage: Out << "weak "; break;
1436 case GlobalValue::WeakODRLinkage: Out << "weak_odr "; break;
1437 case GlobalValue::CommonLinkage: Out << "common "; break;
1438 case GlobalValue::AppendingLinkage: Out << "appending "; break;
1439 case GlobalValue::DLLImportLinkage: Out << "dllimport "; break;
1440 case GlobalValue::DLLExportLinkage: Out << "dllexport "; break;
1441 case GlobalValue::ExternalWeakLinkage: Out << "extern_weak "; break;
Chris Lattner266c7bb2009-04-13 05:44:34 +00001442 case GlobalValue::AvailableExternallyLinkage:
1443 Out << "available_externally ";
1444 break;
Chris Lattnercfb5a202008-08-19 05:06:27 +00001445 case GlobalValue::GhostLinkage:
Torok Edwinc23197a2009-07-14 16:55:14 +00001446 llvm_unreachable("GhostLinkage not allowed in AsmWriter!");
Chris Lattnercfb5a202008-08-19 05:06:27 +00001447 }
1448}
Duncan Sands667d4b82009-03-07 15:45:40 +00001449
Chris Lattnercfb5a202008-08-19 05:06:27 +00001450
1451static void PrintVisibility(GlobalValue::VisibilityTypes Vis,
Dan Gohman683e9222009-08-12 17:23:50 +00001452 formatted_raw_ostream &Out) {
Chris Lattnercfb5a202008-08-19 05:06:27 +00001453 switch (Vis) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001454 default: llvm_unreachable("Invalid visibility style!");
Chris Lattnercfb5a202008-08-19 05:06:27 +00001455 case GlobalValue::DefaultVisibility: break;
1456 case GlobalValue::HiddenVisibility: Out << "hidden "; break;
1457 case GlobalValue::ProtectedVisibility: Out << "protected "; break;
1458 }
1459}
1460
Chris Lattnerc1824992001-10-29 16:05:51 +00001461void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Dan Gohmand6c0f652009-08-13 15:27:57 +00001462 WriteAsOperandInternal(Out, GV, &TypePrinter, &Machine);
Dan Gohman3845e502009-08-12 23:32:33 +00001463 Out << " = ";
Chris Lattnerd70684f2001-09-18 04:01:05 +00001464
Chris Lattner52b26de2008-08-19 05:16:28 +00001465 if (!GV->hasInitializer() && GV->hasExternalLinkage())
1466 Out << "external ";
1467
1468 PrintLinkage(GV->getLinkage(), Out);
1469 PrintVisibility(GV->getVisibility(), Out);
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00001470
1471 if (GV->isThreadLocal()) Out << "thread_local ";
Chris Lattnerdf986172009-01-02 07:01:27 +00001472 if (unsigned AddressSpace = GV->getType()->getAddressSpace())
1473 Out << "addrspace(" << AddressSpace << ") ";
Misha Brukman0313e0b2004-06-21 21:53:56 +00001474 Out << (GV->isConstant() ? "constant " : "global ");
Chris Lattner0f7364b2009-02-28 21:26:53 +00001475 TypePrinter.print(GV->getType()->getElementType(), Out);
Chris Lattnerd70684f2001-09-18 04:01:05 +00001476
Dan Gohman8dae1382008-09-14 17:21:12 +00001477 if (GV->hasInitializer()) {
1478 Out << ' ';
Devang Patel320671d2009-07-08 21:44:25 +00001479 writeOperand(GV->getInitializer(), false);
Dan Gohman8dae1382008-09-14 17:21:12 +00001480 }
Christopher Lambfe63fb92007-12-11 08:59:05 +00001481
Chris Lattner60962db2005-11-12 00:10:19 +00001482 if (GV->hasSection())
1483 Out << ", section \"" << GV->getSection() << '"';
1484 if (GV->getAlignment())
Chris Lattner30caa282005-11-06 06:48:53 +00001485 Out << ", align " << GV->getAlignment();
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001486
Chris Lattner7e708292002-06-25 16:13:24 +00001487 printInfoComment(*GV);
Chris Lattnercfb5a202008-08-19 05:06:27 +00001488 Out << '\n';
Chris Lattner70cc3392001-09-10 07:58:01 +00001489}
1490
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001491void AssemblyWriter::printAlias(const GlobalAlias *GA) {
Dale Johannesen24f07dc2008-06-03 18:14:29 +00001492 // Don't crash when dumping partially built GA
1493 if (!GA->hasName())
1494 Out << "<<nameless>> = ";
Chris Lattnerc97536e2008-08-17 04:40:13 +00001495 else {
1496 PrintLLVMName(Out, GA);
1497 Out << " = ";
1498 }
Chris Lattnercfb5a202008-08-19 05:06:27 +00001499 PrintVisibility(GA->getVisibility(), Out);
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001500
1501 Out << "alias ";
1502
Chris Lattnercfb5a202008-08-19 05:06:27 +00001503 PrintLinkage(GA->getLinkage(), Out);
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001504
Anton Korobeynikovc6c98af2007-04-29 18:02:48 +00001505 const Constant *Aliasee = GA->getAliasee();
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001506
1507 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Aliasee)) {
Chris Lattner0f7364b2009-02-28 21:26:53 +00001508 TypePrinter.print(GV->getType(), Out);
Chris Lattnerc97536e2008-08-17 04:40:13 +00001509 Out << ' ';
1510 PrintLLVMName(Out, GV);
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001511 } else if (const Function *F = dyn_cast<Function>(Aliasee)) {
Chris Lattner0f7364b2009-02-28 21:26:53 +00001512 TypePrinter.print(F->getFunctionType(), Out);
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001513 Out << "* ";
1514
Dan Gohmand6c0f652009-08-13 15:27:57 +00001515 WriteAsOperandInternal(Out, F, &TypePrinter, &Machine);
Anton Korobeynikov591858a2008-03-22 08:17:17 +00001516 } else if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(Aliasee)) {
Chris Lattner0f7364b2009-02-28 21:26:53 +00001517 TypePrinter.print(GA->getType(), Out);
1518 Out << ' ';
Chris Lattnerc97536e2008-08-17 04:40:13 +00001519 PrintLLVMName(Out, GA);
Anton Korobeynikova80e1182007-04-28 13:45:00 +00001520 } else {
Chris Lattnera2165ed2009-04-25 21:23:19 +00001521 const ConstantExpr *CE = cast<ConstantExpr>(Aliasee);
1522 // The only valid GEP is an all zero GEP.
1523 assert((CE->getOpcode() == Instruction::BitCast ||
1524 CE->getOpcode() == Instruction::GetElementPtr) &&
1525 "Unsupported aliasee");
1526 writeOperand(CE, false);
Anton Korobeynikova80e1182007-04-28 13:45:00 +00001527 }
1528
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001529 printInfoComment(*GA);
Chris Lattner52b26de2008-08-19 05:16:28 +00001530 Out << '\n';
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001531}
1532
Reid Spencer78d033e2007-01-06 07:24:44 +00001533void AssemblyWriter::printTypeSymbolTable(const TypeSymbolTable &ST) {
Chris Lattner413fd232009-03-01 00:03:38 +00001534 // Emit all numbered types.
1535 for (unsigned i = 0, e = NumberedTypes.size(); i != e; ++i) {
Dan Gohman3845e502009-08-12 23:32:33 +00001536 Out << '%' << i << " = type ";
Chris Lattner413fd232009-03-01 00:03:38 +00001537
1538 // Make sure we print out at least one level of the type structure, so
1539 // that we do not get %2 = type %2
1540 TypePrinter.printAtLeastOneLevel(NumberedTypes[i], Out);
Dan Gohman9bf0b9b2009-08-12 23:54:22 +00001541 Out << '\n';
Chris Lattner413fd232009-03-01 00:03:38 +00001542 }
1543
1544 // Print the named types.
Reid Spencer78d033e2007-01-06 07:24:44 +00001545 for (TypeSymbolTable::const_iterator TI = ST.begin(), TE = ST.end();
1546 TI != TE; ++TI) {
Daniel Dunbar03d76512009-07-25 23:55:21 +00001547 PrintLLVMName(Out, TI->first, LocalPrefix);
Chris Lattner52b26de2008-08-19 05:16:28 +00001548 Out << " = type ";
Reid Spencer9231ac82004-05-25 08:53:40 +00001549
1550 // Make sure we print out at least one level of the type structure, so
1551 // that we do not get %FILE = type %FILE
Chris Lattner0f7364b2009-02-28 21:26:53 +00001552 TypePrinter.printAtLeastOneLevel(TI->second, Out);
Chris Lattnercfb5a202008-08-19 05:06:27 +00001553 Out << '\n';
Reid Spencer9231ac82004-05-25 08:53:40 +00001554 }
Reid Spencer78d033e2007-01-06 07:24:44 +00001555}
1556
Misha Brukmanab5c6002004-03-02 00:22:19 +00001557/// printFunction - Print all aspects of a function.
1558///
Chris Lattner7e708292002-06-25 16:13:24 +00001559void AssemblyWriter::printFunction(const Function *F) {
Chris Lattnercfb5a202008-08-19 05:06:27 +00001560 // Print out the return type and name.
1561 Out << '\n';
Chris Lattner4ad02e72003-04-16 20:28:45 +00001562
Misha Brukman0313e0b2004-06-21 21:53:56 +00001563 if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, Out);
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001564
Reid Spencer5cbf9852007-01-30 20:08:39 +00001565 if (F->isDeclaration())
Chris Lattner3aa60662007-08-19 22:15:26 +00001566 Out << "declare ";
1567 else
Reid Spencerb951bc02006-12-29 20:29:48 +00001568 Out << "define ";
Chris Lattnercfb5a202008-08-19 05:06:27 +00001569
1570 PrintLinkage(F->getLinkage(), Out);
1571 PrintVisibility(F->getVisibility(), Out);
Chris Lattner4ad02e72003-04-16 20:28:45 +00001572
Chris Lattnerd5118982005-05-06 20:26:43 +00001573 // Print the calling convention.
1574 switch (F->getCallingConv()) {
1575 case CallingConv::C: break; // default
Anton Korobeynikovf8248682006-09-20 22:03:51 +00001576 case CallingConv::Fast: Out << "fastcc "; break;
1577 case CallingConv::Cold: Out << "coldcc "; break;
1578 case CallingConv::X86_StdCall: Out << "x86_stdcallcc "; break;
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001579 case CallingConv::X86_FastCall: Out << "x86_fastcallcc "; break;
1580 case CallingConv::ARM_APCS: Out << "arm_apcscc "; break;
1581 case CallingConv::ARM_AAPCS: Out << "arm_aapcscc "; break;
1582 case CallingConv::ARM_AAPCS_VFP:Out << "arm_aapcs_vfpcc "; break;
Chris Lattnerd5118982005-05-06 20:26:43 +00001583 default: Out << "cc" << F->getCallingConv() << " "; break;
1584 }
1585
Reid Spencerbd5db8e2006-12-31 05:24:50 +00001586 const FunctionType *FT = F->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00001587 const AttrListPtr &Attrs = F->getAttributes();
Devang Patel652203f2008-09-29 20:49:50 +00001588 Attributes RetAttrs = Attrs.getRetAttributes();
1589 if (RetAttrs != Attribute::None)
1590 Out << Attribute::getAsString(Attrs.getRetAttributes()) << ' ';
Chris Lattner0f7364b2009-02-28 21:26:53 +00001591 TypePrinter.print(F->getReturnType(), Out);
Chris Lattner4667b712008-08-19 05:26:17 +00001592 Out << ' ';
Dan Gohmand6c0f652009-08-13 15:27:57 +00001593 WriteAsOperandInternal(Out, F, &TypePrinter, &Machine);
Misha Brukman0313e0b2004-06-21 21:53:56 +00001594 Out << '(';
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001595 Machine.incorporateFunction(F);
Chris Lattner007377f2001-09-07 16:36:04 +00001596
Chris Lattnerc1824992001-10-29 16:05:51 +00001597 // Loop over the arguments, printing them...
Chris Lattner007377f2001-09-07 16:36:04 +00001598
Reid Spencerbd5db8e2006-12-31 05:24:50 +00001599 unsigned Idx = 1;
Chris Lattner8dcd2f12007-04-18 00:57:22 +00001600 if (!F->isDeclaration()) {
1601 // If this isn't a declaration, print the argument names as well.
1602 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
1603 I != E; ++I) {
1604 // Insert commas as we go... the first arg doesn't get a comma
1605 if (I != F->arg_begin()) Out << ", ";
Devang Patel19c87462008-09-26 22:53:05 +00001606 printArgument(I, Attrs.getParamAttributes(Idx));
Chris Lattner8dcd2f12007-04-18 00:57:22 +00001607 Idx++;
1608 }
1609 } else {
1610 // Otherwise, print the types from the function type.
1611 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
1612 // Insert commas as we go... the first arg doesn't get a comma
1613 if (i) Out << ", ";
1614
1615 // Output type...
Chris Lattner0f7364b2009-02-28 21:26:53 +00001616 TypePrinter.print(FT->getParamType(i), Out);
Chris Lattner8dcd2f12007-04-18 00:57:22 +00001617
Devang Patel19c87462008-09-26 22:53:05 +00001618 Attributes ArgAttrs = Attrs.getParamAttributes(i+1);
Devang Patel05988662008-09-25 21:00:45 +00001619 if (ArgAttrs != Attribute::None)
1620 Out << ' ' << Attribute::getAsString(ArgAttrs);
Chris Lattner8dcd2f12007-04-18 00:57:22 +00001621 }
Reid Spencerbd5db8e2006-12-31 05:24:50 +00001622 }
Chris Lattner007377f2001-09-07 16:36:04 +00001623
1624 // Finish printing arguments...
Chris Lattner7e708292002-06-25 16:13:24 +00001625 if (FT->isVarArg()) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001626 if (FT->getNumParams()) Out << ", ";
1627 Out << "..."; // Output varargs portion of signature!
Chris Lattner007377f2001-09-07 16:36:04 +00001628 }
Misha Brukman0313e0b2004-06-21 21:53:56 +00001629 Out << ')';
Devang Patel19c87462008-09-26 22:53:05 +00001630 Attributes FnAttrs = Attrs.getFnAttributes();
1631 if (FnAttrs != Attribute::None)
1632 Out << ' ' << Attribute::getAsString(Attrs.getFnAttributes());
Chris Lattner60962db2005-11-12 00:10:19 +00001633 if (F->hasSection())
1634 Out << " section \"" << F->getSection() << '"';
Chris Lattner30caa282005-11-06 06:48:53 +00001635 if (F->getAlignment())
1636 Out << " align " << F->getAlignment();
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001637 if (F->hasGC())
1638 Out << " gc \"" << F->getGC() << '"';
Reid Spencer5cbf9852007-01-30 20:08:39 +00001639 if (F->isDeclaration()) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001640 Out << "\n";
Chris Lattner03e2acb2002-05-06 03:00:40 +00001641 } else {
Chris Lattnera1b58582008-04-21 06:12:55 +00001642 Out << " {";
Misha Brukmanfd939082005-04-21 23:48:37 +00001643
Chris Lattnerb5794002002-04-07 22:49:37 +00001644 // Output all of its basic blocks... for the function
Chris Lattner7e708292002-06-25 16:13:24 +00001645 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
1646 printBasicBlock(I);
Chris Lattner007377f2001-09-07 16:36:04 +00001647
Misha Brukman0313e0b2004-06-21 21:53:56 +00001648 Out << "}\n";
Chris Lattner007377f2001-09-07 16:36:04 +00001649 }
1650
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001651 Machine.purgeFunction();
Chris Lattner00950542001-06-06 20:29:01 +00001652}
1653
Misha Brukmanab5c6002004-03-02 00:22:19 +00001654/// printArgument - This member is called for every argument that is passed into
1655/// the function. Simply print it out
1656///
Dale Johannesen0d51e7e2008-02-19 21:38:47 +00001657void AssemblyWriter::printArgument(const Argument *Arg,
Devang Pateleaf42ab2008-09-23 23:03:40 +00001658 Attributes Attrs) {
Chris Lattner00950542001-06-06 20:29:01 +00001659 // Output type...
Chris Lattner0f7364b2009-02-28 21:26:53 +00001660 TypePrinter.print(Arg->getType(), Out);
Misha Brukmanfd939082005-04-21 23:48:37 +00001661
Duncan Sandsdc024672007-11-27 13:23:08 +00001662 // Output parameter attributes list
Devang Patel05988662008-09-25 21:00:45 +00001663 if (Attrs != Attribute::None)
1664 Out << ' ' << Attribute::getAsString(Attrs);
Reid Spencerbd5db8e2006-12-31 05:24:50 +00001665
Chris Lattner00950542001-06-06 20:29:01 +00001666 // Output name, if available...
Chris Lattnerc97536e2008-08-17 04:40:13 +00001667 if (Arg->hasName()) {
1668 Out << ' ';
1669 PrintLLVMName(Out, Arg);
1670 }
Chris Lattner00950542001-06-06 20:29:01 +00001671}
1672
Misha Brukmanab5c6002004-03-02 00:22:19 +00001673/// printBasicBlock - This member is called for each basic block in a method.
1674///
Chris Lattnerc1824992001-10-29 16:05:51 +00001675void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Nick Lewycky280a6e62008-04-25 16:53:59 +00001676 if (BB->hasName()) { // Print out the label if it exists...
Chris Lattnerc97536e2008-08-17 04:40:13 +00001677 Out << "\n";
Daniel Dunbar03d76512009-07-25 23:55:21 +00001678 PrintLLVMName(Out, BB->getName(), LabelPrefix);
Chris Lattnerc97536e2008-08-17 04:40:13 +00001679 Out << ':';
Nick Lewycky280a6e62008-04-25 16:53:59 +00001680 } else if (!BB->use_empty()) { // Don't print block # of no uses...
Chris Lattner96c5b2f2008-04-21 04:20:33 +00001681 Out << "\n; <label>:";
Chris Lattner22379bc2007-01-11 03:54:27 +00001682 int Slot = Machine.getLocalSlot(BB);
Chris Lattner69566452004-06-09 19:41:19 +00001683 if (Slot != -1)
Misha Brukman0313e0b2004-06-21 21:53:56 +00001684 Out << Slot;
Chris Lattner69566452004-06-09 19:41:19 +00001685 else
Misha Brukman0313e0b2004-06-21 21:53:56 +00001686 Out << "<badref>";
Chris Lattner061269b2002-10-02 19:38:55 +00001687 }
Chris Lattner4e4d8622003-11-20 00:09:43 +00001688
Dan Gohman683e9222009-08-12 17:23:50 +00001689 if (BB->getParent() == 0) {
1690 Out.PadToColumn(50);
1691 Out << "; Error: Block without parent!";
1692 } else if (BB != &BB->getParent()->getEntryBlock()) { // Not the entry block?
Chris Lattnereb411292008-04-22 02:45:44 +00001693 // Output predecessors for the block...
Dan Gohman683e9222009-08-12 17:23:50 +00001694 Out.PadToColumn(50);
1695 Out << ";";
Chris Lattnereb411292008-04-22 02:45:44 +00001696 pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
1697
1698 if (PI == PE) {
1699 Out << " No predecessors!";
1700 } else {
Dan Gohman8dae1382008-09-14 17:21:12 +00001701 Out << " preds = ";
Chris Lattnereb411292008-04-22 02:45:44 +00001702 writeOperand(*PI, false);
1703 for (++PI; PI != PE; ++PI) {
Dan Gohman8dae1382008-09-14 17:21:12 +00001704 Out << ", ";
Chris Lattner2fcfdb72006-12-06 06:24:27 +00001705 writeOperand(*PI, false);
Chris Lattner40efcec2003-11-16 22:59:57 +00001706 }
Chris Lattner061269b2002-10-02 19:38:55 +00001707 }
Chris Lattner00950542001-06-06 20:29:01 +00001708 }
Misha Brukmanfd939082005-04-21 23:48:37 +00001709
Chris Lattnereb411292008-04-22 02:45:44 +00001710 Out << "\n";
Chris Lattner00950542001-06-06 20:29:01 +00001711
Misha Brukman0313e0b2004-06-21 21:53:56 +00001712 if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, Out);
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001713
Chris Lattner007377f2001-09-07 16:36:04 +00001714 // Output all of the instructions in the basic block...
Dan Gohmanbeca6892009-07-13 18:27:59 +00001715 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
Chris Lattner7e708292002-06-25 16:13:24 +00001716 printInstruction(*I);
Dan Gohmanbeca6892009-07-13 18:27:59 +00001717 Out << '\n';
1718 }
Chris Lattner9f717ef2004-03-08 18:51:45 +00001719
Misha Brukman0313e0b2004-06-21 21:53:56 +00001720 if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, Out);
Chris Lattner00950542001-06-06 20:29:01 +00001721}
1722
Chris Lattnere02fa852001-10-13 06:42:36 +00001723
Misha Brukmanab5c6002004-03-02 00:22:19 +00001724/// printInfoComment - Print a little comment after the instruction indicating
1725/// which slot it occupies.
1726///
Chris Lattner7e708292002-06-25 16:13:24 +00001727void AssemblyWriter::printInfoComment(const Value &V) {
1728 if (V.getType() != Type::VoidTy) {
Dan Gohman683e9222009-08-12 17:23:50 +00001729 Out.PadToColumn(50);
1730 Out << "; <";
Chris Lattner0f7364b2009-02-28 21:26:53 +00001731 TypePrinter.print(V.getType(), Out);
Dan Gohman9bf0b9b2009-08-12 23:54:22 +00001732 Out << "> [#uses=" << V.getNumUses() << ']'; // Output # uses
Chris Lattnere02fa852001-10-13 06:42:36 +00001733 }
1734}
1735
Reid Spencer3a9ec242006-08-28 01:02:49 +00001736// This member is called for each Instruction in a function..
Chris Lattner7e708292002-06-25 16:13:24 +00001737void AssemblyWriter::printInstruction(const Instruction &I) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001738 if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, Out);
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001739
Dan Gohman3845e502009-08-12 23:32:33 +00001740 // Print out indentation for an instruction.
Dan Gohman01889ca2009-08-13 01:41:52 +00001741 Out << " ";
Chris Lattner00950542001-06-06 20:29:01 +00001742
1743 // Print out name if it exists...
Chris Lattnerc97536e2008-08-17 04:40:13 +00001744 if (I.hasName()) {
1745 PrintLLVMName(Out, &I);
1746 Out << " = ";
Chris Lattner828db8a2008-08-29 17:19:30 +00001747 } else if (I.getType() != Type::VoidTy) {
1748 // Print out the def slot taken.
1749 int SlotNum = Machine.getLocalSlot(&I);
1750 if (SlotNum == -1)
1751 Out << "<badref> = ";
1752 else
1753 Out << '%' << SlotNum << " = ";
Chris Lattnerc97536e2008-08-17 04:40:13 +00001754 }
Chris Lattner00950542001-06-06 20:29:01 +00001755
Chris Lattnerddb6db42005-05-06 05:51:46 +00001756 // If this is a volatile load or store, print out the volatile marker.
Chris Lattnere5e475e2003-09-08 17:45:59 +00001757 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) ||
Chris Lattnerddb6db42005-05-06 05:51:46 +00001758 (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile())) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001759 Out << "volatile ";
Chris Lattnerddb6db42005-05-06 05:51:46 +00001760 } else if (isa<CallInst>(I) && cast<CallInst>(I).isTailCall()) {
1761 // If this is a call, check if it's a tail call.
1762 Out << "tail ";
1763 }
Chris Lattnere5e475e2003-09-08 17:45:59 +00001764
Chris Lattner00950542001-06-06 20:29:01 +00001765 // Print out the opcode...
Misha Brukman0313e0b2004-06-21 21:53:56 +00001766 Out << I.getOpcodeName();
Chris Lattner00950542001-06-06 20:29:01 +00001767
Dan Gohman59858cf2009-07-27 16:11:46 +00001768 // Print out optimization information.
1769 WriteOptimizationInfo(Out, &I);
1770
Reid Spencer74f16422006-12-03 06:27:29 +00001771 // Print out the compare instruction predicates
Nate Begemanac80ade2008-05-12 19:01:56 +00001772 if (const CmpInst *CI = dyn_cast<CmpInst>(&I))
Chris Lattnerab49ee72008-08-23 22:52:27 +00001773 Out << ' ' << getPredicateText(CI->getPredicate());
Reid Spencer74f16422006-12-03 06:27:29 +00001774
Chris Lattner00950542001-06-06 20:29:01 +00001775 // Print out the type of the operands...
Chris Lattner7e708292002-06-25 16:13:24 +00001776 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0;
Chris Lattner00950542001-06-06 20:29:01 +00001777
1778 // Special case conditional branches to swizzle the condition out to the front
Gabor Greifccd27fb2009-02-09 15:45:06 +00001779 if (isa<BranchInst>(I) && cast<BranchInst>(I).isConditional()) {
1780 BranchInst &BI(cast<BranchInst>(I));
Dan Gohman8dae1382008-09-14 17:21:12 +00001781 Out << ' ';
Gabor Greifccd27fb2009-02-09 15:45:06 +00001782 writeOperand(BI.getCondition(), true);
Dan Gohman8dae1382008-09-14 17:21:12 +00001783 Out << ", ";
Gabor Greifccd27fb2009-02-09 15:45:06 +00001784 writeOperand(BI.getSuccessor(0), true);
Dan Gohman8dae1382008-09-14 17:21:12 +00001785 Out << ", ";
Gabor Greifccd27fb2009-02-09 15:45:06 +00001786 writeOperand(BI.getSuccessor(1), true);
Chris Lattner00950542001-06-06 20:29:01 +00001787
Chris Lattner94dc1f22002-04-13 18:34:38 +00001788 } else if (isa<SwitchInst>(I)) {
Chris Lattner00950542001-06-06 20:29:01 +00001789 // Special case switch statement to get formatting nice and correct...
Dan Gohman8dae1382008-09-14 17:21:12 +00001790 Out << ' ';
Chris Lattnerab49ee72008-08-23 22:52:27 +00001791 writeOperand(Operand , true);
Dan Gohman8dae1382008-09-14 17:21:12 +00001792 Out << ", ";
Chris Lattnerab49ee72008-08-23 22:52:27 +00001793 writeOperand(I.getOperand(1), true);
1794 Out << " [";
Chris Lattner00950542001-06-06 20:29:01 +00001795
Chris Lattner7e708292002-06-25 16:13:24 +00001796 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; op += 2) {
Dan Gohman01889ca2009-08-13 01:41:52 +00001797 Out << "\n ";
Chris Lattnerab49ee72008-08-23 22:52:27 +00001798 writeOperand(I.getOperand(op ), true);
Dan Gohman8dae1382008-09-14 17:21:12 +00001799 Out << ", ";
Chris Lattner7e708292002-06-25 16:13:24 +00001800 writeOperand(I.getOperand(op+1), true);
Chris Lattner00950542001-06-06 20:29:01 +00001801 }
Dan Gohman01889ca2009-08-13 01:41:52 +00001802 Out << "\n ]";
Chris Lattnerb00c5822001-10-02 03:41:24 +00001803 } else if (isa<PHINode>(I)) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001804 Out << ' ';
Chris Lattner0f7364b2009-02-28 21:26:53 +00001805 TypePrinter.print(I.getType(), Out);
Misha Brukman0313e0b2004-06-21 21:53:56 +00001806 Out << ' ';
Chris Lattner00950542001-06-06 20:29:01 +00001807
Chris Lattner7e708292002-06-25 16:13:24 +00001808 for (unsigned op = 0, Eop = I.getNumOperands(); op < Eop; op += 2) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001809 if (op) Out << ", ";
Dan Gohman8dae1382008-09-14 17:21:12 +00001810 Out << "[ ";
1811 writeOperand(I.getOperand(op ), false); Out << ", ";
Misha Brukman0313e0b2004-06-21 21:53:56 +00001812 writeOperand(I.getOperand(op+1), false); Out << " ]";
Chris Lattnerc24d2082001-06-11 15:04:20 +00001813 }
Dan Gohman995be7d2008-05-31 19:12:39 +00001814 } else if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(&I)) {
Dan Gohman8dae1382008-09-14 17:21:12 +00001815 Out << ' ';
Dan Gohman995be7d2008-05-31 19:12:39 +00001816 writeOperand(I.getOperand(0), true);
1817 for (const unsigned *i = EVI->idx_begin(), *e = EVI->idx_end(); i != e; ++i)
1818 Out << ", " << *i;
1819 } else if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(&I)) {
Dan Gohman8dae1382008-09-14 17:21:12 +00001820 Out << ' ';
1821 writeOperand(I.getOperand(0), true); Out << ", ";
Dan Gohman995be7d2008-05-31 19:12:39 +00001822 writeOperand(I.getOperand(1), true);
1823 for (const unsigned *i = IVI->idx_begin(), *e = IVI->idx_end(); i != e; ++i)
1824 Out << ", " << *i;
Devang Patel57ef4f42008-02-23 00:35:18 +00001825 } else if (isa<ReturnInst>(I) && !Operand) {
1826 Out << " void";
Chris Lattnerd5118982005-05-06 20:26:43 +00001827 } else if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
1828 // Print the calling convention being used.
1829 switch (CI->getCallingConv()) {
1830 case CallingConv::C: break; // default
Chris Lattner0deaab82006-05-19 21:58:52 +00001831 case CallingConv::Fast: Out << " fastcc"; break;
1832 case CallingConv::Cold: Out << " coldcc"; break;
Chris Lattnerb28a6dc2007-11-18 18:32:16 +00001833 case CallingConv::X86_StdCall: Out << " x86_stdcallcc"; break;
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001834 case CallingConv::X86_FastCall: Out << " x86_fastcallcc"; break;
1835 case CallingConv::ARM_APCS: Out << " arm_apcscc "; break;
1836 case CallingConv::ARM_AAPCS: Out << " arm_aapcscc "; break;
1837 case CallingConv::ARM_AAPCS_VFP:Out << " arm_aapcs_vfpcc "; break;
Chris Lattnerd5118982005-05-06 20:26:43 +00001838 default: Out << " cc" << CI->getCallingConv(); break;
1839 }
1840
Reid Spencerb138a062007-04-09 06:10:42 +00001841 const PointerType *PTy = cast<PointerType>(Operand->getType());
1842 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1843 const Type *RetTy = FTy->getReturnType();
Devang Patel05988662008-09-25 21:00:45 +00001844 const AttrListPtr &PAL = CI->getAttributes();
Chris Lattner268de042001-11-06 21:28:12 +00001845
Devang Patel652203f2008-09-29 20:49:50 +00001846 if (PAL.getRetAttributes() != Attribute::None)
1847 Out << ' ' << Attribute::getAsString(PAL.getRetAttributes());
1848
Chris Lattner7a012292003-08-05 15:34:45 +00001849 // If possible, print out the short form of the call instruction. We can
Chris Lattnerb5794002002-04-07 22:49:37 +00001850 // only do this if the first argument is a pointer to a nonvararg function,
Chris Lattner7a012292003-08-05 15:34:45 +00001851 // and if the return type is not a pointer to a function.
Chris Lattner268de042001-11-06 21:28:12 +00001852 //
Dan Gohman8dae1382008-09-14 17:21:12 +00001853 Out << ' ';
Chris Lattner7a012292003-08-05 15:34:45 +00001854 if (!FTy->isVarArg() &&
Misha Brukmanfd939082005-04-21 23:48:37 +00001855 (!isa<PointerType>(RetTy) ||
Chris Lattnerc1b27182002-07-25 20:58:51 +00001856 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Chris Lattner0f7364b2009-02-28 21:26:53 +00001857 TypePrinter.print(RetTy, Out);
Dan Gohman8dae1382008-09-14 17:21:12 +00001858 Out << ' ';
Chris Lattner268de042001-11-06 21:28:12 +00001859 writeOperand(Operand, false);
1860 } else {
1861 writeOperand(Operand, true);
1862 }
Misha Brukman0313e0b2004-06-21 21:53:56 +00001863 Out << '(';
Reid Spencerbd5db8e2006-12-31 05:24:50 +00001864 for (unsigned op = 1, Eop = I.getNumOperands(); op < Eop; ++op) {
1865 if (op > 1)
Dan Gohman8dae1382008-09-14 17:21:12 +00001866 Out << ", ";
Devang Patel19c87462008-09-26 22:53:05 +00001867 writeParamOperand(I.getOperand(op), PAL.getParamAttributes(op));
Chris Lattner00950542001-06-06 20:29:01 +00001868 }
Dan Gohman8dae1382008-09-14 17:21:12 +00001869 Out << ')';
Devang Patel19c87462008-09-26 22:53:05 +00001870 if (PAL.getFnAttributes() != Attribute::None)
1871 Out << ' ' << Attribute::getAsString(PAL.getFnAttributes());
Chris Lattner7e708292002-06-25 16:13:24 +00001872 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
Reid Spencerb138a062007-04-09 06:10:42 +00001873 const PointerType *PTy = cast<PointerType>(Operand->getType());
1874 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1875 const Type *RetTy = FTy->getReturnType();
Devang Patel05988662008-09-25 21:00:45 +00001876 const AttrListPtr &PAL = II->getAttributes();
Chris Lattner7a012292003-08-05 15:34:45 +00001877
Chris Lattnerd5118982005-05-06 20:26:43 +00001878 // Print the calling convention being used.
1879 switch (II->getCallingConv()) {
1880 case CallingConv::C: break; // default
Chris Lattner0deaab82006-05-19 21:58:52 +00001881 case CallingConv::Fast: Out << " fastcc"; break;
1882 case CallingConv::Cold: Out << " coldcc"; break;
Dan Gohman8dae1382008-09-14 17:21:12 +00001883 case CallingConv::X86_StdCall: Out << " x86_stdcallcc"; break;
1884 case CallingConv::X86_FastCall: Out << " x86_fastcallcc"; break;
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001885 case CallingConv::ARM_APCS: Out << " arm_apcscc "; break;
1886 case CallingConv::ARM_AAPCS: Out << " arm_aapcscc "; break;
1887 case CallingConv::ARM_AAPCS_VFP:Out << " arm_aapcs_vfpcc "; break;
Chris Lattnerd5118982005-05-06 20:26:43 +00001888 default: Out << " cc" << II->getCallingConv(); break;
1889 }
1890
Devang Patel652203f2008-09-29 20:49:50 +00001891 if (PAL.getRetAttributes() != Attribute::None)
1892 Out << ' ' << Attribute::getAsString(PAL.getRetAttributes());
1893
Chris Lattner7a012292003-08-05 15:34:45 +00001894 // If possible, print out the short form of the invoke instruction. We can
1895 // only do this if the first argument is a pointer to a nonvararg function,
1896 // and if the return type is not a pointer to a function.
1897 //
Dan Gohman2b6c3d92008-10-15 18:02:08 +00001898 Out << ' ';
Chris Lattner7a012292003-08-05 15:34:45 +00001899 if (!FTy->isVarArg() &&
Misha Brukmanfd939082005-04-21 23:48:37 +00001900 (!isa<PointerType>(RetTy) ||
Chris Lattner7a012292003-08-05 15:34:45 +00001901 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Chris Lattner0f7364b2009-02-28 21:26:53 +00001902 TypePrinter.print(RetTy, Out);
Dan Gohman2b6c3d92008-10-15 18:02:08 +00001903 Out << ' ';
Chris Lattner7a012292003-08-05 15:34:45 +00001904 writeOperand(Operand, false);
1905 } else {
1906 writeOperand(Operand, true);
1907 }
Misha Brukman0313e0b2004-06-21 21:53:56 +00001908 Out << '(';
Bill Wendling9a507cd2009-03-13 21:15:59 +00001909 for (unsigned op = 3, Eop = I.getNumOperands(); op < Eop; ++op) {
1910 if (op > 3)
Dan Gohman8dae1382008-09-14 17:21:12 +00001911 Out << ", ";
Bill Wendling9a507cd2009-03-13 21:15:59 +00001912 writeParamOperand(I.getOperand(op), PAL.getParamAttributes(op-2));
Chris Lattnere02fa852001-10-13 06:42:36 +00001913 }
1914
Dan Gohman8dae1382008-09-14 17:21:12 +00001915 Out << ')';
Devang Patel19c87462008-09-26 22:53:05 +00001916 if (PAL.getFnAttributes() != Attribute::None)
1917 Out << ' ' << Attribute::getAsString(PAL.getFnAttributes());
1918
Dan Gohman01889ca2009-08-13 01:41:52 +00001919 Out << "\n to ";
Chris Lattnere02fa852001-10-13 06:42:36 +00001920 writeOperand(II->getNormalDest(), true);
Dan Gohman8dae1382008-09-14 17:21:12 +00001921 Out << " unwind ";
Chris Lattneraeb2a1d2004-02-08 21:44:31 +00001922 writeOperand(II->getUnwindDest(), true);
Chris Lattnere02fa852001-10-13 06:42:36 +00001923
Chris Lattner7e708292002-06-25 16:13:24 +00001924 } else if (const AllocationInst *AI = dyn_cast<AllocationInst>(&I)) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001925 Out << ' ';
Chris Lattner0f7364b2009-02-28 21:26:53 +00001926 TypePrinter.print(AI->getType()->getElementType(), Out);
Dan Gohman69bff072009-07-31 18:23:24 +00001927 if (!AI->getArraySize() || AI->isArrayAllocation()) {
Dan Gohman8dae1382008-09-14 17:21:12 +00001928 Out << ", ";
Chris Lattner94dc1f22002-04-13 18:34:38 +00001929 writeOperand(AI->getArraySize(), true);
Chris Lattner00950542001-06-06 20:29:01 +00001930 }
Nate Begeman14b05292005-11-05 09:21:28 +00001931 if (AI->getAlignment()) {
Chris Lattner9fad0b92005-11-05 21:20:34 +00001932 Out << ", align " << AI->getAlignment();
Nate Begeman14b05292005-11-05 09:21:28 +00001933 }
Chris Lattnere02fa852001-10-13 06:42:36 +00001934 } else if (isa<CastInst>(I)) {
Dan Gohman8dae1382008-09-14 17:21:12 +00001935 if (Operand) {
1936 Out << ' ';
1937 writeOperand(Operand, true); // Work with broken code
1938 }
Misha Brukman0313e0b2004-06-21 21:53:56 +00001939 Out << " to ";
Chris Lattner0f7364b2009-02-28 21:26:53 +00001940 TypePrinter.print(I.getType(), Out);
Chris Lattner4d45bd02003-10-18 05:57:43 +00001941 } else if (isa<VAArgInst>(I)) {
Dan Gohman8dae1382008-09-14 17:21:12 +00001942 if (Operand) {
1943 Out << ' ';
1944 writeOperand(Operand, true); // Work with broken code
1945 }
Misha Brukman0313e0b2004-06-21 21:53:56 +00001946 Out << ", ";
Chris Lattner0f7364b2009-02-28 21:26:53 +00001947 TypePrinter.print(I.getType(), Out);
1948 } else if (Operand) { // Print the normal way.
Chris Lattner00950542001-06-06 20:29:01 +00001949
Misha Brukmanfd939082005-04-21 23:48:37 +00001950 // PrintAllTypes - Instructions who have operands of all the same type
Chris Lattner00950542001-06-06 20:29:01 +00001951 // omit the type from all but the first operand. If the instruction has
1952 // different type operands (for example br), then they are all printed.
1953 bool PrintAllTypes = false;
1954 const Type *TheType = Operand->getType();
Chris Lattner00950542001-06-06 20:29:01 +00001955
Reid Spencerebe57e32007-02-02 13:54:55 +00001956 // Select, Store and ShuffleVector always print all types.
Devang Patel64947682008-03-04 22:05:14 +00001957 if (isa<SelectInst>(I) || isa<StoreInst>(I) || isa<ShuffleVectorInst>(I)
1958 || isa<ReturnInst>(I)) {
Chris Lattnerffd9bf42003-04-16 20:20:02 +00001959 PrintAllTypes = true;
1960 } else {
1961 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
1962 Operand = I.getOperand(i);
Nuno Lopes6ad2b2a2009-01-15 18:40:57 +00001963 // note that Operand shouldn't be null, but the test helps make dump()
1964 // more tolerant of malformed IR
Nuno Lopesa8c78a92009-01-14 17:51:41 +00001965 if (Operand && Operand->getType() != TheType) {
Chris Lattnerffd9bf42003-04-16 20:20:02 +00001966 PrintAllTypes = true; // We have differing types! Print them all!
1967 break;
1968 }
Chris Lattner00950542001-06-06 20:29:01 +00001969 }
1970 }
Misha Brukmanfd939082005-04-21 23:48:37 +00001971
Chris Lattnerc1824992001-10-29 16:05:51 +00001972 if (!PrintAllTypes) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001973 Out << ' ';
Chris Lattner0f7364b2009-02-28 21:26:53 +00001974 TypePrinter.print(TheType, Out);
Chris Lattnerc1824992001-10-29 16:05:51 +00001975 }
Chris Lattner00950542001-06-06 20:29:01 +00001976
Dan Gohman8dae1382008-09-14 17:21:12 +00001977 Out << ' ';
Chris Lattner7e708292002-06-25 16:13:24 +00001978 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
Dan Gohman8dae1382008-09-14 17:21:12 +00001979 if (i) Out << ", ";
Chris Lattner7e708292002-06-25 16:13:24 +00001980 writeOperand(I.getOperand(i), PrintAllTypes);
Chris Lattner00950542001-06-06 20:29:01 +00001981 }
1982 }
Christopher Lamb43c7f372007-04-22 19:24:39 +00001983
1984 // Print post operand alignment for load/store
1985 if (isa<LoadInst>(I) && cast<LoadInst>(I).getAlignment()) {
1986 Out << ", align " << cast<LoadInst>(I).getAlignment();
1987 } else if (isa<StoreInst>(I) && cast<StoreInst>(I).getAlignment()) {
1988 Out << ", align " << cast<StoreInst>(I).getAlignment();
1989 }
Chris Lattner00950542001-06-06 20:29:01 +00001990
Chris Lattnere02fa852001-10-13 06:42:36 +00001991 printInfoComment(I);
Chris Lattner00950542001-06-06 20:29:01 +00001992}
1993
1994
1995//===----------------------------------------------------------------------===//
1996// External Interface declarations
1997//===----------------------------------------------------------------------===//
1998
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001999void Module::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Chris Lattner944fac72008-08-23 22:23:09 +00002000 raw_os_ostream OS(o);
2001 print(OS, AAW);
2002}
Dan Gohman683e9222009-08-12 17:23:50 +00002003void Module::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW) const {
Chris Lattner0d9574a2008-08-19 04:26:57 +00002004 SlotTracker SlotTable(this);
Dan Gohman1220e102009-08-12 20:56:03 +00002005 size_t OldBufferSize = ROS.GetBufferSize();
Dan Gohman683e9222009-08-12 17:23:50 +00002006 formatted_raw_ostream OS(ROS);
Chris Lattner944fac72008-08-23 22:23:09 +00002007 AssemblyWriter W(OS, SlotTable, this, AAW);
Chris Lattner75cf7cf2002-04-08 22:03:40 +00002008 W.write(this);
Dan Gohman1220e102009-08-12 20:56:03 +00002009 // formatted_raw_ostream forces the underlying raw_ostream to be
2010 // unbuffered. Reset it to its original buffer size.
2011 if (OldBufferSize != 0)
2012 ROS.SetBufferSize(OldBufferSize);
Chris Lattner00950542001-06-06 20:29:01 +00002013}
2014
Misha Brukmanfd939082005-04-21 23:48:37 +00002015void Type::print(std::ostream &o) const {
Chris Lattner944fac72008-08-23 22:23:09 +00002016 raw_os_ostream OS(o);
2017 print(OS);
2018}
2019
Chris Lattner6d4306e2009-02-28 21:11:05 +00002020void Type::print(raw_ostream &OS) const {
2021 if (this == 0) {
2022 OS << "<null Type>";
2023 return;
2024 }
Chris Lattnere9fa33e2009-02-28 23:20:19 +00002025 TypePrinting().print(this, OS);
Chris Lattner75cf7cf2002-04-08 22:03:40 +00002026}
2027
Dan Gohman683e9222009-08-12 17:23:50 +00002028void Value::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW) const {
Chris Lattner944fac72008-08-23 22:23:09 +00002029 if (this == 0) {
Dan Gohman1220e102009-08-12 20:56:03 +00002030 ROS << "printing a <null> value\n";
Chris Lattner944fac72008-08-23 22:23:09 +00002031 return;
2032 }
Dan Gohman1220e102009-08-12 20:56:03 +00002033 size_t OldBufferSize = ROS.GetBufferSize();
2034 formatted_raw_ostream OS(ROS);
Chris Lattner944fac72008-08-23 22:23:09 +00002035 if (const Instruction *I = dyn_cast<Instruction>(this)) {
2036 const Function *F = I->getParent() ? I->getParent()->getParent() : 0;
2037 SlotTracker SlotTable(F);
2038 AssemblyWriter W(OS, SlotTable, F ? F->getParent() : 0, AAW);
2039 W.write(I);
2040 } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(this)) {
2041 SlotTracker SlotTable(BB->getParent());
2042 AssemblyWriter W(OS, SlotTable,
2043 BB->getParent() ? BB->getParent()->getParent() : 0, AAW);
2044 W.write(BB);
2045 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(this)) {
2046 SlotTracker SlotTable(GV->getParent());
Dan Gohmanba0941f2009-04-20 16:10:33 +00002047 AssemblyWriter W(OS, SlotTable, GV->getParent(), AAW);
Chris Lattner944fac72008-08-23 22:23:09 +00002048 W.write(GV);
Devang Patele54abc92009-07-22 17:43:22 +00002049 } else if (const MDString *MDS = dyn_cast<MDString>(this)) {
2050 TypePrinting TypePrinter;
2051 TypePrinter.print(MDS->getType(), OS);
2052 OS << ' ';
2053 OS << "!\"";
Daniel Dunbar03d76512009-07-25 23:55:21 +00002054 PrintEscapedString(MDS->getString(), OS);
Devang Patele54abc92009-07-22 17:43:22 +00002055 OS << '"';
Devang Patelfcd65ae2009-07-01 20:59:15 +00002056 } else if (const MDNode *N = dyn_cast<MDNode>(this)) {
Devang Patel320671d2009-07-08 21:44:25 +00002057 SlotTracker SlotTable(N);
Devang Patelfcd65ae2009-07-01 20:59:15 +00002058 TypePrinting TypePrinter;
Devang Patel320671d2009-07-08 21:44:25 +00002059 SlotTable.initialize();
2060 WriteMDNodes(OS, TypePrinter, SlotTable);
Devang Patelc29d5b32009-07-30 00:02:57 +00002061 } else if (const NamedMDNode *N = dyn_cast<NamedMDNode>(this)) {
2062 SlotTracker SlotTable(N);
2063 TypePrinting TypePrinter;
2064 SlotTable.initialize();
2065 OS << "!" << N->getName() << " = !{";
2066 for (unsigned i = 0, e = N->getNumElements(); i != e; ++i) {
2067 if (i) OS << ", ";
Devang Patele8861b82009-07-30 01:02:04 +00002068 MDNode *MD = dyn_cast_or_null<MDNode>(N->getElement(i));
2069 if (MD)
2070 OS << '!' << SlotTable.getMetadataSlot(MD);
2071 else
2072 OS << "null";
Devang Patelc29d5b32009-07-30 00:02:57 +00002073 }
2074 OS << "}\n";
2075 WriteMDNodes(OS, TypePrinter, SlotTable);
Chris Lattner944fac72008-08-23 22:23:09 +00002076 } else if (const Constant *C = dyn_cast<Constant>(this)) {
Chris Lattnere9fa33e2009-02-28 23:20:19 +00002077 TypePrinting TypePrinter;
Chris Lattner0f7364b2009-02-28 21:26:53 +00002078 TypePrinter.print(C->getType(), OS);
Chris Lattner6d4306e2009-02-28 21:11:05 +00002079 OS << ' ';
Chris Lattner9cc34462009-02-28 20:25:14 +00002080 WriteConstantInt(OS, C, TypePrinter, 0);
Chris Lattner944fac72008-08-23 22:23:09 +00002081 } else if (const Argument *A = dyn_cast<Argument>(this)) {
2082 WriteAsOperand(OS, this, true,
2083 A->getParent() ? A->getParent()->getParent() : 0);
2084 } else if (isa<InlineAsm>(this)) {
2085 WriteAsOperand(OS, this, true, 0);
2086 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +00002087 llvm_unreachable("Unknown value to print out!");
Chris Lattner944fac72008-08-23 22:23:09 +00002088 }
Dan Gohman1220e102009-08-12 20:56:03 +00002089 // formatted_raw_ostream forces the underlying raw_ostream to be
2090 // unbuffered. Reset it to its original buffer size.
2091 if (OldBufferSize != 0)
2092 ROS.SetBufferSize(OldBufferSize);
Chris Lattner944fac72008-08-23 22:23:09 +00002093}
2094
2095void Value::print(std::ostream &O, AssemblyAnnotationWriter *AAW) const {
2096 raw_os_ostream OS(O);
2097 print(OS, AAW);
Chris Lattner75cf7cf2002-04-08 22:03:40 +00002098}
2099
Chris Lattner7059e532008-08-25 17:03:15 +00002100// Value::dump - allow easy printing of Values from the debugger.
Dan Gohmanf871ccb2009-03-23 15:57:19 +00002101void Value::dump() const { print(errs()); errs() << '\n'; }
Reid Spencerfa452c02004-05-25 18:14:38 +00002102
Chris Lattner7059e532008-08-25 17:03:15 +00002103// Type::dump - allow easy printing of Types from the debugger.
Chris Lattner795daec2008-10-01 20:16:19 +00002104// This one uses type names from the given context module
2105void Type::dump(const Module *Context) const {
2106 WriteTypeSymbolic(errs(), this, Context);
2107 errs() << '\n';
Chris Lattner795daec2008-10-01 20:16:19 +00002108}
2109
Chris Lattnerc2871372009-02-28 21:05:51 +00002110// Type::dump - allow easy printing of Types from the debugger.
2111void Type::dump() const { dump(0); }
2112
Chris Lattner7059e532008-08-25 17:03:15 +00002113// Module::dump() - Allow printing of Modules from the debugger.
Dan Gohmanf871ccb2009-03-23 15:57:19 +00002114void Module::dump() const { print(errs(), 0); }