 /***********************************************************************
 *  __________________________________________________________________
 * 
 *              _____ _           ____        _       __ 
 *             / ___/(_)___ ___  / __ \____  (_)___  / /_
 *             \__ \/ / __ `__ \/ /_/ / __ \/ / __ \/ __/
 *            ___/ / / / / / / / ____/ /_/ / / / / / /_  
 *           /____/_/_/ /_/ /_/_/    \____/_/_/ /_/\__/  
 * 
 *  __________________________________________________________________
 * 
 * This file is part of the SimPoint Toolkit written by Greg Hamerly, 
 * Erez Perelman, Tim Sherwood, and Brad Calder as part of Efficient
 * Simulation Project at UCSD.  If you find this toolkit useful please 
 * cite the following paper published at ASPLOS 2002.
 *
 *  Timothy Sherwood, Erez Perelman, Greg Hamerly and Brad Calder,
 *  Automatically Characterizing Large Scale Program Behavior , In the
 *  10th International Conference on Architectural Support for Programming
 *  Languages and Operating Systems, October 2002.
 *
 * Contact info:
 *        Brad Calder <calder@cs.ucsd.edu>, (858) 822 - 1619
 *        Tim Sherwood <sherwood@cs.usd.edu>,
 *        Erez Perelman <eperelma@cs.ucsd.edu>,
 *        Greg Hamerly <ghamerly@cs.ucsd.edu>,
 *
 *        University of California, San Diego 
 *        Department of Computer Science and Engineering 
 *        9500 Gilman Drive, Dept 0114
 *        La Jolla CA 92093-0114 USA
 *
 *
 * Copyright 2001, 2002 The Regents of the University of California
 * All Rights Reserved
 *
 * Permission to use, copy, modify and distribute any part of this
 * SimPoint Toolkit for educational and non-profit purposes,
 * without fee, and without a written agreement is hereby granted,
 * provided that the above copyright notice, this paragraph and the
 * following five paragraphs appear in all copies.
 *
 * Those desiring to incorporate this SimPoint Toolkit into commercial
 * products or use for commercial purposes should contact the Technology
 * Transfer Office, University of California, San Diego, 9500 Gilman
 * Drive, La Jolla, CA 92093-0910, Ph: (619) 534-5815, FAX: (619)
 * 534-7345.
 *
 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
 * INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THE SimPoint
 * Toolkit, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 * 
 * THE SimPoint Toolkit PROVIDED HEREIN IS ON AN "AS IS" BASIS, AND THE
 * UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE,
 * SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. THE UNIVERSITY OF
 * CALIFORNIA MAKES NO REPRESENTATIONS AND EXTENDS NO WARRANTIES OF ANY
 * KIND, EITHER IMPLIED OR EXPRESS, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR
 * PURPOSE, OR THAT THE USE OF THE SimPoint Toolkit WILL NOT INFRINGE ANY
 * PATENT, TRADEMARK OR OTHER RIGHTS.
 * 
 * No nonprofit user may place any restrictions on the use of this
 * software, including as modified by the user, by any other authorized
 * user.
 *
 ************************************************************************/


