Make LLVM Assembly dramatically easier to read by aligning the comments,

using formatted_raw_ostream's PadToColumn.

Before:

bb1:            ; preds = %bb
  %2 = sext i32 %i.01 to i64            ; <i64> [#uses=1]
  %3 = getelementptr double* %p, i64 %2         ; <double*> [#uses=1]
  %4 = load double* %3, align 8         ; <double> [#uses=1]
  %5 = fmul double %4, 1.100000e+00             ; <double> [#uses=1]
  %6 = sext i32 %i.01 to i64            ; <i64> [#uses=1]
  %7 = getelementptr double* %p, i64 %6         ; <double*> [#uses=1]

After:

bb1:                                        ; preds = %bb
  %2 = sext i32 %i.01 to i64                ; <i64> [#uses=1]
  %3 = getelementptr double* %p, i64 %2     ; <double*> [#uses=1]
  %4 = load double* %3, align 8             ; <double> [#uses=1]
  %5 = fmul double %4, 1.100000e+00         ; <double> [#uses=1]
  %6 = sext i32 %i.01 to i64                ; <i64> [#uses=1]
  %7 = getelementptr double* %p, i64 %6     ; <double*> [#uses=1]

Several tests required whitespace adjustments.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@78816 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Dan Gohman 2009-08-12 17:23:50 +00:00
parent 9f7d60f460
commit 683e922d29
10 changed files with 169 additions and 157 deletions

View File

@ -34,7 +34,7 @@
#include "llvm/Support/CFG.h" #include "llvm/Support/CFG.h"
#include "llvm/Support/ErrorHandling.h" #include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/MathExtras.h" #include "llvm/Support/MathExtras.h"
#include "llvm/Support/raw_ostream.h" #include "llvm/Support/FormattedStream.h"
#include <algorithm> #include <algorithm>
#include <cctype> #include <cctype>
#include <map> #include <map>
@ -66,7 +66,8 @@ static const Module *getModuleFromVal(const Value *V) {
// PrintEscapedString - Print each character of the specified string, escaping // PrintEscapedString - Print each character of the specified string, escaping
// it if it is not printable or if it is an escape char. // it if it is not printable or if it is an escape char.
static void PrintEscapedString(const StringRef &Name, raw_ostream &Out) { static void PrintEscapedString(const StringRef &Name,
formatted_raw_ostream &Out) {
for (unsigned i = 0, e = Name.size(); i != e; ++i) { for (unsigned i = 0, e = Name.size(); i != e; ++i) {
unsigned char C = Name[i]; unsigned char C = Name[i];
if (isprint(C) && C != '\\' && C != '"') if (isprint(C) && C != '\\' && C != '"')
@ -86,7 +87,7 @@ enum PrefixType {
/// PrintLLVMName - Turn the specified name into an 'LLVM name', which is either /// PrintLLVMName - Turn the specified name into an 'LLVM name', which is either
/// prefixed with % (if the string only contains simple characters) or is /// prefixed with % (if the string only contains simple characters) or is
/// surrounded with ""'s (if it has special chars in it). Print it out. /// surrounded with ""'s (if it has special chars in it). Print it out.
static void PrintLLVMName(raw_ostream &OS, const StringRef &Name, static void PrintLLVMName(formatted_raw_ostream &OS, const StringRef &Name,
PrefixType Prefix) { PrefixType Prefix) {
assert(Name.data() && "Cannot get empty name!"); assert(Name.data() && "Cannot get empty name!");
switch (Prefix) { switch (Prefix) {
@ -125,7 +126,7 @@ static void PrintLLVMName(raw_ostream &OS, const StringRef &Name,
/// PrintLLVMName - Turn the specified name into an 'LLVM name', which is either /// PrintLLVMName - Turn the specified name into an 'LLVM name', which is either
/// prefixed with % (if the string only contains simple characters) or is /// prefixed with % (if the string only contains simple characters) or is
/// surrounded with ""'s (if it has special chars in it). Print it out. /// surrounded with ""'s (if it has special chars in it). Print it out.
static void PrintLLVMName(raw_ostream &OS, const Value *V) { static void PrintLLVMName(formatted_raw_ostream &OS, const Value *V) {
PrintLLVMName(OS, V->getName(), PrintLLVMName(OS, V->getName(),
isa<GlobalValue>(V) ? GlobalPrefix : LocalPrefix); isa<GlobalValue>(V) ? GlobalPrefix : LocalPrefix);
} }
@ -425,9 +426,11 @@ static void AddModuleTypesToPrinter(TypePrinting &TP,
// Get the name as a string and insert it into TypeNames. // Get the name as a string and insert it into TypeNames.
std::string NameStr; std::string NameStr;
raw_string_ostream NameOS(NameStr); raw_string_ostream NameROS(NameStr);
formatted_raw_ostream NameOS(NameROS);
PrintLLVMName(NameOS, TI->first, LocalPrefix); PrintLLVMName(NameOS, TI->first, LocalPrefix);
TP.addTypeName(Ty, NameOS.str()); NameOS.flush();
TP.addTypeName(Ty, NameStr);
} }
// Walk the entire module to find references to unnamed structure and opaque // Walk the entire module to find references to unnamed structure and opaque
@ -814,7 +817,7 @@ void SlotTracker::CreateMetadataSlot(const MDNode *N) {
// AsmWriter Implementation // AsmWriter Implementation
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
static void WriteAsOperandInternal(raw_ostream &Out, const Value *V, static void WriteAsOperandInternal(formatted_raw_ostream &Out, const Value *V,
TypePrinting &TypePrinter, TypePrinting &TypePrinter,
SlotTracker *Machine); SlotTracker *Machine);
@ -853,7 +856,7 @@ static const char *getPredicateText(unsigned predicate) {
return pred; return pred;
} }
static void WriteMDNodes(raw_ostream &Out, TypePrinting &TypePrinter, static void WriteMDNodes(formatted_raw_ostream &Out, TypePrinting &TypePrinter,
SlotTracker &Machine) { SlotTracker &Machine) {
SmallVector<const MDNode *, 16> Nodes; SmallVector<const MDNode *, 16> Nodes;
Nodes.resize(Machine.mdnSize()); Nodes.resize(Machine.mdnSize());
@ -886,7 +889,7 @@ static void WriteMDNodes(raw_ostream &Out, TypePrinting &TypePrinter,
} }
} }
static void WriteOptimizationInfo(raw_ostream &Out, const User *U) { static void WriteOptimizationInfo(formatted_raw_ostream &Out, const User *U) {
if (const OverflowingBinaryOperator *OBO = if (const OverflowingBinaryOperator *OBO =
dyn_cast<OverflowingBinaryOperator>(U)) { dyn_cast<OverflowingBinaryOperator>(U)) {
if (OBO->hasNoUnsignedOverflow()) if (OBO->hasNoUnsignedOverflow())
@ -902,7 +905,7 @@ static void WriteOptimizationInfo(raw_ostream &Out, const User *U) {
} }
} }
static void WriteConstantInt(raw_ostream &Out, const Constant *CV, static void WriteConstantInt(formatted_raw_ostream &Out, const Constant *CV,
TypePrinting &TypePrinter, SlotTracker *Machine) { TypePrinting &TypePrinter, SlotTracker *Machine) {
if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) { if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
if (CI->getType() == Type::Int1Ty) { if (CI->getType() == Type::Int1Ty) {
@ -1143,7 +1146,7 @@ static void WriteConstantInt(raw_ostream &Out, const Constant *CV,
/// ostream. This can be useful when you just want to print int %reg126, not /// ostream. This can be useful when you just want to print int %reg126, not
/// the whole instruction that generated it. /// the whole instruction that generated it.
/// ///
static void WriteAsOperandInternal(raw_ostream &Out, const Value *V, static void WriteAsOperandInternal(formatted_raw_ostream &Out, const Value *V,
TypePrinting &TypePrinter, TypePrinting &TypePrinter,
SlotTracker *Machine) { SlotTracker *Machine) {
if (V->hasName()) { if (V->hasName()) {
@ -1233,14 +1236,15 @@ void llvm::WriteAsOperand(raw_ostream &Out, const Value *V, bool PrintType,
Out << ' '; Out << ' ';
} }
WriteAsOperandInternal(Out, V, TypePrinter, 0); formatted_raw_ostream FOut(Out);
WriteAsOperandInternal(FOut, V, TypePrinter, 0);
} }
namespace { namespace {
class AssemblyWriter { class AssemblyWriter {
raw_ostream &Out; formatted_raw_ostream &Out;
SlotTracker &Machine; SlotTracker &Machine;
const Module *TheModule; const Module *TheModule;
TypePrinting TypePrinter; TypePrinting TypePrinter;
@ -1251,7 +1255,8 @@ class AssemblyWriter {
std::map<const MDNode *, unsigned> MDNodes; std::map<const MDNode *, unsigned> MDNodes;
unsigned MetadataIDNo; unsigned MetadataIDNo;
public: public:
inline AssemblyWriter(raw_ostream &o, SlotTracker &Mac, const Module *M, inline AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac,
const Module *M,
AssemblyAnnotationWriter *AAW) AssemblyAnnotationWriter *AAW)
: Out(o), Machine(Mac), TheModule(M), AnnotationWriter(AAW), MetadataIDNo(0) { : Out(o), Machine(Mac), TheModule(M), AnnotationWriter(AAW), MetadataIDNo(0) {
AddModuleTypesToPrinter(TypePrinter, NumberedTypes, M); AddModuleTypesToPrinter(TypePrinter, NumberedTypes, M);
@ -1403,7 +1408,8 @@ void AssemblyWriter::printModule(const Module *M) {
WriteMDNodes(Out, TypePrinter, Machine); WriteMDNodes(Out, TypePrinter, Machine);
} }
static void PrintLinkage(GlobalValue::LinkageTypes LT, raw_ostream &Out) { static void PrintLinkage(GlobalValue::LinkageTypes LT,
formatted_raw_ostream &Out) {
switch (LT) { switch (LT) {
case GlobalValue::ExternalLinkage: break; case GlobalValue::ExternalLinkage: break;
case GlobalValue::PrivateLinkage: Out << "private "; break; case GlobalValue::PrivateLinkage: Out << "private "; break;
@ -1428,7 +1434,7 @@ static void PrintLinkage(GlobalValue::LinkageTypes LT, raw_ostream &Out) {
static void PrintVisibility(GlobalValue::VisibilityTypes Vis, static void PrintVisibility(GlobalValue::VisibilityTypes Vis,
raw_ostream &Out) { formatted_raw_ostream &Out) {
switch (Vis) { switch (Vis) {
default: llvm_unreachable("Invalid visibility style!"); default: llvm_unreachable("Invalid visibility style!");
case GlobalValue::DefaultVisibility: break; case GlobalValue::DefaultVisibility: break;
@ -1519,7 +1525,8 @@ void AssemblyWriter::printTypeSymbolTable(const TypeSymbolTable &ST) {
// Make sure we print out at least one level of the type structure, so // Make sure we print out at least one level of the type structure, so
// that we do not get %2 = type %2 // that we do not get %2 = type %2
TypePrinter.printAtLeastOneLevel(NumberedTypes[i], Out); TypePrinter.printAtLeastOneLevel(NumberedTypes[i], Out);
Out << "\t\t; type %" << i << '\n'; Out.PadToColumn(50);
Out << "; type %" << i << '\n';
} }
// Print the named types. // Print the named types.
@ -1668,11 +1675,13 @@ void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Out << "<badref>"; Out << "<badref>";
} }
if (BB->getParent() == 0) if (BB->getParent() == 0) {
Out << "\t\t; Error: Block without parent!"; Out.PadToColumn(50);
else if (BB != &BB->getParent()->getEntryBlock()) { // Not the entry block? Out << "; Error: Block without parent!";
} else if (BB != &BB->getParent()->getEntryBlock()) { // Not the entry block?
// Output predecessors for the block... // Output predecessors for the block...
Out << "\t\t;"; Out.PadToColumn(50);
Out << ";";
pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB); pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
if (PI == PE) { if (PI == PE) {
@ -1706,7 +1715,8 @@ void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
/// ///
void AssemblyWriter::printInfoComment(const Value &V) { void AssemblyWriter::printInfoComment(const Value &V) {
if (V.getType() != Type::VoidTy) { if (V.getType() != Type::VoidTy) {
Out << "\t\t; <"; Out.PadToColumn(50);
Out << "; <";
TypePrinter.print(V.getType(), Out); TypePrinter.print(V.getType(), Out);
Out << '>'; Out << '>';
@ -1991,8 +2001,9 @@ void Module::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
raw_os_ostream OS(o); raw_os_ostream OS(o);
print(OS, AAW); print(OS, AAW);
} }
void Module::print(raw_ostream &OS, AssemblyAnnotationWriter *AAW) const { void Module::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW) const {
SlotTracker SlotTable(this); SlotTracker SlotTable(this);
formatted_raw_ostream OS(ROS);
AssemblyWriter W(OS, SlotTable, this, AAW); AssemblyWriter W(OS, SlotTable, this, AAW);
W.write(this); W.write(this);
} }
@ -2010,7 +2021,8 @@ void Type::print(raw_ostream &OS) const {
TypePrinting().print(this, OS); TypePrinting().print(this, OS);
} }
void Value::print(raw_ostream &OS, AssemblyAnnotationWriter *AAW) const { void Value::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW) const {
formatted_raw_ostream OS(ROS);
if (this == 0) { if (this == 0) {
OS << "printing a <null> value\n"; OS << "printing a <null> value\n";
return; return;