blob: 09b8aa507d8334ca8b17437bb5d10e0e07909d98 [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"
Erick Tryzelaarf3d5c912010-03-02 05:32:52 +000020#include "llvm/LLVMContext.h"
Chris Lattnerd5118982005-05-06 20:26:43 +000021#include "llvm/CallingConv.h"
Chris Lattnerf2d577b2004-01-20 19:50:34 +000022#include "llvm/Constants.h"
Chris Lattner3eb59c02002-04-29 18:46:50 +000023#include "llvm/DerivedTypes.h"
Chris Lattner863517a2006-01-25 18:57:27 +000024#include "llvm/InlineAsm.h"
Chris Lattner3990b122009-12-28 23:41:32 +000025#include "llvm/IntrinsicInst.h"
Dan Gohman1224c382009-07-20 21:19:07 +000026#include "llvm/Operator.h"
Chris Lattnerf2d577b2004-01-20 19:50:34 +000027#include "llvm/Module.h"
Reid Spenceref9b9a72007-02-05 20:47:22 +000028#include "llvm/ValueSymbolTable.h"
Reid Spencer78d033e2007-01-06 07:24:44 +000029#include "llvm/TypeSymbolTable.h"
Chris Lattner413fd232009-03-01 00:03:38 +000030#include "llvm/ADT/DenseSet.h"
Benjamin Kramer59088392010-01-29 14:42:22 +000031#include "llvm/ADT/SmallString.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"
David Greened865e022010-01-05 01:29:26 +000035#include "llvm/Support/Debug.h"
Devang Patel2d5988d2009-09-30 20:16:54 +000036#include "llvm/Support/Dwarf.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000037#include "llvm/Support/ErrorHandling.h"
Jim Laskeycb6682f2005-08-17 19:34:49 +000038#include "llvm/Support/MathExtras.h"
Dan Gohman683e9222009-08-12 17:23:50 +000039#include "llvm/Support/FormattedStream.h"
Chris Lattner007377f2001-09-07 16:36:04 +000040#include <algorithm>
Reid Spencer4ad513c2007-05-22 19:27:35 +000041#include <cctype>
Devang Patel923078c2009-07-01 19:21:12 +000042#include <map>
Chris Lattner31f84992003-11-21 20:23:48 +000043using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000044
Reid Spenceredd5d9e2005-05-15 16:13:11 +000045// Make virtual table appear in this compilation unit.
46AssemblyAnnotationWriter::~AssemblyAnnotationWriter() {}
47
Chris Lattner6ab910b2008-08-19 04:36:02 +000048//===----------------------------------------------------------------------===//
49// Helper Functions
50//===----------------------------------------------------------------------===//
51
52static const Module *getModuleFromVal(const Value *V) {
53 if (const Argument *MA = dyn_cast<Argument>(V))
54 return MA->getParent() ? MA->getParent()->getParent() : 0;
Daniel Dunbara279bc32009-09-20 02:20:51 +000055
Chris Lattner6ab910b2008-08-19 04:36:02 +000056 if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
57 return BB->getParent() ? BB->getParent()->getParent() : 0;
Daniel Dunbara279bc32009-09-20 02:20:51 +000058
Chris Lattner6ab910b2008-08-19 04:36:02 +000059 if (const Instruction *I = dyn_cast<Instruction>(V)) {
60 const Function *M = I->getParent() ? I->getParent()->getParent() : 0;
61 return M ? M->getParent() : 0;
62 }
Chris Lattner4a3d3a52009-12-31 01:41:14 +000063
Chris Lattner6ab910b2008-08-19 04:36:02 +000064 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
65 return GV->getParent();
Chris Lattner4a3d3a52009-12-31 01:41:14 +000066 if (const NamedMDNode *NMD = dyn_cast<NamedMDNode>(V))
67 return NMD->getParent();
Chris Lattner6ab910b2008-08-19 04:36:02 +000068 return 0;
69}
70
Daniel Dunbare9da1332008-10-28 19:33:02 +000071// PrintEscapedString - Print each character of the specified string, escaping
72// it if it is not printable or if it is an escape char.
Chris Lattner8fff1262010-07-07 23:16:37 +000073static void PrintEscapedString(StringRef Name, raw_ostream &Out) {
Daniel Dunbar03d76512009-07-25 23:55:21 +000074 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
75 unsigned char C = Name[i];
Nick Lewycky34a40862009-03-15 06:39:52 +000076 if (isprint(C) && C != '\\' && C != '"')
Daniel Dunbare9da1332008-10-28 19:33:02 +000077 Out << C;
78 else
79 Out << '\\' << hexdigit(C >> 4) << hexdigit(C & 0x0F);
80 }
81}
82
Chris Lattner6ab910b2008-08-19 04:36:02 +000083enum PrefixType {
84 GlobalPrefix,
85 LabelPrefix,
Daniel Dunbarcad35802008-10-14 23:28:09 +000086 LocalPrefix,
87 NoPrefix
Chris Lattner6ab910b2008-08-19 04:36:02 +000088};
89
90/// PrintLLVMName - Turn the specified name into an 'LLVM name', which is either
91/// prefixed with % (if the string only contains simple characters) or is
92/// surrounded with ""'s (if it has special chars in it). Print it out.
Benjamin Kramer38e59892010-07-14 22:38:02 +000093static void PrintLLVMName(raw_ostream &OS, StringRef Name, PrefixType Prefix) {
Daniel Dunbar03d76512009-07-25 23:55:21 +000094 assert(Name.data() && "Cannot get empty name!");
Chris Lattner6ab910b2008-08-19 04:36:02 +000095 switch (Prefix) {
Torok Edwinc23197a2009-07-14 16:55:14 +000096 default: llvm_unreachable("Bad prefix!");
Daniel Dunbarcad35802008-10-14 23:28:09 +000097 case NoPrefix: break;
Chris Lattner52b26de2008-08-19 05:16:28 +000098 case GlobalPrefix: OS << '@'; break;
99 case LabelPrefix: break;
100 case LocalPrefix: OS << '%'; break;
Nick Lewycky04234832009-03-19 06:31:22 +0000101 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000102
Chris Lattner6ab910b2008-08-19 04:36:02 +0000103 // Scan the name to see if it needs quotes first.
Daniel Dunbar03d76512009-07-25 23:55:21 +0000104 bool NeedsQuotes = isdigit(Name[0]);
Chris Lattner6ab910b2008-08-19 04:36:02 +0000105 if (!NeedsQuotes) {
Daniel Dunbar03d76512009-07-25 23:55:21 +0000106 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
107 char C = Name[i];
Chris Lattner6ab910b2008-08-19 04:36:02 +0000108 if (!isalnum(C) && C != '-' && C != '.' && C != '_') {
109 NeedsQuotes = true;
110 break;
111 }
112 }
113 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000114
Chris Lattner6ab910b2008-08-19 04:36:02 +0000115 // If we didn't need any quotes, just write out the name in one blast.
116 if (!NeedsQuotes) {
Daniel Dunbar03d76512009-07-25 23:55:21 +0000117 OS << Name;
Chris Lattner6ab910b2008-08-19 04:36:02 +0000118 return;
119 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000120
Chris Lattner6ab910b2008-08-19 04:36:02 +0000121 // Okay, we need quotes. Output the quotes and escape any scary characters as
122 // needed.
123 OS << '"';
Daniel Dunbar03d76512009-07-25 23:55:21 +0000124 PrintEscapedString(Name, OS);
Chris Lattner6ab910b2008-08-19 04:36:02 +0000125 OS << '"';
126}
127
128/// PrintLLVMName - Turn the specified name into an 'LLVM name', which is either
129/// prefixed with % (if the string only contains simple characters) or is
130/// surrounded with ""'s (if it has special chars in it). Print it out.
Dan Gohman1220e102009-08-12 20:56:03 +0000131static void PrintLLVMName(raw_ostream &OS, const Value *V) {
Daniel Dunbara279bc32009-09-20 02:20:51 +0000132 PrintLLVMName(OS, V->getName(),
Chris Lattner6ab910b2008-08-19 04:36:02 +0000133 isa<GlobalValue>(V) ? GlobalPrefix : LocalPrefix);
134}
135
Chris Lattner9cc34462009-02-28 20:25:14 +0000136//===----------------------------------------------------------------------===//
137// TypePrinting Class: Type printing machinery
138//===----------------------------------------------------------------------===//
139
Chris Lattner87185e82009-02-28 23:03:55 +0000140static DenseMap<const Type *, std::string> &getTypeNamesMap(void *M) {
141 return *static_cast<DenseMap<const Type *, std::string>*>(M);
Chris Lattnerd8030a72009-02-28 22:34:45 +0000142}
143
144void TypePrinting::clear() {
145 getTypeNamesMap(TypeNames).clear();
146}
Chris Lattner9cc34462009-02-28 20:25:14 +0000147
Chris Lattnere9fa33e2009-02-28 23:20:19 +0000148bool TypePrinting::hasTypeName(const Type *Ty) const {
149 return getTypeNamesMap(TypeNames).count(Ty);
150}
151
152void TypePrinting::addTypeName(const Type *Ty, const std::string &N) {
153 getTypeNamesMap(TypeNames).insert(std::make_pair(Ty, N));
154}
155
156
157TypePrinting::TypePrinting() {
Chris Lattner87185e82009-02-28 23:03:55 +0000158 TypeNames = new DenseMap<const Type *, std::string>();
Chris Lattner9cc34462009-02-28 20:25:14 +0000159}
160
Chris Lattnerd8030a72009-02-28 22:34:45 +0000161TypePrinting::~TypePrinting() {
162 delete &getTypeNamesMap(TypeNames);
163}
164
Chris Lattner534361e2009-02-28 20:49:40 +0000165/// CalcTypeName - Write the specified type to the specified raw_ostream, making
166/// use of type names or up references to shorten the type name where possible.
167void TypePrinting::CalcTypeName(const Type *Ty,
Chris Lattnerb840cac2009-02-28 20:34:19 +0000168 SmallVectorImpl<const Type *> &TypeStack,
Chris Lattnerd2b6cb02009-03-01 01:16:21 +0000169 raw_ostream &OS, bool IgnoreTopLevelName) {
Chris Lattner9cc34462009-02-28 20:25:14 +0000170 // Check to see if the type is named.
Chris Lattnerd2b6cb02009-03-01 01:16:21 +0000171 if (!IgnoreTopLevelName) {
Nick Lewycky04234832009-03-19 06:31:22 +0000172 DenseMap<const Type *, std::string> &TM = getTypeNamesMap(TypeNames);
Chris Lattnerd2b6cb02009-03-01 01:16:21 +0000173 DenseMap<const Type *, std::string>::iterator I = TM.find(Ty);
174 if (I != TM.end()) {
175 OS << I->second;
176 return;
177 }
Chris Lattner9cc34462009-02-28 20:25:14 +0000178 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000179
Chris Lattner9cc34462009-02-28 20:25:14 +0000180 // Check to see if the Type is already on the stack...
181 unsigned Slot = 0, CurSize = TypeStack.size();
182 while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type
Daniel Dunbara279bc32009-09-20 02:20:51 +0000183
Chris Lattner9cc34462009-02-28 20:25:14 +0000184 // This is another base case for the recursion. In this case, we know
185 // that we have looped back to a type that we have previously visited.
186 // Generate the appropriate upreference to handle this.
187 if (Slot < CurSize) {
Chris Lattner30794262009-02-28 21:27:31 +0000188 OS << '\\' << unsigned(CurSize-Slot); // Here's the upreference
Chris Lattner9cc34462009-02-28 20:25:14 +0000189 return;
190 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000191
Chris Lattner9cc34462009-02-28 20:25:14 +0000192 TypeStack.push_back(Ty); // Recursive case: Add us to the stack..
Daniel Dunbara279bc32009-09-20 02:20:51 +0000193
Chris Lattner9cc34462009-02-28 20:25:14 +0000194 switch (Ty->getTypeID()) {
Chris Lattner30794262009-02-28 21:27:31 +0000195 case Type::VoidTyID: OS << "void"; break;
196 case Type::FloatTyID: OS << "float"; break;
197 case Type::DoubleTyID: OS << "double"; break;
198 case Type::X86_FP80TyID: OS << "x86_fp80"; break;
199 case Type::FP128TyID: OS << "fp128"; break;
200 case Type::PPC_FP128TyID: OS << "ppc_fp128"; break;
201 case Type::LabelTyID: OS << "label"; break;
Nick Lewycky7a0370f2009-05-30 05:06:04 +0000202 case Type::MetadataTyID: OS << "metadata"; break;
Chris Lattner583ffd82009-02-28 21:18:43 +0000203 case Type::IntegerTyID:
Chris Lattner30794262009-02-28 21:27:31 +0000204 OS << 'i' << cast<IntegerType>(Ty)->getBitWidth();
Chris Lattner583ffd82009-02-28 21:18:43 +0000205 break;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000206
Chris Lattner36942d72009-02-28 20:35:42 +0000207 case Type::FunctionTyID: {
208 const FunctionType *FTy = cast<FunctionType>(Ty);
Chris Lattner30794262009-02-28 21:27:31 +0000209 CalcTypeName(FTy->getReturnType(), TypeStack, OS);
210 OS << " (";
Chris Lattner36942d72009-02-28 20:35:42 +0000211 for (FunctionType::param_iterator I = FTy->param_begin(),
212 E = FTy->param_end(); I != E; ++I) {
213 if (I != FTy->param_begin())
Chris Lattner30794262009-02-28 21:27:31 +0000214 OS << ", ";
215 CalcTypeName(*I, TypeStack, OS);
Chris Lattner9cc34462009-02-28 20:25:14 +0000216 }
Chris Lattner36942d72009-02-28 20:35:42 +0000217 if (FTy->isVarArg()) {
Chris Lattner30794262009-02-28 21:27:31 +0000218 if (FTy->getNumParams()) OS << ", ";
219 OS << "...";
Chris Lattner9cc34462009-02-28 20:25:14 +0000220 }
Chris Lattner30794262009-02-28 21:27:31 +0000221 OS << ')';
Chris Lattner36942d72009-02-28 20:35:42 +0000222 break;
223 }
224 case Type::StructTyID: {
225 const StructType *STy = cast<StructType>(Ty);
226 if (STy->isPacked())
Chris Lattner30794262009-02-28 21:27:31 +0000227 OS << '<';
Dan Gohman29e13e02010-04-08 18:03:05 +0000228 OS << '{';
Chris Lattner36942d72009-02-28 20:35:42 +0000229 for (StructType::element_iterator I = STy->element_begin(),
230 E = STy->element_end(); I != E; ++I) {
Chris Lattner30794262009-02-28 21:27:31 +0000231 OS << ' ';
Dan Gohman29e13e02010-04-08 18:03:05 +0000232 CalcTypeName(*I, TypeStack, OS);
233 if (next(I) == STy->element_end())
234 OS << ' ';
235 else
236 OS << ',';
Chris Lattner9cc34462009-02-28 20:25:14 +0000237 }
Chris Lattner30794262009-02-28 21:27:31 +0000238 OS << '}';
Chris Lattner36942d72009-02-28 20:35:42 +0000239 if (STy->isPacked())
Chris Lattner30794262009-02-28 21:27:31 +0000240 OS << '>';
Chris Lattner36942d72009-02-28 20:35:42 +0000241 break;
242 }
Chris Lattnerfdfeb692010-02-12 20:49:41 +0000243 case Type::UnionTyID: {
244 const UnionType *UTy = cast<UnionType>(Ty);
Dan Gohman29e13e02010-04-08 18:03:05 +0000245 OS << "union {";
Chris Lattnerfdfeb692010-02-12 20:49:41 +0000246 for (StructType::element_iterator I = UTy->element_begin(),
247 E = UTy->element_end(); I != E; ++I) {
Chris Lattnerfdfeb692010-02-12 20:49:41 +0000248 OS << ' ';
Dan Gohman29e13e02010-04-08 18:03:05 +0000249 CalcTypeName(*I, TypeStack, OS);
250 if (next(I) == UTy->element_end())
251 OS << ' ';
252 else
253 OS << ',';
Chris Lattnerfdfeb692010-02-12 20:49:41 +0000254 }
255 OS << '}';
256 break;
257 }
Chris Lattner36942d72009-02-28 20:35:42 +0000258 case Type::PointerTyID: {
259 const PointerType *PTy = cast<PointerType>(Ty);
Chris Lattner30794262009-02-28 21:27:31 +0000260 CalcTypeName(PTy->getElementType(), TypeStack, OS);
Chris Lattner36942d72009-02-28 20:35:42 +0000261 if (unsigned AddressSpace = PTy->getAddressSpace())
Chris Lattner30794262009-02-28 21:27:31 +0000262 OS << " addrspace(" << AddressSpace << ')';
263 OS << '*';
Chris Lattner36942d72009-02-28 20:35:42 +0000264 break;
265 }
266 case Type::ArrayTyID: {
267 const ArrayType *ATy = cast<ArrayType>(Ty);
Chris Lattner30794262009-02-28 21:27:31 +0000268 OS << '[' << ATy->getNumElements() << " x ";
269 CalcTypeName(ATy->getElementType(), TypeStack, OS);
270 OS << ']';
Chris Lattner36942d72009-02-28 20:35:42 +0000271 break;
272 }
273 case Type::VectorTyID: {
274 const VectorType *PTy = cast<VectorType>(Ty);
Chris Lattner30794262009-02-28 21:27:31 +0000275 OS << "<" << PTy->getNumElements() << " x ";
276 CalcTypeName(PTy->getElementType(), TypeStack, OS);
277 OS << '>';
Chris Lattner36942d72009-02-28 20:35:42 +0000278 break;
279 }
280 case Type::OpaqueTyID:
Chris Lattner30794262009-02-28 21:27:31 +0000281 OS << "opaque";
Chris Lattner36942d72009-02-28 20:35:42 +0000282 break;
283 default:
Chris Lattner30794262009-02-28 21:27:31 +0000284 OS << "<unrecognized-type>";
Chris Lattner36942d72009-02-28 20:35:42 +0000285 break;
Chris Lattner9cc34462009-02-28 20:25:14 +0000286 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000287
Chris Lattner534361e2009-02-28 20:49:40 +0000288 TypeStack.pop_back(); // Remove self from stack.
Chris Lattner9cc34462009-02-28 20:25:14 +0000289}
290
291/// printTypeInt - The internal guts of printing out a type that has a
292/// potentially named portion.
293///
Chris Lattnerd2b6cb02009-03-01 01:16:21 +0000294void TypePrinting::print(const Type *Ty, raw_ostream &OS,
295 bool IgnoreTopLevelName) {
Chris Lattner9cc34462009-02-28 20:25:14 +0000296 // Check to see if the type is named.
Chris Lattner87185e82009-02-28 23:03:55 +0000297 DenseMap<const Type*, std::string> &TM = getTypeNamesMap(TypeNames);
Chris Lattnerd2b6cb02009-03-01 01:16:21 +0000298 if (!IgnoreTopLevelName) {
299 DenseMap<const Type*, std::string>::iterator I = TM.find(Ty);
300 if (I != TM.end()) {
301 OS << I->second;
302 return;
303 }
Chris Lattner9cc34462009-02-28 20:25:14 +0000304 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000305
Chris Lattner9cc34462009-02-28 20:25:14 +0000306 // Otherwise we have a type that has not been named but is a derived type.
307 // Carefully recurse the type hierarchy to print out any contained symbolic
308 // names.
Chris Lattnerb840cac2009-02-28 20:34:19 +0000309 SmallVector<const Type *, 16> TypeStack;
Chris Lattner9cc34462009-02-28 20:25:14 +0000310 std::string TypeName;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000311
Chris Lattner534361e2009-02-28 20:49:40 +0000312 raw_string_ostream TypeOS(TypeName);
Chris Lattnerd2b6cb02009-03-01 01:16:21 +0000313 CalcTypeName(Ty, TypeStack, TypeOS, IgnoreTopLevelName);
Chris Lattner534361e2009-02-28 20:49:40 +0000314 OS << TypeOS.str();
315
316 // Cache type name for later use.
Chris Lattnerd2b6cb02009-03-01 01:16:21 +0000317 if (!IgnoreTopLevelName)
318 TM.insert(std::make_pair(Ty, TypeOS.str()));
Chris Lattner9cc34462009-02-28 20:25:14 +0000319}
320
Chris Lattner413fd232009-03-01 00:03:38 +0000321namespace {
322 class TypeFinder {
323 // To avoid walking constant expressions multiple times and other IR
324 // objects, we keep several helper maps.
325 DenseSet<const Value*> VisitedConstants;
326 DenseSet<const Type*> VisitedTypes;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000327
Chris Lattner413fd232009-03-01 00:03:38 +0000328 TypePrinting &TP;
329 std::vector<const Type*> &NumberedTypes;
330 public:
331 TypeFinder(TypePrinting &tp, std::vector<const Type*> &numberedTypes)
332 : TP(tp), NumberedTypes(numberedTypes) {}
Daniel Dunbara279bc32009-09-20 02:20:51 +0000333
Chris Lattner413fd232009-03-01 00:03:38 +0000334 void Run(const Module &M) {
Chris Lattner88485862009-03-01 00:32:33 +0000335 // Get types from the type symbol table. This gets opaque types referened
336 // only through derived named types.
337 const TypeSymbolTable &ST = M.getTypeSymbolTable();
338 for (TypeSymbolTable::const_iterator TI = ST.begin(), E = ST.end();
339 TI != E; ++TI)
340 IncorporateType(TI->second);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000341
Chris Lattner413fd232009-03-01 00:03:38 +0000342 // Get types from global variables.
343 for (Module::const_global_iterator I = M.global_begin(),
344 E = M.global_end(); I != E; ++I) {
345 IncorporateType(I->getType());
346 if (I->hasInitializer())
347 IncorporateValue(I->getInitializer());
348 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000349
Chris Lattner413fd232009-03-01 00:03:38 +0000350 // Get types from aliases.
351 for (Module::const_alias_iterator I = M.alias_begin(),
352 E = M.alias_end(); I != E; ++I) {
353 IncorporateType(I->getType());
354 IncorporateValue(I->getAliasee());
355 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000356
Chris Lattner413fd232009-03-01 00:03:38 +0000357 // Get types from functions.
358 for (Module::const_iterator FI = M.begin(), E = M.end(); FI != E; ++FI) {
359 IncorporateType(FI->getType());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000360
Chris Lattner413fd232009-03-01 00:03:38 +0000361 for (Function::const_iterator BB = FI->begin(), E = FI->end();
362 BB != E;++BB)
363 for (BasicBlock::const_iterator II = BB->begin(),
364 E = BB->end(); II != E; ++II) {
365 const Instruction &I = *II;
366 // Incorporate the type of the instruction and all its operands.
367 IncorporateType(I.getType());
368 for (User::const_op_iterator OI = I.op_begin(), OE = I.op_end();
369 OI != OE; ++OI)
370 IncorporateValue(*OI);
371 }
372 }
373 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000374
Chris Lattner413fd232009-03-01 00:03:38 +0000375 private:
376 void IncorporateType(const Type *Ty) {
377 // Check to see if we're already visited this type.
Chris Lattner88485862009-03-01 00:32:33 +0000378 if (!VisitedTypes.insert(Ty).second)
Chris Lattner413fd232009-03-01 00:03:38 +0000379 return;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000380
Chris Lattner413fd232009-03-01 00:03:38 +0000381 // If this is a structure or opaque type, add a name for the type.
Duncan Sands1df98592010-02-16 11:11:14 +0000382 if (((Ty->isStructTy() && cast<StructType>(Ty)->getNumElements())
Duncan Sands47c51882010-02-16 14:50:09 +0000383 || Ty->isOpaqueTy()) && !TP.hasTypeName(Ty)) {
Chris Lattner413fd232009-03-01 00:03:38 +0000384 TP.addTypeName(Ty, "%"+utostr(unsigned(NumberedTypes.size())));
385 NumberedTypes.push_back(Ty);
386 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000387
Chris Lattner413fd232009-03-01 00:03:38 +0000388 // Recursively walk all contained types.
389 for (Type::subtype_iterator I = Ty->subtype_begin(),
390 E = Ty->subtype_end(); I != E; ++I)
Daniel Dunbara279bc32009-09-20 02:20:51 +0000391 IncorporateType(*I);
Chris Lattner413fd232009-03-01 00:03:38 +0000392 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000393
Chris Lattner413fd232009-03-01 00:03:38 +0000394 /// IncorporateValue - This method is used to walk operand lists finding
395 /// types hiding in constant expressions and other operands that won't be
396 /// walked in other ways. GlobalValues, basic blocks, instructions, and
397 /// inst operands are all explicitly enumerated.
398 void IncorporateValue(const Value *V) {
399 if (V == 0 || !isa<Constant>(V) || isa<GlobalValue>(V)) return;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000400
Chris Lattner413fd232009-03-01 00:03:38 +0000401 // Already visited?
402 if (!VisitedConstants.insert(V).second)
403 return;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000404
Chris Lattner413fd232009-03-01 00:03:38 +0000405 // Check this type.
406 IncorporateType(V->getType());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000407
Chris Lattner413fd232009-03-01 00:03:38 +0000408 // Look in operands for types.
409 const Constant *C = cast<Constant>(V);
410 for (Constant::const_op_iterator I = C->op_begin(),
411 E = C->op_end(); I != E;++I)
412 IncorporateValue(*I);
413 }
414 };
415} // end anonymous namespace
416
417
418/// AddModuleTypesToPrinter - Add all of the symbolic type names for types in
419/// the specified module to the TypePrinter and all numbered types to it and the
420/// NumberedTypes table.
Daniel Dunbara279bc32009-09-20 02:20:51 +0000421static void AddModuleTypesToPrinter(TypePrinting &TP,
Chris Lattner413fd232009-03-01 00:03:38 +0000422 std::vector<const Type*> &NumberedTypes,
423 const Module *M) {
Chris Lattnere9fa33e2009-02-28 23:20:19 +0000424 if (M == 0) return;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000425
Chris Lattnere9fa33e2009-02-28 23:20:19 +0000426 // If the module has a symbol table, take all global types and stuff their
427 // names into the TypeNames map.
428 const TypeSymbolTable &ST = M->getTypeSymbolTable();
429 for (TypeSymbolTable::const_iterator TI = ST.begin(), E = ST.end();
430 TI != E; ++TI) {
431 const Type *Ty = cast<Type>(TI->second);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000432
Chris Lattnere9fa33e2009-02-28 23:20:19 +0000433 // As a heuristic, don't insert pointer to primitive types, because
434 // they are used too often to have a single useful name.
435 if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
436 const Type *PETy = PTy->getElementType();
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000437 if ((PETy->isPrimitiveType() || PETy->isIntegerTy()) &&
Duncan Sands47c51882010-02-16 14:50:09 +0000438 !PETy->isOpaqueTy())
Chris Lattnere9fa33e2009-02-28 23:20:19 +0000439 continue;
440 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000441
Chris Lattnere9fa33e2009-02-28 23:20:19 +0000442 // Likewise don't insert primitives either.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000443 if (Ty->isIntegerTy() || Ty->isPrimitiveType())
Chris Lattnere9fa33e2009-02-28 23:20:19 +0000444 continue;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000445
Chris Lattnere9fa33e2009-02-28 23:20:19 +0000446 // Get the name as a string and insert it into TypeNames.
447 std::string NameStr;
Dan Gohman683e9222009-08-12 17:23:50 +0000448 raw_string_ostream NameROS(NameStr);
449 formatted_raw_ostream NameOS(NameROS);
Daniel Dunbar03d76512009-07-25 23:55:21 +0000450 PrintLLVMName(NameOS, TI->first, LocalPrefix);
Dan Gohman683e9222009-08-12 17:23:50 +0000451 NameOS.flush();
452 TP.addTypeName(Ty, NameStr);
Chris Lattnere9fa33e2009-02-28 23:20:19 +0000453 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000454
Chris Lattner413fd232009-03-01 00:03:38 +0000455 // Walk the entire module to find references to unnamed structure and opaque
456 // types. This is required for correctness by opaque types (because multiple
457 // uses of an unnamed opaque type needs to be referred to by the same ID) and
458 // it shrinks complex recursive structure types substantially in some cases.
459 TypeFinder(TP, NumberedTypes).Run(*M);
Chris Lattnere9fa33e2009-02-28 23:20:19 +0000460}
461
Chris Lattner9cc34462009-02-28 20:25:14 +0000462
Chris Lattner9cc34462009-02-28 20:25:14 +0000463/// WriteTypeSymbolic - This attempts to write the specified type as a symbolic
464/// type, iff there is an entry in the modules symbol table for the specified
Chris Lattnerc2871372009-02-28 21:05:51 +0000465/// type or one of it's component types.
Chris Lattner9cc34462009-02-28 20:25:14 +0000466///
Chris Lattnere9fa33e2009-02-28 23:20:19 +0000467void llvm::WriteTypeSymbolic(raw_ostream &OS, const Type *Ty, const Module *M) {
468 TypePrinting Printer;
Chris Lattner413fd232009-03-01 00:03:38 +0000469 std::vector<const Type*> NumberedTypes;
470 AddModuleTypesToPrinter(Printer, NumberedTypes, M);
Chris Lattnere9fa33e2009-02-28 23:20:19 +0000471 Printer.print(Ty, OS);
Chris Lattner9cc34462009-02-28 20:25:14 +0000472}
473
Chris Lattner6ab910b2008-08-19 04:36:02 +0000474//===----------------------------------------------------------------------===//
475// SlotTracker Class: Enumerate slot numbers for unnamed values
476//===----------------------------------------------------------------------===//
477
Chris Lattnerb64871a2008-08-19 04:28:07 +0000478namespace {
479
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000480/// This class provides computation of slot numbers for LLVM Assembly writing.
Chris Lattner45d4c732008-08-17 04:17:45 +0000481///
Chris Lattner0d9574a2008-08-19 04:26:57 +0000482class SlotTracker {
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000483public:
Devang Patel320671d2009-07-08 21:44:25 +0000484 /// ValueMap - A mapping of Values to slot numbers.
Chris Lattner92255072008-08-17 17:25:25 +0000485 typedef DenseMap<const Value*, unsigned> ValueMap;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000486
487private:
Devang Patel320671d2009-07-08 21:44:25 +0000488 /// TheModule - The module for which we are holding slot numbers.
Chris Lattner45d4c732008-08-17 04:17:45 +0000489 const Module* TheModule;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000490
Devang Patel320671d2009-07-08 21:44:25 +0000491 /// TheFunction - The function for which we are holding slot numbers.
Chris Lattner45d4c732008-08-17 04:17:45 +0000492 const Function* TheFunction;
493 bool FunctionProcessed;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000494
Devang Patel320671d2009-07-08 21:44:25 +0000495 /// mMap - The TypePlanes map for the module level data.
Chris Lattner45d4c732008-08-17 04:17:45 +0000496 ValueMap mMap;
497 unsigned mNext;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000498
Devang Patel320671d2009-07-08 21:44:25 +0000499 /// fMap - The TypePlanes map for the function level data.
Chris Lattner45d4c732008-08-17 04:17:45 +0000500 ValueMap fMap;
501 unsigned fNext;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000502
Devang Patel320671d2009-07-08 21:44:25 +0000503 /// mdnMap - Map for MDNodes.
Chris Lattner307c9892009-12-31 02:20:11 +0000504 DenseMap<const MDNode*, unsigned> mdnMap;
Devang Patel320671d2009-07-08 21:44:25 +0000505 unsigned mdnNext;
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000506public:
Chris Lattner45d4c732008-08-17 04:17:45 +0000507 /// Construct from a module
Chris Lattner0d9574a2008-08-19 04:26:57 +0000508 explicit SlotTracker(const Module *M);
Chris Lattner45d4c732008-08-17 04:17:45 +0000509 /// Construct from a function, starting out in incorp state.
Chris Lattner0d9574a2008-08-19 04:26:57 +0000510 explicit SlotTracker(const Function *F);
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000511
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000512 /// Return the slot number of the specified value in it's type
Chris Lattner0d9574a2008-08-19 04:26:57 +0000513 /// plane. If something is not in the SlotTracker, return -1.
Chris Lattner22379bc2007-01-11 03:54:27 +0000514 int getLocalSlot(const Value *V);
515 int getGlobalSlot(const GlobalValue *V);
Devang Patel320671d2009-07-08 21:44:25 +0000516 int getMetadataSlot(const MDNode *N);
Reid Spencerfc621e22004-06-09 15:26:53 +0000517
Misha Brukmanfd939082005-04-21 23:48:37 +0000518 /// If you'd like to deal with a function instead of just a module, use
Chris Lattner0d9574a2008-08-19 04:26:57 +0000519 /// this method to get its data into the SlotTracker.
Misha Brukmanfd939082005-04-21 23:48:37 +0000520 void incorporateFunction(const Function *F) {
521 TheFunction = F;
Reid Spencer28531c72004-08-16 07:46:33 +0000522 FunctionProcessed = false;
523 }
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000524
Misha Brukmanfd939082005-04-21 23:48:37 +0000525 /// After calling incorporateFunction, use this method to remove the
Chris Lattner0d9574a2008-08-19 04:26:57 +0000526 /// most recently incorporated function from the SlotTracker. This
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000527 /// will reset the state of the machine back to just the module contents.
528 void purgeFunction();
529
Devang Patel320671d2009-07-08 21:44:25 +0000530 /// MDNode map iterators.
Chris Lattner307c9892009-12-31 02:20:11 +0000531 typedef DenseMap<const MDNode*, unsigned>::iterator mdn_iterator;
532 mdn_iterator mdn_begin() { return mdnMap.begin(); }
533 mdn_iterator mdn_end() { return mdnMap.end(); }
534 unsigned mdn_size() const { return mdnMap.size(); }
535 bool mdn_empty() const { return mdnMap.empty(); }
Devang Patel320671d2009-07-08 21:44:25 +0000536
Reid Spencerb03de0c2004-05-26 21:56:09 +0000537 /// This function does the actual initialization.
538 inline void initialize();
539
Devang Patel320671d2009-07-08 21:44:25 +0000540 // Implementation Details
541private:
Chris Lattner9446bbe2007-01-09 07:55:49 +0000542 /// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
543 void CreateModuleSlot(const GlobalValue *V);
Devang Patel320671d2009-07-08 21:44:25 +0000544
545 /// CreateMetadataSlot - Insert the specified MDNode* into the slot table.
546 void CreateMetadataSlot(const MDNode *N);
547
Chris Lattner9446bbe2007-01-09 07:55:49 +0000548 /// CreateFunctionSlot - Insert the specified Value* into the slot table.
549 void CreateFunctionSlot(const Value *V);
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000550
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000551 /// Add all of the module level global variables (and their initializers)
552 /// and function declarations, but not the contents of those functions.
553 void processModule();
554
Devang Patel320671d2009-07-08 21:44:25 +0000555 /// Add all of the functions arguments, basic blocks, and instructions.
Reid Spencerb03de0c2004-05-26 21:56:09 +0000556 void processFunction();
557
Chris Lattner0d9574a2008-08-19 04:26:57 +0000558 SlotTracker(const SlotTracker &); // DO NOT IMPLEMENT
559 void operator=(const SlotTracker &); // DO NOT IMPLEMENT
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000560};
561
Chris Lattnerb64871a2008-08-19 04:28:07 +0000562} // end anonymous namespace
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000563
Chris Lattner6ab910b2008-08-19 04:36:02 +0000564
565static SlotTracker *createSlotTracker(const Value *V) {
566 if (const Argument *FA = dyn_cast<Argument>(V))
567 return new SlotTracker(FA->getParent());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000568
Chris Lattner6ab910b2008-08-19 04:36:02 +0000569 if (const Instruction *I = dyn_cast<Instruction>(V))
570 return new SlotTracker(I->getParent()->getParent());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000571
Chris Lattner6ab910b2008-08-19 04:36:02 +0000572 if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
573 return new SlotTracker(BB->getParent());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000574
Chris Lattner6ab910b2008-08-19 04:36:02 +0000575 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
576 return new SlotTracker(GV->getParent());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000577
Chris Lattner6ab910b2008-08-19 04:36:02 +0000578 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V))
Daniel Dunbara279bc32009-09-20 02:20:51 +0000579 return new SlotTracker(GA->getParent());
580
Chris Lattner6ab910b2008-08-19 04:36:02 +0000581 if (const Function *Func = dyn_cast<Function>(V))
582 return new SlotTracker(Func);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000583
Dale Johannesen5f72a5e2010-01-13 00:00:24 +0000584 if (isa<MDNode>(V))
585 return new SlotTracker((Function *)0);
586
Chris Lattner6ab910b2008-08-19 04:36:02 +0000587 return 0;
588}
589
590#if 0
David Greened865e022010-01-05 01:29:26 +0000591#define ST_DEBUG(X) dbgs() << X
Chris Lattner6ab910b2008-08-19 04:36:02 +0000592#else
Chris Lattner24233032008-08-19 04:47:09 +0000593#define ST_DEBUG(X)
Chris Lattner6ab910b2008-08-19 04:36:02 +0000594#endif
595
596// Module level constructor. Causes the contents of the Module (sans functions)
597// to be added to the slot table.
598SlotTracker::SlotTracker(const Module *M)
Chris Lattner6e6b1802009-12-31 02:13:35 +0000599 : TheModule(M), TheFunction(0), FunctionProcessed(false),
Chris Lattner38cf02e2009-12-31 01:36:50 +0000600 mNext(0), fNext(0), mdnNext(0) {
Chris Lattner6ab910b2008-08-19 04:36:02 +0000601}
602
603// Function level constructor. Causes the contents of the Module and the one
604// function provided to be added to the slot table.
605SlotTracker::SlotTracker(const Function *F)
Chris Lattnercfb5a202008-08-19 05:06:27 +0000606 : TheModule(F ? F->getParent() : 0), TheFunction(F), FunctionProcessed(false),
Chris Lattner6e6b1802009-12-31 02:13:35 +0000607 mNext(0), fNext(0), mdnNext(0) {
Chris Lattner6ab910b2008-08-19 04:36:02 +0000608}
609
610inline void SlotTracker::initialize() {
611 if (TheModule) {
612 processModule();
613 TheModule = 0; ///< Prevent re-processing next time we're called.
614 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000615
Chris Lattner6ab910b2008-08-19 04:36:02 +0000616 if (TheFunction && !FunctionProcessed)
617 processFunction();
618}
619
620// Iterate through all the global variables, functions, and global
621// variable initializers and create slots for them.
622void SlotTracker::processModule() {
Chris Lattner24233032008-08-19 04:47:09 +0000623 ST_DEBUG("begin processModule!\n");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000624
Chris Lattner6ab910b2008-08-19 04:36:02 +0000625 // Add all of the unnamed global variables to the value table.
626 for (Module::const_global_iterator I = TheModule->global_begin(),
Devang Patel320671d2009-07-08 21:44:25 +0000627 E = TheModule->global_end(); I != E; ++I) {
Daniel Dunbara279bc32009-09-20 02:20:51 +0000628 if (!I->hasName())
Chris Lattner6ab910b2008-08-19 04:36:02 +0000629 CreateModuleSlot(I);
Devang Patel320671d2009-07-08 21:44:25 +0000630 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000631
Devang Patel37c4a2d2009-07-29 22:04:47 +0000632 // Add metadata used by named metadata.
Daniel Dunbara279bc32009-09-20 02:20:51 +0000633 for (Module::const_named_metadata_iterator
Devang Patel37c4a2d2009-07-29 22:04:47 +0000634 I = TheModule->named_metadata_begin(),
635 E = TheModule->named_metadata_end(); I != E; ++I) {
636 const NamedMDNode *NMD = I;
Chris Lattner5d0cacd2009-12-31 01:22:29 +0000637 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
Devang Patel69d02e02010-01-05 21:47:32 +0000638 if (MDNode *MD = NMD->getOperand(i))
Devang Patele8861b82009-07-30 01:02:04 +0000639 CreateMetadataSlot(MD);
Devang Patel37c4a2d2009-07-29 22:04:47 +0000640 }
641 }
642
Chris Lattner6ab910b2008-08-19 04:36:02 +0000643 // Add all the unnamed functions to the table.
644 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
645 I != E; ++I)
646 if (!I->hasName())
647 CreateModuleSlot(I);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000648
Chris Lattner24233032008-08-19 04:47:09 +0000649 ST_DEBUG("end processModule!\n");
Chris Lattner6ab910b2008-08-19 04:36:02 +0000650}
651
Chris Lattner6ab910b2008-08-19 04:36:02 +0000652// Process the arguments, basic blocks, and instructions of a function.
653void SlotTracker::processFunction() {
Chris Lattner24233032008-08-19 04:47:09 +0000654 ST_DEBUG("begin processFunction!\n");
Chris Lattner6ab910b2008-08-19 04:36:02 +0000655 fNext = 0;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000656
Chris Lattner6ab910b2008-08-19 04:36:02 +0000657 // Add all the function arguments with no names.
658 for(Function::const_arg_iterator AI = TheFunction->arg_begin(),
659 AE = TheFunction->arg_end(); AI != AE; ++AI)
660 if (!AI->hasName())
661 CreateFunctionSlot(AI);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000662
Chris Lattner24233032008-08-19 04:47:09 +0000663 ST_DEBUG("Inserting Instructions:\n");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000664
Chris Lattner6e6b1802009-12-31 02:13:35 +0000665 SmallVector<std::pair<unsigned, MDNode*>, 4> MDForInst;
Devang Patel43215782009-09-16 20:21:17 +0000666
Chris Lattner6ab910b2008-08-19 04:36:02 +0000667 // Add all of the basic blocks and instructions with no names.
668 for (Function::const_iterator BB = TheFunction->begin(),
669 E = TheFunction->end(); BB != E; ++BB) {
670 if (!BB->hasName())
671 CreateFunctionSlot(BB);
Chris Lattner3990b122009-12-28 23:41:32 +0000672
Daniel Dunbara279bc32009-09-20 02:20:51 +0000673 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E;
Devang Patel320671d2009-07-08 21:44:25 +0000674 ++I) {
Chris Lattner3990b122009-12-28 23:41:32 +0000675 if (!I->getType()->isVoidTy() && !I->hasName())
Chris Lattner6ab910b2008-08-19 04:36:02 +0000676 CreateFunctionSlot(I);
Chris Lattner3990b122009-12-28 23:41:32 +0000677
Chris Lattnerfd450c02010-05-10 20:53:17 +0000678 // Intrinsics can directly use metadata. We allow direct calls to any
679 // llvm.foo function here, because the target may not be linked into the
680 // optimizer.
681 if (const CallInst *CI = dyn_cast<CallInst>(I)) {
682 if (Function *F = CI->getCalledFunction())
683 if (F->getName().startswith("llvm."))
684 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
685 if (MDNode *N = dyn_cast_or_null<MDNode>(I->getOperand(i)))
686 CreateMetadataSlot(N);
687 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000688
Devang Patel43215782009-09-16 20:21:17 +0000689 // Process metadata attached with this instruction.
Chris Lattner3990b122009-12-28 23:41:32 +0000690 I->getAllMetadata(MDForInst);
691 for (unsigned i = 0, e = MDForInst.size(); i != e; ++i)
692 CreateMetadataSlot(MDForInst[i].second);
Chris Lattner6e6b1802009-12-31 02:13:35 +0000693 MDForInst.clear();
Devang Patel320671d2009-07-08 21:44:25 +0000694 }
Chris Lattner6ab910b2008-08-19 04:36:02 +0000695 }
Devang Patel43215782009-09-16 20:21:17 +0000696
Chris Lattner6ab910b2008-08-19 04:36:02 +0000697 FunctionProcessed = true;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000698
Chris Lattner24233032008-08-19 04:47:09 +0000699 ST_DEBUG("end processFunction!\n");
Chris Lattner6ab910b2008-08-19 04:36:02 +0000700}
701
702/// Clean up after incorporating a function. This is the only way to get out of
703/// the function incorporation state that affects get*Slot/Create*Slot. Function
704/// incorporation state is indicated by TheFunction != 0.
705void SlotTracker::purgeFunction() {
Chris Lattner24233032008-08-19 04:47:09 +0000706 ST_DEBUG("begin purgeFunction!\n");
Chris Lattner6ab910b2008-08-19 04:36:02 +0000707 fMap.clear(); // Simply discard the function level map
708 TheFunction = 0;
709 FunctionProcessed = false;
Chris Lattner24233032008-08-19 04:47:09 +0000710 ST_DEBUG("end purgeFunction!\n");
Chris Lattner6ab910b2008-08-19 04:36:02 +0000711}
712
713/// getGlobalSlot - Get the slot number of a global value.
714int SlotTracker::getGlobalSlot(const GlobalValue *V) {
715 // Check for uninitialized state and do lazy initialization.
716 initialize();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000717
Chris Lattner6ab910b2008-08-19 04:36:02 +0000718 // Find the type plane in the module map
719 ValueMap::iterator MI = mMap.find(V);
Dan Gohmanaeaf2452008-10-01 19:58:59 +0000720 return MI == mMap.end() ? -1 : (int)MI->second;
Chris Lattner6ab910b2008-08-19 04:36:02 +0000721}
722
Chris Lattner307c9892009-12-31 02:20:11 +0000723/// getMetadataSlot - Get the slot number of a MDNode.
Devang Patel320671d2009-07-08 21:44:25 +0000724int SlotTracker::getMetadataSlot(const MDNode *N) {
725 // Check for uninitialized state and do lazy initialization.
726 initialize();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000727
Devang Patel320671d2009-07-08 21:44:25 +0000728 // Find the type plane in the module map
Chris Lattner307c9892009-12-31 02:20:11 +0000729 mdn_iterator MI = mdnMap.find(N);
Devang Patel320671d2009-07-08 21:44:25 +0000730 return MI == mdnMap.end() ? -1 : (int)MI->second;
731}
732
Chris Lattner6ab910b2008-08-19 04:36:02 +0000733
734/// getLocalSlot - Get the slot number for a value that is local to a function.
735int SlotTracker::getLocalSlot(const Value *V) {
736 assert(!isa<Constant>(V) && "Can't get a constant or global slot with this!");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000737
Chris Lattner6ab910b2008-08-19 04:36:02 +0000738 // Check for uninitialized state and do lazy initialization.
739 initialize();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000740
Chris Lattner6ab910b2008-08-19 04:36:02 +0000741 ValueMap::iterator FI = fMap.find(V);
Dan Gohmanaeaf2452008-10-01 19:58:59 +0000742 return FI == fMap.end() ? -1 : (int)FI->second;
Chris Lattner6ab910b2008-08-19 04:36:02 +0000743}
744
745
746/// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
747void SlotTracker::CreateModuleSlot(const GlobalValue *V) {
748 assert(V && "Can't insert a null Value into SlotTracker!");
Chris Lattner4ee93c42009-12-29 07:25:48 +0000749 assert(!V->getType()->isVoidTy() && "Doesn't need a slot!");
Chris Lattner6ab910b2008-08-19 04:36:02 +0000750 assert(!V->hasName() && "Doesn't need a slot!");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000751
Chris Lattner6ab910b2008-08-19 04:36:02 +0000752 unsigned DestSlot = mNext++;
753 mMap[V] = DestSlot;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000754
Chris Lattner24233032008-08-19 04:47:09 +0000755 ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" <<
Chris Lattner6ab910b2008-08-19 04:36:02 +0000756 DestSlot << " [");
757 // G = Global, F = Function, A = Alias, o = other
Chris Lattner24233032008-08-19 04:47:09 +0000758 ST_DEBUG((isa<GlobalVariable>(V) ? 'G' :
Chris Lattner6ab910b2008-08-19 04:36:02 +0000759 (isa<Function>(V) ? 'F' :
760 (isa<GlobalAlias>(V) ? 'A' : 'o'))) << "]\n");
761}
762
Chris Lattner6ab910b2008-08-19 04:36:02 +0000763/// CreateSlot - Create a new slot for the specified value if it has no name.
764void SlotTracker::CreateFunctionSlot(const Value *V) {
Chris Lattner4ee93c42009-12-29 07:25:48 +0000765 assert(!V->getType()->isVoidTy() && !V->hasName() && "Doesn't need a slot!");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000766
Chris Lattner6ab910b2008-08-19 04:36:02 +0000767 unsigned DestSlot = fNext++;
768 fMap[V] = DestSlot;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000769
Chris Lattner6ab910b2008-08-19 04:36:02 +0000770 // G = Global, F = Function, o = other
Chris Lattner24233032008-08-19 04:47:09 +0000771 ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" <<
Chris Lattner6ab910b2008-08-19 04:36:02 +0000772 DestSlot << " [o]\n");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000773}
Chris Lattner6ab910b2008-08-19 04:36:02 +0000774
Devang Patel320671d2009-07-08 21:44:25 +0000775/// CreateModuleSlot - Insert the specified MDNode* into the slot table.
776void SlotTracker::CreateMetadataSlot(const MDNode *N) {
777 assert(N && "Can't insert a null Value into SlotTracker!");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000778
Chris Lattner2b4b1e22009-12-31 02:27:30 +0000779 // Don't insert if N is a function-local metadata, these are always printed
780 // inline.
Victor Hernandez5d301622009-12-18 20:09:14 +0000781 if (N->isFunctionLocal())
782 return;
Victor Hernandezff7707e2009-12-04 20:07:10 +0000783
Chris Lattner307c9892009-12-31 02:20:11 +0000784 mdn_iterator I = mdnMap.find(N);
Devang Patel320671d2009-07-08 21:44:25 +0000785 if (I != mdnMap.end())
786 return;
Chris Lattner6ab910b2008-08-19 04:36:02 +0000787
Devang Patel320671d2009-07-08 21:44:25 +0000788 unsigned DestSlot = mdnNext++;
789 mdnMap[N] = DestSlot;
790
Chris Lattner2b4b1e22009-12-31 02:27:30 +0000791 // Recursively add any MDNodes referenced by operands.
792 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
793 if (const MDNode *Op = dyn_cast_or_null<MDNode>(N->getOperand(i)))
794 CreateMetadataSlot(Op);
Devang Patel320671d2009-07-08 21:44:25 +0000795}
Chris Lattner6ab910b2008-08-19 04:36:02 +0000796
797//===----------------------------------------------------------------------===//
798// AsmWriter Implementation
799//===----------------------------------------------------------------------===//
Chris Lattnerf082b802002-07-23 18:07:49 +0000800
Dan Gohman1220e102009-08-12 20:56:03 +0000801static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
Dan Gohmand6c0f652009-08-13 15:27:57 +0000802 TypePrinting *TypePrinter,
Chris Lattner0d9574a2008-08-19 04:26:57 +0000803 SlotTracker *Machine);
Reid Spencer0e25e1c2004-07-04 11:50:43 +0000804
Chris Lattnerc97536e2008-08-17 04:40:13 +0000805
Chris Lattner207b5bc2001-10-29 16:37:48 +0000806
Chris Lattner82c4bc72006-12-06 06:40:49 +0000807static const char *getPredicateText(unsigned predicate) {
Reid Spencer81dfeb32006-12-04 05:19:18 +0000808 const char * pred = "unknown";
809 switch (predicate) {
Chris Lattner6e6b1802009-12-31 02:13:35 +0000810 case FCmpInst::FCMP_FALSE: pred = "false"; break;
811 case FCmpInst::FCMP_OEQ: pred = "oeq"; break;
812 case FCmpInst::FCMP_OGT: pred = "ogt"; break;
813 case FCmpInst::FCMP_OGE: pred = "oge"; break;
814 case FCmpInst::FCMP_OLT: pred = "olt"; break;
815 case FCmpInst::FCMP_OLE: pred = "ole"; break;
816 case FCmpInst::FCMP_ONE: pred = "one"; break;
817 case FCmpInst::FCMP_ORD: pred = "ord"; break;
818 case FCmpInst::FCMP_UNO: pred = "uno"; break;
819 case FCmpInst::FCMP_UEQ: pred = "ueq"; break;
820 case FCmpInst::FCMP_UGT: pred = "ugt"; break;
821 case FCmpInst::FCMP_UGE: pred = "uge"; break;
822 case FCmpInst::FCMP_ULT: pred = "ult"; break;
823 case FCmpInst::FCMP_ULE: pred = "ule"; break;
824 case FCmpInst::FCMP_UNE: pred = "une"; break;
825 case FCmpInst::FCMP_TRUE: pred = "true"; break;
826 case ICmpInst::ICMP_EQ: pred = "eq"; break;
827 case ICmpInst::ICMP_NE: pred = "ne"; break;
828 case ICmpInst::ICMP_SGT: pred = "sgt"; break;
829 case ICmpInst::ICMP_SGE: pred = "sge"; break;
830 case ICmpInst::ICMP_SLT: pred = "slt"; break;
831 case ICmpInst::ICMP_SLE: pred = "sle"; break;
832 case ICmpInst::ICMP_UGT: pred = "ugt"; break;
833 case ICmpInst::ICMP_UGE: pred = "uge"; break;
834 case ICmpInst::ICMP_ULT: pred = "ult"; break;
835 case ICmpInst::ICMP_ULE: pred = "ule"; break;
Reid Spencer81dfeb32006-12-04 05:19:18 +0000836 }
837 return pred;
838}
839
Devang Patel320671d2009-07-08 21:44:25 +0000840
Dan Gohman1220e102009-08-12 20:56:03 +0000841static void WriteOptimizationInfo(raw_ostream &Out, const User *U) {
Dan Gohman1224c382009-07-20 21:19:07 +0000842 if (const OverflowingBinaryOperator *OBO =
843 dyn_cast<OverflowingBinaryOperator>(U)) {
Dan Gohman5078f842009-08-20 17:11:38 +0000844 if (OBO->hasNoUnsignedWrap())
Dan Gohman59858cf2009-07-27 16:11:46 +0000845 Out << " nuw";
Dan Gohman5078f842009-08-20 17:11:38 +0000846 if (OBO->hasNoSignedWrap())
Dan Gohman59858cf2009-07-27 16:11:46 +0000847 Out << " nsw";
Dan Gohman1224c382009-07-20 21:19:07 +0000848 } else if (const SDivOperator *Div = dyn_cast<SDivOperator>(U)) {
849 if (Div->isExact())
Dan Gohman59858cf2009-07-27 16:11:46 +0000850 Out << " exact";
Dan Gohmandd8004d2009-07-27 21:53:46 +0000851 } else if (const GEPOperator *GEP = dyn_cast<GEPOperator>(U)) {
852 if (GEP->isInBounds())
853 Out << " inbounds";
Dan Gohman1224c382009-07-20 21:19:07 +0000854 }
855}
856
Dan Gohman40cf12f2010-07-14 20:57:55 +0000857static void WriteConstantInternal(raw_ostream &Out, const Constant *CV,
858 TypePrinting &TypePrinter,
859 SlotTracker *Machine) {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +0000860 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000861 if (CI->getType()->isIntegerTy(1)) {
Reid Spencer579dca12007-01-12 04:24:46 +0000862 Out << (CI->getZExtValue() ? "true" : "false");
Chris Lattnerfad86b02008-08-17 07:19:36 +0000863 return;
864 }
865 Out << CI->getValue();
866 return;
867 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000868
Chris Lattnerfad86b02008-08-17 07:19:36 +0000869 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000870 if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEdouble ||
871 &CFP->getValueAPF().getSemantics() == &APFloat::IEEEsingle) {
872 // We would like to output the FP constant value in exponential notation,
873 // but we cannot do this if doing so will lose precision. Check here to
874 // make sure that we only output it in exponential format if we can parse
875 // the value back and get the same value.
876 //
Dale Johannesen541ed9f2009-01-21 20:32:55 +0000877 bool ignored;
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000878 bool isDouble = &CFP->getValueAPF().getSemantics()==&APFloat::IEEEdouble;
Chris Lattnerfad86b02008-08-17 07:19:36 +0000879 double Val = isDouble ? CFP->getValueAPF().convertToDouble() :
880 CFP->getValueAPF().convertToFloat();
Benjamin Kramer59088392010-01-29 14:42:22 +0000881 SmallString<128> StrVal;
882 raw_svector_ostream(StrVal) << Val;
Chris Lattner66e810b2002-04-18 18:53:13 +0000883
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000884 // Check to make sure that the stringized number is not some string like
885 // "Inf" or NaN, that atof will accept, but the lexer will not. Check
886 // that the string matches the "[-+]?[0-9]" regex.
887 //
888 if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
889 ((StrVal[0] == '-' || StrVal[0] == '+') &&
890 (StrVal[1] >= '0' && StrVal[1] <= '9'))) {
891 // Reparse stringized version!
892 if (atof(StrVal.c_str()) == Val) {
Benjamin Kramer59088392010-01-29 14:42:22 +0000893 Out << StrVal.str();
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000894 return;
895 }
Chris Lattner66e810b2002-04-18 18:53:13 +0000896 }
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000897 // Otherwise we could not reparse it to exactly the same value, so we must
Dale Johannesen541ed9f2009-01-21 20:32:55 +0000898 // output the string in hexadecimal format! Note that loading and storing
899 // floating point types changes the bits of NaNs on some hosts, notably
900 // x86, so we must not use these types.
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000901 assert(sizeof(double) == sizeof(uint64_t) &&
902 "assuming that double is 64 bits!");
Chris Lattnerc6a13462008-11-10 04:30:26 +0000903 char Buffer[40];
Dale Johannesen541ed9f2009-01-21 20:32:55 +0000904 APFloat apf = CFP->getValueAPF();
905 // Floats are represented in ASCII IR as double, convert.
906 if (!isDouble)
Daniel Dunbara279bc32009-09-20 02:20:51 +0000907 apf.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven,
Dale Johannesen541ed9f2009-01-21 20:32:55 +0000908 &ignored);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000909 Out << "0x" <<
910 utohex_buffer(uint64_t(apf.bitcastToAPInt().getZExtValue()),
Dale Johannesen541ed9f2009-01-21 20:32:55 +0000911 Buffer+40);
Chris Lattnercfb5a202008-08-19 05:06:27 +0000912 return;
913 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000914
Chris Lattnercfb5a202008-08-19 05:06:27 +0000915 // Some form of long double. These appear as a magic letter identifying
916 // the type, then a fixed number of hex digits.
917 Out << "0x";
Dale Johannesen1b25cb22009-03-23 21:16:53 +0000918 if (&CFP->getValueAPF().getSemantics() == &APFloat::x87DoubleExtended) {
Chris Lattnercfb5a202008-08-19 05:06:27 +0000919 Out << 'K';
Dale Johannesen1b25cb22009-03-23 21:16:53 +0000920 // api needed to prevent premature destruction
921 APInt api = CFP->getValueAPF().bitcastToAPInt();
922 const uint64_t* p = api.getRawData();
923 uint64_t word = p[1];
924 int shiftcount=12;
925 int width = api.getBitWidth();
926 for (int j=0; j<width; j+=4, shiftcount-=4) {
927 unsigned int nibble = (word>>shiftcount) & 15;
928 if (nibble < 10)
929 Out << (unsigned char)(nibble + '0');
930 else
931 Out << (unsigned char)(nibble - 10 + 'A');
932 if (shiftcount == 0 && j+4 < width) {
933 word = *p;
934 shiftcount = 64;
935 if (width-j-4 < 64)
936 shiftcount = width-j-4;
937 }
938 }
939 return;
940 } else if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEquad)
Chris Lattnercfb5a202008-08-19 05:06:27 +0000941 Out << 'L';
942 else if (&CFP->getValueAPF().getSemantics() == &APFloat::PPCDoubleDouble)
943 Out << 'M';
944 else
Torok Edwinc23197a2009-07-14 16:55:14 +0000945 llvm_unreachable("Unsupported floating point type");
Chris Lattnercfb5a202008-08-19 05:06:27 +0000946 // api needed to prevent premature destruction
Dale Johannesen7111b022008-10-09 18:53:47 +0000947 APInt api = CFP->getValueAPF().bitcastToAPInt();
Chris Lattnercfb5a202008-08-19 05:06:27 +0000948 const uint64_t* p = api.getRawData();
949 uint64_t word = *p;
950 int shiftcount=60;
951 int width = api.getBitWidth();
952 for (int j=0; j<width; j+=4, shiftcount-=4) {
953 unsigned int nibble = (word>>shiftcount) & 15;
954 if (nibble < 10)
955 Out << (unsigned char)(nibble + '0');
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000956 else
Chris Lattnercfb5a202008-08-19 05:06:27 +0000957 Out << (unsigned char)(nibble - 10 + 'A');
958 if (shiftcount == 0 && j+4 < width) {
959 word = *(++p);
960 shiftcount = 64;
961 if (width-j-4 < 64)
962 shiftcount = width-j-4;
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000963 }
964 }
Chris Lattnercfb5a202008-08-19 05:06:27 +0000965 return;
966 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000967
Chris Lattnercfb5a202008-08-19 05:06:27 +0000968 if (isa<ConstantAggregateZero>(CV)) {
Chris Lattnerde512b52004-02-15 05:55:15 +0000969 Out << "zeroinitializer";
Chris Lattnercfb5a202008-08-19 05:06:27 +0000970 return;
971 }
Chris Lattner73050e12009-10-28 03:38:12 +0000972
973 if (const BlockAddress *BA = dyn_cast<BlockAddress>(CV)) {
974 Out << "blockaddress(";
975 WriteAsOperandInternal(Out, BA->getFunction(), &TypePrinter, Machine);
976 Out << ", ";
Chris Lattnercdfc9402009-11-01 01:27:45 +0000977 WriteAsOperandInternal(Out, BA->getBasicBlock(), &TypePrinter, Machine);
Chris Lattner73050e12009-10-28 03:38:12 +0000978 Out << ")";
979 return;
980 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000981
Chris Lattnercfb5a202008-08-19 05:06:27 +0000982 if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
Chris Lattner66e810b2002-04-18 18:53:13 +0000983 // As a special case, print the array as a string if it is an array of
Dan Gohman9b38d7d2008-05-12 16:34:30 +0000984 // i8 with ConstantInt values.
Misha Brukmanfd939082005-04-21 23:48:37 +0000985 //
Chris Lattner66e810b2002-04-18 18:53:13 +0000986 const Type *ETy = CA->getType()->getElementType();
Chris Lattner18365502006-01-23 23:03:36 +0000987 if (CA->isString()) {
Chris Lattner66e810b2002-04-18 18:53:13 +0000988 Out << "c\"";
Chris Lattner18365502006-01-23 23:03:36 +0000989 PrintEscapedString(CA->getAsString(), Out);
Chris Lattnercfb5a202008-08-19 05:06:27 +0000990 Out << '"';
Chris Lattner66e810b2002-04-18 18:53:13 +0000991 } else { // Cannot output in string format...
Misha Brukman40c732c2004-06-04 21:11:51 +0000992 Out << '[';
Chris Lattner7a716ad2002-04-16 21:36:08 +0000993 if (CA->getNumOperands()) {
Chris Lattner0f7364b2009-02-28 21:26:53 +0000994 TypePrinter.print(ETy, Out);
Dan Gohman8dae1382008-09-14 17:21:12 +0000995 Out << ' ';
Chris Lattner7a716ad2002-04-16 21:36:08 +0000996 WriteAsOperandInternal(Out, CA->getOperand(0),
Dan Gohmand6c0f652009-08-13 15:27:57 +0000997 &TypePrinter, Machine);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000998 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
999 Out << ", ";
Chris Lattner0f7364b2009-02-28 21:26:53 +00001000 TypePrinter.print(ETy, Out);
Dan Gohman8dae1382008-09-14 17:21:12 +00001001 Out << ' ';
Dan Gohmand6c0f652009-08-13 15:27:57 +00001002 WriteAsOperandInternal(Out, CA->getOperand(i), &TypePrinter, Machine);
Chris Lattner7a716ad2002-04-16 21:36:08 +00001003 }
1004 }
Dan Gohman8dae1382008-09-14 17:21:12 +00001005 Out << ']';
Chris Lattner7a716ad2002-04-16 21:36:08 +00001006 }
Chris Lattnercfb5a202008-08-19 05:06:27 +00001007 return;
1008 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001009
Chris Lattnercfb5a202008-08-19 05:06:27 +00001010 if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
Andrew Lenharth43f344a2007-01-08 18:21:30 +00001011 if (CS->getType()->isPacked())
1012 Out << '<';
Misha Brukman40c732c2004-06-04 21:11:51 +00001013 Out << '{';
Jim Laskeya3f332b2006-02-25 12:27:03 +00001014 unsigned N = CS->getNumOperands();
1015 if (N) {
Chris Lattner24233032008-08-19 04:47:09 +00001016 Out << ' ';
Chris Lattner0f7364b2009-02-28 21:26:53 +00001017 TypePrinter.print(CS->getOperand(0)->getType(), Out);
Dan Gohman8dae1382008-09-14 17:21:12 +00001018 Out << ' ';
Chris Lattner7a716ad2002-04-16 21:36:08 +00001019
Dan Gohmand6c0f652009-08-13 15:27:57 +00001020 WriteAsOperandInternal(Out, CS->getOperand(0), &TypePrinter, Machine);
Chris Lattner7a716ad2002-04-16 21:36:08 +00001021
Jim Laskeya3f332b2006-02-25 12:27:03 +00001022 for (unsigned i = 1; i < N; i++) {
Chris Lattner7a716ad2002-04-16 21:36:08 +00001023 Out << ", ";
Chris Lattner0f7364b2009-02-28 21:26:53 +00001024 TypePrinter.print(CS->getOperand(i)->getType(), Out);
Dan Gohman8dae1382008-09-14 17:21:12 +00001025 Out << ' ';
Chris Lattner7a716ad2002-04-16 21:36:08 +00001026
Dan Gohmand6c0f652009-08-13 15:27:57 +00001027 WriteAsOperandInternal(Out, CS->getOperand(i), &TypePrinter, Machine);
Chris Lattner7a716ad2002-04-16 21:36:08 +00001028 }
Dan Gohman8dae1382008-09-14 17:21:12 +00001029 Out << ' ';
Chris Lattner7a716ad2002-04-16 21:36:08 +00001030 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001031
Dan Gohman8dae1382008-09-14 17:21:12 +00001032 Out << '}';
Andrew Lenharth43f344a2007-01-08 18:21:30 +00001033 if (CS->getType()->isPacked())
1034 Out << '>';
Chris Lattnercfb5a202008-08-19 05:06:27 +00001035 return;
1036 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001037
Chris Lattner94c484d2010-03-16 21:21:35 +00001038 if (const ConstantUnion *CU = dyn_cast<ConstantUnion>(CV)) {
1039 Out << "{ ";
1040 TypePrinter.print(CU->getOperand(0)->getType(), Out);
1041 Out << ' ';
1042 WriteAsOperandInternal(Out, CU->getOperand(0), &TypePrinter, Machine);
1043 Out << " }";
1044 return;
1045 }
1046
Chris Lattnercfb5a202008-08-19 05:06:27 +00001047 if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
1048 const Type *ETy = CP->getType()->getElementType();
1049 assert(CP->getNumOperands() > 0 &&
1050 "Number of operands for a PackedConst must be > 0");
Dan Gohman7dfa07f2009-02-11 00:25:25 +00001051 Out << '<';
Chris Lattner0f7364b2009-02-28 21:26:53 +00001052 TypePrinter.print(ETy, Out);
Dan Gohman8dae1382008-09-14 17:21:12 +00001053 Out << ' ';
Dan Gohmand6c0f652009-08-13 15:27:57 +00001054 WriteAsOperandInternal(Out, CP->getOperand(0), &TypePrinter, Machine);
Chris Lattnercfb5a202008-08-19 05:06:27 +00001055 for (unsigned i = 1, e = CP->getNumOperands(); i != e; ++i) {
Chris Lattner4667b712008-08-19 05:26:17 +00001056 Out << ", ";
Chris Lattner0f7364b2009-02-28 21:26:53 +00001057 TypePrinter.print(ETy, Out);
Dan Gohman8dae1382008-09-14 17:21:12 +00001058 Out << ' ';
Dan Gohmand6c0f652009-08-13 15:27:57 +00001059 WriteAsOperandInternal(Out, CP->getOperand(i), &TypePrinter, Machine);
Chris Lattnercfb5a202008-08-19 05:06:27 +00001060 }
Dan Gohman7dfa07f2009-02-11 00:25:25 +00001061 Out << '>';
Chris Lattnercfb5a202008-08-19 05:06:27 +00001062 return;
1063 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001064
Chris Lattnercfb5a202008-08-19 05:06:27 +00001065 if (isa<ConstantPointerNull>(CV)) {
Chris Lattner7a716ad2002-04-16 21:36:08 +00001066 Out << "null";
Chris Lattnercfb5a202008-08-19 05:06:27 +00001067 return;
1068 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001069
Chris Lattnercfb5a202008-08-19 05:06:27 +00001070 if (isa<UndefValue>(CV)) {
Chris Lattnerb976e662004-10-16 18:08:06 +00001071 Out << "undef";
Chris Lattnercfb5a202008-08-19 05:06:27 +00001072 return;
1073 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001074
Devang Patel320671d2009-07-08 21:44:25 +00001075 if (const MDNode *Node = dyn_cast<MDNode>(CV)) {
1076 Out << "!" << Machine->getMetadataSlot(Node);
1077 return;
1078 }
1079
Chris Lattnercfb5a202008-08-19 05:06:27 +00001080 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Reid Spencer81dfeb32006-12-04 05:19:18 +00001081 Out << CE->getOpcodeName();
Dan Gohman59858cf2009-07-27 16:11:46 +00001082 WriteOptimizationInfo(Out, CE);
Reid Spencer81dfeb32006-12-04 05:19:18 +00001083 if (CE->isCompare())
Chris Lattnercfb5a202008-08-19 05:06:27 +00001084 Out << ' ' << getPredicateText(CE->getPredicate());
Reid Spencer81dfeb32006-12-04 05:19:18 +00001085 Out << " (";
Misha Brukmanfd939082005-04-21 23:48:37 +00001086
Vikram S. Adveb4dbb442002-07-14 23:14:45 +00001087 for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) {
Chris Lattner0f7364b2009-02-28 21:26:53 +00001088 TypePrinter.print((*OI)->getType(), Out);
Dan Gohman8dae1382008-09-14 17:21:12 +00001089 Out << ' ';
Dan Gohmand6c0f652009-08-13 15:27:57 +00001090 WriteAsOperandInternal(Out, *OI, &TypePrinter, Machine);
Vikram S. Adveb4dbb442002-07-14 23:14:45 +00001091 if (OI+1 != CE->op_end())
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001092 Out << ", ";
Vikram S. Adveb4dbb442002-07-14 23:14:45 +00001093 }
Misha Brukmanfd939082005-04-21 23:48:37 +00001094
Dan Gohman995be7d2008-05-31 19:12:39 +00001095 if (CE->hasIndices()) {
1096 const SmallVector<unsigned, 4> &Indices = CE->getIndices();
1097 for (unsigned i = 0, e = Indices.size(); i != e; ++i)
1098 Out << ", " << Indices[i];
1099 }
1100
Reid Spencer3da59db2006-11-27 01:05:10 +00001101 if (CE->isCast()) {
Chris Lattner95586b82002-08-15 19:37:43 +00001102 Out << " to ";
Chris Lattner0f7364b2009-02-28 21:26:53 +00001103 TypePrinter.print(CE->getType(), Out);
Chris Lattner95586b82002-08-15 19:37:43 +00001104 }
Reid Spencer3da59db2006-11-27 01:05:10 +00001105
Misha Brukman40c732c2004-06-04 21:11:51 +00001106 Out << ')';
Chris Lattnercfb5a202008-08-19 05:06:27 +00001107 return;
Chris Lattner7a716ad2002-04-16 21:36:08 +00001108 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001109
Chris Lattnercfb5a202008-08-19 05:06:27 +00001110 Out << "<placeholder or erroneous Constant>";
Chris Lattner7a716ad2002-04-16 21:36:08 +00001111}
1112
Chris Lattner85b19122009-12-31 02:31:59 +00001113static void WriteMDNodeBodyInternal(raw_ostream &Out, const MDNode *Node,
1114 TypePrinting *TypePrinter,
1115 SlotTracker *Machine) {
1116 Out << "!{";
1117 for (unsigned mi = 0, me = Node->getNumOperands(); mi != me; ++mi) {
1118 const Value *V = Node->getOperand(mi);
1119 if (V == 0)
1120 Out << "null";
1121 else {
1122 TypePrinter->print(V->getType(), Out);
1123 Out << ' ';
1124 WriteAsOperandInternal(Out, Node->getOperand(mi),
1125 TypePrinter, Machine);
1126 }
1127 if (mi + 1 != me)
1128 Out << ", ";
1129 }
1130
1131 Out << "}";
1132}
1133
Chris Lattner7a716ad2002-04-16 21:36:08 +00001134
Misha Brukmanab5c6002004-03-02 00:22:19 +00001135/// WriteAsOperand - Write the name of the specified value out to the specified
1136/// ostream. This can be useful when you just want to print int %reg126, not
1137/// the whole instruction that generated it.
1138///
Dan Gohman1220e102009-08-12 20:56:03 +00001139static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
Dan Gohmand6c0f652009-08-13 15:27:57 +00001140 TypePrinting *TypePrinter,
Chris Lattner0d9574a2008-08-19 04:26:57 +00001141 SlotTracker *Machine) {
Chris Lattnerc97536e2008-08-17 04:40:13 +00001142 if (V->hasName()) {
1143 PrintLLVMName(Out, V);
1144 return;
1145 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001146
Chris Lattnerc97536e2008-08-17 04:40:13 +00001147 const Constant *CV = dyn_cast<Constant>(V);
1148 if (CV && !isa<GlobalValue>(CV)) {
Dan Gohmand6c0f652009-08-13 15:27:57 +00001149 assert(TypePrinter && "Constants require TypePrinting!");
Dan Gohman40cf12f2010-07-14 20:57:55 +00001150 WriteConstantInternal(Out, CV, *TypePrinter, Machine);
Chris Lattnercfb5a202008-08-19 05:06:27 +00001151 return;
1152 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001153
Chris Lattnercfb5a202008-08-19 05:06:27 +00001154 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
Chris Lattnerc97536e2008-08-17 04:40:13 +00001155 Out << "asm ";
1156 if (IA->hasSideEffects())
1157 Out << "sideeffect ";
Dale Johannesen8ba2d5b2009-10-21 23:28:00 +00001158 if (IA->isAlignStack())
1159 Out << "alignstack ";
Chris Lattnerc97536e2008-08-17 04:40:13 +00001160 Out << '"';
1161 PrintEscapedString(IA->getAsmString(), Out);
1162 Out << "\", \"";
1163 PrintEscapedString(IA->getConstraintString(), Out);
1164 Out << '"';
Chris Lattnercfb5a202008-08-19 05:06:27 +00001165 return;
1166 }
Devang Patele54abc92009-07-22 17:43:22 +00001167
Devang Patel104cf9e2009-07-23 01:07:34 +00001168 if (const MDNode *N = dyn_cast<MDNode>(V)) {
Victor Hernandez5d301622009-12-18 20:09:14 +00001169 if (N->isFunctionLocal()) {
Victor Hernandez97e24502009-12-04 01:35:02 +00001170 // Print metadata inline, not via slot reference number.
Chris Lattner85b19122009-12-31 02:31:59 +00001171 WriteMDNodeBodyInternal(Out, N, TypePrinter, Machine);
Victor Hernandez97e24502009-12-04 01:35:02 +00001172 return;
1173 }
1174
Dale Johannesen5f72a5e2010-01-13 00:00:24 +00001175 if (!Machine)
1176 Machine = createSlotTracker(V);
Devang Patel104cf9e2009-07-23 01:07:34 +00001177 Out << '!' << Machine->getMetadataSlot(N);
1178 return;
1179 }
1180
Devang Patele54abc92009-07-22 17:43:22 +00001181 if (const MDString *MDS = dyn_cast<MDString>(V)) {
Devang Patele54abc92009-07-22 17:43:22 +00001182 Out << "!\"";
Daniel Dunbar03d76512009-07-25 23:55:21 +00001183 PrintEscapedString(MDS->getString(), Out);
Devang Patele54abc92009-07-22 17:43:22 +00001184 Out << '"';
1185 return;
1186 }
1187
Evan Cheng746d5462009-11-16 07:10:36 +00001188 if (V->getValueID() == Value::PseudoSourceValueVal ||
1189 V->getValueID() == Value::FixedStackPseudoSourceValueVal) {
Dan Gohmancd26ec52009-09-23 01:33:16 +00001190 V->print(Out);
1191 return;
1192 }
1193
Chris Lattnercfb5a202008-08-19 05:06:27 +00001194 char Prefix = '%';
1195 int Slot;
1196 if (Machine) {
1197 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1198 Slot = Machine->getGlobalSlot(GV);
1199 Prefix = '@';
1200 } else {
1201 Slot = Machine->getLocalSlot(V);
1202 }
Chris Lattnerc97536e2008-08-17 04:40:13 +00001203 } else {
Chris Lattnercfb5a202008-08-19 05:06:27 +00001204 Machine = createSlotTracker(V);
Chris Lattnerc97536e2008-08-17 04:40:13 +00001205 if (Machine) {
1206 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1207 Slot = Machine->getGlobalSlot(GV);
1208 Prefix = '@';
1209 } else {
1210 Slot = Machine->getLocalSlot(V);
1211 }
Dan Gohmand6c0f652009-08-13 15:27:57 +00001212 delete Machine;
Chris Lattner80cd1152006-01-25 22:26:05 +00001213 } else {
Chris Lattnercfb5a202008-08-19 05:06:27 +00001214 Slot = -1;
Chris Lattner7a716ad2002-04-16 21:36:08 +00001215 }
1216 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001217
Chris Lattnercfb5a202008-08-19 05:06:27 +00001218 if (Slot != -1)
1219 Out << Prefix << Slot;
1220 else
1221 Out << "<badref>";
Chris Lattner7a716ad2002-04-16 21:36:08 +00001222}
1223
Dan Gohman1220e102009-08-12 20:56:03 +00001224void llvm::WriteAsOperand(raw_ostream &Out, const Value *V,
1225 bool PrintType, const Module *Context) {
Dan Gohmand6c0f652009-08-13 15:27:57 +00001226
1227 // Fast path: Don't construct and populate a TypePrinting object if we
1228 // won't be needing any types printed.
Dan Gohman009fc9e2009-08-13 23:07:11 +00001229 if (!PrintType &&
1230 (!isa<Constant>(V) || V->hasName() || isa<GlobalValue>(V))) {
Dan Gohmand6c0f652009-08-13 15:27:57 +00001231 WriteAsOperandInternal(Out, V, 0, 0);
1232 return;
1233 }
1234
Chris Lattner607dc682002-07-10 16:48:17 +00001235 if (Context == 0) Context = getModuleFromVal(V);
Chris Lattner207b5bc2001-10-29 16:37:48 +00001236
Chris Lattnere9fa33e2009-02-28 23:20:19 +00001237 TypePrinting TypePrinter;
Chris Lattner413fd232009-03-01 00:03:38 +00001238 std::vector<const Type*> NumberedTypes;
1239 AddModuleTypesToPrinter(TypePrinter, NumberedTypes, Context);
Dan Gohman8dae1382008-09-14 17:21:12 +00001240 if (PrintType) {
Chris Lattner0f7364b2009-02-28 21:26:53 +00001241 TypePrinter.print(V->getType(), Out);
Dan Gohman8dae1382008-09-14 17:21:12 +00001242 Out << ' ';
1243 }
Misha Brukmanfd939082005-04-21 23:48:37 +00001244
Dan Gohmand6c0f652009-08-13 15:27:57 +00001245 WriteAsOperandInternal(Out, V, &TypePrinter, 0);
Chris Lattner622f7402001-07-20 19:15:21 +00001246}
1247
Chris Lattnercfb5a202008-08-19 05:06:27 +00001248namespace {
Chris Lattnerd8c2e422001-07-12 23:35:26 +00001249
Chris Lattner007377f2001-09-07 16:36:04 +00001250class AssemblyWriter {
Dan Gohman683e9222009-08-12 17:23:50 +00001251 formatted_raw_ostream &Out;
Chris Lattner0d9574a2008-08-19 04:26:57 +00001252 SlotTracker &Machine;
Chris Lattnerc1824992001-10-29 16:05:51 +00001253 const Module *TheModule;
Chris Lattner9cc34462009-02-28 20:25:14 +00001254 TypePrinting TypePrinter;
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001255 AssemblyAnnotationWriter *AnnotationWriter;
Chris Lattner413fd232009-03-01 00:03:38 +00001256 std::vector<const Type*> NumberedTypes;
Chris Lattner7d05c462009-12-28 20:10:43 +00001257
Chris Lattner00950542001-06-06 20:29:01 +00001258public:
Dan Gohman683e9222009-08-12 17:23:50 +00001259 inline AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac,
1260 const Module *M,
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001261 AssemblyAnnotationWriter *AAW)
Devang Patel3168b792009-09-28 18:31:56 +00001262 : Out(o), Machine(Mac), TheModule(M), AnnotationWriter(AAW) {
Chris Lattner413fd232009-03-01 00:03:38 +00001263 AddModuleTypesToPrinter(TypePrinter, NumberedTypes, M);
Chris Lattner00950542001-06-06 20:29:01 +00001264 }
1265
Chris Lattner6e6b1802009-12-31 02:13:35 +00001266 void printMDNodeBody(const MDNode *MD);
Chris Lattnerfdb33562009-12-31 01:54:05 +00001267 void printNamedMDNode(const NamedMDNode *NMD);
1268
Chris Lattnerbd72b322009-12-31 02:23:35 +00001269 void printModule(const Module *M);
Chris Lattner00950542001-06-06 20:29:01 +00001270
Chris Lattner2fcfdb72006-12-06 06:24:27 +00001271 void writeOperand(const Value *Op, bool PrintType);
Devang Pateleaf42ab2008-09-23 23:03:40 +00001272 void writeParamOperand(const Value *Operand, Attributes Attrs);
Chris Lattner66e810b2002-04-18 18:53:13 +00001273
Chris Lattner6e6b1802009-12-31 02:13:35 +00001274 void writeAllMDNodes();
1275
Reid Spencer78d033e2007-01-06 07:24:44 +00001276 void printTypeSymbolTable(const TypeSymbolTable &ST);
Chris Lattnerc1824992001-10-29 16:05:51 +00001277 void printGlobal(const GlobalVariable *GV);
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001278 void printAlias(const GlobalAlias *GV);
Chris Lattner79df7c02002-03-26 18:01:55 +00001279 void printFunction(const Function *F);
Devang Pateleaf42ab2008-09-23 23:03:40 +00001280 void printArgument(const Argument *FA, Attributes Attrs);
Chris Lattnerc1824992001-10-29 16:05:51 +00001281 void printBasicBlock(const BasicBlock *BB);
Chris Lattner7e708292002-06-25 16:13:24 +00001282 void printInstruction(const Instruction &I);
Chris Lattner2761e9f2002-04-13 20:53:41 +00001283
Dan Gohman659d1e82010-02-10 20:42:57 +00001284private:
Chris Lattnere02fa852001-10-13 06:42:36 +00001285 // printInfoComment - Print a little comment after the instruction indicating
1286 // which slot it occupies.
Chris Lattner7e708292002-06-25 16:13:24 +00001287 void printInfoComment(const Value &V);
Chris Lattner00950542001-06-06 20:29:01 +00001288};
Chris Lattner413fd232009-03-01 00:03:38 +00001289} // end of anonymous namespace
Chris Lattner00950542001-06-06 20:29:01 +00001290
Chris Lattner2fcfdb72006-12-06 06:24:27 +00001291void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType) {
1292 if (Operand == 0) {
Chris Lattneraab18202005-02-24 16:58:29 +00001293 Out << "<null operand!>";
Chris Lattnerc65b72c2009-12-31 02:33:14 +00001294 return;
Chris Lattneraab18202005-02-24 16:58:29 +00001295 }
Chris Lattnerc65b72c2009-12-31 02:33:14 +00001296 if (PrintType) {
1297 TypePrinter.print(Operand->getType(), Out);
1298 Out << ' ';
1299 }
1300 WriteAsOperandInternal(Out, Operand, &TypePrinter, &Machine);
Chris Lattner00950542001-06-06 20:29:01 +00001301}
1302
Daniel Dunbara279bc32009-09-20 02:20:51 +00001303void AssemblyWriter::writeParamOperand(const Value *Operand,
Devang Pateleaf42ab2008-09-23 23:03:40 +00001304 Attributes Attrs) {
Duncan Sandsdc024672007-11-27 13:23:08 +00001305 if (Operand == 0) {
1306 Out << "<null operand!>";
Chris Lattnerc65b72c2009-12-31 02:33:14 +00001307 return;
Duncan Sandsdc024672007-11-27 13:23:08 +00001308 }
Chris Lattnerc65b72c2009-12-31 02:33:14 +00001309
1310 // Print the type
1311 TypePrinter.print(Operand->getType(), Out);
1312 // Print parameter attributes list
1313 if (Attrs != Attribute::None)
1314 Out << ' ' << Attribute::getAsString(Attrs);
1315 Out << ' ';
1316 // Print the operand
1317 WriteAsOperandInternal(Out, Operand, &TypePrinter, &Machine);
Duncan Sandsdc024672007-11-27 13:23:08 +00001318}
Chris Lattner00950542001-06-06 20:29:01 +00001319
Chris Lattnerc1824992001-10-29 16:05:51 +00001320void AssemblyWriter::printModule(const Module *M) {
Chris Lattner31ab1b32005-03-02 23:12:40 +00001321 if (!M->getModuleIdentifier().empty() &&
Misha Brukmanfd939082005-04-21 23:48:37 +00001322 // Don't print the ID if it will start a new line (which would
Chris Lattner31ab1b32005-03-02 23:12:40 +00001323 // require a comment char before it).
1324 M->getModuleIdentifier().find('\n') == std::string::npos)
1325 Out << "; ModuleID = '" << M->getModuleIdentifier() << "'\n";
1326
Owen Andersoncf7ff2b2006-10-18 02:21:12 +00001327 if (!M->getDataLayout().empty())
Chris Lattnerd2f9e602006-10-22 06:06:56 +00001328 Out << "target datalayout = \"" << M->getDataLayout() << "\"\n";
Reid Spencercddc86f2004-07-25 21:44:54 +00001329 if (!M->getTargetTriple().empty())
Reid Spencerc9a1f0d2004-07-25 21:29:43 +00001330 Out << "target triple = \"" << M->getTargetTriple() << "\"\n";
Misha Brukmanfd939082005-04-21 23:48:37 +00001331
Chris Lattnercc041ba2006-01-24 04:13:11 +00001332 if (!M->getModuleInlineAsm().empty()) {
Chris Lattner42a162e2006-01-24 00:45:30 +00001333 // Split the string into lines, to make it easier to read the .ll file.
Chris Lattnercc041ba2006-01-24 04:13:11 +00001334 std::string Asm = M->getModuleInlineAsm();
Chris Lattner42a162e2006-01-24 00:45:30 +00001335 size_t CurPos = 0;
1336 size_t NewLine = Asm.find_first_of('\n', CurPos);
Dan Gohman9bf0b9b2009-08-12 23:54:22 +00001337 Out << '\n';
Chris Lattner42a162e2006-01-24 00:45:30 +00001338 while (NewLine != std::string::npos) {
1339 // We found a newline, print the portion of the asm string from the
1340 // last newline up to this newline.
1341 Out << "module asm \"";
1342 PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.begin()+NewLine),
1343 Out);
1344 Out << "\"\n";
1345 CurPos = NewLine+1;
1346 NewLine = Asm.find_first_of('\n', CurPos);
1347 }
Chris Lattner71cdba32006-01-24 00:40:17 +00001348 Out << "module asm \"";
Chris Lattner42a162e2006-01-24 00:45:30 +00001349 PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.end()), Out);
Chris Lattner18365502006-01-23 23:03:36 +00001350 Out << "\"\n";
1351 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001352
Chris Lattner44da7d72004-09-14 05:06:58 +00001353 // Loop over the dependent libraries and emit them.
Chris Lattnercfe97b72004-09-14 04:51:44 +00001354 Module::lib_iterator LI = M->lib_begin();
1355 Module::lib_iterator LE = M->lib_end();
Reid Spencercddc86f2004-07-25 21:44:54 +00001356 if (LI != LE) {
Dan Gohman9bf0b9b2009-08-12 23:54:22 +00001357 Out << '\n';
Chris Lattnercfe97b72004-09-14 04:51:44 +00001358 Out << "deplibs = [ ";
1359 while (LI != LE) {
Chris Lattner44da7d72004-09-14 05:06:58 +00001360 Out << '"' << *LI << '"';
Reid Spencerc9a1f0d2004-07-25 21:29:43 +00001361 ++LI;
Chris Lattnercfe97b72004-09-14 04:51:44 +00001362 if (LI != LE)
1363 Out << ", ";
Reid Spencerc9a1f0d2004-07-25 21:29:43 +00001364 }
Dan Gohman9bf0b9b2009-08-12 23:54:22 +00001365 Out << " ]";
Reid Spencer83f6a772004-07-25 18:08:18 +00001366 }
Reid Spencere59eaf42004-09-13 23:44:23 +00001367
Chris Lattner413fd232009-03-01 00:03:38 +00001368 // Loop over the symbol table, emitting all id'd types.
Dan Gohman9bf0b9b2009-08-12 23:54:22 +00001369 if (!M->getTypeSymbolTable().empty() || !NumberedTypes.empty()) Out << '\n';
Reid Spencer78d033e2007-01-06 07:24:44 +00001370 printTypeSymbolTable(M->getTypeSymbolTable());
Misha Brukmanfd939082005-04-21 23:48:37 +00001371
Dan Gohman9bf0b9b2009-08-12 23:54:22 +00001372 // Output all globals.
1373 if (!M->global_empty()) Out << '\n';
Chris Lattnerd6d826c2006-12-06 04:41:52 +00001374 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
1375 I != E; ++I)
Chris Lattner7e708292002-06-25 16:13:24 +00001376 printGlobal(I);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001377
Chris Lattner69dacfc2007-04-26 02:24:10 +00001378 // Output all aliases.
1379 if (!M->alias_empty()) Out << "\n";
1380 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
1381 I != E; ++I)
1382 printAlias(I);
Chris Lattner007377f2001-09-07 16:36:04 +00001383
Chris Lattner44da7d72004-09-14 05:06:58 +00001384 // Output all of the functions.
Chris Lattner7e708292002-06-25 16:13:24 +00001385 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
1386 printFunction(I);
Devang Patel320671d2009-07-08 21:44:25 +00001387
Devang Patel37c4a2d2009-07-29 22:04:47 +00001388 // Output named metadata.
Dan Gohman9bf0b9b2009-08-12 23:54:22 +00001389 if (!M->named_metadata_empty()) Out << '\n';
Chris Lattnerfdb33562009-12-31 01:54:05 +00001390
Devang Patel37c4a2d2009-07-29 22:04:47 +00001391 for (Module::const_named_metadata_iterator I = M->named_metadata_begin(),
Chris Lattner6e6b1802009-12-31 02:13:35 +00001392 E = M->named_metadata_end(); I != E; ++I)
Chris Lattnerfdb33562009-12-31 01:54:05 +00001393 printNamedMDNode(I);
Devang Patel37c4a2d2009-07-29 22:04:47 +00001394
1395 // Output metadata.
Chris Lattner307c9892009-12-31 02:20:11 +00001396 if (!Machine.mdn_empty()) {
Chris Lattner6e6b1802009-12-31 02:13:35 +00001397 Out << '\n';
1398 writeAllMDNodes();
1399 }
Chris Lattner007377f2001-09-07 16:36:04 +00001400}
1401
Chris Lattnerfdb33562009-12-31 01:54:05 +00001402void AssemblyWriter::printNamedMDNode(const NamedMDNode *NMD) {
1403 Out << "!" << NMD->getName() << " = !{";
1404 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1405 if (i) Out << ", ";
Devang Patel69d02e02010-01-05 21:47:32 +00001406 if (MDNode *MD = NMD->getOperand(i))
1407 Out << '!' << Machine.getMetadataSlot(MD);
1408 else
1409 Out << "null";
Chris Lattnerfdb33562009-12-31 01:54:05 +00001410 }
1411 Out << "}\n";
1412}
1413
1414
Dan Gohman683e9222009-08-12 17:23:50 +00001415static void PrintLinkage(GlobalValue::LinkageTypes LT,
1416 formatted_raw_ostream &Out) {
Chris Lattnercfb5a202008-08-19 05:06:27 +00001417 switch (LT) {
Bill Wendling3d10a5a2009-07-20 01:03:30 +00001418 case GlobalValue::ExternalLinkage: break;
1419 case GlobalValue::PrivateLinkage: Out << "private "; break;
1420 case GlobalValue::LinkerPrivateLinkage: Out << "linker_private "; break;
Bill Wendling5e721d72010-07-01 21:55:59 +00001421 case GlobalValue::LinkerPrivateWeakLinkage:
1422 Out << "linker_private_weak ";
1423 break;
Bill Wendling3d10a5a2009-07-20 01:03:30 +00001424 case GlobalValue::InternalLinkage: Out << "internal "; break;
1425 case GlobalValue::LinkOnceAnyLinkage: Out << "linkonce "; break;
1426 case GlobalValue::LinkOnceODRLinkage: Out << "linkonce_odr "; break;
1427 case GlobalValue::WeakAnyLinkage: Out << "weak "; break;
1428 case GlobalValue::WeakODRLinkage: Out << "weak_odr "; break;
1429 case GlobalValue::CommonLinkage: Out << "common "; break;
1430 case GlobalValue::AppendingLinkage: Out << "appending "; break;
1431 case GlobalValue::DLLImportLinkage: Out << "dllimport "; break;
1432 case GlobalValue::DLLExportLinkage: Out << "dllexport "; break;
1433 case GlobalValue::ExternalWeakLinkage: Out << "extern_weak "; break;
Chris Lattner266c7bb2009-04-13 05:44:34 +00001434 case GlobalValue::AvailableExternallyLinkage:
1435 Out << "available_externally ";
1436 break;
Chris Lattnercfb5a202008-08-19 05:06:27 +00001437 }
1438}
Duncan Sands667d4b82009-03-07 15:45:40 +00001439
Chris Lattnercfb5a202008-08-19 05:06:27 +00001440
1441static void PrintVisibility(GlobalValue::VisibilityTypes Vis,
Dan Gohman683e9222009-08-12 17:23:50 +00001442 formatted_raw_ostream &Out) {
Chris Lattnercfb5a202008-08-19 05:06:27 +00001443 switch (Vis) {
Chris Lattnercfb5a202008-08-19 05:06:27 +00001444 case GlobalValue::DefaultVisibility: break;
1445 case GlobalValue::HiddenVisibility: Out << "hidden "; break;
1446 case GlobalValue::ProtectedVisibility: Out << "protected "; break;
1447 }
1448}
1449
Chris Lattnerc1824992001-10-29 16:05:51 +00001450void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Dan Gohman4483c7b2010-01-29 23:12:36 +00001451 if (GV->isMaterializable())
1452 Out << "; Materializable\n";
1453
Dan Gohmand6c0f652009-08-13 15:27:57 +00001454 WriteAsOperandInternal(Out, GV, &TypePrinter, &Machine);
Dan Gohman3845e502009-08-12 23:32:33 +00001455 Out << " = ";
Chris Lattnerd70684f2001-09-18 04:01:05 +00001456
Chris Lattner52b26de2008-08-19 05:16:28 +00001457 if (!GV->hasInitializer() && GV->hasExternalLinkage())
1458 Out << "external ";
Daniel Dunbara279bc32009-09-20 02:20:51 +00001459
Chris Lattner52b26de2008-08-19 05:16:28 +00001460 PrintLinkage(GV->getLinkage(), Out);
1461 PrintVisibility(GV->getVisibility(), Out);
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00001462
1463 if (GV->isThreadLocal()) Out << "thread_local ";
Chris Lattnerdf986172009-01-02 07:01:27 +00001464 if (unsigned AddressSpace = GV->getType()->getAddressSpace())
1465 Out << "addrspace(" << AddressSpace << ") ";
Misha Brukman0313e0b2004-06-21 21:53:56 +00001466 Out << (GV->isConstant() ? "constant " : "global ");
Chris Lattner0f7364b2009-02-28 21:26:53 +00001467 TypePrinter.print(GV->getType()->getElementType(), Out);
Chris Lattnerd70684f2001-09-18 04:01:05 +00001468
Dan Gohman8dae1382008-09-14 17:21:12 +00001469 if (GV->hasInitializer()) {
1470 Out << ' ';
Devang Patel320671d2009-07-08 21:44:25 +00001471 writeOperand(GV->getInitializer(), false);
Dan Gohman8dae1382008-09-14 17:21:12 +00001472 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001473
Chris Lattner8fff1262010-07-07 23:16:37 +00001474 if (GV->hasSection()) {
1475 Out << ", section \"";
1476 PrintEscapedString(GV->getSection(), Out);
1477 Out << '"';
1478 }
Chris Lattner60962db2005-11-12 00:10:19 +00001479 if (GV->getAlignment())
Chris Lattner30caa282005-11-06 06:48:53 +00001480 Out << ", align " << GV->getAlignment();
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001481
Chris Lattner7e708292002-06-25 16:13:24 +00001482 printInfoComment(*GV);
Chris Lattnercfb5a202008-08-19 05:06:27 +00001483 Out << '\n';
Chris Lattner70cc3392001-09-10 07:58:01 +00001484}
1485
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001486void AssemblyWriter::printAlias(const GlobalAlias *GA) {
Dan Gohman4483c7b2010-01-29 23:12:36 +00001487 if (GA->isMaterializable())
1488 Out << "; Materializable\n";
1489
Dale Johannesen24f07dc2008-06-03 18:14:29 +00001490 // Don't crash when dumping partially built GA
1491 if (!GA->hasName())
1492 Out << "<<nameless>> = ";
Chris Lattnerc97536e2008-08-17 04:40:13 +00001493 else {
1494 PrintLLVMName(Out, GA);
1495 Out << " = ";
1496 }
Chris Lattnercfb5a202008-08-19 05:06:27 +00001497 PrintVisibility(GA->getVisibility(), Out);
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001498
1499 Out << "alias ";
1500
Chris Lattnercfb5a202008-08-19 05:06:27 +00001501 PrintLinkage(GA->getLinkage(), Out);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001502
Anton Korobeynikovc6c98af2007-04-29 18:02:48 +00001503 const Constant *Aliasee = GA->getAliasee();
Daniel Dunbara279bc32009-09-20 02:20:51 +00001504
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001505 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Aliasee)) {
Chris Lattner0f7364b2009-02-28 21:26:53 +00001506 TypePrinter.print(GV->getType(), Out);
Chris Lattnerc97536e2008-08-17 04:40:13 +00001507 Out << ' ';
1508 PrintLLVMName(Out, GV);
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001509 } else if (const Function *F = dyn_cast<Function>(Aliasee)) {
Chris Lattner0f7364b2009-02-28 21:26:53 +00001510 TypePrinter.print(F->getFunctionType(), Out);
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001511 Out << "* ";
1512
Dan Gohmand6c0f652009-08-13 15:27:57 +00001513 WriteAsOperandInternal(Out, F, &TypePrinter, &Machine);
Anton Korobeynikov591858a2008-03-22 08:17:17 +00001514 } else if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(Aliasee)) {
Chris Lattner0f7364b2009-02-28 21:26:53 +00001515 TypePrinter.print(GA->getType(), Out);
1516 Out << ' ';
Chris Lattnerc97536e2008-08-17 04:40:13 +00001517 PrintLLVMName(Out, GA);
Anton Korobeynikova80e1182007-04-28 13:45:00 +00001518 } else {
Chris Lattnera2165ed2009-04-25 21:23:19 +00001519 const ConstantExpr *CE = cast<ConstantExpr>(Aliasee);
1520 // The only valid GEP is an all zero GEP.
1521 assert((CE->getOpcode() == Instruction::BitCast ||
1522 CE->getOpcode() == Instruction::GetElementPtr) &&
1523 "Unsupported aliasee");
1524 writeOperand(CE, false);
Anton Korobeynikova80e1182007-04-28 13:45:00 +00001525 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001526
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001527 printInfoComment(*GA);
Chris Lattner52b26de2008-08-19 05:16:28 +00001528 Out << '\n';
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001529}
1530
Reid Spencer78d033e2007-01-06 07:24:44 +00001531void AssemblyWriter::printTypeSymbolTable(const TypeSymbolTable &ST) {
Chris Lattner413fd232009-03-01 00:03:38 +00001532 // Emit all numbered types.
1533 for (unsigned i = 0, e = NumberedTypes.size(); i != e; ++i) {
Dan Gohman3845e502009-08-12 23:32:33 +00001534 Out << '%' << i << " = type ";
Daniel Dunbara279bc32009-09-20 02:20:51 +00001535
Chris Lattner413fd232009-03-01 00:03:38 +00001536 // Make sure we print out at least one level of the type structure, so
1537 // that we do not get %2 = type %2
1538 TypePrinter.printAtLeastOneLevel(NumberedTypes[i], Out);
Dan Gohman9bf0b9b2009-08-12 23:54:22 +00001539 Out << '\n';
Chris Lattner413fd232009-03-01 00:03:38 +00001540 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001541
Chris Lattner413fd232009-03-01 00:03:38 +00001542 // Print the named types.
Reid Spencer78d033e2007-01-06 07:24:44 +00001543 for (TypeSymbolTable::const_iterator TI = ST.begin(), TE = ST.end();
1544 TI != TE; ++TI) {
Daniel Dunbar03d76512009-07-25 23:55:21 +00001545 PrintLLVMName(Out, TI->first, LocalPrefix);
Chris Lattner52b26de2008-08-19 05:16:28 +00001546 Out << " = type ";
Reid Spencer9231ac82004-05-25 08:53:40 +00001547
1548 // Make sure we print out at least one level of the type structure, so
1549 // that we do not get %FILE = type %FILE
Chris Lattner0f7364b2009-02-28 21:26:53 +00001550 TypePrinter.printAtLeastOneLevel(TI->second, Out);
Chris Lattnercfb5a202008-08-19 05:06:27 +00001551 Out << '\n';
Reid Spencer9231ac82004-05-25 08:53:40 +00001552 }
Reid Spencer78d033e2007-01-06 07:24:44 +00001553}
1554
Misha Brukmanab5c6002004-03-02 00:22:19 +00001555/// printFunction - Print all aspects of a function.
1556///
Chris Lattner7e708292002-06-25 16:13:24 +00001557void AssemblyWriter::printFunction(const Function *F) {
Chris Lattnercfb5a202008-08-19 05:06:27 +00001558 // Print out the return type and name.
1559 Out << '\n';
Chris Lattner4ad02e72003-04-16 20:28:45 +00001560
Misha Brukman0313e0b2004-06-21 21:53:56 +00001561 if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, Out);
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001562
Dan Gohman4483c7b2010-01-29 23:12:36 +00001563 if (F->isMaterializable())
1564 Out << "; Materializable\n";
1565
Reid Spencer5cbf9852007-01-30 20:08:39 +00001566 if (F->isDeclaration())
Chris Lattner3aa60662007-08-19 22:15:26 +00001567 Out << "declare ";
1568 else
Reid Spencerb951bc02006-12-29 20:29:48 +00001569 Out << "define ";
Daniel Dunbara279bc32009-09-20 02:20:51 +00001570
Chris Lattnercfb5a202008-08-19 05:06:27 +00001571 PrintLinkage(F->getLinkage(), Out);
1572 PrintVisibility(F->getVisibility(), Out);
Chris Lattner4ad02e72003-04-16 20:28:45 +00001573
Chris Lattnerd5118982005-05-06 20:26:43 +00001574 // Print the calling convention.
1575 switch (F->getCallingConv()) {
1576 case CallingConv::C: break; // default
Anton Korobeynikovf8248682006-09-20 22:03:51 +00001577 case CallingConv::Fast: Out << "fastcc "; break;
1578 case CallingConv::Cold: Out << "coldcc "; break;
1579 case CallingConv::X86_StdCall: Out << "x86_stdcallcc "; break;
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001580 case CallingConv::X86_FastCall: Out << "x86_fastcallcc "; break;
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001581 case CallingConv::X86_ThisCall: Out << "x86_thiscallcc "; break;
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001582 case CallingConv::ARM_APCS: Out << "arm_apcscc "; break;
1583 case CallingConv::ARM_AAPCS: Out << "arm_aapcscc "; break;
1584 case CallingConv::ARM_AAPCS_VFP:Out << "arm_aapcs_vfpcc "; break;
Anton Korobeynikov211a14e2009-12-07 02:27:35 +00001585 case CallingConv::MSP430_INTR: Out << "msp430_intrcc "; break;
Chris Lattnerd5118982005-05-06 20:26:43 +00001586 default: Out << "cc" << F->getCallingConv() << " "; break;
1587 }
1588
Reid Spencerbd5db8e2006-12-31 05:24:50 +00001589 const FunctionType *FT = F->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00001590 const AttrListPtr &Attrs = F->getAttributes();
Devang Patel652203f2008-09-29 20:49:50 +00001591 Attributes RetAttrs = Attrs.getRetAttributes();
1592 if (RetAttrs != Attribute::None)
1593 Out << Attribute::getAsString(Attrs.getRetAttributes()) << ' ';
Chris Lattner0f7364b2009-02-28 21:26:53 +00001594 TypePrinter.print(F->getReturnType(), Out);
Chris Lattner4667b712008-08-19 05:26:17 +00001595 Out << ' ';
Dan Gohmand6c0f652009-08-13 15:27:57 +00001596 WriteAsOperandInternal(Out, F, &TypePrinter, &Machine);
Misha Brukman0313e0b2004-06-21 21:53:56 +00001597 Out << '(';
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001598 Machine.incorporateFunction(F);
Chris Lattner007377f2001-09-07 16:36:04 +00001599
Chris Lattnerc1824992001-10-29 16:05:51 +00001600 // Loop over the arguments, printing them...
Chris Lattner007377f2001-09-07 16:36:04 +00001601
Reid Spencerbd5db8e2006-12-31 05:24:50 +00001602 unsigned Idx = 1;
Chris Lattner8dcd2f12007-04-18 00:57:22 +00001603 if (!F->isDeclaration()) {
1604 // If this isn't a declaration, print the argument names as well.
1605 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
1606 I != E; ++I) {
1607 // Insert commas as we go... the first arg doesn't get a comma
1608 if (I != F->arg_begin()) Out << ", ";
Devang Patel19c87462008-09-26 22:53:05 +00001609 printArgument(I, Attrs.getParamAttributes(Idx));
Chris Lattner8dcd2f12007-04-18 00:57:22 +00001610 Idx++;
1611 }
1612 } else {
1613 // Otherwise, print the types from the function type.
1614 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
1615 // Insert commas as we go... the first arg doesn't get a comma
1616 if (i) Out << ", ";
Daniel Dunbara279bc32009-09-20 02:20:51 +00001617
Chris Lattner8dcd2f12007-04-18 00:57:22 +00001618 // Output type...
Chris Lattner0f7364b2009-02-28 21:26:53 +00001619 TypePrinter.print(FT->getParamType(i), Out);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001620
Devang Patel19c87462008-09-26 22:53:05 +00001621 Attributes ArgAttrs = Attrs.getParamAttributes(i+1);
Devang Patel05988662008-09-25 21:00:45 +00001622 if (ArgAttrs != Attribute::None)
1623 Out << ' ' << Attribute::getAsString(ArgAttrs);
Chris Lattner8dcd2f12007-04-18 00:57:22 +00001624 }
Reid Spencerbd5db8e2006-12-31 05:24:50 +00001625 }
Chris Lattner007377f2001-09-07 16:36:04 +00001626
1627 // Finish printing arguments...
Chris Lattner7e708292002-06-25 16:13:24 +00001628 if (FT->isVarArg()) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001629 if (FT->getNumParams()) Out << ", ";
1630 Out << "..."; // Output varargs portion of signature!
Chris Lattner007377f2001-09-07 16:36:04 +00001631 }
Misha Brukman0313e0b2004-06-21 21:53:56 +00001632 Out << ')';
Devang Patel19c87462008-09-26 22:53:05 +00001633 Attributes FnAttrs = Attrs.getFnAttributes();
1634 if (FnAttrs != Attribute::None)
1635 Out << ' ' << Attribute::getAsString(Attrs.getFnAttributes());
Chris Lattner8fff1262010-07-07 23:16:37 +00001636 if (F->hasSection()) {
1637 Out << " section \"";
1638 PrintEscapedString(F->getSection(), Out);
1639 Out << '"';
1640 }
Chris Lattner30caa282005-11-06 06:48:53 +00001641 if (F->getAlignment())
1642 Out << " align " << F->getAlignment();
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001643 if (F->hasGC())
1644 Out << " gc \"" << F->getGC() << '"';
Reid Spencer5cbf9852007-01-30 20:08:39 +00001645 if (F->isDeclaration()) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001646 Out << "\n";
Chris Lattner03e2acb2002-05-06 03:00:40 +00001647 } else {
Chris Lattnera1b58582008-04-21 06:12:55 +00001648 Out << " {";
Misha Brukmanfd939082005-04-21 23:48:37 +00001649
Chris Lattnerb5794002002-04-07 22:49:37 +00001650 // Output all of its basic blocks... for the function
Chris Lattner7e708292002-06-25 16:13:24 +00001651 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
1652 printBasicBlock(I);
Chris Lattner007377f2001-09-07 16:36:04 +00001653
Misha Brukman0313e0b2004-06-21 21:53:56 +00001654 Out << "}\n";
Chris Lattner007377f2001-09-07 16:36:04 +00001655 }
1656
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001657 Machine.purgeFunction();
Chris Lattner00950542001-06-06 20:29:01 +00001658}
1659
Misha Brukmanab5c6002004-03-02 00:22:19 +00001660/// printArgument - This member is called for every argument that is passed into
1661/// the function. Simply print it out
1662///
Daniel Dunbara279bc32009-09-20 02:20:51 +00001663void AssemblyWriter::printArgument(const Argument *Arg,
Devang Pateleaf42ab2008-09-23 23:03:40 +00001664 Attributes Attrs) {
Chris Lattner00950542001-06-06 20:29:01 +00001665 // Output type...
Chris Lattner0f7364b2009-02-28 21:26:53 +00001666 TypePrinter.print(Arg->getType(), Out);
Misha Brukmanfd939082005-04-21 23:48:37 +00001667
Duncan Sandsdc024672007-11-27 13:23:08 +00001668 // Output parameter attributes list
Devang Patel05988662008-09-25 21:00:45 +00001669 if (Attrs != Attribute::None)
1670 Out << ' ' << Attribute::getAsString(Attrs);
Reid Spencerbd5db8e2006-12-31 05:24:50 +00001671
Chris Lattner00950542001-06-06 20:29:01 +00001672 // Output name, if available...
Chris Lattnerc97536e2008-08-17 04:40:13 +00001673 if (Arg->hasName()) {
1674 Out << ' ';
1675 PrintLLVMName(Out, Arg);
1676 }
Chris Lattner00950542001-06-06 20:29:01 +00001677}
1678
Misha Brukmanab5c6002004-03-02 00:22:19 +00001679/// printBasicBlock - This member is called for each basic block in a method.
1680///
Chris Lattnerc1824992001-10-29 16:05:51 +00001681void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Nick Lewycky280a6e62008-04-25 16:53:59 +00001682 if (BB->hasName()) { // Print out the label if it exists...
Chris Lattnerc97536e2008-08-17 04:40:13 +00001683 Out << "\n";
Daniel Dunbar03d76512009-07-25 23:55:21 +00001684 PrintLLVMName(Out, BB->getName(), LabelPrefix);
Chris Lattnerc97536e2008-08-17 04:40:13 +00001685 Out << ':';
Nick Lewycky280a6e62008-04-25 16:53:59 +00001686 } else if (!BB->use_empty()) { // Don't print block # of no uses...
Chris Lattner96c5b2f2008-04-21 04:20:33 +00001687 Out << "\n; <label>:";
Chris Lattner22379bc2007-01-11 03:54:27 +00001688 int Slot = Machine.getLocalSlot(BB);
Chris Lattner69566452004-06-09 19:41:19 +00001689 if (Slot != -1)
Misha Brukman0313e0b2004-06-21 21:53:56 +00001690 Out << Slot;
Chris Lattner69566452004-06-09 19:41:19 +00001691 else
Misha Brukman0313e0b2004-06-21 21:53:56 +00001692 Out << "<badref>";
Chris Lattner061269b2002-10-02 19:38:55 +00001693 }
Chris Lattner4e4d8622003-11-20 00:09:43 +00001694
Dan Gohman683e9222009-08-12 17:23:50 +00001695 if (BB->getParent() == 0) {
Chris Lattner8f4b1ec2009-08-17 15:48:08 +00001696 Out.PadToColumn(50);
Dan Gohman683e9222009-08-12 17:23:50 +00001697 Out << "; Error: Block without parent!";
1698 } else if (BB != &BB->getParent()->getEntryBlock()) { // Not the entry block?
Chris Lattnereb411292008-04-22 02:45:44 +00001699 // Output predecessors for the block...
Chris Lattner8f4b1ec2009-08-17 15:48:08 +00001700 Out.PadToColumn(50);
Dan Gohman683e9222009-08-12 17:23:50 +00001701 Out << ";";
Gabor Greif44424642010-03-25 23:25:28 +00001702 const_pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001703
Chris Lattnereb411292008-04-22 02:45:44 +00001704 if (PI == PE) {
1705 Out << " No predecessors!";
1706 } else {
Dan Gohman8dae1382008-09-14 17:21:12 +00001707 Out << " preds = ";
Chris Lattnereb411292008-04-22 02:45:44 +00001708 writeOperand(*PI, false);
1709 for (++PI; PI != PE; ++PI) {
Dan Gohman8dae1382008-09-14 17:21:12 +00001710 Out << ", ";
Chris Lattner2fcfdb72006-12-06 06:24:27 +00001711 writeOperand(*PI, false);
Chris Lattner40efcec2003-11-16 22:59:57 +00001712 }
Chris Lattner061269b2002-10-02 19:38:55 +00001713 }
Chris Lattner00950542001-06-06 20:29:01 +00001714 }
Misha Brukmanfd939082005-04-21 23:48:37 +00001715
Chris Lattnereb411292008-04-22 02:45:44 +00001716 Out << "\n";
Chris Lattner00950542001-06-06 20:29:01 +00001717
Misha Brukman0313e0b2004-06-21 21:53:56 +00001718 if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, Out);
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001719
Chris Lattner007377f2001-09-07 16:36:04 +00001720 // Output all of the instructions in the basic block...
Dan Gohmanbeca6892009-07-13 18:27:59 +00001721 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
Chris Lattner7e708292002-06-25 16:13:24 +00001722 printInstruction(*I);
Dan Gohmanbeca6892009-07-13 18:27:59 +00001723 Out << '\n';
1724 }
Chris Lattner9f717ef2004-03-08 18:51:45 +00001725
Misha Brukman0313e0b2004-06-21 21:53:56 +00001726 if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, Out);
Chris Lattner00950542001-06-06 20:29:01 +00001727}
1728
Misha Brukmanab5c6002004-03-02 00:22:19 +00001729/// printInfoComment - Print a little comment after the instruction indicating
1730/// which slot it occupies.
1731///
Chris Lattner7e708292002-06-25 16:13:24 +00001732void AssemblyWriter::printInfoComment(const Value &V) {
Dan Gohman7a5666e2010-02-10 20:41:46 +00001733 if (AnnotationWriter) {
1734 AnnotationWriter->printInfoComment(V, Out);
1735 return;
1736 }
1737
Chris Lattner4ee93c42009-12-29 07:25:48 +00001738 if (V.getType()->isVoidTy()) return;
1739
1740 Out.PadToColumn(50);
1741 Out << "; <";
1742 TypePrinter.print(V.getType(), Out);
1743 Out << "> [#uses=" << V.getNumUses() << ']'; // Output # uses
Chris Lattnere02fa852001-10-13 06:42:36 +00001744}
1745
Reid Spencer3a9ec242006-08-28 01:02:49 +00001746// This member is called for each Instruction in a function..
Chris Lattner7e708292002-06-25 16:13:24 +00001747void AssemblyWriter::printInstruction(const Instruction &I) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001748 if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, Out);
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001749
Dan Gohman3845e502009-08-12 23:32:33 +00001750 // Print out indentation for an instruction.
Dan Gohman01889ca2009-08-13 01:41:52 +00001751 Out << " ";
Chris Lattner00950542001-06-06 20:29:01 +00001752
1753 // Print out name if it exists...
Chris Lattnerc97536e2008-08-17 04:40:13 +00001754 if (I.hasName()) {
1755 PrintLLVMName(Out, &I);
1756 Out << " = ";
Chris Lattner4ee93c42009-12-29 07:25:48 +00001757 } else if (!I.getType()->isVoidTy()) {
Chris Lattner828db8a2008-08-29 17:19:30 +00001758 // Print out the def slot taken.
1759 int SlotNum = Machine.getLocalSlot(&I);
1760 if (SlotNum == -1)
1761 Out << "<badref> = ";
1762 else
1763 Out << '%' << SlotNum << " = ";
Chris Lattnerc97536e2008-08-17 04:40:13 +00001764 }
Chris Lattner00950542001-06-06 20:29:01 +00001765
Chris Lattnerddb6db42005-05-06 05:51:46 +00001766 // If this is a volatile load or store, print out the volatile marker.
Chris Lattnere5e475e2003-09-08 17:45:59 +00001767 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) ||
Chris Lattnerddb6db42005-05-06 05:51:46 +00001768 (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile())) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001769 Out << "volatile ";
Chris Lattnerddb6db42005-05-06 05:51:46 +00001770 } else if (isa<CallInst>(I) && cast<CallInst>(I).isTailCall()) {
1771 // If this is a call, check if it's a tail call.
1772 Out << "tail ";
1773 }
Chris Lattnere5e475e2003-09-08 17:45:59 +00001774
Chris Lattner00950542001-06-06 20:29:01 +00001775 // Print out the opcode...
Misha Brukman0313e0b2004-06-21 21:53:56 +00001776 Out << I.getOpcodeName();
Chris Lattner00950542001-06-06 20:29:01 +00001777
Dan Gohman59858cf2009-07-27 16:11:46 +00001778 // Print out optimization information.
1779 WriteOptimizationInfo(Out, &I);
1780
Reid Spencer74f16422006-12-03 06:27:29 +00001781 // Print out the compare instruction predicates
Nate Begemanac80ade2008-05-12 19:01:56 +00001782 if (const CmpInst *CI = dyn_cast<CmpInst>(&I))
Chris Lattnerab49ee72008-08-23 22:52:27 +00001783 Out << ' ' << getPredicateText(CI->getPredicate());
Reid Spencer74f16422006-12-03 06:27:29 +00001784
Chris Lattner00950542001-06-06 20:29:01 +00001785 // Print out the type of the operands...
Chris Lattner7e708292002-06-25 16:13:24 +00001786 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0;
Chris Lattner00950542001-06-06 20:29:01 +00001787
1788 // Special case conditional branches to swizzle the condition out to the front
Gabor Greifccd27fb2009-02-09 15:45:06 +00001789 if (isa<BranchInst>(I) && cast<BranchInst>(I).isConditional()) {
1790 BranchInst &BI(cast<BranchInst>(I));
Dan Gohman8dae1382008-09-14 17:21:12 +00001791 Out << ' ';
Gabor Greifccd27fb2009-02-09 15:45:06 +00001792 writeOperand(BI.getCondition(), true);
Dan Gohman8dae1382008-09-14 17:21:12 +00001793 Out << ", ";
Gabor Greifccd27fb2009-02-09 15:45:06 +00001794 writeOperand(BI.getSuccessor(0), true);
Dan Gohman8dae1382008-09-14 17:21:12 +00001795 Out << ", ";
Gabor Greifccd27fb2009-02-09 15:45:06 +00001796 writeOperand(BI.getSuccessor(1), true);
Chris Lattner00950542001-06-06 20:29:01 +00001797
Chris Lattner94dc1f22002-04-13 18:34:38 +00001798 } else if (isa<SwitchInst>(I)) {
Chris Lattnerf9be95f2009-10-27 19:13:16 +00001799 // Special case switch instruction to get formatting nice and correct.
Dan Gohman8dae1382008-09-14 17:21:12 +00001800 Out << ' ';
Chris Lattnerab49ee72008-08-23 22:52:27 +00001801 writeOperand(Operand , true);
Dan Gohman8dae1382008-09-14 17:21:12 +00001802 Out << ", ";
Chris Lattnerab49ee72008-08-23 22:52:27 +00001803 writeOperand(I.getOperand(1), true);
1804 Out << " [";
Chris Lattner00950542001-06-06 20:29:01 +00001805
Chris Lattner7e708292002-06-25 16:13:24 +00001806 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; op += 2) {
Dan Gohman01889ca2009-08-13 01:41:52 +00001807 Out << "\n ";
Chris Lattnerab49ee72008-08-23 22:52:27 +00001808 writeOperand(I.getOperand(op ), true);
Dan Gohman8dae1382008-09-14 17:21:12 +00001809 Out << ", ";
Chris Lattner7e708292002-06-25 16:13:24 +00001810 writeOperand(I.getOperand(op+1), true);
Chris Lattner00950542001-06-06 20:29:01 +00001811 }
Dan Gohman01889ca2009-08-13 01:41:52 +00001812 Out << "\n ]";
Chris Lattnerab21db72009-10-28 00:19:10 +00001813 } else if (isa<IndirectBrInst>(I)) {
1814 // Special case indirectbr instruction to get formatting nice and correct.
Chris Lattnerf9be95f2009-10-27 19:13:16 +00001815 Out << ' ';
1816 writeOperand(Operand, true);
Dan Gohman0ed1f422009-10-30 02:01:10 +00001817 Out << ", [";
Chris Lattnerf9be95f2009-10-27 19:13:16 +00001818
1819 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) {
1820 if (i != 1)
1821 Out << ", ";
1822 writeOperand(I.getOperand(i), true);
1823 }
1824 Out << ']';
Chris Lattnerb00c5822001-10-02 03:41:24 +00001825 } else if (isa<PHINode>(I)) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001826 Out << ' ';
Chris Lattner0f7364b2009-02-28 21:26:53 +00001827 TypePrinter.print(I.getType(), Out);
Misha Brukman0313e0b2004-06-21 21:53:56 +00001828 Out << ' ';
Chris Lattner00950542001-06-06 20:29:01 +00001829
Chris Lattner7e708292002-06-25 16:13:24 +00001830 for (unsigned op = 0, Eop = I.getNumOperands(); op < Eop; op += 2) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001831 if (op) Out << ", ";
Dan Gohman8dae1382008-09-14 17:21:12 +00001832 Out << "[ ";
1833 writeOperand(I.getOperand(op ), false); Out << ", ";
Misha Brukman0313e0b2004-06-21 21:53:56 +00001834 writeOperand(I.getOperand(op+1), false); Out << " ]";
Chris Lattnerc24d2082001-06-11 15:04:20 +00001835 }
Dan Gohman995be7d2008-05-31 19:12:39 +00001836 } else if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(&I)) {
Dan Gohman8dae1382008-09-14 17:21:12 +00001837 Out << ' ';
Dan Gohman995be7d2008-05-31 19:12:39 +00001838 writeOperand(I.getOperand(0), true);
1839 for (const unsigned *i = EVI->idx_begin(), *e = EVI->idx_end(); i != e; ++i)
1840 Out << ", " << *i;
1841 } else if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(&I)) {
Dan Gohman8dae1382008-09-14 17:21:12 +00001842 Out << ' ';
1843 writeOperand(I.getOperand(0), true); Out << ", ";
Dan Gohman995be7d2008-05-31 19:12:39 +00001844 writeOperand(I.getOperand(1), true);
1845 for (const unsigned *i = IVI->idx_begin(), *e = IVI->idx_end(); i != e; ++i)
1846 Out << ", " << *i;
Devang Patel57ef4f42008-02-23 00:35:18 +00001847 } else if (isa<ReturnInst>(I) && !Operand) {
1848 Out << " void";
Chris Lattnerd5118982005-05-06 20:26:43 +00001849 } else if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
1850 // Print the calling convention being used.
1851 switch (CI->getCallingConv()) {
1852 case CallingConv::C: break; // default
Chris Lattner0deaab82006-05-19 21:58:52 +00001853 case CallingConv::Fast: Out << " fastcc"; break;
1854 case CallingConv::Cold: Out << " coldcc"; break;
Chris Lattnerb28a6dc2007-11-18 18:32:16 +00001855 case CallingConv::X86_StdCall: Out << " x86_stdcallcc"; break;
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001856 case CallingConv::X86_FastCall: Out << " x86_fastcallcc"; break;
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001857 case CallingConv::X86_ThisCall: Out << " x86_thiscallcc"; break;
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001858 case CallingConv::ARM_APCS: Out << " arm_apcscc "; break;
1859 case CallingConv::ARM_AAPCS: Out << " arm_aapcscc "; break;
1860 case CallingConv::ARM_AAPCS_VFP:Out << " arm_aapcs_vfpcc "; break;
Anton Korobeynikov211a14e2009-12-07 02:27:35 +00001861 case CallingConv::MSP430_INTR: Out << " msp430_intrcc "; break;
Chris Lattnerd5118982005-05-06 20:26:43 +00001862 default: Out << " cc" << CI->getCallingConv(); break;
1863 }
1864
Gabor Greif7bbdf0c2010-06-23 13:09:06 +00001865 Operand = CI->getCalledValue();
Reid Spencerb138a062007-04-09 06:10:42 +00001866 const PointerType *PTy = cast<PointerType>(Operand->getType());
1867 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1868 const Type *RetTy = FTy->getReturnType();
Devang Patel05988662008-09-25 21:00:45 +00001869 const AttrListPtr &PAL = CI->getAttributes();
Chris Lattner268de042001-11-06 21:28:12 +00001870
Devang Patel652203f2008-09-29 20:49:50 +00001871 if (PAL.getRetAttributes() != Attribute::None)
1872 Out << ' ' << Attribute::getAsString(PAL.getRetAttributes());
1873
Chris Lattner7a012292003-08-05 15:34:45 +00001874 // If possible, print out the short form of the call instruction. We can
Chris Lattnerb5794002002-04-07 22:49:37 +00001875 // only do this if the first argument is a pointer to a nonvararg function,
Chris Lattner7a012292003-08-05 15:34:45 +00001876 // and if the return type is not a pointer to a function.
Chris Lattner268de042001-11-06 21:28:12 +00001877 //
Dan Gohman8dae1382008-09-14 17:21:12 +00001878 Out << ' ';
Chris Lattner7a012292003-08-05 15:34:45 +00001879 if (!FTy->isVarArg() &&
Duncan Sands1df98592010-02-16 11:11:14 +00001880 (!RetTy->isPointerTy() ||
1881 !cast<PointerType>(RetTy)->getElementType()->isFunctionTy())) {
Chris Lattner0f7364b2009-02-28 21:26:53 +00001882 TypePrinter.print(RetTy, Out);
Dan Gohman8dae1382008-09-14 17:21:12 +00001883 Out << ' ';
Chris Lattner268de042001-11-06 21:28:12 +00001884 writeOperand(Operand, false);
1885 } else {
1886 writeOperand(Operand, true);
1887 }
Misha Brukman0313e0b2004-06-21 21:53:56 +00001888 Out << '(';
Gabor Greif7bbdf0c2010-06-23 13:09:06 +00001889 for (unsigned op = 0, Eop = CI->getNumArgOperands(); op < Eop; ++op) {
1890 if (op > 0)
Dan Gohman8dae1382008-09-14 17:21:12 +00001891 Out << ", ";
Gabor Greif7bbdf0c2010-06-23 13:09:06 +00001892 writeParamOperand(CI->getArgOperand(op), PAL.getParamAttributes(op + 1));
Chris Lattner00950542001-06-06 20:29:01 +00001893 }
Dan Gohman8dae1382008-09-14 17:21:12 +00001894 Out << ')';
Devang Patel19c87462008-09-26 22:53:05 +00001895 if (PAL.getFnAttributes() != Attribute::None)
1896 Out << ' ' << Attribute::getAsString(PAL.getFnAttributes());
Chris Lattner7e708292002-06-25 16:13:24 +00001897 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
Gabor Greifc9f75002010-03-24 13:21:49 +00001898 Operand = II->getCalledValue();
Reid Spencerb138a062007-04-09 06:10:42 +00001899 const PointerType *PTy = cast<PointerType>(Operand->getType());
1900 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1901 const Type *RetTy = FTy->getReturnType();
Devang Patel05988662008-09-25 21:00:45 +00001902 const AttrListPtr &PAL = II->getAttributes();
Chris Lattner7a012292003-08-05 15:34:45 +00001903
Chris Lattnerd5118982005-05-06 20:26:43 +00001904 // Print the calling convention being used.
1905 switch (II->getCallingConv()) {
1906 case CallingConv::C: break; // default
Chris Lattner0deaab82006-05-19 21:58:52 +00001907 case CallingConv::Fast: Out << " fastcc"; break;
1908 case CallingConv::Cold: Out << " coldcc"; break;
Dan Gohman8dae1382008-09-14 17:21:12 +00001909 case CallingConv::X86_StdCall: Out << " x86_stdcallcc"; break;
1910 case CallingConv::X86_FastCall: Out << " x86_fastcallcc"; break;
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001911 case CallingConv::X86_ThisCall: Out << " x86_thiscallcc"; break;
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001912 case CallingConv::ARM_APCS: Out << " arm_apcscc "; break;
1913 case CallingConv::ARM_AAPCS: Out << " arm_aapcscc "; break;
1914 case CallingConv::ARM_AAPCS_VFP:Out << " arm_aapcs_vfpcc "; break;
Anton Korobeynikov211a14e2009-12-07 02:27:35 +00001915 case CallingConv::MSP430_INTR: Out << " msp430_intrcc "; break;
Chris Lattnerd5118982005-05-06 20:26:43 +00001916 default: Out << " cc" << II->getCallingConv(); break;
1917 }
1918
Devang Patel652203f2008-09-29 20:49:50 +00001919 if (PAL.getRetAttributes() != Attribute::None)
1920 Out << ' ' << Attribute::getAsString(PAL.getRetAttributes());
1921
Chris Lattner7a012292003-08-05 15:34:45 +00001922 // If possible, print out the short form of the invoke instruction. We can
1923 // only do this if the first argument is a pointer to a nonvararg function,
1924 // and if the return type is not a pointer to a function.
1925 //
Dan Gohman2b6c3d92008-10-15 18:02:08 +00001926 Out << ' ';
Chris Lattner7a012292003-08-05 15:34:45 +00001927 if (!FTy->isVarArg() &&
Duncan Sands1df98592010-02-16 11:11:14 +00001928 (!RetTy->isPointerTy() ||
1929 !cast<PointerType>(RetTy)->getElementType()->isFunctionTy())) {
Chris Lattner0f7364b2009-02-28 21:26:53 +00001930 TypePrinter.print(RetTy, Out);
Dan Gohman2b6c3d92008-10-15 18:02:08 +00001931 Out << ' ';
Chris Lattner7a012292003-08-05 15:34:45 +00001932 writeOperand(Operand, false);
1933 } else {
1934 writeOperand(Operand, true);
1935 }
Misha Brukman0313e0b2004-06-21 21:53:56 +00001936 Out << '(';
Gabor Greif7bbdf0c2010-06-23 13:09:06 +00001937 for (unsigned op = 0, Eop = II->getNumArgOperands(); op < Eop; ++op) {
Gabor Greifc9f75002010-03-24 13:21:49 +00001938 if (op)
Dan Gohman8dae1382008-09-14 17:21:12 +00001939 Out << ", ";
Gabor Greif7bbdf0c2010-06-23 13:09:06 +00001940 writeParamOperand(II->getArgOperand(op), PAL.getParamAttributes(op + 1));
Chris Lattnere02fa852001-10-13 06:42:36 +00001941 }
1942
Dan Gohman8dae1382008-09-14 17:21:12 +00001943 Out << ')';
Devang Patel19c87462008-09-26 22:53:05 +00001944 if (PAL.getFnAttributes() != Attribute::None)
1945 Out << ' ' << Attribute::getAsString(PAL.getFnAttributes());
1946
Dan Gohman01889ca2009-08-13 01:41:52 +00001947 Out << "\n to ";
Chris Lattnere02fa852001-10-13 06:42:36 +00001948 writeOperand(II->getNormalDest(), true);
Dan Gohman8dae1382008-09-14 17:21:12 +00001949 Out << " unwind ";
Chris Lattneraeb2a1d2004-02-08 21:44:31 +00001950 writeOperand(II->getUnwindDest(), true);
Chris Lattnere02fa852001-10-13 06:42:36 +00001951
Victor Hernandez7b929da2009-10-23 21:09:37 +00001952 } else if (const AllocaInst *AI = dyn_cast<AllocaInst>(&I)) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001953 Out << ' ';
Chris Lattner0f7364b2009-02-28 21:26:53 +00001954 TypePrinter.print(AI->getType()->getElementType(), Out);
Dan Gohman69bff072009-07-31 18:23:24 +00001955 if (!AI->getArraySize() || AI->isArrayAllocation()) {
Dan Gohman8dae1382008-09-14 17:21:12 +00001956 Out << ", ";
Chris Lattner94dc1f22002-04-13 18:34:38 +00001957 writeOperand(AI->getArraySize(), true);
Chris Lattner00950542001-06-06 20:29:01 +00001958 }
Nate Begeman14b05292005-11-05 09:21:28 +00001959 if (AI->getAlignment()) {
Chris Lattner9fad0b92005-11-05 21:20:34 +00001960 Out << ", align " << AI->getAlignment();
Nate Begeman14b05292005-11-05 09:21:28 +00001961 }
Chris Lattnere02fa852001-10-13 06:42:36 +00001962 } else if (isa<CastInst>(I)) {
Dan Gohman8dae1382008-09-14 17:21:12 +00001963 if (Operand) {
1964 Out << ' ';
1965 writeOperand(Operand, true); // Work with broken code
1966 }
Misha Brukman0313e0b2004-06-21 21:53:56 +00001967 Out << " to ";
Chris Lattner0f7364b2009-02-28 21:26:53 +00001968 TypePrinter.print(I.getType(), Out);
Chris Lattner4d45bd02003-10-18 05:57:43 +00001969 } else if (isa<VAArgInst>(I)) {
Dan Gohman8dae1382008-09-14 17:21:12 +00001970 if (Operand) {
1971 Out << ' ';
1972 writeOperand(Operand, true); // Work with broken code
1973 }
Misha Brukman0313e0b2004-06-21 21:53:56 +00001974 Out << ", ";
Chris Lattner0f7364b2009-02-28 21:26:53 +00001975 TypePrinter.print(I.getType(), Out);
1976 } else if (Operand) { // Print the normal way.
Chris Lattner00950542001-06-06 20:29:01 +00001977
Misha Brukmanfd939082005-04-21 23:48:37 +00001978 // PrintAllTypes - Instructions who have operands of all the same type
Chris Lattner00950542001-06-06 20:29:01 +00001979 // omit the type from all but the first operand. If the instruction has
1980 // different type operands (for example br), then they are all printed.
1981 bool PrintAllTypes = false;
1982 const Type *TheType = Operand->getType();
Chris Lattner00950542001-06-06 20:29:01 +00001983
Reid Spencerebe57e32007-02-02 13:54:55 +00001984 // Select, Store and ShuffleVector always print all types.
Devang Patel64947682008-03-04 22:05:14 +00001985 if (isa<SelectInst>(I) || isa<StoreInst>(I) || isa<ShuffleVectorInst>(I)
1986 || isa<ReturnInst>(I)) {
Chris Lattnerffd9bf42003-04-16 20:20:02 +00001987 PrintAllTypes = true;
1988 } else {
1989 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
1990 Operand = I.getOperand(i);
Nuno Lopes6ad2b2a2009-01-15 18:40:57 +00001991 // note that Operand shouldn't be null, but the test helps make dump()
1992 // more tolerant of malformed IR
Nuno Lopesa8c78a92009-01-14 17:51:41 +00001993 if (Operand && Operand->getType() != TheType) {
Chris Lattnerffd9bf42003-04-16 20:20:02 +00001994 PrintAllTypes = true; // We have differing types! Print them all!
1995 break;
1996 }
Chris Lattner00950542001-06-06 20:29:01 +00001997 }
1998 }
Misha Brukmanfd939082005-04-21 23:48:37 +00001999
Chris Lattnerc1824992001-10-29 16:05:51 +00002000 if (!PrintAllTypes) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00002001 Out << ' ';
Chris Lattner0f7364b2009-02-28 21:26:53 +00002002 TypePrinter.print(TheType, Out);
Chris Lattnerc1824992001-10-29 16:05:51 +00002003 }
Chris Lattner00950542001-06-06 20:29:01 +00002004
Dan Gohman8dae1382008-09-14 17:21:12 +00002005 Out << ' ';
Chris Lattner7e708292002-06-25 16:13:24 +00002006 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
Dan Gohman8dae1382008-09-14 17:21:12 +00002007 if (i) Out << ", ";
Chris Lattner7e708292002-06-25 16:13:24 +00002008 writeOperand(I.getOperand(i), PrintAllTypes);
Chris Lattner00950542001-06-06 20:29:01 +00002009 }
2010 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002011
Chris Lattner7d05c462009-12-28 20:10:43 +00002012 // Print post operand alignment for load/store.
Christopher Lamb43c7f372007-04-22 19:24:39 +00002013 if (isa<LoadInst>(I) && cast<LoadInst>(I).getAlignment()) {
2014 Out << ", align " << cast<LoadInst>(I).getAlignment();
2015 } else if (isa<StoreInst>(I) && cast<StoreInst>(I).getAlignment()) {
2016 Out << ", align " << cast<StoreInst>(I).getAlignment();
2017 }
Chris Lattner00950542001-06-06 20:29:01 +00002018
Chris Lattner7d05c462009-12-28 20:10:43 +00002019 // Print Metadata info.
Nick Lewyckyfa0c54e2010-02-25 06:53:04 +00002020 SmallVector<std::pair<unsigned, MDNode*>, 4> InstMD;
2021 I.getAllMetadata(InstMD);
Erick Tryzelaarf3d5c912010-03-02 05:32:52 +00002022 if (!InstMD.empty()) {
2023 SmallVector<StringRef, 8> MDNames;
2024 I.getType()->getContext().getMDKindNames(MDNames);
2025 for (unsigned i = 0, e = InstMD.size(); i != e; ++i) {
2026 unsigned Kind = InstMD[i].first;
2027 if (Kind < MDNames.size()) {
2028 Out << ", !" << MDNames[Kind];
2029 } else {
2030 Out << ", !<unknown kind #" << Kind << ">";
2031 }
2032 Out << " !" << Machine.getMetadataSlot(InstMD[i].second);
Nick Lewyckyfa0c54e2010-02-25 06:53:04 +00002033 }
Devang Patel7f93f4d2009-10-07 16:37:55 +00002034 }
Chris Lattnere02fa852001-10-13 06:42:36 +00002035 printInfoComment(I);
Chris Lattner00950542001-06-06 20:29:01 +00002036}
2037
Chris Lattner6e6b1802009-12-31 02:13:35 +00002038static void WriteMDNodeComment(const MDNode *Node,
Duncan Sands34727662010-07-12 08:16:59 +00002039 formatted_raw_ostream &Out) {
Chris Lattner6e6b1802009-12-31 02:13:35 +00002040 if (Node->getNumOperands() < 1)
2041 return;
2042 ConstantInt *CI = dyn_cast_or_null<ConstantInt>(Node->getOperand(0));
2043 if (!CI) return;
Dan Gohman66797ff2010-05-07 22:15:24 +00002044 APInt Val = CI->getValue();
2045 APInt Tag = Val & ~APInt(Val.getBitWidth(), LLVMDebugVersionMask);
2046 if (Val.ult(LLVMDebugVersion))
Chris Lattner6e6b1802009-12-31 02:13:35 +00002047 return;
2048
2049 Out.PadToColumn(50);
2050 if (Tag == dwarf::DW_TAG_auto_variable)
2051 Out << "; [ DW_TAG_auto_variable ]";
2052 else if (Tag == dwarf::DW_TAG_arg_variable)
2053 Out << "; [ DW_TAG_arg_variable ]";
2054 else if (Tag == dwarf::DW_TAG_return_variable)
2055 Out << "; [ DW_TAG_return_variable ]";
2056 else if (Tag == dwarf::DW_TAG_vector_type)
2057 Out << "; [ DW_TAG_vector_type ]";
2058 else if (Tag == dwarf::DW_TAG_user_base)
2059 Out << "; [ DW_TAG_user_base ]";
Dan Gohman66797ff2010-05-07 22:15:24 +00002060 else if (Tag.isIntN(32)) {
2061 if (const char *TagName = dwarf::TagString(Tag.getZExtValue()))
2062 Out << "; [ " << TagName << " ]";
2063 }
Chris Lattner6e6b1802009-12-31 02:13:35 +00002064}
2065
2066void AssemblyWriter::writeAllMDNodes() {
2067 SmallVector<const MDNode *, 16> Nodes;
Chris Lattner307c9892009-12-31 02:20:11 +00002068 Nodes.resize(Machine.mdn_size());
2069 for (SlotTracker::mdn_iterator I = Machine.mdn_begin(), E = Machine.mdn_end();
2070 I != E; ++I)
Chris Lattner6e6b1802009-12-31 02:13:35 +00002071 Nodes[I->second] = cast<MDNode>(I->first);
2072
2073 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
2074 Out << '!' << i << " = metadata ";
Chris Lattner2b4b1e22009-12-31 02:27:30 +00002075 printMDNodeBody(Nodes[i]);
Chris Lattner6e6b1802009-12-31 02:13:35 +00002076 }
2077}
2078
2079void AssemblyWriter::printMDNodeBody(const MDNode *Node) {
Chris Lattner85b19122009-12-31 02:31:59 +00002080 WriteMDNodeBodyInternal(Out, Node, &TypePrinter, &Machine);
Chris Lattner6e6b1802009-12-31 02:13:35 +00002081 WriteMDNodeComment(Node, Out);
2082 Out << "\n";
2083}
Chris Lattner00950542001-06-06 20:29:01 +00002084
2085//===----------------------------------------------------------------------===//
2086// External Interface declarations
2087//===----------------------------------------------------------------------===//
2088
Dan Gohman683e9222009-08-12 17:23:50 +00002089void Module::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW) const {
Chris Lattner0d9574a2008-08-19 04:26:57 +00002090 SlotTracker SlotTable(this);
Dan Gohman683e9222009-08-12 17:23:50 +00002091 formatted_raw_ostream OS(ROS);
Chris Lattner944fac72008-08-23 22:23:09 +00002092 AssemblyWriter W(OS, SlotTable, this, AAW);
Chris Lattnerbd72b322009-12-31 02:23:35 +00002093 W.printModule(this);
Chris Lattner00950542001-06-06 20:29:01 +00002094}
2095
Chris Lattner6d4306e2009-02-28 21:11:05 +00002096void Type::print(raw_ostream &OS) const {
2097 if (this == 0) {
2098 OS << "<null Type>";
2099 return;
2100 }
Chris Lattnere9fa33e2009-02-28 23:20:19 +00002101 TypePrinting().print(this, OS);
Chris Lattner75cf7cf2002-04-08 22:03:40 +00002102}
2103
Dan Gohman683e9222009-08-12 17:23:50 +00002104void Value::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW) const {
Chris Lattner944fac72008-08-23 22:23:09 +00002105 if (this == 0) {
Dan Gohman1220e102009-08-12 20:56:03 +00002106 ROS << "printing a <null> value\n";
Chris Lattner944fac72008-08-23 22:23:09 +00002107 return;
2108 }
Dan Gohman1220e102009-08-12 20:56:03 +00002109 formatted_raw_ostream OS(ROS);
Chris Lattner944fac72008-08-23 22:23:09 +00002110 if (const Instruction *I = dyn_cast<Instruction>(this)) {
2111 const Function *F = I->getParent() ? I->getParent()->getParent() : 0;
2112 SlotTracker SlotTable(F);
Chris Lattnerdbe85bf2009-12-31 08:23:09 +00002113 AssemblyWriter W(OS, SlotTable, getModuleFromVal(I), AAW);
Chris Lattnerbd72b322009-12-31 02:23:35 +00002114 W.printInstruction(*I);
Chris Lattner944fac72008-08-23 22:23:09 +00002115 } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(this)) {
2116 SlotTracker SlotTable(BB->getParent());
Chris Lattner6e6b1802009-12-31 02:13:35 +00002117 AssemblyWriter W(OS, SlotTable, getModuleFromVal(BB), AAW);
Chris Lattnerbd72b322009-12-31 02:23:35 +00002118 W.printBasicBlock(BB);
Chris Lattner944fac72008-08-23 22:23:09 +00002119 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(this)) {
2120 SlotTracker SlotTable(GV->getParent());
Dan Gohmanba0941f2009-04-20 16:10:33 +00002121 AssemblyWriter W(OS, SlotTable, GV->getParent(), AAW);
Chris Lattnerbd72b322009-12-31 02:23:35 +00002122 if (const GlobalVariable *V = dyn_cast<GlobalVariable>(GV))
2123 W.printGlobal(V);
2124 else if (const Function *F = dyn_cast<Function>(GV))
2125 W.printFunction(F);
2126 else
2127 W.printAlias(cast<GlobalAlias>(GV));
Devang Patelfcd65ae2009-07-01 20:59:15 +00002128 } else if (const MDNode *N = dyn_cast<MDNode>(this)) {
Victor Hernandez8fffff52010-01-20 04:45:57 +00002129 const Function *F = N->getFunction();
Victor Hernandez559588b2010-01-14 01:47:37 +00002130 SlotTracker SlotTable(F);
Dan Gohman79b78a42010-07-14 21:12:44 +00002131 AssemblyWriter W(OS, SlotTable, F ? F->getParent() : 0, AAW);
Chris Lattner6e6b1802009-12-31 02:13:35 +00002132 W.printMDNodeBody(N);
Devang Patelc29d5b32009-07-30 00:02:57 +00002133 } else if (const NamedMDNode *N = dyn_cast<NamedMDNode>(this)) {
Chris Lattner38cf02e2009-12-31 01:36:50 +00002134 SlotTracker SlotTable(N->getParent());
Chris Lattnerfdb33562009-12-31 01:54:05 +00002135 AssemblyWriter W(OS, SlotTable, N->getParent(), AAW);
2136 W.printNamedMDNode(N);
Chris Lattner944fac72008-08-23 22:23:09 +00002137 } else if (const Constant *C = dyn_cast<Constant>(this)) {
Chris Lattnere9fa33e2009-02-28 23:20:19 +00002138 TypePrinting TypePrinter;
Chris Lattner0f7364b2009-02-28 21:26:53 +00002139 TypePrinter.print(C->getType(), OS);
Chris Lattner6d4306e2009-02-28 21:11:05 +00002140 OS << ' ';
Dan Gohman40cf12f2010-07-14 20:57:55 +00002141 WriteConstantInternal(OS, C, TypePrinter, 0);
Chris Lattner4a3d3a52009-12-31 01:41:14 +00002142 } else if (isa<InlineAsm>(this) || isa<MDString>(this) ||
2143 isa<Argument>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00002144 WriteAsOperand(OS, this, true, 0);
2145 } else {
Dan Gohmancd26ec52009-09-23 01:33:16 +00002146 // Otherwise we don't know what it is. Call the virtual function to
2147 // allow a subclass to print itself.
2148 printCustom(OS);
Chris Lattner944fac72008-08-23 22:23:09 +00002149 }
2150}
2151
Dan Gohmancd26ec52009-09-23 01:33:16 +00002152// Value::printCustom - subclasses should override this to implement printing.
2153void Value::printCustom(raw_ostream &OS) const {
2154 llvm_unreachable("Unknown value to print out!");
2155}
2156
Chris Lattner7059e532008-08-25 17:03:15 +00002157// Value::dump - allow easy printing of Values from the debugger.
David Greened865e022010-01-05 01:29:26 +00002158void Value::dump() const { print(dbgs()); dbgs() << '\n'; }
Reid Spencerfa452c02004-05-25 18:14:38 +00002159
Chris Lattner7059e532008-08-25 17:03:15 +00002160// Type::dump - allow easy printing of Types from the debugger.
Chris Lattner795daec2008-10-01 20:16:19 +00002161// This one uses type names from the given context module
2162void Type::dump(const Module *Context) const {
David Greened865e022010-01-05 01:29:26 +00002163 WriteTypeSymbolic(dbgs(), this, Context);
2164 dbgs() << '\n';
Chris Lattner795daec2008-10-01 20:16:19 +00002165}
2166
Chris Lattnerc2871372009-02-28 21:05:51 +00002167// Type::dump - allow easy printing of Types from the debugger.
2168void Type::dump() const { dump(0); }
2169
Chris Lattner7059e532008-08-25 17:03:15 +00002170// Module::dump() - Allow printing of Modules from the debugger.
David Greened865e022010-01-05 01:29:26 +00002171void Module::dump() const { print(dbgs(), 0); }