I. ABOUT SIMPOINT

   SimPoint is a simulation analysis tool designed to find the ideal
   simulation points in applications. It provides the user with
   relevant information regarding the execution behavior of the
   application, including an accurate length of the initialization
   phase.

   To make SimPoint a fast and efficient tool, its analysis does not
   use any knowledge of the architectural metrics for the program
   (which consume a great deal of processing power and time to
   collect), but instead utilizes a modeling schema that highly
   correlates with the performance of those metrics.

   The SimPoint analysis has two main components. The first component
   takes a program for input, and returns a basic block profile of the
   execution of that program. The second component analyzes the basic
   block profile and returns the ideal simulation point.


 A. The Branch Profiler

   Running SimPoint requires the basic block execution history of the
   program and the desired simulation duration. Since SimpleScalar
   does not have a notion of basic blocks, we generate a branch profile
   to guide the SimPoint Analysis Toolkit for finding simulation points.
   The SimpleScalar branch profiler tracks the number of instructions
   executed between branches.  After a branch executes the we re-start
   counting the number of instructions, and when the next branch is
   executed that branch is updated with the current instruction count
   in the vector.

   Before generating the branch profile, you must
   first decide the smallest unit of instructions (interval-size) you
   want to simulate.  By default the size is set to 100 million
   instructions (100000000).  The profiler then outputs for each unit of
   instructions (e.g., every 100 millions) a branch vector
   representing the number of times each branch was executed during
   that interval weighted by its the number of instructions executed 
   to reach that branch.

   This parameter can be set to different values to accommodate for the
   size of the program execution, but should be set according to the
   number of instructions you want to simulate. It is also recommended
   that the interval size provide a minimum of 10 intervals in the full
   execution. The more intervals the execution is partitioned into, the
   more precise the SimPoint analysis will be. 

   The branch profiler will output a compressed file, *.bb.gz,
   with the following format:

   T:BB_X:TimesExecuted :BB_Y:TimesExecuted ... :BB_Z:TimesExecuted
   T:BB_X:TimesExecuted :BB_Y:TimesExecuted ... :BB_Z:TimesExecuted  
   T:BB_X:TimesExecuted :BB_Y:TimesExecuted ... :BB_Z:TimesExecuted
	...
	...
	...
   T:BB_X:TimesExecuted :BB_Y:TimesExecuted ... :BB_Z:TimesExecuted
   T:BB_X:TimesExecuted :BB_Y:TimesExecuted ... :BB_Z:TimesExecuted

   Each line represents an execution interval of interval-size
   instructions executed. Each element has two fields:
   
   1) BB_X - Represents a particular basic block with a basic block
      identification number. Each basic block in the program is
      represented with its own unique basic block identification number.

   2) TimesExecuted - The number of times a basic block has executed
      in that execution interval. If a basic block has not executed at
      all during an interval, than it does not have an entry for that
      interval. Hence, each line will only correspond to the basic
      blocks executed in a particular interval, usually a sparse
      matrix.


--- The distribution --------------------------------------

Files:

	bbtracker.h & bbtracker.c - branch vector profiling code

	sim-fast.c  - modified version from SimpleScalar 3.0c release to
			generate branch vectors for finding SimPoints.



Parameters: 
	-interval: the size of interval in terms of number of instructions.
		   Default value 100,000,000.  This is the i
		   (e.g., 100000000 specifies an interval size of 100,000,000
			executed instructions)
	-outdir: branch vector output directory
	-outfile: branch vector output file



------------------------------------------------------------------------
The following are the three changes required in sim-fast.c to
incorporate the branch profiler.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Change#1: Declare parameters for generated basic block vectors:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

  /* interval size in instructions for basic block profiling */
  static int m_interval_size;
  /* destination directory for output file */
  static char *m_outdir;
  /* ouput file name */
  static char *m_outfile;


  /* interval size */
  opt_reg_int(odb, "-interval", "size of interval in instructions",
              &m_interval_size, /* default */100000000,
              /* print */TRUE, /* format */NULL);
  opt_reg_string(odb, "-outdir",
                 "path for simpoint file",
                 &m_outdir, /* default */NULL,
                 /* print */TRUE, /* format */NULL);
  opt_reg_string(odb, "-outfile",
                 "name of output file",
                 &m_outfile, /* default */NULL,
                 /* print */TRUE, /* format */NULL);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Change#2: Initialize the basic block tracker at the start of sim_main
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


  int num_inst=1;
  init_bb_tracker(m_outdir, m_outfile, m_interval_size);



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Change#3: Inside the main loop of sim_main, add the following check, which
will call the basic block tracker whenever a branch is executed.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


  if (MD_OP_FLAGS(op) & F_CTRL) {
     /* instruction is control flow, hence it is the end
          of a basic block  */
     bb_tracker((long)regs.regs_PC, num_inst);
     // initialize number of instruction in basic block
     num_inst = 1;
  } else {
     /* keep track of number of instructions in basic block */
     num_inst++;
  }
