blob: f275305cd99fb7ea23a6b2242b0a50d9898bf9ad [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
Daniel Maleaf8c243a2013-05-23 22:34:33 +000017#include "AsmWriter.h"
18
Chris Lattner75cf7cf2002-04-08 22:03:40 +000019#include "llvm/Assembly/Writer.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000020#include "llvm/ADT/DenseMap.h"
21#include "llvm/ADT/STLExtras.h"
22#include "llvm/ADT/SmallString.h"
23#include "llvm/ADT/StringExtras.h"
Chris Lattner1dbb3872010-09-02 23:09:42 +000024#include "llvm/Assembly/AssemblyAnnotationWriter.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000025#include "llvm/Assembly/PrintModulePass.h"
Bill Wendling58a6cf22012-06-28 00:41:44 +000026#include "llvm/DebugInfo.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000027#include "llvm/IR/CallingConv.h"
28#include "llvm/IR/Constants.h"
29#include "llvm/IR/DerivedTypes.h"
30#include "llvm/IR/InlineAsm.h"
31#include "llvm/IR/IntrinsicInst.h"
32#include "llvm/IR/LLVMContext.h"
33#include "llvm/IR/Module.h"
34#include "llvm/IR/Operator.h"
Chandler Carruth4068e1a2013-01-07 15:43:51 +000035#include "llvm/IR/TypeFinder.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000036#include "llvm/IR/ValueSymbolTable.h"
Bill Wendling8f487662006-11-28 02:09:03 +000037#include "llvm/Support/CFG.h"
David Greened865e022010-01-05 01:29:26 +000038#include "llvm/Support/Debug.h"
Devang Patel2d5988d2009-09-30 20:16:54 +000039#include "llvm/Support/Dwarf.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000040#include "llvm/Support/ErrorHandling.h"
Dan Gohman683e9222009-08-12 17:23:50 +000041#include "llvm/Support/FormattedStream.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000042#include "llvm/Support/MathExtras.h"
Daniel Maleaf8c243a2013-05-23 22:34:33 +000043
Chris Lattner007377f2001-09-07 16:36:04 +000044#include <algorithm>
Reid Spencer4ad513c2007-05-22 19:27:35 +000045#include <cctype>
Chris Lattner31f84992003-11-21 20:23:48 +000046using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000047
Reid Spenceredd5d9e2005-05-15 16:13:11 +000048// Make virtual table appear in this compilation unit.
49AssemblyAnnotationWriter::~AssemblyAnnotationWriter() {}
50
Chris Lattner6ab910b2008-08-19 04:36:02 +000051//===----------------------------------------------------------------------===//
52// Helper Functions
53//===----------------------------------------------------------------------===//
54
55static const Module *getModuleFromVal(const Value *V) {
56 if (const Argument *MA = dyn_cast<Argument>(V))
57 return MA->getParent() ? MA->getParent()->getParent() : 0;
Daniel Dunbara279bc32009-09-20 02:20:51 +000058
Chris Lattner6ab910b2008-08-19 04:36:02 +000059 if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
60 return BB->getParent() ? BB->getParent()->getParent() : 0;
Daniel Dunbara279bc32009-09-20 02:20:51 +000061
Chris Lattner6ab910b2008-08-19 04:36:02 +000062 if (const Instruction *I = dyn_cast<Instruction>(V)) {
63 const Function *M = I->getParent() ? I->getParent()->getParent() : 0;
64 return M ? M->getParent() : 0;
65 }
Andrew Trick18801ec2011-09-30 19:48:58 +000066
Chris Lattner6ab910b2008-08-19 04:36:02 +000067 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
68 return GV->getParent();
69 return 0;
70}
71
Bill Wendling7ab6c762013-02-20 07:21:42 +000072static void PrintCallingConv(unsigned cc, raw_ostream &Out) {
Micah Villmowd3766df2012-09-13 15:11:12 +000073 switch (cc) {
Bill Wendling7ab6c762013-02-20 07:21:42 +000074 default: Out << "cc" << cc; break;
75 case CallingConv::Fast: Out << "fastcc"; break;
76 case CallingConv::Cold: Out << "coldcc"; break;
77 case CallingConv::X86_StdCall: Out << "x86_stdcallcc"; break;
78 case CallingConv::X86_FastCall: Out << "x86_fastcallcc"; break;
79 case CallingConv::X86_ThisCall: Out << "x86_thiscallcc"; break;
80 case CallingConv::Intel_OCL_BI: Out << "intel_ocl_bicc"; break;
81 case CallingConv::ARM_APCS: Out << "arm_apcscc"; break;
82 case CallingConv::ARM_AAPCS: Out << "arm_aapcscc"; break;
83 case CallingConv::ARM_AAPCS_VFP: Out << "arm_aapcs_vfpcc"; break;
84 case CallingConv::MSP430_INTR: Out << "msp430_intrcc"; break;
85 case CallingConv::PTX_Kernel: Out << "ptx_kernel"; break;
86 case CallingConv::PTX_Device: Out << "ptx_device"; break;
Charles Davisac226bb2013-07-12 06:02:35 +000087 case CallingConv::X86_64_SysV: Out << "x86_64_sysvcc"; break;
88 case CallingConv::X86_64_Win64: Out << "x86_64_win64cc"; break;
Micah Villmowd3766df2012-09-13 15:11:12 +000089 }
90}
Michael Ilseman407a6162012-11-15 22:34:00 +000091
Daniel Dunbare9da1332008-10-28 19:33:02 +000092// PrintEscapedString - Print each character of the specified string, escaping
93// it if it is not printable or if it is an escape char.
Chris Lattner8fff1262010-07-07 23:16:37 +000094static void PrintEscapedString(StringRef Name, raw_ostream &Out) {
Daniel Dunbar03d76512009-07-25 23:55:21 +000095 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
96 unsigned char C = Name[i];
Nick Lewycky34a40862009-03-15 06:39:52 +000097 if (isprint(C) && C != '\\' && C != '"')
Daniel Dunbare9da1332008-10-28 19:33:02 +000098 Out << C;
99 else
100 Out << '\\' << hexdigit(C >> 4) << hexdigit(C & 0x0F);
101 }
102}
103
Chris Lattner6ab910b2008-08-19 04:36:02 +0000104enum PrefixType {
105 GlobalPrefix,
106 LabelPrefix,
Daniel Dunbarcad35802008-10-14 23:28:09 +0000107 LocalPrefix,
108 NoPrefix
Chris Lattner6ab910b2008-08-19 04:36:02 +0000109};
110
111/// PrintLLVMName - Turn the specified name into an 'LLVM name', which is either
112/// prefixed with % (if the string only contains simple characters) or is
113/// surrounded with ""'s (if it has special chars in it). Print it out.
Benjamin Kramer38e59892010-07-14 22:38:02 +0000114static void PrintLLVMName(raw_ostream &OS, StringRef Name, PrefixType Prefix) {
Jay Foad61717b32011-04-24 14:30:00 +0000115 assert(!Name.empty() && "Cannot get empty name!");
Chris Lattner6ab910b2008-08-19 04:36:02 +0000116 switch (Prefix) {
Daniel Dunbarcad35802008-10-14 23:28:09 +0000117 case NoPrefix: break;
Chris Lattner52b26de2008-08-19 05:16:28 +0000118 case GlobalPrefix: OS << '@'; break;
119 case LabelPrefix: break;
120 case LocalPrefix: OS << '%'; break;
Nick Lewycky04234832009-03-19 06:31:22 +0000121 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000122
Chris Lattner6ab910b2008-08-19 04:36:02 +0000123 // Scan the name to see if it needs quotes first.
Guy Benyei87d0b9e2013-02-12 21:21:59 +0000124 bool NeedsQuotes = isdigit(static_cast<unsigned char>(Name[0]));
Chris Lattner6ab910b2008-08-19 04:36:02 +0000125 if (!NeedsQuotes) {
Daniel Dunbar03d76512009-07-25 23:55:21 +0000126 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
Aaron Ballman09dab822012-07-16 16:18:18 +0000127 // By making this unsigned, the value passed in to isalnum will always be
128 // in the range 0-255. This is important when building with MSVC because
129 // its implementation will assert. This situation can arise when dealing
130 // with UTF-8 multibyte characters.
131 unsigned char C = Name[i];
Guy Benyei87d0b9e2013-02-12 21:21:59 +0000132 if (!isalnum(static_cast<unsigned char>(C)) && C != '-' && C != '.' &&
133 C != '_') {
Chris Lattner6ab910b2008-08-19 04:36:02 +0000134 NeedsQuotes = true;
135 break;
136 }
137 }
138 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000139
Chris Lattner6ab910b2008-08-19 04:36:02 +0000140 // If we didn't need any quotes, just write out the name in one blast.
141 if (!NeedsQuotes) {
Daniel Dunbar03d76512009-07-25 23:55:21 +0000142 OS << Name;
Chris Lattner6ab910b2008-08-19 04:36:02 +0000143 return;
144 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000145
Chris Lattner6ab910b2008-08-19 04:36:02 +0000146 // Okay, we need quotes. Output the quotes and escape any scary characters as
147 // needed.
148 OS << '"';
Daniel Dunbar03d76512009-07-25 23:55:21 +0000149 PrintEscapedString(Name, OS);
Chris Lattner6ab910b2008-08-19 04:36:02 +0000150 OS << '"';
151}
152
153/// PrintLLVMName - Turn the specified name into an 'LLVM name', which is either
154/// prefixed with % (if the string only contains simple characters) or is
155/// surrounded with ""'s (if it has special chars in it). Print it out.
Dan Gohman1220e102009-08-12 20:56:03 +0000156static void PrintLLVMName(raw_ostream &OS, const Value *V) {
Daniel Dunbara279bc32009-09-20 02:20:51 +0000157 PrintLLVMName(OS, V->getName(),
Chris Lattner6ab910b2008-08-19 04:36:02 +0000158 isa<GlobalValue>(V) ? GlobalPrefix : LocalPrefix);
159}
160
Chris Lattner9cc34462009-02-28 20:25:14 +0000161
Daniel Malead0fef322013-05-08 20:38:31 +0000162namespace llvm {
Chris Lattner1afcace2011-07-09 17:41:24 +0000163
164void TypePrinting::incorporateTypes(const Module &M) {
Bill Wendling573e9732012-08-03 00:30:35 +0000165 NamedTypes.run(M, false);
Andrew Trick18801ec2011-09-30 19:48:58 +0000166
Chris Lattner1afcace2011-07-09 17:41:24 +0000167 // The list of struct types we got back includes all the struct types, split
168 // the unnamed ones out to a numbering and remove the anonymous structs.
169 unsigned NextNumber = 0;
Andrew Trick18801ec2011-09-30 19:48:58 +0000170
Chris Lattner1afcace2011-07-09 17:41:24 +0000171 std::vector<StructType*>::iterator NextToUse = NamedTypes.begin(), I, E;
172 for (I = NamedTypes.begin(), E = NamedTypes.end(); I != E; ++I) {
173 StructType *STy = *I;
Andrew Trick18801ec2011-09-30 19:48:58 +0000174
Chris Lattner1afcace2011-07-09 17:41:24 +0000175 // Ignore anonymous types.
Chris Lattnerc4d0e9f2011-08-12 18:07:07 +0000176 if (STy->isLiteral())
Chris Lattner1afcace2011-07-09 17:41:24 +0000177 continue;
Andrew Trick18801ec2011-09-30 19:48:58 +0000178
Chris Lattner1afcace2011-07-09 17:41:24 +0000179 if (STy->getName().empty())
180 NumberedTypes[STy] = NextNumber++;
181 else
182 *NextToUse++ = STy;
183 }
Andrew Trick18801ec2011-09-30 19:48:58 +0000184
Chris Lattner1afcace2011-07-09 17:41:24 +0000185 NamedTypes.erase(NextToUse, NamedTypes.end());
186}
187
188
Chris Lattner534361e2009-02-28 20:49:40 +0000189/// CalcTypeName - Write the specified type to the specified raw_ostream, making
190/// use of type names or up references to shorten the type name where possible.
Chris Lattner1afcace2011-07-09 17:41:24 +0000191void TypePrinting::print(Type *Ty, raw_ostream &OS) {
Chris Lattner9cc34462009-02-28 20:25:14 +0000192 switch (Ty->getTypeID()) {
Chris Lattner30794262009-02-28 21:27:31 +0000193 case Type::VoidTyID: OS << "void"; break;
Dan Gohmance163392011-12-17 00:04:22 +0000194 case Type::HalfTyID: OS << "half"; break;
Chris Lattner30794262009-02-28 21:27:31 +0000195 case Type::FloatTyID: OS << "float"; break;
196 case Type::DoubleTyID: OS << "double"; break;
197 case Type::X86_FP80TyID: OS << "x86_fp80"; break;
198 case Type::FP128TyID: OS << "fp128"; break;
199 case Type::PPC_FP128TyID: OS << "ppc_fp128"; break;
200 case Type::LabelTyID: OS << "label"; break;
Nick Lewycky7a0370f2009-05-30 05:06:04 +0000201 case Type::MetadataTyID: OS << "metadata"; break;
Dale Johannesenbb811a22010-09-10 20:55:01 +0000202 case Type::X86_MMXTyID: OS << "x86_mmx"; break;
Chris Lattner583ffd82009-02-28 21:18:43 +0000203 case Type::IntegerTyID:
Chris Lattner30794262009-02-28 21:27:31 +0000204 OS << 'i' << cast<IntegerType>(Ty)->getBitWidth();
Chris Lattner1afcace2011-07-09 17:41:24 +0000205 return;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000206
Chris Lattner36942d72009-02-28 20:35:42 +0000207 case Type::FunctionTyID: {
Chris Lattner1afcace2011-07-09 17:41:24 +0000208 FunctionType *FTy = cast<FunctionType>(Ty);
209 print(FTy->getReturnType(), OS);
Chris Lattner30794262009-02-28 21:27:31 +0000210 OS << " (";
Chris Lattner36942d72009-02-28 20:35:42 +0000211 for (FunctionType::param_iterator I = FTy->param_begin(),
212 E = FTy->param_end(); I != E; ++I) {
213 if (I != FTy->param_begin())
Chris Lattner30794262009-02-28 21:27:31 +0000214 OS << ", ";
Chris Lattner1afcace2011-07-09 17:41:24 +0000215 print(*I, OS);
Chris Lattner9cc34462009-02-28 20:25:14 +0000216 }
Chris Lattner36942d72009-02-28 20:35:42 +0000217 if (FTy->isVarArg()) {
Chris Lattner30794262009-02-28 21:27:31 +0000218 if (FTy->getNumParams()) OS << ", ";
219 OS << "...";
Chris Lattner9cc34462009-02-28 20:25:14 +0000220 }
Chris Lattner30794262009-02-28 21:27:31 +0000221 OS << ')';
Chris Lattner1afcace2011-07-09 17:41:24 +0000222 return;
Chris Lattner36942d72009-02-28 20:35:42 +0000223 }
224 case Type::StructTyID: {
Chris Lattner1afcace2011-07-09 17:41:24 +0000225 StructType *STy = cast<StructType>(Ty);
Andrew Trick18801ec2011-09-30 19:48:58 +0000226
Chris Lattnerc4d0e9f2011-08-12 18:07:07 +0000227 if (STy->isLiteral())
Chris Lattner1afcace2011-07-09 17:41:24 +0000228 return printStructBody(STy, OS);
229
230 if (!STy->getName().empty())
231 return PrintLLVMName(OS, STy->getName(), LocalPrefix);
Andrew Trick18801ec2011-09-30 19:48:58 +0000232
Chris Lattner1afcace2011-07-09 17:41:24 +0000233 DenseMap<StructType*, unsigned>::iterator I = NumberedTypes.find(STy);
234 if (I != NumberedTypes.end())
235 OS << '%' << I->second;
236 else // Not enumerated, print the hex address.
Benjamin Kramer5a832642011-11-02 17:24:36 +0000237 OS << "%\"type " << STy << '\"';
Chris Lattner1afcace2011-07-09 17:41:24 +0000238 return;
Chris Lattner36942d72009-02-28 20:35:42 +0000239 }
240 case Type::PointerTyID: {
Chris Lattner1afcace2011-07-09 17:41:24 +0000241 PointerType *PTy = cast<PointerType>(Ty);
242 print(PTy->getElementType(), OS);
Chris Lattner36942d72009-02-28 20:35:42 +0000243 if (unsigned AddressSpace = PTy->getAddressSpace())
Chris Lattner30794262009-02-28 21:27:31 +0000244 OS << " addrspace(" << AddressSpace << ')';
245 OS << '*';
Chris Lattner1afcace2011-07-09 17:41:24 +0000246 return;
Chris Lattner36942d72009-02-28 20:35:42 +0000247 }
248 case Type::ArrayTyID: {
Chris Lattner1afcace2011-07-09 17:41:24 +0000249 ArrayType *ATy = cast<ArrayType>(Ty);
Chris Lattner30794262009-02-28 21:27:31 +0000250 OS << '[' << ATy->getNumElements() << " x ";
Chris Lattner1afcace2011-07-09 17:41:24 +0000251 print(ATy->getElementType(), OS);
Chris Lattner30794262009-02-28 21:27:31 +0000252 OS << ']';
Chris Lattner1afcace2011-07-09 17:41:24 +0000253 return;
Chris Lattner36942d72009-02-28 20:35:42 +0000254 }
255 case Type::VectorTyID: {
Chris Lattner1afcace2011-07-09 17:41:24 +0000256 VectorType *PTy = cast<VectorType>(Ty);
Chris Lattner30794262009-02-28 21:27:31 +0000257 OS << "<" << PTy->getNumElements() << " x ";
Chris Lattner1afcace2011-07-09 17:41:24 +0000258 print(PTy->getElementType(), OS);
Chris Lattner30794262009-02-28 21:27:31 +0000259 OS << '>';
Chris Lattner1afcace2011-07-09 17:41:24 +0000260 return;
Chris Lattner36942d72009-02-28 20:35:42 +0000261 }
Chris Lattner36942d72009-02-28 20:35:42 +0000262 default:
Chris Lattner30794262009-02-28 21:27:31 +0000263 OS << "<unrecognized-type>";
Chris Lattner1afcace2011-07-09 17:41:24 +0000264 return;
Chris Lattner9cc34462009-02-28 20:25:14 +0000265 }
Chris Lattner9cc34462009-02-28 20:25:14 +0000266}
267
Chris Lattner1afcace2011-07-09 17:41:24 +0000268void TypePrinting::printStructBody(StructType *STy, raw_ostream &OS) {
269 if (STy->isOpaque()) {
270 OS << "opaque";
271 return;
Chris Lattner9cc34462009-02-28 20:25:14 +0000272 }
Andrew Trick18801ec2011-09-30 19:48:58 +0000273
Chris Lattner1afcace2011-07-09 17:41:24 +0000274 if (STy->isPacked())
275 OS << '<';
Andrew Trick18801ec2011-09-30 19:48:58 +0000276
Chris Lattner1afcace2011-07-09 17:41:24 +0000277 if (STy->getNumElements() == 0) {
278 OS << "{}";
279 } else {
280 StructType::element_iterator I = STy->element_begin();
281 OS << "{ ";
282 print(*I++, OS);
283 for (StructType::element_iterator E = STy->element_end(); I != E; ++I) {
284 OS << ", ";
285 print(*I, OS);
Chris Lattner413fd232009-03-01 00:03:38 +0000286 }
Andrew Trick18801ec2011-09-30 19:48:58 +0000287
Chris Lattner1afcace2011-07-09 17:41:24 +0000288 OS << " }";
Chris Lattnere9fa33e2009-02-28 23:20:19 +0000289 }
Chris Lattner1afcace2011-07-09 17:41:24 +0000290 if (STy->isPacked())
291 OS << '>';
Chris Lattnere9fa33e2009-02-28 23:20:19 +0000292}
293
Chris Lattner6ab910b2008-08-19 04:36:02 +0000294//===----------------------------------------------------------------------===//
295// SlotTracker Class: Enumerate slot numbers for unnamed values
296//===----------------------------------------------------------------------===//
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000297/// This class provides computation of slot numbers for LLVM Assembly writing.
Chris Lattner45d4c732008-08-17 04:17:45 +0000298///
Chris Lattner0d9574a2008-08-19 04:26:57 +0000299class SlotTracker {
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000300public:
Devang Patel320671d2009-07-08 21:44:25 +0000301 /// ValueMap - A mapping of Values to slot numbers.
Chris Lattner92255072008-08-17 17:25:25 +0000302 typedef DenseMap<const Value*, unsigned> ValueMap;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000303
304private:
Devang Patel320671d2009-07-08 21:44:25 +0000305 /// TheModule - The module for which we are holding slot numbers.
Chris Lattner45d4c732008-08-17 04:17:45 +0000306 const Module* TheModule;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000307
Devang Patel320671d2009-07-08 21:44:25 +0000308 /// TheFunction - The function for which we are holding slot numbers.
Chris Lattner45d4c732008-08-17 04:17:45 +0000309 const Function* TheFunction;
310 bool FunctionProcessed;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000311
Jay Foad7f0ce342011-07-11 07:28:49 +0000312 /// mMap - The slot map for the module level data.
Chris Lattner45d4c732008-08-17 04:17:45 +0000313 ValueMap mMap;
314 unsigned mNext;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000315
Jay Foad7f0ce342011-07-11 07:28:49 +0000316 /// fMap - The slot map for the function level data.
Chris Lattner45d4c732008-08-17 04:17:45 +0000317 ValueMap fMap;
318 unsigned fNext;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000319
Devang Patel320671d2009-07-08 21:44:25 +0000320 /// mdnMap - Map for MDNodes.
Chris Lattner307c9892009-12-31 02:20:11 +0000321 DenseMap<const MDNode*, unsigned> mdnMap;
Devang Patel320671d2009-07-08 21:44:25 +0000322 unsigned mdnNext;
Bill Wendlingb29ce262013-02-11 08:43:33 +0000323
324 /// asMap - The slot map for attribute sets.
325 DenseMap<AttributeSet, unsigned> asMap;
326 unsigned asNext;
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000327public:
Chris Lattner45d4c732008-08-17 04:17:45 +0000328 /// Construct from a module
Chris Lattner0d9574a2008-08-19 04:26:57 +0000329 explicit SlotTracker(const Module *M);
Chris Lattner45d4c732008-08-17 04:17:45 +0000330 /// Construct from a function, starting out in incorp state.
Chris Lattner0d9574a2008-08-19 04:26:57 +0000331 explicit SlotTracker(const Function *F);
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000332
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000333 /// Return the slot number of the specified value in it's type
Chris Lattner0d9574a2008-08-19 04:26:57 +0000334 /// plane. If something is not in the SlotTracker, return -1.
Chris Lattner22379bc2007-01-11 03:54:27 +0000335 int getLocalSlot(const Value *V);
336 int getGlobalSlot(const GlobalValue *V);
Devang Patel320671d2009-07-08 21:44:25 +0000337 int getMetadataSlot(const MDNode *N);
Bill Wendlingb29ce262013-02-11 08:43:33 +0000338 int getAttributeGroupSlot(AttributeSet AS);
Reid Spencerfc621e22004-06-09 15:26:53 +0000339
Misha Brukmanfd939082005-04-21 23:48:37 +0000340 /// If you'd like to deal with a function instead of just a module, use
Chris Lattner0d9574a2008-08-19 04:26:57 +0000341 /// this method to get its data into the SlotTracker.
Misha Brukmanfd939082005-04-21 23:48:37 +0000342 void incorporateFunction(const Function *F) {
343 TheFunction = F;
Reid Spencer28531c72004-08-16 07:46:33 +0000344 FunctionProcessed = false;
345 }
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000346
Misha Brukmanfd939082005-04-21 23:48:37 +0000347 /// After calling incorporateFunction, use this method to remove the
Chris Lattner0d9574a2008-08-19 04:26:57 +0000348 /// most recently incorporated function from the SlotTracker. This
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000349 /// will reset the state of the machine back to just the module contents.
350 void purgeFunction();
351
Devang Patel320671d2009-07-08 21:44:25 +0000352 /// MDNode map iterators.
Chris Lattner307c9892009-12-31 02:20:11 +0000353 typedef DenseMap<const MDNode*, unsigned>::iterator mdn_iterator;
354 mdn_iterator mdn_begin() { return mdnMap.begin(); }
355 mdn_iterator mdn_end() { return mdnMap.end(); }
356 unsigned mdn_size() const { return mdnMap.size(); }
357 bool mdn_empty() const { return mdnMap.empty(); }
Devang Patel320671d2009-07-08 21:44:25 +0000358
Bill Wendlingb29ce262013-02-11 08:43:33 +0000359 /// AttributeSet map iterators.
360 typedef DenseMap<AttributeSet, unsigned>::iterator as_iterator;
361 as_iterator as_begin() { return asMap.begin(); }
362 as_iterator as_end() { return asMap.end(); }
363 unsigned as_size() const { return asMap.size(); }
364 bool as_empty() const { return asMap.empty(); }
365
Reid Spencerb03de0c2004-05-26 21:56:09 +0000366 /// This function does the actual initialization.
367 inline void initialize();
368
Devang Patel320671d2009-07-08 21:44:25 +0000369 // Implementation Details
370private:
Chris Lattner9446bbe2007-01-09 07:55:49 +0000371 /// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
372 void CreateModuleSlot(const GlobalValue *V);
Devang Patel320671d2009-07-08 21:44:25 +0000373
374 /// CreateMetadataSlot - Insert the specified MDNode* into the slot table.
375 void CreateMetadataSlot(const MDNode *N);
376
Chris Lattner9446bbe2007-01-09 07:55:49 +0000377 /// CreateFunctionSlot - Insert the specified Value* into the slot table.
378 void CreateFunctionSlot(const Value *V);
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000379
Bill Wendlingb29ce262013-02-11 08:43:33 +0000380 /// \brief Insert the specified AttributeSet into the slot table.
381 void CreateAttributeSetSlot(AttributeSet AS);
382
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000383 /// Add all of the module level global variables (and their initializers)
384 /// and function declarations, but not the contents of those functions.
385 void processModule();
386
Devang Patel320671d2009-07-08 21:44:25 +0000387 /// Add all of the functions arguments, basic blocks, and instructions.
Reid Spencerb03de0c2004-05-26 21:56:09 +0000388 void processFunction();
389
Craig Topper86a1c322012-09-15 17:09:36 +0000390 SlotTracker(const SlotTracker &) LLVM_DELETED_FUNCTION;
391 void operator=(const SlotTracker &) LLVM_DELETED_FUNCTION;
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000392};
393
Daniel Malead0fef322013-05-08 20:38:31 +0000394SlotTracker *createSlotTracker(const Module *M) {
395 return new SlotTracker(M);
396}
Chris Lattner6ab910b2008-08-19 04:36:02 +0000397
398static SlotTracker *createSlotTracker(const Value *V) {
399 if (const Argument *FA = dyn_cast<Argument>(V))
400 return new SlotTracker(FA->getParent());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000401
Chris Lattner6ab910b2008-08-19 04:36:02 +0000402 if (const Instruction *I = dyn_cast<Instruction>(V))
Andrew Trick62e05902011-09-30 19:50:40 +0000403 if (I->getParent())
404 return new SlotTracker(I->getParent()->getParent());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000405
Chris Lattner6ab910b2008-08-19 04:36:02 +0000406 if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
407 return new SlotTracker(BB->getParent());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000408
Chris Lattner6ab910b2008-08-19 04:36:02 +0000409 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
410 return new SlotTracker(GV->getParent());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000411
Chris Lattner6ab910b2008-08-19 04:36:02 +0000412 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V))
Daniel Dunbara279bc32009-09-20 02:20:51 +0000413 return new SlotTracker(GA->getParent());
414
Chris Lattner6ab910b2008-08-19 04:36:02 +0000415 if (const Function *Func = dyn_cast<Function>(V))
416 return new SlotTracker(Func);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000417
Dan Gohman3bdfbf52010-07-20 23:55:01 +0000418 if (const MDNode *MD = dyn_cast<MDNode>(V)) {
419 if (!MD->isFunctionLocal())
420 return new SlotTracker(MD->getFunction());
421
Dale Johannesen5f72a5e2010-01-13 00:00:24 +0000422 return new SlotTracker((Function *)0);
Dan Gohman3bdfbf52010-07-20 23:55:01 +0000423 }
Dale Johannesen5f72a5e2010-01-13 00:00:24 +0000424
Chris Lattner6ab910b2008-08-19 04:36:02 +0000425 return 0;
426}
427
428#if 0
David Greened865e022010-01-05 01:29:26 +0000429#define ST_DEBUG(X) dbgs() << X
Chris Lattner6ab910b2008-08-19 04:36:02 +0000430#else
Chris Lattner24233032008-08-19 04:47:09 +0000431#define ST_DEBUG(X)
Chris Lattner6ab910b2008-08-19 04:36:02 +0000432#endif
433
434// Module level constructor. Causes the contents of the Module (sans functions)
435// to be added to the slot table.
436SlotTracker::SlotTracker(const Module *M)
Andrew Trick18801ec2011-09-30 19:48:58 +0000437 : TheModule(M), TheFunction(0), FunctionProcessed(false),
Bill Wendlingb29ce262013-02-11 08:43:33 +0000438 mNext(0), fNext(0), mdnNext(0), asNext(0) {
Chris Lattner6ab910b2008-08-19 04:36:02 +0000439}
440
441// Function level constructor. Causes the contents of the Module and the one
442// function provided to be added to the slot table.
443SlotTracker::SlotTracker(const Function *F)
Chris Lattnercfb5a202008-08-19 05:06:27 +0000444 : TheModule(F ? F->getParent() : 0), TheFunction(F), FunctionProcessed(false),
Bill Wendlingb29ce262013-02-11 08:43:33 +0000445 mNext(0), fNext(0), mdnNext(0), asNext(0) {
Chris Lattner6ab910b2008-08-19 04:36:02 +0000446}
447
448inline void SlotTracker::initialize() {
449 if (TheModule) {
450 processModule();
451 TheModule = 0; ///< Prevent re-processing next time we're called.
452 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000453
Chris Lattner6ab910b2008-08-19 04:36:02 +0000454 if (TheFunction && !FunctionProcessed)
455 processFunction();
456}
457
458// Iterate through all the global variables, functions, and global
459// variable initializers and create slots for them.
460void SlotTracker::processModule() {
Chris Lattner24233032008-08-19 04:47:09 +0000461 ST_DEBUG("begin processModule!\n");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000462
Chris Lattner6ab910b2008-08-19 04:36:02 +0000463 // Add all of the unnamed global variables to the value table.
464 for (Module::const_global_iterator I = TheModule->global_begin(),
Devang Patel320671d2009-07-08 21:44:25 +0000465 E = TheModule->global_end(); I != E; ++I) {
Daniel Dunbara279bc32009-09-20 02:20:51 +0000466 if (!I->hasName())
Chris Lattner6ab910b2008-08-19 04:36:02 +0000467 CreateModuleSlot(I);
Devang Patel320671d2009-07-08 21:44:25 +0000468 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000469
Devang Patel37c4a2d2009-07-29 22:04:47 +0000470 // Add metadata used by named metadata.
Daniel Dunbara279bc32009-09-20 02:20:51 +0000471 for (Module::const_named_metadata_iterator
Devang Patel37c4a2d2009-07-29 22:04:47 +0000472 I = TheModule->named_metadata_begin(),
473 E = TheModule->named_metadata_end(); I != E; ++I) {
474 const NamedMDNode *NMD = I;
Dan Gohman872814a2010-07-21 18:54:18 +0000475 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
476 CreateMetadataSlot(NMD->getOperand(i));
Devang Patel37c4a2d2009-07-29 22:04:47 +0000477 }
478
Chris Lattner6ab910b2008-08-19 04:36:02 +0000479 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
Bill Wendlingb29ce262013-02-11 08:43:33 +0000480 I != E; ++I) {
Chris Lattner6ab910b2008-08-19 04:36:02 +0000481 if (!I->hasName())
Bill Wendlingb29ce262013-02-11 08:43:33 +0000482 // Add all the unnamed functions to the table.
Chris Lattner6ab910b2008-08-19 04:36:02 +0000483 CreateModuleSlot(I);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000484
Bill Wendlingb29ce262013-02-11 08:43:33 +0000485 // Add all the function attributes to the table.
Bill Wendling7ab6c762013-02-20 07:21:42 +0000486 // FIXME: Add attributes of other objects?
Bill Wendlingb29ce262013-02-11 08:43:33 +0000487 AttributeSet FnAttrs = I->getAttributes().getFnAttributes();
488 if (FnAttrs.hasAttributes(AttributeSet::FunctionIndex))
489 CreateAttributeSetSlot(FnAttrs);
490 }
491
Chris Lattner24233032008-08-19 04:47:09 +0000492 ST_DEBUG("end processModule!\n");
Chris Lattner6ab910b2008-08-19 04:36:02 +0000493}
494
Chris Lattner6ab910b2008-08-19 04:36:02 +0000495// Process the arguments, basic blocks, and instructions of a function.
496void SlotTracker::processFunction() {
Chris Lattner24233032008-08-19 04:47:09 +0000497 ST_DEBUG("begin processFunction!\n");
Chris Lattner6ab910b2008-08-19 04:36:02 +0000498 fNext = 0;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000499
Chris Lattner6ab910b2008-08-19 04:36:02 +0000500 // Add all the function arguments with no names.
501 for(Function::const_arg_iterator AI = TheFunction->arg_begin(),
502 AE = TheFunction->arg_end(); AI != AE; ++AI)
503 if (!AI->hasName())
504 CreateFunctionSlot(AI);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000505
Chris Lattner24233032008-08-19 04:47:09 +0000506 ST_DEBUG("Inserting Instructions:\n");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000507
Chris Lattner6e6b1802009-12-31 02:13:35 +0000508 SmallVector<std::pair<unsigned, MDNode*>, 4> MDForInst;
Devang Patel43215782009-09-16 20:21:17 +0000509
Chris Lattner6ab910b2008-08-19 04:36:02 +0000510 // Add all of the basic blocks and instructions with no names.
511 for (Function::const_iterator BB = TheFunction->begin(),
512 E = TheFunction->end(); BB != E; ++BB) {
513 if (!BB->hasName())
514 CreateFunctionSlot(BB);
Andrew Trick18801ec2011-09-30 19:48:58 +0000515
Daniel Dunbara279bc32009-09-20 02:20:51 +0000516 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E;
Devang Patel320671d2009-07-08 21:44:25 +0000517 ++I) {
Chris Lattner3990b122009-12-28 23:41:32 +0000518 if (!I->getType()->isVoidTy() && !I->hasName())
Chris Lattner6ab910b2008-08-19 04:36:02 +0000519 CreateFunctionSlot(I);
Andrew Trick18801ec2011-09-30 19:48:58 +0000520
Chris Lattnerfd450c02010-05-10 20:53:17 +0000521 // Intrinsics can directly use metadata. We allow direct calls to any
522 // llvm.foo function here, because the target may not be linked into the
523 // optimizer.
524 if (const CallInst *CI = dyn_cast<CallInst>(I)) {
525 if (Function *F = CI->getCalledFunction())
526 if (F->getName().startswith("llvm."))
527 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
528 if (MDNode *N = dyn_cast_or_null<MDNode>(I->getOperand(i)))
529 CreateMetadataSlot(N);
Bill Wendling2bb471f2013-02-20 00:04:41 +0000530
Bill Wendling351b7a12013-02-22 09:09:42 +0000531 // Add all the call attributes to the table.
532 AttributeSet Attrs = CI->getAttributes().getFnAttributes();
533 if (Attrs.hasAttributes(AttributeSet::FunctionIndex))
534 CreateAttributeSetSlot(Attrs);
535 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(I)) {
536 // Add all the call attributes to the table.
537 AttributeSet Attrs = II->getAttributes().getFnAttributes();
538 if (Attrs.hasAttributes(AttributeSet::FunctionIndex))
539 CreateAttributeSetSlot(Attrs);
Chris Lattnerfd450c02010-05-10 20:53:17 +0000540 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000541
Devang Patel43215782009-09-16 20:21:17 +0000542 // Process metadata attached with this instruction.
Chris Lattner3990b122009-12-28 23:41:32 +0000543 I->getAllMetadata(MDForInst);
544 for (unsigned i = 0, e = MDForInst.size(); i != e; ++i)
545 CreateMetadataSlot(MDForInst[i].second);
Chris Lattner6e6b1802009-12-31 02:13:35 +0000546 MDForInst.clear();
Devang Patel320671d2009-07-08 21:44:25 +0000547 }
Chris Lattner6ab910b2008-08-19 04:36:02 +0000548 }
Devang Patel43215782009-09-16 20:21:17 +0000549
Chris Lattner6ab910b2008-08-19 04:36:02 +0000550 FunctionProcessed = true;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000551
Chris Lattner24233032008-08-19 04:47:09 +0000552 ST_DEBUG("end processFunction!\n");
Chris Lattner6ab910b2008-08-19 04:36:02 +0000553}
554
555/// Clean up after incorporating a function. This is the only way to get out of
556/// the function incorporation state that affects get*Slot/Create*Slot. Function
557/// incorporation state is indicated by TheFunction != 0.
558void SlotTracker::purgeFunction() {
Chris Lattner24233032008-08-19 04:47:09 +0000559 ST_DEBUG("begin purgeFunction!\n");
Chris Lattner6ab910b2008-08-19 04:36:02 +0000560 fMap.clear(); // Simply discard the function level map
561 TheFunction = 0;
562 FunctionProcessed = false;
Chris Lattner24233032008-08-19 04:47:09 +0000563 ST_DEBUG("end purgeFunction!\n");
Chris Lattner6ab910b2008-08-19 04:36:02 +0000564}
565
566/// getGlobalSlot - Get the slot number of a global value.
567int SlotTracker::getGlobalSlot(const GlobalValue *V) {
568 // Check for uninitialized state and do lazy initialization.
569 initialize();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000570
Jay Foad7f0ce342011-07-11 07:28:49 +0000571 // Find the value in the module map
Chris Lattner6ab910b2008-08-19 04:36:02 +0000572 ValueMap::iterator MI = mMap.find(V);
Dan Gohmanaeaf2452008-10-01 19:58:59 +0000573 return MI == mMap.end() ? -1 : (int)MI->second;
Chris Lattner6ab910b2008-08-19 04:36:02 +0000574}
575
Chris Lattner307c9892009-12-31 02:20:11 +0000576/// getMetadataSlot - Get the slot number of a MDNode.
Devang Patel320671d2009-07-08 21:44:25 +0000577int SlotTracker::getMetadataSlot(const MDNode *N) {
578 // Check for uninitialized state and do lazy initialization.
579 initialize();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000580
Jay Foad7f0ce342011-07-11 07:28:49 +0000581 // Find the MDNode in the module map
Chris Lattner307c9892009-12-31 02:20:11 +0000582 mdn_iterator MI = mdnMap.find(N);
Devang Patel320671d2009-07-08 21:44:25 +0000583 return MI == mdnMap.end() ? -1 : (int)MI->second;
584}
585
Chris Lattner6ab910b2008-08-19 04:36:02 +0000586
587/// getLocalSlot - Get the slot number for a value that is local to a function.
588int SlotTracker::getLocalSlot(const Value *V) {
589 assert(!isa<Constant>(V) && "Can't get a constant or global slot with this!");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000590
Chris Lattner6ab910b2008-08-19 04:36:02 +0000591 // Check for uninitialized state and do lazy initialization.
592 initialize();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000593
Chris Lattner6ab910b2008-08-19 04:36:02 +0000594 ValueMap::iterator FI = fMap.find(V);
Dan Gohmanaeaf2452008-10-01 19:58:59 +0000595 return FI == fMap.end() ? -1 : (int)FI->second;
Chris Lattner6ab910b2008-08-19 04:36:02 +0000596}
597
Bill Wendlingb29ce262013-02-11 08:43:33 +0000598int SlotTracker::getAttributeGroupSlot(AttributeSet AS) {
599 // Check for uninitialized state and do lazy initialization.
600 initialize();
601
602 // Find the AttributeSet in the module map.
603 as_iterator AI = asMap.find(AS);
604 return AI == asMap.end() ? -1 : (int)AI->second;
605}
Chris Lattner6ab910b2008-08-19 04:36:02 +0000606
607/// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
608void SlotTracker::CreateModuleSlot(const GlobalValue *V) {
609 assert(V && "Can't insert a null Value into SlotTracker!");
Chris Lattner4ee93c42009-12-29 07:25:48 +0000610 assert(!V->getType()->isVoidTy() && "Doesn't need a slot!");
Chris Lattner6ab910b2008-08-19 04:36:02 +0000611 assert(!V->hasName() && "Doesn't need a slot!");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000612
Chris Lattner6ab910b2008-08-19 04:36:02 +0000613 unsigned DestSlot = mNext++;
614 mMap[V] = DestSlot;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000615
Chris Lattner24233032008-08-19 04:47:09 +0000616 ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" <<
Chris Lattner6ab910b2008-08-19 04:36:02 +0000617 DestSlot << " [");
618 // G = Global, F = Function, A = Alias, o = other
Chris Lattner24233032008-08-19 04:47:09 +0000619 ST_DEBUG((isa<GlobalVariable>(V) ? 'G' :
Chris Lattner6ab910b2008-08-19 04:36:02 +0000620 (isa<Function>(V) ? 'F' :
621 (isa<GlobalAlias>(V) ? 'A' : 'o'))) << "]\n");
622}
623
Chris Lattner6ab910b2008-08-19 04:36:02 +0000624/// CreateSlot - Create a new slot for the specified value if it has no name.
625void SlotTracker::CreateFunctionSlot(const Value *V) {
Chris Lattner4ee93c42009-12-29 07:25:48 +0000626 assert(!V->getType()->isVoidTy() && !V->hasName() && "Doesn't need a slot!");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000627
Chris Lattner6ab910b2008-08-19 04:36:02 +0000628 unsigned DestSlot = fNext++;
629 fMap[V] = DestSlot;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000630
Chris Lattner6ab910b2008-08-19 04:36:02 +0000631 // G = Global, F = Function, o = other
Chris Lattner24233032008-08-19 04:47:09 +0000632 ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" <<
Chris Lattner6ab910b2008-08-19 04:36:02 +0000633 DestSlot << " [o]\n");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000634}
Chris Lattner6ab910b2008-08-19 04:36:02 +0000635
Devang Patel320671d2009-07-08 21:44:25 +0000636/// CreateModuleSlot - Insert the specified MDNode* into the slot table.
637void SlotTracker::CreateMetadataSlot(const MDNode *N) {
638 assert(N && "Can't insert a null Value into SlotTracker!");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000639
Chris Lattner2b4b1e22009-12-31 02:27:30 +0000640 // Don't insert if N is a function-local metadata, these are always printed
641 // inline.
Dan Gohman3bdfbf52010-07-20 23:55:01 +0000642 if (!N->isFunctionLocal()) {
643 mdn_iterator I = mdnMap.find(N);
644 if (I != mdnMap.end())
645 return;
Victor Hernandezff7707e2009-12-04 20:07:10 +0000646
Dan Gohman3bdfbf52010-07-20 23:55:01 +0000647 unsigned DestSlot = mdnNext++;
648 mdnMap[N] = DestSlot;
649 }
Devang Patel320671d2009-07-08 21:44:25 +0000650
Chris Lattner2b4b1e22009-12-31 02:27:30 +0000651 // Recursively add any MDNodes referenced by operands.
652 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
653 if (const MDNode *Op = dyn_cast_or_null<MDNode>(N->getOperand(i)))
654 CreateMetadataSlot(Op);
Devang Patel320671d2009-07-08 21:44:25 +0000655}
Chris Lattner6ab910b2008-08-19 04:36:02 +0000656
Bill Wendlingb29ce262013-02-11 08:43:33 +0000657void SlotTracker::CreateAttributeSetSlot(AttributeSet AS) {
658 assert(AS.hasAttributes(AttributeSet::FunctionIndex) &&
659 "Doesn't need a slot!");
660
661 as_iterator I = asMap.find(AS);
662 if (I != asMap.end())
663 return;
664
665 unsigned DestSlot = asNext++;
666 asMap[AS] = DestSlot;
667}
668
Chris Lattner6ab910b2008-08-19 04:36:02 +0000669//===----------------------------------------------------------------------===//
670// AsmWriter Implementation
671//===----------------------------------------------------------------------===//
Chris Lattnerf082b802002-07-23 18:07:49 +0000672
Dan Gohman1220e102009-08-12 20:56:03 +0000673static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
Dan Gohmand6c0f652009-08-13 15:27:57 +0000674 TypePrinting *TypePrinter,
Dan Gohman3bdfbf52010-07-20 23:55:01 +0000675 SlotTracker *Machine,
676 const Module *Context);
Reid Spencer0e25e1c2004-07-04 11:50:43 +0000677
Chris Lattnerc97536e2008-08-17 04:40:13 +0000678
Chris Lattner207b5bc2001-10-29 16:37:48 +0000679
Chris Lattner82c4bc72006-12-06 06:40:49 +0000680static const char *getPredicateText(unsigned predicate) {
Reid Spencer81dfeb32006-12-04 05:19:18 +0000681 const char * pred = "unknown";
682 switch (predicate) {
Chris Lattner6e6b1802009-12-31 02:13:35 +0000683 case FCmpInst::FCMP_FALSE: pred = "false"; break;
684 case FCmpInst::FCMP_OEQ: pred = "oeq"; break;
685 case FCmpInst::FCMP_OGT: pred = "ogt"; break;
686 case FCmpInst::FCMP_OGE: pred = "oge"; break;
687 case FCmpInst::FCMP_OLT: pred = "olt"; break;
688 case FCmpInst::FCMP_OLE: pred = "ole"; break;
689 case FCmpInst::FCMP_ONE: pred = "one"; break;
690 case FCmpInst::FCMP_ORD: pred = "ord"; break;
691 case FCmpInst::FCMP_UNO: pred = "uno"; break;
692 case FCmpInst::FCMP_UEQ: pred = "ueq"; break;
693 case FCmpInst::FCMP_UGT: pred = "ugt"; break;
694 case FCmpInst::FCMP_UGE: pred = "uge"; break;
695 case FCmpInst::FCMP_ULT: pred = "ult"; break;
696 case FCmpInst::FCMP_ULE: pred = "ule"; break;
697 case FCmpInst::FCMP_UNE: pred = "une"; break;
698 case FCmpInst::FCMP_TRUE: pred = "true"; break;
699 case ICmpInst::ICMP_EQ: pred = "eq"; break;
700 case ICmpInst::ICMP_NE: pred = "ne"; break;
701 case ICmpInst::ICMP_SGT: pred = "sgt"; break;
702 case ICmpInst::ICMP_SGE: pred = "sge"; break;
703 case ICmpInst::ICMP_SLT: pred = "slt"; break;
704 case ICmpInst::ICMP_SLE: pred = "sle"; break;
705 case ICmpInst::ICMP_UGT: pred = "ugt"; break;
706 case ICmpInst::ICMP_UGE: pred = "uge"; break;
707 case ICmpInst::ICMP_ULT: pred = "ult"; break;
708 case ICmpInst::ICMP_ULE: pred = "ule"; break;
Reid Spencer81dfeb32006-12-04 05:19:18 +0000709 }
710 return pred;
711}
712
Eli Friedmanff030482011-07-28 21:48:00 +0000713static void writeAtomicRMWOperation(raw_ostream &Out,
714 AtomicRMWInst::BinOp Op) {
715 switch (Op) {
716 default: Out << " <unknown operation " << Op << ">"; break;
717 case AtomicRMWInst::Xchg: Out << " xchg"; break;
718 case AtomicRMWInst::Add: Out << " add"; break;
719 case AtomicRMWInst::Sub: Out << " sub"; break;
720 case AtomicRMWInst::And: Out << " and"; break;
721 case AtomicRMWInst::Nand: Out << " nand"; break;
722 case AtomicRMWInst::Or: Out << " or"; break;
723 case AtomicRMWInst::Xor: Out << " xor"; break;
724 case AtomicRMWInst::Max: Out << " max"; break;
725 case AtomicRMWInst::Min: Out << " min"; break;
726 case AtomicRMWInst::UMax: Out << " umax"; break;
727 case AtomicRMWInst::UMin: Out << " umin"; break;
728 }
729}
Devang Patel320671d2009-07-08 21:44:25 +0000730
Dan Gohman1220e102009-08-12 20:56:03 +0000731static void WriteOptimizationInfo(raw_ostream &Out, const User *U) {
Michael Ilseman15c13d32012-11-27 00:42:44 +0000732 if (const FPMathOperator *FPO = dyn_cast<const FPMathOperator>(U)) {
733 // Unsafe algebra implies all the others, no need to write them all out
734 if (FPO->hasUnsafeAlgebra())
735 Out << " fast";
736 else {
737 if (FPO->hasNoNaNs())
738 Out << " nnan";
739 if (FPO->hasNoInfs())
740 Out << " ninf";
741 if (FPO->hasNoSignedZeros())
742 Out << " nsz";
743 if (FPO->hasAllowReciprocal())
744 Out << " arcp";
745 }
746 }
747
Dan Gohman1224c382009-07-20 21:19:07 +0000748 if (const OverflowingBinaryOperator *OBO =
749 dyn_cast<OverflowingBinaryOperator>(U)) {
Dan Gohman5078f842009-08-20 17:11:38 +0000750 if (OBO->hasNoUnsignedWrap())
Dan Gohman59858cf2009-07-27 16:11:46 +0000751 Out << " nuw";
Dan Gohman5078f842009-08-20 17:11:38 +0000752 if (OBO->hasNoSignedWrap())
Dan Gohman59858cf2009-07-27 16:11:46 +0000753 Out << " nsw";
Chris Lattner35bda892011-02-06 21:44:57 +0000754 } else if (const PossiblyExactOperator *Div =
755 dyn_cast<PossiblyExactOperator>(U)) {
Dan Gohman1224c382009-07-20 21:19:07 +0000756 if (Div->isExact())
Dan Gohman59858cf2009-07-27 16:11:46 +0000757 Out << " exact";
Dan Gohmandd8004d2009-07-27 21:53:46 +0000758 } else if (const GEPOperator *GEP = dyn_cast<GEPOperator>(U)) {
759 if (GEP->isInBounds())
760 Out << " inbounds";
Dan Gohman1224c382009-07-20 21:19:07 +0000761 }
762}
763
Dan Gohman40cf12f2010-07-14 20:57:55 +0000764static void WriteConstantInternal(raw_ostream &Out, const Constant *CV,
765 TypePrinting &TypePrinter,
Dan Gohman3bdfbf52010-07-20 23:55:01 +0000766 SlotTracker *Machine,
767 const Module *Context) {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +0000768 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000769 if (CI->getType()->isIntegerTy(1)) {
Reid Spencer579dca12007-01-12 04:24:46 +0000770 Out << (CI->getZExtValue() ? "true" : "false");
Chris Lattnerfad86b02008-08-17 07:19:36 +0000771 return;
772 }
773 Out << CI->getValue();
774 return;
775 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000776
Chris Lattnerfad86b02008-08-17 07:19:36 +0000777 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Tobias Grosser057beb82012-05-24 15:59:06 +0000778 if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEsingle ||
Dan Gohmance163392011-12-17 00:04:22 +0000779 &CFP->getValueAPF().getSemantics() == &APFloat::IEEEdouble) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000780 // We would like to output the FP constant value in exponential notation,
781 // but we cannot do this if doing so will lose precision. Check here to
782 // make sure that we only output it in exponential format if we can parse
783 // the value back and get the same value.
784 //
Dale Johannesen541ed9f2009-01-21 20:32:55 +0000785 bool ignored;
Dan Gohmance163392011-12-17 00:04:22 +0000786 bool isHalf = &CFP->getValueAPF().getSemantics()==&APFloat::IEEEhalf;
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000787 bool isDouble = &CFP->getValueAPF().getSemantics()==&APFloat::IEEEdouble;
NAKAMURA Takumi22bed5d2012-02-16 08:12:24 +0000788 bool isInf = CFP->getValueAPF().isInfinity();
789 bool isNaN = CFP->getValueAPF().isNaN();
790 if (!isHalf && !isInf && !isNaN) {
Dan Gohmance163392011-12-17 00:04:22 +0000791 double Val = isDouble ? CFP->getValueAPF().convertToDouble() :
792 CFP->getValueAPF().convertToFloat();
793 SmallString<128> StrVal;
794 raw_svector_ostream(StrVal) << Val;
Chris Lattner66e810b2002-04-18 18:53:13 +0000795
Dan Gohmance163392011-12-17 00:04:22 +0000796 // Check to make sure that the stringized number is not some string like
797 // "Inf" or NaN, that atof will accept, but the lexer will not. Check
798 // that the string matches the "[-+]?[0-9]" regex.
799 //
800 if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
801 ((StrVal[0] == '-' || StrVal[0] == '+') &&
802 (StrVal[1] >= '0' && StrVal[1] <= '9'))) {
803 // Reparse stringized version!
NAKAMURA Takumic8782a12012-02-16 04:19:15 +0000804 if (APFloat(APFloat::IEEEdouble, StrVal).convertToDouble() == Val) {
Dan Gohmance163392011-12-17 00:04:22 +0000805 Out << StrVal.str();
806 return;
807 }
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000808 }
Chris Lattner66e810b2002-04-18 18:53:13 +0000809 }
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000810 // Otherwise we could not reparse it to exactly the same value, so we must
Dale Johannesen541ed9f2009-01-21 20:32:55 +0000811 // output the string in hexadecimal format! Note that loading and storing
812 // floating point types changes the bits of NaNs on some hosts, notably
813 // x86, so we must not use these types.
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000814 assert(sizeof(double) == sizeof(uint64_t) &&
815 "assuming that double is 64 bits!");
Chris Lattnerc6a13462008-11-10 04:30:26 +0000816 char Buffer[40];
Dale Johannesen541ed9f2009-01-21 20:32:55 +0000817 APFloat apf = CFP->getValueAPF();
Dan Gohmance163392011-12-17 00:04:22 +0000818 // Halves and floats are represented in ASCII IR as double, convert.
Dale Johannesen541ed9f2009-01-21 20:32:55 +0000819 if (!isDouble)
Daniel Dunbara279bc32009-09-20 02:20:51 +0000820 apf.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven,
Dale Johannesen541ed9f2009-01-21 20:32:55 +0000821 &ignored);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000822 Out << "0x" <<
823 utohex_buffer(uint64_t(apf.bitcastToAPInt().getZExtValue()),
Dale Johannesen541ed9f2009-01-21 20:32:55 +0000824 Buffer+40);
Chris Lattnercfb5a202008-08-19 05:06:27 +0000825 return;
826 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000827
Tobias Grosser057beb82012-05-24 15:59:06 +0000828 // Either half, or some form of long double.
829 // These appear as a magic letter identifying the type, then a
830 // fixed number of hex digits.
Chris Lattnercfb5a202008-08-19 05:06:27 +0000831 Out << "0x";
Tobias Grosser057beb82012-05-24 15:59:06 +0000832 // Bit position, in the current word, of the next nibble to print.
833 int shiftcount;
834
Dale Johannesen1b25cb22009-03-23 21:16:53 +0000835 if (&CFP->getValueAPF().getSemantics() == &APFloat::x87DoubleExtended) {
Chris Lattnercfb5a202008-08-19 05:06:27 +0000836 Out << 'K';
Dale Johannesen1b25cb22009-03-23 21:16:53 +0000837 // api needed to prevent premature destruction
838 APInt api = CFP->getValueAPF().bitcastToAPInt();
839 const uint64_t* p = api.getRawData();
840 uint64_t word = p[1];
Tobias Grosser057beb82012-05-24 15:59:06 +0000841 shiftcount = 12;
Dale Johannesen1b25cb22009-03-23 21:16:53 +0000842 int width = api.getBitWidth();
843 for (int j=0; j<width; j+=4, shiftcount-=4) {
844 unsigned int nibble = (word>>shiftcount) & 15;
845 if (nibble < 10)
846 Out << (unsigned char)(nibble + '0');
847 else
848 Out << (unsigned char)(nibble - 10 + 'A');
849 if (shiftcount == 0 && j+4 < width) {
850 word = *p;
851 shiftcount = 64;
852 if (width-j-4 < 64)
853 shiftcount = width-j-4;
854 }
855 }
856 return;
Tobias Grosser057beb82012-05-24 15:59:06 +0000857 } else if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEquad) {
858 shiftcount = 60;
Chris Lattnercfb5a202008-08-19 05:06:27 +0000859 Out << 'L';
Tobias Grosser057beb82012-05-24 15:59:06 +0000860 } else if (&CFP->getValueAPF().getSemantics() == &APFloat::PPCDoubleDouble) {
861 shiftcount = 60;
Chris Lattnercfb5a202008-08-19 05:06:27 +0000862 Out << 'M';
Tobias Grosser057beb82012-05-24 15:59:06 +0000863 } else if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEhalf) {
864 shiftcount = 12;
865 Out << 'H';
866 } else
Torok Edwinc23197a2009-07-14 16:55:14 +0000867 llvm_unreachable("Unsupported floating point type");
Chris Lattnercfb5a202008-08-19 05:06:27 +0000868 // api needed to prevent premature destruction
Dale Johannesen7111b022008-10-09 18:53:47 +0000869 APInt api = CFP->getValueAPF().bitcastToAPInt();
Chris Lattnercfb5a202008-08-19 05:06:27 +0000870 const uint64_t* p = api.getRawData();
871 uint64_t word = *p;
Chris Lattnercfb5a202008-08-19 05:06:27 +0000872 int width = api.getBitWidth();
873 for (int j=0; j<width; j+=4, shiftcount-=4) {
874 unsigned int nibble = (word>>shiftcount) & 15;
875 if (nibble < 10)
876 Out << (unsigned char)(nibble + '0');
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000877 else
Chris Lattnercfb5a202008-08-19 05:06:27 +0000878 Out << (unsigned char)(nibble - 10 + 'A');
879 if (shiftcount == 0 && j+4 < width) {
880 word = *(++p);
881 shiftcount = 64;
882 if (width-j-4 < 64)
883 shiftcount = width-j-4;
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000884 }
885 }
Chris Lattnercfb5a202008-08-19 05:06:27 +0000886 return;
887 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000888
Chris Lattnercfb5a202008-08-19 05:06:27 +0000889 if (isa<ConstantAggregateZero>(CV)) {
Chris Lattnerde512b52004-02-15 05:55:15 +0000890 Out << "zeroinitializer";
Chris Lattnercfb5a202008-08-19 05:06:27 +0000891 return;
892 }
Andrew Trick18801ec2011-09-30 19:48:58 +0000893
Chris Lattner73050e12009-10-28 03:38:12 +0000894 if (const BlockAddress *BA = dyn_cast<BlockAddress>(CV)) {
895 Out << "blockaddress(";
Dan Gohman3bdfbf52010-07-20 23:55:01 +0000896 WriteAsOperandInternal(Out, BA->getFunction(), &TypePrinter, Machine,
897 Context);
Chris Lattner73050e12009-10-28 03:38:12 +0000898 Out << ", ";
Dan Gohman3bdfbf52010-07-20 23:55:01 +0000899 WriteAsOperandInternal(Out, BA->getBasicBlock(), &TypePrinter, Machine,
900 Context);
Chris Lattner73050e12009-10-28 03:38:12 +0000901 Out << ")";
902 return;
903 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000904
Chris Lattnercfb5a202008-08-19 05:06:27 +0000905 if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
Chris Lattner1afcace2011-07-09 17:41:24 +0000906 Type *ETy = CA->getType()->getElementType();
Chris Lattner18c7f802012-02-05 02:29:43 +0000907 Out << '[';
908 TypePrinter.print(ETy, Out);
909 Out << ' ';
910 WriteAsOperandInternal(Out, CA->getOperand(0),
911 &TypePrinter, Machine,
912 Context);
913 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
914 Out << ", ";
Chris Lattner8b10b692012-01-31 03:15:40 +0000915 TypePrinter.print(ETy, Out);
916 Out << ' ';
Chris Lattner18c7f802012-02-05 02:29:43 +0000917 WriteAsOperandInternal(Out, CA->getOperand(i), &TypePrinter, Machine,
Chris Lattner8b10b692012-01-31 03:15:40 +0000918 Context);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000919 }
Chris Lattner18c7f802012-02-05 02:29:43 +0000920 Out << ']';
Chris Lattnercfb5a202008-08-19 05:06:27 +0000921 return;
922 }
Michael Ilseman407a6162012-11-15 22:34:00 +0000923
Chris Lattnerd59ae902012-01-26 02:32:04 +0000924 if (const ConstantDataArray *CA = dyn_cast<ConstantDataArray>(CV)) {
925 // As a special case, print the array as a string if it is an array of
926 // i8 with ConstantInt values.
927 if (CA->isString()) {
928 Out << "c\"";
929 PrintEscapedString(CA->getAsString(), Out);
930 Out << '"';
931 return;
932 }
933
934 Type *ETy = CA->getType()->getElementType();
935 Out << '[';
Chris Lattner8b10b692012-01-31 03:15:40 +0000936 TypePrinter.print(ETy, Out);
937 Out << ' ';
938 WriteAsOperandInternal(Out, CA->getElementAsConstant(0),
939 &TypePrinter, Machine,
940 Context);
941 for (unsigned i = 1, e = CA->getNumElements(); i != e; ++i) {
942 Out << ", ";
Chris Lattnerd59ae902012-01-26 02:32:04 +0000943 TypePrinter.print(ETy, Out);
944 Out << ' ';
Chris Lattner8b10b692012-01-31 03:15:40 +0000945 WriteAsOperandInternal(Out, CA->getElementAsConstant(i), &TypePrinter,
946 Machine, Context);
Chris Lattnerd59ae902012-01-26 02:32:04 +0000947 }
Chris Lattner8b10b692012-01-31 03:15:40 +0000948 Out << ']';
Chris Lattnerd59ae902012-01-26 02:32:04 +0000949 return;
950 }
951
Daniel Dunbara279bc32009-09-20 02:20:51 +0000952
Chris Lattnercfb5a202008-08-19 05:06:27 +0000953 if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
Andrew Lenharth43f344a2007-01-08 18:21:30 +0000954 if (CS->getType()->isPacked())
955 Out << '<';
Misha Brukman40c732c2004-06-04 21:11:51 +0000956 Out << '{';
Jim Laskeya3f332b2006-02-25 12:27:03 +0000957 unsigned N = CS->getNumOperands();
958 if (N) {
Chris Lattner24233032008-08-19 04:47:09 +0000959 Out << ' ';
Chris Lattner0f7364b2009-02-28 21:26:53 +0000960 TypePrinter.print(CS->getOperand(0)->getType(), Out);
Dan Gohman8dae1382008-09-14 17:21:12 +0000961 Out << ' ';
Chris Lattner7a716ad2002-04-16 21:36:08 +0000962
Dan Gohman3bdfbf52010-07-20 23:55:01 +0000963 WriteAsOperandInternal(Out, CS->getOperand(0), &TypePrinter, Machine,
964 Context);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000965
Jim Laskeya3f332b2006-02-25 12:27:03 +0000966 for (unsigned i = 1; i < N; i++) {
Chris Lattner7a716ad2002-04-16 21:36:08 +0000967 Out << ", ";
Chris Lattner0f7364b2009-02-28 21:26:53 +0000968 TypePrinter.print(CS->getOperand(i)->getType(), Out);
Dan Gohman8dae1382008-09-14 17:21:12 +0000969 Out << ' ';
Chris Lattner7a716ad2002-04-16 21:36:08 +0000970
Dan Gohman3bdfbf52010-07-20 23:55:01 +0000971 WriteAsOperandInternal(Out, CS->getOperand(i), &TypePrinter, Machine,
972 Context);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000973 }
Dan Gohman8dae1382008-09-14 17:21:12 +0000974 Out << ' ';
Chris Lattner7a716ad2002-04-16 21:36:08 +0000975 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000976
Dan Gohman8dae1382008-09-14 17:21:12 +0000977 Out << '}';
Andrew Lenharth43f344a2007-01-08 18:21:30 +0000978 if (CS->getType()->isPacked())
979 Out << '>';
Chris Lattnercfb5a202008-08-19 05:06:27 +0000980 return;
981 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000982
Chris Lattnerd59ae902012-01-26 02:32:04 +0000983 if (isa<ConstantVector>(CV) || isa<ConstantDataVector>(CV)) {
984 Type *ETy = CV->getType()->getVectorElementType();
Dan Gohman7dfa07f2009-02-11 00:25:25 +0000985 Out << '<';
Chris Lattner0f7364b2009-02-28 21:26:53 +0000986 TypePrinter.print(ETy, Out);
Dan Gohman8dae1382008-09-14 17:21:12 +0000987 Out << ' ';
Chris Lattnerd59ae902012-01-26 02:32:04 +0000988 WriteAsOperandInternal(Out, CV->getAggregateElement(0U), &TypePrinter,
989 Machine, Context);
990 for (unsigned i = 1, e = CV->getType()->getVectorNumElements(); i != e;++i){
Chris Lattner4667b712008-08-19 05:26:17 +0000991 Out << ", ";
Chris Lattner0f7364b2009-02-28 21:26:53 +0000992 TypePrinter.print(ETy, Out);
Dan Gohman8dae1382008-09-14 17:21:12 +0000993 Out << ' ';
Chris Lattnerd59ae902012-01-26 02:32:04 +0000994 WriteAsOperandInternal(Out, CV->getAggregateElement(i), &TypePrinter,
995 Machine, Context);
Chris Lattnercfb5a202008-08-19 05:06:27 +0000996 }
Dan Gohman7dfa07f2009-02-11 00:25:25 +0000997 Out << '>';
Chris Lattnercfb5a202008-08-19 05:06:27 +0000998 return;
999 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001000
Chris Lattnercfb5a202008-08-19 05:06:27 +00001001 if (isa<ConstantPointerNull>(CV)) {
Chris Lattner7a716ad2002-04-16 21:36:08 +00001002 Out << "null";
Chris Lattnercfb5a202008-08-19 05:06:27 +00001003 return;
1004 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001005
Chris Lattnercfb5a202008-08-19 05:06:27 +00001006 if (isa<UndefValue>(CV)) {
Chris Lattnerb976e662004-10-16 18:08:06 +00001007 Out << "undef";
Chris Lattnercfb5a202008-08-19 05:06:27 +00001008 return;
1009 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001010
Chris Lattnercfb5a202008-08-19 05:06:27 +00001011 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Reid Spencer81dfeb32006-12-04 05:19:18 +00001012 Out << CE->getOpcodeName();
Dan Gohman59858cf2009-07-27 16:11:46 +00001013 WriteOptimizationInfo(Out, CE);
Reid Spencer81dfeb32006-12-04 05:19:18 +00001014 if (CE->isCompare())
Chris Lattnercfb5a202008-08-19 05:06:27 +00001015 Out << ' ' << getPredicateText(CE->getPredicate());
Reid Spencer81dfeb32006-12-04 05:19:18 +00001016 Out << " (";
Misha Brukmanfd939082005-04-21 23:48:37 +00001017
Vikram S. Adveb4dbb442002-07-14 23:14:45 +00001018 for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) {
Chris Lattner0f7364b2009-02-28 21:26:53 +00001019 TypePrinter.print((*OI)->getType(), Out);
Dan Gohman8dae1382008-09-14 17:21:12 +00001020 Out << ' ';
Dan Gohman3bdfbf52010-07-20 23:55:01 +00001021 WriteAsOperandInternal(Out, *OI, &TypePrinter, Machine, Context);
Vikram S. Adveb4dbb442002-07-14 23:14:45 +00001022 if (OI+1 != CE->op_end())
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001023 Out << ", ";
Vikram S. Adveb4dbb442002-07-14 23:14:45 +00001024 }
Misha Brukmanfd939082005-04-21 23:48:37 +00001025
Dan Gohman995be7d2008-05-31 19:12:39 +00001026 if (CE->hasIndices()) {
Jay Foadd30aa5a2011-04-13 15:22:40 +00001027 ArrayRef<unsigned> Indices = CE->getIndices();
Dan Gohman995be7d2008-05-31 19:12:39 +00001028 for (unsigned i = 0, e = Indices.size(); i != e; ++i)
1029 Out << ", " << Indices[i];
1030 }
1031
Reid Spencer3da59db2006-11-27 01:05:10 +00001032 if (CE->isCast()) {
Chris Lattner95586b82002-08-15 19:37:43 +00001033 Out << " to ";
Chris Lattner0f7364b2009-02-28 21:26:53 +00001034 TypePrinter.print(CE->getType(), Out);
Chris Lattner95586b82002-08-15 19:37:43 +00001035 }
Reid Spencer3da59db2006-11-27 01:05:10 +00001036
Misha Brukman40c732c2004-06-04 21:11:51 +00001037 Out << ')';
Chris Lattnercfb5a202008-08-19 05:06:27 +00001038 return;
Chris Lattner7a716ad2002-04-16 21:36:08 +00001039 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001040
Chris Lattnercfb5a202008-08-19 05:06:27 +00001041 Out << "<placeholder or erroneous Constant>";
Chris Lattner7a716ad2002-04-16 21:36:08 +00001042}
1043
Chris Lattner85b19122009-12-31 02:31:59 +00001044static void WriteMDNodeBodyInternal(raw_ostream &Out, const MDNode *Node,
1045 TypePrinting *TypePrinter,
Dan Gohman3bdfbf52010-07-20 23:55:01 +00001046 SlotTracker *Machine,
1047 const Module *Context) {
Chris Lattner85b19122009-12-31 02:31:59 +00001048 Out << "!{";
1049 for (unsigned mi = 0, me = Node->getNumOperands(); mi != me; ++mi) {
1050 const Value *V = Node->getOperand(mi);
1051 if (V == 0)
1052 Out << "null";
1053 else {
1054 TypePrinter->print(V->getType(), Out);
1055 Out << ' ';
Andrew Trick18801ec2011-09-30 19:48:58 +00001056 WriteAsOperandInternal(Out, Node->getOperand(mi),
Dan Gohman3bdfbf52010-07-20 23:55:01 +00001057 TypePrinter, Machine, Context);
Chris Lattner85b19122009-12-31 02:31:59 +00001058 }
1059 if (mi + 1 != me)
1060 Out << ", ";
1061 }
Andrew Trick18801ec2011-09-30 19:48:58 +00001062
Chris Lattner85b19122009-12-31 02:31:59 +00001063 Out << "}";
1064}
1065
Chris Lattner7a716ad2002-04-16 21:36:08 +00001066
Misha Brukmanab5c6002004-03-02 00:22:19 +00001067/// WriteAsOperand - Write the name of the specified value out to the specified
1068/// ostream. This can be useful when you just want to print int %reg126, not
1069/// the whole instruction that generated it.
1070///
Dan Gohman1220e102009-08-12 20:56:03 +00001071static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
Dan Gohmand6c0f652009-08-13 15:27:57 +00001072 TypePrinting *TypePrinter,
Dan Gohman3bdfbf52010-07-20 23:55:01 +00001073 SlotTracker *Machine,
1074 const Module *Context) {
Chris Lattnerc97536e2008-08-17 04:40:13 +00001075 if (V->hasName()) {
1076 PrintLLVMName(Out, V);
1077 return;
1078 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001079
Chris Lattnerc97536e2008-08-17 04:40:13 +00001080 const Constant *CV = dyn_cast<Constant>(V);
1081 if (CV && !isa<GlobalValue>(CV)) {
Dan Gohmand6c0f652009-08-13 15:27:57 +00001082 assert(TypePrinter && "Constants require TypePrinting!");
Dan Gohman3bdfbf52010-07-20 23:55:01 +00001083 WriteConstantInternal(Out, CV, *TypePrinter, Machine, Context);
Chris Lattnercfb5a202008-08-19 05:06:27 +00001084 return;
1085 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001086
Chris Lattnercfb5a202008-08-19 05:06:27 +00001087 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
Chris Lattnerc97536e2008-08-17 04:40:13 +00001088 Out << "asm ";
1089 if (IA->hasSideEffects())
1090 Out << "sideeffect ";
Dale Johannesen8ba2d5b2009-10-21 23:28:00 +00001091 if (IA->isAlignStack())
1092 Out << "alignstack ";
Chad Rosier581600b2012-09-05 19:00:49 +00001093 // We don't emit the AD_ATT dialect as it's the assumed default.
1094 if (IA->getDialect() == InlineAsm::AD_Intel)
1095 Out << "inteldialect ";
Chris Lattnerc97536e2008-08-17 04:40:13 +00001096 Out << '"';
1097 PrintEscapedString(IA->getAsmString(), Out);
1098 Out << "\", \"";
1099 PrintEscapedString(IA->getConstraintString(), Out);
1100 Out << '"';
Chris Lattnercfb5a202008-08-19 05:06:27 +00001101 return;
1102 }
Devang Patele54abc92009-07-22 17:43:22 +00001103
Devang Patel104cf9e2009-07-23 01:07:34 +00001104 if (const MDNode *N = dyn_cast<MDNode>(V)) {
Victor Hernandez5d301622009-12-18 20:09:14 +00001105 if (N->isFunctionLocal()) {
Victor Hernandez97e24502009-12-04 01:35:02 +00001106 // Print metadata inline, not via slot reference number.
Dan Gohman3bdfbf52010-07-20 23:55:01 +00001107 WriteMDNodeBodyInternal(Out, N, TypePrinter, Machine, Context);
Victor Hernandez97e24502009-12-04 01:35:02 +00001108 return;
1109 }
Andrew Trick18801ec2011-09-30 19:48:58 +00001110
Dan Gohman3bdfbf52010-07-20 23:55:01 +00001111 if (!Machine) {
1112 if (N->isFunctionLocal())
1113 Machine = new SlotTracker(N->getFunction());
1114 else
1115 Machine = new SlotTracker(Context);
1116 }
Dan Gohman3da076f2010-09-09 20:53:58 +00001117 int Slot = Machine->getMetadataSlot(N);
1118 if (Slot == -1)
1119 Out << "<badref>";
1120 else
1121 Out << '!' << Slot;
Devang Patel104cf9e2009-07-23 01:07:34 +00001122 return;
1123 }
1124
Devang Patele54abc92009-07-22 17:43:22 +00001125 if (const MDString *MDS = dyn_cast<MDString>(V)) {
Devang Patele54abc92009-07-22 17:43:22 +00001126 Out << "!\"";
Daniel Dunbar03d76512009-07-25 23:55:21 +00001127 PrintEscapedString(MDS->getString(), Out);
Devang Patele54abc92009-07-22 17:43:22 +00001128 Out << '"';
1129 return;
1130 }
1131
Evan Cheng746d5462009-11-16 07:10:36 +00001132 if (V->getValueID() == Value::PseudoSourceValueVal ||
1133 V->getValueID() == Value::FixedStackPseudoSourceValueVal) {
Dan Gohmancd26ec52009-09-23 01:33:16 +00001134 V->print(Out);
1135 return;
1136 }
1137
Chris Lattnercfb5a202008-08-19 05:06:27 +00001138 char Prefix = '%';
1139 int Slot;
Chris Lattnerfb5179a2011-08-03 06:15:41 +00001140 // If we have a SlotTracker, use it.
Chris Lattnercfb5a202008-08-19 05:06:27 +00001141 if (Machine) {
1142 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1143 Slot = Machine->getGlobalSlot(GV);
1144 Prefix = '@';
1145 } else {
1146 Slot = Machine->getLocalSlot(V);
Andrew Trick18801ec2011-09-30 19:48:58 +00001147
Chris Lattnerfb5179a2011-08-03 06:15:41 +00001148 // If the local value didn't succeed, then we may be referring to a value
1149 // from a different function. Translate it, as this can happen when using
1150 // address of blocks.
1151 if (Slot == -1)
1152 if ((Machine = createSlotTracker(V))) {
1153 Slot = Machine->getLocalSlot(V);
1154 delete Machine;
1155 }
Chris Lattnercfb5a202008-08-19 05:06:27 +00001156 }
Chris Lattnerfb5179a2011-08-03 06:15:41 +00001157 } else if ((Machine = createSlotTracker(V))) {
1158 // Otherwise, create one to get the # and then destroy it.
1159 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1160 Slot = Machine->getGlobalSlot(GV);
1161 Prefix = '@';
Chris Lattner80cd1152006-01-25 22:26:05 +00001162 } else {
Chris Lattnerfb5179a2011-08-03 06:15:41 +00001163 Slot = Machine->getLocalSlot(V);
Chris Lattner7a716ad2002-04-16 21:36:08 +00001164 }
Chris Lattnerfb5179a2011-08-03 06:15:41 +00001165 delete Machine;
1166 Machine = 0;
1167 } else {
1168 Slot = -1;
Chris Lattner7a716ad2002-04-16 21:36:08 +00001169 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001170
Chris Lattnercfb5a202008-08-19 05:06:27 +00001171 if (Slot != -1)
1172 Out << Prefix << Slot;
1173 else
1174 Out << "<badref>";
Chris Lattner7a716ad2002-04-16 21:36:08 +00001175}
1176
Daniel Malead0fef322013-05-08 20:38:31 +00001177void WriteAsOperand(raw_ostream &Out, const Value *V,
1178 bool PrintType, const Module *Context) {
Dan Gohmand6c0f652009-08-13 15:27:57 +00001179
1180 // Fast path: Don't construct and populate a TypePrinting object if we
1181 // won't be needing any types printed.
Dan Gohman009fc9e2009-08-13 23:07:11 +00001182 if (!PrintType &&
Dan Gohman3bdfbf52010-07-20 23:55:01 +00001183 ((!isa<Constant>(V) && !isa<MDNode>(V)) ||
1184 V->hasName() || isa<GlobalValue>(V))) {
1185 WriteAsOperandInternal(Out, V, 0, 0, Context);
Dan Gohmand6c0f652009-08-13 15:27:57 +00001186 return;
1187 }
1188
Chris Lattner607dc682002-07-10 16:48:17 +00001189 if (Context == 0) Context = getModuleFromVal(V);
Chris Lattner207b5bc2001-10-29 16:37:48 +00001190
Chris Lattnere9fa33e2009-02-28 23:20:19 +00001191 TypePrinting TypePrinter;
Chris Lattner1afcace2011-07-09 17:41:24 +00001192 if (Context)
1193 TypePrinter.incorporateTypes(*Context);
Dan Gohman8dae1382008-09-14 17:21:12 +00001194 if (PrintType) {
Chris Lattner0f7364b2009-02-28 21:26:53 +00001195 TypePrinter.print(V->getType(), Out);
Dan Gohman8dae1382008-09-14 17:21:12 +00001196 Out << ' ';
1197 }
Misha Brukmanfd939082005-04-21 23:48:37 +00001198
Dan Gohman3bdfbf52010-07-20 23:55:01 +00001199 WriteAsOperandInternal(Out, V, &TypePrinter, 0, Context);
Chris Lattner622f7402001-07-20 19:15:21 +00001200}
1201
Daniel Malead0fef322013-05-08 20:38:31 +00001202void AssemblyWriter::init() {
1203 if (TheModule)
1204 TypePrinter.incorporateTypes(*TheModule);
1205}
Chris Lattnerd8c2e422001-07-12 23:35:26 +00001206
Andrew Trick18801ec2011-09-30 19:48:58 +00001207
Daniel Malead0fef322013-05-08 20:38:31 +00001208AssemblyWriter::AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac,
1209 const Module *M,
1210 AssemblyAnnotationWriter *AAW)
1211 : Out(o), TheModule(M), Machine(Mac), AnnotationWriter(AAW) {
1212 init();
1213}
Chris Lattner00950542001-06-06 20:29:01 +00001214
Daniel Malead0fef322013-05-08 20:38:31 +00001215AssemblyWriter::AssemblyWriter(formatted_raw_ostream &o, const Module *M,
1216 AssemblyAnnotationWriter *AAW)
1217 : Out(o), TheModule(M), ModuleSlotTracker(createSlotTracker(M)),
1218 Machine(*ModuleSlotTracker), AnnotationWriter(AAW) {
1219 init();
1220}
Andrew Trick18801ec2011-09-30 19:48:58 +00001221
Daniel Malead0fef322013-05-08 20:38:31 +00001222AssemblyWriter::~AssemblyWriter() { }
Chris Lattner00950542001-06-06 20:29:01 +00001223
Chris Lattner2fcfdb72006-12-06 06:24:27 +00001224void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType) {
1225 if (Operand == 0) {
Chris Lattneraab18202005-02-24 16:58:29 +00001226 Out << "<null operand!>";
Chris Lattnerc65b72c2009-12-31 02:33:14 +00001227 return;
Chris Lattneraab18202005-02-24 16:58:29 +00001228 }
Chris Lattnerc65b72c2009-12-31 02:33:14 +00001229 if (PrintType) {
1230 TypePrinter.print(Operand->getType(), Out);
1231 Out << ' ';
1232 }
Dan Gohman3bdfbf52010-07-20 23:55:01 +00001233 WriteAsOperandInternal(Out, Operand, &TypePrinter, &Machine, TheModule);
Chris Lattner00950542001-06-06 20:29:01 +00001234}
1235
Eli Friedman47f35132011-07-25 23:16:38 +00001236void AssemblyWriter::writeAtomic(AtomicOrdering Ordering,
1237 SynchronizationScope SynchScope) {
1238 if (Ordering == NotAtomic)
1239 return;
1240
1241 switch (SynchScope) {
Eli Friedman47f35132011-07-25 23:16:38 +00001242 case SingleThread: Out << " singlethread"; break;
1243 case CrossThread: break;
1244 }
1245
1246 switch (Ordering) {
1247 default: Out << " <bad ordering " << int(Ordering) << ">"; break;
1248 case Unordered: Out << " unordered"; break;
1249 case Monotonic: Out << " monotonic"; break;
1250 case Acquire: Out << " acquire"; break;
1251 case Release: Out << " release"; break;
1252 case AcquireRelease: Out << " acq_rel"; break;
1253 case SequentiallyConsistent: Out << " seq_cst"; break;
1254 }
1255}
1256
Daniel Dunbara279bc32009-09-20 02:20:51 +00001257void AssemblyWriter::writeParamOperand(const Value *Operand,
Bill Wendling94e94b32012-12-30 13:50:49 +00001258 AttributeSet Attrs, unsigned Idx) {
Duncan Sandsdc024672007-11-27 13:23:08 +00001259 if (Operand == 0) {
1260 Out << "<null operand!>";
Chris Lattnerc65b72c2009-12-31 02:33:14 +00001261 return;
Duncan Sandsdc024672007-11-27 13:23:08 +00001262 }
Chris Lattnerc65b72c2009-12-31 02:33:14 +00001263
1264 // Print the type
1265 TypePrinter.print(Operand->getType(), Out);
1266 // Print parameter attributes list
Bill Wendling94e94b32012-12-30 13:50:49 +00001267 if (Attrs.hasAttributes(Idx))
1268 Out << ' ' << Attrs.getAsString(Idx);
Chris Lattnerc65b72c2009-12-31 02:33:14 +00001269 Out << ' ';
1270 // Print the operand
Dan Gohman3bdfbf52010-07-20 23:55:01 +00001271 WriteAsOperandInternal(Out, Operand, &TypePrinter, &Machine, TheModule);
Duncan Sandsdc024672007-11-27 13:23:08 +00001272}
Chris Lattner00950542001-06-06 20:29:01 +00001273
Chris Lattnerc1824992001-10-29 16:05:51 +00001274void AssemblyWriter::printModule(const Module *M) {
Bill Wendlingb29ce262013-02-11 08:43:33 +00001275 Machine.initialize();
1276
Chris Lattner31ab1b32005-03-02 23:12:40 +00001277 if (!M->getModuleIdentifier().empty() &&
Misha Brukmanfd939082005-04-21 23:48:37 +00001278 // Don't print the ID if it will start a new line (which would
Chris Lattner31ab1b32005-03-02 23:12:40 +00001279 // require a comment char before it).
1280 M->getModuleIdentifier().find('\n') == std::string::npos)
1281 Out << "; ModuleID = '" << M->getModuleIdentifier() << "'\n";
1282
Owen Andersoncf7ff2b2006-10-18 02:21:12 +00001283 if (!M->getDataLayout().empty())
Chris Lattnerd2f9e602006-10-22 06:06:56 +00001284 Out << "target datalayout = \"" << M->getDataLayout() << "\"\n";
Reid Spencercddc86f2004-07-25 21:44:54 +00001285 if (!M->getTargetTriple().empty())
Reid Spencerc9a1f0d2004-07-25 21:29:43 +00001286 Out << "target triple = \"" << M->getTargetTriple() << "\"\n";
Misha Brukmanfd939082005-04-21 23:48:37 +00001287
Chris Lattnercc041ba2006-01-24 04:13:11 +00001288 if (!M->getModuleInlineAsm().empty()) {
Chris Lattner42a162e2006-01-24 00:45:30 +00001289 // Split the string into lines, to make it easier to read the .ll file.
Chris Lattnercc041ba2006-01-24 04:13:11 +00001290 std::string Asm = M->getModuleInlineAsm();
Chris Lattner42a162e2006-01-24 00:45:30 +00001291 size_t CurPos = 0;
1292 size_t NewLine = Asm.find_first_of('\n', CurPos);
Dan Gohman9bf0b9b2009-08-12 23:54:22 +00001293 Out << '\n';
Chris Lattner42a162e2006-01-24 00:45:30 +00001294 while (NewLine != std::string::npos) {
1295 // We found a newline, print the portion of the asm string from the
1296 // last newline up to this newline.
1297 Out << "module asm \"";
1298 PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.begin()+NewLine),
1299 Out);
1300 Out << "\"\n";
1301 CurPos = NewLine+1;
1302 NewLine = Asm.find_first_of('\n', CurPos);
1303 }
Rafael Espindola38c4e532011-03-02 04:14:42 +00001304 std::string rest(Asm.begin()+CurPos, Asm.end());
1305 if (!rest.empty()) {
1306 Out << "module asm \"";
1307 PrintEscapedString(rest, Out);
1308 Out << "\"\n";
1309 }
Chris Lattner18365502006-01-23 23:03:36 +00001310 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001311
Chris Lattner1afcace2011-07-09 17:41:24 +00001312 printTypeIdentities();
Misha Brukmanfd939082005-04-21 23:48:37 +00001313
Dan Gohman9bf0b9b2009-08-12 23:54:22 +00001314 // Output all globals.
1315 if (!M->global_empty()) Out << '\n';
Chris Lattnerd6d826c2006-12-06 04:41:52 +00001316 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
Duncan Sands79da6ef2012-09-12 09:55:51 +00001317 I != E; ++I) {
1318 printGlobal(I); Out << '\n';
1319 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001320
Chris Lattner69dacfc2007-04-26 02:24:10 +00001321 // Output all aliases.
1322 if (!M->alias_empty()) Out << "\n";
1323 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
1324 I != E; ++I)
1325 printAlias(I);
Chris Lattner007377f2001-09-07 16:36:04 +00001326
Chris Lattner44da7d72004-09-14 05:06:58 +00001327 // Output all of the functions.
Chris Lattner7e708292002-06-25 16:13:24 +00001328 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
1329 printFunction(I);
Devang Patel320671d2009-07-08 21:44:25 +00001330
Bill Wendlingb29ce262013-02-11 08:43:33 +00001331 // Output all attribute groups.
Bill Wendling725dae52013-04-29 23:48:06 +00001332 if (!Machine.as_empty()) {
Bill Wendlingb29ce262013-02-11 08:43:33 +00001333 Out << '\n';
1334 writeAllAttributeGroups();
1335 }
1336
Devang Patel37c4a2d2009-07-29 22:04:47 +00001337 // Output named metadata.
Dan Gohman9bf0b9b2009-08-12 23:54:22 +00001338 if (!M->named_metadata_empty()) Out << '\n';
Andrew Trick18801ec2011-09-30 19:48:58 +00001339
Devang Patel37c4a2d2009-07-29 22:04:47 +00001340 for (Module::const_named_metadata_iterator I = M->named_metadata_begin(),
Chris Lattner6e6b1802009-12-31 02:13:35 +00001341 E = M->named_metadata_end(); I != E; ++I)
Chris Lattnerfdb33562009-12-31 01:54:05 +00001342 printNamedMDNode(I);
Devang Patel37c4a2d2009-07-29 22:04:47 +00001343
1344 // Output metadata.
Chris Lattner307c9892009-12-31 02:20:11 +00001345 if (!Machine.mdn_empty()) {
Chris Lattner6e6b1802009-12-31 02:13:35 +00001346 Out << '\n';
1347 writeAllMDNodes();
1348 }
Chris Lattner007377f2001-09-07 16:36:04 +00001349}
1350
Chris Lattnerfdb33562009-12-31 01:54:05 +00001351void AssemblyWriter::printNamedMDNode(const NamedMDNode *NMD) {
Nick Lewycky9100a782011-06-15 06:37:58 +00001352 Out << '!';
1353 StringRef Name = NMD->getName();
1354 if (Name.empty()) {
1355 Out << "<empty name> ";
1356 } else {
Guy Benyei87d0b9e2013-02-12 21:21:59 +00001357 if (isalpha(static_cast<unsigned char>(Name[0])) ||
1358 Name[0] == '-' || Name[0] == '$' ||
Nick Lewycky9100a782011-06-15 06:37:58 +00001359 Name[0] == '.' || Name[0] == '_')
1360 Out << Name[0];
1361 else
1362 Out << '\\' << hexdigit(Name[0] >> 4) << hexdigit(Name[0] & 0x0F);
1363 for (unsigned i = 1, e = Name.size(); i != e; ++i) {
1364 unsigned char C = Name[i];
Guy Benyei87d0b9e2013-02-12 21:21:59 +00001365 if (isalnum(static_cast<unsigned char>(C)) || C == '-' || C == '$' ||
1366 C == '.' || C == '_')
Nick Lewycky9100a782011-06-15 06:37:58 +00001367 Out << C;
1368 else
1369 Out << '\\' << hexdigit(C >> 4) << hexdigit(C & 0x0F);
1370 }
1371 }
1372 Out << " = !{";
Chris Lattnerfdb33562009-12-31 01:54:05 +00001373 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1374 if (i) Out << ", ";
Dan Gohman3da076f2010-09-09 20:53:58 +00001375 int Slot = Machine.getMetadataSlot(NMD->getOperand(i));
1376 if (Slot == -1)
1377 Out << "<badref>";
1378 else
1379 Out << '!' << Slot;
Chris Lattnerfdb33562009-12-31 01:54:05 +00001380 }
1381 Out << "}\n";
1382}
1383
1384
Dan Gohman683e9222009-08-12 17:23:50 +00001385static void PrintLinkage(GlobalValue::LinkageTypes LT,
1386 formatted_raw_ostream &Out) {
Chris Lattnercfb5a202008-08-19 05:06:27 +00001387 switch (LT) {
Bill Wendling3d10a5a2009-07-20 01:03:30 +00001388 case GlobalValue::ExternalLinkage: break;
1389 case GlobalValue::PrivateLinkage: Out << "private "; break;
1390 case GlobalValue::LinkerPrivateLinkage: Out << "linker_private "; break;
Bill Wendling5e721d72010-07-01 21:55:59 +00001391 case GlobalValue::LinkerPrivateWeakLinkage:
1392 Out << "linker_private_weak ";
1393 break;
Bill Wendling3d10a5a2009-07-20 01:03:30 +00001394 case GlobalValue::InternalLinkage: Out << "internal "; break;
1395 case GlobalValue::LinkOnceAnyLinkage: Out << "linkonce "; break;
1396 case GlobalValue::LinkOnceODRLinkage: Out << "linkonce_odr "; break;
Bill Wendling32811be2012-08-17 18:33:14 +00001397 case GlobalValue::LinkOnceODRAutoHideLinkage:
1398 Out << "linkonce_odr_auto_hide ";
1399 break;
Bill Wendling3d10a5a2009-07-20 01:03:30 +00001400 case GlobalValue::WeakAnyLinkage: Out << "weak "; break;
1401 case GlobalValue::WeakODRLinkage: Out << "weak_odr "; break;
1402 case GlobalValue::CommonLinkage: Out << "common "; break;
1403 case GlobalValue::AppendingLinkage: Out << "appending "; break;
1404 case GlobalValue::DLLImportLinkage: Out << "dllimport "; break;
1405 case GlobalValue::DLLExportLinkage: Out << "dllexport "; break;
1406 case GlobalValue::ExternalWeakLinkage: Out << "extern_weak "; break;
Chris Lattner266c7bb2009-04-13 05:44:34 +00001407 case GlobalValue::AvailableExternallyLinkage:
1408 Out << "available_externally ";
1409 break;
Chris Lattnercfb5a202008-08-19 05:06:27 +00001410 }
1411}
Duncan Sands667d4b82009-03-07 15:45:40 +00001412
Chris Lattnercfb5a202008-08-19 05:06:27 +00001413
1414static void PrintVisibility(GlobalValue::VisibilityTypes Vis,
Dan Gohman683e9222009-08-12 17:23:50 +00001415 formatted_raw_ostream &Out) {
Chris Lattnercfb5a202008-08-19 05:06:27 +00001416 switch (Vis) {
Chris Lattnercfb5a202008-08-19 05:06:27 +00001417 case GlobalValue::DefaultVisibility: break;
1418 case GlobalValue::HiddenVisibility: Out << "hidden "; break;
1419 case GlobalValue::ProtectedVisibility: Out << "protected "; break;
1420 }
1421}
1422
Hans Wennborgce718ff2012-06-23 11:37:03 +00001423static void PrintThreadLocalModel(GlobalVariable::ThreadLocalMode TLM,
1424 formatted_raw_ostream &Out) {
1425 switch (TLM) {
1426 case GlobalVariable::NotThreadLocal:
1427 break;
1428 case GlobalVariable::GeneralDynamicTLSModel:
1429 Out << "thread_local ";
1430 break;
1431 case GlobalVariable::LocalDynamicTLSModel:
1432 Out << "thread_local(localdynamic) ";
1433 break;
1434 case GlobalVariable::InitialExecTLSModel:
1435 Out << "thread_local(initialexec) ";
1436 break;
1437 case GlobalVariable::LocalExecTLSModel:
1438 Out << "thread_local(localexec) ";
1439 break;
1440 }
1441}
1442
Chris Lattnerc1824992001-10-29 16:05:51 +00001443void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Dan Gohman4483c7b2010-01-29 23:12:36 +00001444 if (GV->isMaterializable())
1445 Out << "; Materializable\n";
1446
Dan Gohman3bdfbf52010-07-20 23:55:01 +00001447 WriteAsOperandInternal(Out, GV, &TypePrinter, &Machine, GV->getParent());
Dan Gohman3845e502009-08-12 23:32:33 +00001448 Out << " = ";
Chris Lattnerd70684f2001-09-18 04:01:05 +00001449
Chris Lattner52b26de2008-08-19 05:16:28 +00001450 if (!GV->hasInitializer() && GV->hasExternalLinkage())
1451 Out << "external ";
Daniel Dunbara279bc32009-09-20 02:20:51 +00001452
Chris Lattner52b26de2008-08-19 05:16:28 +00001453 PrintLinkage(GV->getLinkage(), Out);
1454 PrintVisibility(GV->getVisibility(), Out);
Hans Wennborgce718ff2012-06-23 11:37:03 +00001455 PrintThreadLocalModel(GV->getThreadLocalMode(), Out);
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00001456
Chris Lattnerdf986172009-01-02 07:01:27 +00001457 if (unsigned AddressSpace = GV->getType()->getAddressSpace())
1458 Out << "addrspace(" << AddressSpace << ") ";
Rafael Espindolabea46262011-01-08 16:42:36 +00001459 if (GV->hasUnnamedAddr()) Out << "unnamed_addr ";
Michael Gottesmana2de37c2013-02-05 05:57:38 +00001460 if (GV->isExternallyInitialized()) Out << "externally_initialized ";
Misha Brukman0313e0b2004-06-21 21:53:56 +00001461 Out << (GV->isConstant() ? "constant " : "global ");
Chris Lattner0f7364b2009-02-28 21:26:53 +00001462 TypePrinter.print(GV->getType()->getElementType(), Out);
Chris Lattnerd70684f2001-09-18 04:01:05 +00001463
Dan Gohman8dae1382008-09-14 17:21:12 +00001464 if (GV->hasInitializer()) {
1465 Out << ' ';
Devang Patel320671d2009-07-08 21:44:25 +00001466 writeOperand(GV->getInitializer(), false);
Dan Gohman8dae1382008-09-14 17:21:12 +00001467 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001468
Chris Lattner8fff1262010-07-07 23:16:37 +00001469 if (GV->hasSection()) {
1470 Out << ", section \"";
1471 PrintEscapedString(GV->getSection(), Out);
1472 Out << '"';
1473 }
Chris Lattner60962db2005-11-12 00:10:19 +00001474 if (GV->getAlignment())
Chris Lattner30caa282005-11-06 06:48:53 +00001475 Out << ", align " << GV->getAlignment();
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001476
Chris Lattner7e708292002-06-25 16:13:24 +00001477 printInfoComment(*GV);
Chris Lattner70cc3392001-09-10 07:58:01 +00001478}
1479
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001480void AssemblyWriter::printAlias(const GlobalAlias *GA) {
Dan Gohman4483c7b2010-01-29 23:12:36 +00001481 if (GA->isMaterializable())
1482 Out << "; Materializable\n";
1483
Dale Johannesen24f07dc2008-06-03 18:14:29 +00001484 // Don't crash when dumping partially built GA
1485 if (!GA->hasName())
1486 Out << "<<nameless>> = ";
Chris Lattnerc97536e2008-08-17 04:40:13 +00001487 else {
1488 PrintLLVMName(Out, GA);
1489 Out << " = ";
1490 }
Chris Lattnercfb5a202008-08-19 05:06:27 +00001491 PrintVisibility(GA->getVisibility(), Out);
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001492
1493 Out << "alias ";
1494
Chris Lattnercfb5a202008-08-19 05:06:27 +00001495 PrintLinkage(GA->getLinkage(), Out);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001496
Anton Korobeynikovc6c98af2007-04-29 18:02:48 +00001497 const Constant *Aliasee = GA->getAliasee();
Daniel Dunbara279bc32009-09-20 02:20:51 +00001498
Chris Lattner1afcace2011-07-09 17:41:24 +00001499 if (Aliasee == 0) {
1500 TypePrinter.print(GA->getType(), Out);
1501 Out << " <<NULL ALIASEE>>";
Jay Foad5cd8ea22011-08-01 12:48:54 +00001502 } else {
Jay Foad8d948652011-08-01 12:29:14 +00001503 writeOperand(Aliasee, !isa<ConstantExpr>(Aliasee));
Jay Foad5cd8ea22011-08-01 12:48:54 +00001504 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001505
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001506 printInfoComment(*GA);
Chris Lattner52b26de2008-08-19 05:16:28 +00001507 Out << '\n';
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001508}
1509
Chris Lattner1afcace2011-07-09 17:41:24 +00001510void AssemblyWriter::printTypeIdentities() {
1511 if (TypePrinter.NumberedTypes.empty() &&
1512 TypePrinter.NamedTypes.empty())
1513 return;
Andrew Trick18801ec2011-09-30 19:48:58 +00001514
Chris Lattner1afcace2011-07-09 17:41:24 +00001515 Out << '\n';
Andrew Trick18801ec2011-09-30 19:48:58 +00001516
Chris Lattner1afcace2011-07-09 17:41:24 +00001517 // We know all the numbers that each type is used and we know that it is a
1518 // dense assignment. Convert the map to an index table.
1519 std::vector<StructType*> NumberedTypes(TypePrinter.NumberedTypes.size());
Andrew Trick18801ec2011-09-30 19:48:58 +00001520 for (DenseMap<StructType*, unsigned>::iterator I =
Chris Lattner1afcace2011-07-09 17:41:24 +00001521 TypePrinter.NumberedTypes.begin(), E = TypePrinter.NumberedTypes.end();
1522 I != E; ++I) {
1523 assert(I->second < NumberedTypes.size() && "Didn't get a dense numbering?");
1524 NumberedTypes[I->second] = I->first;
1525 }
Andrew Trick18801ec2011-09-30 19:48:58 +00001526
Chris Lattner413fd232009-03-01 00:03:38 +00001527 // Emit all numbered types.
1528 for (unsigned i = 0, e = NumberedTypes.size(); i != e; ++i) {
Dan Gohman3845e502009-08-12 23:32:33 +00001529 Out << '%' << i << " = type ";
Andrew Trick18801ec2011-09-30 19:48:58 +00001530
Chris Lattner413fd232009-03-01 00:03:38 +00001531 // Make sure we print out at least one level of the type structure, so
1532 // that we do not get %2 = type %2
Chris Lattner1afcace2011-07-09 17:41:24 +00001533 TypePrinter.printStructBody(NumberedTypes[i], Out);
Dan Gohman9bf0b9b2009-08-12 23:54:22 +00001534 Out << '\n';
Chris Lattner413fd232009-03-01 00:03:38 +00001535 }
Andrew Trick18801ec2011-09-30 19:48:58 +00001536
Chris Lattner1afcace2011-07-09 17:41:24 +00001537 for (unsigned i = 0, e = TypePrinter.NamedTypes.size(); i != e; ++i) {
1538 PrintLLVMName(Out, TypePrinter.NamedTypes[i]->getName(), LocalPrefix);
Chris Lattner52b26de2008-08-19 05:16:28 +00001539 Out << " = type ";
Reid Spencer9231ac82004-05-25 08:53:40 +00001540
1541 // Make sure we print out at least one level of the type structure, so
1542 // that we do not get %FILE = type %FILE
Chris Lattner1afcace2011-07-09 17:41:24 +00001543 TypePrinter.printStructBody(TypePrinter.NamedTypes[i], Out);
Chris Lattnercfb5a202008-08-19 05:06:27 +00001544 Out << '\n';
Reid Spencer9231ac82004-05-25 08:53:40 +00001545 }
Reid Spencer78d033e2007-01-06 07:24:44 +00001546}
1547
Misha Brukmanab5c6002004-03-02 00:22:19 +00001548/// printFunction - Print all aspects of a function.
1549///
Chris Lattner7e708292002-06-25 16:13:24 +00001550void AssemblyWriter::printFunction(const Function *F) {
Chris Lattnercfb5a202008-08-19 05:06:27 +00001551 // Print out the return type and name.
1552 Out << '\n';
Chris Lattner4ad02e72003-04-16 20:28:45 +00001553
Misha Brukman0313e0b2004-06-21 21:53:56 +00001554 if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, Out);
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001555
Dan Gohman4483c7b2010-01-29 23:12:36 +00001556 if (F->isMaterializable())
1557 Out << "; Materializable\n";
1558
Bill Wendling63405492013-04-16 20:55:47 +00001559 const AttributeSet &Attrs = F->getAttributes();
Bill Wendling725dae52013-04-29 23:48:06 +00001560 if (Attrs.hasAttributes(AttributeSet::FunctionIndex)) {
Bill Wendling63405492013-04-16 20:55:47 +00001561 AttributeSet AS = Attrs.getFnAttributes();
Rafael Espindolaaae02982013-05-01 13:07:03 +00001562 std::string AttrStr;
1563
1564 unsigned Idx = 0;
1565 for (unsigned E = AS.getNumSlots(); Idx != E; ++Idx)
1566 if (AS.getSlotIndex(Idx) == AttributeSet::FunctionIndex)
1567 break;
1568
1569 for (AttributeSet::iterator I = AS.begin(Idx), E = AS.end(Idx);
1570 I != E; ++I) {
1571 Attribute Attr = *I;
1572 if (!Attr.isStringAttribute()) {
1573 if (!AttrStr.empty()) AttrStr += ' ';
1574 AttrStr += Attr.getAsString();
1575 }
1576 }
1577
Bill Wendling63405492013-04-16 20:55:47 +00001578 if (!AttrStr.empty())
1579 Out << "; Function Attrs: " << AttrStr << '\n';
1580 }
1581
Reid Spencer5cbf9852007-01-30 20:08:39 +00001582 if (F->isDeclaration())
Chris Lattner3aa60662007-08-19 22:15:26 +00001583 Out << "declare ";
1584 else
Reid Spencerb951bc02006-12-29 20:29:48 +00001585 Out << "define ";
Daniel Dunbara279bc32009-09-20 02:20:51 +00001586
Chris Lattnercfb5a202008-08-19 05:06:27 +00001587 PrintLinkage(F->getLinkage(), Out);
1588 PrintVisibility(F->getVisibility(), Out);
Chris Lattner4ad02e72003-04-16 20:28:45 +00001589
Chris Lattnerd5118982005-05-06 20:26:43 +00001590 // Print the calling convention.
Micah Villmowd3766df2012-09-13 15:11:12 +00001591 if (F->getCallingConv() != CallingConv::C) {
1592 PrintCallingConv(F->getCallingConv(), Out);
1593 Out << " ";
Chris Lattnerd5118982005-05-06 20:26:43 +00001594 }
1595
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001596 FunctionType *FT = F->getFunctionType();
Bill Wendling1b0c54f2013-01-18 21:53:16 +00001597 if (Attrs.hasAttributes(AttributeSet::ReturnIndex))
1598 Out << Attrs.getAsString(AttributeSet::ReturnIndex) << ' ';
Chris Lattner0f7364b2009-02-28 21:26:53 +00001599 TypePrinter.print(F->getReturnType(), Out);
Chris Lattner4667b712008-08-19 05:26:17 +00001600 Out << ' ';
Dan Gohman3bdfbf52010-07-20 23:55:01 +00001601 WriteAsOperandInternal(Out, F, &TypePrinter, &Machine, F->getParent());
Misha Brukman0313e0b2004-06-21 21:53:56 +00001602 Out << '(';
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001603 Machine.incorporateFunction(F);
Chris Lattner007377f2001-09-07 16:36:04 +00001604
Chris Lattnerc1824992001-10-29 16:05:51 +00001605 // Loop over the arguments, printing them...
Chris Lattner007377f2001-09-07 16:36:04 +00001606
Reid Spencerbd5db8e2006-12-31 05:24:50 +00001607 unsigned Idx = 1;
Chris Lattner8dcd2f12007-04-18 00:57:22 +00001608 if (!F->isDeclaration()) {
1609 // If this isn't a declaration, print the argument names as well.
1610 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
1611 I != E; ++I) {
1612 // Insert commas as we go... the first arg doesn't get a comma
1613 if (I != F->arg_begin()) Out << ", ";
Bill Wendling94e94b32012-12-30 13:50:49 +00001614 printArgument(I, Attrs, Idx);
Chris Lattner8dcd2f12007-04-18 00:57:22 +00001615 Idx++;
1616 }
1617 } else {
1618 // Otherwise, print the types from the function type.
1619 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
1620 // Insert commas as we go... the first arg doesn't get a comma
1621 if (i) Out << ", ";
Daniel Dunbara279bc32009-09-20 02:20:51 +00001622
Chris Lattner8dcd2f12007-04-18 00:57:22 +00001623 // Output type...
Chris Lattner0f7364b2009-02-28 21:26:53 +00001624 TypePrinter.print(FT->getParamType(i), Out);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001625
Bill Wendling94e94b32012-12-30 13:50:49 +00001626 if (Attrs.hasAttributes(i+1))
1627 Out << ' ' << Attrs.getAsString(i+1);
Chris Lattner8dcd2f12007-04-18 00:57:22 +00001628 }
Reid Spencerbd5db8e2006-12-31 05:24:50 +00001629 }
Chris Lattner007377f2001-09-07 16:36:04 +00001630
1631 // Finish printing arguments...
Chris Lattner7e708292002-06-25 16:13:24 +00001632 if (FT->isVarArg()) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001633 if (FT->getNumParams()) Out << ", ";
1634 Out << "..."; // Output varargs portion of signature!
Chris Lattner007377f2001-09-07 16:36:04 +00001635 }
Misha Brukman0313e0b2004-06-21 21:53:56 +00001636 Out << ')';
Rafael Espindola3971df52011-01-25 19:09:56 +00001637 if (F->hasUnnamedAddr())
1638 Out << " unnamed_addr";
Bill Wendling725dae52013-04-29 23:48:06 +00001639 if (Attrs.hasAttributes(AttributeSet::FunctionIndex))
1640 Out << " #" << Machine.getAttributeGroupSlot(Attrs.getFnAttributes());
Chris Lattner8fff1262010-07-07 23:16:37 +00001641 if (F->hasSection()) {
1642 Out << " section \"";
1643 PrintEscapedString(F->getSection(), Out);
1644 Out << '"';
1645 }
Chris Lattner30caa282005-11-06 06:48:53 +00001646 if (F->getAlignment())
1647 Out << " align " << F->getAlignment();
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001648 if (F->hasGC())
1649 Out << " gc \"" << F->getGC() << '"';
Reid Spencer5cbf9852007-01-30 20:08:39 +00001650 if (F->isDeclaration()) {
Chris Lattner91fb4072010-09-02 22:52:10 +00001651 Out << '\n';
Chris Lattner03e2acb2002-05-06 03:00:40 +00001652 } else {
Chris Lattner91fb4072010-09-02 22:52:10 +00001653 Out << " {";
1654 // Output all of the function's basic blocks.
Chris Lattner7e708292002-06-25 16:13:24 +00001655 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
1656 printBasicBlock(I);
Chris Lattner007377f2001-09-07 16:36:04 +00001657
Misha Brukman0313e0b2004-06-21 21:53:56 +00001658 Out << "}\n";
Chris Lattner007377f2001-09-07 16:36:04 +00001659 }
1660
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001661 Machine.purgeFunction();
Chris Lattner00950542001-06-06 20:29:01 +00001662}
1663
Misha Brukmanab5c6002004-03-02 00:22:19 +00001664/// printArgument - This member is called for every argument that is passed into
1665/// the function. Simply print it out
1666///
Daniel Dunbara279bc32009-09-20 02:20:51 +00001667void AssemblyWriter::printArgument(const Argument *Arg,
Bill Wendling94e94b32012-12-30 13:50:49 +00001668 AttributeSet Attrs, unsigned Idx) {
Chris Lattner00950542001-06-06 20:29:01 +00001669 // Output type...
Chris Lattner0f7364b2009-02-28 21:26:53 +00001670 TypePrinter.print(Arg->getType(), Out);
Misha Brukmanfd939082005-04-21 23:48:37 +00001671
Duncan Sandsdc024672007-11-27 13:23:08 +00001672 // Output parameter attributes list
Bill Wendling94e94b32012-12-30 13:50:49 +00001673 if (Attrs.hasAttributes(Idx))
1674 Out << ' ' << Attrs.getAsString(Idx);
Reid Spencerbd5db8e2006-12-31 05:24:50 +00001675
Chris Lattner00950542001-06-06 20:29:01 +00001676 // Output name, if available...
Chris Lattnerc97536e2008-08-17 04:40:13 +00001677 if (Arg->hasName()) {
1678 Out << ' ';
1679 PrintLLVMName(Out, Arg);
1680 }
Chris Lattner00950542001-06-06 20:29:01 +00001681}
1682
Misha Brukmanab5c6002004-03-02 00:22:19 +00001683/// printBasicBlock - This member is called for each basic block in a method.
1684///
Chris Lattnerc1824992001-10-29 16:05:51 +00001685void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Nick Lewycky280a6e62008-04-25 16:53:59 +00001686 if (BB->hasName()) { // Print out the label if it exists...
Chris Lattnerc97536e2008-08-17 04:40:13 +00001687 Out << "\n";
Daniel Dunbar03d76512009-07-25 23:55:21 +00001688 PrintLLVMName(Out, BB->getName(), LabelPrefix);
Chris Lattnerc97536e2008-08-17 04:40:13 +00001689 Out << ':';
Nick Lewycky280a6e62008-04-25 16:53:59 +00001690 } else if (!BB->use_empty()) { // Don't print block # of no uses...
Bill Wendling5d7a5a42011-04-10 23:18:04 +00001691 Out << "\n; <label>:";
Chris Lattner22379bc2007-01-11 03:54:27 +00001692 int Slot = Machine.getLocalSlot(BB);
Chris Lattner69566452004-06-09 19:41:19 +00001693 if (Slot != -1)
Misha Brukman0313e0b2004-06-21 21:53:56 +00001694 Out << Slot;
Chris Lattner69566452004-06-09 19:41:19 +00001695 else
Misha Brukman0313e0b2004-06-21 21:53:56 +00001696 Out << "<badref>";
Chris Lattner061269b2002-10-02 19:38:55 +00001697 }
Chris Lattner4e4d8622003-11-20 00:09:43 +00001698
Dan Gohman683e9222009-08-12 17:23:50 +00001699 if (BB->getParent() == 0) {
Chris Lattner8f4b1ec2009-08-17 15:48:08 +00001700 Out.PadToColumn(50);
Dan Gohman683e9222009-08-12 17:23:50 +00001701 Out << "; Error: Block without parent!";
1702 } else if (BB != &BB->getParent()->getEntryBlock()) { // Not the entry block?
Chris Lattner91fb4072010-09-02 22:52:10 +00001703 // Output predecessors for the block.
Chris Lattner8f4b1ec2009-08-17 15:48:08 +00001704 Out.PadToColumn(50);
Dan Gohman683e9222009-08-12 17:23:50 +00001705 Out << ";";
Gabor Greif44424642010-03-25 23:25:28 +00001706 const_pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001707
Chris Lattnereb411292008-04-22 02:45:44 +00001708 if (PI == PE) {
1709 Out << " No predecessors!";
1710 } else {
Dan Gohman8dae1382008-09-14 17:21:12 +00001711 Out << " preds = ";
Chris Lattnereb411292008-04-22 02:45:44 +00001712 writeOperand(*PI, false);
1713 for (++PI; PI != PE; ++PI) {
Dan Gohman8dae1382008-09-14 17:21:12 +00001714 Out << ", ";
Chris Lattner2fcfdb72006-12-06 06:24:27 +00001715 writeOperand(*PI, false);
Chris Lattner40efcec2003-11-16 22:59:57 +00001716 }
Chris Lattner061269b2002-10-02 19:38:55 +00001717 }
Chris Lattner00950542001-06-06 20:29:01 +00001718 }
Misha Brukmanfd939082005-04-21 23:48:37 +00001719
Chris Lattnereb411292008-04-22 02:45:44 +00001720 Out << "\n";
Chris Lattner00950542001-06-06 20:29:01 +00001721
Misha Brukman0313e0b2004-06-21 21:53:56 +00001722 if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, Out);
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001723
Chris Lattner007377f2001-09-07 16:36:04 +00001724 // Output all of the instructions in the basic block...
Dan Gohmanbeca6892009-07-13 18:27:59 +00001725 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
Daniel Malead0fef322013-05-08 20:38:31 +00001726 printInstructionLine(*I);
Dan Gohmanbeca6892009-07-13 18:27:59 +00001727 }
Chris Lattner9f717ef2004-03-08 18:51:45 +00001728
Misha Brukman0313e0b2004-06-21 21:53:56 +00001729 if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, Out);
Chris Lattner00950542001-06-06 20:29:01 +00001730}
1731
Daniel Malead0fef322013-05-08 20:38:31 +00001732/// printInstructionLine - Print an instruction and a newline character.
1733void AssemblyWriter::printInstructionLine(const Instruction &I) {
1734 printInstruction(I);
1735 Out << '\n';
1736}
1737
Misha Brukmanab5c6002004-03-02 00:22:19 +00001738/// printInfoComment - Print a little comment after the instruction indicating
1739/// which slot it occupies.
1740///
Chris Lattner7e708292002-06-25 16:13:24 +00001741void AssemblyWriter::printInfoComment(const Value &V) {
Bill Wendling63405492013-04-16 20:55:47 +00001742 if (AnnotationWriter)
Dan Gohman7a5666e2010-02-10 20:41:46 +00001743 AnnotationWriter->printInfoComment(V, Out);
Chris Lattnere02fa852001-10-13 06:42:36 +00001744}
1745
Reid Spencer3a9ec242006-08-28 01:02:49 +00001746// This member is called for each Instruction in a function..
Chris Lattner7e708292002-06-25 16:13:24 +00001747void AssemblyWriter::printInstruction(const Instruction &I) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001748 if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, Out);
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001749
Dan Gohman3845e502009-08-12 23:32:33 +00001750 // Print out indentation for an instruction.
Dan Gohman01889ca2009-08-13 01:41:52 +00001751 Out << " ";
Chris Lattner00950542001-06-06 20:29:01 +00001752
1753 // Print out name if it exists...
Chris Lattnerc97536e2008-08-17 04:40:13 +00001754 if (I.hasName()) {
1755 PrintLLVMName(Out, &I);
1756 Out << " = ";
Chris Lattner4ee93c42009-12-29 07:25:48 +00001757 } else if (!I.getType()->isVoidTy()) {
Chris Lattner828db8a2008-08-29 17:19:30 +00001758 // Print out the def slot taken.
1759 int SlotNum = Machine.getLocalSlot(&I);
1760 if (SlotNum == -1)
1761 Out << "<badref> = ";
1762 else
1763 Out << '%' << SlotNum << " = ";
Chris Lattnerc97536e2008-08-17 04:40:13 +00001764 }
Andrew Trick18801ec2011-09-30 19:48:58 +00001765
Eli Friedman21006d42011-08-09 23:02:53 +00001766 if (isa<CallInst>(I) && cast<CallInst>(I).isTailCall())
Chris Lattnerddb6db42005-05-06 05:51:46 +00001767 Out << "tail ";
Chris Lattnere5e475e2003-09-08 17:45:59 +00001768
Chris Lattner00950542001-06-06 20:29:01 +00001769 // Print out the opcode...
Misha Brukman0313e0b2004-06-21 21:53:56 +00001770 Out << I.getOpcodeName();
Chris Lattner00950542001-06-06 20:29:01 +00001771
Eli Friedmanf03bb262011-08-12 22:50:01 +00001772 // If this is an atomic load or store, print out the atomic marker.
1773 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isAtomic()) ||
1774 (isa<StoreInst>(I) && cast<StoreInst>(I).isAtomic()))
1775 Out << " atomic";
1776
1777 // If this is a volatile operation, print out the volatile marker.
1778 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) ||
1779 (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile()) ||
1780 (isa<AtomicCmpXchgInst>(I) && cast<AtomicCmpXchgInst>(I).isVolatile()) ||
1781 (isa<AtomicRMWInst>(I) && cast<AtomicRMWInst>(I).isVolatile()))
1782 Out << " volatile";
1783
Dan Gohman59858cf2009-07-27 16:11:46 +00001784 // Print out optimization information.
1785 WriteOptimizationInfo(Out, &I);
1786
Reid Spencer74f16422006-12-03 06:27:29 +00001787 // Print out the compare instruction predicates
Nate Begemanac80ade2008-05-12 19:01:56 +00001788 if (const CmpInst *CI = dyn_cast<CmpInst>(&I))
Chris Lattnerab49ee72008-08-23 22:52:27 +00001789 Out << ' ' << getPredicateText(CI->getPredicate());
Reid Spencer74f16422006-12-03 06:27:29 +00001790
Eli Friedmanff030482011-07-28 21:48:00 +00001791 // Print out the atomicrmw operation
1792 if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(&I))
1793 writeAtomicRMWOperation(Out, RMWI->getOperation());
1794
Chris Lattner00950542001-06-06 20:29:01 +00001795 // Print out the type of the operands...
Chris Lattner7e708292002-06-25 16:13:24 +00001796 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0;
Chris Lattner00950542001-06-06 20:29:01 +00001797
1798 // Special case conditional branches to swizzle the condition out to the front
Gabor Greifccd27fb2009-02-09 15:45:06 +00001799 if (isa<BranchInst>(I) && cast<BranchInst>(I).isConditional()) {
David Blaikief12b3792013-02-11 01:16:51 +00001800 const BranchInst &BI(cast<BranchInst>(I));
Dan Gohman8dae1382008-09-14 17:21:12 +00001801 Out << ' ';
Gabor Greifccd27fb2009-02-09 15:45:06 +00001802 writeOperand(BI.getCondition(), true);
Dan Gohman8dae1382008-09-14 17:21:12 +00001803 Out << ", ";
Gabor Greifccd27fb2009-02-09 15:45:06 +00001804 writeOperand(BI.getSuccessor(0), true);
Dan Gohman8dae1382008-09-14 17:21:12 +00001805 Out << ", ";
Gabor Greifccd27fb2009-02-09 15:45:06 +00001806 writeOperand(BI.getSuccessor(1), true);
Chris Lattner00950542001-06-06 20:29:01 +00001807
Chris Lattner94dc1f22002-04-13 18:34:38 +00001808 } else if (isa<SwitchInst>(I)) {
David Blaikief12b3792013-02-11 01:16:51 +00001809 const SwitchInst& SI(cast<SwitchInst>(I));
Chris Lattnerf9be95f2009-10-27 19:13:16 +00001810 // Special case switch instruction to get formatting nice and correct.
Dan Gohman8dae1382008-09-14 17:21:12 +00001811 Out << ' ';
Eli Friedmanbb5a7442011-09-29 20:21:17 +00001812 writeOperand(SI.getCondition(), true);
Dan Gohman8dae1382008-09-14 17:21:12 +00001813 Out << ", ";
Eli Friedmanbb5a7442011-09-29 20:21:17 +00001814 writeOperand(SI.getDefaultDest(), true);
Chris Lattnerab49ee72008-08-23 22:52:27 +00001815 Out << " [";
David Blaikief12b3792013-02-11 01:16:51 +00001816 for (SwitchInst::ConstCaseIt i = SI.case_begin(), e = SI.case_end();
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00001817 i != e; ++i) {
Dan Gohman01889ca2009-08-13 01:41:52 +00001818 Out << "\n ";
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00001819 writeOperand(i.getCaseValue(), true);
Dan Gohman8dae1382008-09-14 17:21:12 +00001820 Out << ", ";
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00001821 writeOperand(i.getCaseSuccessor(), true);
Chris Lattner00950542001-06-06 20:29:01 +00001822 }
Dan Gohman01889ca2009-08-13 01:41:52 +00001823 Out << "\n ]";
Chris Lattnerab21db72009-10-28 00:19:10 +00001824 } else if (isa<IndirectBrInst>(I)) {
1825 // Special case indirectbr instruction to get formatting nice and correct.
Chris Lattnerf9be95f2009-10-27 19:13:16 +00001826 Out << ' ';
1827 writeOperand(Operand, true);
Dan Gohman0ed1f422009-10-30 02:01:10 +00001828 Out << ", [";
Andrew Trick18801ec2011-09-30 19:48:58 +00001829
Chris Lattnerf9be95f2009-10-27 19:13:16 +00001830 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) {
1831 if (i != 1)
1832 Out << ", ";
1833 writeOperand(I.getOperand(i), true);
1834 }
1835 Out << ']';
Jay Foadc1371202011-06-20 14:18:48 +00001836 } else if (const PHINode *PN = dyn_cast<PHINode>(&I)) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001837 Out << ' ';
Chris Lattner0f7364b2009-02-28 21:26:53 +00001838 TypePrinter.print(I.getType(), Out);
Misha Brukman0313e0b2004-06-21 21:53:56 +00001839 Out << ' ';
Chris Lattner00950542001-06-06 20:29:01 +00001840
Jay Foadc1371202011-06-20 14:18:48 +00001841 for (unsigned op = 0, Eop = PN->getNumIncomingValues(); op < Eop; ++op) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001842 if (op) Out << ", ";
Dan Gohman8dae1382008-09-14 17:21:12 +00001843 Out << "[ ";
Jay Foadc1371202011-06-20 14:18:48 +00001844 writeOperand(PN->getIncomingValue(op), false); Out << ", ";
1845 writeOperand(PN->getIncomingBlock(op), false); Out << " ]";
Chris Lattnerc24d2082001-06-11 15:04:20 +00001846 }
Dan Gohman995be7d2008-05-31 19:12:39 +00001847 } else if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(&I)) {
Dan Gohman8dae1382008-09-14 17:21:12 +00001848 Out << ' ';
Dan Gohman995be7d2008-05-31 19:12:39 +00001849 writeOperand(I.getOperand(0), true);
1850 for (const unsigned *i = EVI->idx_begin(), *e = EVI->idx_end(); i != e; ++i)
1851 Out << ", " << *i;
1852 } else if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(&I)) {
Dan Gohman8dae1382008-09-14 17:21:12 +00001853 Out << ' ';
1854 writeOperand(I.getOperand(0), true); Out << ", ";
Dan Gohman995be7d2008-05-31 19:12:39 +00001855 writeOperand(I.getOperand(1), true);
1856 for (const unsigned *i = IVI->idx_begin(), *e = IVI->idx_end(); i != e; ++i)
1857 Out << ", " << *i;
Bill Wendlinge6e88262011-08-12 20:24:12 +00001858 } else if (const LandingPadInst *LPI = dyn_cast<LandingPadInst>(&I)) {
1859 Out << ' ';
1860 TypePrinter.print(I.getType(), Out);
1861 Out << " personality ";
1862 writeOperand(I.getOperand(0), true); Out << '\n';
1863
1864 if (LPI->isCleanup())
1865 Out << " cleanup";
1866
1867 for (unsigned i = 0, e = LPI->getNumClauses(); i != e; ++i) {
1868 if (i != 0 || LPI->isCleanup()) Out << "\n";
1869 if (LPI->isCatch(i))
1870 Out << " catch ";
1871 else
1872 Out << " filter ";
1873
1874 writeOperand(LPI->getClause(i), true);
1875 }
Devang Patel57ef4f42008-02-23 00:35:18 +00001876 } else if (isa<ReturnInst>(I) && !Operand) {
1877 Out << " void";
Chris Lattnerd5118982005-05-06 20:26:43 +00001878 } else if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
1879 // Print the calling convention being used.
Micah Villmowd3766df2012-09-13 15:11:12 +00001880 if (CI->getCallingConv() != CallingConv::C) {
1881 Out << " ";
1882 PrintCallingConv(CI->getCallingConv(), Out);
Chris Lattnerd5118982005-05-06 20:26:43 +00001883 }
1884
Gabor Greif7bbdf0c2010-06-23 13:09:06 +00001885 Operand = CI->getCalledValue();
Chris Lattner1afcace2011-07-09 17:41:24 +00001886 PointerType *PTy = cast<PointerType>(Operand->getType());
1887 FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1888 Type *RetTy = FTy->getReturnType();
Bill Wendling99faa3b2012-12-07 23:16:57 +00001889 const AttributeSet &PAL = CI->getAttributes();
Chris Lattner268de042001-11-06 21:28:12 +00001890
Bill Wendling1b0c54f2013-01-18 21:53:16 +00001891 if (PAL.hasAttributes(AttributeSet::ReturnIndex))
1892 Out << ' ' << PAL.getAsString(AttributeSet::ReturnIndex);
Devang Patel652203f2008-09-29 20:49:50 +00001893
Chris Lattner7a012292003-08-05 15:34:45 +00001894 // If possible, print out the short form of the call instruction. We can
Chris Lattnerb5794002002-04-07 22:49:37 +00001895 // only do this if the first argument is a pointer to a nonvararg function,
Chris Lattner7a012292003-08-05 15:34:45 +00001896 // and if the return type is not a pointer to a function.
Chris Lattner268de042001-11-06 21:28:12 +00001897 //
Dan Gohman8dae1382008-09-14 17:21:12 +00001898 Out << ' ';
Chris Lattner7a012292003-08-05 15:34:45 +00001899 if (!FTy->isVarArg() &&
Duncan Sands1df98592010-02-16 11:11:14 +00001900 (!RetTy->isPointerTy() ||
1901 !cast<PointerType>(RetTy)->getElementType()->isFunctionTy())) {
Chris Lattner0f7364b2009-02-28 21:26:53 +00001902 TypePrinter.print(RetTy, Out);
Dan Gohman8dae1382008-09-14 17:21:12 +00001903 Out << ' ';
Chris Lattner268de042001-11-06 21:28:12 +00001904 writeOperand(Operand, false);
1905 } else {
1906 writeOperand(Operand, true);
1907 }
Misha Brukman0313e0b2004-06-21 21:53:56 +00001908 Out << '(';
Gabor Greif7bbdf0c2010-06-23 13:09:06 +00001909 for (unsigned op = 0, Eop = CI->getNumArgOperands(); op < Eop; ++op) {
1910 if (op > 0)
Dan Gohman8dae1382008-09-14 17:21:12 +00001911 Out << ", ";
Bill Wendling94e94b32012-12-30 13:50:49 +00001912 writeParamOperand(CI->getArgOperand(op), PAL, op + 1);
Chris Lattner00950542001-06-06 20:29:01 +00001913 }
Dan Gohman8dae1382008-09-14 17:21:12 +00001914 Out << ')';
Bill Wendling831737d2012-12-30 10:32:01 +00001915 if (PAL.hasAttributes(AttributeSet::FunctionIndex))
Bill Wendling351b7a12013-02-22 09:09:42 +00001916 Out << " #" << Machine.getAttributeGroupSlot(PAL.getFnAttributes());
Chris Lattner7e708292002-06-25 16:13:24 +00001917 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
Gabor Greifc9f75002010-03-24 13:21:49 +00001918 Operand = II->getCalledValue();
Chris Lattner1afcace2011-07-09 17:41:24 +00001919 PointerType *PTy = cast<PointerType>(Operand->getType());
1920 FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1921 Type *RetTy = FTy->getReturnType();
Bill Wendling99faa3b2012-12-07 23:16:57 +00001922 const AttributeSet &PAL = II->getAttributes();
Chris Lattner7a012292003-08-05 15:34:45 +00001923
Chris Lattnerd5118982005-05-06 20:26:43 +00001924 // Print the calling convention being used.
Micah Villmowd3766df2012-09-13 15:11:12 +00001925 if (II->getCallingConv() != CallingConv::C) {
1926 Out << " ";
1927 PrintCallingConv(II->getCallingConv(), Out);
Chris Lattnerd5118982005-05-06 20:26:43 +00001928 }
1929
Bill Wendling1b0c54f2013-01-18 21:53:16 +00001930 if (PAL.hasAttributes(AttributeSet::ReturnIndex))
1931 Out << ' ' << PAL.getAsString(AttributeSet::ReturnIndex);
Devang Patel652203f2008-09-29 20:49:50 +00001932
Chris Lattner7a012292003-08-05 15:34:45 +00001933 // If possible, print out the short form of the invoke instruction. We can
1934 // only do this if the first argument is a pointer to a nonvararg function,
1935 // and if the return type is not a pointer to a function.
1936 //
Dan Gohman2b6c3d92008-10-15 18:02:08 +00001937 Out << ' ';
Chris Lattner7a012292003-08-05 15:34:45 +00001938 if (!FTy->isVarArg() &&
Duncan Sands1df98592010-02-16 11:11:14 +00001939 (!RetTy->isPointerTy() ||
1940 !cast<PointerType>(RetTy)->getElementType()->isFunctionTy())) {
Chris Lattner0f7364b2009-02-28 21:26:53 +00001941 TypePrinter.print(RetTy, Out);
Dan Gohman2b6c3d92008-10-15 18:02:08 +00001942 Out << ' ';
Chris Lattner7a012292003-08-05 15:34:45 +00001943 writeOperand(Operand, false);
1944 } else {
1945 writeOperand(Operand, true);
1946 }
Misha Brukman0313e0b2004-06-21 21:53:56 +00001947 Out << '(';
Gabor Greif7bbdf0c2010-06-23 13:09:06 +00001948 for (unsigned op = 0, Eop = II->getNumArgOperands(); op < Eop; ++op) {
Gabor Greifc9f75002010-03-24 13:21:49 +00001949 if (op)
Dan Gohman8dae1382008-09-14 17:21:12 +00001950 Out << ", ";
Bill Wendling94e94b32012-12-30 13:50:49 +00001951 writeParamOperand(II->getArgOperand(op), PAL, op + 1);
Chris Lattnere02fa852001-10-13 06:42:36 +00001952 }
1953
Dan Gohman8dae1382008-09-14 17:21:12 +00001954 Out << ')';
Bill Wendling831737d2012-12-30 10:32:01 +00001955 if (PAL.hasAttributes(AttributeSet::FunctionIndex))
Bill Wendling351b7a12013-02-22 09:09:42 +00001956 Out << " #" << Machine.getAttributeGroupSlot(PAL.getFnAttributes());
Devang Patel19c87462008-09-26 22:53:05 +00001957
Dan Gohman01889ca2009-08-13 01:41:52 +00001958 Out << "\n to ";
Chris Lattnere02fa852001-10-13 06:42:36 +00001959 writeOperand(II->getNormalDest(), true);
Dan Gohman8dae1382008-09-14 17:21:12 +00001960 Out << " unwind ";
Chris Lattneraeb2a1d2004-02-08 21:44:31 +00001961 writeOperand(II->getUnwindDest(), true);
Chris Lattnere02fa852001-10-13 06:42:36 +00001962
Victor Hernandez7b929da2009-10-23 21:09:37 +00001963 } else if (const AllocaInst *AI = dyn_cast<AllocaInst>(&I)) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001964 Out << ' ';
Dan Gohmand3da6d52013-02-08 22:01:47 +00001965 TypePrinter.print(AI->getAllocatedType(), Out);
Dan Gohman69bff072009-07-31 18:23:24 +00001966 if (!AI->getArraySize() || AI->isArrayAllocation()) {
Dan Gohman8dae1382008-09-14 17:21:12 +00001967 Out << ", ";
Chris Lattner94dc1f22002-04-13 18:34:38 +00001968 writeOperand(AI->getArraySize(), true);
Chris Lattner00950542001-06-06 20:29:01 +00001969 }
Nate Begeman14b05292005-11-05 09:21:28 +00001970 if (AI->getAlignment()) {
Chris Lattner9fad0b92005-11-05 21:20:34 +00001971 Out << ", align " << AI->getAlignment();
Nate Begeman14b05292005-11-05 09:21:28 +00001972 }
Chris Lattnere02fa852001-10-13 06:42:36 +00001973 } else if (isa<CastInst>(I)) {
Dan Gohman8dae1382008-09-14 17:21:12 +00001974 if (Operand) {
1975 Out << ' ';
1976 writeOperand(Operand, true); // Work with broken code
1977 }
Misha Brukman0313e0b2004-06-21 21:53:56 +00001978 Out << " to ";
Chris Lattner0f7364b2009-02-28 21:26:53 +00001979 TypePrinter.print(I.getType(), Out);
Chris Lattner4d45bd02003-10-18 05:57:43 +00001980 } else if (isa<VAArgInst>(I)) {
Dan Gohman8dae1382008-09-14 17:21:12 +00001981 if (Operand) {
1982 Out << ' ';
1983 writeOperand(Operand, true); // Work with broken code
1984 }
Misha Brukman0313e0b2004-06-21 21:53:56 +00001985 Out << ", ";
Chris Lattner0f7364b2009-02-28 21:26:53 +00001986 TypePrinter.print(I.getType(), Out);
1987 } else if (Operand) { // Print the normal way.
Chris Lattner00950542001-06-06 20:29:01 +00001988
Misha Brukmanfd939082005-04-21 23:48:37 +00001989 // PrintAllTypes - Instructions who have operands of all the same type
Chris Lattner00950542001-06-06 20:29:01 +00001990 // omit the type from all but the first operand. If the instruction has
1991 // different type operands (for example br), then they are all printed.
1992 bool PrintAllTypes = false;
Chris Lattner1afcace2011-07-09 17:41:24 +00001993 Type *TheType = Operand->getType();
Chris Lattner00950542001-06-06 20:29:01 +00001994
Reid Spencerebe57e32007-02-02 13:54:55 +00001995 // Select, Store and ShuffleVector always print all types.
Devang Patel64947682008-03-04 22:05:14 +00001996 if (isa<SelectInst>(I) || isa<StoreInst>(I) || isa<ShuffleVectorInst>(I)
1997 || isa<ReturnInst>(I)) {
Chris Lattnerffd9bf42003-04-16 20:20:02 +00001998 PrintAllTypes = true;
1999 } else {
2000 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
2001 Operand = I.getOperand(i);
Nuno Lopes6ad2b2a2009-01-15 18:40:57 +00002002 // note that Operand shouldn't be null, but the test helps make dump()
2003 // more tolerant of malformed IR
Nuno Lopesa8c78a92009-01-14 17:51:41 +00002004 if (Operand && Operand->getType() != TheType) {
Chris Lattnerffd9bf42003-04-16 20:20:02 +00002005 PrintAllTypes = true; // We have differing types! Print them all!
2006 break;
2007 }
Chris Lattner00950542001-06-06 20:29:01 +00002008 }
2009 }
Misha Brukmanfd939082005-04-21 23:48:37 +00002010
Chris Lattnerc1824992001-10-29 16:05:51 +00002011 if (!PrintAllTypes) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00002012 Out << ' ';
Chris Lattner0f7364b2009-02-28 21:26:53 +00002013 TypePrinter.print(TheType, Out);
Chris Lattnerc1824992001-10-29 16:05:51 +00002014 }
Chris Lattner00950542001-06-06 20:29:01 +00002015
Dan Gohman8dae1382008-09-14 17:21:12 +00002016 Out << ' ';
Chris Lattner7e708292002-06-25 16:13:24 +00002017 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
Dan Gohman8dae1382008-09-14 17:21:12 +00002018 if (i) Out << ", ";
Chris Lattner7e708292002-06-25 16:13:24 +00002019 writeOperand(I.getOperand(i), PrintAllTypes);
Chris Lattner00950542001-06-06 20:29:01 +00002020 }
2021 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002022
Eli Friedman21006d42011-08-09 23:02:53 +00002023 // Print atomic ordering/alignment for memory operations
2024 if (const LoadInst *LI = dyn_cast<LoadInst>(&I)) {
2025 if (LI->isAtomic())
2026 writeAtomic(LI->getOrdering(), LI->getSynchScope());
2027 if (LI->getAlignment())
2028 Out << ", align " << LI->getAlignment();
2029 } else if (const StoreInst *SI = dyn_cast<StoreInst>(&I)) {
2030 if (SI->isAtomic())
2031 writeAtomic(SI->getOrdering(), SI->getSynchScope());
2032 if (SI->getAlignment())
2033 Out << ", align " << SI->getAlignment();
Eli Friedmanff030482011-07-28 21:48:00 +00002034 } else if (const AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(&I)) {
2035 writeAtomic(CXI->getOrdering(), CXI->getSynchScope());
2036 } else if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(&I)) {
2037 writeAtomic(RMWI->getOrdering(), RMWI->getSynchScope());
Eli Friedman47f35132011-07-25 23:16:38 +00002038 } else if (const FenceInst *FI = dyn_cast<FenceInst>(&I)) {
2039 writeAtomic(FI->getOrdering(), FI->getSynchScope());
Christopher Lamb43c7f372007-04-22 19:24:39 +00002040 }
Chris Lattner00950542001-06-06 20:29:01 +00002041
Chris Lattner7d05c462009-12-28 20:10:43 +00002042 // Print Metadata info.
Nick Lewyckyfa0c54e2010-02-25 06:53:04 +00002043 SmallVector<std::pair<unsigned, MDNode*>, 4> InstMD;
2044 I.getAllMetadata(InstMD);
Erick Tryzelaarf3d5c912010-03-02 05:32:52 +00002045 if (!InstMD.empty()) {
2046 SmallVector<StringRef, 8> MDNames;
2047 I.getType()->getContext().getMDKindNames(MDNames);
2048 for (unsigned i = 0, e = InstMD.size(); i != e; ++i) {
2049 unsigned Kind = InstMD[i].first;
2050 if (Kind < MDNames.size()) {
2051 Out << ", !" << MDNames[Kind];
Daniel Malead0fef322013-05-08 20:38:31 +00002052 } else {
2053 Out << ", !<unknown kind #" << Kind << ">";
2054 }
Dan Gohman3bdfbf52010-07-20 23:55:01 +00002055 Out << ' ';
2056 WriteAsOperandInternal(Out, InstMD[i].second, &TypePrinter, &Machine,
2057 TheModule);
Nick Lewyckyfa0c54e2010-02-25 06:53:04 +00002058 }
Devang Patel7f93f4d2009-10-07 16:37:55 +00002059 }
Chris Lattnere02fa852001-10-13 06:42:36 +00002060 printInfoComment(I);
Chris Lattner00950542001-06-06 20:29:01 +00002061}
2062
Chris Lattner6e6b1802009-12-31 02:13:35 +00002063static void WriteMDNodeComment(const MDNode *Node,
Duncan Sands34727662010-07-12 08:16:59 +00002064 formatted_raw_ostream &Out) {
Chris Lattner6e6b1802009-12-31 02:13:35 +00002065 if (Node->getNumOperands() < 1)
2066 return;
Bill Wendling58a6cf22012-06-28 00:41:44 +00002067
2068 Value *Op = Node->getOperand(0);
2069 if (!Op || !isa<ConstantInt>(Op) || cast<ConstantInt>(Op)->getBitWidth() < 32)
Chris Lattner6e6b1802009-12-31 02:13:35 +00002070 return;
Andrew Trick18801ec2011-09-30 19:48:58 +00002071
Bill Wendling58a6cf22012-06-28 00:41:44 +00002072 DIDescriptor Desc(Node);
David Blaikiec0ec8a42013-03-11 23:39:23 +00002073 if (!Desc.Verify())
Bill Wendling58a6cf22012-06-28 00:41:44 +00002074 return;
2075
2076 unsigned Tag = Desc.getTag();
Chris Lattner6e6b1802009-12-31 02:13:35 +00002077 Out.PadToColumn(50);
Bill Wendling86b032b2012-07-03 20:01:02 +00002078 if (dwarf::TagString(Tag)) {
2079 Out << "; ";
2080 Desc.print(Out);
2081 } else if (Tag == dwarf::DW_TAG_user_base) {
Chris Lattner6e6b1802009-12-31 02:13:35 +00002082 Out << "; [ DW_TAG_user_base ]";
Bill Wendling86b032b2012-07-03 20:01:02 +00002083 }
Chris Lattner6e6b1802009-12-31 02:13:35 +00002084}
2085
Daniel Malead0fef322013-05-08 20:38:31 +00002086void AssemblyWriter::writeMDNode(unsigned Slot, const MDNode *Node) {
2087 Out << '!' << Slot << " = metadata ";
2088 printMDNodeBody(Node);
2089}
2090
Chris Lattner6e6b1802009-12-31 02:13:35 +00002091void AssemblyWriter::writeAllMDNodes() {
2092 SmallVector<const MDNode *, 16> Nodes;
Chris Lattner307c9892009-12-31 02:20:11 +00002093 Nodes.resize(Machine.mdn_size());
2094 for (SlotTracker::mdn_iterator I = Machine.mdn_begin(), E = Machine.mdn_end();
2095 I != E; ++I)
Chris Lattner6e6b1802009-12-31 02:13:35 +00002096 Nodes[I->second] = cast<MDNode>(I->first);
Andrew Trick18801ec2011-09-30 19:48:58 +00002097
Chris Lattner6e6b1802009-12-31 02:13:35 +00002098 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Daniel Malead0fef322013-05-08 20:38:31 +00002099 writeMDNode(i, Nodes[i]);
Chris Lattner6e6b1802009-12-31 02:13:35 +00002100 }
2101}
2102
2103void AssemblyWriter::printMDNodeBody(const MDNode *Node) {
Dan Gohman3bdfbf52010-07-20 23:55:01 +00002104 WriteMDNodeBodyInternal(Out, Node, &TypePrinter, &Machine, TheModule);
Chris Lattner6e6b1802009-12-31 02:13:35 +00002105 WriteMDNodeComment(Node, Out);
2106 Out << "\n";
2107}
Chris Lattner00950542001-06-06 20:29:01 +00002108
Bill Wendlingb29ce262013-02-11 08:43:33 +00002109void AssemblyWriter::writeAllAttributeGroups() {
2110 std::vector<std::pair<AttributeSet, unsigned> > asVec;
2111 asVec.resize(Machine.as_size());
2112
2113 for (SlotTracker::as_iterator I = Machine.as_begin(), E = Machine.as_end();
2114 I != E; ++I)
2115 asVec[I->second] = *I;
2116
2117 for (std::vector<std::pair<AttributeSet, unsigned> >::iterator
2118 I = asVec.begin(), E = asVec.end(); I != E; ++I)
2119 Out << "attributes #" << I->second << " = { "
Rafael Espindolaaae02982013-05-01 13:07:03 +00002120 << I->first.getAsString(AttributeSet::FunctionIndex, true) << " }\n";
Bill Wendlingb29ce262013-02-11 08:43:33 +00002121}
2122
Daniel Malead0fef322013-05-08 20:38:31 +00002123} // namespace llvm
2124
Chris Lattner00950542001-06-06 20:29:01 +00002125//===----------------------------------------------------------------------===//
2126// External Interface declarations
2127//===----------------------------------------------------------------------===//
2128
Dan Gohman683e9222009-08-12 17:23:50 +00002129void Module::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW) const {
Chris Lattner0d9574a2008-08-19 04:26:57 +00002130 SlotTracker SlotTable(this);
Dan Gohman683e9222009-08-12 17:23:50 +00002131 formatted_raw_ostream OS(ROS);
Chris Lattner944fac72008-08-23 22:23:09 +00002132 AssemblyWriter W(OS, SlotTable, this, AAW);
Chris Lattnerbd72b322009-12-31 02:23:35 +00002133 W.printModule(this);
Chris Lattner00950542001-06-06 20:29:01 +00002134}
2135
Dan Gohman17aa92c2010-07-21 23:38:33 +00002136void NamedMDNode::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW) const {
2137 SlotTracker SlotTable(getParent());
2138 formatted_raw_ostream OS(ROS);
2139 AssemblyWriter W(OS, SlotTable, getParent(), AAW);
2140 W.printNamedMDNode(this);
2141}
2142
Chris Lattner6d4306e2009-02-28 21:11:05 +00002143void Type::print(raw_ostream &OS) const {
2144 if (this == 0) {
2145 OS << "<null Type>";
2146 return;
2147 }
Chris Lattner1afcace2011-07-09 17:41:24 +00002148 TypePrinting TP;
2149 TP.print(const_cast<Type*>(this), OS);
Andrew Trick18801ec2011-09-30 19:48:58 +00002150
Chris Lattner1afcace2011-07-09 17:41:24 +00002151 // If the type is a named struct type, print the body as well.
2152 if (StructType *STy = dyn_cast<StructType>(const_cast<Type*>(this)))
Chris Lattnerc4d0e9f2011-08-12 18:07:07 +00002153 if (!STy->isLiteral()) {
Chris Lattner1afcace2011-07-09 17:41:24 +00002154 OS << " = type ";
2155 TP.printStructBody(STy, OS);
2156 }
Chris Lattner75cf7cf2002-04-08 22:03:40 +00002157}
2158
Dan Gohman683e9222009-08-12 17:23:50 +00002159void Value::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW) const {
Chris Lattner944fac72008-08-23 22:23:09 +00002160 if (this == 0) {
Dan Gohman1220e102009-08-12 20:56:03 +00002161 ROS << "printing a <null> value\n";
Chris Lattner944fac72008-08-23 22:23:09 +00002162 return;
2163 }
Dan Gohman1220e102009-08-12 20:56:03 +00002164 formatted_raw_ostream OS(ROS);
Chris Lattner944fac72008-08-23 22:23:09 +00002165 if (const Instruction *I = dyn_cast<Instruction>(this)) {
2166 const Function *F = I->getParent() ? I->getParent()->getParent() : 0;
2167 SlotTracker SlotTable(F);
Chris Lattnerdbe85bf2009-12-31 08:23:09 +00002168 AssemblyWriter W(OS, SlotTable, getModuleFromVal(I), AAW);
Chris Lattnerbd72b322009-12-31 02:23:35 +00002169 W.printInstruction(*I);
Chris Lattner944fac72008-08-23 22:23:09 +00002170 } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(this)) {
2171 SlotTracker SlotTable(BB->getParent());
Chris Lattner6e6b1802009-12-31 02:13:35 +00002172 AssemblyWriter W(OS, SlotTable, getModuleFromVal(BB), AAW);
Chris Lattnerbd72b322009-12-31 02:23:35 +00002173 W.printBasicBlock(BB);
Chris Lattner944fac72008-08-23 22:23:09 +00002174 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(this)) {
2175 SlotTracker SlotTable(GV->getParent());
Dan Gohmanba0941f2009-04-20 16:10:33 +00002176 AssemblyWriter W(OS, SlotTable, GV->getParent(), AAW);
Chris Lattnerbd72b322009-12-31 02:23:35 +00002177 if (const GlobalVariable *V = dyn_cast<GlobalVariable>(GV))
2178 W.printGlobal(V);
2179 else if (const Function *F = dyn_cast<Function>(GV))
2180 W.printFunction(F);
2181 else
2182 W.printAlias(cast<GlobalAlias>(GV));
Devang Patelfcd65ae2009-07-01 20:59:15 +00002183 } else if (const MDNode *N = dyn_cast<MDNode>(this)) {
Victor Hernandez8fffff52010-01-20 04:45:57 +00002184 const Function *F = N->getFunction();
Victor Hernandez559588b2010-01-14 01:47:37 +00002185 SlotTracker SlotTable(F);
Dan Gohman79b78a42010-07-14 21:12:44 +00002186 AssemblyWriter W(OS, SlotTable, F ? F->getParent() : 0, AAW);
Chris Lattner6e6b1802009-12-31 02:13:35 +00002187 W.printMDNodeBody(N);
Chris Lattner944fac72008-08-23 22:23:09 +00002188 } else if (const Constant *C = dyn_cast<Constant>(this)) {
Chris Lattnere9fa33e2009-02-28 23:20:19 +00002189 TypePrinting TypePrinter;
Chris Lattner0f7364b2009-02-28 21:26:53 +00002190 TypePrinter.print(C->getType(), OS);
Chris Lattner6d4306e2009-02-28 21:11:05 +00002191 OS << ' ';
Dan Gohman3bdfbf52010-07-20 23:55:01 +00002192 WriteConstantInternal(OS, C, TypePrinter, 0, 0);
Chris Lattner4a3d3a52009-12-31 01:41:14 +00002193 } else if (isa<InlineAsm>(this) || isa<MDString>(this) ||
2194 isa<Argument>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00002195 WriteAsOperand(OS, this, true, 0);
2196 } else {
Dan Gohmancd26ec52009-09-23 01:33:16 +00002197 // Otherwise we don't know what it is. Call the virtual function to
2198 // allow a subclass to print itself.
2199 printCustom(OS);
Chris Lattner944fac72008-08-23 22:23:09 +00002200 }
2201}
2202
Dan Gohmancd26ec52009-09-23 01:33:16 +00002203// Value::printCustom - subclasses should override this to implement printing.
2204void Value::printCustom(raw_ostream &OS) const {
2205 llvm_unreachable("Unknown value to print out!");
2206}
2207
Chris Lattner7059e532008-08-25 17:03:15 +00002208// Value::dump - allow easy printing of Values from the debugger.
David Greened865e022010-01-05 01:29:26 +00002209void Value::dump() const { print(dbgs()); dbgs() << '\n'; }
Reid Spencerfa452c02004-05-25 18:14:38 +00002210
Chris Lattner7059e532008-08-25 17:03:15 +00002211// Type::dump - allow easy printing of Types from the debugger.
Chris Lattner1afcace2011-07-09 17:41:24 +00002212void Type::dump() const { print(dbgs()); }
Chris Lattnerc2871372009-02-28 21:05:51 +00002213
Chris Lattner7059e532008-08-25 17:03:15 +00002214// Module::dump() - Allow printing of Modules from the debugger.
David Greened865e022010-01-05 01:29:26 +00002215void Module::dump() const { print(dbgs(), 0); }
Bill Wendlingf4374e42011-12-09 23:18:34 +00002216
2217// NamedMDNode::dump() - Allow printing of NamedMDNodes from the debugger.
2218void NamedMDNode::dump() const { print(dbgs(), 0); }