blob: 7b39efb7c7a0fd878ffa9f36b46c5c23b83f6036 [file] [log] [blame]
Chris Lattner8da78af2002-04-07 22:31:46 +00001//===-- AsmWriter.cpp - Printing LLVM as an assembly file -----------------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
10// This library implements the functionality defined in llvm/Assembly/Writer.h
11//
Chris Lattner02b93992002-04-12 18:21:53 +000012// Note that these routines must be extremely tolerant of various errors in the
Chris Lattner8f77dae2003-05-08 02:44:12 +000013// LLVM code, because it can be used for debugging transformations.
Chris Lattner02b93992002-04-12 18:21:53 +000014//
Chris Lattner00950542001-06-06 20:29:01 +000015//===----------------------------------------------------------------------===//
16
Chris Lattner75cf7cf2002-04-08 22:03:40 +000017#include "llvm/Assembly/Writer.h"
Chris Lattnerf082b802002-07-23 18:07:49 +000018#include "llvm/Assembly/PrintModulePass.h"
Chris Lattner1dbb3872010-09-02 23:09:42 +000019#include "llvm/Assembly/AssemblyAnnotationWriter.h"
Erick Tryzelaarf3d5c912010-03-02 05:32:52 +000020#include "llvm/LLVMContext.h"
Chris Lattnerd5118982005-05-06 20:26:43 +000021#include "llvm/CallingConv.h"
Chris Lattnerf2d577b2004-01-20 19:50:34 +000022#include "llvm/Constants.h"
Chris Lattner3eb59c02002-04-29 18:46:50 +000023#include "llvm/DerivedTypes.h"
Chris Lattner863517a2006-01-25 18:57:27 +000024#include "llvm/InlineAsm.h"
Chris Lattner3990b122009-12-28 23:41:32 +000025#include "llvm/IntrinsicInst.h"
Dan Gohman1224c382009-07-20 21:19:07 +000026#include "llvm/Operator.h"
Chris Lattnerf2d577b2004-01-20 19:50:34 +000027#include "llvm/Module.h"
Reid Spenceref9b9a72007-02-05 20:47:22 +000028#include "llvm/ValueSymbolTable.h"
Chris Lattner1afcace2011-07-09 17:41:24 +000029#include "llvm/ADT/DenseMap.h"
Benjamin Kramer59088392010-01-29 14:42:22 +000030#include "llvm/ADT/SmallString.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000031#include "llvm/ADT/StringExtras.h"
32#include "llvm/ADT/STLExtras.h"
Bill Wendling8f487662006-11-28 02:09:03 +000033#include "llvm/Support/CFG.h"
David Greened865e022010-01-05 01:29:26 +000034#include "llvm/Support/Debug.h"
Devang Patel2d5988d2009-09-30 20:16:54 +000035#include "llvm/Support/Dwarf.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000036#include "llvm/Support/ErrorHandling.h"
Jim Laskeycb6682f2005-08-17 19:34:49 +000037#include "llvm/Support/MathExtras.h"
Dan Gohman683e9222009-08-12 17:23:50 +000038#include "llvm/Support/FormattedStream.h"
Chris Lattner007377f2001-09-07 16:36:04 +000039#include <algorithm>
Reid Spencer4ad513c2007-05-22 19:27:35 +000040#include <cctype>
Chris Lattner31f84992003-11-21 20:23:48 +000041using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000042
Reid Spenceredd5d9e2005-05-15 16:13:11 +000043// Make virtual table appear in this compilation unit.
44AssemblyAnnotationWriter::~AssemblyAnnotationWriter() {}
45
Chris Lattner6ab910b2008-08-19 04:36:02 +000046//===----------------------------------------------------------------------===//
47// Helper Functions
48//===----------------------------------------------------------------------===//
49
50static const Module *getModuleFromVal(const Value *V) {
51 if (const Argument *MA = dyn_cast<Argument>(V))
52 return MA->getParent() ? MA->getParent()->getParent() : 0;
Daniel Dunbara279bc32009-09-20 02:20:51 +000053
Chris Lattner6ab910b2008-08-19 04:36:02 +000054 if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
55 return BB->getParent() ? BB->getParent()->getParent() : 0;
Daniel Dunbara279bc32009-09-20 02:20:51 +000056
Chris Lattner6ab910b2008-08-19 04:36:02 +000057 if (const Instruction *I = dyn_cast<Instruction>(V)) {
58 const Function *M = I->getParent() ? I->getParent()->getParent() : 0;
59 return M ? M->getParent() : 0;
60 }
Andrew Trick18801ec2011-09-30 19:48:58 +000061
Chris Lattner6ab910b2008-08-19 04:36:02 +000062 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
63 return GV->getParent();
64 return 0;
65}
66
Daniel Dunbare9da1332008-10-28 19:33:02 +000067// PrintEscapedString - Print each character of the specified string, escaping
68// it if it is not printable or if it is an escape char.
Chris Lattner8fff1262010-07-07 23:16:37 +000069static void PrintEscapedString(StringRef Name, raw_ostream &Out) {
Daniel Dunbar03d76512009-07-25 23:55:21 +000070 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
71 unsigned char C = Name[i];
Nick Lewycky34a40862009-03-15 06:39:52 +000072 if (isprint(C) && C != '\\' && C != '"')
Daniel Dunbare9da1332008-10-28 19:33:02 +000073 Out << C;
74 else
75 Out << '\\' << hexdigit(C >> 4) << hexdigit(C & 0x0F);
76 }
77}
78
Chris Lattner6ab910b2008-08-19 04:36:02 +000079enum PrefixType {
80 GlobalPrefix,
81 LabelPrefix,
Daniel Dunbarcad35802008-10-14 23:28:09 +000082 LocalPrefix,
83 NoPrefix
Chris Lattner6ab910b2008-08-19 04:36:02 +000084};
85
86/// PrintLLVMName - Turn the specified name into an 'LLVM name', which is either
87/// prefixed with % (if the string only contains simple characters) or is
88/// surrounded with ""'s (if it has special chars in it). Print it out.
Benjamin Kramer38e59892010-07-14 22:38:02 +000089static void PrintLLVMName(raw_ostream &OS, StringRef Name, PrefixType Prefix) {
Jay Foad61717b32011-04-24 14:30:00 +000090 assert(!Name.empty() && "Cannot get empty name!");
Chris Lattner6ab910b2008-08-19 04:36:02 +000091 switch (Prefix) {
Daniel Dunbarcad35802008-10-14 23:28:09 +000092 case NoPrefix: break;
Chris Lattner52b26de2008-08-19 05:16:28 +000093 case GlobalPrefix: OS << '@'; break;
94 case LabelPrefix: break;
95 case LocalPrefix: OS << '%'; break;
Nick Lewycky04234832009-03-19 06:31:22 +000096 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000097
Chris Lattner6ab910b2008-08-19 04:36:02 +000098 // Scan the name to see if it needs quotes first.
Daniel Dunbar03d76512009-07-25 23:55:21 +000099 bool NeedsQuotes = isdigit(Name[0]);
Chris Lattner6ab910b2008-08-19 04:36:02 +0000100 if (!NeedsQuotes) {
Daniel Dunbar03d76512009-07-25 23:55:21 +0000101 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
102 char C = Name[i];
Chris Lattner6ab910b2008-08-19 04:36:02 +0000103 if (!isalnum(C) && C != '-' && C != '.' && C != '_') {
104 NeedsQuotes = true;
105 break;
106 }
107 }
108 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000109
Chris Lattner6ab910b2008-08-19 04:36:02 +0000110 // If we didn't need any quotes, just write out the name in one blast.
111 if (!NeedsQuotes) {
Daniel Dunbar03d76512009-07-25 23:55:21 +0000112 OS << Name;
Chris Lattner6ab910b2008-08-19 04:36:02 +0000113 return;
114 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000115
Chris Lattner6ab910b2008-08-19 04:36:02 +0000116 // Okay, we need quotes. Output the quotes and escape any scary characters as
117 // needed.
118 OS << '"';
Daniel Dunbar03d76512009-07-25 23:55:21 +0000119 PrintEscapedString(Name, OS);
Chris Lattner6ab910b2008-08-19 04:36:02 +0000120 OS << '"';
121}
122
123/// PrintLLVMName - Turn the specified name into an 'LLVM name', which is either
124/// prefixed with % (if the string only contains simple characters) or is
125/// surrounded with ""'s (if it has special chars in it). Print it out.
Dan Gohman1220e102009-08-12 20:56:03 +0000126static void PrintLLVMName(raw_ostream &OS, const Value *V) {
Daniel Dunbara279bc32009-09-20 02:20:51 +0000127 PrintLLVMName(OS, V->getName(),
Chris Lattner6ab910b2008-08-19 04:36:02 +0000128 isa<GlobalValue>(V) ? GlobalPrefix : LocalPrefix);
129}
130
Chris Lattner9cc34462009-02-28 20:25:14 +0000131//===----------------------------------------------------------------------===//
132// TypePrinting Class: Type printing machinery
133//===----------------------------------------------------------------------===//
134
Chris Lattnerfb78b332011-06-18 21:23:04 +0000135/// TypePrinting - Type printing machinery.
136namespace {
137class TypePrinting {
Chris Lattnerfb78b332011-06-18 21:23:04 +0000138 TypePrinting(const TypePrinting &); // DO NOT IMPLEMENT
139 void operator=(const TypePrinting&); // DO NOT IMPLEMENT
140public:
Chris Lattner1afcace2011-07-09 17:41:24 +0000141
142 /// NamedTypes - The named types that are used by the current module.
143 std::vector<StructType*> NamedTypes;
Andrew Trick18801ec2011-09-30 19:48:58 +0000144
Chris Lattner1afcace2011-07-09 17:41:24 +0000145 /// NumberedTypes - The numbered types, along with their value.
146 DenseMap<StructType*, unsigned> NumberedTypes;
Andrew Trick18801ec2011-09-30 19:48:58 +0000147
Chris Lattner1afcace2011-07-09 17:41:24 +0000148
Chris Lattnerfb78b332011-06-18 21:23:04 +0000149 TypePrinting() {}
150 ~TypePrinting() {}
Andrew Trick18801ec2011-09-30 19:48:58 +0000151
Chris Lattner1afcace2011-07-09 17:41:24 +0000152 void incorporateTypes(const Module &M);
Andrew Trick18801ec2011-09-30 19:48:58 +0000153
Chris Lattner1afcace2011-07-09 17:41:24 +0000154 void print(Type *Ty, raw_ostream &OS);
Andrew Trick18801ec2011-09-30 19:48:58 +0000155
Chris Lattner1afcace2011-07-09 17:41:24 +0000156 void printStructBody(StructType *Ty, raw_ostream &OS);
Chris Lattnerfb78b332011-06-18 21:23:04 +0000157};
158} // end anonymous namespace.
Chris Lattnerd8030a72009-02-28 22:34:45 +0000159
Chris Lattner1afcace2011-07-09 17:41:24 +0000160
161void TypePrinting::incorporateTypes(const Module &M) {
162 M.findUsedStructTypes(NamedTypes);
Andrew Trick18801ec2011-09-30 19:48:58 +0000163
Chris Lattner1afcace2011-07-09 17:41:24 +0000164 // The list of struct types we got back includes all the struct types, split
165 // the unnamed ones out to a numbering and remove the anonymous structs.
166 unsigned NextNumber = 0;
Andrew Trick18801ec2011-09-30 19:48:58 +0000167
Chris Lattner1afcace2011-07-09 17:41:24 +0000168 std::vector<StructType*>::iterator NextToUse = NamedTypes.begin(), I, E;
169 for (I = NamedTypes.begin(), E = NamedTypes.end(); I != E; ++I) {
170 StructType *STy = *I;
Andrew Trick18801ec2011-09-30 19:48:58 +0000171
Chris Lattner1afcace2011-07-09 17:41:24 +0000172 // Ignore anonymous types.
Chris Lattnerc4d0e9f2011-08-12 18:07:07 +0000173 if (STy->isLiteral())
Chris Lattner1afcace2011-07-09 17:41:24 +0000174 continue;
Andrew Trick18801ec2011-09-30 19:48:58 +0000175
Chris Lattner1afcace2011-07-09 17:41:24 +0000176 if (STy->getName().empty())
177 NumberedTypes[STy] = NextNumber++;
178 else
179 *NextToUse++ = STy;
180 }
Andrew Trick18801ec2011-09-30 19:48:58 +0000181
Chris Lattner1afcace2011-07-09 17:41:24 +0000182 NamedTypes.erase(NextToUse, NamedTypes.end());
183}
184
185
Chris Lattner534361e2009-02-28 20:49:40 +0000186/// CalcTypeName - Write the specified type to the specified raw_ostream, making
187/// use of type names or up references to shorten the type name where possible.
Chris Lattner1afcace2011-07-09 17:41:24 +0000188void TypePrinting::print(Type *Ty, raw_ostream &OS) {
Chris Lattner9cc34462009-02-28 20:25:14 +0000189 switch (Ty->getTypeID()) {
Chris Lattner30794262009-02-28 21:27:31 +0000190 case Type::VoidTyID: OS << "void"; break;
Dan Gohmance163392011-12-17 00:04:22 +0000191 case Type::HalfTyID: OS << "half"; break;
Chris Lattner30794262009-02-28 21:27:31 +0000192 case Type::FloatTyID: OS << "float"; break;
193 case Type::DoubleTyID: OS << "double"; break;
194 case Type::X86_FP80TyID: OS << "x86_fp80"; break;
195 case Type::FP128TyID: OS << "fp128"; break;
196 case Type::PPC_FP128TyID: OS << "ppc_fp128"; break;
197 case Type::LabelTyID: OS << "label"; break;
Nick Lewycky7a0370f2009-05-30 05:06:04 +0000198 case Type::MetadataTyID: OS << "metadata"; break;
Dale Johannesenbb811a22010-09-10 20:55:01 +0000199 case Type::X86_MMXTyID: OS << "x86_mmx"; break;
Chris Lattner583ffd82009-02-28 21:18:43 +0000200 case Type::IntegerTyID:
Chris Lattner30794262009-02-28 21:27:31 +0000201 OS << 'i' << cast<IntegerType>(Ty)->getBitWidth();
Chris Lattner1afcace2011-07-09 17:41:24 +0000202 return;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000203
Chris Lattner36942d72009-02-28 20:35:42 +0000204 case Type::FunctionTyID: {
Chris Lattner1afcace2011-07-09 17:41:24 +0000205 FunctionType *FTy = cast<FunctionType>(Ty);
206 print(FTy->getReturnType(), OS);
Chris Lattner30794262009-02-28 21:27:31 +0000207 OS << " (";
Chris Lattner36942d72009-02-28 20:35:42 +0000208 for (FunctionType::param_iterator I = FTy->param_begin(),
209 E = FTy->param_end(); I != E; ++I) {
210 if (I != FTy->param_begin())
Chris Lattner30794262009-02-28 21:27:31 +0000211 OS << ", ";
Chris Lattner1afcace2011-07-09 17:41:24 +0000212 print(*I, OS);
Chris Lattner9cc34462009-02-28 20:25:14 +0000213 }
Chris Lattner36942d72009-02-28 20:35:42 +0000214 if (FTy->isVarArg()) {
Chris Lattner30794262009-02-28 21:27:31 +0000215 if (FTy->getNumParams()) OS << ", ";
216 OS << "...";
Chris Lattner9cc34462009-02-28 20:25:14 +0000217 }
Chris Lattner30794262009-02-28 21:27:31 +0000218 OS << ')';
Chris Lattner1afcace2011-07-09 17:41:24 +0000219 return;
Chris Lattner36942d72009-02-28 20:35:42 +0000220 }
221 case Type::StructTyID: {
Chris Lattner1afcace2011-07-09 17:41:24 +0000222 StructType *STy = cast<StructType>(Ty);
Andrew Trick18801ec2011-09-30 19:48:58 +0000223
Chris Lattnerc4d0e9f2011-08-12 18:07:07 +0000224 if (STy->isLiteral())
Chris Lattner1afcace2011-07-09 17:41:24 +0000225 return printStructBody(STy, OS);
226
227 if (!STy->getName().empty())
228 return PrintLLVMName(OS, STy->getName(), LocalPrefix);
Andrew Trick18801ec2011-09-30 19:48:58 +0000229
Chris Lattner1afcace2011-07-09 17:41:24 +0000230 DenseMap<StructType*, unsigned>::iterator I = NumberedTypes.find(STy);
231 if (I != NumberedTypes.end())
232 OS << '%' << I->second;
233 else // Not enumerated, print the hex address.
Benjamin Kramer5a832642011-11-02 17:24:36 +0000234 OS << "%\"type " << STy << '\"';
Chris Lattner1afcace2011-07-09 17:41:24 +0000235 return;
Chris Lattner36942d72009-02-28 20:35:42 +0000236 }
237 case Type::PointerTyID: {
Chris Lattner1afcace2011-07-09 17:41:24 +0000238 PointerType *PTy = cast<PointerType>(Ty);
239 print(PTy->getElementType(), OS);
Chris Lattner36942d72009-02-28 20:35:42 +0000240 if (unsigned AddressSpace = PTy->getAddressSpace())
Chris Lattner30794262009-02-28 21:27:31 +0000241 OS << " addrspace(" << AddressSpace << ')';
242 OS << '*';
Chris Lattner1afcace2011-07-09 17:41:24 +0000243 return;
Chris Lattner36942d72009-02-28 20:35:42 +0000244 }
245 case Type::ArrayTyID: {
Chris Lattner1afcace2011-07-09 17:41:24 +0000246 ArrayType *ATy = cast<ArrayType>(Ty);
Chris Lattner30794262009-02-28 21:27:31 +0000247 OS << '[' << ATy->getNumElements() << " x ";
Chris Lattner1afcace2011-07-09 17:41:24 +0000248 print(ATy->getElementType(), OS);
Chris Lattner30794262009-02-28 21:27:31 +0000249 OS << ']';
Chris Lattner1afcace2011-07-09 17:41:24 +0000250 return;
Chris Lattner36942d72009-02-28 20:35:42 +0000251 }
252 case Type::VectorTyID: {
Chris Lattner1afcace2011-07-09 17:41:24 +0000253 VectorType *PTy = cast<VectorType>(Ty);
Chris Lattner30794262009-02-28 21:27:31 +0000254 OS << "<" << PTy->getNumElements() << " x ";
Chris Lattner1afcace2011-07-09 17:41:24 +0000255 print(PTy->getElementType(), OS);
Chris Lattner30794262009-02-28 21:27:31 +0000256 OS << '>';
Chris Lattner1afcace2011-07-09 17:41:24 +0000257 return;
Chris Lattner36942d72009-02-28 20:35:42 +0000258 }
Chris Lattner36942d72009-02-28 20:35:42 +0000259 default:
Chris Lattner30794262009-02-28 21:27:31 +0000260 OS << "<unrecognized-type>";
Chris Lattner1afcace2011-07-09 17:41:24 +0000261 return;
Chris Lattner9cc34462009-02-28 20:25:14 +0000262 }
Chris Lattner9cc34462009-02-28 20:25:14 +0000263}
264
Chris Lattner1afcace2011-07-09 17:41:24 +0000265void TypePrinting::printStructBody(StructType *STy, raw_ostream &OS) {
266 if (STy->isOpaque()) {
267 OS << "opaque";
268 return;
Chris Lattner9cc34462009-02-28 20:25:14 +0000269 }
Andrew Trick18801ec2011-09-30 19:48:58 +0000270
Chris Lattner1afcace2011-07-09 17:41:24 +0000271 if (STy->isPacked())
272 OS << '<';
Andrew Trick18801ec2011-09-30 19:48:58 +0000273
Chris Lattner1afcace2011-07-09 17:41:24 +0000274 if (STy->getNumElements() == 0) {
275 OS << "{}";
276 } else {
277 StructType::element_iterator I = STy->element_begin();
278 OS << "{ ";
279 print(*I++, OS);
280 for (StructType::element_iterator E = STy->element_end(); I != E; ++I) {
281 OS << ", ";
282 print(*I, OS);
Chris Lattner413fd232009-03-01 00:03:38 +0000283 }
Andrew Trick18801ec2011-09-30 19:48:58 +0000284
Chris Lattner1afcace2011-07-09 17:41:24 +0000285 OS << " }";
Chris Lattnere9fa33e2009-02-28 23:20:19 +0000286 }
Chris Lattner1afcace2011-07-09 17:41:24 +0000287 if (STy->isPacked())
288 OS << '>';
Chris Lattnere9fa33e2009-02-28 23:20:19 +0000289}
290
Chris Lattner9cc34462009-02-28 20:25:14 +0000291
Chris Lattner1afcace2011-07-09 17:41:24 +0000292
Chris Lattner6ab910b2008-08-19 04:36:02 +0000293//===----------------------------------------------------------------------===//
294// SlotTracker Class: Enumerate slot numbers for unnamed values
295//===----------------------------------------------------------------------===//
296
Chris Lattnerb64871a2008-08-19 04:28:07 +0000297namespace {
298
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000299/// This class provides computation of slot numbers for LLVM Assembly writing.
Chris Lattner45d4c732008-08-17 04:17:45 +0000300///
Chris Lattner0d9574a2008-08-19 04:26:57 +0000301class SlotTracker {
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000302public:
Devang Patel320671d2009-07-08 21:44:25 +0000303 /// ValueMap - A mapping of Values to slot numbers.
Chris Lattner92255072008-08-17 17:25:25 +0000304 typedef DenseMap<const Value*, unsigned> ValueMap;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000305
306private:
Devang Patel320671d2009-07-08 21:44:25 +0000307 /// TheModule - The module for which we are holding slot numbers.
Chris Lattner45d4c732008-08-17 04:17:45 +0000308 const Module* TheModule;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000309
Devang Patel320671d2009-07-08 21:44:25 +0000310 /// TheFunction - The function for which we are holding slot numbers.
Chris Lattner45d4c732008-08-17 04:17:45 +0000311 const Function* TheFunction;
312 bool FunctionProcessed;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000313
Jay Foad7f0ce342011-07-11 07:28:49 +0000314 /// mMap - The slot map for the module level data.
Chris Lattner45d4c732008-08-17 04:17:45 +0000315 ValueMap mMap;
316 unsigned mNext;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000317
Jay Foad7f0ce342011-07-11 07:28:49 +0000318 /// fMap - The slot map for the function level data.
Chris Lattner45d4c732008-08-17 04:17:45 +0000319 ValueMap fMap;
320 unsigned fNext;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000321
Devang Patel320671d2009-07-08 21:44:25 +0000322 /// mdnMap - Map for MDNodes.
Chris Lattner307c9892009-12-31 02:20:11 +0000323 DenseMap<const MDNode*, unsigned> mdnMap;
Devang Patel320671d2009-07-08 21:44:25 +0000324 unsigned mdnNext;
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000325public:
Chris Lattner45d4c732008-08-17 04:17:45 +0000326 /// Construct from a module
Chris Lattner0d9574a2008-08-19 04:26:57 +0000327 explicit SlotTracker(const Module *M);
Chris Lattner45d4c732008-08-17 04:17:45 +0000328 /// Construct from a function, starting out in incorp state.
Chris Lattner0d9574a2008-08-19 04:26:57 +0000329 explicit SlotTracker(const Function *F);
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000330
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000331 /// Return the slot number of the specified value in it's type
Chris Lattner0d9574a2008-08-19 04:26:57 +0000332 /// plane. If something is not in the SlotTracker, return -1.
Chris Lattner22379bc2007-01-11 03:54:27 +0000333 int getLocalSlot(const Value *V);
334 int getGlobalSlot(const GlobalValue *V);
Devang Patel320671d2009-07-08 21:44:25 +0000335 int getMetadataSlot(const MDNode *N);
Reid Spencerfc621e22004-06-09 15:26:53 +0000336
Misha Brukmanfd939082005-04-21 23:48:37 +0000337 /// If you'd like to deal with a function instead of just a module, use
Chris Lattner0d9574a2008-08-19 04:26:57 +0000338 /// this method to get its data into the SlotTracker.
Misha Brukmanfd939082005-04-21 23:48:37 +0000339 void incorporateFunction(const Function *F) {
340 TheFunction = F;
Reid Spencer28531c72004-08-16 07:46:33 +0000341 FunctionProcessed = false;
342 }
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000343
Misha Brukmanfd939082005-04-21 23:48:37 +0000344 /// After calling incorporateFunction, use this method to remove the
Chris Lattner0d9574a2008-08-19 04:26:57 +0000345 /// most recently incorporated function from the SlotTracker. This
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000346 /// will reset the state of the machine back to just the module contents.
347 void purgeFunction();
348
Devang Patel320671d2009-07-08 21:44:25 +0000349 /// MDNode map iterators.
Chris Lattner307c9892009-12-31 02:20:11 +0000350 typedef DenseMap<const MDNode*, unsigned>::iterator mdn_iterator;
351 mdn_iterator mdn_begin() { return mdnMap.begin(); }
352 mdn_iterator mdn_end() { return mdnMap.end(); }
353 unsigned mdn_size() const { return mdnMap.size(); }
354 bool mdn_empty() const { return mdnMap.empty(); }
Devang Patel320671d2009-07-08 21:44:25 +0000355
Reid Spencerb03de0c2004-05-26 21:56:09 +0000356 /// This function does the actual initialization.
357 inline void initialize();
358
Devang Patel320671d2009-07-08 21:44:25 +0000359 // Implementation Details
360private:
Chris Lattner9446bbe2007-01-09 07:55:49 +0000361 /// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
362 void CreateModuleSlot(const GlobalValue *V);
Devang Patel320671d2009-07-08 21:44:25 +0000363
364 /// CreateMetadataSlot - Insert the specified MDNode* into the slot table.
365 void CreateMetadataSlot(const MDNode *N);
366
Chris Lattner9446bbe2007-01-09 07:55:49 +0000367 /// CreateFunctionSlot - Insert the specified Value* into the slot table.
368 void CreateFunctionSlot(const Value *V);
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000369
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000370 /// Add all of the module level global variables (and their initializers)
371 /// and function declarations, but not the contents of those functions.
372 void processModule();
373
Devang Patel320671d2009-07-08 21:44:25 +0000374 /// Add all of the functions arguments, basic blocks, and instructions.
Reid Spencerb03de0c2004-05-26 21:56:09 +0000375 void processFunction();
376
Chris Lattner0d9574a2008-08-19 04:26:57 +0000377 SlotTracker(const SlotTracker &); // DO NOT IMPLEMENT
378 void operator=(const SlotTracker &); // DO NOT IMPLEMENT
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000379};
380
Chris Lattnerb64871a2008-08-19 04:28:07 +0000381} // end anonymous namespace
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000382
Chris Lattner6ab910b2008-08-19 04:36:02 +0000383
384static SlotTracker *createSlotTracker(const Value *V) {
385 if (const Argument *FA = dyn_cast<Argument>(V))
386 return new SlotTracker(FA->getParent());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000387
Chris Lattner6ab910b2008-08-19 04:36:02 +0000388 if (const Instruction *I = dyn_cast<Instruction>(V))
Andrew Trick62e05902011-09-30 19:50:40 +0000389 if (I->getParent())
390 return new SlotTracker(I->getParent()->getParent());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000391
Chris Lattner6ab910b2008-08-19 04:36:02 +0000392 if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
393 return new SlotTracker(BB->getParent());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000394
Chris Lattner6ab910b2008-08-19 04:36:02 +0000395 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
396 return new SlotTracker(GV->getParent());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000397
Chris Lattner6ab910b2008-08-19 04:36:02 +0000398 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V))
Daniel Dunbara279bc32009-09-20 02:20:51 +0000399 return new SlotTracker(GA->getParent());
400
Chris Lattner6ab910b2008-08-19 04:36:02 +0000401 if (const Function *Func = dyn_cast<Function>(V))
402 return new SlotTracker(Func);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000403
Dan Gohman3bdfbf52010-07-20 23:55:01 +0000404 if (const MDNode *MD = dyn_cast<MDNode>(V)) {
405 if (!MD->isFunctionLocal())
406 return new SlotTracker(MD->getFunction());
407
Dale Johannesen5f72a5e2010-01-13 00:00:24 +0000408 return new SlotTracker((Function *)0);
Dan Gohman3bdfbf52010-07-20 23:55:01 +0000409 }
Dale Johannesen5f72a5e2010-01-13 00:00:24 +0000410
Chris Lattner6ab910b2008-08-19 04:36:02 +0000411 return 0;
412}
413
414#if 0
David Greened865e022010-01-05 01:29:26 +0000415#define ST_DEBUG(X) dbgs() << X
Chris Lattner6ab910b2008-08-19 04:36:02 +0000416#else
Chris Lattner24233032008-08-19 04:47:09 +0000417#define ST_DEBUG(X)
Chris Lattner6ab910b2008-08-19 04:36:02 +0000418#endif
419
420// Module level constructor. Causes the contents of the Module (sans functions)
421// to be added to the slot table.
422SlotTracker::SlotTracker(const Module *M)
Andrew Trick18801ec2011-09-30 19:48:58 +0000423 : TheModule(M), TheFunction(0), FunctionProcessed(false),
Chris Lattner38cf02e2009-12-31 01:36:50 +0000424 mNext(0), fNext(0), mdnNext(0) {
Chris Lattner6ab910b2008-08-19 04:36:02 +0000425}
426
427// Function level constructor. Causes the contents of the Module and the one
428// function provided to be added to the slot table.
429SlotTracker::SlotTracker(const Function *F)
Chris Lattnercfb5a202008-08-19 05:06:27 +0000430 : TheModule(F ? F->getParent() : 0), TheFunction(F), FunctionProcessed(false),
Chris Lattner6e6b1802009-12-31 02:13:35 +0000431 mNext(0), fNext(0), mdnNext(0) {
Chris Lattner6ab910b2008-08-19 04:36:02 +0000432}
433
434inline void SlotTracker::initialize() {
435 if (TheModule) {
436 processModule();
437 TheModule = 0; ///< Prevent re-processing next time we're called.
438 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000439
Chris Lattner6ab910b2008-08-19 04:36:02 +0000440 if (TheFunction && !FunctionProcessed)
441 processFunction();
442}
443
444// Iterate through all the global variables, functions, and global
445// variable initializers and create slots for them.
446void SlotTracker::processModule() {
Chris Lattner24233032008-08-19 04:47:09 +0000447 ST_DEBUG("begin processModule!\n");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000448
Chris Lattner6ab910b2008-08-19 04:36:02 +0000449 // Add all of the unnamed global variables to the value table.
450 for (Module::const_global_iterator I = TheModule->global_begin(),
Devang Patel320671d2009-07-08 21:44:25 +0000451 E = TheModule->global_end(); I != E; ++I) {
Daniel Dunbara279bc32009-09-20 02:20:51 +0000452 if (!I->hasName())
Chris Lattner6ab910b2008-08-19 04:36:02 +0000453 CreateModuleSlot(I);
Devang Patel320671d2009-07-08 21:44:25 +0000454 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000455
Devang Patel37c4a2d2009-07-29 22:04:47 +0000456 // Add metadata used by named metadata.
Daniel Dunbara279bc32009-09-20 02:20:51 +0000457 for (Module::const_named_metadata_iterator
Devang Patel37c4a2d2009-07-29 22:04:47 +0000458 I = TheModule->named_metadata_begin(),
459 E = TheModule->named_metadata_end(); I != E; ++I) {
460 const NamedMDNode *NMD = I;
Dan Gohman872814a2010-07-21 18:54:18 +0000461 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
462 CreateMetadataSlot(NMD->getOperand(i));
Devang Patel37c4a2d2009-07-29 22:04:47 +0000463 }
464
Chris Lattner6ab910b2008-08-19 04:36:02 +0000465 // Add all the unnamed functions to the table.
466 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
467 I != E; ++I)
468 if (!I->hasName())
469 CreateModuleSlot(I);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000470
Chris Lattner24233032008-08-19 04:47:09 +0000471 ST_DEBUG("end processModule!\n");
Chris Lattner6ab910b2008-08-19 04:36:02 +0000472}
473
Chris Lattner6ab910b2008-08-19 04:36:02 +0000474// Process the arguments, basic blocks, and instructions of a function.
475void SlotTracker::processFunction() {
Chris Lattner24233032008-08-19 04:47:09 +0000476 ST_DEBUG("begin processFunction!\n");
Chris Lattner6ab910b2008-08-19 04:36:02 +0000477 fNext = 0;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000478
Chris Lattner6ab910b2008-08-19 04:36:02 +0000479 // Add all the function arguments with no names.
480 for(Function::const_arg_iterator AI = TheFunction->arg_begin(),
481 AE = TheFunction->arg_end(); AI != AE; ++AI)
482 if (!AI->hasName())
483 CreateFunctionSlot(AI);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000484
Chris Lattner24233032008-08-19 04:47:09 +0000485 ST_DEBUG("Inserting Instructions:\n");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000486
Chris Lattner6e6b1802009-12-31 02:13:35 +0000487 SmallVector<std::pair<unsigned, MDNode*>, 4> MDForInst;
Devang Patel43215782009-09-16 20:21:17 +0000488
Chris Lattner6ab910b2008-08-19 04:36:02 +0000489 // Add all of the basic blocks and instructions with no names.
490 for (Function::const_iterator BB = TheFunction->begin(),
491 E = TheFunction->end(); BB != E; ++BB) {
492 if (!BB->hasName())
493 CreateFunctionSlot(BB);
Andrew Trick18801ec2011-09-30 19:48:58 +0000494
Daniel Dunbara279bc32009-09-20 02:20:51 +0000495 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E;
Devang Patel320671d2009-07-08 21:44:25 +0000496 ++I) {
Chris Lattner3990b122009-12-28 23:41:32 +0000497 if (!I->getType()->isVoidTy() && !I->hasName())
Chris Lattner6ab910b2008-08-19 04:36:02 +0000498 CreateFunctionSlot(I);
Andrew Trick18801ec2011-09-30 19:48:58 +0000499
Chris Lattnerfd450c02010-05-10 20:53:17 +0000500 // Intrinsics can directly use metadata. We allow direct calls to any
501 // llvm.foo function here, because the target may not be linked into the
502 // optimizer.
503 if (const CallInst *CI = dyn_cast<CallInst>(I)) {
504 if (Function *F = CI->getCalledFunction())
505 if (F->getName().startswith("llvm."))
506 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
507 if (MDNode *N = dyn_cast_or_null<MDNode>(I->getOperand(i)))
508 CreateMetadataSlot(N);
509 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000510
Devang Patel43215782009-09-16 20:21:17 +0000511 // Process metadata attached with this instruction.
Chris Lattner3990b122009-12-28 23:41:32 +0000512 I->getAllMetadata(MDForInst);
513 for (unsigned i = 0, e = MDForInst.size(); i != e; ++i)
514 CreateMetadataSlot(MDForInst[i].second);
Chris Lattner6e6b1802009-12-31 02:13:35 +0000515 MDForInst.clear();
Devang Patel320671d2009-07-08 21:44:25 +0000516 }
Chris Lattner6ab910b2008-08-19 04:36:02 +0000517 }
Devang Patel43215782009-09-16 20:21:17 +0000518
Chris Lattner6ab910b2008-08-19 04:36:02 +0000519 FunctionProcessed = true;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000520
Chris Lattner24233032008-08-19 04:47:09 +0000521 ST_DEBUG("end processFunction!\n");
Chris Lattner6ab910b2008-08-19 04:36:02 +0000522}
523
524/// Clean up after incorporating a function. This is the only way to get out of
525/// the function incorporation state that affects get*Slot/Create*Slot. Function
526/// incorporation state is indicated by TheFunction != 0.
527void SlotTracker::purgeFunction() {
Chris Lattner24233032008-08-19 04:47:09 +0000528 ST_DEBUG("begin purgeFunction!\n");
Chris Lattner6ab910b2008-08-19 04:36:02 +0000529 fMap.clear(); // Simply discard the function level map
530 TheFunction = 0;
531 FunctionProcessed = false;
Chris Lattner24233032008-08-19 04:47:09 +0000532 ST_DEBUG("end purgeFunction!\n");
Chris Lattner6ab910b2008-08-19 04:36:02 +0000533}
534
535/// getGlobalSlot - Get the slot number of a global value.
536int SlotTracker::getGlobalSlot(const GlobalValue *V) {
537 // Check for uninitialized state and do lazy initialization.
538 initialize();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000539
Jay Foad7f0ce342011-07-11 07:28:49 +0000540 // Find the value in the module map
Chris Lattner6ab910b2008-08-19 04:36:02 +0000541 ValueMap::iterator MI = mMap.find(V);
Dan Gohmanaeaf2452008-10-01 19:58:59 +0000542 return MI == mMap.end() ? -1 : (int)MI->second;
Chris Lattner6ab910b2008-08-19 04:36:02 +0000543}
544
Chris Lattner307c9892009-12-31 02:20:11 +0000545/// getMetadataSlot - Get the slot number of a MDNode.
Devang Patel320671d2009-07-08 21:44:25 +0000546int SlotTracker::getMetadataSlot(const MDNode *N) {
547 // Check for uninitialized state and do lazy initialization.
548 initialize();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000549
Jay Foad7f0ce342011-07-11 07:28:49 +0000550 // Find the MDNode in the module map
Chris Lattner307c9892009-12-31 02:20:11 +0000551 mdn_iterator MI = mdnMap.find(N);
Devang Patel320671d2009-07-08 21:44:25 +0000552 return MI == mdnMap.end() ? -1 : (int)MI->second;
553}
554
Chris Lattner6ab910b2008-08-19 04:36:02 +0000555
556/// getLocalSlot - Get the slot number for a value that is local to a function.
557int SlotTracker::getLocalSlot(const Value *V) {
558 assert(!isa<Constant>(V) && "Can't get a constant or global slot with this!");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000559
Chris Lattner6ab910b2008-08-19 04:36:02 +0000560 // Check for uninitialized state and do lazy initialization.
561 initialize();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000562
Chris Lattner6ab910b2008-08-19 04:36:02 +0000563 ValueMap::iterator FI = fMap.find(V);
Dan Gohmanaeaf2452008-10-01 19:58:59 +0000564 return FI == fMap.end() ? -1 : (int)FI->second;
Chris Lattner6ab910b2008-08-19 04:36:02 +0000565}
566
567
568/// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
569void SlotTracker::CreateModuleSlot(const GlobalValue *V) {
570 assert(V && "Can't insert a null Value into SlotTracker!");
Chris Lattner4ee93c42009-12-29 07:25:48 +0000571 assert(!V->getType()->isVoidTy() && "Doesn't need a slot!");
Chris Lattner6ab910b2008-08-19 04:36:02 +0000572 assert(!V->hasName() && "Doesn't need a slot!");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000573
Chris Lattner6ab910b2008-08-19 04:36:02 +0000574 unsigned DestSlot = mNext++;
575 mMap[V] = DestSlot;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000576
Chris Lattner24233032008-08-19 04:47:09 +0000577 ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" <<
Chris Lattner6ab910b2008-08-19 04:36:02 +0000578 DestSlot << " [");
579 // G = Global, F = Function, A = Alias, o = other
Chris Lattner24233032008-08-19 04:47:09 +0000580 ST_DEBUG((isa<GlobalVariable>(V) ? 'G' :
Chris Lattner6ab910b2008-08-19 04:36:02 +0000581 (isa<Function>(V) ? 'F' :
582 (isa<GlobalAlias>(V) ? 'A' : 'o'))) << "]\n");
583}
584
Chris Lattner6ab910b2008-08-19 04:36:02 +0000585/// CreateSlot - Create a new slot for the specified value if it has no name.
586void SlotTracker::CreateFunctionSlot(const Value *V) {
Chris Lattner4ee93c42009-12-29 07:25:48 +0000587 assert(!V->getType()->isVoidTy() && !V->hasName() && "Doesn't need a slot!");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000588
Chris Lattner6ab910b2008-08-19 04:36:02 +0000589 unsigned DestSlot = fNext++;
590 fMap[V] = DestSlot;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000591
Chris Lattner6ab910b2008-08-19 04:36:02 +0000592 // G = Global, F = Function, o = other
Chris Lattner24233032008-08-19 04:47:09 +0000593 ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" <<
Chris Lattner6ab910b2008-08-19 04:36:02 +0000594 DestSlot << " [o]\n");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000595}
Chris Lattner6ab910b2008-08-19 04:36:02 +0000596
Devang Patel320671d2009-07-08 21:44:25 +0000597/// CreateModuleSlot - Insert the specified MDNode* into the slot table.
598void SlotTracker::CreateMetadataSlot(const MDNode *N) {
599 assert(N && "Can't insert a null Value into SlotTracker!");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000600
Chris Lattner2b4b1e22009-12-31 02:27:30 +0000601 // Don't insert if N is a function-local metadata, these are always printed
602 // inline.
Dan Gohman3bdfbf52010-07-20 23:55:01 +0000603 if (!N->isFunctionLocal()) {
604 mdn_iterator I = mdnMap.find(N);
605 if (I != mdnMap.end())
606 return;
Victor Hernandezff7707e2009-12-04 20:07:10 +0000607
Dan Gohman3bdfbf52010-07-20 23:55:01 +0000608 unsigned DestSlot = mdnNext++;
609 mdnMap[N] = DestSlot;
610 }
Devang Patel320671d2009-07-08 21:44:25 +0000611
Chris Lattner2b4b1e22009-12-31 02:27:30 +0000612 // Recursively add any MDNodes referenced by operands.
613 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
614 if (const MDNode *Op = dyn_cast_or_null<MDNode>(N->getOperand(i)))
615 CreateMetadataSlot(Op);
Devang Patel320671d2009-07-08 21:44:25 +0000616}
Chris Lattner6ab910b2008-08-19 04:36:02 +0000617
618//===----------------------------------------------------------------------===//
619// AsmWriter Implementation
620//===----------------------------------------------------------------------===//
Chris Lattnerf082b802002-07-23 18:07:49 +0000621
Dan Gohman1220e102009-08-12 20:56:03 +0000622static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
Dan Gohmand6c0f652009-08-13 15:27:57 +0000623 TypePrinting *TypePrinter,
Dan Gohman3bdfbf52010-07-20 23:55:01 +0000624 SlotTracker *Machine,
625 const Module *Context);
Reid Spencer0e25e1c2004-07-04 11:50:43 +0000626
Chris Lattnerc97536e2008-08-17 04:40:13 +0000627
Chris Lattner207b5bc2001-10-29 16:37:48 +0000628
Chris Lattner82c4bc72006-12-06 06:40:49 +0000629static const char *getPredicateText(unsigned predicate) {
Reid Spencer81dfeb32006-12-04 05:19:18 +0000630 const char * pred = "unknown";
631 switch (predicate) {
Chris Lattner6e6b1802009-12-31 02:13:35 +0000632 case FCmpInst::FCMP_FALSE: pred = "false"; break;
633 case FCmpInst::FCMP_OEQ: pred = "oeq"; break;
634 case FCmpInst::FCMP_OGT: pred = "ogt"; break;
635 case FCmpInst::FCMP_OGE: pred = "oge"; break;
636 case FCmpInst::FCMP_OLT: pred = "olt"; break;
637 case FCmpInst::FCMP_OLE: pred = "ole"; break;
638 case FCmpInst::FCMP_ONE: pred = "one"; break;
639 case FCmpInst::FCMP_ORD: pred = "ord"; break;
640 case FCmpInst::FCMP_UNO: pred = "uno"; break;
641 case FCmpInst::FCMP_UEQ: pred = "ueq"; break;
642 case FCmpInst::FCMP_UGT: pred = "ugt"; break;
643 case FCmpInst::FCMP_UGE: pred = "uge"; break;
644 case FCmpInst::FCMP_ULT: pred = "ult"; break;
645 case FCmpInst::FCMP_ULE: pred = "ule"; break;
646 case FCmpInst::FCMP_UNE: pred = "une"; break;
647 case FCmpInst::FCMP_TRUE: pred = "true"; break;
648 case ICmpInst::ICMP_EQ: pred = "eq"; break;
649 case ICmpInst::ICMP_NE: pred = "ne"; break;
650 case ICmpInst::ICMP_SGT: pred = "sgt"; break;
651 case ICmpInst::ICMP_SGE: pred = "sge"; break;
652 case ICmpInst::ICMP_SLT: pred = "slt"; break;
653 case ICmpInst::ICMP_SLE: pred = "sle"; break;
654 case ICmpInst::ICMP_UGT: pred = "ugt"; break;
655 case ICmpInst::ICMP_UGE: pred = "uge"; break;
656 case ICmpInst::ICMP_ULT: pred = "ult"; break;
657 case ICmpInst::ICMP_ULE: pred = "ule"; break;
Reid Spencer81dfeb32006-12-04 05:19:18 +0000658 }
659 return pred;
660}
661
Eli Friedmanff030482011-07-28 21:48:00 +0000662static void writeAtomicRMWOperation(raw_ostream &Out,
663 AtomicRMWInst::BinOp Op) {
664 switch (Op) {
665 default: Out << " <unknown operation " << Op << ">"; break;
666 case AtomicRMWInst::Xchg: Out << " xchg"; break;
667 case AtomicRMWInst::Add: Out << " add"; break;
668 case AtomicRMWInst::Sub: Out << " sub"; break;
669 case AtomicRMWInst::And: Out << " and"; break;
670 case AtomicRMWInst::Nand: Out << " nand"; break;
671 case AtomicRMWInst::Or: Out << " or"; break;
672 case AtomicRMWInst::Xor: Out << " xor"; break;
673 case AtomicRMWInst::Max: Out << " max"; break;
674 case AtomicRMWInst::Min: Out << " min"; break;
675 case AtomicRMWInst::UMax: Out << " umax"; break;
676 case AtomicRMWInst::UMin: Out << " umin"; break;
677 }
678}
Devang Patel320671d2009-07-08 21:44:25 +0000679
Dan Gohman1220e102009-08-12 20:56:03 +0000680static void WriteOptimizationInfo(raw_ostream &Out, const User *U) {
Dan Gohman1224c382009-07-20 21:19:07 +0000681 if (const OverflowingBinaryOperator *OBO =
682 dyn_cast<OverflowingBinaryOperator>(U)) {
Dan Gohman5078f842009-08-20 17:11:38 +0000683 if (OBO->hasNoUnsignedWrap())
Dan Gohman59858cf2009-07-27 16:11:46 +0000684 Out << " nuw";
Dan Gohman5078f842009-08-20 17:11:38 +0000685 if (OBO->hasNoSignedWrap())
Dan Gohman59858cf2009-07-27 16:11:46 +0000686 Out << " nsw";
Chris Lattner35bda892011-02-06 21:44:57 +0000687 } else if (const PossiblyExactOperator *Div =
688 dyn_cast<PossiblyExactOperator>(U)) {
Dan Gohman1224c382009-07-20 21:19:07 +0000689 if (Div->isExact())
Dan Gohman59858cf2009-07-27 16:11:46 +0000690 Out << " exact";
Dan Gohmandd8004d2009-07-27 21:53:46 +0000691 } else if (const GEPOperator *GEP = dyn_cast<GEPOperator>(U)) {
692 if (GEP->isInBounds())
693 Out << " inbounds";
Dan Gohman1224c382009-07-20 21:19:07 +0000694 }
695}
696
Dan Gohman40cf12f2010-07-14 20:57:55 +0000697static void WriteConstantInternal(raw_ostream &Out, const Constant *CV,
698 TypePrinting &TypePrinter,
Dan Gohman3bdfbf52010-07-20 23:55:01 +0000699 SlotTracker *Machine,
700 const Module *Context) {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +0000701 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000702 if (CI->getType()->isIntegerTy(1)) {
Reid Spencer579dca12007-01-12 04:24:46 +0000703 Out << (CI->getZExtValue() ? "true" : "false");
Chris Lattnerfad86b02008-08-17 07:19:36 +0000704 return;
705 }
706 Out << CI->getValue();
707 return;
708 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000709
Chris Lattnerfad86b02008-08-17 07:19:36 +0000710 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Dan Gohmance163392011-12-17 00:04:22 +0000711 if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEhalf ||
712 &CFP->getValueAPF().getSemantics() == &APFloat::IEEEsingle ||
713 &CFP->getValueAPF().getSemantics() == &APFloat::IEEEdouble) {
Dale Johannesen9d5f4562007-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 Johannesen541ed9f2009-01-21 20:32:55 +0000719 bool ignored;
Dan Gohmance163392011-12-17 00:04:22 +0000720 bool isHalf = &CFP->getValueAPF().getSemantics()==&APFloat::IEEEhalf;
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000721 bool isDouble = &CFP->getValueAPF().getSemantics()==&APFloat::IEEEdouble;
NAKAMURA Takumi22bed5d2012-02-16 08:12:24 +0000722 bool isInf = CFP->getValueAPF().isInfinity();
723 bool isNaN = CFP->getValueAPF().isNaN();
724 if (!isHalf && !isInf && !isNaN) {
Dan Gohmance163392011-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 Lattner66e810b2002-04-18 18:53:13 +0000729
Dan Gohmance163392011-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 Takumic8782a12012-02-16 04:19:15 +0000738 if (APFloat(APFloat::IEEEdouble, StrVal).convertToDouble() == Val) {
Dan Gohmance163392011-12-17 00:04:22 +0000739 Out << StrVal.str();
740 return;
741 }
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000742 }
Chris Lattner66e810b2002-04-18 18:53:13 +0000743 }
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000744 // Otherwise we could not reparse it to exactly the same value, so we must
Dale Johannesen541ed9f2009-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 Johannesen9d5f4562007-09-12 03:30:33 +0000748 assert(sizeof(double) == sizeof(uint64_t) &&
749 "assuming that double is 64 bits!");
Chris Lattnerc6a13462008-11-10 04:30:26 +0000750 char Buffer[40];
Dale Johannesen541ed9f2009-01-21 20:32:55 +0000751 APFloat apf = CFP->getValueAPF();
Dan Gohmance163392011-12-17 00:04:22 +0000752 // Halves and floats are represented in ASCII IR as double, convert.
Dale Johannesen541ed9f2009-01-21 20:32:55 +0000753 if (!isDouble)
Daniel Dunbara279bc32009-09-20 02:20:51 +0000754 apf.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven,
Dale Johannesen541ed9f2009-01-21 20:32:55 +0000755 &ignored);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000756 Out << "0x" <<
757 utohex_buffer(uint64_t(apf.bitcastToAPInt().getZExtValue()),
Dale Johannesen541ed9f2009-01-21 20:32:55 +0000758 Buffer+40);
Chris Lattnercfb5a202008-08-19 05:06:27 +0000759 return;
760 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000761
Chris Lattnercfb5a202008-08-19 05:06:27 +0000762 // Some form of long double. These appear as a magic letter identifying
763 // the type, then a fixed number of hex digits.
764 Out << "0x";
Dale Johannesen1b25cb22009-03-23 21:16:53 +0000765 if (&CFP->getValueAPF().getSemantics() == &APFloat::x87DoubleExtended) {
Chris Lattnercfb5a202008-08-19 05:06:27 +0000766 Out << 'K';
Dale Johannesen1b25cb22009-03-23 21:16:53 +0000767 // api needed to prevent premature destruction
768 APInt api = CFP->getValueAPF().bitcastToAPInt();
769 const uint64_t* p = api.getRawData();
770 uint64_t word = p[1];
771 int shiftcount=12;
772 int width = api.getBitWidth();
773 for (int j=0; j<width; j+=4, shiftcount-=4) {
774 unsigned int nibble = (word>>shiftcount) & 15;
775 if (nibble < 10)
776 Out << (unsigned char)(nibble + '0');
777 else
778 Out << (unsigned char)(nibble - 10 + 'A');
779 if (shiftcount == 0 && j+4 < width) {
780 word = *p;
781 shiftcount = 64;
782 if (width-j-4 < 64)
783 shiftcount = width-j-4;
784 }
785 }
786 return;
787 } else if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEquad)
Chris Lattnercfb5a202008-08-19 05:06:27 +0000788 Out << 'L';
789 else if (&CFP->getValueAPF().getSemantics() == &APFloat::PPCDoubleDouble)
790 Out << 'M';
791 else
Torok Edwinc23197a2009-07-14 16:55:14 +0000792 llvm_unreachable("Unsupported floating point type");
Chris Lattnercfb5a202008-08-19 05:06:27 +0000793 // api needed to prevent premature destruction
Dale Johannesen7111b022008-10-09 18:53:47 +0000794 APInt api = CFP->getValueAPF().bitcastToAPInt();
Chris Lattnercfb5a202008-08-19 05:06:27 +0000795 const uint64_t* p = api.getRawData();
796 uint64_t word = *p;
797 int shiftcount=60;
798 int width = api.getBitWidth();
799 for (int j=0; j<width; j+=4, shiftcount-=4) {
800 unsigned int nibble = (word>>shiftcount) & 15;
801 if (nibble < 10)
802 Out << (unsigned char)(nibble + '0');
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000803 else
Chris Lattnercfb5a202008-08-19 05:06:27 +0000804 Out << (unsigned char)(nibble - 10 + 'A');
805 if (shiftcount == 0 && j+4 < width) {
806 word = *(++p);
807 shiftcount = 64;
808 if (width-j-4 < 64)
809 shiftcount = width-j-4;
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000810 }
811 }
Chris Lattnercfb5a202008-08-19 05:06:27 +0000812 return;
813 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000814
Chris Lattnercfb5a202008-08-19 05:06:27 +0000815 if (isa<ConstantAggregateZero>(CV)) {
Chris Lattnerde512b52004-02-15 05:55:15 +0000816 Out << "zeroinitializer";
Chris Lattnercfb5a202008-08-19 05:06:27 +0000817 return;
818 }
Andrew Trick18801ec2011-09-30 19:48:58 +0000819
Chris Lattner73050e12009-10-28 03:38:12 +0000820 if (const BlockAddress *BA = dyn_cast<BlockAddress>(CV)) {
821 Out << "blockaddress(";
Dan Gohman3bdfbf52010-07-20 23:55:01 +0000822 WriteAsOperandInternal(Out, BA->getFunction(), &TypePrinter, Machine,
823 Context);
Chris Lattner73050e12009-10-28 03:38:12 +0000824 Out << ", ";
Dan Gohman3bdfbf52010-07-20 23:55:01 +0000825 WriteAsOperandInternal(Out, BA->getBasicBlock(), &TypePrinter, Machine,
826 Context);
Chris Lattner73050e12009-10-28 03:38:12 +0000827 Out << ")";
828 return;
829 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000830
Chris Lattnercfb5a202008-08-19 05:06:27 +0000831 if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
Chris Lattner1afcace2011-07-09 17:41:24 +0000832 Type *ETy = CA->getType()->getElementType();
Chris Lattner18c7f802012-02-05 02:29:43 +0000833 Out << '[';
834 TypePrinter.print(ETy, Out);
835 Out << ' ';
836 WriteAsOperandInternal(Out, CA->getOperand(0),
837 &TypePrinter, Machine,
838 Context);
839 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
840 Out << ", ";
Chris Lattner8b10b692012-01-31 03:15:40 +0000841 TypePrinter.print(ETy, Out);
842 Out << ' ';
Chris Lattner18c7f802012-02-05 02:29:43 +0000843 WriteAsOperandInternal(Out, CA->getOperand(i), &TypePrinter, Machine,
Chris Lattner8b10b692012-01-31 03:15:40 +0000844 Context);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000845 }
Chris Lattner18c7f802012-02-05 02:29:43 +0000846 Out << ']';
Chris Lattnercfb5a202008-08-19 05:06:27 +0000847 return;
848 }
Chris Lattnerd59ae902012-01-26 02:32:04 +0000849
850 if (const ConstantDataArray *CA = dyn_cast<ConstantDataArray>(CV)) {
851 // As a special case, print the array as a string if it is an array of
852 // i8 with ConstantInt values.
853 if (CA->isString()) {
854 Out << "c\"";
855 PrintEscapedString(CA->getAsString(), Out);
856 Out << '"';
857 return;
858 }
859
860 Type *ETy = CA->getType()->getElementType();
861 Out << '[';
Chris Lattner8b10b692012-01-31 03:15:40 +0000862 TypePrinter.print(ETy, Out);
863 Out << ' ';
864 WriteAsOperandInternal(Out, CA->getElementAsConstant(0),
865 &TypePrinter, Machine,
866 Context);
867 for (unsigned i = 1, e = CA->getNumElements(); i != e; ++i) {
868 Out << ", ";
Chris Lattnerd59ae902012-01-26 02:32:04 +0000869 TypePrinter.print(ETy, Out);
870 Out << ' ';
Chris Lattner8b10b692012-01-31 03:15:40 +0000871 WriteAsOperandInternal(Out, CA->getElementAsConstant(i), &TypePrinter,
872 Machine, Context);
Chris Lattnerd59ae902012-01-26 02:32:04 +0000873 }
Chris Lattner8b10b692012-01-31 03:15:40 +0000874 Out << ']';
Chris Lattnerd59ae902012-01-26 02:32:04 +0000875 return;
876 }
877
Daniel Dunbara279bc32009-09-20 02:20:51 +0000878
Chris Lattnercfb5a202008-08-19 05:06:27 +0000879 if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
Andrew Lenharth43f344a2007-01-08 18:21:30 +0000880 if (CS->getType()->isPacked())
881 Out << '<';
Misha Brukman40c732c2004-06-04 21:11:51 +0000882 Out << '{';
Jim Laskeya3f332b2006-02-25 12:27:03 +0000883 unsigned N = CS->getNumOperands();
884 if (N) {
Chris Lattner24233032008-08-19 04:47:09 +0000885 Out << ' ';
Chris Lattner0f7364b2009-02-28 21:26:53 +0000886 TypePrinter.print(CS->getOperand(0)->getType(), Out);
Dan Gohman8dae1382008-09-14 17:21:12 +0000887 Out << ' ';
Chris Lattner7a716ad2002-04-16 21:36:08 +0000888
Dan Gohman3bdfbf52010-07-20 23:55:01 +0000889 WriteAsOperandInternal(Out, CS->getOperand(0), &TypePrinter, Machine,
890 Context);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000891
Jim Laskeya3f332b2006-02-25 12:27:03 +0000892 for (unsigned i = 1; i < N; i++) {
Chris Lattner7a716ad2002-04-16 21:36:08 +0000893 Out << ", ";
Chris Lattner0f7364b2009-02-28 21:26:53 +0000894 TypePrinter.print(CS->getOperand(i)->getType(), Out);
Dan Gohman8dae1382008-09-14 17:21:12 +0000895 Out << ' ';
Chris Lattner7a716ad2002-04-16 21:36:08 +0000896
Dan Gohman3bdfbf52010-07-20 23:55:01 +0000897 WriteAsOperandInternal(Out, CS->getOperand(i), &TypePrinter, Machine,
898 Context);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000899 }
Dan Gohman8dae1382008-09-14 17:21:12 +0000900 Out << ' ';
Chris Lattner7a716ad2002-04-16 21:36:08 +0000901 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000902
Dan Gohman8dae1382008-09-14 17:21:12 +0000903 Out << '}';
Andrew Lenharth43f344a2007-01-08 18:21:30 +0000904 if (CS->getType()->isPacked())
905 Out << '>';
Chris Lattnercfb5a202008-08-19 05:06:27 +0000906 return;
907 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000908
Chris Lattnerd59ae902012-01-26 02:32:04 +0000909 if (isa<ConstantVector>(CV) || isa<ConstantDataVector>(CV)) {
910 Type *ETy = CV->getType()->getVectorElementType();
Dan Gohman7dfa07f2009-02-11 00:25:25 +0000911 Out << '<';
Chris Lattner0f7364b2009-02-28 21:26:53 +0000912 TypePrinter.print(ETy, Out);
Dan Gohman8dae1382008-09-14 17:21:12 +0000913 Out << ' ';
Chris Lattnerd59ae902012-01-26 02:32:04 +0000914 WriteAsOperandInternal(Out, CV->getAggregateElement(0U), &TypePrinter,
915 Machine, Context);
916 for (unsigned i = 1, e = CV->getType()->getVectorNumElements(); i != e;++i){
Chris Lattner4667b712008-08-19 05:26:17 +0000917 Out << ", ";
Chris Lattner0f7364b2009-02-28 21:26:53 +0000918 TypePrinter.print(ETy, Out);
Dan Gohman8dae1382008-09-14 17:21:12 +0000919 Out << ' ';
Chris Lattnerd59ae902012-01-26 02:32:04 +0000920 WriteAsOperandInternal(Out, CV->getAggregateElement(i), &TypePrinter,
921 Machine, Context);
Chris Lattnercfb5a202008-08-19 05:06:27 +0000922 }
Dan Gohman7dfa07f2009-02-11 00:25:25 +0000923 Out << '>';
Chris Lattnercfb5a202008-08-19 05:06:27 +0000924 return;
925 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000926
Chris Lattnercfb5a202008-08-19 05:06:27 +0000927 if (isa<ConstantPointerNull>(CV)) {
Chris Lattner7a716ad2002-04-16 21:36:08 +0000928 Out << "null";
Chris Lattnercfb5a202008-08-19 05:06:27 +0000929 return;
930 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000931
Chris Lattnercfb5a202008-08-19 05:06:27 +0000932 if (isa<UndefValue>(CV)) {
Chris Lattnerb976e662004-10-16 18:08:06 +0000933 Out << "undef";
Chris Lattnercfb5a202008-08-19 05:06:27 +0000934 return;
935 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000936
Chris Lattnercfb5a202008-08-19 05:06:27 +0000937 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Reid Spencer81dfeb32006-12-04 05:19:18 +0000938 Out << CE->getOpcodeName();
Dan Gohman59858cf2009-07-27 16:11:46 +0000939 WriteOptimizationInfo(Out, CE);
Reid Spencer81dfeb32006-12-04 05:19:18 +0000940 if (CE->isCompare())
Chris Lattnercfb5a202008-08-19 05:06:27 +0000941 Out << ' ' << getPredicateText(CE->getPredicate());
Reid Spencer81dfeb32006-12-04 05:19:18 +0000942 Out << " (";
Misha Brukmanfd939082005-04-21 23:48:37 +0000943
Vikram S. Adveb4dbb442002-07-14 23:14:45 +0000944 for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) {
Chris Lattner0f7364b2009-02-28 21:26:53 +0000945 TypePrinter.print((*OI)->getType(), Out);
Dan Gohman8dae1382008-09-14 17:21:12 +0000946 Out << ' ';
Dan Gohman3bdfbf52010-07-20 23:55:01 +0000947 WriteAsOperandInternal(Out, *OI, &TypePrinter, Machine, Context);
Vikram S. Adveb4dbb442002-07-14 23:14:45 +0000948 if (OI+1 != CE->op_end())
Chris Lattnerc188eeb2002-07-30 18:54:25 +0000949 Out << ", ";
Vikram S. Adveb4dbb442002-07-14 23:14:45 +0000950 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000951
Dan Gohman995be7d2008-05-31 19:12:39 +0000952 if (CE->hasIndices()) {
Jay Foadd30aa5a2011-04-13 15:22:40 +0000953 ArrayRef<unsigned> Indices = CE->getIndices();
Dan Gohman995be7d2008-05-31 19:12:39 +0000954 for (unsigned i = 0, e = Indices.size(); i != e; ++i)
955 Out << ", " << Indices[i];
956 }
957
Reid Spencer3da59db2006-11-27 01:05:10 +0000958 if (CE->isCast()) {
Chris Lattner95586b82002-08-15 19:37:43 +0000959 Out << " to ";
Chris Lattner0f7364b2009-02-28 21:26:53 +0000960 TypePrinter.print(CE->getType(), Out);
Chris Lattner95586b82002-08-15 19:37:43 +0000961 }
Reid Spencer3da59db2006-11-27 01:05:10 +0000962
Misha Brukman40c732c2004-06-04 21:11:51 +0000963 Out << ')';
Chris Lattnercfb5a202008-08-19 05:06:27 +0000964 return;
Chris Lattner7a716ad2002-04-16 21:36:08 +0000965 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000966
Chris Lattnercfb5a202008-08-19 05:06:27 +0000967 Out << "<placeholder or erroneous Constant>";
Chris Lattner7a716ad2002-04-16 21:36:08 +0000968}
969
Chris Lattner85b19122009-12-31 02:31:59 +0000970static void WriteMDNodeBodyInternal(raw_ostream &Out, const MDNode *Node,
971 TypePrinting *TypePrinter,
Dan Gohman3bdfbf52010-07-20 23:55:01 +0000972 SlotTracker *Machine,
973 const Module *Context) {
Chris Lattner85b19122009-12-31 02:31:59 +0000974 Out << "!{";
975 for (unsigned mi = 0, me = Node->getNumOperands(); mi != me; ++mi) {
976 const Value *V = Node->getOperand(mi);
977 if (V == 0)
978 Out << "null";
979 else {
980 TypePrinter->print(V->getType(), Out);
981 Out << ' ';
Andrew Trick18801ec2011-09-30 19:48:58 +0000982 WriteAsOperandInternal(Out, Node->getOperand(mi),
Dan Gohman3bdfbf52010-07-20 23:55:01 +0000983 TypePrinter, Machine, Context);
Chris Lattner85b19122009-12-31 02:31:59 +0000984 }
985 if (mi + 1 != me)
986 Out << ", ";
987 }
Andrew Trick18801ec2011-09-30 19:48:58 +0000988
Chris Lattner85b19122009-12-31 02:31:59 +0000989 Out << "}";
990}
991
Chris Lattner7a716ad2002-04-16 21:36:08 +0000992
Misha Brukmanab5c6002004-03-02 00:22:19 +0000993/// WriteAsOperand - Write the name of the specified value out to the specified
994/// ostream. This can be useful when you just want to print int %reg126, not
995/// the whole instruction that generated it.
996///
Dan Gohman1220e102009-08-12 20:56:03 +0000997static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
Dan Gohmand6c0f652009-08-13 15:27:57 +0000998 TypePrinting *TypePrinter,
Dan Gohman3bdfbf52010-07-20 23:55:01 +0000999 SlotTracker *Machine,
1000 const Module *Context) {
Chris Lattnerc97536e2008-08-17 04:40:13 +00001001 if (V->hasName()) {
1002 PrintLLVMName(Out, V);
1003 return;
1004 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001005
Chris Lattnerc97536e2008-08-17 04:40:13 +00001006 const Constant *CV = dyn_cast<Constant>(V);
1007 if (CV && !isa<GlobalValue>(CV)) {
Dan Gohmand6c0f652009-08-13 15:27:57 +00001008 assert(TypePrinter && "Constants require TypePrinting!");
Dan Gohman3bdfbf52010-07-20 23:55:01 +00001009 WriteConstantInternal(Out, CV, *TypePrinter, Machine, Context);
Chris Lattnercfb5a202008-08-19 05:06:27 +00001010 return;
1011 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001012
Chris Lattnercfb5a202008-08-19 05:06:27 +00001013 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
Chris Lattnerc97536e2008-08-17 04:40:13 +00001014 Out << "asm ";
1015 if (IA->hasSideEffects())
1016 Out << "sideeffect ";
Dale Johannesen8ba2d5b2009-10-21 23:28:00 +00001017 if (IA->isAlignStack())
1018 Out << "alignstack ";
Chris Lattnerc97536e2008-08-17 04:40:13 +00001019 Out << '"';
1020 PrintEscapedString(IA->getAsmString(), Out);
1021 Out << "\", \"";
1022 PrintEscapedString(IA->getConstraintString(), Out);
1023 Out << '"';
Chris Lattnercfb5a202008-08-19 05:06:27 +00001024 return;
1025 }
Devang Patele54abc92009-07-22 17:43:22 +00001026
Devang Patel104cf9e2009-07-23 01:07:34 +00001027 if (const MDNode *N = dyn_cast<MDNode>(V)) {
Victor Hernandez5d301622009-12-18 20:09:14 +00001028 if (N->isFunctionLocal()) {
Victor Hernandez97e24502009-12-04 01:35:02 +00001029 // Print metadata inline, not via slot reference number.
Dan Gohman3bdfbf52010-07-20 23:55:01 +00001030 WriteMDNodeBodyInternal(Out, N, TypePrinter, Machine, Context);
Victor Hernandez97e24502009-12-04 01:35:02 +00001031 return;
1032 }
Andrew Trick18801ec2011-09-30 19:48:58 +00001033
Dan Gohman3bdfbf52010-07-20 23:55:01 +00001034 if (!Machine) {
1035 if (N->isFunctionLocal())
1036 Machine = new SlotTracker(N->getFunction());
1037 else
1038 Machine = new SlotTracker(Context);
1039 }
Dan Gohman3da076f2010-09-09 20:53:58 +00001040 int Slot = Machine->getMetadataSlot(N);
1041 if (Slot == -1)
1042 Out << "<badref>";
1043 else
1044 Out << '!' << Slot;
Devang Patel104cf9e2009-07-23 01:07:34 +00001045 return;
1046 }
1047
Devang Patele54abc92009-07-22 17:43:22 +00001048 if (const MDString *MDS = dyn_cast<MDString>(V)) {
Devang Patele54abc92009-07-22 17:43:22 +00001049 Out << "!\"";
Daniel Dunbar03d76512009-07-25 23:55:21 +00001050 PrintEscapedString(MDS->getString(), Out);
Devang Patele54abc92009-07-22 17:43:22 +00001051 Out << '"';
1052 return;
1053 }
1054
Evan Cheng746d5462009-11-16 07:10:36 +00001055 if (V->getValueID() == Value::PseudoSourceValueVal ||
1056 V->getValueID() == Value::FixedStackPseudoSourceValueVal) {
Dan Gohmancd26ec52009-09-23 01:33:16 +00001057 V->print(Out);
1058 return;
1059 }
1060
Chris Lattnercfb5a202008-08-19 05:06:27 +00001061 char Prefix = '%';
1062 int Slot;
Chris Lattnerfb5179a2011-08-03 06:15:41 +00001063 // If we have a SlotTracker, use it.
Chris Lattnercfb5a202008-08-19 05:06:27 +00001064 if (Machine) {
1065 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1066 Slot = Machine->getGlobalSlot(GV);
1067 Prefix = '@';
1068 } else {
1069 Slot = Machine->getLocalSlot(V);
Andrew Trick18801ec2011-09-30 19:48:58 +00001070
Chris Lattnerfb5179a2011-08-03 06:15:41 +00001071 // If the local value didn't succeed, then we may be referring to a value
1072 // from a different function. Translate it, as this can happen when using
1073 // address of blocks.
1074 if (Slot == -1)
1075 if ((Machine = createSlotTracker(V))) {
1076 Slot = Machine->getLocalSlot(V);
1077 delete Machine;
1078 }
Chris Lattnercfb5a202008-08-19 05:06:27 +00001079 }
Chris Lattnerfb5179a2011-08-03 06:15:41 +00001080 } else if ((Machine = createSlotTracker(V))) {
1081 // Otherwise, create one to get the # and then destroy it.
1082 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1083 Slot = Machine->getGlobalSlot(GV);
1084 Prefix = '@';
Chris Lattner80cd1152006-01-25 22:26:05 +00001085 } else {
Chris Lattnerfb5179a2011-08-03 06:15:41 +00001086 Slot = Machine->getLocalSlot(V);
Chris Lattner7a716ad2002-04-16 21:36:08 +00001087 }
Chris Lattnerfb5179a2011-08-03 06:15:41 +00001088 delete Machine;
1089 Machine = 0;
1090 } else {
1091 Slot = -1;
Chris Lattner7a716ad2002-04-16 21:36:08 +00001092 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001093
Chris Lattnercfb5a202008-08-19 05:06:27 +00001094 if (Slot != -1)
1095 Out << Prefix << Slot;
1096 else
1097 Out << "<badref>";
Chris Lattner7a716ad2002-04-16 21:36:08 +00001098}
1099
Dan Gohman1220e102009-08-12 20:56:03 +00001100void llvm::WriteAsOperand(raw_ostream &Out, const Value *V,
1101 bool PrintType, const Module *Context) {
Dan Gohmand6c0f652009-08-13 15:27:57 +00001102
1103 // Fast path: Don't construct and populate a TypePrinting object if we
1104 // won't be needing any types printed.
Dan Gohman009fc9e2009-08-13 23:07:11 +00001105 if (!PrintType &&
Dan Gohman3bdfbf52010-07-20 23:55:01 +00001106 ((!isa<Constant>(V) && !isa<MDNode>(V)) ||
1107 V->hasName() || isa<GlobalValue>(V))) {
1108 WriteAsOperandInternal(Out, V, 0, 0, Context);
Dan Gohmand6c0f652009-08-13 15:27:57 +00001109 return;
1110 }
1111
Chris Lattner607dc682002-07-10 16:48:17 +00001112 if (Context == 0) Context = getModuleFromVal(V);
Chris Lattner207b5bc2001-10-29 16:37:48 +00001113
Chris Lattnere9fa33e2009-02-28 23:20:19 +00001114 TypePrinting TypePrinter;
Chris Lattner1afcace2011-07-09 17:41:24 +00001115 if (Context)
1116 TypePrinter.incorporateTypes(*Context);
Dan Gohman8dae1382008-09-14 17:21:12 +00001117 if (PrintType) {
Chris Lattner0f7364b2009-02-28 21:26:53 +00001118 TypePrinter.print(V->getType(), Out);
Dan Gohman8dae1382008-09-14 17:21:12 +00001119 Out << ' ';
1120 }
Misha Brukmanfd939082005-04-21 23:48:37 +00001121
Dan Gohman3bdfbf52010-07-20 23:55:01 +00001122 WriteAsOperandInternal(Out, V, &TypePrinter, 0, Context);
Chris Lattner622f7402001-07-20 19:15:21 +00001123}
1124
Chris Lattnercfb5a202008-08-19 05:06:27 +00001125namespace {
Chris Lattnerd8c2e422001-07-12 23:35:26 +00001126
Chris Lattner007377f2001-09-07 16:36:04 +00001127class AssemblyWriter {
Dan Gohman683e9222009-08-12 17:23:50 +00001128 formatted_raw_ostream &Out;
Chris Lattner0d9574a2008-08-19 04:26:57 +00001129 SlotTracker &Machine;
Chris Lattnerc1824992001-10-29 16:05:51 +00001130 const Module *TheModule;
Chris Lattner9cc34462009-02-28 20:25:14 +00001131 TypePrinting TypePrinter;
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001132 AssemblyAnnotationWriter *AnnotationWriter;
Andrew Trick18801ec2011-09-30 19:48:58 +00001133
Chris Lattner00950542001-06-06 20:29:01 +00001134public:
Dan Gohman683e9222009-08-12 17:23:50 +00001135 inline AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac,
1136 const Module *M,
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001137 AssemblyAnnotationWriter *AAW)
Devang Patel3168b792009-09-28 18:31:56 +00001138 : Out(o), Machine(Mac), TheModule(M), AnnotationWriter(AAW) {
Chris Lattner1afcace2011-07-09 17:41:24 +00001139 if (M)
1140 TypePrinter.incorporateTypes(*M);
Chris Lattner00950542001-06-06 20:29:01 +00001141 }
1142
Chris Lattner6e6b1802009-12-31 02:13:35 +00001143 void printMDNodeBody(const MDNode *MD);
Chris Lattnerfdb33562009-12-31 01:54:05 +00001144 void printNamedMDNode(const NamedMDNode *NMD);
Andrew Trick18801ec2011-09-30 19:48:58 +00001145
Chris Lattnerbd72b322009-12-31 02:23:35 +00001146 void printModule(const Module *M);
Chris Lattner00950542001-06-06 20:29:01 +00001147
Chris Lattner2fcfdb72006-12-06 06:24:27 +00001148 void writeOperand(const Value *Op, bool PrintType);
Devang Pateleaf42ab2008-09-23 23:03:40 +00001149 void writeParamOperand(const Value *Operand, Attributes Attrs);
Eli Friedman47f35132011-07-25 23:16:38 +00001150 void writeAtomic(AtomicOrdering Ordering, SynchronizationScope SynchScope);
Chris Lattner66e810b2002-04-18 18:53:13 +00001151
Chris Lattner6e6b1802009-12-31 02:13:35 +00001152 void writeAllMDNodes();
1153
Chris Lattner1afcace2011-07-09 17:41:24 +00001154 void printTypeIdentities();
Chris Lattnerc1824992001-10-29 16:05:51 +00001155 void printGlobal(const GlobalVariable *GV);
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001156 void printAlias(const GlobalAlias *GV);
Chris Lattner79df7c02002-03-26 18:01:55 +00001157 void printFunction(const Function *F);
Devang Pateleaf42ab2008-09-23 23:03:40 +00001158 void printArgument(const Argument *FA, Attributes Attrs);
Chris Lattnerc1824992001-10-29 16:05:51 +00001159 void printBasicBlock(const BasicBlock *BB);
Chris Lattner7e708292002-06-25 16:13:24 +00001160 void printInstruction(const Instruction &I);
Chris Lattner2761e9f2002-04-13 20:53:41 +00001161
Dan Gohman659d1e82010-02-10 20:42:57 +00001162private:
Chris Lattnere02fa852001-10-13 06:42:36 +00001163 // printInfoComment - Print a little comment after the instruction indicating
1164 // which slot it occupies.
Chris Lattner7e708292002-06-25 16:13:24 +00001165 void printInfoComment(const Value &V);
Chris Lattner00950542001-06-06 20:29:01 +00001166};
Chris Lattner413fd232009-03-01 00:03:38 +00001167} // end of anonymous namespace
Chris Lattner00950542001-06-06 20:29:01 +00001168
Chris Lattner2fcfdb72006-12-06 06:24:27 +00001169void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType) {
1170 if (Operand == 0) {
Chris Lattneraab18202005-02-24 16:58:29 +00001171 Out << "<null operand!>";
Chris Lattnerc65b72c2009-12-31 02:33:14 +00001172 return;
Chris Lattneraab18202005-02-24 16:58:29 +00001173 }
Chris Lattnerc65b72c2009-12-31 02:33:14 +00001174 if (PrintType) {
1175 TypePrinter.print(Operand->getType(), Out);
1176 Out << ' ';
1177 }
Dan Gohman3bdfbf52010-07-20 23:55:01 +00001178 WriteAsOperandInternal(Out, Operand, &TypePrinter, &Machine, TheModule);
Chris Lattner00950542001-06-06 20:29:01 +00001179}
1180
Eli Friedman47f35132011-07-25 23:16:38 +00001181void AssemblyWriter::writeAtomic(AtomicOrdering Ordering,
1182 SynchronizationScope SynchScope) {
1183 if (Ordering == NotAtomic)
1184 return;
1185
1186 switch (SynchScope) {
Eli Friedman47f35132011-07-25 23:16:38 +00001187 case SingleThread: Out << " singlethread"; break;
1188 case CrossThread: break;
1189 }
1190
1191 switch (Ordering) {
1192 default: Out << " <bad ordering " << int(Ordering) << ">"; break;
1193 case Unordered: Out << " unordered"; break;
1194 case Monotonic: Out << " monotonic"; break;
1195 case Acquire: Out << " acquire"; break;
1196 case Release: Out << " release"; break;
1197 case AcquireRelease: Out << " acq_rel"; break;
1198 case SequentiallyConsistent: Out << " seq_cst"; break;
1199 }
1200}
1201
Daniel Dunbara279bc32009-09-20 02:20:51 +00001202void AssemblyWriter::writeParamOperand(const Value *Operand,
Devang Pateleaf42ab2008-09-23 23:03:40 +00001203 Attributes Attrs) {
Duncan Sandsdc024672007-11-27 13:23:08 +00001204 if (Operand == 0) {
1205 Out << "<null operand!>";
Chris Lattnerc65b72c2009-12-31 02:33:14 +00001206 return;
Duncan Sandsdc024672007-11-27 13:23:08 +00001207 }
Chris Lattnerc65b72c2009-12-31 02:33:14 +00001208
1209 // Print the type
1210 TypePrinter.print(Operand->getType(), Out);
1211 // Print parameter attributes list
1212 if (Attrs != Attribute::None)
1213 Out << ' ' << Attribute::getAsString(Attrs);
1214 Out << ' ';
1215 // Print the operand
Dan Gohman3bdfbf52010-07-20 23:55:01 +00001216 WriteAsOperandInternal(Out, Operand, &TypePrinter, &Machine, TheModule);
Duncan Sandsdc024672007-11-27 13:23:08 +00001217}
Chris Lattner00950542001-06-06 20:29:01 +00001218
Chris Lattnerc1824992001-10-29 16:05:51 +00001219void AssemblyWriter::printModule(const Module *M) {
Chris Lattner31ab1b32005-03-02 23:12:40 +00001220 if (!M->getModuleIdentifier().empty() &&
Misha Brukmanfd939082005-04-21 23:48:37 +00001221 // Don't print the ID if it will start a new line (which would
Chris Lattner31ab1b32005-03-02 23:12:40 +00001222 // require a comment char before it).
1223 M->getModuleIdentifier().find('\n') == std::string::npos)
1224 Out << "; ModuleID = '" << M->getModuleIdentifier() << "'\n";
1225
Owen Andersoncf7ff2b2006-10-18 02:21:12 +00001226 if (!M->getDataLayout().empty())
Chris Lattnerd2f9e602006-10-22 06:06:56 +00001227 Out << "target datalayout = \"" << M->getDataLayout() << "\"\n";
Reid Spencercddc86f2004-07-25 21:44:54 +00001228 if (!M->getTargetTriple().empty())
Reid Spencerc9a1f0d2004-07-25 21:29:43 +00001229 Out << "target triple = \"" << M->getTargetTriple() << "\"\n";
Misha Brukmanfd939082005-04-21 23:48:37 +00001230
Chris Lattnercc041ba2006-01-24 04:13:11 +00001231 if (!M->getModuleInlineAsm().empty()) {
Chris Lattner42a162e2006-01-24 00:45:30 +00001232 // Split the string into lines, to make it easier to read the .ll file.
Chris Lattnercc041ba2006-01-24 04:13:11 +00001233 std::string Asm = M->getModuleInlineAsm();
Chris Lattner42a162e2006-01-24 00:45:30 +00001234 size_t CurPos = 0;
1235 size_t NewLine = Asm.find_first_of('\n', CurPos);
Dan Gohman9bf0b9b2009-08-12 23:54:22 +00001236 Out << '\n';
Chris Lattner42a162e2006-01-24 00:45:30 +00001237 while (NewLine != std::string::npos) {
1238 // We found a newline, print the portion of the asm string from the
1239 // last newline up to this newline.
1240 Out << "module asm \"";
1241 PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.begin()+NewLine),
1242 Out);
1243 Out << "\"\n";
1244 CurPos = NewLine+1;
1245 NewLine = Asm.find_first_of('\n', CurPos);
1246 }
Rafael Espindola38c4e532011-03-02 04:14:42 +00001247 std::string rest(Asm.begin()+CurPos, Asm.end());
1248 if (!rest.empty()) {
1249 Out << "module asm \"";
1250 PrintEscapedString(rest, Out);
1251 Out << "\"\n";
1252 }
Chris Lattner18365502006-01-23 23:03:36 +00001253 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001254
Chris Lattner44da7d72004-09-14 05:06:58 +00001255 // Loop over the dependent libraries and emit them.
Chris Lattnercfe97b72004-09-14 04:51:44 +00001256 Module::lib_iterator LI = M->lib_begin();
1257 Module::lib_iterator LE = M->lib_end();
Reid Spencercddc86f2004-07-25 21:44:54 +00001258 if (LI != LE) {
Dan Gohman9bf0b9b2009-08-12 23:54:22 +00001259 Out << '\n';
Chris Lattnercfe97b72004-09-14 04:51:44 +00001260 Out << "deplibs = [ ";
1261 while (LI != LE) {
Chris Lattner44da7d72004-09-14 05:06:58 +00001262 Out << '"' << *LI << '"';
Reid Spencerc9a1f0d2004-07-25 21:29:43 +00001263 ++LI;
Chris Lattnercfe97b72004-09-14 04:51:44 +00001264 if (LI != LE)
1265 Out << ", ";
Reid Spencerc9a1f0d2004-07-25 21:29:43 +00001266 }
Dan Gohman9bf0b9b2009-08-12 23:54:22 +00001267 Out << " ]";
Reid Spencer83f6a772004-07-25 18:08:18 +00001268 }
Reid Spencere59eaf42004-09-13 23:44:23 +00001269
Chris Lattner1afcace2011-07-09 17:41:24 +00001270 printTypeIdentities();
Misha Brukmanfd939082005-04-21 23:48:37 +00001271
Dan Gohman9bf0b9b2009-08-12 23:54:22 +00001272 // Output all globals.
1273 if (!M->global_empty()) Out << '\n';
Chris Lattnerd6d826c2006-12-06 04:41:52 +00001274 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
1275 I != E; ++I)
Chris Lattner7e708292002-06-25 16:13:24 +00001276 printGlobal(I);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001277
Chris Lattner69dacfc2007-04-26 02:24:10 +00001278 // Output all aliases.
1279 if (!M->alias_empty()) Out << "\n";
1280 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
1281 I != E; ++I)
1282 printAlias(I);
Chris Lattner007377f2001-09-07 16:36:04 +00001283
Chris Lattner44da7d72004-09-14 05:06:58 +00001284 // Output all of the functions.
Chris Lattner7e708292002-06-25 16:13:24 +00001285 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
1286 printFunction(I);
Devang Patel320671d2009-07-08 21:44:25 +00001287
Devang Patel37c4a2d2009-07-29 22:04:47 +00001288 // Output named metadata.
Dan Gohman9bf0b9b2009-08-12 23:54:22 +00001289 if (!M->named_metadata_empty()) Out << '\n';
Andrew Trick18801ec2011-09-30 19:48:58 +00001290
Devang Patel37c4a2d2009-07-29 22:04:47 +00001291 for (Module::const_named_metadata_iterator I = M->named_metadata_begin(),
Chris Lattner6e6b1802009-12-31 02:13:35 +00001292 E = M->named_metadata_end(); I != E; ++I)
Chris Lattnerfdb33562009-12-31 01:54:05 +00001293 printNamedMDNode(I);
Devang Patel37c4a2d2009-07-29 22:04:47 +00001294
1295 // Output metadata.
Chris Lattner307c9892009-12-31 02:20:11 +00001296 if (!Machine.mdn_empty()) {
Chris Lattner6e6b1802009-12-31 02:13:35 +00001297 Out << '\n';
1298 writeAllMDNodes();
1299 }
Chris Lattner007377f2001-09-07 16:36:04 +00001300}
1301
Chris Lattnerfdb33562009-12-31 01:54:05 +00001302void AssemblyWriter::printNamedMDNode(const NamedMDNode *NMD) {
Nick Lewycky9100a782011-06-15 06:37:58 +00001303 Out << '!';
1304 StringRef Name = NMD->getName();
1305 if (Name.empty()) {
1306 Out << "<empty name> ";
1307 } else {
1308 if (isalpha(Name[0]) || Name[0] == '-' || Name[0] == '$' ||
1309 Name[0] == '.' || Name[0] == '_')
1310 Out << Name[0];
1311 else
1312 Out << '\\' << hexdigit(Name[0] >> 4) << hexdigit(Name[0] & 0x0F);
1313 for (unsigned i = 1, e = Name.size(); i != e; ++i) {
1314 unsigned char C = Name[i];
1315 if (isalnum(C) || C == '-' || C == '$' || C == '.' || C == '_')
1316 Out << C;
1317 else
1318 Out << '\\' << hexdigit(C >> 4) << hexdigit(C & 0x0F);
1319 }
1320 }
1321 Out << " = !{";
Chris Lattnerfdb33562009-12-31 01:54:05 +00001322 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1323 if (i) Out << ", ";
Dan Gohman3da076f2010-09-09 20:53:58 +00001324 int Slot = Machine.getMetadataSlot(NMD->getOperand(i));
1325 if (Slot == -1)
1326 Out << "<badref>";
1327 else
1328 Out << '!' << Slot;
Chris Lattnerfdb33562009-12-31 01:54:05 +00001329 }
1330 Out << "}\n";
1331}
1332
1333
Dan Gohman683e9222009-08-12 17:23:50 +00001334static void PrintLinkage(GlobalValue::LinkageTypes LT,
1335 formatted_raw_ostream &Out) {
Chris Lattnercfb5a202008-08-19 05:06:27 +00001336 switch (LT) {
Bill Wendling3d10a5a2009-07-20 01:03:30 +00001337 case GlobalValue::ExternalLinkage: break;
1338 case GlobalValue::PrivateLinkage: Out << "private "; break;
1339 case GlobalValue::LinkerPrivateLinkage: Out << "linker_private "; break;
Bill Wendling5e721d72010-07-01 21:55:59 +00001340 case GlobalValue::LinkerPrivateWeakLinkage:
1341 Out << "linker_private_weak ";
1342 break;
Bill Wendling55ae5152010-08-20 22:05:50 +00001343 case GlobalValue::LinkerPrivateWeakDefAutoLinkage:
1344 Out << "linker_private_weak_def_auto ";
1345 break;
Bill Wendling3d10a5a2009-07-20 01:03:30 +00001346 case GlobalValue::InternalLinkage: Out << "internal "; break;
1347 case GlobalValue::LinkOnceAnyLinkage: Out << "linkonce "; break;
1348 case GlobalValue::LinkOnceODRLinkage: Out << "linkonce_odr "; break;
1349 case GlobalValue::WeakAnyLinkage: Out << "weak "; break;
1350 case GlobalValue::WeakODRLinkage: Out << "weak_odr "; break;
1351 case GlobalValue::CommonLinkage: Out << "common "; break;
1352 case GlobalValue::AppendingLinkage: Out << "appending "; break;
1353 case GlobalValue::DLLImportLinkage: Out << "dllimport "; break;
1354 case GlobalValue::DLLExportLinkage: Out << "dllexport "; break;
1355 case GlobalValue::ExternalWeakLinkage: Out << "extern_weak "; break;
Chris Lattner266c7bb2009-04-13 05:44:34 +00001356 case GlobalValue::AvailableExternallyLinkage:
1357 Out << "available_externally ";
1358 break;
Chris Lattnercfb5a202008-08-19 05:06:27 +00001359 }
1360}
Duncan Sands667d4b82009-03-07 15:45:40 +00001361
Chris Lattnercfb5a202008-08-19 05:06:27 +00001362
1363static void PrintVisibility(GlobalValue::VisibilityTypes Vis,
Dan Gohman683e9222009-08-12 17:23:50 +00001364 formatted_raw_ostream &Out) {
Chris Lattnercfb5a202008-08-19 05:06:27 +00001365 switch (Vis) {
Chris Lattnercfb5a202008-08-19 05:06:27 +00001366 case GlobalValue::DefaultVisibility: break;
1367 case GlobalValue::HiddenVisibility: Out << "hidden "; break;
1368 case GlobalValue::ProtectedVisibility: Out << "protected "; break;
1369 }
1370}
1371
Chris Lattnerc1824992001-10-29 16:05:51 +00001372void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Dan Gohman4483c7b2010-01-29 23:12:36 +00001373 if (GV->isMaterializable())
1374 Out << "; Materializable\n";
1375
Dan Gohman3bdfbf52010-07-20 23:55:01 +00001376 WriteAsOperandInternal(Out, GV, &TypePrinter, &Machine, GV->getParent());
Dan Gohman3845e502009-08-12 23:32:33 +00001377 Out << " = ";
Chris Lattnerd70684f2001-09-18 04:01:05 +00001378
Chris Lattner52b26de2008-08-19 05:16:28 +00001379 if (!GV->hasInitializer() && GV->hasExternalLinkage())
1380 Out << "external ";
Daniel Dunbara279bc32009-09-20 02:20:51 +00001381
Chris Lattner52b26de2008-08-19 05:16:28 +00001382 PrintLinkage(GV->getLinkage(), Out);
1383 PrintVisibility(GV->getVisibility(), Out);
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00001384
1385 if (GV->isThreadLocal()) Out << "thread_local ";
Chris Lattnerdf986172009-01-02 07:01:27 +00001386 if (unsigned AddressSpace = GV->getType()->getAddressSpace())
1387 Out << "addrspace(" << AddressSpace << ") ";
Rafael Espindolabea46262011-01-08 16:42:36 +00001388 if (GV->hasUnnamedAddr()) Out << "unnamed_addr ";
Misha Brukman0313e0b2004-06-21 21:53:56 +00001389 Out << (GV->isConstant() ? "constant " : "global ");
Chris Lattner0f7364b2009-02-28 21:26:53 +00001390 TypePrinter.print(GV->getType()->getElementType(), Out);
Chris Lattnerd70684f2001-09-18 04:01:05 +00001391
Dan Gohman8dae1382008-09-14 17:21:12 +00001392 if (GV->hasInitializer()) {
1393 Out << ' ';
Devang Patel320671d2009-07-08 21:44:25 +00001394 writeOperand(GV->getInitializer(), false);
Dan Gohman8dae1382008-09-14 17:21:12 +00001395 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001396
Chris Lattner8fff1262010-07-07 23:16:37 +00001397 if (GV->hasSection()) {
1398 Out << ", section \"";
1399 PrintEscapedString(GV->getSection(), Out);
1400 Out << '"';
1401 }
Chris Lattner60962db2005-11-12 00:10:19 +00001402 if (GV->getAlignment())
Chris Lattner30caa282005-11-06 06:48:53 +00001403 Out << ", align " << GV->getAlignment();
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001404
Chris Lattner7e708292002-06-25 16:13:24 +00001405 printInfoComment(*GV);
Chris Lattnercfb5a202008-08-19 05:06:27 +00001406 Out << '\n';
Chris Lattner70cc3392001-09-10 07:58:01 +00001407}
1408
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001409void AssemblyWriter::printAlias(const GlobalAlias *GA) {
Dan Gohman4483c7b2010-01-29 23:12:36 +00001410 if (GA->isMaterializable())
1411 Out << "; Materializable\n";
1412
Dale Johannesen24f07dc2008-06-03 18:14:29 +00001413 // Don't crash when dumping partially built GA
1414 if (!GA->hasName())
1415 Out << "<<nameless>> = ";
Chris Lattnerc97536e2008-08-17 04:40:13 +00001416 else {
1417 PrintLLVMName(Out, GA);
1418 Out << " = ";
1419 }
Chris Lattnercfb5a202008-08-19 05:06:27 +00001420 PrintVisibility(GA->getVisibility(), Out);
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001421
1422 Out << "alias ";
1423
Chris Lattnercfb5a202008-08-19 05:06:27 +00001424 PrintLinkage(GA->getLinkage(), Out);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001425
Anton Korobeynikovc6c98af2007-04-29 18:02:48 +00001426 const Constant *Aliasee = GA->getAliasee();
Daniel Dunbara279bc32009-09-20 02:20:51 +00001427
Chris Lattner1afcace2011-07-09 17:41:24 +00001428 if (Aliasee == 0) {
1429 TypePrinter.print(GA->getType(), Out);
1430 Out << " <<NULL ALIASEE>>";
Jay Foad5cd8ea22011-08-01 12:48:54 +00001431 } else {
Jay Foad8d948652011-08-01 12:29:14 +00001432 writeOperand(Aliasee, !isa<ConstantExpr>(Aliasee));
Jay Foad5cd8ea22011-08-01 12:48:54 +00001433 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001434
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001435 printInfoComment(*GA);
Chris Lattner52b26de2008-08-19 05:16:28 +00001436 Out << '\n';
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001437}
1438
Chris Lattner1afcace2011-07-09 17:41:24 +00001439void AssemblyWriter::printTypeIdentities() {
1440 if (TypePrinter.NumberedTypes.empty() &&
1441 TypePrinter.NamedTypes.empty())
1442 return;
Andrew Trick18801ec2011-09-30 19:48:58 +00001443
Chris Lattner1afcace2011-07-09 17:41:24 +00001444 Out << '\n';
Andrew Trick18801ec2011-09-30 19:48:58 +00001445
Chris Lattner1afcace2011-07-09 17:41:24 +00001446 // We know all the numbers that each type is used and we know that it is a
1447 // dense assignment. Convert the map to an index table.
1448 std::vector<StructType*> NumberedTypes(TypePrinter.NumberedTypes.size());
Andrew Trick18801ec2011-09-30 19:48:58 +00001449 for (DenseMap<StructType*, unsigned>::iterator I =
Chris Lattner1afcace2011-07-09 17:41:24 +00001450 TypePrinter.NumberedTypes.begin(), E = TypePrinter.NumberedTypes.end();
1451 I != E; ++I) {
1452 assert(I->second < NumberedTypes.size() && "Didn't get a dense numbering?");
1453 NumberedTypes[I->second] = I->first;
1454 }
Andrew Trick18801ec2011-09-30 19:48:58 +00001455
Chris Lattner413fd232009-03-01 00:03:38 +00001456 // Emit all numbered types.
1457 for (unsigned i = 0, e = NumberedTypes.size(); i != e; ++i) {
Dan Gohman3845e502009-08-12 23:32:33 +00001458 Out << '%' << i << " = type ";
Andrew Trick18801ec2011-09-30 19:48:58 +00001459
Chris Lattner413fd232009-03-01 00:03:38 +00001460 // Make sure we print out at least one level of the type structure, so
1461 // that we do not get %2 = type %2
Chris Lattner1afcace2011-07-09 17:41:24 +00001462 TypePrinter.printStructBody(NumberedTypes[i], Out);
Dan Gohman9bf0b9b2009-08-12 23:54:22 +00001463 Out << '\n';
Chris Lattner413fd232009-03-01 00:03:38 +00001464 }
Andrew Trick18801ec2011-09-30 19:48:58 +00001465
Chris Lattner1afcace2011-07-09 17:41:24 +00001466 for (unsigned i = 0, e = TypePrinter.NamedTypes.size(); i != e; ++i) {
1467 PrintLLVMName(Out, TypePrinter.NamedTypes[i]->getName(), LocalPrefix);
Chris Lattner52b26de2008-08-19 05:16:28 +00001468 Out << " = type ";
Reid Spencer9231ac82004-05-25 08:53:40 +00001469
1470 // Make sure we print out at least one level of the type structure, so
1471 // that we do not get %FILE = type %FILE
Chris Lattner1afcace2011-07-09 17:41:24 +00001472 TypePrinter.printStructBody(TypePrinter.NamedTypes[i], Out);
Chris Lattnercfb5a202008-08-19 05:06:27 +00001473 Out << '\n';
Reid Spencer9231ac82004-05-25 08:53:40 +00001474 }
Reid Spencer78d033e2007-01-06 07:24:44 +00001475}
1476
Misha Brukmanab5c6002004-03-02 00:22:19 +00001477/// printFunction - Print all aspects of a function.
1478///
Chris Lattner7e708292002-06-25 16:13:24 +00001479void AssemblyWriter::printFunction(const Function *F) {
Chris Lattnercfb5a202008-08-19 05:06:27 +00001480 // Print out the return type and name.
1481 Out << '\n';
Chris Lattner4ad02e72003-04-16 20:28:45 +00001482
Misha Brukman0313e0b2004-06-21 21:53:56 +00001483 if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, Out);
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001484
Dan Gohman4483c7b2010-01-29 23:12:36 +00001485 if (F->isMaterializable())
1486 Out << "; Materializable\n";
1487
Reid Spencer5cbf9852007-01-30 20:08:39 +00001488 if (F->isDeclaration())
Chris Lattner3aa60662007-08-19 22:15:26 +00001489 Out << "declare ";
1490 else
Reid Spencerb951bc02006-12-29 20:29:48 +00001491 Out << "define ";
Daniel Dunbara279bc32009-09-20 02:20:51 +00001492
Chris Lattnercfb5a202008-08-19 05:06:27 +00001493 PrintLinkage(F->getLinkage(), Out);
1494 PrintVisibility(F->getVisibility(), Out);
Chris Lattner4ad02e72003-04-16 20:28:45 +00001495
Chris Lattnerd5118982005-05-06 20:26:43 +00001496 // Print the calling convention.
1497 switch (F->getCallingConv()) {
1498 case CallingConv::C: break; // default
Anton Korobeynikovf8248682006-09-20 22:03:51 +00001499 case CallingConv::Fast: Out << "fastcc "; break;
1500 case CallingConv::Cold: Out << "coldcc "; break;
1501 case CallingConv::X86_StdCall: Out << "x86_stdcallcc "; break;
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001502 case CallingConv::X86_FastCall: Out << "x86_fastcallcc "; break;
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001503 case CallingConv::X86_ThisCall: Out << "x86_thiscallcc "; break;
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001504 case CallingConv::ARM_APCS: Out << "arm_apcscc "; break;
1505 case CallingConv::ARM_AAPCS: Out << "arm_aapcscc "; break;
1506 case CallingConv::ARM_AAPCS_VFP:Out << "arm_aapcs_vfpcc "; break;
Anton Korobeynikov211a14e2009-12-07 02:27:35 +00001507 case CallingConv::MSP430_INTR: Out << "msp430_intrcc "; break;
Justin Holewinskia6428a42011-03-07 14:32:30 +00001508 case CallingConv::PTX_Kernel: Out << "ptx_kernel "; break;
1509 case CallingConv::PTX_Device: Out << "ptx_device "; break;
Chris Lattnerd5118982005-05-06 20:26:43 +00001510 default: Out << "cc" << F->getCallingConv() << " "; break;
1511 }
1512
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001513 FunctionType *FT = F->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00001514 const AttrListPtr &Attrs = F->getAttributes();
Devang Patel652203f2008-09-29 20:49:50 +00001515 Attributes RetAttrs = Attrs.getRetAttributes();
1516 if (RetAttrs != Attribute::None)
1517 Out << Attribute::getAsString(Attrs.getRetAttributes()) << ' ';
Chris Lattner0f7364b2009-02-28 21:26:53 +00001518 TypePrinter.print(F->getReturnType(), Out);
Chris Lattner4667b712008-08-19 05:26:17 +00001519 Out << ' ';
Dan Gohman3bdfbf52010-07-20 23:55:01 +00001520 WriteAsOperandInternal(Out, F, &TypePrinter, &Machine, F->getParent());
Misha Brukman0313e0b2004-06-21 21:53:56 +00001521 Out << '(';
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001522 Machine.incorporateFunction(F);
Chris Lattner007377f2001-09-07 16:36:04 +00001523
Chris Lattnerc1824992001-10-29 16:05:51 +00001524 // Loop over the arguments, printing them...
Chris Lattner007377f2001-09-07 16:36:04 +00001525
Reid Spencerbd5db8e2006-12-31 05:24:50 +00001526 unsigned Idx = 1;
Chris Lattner8dcd2f12007-04-18 00:57:22 +00001527 if (!F->isDeclaration()) {
1528 // If this isn't a declaration, print the argument names as well.
1529 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
1530 I != E; ++I) {
1531 // Insert commas as we go... the first arg doesn't get a comma
1532 if (I != F->arg_begin()) Out << ", ";
Devang Patel19c87462008-09-26 22:53:05 +00001533 printArgument(I, Attrs.getParamAttributes(Idx));
Chris Lattner8dcd2f12007-04-18 00:57:22 +00001534 Idx++;
1535 }
1536 } else {
1537 // Otherwise, print the types from the function type.
1538 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
1539 // Insert commas as we go... the first arg doesn't get a comma
1540 if (i) Out << ", ";
Daniel Dunbara279bc32009-09-20 02:20:51 +00001541
Chris Lattner8dcd2f12007-04-18 00:57:22 +00001542 // Output type...
Chris Lattner0f7364b2009-02-28 21:26:53 +00001543 TypePrinter.print(FT->getParamType(i), Out);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001544
Devang Patel19c87462008-09-26 22:53:05 +00001545 Attributes ArgAttrs = Attrs.getParamAttributes(i+1);
Devang Patel05988662008-09-25 21:00:45 +00001546 if (ArgAttrs != Attribute::None)
1547 Out << ' ' << Attribute::getAsString(ArgAttrs);
Chris Lattner8dcd2f12007-04-18 00:57:22 +00001548 }
Reid Spencerbd5db8e2006-12-31 05:24:50 +00001549 }
Chris Lattner007377f2001-09-07 16:36:04 +00001550
1551 // Finish printing arguments...
Chris Lattner7e708292002-06-25 16:13:24 +00001552 if (FT->isVarArg()) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001553 if (FT->getNumParams()) Out << ", ";
1554 Out << "..."; // Output varargs portion of signature!
Chris Lattner007377f2001-09-07 16:36:04 +00001555 }
Misha Brukman0313e0b2004-06-21 21:53:56 +00001556 Out << ')';
Rafael Espindola3971df52011-01-25 19:09:56 +00001557 if (F->hasUnnamedAddr())
1558 Out << " unnamed_addr";
Devang Patel19c87462008-09-26 22:53:05 +00001559 Attributes FnAttrs = Attrs.getFnAttributes();
1560 if (FnAttrs != Attribute::None)
1561 Out << ' ' << Attribute::getAsString(Attrs.getFnAttributes());
Chris Lattner8fff1262010-07-07 23:16:37 +00001562 if (F->hasSection()) {
1563 Out << " section \"";
1564 PrintEscapedString(F->getSection(), Out);
1565 Out << '"';
1566 }
Chris Lattner30caa282005-11-06 06:48:53 +00001567 if (F->getAlignment())
1568 Out << " align " << F->getAlignment();
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001569 if (F->hasGC())
1570 Out << " gc \"" << F->getGC() << '"';
Reid Spencer5cbf9852007-01-30 20:08:39 +00001571 if (F->isDeclaration()) {
Chris Lattner91fb4072010-09-02 22:52:10 +00001572 Out << '\n';
Chris Lattner03e2acb2002-05-06 03:00:40 +00001573 } else {
Chris Lattner91fb4072010-09-02 22:52:10 +00001574 Out << " {";
1575 // Output all of the function's basic blocks.
Chris Lattner7e708292002-06-25 16:13:24 +00001576 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
1577 printBasicBlock(I);
Chris Lattner007377f2001-09-07 16:36:04 +00001578
Misha Brukman0313e0b2004-06-21 21:53:56 +00001579 Out << "}\n";
Chris Lattner007377f2001-09-07 16:36:04 +00001580 }
1581
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001582 Machine.purgeFunction();
Chris Lattner00950542001-06-06 20:29:01 +00001583}
1584
Misha Brukmanab5c6002004-03-02 00:22:19 +00001585/// printArgument - This member is called for every argument that is passed into
1586/// the function. Simply print it out
1587///
Daniel Dunbara279bc32009-09-20 02:20:51 +00001588void AssemblyWriter::printArgument(const Argument *Arg,
Devang Pateleaf42ab2008-09-23 23:03:40 +00001589 Attributes Attrs) {
Chris Lattner00950542001-06-06 20:29:01 +00001590 // Output type...
Chris Lattner0f7364b2009-02-28 21:26:53 +00001591 TypePrinter.print(Arg->getType(), Out);
Misha Brukmanfd939082005-04-21 23:48:37 +00001592
Duncan Sandsdc024672007-11-27 13:23:08 +00001593 // Output parameter attributes list
Devang Patel05988662008-09-25 21:00:45 +00001594 if (Attrs != Attribute::None)
1595 Out << ' ' << Attribute::getAsString(Attrs);
Reid Spencerbd5db8e2006-12-31 05:24:50 +00001596
Chris Lattner00950542001-06-06 20:29:01 +00001597 // Output name, if available...
Chris Lattnerc97536e2008-08-17 04:40:13 +00001598 if (Arg->hasName()) {
1599 Out << ' ';
1600 PrintLLVMName(Out, Arg);
1601 }
Chris Lattner00950542001-06-06 20:29:01 +00001602}
1603
Misha Brukmanab5c6002004-03-02 00:22:19 +00001604/// printBasicBlock - This member is called for each basic block in a method.
1605///
Chris Lattnerc1824992001-10-29 16:05:51 +00001606void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Nick Lewycky280a6e62008-04-25 16:53:59 +00001607 if (BB->hasName()) { // Print out the label if it exists...
Chris Lattnerc97536e2008-08-17 04:40:13 +00001608 Out << "\n";
Daniel Dunbar03d76512009-07-25 23:55:21 +00001609 PrintLLVMName(Out, BB->getName(), LabelPrefix);
Chris Lattnerc97536e2008-08-17 04:40:13 +00001610 Out << ':';
Nick Lewycky280a6e62008-04-25 16:53:59 +00001611 } else if (!BB->use_empty()) { // Don't print block # of no uses...
Bill Wendling5d7a5a42011-04-10 23:18:04 +00001612 Out << "\n; <label>:";
Chris Lattner22379bc2007-01-11 03:54:27 +00001613 int Slot = Machine.getLocalSlot(BB);
Chris Lattner69566452004-06-09 19:41:19 +00001614 if (Slot != -1)
Misha Brukman0313e0b2004-06-21 21:53:56 +00001615 Out << Slot;
Chris Lattner69566452004-06-09 19:41:19 +00001616 else
Misha Brukman0313e0b2004-06-21 21:53:56 +00001617 Out << "<badref>";
Chris Lattner061269b2002-10-02 19:38:55 +00001618 }
Chris Lattner4e4d8622003-11-20 00:09:43 +00001619
Dan Gohman683e9222009-08-12 17:23:50 +00001620 if (BB->getParent() == 0) {
Chris Lattner8f4b1ec2009-08-17 15:48:08 +00001621 Out.PadToColumn(50);
Dan Gohman683e9222009-08-12 17:23:50 +00001622 Out << "; Error: Block without parent!";
1623 } else if (BB != &BB->getParent()->getEntryBlock()) { // Not the entry block?
Chris Lattner91fb4072010-09-02 22:52:10 +00001624 // Output predecessors for the block.
Chris Lattner8f4b1ec2009-08-17 15:48:08 +00001625 Out.PadToColumn(50);
Dan Gohman683e9222009-08-12 17:23:50 +00001626 Out << ";";
Gabor Greif44424642010-03-25 23:25:28 +00001627 const_pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001628
Chris Lattnereb411292008-04-22 02:45:44 +00001629 if (PI == PE) {
1630 Out << " No predecessors!";
1631 } else {
Dan Gohman8dae1382008-09-14 17:21:12 +00001632 Out << " preds = ";
Chris Lattnereb411292008-04-22 02:45:44 +00001633 writeOperand(*PI, false);
1634 for (++PI; PI != PE; ++PI) {
Dan Gohman8dae1382008-09-14 17:21:12 +00001635 Out << ", ";
Chris Lattner2fcfdb72006-12-06 06:24:27 +00001636 writeOperand(*PI, false);
Chris Lattner40efcec2003-11-16 22:59:57 +00001637 }
Chris Lattner061269b2002-10-02 19:38:55 +00001638 }
Chris Lattner00950542001-06-06 20:29:01 +00001639 }
Misha Brukmanfd939082005-04-21 23:48:37 +00001640
Chris Lattnereb411292008-04-22 02:45:44 +00001641 Out << "\n";
Chris Lattner00950542001-06-06 20:29:01 +00001642
Misha Brukman0313e0b2004-06-21 21:53:56 +00001643 if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, Out);
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001644
Chris Lattner007377f2001-09-07 16:36:04 +00001645 // Output all of the instructions in the basic block...
Dan Gohmanbeca6892009-07-13 18:27:59 +00001646 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
Chris Lattner7e708292002-06-25 16:13:24 +00001647 printInstruction(*I);
Dan Gohmanbeca6892009-07-13 18:27:59 +00001648 Out << '\n';
1649 }
Chris Lattner9f717ef2004-03-08 18:51:45 +00001650
Misha Brukman0313e0b2004-06-21 21:53:56 +00001651 if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, Out);
Chris Lattner00950542001-06-06 20:29:01 +00001652}
1653
Misha Brukmanab5c6002004-03-02 00:22:19 +00001654/// printInfoComment - Print a little comment after the instruction indicating
1655/// which slot it occupies.
1656///
Chris Lattner7e708292002-06-25 16:13:24 +00001657void AssemblyWriter::printInfoComment(const Value &V) {
Dan Gohman7a5666e2010-02-10 20:41:46 +00001658 if (AnnotationWriter) {
1659 AnnotationWriter->printInfoComment(V, Out);
1660 return;
1661 }
Chris Lattnere02fa852001-10-13 06:42:36 +00001662}
1663
Reid Spencer3a9ec242006-08-28 01:02:49 +00001664// This member is called for each Instruction in a function..
Chris Lattner7e708292002-06-25 16:13:24 +00001665void AssemblyWriter::printInstruction(const Instruction &I) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001666 if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, Out);
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001667
Dan Gohman3845e502009-08-12 23:32:33 +00001668 // Print out indentation for an instruction.
Dan Gohman01889ca2009-08-13 01:41:52 +00001669 Out << " ";
Chris Lattner00950542001-06-06 20:29:01 +00001670
1671 // Print out name if it exists...
Chris Lattnerc97536e2008-08-17 04:40:13 +00001672 if (I.hasName()) {
1673 PrintLLVMName(Out, &I);
1674 Out << " = ";
Chris Lattner4ee93c42009-12-29 07:25:48 +00001675 } else if (!I.getType()->isVoidTy()) {
Chris Lattner828db8a2008-08-29 17:19:30 +00001676 // Print out the def slot taken.
1677 int SlotNum = Machine.getLocalSlot(&I);
1678 if (SlotNum == -1)
1679 Out << "<badref> = ";
1680 else
1681 Out << '%' << SlotNum << " = ";
Chris Lattnerc97536e2008-08-17 04:40:13 +00001682 }
Andrew Trick18801ec2011-09-30 19:48:58 +00001683
Eli Friedman21006d42011-08-09 23:02:53 +00001684 if (isa<CallInst>(I) && cast<CallInst>(I).isTailCall())
Chris Lattnerddb6db42005-05-06 05:51:46 +00001685 Out << "tail ";
Chris Lattnere5e475e2003-09-08 17:45:59 +00001686
Chris Lattner00950542001-06-06 20:29:01 +00001687 // Print out the opcode...
Misha Brukman0313e0b2004-06-21 21:53:56 +00001688 Out << I.getOpcodeName();
Chris Lattner00950542001-06-06 20:29:01 +00001689
Eli Friedmanf03bb262011-08-12 22:50:01 +00001690 // If this is an atomic load or store, print out the atomic marker.
1691 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isAtomic()) ||
1692 (isa<StoreInst>(I) && cast<StoreInst>(I).isAtomic()))
1693 Out << " atomic";
1694
1695 // If this is a volatile operation, print out the volatile marker.
1696 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) ||
1697 (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile()) ||
1698 (isa<AtomicCmpXchgInst>(I) && cast<AtomicCmpXchgInst>(I).isVolatile()) ||
1699 (isa<AtomicRMWInst>(I) && cast<AtomicRMWInst>(I).isVolatile()))
1700 Out << " volatile";
1701
Dan Gohman59858cf2009-07-27 16:11:46 +00001702 // Print out optimization information.
1703 WriteOptimizationInfo(Out, &I);
1704
Reid Spencer74f16422006-12-03 06:27:29 +00001705 // Print out the compare instruction predicates
Nate Begemanac80ade2008-05-12 19:01:56 +00001706 if (const CmpInst *CI = dyn_cast<CmpInst>(&I))
Chris Lattnerab49ee72008-08-23 22:52:27 +00001707 Out << ' ' << getPredicateText(CI->getPredicate());
Reid Spencer74f16422006-12-03 06:27:29 +00001708
Eli Friedmanff030482011-07-28 21:48:00 +00001709 // Print out the atomicrmw operation
1710 if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(&I))
1711 writeAtomicRMWOperation(Out, RMWI->getOperation());
1712
Chris Lattner00950542001-06-06 20:29:01 +00001713 // Print out the type of the operands...
Chris Lattner7e708292002-06-25 16:13:24 +00001714 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0;
Chris Lattner00950542001-06-06 20:29:01 +00001715
1716 // Special case conditional branches to swizzle the condition out to the front
Gabor Greifccd27fb2009-02-09 15:45:06 +00001717 if (isa<BranchInst>(I) && cast<BranchInst>(I).isConditional()) {
1718 BranchInst &BI(cast<BranchInst>(I));
Dan Gohman8dae1382008-09-14 17:21:12 +00001719 Out << ' ';
Gabor Greifccd27fb2009-02-09 15:45:06 +00001720 writeOperand(BI.getCondition(), true);
Dan Gohman8dae1382008-09-14 17:21:12 +00001721 Out << ", ";
Gabor Greifccd27fb2009-02-09 15:45:06 +00001722 writeOperand(BI.getSuccessor(0), true);
Dan Gohman8dae1382008-09-14 17:21:12 +00001723 Out << ", ";
Gabor Greifccd27fb2009-02-09 15:45:06 +00001724 writeOperand(BI.getSuccessor(1), true);
Chris Lattner00950542001-06-06 20:29:01 +00001725
Chris Lattner94dc1f22002-04-13 18:34:38 +00001726 } else if (isa<SwitchInst>(I)) {
Eli Friedmanbb5a7442011-09-29 20:21:17 +00001727 SwitchInst& SI(cast<SwitchInst>(I));
Chris Lattnerf9be95f2009-10-27 19:13:16 +00001728 // Special case switch instruction to get formatting nice and correct.
Dan Gohman8dae1382008-09-14 17:21:12 +00001729 Out << ' ';
Eli Friedmanbb5a7442011-09-29 20:21:17 +00001730 writeOperand(SI.getCondition(), true);
Dan Gohman8dae1382008-09-14 17:21:12 +00001731 Out << ", ";
Eli Friedmanbb5a7442011-09-29 20:21:17 +00001732 writeOperand(SI.getDefaultDest(), true);
Chris Lattnerab49ee72008-08-23 22:52:27 +00001733 Out << " [";
Stepan Dyatkovskiy3d3abe02012-03-11 06:09:17 +00001734 for (SwitchInst::CaseIt i = SI.case_begin(), e = SI.case_end();
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00001735 i != e; ++i) {
Dan Gohman01889ca2009-08-13 01:41:52 +00001736 Out << "\n ";
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00001737 writeOperand(i.getCaseValue(), true);
Dan Gohman8dae1382008-09-14 17:21:12 +00001738 Out << ", ";
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00001739 writeOperand(i.getCaseSuccessor(), true);
Chris Lattner00950542001-06-06 20:29:01 +00001740 }
Dan Gohman01889ca2009-08-13 01:41:52 +00001741 Out << "\n ]";
Chris Lattnerab21db72009-10-28 00:19:10 +00001742 } else if (isa<IndirectBrInst>(I)) {
1743 // Special case indirectbr instruction to get formatting nice and correct.
Chris Lattnerf9be95f2009-10-27 19:13:16 +00001744 Out << ' ';
1745 writeOperand(Operand, true);
Dan Gohman0ed1f422009-10-30 02:01:10 +00001746 Out << ", [";
Andrew Trick18801ec2011-09-30 19:48:58 +00001747
Chris Lattnerf9be95f2009-10-27 19:13:16 +00001748 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) {
1749 if (i != 1)
1750 Out << ", ";
1751 writeOperand(I.getOperand(i), true);
1752 }
1753 Out << ']';
Jay Foadc1371202011-06-20 14:18:48 +00001754 } else if (const PHINode *PN = dyn_cast<PHINode>(&I)) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001755 Out << ' ';
Chris Lattner0f7364b2009-02-28 21:26:53 +00001756 TypePrinter.print(I.getType(), Out);
Misha Brukman0313e0b2004-06-21 21:53:56 +00001757 Out << ' ';
Chris Lattner00950542001-06-06 20:29:01 +00001758
Jay Foadc1371202011-06-20 14:18:48 +00001759 for (unsigned op = 0, Eop = PN->getNumIncomingValues(); op < Eop; ++op) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001760 if (op) Out << ", ";
Dan Gohman8dae1382008-09-14 17:21:12 +00001761 Out << "[ ";
Jay Foadc1371202011-06-20 14:18:48 +00001762 writeOperand(PN->getIncomingValue(op), false); Out << ", ";
1763 writeOperand(PN->getIncomingBlock(op), false); Out << " ]";
Chris Lattnerc24d2082001-06-11 15:04:20 +00001764 }
Dan Gohman995be7d2008-05-31 19:12:39 +00001765 } else if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(&I)) {
Dan Gohman8dae1382008-09-14 17:21:12 +00001766 Out << ' ';
Dan Gohman995be7d2008-05-31 19:12:39 +00001767 writeOperand(I.getOperand(0), true);
1768 for (const unsigned *i = EVI->idx_begin(), *e = EVI->idx_end(); i != e; ++i)
1769 Out << ", " << *i;
1770 } else if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(&I)) {
Dan Gohman8dae1382008-09-14 17:21:12 +00001771 Out << ' ';
1772 writeOperand(I.getOperand(0), true); Out << ", ";
Dan Gohman995be7d2008-05-31 19:12:39 +00001773 writeOperand(I.getOperand(1), true);
1774 for (const unsigned *i = IVI->idx_begin(), *e = IVI->idx_end(); i != e; ++i)
1775 Out << ", " << *i;
Bill Wendlinge6e88262011-08-12 20:24:12 +00001776 } else if (const LandingPadInst *LPI = dyn_cast<LandingPadInst>(&I)) {
1777 Out << ' ';
1778 TypePrinter.print(I.getType(), Out);
1779 Out << " personality ";
1780 writeOperand(I.getOperand(0), true); Out << '\n';
1781
1782 if (LPI->isCleanup())
1783 Out << " cleanup";
1784
1785 for (unsigned i = 0, e = LPI->getNumClauses(); i != e; ++i) {
1786 if (i != 0 || LPI->isCleanup()) Out << "\n";
1787 if (LPI->isCatch(i))
1788 Out << " catch ";
1789 else
1790 Out << " filter ";
1791
1792 writeOperand(LPI->getClause(i), true);
1793 }
Devang Patel57ef4f42008-02-23 00:35:18 +00001794 } else if (isa<ReturnInst>(I) && !Operand) {
1795 Out << " void";
Chris Lattnerd5118982005-05-06 20:26:43 +00001796 } else if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
1797 // Print the calling convention being used.
1798 switch (CI->getCallingConv()) {
1799 case CallingConv::C: break; // default
Chris Lattner0deaab82006-05-19 21:58:52 +00001800 case CallingConv::Fast: Out << " fastcc"; break;
1801 case CallingConv::Cold: Out << " coldcc"; break;
Chris Lattnerb28a6dc2007-11-18 18:32:16 +00001802 case CallingConv::X86_StdCall: Out << " x86_stdcallcc"; break;
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001803 case CallingConv::X86_FastCall: Out << " x86_fastcallcc"; break;
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001804 case CallingConv::X86_ThisCall: Out << " x86_thiscallcc"; break;
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001805 case CallingConv::ARM_APCS: Out << " arm_apcscc "; break;
1806 case CallingConv::ARM_AAPCS: Out << " arm_aapcscc "; break;
1807 case CallingConv::ARM_AAPCS_VFP:Out << " arm_aapcs_vfpcc "; break;
Anton Korobeynikov211a14e2009-12-07 02:27:35 +00001808 case CallingConv::MSP430_INTR: Out << " msp430_intrcc "; break;
Che-Liang Chiouf9930da2010-09-25 07:46:17 +00001809 case CallingConv::PTX_Kernel: Out << " ptx_kernel"; break;
1810 case CallingConv::PTX_Device: Out << " ptx_device"; break;
Chris Lattnerd5118982005-05-06 20:26:43 +00001811 default: Out << " cc" << CI->getCallingConv(); break;
1812 }
1813
Gabor Greif7bbdf0c2010-06-23 13:09:06 +00001814 Operand = CI->getCalledValue();
Chris Lattner1afcace2011-07-09 17:41:24 +00001815 PointerType *PTy = cast<PointerType>(Operand->getType());
1816 FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1817 Type *RetTy = FTy->getReturnType();
Devang Patel05988662008-09-25 21:00:45 +00001818 const AttrListPtr &PAL = CI->getAttributes();
Chris Lattner268de042001-11-06 21:28:12 +00001819
Devang Patel652203f2008-09-29 20:49:50 +00001820 if (PAL.getRetAttributes() != Attribute::None)
1821 Out << ' ' << Attribute::getAsString(PAL.getRetAttributes());
1822
Chris Lattner7a012292003-08-05 15:34:45 +00001823 // If possible, print out the short form of the call instruction. We can
Chris Lattnerb5794002002-04-07 22:49:37 +00001824 // only do this if the first argument is a pointer to a nonvararg function,
Chris Lattner7a012292003-08-05 15:34:45 +00001825 // and if the return type is not a pointer to a function.
Chris Lattner268de042001-11-06 21:28:12 +00001826 //
Dan Gohman8dae1382008-09-14 17:21:12 +00001827 Out << ' ';
Chris Lattner7a012292003-08-05 15:34:45 +00001828 if (!FTy->isVarArg() &&
Duncan Sands1df98592010-02-16 11:11:14 +00001829 (!RetTy->isPointerTy() ||
1830 !cast<PointerType>(RetTy)->getElementType()->isFunctionTy())) {
Chris Lattner0f7364b2009-02-28 21:26:53 +00001831 TypePrinter.print(RetTy, Out);
Dan Gohman8dae1382008-09-14 17:21:12 +00001832 Out << ' ';
Chris Lattner268de042001-11-06 21:28:12 +00001833 writeOperand(Operand, false);
1834 } else {
1835 writeOperand(Operand, true);
1836 }
Misha Brukman0313e0b2004-06-21 21:53:56 +00001837 Out << '(';
Gabor Greif7bbdf0c2010-06-23 13:09:06 +00001838 for (unsigned op = 0, Eop = CI->getNumArgOperands(); op < Eop; ++op) {
1839 if (op > 0)
Dan Gohman8dae1382008-09-14 17:21:12 +00001840 Out << ", ";
Gabor Greif7bbdf0c2010-06-23 13:09:06 +00001841 writeParamOperand(CI->getArgOperand(op), PAL.getParamAttributes(op + 1));
Chris Lattner00950542001-06-06 20:29:01 +00001842 }
Dan Gohman8dae1382008-09-14 17:21:12 +00001843 Out << ')';
Devang Patel19c87462008-09-26 22:53:05 +00001844 if (PAL.getFnAttributes() != Attribute::None)
1845 Out << ' ' << Attribute::getAsString(PAL.getFnAttributes());
Chris Lattner7e708292002-06-25 16:13:24 +00001846 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
Gabor Greifc9f75002010-03-24 13:21:49 +00001847 Operand = II->getCalledValue();
Chris Lattner1afcace2011-07-09 17:41:24 +00001848 PointerType *PTy = cast<PointerType>(Operand->getType());
1849 FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1850 Type *RetTy = FTy->getReturnType();
Devang Patel05988662008-09-25 21:00:45 +00001851 const AttrListPtr &PAL = II->getAttributes();
Chris Lattner7a012292003-08-05 15:34:45 +00001852
Chris Lattnerd5118982005-05-06 20:26:43 +00001853 // Print the calling convention being used.
1854 switch (II->getCallingConv()) {
1855 case CallingConv::C: break; // default
Chris Lattner0deaab82006-05-19 21:58:52 +00001856 case CallingConv::Fast: Out << " fastcc"; break;
1857 case CallingConv::Cold: Out << " coldcc"; break;
Dan Gohman8dae1382008-09-14 17:21:12 +00001858 case CallingConv::X86_StdCall: Out << " x86_stdcallcc"; break;
1859 case CallingConv::X86_FastCall: Out << " x86_fastcallcc"; break;
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001860 case CallingConv::X86_ThisCall: Out << " x86_thiscallcc"; break;
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001861 case CallingConv::ARM_APCS: Out << " arm_apcscc "; break;
1862 case CallingConv::ARM_AAPCS: Out << " arm_aapcscc "; break;
1863 case CallingConv::ARM_AAPCS_VFP:Out << " arm_aapcs_vfpcc "; break;
Anton Korobeynikov211a14e2009-12-07 02:27:35 +00001864 case CallingConv::MSP430_INTR: Out << " msp430_intrcc "; break;
Che-Liang Chiouf9930da2010-09-25 07:46:17 +00001865 case CallingConv::PTX_Kernel: Out << " ptx_kernel"; break;
1866 case CallingConv::PTX_Device: Out << " ptx_device"; break;
Chris Lattnerd5118982005-05-06 20:26:43 +00001867 default: Out << " cc" << II->getCallingConv(); break;
1868 }
1869
Devang Patel652203f2008-09-29 20:49:50 +00001870 if (PAL.getRetAttributes() != Attribute::None)
1871 Out << ' ' << Attribute::getAsString(PAL.getRetAttributes());
1872
Chris Lattner7a012292003-08-05 15:34:45 +00001873 // If possible, print out the short form of the invoke instruction. We can
1874 // only do this if the first argument is a pointer to a nonvararg function,
1875 // and if the return type is not a pointer to a function.
1876 //
Dan Gohman2b6c3d92008-10-15 18:02:08 +00001877 Out << ' ';
Chris Lattner7a012292003-08-05 15:34:45 +00001878 if (!FTy->isVarArg() &&
Duncan Sands1df98592010-02-16 11:11:14 +00001879 (!RetTy->isPointerTy() ||
1880 !cast<PointerType>(RetTy)->getElementType()->isFunctionTy())) {
Chris Lattner0f7364b2009-02-28 21:26:53 +00001881 TypePrinter.print(RetTy, Out);
Dan Gohman2b6c3d92008-10-15 18:02:08 +00001882 Out << ' ';
Chris Lattner7a012292003-08-05 15:34:45 +00001883 writeOperand(Operand, false);
1884 } else {
1885 writeOperand(Operand, true);
1886 }
Misha Brukman0313e0b2004-06-21 21:53:56 +00001887 Out << '(';
Gabor Greif7bbdf0c2010-06-23 13:09:06 +00001888 for (unsigned op = 0, Eop = II->getNumArgOperands(); op < Eop; ++op) {
Gabor Greifc9f75002010-03-24 13:21:49 +00001889 if (op)
Dan Gohman8dae1382008-09-14 17:21:12 +00001890 Out << ", ";
Gabor Greif7bbdf0c2010-06-23 13:09:06 +00001891 writeParamOperand(II->getArgOperand(op), PAL.getParamAttributes(op + 1));
Chris Lattnere02fa852001-10-13 06:42:36 +00001892 }
1893
Dan Gohman8dae1382008-09-14 17:21:12 +00001894 Out << ')';
Devang Patel19c87462008-09-26 22:53:05 +00001895 if (PAL.getFnAttributes() != Attribute::None)
1896 Out << ' ' << Attribute::getAsString(PAL.getFnAttributes());
1897
Dan Gohman01889ca2009-08-13 01:41:52 +00001898 Out << "\n to ";
Chris Lattnere02fa852001-10-13 06:42:36 +00001899 writeOperand(II->getNormalDest(), true);
Dan Gohman8dae1382008-09-14 17:21:12 +00001900 Out << " unwind ";
Chris Lattneraeb2a1d2004-02-08 21:44:31 +00001901 writeOperand(II->getUnwindDest(), true);
Chris Lattnere02fa852001-10-13 06:42:36 +00001902
Victor Hernandez7b929da2009-10-23 21:09:37 +00001903 } else if (const AllocaInst *AI = dyn_cast<AllocaInst>(&I)) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001904 Out << ' ';
Chris Lattner0f7364b2009-02-28 21:26:53 +00001905 TypePrinter.print(AI->getType()->getElementType(), Out);
Dan Gohman69bff072009-07-31 18:23:24 +00001906 if (!AI->getArraySize() || AI->isArrayAllocation()) {
Dan Gohman8dae1382008-09-14 17:21:12 +00001907 Out << ", ";
Chris Lattner94dc1f22002-04-13 18:34:38 +00001908 writeOperand(AI->getArraySize(), true);
Chris Lattner00950542001-06-06 20:29:01 +00001909 }
Nate Begeman14b05292005-11-05 09:21:28 +00001910 if (AI->getAlignment()) {
Chris Lattner9fad0b92005-11-05 21:20:34 +00001911 Out << ", align " << AI->getAlignment();
Nate Begeman14b05292005-11-05 09:21:28 +00001912 }
Chris Lattnere02fa852001-10-13 06:42:36 +00001913 } else if (isa<CastInst>(I)) {
Dan Gohman8dae1382008-09-14 17:21:12 +00001914 if (Operand) {
1915 Out << ' ';
1916 writeOperand(Operand, true); // Work with broken code
1917 }
Misha Brukman0313e0b2004-06-21 21:53:56 +00001918 Out << " to ";
Chris Lattner0f7364b2009-02-28 21:26:53 +00001919 TypePrinter.print(I.getType(), Out);
Chris Lattner4d45bd02003-10-18 05:57:43 +00001920 } else if (isa<VAArgInst>(I)) {
Dan Gohman8dae1382008-09-14 17:21:12 +00001921 if (Operand) {
1922 Out << ' ';
1923 writeOperand(Operand, true); // Work with broken code
1924 }
Misha Brukman0313e0b2004-06-21 21:53:56 +00001925 Out << ", ";
Chris Lattner0f7364b2009-02-28 21:26:53 +00001926 TypePrinter.print(I.getType(), Out);
1927 } else if (Operand) { // Print the normal way.
Chris Lattner00950542001-06-06 20:29:01 +00001928
Misha Brukmanfd939082005-04-21 23:48:37 +00001929 // PrintAllTypes - Instructions who have operands of all the same type
Chris Lattner00950542001-06-06 20:29:01 +00001930 // omit the type from all but the first operand. If the instruction has
1931 // different type operands (for example br), then they are all printed.
1932 bool PrintAllTypes = false;
Chris Lattner1afcace2011-07-09 17:41:24 +00001933 Type *TheType = Operand->getType();
Chris Lattner00950542001-06-06 20:29:01 +00001934
Reid Spencerebe57e32007-02-02 13:54:55 +00001935 // Select, Store and ShuffleVector always print all types.
Devang Patel64947682008-03-04 22:05:14 +00001936 if (isa<SelectInst>(I) || isa<StoreInst>(I) || isa<ShuffleVectorInst>(I)
1937 || isa<ReturnInst>(I)) {
Chris Lattnerffd9bf42003-04-16 20:20:02 +00001938 PrintAllTypes = true;
1939 } else {
1940 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
1941 Operand = I.getOperand(i);
Nuno Lopes6ad2b2a2009-01-15 18:40:57 +00001942 // note that Operand shouldn't be null, but the test helps make dump()
1943 // more tolerant of malformed IR
Nuno Lopesa8c78a92009-01-14 17:51:41 +00001944 if (Operand && Operand->getType() != TheType) {
Chris Lattnerffd9bf42003-04-16 20:20:02 +00001945 PrintAllTypes = true; // We have differing types! Print them all!
1946 break;
1947 }
Chris Lattner00950542001-06-06 20:29:01 +00001948 }
1949 }
Misha Brukmanfd939082005-04-21 23:48:37 +00001950
Chris Lattnerc1824992001-10-29 16:05:51 +00001951 if (!PrintAllTypes) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001952 Out << ' ';
Chris Lattner0f7364b2009-02-28 21:26:53 +00001953 TypePrinter.print(TheType, Out);
Chris Lattnerc1824992001-10-29 16:05:51 +00001954 }
Chris Lattner00950542001-06-06 20:29:01 +00001955
Dan Gohman8dae1382008-09-14 17:21:12 +00001956 Out << ' ';
Chris Lattner7e708292002-06-25 16:13:24 +00001957 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
Dan Gohman8dae1382008-09-14 17:21:12 +00001958 if (i) Out << ", ";
Chris Lattner7e708292002-06-25 16:13:24 +00001959 writeOperand(I.getOperand(i), PrintAllTypes);
Chris Lattner00950542001-06-06 20:29:01 +00001960 }
1961 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001962
Eli Friedman21006d42011-08-09 23:02:53 +00001963 // Print atomic ordering/alignment for memory operations
1964 if (const LoadInst *LI = dyn_cast<LoadInst>(&I)) {
1965 if (LI->isAtomic())
1966 writeAtomic(LI->getOrdering(), LI->getSynchScope());
1967 if (LI->getAlignment())
1968 Out << ", align " << LI->getAlignment();
1969 } else if (const StoreInst *SI = dyn_cast<StoreInst>(&I)) {
1970 if (SI->isAtomic())
1971 writeAtomic(SI->getOrdering(), SI->getSynchScope());
1972 if (SI->getAlignment())
1973 Out << ", align " << SI->getAlignment();
Eli Friedmanff030482011-07-28 21:48:00 +00001974 } else if (const AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(&I)) {
1975 writeAtomic(CXI->getOrdering(), CXI->getSynchScope());
1976 } else if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(&I)) {
1977 writeAtomic(RMWI->getOrdering(), RMWI->getSynchScope());
Eli Friedman47f35132011-07-25 23:16:38 +00001978 } else if (const FenceInst *FI = dyn_cast<FenceInst>(&I)) {
1979 writeAtomic(FI->getOrdering(), FI->getSynchScope());
Christopher Lamb43c7f372007-04-22 19:24:39 +00001980 }
Chris Lattner00950542001-06-06 20:29:01 +00001981
Chris Lattner7d05c462009-12-28 20:10:43 +00001982 // Print Metadata info.
Nick Lewyckyfa0c54e2010-02-25 06:53:04 +00001983 SmallVector<std::pair<unsigned, MDNode*>, 4> InstMD;
1984 I.getAllMetadata(InstMD);
Erick Tryzelaarf3d5c912010-03-02 05:32:52 +00001985 if (!InstMD.empty()) {
1986 SmallVector<StringRef, 8> MDNames;
1987 I.getType()->getContext().getMDKindNames(MDNames);
1988 for (unsigned i = 0, e = InstMD.size(); i != e; ++i) {
1989 unsigned Kind = InstMD[i].first;
1990 if (Kind < MDNames.size()) {
1991 Out << ", !" << MDNames[Kind];
1992 } else {
1993 Out << ", !<unknown kind #" << Kind << ">";
1994 }
Dan Gohman3bdfbf52010-07-20 23:55:01 +00001995 Out << ' ';
1996 WriteAsOperandInternal(Out, InstMD[i].second, &TypePrinter, &Machine,
1997 TheModule);
Nick Lewyckyfa0c54e2010-02-25 06:53:04 +00001998 }
Devang Patel7f93f4d2009-10-07 16:37:55 +00001999 }
Chris Lattnere02fa852001-10-13 06:42:36 +00002000 printInfoComment(I);
Chris Lattner00950542001-06-06 20:29:01 +00002001}
2002
Chris Lattner6e6b1802009-12-31 02:13:35 +00002003static void WriteMDNodeComment(const MDNode *Node,
Duncan Sands34727662010-07-12 08:16:59 +00002004 formatted_raw_ostream &Out) {
Chris Lattner6e6b1802009-12-31 02:13:35 +00002005 if (Node->getNumOperands() < 1)
2006 return;
2007 ConstantInt *CI = dyn_cast_or_null<ConstantInt>(Node->getOperand(0));
2008 if (!CI) return;
Dan Gohman66797ff2010-05-07 22:15:24 +00002009 APInt Val = CI->getValue();
2010 APInt Tag = Val & ~APInt(Val.getBitWidth(), LLVMDebugVersionMask);
Devang Patel0df82342012-02-04 01:30:01 +00002011 if (Val.ult(LLVMDebugVersion11))
Chris Lattner6e6b1802009-12-31 02:13:35 +00002012 return;
Andrew Trick18801ec2011-09-30 19:48:58 +00002013
Chris Lattner6e6b1802009-12-31 02:13:35 +00002014 Out.PadToColumn(50);
Devang Patel29020a32011-02-18 23:13:40 +00002015 if (Tag == dwarf::DW_TAG_user_base)
Chris Lattner6e6b1802009-12-31 02:13:35 +00002016 Out << "; [ DW_TAG_user_base ]";
Dan Gohman66797ff2010-05-07 22:15:24 +00002017 else if (Tag.isIntN(32)) {
2018 if (const char *TagName = dwarf::TagString(Tag.getZExtValue()))
2019 Out << "; [ " << TagName << " ]";
2020 }
Chris Lattner6e6b1802009-12-31 02:13:35 +00002021}
2022
2023void AssemblyWriter::writeAllMDNodes() {
2024 SmallVector<const MDNode *, 16> Nodes;
Chris Lattner307c9892009-12-31 02:20:11 +00002025 Nodes.resize(Machine.mdn_size());
2026 for (SlotTracker::mdn_iterator I = Machine.mdn_begin(), E = Machine.mdn_end();
2027 I != E; ++I)
Chris Lattner6e6b1802009-12-31 02:13:35 +00002028 Nodes[I->second] = cast<MDNode>(I->first);
Andrew Trick18801ec2011-09-30 19:48:58 +00002029
Chris Lattner6e6b1802009-12-31 02:13:35 +00002030 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
2031 Out << '!' << i << " = metadata ";
Chris Lattner2b4b1e22009-12-31 02:27:30 +00002032 printMDNodeBody(Nodes[i]);
Chris Lattner6e6b1802009-12-31 02:13:35 +00002033 }
2034}
2035
2036void AssemblyWriter::printMDNodeBody(const MDNode *Node) {
Dan Gohman3bdfbf52010-07-20 23:55:01 +00002037 WriteMDNodeBodyInternal(Out, Node, &TypePrinter, &Machine, TheModule);
Chris Lattner6e6b1802009-12-31 02:13:35 +00002038 WriteMDNodeComment(Node, Out);
2039 Out << "\n";
2040}
Chris Lattner00950542001-06-06 20:29:01 +00002041
2042//===----------------------------------------------------------------------===//
2043// External Interface declarations
2044//===----------------------------------------------------------------------===//
2045
Dan Gohman683e9222009-08-12 17:23:50 +00002046void Module::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW) const {
Chris Lattner0d9574a2008-08-19 04:26:57 +00002047 SlotTracker SlotTable(this);
Dan Gohman683e9222009-08-12 17:23:50 +00002048 formatted_raw_ostream OS(ROS);
Chris Lattner944fac72008-08-23 22:23:09 +00002049 AssemblyWriter W(OS, SlotTable, this, AAW);
Chris Lattnerbd72b322009-12-31 02:23:35 +00002050 W.printModule(this);
Chris Lattner00950542001-06-06 20:29:01 +00002051}
2052
Dan Gohman17aa92c2010-07-21 23:38:33 +00002053void NamedMDNode::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW) const {
2054 SlotTracker SlotTable(getParent());
2055 formatted_raw_ostream OS(ROS);
2056 AssemblyWriter W(OS, SlotTable, getParent(), AAW);
2057 W.printNamedMDNode(this);
2058}
2059
Chris Lattner6d4306e2009-02-28 21:11:05 +00002060void Type::print(raw_ostream &OS) const {
2061 if (this == 0) {
2062 OS << "<null Type>";
2063 return;
2064 }
Chris Lattner1afcace2011-07-09 17:41:24 +00002065 TypePrinting TP;
2066 TP.print(const_cast<Type*>(this), OS);
Andrew Trick18801ec2011-09-30 19:48:58 +00002067
Chris Lattner1afcace2011-07-09 17:41:24 +00002068 // If the type is a named struct type, print the body as well.
2069 if (StructType *STy = dyn_cast<StructType>(const_cast<Type*>(this)))
Chris Lattnerc4d0e9f2011-08-12 18:07:07 +00002070 if (!STy->isLiteral()) {
Chris Lattner1afcace2011-07-09 17:41:24 +00002071 OS << " = type ";
2072 TP.printStructBody(STy, OS);
2073 }
Chris Lattner75cf7cf2002-04-08 22:03:40 +00002074}
2075
Dan Gohman683e9222009-08-12 17:23:50 +00002076void Value::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW) const {
Chris Lattner944fac72008-08-23 22:23:09 +00002077 if (this == 0) {
Dan Gohman1220e102009-08-12 20:56:03 +00002078 ROS << "printing a <null> value\n";
Chris Lattner944fac72008-08-23 22:23:09 +00002079 return;
2080 }
Dan Gohman1220e102009-08-12 20:56:03 +00002081 formatted_raw_ostream OS(ROS);
Chris Lattner944fac72008-08-23 22:23:09 +00002082 if (const Instruction *I = dyn_cast<Instruction>(this)) {
2083 const Function *F = I->getParent() ? I->getParent()->getParent() : 0;
2084 SlotTracker SlotTable(F);
Chris Lattnerdbe85bf2009-12-31 08:23:09 +00002085 AssemblyWriter W(OS, SlotTable, getModuleFromVal(I), AAW);
Chris Lattnerbd72b322009-12-31 02:23:35 +00002086 W.printInstruction(*I);
Chris Lattner944fac72008-08-23 22:23:09 +00002087 } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(this)) {
2088 SlotTracker SlotTable(BB->getParent());
Chris Lattner6e6b1802009-12-31 02:13:35 +00002089 AssemblyWriter W(OS, SlotTable, getModuleFromVal(BB), AAW);
Chris Lattnerbd72b322009-12-31 02:23:35 +00002090 W.printBasicBlock(BB);
Chris Lattner944fac72008-08-23 22:23:09 +00002091 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(this)) {
2092 SlotTracker SlotTable(GV->getParent());
Dan Gohmanba0941f2009-04-20 16:10:33 +00002093 AssemblyWriter W(OS, SlotTable, GV->getParent(), AAW);
Chris Lattnerbd72b322009-12-31 02:23:35 +00002094 if (const GlobalVariable *V = dyn_cast<GlobalVariable>(GV))
2095 W.printGlobal(V);
2096 else if (const Function *F = dyn_cast<Function>(GV))
2097 W.printFunction(F);
2098 else
2099 W.printAlias(cast<GlobalAlias>(GV));
Devang Patelfcd65ae2009-07-01 20:59:15 +00002100 } else if (const MDNode *N = dyn_cast<MDNode>(this)) {
Victor Hernandez8fffff52010-01-20 04:45:57 +00002101 const Function *F = N->getFunction();
Victor Hernandez559588b2010-01-14 01:47:37 +00002102 SlotTracker SlotTable(F);
Dan Gohman79b78a42010-07-14 21:12:44 +00002103 AssemblyWriter W(OS, SlotTable, F ? F->getParent() : 0, AAW);
Chris Lattner6e6b1802009-12-31 02:13:35 +00002104 W.printMDNodeBody(N);
Chris Lattner944fac72008-08-23 22:23:09 +00002105 } else if (const Constant *C = dyn_cast<Constant>(this)) {
Chris Lattnere9fa33e2009-02-28 23:20:19 +00002106 TypePrinting TypePrinter;
Chris Lattner0f7364b2009-02-28 21:26:53 +00002107 TypePrinter.print(C->getType(), OS);
Chris Lattner6d4306e2009-02-28 21:11:05 +00002108 OS << ' ';
Dan Gohman3bdfbf52010-07-20 23:55:01 +00002109 WriteConstantInternal(OS, C, TypePrinter, 0, 0);
Chris Lattner4a3d3a52009-12-31 01:41:14 +00002110 } else if (isa<InlineAsm>(this) || isa<MDString>(this) ||
2111 isa<Argument>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00002112 WriteAsOperand(OS, this, true, 0);
2113 } else {
Dan Gohmancd26ec52009-09-23 01:33:16 +00002114 // Otherwise we don't know what it is. Call the virtual function to
2115 // allow a subclass to print itself.
2116 printCustom(OS);
Chris Lattner944fac72008-08-23 22:23:09 +00002117 }
2118}
2119
Dan Gohmancd26ec52009-09-23 01:33:16 +00002120// Value::printCustom - subclasses should override this to implement printing.
2121void Value::printCustom(raw_ostream &OS) const {
2122 llvm_unreachable("Unknown value to print out!");
2123}
2124
Chris Lattner7059e532008-08-25 17:03:15 +00002125// Value::dump - allow easy printing of Values from the debugger.
David Greened865e022010-01-05 01:29:26 +00002126void Value::dump() const { print(dbgs()); dbgs() << '\n'; }
Reid Spencerfa452c02004-05-25 18:14:38 +00002127
Chris Lattner7059e532008-08-25 17:03:15 +00002128// Type::dump - allow easy printing of Types from the debugger.
Chris Lattner1afcace2011-07-09 17:41:24 +00002129void Type::dump() const { print(dbgs()); }
Chris Lattnerc2871372009-02-28 21:05:51 +00002130
Chris Lattner7059e532008-08-25 17:03:15 +00002131// Module::dump() - Allow printing of Modules from the debugger.
David Greened865e022010-01-05 01:29:26 +00002132void Module::dump() const { print(dbgs(), 0); }
Bill Wendlingf4374e42011-12-09 23:18:34 +00002133
2134// NamedMDNode::dump() - Allow printing of NamedMDNodes from the debugger.
2135void NamedMDNode::dump() const { print(dbgs(), 0); }