[llvm-commits] [llvm] r78567 - in /llvm/trunk: include/llvm/CodeGen/AsmPrinter.h include/llvm/Support/IOManip.h lib/CodeGen/AsmPrinter/AsmPrinter.cpp

David Greene greened at obbligato.org
Mon Aug 10 09:38:08 PDT 2009


Author: greened
Date: Mon Aug 10 11:38:07 2009
New Revision: 78567

URL: http://llvm.org/viewvc/llvm-project?rev=78567&view=rev
Log:

Add support for printing loop structure information in asm comments.
This definitely slows down asm output so put it under an -asm-exuberant
flag.

This information is useful when doing static analysis of performance
issues.

Added:
    llvm/trunk/include/llvm/Support/IOManip.h
Modified:
    llvm/trunk/include/llvm/CodeGen/AsmPrinter.h
    llvm/trunk/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

Modified: llvm/trunk/include/llvm/CodeGen/AsmPrinter.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/CodeGen/AsmPrinter.h?rev=78567&r1=78566&r2=78567&view=diff

==============================================================================
--- llvm/trunk/include/llvm/CodeGen/AsmPrinter.h (original)
+++ llvm/trunk/include/llvm/CodeGen/AsmPrinter.h Mon Aug 10 11:38:07 2009
@@ -29,6 +29,8 @@
   class ConstantVector;
   class GCMetadataPrinter;
   class GlobalVariable;
+  class MachineLoopInfo;
+  class MachineLoop;
   class MachineConstantPoolEntry;
   class MachineConstantPoolValue;
   class MachineModuleInfo;
@@ -64,6 +66,16 @@
     /// controlled and used by the SwitchToSection method.
     const MCSection *CurrentSection;
     
+    /// If ExuberantAsm is set, a pointer to the loop info for this
+    /// function.
+    ///
+    MachineLoopInfo *LI;
+
+    /// PrintChildLoopComment - Print comments about child loops
+    /// within the loop for this basic block, with nesting.
+    ///
+    void PrintChildLoopComment(const MachineLoop *loop) const;
+
   protected:
     /// MMI - If available, this is a pointer to the current MachineModuleInfo.
     MachineModuleInfo *MMI;
@@ -122,6 +134,11 @@
     ///
     bool VerboseAsm;
 
+    /// ExuberantAsm - Emit many more comments in assembly output if
+    /// this is true.
+    ///
+    bool ExuberantAsm;
+
     /// Private state for PrintSpecial()
     // Assign a unique ID to this machine instruction.
     mutable const MachineInstr *LastMI;
@@ -325,6 +342,8 @@
     void EmitComments(const MachineInstr &MI) const;
     /// EmitComments - Pretty-print comments for instructions
     void EmitComments(const MCInst &MI) const;
+    /// EmitComments - Pretty-print comments for basic blocks
+    void EmitComments(const MachineBasicBlock &MBB) const;
 
   protected:
     /// EmitZeros - Emit a block of zeros.

Added: llvm/trunk/include/llvm/Support/IOManip.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/IOManip.h?rev=78567&view=auto

==============================================================================
--- llvm/trunk/include/llvm/Support/IOManip.h (added)
+++ llvm/trunk/include/llvm/Support/IOManip.h Mon Aug 10 11:38:07 2009
@@ -0,0 +1,43 @@
+//===----------------- IOManip.h - iostream manipulators ---------*- C++ -*===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// Manipulators to do special-purpose formatting.
+//
+//===----------------------------------------------------------------------===//
+
+namespace llvm {
+  /// Indent - Insert spaces into the character output stream.  The
+  /// "level" is multiplied by the "scale" to calculate the number of
+  /// spaces to insert.  "level" can represent something like loop
+  /// nesting level, for example.
+  ///
+  class Indent {
+  public:
+    explicit Indent(int lvl, int amt = 2)
+        : level(lvl), scale(amt) {}
+
+    template<typename OStream>
+    OStream &operator()(OStream &out) const {
+      for(int i = 0; i < level*scale; ++i) {
+        out << " ";
+      }
+      return out;
+    }
+
+  private:
+    int level;
+    int scale;
+  };
+
+  template<typename OStream>
+  OStream &operator<<(OStream &out, const Indent &indent)
+  {
+    return(indent(out));
+  }
+}

Modified: llvm/trunk/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/AsmPrinter/AsmPrinter.cpp?rev=78567&r1=78566&r2=78567&view=diff

==============================================================================
--- llvm/trunk/lib/CodeGen/AsmPrinter/AsmPrinter.cpp (original)
+++ llvm/trunk/lib/CodeGen/AsmPrinter/AsmPrinter.cpp Mon Aug 10 11:38:07 2009
@@ -19,6 +19,7 @@
 #include "llvm/CodeGen/GCMetadataPrinter.h"
 #include "llvm/CodeGen/MachineConstantPool.h"
 #include "llvm/CodeGen/MachineJumpTableInfo.h"
+#include "llvm/CodeGen/MachineLoopInfo.h"
 #include "llvm/CodeGen/MachineModuleInfo.h"
 #include "llvm/CodeGen/DwarfWriter.h"
 #include "llvm/Analysis/DebugInfo.h"
@@ -29,6 +30,7 @@
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/FormattedStream.h"
+#include "llvm/Support/IOManip.h"
 #include "llvm/Support/Mangler.h"
 #include "llvm/Target/TargetAsmInfo.h"
 #include "llvm/Target/TargetData.h"
