[llvm-commits] [parallel] CVS: llvm/lib/Analysis/Trace.cpp BasicAliasAnalysis.cpp InductionVariable.cpp ProfileInfo.cpp ProfileInfoLoader.cpp ProfileInfoLoaderPass.cpp

Misha Brukman brukman at cs.uiuc.edu
Wed Mar 10 19:33:00 PST 2004


Changes in directory llvm/lib/Analysis:

Trace.cpp added (r1.1.2.1)
BasicAliasAnalysis.cpp updated: 1.26.2.1 -> 1.26.2.2
InductionVariable.cpp updated: 1.34 -> 1.34.2.1
ProfileInfo.cpp updated: 1.3.2.1 -> 1.3.2.2
ProfileInfoLoader.cpp updated: 1.2.2.1 -> 1.2.2.2
ProfileInfoLoaderPass.cpp updated: 1.3.2.1 -> 1.3.2.2

---
Log message:

Merge from trunk.

---
Diffs of the changes:  (+232 -29)

Index: llvm/lib/Analysis/Trace.cpp
diff -c /dev/null llvm/lib/Analysis/Trace.cpp:1.1.2.1
*** /dev/null	Wed Mar 10 19:32:01 2004
--- llvm/lib/Analysis/Trace.cpp	Wed Mar 10 19:31:51 2004
***************
*** 0 ****
--- 1,50 ----
+ //===- Trace.cpp - Implementation of Trace class --------------------------===//
+ //
+ //                     The LLVM Compiler Infrastructure
+ //
+ // This file was developed by the LLVM research group and is distributed under
+ // the University of Illinois Open Source License. See LICENSE.TXT for details.
+ //
+ //===----------------------------------------------------------------------===//
+ //
+ // This class represents a single trace of LLVM basic blocks.  A trace is a
+ // single entry, multiple exit, region of code that is often hot.  Trace-based
+ // optimizations treat traces almost like they are a large, strange, basic
+ // block: because the trace path is assumed to be hot, optimizations for the
+ // fall-through path are made at the expense of the non-fall-through paths.
+ //
+ //===----------------------------------------------------------------------===//
+ 
+ #include "llvm/Analysis/Trace.h"
+ #include "llvm/Function.h"
+ #include "llvm/Assembly/Writer.h"
+ using namespace llvm;
+ 
+ Function *Trace::getFunction() const {
+   return getEntryBasicBlock()->getParent();
+ }
+ 
+ 
+ Module *Trace::getModule() const {
+   return getFunction()->getParent();
+ }
+ 
+ /// print - Write trace to output stream.
+ ///
+ void Trace::print (std::ostream &O) const {
+   Function *F = getFunction ();
+   O << "; Trace from function " << F->getName () << ", blocks:\n";
+   for (const_iterator i = begin (), e = end (); i != e; ++i) {
+     O << "; ";
+     WriteAsOperand (O, *i, true, true, getModule ());
+     O << "\n";
+   }
+   O << "; Trace parent function: \n" << *F;
+ }
+ 
+ /// dump - Debugger convenience method; writes trace to standard error
+ /// output stream.
+ ///
+ void Trace::dump () const {
+   print (std::cerr);
+ }


Index: llvm/lib/Analysis/BasicAliasAnalysis.cpp
diff -u llvm/lib/Analysis/BasicAliasAnalysis.cpp:1.26.2.1 llvm/lib/Analysis/BasicAliasAnalysis.cpp:1.26.2.2
--- llvm/lib/Analysis/BasicAliasAnalysis.cpp:1.26.2.1	Mon Mar  1 17:58:12 2004
+++ llvm/lib/Analysis/BasicAliasAnalysis.cpp	Wed Mar 10 19:31:51 2004
@@ -20,16 +20,19 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "llvm/Analysis/AliasAnalysis.h"
-#include "llvm/Pass.h"
 #include "llvm/Argument.h"
 #include "llvm/iOther.h"
 #include "llvm/iMemory.h"
 #include "llvm/Constants.h"
 #include "llvm/GlobalVariable.h"
 #include "llvm/DerivedTypes.h"
-#include "llvm/Target/TargetData.h"
+#include "llvm/GlobalVariable.h"
+#include "llvm/iOther.h"
+#include "llvm/iMemory.h"
+#include "llvm/Pass.h"
+#include "llvm/Analysis/AliasAnalysis.h"
 #include "llvm/Support/GetElementPtrTypeIterator.h"
+#include "llvm/Target/TargetData.h"
 using namespace llvm;
 
 // Make sure that anything that uses AliasAnalysis pulls in this file...


