sewardj | 07133bf | 2002-06-13 10:25:56 +0000 | [diff] [blame] | 1 | |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 2 | /*--------------------------------------------------------------------*/ |
njn | 101e572 | 2005-04-21 02:37:54 +0000 | [diff] [blame] | 3 | /*--- Cachegrind: everything but the simulation itself. ---*/ |
njn25 | cac76cb | 2002-09-23 11:21:57 +0000 | [diff] [blame] | 4 | /*--- cg_main.c ---*/ |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 5 | /*--------------------------------------------------------------------*/ |
| 6 | |
| 7 | /* |
nethercote | 137bc55 | 2003-11-14 17:47:54 +0000 | [diff] [blame] | 8 | This file is part of Cachegrind, a Valgrind tool for cache |
njn | c953984 | 2002-10-02 13:26:35 +0000 | [diff] [blame] | 9 | profiling programs. |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 10 | |
sewardj | ec062e8 | 2011-10-23 07:32:08 +0000 | [diff] [blame] | 11 | Copyright (C) 2002-2011 Nicholas Nethercote |
njn | 2bc1012 | 2005-05-08 02:10:27 +0000 | [diff] [blame] | 12 | njn@valgrind.org |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 13 | |
| 14 | This program is free software; you can redistribute it and/or |
| 15 | modify it under the terms of the GNU General Public License as |
| 16 | published by the Free Software Foundation; either version 2 of the |
| 17 | License, or (at your option) any later version. |
| 18 | |
| 19 | This program is distributed in the hope that it will be useful, but |
| 20 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 22 | General Public License for more details. |
| 23 | |
| 24 | You should have received a copy of the GNU General Public License |
| 25 | along with this program; if not, write to the Free Software |
| 26 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 27 | 02111-1307, USA. |
| 28 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 29 | The GNU General Public License is contained in the file COPYING. |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 30 | */ |
| 31 | |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 32 | #include "pub_tool_basics.h" |
sewardj | 4cfea4f | 2006-10-14 19:26:10 +0000 | [diff] [blame] | 33 | #include "pub_tool_vki.h" |
njn | ea27e46 | 2005-05-31 02:38:09 +0000 | [diff] [blame] | 34 | #include "pub_tool_debuginfo.h" |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 35 | #include "pub_tool_libcbase.h" |
njn | 132bfcc | 2005-06-04 19:16:06 +0000 | [diff] [blame] | 36 | #include "pub_tool_libcassert.h" |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 37 | #include "pub_tool_libcfile.h" |
njn | 36a20fa | 2005-06-03 03:08:39 +0000 | [diff] [blame] | 38 | #include "pub_tool_libcprint.h" |
njn | f39e9a3 | 2005-06-12 02:43:17 +0000 | [diff] [blame] | 39 | #include "pub_tool_libcproc.h" |
njn | f536bbb | 2005-06-13 04:21:38 +0000 | [diff] [blame] | 40 | #include "pub_tool_machine.h" |
njn | 717cde5 | 2005-05-10 02:47:21 +0000 | [diff] [blame] | 41 | #include "pub_tool_mallocfree.h" |
njn | 2024234 | 2005-05-16 23:31:24 +0000 | [diff] [blame] | 42 | #include "pub_tool_options.h" |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 43 | #include "pub_tool_oset.h" |
njn | 43b9a8a | 2005-05-10 04:37:01 +0000 | [diff] [blame] | 44 | #include "pub_tool_tooliface.h" |
sewardj | 14c7cc5 | 2007-02-25 15:08:24 +0000 | [diff] [blame] | 45 | #include "pub_tool_xarray.h" |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 46 | #include "pub_tool_clientstate.h" |
sewardj | 5bb8682 | 2005-12-23 12:47:42 +0000 | [diff] [blame] | 47 | #include "pub_tool_machine.h" // VG_(fnptr_to_fnentry) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 48 | |
nethercote | b35a8b9 | 2004-09-11 16:45:27 +0000 | [diff] [blame] | 49 | #include "cg_arch.h" |
nethercote | 27fc1da | 2004-01-04 16:56:57 +0000 | [diff] [blame] | 50 | #include "cg_sim.c" |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 51 | #include "cg_branchpred.c" |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 52 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 53 | /*------------------------------------------------------------*/ |
| 54 | /*--- Constants ---*/ |
| 55 | /*------------------------------------------------------------*/ |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 56 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 57 | /* Set to 1 for very verbose debugging */ |
| 58 | #define DEBUG_CG 0 |
| 59 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 60 | #define MIN_LINE_SIZE 16 |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 61 | #define FILE_LEN VKI_PATH_MAX |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 62 | #define FN_LEN 256 |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 63 | |
| 64 | /*------------------------------------------------------------*/ |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 65 | /*--- Options ---*/ |
| 66 | /*------------------------------------------------------------*/ |
| 67 | |
njn | 374a36d | 2007-11-23 01:41:32 +0000 | [diff] [blame] | 68 | static Bool clo_cache_sim = True; /* do cache simulation? */ |
| 69 | static Bool clo_branch_sim = False; /* do branch simulation? */ |
| 70 | static Char* clo_cachegrind_out_file = "cachegrind.out.%p"; |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 71 | |
| 72 | /*------------------------------------------------------------*/ |
sewardj | 98763d5 | 2012-06-03 22:40:07 +0000 | [diff] [blame^] | 73 | /*--- Cachesim configuration ---*/ |
| 74 | /*------------------------------------------------------------*/ |
| 75 | |
| 76 | static Int min_line_size = 0; /* min of L1 and LL cache line sizes */ |
| 77 | |
| 78 | /*------------------------------------------------------------*/ |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 79 | /*--- Types and Data Structures ---*/ |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 80 | /*------------------------------------------------------------*/ |
| 81 | |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 82 | typedef |
| 83 | struct { |
| 84 | ULong a; /* total # memory accesses of this kind */ |
| 85 | ULong m1; /* misses in the first level cache */ |
njn | 2d853a1 | 2010-10-06 22:46:31 +0000 | [diff] [blame] | 86 | ULong mL; /* misses in the second level cache */ |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 87 | } |
| 88 | CacheCC; |
| 89 | |
| 90 | typedef |
| 91 | struct { |
| 92 | ULong b; /* total # branches of this kind */ |
| 93 | ULong mp; /* number of branches mispredicted */ |
| 94 | } |
| 95 | BranchCC; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 96 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 97 | //------------------------------------------------------------ |
| 98 | // Primary data structure #1: CC table |
| 99 | // - Holds the per-source-line hit/miss stats, grouped by file/function/line. |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 100 | // - an ordered set of CCs. CC indexing done by file/function/line (as |
| 101 | // determined from the instrAddr). |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 102 | // - Traversed for dumping stats at end in file/func/line hierarchy. |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 103 | |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 104 | typedef struct { |
| 105 | Char* file; |
| 106 | Char* fn; |
| 107 | Int line; |
| 108 | } |
| 109 | CodeLoc; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 110 | |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 111 | typedef struct { |
| 112 | CodeLoc loc; /* Source location that these counts pertain to */ |
| 113 | CacheCC Ir; /* Insn read counts */ |
| 114 | CacheCC Dr; /* Data read counts */ |
| 115 | CacheCC Dw; /* Data write/modify counts */ |
| 116 | BranchCC Bc; /* Conditional branch counts */ |
| 117 | BranchCC Bi; /* Indirect branch counts */ |
| 118 | } LineCC; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 119 | |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 120 | // First compare file, then fn, then line. |
tom | 5a835d5 | 2007-12-30 12:28:26 +0000 | [diff] [blame] | 121 | static Word cmp_CodeLoc_LineCC(const void *vloc, const void *vcc) |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 122 | { |
njn | afa1226 | 2005-12-24 03:10:56 +0000 | [diff] [blame] | 123 | Word res; |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 124 | CodeLoc* a = (CodeLoc*)vloc; |
| 125 | CodeLoc* b = &(((LineCC*)vcc)->loc); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 126 | |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 127 | res = VG_(strcmp)(a->file, b->file); |
| 128 | if (0 != res) |
| 129 | return res; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 130 | |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 131 | res = VG_(strcmp)(a->fn, b->fn); |
| 132 | if (0 != res) |
| 133 | return res; |
| 134 | |
| 135 | return a->line - b->line; |
| 136 | } |
| 137 | |
| 138 | static OSet* CC_table; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 139 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 140 | //------------------------------------------------------------ |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 141 | // Primary data structure #2: InstrInfo table |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 142 | // - Holds the cached info about each instr that is used for simulation. |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 143 | // - table(SB_start_addr, list(InstrInfo)) |
| 144 | // - For each SB, each InstrInfo in the list holds info about the |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 145 | // instruction (instrLen, instrAddr, etc), plus a pointer to its line |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 146 | // CC. This node is what's passed to the simulation function. |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 147 | // - When SBs are discarded the relevant list(instr_details) is freed. |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 148 | |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 149 | typedef struct _InstrInfo InstrInfo; |
| 150 | struct _InstrInfo { |
nethercote | ca1f2dc | 2004-07-21 08:49:02 +0000 | [diff] [blame] | 151 | Addr instr_addr; |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 152 | UChar instr_len; |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 153 | LineCC* parent; // parent line-CC |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 154 | }; |
| 155 | |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 156 | typedef struct _SB_info SB_info; |
| 157 | struct _SB_info { |
| 158 | Addr SB_addr; // key; MUST BE FIRST |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 159 | Int n_instrs; |
| 160 | InstrInfo instrs[0]; |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 161 | }; |
| 162 | |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 163 | static OSet* instrInfoTable; |
| 164 | |
| 165 | //------------------------------------------------------------ |
| 166 | // Secondary data structure: string table |
| 167 | // - holds strings, avoiding dups |
| 168 | // - used for filenames and function names, each of which will be |
| 169 | // pointed to by one or more CCs. |
| 170 | // - it also allows equality checks just by pointer comparison, which |
| 171 | // is good when printing the output file at the end. |
| 172 | |
| 173 | static OSet* stringTable; |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 174 | |
| 175 | //------------------------------------------------------------ |
| 176 | // Stats |
sewardj | 4f29ddf | 2002-05-03 22:29:04 +0000 | [diff] [blame] | 177 | static Int distinct_files = 0; |
| 178 | static Int distinct_fns = 0; |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 179 | static Int distinct_lines = 0; |
sewardj | 4f29ddf | 2002-05-03 22:29:04 +0000 | [diff] [blame] | 180 | static Int distinct_instrs = 0; |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 181 | |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 182 | static Int full_debugs = 0; |
| 183 | static Int file_line_debugs = 0; |
| 184 | static Int fn_debugs = 0; |
| 185 | static Int no_debugs = 0; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 186 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 187 | /*------------------------------------------------------------*/ |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 188 | /*--- String table operations ---*/ |
| 189 | /*------------------------------------------------------------*/ |
| 190 | |
tom | 5a835d5 | 2007-12-30 12:28:26 +0000 | [diff] [blame] | 191 | static Word stringCmp( const void* key, const void* elem ) |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 192 | { |
| 193 | return VG_(strcmp)(*(Char**)key, *(Char**)elem); |
| 194 | } |
| 195 | |
| 196 | // Get a permanent string; either pull it out of the string table if it's |
| 197 | // been encountered before, or dup it and put it into the string table. |
| 198 | static Char* get_perm_string(Char* s) |
| 199 | { |
njn | e2a9ad3 | 2007-09-17 05:30:48 +0000 | [diff] [blame] | 200 | Char** s_ptr = VG_(OSetGen_Lookup)(stringTable, &s); |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 201 | if (s_ptr) { |
| 202 | return *s_ptr; |
| 203 | } else { |
njn | e2a9ad3 | 2007-09-17 05:30:48 +0000 | [diff] [blame] | 204 | Char** s_node = VG_(OSetGen_AllocNode)(stringTable, sizeof(Char*)); |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 205 | *s_node = VG_(strdup)("cg.main.gps.1", s); |
njn | e2a9ad3 | 2007-09-17 05:30:48 +0000 | [diff] [blame] | 206 | VG_(OSetGen_Insert)(stringTable, s_node); |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 207 | return *s_node; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | /*------------------------------------------------------------*/ |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 212 | /*--- CC table operations ---*/ |
| 213 | /*------------------------------------------------------------*/ |
njn | 4294fd4 | 2002-06-05 14:41:10 +0000 | [diff] [blame] | 214 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 215 | static void get_debug_info(Addr instr_addr, Char file[FILE_LEN], |
| 216 | Char fn[FN_LEN], Int* line) |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 217 | { |
njn | f3b61d6 | 2007-09-17 00:41:07 +0000 | [diff] [blame] | 218 | Char dir[FILE_LEN]; |
| 219 | Bool found_dirname; |
sewardj | 7cee6f9 | 2005-06-13 17:39:06 +0000 | [diff] [blame] | 220 | Bool found_file_line = VG_(get_filename_linenum)( |
| 221 | instr_addr, |
| 222 | file, FILE_LEN, |
njn | f3b61d6 | 2007-09-17 00:41:07 +0000 | [diff] [blame] | 223 | dir, FILE_LEN, &found_dirname, |
sewardj | 7cee6f9 | 2005-06-13 17:39:06 +0000 | [diff] [blame] | 224 | line |
| 225 | ); |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 226 | Bool found_fn = VG_(get_fnname)(instr_addr, fn, FN_LEN); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 227 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 228 | if (!found_file_line) { |
| 229 | VG_(strcpy)(file, "???"); |
| 230 | *line = 0; |
| 231 | } |
| 232 | if (!found_fn) { |
| 233 | VG_(strcpy)(fn, "???"); |
| 234 | } |
njn | f3b61d6 | 2007-09-17 00:41:07 +0000 | [diff] [blame] | 235 | |
| 236 | if (found_dirname) { |
| 237 | // +1 for the '/'. |
| 238 | tl_assert(VG_(strlen)(dir) + VG_(strlen)(file) + 1 < FILE_LEN); |
| 239 | VG_(strcat)(dir, "/"); // Append '/' |
| 240 | VG_(strcat)(dir, file); // Append file to dir |
| 241 | VG_(strcpy)(file, dir); // Move dir+file to file |
| 242 | } |
| 243 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 244 | if (found_file_line) { |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 245 | if (found_fn) full_debugs++; |
| 246 | else file_line_debugs++; |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 247 | } else { |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 248 | if (found_fn) fn_debugs++; |
| 249 | else no_debugs++; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 253 | // Do a three step traversal: by file, then fn, then line. |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 254 | // Returns a pointer to the line CC, creates a new one if necessary. |
| 255 | static LineCC* get_lineCC(Addr origAddr) |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 256 | { |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 257 | Char file[FILE_LEN], fn[FN_LEN]; |
| 258 | Int line; |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 259 | CodeLoc loc; |
| 260 | LineCC* lineCC; |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 261 | |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 262 | get_debug_info(origAddr, file, fn, &line); |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 263 | |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 264 | loc.file = file; |
| 265 | loc.fn = fn; |
| 266 | loc.line = line; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 267 | |
njn | e2a9ad3 | 2007-09-17 05:30:48 +0000 | [diff] [blame] | 268 | lineCC = VG_(OSetGen_Lookup)(CC_table, &loc); |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 269 | if (!lineCC) { |
| 270 | // Allocate and zero a new node. |
njn | e2a9ad3 | 2007-09-17 05:30:48 +0000 | [diff] [blame] | 271 | lineCC = VG_(OSetGen_AllocNode)(CC_table, sizeof(LineCC)); |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 272 | lineCC->loc.file = get_perm_string(loc.file); |
| 273 | lineCC->loc.fn = get_perm_string(loc.fn); |
| 274 | lineCC->loc.line = loc.line; |
njn | 0a8db5c | 2007-04-02 03:11:41 +0000 | [diff] [blame] | 275 | lineCC->Ir.a = 0; |
| 276 | lineCC->Ir.m1 = 0; |
njn | 2d853a1 | 2010-10-06 22:46:31 +0000 | [diff] [blame] | 277 | lineCC->Ir.mL = 0; |
njn | 0a8db5c | 2007-04-02 03:11:41 +0000 | [diff] [blame] | 278 | lineCC->Dr.a = 0; |
| 279 | lineCC->Dr.m1 = 0; |
njn | 2d853a1 | 2010-10-06 22:46:31 +0000 | [diff] [blame] | 280 | lineCC->Dr.mL = 0; |
njn | 0a8db5c | 2007-04-02 03:11:41 +0000 | [diff] [blame] | 281 | lineCC->Dw.a = 0; |
| 282 | lineCC->Dw.m1 = 0; |
njn | 2d853a1 | 2010-10-06 22:46:31 +0000 | [diff] [blame] | 283 | lineCC->Dw.mL = 0; |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 284 | lineCC->Bc.b = 0; |
| 285 | lineCC->Bc.mp = 0; |
| 286 | lineCC->Bi.b = 0; |
| 287 | lineCC->Bi.mp = 0; |
njn | e2a9ad3 | 2007-09-17 05:30:48 +0000 | [diff] [blame] | 288 | VG_(OSetGen_Insert)(CC_table, lineCC); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 289 | } |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 290 | |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 291 | return lineCC; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | /*------------------------------------------------------------*/ |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 295 | /*--- Cache simulation functions ---*/ |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 296 | /*------------------------------------------------------------*/ |
| 297 | |
njn | c52b932 | 2010-09-27 02:20:38 +0000 | [diff] [blame] | 298 | // Only used with --cache-sim=no. |
| 299 | static VG_REGPARM(1) |
| 300 | void log_1I(InstrInfo* n) |
| 301 | { |
| 302 | n->parent->Ir.a++; |
| 303 | } |
| 304 | |
| 305 | // Only used with --cache-sim=no. |
| 306 | static VG_REGPARM(2) |
| 307 | void log_2I(InstrInfo* n, InstrInfo* n2) |
| 308 | { |
| 309 | n->parent->Ir.a++; |
| 310 | n2->parent->Ir.a++; |
| 311 | } |
| 312 | |
| 313 | // Only used with --cache-sim=no. |
| 314 | static VG_REGPARM(3) |
| 315 | void log_3I(InstrInfo* n, InstrInfo* n2, InstrInfo* n3) |
| 316 | { |
| 317 | n->parent->Ir.a++; |
| 318 | n2->parent->Ir.a++; |
| 319 | n3->parent->Ir.a++; |
| 320 | } |
| 321 | |
njn | af839f5 | 2005-06-23 03:27:57 +0000 | [diff] [blame] | 322 | static VG_REGPARM(1) |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 323 | void log_1I_0D_cache_access(InstrInfo* n) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 324 | { |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 325 | //VG_(printf)("1I_0D : CCaddr=0x%010lx, iaddr=0x%010lx, isize=%lu\n", |
| 326 | // n, n->instr_addr, n->instr_len); |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 327 | cachesim_I1_doref(n->instr_addr, n->instr_len, |
njn | 2d853a1 | 2010-10-06 22:46:31 +0000 | [diff] [blame] | 328 | &n->parent->Ir.m1, &n->parent->Ir.mL); |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 329 | n->parent->Ir.a++; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 330 | } |
| 331 | |
njn | af839f5 | 2005-06-23 03:27:57 +0000 | [diff] [blame] | 332 | static VG_REGPARM(2) |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 333 | void log_2I_0D_cache_access(InstrInfo* n, InstrInfo* n2) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 334 | { |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 335 | //VG_(printf)("2I_0D : CC1addr=0x%010lx, i1addr=0x%010lx, i1size=%lu\n" |
| 336 | // " CC2addr=0x%010lx, i2addr=0x%010lx, i2size=%lu\n", |
| 337 | // n, n->instr_addr, n->instr_len, |
| 338 | // n2, n2->instr_addr, n2->instr_len); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 339 | cachesim_I1_doref(n->instr_addr, n->instr_len, |
njn | 2d853a1 | 2010-10-06 22:46:31 +0000 | [diff] [blame] | 340 | &n->parent->Ir.m1, &n->parent->Ir.mL); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 341 | n->parent->Ir.a++; |
| 342 | cachesim_I1_doref(n2->instr_addr, n2->instr_len, |
njn | 2d853a1 | 2010-10-06 22:46:31 +0000 | [diff] [blame] | 343 | &n2->parent->Ir.m1, &n2->parent->Ir.mL); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 344 | n2->parent->Ir.a++; |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | static VG_REGPARM(3) |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 348 | void log_3I_0D_cache_access(InstrInfo* n, InstrInfo* n2, InstrInfo* n3) |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 349 | { |
| 350 | //VG_(printf)("3I_0D : CC1addr=0x%010lx, i1addr=0x%010lx, i1size=%lu\n" |
| 351 | // " CC2addr=0x%010lx, i2addr=0x%010lx, i2size=%lu\n" |
| 352 | // " CC3addr=0x%010lx, i3addr=0x%010lx, i3size=%lu\n", |
| 353 | // n, n->instr_addr, n->instr_len, |
| 354 | // n2, n2->instr_addr, n2->instr_len, |
| 355 | // n3, n3->instr_addr, n3->instr_len); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 356 | cachesim_I1_doref(n->instr_addr, n->instr_len, |
njn | 2d853a1 | 2010-10-06 22:46:31 +0000 | [diff] [blame] | 357 | &n->parent->Ir.m1, &n->parent->Ir.mL); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 358 | n->parent->Ir.a++; |
| 359 | cachesim_I1_doref(n2->instr_addr, n2->instr_len, |
njn | 2d853a1 | 2010-10-06 22:46:31 +0000 | [diff] [blame] | 360 | &n2->parent->Ir.m1, &n2->parent->Ir.mL); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 361 | n2->parent->Ir.a++; |
| 362 | cachesim_I1_doref(n3->instr_addr, n3->instr_len, |
njn | 2d853a1 | 2010-10-06 22:46:31 +0000 | [diff] [blame] | 363 | &n3->parent->Ir.m1, &n3->parent->Ir.mL); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 364 | n3->parent->Ir.a++; |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | static VG_REGPARM(3) |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 368 | void log_1I_1Dr_cache_access(InstrInfo* n, Addr data_addr, Word data_size) |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 369 | { |
| 370 | //VG_(printf)("1I_1Dr: CCaddr=0x%010lx, iaddr=0x%010lx, isize=%lu\n" |
| 371 | // " daddr=0x%010lx, dsize=%lu\n", |
| 372 | // n, n->instr_addr, n->instr_len, data_addr, data_size); |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 373 | cachesim_I1_doref(n->instr_addr, n->instr_len, |
njn | 2d853a1 | 2010-10-06 22:46:31 +0000 | [diff] [blame] | 374 | &n->parent->Ir.m1, &n->parent->Ir.mL); |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 375 | n->parent->Ir.a++; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 376 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 377 | cachesim_D1_doref(data_addr, data_size, |
njn | 2d853a1 | 2010-10-06 22:46:31 +0000 | [diff] [blame] | 378 | &n->parent->Dr.m1, &n->parent->Dr.mL); |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 379 | n->parent->Dr.a++; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 380 | } |
| 381 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 382 | static VG_REGPARM(3) |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 383 | void log_1I_1Dw_cache_access(InstrInfo* n, Addr data_addr, Word data_size) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 384 | { |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 385 | //VG_(printf)("1I_1Dw: CCaddr=0x%010lx, iaddr=0x%010lx, isize=%lu\n" |
| 386 | // " daddr=0x%010lx, dsize=%lu\n", |
| 387 | // n, n->instr_addr, n->instr_len, data_addr, data_size); |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 388 | cachesim_I1_doref(n->instr_addr, n->instr_len, |
njn | 2d853a1 | 2010-10-06 22:46:31 +0000 | [diff] [blame] | 389 | &n->parent->Ir.m1, &n->parent->Ir.mL); |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 390 | n->parent->Ir.a++; |
| 391 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 392 | cachesim_D1_doref(data_addr, data_size, |
njn | 2d853a1 | 2010-10-06 22:46:31 +0000 | [diff] [blame] | 393 | &n->parent->Dw.m1, &n->parent->Dw.mL); |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 394 | n->parent->Dw.a++; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 395 | } |
| 396 | |
njn | af839f5 | 2005-06-23 03:27:57 +0000 | [diff] [blame] | 397 | static VG_REGPARM(3) |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 398 | void log_0I_1Dr_cache_access(InstrInfo* n, Addr data_addr, Word data_size) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 399 | { |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 400 | //VG_(printf)("0I_1Dr: CCaddr=0x%010lx, daddr=0x%010lx, dsize=%lu\n", |
| 401 | // n, data_addr, data_size); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 402 | cachesim_D1_doref(data_addr, data_size, |
njn | 2d853a1 | 2010-10-06 22:46:31 +0000 | [diff] [blame] | 403 | &n->parent->Dr.m1, &n->parent->Dr.mL); |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 404 | n->parent->Dr.a++; |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | static VG_REGPARM(3) |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 408 | void log_0I_1Dw_cache_access(InstrInfo* n, Addr data_addr, Word data_size) |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 409 | { |
| 410 | //VG_(printf)("0I_1Dw: CCaddr=0x%010lx, daddr=0x%010lx, dsize=%lu\n", |
| 411 | // n, data_addr, data_size); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 412 | cachesim_D1_doref(data_addr, data_size, |
njn | 2d853a1 | 2010-10-06 22:46:31 +0000 | [diff] [blame] | 413 | &n->parent->Dw.m1, &n->parent->Dw.mL); |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 414 | n->parent->Dw.a++; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 415 | } |
| 416 | |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 417 | /* For branches, we consult two different predictors, one which |
| 418 | predicts taken/untaken for conditional branches, and the other |
| 419 | which predicts the branch target address for indirect branches |
| 420 | (jump-to-register style ones). */ |
| 421 | |
| 422 | static VG_REGPARM(2) |
| 423 | void log_cond_branch(InstrInfo* n, Word taken) |
| 424 | { |
| 425 | //VG_(printf)("cbrnch: CCaddr=0x%010lx, taken=0x%010lx\n", |
| 426 | // n, taken); |
| 427 | n->parent->Bc.b++; |
| 428 | n->parent->Bc.mp |
| 429 | += (1 & do_cond_branch_predict(n->instr_addr, taken)); |
| 430 | } |
| 431 | |
| 432 | static VG_REGPARM(2) |
| 433 | void log_ind_branch(InstrInfo* n, UWord actual_dst) |
| 434 | { |
| 435 | //VG_(printf)("ibrnch: CCaddr=0x%010lx, dst=0x%010lx\n", |
| 436 | // n, actual_dst); |
| 437 | n->parent->Bi.b++; |
| 438 | n->parent->Bi.mp |
| 439 | += (1 & do_ind_branch_predict(n->instr_addr, actual_dst)); |
| 440 | } |
| 441 | |
| 442 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 443 | /*------------------------------------------------------------*/ |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 444 | /*--- Instrumentation types and structures ---*/ |
| 445 | /*------------------------------------------------------------*/ |
| 446 | |
| 447 | /* Maintain an ordered list of memory events which are outstanding, in |
| 448 | the sense that no IR has yet been generated to do the relevant |
| 449 | helper calls. The BB is scanned top to bottom and memory events |
| 450 | are added to the end of the list, merging with the most recent |
| 451 | notified event where possible (Dw immediately following Dr and |
| 452 | having the same size and EA can be merged). |
| 453 | |
| 454 | This merging is done so that for architectures which have |
| 455 | load-op-store instructions (x86, amd64), the insn is treated as if |
| 456 | it makes just one memory reference (a modify), rather than two (a |
| 457 | read followed by a write at the same address). |
| 458 | |
| 459 | At various points the list will need to be flushed, that is, IR |
| 460 | generated from it. That must happen before any possible exit from |
| 461 | the block (the end, or an IRStmt_Exit). Flushing also takes place |
| 462 | when there is no space to add a new event. |
| 463 | |
| 464 | If we require the simulation statistics to be up to date with |
| 465 | respect to possible memory exceptions, then the list would have to |
| 466 | be flushed before each memory reference. That would however lose |
| 467 | performance by inhibiting event-merging during flushing. |
| 468 | |
| 469 | Flushing the list consists of walking it start to end and emitting |
| 470 | instrumentation IR for each event, in the order in which they |
| 471 | appear. It may be possible to emit a single call for two adjacent |
| 472 | events in order to reduce the number of helper function calls made. |
| 473 | For example, it could well be profitable to handle two adjacent Ir |
| 474 | events with a single helper call. */ |
| 475 | |
| 476 | typedef |
| 477 | IRExpr |
| 478 | IRAtom; |
| 479 | |
| 480 | typedef |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 481 | enum { |
| 482 | Ev_Ir, // Instruction read |
| 483 | Ev_Dr, // Data read |
| 484 | Ev_Dw, // Data write |
| 485 | Ev_Dm, // Data modify (read then write) |
| 486 | Ev_Bc, // branch conditional |
| 487 | Ev_Bi // branch indirect (to unknown destination) |
| 488 | } |
| 489 | EventTag; |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 490 | |
| 491 | typedef |
| 492 | struct { |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 493 | EventTag tag; |
| 494 | InstrInfo* inode; |
| 495 | union { |
| 496 | struct { |
| 497 | } Ir; |
| 498 | struct { |
| 499 | IRAtom* ea; |
| 500 | Int szB; |
| 501 | } Dr; |
| 502 | struct { |
| 503 | IRAtom* ea; |
| 504 | Int szB; |
| 505 | } Dw; |
| 506 | struct { |
| 507 | IRAtom* ea; |
| 508 | Int szB; |
| 509 | } Dm; |
| 510 | struct { |
| 511 | IRAtom* taken; /* :: Ity_I1 */ |
| 512 | } Bc; |
| 513 | struct { |
| 514 | IRAtom* dst; |
| 515 | } Bi; |
| 516 | } Ev; |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 517 | } |
| 518 | Event; |
| 519 | |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 520 | static void init_Event ( Event* ev ) { |
| 521 | VG_(memset)(ev, 0, sizeof(Event)); |
| 522 | } |
| 523 | |
| 524 | static IRAtom* get_Event_dea ( Event* ev ) { |
| 525 | switch (ev->tag) { |
| 526 | case Ev_Dr: return ev->Ev.Dr.ea; |
| 527 | case Ev_Dw: return ev->Ev.Dw.ea; |
| 528 | case Ev_Dm: return ev->Ev.Dm.ea; |
| 529 | default: tl_assert(0); |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | static Int get_Event_dszB ( Event* ev ) { |
| 534 | switch (ev->tag) { |
| 535 | case Ev_Dr: return ev->Ev.Dr.szB; |
| 536 | case Ev_Dw: return ev->Ev.Dw.szB; |
| 537 | case Ev_Dm: return ev->Ev.Dm.szB; |
| 538 | default: tl_assert(0); |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 543 | /* Up to this many unnotified events are allowed. Number is |
| 544 | arbitrary. Larger numbers allow more event merging to occur, but |
| 545 | potentially induce more spilling due to extending live ranges of |
| 546 | address temporaries. */ |
| 547 | #define N_EVENTS 16 |
| 548 | |
| 549 | |
| 550 | /* A struct which holds all the running state during instrumentation. |
| 551 | Mostly to avoid passing loads of parameters everywhere. */ |
| 552 | typedef |
| 553 | struct { |
| 554 | /* The current outstanding-memory-event list. */ |
| 555 | Event events[N_EVENTS]; |
| 556 | Int events_used; |
| 557 | |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 558 | /* The array of InstrInfo bins for the BB. */ |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 559 | SB_info* sbInfo; |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 560 | |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 561 | /* Number InstrInfo bins 'used' so far. */ |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 562 | Int sbInfo_i; |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 563 | |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 564 | /* The output SB being constructed. */ |
| 565 | IRSB* sbOut; |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 566 | } |
| 567 | CgState; |
| 568 | |
| 569 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 570 | /*------------------------------------------------------------*/ |
| 571 | /*--- Instrumentation main ---*/ |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 572 | /*------------------------------------------------------------*/ |
| 573 | |
sewardj | 4ba057c | 2005-10-18 12:04:18 +0000 | [diff] [blame] | 574 | // Note that origAddr is the real origAddr, not the address of the first |
| 575 | // instruction in the block (they can be different due to redirection). |
nethercote | 564b2b0 | 2004-08-07 15:54:53 +0000 | [diff] [blame] | 576 | static |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 577 | SB_info* get_SB_info(IRSB* sbIn, Addr origAddr) |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 578 | { |
njn | 4bd67b5 | 2005-08-11 00:47:10 +0000 | [diff] [blame] | 579 | Int i, n_instrs; |
| 580 | IRStmt* st; |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 581 | SB_info* sbInfo; |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 582 | |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 583 | // Count number of original instrs in SB |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 584 | n_instrs = 0; |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 585 | for (i = 0; i < sbIn->stmts_used; i++) { |
| 586 | st = sbIn->stmts[i]; |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 587 | if (Ist_IMark == st->tag) n_instrs++; |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 588 | } |
| 589 | |
njn | f7d2609 | 2005-10-12 16:45:17 +0000 | [diff] [blame] | 590 | // Check that we don't have an entry for this BB in the instr-info table. |
| 591 | // If this assertion fails, there has been some screwup: some |
| 592 | // translations must have been discarded but Cachegrind hasn't discarded |
| 593 | // the corresponding entries in the instr-info table. |
njn | e2a9ad3 | 2007-09-17 05:30:48 +0000 | [diff] [blame] | 594 | sbInfo = VG_(OSetGen_Lookup)(instrInfoTable, &origAddr); |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 595 | tl_assert(NULL == sbInfo); |
sewardj | a3a29a5 | 2005-10-12 16:16:03 +0000 | [diff] [blame] | 596 | |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 597 | // BB never translated before (at this address, at least; could have |
| 598 | // been unloaded and then reloaded elsewhere in memory) |
njn | e2a9ad3 | 2007-09-17 05:30:48 +0000 | [diff] [blame] | 599 | sbInfo = VG_(OSetGen_AllocNode)(instrInfoTable, |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 600 | sizeof(SB_info) + n_instrs*sizeof(InstrInfo)); |
| 601 | sbInfo->SB_addr = origAddr; |
| 602 | sbInfo->n_instrs = n_instrs; |
njn | e2a9ad3 | 2007-09-17 05:30:48 +0000 | [diff] [blame] | 603 | VG_(OSetGen_Insert)( instrInfoTable, sbInfo ); |
sewardj | a3a29a5 | 2005-10-12 16:16:03 +0000 | [diff] [blame] | 604 | distinct_instrs++; |
| 605 | |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 606 | return sbInfo; |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 607 | } |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 608 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 609 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 610 | static void showEvent ( Event* ev ) |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 611 | { |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 612 | switch (ev->tag) { |
| 613 | case Ev_Ir: |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 614 | VG_(printf)("Ir %p\n", ev->inode); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 615 | break; |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 616 | case Ev_Dr: |
| 617 | VG_(printf)("Dr %p %d EA=", ev->inode, ev->Ev.Dr.szB); |
| 618 | ppIRExpr(ev->Ev.Dr.ea); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 619 | VG_(printf)("\n"); |
| 620 | break; |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 621 | case Ev_Dw: |
| 622 | VG_(printf)("Dw %p %d EA=", ev->inode, ev->Ev.Dw.szB); |
| 623 | ppIRExpr(ev->Ev.Dw.ea); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 624 | VG_(printf)("\n"); |
| 625 | break; |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 626 | case Ev_Dm: |
| 627 | VG_(printf)("Dm %p %d EA=", ev->inode, ev->Ev.Dm.szB); |
| 628 | ppIRExpr(ev->Ev.Dm.ea); |
| 629 | VG_(printf)("\n"); |
| 630 | break; |
| 631 | case Ev_Bc: |
| 632 | VG_(printf)("Bc %p GA=", ev->inode); |
| 633 | ppIRExpr(ev->Ev.Bc.taken); |
| 634 | VG_(printf)("\n"); |
| 635 | break; |
| 636 | case Ev_Bi: |
| 637 | VG_(printf)("Bi %p DST=", ev->inode); |
| 638 | ppIRExpr(ev->Ev.Bi.dst); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 639 | VG_(printf)("\n"); |
| 640 | break; |
| 641 | default: |
| 642 | tl_assert(0); |
| 643 | break; |
| 644 | } |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 645 | } |
| 646 | |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 647 | // Reserve and initialise an InstrInfo for the first mention of a new insn. |
| 648 | static |
| 649 | InstrInfo* setup_InstrInfo ( CgState* cgs, Addr instr_addr, UInt instr_len ) |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 650 | { |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 651 | InstrInfo* i_node; |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 652 | tl_assert(cgs->sbInfo_i >= 0); |
| 653 | tl_assert(cgs->sbInfo_i < cgs->sbInfo->n_instrs); |
| 654 | i_node = &cgs->sbInfo->instrs[ cgs->sbInfo_i ]; |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 655 | i_node->instr_addr = instr_addr; |
| 656 | i_node->instr_len = instr_len; |
| 657 | i_node->parent = get_lineCC(instr_addr); |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 658 | cgs->sbInfo_i++; |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 659 | return i_node; |
| 660 | } |
sewardj | 17a56bf | 2005-03-21 01:35:02 +0000 | [diff] [blame] | 661 | |
sewardj | 17a56bf | 2005-03-21 01:35:02 +0000 | [diff] [blame] | 662 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 663 | /* Generate code for all outstanding memory events, and mark the queue |
| 664 | empty. Code is generated into cgs->bbOut, and this activity |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 665 | 'consumes' slots in cgs->sbInfo. */ |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 666 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 667 | static void flushEvents ( CgState* cgs ) |
| 668 | { |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 669 | Int i, regparms; |
| 670 | Char* helperName; |
| 671 | void* helperAddr; |
| 672 | IRExpr** argv; |
| 673 | IRExpr* i_node_expr; |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 674 | IRDirty* di; |
njn | c285dca | 2005-10-15 22:07:28 +0000 | [diff] [blame] | 675 | Event* ev; |
| 676 | Event* ev2; |
| 677 | Event* ev3; |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 678 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 679 | i = 0; |
| 680 | while (i < cgs->events_used) { |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 681 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 682 | helperName = NULL; |
| 683 | helperAddr = NULL; |
| 684 | argv = NULL; |
| 685 | regparms = 0; |
| 686 | |
| 687 | /* generate IR to notify event i and possibly the ones |
| 688 | immediately following it. */ |
| 689 | tl_assert(i >= 0 && i < cgs->events_used); |
njn | c285dca | 2005-10-15 22:07:28 +0000 | [diff] [blame] | 690 | |
| 691 | ev = &cgs->events[i]; |
| 692 | ev2 = ( i < cgs->events_used-1 ? &cgs->events[i+1] : NULL ); |
| 693 | ev3 = ( i < cgs->events_used-2 ? &cgs->events[i+2] : NULL ); |
| 694 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 695 | if (DEBUG_CG) { |
| 696 | VG_(printf)(" flush "); |
njn | c285dca | 2005-10-15 22:07:28 +0000 | [diff] [blame] | 697 | showEvent( ev ); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 698 | } |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 699 | |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 700 | i_node_expr = mkIRExpr_HWord( (HWord)ev->inode ); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 701 | |
| 702 | /* Decide on helper fn to call and args to pass it, and advance |
| 703 | i appropriately. */ |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 704 | switch (ev->tag) { |
| 705 | case Ev_Ir: |
| 706 | /* Merge an Ir with a following Dr/Dm. */ |
| 707 | if (ev2 && (ev2->tag == Ev_Dr || ev2->tag == Ev_Dm)) { |
| 708 | /* Why is this true? It's because we're merging an Ir |
| 709 | with a following Dr or Dm. The Ir derives from the |
| 710 | instruction's IMark and the Dr/Dm from data |
| 711 | references which follow it. In short it holds |
| 712 | because each insn starts with an IMark, hence an |
| 713 | Ev_Ir, and so these Dr/Dm must pertain to the |
| 714 | immediately preceding Ir. Same applies to analogous |
| 715 | assertions in the subsequent cases. */ |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 716 | tl_assert(ev2->inode == ev->inode); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 717 | helperName = "log_1I_1Dr_cache_access"; |
| 718 | helperAddr = &log_1I_1Dr_cache_access; |
| 719 | argv = mkIRExprVec_3( i_node_expr, |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 720 | get_Event_dea(ev2), |
| 721 | mkIRExpr_HWord( get_Event_dszB(ev2) ) ); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 722 | regparms = 3; |
| 723 | i += 2; |
| 724 | } |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 725 | /* Merge an Ir with a following Dw. */ |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 726 | else |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 727 | if (ev2 && ev2->tag == Ev_Dw) { |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 728 | tl_assert(ev2->inode == ev->inode); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 729 | helperName = "log_1I_1Dw_cache_access"; |
| 730 | helperAddr = &log_1I_1Dw_cache_access; |
| 731 | argv = mkIRExprVec_3( i_node_expr, |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 732 | get_Event_dea(ev2), |
| 733 | mkIRExpr_HWord( get_Event_dszB(ev2) ) ); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 734 | regparms = 3; |
| 735 | i += 2; |
| 736 | } |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 737 | /* Merge an Ir with two following Irs. */ |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 738 | else |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 739 | if (ev2 && ev3 && ev2->tag == Ev_Ir && ev3->tag == Ev_Ir) |
njn | c285dca | 2005-10-15 22:07:28 +0000 | [diff] [blame] | 740 | { |
njn | c52b932 | 2010-09-27 02:20:38 +0000 | [diff] [blame] | 741 | if (clo_cache_sim) { |
| 742 | helperName = "log_3I_0D_cache_access"; |
| 743 | helperAddr = &log_3I_0D_cache_access; |
| 744 | } else { |
| 745 | helperName = "log_3I"; |
| 746 | helperAddr = &log_3I; |
| 747 | } |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 748 | argv = mkIRExprVec_3( i_node_expr, |
| 749 | mkIRExpr_HWord( (HWord)ev2->inode ), |
| 750 | mkIRExpr_HWord( (HWord)ev3->inode ) ); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 751 | regparms = 3; |
| 752 | i += 3; |
| 753 | } |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 754 | /* Merge an Ir with one following Ir. */ |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 755 | else |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 756 | if (ev2 && ev2->tag == Ev_Ir) { |
njn | c52b932 | 2010-09-27 02:20:38 +0000 | [diff] [blame] | 757 | if (clo_cache_sim) { |
| 758 | helperName = "log_2I_0D_cache_access"; |
| 759 | helperAddr = &log_2I_0D_cache_access; |
| 760 | } else { |
| 761 | helperName = "log_2I"; |
| 762 | helperAddr = &log_2I; |
| 763 | } |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 764 | argv = mkIRExprVec_2( i_node_expr, |
| 765 | mkIRExpr_HWord( (HWord)ev2->inode ) ); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 766 | regparms = 2; |
| 767 | i += 2; |
| 768 | } |
| 769 | /* No merging possible; emit as-is. */ |
| 770 | else { |
njn | c52b932 | 2010-09-27 02:20:38 +0000 | [diff] [blame] | 771 | if (clo_cache_sim) { |
| 772 | helperName = "log_1I_0D_cache_access"; |
| 773 | helperAddr = &log_1I_0D_cache_access; |
| 774 | } else { |
| 775 | helperName = "log_1I"; |
| 776 | helperAddr = &log_1I; |
| 777 | } |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 778 | argv = mkIRExprVec_1( i_node_expr ); |
| 779 | regparms = 1; |
| 780 | i++; |
| 781 | } |
| 782 | break; |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 783 | case Ev_Dr: |
| 784 | case Ev_Dm: |
| 785 | /* Data read or modify */ |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 786 | helperName = "log_0I_1Dr_cache_access"; |
| 787 | helperAddr = &log_0I_1Dr_cache_access; |
| 788 | argv = mkIRExprVec_3( i_node_expr, |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 789 | get_Event_dea(ev), |
| 790 | mkIRExpr_HWord( get_Event_dszB(ev) ) ); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 791 | regparms = 3; |
| 792 | i++; |
| 793 | break; |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 794 | case Ev_Dw: |
| 795 | /* Data write */ |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 796 | helperName = "log_0I_1Dw_cache_access"; |
| 797 | helperAddr = &log_0I_1Dw_cache_access; |
| 798 | argv = mkIRExprVec_3( i_node_expr, |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 799 | get_Event_dea(ev), |
| 800 | mkIRExpr_HWord( get_Event_dszB(ev) ) ); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 801 | regparms = 3; |
| 802 | i++; |
| 803 | break; |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 804 | case Ev_Bc: |
| 805 | /* Conditional branch */ |
| 806 | helperName = "log_cond_branch"; |
| 807 | helperAddr = &log_cond_branch; |
| 808 | argv = mkIRExprVec_2( i_node_expr, ev->Ev.Bc.taken ); |
| 809 | regparms = 2; |
| 810 | i++; |
| 811 | break; |
| 812 | case Ev_Bi: |
| 813 | /* Branch to an unknown destination */ |
| 814 | helperName = "log_ind_branch"; |
| 815 | helperAddr = &log_ind_branch; |
| 816 | argv = mkIRExprVec_2( i_node_expr, ev->Ev.Bi.dst ); |
| 817 | regparms = 2; |
| 818 | i++; |
| 819 | break; |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 820 | default: |
| 821 | tl_assert(0); |
| 822 | } |
| 823 | |
| 824 | /* Add the helper. */ |
| 825 | tl_assert(helperName); |
| 826 | tl_assert(helperAddr); |
| 827 | tl_assert(argv); |
sewardj | 5bb8682 | 2005-12-23 12:47:42 +0000 | [diff] [blame] | 828 | di = unsafeIRDirty_0_N( regparms, |
| 829 | helperName, VG_(fnptr_to_fnentry)( helperAddr ), |
| 830 | argv ); |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 831 | addStmtToIRSB( cgs->sbOut, IRStmt_Dirty(di) ); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 832 | } |
| 833 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 834 | cgs->events_used = 0; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 835 | } |
njn | 14d01ce | 2004-11-26 11:30:14 +0000 | [diff] [blame] | 836 | |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 837 | static void addEvent_Ir ( CgState* cgs, InstrInfo* inode ) |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 838 | { |
| 839 | Event* evt; |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 840 | if (cgs->events_used == N_EVENTS) |
| 841 | flushEvents(cgs); |
| 842 | tl_assert(cgs->events_used >= 0 && cgs->events_used < N_EVENTS); |
| 843 | evt = &cgs->events[cgs->events_used]; |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 844 | init_Event(evt); |
| 845 | evt->tag = Ev_Ir; |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 846 | evt->inode = inode; |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 847 | cgs->events_used++; |
| 848 | } |
| 849 | |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 850 | static |
| 851 | void addEvent_Dr ( CgState* cgs, InstrInfo* inode, Int datasize, IRAtom* ea ) |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 852 | { |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 853 | Event* evt; |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 854 | tl_assert(isIRAtom(ea)); |
sewardj | 98763d5 | 2012-06-03 22:40:07 +0000 | [diff] [blame^] | 855 | tl_assert(datasize >= 1 && datasize <= min_line_size); |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 856 | if (!clo_cache_sim) |
| 857 | return; |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 858 | if (cgs->events_used == N_EVENTS) |
| 859 | flushEvents(cgs); |
| 860 | tl_assert(cgs->events_used >= 0 && cgs->events_used < N_EVENTS); |
| 861 | evt = &cgs->events[cgs->events_used]; |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 862 | init_Event(evt); |
| 863 | evt->tag = Ev_Dr; |
| 864 | evt->inode = inode; |
| 865 | evt->Ev.Dr.szB = datasize; |
| 866 | evt->Ev.Dr.ea = ea; |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 867 | cgs->events_used++; |
| 868 | } |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 869 | |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 870 | static |
| 871 | void addEvent_Dw ( CgState* cgs, InstrInfo* inode, Int datasize, IRAtom* ea ) |
| 872 | { |
| 873 | Event* lastEvt; |
| 874 | Event* evt; |
| 875 | |
| 876 | tl_assert(isIRAtom(ea)); |
sewardj | 98763d5 | 2012-06-03 22:40:07 +0000 | [diff] [blame^] | 877 | tl_assert(datasize >= 1 && datasize <= min_line_size); |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 878 | |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 879 | if (!clo_cache_sim) |
| 880 | return; |
| 881 | |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 882 | /* Is it possible to merge this write with the preceding read? */ |
| 883 | lastEvt = &cgs->events[cgs->events_used-1]; |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 884 | if (cgs->events_used > 0 |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 885 | && lastEvt->tag == Ev_Dr |
| 886 | && lastEvt->Ev.Dr.szB == datasize |
| 887 | && lastEvt->inode == inode |
| 888 | && eqIRAtom(lastEvt->Ev.Dr.ea, ea)) |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 889 | { |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 890 | lastEvt->tag = Ev_Dm; |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 891 | return; |
| 892 | } |
| 893 | |
| 894 | /* No. Add as normal. */ |
| 895 | if (cgs->events_used == N_EVENTS) |
| 896 | flushEvents(cgs); |
| 897 | tl_assert(cgs->events_used >= 0 && cgs->events_used < N_EVENTS); |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 898 | evt = &cgs->events[cgs->events_used]; |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 899 | init_Event(evt); |
| 900 | evt->tag = Ev_Dw; |
| 901 | evt->inode = inode; |
| 902 | evt->Ev.Dw.szB = datasize; |
| 903 | evt->Ev.Dw.ea = ea; |
| 904 | cgs->events_used++; |
| 905 | } |
| 906 | |
| 907 | static |
| 908 | void addEvent_Bc ( CgState* cgs, InstrInfo* inode, IRAtom* guard ) |
| 909 | { |
| 910 | Event* evt; |
| 911 | tl_assert(isIRAtom(guard)); |
| 912 | tl_assert(typeOfIRExpr(cgs->sbOut->tyenv, guard) |
| 913 | == (sizeof(HWord)==4 ? Ity_I32 : Ity_I64)); |
| 914 | if (!clo_branch_sim) |
| 915 | return; |
| 916 | if (cgs->events_used == N_EVENTS) |
| 917 | flushEvents(cgs); |
| 918 | tl_assert(cgs->events_used >= 0 && cgs->events_used < N_EVENTS); |
| 919 | evt = &cgs->events[cgs->events_used]; |
| 920 | init_Event(evt); |
| 921 | evt->tag = Ev_Bc; |
| 922 | evt->inode = inode; |
| 923 | evt->Ev.Bc.taken = guard; |
| 924 | cgs->events_used++; |
| 925 | } |
| 926 | |
| 927 | static |
| 928 | void addEvent_Bi ( CgState* cgs, InstrInfo* inode, IRAtom* whereTo ) |
| 929 | { |
| 930 | Event* evt; |
| 931 | tl_assert(isIRAtom(whereTo)); |
| 932 | tl_assert(typeOfIRExpr(cgs->sbOut->tyenv, whereTo) |
| 933 | == (sizeof(HWord)==4 ? Ity_I32 : Ity_I64)); |
| 934 | if (!clo_branch_sim) |
| 935 | return; |
| 936 | if (cgs->events_used == N_EVENTS) |
| 937 | flushEvents(cgs); |
| 938 | tl_assert(cgs->events_used >= 0 && cgs->events_used < N_EVENTS); |
| 939 | evt = &cgs->events[cgs->events_used]; |
| 940 | init_Event(evt); |
| 941 | evt->tag = Ev_Bi; |
| 942 | evt->inode = inode; |
| 943 | evt->Ev.Bi.dst = whereTo; |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 944 | cgs->events_used++; |
| 945 | } |
| 946 | |
| 947 | //////////////////////////////////////////////////////////// |
| 948 | |
| 949 | |
sewardj | 4ba057c | 2005-10-18 12:04:18 +0000 | [diff] [blame] | 950 | static |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 951 | IRSB* cg_instrument ( VgCallbackClosure* closure, |
| 952 | IRSB* sbIn, |
sewardj | 461df9c | 2006-01-17 02:06:39 +0000 | [diff] [blame] | 953 | VexGuestLayout* layout, |
| 954 | VexGuestExtents* vge, |
sewardj | 4ba057c | 2005-10-18 12:04:18 +0000 | [diff] [blame] | 955 | IRType gWordTy, IRType hWordTy ) |
njn | 14d01ce | 2004-11-26 11:30:14 +0000 | [diff] [blame] | 956 | { |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 957 | Int i, isize; |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 958 | IRStmt* st; |
| 959 | Addr64 cia; /* address of current insn */ |
| 960 | CgState cgs; |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 961 | IRTypeEnv* tyenv = sbIn->tyenv; |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 962 | InstrInfo* curr_inode = NULL; |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 963 | |
sewardj | d54babf | 2005-03-21 00:55:49 +0000 | [diff] [blame] | 964 | if (gWordTy != hWordTy) { |
| 965 | /* We don't currently support this case. */ |
| 966 | VG_(tool_panic)("host/guest word size mismatch"); |
| 967 | } |
| 968 | |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 969 | // Set up new SB |
| 970 | cgs.sbOut = deepCopyIRSBExceptStmts(sbIn); |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 971 | |
sewardj | a9f538c | 2005-10-23 12:06:55 +0000 | [diff] [blame] | 972 | // Copy verbatim any IR preamble preceding the first IMark |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 973 | i = 0; |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 974 | while (i < sbIn->stmts_used && sbIn->stmts[i]->tag != Ist_IMark) { |
| 975 | addStmtToIRSB( cgs.sbOut, sbIn->stmts[i] ); |
sewardj | a9f538c | 2005-10-23 12:06:55 +0000 | [diff] [blame] | 976 | i++; |
| 977 | } |
| 978 | |
| 979 | // Get the first statement, and initial cia from it |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 980 | tl_assert(sbIn->stmts_used > 0); |
| 981 | tl_assert(i < sbIn->stmts_used); |
| 982 | st = sbIn->stmts[i]; |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 983 | tl_assert(Ist_IMark == st->tag); |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 984 | |
| 985 | cia = st->Ist.IMark.addr; |
| 986 | isize = st->Ist.IMark.len; |
| 987 | // If Vex fails to decode an instruction, the size will be zero. |
| 988 | // Pretend otherwise. |
| 989 | if (isize == 0) isize = VG_MIN_INSTR_SZB; |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 990 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 991 | // Set up running state and get block info |
sewardj | 3a384b3 | 2006-01-22 01:12:51 +0000 | [diff] [blame] | 992 | tl_assert(closure->readdr == vge->base[0]); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 993 | cgs.events_used = 0; |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 994 | cgs.sbInfo = get_SB_info(sbIn, (Addr)closure->readdr); |
| 995 | cgs.sbInfo_i = 0; |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 996 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 997 | if (DEBUG_CG) |
| 998 | VG_(printf)("\n\n---------- cg_instrument ----------\n"); |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 999 | |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 1000 | // Traverse the block, initialising inodes, adding events and flushing as |
| 1001 | // necessary. |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 1002 | for (/*use current i*/; i < sbIn->stmts_used; i++) { |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 1003 | |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 1004 | st = sbIn->stmts[i]; |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 1005 | tl_assert(isFlatIRStmt(st)); |
njn | b3507ea | 2005-08-02 23:07:02 +0000 | [diff] [blame] | 1006 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 1007 | switch (st->tag) { |
| 1008 | case Ist_NoOp: |
| 1009 | case Ist_AbiHint: |
| 1010 | case Ist_Put: |
| 1011 | case Ist_PutI: |
sewardj | 72d7513 | 2007-11-09 23:06:35 +0000 | [diff] [blame] | 1012 | case Ist_MBE: |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 1013 | break; |
njn | 20677cc | 2005-08-12 23:47:51 +0000 | [diff] [blame] | 1014 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 1015 | case Ist_IMark: |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 1016 | cia = st->Ist.IMark.addr; |
| 1017 | isize = st->Ist.IMark.len; |
| 1018 | |
| 1019 | // If Vex fails to decode an instruction, the size will be zero. |
| 1020 | // Pretend otherwise. |
| 1021 | if (isize == 0) isize = VG_MIN_INSTR_SZB; |
| 1022 | |
njn | a5ad9ba | 2005-11-10 15:20:37 +0000 | [diff] [blame] | 1023 | // Sanity-check size. |
| 1024 | tl_assert( (VG_MIN_INSTR_SZB <= isize && isize <= VG_MAX_INSTR_SZB) |
| 1025 | || VG_CLREQ_SZB == isize ); |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 1026 | |
| 1027 | // Get space for and init the inode, record it as the current one. |
| 1028 | // Subsequent Dr/Dw/Dm events from the same instruction will |
| 1029 | // also use it. |
| 1030 | curr_inode = setup_InstrInfo(&cgs, cia, isize); |
| 1031 | |
| 1032 | addEvent_Ir( &cgs, curr_inode ); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 1033 | break; |
| 1034 | |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 1035 | case Ist_WrTmp: { |
| 1036 | IRExpr* data = st->Ist.WrTmp.data; |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 1037 | if (data->tag == Iex_Load) { |
| 1038 | IRExpr* aexpr = data->Iex.Load.addr; |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 1039 | // Note also, endianness info is ignored. I guess |
| 1040 | // that's not interesting. |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 1041 | addEvent_Dr( &cgs, curr_inode, sizeofIRType(data->Iex.Load.ty), |
| 1042 | aexpr ); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 1043 | } |
| 1044 | break; |
njn | b3507ea | 2005-08-02 23:07:02 +0000 | [diff] [blame] | 1045 | } |
| 1046 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 1047 | case Ist_Store: { |
| 1048 | IRExpr* data = st->Ist.Store.data; |
| 1049 | IRExpr* aexpr = st->Ist.Store.addr; |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 1050 | addEvent_Dw( &cgs, curr_inode, |
| 1051 | sizeofIRType(typeOfIRExpr(tyenv, data)), aexpr ); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 1052 | break; |
| 1053 | } |
njn | b3507ea | 2005-08-02 23:07:02 +0000 | [diff] [blame] | 1054 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 1055 | case Ist_Dirty: { |
| 1056 | Int dataSize; |
| 1057 | IRDirty* d = st->Ist.Dirty.details; |
| 1058 | if (d->mFx != Ifx_None) { |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 1059 | /* This dirty helper accesses memory. Collect the details. */ |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 1060 | tl_assert(d->mAddr != NULL); |
| 1061 | tl_assert(d->mSize != 0); |
| 1062 | dataSize = d->mSize; |
| 1063 | // Large (eg. 28B, 108B, 512B on x86) data-sized |
| 1064 | // instructions will be done inaccurately, but they're |
| 1065 | // very rare and this avoids errors from hitting more |
| 1066 | // than two cache lines in the simulation. |
sewardj | 98763d5 | 2012-06-03 22:40:07 +0000 | [diff] [blame^] | 1067 | if (dataSize > min_line_size) |
| 1068 | dataSize = min_line_size; |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 1069 | if (d->mFx == Ifx_Read || d->mFx == Ifx_Modify) |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 1070 | addEvent_Dr( &cgs, curr_inode, dataSize, d->mAddr ); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 1071 | if (d->mFx == Ifx_Write || d->mFx == Ifx_Modify) |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 1072 | addEvent_Dw( &cgs, curr_inode, dataSize, d->mAddr ); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 1073 | } else { |
| 1074 | tl_assert(d->mAddr == NULL); |
| 1075 | tl_assert(d->mSize == 0); |
| 1076 | } |
| 1077 | break; |
| 1078 | } |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 1079 | |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 1080 | case Ist_CAS: { |
| 1081 | /* We treat it as a read and a write of the location. I |
| 1082 | think that is the same behaviour as it was before IRCAS |
| 1083 | was introduced, since prior to that point, the Vex |
| 1084 | front ends would translate a lock-prefixed instruction |
| 1085 | into a (normal) read followed by a (normal) write. */ |
| 1086 | Int dataSize; |
| 1087 | IRCAS* cas = st->Ist.CAS.details; |
| 1088 | tl_assert(cas->addr != NULL); |
| 1089 | tl_assert(cas->dataLo != NULL); |
| 1090 | dataSize = sizeofIRType(typeOfIRExpr(tyenv, cas->dataLo)); |
| 1091 | if (cas->dataHi != NULL) |
| 1092 | dataSize *= 2; /* since it's a doubleword-CAS */ |
| 1093 | /* I don't think this can ever happen, but play safe. */ |
sewardj | 98763d5 | 2012-06-03 22:40:07 +0000 | [diff] [blame^] | 1094 | if (dataSize > min_line_size) |
| 1095 | dataSize = min_line_size; |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 1096 | addEvent_Dr( &cgs, curr_inode, dataSize, cas->addr ); |
| 1097 | addEvent_Dw( &cgs, curr_inode, dataSize, cas->addr ); |
| 1098 | break; |
| 1099 | } |
| 1100 | |
sewardj | db5907d | 2009-11-26 17:20:21 +0000 | [diff] [blame] | 1101 | case Ist_LLSC: { |
| 1102 | IRType dataTy; |
| 1103 | if (st->Ist.LLSC.storedata == NULL) { |
| 1104 | /* LL */ |
| 1105 | dataTy = typeOfIRTemp(tyenv, st->Ist.LLSC.result); |
| 1106 | addEvent_Dr( &cgs, curr_inode, |
| 1107 | sizeofIRType(dataTy), st->Ist.LLSC.addr ); |
| 1108 | } else { |
| 1109 | /* SC */ |
| 1110 | dataTy = typeOfIRExpr(tyenv, st->Ist.LLSC.storedata); |
| 1111 | addEvent_Dw( &cgs, curr_inode, |
| 1112 | sizeofIRType(dataTy), st->Ist.LLSC.addr ); |
| 1113 | } |
| 1114 | break; |
| 1115 | } |
| 1116 | |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 1117 | case Ist_Exit: { |
weidendo | 374a48f | 2010-09-02 17:06:49 +0000 | [diff] [blame] | 1118 | // call branch predictor only if this is a branch in guest code |
| 1119 | if ( (st->Ist.Exit.jk == Ijk_Boring) || |
| 1120 | (st->Ist.Exit.jk == Ijk_Call) || |
| 1121 | (st->Ist.Exit.jk == Ijk_Ret) ) |
| 1122 | { |
| 1123 | /* Stuff to widen the guard expression to a host word, so |
| 1124 | we can pass it to the branch predictor simulation |
| 1125 | functions easily. */ |
| 1126 | Bool inverted; |
| 1127 | Addr64 nia, sea; |
| 1128 | IRConst* dst; |
| 1129 | IRType tyW = hWordTy; |
| 1130 | IROp widen = tyW==Ity_I32 ? Iop_1Uto32 : Iop_1Uto64; |
| 1131 | IROp opXOR = tyW==Ity_I32 ? Iop_Xor32 : Iop_Xor64; |
| 1132 | IRTemp guard1 = newIRTemp(cgs.sbOut->tyenv, Ity_I1); |
| 1133 | IRTemp guardW = newIRTemp(cgs.sbOut->tyenv, tyW); |
| 1134 | IRTemp guard = newIRTemp(cgs.sbOut->tyenv, tyW); |
| 1135 | IRExpr* one = tyW==Ity_I32 ? IRExpr_Const(IRConst_U32(1)) |
| 1136 | : IRExpr_Const(IRConst_U64(1)); |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 1137 | |
weidendo | 374a48f | 2010-09-02 17:06:49 +0000 | [diff] [blame] | 1138 | /* First we need to figure out whether the side exit got |
| 1139 | inverted by the ir optimiser. To do that, figure out |
| 1140 | the next (fallthrough) instruction's address and the |
| 1141 | side exit address and see if they are the same. */ |
| 1142 | nia = cia + (Addr64)isize; |
| 1143 | if (tyW == Ity_I32) |
| 1144 | nia &= 0xFFFFFFFFULL; |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 1145 | |
weidendo | 374a48f | 2010-09-02 17:06:49 +0000 | [diff] [blame] | 1146 | /* Side exit address */ |
| 1147 | dst = st->Ist.Exit.dst; |
| 1148 | if (tyW == Ity_I32) { |
| 1149 | tl_assert(dst->tag == Ico_U32); |
| 1150 | sea = (Addr64)(UInt)dst->Ico.U32; |
| 1151 | } else { |
| 1152 | tl_assert(tyW == Ity_I64); |
| 1153 | tl_assert(dst->tag == Ico_U64); |
| 1154 | sea = dst->Ico.U64; |
| 1155 | } |
| 1156 | |
| 1157 | inverted = nia == sea; |
| 1158 | |
| 1159 | /* Widen the guard expression. */ |
| 1160 | addStmtToIRSB( cgs.sbOut, |
| 1161 | IRStmt_WrTmp( guard1, st->Ist.Exit.guard )); |
| 1162 | addStmtToIRSB( cgs.sbOut, |
| 1163 | IRStmt_WrTmp( guardW, |
| 1164 | IRExpr_Unop(widen, |
| 1165 | IRExpr_RdTmp(guard1))) ); |
| 1166 | /* If the exit is inverted, invert the sense of the guard. */ |
| 1167 | addStmtToIRSB( |
| 1168 | cgs.sbOut, |
| 1169 | IRStmt_WrTmp( |
| 1170 | guard, |
| 1171 | inverted ? IRExpr_Binop(opXOR, IRExpr_RdTmp(guardW), one) |
| 1172 | : IRExpr_RdTmp(guardW) |
| 1173 | )); |
| 1174 | /* And post the event. */ |
| 1175 | addEvent_Bc( &cgs, curr_inode, IRExpr_RdTmp(guard) ); |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 1176 | } |
| 1177 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 1178 | /* We may never reach the next statement, so need to flush |
| 1179 | all outstanding transactions now. */ |
| 1180 | flushEvents( &cgs ); |
| 1181 | break; |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 1182 | } |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 1183 | |
| 1184 | default: |
| 1185 | tl_assert(0); |
| 1186 | break; |
njn | b3507ea | 2005-08-02 23:07:02 +0000 | [diff] [blame] | 1187 | } |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 1188 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 1189 | /* Copy the original statement */ |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 1190 | addStmtToIRSB( cgs.sbOut, st ); |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 1191 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 1192 | if (DEBUG_CG) { |
| 1193 | ppIRStmt(st); |
| 1194 | VG_(printf)("\n"); |
| 1195 | } |
| 1196 | } |
| 1197 | |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 1198 | /* Deal with branches to unknown destinations. Except ignore ones |
| 1199 | which are function returns as we assume the return stack |
| 1200 | predictor never mispredicts. */ |
weidendo | 374a48f | 2010-09-02 17:06:49 +0000 | [diff] [blame] | 1201 | if ((sbIn->jumpkind == Ijk_Boring) || (sbIn->jumpkind == Ijk_Call)) { |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 1202 | if (0) { ppIRExpr( sbIn->next ); VG_(printf)("\n"); } |
| 1203 | switch (sbIn->next->tag) { |
| 1204 | case Iex_Const: |
| 1205 | break; /* boring - branch to known address */ |
| 1206 | case Iex_RdTmp: |
| 1207 | /* looks like an indirect branch (branch to unknown) */ |
| 1208 | addEvent_Bi( &cgs, curr_inode, sbIn->next ); |
| 1209 | break; |
| 1210 | default: |
| 1211 | /* shouldn't happen - if the incoming IR is properly |
| 1212 | flattened, should only have tmp and const cases to |
| 1213 | consider. */ |
| 1214 | tl_assert(0); |
| 1215 | } |
| 1216 | } |
| 1217 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 1218 | /* At the end of the bb. Flush outstandings. */ |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 1219 | flushEvents( &cgs ); |
| 1220 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 1221 | /* done. stay sane ... */ |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 1222 | tl_assert(cgs.sbInfo_i == cgs.sbInfo->n_instrs); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 1223 | |
| 1224 | if (DEBUG_CG) { |
| 1225 | VG_(printf)( "goto {"); |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 1226 | ppIRJumpKind(sbIn->jumpkind); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 1227 | VG_(printf)( "} "); |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 1228 | ppIRExpr( sbIn->next ); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 1229 | VG_(printf)( "}\n"); |
| 1230 | } |
| 1231 | |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 1232 | return cgs.sbOut; |
njn | 14d01ce | 2004-11-26 11:30:14 +0000 | [diff] [blame] | 1233 | } |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1234 | |
| 1235 | /*------------------------------------------------------------*/ |
nethercote | b35a8b9 | 2004-09-11 16:45:27 +0000 | [diff] [blame] | 1236 | /*--- Cache configuration ---*/ |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1237 | /*------------------------------------------------------------*/ |
| 1238 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1239 | #define UNDEFINED_CACHE { -1, -1, -1 } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1240 | |
| 1241 | static cache_t clo_I1_cache = UNDEFINED_CACHE; |
| 1242 | static cache_t clo_D1_cache = UNDEFINED_CACHE; |
njn | 2d853a1 | 2010-10-06 22:46:31 +0000 | [diff] [blame] | 1243 | static cache_t clo_LL_cache = UNDEFINED_CACHE; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1244 | |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1245 | /*------------------------------------------------------------*/ |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1246 | /*--- cg_fini() and related function ---*/ |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1247 | /*------------------------------------------------------------*/ |
| 1248 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1249 | // Total reads/writes/misses. Calculated during CC traversal at the end. |
| 1250 | // All auto-zeroed. |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 1251 | static CacheCC Ir_total; |
| 1252 | static CacheCC Dr_total; |
| 1253 | static CacheCC Dw_total; |
| 1254 | static BranchCC Bc_total; |
| 1255 | static BranchCC Bi_total; |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1256 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1257 | static void fprint_CC_table_and_calc_totals(void) |
| 1258 | { |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 1259 | Int i, fd; |
sewardj | 9264559 | 2005-07-23 09:18:34 +0000 | [diff] [blame] | 1260 | SysRes sres; |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 1261 | Char buf[512], *currFile = NULL, *currFn = NULL; |
| 1262 | LineCC* lineCC; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1263 | |
njn | 7064fb2 | 2008-05-29 23:09:52 +0000 | [diff] [blame] | 1264 | // Setup output filename. Nb: it's important to do this now, ie. as late |
| 1265 | // as possible. If we do it at start-up and the program forks and the |
| 1266 | // output file format string contains a %p (pid) specifier, both the |
| 1267 | // parent and child will incorrectly write to the same file; this |
| 1268 | // happened in 3.3.0. |
| 1269 | Char* cachegrind_out_file = |
| 1270 | VG_(expand_file_name)("--cachegrind-out-file", clo_cachegrind_out_file); |
| 1271 | |
sewardj | 9264559 | 2005-07-23 09:18:34 +0000 | [diff] [blame] | 1272 | sres = VG_(open)(cachegrind_out_file, VKI_O_CREAT|VKI_O_TRUNC|VKI_O_WRONLY, |
| 1273 | VKI_S_IRUSR|VKI_S_IWUSR); |
njn | cda2f0f | 2009-05-18 02:12:08 +0000 | [diff] [blame] | 1274 | if (sr_isError(sres)) { |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1275 | // If the file can't be opened for whatever reason (conflict |
| 1276 | // between multiple cachegrinded processes?), give up now. |
sewardj | b2c985b | 2009-07-15 14:51:17 +0000 | [diff] [blame] | 1277 | VG_(umsg)("error: can't open cache simulation output file '%s'\n", |
| 1278 | cachegrind_out_file ); |
| 1279 | VG_(umsg)(" ... so simulation results will be missing.\n"); |
njn | 7064fb2 | 2008-05-29 23:09:52 +0000 | [diff] [blame] | 1280 | VG_(free)(cachegrind_out_file); |
sewardj | 0744b6c | 2002-12-11 00:45:42 +0000 | [diff] [blame] | 1281 | return; |
sewardj | 9264559 | 2005-07-23 09:18:34 +0000 | [diff] [blame] | 1282 | } else { |
njn | cda2f0f | 2009-05-18 02:12:08 +0000 | [diff] [blame] | 1283 | fd = sr_Res(sres); |
njn | 7064fb2 | 2008-05-29 23:09:52 +0000 | [diff] [blame] | 1284 | VG_(free)(cachegrind_out_file); |
sewardj | 0744b6c | 2002-12-11 00:45:42 +0000 | [diff] [blame] | 1285 | } |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1286 | |
njn | 2d853a1 | 2010-10-06 22:46:31 +0000 | [diff] [blame] | 1287 | // "desc:" lines (giving I1/D1/LL cache configuration). The spaces after |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1288 | // the 2nd colon makes cg_annotate's output look nicer. |
| 1289 | VG_(sprintf)(buf, "desc: I1 cache: %s\n" |
| 1290 | "desc: D1 cache: %s\n" |
njn | 2d853a1 | 2010-10-06 22:46:31 +0000 | [diff] [blame] | 1291 | "desc: LL cache: %s\n", |
| 1292 | I1.desc_line, D1.desc_line, LL.desc_line); |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1293 | VG_(write)(fd, (void*)buf, VG_(strlen)(buf)); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1294 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1295 | // "cmd:" line |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1296 | VG_(strcpy)(buf, "cmd:"); |
| 1297 | VG_(write)(fd, (void*)buf, VG_(strlen)(buf)); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1298 | if (VG_(args_the_exename)) { |
| 1299 | VG_(write)(fd, " ", 1); |
| 1300 | VG_(write)(fd, VG_(args_the_exename), |
| 1301 | VG_(strlen)( VG_(args_the_exename) )); |
| 1302 | } |
sewardj | 14c7cc5 | 2007-02-25 15:08:24 +0000 | [diff] [blame] | 1303 | for (i = 0; i < VG_(sizeXA)( VG_(args_for_client) ); i++) { |
| 1304 | HChar* arg = * (HChar**) VG_(indexXA)( VG_(args_for_client), i ); |
| 1305 | if (arg) { |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1306 | VG_(write)(fd, " ", 1); |
sewardj | 14c7cc5 | 2007-02-25 15:08:24 +0000 | [diff] [blame] | 1307 | VG_(write)(fd, arg, VG_(strlen)( arg )); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1308 | } |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1309 | } |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1310 | // "events:" line |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 1311 | if (clo_cache_sim && clo_branch_sim) { |
njn | 2d853a1 | 2010-10-06 22:46:31 +0000 | [diff] [blame] | 1312 | VG_(sprintf)(buf, "\nevents: Ir I1mr ILmr Dr D1mr DLmr Dw D1mw DLmw " |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 1313 | "Bc Bcm Bi Bim\n"); |
| 1314 | } |
| 1315 | else if (clo_cache_sim && !clo_branch_sim) { |
njn | 2d853a1 | 2010-10-06 22:46:31 +0000 | [diff] [blame] | 1316 | VG_(sprintf)(buf, "\nevents: Ir I1mr ILmr Dr D1mr DLmr Dw D1mw DLmw " |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 1317 | "\n"); |
| 1318 | } |
| 1319 | else if (!clo_cache_sim && clo_branch_sim) { |
| 1320 | VG_(sprintf)(buf, "\nevents: Ir " |
| 1321 | "Bc Bcm Bi Bim\n"); |
| 1322 | } |
njn | e90711c | 2010-09-27 01:04:20 +0000 | [diff] [blame] | 1323 | else { |
| 1324 | VG_(sprintf)(buf, "\nevents: Ir\n"); |
| 1325 | } |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 1326 | |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1327 | VG_(write)(fd, (void*)buf, VG_(strlen)(buf)); |
| 1328 | |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 1329 | // Traverse every lineCC |
njn | e2a9ad3 | 2007-09-17 05:30:48 +0000 | [diff] [blame] | 1330 | VG_(OSetGen_ResetIter)(CC_table); |
| 1331 | while ( (lineCC = VG_(OSetGen_Next)(CC_table)) ) { |
njn | 4311fe6 | 2005-12-08 23:18:50 +0000 | [diff] [blame] | 1332 | Bool just_hit_a_new_file = False; |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 1333 | // If we've hit a new file, print a "fl=" line. Note that because |
| 1334 | // each string is stored exactly once in the string table, we can use |
| 1335 | // pointer comparison rather than strcmp() to test for equality, which |
| 1336 | // is good because most of the time the comparisons are equal and so |
njn | 4311fe6 | 2005-12-08 23:18:50 +0000 | [diff] [blame] | 1337 | // the whole strings would have to be checked. |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 1338 | if ( lineCC->loc.file != currFile ) { |
| 1339 | currFile = lineCC->loc.file; |
| 1340 | VG_(sprintf)(buf, "fl=%s\n", currFile); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1341 | VG_(write)(fd, (void*)buf, VG_(strlen)(buf)); |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 1342 | distinct_files++; |
njn | 4311fe6 | 2005-12-08 23:18:50 +0000 | [diff] [blame] | 1343 | just_hit_a_new_file = True; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1344 | } |
njn | 4311fe6 | 2005-12-08 23:18:50 +0000 | [diff] [blame] | 1345 | // If we've hit a new function, print a "fn=" line. We know to do |
| 1346 | // this when the function name changes, and also every time we hit a |
| 1347 | // new file (in which case the new function name might be the same as |
| 1348 | // in the old file, hence the just_hit_a_new_file test). |
| 1349 | if ( just_hit_a_new_file || lineCC->loc.fn != currFn ) { |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 1350 | currFn = lineCC->loc.fn; |
| 1351 | VG_(sprintf)(buf, "fn=%s\n", currFn); |
| 1352 | VG_(write)(fd, (void*)buf, VG_(strlen)(buf)); |
| 1353 | distinct_fns++; |
| 1354 | } |
| 1355 | |
| 1356 | // Print the LineCC |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 1357 | if (clo_cache_sim && clo_branch_sim) { |
| 1358 | VG_(sprintf)(buf, "%u %llu %llu %llu" |
| 1359 | " %llu %llu %llu" |
| 1360 | " %llu %llu %llu" |
| 1361 | " %llu %llu %llu %llu\n", |
| 1362 | lineCC->loc.line, |
njn | 2d853a1 | 2010-10-06 22:46:31 +0000 | [diff] [blame] | 1363 | lineCC->Ir.a, lineCC->Ir.m1, lineCC->Ir.mL, |
| 1364 | lineCC->Dr.a, lineCC->Dr.m1, lineCC->Dr.mL, |
| 1365 | lineCC->Dw.a, lineCC->Dw.m1, lineCC->Dw.mL, |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 1366 | lineCC->Bc.b, lineCC->Bc.mp, |
| 1367 | lineCC->Bi.b, lineCC->Bi.mp); |
| 1368 | } |
| 1369 | else if (clo_cache_sim && !clo_branch_sim) { |
| 1370 | VG_(sprintf)(buf, "%u %llu %llu %llu" |
| 1371 | " %llu %llu %llu" |
| 1372 | " %llu %llu %llu\n", |
| 1373 | lineCC->loc.line, |
njn | 2d853a1 | 2010-10-06 22:46:31 +0000 | [diff] [blame] | 1374 | lineCC->Ir.a, lineCC->Ir.m1, lineCC->Ir.mL, |
| 1375 | lineCC->Dr.a, lineCC->Dr.m1, lineCC->Dr.mL, |
| 1376 | lineCC->Dw.a, lineCC->Dw.m1, lineCC->Dw.mL); |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 1377 | } |
| 1378 | else if (!clo_cache_sim && clo_branch_sim) { |
| 1379 | VG_(sprintf)(buf, "%u %llu" |
| 1380 | " %llu %llu %llu %llu\n", |
| 1381 | lineCC->loc.line, |
| 1382 | lineCC->Ir.a, |
| 1383 | lineCC->Bc.b, lineCC->Bc.mp, |
| 1384 | lineCC->Bi.b, lineCC->Bi.mp); |
| 1385 | } |
njn | e90711c | 2010-09-27 01:04:20 +0000 | [diff] [blame] | 1386 | else { |
| 1387 | VG_(sprintf)(buf, "%u %llu\n", |
| 1388 | lineCC->loc.line, |
| 1389 | lineCC->Ir.a); |
| 1390 | } |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 1391 | |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 1392 | VG_(write)(fd, (void*)buf, VG_(strlen)(buf)); |
| 1393 | |
| 1394 | // Update summary stats |
| 1395 | Ir_total.a += lineCC->Ir.a; |
| 1396 | Ir_total.m1 += lineCC->Ir.m1; |
njn | 2d853a1 | 2010-10-06 22:46:31 +0000 | [diff] [blame] | 1397 | Ir_total.mL += lineCC->Ir.mL; |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 1398 | Dr_total.a += lineCC->Dr.a; |
| 1399 | Dr_total.m1 += lineCC->Dr.m1; |
njn | 2d853a1 | 2010-10-06 22:46:31 +0000 | [diff] [blame] | 1400 | Dr_total.mL += lineCC->Dr.mL; |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 1401 | Dw_total.a += lineCC->Dw.a; |
| 1402 | Dw_total.m1 += lineCC->Dw.m1; |
njn | 2d853a1 | 2010-10-06 22:46:31 +0000 | [diff] [blame] | 1403 | Dw_total.mL += lineCC->Dw.mL; |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 1404 | Bc_total.b += lineCC->Bc.b; |
| 1405 | Bc_total.mp += lineCC->Bc.mp; |
| 1406 | Bi_total.b += lineCC->Bi.b; |
| 1407 | Bi_total.mp += lineCC->Bi.mp; |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 1408 | |
| 1409 | distinct_lines++; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1410 | } |
| 1411 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1412 | // Summary stats must come after rest of table, since we calculate them |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 1413 | // during traversal. */ |
| 1414 | if (clo_cache_sim && clo_branch_sim) { |
| 1415 | VG_(sprintf)(buf, "summary:" |
| 1416 | " %llu %llu %llu" |
| 1417 | " %llu %llu %llu" |
| 1418 | " %llu %llu %llu" |
| 1419 | " %llu %llu %llu %llu\n", |
njn | 2d853a1 | 2010-10-06 22:46:31 +0000 | [diff] [blame] | 1420 | Ir_total.a, Ir_total.m1, Ir_total.mL, |
| 1421 | Dr_total.a, Dr_total.m1, Dr_total.mL, |
| 1422 | Dw_total.a, Dw_total.m1, Dw_total.mL, |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 1423 | Bc_total.b, Bc_total.mp, |
| 1424 | Bi_total.b, Bi_total.mp); |
| 1425 | } |
| 1426 | else if (clo_cache_sim && !clo_branch_sim) { |
| 1427 | VG_(sprintf)(buf, "summary:" |
| 1428 | " %llu %llu %llu" |
| 1429 | " %llu %llu %llu" |
| 1430 | " %llu %llu %llu\n", |
njn | 2d853a1 | 2010-10-06 22:46:31 +0000 | [diff] [blame] | 1431 | Ir_total.a, Ir_total.m1, Ir_total.mL, |
| 1432 | Dr_total.a, Dr_total.m1, Dr_total.mL, |
| 1433 | Dw_total.a, Dw_total.m1, Dw_total.mL); |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 1434 | } |
| 1435 | else if (!clo_cache_sim && clo_branch_sim) { |
| 1436 | VG_(sprintf)(buf, "summary:" |
| 1437 | " %llu" |
| 1438 | " %llu %llu %llu %llu\n", |
| 1439 | Ir_total.a, |
| 1440 | Bc_total.b, Bc_total.mp, |
| 1441 | Bi_total.b, Bi_total.mp); |
| 1442 | } |
njn | e90711c | 2010-09-27 01:04:20 +0000 | [diff] [blame] | 1443 | else { |
| 1444 | VG_(sprintf)(buf, "summary:" |
| 1445 | " %llu\n", |
| 1446 | Ir_total.a); |
| 1447 | } |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 1448 | |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1449 | VG_(write)(fd, (void*)buf, VG_(strlen)(buf)); |
| 1450 | VG_(close)(fd); |
| 1451 | } |
| 1452 | |
njn | 607adfc | 2003-09-30 14:15:44 +0000 | [diff] [blame] | 1453 | static UInt ULong_width(ULong n) |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1454 | { |
njn | 607adfc | 2003-09-30 14:15:44 +0000 | [diff] [blame] | 1455 | UInt w = 0; |
| 1456 | while (n > 0) { |
| 1457 | n = n / 10; |
| 1458 | w++; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1459 | } |
sewardj | 46c59b1 | 2005-11-01 02:20:19 +0000 | [diff] [blame] | 1460 | if (w == 0) w = 1; |
njn | 607adfc | 2003-09-30 14:15:44 +0000 | [diff] [blame] | 1461 | return w + (w-1)/3; // add space for commas |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1462 | } |
| 1463 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1464 | static void cg_fini(Int exitcode) |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1465 | { |
njn | 1baf7db | 2006-04-18 22:34:48 +0000 | [diff] [blame] | 1466 | static Char buf1[128], buf2[128], buf3[128], buf4[123], fmt[128]; |
njn | 607adfc | 2003-09-30 14:15:44 +0000 | [diff] [blame] | 1467 | |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 1468 | CacheCC D_total; |
| 1469 | BranchCC B_total; |
njn | 2d853a1 | 2010-10-06 22:46:31 +0000 | [diff] [blame] | 1470 | ULong LL_total_m, LL_total_mr, LL_total_mw, |
| 1471 | LL_total, LL_total_r, LL_total_w; |
njn | 4c245e5 | 2009-03-15 23:25:38 +0000 | [diff] [blame] | 1472 | Int l1, l2, l3; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1473 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1474 | fprint_CC_table_and_calc_totals(); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1475 | |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1476 | if (VG_(clo_verbosity) == 0) |
| 1477 | return; |
| 1478 | |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 1479 | // Nb: this isn't called "MAX" because that overshadows a global on Darwin. |
| 1480 | #define CG_MAX(a, b) ((a) >= (b) ? (a) : (b)) |
njn | 4c245e5 | 2009-03-15 23:25:38 +0000 | [diff] [blame] | 1481 | |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1482 | /* I cache results. Use the I_refs value to determine the first column |
| 1483 | * width. */ |
njn | 607adfc | 2003-09-30 14:15:44 +0000 | [diff] [blame] | 1484 | l1 = ULong_width(Ir_total.a); |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 1485 | l2 = ULong_width(CG_MAX(Dr_total.a, Bc_total.b)); |
| 1486 | l3 = ULong_width(CG_MAX(Dw_total.a, Bi_total.b)); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1487 | |
njn | 607adfc | 2003-09-30 14:15:44 +0000 | [diff] [blame] | 1488 | /* Make format string, getting width right for numbers */ |
sewardj | b2c985b | 2009-07-15 14:51:17 +0000 | [diff] [blame] | 1489 | VG_(sprintf)(fmt, "%%s %%,%dllu\n", l1); |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 1490 | |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 1491 | /* Always print this */ |
sewardj | b2c985b | 2009-07-15 14:51:17 +0000 | [diff] [blame] | 1492 | VG_(umsg)(fmt, "I refs: ", Ir_total.a); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1493 | |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 1494 | /* If cache profiling is enabled, show D access numbers and all |
| 1495 | miss numbers */ |
| 1496 | if (clo_cache_sim) { |
sewardj | b2c985b | 2009-07-15 14:51:17 +0000 | [diff] [blame] | 1497 | VG_(umsg)(fmt, "I1 misses: ", Ir_total.m1); |
njn | 2d853a1 | 2010-10-06 22:46:31 +0000 | [diff] [blame] | 1498 | VG_(umsg)(fmt, "LLi misses: ", Ir_total.mL); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1499 | |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 1500 | if (0 == Ir_total.a) Ir_total.a = 1; |
| 1501 | VG_(percentify)(Ir_total.m1, Ir_total.a, 2, l1+1, buf1); |
sewardj | b2c985b | 2009-07-15 14:51:17 +0000 | [diff] [blame] | 1502 | VG_(umsg)("I1 miss rate: %s\n", buf1); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1503 | |
njn | 2d853a1 | 2010-10-06 22:46:31 +0000 | [diff] [blame] | 1504 | VG_(percentify)(Ir_total.mL, Ir_total.a, 2, l1+1, buf1); |
| 1505 | VG_(umsg)("LLi miss rate: %s\n", buf1); |
sewardj | b2c985b | 2009-07-15 14:51:17 +0000 | [diff] [blame] | 1506 | VG_(umsg)("\n"); |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 1507 | |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 1508 | /* D cache results. Use the D_refs.rd and D_refs.wr values to |
| 1509 | * determine the width of columns 2 & 3. */ |
| 1510 | D_total.a = Dr_total.a + Dw_total.a; |
| 1511 | D_total.m1 = Dr_total.m1 + Dw_total.m1; |
njn | 2d853a1 | 2010-10-06 22:46:31 +0000 | [diff] [blame] | 1512 | D_total.mL = Dr_total.mL + Dw_total.mL; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1513 | |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 1514 | /* Make format string, getting width right for numbers */ |
sewardj | b2c985b | 2009-07-15 14:51:17 +0000 | [diff] [blame] | 1515 | VG_(sprintf)(fmt, "%%s %%,%dllu (%%,%dllu rd + %%,%dllu wr)\n", |
| 1516 | l1, l2, l3); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1517 | |
sewardj | b2c985b | 2009-07-15 14:51:17 +0000 | [diff] [blame] | 1518 | VG_(umsg)(fmt, "D refs: ", |
| 1519 | D_total.a, Dr_total.a, Dw_total.a); |
| 1520 | VG_(umsg)(fmt, "D1 misses: ", |
| 1521 | D_total.m1, Dr_total.m1, Dw_total.m1); |
njn | 2d853a1 | 2010-10-06 22:46:31 +0000 | [diff] [blame] | 1522 | VG_(umsg)(fmt, "LLd misses: ", |
| 1523 | D_total.mL, Dr_total.mL, Dw_total.mL); |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 1524 | |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 1525 | if (0 == D_total.a) D_total.a = 1; |
| 1526 | if (0 == Dr_total.a) Dr_total.a = 1; |
| 1527 | if (0 == Dw_total.a) Dw_total.a = 1; |
| 1528 | VG_(percentify)( D_total.m1, D_total.a, 1, l1+1, buf1); |
| 1529 | VG_(percentify)(Dr_total.m1, Dr_total.a, 1, l2+1, buf2); |
| 1530 | VG_(percentify)(Dw_total.m1, Dw_total.a, 1, l3+1, buf3); |
sewardj | b2c985b | 2009-07-15 14:51:17 +0000 | [diff] [blame] | 1531 | VG_(umsg)("D1 miss rate: %s (%s + %s )\n", buf1, buf2,buf3); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1532 | |
njn | 2d853a1 | 2010-10-06 22:46:31 +0000 | [diff] [blame] | 1533 | VG_(percentify)( D_total.mL, D_total.a, 1, l1+1, buf1); |
| 1534 | VG_(percentify)(Dr_total.mL, Dr_total.a, 1, l2+1, buf2); |
| 1535 | VG_(percentify)(Dw_total.mL, Dw_total.a, 1, l3+1, buf3); |
| 1536 | VG_(umsg)("LLd miss rate: %s (%s + %s )\n", buf1, buf2,buf3); |
sewardj | b2c985b | 2009-07-15 14:51:17 +0000 | [diff] [blame] | 1537 | VG_(umsg)("\n"); |
njn | 1d021fa | 2002-05-02 13:56:34 +0000 | [diff] [blame] | 1538 | |
njn | 2d853a1 | 2010-10-06 22:46:31 +0000 | [diff] [blame] | 1539 | /* LL overall results */ |
njn | 1d021fa | 2002-05-02 13:56:34 +0000 | [diff] [blame] | 1540 | |
njn | 2d853a1 | 2010-10-06 22:46:31 +0000 | [diff] [blame] | 1541 | LL_total = Dr_total.m1 + Dw_total.m1 + Ir_total.m1; |
| 1542 | LL_total_r = Dr_total.m1 + Ir_total.m1; |
| 1543 | LL_total_w = Dw_total.m1; |
| 1544 | VG_(umsg)(fmt, "LL refs: ", |
| 1545 | LL_total, LL_total_r, LL_total_w); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1546 | |
njn | 2d853a1 | 2010-10-06 22:46:31 +0000 | [diff] [blame] | 1547 | LL_total_m = Dr_total.mL + Dw_total.mL + Ir_total.mL; |
| 1548 | LL_total_mr = Dr_total.mL + Ir_total.mL; |
| 1549 | LL_total_mw = Dw_total.mL; |
| 1550 | VG_(umsg)(fmt, "LL misses: ", |
| 1551 | LL_total_m, LL_total_mr, LL_total_mw); |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 1552 | |
njn | 2d853a1 | 2010-10-06 22:46:31 +0000 | [diff] [blame] | 1553 | VG_(percentify)(LL_total_m, (Ir_total.a + D_total.a), 1, l1+1, buf1); |
| 1554 | VG_(percentify)(LL_total_mr, (Ir_total.a + Dr_total.a), 1, l2+1, buf2); |
| 1555 | VG_(percentify)(LL_total_mw, Dw_total.a, 1, l3+1, buf3); |
| 1556 | VG_(umsg)("LL miss rate: %s (%s + %s )\n", buf1, buf2,buf3); |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 1557 | } |
| 1558 | |
| 1559 | /* If branch profiling is enabled, show branch overall results. */ |
| 1560 | if (clo_branch_sim) { |
| 1561 | /* Make format string, getting width right for numbers */ |
sewardj | b2c985b | 2009-07-15 14:51:17 +0000 | [diff] [blame] | 1562 | VG_(sprintf)(fmt, "%%s %%,%dllu (%%,%dllu cond + %%,%dllu ind)\n", |
| 1563 | l1, l2, l3); |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 1564 | |
| 1565 | if (0 == Bc_total.b) Bc_total.b = 1; |
| 1566 | if (0 == Bi_total.b) Bi_total.b = 1; |
| 1567 | B_total.b = Bc_total.b + Bi_total.b; |
| 1568 | B_total.mp = Bc_total.mp + Bi_total.mp; |
| 1569 | |
sewardj | b2c985b | 2009-07-15 14:51:17 +0000 | [diff] [blame] | 1570 | VG_(umsg)("\n"); |
| 1571 | VG_(umsg)(fmt, "Branches: ", |
| 1572 | B_total.b, Bc_total.b, Bi_total.b); |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 1573 | |
sewardj | b2c985b | 2009-07-15 14:51:17 +0000 | [diff] [blame] | 1574 | VG_(umsg)(fmt, "Mispredicts: ", |
| 1575 | B_total.mp, Bc_total.mp, Bi_total.mp); |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 1576 | |
| 1577 | VG_(percentify)(B_total.mp, B_total.b, 1, l1+1, buf1); |
| 1578 | VG_(percentify)(Bc_total.mp, Bc_total.b, 1, l2+1, buf2); |
| 1579 | VG_(percentify)(Bi_total.mp, Bi_total.b, 1, l3+1, buf3); |
| 1580 | |
sewardj | b2c985b | 2009-07-15 14:51:17 +0000 | [diff] [blame] | 1581 | VG_(umsg)("Mispred rate: %s (%s + %s )\n", buf1, buf2,buf3); |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 1582 | } |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1583 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1584 | // Various stats |
sewardj | 2d9e874 | 2009-08-07 15:46:56 +0000 | [diff] [blame] | 1585 | if (VG_(clo_stats)) { |
njn | 1baf7db | 2006-04-18 22:34:48 +0000 | [diff] [blame] | 1586 | Int debug_lookups = full_debugs + fn_debugs + |
| 1587 | file_line_debugs + no_debugs; |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 1588 | |
sewardj | b2c985b | 2009-07-15 14:51:17 +0000 | [diff] [blame] | 1589 | VG_(dmsg)("\n"); |
| 1590 | VG_(dmsg)("cachegrind: distinct files: %d\n", distinct_files); |
| 1591 | VG_(dmsg)("cachegrind: distinct fns: %d\n", distinct_fns); |
| 1592 | VG_(dmsg)("cachegrind: distinct lines: %d\n", distinct_lines); |
| 1593 | VG_(dmsg)("cachegrind: distinct instrs:%d\n", distinct_instrs); |
| 1594 | VG_(dmsg)("cachegrind: debug lookups : %d\n", debug_lookups); |
njn | 1baf7db | 2006-04-18 22:34:48 +0000 | [diff] [blame] | 1595 | |
| 1596 | VG_(percentify)(full_debugs, debug_lookups, 1, 6, buf1); |
| 1597 | VG_(percentify)(file_line_debugs, debug_lookups, 1, 6, buf2); |
| 1598 | VG_(percentify)(fn_debugs, debug_lookups, 1, 6, buf3); |
| 1599 | VG_(percentify)(no_debugs, debug_lookups, 1, 6, buf4); |
sewardj | b2c985b | 2009-07-15 14:51:17 +0000 | [diff] [blame] | 1600 | VG_(dmsg)("cachegrind: with full info:%s (%d)\n", |
| 1601 | buf1, full_debugs); |
| 1602 | VG_(dmsg)("cachegrind: with file/line info:%s (%d)\n", |
| 1603 | buf2, file_line_debugs); |
| 1604 | VG_(dmsg)("cachegrind: with fn name info:%s (%d)\n", |
| 1605 | buf3, fn_debugs); |
| 1606 | VG_(dmsg)("cachegrind: with zero info:%s (%d)\n", |
| 1607 | buf4, no_debugs); |
njn | 1baf7db | 2006-04-18 22:34:48 +0000 | [diff] [blame] | 1608 | |
sewardj | b2c985b | 2009-07-15 14:51:17 +0000 | [diff] [blame] | 1609 | VG_(dmsg)("cachegrind: string table size: %lu\n", |
| 1610 | VG_(OSetGen_Size)(stringTable)); |
| 1611 | VG_(dmsg)("cachegrind: CC table size: %lu\n", |
| 1612 | VG_(OSetGen_Size)(CC_table)); |
| 1613 | VG_(dmsg)("cachegrind: InstrInfo table size: %lu\n", |
| 1614 | VG_(OSetGen_Size)(instrInfoTable)); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1615 | } |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1616 | } |
| 1617 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1618 | /*--------------------------------------------------------------------*/ |
| 1619 | /*--- Discarding BB info ---*/ |
| 1620 | /*--------------------------------------------------------------------*/ |
sewardj | 18d7513 | 2002-05-16 11:06:21 +0000 | [diff] [blame] | 1621 | |
sewardj | a3a29a5 | 2005-10-12 16:16:03 +0000 | [diff] [blame] | 1622 | // Called when a translation is removed from the translation cache for |
| 1623 | // any reason at all: to free up space, because the guest code was |
| 1624 | // unmapped or modified, or for any arbitrary reason. |
sewardj | 4ba057c | 2005-10-18 12:04:18 +0000 | [diff] [blame] | 1625 | static |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 1626 | void cg_discard_superblock_info ( Addr64 orig_addr64, VexGuestExtents vge ) |
sewardj | 18d7513 | 2002-05-16 11:06:21 +0000 | [diff] [blame] | 1627 | { |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 1628 | SB_info* sbInfo; |
sewardj | 3a384b3 | 2006-01-22 01:12:51 +0000 | [diff] [blame] | 1629 | Addr orig_addr = (Addr)vge.base[0]; |
njn | 4294fd4 | 2002-06-05 14:41:10 +0000 | [diff] [blame] | 1630 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 1631 | tl_assert(vge.n_used > 0); |
| 1632 | |
| 1633 | if (DEBUG_CG) |
sewardj | 4ba057c | 2005-10-18 12:04:18 +0000 | [diff] [blame] | 1634 | VG_(printf)( "discard_basic_block_info: %p, %p, %llu\n", |
| 1635 | (void*)(Addr)orig_addr, |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 1636 | (void*)(Addr)vge.base[0], (ULong)vge.len[0]); |
njn | 4294fd4 | 2002-06-05 14:41:10 +0000 | [diff] [blame] | 1637 | |
sewardj | 4ba057c | 2005-10-18 12:04:18 +0000 | [diff] [blame] | 1638 | // Get BB info, remove from table, free BB info. Simple! Note that we |
| 1639 | // use orig_addr, not the first instruction address in vge. |
njn | e2a9ad3 | 2007-09-17 05:30:48 +0000 | [diff] [blame] | 1640 | sbInfo = VG_(OSetGen_Remove)(instrInfoTable, &orig_addr); |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 1641 | tl_assert(NULL != sbInfo); |
njn | e2a9ad3 | 2007-09-17 05:30:48 +0000 | [diff] [blame] | 1642 | VG_(OSetGen_FreeNode)(instrInfoTable, sbInfo); |
sewardj | 18d7513 | 2002-05-16 11:06:21 +0000 | [diff] [blame] | 1643 | } |
| 1644 | |
| 1645 | /*--------------------------------------------------------------------*/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1646 | /*--- Command line processing ---*/ |
| 1647 | /*--------------------------------------------------------------------*/ |
| 1648 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1649 | static Bool cg_process_cmd_line_option(Char* arg) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1650 | { |
weidendo | 2364227 | 2011-09-06 19:08:31 +0000 | [diff] [blame] | 1651 | if (VG_(str_clo_cache_opt)(arg, |
| 1652 | &clo_I1_cache, |
| 1653 | &clo_D1_cache, |
| 1654 | &clo_LL_cache)) {} |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 1655 | |
| 1656 | else if VG_STR_CLO( arg, "--cachegrind-out-file", clo_cachegrind_out_file) {} |
| 1657 | else if VG_BOOL_CLO(arg, "--cache-sim", clo_cache_sim) {} |
| 1658 | else if VG_BOOL_CLO(arg, "--branch-sim", clo_branch_sim) {} |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1659 | else |
| 1660 | return False; |
| 1661 | |
| 1662 | return True; |
| 1663 | } |
| 1664 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1665 | static void cg_print_usage(void) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1666 | { |
weidendo | 2364227 | 2011-09-06 19:08:31 +0000 | [diff] [blame] | 1667 | VG_(print_cache_clo_opts)(); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1668 | VG_(printf)( |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 1669 | " --cache-sim=yes|no [yes] collect cache stats?\n" |
| 1670 | " --branch-sim=yes|no [no] collect branch prediction stats?\n" |
njn | 374a36d | 2007-11-23 01:41:32 +0000 | [diff] [blame] | 1671 | " --cachegrind-out-file=<file> output file name [cachegrind.out.%%p]\n" |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1672 | ); |
| 1673 | } |
| 1674 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1675 | static void cg_print_debug_usage(void) |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1676 | { |
| 1677 | VG_(printf)( |
| 1678 | " (none)\n" |
| 1679 | ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1680 | } |
| 1681 | |
| 1682 | /*--------------------------------------------------------------------*/ |
| 1683 | /*--- Setup ---*/ |
| 1684 | /*--------------------------------------------------------------------*/ |
| 1685 | |
sewardj | e1216cb | 2007-02-07 19:55:30 +0000 | [diff] [blame] | 1686 | static void cg_post_clo_init(void); /* just below */ |
| 1687 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1688 | static void cg_pre_clo_init(void) |
| 1689 | { |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1690 | VG_(details_name) ("Cachegrind"); |
| 1691 | VG_(details_version) (NULL); |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 1692 | VG_(details_description) ("a cache and branch-prediction profiler"); |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1693 | VG_(details_copyright_author)( |
sewardj | ec062e8 | 2011-10-23 07:32:08 +0000 | [diff] [blame] | 1694 | "Copyright (C) 2002-2011, and GNU GPL'd, by Nicholas Nethercote et al."); |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1695 | VG_(details_bug_reports_to) (VG_BUGS_TO); |
sewardj | e808930 | 2006-10-17 02:15:17 +0000 | [diff] [blame] | 1696 | VG_(details_avg_translation_sizeB) ( 500 ); |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1697 | |
| 1698 | VG_(basic_tool_funcs) (cg_post_clo_init, |
| 1699 | cg_instrument, |
| 1700 | cg_fini); |
| 1701 | |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 1702 | VG_(needs_superblock_discards)(cg_discard_superblock_info); |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1703 | VG_(needs_command_line_options)(cg_process_cmd_line_option, |
| 1704 | cg_print_usage, |
| 1705 | cg_print_debug_usage); |
sewardj | e1216cb | 2007-02-07 19:55:30 +0000 | [diff] [blame] | 1706 | } |
| 1707 | |
| 1708 | static void cg_post_clo_init(void) |
| 1709 | { |
njn | 2d853a1 | 2010-10-06 22:46:31 +0000 | [diff] [blame] | 1710 | cache_t I1c, D1c, LLc; |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1711 | |
njn | e2a9ad3 | 2007-09-17 05:30:48 +0000 | [diff] [blame] | 1712 | CC_table = |
| 1713 | VG_(OSetGen_Create)(offsetof(LineCC, loc), |
| 1714 | cmp_CodeLoc_LineCC, |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 1715 | VG_(malloc), "cg.main.cpci.1", |
| 1716 | VG_(free)); |
njn | e2a9ad3 | 2007-09-17 05:30:48 +0000 | [diff] [blame] | 1717 | instrInfoTable = |
| 1718 | VG_(OSetGen_Create)(/*keyOff*/0, |
| 1719 | NULL, |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 1720 | VG_(malloc), "cg.main.cpci.2", |
| 1721 | VG_(free)); |
njn | e2a9ad3 | 2007-09-17 05:30:48 +0000 | [diff] [blame] | 1722 | stringTable = |
| 1723 | VG_(OSetGen_Create)(/*keyOff*/0, |
| 1724 | stringCmp, |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 1725 | VG_(malloc), "cg.main.cpci.3", |
| 1726 | VG_(free)); |
sewardj | e1216cb | 2007-02-07 19:55:30 +0000 | [diff] [blame] | 1727 | |
weidendo | 2364227 | 2011-09-06 19:08:31 +0000 | [diff] [blame] | 1728 | VG_(post_clo_init_configure_caches)(&I1c, &D1c, &LLc, |
| 1729 | &clo_I1_cache, |
| 1730 | &clo_D1_cache, |
| 1731 | &clo_LL_cache); |
sewardj | e1216cb | 2007-02-07 19:55:30 +0000 | [diff] [blame] | 1732 | |
sewardj | 98763d5 | 2012-06-03 22:40:07 +0000 | [diff] [blame^] | 1733 | // min_line_size is used to make sure that we never feed |
| 1734 | // accesses to the simulator straddling more than two |
| 1735 | // cache lines at any cache level |
| 1736 | min_line_size = (I1c.line_size < D1c.line_size) ? I1c.line_size : D1c.line_size; |
| 1737 | min_line_size = (LLc.line_size < min_line_size) ? LLc.line_size : min_line_size; |
| 1738 | |
| 1739 | Int largest_load_or_store_size |
| 1740 | = VG_(machine_get_size_of_largest_guest_register)(); |
| 1741 | if (min_line_size < largest_load_or_store_size) { |
| 1742 | /* We can't continue, because the cache simulation might |
| 1743 | straddle more than 2 lines, and it will assert. So let's |
| 1744 | just stop before we start. */ |
| 1745 | VG_(umsg)("Cachegrind: cannot continue: the minimum line size (%d)\n", |
| 1746 | (Int)min_line_size); |
| 1747 | VG_(umsg)(" must be equal to or larger than the maximum register size (%d)\n", |
| 1748 | largest_load_or_store_size ); |
| 1749 | VG_(umsg)(" but it is not. Exiting now.\n"); |
| 1750 | VG_(exit)(1); |
| 1751 | } |
| 1752 | |
sewardj | e1216cb | 2007-02-07 19:55:30 +0000 | [diff] [blame] | 1753 | cachesim_I1_initcache(I1c); |
| 1754 | cachesim_D1_initcache(D1c); |
njn | 2d853a1 | 2010-10-06 22:46:31 +0000 | [diff] [blame] | 1755 | cachesim_LL_initcache(LLc); |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1756 | } |
| 1757 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1758 | VG_DETERMINE_INTERFACE_VERSION(cg_pre_clo_init) |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1759 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1760 | /*--------------------------------------------------------------------*/ |
njn | f69f945 | 2005-07-03 17:53:11 +0000 | [diff] [blame] | 1761 | /*--- end ---*/ |
sewardj | 18d7513 | 2002-05-16 11:06:21 +0000 | [diff] [blame] | 1762 | /*--------------------------------------------------------------------*/ |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 1763 | |