blob: bfdb0489b5fedacd20b405a7d8aeb39e4966054f [file] [log] [blame]
Chris Lattnerf7e79482002-04-07 22:31:46 +00001//===-- AsmWriter.cpp - Printing LLVM as an assembly file -----------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
10// This library implements the functionality defined in llvm/Assembly/Writer.h
11//
Chris Lattner189088e2002-04-12 18:21:53 +000012// Note that these routines must be extremely tolerant of various errors in the
Chris Lattnerf70da102003-05-08 02:44:12 +000013// LLVM code, because it can be used for debugging transformations.
Chris Lattner189088e2002-04-12 18:21:53 +000014//
Chris Lattner2f7c9632001-06-06 20:29:01 +000015//===----------------------------------------------------------------------===//
16
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +000017#include "llvm/Assembly/Writer.h"
Chris Lattner7f8845a2002-07-23 18:07:49 +000018#include "llvm/Assembly/PrintModulePass.h"
Chris Lattner8339f7d2003-10-30 23:41:03 +000019#include "llvm/Assembly/AsmAnnotationWriter.h"
Chris Lattnerf7b6d312005-05-06 20:26:43 +000020#include "llvm/CallingConv.h"
Chris Lattnerc70b3f62004-01-20 19:50:34 +000021#include "llvm/Constants.h"
Chris Lattner913d18f2002-04-29 18:46:50 +000022#include "llvm/DerivedTypes.h"
Chris Lattner8bbcda22006-01-25 18:57:27 +000023#include "llvm/InlineAsm.h"
Vikram S. Adveb952b542002-07-14 23:14:45 +000024#include "llvm/Instruction.h"
Misha Brukman2d3fa9e2004-07-29 16:53:53 +000025#include "llvm/Instructions.h"
Dan Gohman0ebd6962009-07-20 21:19:07 +000026#include "llvm/Operator.h"
Devang Patela4f43fb2009-07-28 21:49:47 +000027#include "llvm/Metadata.h"
Chris Lattnerc70b3f62004-01-20 19:50:34 +000028#include "llvm/Module.h"
Reid Spencer3aaaa0b2007-02-05 20:47:22 +000029#include "llvm/ValueSymbolTable.h"
Reid Spencer32af9e82007-01-06 07:24:44 +000030#include "llvm/TypeSymbolTable.h"
Chris Lattner3243ea12009-03-01 00:03:38 +000031#include "llvm/ADT/DenseSet.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000032#include "llvm/ADT/StringExtras.h"
33#include "llvm/ADT/STLExtras.h"
Bill Wendlingdfc91892006-11-28 02:09:03 +000034#include "llvm/Support/CFG.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000035#include "llvm/Support/ErrorHandling.h"
Jim Laskeyb74c6662005-08-17 19:34:49 +000036#include "llvm/Support/MathExtras.h"
Chris Lattner393b7cd2008-08-17 04:17:45 +000037#include "llvm/Support/raw_ostream.h"
Chris Lattnerfee714f2001-09-07 16:36:04 +000038#include <algorithm>
Reid Spencerbdf03b42007-05-22 19:27:35 +000039#include <cctype>
Devang Patel39e64d42009-07-01 19:21:12 +000040#include <map>
Chris Lattner189d19f2003-11-21 20:23:48 +000041using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000042
Reid Spencer294715b2005-05-15 16:13:11 +000043// Make virtual table appear in this compilation unit.
44AssemblyAnnotationWriter::~AssemblyAnnotationWriter() {}
45
Chris Lattner3eee99c2008-08-19 04:36:02 +000046//===----------------------------------------------------------------------===//
47// Helper Functions
48//===----------------------------------------------------------------------===//
49
50static const Module *getModuleFromVal(const Value *V) {
51 if (const Argument *MA = dyn_cast<Argument>(V))
52 return MA->getParent() ? MA->getParent()->getParent() : 0;
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 Dunbardb0b70a2008-10-28 19:33:02 +000067// PrintEscapedString - Print each character of the specified string, escaping
68// it if it is not printable or if it is an escape char.
Daniel Dunbare03eecb2009-07-25 23:55:21 +000069static void PrintEscapedString(const StringRef &Name, raw_ostream &Out) {
70 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
71 unsigned char C = Name[i];
Nick Lewycky5aa592a2009-03-15 06:39:52 +000072 if (isprint(C) && C != '\\' && C != '"')
Daniel Dunbardb0b70a2008-10-28 19:33:02 +000073 Out << C;
74 else
75 Out << '\\' << hexdigit(C >> 4) << hexdigit(C & 0x0F);
76 }
77}
78
Chris Lattner3eee99c2008-08-19 04:36:02 +000079enum PrefixType {
80 GlobalPrefix,
81 LabelPrefix,
Daniel Dunbar389529a2008-10-14 23:28:09 +000082 LocalPrefix,
83 NoPrefix
Chris Lattner3eee99c2008-08-19 04:36:02 +000084};
85
86/// PrintLLVMName - Turn the specified name into an 'LLVM name', which is either
87/// prefixed with % (if the string only contains simple characters) or is
88/// surrounded with ""'s (if it has special chars in it). Print it out.
Daniel Dunbare03eecb2009-07-25 23:55:21 +000089static void PrintLLVMName(raw_ostream &OS, const StringRef &Name,
90 PrefixType Prefix) {
91 assert(Name.data() && "Cannot get empty name!");
Chris Lattner3eee99c2008-08-19 04:36:02 +000092 switch (Prefix) {
Torok Edwinfbcc6632009-07-14 16:55:14 +000093 default: llvm_unreachable("Bad prefix!");
Daniel Dunbar389529a2008-10-14 23:28:09 +000094 case NoPrefix: break;
Chris Lattner1508d3f2008-08-19 05:16:28 +000095 case GlobalPrefix: OS << '@'; break;
96 case LabelPrefix: break;
97 case LocalPrefix: OS << '%'; break;
Nick Lewycky063699a2009-03-19 06:31:22 +000098 }
Chris Lattner3eee99c2008-08-19 04:36:02 +000099
100 // Scan the name to see if it needs quotes first.
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000101 bool NeedsQuotes = isdigit(Name[0]);
Chris Lattner3eee99c2008-08-19 04:36:02 +0000102 if (!NeedsQuotes) {
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000103 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
104 char C = Name[i];
Chris Lattner3eee99c2008-08-19 04:36:02 +0000105 if (!isalnum(C) && C != '-' && C != '.' && C != '_') {
106 NeedsQuotes = true;
107 break;
108 }
109 }
110 }
111
112 // If we didn't need any quotes, just write out the name in one blast.
113 if (!NeedsQuotes) {
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000114 OS << Name;
Chris Lattner3eee99c2008-08-19 04:36:02 +0000115 return;
116 }
117
118 // Okay, we need quotes. Output the quotes and escape any scary characters as
119 // needed.
120 OS << '"';
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000121 PrintEscapedString(Name, OS);
Chris Lattner3eee99c2008-08-19 04:36:02 +0000122 OS << '"';
123}
124
125/// PrintLLVMName - Turn the specified name into an 'LLVM name', which is either
126/// prefixed with % (if the string only contains simple characters) or is
127/// surrounded with ""'s (if it has special chars in it). Print it out.
Chris Lattner0c19df42008-08-23 22:23:09 +0000128static void PrintLLVMName(raw_ostream &OS, const Value *V) {
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000129 PrintLLVMName(OS, V->getName(),
Chris Lattner3eee99c2008-08-19 04:36:02 +0000130 isa<GlobalValue>(V) ? GlobalPrefix : LocalPrefix);
131}
132
Chris Lattner0f578952009-02-28 20:25:14 +0000133//===----------------------------------------------------------------------===//
134// TypePrinting Class: Type printing machinery
135//===----------------------------------------------------------------------===//
136
Chris Lattner3339d7f2009-02-28 23:03:55 +0000137static DenseMap<const Type *, std::string> &getTypeNamesMap(void *M) {
138 return *static_cast<DenseMap<const Type *, std::string>*>(M);
Chris Lattner3ad46822009-02-28 22:34:45 +0000139}
140
141void TypePrinting::clear() {
142 getTypeNamesMap(TypeNames).clear();
143}
Chris Lattner0f578952009-02-28 20:25:14 +0000144
Chris Lattner92c5c122009-02-28 23:20:19 +0000145bool TypePrinting::hasTypeName(const Type *Ty) const {
146 return getTypeNamesMap(TypeNames).count(Ty);
147}
148
149void TypePrinting::addTypeName(const Type *Ty, const std::string &N) {
150 getTypeNamesMap(TypeNames).insert(std::make_pair(Ty, N));
151}
152
153
154TypePrinting::TypePrinting() {
Chris Lattner3339d7f2009-02-28 23:03:55 +0000155 TypeNames = new DenseMap<const Type *, std::string>();
Chris Lattner0f578952009-02-28 20:25:14 +0000156}
157
Chris Lattner3ad46822009-02-28 22:34:45 +0000158TypePrinting::~TypePrinting() {
159 delete &getTypeNamesMap(TypeNames);
160}
161
Chris Lattner40959d02009-02-28 20:49:40 +0000162/// CalcTypeName - Write the specified type to the specified raw_ostream, making
163/// use of type names or up references to shorten the type name where possible.
164void TypePrinting::CalcTypeName(const Type *Ty,
Chris Lattner765338d2009-02-28 20:34:19 +0000165 SmallVectorImpl<const Type *> &TypeStack,
Chris Lattner242d91a2009-03-01 01:16:21 +0000166 raw_ostream &OS, bool IgnoreTopLevelName) {
Chris Lattner0f578952009-02-28 20:25:14 +0000167 // Check to see if the type is named.
Chris Lattner242d91a2009-03-01 01:16:21 +0000168 if (!IgnoreTopLevelName) {
Nick Lewycky063699a2009-03-19 06:31:22 +0000169 DenseMap<const Type *, std::string> &TM = getTypeNamesMap(TypeNames);
Chris Lattner242d91a2009-03-01 01:16:21 +0000170 DenseMap<const Type *, std::string>::iterator I = TM.find(Ty);
171 if (I != TM.end()) {
172 OS << I->second;
173 return;
174 }
Chris Lattner0f578952009-02-28 20:25:14 +0000175 }
176
177 // Check to see if the Type is already on the stack...
178 unsigned Slot = 0, CurSize = TypeStack.size();
179 while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type
180
181 // This is another base case for the recursion. In this case, we know
182 // that we have looped back to a type that we have previously visited.
183 // Generate the appropriate upreference to handle this.
184 if (Slot < CurSize) {
Chris Lattner06a23212009-02-28 21:27:31 +0000185 OS << '\\' << unsigned(CurSize-Slot); // Here's the upreference
Chris Lattner0f578952009-02-28 20:25:14 +0000186 return;
187 }
188
189 TypeStack.push_back(Ty); // Recursive case: Add us to the stack..
190
191 switch (Ty->getTypeID()) {
Chris Lattner06a23212009-02-28 21:27:31 +0000192 case Type::VoidTyID: OS << "void"; break;
193 case Type::FloatTyID: OS << "float"; break;
194 case Type::DoubleTyID: OS << "double"; break;
195 case Type::X86_FP80TyID: OS << "x86_fp80"; break;
196 case Type::FP128TyID: OS << "fp128"; break;
197 case Type::PPC_FP128TyID: OS << "ppc_fp128"; break;
198 case Type::LabelTyID: OS << "label"; break;
Nick Lewyckyadbc2842009-05-30 05:06:04 +0000199 case Type::MetadataTyID: OS << "metadata"; break;
Chris Lattner68318b12009-02-28 21:18:43 +0000200 case Type::IntegerTyID:
Chris Lattner06a23212009-02-28 21:27:31 +0000201 OS << 'i' << cast<IntegerType>(Ty)->getBitWidth();
Chris Lattner68318b12009-02-28 21:18:43 +0000202 break;
203
Chris Lattner07d88292009-02-28 20:35:42 +0000204 case Type::FunctionTyID: {
205 const FunctionType *FTy = cast<FunctionType>(Ty);
Chris Lattner06a23212009-02-28 21:27:31 +0000206 CalcTypeName(FTy->getReturnType(), TypeStack, OS);
207 OS << " (";
Chris Lattner07d88292009-02-28 20:35:42 +0000208 for (FunctionType::param_iterator I = FTy->param_begin(),
209 E = FTy->param_end(); I != E; ++I) {
210 if (I != FTy->param_begin())
Chris Lattner06a23212009-02-28 21:27:31 +0000211 OS << ", ";
212 CalcTypeName(*I, TypeStack, OS);
Chris Lattner0f578952009-02-28 20:25:14 +0000213 }
Chris Lattner07d88292009-02-28 20:35:42 +0000214 if (FTy->isVarArg()) {
Chris Lattner06a23212009-02-28 21:27:31 +0000215 if (FTy->getNumParams()) OS << ", ";
216 OS << "...";
Chris Lattner0f578952009-02-28 20:25:14 +0000217 }
Chris Lattner06a23212009-02-28 21:27:31 +0000218 OS << ')';
Chris Lattner07d88292009-02-28 20:35:42 +0000219 break;
220 }
221 case Type::StructTyID: {
222 const StructType *STy = cast<StructType>(Ty);
223 if (STy->isPacked())
Chris Lattner06a23212009-02-28 21:27:31 +0000224 OS << '<';
225 OS << "{ ";
Chris Lattner07d88292009-02-28 20:35:42 +0000226 for (StructType::element_iterator I = STy->element_begin(),
227 E = STy->element_end(); I != E; ++I) {
Chris Lattner06a23212009-02-28 21:27:31 +0000228 CalcTypeName(*I, TypeStack, OS);
Chris Lattner07d88292009-02-28 20:35:42 +0000229 if (next(I) != STy->element_end())
Chris Lattner06a23212009-02-28 21:27:31 +0000230 OS << ',';
231 OS << ' ';
Chris Lattner0f578952009-02-28 20:25:14 +0000232 }
Chris Lattner06a23212009-02-28 21:27:31 +0000233 OS << '}';
Chris Lattner07d88292009-02-28 20:35:42 +0000234 if (STy->isPacked())
Chris Lattner06a23212009-02-28 21:27:31 +0000235 OS << '>';
Chris Lattner07d88292009-02-28 20:35:42 +0000236 break;
237 }
238 case Type::PointerTyID: {
239 const PointerType *PTy = cast<PointerType>(Ty);
Chris Lattner06a23212009-02-28 21:27:31 +0000240 CalcTypeName(PTy->getElementType(), TypeStack, OS);
Chris Lattner07d88292009-02-28 20:35:42 +0000241 if (unsigned AddressSpace = PTy->getAddressSpace())
Chris Lattner06a23212009-02-28 21:27:31 +0000242 OS << " addrspace(" << AddressSpace << ')';
243 OS << '*';
Chris Lattner07d88292009-02-28 20:35:42 +0000244 break;
245 }
246 case Type::ArrayTyID: {
247 const ArrayType *ATy = cast<ArrayType>(Ty);
Chris Lattner06a23212009-02-28 21:27:31 +0000248 OS << '[' << ATy->getNumElements() << " x ";
249 CalcTypeName(ATy->getElementType(), TypeStack, OS);
250 OS << ']';
Chris Lattner07d88292009-02-28 20:35:42 +0000251 break;
252 }
253 case Type::VectorTyID: {
254 const VectorType *PTy = cast<VectorType>(Ty);
Chris Lattner06a23212009-02-28 21:27:31 +0000255 OS << "<" << PTy->getNumElements() << " x ";
256 CalcTypeName(PTy->getElementType(), TypeStack, OS);
257 OS << '>';
Chris Lattner07d88292009-02-28 20:35:42 +0000258 break;
259 }
260 case Type::OpaqueTyID:
Chris Lattner06a23212009-02-28 21:27:31 +0000261 OS << "opaque";
Chris Lattner07d88292009-02-28 20:35:42 +0000262 break;
263 default:
Chris Lattner06a23212009-02-28 21:27:31 +0000264 OS << "<unrecognized-type>";
Chris Lattner07d88292009-02-28 20:35:42 +0000265 break;
Chris Lattner0f578952009-02-28 20:25:14 +0000266 }
267
Chris Lattner40959d02009-02-28 20:49:40 +0000268 TypeStack.pop_back(); // Remove self from stack.
Chris Lattner0f578952009-02-28 20:25:14 +0000269}
270
271/// printTypeInt - The internal guts of printing out a type that has a
272/// potentially named portion.
273///
Chris Lattner242d91a2009-03-01 01:16:21 +0000274void TypePrinting::print(const Type *Ty, raw_ostream &OS,
275 bool IgnoreTopLevelName) {
Chris Lattner0f578952009-02-28 20:25:14 +0000276 // Check to see if the type is named.
Chris Lattner3339d7f2009-02-28 23:03:55 +0000277 DenseMap<const Type*, std::string> &TM = getTypeNamesMap(TypeNames);
Chris Lattner242d91a2009-03-01 01:16:21 +0000278 if (!IgnoreTopLevelName) {
279 DenseMap<const Type*, std::string>::iterator I = TM.find(Ty);
280 if (I != TM.end()) {
281 OS << I->second;
282 return;
283 }
Chris Lattner0f578952009-02-28 20:25:14 +0000284 }
285
286 // Otherwise we have a type that has not been named but is a derived type.
287 // Carefully recurse the type hierarchy to print out any contained symbolic
288 // names.
Chris Lattner765338d2009-02-28 20:34:19 +0000289 SmallVector<const Type *, 16> TypeStack;
Chris Lattner0f578952009-02-28 20:25:14 +0000290 std::string TypeName;
Chris Lattner40959d02009-02-28 20:49:40 +0000291
292 raw_string_ostream TypeOS(TypeName);
Chris Lattner242d91a2009-03-01 01:16:21 +0000293 CalcTypeName(Ty, TypeStack, TypeOS, IgnoreTopLevelName);
Chris Lattner40959d02009-02-28 20:49:40 +0000294 OS << TypeOS.str();
295
296 // Cache type name for later use.
Chris Lattner242d91a2009-03-01 01:16:21 +0000297 if (!IgnoreTopLevelName)
298 TM.insert(std::make_pair(Ty, TypeOS.str()));
Chris Lattner0f578952009-02-28 20:25:14 +0000299}
300
Chris Lattner3243ea12009-03-01 00:03:38 +0000301namespace {
302 class TypeFinder {
303 // To avoid walking constant expressions multiple times and other IR
304 // objects, we keep several helper maps.
305 DenseSet<const Value*> VisitedConstants;
306 DenseSet<const Type*> VisitedTypes;
307
308 TypePrinting &TP;
309 std::vector<const Type*> &NumberedTypes;
310 public:
311 TypeFinder(TypePrinting &tp, std::vector<const Type*> &numberedTypes)
312 : TP(tp), NumberedTypes(numberedTypes) {}
313
314 void Run(const Module &M) {
Chris Lattner84516482009-03-01 00:32:33 +0000315 // Get types from the type symbol table. This gets opaque types referened
316 // only through derived named types.
317 const TypeSymbolTable &ST = M.getTypeSymbolTable();
318 for (TypeSymbolTable::const_iterator TI = ST.begin(), E = ST.end();
319 TI != E; ++TI)
320 IncorporateType(TI->second);
321
Chris Lattner3243ea12009-03-01 00:03:38 +0000322 // Get types from global variables.
323 for (Module::const_global_iterator I = M.global_begin(),
324 E = M.global_end(); I != E; ++I) {
325 IncorporateType(I->getType());
326 if (I->hasInitializer())
327 IncorporateValue(I->getInitializer());
328 }
329
330 // Get types from aliases.
331 for (Module::const_alias_iterator I = M.alias_begin(),
332 E = M.alias_end(); I != E; ++I) {
333 IncorporateType(I->getType());
334 IncorporateValue(I->getAliasee());
335 }
336
337 // Get types from functions.
338 for (Module::const_iterator FI = M.begin(), E = M.end(); FI != E; ++FI) {
339 IncorporateType(FI->getType());
340
341 for (Function::const_iterator BB = FI->begin(), E = FI->end();
342 BB != E;++BB)
343 for (BasicBlock::const_iterator II = BB->begin(),
344 E = BB->end(); II != E; ++II) {
345 const Instruction &I = *II;
346 // Incorporate the type of the instruction and all its operands.
347 IncorporateType(I.getType());
348 for (User::const_op_iterator OI = I.op_begin(), OE = I.op_end();
349 OI != OE; ++OI)
350 IncorporateValue(*OI);
351 }
352 }
353 }
354
355 private:
356 void IncorporateType(const Type *Ty) {
357 // Check to see if we're already visited this type.
Chris Lattner84516482009-03-01 00:32:33 +0000358 if (!VisitedTypes.insert(Ty).second)
Chris Lattner3243ea12009-03-01 00:03:38 +0000359 return;
360
361 // If this is a structure or opaque type, add a name for the type.
Nick Lewycky49f89192009-04-04 07:22:01 +0000362 if (((isa<StructType>(Ty) && cast<StructType>(Ty)->getNumElements())
363 || isa<OpaqueType>(Ty)) && !TP.hasTypeName(Ty)) {
Chris Lattner3243ea12009-03-01 00:03:38 +0000364 TP.addTypeName(Ty, "%"+utostr(unsigned(NumberedTypes.size())));
365 NumberedTypes.push_back(Ty);
366 }
367
368 // Recursively walk all contained types.
369 for (Type::subtype_iterator I = Ty->subtype_begin(),
370 E = Ty->subtype_end(); I != E; ++I)
371 IncorporateType(*I);
372 }
373
374 /// IncorporateValue - This method is used to walk operand lists finding
375 /// types hiding in constant expressions and other operands that won't be
376 /// walked in other ways. GlobalValues, basic blocks, instructions, and
377 /// inst operands are all explicitly enumerated.
378 void IncorporateValue(const Value *V) {
379 if (V == 0 || !isa<Constant>(V) || isa<GlobalValue>(V)) return;
380
381 // Already visited?
382 if (!VisitedConstants.insert(V).second)
383 return;
384
385 // Check this type.
386 IncorporateType(V->getType());
387
388 // Look in operands for types.
389 const Constant *C = cast<Constant>(V);
390 for (Constant::const_op_iterator I = C->op_begin(),
391 E = C->op_end(); I != E;++I)
392 IncorporateValue(*I);
393 }
394 };
395} // end anonymous namespace
396
397
398/// AddModuleTypesToPrinter - Add all of the symbolic type names for types in
399/// the specified module to the TypePrinter and all numbered types to it and the
400/// NumberedTypes table.
401static void AddModuleTypesToPrinter(TypePrinting &TP,
402 std::vector<const Type*> &NumberedTypes,
403 const Module *M) {
Chris Lattner92c5c122009-02-28 23:20:19 +0000404 if (M == 0) return;
405
406 // If the module has a symbol table, take all global types and stuff their
407 // names into the TypeNames map.
408 const TypeSymbolTable &ST = M->getTypeSymbolTable();
409 for (TypeSymbolTable::const_iterator TI = ST.begin(), E = ST.end();
410 TI != E; ++TI) {
411 const Type *Ty = cast<Type>(TI->second);
412
413 // As a heuristic, don't insert pointer to primitive types, because
414 // they are used too often to have a single useful name.
415 if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
416 const Type *PETy = PTy->getElementType();
417 if ((PETy->isPrimitiveType() || PETy->isInteger()) &&
418 !isa<OpaqueType>(PETy))
419 continue;
420 }
421
422 // Likewise don't insert primitives either.
423 if (Ty->isInteger() || Ty->isPrimitiveType())
424 continue;
425
426 // Get the name as a string and insert it into TypeNames.
427 std::string NameStr;
428 raw_string_ostream NameOS(NameStr);
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000429 PrintLLVMName(NameOS, TI->first, LocalPrefix);
Chris Lattner92c5c122009-02-28 23:20:19 +0000430 TP.addTypeName(Ty, NameOS.str());
431 }
Chris Lattner3243ea12009-03-01 00:03:38 +0000432
433 // Walk the entire module to find references to unnamed structure and opaque
434 // types. This is required for correctness by opaque types (because multiple
435 // uses of an unnamed opaque type needs to be referred to by the same ID) and
436 // it shrinks complex recursive structure types substantially in some cases.
437 TypeFinder(TP, NumberedTypes).Run(*M);
Chris Lattner92c5c122009-02-28 23:20:19 +0000438}
439
Chris Lattner0f578952009-02-28 20:25:14 +0000440
Chris Lattner0f578952009-02-28 20:25:14 +0000441/// WriteTypeSymbolic - This attempts to write the specified type as a symbolic
442/// type, iff there is an entry in the modules symbol table for the specified
Chris Lattner24025262009-02-28 21:05:51 +0000443/// type or one of it's component types.
Chris Lattner0f578952009-02-28 20:25:14 +0000444///
Chris Lattner92c5c122009-02-28 23:20:19 +0000445void llvm::WriteTypeSymbolic(raw_ostream &OS, const Type *Ty, const Module *M) {
446 TypePrinting Printer;
Chris Lattner3243ea12009-03-01 00:03:38 +0000447 std::vector<const Type*> NumberedTypes;
448 AddModuleTypesToPrinter(Printer, NumberedTypes, M);
Chris Lattner92c5c122009-02-28 23:20:19 +0000449 Printer.print(Ty, OS);
Chris Lattner0f578952009-02-28 20:25:14 +0000450}
451
Chris Lattner3eee99c2008-08-19 04:36:02 +0000452//===----------------------------------------------------------------------===//
453// SlotTracker Class: Enumerate slot numbers for unnamed values
454//===----------------------------------------------------------------------===//
455
Chris Lattner3ee58762008-08-19 04:28:07 +0000456namespace {
457
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000458/// This class provides computation of slot numbers for LLVM Assembly writing.
Chris Lattner393b7cd2008-08-17 04:17:45 +0000459///
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000460class SlotTracker {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000461public:
Devang Patel983c6b12009-07-08 21:44:25 +0000462 /// ValueMap - A mapping of Values to slot numbers.
Chris Lattnera204d412008-08-17 17:25:25 +0000463 typedef DenseMap<const Value*, unsigned> ValueMap;
Chris Lattner393b7cd2008-08-17 04:17:45 +0000464
465private:
Devang Patel983c6b12009-07-08 21:44:25 +0000466 /// TheModule - The module for which we are holding slot numbers.
Chris Lattner393b7cd2008-08-17 04:17:45 +0000467 const Module* TheModule;
468
Devang Patel983c6b12009-07-08 21:44:25 +0000469 /// TheFunction - The function for which we are holding slot numbers.
Chris Lattner393b7cd2008-08-17 04:17:45 +0000470 const Function* TheFunction;
471 bool FunctionProcessed;
472
Devang Patel983c6b12009-07-08 21:44:25 +0000473 /// TheMDNode - The MDNode for which we are holding slot numbers.
474 const MDNode *TheMDNode;
475
Devang Patelb4a4e772009-07-30 00:02:57 +0000476 /// TheNamedMDNode - The MDNode for which we are holding slot numbers.
477 const NamedMDNode *TheNamedMDNode;
478
Devang Patel983c6b12009-07-08 21:44:25 +0000479 /// mMap - The TypePlanes map for the module level data.
Chris Lattner393b7cd2008-08-17 04:17:45 +0000480 ValueMap mMap;
481 unsigned mNext;
482
Devang Patel983c6b12009-07-08 21:44:25 +0000483 /// fMap - The TypePlanes map for the function level data.
Chris Lattner393b7cd2008-08-17 04:17:45 +0000484 ValueMap fMap;
485 unsigned fNext;
486
Devang Patel983c6b12009-07-08 21:44:25 +0000487 /// mdnMap - Map for MDNodes.
488 ValueMap mdnMap;
489 unsigned mdnNext;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000490public:
Chris Lattner393b7cd2008-08-17 04:17:45 +0000491 /// Construct from a module
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000492 explicit SlotTracker(const Module *M);
Chris Lattner393b7cd2008-08-17 04:17:45 +0000493 /// Construct from a function, starting out in incorp state.
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000494 explicit SlotTracker(const Function *F);
Devang Patel983c6b12009-07-08 21:44:25 +0000495 /// Construct from a mdnode.
496 explicit SlotTracker(const MDNode *N);
Devang Patelb4a4e772009-07-30 00:02:57 +0000497 /// Construct from a named mdnode.
498 explicit SlotTracker(const NamedMDNode *N);
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000499
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000500 /// Return the slot number of the specified value in it's type
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000501 /// plane. If something is not in the SlotTracker, return -1.
Chris Lattner5e043322007-01-11 03:54:27 +0000502 int getLocalSlot(const Value *V);
503 int getGlobalSlot(const GlobalValue *V);
Devang Patel983c6b12009-07-08 21:44:25 +0000504 int getMetadataSlot(const MDNode *N);
Reid Spencer8beac692004-06-09 15:26:53 +0000505
Misha Brukmanb1c93172005-04-21 23:48:37 +0000506 /// If you'd like to deal with a function instead of just a module, use
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000507 /// this method to get its data into the SlotTracker.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000508 void incorporateFunction(const Function *F) {
509 TheFunction = F;
Reid Spencerb0ac8c42004-08-16 07:46:33 +0000510 FunctionProcessed = false;
511 }
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000512
Misha Brukmanb1c93172005-04-21 23:48:37 +0000513 /// After calling incorporateFunction, use this method to remove the
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000514 /// most recently incorporated function from the SlotTracker. This
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000515 /// will reset the state of the machine back to just the module contents.
516 void purgeFunction();
517
Devang Patel983c6b12009-07-08 21:44:25 +0000518 /// MDNode map iterators.
519 ValueMap::iterator mdnBegin() { return mdnMap.begin(); }
520 ValueMap::iterator mdnEnd() { return mdnMap.end(); }
521 unsigned mdnSize() { return mdnMap.size(); }
522
Reid Spencer56010e42004-05-26 21:56:09 +0000523 /// This function does the actual initialization.
524 inline void initialize();
525
Devang Patel983c6b12009-07-08 21:44:25 +0000526 // Implementation Details
527private:
Chris Lattnerea862a32007-01-09 07:55:49 +0000528 /// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
529 void CreateModuleSlot(const GlobalValue *V);
Devang Patel983c6b12009-07-08 21:44:25 +0000530
531 /// CreateMetadataSlot - Insert the specified MDNode* into the slot table.
532 void CreateMetadataSlot(const MDNode *N);
533
Chris Lattnerea862a32007-01-09 07:55:49 +0000534 /// CreateFunctionSlot - Insert the specified Value* into the slot table.
535 void CreateFunctionSlot(const Value *V);
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000536
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000537 /// Add all of the module level global variables (and their initializers)
538 /// and function declarations, but not the contents of those functions.
539 void processModule();
540
Devang Patel983c6b12009-07-08 21:44:25 +0000541 /// Add all of the functions arguments, basic blocks, and instructions.
Reid Spencer56010e42004-05-26 21:56:09 +0000542 void processFunction();
543
Devang Patel983c6b12009-07-08 21:44:25 +0000544 /// Add all MDNode operands.
545 void processMDNode();
546
Devang Patelb4a4e772009-07-30 00:02:57 +0000547 /// Add all MDNode operands.
548 void processNamedMDNode();
549
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000550 SlotTracker(const SlotTracker &); // DO NOT IMPLEMENT
551 void operator=(const SlotTracker &); // DO NOT IMPLEMENT
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000552};
553
Chris Lattner3ee58762008-08-19 04:28:07 +0000554} // end anonymous namespace
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000555
Chris Lattner3eee99c2008-08-19 04:36:02 +0000556
557static SlotTracker *createSlotTracker(const Value *V) {
558 if (const Argument *FA = dyn_cast<Argument>(V))
559 return new SlotTracker(FA->getParent());
560
561 if (const Instruction *I = dyn_cast<Instruction>(V))
562 return new SlotTracker(I->getParent()->getParent());
563
564 if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
565 return new SlotTracker(BB->getParent());
566
567 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
568 return new SlotTracker(GV->getParent());
569
570 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V))
571 return new SlotTracker(GA->getParent());
572
573 if (const Function *Func = dyn_cast<Function>(V))
574 return new SlotTracker(Func);
575
576 return 0;
577}
578
579#if 0
Dan Gohman1ddf98a2009-07-25 01:43:01 +0000580#define ST_DEBUG(X) errs() << X
Chris Lattner3eee99c2008-08-19 04:36:02 +0000581#else
Chris Lattner604e3512008-08-19 04:47:09 +0000582#define ST_DEBUG(X)
Chris Lattner3eee99c2008-08-19 04:36:02 +0000583#endif
584
585// Module level constructor. Causes the contents of the Module (sans functions)
586// to be added to the slot table.
587SlotTracker::SlotTracker(const Module *M)
Devang Patel983c6b12009-07-08 21:44:25 +0000588 : TheModule(M), TheFunction(0), FunctionProcessed(false), TheMDNode(0),
Devang Patelb4a4e772009-07-30 00:02:57 +0000589 TheNamedMDNode(0), mNext(0), fNext(0), mdnNext(0) {
Chris Lattner3eee99c2008-08-19 04:36:02 +0000590}
591
592// Function level constructor. Causes the contents of the Module and the one
593// function provided to be added to the slot table.
594SlotTracker::SlotTracker(const Function *F)
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000595 : TheModule(F ? F->getParent() : 0), TheFunction(F), FunctionProcessed(false),
Devang Patelb4a4e772009-07-30 00:02:57 +0000596 TheMDNode(0), TheNamedMDNode(0), mNext(0), fNext(0), mdnNext(0) {
Devang Patel983c6b12009-07-08 21:44:25 +0000597}
598
599// Constructor to handle single MDNode.
600SlotTracker::SlotTracker(const MDNode *C)
601 : TheModule(0), TheFunction(0), FunctionProcessed(false), TheMDNode(C),
Devang Patelb4a4e772009-07-30 00:02:57 +0000602 TheNamedMDNode(0), mNext(0), fNext(0), mdnNext(0) {
603}
604
605// Constructor to handle single NamedMDNode.
606SlotTracker::SlotTracker(const NamedMDNode *N)
607 : TheModule(0), TheFunction(0), FunctionProcessed(false), TheMDNode(0),
608 TheNamedMDNode(N), mNext(0), fNext(0), mdnNext(0) {
Chris Lattner3eee99c2008-08-19 04:36:02 +0000609}
610
611inline void SlotTracker::initialize() {
612 if (TheModule) {
613 processModule();
614 TheModule = 0; ///< Prevent re-processing next time we're called.
615 }
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000616
Chris Lattner3eee99c2008-08-19 04:36:02 +0000617 if (TheFunction && !FunctionProcessed)
618 processFunction();
Devang Patel983c6b12009-07-08 21:44:25 +0000619
620 if (TheMDNode)
621 processMDNode();
Devang Patelb4a4e772009-07-30 00:02:57 +0000622
623 if (TheNamedMDNode)
624 processNamedMDNode();
Chris Lattner3eee99c2008-08-19 04:36:02 +0000625}
626
627// Iterate through all the global variables, functions, and global
628// variable initializers and create slots for them.
629void SlotTracker::processModule() {
Chris Lattner604e3512008-08-19 04:47:09 +0000630 ST_DEBUG("begin processModule!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000631
632 // Add all of the unnamed global variables to the value table.
633 for (Module::const_global_iterator I = TheModule->global_begin(),
Devang Patel983c6b12009-07-08 21:44:25 +0000634 E = TheModule->global_end(); I != E; ++I) {
Chris Lattner3eee99c2008-08-19 04:36:02 +0000635 if (!I->hasName())
636 CreateModuleSlot(I);
Devang Patel983c6b12009-07-08 21:44:25 +0000637 if (I->hasInitializer()) {
638 if (MDNode *N = dyn_cast<MDNode>(I->getInitializer()))
639 CreateMetadataSlot(N);
640 }
641 }
Chris Lattner3eee99c2008-08-19 04:36:02 +0000642
Devang Patel23e68302009-07-29 22:04:47 +0000643 // Add metadata used by named metadata.
644 for (Module::const_named_metadata_iterator
645 I = TheModule->named_metadata_begin(),
646 E = TheModule->named_metadata_end(); I != E; ++I) {
647 const NamedMDNode *NMD = I;
648 for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i) {
649 MDNode *MD = dyn_cast_or_null<MDNode>(NMD->getElement(i));
650 CreateMetadataSlot(MD);
651 }
652 }
653
Chris Lattner3eee99c2008-08-19 04:36:02 +0000654 // Add all the unnamed functions to the table.
655 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
656 I != E; ++I)
657 if (!I->hasName())
658 CreateModuleSlot(I);
659
Chris Lattner604e3512008-08-19 04:47:09 +0000660 ST_DEBUG("end processModule!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000661}
662
Chris Lattner3eee99c2008-08-19 04:36:02 +0000663// Process the arguments, basic blocks, and instructions of a function.
664void SlotTracker::processFunction() {
Chris Lattner604e3512008-08-19 04:47:09 +0000665 ST_DEBUG("begin processFunction!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000666 fNext = 0;
667
668 // Add all the function arguments with no names.
669 for(Function::const_arg_iterator AI = TheFunction->arg_begin(),
670 AE = TheFunction->arg_end(); AI != AE; ++AI)
671 if (!AI->hasName())
672 CreateFunctionSlot(AI);
673
Chris Lattner604e3512008-08-19 04:47:09 +0000674 ST_DEBUG("Inserting Instructions:\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000675
676 // Add all of the basic blocks and instructions with no names.
677 for (Function::const_iterator BB = TheFunction->begin(),
678 E = TheFunction->end(); BB != E; ++BB) {
679 if (!BB->hasName())
680 CreateFunctionSlot(BB);
Devang Patel983c6b12009-07-08 21:44:25 +0000681 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E;
682 ++I) {
Chris Lattner3eee99c2008-08-19 04:36:02 +0000683 if (I->getType() != Type::VoidTy && !I->hasName())
684 CreateFunctionSlot(I);
Devang Patel983c6b12009-07-08 21:44:25 +0000685 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
686 if (MDNode *N = dyn_cast<MDNode>(I->getOperand(i)))
687 CreateMetadataSlot(N);
688 }
Chris Lattner3eee99c2008-08-19 04:36:02 +0000689 }
690
691 FunctionProcessed = true;
692
Chris Lattner604e3512008-08-19 04:47:09 +0000693 ST_DEBUG("end processFunction!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000694}
695
Devang Patel983c6b12009-07-08 21:44:25 +0000696/// processMDNode - Process TheMDNode.
697void SlotTracker::processMDNode() {
698 ST_DEBUG("begin processMDNode!\n");
699 mdnNext = 0;
700 CreateMetadataSlot(TheMDNode);
701 TheMDNode = 0;
702 ST_DEBUG("end processMDNode!\n");
703}
704
Devang Patelb4a4e772009-07-30 00:02:57 +0000705/// processNamedMDNode - Process TheNamedMDNode.
706void SlotTracker::processNamedMDNode() {
707 ST_DEBUG("begin processNamedMDNode!\n");
708 mdnNext = 0;
709 for (unsigned i = 0, e = TheNamedMDNode->getNumElements(); i != e; ++i) {
710 MDNode *MD = dyn_cast_or_null<MDNode>(TheNamedMDNode->getElement(i));
711 if (MD)
712 CreateMetadataSlot(MD);
713 }
714 TheNamedMDNode = 0;
715 ST_DEBUG("end processNamedMDNode!\n");
716}
717
Chris Lattner3eee99c2008-08-19 04:36:02 +0000718/// Clean up after incorporating a function. This is the only way to get out of
719/// the function incorporation state that affects get*Slot/Create*Slot. Function
720/// incorporation state is indicated by TheFunction != 0.
721void SlotTracker::purgeFunction() {
Chris Lattner604e3512008-08-19 04:47:09 +0000722 ST_DEBUG("begin purgeFunction!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000723 fMap.clear(); // Simply discard the function level map
724 TheFunction = 0;
725 FunctionProcessed = false;
Chris Lattner604e3512008-08-19 04:47:09 +0000726 ST_DEBUG("end purgeFunction!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000727}
728
729/// getGlobalSlot - Get the slot number of a global value.
730int SlotTracker::getGlobalSlot(const GlobalValue *V) {
731 // Check for uninitialized state and do lazy initialization.
732 initialize();
733
734 // Find the type plane in the module map
735 ValueMap::iterator MI = mMap.find(V);
Dan Gohman1dd27572008-10-01 19:58:59 +0000736 return MI == mMap.end() ? -1 : (int)MI->second;
Chris Lattner3eee99c2008-08-19 04:36:02 +0000737}
738
Devang Patel983c6b12009-07-08 21:44:25 +0000739/// getGlobalSlot - Get the slot number of a MDNode.
740int SlotTracker::getMetadataSlot(const MDNode *N) {
741 // Check for uninitialized state and do lazy initialization.
742 initialize();
743
744 // Find the type plane in the module map
745 ValueMap::iterator MI = mdnMap.find(N);
746 return MI == mdnMap.end() ? -1 : (int)MI->second;
747}
748
Chris Lattner3eee99c2008-08-19 04:36:02 +0000749
750/// getLocalSlot - Get the slot number for a value that is local to a function.
751int SlotTracker::getLocalSlot(const Value *V) {
752 assert(!isa<Constant>(V) && "Can't get a constant or global slot with this!");
753
754 // Check for uninitialized state and do lazy initialization.
755 initialize();
756
757 ValueMap::iterator FI = fMap.find(V);
Dan Gohman1dd27572008-10-01 19:58:59 +0000758 return FI == fMap.end() ? -1 : (int)FI->second;
Chris Lattner3eee99c2008-08-19 04:36:02 +0000759}
760
761
762/// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
763void SlotTracker::CreateModuleSlot(const GlobalValue *V) {
764 assert(V && "Can't insert a null Value into SlotTracker!");
765 assert(V->getType() != Type::VoidTy && "Doesn't need a slot!");
766 assert(!V->hasName() && "Doesn't need a slot!");
767
768 unsigned DestSlot = mNext++;
769 mMap[V] = DestSlot;
770
Chris Lattner604e3512008-08-19 04:47:09 +0000771 ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" <<
Chris Lattner3eee99c2008-08-19 04:36:02 +0000772 DestSlot << " [");
773 // G = Global, F = Function, A = Alias, o = other
Chris Lattner604e3512008-08-19 04:47:09 +0000774 ST_DEBUG((isa<GlobalVariable>(V) ? 'G' :
Chris Lattner3eee99c2008-08-19 04:36:02 +0000775 (isa<Function>(V) ? 'F' :
776 (isa<GlobalAlias>(V) ? 'A' : 'o'))) << "]\n");
777}
778
Chris Lattner3eee99c2008-08-19 04:36:02 +0000779/// CreateSlot - Create a new slot for the specified value if it has no name.
780void SlotTracker::CreateFunctionSlot(const Value *V) {
781 assert(V->getType() != Type::VoidTy && !V->hasName() &&
782 "Doesn't need a slot!");
783
784 unsigned DestSlot = fNext++;
785 fMap[V] = DestSlot;
786
787 // G = Global, F = Function, o = other
Chris Lattner604e3512008-08-19 04:47:09 +0000788 ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" <<
Chris Lattner3eee99c2008-08-19 04:36:02 +0000789 DestSlot << " [o]\n");
790}
791
Devang Patel983c6b12009-07-08 21:44:25 +0000792/// CreateModuleSlot - Insert the specified MDNode* into the slot table.
793void SlotTracker::CreateMetadataSlot(const MDNode *N) {
794 assert(N && "Can't insert a null Value into SlotTracker!");
795
796 ValueMap::iterator I = mdnMap.find(N);
797 if (I != mdnMap.end())
798 return;
Chris Lattner3eee99c2008-08-19 04:36:02 +0000799
Devang Patel983c6b12009-07-08 21:44:25 +0000800 unsigned DestSlot = mdnNext++;
801 mdnMap[N] = DestSlot;
802
803 for (MDNode::const_elem_iterator MDI = N->elem_begin(),
804 MDE = N->elem_end(); MDI != MDE; ++MDI) {
805 const Value *TV = *MDI;
806 if (TV)
807 if (const MDNode *N2 = dyn_cast<MDNode>(TV))
808 CreateMetadataSlot(N2);
809 }
810}
Chris Lattner3eee99c2008-08-19 04:36:02 +0000811
812//===----------------------------------------------------------------------===//
813// AsmWriter Implementation
814//===----------------------------------------------------------------------===//
Chris Lattner7f8845a2002-07-23 18:07:49 +0000815
Chris Lattner0c19df42008-08-23 22:23:09 +0000816static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
Chris Lattner0f578952009-02-28 20:25:14 +0000817 TypePrinting &TypePrinter,
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000818 SlotTracker *Machine);
Reid Spencer58d30f22004-07-04 11:50:43 +0000819
Chris Lattner033935d2008-08-17 04:40:13 +0000820
Chris Lattnerb86620e2001-10-29 16:37:48 +0000821
Chris Lattnerfc9f1c92006-12-06 06:40:49 +0000822static const char *getPredicateText(unsigned predicate) {
Reid Spencer812a1be2006-12-04 05:19:18 +0000823 const char * pred = "unknown";
824 switch (predicate) {
825 case FCmpInst::FCMP_FALSE: pred = "false"; break;
826 case FCmpInst::FCMP_OEQ: pred = "oeq"; break;
827 case FCmpInst::FCMP_OGT: pred = "ogt"; break;
828 case FCmpInst::FCMP_OGE: pred = "oge"; break;
829 case FCmpInst::FCMP_OLT: pred = "olt"; break;
830 case FCmpInst::FCMP_OLE: pred = "ole"; break;
831 case FCmpInst::FCMP_ONE: pred = "one"; break;
832 case FCmpInst::FCMP_ORD: pred = "ord"; break;
833 case FCmpInst::FCMP_UNO: pred = "uno"; break;
834 case FCmpInst::FCMP_UEQ: pred = "ueq"; break;
835 case FCmpInst::FCMP_UGT: pred = "ugt"; break;
836 case FCmpInst::FCMP_UGE: pred = "uge"; break;
837 case FCmpInst::FCMP_ULT: pred = "ult"; break;
838 case FCmpInst::FCMP_ULE: pred = "ule"; break;
839 case FCmpInst::FCMP_UNE: pred = "une"; break;
840 case FCmpInst::FCMP_TRUE: pred = "true"; break;
841 case ICmpInst::ICMP_EQ: pred = "eq"; break;
842 case ICmpInst::ICMP_NE: pred = "ne"; break;
843 case ICmpInst::ICMP_SGT: pred = "sgt"; break;
844 case ICmpInst::ICMP_SGE: pred = "sge"; break;
845 case ICmpInst::ICMP_SLT: pred = "slt"; break;
846 case ICmpInst::ICMP_SLE: pred = "sle"; break;
847 case ICmpInst::ICMP_UGT: pred = "ugt"; break;
848 case ICmpInst::ICMP_UGE: pred = "uge"; break;
849 case ICmpInst::ICMP_ULT: pred = "ult"; break;
850 case ICmpInst::ICMP_ULE: pred = "ule"; break;
851 }
852 return pred;
853}
854
Devang Patel983c6b12009-07-08 21:44:25 +0000855static void WriteMDNodes(raw_ostream &Out, TypePrinting &TypePrinter,
856 SlotTracker &Machine) {
857 SmallVector<const MDNode *, 16> Nodes;
858 Nodes.resize(Machine.mdnSize());
859 for (SlotTracker::ValueMap::iterator I =
860 Machine.mdnBegin(), E = Machine.mdnEnd(); I != E; ++I)
861 Nodes[I->second] = cast<MDNode>(I->first);
862
863 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Devang Patelf04d63a2009-07-08 21:57:07 +0000864 Out << '!' << i << " = metadata ";
Devang Patel983c6b12009-07-08 21:44:25 +0000865 const MDNode *Node = Nodes[i];
866 Out << "!{";
867 for (MDNode::const_elem_iterator NI = Node->elem_begin(),
868 NE = Node->elem_end(); NI != NE;) {
869 const Value *V = *NI;
870 if (!V)
871 Out << "null";
872 else if (const MDNode *N = dyn_cast<MDNode>(V)) {
873 Out << "metadata ";
874 Out << '!' << Machine.getMetadataSlot(N);
875 }
876 else {
877 TypePrinter.print((*NI)->getType(), Out);
878 Out << ' ';
879 WriteAsOperandInternal(Out, *NI, TypePrinter, &Machine);
880 }
881 if (++NI != NE)
882 Out << ", ";
883 }
884 Out << "}\n";
885 }
886}
887
Dan Gohman0ebd6962009-07-20 21:19:07 +0000888static void WriteOptimizationInfo(raw_ostream &Out, const User *U) {
889 if (const OverflowingBinaryOperator *OBO =
890 dyn_cast<OverflowingBinaryOperator>(U)) {
891 if (OBO->hasNoUnsignedOverflow())
Dan Gohman9c7f8082009-07-27 16:11:46 +0000892 Out << " nuw";
Dan Gohman0ebd6962009-07-20 21:19:07 +0000893 if (OBO->hasNoSignedOverflow())
Dan Gohman9c7f8082009-07-27 16:11:46 +0000894 Out << " nsw";
Dan Gohman0ebd6962009-07-20 21:19:07 +0000895 } else if (const SDivOperator *Div = dyn_cast<SDivOperator>(U)) {
896 if (Div->isExact())
Dan Gohman9c7f8082009-07-27 16:11:46 +0000897 Out << " exact";
Dan Gohman1639c392009-07-27 21:53:46 +0000898 } else if (const GEPOperator *GEP = dyn_cast<GEPOperator>(U)) {
899 if (GEP->isInBounds())
900 Out << " inbounds";
Dan Gohman0ebd6962009-07-20 21:19:07 +0000901 }
902}
903
Chris Lattner0c19df42008-08-23 22:23:09 +0000904static void WriteConstantInt(raw_ostream &Out, const Constant *CV,
Chris Lattner0f578952009-02-28 20:25:14 +0000905 TypePrinting &TypePrinter, SlotTracker *Machine) {
Zhou Sheng75b871f2007-01-11 12:24:14 +0000906 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Chris Lattner17f71652008-08-17 07:19:36 +0000907 if (CI->getType() == Type::Int1Ty) {
Reid Spencercddc9df2007-01-12 04:24:46 +0000908 Out << (CI->getZExtValue() ? "true" : "false");
Chris Lattner17f71652008-08-17 07:19:36 +0000909 return;
910 }
911 Out << CI->getValue();
912 return;
913 }
914
915 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Dale Johannesen028084e2007-09-12 03:30:33 +0000916 if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEdouble ||
917 &CFP->getValueAPF().getSemantics() == &APFloat::IEEEsingle) {
918 // We would like to output the FP constant value in exponential notation,
919 // but we cannot do this if doing so will lose precision. Check here to
920 // make sure that we only output it in exponential format if we can parse
921 // the value back and get the same value.
922 //
Dale Johannesen1f864982009-01-21 20:32:55 +0000923 bool ignored;
Dale Johannesen028084e2007-09-12 03:30:33 +0000924 bool isDouble = &CFP->getValueAPF().getSemantics()==&APFloat::IEEEdouble;
Chris Lattner17f71652008-08-17 07:19:36 +0000925 double Val = isDouble ? CFP->getValueAPF().convertToDouble() :
926 CFP->getValueAPF().convertToFloat();
Dale Johannesen028084e2007-09-12 03:30:33 +0000927 std::string StrVal = ftostr(CFP->getValueAPF());
Chris Lattner1e194682002-04-18 18:53:13 +0000928
Dale Johannesen028084e2007-09-12 03:30:33 +0000929 // Check to make sure that the stringized number is not some string like
930 // "Inf" or NaN, that atof will accept, but the lexer will not. Check
931 // that the string matches the "[-+]?[0-9]" regex.
932 //
933 if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
934 ((StrVal[0] == '-' || StrVal[0] == '+') &&
935 (StrVal[1] >= '0' && StrVal[1] <= '9'))) {
936 // Reparse stringized version!
937 if (atof(StrVal.c_str()) == Val) {
938 Out << StrVal;
939 return;
940 }
Chris Lattner1e194682002-04-18 18:53:13 +0000941 }
Dale Johannesen028084e2007-09-12 03:30:33 +0000942 // Otherwise we could not reparse it to exactly the same value, so we must
Dale Johannesen1f864982009-01-21 20:32:55 +0000943 // output the string in hexadecimal format! Note that loading and storing
944 // floating point types changes the bits of NaNs on some hosts, notably
945 // x86, so we must not use these types.
Dale Johannesen028084e2007-09-12 03:30:33 +0000946 assert(sizeof(double) == sizeof(uint64_t) &&
947 "assuming that double is 64 bits!");
Chris Lattner5505eed2008-11-10 04:30:26 +0000948 char Buffer[40];
Dale Johannesen1f864982009-01-21 20:32:55 +0000949 APFloat apf = CFP->getValueAPF();
950 // Floats are represented in ASCII IR as double, convert.
951 if (!isDouble)
952 apf.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven,
953 &ignored);
954 Out << "0x" <<
955 utohex_buffer(uint64_t(apf.bitcastToAPInt().getZExtValue()),
956 Buffer+40);
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000957 return;
958 }
959
960 // Some form of long double. These appear as a magic letter identifying
961 // the type, then a fixed number of hex digits.
962 Out << "0x";
Dale Johannesen93eefa02009-03-23 21:16:53 +0000963 if (&CFP->getValueAPF().getSemantics() == &APFloat::x87DoubleExtended) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000964 Out << 'K';
Dale Johannesen93eefa02009-03-23 21:16:53 +0000965 // api needed to prevent premature destruction
966 APInt api = CFP->getValueAPF().bitcastToAPInt();
967 const uint64_t* p = api.getRawData();
968 uint64_t word = p[1];
969 int shiftcount=12;
970 int width = api.getBitWidth();
971 for (int j=0; j<width; j+=4, shiftcount-=4) {
972 unsigned int nibble = (word>>shiftcount) & 15;
973 if (nibble < 10)
974 Out << (unsigned char)(nibble + '0');
975 else
976 Out << (unsigned char)(nibble - 10 + 'A');
977 if (shiftcount == 0 && j+4 < width) {
978 word = *p;
979 shiftcount = 64;
980 if (width-j-4 < 64)
981 shiftcount = width-j-4;
982 }
983 }
984 return;
985 } else if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEquad)
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000986 Out << 'L';
987 else if (&CFP->getValueAPF().getSemantics() == &APFloat::PPCDoubleDouble)
988 Out << 'M';
989 else
Torok Edwinfbcc6632009-07-14 16:55:14 +0000990 llvm_unreachable("Unsupported floating point type");
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000991 // api needed to prevent premature destruction
Dale Johannesen54306fe2008-10-09 18:53:47 +0000992 APInt api = CFP->getValueAPF().bitcastToAPInt();
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000993 const uint64_t* p = api.getRawData();
994 uint64_t word = *p;
995 int shiftcount=60;
996 int width = api.getBitWidth();
997 for (int j=0; j<width; j+=4, shiftcount-=4) {
998 unsigned int nibble = (word>>shiftcount) & 15;
999 if (nibble < 10)
1000 Out << (unsigned char)(nibble + '0');
Dale Johannesen028084e2007-09-12 03:30:33 +00001001 else
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001002 Out << (unsigned char)(nibble - 10 + 'A');
1003 if (shiftcount == 0 && j+4 < width) {
1004 word = *(++p);
1005 shiftcount = 64;
1006 if (width-j-4 < 64)
1007 shiftcount = width-j-4;
Dale Johannesen028084e2007-09-12 03:30:33 +00001008 }
1009 }
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001010 return;
1011 }
1012
1013 if (isa<ConstantAggregateZero>(CV)) {
Chris Lattner76b2ff42004-02-15 05:55:15 +00001014 Out << "zeroinitializer";
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001015 return;
1016 }
1017
1018 if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
Chris Lattner1e194682002-04-18 18:53:13 +00001019 // As a special case, print the array as a string if it is an array of
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00001020 // i8 with ConstantInt values.
Misha Brukmanb1c93172005-04-21 23:48:37 +00001021 //
Chris Lattner1e194682002-04-18 18:53:13 +00001022 const Type *ETy = CA->getType()->getElementType();
Chris Lattner6ed87bd2006-01-23 23:03:36 +00001023 if (CA->isString()) {
Chris Lattner1e194682002-04-18 18:53:13 +00001024 Out << "c\"";
Chris Lattner6ed87bd2006-01-23 23:03:36 +00001025 PrintEscapedString(CA->getAsString(), Out);
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001026 Out << '"';
Chris Lattner1e194682002-04-18 18:53:13 +00001027 } else { // Cannot output in string format...
Misha Brukman21bbdb92004-06-04 21:11:51 +00001028 Out << '[';
Chris Lattnerd84bb632002-04-16 21:36:08 +00001029 if (CA->getNumOperands()) {
Chris Lattnere101c442009-02-28 21:26:53 +00001030 TypePrinter.print(ETy, Out);
Dan Gohman81313fd2008-09-14 17:21:12 +00001031 Out << ' ';
Chris Lattnerd84bb632002-04-16 21:36:08 +00001032 WriteAsOperandInternal(Out, CA->getOperand(0),
Chris Lattner0f578952009-02-28 20:25:14 +00001033 TypePrinter, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +00001034 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
1035 Out << ", ";
Chris Lattnere101c442009-02-28 21:26:53 +00001036 TypePrinter.print(ETy, Out);
Dan Gohman81313fd2008-09-14 17:21:12 +00001037 Out << ' ';
Chris Lattner0f578952009-02-28 20:25:14 +00001038 WriteAsOperandInternal(Out, CA->getOperand(i), TypePrinter, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +00001039 }
1040 }
Dan Gohman81313fd2008-09-14 17:21:12 +00001041 Out << ']';
Chris Lattnerd84bb632002-04-16 21:36:08 +00001042 }
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001043 return;
1044 }
1045
1046 if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
Andrew Lenharth0d124b82007-01-08 18:21:30 +00001047 if (CS->getType()->isPacked())
1048 Out << '<';
Misha Brukman21bbdb92004-06-04 21:11:51 +00001049 Out << '{';
Jim Laskey3bb78742006-02-25 12:27:03 +00001050 unsigned N = CS->getNumOperands();
1051 if (N) {
Chris Lattner604e3512008-08-19 04:47:09 +00001052 Out << ' ';
Chris Lattnere101c442009-02-28 21:26:53 +00001053 TypePrinter.print(CS->getOperand(0)->getType(), Out);
Dan Gohman81313fd2008-09-14 17:21:12 +00001054 Out << ' ';
Chris Lattnerd84bb632002-04-16 21:36:08 +00001055
Chris Lattner0f578952009-02-28 20:25:14 +00001056 WriteAsOperandInternal(Out, CS->getOperand(0), TypePrinter, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +00001057
Jim Laskey3bb78742006-02-25 12:27:03 +00001058 for (unsigned i = 1; i < N; i++) {
Chris Lattnerd84bb632002-04-16 21:36:08 +00001059 Out << ", ";
Chris Lattnere101c442009-02-28 21:26:53 +00001060 TypePrinter.print(CS->getOperand(i)->getType(), Out);
Dan Gohman81313fd2008-09-14 17:21:12 +00001061 Out << ' ';
Chris Lattnerd84bb632002-04-16 21:36:08 +00001062
Chris Lattner0f578952009-02-28 20:25:14 +00001063 WriteAsOperandInternal(Out, CS->getOperand(i), TypePrinter, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +00001064 }
Dan Gohman81313fd2008-09-14 17:21:12 +00001065 Out << ' ';
Chris Lattnerd84bb632002-04-16 21:36:08 +00001066 }
Jim Laskey3bb78742006-02-25 12:27:03 +00001067
Dan Gohman81313fd2008-09-14 17:21:12 +00001068 Out << '}';
Andrew Lenharth0d124b82007-01-08 18:21:30 +00001069 if (CS->getType()->isPacked())
1070 Out << '>';
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001071 return;
1072 }
1073
1074 if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
1075 const Type *ETy = CP->getType()->getElementType();
1076 assert(CP->getNumOperands() > 0 &&
1077 "Number of operands for a PackedConst must be > 0");
Dan Gohman1262a252009-02-11 00:25:25 +00001078 Out << '<';
Chris Lattnere101c442009-02-28 21:26:53 +00001079 TypePrinter.print(ETy, Out);
Dan Gohman81313fd2008-09-14 17:21:12 +00001080 Out << ' ';
Chris Lattner0f578952009-02-28 20:25:14 +00001081 WriteAsOperandInternal(Out, CP->getOperand(0), TypePrinter, Machine);
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001082 for (unsigned i = 1, e = CP->getNumOperands(); i != e; ++i) {
Chris Lattner585297e82008-08-19 05:26:17 +00001083 Out << ", ";
Chris Lattnere101c442009-02-28 21:26:53 +00001084 TypePrinter.print(ETy, Out);
Dan Gohman81313fd2008-09-14 17:21:12 +00001085 Out << ' ';
Chris Lattner0f578952009-02-28 20:25:14 +00001086 WriteAsOperandInternal(Out, CP->getOperand(i), TypePrinter, Machine);
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001087 }
Dan Gohman1262a252009-02-11 00:25:25 +00001088 Out << '>';
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001089 return;
1090 }
1091
1092 if (isa<ConstantPointerNull>(CV)) {
Chris Lattnerd84bb632002-04-16 21:36:08 +00001093 Out << "null";
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001094 return;
1095 }
1096
1097 if (isa<UndefValue>(CV)) {
Chris Lattner5e0b9f22004-10-16 18:08:06 +00001098 Out << "undef";
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001099 return;
1100 }
Nick Lewycky49f89192009-04-04 07:22:01 +00001101
Devang Patel983c6b12009-07-08 21:44:25 +00001102 if (const MDNode *Node = dyn_cast<MDNode>(CV)) {
1103 Out << "!" << Machine->getMetadataSlot(Node);
1104 return;
1105 }
1106
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001107 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Reid Spencer812a1be2006-12-04 05:19:18 +00001108 Out << CE->getOpcodeName();
Dan Gohman9c7f8082009-07-27 16:11:46 +00001109 WriteOptimizationInfo(Out, CE);
Reid Spencer812a1be2006-12-04 05:19:18 +00001110 if (CE->isCompare())
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001111 Out << ' ' << getPredicateText(CE->getPredicate());
Reid Spencer812a1be2006-12-04 05:19:18 +00001112 Out << " (";
Misha Brukmanb1c93172005-04-21 23:48:37 +00001113
Vikram S. Adveb952b542002-07-14 23:14:45 +00001114 for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) {
Chris Lattnere101c442009-02-28 21:26:53 +00001115 TypePrinter.print((*OI)->getType(), Out);
Dan Gohman81313fd2008-09-14 17:21:12 +00001116 Out << ' ';
Chris Lattner0f578952009-02-28 20:25:14 +00001117 WriteAsOperandInternal(Out, *OI, TypePrinter, Machine);
Vikram S. Adveb952b542002-07-14 23:14:45 +00001118 if (OI+1 != CE->op_end())
Chris Lattner3cd8c562002-07-30 18:54:25 +00001119 Out << ", ";
Vikram S. Adveb952b542002-07-14 23:14:45 +00001120 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001121
Dan Gohmana76f0f72008-05-31 19:12:39 +00001122 if (CE->hasIndices()) {
1123 const SmallVector<unsigned, 4> &Indices = CE->getIndices();
1124 for (unsigned i = 0, e = Indices.size(); i != e; ++i)
1125 Out << ", " << Indices[i];
1126 }
1127
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001128 if (CE->isCast()) {
Chris Lattner83b396b2002-08-15 19:37:43 +00001129 Out << " to ";
Chris Lattnere101c442009-02-28 21:26:53 +00001130 TypePrinter.print(CE->getType(), Out);
Chris Lattner83b396b2002-08-15 19:37:43 +00001131 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001132
Misha Brukman21bbdb92004-06-04 21:11:51 +00001133 Out << ')';
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001134 return;
Chris Lattnerd84bb632002-04-16 21:36:08 +00001135 }
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001136
1137 Out << "<placeholder or erroneous Constant>";
Chris Lattnerd84bb632002-04-16 21:36:08 +00001138}
1139
1140
Misha Brukmanc566ca362004-03-02 00:22:19 +00001141/// WriteAsOperand - Write the name of the specified value out to the specified
1142/// ostream. This can be useful when you just want to print int %reg126, not
1143/// the whole instruction that generated it.
1144///
Chris Lattner0c19df42008-08-23 22:23:09 +00001145static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
Chris Lattner0f578952009-02-28 20:25:14 +00001146 TypePrinting &TypePrinter,
Chris Lattnere36fd8a2008-08-19 04:26:57 +00001147 SlotTracker *Machine) {
Chris Lattner033935d2008-08-17 04:40:13 +00001148 if (V->hasName()) {
1149 PrintLLVMName(Out, V);
1150 return;
1151 }
1152
1153 const Constant *CV = dyn_cast<Constant>(V);
1154 if (CV && !isa<GlobalValue>(CV)) {
Chris Lattner0f578952009-02-28 20:25:14 +00001155 WriteConstantInt(Out, CV, TypePrinter, Machine);
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001156 return;
1157 }
1158
1159 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
Chris Lattner033935d2008-08-17 04:40:13 +00001160 Out << "asm ";
1161 if (IA->hasSideEffects())
1162 Out << "sideeffect ";
1163 Out << '"';
1164 PrintEscapedString(IA->getAsmString(), Out);
1165 Out << "\", \"";
1166 PrintEscapedString(IA->getConstraintString(), Out);
1167 Out << '"';
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001168 return;
1169 }
Devang Patel7428d8a2009-07-22 17:43:22 +00001170
Devang Patele059ba6e2009-07-23 01:07:34 +00001171 if (const MDNode *N = dyn_cast<MDNode>(V)) {
1172 Out << '!' << Machine->getMetadataSlot(N);
1173 return;
1174 }
1175
Devang Patel7428d8a2009-07-22 17:43:22 +00001176 if (const MDString *MDS = dyn_cast<MDString>(V)) {
Devang Patel7428d8a2009-07-22 17:43:22 +00001177 Out << "!\"";
Daniel Dunbare03eecb2009-07-25 23:55:21 +00001178 PrintEscapedString(MDS->getString(), Out);
Devang Patel7428d8a2009-07-22 17:43:22 +00001179 Out << '"';
1180 return;
1181 }
1182
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001183 char Prefix = '%';
1184 int Slot;
1185 if (Machine) {
1186 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1187 Slot = Machine->getGlobalSlot(GV);
1188 Prefix = '@';
1189 } else {
1190 Slot = Machine->getLocalSlot(V);
1191 }
Chris Lattner033935d2008-08-17 04:40:13 +00001192 } else {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001193 Machine = createSlotTracker(V);
Chris Lattner033935d2008-08-17 04:40:13 +00001194 if (Machine) {
1195 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1196 Slot = Machine->getGlobalSlot(GV);
1197 Prefix = '@';
1198 } else {
1199 Slot = Machine->getLocalSlot(V);
1200 }
Chris Lattnera2d810d2006-01-25 22:26:05 +00001201 } else {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001202 Slot = -1;
Chris Lattnerd84bb632002-04-16 21:36:08 +00001203 }
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001204 delete Machine;
Chris Lattnerd84bb632002-04-16 21:36:08 +00001205 }
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001206
1207 if (Slot != -1)
1208 Out << Prefix << Slot;
1209 else
1210 Out << "<badref>";
Chris Lattnerd84bb632002-04-16 21:36:08 +00001211}
1212
Misha Brukmanb22d09c2004-03-01 19:48:13 +00001213/// WriteAsOperand - Write the name of the specified value out to the specified
1214/// ostream. This can be useful when you just want to print int %reg126, not
1215/// the whole instruction that generated it.
1216///
Chris Lattner604e3512008-08-19 04:47:09 +00001217void llvm::WriteAsOperand(std::ostream &Out, const Value *V, bool PrintType,
1218 const Module *Context) {
Chris Lattner0c19df42008-08-23 22:23:09 +00001219 raw_os_ostream OS(Out);
1220 WriteAsOperand(OS, V, PrintType, Context);
1221}
1222
1223void llvm::WriteAsOperand(raw_ostream &Out, const Value *V, bool PrintType,
1224 const Module *Context) {
Chris Lattner5a9f63e2002-07-10 16:48:17 +00001225 if (Context == 0) Context = getModuleFromVal(V);
Chris Lattnerb86620e2001-10-29 16:37:48 +00001226
Chris Lattner92c5c122009-02-28 23:20:19 +00001227 TypePrinting TypePrinter;
Chris Lattner3243ea12009-03-01 00:03:38 +00001228 std::vector<const Type*> NumberedTypes;
1229 AddModuleTypesToPrinter(TypePrinter, NumberedTypes, Context);
Dan Gohman81313fd2008-09-14 17:21:12 +00001230 if (PrintType) {
Chris Lattnere101c442009-02-28 21:26:53 +00001231 TypePrinter.print(V->getType(), Out);
Dan Gohman81313fd2008-09-14 17:21:12 +00001232 Out << ' ';
1233 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001234
Chris Lattner0f578952009-02-28 20:25:14 +00001235 WriteAsOperandInternal(Out, V, TypePrinter, 0);
Chris Lattner5e5abe32001-07-20 19:15:21 +00001236}
1237
Reid Spencer58d30f22004-07-04 11:50:43 +00001238
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001239namespace {
Chris Lattner2e9fee42001-07-12 23:35:26 +00001240
Chris Lattnerfee714f2001-09-07 16:36:04 +00001241class AssemblyWriter {
Chris Lattner0c19df42008-08-23 22:23:09 +00001242 raw_ostream &Out;
Chris Lattnere36fd8a2008-08-19 04:26:57 +00001243 SlotTracker &Machine;
Chris Lattner7bfee412001-10-29 16:05:51 +00001244 const Module *TheModule;
Chris Lattner0f578952009-02-28 20:25:14 +00001245 TypePrinting TypePrinter;
Chris Lattner8339f7d2003-10-30 23:41:03 +00001246 AssemblyAnnotationWriter *AnnotationWriter;
Chris Lattner3243ea12009-03-01 00:03:38 +00001247 std::vector<const Type*> NumberedTypes;
Devang Patel39e64d42009-07-01 19:21:12 +00001248
1249 // Each MDNode is assigned unique MetadataIDNo.
1250 std::map<const MDNode *, unsigned> MDNodes;
1251 unsigned MetadataIDNo;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001252public:
Chris Lattner0c19df42008-08-23 22:23:09 +00001253 inline AssemblyWriter(raw_ostream &o, SlotTracker &Mac, const Module *M,
Chris Lattner8339f7d2003-10-30 23:41:03 +00001254 AssemblyAnnotationWriter *AAW)
Devang Patel39e64d42009-07-01 19:21:12 +00001255 : Out(o), Machine(Mac), TheModule(M), AnnotationWriter(AAW), MetadataIDNo(0) {
Chris Lattner3243ea12009-03-01 00:03:38 +00001256 AddModuleTypesToPrinter(TypePrinter, NumberedTypes, M);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001257 }
1258
Chris Lattner3243ea12009-03-01 00:03:38 +00001259 void write(const Module *M) { printModule(M); }
Chris Lattner0c19df42008-08-23 22:23:09 +00001260
1261 void write(const GlobalValue *G) {
1262 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(G))
1263 printGlobal(GV);
1264 else if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(G))
1265 printAlias(GA);
1266 else if (const Function *F = dyn_cast<Function>(G))
1267 printFunction(F);
1268 else
Torok Edwinfbcc6632009-07-14 16:55:14 +00001269 llvm_unreachable("Unknown global");
Chris Lattner0c19df42008-08-23 22:23:09 +00001270 }
1271
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001272 void write(const BasicBlock *BB) { printBasicBlock(BB); }
1273 void write(const Instruction *I) { printInstruction(*I); }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001274
Chris Lattner78e2e8b2006-12-06 06:24:27 +00001275 void writeOperand(const Value *Op, bool PrintType);
Devang Patelba3fa6c2008-09-23 23:03:40 +00001276 void writeParamOperand(const Value *Operand, Attributes Attrs);
Chris Lattner1e194682002-04-18 18:53:13 +00001277
Misha Brukman4685e262004-04-28 15:31:21 +00001278 const Module* getModule() { return TheModule; }
1279
Misha Brukmand92f54a2004-11-15 19:30:05 +00001280private:
Chris Lattner7bfee412001-10-29 16:05:51 +00001281 void printModule(const Module *M);
Reid Spencer32af9e82007-01-06 07:24:44 +00001282 void printTypeSymbolTable(const TypeSymbolTable &ST);
Chris Lattner7bfee412001-10-29 16:05:51 +00001283 void printGlobal(const GlobalVariable *GV);
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001284 void printAlias(const GlobalAlias *GV);
Chris Lattner57698e22002-03-26 18:01:55 +00001285 void printFunction(const Function *F);
Devang Patelba3fa6c2008-09-23 23:03:40 +00001286 void printArgument(const Argument *FA, Attributes Attrs);
Chris Lattner7bfee412001-10-29 16:05:51 +00001287 void printBasicBlock(const BasicBlock *BB);
Chris Lattner113f4f42002-06-25 16:13:24 +00001288 void printInstruction(const Instruction &I);
Chris Lattnerd816b532002-04-13 20:53:41 +00001289
Chris Lattner862e3382001-10-13 06:42:36 +00001290 // printInfoComment - Print a little comment after the instruction indicating
1291 // which slot it occupies.
Chris Lattner113f4f42002-06-25 16:13:24 +00001292 void printInfoComment(const Value &V);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001293};
Chris Lattner3243ea12009-03-01 00:03:38 +00001294} // end of anonymous namespace
Chris Lattner2f7c9632001-06-06 20:29:01 +00001295
Chris Lattnerd816b532002-04-13 20:53:41 +00001296
Chris Lattner78e2e8b2006-12-06 06:24:27 +00001297void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType) {
1298 if (Operand == 0) {
Chris Lattner08f7d0c2005-02-24 16:58:29 +00001299 Out << "<null operand!>";
Chris Lattner78e2e8b2006-12-06 06:24:27 +00001300 } else {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001301 if (PrintType) {
Chris Lattnere101c442009-02-28 21:26:53 +00001302 TypePrinter.print(Operand->getType(), Out);
Dan Gohman81313fd2008-09-14 17:21:12 +00001303 Out << ' ';
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001304 }
Chris Lattner0f578952009-02-28 20:25:14 +00001305 WriteAsOperandInternal(Out, Operand, TypePrinter, &Machine);
Chris Lattner08f7d0c2005-02-24 16:58:29 +00001306 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001307}
1308
Dale Johannesen89268bc2008-02-19 21:38:47 +00001309void AssemblyWriter::writeParamOperand(const Value *Operand,
Devang Patelba3fa6c2008-09-23 23:03:40 +00001310 Attributes Attrs) {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00001311 if (Operand == 0) {
1312 Out << "<null operand!>";
1313 } else {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00001314 // Print the type
Chris Lattnere101c442009-02-28 21:26:53 +00001315 TypePrinter.print(Operand->getType(), Out);
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00001316 // Print parameter attributes list
Devang Patel4c758ea2008-09-25 21:00:45 +00001317 if (Attrs != Attribute::None)
1318 Out << ' ' << Attribute::getAsString(Attrs);
Dan Gohman81313fd2008-09-14 17:21:12 +00001319 Out << ' ';
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00001320 // Print the operand
Chris Lattner0f578952009-02-28 20:25:14 +00001321 WriteAsOperandInternal(Out, Operand, TypePrinter, &Machine);
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00001322 }
1323}
Chris Lattner2f7c9632001-06-06 20:29:01 +00001324
Chris Lattner7bfee412001-10-29 16:05:51 +00001325void AssemblyWriter::printModule(const Module *M) {
Chris Lattner4d8689e2005-03-02 23:12:40 +00001326 if (!M->getModuleIdentifier().empty() &&
Misha Brukmanb1c93172005-04-21 23:48:37 +00001327 // Don't print the ID if it will start a new line (which would
Chris Lattner4d8689e2005-03-02 23:12:40 +00001328 // require a comment char before it).
1329 M->getModuleIdentifier().find('\n') == std::string::npos)
1330 Out << "; ModuleID = '" << M->getModuleIdentifier() << "'\n";
1331
Owen Andersone2237542006-10-18 02:21:12 +00001332 if (!M->getDataLayout().empty())
Chris Lattner04897162006-10-22 06:06:56 +00001333 Out << "target datalayout = \"" << M->getDataLayout() << "\"\n";
Reid Spencer48f98c82004-07-25 21:44:54 +00001334 if (!M->getTargetTriple().empty())
Reid Spencerffec7df2004-07-25 21:29:43 +00001335 Out << "target triple = \"" << M->getTargetTriple() << "\"\n";
Misha Brukmanb1c93172005-04-21 23:48:37 +00001336
Chris Lattnereef2fe72006-01-24 04:13:11 +00001337 if (!M->getModuleInlineAsm().empty()) {
Chris Lattnerefaf35d2006-01-24 00:45:30 +00001338 // Split the string into lines, to make it easier to read the .ll file.
Chris Lattnereef2fe72006-01-24 04:13:11 +00001339 std::string Asm = M->getModuleInlineAsm();
Chris Lattnerefaf35d2006-01-24 00:45:30 +00001340 size_t CurPos = 0;
1341 size_t NewLine = Asm.find_first_of('\n', CurPos);
1342 while (NewLine != std::string::npos) {
1343 // We found a newline, print the portion of the asm string from the
1344 // last newline up to this newline.
1345 Out << "module asm \"";
1346 PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.begin()+NewLine),
1347 Out);
1348 Out << "\"\n";
1349 CurPos = NewLine+1;
1350 NewLine = Asm.find_first_of('\n', CurPos);
1351 }
Chris Lattner3acaf5c2006-01-24 00:40:17 +00001352 Out << "module asm \"";
Chris Lattnerefaf35d2006-01-24 00:45:30 +00001353 PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.end()), Out);
Chris Lattner6ed87bd2006-01-23 23:03:36 +00001354 Out << "\"\n";
1355 }
1356
Chris Lattner2cdd49d2004-09-14 05:06:58 +00001357 // Loop over the dependent libraries and emit them.
Chris Lattner730cfe42004-09-14 04:51:44 +00001358 Module::lib_iterator LI = M->lib_begin();
1359 Module::lib_iterator LE = M->lib_end();
Reid Spencer48f98c82004-07-25 21:44:54 +00001360 if (LI != LE) {
Chris Lattner730cfe42004-09-14 04:51:44 +00001361 Out << "deplibs = [ ";
1362 while (LI != LE) {
Chris Lattner2cdd49d2004-09-14 05:06:58 +00001363 Out << '"' << *LI << '"';
Reid Spencerffec7df2004-07-25 21:29:43 +00001364 ++LI;
Chris Lattner730cfe42004-09-14 04:51:44 +00001365 if (LI != LE)
1366 Out << ", ";
Reid Spencerffec7df2004-07-25 21:29:43 +00001367 }
1368 Out << " ]\n";
Reid Spencercc5ff642004-07-25 18:08:18 +00001369 }
Reid Spencerb9e08772004-09-13 23:44:23 +00001370
Chris Lattner3243ea12009-03-01 00:03:38 +00001371 // Loop over the symbol table, emitting all id'd types.
Reid Spencer32af9e82007-01-06 07:24:44 +00001372 printTypeSymbolTable(M->getTypeSymbolTable());
Misha Brukmanb1c93172005-04-21 23:48:37 +00001373
Chris Lattner54932b02006-12-06 04:41:52 +00001374 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
1375 I != E; ++I)
Chris Lattner113f4f42002-06-25 16:13:24 +00001376 printGlobal(I);
Chris Lattnerd2747052007-04-26 02:24:10 +00001377
1378 // Output all aliases.
1379 if (!M->alias_empty()) Out << "\n";
1380 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
1381 I != E; ++I)
1382 printAlias(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +00001383
Chris Lattner2cdd49d2004-09-14 05:06:58 +00001384 // Output all of the functions.
Chris Lattner113f4f42002-06-25 16:13:24 +00001385 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
1386 printFunction(I);
Devang Patel983c6b12009-07-08 21:44:25 +00001387
Devang Patel23e68302009-07-29 22:04:47 +00001388 // Output named metadata.
1389 for (Module::const_named_metadata_iterator I = M->named_metadata_begin(),
1390 E = M->named_metadata_end(); I != E; ++I) {
1391 const NamedMDNode *NMD = I;
1392 Out << "!" << NMD->getName() << " = !{";
1393 for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i) {
1394 if (i) Out << ", ";
1395 MDNode *MD = cast<MDNode>(NMD->getElement(i));
1396 Out << '!' << Machine.getMetadataSlot(MD);
1397 }
1398 Out << "}\n";
1399 }
1400
1401 // Output metadata.
Devang Patel983c6b12009-07-08 21:44:25 +00001402 WriteMDNodes(Out, TypePrinter, Machine);
Chris Lattnerfee714f2001-09-07 16:36:04 +00001403}
1404
Chris Lattner0c19df42008-08-23 22:23:09 +00001405static void PrintLinkage(GlobalValue::LinkageTypes LT, raw_ostream &Out) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001406 switch (LT) {
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001407 case GlobalValue::ExternalLinkage: break;
1408 case GlobalValue::PrivateLinkage: Out << "private "; break;
1409 case GlobalValue::LinkerPrivateLinkage: Out << "linker_private "; break;
1410 case GlobalValue::InternalLinkage: Out << "internal "; break;
1411 case GlobalValue::LinkOnceAnyLinkage: Out << "linkonce "; break;
1412 case GlobalValue::LinkOnceODRLinkage: Out << "linkonce_odr "; break;
1413 case GlobalValue::WeakAnyLinkage: Out << "weak "; break;
1414 case GlobalValue::WeakODRLinkage: Out << "weak_odr "; break;
1415 case GlobalValue::CommonLinkage: Out << "common "; break;
1416 case GlobalValue::AppendingLinkage: Out << "appending "; break;
1417 case GlobalValue::DLLImportLinkage: Out << "dllimport "; break;
1418 case GlobalValue::DLLExportLinkage: Out << "dllexport "; break;
1419 case GlobalValue::ExternalWeakLinkage: Out << "extern_weak "; break;
Chris Lattner184f1be2009-04-13 05:44:34 +00001420 case GlobalValue::AvailableExternallyLinkage:
1421 Out << "available_externally ";
1422 break;
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001423 case GlobalValue::GhostLinkage:
Torok Edwinfbcc6632009-07-14 16:55:14 +00001424 llvm_unreachable("GhostLinkage not allowed in AsmWriter!");
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001425 }
1426}
Duncan Sands12da8ce2009-03-07 15:45:40 +00001427
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001428
1429static void PrintVisibility(GlobalValue::VisibilityTypes Vis,
Chris Lattner0c19df42008-08-23 22:23:09 +00001430 raw_ostream &Out) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001431 switch (Vis) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001432 default: llvm_unreachable("Invalid visibility style!");
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001433 case GlobalValue::DefaultVisibility: break;
1434 case GlobalValue::HiddenVisibility: Out << "hidden "; break;
1435 case GlobalValue::ProtectedVisibility: Out << "protected "; break;
1436 }
1437}
1438
Chris Lattner7bfee412001-10-29 16:05:51 +00001439void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Chris Lattner033935d2008-08-17 04:40:13 +00001440 if (GV->hasName()) {
1441 PrintLLVMName(Out, GV);
1442 Out << " = ";
1443 }
Chris Lattner37798642001-09-18 04:01:05 +00001444
Chris Lattner1508d3f2008-08-19 05:16:28 +00001445 if (!GV->hasInitializer() && GV->hasExternalLinkage())
1446 Out << "external ";
1447
1448 PrintLinkage(GV->getLinkage(), Out);
1449 PrintVisibility(GV->getVisibility(), Out);
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +00001450
1451 if (GV->isThreadLocal()) Out << "thread_local ";
Chris Lattnerac161bf2009-01-02 07:01:27 +00001452 if (unsigned AddressSpace = GV->getType()->getAddressSpace())
1453 Out << "addrspace(" << AddressSpace << ") ";
Misha Brukmana6619a92004-06-21 21:53:56 +00001454 Out << (GV->isConstant() ? "constant " : "global ");
Chris Lattnere101c442009-02-28 21:26:53 +00001455 TypePrinter.print(GV->getType()->getElementType(), Out);
Chris Lattner37798642001-09-18 04:01:05 +00001456
Dan Gohman81313fd2008-09-14 17:21:12 +00001457 if (GV->hasInitializer()) {
1458 Out << ' ';
Devang Patel983c6b12009-07-08 21:44:25 +00001459 writeOperand(GV->getInitializer(), false);
Dan Gohman81313fd2008-09-14 17:21:12 +00001460 }
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001461
Chris Lattner4b96c542005-11-12 00:10:19 +00001462 if (GV->hasSection())
1463 Out << ", section \"" << GV->getSection() << '"';
1464 if (GV->getAlignment())
Chris Lattnerf8a974d2005-11-06 06:48:53 +00001465 Out << ", align " << GV->getAlignment();
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001466
Chris Lattner113f4f42002-06-25 16:13:24 +00001467 printInfoComment(*GV);
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001468 Out << '\n';
Chris Lattnerda975502001-09-10 07:58:01 +00001469}
1470
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001471void AssemblyWriter::printAlias(const GlobalAlias *GA) {
Dale Johannesen83e468a2008-06-03 18:14:29 +00001472 // Don't crash when dumping partially built GA
1473 if (!GA->hasName())
1474 Out << "<<nameless>> = ";
Chris Lattner033935d2008-08-17 04:40:13 +00001475 else {
1476 PrintLLVMName(Out, GA);
1477 Out << " = ";
1478 }
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001479 PrintVisibility(GA->getVisibility(), Out);
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001480
1481 Out << "alias ";
1482
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001483 PrintLinkage(GA->getLinkage(), Out);
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001484
Anton Korobeynikov546ea7e2007-04-29 18:02:48 +00001485 const Constant *Aliasee = GA->getAliasee();
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001486
1487 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Aliasee)) {
Chris Lattnere101c442009-02-28 21:26:53 +00001488 TypePrinter.print(GV->getType(), Out);
Chris Lattner033935d2008-08-17 04:40:13 +00001489 Out << ' ';
1490 PrintLLVMName(Out, GV);
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001491 } else if (const Function *F = dyn_cast<Function>(Aliasee)) {
Chris Lattnere101c442009-02-28 21:26:53 +00001492 TypePrinter.print(F->getFunctionType(), Out);
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001493 Out << "* ";
1494
Chris Lattner0f578952009-02-28 20:25:14 +00001495 WriteAsOperandInternal(Out, F, TypePrinter, &Machine);
Anton Korobeynikov72d5d422008-03-22 08:17:17 +00001496 } else if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(Aliasee)) {
Chris Lattnere101c442009-02-28 21:26:53 +00001497 TypePrinter.print(GA->getType(), Out);
1498 Out << ' ';
Chris Lattner033935d2008-08-17 04:40:13 +00001499 PrintLLVMName(Out, GA);
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +00001500 } else {
Chris Lattnercde89e42009-04-25 21:23:19 +00001501 const ConstantExpr *CE = cast<ConstantExpr>(Aliasee);
1502 // The only valid GEP is an all zero GEP.
1503 assert((CE->getOpcode() == Instruction::BitCast ||
1504 CE->getOpcode() == Instruction::GetElementPtr) &&
1505 "Unsupported aliasee");
1506 writeOperand(CE, false);
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +00001507 }
1508
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001509 printInfoComment(*GA);
Chris Lattner1508d3f2008-08-19 05:16:28 +00001510 Out << '\n';
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001511}
1512
Reid Spencer32af9e82007-01-06 07:24:44 +00001513void AssemblyWriter::printTypeSymbolTable(const TypeSymbolTable &ST) {
Chris Lattner3243ea12009-03-01 00:03:38 +00001514 // Emit all numbered types.
1515 for (unsigned i = 0, e = NumberedTypes.size(); i != e; ++i) {
1516 Out << "\ttype ";
1517
1518 // Make sure we print out at least one level of the type structure, so
1519 // that we do not get %2 = type %2
1520 TypePrinter.printAtLeastOneLevel(NumberedTypes[i], Out);
1521 Out << "\t\t; type %" << i << '\n';
1522 }
1523
1524 // Print the named types.
Reid Spencer32af9e82007-01-06 07:24:44 +00001525 for (TypeSymbolTable::const_iterator TI = ST.begin(), TE = ST.end();
1526 TI != TE; ++TI) {
Chris Lattner1508d3f2008-08-19 05:16:28 +00001527 Out << '\t';
Daniel Dunbare03eecb2009-07-25 23:55:21 +00001528 PrintLLVMName(Out, TI->first, LocalPrefix);
Chris Lattner1508d3f2008-08-19 05:16:28 +00001529 Out << " = type ";
Reid Spencere7e96712004-05-25 08:53:40 +00001530
1531 // Make sure we print out at least one level of the type structure, so
1532 // that we do not get %FILE = type %FILE
Chris Lattnere101c442009-02-28 21:26:53 +00001533 TypePrinter.printAtLeastOneLevel(TI->second, Out);
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001534 Out << '\n';
Reid Spencere7e96712004-05-25 08:53:40 +00001535 }
Reid Spencer32af9e82007-01-06 07:24:44 +00001536}
1537
Misha Brukmanc566ca362004-03-02 00:22:19 +00001538/// printFunction - Print all aspects of a function.
1539///
Chris Lattner113f4f42002-06-25 16:13:24 +00001540void AssemblyWriter::printFunction(const Function *F) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001541 // Print out the return type and name.
1542 Out << '\n';
Chris Lattner379a8d22003-04-16 20:28:45 +00001543
Misha Brukmana6619a92004-06-21 21:53:56 +00001544 if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001545
Reid Spencer5301e7c2007-01-30 20:08:39 +00001546 if (F->isDeclaration())
Chris Lattner10f03a62007-08-19 22:15:26 +00001547 Out << "declare ";
1548 else
Reid Spencer7ce2d2a2006-12-29 20:29:48 +00001549 Out << "define ";
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001550
1551 PrintLinkage(F->getLinkage(), Out);
1552 PrintVisibility(F->getVisibility(), Out);
Chris Lattner379a8d22003-04-16 20:28:45 +00001553
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001554 // Print the calling convention.
1555 switch (F->getCallingConv()) {
1556 case CallingConv::C: break; // default
Anton Korobeynikov3c5b3df2006-09-20 22:03:51 +00001557 case CallingConv::Fast: Out << "fastcc "; break;
1558 case CallingConv::Cold: Out << "coldcc "; break;
1559 case CallingConv::X86_StdCall: Out << "x86_stdcallcc "; break;
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001560 case CallingConv::X86_FastCall: Out << "x86_fastcallcc "; break;
1561 case CallingConv::ARM_APCS: Out << "arm_apcscc "; break;
1562 case CallingConv::ARM_AAPCS: Out << "arm_aapcscc "; break;
1563 case CallingConv::ARM_AAPCS_VFP:Out << "arm_aapcs_vfpcc "; break;
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001564 default: Out << "cc" << F->getCallingConv() << " "; break;
1565 }
1566
Reid Spencer8c4914c2006-12-31 05:24:50 +00001567 const FunctionType *FT = F->getFunctionType();
Devang Patel4c758ea2008-09-25 21:00:45 +00001568 const AttrListPtr &Attrs = F->getAttributes();
Devang Patel221fe422008-09-29 20:49:50 +00001569 Attributes RetAttrs = Attrs.getRetAttributes();
1570 if (RetAttrs != Attribute::None)
1571 Out << Attribute::getAsString(Attrs.getRetAttributes()) << ' ';
Chris Lattnere101c442009-02-28 21:26:53 +00001572 TypePrinter.print(F->getReturnType(), Out);
Chris Lattner585297e82008-08-19 05:26:17 +00001573 Out << ' ';
Chris Lattner0f578952009-02-28 20:25:14 +00001574 WriteAsOperandInternal(Out, F, TypePrinter, &Machine);
Misha Brukmana6619a92004-06-21 21:53:56 +00001575 Out << '(';
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001576 Machine.incorporateFunction(F);
Chris Lattnerfee714f2001-09-07 16:36:04 +00001577
Chris Lattner7bfee412001-10-29 16:05:51 +00001578 // Loop over the arguments, printing them...
Chris Lattnerfee714f2001-09-07 16:36:04 +00001579
Reid Spencer8c4914c2006-12-31 05:24:50 +00001580 unsigned Idx = 1;
Chris Lattner82738fe2007-04-18 00:57:22 +00001581 if (!F->isDeclaration()) {
1582 // If this isn't a declaration, print the argument names as well.
1583 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
1584 I != E; ++I) {
1585 // Insert commas as we go... the first arg doesn't get a comma
1586 if (I != F->arg_begin()) Out << ", ";
Devang Patela05633e2008-09-26 22:53:05 +00001587 printArgument(I, Attrs.getParamAttributes(Idx));
Chris Lattner82738fe2007-04-18 00:57:22 +00001588 Idx++;
1589 }
1590 } else {
1591 // Otherwise, print the types from the function type.
1592 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
1593 // Insert commas as we go... the first arg doesn't get a comma
1594 if (i) Out << ", ";
1595
1596 // Output type...
Chris Lattnere101c442009-02-28 21:26:53 +00001597 TypePrinter.print(FT->getParamType(i), Out);
Chris Lattner82738fe2007-04-18 00:57:22 +00001598
Devang Patela05633e2008-09-26 22:53:05 +00001599 Attributes ArgAttrs = Attrs.getParamAttributes(i+1);
Devang Patel4c758ea2008-09-25 21:00:45 +00001600 if (ArgAttrs != Attribute::None)
1601 Out << ' ' << Attribute::getAsString(ArgAttrs);
Chris Lattner82738fe2007-04-18 00:57:22 +00001602 }
Reid Spencer8c4914c2006-12-31 05:24:50 +00001603 }
Chris Lattnerfee714f2001-09-07 16:36:04 +00001604
1605 // Finish printing arguments...
Chris Lattner113f4f42002-06-25 16:13:24 +00001606 if (FT->isVarArg()) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001607 if (FT->getNumParams()) Out << ", ";
1608 Out << "..."; // Output varargs portion of signature!
Chris Lattnerfee714f2001-09-07 16:36:04 +00001609 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001610 Out << ')';
Devang Patela05633e2008-09-26 22:53:05 +00001611 Attributes FnAttrs = Attrs.getFnAttributes();
1612 if (FnAttrs != Attribute::None)
1613 Out << ' ' << Attribute::getAsString(Attrs.getFnAttributes());
Chris Lattner4b96c542005-11-12 00:10:19 +00001614 if (F->hasSection())
1615 Out << " section \"" << F->getSection() << '"';
Chris Lattnerf8a974d2005-11-06 06:48:53 +00001616 if (F->getAlignment())
1617 Out << " align " << F->getAlignment();
Gordon Henriksend930f912008-08-17 18:44:35 +00001618 if (F->hasGC())
1619 Out << " gc \"" << F->getGC() << '"';
Reid Spencer5301e7c2007-01-30 20:08:39 +00001620 if (F->isDeclaration()) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001621 Out << "\n";
Chris Lattnerb2f02e52002-05-06 03:00:40 +00001622 } else {
Chris Lattnerd5fbc8f2008-04-21 06:12:55 +00001623 Out << " {";
Misha Brukmanb1c93172005-04-21 23:48:37 +00001624
Chris Lattner6915f8f2002-04-07 22:49:37 +00001625 // Output all of its basic blocks... for the function
Chris Lattner113f4f42002-06-25 16:13:24 +00001626 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
1627 printBasicBlock(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +00001628
Misha Brukmana6619a92004-06-21 21:53:56 +00001629 Out << "}\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +00001630 }
1631
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001632 Machine.purgeFunction();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001633}
1634
Misha Brukmanc566ca362004-03-02 00:22:19 +00001635/// printArgument - This member is called for every argument that is passed into
1636/// the function. Simply print it out
1637///
Dale Johannesen89268bc2008-02-19 21:38:47 +00001638void AssemblyWriter::printArgument(const Argument *Arg,
Devang Patelba3fa6c2008-09-23 23:03:40 +00001639 Attributes Attrs) {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001640 // Output type...
Chris Lattnere101c442009-02-28 21:26:53 +00001641 TypePrinter.print(Arg->getType(), Out);
Misha Brukmanb1c93172005-04-21 23:48:37 +00001642
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00001643 // Output parameter attributes list
Devang Patel4c758ea2008-09-25 21:00:45 +00001644 if (Attrs != Attribute::None)
1645 Out << ' ' << Attribute::getAsString(Attrs);
Reid Spencer8c4914c2006-12-31 05:24:50 +00001646
Chris Lattner2f7c9632001-06-06 20:29:01 +00001647 // Output name, if available...
Chris Lattner033935d2008-08-17 04:40:13 +00001648 if (Arg->hasName()) {
1649 Out << ' ';
1650 PrintLLVMName(Out, Arg);
1651 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001652}
1653
Misha Brukmanc566ca362004-03-02 00:22:19 +00001654/// printBasicBlock - This member is called for each basic block in a method.
1655///
Chris Lattner7bfee412001-10-29 16:05:51 +00001656void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Nick Lewycky4d43d3c2008-04-25 16:53:59 +00001657 if (BB->hasName()) { // Print out the label if it exists...
Chris Lattner033935d2008-08-17 04:40:13 +00001658 Out << "\n";
Daniel Dunbare03eecb2009-07-25 23:55:21 +00001659 PrintLLVMName(Out, BB->getName(), LabelPrefix);
Chris Lattner033935d2008-08-17 04:40:13 +00001660 Out << ':';
Nick Lewycky4d43d3c2008-04-25 16:53:59 +00001661 } else if (!BB->use_empty()) { // Don't print block # of no uses...
Chris Lattner67bec132008-04-21 04:20:33 +00001662 Out << "\n; <label>:";
Chris Lattner5e043322007-01-11 03:54:27 +00001663 int Slot = Machine.getLocalSlot(BB);
Chris Lattner757ee0b2004-06-09 19:41:19 +00001664 if (Slot != -1)
Misha Brukmana6619a92004-06-21 21:53:56 +00001665 Out << Slot;
Chris Lattner757ee0b2004-06-09 19:41:19 +00001666 else
Misha Brukmana6619a92004-06-21 21:53:56 +00001667 Out << "<badref>";
Chris Lattner58185f22002-10-02 19:38:55 +00001668 }
Chris Lattner2447ef52003-11-20 00:09:43 +00001669
1670 if (BB->getParent() == 0)
Misha Brukmana6619a92004-06-21 21:53:56 +00001671 Out << "\t\t; Error: Block without parent!";
Chris Lattnerff834c02008-04-22 02:45:44 +00001672 else if (BB != &BB->getParent()->getEntryBlock()) { // Not the entry block?
1673 // Output predecessors for the block...
1674 Out << "\t\t;";
1675 pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
1676
1677 if (PI == PE) {
1678 Out << " No predecessors!";
1679 } else {
Dan Gohman81313fd2008-09-14 17:21:12 +00001680 Out << " preds = ";
Chris Lattnerff834c02008-04-22 02:45:44 +00001681 writeOperand(*PI, false);
1682 for (++PI; PI != PE; ++PI) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001683 Out << ", ";
Chris Lattner78e2e8b2006-12-06 06:24:27 +00001684 writeOperand(*PI, false);
Chris Lattner00211f12003-11-16 22:59:57 +00001685 }
Chris Lattner58185f22002-10-02 19:38:55 +00001686 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001687 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001688
Chris Lattnerff834c02008-04-22 02:45:44 +00001689 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001690
Misha Brukmana6619a92004-06-21 21:53:56 +00001691 if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001692
Chris Lattnerfee714f2001-09-07 16:36:04 +00001693 // Output all of the instructions in the basic block...
Dan Gohmana4f709e2009-07-13 18:27:59 +00001694 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
Chris Lattner113f4f42002-06-25 16:13:24 +00001695 printInstruction(*I);
Dan Gohmana4f709e2009-07-13 18:27:59 +00001696 Out << '\n';
1697 }
Chris Lattner96cdd272004-03-08 18:51:45 +00001698
Misha Brukmana6619a92004-06-21 21:53:56 +00001699 if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, Out);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001700}
1701
Chris Lattner862e3382001-10-13 06:42:36 +00001702
Misha Brukmanc566ca362004-03-02 00:22:19 +00001703/// printInfoComment - Print a little comment after the instruction indicating
1704/// which slot it occupies.
1705///
Chris Lattner113f4f42002-06-25 16:13:24 +00001706void AssemblyWriter::printInfoComment(const Value &V) {
1707 if (V.getType() != Type::VoidTy) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001708 Out << "\t\t; <";
Chris Lattnere101c442009-02-28 21:26:53 +00001709 TypePrinter.print(V.getType(), Out);
Chris Lattner585297e82008-08-19 05:26:17 +00001710 Out << '>';
Chris Lattner862e3382001-10-13 06:42:36 +00001711
Chris Lattnerb25e5ea2008-08-29 17:19:30 +00001712 if (!V.hasName() && !isa<Instruction>(V)) {
Chris Lattner5e043322007-01-11 03:54:27 +00001713 int SlotNum;
1714 if (const GlobalValue *GV = dyn_cast<GlobalValue>(&V))
1715 SlotNum = Machine.getGlobalSlot(GV);
1716 else
1717 SlotNum = Machine.getLocalSlot(&V);
Chris Lattner757ee0b2004-06-09 19:41:19 +00001718 if (SlotNum == -1)
Misha Brukmana6619a92004-06-21 21:53:56 +00001719 Out << ":<badref>";
Reid Spencer8beac692004-06-09 15:26:53 +00001720 else
Misha Brukmana6619a92004-06-21 21:53:56 +00001721 Out << ':' << SlotNum; // Print out the def slot taken.
Chris Lattner862e3382001-10-13 06:42:36 +00001722 }
Chris Lattnerb6c21db2005-02-01 01:24:01 +00001723 Out << " [#uses=" << V.getNumUses() << ']'; // Output # uses
Chris Lattner862e3382001-10-13 06:42:36 +00001724 }
1725}
1726
Reid Spencere7141c82006-08-28 01:02:49 +00001727// This member is called for each Instruction in a function..
Chris Lattner113f4f42002-06-25 16:13:24 +00001728void AssemblyWriter::printInstruction(const Instruction &I) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001729 if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001730
Chris Lattner82ff9232008-08-23 22:52:27 +00001731 Out << '\t';
Chris Lattner2f7c9632001-06-06 20:29:01 +00001732
1733 // Print out name if it exists...
Chris Lattner033935d2008-08-17 04:40:13 +00001734 if (I.hasName()) {
1735 PrintLLVMName(Out, &I);
1736 Out << " = ";
Chris Lattnerb25e5ea2008-08-29 17:19:30 +00001737 } else if (I.getType() != Type::VoidTy) {
1738 // Print out the def slot taken.
1739 int SlotNum = Machine.getLocalSlot(&I);
1740 if (SlotNum == -1)
1741 Out << "<badref> = ";
1742 else
1743 Out << '%' << SlotNum << " = ";
Chris Lattner033935d2008-08-17 04:40:13 +00001744 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001745
Chris Lattner06038452005-05-06 05:51:46 +00001746 // If this is a volatile load or store, print out the volatile marker.
Chris Lattner504f9242003-09-08 17:45:59 +00001747 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) ||
Chris Lattner06038452005-05-06 05:51:46 +00001748 (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile())) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001749 Out << "volatile ";
Chris Lattner06038452005-05-06 05:51:46 +00001750 } else if (isa<CallInst>(I) && cast<CallInst>(I).isTailCall()) {
1751 // If this is a call, check if it's a tail call.
1752 Out << "tail ";
1753 }
Chris Lattner504f9242003-09-08 17:45:59 +00001754
Chris Lattner2f7c9632001-06-06 20:29:01 +00001755 // Print out the opcode...
Misha Brukmana6619a92004-06-21 21:53:56 +00001756 Out << I.getOpcodeName();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001757
Dan Gohman9c7f8082009-07-27 16:11:46 +00001758 // Print out optimization information.
1759 WriteOptimizationInfo(Out, &I);
1760
Reid Spencer45e52392006-12-03 06:27:29 +00001761 // Print out the compare instruction predicates
Nate Begemand2195702008-05-12 19:01:56 +00001762 if (const CmpInst *CI = dyn_cast<CmpInst>(&I))
Chris Lattner82ff9232008-08-23 22:52:27 +00001763 Out << ' ' << getPredicateText(CI->getPredicate());
Reid Spencer45e52392006-12-03 06:27:29 +00001764
Chris Lattner2f7c9632001-06-06 20:29:01 +00001765 // Print out the type of the operands...
Chris Lattner113f4f42002-06-25 16:13:24 +00001766 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001767
1768 // Special case conditional branches to swizzle the condition out to the front
Gabor Greifcab008f2009-02-09 15:45:06 +00001769 if (isa<BranchInst>(I) && cast<BranchInst>(I).isConditional()) {
1770 BranchInst &BI(cast<BranchInst>(I));
Dan Gohman81313fd2008-09-14 17:21:12 +00001771 Out << ' ';
Gabor Greifcab008f2009-02-09 15:45:06 +00001772 writeOperand(BI.getCondition(), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00001773 Out << ", ";
Gabor Greifcab008f2009-02-09 15:45:06 +00001774 writeOperand(BI.getSuccessor(0), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00001775 Out << ", ";
Gabor Greifcab008f2009-02-09 15:45:06 +00001776 writeOperand(BI.getSuccessor(1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001777
Chris Lattner8d48df22002-04-13 18:34:38 +00001778 } else if (isa<SwitchInst>(I)) {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001779 // Special case switch statement to get formatting nice and correct...
Dan Gohman81313fd2008-09-14 17:21:12 +00001780 Out << ' ';
Chris Lattner82ff9232008-08-23 22:52:27 +00001781 writeOperand(Operand , true);
Dan Gohman81313fd2008-09-14 17:21:12 +00001782 Out << ", ";
Chris Lattner82ff9232008-08-23 22:52:27 +00001783 writeOperand(I.getOperand(1), true);
1784 Out << " [";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001785
Chris Lattner113f4f42002-06-25 16:13:24 +00001786 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; op += 2) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001787 Out << "\n\t\t";
Chris Lattner82ff9232008-08-23 22:52:27 +00001788 writeOperand(I.getOperand(op ), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00001789 Out << ", ";
Chris Lattner113f4f42002-06-25 16:13:24 +00001790 writeOperand(I.getOperand(op+1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001791 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001792 Out << "\n\t]";
Chris Lattnerda558102001-10-02 03:41:24 +00001793 } else if (isa<PHINode>(I)) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001794 Out << ' ';
Chris Lattnere101c442009-02-28 21:26:53 +00001795 TypePrinter.print(I.getType(), Out);
Misha Brukmana6619a92004-06-21 21:53:56 +00001796 Out << ' ';
Chris Lattner2f7c9632001-06-06 20:29:01 +00001797
Chris Lattner113f4f42002-06-25 16:13:24 +00001798 for (unsigned op = 0, Eop = I.getNumOperands(); op < Eop; op += 2) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001799 if (op) Out << ", ";
Dan Gohman81313fd2008-09-14 17:21:12 +00001800 Out << "[ ";
1801 writeOperand(I.getOperand(op ), false); Out << ", ";
Misha Brukmana6619a92004-06-21 21:53:56 +00001802 writeOperand(I.getOperand(op+1), false); Out << " ]";
Chris Lattner931ef3b2001-06-11 15:04:20 +00001803 }
Dan Gohmana76f0f72008-05-31 19:12:39 +00001804 } else if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(&I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001805 Out << ' ';
Dan Gohmana76f0f72008-05-31 19:12:39 +00001806 writeOperand(I.getOperand(0), true);
1807 for (const unsigned *i = EVI->idx_begin(), *e = EVI->idx_end(); i != e; ++i)
1808 Out << ", " << *i;
1809 } else if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(&I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001810 Out << ' ';
1811 writeOperand(I.getOperand(0), true); Out << ", ";
Dan Gohmana76f0f72008-05-31 19:12:39 +00001812 writeOperand(I.getOperand(1), true);
1813 for (const unsigned *i = IVI->idx_begin(), *e = IVI->idx_end(); i != e; ++i)
1814 Out << ", " << *i;
Devang Patel59643e52008-02-23 00:35:18 +00001815 } else if (isa<ReturnInst>(I) && !Operand) {
1816 Out << " void";
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001817 } else if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
1818 // Print the calling convention being used.
1819 switch (CI->getCallingConv()) {
1820 case CallingConv::C: break; // default
Chris Lattner29d20852006-05-19 21:58:52 +00001821 case CallingConv::Fast: Out << " fastcc"; break;
1822 case CallingConv::Cold: Out << " coldcc"; break;
Chris Lattnerf5270372007-11-18 18:32:16 +00001823 case CallingConv::X86_StdCall: Out << " x86_stdcallcc"; break;
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001824 case CallingConv::X86_FastCall: Out << " x86_fastcallcc"; break;
1825 case CallingConv::ARM_APCS: Out << " arm_apcscc "; break;
1826 case CallingConv::ARM_AAPCS: Out << " arm_aapcscc "; break;
1827 case CallingConv::ARM_AAPCS_VFP:Out << " arm_aapcs_vfpcc "; break;
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001828 default: Out << " cc" << CI->getCallingConv(); break;
1829 }
1830
Reid Spencer1517de32007-04-09 06:10:42 +00001831 const PointerType *PTy = cast<PointerType>(Operand->getType());
1832 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1833 const Type *RetTy = FTy->getReturnType();
Devang Patel4c758ea2008-09-25 21:00:45 +00001834 const AttrListPtr &PAL = CI->getAttributes();
Chris Lattner2f2d9472001-11-06 21:28:12 +00001835
Devang Patel221fe422008-09-29 20:49:50 +00001836 if (PAL.getRetAttributes() != Attribute::None)
1837 Out << ' ' << Attribute::getAsString(PAL.getRetAttributes());
1838
Chris Lattner463d6a52003-08-05 15:34:45 +00001839 // If possible, print out the short form of the call instruction. We can
Chris Lattner6915f8f2002-04-07 22:49:37 +00001840 // only do this if the first argument is a pointer to a nonvararg function,
Chris Lattner463d6a52003-08-05 15:34:45 +00001841 // and if the return type is not a pointer to a function.
Chris Lattner2f2d9472001-11-06 21:28:12 +00001842 //
Dan Gohman81313fd2008-09-14 17:21:12 +00001843 Out << ' ';
Chris Lattner463d6a52003-08-05 15:34:45 +00001844 if (!FTy->isVarArg() &&
Misha Brukmanb1c93172005-04-21 23:48:37 +00001845 (!isa<PointerType>(RetTy) ||
Chris Lattnerd9a36a62002-07-25 20:58:51 +00001846 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Chris Lattnere101c442009-02-28 21:26:53 +00001847 TypePrinter.print(RetTy, Out);
Dan Gohman81313fd2008-09-14 17:21:12 +00001848 Out << ' ';
Chris Lattner2f2d9472001-11-06 21:28:12 +00001849 writeOperand(Operand, false);
1850 } else {
1851 writeOperand(Operand, true);
1852 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001853 Out << '(';
Reid Spencer8c4914c2006-12-31 05:24:50 +00001854 for (unsigned op = 1, Eop = I.getNumOperands(); op < Eop; ++op) {
1855 if (op > 1)
Dan Gohman81313fd2008-09-14 17:21:12 +00001856 Out << ", ";
Devang Patela05633e2008-09-26 22:53:05 +00001857 writeParamOperand(I.getOperand(op), PAL.getParamAttributes(op));
Chris Lattner2f7c9632001-06-06 20:29:01 +00001858 }
Dan Gohman81313fd2008-09-14 17:21:12 +00001859 Out << ')';
Devang Patela05633e2008-09-26 22:53:05 +00001860 if (PAL.getFnAttributes() != Attribute::None)
1861 Out << ' ' << Attribute::getAsString(PAL.getFnAttributes());
Chris Lattner113f4f42002-06-25 16:13:24 +00001862 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
Reid Spencer1517de32007-04-09 06:10:42 +00001863 const PointerType *PTy = cast<PointerType>(Operand->getType());
1864 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1865 const Type *RetTy = FTy->getReturnType();
Devang Patel4c758ea2008-09-25 21:00:45 +00001866 const AttrListPtr &PAL = II->getAttributes();
Chris Lattner463d6a52003-08-05 15:34:45 +00001867
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001868 // Print the calling convention being used.
1869 switch (II->getCallingConv()) {
1870 case CallingConv::C: break; // default
Chris Lattner29d20852006-05-19 21:58:52 +00001871 case CallingConv::Fast: Out << " fastcc"; break;
1872 case CallingConv::Cold: Out << " coldcc"; break;
Dan Gohman81313fd2008-09-14 17:21:12 +00001873 case CallingConv::X86_StdCall: Out << " x86_stdcallcc"; break;
1874 case CallingConv::X86_FastCall: Out << " x86_fastcallcc"; break;
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001875 case CallingConv::ARM_APCS: Out << " arm_apcscc "; break;
1876 case CallingConv::ARM_AAPCS: Out << " arm_aapcscc "; break;
1877 case CallingConv::ARM_AAPCS_VFP:Out << " arm_aapcs_vfpcc "; break;
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001878 default: Out << " cc" << II->getCallingConv(); break;
1879 }
1880
Devang Patel221fe422008-09-29 20:49:50 +00001881 if (PAL.getRetAttributes() != Attribute::None)
1882 Out << ' ' << Attribute::getAsString(PAL.getRetAttributes());
1883
Chris Lattner463d6a52003-08-05 15:34:45 +00001884 // If possible, print out the short form of the invoke instruction. We can
1885 // only do this if the first argument is a pointer to a nonvararg function,
1886 // and if the return type is not a pointer to a function.
1887 //
Dan Gohmanc7e00ba2008-10-15 18:02:08 +00001888 Out << ' ';
Chris Lattner463d6a52003-08-05 15:34:45 +00001889 if (!FTy->isVarArg() &&
Misha Brukmanb1c93172005-04-21 23:48:37 +00001890 (!isa<PointerType>(RetTy) ||
Chris Lattner463d6a52003-08-05 15:34:45 +00001891 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Chris Lattnere101c442009-02-28 21:26:53 +00001892 TypePrinter.print(RetTy, Out);
Dan Gohmanc7e00ba2008-10-15 18:02:08 +00001893 Out << ' ';
Chris Lattner463d6a52003-08-05 15:34:45 +00001894 writeOperand(Operand, false);
1895 } else {
1896 writeOperand(Operand, true);
1897 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001898 Out << '(';
Bill Wendling4bb96e92009-03-13 21:15:59 +00001899 for (unsigned op = 3, Eop = I.getNumOperands(); op < Eop; ++op) {
1900 if (op > 3)
Dan Gohman81313fd2008-09-14 17:21:12 +00001901 Out << ", ";
Bill Wendling4bb96e92009-03-13 21:15:59 +00001902 writeParamOperand(I.getOperand(op), PAL.getParamAttributes(op-2));
Chris Lattner862e3382001-10-13 06:42:36 +00001903 }
1904
Dan Gohman81313fd2008-09-14 17:21:12 +00001905 Out << ')';
Devang Patela05633e2008-09-26 22:53:05 +00001906 if (PAL.getFnAttributes() != Attribute::None)
1907 Out << ' ' << Attribute::getAsString(PAL.getFnAttributes());
1908
Dan Gohman81313fd2008-09-14 17:21:12 +00001909 Out << "\n\t\t\tto ";
Chris Lattner862e3382001-10-13 06:42:36 +00001910 writeOperand(II->getNormalDest(), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00001911 Out << " unwind ";
Chris Lattnerfae8ab32004-02-08 21:44:31 +00001912 writeOperand(II->getUnwindDest(), true);
Chris Lattner862e3382001-10-13 06:42:36 +00001913
Chris Lattner113f4f42002-06-25 16:13:24 +00001914 } else if (const AllocationInst *AI = dyn_cast<AllocationInst>(&I)) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001915 Out << ' ';
Chris Lattnere101c442009-02-28 21:26:53 +00001916 TypePrinter.print(AI->getType()->getElementType(), Out);
Chris Lattner8d48df22002-04-13 18:34:38 +00001917 if (AI->isArrayAllocation()) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001918 Out << ", ";
Chris Lattner8d48df22002-04-13 18:34:38 +00001919 writeOperand(AI->getArraySize(), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001920 }
Nate Begeman848622f2005-11-05 09:21:28 +00001921 if (AI->getAlignment()) {
Chris Lattner7aeee3a2005-11-05 21:20:34 +00001922 Out << ", align " << AI->getAlignment();
Nate Begeman848622f2005-11-05 09:21:28 +00001923 }
Chris Lattner862e3382001-10-13 06:42:36 +00001924 } else if (isa<CastInst>(I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001925 if (Operand) {
1926 Out << ' ';
1927 writeOperand(Operand, true); // Work with broken code
1928 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001929 Out << " to ";
Chris Lattnere101c442009-02-28 21:26:53 +00001930 TypePrinter.print(I.getType(), Out);
Chris Lattner5b337482003-10-18 05:57:43 +00001931 } else if (isa<VAArgInst>(I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001932 if (Operand) {
1933 Out << ' ';
1934 writeOperand(Operand, true); // Work with broken code
1935 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001936 Out << ", ";
Chris Lattnere101c442009-02-28 21:26:53 +00001937 TypePrinter.print(I.getType(), Out);
1938 } else if (Operand) { // Print the normal way.
Chris Lattner2f7c9632001-06-06 20:29:01 +00001939
Misha Brukmanb1c93172005-04-21 23:48:37 +00001940 // PrintAllTypes - Instructions who have operands of all the same type
Chris Lattner2f7c9632001-06-06 20:29:01 +00001941 // omit the type from all but the first operand. If the instruction has
1942 // different type operands (for example br), then they are all printed.
1943 bool PrintAllTypes = false;
1944 const Type *TheType = Operand->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001945
Reid Spencer0cdd04f2007-02-02 13:54:55 +00001946 // Select, Store and ShuffleVector always print all types.
Devang Patelce556d92008-03-04 22:05:14 +00001947 if (isa<SelectInst>(I) || isa<StoreInst>(I) || isa<ShuffleVectorInst>(I)
1948 || isa<ReturnInst>(I)) {
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00001949 PrintAllTypes = true;
1950 } else {
1951 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
1952 Operand = I.getOperand(i);
Nuno Lopes04fe2f02009-01-15 18:40:57 +00001953 // note that Operand shouldn't be null, but the test helps make dump()
1954 // more tolerant of malformed IR
Nuno Lopes0971e772009-01-14 17:51:41 +00001955 if (Operand && Operand->getType() != TheType) {
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00001956 PrintAllTypes = true; // We have differing types! Print them all!
1957 break;
1958 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001959 }
1960 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001961
Chris Lattner7bfee412001-10-29 16:05:51 +00001962 if (!PrintAllTypes) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001963 Out << ' ';
Chris Lattnere101c442009-02-28 21:26:53 +00001964 TypePrinter.print(TheType, Out);
Chris Lattner7bfee412001-10-29 16:05:51 +00001965 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001966
Dan Gohman81313fd2008-09-14 17:21:12 +00001967 Out << ' ';
Chris Lattner113f4f42002-06-25 16:13:24 +00001968 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001969 if (i) Out << ", ";
Chris Lattner113f4f42002-06-25 16:13:24 +00001970 writeOperand(I.getOperand(i), PrintAllTypes);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001971 }
1972 }
Christopher Lamb84485702007-04-22 19:24:39 +00001973
1974 // Print post operand alignment for load/store
1975 if (isa<LoadInst>(I) && cast<LoadInst>(I).getAlignment()) {
1976 Out << ", align " << cast<LoadInst>(I).getAlignment();
1977 } else if (isa<StoreInst>(I) && cast<StoreInst>(I).getAlignment()) {
1978 Out << ", align " << cast<StoreInst>(I).getAlignment();
1979 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001980
Chris Lattner862e3382001-10-13 06:42:36 +00001981 printInfoComment(I);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001982}
1983
1984
1985//===----------------------------------------------------------------------===//
1986// External Interface declarations
1987//===----------------------------------------------------------------------===//
1988
Chris Lattner8339f7d2003-10-30 23:41:03 +00001989void Module::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Chris Lattner0c19df42008-08-23 22:23:09 +00001990 raw_os_ostream OS(o);
1991 print(OS, AAW);
1992}
1993void Module::print(raw_ostream &OS, AssemblyAnnotationWriter *AAW) const {
Chris Lattnere36fd8a2008-08-19 04:26:57 +00001994 SlotTracker SlotTable(this);
Chris Lattner0c19df42008-08-23 22:23:09 +00001995 AssemblyWriter W(OS, SlotTable, this, AAW);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001996 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001997}
1998
Misha Brukmanb1c93172005-04-21 23:48:37 +00001999void Type::print(std::ostream &o) const {
Chris Lattner0c19df42008-08-23 22:23:09 +00002000 raw_os_ostream OS(o);
2001 print(OS);
2002}
2003
Chris Lattner2ee62d22009-02-28 21:11:05 +00002004void Type::print(raw_ostream &OS) const {
2005 if (this == 0) {
2006 OS << "<null Type>";
2007 return;
2008 }
Chris Lattner92c5c122009-02-28 23:20:19 +00002009 TypePrinting().print(this, OS);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00002010}
2011
Chris Lattner0c19df42008-08-23 22:23:09 +00002012void Value::print(raw_ostream &OS, AssemblyAnnotationWriter *AAW) const {
2013 if (this == 0) {
2014 OS << "printing a <null> value\n";
2015 return;
2016 }
Chris Lattner0c19df42008-08-23 22:23:09 +00002017 if (const Instruction *I = dyn_cast<Instruction>(this)) {
2018 const Function *F = I->getParent() ? I->getParent()->getParent() : 0;
2019 SlotTracker SlotTable(F);
2020 AssemblyWriter W(OS, SlotTable, F ? F->getParent() : 0, AAW);
2021 W.write(I);
2022 } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(this)) {
2023 SlotTracker SlotTable(BB->getParent());
2024 AssemblyWriter W(OS, SlotTable,
2025 BB->getParent() ? BB->getParent()->getParent() : 0, AAW);
2026 W.write(BB);
2027 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(this)) {
2028 SlotTracker SlotTable(GV->getParent());
Dan Gohman6c7a4852009-04-20 16:10:33 +00002029 AssemblyWriter W(OS, SlotTable, GV->getParent(), AAW);
Chris Lattner0c19df42008-08-23 22:23:09 +00002030 W.write(GV);
Devang Patel7428d8a2009-07-22 17:43:22 +00002031 } else if (const MDString *MDS = dyn_cast<MDString>(this)) {
2032 TypePrinting TypePrinter;
2033 TypePrinter.print(MDS->getType(), OS);
2034 OS << ' ';
2035 OS << "!\"";
Daniel Dunbare03eecb2009-07-25 23:55:21 +00002036 PrintEscapedString(MDS->getString(), OS);
Devang Patel7428d8a2009-07-22 17:43:22 +00002037 OS << '"';
Devang Patel5546b982009-07-01 20:59:15 +00002038 } else if (const MDNode *N = dyn_cast<MDNode>(this)) {
Devang Patel983c6b12009-07-08 21:44:25 +00002039 SlotTracker SlotTable(N);
Devang Patel5546b982009-07-01 20:59:15 +00002040 TypePrinting TypePrinter;
Devang Patel983c6b12009-07-08 21:44:25 +00002041 SlotTable.initialize();
2042 WriteMDNodes(OS, TypePrinter, SlotTable);
Devang Patelb4a4e772009-07-30 00:02:57 +00002043 } else if (const NamedMDNode *N = dyn_cast<NamedMDNode>(this)) {
2044 SlotTracker SlotTable(N);
2045 TypePrinting TypePrinter;
2046 SlotTable.initialize();
2047 OS << "!" << N->getName() << " = !{";
2048 for (unsigned i = 0, e = N->getNumElements(); i != e; ++i) {
2049 if (i) OS << ", ";
2050 MDNode *MD = cast<MDNode>(N->getElement(i));
2051 OS << '!' << SlotTable.getMetadataSlot(MD);
2052 }
2053 OS << "}\n";
2054 WriteMDNodes(OS, TypePrinter, SlotTable);
Chris Lattner0c19df42008-08-23 22:23:09 +00002055 } else if (const Constant *C = dyn_cast<Constant>(this)) {
Chris Lattner92c5c122009-02-28 23:20:19 +00002056 TypePrinting TypePrinter;
Chris Lattnere101c442009-02-28 21:26:53 +00002057 TypePrinter.print(C->getType(), OS);
Chris Lattner2ee62d22009-02-28 21:11:05 +00002058 OS << ' ';
Chris Lattner0f578952009-02-28 20:25:14 +00002059 WriteConstantInt(OS, C, TypePrinter, 0);
Chris Lattner0c19df42008-08-23 22:23:09 +00002060 } else if (const Argument *A = dyn_cast<Argument>(this)) {
2061 WriteAsOperand(OS, this, true,
2062 A->getParent() ? A->getParent()->getParent() : 0);
2063 } else if (isa<InlineAsm>(this)) {
2064 WriteAsOperand(OS, this, true, 0);
2065 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002066 llvm_unreachable("Unknown value to print out!");
Chris Lattner0c19df42008-08-23 22:23:09 +00002067 }
2068}
2069
2070void Value::print(std::ostream &O, AssemblyAnnotationWriter *AAW) const {
2071 raw_os_ostream OS(O);
2072 print(OS, AAW);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00002073}
2074
Chris Lattnerdab942552008-08-25 17:03:15 +00002075// Value::dump - allow easy printing of Values from the debugger.
Dan Gohman4f2fea12009-03-23 15:57:19 +00002076void Value::dump() const { print(errs()); errs() << '\n'; }
Reid Spencer52641832004-05-25 18:14:38 +00002077
Chris Lattnerdab942552008-08-25 17:03:15 +00002078// Type::dump - allow easy printing of Types from the debugger.
Chris Lattner41a83d92008-10-01 20:16:19 +00002079// This one uses type names from the given context module
2080void Type::dump(const Module *Context) const {
2081 WriteTypeSymbolic(errs(), this, Context);
2082 errs() << '\n';
Chris Lattner41a83d92008-10-01 20:16:19 +00002083}
2084
Chris Lattner24025262009-02-28 21:05:51 +00002085// Type::dump - allow easy printing of Types from the debugger.
2086void Type::dump() const { dump(0); }
2087
Chris Lattnerdab942552008-08-25 17:03:15 +00002088// Module::dump() - Allow printing of Modules from the debugger.
Dan Gohman4f2fea12009-03-23 15:57:19 +00002089void Module::dump() const { print(errs(), 0); }