blob: 669d308b1a1c80cffce755365643fd5b9a077987 [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) {
103 char C = Name[i];
Chris Lattner3eee99c2008-08-19 04:36:02 +0000104 if (!isalnum(C) && C != '-' && C != '.' && C != '_') {
105 NeedsQuotes = true;
106 break;
107 }
108 }
109 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000110
Chris Lattner3eee99c2008-08-19 04:36:02 +0000111 // If we didn't need any quotes, just write out the name in one blast.
112 if (!NeedsQuotes) {
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000113 OS << Name;
Chris Lattner3eee99c2008-08-19 04:36:02 +0000114 return;
115 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000116
Chris Lattner3eee99c2008-08-19 04:36:02 +0000117 // Okay, we need quotes. Output the quotes and escape any scary characters as
118 // needed.
119 OS << '"';
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000120 PrintEscapedString(Name, OS);
Chris Lattner3eee99c2008-08-19 04:36:02 +0000121 OS << '"';
122}
123
124/// PrintLLVMName - Turn the specified name into an 'LLVM name', which is either
125/// prefixed with % (if the string only contains simple characters) or is
126/// surrounded with ""'s (if it has special chars in it). Print it out.
Dan Gohman12dad632009-08-12 20:56:03 +0000127static void PrintLLVMName(raw_ostream &OS, const Value *V) {
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000128 PrintLLVMName(OS, V->getName(),
Chris Lattner3eee99c2008-08-19 04:36:02 +0000129 isa<GlobalValue>(V) ? GlobalPrefix : LocalPrefix);
130}
131
Chris Lattner0f578952009-02-28 20:25:14 +0000132//===----------------------------------------------------------------------===//
133// TypePrinting Class: Type printing machinery
134//===----------------------------------------------------------------------===//
135
Chris Lattner3a16c712011-06-18 21:23:04 +0000136/// TypePrinting - Type printing machinery.
137namespace {
138class TypePrinting {
Chris Lattner3a16c712011-06-18 21:23:04 +0000139 TypePrinting(const TypePrinting &); // DO NOT IMPLEMENT
140 void operator=(const TypePrinting&); // DO NOT IMPLEMENT
141public:
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000142
143 /// NamedTypes - The named types that are used by the current module.
144 std::vector<StructType*> NamedTypes;
Andrew Trickec4b6e72011-09-30 19:48:58 +0000145
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000146 /// NumberedTypes - The numbered types, along with their value.
147 DenseMap<StructType*, unsigned> NumberedTypes;
Andrew Trickec4b6e72011-09-30 19:48:58 +0000148
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000149
Chris Lattner3a16c712011-06-18 21:23:04 +0000150 TypePrinting() {}
151 ~TypePrinting() {}
Andrew Trickec4b6e72011-09-30 19:48:58 +0000152
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000153 void incorporateTypes(const Module &M);
Andrew Trickec4b6e72011-09-30 19:48:58 +0000154
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000155 void print(Type *Ty, raw_ostream &OS);
Andrew Trickec4b6e72011-09-30 19:48:58 +0000156
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000157 void printStructBody(StructType *Ty, raw_ostream &OS);
Chris Lattner3a16c712011-06-18 21:23:04 +0000158};
159} // end anonymous namespace.
Chris Lattner3ad46822009-02-28 22:34:45 +0000160
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000161
162void TypePrinting::incorporateTypes(const Module &M) {
163 M.findUsedStructTypes(NamedTypes);
Andrew Trickec4b6e72011-09-30 19:48:58 +0000164
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000165 // The list of struct types we got back includes all the struct types, split
166 // the unnamed ones out to a numbering and remove the anonymous structs.
167 unsigned NextNumber = 0;
Andrew Trickec4b6e72011-09-30 19:48:58 +0000168
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000169 std::vector<StructType*>::iterator NextToUse = NamedTypes.begin(), I, E;
170 for (I = NamedTypes.begin(), E = NamedTypes.end(); I != E; ++I) {
171 StructType *STy = *I;
Andrew Trickec4b6e72011-09-30 19:48:58 +0000172
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000173 // Ignore anonymous types.
Chris Lattner01beceb2011-08-12 18:07:07 +0000174 if (STy->isLiteral())
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000175 continue;
Andrew Trickec4b6e72011-09-30 19:48:58 +0000176
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000177 if (STy->getName().empty())
178 NumberedTypes[STy] = NextNumber++;
179 else
180 *NextToUse++ = STy;
181 }
Andrew Trickec4b6e72011-09-30 19:48:58 +0000182
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000183 NamedTypes.erase(NextToUse, NamedTypes.end());
184}
185
186
Chris Lattner40959d02009-02-28 20:49:40 +0000187/// CalcTypeName - Write the specified type to the specified raw_ostream, making
188/// use of type names or up references to shorten the type name where possible.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000189void TypePrinting::print(Type *Ty, raw_ostream &OS) {
Chris Lattner0f578952009-02-28 20:25:14 +0000190 switch (Ty->getTypeID()) {
Chris Lattner06a23212009-02-28 21:27:31 +0000191 case Type::VoidTyID: OS << "void"; break;
Dan Gohman518cda42011-12-17 00:04:22 +0000192 case Type::HalfTyID: OS << "half"; break;
Chris Lattner06a23212009-02-28 21:27:31 +0000193 case Type::FloatTyID: OS << "float"; break;
194 case Type::DoubleTyID: OS << "double"; break;
195 case Type::X86_FP80TyID: OS << "x86_fp80"; break;
196 case Type::FP128TyID: OS << "fp128"; break;
197 case Type::PPC_FP128TyID: OS << "ppc_fp128"; break;
198 case Type::LabelTyID: OS << "label"; break;
Nick Lewyckyadbc2842009-05-30 05:06:04 +0000199 case Type::MetadataTyID: OS << "metadata"; break;
Dale Johannesenbaa5d042010-09-10 20:55:01 +0000200 case Type::X86_MMXTyID: OS << "x86_mmx"; break;
Chris Lattner68318b12009-02-28 21:18:43 +0000201 case Type::IntegerTyID:
Chris Lattner06a23212009-02-28 21:27:31 +0000202 OS << 'i' << cast<IntegerType>(Ty)->getBitWidth();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000203 return;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000204
Chris Lattner07d88292009-02-28 20:35:42 +0000205 case Type::FunctionTyID: {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000206 FunctionType *FTy = cast<FunctionType>(Ty);
207 print(FTy->getReturnType(), OS);
Chris Lattner06a23212009-02-28 21:27:31 +0000208 OS << " (";
Chris Lattner07d88292009-02-28 20:35:42 +0000209 for (FunctionType::param_iterator I = FTy->param_begin(),
210 E = FTy->param_end(); I != E; ++I) {
211 if (I != FTy->param_begin())
Chris Lattner06a23212009-02-28 21:27:31 +0000212 OS << ", ";
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000213 print(*I, OS);
Chris Lattner0f578952009-02-28 20:25:14 +0000214 }
Chris Lattner07d88292009-02-28 20:35:42 +0000215 if (FTy->isVarArg()) {
Chris Lattner06a23212009-02-28 21:27:31 +0000216 if (FTy->getNumParams()) OS << ", ";
217 OS << "...";
Chris Lattner0f578952009-02-28 20:25:14 +0000218 }
Chris Lattner06a23212009-02-28 21:27:31 +0000219 OS << ')';
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000220 return;
Chris Lattner07d88292009-02-28 20:35:42 +0000221 }
222 case Type::StructTyID: {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000223 StructType *STy = cast<StructType>(Ty);
Andrew Trickec4b6e72011-09-30 19:48:58 +0000224
Chris Lattner01beceb2011-08-12 18:07:07 +0000225 if (STy->isLiteral())
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000226 return printStructBody(STy, OS);
227
228 if (!STy->getName().empty())
229 return PrintLLVMName(OS, STy->getName(), LocalPrefix);
Andrew Trickec4b6e72011-09-30 19:48:58 +0000230
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000231 DenseMap<StructType*, unsigned>::iterator I = NumberedTypes.find(STy);
232 if (I != NumberedTypes.end())
233 OS << '%' << I->second;
234 else // Not enumerated, print the hex address.
Benjamin Kramer4e8b4632011-11-02 17:24:36 +0000235 OS << "%\"type " << STy << '\"';
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000236 return;
Chris Lattner07d88292009-02-28 20:35:42 +0000237 }
238 case Type::PointerTyID: {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000239 PointerType *PTy = cast<PointerType>(Ty);
240 print(PTy->getElementType(), OS);
Chris Lattner07d88292009-02-28 20:35:42 +0000241 if (unsigned AddressSpace = PTy->getAddressSpace())
Chris Lattner06a23212009-02-28 21:27:31 +0000242 OS << " addrspace(" << AddressSpace << ')';
243 OS << '*';
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000244 return;
Chris Lattner07d88292009-02-28 20:35:42 +0000245 }
246 case Type::ArrayTyID: {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000247 ArrayType *ATy = cast<ArrayType>(Ty);
Chris Lattner06a23212009-02-28 21:27:31 +0000248 OS << '[' << ATy->getNumElements() << " x ";
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000249 print(ATy->getElementType(), OS);
Chris Lattner06a23212009-02-28 21:27:31 +0000250 OS << ']';
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000251 return;
Chris Lattner07d88292009-02-28 20:35:42 +0000252 }
253 case Type::VectorTyID: {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000254 VectorType *PTy = cast<VectorType>(Ty);
Chris Lattner06a23212009-02-28 21:27:31 +0000255 OS << "<" << PTy->getNumElements() << " x ";
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000256 print(PTy->getElementType(), OS);
Chris Lattner06a23212009-02-28 21:27:31 +0000257 OS << '>';
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000258 return;
Chris Lattner07d88292009-02-28 20:35:42 +0000259 }
Chris Lattner07d88292009-02-28 20:35:42 +0000260 default:
Chris Lattner06a23212009-02-28 21:27:31 +0000261 OS << "<unrecognized-type>";
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000262 return;
Chris Lattner0f578952009-02-28 20:25:14 +0000263 }
Chris Lattner0f578952009-02-28 20:25:14 +0000264}
265
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000266void TypePrinting::printStructBody(StructType *STy, raw_ostream &OS) {
267 if (STy->isOpaque()) {
268 OS << "opaque";
269 return;
Chris Lattner0f578952009-02-28 20:25:14 +0000270 }
Andrew Trickec4b6e72011-09-30 19:48:58 +0000271
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000272 if (STy->isPacked())
273 OS << '<';
Andrew Trickec4b6e72011-09-30 19:48:58 +0000274
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000275 if (STy->getNumElements() == 0) {
276 OS << "{}";
277 } else {
278 StructType::element_iterator I = STy->element_begin();
279 OS << "{ ";
280 print(*I++, OS);
281 for (StructType::element_iterator E = STy->element_end(); I != E; ++I) {
282 OS << ", ";
283 print(*I, OS);
Chris Lattner3243ea12009-03-01 00:03:38 +0000284 }
Andrew Trickec4b6e72011-09-30 19:48:58 +0000285
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000286 OS << " }";
Chris Lattner92c5c122009-02-28 23:20:19 +0000287 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000288 if (STy->isPacked())
289 OS << '>';
Chris Lattner92c5c122009-02-28 23:20:19 +0000290}
291
Chris Lattner0f578952009-02-28 20:25:14 +0000292
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000293
Chris Lattner3eee99c2008-08-19 04:36:02 +0000294//===----------------------------------------------------------------------===//
295// SlotTracker Class: Enumerate slot numbers for unnamed values
296//===----------------------------------------------------------------------===//
297
Chris Lattner3ee58762008-08-19 04:28:07 +0000298namespace {
299
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000300/// This class provides computation of slot numbers for LLVM Assembly writing.
Chris Lattner393b7cd2008-08-17 04:17:45 +0000301///
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000302class SlotTracker {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000303public:
Devang Patel983c6b12009-07-08 21:44:25 +0000304 /// ValueMap - A mapping of Values to slot numbers.
Chris Lattnera204d412008-08-17 17:25:25 +0000305 typedef DenseMap<const Value*, unsigned> ValueMap;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000306
307private:
Devang Patel983c6b12009-07-08 21:44:25 +0000308 /// TheModule - The module for which we are holding slot numbers.
Chris Lattner393b7cd2008-08-17 04:17:45 +0000309 const Module* TheModule;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000310
Devang Patel983c6b12009-07-08 21:44:25 +0000311 /// TheFunction - The function for which we are holding slot numbers.
Chris Lattner393b7cd2008-08-17 04:17:45 +0000312 const Function* TheFunction;
313 bool FunctionProcessed;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000314
Jay Foaddbb221d2011-07-11 07:28:49 +0000315 /// mMap - The slot map for the module level data.
Chris Lattner393b7cd2008-08-17 04:17:45 +0000316 ValueMap mMap;
317 unsigned mNext;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000318
Jay Foaddbb221d2011-07-11 07:28:49 +0000319 /// fMap - The slot map for the function level data.
Chris Lattner393b7cd2008-08-17 04:17:45 +0000320 ValueMap fMap;
321 unsigned fNext;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000322
Devang Patel983c6b12009-07-08 21:44:25 +0000323 /// mdnMap - Map for MDNodes.
Chris Lattnercf4a76e2009-12-31 02:20:11 +0000324 DenseMap<const MDNode*, unsigned> mdnMap;
Devang Patel983c6b12009-07-08 21:44:25 +0000325 unsigned mdnNext;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000326public:
Chris Lattner393b7cd2008-08-17 04:17:45 +0000327 /// Construct from a module
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000328 explicit SlotTracker(const Module *M);
Chris Lattner393b7cd2008-08-17 04:17:45 +0000329 /// Construct from a function, starting out in incorp state.
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000330 explicit SlotTracker(const Function *F);
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000331
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000332 /// Return the slot number of the specified value in it's type
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000333 /// plane. If something is not in the SlotTracker, return -1.
Chris Lattner5e043322007-01-11 03:54:27 +0000334 int getLocalSlot(const Value *V);
335 int getGlobalSlot(const GlobalValue *V);
Devang Patel983c6b12009-07-08 21:44:25 +0000336 int getMetadataSlot(const MDNode *N);
Reid Spencer8beac692004-06-09 15:26:53 +0000337
Misha Brukmanb1c93172005-04-21 23:48:37 +0000338 /// If you'd like to deal with a function instead of just a module, use
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000339 /// this method to get its data into the SlotTracker.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000340 void incorporateFunction(const Function *F) {
341 TheFunction = F;
Reid Spencerb0ac8c42004-08-16 07:46:33 +0000342 FunctionProcessed = false;
343 }
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000344
Misha Brukmanb1c93172005-04-21 23:48:37 +0000345 /// After calling incorporateFunction, use this method to remove the
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000346 /// most recently incorporated function from the SlotTracker. This
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000347 /// will reset the state of the machine back to just the module contents.
348 void purgeFunction();
349
Devang Patel983c6b12009-07-08 21:44:25 +0000350 /// MDNode map iterators.
Chris Lattnercf4a76e2009-12-31 02:20:11 +0000351 typedef DenseMap<const MDNode*, unsigned>::iterator mdn_iterator;
352 mdn_iterator mdn_begin() { return mdnMap.begin(); }
353 mdn_iterator mdn_end() { return mdnMap.end(); }
354 unsigned mdn_size() const { return mdnMap.size(); }
355 bool mdn_empty() const { return mdnMap.empty(); }
Devang Patel983c6b12009-07-08 21:44:25 +0000356
Reid Spencer56010e42004-05-26 21:56:09 +0000357 /// This function does the actual initialization.
358 inline void initialize();
359
Devang Patel983c6b12009-07-08 21:44:25 +0000360 // Implementation Details
361private:
Chris Lattnerea862a32007-01-09 07:55:49 +0000362 /// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
363 void CreateModuleSlot(const GlobalValue *V);
Devang Patel983c6b12009-07-08 21:44:25 +0000364
365 /// CreateMetadataSlot - Insert the specified MDNode* into the slot table.
366 void CreateMetadataSlot(const MDNode *N);
367
Chris Lattnerea862a32007-01-09 07:55:49 +0000368 /// CreateFunctionSlot - Insert the specified Value* into the slot table.
369 void CreateFunctionSlot(const Value *V);
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000370
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000371 /// Add all of the module level global variables (and their initializers)
372 /// and function declarations, but not the contents of those functions.
373 void processModule();
374
Devang Patel983c6b12009-07-08 21:44:25 +0000375 /// Add all of the functions arguments, basic blocks, and instructions.
Reid Spencer56010e42004-05-26 21:56:09 +0000376 void processFunction();
377
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000378 SlotTracker(const SlotTracker &); // DO NOT IMPLEMENT
379 void operator=(const SlotTracker &); // DO NOT IMPLEMENT
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000380};
381
Chris Lattner3ee58762008-08-19 04:28:07 +0000382} // end anonymous namespace
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000383
Chris Lattner3eee99c2008-08-19 04:36:02 +0000384
385static SlotTracker *createSlotTracker(const Value *V) {
386 if (const Argument *FA = dyn_cast<Argument>(V))
387 return new SlotTracker(FA->getParent());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000388
Chris Lattner3eee99c2008-08-19 04:36:02 +0000389 if (const Instruction *I = dyn_cast<Instruction>(V))
Andrew Trick2f0cbf62011-09-30 19:50:40 +0000390 if (I->getParent())
391 return new SlotTracker(I->getParent()->getParent());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000392
Chris Lattner3eee99c2008-08-19 04:36:02 +0000393 if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
394 return new SlotTracker(BB->getParent());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000395
Chris Lattner3eee99c2008-08-19 04:36:02 +0000396 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
397 return new SlotTracker(GV->getParent());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000398
Chris Lattner3eee99c2008-08-19 04:36:02 +0000399 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V))
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000400 return new SlotTracker(GA->getParent());
401
Chris Lattner3eee99c2008-08-19 04:36:02 +0000402 if (const Function *Func = dyn_cast<Function>(V))
403 return new SlotTracker(Func);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000404
Dan Gohmana2489d12010-07-20 23:55:01 +0000405 if (const MDNode *MD = dyn_cast<MDNode>(V)) {
406 if (!MD->isFunctionLocal())
407 return new SlotTracker(MD->getFunction());
408
Dale Johannesen7b1a7ed2010-01-13 00:00:24 +0000409 return new SlotTracker((Function *)0);
Dan Gohmana2489d12010-07-20 23:55:01 +0000410 }
Dale Johannesen7b1a7ed2010-01-13 00:00:24 +0000411
Chris Lattner3eee99c2008-08-19 04:36:02 +0000412 return 0;
413}
414
415#if 0
David Greenec7f9b122010-01-05 01:29:26 +0000416#define ST_DEBUG(X) dbgs() << X
Chris Lattner3eee99c2008-08-19 04:36:02 +0000417#else
Chris Lattner604e3512008-08-19 04:47:09 +0000418#define ST_DEBUG(X)
Chris Lattner3eee99c2008-08-19 04:36:02 +0000419#endif
420
421// Module level constructor. Causes the contents of the Module (sans functions)
422// to be added to the slot table.
423SlotTracker::SlotTracker(const Module *M)
Andrew Trickec4b6e72011-09-30 19:48:58 +0000424 : TheModule(M), TheFunction(0), FunctionProcessed(false),
Chris Lattnerbe2de792009-12-31 01:36:50 +0000425 mNext(0), fNext(0), mdnNext(0) {
Chris Lattner3eee99c2008-08-19 04:36:02 +0000426}
427
428// Function level constructor. Causes the contents of the Module and the one
429// function provided to be added to the slot table.
430SlotTracker::SlotTracker(const Function *F)
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000431 : TheModule(F ? F->getParent() : 0), TheFunction(F), FunctionProcessed(false),
Chris Lattnerbddea6a2009-12-31 02:13:35 +0000432 mNext(0), fNext(0), mdnNext(0) {
Chris Lattner3eee99c2008-08-19 04:36:02 +0000433}
434
435inline void SlotTracker::initialize() {
436 if (TheModule) {
437 processModule();
438 TheModule = 0; ///< Prevent re-processing next time we're called.
439 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000440
Chris Lattner3eee99c2008-08-19 04:36:02 +0000441 if (TheFunction && !FunctionProcessed)
442 processFunction();
443}
444
445// Iterate through all the global variables, functions, and global
446// variable initializers and create slots for them.
447void SlotTracker::processModule() {
Chris Lattner604e3512008-08-19 04:47:09 +0000448 ST_DEBUG("begin processModule!\n");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000449
Chris Lattner3eee99c2008-08-19 04:36:02 +0000450 // Add all of the unnamed global variables to the value table.
451 for (Module::const_global_iterator I = TheModule->global_begin(),
Devang Patel983c6b12009-07-08 21:44:25 +0000452 E = TheModule->global_end(); I != E; ++I) {
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000453 if (!I->hasName())
Chris Lattner3eee99c2008-08-19 04:36:02 +0000454 CreateModuleSlot(I);
Devang Patel983c6b12009-07-08 21:44:25 +0000455 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000456
Devang Patel23e68302009-07-29 22:04:47 +0000457 // Add metadata used by named metadata.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000458 for (Module::const_named_metadata_iterator
Devang Patel23e68302009-07-29 22:04:47 +0000459 I = TheModule->named_metadata_begin(),
460 E = TheModule->named_metadata_end(); I != E; ++I) {
461 const NamedMDNode *NMD = I;
Dan Gohman093cb792010-07-21 18:54:18 +0000462 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
463 CreateMetadataSlot(NMD->getOperand(i));
Devang Patel23e68302009-07-29 22:04:47 +0000464 }
465
Chris Lattner3eee99c2008-08-19 04:36:02 +0000466 // Add all the unnamed functions to the table.
467 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
468 I != E; ++I)
469 if (!I->hasName())
470 CreateModuleSlot(I);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000471
Chris Lattner604e3512008-08-19 04:47:09 +0000472 ST_DEBUG("end processModule!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000473}
474
Chris Lattner3eee99c2008-08-19 04:36:02 +0000475// Process the arguments, basic blocks, and instructions of a function.
476void SlotTracker::processFunction() {
Chris Lattner604e3512008-08-19 04:47:09 +0000477 ST_DEBUG("begin processFunction!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000478 fNext = 0;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000479
Chris Lattner3eee99c2008-08-19 04:36:02 +0000480 // Add all the function arguments with no names.
481 for(Function::const_arg_iterator AI = TheFunction->arg_begin(),
482 AE = TheFunction->arg_end(); AI != AE; ++AI)
483 if (!AI->hasName())
484 CreateFunctionSlot(AI);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000485
Chris Lattner604e3512008-08-19 04:47:09 +0000486 ST_DEBUG("Inserting Instructions:\n");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000487
Chris Lattnerbddea6a2009-12-31 02:13:35 +0000488 SmallVector<std::pair<unsigned, MDNode*>, 4> MDForInst;
Devang Pateldec23fd2009-09-16 20:21:17 +0000489
Chris Lattner3eee99c2008-08-19 04:36:02 +0000490 // Add all of the basic blocks and instructions with no names.
491 for (Function::const_iterator BB = TheFunction->begin(),
492 E = TheFunction->end(); BB != E; ++BB) {
493 if (!BB->hasName())
494 CreateFunctionSlot(BB);
Andrew Trickec4b6e72011-09-30 19:48:58 +0000495
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000496 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E;
Devang Patel983c6b12009-07-08 21:44:25 +0000497 ++I) {
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000498 if (!I->getType()->isVoidTy() && !I->hasName())
Chris Lattner3eee99c2008-08-19 04:36:02 +0000499 CreateFunctionSlot(I);
Andrew Trickec4b6e72011-09-30 19:48:58 +0000500
Chris Lattner58aff8f2010-05-10 20:53:17 +0000501 // Intrinsics can directly use metadata. We allow direct calls to any
502 // llvm.foo function here, because the target may not be linked into the
503 // optimizer.
504 if (const CallInst *CI = dyn_cast<CallInst>(I)) {
505 if (Function *F = CI->getCalledFunction())
506 if (F->getName().startswith("llvm."))
507 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
508 if (MDNode *N = dyn_cast_or_null<MDNode>(I->getOperand(i)))
509 CreateMetadataSlot(N);
510 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000511
Devang Pateldec23fd2009-09-16 20:21:17 +0000512 // Process metadata attached with this instruction.
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000513 I->getAllMetadata(MDForInst);
514 for (unsigned i = 0, e = MDForInst.size(); i != e; ++i)
515 CreateMetadataSlot(MDForInst[i].second);
Chris Lattnerbddea6a2009-12-31 02:13:35 +0000516 MDForInst.clear();
Devang Patel983c6b12009-07-08 21:44:25 +0000517 }
Chris Lattner3eee99c2008-08-19 04:36:02 +0000518 }
Devang Pateldec23fd2009-09-16 20:21:17 +0000519
Chris Lattner3eee99c2008-08-19 04:36:02 +0000520 FunctionProcessed = true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000521
Chris Lattner604e3512008-08-19 04:47:09 +0000522 ST_DEBUG("end processFunction!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000523}
524
525/// Clean up after incorporating a function. This is the only way to get out of
526/// the function incorporation state that affects get*Slot/Create*Slot. Function
527/// incorporation state is indicated by TheFunction != 0.
528void SlotTracker::purgeFunction() {
Chris Lattner604e3512008-08-19 04:47:09 +0000529 ST_DEBUG("begin purgeFunction!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000530 fMap.clear(); // Simply discard the function level map
531 TheFunction = 0;
532 FunctionProcessed = false;
Chris Lattner604e3512008-08-19 04:47:09 +0000533 ST_DEBUG("end purgeFunction!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000534}
535
536/// getGlobalSlot - Get the slot number of a global value.
537int SlotTracker::getGlobalSlot(const GlobalValue *V) {
538 // Check for uninitialized state and do lazy initialization.
539 initialize();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000540
Jay Foaddbb221d2011-07-11 07:28:49 +0000541 // Find the value in the module map
Chris Lattner3eee99c2008-08-19 04:36:02 +0000542 ValueMap::iterator MI = mMap.find(V);
Dan Gohman1dd27572008-10-01 19:58:59 +0000543 return MI == mMap.end() ? -1 : (int)MI->second;
Chris Lattner3eee99c2008-08-19 04:36:02 +0000544}
545
Chris Lattnercf4a76e2009-12-31 02:20:11 +0000546/// getMetadataSlot - Get the slot number of a MDNode.
Devang Patel983c6b12009-07-08 21:44:25 +0000547int SlotTracker::getMetadataSlot(const MDNode *N) {
548 // Check for uninitialized state and do lazy initialization.
549 initialize();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000550
Jay Foaddbb221d2011-07-11 07:28:49 +0000551 // Find the MDNode in the module map
Chris Lattnercf4a76e2009-12-31 02:20:11 +0000552 mdn_iterator MI = mdnMap.find(N);
Devang Patel983c6b12009-07-08 21:44:25 +0000553 return MI == mdnMap.end() ? -1 : (int)MI->second;
554}
555
Chris Lattner3eee99c2008-08-19 04:36:02 +0000556
557/// getLocalSlot - Get the slot number for a value that is local to a function.
558int SlotTracker::getLocalSlot(const Value *V) {
559 assert(!isa<Constant>(V) && "Can't get a constant or global slot with this!");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000560
Chris Lattner3eee99c2008-08-19 04:36:02 +0000561 // Check for uninitialized state and do lazy initialization.
562 initialize();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000563
Chris Lattner3eee99c2008-08-19 04:36:02 +0000564 ValueMap::iterator FI = fMap.find(V);
Dan Gohman1dd27572008-10-01 19:58:59 +0000565 return FI == fMap.end() ? -1 : (int)FI->second;
Chris Lattner3eee99c2008-08-19 04:36:02 +0000566}
567
568
569/// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
570void SlotTracker::CreateModuleSlot(const GlobalValue *V) {
571 assert(V && "Can't insert a null Value into SlotTracker!");
Chris Lattner031560c2009-12-29 07:25:48 +0000572 assert(!V->getType()->isVoidTy() && "Doesn't need a slot!");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000573 assert(!V->hasName() && "Doesn't need a slot!");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000574
Chris Lattner3eee99c2008-08-19 04:36:02 +0000575 unsigned DestSlot = mNext++;
576 mMap[V] = DestSlot;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000577
Chris Lattner604e3512008-08-19 04:47:09 +0000578 ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" <<
Chris Lattner3eee99c2008-08-19 04:36:02 +0000579 DestSlot << " [");
580 // G = Global, F = Function, A = Alias, o = other
Chris Lattner604e3512008-08-19 04:47:09 +0000581 ST_DEBUG((isa<GlobalVariable>(V) ? 'G' :
Chris Lattner3eee99c2008-08-19 04:36:02 +0000582 (isa<Function>(V) ? 'F' :
583 (isa<GlobalAlias>(V) ? 'A' : 'o'))) << "]\n");
584}
585
Chris Lattner3eee99c2008-08-19 04:36:02 +0000586/// CreateSlot - Create a new slot for the specified value if it has no name.
587void SlotTracker::CreateFunctionSlot(const Value *V) {
Chris Lattner031560c2009-12-29 07:25:48 +0000588 assert(!V->getType()->isVoidTy() && !V->hasName() && "Doesn't need a slot!");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000589
Chris Lattner3eee99c2008-08-19 04:36:02 +0000590 unsigned DestSlot = fNext++;
591 fMap[V] = DestSlot;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000592
Chris Lattner3eee99c2008-08-19 04:36:02 +0000593 // G = Global, F = Function, o = other
Chris Lattner604e3512008-08-19 04:47:09 +0000594 ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" <<
Chris Lattner3eee99c2008-08-19 04:36:02 +0000595 DestSlot << " [o]\n");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000596}
Chris Lattner3eee99c2008-08-19 04:36:02 +0000597
Devang Patel983c6b12009-07-08 21:44:25 +0000598/// CreateModuleSlot - Insert the specified MDNode* into the slot table.
599void SlotTracker::CreateMetadataSlot(const MDNode *N) {
600 assert(N && "Can't insert a null Value into SlotTracker!");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000601
Chris Lattner0d50bdd2009-12-31 02:27:30 +0000602 // Don't insert if N is a function-local metadata, these are always printed
603 // inline.
Dan Gohmana2489d12010-07-20 23:55:01 +0000604 if (!N->isFunctionLocal()) {
605 mdn_iterator I = mdnMap.find(N);
606 if (I != mdnMap.end())
607 return;
Victor Hernandez4d633542009-12-04 20:07:10 +0000608
Dan Gohmana2489d12010-07-20 23:55:01 +0000609 unsigned DestSlot = mdnNext++;
610 mdnMap[N] = DestSlot;
611 }
Devang Patel983c6b12009-07-08 21:44:25 +0000612
Chris Lattner0d50bdd2009-12-31 02:27:30 +0000613 // Recursively add any MDNodes referenced by operands.
614 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
615 if (const MDNode *Op = dyn_cast_or_null<MDNode>(N->getOperand(i)))
616 CreateMetadataSlot(Op);
Devang Patel983c6b12009-07-08 21:44:25 +0000617}
Chris Lattner3eee99c2008-08-19 04:36:02 +0000618
619//===----------------------------------------------------------------------===//
620// AsmWriter Implementation
621//===----------------------------------------------------------------------===//
Chris Lattner7f8845a2002-07-23 18:07:49 +0000622
Dan Gohman12dad632009-08-12 20:56:03 +0000623static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
Dan Gohman8061d9e2009-08-13 15:27:57 +0000624 TypePrinting *TypePrinter,
Dan Gohmana2489d12010-07-20 23:55:01 +0000625 SlotTracker *Machine,
626 const Module *Context);
Reid Spencer58d30f22004-07-04 11:50:43 +0000627
Chris Lattner033935d2008-08-17 04:40:13 +0000628
Chris Lattnerb86620e2001-10-29 16:37:48 +0000629
Chris Lattnerfc9f1c92006-12-06 06:40:49 +0000630static const char *getPredicateText(unsigned predicate) {
Reid Spencer812a1be2006-12-04 05:19:18 +0000631 const char * pred = "unknown";
632 switch (predicate) {
Chris Lattnerbddea6a2009-12-31 02:13:35 +0000633 case FCmpInst::FCMP_FALSE: pred = "false"; break;
634 case FCmpInst::FCMP_OEQ: pred = "oeq"; break;
635 case FCmpInst::FCMP_OGT: pred = "ogt"; break;
636 case FCmpInst::FCMP_OGE: pred = "oge"; break;
637 case FCmpInst::FCMP_OLT: pred = "olt"; break;
638 case FCmpInst::FCMP_OLE: pred = "ole"; break;
639 case FCmpInst::FCMP_ONE: pred = "one"; break;
640 case FCmpInst::FCMP_ORD: pred = "ord"; break;
641 case FCmpInst::FCMP_UNO: pred = "uno"; break;
642 case FCmpInst::FCMP_UEQ: pred = "ueq"; break;
643 case FCmpInst::FCMP_UGT: pred = "ugt"; break;
644 case FCmpInst::FCMP_UGE: pred = "uge"; break;
645 case FCmpInst::FCMP_ULT: pred = "ult"; break;
646 case FCmpInst::FCMP_ULE: pred = "ule"; break;
647 case FCmpInst::FCMP_UNE: pred = "une"; break;
648 case FCmpInst::FCMP_TRUE: pred = "true"; break;
649 case ICmpInst::ICMP_EQ: pred = "eq"; break;
650 case ICmpInst::ICMP_NE: pred = "ne"; break;
651 case ICmpInst::ICMP_SGT: pred = "sgt"; break;
652 case ICmpInst::ICMP_SGE: pred = "sge"; break;
653 case ICmpInst::ICMP_SLT: pred = "slt"; break;
654 case ICmpInst::ICMP_SLE: pred = "sle"; break;
655 case ICmpInst::ICMP_UGT: pred = "ugt"; break;
656 case ICmpInst::ICMP_UGE: pred = "uge"; break;
657 case ICmpInst::ICMP_ULT: pred = "ult"; break;
658 case ICmpInst::ICMP_ULE: pred = "ule"; break;
Reid Spencer812a1be2006-12-04 05:19:18 +0000659 }
660 return pred;
661}
662
Eli Friedmanc9a551e2011-07-28 21:48:00 +0000663static void writeAtomicRMWOperation(raw_ostream &Out,
664 AtomicRMWInst::BinOp Op) {
665 switch (Op) {
666 default: Out << " <unknown operation " << Op << ">"; break;
667 case AtomicRMWInst::Xchg: Out << " xchg"; break;
668 case AtomicRMWInst::Add: Out << " add"; break;
669 case AtomicRMWInst::Sub: Out << " sub"; break;
670 case AtomicRMWInst::And: Out << " and"; break;
671 case AtomicRMWInst::Nand: Out << " nand"; break;
672 case AtomicRMWInst::Or: Out << " or"; break;
673 case AtomicRMWInst::Xor: Out << " xor"; break;
674 case AtomicRMWInst::Max: Out << " max"; break;
675 case AtomicRMWInst::Min: Out << " min"; break;
676 case AtomicRMWInst::UMax: Out << " umax"; break;
677 case AtomicRMWInst::UMin: Out << " umin"; break;
678 }
679}
Devang Patel983c6b12009-07-08 21:44:25 +0000680
Dan Gohman12dad632009-08-12 20:56:03 +0000681static void WriteOptimizationInfo(raw_ostream &Out, const User *U) {
Dan Gohman0ebd6962009-07-20 21:19:07 +0000682 if (const OverflowingBinaryOperator *OBO =
683 dyn_cast<OverflowingBinaryOperator>(U)) {
Dan Gohman16f54152009-08-20 17:11:38 +0000684 if (OBO->hasNoUnsignedWrap())
Dan Gohman9c7f8082009-07-27 16:11:46 +0000685 Out << " nuw";
Dan Gohman16f54152009-08-20 17:11:38 +0000686 if (OBO->hasNoSignedWrap())
Dan Gohman9c7f8082009-07-27 16:11:46 +0000687 Out << " nsw";
Chris Lattner35315d02011-02-06 21:44:57 +0000688 } else if (const PossiblyExactOperator *Div =
689 dyn_cast<PossiblyExactOperator>(U)) {
Dan Gohman0ebd6962009-07-20 21:19:07 +0000690 if (Div->isExact())
Dan Gohman9c7f8082009-07-27 16:11:46 +0000691 Out << " exact";
Dan Gohman1639c392009-07-27 21:53:46 +0000692 } else if (const GEPOperator *GEP = dyn_cast<GEPOperator>(U)) {
693 if (GEP->isInBounds())
694 Out << " inbounds";
Dan Gohman0ebd6962009-07-20 21:19:07 +0000695 }
696}
697
Dan Gohmanefb8dbb2010-07-14 20:57:55 +0000698static void WriteConstantInternal(raw_ostream &Out, const Constant *CV,
699 TypePrinting &TypePrinter,
Dan Gohmana2489d12010-07-20 23:55:01 +0000700 SlotTracker *Machine,
701 const Module *Context) {
Zhou Sheng75b871f2007-01-11 12:24:14 +0000702 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Duncan Sands9dff9be2010-02-15 16:12:20 +0000703 if (CI->getType()->isIntegerTy(1)) {
Reid Spencercddc9df2007-01-12 04:24:46 +0000704 Out << (CI->getZExtValue() ? "true" : "false");
Chris Lattner17f71652008-08-17 07:19:36 +0000705 return;
706 }
707 Out << CI->getValue();
708 return;
709 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000710
Chris Lattner17f71652008-08-17 07:19:36 +0000711 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Tobias Grosser6b31d172012-05-24 15:59:06 +0000712 if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEsingle ||
Dan Gohman518cda42011-12-17 00:04:22 +0000713 &CFP->getValueAPF().getSemantics() == &APFloat::IEEEdouble) {
Dale Johannesen028084e2007-09-12 03:30:33 +0000714 // We would like to output the FP constant value in exponential notation,
715 // but we cannot do this if doing so will lose precision. Check here to
716 // make sure that we only output it in exponential format if we can parse
717 // the value back and get the same value.
718 //
Dale Johannesen1f864982009-01-21 20:32:55 +0000719 bool ignored;
Dan Gohman518cda42011-12-17 00:04:22 +0000720 bool isHalf = &CFP->getValueAPF().getSemantics()==&APFloat::IEEEhalf;
Dale Johannesen028084e2007-09-12 03:30:33 +0000721 bool isDouble = &CFP->getValueAPF().getSemantics()==&APFloat::IEEEdouble;
NAKAMURA Takumi35d19c02012-02-16 08:12:24 +0000722 bool isInf = CFP->getValueAPF().isInfinity();
723 bool isNaN = CFP->getValueAPF().isNaN();
724 if (!isHalf && !isInf && !isNaN) {
Dan Gohman518cda42011-12-17 00:04:22 +0000725 double Val = isDouble ? CFP->getValueAPF().convertToDouble() :
726 CFP->getValueAPF().convertToFloat();
727 SmallString<128> StrVal;
728 raw_svector_ostream(StrVal) << Val;
Chris Lattner1e194682002-04-18 18:53:13 +0000729
Dan Gohman518cda42011-12-17 00:04:22 +0000730 // Check to make sure that the stringized number is not some string like
731 // "Inf" or NaN, that atof will accept, but the lexer will not. Check
732 // that the string matches the "[-+]?[0-9]" regex.
733 //
734 if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
735 ((StrVal[0] == '-' || StrVal[0] == '+') &&
736 (StrVal[1] >= '0' && StrVal[1] <= '9'))) {
737 // Reparse stringized version!
NAKAMURA Takumiaec41232012-02-16 04:19:15 +0000738 if (APFloat(APFloat::IEEEdouble, StrVal).convertToDouble() == Val) {
Dan Gohman518cda42011-12-17 00:04:22 +0000739 Out << StrVal.str();
740 return;
741 }
Dale Johannesen028084e2007-09-12 03:30:33 +0000742 }
Chris Lattner1e194682002-04-18 18:53:13 +0000743 }
Dale Johannesen028084e2007-09-12 03:30:33 +0000744 // Otherwise we could not reparse it to exactly the same value, so we must
Dale Johannesen1f864982009-01-21 20:32:55 +0000745 // output the string in hexadecimal format! Note that loading and storing
746 // floating point types changes the bits of NaNs on some hosts, notably
747 // x86, so we must not use these types.
Dale Johannesen028084e2007-09-12 03:30:33 +0000748 assert(sizeof(double) == sizeof(uint64_t) &&
749 "assuming that double is 64 bits!");
Chris Lattner5505eed2008-11-10 04:30:26 +0000750 char Buffer[40];
Dale Johannesen1f864982009-01-21 20:32:55 +0000751 APFloat apf = CFP->getValueAPF();
Dan Gohman518cda42011-12-17 00:04:22 +0000752 // Halves and floats are represented in ASCII IR as double, convert.
Dale Johannesen1f864982009-01-21 20:32:55 +0000753 if (!isDouble)
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000754 apf.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven,
Dale Johannesen1f864982009-01-21 20:32:55 +0000755 &ignored);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000756 Out << "0x" <<
757 utohex_buffer(uint64_t(apf.bitcastToAPInt().getZExtValue()),
Dale Johannesen1f864982009-01-21 20:32:55 +0000758 Buffer+40);
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000759 return;
760 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000761
Tobias Grosser6b31d172012-05-24 15:59:06 +0000762 // Either half, or some form of long double.
763 // These appear as a magic letter identifying the type, then a
764 // fixed number of hex digits.
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000765 Out << "0x";
Tobias Grosser6b31d172012-05-24 15:59:06 +0000766 // Bit position, in the current word, of the next nibble to print.
767 int shiftcount;
768
Dale Johannesen93eefa02009-03-23 21:16:53 +0000769 if (&CFP->getValueAPF().getSemantics() == &APFloat::x87DoubleExtended) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000770 Out << 'K';
Dale Johannesen93eefa02009-03-23 21:16:53 +0000771 // api needed to prevent premature destruction
772 APInt api = CFP->getValueAPF().bitcastToAPInt();
773 const uint64_t* p = api.getRawData();
774 uint64_t word = p[1];
Tobias Grosser6b31d172012-05-24 15:59:06 +0000775 shiftcount = 12;
Dale Johannesen93eefa02009-03-23 21:16:53 +0000776 int width = api.getBitWidth();
777 for (int j=0; j<width; j+=4, shiftcount-=4) {
778 unsigned int nibble = (word>>shiftcount) & 15;
779 if (nibble < 10)
780 Out << (unsigned char)(nibble + '0');
781 else
782 Out << (unsigned char)(nibble - 10 + 'A');
783 if (shiftcount == 0 && j+4 < width) {
784 word = *p;
785 shiftcount = 64;
786 if (width-j-4 < 64)
787 shiftcount = width-j-4;
788 }
789 }
790 return;
Tobias Grosser6b31d172012-05-24 15:59:06 +0000791 } else if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEquad) {
792 shiftcount = 60;
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000793 Out << 'L';
Tobias Grosser6b31d172012-05-24 15:59:06 +0000794 } else if (&CFP->getValueAPF().getSemantics() == &APFloat::PPCDoubleDouble) {
795 shiftcount = 60;
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000796 Out << 'M';
Tobias Grosser6b31d172012-05-24 15:59:06 +0000797 } else if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEhalf) {
798 shiftcount = 12;
799 Out << 'H';
800 } else
Torok Edwinfbcc6632009-07-14 16:55:14 +0000801 llvm_unreachable("Unsupported floating point type");
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000802 // api needed to prevent premature destruction
Dale Johannesen54306fe2008-10-09 18:53:47 +0000803 APInt api = CFP->getValueAPF().bitcastToAPInt();
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000804 const uint64_t* p = api.getRawData();
805 uint64_t word = *p;
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000806 int width = api.getBitWidth();
807 for (int j=0; j<width; j+=4, shiftcount-=4) {
808 unsigned int nibble = (word>>shiftcount) & 15;
809 if (nibble < 10)
810 Out << (unsigned char)(nibble + '0');
Dale Johannesen028084e2007-09-12 03:30:33 +0000811 else
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000812 Out << (unsigned char)(nibble - 10 + 'A');
813 if (shiftcount == 0 && j+4 < width) {
814 word = *(++p);
815 shiftcount = 64;
816 if (width-j-4 < 64)
817 shiftcount = width-j-4;
Dale Johannesen028084e2007-09-12 03:30:33 +0000818 }
819 }
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000820 return;
821 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000822
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000823 if (isa<ConstantAggregateZero>(CV)) {
Chris Lattner76b2ff42004-02-15 05:55:15 +0000824 Out << "zeroinitializer";
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000825 return;
826 }
Andrew Trickec4b6e72011-09-30 19:48:58 +0000827
Chris Lattner214cc702009-10-28 03:38:12 +0000828 if (const BlockAddress *BA = dyn_cast<BlockAddress>(CV)) {
829 Out << "blockaddress(";
Dan Gohmana2489d12010-07-20 23:55:01 +0000830 WriteAsOperandInternal(Out, BA->getFunction(), &TypePrinter, Machine,
831 Context);
Chris Lattner214cc702009-10-28 03:38:12 +0000832 Out << ", ";
Dan Gohmana2489d12010-07-20 23:55:01 +0000833 WriteAsOperandInternal(Out, BA->getBasicBlock(), &TypePrinter, Machine,
834 Context);
Chris Lattner214cc702009-10-28 03:38:12 +0000835 Out << ")";
836 return;
837 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000838
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000839 if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000840 Type *ETy = CA->getType()->getElementType();
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000841 Out << '[';
842 TypePrinter.print(ETy, Out);
843 Out << ' ';
844 WriteAsOperandInternal(Out, CA->getOperand(0),
845 &TypePrinter, Machine,
846 Context);
847 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
848 Out << ", ";
Chris Lattner9c181f62012-01-31 03:15:40 +0000849 TypePrinter.print(ETy, Out);
850 Out << ' ';
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000851 WriteAsOperandInternal(Out, CA->getOperand(i), &TypePrinter, Machine,
Chris Lattner9c181f62012-01-31 03:15:40 +0000852 Context);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000853 }
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000854 Out << ']';
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000855 return;
856 }
Chris Lattnerfa775002012-01-26 02:32:04 +0000857
858 if (const ConstantDataArray *CA = dyn_cast<ConstantDataArray>(CV)) {
859 // As a special case, print the array as a string if it is an array of
860 // i8 with ConstantInt values.
861 if (CA->isString()) {
862 Out << "c\"";
863 PrintEscapedString(CA->getAsString(), Out);
864 Out << '"';
865 return;
866 }
867
868 Type *ETy = CA->getType()->getElementType();
869 Out << '[';
Chris Lattner9c181f62012-01-31 03:15:40 +0000870 TypePrinter.print(ETy, Out);
871 Out << ' ';
872 WriteAsOperandInternal(Out, CA->getElementAsConstant(0),
873 &TypePrinter, Machine,
874 Context);
875 for (unsigned i = 1, e = CA->getNumElements(); i != e; ++i) {
876 Out << ", ";
Chris Lattnerfa775002012-01-26 02:32:04 +0000877 TypePrinter.print(ETy, Out);
878 Out << ' ';
Chris Lattner9c181f62012-01-31 03:15:40 +0000879 WriteAsOperandInternal(Out, CA->getElementAsConstant(i), &TypePrinter,
880 Machine, Context);
Chris Lattnerfa775002012-01-26 02:32:04 +0000881 }
Chris Lattner9c181f62012-01-31 03:15:40 +0000882 Out << ']';
Chris Lattnerfa775002012-01-26 02:32:04 +0000883 return;
884 }
885
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000886
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000887 if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
Andrew Lenharth0d124b82007-01-08 18:21:30 +0000888 if (CS->getType()->isPacked())
889 Out << '<';
Misha Brukman21bbdb92004-06-04 21:11:51 +0000890 Out << '{';
Jim Laskey3bb78742006-02-25 12:27:03 +0000891 unsigned N = CS->getNumOperands();
892 if (N) {
Chris Lattner604e3512008-08-19 04:47:09 +0000893 Out << ' ';
Chris Lattnere101c442009-02-28 21:26:53 +0000894 TypePrinter.print(CS->getOperand(0)->getType(), Out);
Dan Gohman81313fd2008-09-14 17:21:12 +0000895 Out << ' ';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000896
Dan Gohmana2489d12010-07-20 23:55:01 +0000897 WriteAsOperandInternal(Out, CS->getOperand(0), &TypePrinter, Machine,
898 Context);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000899
Jim Laskey3bb78742006-02-25 12:27:03 +0000900 for (unsigned i = 1; i < N; i++) {
Chris Lattnerd84bb632002-04-16 21:36:08 +0000901 Out << ", ";
Chris Lattnere101c442009-02-28 21:26:53 +0000902 TypePrinter.print(CS->getOperand(i)->getType(), Out);
Dan Gohman81313fd2008-09-14 17:21:12 +0000903 Out << ' ';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000904
Dan Gohmana2489d12010-07-20 23:55:01 +0000905 WriteAsOperandInternal(Out, CS->getOperand(i), &TypePrinter, Machine,
906 Context);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000907 }
Dan Gohman81313fd2008-09-14 17:21:12 +0000908 Out << ' ';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000909 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000910
Dan Gohman81313fd2008-09-14 17:21:12 +0000911 Out << '}';
Andrew Lenharth0d124b82007-01-08 18:21:30 +0000912 if (CS->getType()->isPacked())
913 Out << '>';
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000914 return;
915 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000916
Chris Lattnerfa775002012-01-26 02:32:04 +0000917 if (isa<ConstantVector>(CV) || isa<ConstantDataVector>(CV)) {
918 Type *ETy = CV->getType()->getVectorElementType();
Dan Gohman1262a252009-02-11 00:25:25 +0000919 Out << '<';
Chris Lattnere101c442009-02-28 21:26:53 +0000920 TypePrinter.print(ETy, Out);
Dan Gohman81313fd2008-09-14 17:21:12 +0000921 Out << ' ';
Chris Lattnerfa775002012-01-26 02:32:04 +0000922 WriteAsOperandInternal(Out, CV->getAggregateElement(0U), &TypePrinter,
923 Machine, Context);
924 for (unsigned i = 1, e = CV->getType()->getVectorNumElements(); i != e;++i){
Chris Lattner585297e82008-08-19 05:26:17 +0000925 Out << ", ";
Chris Lattnere101c442009-02-28 21:26:53 +0000926 TypePrinter.print(ETy, Out);
Dan Gohman81313fd2008-09-14 17:21:12 +0000927 Out << ' ';
Chris Lattnerfa775002012-01-26 02:32:04 +0000928 WriteAsOperandInternal(Out, CV->getAggregateElement(i), &TypePrinter,
929 Machine, Context);
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000930 }
Dan Gohman1262a252009-02-11 00:25:25 +0000931 Out << '>';
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000932 return;
933 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000934
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000935 if (isa<ConstantPointerNull>(CV)) {
Chris Lattnerd84bb632002-04-16 21:36:08 +0000936 Out << "null";
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000937 return;
938 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000939
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000940 if (isa<UndefValue>(CV)) {
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000941 Out << "undef";
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000942 return;
943 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000944
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000945 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Reid Spencer812a1be2006-12-04 05:19:18 +0000946 Out << CE->getOpcodeName();
Dan Gohman9c7f8082009-07-27 16:11:46 +0000947 WriteOptimizationInfo(Out, CE);
Reid Spencer812a1be2006-12-04 05:19:18 +0000948 if (CE->isCompare())
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000949 Out << ' ' << getPredicateText(CE->getPredicate());
Reid Spencer812a1be2006-12-04 05:19:18 +0000950 Out << " (";
Misha Brukmanb1c93172005-04-21 23:48:37 +0000951
Vikram S. Adveb952b542002-07-14 23:14:45 +0000952 for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) {
Chris Lattnere101c442009-02-28 21:26:53 +0000953 TypePrinter.print((*OI)->getType(), Out);
Dan Gohman81313fd2008-09-14 17:21:12 +0000954 Out << ' ';
Dan Gohmana2489d12010-07-20 23:55:01 +0000955 WriteAsOperandInternal(Out, *OI, &TypePrinter, Machine, Context);
Vikram S. Adveb952b542002-07-14 23:14:45 +0000956 if (OI+1 != CE->op_end())
Chris Lattner3cd8c562002-07-30 18:54:25 +0000957 Out << ", ";
Vikram S. Adveb952b542002-07-14 23:14:45 +0000958 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000959
Dan Gohmana76f0f72008-05-31 19:12:39 +0000960 if (CE->hasIndices()) {
Jay Foad0091fe82011-04-13 15:22:40 +0000961 ArrayRef<unsigned> Indices = CE->getIndices();
Dan Gohmana76f0f72008-05-31 19:12:39 +0000962 for (unsigned i = 0, e = Indices.size(); i != e; ++i)
963 Out << ", " << Indices[i];
964 }
965
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000966 if (CE->isCast()) {
Chris Lattner83b396b2002-08-15 19:37:43 +0000967 Out << " to ";
Chris Lattnere101c442009-02-28 21:26:53 +0000968 TypePrinter.print(CE->getType(), Out);
Chris Lattner83b396b2002-08-15 19:37:43 +0000969 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000970
Misha Brukman21bbdb92004-06-04 21:11:51 +0000971 Out << ')';
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000972 return;
Chris Lattnerd84bb632002-04-16 21:36:08 +0000973 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000974
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000975 Out << "<placeholder or erroneous Constant>";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000976}
977
Chris Lattnercdec5812009-12-31 02:31:59 +0000978static void WriteMDNodeBodyInternal(raw_ostream &Out, const MDNode *Node,
979 TypePrinting *TypePrinter,
Dan Gohmana2489d12010-07-20 23:55:01 +0000980 SlotTracker *Machine,
981 const Module *Context) {
Chris Lattnercdec5812009-12-31 02:31:59 +0000982 Out << "!{";
983 for (unsigned mi = 0, me = Node->getNumOperands(); mi != me; ++mi) {
984 const Value *V = Node->getOperand(mi);
985 if (V == 0)
986 Out << "null";
987 else {
988 TypePrinter->print(V->getType(), Out);
989 Out << ' ';
Andrew Trickec4b6e72011-09-30 19:48:58 +0000990 WriteAsOperandInternal(Out, Node->getOperand(mi),
Dan Gohmana2489d12010-07-20 23:55:01 +0000991 TypePrinter, Machine, Context);
Chris Lattnercdec5812009-12-31 02:31:59 +0000992 }
993 if (mi + 1 != me)
994 Out << ", ";
995 }
Andrew Trickec4b6e72011-09-30 19:48:58 +0000996
Chris Lattnercdec5812009-12-31 02:31:59 +0000997 Out << "}";
998}
999
Chris Lattnerd84bb632002-04-16 21:36:08 +00001000
Misha Brukmanc566ca362004-03-02 00:22:19 +00001001/// WriteAsOperand - Write the name of the specified value out to the specified
1002/// ostream. This can be useful when you just want to print int %reg126, not
1003/// the whole instruction that generated it.
1004///
Dan Gohman12dad632009-08-12 20:56:03 +00001005static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
Dan Gohman8061d9e2009-08-13 15:27:57 +00001006 TypePrinting *TypePrinter,
Dan Gohmana2489d12010-07-20 23:55:01 +00001007 SlotTracker *Machine,
1008 const Module *Context) {
Chris Lattner033935d2008-08-17 04:40:13 +00001009 if (V->hasName()) {
1010 PrintLLVMName(Out, V);
1011 return;
1012 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001013
Chris Lattner033935d2008-08-17 04:40:13 +00001014 const Constant *CV = dyn_cast<Constant>(V);
1015 if (CV && !isa<GlobalValue>(CV)) {
Dan Gohman8061d9e2009-08-13 15:27:57 +00001016 assert(TypePrinter && "Constants require TypePrinting!");
Dan Gohmana2489d12010-07-20 23:55:01 +00001017 WriteConstantInternal(Out, CV, *TypePrinter, Machine, Context);
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001018 return;
1019 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001020
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001021 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
Chris Lattner033935d2008-08-17 04:40:13 +00001022 Out << "asm ";
1023 if (IA->hasSideEffects())
1024 Out << "sideeffect ";
Dale Johannesen1cfb9582009-10-21 23:28:00 +00001025 if (IA->isAlignStack())
1026 Out << "alignstack ";
Chris Lattner033935d2008-08-17 04:40:13 +00001027 Out << '"';
1028 PrintEscapedString(IA->getAsmString(), Out);
1029 Out << "\", \"";
1030 PrintEscapedString(IA->getConstraintString(), Out);
1031 Out << '"';
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001032 return;
1033 }
Devang Patel7428d8a2009-07-22 17:43:22 +00001034
Devang Patele059ba6e2009-07-23 01:07:34 +00001035 if (const MDNode *N = dyn_cast<MDNode>(V)) {
Victor Hernandez0471abd2009-12-18 20:09:14 +00001036 if (N->isFunctionLocal()) {
Victor Hernandezb7176a12009-12-04 01:35:02 +00001037 // Print metadata inline, not via slot reference number.
Dan Gohmana2489d12010-07-20 23:55:01 +00001038 WriteMDNodeBodyInternal(Out, N, TypePrinter, Machine, Context);
Victor Hernandezb7176a12009-12-04 01:35:02 +00001039 return;
1040 }
Andrew Trickec4b6e72011-09-30 19:48:58 +00001041
Dan Gohmana2489d12010-07-20 23:55:01 +00001042 if (!Machine) {
1043 if (N->isFunctionLocal())
1044 Machine = new SlotTracker(N->getFunction());
1045 else
1046 Machine = new SlotTracker(Context);
1047 }
Dan Gohman0a54da22010-09-09 20:53:58 +00001048 int Slot = Machine->getMetadataSlot(N);
1049 if (Slot == -1)
1050 Out << "<badref>";
1051 else
1052 Out << '!' << Slot;
Devang Patele059ba6e2009-07-23 01:07:34 +00001053 return;
1054 }
1055
Devang Patel7428d8a2009-07-22 17:43:22 +00001056 if (const MDString *MDS = dyn_cast<MDString>(V)) {
Devang Patel7428d8a2009-07-22 17:43:22 +00001057 Out << "!\"";
Daniel Dunbare03eecb2009-07-25 23:55:21 +00001058 PrintEscapedString(MDS->getString(), Out);
Devang Patel7428d8a2009-07-22 17:43:22 +00001059 Out << '"';
1060 return;
1061 }
1062
Evan Cheng00e87d12009-11-16 07:10:36 +00001063 if (V->getValueID() == Value::PseudoSourceValueVal ||
1064 V->getValueID() == Value::FixedStackPseudoSourceValueVal) {
Dan Gohmanc0353bf2009-09-23 01:33:16 +00001065 V->print(Out);
1066 return;
1067 }
1068
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001069 char Prefix = '%';
1070 int Slot;
Chris Lattner5b82a0a2011-08-03 06:15:41 +00001071 // If we have a SlotTracker, use it.
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001072 if (Machine) {
1073 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1074 Slot = Machine->getGlobalSlot(GV);
1075 Prefix = '@';
1076 } else {
1077 Slot = Machine->getLocalSlot(V);
Andrew Trickec4b6e72011-09-30 19:48:58 +00001078
Chris Lattner5b82a0a2011-08-03 06:15:41 +00001079 // If the local value didn't succeed, then we may be referring to a value
1080 // from a different function. Translate it, as this can happen when using
1081 // address of blocks.
1082 if (Slot == -1)
1083 if ((Machine = createSlotTracker(V))) {
1084 Slot = Machine->getLocalSlot(V);
1085 delete Machine;
1086 }
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001087 }
Chris Lattner5b82a0a2011-08-03 06:15:41 +00001088 } else if ((Machine = createSlotTracker(V))) {
1089 // Otherwise, create one to get the # and then destroy it.
1090 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1091 Slot = Machine->getGlobalSlot(GV);
1092 Prefix = '@';
Chris Lattnera2d810d2006-01-25 22:26:05 +00001093 } else {
Chris Lattner5b82a0a2011-08-03 06:15:41 +00001094 Slot = Machine->getLocalSlot(V);
Chris Lattnerd84bb632002-04-16 21:36:08 +00001095 }
Chris Lattner5b82a0a2011-08-03 06:15:41 +00001096 delete Machine;
1097 Machine = 0;
1098 } else {
1099 Slot = -1;
Chris Lattnerd84bb632002-04-16 21:36:08 +00001100 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001101
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001102 if (Slot != -1)
1103 Out << Prefix << Slot;
1104 else
1105 Out << "<badref>";
Chris Lattnerd84bb632002-04-16 21:36:08 +00001106}
1107
Dan Gohman12dad632009-08-12 20:56:03 +00001108void llvm::WriteAsOperand(raw_ostream &Out, const Value *V,
1109 bool PrintType, const Module *Context) {
Dan Gohman8061d9e2009-08-13 15:27:57 +00001110
1111 // Fast path: Don't construct and populate a TypePrinting object if we
1112 // won't be needing any types printed.
Dan Gohman08097122009-08-13 23:07:11 +00001113 if (!PrintType &&
Dan Gohmana2489d12010-07-20 23:55:01 +00001114 ((!isa<Constant>(V) && !isa<MDNode>(V)) ||
1115 V->hasName() || isa<GlobalValue>(V))) {
1116 WriteAsOperandInternal(Out, V, 0, 0, Context);
Dan Gohman8061d9e2009-08-13 15:27:57 +00001117 return;
1118 }
1119
Chris Lattner5a9f63e2002-07-10 16:48:17 +00001120 if (Context == 0) Context = getModuleFromVal(V);
Chris Lattnerb86620e2001-10-29 16:37:48 +00001121
Chris Lattner92c5c122009-02-28 23:20:19 +00001122 TypePrinting TypePrinter;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001123 if (Context)
1124 TypePrinter.incorporateTypes(*Context);
Dan Gohman81313fd2008-09-14 17:21:12 +00001125 if (PrintType) {
Chris Lattnere101c442009-02-28 21:26:53 +00001126 TypePrinter.print(V->getType(), Out);
Dan Gohman81313fd2008-09-14 17:21:12 +00001127 Out << ' ';
1128 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001129
Dan Gohmana2489d12010-07-20 23:55:01 +00001130 WriteAsOperandInternal(Out, V, &TypePrinter, 0, Context);
Chris Lattner5e5abe32001-07-20 19:15:21 +00001131}
1132
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001133namespace {
Chris Lattner2e9fee42001-07-12 23:35:26 +00001134
Chris Lattnerfee714f2001-09-07 16:36:04 +00001135class AssemblyWriter {
Dan Gohmane2745262009-08-12 17:23:50 +00001136 formatted_raw_ostream &Out;
Chris Lattnere36fd8a2008-08-19 04:26:57 +00001137 SlotTracker &Machine;
Chris Lattner7bfee412001-10-29 16:05:51 +00001138 const Module *TheModule;
Chris Lattner0f578952009-02-28 20:25:14 +00001139 TypePrinting TypePrinter;
Chris Lattner8339f7d2003-10-30 23:41:03 +00001140 AssemblyAnnotationWriter *AnnotationWriter;
Andrew Trickec4b6e72011-09-30 19:48:58 +00001141
Chris Lattner2f7c9632001-06-06 20:29:01 +00001142public:
Dan Gohmane2745262009-08-12 17:23:50 +00001143 inline AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac,
1144 const Module *M,
Chris Lattner8339f7d2003-10-30 23:41:03 +00001145 AssemblyAnnotationWriter *AAW)
Devang Patel59c0c132009-09-28 18:31:56 +00001146 : Out(o), Machine(Mac), TheModule(M), AnnotationWriter(AAW) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001147 if (M)
1148 TypePrinter.incorporateTypes(*M);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001149 }
1150
Chris Lattnerbddea6a2009-12-31 02:13:35 +00001151 void printMDNodeBody(const MDNode *MD);
Chris Lattner2c48c642009-12-31 01:54:05 +00001152 void printNamedMDNode(const NamedMDNode *NMD);
Andrew Trickec4b6e72011-09-30 19:48:58 +00001153
Chris Lattnerefb5e392009-12-31 02:23:35 +00001154 void printModule(const Module *M);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001155
Chris Lattner78e2e8b2006-12-06 06:24:27 +00001156 void writeOperand(const Value *Op, bool PrintType);
Devang Patelba3fa6c2008-09-23 23:03:40 +00001157 void writeParamOperand(const Value *Operand, Attributes Attrs);
Eli Friedmanfee02c62011-07-25 23:16:38 +00001158 void writeAtomic(AtomicOrdering Ordering, SynchronizationScope SynchScope);
Chris Lattner1e194682002-04-18 18:53:13 +00001159
Chris Lattnerbddea6a2009-12-31 02:13:35 +00001160 void writeAllMDNodes();
1161
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001162 void printTypeIdentities();
Chris Lattner7bfee412001-10-29 16:05:51 +00001163 void printGlobal(const GlobalVariable *GV);
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001164 void printAlias(const GlobalAlias *GV);
Chris Lattner57698e22002-03-26 18:01:55 +00001165 void printFunction(const Function *F);
Devang Patelba3fa6c2008-09-23 23:03:40 +00001166 void printArgument(const Argument *FA, Attributes Attrs);
Chris Lattner7bfee412001-10-29 16:05:51 +00001167 void printBasicBlock(const BasicBlock *BB);
Chris Lattner113f4f42002-06-25 16:13:24 +00001168 void printInstruction(const Instruction &I);
Chris Lattnerd816b532002-04-13 20:53:41 +00001169
Dan Gohmanbd11c412010-02-10 20:42:57 +00001170private:
Chris Lattner862e3382001-10-13 06:42:36 +00001171 // printInfoComment - Print a little comment after the instruction indicating
1172 // which slot it occupies.
Chris Lattner113f4f42002-06-25 16:13:24 +00001173 void printInfoComment(const Value &V);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001174};
Chris Lattner3243ea12009-03-01 00:03:38 +00001175} // end of anonymous namespace
Chris Lattner2f7c9632001-06-06 20:29:01 +00001176
Chris Lattner78e2e8b2006-12-06 06:24:27 +00001177void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType) {
1178 if (Operand == 0) {
Chris Lattner08f7d0c2005-02-24 16:58:29 +00001179 Out << "<null operand!>";
Chris Lattnerb419a1e2009-12-31 02:33:14 +00001180 return;
Chris Lattner08f7d0c2005-02-24 16:58:29 +00001181 }
Chris Lattnerb419a1e2009-12-31 02:33:14 +00001182 if (PrintType) {
1183 TypePrinter.print(Operand->getType(), Out);
1184 Out << ' ';
1185 }
Dan Gohmana2489d12010-07-20 23:55:01 +00001186 WriteAsOperandInternal(Out, Operand, &TypePrinter, &Machine, TheModule);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001187}
1188
Eli Friedmanfee02c62011-07-25 23:16:38 +00001189void AssemblyWriter::writeAtomic(AtomicOrdering Ordering,
1190 SynchronizationScope SynchScope) {
1191 if (Ordering == NotAtomic)
1192 return;
1193
1194 switch (SynchScope) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001195 case SingleThread: Out << " singlethread"; break;
1196 case CrossThread: break;
1197 }
1198
1199 switch (Ordering) {
1200 default: Out << " <bad ordering " << int(Ordering) << ">"; break;
1201 case Unordered: Out << " unordered"; break;
1202 case Monotonic: Out << " monotonic"; break;
1203 case Acquire: Out << " acquire"; break;
1204 case Release: Out << " release"; break;
1205 case AcquireRelease: Out << " acq_rel"; break;
1206 case SequentiallyConsistent: Out << " seq_cst"; break;
1207 }
1208}
1209
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001210void AssemblyWriter::writeParamOperand(const Value *Operand,
Devang Patelba3fa6c2008-09-23 23:03:40 +00001211 Attributes Attrs) {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00001212 if (Operand == 0) {
1213 Out << "<null operand!>";
Chris Lattnerb419a1e2009-12-31 02:33:14 +00001214 return;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00001215 }
Chris Lattnerb419a1e2009-12-31 02:33:14 +00001216
1217 // Print the type
1218 TypePrinter.print(Operand->getType(), Out);
1219 // Print parameter attributes list
1220 if (Attrs != Attribute::None)
1221 Out << ' ' << Attribute::getAsString(Attrs);
1222 Out << ' ';
1223 // Print the operand
Dan Gohmana2489d12010-07-20 23:55:01 +00001224 WriteAsOperandInternal(Out, Operand, &TypePrinter, &Machine, TheModule);
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00001225}
Chris Lattner2f7c9632001-06-06 20:29:01 +00001226
Chris Lattner7bfee412001-10-29 16:05:51 +00001227void AssemblyWriter::printModule(const Module *M) {
Chris Lattner4d8689e2005-03-02 23:12:40 +00001228 if (!M->getModuleIdentifier().empty() &&
Misha Brukmanb1c93172005-04-21 23:48:37 +00001229 // Don't print the ID if it will start a new line (which would
Chris Lattner4d8689e2005-03-02 23:12:40 +00001230 // require a comment char before it).
1231 M->getModuleIdentifier().find('\n') == std::string::npos)
1232 Out << "; ModuleID = '" << M->getModuleIdentifier() << "'\n";
1233
Owen Andersone2237542006-10-18 02:21:12 +00001234 if (!M->getDataLayout().empty())
Chris Lattner04897162006-10-22 06:06:56 +00001235 Out << "target datalayout = \"" << M->getDataLayout() << "\"\n";
Reid Spencer48f98c82004-07-25 21:44:54 +00001236 if (!M->getTargetTriple().empty())
Reid Spencerffec7df2004-07-25 21:29:43 +00001237 Out << "target triple = \"" << M->getTargetTriple() << "\"\n";
Misha Brukmanb1c93172005-04-21 23:48:37 +00001238
Chris Lattnereef2fe72006-01-24 04:13:11 +00001239 if (!M->getModuleInlineAsm().empty()) {
Chris Lattnerefaf35d2006-01-24 00:45:30 +00001240 // Split the string into lines, to make it easier to read the .ll file.
Chris Lattnereef2fe72006-01-24 04:13:11 +00001241 std::string Asm = M->getModuleInlineAsm();
Chris Lattnerefaf35d2006-01-24 00:45:30 +00001242 size_t CurPos = 0;
1243 size_t NewLine = Asm.find_first_of('\n', CurPos);
Dan Gohman69273e62009-08-12 23:54:22 +00001244 Out << '\n';
Chris Lattnerefaf35d2006-01-24 00:45:30 +00001245 while (NewLine != std::string::npos) {
1246 // We found a newline, print the portion of the asm string from the
1247 // last newline up to this newline.
1248 Out << "module asm \"";
1249 PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.begin()+NewLine),
1250 Out);
1251 Out << "\"\n";
1252 CurPos = NewLine+1;
1253 NewLine = Asm.find_first_of('\n', CurPos);
1254 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +00001255 std::string rest(Asm.begin()+CurPos, Asm.end());
1256 if (!rest.empty()) {
1257 Out << "module asm \"";
1258 PrintEscapedString(rest, Out);
1259 Out << "\"\n";
1260 }
Chris Lattner6ed87bd2006-01-23 23:03:36 +00001261 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001262
Chris Lattner2cdd49d2004-09-14 05:06:58 +00001263 // Loop over the dependent libraries and emit them.
Chris Lattner730cfe42004-09-14 04:51:44 +00001264 Module::lib_iterator LI = M->lib_begin();
1265 Module::lib_iterator LE = M->lib_end();
Reid Spencer48f98c82004-07-25 21:44:54 +00001266 if (LI != LE) {
Dan Gohman69273e62009-08-12 23:54:22 +00001267 Out << '\n';
Chris Lattner730cfe42004-09-14 04:51:44 +00001268 Out << "deplibs = [ ";
1269 while (LI != LE) {
Chris Lattner2cdd49d2004-09-14 05:06:58 +00001270 Out << '"' << *LI << '"';
Reid Spencerffec7df2004-07-25 21:29:43 +00001271 ++LI;
Chris Lattner730cfe42004-09-14 04:51:44 +00001272 if (LI != LE)
1273 Out << ", ";
Reid Spencerffec7df2004-07-25 21:29:43 +00001274 }
Dan Gohman69273e62009-08-12 23:54:22 +00001275 Out << " ]";
Reid Spencercc5ff642004-07-25 18:08:18 +00001276 }
Reid Spencerb9e08772004-09-13 23:44:23 +00001277
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001278 printTypeIdentities();
Misha Brukmanb1c93172005-04-21 23:48:37 +00001279
Dan Gohman69273e62009-08-12 23:54:22 +00001280 // Output all globals.
1281 if (!M->global_empty()) Out << '\n';
Chris Lattner54932b02006-12-06 04:41:52 +00001282 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
1283 I != E; ++I)
Chris Lattner113f4f42002-06-25 16:13:24 +00001284 printGlobal(I);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001285
Chris Lattnerd2747052007-04-26 02:24:10 +00001286 // Output all aliases.
1287 if (!M->alias_empty()) Out << "\n";
1288 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
1289 I != E; ++I)
1290 printAlias(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +00001291
Chris Lattner2cdd49d2004-09-14 05:06:58 +00001292 // Output all of the functions.
Chris Lattner113f4f42002-06-25 16:13:24 +00001293 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
1294 printFunction(I);
Devang Patel983c6b12009-07-08 21:44:25 +00001295
Devang Patel23e68302009-07-29 22:04:47 +00001296 // Output named metadata.
Dan Gohman69273e62009-08-12 23:54:22 +00001297 if (!M->named_metadata_empty()) Out << '\n';
Andrew Trickec4b6e72011-09-30 19:48:58 +00001298
Devang Patel23e68302009-07-29 22:04:47 +00001299 for (Module::const_named_metadata_iterator I = M->named_metadata_begin(),
Chris Lattnerbddea6a2009-12-31 02:13:35 +00001300 E = M->named_metadata_end(); I != E; ++I)
Chris Lattner2c48c642009-12-31 01:54:05 +00001301 printNamedMDNode(I);
Devang Patel23e68302009-07-29 22:04:47 +00001302
1303 // Output metadata.
Chris Lattnercf4a76e2009-12-31 02:20:11 +00001304 if (!Machine.mdn_empty()) {
Chris Lattnerbddea6a2009-12-31 02:13:35 +00001305 Out << '\n';
1306 writeAllMDNodes();
1307 }
Chris Lattnerfee714f2001-09-07 16:36:04 +00001308}
1309
Chris Lattner2c48c642009-12-31 01:54:05 +00001310void AssemblyWriter::printNamedMDNode(const NamedMDNode *NMD) {
Nick Lewycky5bcbd732011-06-15 06:37:58 +00001311 Out << '!';
1312 StringRef Name = NMD->getName();
1313 if (Name.empty()) {
1314 Out << "<empty name> ";
1315 } else {
1316 if (isalpha(Name[0]) || Name[0] == '-' || Name[0] == '$' ||
1317 Name[0] == '.' || Name[0] == '_')
1318 Out << Name[0];
1319 else
1320 Out << '\\' << hexdigit(Name[0] >> 4) << hexdigit(Name[0] & 0x0F);
1321 for (unsigned i = 1, e = Name.size(); i != e; ++i) {
1322 unsigned char C = Name[i];
1323 if (isalnum(C) || C == '-' || C == '$' || C == '.' || C == '_')
1324 Out << C;
1325 else
1326 Out << '\\' << hexdigit(C >> 4) << hexdigit(C & 0x0F);
1327 }
1328 }
1329 Out << " = !{";
Chris Lattner2c48c642009-12-31 01:54:05 +00001330 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1331 if (i) Out << ", ";
Dan Gohman0a54da22010-09-09 20:53:58 +00001332 int Slot = Machine.getMetadataSlot(NMD->getOperand(i));
1333 if (Slot == -1)
1334 Out << "<badref>";
1335 else
1336 Out << '!' << Slot;
Chris Lattner2c48c642009-12-31 01:54:05 +00001337 }
1338 Out << "}\n";
1339}
1340
1341
Dan Gohmane2745262009-08-12 17:23:50 +00001342static void PrintLinkage(GlobalValue::LinkageTypes LT,
1343 formatted_raw_ostream &Out) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001344 switch (LT) {
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001345 case GlobalValue::ExternalLinkage: break;
1346 case GlobalValue::PrivateLinkage: Out << "private "; break;
1347 case GlobalValue::LinkerPrivateLinkage: Out << "linker_private "; break;
Bill Wendling03bcd6e2010-07-01 21:55:59 +00001348 case GlobalValue::LinkerPrivateWeakLinkage:
1349 Out << "linker_private_weak ";
1350 break;
Bill Wendling578ee402010-08-20 22:05:50 +00001351 case GlobalValue::LinkerPrivateWeakDefAutoLinkage:
1352 Out << "linker_private_weak_def_auto ";
1353 break;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001354 case GlobalValue::InternalLinkage: Out << "internal "; break;
1355 case GlobalValue::LinkOnceAnyLinkage: Out << "linkonce "; break;
1356 case GlobalValue::LinkOnceODRLinkage: Out << "linkonce_odr "; break;
1357 case GlobalValue::WeakAnyLinkage: Out << "weak "; break;
1358 case GlobalValue::WeakODRLinkage: Out << "weak_odr "; break;
1359 case GlobalValue::CommonLinkage: Out << "common "; break;
1360 case GlobalValue::AppendingLinkage: Out << "appending "; break;
1361 case GlobalValue::DLLImportLinkage: Out << "dllimport "; break;
1362 case GlobalValue::DLLExportLinkage: Out << "dllexport "; break;
1363 case GlobalValue::ExternalWeakLinkage: Out << "extern_weak "; break;
Chris Lattner184f1be2009-04-13 05:44:34 +00001364 case GlobalValue::AvailableExternallyLinkage:
1365 Out << "available_externally ";
1366 break;
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001367 }
1368}
Duncan Sands12da8ce2009-03-07 15:45:40 +00001369
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001370
1371static void PrintVisibility(GlobalValue::VisibilityTypes Vis,
Dan Gohmane2745262009-08-12 17:23:50 +00001372 formatted_raw_ostream &Out) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001373 switch (Vis) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001374 case GlobalValue::DefaultVisibility: break;
1375 case GlobalValue::HiddenVisibility: Out << "hidden "; break;
1376 case GlobalValue::ProtectedVisibility: Out << "protected "; break;
1377 }
1378}
1379
Hans Wennborgcbe34b42012-06-23 11:37:03 +00001380static void PrintThreadLocalModel(GlobalVariable::ThreadLocalMode TLM,
1381 formatted_raw_ostream &Out) {
1382 switch (TLM) {
1383 case GlobalVariable::NotThreadLocal:
1384 break;
1385 case GlobalVariable::GeneralDynamicTLSModel:
1386 Out << "thread_local ";
1387 break;
1388 case GlobalVariable::LocalDynamicTLSModel:
1389 Out << "thread_local(localdynamic) ";
1390 break;
1391 case GlobalVariable::InitialExecTLSModel:
1392 Out << "thread_local(initialexec) ";
1393 break;
1394 case GlobalVariable::LocalExecTLSModel:
1395 Out << "thread_local(localexec) ";
1396 break;
1397 }
1398}
1399
Chris Lattner7bfee412001-10-29 16:05:51 +00001400void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Dan Gohman5ded1422010-01-29 23:12:36 +00001401 if (GV->isMaterializable())
1402 Out << "; Materializable\n";
1403
Dan Gohmana2489d12010-07-20 23:55:01 +00001404 WriteAsOperandInternal(Out, GV, &TypePrinter, &Machine, GV->getParent());
Dan Gohman466876b2009-08-12 23:32:33 +00001405 Out << " = ";
Chris Lattner37798642001-09-18 04:01:05 +00001406
Chris Lattner1508d3f2008-08-19 05:16:28 +00001407 if (!GV->hasInitializer() && GV->hasExternalLinkage())
1408 Out << "external ";
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001409
Chris Lattner1508d3f2008-08-19 05:16:28 +00001410 PrintLinkage(GV->getLinkage(), Out);
1411 PrintVisibility(GV->getVisibility(), Out);
Hans Wennborgcbe34b42012-06-23 11:37:03 +00001412 PrintThreadLocalModel(GV->getThreadLocalMode(), Out);
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +00001413
Chris Lattnerac161bf2009-01-02 07:01:27 +00001414 if (unsigned AddressSpace = GV->getType()->getAddressSpace())
1415 Out << "addrspace(" << AddressSpace << ") ";
Rafael Espindola45e6c192011-01-08 16:42:36 +00001416 if (GV->hasUnnamedAddr()) Out << "unnamed_addr ";
Misha Brukmana6619a92004-06-21 21:53:56 +00001417 Out << (GV->isConstant() ? "constant " : "global ");
Chris Lattnere101c442009-02-28 21:26:53 +00001418 TypePrinter.print(GV->getType()->getElementType(), Out);
Chris Lattner37798642001-09-18 04:01:05 +00001419
Dan Gohman81313fd2008-09-14 17:21:12 +00001420 if (GV->hasInitializer()) {
1421 Out << ' ';
Devang Patel983c6b12009-07-08 21:44:25 +00001422 writeOperand(GV->getInitializer(), false);
Dan Gohman81313fd2008-09-14 17:21:12 +00001423 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001424
Chris Lattner9380b812010-07-07 23:16:37 +00001425 if (GV->hasSection()) {
1426 Out << ", section \"";
1427 PrintEscapedString(GV->getSection(), Out);
1428 Out << '"';
1429 }
Chris Lattner4b96c542005-11-12 00:10:19 +00001430 if (GV->getAlignment())
Chris Lattnerf8a974d2005-11-06 06:48:53 +00001431 Out << ", align " << GV->getAlignment();
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001432
Chris Lattner113f4f42002-06-25 16:13:24 +00001433 printInfoComment(*GV);
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001434 Out << '\n';
Chris Lattnerda975502001-09-10 07:58:01 +00001435}
1436
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001437void AssemblyWriter::printAlias(const GlobalAlias *GA) {
Dan Gohman5ded1422010-01-29 23:12:36 +00001438 if (GA->isMaterializable())
1439 Out << "; Materializable\n";
1440
Dale Johannesen83e468a2008-06-03 18:14:29 +00001441 // Don't crash when dumping partially built GA
1442 if (!GA->hasName())
1443 Out << "<<nameless>> = ";
Chris Lattner033935d2008-08-17 04:40:13 +00001444 else {
1445 PrintLLVMName(Out, GA);
1446 Out << " = ";
1447 }
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001448 PrintVisibility(GA->getVisibility(), Out);
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001449
1450 Out << "alias ";
1451
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001452 PrintLinkage(GA->getLinkage(), Out);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001453
Anton Korobeynikov546ea7e2007-04-29 18:02:48 +00001454 const Constant *Aliasee = GA->getAliasee();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001455
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001456 if (Aliasee == 0) {
1457 TypePrinter.print(GA->getType(), Out);
1458 Out << " <<NULL ALIASEE>>";
Jay Foad92c19132011-08-01 12:48:54 +00001459 } else {
Jay Foad26db79d2011-08-01 12:29:14 +00001460 writeOperand(Aliasee, !isa<ConstantExpr>(Aliasee));
Jay Foad92c19132011-08-01 12:48:54 +00001461 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001462
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001463 printInfoComment(*GA);
Chris Lattner1508d3f2008-08-19 05:16:28 +00001464 Out << '\n';
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001465}
1466
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001467void AssemblyWriter::printTypeIdentities() {
1468 if (TypePrinter.NumberedTypes.empty() &&
1469 TypePrinter.NamedTypes.empty())
1470 return;
Andrew Trickec4b6e72011-09-30 19:48:58 +00001471
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001472 Out << '\n';
Andrew Trickec4b6e72011-09-30 19:48:58 +00001473
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001474 // We know all the numbers that each type is used and we know that it is a
1475 // dense assignment. Convert the map to an index table.
1476 std::vector<StructType*> NumberedTypes(TypePrinter.NumberedTypes.size());
Andrew Trickec4b6e72011-09-30 19:48:58 +00001477 for (DenseMap<StructType*, unsigned>::iterator I =
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001478 TypePrinter.NumberedTypes.begin(), E = TypePrinter.NumberedTypes.end();
1479 I != E; ++I) {
1480 assert(I->second < NumberedTypes.size() && "Didn't get a dense numbering?");
1481 NumberedTypes[I->second] = I->first;
1482 }
Andrew Trickec4b6e72011-09-30 19:48:58 +00001483
Chris Lattner3243ea12009-03-01 00:03:38 +00001484 // Emit all numbered types.
1485 for (unsigned i = 0, e = NumberedTypes.size(); i != e; ++i) {
Dan Gohman466876b2009-08-12 23:32:33 +00001486 Out << '%' << i << " = type ";
Andrew Trickec4b6e72011-09-30 19:48:58 +00001487
Chris Lattner3243ea12009-03-01 00:03:38 +00001488 // Make sure we print out at least one level of the type structure, so
1489 // that we do not get %2 = type %2
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001490 TypePrinter.printStructBody(NumberedTypes[i], Out);
Dan Gohman69273e62009-08-12 23:54:22 +00001491 Out << '\n';
Chris Lattner3243ea12009-03-01 00:03:38 +00001492 }
Andrew Trickec4b6e72011-09-30 19:48:58 +00001493
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001494 for (unsigned i = 0, e = TypePrinter.NamedTypes.size(); i != e; ++i) {
1495 PrintLLVMName(Out, TypePrinter.NamedTypes[i]->getName(), LocalPrefix);
Chris Lattner1508d3f2008-08-19 05:16:28 +00001496 Out << " = type ";
Reid Spencere7e96712004-05-25 08:53:40 +00001497
1498 // Make sure we print out at least one level of the type structure, so
1499 // that we do not get %FILE = type %FILE
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001500 TypePrinter.printStructBody(TypePrinter.NamedTypes[i], Out);
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001501 Out << '\n';
Reid Spencere7e96712004-05-25 08:53:40 +00001502 }
Reid Spencer32af9e82007-01-06 07:24:44 +00001503}
1504
Misha Brukmanc566ca362004-03-02 00:22:19 +00001505/// printFunction - Print all aspects of a function.
1506///
Chris Lattner113f4f42002-06-25 16:13:24 +00001507void AssemblyWriter::printFunction(const Function *F) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001508 // Print out the return type and name.
1509 Out << '\n';
Chris Lattner379a8d22003-04-16 20:28:45 +00001510
Misha Brukmana6619a92004-06-21 21:53:56 +00001511 if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001512
Dan Gohman5ded1422010-01-29 23:12:36 +00001513 if (F->isMaterializable())
1514 Out << "; Materializable\n";
1515
Reid Spencer5301e7c2007-01-30 20:08:39 +00001516 if (F->isDeclaration())
Chris Lattner10f03a62007-08-19 22:15:26 +00001517 Out << "declare ";
1518 else
Reid Spencer7ce2d2a2006-12-29 20:29:48 +00001519 Out << "define ";
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001520
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001521 PrintLinkage(F->getLinkage(), Out);
1522 PrintVisibility(F->getVisibility(), Out);
Chris Lattner379a8d22003-04-16 20:28:45 +00001523
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001524 // Print the calling convention.
1525 switch (F->getCallingConv()) {
1526 case CallingConv::C: break; // default
Anton Korobeynikov3c5b3df2006-09-20 22:03:51 +00001527 case CallingConv::Fast: Out << "fastcc "; break;
1528 case CallingConv::Cold: Out << "coldcc "; break;
1529 case CallingConv::X86_StdCall: Out << "x86_stdcallcc "; break;
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001530 case CallingConv::X86_FastCall: Out << "x86_fastcallcc "; break;
Anton Korobeynikov8f35fab2010-05-16 09:08:45 +00001531 case CallingConv::X86_ThisCall: Out << "x86_thiscallcc "; break;
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001532 case CallingConv::ARM_APCS: Out << "arm_apcscc "; break;
1533 case CallingConv::ARM_AAPCS: Out << "arm_aapcscc "; break;
1534 case CallingConv::ARM_AAPCS_VFP:Out << "arm_aapcs_vfpcc "; break;
Anton Korobeynikov27a0ecf2009-12-07 02:27:35 +00001535 case CallingConv::MSP430_INTR: Out << "msp430_intrcc "; break;
Justin Holewinski0cfa7372011-03-07 14:32:30 +00001536 case CallingConv::PTX_Kernel: Out << "ptx_kernel "; break;
1537 case CallingConv::PTX_Device: Out << "ptx_device "; break;
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001538 default: Out << "cc" << F->getCallingConv() << " "; break;
1539 }
1540
Chris Lattner229907c2011-07-18 04:54:35 +00001541 FunctionType *FT = F->getFunctionType();
Devang Patel4c758ea2008-09-25 21:00:45 +00001542 const AttrListPtr &Attrs = F->getAttributes();
Devang Patel221fe422008-09-29 20:49:50 +00001543 Attributes RetAttrs = Attrs.getRetAttributes();
1544 if (RetAttrs != Attribute::None)
1545 Out << Attribute::getAsString(Attrs.getRetAttributes()) << ' ';
Chris Lattnere101c442009-02-28 21:26:53 +00001546 TypePrinter.print(F->getReturnType(), Out);
Chris Lattner585297e82008-08-19 05:26:17 +00001547 Out << ' ';
Dan Gohmana2489d12010-07-20 23:55:01 +00001548 WriteAsOperandInternal(Out, F, &TypePrinter, &Machine, F->getParent());
Misha Brukmana6619a92004-06-21 21:53:56 +00001549 Out << '(';
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001550 Machine.incorporateFunction(F);
Chris Lattnerfee714f2001-09-07 16:36:04 +00001551
Chris Lattner7bfee412001-10-29 16:05:51 +00001552 // Loop over the arguments, printing them...
Chris Lattnerfee714f2001-09-07 16:36:04 +00001553
Reid Spencer8c4914c2006-12-31 05:24:50 +00001554 unsigned Idx = 1;
Chris Lattner82738fe2007-04-18 00:57:22 +00001555 if (!F->isDeclaration()) {
1556 // If this isn't a declaration, print the argument names as well.
1557 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
1558 I != E; ++I) {
1559 // Insert commas as we go... the first arg doesn't get a comma
1560 if (I != F->arg_begin()) Out << ", ";
Devang Patela05633e2008-09-26 22:53:05 +00001561 printArgument(I, Attrs.getParamAttributes(Idx));
Chris Lattner82738fe2007-04-18 00:57:22 +00001562 Idx++;
1563 }
1564 } else {
1565 // Otherwise, print the types from the function type.
1566 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
1567 // Insert commas as we go... the first arg doesn't get a comma
1568 if (i) Out << ", ";
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001569
Chris Lattner82738fe2007-04-18 00:57:22 +00001570 // Output type...
Chris Lattnere101c442009-02-28 21:26:53 +00001571 TypePrinter.print(FT->getParamType(i), Out);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001572
Devang Patela05633e2008-09-26 22:53:05 +00001573 Attributes ArgAttrs = Attrs.getParamAttributes(i+1);
Devang Patel4c758ea2008-09-25 21:00:45 +00001574 if (ArgAttrs != Attribute::None)
1575 Out << ' ' << Attribute::getAsString(ArgAttrs);
Chris Lattner82738fe2007-04-18 00:57:22 +00001576 }
Reid Spencer8c4914c2006-12-31 05:24:50 +00001577 }
Chris Lattnerfee714f2001-09-07 16:36:04 +00001578
1579 // Finish printing arguments...
Chris Lattner113f4f42002-06-25 16:13:24 +00001580 if (FT->isVarArg()) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001581 if (FT->getNumParams()) Out << ", ";
1582 Out << "..."; // Output varargs portion of signature!
Chris Lattnerfee714f2001-09-07 16:36:04 +00001583 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001584 Out << ')';
Rafael Espindola563eb4b2011-01-25 19:09:56 +00001585 if (F->hasUnnamedAddr())
1586 Out << " unnamed_addr";
Devang Patela05633e2008-09-26 22:53:05 +00001587 Attributes FnAttrs = Attrs.getFnAttributes();
1588 if (FnAttrs != Attribute::None)
1589 Out << ' ' << Attribute::getAsString(Attrs.getFnAttributes());
Chris Lattner9380b812010-07-07 23:16:37 +00001590 if (F->hasSection()) {
1591 Out << " section \"";
1592 PrintEscapedString(F->getSection(), Out);
1593 Out << '"';
1594 }
Chris Lattnerf8a974d2005-11-06 06:48:53 +00001595 if (F->getAlignment())
1596 Out << " align " << F->getAlignment();
Gordon Henriksend930f912008-08-17 18:44:35 +00001597 if (F->hasGC())
1598 Out << " gc \"" << F->getGC() << '"';
Reid Spencer5301e7c2007-01-30 20:08:39 +00001599 if (F->isDeclaration()) {
Chris Lattnerfb483622010-09-02 22:52:10 +00001600 Out << '\n';
Chris Lattnerb2f02e52002-05-06 03:00:40 +00001601 } else {
Chris Lattnerfb483622010-09-02 22:52:10 +00001602 Out << " {";
1603 // Output all of the function's basic blocks.
Chris Lattner113f4f42002-06-25 16:13:24 +00001604 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
1605 printBasicBlock(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +00001606
Misha Brukmana6619a92004-06-21 21:53:56 +00001607 Out << "}\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +00001608 }
1609
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001610 Machine.purgeFunction();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001611}
1612
Misha Brukmanc566ca362004-03-02 00:22:19 +00001613/// printArgument - This member is called for every argument that is passed into
1614/// the function. Simply print it out
1615///
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001616void AssemblyWriter::printArgument(const Argument *Arg,
Devang Patelba3fa6c2008-09-23 23:03:40 +00001617 Attributes Attrs) {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001618 // Output type...
Chris Lattnere101c442009-02-28 21:26:53 +00001619 TypePrinter.print(Arg->getType(), Out);
Misha Brukmanb1c93172005-04-21 23:48:37 +00001620
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00001621 // Output parameter attributes list
Devang Patel4c758ea2008-09-25 21:00:45 +00001622 if (Attrs != Attribute::None)
1623 Out << ' ' << Attribute::getAsString(Attrs);
Reid Spencer8c4914c2006-12-31 05:24:50 +00001624
Chris Lattner2f7c9632001-06-06 20:29:01 +00001625 // Output name, if available...
Chris Lattner033935d2008-08-17 04:40:13 +00001626 if (Arg->hasName()) {
1627 Out << ' ';
1628 PrintLLVMName(Out, Arg);
1629 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001630}
1631
Misha Brukmanc566ca362004-03-02 00:22:19 +00001632/// printBasicBlock - This member is called for each basic block in a method.
1633///
Chris Lattner7bfee412001-10-29 16:05:51 +00001634void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Nick Lewycky4d43d3c2008-04-25 16:53:59 +00001635 if (BB->hasName()) { // Print out the label if it exists...
Chris Lattner033935d2008-08-17 04:40:13 +00001636 Out << "\n";
Daniel Dunbare03eecb2009-07-25 23:55:21 +00001637 PrintLLVMName(Out, BB->getName(), LabelPrefix);
Chris Lattner033935d2008-08-17 04:40:13 +00001638 Out << ':';
Nick Lewycky4d43d3c2008-04-25 16:53:59 +00001639 } else if (!BB->use_empty()) { // Don't print block # of no uses...
Bill Wendling35a9c3c2011-04-10 23:18:04 +00001640 Out << "\n; <label>:";
Chris Lattner5e043322007-01-11 03:54:27 +00001641 int Slot = Machine.getLocalSlot(BB);
Chris Lattner757ee0b2004-06-09 19:41:19 +00001642 if (Slot != -1)
Misha Brukmana6619a92004-06-21 21:53:56 +00001643 Out << Slot;
Chris Lattner757ee0b2004-06-09 19:41:19 +00001644 else
Misha Brukmana6619a92004-06-21 21:53:56 +00001645 Out << "<badref>";
Chris Lattner58185f22002-10-02 19:38:55 +00001646 }
Chris Lattner2447ef52003-11-20 00:09:43 +00001647
Dan Gohmane2745262009-08-12 17:23:50 +00001648 if (BB->getParent() == 0) {
Chris Lattneree97b8b2009-08-17 15:48:08 +00001649 Out.PadToColumn(50);
Dan Gohmane2745262009-08-12 17:23:50 +00001650 Out << "; Error: Block without parent!";
1651 } else if (BB != &BB->getParent()->getEntryBlock()) { // Not the entry block?
Chris Lattnerfb483622010-09-02 22:52:10 +00001652 // Output predecessors for the block.
Chris Lattneree97b8b2009-08-17 15:48:08 +00001653 Out.PadToColumn(50);
Dan Gohmane2745262009-08-12 17:23:50 +00001654 Out << ";";
Gabor Greif6c6b2fd2010-03-25 23:25:28 +00001655 const_pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001656
Chris Lattnerff834c02008-04-22 02:45:44 +00001657 if (PI == PE) {
1658 Out << " No predecessors!";
1659 } else {
Dan Gohman81313fd2008-09-14 17:21:12 +00001660 Out << " preds = ";
Chris Lattnerff834c02008-04-22 02:45:44 +00001661 writeOperand(*PI, false);
1662 for (++PI; PI != PE; ++PI) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001663 Out << ", ";
Chris Lattner78e2e8b2006-12-06 06:24:27 +00001664 writeOperand(*PI, false);
Chris Lattner00211f12003-11-16 22:59:57 +00001665 }
Chris Lattner58185f22002-10-02 19:38:55 +00001666 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001667 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001668
Chris Lattnerff834c02008-04-22 02:45:44 +00001669 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001670
Misha Brukmana6619a92004-06-21 21:53:56 +00001671 if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001672
Chris Lattnerfee714f2001-09-07 16:36:04 +00001673 // Output all of the instructions in the basic block...
Dan Gohmana4f709e2009-07-13 18:27:59 +00001674 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
Chris Lattner113f4f42002-06-25 16:13:24 +00001675 printInstruction(*I);
Dan Gohmana4f709e2009-07-13 18:27:59 +00001676 Out << '\n';
1677 }
Chris Lattner96cdd272004-03-08 18:51:45 +00001678
Misha Brukmana6619a92004-06-21 21:53:56 +00001679 if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, Out);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001680}
1681
Misha Brukmanc566ca362004-03-02 00:22:19 +00001682/// printInfoComment - Print a little comment after the instruction indicating
1683/// which slot it occupies.
1684///
Chris Lattner113f4f42002-06-25 16:13:24 +00001685void AssemblyWriter::printInfoComment(const Value &V) {
Dan Gohmane34890f2010-02-10 20:41:46 +00001686 if (AnnotationWriter) {
1687 AnnotationWriter->printInfoComment(V, Out);
1688 return;
1689 }
Chris Lattner862e3382001-10-13 06:42:36 +00001690}
1691
Reid Spencere7141c82006-08-28 01:02:49 +00001692// This member is called for each Instruction in a function..
Chris Lattner113f4f42002-06-25 16:13:24 +00001693void AssemblyWriter::printInstruction(const Instruction &I) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001694 if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001695
Dan Gohman466876b2009-08-12 23:32:33 +00001696 // Print out indentation for an instruction.
Dan Gohmanb4583b12009-08-13 01:41:52 +00001697 Out << " ";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001698
1699 // Print out name if it exists...
Chris Lattner033935d2008-08-17 04:40:13 +00001700 if (I.hasName()) {
1701 PrintLLVMName(Out, &I);
1702 Out << " = ";
Chris Lattner031560c2009-12-29 07:25:48 +00001703 } else if (!I.getType()->isVoidTy()) {
Chris Lattnerb25e5ea2008-08-29 17:19:30 +00001704 // Print out the def slot taken.
1705 int SlotNum = Machine.getLocalSlot(&I);
1706 if (SlotNum == -1)
1707 Out << "<badref> = ";
1708 else
1709 Out << '%' << SlotNum << " = ";
Chris Lattner033935d2008-08-17 04:40:13 +00001710 }
Andrew Trickec4b6e72011-09-30 19:48:58 +00001711
Eli Friedman59b66882011-08-09 23:02:53 +00001712 if (isa<CallInst>(I) && cast<CallInst>(I).isTailCall())
Chris Lattner06038452005-05-06 05:51:46 +00001713 Out << "tail ";
Chris Lattner504f9242003-09-08 17:45:59 +00001714
Chris Lattner2f7c9632001-06-06 20:29:01 +00001715 // Print out the opcode...
Misha Brukmana6619a92004-06-21 21:53:56 +00001716 Out << I.getOpcodeName();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001717
Eli Friedman02e737b2011-08-12 22:50:01 +00001718 // If this is an atomic load or store, print out the atomic marker.
1719 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isAtomic()) ||
1720 (isa<StoreInst>(I) && cast<StoreInst>(I).isAtomic()))
1721 Out << " atomic";
1722
1723 // If this is a volatile operation, print out the volatile marker.
1724 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) ||
1725 (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile()) ||
1726 (isa<AtomicCmpXchgInst>(I) && cast<AtomicCmpXchgInst>(I).isVolatile()) ||
1727 (isa<AtomicRMWInst>(I) && cast<AtomicRMWInst>(I).isVolatile()))
1728 Out << " volatile";
1729
Dan Gohman9c7f8082009-07-27 16:11:46 +00001730 // Print out optimization information.
1731 WriteOptimizationInfo(Out, &I);
1732
Reid Spencer45e52392006-12-03 06:27:29 +00001733 // Print out the compare instruction predicates
Nate Begemand2195702008-05-12 19:01:56 +00001734 if (const CmpInst *CI = dyn_cast<CmpInst>(&I))
Chris Lattner82ff9232008-08-23 22:52:27 +00001735 Out << ' ' << getPredicateText(CI->getPredicate());
Reid Spencer45e52392006-12-03 06:27:29 +00001736
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001737 // Print out the atomicrmw operation
1738 if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(&I))
1739 writeAtomicRMWOperation(Out, RMWI->getOperation());
1740
Chris Lattner2f7c9632001-06-06 20:29:01 +00001741 // Print out the type of the operands...
Chris Lattner113f4f42002-06-25 16:13:24 +00001742 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001743
1744 // Special case conditional branches to swizzle the condition out to the front
Gabor Greifcab008f2009-02-09 15:45:06 +00001745 if (isa<BranchInst>(I) && cast<BranchInst>(I).isConditional()) {
1746 BranchInst &BI(cast<BranchInst>(I));
Dan Gohman81313fd2008-09-14 17:21:12 +00001747 Out << ' ';
Gabor Greifcab008f2009-02-09 15:45:06 +00001748 writeOperand(BI.getCondition(), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00001749 Out << ", ";
Gabor Greifcab008f2009-02-09 15:45:06 +00001750 writeOperand(BI.getSuccessor(0), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00001751 Out << ", ";
Gabor Greifcab008f2009-02-09 15:45:06 +00001752 writeOperand(BI.getSuccessor(1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001753
Chris Lattner8d48df22002-04-13 18:34:38 +00001754 } else if (isa<SwitchInst>(I)) {
Eli Friedman95031ed2011-09-29 20:21:17 +00001755 SwitchInst& SI(cast<SwitchInst>(I));
Chris Lattner3ed871f2009-10-27 19:13:16 +00001756 // Special case switch instruction to get formatting nice and correct.
Dan Gohman81313fd2008-09-14 17:21:12 +00001757 Out << ' ';
Eli Friedman95031ed2011-09-29 20:21:17 +00001758 writeOperand(SI.getCondition(), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00001759 Out << ", ";
Eli Friedman95031ed2011-09-29 20:21:17 +00001760 writeOperand(SI.getDefaultDest(), true);
Chris Lattner82ff9232008-08-23 22:52:27 +00001761 Out << " [";
Stepan Dyatkovskiy97b02fc2012-03-11 06:09:17 +00001762 for (SwitchInst::CaseIt i = SI.case_begin(), e = SI.case_end();
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00001763 i != e; ++i) {
Dan Gohmanb4583b12009-08-13 01:41:52 +00001764 Out << "\n ";
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00001765 writeOperand(i.getCaseValue(), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00001766 Out << ", ";
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00001767 writeOperand(i.getCaseSuccessor(), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001768 }
Dan Gohmanb4583b12009-08-13 01:41:52 +00001769 Out << "\n ]";
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00001770 } else if (isa<IndirectBrInst>(I)) {
1771 // Special case indirectbr instruction to get formatting nice and correct.
Chris Lattner3ed871f2009-10-27 19:13:16 +00001772 Out << ' ';
1773 writeOperand(Operand, true);
Dan Gohman43c57402009-10-30 02:01:10 +00001774 Out << ", [";
Andrew Trickec4b6e72011-09-30 19:48:58 +00001775
Chris Lattner3ed871f2009-10-27 19:13:16 +00001776 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) {
1777 if (i != 1)
1778 Out << ", ";
1779 writeOperand(I.getOperand(i), true);
1780 }
1781 Out << ']';
Jay Foad372ad642011-06-20 14:18:48 +00001782 } else if (const PHINode *PN = dyn_cast<PHINode>(&I)) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001783 Out << ' ';
Chris Lattnere101c442009-02-28 21:26:53 +00001784 TypePrinter.print(I.getType(), Out);
Misha Brukmana6619a92004-06-21 21:53:56 +00001785 Out << ' ';
Chris Lattner2f7c9632001-06-06 20:29:01 +00001786
Jay Foad372ad642011-06-20 14:18:48 +00001787 for (unsigned op = 0, Eop = PN->getNumIncomingValues(); op < Eop; ++op) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001788 if (op) Out << ", ";
Dan Gohman81313fd2008-09-14 17:21:12 +00001789 Out << "[ ";
Jay Foad372ad642011-06-20 14:18:48 +00001790 writeOperand(PN->getIncomingValue(op), false); Out << ", ";
1791 writeOperand(PN->getIncomingBlock(op), false); Out << " ]";
Chris Lattner931ef3b2001-06-11 15:04:20 +00001792 }
Dan Gohmana76f0f72008-05-31 19:12:39 +00001793 } else if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(&I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001794 Out << ' ';
Dan Gohmana76f0f72008-05-31 19:12:39 +00001795 writeOperand(I.getOperand(0), true);
1796 for (const unsigned *i = EVI->idx_begin(), *e = EVI->idx_end(); i != e; ++i)
1797 Out << ", " << *i;
1798 } else if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(&I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001799 Out << ' ';
1800 writeOperand(I.getOperand(0), true); Out << ", ";
Dan Gohmana76f0f72008-05-31 19:12:39 +00001801 writeOperand(I.getOperand(1), true);
1802 for (const unsigned *i = IVI->idx_begin(), *e = IVI->idx_end(); i != e; ++i)
1803 Out << ", " << *i;
Bill Wendlingfae14752011-08-12 20:24:12 +00001804 } else if (const LandingPadInst *LPI = dyn_cast<LandingPadInst>(&I)) {
1805 Out << ' ';
1806 TypePrinter.print(I.getType(), Out);
1807 Out << " personality ";
1808 writeOperand(I.getOperand(0), true); Out << '\n';
1809
1810 if (LPI->isCleanup())
1811 Out << " cleanup";
1812
1813 for (unsigned i = 0, e = LPI->getNumClauses(); i != e; ++i) {
1814 if (i != 0 || LPI->isCleanup()) Out << "\n";
1815 if (LPI->isCatch(i))
1816 Out << " catch ";
1817 else
1818 Out << " filter ";
1819
1820 writeOperand(LPI->getClause(i), true);
1821 }
Devang Patel59643e52008-02-23 00:35:18 +00001822 } else if (isa<ReturnInst>(I) && !Operand) {
1823 Out << " void";
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001824 } else if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
1825 // Print the calling convention being used.
1826 switch (CI->getCallingConv()) {
1827 case CallingConv::C: break; // default
Chris Lattner29d20852006-05-19 21:58:52 +00001828 case CallingConv::Fast: Out << " fastcc"; break;
1829 case CallingConv::Cold: Out << " coldcc"; break;
Chris Lattnerf5270372007-11-18 18:32:16 +00001830 case CallingConv::X86_StdCall: Out << " x86_stdcallcc"; break;
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001831 case CallingConv::X86_FastCall: Out << " x86_fastcallcc"; break;
Anton Korobeynikov8f35fab2010-05-16 09:08:45 +00001832 case CallingConv::X86_ThisCall: Out << " x86_thiscallcc"; break;
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001833 case CallingConv::ARM_APCS: Out << " arm_apcscc "; break;
1834 case CallingConv::ARM_AAPCS: Out << " arm_aapcscc "; break;
1835 case CallingConv::ARM_AAPCS_VFP:Out << " arm_aapcs_vfpcc "; break;
Anton Korobeynikov27a0ecf2009-12-07 02:27:35 +00001836 case CallingConv::MSP430_INTR: Out << " msp430_intrcc "; break;
Che-Liang Chiou29947902010-09-25 07:46:17 +00001837 case CallingConv::PTX_Kernel: Out << " ptx_kernel"; break;
1838 case CallingConv::PTX_Device: Out << " ptx_device"; break;
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001839 default: Out << " cc" << CI->getCallingConv(); break;
1840 }
1841
Gabor Greifc9a92512010-06-23 13:09:06 +00001842 Operand = CI->getCalledValue();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001843 PointerType *PTy = cast<PointerType>(Operand->getType());
1844 FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1845 Type *RetTy = FTy->getReturnType();
Devang Patel4c758ea2008-09-25 21:00:45 +00001846 const AttrListPtr &PAL = CI->getAttributes();
Chris Lattner2f2d9472001-11-06 21:28:12 +00001847
Devang Patel221fe422008-09-29 20:49:50 +00001848 if (PAL.getRetAttributes() != Attribute::None)
1849 Out << ' ' << Attribute::getAsString(PAL.getRetAttributes());
1850
Chris Lattner463d6a52003-08-05 15:34:45 +00001851 // If possible, print out the short form of the call instruction. We can
Chris Lattner6915f8f2002-04-07 22:49:37 +00001852 // only do this if the first argument is a pointer to a nonvararg function,
Chris Lattner463d6a52003-08-05 15:34:45 +00001853 // and if the return type is not a pointer to a function.
Chris Lattner2f2d9472001-11-06 21:28:12 +00001854 //
Dan Gohman81313fd2008-09-14 17:21:12 +00001855 Out << ' ';
Chris Lattner463d6a52003-08-05 15:34:45 +00001856 if (!FTy->isVarArg() &&
Duncan Sands19d0b472010-02-16 11:11:14 +00001857 (!RetTy->isPointerTy() ||
1858 !cast<PointerType>(RetTy)->getElementType()->isFunctionTy())) {
Chris Lattnere101c442009-02-28 21:26:53 +00001859 TypePrinter.print(RetTy, Out);
Dan Gohman81313fd2008-09-14 17:21:12 +00001860 Out << ' ';
Chris Lattner2f2d9472001-11-06 21:28:12 +00001861 writeOperand(Operand, false);
1862 } else {
1863 writeOperand(Operand, true);
1864 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001865 Out << '(';
Gabor Greifc9a92512010-06-23 13:09:06 +00001866 for (unsigned op = 0, Eop = CI->getNumArgOperands(); op < Eop; ++op) {
1867 if (op > 0)
Dan Gohman81313fd2008-09-14 17:21:12 +00001868 Out << ", ";
Gabor Greifc9a92512010-06-23 13:09:06 +00001869 writeParamOperand(CI->getArgOperand(op), PAL.getParamAttributes(op + 1));
Chris Lattner2f7c9632001-06-06 20:29:01 +00001870 }
Dan Gohman81313fd2008-09-14 17:21:12 +00001871 Out << ')';
Devang Patela05633e2008-09-26 22:53:05 +00001872 if (PAL.getFnAttributes() != Attribute::None)
1873 Out << ' ' << Attribute::getAsString(PAL.getFnAttributes());
Chris Lattner113f4f42002-06-25 16:13:24 +00001874 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
Gabor Greifa2fbc0a2010-03-24 13:21:49 +00001875 Operand = II->getCalledValue();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001876 PointerType *PTy = cast<PointerType>(Operand->getType());
1877 FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1878 Type *RetTy = FTy->getReturnType();
Devang Patel4c758ea2008-09-25 21:00:45 +00001879 const AttrListPtr &PAL = II->getAttributes();
Chris Lattner463d6a52003-08-05 15:34:45 +00001880
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001881 // Print the calling convention being used.
1882 switch (II->getCallingConv()) {
1883 case CallingConv::C: break; // default
Chris Lattner29d20852006-05-19 21:58:52 +00001884 case CallingConv::Fast: Out << " fastcc"; break;
1885 case CallingConv::Cold: Out << " coldcc"; break;
Dan Gohman81313fd2008-09-14 17:21:12 +00001886 case CallingConv::X86_StdCall: Out << " x86_stdcallcc"; break;
1887 case CallingConv::X86_FastCall: Out << " x86_fastcallcc"; break;
Anton Korobeynikov8f35fab2010-05-16 09:08:45 +00001888 case CallingConv::X86_ThisCall: Out << " x86_thiscallcc"; break;
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001889 case CallingConv::ARM_APCS: Out << " arm_apcscc "; break;
1890 case CallingConv::ARM_AAPCS: Out << " arm_aapcscc "; break;
1891 case CallingConv::ARM_AAPCS_VFP:Out << " arm_aapcs_vfpcc "; break;
Anton Korobeynikov27a0ecf2009-12-07 02:27:35 +00001892 case CallingConv::MSP430_INTR: Out << " msp430_intrcc "; break;
Che-Liang Chiou29947902010-09-25 07:46:17 +00001893 case CallingConv::PTX_Kernel: Out << " ptx_kernel"; break;
1894 case CallingConv::PTX_Device: Out << " ptx_device"; break;
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001895 default: Out << " cc" << II->getCallingConv(); break;
1896 }
1897
Devang Patel221fe422008-09-29 20:49:50 +00001898 if (PAL.getRetAttributes() != Attribute::None)
1899 Out << ' ' << Attribute::getAsString(PAL.getRetAttributes());
1900
Chris Lattner463d6a52003-08-05 15:34:45 +00001901 // If possible, print out the short form of the invoke instruction. We can
1902 // only do this if the first argument is a pointer to a nonvararg function,
1903 // and if the return type is not a pointer to a function.
1904 //
Dan Gohmanc7e00ba2008-10-15 18:02:08 +00001905 Out << ' ';
Chris Lattner463d6a52003-08-05 15:34:45 +00001906 if (!FTy->isVarArg() &&
Duncan Sands19d0b472010-02-16 11:11:14 +00001907 (!RetTy->isPointerTy() ||
1908 !cast<PointerType>(RetTy)->getElementType()->isFunctionTy())) {
Chris Lattnere101c442009-02-28 21:26:53 +00001909 TypePrinter.print(RetTy, Out);
Dan Gohmanc7e00ba2008-10-15 18:02:08 +00001910 Out << ' ';
Chris Lattner463d6a52003-08-05 15:34:45 +00001911 writeOperand(Operand, false);
1912 } else {
1913 writeOperand(Operand, true);
1914 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001915 Out << '(';
Gabor Greifc9a92512010-06-23 13:09:06 +00001916 for (unsigned op = 0, Eop = II->getNumArgOperands(); op < Eop; ++op) {
Gabor Greifa2fbc0a2010-03-24 13:21:49 +00001917 if (op)
Dan Gohman81313fd2008-09-14 17:21:12 +00001918 Out << ", ";
Gabor Greifc9a92512010-06-23 13:09:06 +00001919 writeParamOperand(II->getArgOperand(op), PAL.getParamAttributes(op + 1));
Chris Lattner862e3382001-10-13 06:42:36 +00001920 }
1921
Dan Gohman81313fd2008-09-14 17:21:12 +00001922 Out << ')';
Devang Patela05633e2008-09-26 22:53:05 +00001923 if (PAL.getFnAttributes() != Attribute::None)
1924 Out << ' ' << Attribute::getAsString(PAL.getFnAttributes());
1925
Dan Gohmanb4583b12009-08-13 01:41:52 +00001926 Out << "\n to ";
Chris Lattner862e3382001-10-13 06:42:36 +00001927 writeOperand(II->getNormalDest(), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00001928 Out << " unwind ";
Chris Lattnerfae8ab32004-02-08 21:44:31 +00001929 writeOperand(II->getUnwindDest(), true);
Chris Lattner862e3382001-10-13 06:42:36 +00001930
Victor Hernandez8acf2952009-10-23 21:09:37 +00001931 } else if (const AllocaInst *AI = dyn_cast<AllocaInst>(&I)) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001932 Out << ' ';
Chris Lattnere101c442009-02-28 21:26:53 +00001933 TypePrinter.print(AI->getType()->getElementType(), Out);
Dan Gohmanb53e26c2009-07-31 18:23:24 +00001934 if (!AI->getArraySize() || AI->isArrayAllocation()) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001935 Out << ", ";
Chris Lattner8d48df22002-04-13 18:34:38 +00001936 writeOperand(AI->getArraySize(), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001937 }
Nate Begeman848622f2005-11-05 09:21:28 +00001938 if (AI->getAlignment()) {
Chris Lattner7aeee3a2005-11-05 21:20:34 +00001939 Out << ", align " << AI->getAlignment();
Nate Begeman848622f2005-11-05 09:21:28 +00001940 }
Chris Lattner862e3382001-10-13 06:42:36 +00001941 } else if (isa<CastInst>(I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001942 if (Operand) {
1943 Out << ' ';
1944 writeOperand(Operand, true); // Work with broken code
1945 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001946 Out << " to ";
Chris Lattnere101c442009-02-28 21:26:53 +00001947 TypePrinter.print(I.getType(), Out);
Chris Lattner5b337482003-10-18 05:57:43 +00001948 } else if (isa<VAArgInst>(I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001949 if (Operand) {
1950 Out << ' ';
1951 writeOperand(Operand, true); // Work with broken code
1952 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001953 Out << ", ";
Chris Lattnere101c442009-02-28 21:26:53 +00001954 TypePrinter.print(I.getType(), Out);
1955 } else if (Operand) { // Print the normal way.
Chris Lattner2f7c9632001-06-06 20:29:01 +00001956
Misha Brukmanb1c93172005-04-21 23:48:37 +00001957 // PrintAllTypes - Instructions who have operands of all the same type
Chris Lattner2f7c9632001-06-06 20:29:01 +00001958 // omit the type from all but the first operand. If the instruction has
1959 // different type operands (for example br), then they are all printed.
1960 bool PrintAllTypes = false;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001961 Type *TheType = Operand->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001962
Reid Spencer0cdd04f2007-02-02 13:54:55 +00001963 // Select, Store and ShuffleVector always print all types.
Devang Patelce556d92008-03-04 22:05:14 +00001964 if (isa<SelectInst>(I) || isa<StoreInst>(I) || isa<ShuffleVectorInst>(I)
1965 || isa<ReturnInst>(I)) {
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00001966 PrintAllTypes = true;
1967 } else {
1968 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
1969 Operand = I.getOperand(i);
Nuno Lopes04fe2f02009-01-15 18:40:57 +00001970 // note that Operand shouldn't be null, but the test helps make dump()
1971 // more tolerant of malformed IR
Nuno Lopes0971e772009-01-14 17:51:41 +00001972 if (Operand && Operand->getType() != TheType) {
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00001973 PrintAllTypes = true; // We have differing types! Print them all!
1974 break;
1975 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001976 }
1977 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001978
Chris Lattner7bfee412001-10-29 16:05:51 +00001979 if (!PrintAllTypes) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001980 Out << ' ';
Chris Lattnere101c442009-02-28 21:26:53 +00001981 TypePrinter.print(TheType, Out);
Chris Lattner7bfee412001-10-29 16:05:51 +00001982 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001983
Dan Gohman81313fd2008-09-14 17:21:12 +00001984 Out << ' ';
Chris Lattner113f4f42002-06-25 16:13:24 +00001985 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001986 if (i) Out << ", ";
Chris Lattner113f4f42002-06-25 16:13:24 +00001987 writeOperand(I.getOperand(i), PrintAllTypes);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001988 }
1989 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001990
Eli Friedman59b66882011-08-09 23:02:53 +00001991 // Print atomic ordering/alignment for memory operations
1992 if (const LoadInst *LI = dyn_cast<LoadInst>(&I)) {
1993 if (LI->isAtomic())
1994 writeAtomic(LI->getOrdering(), LI->getSynchScope());
1995 if (LI->getAlignment())
1996 Out << ", align " << LI->getAlignment();
1997 } else if (const StoreInst *SI = dyn_cast<StoreInst>(&I)) {
1998 if (SI->isAtomic())
1999 writeAtomic(SI->getOrdering(), SI->getSynchScope());
2000 if (SI->getAlignment())
2001 Out << ", align " << SI->getAlignment();
Eli Friedmanc9a551e2011-07-28 21:48:00 +00002002 } else if (const AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(&I)) {
2003 writeAtomic(CXI->getOrdering(), CXI->getSynchScope());
2004 } else if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(&I)) {
2005 writeAtomic(RMWI->getOrdering(), RMWI->getSynchScope());
Eli Friedmanfee02c62011-07-25 23:16:38 +00002006 } else if (const FenceInst *FI = dyn_cast<FenceInst>(&I)) {
2007 writeAtomic(FI->getOrdering(), FI->getSynchScope());
Christopher Lamb84485702007-04-22 19:24:39 +00002008 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00002009
Chris Lattnerc9558df2009-12-28 20:10:43 +00002010 // Print Metadata info.
Nick Lewyckyba8ec9a2010-02-25 06:53:04 +00002011 SmallVector<std::pair<unsigned, MDNode*>, 4> InstMD;
2012 I.getAllMetadata(InstMD);
Erick Tryzelaar72a37132010-03-02 05:32:52 +00002013 if (!InstMD.empty()) {
2014 SmallVector<StringRef, 8> MDNames;
2015 I.getType()->getContext().getMDKindNames(MDNames);
2016 for (unsigned i = 0, e = InstMD.size(); i != e; ++i) {
2017 unsigned Kind = InstMD[i].first;
2018 if (Kind < MDNames.size()) {
2019 Out << ", !" << MDNames[Kind];
2020 } else {
2021 Out << ", !<unknown kind #" << Kind << ">";
2022 }
Dan Gohmana2489d12010-07-20 23:55:01 +00002023 Out << ' ';
2024 WriteAsOperandInternal(Out, InstMD[i].second, &TypePrinter, &Machine,
2025 TheModule);
Nick Lewyckyba8ec9a2010-02-25 06:53:04 +00002026 }
Devang Patelbcdb0252009-10-07 16:37:55 +00002027 }
Chris Lattner862e3382001-10-13 06:42:36 +00002028 printInfoComment(I);
Chris Lattner2f7c9632001-06-06 20:29:01 +00002029}
2030
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002031static void WriteMDNodeComment(const MDNode *Node,
Duncan Sands41b4a6b2010-07-12 08:16:59 +00002032 formatted_raw_ostream &Out) {
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002033 if (Node->getNumOperands() < 1)
2034 return;
Bill Wendling5cb50c52012-06-28 00:41:44 +00002035
2036 Value *Op = Node->getOperand(0);
2037 if (!Op || !isa<ConstantInt>(Op) || cast<ConstantInt>(Op)->getBitWidth() < 32)
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002038 return;
Andrew Trickec4b6e72011-09-30 19:48:58 +00002039
Bill Wendling5cb50c52012-06-28 00:41:44 +00002040 DIDescriptor Desc(Node);
2041 if (Desc.getVersion() < LLVMDebugVersion11)
2042 return;
2043
2044 unsigned Tag = Desc.getTag();
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002045 Out.PadToColumn(50);
Bill Wendlinga0bc1082012-07-03 20:01:02 +00002046 if (dwarf::TagString(Tag)) {
2047 Out << "; ";
2048 Desc.print(Out);
2049 } else if (Tag == dwarf::DW_TAG_user_base) {
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002050 Out << "; [ DW_TAG_user_base ]";
Bill Wendlinga0bc1082012-07-03 20:01:02 +00002051 }
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002052}
2053
2054void AssemblyWriter::writeAllMDNodes() {
2055 SmallVector<const MDNode *, 16> Nodes;
Chris Lattnercf4a76e2009-12-31 02:20:11 +00002056 Nodes.resize(Machine.mdn_size());
2057 for (SlotTracker::mdn_iterator I = Machine.mdn_begin(), E = Machine.mdn_end();
2058 I != E; ++I)
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002059 Nodes[I->second] = cast<MDNode>(I->first);
Andrew Trickec4b6e72011-09-30 19:48:58 +00002060
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002061 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
2062 Out << '!' << i << " = metadata ";
Chris Lattner0d50bdd2009-12-31 02:27:30 +00002063 printMDNodeBody(Nodes[i]);
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002064 }
2065}
2066
2067void AssemblyWriter::printMDNodeBody(const MDNode *Node) {
Dan Gohmana2489d12010-07-20 23:55:01 +00002068 WriteMDNodeBodyInternal(Out, Node, &TypePrinter, &Machine, TheModule);
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002069 WriteMDNodeComment(Node, Out);
2070 Out << "\n";
2071}
Chris Lattner2f7c9632001-06-06 20:29:01 +00002072
2073//===----------------------------------------------------------------------===//
2074// External Interface declarations
2075//===----------------------------------------------------------------------===//
2076
Dan Gohmane2745262009-08-12 17:23:50 +00002077void Module::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW) const {
Chris Lattnere36fd8a2008-08-19 04:26:57 +00002078 SlotTracker SlotTable(this);
Dan Gohmane2745262009-08-12 17:23:50 +00002079 formatted_raw_ostream OS(ROS);
Chris Lattner0c19df42008-08-23 22:23:09 +00002080 AssemblyWriter W(OS, SlotTable, this, AAW);
Chris Lattnerefb5e392009-12-31 02:23:35 +00002081 W.printModule(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00002082}
2083
Dan Gohman2637cc12010-07-21 23:38:33 +00002084void NamedMDNode::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW) const {
2085 SlotTracker SlotTable(getParent());
2086 formatted_raw_ostream OS(ROS);
2087 AssemblyWriter W(OS, SlotTable, getParent(), AAW);
2088 W.printNamedMDNode(this);
2089}
2090
Chris Lattner2ee62d22009-02-28 21:11:05 +00002091void Type::print(raw_ostream &OS) const {
2092 if (this == 0) {
2093 OS << "<null Type>";
2094 return;
2095 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002096 TypePrinting TP;
2097 TP.print(const_cast<Type*>(this), OS);
Andrew Trickec4b6e72011-09-30 19:48:58 +00002098
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002099 // If the type is a named struct type, print the body as well.
2100 if (StructType *STy = dyn_cast<StructType>(const_cast<Type*>(this)))
Chris Lattner01beceb2011-08-12 18:07:07 +00002101 if (!STy->isLiteral()) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002102 OS << " = type ";
2103 TP.printStructBody(STy, OS);
2104 }
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00002105}
2106
Dan Gohmane2745262009-08-12 17:23:50 +00002107void Value::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW) const {
Chris Lattner0c19df42008-08-23 22:23:09 +00002108 if (this == 0) {
Dan Gohman12dad632009-08-12 20:56:03 +00002109 ROS << "printing a <null> value\n";
Chris Lattner0c19df42008-08-23 22:23:09 +00002110 return;
2111 }
Dan Gohman12dad632009-08-12 20:56:03 +00002112 formatted_raw_ostream OS(ROS);
Chris Lattner0c19df42008-08-23 22:23:09 +00002113 if (const Instruction *I = dyn_cast<Instruction>(this)) {
2114 const Function *F = I->getParent() ? I->getParent()->getParent() : 0;
2115 SlotTracker SlotTable(F);
Chris Lattnerd5bace72009-12-31 08:23:09 +00002116 AssemblyWriter W(OS, SlotTable, getModuleFromVal(I), AAW);
Chris Lattnerefb5e392009-12-31 02:23:35 +00002117 W.printInstruction(*I);
Chris Lattner0c19df42008-08-23 22:23:09 +00002118 } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(this)) {
2119 SlotTracker SlotTable(BB->getParent());
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002120 AssemblyWriter W(OS, SlotTable, getModuleFromVal(BB), AAW);
Chris Lattnerefb5e392009-12-31 02:23:35 +00002121 W.printBasicBlock(BB);
Chris Lattner0c19df42008-08-23 22:23:09 +00002122 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(this)) {
2123 SlotTracker SlotTable(GV->getParent());
Dan Gohman6c7a4852009-04-20 16:10:33 +00002124 AssemblyWriter W(OS, SlotTable, GV->getParent(), AAW);
Chris Lattnerefb5e392009-12-31 02:23:35 +00002125 if (const GlobalVariable *V = dyn_cast<GlobalVariable>(GV))
2126 W.printGlobal(V);
2127 else if (const Function *F = dyn_cast<Function>(GV))
2128 W.printFunction(F);
2129 else
2130 W.printAlias(cast<GlobalAlias>(GV));
Devang Patel5546b982009-07-01 20:59:15 +00002131 } else if (const MDNode *N = dyn_cast<MDNode>(this)) {
Victor Hernandeze5f2af72010-01-20 04:45:57 +00002132 const Function *F = N->getFunction();
Victor Hernandez61e6e822010-01-14 01:47:37 +00002133 SlotTracker SlotTable(F);
Dan Gohman15132172010-07-14 21:12:44 +00002134 AssemblyWriter W(OS, SlotTable, F ? F->getParent() : 0, AAW);
Chris Lattnerbddea6a2009-12-31 02:13:35 +00002135 W.printMDNodeBody(N);
Chris Lattner0c19df42008-08-23 22:23:09 +00002136 } else if (const Constant *C = dyn_cast<Constant>(this)) {
Chris Lattner92c5c122009-02-28 23:20:19 +00002137 TypePrinting TypePrinter;
Chris Lattnere101c442009-02-28 21:26:53 +00002138 TypePrinter.print(C->getType(), OS);
Chris Lattner2ee62d22009-02-28 21:11:05 +00002139 OS << ' ';
Dan Gohmana2489d12010-07-20 23:55:01 +00002140 WriteConstantInternal(OS, C, TypePrinter, 0, 0);
Chris Lattner31fcc282009-12-31 01:41:14 +00002141 } else if (isa<InlineAsm>(this) || isa<MDString>(this) ||
2142 isa<Argument>(this)) {
Chris Lattner0c19df42008-08-23 22:23:09 +00002143 WriteAsOperand(OS, this, true, 0);
2144 } else {
Dan Gohmanc0353bf2009-09-23 01:33:16 +00002145 // Otherwise we don't know what it is. Call the virtual function to
2146 // allow a subclass to print itself.
2147 printCustom(OS);
Chris Lattner0c19df42008-08-23 22:23:09 +00002148 }
2149}
2150
Dan Gohmanc0353bf2009-09-23 01:33:16 +00002151// Value::printCustom - subclasses should override this to implement printing.
2152void Value::printCustom(raw_ostream &OS) const {
2153 llvm_unreachable("Unknown value to print out!");
2154}
2155
Chris Lattnerdab942552008-08-25 17:03:15 +00002156// Value::dump - allow easy printing of Values from the debugger.
David Greenec7f9b122010-01-05 01:29:26 +00002157void Value::dump() const { print(dbgs()); dbgs() << '\n'; }
Reid Spencer52641832004-05-25 18:14:38 +00002158
Chris Lattnerdab942552008-08-25 17:03:15 +00002159// Type::dump - allow easy printing of Types from the debugger.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002160void Type::dump() const { print(dbgs()); }
Chris Lattner24025262009-02-28 21:05:51 +00002161
Chris Lattnerdab942552008-08-25 17:03:15 +00002162// Module::dump() - Allow printing of Modules from the debugger.
David Greenec7f9b122010-01-05 01:29:26 +00002163void Module::dump() const { print(dbgs(), 0); }
Bill Wendling3681d112011-12-09 23:18:34 +00002164
2165// NamedMDNode::dump() - Allow printing of NamedMDNodes from the debugger.
2166void NamedMDNode::dump() const { print(dbgs(), 0); }