blob: f6c5fd3f11ecf25c067b47a92aadb517865d295b [file] [log] [blame]
Chris Lattner8da78af2002-04-07 22:31:46 +00001//===-- AsmWriter.cpp - Printing LLVM as an assembly file -----------------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
10// This library implements the functionality defined in llvm/Assembly/Writer.h
11//
Chris Lattner02b93992002-04-12 18:21:53 +000012// Note that these routines must be extremely tolerant of various errors in the
Chris Lattner8f77dae2003-05-08 02:44:12 +000013// LLVM code, because it can be used for debugging transformations.
Chris Lattner02b93992002-04-12 18:21:53 +000014//
Chris Lattner00950542001-06-06 20:29:01 +000015//===----------------------------------------------------------------------===//
16
Chris Lattner75cf7cf2002-04-08 22:03:40 +000017#include "llvm/Assembly/Writer.h"
Chris Lattnerf082b802002-07-23 18:07:49 +000018#include "llvm/Assembly/PrintModulePass.h"
Chris Lattner95e5a2c2003-10-30 23:41:03 +000019#include "llvm/Assembly/AsmAnnotationWriter.h"
Chris Lattnerd5118982005-05-06 20:26:43 +000020#include "llvm/CallingConv.h"
Chris Lattnerf2d577b2004-01-20 19:50:34 +000021#include "llvm/Constants.h"
Chris Lattner3eb59c02002-04-29 18:46:50 +000022#include "llvm/DerivedTypes.h"
Chris Lattner863517a2006-01-25 18:57:27 +000023#include "llvm/InlineAsm.h"
Vikram S. Adveb4dbb442002-07-14 23:14:45 +000024#include "llvm/Instruction.h"
Misha Brukman44336292004-07-29 16:53:53 +000025#include "llvm/Instructions.h"
Dan Gohman1224c382009-07-20 21:19:07 +000026#include "llvm/Operator.h"
Devang Patel0a9f7b92009-07-28 21:49:47 +000027#include "llvm/Metadata.h"
Chris Lattnerf2d577b2004-01-20 19:50:34 +000028#include "llvm/Module.h"
Reid Spenceref9b9a72007-02-05 20:47:22 +000029#include "llvm/ValueSymbolTable.h"
Reid Spencer78d033e2007-01-06 07:24:44 +000030#include "llvm/TypeSymbolTable.h"
Chris Lattner413fd232009-03-01 00:03:38 +000031#include "llvm/ADT/DenseSet.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000032#include "llvm/ADT/StringExtras.h"
33#include "llvm/ADT/STLExtras.h"
Bill Wendling8f487662006-11-28 02:09:03 +000034#include "llvm/Support/CFG.h"
Devang Patel2d5988d2009-09-30 20:16:54 +000035#include "llvm/Support/Dwarf.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000036#include "llvm/Support/ErrorHandling.h"
Jim Laskeycb6682f2005-08-17 19:34:49 +000037#include "llvm/Support/MathExtras.h"
Dan Gohman683e9222009-08-12 17:23:50 +000038#include "llvm/Support/FormattedStream.h"
Chris Lattner007377f2001-09-07 16:36:04 +000039#include <algorithm>
Reid Spencer4ad513c2007-05-22 19:27:35 +000040#include <cctype>
Devang Patel923078c2009-07-01 19:21:12 +000041#include <map>
Chris Lattner31f84992003-11-21 20:23:48 +000042using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000043
Reid Spenceredd5d9e2005-05-15 16:13:11 +000044// Make virtual table appear in this compilation unit.
45AssemblyAnnotationWriter::~AssemblyAnnotationWriter() {}
46
Chris Lattner6ab910b2008-08-19 04:36:02 +000047//===----------------------------------------------------------------------===//
48// Helper Functions
49//===----------------------------------------------------------------------===//
50
51static const Module *getModuleFromVal(const Value *V) {
52 if (const Argument *MA = dyn_cast<Argument>(V))
53 return MA->getParent() ? MA->getParent()->getParent() : 0;
Daniel Dunbara279bc32009-09-20 02:20:51 +000054
Chris Lattner6ab910b2008-08-19 04:36:02 +000055 if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
56 return BB->getParent() ? BB->getParent()->getParent() : 0;
Daniel Dunbara279bc32009-09-20 02:20:51 +000057
Chris Lattner6ab910b2008-08-19 04:36:02 +000058 if (const Instruction *I = dyn_cast<Instruction>(V)) {
59 const Function *M = I->getParent() ? I->getParent()->getParent() : 0;
60 return M ? M->getParent() : 0;
61 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000062
Chris Lattner6ab910b2008-08-19 04:36:02 +000063 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
64 return GV->getParent();
65 return 0;
66}
67
Daniel Dunbare9da1332008-10-28 19:33:02 +000068// PrintEscapedString - Print each character of the specified string, escaping
69// it if it is not printable or if it is an escape char.
Dan Gohman683e9222009-08-12 17:23:50 +000070static void PrintEscapedString(const StringRef &Name,
Dan Gohman1220e102009-08-12 20:56:03 +000071 raw_ostream &Out) {
Daniel Dunbar03d76512009-07-25 23:55:21 +000072 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
73 unsigned char C = Name[i];
Nick Lewycky34a40862009-03-15 06:39:52 +000074 if (isprint(C) && C != '\\' && C != '"')
Daniel Dunbare9da1332008-10-28 19:33:02 +000075 Out << C;
76 else
77 Out << '\\' << hexdigit(C >> 4) << hexdigit(C & 0x0F);
78 }
79}
80
Chris Lattner6ab910b2008-08-19 04:36:02 +000081enum PrefixType {
82 GlobalPrefix,
83 LabelPrefix,
Daniel Dunbarcad35802008-10-14 23:28:09 +000084 LocalPrefix,
85 NoPrefix
Chris Lattner6ab910b2008-08-19 04:36:02 +000086};
87
88/// PrintLLVMName - Turn the specified name into an 'LLVM name', which is either
89/// prefixed with % (if the string only contains simple characters) or is
90/// surrounded with ""'s (if it has special chars in it). Print it out.
Dan Gohman1220e102009-08-12 20:56:03 +000091static void PrintLLVMName(raw_ostream &OS, const StringRef &Name,
Daniel Dunbar03d76512009-07-25 23:55:21 +000092 PrefixType Prefix) {
93 assert(Name.data() && "Cannot get empty name!");
Chris Lattner6ab910b2008-08-19 04:36:02 +000094 switch (Prefix) {
Torok Edwinc23197a2009-07-14 16:55:14 +000095 default: llvm_unreachable("Bad prefix!");
Daniel Dunbarcad35802008-10-14 23:28:09 +000096 case NoPrefix: break;
Chris Lattner52b26de2008-08-19 05:16:28 +000097 case GlobalPrefix: OS << '@'; break;
98 case LabelPrefix: break;
99 case LocalPrefix: OS << '%'; break;
Nick Lewycky04234832009-03-19 06:31:22 +0000100 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000101
Chris Lattner6ab910b2008-08-19 04:36:02 +0000102 // Scan the name to see if it needs quotes first.
Daniel Dunbar03d76512009-07-25 23:55:21 +0000103 bool NeedsQuotes = isdigit(Name[0]);
Chris Lattner6ab910b2008-08-19 04:36:02 +0000104 if (!NeedsQuotes) {
Daniel Dunbar03d76512009-07-25 23:55:21 +0000105 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
106 char C = Name[i];
Chris Lattner6ab910b2008-08-19 04:36:02 +0000107 if (!isalnum(C) && C != '-' && C != '.' && C != '_') {
108 NeedsQuotes = true;
109 break;
110 }
111 }
112 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000113
Chris Lattner6ab910b2008-08-19 04:36:02 +0000114 // If we didn't need any quotes, just write out the name in one blast.
115 if (!NeedsQuotes) {
Daniel Dunbar03d76512009-07-25 23:55:21 +0000116 OS << Name;
Chris Lattner6ab910b2008-08-19 04:36:02 +0000117 return;
118 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000119
Chris Lattner6ab910b2008-08-19 04:36:02 +0000120 // Okay, we need quotes. Output the quotes and escape any scary characters as
121 // needed.
122 OS << '"';
Daniel Dunbar03d76512009-07-25 23:55:21 +0000123 PrintEscapedString(Name, OS);
Chris Lattner6ab910b2008-08-19 04:36:02 +0000124 OS << '"';
125}
126
127/// PrintLLVMName - Turn the specified name into an 'LLVM name', which is either
128/// prefixed with % (if the string only contains simple characters) or is
129/// surrounded with ""'s (if it has special chars in it). Print it out.
Dan Gohman1220e102009-08-12 20:56:03 +0000130static void PrintLLVMName(raw_ostream &OS, const Value *V) {
Daniel Dunbara279bc32009-09-20 02:20:51 +0000131 PrintLLVMName(OS, V->getName(),
Chris Lattner6ab910b2008-08-19 04:36:02 +0000132 isa<GlobalValue>(V) ? GlobalPrefix : LocalPrefix);
133}
134
Chris Lattner9cc34462009-02-28 20:25:14 +0000135//===----------------------------------------------------------------------===//
136// TypePrinting Class: Type printing machinery
137//===----------------------------------------------------------------------===//
138
Chris Lattner87185e82009-02-28 23:03:55 +0000139static DenseMap<const Type *, std::string> &getTypeNamesMap(void *M) {
140 return *static_cast<DenseMap<const Type *, std::string>*>(M);
Chris Lattnerd8030a72009-02-28 22:34:45 +0000141}
142
143void TypePrinting::clear() {
144 getTypeNamesMap(TypeNames).clear();
145}
Chris Lattner9cc34462009-02-28 20:25:14 +0000146
Chris Lattnere9fa33e2009-02-28 23:20:19 +0000147bool TypePrinting::hasTypeName(const Type *Ty) const {
148 return getTypeNamesMap(TypeNames).count(Ty);
149}
150
151void TypePrinting::addTypeName(const Type *Ty, const std::string &N) {
152 getTypeNamesMap(TypeNames).insert(std::make_pair(Ty, N));
153}
154
155
156TypePrinting::TypePrinting() {
Chris Lattner87185e82009-02-28 23:03:55 +0000157 TypeNames = new DenseMap<const Type *, std::string>();
Chris Lattner9cc34462009-02-28 20:25:14 +0000158}
159
Chris Lattnerd8030a72009-02-28 22:34:45 +0000160TypePrinting::~TypePrinting() {
161 delete &getTypeNamesMap(TypeNames);
162}
163
Chris Lattner534361e2009-02-28 20:49:40 +0000164/// CalcTypeName - Write the specified type to the specified raw_ostream, making
165/// use of type names or up references to shorten the type name where possible.
166void TypePrinting::CalcTypeName(const Type *Ty,
Chris Lattnerb840cac2009-02-28 20:34:19 +0000167 SmallVectorImpl<const Type *> &TypeStack,
Chris Lattnerd2b6cb02009-03-01 01:16:21 +0000168 raw_ostream &OS, bool IgnoreTopLevelName) {
Chris Lattner9cc34462009-02-28 20:25:14 +0000169 // Check to see if the type is named.
Chris Lattnerd2b6cb02009-03-01 01:16:21 +0000170 if (!IgnoreTopLevelName) {
Nick Lewycky04234832009-03-19 06:31:22 +0000171 DenseMap<const Type *, std::string> &TM = getTypeNamesMap(TypeNames);
Chris Lattnerd2b6cb02009-03-01 01:16:21 +0000172 DenseMap<const Type *, std::string>::iterator I = TM.find(Ty);
173 if (I != TM.end()) {
174 OS << I->second;
175 return;
176 }
Chris Lattner9cc34462009-02-28 20:25:14 +0000177 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000178
Chris Lattner9cc34462009-02-28 20:25:14 +0000179 // Check to see if the Type is already on the stack...
180 unsigned Slot = 0, CurSize = TypeStack.size();
181 while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type
Daniel Dunbara279bc32009-09-20 02:20:51 +0000182
Chris Lattner9cc34462009-02-28 20:25:14 +0000183 // This is another base case for the recursion. In this case, we know
184 // that we have looped back to a type that we have previously visited.
185 // Generate the appropriate upreference to handle this.
186 if (Slot < CurSize) {
Chris Lattner30794262009-02-28 21:27:31 +0000187 OS << '\\' << unsigned(CurSize-Slot); // Here's the upreference
Chris Lattner9cc34462009-02-28 20:25:14 +0000188 return;
189 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000190
Chris Lattner9cc34462009-02-28 20:25:14 +0000191 TypeStack.push_back(Ty); // Recursive case: Add us to the stack..
Daniel Dunbara279bc32009-09-20 02:20:51 +0000192
Chris Lattner9cc34462009-02-28 20:25:14 +0000193 switch (Ty->getTypeID()) {
Chris Lattner30794262009-02-28 21:27:31 +0000194 case Type::VoidTyID: OS << "void"; break;
195 case Type::FloatTyID: OS << "float"; break;
196 case Type::DoubleTyID: OS << "double"; break;
197 case Type::X86_FP80TyID: OS << "x86_fp80"; break;
198 case Type::FP128TyID: OS << "fp128"; break;
199 case Type::PPC_FP128TyID: OS << "ppc_fp128"; break;
200 case Type::LabelTyID: OS << "label"; break;
Nick Lewycky7a0370f2009-05-30 05:06:04 +0000201 case Type::MetadataTyID: OS << "metadata"; break;
Chris Lattner583ffd82009-02-28 21:18:43 +0000202 case Type::IntegerTyID:
Chris Lattner30794262009-02-28 21:27:31 +0000203 OS << 'i' << cast<IntegerType>(Ty)->getBitWidth();
Chris Lattner583ffd82009-02-28 21:18:43 +0000204 break;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000205
Chris Lattner36942d72009-02-28 20:35:42 +0000206 case Type::FunctionTyID: {
207 const FunctionType *FTy = cast<FunctionType>(Ty);
Chris Lattner30794262009-02-28 21:27:31 +0000208 CalcTypeName(FTy->getReturnType(), TypeStack, OS);
209 OS << " (";
Chris Lattner36942d72009-02-28 20:35:42 +0000210 for (FunctionType::param_iterator I = FTy->param_begin(),
211 E = FTy->param_end(); I != E; ++I) {
212 if (I != FTy->param_begin())
Chris Lattner30794262009-02-28 21:27:31 +0000213 OS << ", ";
214 CalcTypeName(*I, TypeStack, OS);
Chris Lattner9cc34462009-02-28 20:25:14 +0000215 }
Chris Lattner36942d72009-02-28 20:35:42 +0000216 if (FTy->isVarArg()) {
Chris Lattner30794262009-02-28 21:27:31 +0000217 if (FTy->getNumParams()) OS << ", ";
218 OS << "...";
Chris Lattner9cc34462009-02-28 20:25:14 +0000219 }
Chris Lattner30794262009-02-28 21:27:31 +0000220 OS << ')';
Chris Lattner36942d72009-02-28 20:35:42 +0000221 break;
222 }
223 case Type::StructTyID: {
224 const StructType *STy = cast<StructType>(Ty);
225 if (STy->isPacked())
Chris Lattner30794262009-02-28 21:27:31 +0000226 OS << '<';
227 OS << "{ ";
Chris Lattner36942d72009-02-28 20:35:42 +0000228 for (StructType::element_iterator I = STy->element_begin(),
229 E = STy->element_end(); I != E; ++I) {
Chris Lattner30794262009-02-28 21:27:31 +0000230 CalcTypeName(*I, TypeStack, OS);
Chris Lattner36942d72009-02-28 20:35:42 +0000231 if (next(I) != STy->element_end())
Chris Lattner30794262009-02-28 21:27:31 +0000232 OS << ',';
233 OS << ' ';
Chris Lattner9cc34462009-02-28 20:25:14 +0000234 }
Chris Lattner30794262009-02-28 21:27:31 +0000235 OS << '}';
Chris Lattner36942d72009-02-28 20:35:42 +0000236 if (STy->isPacked())
Chris Lattner30794262009-02-28 21:27:31 +0000237 OS << '>';
Chris Lattner36942d72009-02-28 20:35:42 +0000238 break;
239 }
240 case Type::PointerTyID: {
241 const PointerType *PTy = cast<PointerType>(Ty);
Chris Lattner30794262009-02-28 21:27:31 +0000242 CalcTypeName(PTy->getElementType(), TypeStack, OS);
Chris Lattner36942d72009-02-28 20:35:42 +0000243 if (unsigned AddressSpace = PTy->getAddressSpace())
Chris Lattner30794262009-02-28 21:27:31 +0000244 OS << " addrspace(" << AddressSpace << ')';
245 OS << '*';
Chris Lattner36942d72009-02-28 20:35:42 +0000246 break;
247 }
248 case Type::ArrayTyID: {
249 const ArrayType *ATy = cast<ArrayType>(Ty);
Chris Lattner30794262009-02-28 21:27:31 +0000250 OS << '[' << ATy->getNumElements() << " x ";
251 CalcTypeName(ATy->getElementType(), TypeStack, OS);
252 OS << ']';
Chris Lattner36942d72009-02-28 20:35:42 +0000253 break;
254 }
255 case Type::VectorTyID: {
256 const VectorType *PTy = cast<VectorType>(Ty);
Chris Lattner30794262009-02-28 21:27:31 +0000257 OS << "<" << PTy->getNumElements() << " x ";
258 CalcTypeName(PTy->getElementType(), TypeStack, OS);
259 OS << '>';
Chris Lattner36942d72009-02-28 20:35:42 +0000260 break;
261 }
262 case Type::OpaqueTyID:
Chris Lattner30794262009-02-28 21:27:31 +0000263 OS << "opaque";
Chris Lattner36942d72009-02-28 20:35:42 +0000264 break;
265 default:
Chris Lattner30794262009-02-28 21:27:31 +0000266 OS << "<unrecognized-type>";
Chris Lattner36942d72009-02-28 20:35:42 +0000267 break;
Chris Lattner9cc34462009-02-28 20:25:14 +0000268 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000269
Chris Lattner534361e2009-02-28 20:49:40 +0000270 TypeStack.pop_back(); // Remove self from stack.
Chris Lattner9cc34462009-02-28 20:25:14 +0000271}
272
273/// printTypeInt - The internal guts of printing out a type that has a
274/// potentially named portion.
275///
Chris Lattnerd2b6cb02009-03-01 01:16:21 +0000276void TypePrinting::print(const Type *Ty, raw_ostream &OS,
277 bool IgnoreTopLevelName) {
Chris Lattner9cc34462009-02-28 20:25:14 +0000278 // Check to see if the type is named.
Chris Lattner87185e82009-02-28 23:03:55 +0000279 DenseMap<const Type*, std::string> &TM = getTypeNamesMap(TypeNames);
Chris Lattnerd2b6cb02009-03-01 01:16:21 +0000280 if (!IgnoreTopLevelName) {
281 DenseMap<const Type*, std::string>::iterator I = TM.find(Ty);
282 if (I != TM.end()) {
283 OS << I->second;
284 return;
285 }
Chris Lattner9cc34462009-02-28 20:25:14 +0000286 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000287
Chris Lattner9cc34462009-02-28 20:25:14 +0000288 // Otherwise we have a type that has not been named but is a derived type.
289 // Carefully recurse the type hierarchy to print out any contained symbolic
290 // names.
Chris Lattnerb840cac2009-02-28 20:34:19 +0000291 SmallVector<const Type *, 16> TypeStack;
Chris Lattner9cc34462009-02-28 20:25:14 +0000292 std::string TypeName;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000293
Chris Lattner534361e2009-02-28 20:49:40 +0000294 raw_string_ostream TypeOS(TypeName);
Chris Lattnerd2b6cb02009-03-01 01:16:21 +0000295 CalcTypeName(Ty, TypeStack, TypeOS, IgnoreTopLevelName);
Chris Lattner534361e2009-02-28 20:49:40 +0000296 OS << TypeOS.str();
297
298 // Cache type name for later use.
Chris Lattnerd2b6cb02009-03-01 01:16:21 +0000299 if (!IgnoreTopLevelName)
300 TM.insert(std::make_pair(Ty, TypeOS.str()));
Chris Lattner9cc34462009-02-28 20:25:14 +0000301}
302
Chris Lattner413fd232009-03-01 00:03:38 +0000303namespace {
304 class TypeFinder {
305 // To avoid walking constant expressions multiple times and other IR
306 // objects, we keep several helper maps.
307 DenseSet<const Value*> VisitedConstants;
308 DenseSet<const Type*> VisitedTypes;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000309
Chris Lattner413fd232009-03-01 00:03:38 +0000310 TypePrinting &TP;
311 std::vector<const Type*> &NumberedTypes;
312 public:
313 TypeFinder(TypePrinting &tp, std::vector<const Type*> &numberedTypes)
314 : TP(tp), NumberedTypes(numberedTypes) {}
Daniel Dunbara279bc32009-09-20 02:20:51 +0000315
Chris Lattner413fd232009-03-01 00:03:38 +0000316 void Run(const Module &M) {
Chris Lattner88485862009-03-01 00:32:33 +0000317 // Get types from the type symbol table. This gets opaque types referened
318 // only through derived named types.
319 const TypeSymbolTable &ST = M.getTypeSymbolTable();
320 for (TypeSymbolTable::const_iterator TI = ST.begin(), E = ST.end();
321 TI != E; ++TI)
322 IncorporateType(TI->second);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000323
Chris Lattner413fd232009-03-01 00:03:38 +0000324 // Get types from global variables.
325 for (Module::const_global_iterator I = M.global_begin(),
326 E = M.global_end(); I != E; ++I) {
327 IncorporateType(I->getType());
328 if (I->hasInitializer())
329 IncorporateValue(I->getInitializer());
330 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000331
Chris Lattner413fd232009-03-01 00:03:38 +0000332 // Get types from aliases.
333 for (Module::const_alias_iterator I = M.alias_begin(),
334 E = M.alias_end(); I != E; ++I) {
335 IncorporateType(I->getType());
336 IncorporateValue(I->getAliasee());
337 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000338
Chris Lattner413fd232009-03-01 00:03:38 +0000339 // Get types from functions.
340 for (Module::const_iterator FI = M.begin(), E = M.end(); FI != E; ++FI) {
341 IncorporateType(FI->getType());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000342
Chris Lattner413fd232009-03-01 00:03:38 +0000343 for (Function::const_iterator BB = FI->begin(), E = FI->end();
344 BB != E;++BB)
345 for (BasicBlock::const_iterator II = BB->begin(),
346 E = BB->end(); II != E; ++II) {
347 const Instruction &I = *II;
348 // Incorporate the type of the instruction and all its operands.
349 IncorporateType(I.getType());
350 for (User::const_op_iterator OI = I.op_begin(), OE = I.op_end();
351 OI != OE; ++OI)
352 IncorporateValue(*OI);
353 }
354 }
355 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000356
Chris Lattner413fd232009-03-01 00:03:38 +0000357 private:
358 void IncorporateType(const Type *Ty) {
359 // Check to see if we're already visited this type.
Chris Lattner88485862009-03-01 00:32:33 +0000360 if (!VisitedTypes.insert(Ty).second)
Chris Lattner413fd232009-03-01 00:03:38 +0000361 return;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000362
Chris Lattner413fd232009-03-01 00:03:38 +0000363 // If this is a structure or opaque type, add a name for the type.
Nick Lewycky21cc4462009-04-04 07:22:01 +0000364 if (((isa<StructType>(Ty) && cast<StructType>(Ty)->getNumElements())
365 || isa<OpaqueType>(Ty)) && !TP.hasTypeName(Ty)) {
Chris Lattner413fd232009-03-01 00:03:38 +0000366 TP.addTypeName(Ty, "%"+utostr(unsigned(NumberedTypes.size())));
367 NumberedTypes.push_back(Ty);
368 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000369
Chris Lattner413fd232009-03-01 00:03:38 +0000370 // Recursively walk all contained types.
371 for (Type::subtype_iterator I = Ty->subtype_begin(),
372 E = Ty->subtype_end(); I != E; ++I)
Daniel Dunbara279bc32009-09-20 02:20:51 +0000373 IncorporateType(*I);
Chris Lattner413fd232009-03-01 00:03:38 +0000374 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000375
Chris Lattner413fd232009-03-01 00:03:38 +0000376 /// IncorporateValue - This method is used to walk operand lists finding
377 /// types hiding in constant expressions and other operands that won't be
378 /// walked in other ways. GlobalValues, basic blocks, instructions, and
379 /// inst operands are all explicitly enumerated.
380 void IncorporateValue(const Value *V) {
381 if (V == 0 || !isa<Constant>(V) || isa<GlobalValue>(V)) return;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000382
Chris Lattner413fd232009-03-01 00:03:38 +0000383 // Already visited?
384 if (!VisitedConstants.insert(V).second)
385 return;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000386
Chris Lattner413fd232009-03-01 00:03:38 +0000387 // Check this type.
388 IncorporateType(V->getType());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000389
Chris Lattner413fd232009-03-01 00:03:38 +0000390 // Look in operands for types.
391 const Constant *C = cast<Constant>(V);
392 for (Constant::const_op_iterator I = C->op_begin(),
393 E = C->op_end(); I != E;++I)
394 IncorporateValue(*I);
395 }
396 };
397} // end anonymous namespace
398
399
400/// AddModuleTypesToPrinter - Add all of the symbolic type names for types in
401/// the specified module to the TypePrinter and all numbered types to it and the
402/// NumberedTypes table.
Daniel Dunbara279bc32009-09-20 02:20:51 +0000403static void AddModuleTypesToPrinter(TypePrinting &TP,
Chris Lattner413fd232009-03-01 00:03:38 +0000404 std::vector<const Type*> &NumberedTypes,
405 const Module *M) {
Chris Lattnere9fa33e2009-02-28 23:20:19 +0000406 if (M == 0) return;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000407
Chris Lattnere9fa33e2009-02-28 23:20:19 +0000408 // If the module has a symbol table, take all global types and stuff their
409 // names into the TypeNames map.
410 const TypeSymbolTable &ST = M->getTypeSymbolTable();
411 for (TypeSymbolTable::const_iterator TI = ST.begin(), E = ST.end();
412 TI != E; ++TI) {
413 const Type *Ty = cast<Type>(TI->second);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000414
Chris Lattnere9fa33e2009-02-28 23:20:19 +0000415 // As a heuristic, don't insert pointer to primitive types, because
416 // they are used too often to have a single useful name.
417 if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
418 const Type *PETy = PTy->getElementType();
419 if ((PETy->isPrimitiveType() || PETy->isInteger()) &&
420 !isa<OpaqueType>(PETy))
421 continue;
422 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000423
Chris Lattnere9fa33e2009-02-28 23:20:19 +0000424 // Likewise don't insert primitives either.
425 if (Ty->isInteger() || Ty->isPrimitiveType())
426 continue;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000427
Chris Lattnere9fa33e2009-02-28 23:20:19 +0000428 // Get the name as a string and insert it into TypeNames.
429 std::string NameStr;
Dan Gohman683e9222009-08-12 17:23:50 +0000430 raw_string_ostream NameROS(NameStr);
431 formatted_raw_ostream NameOS(NameROS);
Daniel Dunbar03d76512009-07-25 23:55:21 +0000432 PrintLLVMName(NameOS, TI->first, LocalPrefix);
Dan Gohman683e9222009-08-12 17:23:50 +0000433 NameOS.flush();
434 TP.addTypeName(Ty, NameStr);
Chris Lattnere9fa33e2009-02-28 23:20:19 +0000435 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000436
Chris Lattner413fd232009-03-01 00:03:38 +0000437 // Walk the entire module to find references to unnamed structure and opaque
438 // types. This is required for correctness by opaque types (because multiple
439 // uses of an unnamed opaque type needs to be referred to by the same ID) and
440 // it shrinks complex recursive structure types substantially in some cases.
441 TypeFinder(TP, NumberedTypes).Run(*M);
Chris Lattnere9fa33e2009-02-28 23:20:19 +0000442}
443
Chris Lattner9cc34462009-02-28 20:25:14 +0000444
Chris Lattner9cc34462009-02-28 20:25:14 +0000445/// WriteTypeSymbolic - This attempts to write the specified type as a symbolic
446/// type, iff there is an entry in the modules symbol table for the specified
Chris Lattnerc2871372009-02-28 21:05:51 +0000447/// type or one of it's component types.
Chris Lattner9cc34462009-02-28 20:25:14 +0000448///
Chris Lattnere9fa33e2009-02-28 23:20:19 +0000449void llvm::WriteTypeSymbolic(raw_ostream &OS, const Type *Ty, const Module *M) {
450 TypePrinting Printer;
Chris Lattner413fd232009-03-01 00:03:38 +0000451 std::vector<const Type*> NumberedTypes;
452 AddModuleTypesToPrinter(Printer, NumberedTypes, M);
Chris Lattnere9fa33e2009-02-28 23:20:19 +0000453 Printer.print(Ty, OS);
Chris Lattner9cc34462009-02-28 20:25:14 +0000454}
455
Chris Lattner6ab910b2008-08-19 04:36:02 +0000456//===----------------------------------------------------------------------===//
457// SlotTracker Class: Enumerate slot numbers for unnamed values
458//===----------------------------------------------------------------------===//
459
Chris Lattnerb64871a2008-08-19 04:28:07 +0000460namespace {
461
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000462/// This class provides computation of slot numbers for LLVM Assembly writing.
Chris Lattner45d4c732008-08-17 04:17:45 +0000463///
Chris Lattner0d9574a2008-08-19 04:26:57 +0000464class SlotTracker {
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000465public:
Devang Patel320671d2009-07-08 21:44:25 +0000466 /// ValueMap - A mapping of Values to slot numbers.
Chris Lattner92255072008-08-17 17:25:25 +0000467 typedef DenseMap<const Value*, unsigned> ValueMap;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000468
469private:
Devang Patel320671d2009-07-08 21:44:25 +0000470 /// TheModule - The module for which we are holding slot numbers.
Chris Lattner45d4c732008-08-17 04:17:45 +0000471 const Module* TheModule;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000472
Devang Patel320671d2009-07-08 21:44:25 +0000473 /// TheFunction - The function for which we are holding slot numbers.
Chris Lattner45d4c732008-08-17 04:17:45 +0000474 const Function* TheFunction;
475 bool FunctionProcessed;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000476
Devang Patel320671d2009-07-08 21:44:25 +0000477 /// TheMDNode - The MDNode for which we are holding slot numbers.
478 const MDNode *TheMDNode;
479
Devang Patelc29d5b32009-07-30 00:02:57 +0000480 /// TheNamedMDNode - The MDNode for which we are holding slot numbers.
481 const NamedMDNode *TheNamedMDNode;
482
Devang Patel320671d2009-07-08 21:44:25 +0000483 /// mMap - The TypePlanes map for the module level data.
Chris Lattner45d4c732008-08-17 04:17:45 +0000484 ValueMap mMap;
485 unsigned mNext;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000486
Devang Patel320671d2009-07-08 21:44:25 +0000487 /// fMap - The TypePlanes map for the function level data.
Chris Lattner45d4c732008-08-17 04:17:45 +0000488 ValueMap fMap;
489 unsigned fNext;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000490
Devang Patel320671d2009-07-08 21:44:25 +0000491 /// mdnMap - Map for MDNodes.
492 ValueMap mdnMap;
493 unsigned mdnNext;
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000494public:
Chris Lattner45d4c732008-08-17 04:17:45 +0000495 /// Construct from a module
Chris Lattner0d9574a2008-08-19 04:26:57 +0000496 explicit SlotTracker(const Module *M);
Chris Lattner45d4c732008-08-17 04:17:45 +0000497 /// Construct from a function, starting out in incorp state.
Chris Lattner0d9574a2008-08-19 04:26:57 +0000498 explicit SlotTracker(const Function *F);
Devang Patel320671d2009-07-08 21:44:25 +0000499 /// Construct from a mdnode.
500 explicit SlotTracker(const MDNode *N);
Devang Patelc29d5b32009-07-30 00:02:57 +0000501 /// Construct from a named mdnode.
502 explicit SlotTracker(const NamedMDNode *N);
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000503
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000504 /// Return the slot number of the specified value in it's type
Chris Lattner0d9574a2008-08-19 04:26:57 +0000505 /// plane. If something is not in the SlotTracker, return -1.
Chris Lattner22379bc2007-01-11 03:54:27 +0000506 int getLocalSlot(const Value *V);
507 int getGlobalSlot(const GlobalValue *V);
Devang Patel320671d2009-07-08 21:44:25 +0000508 int getMetadataSlot(const MDNode *N);
Reid Spencerfc621e22004-06-09 15:26:53 +0000509
Misha Brukmanfd939082005-04-21 23:48:37 +0000510 /// If you'd like to deal with a function instead of just a module, use
Chris Lattner0d9574a2008-08-19 04:26:57 +0000511 /// this method to get its data into the SlotTracker.
Misha Brukmanfd939082005-04-21 23:48:37 +0000512 void incorporateFunction(const Function *F) {
513 TheFunction = F;
Reid Spencer28531c72004-08-16 07:46:33 +0000514 FunctionProcessed = false;
515 }
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000516
Misha Brukmanfd939082005-04-21 23:48:37 +0000517 /// After calling incorporateFunction, use this method to remove the
Chris Lattner0d9574a2008-08-19 04:26:57 +0000518 /// most recently incorporated function from the SlotTracker. This
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000519 /// will reset the state of the machine back to just the module contents.
520 void purgeFunction();
521
Devang Patel320671d2009-07-08 21:44:25 +0000522 /// MDNode map iterators.
523 ValueMap::iterator mdnBegin() { return mdnMap.begin(); }
524 ValueMap::iterator mdnEnd() { return mdnMap.end(); }
Dan Gohman9bf0b9b2009-08-12 23:54:22 +0000525 unsigned mdnSize() const { return mdnMap.size(); }
526 bool mdnEmpty() const { return mdnMap.empty(); }
Devang Patel320671d2009-07-08 21:44:25 +0000527
Reid Spencerb03de0c2004-05-26 21:56:09 +0000528 /// This function does the actual initialization.
529 inline void initialize();
530
Devang Patel320671d2009-07-08 21:44:25 +0000531 // Implementation Details
532private:
Chris Lattner9446bbe2007-01-09 07:55:49 +0000533 /// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
534 void CreateModuleSlot(const GlobalValue *V);
Devang Patel320671d2009-07-08 21:44:25 +0000535
536 /// CreateMetadataSlot - Insert the specified MDNode* into the slot table.
537 void CreateMetadataSlot(const MDNode *N);
538
Chris Lattner9446bbe2007-01-09 07:55:49 +0000539 /// CreateFunctionSlot - Insert the specified Value* into the slot table.
540 void CreateFunctionSlot(const Value *V);
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000541
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000542 /// Add all of the module level global variables (and their initializers)
543 /// and function declarations, but not the contents of those functions.
544 void processModule();
545
Devang Patel320671d2009-07-08 21:44:25 +0000546 /// Add all of the functions arguments, basic blocks, and instructions.
Reid Spencerb03de0c2004-05-26 21:56:09 +0000547 void processFunction();
548
Devang Patel320671d2009-07-08 21:44:25 +0000549 /// Add all MDNode operands.
550 void processMDNode();
551
Devang Patelc29d5b32009-07-30 00:02:57 +0000552 /// Add all MDNode operands.
553 void processNamedMDNode();
554
Chris Lattner0d9574a2008-08-19 04:26:57 +0000555 SlotTracker(const SlotTracker &); // DO NOT IMPLEMENT
556 void operator=(const SlotTracker &); // DO NOT IMPLEMENT
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000557};
558
Chris Lattnerb64871a2008-08-19 04:28:07 +0000559} // end anonymous namespace
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000560
Chris Lattner6ab910b2008-08-19 04:36:02 +0000561
562static SlotTracker *createSlotTracker(const Value *V) {
563 if (const Argument *FA = dyn_cast<Argument>(V))
564 return new SlotTracker(FA->getParent());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000565
Chris Lattner6ab910b2008-08-19 04:36:02 +0000566 if (const Instruction *I = dyn_cast<Instruction>(V))
567 return new SlotTracker(I->getParent()->getParent());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000568
Chris Lattner6ab910b2008-08-19 04:36:02 +0000569 if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
570 return new SlotTracker(BB->getParent());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000571
Chris Lattner6ab910b2008-08-19 04:36:02 +0000572 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
573 return new SlotTracker(GV->getParent());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000574
Chris Lattner6ab910b2008-08-19 04:36:02 +0000575 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V))
Daniel Dunbara279bc32009-09-20 02:20:51 +0000576 return new SlotTracker(GA->getParent());
577
Chris Lattner6ab910b2008-08-19 04:36:02 +0000578 if (const Function *Func = dyn_cast<Function>(V))
579 return new SlotTracker(Func);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000580
Chris Lattner6ab910b2008-08-19 04:36:02 +0000581 return 0;
582}
583
584#if 0
Dan Gohmanfd87a542009-07-25 01:43:01 +0000585#define ST_DEBUG(X) errs() << X
Chris Lattner6ab910b2008-08-19 04:36:02 +0000586#else
Chris Lattner24233032008-08-19 04:47:09 +0000587#define ST_DEBUG(X)
Chris Lattner6ab910b2008-08-19 04:36:02 +0000588#endif
589
590// Module level constructor. Causes the contents of the Module (sans functions)
591// to be added to the slot table.
592SlotTracker::SlotTracker(const Module *M)
Devang Patel320671d2009-07-08 21:44:25 +0000593 : TheModule(M), TheFunction(0), FunctionProcessed(false), TheMDNode(0),
Devang Patelc29d5b32009-07-30 00:02:57 +0000594 TheNamedMDNode(0), mNext(0), fNext(0), mdnNext(0) {
Chris Lattner6ab910b2008-08-19 04:36:02 +0000595}
596
597// Function level constructor. Causes the contents of the Module and the one
598// function provided to be added to the slot table.
599SlotTracker::SlotTracker(const Function *F)
Chris Lattnercfb5a202008-08-19 05:06:27 +0000600 : TheModule(F ? F->getParent() : 0), TheFunction(F), FunctionProcessed(false),
Devang Patelc29d5b32009-07-30 00:02:57 +0000601 TheMDNode(0), TheNamedMDNode(0), mNext(0), fNext(0), mdnNext(0) {
Devang Patel320671d2009-07-08 21:44:25 +0000602}
603
604// Constructor to handle single MDNode.
605SlotTracker::SlotTracker(const MDNode *C)
606 : TheModule(0), TheFunction(0), FunctionProcessed(false), TheMDNode(C),
Devang Patelc29d5b32009-07-30 00:02:57 +0000607 TheNamedMDNode(0), mNext(0), fNext(0), mdnNext(0) {
608}
609
610// Constructor to handle single NamedMDNode.
611SlotTracker::SlotTracker(const NamedMDNode *N)
612 : TheModule(0), TheFunction(0), FunctionProcessed(false), TheMDNode(0),
613 TheNamedMDNode(N), mNext(0), fNext(0), mdnNext(0) {
Chris Lattner6ab910b2008-08-19 04:36:02 +0000614}
615
616inline void SlotTracker::initialize() {
617 if (TheModule) {
618 processModule();
619 TheModule = 0; ///< Prevent re-processing next time we're called.
620 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000621
Chris Lattner6ab910b2008-08-19 04:36:02 +0000622 if (TheFunction && !FunctionProcessed)
623 processFunction();
Devang Patel320671d2009-07-08 21:44:25 +0000624
625 if (TheMDNode)
626 processMDNode();
Devang Patelc29d5b32009-07-30 00:02:57 +0000627
628 if (TheNamedMDNode)
629 processNamedMDNode();
Chris Lattner6ab910b2008-08-19 04:36:02 +0000630}
631
632// Iterate through all the global variables, functions, and global
633// variable initializers and create slots for them.
634void SlotTracker::processModule() {
Chris Lattner24233032008-08-19 04:47:09 +0000635 ST_DEBUG("begin processModule!\n");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000636
Chris Lattner6ab910b2008-08-19 04:36:02 +0000637 // Add all of the unnamed global variables to the value table.
638 for (Module::const_global_iterator I = TheModule->global_begin(),
Devang Patel320671d2009-07-08 21:44:25 +0000639 E = TheModule->global_end(); I != E; ++I) {
Daniel Dunbara279bc32009-09-20 02:20:51 +0000640 if (!I->hasName())
Chris Lattner6ab910b2008-08-19 04:36:02 +0000641 CreateModuleSlot(I);
Devang Patel320671d2009-07-08 21:44:25 +0000642 if (I->hasInitializer()) {
Daniel Dunbara279bc32009-09-20 02:20:51 +0000643 if (MDNode *N = dyn_cast<MDNode>(I->getInitializer()))
Devang Patel320671d2009-07-08 21:44:25 +0000644 CreateMetadataSlot(N);
645 }
646 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000647
Devang Patel37c4a2d2009-07-29 22:04:47 +0000648 // Add metadata used by named metadata.
Daniel Dunbara279bc32009-09-20 02:20:51 +0000649 for (Module::const_named_metadata_iterator
Devang Patel37c4a2d2009-07-29 22:04:47 +0000650 I = TheModule->named_metadata_begin(),
651 E = TheModule->named_metadata_end(); I != E; ++I) {
652 const NamedMDNode *NMD = I;
653 for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i) {
654 MDNode *MD = dyn_cast_or_null<MDNode>(NMD->getElement(i));
Devang Patele8861b82009-07-30 01:02:04 +0000655 if (MD)
656 CreateMetadataSlot(MD);
Devang Patel37c4a2d2009-07-29 22:04:47 +0000657 }
658 }
659
Chris Lattner6ab910b2008-08-19 04:36:02 +0000660 // Add all the unnamed functions to the table.
661 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
662 I != E; ++I)
663 if (!I->hasName())
664 CreateModuleSlot(I);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000665
Chris Lattner24233032008-08-19 04:47:09 +0000666 ST_DEBUG("end processModule!\n");
Chris Lattner6ab910b2008-08-19 04:36:02 +0000667}
668
Chris Lattner6ab910b2008-08-19 04:36:02 +0000669// Process the arguments, basic blocks, and instructions of a function.
670void SlotTracker::processFunction() {
Chris Lattner24233032008-08-19 04:47:09 +0000671 ST_DEBUG("begin processFunction!\n");
Chris Lattner6ab910b2008-08-19 04:36:02 +0000672 fNext = 0;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000673
Chris Lattner6ab910b2008-08-19 04:36:02 +0000674 // Add all the function arguments with no names.
675 for(Function::const_arg_iterator AI = TheFunction->arg_begin(),
676 AE = TheFunction->arg_end(); AI != AE; ++AI)
677 if (!AI->hasName())
678 CreateFunctionSlot(AI);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000679
Chris Lattner24233032008-08-19 04:47:09 +0000680 ST_DEBUG("Inserting Instructions:\n");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000681
Devang Patele30e6782009-09-28 21:41:20 +0000682 MetadataContext &TheMetadata = TheFunction->getContext().getMetadata();
Devang Patelf61b2372009-10-22 18:55:16 +0000683 typedef SmallVector<std::pair<unsigned, TrackingVH<MDNode> >, 2> MDMapTy;
684 MDMapTy MDs;
Devang Patel43215782009-09-16 20:21:17 +0000685
Chris Lattner6ab910b2008-08-19 04:36:02 +0000686 // Add all of the basic blocks and instructions with no names.
687 for (Function::const_iterator BB = TheFunction->begin(),
688 E = TheFunction->end(); BB != E; ++BB) {
689 if (!BB->hasName())
690 CreateFunctionSlot(BB);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000691 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E;
Devang Patel320671d2009-07-08 21:44:25 +0000692 ++I) {
Owen Anderson1d0be152009-08-13 21:58:54 +0000693 if (I->getType() != Type::getVoidTy(TheFunction->getContext()) &&
694 !I->hasName())
Chris Lattner6ab910b2008-08-19 04:36:02 +0000695 CreateFunctionSlot(I);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000696 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
Dan Gohman48cc73d2009-08-17 15:28:08 +0000697 if (MDNode *N = dyn_cast_or_null<MDNode>(I->getOperand(i)))
Devang Patel320671d2009-07-08 21:44:25 +0000698 CreateMetadataSlot(N);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000699
Devang Patel43215782009-09-16 20:21:17 +0000700 // Process metadata attached with this instruction.
Devang Patelf61b2372009-10-22 18:55:16 +0000701 MDs.clear();
702 TheMetadata.getMDs(I, MDs);
703 for (MDMapTy::const_iterator MI = MDs.begin(), ME = MDs.end(); MI != ME;
704 ++MI)
705 CreateMetadataSlot(MI->second);
Devang Patel320671d2009-07-08 21:44:25 +0000706 }
Chris Lattner6ab910b2008-08-19 04:36:02 +0000707 }
Devang Patel43215782009-09-16 20:21:17 +0000708
Chris Lattner6ab910b2008-08-19 04:36:02 +0000709 FunctionProcessed = true;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000710
Chris Lattner24233032008-08-19 04:47:09 +0000711 ST_DEBUG("end processFunction!\n");
Chris Lattner6ab910b2008-08-19 04:36:02 +0000712}
713
Devang Patel320671d2009-07-08 21:44:25 +0000714/// processMDNode - Process TheMDNode.
715void SlotTracker::processMDNode() {
716 ST_DEBUG("begin processMDNode!\n");
717 mdnNext = 0;
718 CreateMetadataSlot(TheMDNode);
719 TheMDNode = 0;
720 ST_DEBUG("end processMDNode!\n");
721}
722
Devang Patelc29d5b32009-07-30 00:02:57 +0000723/// processNamedMDNode - Process TheNamedMDNode.
724void SlotTracker::processNamedMDNode() {
725 ST_DEBUG("begin processNamedMDNode!\n");
726 mdnNext = 0;
727 for (unsigned i = 0, e = TheNamedMDNode->getNumElements(); i != e; ++i) {
728 MDNode *MD = dyn_cast_or_null<MDNode>(TheNamedMDNode->getElement(i));
729 if (MD)
730 CreateMetadataSlot(MD);
731 }
732 TheNamedMDNode = 0;
733 ST_DEBUG("end processNamedMDNode!\n");
734}
735
Chris Lattner6ab910b2008-08-19 04:36:02 +0000736/// Clean up after incorporating a function. This is the only way to get out of
737/// the function incorporation state that affects get*Slot/Create*Slot. Function
738/// incorporation state is indicated by TheFunction != 0.
739void SlotTracker::purgeFunction() {
Chris Lattner24233032008-08-19 04:47:09 +0000740 ST_DEBUG("begin purgeFunction!\n");
Chris Lattner6ab910b2008-08-19 04:36:02 +0000741 fMap.clear(); // Simply discard the function level map
742 TheFunction = 0;
743 FunctionProcessed = false;
Chris Lattner24233032008-08-19 04:47:09 +0000744 ST_DEBUG("end purgeFunction!\n");
Chris Lattner6ab910b2008-08-19 04:36:02 +0000745}
746
747/// getGlobalSlot - Get the slot number of a global value.
748int SlotTracker::getGlobalSlot(const GlobalValue *V) {
749 // Check for uninitialized state and do lazy initialization.
750 initialize();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000751
Chris Lattner6ab910b2008-08-19 04:36:02 +0000752 // Find the type plane in the module map
753 ValueMap::iterator MI = mMap.find(V);
Dan Gohmanaeaf2452008-10-01 19:58:59 +0000754 return MI == mMap.end() ? -1 : (int)MI->second;
Chris Lattner6ab910b2008-08-19 04:36:02 +0000755}
756
Devang Patel320671d2009-07-08 21:44:25 +0000757/// getGlobalSlot - Get the slot number of a MDNode.
758int SlotTracker::getMetadataSlot(const MDNode *N) {
759 // Check for uninitialized state and do lazy initialization.
760 initialize();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000761
Devang Patel320671d2009-07-08 21:44:25 +0000762 // Find the type plane in the module map
763 ValueMap::iterator MI = mdnMap.find(N);
764 return MI == mdnMap.end() ? -1 : (int)MI->second;
765}
766
Chris Lattner6ab910b2008-08-19 04:36:02 +0000767
768/// getLocalSlot - Get the slot number for a value that is local to a function.
769int SlotTracker::getLocalSlot(const Value *V) {
770 assert(!isa<Constant>(V) && "Can't get a constant or global slot with this!");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000771
Chris Lattner6ab910b2008-08-19 04:36:02 +0000772 // Check for uninitialized state and do lazy initialization.
773 initialize();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000774
Chris Lattner6ab910b2008-08-19 04:36:02 +0000775 ValueMap::iterator FI = fMap.find(V);
Dan Gohmanaeaf2452008-10-01 19:58:59 +0000776 return FI == fMap.end() ? -1 : (int)FI->second;
Chris Lattner6ab910b2008-08-19 04:36:02 +0000777}
778
779
780/// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
781void SlotTracker::CreateModuleSlot(const GlobalValue *V) {
782 assert(V && "Can't insert a null Value into SlotTracker!");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000783 assert(V->getType() != Type::getVoidTy(V->getContext()) &&
Owen Anderson1d0be152009-08-13 21:58:54 +0000784 "Doesn't need a slot!");
Chris Lattner6ab910b2008-08-19 04:36:02 +0000785 assert(!V->hasName() && "Doesn't need a slot!");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000786
Chris Lattner6ab910b2008-08-19 04:36:02 +0000787 unsigned DestSlot = mNext++;
788 mMap[V] = DestSlot;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000789
Chris Lattner24233032008-08-19 04:47:09 +0000790 ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" <<
Chris Lattner6ab910b2008-08-19 04:36:02 +0000791 DestSlot << " [");
792 // G = Global, F = Function, A = Alias, o = other
Chris Lattner24233032008-08-19 04:47:09 +0000793 ST_DEBUG((isa<GlobalVariable>(V) ? 'G' :
Chris Lattner6ab910b2008-08-19 04:36:02 +0000794 (isa<Function>(V) ? 'F' :
795 (isa<GlobalAlias>(V) ? 'A' : 'o'))) << "]\n");
796}
797
Chris Lattner6ab910b2008-08-19 04:36:02 +0000798/// CreateSlot - Create a new slot for the specified value if it has no name.
799void SlotTracker::CreateFunctionSlot(const Value *V) {
Daniel Dunbara279bc32009-09-20 02:20:51 +0000800 assert(V->getType() != Type::getVoidTy(TheFunction->getContext()) &&
Owen Anderson1d0be152009-08-13 21:58:54 +0000801 !V->hasName() && "Doesn't need a slot!");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000802
Chris Lattner6ab910b2008-08-19 04:36:02 +0000803 unsigned DestSlot = fNext++;
804 fMap[V] = DestSlot;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000805
Chris Lattner6ab910b2008-08-19 04:36:02 +0000806 // G = Global, F = Function, o = other
Chris Lattner24233032008-08-19 04:47:09 +0000807 ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" <<
Chris Lattner6ab910b2008-08-19 04:36:02 +0000808 DestSlot << " [o]\n");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000809}
Chris Lattner6ab910b2008-08-19 04:36:02 +0000810
Devang Patel320671d2009-07-08 21:44:25 +0000811/// CreateModuleSlot - Insert the specified MDNode* into the slot table.
812void SlotTracker::CreateMetadataSlot(const MDNode *N) {
813 assert(N && "Can't insert a null Value into SlotTracker!");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000814
Devang Patel320671d2009-07-08 21:44:25 +0000815 ValueMap::iterator I = mdnMap.find(N);
816 if (I != mdnMap.end())
817 return;
Chris Lattner6ab910b2008-08-19 04:36:02 +0000818
Devang Patel320671d2009-07-08 21:44:25 +0000819 unsigned DestSlot = mdnNext++;
820 mdnMap[N] = DestSlot;
821
Devang Patel028fa772009-10-21 21:25:09 +0000822 for (unsigned i = 0, e = N->getNumElements(); i != e; ++i) {
823 const Value *TV = N->getElement(i);
Devang Patel320671d2009-07-08 21:44:25 +0000824 if (TV)
Daniel Dunbara279bc32009-09-20 02:20:51 +0000825 if (const MDNode *N2 = dyn_cast<MDNode>(TV))
Devang Patel320671d2009-07-08 21:44:25 +0000826 CreateMetadataSlot(N2);
827 }
828}
Chris Lattner6ab910b2008-08-19 04:36:02 +0000829
830//===----------------------------------------------------------------------===//
831// AsmWriter Implementation
832//===----------------------------------------------------------------------===//
Chris Lattnerf082b802002-07-23 18:07:49 +0000833
Dan Gohman1220e102009-08-12 20:56:03 +0000834static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
Dan Gohmand6c0f652009-08-13 15:27:57 +0000835 TypePrinting *TypePrinter,
Chris Lattner0d9574a2008-08-19 04:26:57 +0000836 SlotTracker *Machine);
Reid Spencer0e25e1c2004-07-04 11:50:43 +0000837
Chris Lattnerc97536e2008-08-17 04:40:13 +0000838
Chris Lattner207b5bc2001-10-29 16:37:48 +0000839
Chris Lattner82c4bc72006-12-06 06:40:49 +0000840static const char *getPredicateText(unsigned predicate) {
Reid Spencer81dfeb32006-12-04 05:19:18 +0000841 const char * pred = "unknown";
842 switch (predicate) {
843 case FCmpInst::FCMP_FALSE: pred = "false"; break;
844 case FCmpInst::FCMP_OEQ: pred = "oeq"; break;
845 case FCmpInst::FCMP_OGT: pred = "ogt"; break;
846 case FCmpInst::FCMP_OGE: pred = "oge"; break;
847 case FCmpInst::FCMP_OLT: pred = "olt"; break;
848 case FCmpInst::FCMP_OLE: pred = "ole"; break;
849 case FCmpInst::FCMP_ONE: pred = "one"; break;
850 case FCmpInst::FCMP_ORD: pred = "ord"; break;
851 case FCmpInst::FCMP_UNO: pred = "uno"; break;
852 case FCmpInst::FCMP_UEQ: pred = "ueq"; break;
853 case FCmpInst::FCMP_UGT: pred = "ugt"; break;
854 case FCmpInst::FCMP_UGE: pred = "uge"; break;
855 case FCmpInst::FCMP_ULT: pred = "ult"; break;
856 case FCmpInst::FCMP_ULE: pred = "ule"; break;
857 case FCmpInst::FCMP_UNE: pred = "une"; break;
858 case FCmpInst::FCMP_TRUE: pred = "true"; break;
859 case ICmpInst::ICMP_EQ: pred = "eq"; break;
860 case ICmpInst::ICMP_NE: pred = "ne"; break;
861 case ICmpInst::ICMP_SGT: pred = "sgt"; break;
862 case ICmpInst::ICMP_SGE: pred = "sge"; break;
863 case ICmpInst::ICMP_SLT: pred = "slt"; break;
864 case ICmpInst::ICMP_SLE: pred = "sle"; break;
865 case ICmpInst::ICMP_UGT: pred = "ugt"; break;
866 case ICmpInst::ICMP_UGE: pred = "uge"; break;
867 case ICmpInst::ICMP_ULT: pred = "ult"; break;
868 case ICmpInst::ICMP_ULE: pred = "ule"; break;
869 }
870 return pred;
871}
872
Devang Patel2d5988d2009-09-30 20:16:54 +0000873static void WriteMDNodeComment(const MDNode *Node,
874 formatted_raw_ostream &Out) {
875 if (Node->getNumElements() < 1)
876 return;
Devang Patel4d7a2062009-09-30 21:26:51 +0000877 ConstantInt *CI = dyn_cast_or_null<ConstantInt>(Node->getElement(0));
Devang Patel2d5988d2009-09-30 20:16:54 +0000878 if (!CI) return;
879 unsigned Val = CI->getZExtValue();
880 unsigned Tag = Val & ~LLVMDebugVersionMask;
881 if (Val >= LLVMDebugVersion) {
882 if (Tag == dwarf::DW_TAG_auto_variable)
883 Out << "; [ DW_TAG_auto_variable ]";
884 else if (Tag == dwarf::DW_TAG_arg_variable)
885 Out << "; [ DW_TAG_arg_variable ]";
886 else if (Tag == dwarf::DW_TAG_return_variable)
887 Out << "; [ DW_TAG_return_variable ]";
888 else if (Tag == dwarf::DW_TAG_vector_type)
889 Out << "; [ DW_TAG_vector_type ]";
890 else if (Tag == dwarf::DW_TAG_user_base)
891 Out << "; [ DW_TAG_user_base ]";
892 else
893 Out << "; [" << dwarf::TagString(Tag) << " ]";
894 }
895}
896
Dan Gohman683e9222009-08-12 17:23:50 +0000897static void WriteMDNodes(formatted_raw_ostream &Out, TypePrinting &TypePrinter,
Devang Patel320671d2009-07-08 21:44:25 +0000898 SlotTracker &Machine) {
899 SmallVector<const MDNode *, 16> Nodes;
900 Nodes.resize(Machine.mdnSize());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000901 for (SlotTracker::ValueMap::iterator I =
902 Machine.mdnBegin(), E = Machine.mdnEnd(); I != E; ++I)
Devang Patel320671d2009-07-08 21:44:25 +0000903 Nodes[I->second] = cast<MDNode>(I->first);
904
905 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Devang Patel2214c942009-07-08 21:57:07 +0000906 Out << '!' << i << " = metadata ";
Devang Patel320671d2009-07-08 21:44:25 +0000907 const MDNode *Node = Nodes[i];
908 Out << "!{";
Devang Patel028fa772009-10-21 21:25:09 +0000909 for (unsigned mi = 0, me = Node->getNumElements(); mi != me; ++mi) {
910 const Value *V = Node->getElement(mi);
Devang Patel320671d2009-07-08 21:44:25 +0000911 if (!V)
912 Out << "null";
913 else if (const MDNode *N = dyn_cast<MDNode>(V)) {
914 Out << "metadata ";
915 Out << '!' << Machine.getMetadataSlot(N);
916 }
917 else {
Devang Patel028fa772009-10-21 21:25:09 +0000918 TypePrinter.print(V->getType(), Out);
Devang Patel320671d2009-07-08 21:44:25 +0000919 Out << ' ';
Devang Patel028fa772009-10-21 21:25:09 +0000920 WriteAsOperandInternal(Out, Node->getElement(mi),
921 &TypePrinter, &Machine);
Devang Patel320671d2009-07-08 21:44:25 +0000922 }
Devang Patel028fa772009-10-21 21:25:09 +0000923 if (mi + 1 != me)
Devang Patel320671d2009-07-08 21:44:25 +0000924 Out << ", ";
925 }
Devang Patel2d5988d2009-09-30 20:16:54 +0000926
927 Out << "}";
928 WriteMDNodeComment(Node, Out);
929 Out << "\n";
Devang Patel320671d2009-07-08 21:44:25 +0000930 }
931}
932
Dan Gohman1220e102009-08-12 20:56:03 +0000933static void WriteOptimizationInfo(raw_ostream &Out, const User *U) {
Dan Gohman1224c382009-07-20 21:19:07 +0000934 if (const OverflowingBinaryOperator *OBO =
935 dyn_cast<OverflowingBinaryOperator>(U)) {
Dan Gohman5078f842009-08-20 17:11:38 +0000936 if (OBO->hasNoUnsignedWrap())
Dan Gohman59858cf2009-07-27 16:11:46 +0000937 Out << " nuw";
Dan Gohman5078f842009-08-20 17:11:38 +0000938 if (OBO->hasNoSignedWrap())
Dan Gohman59858cf2009-07-27 16:11:46 +0000939 Out << " nsw";
Dan Gohman1224c382009-07-20 21:19:07 +0000940 } else if (const SDivOperator *Div = dyn_cast<SDivOperator>(U)) {
941 if (Div->isExact())
Dan Gohman59858cf2009-07-27 16:11:46 +0000942 Out << " exact";
Dan Gohmandd8004d2009-07-27 21:53:46 +0000943 } else if (const GEPOperator *GEP = dyn_cast<GEPOperator>(U)) {
944 if (GEP->isInBounds())
945 Out << " inbounds";
Dan Gohman1224c382009-07-20 21:19:07 +0000946 }
947}
948
Dan Gohman1220e102009-08-12 20:56:03 +0000949static void WriteConstantInt(raw_ostream &Out, const Constant *CV,
Chris Lattner9cc34462009-02-28 20:25:14 +0000950 TypePrinting &TypePrinter, SlotTracker *Machine) {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +0000951 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Owen Anderson1d0be152009-08-13 21:58:54 +0000952 if (CI->getType() == Type::getInt1Ty(CV->getContext())) {
Reid Spencer579dca12007-01-12 04:24:46 +0000953 Out << (CI->getZExtValue() ? "true" : "false");
Chris Lattnerfad86b02008-08-17 07:19:36 +0000954 return;
955 }
956 Out << CI->getValue();
957 return;
958 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000959
Chris Lattnerfad86b02008-08-17 07:19:36 +0000960 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000961 if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEdouble ||
962 &CFP->getValueAPF().getSemantics() == &APFloat::IEEEsingle) {
963 // We would like to output the FP constant value in exponential notation,
964 // but we cannot do this if doing so will lose precision. Check here to
965 // make sure that we only output it in exponential format if we can parse
966 // the value back and get the same value.
967 //
Dale Johannesen541ed9f2009-01-21 20:32:55 +0000968 bool ignored;
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000969 bool isDouble = &CFP->getValueAPF().getSemantics()==&APFloat::IEEEdouble;
Chris Lattnerfad86b02008-08-17 07:19:36 +0000970 double Val = isDouble ? CFP->getValueAPF().convertToDouble() :
971 CFP->getValueAPF().convertToFloat();
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000972 std::string StrVal = ftostr(CFP->getValueAPF());
Chris Lattner66e810b2002-04-18 18:53:13 +0000973
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000974 // Check to make sure that the stringized number is not some string like
975 // "Inf" or NaN, that atof will accept, but the lexer will not. Check
976 // that the string matches the "[-+]?[0-9]" regex.
977 //
978 if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
979 ((StrVal[0] == '-' || StrVal[0] == '+') &&
980 (StrVal[1] >= '0' && StrVal[1] <= '9'))) {
981 // Reparse stringized version!
982 if (atof(StrVal.c_str()) == Val) {
983 Out << StrVal;
984 return;
985 }
Chris Lattner66e810b2002-04-18 18:53:13 +0000986 }
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000987 // Otherwise we could not reparse it to exactly the same value, so we must
Dale Johannesen541ed9f2009-01-21 20:32:55 +0000988 // output the string in hexadecimal format! Note that loading and storing
989 // floating point types changes the bits of NaNs on some hosts, notably
990 // x86, so we must not use these types.
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000991 assert(sizeof(double) == sizeof(uint64_t) &&
992 "assuming that double is 64 bits!");
Chris Lattnerc6a13462008-11-10 04:30:26 +0000993 char Buffer[40];
Dale Johannesen541ed9f2009-01-21 20:32:55 +0000994 APFloat apf = CFP->getValueAPF();
995 // Floats are represented in ASCII IR as double, convert.
996 if (!isDouble)
Daniel Dunbara279bc32009-09-20 02:20:51 +0000997 apf.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven,
Dale Johannesen541ed9f2009-01-21 20:32:55 +0000998 &ignored);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000999 Out << "0x" <<
1000 utohex_buffer(uint64_t(apf.bitcastToAPInt().getZExtValue()),
Dale Johannesen541ed9f2009-01-21 20:32:55 +00001001 Buffer+40);
Chris Lattnercfb5a202008-08-19 05:06:27 +00001002 return;
1003 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001004
Chris Lattnercfb5a202008-08-19 05:06:27 +00001005 // Some form of long double. These appear as a magic letter identifying
1006 // the type, then a fixed number of hex digits.
1007 Out << "0x";
Dale Johannesen1b25cb22009-03-23 21:16:53 +00001008 if (&CFP->getValueAPF().getSemantics() == &APFloat::x87DoubleExtended) {
Chris Lattnercfb5a202008-08-19 05:06:27 +00001009 Out << 'K';
Dale Johannesen1b25cb22009-03-23 21:16:53 +00001010 // api needed to prevent premature destruction
1011 APInt api = CFP->getValueAPF().bitcastToAPInt();
1012 const uint64_t* p = api.getRawData();
1013 uint64_t word = p[1];
1014 int shiftcount=12;
1015 int width = api.getBitWidth();
1016 for (int j=0; j<width; j+=4, shiftcount-=4) {
1017 unsigned int nibble = (word>>shiftcount) & 15;
1018 if (nibble < 10)
1019 Out << (unsigned char)(nibble + '0');
1020 else
1021 Out << (unsigned char)(nibble - 10 + 'A');
1022 if (shiftcount == 0 && j+4 < width) {
1023 word = *p;
1024 shiftcount = 64;
1025 if (width-j-4 < 64)
1026 shiftcount = width-j-4;
1027 }
1028 }
1029 return;
1030 } else if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEquad)
Chris Lattnercfb5a202008-08-19 05:06:27 +00001031 Out << 'L';
1032 else if (&CFP->getValueAPF().getSemantics() == &APFloat::PPCDoubleDouble)
1033 Out << 'M';
1034 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001035 llvm_unreachable("Unsupported floating point type");
Chris Lattnercfb5a202008-08-19 05:06:27 +00001036 // api needed to prevent premature destruction
Dale Johannesen7111b022008-10-09 18:53:47 +00001037 APInt api = CFP->getValueAPF().bitcastToAPInt();
Chris Lattnercfb5a202008-08-19 05:06:27 +00001038 const uint64_t* p = api.getRawData();
1039 uint64_t word = *p;
1040 int shiftcount=60;
1041 int width = api.getBitWidth();
1042 for (int j=0; j<width; j+=4, shiftcount-=4) {
1043 unsigned int nibble = (word>>shiftcount) & 15;
1044 if (nibble < 10)
1045 Out << (unsigned char)(nibble + '0');
Dale Johannesen9d5f4562007-09-12 03:30:33 +00001046 else
Chris Lattnercfb5a202008-08-19 05:06:27 +00001047 Out << (unsigned char)(nibble - 10 + 'A');
1048 if (shiftcount == 0 && j+4 < width) {
1049 word = *(++p);
1050 shiftcount = 64;
1051 if (width-j-4 < 64)
1052 shiftcount = width-j-4;
Dale Johannesen9d5f4562007-09-12 03:30:33 +00001053 }
1054 }
Chris Lattnercfb5a202008-08-19 05:06:27 +00001055 return;
1056 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001057
Chris Lattnercfb5a202008-08-19 05:06:27 +00001058 if (isa<ConstantAggregateZero>(CV)) {
Chris Lattnerde512b52004-02-15 05:55:15 +00001059 Out << "zeroinitializer";
Chris Lattnercfb5a202008-08-19 05:06:27 +00001060 return;
1061 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001062
Chris Lattnercfb5a202008-08-19 05:06:27 +00001063 if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
Chris Lattner66e810b2002-04-18 18:53:13 +00001064 // As a special case, print the array as a string if it is an array of
Dan Gohman9b38d7d2008-05-12 16:34:30 +00001065 // i8 with ConstantInt values.
Misha Brukmanfd939082005-04-21 23:48:37 +00001066 //
Chris Lattner66e810b2002-04-18 18:53:13 +00001067 const Type *ETy = CA->getType()->getElementType();
Chris Lattner18365502006-01-23 23:03:36 +00001068 if (CA->isString()) {
Chris Lattner66e810b2002-04-18 18:53:13 +00001069 Out << "c\"";
Chris Lattner18365502006-01-23 23:03:36 +00001070 PrintEscapedString(CA->getAsString(), Out);
Chris Lattnercfb5a202008-08-19 05:06:27 +00001071 Out << '"';
Chris Lattner66e810b2002-04-18 18:53:13 +00001072 } else { // Cannot output in string format...
Misha Brukman40c732c2004-06-04 21:11:51 +00001073 Out << '[';
Chris Lattner7a716ad2002-04-16 21:36:08 +00001074 if (CA->getNumOperands()) {
Chris Lattner0f7364b2009-02-28 21:26:53 +00001075 TypePrinter.print(ETy, Out);
Dan Gohman8dae1382008-09-14 17:21:12 +00001076 Out << ' ';
Chris Lattner7a716ad2002-04-16 21:36:08 +00001077 WriteAsOperandInternal(Out, CA->getOperand(0),
Dan Gohmand6c0f652009-08-13 15:27:57 +00001078 &TypePrinter, Machine);
Chris Lattner7a716ad2002-04-16 21:36:08 +00001079 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
1080 Out << ", ";
Chris Lattner0f7364b2009-02-28 21:26:53 +00001081 TypePrinter.print(ETy, Out);
Dan Gohman8dae1382008-09-14 17:21:12 +00001082 Out << ' ';
Dan Gohmand6c0f652009-08-13 15:27:57 +00001083 WriteAsOperandInternal(Out, CA->getOperand(i), &TypePrinter, Machine);
Chris Lattner7a716ad2002-04-16 21:36:08 +00001084 }
1085 }
Dan Gohman8dae1382008-09-14 17:21:12 +00001086 Out << ']';
Chris Lattner7a716ad2002-04-16 21:36:08 +00001087 }
Chris Lattnercfb5a202008-08-19 05:06:27 +00001088 return;
1089 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001090
Chris Lattnercfb5a202008-08-19 05:06:27 +00001091 if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
Andrew Lenharth43f344a2007-01-08 18:21:30 +00001092 if (CS->getType()->isPacked())
1093 Out << '<';
Misha Brukman40c732c2004-06-04 21:11:51 +00001094 Out << '{';
Jim Laskeya3f332b2006-02-25 12:27:03 +00001095 unsigned N = CS->getNumOperands();
1096 if (N) {
Chris Lattner24233032008-08-19 04:47:09 +00001097 Out << ' ';
Chris Lattner0f7364b2009-02-28 21:26:53 +00001098 TypePrinter.print(CS->getOperand(0)->getType(), Out);
Dan Gohman8dae1382008-09-14 17:21:12 +00001099 Out << ' ';
Chris Lattner7a716ad2002-04-16 21:36:08 +00001100
Dan Gohmand6c0f652009-08-13 15:27:57 +00001101 WriteAsOperandInternal(Out, CS->getOperand(0), &TypePrinter, Machine);
Chris Lattner7a716ad2002-04-16 21:36:08 +00001102
Jim Laskeya3f332b2006-02-25 12:27:03 +00001103 for (unsigned i = 1; i < N; i++) {
Chris Lattner7a716ad2002-04-16 21:36:08 +00001104 Out << ", ";
Chris Lattner0f7364b2009-02-28 21:26:53 +00001105 TypePrinter.print(CS->getOperand(i)->getType(), Out);
Dan Gohman8dae1382008-09-14 17:21:12 +00001106 Out << ' ';
Chris Lattner7a716ad2002-04-16 21:36:08 +00001107
Dan Gohmand6c0f652009-08-13 15:27:57 +00001108 WriteAsOperandInternal(Out, CS->getOperand(i), &TypePrinter, Machine);
Chris Lattner7a716ad2002-04-16 21:36:08 +00001109 }
Dan Gohman8dae1382008-09-14 17:21:12 +00001110 Out << ' ';
Chris Lattner7a716ad2002-04-16 21:36:08 +00001111 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001112
Dan Gohman8dae1382008-09-14 17:21:12 +00001113 Out << '}';
Andrew Lenharth43f344a2007-01-08 18:21:30 +00001114 if (CS->getType()->isPacked())
1115 Out << '>';
Chris Lattnercfb5a202008-08-19 05:06:27 +00001116 return;
1117 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001118
Chris Lattnercfb5a202008-08-19 05:06:27 +00001119 if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
1120 const Type *ETy = CP->getType()->getElementType();
1121 assert(CP->getNumOperands() > 0 &&
1122 "Number of operands for a PackedConst must be > 0");
Dan Gohman7dfa07f2009-02-11 00:25:25 +00001123 Out << '<';
Chris Lattner0f7364b2009-02-28 21:26:53 +00001124 TypePrinter.print(ETy, Out);
Dan Gohman8dae1382008-09-14 17:21:12 +00001125 Out << ' ';
Dan Gohmand6c0f652009-08-13 15:27:57 +00001126 WriteAsOperandInternal(Out, CP->getOperand(0), &TypePrinter, Machine);
Chris Lattnercfb5a202008-08-19 05:06:27 +00001127 for (unsigned i = 1, e = CP->getNumOperands(); i != e; ++i) {
Chris Lattner4667b712008-08-19 05:26:17 +00001128 Out << ", ";
Chris Lattner0f7364b2009-02-28 21:26:53 +00001129 TypePrinter.print(ETy, Out);
Dan Gohman8dae1382008-09-14 17:21:12 +00001130 Out << ' ';
Dan Gohmand6c0f652009-08-13 15:27:57 +00001131 WriteAsOperandInternal(Out, CP->getOperand(i), &TypePrinter, Machine);
Chris Lattnercfb5a202008-08-19 05:06:27 +00001132 }
Dan Gohman7dfa07f2009-02-11 00:25:25 +00001133 Out << '>';
Chris Lattnercfb5a202008-08-19 05:06:27 +00001134 return;
1135 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001136
Chris Lattnercfb5a202008-08-19 05:06:27 +00001137 if (isa<ConstantPointerNull>(CV)) {
Chris Lattner7a716ad2002-04-16 21:36:08 +00001138 Out << "null";
Chris Lattnercfb5a202008-08-19 05:06:27 +00001139 return;
1140 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001141
Chris Lattnercfb5a202008-08-19 05:06:27 +00001142 if (isa<UndefValue>(CV)) {
Chris Lattnerb976e662004-10-16 18:08:06 +00001143 Out << "undef";
Chris Lattnercfb5a202008-08-19 05:06:27 +00001144 return;
1145 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001146
Devang Patel320671d2009-07-08 21:44:25 +00001147 if (const MDNode *Node = dyn_cast<MDNode>(CV)) {
1148 Out << "!" << Machine->getMetadataSlot(Node);
1149 return;
1150 }
1151
Chris Lattnercfb5a202008-08-19 05:06:27 +00001152 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Reid Spencer81dfeb32006-12-04 05:19:18 +00001153 Out << CE->getOpcodeName();
Dan Gohman59858cf2009-07-27 16:11:46 +00001154 WriteOptimizationInfo(Out, CE);
Reid Spencer81dfeb32006-12-04 05:19:18 +00001155 if (CE->isCompare())
Chris Lattnercfb5a202008-08-19 05:06:27 +00001156 Out << ' ' << getPredicateText(CE->getPredicate());
Reid Spencer81dfeb32006-12-04 05:19:18 +00001157 Out << " (";
Misha Brukmanfd939082005-04-21 23:48:37 +00001158
Vikram S. Adveb4dbb442002-07-14 23:14:45 +00001159 for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) {
Chris Lattner0f7364b2009-02-28 21:26:53 +00001160 TypePrinter.print((*OI)->getType(), Out);
Dan Gohman8dae1382008-09-14 17:21:12 +00001161 Out << ' ';
Dan Gohmand6c0f652009-08-13 15:27:57 +00001162 WriteAsOperandInternal(Out, *OI, &TypePrinter, Machine);
Vikram S. Adveb4dbb442002-07-14 23:14:45 +00001163 if (OI+1 != CE->op_end())
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001164 Out << ", ";
Vikram S. Adveb4dbb442002-07-14 23:14:45 +00001165 }
Misha Brukmanfd939082005-04-21 23:48:37 +00001166
Dan Gohman995be7d2008-05-31 19:12:39 +00001167 if (CE->hasIndices()) {
1168 const SmallVector<unsigned, 4> &Indices = CE->getIndices();
1169 for (unsigned i = 0, e = Indices.size(); i != e; ++i)
1170 Out << ", " << Indices[i];
1171 }
1172
Reid Spencer3da59db2006-11-27 01:05:10 +00001173 if (CE->isCast()) {
Chris Lattner95586b82002-08-15 19:37:43 +00001174 Out << " to ";
Chris Lattner0f7364b2009-02-28 21:26:53 +00001175 TypePrinter.print(CE->getType(), Out);
Chris Lattner95586b82002-08-15 19:37:43 +00001176 }
Reid Spencer3da59db2006-11-27 01:05:10 +00001177
Misha Brukman40c732c2004-06-04 21:11:51 +00001178 Out << ')';
Chris Lattnercfb5a202008-08-19 05:06:27 +00001179 return;
Chris Lattner7a716ad2002-04-16 21:36:08 +00001180 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001181
Chris Lattnercfb5a202008-08-19 05:06:27 +00001182 Out << "<placeholder or erroneous Constant>";
Chris Lattner7a716ad2002-04-16 21:36:08 +00001183}
1184
1185
Misha Brukmanab5c6002004-03-02 00:22:19 +00001186/// WriteAsOperand - Write the name of the specified value out to the specified
1187/// ostream. This can be useful when you just want to print int %reg126, not
1188/// the whole instruction that generated it.
1189///
Dan Gohman1220e102009-08-12 20:56:03 +00001190static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
Dan Gohmand6c0f652009-08-13 15:27:57 +00001191 TypePrinting *TypePrinter,
Chris Lattner0d9574a2008-08-19 04:26:57 +00001192 SlotTracker *Machine) {
Chris Lattnerc97536e2008-08-17 04:40:13 +00001193 if (V->hasName()) {
1194 PrintLLVMName(Out, V);
1195 return;
1196 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001197
Chris Lattnerc97536e2008-08-17 04:40:13 +00001198 const Constant *CV = dyn_cast<Constant>(V);
1199 if (CV && !isa<GlobalValue>(CV)) {
Dan Gohmand6c0f652009-08-13 15:27:57 +00001200 assert(TypePrinter && "Constants require TypePrinting!");
1201 WriteConstantInt(Out, CV, *TypePrinter, Machine);
Chris Lattnercfb5a202008-08-19 05:06:27 +00001202 return;
1203 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001204
Chris Lattnercfb5a202008-08-19 05:06:27 +00001205 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
Chris Lattnerc97536e2008-08-17 04:40:13 +00001206 Out << "asm ";
1207 if (IA->hasSideEffects())
1208 Out << "sideeffect ";
Dale Johannesen8ba2d5b2009-10-21 23:28:00 +00001209 if (IA->isAlignStack())
1210 Out << "alignstack ";
Chris Lattnerc97536e2008-08-17 04:40:13 +00001211 Out << '"';
1212 PrintEscapedString(IA->getAsmString(), Out);
1213 Out << "\", \"";
1214 PrintEscapedString(IA->getConstraintString(), Out);
1215 Out << '"';
Chris Lattnercfb5a202008-08-19 05:06:27 +00001216 return;
1217 }
Devang Patele54abc92009-07-22 17:43:22 +00001218
Devang Patel104cf9e2009-07-23 01:07:34 +00001219 if (const MDNode *N = dyn_cast<MDNode>(V)) {
1220 Out << '!' << Machine->getMetadataSlot(N);
1221 return;
1222 }
1223
Devang Patele54abc92009-07-22 17:43:22 +00001224 if (const MDString *MDS = dyn_cast<MDString>(V)) {
Devang Patele54abc92009-07-22 17:43:22 +00001225 Out << "!\"";
Daniel Dunbar03d76512009-07-25 23:55:21 +00001226 PrintEscapedString(MDS->getString(), Out);
Devang Patele54abc92009-07-22 17:43:22 +00001227 Out << '"';
1228 return;
1229 }
1230
Dan Gohmancd26ec52009-09-23 01:33:16 +00001231 if (V->getValueID() == Value::PseudoSourceValueVal) {
1232 V->print(Out);
1233 return;
1234 }
1235
Chris Lattnercfb5a202008-08-19 05:06:27 +00001236 char Prefix = '%';
1237 int Slot;
1238 if (Machine) {
1239 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1240 Slot = Machine->getGlobalSlot(GV);
1241 Prefix = '@';
1242 } else {
1243 Slot = Machine->getLocalSlot(V);
1244 }
Chris Lattnerc97536e2008-08-17 04:40:13 +00001245 } else {
Chris Lattnercfb5a202008-08-19 05:06:27 +00001246 Machine = createSlotTracker(V);
Chris Lattnerc97536e2008-08-17 04:40:13 +00001247 if (Machine) {
1248 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1249 Slot = Machine->getGlobalSlot(GV);
1250 Prefix = '@';
1251 } else {
1252 Slot = Machine->getLocalSlot(V);
1253 }
Dan Gohmand6c0f652009-08-13 15:27:57 +00001254 delete Machine;
Chris Lattner80cd1152006-01-25 22:26:05 +00001255 } else {
Chris Lattnercfb5a202008-08-19 05:06:27 +00001256 Slot = -1;
Chris Lattner7a716ad2002-04-16 21:36:08 +00001257 }
1258 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001259
Chris Lattnercfb5a202008-08-19 05:06:27 +00001260 if (Slot != -1)
1261 Out << Prefix << Slot;
1262 else
1263 Out << "<badref>";
Chris Lattner7a716ad2002-04-16 21:36:08 +00001264}
1265
Dan Gohman1220e102009-08-12 20:56:03 +00001266void llvm::WriteAsOperand(raw_ostream &Out, const Value *V,
1267 bool PrintType, const Module *Context) {
Dan Gohmand6c0f652009-08-13 15:27:57 +00001268
1269 // Fast path: Don't construct and populate a TypePrinting object if we
1270 // won't be needing any types printed.
Dan Gohman009fc9e2009-08-13 23:07:11 +00001271 if (!PrintType &&
1272 (!isa<Constant>(V) || V->hasName() || isa<GlobalValue>(V))) {
Dan Gohmand6c0f652009-08-13 15:27:57 +00001273 WriteAsOperandInternal(Out, V, 0, 0);
1274 return;
1275 }
1276
Chris Lattner607dc682002-07-10 16:48:17 +00001277 if (Context == 0) Context = getModuleFromVal(V);
Chris Lattner207b5bc2001-10-29 16:37:48 +00001278
Chris Lattnere9fa33e2009-02-28 23:20:19 +00001279 TypePrinting TypePrinter;
Chris Lattner413fd232009-03-01 00:03:38 +00001280 std::vector<const Type*> NumberedTypes;
1281 AddModuleTypesToPrinter(TypePrinter, NumberedTypes, Context);
Dan Gohman8dae1382008-09-14 17:21:12 +00001282 if (PrintType) {
Chris Lattner0f7364b2009-02-28 21:26:53 +00001283 TypePrinter.print(V->getType(), Out);
Dan Gohman8dae1382008-09-14 17:21:12 +00001284 Out << ' ';
1285 }
Misha Brukmanfd939082005-04-21 23:48:37 +00001286
Dan Gohmand6c0f652009-08-13 15:27:57 +00001287 WriteAsOperandInternal(Out, V, &TypePrinter, 0);
Chris Lattner622f7402001-07-20 19:15:21 +00001288}
1289
Chris Lattnercfb5a202008-08-19 05:06:27 +00001290namespace {
Chris Lattnerd8c2e422001-07-12 23:35:26 +00001291
Chris Lattner007377f2001-09-07 16:36:04 +00001292class AssemblyWriter {
Dan Gohman683e9222009-08-12 17:23:50 +00001293 formatted_raw_ostream &Out;
Chris Lattner0d9574a2008-08-19 04:26:57 +00001294 SlotTracker &Machine;
Chris Lattnerc1824992001-10-29 16:05:51 +00001295 const Module *TheModule;
Chris Lattner9cc34462009-02-28 20:25:14 +00001296 TypePrinting TypePrinter;
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001297 AssemblyAnnotationWriter *AnnotationWriter;
Chris Lattner413fd232009-03-01 00:03:38 +00001298 std::vector<const Type*> NumberedTypes;
Devang Patelce6a1c92009-10-22 01:01:24 +00001299 DenseMap<unsigned, StringRef> MDNames;
Devang Patel923078c2009-07-01 19:21:12 +00001300
Chris Lattner00950542001-06-06 20:29:01 +00001301public:
Dan Gohman683e9222009-08-12 17:23:50 +00001302 inline AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac,
1303 const Module *M,
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001304 AssemblyAnnotationWriter *AAW)
Devang Patel3168b792009-09-28 18:31:56 +00001305 : Out(o), Machine(Mac), TheModule(M), AnnotationWriter(AAW) {
Chris Lattner413fd232009-03-01 00:03:38 +00001306 AddModuleTypesToPrinter(TypePrinter, NumberedTypes, M);
Devang Patel18f0c262009-09-28 20:56:00 +00001307 // FIXME: Provide MDPrinter
Devang Patel7f93f4d2009-10-07 16:37:55 +00001308 if (M) {
1309 MetadataContext &TheMetadata = M->getContext().getMetadata();
Devang Patelce6a1c92009-10-22 01:01:24 +00001310 SmallVector<std::pair<unsigned, StringRef>, 4> Names;
1311 TheMetadata.getHandlerNames(Names);
1312 for (SmallVector<std::pair<unsigned, StringRef>, 4>::iterator
1313 I = Names.begin(),
1314 E = Names.end(); I != E; ++I) {
1315 MDNames[I->first] = I->second;
Devang Patel7f93f4d2009-10-07 16:37:55 +00001316 }
Devang Patel18f0c262009-09-28 20:56:00 +00001317 }
Chris Lattner00950542001-06-06 20:29:01 +00001318 }
1319
Chris Lattner413fd232009-03-01 00:03:38 +00001320 void write(const Module *M) { printModule(M); }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001321
Chris Lattner944fac72008-08-23 22:23:09 +00001322 void write(const GlobalValue *G) {
1323 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(G))
1324 printGlobal(GV);
1325 else if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(G))
1326 printAlias(GA);
1327 else if (const Function *F = dyn_cast<Function>(G))
1328 printFunction(F);
1329 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001330 llvm_unreachable("Unknown global");
Chris Lattner944fac72008-08-23 22:23:09 +00001331 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001332
Chris Lattnercfb5a202008-08-19 05:06:27 +00001333 void write(const BasicBlock *BB) { printBasicBlock(BB); }
1334 void write(const Instruction *I) { printInstruction(*I); }
Chris Lattner00950542001-06-06 20:29:01 +00001335
Chris Lattner2fcfdb72006-12-06 06:24:27 +00001336 void writeOperand(const Value *Op, bool PrintType);
Devang Pateleaf42ab2008-09-23 23:03:40 +00001337 void writeParamOperand(const Value *Operand, Attributes Attrs);
Chris Lattner66e810b2002-04-18 18:53:13 +00001338
Misha Brukmanf771bea2004-11-15 19:30:05 +00001339private:
Chris Lattnerc1824992001-10-29 16:05:51 +00001340 void printModule(const Module *M);
Reid Spencer78d033e2007-01-06 07:24:44 +00001341 void printTypeSymbolTable(const TypeSymbolTable &ST);
Chris Lattnerc1824992001-10-29 16:05:51 +00001342 void printGlobal(const GlobalVariable *GV);
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001343 void printAlias(const GlobalAlias *GV);
Chris Lattner79df7c02002-03-26 18:01:55 +00001344 void printFunction(const Function *F);
Devang Pateleaf42ab2008-09-23 23:03:40 +00001345 void printArgument(const Argument *FA, Attributes Attrs);
Chris Lattnerc1824992001-10-29 16:05:51 +00001346 void printBasicBlock(const BasicBlock *BB);
Chris Lattner7e708292002-06-25 16:13:24 +00001347 void printInstruction(const Instruction &I);
Chris Lattner2761e9f2002-04-13 20:53:41 +00001348
Chris Lattnere02fa852001-10-13 06:42:36 +00001349 // printInfoComment - Print a little comment after the instruction indicating
1350 // which slot it occupies.
Chris Lattner7e708292002-06-25 16:13:24 +00001351 void printInfoComment(const Value &V);
Chris Lattner00950542001-06-06 20:29:01 +00001352};
Chris Lattner413fd232009-03-01 00:03:38 +00001353} // end of anonymous namespace
Chris Lattner00950542001-06-06 20:29:01 +00001354
Chris Lattner2761e9f2002-04-13 20:53:41 +00001355
Chris Lattner2fcfdb72006-12-06 06:24:27 +00001356void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType) {
1357 if (Operand == 0) {
Chris Lattneraab18202005-02-24 16:58:29 +00001358 Out << "<null operand!>";
Chris Lattner2fcfdb72006-12-06 06:24:27 +00001359 } else {
Chris Lattnercfb5a202008-08-19 05:06:27 +00001360 if (PrintType) {
Chris Lattner0f7364b2009-02-28 21:26:53 +00001361 TypePrinter.print(Operand->getType(), Out);
Dan Gohman8dae1382008-09-14 17:21:12 +00001362 Out << ' ';
Chris Lattnercfb5a202008-08-19 05:06:27 +00001363 }
Dan Gohmand6c0f652009-08-13 15:27:57 +00001364 WriteAsOperandInternal(Out, Operand, &TypePrinter, &Machine);
Chris Lattneraab18202005-02-24 16:58:29 +00001365 }
Chris Lattner00950542001-06-06 20:29:01 +00001366}
1367
Daniel Dunbara279bc32009-09-20 02:20:51 +00001368void AssemblyWriter::writeParamOperand(const Value *Operand,
Devang Pateleaf42ab2008-09-23 23:03:40 +00001369 Attributes Attrs) {
Duncan Sandsdc024672007-11-27 13:23:08 +00001370 if (Operand == 0) {
1371 Out << "<null operand!>";
1372 } else {
Duncan Sandsdc024672007-11-27 13:23:08 +00001373 // Print the type
Chris Lattner0f7364b2009-02-28 21:26:53 +00001374 TypePrinter.print(Operand->getType(), Out);
Duncan Sandsdc024672007-11-27 13:23:08 +00001375 // Print parameter attributes list
Devang Patel05988662008-09-25 21:00:45 +00001376 if (Attrs != Attribute::None)
1377 Out << ' ' << Attribute::getAsString(Attrs);
Dan Gohman8dae1382008-09-14 17:21:12 +00001378 Out << ' ';
Duncan Sandsdc024672007-11-27 13:23:08 +00001379 // Print the operand
Dan Gohmand6c0f652009-08-13 15:27:57 +00001380 WriteAsOperandInternal(Out, Operand, &TypePrinter, &Machine);
Duncan Sandsdc024672007-11-27 13:23:08 +00001381 }
1382}
Chris Lattner00950542001-06-06 20:29:01 +00001383
Chris Lattnerc1824992001-10-29 16:05:51 +00001384void AssemblyWriter::printModule(const Module *M) {
Chris Lattner31ab1b32005-03-02 23:12:40 +00001385 if (!M->getModuleIdentifier().empty() &&
Misha Brukmanfd939082005-04-21 23:48:37 +00001386 // Don't print the ID if it will start a new line (which would
Chris Lattner31ab1b32005-03-02 23:12:40 +00001387 // require a comment char before it).
1388 M->getModuleIdentifier().find('\n') == std::string::npos)
1389 Out << "; ModuleID = '" << M->getModuleIdentifier() << "'\n";
1390
Owen Andersoncf7ff2b2006-10-18 02:21:12 +00001391 if (!M->getDataLayout().empty())
Chris Lattnerd2f9e602006-10-22 06:06:56 +00001392 Out << "target datalayout = \"" << M->getDataLayout() << "\"\n";
Reid Spencercddc86f2004-07-25 21:44:54 +00001393 if (!M->getTargetTriple().empty())
Reid Spencerc9a1f0d2004-07-25 21:29:43 +00001394 Out << "target triple = \"" << M->getTargetTriple() << "\"\n";
Misha Brukmanfd939082005-04-21 23:48:37 +00001395
Chris Lattnercc041ba2006-01-24 04:13:11 +00001396 if (!M->getModuleInlineAsm().empty()) {
Chris Lattner42a162e2006-01-24 00:45:30 +00001397 // Split the string into lines, to make it easier to read the .ll file.
Chris Lattnercc041ba2006-01-24 04:13:11 +00001398 std::string Asm = M->getModuleInlineAsm();
Chris Lattner42a162e2006-01-24 00:45:30 +00001399 size_t CurPos = 0;
1400 size_t NewLine = Asm.find_first_of('\n', CurPos);
Dan Gohman9bf0b9b2009-08-12 23:54:22 +00001401 Out << '\n';
Chris Lattner42a162e2006-01-24 00:45:30 +00001402 while (NewLine != std::string::npos) {
1403 // We found a newline, print the portion of the asm string from the
1404 // last newline up to this newline.
1405 Out << "module asm \"";
1406 PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.begin()+NewLine),
1407 Out);
1408 Out << "\"\n";
1409 CurPos = NewLine+1;
1410 NewLine = Asm.find_first_of('\n', CurPos);
1411 }
Chris Lattner71cdba32006-01-24 00:40:17 +00001412 Out << "module asm \"";
Chris Lattner42a162e2006-01-24 00:45:30 +00001413 PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.end()), Out);
Chris Lattner18365502006-01-23 23:03:36 +00001414 Out << "\"\n";
1415 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001416
Chris Lattner44da7d72004-09-14 05:06:58 +00001417 // Loop over the dependent libraries and emit them.
Chris Lattnercfe97b72004-09-14 04:51:44 +00001418 Module::lib_iterator LI = M->lib_begin();
1419 Module::lib_iterator LE = M->lib_end();
Reid Spencercddc86f2004-07-25 21:44:54 +00001420 if (LI != LE) {
Dan Gohman9bf0b9b2009-08-12 23:54:22 +00001421 Out << '\n';
Chris Lattnercfe97b72004-09-14 04:51:44 +00001422 Out << "deplibs = [ ";
1423 while (LI != LE) {
Chris Lattner44da7d72004-09-14 05:06:58 +00001424 Out << '"' << *LI << '"';
Reid Spencerc9a1f0d2004-07-25 21:29:43 +00001425 ++LI;
Chris Lattnercfe97b72004-09-14 04:51:44 +00001426 if (LI != LE)
1427 Out << ", ";
Reid Spencerc9a1f0d2004-07-25 21:29:43 +00001428 }
Dan Gohman9bf0b9b2009-08-12 23:54:22 +00001429 Out << " ]";
Reid Spencer83f6a772004-07-25 18:08:18 +00001430 }
Reid Spencere59eaf42004-09-13 23:44:23 +00001431
Chris Lattner413fd232009-03-01 00:03:38 +00001432 // Loop over the symbol table, emitting all id'd types.
Dan Gohman9bf0b9b2009-08-12 23:54:22 +00001433 if (!M->getTypeSymbolTable().empty() || !NumberedTypes.empty()) Out << '\n';
Reid Spencer78d033e2007-01-06 07:24:44 +00001434 printTypeSymbolTable(M->getTypeSymbolTable());
Misha Brukmanfd939082005-04-21 23:48:37 +00001435
Dan Gohman9bf0b9b2009-08-12 23:54:22 +00001436 // Output all globals.
1437 if (!M->global_empty()) Out << '\n';
Chris Lattnerd6d826c2006-12-06 04:41:52 +00001438 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
1439 I != E; ++I)
Chris Lattner7e708292002-06-25 16:13:24 +00001440 printGlobal(I);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001441
Chris Lattner69dacfc2007-04-26 02:24:10 +00001442 // Output all aliases.
1443 if (!M->alias_empty()) Out << "\n";
1444 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
1445 I != E; ++I)
1446 printAlias(I);
Chris Lattner007377f2001-09-07 16:36:04 +00001447
Chris Lattner44da7d72004-09-14 05:06:58 +00001448 // Output all of the functions.
Chris Lattner7e708292002-06-25 16:13:24 +00001449 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
1450 printFunction(I);
Devang Patel320671d2009-07-08 21:44:25 +00001451
Devang Patel37c4a2d2009-07-29 22:04:47 +00001452 // Output named metadata.
Dan Gohman9bf0b9b2009-08-12 23:54:22 +00001453 if (!M->named_metadata_empty()) Out << '\n';
Devang Patel37c4a2d2009-07-29 22:04:47 +00001454 for (Module::const_named_metadata_iterator I = M->named_metadata_begin(),
1455 E = M->named_metadata_end(); I != E; ++I) {
1456 const NamedMDNode *NMD = I;
1457 Out << "!" << NMD->getName() << " = !{";
1458 for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i) {
1459 if (i) Out << ", ";
Devang Patele8861b82009-07-30 01:02:04 +00001460 MDNode *MD = dyn_cast_or_null<MDNode>(NMD->getElement(i));
Devang Patel37c4a2d2009-07-29 22:04:47 +00001461 Out << '!' << Machine.getMetadataSlot(MD);
1462 }
1463 Out << "}\n";
1464 }
1465
1466 // Output metadata.
Dan Gohman9bf0b9b2009-08-12 23:54:22 +00001467 if (!Machine.mdnEmpty()) Out << '\n';
Devang Patel320671d2009-07-08 21:44:25 +00001468 WriteMDNodes(Out, TypePrinter, Machine);
Chris Lattner007377f2001-09-07 16:36:04 +00001469}
1470
Dan Gohman683e9222009-08-12 17:23:50 +00001471static void PrintLinkage(GlobalValue::LinkageTypes LT,
1472 formatted_raw_ostream &Out) {
Chris Lattnercfb5a202008-08-19 05:06:27 +00001473 switch (LT) {
Bill Wendling3d10a5a2009-07-20 01:03:30 +00001474 case GlobalValue::ExternalLinkage: break;
1475 case GlobalValue::PrivateLinkage: Out << "private "; break;
1476 case GlobalValue::LinkerPrivateLinkage: Out << "linker_private "; break;
1477 case GlobalValue::InternalLinkage: Out << "internal "; break;
1478 case GlobalValue::LinkOnceAnyLinkage: Out << "linkonce "; break;
1479 case GlobalValue::LinkOnceODRLinkage: Out << "linkonce_odr "; break;
1480 case GlobalValue::WeakAnyLinkage: Out << "weak "; break;
1481 case GlobalValue::WeakODRLinkage: Out << "weak_odr "; break;
1482 case GlobalValue::CommonLinkage: Out << "common "; break;
1483 case GlobalValue::AppendingLinkage: Out << "appending "; break;
1484 case GlobalValue::DLLImportLinkage: Out << "dllimport "; break;
1485 case GlobalValue::DLLExportLinkage: Out << "dllexport "; break;
1486 case GlobalValue::ExternalWeakLinkage: Out << "extern_weak "; break;
Chris Lattner266c7bb2009-04-13 05:44:34 +00001487 case GlobalValue::AvailableExternallyLinkage:
1488 Out << "available_externally ";
1489 break;
Chris Lattnercfb5a202008-08-19 05:06:27 +00001490 case GlobalValue::GhostLinkage:
Torok Edwinc23197a2009-07-14 16:55:14 +00001491 llvm_unreachable("GhostLinkage not allowed in AsmWriter!");
Chris Lattnercfb5a202008-08-19 05:06:27 +00001492 }
1493}
Duncan Sands667d4b82009-03-07 15:45:40 +00001494
Chris Lattnercfb5a202008-08-19 05:06:27 +00001495
1496static void PrintVisibility(GlobalValue::VisibilityTypes Vis,
Dan Gohman683e9222009-08-12 17:23:50 +00001497 formatted_raw_ostream &Out) {
Chris Lattnercfb5a202008-08-19 05:06:27 +00001498 switch (Vis) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001499 default: llvm_unreachable("Invalid visibility style!");
Chris Lattnercfb5a202008-08-19 05:06:27 +00001500 case GlobalValue::DefaultVisibility: break;
1501 case GlobalValue::HiddenVisibility: Out << "hidden "; break;
1502 case GlobalValue::ProtectedVisibility: Out << "protected "; break;
1503 }
1504}
1505
Chris Lattnerc1824992001-10-29 16:05:51 +00001506void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Dan Gohmand6c0f652009-08-13 15:27:57 +00001507 WriteAsOperandInternal(Out, GV, &TypePrinter, &Machine);
Dan Gohman3845e502009-08-12 23:32:33 +00001508 Out << " = ";
Chris Lattnerd70684f2001-09-18 04:01:05 +00001509
Chris Lattner52b26de2008-08-19 05:16:28 +00001510 if (!GV->hasInitializer() && GV->hasExternalLinkage())
1511 Out << "external ";
Daniel Dunbara279bc32009-09-20 02:20:51 +00001512
Chris Lattner52b26de2008-08-19 05:16:28 +00001513 PrintLinkage(GV->getLinkage(), Out);
1514 PrintVisibility(GV->getVisibility(), Out);
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00001515
1516 if (GV->isThreadLocal()) Out << "thread_local ";
Chris Lattnerdf986172009-01-02 07:01:27 +00001517 if (unsigned AddressSpace = GV->getType()->getAddressSpace())
1518 Out << "addrspace(" << AddressSpace << ") ";
Misha Brukman0313e0b2004-06-21 21:53:56 +00001519 Out << (GV->isConstant() ? "constant " : "global ");
Chris Lattner0f7364b2009-02-28 21:26:53 +00001520 TypePrinter.print(GV->getType()->getElementType(), Out);
Chris Lattnerd70684f2001-09-18 04:01:05 +00001521
Dan Gohman8dae1382008-09-14 17:21:12 +00001522 if (GV->hasInitializer()) {
1523 Out << ' ';
Devang Patel320671d2009-07-08 21:44:25 +00001524 writeOperand(GV->getInitializer(), false);
Dan Gohman8dae1382008-09-14 17:21:12 +00001525 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001526
Chris Lattner60962db2005-11-12 00:10:19 +00001527 if (GV->hasSection())
1528 Out << ", section \"" << GV->getSection() << '"';
1529 if (GV->getAlignment())
Chris Lattner30caa282005-11-06 06:48:53 +00001530 Out << ", align " << GV->getAlignment();
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001531
Chris Lattner7e708292002-06-25 16:13:24 +00001532 printInfoComment(*GV);
Chris Lattnercfb5a202008-08-19 05:06:27 +00001533 Out << '\n';
Chris Lattner70cc3392001-09-10 07:58:01 +00001534}
1535
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001536void AssemblyWriter::printAlias(const GlobalAlias *GA) {
Dale Johannesen24f07dc2008-06-03 18:14:29 +00001537 // Don't crash when dumping partially built GA
1538 if (!GA->hasName())
1539 Out << "<<nameless>> = ";
Chris Lattnerc97536e2008-08-17 04:40:13 +00001540 else {
1541 PrintLLVMName(Out, GA);
1542 Out << " = ";
1543 }
Chris Lattnercfb5a202008-08-19 05:06:27 +00001544 PrintVisibility(GA->getVisibility(), Out);
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001545
1546 Out << "alias ";
1547
Chris Lattnercfb5a202008-08-19 05:06:27 +00001548 PrintLinkage(GA->getLinkage(), Out);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001549
Anton Korobeynikovc6c98af2007-04-29 18:02:48 +00001550 const Constant *Aliasee = GA->getAliasee();
Daniel Dunbara279bc32009-09-20 02:20:51 +00001551
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001552 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Aliasee)) {
Chris Lattner0f7364b2009-02-28 21:26:53 +00001553 TypePrinter.print(GV->getType(), Out);
Chris Lattnerc97536e2008-08-17 04:40:13 +00001554 Out << ' ';
1555 PrintLLVMName(Out, GV);
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001556 } else if (const Function *F = dyn_cast<Function>(Aliasee)) {
Chris Lattner0f7364b2009-02-28 21:26:53 +00001557 TypePrinter.print(F->getFunctionType(), Out);
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001558 Out << "* ";
1559
Dan Gohmand6c0f652009-08-13 15:27:57 +00001560 WriteAsOperandInternal(Out, F, &TypePrinter, &Machine);
Anton Korobeynikov591858a2008-03-22 08:17:17 +00001561 } else if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(Aliasee)) {
Chris Lattner0f7364b2009-02-28 21:26:53 +00001562 TypePrinter.print(GA->getType(), Out);
1563 Out << ' ';
Chris Lattnerc97536e2008-08-17 04:40:13 +00001564 PrintLLVMName(Out, GA);
Anton Korobeynikova80e1182007-04-28 13:45:00 +00001565 } else {
Chris Lattnera2165ed2009-04-25 21:23:19 +00001566 const ConstantExpr *CE = cast<ConstantExpr>(Aliasee);
1567 // The only valid GEP is an all zero GEP.
1568 assert((CE->getOpcode() == Instruction::BitCast ||
1569 CE->getOpcode() == Instruction::GetElementPtr) &&
1570 "Unsupported aliasee");
1571 writeOperand(CE, false);
Anton Korobeynikova80e1182007-04-28 13:45:00 +00001572 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001573
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001574 printInfoComment(*GA);
Chris Lattner52b26de2008-08-19 05:16:28 +00001575 Out << '\n';
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001576}
1577
Reid Spencer78d033e2007-01-06 07:24:44 +00001578void AssemblyWriter::printTypeSymbolTable(const TypeSymbolTable &ST) {
Chris Lattner413fd232009-03-01 00:03:38 +00001579 // Emit all numbered types.
1580 for (unsigned i = 0, e = NumberedTypes.size(); i != e; ++i) {
Dan Gohman3845e502009-08-12 23:32:33 +00001581 Out << '%' << i << " = type ";
Daniel Dunbara279bc32009-09-20 02:20:51 +00001582
Chris Lattner413fd232009-03-01 00:03:38 +00001583 // Make sure we print out at least one level of the type structure, so
1584 // that we do not get %2 = type %2
1585 TypePrinter.printAtLeastOneLevel(NumberedTypes[i], Out);
Dan Gohman9bf0b9b2009-08-12 23:54:22 +00001586 Out << '\n';
Chris Lattner413fd232009-03-01 00:03:38 +00001587 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001588
Chris Lattner413fd232009-03-01 00:03:38 +00001589 // Print the named types.
Reid Spencer78d033e2007-01-06 07:24:44 +00001590 for (TypeSymbolTable::const_iterator TI = ST.begin(), TE = ST.end();
1591 TI != TE; ++TI) {
Daniel Dunbar03d76512009-07-25 23:55:21 +00001592 PrintLLVMName(Out, TI->first, LocalPrefix);
Chris Lattner52b26de2008-08-19 05:16:28 +00001593 Out << " = type ";
Reid Spencer9231ac82004-05-25 08:53:40 +00001594
1595 // Make sure we print out at least one level of the type structure, so
1596 // that we do not get %FILE = type %FILE
Chris Lattner0f7364b2009-02-28 21:26:53 +00001597 TypePrinter.printAtLeastOneLevel(TI->second, Out);
Chris Lattnercfb5a202008-08-19 05:06:27 +00001598 Out << '\n';
Reid Spencer9231ac82004-05-25 08:53:40 +00001599 }
Reid Spencer78d033e2007-01-06 07:24:44 +00001600}
1601
Misha Brukmanab5c6002004-03-02 00:22:19 +00001602/// printFunction - Print all aspects of a function.
1603///
Chris Lattner7e708292002-06-25 16:13:24 +00001604void AssemblyWriter::printFunction(const Function *F) {
Chris Lattnercfb5a202008-08-19 05:06:27 +00001605 // Print out the return type and name.
1606 Out << '\n';
Chris Lattner4ad02e72003-04-16 20:28:45 +00001607
Misha Brukman0313e0b2004-06-21 21:53:56 +00001608 if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, Out);
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001609
Reid Spencer5cbf9852007-01-30 20:08:39 +00001610 if (F->isDeclaration())
Chris Lattner3aa60662007-08-19 22:15:26 +00001611 Out << "declare ";
1612 else
Reid Spencerb951bc02006-12-29 20:29:48 +00001613 Out << "define ";
Daniel Dunbara279bc32009-09-20 02:20:51 +00001614
Chris Lattnercfb5a202008-08-19 05:06:27 +00001615 PrintLinkage(F->getLinkage(), Out);
1616 PrintVisibility(F->getVisibility(), Out);
Chris Lattner4ad02e72003-04-16 20:28:45 +00001617
Chris Lattnerd5118982005-05-06 20:26:43 +00001618 // Print the calling convention.
1619 switch (F->getCallingConv()) {
1620 case CallingConv::C: break; // default
Anton Korobeynikovf8248682006-09-20 22:03:51 +00001621 case CallingConv::Fast: Out << "fastcc "; break;
1622 case CallingConv::Cold: Out << "coldcc "; break;
1623 case CallingConv::X86_StdCall: Out << "x86_stdcallcc "; break;
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001624 case CallingConv::X86_FastCall: Out << "x86_fastcallcc "; break;
1625 case CallingConv::ARM_APCS: Out << "arm_apcscc "; break;
1626 case CallingConv::ARM_AAPCS: Out << "arm_aapcscc "; break;
1627 case CallingConv::ARM_AAPCS_VFP:Out << "arm_aapcs_vfpcc "; break;
Chris Lattnerd5118982005-05-06 20:26:43 +00001628 default: Out << "cc" << F->getCallingConv() << " "; break;
1629 }
1630
Reid Spencerbd5db8e2006-12-31 05:24:50 +00001631 const FunctionType *FT = F->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00001632 const AttrListPtr &Attrs = F->getAttributes();
Devang Patel652203f2008-09-29 20:49:50 +00001633 Attributes RetAttrs = Attrs.getRetAttributes();
1634 if (RetAttrs != Attribute::None)
1635 Out << Attribute::getAsString(Attrs.getRetAttributes()) << ' ';
Chris Lattner0f7364b2009-02-28 21:26:53 +00001636 TypePrinter.print(F->getReturnType(), Out);
Chris Lattner4667b712008-08-19 05:26:17 +00001637 Out << ' ';
Dan Gohmand6c0f652009-08-13 15:27:57 +00001638 WriteAsOperandInternal(Out, F, &TypePrinter, &Machine);
Misha Brukman0313e0b2004-06-21 21:53:56 +00001639 Out << '(';
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001640 Machine.incorporateFunction(F);
Chris Lattner007377f2001-09-07 16:36:04 +00001641
Chris Lattnerc1824992001-10-29 16:05:51 +00001642 // Loop over the arguments, printing them...
Chris Lattner007377f2001-09-07 16:36:04 +00001643
Reid Spencerbd5db8e2006-12-31 05:24:50 +00001644 unsigned Idx = 1;
Chris Lattner8dcd2f12007-04-18 00:57:22 +00001645 if (!F->isDeclaration()) {
1646 // If this isn't a declaration, print the argument names as well.
1647 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
1648 I != E; ++I) {
1649 // Insert commas as we go... the first arg doesn't get a comma
1650 if (I != F->arg_begin()) Out << ", ";
Devang Patel19c87462008-09-26 22:53:05 +00001651 printArgument(I, Attrs.getParamAttributes(Idx));
Chris Lattner8dcd2f12007-04-18 00:57:22 +00001652 Idx++;
1653 }
1654 } else {
1655 // Otherwise, print the types from the function type.
1656 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
1657 // Insert commas as we go... the first arg doesn't get a comma
1658 if (i) Out << ", ";
Daniel Dunbara279bc32009-09-20 02:20:51 +00001659
Chris Lattner8dcd2f12007-04-18 00:57:22 +00001660 // Output type...
Chris Lattner0f7364b2009-02-28 21:26:53 +00001661 TypePrinter.print(FT->getParamType(i), Out);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001662
Devang Patel19c87462008-09-26 22:53:05 +00001663 Attributes ArgAttrs = Attrs.getParamAttributes(i+1);
Devang Patel05988662008-09-25 21:00:45 +00001664 if (ArgAttrs != Attribute::None)
1665 Out << ' ' << Attribute::getAsString(ArgAttrs);
Chris Lattner8dcd2f12007-04-18 00:57:22 +00001666 }
Reid Spencerbd5db8e2006-12-31 05:24:50 +00001667 }
Chris Lattner007377f2001-09-07 16:36:04 +00001668
1669 // Finish printing arguments...
Chris Lattner7e708292002-06-25 16:13:24 +00001670 if (FT->isVarArg()) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001671 if (FT->getNumParams()) Out << ", ";
1672 Out << "..."; // Output varargs portion of signature!
Chris Lattner007377f2001-09-07 16:36:04 +00001673 }
Misha Brukman0313e0b2004-06-21 21:53:56 +00001674 Out << ')';
Devang Patel19c87462008-09-26 22:53:05 +00001675 Attributes FnAttrs = Attrs.getFnAttributes();
1676 if (FnAttrs != Attribute::None)
1677 Out << ' ' << Attribute::getAsString(Attrs.getFnAttributes());
Chris Lattner60962db2005-11-12 00:10:19 +00001678 if (F->hasSection())
1679 Out << " section \"" << F->getSection() << '"';
Chris Lattner30caa282005-11-06 06:48:53 +00001680 if (F->getAlignment())
1681 Out << " align " << F->getAlignment();
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001682 if (F->hasGC())
1683 Out << " gc \"" << F->getGC() << '"';
Reid Spencer5cbf9852007-01-30 20:08:39 +00001684 if (F->isDeclaration()) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001685 Out << "\n";
Chris Lattner03e2acb2002-05-06 03:00:40 +00001686 } else {
Chris Lattnera1b58582008-04-21 06:12:55 +00001687 Out << " {";
Misha Brukmanfd939082005-04-21 23:48:37 +00001688
Chris Lattnerb5794002002-04-07 22:49:37 +00001689 // Output all of its basic blocks... for the function
Chris Lattner7e708292002-06-25 16:13:24 +00001690 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
1691 printBasicBlock(I);
Chris Lattner007377f2001-09-07 16:36:04 +00001692
Misha Brukman0313e0b2004-06-21 21:53:56 +00001693 Out << "}\n";
Chris Lattner007377f2001-09-07 16:36:04 +00001694 }
1695
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001696 Machine.purgeFunction();
Chris Lattner00950542001-06-06 20:29:01 +00001697}
1698
Misha Brukmanab5c6002004-03-02 00:22:19 +00001699/// printArgument - This member is called for every argument that is passed into
1700/// the function. Simply print it out
1701///
Daniel Dunbara279bc32009-09-20 02:20:51 +00001702void AssemblyWriter::printArgument(const Argument *Arg,
Devang Pateleaf42ab2008-09-23 23:03:40 +00001703 Attributes Attrs) {
Chris Lattner00950542001-06-06 20:29:01 +00001704 // Output type...
Chris Lattner0f7364b2009-02-28 21:26:53 +00001705 TypePrinter.print(Arg->getType(), Out);
Misha Brukmanfd939082005-04-21 23:48:37 +00001706
Duncan Sandsdc024672007-11-27 13:23:08 +00001707 // Output parameter attributes list
Devang Patel05988662008-09-25 21:00:45 +00001708 if (Attrs != Attribute::None)
1709 Out << ' ' << Attribute::getAsString(Attrs);
Reid Spencerbd5db8e2006-12-31 05:24:50 +00001710
Chris Lattner00950542001-06-06 20:29:01 +00001711 // Output name, if available...
Chris Lattnerc97536e2008-08-17 04:40:13 +00001712 if (Arg->hasName()) {
1713 Out << ' ';
1714 PrintLLVMName(Out, Arg);
1715 }
Chris Lattner00950542001-06-06 20:29:01 +00001716}
1717
Misha Brukmanab5c6002004-03-02 00:22:19 +00001718/// printBasicBlock - This member is called for each basic block in a method.
1719///
Chris Lattnerc1824992001-10-29 16:05:51 +00001720void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Nick Lewycky280a6e62008-04-25 16:53:59 +00001721 if (BB->hasName()) { // Print out the label if it exists...
Chris Lattnerc97536e2008-08-17 04:40:13 +00001722 Out << "\n";
Daniel Dunbar03d76512009-07-25 23:55:21 +00001723 PrintLLVMName(Out, BB->getName(), LabelPrefix);
Chris Lattnerc97536e2008-08-17 04:40:13 +00001724 Out << ':';
Nick Lewycky280a6e62008-04-25 16:53:59 +00001725 } else if (!BB->use_empty()) { // Don't print block # of no uses...
Chris Lattner96c5b2f2008-04-21 04:20:33 +00001726 Out << "\n; <label>:";
Chris Lattner22379bc2007-01-11 03:54:27 +00001727 int Slot = Machine.getLocalSlot(BB);
Chris Lattner69566452004-06-09 19:41:19 +00001728 if (Slot != -1)
Misha Brukman0313e0b2004-06-21 21:53:56 +00001729 Out << Slot;
Chris Lattner69566452004-06-09 19:41:19 +00001730 else
Misha Brukman0313e0b2004-06-21 21:53:56 +00001731 Out << "<badref>";
Chris Lattner061269b2002-10-02 19:38:55 +00001732 }
Chris Lattner4e4d8622003-11-20 00:09:43 +00001733
Dan Gohman683e9222009-08-12 17:23:50 +00001734 if (BB->getParent() == 0) {
Chris Lattner8f4b1ec2009-08-17 15:48:08 +00001735 Out.PadToColumn(50);
Dan Gohman683e9222009-08-12 17:23:50 +00001736 Out << "; Error: Block without parent!";
1737 } else if (BB != &BB->getParent()->getEntryBlock()) { // Not the entry block?
Chris Lattnereb411292008-04-22 02:45:44 +00001738 // Output predecessors for the block...
Chris Lattner8f4b1ec2009-08-17 15:48:08 +00001739 Out.PadToColumn(50);
Dan Gohman683e9222009-08-12 17:23:50 +00001740 Out << ";";
Chris Lattnereb411292008-04-22 02:45:44 +00001741 pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001742
Chris Lattnereb411292008-04-22 02:45:44 +00001743 if (PI == PE) {
1744 Out << " No predecessors!";
1745 } else {
Dan Gohman8dae1382008-09-14 17:21:12 +00001746 Out << " preds = ";
Chris Lattnereb411292008-04-22 02:45:44 +00001747 writeOperand(*PI, false);
1748 for (++PI; PI != PE; ++PI) {
Dan Gohman8dae1382008-09-14 17:21:12 +00001749 Out << ", ";
Chris Lattner2fcfdb72006-12-06 06:24:27 +00001750 writeOperand(*PI, false);
Chris Lattner40efcec2003-11-16 22:59:57 +00001751 }
Chris Lattner061269b2002-10-02 19:38:55 +00001752 }
Chris Lattner00950542001-06-06 20:29:01 +00001753 }
Misha Brukmanfd939082005-04-21 23:48:37 +00001754
Chris Lattnereb411292008-04-22 02:45:44 +00001755 Out << "\n";
Chris Lattner00950542001-06-06 20:29:01 +00001756
Misha Brukman0313e0b2004-06-21 21:53:56 +00001757 if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, Out);
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001758
Chris Lattner007377f2001-09-07 16:36:04 +00001759 // Output all of the instructions in the basic block...
Dan Gohmanbeca6892009-07-13 18:27:59 +00001760 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
Chris Lattner7e708292002-06-25 16:13:24 +00001761 printInstruction(*I);
Dan Gohmanbeca6892009-07-13 18:27:59 +00001762 Out << '\n';
1763 }
Chris Lattner9f717ef2004-03-08 18:51:45 +00001764
Misha Brukman0313e0b2004-06-21 21:53:56 +00001765 if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, Out);
Chris Lattner00950542001-06-06 20:29:01 +00001766}
1767
Chris Lattnere02fa852001-10-13 06:42:36 +00001768
Misha Brukmanab5c6002004-03-02 00:22:19 +00001769/// printInfoComment - Print a little comment after the instruction indicating
1770/// which slot it occupies.
1771///
Chris Lattner7e708292002-06-25 16:13:24 +00001772void AssemblyWriter::printInfoComment(const Value &V) {
Owen Anderson1d0be152009-08-13 21:58:54 +00001773 if (V.getType() != Type::getVoidTy(V.getContext())) {
Chris Lattner8f4b1ec2009-08-17 15:48:08 +00001774 Out.PadToColumn(50);
Dan Gohman683e9222009-08-12 17:23:50 +00001775 Out << "; <";
Chris Lattner0f7364b2009-02-28 21:26:53 +00001776 TypePrinter.print(V.getType(), Out);
Dan Gohman9bf0b9b2009-08-12 23:54:22 +00001777 Out << "> [#uses=" << V.getNumUses() << ']'; // Output # uses
Chris Lattnere02fa852001-10-13 06:42:36 +00001778 }
1779}
1780
Reid Spencer3a9ec242006-08-28 01:02:49 +00001781// This member is called for each Instruction in a function..
Chris Lattner7e708292002-06-25 16:13:24 +00001782void AssemblyWriter::printInstruction(const Instruction &I) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001783 if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, Out);
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001784
Dan Gohman3845e502009-08-12 23:32:33 +00001785 // Print out indentation for an instruction.
Dan Gohman01889ca2009-08-13 01:41:52 +00001786 Out << " ";
Chris Lattner00950542001-06-06 20:29:01 +00001787
1788 // Print out name if it exists...
Chris Lattnerc97536e2008-08-17 04:40:13 +00001789 if (I.hasName()) {
1790 PrintLLVMName(Out, &I);
1791 Out << " = ";
Owen Anderson1d0be152009-08-13 21:58:54 +00001792 } else if (I.getType() != Type::getVoidTy(I.getContext())) {
Chris Lattner828db8a2008-08-29 17:19:30 +00001793 // Print out the def slot taken.
1794 int SlotNum = Machine.getLocalSlot(&I);
1795 if (SlotNum == -1)
1796 Out << "<badref> = ";
1797 else
1798 Out << '%' << SlotNum << " = ";
Chris Lattnerc97536e2008-08-17 04:40:13 +00001799 }
Chris Lattner00950542001-06-06 20:29:01 +00001800
Chris Lattnerddb6db42005-05-06 05:51:46 +00001801 // If this is a volatile load or store, print out the volatile marker.
Chris Lattnere5e475e2003-09-08 17:45:59 +00001802 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) ||
Chris Lattnerddb6db42005-05-06 05:51:46 +00001803 (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile())) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001804 Out << "volatile ";
Chris Lattnerddb6db42005-05-06 05:51:46 +00001805 } else if (isa<CallInst>(I) && cast<CallInst>(I).isTailCall()) {
1806 // If this is a call, check if it's a tail call.
1807 Out << "tail ";
1808 }
Chris Lattnere5e475e2003-09-08 17:45:59 +00001809
Chris Lattner00950542001-06-06 20:29:01 +00001810 // Print out the opcode...
Misha Brukman0313e0b2004-06-21 21:53:56 +00001811 Out << I.getOpcodeName();
Chris Lattner00950542001-06-06 20:29:01 +00001812
Dan Gohman59858cf2009-07-27 16:11:46 +00001813 // Print out optimization information.
1814 WriteOptimizationInfo(Out, &I);
1815
Reid Spencer74f16422006-12-03 06:27:29 +00001816 // Print out the compare instruction predicates
Nate Begemanac80ade2008-05-12 19:01:56 +00001817 if (const CmpInst *CI = dyn_cast<CmpInst>(&I))
Chris Lattnerab49ee72008-08-23 22:52:27 +00001818 Out << ' ' << getPredicateText(CI->getPredicate());
Reid Spencer74f16422006-12-03 06:27:29 +00001819
Chris Lattner00950542001-06-06 20:29:01 +00001820 // Print out the type of the operands...
Chris Lattner7e708292002-06-25 16:13:24 +00001821 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0;
Chris Lattner00950542001-06-06 20:29:01 +00001822
1823 // Special case conditional branches to swizzle the condition out to the front
Gabor Greifccd27fb2009-02-09 15:45:06 +00001824 if (isa<BranchInst>(I) && cast<BranchInst>(I).isConditional()) {
1825 BranchInst &BI(cast<BranchInst>(I));
Dan Gohman8dae1382008-09-14 17:21:12 +00001826 Out << ' ';
Gabor Greifccd27fb2009-02-09 15:45:06 +00001827 writeOperand(BI.getCondition(), true);
Dan Gohman8dae1382008-09-14 17:21:12 +00001828 Out << ", ";
Gabor Greifccd27fb2009-02-09 15:45:06 +00001829 writeOperand(BI.getSuccessor(0), true);
Dan Gohman8dae1382008-09-14 17:21:12 +00001830 Out << ", ";
Gabor Greifccd27fb2009-02-09 15:45:06 +00001831 writeOperand(BI.getSuccessor(1), true);
Chris Lattner00950542001-06-06 20:29:01 +00001832
Chris Lattner94dc1f22002-04-13 18:34:38 +00001833 } else if (isa<SwitchInst>(I)) {
Chris Lattner00950542001-06-06 20:29:01 +00001834 // Special case switch statement to get formatting nice and correct...
Dan Gohman8dae1382008-09-14 17:21:12 +00001835 Out << ' ';
Chris Lattnerab49ee72008-08-23 22:52:27 +00001836 writeOperand(Operand , true);
Dan Gohman8dae1382008-09-14 17:21:12 +00001837 Out << ", ";
Chris Lattnerab49ee72008-08-23 22:52:27 +00001838 writeOperand(I.getOperand(1), true);
1839 Out << " [";
Chris Lattner00950542001-06-06 20:29:01 +00001840
Chris Lattner7e708292002-06-25 16:13:24 +00001841 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; op += 2) {
Dan Gohman01889ca2009-08-13 01:41:52 +00001842 Out << "\n ";
Chris Lattnerab49ee72008-08-23 22:52:27 +00001843 writeOperand(I.getOperand(op ), true);
Dan Gohman8dae1382008-09-14 17:21:12 +00001844 Out << ", ";
Chris Lattner7e708292002-06-25 16:13:24 +00001845 writeOperand(I.getOperand(op+1), true);
Chris Lattner00950542001-06-06 20:29:01 +00001846 }
Dan Gohman01889ca2009-08-13 01:41:52 +00001847 Out << "\n ]";
Chris Lattnerb00c5822001-10-02 03:41:24 +00001848 } else if (isa<PHINode>(I)) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001849 Out << ' ';
Chris Lattner0f7364b2009-02-28 21:26:53 +00001850 TypePrinter.print(I.getType(), Out);
Misha Brukman0313e0b2004-06-21 21:53:56 +00001851 Out << ' ';
Chris Lattner00950542001-06-06 20:29:01 +00001852
Chris Lattner7e708292002-06-25 16:13:24 +00001853 for (unsigned op = 0, Eop = I.getNumOperands(); op < Eop; op += 2) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001854 if (op) Out << ", ";
Dan Gohman8dae1382008-09-14 17:21:12 +00001855 Out << "[ ";
1856 writeOperand(I.getOperand(op ), false); Out << ", ";
Misha Brukman0313e0b2004-06-21 21:53:56 +00001857 writeOperand(I.getOperand(op+1), false); Out << " ]";
Chris Lattnerc24d2082001-06-11 15:04:20 +00001858 }
Dan Gohman995be7d2008-05-31 19:12:39 +00001859 } else if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(&I)) {
Dan Gohman8dae1382008-09-14 17:21:12 +00001860 Out << ' ';
Dan Gohman995be7d2008-05-31 19:12:39 +00001861 writeOperand(I.getOperand(0), true);
1862 for (const unsigned *i = EVI->idx_begin(), *e = EVI->idx_end(); i != e; ++i)
1863 Out << ", " << *i;
1864 } else if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(&I)) {
Dan Gohman8dae1382008-09-14 17:21:12 +00001865 Out << ' ';
1866 writeOperand(I.getOperand(0), true); Out << ", ";
Dan Gohman995be7d2008-05-31 19:12:39 +00001867 writeOperand(I.getOperand(1), true);
1868 for (const unsigned *i = IVI->idx_begin(), *e = IVI->idx_end(); i != e; ++i)
1869 Out << ", " << *i;
Devang Patel57ef4f42008-02-23 00:35:18 +00001870 } else if (isa<ReturnInst>(I) && !Operand) {
1871 Out << " void";
Chris Lattnerd5118982005-05-06 20:26:43 +00001872 } else if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
1873 // Print the calling convention being used.
1874 switch (CI->getCallingConv()) {
1875 case CallingConv::C: break; // default
Chris Lattner0deaab82006-05-19 21:58:52 +00001876 case CallingConv::Fast: Out << " fastcc"; break;
1877 case CallingConv::Cold: Out << " coldcc"; break;
Chris Lattnerb28a6dc2007-11-18 18:32:16 +00001878 case CallingConv::X86_StdCall: Out << " x86_stdcallcc"; break;
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001879 case CallingConv::X86_FastCall: Out << " x86_fastcallcc"; break;
1880 case CallingConv::ARM_APCS: Out << " arm_apcscc "; break;
1881 case CallingConv::ARM_AAPCS: Out << " arm_aapcscc "; break;
1882 case CallingConv::ARM_AAPCS_VFP:Out << " arm_aapcs_vfpcc "; break;
Chris Lattnerd5118982005-05-06 20:26:43 +00001883 default: Out << " cc" << CI->getCallingConv(); break;
1884 }
1885
Reid Spencerb138a062007-04-09 06:10:42 +00001886 const PointerType *PTy = cast<PointerType>(Operand->getType());
1887 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1888 const Type *RetTy = FTy->getReturnType();
Devang Patel05988662008-09-25 21:00:45 +00001889 const AttrListPtr &PAL = CI->getAttributes();
Chris Lattner268de042001-11-06 21:28:12 +00001890
Devang Patel652203f2008-09-29 20:49:50 +00001891 if (PAL.getRetAttributes() != Attribute::None)
1892 Out << ' ' << Attribute::getAsString(PAL.getRetAttributes());
1893
Chris Lattner7a012292003-08-05 15:34:45 +00001894 // If possible, print out the short form of the call instruction. We can
Chris Lattnerb5794002002-04-07 22:49:37 +00001895 // only do this if the first argument is a pointer to a nonvararg function,
Chris Lattner7a012292003-08-05 15:34:45 +00001896 // and if the return type is not a pointer to a function.
Chris Lattner268de042001-11-06 21:28:12 +00001897 //
Dan Gohman8dae1382008-09-14 17:21:12 +00001898 Out << ' ';
Chris Lattner7a012292003-08-05 15:34:45 +00001899 if (!FTy->isVarArg() &&
Misha Brukmanfd939082005-04-21 23:48:37 +00001900 (!isa<PointerType>(RetTy) ||
Chris Lattnerc1b27182002-07-25 20:58:51 +00001901 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Chris Lattner0f7364b2009-02-28 21:26:53 +00001902 TypePrinter.print(RetTy, Out);
Dan Gohman8dae1382008-09-14 17:21:12 +00001903 Out << ' ';
Chris Lattner268de042001-11-06 21:28:12 +00001904 writeOperand(Operand, false);
1905 } else {
1906 writeOperand(Operand, true);
1907 }
Misha Brukman0313e0b2004-06-21 21:53:56 +00001908 Out << '(';
Reid Spencerbd5db8e2006-12-31 05:24:50 +00001909 for (unsigned op = 1, Eop = I.getNumOperands(); op < Eop; ++op) {
1910 if (op > 1)
Dan Gohman8dae1382008-09-14 17:21:12 +00001911 Out << ", ";
Devang Patel19c87462008-09-26 22:53:05 +00001912 writeParamOperand(I.getOperand(op), PAL.getParamAttributes(op));
Chris Lattner00950542001-06-06 20:29:01 +00001913 }
Dan Gohman8dae1382008-09-14 17:21:12 +00001914 Out << ')';
Devang Patel19c87462008-09-26 22:53:05 +00001915 if (PAL.getFnAttributes() != Attribute::None)
1916 Out << ' ' << Attribute::getAsString(PAL.getFnAttributes());
Chris Lattner7e708292002-06-25 16:13:24 +00001917 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
Reid Spencerb138a062007-04-09 06:10:42 +00001918 const PointerType *PTy = cast<PointerType>(Operand->getType());
1919 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1920 const Type *RetTy = FTy->getReturnType();
Devang Patel05988662008-09-25 21:00:45 +00001921 const AttrListPtr &PAL = II->getAttributes();
Chris Lattner7a012292003-08-05 15:34:45 +00001922
Chris Lattnerd5118982005-05-06 20:26:43 +00001923 // Print the calling convention being used.
1924 switch (II->getCallingConv()) {
1925 case CallingConv::C: break; // default
Chris Lattner0deaab82006-05-19 21:58:52 +00001926 case CallingConv::Fast: Out << " fastcc"; break;
1927 case CallingConv::Cold: Out << " coldcc"; break;
Dan Gohman8dae1382008-09-14 17:21:12 +00001928 case CallingConv::X86_StdCall: Out << " x86_stdcallcc"; break;
1929 case CallingConv::X86_FastCall: Out << " x86_fastcallcc"; break;
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001930 case CallingConv::ARM_APCS: Out << " arm_apcscc "; break;
1931 case CallingConv::ARM_AAPCS: Out << " arm_aapcscc "; break;
1932 case CallingConv::ARM_AAPCS_VFP:Out << " arm_aapcs_vfpcc "; break;
Chris Lattnerd5118982005-05-06 20:26:43 +00001933 default: Out << " cc" << II->getCallingConv(); break;
1934 }
1935
Devang Patel652203f2008-09-29 20:49:50 +00001936 if (PAL.getRetAttributes() != Attribute::None)
1937 Out << ' ' << Attribute::getAsString(PAL.getRetAttributes());
1938
Chris Lattner7a012292003-08-05 15:34:45 +00001939 // If possible, print out the short form of the invoke instruction. We can
1940 // only do this if the first argument is a pointer to a nonvararg function,
1941 // and if the return type is not a pointer to a function.
1942 //
Dan Gohman2b6c3d92008-10-15 18:02:08 +00001943 Out << ' ';
Chris Lattner7a012292003-08-05 15:34:45 +00001944 if (!FTy->isVarArg() &&
Misha Brukmanfd939082005-04-21 23:48:37 +00001945 (!isa<PointerType>(RetTy) ||
Chris Lattner7a012292003-08-05 15:34:45 +00001946 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Chris Lattner0f7364b2009-02-28 21:26:53 +00001947 TypePrinter.print(RetTy, Out);
Dan Gohman2b6c3d92008-10-15 18:02:08 +00001948 Out << ' ';
Chris Lattner7a012292003-08-05 15:34:45 +00001949 writeOperand(Operand, false);
1950 } else {
1951 writeOperand(Operand, true);
1952 }
Misha Brukman0313e0b2004-06-21 21:53:56 +00001953 Out << '(';
Gabor Greif03a5f132009-09-03 02:02:59 +00001954 for (unsigned op = 3, Eop = I.getNumOperands(); op < Eop; ++op) {
1955 if (op > 3)
Dan Gohman8dae1382008-09-14 17:21:12 +00001956 Out << ", ";
Gabor Greif03a5f132009-09-03 02:02:59 +00001957 writeParamOperand(I.getOperand(op), PAL.getParamAttributes(op-2));
Chris Lattnere02fa852001-10-13 06:42:36 +00001958 }
1959
Dan Gohman8dae1382008-09-14 17:21:12 +00001960 Out << ')';
Devang Patel19c87462008-09-26 22:53:05 +00001961 if (PAL.getFnAttributes() != Attribute::None)
1962 Out << ' ' << Attribute::getAsString(PAL.getFnAttributes());
1963
Dan Gohman01889ca2009-08-13 01:41:52 +00001964 Out << "\n to ";
Chris Lattnere02fa852001-10-13 06:42:36 +00001965 writeOperand(II->getNormalDest(), true);
Dan Gohman8dae1382008-09-14 17:21:12 +00001966 Out << " unwind ";
Chris Lattneraeb2a1d2004-02-08 21:44:31 +00001967 writeOperand(II->getUnwindDest(), true);
Chris Lattnere02fa852001-10-13 06:42:36 +00001968
Victor Hernandez7b929da2009-10-23 21:09:37 +00001969 } else if (const AllocaInst *AI = dyn_cast<AllocaInst>(&I)) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001970 Out << ' ';
Chris Lattner0f7364b2009-02-28 21:26:53 +00001971 TypePrinter.print(AI->getType()->getElementType(), Out);
Dan Gohman69bff072009-07-31 18:23:24 +00001972 if (!AI->getArraySize() || AI->isArrayAllocation()) {
Dan Gohman8dae1382008-09-14 17:21:12 +00001973 Out << ", ";
Chris Lattner94dc1f22002-04-13 18:34:38 +00001974 writeOperand(AI->getArraySize(), true);
Chris Lattner00950542001-06-06 20:29:01 +00001975 }
Nate Begeman14b05292005-11-05 09:21:28 +00001976 if (AI->getAlignment()) {
Chris Lattner9fad0b92005-11-05 21:20:34 +00001977 Out << ", align " << AI->getAlignment();
Nate Begeman14b05292005-11-05 09:21:28 +00001978 }
Chris Lattnere02fa852001-10-13 06:42:36 +00001979 } else if (isa<CastInst>(I)) {
Dan Gohman8dae1382008-09-14 17:21:12 +00001980 if (Operand) {
1981 Out << ' ';
1982 writeOperand(Operand, true); // Work with broken code
1983 }
Misha Brukman0313e0b2004-06-21 21:53:56 +00001984 Out << " to ";
Chris Lattner0f7364b2009-02-28 21:26:53 +00001985 TypePrinter.print(I.getType(), Out);
Chris Lattner4d45bd02003-10-18 05:57:43 +00001986 } else if (isa<VAArgInst>(I)) {
Dan Gohman8dae1382008-09-14 17:21:12 +00001987 if (Operand) {
1988 Out << ' ';
1989 writeOperand(Operand, true); // Work with broken code
1990 }
Misha Brukman0313e0b2004-06-21 21:53:56 +00001991 Out << ", ";
Chris Lattner0f7364b2009-02-28 21:26:53 +00001992 TypePrinter.print(I.getType(), Out);
1993 } else if (Operand) { // Print the normal way.
Chris Lattner00950542001-06-06 20:29:01 +00001994
Misha Brukmanfd939082005-04-21 23:48:37 +00001995 // PrintAllTypes - Instructions who have operands of all the same type
Chris Lattner00950542001-06-06 20:29:01 +00001996 // omit the type from all but the first operand. If the instruction has
1997 // different type operands (for example br), then they are all printed.
1998 bool PrintAllTypes = false;
1999 const Type *TheType = Operand->getType();
Chris Lattner00950542001-06-06 20:29:01 +00002000
Reid Spencerebe57e32007-02-02 13:54:55 +00002001 // Select, Store and ShuffleVector always print all types.
Devang Patel64947682008-03-04 22:05:14 +00002002 if (isa<SelectInst>(I) || isa<StoreInst>(I) || isa<ShuffleVectorInst>(I)
2003 || isa<ReturnInst>(I)) {
Chris Lattnerffd9bf42003-04-16 20:20:02 +00002004 PrintAllTypes = true;
2005 } else {
2006 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
2007 Operand = I.getOperand(i);
Nuno Lopes6ad2b2a2009-01-15 18:40:57 +00002008 // note that Operand shouldn't be null, but the test helps make dump()
2009 // more tolerant of malformed IR
Nuno Lopesa8c78a92009-01-14 17:51:41 +00002010 if (Operand && Operand->getType() != TheType) {
Chris Lattnerffd9bf42003-04-16 20:20:02 +00002011 PrintAllTypes = true; // We have differing types! Print them all!
2012 break;
2013 }
Chris Lattner00950542001-06-06 20:29:01 +00002014 }
2015 }
Misha Brukmanfd939082005-04-21 23:48:37 +00002016
Chris Lattnerc1824992001-10-29 16:05:51 +00002017 if (!PrintAllTypes) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00002018 Out << ' ';
Chris Lattner0f7364b2009-02-28 21:26:53 +00002019 TypePrinter.print(TheType, Out);
Chris Lattnerc1824992001-10-29 16:05:51 +00002020 }
Chris Lattner00950542001-06-06 20:29:01 +00002021
Dan Gohman8dae1382008-09-14 17:21:12 +00002022 Out << ' ';
Chris Lattner7e708292002-06-25 16:13:24 +00002023 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
Dan Gohman8dae1382008-09-14 17:21:12 +00002024 if (i) Out << ", ";
Chris Lattner7e708292002-06-25 16:13:24 +00002025 writeOperand(I.getOperand(i), PrintAllTypes);
Chris Lattner00950542001-06-06 20:29:01 +00002026 }
2027 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002028
Christopher Lamb43c7f372007-04-22 19:24:39 +00002029 // Print post operand alignment for load/store
2030 if (isa<LoadInst>(I) && cast<LoadInst>(I).getAlignment()) {
2031 Out << ", align " << cast<LoadInst>(I).getAlignment();
2032 } else if (isa<StoreInst>(I) && cast<StoreInst>(I).getAlignment()) {
2033 Out << ", align " << cast<StoreInst>(I).getAlignment();
2034 }
Chris Lattner00950542001-06-06 20:29:01 +00002035
Devang Patel18f0c262009-09-28 20:56:00 +00002036 // Print Metadata info
Devang Patel7f93f4d2009-10-07 16:37:55 +00002037 if (!MDNames.empty()) {
2038 MetadataContext &TheMetadata = I.getContext().getMetadata();
Devang Patelf61b2372009-10-22 18:55:16 +00002039 typedef SmallVector<std::pair<unsigned, TrackingVH<MDNode> >, 2> MDMapTy;
2040 MDMapTy MDs;
2041 TheMetadata.getMDs(&I, MDs);
2042 for (MDMapTy::const_iterator MI = MDs.begin(), ME = MDs.end(); MI != ME;
2043 ++MI)
2044 Out << ", !" << MDNames[MI->first]
2045 << " !" << Machine.getMetadataSlot(MI->second);
Devang Patel7f93f4d2009-10-07 16:37:55 +00002046 }
Chris Lattnere02fa852001-10-13 06:42:36 +00002047 printInfoComment(I);
Chris Lattner00950542001-06-06 20:29:01 +00002048}
2049
2050
2051//===----------------------------------------------------------------------===//
2052// External Interface declarations
2053//===----------------------------------------------------------------------===//
2054
Dan Gohman683e9222009-08-12 17:23:50 +00002055void Module::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW) const {
Chris Lattner0d9574a2008-08-19 04:26:57 +00002056 SlotTracker SlotTable(this);
Dan Gohman683e9222009-08-12 17:23:50 +00002057 formatted_raw_ostream OS(ROS);
Chris Lattner944fac72008-08-23 22:23:09 +00002058 AssemblyWriter W(OS, SlotTable, this, AAW);
Chris Lattner75cf7cf2002-04-08 22:03:40 +00002059 W.write(this);
Chris Lattner00950542001-06-06 20:29:01 +00002060}
2061
Chris Lattner6d4306e2009-02-28 21:11:05 +00002062void Type::print(raw_ostream &OS) const {
2063 if (this == 0) {
2064 OS << "<null Type>";
2065 return;
2066 }
Chris Lattnere9fa33e2009-02-28 23:20:19 +00002067 TypePrinting().print(this, OS);
Chris Lattner75cf7cf2002-04-08 22:03:40 +00002068}
2069
Dan Gohman683e9222009-08-12 17:23:50 +00002070void Value::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW) const {
Chris Lattner944fac72008-08-23 22:23:09 +00002071 if (this == 0) {
Dan Gohman1220e102009-08-12 20:56:03 +00002072 ROS << "printing a <null> value\n";
Chris Lattner944fac72008-08-23 22:23:09 +00002073 return;
2074 }
Dan Gohman1220e102009-08-12 20:56:03 +00002075 formatted_raw_ostream OS(ROS);
Chris Lattner944fac72008-08-23 22:23:09 +00002076 if (const Instruction *I = dyn_cast<Instruction>(this)) {
2077 const Function *F = I->getParent() ? I->getParent()->getParent() : 0;
2078 SlotTracker SlotTable(F);
2079 AssemblyWriter W(OS, SlotTable, F ? F->getParent() : 0, AAW);
2080 W.write(I);
2081 } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(this)) {
2082 SlotTracker SlotTable(BB->getParent());
2083 AssemblyWriter W(OS, SlotTable,
2084 BB->getParent() ? BB->getParent()->getParent() : 0, AAW);
2085 W.write(BB);
2086 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(this)) {
2087 SlotTracker SlotTable(GV->getParent());
Dan Gohmanba0941f2009-04-20 16:10:33 +00002088 AssemblyWriter W(OS, SlotTable, GV->getParent(), AAW);
Chris Lattner944fac72008-08-23 22:23:09 +00002089 W.write(GV);
Devang Patele54abc92009-07-22 17:43:22 +00002090 } else if (const MDString *MDS = dyn_cast<MDString>(this)) {
2091 TypePrinting TypePrinter;
2092 TypePrinter.print(MDS->getType(), OS);
2093 OS << ' ';
2094 OS << "!\"";
Daniel Dunbar03d76512009-07-25 23:55:21 +00002095 PrintEscapedString(MDS->getString(), OS);
Devang Patele54abc92009-07-22 17:43:22 +00002096 OS << '"';
Devang Patelfcd65ae2009-07-01 20:59:15 +00002097 } else if (const MDNode *N = dyn_cast<MDNode>(this)) {
Devang Patel320671d2009-07-08 21:44:25 +00002098 SlotTracker SlotTable(N);
Devang Patelfcd65ae2009-07-01 20:59:15 +00002099 TypePrinting TypePrinter;
Devang Patel320671d2009-07-08 21:44:25 +00002100 SlotTable.initialize();
2101 WriteMDNodes(OS, TypePrinter, SlotTable);
Devang Patelc29d5b32009-07-30 00:02:57 +00002102 } else if (const NamedMDNode *N = dyn_cast<NamedMDNode>(this)) {
2103 SlotTracker SlotTable(N);
2104 TypePrinting TypePrinter;
2105 SlotTable.initialize();
2106 OS << "!" << N->getName() << " = !{";
2107 for (unsigned i = 0, e = N->getNumElements(); i != e; ++i) {
2108 if (i) OS << ", ";
Devang Patele8861b82009-07-30 01:02:04 +00002109 MDNode *MD = dyn_cast_or_null<MDNode>(N->getElement(i));
2110 if (MD)
2111 OS << '!' << SlotTable.getMetadataSlot(MD);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002112 else
Devang Patele8861b82009-07-30 01:02:04 +00002113 OS << "null";
Devang Patelc29d5b32009-07-30 00:02:57 +00002114 }
2115 OS << "}\n";
2116 WriteMDNodes(OS, TypePrinter, SlotTable);
Chris Lattner944fac72008-08-23 22:23:09 +00002117 } else if (const Constant *C = dyn_cast<Constant>(this)) {
Chris Lattnere9fa33e2009-02-28 23:20:19 +00002118 TypePrinting TypePrinter;
Chris Lattner0f7364b2009-02-28 21:26:53 +00002119 TypePrinter.print(C->getType(), OS);
Chris Lattner6d4306e2009-02-28 21:11:05 +00002120 OS << ' ';
Chris Lattner9cc34462009-02-28 20:25:14 +00002121 WriteConstantInt(OS, C, TypePrinter, 0);
Chris Lattner944fac72008-08-23 22:23:09 +00002122 } else if (const Argument *A = dyn_cast<Argument>(this)) {
2123 WriteAsOperand(OS, this, true,
2124 A->getParent() ? A->getParent()->getParent() : 0);
2125 } else if (isa<InlineAsm>(this)) {
2126 WriteAsOperand(OS, this, true, 0);
2127 } else {
Dan Gohmancd26ec52009-09-23 01:33:16 +00002128 // Otherwise we don't know what it is. Call the virtual function to
2129 // allow a subclass to print itself.
2130 printCustom(OS);
Chris Lattner944fac72008-08-23 22:23:09 +00002131 }
2132}
2133
Dan Gohmancd26ec52009-09-23 01:33:16 +00002134// Value::printCustom - subclasses should override this to implement printing.
2135void Value::printCustom(raw_ostream &OS) const {
2136 llvm_unreachable("Unknown value to print out!");
2137}
2138
Chris Lattner7059e532008-08-25 17:03:15 +00002139// Value::dump - allow easy printing of Values from the debugger.
Dan Gohmanf871ccb2009-03-23 15:57:19 +00002140void Value::dump() const { print(errs()); errs() << '\n'; }
Reid Spencerfa452c02004-05-25 18:14:38 +00002141
Chris Lattner7059e532008-08-25 17:03:15 +00002142// Type::dump - allow easy printing of Types from the debugger.
Chris Lattner795daec2008-10-01 20:16:19 +00002143// This one uses type names from the given context module
2144void Type::dump(const Module *Context) const {
2145 WriteTypeSymbolic(errs(), this, Context);
2146 errs() << '\n';
Chris Lattner795daec2008-10-01 20:16:19 +00002147}
2148
Chris Lattnerc2871372009-02-28 21:05:51 +00002149// Type::dump - allow easy printing of Types from the debugger.
2150void Type::dump() const { dump(0); }
2151
Chris Lattner7059e532008-08-25 17:03:15 +00002152// Module::dump() - Allow printing of Modules from the debugger.
Dan Gohmanf871ccb2009-03-23 15:57:19 +00002153void Module::dump() const { print(errs(), 0); }