[llvm-commits] [llvm] r135352 - in /llvm/trunk: include/llvm/Analysis/BlockFrequencyImpl.h include/llvm/CodeGen/MachineBlockFrequency.h include/llvm/InitializePasses.h lib/CodeGen/CMakeLists.txt lib/CodeGen/MachineBlockFrequency.cpp

Jakub Staszak jstaszak at apple.com
Sat Jul 16 13:23:20 PDT 2011


Author: kuba
Date: Sat Jul 16 15:23:20 2011
New Revision: 135352

URL: http://llvm.org/viewvc/llvm-project?rev=135352&view=rev
Log:
Add MachineBlockFrequency analysis.

Added:
    llvm/trunk/include/llvm/CodeGen/MachineBlockFrequency.h   (with props)
    llvm/trunk/lib/CodeGen/MachineBlockFrequency.cpp   (with props)
Modified:
    llvm/trunk/include/llvm/Analysis/BlockFrequencyImpl.h
    llvm/trunk/include/llvm/InitializePasses.h
    llvm/trunk/lib/CodeGen/CMakeLists.txt

Modified: llvm/trunk/include/llvm/Analysis/BlockFrequencyImpl.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Analysis/BlockFrequencyImpl.h?rev=135352&r1=135351&r2=135352&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Analysis/BlockFrequencyImpl.h (original)
+++ llvm/trunk/include/llvm/Analysis/BlockFrequencyImpl.h Sat Jul 16 15:23:20 2011
@@ -18,8 +18,10 @@
 #include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/PostOrderIterator.h"
 #include "llvm/CodeGen/MachineBasicBlock.h"
+#include "llvm/CodeGen/MachineFunction.h"
 #include "llvm/Support/BranchProbability.h"
 #include "llvm/Support/Debug.h"
+#include "llvm/Support/raw_ostream.h"
 #include <vector>
 #include <sstream>
 #include <string>
@@ -28,6 +30,8 @@
 
 
 class BlockFrequency;
+class MachineBlockFrequency;
+
 /// BlockFrequencyImpl implements block frequency algorithm for IR and
 /// Machine Instructions. Algorithm starts with value 1024 (START_FREQ)
 /// for the entry block and then propagates frequencies using branch weights
@@ -53,9 +57,8 @@
   std::string getBlockName(MachineBasicBlock *MBB) const {
     std::stringstream ss;
     ss << "BB#" << MBB->getNumber();
-    const BasicBlock *BB = MBB->getBasicBlock();
 
-    if (BB)
+    if (const BasicBlock *BB = MBB->getBasicBlock())
       ss << " derived from LLVM BB " << BB->getNameStr();
 
     return ss.str();
@@ -261,6 +264,7 @@
   }
 
   friend class BlockFrequency;
