1 //===-- AsmWriter.cpp - Printing LLVM as an assembly file -----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This library implements the functionality defined in llvm/Assembly/Writer.h
12 // Note that these routines must be extremely tolerant of various errors in the
13 // LLVM code, because it can be used for debugging transformations.
15 //===----------------------------------------------------------------------===//
17 #include "llvm/Assembly/CachedWriter.h"
18 #include "llvm/Assembly/Writer.h"
19 #include "llvm/Assembly/PrintModulePass.h"
20 #include "llvm/Assembly/AsmAnnotationWriter.h"
21 #include "llvm/Constants.h"
22 #include "llvm/DerivedTypes.h"
23 #include "llvm/Instruction.h"
24 #include "llvm/Instructions.h"
25 #include "llvm/Module.h"
26 #include "llvm/SymbolTable.h"
27 #include "llvm/Assembly/Writer.h"
28 #include "llvm/Support/CFG.h"
29 #include "Support/StringExtras.h"
30 #include "Support/STLExtras.h"
36 /// This class provides computation of slot numbers for LLVM Assembly writing.
37 /// @brief LLVM Assembly Writing Slot Computation.
44 /// @brief A mapping of Values to slot numbers
45 typedef std::map<const Value*, unsigned> ValueMap;
46 typedef std::map<const Type*, unsigned> TypeMap;
48 /// @brief A plane with next slot number and ValueMap
50 unsigned next_slot; ///< The next slot number to use
51 ValueMap map; ///< The map of Value* -> unsigned
52 ValuePlane() { next_slot = 0; } ///< Make sure we start at 0
58 TypePlane() { next_slot = 0; }
59 void clear() { map.clear(); next_slot = 0; }
62 /// @brief The map of planes by Type
63 typedef std::map<const Type*, ValuePlane> TypedPlanes;
66 /// @name Constructors
69 /// @brief Construct from a module
70 SlotMachine(const Module *M );
72 /// @brief Construct from a function, starting out in incorp state.
73 SlotMachine(const Function *F );
79 /// Return the slot number of the specified value in it's type
80 /// plane. Its an error to ask for something not in the SlotMachine.
81 /// Its an error to ask for a Type*
82 int getSlot(const Value *V);
83 int getSlot(const Type*Ty);
85 /// Determine if a Value has a slot or not
86 bool hasSlot(const Value* V);
87 bool hasSlot(const Type* Ty);
93 /// If you'd like to deal with a function instead of just a module, use
94 /// this method to get its data into the SlotMachine.
95 void incorporateFunction(const Function *F) { TheFunction = F; }
97 /// After calling incorporateFunction, use this method to remove the
98 /// most recently incorporated function from the SlotMachine. This
99 /// will reset the state of the machine back to just the module contents.
100 void purgeFunction();
103 /// @name Implementation Details
106 /// This function does the actual initialization.
107 inline void initialize();
109 /// Values can be crammed into here at will. If they haven't
110 /// been inserted already, they get inserted, otherwise they are ignored.
111 /// Either way, the slot number for the Value* is returned.
112 unsigned createSlot(const Value *V);
113 unsigned createSlot(const Type* Ty);
115 /// Insert a value into the value table. Return the slot number
116 /// that it now occupies. BadThings(TM) will happen if you insert a
117 /// Value that's already been inserted.
118 unsigned insertValue( const Value *V );
119 unsigned insertValue( const Type* Ty);
121 /// Add all of the module level global variables (and their initializers)
122 /// and function declarations, but not the contents of those functions.
123 void processModule();
125 /// Add all of the functions arguments, basic blocks, and instructions
126 void processFunction();
128 SlotMachine(const SlotMachine &); // DO NOT IMPLEMENT
129 void operator=(const SlotMachine &); // DO NOT IMPLEMENT
136 /// @brief The module for which we are holding slot numbers
137 const Module* TheModule;
139 /// @brief The function for which we are holding slot numbers
140 const Function* TheFunction;
142 /// @brief The TypePlanes map for the module level data
146 /// @brief The TypePlanes map for the function level data
154 } // end namespace llvm
156 static RegisterPass<PrintModulePass>
157 X("printm", "Print module to stderr",PassInfo::Analysis|PassInfo::Optimization);
158 static RegisterPass<PrintFunctionPass>
159 Y("print","Print function to stderr",PassInfo::Analysis|PassInfo::Optimization);
161 static void WriteAsOperandInternal(std::ostream &Out, const Value *V,
163 std::map<const Type *, std::string> &TypeTable,
164 SlotMachine *Machine);
166 static void WriteAsOperandInternal(std::ostream &Out, const Type *T,
168 std::map<const Type *, std::string> &TypeTable,
169 SlotMachine *Machine);
171 static const Module *getModuleFromVal(const Value *V) {
172 if (const Argument *MA = dyn_cast<Argument>(V))
173 return MA->getParent() ? MA->getParent()->getParent() : 0;
174 else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
175 return BB->getParent() ? BB->getParent()->getParent() : 0;
176 else if (const Instruction *I = dyn_cast<Instruction>(V)) {
177 const Function *M = I->getParent() ? I->getParent()->getParent() : 0;
178 return M ? M->getParent() : 0;
179 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
180 return GV->getParent();
184 static SlotMachine *createSlotMachine(const Value *V) {
185 if (const Argument *FA = dyn_cast<Argument>(V)) {
186 return new SlotMachine(FA->getParent());
187 } else if (const Instruction *I = dyn_cast<Instruction>(V)) {
188 return new SlotMachine(I->getParent()->getParent());
189 } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
190 return new SlotMachine(BB->getParent());
191 } else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)){
192 return new SlotMachine(GV->getParent());
193 } else if (const Function *Func = dyn_cast<Function>(V)) {
194 return new SlotMachine(Func);
199 // getLLVMName - Turn the specified string into an 'LLVM name', which is either
200 // prefixed with % (if the string only contains simple characters) or is
201 // surrounded with ""'s (if it has special chars in it).
202 static std::string getLLVMName(const std::string &Name) {
203 assert(!Name.empty() && "Cannot get empty name!");
205 // First character cannot start with a number...
206 if (Name[0] >= '0' && Name[0] <= '9')
207 return "\"" + Name + "\"";
209 // Scan to see if we have any characters that are not on the "white list"
210 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
212 assert(C != '"' && "Illegal character in LLVM value name!");
213 if ((C < 'a' || C > 'z') && (C < 'A' || C > 'Z') && (C < '0' || C > '9') &&
214 C != '-' && C != '.' && C != '_')
215 return "\"" + Name + "\"";
218 // If we get here, then the identifier is legal to use as a "VarID".
223 /// fillTypeNameTable - If the module has a symbol table, take all global types
224 /// and stuff their names into the TypeNames map.
226 static void fillTypeNameTable(const Module *M,
227 std::map<const Type *, std::string> &TypeNames) {
229 const SymbolTable &ST = M->getSymbolTable();
230 SymbolTable::type_const_iterator TI = ST.type_begin();
231 for (; TI != ST.type_end(); ++TI ) {
232 // As a heuristic, don't insert pointer to primitive types, because
233 // they are used too often to have a single useful name.
235 const Type *Ty = cast<Type>(TI->second);
236 if (!isa<PointerType>(Ty) ||
237 !cast<PointerType>(Ty)->getElementType()->isPrimitiveType() ||
238 isa<OpaqueType>(cast<PointerType>(Ty)->getElementType()))
239 TypeNames.insert(std::make_pair(Ty, getLLVMName(TI->first)));
245 static void calcTypeName(const Type *Ty,
246 std::vector<const Type *> &TypeStack,
247 std::map<const Type *, std::string> &TypeNames,
248 std::string & Result){
249 if (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty)) {
250 Result += Ty->getDescription(); // Base case
254 // Check to see if the type is named.
255 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
256 if (I != TypeNames.end()) {
261 if (isa<OpaqueType>(Ty)) {
266 // Check to see if the Type is already on the stack...
267 unsigned Slot = 0, CurSize = TypeStack.size();
268 while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type
270 // This is another base case for the recursion. In this case, we know
271 // that we have looped back to a type that we have previously visited.
272 // Generate the appropriate upreference to handle this.
273 if (Slot < CurSize) {
274 Result += "\\" + utostr(CurSize-Slot); // Here's the upreference
278 TypeStack.push_back(Ty); // Recursive case: Add us to the stack..
280 switch (Ty->getTypeID()) {
281 case Type::FunctionTyID: {
282 const FunctionType *FTy = cast<FunctionType>(Ty);
283 calcTypeName(FTy->getReturnType(), TypeStack, TypeNames, Result);
285 for (FunctionType::param_iterator I = FTy->param_begin(),
286 E = FTy->param_end(); I != E; ++I) {
287 if (I != FTy->param_begin())
289 calcTypeName(*I, TypeStack, TypeNames, Result);
291 if (FTy->isVarArg()) {
292 if (FTy->getNumParams()) Result += ", ";
298 case Type::StructTyID: {
299 const StructType *STy = cast<StructType>(Ty);
301 for (StructType::element_iterator I = STy->element_begin(),
302 E = STy->element_end(); I != E; ++I) {
303 if (I != STy->element_begin())
305 calcTypeName(*I, TypeStack, TypeNames, Result);
310 case Type::PointerTyID:
311 calcTypeName(cast<PointerType>(Ty)->getElementType(),
312 TypeStack, TypeNames, Result);
315 case Type::ArrayTyID: {
316 const ArrayType *ATy = cast<ArrayType>(Ty);
317 Result += "[" + utostr(ATy->getNumElements()) + " x ";
318 calcTypeName(ATy->getElementType(), TypeStack, TypeNames, Result);
322 case Type::OpaqueTyID:
326 Result += "<unrecognized-type>";
329 TypeStack.pop_back(); // Remove self from stack...
334 /// printTypeInt - The internal guts of printing out a type that has a
335 /// potentially named portion.
337 static std::ostream &printTypeInt(std::ostream &Out, const Type *Ty,
338 std::map<const Type *, std::string> &TypeNames) {
339 // Primitive types always print out their description, regardless of whether
340 // they have been named or not.
342 if (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty))
343 return Out << Ty->getDescription();
345 // Check to see if the type is named.
346 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
347 if (I != TypeNames.end()) return Out << I->second;
349 // Otherwise we have a type that has not been named but is a derived type.
350 // Carefully recurse the type hierarchy to print out any contained symbolic
353 std::vector<const Type *> TypeStack;
354 std::string TypeName;
355 calcTypeName(Ty, TypeStack, TypeNames, TypeName);
356 TypeNames.insert(std::make_pair(Ty, TypeName));//Cache type name for later use
357 return (Out << TypeName);
361 /// WriteTypeSymbolic - This attempts to write the specified type as a symbolic
362 /// type, iff there is an entry in the modules symbol table for the specified
363 /// type or one of it's component types. This is slower than a simple x << Type
365 std::ostream &llvm::WriteTypeSymbolic(std::ostream &Out, const Type *Ty,
369 // If they want us to print out a type, attempt to make it symbolic if there
370 // is a symbol table in the module...
372 std::map<const Type *, std::string> TypeNames;
373 fillTypeNameTable(M, TypeNames);
375 return printTypeInt(Out, Ty, TypeNames);
377 return Out << Ty->getDescription();
381 /// @brief Internal constant writer.
382 static void WriteConstantInt(std::ostream &Out, const Constant *CV,
384 std::map<const Type *, std::string> &TypeTable,
385 SlotMachine *Machine) {
386 if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
387 Out << (CB == ConstantBool::True ? "true" : "false");
388 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV)) {
389 Out << CI->getValue();
390 } else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV)) {
391 Out << CI->getValue();
392 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
393 // We would like to output the FP constant value in exponential notation,
394 // but we cannot do this if doing so will lose precision. Check here to
395 // make sure that we only output it in exponential format if we can parse
396 // the value back and get the same value.
398 std::string StrVal = ftostr(CFP->getValue());
400 // Check to make sure that the stringized number is not some string like
401 // "Inf" or NaN, that atof will accept, but the lexer will not. Check that
402 // the string matches the "[-+]?[0-9]" regex.
404 if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
405 ((StrVal[0] == '-' || StrVal[0] == '+') &&
406 (StrVal[1] >= '0' && StrVal[1] <= '9')))
407 // Reparse stringized version!
408 if (atof(StrVal.c_str()) == CFP->getValue()) {
409 Out << StrVal; return;
412 // Otherwise we could not reparse it to exactly the same value, so we must
413 // output the string in hexadecimal format!
415 // Behave nicely in the face of C TBAA rules... see:
416 // http://www.nullstone.com/htmls/category/aliastyp.htm
418 double Val = CFP->getValue();
419 char *Ptr = (char*)&Val;
420 assert(sizeof(double) == sizeof(uint64_t) && sizeof(double) == 8 &&
421 "assuming that double is 64 bits!");
422 Out << "0x" << utohexstr(*(uint64_t*)Ptr);
424 } else if (isa<ConstantAggregateZero>(CV)) {
425 Out << "zeroinitializer";
426 } else if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
427 // As a special case, print the array as a string if it is an array of
428 // ubytes or an array of sbytes with positive values.
430 const Type *ETy = CA->getType()->getElementType();
431 bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy);
433 if (ETy == Type::SByteTy)
434 for (unsigned i = 0; i < CA->getNumOperands(); ++i)
435 if (cast<ConstantSInt>(CA->getOperand(i))->getValue() < 0) {
442 for (unsigned i = 0; i < CA->getNumOperands(); ++i) {
444 (unsigned char)cast<ConstantInt>(CA->getOperand(i))->getRawValue();
446 if (isprint(C) && C != '"' && C != '\\') {
450 << (char) ((C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'))
451 << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
456 } else { // Cannot output in string format...
458 if (CA->getNumOperands()) {
460 printTypeInt(Out, ETy, TypeTable);
461 WriteAsOperandInternal(Out, CA->getOperand(0),
462 PrintName, TypeTable, Machine);
463 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
465 printTypeInt(Out, ETy, TypeTable);
466 WriteAsOperandInternal(Out, CA->getOperand(i), PrintName,
472 } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
474 if (CS->getNumOperands()) {
476 printTypeInt(Out, CS->getOperand(0)->getType(), TypeTable);
478 WriteAsOperandInternal(Out, CS->getOperand(0),
479 PrintName, TypeTable, Machine);
481 for (unsigned i = 1; i < CS->getNumOperands(); i++) {
483 printTypeInt(Out, CS->getOperand(i)->getType(), TypeTable);
485 WriteAsOperandInternal(Out, CS->getOperand(i),
486 PrintName, TypeTable, Machine);
491 } else if (isa<ConstantPointerNull>(CV)) {
494 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
495 Out << CE->getOpcodeName() << " (";
497 for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) {
498 printTypeInt(Out, (*OI)->getType(), TypeTable);
499 WriteAsOperandInternal(Out, *OI, PrintName, TypeTable, Machine);
500 if (OI+1 != CE->op_end())
504 if (CE->getOpcode() == Instruction::Cast) {
506 printTypeInt(Out, CE->getType(), TypeTable);
511 Out << "<placeholder or erroneous Constant>";
516 /// WriteAsOperand - Write the name of the specified value out to the specified
517 /// ostream. This can be useful when you just want to print int %reg126, not
518 /// the whole instruction that generated it.
520 static void WriteAsOperandInternal(std::ostream &Out, const Value *V,
522 std::map<const Type*, std::string> &TypeTable,
523 SlotMachine *Machine) {
525 if ((PrintName || isa<GlobalValue>(V)) && V->hasName())
526 Out << getLLVMName(V->getName());
528 const Constant *CV = dyn_cast<Constant>(V);
529 if (CV && !isa<GlobalValue>(CV))
530 WriteConstantInt(Out, CV, PrintName, TypeTable, Machine);
534 Slot = Machine->getSlot(V);
536 Machine = createSlotMachine(V);
538 Slot = Machine->getSlot(V);
551 /// WriteAsOperand - Write the name of the specified value out to the specified
552 /// ostream. This can be useful when you just want to print int %reg126, not
553 /// the whole instruction that generated it.
555 std::ostream &llvm::WriteAsOperand(std::ostream &Out, const Value *V,
556 bool PrintType, bool PrintName,
557 const Module *Context) {
558 std::map<const Type *, std::string> TypeNames;
559 if (Context == 0) Context = getModuleFromVal(V);
562 fillTypeNameTable(Context, TypeNames);
565 printTypeInt(Out, V->getType(), TypeNames);
567 WriteAsOperandInternal(Out, V, PrintName, TypeNames, 0);
571 /// WriteAsOperandInternal - Write the name of the specified value out to
572 /// the specified ostream. This can be useful when you just want to print
573 /// int %reg126, not the whole instruction that generated it.
575 static void WriteAsOperandInternal(std::ostream &Out, const Type *T,
577 std::map<const Type*, std::string> &TypeTable,
578 SlotMachine *Machine) {
582 Slot = Machine->getSlot(T);
588 Out << T->getDescription();
592 /// WriteAsOperand - Write the name of the specified value out to the specified
593 /// ostream. This can be useful when you just want to print int %reg126, not
594 /// the whole instruction that generated it.
596 std::ostream &llvm::WriteAsOperand(std::ostream &Out, const Type *Ty,
597 bool PrintType, bool PrintName,
598 const Module *Context) {
599 std::map<const Type *, std::string> TypeNames;
600 assert(Context != 0 && "Can't write types as operand without module context");
602 fillTypeNameTable(Context, TypeNames);
605 // printTypeInt(Out, V->getType(), TypeNames);
607 printTypeInt(Out, Ty, TypeNames);
609 WriteAsOperandInternal(Out, Ty, PrintName, TypeNames, 0);
615 class AssemblyWriter {
617 SlotMachine &Machine;
618 const Module *TheModule;
619 std::map<const Type *, std::string> TypeNames;
620 AssemblyAnnotationWriter *AnnotationWriter;
622 inline AssemblyWriter(std::ostream &o, SlotMachine &Mac, const Module *M,
623 AssemblyAnnotationWriter *AAW)
624 : Out(o), Machine(Mac), TheModule(M), AnnotationWriter(AAW) {
626 // If the module has a symbol table, take all global types and stuff their
627 // names into the TypeNames map.
629 fillTypeNameTable(M, TypeNames);
632 inline void write(const Module *M) { printModule(M); }
633 inline void write(const GlobalVariable *G) { printGlobal(G); }
634 inline void write(const Function *F) { printFunction(F); }
635 inline void write(const BasicBlock *BB) { printBasicBlock(BB); }
636 inline void write(const Instruction *I) { printInstruction(*I); }
637 inline void write(const Constant *CPV) { printConstant(CPV); }
638 inline void write(const Type *Ty) { printType(Ty); }
640 void writeOperand(const Value *Op, bool PrintType, bool PrintName = true);
642 const Module* getModule() { return TheModule; }
645 void printModule(const Module *M);
646 void printSymbolTable(const SymbolTable &ST);
647 void printConstant(const Constant *CPV);
648 void printGlobal(const GlobalVariable *GV);
649 void printFunction(const Function *F);
650 void printArgument(const Argument *FA);
651 void printBasicBlock(const BasicBlock *BB);
652 void printInstruction(const Instruction &I);
654 // printType - Go to extreme measures to attempt to print out a short,
655 // symbolic version of a type name.
657 std::ostream &printType(const Type *Ty) {
658 return printTypeInt(Out, Ty, TypeNames);
661 // printTypeAtLeastOneLevel - Print out one level of the possibly complex type
662 // without considering any symbolic types that we may have equal to it.
664 std::ostream &printTypeAtLeastOneLevel(const Type *Ty);
666 // printInfoComment - Print a little comment after the instruction indicating
667 // which slot it occupies.
668 void printInfoComment(const Value &V);
670 } // end of llvm namespace
672 /// printTypeAtLeastOneLevel - Print out one level of the possibly complex type
673 /// without considering any symbolic types that we may have equal to it.
675 std::ostream &AssemblyWriter::printTypeAtLeastOneLevel(const Type *Ty) {
676 if (const FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
677 printType(FTy->getReturnType()) << " (";
678 for (FunctionType::param_iterator I = FTy->param_begin(),
679 E = FTy->param_end(); I != E; ++I) {
680 if (I != FTy->param_begin())
684 if (FTy->isVarArg()) {
685 if (FTy->getNumParams()) Out << ", ";
689 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
691 for (StructType::element_iterator I = STy->element_begin(),
692 E = STy->element_end(); I != E; ++I) {
693 if (I != STy->element_begin())
698 } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
699 printType(PTy->getElementType()) << '*';
700 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
701 Out << '[' << ATy->getNumElements() << " x ";
702 printType(ATy->getElementType()) << ']';
703 } else if (const OpaqueType *OTy = dyn_cast<OpaqueType>(Ty)) {
706 if (!Ty->isPrimitiveType())
707 Out << "<unknown derived type>";
714 void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType,
716 if (PrintType) { Out << ' '; printType(Operand->getType()); }
717 WriteAsOperandInternal(Out, Operand, PrintName, TypeNames, &Machine);
721 void AssemblyWriter::printModule(const Module *M) {
722 switch (M->getEndianness()) {
723 case Module::LittleEndian: Out << "target endian = little\n"; break;
724 case Module::BigEndian: Out << "target endian = big\n"; break;
725 case Module::AnyEndianness: break;
727 switch (M->getPointerSize()) {
728 case Module::Pointer32: Out << "target pointersize = 32\n"; break;
729 case Module::Pointer64: Out << "target pointersize = 64\n"; break;
730 case Module::AnyPointerSize: break;
732 if (!M->getTargetTriple().empty())
733 Out << "target triple = \"" << M->getTargetTriple() << "\"\n";
735 // Loop over the dependent libraries and emit them
736 Module::lib_iterator LI= M->lib_begin();
737 Module::lib_iterator LE= M->lib_end();
739 Out << "deplibs = [\n";
741 Out << "\"" << *LI << "\"";
749 // Loop over the symbol table, emitting all named constants...
750 printSymbolTable(M->getSymbolTable());
752 for (Module::const_giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
755 Out << "\nimplementation ; Functions:\n";
757 // Output all of the functions...
758 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
762 void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
763 if (GV->hasName()) Out << getLLVMName(GV->getName()) << " = ";
765 if (!GV->hasInitializer())
768 switch (GV->getLinkage()) {
769 case GlobalValue::InternalLinkage: Out << "internal "; break;
770 case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break;
771 case GlobalValue::WeakLinkage: Out << "weak "; break;
772 case GlobalValue::AppendingLinkage: Out << "appending "; break;
773 case GlobalValue::ExternalLinkage: break;
776 Out << (GV->isConstant() ? "constant " : "global ");
777 printType(GV->getType()->getElementType());
779 if (GV->hasInitializer()) {
780 Constant* C = cast<Constant>(GV->getInitializer());
781 assert(C && "GlobalVar initializer isn't constant?");
782 writeOperand(GV->getInitializer(), false, isa<GlobalValue>(C));
785 printInfoComment(*GV);
790 // printSymbolTable - Run through symbol table looking for constants
791 // and types. Emit their declarations.
792 void AssemblyWriter::printSymbolTable(const SymbolTable &ST) {
795 for (SymbolTable::type_const_iterator TI = ST.type_begin();
796 TI != ST.type_end(); ++TI ) {
797 Out << "\t" << getLLVMName(TI->first) << " = type ";
799 // Make sure we print out at least one level of the type structure, so
800 // that we do not get %FILE = type %FILE
802 printTypeAtLeastOneLevel(TI->second) << "\n";
805 // Print the constants, in type plane order.
806 for (SymbolTable::plane_const_iterator PI = ST.plane_begin();
807 PI != ST.plane_end(); ++PI ) {
808 SymbolTable::value_const_iterator VI = ST.value_begin(PI->first);
809 SymbolTable::value_const_iterator VE = ST.value_end(PI->first);
811 for (; VI != VE; ++VI) {
812 const Value* V = VI->second;
813 const Constant *CPV = dyn_cast<Constant>(V) ;
814 if (CPV && !isa<GlobalValue>(V)) {
822 /// printConstant - Print out a constant pool entry...
824 void AssemblyWriter::printConstant(const Constant *CPV) {
825 // Don't print out unnamed constants, they will be inlined
826 if (!CPV->hasName()) return;
829 Out << "\t" << getLLVMName(CPV->getName()) << " =";
831 // Write the value out now...
832 writeOperand(CPV, true, false);
834 printInfoComment(*CPV);
838 /// printFunction - Print all aspects of a function.
840 void AssemblyWriter::printFunction(const Function *F) {
841 // Print out the return type and name...
844 if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, Out);
849 switch (F->getLinkage()) {
850 case GlobalValue::InternalLinkage: Out << "internal "; break;
851 case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break;
852 case GlobalValue::WeakLinkage: Out << "weak "; break;
853 case GlobalValue::AppendingLinkage: Out << "appending "; break;
854 case GlobalValue::ExternalLinkage: break;
857 printType(F->getReturnType()) << ' ';
858 if (!F->getName().empty())
859 Out << getLLVMName(F->getName());
863 Machine.incorporateFunction(F);
865 // Loop over the arguments, printing them...
866 const FunctionType *FT = F->getFunctionType();
868 for(Function::const_aiterator I = F->abegin(), E = F->aend(); I != E; ++I)
871 // Finish printing arguments...
872 if (FT->isVarArg()) {
873 if (FT->getNumParams()) Out << ", ";
874 Out << "..."; // Output varargs portion of signature!
878 if (F->isExternal()) {
883 // Output all of its basic blocks... for the function
884 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
890 Machine.purgeFunction();
893 /// printArgument - This member is called for every argument that is passed into
894 /// the function. Simply print it out
896 void AssemblyWriter::printArgument(const Argument *Arg) {
897 // Insert commas as we go... the first arg doesn't get a comma
898 if (Arg != &Arg->getParent()->afront()) Out << ", ";
901 printType(Arg->getType());
903 // Output name, if available...
905 Out << ' ' << getLLVMName(Arg->getName());
908 /// printBasicBlock - This member is called for each basic block in a method.
910 void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
911 if (BB->hasName()) { // Print out the label if it exists...
912 Out << "\n" << BB->getName() << ':';
913 } else if (!BB->use_empty()) { // Don't print block # of no uses...
914 Out << "\n; <label>:";
915 int Slot = Machine.getSlot(BB);
922 if (BB->getParent() == 0)
923 Out << "\t\t; Error: Block without parent!";
925 if (BB != &BB->getParent()->front()) { // Not the entry block?
926 // Output predecessors for the block...
928 pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
931 Out << " No predecessors!";
934 writeOperand(*PI, false, true);
935 for (++PI; PI != PE; ++PI) {
937 writeOperand(*PI, false, true);
945 if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, Out);
947 // Output all of the instructions in the basic block...
948 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
949 printInstruction(*I);
951 if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, Out);
955 /// printInfoComment - Print a little comment after the instruction indicating
956 /// which slot it occupies.
958 void AssemblyWriter::printInfoComment(const Value &V) {
959 if (V.getType() != Type::VoidTy) {
961 printType(V.getType()) << '>';
964 int SlotNum = Machine.getSlot(&V);
968 Out << ':' << SlotNum; // Print out the def slot taken.
970 Out << " [#uses=" << V.use_size() << ']'; // Output # uses
974 /// printInstruction - This member is called for each Instruction in a function..
976 void AssemblyWriter::printInstruction(const Instruction &I) {
977 if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, Out);
981 // Print out name if it exists...
983 Out << getLLVMName(I.getName()) << " = ";
985 // If this is a volatile load or store, print out the volatile marker
986 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) ||
987 (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile()))
990 // Print out the opcode...
991 Out << I.getOpcodeName();
993 // Print out the type of the operands...
994 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0;
996 // Special case conditional branches to swizzle the condition out to the front
997 if (isa<BranchInst>(I) && I.getNumOperands() > 1) {
998 writeOperand(I.getOperand(2), true);
1000 writeOperand(Operand, true);
1002 writeOperand(I.getOperand(1), true);
1004 } else if (isa<SwitchInst>(I)) {
1005 // Special case switch statement to get formatting nice and correct...
1006 writeOperand(Operand , true); Out << ',';
1007 writeOperand(I.getOperand(1), true); Out << " [";
1009 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; op += 2) {
1011 writeOperand(I.getOperand(op ), true); Out << ',';
1012 writeOperand(I.getOperand(op+1), true);
1015 } else if (isa<PHINode>(I)) {
1017 printType(I.getType());
1020 for (unsigned op = 0, Eop = I.getNumOperands(); op < Eop; op += 2) {
1021 if (op) Out << ", ";
1023 writeOperand(I.getOperand(op ), false); Out << ',';
1024 writeOperand(I.getOperand(op+1), false); Out << " ]";
1026 } else if (isa<ReturnInst>(I) && !Operand) {
1028 } else if (isa<CallInst>(I)) {
1029 const PointerType *PTy = cast<PointerType>(Operand->getType());
1030 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1031 const Type *RetTy = FTy->getReturnType();
1033 // If possible, print out the short form of the call instruction. We can
1034 // only do this if the first argument is a pointer to a nonvararg function,
1035 // and if the return type is not a pointer to a function.
1037 if (!FTy->isVarArg() &&
1038 (!isa<PointerType>(RetTy) ||
1039 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
1040 Out << ' '; printType(RetTy);
1041 writeOperand(Operand, false);
1043 writeOperand(Operand, true);
1046 if (I.getNumOperands() > 1) writeOperand(I.getOperand(1), true);
1047 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; ++op) {
1049 writeOperand(I.getOperand(op), true);
1053 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
1054 const PointerType *PTy = cast<PointerType>(Operand->getType());
1055 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1056 const Type *RetTy = FTy->getReturnType();
1058 // If possible, print out the short form of the invoke instruction. We can
1059 // only do this if the first argument is a pointer to a nonvararg function,
1060 // and if the return type is not a pointer to a function.
1062 if (!FTy->isVarArg() &&
1063 (!isa<PointerType>(RetTy) ||
1064 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
1065 Out << ' '; printType(RetTy);
1066 writeOperand(Operand, false);
1068 writeOperand(Operand, true);
1072 if (I.getNumOperands() > 3) writeOperand(I.getOperand(3), true);
1073 for (unsigned op = 4, Eop = I.getNumOperands(); op < Eop; ++op) {
1075 writeOperand(I.getOperand(op), true);
1078 Out << " )\n\t\t\tto";
1079 writeOperand(II->getNormalDest(), true);
1081 writeOperand(II->getUnwindDest(), true);
1083 } else if (const AllocationInst *AI = dyn_cast<AllocationInst>(&I)) {
1085 printType(AI->getType()->getElementType());
1086 if (AI->isArrayAllocation()) {
1088 writeOperand(AI->getArraySize(), true);
1090 } else if (isa<CastInst>(I)) {
1091 if (Operand) writeOperand(Operand, true); // Work with broken code
1093 printType(I.getType());
1094 } else if (isa<VAArgInst>(I)) {
1095 if (Operand) writeOperand(Operand, true); // Work with broken code
1097 printType(I.getType());
1098 } else if (const VANextInst *VAN = dyn_cast<VANextInst>(&I)) {
1099 if (Operand) writeOperand(Operand, true); // Work with broken code
1101 printType(VAN->getArgType());
1102 } else if (Operand) { // Print the normal way...
1104 // PrintAllTypes - Instructions who have operands of all the same type
1105 // omit the type from all but the first operand. If the instruction has
1106 // different type operands (for example br), then they are all printed.
1107 bool PrintAllTypes = false;
1108 const Type *TheType = Operand->getType();
1110 // Shift Left & Right print both types even for Ubyte LHS, and select prints
1111 // types even if all operands are bools.
1112 if (isa<ShiftInst>(I) || isa<SelectInst>(I)) {
1113 PrintAllTypes = true;
1115 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
1116 Operand = I.getOperand(i);
1117 if (Operand->getType() != TheType) {
1118 PrintAllTypes = true; // We have differing types! Print them all!
1124 if (!PrintAllTypes) {
1129 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
1131 writeOperand(I.getOperand(i), PrintAllTypes);
1135 printInfoComment(I);
1140 //===----------------------------------------------------------------------===//
1141 // External Interface declarations
1142 //===----------------------------------------------------------------------===//
1144 void Module::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
1145 SlotMachine SlotTable(this);
1146 AssemblyWriter W(o, SlotTable, this, AAW);
1150 void GlobalVariable::print(std::ostream &o) const {
1151 SlotMachine SlotTable(getParent());
1152 AssemblyWriter W(o, SlotTable, getParent(), 0);
1156 void Function::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
1157 SlotMachine SlotTable(getParent());
1158 AssemblyWriter W(o, SlotTable, getParent(), AAW);
1163 void BasicBlock::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
1164 SlotMachine SlotTable(getParent());
1165 AssemblyWriter W(o, SlotTable,
1166 getParent() ? getParent()->getParent() : 0, AAW);
1170 void Instruction::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
1171 const Function *F = getParent() ? getParent()->getParent() : 0;
1172 SlotMachine SlotTable(F);
1173 AssemblyWriter W(o, SlotTable, F ? F->getParent() : 0, AAW);
1178 void Constant::print(std::ostream &o) const {
1179 if (this == 0) { o << "<null> constant value\n"; return; }
1181 o << ' ' << getType()->getDescription() << ' ';
1183 std::map<const Type *, std::string> TypeTable;
1184 WriteConstantInt(o, this, false, TypeTable, 0);
1187 void Type::print(std::ostream &o) const {
1191 o << getDescription();
1194 void Argument::print(std::ostream &o) const {
1195 WriteAsOperand(o, this, true, true,
1196 getParent() ? getParent()->getParent() : 0);
1199 // Value::dump - allow easy printing of Values from the debugger.
1200 // Located here because so much of the needed functionality is here.
1201 void Value::dump() const { print(std::cerr); }
1203 // Type::dump - allow easy printing of Values from the debugger.
1204 // Located here because so much of the needed functionality is here.
1205 void Type::dump() const { print(std::cerr); }
1207 //===----------------------------------------------------------------------===//
1208 // CachedWriter Class Implementation
1209 //===----------------------------------------------------------------------===//
1211 void CachedWriter::setModule(const Module *M) {
1212 delete SC; delete AW;
1214 SC = new SlotMachine(M );
1215 AW = new AssemblyWriter(Out, *SC, M, 0);
1221 CachedWriter::~CachedWriter() {
1226 CachedWriter &CachedWriter::operator<<(const Value &V) {
1227 assert(AW && SC && "CachedWriter does not have a current module!");
1228 if (const Instruction *I = dyn_cast<Instruction>(&V))
1230 else if (const BasicBlock *BB = dyn_cast<BasicBlock>(&V))
1232 else if (const Function *F = dyn_cast<Function>(&V))
1234 else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(&V))
1237 AW->writeOperand(&V, true, true);
1241 CachedWriter& CachedWriter::operator<<(const Type &Ty) {
1242 if (SymbolicTypes) {
1243 const Module *M = AW->getModule();
1244 if (M) WriteTypeSymbolic(Out, &Ty, M);
1251 //===----------------------------------------------------------------------===//
1252 //===-- SlotMachine Implementation
1253 //===----------------------------------------------------------------------===//
1256 #define SC_DEBUG(X) std::cerr << X
1261 // Module level constructor. Causes the contents of the Module (sans functions)
1262 // to be added to the slot table.
1263 SlotMachine::SlotMachine(const Module *M)
1264 : TheModule(M) ///< Saved for lazy initialization.
1273 // Function level constructor. Causes the contents of the Module and the one
1274 // function provided to be added to the slot table.
1275 SlotMachine::SlotMachine(const Function *F )
1276 : TheModule( F ? F->getParent() : 0 ) ///< Saved for lazy initialization
1277 , TheFunction(F) ///< Saved for lazy initialization
1285 inline void SlotMachine::initialize(void) {
1288 TheModule = 0; ///< Prevent re-processing next time we're called.
1290 if ( TheFunction ) {
1295 // Iterate through all the global variables, functions, and global
1296 // variable initializers and create slots for them.
1297 void SlotMachine::processModule() {
1298 SC_DEBUG("begin processModule!\n");
1300 // Add all of the global variables to the value table...
1301 for (Module::const_giterator I = TheModule->gbegin(), E = TheModule->gend();
1305 // Add all the functions to the table
1306 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
1310 SC_DEBUG("end processModule!\n");
1314 // Process the arguments, basic blocks, and instructions of a function.
1315 void SlotMachine::processFunction() {
1316 SC_DEBUG("begin processFunction!\n");
1318 // Add all the function arguments
1319 for(Function::const_aiterator AI = TheFunction->abegin(),
1320 AE = TheFunction->aend(); AI != AE; ++AI)
1323 SC_DEBUG("Inserting Instructions:\n");
1325 // Add all of the basic blocks and instructions
1326 for (Function::const_iterator BB = TheFunction->begin(),
1327 E = TheFunction->end(); BB != E; ++BB) {
1329 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) {
1334 SC_DEBUG("end processFunction!\n");
1337 // Clean up after incorporating a function. This is the only way
1338 // to get out of the function incorporation state that affects the
1339 // getSlot/createSlot lock. Function incorporation state is indicated
1340 // by TheFunction != 0.
1341 void SlotMachine::purgeFunction() {
1342 SC_DEBUG("begin purgeFunction!\n");
1343 fMap.clear(); // Simply discard the function level map
1346 SC_DEBUG("end purgeFunction!\n");
1349 /// Get the slot number for a value. This function will assert if you
1350 /// ask for a Value that hasn't previously been inserted with createSlot.
1351 /// Types are forbidden because Type does not inherit from Value (any more).
1352 int SlotMachine::getSlot(const Value *V) {
1353 assert( V && "Can't get slot for null Value" );
1354 assert(!isa<Constant>(V) || isa<GlobalValue>(V) &&
1355 "Can't insert a non-GlobalValue Constant into SlotMachine");
1357 // Check for uninitialized state and do lazy initialization
1360 // Get the type of the value
1361 const Type* VTy = V->getType();
1363 // Find the type plane in the module map
1364 TypedPlanes::const_iterator MI = mMap.find(VTy);
1366 if ( TheFunction ) {
1367 // Lookup the type in the function map too
1368 TypedPlanes::const_iterator FI = fMap.find(VTy);
1369 // If there is a corresponding type plane in the function map
1370 if ( FI != fMap.end() ) {
1371 // Lookup the Value in the function map
1372 ValueMap::const_iterator FVI = FI->second.map.find(V);
1373 // If the value doesn't exist in the function map
1374 if ( FVI == FI->second.map.end() ) {
1375 // Look up the value in the module map.
1376 if (MI == mMap.end()) return -1;
1377 ValueMap::const_iterator MVI = MI->second.map.find(V);
1378 // If we didn't find it, it wasn't inserted
1379 if (MVI == MI->second.map.end()) return -1;
1380 assert( MVI != MI->second.map.end() && "Value not found");
1381 // We found it only at the module level
1384 // else the value exists in the function map
1386 // Return the slot number as the module's contribution to
1387 // the type plane plus the index in the function's contribution
1388 // to the type plane.
1389 if (MI != mMap.end())
1390 return MI->second.next_slot + FVI->second;
1397 // N.B. Can get here only if either !TheFunction or the function doesn't
1398 // have a corresponding type plane for the Value
1400 // Make sure the type plane exists
1401 if (MI == mMap.end()) return -1;
1402 // Lookup the value in the module's map
1403 ValueMap::const_iterator MVI = MI->second.map.find(V);
1404 // Make sure we found it.
1405 if (MVI == MI->second.map.end()) return -1;
1410 /// Get the slot number for a value. This function will assert if you
1411 /// ask for a Value that hasn't previously been inserted with createSlot.
1412 /// Types are forbidden because Type does not inherit from Value (any more).
1413 int SlotMachine::getSlot(const Type *Ty) {
1414 assert( Ty && "Can't get slot for null Type" );
1416 // Check for uninitialized state and do lazy initialization
1419 if ( TheFunction ) {
1420 // Lookup the Type in the function map
1421 TypeMap::const_iterator FTI = fTypes.map.find(Ty);
1422 // If the Type doesn't exist in the function map
1423 if ( FTI == fTypes.map.end() ) {
1424 TypeMap::const_iterator MTI = mTypes.map.find(Ty);
1425 // If we didn't find it, it wasn't inserted
1426 if (MTI == mTypes.map.end())
1428 // We found it only at the module level
1431 // else the value exists in the function map
1433 // Return the slot number as the module's contribution to
1434 // the type plane plus the index in the function's contribution
1435 // to the type plane.
1436 return mTypes.next_slot + FTI->second;
1440 // N.B. Can get here only if either !TheFunction
1442 // Lookup the value in the module's map
1443 TypeMap::const_iterator MTI = mTypes.map.find(Ty);
1444 // Make sure we found it.
1445 if (MTI == mTypes.map.end()) return -1;
1450 // Create a new slot, or return the existing slot if it is already
1451 // inserted. Note that the logic here parallels getSlot but instead
1452 // of asserting when the Value* isn't found, it inserts the value.
1453 unsigned SlotMachine::createSlot(const Value *V) {
1454 assert( V && "Can't insert a null Value to SlotMachine");
1455 assert(!isa<Constant>(V) || isa<GlobalValue>(V) &&
1456 "Can't insert a non-GlobalValue Constant into SlotMachine");
1458 const Type* VTy = V->getType();
1460 // Just ignore void typed things
1461 if (VTy == Type::VoidTy) return 0; // FIXME: Wrong return value!
1463 // Look up the type plane for the Value's type from the module map
1464 TypedPlanes::const_iterator MI = mMap.find(VTy);
1466 if ( TheFunction ) {
1467 // Get the type plane for the Value's type from the function map
1468 TypedPlanes::const_iterator FI = fMap.find(VTy);
1469 // If there is a corresponding type plane in the function map
1470 if ( FI != fMap.end() ) {
1471 // Lookup the Value in the function map
1472 ValueMap::const_iterator FVI = FI->second.map.find(V);
1473 // If the value doesn't exist in the function map
1474 if ( FVI == FI->second.map.end() ) {
1475 // If there is no corresponding type plane in the module map
1476 if ( MI == mMap.end() )
1477 return insertValue(V);
1478 // Look up the value in the module map
1479 ValueMap::const_iterator MVI = MI->second.map.find(V);
1480 // If we didn't find it, it wasn't inserted
1481 if ( MVI == MI->second.map.end() )
1482 return insertValue(V);
1484 // We found it only at the module level
1487 // else the value exists in the function map
1489 if ( MI == mMap.end() )
1492 // Return the slot number as the module's contribution to
1493 // the type plane plus the index in the function's contribution
1494 // to the type plane.
1495 return MI->second.next_slot + FVI->second;
1498 // else there is not a corresponding type plane in the function map
1500 // If the type plane doesn't exists at the module level
1501 if ( MI == mMap.end() ) {
1502 return insertValue(V);
1503 // else type plane exists at the module level, examine it
1505 // Look up the value in the module's map
1506 ValueMap::const_iterator MVI = MI->second.map.find(V);
1507 // If we didn't find it there either
1508 if ( MVI == MI->second.map.end() )
1509 // Return the slot number as the module's contribution to
1510 // the type plane plus the index of the function map insertion.
1511 return MI->second.next_slot + insertValue(V);
1518 // N.B. Can only get here if !TheFunction
1520 // If the module map's type plane is not for the Value's type
1521 if ( MI != mMap.end() ) {
1522 // Lookup the value in the module's map
1523 ValueMap::const_iterator MVI = MI->second.map.find(V);
1524 if ( MVI != MI->second.map.end() )
1528 return insertValue(V);
1531 // Create a new slot, or return the existing slot if it is already
1532 // inserted. Note that the logic here parallels getSlot but instead
1533 // of asserting when the Value* isn't found, it inserts the value.
1534 unsigned SlotMachine::createSlot(const Type *Ty) {
1535 assert( Ty && "Can't insert a null Type to SlotMachine");
1537 if ( TheFunction ) {
1538 // Lookup the Type in the function map
1539 TypeMap::const_iterator FTI = fTypes.map.find(Ty);
1540 // If the type doesn't exist in the function map
1541 if ( FTI == fTypes.map.end() ) {
1542 // Look up the type in the module map
1543 TypeMap::const_iterator MTI = mTypes.map.find(Ty);
1544 // If we didn't find it, it wasn't inserted
1545 if ( MTI == mTypes.map.end() )
1546 return insertValue(Ty);
1548 // We found it only at the module level
1551 // else the value exists in the function map
1553 // Return the slot number as the module's contribution to
1554 // the type plane plus the index in the function's contribution
1555 // to the type plane.
1556 return mTypes.next_slot + FTI->second;
1560 // N.B. Can only get here if !TheFunction
1562 // Lookup the type in the module's map
1563 TypeMap::const_iterator MTI = mTypes.map.find(Ty);
1564 if ( MTI != mTypes.map.end() )
1567 return insertValue(Ty);
1570 // Low level insert function. Minimal checking is done. This
1571 // function is just for the convenience of createSlot (above).
1572 unsigned SlotMachine::insertValue(const Value *V ) {
1573 assert(V && "Can't insert a null Value into SlotMachine!");
1574 assert(!isa<Constant>(V) || isa<GlobalValue>(V) &&
1575 "Can't insert a non-GlobalValue Constant into SlotMachine");
1577 // If this value does not contribute to a plane (is void)
1578 // or if the value already has a name then ignore it.
1579 if (V->getType() == Type::VoidTy || V->hasName() ) {
1580 SC_DEBUG("ignored value " << *V << "\n");
1581 return 0; // FIXME: Wrong return value
1584 const Type *VTy = V->getType();
1585 unsigned DestSlot = 0;
1587 if ( TheFunction ) {
1588 TypedPlanes::iterator I = fMap.find( VTy );
1589 if ( I == fMap.end() )
1590 I = fMap.insert(std::make_pair(VTy,ValuePlane())).first;
1591 DestSlot = I->second.map[V] = I->second.next_slot++;
1593 TypedPlanes::iterator I = mMap.find( VTy );
1594 if ( I == mMap.end() )
1595 I = mMap.insert(std::make_pair(VTy,ValuePlane())).first;
1596 DestSlot = I->second.map[V] = I->second.next_slot++;
1599 SC_DEBUG(" Inserting value [" << VTy << "] = " << V << " slot=" <<
1601 // G = Global, C = Constant, T = Type, F = Function, o = other
1602 SC_DEBUG((isa<GlobalVariable>(V) ? 'G' : (isa<Function>(V) ? 'F' :
1603 (isa<Constant>(V) ? 'C' : 'o'))));
1608 // Low level insert function. Minimal checking is done. This
1609 // function is just for the convenience of createSlot (above).
1610 unsigned SlotMachine::insertValue(const Type *Ty ) {
1611 assert(Ty && "Can't insert a null Type into SlotMachine!");
1613 unsigned DestSlot = 0;
1615 if ( TheFunction ) {
1616 DestSlot = fTypes.map[Ty] = fTypes.next_slot++;
1618 DestSlot = fTypes.map[Ty] = fTypes.next_slot++;
1620 SC_DEBUG(" Inserting type [" << DestSlot << "] = " << Ty << "\n");