blob: aedb86b3eed0d4924b4117e780ff3e20cc7fdd1a [file] [log] [blame]
Chris Lattnerf7e79482002-04-07 22:31:46 +00001//===-- AsmWriter.cpp - Printing LLVM as an assembly file -----------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
10// This library implements the functionality defined in llvm/Assembly/Writer.h
11//
Chris Lattner189088e2002-04-12 18:21:53 +000012// Note that these routines must be extremely tolerant of various errors in the
Chris Lattnerf70da102003-05-08 02:44:12 +000013// LLVM code, because it can be used for debugging transformations.
Chris Lattner189088e2002-04-12 18:21:53 +000014//
Chris Lattner2f7c9632001-06-06 20:29:01 +000015//===----------------------------------------------------------------------===//
16
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +000017#include "llvm/Assembly/Writer.h"
Chris Lattner7f8845a2002-07-23 18:07:49 +000018#include "llvm/Assembly/PrintModulePass.h"
Chris Lattnerad074cb2010-09-02 23:09:42 +000019#include "llvm/Assembly/AssemblyAnnotationWriter.h"
Erick Tryzelaar72a37132010-03-02 05:32:52 +000020#include "llvm/LLVMContext.h"
Chris Lattnerf7b6d312005-05-06 20:26:43 +000021#include "llvm/CallingConv.h"
Chris Lattnerc70b3f62004-01-20 19:50:34 +000022#include "llvm/Constants.h"
Bill Wendling5cb50c52012-06-28 00:41:44 +000023#include "llvm/DebugInfo.h"
Chris Lattner913d18f2002-04-29 18:46:50 +000024#include "llvm/DerivedTypes.h"
Chris Lattner8bbcda22006-01-25 18:57:27 +000025#include "llvm/InlineAsm.h"
Chris Lattner2f2aa2b2009-12-28 23:41:32 +000026#include "llvm/IntrinsicInst.h"
Dan Gohman0ebd6962009-07-20 21:19:07 +000027#include "llvm/Operator.h"
Chris Lattnerc70b3f62004-01-20 19:50:34 +000028#include "llvm/Module.h"
Reid Spencer3aaaa0b2007-02-05 20:47:22 +000029#include "llvm/ValueSymbolTable.h"
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000030#include "llvm/ADT/DenseMap.h"
Benjamin Kramerb17c5862010-01-29 14:42:22 +000031#include "llvm/ADT/SmallString.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000032#include "llvm/ADT/StringExtras.h"
33#include "llvm/ADT/STLExtras.h"
Bill Wendlingdfc91892006-11-28 02:09:03 +000034#include "llvm/Support/CFG.h"
David Greenec7f9b122010-01-05 01:29:26 +000035#include "llvm/Support/Debug.h"
Devang Patel711ab5b2009-09-30 20:16:54 +000036#include "llvm/Support/Dwarf.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000037#include "llvm/Support/ErrorHandling.h"
Jim Laskeyb74c6662005-08-17 19:34:49 +000038#include "llvm/Support/MathExtras.h"
Dan Gohmane2745262009-08-12 17:23:50 +000039#include "llvm/Support/FormattedStream.h"
Chris Lattnerfee714f2001-09-07 16:36:04 +000040#include <algorithm>
Reid Spencerbdf03b42007-05-22 19:27:35 +000041#include <cctype>
Chris Lattner189d19f2003-11-21 20:23:48 +000042using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000043
Reid Spencer294715b2005-05-15 16:13:11 +000044// Make virtual table appear in this compilation unit.
45AssemblyAnnotationWriter::~AssemblyAnnotationWriter() {}
46
Chris Lattner3eee99c2008-08-19 04:36:02 +000047//===----------------------------------------------------------------------===//
48// Helper Functions
49//===----------------------------------------------------------------------===//
50
51static const Module *getModuleFromVal(const Value *V) {
52 if (const Argument *MA = dyn_cast<Argument>(V))
53 return MA->getParent() ? MA->getParent()->getParent() : 0;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +000054
Chris Lattner3eee99c2008-08-19 04:36:02 +000055 if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
56 return BB->getParent() ? BB->getParent()->getParent() : 0;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +000057
Chris Lattner3eee99c2008-08-19 04:36:02 +000058 if (const Instruction *I = dyn_cast<Instruction>(V)) {
59 const Function *M = I->getParent() ? I->getParent()->getParent() : 0;
60 return M ? M->getParent() : 0;
61 }
Andrew Trickec4b6e72011-09-30 19:48:58 +000062
Chris Lattner3eee99c2008-08-19 04:36:02 +000063 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
64 return GV->getParent();
65 return 0;
66}
67
Daniel Dunbardb0b70a2008-10-28 19:33:02 +000068// PrintEscapedString - Print each character of the specified string, escaping
69// it if it is not printable or if it is an escape char.
Chris Lattner9380b812010-07-07 23:16:37 +000070static void PrintEscapedString(StringRef Name, raw_ostream &Out) {
Daniel Dunbare03eecb2009-07-25 23:55:21 +000071 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
72 unsigned char C = Name[i];
Nick Lewycky5aa592a2009-03-15 06:39:52 +000073 if (isprint(C) && C != '\\' && C != '"')
Daniel Dunbardb0b70a2008-10-28 19:33:02 +000074 Out << C;
75 else
76 Out << '\\' << hexdigit(C >> 4) << hexdigit(C & 0x0F);
77 }
78}
79
Chris Lattner3eee99c2008-08-19 04:36:02 +000080enum PrefixType {
81 GlobalPrefix,
82 LabelPrefix,
Daniel Dunbar389529a2008-10-14 23:28:09 +000083 LocalPrefix,
84 NoPrefix
Chris Lattner3eee99c2008-08-19 04:36:02 +000085};
86
87/// PrintLLVMName - Turn the specified name into an 'LLVM name', which is either
88/// prefixed with % (if the string only contains simple characters) or is
89/// surrounded with ""'s (if it has special chars in it). Print it out.
Benjamin Kramer92d89982010-07-14 22:38:02 +000090static void PrintLLVMName(raw_ostream &OS, StringRef Name, PrefixType Prefix) {
Jay Foadf7adb322011-04-24 14:30:00 +000091 assert(!Name.empty() && "Cannot get empty name!");
Chris Lattner3eee99c2008-08-19 04:36:02 +000092 switch (Prefix) {
Daniel Dunbar389529a2008-10-14 23:28:09 +000093 case NoPrefix: break;
Chris Lattner1508d3f2008-08-19 05:16:28 +000094 case GlobalPrefix: OS << '@'; break;
95 case LabelPrefix: break;
96 case LocalPrefix: OS << '%'; break;
Nick Lewycky063699a2009-03-19 06:31:22 +000097 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +000098
Chris Lattner3eee99c2008-08-19 04:36:02 +000099 // Scan the name to see if it needs quotes first.
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000100 bool NeedsQuotes = isdigit(Name[0]);
Chris Lattner3eee99c2008-08-19 04:36:02 +0000101 if (!NeedsQuotes) {
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000102 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
Aaron Ballmaned9b0a92012-07-16 16:18:18 +0000103 // By making this unsigned, the value passed in to isalnum will always be
104 // in the range 0-255. This is important when building with MSVC because
105 // its implementation will assert. This situation can arise when dealing
106 // with UTF-8 multibyte characters.
107 unsigned char C = Name[i];
Chris Lattner3eee99c2008-08-19 04:36:02 +0000108 if (!isalnum(C) && C != '-' && C != '.' && C != '_') {
109 NeedsQuotes = true;
110 break;
111 }
112 }
113 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000114
Chris Lattner3eee99c2008-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 Dunbare03eecb2009-07-25 23:55:21 +0000117 OS << Name;
Chris Lattner3eee99c2008-08-19 04:36:02 +0000118 return;
119 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000120
Chris Lattner3eee99c2008-08-19 04:36:02 +0000121 // Okay, we need quotes. Output the quotes and escape any scary characters as
122 // needed.
123 OS << '"';
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000124 PrintEscapedString(Name, OS);
Chris Lattner3eee99c2008-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 Gohman12dad632009-08-12 20:56:03 +0000131static void PrintLLVMName(raw_ostream &OS, const Value *V) {
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000132 PrintLLVMName(OS, V->getName(),
Chris Lattner3eee99c2008-08-19 04:36:02 +0000133 isa<GlobalValue>(V) ? GlobalPrefix : LocalPrefix);
134}
135
Chris Lattner0f578952009-02-28 20:25:14 +0000136//===----------------------------------------------------------------------===//
137// TypePrinting Class: Type printing machinery
138//===----------------------------------------------------------------------===//
139
Chris Lattner3a16c712011-06-18 21:23:04 +0000140/// TypePrinting - Type printing machinery.
141namespace {
142class TypePrinting {
Chris Lattner3a16c712011-06-18 21:23:04 +0000143 TypePrinting(const TypePrinting &); // DO NOT IMPLEMENT
144 void operator=(const TypePrinting&); // DO NOT IMPLEMENT
145public:
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000146
147 /// NamedTypes - The named types that are used by the current module.
148 std::vector<StructType*> NamedTypes;
Andrew Trickec4b6e72011-09-30 19:48:58 +0000149
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000150 /// NumberedTypes - The numbered types, along with their value.
151 DenseMap<StructType*, unsigned> NumberedTypes;
Andrew Trickec4b6e72011-09-30 19:48:58 +0000152
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000153
Chris Lattner3a16c712011-06-18 21:23:04 +0000154 TypePrinting() {}
155 ~TypePrinting() {}
Andrew Trickec4b6e72011-09-30 19:48:58 +0000156
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000157 void incorporateTypes(const Module &M);
Andrew Trickec4b6e72011-09-30 19:48:58 +0000158
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000159 void print(Type *Ty, raw_ostream &OS);
Andrew Trickec4b6e72011-09-30 19:48:58 +0000160
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000161 void printStructBody(StructType *Ty, raw_ostream &OS);
Chris Lattner3a16c712011-06-18 21:23:04 +0000162};
163} // end anonymous namespace.
Chris Lattner3ad46822009-02-28 22:34:45 +0000164
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000165
166void TypePrinting::incorporateTypes(const Module &M) {
167 M.findUsedStructTypes(NamedTypes);
Andrew Trickec4b6e72011-09-30 19:48:58 +0000168
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000169 // The list of struct types we got back includes all the struct types, split
170 // the unnamed ones out to a numbering and remove the anonymous structs.
171 unsigned NextNumber = 0;
Andrew Trickec4b6e72011-09-30 19:48:58 +0000172
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000173 std::vector<StructType*>::iterator NextToUse = NamedTypes.begin(), I, E;
174 for (I = NamedTypes.begin(), E = NamedTypes.end(); I != E; ++I) {
175 StructType *STy = *I;
Andrew Trickec4b6e72011-09-30 19:48:58 +0000176
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000177 // Ignore anonymous types.
Chris Lattner01beceb2011-08-12 18:07:07 +0000178 if (STy->isLiteral())
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000179 continue;
Andrew Trickec4b6e72011-09-30 19:48:58 +0000180
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000181 if (STy->getName().empty())
182 NumberedTypes[STy] = NextNumber++;
183 else
184 *NextToUse++ = STy;
185 }
Andrew Trickec4b6e72011-09-30 19:48:58 +0000186
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000187 NamedTypes.erase(NextToUse, NamedTypes.end());
188}
189
190
Chris Lattner40959d02009-02-28 20:49:40 +0000191/// CalcTypeName - Write the specified type to the specified raw_ostream, making
192/// use of type names or up references to shorten the type name where possible.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000193void TypePrinting::print(Type *Ty, raw_ostream &OS) {
Chris Lattner0f578952009-02-28 20:25:14 +0000194 switch (Ty->getTypeID()) {
Chris Lattner06a23212009-02-28 21:27:31 +0000195 case Type::VoidTyID: OS << "void"; break;
Dan Gohman518cda42011-12-17 00:04:22 +0000196 case Type::HalfTyID: OS << "half"; break;
Chris Lattner06a23212009-02-28 21:27:31 +0000197 case Type::FloatTyID: OS << "float"; break;
198 case Type::DoubleTyID: OS << "double"; break;
199 case Type::X86_FP80TyID: OS << "x86_fp80"; break;
200 case Type::FP128TyID: OS << "fp128"; break;
201 case Type::PPC_FP128TyID: OS << "ppc_fp128"; break;
202 case Type::LabelTyID: OS << "label"; break;
Nick Lewyckyadbc2842009-05-30 05:06:04 +0000203 case Type::MetadataTyID: OS << "metadata"; break;
Dale Johannesenbaa5d042010-09-10 20:55:01 +0000204 case Type::X86_MMXTyID: OS << "x86_mmx"; break;
Chris Lattner68318b12009-02-28 21:18:43 +0000205 case Type::IntegerTyID:
Chris Lattner06a23212009-02-28 21:27:31 +0000206 OS << 'i' << cast<IntegerType>(Ty)->getBitWidth();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000207 return;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000208
Chris Lattner07d88292009-02-28 20:35:42 +0000209 case Type::FunctionTyID: {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000210 FunctionType *FTy = cast<FunctionType>(Ty);
211 print(FTy->getReturnType(), OS);
Chris Lattner06a23212009-02-28 21:27:31 +0000212 OS << " (";
Chris Lattner07d88292009-02-28 20:35:42 +0000213 for (FunctionType::param_iterator I = FTy->param_begin(),
214 E = FTy->param_end(); I != E; ++I) {
215 if (I != FTy->param_begin())
Chris Lattner06a23212009-02-28 21:27:31 +0000216 OS << ", ";
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000217 print(*I, OS);
Chris Lattner0f578952009-02-28 20:25:14 +0000218 }
Chris Lattner07d88292009-02-28 20:35:42 +0000219 if (FTy->isVarArg()) {
Chris Lattner06a23212009-02-28 21:27:31 +0000220 if (FTy->getNumParams()) OS << ", ";
221 OS << "...";
Chris Lattner0f578952009-02-28 20:25:14 +0000222 }
Chris Lattner06a23212009-02-28 21:27:31 +0000223 OS << ')';
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000224 return;
Chris Lattner07d88292009-02-28 20:35:42 +0000225 }
226 case Type::StructTyID: {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000227 StructType *STy = cast<StructType>(Ty);
Andrew Trickec4b6e72011-09-30 19:48:58 +0000228
Chris Lattner01beceb2011-08-12 18:07:07 +0000229 if (STy->isLiteral())
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000230 return printStructBody(STy, OS);
231
232 if (!STy->getName().empty())
233 return PrintLLVMName(OS, STy->getName(), LocalPrefix);
Andrew Trickec4b6e72011-09-30 19:48:58 +0000234
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000235 DenseMap<StructType*, unsigned>::iterator I = NumberedTypes.find(STy);
236 if (I != NumberedTypes.end())
237 OS << '%' << I->second;
238 else // Not enumerated, print the hex address.
Benjamin Kramer4e8b4632011-11-02 17:24:36 +0000239 OS << "%\"type " << STy << '\"';
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000240 return;
Chris Lattner07d88292009-02-28 20:35:42 +0000241 }
242 case Type::PointerTyID: {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000243 PointerType *PTy = cast<PointerType>(Ty);
244 print(PTy->getElementType(), OS);
Chris Lattner07d88292009-02-28 20:35:42 +0000245 if (unsigned AddressSpace = PTy->getAddressSpace())
Chris Lattner06a23212009-02-28 21:27:31 +0000246 OS << " addrspace(" << AddressSpace << ')';
247 OS << '*';
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000248 return;
Chris Lattner07d88292009-02-28 20:35:42 +0000249 }
250 case Type::ArrayTyID: {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000251 ArrayType *ATy = cast<ArrayType>(Ty);
Chris Lattner06a23212009-02-28 21:27:31 +0000252 OS << '[' << ATy->getNumElements() << " x ";
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000253 print(ATy->getElementType(), OS);
Chris Lattner06a23212009-02-28 21:27:31 +0000254 OS << ']';
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000255 return;
Chris Lattner07d88292009-02-28 20:35:42 +0000256 }
257 case Type::VectorTyID: {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000258 VectorType *PTy = cast<VectorType>(Ty);
Chris Lattner06a23212009-02-28 21:27:31 +0000259 OS << "<" << PTy->getNumElements() << " x ";
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000260 print(PTy->getElementType(), OS);
Chris Lattner06a23212009-02-28 21:27:31 +0000261 OS << '>';
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000262 return;
Chris Lattner07d88292009-02-28 20:35:42 +0000263 }
Chris Lattner07d88292009-02-28 20:35:42 +0000264 default:
Chris Lattner06a23212009-02-28 21:27:31 +0000265 OS << "<unrecognized-type>";
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000266 return;
Chris Lattner0f578952009-02-28 20:25:14 +0000267 }
Chris Lattner0f578952009-02-28 20:25:14 +0000268}
269
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000270void TypePrinting::printStructBody(StructType *STy, raw_ostream &OS) {
271 if (STy->isOpaque()) {
272 OS << "opaque";
273 return;
Chris Lattner0f578952009-02-28 20:25:14 +0000274 }
Andrew Trickec4b6e72011-09-30 19:48:58 +0000275
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000276 if (STy->isPacked())
277 OS << '<';
Andrew Trickec4b6e72011-09-30 19:48:58 +0000278
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000279 if (STy->getNumElements() == 0) {
280 OS << "{}";
281 } else {
282 StructType::element_iterator I = STy->element_begin();
283 OS << "{ ";
284 print(*I++, OS);
285 for (StructType::element_iterator E = STy->element_end(); I != E; ++I) {
286 OS << ", ";
287 print(*I, OS);
Chris Lattner3243ea12009-03-01 00:03:38 +0000288 }
Andrew Trickec4b6e72011-09-30 19:48:58 +0000289
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000290 OS << " }";
Chris Lattner92c5c122009-02-28 23:20:19 +0000291 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000292 if (STy->isPacked())
293 OS << '>';
Chris Lattner92c5c122009-02-28 23:20:19 +0000294}
295
Chris Lattner0f578952009-02-28 20:25:14 +0000296
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000297
Chris Lattner3eee99c2008-08-19 04:36:02 +0000298//===----------------------------------------------------------------------===//
299// SlotTracker Class: Enumerate slot numbers for unnamed values
300//===----------------------------------------------------------------------===//
301
Chris Lattner3ee58762008-08-19 04:28:07 +0000302namespace {
303
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000304/// This class provides computation of slot numbers for LLVM Assembly writing.
Chris Lattner393b7cd2008-08-17 04:17:45 +0000305///
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000306class SlotTracker {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000307public:
Devang Patel983c6b12009-07-08 21:44:25 +0000308 /// ValueMap - A mapping of Values to slot numbers.
Chris Lattnera204d412008-08-17 17:25:25 +0000309 typedef DenseMap<const Value*, unsigned> ValueMap;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000310
311private:
Devang Patel983c6b12009-07-08 21:44:25 +0000312 /// TheModule - The module for which we are holding slot numbers.
Chris Lattner393b7cd2008-08-17 04:17:45 +0000313 const Module* TheModule;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000314
Devang Patel983c6b12009-07-08 21:44:25 +0000315 /// TheFunction - The function for which we are holding slot numbers.
Chris Lattner393b7cd2008-08-17 04:17:45 +0000316 const Function* TheFunction;
317 bool FunctionProcessed;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000318
Jay Foaddbb221d2011-07-11 07:28:49 +0000319 /// mMap - The slot map for the module level data.
Chris Lattner393b7cd2008-08-17 04:17:45 +0000320 ValueMap mMap;
321 unsigned mNext;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000322
Jay Foaddbb221d2011-07-11 07:28:49 +0000323 /// fMap - The slot map for the function level data.
Chris Lattner393b7cd2008-08-17 04:17:45 +0000324 ValueMap fMap;
325 unsigned fNext;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000326
Devang Patel983c6b12009-07-08 21:44:25 +0000327 /// mdnMap - Map for MDNodes.
Chris Lattnercf4a76e2009-12-31 02:20:11 +0000328 DenseMap<const MDNode*, unsigned> mdnMap;
Devang Patel983c6b12009-07-08 21:44:25 +0000329 unsigned mdnNext;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000330public:
Chris Lattner393b7cd2008-08-17 04:17:45 +0000331 /// Construct from a module
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000332 explicit SlotTracker(const Module *M);
Chris Lattner393b7cd2008-08-17 04:17:45 +0000333 /// Construct from a function, starting out in incorp state.
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000334 explicit SlotTracker(const Function *F);
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000335
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000336 /// Return the slot number of the specified value in it's type
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000337 /// plane. If something is not in the SlotTracker, return -1.
Chris Lattner5e043322007-01-11 03:54:27 +0000338 int getLocalSlot(const Value *V);
339 int getGlobalSlot(const GlobalValue *V);
Devang Patel983c6b12009-07-08 21:44:25 +0000340 int getMetadataSlot(const MDNode *N);
Reid Spencer8beac692004-06-09 15:26:53 +0000341
Misha Brukmanb1c93172005-04-21 23:48:37 +0000342 /// If you'd like to deal with a function instead of just a module, use
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000343 /// this method to get its data into the SlotTracker.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000344 void incorporateFunction(const Function *F) {
345 TheFunction = F;
Reid Spencerb0ac8c42004-08-16 07:46:33 +0000346 FunctionProcessed = false;
347 }
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000348
Misha Brukmanb1c93172005-04-21 23:48:37 +0000349 /// After calling incorporateFunction, use this method to remove the
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000350 /// most recently incorporated function from the SlotTracker. This
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000351 /// will reset the state of the machine back to just the module contents.
352 void purgeFunction();
353
Devang Patel983c6b12009-07-08 21:44:25 +0000354 /// MDNode map iterators.
Chris Lattnercf4a76e2009-12-31 02:20:11 +0000355 typedef DenseMap<const MDNode*, unsigned>::iterator mdn_iterator;
356 mdn_iterator mdn_begin() { return mdnMap.begin(); }
357 mdn_iterator mdn_end() { return mdnMap.end(); }
358 unsigned mdn_size() const { return mdnMap.size(); }
359 bool mdn_empty() const { return mdnMap.empty(); }
Devang Patel983c6b12009-07-08 21:44:25 +0000360
Reid Spencer56010e42004-05-26 21:56:09 +0000361 /// This function does the actual initialization.
362 inline void initialize();
363
Devang Patel983c6b12009-07-08 21:44:25 +0000364 // Implementation Details
365private:
Chris Lattnerea862a32007-01-09 07:55:49 +0000366 /// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
367 void CreateModuleSlot(const GlobalValue *V);
Devang Patel983c6b12009-07-08 21:44:25 +0000368
369 /// CreateMetadataSlot - Insert the specified MDNode* into the slot table.
370 void CreateMetadataSlot(const MDNode *N);
371
Chris Lattnerea862a32007-01-09 07:55:49 +0000372 /// CreateFunctionSlot - Insert the specified Value* into the slot table.
373 void CreateFunctionSlot(const Value *V);
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000374
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000375 /// Add all of the module level global variables (and their initializers)
376 /// and function declarations, but not the contents of those functions.
377 void processModule();
378
Devang Patel983c6b12009-07-08 21:44:25 +0000379 /// Add all of the functions arguments, basic blocks, and instructions.
Reid Spencer56010e42004-05-26 21:56:09 +0000380 void processFunction();
381
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000382 SlotTracker(const SlotTracker &); // DO NOT IMPLEMENT
383 void operator=(const SlotTracker &); // DO NOT IMPLEMENT
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000384};
385
Chris Lattner3ee58762008-08-19 04:28:07 +0000386} // end anonymous namespace
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000387
Chris Lattner3eee99c2008-08-19 04:36:02 +0000388
389static SlotTracker *createSlotTracker(const Value *V) {
390 if (const Argument *FA = dyn_cast<Argument>(V))
391 return new SlotTracker(FA->getParent());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000392
Chris Lattner3eee99c2008-08-19 04:36:02 +0000393 if (const Instruction *I = dyn_cast<Instruction>(V))
Andrew Trick2f0cbf62011-09-30 19:50:40 +0000394 if (I->getParent())
395 return new SlotTracker(I->getParent()->getParent());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000396
Chris Lattner3eee99c2008-08-19 04:36:02 +0000397 if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
398 return new SlotTracker(BB->getParent());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000399
Chris Lattner3eee99c2008-08-19 04:36:02 +0000400 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
401 return new SlotTracker(GV->getParent());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000402
Chris Lattner3eee99c2008-08-19 04:36:02 +0000403 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V))
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000404 return new SlotTracker(GA->getParent());
405
Chris Lattner3eee99c2008-08-19 04:36:02 +0000406 if (const Function *Func = dyn_cast<Function>(V))
407 return new SlotTracker(Func);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000408
Dan Gohmana2489d12010-07-20 23:55:01 +0000409 if (const MDNode *MD = dyn_cast<MDNode>(V)) {
410 if (!MD->isFunctionLocal())
411 return new SlotTracker(MD->getFunction());
412
Dale Johannesen7b1a7ed2010-01-13 00:00:24 +0000413 return new SlotTracker((Function *)0);
Dan Gohmana2489d12010-07-20 23:55:01 +0000414 }
Dale Johannesen7b1a7ed2010-01-13 00:00:24 +0000415
Chris Lattner3eee99c2008-08-19 04:36:02 +0000416 return 0;
417}
418
419#if 0
David Greenec7f9b122010-01-05 01:29:26 +0000420#define ST_DEBUG(X) dbgs() << X
Chris Lattner3eee99c2008-08-19 04:36:02 +0000421#else
Chris Lattner604e3512008-08-19 04:47:09 +0000422#define ST_DEBUG(X)
Chris Lattner3eee99c2008-08-19 04:36:02 +0000423#endif
424
425// Module level constructor. Causes the contents of the Module (sans functions)
426// to be added to the slot table.
427SlotTracker::SlotTracker(const Module *M)
Andrew Trickec4b6e72011-09-30 19:48:58 +0000428 : TheModule(M), TheFunction(0), FunctionProcessed(false),
Chris Lattnerbe2de792009-12-31 01:36:50 +0000429 mNext(0), fNext(0), mdnNext(0) {
Chris Lattner3eee99c2008-08-19 04:36:02 +0000430}
431
432// Function level constructor. Causes the contents of the Module and the one
433// function provided to be added to the slot table.
434SlotTracker::SlotTracker(const Function *F)
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000435 : TheModule(F ? F->getParent() : 0), TheFunction(F), FunctionProcessed(false),
Chris Lattnerbddea6a2009-12-31 02:13:35 +0000436 mNext(0), fNext(0), mdnNext(0) {
Chris Lattner3eee99c2008-08-19 04:36:02 +0000437}
438
439inline void SlotTracker::initialize() {
440 if (TheModule) {
441 processModule();
442 TheModule = 0; ///< Prevent re-processing next time we're called.
443 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000444
Chris Lattner3eee99c2008-08-19 04:36:02 +0000445 if (TheFunction && !FunctionProcessed)
446 processFunction();
447}
448
449// Iterate through all the global variables, functions, and global
450// variable initializers and create slots for them.
451void SlotTracker::processModule() {
Chris Lattner604e3512008-08-19 04:47:09 +0000452 ST_DEBUG("begin processModule!\n");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000453
Chris Lattner3eee99c2008-08-19 04:36:02 +0000454 // Add all of the unnamed global variables to the value table.
455 for (Module::const_global_iterator I = TheModule->global_begin(),
Devang Patel983c6b12009-07-08 21:44:25 +0000456 E = TheModule->global_end(); I != E; ++I) {
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000457 if (!I->hasName())
Chris Lattner3eee99c2008-08-19 04:36:02 +0000458 CreateModuleSlot(I);
Devang Patel983c6b12009-07-08 21:44:25 +0000459 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000460
Devang Patel23e68302009-07-29 22:04:47 +0000461 // Add metadata used by named metadata.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000462 for (Module::const_named_metadata_iterator
Devang Patel23e68302009-07-29 22:04:47 +0000463 I = TheModule->named_metadata_begin(),
464 E = TheModule->named_metadata_end(); I != E; ++I) {
465 const NamedMDNode *NMD = I;
Dan Gohman093cb792010-07-21 18:54:18 +0000466 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
467 CreateMetadataSlot(NMD->getOperand(i));
Devang Patel23e68302009-07-29 22:04:47 +0000468 }
469
Chris Lattner3eee99c2008-08-19 04:36:02 +0000470 // Add all the unnamed functions to the table.
471 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
472 I != E; ++I)
473 if (!I->hasName())
474 CreateModuleSlot(I);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000475
Chris Lattner604e3512008-08-19 04:47:09 +0000476 ST_DEBUG("end processModule!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000477}
478
Chris Lattner3eee99c2008-08-19 04:36:02 +0000479// Process the arguments, basic blocks, and instructions of a function.
480void SlotTracker::processFunction() {
Chris Lattner604e3512008-08-19 04:47:09 +0000481 ST_DEBUG("begin processFunction!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000482 fNext = 0;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000483
Chris Lattner3eee99c2008-08-19 04:36:02 +0000484 // Add all the function arguments with no names.
485 for(Function::const_arg_iterator AI = TheFunction->arg_begin(),
486 AE = TheFunction->arg_end(); AI != AE; ++AI)
487 if (!AI->hasName())
488 CreateFunctionSlot(AI);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000489
Chris Lattner604e3512008-08-19 04:47:09 +0000490 ST_DEBUG("Inserting Instructions:\n");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000491
Chris Lattnerbddea6a2009-12-31 02:13:35 +0000492 SmallVector<std::pair<unsigned, MDNode*>, 4> MDForInst;
Devang Pateldec23fd2009-09-16 20:21:17 +0000493
Chris Lattner3eee99c2008-08-19 04:36:02 +0000494 // Add all of the basic blocks and instructions with no names.
495 for (Function::const_iterator BB = TheFunction->begin(),
496 E = TheFunction->end(); BB != E; ++BB) {
497 if (!BB->hasName())
498 CreateFunctionSlot(BB);
Andrew Trickec4b6e72011-09-30 19:48:58 +0000499
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000500 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E;
Devang Patel983c6b12009-07-08 21:44:25 +0000501 ++I) {
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000502 if (!I->getType()->isVoidTy() && !I->hasName())
Chris Lattner3eee99c2008-08-19 04:36:02 +0000503 CreateFunctionSlot(I);
Andrew Trickec4b6e72011-09-30 19:48:58 +0000504
Chris Lattner58aff8f2010-05-10 20:53:17 +0000505 // Intrinsics can directly use metadata. We allow direct calls to any
506 // llvm.foo function here, because the target may not be linked into the
507 // optimizer.
508 if (const CallInst *CI = dyn_cast<CallInst>(I)) {
509 if (Function *F = CI->getCalledFunction())
510 if (F->getName().startswith("llvm."))
511 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
512 if (MDNode *N = dyn_cast_or_null<MDNode>(I->getOperand(i)))
513 CreateMetadataSlot(N);
514 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000515
Devang Pateldec23fd2009-09-16 20:21:17 +0000516 // Process metadata attached with this instruction.
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000517 I->getAllMetadata(MDForInst);
518 for (unsigned i = 0, e = MDForInst.size(); i != e; ++i)
519 CreateMetadataSlot(MDForInst[i].second);
Chris Lattnerbddea6a2009-12-31 02:13:35 +0000520 MDForInst.clear();
Devang Patel983c6b12009-07-08 21:44:25 +0000521 }
Chris Lattner3eee99c2008-08-19 04:36:02 +0000522 }
Devang Pateldec23fd2009-09-16 20:21:17 +0000523
Chris Lattner3eee99c2008-08-19 04:36:02 +0000524 FunctionProcessed = true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000525
Chris Lattner604e3512008-08-19 04:47:09 +0000526 ST_DEBUG("end processFunction!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000527}
528
529/// Clean up after incorporating a function. This is the only way to get out of
530/// the function incorporation state that affects get*Slot/Create*Slot. Function
531/// incorporation state is indicated by TheFunction != 0.
532void SlotTracker::purgeFunction() {
Chris Lattner604e3512008-08-19 04:47:09 +0000533 ST_DEBUG("begin purgeFunction!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000534 fMap.clear(); // Simply discard the function level map
535 TheFunction = 0;
536 FunctionProcessed = false;
Chris Lattner604e3512008-08-19 04:47:09 +0000537 ST_DEBUG("end purgeFunction!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000538}
539
540/// getGlobalSlot - Get the slot number of a global value.
541int SlotTracker::getGlobalSlot(const GlobalValue *V) {
542 // Check for uninitialized state and do lazy initialization.
543 initialize();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000544
Jay Foaddbb221d2011-07-11 07:28:49 +0000545 // Find the value in the module map
Chris Lattner3eee99c2008-08-19 04:36:02 +0000546 ValueMap::iterator MI = mMap.find(V);
Dan Gohman1dd27572008-10-01 19:58:59 +0000547 return MI == mMap.end() ? -1 : (int)MI->second;
Chris Lattner3eee99c2008-08-19 04:36:02 +0000548}
549
Chris Lattnercf4a76e2009-12-31 02:20:11 +0000550/// getMetadataSlot - Get the slot number of a MDNode.
Devang Patel983c6b12009-07-08 21:44:25 +0000551int SlotTracker::getMetadataSlot(const MDNode *N) {
552 // Check for uninitialized state and do lazy initialization.
553 initialize();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000554
Jay Foaddbb221d2011-07-11 07:28:49 +0000555 // Find the MDNode in the module map
Chris Lattnercf4a76e2009-12-31 02:20:11 +0000556 mdn_iterator MI = mdnMap.find(N);
Devang Patel983c6b12009-07-08 21:44:25 +0000557 return MI == mdnMap.end() ? -1 : (int)MI->second;
558}
559
Chris Lattner3eee99c2008-08-19 04:36:02 +0000560
561/// getLocalSlot - Get the slot number for a value that is local to a function.
562int SlotTracker::getLocalSlot(const Value *V) {
563 assert(!isa<Constant>(V) && "Can't get a constant or global slot with this!");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000564
Chris Lattner3eee99c2008-08-19 04:36:02 +0000565 // Check for uninitialized state and do lazy initialization.
566 initialize();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000567
Chris Lattner3eee99c2008-08-19 04:36:02 +0000568 ValueMap::iterator FI = fMap.find(V);
Dan Gohman1dd27572008-10-01 19:58:59 +0000569 return FI == fMap.end() ? -1 : (int)FI->second;
Chris Lattner3eee99c2008-08-19 04:36:02 +0000570}
571
572
573/// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
574void SlotTracker::CreateModuleSlot(const GlobalValue *V) {
575 assert(V && "Can't insert a null Value into SlotTracker!");
Chris Lattner031560c2009-12-29 07:25:48 +0000576 assert(!V->getType()->isVoidTy() && "Doesn't need a slot!");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000577 assert(!V->hasName() && "Doesn't need a slot!");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000578
Chris Lattner3eee99c2008-08-19 04:36:02 +0000579 unsigned DestSlot = mNext++;
580 mMap[V] = DestSlot;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000581
Chris Lattner604e3512008-08-19 04:47:09 +0000582 ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" <<
Chris Lattner3eee99c2008-08-19 04:36:02 +0000583 DestSlot << " [");
584 // G = Global, F = Function, A = Alias, o = other
Chris Lattner604e3512008-08-19 04:47:09 +0000585 ST_DEBUG((isa<GlobalVariable>(V) ? 'G' :
Chris Lattner3eee99c2008-08-19 04:36:02 +0000586 (isa<Function>(V) ? 'F' :
587 (isa<GlobalAlias>(V) ? 'A' : 'o'))) << "]\n");
588}
589
Chris Lattner3eee99c2008-08-19 04:36:02 +0000590/// CreateSlot - Create a new slot for the specified value if it has no name.
591void SlotTracker::CreateFunctionSlot(const Value *V) {
Chris Lattner031560c2009-12-29 07:25:48 +0000592 assert(!V->getType()->isVoidTy() && !V->hasName() && "Doesn't need a slot!");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000593
Chris Lattner3eee99c2008-08-19 04:36:02 +0000594 unsigned DestSlot = fNext++;
595 fMap[V] = DestSlot;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000596
Chris Lattner3eee99c2008-08-19 04:36:02 +0000597 // G = Global, F = Function, o = other
Chris Lattner604e3512008-08-19 04:47:09 +0000598 ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" <<
Chris Lattner3eee99c2008-08-19 04:36:02 +0000599 DestSlot << " [o]\n");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000600}
Chris Lattner3eee99c2008-08-19 04:36:02 +0000601
Devang Patel983c6b12009-07-08 21:44:25 +0000602/// CreateModuleSlot - Insert the specified MDNode* into the slot table.
603void SlotTracker::CreateMetadataSlot(const MDNode *N) {
604 assert(N && "Can't insert a null Value into SlotTracker!");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000605
Chris Lattner0d50bdd2009-12-31 02:27:30 +0000606 // Don't insert if N is a function-local metadata, these are always printed
607 // inline.
Dan Gohmana2489d12010-07-20 23:55:01 +0000608 if (!N->isFunctionLocal()) {
609 mdn_iterator I = mdnMap.find(N);
610 if (I != mdnMap.end())
611 return;
Victor Hernandez4d633542009-12-04 20:07:10 +0000612
Dan Gohmana2489d12010-07-20 23:55:01 +0000613 unsigned DestSlot = mdnNext++;
614 mdnMap[N] = DestSlot;
615 }
Devang Patel983c6b12009-07-08 21:44:25 +0000616
Chris Lattner0d50bdd2009-12-31 02:27:30 +0000617 // Recursively add any MDNodes referenced by operands.
618 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
619 if (const MDNode *Op = dyn_cast_or_null<MDNode>(N->getOperand(i)))
620 CreateMetadataSlot(Op);
Devang Patel983c6b12009-07-08 21:44:25 +0000621}
Chris Lattner3eee99c2008-08-19 04:36:02 +0000622
623//===----------------------------------------------------------------------===//
624// AsmWriter Implementation
625//===----------------------------------------------------------------------===//
Chris Lattner7f8845a2002-07-23 18:07:49 +0000626
Dan Gohman12dad632009-08-12 20:56:03 +0000627static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
Dan Gohman8061d9e2009-08-13 15:27:57 +0000628 TypePrinting *TypePrinter,
Dan Gohmana2489d12010-07-20 23:55:01 +0000629 SlotTracker *Machine,
630 const Module *Context);
Reid Spencer58d30f22004-07-04 11:50:43 +0000631
Chris Lattner033935d2008-08-17 04:40:13 +0000632
Chris Lattnerb86620e2001-10-29 16:37:48 +0000633
Chris Lattnerfc9f1c92006-12-06 06:40:49 +0000634static const char *getPredicateText(unsigned predicate) {
Reid Spencer812a1be2006-12-04 05:19:18 +0000635 const char * pred = "unknown";
636 switch (predicate) {
Chris Lattnerbddea6a2009-12-31 02:13:35 +0000637 case FCmpInst::FCMP_FALSE: pred = "false"; break;
638 case FCmpInst::FCMP_OEQ: pred = "oeq"; break;
639 case FCmpInst::FCMP_OGT: pred = "ogt"; break;
640 case FCmpInst::FCMP_OGE: pred = "oge"; break;
641 case FCmpInst::FCMP_OLT: pred = "olt"; break;
642 case FCmpInst::FCMP_OLE: pred = "ole"; break;
643 case FCmpInst::FCMP_ONE: pred = "one"; break;
644 case FCmpInst::FCMP_ORD: pred = "ord"; break;
645 case FCmpInst::FCMP_UNO: pred = "uno"; break;
646 case FCmpInst::FCMP_UEQ: pred = "ueq"; break;
647 case FCmpInst::FCMP_UGT: pred = "ugt"; break;
648 case FCmpInst::FCMP_UGE: pred = "uge"; break;
649 case FCmpInst::FCMP_ULT: pred = "ult"; break;
650 case FCmpInst::FCMP_ULE: pred = "ule"; break;
651 case FCmpInst::FCMP_UNE: pred = "une"; break;
652 case FCmpInst::FCMP_TRUE: pred = "true"; break;
653 case ICmpInst::ICMP_EQ: pred = "eq"; break;
654 case ICmpInst::ICMP_NE: pred = "ne"; break;
655 case ICmpInst::ICMP_SGT: pred = "sgt"; break;
656 case ICmpInst::ICMP_SGE: pred = "sge"; break;
657 case ICmpInst::ICMP_SLT: pred = "slt"; break;
658 case ICmpInst::ICMP_SLE: pred = "sle"; break;
659 case ICmpInst::ICMP_UGT: pred = "ugt"; break;
660 case ICmpInst::ICMP_UGE: pred = "uge"; break;
661 case ICmpInst::ICMP_ULT: pred = "ult"; break;
662 case ICmpInst::ICMP_ULE: pred = "ule"; break;
Reid Spencer812a1be2006-12-04 05:19:18 +0000663 }
664 return pred;
665}
666
Eli Friedmanc9a551e2011-07-28 21:48:00 +0000667static void writeAtomicRMWOperation(raw_ostream &Out,
668 AtomicRMWInst::BinOp Op) {
669 switch (Op) {
670 default: Out << " <unknown operation " << Op << ">"; break;
671 case AtomicRMWInst::Xchg: Out << " xchg"; break;
672 case AtomicRMWInst::Add: Out << " add"; break;
673 case AtomicRMWInst::Sub: Out << " sub"; break;
674 case AtomicRMWInst::And: Out << " and"; break;
675 case AtomicRMWInst::Nand: Out << " nand"; break;
676 case AtomicRMWInst::Or: Out << " or"; break;
677 case AtomicRMWInst::Xor: Out << " xor"; break;
678 case AtomicRMWInst::Max: Out << " max"; break;
679 case AtomicRMWInst::Min: Out << " min"; break;
680 case AtomicRMWInst::UMax: Out << " umax"; break;
681 case AtomicRMWInst::UMin: Out << " umin"; break;
682 }
683}
Devang Patel983c6b12009-07-08 21:44:25 +0000684
Dan Gohman12dad632009-08-12 20:56:03 +0000685static void WriteOptimizationInfo(raw_ostream &Out, const User *U) {
Dan Gohman0ebd6962009-07-20 21:19:07 +0000686 if (const OverflowingBinaryOperator *OBO =
687 dyn_cast<OverflowingBinaryOperator>(U)) {
Dan Gohman16f54152009-08-20 17:11:38 +0000688 if (OBO->hasNoUnsignedWrap())
Dan Gohman9c7f8082009-07-27 16:11:46 +0000689 Out << " nuw";
Dan Gohman16f54152009-08-20 17:11:38 +0000690 if (OBO->hasNoSignedWrap())
Dan Gohman9c7f8082009-07-27 16:11:46 +0000691 Out << " nsw";
Chris Lattner35315d02011-02-06 21:44:57 +0000692 } else if (const PossiblyExactOperator *Div =
693 dyn_cast<PossiblyExactOperator>(U)) {
Dan Gohman0ebd6962009-07-20 21:19:07 +0000694 if (Div->isExact())
Dan Gohman9c7f8082009-07-27 16:11:46 +0000695 Out << " exact";
Dan Gohman1639c392009-07-27 21:53:46 +0000696 } else if (const GEPOperator *GEP = dyn_cast<GEPOperator>(U)) {
697 if (GEP->isInBounds())
698 Out << " inbounds";
Dan Gohman0ebd6962009-07-20 21:19:07 +0000699 }
700}
701
Dan Gohmanefb8dbb2010-07-14 20:57:55 +0000702static void WriteConstantInternal(raw_ostream &Out, const Constant *CV,
703 TypePrinting &TypePrinter,
Dan Gohmana2489d12010-07-20 23:55:01 +0000704 SlotTracker *Machine,
705 const Module *Context) {
Zhou Sheng75b871f2007-01-11 12:24:14 +0000706 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Duncan Sands9dff9be2010-02-15 16:12:20 +0000707 if (CI->getType()->isIntegerTy(1)) {
Reid Spencercddc9df2007-01-12 04:24:46 +0000708 Out << (CI->getZExtValue() ? "true" : "false");
Chris Lattner17f71652008-08-17 07:19:36 +0000709 return;
710 }
711 Out << CI->getValue();
712 return;
713 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000714
Chris Lattner17f71652008-08-17 07:19:36 +0000715 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Tobias Grosser6b31d172012-05-24 15:59:06 +0000716 if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEsingle ||
Dan Gohman518cda42011-12-17 00:04:22 +0000717 &CFP->getValueAPF().getSemantics() == &APFloat::IEEEdouble) {
Dale Johannesen028084e2007-09-12 03:30:33 +0000718 // We would like to output the FP constant value in exponential notation,
719 // but we cannot do this if doing so will lose precision. Check here to
720 // make sure that we only output it in exponential format if we can parse
721 // the value back and get the same value.
722 //
Dale Johannesen1f864982009-01-21 20:32:55 +0000723 bool ignored;
Dan Gohman518cda42011-12-17 00:04:22 +0000724 bool isHalf = &CFP->getValueAPF().getSemantics()==&APFloat::IEEEhalf;
Dale Johannesen028084e2007-09-12 03:30:33 +0000725 bool isDouble = &CFP->getValueAPF().getSemantics()==&APFloat::IEEEdouble;
NAKAMURA Takumi35d19c02012-02-16 08:12:24 +0000726 bool isInf = CFP->getValueAPF().isInfinity();
727 bool isNaN = CFP->getValueAPF().isNaN();
728 if (!isHalf && !isInf && !isNaN) {
Dan Gohman518cda42011-12-17 00:04:22 +0000729 double Val = isDouble ? CFP->getValueAPF().convertToDouble() :
730 CFP->getValueAPF().convertToFloat();
731 SmallString<128> StrVal;
732 raw_svector_ostream(StrVal) << Val;
Chris Lattner1e194682002-04-18 18:53:13 +0000733
Dan Gohman518cda42011-12-17 00:04:22 +0000734 // Check to make sure that the stringized number is not some string like
735 // "Inf" or NaN, that atof will accept, but the lexer will not. Check
736 // that the string matches the "[-+]?[0-9]" regex.
737 //
738 if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
739 ((StrVal[0] == '-' || StrVal[0] == '+') &&
740 (StrVal[1] >= '0' && StrVal[1] <= '9'))) {
741 // Reparse stringized version!
NAKAMURA Takumiaec41232012-02-16 04:19:15 +0000742 if (APFloat(APFloat::IEEEdouble, StrVal).convertToDouble() == Val) {
Dan Gohman518cda42011-12-17 00:04:22 +0000743 Out << StrVal.str();
744 return;
745 }
Dale Johannesen028084e2007-09-12 03:30:33 +0000746 }
Chris Lattner1e194682002-04-18 18:53:13 +0000747 }
Dale Johannesen028084e2007-09-12 03:30:33 +0000748 // Otherwise we could not reparse it to exactly the same value, so we must
Dale Johannesen1f864982009-01-21 20:32:55 +0000749 // output the string in hexadecimal format! Note that loading and storing
750 // floating point types changes the bits of NaNs on some hosts, notably
751 // x86, so we must not use these types.
Dale Johannesen028084e2007-09-12 03:30:33 +0000752 assert(sizeof(double) == sizeof(uint64_t) &&
753 "assuming that double is 64 bits!");
Chris Lattner5505eed2008-11-10 04:30:26 +0000754 char Buffer[40];
Dale Johannesen1f864982009-01-21 20:32:55 +0000755 APFloat apf = CFP->getValueAPF();
Dan Gohman518cda42011-12-17 00:04:22 +0000756 // Halves and floats are represented in ASCII IR as double, convert.
Dale Johannesen1f864982009-01-21 20:32:55 +0000757 if (!isDouble)
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000758 apf.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven,
Dale Johannesen1f864982009-01-21 20:32:55 +0000759 &ignored);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000760 Out << "0x" <<
761 utohex_buffer(uint64_t(apf.bitcastToAPInt().getZExtValue()),
Dale Johannesen1f864982009-01-21 20:32:55 +0000762 Buffer+40);
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000763 return;
764 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000765
Tobias Grosser6b31d172012-05-24 15:59:06 +0000766 // Either half, or some form of long double.
767 // These appear as a magic letter identifying the type, then a
768 // fixed number of hex digits.
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000769 Out << "0x";
Tobias Grosser6b31d172012-05-24 15:59:06 +0000770 // Bit position, in the current word, of the next nibble to print.
771 int shiftcount;
772
Dale Johannesen93eefa02009-03-23 21:16:53 +0000773 if (&CFP->getValueAPF().getSemantics() == &APFloat::x87DoubleExtended) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000774 Out << 'K';
Dale Johannesen93eefa02009-03-23 21:16:53 +0000775 // api needed to prevent premature destruction
776 APInt api = CFP->getValueAPF().bitcastToAPInt();
777 const uint64_t* p = api.getRawData();
778 uint64_t word = p[1];
Tobias Grosser6b31d172012-05-24 15:59:06 +0000779 shiftcount = 12;
Dale Johannesen93eefa02009-03-23 21:16:53 +0000780 int width = api.getBitWidth();
781 for (int j=0; j<width; j+=4, shiftcount-=4) {
782 unsigned int nibble = (word>>shiftcount) & 15;
783 if (nibble < 10)
784 Out << (unsigned char)(nibble + '0');
785 else
786 Out << (unsigned char)(nibble - 10 + 'A');
787 if (shiftcount == 0 && j+4 < width) {
788 word = *p;
789 shiftcount = 64;
790 if (width-j-4 < 64)
791 shiftcount = width-j-4;
792 }
793 }
794 return;
Tobias Grosser6b31d172012-05-24 15:59:06 +0000795 } else if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEquad) {
796 shiftcount = 60;
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000797 Out << 'L';
Tobias Grosser6b31d172012-05-24 15:59:06 +0000798 } else if (&CFP->getValueAPF().getSemantics() == &APFloat::PPCDoubleDouble) {
799 shiftcount = 60;
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000800 Out << 'M';
Tobias Grosser6b31d172012-05-24 15:59:06 +0000801 } else if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEhalf) {
802 shiftcount = 12;
803 Out << 'H';
804 } else
Torok Edwinfbcc6632009-07-14 16:55:14 +0000805 llvm_unreachable("Unsupported floating point type");
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000806 // api needed to prevent premature destruction
Dale Johannesen54306fe2008-10-09 18:53:47 +0000807 APInt api = CFP->getValueAPF().bitcastToAPInt();
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000808 const uint64_t* p = api.getRawData();
809 uint64_t word = *p;
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000810 int width = api.getBitWidth();
811 for (int j=0; j<width; j+=4, shiftcount-=4) {
812 unsigned int nibble = (word>>shiftcount) & 15;
813 if (nibble < 10)
814 Out << (unsigned char)(nibble + '0');
Dale Johannesen028084e2007-09-12 03:30:33 +0000815 else
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000816 Out << (unsigned char)(nibble - 10 + 'A');
817 if (shiftcount == 0 && j+4 < width) {
818 word = *(++p);
819 shiftcount = 64;
820 if (width-j-4 < 64)
821 shiftcount = width-j-4;
Dale Johannesen028084e2007-09-12 03:30:33 +0000822 }
823 }
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000824 return;
825 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000826
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000827 if (isa<ConstantAggregateZero>(CV)) {
Chris Lattner76b2ff42004-02-15 05:55:15 +0000828 Out << "zeroinitializer";
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000829 return;
830 }
Andrew Trickec4b6e72011-09-30 19:48:58 +0000831
Chris Lattner214cc702009-10-28 03:38:12 +0000832 if (const BlockAddress *BA = dyn_cast<BlockAddress>(CV)) {
833 Out << "blockaddress(";
Dan Gohmana2489d12010-07-20 23:55:01 +0000834 WriteAsOperandInternal(Out, BA->getFunction(), &TypePrinter, Machine,
835 Context);
Chris Lattner214cc702009-10-28 03:38:12 +0000836 Out << ", ";
Dan Gohmana2489d12010-07-20 23:55:01 +0000837 WriteAsOperandInternal(Out, BA->getBasicBlock(), &TypePrinter, Machine,
838 Context);
Chris Lattner214cc702009-10-28 03:38:12 +0000839 Out << ")";
840 return;
841 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000842
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000843 if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000844 Type *ETy = CA->getType()->getElementType();
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000845 Out << '[';
846 TypePrinter.print(ETy, Out);
847 Out << ' ';
848 WriteAsOperandInternal(Out, CA->getOperand(0),
849 &TypePrinter, Machine,
850 Context);
851 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
852 Out << ", ";
Chris Lattner9c181f62012-01-31 03:15:40 +0000853 TypePrinter.print(ETy, Out);
854 Out << ' ';
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000855 WriteAsOperandInternal(Out, CA->getOperand(i), &TypePrinter, Machine,
Chris Lattner9c181f62012-01-31 03:15:40 +0000856 Context);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000857 }
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000858 Out << ']';
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000859 return;
860 }
Chris Lattnerfa775002012-01-26 02:32:04 +0000861
862 if (const ConstantDataArray *CA = dyn_cast<ConstantDataArray>(CV)) {
863 // As a special case, print the array as a string if it is an array of
864 // i8 with ConstantInt values.
865 if (CA->isString()) {
866 Out << "c\"";
867 PrintEscapedString(CA->getAsString(), Out);
868 Out << '"';
869 return;
870 }
871
872 Type *ETy = CA->getType()->getElementType();
873 Out << '[';
Chris Lattner9c181f62012-01-31 03:15:40 +0000874 TypePrinter.print(ETy, Out);
875 Out << ' ';
876 WriteAsOperandInternal(Out, CA->getElementAsConstant(0),
877 &TypePrinter, Machine,
878 Context);
879 for (unsigned i = 1, e = CA->getNumElements(); i != e; ++i) {
880 Out << ", ";
Chris Lattnerfa775002012-01-26 02:32:04 +0000881 TypePrinter.print(ETy, Out);
882 Out << ' ';
Chris Lattner9c181f62012-01-31 03:15:40 +0000883 WriteAsOperandInternal(Out, CA->getElementAsConstant(i), &TypePrinter,
884 Machine, Context);
Chris Lattnerfa775002012-01-26 02:32:04 +0000885 }
Chris Lattner9c181f62012-01-31 03:15:40 +0000886 Out << ']';
Chris Lattnerfa775002012-01-26 02:32:04 +0000887 return;
888 }
889
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000890
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000891 if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
Andrew Lenharth0d124b82007-01-08 18:21:30 +0000892 if (CS->getType()->isPacked())
893 Out << '<';
Misha Brukman21bbdb92004-06-04 21:11:51 +0000894 Out << '{';
Jim Laskey3bb78742006-02-25 12:27:03 +0000895 unsigned N = CS->getNumOperands();
896 if (N) {
Chris Lattner604e3512008-08-19 04:47:09 +0000897 Out << ' ';
Chris Lattnere101c442009-02-28 21:26:53 +0000898 TypePrinter.print(CS->getOperand(0)->getType(), Out);
Dan Gohman81313fd2008-09-14 17:21:12 +0000899 Out << ' ';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000900
Dan Gohmana2489d12010-07-20 23:55:01 +0000901 WriteAsOperandInternal(Out, CS->getOperand(0), &TypePrinter, Machine,
902 Context);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000903
Jim Laskey3bb78742006-02-25 12:27:03 +0000904 for (unsigned i = 1; i < N; i++) {
Chris Lattnerd84bb632002-04-16 21:36:08 +0000905 Out << ", ";
Chris Lattnere101c442009-02-28 21:26:53 +0000906 TypePrinter.print(CS->getOperand(i)->getType(), Out);
Dan Gohman81313fd2008-09-14 17:21:12 +0000907 Out << ' ';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000908
Dan Gohmana2489d12010-07-20 23:55:01 +0000909 WriteAsOperandInternal(Out, CS->getOperand(i), &TypePrinter, Machine,
910 Context);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000911 }
Dan Gohman81313fd2008-09-14 17:21:12 +0000912 Out << ' ';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000913 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000914
Dan Gohman81313fd2008-09-14 17:21:12 +0000915 Out << '}';
Andrew Lenharth0d124b82007-01-08 18:21:30 +0000916 if (CS->getType()->isPacked())
917 Out << '>';
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000918 return;
919 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000920
Chris Lattnerfa775002012-01-26 02:32:04 +0000921 if (isa<ConstantVector>(CV) || isa<ConstantDataVector>(CV)) {
922 Type *ETy = CV->getType()->getVectorElementType();
Dan Gohman1262a252009-02-11 00:25:25 +0000923 Out << '<';
Chris Lattnere101c442009-02-28 21:26:53 +0000924 TypePrinter.print(ETy, Out);
Dan Gohman81313fd2008-09-14 17:21:12 +0000925 Out << ' ';
Chris Lattnerfa775002012-01-26 02:32:04 +0000926 WriteAsOperandInternal(Out, CV->getAggregateElement(0U), &TypePrinter,
927 Machine, Context);
928 for (unsigned i = 1, e = CV->getType()->getVectorNumElements(); i != e;++i){
Chris Lattner585297e82008-08-19 05:26:17 +0000929 Out << ", ";
Chris Lattnere101c442009-02-28 21:26:53 +0000930 TypePrinter.print(ETy, Out);
Dan Gohman81313fd2008-09-14 17:21:12 +0000931 Out << ' ';
Chris Lattnerfa775002012-01-26 02:32:04 +0000932 WriteAsOperandInternal(Out, CV->getAggregateElement(i), &TypePrinter,
933 Machine, Context);
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000934 }
Dan Gohman1262a252009-02-11 00:25:25 +0000935 Out << '>';
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000936 return;
937 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000938
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000939 if (isa<ConstantPointerNull>(CV)) {
Chris Lattnerd84bb632002-04-16 21:36:08 +0000940 Out << "null";
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000941 return;
942 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000943
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000944 if (isa<UndefValue>(CV)) {
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000945 Out << "undef";
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000946 return;
947 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000948
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000949 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Reid Spencer812a1be2006-12-04 05:19:18 +0000950 Out << CE->getOpcodeName();
Dan Gohman9c7f8082009-07-27 16:11:46 +0000951 WriteOptimizationInfo(Out, CE);
Reid Spencer812a1be2006-12-04 05:19:18 +0000952 if (CE->isCompare())
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000953 Out << ' ' << getPredicateText(CE->getPredicate());
Reid Spencer812a1be2006-12-04 05:19:18 +0000954 Out << " (";
Misha Brukmanb1c93172005-04-21 23:48:37 +0000955
Vikram S. Adveb952b542002-07-14 23:14:45 +0000956 for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) {
Chris Lattnere101c442009-02-28 21:26:53 +0000957 TypePrinter.print((*OI)->getType(), Out);
Dan Gohman81313fd2008-09-14 17:21:12 +0000958 Out << ' ';
Dan Gohmana2489d12010-07-20 23:55:01 +0000959 WriteAsOperandInternal(Out, *OI, &TypePrinter, Machine, Context);
Vikram S. Adveb952b542002-07-14 23:14:45 +0000960 if (OI+1 != CE->op_end())
Chris Lattner3cd8c562002-07-30 18:54:25 +0000961 Out << ", ";
Vikram S. Adveb952b542002-07-14 23:14:45 +0000962 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000963
Dan Gohmana76f0f72008-05-31 19:12:39 +0000964 if (CE->hasIndices()) {
Jay Foad0091fe82011-04-13 15:22:40 +0000965 ArrayRef<unsigned> Indices = CE->getIndices();
Dan Gohmana76f0f72008-05-31 19:12:39 +0000966 for (unsigned i = 0, e = Indices.size(); i != e; ++i)
967 Out << ", " << Indices[i];
968 }
969
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000970 if (CE->isCast()) {
Chris Lattner83b396b2002-08-15 19:37:43 +0000971 Out << " to ";
Chris Lattnere101c442009-02-28 21:26:53 +0000972 TypePrinter.print(CE->getType(), Out);
Chris Lattner83b396b2002-08-15 19:37:43 +0000973 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000974
Misha Brukman21bbdb92004-06-04 21:11:51 +0000975 Out << ')';
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000976 return;
Chris Lattnerd84bb632002-04-16 21:36:08 +0000977 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000978
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000979 Out << "<placeholder or erroneous Constant>";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000980}
981
Chris Lattnercdec5812009-12-31 02:31:59 +0000982static void WriteMDNodeBodyInternal(raw_ostream &Out, const MDNode *Node,
983 TypePrinting *TypePrinter,
Dan Gohmana2489d12010-07-20 23:55:01 +0000984 SlotTracker *Machine,
985 const Module *Context) {
Chris Lattnercdec5812009-12-31 02:31:59 +0000986 Out << "!{";
987 for (unsigned mi = 0, me = Node->getNumOperands(); mi != me; ++mi) {
988 const Value *V = Node->getOperand(mi);
989 if (V == 0)
990 Out << "null";
991 else {
992 TypePrinter->print(V->getType(), Out);
993 Out << ' ';
Andrew Trickec4b6e72011-09-30 19:48:58 +0000994 WriteAsOperandInternal(Out, Node->getOperand(mi),
Dan Gohmana2489d12010-07-20 23:55:01 +0000995 TypePrinter, Machine, Context);
Chris Lattnercdec5812009-12-31 02:31:59 +0000996 }
997 if (mi + 1 != me)
998 Out << ", ";
999 }
Andrew Trickec4b6e72011-09-30 19:48:58 +00001000
Chris Lattnercdec5812009-12-31 02:31:59 +00001001 Out << "}";
1002}
1003
Chris Lattnerd84bb632002-04-16 21:36:08 +00001004
Misha Brukmanc566ca362004-03-02 00:22:19 +00001005/// WriteAsOperand - Write the name of the specified value out to the specified
1006/// ostream. This can be useful when you just want to print int %reg126, not
1007/// the whole instruction that generated it.
1008///
Dan Gohman12dad632009-08-12 20:56:03 +00001009static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
Dan Gohman8061d9e2009-08-13 15:27:57 +00001010 TypePrinting *TypePrinter,
Dan Gohmana2489d12010-07-20 23:55:01 +00001011 SlotTracker *Machine,
1012 const Module *Context) {
Chris Lattner033935d2008-08-17 04:40:13 +00001013 if (V->hasName()) {
1014 PrintLLVMName(Out, V);
1015 return;
1016 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001017
Chris Lattner033935d2008-08-17 04:40:13 +00001018 const Constant *CV = dyn_cast<Constant>(V);
1019 if (CV && !isa<GlobalValue>(CV)) {
Dan Gohman8061d9e2009-08-13 15:27:57 +00001020 assert(TypePrinter && "Constants require TypePrinting!");
Dan Gohmana2489d12010-07-20 23:55:01 +00001021 WriteConstantInternal(Out, CV, *TypePrinter, Machine, Context);
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001022 return;
1023 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001024
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001025 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
Chris Lattner033935d2008-08-17 04:40:13 +00001026 Out << "asm ";
1027 if (IA->hasSideEffects())
1028 Out << "sideeffect ";
Dale Johannesen1cfb9582009-10-21 23:28:00 +00001029 if (IA->isAlignStack())
1030 Out << "alignstack ";
Chris Lattner033935d2008-08-17 04:40:13 +00001031 Out << '"';
1032 PrintEscapedString(IA->getAsmString(), Out);
1033 Out << "\", \"";
1034 PrintEscapedString(IA->getConstraintString(), Out);
1035 Out << '"';
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001036 return;
1037 }
Devang Patel7428d8a2009-07-22 17:43:22 +00001038
Devang Patele059ba6e2009-07-23 01:07:34 +00001039 if (const MDNode *N = dyn_cast<MDNode>(V)) {
Victor Hernandez0471abd2009-12-18 20:09:14 +00001040 if (N->isFunctionLocal()) {
Victor Hernandezb7176a12009-12-04 01:35:02 +00001041 // Print metadata inline, not via slot reference number.
Dan Gohmana2489d12010-07-20 23:55:01 +00001042 WriteMDNodeBodyInternal(Out, N, TypePrinter, Machine, Context);
Victor Hernandezb7176a12009-12-04 01:35:02 +00001043 return;
1044 }
Andrew Trickec4b6e72011-09-30 19:48:58 +00001045
Dan Gohmana2489d12010-07-20 23:55:01 +00001046 if (!Machine) {
1047 if (N->isFunctionLocal())
1048 Machine = new SlotTracker(N->getFunction());
1049 else
1050 Machine = new SlotTracker(Context);
1051 }
Dan Gohman0a54da22010-09-09 20:53:58 +00001052 int Slot = Machine->getMetadataSlot(N);
1053 if (Slot == -1)
1054 Out << "<badref>";
1055 else
1056 Out << '!' << Slot;
Devang Patele059ba6e2009-07-23 01:07:34 +00001057 return;
1058 }
1059
Devang Patel7428d8a2009-07-22 17:43:22 +00001060 if (const MDString *MDS = dyn_cast<MDString>(V)) {
Devang Patel7428d8a2009-07-22 17:43:22 +00001061 Out << "!\"";
Daniel Dunbare03eecb2009-07-25 23:55:21 +00001062 PrintEscapedString(MDS->getString(), Out);
Devang Patel7428d8a2009-07-22 17:43:22 +00001063 Out << '"';
1064 return;
1065 }
1066
Evan Cheng00e87d12009-11-16 07:10:36 +00001067 if (V->getValueID() == Value::PseudoSourceValueVal ||
1068 V->getValueID() == Value::FixedStackPseudoSourceValueVal) {
Dan Gohmanc0353bf2009-09-23 01:33:16 +00001069 V->print(Out);
1070 return;
1071 }
1072
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001073 char Prefix = '%';
1074 int Slot;
Chris Lattner5b82a0a2011-08-03 06:15:41 +00001075 // If we have a SlotTracker, use it.
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001076 if (Machine) {
1077 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1078 Slot = Machine->getGlobalSlot(GV);
1079 Prefix = '@';
1080 } else {
1081 Slot = Machine->getLocalSlot(V);
Andrew Trickec4b6e72011-09-30 19:48:58 +00001082
Chris Lattner5b82a0a2011-08-03 06:15:41 +00001083 // If the local value didn't succeed, then we may be referring to a value
1084 // from a different function. Translate it, as this can happen when using
1085 // address of blocks.
1086 if (Slot == -1)
1087 if ((Machine = createSlotTracker(V))) {
1088 Slot = Machine->getLocalSlot(V);
1089 delete Machine;
1090 }
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001091 }
Chris Lattner5b82a0a2011-08-03 06:15:41 +00001092 } else if ((Machine = createSlotTracker(V))) {
1093 // Otherwise, create one to get the # and then destroy it.
1094 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1095 Slot = Machine->getGlobalSlot(GV);
1096 Prefix = '@';
Chris Lattnera2d810d2006-01-25 22:26:05 +00001097 } else {
Chris Lattner5b82a0a2011-08-03 06:15:41 +00001098 Slot = Machine->getLocalSlot(V);
Chris Lattnerd84bb632002-04-16 21:36:08 +00001099 }
Chris Lattner5b82a0a2011-08-03 06:15:41 +00001100 delete Machine;
1101 Machine = 0;
1102 } else {
1103 Slot = -1;
Chris Lattnerd84bb632002-04-16 21:36:08 +00001104 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001105
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001106 if (Slot != -1)
1107 Out << Prefix << Slot;
1108 else
1109 Out << "<badref>";
Chris Lattnerd84bb632002-04-16 21:36:08 +00001110}
1111
Dan Gohman12dad632009-08-12 20:56:03 +00001112void llvm::WriteAsOperand(raw_ostream &Out, const Value *V,
1113 bool PrintType, const Module *Context) {
Dan Gohman8061d9e2009-08-13 15:27:57 +00001114
1115 // Fast path: Don't construct and populate a TypePrinting object if we
1116 // won't be needing any types printed.
Dan Gohman08097122009-08-13 23:07:11 +00001117 if (!PrintType &&
Dan Gohmana2489d12010-07-20 23:55:01 +00001118 ((!isa<Constant>(V) && !isa<MDNode>(V)) ||
1119 V->hasName() || isa<GlobalValue>(V))) {
1120 WriteAsOperandInternal(Out, V, 0, 0, Context);
Dan Gohman8061d9e2009-08-13 15:27:57 +00001121 return;
1122 }
1123
Chris Lattner5a9f63e2002-07-10 16:48:17 +00001124 if (Context == 0) Context = getModuleFromVal(V);
Chris Lattnerb86620e2001-10-29 16:37:48 +00001125
Chris Lattner92c5c122009-02-28 23:20:19 +00001126 TypePrinting TypePrinter;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001127 if (Context)
1128 TypePrinter.incorporateTypes(*Context);
Dan Gohman81313fd2008-09-14 17:21:12 +00001129 if (PrintType) {
Chris Lattnere101c442009-02-28 21:26:53 +00001130 TypePrinter.print(V->getType(), Out);
Dan Gohman81313fd2008-09-14 17:21:12 +00001131 Out << ' ';
1132 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001133
Dan Gohmana2489d12010-07-20 23:55:01 +00001134 WriteAsOperandInternal(Out, V, &TypePrinter, 0, Context);
Chris Lattner5e5abe32001-07-20 19:15:21 +00001135}
1136
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001137namespace {
Chris Lattner2e9fee42001-07-12 23:35:26 +00001138
Chris Lattnerfee714f2001-09-07 16:36:04 +00001139class AssemblyWriter {
Dan Gohmane2745262009-08-12 17:23:50 +00001140 formatted_raw_ostream &Out;
Chris Lattnere36fd8a2008-08-19 04:26:57 +00001141 SlotTracker &Machine;
Chris Lattner7bfee412001-10-29 16:05:51 +00001142 const Module *TheModule;
Chris Lattner0f578952009-02-28 20:25:14 +00001143 TypePrinting TypePrinter;
Chris Lattner8339f7d2003-10-30 23:41:03 +00001144 AssemblyAnnotationWriter *AnnotationWriter;
Andrew Trickec4b6e72011-09-30 19:48:58 +00001145
Chris Lattner2f7c9632001-06-06 20:29:01 +00001146public:
Dan Gohmane2745262009-08-12 17:23:50 +00001147 inline AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac,
1148 const Module *M,
Chris Lattner8339f7d2003-10-30 23:41:03 +00001149 AssemblyAnnotationWriter *AAW)
Devang Patel59c0c132009-09-28 18:31:56 +00001150 : Out(o), Machine(Mac), TheModule(M), AnnotationWriter(AAW) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001151 if (M)
1152 TypePrinter.incorporateTypes(*M);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001153 }
1154
Chris Lattnerbddea6a2009-12-31 02:13:35 +00001155 void printMDNodeBody(const MDNode *MD);
Chris Lattner2c48c642009-12-31 01:54:05 +00001156 void printNamedMDNode(const NamedMDNode *NMD);
Andrew Trickec4b6e72011-09-30 19:48:58 +00001157
Chris Lattnerefb5e392009-12-31 02:23:35 +00001158 void printModule(const Module *M);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001159
Chris Lattner78e2e8b2006-12-06 06:24:27 +00001160 void writeOperand(const Value *Op, bool PrintType);
Devang Patelba3fa6c2008-09-23 23:03:40 +00001161 void writeParamOperand(const Value *Operand, Attributes Attrs);
Eli Friedmanfee02c62011-07-25 23:16:38 +00001162 void writeAtomic(AtomicOrdering Ordering, SynchronizationScope SynchScope);
Chris Lattner1e194682002-04-18 18:53:13 +00001163
Chris Lattnerbddea6a2009-12-31 02:13:35 +00001164 void writeAllMDNodes();
1165
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001166 void printTypeIdentities();
Chris Lattner7bfee412001-10-29 16:05:51 +00001167 void printGlobal(const GlobalVariable *GV);
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001168 void printAlias(const GlobalAlias *GV);
Chris Lattner57698e22002-03-26 18:01:55 +00001169 void printFunction(const Function *F);
Devang Patelba3fa6c2008-09-23 23:03:40 +00001170 void printArgument(const Argument *FA, Attributes Attrs);
Chris Lattner7bfee412001-10-29 16:05:51 +00001171 void printBasicBlock(const BasicBlock *BB);
Chris Lattner113f4f42002-06-25 16:13:24 +00001172 void printInstruction(const Instruction &I);
Chris Lattnerd816b532002-04-13 20:53:41 +00001173
Dan Gohmanbd11c412010-02-10 20:42:57 +00001174private:
Chris Lattner862e3382001-10-13 06:42:36 +00001175 // printInfoComment - Print a little comment after the instruction indicating
1176 // which slot it occupies.
Chris Lattner113f4f42002-06-25 16:13:24 +00001177 void printInfoComment(const Value &V);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001178};
Chris Lattner3243ea12009-03-01 00:03:38 +00001179} // end of anonymous namespace
Chris Lattner2f7c9632001-06-06 20:29:01 +00001180
Chris Lattner78e2e8b2006-12-06 06:24:27 +00001181void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType) {
1182 if (Operand == 0) {
Chris Lattner08f7d0c2005-02-24 16:58:29 +00001183 Out << "<null operand!>";
Chris Lattnerb419a1e2009-12-31 02:33:14 +00001184 return;
Chris Lattner08f7d0c2005-02-24 16:58:29 +00001185 }
Chris Lattnerb419a1e2009-12-31 02:33:14 +00001186 if (PrintType) {
1187 TypePrinter.print(Operand->getType(), Out);
1188 Out << ' ';
1189 }
Dan Gohmana2489d12010-07-20 23:55:01 +00001190 WriteAsOperandInternal(Out, Operand, &TypePrinter, &Machine, TheModule);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001191}
1192
Eli Friedmanfee02c62011-07-25 23:16:38 +00001193void AssemblyWriter::writeAtomic(AtomicOrdering Ordering,
1194 SynchronizationScope SynchScope) {
1195 if (Ordering == NotAtomic)
1196 return;
1197
1198 switch (SynchScope) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001199 case SingleThread: Out << " singlethread"; break;
1200 case CrossThread: break;
1201 }
1202
1203 switch (Ordering) {
1204 default: Out << " <bad ordering " << int(Ordering) << ">"; break;
1205 case Unordered: Out << " unordered"; break;
1206 case Monotonic: Out << " monotonic"; break;
1207 case Acquire: Out << " acquire"; break;
1208 case Release: Out << " release"; break;
1209 case AcquireRelease: Out << " acq_rel"; break;
1210 case SequentiallyConsistent: Out << " seq_cst"; break;
1211 }
1212}
1213
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001214void AssemblyWriter::writeParamOperand(const Value *Operand,
Devang Patelba3fa6c2008-09-23 23:03:40 +00001215 Attributes Attrs) {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00001216 if (Operand == 0) {
1217 Out << "<null operand!>";
Chris Lattnerb419a1e2009-12-31 02:33:14 +00001218 return;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00001219 }
Chris Lattnerb419a1e2009-12-31 02:33:14 +00001220
1221 // Print the type
1222 TypePrinter.print(Operand->getType(), Out);
1223 // Print parameter attributes list
1224 if (Attrs != Attribute::None)
1225 Out << ' ' << Attribute::getAsString(Attrs);
1226 Out << ' ';
1227 // Print the operand
Dan Gohmana2489d12010-07-20 23:55:01 +00001228 WriteAsOperandInternal(Out, Operand, &TypePrinter, &Machine, TheModule);
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00001229}
Chris Lattner2f7c9632001-06-06 20:29:01 +00001230
Chris Lattner7bfee412001-10-29 16:05:51 +00001231void AssemblyWriter::printModule(const Module *M) {
Chris Lattner4d8689e2005-03-02 23:12:40 +00001232 if (!M->getModuleIdentifier().empty() &&
Misha Brukmanb1c93172005-04-21 23:48:37 +00001233 // Don't print the ID if it will start a new line (which would
Chris Lattner4d8689e2005-03-02 23:12:40 +00001234 // require a comment char before it).
1235 M->getModuleIdentifier().find('\n') == std::string::npos)
1236 Out << "; ModuleID = '" << M->getModuleIdentifier() << "'\n";
1237
Owen Andersone2237542006-10-18 02:21:12 +00001238 if (!M->getDataLayout().empty())
Chris Lattner04897162006-10-22 06:06:56 +00001239 Out << "target datalayout = \"" << M->getDataLayout() << "\"\n";
Reid Spencer48f98c82004-07-25 21:44:54 +00001240 if (!M->getTargetTriple().empty())
Reid Spencerffec7df2004-07-25 21:29:43 +00001241 Out << "target triple = \"" << M->getTargetTriple() << "\"\n";
Misha Brukmanb1c93172005-04-21 23:48:37 +00001242
Chris Lattnereef2fe72006-01-24 04:13:11 +00001243 if (!M->getModuleInlineAsm().empty()) {
Chris Lattnerefaf35d2006-01-24 00:45:30 +00001244 // Split the string into lines, to make it easier to read the .ll file.
Chris Lattnereef2fe72006-01-24 04:13:11 +00001245 std::string Asm = M->getModuleInlineAsm();
Chris Lattnerefaf35d2006-01-24 00:45:30 +00001246 size_t CurPos = 0;
1247 size_t NewLine = Asm.find_first_of('\n', CurPos);
Dan Gohman69273e62009-08-12 23:54:22 +00001248 Out << '\n';
Chris Lattnerefaf35d2006-01-24 00:45:30 +00001249 while (NewLine != std::string::npos) {
1250 // We found a newline, print the portion of the asm string from the
1251 // last newline up to this newline.
1252 Out << "module asm \"";
1253 PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.begin()+NewLine),
1254 Out);
1255 Out << "\"\n";
1256 CurPos = NewLine+1;
1257 NewLine = Asm.find_first_of('\n', CurPos);
1258 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +00001259 std::string rest(Asm.begin()+CurPos, Asm.end());
1260 if (!rest.empty()) {
1261 Out << "module asm \"";
1262 PrintEscapedString(rest, Out);
1263 Out << "\"\n";
1264 }
Chris Lattner6ed87bd2006-01-23 23:03:36 +00001265 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001266
Chris Lattner2cdd49d2004-09-14 05:06:58 +00001267 // Loop over the dependent libraries and emit them.
Chris Lattner730cfe42004-09-14 04:51:44 +00001268 Module::lib_iterator LI = M->lib_begin();
1269 Module::lib_iterator LE = M->lib_end();
Reid Spencer48f98c82004-07-25 21:44:54 +00001270 if (LI != LE) {
Dan Gohman69273e62009-08-12 23:54:22 +00001271 Out << '\n';
Chris Lattner730cfe42004-09-14 04:51:44 +00001272 Out << "deplibs = [ ";
1273 while (LI != LE) {
Chris Lattner2cdd49d2004-09-14 05:06:58 +00001274 Out << '"' << *LI << '"';
Reid Spencerffec7df2004-07-25 21:29:43 +00001275 ++LI;
Chris Lattner730cfe42004-09-14 04:51:44 +00001276 if (LI != LE)
1277 Out << ", ";
Reid Spencerffec7df2004-07-25 21:29:43 +00001278 }
Dan Gohman69273e62009-08-12 23:54:22 +00001279 Out << " ]";
Reid Spencercc5ff642004-07-25 18:08:18 +00001280 }
Reid Spencerb9e08772004-09-13 23:44:23 +00001281
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001282 printTypeIdentities();
Misha Brukmanb1c93172005-04-21 23:48:37 +00001283
Dan Gohman69273e62009-08-12 23:54:22 +00001284 // Output all globals.
1285 if (!M->global_empty()) Out << '\n';
Chris Lattner54932b02006-12-06 04:41:52 +00001286 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
1287 I != E; ++I)
Chris Lattner113f4f42002-06-25 16:13:24 +00001288 printGlobal(I);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001289
Chris Lattnerd2747052007-04-26 02:24:10 +00001290 // Output all aliases.
1291 if (!M->alias_empty()) Out << "\n";
1292 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
1293 I != E; ++I)
1294 printAlias(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +00001295
Chris Lattner2cdd49d2004-09-14 05:06:58 +00001296 // Output all of the functions.
Chris Lattner113f4f42002-06-25 16:13:24 +00001297 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
1298 printFunction(I);
Devang Patel983c6b12009-07-08 21:44:25 +00001299
Devang Patel23e68302009-07-29 22:04:47 +00001300 // Output named metadata.
Dan Gohman69273e62009-08-12 23:54:22 +00001301 if (!M->named_metadata_empty()) Out << '\n';
Andrew Trickec4b6e72011-09-30 19:48:58 +00001302
Devang Patel23e68302009-07-29 22:04:47 +00001303 for (Module::const_named_metadata_iterator I = M->named_metadata_begin(),
Chris Lattnerbddea6a2009-12-31 02:13:35 +00001304 E = M->named_metadata_end(); I != E; ++I)
Chris Lattner2c48c642009-12-31 01:54:05 +00001305 printNamedMDNode(I);
Devang Patel23e68302009-07-29 22:04:47 +00001306
1307 // Output metadata.
Chris Lattnercf4a76e2009-12-31 02:20:11 +00001308 if (!Machine.mdn_empty()) {
Chris Lattnerbddea6a2009-12-31 02:13:35 +00001309 Out << '\n';
1310 writeAllMDNodes();
1311 }
Chris Lattnerfee714f2001-09-07 16:36:04 +00001312}
1313
Chris Lattner2c48c642009-12-31 01:54:05 +00001314void AssemblyWriter::printNamedMDNode(const NamedMDNode *NMD) {
Nick Lewycky5bcbd732011-06-15 06:37:58 +00001315 Out << '!';
1316 StringRef Name = NMD->getName();
1317 if (Name.empty()) {
1318 Out << "<empty name> ";
1319 } else {
1320 if (isalpha(Name[0]) || Name[0] == '-' || Name[0] == '$' ||
1321 Name[0] == '.' || Name[0] == '_')
1322 Out << Name[0];
1323 else
1324 Out << '\\' << hexdigit(Name[0] >> 4) << hexdigit(Name[0] & 0x0F);
1325 for (unsigned i = 1, e = Name.size(); i != e; ++i) {
1326 unsigned char C = Name[i];
1327 if (isalnum(C) || C == '-' || C == '$' || C == '.' || C == '_')
1328 Out << C;
1329 else
1330 Out << '\\' << hexdigit(C >> 4) << hexdigit(C & 0x0F);
1331 }
1332 }
1333 Out << " = !{";
Chris Lattner2c48c642009-12-31 01:54:05 +00001334 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1335 if (i) Out << ", ";
Dan Gohman0a54da22010-09-09 20:53:58 +00001336 int Slot = Machine.getMetadataSlot(NMD->getOperand(i));
1337 if (Slot == -1)
1338 Out << "<badref>";
1339 else
1340 Out << '!' << Slot;
Chris Lattner2c48c642009-12-31 01:54:05 +00001341 }
1342 Out << "}\n";
1343}
1344
1345
Dan Gohmane2745262009-08-12 17:23:50 +00001346static void PrintLinkage(GlobalValue::LinkageTypes LT,
1347 formatted_raw_ostream &Out) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001348 switch (LT) {
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001349 case GlobalValue::ExternalLinkage: break;
1350 case GlobalValue::PrivateLinkage: Out << "private "; break;
1351 case GlobalValue::LinkerPrivateLinkage: Out << "linker_private "; break;
Bill Wendling03bcd6e2010-07-01 21:55:59 +00001352 case GlobalValue::LinkerPrivateWeakLinkage:
1353 Out << "linker_private_weak ";
1354 break;
Bill Wendling578ee402010-08-20 22:05:50 +00001355 case GlobalValue::LinkerPrivateWeakDefAutoLinkage:
1356 Out << "linker_private_weak_def_auto ";
1357 break;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001358 case GlobalValue::InternalLinkage: Out << "internal "; break;
1359 case GlobalValue::LinkOnceAnyLinkage: Out << "linkonce "; break;
1360 case GlobalValue::LinkOnceODRLinkage: Out << "linkonce_odr "; break;
1361 case GlobalValue::WeakAnyLinkage: Out << "weak "; break;
1362 case GlobalValue::WeakODRLinkage: Out << "weak_odr "; break;
1363 case GlobalValue::CommonLinkage: Out << "common "; break;
1364 case GlobalValue::AppendingLinkage: Out << "appending "; break;
1365 case GlobalValue::DLLImportLinkage: Out << "dllimport "; break;
1366 case GlobalValue::DLLExportLinkage: Out << "dllexport "; break;
1367 case GlobalValue::ExternalWeakLinkage: Out << "extern_weak "; break;
Chris Lattner184f1be2009-04-13 05:44:34 +00001368 case GlobalValue::AvailableExternallyLinkage:
1369 Out << "available_externally ";
1370 break;
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001371 }
1372}
Duncan Sands12da8ce2009-03-07 15:45:40 +00001373
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001374
1375static void PrintVisibility(GlobalValue::VisibilityTypes Vis,
Dan Gohmane2745262009-08-12 17:23:50 +00001376 formatted_raw_ostream &Out) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001377 switch (Vis) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001378 case GlobalValue::DefaultVisibility: break;
1379 case GlobalValue::HiddenVisibility: Out << "hidden "; break;
1380 case GlobalValue::ProtectedVisibility: Out << "protected "; break;
1381 }
1382}
1383
Hans Wennborgcbe34b42012-06-23 11:37:03 +00001384static void PrintThreadLocalModel(GlobalVariable::ThreadLocalMode TLM,
1385 formatted_raw_ostream &Out) {
1386 switch (TLM) {
1387 case GlobalVariable::NotThreadLocal:
1388 break;
1389 case GlobalVariable::GeneralDynamicTLSModel:
1390 Out << "thread_local ";
1391 break;
1392 case GlobalVariable::LocalDynamicTLSModel:
1393 Out << "thread_local(localdynamic) ";
1394 break;
1395 case GlobalVariable::InitialExecTLSModel:
1396 Out << "thread_local(initialexec) ";
1397 break;
1398 case GlobalVariable::LocalExecTLSModel:
1399 Out << "thread_local(localexec) ";
1400 break;
1401 }
1402}
1403
Chris Lattner7bfee412001-10-29 16:05:51 +00001404void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Dan Gohman5ded1422010-01-29 23:12:36 +00001405 if (GV->isMaterializable())
1406 Out << "; Materializable\n";
1407
Dan Gohmana2489d12010-07-20 23:55:01 +00001408 WriteAsOperandInternal(Out, GV, &TypePrinter, &Machine, GV->getParent());
Dan Gohman466876b2009-08-12 23:32:33 +00001409 Out << " = ";
Chris Lattner37798642001-09-18 04:01:05 +00001410
Chris Lattner1508d3f2008-08-19 05:16:28 +00001411 if (!GV->hasInitializer() && GV->hasExternalLinkage())
1412 Out << "external ";
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001413
Chris Lattner1508d3f2008-08-19 05:16:28 +00001414 PrintLinkage(GV->getLinkage(), Out);
1415 PrintVisibility(GV->getVisibility(), Out);
Hans Wennborgcbe34b42012-06-23 11:37:03 +00001416 PrintThreadLocalModel(GV->getThreadLocalMode(), Out);
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +00001417
Chris Lattnerac161bf2009-01-02 07:01:27 +00001418 if (unsigned AddressSpace = GV->getType()->getAddressSpace())
1419 Out << "addrspace(" << AddressSpace << ") ";
Rafael Espindola45e6c192011-01-08 16:42:36 +00001420 if (GV->hasUnnamedAddr()) Out << "unnamed_addr ";
Misha Brukmana6619a92004-06-21 21:53:56 +00001421 Out << (GV->isConstant() ? "constant " : "global ");
Chris Lattnere101c442009-02-28 21:26:53 +00001422 TypePrinter.print(GV->getType()->getElementType(), Out);
Chris Lattner37798642001-09-18 04:01:05 +00001423
Dan Gohman81313fd2008-09-14 17:21:12 +00001424 if (GV->hasInitializer()) {
1425 Out << ' ';
Devang Patel983c6b12009-07-08 21:44:25 +00001426 writeOperand(GV->getInitializer(), false);
Dan Gohman81313fd2008-09-14 17:21:12 +00001427 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001428
Chris Lattner9380b812010-07-07 23:16:37 +00001429 if (GV->hasSection()) {
1430 Out << ", section \"";
1431 PrintEscapedString(GV->getSection(), Out);
1432 Out << '"';
1433 }
Chris Lattner4b96c542005-11-12 00:10:19 +00001434 if (GV->getAlignment())
Chris Lattnerf8a974d2005-11-06 06:48:53 +00001435 Out << ", align " << GV->getAlignment();
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001436
Chris Lattner113f4f42002-06-25 16:13:24 +00001437 printInfoComment(*GV);
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001438 Out << '\n';
Chris Lattnerda975502001-09-10 07:58:01 +00001439}
1440
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001441void AssemblyWriter::printAlias(const GlobalAlias *GA) {
Dan Gohman5ded1422010-01-29 23:12:36 +00001442 if (GA->isMaterializable())
1443 Out << "; Materializable\n";
1444
Dale Johannesen83e468a2008-06-03 18:14:29 +00001445 // Don't crash when dumping partially built GA
1446 if (!GA->hasName())
1447 Out << "<<nameless>> = ";
Chris Lattner033935d2008-08-17 04:40:13 +00001448 else {
1449 PrintLLVMName(Out, GA);
1450 Out << " = ";
1451 }
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001452 PrintVisibility(GA->getVisibility(), Out);
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001453
1454 Out << "alias ";
1455
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001456 PrintLinkage(GA->getLinkage(), Out);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001457
Anton Korobeynikov546ea7e2007-04-29 18:02:48 +00001458 const Constant *Aliasee = GA->getAliasee();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001459
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001460 if (Aliasee == 0) {
1461 TypePrinter.print(GA->getType(), Out);
1462 Out << " <<NULL ALIASEE>>";
Jay Foad92c19132011-08-01 12:48:54 +00001463 } else {
Jay Foad26db79d2011-08-01 12:29:14 +00001464 writeOperand(Aliasee, !isa<ConstantExpr>(Aliasee));
Jay Foad92c19132011-08-01 12:48:54 +00001465 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001466
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001467 printInfoComment(*GA);
Chris Lattner1508d3f2008-08-19 05:16:28 +00001468 Out << '\n';
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001469}
1470
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001471void AssemblyWriter::printTypeIdentities() {
1472 if (TypePrinter.NumberedTypes.empty() &&
1473 TypePrinter.NamedTypes.empty())
1474 return;
Andrew Trickec4b6e72011-09-30 19:48:58 +00001475
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001476 Out << '\n';
Andrew Trickec4b6e72011-09-30 19:48:58 +00001477
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001478 // We know all the numbers that each type is used and we know that it is a
1479 // dense assignment. Convert the map to an index table.
1480 std::vector<StructType*> NumberedTypes(TypePrinter.NumberedTypes.size());
Andrew Trickec4b6e72011-09-30 19:48:58 +00001481 for (DenseMap<StructType*, unsigned>::iterator I =
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001482 TypePrinter.NumberedTypes.begin(), E = TypePrinter.NumberedTypes.end();
1483 I != E; ++I) {
1484 assert(I->second < NumberedTypes.size() && "Didn't get a dense numbering?");
1485 NumberedTypes[I->second] = I->first;
1486 }
Andrew Trickec4b6e72011-09-30 19:48:58 +00001487
Chris Lattner3243ea12009-03-01 00:03:38 +00001488 // Emit all numbered types.
1489 for (unsigned i = 0, e = NumberedTypes.size(); i != e; ++i) {
Dan Gohman466876b2009-08-12 23:32:33 +00001490 Out << '%' << i << " = type ";
Andrew Trickec4b6e72011-09-30 19:48:58 +00001491
Chris Lattner3243ea12009-03-01 00:03:38 +00001492 // Make sure we print out at least one level of the type structure, so
1493 // that we do not get %2 = type %2
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001494 TypePrinter.printStructBody(NumberedTypes[i], Out);
Dan Gohman69273e62009-08-12 23:54:22 +00001495 Out << '\n';
Chris Lattner3243ea12009-03-01 00:03:38 +00001496 }
Andrew Trickec4b6e72011-09-30 19:48:58 +00001497
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001498 for (unsigned i = 0, e = TypePrinter.NamedTypes.size(); i != e; ++i) {
1499 PrintLLVMName(Out, TypePrinter.NamedTypes[i]->getName(), LocalPrefix);
Chris Lattner1508d3f2008-08-19 05:16:28 +00001500 Out << " = type ";
Reid Spencere7e96712004-05-25 08:53:40 +00001501
1502 // Make sure we print out at least one level of the type structure, so
1503 // that we do not get %FILE = type %FILE
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001504 TypePrinter.printStructBody(TypePrinter.NamedTypes[i], Out);
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001505 Out << '\n';
Reid Spencere7e96712004-05-25 08:53:40 +00001506 }
Reid Spencer32af9e82007-01-06 07:24:44 +00001507}
1508
Misha Brukmanc566ca362004-03-02 00:22:19 +00001509/// printFunction - Print all aspects of a function.
1510///
Chris Lattner113f4f42002-06-25 16:13:24 +00001511void AssemblyWriter::printFunction(const Function *F) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001512 // Print out the return type and name.
1513 Out << '\n';
Chris Lattner379a8d22003-04-16 20:28:45 +00001514
Misha Brukmana6619a92004-06-21 21:53:56 +00001515 if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001516
Dan Gohman5ded1422010-01-29 23:12:36 +00001517 if (F->isMaterializable())
1518 Out << "; Materializable\n";
1519
Reid Spencer5301e7c2007-01-30 20:08:39 +00001520 if (F->isDeclaration())
Chris Lattner10f03a62007-08-19 22:15:26 +00001521 Out << "declare ";
1522 else
Reid Spencer7ce2d2a2006-12-29 20:29:48 +00001523 Out << "define ";
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001524
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001525 PrintLinkage(F->getLinkage(), Out);
1526 PrintVisibility(F->getVisibility(), Out);
Chris Lattner379a8d22003-04-16 20:28:45 +00001527
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001528 // Print the calling convention.
1529 switch (F->getCallingConv()) {
1530 case CallingConv::C: break; // default
Anton Korobeynikov3c5b3df2006-09-20 22:03:51 +00001531 case CallingConv::Fast: Out << "fastcc "; break;
1532 case CallingConv::Cold: Out << "coldcc "; break;
1533 case CallingConv::X86_StdCall: Out << "x86_stdcallcc "; break;
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001534 case CallingConv::X86_FastCall: Out << "x86_fastcallcc "; break;
Anton Korobeynikov8f35fab2010-05-16 09:08:45 +00001535 case CallingConv::X86_ThisCall: Out << "x86_thiscallcc "; break;
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001536 case CallingConv::ARM_APCS: Out << "arm_apcscc "; break;
1537 case CallingConv::ARM_AAPCS: Out << "arm_aapcscc "; break;
1538 case CallingConv::ARM_AAPCS_VFP:Out << "arm_aapcs_vfpcc "; break;
Anton Korobeynikov27a0ecf2009-12-07 02:27:35 +00001539 case CallingConv::MSP430_INTR: Out << "msp430_intrcc "; break;
Justin Holewinski0cfa7372011-03-07 14:32:30 +00001540 case CallingConv::PTX_Kernel: Out << "ptx_kernel "; break;
1541 case CallingConv::PTX_Device: Out << "ptx_device "; break;
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001542 default: Out << "cc" << F->getCallingConv() << " "; break;
1543 }
1544
Chris Lattner229907c2011-07-18 04:54:35 +00001545 FunctionType *FT = F->getFunctionType();
Devang Patel4c758ea2008-09-25 21:00:45 +00001546 const AttrListPtr &Attrs = F->getAttributes();
Devang Patel221fe422008-09-29 20:49:50 +00001547 Attributes RetAttrs = Attrs.getRetAttributes();
1548 if (RetAttrs != Attribute::None)
1549 Out << Attribute::getAsString(Attrs.getRetAttributes()) << ' ';
Chris Lattnere101c442009-02-28 21:26:53 +00001550 TypePrinter.print(F->getReturnType(), Out);
Chris Lattner585297e82008-08-19 05:26:17 +00001551 Out << ' ';
Dan Gohmana2489d12010-07-20 23:55:01 +00001552 WriteAsOperandInternal(Out, F, &TypePrinter, &Machine, F->getParent());
Misha Brukmana6619a92004-06-21 21:53:56 +00001553 Out << '(';
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001554 Machine.incorporateFunction(F);
Chris Lattnerfee714f2001-09-07 16:36:04 +00001555
Chris Lattner7bfee412001-10-29 16:05:51 +00001556 // Loop over the arguments, printing them...
Chris Lattnerfee714f2001-09-07 16:36:04 +00001557
Reid Spencer8c4914c2006-12-31 05:24:50 +00001558 unsigned Idx = 1;
Chris Lattner82738fe2007-04-18 00:57:22 +00001559 if (!F->isDeclaration()) {
1560 // If this isn't a declaration, print the argument names as well.
1561 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
1562 I != E; ++I) {
1563 // Insert commas as we go... the first arg doesn't get a comma
1564 if (I != F->arg_begin()) Out << ", ";
Devang Patela05633e2008-09-26 22:53:05 +00001565 printArgument(I, Attrs.getParamAttributes(Idx));
Chris Lattner82738fe2007-04-18 00:57:22 +00001566 Idx++;
1567 }
1568 } else {
1569 // Otherwise, print the types from the function type.
1570 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
1571 // Insert commas as we go... the first arg doesn't get a comma
1572 if (i) Out << ", ";
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001573
Chris Lattner82738fe2007-04-18 00:57:22 +00001574 // Output type...
Chris Lattnere101c442009-02-28 21:26:53 +00001575 TypePrinter.print(FT->getParamType(i), Out);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001576
Devang Patela05633e2008-09-26 22:53:05 +00001577 Attributes ArgAttrs = Attrs.getParamAttributes(i+1);
Devang Patel4c758ea2008-09-25 21:00:45 +00001578 if (ArgAttrs != Attribute::None)
1579 Out << ' ' << Attribute::getAsString(ArgAttrs);
Chris Lattner82738fe2007-04-18 00:57:22 +00001580 }
Reid Spencer8c4914c2006-12-31 05:24:50 +00001581 }
Chris Lattnerfee714f2001-09-07 16:36:04 +00001582
1583 // Finish printing arguments...
Chris Lattner113f4f42002-06-25 16:13:24 +00001584 if (FT->isVarArg()) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001585 if (FT->getNumParams()) Out << ", ";
1586 Out << "..."; // Output varargs portion of signature!
Chris Lattnerfee714f2001-09-07 16:36:04 +00001587 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001588 Out << ')';
Rafael Espindola563eb4b2011-01-25 19:09:56 +00001589 if (F->hasUnnamedAddr())
1590 Out << " unnamed_addr";
Devang Patela05633e2008-09-26 22:53:05 +00001591 Attributes FnAttrs = Attrs.getFnAttributes();
1592 if (FnAttrs != Attribute::None)
1593 Out << ' ' << Attribute::getAsString(Attrs.getFnAttributes());
Chris Lattner9380b812010-07-07 23:16:37 +00001594 if (F->hasSection()) {
1595 Out << " section \"";
1596 PrintEscapedString(F->getSection(), Out);
1597 Out << '"';
1598 }
Chris Lattnerf8a974d2005-11-06 06:48:53 +00001599 if (F->getAlignment())
1600 Out << " align " << F->getAlignment();
Gordon Henriksend930f912008-08-17 18:44:35 +00001601 if (F->hasGC())
1602 Out << " gc \"" << F->getGC() << '"';
Reid Spencer5301e7c2007-01-30 20:08:39 +00001603 if (F->isDeclaration()) {
Chris Lattnerfb483622010-09-02 22:52:10 +00001604 Out << '\n';
Chris Lattnerb2f02e52002-05-06 03:00:40 +00001605 } else {
Chris Lattnerfb483622010-09-02 22:52:10 +00001606 Out << " {";
1607 // Output all of the function's basic blocks.
Chris Lattner113f4f42002-06-25 16:13:24 +00001608 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
1609 printBasicBlock(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +00001610
Misha Brukmana6619a92004-06-21 21:53:56 +00001611 Out << "}\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +00001612 }
1613
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001614 Machine.purgeFunction();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001615}
1616
Misha Brukmanc566ca362004-03-02 00:22:19 +00001617/// printArgument - This member is called for every argument that is passed into
1618/// the function. Simply print it out
1619///
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001620void AssemblyWriter::printArgument(const Argument *Arg,
Devang Patelba3fa6c2008-09-23 23:03:40 +00001621 Attributes Attrs) {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001622 // Output type...
Chris Lattnere101c442009-02-28 21:26:53 +00001623 TypePrinter.print(Arg->getType(), Out);
Misha Brukmanb1c93172005-04-21 23:48:37 +00001624
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00001625 // Output parameter attributes list
Devang Patel4c758ea2008-09-25 21:00:45 +00001626 if (Attrs != Attribute::None)
1627 Out << ' ' << Attribute::getAsString(Attrs);
Reid Spencer8c4914c2006-12-31 05:24:50 +00001628
Chris Lattner2f7c9632001-06-06 20:29:01 +00001629 // Output name, if available...
Chris Lattner033935d2008-08-17 04:40:13 +00001630 if (Arg->hasName()) {
1631 Out << ' ';
1632 PrintLLVMName(Out, Arg);
1633 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001634}
1635
Misha Brukmanc566ca362004-03-02 00:22:19 +00001636/// printBasicBlock - This member is called for each basic block in a method.
1637///
Chris Lattner7bfee412001-10-29 16:05:51 +00001638void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Nick Lewycky4d43d3c2008-04-25 16:53:59 +00001639 if (BB->hasName()) { // Print out the label if it exists...
Chris Lattner033935d2008-08-17 04:40:13 +00001640 Out << "\n";
Daniel Dunbare03eecb2009-07-25 23:55:21 +00001641 PrintLLVMName(Out, BB->getName(), LabelPrefix);
Chris Lattner033935d2008-08-17 04:40:13 +00001642 Out << ':';
Nick Lewycky4d43d3c2008-04-25 16:53:59 +00001643 } else if (!BB->use_empty()) { // Don't print block # of no uses...
Bill Wendling35a9c3c2011-04-10 23:18:04 +00001644 Out << "\n; <label>:";
Chris Lattner5e043322007-01-11 03:54:27 +00001645 int Slot = Machine.getLocalSlot(BB);
Chris Lattner757ee0b2004-06-09 19:41:19 +00001646 if (Slot != -1)
Misha Brukmana6619a92004-06-21 21:53:56 +00001647 Out << Slot;
Chris Lattner757ee0b2004-06-09 19:41:19 +00001648 else
Misha Brukmana6619a92004-06-21 21:53:56 +00001649 Out << "<badref>";
Chris Lattner58185f22002-10-02 19:38:55 +00001650 }
Chris Lattner2447ef52003-11-20 00:09:43 +00001651
Dan Gohmane2745262009-08-12 17:23:50 +00001652 if (BB->getParent() == 0) {
Chris Lattneree97b8b2009-08-17 15:48:08 +00001653 Out.PadToColumn(50);
Dan Gohmane2745262009-08-12 17:23:50 +00001654 Out << "; Error: Block without parent!";
1655 } else if (BB != &BB->getParent()->getEntryBlock()) { // Not the entry block?
Chris Lattnerfb483622010-09-02 22:52:10 +00001656 // Output predecessors for the block.
Chris Lattneree97b8b2009-08-17 15:48:08 +00001657 Out.PadToColumn(50);
Dan Gohmane2745262009-08-12 17:23:50 +00001658 Out << ";";
Gabor Greif6c6b2fd2010-03-25 23:25:28 +00001659 const_pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001660
Chris Lattnerff834c02008-04-22 02:45:44 +00001661 if (PI == PE) {
1662 Out << " No predecessors!";
1663 } else {
Dan Gohman81313fd2008-09-14 17:21:12 +00001664 Out << " preds = ";
Chris Lattnerff834c02008-04-22 02:45:44 +00001665 writeOperand(*PI, false);
1666 for (++PI; PI != PE; ++PI) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001667 Out << ", ";
Chris Lattner78e2e8b2006-12-06 06:24:27 +00001668 writeOperand(*PI, false);
Chris Lattner00211f12003-11-16 22:59:57 +00001669 }
Chris Lattner58185f22002-10-02 19:38:55 +00001670 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001671 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001672
Chris Lattnerff834c02008-04-22 02:45:44 +00001673 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001674
Misha Brukmana6619a92004-06-21 21:53:56 +00001675 if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001676
Chris Lattnerfee714f2001-09-07 16:36:04 +00001677 // Output all of the instructions in the basic block...
Dan Gohmana4f709e2009-07-13 18:27:59 +00001678 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
Chris Lattner113f4f42002-06-25 16:13:24 +00001679 printInstruction(*I);
Dan Gohmana4f709e2009-07-13 18:27:59 +00001680 Out << '\n';
1681 }
Chris Lattner96cdd272004-03-08 18:51:45 +00001682
Misha Brukmana6619a92004-06-21 21:53:56 +00001683 if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, Out);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001684}
1685
Misha Brukmanc566ca362004-03-02 00:22:19 +00001686/// printInfoComment - Print a little comment after the instruction indicating
1687/// which slot it occupies.
1688///
Chris Lattner113f4f42002-06-25 16:13:24 +00001689void AssemblyWriter::printInfoComment(const Value &V) {
Dan Gohmane34890f2010-02-10 20:41:46 +00001690 if (AnnotationWriter) {
1691 AnnotationWriter->printInfoComment(V, Out);
1692 return;
1693 }
Chris Lattner862e3382001-10-13 06:42:36 +00001694}
1695
Reid Spencere7141c82006-08-28 01:02:49 +00001696// This member is called for each Instruction in a function..
Chris Lattner113f4f42002-06-25 16:13:24 +00001697void AssemblyWriter::printInstruction(const Instruction &I) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001698 if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001699
Dan Gohman466876b2009-08-12 23:32:33 +00001700 // Print out indentation for an instruction.
Dan Gohmanb4583b12009-08-13 01:41:52 +00001701 Out << " ";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001702
1703 // Print out name if it exists...
Chris Lattner033935d2008-08-17 04:40:13 +00001704 if (I.hasName()) {
1705 PrintLLVMName(Out, &I);
1706 Out << " = ";
Chris Lattner031560c2009-12-29 07:25:48 +00001707 } else if (!I.getType()->isVoidTy()) {
Chris Lattnerb25e5ea2008-08-29 17:19:30 +00001708 // Print out the def slot taken.
1709 int SlotNum = Machine.getLocalSlot(&I);
1710 if (SlotNum == -1)
1711 Out << "<badref> = ";
1712 else
1713 Out << '%' << SlotNum << " = ";
Chris Lattner033935d2008-08-17 04:40:13 +00001714 }
Andrew Trickec4b6e72011-09-30 19:48:58 +00001715
Eli Friedman59b66882011-08-09 23:02:53 +00001716 if (isa<CallInst>(I) && cast<CallInst>(I).isTailCall())
Chris Lattner06038452005-05-06 05:51:46 +00001717 Out << "tail ";
Chris Lattner504f9242003-09-08 17:45:59 +00001718
Chris Lattner2f7c9632001-06-06 20:29:01 +00001719 // Print out the opcode...
Misha Brukmana6619a92004-06-21 21:53:56 +00001720 Out << I.getOpcodeName();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001721
Eli Friedman02e737b2011-08-12 22:50:01 +00001722 // If this is an atomic load or store, print out the atomic marker.
1723 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isAtomic()) ||
1724 (isa<StoreInst>(I) && cast<StoreInst>(I).isAtomic()))
1725 Out << " atomic";
1726
1727 // If this is a volatile operation, print out the volatile marker.
1728 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) ||
1729 (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile()) ||
1730 (isa<AtomicCmpXchgInst>(I) && cast<AtomicCmpXchgInst>(I).isVolatile()) ||
1731 (isa<AtomicRMWInst>(I) && cast<AtomicRMWInst>(I).isVolatile()))
1732 Out << " volatile";
1733
Dan Gohman9c7f8082009-07-27 16:11:46 +00001734 // Print out optimization information.
1735 WriteOptimizationInfo(Out, &I);
1736
Reid Spencer45e52392006-12-03 06:27:29 +00001737 // Print out the compare instruction predicates
Nate Begemand2195702008-05-12 19:01:56 +00001738 if (const CmpInst *CI = dyn_cast<CmpInst>(&I))
Chris Lattner82ff9232008-08-23 22:52:27 +00001739 Out << ' ' << getPredicateText(CI->getPredicate());
Reid Spencer45e52392006-12-03 06:27:29 +00001740
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001741 // Print out the atomicrmw operation
1742 if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(&I))
1743 writeAtomicRMWOperation(Out, RMWI->getOperation());
1744
Chris Lattner2f7c9632001-06-06 20:29:01 +00001745 // Print out the type of the operands...
Chris Lattner113f4f42002-06-25 16:13:24 +00001746 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001747
1748 // Special case conditional branches to swizzle the condition out to the front
Gabor Greifcab008f2009-02-09 15:45:06 +00001749 if (isa<BranchInst>(I) && cast<BranchInst>(I).isConditional()) {
1750 BranchInst &BI(cast<BranchInst>(I));
Dan Gohman81313fd2008-09-14 17:21:12 +00001751 Out << ' ';
Gabor Greifcab008f2009-02-09 15:45:06 +00001752 writeOperand(BI.getCondition(), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00001753 Out << ", ";
Gabor Greifcab008f2009-02-09 15:45:06 +00001754 writeOperand(BI.getSuccessor(0), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00001755 Out << ", ";
Gabor Greifcab008f2009-02-09 15:45:06 +00001756 writeOperand(BI.getSuccessor(1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001757
Chris Lattner8d48df22002-04-13 18:34:38 +00001758 } else if (isa<SwitchInst>(I)) {
Eli Friedman95031ed2011-09-29 20:21:17 +00001759 SwitchInst& SI(cast<SwitchInst>(I));
Chris Lattner3ed871f2009-10-27 19:13:16 +00001760 // Special case switch instruction to get formatting nice and correct.
Dan Gohman81313fd2008-09-14 17:21:12 +00001761 Out << ' ';
Eli Friedman95031ed2011-09-29 20:21:17 +00001762 writeOperand(SI.getCondition(), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00001763 Out << ", ";
Eli Friedman95031ed2011-09-29 20:21:17 +00001764 writeOperand(SI.getDefaultDest(), true);
Chris Lattner82ff9232008-08-23 22:52:27 +00001765 Out << " [";
Stepan Dyatkovskiy97b02fc2012-03-11 06:09:17 +00001766 for (SwitchInst::CaseIt i = SI.case_begin(), e = SI.case_end();
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00001767 i != e; ++i) {
Dan Gohmanb4583b12009-08-13 01:41:52 +00001768 Out << "\n ";
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00001769 writeOperand(i.getCaseValue(), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00001770 Out << ", ";
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00001771 writeOperand(i.getCaseSuccessor(), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001772 }
Dan Gohmanb4583b12009-08-13 01:41:52 +00001773 Out << "\n ]";
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00001774 } else if (isa<IndirectBrInst>(I)) {
1775 // Special case indirectbr instruction to get formatting nice and correct.
Chris Lattner3ed871f2009-10-27 19:13:16 +00001776 Out << ' ';
1777 writeOperand(Operand, true);
Dan Gohman43c57402009-10-30 02:01:10 +00001778 Out << ", [";
Andrew Trickec4b6e72011-09-30 19:48:58 +00001779
Chris Lattner3ed871f2009-10-27 19:13:16 +00001780 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) {
1781 if (i != 1)
1782 Out << ", ";
1783 writeOperand(I.getOperand(i), true);
1784 }
1785 Out << ']';
Jay Foad372ad642011-06-20 14:18:48 +00001786 } else if (const PHINode *PN = dyn_cast<PHINode>(&I)) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001787 Out << ' ';
Chris Lattnere101c442009-02-28 21:26:53 +00001788 TypePrinter.print(I.getType(), Out);
Misha Brukmana6619a92004-06-21 21:53:56 +00001789 Out << ' ';
Chris Lattner2f7c9632001-06-06 20:29:01 +00001790
Jay Foad372ad642011-06-20 14:18:48 +00001791 for (unsigned op = 0, Eop = PN->getNumIncomingValues(); op < Eop; ++op) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001792 if (op) Out << ", ";
Dan Gohman81313fd2008-09-14 17:21:12 +00001793 Out << "[ ";
Jay Foad372ad642011-06-20 14:18:48 +00001794 writeOperand(PN->getIncomingValue(op), false); Out << ", ";
1795 writeOperand(PN->getIncomingBlock(op), false); Out << " ]";
Chris Lattner931ef3b2001-06-11 15:04:20 +00001796 }
Dan Gohmana76f0f72008-05-31 19:12:39 +00001797 } else if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(&I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001798 Out << ' ';
Dan Gohmana76f0f72008-05-31 19:12:39 +00001799 writeOperand(I.getOperand(0), true);
1800 for (const unsigned *i = EVI->idx_begin(), *e = EVI->idx_end(); i != e; ++i)
1801 Out << ", " << *i;
1802 } else if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(&I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001803 Out << ' ';
1804 writeOperand(I.getOperand(0), true); Out << ", ";
Dan Gohmana76f0f72008-05-31 19:12:39 +00001805 writeOperand(I.getOperand(1), true);
1806 for (const unsigned *i = IVI->idx_begin(), *e = IVI->idx_end(); i != e; ++i)
1807 Out << ", " << *i;
Bill Wendlingfae14752011-08-12 20:24:12 +00001808 } else if (const LandingPadInst *LPI = dyn_cast<LandingPadInst>(&I)) {
1809 Out << ' ';
1810 TypePrinter.print(I.getType(), Out);
1811 Out << " personality ";
1812 writeOperand(I.getOperand(0), true); Out << '\n';
1813
1814 if (LPI->isCleanup())
1815 Out << " cleanup";
1816
1817 for (unsigned i = 0, e = LPI->getNumClauses(); i != e; ++i) {
1818 if (i != 0 || LPI->isCleanup()) Out << "\n";
1819 if (LPI->isCatch(i))
1820 Out << " catch ";
1821 else
1822 Out << " filter ";
1823
1824 writeOperand(LPI->getClause(i), true);
1825 }
Devang Patel59643e52008-02-23 00:35:18 +00001826 } else if (isa<ReturnInst>(I) && !Operand) {
1827 Out << " void";
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001828 } else if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
1829 // Print the calling convention being used.
1830 switch (CI->getCallingConv()) {
1831 case CallingConv::C: break; // default
Chris Lattner29d20852006-05-19 21:58:52 +00001832 case CallingConv::Fast: Out << " fastcc"; break;
1833 case CallingConv::Cold: Out << " coldcc"; break;
Chris Lattnerf5270372007-11-18 18:32:16 +00001834 case CallingConv::X86_StdCall: Out << " x86_stdcallcc"; break;
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001835 case CallingConv::X86_FastCall: Out << " x86_fastcallcc"; break;
Anton Korobeynikov8f35fab2010-05-16 09:08:45 +00001836 case CallingConv::X86_ThisCall: Out << " x86_thiscallcc"; break;
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001837 case CallingConv::ARM_APCS: Out << " arm_apcscc "; break;
1838 case CallingConv::ARM_AAPCS: Out << " arm_aapcscc "; break;
1839 case CallingConv::ARM_AAPCS_VFP:Out << " arm_aapcs_vfpcc "; break;
Anton Korobeynikov27a0ecf2009-12-07 02:27:35 +00001840 case CallingConv::MSP430_INTR: Out << " msp430_intrcc "; break;
Che-Liang Chiou29947902010-09-25 07:46:17 +00001841 case CallingConv::PTX_Kernel: Out << " ptx_kernel"; break;
1842 case CallingConv::PTX_Device: Out << " ptx_device"; break;
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001843 default: Out << " cc" << CI->getCallingConv(); break;
1844 }
1845
Gabor Greifc9a92512010-06-23 13:09:06 +00001846 Operand = CI->getCalledValue();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001847 PointerType *PTy = cast<PointerType>(Operand->getType());
1848 FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1849 Type *RetTy = FTy->getReturnType();
Devang Patel4c758ea2008-09-25 21:00:45 +00001850 const AttrListPtr &PAL = CI->getAttributes();
Chris Lattner2f2d9472001-11-06 21:28:12 +00001851
Devang Patel221fe422008-09-29 20:49:50 +00001852 if (PAL.getRetAttributes() != Attribute::None)
1853 Out << ' ' << Attribute::getAsString(PAL.getRetAttributes());
1854
Chris Lattner463d6a52003-08-05 15:34:45 +00001855 // If possible, print out the short form of the call instruction. We can
Chris Lattner6915f8f2002-04-07 22:49:37 +00001856 // only do this if the first argument is a pointer to a nonvararg function,
Chris Lattner463d6a52003-08-05 15:34:45 +00001857 // and if the return type is not a pointer to a function.
Chris Lattner2f2d9472001-11-06 21:28:12 +00001858 //
Dan Gohman81313fd2008-09-14 17:21:12 +00001859 Out << ' ';
Chris Lattner463d6a52003-08-05 15:34:45 +00001860 if (!FTy->isVarArg() &&
Duncan Sands19d0b472010-02-16 11:11:14 +00001861 (!RetTy->isPointerTy() ||
1862 !cast<PointerType>(RetTy)->getElementType()->isFunctionTy())) {
Chris Lattnere101c442009-02-28 21:26:53 +00001863 TypePrinter.print(RetTy, Out);
Dan Gohman81313fd2008-09-14 17:21:12 +00001864 Out << ' ';
Chris Lattner2f2d9472001-11-06 21:28:12 +00001865 writeOperand(Operand, false);
1866 } else {
1867 writeOperand(Operand, true);
1868 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001869 Out << '(';
Gabor Greifc9a92512010-06-23 13:09:06 +00001870 for (unsigned op = 0, Eop = CI->getNumArgOperands(); op < Eop; ++op) {
1871 if (op > 0)
Dan Gohman81313fd2008-09-14 17:21:12 +00001872 Out << ", ";
Gabor Greifc9a92512010-06-23 13:09:06 +00001873 writeParamOperand(CI->getArgOperand(op), PAL.getParamAttributes(op + 1));
Chris Lattner2f7c9632001-06-06 20:29:01 +00001874 }
Dan Gohman81313fd2008-09-14 17:21:12 +00001875 Out << ')';
Devang Patela05633e2008-09-26 22:53:05 +00001876 if (PAL.getFnAttributes() != Attribute::None)
1877 Out << ' ' << Attribute::getAsString(PAL.getFnAttributes());
Chris Lattner113f4f42002-06-25 16:13:24 +00001878 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
Gabor Greifa2fbc0a2010-03-24 13:21:49 +00001879 Operand = II->getCalledValue();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001880 PointerType *PTy = cast<PointerType>(Operand->getType());
1881 FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1882 Type *RetTy = FTy->getReturnType();
Devang Patel4c758ea2008-09-25 21:00:45 +00001883 const AttrListPtr &PAL = II->getAttributes();
Chris Lattner463d6a52003-08-05 15:34:45 +00001884
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001885 // Print the calling convention being used.
1886 switch (II->getCallingConv()) {
1887 case CallingConv::C: break; // default
Chris Lattner29d20852006-05-19 21:58:52 +00001888 case CallingConv::Fast: Out << " fastcc"; break;
1889 case CallingConv::Cold: Out << " coldcc"; break;
Dan Gohman81313fd2008-09-14 17:21:12 +00001890 case CallingConv::X86_StdCall: Out << " x86_stdcallcc"; break;
1891 case CallingConv::X86_FastCall: Out << " x86_fastcallcc"; break;
Anton Korobeynikov8f35fab2010-05-16 09:08:45 +00001892 case CallingConv::X86_ThisCall: Out << " x86_thiscallcc"; break;
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001893 case CallingConv::ARM_APCS: Out << " arm_apcscc "; break;
1894 case CallingConv::ARM_AAPCS: Out << " arm_aapcscc "; break;
1895 case CallingConv::ARM_AAPCS_VFP:Out << " arm_aapcs_vfpcc "; break;
Anton Korobeynikov27a0ecf2009-12-07 02:27:35 +00001896 case CallingConv::MSP430_INTR: Out << " msp430_intrcc "; break;
Che-Liang Chiou29947902010-09-25 07:46:17 +00001897 case CallingConv::PTX_Kernel: Out << " ptx_kernel"; break;
1898 case CallingConv::PTX_Device: Out << " ptx_device"; break;
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001899 default: Out << " cc" << II->getCallingConv(); break;
1900 }
1901
Devang Patel221fe422008-09-29 20:49:50 +00001902 if (PAL.getRetAttributes() != Attribute::None)
1903 Out << ' ' << Attribute::getAsString(PAL.getRetAttributes());
1904
Chris Lattner463d6a52003-08-05 15:34:45 +00001905 // If possible, print out the short form of the invoke instruction. We can
1906 // only do this if the first argument is a pointer to a nonvararg function,
1907 // and if the return type is not a pointer to a function.
1908 //
Dan Gohmanc7e00ba2008-10-15 18:02:08 +00001909 Out << ' ';
Chris Lattner463d6a52003-08-05 15:34:45 +00001910 if (!FTy->isVarArg() &&
Duncan Sands19d0b472010-02-16 11:11:14 +00001911 (!RetTy->isPointerTy() ||
1912 !cast<PointerType>(RetTy)->getElementType()->isFunctionTy())) {
Chris Lattnere101c442009-02-28 21:26:53 +00001913 TypePrinter.print(RetTy, Out);
Dan Gohmanc7e00ba2008-10-15 18:02:08 +00001914 Out << ' ';
Chris Lattner463d6a52003-08-05 15:34:45 +00001915 writeOperand(Operand, false);
1916 } else {
1917 writeOperand(Operand, true);
1918 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001919 Out << '(';
Gabor Greifc9a92512010-06-23 13:09:06 +00001920 for (unsigned op = 0, Eop = II->getNumArgOperands(); op < Eop; ++op) {
Gabor Greifa2fbc0a2010-03-24 13:21:49 +00001921 if (op)
Dan Gohman81313fd2008-09-14 17:21:12 +00001922 Out << ", ";
Gabor Greifc9a92512010-06-23 13:09:06 +00001923 writeParamOperand(II->getArgOperand(op), PAL.getParamAttributes(op + 1));
Chris Lattner862e3382001-10-13 06:42:36 +00001924 }
1925
Dan Gohman81313fd2008-09-14 17:21:12 +00001926 Out << ')';
Devang Patela05633e2008-09-26 22:53:05 +00001927 if (PAL.getFnAttributes() != Attribute::None)
1928 Out << ' ' << Attribute::getAsString(PAL.getFnAttributes());
1929
Dan Gohmanb4583b12009-08-13 01:41:52 +00001930 Out << "\n to ";
Chris Lattner862e3382001-10-13 06:42:36 +00001931 writeOperand(II->getNormalDest(), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00001932 Out << " unwind ";
Chris Lattnerfae8ab32004-02-08 21:44:31 +00001933 writeOperand(II->getUnwindDest(), true);
Chris Lattner862e3382001-10-13 06:42:36 +00001934
Victor Hernandez8acf2952009-10-23 21:09:37 +00001935 } else if (const AllocaInst *AI = dyn_cast<AllocaInst>(&I)) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001936 Out << ' ';
Chris Lattnere101c442009-02-28 21:26:53 +00001937 TypePrinter.print(AI->getType()->getElementType(), Out);
Dan Gohmanb53e26c2009-07-31 18:23:24 +00001938 if (!AI->getArraySize() || AI->isArrayAllocation()) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001939 Out << ", ";
Chris Lattner8d48df22002-04-13 18:34:38 +00001940 writeOperand(AI->getArraySize(), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001941 }
Nate Begeman848622f2005-11-05 09:21:28 +00001942 if (AI->getAlignment()) {
Chris Lattner7aeee3a2005-11-05 21:20:34 +00001943 Out << ", align " << AI->getAlignment();
Nate Begeman848622f2005-11-05 09:21:28 +00001944 }
Chris Lattner862e3382001-10-13 06:42:36 +00001945 } else if (isa<CastInst>(I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001946 if (Operand) {
1947 Out << ' ';
1948 writeOperand(Operand, true); // Work with broken code
1949 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001950 Out << " to ";
Chris Lattnere101c442009-02-28 21:26:53 +00001951 TypePrinter.print(I.getType(), Out);
Chris Lattner5b337482003-10-18 05:57:43 +00001952 } else if (isa<VAArgInst>(I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001953 if (Operand) {
1954 Out << ' ';
1955 writeOperand(Operand, true); // Work with broken code
1956 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001957 Out << ", ";
Chris Lattnere101c442009-02-28 21:26:53 +00001958 TypePrinter.print(I.getType(), Out);
1959 } else if (Operand) { // Print the normal way.
Chris Lattner2f7c9632001-06-06 20:29:01 +00001960
Misha Brukmanb1c93172005-04-21 23:48:37 +00001961 // PrintAllTypes - Instructions who have operands of all the same type
Chris Lattner2f7c9632001-06-06 20:29:01 +00001962 // omit the type from all but the first operand. If the instruction has
1963 // different type operands (for example br), then they are all printed.
1964 bool PrintAllTypes = false;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001965 Type *TheType = Operand->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001966
Reid Spencer0cdd04f2007-02-02 13:54:55 +00001967 // Select, Store and ShuffleVector always print all types.
Devang Patelce556d92008-03-04 22:05:14 +00001968 if (isa<SelectInst>(I) || isa<StoreInst>(I) || isa<ShuffleVectorInst>(I)
1969 || isa<ReturnInst>(I)) {
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00001970 PrintAllTypes = true;
1971 } else {
1972 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
1973 Operand = I.getOperand(i);
Nuno Lopes04fe2f02009-01-15 18:40:57 +00001974 // note that Operand shouldn't be null, but the test helps make dump()
1975 // more tolerant of malformed IR
Nuno Lopes0971e772009-01-14 17:51:41 +00001976 if (Operand && Operand->getType() != TheType) {
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00001977 PrintAllTypes = true; // We have differing types! Print them all!
1978 break;
1979 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001980 }
1981 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001982
Chris Lattner7bfee412001-10-29 16:05:51 +00001983 if (!PrintAllTypes) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001984 Out << ' ';
Chris Lattnere101c442009-02-28 21:26:53 +00001985 TypePrinter.print(TheType, Out);
Chris Lattner7bfee412001-10-29 16:05:51 +00001986 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001987
Dan Gohman81313fd2008-09-14 17:21:12 +00001988 Out << ' ';
Chris Lattner113f4f42002-06-25 16:13:24 +00001989 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001990 if (i) Out << ", ";
Chris Lattner113f4f42002-06-25 16:13:24 +00001991 writeOperand(I.getOperand(i), PrintAllTypes);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001992 }
1993 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001994
Eli Friedman59b66882011-08-09 23:02:53 +00001995 // Print atomic ordering/alignment for memory operations
1996 if (const LoadInst *LI = dyn_cast<LoadInst>(&I)) {
1997 if (LI->isAtomic())
1998 writeAtomic(LI->getOrdering(), LI->getSynchScope());
1999 if (LI->getAlignment())
2000 Out << ", align " << LI->getAlignment();
2001 } else if (const StoreInst *SI = dyn_cast<StoreInst>(&I)) {
2002 if (SI->isAtomic())
2003 writeAtomic(SI->getOrdering(), SI->getSynchScope());
2004 if (SI->getAlignment())
2005 Out << ", align " << SI->getAlignment();
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002006 } else if (const AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(&I)) {
2007 writeAtomic(CXI->getOrdering(), CXI->getSynchScope());
2008 } else if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(&I)) {
2009 writeAtomic(RMWI->getOrdering(), RMWI->getSynchScope());
Eli Friedmanfee02c62011-07-25 23:16:38 +00002010 } else if (const FenceInst *FI = dyn_cast<FenceInst>(&I)) {
2011 writeAtomic(FI->getOrdering(), FI->getSynchScope());
Christopher Lamb84485702007-04-22 19:24:39 +00002012 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00002013
Chris Lattnerc9558df2009-12-28 20:10:43 +00002014 // Print Metadata info.
Nick Lewyckyba8ec9a2010-02-25 06:53:04 +00002015 SmallVector<std::pair<unsigned, MDNode*>, 4> InstMD;
2016 I.getAllMetadata(InstMD);
Erick Tryzelaar72a37132010-03-02 05:32:52 +00002017 if (!InstMD.empty()) {
2018 SmallVector<StringRef, 8> MDNames;
2019 I.getType()->getContext().getMDKindNames(MDNames);
2020 for (unsigned i = 0, e = InstMD.size(); i != e; ++i) {
2021 unsigned Kind = InstMD[i].first;
2022 if (Kind < MDNames.size()) {
2023 Out << ", !" << MDNames[Kind];
2024 } else {
2025 Out << ", !<unknown kind #" << Kind << ">";
2026 }
Dan Gohmana2489d12010-07-20 23:55:01 +00002027 Out << ' ';
2028 WriteAsOperandInternal(Out, InstMD[i].second, &TypePrinter, &Machine,
2029 TheModule);
Nick Lewyckyba8ec9a2010-02-25 06:53:04 +00002030 }
Devang Patelbcdb0252009-10-07 16:37:55 +00002031 }
Chris Lattner862e3382001-10-13 06:42:36 +00002032 printInfoComment(I);
Chris Lattner2f7c9632001-06-06 20:29:01 +00002033}
2034
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002035static void WriteMDNodeComment(const MDNode *Node,
Duncan Sands41b4a6b2010-07-12 08:16:59 +00002036 formatted_raw_ostream &Out) {
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002037 if (Node->getNumOperands() < 1)
2038 return;
Bill Wendling5cb50c52012-06-28 00:41:44 +00002039
2040 Value *Op = Node->getOperand(0);
2041 if (!Op || !isa<ConstantInt>(Op) || cast<ConstantInt>(Op)->getBitWidth() < 32)
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002042 return;
Andrew Trickec4b6e72011-09-30 19:48:58 +00002043
Bill Wendling5cb50c52012-06-28 00:41:44 +00002044 DIDescriptor Desc(Node);
2045 if (Desc.getVersion() < LLVMDebugVersion11)
2046 return;
2047
2048 unsigned Tag = Desc.getTag();
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002049 Out.PadToColumn(50);
Bill Wendlinga0bc1082012-07-03 20:01:02 +00002050 if (dwarf::TagString(Tag)) {
2051 Out << "; ";
2052 Desc.print(Out);
2053 } else if (Tag == dwarf::DW_TAG_user_base) {
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002054 Out << "; [ DW_TAG_user_base ]";
Bill Wendlinga0bc1082012-07-03 20:01:02 +00002055 }
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002056}
2057
2058void AssemblyWriter::writeAllMDNodes() {
2059 SmallVector<const MDNode *, 16> Nodes;
Chris Lattnercf4a76e2009-12-31 02:20:11 +00002060 Nodes.resize(Machine.mdn_size());
2061 for (SlotTracker::mdn_iterator I = Machine.mdn_begin(), E = Machine.mdn_end();
2062 I != E; ++I)
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002063 Nodes[I->second] = cast<MDNode>(I->first);
Andrew Trickec4b6e72011-09-30 19:48:58 +00002064
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002065 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
2066 Out << '!' << i << " = metadata ";
Chris Lattner0d50bdd2009-12-31 02:27:30 +00002067 printMDNodeBody(Nodes[i]);
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002068 }
2069}
2070
2071void AssemblyWriter::printMDNodeBody(const MDNode *Node) {
Dan Gohmana2489d12010-07-20 23:55:01 +00002072 WriteMDNodeBodyInternal(Out, Node, &TypePrinter, &Machine, TheModule);
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002073 WriteMDNodeComment(Node, Out);
2074 Out << "\n";
2075}
Chris Lattner2f7c9632001-06-06 20:29:01 +00002076
2077//===----------------------------------------------------------------------===//
2078// External Interface declarations
2079//===----------------------------------------------------------------------===//
2080
Dan Gohmane2745262009-08-12 17:23:50 +00002081void Module::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW) const {
Chris Lattnere36fd8a2008-08-19 04:26:57 +00002082 SlotTracker SlotTable(this);
Dan Gohmane2745262009-08-12 17:23:50 +00002083 formatted_raw_ostream OS(ROS);
Chris Lattner0c19df42008-08-23 22:23:09 +00002084 AssemblyWriter W(OS, SlotTable, this, AAW);
Chris Lattnerefb5e392009-12-31 02:23:35 +00002085 W.printModule(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00002086}
2087
Dan Gohman2637cc12010-07-21 23:38:33 +00002088void NamedMDNode::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW) const {
2089 SlotTracker SlotTable(getParent());
2090 formatted_raw_ostream OS(ROS);
2091 AssemblyWriter W(OS, SlotTable, getParent(), AAW);
2092 W.printNamedMDNode(this);
2093}
2094
Chris Lattner2ee62d22009-02-28 21:11:05 +00002095void Type::print(raw_ostream &OS) const {
2096 if (this == 0) {
2097 OS << "<null Type>";
2098 return;
2099 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002100 TypePrinting TP;
2101 TP.print(const_cast<Type*>(this), OS);
Andrew Trickec4b6e72011-09-30 19:48:58 +00002102
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002103 // If the type is a named struct type, print the body as well.
2104 if (StructType *STy = dyn_cast<StructType>(const_cast<Type*>(this)))
Chris Lattner01beceb2011-08-12 18:07:07 +00002105 if (!STy->isLiteral()) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002106 OS << " = type ";
2107 TP.printStructBody(STy, OS);
2108 }
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00002109}
2110
Dan Gohmane2745262009-08-12 17:23:50 +00002111void Value::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW) const {
Chris Lattner0c19df42008-08-23 22:23:09 +00002112 if (this == 0) {
Dan Gohman12dad632009-08-12 20:56:03 +00002113 ROS << "printing a <null> value\n";
Chris Lattner0c19df42008-08-23 22:23:09 +00002114 return;
2115 }
Dan Gohman12dad632009-08-12 20:56:03 +00002116 formatted_raw_ostream OS(ROS);
Chris Lattner0c19df42008-08-23 22:23:09 +00002117 if (const Instruction *I = dyn_cast<Instruction>(this)) {
2118 const Function *F = I->getParent() ? I->getParent()->getParent() : 0;
2119 SlotTracker SlotTable(F);
Chris Lattnerd5bace72009-12-31 08:23:09 +00002120 AssemblyWriter W(OS, SlotTable, getModuleFromVal(I), AAW);
Chris Lattnerefb5e392009-12-31 02:23:35 +00002121 W.printInstruction(*I);
Chris Lattner0c19df42008-08-23 22:23:09 +00002122 } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(this)) {
2123 SlotTracker SlotTable(BB->getParent());
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002124 AssemblyWriter W(OS, SlotTable, getModuleFromVal(BB), AAW);
Chris Lattnerefb5e392009-12-31 02:23:35 +00002125 W.printBasicBlock(BB);
Chris Lattner0c19df42008-08-23 22:23:09 +00002126 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(this)) {
2127 SlotTracker SlotTable(GV->getParent());
Dan Gohman6c7a4852009-04-20 16:10:33 +00002128 AssemblyWriter W(OS, SlotTable, GV->getParent(), AAW);
Chris Lattnerefb5e392009-12-31 02:23:35 +00002129 if (const GlobalVariable *V = dyn_cast<GlobalVariable>(GV))
2130 W.printGlobal(V);
2131 else if (const Function *F = dyn_cast<Function>(GV))
2132 W.printFunction(F);
2133 else
2134 W.printAlias(cast<GlobalAlias>(GV));
Devang Patel5546b982009-07-01 20:59:15 +00002135 } else if (const MDNode *N = dyn_cast<MDNode>(this)) {
Victor Hernandeze5f2af72010-01-20 04:45:57 +00002136 const Function *F = N->getFunction();
Victor Hernandez61e6e822010-01-14 01:47:37 +00002137 SlotTracker SlotTable(F);
Dan Gohman15132172010-07-14 21:12:44 +00002138 AssemblyWriter W(OS, SlotTable, F ? F->getParent() : 0, AAW);
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002139 W.printMDNodeBody(N);
Chris Lattner0c19df42008-08-23 22:23:09 +00002140 } else if (const Constant *C = dyn_cast<Constant>(this)) {
Chris Lattner92c5c122009-02-28 23:20:19 +00002141 TypePrinting TypePrinter;
Chris Lattnere101c442009-02-28 21:26:53 +00002142 TypePrinter.print(C->getType(), OS);
Chris Lattner2ee62d22009-02-28 21:11:05 +00002143 OS << ' ';
Dan Gohmana2489d12010-07-20 23:55:01 +00002144 WriteConstantInternal(OS, C, TypePrinter, 0, 0);
Chris Lattner31fcc282009-12-31 01:41:14 +00002145 } else if (isa<InlineAsm>(this) || isa<MDString>(this) ||
2146 isa<Argument>(this)) {
Chris Lattner0c19df42008-08-23 22:23:09 +00002147 WriteAsOperand(OS, this, true, 0);
2148 } else {
Dan Gohmanc0353bf2009-09-23 01:33:16 +00002149 // Otherwise we don't know what it is. Call the virtual function to
2150 // allow a subclass to print itself.
2151 printCustom(OS);
Chris Lattner0c19df42008-08-23 22:23:09 +00002152 }
2153}
2154
Dan Gohmanc0353bf2009-09-23 01:33:16 +00002155// Value::printCustom - subclasses should override this to implement printing.
2156void Value::printCustom(raw_ostream &OS) const {
2157 llvm_unreachable("Unknown value to print out!");
2158}
2159
Chris Lattnerdab942552008-08-25 17:03:15 +00002160// Value::dump - allow easy printing of Values from the debugger.
David Greenec7f9b122010-01-05 01:29:26 +00002161void Value::dump() const { print(dbgs()); dbgs() << '\n'; }
Reid Spencer52641832004-05-25 18:14:38 +00002162
Chris Lattnerdab942552008-08-25 17:03:15 +00002163// Type::dump - allow easy printing of Types from the debugger.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002164void Type::dump() const { print(dbgs()); }
Chris Lattner24025262009-02-28 21:05:51 +00002165
Chris Lattnerdab942552008-08-25 17:03:15 +00002166// Module::dump() - Allow printing of Modules from the debugger.
David Greenec7f9b122010-01-05 01:29:26 +00002167void Module::dump() const { print(dbgs(), 0); }
Bill Wendling3681d112011-12-09 23:18:34 +00002168
2169// NamedMDNode::dump() - Allow printing of NamedMDNodes from the debugger.
2170void NamedMDNode::dump() const { print(dbgs(), 0); }