blob: 4fe1eeeb4abc5b0515168aafe8a71dc7a6815ebc [file] [log] [blame]
Chris Lattner8da78af2002-04-07 22:31:46 +00001//===-- AsmWriter.cpp - Printing LLVM as an assembly file -----------------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
10// This library implements the functionality defined in llvm/Assembly/Writer.h
11//
Chris Lattner02b93992002-04-12 18:21:53 +000012// Note that these routines must be extremely tolerant of various errors in the
Chris Lattner8f77dae2003-05-08 02:44:12 +000013// LLVM code, because it can be used for debugging transformations.
Chris Lattner02b93992002-04-12 18:21:53 +000014//
Chris Lattner00950542001-06-06 20:29:01 +000015//===----------------------------------------------------------------------===//
16
Chris Lattner75cf7cf2002-04-08 22:03:40 +000017#include "llvm/Assembly/Writer.h"
Chris Lattnerf082b802002-07-23 18:07:49 +000018#include "llvm/Assembly/PrintModulePass.h"
Chris Lattner95e5a2c2003-10-30 23:41:03 +000019#include "llvm/Assembly/AsmAnnotationWriter.h"
Chris Lattnerd5118982005-05-06 20:26:43 +000020#include "llvm/CallingConv.h"
Chris Lattnerf2d577b2004-01-20 19:50:34 +000021#include "llvm/Constants.h"
Chris Lattner3eb59c02002-04-29 18:46:50 +000022#include "llvm/DerivedTypes.h"
Chris Lattner863517a2006-01-25 18:57:27 +000023#include "llvm/InlineAsm.h"
Chris Lattner3990b122009-12-28 23:41:32 +000024#include "llvm/IntrinsicInst.h"
Dan Gohman1224c382009-07-20 21:19:07 +000025#include "llvm/Operator.h"
Chris Lattnerf2d577b2004-01-20 19:50:34 +000026#include "llvm/Module.h"
Reid Spenceref9b9a72007-02-05 20:47:22 +000027#include "llvm/ValueSymbolTable.h"
Reid Spencer78d033e2007-01-06 07:24:44 +000028#include "llvm/TypeSymbolTable.h"
Chris Lattner413fd232009-03-01 00:03:38 +000029#include "llvm/ADT/DenseSet.h"
Benjamin Kramer59088392010-01-29 14:42:22 +000030#include "llvm/ADT/SmallString.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000031#include "llvm/ADT/StringExtras.h"
32#include "llvm/ADT/STLExtras.h"
Bill Wendling8f487662006-11-28 02:09:03 +000033#include "llvm/Support/CFG.h"
David Greened865e022010-01-05 01:29:26 +000034#include "llvm/Support/Debug.h"
Devang Patel2d5988d2009-09-30 20:16:54 +000035#include "llvm/Support/Dwarf.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000036#include "llvm/Support/ErrorHandling.h"
Jim Laskeycb6682f2005-08-17 19:34:49 +000037#include "llvm/Support/MathExtras.h"
Dan Gohman683e9222009-08-12 17:23:50 +000038#include "llvm/Support/FormattedStream.h"
Chris Lattner007377f2001-09-07 16:36:04 +000039#include <algorithm>
Reid Spencer4ad513c2007-05-22 19:27:35 +000040#include <cctype>
Devang Patel923078c2009-07-01 19:21:12 +000041#include <map>
Chris Lattner31f84992003-11-21 20:23:48 +000042using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000043
Reid Spenceredd5d9e2005-05-15 16:13:11 +000044// Make virtual table appear in this compilation unit.
45AssemblyAnnotationWriter::~AssemblyAnnotationWriter() {}
46
Chris Lattner6ab910b2008-08-19 04:36:02 +000047//===----------------------------------------------------------------------===//
48// Helper Functions
49//===----------------------------------------------------------------------===//
50
51static const Module *getModuleFromVal(const Value *V) {
52 if (const Argument *MA = dyn_cast<Argument>(V))
53 return MA->getParent() ? MA->getParent()->getParent() : 0;
Daniel Dunbara279bc32009-09-20 02:20:51 +000054
Chris Lattner6ab910b2008-08-19 04:36:02 +000055 if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
56 return BB->getParent() ? BB->getParent()->getParent() : 0;
Daniel Dunbara279bc32009-09-20 02:20:51 +000057
Chris Lattner6ab910b2008-08-19 04:36:02 +000058 if (const Instruction *I = dyn_cast<Instruction>(V)) {
59 const Function *M = I->getParent() ? I->getParent()->getParent() : 0;
60 return M ? M->getParent() : 0;
61 }
Chris Lattner4a3d3a52009-12-31 01:41:14 +000062
Chris Lattner6ab910b2008-08-19 04:36:02 +000063 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
64 return GV->getParent();
Chris Lattner4a3d3a52009-12-31 01:41:14 +000065 if (const NamedMDNode *NMD = dyn_cast<NamedMDNode>(V))
66 return NMD->getParent();
Chris Lattner6ab910b2008-08-19 04:36:02 +000067 return 0;
68}
69
Daniel Dunbare9da1332008-10-28 19:33:02 +000070// PrintEscapedString - Print each character of the specified string, escaping
71// it if it is not printable or if it is an escape char.
Dan Gohman683e9222009-08-12 17:23:50 +000072static void PrintEscapedString(const StringRef &Name,
Dan Gohman1220e102009-08-12 20:56:03 +000073 raw_ostream &Out) {
Daniel Dunbar03d76512009-07-25 23:55:21 +000074 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
75 unsigned char C = Name[i];
Nick Lewycky34a40862009-03-15 06:39:52 +000076 if (isprint(C) && C != '\\' && C != '"')
Daniel Dunbare9da1332008-10-28 19:33:02 +000077 Out << C;
78 else
79 Out << '\\' << hexdigit(C >> 4) << hexdigit(C & 0x0F);
80 }
81}
82
Chris Lattner6ab910b2008-08-19 04:36:02 +000083enum PrefixType {
84 GlobalPrefix,
85 LabelPrefix,
Daniel Dunbarcad35802008-10-14 23:28:09 +000086 LocalPrefix,
87 NoPrefix
Chris Lattner6ab910b2008-08-19 04:36:02 +000088};
89
90/// PrintLLVMName - Turn the specified name into an 'LLVM name', which is either
91/// prefixed with % (if the string only contains simple characters) or is
92/// surrounded with ""'s (if it has special chars in it). Print it out.
Dan Gohman1220e102009-08-12 20:56:03 +000093static void PrintLLVMName(raw_ostream &OS, const StringRef &Name,
Daniel Dunbar03d76512009-07-25 23:55:21 +000094 PrefixType Prefix) {
95 assert(Name.data() && "Cannot get empty name!");
Chris Lattner6ab910b2008-08-19 04:36:02 +000096 switch (Prefix) {
Torok Edwinc23197a2009-07-14 16:55:14 +000097 default: llvm_unreachable("Bad prefix!");
Daniel Dunbarcad35802008-10-14 23:28:09 +000098 case NoPrefix: break;
Chris Lattner52b26de2008-08-19 05:16:28 +000099 case GlobalPrefix: OS << '@'; break;
100 case LabelPrefix: break;
101 case LocalPrefix: OS << '%'; break;
Nick Lewycky04234832009-03-19 06:31:22 +0000102 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000103
Chris Lattner6ab910b2008-08-19 04:36:02 +0000104 // Scan the name to see if it needs quotes first.
Daniel Dunbar03d76512009-07-25 23:55:21 +0000105 bool NeedsQuotes = isdigit(Name[0]);
Chris Lattner6ab910b2008-08-19 04:36:02 +0000106 if (!NeedsQuotes) {
Daniel Dunbar03d76512009-07-25 23:55:21 +0000107 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
108 char C = Name[i];
Chris Lattner6ab910b2008-08-19 04:36:02 +0000109 if (!isalnum(C) && C != '-' && C != '.' && C != '_') {
110 NeedsQuotes = true;
111 break;
112 }
113 }
114 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000115
Chris Lattner6ab910b2008-08-19 04:36:02 +0000116 // If we didn't need any quotes, just write out the name in one blast.
117 if (!NeedsQuotes) {
Daniel Dunbar03d76512009-07-25 23:55:21 +0000118 OS << Name;
Chris Lattner6ab910b2008-08-19 04:36:02 +0000119 return;
120 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000121
Chris Lattner6ab910b2008-08-19 04:36:02 +0000122 // Okay, we need quotes. Output the quotes and escape any scary characters as
123 // needed.
124 OS << '"';
Daniel Dunbar03d76512009-07-25 23:55:21 +0000125 PrintEscapedString(Name, OS);
Chris Lattner6ab910b2008-08-19 04:36:02 +0000126 OS << '"';
127}
128
129/// PrintLLVMName - Turn the specified name into an 'LLVM name', which is either
130/// prefixed with % (if the string only contains simple characters) or is
131/// surrounded with ""'s (if it has special chars in it). Print it out.
Dan Gohman1220e102009-08-12 20:56:03 +0000132static void PrintLLVMName(raw_ostream &OS, const Value *V) {
Daniel Dunbara279bc32009-09-20 02:20:51 +0000133 PrintLLVMName(OS, V->getName(),
Chris Lattner6ab910b2008-08-19 04:36:02 +0000134 isa<GlobalValue>(V) ? GlobalPrefix : LocalPrefix);
135}
136
Chris Lattner9cc34462009-02-28 20:25:14 +0000137//===----------------------------------------------------------------------===//
138// TypePrinting Class: Type printing machinery
139//===----------------------------------------------------------------------===//
140
Chris Lattner87185e82009-02-28 23:03:55 +0000141static DenseMap<const Type *, std::string> &getTypeNamesMap(void *M) {
142 return *static_cast<DenseMap<const Type *, std::string>*>(M);
Chris Lattnerd8030a72009-02-28 22:34:45 +0000143}
144
145void TypePrinting::clear() {
146 getTypeNamesMap(TypeNames).clear();
147}
Chris Lattner9cc34462009-02-28 20:25:14 +0000148
Chris Lattnere9fa33e2009-02-28 23:20:19 +0000149bool TypePrinting::hasTypeName(const Type *Ty) const {
150 return getTypeNamesMap(TypeNames).count(Ty);
151}
152
153void TypePrinting::addTypeName(const Type *Ty, const std::string &N) {
154 getTypeNamesMap(TypeNames).insert(std::make_pair(Ty, N));
155}
156
157
158TypePrinting::TypePrinting() {
Chris Lattner87185e82009-02-28 23:03:55 +0000159 TypeNames = new DenseMap<const Type *, std::string>();
Chris Lattner9cc34462009-02-28 20:25:14 +0000160}
161
Chris Lattnerd8030a72009-02-28 22:34:45 +0000162TypePrinting::~TypePrinting() {
163 delete &getTypeNamesMap(TypeNames);
164}
165
Chris Lattner534361e2009-02-28 20:49:40 +0000166/// CalcTypeName - Write the specified type to the specified raw_ostream, making
167/// use of type names or up references to shorten the type name where possible.
168void TypePrinting::CalcTypeName(const Type *Ty,
Chris Lattnerb840cac2009-02-28 20:34:19 +0000169 SmallVectorImpl<const Type *> &TypeStack,
Chris Lattnerd2b6cb02009-03-01 01:16:21 +0000170 raw_ostream &OS, bool IgnoreTopLevelName) {
Chris Lattner9cc34462009-02-28 20:25:14 +0000171 // Check to see if the type is named.
Chris Lattnerd2b6cb02009-03-01 01:16:21 +0000172 if (!IgnoreTopLevelName) {
Nick Lewycky04234832009-03-19 06:31:22 +0000173 DenseMap<const Type *, std::string> &TM = getTypeNamesMap(TypeNames);
Chris Lattnerd2b6cb02009-03-01 01:16:21 +0000174 DenseMap<const Type *, std::string>::iterator I = TM.find(Ty);
175 if (I != TM.end()) {
176 OS << I->second;
177 return;
178 }
Chris Lattner9cc34462009-02-28 20:25:14 +0000179 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000180
Chris Lattner9cc34462009-02-28 20:25:14 +0000181 // Check to see if the Type is already on the stack...
182 unsigned Slot = 0, CurSize = TypeStack.size();
183 while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type
Daniel Dunbara279bc32009-09-20 02:20:51 +0000184
Chris Lattner9cc34462009-02-28 20:25:14 +0000185 // This is another base case for the recursion. In this case, we know
186 // that we have looped back to a type that we have previously visited.
187 // Generate the appropriate upreference to handle this.
188 if (Slot < CurSize) {
Chris Lattner30794262009-02-28 21:27:31 +0000189 OS << '\\' << unsigned(CurSize-Slot); // Here's the upreference
Chris Lattner9cc34462009-02-28 20:25:14 +0000190 return;
191 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000192
Chris Lattner9cc34462009-02-28 20:25:14 +0000193 TypeStack.push_back(Ty); // Recursive case: Add us to the stack..
Daniel Dunbara279bc32009-09-20 02:20:51 +0000194
Chris Lattner9cc34462009-02-28 20:25:14 +0000195 switch (Ty->getTypeID()) {
Chris Lattner30794262009-02-28 21:27:31 +0000196 case Type::VoidTyID: OS << "void"; break;
197 case Type::FloatTyID: OS << "float"; break;
198 case Type::DoubleTyID: OS << "double"; break;
199 case Type::X86_FP80TyID: OS << "x86_fp80"; break;
200 case Type::FP128TyID: OS << "fp128"; break;
201 case Type::PPC_FP128TyID: OS << "ppc_fp128"; break;
202 case Type::LabelTyID: OS << "label"; break;
Nick Lewycky7a0370f2009-05-30 05:06:04 +0000203 case Type::MetadataTyID: OS << "metadata"; break;
Chris Lattner583ffd82009-02-28 21:18:43 +0000204 case Type::IntegerTyID:
Chris Lattner30794262009-02-28 21:27:31 +0000205 OS << 'i' << cast<IntegerType>(Ty)->getBitWidth();
Chris Lattner583ffd82009-02-28 21:18:43 +0000206 break;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000207
Chris Lattner36942d72009-02-28 20:35:42 +0000208 case Type::FunctionTyID: {
209 const FunctionType *FTy = cast<FunctionType>(Ty);
Chris Lattner30794262009-02-28 21:27:31 +0000210 CalcTypeName(FTy->getReturnType(), TypeStack, OS);
211 OS << " (";
Chris Lattner36942d72009-02-28 20:35:42 +0000212 for (FunctionType::param_iterator I = FTy->param_begin(),
213 E = FTy->param_end(); I != E; ++I) {
214 if (I != FTy->param_begin())
Chris Lattner30794262009-02-28 21:27:31 +0000215 OS << ", ";
216 CalcTypeName(*I, TypeStack, OS);
Chris Lattner9cc34462009-02-28 20:25:14 +0000217 }
Chris Lattner36942d72009-02-28 20:35:42 +0000218 if (FTy->isVarArg()) {
Chris Lattner30794262009-02-28 21:27:31 +0000219 if (FTy->getNumParams()) OS << ", ";
220 OS << "...";
Chris Lattner9cc34462009-02-28 20:25:14 +0000221 }
Chris Lattner30794262009-02-28 21:27:31 +0000222 OS << ')';
Chris Lattner36942d72009-02-28 20:35:42 +0000223 break;
224 }
225 case Type::StructTyID: {
226 const StructType *STy = cast<StructType>(Ty);
227 if (STy->isPacked())
Chris Lattner30794262009-02-28 21:27:31 +0000228 OS << '<';
229 OS << "{ ";
Chris Lattner36942d72009-02-28 20:35:42 +0000230 for (StructType::element_iterator I = STy->element_begin(),
231 E = STy->element_end(); I != E; ++I) {
Chris Lattner30794262009-02-28 21:27:31 +0000232 CalcTypeName(*I, TypeStack, OS);
Chris Lattner36942d72009-02-28 20:35:42 +0000233 if (next(I) != STy->element_end())
Chris Lattner30794262009-02-28 21:27:31 +0000234 OS << ',';
235 OS << ' ';
Chris Lattner9cc34462009-02-28 20:25:14 +0000236 }
Chris Lattner30794262009-02-28 21:27:31 +0000237 OS << '}';
Chris Lattner36942d72009-02-28 20:35:42 +0000238 if (STy->isPacked())
Chris Lattner30794262009-02-28 21:27:31 +0000239 OS << '>';
Chris Lattner36942d72009-02-28 20:35:42 +0000240 break;
241 }
Chris Lattnerfdfeb692010-02-12 20:49:41 +0000242 case Type::UnionTyID: {
243 const UnionType *UTy = cast<UnionType>(Ty);
244 OS << "union { ";
245 for (StructType::element_iterator I = UTy->element_begin(),
246 E = UTy->element_end(); I != E; ++I) {
247 CalcTypeName(*I, TypeStack, OS);
248 if (next(I) != UTy->element_end())
249 OS << ',';
250 OS << ' ';
251 }
252 OS << '}';
253 break;
254 }
Chris Lattner36942d72009-02-28 20:35:42 +0000255 case Type::PointerTyID: {
256 const PointerType *PTy = cast<PointerType>(Ty);
Chris Lattner30794262009-02-28 21:27:31 +0000257 CalcTypeName(PTy->getElementType(), TypeStack, OS);
Chris Lattner36942d72009-02-28 20:35:42 +0000258 if (unsigned AddressSpace = PTy->getAddressSpace())
Chris Lattner30794262009-02-28 21:27:31 +0000259 OS << " addrspace(" << AddressSpace << ')';
260 OS << '*';
Chris Lattner36942d72009-02-28 20:35:42 +0000261 break;
262 }
263 case Type::ArrayTyID: {
264 const ArrayType *ATy = cast<ArrayType>(Ty);
Chris Lattner30794262009-02-28 21:27:31 +0000265 OS << '[' << ATy->getNumElements() << " x ";
266 CalcTypeName(ATy->getElementType(), TypeStack, OS);
267 OS << ']';
Chris Lattner36942d72009-02-28 20:35:42 +0000268 break;
269 }
270 case Type::VectorTyID: {
271 const VectorType *PTy = cast<VectorType>(Ty);
Chris Lattner30794262009-02-28 21:27:31 +0000272 OS << "<" << PTy->getNumElements() << " x ";
273 CalcTypeName(PTy->getElementType(), TypeStack, OS);
274 OS << '>';
Chris Lattner36942d72009-02-28 20:35:42 +0000275 break;
276 }
277 case Type::OpaqueTyID:
Chris Lattner30794262009-02-28 21:27:31 +0000278 OS << "opaque";
Chris Lattner36942d72009-02-28 20:35:42 +0000279 break;
280 default:
Chris Lattner30794262009-02-28 21:27:31 +0000281 OS << "<unrecognized-type>";
Chris Lattner36942d72009-02-28 20:35:42 +0000282 break;
Chris Lattner9cc34462009-02-28 20:25:14 +0000283 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000284
Chris Lattner534361e2009-02-28 20:49:40 +0000285 TypeStack.pop_back(); // Remove self from stack.
Chris Lattner9cc34462009-02-28 20:25:14 +0000286}
287
288/// printTypeInt - The internal guts of printing out a type that has a
289/// potentially named portion.
290///
Chris Lattnerd2b6cb02009-03-01 01:16:21 +0000291void TypePrinting::print(const Type *Ty, raw_ostream &OS,
292 bool IgnoreTopLevelName) {
Chris Lattner9cc34462009-02-28 20:25:14 +0000293 // Check to see if the type is named.
Chris Lattner87185e82009-02-28 23:03:55 +0000294 DenseMap<const Type*, std::string> &TM = getTypeNamesMap(TypeNames);
Chris Lattnerd2b6cb02009-03-01 01:16:21 +0000295 if (!IgnoreTopLevelName) {
296 DenseMap<const Type*, std::string>::iterator I = TM.find(Ty);
297 if (I != TM.end()) {
298 OS << I->second;
299 return;
300 }
Chris Lattner9cc34462009-02-28 20:25:14 +0000301 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000302
Chris Lattner9cc34462009-02-28 20:25:14 +0000303 // Otherwise we have a type that has not been named but is a derived type.
304 // Carefully recurse the type hierarchy to print out any contained symbolic
305 // names.
Chris Lattnerb840cac2009-02-28 20:34:19 +0000306 SmallVector<const Type *, 16> TypeStack;
Chris Lattner9cc34462009-02-28 20:25:14 +0000307 std::string TypeName;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000308
Chris Lattner534361e2009-02-28 20:49:40 +0000309 raw_string_ostream TypeOS(TypeName);
Chris Lattnerd2b6cb02009-03-01 01:16:21 +0000310 CalcTypeName(Ty, TypeStack, TypeOS, IgnoreTopLevelName);
Chris Lattner534361e2009-02-28 20:49:40 +0000311 OS << TypeOS.str();
312
313 // Cache type name for later use.
Chris Lattnerd2b6cb02009-03-01 01:16:21 +0000314 if (!IgnoreTopLevelName)
315 TM.insert(std::make_pair(Ty, TypeOS.str()));
Chris Lattner9cc34462009-02-28 20:25:14 +0000316}
317
Chris Lattner413fd232009-03-01 00:03:38 +0000318namespace {
319 class TypeFinder {
320 // To avoid walking constant expressions multiple times and other IR
321 // objects, we keep several helper maps.
322 DenseSet<const Value*> VisitedConstants;
323 DenseSet<const Type*> VisitedTypes;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000324
Chris Lattner413fd232009-03-01 00:03:38 +0000325 TypePrinting &TP;
326 std::vector<const Type*> &NumberedTypes;
327 public:
328 TypeFinder(TypePrinting &tp, std::vector<const Type*> &numberedTypes)
329 : TP(tp), NumberedTypes(numberedTypes) {}
Daniel Dunbara279bc32009-09-20 02:20:51 +0000330
Chris Lattner413fd232009-03-01 00:03:38 +0000331 void Run(const Module &M) {
Chris Lattner88485862009-03-01 00:32:33 +0000332 // Get types from the type symbol table. This gets opaque types referened
333 // only through derived named types.
334 const TypeSymbolTable &ST = M.getTypeSymbolTable();
335 for (TypeSymbolTable::const_iterator TI = ST.begin(), E = ST.end();
336 TI != E; ++TI)
337 IncorporateType(TI->second);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000338
Chris Lattner413fd232009-03-01 00:03:38 +0000339 // Get types from global variables.
340 for (Module::const_global_iterator I = M.global_begin(),
341 E = M.global_end(); I != E; ++I) {
342 IncorporateType(I->getType());
343 if (I->hasInitializer())
344 IncorporateValue(I->getInitializer());
345 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000346
Chris Lattner413fd232009-03-01 00:03:38 +0000347 // Get types from aliases.
348 for (Module::const_alias_iterator I = M.alias_begin(),
349 E = M.alias_end(); I != E; ++I) {
350 IncorporateType(I->getType());
351 IncorporateValue(I->getAliasee());
352 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000353
Chris Lattner413fd232009-03-01 00:03:38 +0000354 // Get types from functions.
355 for (Module::const_iterator FI = M.begin(), E = M.end(); FI != E; ++FI) {
356 IncorporateType(FI->getType());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000357
Chris Lattner413fd232009-03-01 00:03:38 +0000358 for (Function::const_iterator BB = FI->begin(), E = FI->end();
359 BB != E;++BB)
360 for (BasicBlock::const_iterator II = BB->begin(),
361 E = BB->end(); II != E; ++II) {
362 const Instruction &I = *II;
363 // Incorporate the type of the instruction and all its operands.
364 IncorporateType(I.getType());
365 for (User::const_op_iterator OI = I.op_begin(), OE = I.op_end();
366 OI != OE; ++OI)
367 IncorporateValue(*OI);
368 }
369 }
370 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000371
Chris Lattner413fd232009-03-01 00:03:38 +0000372 private:
373 void IncorporateType(const Type *Ty) {
374 // Check to see if we're already visited this type.
Chris Lattner88485862009-03-01 00:32:33 +0000375 if (!VisitedTypes.insert(Ty).second)
Chris Lattner413fd232009-03-01 00:03:38 +0000376 return;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000377
Chris Lattner413fd232009-03-01 00:03:38 +0000378 // If this is a structure or opaque type, add a name for the type.
Nick Lewycky21cc4462009-04-04 07:22:01 +0000379 if (((isa<StructType>(Ty) && cast<StructType>(Ty)->getNumElements())
380 || isa<OpaqueType>(Ty)) && !TP.hasTypeName(Ty)) {
Chris Lattner413fd232009-03-01 00:03:38 +0000381 TP.addTypeName(Ty, "%"+utostr(unsigned(NumberedTypes.size())));
382 NumberedTypes.push_back(Ty);
383 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000384
Chris Lattner413fd232009-03-01 00:03:38 +0000385 // Recursively walk all contained types.
386 for (Type::subtype_iterator I = Ty->subtype_begin(),
387 E = Ty->subtype_end(); I != E; ++I)
Daniel Dunbara279bc32009-09-20 02:20:51 +0000388 IncorporateType(*I);
Chris Lattner413fd232009-03-01 00:03:38 +0000389 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000390
Chris Lattner413fd232009-03-01 00:03:38 +0000391 /// IncorporateValue - This method is used to walk operand lists finding
392 /// types hiding in constant expressions and other operands that won't be
393 /// walked in other ways. GlobalValues, basic blocks, instructions, and
394 /// inst operands are all explicitly enumerated.
395 void IncorporateValue(const Value *V) {
396 if (V == 0 || !isa<Constant>(V) || isa<GlobalValue>(V)) return;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000397
Chris Lattner413fd232009-03-01 00:03:38 +0000398 // Already visited?
399 if (!VisitedConstants.insert(V).second)
400 return;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000401
Chris Lattner413fd232009-03-01 00:03:38 +0000402 // Check this type.
403 IncorporateType(V->getType());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000404
Chris Lattner413fd232009-03-01 00:03:38 +0000405 // Look in operands for types.
406 const Constant *C = cast<Constant>(V);
407 for (Constant::const_op_iterator I = C->op_begin(),
408 E = C->op_end(); I != E;++I)
409 IncorporateValue(*I);
410 }
411 };
412} // end anonymous namespace
413
414
415/// AddModuleTypesToPrinter - Add all of the symbolic type names for types in
416/// the specified module to the TypePrinter and all numbered types to it and the
417/// NumberedTypes table.
Daniel Dunbara279bc32009-09-20 02:20:51 +0000418static void AddModuleTypesToPrinter(TypePrinting &TP,
Chris Lattner413fd232009-03-01 00:03:38 +0000419 std::vector<const Type*> &NumberedTypes,
420 const Module *M) {
Chris Lattnere9fa33e2009-02-28 23:20:19 +0000421 if (M == 0) return;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000422
Chris Lattnere9fa33e2009-02-28 23:20:19 +0000423 // If the module has a symbol table, take all global types and stuff their
424 // names into the TypeNames map.
425 const TypeSymbolTable &ST = M->getTypeSymbolTable();
426 for (TypeSymbolTable::const_iterator TI = ST.begin(), E = ST.end();
427 TI != E; ++TI) {
428 const Type *Ty = cast<Type>(TI->second);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000429
Chris Lattnere9fa33e2009-02-28 23:20:19 +0000430 // As a heuristic, don't insert pointer to primitive types, because
431 // they are used too often to have a single useful name.
432 if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
433 const Type *PETy = PTy->getElementType();
434 if ((PETy->isPrimitiveType() || PETy->isInteger()) &&
435 !isa<OpaqueType>(PETy))
436 continue;
437 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000438
Chris Lattnere9fa33e2009-02-28 23:20:19 +0000439 // Likewise don't insert primitives either.
440 if (Ty->isInteger() || Ty->isPrimitiveType())
441 continue;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000442
Chris Lattnere9fa33e2009-02-28 23:20:19 +0000443 // Get the name as a string and insert it into TypeNames.
444 std::string NameStr;
Dan Gohman683e9222009-08-12 17:23:50 +0000445 raw_string_ostream NameROS(NameStr);
446 formatted_raw_ostream NameOS(NameROS);
Daniel Dunbar03d76512009-07-25 23:55:21 +0000447 PrintLLVMName(NameOS, TI->first, LocalPrefix);
Dan Gohman683e9222009-08-12 17:23:50 +0000448 NameOS.flush();
449 TP.addTypeName(Ty, NameStr);
Chris Lattnere9fa33e2009-02-28 23:20:19 +0000450 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000451
Chris Lattner413fd232009-03-01 00:03:38 +0000452 // Walk the entire module to find references to unnamed structure and opaque
453 // types. This is required for correctness by opaque types (because multiple
454 // uses of an unnamed opaque type needs to be referred to by the same ID) and
455 // it shrinks complex recursive structure types substantially in some cases.
456 TypeFinder(TP, NumberedTypes).Run(*M);
Chris Lattnere9fa33e2009-02-28 23:20:19 +0000457}
458
Chris Lattner9cc34462009-02-28 20:25:14 +0000459
Chris Lattner9cc34462009-02-28 20:25:14 +0000460/// WriteTypeSymbolic - This attempts to write the specified type as a symbolic
461/// type, iff there is an entry in the modules symbol table for the specified
Chris Lattnerc2871372009-02-28 21:05:51 +0000462/// type or one of it's component types.
Chris Lattner9cc34462009-02-28 20:25:14 +0000463///
Chris Lattnere9fa33e2009-02-28 23:20:19 +0000464void llvm::WriteTypeSymbolic(raw_ostream &OS, const Type *Ty, const Module *M) {
465 TypePrinting Printer;
Chris Lattner413fd232009-03-01 00:03:38 +0000466 std::vector<const Type*> NumberedTypes;
467 AddModuleTypesToPrinter(Printer, NumberedTypes, M);
Chris Lattnere9fa33e2009-02-28 23:20:19 +0000468 Printer.print(Ty, OS);
Chris Lattner9cc34462009-02-28 20:25:14 +0000469}
470
Chris Lattner6ab910b2008-08-19 04:36:02 +0000471//===----------------------------------------------------------------------===//
472// SlotTracker Class: Enumerate slot numbers for unnamed values
473//===----------------------------------------------------------------------===//
474
Chris Lattnerb64871a2008-08-19 04:28:07 +0000475namespace {
476
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000477/// This class provides computation of slot numbers for LLVM Assembly writing.
Chris Lattner45d4c732008-08-17 04:17:45 +0000478///
Chris Lattner0d9574a2008-08-19 04:26:57 +0000479class SlotTracker {
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000480public:
Devang Patel320671d2009-07-08 21:44:25 +0000481 /// ValueMap - A mapping of Values to slot numbers.
Chris Lattner92255072008-08-17 17:25:25 +0000482 typedef DenseMap<const Value*, unsigned> ValueMap;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000483
484private:
Devang Patel320671d2009-07-08 21:44:25 +0000485 /// TheModule - The module for which we are holding slot numbers.
Chris Lattner45d4c732008-08-17 04:17:45 +0000486 const Module* TheModule;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000487
Devang Patel320671d2009-07-08 21:44:25 +0000488 /// TheFunction - The function for which we are holding slot numbers.
Chris Lattner45d4c732008-08-17 04:17:45 +0000489 const Function* TheFunction;
490 bool FunctionProcessed;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000491
Devang Patel320671d2009-07-08 21:44:25 +0000492 /// mMap - The TypePlanes map for the module level data.
Chris Lattner45d4c732008-08-17 04:17:45 +0000493 ValueMap mMap;
494 unsigned mNext;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000495
Devang Patel320671d2009-07-08 21:44:25 +0000496 /// fMap - The TypePlanes map for the function level data.
Chris Lattner45d4c732008-08-17 04:17:45 +0000497 ValueMap fMap;
498 unsigned fNext;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000499
Devang Patel320671d2009-07-08 21:44:25 +0000500 /// mdnMap - Map for MDNodes.
Chris Lattner307c9892009-12-31 02:20:11 +0000501 DenseMap<const MDNode*, unsigned> mdnMap;
Devang Patel320671d2009-07-08 21:44:25 +0000502 unsigned mdnNext;
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000503public:
Chris Lattner45d4c732008-08-17 04:17:45 +0000504 /// Construct from a module
Chris Lattner0d9574a2008-08-19 04:26:57 +0000505 explicit SlotTracker(const Module *M);
Chris Lattner45d4c732008-08-17 04:17:45 +0000506 /// Construct from a function, starting out in incorp state.
Chris Lattner0d9574a2008-08-19 04:26:57 +0000507 explicit SlotTracker(const Function *F);
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000508
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000509 /// Return the slot number of the specified value in it's type
Chris Lattner0d9574a2008-08-19 04:26:57 +0000510 /// plane. If something is not in the SlotTracker, return -1.
Chris Lattner22379bc2007-01-11 03:54:27 +0000511 int getLocalSlot(const Value *V);
512 int getGlobalSlot(const GlobalValue *V);
Devang Patel320671d2009-07-08 21:44:25 +0000513 int getMetadataSlot(const MDNode *N);
Reid Spencerfc621e22004-06-09 15:26:53 +0000514
Misha Brukmanfd939082005-04-21 23:48:37 +0000515 /// If you'd like to deal with a function instead of just a module, use
Chris Lattner0d9574a2008-08-19 04:26:57 +0000516 /// this method to get its data into the SlotTracker.
Misha Brukmanfd939082005-04-21 23:48:37 +0000517 void incorporateFunction(const Function *F) {
518 TheFunction = F;
Reid Spencer28531c72004-08-16 07:46:33 +0000519 FunctionProcessed = false;
520 }
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000521
Misha Brukmanfd939082005-04-21 23:48:37 +0000522 /// After calling incorporateFunction, use this method to remove the
Chris Lattner0d9574a2008-08-19 04:26:57 +0000523 /// most recently incorporated function from the SlotTracker. This
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000524 /// will reset the state of the machine back to just the module contents.
525 void purgeFunction();
526
Devang Patel320671d2009-07-08 21:44:25 +0000527 /// MDNode map iterators.
Chris Lattner307c9892009-12-31 02:20:11 +0000528 typedef DenseMap<const MDNode*, unsigned>::iterator mdn_iterator;
529 mdn_iterator mdn_begin() { return mdnMap.begin(); }
530 mdn_iterator mdn_end() { return mdnMap.end(); }
531 unsigned mdn_size() const { return mdnMap.size(); }
532 bool mdn_empty() const { return mdnMap.empty(); }
Devang Patel320671d2009-07-08 21:44:25 +0000533
Reid Spencerb03de0c2004-05-26 21:56:09 +0000534 /// This function does the actual initialization.
535 inline void initialize();
536
Devang Patel320671d2009-07-08 21:44:25 +0000537 // Implementation Details
538private:
Chris Lattner9446bbe2007-01-09 07:55:49 +0000539 /// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
540 void CreateModuleSlot(const GlobalValue *V);
Devang Patel320671d2009-07-08 21:44:25 +0000541
542 /// CreateMetadataSlot - Insert the specified MDNode* into the slot table.
543 void CreateMetadataSlot(const MDNode *N);
544
Chris Lattner9446bbe2007-01-09 07:55:49 +0000545 /// CreateFunctionSlot - Insert the specified Value* into the slot table.
546 void CreateFunctionSlot(const Value *V);
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000547
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000548 /// Add all of the module level global variables (and their initializers)
549 /// and function declarations, but not the contents of those functions.
550 void processModule();
551
Devang Patel320671d2009-07-08 21:44:25 +0000552 /// Add all of the functions arguments, basic blocks, and instructions.
Reid Spencerb03de0c2004-05-26 21:56:09 +0000553 void processFunction();
554
Chris Lattner0d9574a2008-08-19 04:26:57 +0000555 SlotTracker(const SlotTracker &); // DO NOT IMPLEMENT
556 void operator=(const SlotTracker &); // DO NOT IMPLEMENT
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000557};
558
Chris Lattnerb64871a2008-08-19 04:28:07 +0000559} // end anonymous namespace
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000560
Chris Lattner6ab910b2008-08-19 04:36:02 +0000561
562static SlotTracker *createSlotTracker(const Value *V) {
563 if (const Argument *FA = dyn_cast<Argument>(V))
564 return new SlotTracker(FA->getParent());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000565
Chris Lattner6ab910b2008-08-19 04:36:02 +0000566 if (const Instruction *I = dyn_cast<Instruction>(V))
567 return new SlotTracker(I->getParent()->getParent());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000568
Chris Lattner6ab910b2008-08-19 04:36:02 +0000569 if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
570 return new SlotTracker(BB->getParent());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000571
Chris Lattner6ab910b2008-08-19 04:36:02 +0000572 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
573 return new SlotTracker(GV->getParent());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000574
Chris Lattner6ab910b2008-08-19 04:36:02 +0000575 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V))
Daniel Dunbara279bc32009-09-20 02:20:51 +0000576 return new SlotTracker(GA->getParent());
577
Chris Lattner6ab910b2008-08-19 04:36:02 +0000578 if (const Function *Func = dyn_cast<Function>(V))
579 return new SlotTracker(Func);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000580
Dale Johannesen5f72a5e2010-01-13 00:00:24 +0000581 if (isa<MDNode>(V))
582 return new SlotTracker((Function *)0);
583
Chris Lattner6ab910b2008-08-19 04:36:02 +0000584 return 0;
585}
586
587#if 0
David Greened865e022010-01-05 01:29:26 +0000588#define ST_DEBUG(X) dbgs() << X
Chris Lattner6ab910b2008-08-19 04:36:02 +0000589#else
Chris Lattner24233032008-08-19 04:47:09 +0000590#define ST_DEBUG(X)
Chris Lattner6ab910b2008-08-19 04:36:02 +0000591#endif
592
593// Module level constructor. Causes the contents of the Module (sans functions)
594// to be added to the slot table.
595SlotTracker::SlotTracker(const Module *M)
Chris Lattner6e6b1802009-12-31 02:13:35 +0000596 : TheModule(M), TheFunction(0), FunctionProcessed(false),
Chris Lattner38cf02e2009-12-31 01:36:50 +0000597 mNext(0), fNext(0), mdnNext(0) {
Chris Lattner6ab910b2008-08-19 04:36:02 +0000598}
599
600// Function level constructor. Causes the contents of the Module and the one
601// function provided to be added to the slot table.
602SlotTracker::SlotTracker(const Function *F)
Chris Lattnercfb5a202008-08-19 05:06:27 +0000603 : TheModule(F ? F->getParent() : 0), TheFunction(F), FunctionProcessed(false),
Chris Lattner6e6b1802009-12-31 02:13:35 +0000604 mNext(0), fNext(0), mdnNext(0) {
Chris Lattner6ab910b2008-08-19 04:36:02 +0000605}
606
607inline void SlotTracker::initialize() {
608 if (TheModule) {
609 processModule();
610 TheModule = 0; ///< Prevent re-processing next time we're called.
611 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000612
Chris Lattner6ab910b2008-08-19 04:36:02 +0000613 if (TheFunction && !FunctionProcessed)
614 processFunction();
615}
616
617// Iterate through all the global variables, functions, and global
618// variable initializers and create slots for them.
619void SlotTracker::processModule() {
Chris Lattner24233032008-08-19 04:47:09 +0000620 ST_DEBUG("begin processModule!\n");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000621
Chris Lattner6ab910b2008-08-19 04:36:02 +0000622 // Add all of the unnamed global variables to the value table.
623 for (Module::const_global_iterator I = TheModule->global_begin(),
Devang Patel320671d2009-07-08 21:44:25 +0000624 E = TheModule->global_end(); I != E; ++I) {
Daniel Dunbara279bc32009-09-20 02:20:51 +0000625 if (!I->hasName())
Chris Lattner6ab910b2008-08-19 04:36:02 +0000626 CreateModuleSlot(I);
Devang Patel320671d2009-07-08 21:44:25 +0000627 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000628
Devang Patel37c4a2d2009-07-29 22:04:47 +0000629 // Add metadata used by named metadata.
Daniel Dunbara279bc32009-09-20 02:20:51 +0000630 for (Module::const_named_metadata_iterator
Devang Patel37c4a2d2009-07-29 22:04:47 +0000631 I = TheModule->named_metadata_begin(),
632 E = TheModule->named_metadata_end(); I != E; ++I) {
633 const NamedMDNode *NMD = I;
Chris Lattner5d0cacd2009-12-31 01:22:29 +0000634 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
Devang Patel69d02e02010-01-05 21:47:32 +0000635 if (MDNode *MD = NMD->getOperand(i))
Devang Patele8861b82009-07-30 01:02:04 +0000636 CreateMetadataSlot(MD);
Devang Patel37c4a2d2009-07-29 22:04:47 +0000637 }
638 }
639
Chris Lattner6ab910b2008-08-19 04:36:02 +0000640 // Add all the unnamed functions to the table.
641 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
642 I != E; ++I)
643 if (!I->hasName())
644 CreateModuleSlot(I);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000645
Chris Lattner24233032008-08-19 04:47:09 +0000646 ST_DEBUG("end processModule!\n");
Chris Lattner6ab910b2008-08-19 04:36:02 +0000647}
648
Chris Lattner6ab910b2008-08-19 04:36:02 +0000649// Process the arguments, basic blocks, and instructions of a function.
650void SlotTracker::processFunction() {
Chris Lattner24233032008-08-19 04:47:09 +0000651 ST_DEBUG("begin processFunction!\n");
Chris Lattner6ab910b2008-08-19 04:36:02 +0000652 fNext = 0;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000653
Chris Lattner6ab910b2008-08-19 04:36:02 +0000654 // Add all the function arguments with no names.
655 for(Function::const_arg_iterator AI = TheFunction->arg_begin(),
656 AE = TheFunction->arg_end(); AI != AE; ++AI)
657 if (!AI->hasName())
658 CreateFunctionSlot(AI);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000659
Chris Lattner24233032008-08-19 04:47:09 +0000660 ST_DEBUG("Inserting Instructions:\n");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000661
Chris Lattner6e6b1802009-12-31 02:13:35 +0000662 SmallVector<std::pair<unsigned, MDNode*>, 4> MDForInst;
Devang Patel43215782009-09-16 20:21:17 +0000663
Chris Lattner6ab910b2008-08-19 04:36:02 +0000664 // Add all of the basic blocks and instructions with no names.
665 for (Function::const_iterator BB = TheFunction->begin(),
666 E = TheFunction->end(); BB != E; ++BB) {
667 if (!BB->hasName())
668 CreateFunctionSlot(BB);
Chris Lattner3990b122009-12-28 23:41:32 +0000669
Daniel Dunbara279bc32009-09-20 02:20:51 +0000670 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E;
Devang Patel320671d2009-07-08 21:44:25 +0000671 ++I) {
Chris Lattner3990b122009-12-28 23:41:32 +0000672 if (!I->getType()->isVoidTy() && !I->hasName())
Chris Lattner6ab910b2008-08-19 04:36:02 +0000673 CreateFunctionSlot(I);
Chris Lattner3990b122009-12-28 23:41:32 +0000674
675 // Intrinsics can directly use metadata.
676 if (isa<IntrinsicInst>(I))
677 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
678 if (MDNode *N = dyn_cast_or_null<MDNode>(I->getOperand(i)))
679 CreateMetadataSlot(N);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000680
Devang Patel43215782009-09-16 20:21:17 +0000681 // Process metadata attached with this instruction.
Chris Lattner3990b122009-12-28 23:41:32 +0000682 I->getAllMetadata(MDForInst);
683 for (unsigned i = 0, e = MDForInst.size(); i != e; ++i)
684 CreateMetadataSlot(MDForInst[i].second);
Chris Lattner6e6b1802009-12-31 02:13:35 +0000685 MDForInst.clear();
Devang Patel320671d2009-07-08 21:44:25 +0000686 }
Chris Lattner6ab910b2008-08-19 04:36:02 +0000687 }
Devang Patel43215782009-09-16 20:21:17 +0000688
Chris Lattner6ab910b2008-08-19 04:36:02 +0000689 FunctionProcessed = true;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000690
Chris Lattner24233032008-08-19 04:47:09 +0000691 ST_DEBUG("end processFunction!\n");
Chris Lattner6ab910b2008-08-19 04:36:02 +0000692}
693
694/// Clean up after incorporating a function. This is the only way to get out of
695/// the function incorporation state that affects get*Slot/Create*Slot. Function
696/// incorporation state is indicated by TheFunction != 0.
697void SlotTracker::purgeFunction() {
Chris Lattner24233032008-08-19 04:47:09 +0000698 ST_DEBUG("begin purgeFunction!\n");
Chris Lattner6ab910b2008-08-19 04:36:02 +0000699 fMap.clear(); // Simply discard the function level map
700 TheFunction = 0;
701 FunctionProcessed = false;
Chris Lattner24233032008-08-19 04:47:09 +0000702 ST_DEBUG("end purgeFunction!\n");
Chris Lattner6ab910b2008-08-19 04:36:02 +0000703}
704
705/// getGlobalSlot - Get the slot number of a global value.
706int SlotTracker::getGlobalSlot(const GlobalValue *V) {
707 // Check for uninitialized state and do lazy initialization.
708 initialize();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000709
Chris Lattner6ab910b2008-08-19 04:36:02 +0000710 // Find the type plane in the module map
711 ValueMap::iterator MI = mMap.find(V);
Dan Gohmanaeaf2452008-10-01 19:58:59 +0000712 return MI == mMap.end() ? -1 : (int)MI->second;
Chris Lattner6ab910b2008-08-19 04:36:02 +0000713}
714
Chris Lattner307c9892009-12-31 02:20:11 +0000715/// getMetadataSlot - Get the slot number of a MDNode.
Devang Patel320671d2009-07-08 21:44:25 +0000716int SlotTracker::getMetadataSlot(const MDNode *N) {
717 // Check for uninitialized state and do lazy initialization.
718 initialize();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000719
Devang Patel320671d2009-07-08 21:44:25 +0000720 // Find the type plane in the module map
Chris Lattner307c9892009-12-31 02:20:11 +0000721 mdn_iterator MI = mdnMap.find(N);
Devang Patel320671d2009-07-08 21:44:25 +0000722 return MI == mdnMap.end() ? -1 : (int)MI->second;
723}
724
Chris Lattner6ab910b2008-08-19 04:36:02 +0000725
726/// getLocalSlot - Get the slot number for a value that is local to a function.
727int SlotTracker::getLocalSlot(const Value *V) {
728 assert(!isa<Constant>(V) && "Can't get a constant or global slot with this!");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000729
Chris Lattner6ab910b2008-08-19 04:36:02 +0000730 // Check for uninitialized state and do lazy initialization.
731 initialize();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000732
Chris Lattner6ab910b2008-08-19 04:36:02 +0000733 ValueMap::iterator FI = fMap.find(V);
Dan Gohmanaeaf2452008-10-01 19:58:59 +0000734 return FI == fMap.end() ? -1 : (int)FI->second;
Chris Lattner6ab910b2008-08-19 04:36:02 +0000735}
736
737
738/// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
739void SlotTracker::CreateModuleSlot(const GlobalValue *V) {
740 assert(V && "Can't insert a null Value into SlotTracker!");
Chris Lattner4ee93c42009-12-29 07:25:48 +0000741 assert(!V->getType()->isVoidTy() && "Doesn't need a slot!");
Chris Lattner6ab910b2008-08-19 04:36:02 +0000742 assert(!V->hasName() && "Doesn't need a slot!");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000743
Chris Lattner6ab910b2008-08-19 04:36:02 +0000744 unsigned DestSlot = mNext++;
745 mMap[V] = DestSlot;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000746
Chris Lattner24233032008-08-19 04:47:09 +0000747 ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" <<
Chris Lattner6ab910b2008-08-19 04:36:02 +0000748 DestSlot << " [");
749 // G = Global, F = Function, A = Alias, o = other
Chris Lattner24233032008-08-19 04:47:09 +0000750 ST_DEBUG((isa<GlobalVariable>(V) ? 'G' :
Chris Lattner6ab910b2008-08-19 04:36:02 +0000751 (isa<Function>(V) ? 'F' :
752 (isa<GlobalAlias>(V) ? 'A' : 'o'))) << "]\n");
753}
754
Chris Lattner6ab910b2008-08-19 04:36:02 +0000755/// CreateSlot - Create a new slot for the specified value if it has no name.
756void SlotTracker::CreateFunctionSlot(const Value *V) {
Chris Lattner4ee93c42009-12-29 07:25:48 +0000757 assert(!V->getType()->isVoidTy() && !V->hasName() && "Doesn't need a slot!");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000758
Chris Lattner6ab910b2008-08-19 04:36:02 +0000759 unsigned DestSlot = fNext++;
760 fMap[V] = DestSlot;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000761
Chris Lattner6ab910b2008-08-19 04:36:02 +0000762 // G = Global, F = Function, o = other
Chris Lattner24233032008-08-19 04:47:09 +0000763 ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" <<
Chris Lattner6ab910b2008-08-19 04:36:02 +0000764 DestSlot << " [o]\n");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000765}
Chris Lattner6ab910b2008-08-19 04:36:02 +0000766
Devang Patel320671d2009-07-08 21:44:25 +0000767/// CreateModuleSlot - Insert the specified MDNode* into the slot table.
768void SlotTracker::CreateMetadataSlot(const MDNode *N) {
769 assert(N && "Can't insert a null Value into SlotTracker!");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000770
Chris Lattner2b4b1e22009-12-31 02:27:30 +0000771 // Don't insert if N is a function-local metadata, these are always printed
772 // inline.
Victor Hernandez5d301622009-12-18 20:09:14 +0000773 if (N->isFunctionLocal())
774 return;
Victor Hernandezff7707e2009-12-04 20:07:10 +0000775
Chris Lattner307c9892009-12-31 02:20:11 +0000776 mdn_iterator I = mdnMap.find(N);
Devang Patel320671d2009-07-08 21:44:25 +0000777 if (I != mdnMap.end())
778 return;
Chris Lattner6ab910b2008-08-19 04:36:02 +0000779
Devang Patel320671d2009-07-08 21:44:25 +0000780 unsigned DestSlot = mdnNext++;
781 mdnMap[N] = DestSlot;
782
Chris Lattner2b4b1e22009-12-31 02:27:30 +0000783 // Recursively add any MDNodes referenced by operands.
784 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
785 if (const MDNode *Op = dyn_cast_or_null<MDNode>(N->getOperand(i)))
786 CreateMetadataSlot(Op);
Devang Patel320671d2009-07-08 21:44:25 +0000787}
Chris Lattner6ab910b2008-08-19 04:36:02 +0000788
789//===----------------------------------------------------------------------===//
790// AsmWriter Implementation
791//===----------------------------------------------------------------------===//
Chris Lattnerf082b802002-07-23 18:07:49 +0000792
Dan Gohman1220e102009-08-12 20:56:03 +0000793static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
Dan Gohmand6c0f652009-08-13 15:27:57 +0000794 TypePrinting *TypePrinter,
Chris Lattner0d9574a2008-08-19 04:26:57 +0000795 SlotTracker *Machine);
Reid Spencer0e25e1c2004-07-04 11:50:43 +0000796
Chris Lattnerc97536e2008-08-17 04:40:13 +0000797
Chris Lattner207b5bc2001-10-29 16:37:48 +0000798
Chris Lattner82c4bc72006-12-06 06:40:49 +0000799static const char *getPredicateText(unsigned predicate) {
Reid Spencer81dfeb32006-12-04 05:19:18 +0000800 const char * pred = "unknown";
801 switch (predicate) {
Chris Lattner6e6b1802009-12-31 02:13:35 +0000802 case FCmpInst::FCMP_FALSE: pred = "false"; break;
803 case FCmpInst::FCMP_OEQ: pred = "oeq"; break;
804 case FCmpInst::FCMP_OGT: pred = "ogt"; break;
805 case FCmpInst::FCMP_OGE: pred = "oge"; break;
806 case FCmpInst::FCMP_OLT: pred = "olt"; break;
807 case FCmpInst::FCMP_OLE: pred = "ole"; break;
808 case FCmpInst::FCMP_ONE: pred = "one"; break;
809 case FCmpInst::FCMP_ORD: pred = "ord"; break;
810 case FCmpInst::FCMP_UNO: pred = "uno"; break;
811 case FCmpInst::FCMP_UEQ: pred = "ueq"; break;
812 case FCmpInst::FCMP_UGT: pred = "ugt"; break;
813 case FCmpInst::FCMP_UGE: pred = "uge"; break;
814 case FCmpInst::FCMP_ULT: pred = "ult"; break;
815 case FCmpInst::FCMP_ULE: pred = "ule"; break;
816 case FCmpInst::FCMP_UNE: pred = "une"; break;
817 case FCmpInst::FCMP_TRUE: pred = "true"; break;
818 case ICmpInst::ICMP_EQ: pred = "eq"; break;
819 case ICmpInst::ICMP_NE: pred = "ne"; break;
820 case ICmpInst::ICMP_SGT: pred = "sgt"; break;
821 case ICmpInst::ICMP_SGE: pred = "sge"; break;
822 case ICmpInst::ICMP_SLT: pred = "slt"; break;
823 case ICmpInst::ICMP_SLE: pred = "sle"; break;
824 case ICmpInst::ICMP_UGT: pred = "ugt"; break;
825 case ICmpInst::ICMP_UGE: pred = "uge"; break;
826 case ICmpInst::ICMP_ULT: pred = "ult"; break;
827 case ICmpInst::ICMP_ULE: pred = "ule"; break;
Reid Spencer81dfeb32006-12-04 05:19:18 +0000828 }
829 return pred;
830}
831
Devang Patel320671d2009-07-08 21:44:25 +0000832
Dan Gohman1220e102009-08-12 20:56:03 +0000833static void WriteOptimizationInfo(raw_ostream &Out, const User *U) {
Dan Gohman1224c382009-07-20 21:19:07 +0000834 if (const OverflowingBinaryOperator *OBO =
835 dyn_cast<OverflowingBinaryOperator>(U)) {
Dan Gohman5078f842009-08-20 17:11:38 +0000836 if (OBO->hasNoUnsignedWrap())
Dan Gohman59858cf2009-07-27 16:11:46 +0000837 Out << " nuw";
Dan Gohman5078f842009-08-20 17:11:38 +0000838 if (OBO->hasNoSignedWrap())
Dan Gohman59858cf2009-07-27 16:11:46 +0000839 Out << " nsw";
Dan Gohman1224c382009-07-20 21:19:07 +0000840 } else if (const SDivOperator *Div = dyn_cast<SDivOperator>(U)) {
841 if (Div->isExact())
Dan Gohman59858cf2009-07-27 16:11:46 +0000842 Out << " exact";
Dan Gohmandd8004d2009-07-27 21:53:46 +0000843 } else if (const GEPOperator *GEP = dyn_cast<GEPOperator>(U)) {
844 if (GEP->isInBounds())
845 Out << " inbounds";
Dan Gohman1224c382009-07-20 21:19:07 +0000846 }
847}
848
Dan Gohman1220e102009-08-12 20:56:03 +0000849static void WriteConstantInt(raw_ostream &Out, const Constant *CV,
Chris Lattner9cc34462009-02-28 20:25:14 +0000850 TypePrinting &TypePrinter, SlotTracker *Machine) {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +0000851 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Benjamin Kramer11acaa32010-01-05 20:07:06 +0000852 if (CI->getType()->isInteger(1)) {
Reid Spencer579dca12007-01-12 04:24:46 +0000853 Out << (CI->getZExtValue() ? "true" : "false");
Chris Lattnerfad86b02008-08-17 07:19:36 +0000854 return;
855 }
856 Out << CI->getValue();
857 return;
858 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000859
Chris Lattnerfad86b02008-08-17 07:19:36 +0000860 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000861 if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEdouble ||
862 &CFP->getValueAPF().getSemantics() == &APFloat::IEEEsingle) {
863 // We would like to output the FP constant value in exponential notation,
864 // but we cannot do this if doing so will lose precision. Check here to
865 // make sure that we only output it in exponential format if we can parse
866 // the value back and get the same value.
867 //
Dale Johannesen541ed9f2009-01-21 20:32:55 +0000868 bool ignored;
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000869 bool isDouble = &CFP->getValueAPF().getSemantics()==&APFloat::IEEEdouble;
Chris Lattnerfad86b02008-08-17 07:19:36 +0000870 double Val = isDouble ? CFP->getValueAPF().convertToDouble() :
871 CFP->getValueAPF().convertToFloat();
Benjamin Kramer59088392010-01-29 14:42:22 +0000872 SmallString<128> StrVal;
873 raw_svector_ostream(StrVal) << Val;
Chris Lattner66e810b2002-04-18 18:53:13 +0000874
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000875 // Check to make sure that the stringized number is not some string like
876 // "Inf" or NaN, that atof will accept, but the lexer will not. Check
877 // that the string matches the "[-+]?[0-9]" regex.
878 //
879 if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
880 ((StrVal[0] == '-' || StrVal[0] == '+') &&
881 (StrVal[1] >= '0' && StrVal[1] <= '9'))) {
882 // Reparse stringized version!
883 if (atof(StrVal.c_str()) == Val) {
Benjamin Kramer59088392010-01-29 14:42:22 +0000884 Out << StrVal.str();
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000885 return;
886 }
Chris Lattner66e810b2002-04-18 18:53:13 +0000887 }
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000888 // Otherwise we could not reparse it to exactly the same value, so we must
Dale Johannesen541ed9f2009-01-21 20:32:55 +0000889 // output the string in hexadecimal format! Note that loading and storing
890 // floating point types changes the bits of NaNs on some hosts, notably
891 // x86, so we must not use these types.
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000892 assert(sizeof(double) == sizeof(uint64_t) &&
893 "assuming that double is 64 bits!");
Chris Lattnerc6a13462008-11-10 04:30:26 +0000894 char Buffer[40];
Dale Johannesen541ed9f2009-01-21 20:32:55 +0000895 APFloat apf = CFP->getValueAPF();
896 // Floats are represented in ASCII IR as double, convert.
897 if (!isDouble)
Daniel Dunbara279bc32009-09-20 02:20:51 +0000898 apf.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven,
Dale Johannesen541ed9f2009-01-21 20:32:55 +0000899 &ignored);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000900 Out << "0x" <<
901 utohex_buffer(uint64_t(apf.bitcastToAPInt().getZExtValue()),
Dale Johannesen541ed9f2009-01-21 20:32:55 +0000902 Buffer+40);
Chris Lattnercfb5a202008-08-19 05:06:27 +0000903 return;
904 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000905
Chris Lattnercfb5a202008-08-19 05:06:27 +0000906 // Some form of long double. These appear as a magic letter identifying
907 // the type, then a fixed number of hex digits.
908 Out << "0x";
Dale Johannesen1b25cb22009-03-23 21:16:53 +0000909 if (&CFP->getValueAPF().getSemantics() == &APFloat::x87DoubleExtended) {
Chris Lattnercfb5a202008-08-19 05:06:27 +0000910 Out << 'K';
Dale Johannesen1b25cb22009-03-23 21:16:53 +0000911 // api needed to prevent premature destruction
912 APInt api = CFP->getValueAPF().bitcastToAPInt();
913 const uint64_t* p = api.getRawData();
914 uint64_t word = p[1];
915 int shiftcount=12;
916 int width = api.getBitWidth();
917 for (int j=0; j<width; j+=4, shiftcount-=4) {
918 unsigned int nibble = (word>>shiftcount) & 15;
919 if (nibble < 10)
920 Out << (unsigned char)(nibble + '0');
921 else
922 Out << (unsigned char)(nibble - 10 + 'A');
923 if (shiftcount == 0 && j+4 < width) {
924 word = *p;
925 shiftcount = 64;
926 if (width-j-4 < 64)
927 shiftcount = width-j-4;
928 }
929 }
930 return;
931 } else if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEquad)
Chris Lattnercfb5a202008-08-19 05:06:27 +0000932 Out << 'L';
933 else if (&CFP->getValueAPF().getSemantics() == &APFloat::PPCDoubleDouble)
934 Out << 'M';
935 else
Torok Edwinc23197a2009-07-14 16:55:14 +0000936 llvm_unreachable("Unsupported floating point type");
Chris Lattnercfb5a202008-08-19 05:06:27 +0000937 // api needed to prevent premature destruction
Dale Johannesen7111b022008-10-09 18:53:47 +0000938 APInt api = CFP->getValueAPF().bitcastToAPInt();
Chris Lattnercfb5a202008-08-19 05:06:27 +0000939 const uint64_t* p = api.getRawData();
940 uint64_t word = *p;
941 int shiftcount=60;
942 int width = api.getBitWidth();
943 for (int j=0; j<width; j+=4, shiftcount-=4) {
944 unsigned int nibble = (word>>shiftcount) & 15;
945 if (nibble < 10)
946 Out << (unsigned char)(nibble + '0');
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000947 else
Chris Lattnercfb5a202008-08-19 05:06:27 +0000948 Out << (unsigned char)(nibble - 10 + 'A');
949 if (shiftcount == 0 && j+4 < width) {
950 word = *(++p);
951 shiftcount = 64;
952 if (width-j-4 < 64)
953 shiftcount = width-j-4;
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000954 }
955 }
Chris Lattnercfb5a202008-08-19 05:06:27 +0000956 return;
957 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000958
Chris Lattnercfb5a202008-08-19 05:06:27 +0000959 if (isa<ConstantAggregateZero>(CV)) {
Chris Lattnerde512b52004-02-15 05:55:15 +0000960 Out << "zeroinitializer";
Chris Lattnercfb5a202008-08-19 05:06:27 +0000961 return;
962 }
Chris Lattner73050e12009-10-28 03:38:12 +0000963
964 if (const BlockAddress *BA = dyn_cast<BlockAddress>(CV)) {
965 Out << "blockaddress(";
966 WriteAsOperandInternal(Out, BA->getFunction(), &TypePrinter, Machine);
967 Out << ", ";
Chris Lattnercdfc9402009-11-01 01:27:45 +0000968 WriteAsOperandInternal(Out, BA->getBasicBlock(), &TypePrinter, Machine);
Chris Lattner73050e12009-10-28 03:38:12 +0000969 Out << ")";
970 return;
971 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000972
Chris Lattnercfb5a202008-08-19 05:06:27 +0000973 if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
Chris Lattner66e810b2002-04-18 18:53:13 +0000974 // As a special case, print the array as a string if it is an array of
Dan Gohman9b38d7d2008-05-12 16:34:30 +0000975 // i8 with ConstantInt values.
Misha Brukmanfd939082005-04-21 23:48:37 +0000976 //
Chris Lattner66e810b2002-04-18 18:53:13 +0000977 const Type *ETy = CA->getType()->getElementType();
Chris Lattner18365502006-01-23 23:03:36 +0000978 if (CA->isString()) {
Chris Lattner66e810b2002-04-18 18:53:13 +0000979 Out << "c\"";
Chris Lattner18365502006-01-23 23:03:36 +0000980 PrintEscapedString(CA->getAsString(), Out);
Chris Lattnercfb5a202008-08-19 05:06:27 +0000981 Out << '"';
Chris Lattner66e810b2002-04-18 18:53:13 +0000982 } else { // Cannot output in string format...
Misha Brukman40c732c2004-06-04 21:11:51 +0000983 Out << '[';
Chris Lattner7a716ad2002-04-16 21:36:08 +0000984 if (CA->getNumOperands()) {
Chris Lattner0f7364b2009-02-28 21:26:53 +0000985 TypePrinter.print(ETy, Out);
Dan Gohman8dae1382008-09-14 17:21:12 +0000986 Out << ' ';
Chris Lattner7a716ad2002-04-16 21:36:08 +0000987 WriteAsOperandInternal(Out, CA->getOperand(0),
Dan Gohmand6c0f652009-08-13 15:27:57 +0000988 &TypePrinter, Machine);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000989 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
990 Out << ", ";
Chris Lattner0f7364b2009-02-28 21:26:53 +0000991 TypePrinter.print(ETy, Out);
Dan Gohman8dae1382008-09-14 17:21:12 +0000992 Out << ' ';
Dan Gohmand6c0f652009-08-13 15:27:57 +0000993 WriteAsOperandInternal(Out, CA->getOperand(i), &TypePrinter, Machine);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000994 }
995 }
Dan Gohman8dae1382008-09-14 17:21:12 +0000996 Out << ']';
Chris Lattner7a716ad2002-04-16 21:36:08 +0000997 }
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 (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
Andrew Lenharth43f344a2007-01-08 18:21:30 +00001002 if (CS->getType()->isPacked())
1003 Out << '<';
Misha Brukman40c732c2004-06-04 21:11:51 +00001004 Out << '{';
Jim Laskeya3f332b2006-02-25 12:27:03 +00001005 unsigned N = CS->getNumOperands();
1006 if (N) {
Chris Lattner24233032008-08-19 04:47:09 +00001007 Out << ' ';
Chris Lattner0f7364b2009-02-28 21:26:53 +00001008 TypePrinter.print(CS->getOperand(0)->getType(), Out);
Dan Gohman8dae1382008-09-14 17:21:12 +00001009 Out << ' ';
Chris Lattner7a716ad2002-04-16 21:36:08 +00001010
Dan Gohmand6c0f652009-08-13 15:27:57 +00001011 WriteAsOperandInternal(Out, CS->getOperand(0), &TypePrinter, Machine);
Chris Lattner7a716ad2002-04-16 21:36:08 +00001012
Jim Laskeya3f332b2006-02-25 12:27:03 +00001013 for (unsigned i = 1; i < N; i++) {
Chris Lattner7a716ad2002-04-16 21:36:08 +00001014 Out << ", ";
Chris Lattner0f7364b2009-02-28 21:26:53 +00001015 TypePrinter.print(CS->getOperand(i)->getType(), Out);
Dan Gohman8dae1382008-09-14 17:21:12 +00001016 Out << ' ';
Chris Lattner7a716ad2002-04-16 21:36:08 +00001017
Dan Gohmand6c0f652009-08-13 15:27:57 +00001018 WriteAsOperandInternal(Out, CS->getOperand(i), &TypePrinter, Machine);
Chris Lattner7a716ad2002-04-16 21:36:08 +00001019 }
Dan Gohman8dae1382008-09-14 17:21:12 +00001020 Out << ' ';
Chris Lattner7a716ad2002-04-16 21:36:08 +00001021 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001022
Dan Gohman8dae1382008-09-14 17:21:12 +00001023 Out << '}';
Andrew Lenharth43f344a2007-01-08 18:21:30 +00001024 if (CS->getType()->isPacked())
1025 Out << '>';
Chris Lattnercfb5a202008-08-19 05:06:27 +00001026 return;
1027 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001028
Chris Lattnercfb5a202008-08-19 05:06:27 +00001029 if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
1030 const Type *ETy = CP->getType()->getElementType();
1031 assert(CP->getNumOperands() > 0 &&
1032 "Number of operands for a PackedConst must be > 0");
Dan Gohman7dfa07f2009-02-11 00:25:25 +00001033 Out << '<';
Chris Lattner0f7364b2009-02-28 21:26:53 +00001034 TypePrinter.print(ETy, Out);
Dan Gohman8dae1382008-09-14 17:21:12 +00001035 Out << ' ';
Dan Gohmand6c0f652009-08-13 15:27:57 +00001036 WriteAsOperandInternal(Out, CP->getOperand(0), &TypePrinter, Machine);
Chris Lattnercfb5a202008-08-19 05:06:27 +00001037 for (unsigned i = 1, e = CP->getNumOperands(); i != e; ++i) {
Chris Lattner4667b712008-08-19 05:26:17 +00001038 Out << ", ";
Chris Lattner0f7364b2009-02-28 21:26:53 +00001039 TypePrinter.print(ETy, Out);
Dan Gohman8dae1382008-09-14 17:21:12 +00001040 Out << ' ';
Dan Gohmand6c0f652009-08-13 15:27:57 +00001041 WriteAsOperandInternal(Out, CP->getOperand(i), &TypePrinter, Machine);
Chris Lattnercfb5a202008-08-19 05:06:27 +00001042 }
Dan Gohman7dfa07f2009-02-11 00:25:25 +00001043 Out << '>';
Chris Lattnercfb5a202008-08-19 05:06:27 +00001044 return;
1045 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001046
Chris Lattnercfb5a202008-08-19 05:06:27 +00001047 if (isa<ConstantPointerNull>(CV)) {
Chris Lattner7a716ad2002-04-16 21:36:08 +00001048 Out << "null";
Chris Lattnercfb5a202008-08-19 05:06:27 +00001049 return;
1050 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001051
Chris Lattnercfb5a202008-08-19 05:06:27 +00001052 if (isa<UndefValue>(CV)) {
Chris Lattnerb976e662004-10-16 18:08:06 +00001053 Out << "undef";
Chris Lattnercfb5a202008-08-19 05:06:27 +00001054 return;
1055 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001056
Devang Patel320671d2009-07-08 21:44:25 +00001057 if (const MDNode *Node = dyn_cast<MDNode>(CV)) {
1058 Out << "!" << Machine->getMetadataSlot(Node);
1059 return;
1060 }
1061
Chris Lattnercfb5a202008-08-19 05:06:27 +00001062 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Reid Spencer81dfeb32006-12-04 05:19:18 +00001063 Out << CE->getOpcodeName();
Dan Gohman59858cf2009-07-27 16:11:46 +00001064 WriteOptimizationInfo(Out, CE);
Reid Spencer81dfeb32006-12-04 05:19:18 +00001065 if (CE->isCompare())
Chris Lattnercfb5a202008-08-19 05:06:27 +00001066 Out << ' ' << getPredicateText(CE->getPredicate());
Reid Spencer81dfeb32006-12-04 05:19:18 +00001067 Out << " (";
Misha Brukmanfd939082005-04-21 23:48:37 +00001068
Vikram S. Adveb4dbb442002-07-14 23:14:45 +00001069 for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) {
Chris Lattner0f7364b2009-02-28 21:26:53 +00001070 TypePrinter.print((*OI)->getType(), Out);
Dan Gohman8dae1382008-09-14 17:21:12 +00001071 Out << ' ';
Dan Gohmand6c0f652009-08-13 15:27:57 +00001072 WriteAsOperandInternal(Out, *OI, &TypePrinter, Machine);
Vikram S. Adveb4dbb442002-07-14 23:14:45 +00001073 if (OI+1 != CE->op_end())
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001074 Out << ", ";
Vikram S. Adveb4dbb442002-07-14 23:14:45 +00001075 }
Misha Brukmanfd939082005-04-21 23:48:37 +00001076
Dan Gohman995be7d2008-05-31 19:12:39 +00001077 if (CE->hasIndices()) {
1078 const SmallVector<unsigned, 4> &Indices = CE->getIndices();
1079 for (unsigned i = 0, e = Indices.size(); i != e; ++i)
1080 Out << ", " << Indices[i];
1081 }
1082
Reid Spencer3da59db2006-11-27 01:05:10 +00001083 if (CE->isCast()) {
Chris Lattner95586b82002-08-15 19:37:43 +00001084 Out << " to ";
Chris Lattner0f7364b2009-02-28 21:26:53 +00001085 TypePrinter.print(CE->getType(), Out);
Chris Lattner95586b82002-08-15 19:37:43 +00001086 }
Reid Spencer3da59db2006-11-27 01:05:10 +00001087
Misha Brukman40c732c2004-06-04 21:11:51 +00001088 Out << ')';
Chris Lattnercfb5a202008-08-19 05:06:27 +00001089 return;
Chris Lattner7a716ad2002-04-16 21:36:08 +00001090 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001091
Chris Lattnercfb5a202008-08-19 05:06:27 +00001092 Out << "<placeholder or erroneous Constant>";
Chris Lattner7a716ad2002-04-16 21:36:08 +00001093}
1094
Chris Lattner85b19122009-12-31 02:31:59 +00001095static void WriteMDNodeBodyInternal(raw_ostream &Out, const MDNode *Node,
1096 TypePrinting *TypePrinter,
1097 SlotTracker *Machine) {
1098 Out << "!{";
1099 for (unsigned mi = 0, me = Node->getNumOperands(); mi != me; ++mi) {
1100 const Value *V = Node->getOperand(mi);
1101 if (V == 0)
1102 Out << "null";
1103 else {
1104 TypePrinter->print(V->getType(), Out);
1105 Out << ' ';
1106 WriteAsOperandInternal(Out, Node->getOperand(mi),
1107 TypePrinter, Machine);
1108 }
1109 if (mi + 1 != me)
1110 Out << ", ";
1111 }
1112
1113 Out << "}";
1114}
1115
Chris Lattner7a716ad2002-04-16 21:36:08 +00001116
Misha Brukmanab5c6002004-03-02 00:22:19 +00001117/// WriteAsOperand - Write the name of the specified value out to the specified
1118/// ostream. This can be useful when you just want to print int %reg126, not
1119/// the whole instruction that generated it.
1120///
Dan Gohman1220e102009-08-12 20:56:03 +00001121static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
Dan Gohmand6c0f652009-08-13 15:27:57 +00001122 TypePrinting *TypePrinter,
Chris Lattner0d9574a2008-08-19 04:26:57 +00001123 SlotTracker *Machine) {
Chris Lattnerc97536e2008-08-17 04:40:13 +00001124 if (V->hasName()) {
1125 PrintLLVMName(Out, V);
1126 return;
1127 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001128
Chris Lattnerc97536e2008-08-17 04:40:13 +00001129 const Constant *CV = dyn_cast<Constant>(V);
1130 if (CV && !isa<GlobalValue>(CV)) {
Dan Gohmand6c0f652009-08-13 15:27:57 +00001131 assert(TypePrinter && "Constants require TypePrinting!");
1132 WriteConstantInt(Out, CV, *TypePrinter, Machine);
Chris Lattnercfb5a202008-08-19 05:06:27 +00001133 return;
1134 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001135
Chris Lattnercfb5a202008-08-19 05:06:27 +00001136 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
Chris Lattnerc97536e2008-08-17 04:40:13 +00001137 Out << "asm ";
1138 if (IA->hasSideEffects())
1139 Out << "sideeffect ";
Dale Johannesen8ba2d5b2009-10-21 23:28:00 +00001140 if (IA->isAlignStack())
1141 Out << "alignstack ";
Chris Lattnerc97536e2008-08-17 04:40:13 +00001142 Out << '"';
1143 PrintEscapedString(IA->getAsmString(), Out);
1144 Out << "\", \"";
1145 PrintEscapedString(IA->getConstraintString(), Out);
1146 Out << '"';
Chris Lattnercfb5a202008-08-19 05:06:27 +00001147 return;
1148 }
Devang Patele54abc92009-07-22 17:43:22 +00001149
Devang Patel104cf9e2009-07-23 01:07:34 +00001150 if (const MDNode *N = dyn_cast<MDNode>(V)) {
Victor Hernandez5d301622009-12-18 20:09:14 +00001151 if (N->isFunctionLocal()) {
Victor Hernandez97e24502009-12-04 01:35:02 +00001152 // Print metadata inline, not via slot reference number.
Chris Lattner85b19122009-12-31 02:31:59 +00001153 WriteMDNodeBodyInternal(Out, N, TypePrinter, Machine);
Victor Hernandez97e24502009-12-04 01:35:02 +00001154 return;
1155 }
1156
Dale Johannesen5f72a5e2010-01-13 00:00:24 +00001157 if (!Machine)
1158 Machine = createSlotTracker(V);
Devang Patel104cf9e2009-07-23 01:07:34 +00001159 Out << '!' << Machine->getMetadataSlot(N);
1160 return;
1161 }
1162
Devang Patele54abc92009-07-22 17:43:22 +00001163 if (const MDString *MDS = dyn_cast<MDString>(V)) {
Devang Patele54abc92009-07-22 17:43:22 +00001164 Out << "!\"";
Daniel Dunbar03d76512009-07-25 23:55:21 +00001165 PrintEscapedString(MDS->getString(), Out);
Devang Patele54abc92009-07-22 17:43:22 +00001166 Out << '"';
1167 return;
1168 }
1169
Evan Cheng746d5462009-11-16 07:10:36 +00001170 if (V->getValueID() == Value::PseudoSourceValueVal ||
1171 V->getValueID() == Value::FixedStackPseudoSourceValueVal) {
Dan Gohmancd26ec52009-09-23 01:33:16 +00001172 V->print(Out);
1173 return;
1174 }
1175
Chris Lattnercfb5a202008-08-19 05:06:27 +00001176 char Prefix = '%';
1177 int Slot;
1178 if (Machine) {
1179 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1180 Slot = Machine->getGlobalSlot(GV);
1181 Prefix = '@';
1182 } else {
1183 Slot = Machine->getLocalSlot(V);
1184 }
Chris Lattnerc97536e2008-08-17 04:40:13 +00001185 } else {
Chris Lattnercfb5a202008-08-19 05:06:27 +00001186 Machine = createSlotTracker(V);
Chris Lattnerc97536e2008-08-17 04:40:13 +00001187 if (Machine) {
1188 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1189 Slot = Machine->getGlobalSlot(GV);
1190 Prefix = '@';
1191 } else {
1192 Slot = Machine->getLocalSlot(V);
1193 }
Dan Gohmand6c0f652009-08-13 15:27:57 +00001194 delete Machine;
Chris Lattner80cd1152006-01-25 22:26:05 +00001195 } else {
Chris Lattnercfb5a202008-08-19 05:06:27 +00001196 Slot = -1;
Chris Lattner7a716ad2002-04-16 21:36:08 +00001197 }
1198 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001199
Chris Lattnercfb5a202008-08-19 05:06:27 +00001200 if (Slot != -1)
1201 Out << Prefix << Slot;
1202 else
1203 Out << "<badref>";
Chris Lattner7a716ad2002-04-16 21:36:08 +00001204}
1205
Dan Gohman1220e102009-08-12 20:56:03 +00001206void llvm::WriteAsOperand(raw_ostream &Out, const Value *V,
1207 bool PrintType, const Module *Context) {
Dan Gohmand6c0f652009-08-13 15:27:57 +00001208
1209 // Fast path: Don't construct and populate a TypePrinting object if we
1210 // won't be needing any types printed.
Dan Gohman009fc9e2009-08-13 23:07:11 +00001211 if (!PrintType &&
1212 (!isa<Constant>(V) || V->hasName() || isa<GlobalValue>(V))) {
Dan Gohmand6c0f652009-08-13 15:27:57 +00001213 WriteAsOperandInternal(Out, V, 0, 0);
1214 return;
1215 }
1216
Chris Lattner607dc682002-07-10 16:48:17 +00001217 if (Context == 0) Context = getModuleFromVal(V);
Chris Lattner207b5bc2001-10-29 16:37:48 +00001218
Chris Lattnere9fa33e2009-02-28 23:20:19 +00001219 TypePrinting TypePrinter;
Chris Lattner413fd232009-03-01 00:03:38 +00001220 std::vector<const Type*> NumberedTypes;
1221 AddModuleTypesToPrinter(TypePrinter, NumberedTypes, Context);
Dan Gohman8dae1382008-09-14 17:21:12 +00001222 if (PrintType) {
Chris Lattner0f7364b2009-02-28 21:26:53 +00001223 TypePrinter.print(V->getType(), Out);
Dan Gohman8dae1382008-09-14 17:21:12 +00001224 Out << ' ';
1225 }
Misha Brukmanfd939082005-04-21 23:48:37 +00001226
Dan Gohmand6c0f652009-08-13 15:27:57 +00001227 WriteAsOperandInternal(Out, V, &TypePrinter, 0);
Chris Lattner622f7402001-07-20 19:15:21 +00001228}
1229
Chris Lattnercfb5a202008-08-19 05:06:27 +00001230namespace {
Chris Lattnerd8c2e422001-07-12 23:35:26 +00001231
Chris Lattner007377f2001-09-07 16:36:04 +00001232class AssemblyWriter {
Dan Gohman683e9222009-08-12 17:23:50 +00001233 formatted_raw_ostream &Out;
Chris Lattner0d9574a2008-08-19 04:26:57 +00001234 SlotTracker &Machine;
Chris Lattnerc1824992001-10-29 16:05:51 +00001235 const Module *TheModule;
Chris Lattner9cc34462009-02-28 20:25:14 +00001236 TypePrinting TypePrinter;
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001237 AssemblyAnnotationWriter *AnnotationWriter;
Chris Lattner413fd232009-03-01 00:03:38 +00001238 std::vector<const Type*> NumberedTypes;
Chris Lattner7d05c462009-12-28 20:10:43 +00001239 SmallVector<StringRef, 8> MDNames;
1240
Chris Lattner00950542001-06-06 20:29:01 +00001241public:
Dan Gohman683e9222009-08-12 17:23:50 +00001242 inline AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac,
1243 const Module *M,
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001244 AssemblyAnnotationWriter *AAW)
Devang Patel3168b792009-09-28 18:31:56 +00001245 : Out(o), Machine(Mac), TheModule(M), AnnotationWriter(AAW) {
Chris Lattner413fd232009-03-01 00:03:38 +00001246 AddModuleTypesToPrinter(TypePrinter, NumberedTypes, M);
Chris Lattner7d05c462009-12-28 20:10:43 +00001247 if (M)
Chris Lattner08113472009-12-29 09:01:33 +00001248 M->getMDKindNames(MDNames);
Chris Lattner00950542001-06-06 20:29:01 +00001249 }
1250
Chris Lattner6e6b1802009-12-31 02:13:35 +00001251 void printMDNodeBody(const MDNode *MD);
Chris Lattnerfdb33562009-12-31 01:54:05 +00001252 void printNamedMDNode(const NamedMDNode *NMD);
1253
Chris Lattnerbd72b322009-12-31 02:23:35 +00001254 void printModule(const Module *M);
Chris Lattner00950542001-06-06 20:29:01 +00001255
Chris Lattner2fcfdb72006-12-06 06:24:27 +00001256 void writeOperand(const Value *Op, bool PrintType);
Devang Pateleaf42ab2008-09-23 23:03:40 +00001257 void writeParamOperand(const Value *Operand, Attributes Attrs);
Chris Lattner66e810b2002-04-18 18:53:13 +00001258
Chris Lattner6e6b1802009-12-31 02:13:35 +00001259 void writeAllMDNodes();
1260
Reid Spencer78d033e2007-01-06 07:24:44 +00001261 void printTypeSymbolTable(const TypeSymbolTable &ST);
Chris Lattnerc1824992001-10-29 16:05:51 +00001262 void printGlobal(const GlobalVariable *GV);
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001263 void printAlias(const GlobalAlias *GV);
Chris Lattner79df7c02002-03-26 18:01:55 +00001264 void printFunction(const Function *F);
Devang Pateleaf42ab2008-09-23 23:03:40 +00001265 void printArgument(const Argument *FA, Attributes Attrs);
Chris Lattnerc1824992001-10-29 16:05:51 +00001266 void printBasicBlock(const BasicBlock *BB);
Chris Lattner7e708292002-06-25 16:13:24 +00001267 void printInstruction(const Instruction &I);
Chris Lattner2761e9f2002-04-13 20:53:41 +00001268
Dan Gohman659d1e82010-02-10 20:42:57 +00001269private:
Chris Lattnere02fa852001-10-13 06:42:36 +00001270 // printInfoComment - Print a little comment after the instruction indicating
1271 // which slot it occupies.
Chris Lattner7e708292002-06-25 16:13:24 +00001272 void printInfoComment(const Value &V);
Chris Lattner00950542001-06-06 20:29:01 +00001273};
Chris Lattner413fd232009-03-01 00:03:38 +00001274} // end of anonymous namespace
Chris Lattner00950542001-06-06 20:29:01 +00001275
Chris Lattner2fcfdb72006-12-06 06:24:27 +00001276void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType) {
1277 if (Operand == 0) {
Chris Lattneraab18202005-02-24 16:58:29 +00001278 Out << "<null operand!>";
Chris Lattnerc65b72c2009-12-31 02:33:14 +00001279 return;
Chris Lattneraab18202005-02-24 16:58:29 +00001280 }
Chris Lattnerc65b72c2009-12-31 02:33:14 +00001281 if (PrintType) {
1282 TypePrinter.print(Operand->getType(), Out);
1283 Out << ' ';
1284 }
1285 WriteAsOperandInternal(Out, Operand, &TypePrinter, &Machine);
Chris Lattner00950542001-06-06 20:29:01 +00001286}
1287
Daniel Dunbara279bc32009-09-20 02:20:51 +00001288void AssemblyWriter::writeParamOperand(const Value *Operand,
Devang Pateleaf42ab2008-09-23 23:03:40 +00001289 Attributes Attrs) {
Duncan Sandsdc024672007-11-27 13:23:08 +00001290 if (Operand == 0) {
1291 Out << "<null operand!>";
Chris Lattnerc65b72c2009-12-31 02:33:14 +00001292 return;
Duncan Sandsdc024672007-11-27 13:23:08 +00001293 }
Chris Lattnerc65b72c2009-12-31 02:33:14 +00001294
1295 // Print the type
1296 TypePrinter.print(Operand->getType(), Out);
1297 // Print parameter attributes list
1298 if (Attrs != Attribute::None)
1299 Out << ' ' << Attribute::getAsString(Attrs);
1300 Out << ' ';
1301 // Print the operand
1302 WriteAsOperandInternal(Out, Operand, &TypePrinter, &Machine);
Duncan Sandsdc024672007-11-27 13:23:08 +00001303}
Chris Lattner00950542001-06-06 20:29:01 +00001304
Chris Lattnerc1824992001-10-29 16:05:51 +00001305void AssemblyWriter::printModule(const Module *M) {
Chris Lattner31ab1b32005-03-02 23:12:40 +00001306 if (!M->getModuleIdentifier().empty() &&
Misha Brukmanfd939082005-04-21 23:48:37 +00001307 // Don't print the ID if it will start a new line (which would
Chris Lattner31ab1b32005-03-02 23:12:40 +00001308 // require a comment char before it).
1309 M->getModuleIdentifier().find('\n') == std::string::npos)
1310 Out << "; ModuleID = '" << M->getModuleIdentifier() << "'\n";
1311
Owen Andersoncf7ff2b2006-10-18 02:21:12 +00001312 if (!M->getDataLayout().empty())
Chris Lattnerd2f9e602006-10-22 06:06:56 +00001313 Out << "target datalayout = \"" << M->getDataLayout() << "\"\n";
Reid Spencercddc86f2004-07-25 21:44:54 +00001314 if (!M->getTargetTriple().empty())
Reid Spencerc9a1f0d2004-07-25 21:29:43 +00001315 Out << "target triple = \"" << M->getTargetTriple() << "\"\n";
Misha Brukmanfd939082005-04-21 23:48:37 +00001316
Chris Lattnercc041ba2006-01-24 04:13:11 +00001317 if (!M->getModuleInlineAsm().empty()) {
Chris Lattner42a162e2006-01-24 00:45:30 +00001318 // Split the string into lines, to make it easier to read the .ll file.
Chris Lattnercc041ba2006-01-24 04:13:11 +00001319 std::string Asm = M->getModuleInlineAsm();
Chris Lattner42a162e2006-01-24 00:45:30 +00001320 size_t CurPos = 0;
1321 size_t NewLine = Asm.find_first_of('\n', CurPos);
Dan Gohman9bf0b9b2009-08-12 23:54:22 +00001322 Out << '\n';
Chris Lattner42a162e2006-01-24 00:45:30 +00001323 while (NewLine != std::string::npos) {
1324 // We found a newline, print the portion of the asm string from the
1325 // last newline up to this newline.
1326 Out << "module asm \"";
1327 PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.begin()+NewLine),
1328 Out);
1329 Out << "\"\n";
1330 CurPos = NewLine+1;
1331 NewLine = Asm.find_first_of('\n', CurPos);
1332 }
Chris Lattner71cdba32006-01-24 00:40:17 +00001333 Out << "module asm \"";
Chris Lattner42a162e2006-01-24 00:45:30 +00001334 PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.end()), Out);
Chris Lattner18365502006-01-23 23:03:36 +00001335 Out << "\"\n";
1336 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001337
Chris Lattner44da7d72004-09-14 05:06:58 +00001338 // Loop over the dependent libraries and emit them.
Chris Lattnercfe97b72004-09-14 04:51:44 +00001339 Module::lib_iterator LI = M->lib_begin();
1340 Module::lib_iterator LE = M->lib_end();
Reid Spencercddc86f2004-07-25 21:44:54 +00001341 if (LI != LE) {
Dan Gohman9bf0b9b2009-08-12 23:54:22 +00001342 Out << '\n';
Chris Lattnercfe97b72004-09-14 04:51:44 +00001343 Out << "deplibs = [ ";
1344 while (LI != LE) {
Chris Lattner44da7d72004-09-14 05:06:58 +00001345 Out << '"' << *LI << '"';
Reid Spencerc9a1f0d2004-07-25 21:29:43 +00001346 ++LI;
Chris Lattnercfe97b72004-09-14 04:51:44 +00001347 if (LI != LE)
1348 Out << ", ";
Reid Spencerc9a1f0d2004-07-25 21:29:43 +00001349 }
Dan Gohman9bf0b9b2009-08-12 23:54:22 +00001350 Out << " ]";
Reid Spencer83f6a772004-07-25 18:08:18 +00001351 }
Reid Spencere59eaf42004-09-13 23:44:23 +00001352
Chris Lattner413fd232009-03-01 00:03:38 +00001353 // Loop over the symbol table, emitting all id'd types.
Dan Gohman9bf0b9b2009-08-12 23:54:22 +00001354 if (!M->getTypeSymbolTable().empty() || !NumberedTypes.empty()) Out << '\n';
Reid Spencer78d033e2007-01-06 07:24:44 +00001355 printTypeSymbolTable(M->getTypeSymbolTable());
Misha Brukmanfd939082005-04-21 23:48:37 +00001356
Dan Gohman9bf0b9b2009-08-12 23:54:22 +00001357 // Output all globals.
1358 if (!M->global_empty()) Out << '\n';
Chris Lattnerd6d826c2006-12-06 04:41:52 +00001359 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
1360 I != E; ++I)
Chris Lattner7e708292002-06-25 16:13:24 +00001361 printGlobal(I);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001362
Chris Lattner69dacfc2007-04-26 02:24:10 +00001363 // Output all aliases.
1364 if (!M->alias_empty()) Out << "\n";
1365 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
1366 I != E; ++I)
1367 printAlias(I);
Chris Lattner007377f2001-09-07 16:36:04 +00001368
Chris Lattner44da7d72004-09-14 05:06:58 +00001369 // Output all of the functions.
Chris Lattner7e708292002-06-25 16:13:24 +00001370 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
1371 printFunction(I);
Devang Patel320671d2009-07-08 21:44:25 +00001372
Devang Patel37c4a2d2009-07-29 22:04:47 +00001373 // Output named metadata.
Dan Gohman9bf0b9b2009-08-12 23:54:22 +00001374 if (!M->named_metadata_empty()) Out << '\n';
Chris Lattnerfdb33562009-12-31 01:54:05 +00001375
Devang Patel37c4a2d2009-07-29 22:04:47 +00001376 for (Module::const_named_metadata_iterator I = M->named_metadata_begin(),
Chris Lattner6e6b1802009-12-31 02:13:35 +00001377 E = M->named_metadata_end(); I != E; ++I)
Chris Lattnerfdb33562009-12-31 01:54:05 +00001378 printNamedMDNode(I);
Devang Patel37c4a2d2009-07-29 22:04:47 +00001379
1380 // Output metadata.
Chris Lattner307c9892009-12-31 02:20:11 +00001381 if (!Machine.mdn_empty()) {
Chris Lattner6e6b1802009-12-31 02:13:35 +00001382 Out << '\n';
1383 writeAllMDNodes();
1384 }
Chris Lattner007377f2001-09-07 16:36:04 +00001385}
1386
Chris Lattnerfdb33562009-12-31 01:54:05 +00001387void AssemblyWriter::printNamedMDNode(const NamedMDNode *NMD) {
1388 Out << "!" << NMD->getName() << " = !{";
1389 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1390 if (i) Out << ", ";
Devang Patel69d02e02010-01-05 21:47:32 +00001391 if (MDNode *MD = NMD->getOperand(i))
1392 Out << '!' << Machine.getMetadataSlot(MD);
1393 else
1394 Out << "null";
Chris Lattnerfdb33562009-12-31 01:54:05 +00001395 }
1396 Out << "}\n";
1397}
1398
1399
Dan Gohman683e9222009-08-12 17:23:50 +00001400static void PrintLinkage(GlobalValue::LinkageTypes LT,
1401 formatted_raw_ostream &Out) {
Chris Lattnercfb5a202008-08-19 05:06:27 +00001402 switch (LT) {
Bill Wendling3d10a5a2009-07-20 01:03:30 +00001403 case GlobalValue::ExternalLinkage: break;
1404 case GlobalValue::PrivateLinkage: Out << "private "; break;
1405 case GlobalValue::LinkerPrivateLinkage: Out << "linker_private "; break;
1406 case GlobalValue::InternalLinkage: Out << "internal "; break;
1407 case GlobalValue::LinkOnceAnyLinkage: Out << "linkonce "; break;
1408 case GlobalValue::LinkOnceODRLinkage: Out << "linkonce_odr "; break;
1409 case GlobalValue::WeakAnyLinkage: Out << "weak "; break;
1410 case GlobalValue::WeakODRLinkage: Out << "weak_odr "; break;
1411 case GlobalValue::CommonLinkage: Out << "common "; break;
1412 case GlobalValue::AppendingLinkage: Out << "appending "; break;
1413 case GlobalValue::DLLImportLinkage: Out << "dllimport "; break;
1414 case GlobalValue::DLLExportLinkage: Out << "dllexport "; break;
1415 case GlobalValue::ExternalWeakLinkage: Out << "extern_weak "; break;
Chris Lattner266c7bb2009-04-13 05:44:34 +00001416 case GlobalValue::AvailableExternallyLinkage:
1417 Out << "available_externally ";
1418 break;
Chris Lattnercfb5a202008-08-19 05:06:27 +00001419 }
1420}
Duncan Sands667d4b82009-03-07 15:45:40 +00001421
Chris Lattnercfb5a202008-08-19 05:06:27 +00001422
1423static void PrintVisibility(GlobalValue::VisibilityTypes Vis,
Dan Gohman683e9222009-08-12 17:23:50 +00001424 formatted_raw_ostream &Out) {
Chris Lattnercfb5a202008-08-19 05:06:27 +00001425 switch (Vis) {
Chris Lattnercfb5a202008-08-19 05:06:27 +00001426 case GlobalValue::DefaultVisibility: break;
1427 case GlobalValue::HiddenVisibility: Out << "hidden "; break;
1428 case GlobalValue::ProtectedVisibility: Out << "protected "; break;
1429 }
1430}
1431
Chris Lattnerc1824992001-10-29 16:05:51 +00001432void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Dan Gohman4483c7b2010-01-29 23:12:36 +00001433 if (GV->isMaterializable())
1434 Out << "; Materializable\n";
1435
Dan Gohmand6c0f652009-08-13 15:27:57 +00001436 WriteAsOperandInternal(Out, GV, &TypePrinter, &Machine);
Dan Gohman3845e502009-08-12 23:32:33 +00001437 Out << " = ";
Chris Lattnerd70684f2001-09-18 04:01:05 +00001438
Chris Lattner52b26de2008-08-19 05:16:28 +00001439 if (!GV->hasInitializer() && GV->hasExternalLinkage())
1440 Out << "external ";
Daniel Dunbara279bc32009-09-20 02:20:51 +00001441
Chris Lattner52b26de2008-08-19 05:16:28 +00001442 PrintLinkage(GV->getLinkage(), Out);
1443 PrintVisibility(GV->getVisibility(), Out);
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00001444
1445 if (GV->isThreadLocal()) Out << "thread_local ";
Chris Lattnerdf986172009-01-02 07:01:27 +00001446 if (unsigned AddressSpace = GV->getType()->getAddressSpace())
1447 Out << "addrspace(" << AddressSpace << ") ";
Misha Brukman0313e0b2004-06-21 21:53:56 +00001448 Out << (GV->isConstant() ? "constant " : "global ");
Chris Lattner0f7364b2009-02-28 21:26:53 +00001449 TypePrinter.print(GV->getType()->getElementType(), Out);
Chris Lattnerd70684f2001-09-18 04:01:05 +00001450
Dan Gohman8dae1382008-09-14 17:21:12 +00001451 if (GV->hasInitializer()) {
1452 Out << ' ';
Devang Patel320671d2009-07-08 21:44:25 +00001453 writeOperand(GV->getInitializer(), false);
Dan Gohman8dae1382008-09-14 17:21:12 +00001454 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001455
Chris Lattner60962db2005-11-12 00:10:19 +00001456 if (GV->hasSection())
1457 Out << ", section \"" << GV->getSection() << '"';
1458 if (GV->getAlignment())
Chris Lattner30caa282005-11-06 06:48:53 +00001459 Out << ", align " << GV->getAlignment();
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001460
Chris Lattner7e708292002-06-25 16:13:24 +00001461 printInfoComment(*GV);
Chris Lattnercfb5a202008-08-19 05:06:27 +00001462 Out << '\n';
Chris Lattner70cc3392001-09-10 07:58:01 +00001463}
1464
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001465void AssemblyWriter::printAlias(const GlobalAlias *GA) {
Dan Gohman4483c7b2010-01-29 23:12:36 +00001466 if (GA->isMaterializable())
1467 Out << "; Materializable\n";
1468
Dale Johannesen24f07dc2008-06-03 18:14:29 +00001469 // Don't crash when dumping partially built GA
1470 if (!GA->hasName())
1471 Out << "<<nameless>> = ";
Chris Lattnerc97536e2008-08-17 04:40:13 +00001472 else {
1473 PrintLLVMName(Out, GA);
1474 Out << " = ";
1475 }
Chris Lattnercfb5a202008-08-19 05:06:27 +00001476 PrintVisibility(GA->getVisibility(), Out);
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001477
1478 Out << "alias ";
1479
Chris Lattnercfb5a202008-08-19 05:06:27 +00001480 PrintLinkage(GA->getLinkage(), Out);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001481
Anton Korobeynikovc6c98af2007-04-29 18:02:48 +00001482 const Constant *Aliasee = GA->getAliasee();
Daniel Dunbara279bc32009-09-20 02:20:51 +00001483
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001484 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Aliasee)) {
Chris Lattner0f7364b2009-02-28 21:26:53 +00001485 TypePrinter.print(GV->getType(), Out);
Chris Lattnerc97536e2008-08-17 04:40:13 +00001486 Out << ' ';
1487 PrintLLVMName(Out, GV);
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001488 } else if (const Function *F = dyn_cast<Function>(Aliasee)) {
Chris Lattner0f7364b2009-02-28 21:26:53 +00001489 TypePrinter.print(F->getFunctionType(), Out);
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001490 Out << "* ";
1491
Dan Gohmand6c0f652009-08-13 15:27:57 +00001492 WriteAsOperandInternal(Out, F, &TypePrinter, &Machine);
Anton Korobeynikov591858a2008-03-22 08:17:17 +00001493 } else if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(Aliasee)) {
Chris Lattner0f7364b2009-02-28 21:26:53 +00001494 TypePrinter.print(GA->getType(), Out);
1495 Out << ' ';
Chris Lattnerc97536e2008-08-17 04:40:13 +00001496 PrintLLVMName(Out, GA);
Anton Korobeynikova80e1182007-04-28 13:45:00 +00001497 } else {
Chris Lattnera2165ed2009-04-25 21:23:19 +00001498 const ConstantExpr *CE = cast<ConstantExpr>(Aliasee);
1499 // The only valid GEP is an all zero GEP.
1500 assert((CE->getOpcode() == Instruction::BitCast ||
1501 CE->getOpcode() == Instruction::GetElementPtr) &&
1502 "Unsupported aliasee");
1503 writeOperand(CE, false);
Anton Korobeynikova80e1182007-04-28 13:45:00 +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
Reid Spencer78d033e2007-01-06 07:24:44 +00001510void AssemblyWriter::printTypeSymbolTable(const TypeSymbolTable &ST) {
Chris Lattner413fd232009-03-01 00:03:38 +00001511 // Emit all numbered types.
1512 for (unsigned i = 0, e = NumberedTypes.size(); i != e; ++i) {
Dan Gohman3845e502009-08-12 23:32:33 +00001513 Out << '%' << i << " = type ";
Daniel Dunbara279bc32009-09-20 02:20:51 +00001514
Chris Lattner413fd232009-03-01 00:03:38 +00001515 // Make sure we print out at least one level of the type structure, so
1516 // that we do not get %2 = type %2
1517 TypePrinter.printAtLeastOneLevel(NumberedTypes[i], Out);
Dan Gohman9bf0b9b2009-08-12 23:54:22 +00001518 Out << '\n';
Chris Lattner413fd232009-03-01 00:03:38 +00001519 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001520
Chris Lattner413fd232009-03-01 00:03:38 +00001521 // Print the named types.
Reid Spencer78d033e2007-01-06 07:24:44 +00001522 for (TypeSymbolTable::const_iterator TI = ST.begin(), TE = ST.end();
1523 TI != TE; ++TI) {
Daniel Dunbar03d76512009-07-25 23:55:21 +00001524 PrintLLVMName(Out, TI->first, LocalPrefix);
Chris Lattner52b26de2008-08-19 05:16:28 +00001525 Out << " = type ";
Reid Spencer9231ac82004-05-25 08:53:40 +00001526
1527 // Make sure we print out at least one level of the type structure, so
1528 // that we do not get %FILE = type %FILE
Chris Lattner0f7364b2009-02-28 21:26:53 +00001529 TypePrinter.printAtLeastOneLevel(TI->second, Out);
Chris Lattnercfb5a202008-08-19 05:06:27 +00001530 Out << '\n';
Reid Spencer9231ac82004-05-25 08:53:40 +00001531 }
Reid Spencer78d033e2007-01-06 07:24:44 +00001532}
1533
Misha Brukmanab5c6002004-03-02 00:22:19 +00001534/// printFunction - Print all aspects of a function.
1535///
Chris Lattner7e708292002-06-25 16:13:24 +00001536void AssemblyWriter::printFunction(const Function *F) {
Chris Lattnercfb5a202008-08-19 05:06:27 +00001537 // Print out the return type and name.
1538 Out << '\n';
Chris Lattner4ad02e72003-04-16 20:28:45 +00001539
Misha Brukman0313e0b2004-06-21 21:53:56 +00001540 if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, Out);
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001541
Dan Gohman4483c7b2010-01-29 23:12:36 +00001542 if (F->isMaterializable())
1543 Out << "; Materializable\n";
1544
Reid Spencer5cbf9852007-01-30 20:08:39 +00001545 if (F->isDeclaration())
Chris Lattner3aa60662007-08-19 22:15:26 +00001546 Out << "declare ";
1547 else
Reid Spencerb951bc02006-12-29 20:29:48 +00001548 Out << "define ";
Daniel Dunbara279bc32009-09-20 02:20:51 +00001549
Chris Lattnercfb5a202008-08-19 05:06:27 +00001550 PrintLinkage(F->getLinkage(), Out);
1551 PrintVisibility(F->getVisibility(), Out);
Chris Lattner4ad02e72003-04-16 20:28:45 +00001552
Chris Lattnerd5118982005-05-06 20:26:43 +00001553 // Print the calling convention.
1554 switch (F->getCallingConv()) {
1555 case CallingConv::C: break; // default
Anton Korobeynikovf8248682006-09-20 22:03:51 +00001556 case CallingConv::Fast: Out << "fastcc "; break;
1557 case CallingConv::Cold: Out << "coldcc "; break;
1558 case CallingConv::X86_StdCall: Out << "x86_stdcallcc "; break;
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001559 case CallingConv::X86_FastCall: Out << "x86_fastcallcc "; break;
1560 case CallingConv::ARM_APCS: Out << "arm_apcscc "; break;
1561 case CallingConv::ARM_AAPCS: Out << "arm_aapcscc "; break;
1562 case CallingConv::ARM_AAPCS_VFP:Out << "arm_aapcs_vfpcc "; break;
Anton Korobeynikov211a14e2009-12-07 02:27:35 +00001563 case CallingConv::MSP430_INTR: Out << "msp430_intrcc "; break;
Chris Lattnerd5118982005-05-06 20:26:43 +00001564 default: Out << "cc" << F->getCallingConv() << " "; break;
1565 }
1566
Reid Spencerbd5db8e2006-12-31 05:24:50 +00001567 const FunctionType *FT = F->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00001568 const AttrListPtr &Attrs = F->getAttributes();
Devang Patel652203f2008-09-29 20:49:50 +00001569 Attributes RetAttrs = Attrs.getRetAttributes();
1570 if (RetAttrs != Attribute::None)
1571 Out << Attribute::getAsString(Attrs.getRetAttributes()) << ' ';
Chris Lattner0f7364b2009-02-28 21:26:53 +00001572 TypePrinter.print(F->getReturnType(), Out);
Chris Lattner4667b712008-08-19 05:26:17 +00001573 Out << ' ';
Dan Gohmand6c0f652009-08-13 15:27:57 +00001574 WriteAsOperandInternal(Out, F, &TypePrinter, &Machine);
Misha Brukman0313e0b2004-06-21 21:53:56 +00001575 Out << '(';
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001576 Machine.incorporateFunction(F);
Chris Lattner007377f2001-09-07 16:36:04 +00001577
Chris Lattnerc1824992001-10-29 16:05:51 +00001578 // Loop over the arguments, printing them...
Chris Lattner007377f2001-09-07 16:36:04 +00001579
Reid Spencerbd5db8e2006-12-31 05:24:50 +00001580 unsigned Idx = 1;
Chris Lattner8dcd2f12007-04-18 00:57:22 +00001581 if (!F->isDeclaration()) {
1582 // If this isn't a declaration, print the argument names as well.
1583 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
1584 I != E; ++I) {
1585 // Insert commas as we go... the first arg doesn't get a comma
1586 if (I != F->arg_begin()) Out << ", ";
Devang Patel19c87462008-09-26 22:53:05 +00001587 printArgument(I, Attrs.getParamAttributes(Idx));
Chris Lattner8dcd2f12007-04-18 00:57:22 +00001588 Idx++;
1589 }
1590 } else {
1591 // Otherwise, print the types from the function type.
1592 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
1593 // Insert commas as we go... the first arg doesn't get a comma
1594 if (i) Out << ", ";
Daniel Dunbara279bc32009-09-20 02:20:51 +00001595
Chris Lattner8dcd2f12007-04-18 00:57:22 +00001596 // Output type...
Chris Lattner0f7364b2009-02-28 21:26:53 +00001597 TypePrinter.print(FT->getParamType(i), Out);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001598
Devang Patel19c87462008-09-26 22:53:05 +00001599 Attributes ArgAttrs = Attrs.getParamAttributes(i+1);
Devang Patel05988662008-09-25 21:00:45 +00001600 if (ArgAttrs != Attribute::None)
1601 Out << ' ' << Attribute::getAsString(ArgAttrs);
Chris Lattner8dcd2f12007-04-18 00:57:22 +00001602 }
Reid Spencerbd5db8e2006-12-31 05:24:50 +00001603 }
Chris Lattner007377f2001-09-07 16:36:04 +00001604
1605 // Finish printing arguments...
Chris Lattner7e708292002-06-25 16:13:24 +00001606 if (FT->isVarArg()) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001607 if (FT->getNumParams()) Out << ", ";
1608 Out << "..."; // Output varargs portion of signature!
Chris Lattner007377f2001-09-07 16:36:04 +00001609 }
Misha Brukman0313e0b2004-06-21 21:53:56 +00001610 Out << ')';
Devang Patel19c87462008-09-26 22:53:05 +00001611 Attributes FnAttrs = Attrs.getFnAttributes();
1612 if (FnAttrs != Attribute::None)
1613 Out << ' ' << Attribute::getAsString(Attrs.getFnAttributes());
Chris Lattner60962db2005-11-12 00:10:19 +00001614 if (F->hasSection())
1615 Out << " section \"" << F->getSection() << '"';
Chris Lattner30caa282005-11-06 06:48:53 +00001616 if (F->getAlignment())
1617 Out << " align " << F->getAlignment();
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001618 if (F->hasGC())
1619 Out << " gc \"" << F->getGC() << '"';
Reid Spencer5cbf9852007-01-30 20:08:39 +00001620 if (F->isDeclaration()) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001621 Out << "\n";
Chris Lattner03e2acb2002-05-06 03:00:40 +00001622 } else {
Chris Lattnera1b58582008-04-21 06:12:55 +00001623 Out << " {";
Misha Brukmanfd939082005-04-21 23:48:37 +00001624
Chris Lattnerb5794002002-04-07 22:49:37 +00001625 // Output all of its basic blocks... for the function
Chris Lattner7e708292002-06-25 16:13:24 +00001626 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
1627 printBasicBlock(I);
Chris Lattner007377f2001-09-07 16:36:04 +00001628
Misha Brukman0313e0b2004-06-21 21:53:56 +00001629 Out << "}\n";
Chris Lattner007377f2001-09-07 16:36:04 +00001630 }
1631
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001632 Machine.purgeFunction();
Chris Lattner00950542001-06-06 20:29:01 +00001633}
1634
Misha Brukmanab5c6002004-03-02 00:22:19 +00001635/// printArgument - This member is called for every argument that is passed into
1636/// the function. Simply print it out
1637///
Daniel Dunbara279bc32009-09-20 02:20:51 +00001638void AssemblyWriter::printArgument(const Argument *Arg,
Devang Pateleaf42ab2008-09-23 23:03:40 +00001639 Attributes Attrs) {
Chris Lattner00950542001-06-06 20:29:01 +00001640 // Output type...
Chris Lattner0f7364b2009-02-28 21:26:53 +00001641 TypePrinter.print(Arg->getType(), Out);
Misha Brukmanfd939082005-04-21 23:48:37 +00001642
Duncan Sandsdc024672007-11-27 13:23:08 +00001643 // Output parameter attributes list
Devang Patel05988662008-09-25 21:00:45 +00001644 if (Attrs != Attribute::None)
1645 Out << ' ' << Attribute::getAsString(Attrs);
Reid Spencerbd5db8e2006-12-31 05:24:50 +00001646
Chris Lattner00950542001-06-06 20:29:01 +00001647 // Output name, if available...
Chris Lattnerc97536e2008-08-17 04:40:13 +00001648 if (Arg->hasName()) {
1649 Out << ' ';
1650 PrintLLVMName(Out, Arg);
1651 }
Chris Lattner00950542001-06-06 20:29:01 +00001652}
1653
Misha Brukmanab5c6002004-03-02 00:22:19 +00001654/// printBasicBlock - This member is called for each basic block in a method.
1655///
Chris Lattnerc1824992001-10-29 16:05:51 +00001656void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Nick Lewycky280a6e62008-04-25 16:53:59 +00001657 if (BB->hasName()) { // Print out the label if it exists...
Chris Lattnerc97536e2008-08-17 04:40:13 +00001658 Out << "\n";
Daniel Dunbar03d76512009-07-25 23:55:21 +00001659 PrintLLVMName(Out, BB->getName(), LabelPrefix);
Chris Lattnerc97536e2008-08-17 04:40:13 +00001660 Out << ':';
Nick Lewycky280a6e62008-04-25 16:53:59 +00001661 } else if (!BB->use_empty()) { // Don't print block # of no uses...
Chris Lattner96c5b2f2008-04-21 04:20:33 +00001662 Out << "\n; <label>:";
Chris Lattner22379bc2007-01-11 03:54:27 +00001663 int Slot = Machine.getLocalSlot(BB);
Chris Lattner69566452004-06-09 19:41:19 +00001664 if (Slot != -1)
Misha Brukman0313e0b2004-06-21 21:53:56 +00001665 Out << Slot;
Chris Lattner69566452004-06-09 19:41:19 +00001666 else
Misha Brukman0313e0b2004-06-21 21:53:56 +00001667 Out << "<badref>";
Chris Lattner061269b2002-10-02 19:38:55 +00001668 }
Chris Lattner4e4d8622003-11-20 00:09:43 +00001669
Dan Gohman683e9222009-08-12 17:23:50 +00001670 if (BB->getParent() == 0) {
Chris Lattner8f4b1ec2009-08-17 15:48:08 +00001671 Out.PadToColumn(50);
Dan Gohman683e9222009-08-12 17:23:50 +00001672 Out << "; Error: Block without parent!";
1673 } else if (BB != &BB->getParent()->getEntryBlock()) { // Not the entry block?
Chris Lattnereb411292008-04-22 02:45:44 +00001674 // Output predecessors for the block...
Chris Lattner8f4b1ec2009-08-17 15:48:08 +00001675 Out.PadToColumn(50);
Dan Gohman683e9222009-08-12 17:23:50 +00001676 Out << ";";
Chris Lattnereb411292008-04-22 02:45:44 +00001677 pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001678
Chris Lattnereb411292008-04-22 02:45:44 +00001679 if (PI == PE) {
1680 Out << " No predecessors!";
1681 } else {
Dan Gohman8dae1382008-09-14 17:21:12 +00001682 Out << " preds = ";
Chris Lattnereb411292008-04-22 02:45:44 +00001683 writeOperand(*PI, false);
1684 for (++PI; PI != PE; ++PI) {
Dan Gohman8dae1382008-09-14 17:21:12 +00001685 Out << ", ";
Chris Lattner2fcfdb72006-12-06 06:24:27 +00001686 writeOperand(*PI, false);
Chris Lattner40efcec2003-11-16 22:59:57 +00001687 }
Chris Lattner061269b2002-10-02 19:38:55 +00001688 }
Chris Lattner00950542001-06-06 20:29:01 +00001689 }
Misha Brukmanfd939082005-04-21 23:48:37 +00001690
Chris Lattnereb411292008-04-22 02:45:44 +00001691 Out << "\n";
Chris Lattner00950542001-06-06 20:29:01 +00001692
Misha Brukman0313e0b2004-06-21 21:53:56 +00001693 if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, Out);
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001694
Chris Lattner007377f2001-09-07 16:36:04 +00001695 // Output all of the instructions in the basic block...
Dan Gohmanbeca6892009-07-13 18:27:59 +00001696 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
Chris Lattner7e708292002-06-25 16:13:24 +00001697 printInstruction(*I);
Dan Gohmanbeca6892009-07-13 18:27:59 +00001698 Out << '\n';
1699 }
Chris Lattner9f717ef2004-03-08 18:51:45 +00001700
Misha Brukman0313e0b2004-06-21 21:53:56 +00001701 if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, Out);
Chris Lattner00950542001-06-06 20:29:01 +00001702}
1703
Misha Brukmanab5c6002004-03-02 00:22:19 +00001704/// printInfoComment - Print a little comment after the instruction indicating
1705/// which slot it occupies.
1706///
Chris Lattner7e708292002-06-25 16:13:24 +00001707void AssemblyWriter::printInfoComment(const Value &V) {
Dan Gohman7a5666e2010-02-10 20:41:46 +00001708 if (AnnotationWriter) {
1709 AnnotationWriter->printInfoComment(V, Out);
1710 return;
1711 }
1712
Chris Lattner4ee93c42009-12-29 07:25:48 +00001713 if (V.getType()->isVoidTy()) return;
1714
1715 Out.PadToColumn(50);
1716 Out << "; <";
1717 TypePrinter.print(V.getType(), Out);
1718 Out << "> [#uses=" << V.getNumUses() << ']'; // Output # uses
Chris Lattnere02fa852001-10-13 06:42:36 +00001719}
1720
Reid Spencer3a9ec242006-08-28 01:02:49 +00001721// This member is called for each Instruction in a function..
Chris Lattner7e708292002-06-25 16:13:24 +00001722void AssemblyWriter::printInstruction(const Instruction &I) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001723 if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, Out);
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001724
Dan Gohman3845e502009-08-12 23:32:33 +00001725 // Print out indentation for an instruction.
Dan Gohman01889ca2009-08-13 01:41:52 +00001726 Out << " ";
Chris Lattner00950542001-06-06 20:29:01 +00001727
1728 // Print out name if it exists...
Chris Lattnerc97536e2008-08-17 04:40:13 +00001729 if (I.hasName()) {
1730 PrintLLVMName(Out, &I);
1731 Out << " = ";
Chris Lattner4ee93c42009-12-29 07:25:48 +00001732 } else if (!I.getType()->isVoidTy()) {
Chris Lattner828db8a2008-08-29 17:19:30 +00001733 // Print out the def slot taken.
1734 int SlotNum = Machine.getLocalSlot(&I);
1735 if (SlotNum == -1)
1736 Out << "<badref> = ";
1737 else
1738 Out << '%' << SlotNum << " = ";
Chris Lattnerc97536e2008-08-17 04:40:13 +00001739 }
Chris Lattner00950542001-06-06 20:29:01 +00001740
Chris Lattnerddb6db42005-05-06 05:51:46 +00001741 // If this is a volatile load or store, print out the volatile marker.
Chris Lattnere5e475e2003-09-08 17:45:59 +00001742 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) ||
Chris Lattnerddb6db42005-05-06 05:51:46 +00001743 (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile())) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001744 Out << "volatile ";
Chris Lattnerddb6db42005-05-06 05:51:46 +00001745 } else if (isa<CallInst>(I) && cast<CallInst>(I).isTailCall()) {
1746 // If this is a call, check if it's a tail call.
1747 Out << "tail ";
1748 }
Chris Lattnere5e475e2003-09-08 17:45:59 +00001749
Chris Lattner00950542001-06-06 20:29:01 +00001750 // Print out the opcode...
Misha Brukman0313e0b2004-06-21 21:53:56 +00001751 Out << I.getOpcodeName();
Chris Lattner00950542001-06-06 20:29:01 +00001752
Dan Gohman59858cf2009-07-27 16:11:46 +00001753 // Print out optimization information.
1754 WriteOptimizationInfo(Out, &I);
1755
Reid Spencer74f16422006-12-03 06:27:29 +00001756 // Print out the compare instruction predicates
Nate Begemanac80ade2008-05-12 19:01:56 +00001757 if (const CmpInst *CI = dyn_cast<CmpInst>(&I))
Chris Lattnerab49ee72008-08-23 22:52:27 +00001758 Out << ' ' << getPredicateText(CI->getPredicate());
Reid Spencer74f16422006-12-03 06:27:29 +00001759
Chris Lattner00950542001-06-06 20:29:01 +00001760 // Print out the type of the operands...
Chris Lattner7e708292002-06-25 16:13:24 +00001761 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0;
Chris Lattner00950542001-06-06 20:29:01 +00001762
1763 // Special case conditional branches to swizzle the condition out to the front
Gabor Greifccd27fb2009-02-09 15:45:06 +00001764 if (isa<BranchInst>(I) && cast<BranchInst>(I).isConditional()) {
1765 BranchInst &BI(cast<BranchInst>(I));
Dan Gohman8dae1382008-09-14 17:21:12 +00001766 Out << ' ';
Gabor Greifccd27fb2009-02-09 15:45:06 +00001767 writeOperand(BI.getCondition(), true);
Dan Gohman8dae1382008-09-14 17:21:12 +00001768 Out << ", ";
Gabor Greifccd27fb2009-02-09 15:45:06 +00001769 writeOperand(BI.getSuccessor(0), true);
Dan Gohman8dae1382008-09-14 17:21:12 +00001770 Out << ", ";
Gabor Greifccd27fb2009-02-09 15:45:06 +00001771 writeOperand(BI.getSuccessor(1), true);
Chris Lattner00950542001-06-06 20:29:01 +00001772
Chris Lattner94dc1f22002-04-13 18:34:38 +00001773 } else if (isa<SwitchInst>(I)) {
Chris Lattnerf9be95f2009-10-27 19:13:16 +00001774 // Special case switch instruction to get formatting nice and correct.
Dan Gohman8dae1382008-09-14 17:21:12 +00001775 Out << ' ';
Chris Lattnerab49ee72008-08-23 22:52:27 +00001776 writeOperand(Operand , true);
Dan Gohman8dae1382008-09-14 17:21:12 +00001777 Out << ", ";
Chris Lattnerab49ee72008-08-23 22:52:27 +00001778 writeOperand(I.getOperand(1), true);
1779 Out << " [";
Chris Lattner00950542001-06-06 20:29:01 +00001780
Chris Lattner7e708292002-06-25 16:13:24 +00001781 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; op += 2) {
Dan Gohman01889ca2009-08-13 01:41:52 +00001782 Out << "\n ";
Chris Lattnerab49ee72008-08-23 22:52:27 +00001783 writeOperand(I.getOperand(op ), true);
Dan Gohman8dae1382008-09-14 17:21:12 +00001784 Out << ", ";
Chris Lattner7e708292002-06-25 16:13:24 +00001785 writeOperand(I.getOperand(op+1), true);
Chris Lattner00950542001-06-06 20:29:01 +00001786 }
Dan Gohman01889ca2009-08-13 01:41:52 +00001787 Out << "\n ]";
Chris Lattnerab21db72009-10-28 00:19:10 +00001788 } else if (isa<IndirectBrInst>(I)) {
1789 // Special case indirectbr instruction to get formatting nice and correct.
Chris Lattnerf9be95f2009-10-27 19:13:16 +00001790 Out << ' ';
1791 writeOperand(Operand, true);
Dan Gohman0ed1f422009-10-30 02:01:10 +00001792 Out << ", [";
Chris Lattnerf9be95f2009-10-27 19:13:16 +00001793
1794 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) {
1795 if (i != 1)
1796 Out << ", ";
1797 writeOperand(I.getOperand(i), true);
1798 }
1799 Out << ']';
Chris Lattnerb00c5822001-10-02 03:41:24 +00001800 } else if (isa<PHINode>(I)) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001801 Out << ' ';
Chris Lattner0f7364b2009-02-28 21:26:53 +00001802 TypePrinter.print(I.getType(), Out);
Misha Brukman0313e0b2004-06-21 21:53:56 +00001803 Out << ' ';
Chris Lattner00950542001-06-06 20:29:01 +00001804
Chris Lattner7e708292002-06-25 16:13:24 +00001805 for (unsigned op = 0, Eop = I.getNumOperands(); op < Eop; op += 2) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001806 if (op) Out << ", ";
Dan Gohman8dae1382008-09-14 17:21:12 +00001807 Out << "[ ";
1808 writeOperand(I.getOperand(op ), false); Out << ", ";
Misha Brukman0313e0b2004-06-21 21:53:56 +00001809 writeOperand(I.getOperand(op+1), false); Out << " ]";
Chris Lattnerc24d2082001-06-11 15:04:20 +00001810 }
Dan Gohman995be7d2008-05-31 19:12:39 +00001811 } else if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(&I)) {
Dan Gohman8dae1382008-09-14 17:21:12 +00001812 Out << ' ';
Dan Gohman995be7d2008-05-31 19:12:39 +00001813 writeOperand(I.getOperand(0), true);
1814 for (const unsigned *i = EVI->idx_begin(), *e = EVI->idx_end(); i != e; ++i)
1815 Out << ", " << *i;
1816 } else if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(&I)) {
Dan Gohman8dae1382008-09-14 17:21:12 +00001817 Out << ' ';
1818 writeOperand(I.getOperand(0), true); Out << ", ";
Dan Gohman995be7d2008-05-31 19:12:39 +00001819 writeOperand(I.getOperand(1), true);
1820 for (const unsigned *i = IVI->idx_begin(), *e = IVI->idx_end(); i != e; ++i)
1821 Out << ", " << *i;
Devang Patel57ef4f42008-02-23 00:35:18 +00001822 } else if (isa<ReturnInst>(I) && !Operand) {
1823 Out << " void";
Chris Lattnerd5118982005-05-06 20:26:43 +00001824 } else if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
1825 // Print the calling convention being used.
1826 switch (CI->getCallingConv()) {
1827 case CallingConv::C: break; // default
Chris Lattner0deaab82006-05-19 21:58:52 +00001828 case CallingConv::Fast: Out << " fastcc"; break;
1829 case CallingConv::Cold: Out << " coldcc"; break;
Chris Lattnerb28a6dc2007-11-18 18:32:16 +00001830 case CallingConv::X86_StdCall: Out << " x86_stdcallcc"; break;
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001831 case CallingConv::X86_FastCall: Out << " x86_fastcallcc"; break;
1832 case CallingConv::ARM_APCS: Out << " arm_apcscc "; break;
1833 case CallingConv::ARM_AAPCS: Out << " arm_aapcscc "; break;
1834 case CallingConv::ARM_AAPCS_VFP:Out << " arm_aapcs_vfpcc "; break;
Anton Korobeynikov211a14e2009-12-07 02:27:35 +00001835 case CallingConv::MSP430_INTR: Out << " msp430_intrcc "; break;
Chris Lattnerd5118982005-05-06 20:26:43 +00001836 default: Out << " cc" << CI->getCallingConv(); break;
1837 }
1838
Reid Spencerb138a062007-04-09 06:10:42 +00001839 const PointerType *PTy = cast<PointerType>(Operand->getType());
1840 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1841 const Type *RetTy = FTy->getReturnType();
Devang Patel05988662008-09-25 21:00:45 +00001842 const AttrListPtr &PAL = CI->getAttributes();
Chris Lattner268de042001-11-06 21:28:12 +00001843
Devang Patel652203f2008-09-29 20:49:50 +00001844 if (PAL.getRetAttributes() != Attribute::None)
1845 Out << ' ' << Attribute::getAsString(PAL.getRetAttributes());
1846
Chris Lattner7a012292003-08-05 15:34:45 +00001847 // If possible, print out the short form of the call instruction. We can
Chris Lattnerb5794002002-04-07 22:49:37 +00001848 // only do this if the first argument is a pointer to a nonvararg function,
Chris Lattner7a012292003-08-05 15:34:45 +00001849 // and if the return type is not a pointer to a function.
Chris Lattner268de042001-11-06 21:28:12 +00001850 //
Dan Gohman8dae1382008-09-14 17:21:12 +00001851 Out << ' ';
Chris Lattner7a012292003-08-05 15:34:45 +00001852 if (!FTy->isVarArg() &&
Misha Brukmanfd939082005-04-21 23:48:37 +00001853 (!isa<PointerType>(RetTy) ||
Chris Lattnerc1b27182002-07-25 20:58:51 +00001854 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Chris Lattner0f7364b2009-02-28 21:26:53 +00001855 TypePrinter.print(RetTy, Out);
Dan Gohman8dae1382008-09-14 17:21:12 +00001856 Out << ' ';
Chris Lattner268de042001-11-06 21:28:12 +00001857 writeOperand(Operand, false);
1858 } else {
1859 writeOperand(Operand, true);
1860 }
Misha Brukman0313e0b2004-06-21 21:53:56 +00001861 Out << '(';
Reid Spencerbd5db8e2006-12-31 05:24:50 +00001862 for (unsigned op = 1, Eop = I.getNumOperands(); op < Eop; ++op) {
1863 if (op > 1)
Dan Gohman8dae1382008-09-14 17:21:12 +00001864 Out << ", ";
Devang Patel19c87462008-09-26 22:53:05 +00001865 writeParamOperand(I.getOperand(op), PAL.getParamAttributes(op));
Chris Lattner00950542001-06-06 20:29:01 +00001866 }
Dan Gohman8dae1382008-09-14 17:21:12 +00001867 Out << ')';
Devang Patel19c87462008-09-26 22:53:05 +00001868 if (PAL.getFnAttributes() != Attribute::None)
1869 Out << ' ' << Attribute::getAsString(PAL.getFnAttributes());
Chris Lattner7e708292002-06-25 16:13:24 +00001870 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
Reid Spencerb138a062007-04-09 06:10:42 +00001871 const PointerType *PTy = cast<PointerType>(Operand->getType());
1872 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1873 const Type *RetTy = FTy->getReturnType();
Devang Patel05988662008-09-25 21:00:45 +00001874 const AttrListPtr &PAL = II->getAttributes();
Chris Lattner7a012292003-08-05 15:34:45 +00001875
Chris Lattnerd5118982005-05-06 20:26:43 +00001876 // Print the calling convention being used.
1877 switch (II->getCallingConv()) {
1878 case CallingConv::C: break; // default
Chris Lattner0deaab82006-05-19 21:58:52 +00001879 case CallingConv::Fast: Out << " fastcc"; break;
1880 case CallingConv::Cold: Out << " coldcc"; break;
Dan Gohman8dae1382008-09-14 17:21:12 +00001881 case CallingConv::X86_StdCall: Out << " x86_stdcallcc"; break;
1882 case CallingConv::X86_FastCall: Out << " x86_fastcallcc"; break;
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001883 case CallingConv::ARM_APCS: Out << " arm_apcscc "; break;
1884 case CallingConv::ARM_AAPCS: Out << " arm_aapcscc "; break;
1885 case CallingConv::ARM_AAPCS_VFP:Out << " arm_aapcs_vfpcc "; break;
Anton Korobeynikov211a14e2009-12-07 02:27:35 +00001886 case CallingConv::MSP430_INTR: Out << " msp430_intrcc "; break;
Chris Lattnerd5118982005-05-06 20:26:43 +00001887 default: Out << " cc" << II->getCallingConv(); break;
1888 }
1889
Devang Patel652203f2008-09-29 20:49:50 +00001890 if (PAL.getRetAttributes() != Attribute::None)
1891 Out << ' ' << Attribute::getAsString(PAL.getRetAttributes());
1892
Chris Lattner7a012292003-08-05 15:34:45 +00001893 // If possible, print out the short form of the invoke instruction. We can
1894 // only do this if the first argument is a pointer to a nonvararg function,
1895 // and if the return type is not a pointer to a function.
1896 //
Dan Gohman2b6c3d92008-10-15 18:02:08 +00001897 Out << ' ';
Chris Lattner7a012292003-08-05 15:34:45 +00001898 if (!FTy->isVarArg() &&
Misha Brukmanfd939082005-04-21 23:48:37 +00001899 (!isa<PointerType>(RetTy) ||
Chris Lattner7a012292003-08-05 15:34:45 +00001900 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Chris Lattner0f7364b2009-02-28 21:26:53 +00001901 TypePrinter.print(RetTy, Out);
Dan Gohman2b6c3d92008-10-15 18:02:08 +00001902 Out << ' ';
Chris Lattner7a012292003-08-05 15:34:45 +00001903 writeOperand(Operand, false);
1904 } else {
1905 writeOperand(Operand, true);
1906 }
Misha Brukman0313e0b2004-06-21 21:53:56 +00001907 Out << '(';
Gabor Greif03a5f132009-09-03 02:02:59 +00001908 for (unsigned op = 3, Eop = I.getNumOperands(); op < Eop; ++op) {
1909 if (op > 3)
Dan Gohman8dae1382008-09-14 17:21:12 +00001910 Out << ", ";
Gabor Greif03a5f132009-09-03 02:02:59 +00001911 writeParamOperand(I.getOperand(op), PAL.getParamAttributes(op-2));
Chris Lattnere02fa852001-10-13 06:42:36 +00001912 }
1913
Dan Gohman8dae1382008-09-14 17:21:12 +00001914 Out << ')';
Devang Patel19c87462008-09-26 22:53:05 +00001915 if (PAL.getFnAttributes() != Attribute::None)
1916 Out << ' ' << Attribute::getAsString(PAL.getFnAttributes());
1917
Dan Gohman01889ca2009-08-13 01:41:52 +00001918 Out << "\n to ";
Chris Lattnere02fa852001-10-13 06:42:36 +00001919 writeOperand(II->getNormalDest(), true);
Dan Gohman8dae1382008-09-14 17:21:12 +00001920 Out << " unwind ";
Chris Lattneraeb2a1d2004-02-08 21:44:31 +00001921 writeOperand(II->getUnwindDest(), true);
Chris Lattnere02fa852001-10-13 06:42:36 +00001922
Victor Hernandez7b929da2009-10-23 21:09:37 +00001923 } else if (const AllocaInst *AI = dyn_cast<AllocaInst>(&I)) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001924 Out << ' ';
Chris Lattner0f7364b2009-02-28 21:26:53 +00001925 TypePrinter.print(AI->getType()->getElementType(), Out);
Dan Gohman69bff072009-07-31 18:23:24 +00001926 if (!AI->getArraySize() || AI->isArrayAllocation()) {
Dan Gohman8dae1382008-09-14 17:21:12 +00001927 Out << ", ";
Chris Lattner94dc1f22002-04-13 18:34:38 +00001928 writeOperand(AI->getArraySize(), true);
Chris Lattner00950542001-06-06 20:29:01 +00001929 }
Nate Begeman14b05292005-11-05 09:21:28 +00001930 if (AI->getAlignment()) {
Chris Lattner9fad0b92005-11-05 21:20:34 +00001931 Out << ", align " << AI->getAlignment();
Nate Begeman14b05292005-11-05 09:21:28 +00001932 }
Chris Lattnere02fa852001-10-13 06:42:36 +00001933 } else if (isa<CastInst>(I)) {
Dan Gohman8dae1382008-09-14 17:21:12 +00001934 if (Operand) {
1935 Out << ' ';
1936 writeOperand(Operand, true); // Work with broken code
1937 }
Misha Brukman0313e0b2004-06-21 21:53:56 +00001938 Out << " to ";
Chris Lattner0f7364b2009-02-28 21:26:53 +00001939 TypePrinter.print(I.getType(), Out);
Chris Lattner4d45bd02003-10-18 05:57:43 +00001940 } else if (isa<VAArgInst>(I)) {
Dan Gohman8dae1382008-09-14 17:21:12 +00001941 if (Operand) {
1942 Out << ' ';
1943 writeOperand(Operand, true); // Work with broken code
1944 }
Misha Brukman0313e0b2004-06-21 21:53:56 +00001945 Out << ", ";
Chris Lattner0f7364b2009-02-28 21:26:53 +00001946 TypePrinter.print(I.getType(), Out);
1947 } else if (Operand) { // Print the normal way.
Chris Lattner00950542001-06-06 20:29:01 +00001948
Misha Brukmanfd939082005-04-21 23:48:37 +00001949 // PrintAllTypes - Instructions who have operands of all the same type
Chris Lattner00950542001-06-06 20:29:01 +00001950 // omit the type from all but the first operand. If the instruction has
1951 // different type operands (for example br), then they are all printed.
1952 bool PrintAllTypes = false;
1953 const Type *TheType = Operand->getType();
Chris Lattner00950542001-06-06 20:29:01 +00001954
Reid Spencerebe57e32007-02-02 13:54:55 +00001955 // Select, Store and ShuffleVector always print all types.
Devang Patel64947682008-03-04 22:05:14 +00001956 if (isa<SelectInst>(I) || isa<StoreInst>(I) || isa<ShuffleVectorInst>(I)
1957 || isa<ReturnInst>(I)) {
Chris Lattnerffd9bf42003-04-16 20:20:02 +00001958 PrintAllTypes = true;
1959 } else {
1960 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
1961 Operand = I.getOperand(i);
Nuno Lopes6ad2b2a2009-01-15 18:40:57 +00001962 // note that Operand shouldn't be null, but the test helps make dump()
1963 // more tolerant of malformed IR
Nuno Lopesa8c78a92009-01-14 17:51:41 +00001964 if (Operand && Operand->getType() != TheType) {
Chris Lattnerffd9bf42003-04-16 20:20:02 +00001965 PrintAllTypes = true; // We have differing types! Print them all!
1966 break;
1967 }
Chris Lattner00950542001-06-06 20:29:01 +00001968 }
1969 }
Misha Brukmanfd939082005-04-21 23:48:37 +00001970
Chris Lattnerc1824992001-10-29 16:05:51 +00001971 if (!PrintAllTypes) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001972 Out << ' ';
Chris Lattner0f7364b2009-02-28 21:26:53 +00001973 TypePrinter.print(TheType, Out);
Chris Lattnerc1824992001-10-29 16:05:51 +00001974 }
Chris Lattner00950542001-06-06 20:29:01 +00001975
Dan Gohman8dae1382008-09-14 17:21:12 +00001976 Out << ' ';
Chris Lattner7e708292002-06-25 16:13:24 +00001977 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
Dan Gohman8dae1382008-09-14 17:21:12 +00001978 if (i) Out << ", ";
Chris Lattner7e708292002-06-25 16:13:24 +00001979 writeOperand(I.getOperand(i), PrintAllTypes);
Chris Lattner00950542001-06-06 20:29:01 +00001980 }
1981 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001982
Chris Lattner7d05c462009-12-28 20:10:43 +00001983 // Print post operand alignment for load/store.
Christopher Lamb43c7f372007-04-22 19:24:39 +00001984 if (isa<LoadInst>(I) && cast<LoadInst>(I).getAlignment()) {
1985 Out << ", align " << cast<LoadInst>(I).getAlignment();
1986 } else if (isa<StoreInst>(I) && cast<StoreInst>(I).getAlignment()) {
1987 Out << ", align " << cast<StoreInst>(I).getAlignment();
1988 }
Chris Lattner00950542001-06-06 20:29:01 +00001989
Chris Lattner7d05c462009-12-28 20:10:43 +00001990 // Print Metadata info.
Devang Patel7f93f4d2009-10-07 16:37:55 +00001991 if (!MDNames.empty()) {
Chris Lattner3990b122009-12-28 23:41:32 +00001992 SmallVector<std::pair<unsigned, MDNode*>, 4> InstMD;
1993 I.getAllMetadata(InstMD);
1994 for (unsigned i = 0, e = InstMD.size(); i != e; ++i)
1995 Out << ", !" << MDNames[InstMD[i].first]
1996 << " !" << Machine.getMetadataSlot(InstMD[i].second);
Devang Patel7f93f4d2009-10-07 16:37:55 +00001997 }
Chris Lattnere02fa852001-10-13 06:42:36 +00001998 printInfoComment(I);
Chris Lattner00950542001-06-06 20:29:01 +00001999}
2000
Chris Lattner6e6b1802009-12-31 02:13:35 +00002001static void WriteMDNodeComment(const MDNode *Node,
2002 formatted_raw_ostream &Out) {
2003 if (Node->getNumOperands() < 1)
2004 return;
2005 ConstantInt *CI = dyn_cast_or_null<ConstantInt>(Node->getOperand(0));
2006 if (!CI) return;
2007 unsigned Val = CI->getZExtValue();
2008 unsigned Tag = Val & ~LLVMDebugVersionMask;
2009 if (Val < LLVMDebugVersion)
2010 return;
2011
2012 Out.PadToColumn(50);
2013 if (Tag == dwarf::DW_TAG_auto_variable)
2014 Out << "; [ DW_TAG_auto_variable ]";
2015 else if (Tag == dwarf::DW_TAG_arg_variable)
2016 Out << "; [ DW_TAG_arg_variable ]";
2017 else if (Tag == dwarf::DW_TAG_return_variable)
2018 Out << "; [ DW_TAG_return_variable ]";
2019 else if (Tag == dwarf::DW_TAG_vector_type)
2020 Out << "; [ DW_TAG_vector_type ]";
2021 else if (Tag == dwarf::DW_TAG_user_base)
2022 Out << "; [ DW_TAG_user_base ]";
2023 else if (const char *TagName = dwarf::TagString(Tag))
2024 Out << "; [ " << TagName << " ]";
2025}
2026
2027void AssemblyWriter::writeAllMDNodes() {
2028 SmallVector<const MDNode *, 16> Nodes;
Chris Lattner307c9892009-12-31 02:20:11 +00002029 Nodes.resize(Machine.mdn_size());
2030 for (SlotTracker::mdn_iterator I = Machine.mdn_begin(), E = Machine.mdn_end();
2031 I != E; ++I)
Chris Lattner6e6b1802009-12-31 02:13:35 +00002032 Nodes[I->second] = cast<MDNode>(I->first);
2033
2034 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
2035 Out << '!' << i << " = metadata ";
Chris Lattner2b4b1e22009-12-31 02:27:30 +00002036 printMDNodeBody(Nodes[i]);
Chris Lattner6e6b1802009-12-31 02:13:35 +00002037 }
2038}
2039
2040void AssemblyWriter::printMDNodeBody(const MDNode *Node) {
Chris Lattner85b19122009-12-31 02:31:59 +00002041 WriteMDNodeBodyInternal(Out, Node, &TypePrinter, &Machine);
Chris Lattner6e6b1802009-12-31 02:13:35 +00002042 WriteMDNodeComment(Node, Out);
2043 Out << "\n";
2044}
Chris Lattner00950542001-06-06 20:29:01 +00002045
2046//===----------------------------------------------------------------------===//
2047// External Interface declarations
2048//===----------------------------------------------------------------------===//
2049
Dan Gohman683e9222009-08-12 17:23:50 +00002050void Module::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW) const {
Chris Lattner0d9574a2008-08-19 04:26:57 +00002051 SlotTracker SlotTable(this);
Dan Gohman683e9222009-08-12 17:23:50 +00002052 formatted_raw_ostream OS(ROS);
Chris Lattner944fac72008-08-23 22:23:09 +00002053 AssemblyWriter W(OS, SlotTable, this, AAW);
Chris Lattnerbd72b322009-12-31 02:23:35 +00002054 W.printModule(this);
Chris Lattner00950542001-06-06 20:29:01 +00002055}
2056
Chris Lattner6d4306e2009-02-28 21:11:05 +00002057void Type::print(raw_ostream &OS) const {
2058 if (this == 0) {
2059 OS << "<null Type>";
2060 return;
2061 }
Chris Lattnere9fa33e2009-02-28 23:20:19 +00002062 TypePrinting().print(this, OS);
Chris Lattner75cf7cf2002-04-08 22:03:40 +00002063}
2064
Dan Gohman683e9222009-08-12 17:23:50 +00002065void Value::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW) const {
Chris Lattner944fac72008-08-23 22:23:09 +00002066 if (this == 0) {
Dan Gohman1220e102009-08-12 20:56:03 +00002067 ROS << "printing a <null> value\n";
Chris Lattner944fac72008-08-23 22:23:09 +00002068 return;
2069 }
Dan Gohman1220e102009-08-12 20:56:03 +00002070 formatted_raw_ostream OS(ROS);
Chris Lattner944fac72008-08-23 22:23:09 +00002071 if (const Instruction *I = dyn_cast<Instruction>(this)) {
2072 const Function *F = I->getParent() ? I->getParent()->getParent() : 0;
2073 SlotTracker SlotTable(F);
Chris Lattnerdbe85bf2009-12-31 08:23:09 +00002074 AssemblyWriter W(OS, SlotTable, getModuleFromVal(I), AAW);
Chris Lattnerbd72b322009-12-31 02:23:35 +00002075 W.printInstruction(*I);
Chris Lattner944fac72008-08-23 22:23:09 +00002076 } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(this)) {
2077 SlotTracker SlotTable(BB->getParent());
Chris Lattner6e6b1802009-12-31 02:13:35 +00002078 AssemblyWriter W(OS, SlotTable, getModuleFromVal(BB), AAW);
Chris Lattnerbd72b322009-12-31 02:23:35 +00002079 W.printBasicBlock(BB);
Chris Lattner944fac72008-08-23 22:23:09 +00002080 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(this)) {
2081 SlotTracker SlotTable(GV->getParent());
Dan Gohmanba0941f2009-04-20 16:10:33 +00002082 AssemblyWriter W(OS, SlotTable, GV->getParent(), AAW);
Chris Lattnerbd72b322009-12-31 02:23:35 +00002083 if (const GlobalVariable *V = dyn_cast<GlobalVariable>(GV))
2084 W.printGlobal(V);
2085 else if (const Function *F = dyn_cast<Function>(GV))
2086 W.printFunction(F);
2087 else
2088 W.printAlias(cast<GlobalAlias>(GV));
Devang Patelfcd65ae2009-07-01 20:59:15 +00002089 } else if (const MDNode *N = dyn_cast<MDNode>(this)) {
Victor Hernandez8fffff52010-01-20 04:45:57 +00002090 const Function *F = N->getFunction();
Victor Hernandez559588b2010-01-14 01:47:37 +00002091 SlotTracker SlotTable(F);
Victor Hernandez38812df2010-01-18 19:15:57 +00002092 AssemblyWriter W(OS, SlotTable, F ? getModuleFromVal(F) : 0, AAW);
Chris Lattner6e6b1802009-12-31 02:13:35 +00002093 W.printMDNodeBody(N);
Devang Patelc29d5b32009-07-30 00:02:57 +00002094 } else if (const NamedMDNode *N = dyn_cast<NamedMDNode>(this)) {
Chris Lattner38cf02e2009-12-31 01:36:50 +00002095 SlotTracker SlotTable(N->getParent());
Chris Lattnerfdb33562009-12-31 01:54:05 +00002096 AssemblyWriter W(OS, SlotTable, N->getParent(), AAW);
2097 W.printNamedMDNode(N);
Chris Lattner944fac72008-08-23 22:23:09 +00002098 } else if (const Constant *C = dyn_cast<Constant>(this)) {
Chris Lattnere9fa33e2009-02-28 23:20:19 +00002099 TypePrinting TypePrinter;
Chris Lattner0f7364b2009-02-28 21:26:53 +00002100 TypePrinter.print(C->getType(), OS);
Chris Lattner6d4306e2009-02-28 21:11:05 +00002101 OS << ' ';
Chris Lattner9cc34462009-02-28 20:25:14 +00002102 WriteConstantInt(OS, C, TypePrinter, 0);
Chris Lattner4a3d3a52009-12-31 01:41:14 +00002103 } else if (isa<InlineAsm>(this) || isa<MDString>(this) ||
2104 isa<Argument>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00002105 WriteAsOperand(OS, this, true, 0);
2106 } else {
Dan Gohmancd26ec52009-09-23 01:33:16 +00002107 // Otherwise we don't know what it is. Call the virtual function to
2108 // allow a subclass to print itself.
2109 printCustom(OS);
Chris Lattner944fac72008-08-23 22:23:09 +00002110 }
2111}
2112
Dan Gohmancd26ec52009-09-23 01:33:16 +00002113// Value::printCustom - subclasses should override this to implement printing.
2114void Value::printCustom(raw_ostream &OS) const {
2115 llvm_unreachable("Unknown value to print out!");
2116}
2117
Chris Lattner7059e532008-08-25 17:03:15 +00002118// Value::dump - allow easy printing of Values from the debugger.
David Greened865e022010-01-05 01:29:26 +00002119void Value::dump() const { print(dbgs()); dbgs() << '\n'; }
Reid Spencerfa452c02004-05-25 18:14:38 +00002120
Chris Lattner7059e532008-08-25 17:03:15 +00002121// Type::dump - allow easy printing of Types from the debugger.
Chris Lattner795daec2008-10-01 20:16:19 +00002122// This one uses type names from the given context module
2123void Type::dump(const Module *Context) const {
David Greened865e022010-01-05 01:29:26 +00002124 WriteTypeSymbolic(dbgs(), this, Context);
2125 dbgs() << '\n';
Chris Lattner795daec2008-10-01 20:16:19 +00002126}
2127
Chris Lattnerc2871372009-02-28 21:05:51 +00002128// Type::dump - allow easy printing of Types from the debugger.
2129void Type::dump() const { dump(0); }
2130
Chris Lattner7059e532008-08-25 17:03:15 +00002131// Module::dump() - Allow printing of Modules from the debugger.
David Greened865e022010-01-05 01:29:26 +00002132void Module::dump() const { print(dbgs(), 0); }