@@ -46,6 +48,10 @@
 AsmVerbose("asm-verbose", cl::desc("Add comments to directives."),
            cl::init(cl::BOU_UNSET));
 
+static cl::opt<cl::boolOrDefault>
+AsmExuberant("asm-exuberant", cl::desc("Add many comments."),
+           cl::init(cl::BOU_FALSE));
+
 char AsmPrinter::ID = 0;
 AsmPrinter::AsmPrinter(formatted_raw_ostream &o, TargetMachine &tm,
                        const TargetAsmInfo *T, bool VDef)
@@ -64,6 +70,11 @@
   case cl::BOU_TRUE:  VerboseAsm = true;  break;
   case cl::BOU_FALSE: VerboseAsm = false; break;
   }
+  switch (AsmExuberant) {
+  case cl::BOU_UNSET: ExuberantAsm = false;  break;
+  case cl::BOU_TRUE:  ExuberantAsm = true;  break;
+  case cl::BOU_FALSE: ExuberantAsm = false; break;
+  }
 }
 
 AsmPrinter::~AsmPrinter() {
@@ -101,6 +112,9 @@
   AU.setPreservesAll();
   MachineFunctionPass::getAnalysisUsage(AU);
   AU.addRequired<GCModuleInfo>();
+  if (ExuberantAsm) {
+    AU.addRequired<MachineLoopInfo>();
+  }
 }
 
 bool AsmPrinter::doInitialization(Module &M) {
@@ -233,6 +247,10 @@
   // What's my mangled name?
   CurrentFnName = Mang->getMangledName(MF.getFunction());
   IncrementFunctionNumber();
+
+  if (ExuberantAsm) {
+    LI = &getAnalysis<MachineLoopInfo>();
+  }
 }
 
 namespace {
@@ -1558,9 +1576,16 @@
     << MBB->getNumber();
   if (printColon)
     O << ':';
-  if (printComment && MBB->getBasicBlock())
-    O << '\t' << TAI->getCommentString() << ' '
-      << MBB->getBasicBlock()->getNameStr();
+  if (printComment) {
+    O.PadToColumn(TAI->getCommentColumn(), 1);
+
+    if (MBB->getBasicBlock())
+      O << '\t' << TAI->getCommentString() << ' '
+        << MBB->getBasicBlock()->getNameStr();
+
+    if (printColon)
+      EmitComments(*MBB);
+  }
 }
 
 /// printPICJumpTableSetLabel - This method prints a set label for the
@@ -1720,3 +1745,74 @@
     }
   }
 }
+
+/// EmitComments - Pretty-print comments for basic blocks
+void AsmPrinter::EmitComments(const MachineBasicBlock &MBB) const
+{
+  if (ExuberantAsm) {
+    // Add loop depth information
+    const MachineLoop *loop = LI->getLoopFor(&MBB);
+
+    if (loop) {
+      // Print a newline after bb# annotation.
+      O << "\n";
+      O.PadToColumn(TAI->getCommentColumn(), 1);
+      O << TAI->getCommentString() << " Loop Depth " << loop->getLoopDepth()
+        << '\n';
+
+      O.PadToColumn(TAI->getCommentColumn(), 1);
+
+      MachineBasicBlock *Header = loop->getHeader();
+      assert(Header && "No header for loop");
+      
+      if (Header == &MBB) {
+        O << TAI->getCommentString() << " Loop Header";
+        PrintChildLoopComment(loop);
+      }
+      else {
+        O << TAI->getCommentString() << " Loop Header is BB"
+          << getFunctionNumber() << "_" << loop->getHeader()->getNumber();
+      }
+
+      if (loop->empty()) {
+        O << '\n';
+        O.PadToColumn(TAI->getCommentColumn(), 1);
+        O << TAI->getCommentString() << " Inner Loop";
+      }
+
+      // Add parent loop information
+      for (const MachineLoop *CurLoop = loop->getParentLoop();
+           CurLoop;
+           CurLoop = CurLoop->getParentLoop()) {
+        MachineBasicBlock *Header = CurLoop->getHeader();
+        assert(Header && "No header for loop");
+
+        O << '\n';
+        O.PadToColumn(TAI->getCommentColumn(), 1);
+        O << TAI->getCommentString() << Indent(CurLoop->getLoopDepth()-1)
+          << " Inside Loop BB" << getFunctionNumber() << "_" 
+          << Header->getNumber() << " Depth " << CurLoop->getLoopDepth();
+      }
+    }
+  }
+}
+
+void AsmPrinter::PrintChildLoopComment(const MachineLoop *loop) const {
+  // Add child loop information
+  for(MachineLoop::iterator cl = loop->begin(),
+        clend = loop->end();
+      cl != clend;
+      ++cl) {
+    MachineBasicBlock *Header = (*cl)->getHeader();
+    assert(Header && "No header for loop");
+
+    O << '\n';
+    O.PadToColumn(TAI->getCommentColumn(), 1);
+
+    O << TAI->getCommentString() << Indent((*cl)->getLoopDepth()-1)
+      << " Child Loop BB" << getFunctionNumber() << "_"
+      << Header->getNumber() << " Depth " << (*cl)->getLoopDepth();
+
+    PrintChildLoopComment(*cl);
+  }
+}





More information about the llvm-commits mailing list