+  friend class MachineBlockFrequency;
 
   void doFunction(FunctionT *fn, BlockProbInfoT *bpi) {
     Fn = fn;

Added: llvm/trunk/include/llvm/CodeGen/MachineBlockFrequency.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/CodeGen/MachineBlockFrequency.h?rev=135352&view=auto
==============================================================================
--- llvm/trunk/include/llvm/CodeGen/MachineBlockFrequency.h (added)
+++ llvm/trunk/include/llvm/CodeGen/MachineBlockFrequency.h Sat Jul 16 15:23:20 2011
@@ -0,0 +1,53 @@
+//====----- MachineBlockFrequency.h - MachineBlock Frequency Analysis ----====//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// Loops should be simplified before this analysis.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CODEGEN_MACHINEBLOCKFREQUENCY_H
+#define LLVM_CODEGEN_MACHINEBLOCKFREQUENCY_H
+
+#include "llvm/CodeGen/MachineFunctionPass.h"
+#include <climits>
+
+namespace llvm {
+
+class MachineBranchProbabilityInfo;
+template<class BlockT, class FunctionT, class BranchProbInfoT>
+class BlockFrequencyImpl;
+
+/// MachineBlockFrequency pass uses BlockFrequencyImpl implementation to estimate
+/// machine basic block frequencies.
+class MachineBlockFrequency : public MachineFunctionPass {
+
+  BlockFrequencyImpl<MachineBasicBlock, MachineFunction, MachineBranchProbabilityInfo> *MBFI;
+
+public:
+  static char ID;
+
+  MachineBlockFrequency();
+
+  ~MachineBlockFrequency();
+
+  void getAnalysisUsage(AnalysisUsage &AU) const;
+
+  bool runOnMachineFunction(MachineFunction &F);
+
+  /// getblockFreq - Return block frequency. Never return 0, value must be
+  /// positive. Please note that initial frequency is equal to 1024. It means
+  /// that we should not rely on the value itself, but only on the comparison to
+  /// the other block frequencies. We do this to avoid using of the floating
+  /// points.
+  uint32_t getBlockFreq(MachineBasicBlock *MBB);
+};
+
+}
+
+#endif

Propchange: llvm/trunk/include/llvm/CodeGen/MachineBlockFrequency.h
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: llvm/trunk/include/llvm/CodeGen/MachineBlockFrequency.h
------------------------------------------------------------------------------
    svn:keywords = Date Rev Id

Modified: llvm/trunk/include/llvm/InitializePasses.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/InitializePasses.h?rev=135352&r1=135351&r2=135352&view=diff
==============================================================================
--- llvm/trunk/include/llvm/InitializePasses.h (original)
+++ llvm/trunk/include/llvm/InitializePasses.h Sat Jul 16 15:23:20 2011
@@ -145,6 +145,7 @@
 void initializeLowerInvokePass(PassRegistry&);
 void initializeLowerSetJmpPass(PassRegistry&);
 void initializeLowerSwitchPass(PassRegistry&);
+void initializeMachineBlockFrequencyPass(PassRegistry&);
 void initializeMachineBranchProbabilityInfoPass(PassRegistry&);
 void initializeMachineCSEPass(PassRegistry&);
 void initializeMachineDominatorTreePass(PassRegistry&);

Modified: llvm/trunk/lib/CodeGen/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/CMakeLists.txt?rev=135352&r1=135351&r2=135352&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/CMakeLists.txt (original)
+++ llvm/trunk/lib/CodeGen/CMakeLists.txt Sat Jul 16 15:23:20 2011
@@ -33,6 +33,7 @@
   LocalStackSlotAllocation.cpp
   LowerSubregs.cpp
   MachineBasicBlock.cpp
+  MachineBlockFrequency.cpp
   MachineBranchProbabilityInfo.cpp
   MachineCSE.cpp
   MachineDominators.cpp

Added: llvm/trunk/lib/CodeGen/MachineBlockFrequency.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/MachineBlockFrequency.cpp?rev=135352&view=auto
==============================================================================
--- llvm/trunk/lib/CodeGen/MachineBlockFrequency.cpp (added)
+++ llvm/trunk/lib/CodeGen/MachineBlockFrequency.cpp Sat Jul 16 15:23:20 2011
@@ -0,0 +1,59 @@
+//====----- MachineBlockFrequency.cpp - Machine Block Frequency Analysis ----====//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// Loops should be simplified before this analysis.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/InitializePasses.h"
+#include "llvm/Analysis/BlockFrequencyImpl.h"
+#include "llvm/CodeGen/MachineBlockFrequency.h"
+#include "llvm/CodeGen/Passes.h"
+#include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
+
+using namespace llvm;
+
+INITIALIZE_PASS_BEGIN(MachineBlockFrequency, "machine-block-freq",
+                      "Machine Block Frequency Analysis", true, true)
+INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
+INITIALIZE_PASS_END(MachineBlockFrequency, "machine-block-freq",
+                    "Machine Block Frequency Analysis", true, true)
+
+char MachineBlockFrequency::ID = 0;
+
+
+MachineBlockFrequency::MachineBlockFrequency() : MachineFunctionPass(ID) {
+  initializeMachineBlockFrequencyPass(*PassRegistry::getPassRegistry());
+  MBFI = new BlockFrequencyImpl<MachineBasicBlock, MachineFunction,
+                                MachineBranchProbabilityInfo>();
+}
+
+MachineBlockFrequency::~MachineBlockFrequency() {
+  delete MBFI;
+}
+
+void MachineBlockFrequency::getAnalysisUsage(AnalysisUsage &AU) const {
+  AU.addRequired<MachineBranchProbabilityInfo>();
+  AU.setPreservesAll();
+}
+
+bool MachineBlockFrequency::runOnMachineFunction(MachineFunction &F) {
+  MachineBranchProbabilityInfo &MBPI = getAnalysis<MachineBranchProbabilityInfo>();
+  MBFI->doFunction(&F, &MBPI);
+  return false;
+}
+
+/// getblockFreq - Return block frequency. Never return 0, value must be
+/// positive. Please note that initial frequency is equal to 1024. It means that
+/// we should not rely on the value itself, but only on the comparison to the
+/// other block frequencies. We do this to avoid using of floating points.
+///
+uint32_t MachineBlockFrequency::getBlockFreq(MachineBasicBlock *MBB) {
+  return MBFI->getBlockFreq(MBB);
+}

Propchange: llvm/trunk/lib/CodeGen/MachineBlockFrequency.cpp
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: llvm/trunk/lib/CodeGen/MachineBlockFrequency.cpp
------------------------------------------------------------------------------
    svn:keywords = Date Rev Id





More information about the llvm-commits mailing list