sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1 | /* |
| 2 | This file is part of drd, a data race detector. |
| 3 | |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 4 | Copyright (C) 2006-2008 Bart Van Assche |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 5 | bart.vanassche@gmail.com |
| 6 | |
| 7 | This program is free software; you can redistribute it and/or |
| 8 | modify it under the terms of the GNU General Public License as |
| 9 | published by the Free Software Foundation; either version 2 of the |
| 10 | License, or (at your option) any later version. |
| 11 | |
| 12 | This program is distributed in the hope that it will be useful, but |
| 13 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with this program; if not, write to the Free Software |
| 19 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 20 | 02111-1307, USA. |
| 21 | |
| 22 | The GNU General Public License is contained in the file COPYING. |
| 23 | */ |
| 24 | |
| 25 | |
| 26 | #include "pub_tool_basics.h" // Addr, SizeT |
| 27 | #include "pub_tool_debuginfo.h" // VG_(get_objname)() |
| 28 | #include "pub_tool_libcassert.h" // tl_assert() |
| 29 | #include "pub_tool_libcbase.h" // VG_(memset) |
| 30 | #include "pub_tool_libcprint.h" // VG_(printf) |
| 31 | #include "pub_tool_machine.h" // VG_(get_IP)() |
| 32 | #include "pub_tool_mallocfree.h" // VG_(malloc), VG_(free) |
| 33 | #include "pub_drd_bitmap.h" |
| 34 | #include "drd_bitmap.h" |
| 35 | #include "drd_error.h" |
| 36 | #include "drd_suppression.h" |
| 37 | |
| 38 | |
| 39 | // Local constants. |
| 40 | |
| 41 | static ULong s_bitmap_creation_count; |
| 42 | |
| 43 | |
| 44 | // Local function declarations. |
| 45 | |
| 46 | static void bm2_merge(struct bitmap2* const bm2l, |
| 47 | const struct bitmap2* const bm2r); |
| 48 | |
| 49 | |
| 50 | // Function definitions. |
| 51 | |
| 52 | struct bitmap* bm_new() |
| 53 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 54 | struct bitmap* bm; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 55 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 56 | // If this assert fails, fix the definition of BITS_PER_BITS_PER_UWORD |
| 57 | // in drd_bitmap.h. |
| 58 | tl_assert((1 << BITS_PER_BITS_PER_UWORD) == BITS_PER_UWORD); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 59 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 60 | bm = VG_(malloc)(sizeof(*bm)); |
| 61 | tl_assert(bm); |
| 62 | bm->oset = VG_(OSetGen_Create)(0, 0, VG_(malloc), VG_(free)); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 63 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 64 | s_bitmap_creation_count++; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 65 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 66 | return bm; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | void bm_delete(struct bitmap* const bm) |
| 70 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 71 | tl_assert(bm); |
| 72 | VG_(OSetGen_Destroy)(bm->oset); |
| 73 | VG_(free)(bm); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | /** |
bart | 3655612 | 2008-03-13 19:24:30 +0000 | [diff] [blame] | 77 | * Record an access of type access_type at addresses a .. a + size - 1 in |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 78 | * bitmap bm. |
| 79 | */ |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 80 | static |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 81 | void bm_access_range(struct bitmap* const bm, |
bart | 9036dea | 2008-03-13 19:10:06 +0000 | [diff] [blame] | 82 | const Addr a1, const Addr a2, |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 83 | const BmAccessTypeT access_type) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 84 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 85 | Addr b, b_next; |
bart | 3655612 | 2008-03-13 19:24:30 +0000 | [diff] [blame] | 86 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 87 | tl_assert(bm); |
| 88 | tl_assert(a1 < a2); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 89 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 90 | for (b = a1; b < a2; b = b_next) |
| 91 | { |
| 92 | Addr b_start; |
| 93 | Addr b_end; |
| 94 | struct bitmap2* bm2; |
| 95 | SPLIT_ADDRESS(b); |
bart | 3655612 | 2008-03-13 19:24:30 +0000 | [diff] [blame] | 96 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 97 | b_next = (b & ~ADDR0_MASK) + ADDR0_COUNT; |
| 98 | if (b_next > a2) |
| 99 | { |
| 100 | b_next = a2; |
| 101 | } |
bart | 3655612 | 2008-03-13 19:24:30 +0000 | [diff] [blame] | 102 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 103 | bm2 = bm2_lookup_or_insert(bm, b1); |
| 104 | tl_assert(bm2); |
bart | 3655612 | 2008-03-13 19:24:30 +0000 | [diff] [blame] | 105 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 106 | if ((bm2->addr << ADDR0_BITS) < a1) |
| 107 | b_start = a1; |
| 108 | else |
| 109 | if ((bm2->addr << ADDR0_BITS) < a2) |
| 110 | b_start = (bm2->addr << ADDR0_BITS); |
bart | 3655612 | 2008-03-13 19:24:30 +0000 | [diff] [blame] | 111 | else |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 112 | break; |
| 113 | tl_assert(a1 <= b_start && b_start <= a2); |
bart | 3655612 | 2008-03-13 19:24:30 +0000 | [diff] [blame] | 114 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 115 | if ((bm2->addr << ADDR0_BITS) + ADDR0_COUNT < a2) |
| 116 | b_end = (bm2->addr << ADDR0_BITS) + ADDR0_COUNT; |
| 117 | else |
| 118 | b_end = a2; |
| 119 | tl_assert(a1 <= b_end && b_end <= a2); |
| 120 | tl_assert(b_start < b_end); |
| 121 | tl_assert((b_start & ADDR0_MASK) <= ((b_end - 1) & ADDR0_MASK)); |
bart | 3655612 | 2008-03-13 19:24:30 +0000 | [diff] [blame] | 122 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 123 | for (b0 = b_start & ADDR0_MASK; b0 <= ((b_end - 1) & ADDR0_MASK); b0++) |
| 124 | { |
| 125 | if (access_type == eLoad) |
bart | 3655612 | 2008-03-13 19:24:30 +0000 | [diff] [blame] | 126 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 127 | bm0_set(bm2->bm1.bm0_r, b0); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 128 | } |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 129 | else |
| 130 | { |
| 131 | bm0_set(bm2->bm1.bm0_w, b0); |
| 132 | } |
| 133 | } |
| 134 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 135 | } |
| 136 | |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 137 | static inline |
| 138 | void bm_access_aligned_load(struct bitmap* const bm, |
| 139 | const Addr a1, const Addr a2) |
| 140 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 141 | struct bitmap2* bm2; |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 142 | |
| 143 | #if 0 |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 144 | /* Commented out the statements below because of performance reasons. */ |
| 145 | tl_assert(bm); |
| 146 | tl_assert(a1 < a2); |
| 147 | tl_assert((a2 - a1) == 1 || (a2 - a1) == 2 |
| 148 | || (a2 - a1) == 4 || (a2 - a1) == 8); |
| 149 | tl_assert((a1 & (a2 - a1 - 1)) == 0); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 150 | #endif |
| 151 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 152 | bm2 = bm2_lookup_or_insert(bm, a1 >> ADDR0_BITS); |
| 153 | tl_assert(bm2); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 154 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 155 | bm0_set_range(bm2->bm1.bm0_r, a1 & ADDR0_MASK, (a2 - 1) & ADDR0_MASK); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | static inline |
| 159 | void bm_access_aligned_store(struct bitmap* const bm, |
| 160 | const Addr a1, const Addr a2) |
| 161 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 162 | struct bitmap2* bm2; |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 163 | |
| 164 | #if 0 |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 165 | /* Commented out the statements below because of performance reasons. */ |
| 166 | tl_assert(bm); |
| 167 | tl_assert(a1 < a2); |
| 168 | tl_assert((a2 - a1) == 1 || (a2 - a1) == 2 |
| 169 | || (a2 - a1) == 4 || (a2 - a1) == 8); |
| 170 | tl_assert((a1 & (a2 - a1 - 1)) == 0); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 171 | #endif |
| 172 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 173 | bm2 = bm2_lookup_or_insert(bm, a1 >> ADDR0_BITS); |
| 174 | tl_assert(bm2); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 175 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 176 | bm0_set_range(bm2->bm1.bm0_w, a1 & ADDR0_MASK, (a2 - 1) & ADDR0_MASK); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 177 | } |
| 178 | |
bart | 3655612 | 2008-03-13 19:24:30 +0000 | [diff] [blame] | 179 | void bm_access_range_load(struct bitmap* const bm, |
| 180 | const Addr a1, const Addr a2) |
| 181 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 182 | bm_access_range(bm, a1, a2, eLoad); |
bart | 3655612 | 2008-03-13 19:24:30 +0000 | [diff] [blame] | 183 | } |
| 184 | |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 185 | void bm_access_load_1(struct bitmap* const bm, const Addr a1) |
| 186 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 187 | bm_access_aligned_load(bm, a1, a1 + 1); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | void bm_access_load_2(struct bitmap* const bm, const Addr a1) |
| 191 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 192 | if ((a1 & 1) == 0) |
| 193 | bm_access_aligned_load(bm, a1, a1 + 2); |
| 194 | else |
| 195 | bm_access_range(bm, a1, a1 + 2, eLoad); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | void bm_access_load_4(struct bitmap* const bm, const Addr a1) |
| 199 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 200 | if ((a1 & 3) == 0) |
| 201 | bm_access_aligned_load(bm, a1, a1 + 4); |
| 202 | else |
| 203 | bm_access_range(bm, a1, a1 + 4, eLoad); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | void bm_access_load_8(struct bitmap* const bm, const Addr a1) |
| 207 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 208 | if ((a1 & 7) == 0) |
| 209 | bm_access_aligned_load(bm, a1, a1 + 8); |
| 210 | else if ((a1 & 3) == 0) |
| 211 | { |
| 212 | bm_access_aligned_load(bm, a1 + 0, a1 + 4); |
| 213 | bm_access_aligned_load(bm, a1 + 4, a1 + 8); |
| 214 | } |
| 215 | else |
| 216 | bm_access_range(bm, a1, a1 + 8, eLoad); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | void bm_access_store_1(struct bitmap* const bm, const Addr a1) |
| 220 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 221 | bm_access_aligned_store(bm, a1, a1 + 1); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | void bm_access_store_2(struct bitmap* const bm, const Addr a1) |
| 225 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 226 | if ((a1 & 1) == 0) |
| 227 | bm_access_aligned_store(bm, a1, a1 + 2); |
| 228 | else |
| 229 | bm_access_range(bm, a1, a1 + 2, eStore); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | void bm_access_store_4(struct bitmap* const bm, const Addr a1) |
| 233 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 234 | if ((a1 & 3) == 0) |
| 235 | bm_access_aligned_store(bm, a1, a1 + 4); |
| 236 | else |
| 237 | bm_access_range(bm, a1, a1 + 4, eStore); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | void bm_access_store_8(struct bitmap* const bm, const Addr a1) |
| 241 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 242 | if ((a1 & 7) == 0) |
| 243 | bm_access_aligned_store(bm, a1, a1 + 8); |
| 244 | else if ((a1 & 3) == 0) |
| 245 | { |
| 246 | bm_access_aligned_store(bm, a1 + 0, a1 + 4); |
| 247 | bm_access_aligned_store(bm, a1 + 4, a1 + 8); |
| 248 | } |
| 249 | else |
| 250 | bm_access_range(bm, a1, a1 + 8, eStore); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 251 | } |
| 252 | |
bart | 3655612 | 2008-03-13 19:24:30 +0000 | [diff] [blame] | 253 | void bm_access_range_store(struct bitmap* const bm, |
| 254 | const Addr a1, const Addr a2) |
| 255 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 256 | bm_access_range(bm, a1, a2, eStore); |
bart | 3655612 | 2008-03-13 19:24:30 +0000 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | Bool bm_has(const struct bitmap* const bm, const Addr a1, const Addr a2, |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 260 | const BmAccessTypeT access_type) |
| 261 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 262 | Addr b; |
| 263 | for (b = a1; b < a2; b++) |
| 264 | { |
| 265 | if (! bm_has_1(bm, b, access_type)) |
| 266 | { |
| 267 | return False; |
| 268 | } |
| 269 | } |
| 270 | return True; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | Bool bm_has_any(const struct bitmap* const bm, |
bart | 3655612 | 2008-03-13 19:24:30 +0000 | [diff] [blame] | 274 | const Addr a1, const Addr a2, |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 275 | const BmAccessTypeT access_type) |
| 276 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 277 | Addr b; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 278 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 279 | tl_assert(bm); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 280 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 281 | for (b = a1; b < a2; b++) |
| 282 | { |
| 283 | if (bm_has_1(bm, b, access_type)) |
| 284 | { |
| 285 | return True; |
| 286 | } |
| 287 | } |
| 288 | return False; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | /* Return a non-zero value if there is a read access, write access or both */ |
| 292 | /* to any of the addresses in the range [ a1, a2 [ in bitmap bm. */ |
| 293 | UWord bm_has_any_access(const struct bitmap* const bm, |
| 294 | const Addr a1, |
| 295 | const Addr a2) |
| 296 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 297 | Addr b, b_next; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 298 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 299 | tl_assert(bm); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 300 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 301 | for (b = a1; b < a2; b = b_next) |
| 302 | { |
| 303 | struct bitmap2* bm2 = bm_lookup(bm, b); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 304 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 305 | b_next = (b & ~ADDR0_MASK) + ADDR0_COUNT; |
| 306 | if (b_next > a2) |
| 307 | { |
| 308 | b_next = a2; |
| 309 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 310 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 311 | if (bm2) |
| 312 | { |
| 313 | Addr b_start; |
| 314 | Addr b_end; |
| 315 | UWord b0; |
| 316 | const struct bitmap1* const p1 = &bm2->bm1; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 317 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 318 | if ((bm2->addr << ADDR0_BITS) < a1) |
| 319 | b_start = a1; |
| 320 | else |
| 321 | if ((bm2->addr << ADDR0_BITS) < a2) |
| 322 | b_start = (bm2->addr << ADDR0_BITS); |
| 323 | else |
| 324 | break; |
| 325 | tl_assert(a1 <= b_start && b_start <= a2); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 326 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 327 | if ((bm2->addr << ADDR0_BITS) + ADDR0_COUNT < a2) |
| 328 | b_end = (bm2->addr << ADDR0_BITS) + ADDR0_COUNT; |
| 329 | else |
| 330 | b_end = a2; |
| 331 | tl_assert(a1 <= b_end && b_end <= a2); |
| 332 | tl_assert(b_start < b_end); |
| 333 | tl_assert((b_start & ADDR0_MASK) <= ((b_end - 1) & ADDR0_MASK)); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 334 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 335 | for (b0 = b_start & ADDR0_MASK; b0 <= ((b_end-1) & ADDR0_MASK); b0++) |
| 336 | { |
| 337 | const UWord mask |
| 338 | = bm0_is_set(p1->bm0_r, b0) | bm0_is_set(p1->bm0_w, b0); |
| 339 | if (mask) |
| 340 | { |
| 341 | return mask; |
| 342 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 343 | } |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 344 | } |
| 345 | } |
| 346 | return 0; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | /** |
| 350 | * Report whether an access of type access_type at address a is recorded in |
| 351 | * bitmap bm. |
| 352 | * @return != 0 means true, and == 0 means false |
| 353 | */ |
| 354 | UWord bm_has_1(const struct bitmap* const bm, |
| 355 | const Addr a, |
| 356 | const BmAccessTypeT access_type) |
| 357 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 358 | struct bitmap2* p2; |
| 359 | struct bitmap1* p1; |
| 360 | UWord* p0; |
| 361 | const UWord a0 = a & ADDR0_MASK; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 362 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 363 | tl_assert(bm); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 364 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 365 | p2 = bm_lookup(bm, a); |
| 366 | if (p2) |
| 367 | { |
| 368 | p1 = &p2->bm1; |
| 369 | p0 = (access_type == eLoad) ? p1->bm0_r : p1->bm0_w; |
| 370 | return bm0_is_set(p0, a0); |
| 371 | } |
| 372 | return 0; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | static __inline__ |
| 376 | void bm1_clear(struct bitmap1* const bm1, const Addr a1, const Addr a2) |
| 377 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 378 | UWord idx; |
| 379 | UWord mask; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 380 | |
| 381 | #if 0 |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 382 | /* Commented out the statements below because of performance reasons. */ |
| 383 | tl_assert(a1); |
| 384 | tl_assert(a1 <= a2); |
| 385 | tl_assert(UWORD_MSB(a1) == UWORD_MSB(a2) |
| 386 | || UWORD_MSB(a1) == UWORD_MSB(a2 - 1)); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 387 | #endif |
| 388 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 389 | idx = (a1 & ADDR0_MASK) >> BITS_PER_BITS_PER_UWORD; |
| 390 | /* mask: a contiguous series of one bits. The first bit set is bit */ |
| 391 | /* UWORD_LSB(a2-1), and the last bit set is UWORD_LSB(a1). */ |
| 392 | mask = UWORD_LSB(a2) ? bm0_mask(a2) - bm0_mask(a1) : - bm0_mask(a1); |
| 393 | bm1->bm0_r[idx] &= ~mask; |
| 394 | bm1->bm0_w[idx] &= ~mask; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | void bm_clear_all(const struct bitmap* const bm) |
| 398 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 399 | struct bitmap2* bm2; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 400 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 401 | VG_(OSetGen_ResetIter)(bm->oset); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 402 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 403 | for ( ; (bm2 = VG_(OSetGen_Next)(bm->oset)) != 0; ) |
| 404 | { |
| 405 | struct bitmap1* const bm1 = &bm2->bm1; |
| 406 | tl_assert(bm1); |
| 407 | VG_(memset)(&bm1->bm0_r[0], 0, sizeof(bm1->bm0_r)); |
| 408 | VG_(memset)(&bm1->bm0_w[0], 0, sizeof(bm1->bm0_w)); |
| 409 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 410 | } |
| 411 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 412 | void bm_clear(const struct bitmap* const bm, |
| 413 | const Addr a1, |
| 414 | const Addr a2) |
| 415 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 416 | Addr b, b_next; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 417 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 418 | tl_assert(bm); |
| 419 | tl_assert(a1); |
| 420 | tl_assert(a1 <= a2); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 421 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 422 | for (b = a1; b < a2; b = b_next) |
| 423 | { |
| 424 | struct bitmap2* const p2 = bm_lookup(bm, b); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 425 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 426 | b_next = (b & ~ADDR0_MASK) + ADDR0_COUNT; |
| 427 | if (b_next > a2) |
| 428 | { |
| 429 | b_next = a2; |
| 430 | } |
| 431 | |
| 432 | if (p2) |
| 433 | { |
| 434 | Addr c = b; |
| 435 | if (UWORD_LSB(c)) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 436 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 437 | Addr c_next = UWORD_MSB(c) + BITS_PER_UWORD; |
| 438 | if (c_next > b_next) |
| 439 | c_next = b_next; |
| 440 | bm1_clear(&p2->bm1, c, c_next); |
| 441 | c = c_next; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 442 | } |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 443 | if (UWORD_LSB(c) == 0) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 444 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 445 | const Addr c_next = UWORD_MSB(b_next); |
| 446 | tl_assert(UWORD_LSB(c) == 0); |
| 447 | tl_assert(UWORD_LSB(c_next) == 0); |
| 448 | tl_assert(c_next <= b_next); |
| 449 | tl_assert(c <= c_next); |
| 450 | if (c_next > c) |
| 451 | { |
| 452 | UWord idx = (c & ADDR0_MASK) >> BITS_PER_BITS_PER_UWORD; |
| 453 | VG_(memset)(&p2->bm1.bm0_r[idx], 0, (c_next - c) / 8); |
| 454 | VG_(memset)(&p2->bm1.bm0_w[idx], 0, (c_next - c) / 8); |
| 455 | c = c_next; |
| 456 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 457 | } |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 458 | if (c != b_next) |
| 459 | { |
| 460 | bm1_clear(&p2->bm1, c, b_next); |
| 461 | } |
| 462 | } |
| 463 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 464 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 465 | |
bart | 3655612 | 2008-03-13 19:24:30 +0000 | [diff] [blame] | 466 | Bool bm_has_conflict_with(const struct bitmap* const bm, |
| 467 | const Addr a1, const Addr a2, |
| 468 | const BmAccessTypeT access_type) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 469 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 470 | Addr b, b_next; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 471 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 472 | tl_assert(bm); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 473 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 474 | for (b = a1; b < a2; b = b_next) |
| 475 | { |
| 476 | struct bitmap2* bm2 = bm_lookup(bm, b); |
bart | 3655612 | 2008-03-13 19:24:30 +0000 | [diff] [blame] | 477 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 478 | b_next = (b & ~ADDR0_MASK) + ADDR0_COUNT; |
| 479 | if (b_next > a2) |
| 480 | { |
| 481 | b_next = a2; |
| 482 | } |
bart | 3655612 | 2008-03-13 19:24:30 +0000 | [diff] [blame] | 483 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 484 | if (bm2) |
| 485 | { |
| 486 | Addr b_start; |
| 487 | Addr b_end; |
| 488 | UWord b0; |
| 489 | const struct bitmap1* const p1 = &bm2->bm1; |
bart | 3655612 | 2008-03-13 19:24:30 +0000 | [diff] [blame] | 490 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 491 | if ((bm2->addr << ADDR0_BITS) < a1) |
| 492 | b_start = a1; |
| 493 | else |
| 494 | if ((bm2->addr << ADDR0_BITS) < a2) |
| 495 | b_start = (bm2->addr << ADDR0_BITS); |
| 496 | else |
| 497 | break; |
| 498 | tl_assert(a1 <= b_start && b_start <= a2); |
bart | 3655612 | 2008-03-13 19:24:30 +0000 | [diff] [blame] | 499 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 500 | if ((bm2->addr << ADDR0_BITS) + ADDR0_COUNT < a2) |
| 501 | b_end = (bm2->addr << ADDR0_BITS) + ADDR0_COUNT; |
| 502 | else |
| 503 | b_end = a2; |
| 504 | tl_assert(a1 <= b_end && b_end <= a2); |
| 505 | tl_assert(b_start < b_end); |
| 506 | tl_assert((b_start & ADDR0_MASK) <= ((b_end - 1) & ADDR0_MASK)); |
bart | 3655612 | 2008-03-13 19:24:30 +0000 | [diff] [blame] | 507 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 508 | for (b0 = b_start & ADDR0_MASK; b0 <= ((b_end-1) & ADDR0_MASK); b0++) |
| 509 | { |
| 510 | if (access_type == eLoad) |
| 511 | { |
| 512 | if (bm0_is_set(p1->bm0_w, b0)) |
| 513 | { |
| 514 | return True; |
| 515 | } |
| 516 | } |
| 517 | else |
| 518 | { |
| 519 | tl_assert(access_type == eStore); |
| 520 | if (bm0_is_set(p1->bm0_r, b0) |
| 521 | | bm0_is_set(p1->bm0_w, b0)) |
| 522 | { |
| 523 | return True; |
| 524 | } |
| 525 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 526 | } |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 527 | } |
| 528 | } |
| 529 | return False; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 530 | } |
| 531 | |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 532 | static inline |
| 533 | Bool bm_aligned_load_has_conflict_with(const struct bitmap* const bm, |
| 534 | const Addr a1, const Addr a2) |
| 535 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 536 | struct bitmap2* bm2; |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 537 | |
| 538 | #if 0 |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 539 | /* Commented out the statements below because of performance reasons. */ |
| 540 | tl_assert(bm); |
| 541 | tl_assert(a1 < a2); |
| 542 | tl_assert((a2 - a1) == 1 || (a2 - a1) == 2 |
| 543 | || (a2 - a1) == 4 || (a2 - a1) == 8); |
| 544 | tl_assert((a1 & (a2 - a1 - 1)) == 0); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 545 | #endif |
| 546 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 547 | bm2 = bm_lookup(bm, a1); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 548 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 549 | if (bm2 |
| 550 | && bm0_is_any_set(bm2->bm1.bm0_w, a1 & ADDR0_MASK, (a2-1) & ADDR0_MASK)) |
| 551 | { |
| 552 | return True; |
| 553 | } |
| 554 | return False; |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | static inline |
| 558 | Bool bm_aligned_store_has_conflict_with(const struct bitmap* const bm, |
| 559 | const Addr a1, const Addr a2) |
| 560 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 561 | struct bitmap2* bm2; |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 562 | |
| 563 | #if 0 |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 564 | /* Commented out the statements below because of performance reasons. */ |
| 565 | tl_assert(bm); |
| 566 | tl_assert(a1 < a2); |
| 567 | tl_assert((a2 - a1) == 1 || (a2 - a1) == 2 |
| 568 | || (a2 - a1) == 4 || (a2 - a1) == 8); |
| 569 | tl_assert((a1 & (a2 - a1 - 1)) == 0); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 570 | #endif |
| 571 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 572 | bm2 = bm_lookup(bm, a1); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 573 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 574 | if (bm2) |
| 575 | { |
| 576 | const struct bitmap1* const p1 = &bm2->bm1; |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 577 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 578 | if (bm0_is_any_set(p1->bm0_r, a1 & ADDR0_MASK, (a2-1) & ADDR0_MASK) |
| 579 | | bm0_is_any_set(p1->bm0_w, a1 & ADDR0_MASK, (a2-1) & ADDR0_MASK)) |
| 580 | { |
| 581 | return True; |
| 582 | } |
| 583 | } |
| 584 | return False; |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 585 | } |
| 586 | |
bart | 3655612 | 2008-03-13 19:24:30 +0000 | [diff] [blame] | 587 | Bool bm_load_has_conflict_with(const struct bitmap* const bm, |
| 588 | const Addr a1, const Addr a2) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 589 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 590 | return bm_has_conflict_with(bm, a1, a2, eLoad); |
bart | 3655612 | 2008-03-13 19:24:30 +0000 | [diff] [blame] | 591 | } |
| 592 | |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 593 | Bool bm_load_1_has_conflict_with(const struct bitmap* const bm, const Addr a1) |
| 594 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 595 | return bm_aligned_load_has_conflict_with(bm, a1, a1 + 1); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | Bool bm_load_2_has_conflict_with(const struct bitmap* const bm, const Addr a1) |
| 599 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 600 | if ((a1 & 1) == 0) |
| 601 | return bm_aligned_load_has_conflict_with(bm, a1, a1 + 2); |
| 602 | else |
| 603 | return bm_has_conflict_with(bm, a1, a1 + 2, eLoad); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | Bool bm_load_4_has_conflict_with(const struct bitmap* const bm, const Addr a1) |
| 607 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 608 | if ((a1 & 3) == 0) |
| 609 | return bm_aligned_load_has_conflict_with(bm, a1, a1 + 4); |
| 610 | else |
| 611 | return bm_has_conflict_with(bm, a1, a1 + 4, eLoad); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | Bool bm_load_8_has_conflict_with(const struct bitmap* const bm, const Addr a1) |
| 615 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 616 | if ((a1 & 7) == 0) |
| 617 | return bm_aligned_load_has_conflict_with(bm, a1, a1 + 8); |
| 618 | else |
| 619 | return bm_has_conflict_with(bm, a1, a1 + 8, eLoad); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | Bool bm_store_1_has_conflict_with(const struct bitmap* const bm, const Addr a1) |
| 623 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 624 | return bm_aligned_store_has_conflict_with(bm, a1, a1 + 1); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | Bool bm_store_2_has_conflict_with(const struct bitmap* const bm, const Addr a1) |
| 628 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 629 | if ((a1 & 1) == 0) |
| 630 | return bm_aligned_store_has_conflict_with(bm, a1, a1 + 2); |
| 631 | else |
| 632 | return bm_has_conflict_with(bm, a1, a1 + 2, eStore); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | Bool bm_store_4_has_conflict_with(const struct bitmap* const bm, const Addr a1) |
| 636 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 637 | if ((a1 & 3) == 0) |
| 638 | return bm_aligned_store_has_conflict_with(bm, a1, a1 + 4); |
| 639 | else |
| 640 | return bm_has_conflict_with(bm, a1, a1 + 4, eStore); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | Bool bm_store_8_has_conflict_with(const struct bitmap* const bm, const Addr a1) |
| 644 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 645 | if ((a1 & 7) == 0) |
| 646 | return bm_aligned_store_has_conflict_with(bm, a1, a1 + 8); |
| 647 | else |
| 648 | return bm_has_conflict_with(bm, a1, a1 + 8, eStore); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 649 | } |
| 650 | |
bart | 3655612 | 2008-03-13 19:24:30 +0000 | [diff] [blame] | 651 | Bool bm_store_has_conflict_with(const struct bitmap* const bm, |
| 652 | const Addr a1, const Addr a2) |
| 653 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 654 | return bm_has_conflict_with(bm, a1, a2, eStore); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | void bm_swap(struct bitmap* const bm1, struct bitmap* const bm2) |
| 658 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 659 | OSet* const tmp = bm1->oset; |
| 660 | bm1->oset = bm2->oset; |
| 661 | bm2->oset = tmp; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | void bm_merge2(struct bitmap* const lhs, |
| 665 | const struct bitmap* const rhs) |
| 666 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 667 | struct bitmap2* bm2l; |
| 668 | const struct bitmap2* bm2r; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 669 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 670 | // First step: allocate any missing bitmaps in *lhs. |
| 671 | VG_(OSetGen_ResetIter)(rhs->oset); |
| 672 | for ( ; (bm2r = VG_(OSetGen_Next)(rhs->oset)) != 0; ) |
| 673 | { |
| 674 | bm2_lookup_or_insert(lhs, bm2r->addr); |
| 675 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 676 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 677 | VG_(OSetGen_ResetIter)(lhs->oset); |
| 678 | VG_(OSetGen_ResetIter)(rhs->oset); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 679 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 680 | for ( ; (bm2r = VG_(OSetGen_Next)(rhs->oset)) != 0; ) |
| 681 | { |
| 682 | do |
| 683 | { |
| 684 | bm2l = VG_(OSetGen_Next)(lhs->oset); |
| 685 | } while (bm2l->addr < bm2r->addr); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 686 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 687 | tl_assert(bm2l->addr == bm2r->addr); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 688 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 689 | bm2_merge(bm2l, bm2r); |
| 690 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | /** |
| 694 | * Report whether there are any RW / WR / WW patterns in lhs and rhs. |
| 695 | * @param lhs First bitmap. |
| 696 | * @param rhs Bitmap to be compared with lhs. |
| 697 | * @return !=0 if there are data races, == 0 if there are none. |
| 698 | */ |
| 699 | int bm_has_races(const struct bitmap* const lhs, |
| 700 | const struct bitmap* const rhs) |
| 701 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 702 | VG_(OSetGen_ResetIter)(lhs->oset); |
| 703 | VG_(OSetGen_ResetIter)(rhs->oset); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 704 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 705 | for (;;) |
| 706 | { |
| 707 | const struct bitmap2* bm2l = VG_(OSetGen_Next)(lhs->oset); |
| 708 | const struct bitmap2* bm2r = VG_(OSetGen_Next)(rhs->oset); |
| 709 | const struct bitmap1* bm1l; |
| 710 | const struct bitmap1* bm1r; |
| 711 | unsigned k; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 712 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 713 | while (bm2l && bm2r && bm2l->addr != bm2r->addr) |
| 714 | { |
| 715 | if (bm2l->addr < bm2r->addr) |
| 716 | bm2l = VG_(OSetGen_Next)(lhs->oset); |
| 717 | else |
| 718 | bm2r = VG_(OSetGen_Next)(rhs->oset); |
| 719 | } |
| 720 | if (bm2l == 0 || bm2r == 0) |
| 721 | break; |
| 722 | |
| 723 | bm1l = &bm2l->bm1; |
| 724 | bm1r = &bm2r->bm1; |
| 725 | |
| 726 | for (k = 0; k < BITMAP1_UWORD_COUNT; k++) |
| 727 | { |
| 728 | unsigned b; |
| 729 | for (b = 0; b < BITS_PER_UWORD; b++) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 730 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 731 | UWord const access |
| 732 | = ((bm1l->bm0_r[k] & bm0_mask(b)) ? LHS_R : 0) |
| 733 | | ((bm1l->bm0_w[k] & bm0_mask(b)) ? LHS_W : 0) |
| 734 | | ((bm1r->bm0_r[k] & bm0_mask(b)) ? RHS_R : 0) |
| 735 | | ((bm1r->bm0_w[k] & bm0_mask(b)) ? RHS_W : 0); |
| 736 | Addr const a = MAKE_ADDRESS(bm2l->addr, k * BITS_PER_UWORD | b); |
| 737 | if (HAS_RACE(access) && ! drd_is_suppressed(a, a + 1)) |
| 738 | { |
| 739 | return 1; |
| 740 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 741 | } |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 742 | } |
| 743 | } |
| 744 | return 0; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 745 | } |
| 746 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 747 | void bm_print(const struct bitmap* const bm) |
| 748 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 749 | struct bitmap2* bm2; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 750 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 751 | VG_(OSetGen_ResetIter)(bm->oset); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 752 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 753 | for ( ; (bm2 = VG_(OSetGen_Next)(bm->oset)) != 0; ) |
| 754 | { |
| 755 | const struct bitmap1* const bm1 = &bm2->bm1; |
| 756 | unsigned k; |
| 757 | for (k = 0; k < BITMAP1_UWORD_COUNT; k++) |
| 758 | { |
| 759 | unsigned b; |
| 760 | for (b = 0; b < BITS_PER_UWORD; b++) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 761 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 762 | int const r = bm1->bm0_r[k] & bm0_mask(b); |
| 763 | int const w = bm1->bm0_w[k] & bm0_mask(b); |
| 764 | Addr const a = MAKE_ADDRESS(bm2->addr, k * BITS_PER_UWORD | b); |
| 765 | if (r || w) |
| 766 | { |
| 767 | VG_(printf)("0x%08lx %c %c\n", |
| 768 | (Addr)(a), |
| 769 | w ? 'W' : ' ', r ? 'R' : ' '); |
| 770 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 771 | } |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 772 | } |
| 773 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | ULong bm_get_bitmap_creation_count(void) |
| 777 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 778 | return s_bitmap_creation_count; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 779 | } |
| 780 | |
| 781 | ULong bm_get_bitmap2_creation_count(void) |
| 782 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 783 | return s_bitmap2_creation_count; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | static void bm2_merge(struct bitmap2* const bm2l, |
| 787 | const struct bitmap2* const bm2r) |
| 788 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 789 | unsigned k; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 790 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 791 | tl_assert(bm2l->addr == bm2r->addr); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 792 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 793 | for (k = 0; k < BITMAP1_UWORD_COUNT; k++) |
| 794 | { |
| 795 | bm2l->bm1.bm0_r[k] |= bm2r->bm1.bm0_r[k]; |
| 796 | } |
| 797 | for (k = 0; k < BITMAP1_UWORD_COUNT; k++) |
| 798 | { |
| 799 | bm2l->bm1.bm0_w[k] |= bm2r->bm1.bm0_w[k]; |
| 800 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | #if 0 |
| 804 | |
| 805 | /* Unit test */ |
| 806 | static |
| 807 | struct { Addr address; SizeT size; BmAccessTypeT access_type; } |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 808 | s_args[] = { |
| 809 | { 0, 1, eLoad }, |
| 810 | { 666, 4, eLoad }, |
| 811 | { 667, 2, eStore }, |
| 812 | { 1024, 1, eStore }, |
| 813 | { 0x0000ffff, 1, eLoad }, |
| 814 | { 0x0001ffff, 1, eLoad }, |
| 815 | { 0x00ffffff, 1, eLoad }, |
| 816 | { 0xffffffff, 1, eStore }, |
| 817 | }; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 818 | |
| 819 | void bm_test(void) |
| 820 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 821 | struct bitmap* bm; |
| 822 | struct bitmap* bm2; |
| 823 | int i, j; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 824 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 825 | VG_(printf)("Start of DRD BM unit test.\n"); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 826 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 827 | bm = bm_new(); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 828 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 829 | for (i = 0; i < sizeof(s_args)/sizeof(s_args[0]); i++) |
| 830 | { |
| 831 | bm_access_range(bm, |
| 832 | s_args[i].address, |
| 833 | s_args[i].address + s_args[i].size, |
| 834 | s_args[i].access_type); |
| 835 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 836 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 837 | VG_(printf)("Map contents -- should contain 10 addresses:\n"); |
| 838 | bm_print(bm); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 839 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 840 | for (i = 0; i < sizeof(s_args)/sizeof(s_args[0]); i++) |
| 841 | { |
| 842 | for (j = 0; j < s_args[i].size; j++) |
| 843 | { |
| 844 | tl_assert(bm_has_1(bm, s_args[i].address + j, s_args[i].access_type)); |
| 845 | } |
| 846 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 847 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 848 | VG_(printf)("Merge result:\n"); |
| 849 | bm2 = bm_merge(bm, bm); |
| 850 | bm_print(bm); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 851 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 852 | bm_delete(bm); |
| 853 | bm_delete(bm2); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 854 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame^] | 855 | VG_(printf)("End of DRD BM unit test.\n"); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 856 | } |
| 857 | #endif |