Index: llvm/lib/Analysis/InductionVariable.cpp
diff -u llvm/lib/Analysis/InductionVariable.cpp:1.34 llvm/lib/Analysis/InductionVariable.cpp:1.34.2.1
--- llvm/lib/Analysis/InductionVariable.cpp:1.34	Tue Dec 23 02:04:02 2003
+++ llvm/lib/Analysis/InductionVariable.cpp	Wed Mar 10 19:31:51 2004
@@ -136,10 +136,14 @@
         if (Constant *CV = dyn_cast<Constant>(V))
           Step = ConstantExpr::get(Instruction::Sub, Zero, CV);
         else if (Instruction *I = dyn_cast<Instruction>(V)) {
+          BasicBlock::iterator InsertPt = I;
+          for (++InsertPt; isa<PHINode>(InsertPt); ++InsertPt)
+            /*empty*/;
           Step = BinaryOperator::create(Instruction::Sub, Zero, V,
-                                        V->getName()+".neg", I->getNext());
+                                        V->getName()+".neg", InsertPt);
 
         } else {
+          // Must be loop invariant
           Step = BinaryOperator::create(Instruction::Sub, Zero, V,
                                         V->getName()+".neg", 
                               Phi->getParent()->getParent()->begin()->begin());


Index: llvm/lib/Analysis/ProfileInfo.cpp
diff -u llvm/lib/Analysis/ProfileInfo.cpp:1.3.2.1 llvm/lib/Analysis/ProfileInfo.cpp:1.3.2.2
--- llvm/lib/Analysis/ProfileInfo.cpp:1.3.2.1	Mon Mar  1 17:58:12 2004
+++ llvm/lib/Analysis/ProfileInfo.cpp	Wed Mar 10 19:31:51 2004
@@ -14,6 +14,8 @@
 
 #include "llvm/Analysis/ProfileInfo.h"
 #include "llvm/Pass.h"
+#include "llvm/Support/CFG.h"
+#include <set>
 using namespace llvm;
 
 // Register the ProfileInfo interface, providing a nice name to refer to.
@@ -23,15 +25,63 @@
 
 ProfileInfo::~ProfileInfo() {}
 
+unsigned ProfileInfo::getExecutionCount(BasicBlock *BB) const {
+  pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
+
+  // Are there zero predecessors of this block?
+  if (PI == PE) {
+    // If this is the entry block, look for the Null -> Entry edge.
+    if (BB == &BB->getParent()->front())
+      return getEdgeWeight(0, BB);
+    else
+      return 0;   // Otherwise, this is a dead block.
+  }
+
+  // Otherwise, if there are predecessors, the execution count of this block is
+  // the sum of the edge frequencies from the incoming edges.  Note that if
+  // there are multiple edges from a predecessor to this block that we don't
+  // want to count its weight multiple times.  For this reason, we keep track of
+  // the predecessors we've seen and only count them if we haven't run into them
+  // yet.
+  //
+  // We don't want to create an std::set unless we are dealing with a block that
+  // has a LARGE number of in-edges.  Handle the common case of having only a
+  // few in-edges with special code.
+  //
+  BasicBlock *FirstPred = *PI;
+  unsigned Count = getEdgeWeight(FirstPred, BB);
+  ++PI;
+  if (PI == PE) return Count;   // Quick exit for single predecessor blocks
+
+  BasicBlock *SecondPred = *PI;
+  if (SecondPred != FirstPred) Count += getEdgeWeight(SecondPred, BB);
+  ++PI;
+  if (PI == PE) return Count;   // Quick exit for two predecessor blocks
+
+  BasicBlock *ThirdPred = *PI;
+  if (ThirdPred != FirstPred && ThirdPred != SecondPred)
+    Count += getEdgeWeight(ThirdPred, BB);
+  ++PI;
+  if (PI == PE) return Count;   // Quick exit for three predecessor blocks
+
+  std::set<BasicBlock*> ProcessedPreds;
+  ProcessedPreds.insert(FirstPred);
+  ProcessedPreds.insert(SecondPred);
+  ProcessedPreds.insert(ThirdPred);
+  for (; PI != PE; ++PI)
+    if (ProcessedPreds.insert(*PI).second)
+      Count += getEdgeWeight(*PI, BB);
+  return Count;
+}
+
+
 
 //===----------------------------------------------------------------------===//
 //  NoProfile ProfileInfo implementation
 //
 
 namespace {
-  struct NoProfileInfo : public ImmutablePass, public ProfileInfo {
-    unsigned getExecutionCount(BasicBlock *BB) { return 0; }
-  };
+  struct NoProfileInfo : public ImmutablePass, public ProfileInfo {};
  
   // Register this pass...
   RegisterOpt<NoProfileInfo>


Index: llvm/lib/Analysis/ProfileInfoLoader.cpp
diff -u llvm/lib/Analysis/ProfileInfoLoader.cpp:1.2.2.1 llvm/lib/Analysis/ProfileInfoLoader.cpp:1.2.2.2
--- llvm/lib/Analysis/ProfileInfoLoader.cpp:1.2.2.1	Mon Mar  1 17:58:12 2004
+++ llvm/lib/Analysis/ProfileInfoLoader.cpp	Wed Mar 10 19:31:51 2004
@@ -14,13 +14,16 @@
 
 #include "llvm/Analysis/ProfileInfoLoader.h"
 #include "llvm/Module.h"
+#include "llvm/InstrTypes.h"
 #include <cstdio>
+#include <map>
 using namespace llvm;
 
 enum ProfilingType {
   ArgumentInfo = 1,   // The command line argument block
   FunctionInfo = 2,   // Function profiling information
   BlockInfo    = 3,   // Block profiling information
+  EdgeInfo     = 4,   // Edge profiling information
 };
 
 // ByteSwap - Byteswap 'Var' if 'Really' is true.
@@ -122,6 +125,10 @@
       ReadProfilingBlock(ToolName, F, ShouldByteSwap, BlockCounts);
       break;
 
+    case EdgeInfo:
+      ReadProfilingBlock(ToolName, F, ShouldByteSwap, EdgeCounts);
+      break;
+
     default:
       std::cerr << ToolName << ": Unknown packet type #" << PacketType << "!\n";
       exit(1);
@@ -139,15 +146,19 @@
 void ProfileInfoLoader::getFunctionCounts(std::vector<std::pair<Function*,
                                                       unsigned> > &Counts) {
   if (FunctionCounts.empty()) {
-    // Synthesize function frequency information from the number of times their
-    // entry blocks were executed.
-    std::vector<std::pair<BasicBlock*, unsigned> > BlockCounts;
-    getBlockCounts(BlockCounts);
-
-    for (unsigned i = 0, e = BlockCounts.size(); i != e; ++i)
-      if (&BlockCounts[i].first->getParent()->front() == BlockCounts[i].first)
-        Counts.push_back(std::make_pair(BlockCounts[i].first->getParent(),
-                                        BlockCounts[i].second));
+    if (hasAccurateBlockCounts()) {
+      // Synthesize function frequency information from the number of times
+      // their entry blocks were executed.
+      std::vector<std::pair<BasicBlock*, unsigned> > BlockCounts;
+      getBlockCounts(BlockCounts);
+      
+      for (unsigned i = 0, e = BlockCounts.size(); i != e; ++i)
+        if (&BlockCounts[i].first->getParent()->front() == BlockCounts[i].first)
+          Counts.push_back(std::make_pair(BlockCounts[i].first->getParent(),
+                                          BlockCounts[i].second));
+    } else {
+      std::cerr << "Function counts are not available!\n";
+    }
     return;
   }
   
@@ -165,8 +176,59 @@
 void ProfileInfoLoader::getBlockCounts(std::vector<std::pair<BasicBlock*,
                                                          unsigned> > &Counts) {
   if (BlockCounts.empty()) {
-    std::cerr << "Block counts not available, and no synthesis "
-              << "is implemented yet!\n";
+    if (hasAccurateEdgeCounts()) {
+      // Synthesize block count information from edge frequency information.
+      // The block execution frequency is equal to the sum of the execution
+      // frequency of all outgoing edges from a block.
+      //
+      // If a block has no successors, this will not be correct, so we have to
+      // special case it. :(
+      std::vector<std::pair<Edge, unsigned> > EdgeCounts;
+      getEdgeCounts(EdgeCounts);
+
+      std::map<BasicBlock*, unsigned> InEdgeFreqs;
+
+      BasicBlock *LastBlock = 0;
+      TerminatorInst *TI = 0;
+      for (unsigned i = 0, e = EdgeCounts.size(); i != e; ++i) {
+        if (EdgeCounts[i].first.first != LastBlock) {
+          LastBlock = EdgeCounts[i].first.first;
+          TI = LastBlock->getTerminator();
+          Counts.push_back(std::make_pair(LastBlock, 0));
+        }
+        Counts.back().second += EdgeCounts[i].second;
+        unsigned SuccNum = EdgeCounts[i].first.second;
+        if (SuccNum >= TI->getNumSuccessors()) {
+          static bool Warned = false;
+          if (!Warned) {
+            std::cerr << "WARNING: profile info doesn't seem to match"
+                      << " the program!\n";
+            Warned = true;
+          }
+        } else {
+          // If this successor has no successors of its own, we will never
+          // compute an execution count for that block.  Remember the incoming
+          // edge frequencies to add later.
+          BasicBlock *Succ = TI->getSuccessor(SuccNum);
+          if (Succ->getTerminator()->getNumSuccessors() == 0)
+            InEdgeFreqs[Succ] += EdgeCounts[i].second;
+        }
+      }
+
+      // Now we have to accumulate information for those blocks without
+      // successors into our table.
+      for (std::map<BasicBlock*, unsigned>::iterator I = InEdgeFreqs.begin(),
+             E = InEdgeFreqs.end(); I != E; ++I) {
+        unsigned i = 0;
+        for (; i != Counts.size() && Counts[i].first != I->first; ++i)
+          /*empty*/;
+        if (i == Counts.size()) Counts.push_back(std::make_pair(I->first, 0));
+        Counts[i].second += I->second;
+      }
+
+    } else {
+      std::cerr << "Block counts are not available!\n";
+    }
     return;
   }
 
@@ -177,4 +239,27 @@
       if (Counter == BlockCounts.size())
         return;
     }
+}
+
+// getEdgeCounts - This method is used by consumers of edge counting
+// information.  If we do not directly have edge count information, we compute
+// it from other, more refined, types of profile information.
+//
+void ProfileInfoLoader::getEdgeCounts(std::vector<std::pair<Edge,
+                                                  unsigned> > &Counts) {
+  if (EdgeCounts.empty()) {
+    std::cerr << "Edge counts not available, and no synthesis "
+              << "is implemented yet!\n";
+    return;
+  }
+
+  unsigned Counter = 0;
+  for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F)
+    for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
+      for (unsigned i = 0, e = BB->getTerminator()->getNumSuccessors();
+           i != e; ++i) {
+        Counts.push_back(std::make_pair(Edge(BB, i), EdgeCounts[Counter++]));
+        if (Counter == EdgeCounts.size())
+          return;
+      }
 }


Index: llvm/lib/Analysis/ProfileInfoLoaderPass.cpp
diff -u llvm/lib/Analysis/ProfileInfoLoaderPass.cpp:1.3.2.1 llvm/lib/Analysis/ProfileInfoLoaderPass.cpp:1.3.2.2
--- llvm/lib/Analysis/ProfileInfoLoaderPass.cpp:1.3.2.1	Mon Mar  1 17:58:12 2004
+++ llvm/lib/Analysis/ProfileInfoLoaderPass.cpp	Wed Mar 10 19:31:51 2004
@@ -12,6 +12,8 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "llvm/BasicBlock.h"
+#include "llvm/InstrTypes.h"
 #include "llvm/Pass.h"
 #include "llvm/Analysis/ProfileInfo.h"
 #include "llvm/Analysis/ProfileInfoLoader.h"
@@ -26,7 +28,6 @@
 
   class LoaderPass : public Pass, public ProfileInfo {
     std::string Filename;
-    std::map<BasicBlock*, unsigned> ExecutionCounts;
   public:
     LoaderPass(const std::string &filename = "")
       : Filename(filename) {
@@ -43,11 +44,6 @@
 
     /// run - Load the profile information from the specified file.
     virtual bool run(Module &M);
-
-    virtual unsigned getExecutionCount(BasicBlock *BB) {
-      std::map<BasicBlock*, unsigned>::iterator I = ExecutionCounts.find(BB);
-      return I != ExecutionCounts.end() ? I->second : 0;
-    }
   };
  
   RegisterOpt<LoaderPass>
@@ -65,11 +61,26 @@
 }
 
 bool LoaderPass::run(Module &M) {
-  ProfileInfoLoader PIL("opt", Filename, M);
-  if (PIL.hasAccurateBlockCounts()) {
-    std::vector<std::pair<BasicBlock*, unsigned> > Counts;
-    PIL.getBlockCounts(Counts);
-    ExecutionCounts.insert(Counts.begin(), Counts.end());
+  ProfileInfoLoader PIL("profile-loader", Filename, M);
+  EdgeCounts.clear();
+  bool PrintedWarning = false;
+  
+  std::vector<std::pair<ProfileInfoLoader::Edge, unsigned> > ECs;
+  PIL.getEdgeCounts(ECs);
+  for (unsigned i = 0, e = ECs.size(); i != e; ++i) {
+    BasicBlock *BB = ECs[i].first.first;
+    unsigned SuccNum = ECs[i].first.second;
+    TerminatorInst *TI = BB->getTerminator();
+    if (SuccNum >= TI->getNumSuccessors()) {
+      if (!PrintedWarning) {
+        std::cerr << "WARNING: profile information is inconsistent with "
+                  << "the current program!\n";
+        PrintedWarning = true;
+      }
+    } else {
+      EdgeCounts[std::make_pair(BB, TI->getSuccessor(SuccNum))]+= ECs[i].second;
+    }
   }
+
   return false;
 }





More information about the llvm-commits mailing list