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, |
bart | f8bc71d | 2008-03-15 11:42:34 +0000 | [diff] [blame] | 139 | const Addr a1, const SizeT size) |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 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 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 143 | bm2 = bm2_lookup_or_insert(bm, a1 >> ADDR0_BITS); |
bart | f8bc71d | 2008-03-15 11:42:34 +0000 | [diff] [blame] | 144 | bm0_set_range(bm2->bm1.bm0_r, a1 & ADDR0_MASK, size); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | static inline |
| 148 | void bm_access_aligned_store(struct bitmap* const bm, |
bart | f8bc71d | 2008-03-15 11:42:34 +0000 | [diff] [blame] | 149 | const Addr a1, const SizeT size) |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 150 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 151 | struct bitmap2* bm2; |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 152 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 153 | bm2 = bm2_lookup_or_insert(bm, a1 >> ADDR0_BITS); |
bart | f8bc71d | 2008-03-15 11:42:34 +0000 | [diff] [blame] | 154 | bm0_set_range(bm2->bm1.bm0_w, a1 & ADDR0_MASK, size); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 155 | } |
| 156 | |
bart | 3655612 | 2008-03-13 19:24:30 +0000 | [diff] [blame] | 157 | void bm_access_range_load(struct bitmap* const bm, |
| 158 | const Addr a1, const Addr a2) |
| 159 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 160 | bm_access_range(bm, a1, a2, eLoad); |
bart | 3655612 | 2008-03-13 19:24:30 +0000 | [diff] [blame] | 161 | } |
| 162 | |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 163 | void bm_access_load_1(struct bitmap* const bm, const Addr a1) |
| 164 | { |
bart | f8bc71d | 2008-03-15 11:42:34 +0000 | [diff] [blame] | 165 | bm_access_aligned_load(bm, a1, 1); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | void bm_access_load_2(struct bitmap* const bm, const Addr a1) |
| 169 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 170 | if ((a1 & 1) == 0) |
bart | f8bc71d | 2008-03-15 11:42:34 +0000 | [diff] [blame] | 171 | bm_access_aligned_load(bm, a1, 2); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 172 | else |
| 173 | bm_access_range(bm, a1, a1 + 2, eLoad); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | void bm_access_load_4(struct bitmap* const bm, const Addr a1) |
| 177 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 178 | if ((a1 & 3) == 0) |
bart | f8bc71d | 2008-03-15 11:42:34 +0000 | [diff] [blame] | 179 | bm_access_aligned_load(bm, a1, 4); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 180 | else |
| 181 | bm_access_range(bm, a1, a1 + 4, eLoad); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | void bm_access_load_8(struct bitmap* const bm, const Addr a1) |
| 185 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 186 | if ((a1 & 7) == 0) |
bart | f8bc71d | 2008-03-15 11:42:34 +0000 | [diff] [blame] | 187 | bm_access_aligned_load(bm, a1, 8); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 188 | else if ((a1 & 3) == 0) |
| 189 | { |
bart | f8bc71d | 2008-03-15 11:42:34 +0000 | [diff] [blame] | 190 | bm_access_aligned_load(bm, a1 + 0, 4); |
| 191 | bm_access_aligned_load(bm, a1 + 4, 4); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 192 | } |
| 193 | else |
| 194 | bm_access_range(bm, a1, a1 + 8, eLoad); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | void bm_access_store_1(struct bitmap* const bm, const Addr a1) |
| 198 | { |
bart | f8bc71d | 2008-03-15 11:42:34 +0000 | [diff] [blame] | 199 | bm_access_aligned_store(bm, a1, 1); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | void bm_access_store_2(struct bitmap* const bm, const Addr a1) |
| 203 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 204 | if ((a1 & 1) == 0) |
bart | f8bc71d | 2008-03-15 11:42:34 +0000 | [diff] [blame] | 205 | bm_access_aligned_store(bm, a1, 2); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 206 | else |
| 207 | bm_access_range(bm, a1, a1 + 2, eStore); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | void bm_access_store_4(struct bitmap* const bm, const Addr a1) |
| 211 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 212 | if ((a1 & 3) == 0) |
bart | f8bc71d | 2008-03-15 11:42:34 +0000 | [diff] [blame] | 213 | bm_access_aligned_store(bm, a1, 4); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 214 | else |
| 215 | bm_access_range(bm, a1, a1 + 4, eStore); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | void bm_access_store_8(struct bitmap* const bm, const Addr a1) |
| 219 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 220 | if ((a1 & 7) == 0) |
bart | f8bc71d | 2008-03-15 11:42:34 +0000 | [diff] [blame] | 221 | bm_access_aligned_store(bm, a1, 8); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 222 | else if ((a1 & 3) == 0) |
| 223 | { |
bart | f8bc71d | 2008-03-15 11:42:34 +0000 | [diff] [blame] | 224 | bm_access_aligned_store(bm, a1 + 0, 4); |
| 225 | bm_access_aligned_store(bm, a1 + 4, 4); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 226 | } |
| 227 | else |
| 228 | bm_access_range(bm, a1, a1 + 8, eStore); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 229 | } |
| 230 | |
bart | 3655612 | 2008-03-13 19:24:30 +0000 | [diff] [blame] | 231 | void bm_access_range_store(struct bitmap* const bm, |
| 232 | const Addr a1, const Addr a2) |
| 233 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 234 | bm_access_range(bm, a1, a2, eStore); |
bart | 3655612 | 2008-03-13 19:24:30 +0000 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | 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] | 238 | const BmAccessTypeT access_type) |
| 239 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 240 | Addr b; |
| 241 | for (b = a1; b < a2; b++) |
| 242 | { |
| 243 | if (! bm_has_1(bm, b, access_type)) |
| 244 | { |
| 245 | return False; |
| 246 | } |
| 247 | } |
| 248 | return True; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | Bool bm_has_any(const struct bitmap* const bm, |
bart | 3655612 | 2008-03-13 19:24:30 +0000 | [diff] [blame] | 252 | const Addr a1, const Addr a2, |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 253 | const BmAccessTypeT access_type) |
| 254 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 255 | Addr b; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 256 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 257 | tl_assert(bm); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 258 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 259 | for (b = a1; b < a2; b++) |
| 260 | { |
| 261 | if (bm_has_1(bm, b, access_type)) |
| 262 | { |
| 263 | return True; |
| 264 | } |
| 265 | } |
| 266 | return False; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | /* Return a non-zero value if there is a read access, write access or both */ |
| 270 | /* to any of the addresses in the range [ a1, a2 [ in bitmap bm. */ |
| 271 | UWord bm_has_any_access(const struct bitmap* const bm, |
| 272 | const Addr a1, |
| 273 | const Addr a2) |
| 274 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 275 | Addr b, b_next; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 276 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 277 | tl_assert(bm); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 278 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 279 | for (b = a1; b < a2; b = b_next) |
| 280 | { |
| 281 | struct bitmap2* bm2 = bm_lookup(bm, b); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 282 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 283 | b_next = (b & ~ADDR0_MASK) + ADDR0_COUNT; |
| 284 | if (b_next > a2) |
| 285 | { |
| 286 | b_next = a2; |
| 287 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 288 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 289 | if (bm2) |
| 290 | { |
| 291 | Addr b_start; |
| 292 | Addr b_end; |
| 293 | UWord b0; |
| 294 | const struct bitmap1* const p1 = &bm2->bm1; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 295 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 296 | if ((bm2->addr << ADDR0_BITS) < a1) |
| 297 | b_start = a1; |
| 298 | else |
| 299 | if ((bm2->addr << ADDR0_BITS) < a2) |
| 300 | b_start = (bm2->addr << ADDR0_BITS); |
| 301 | else |
| 302 | break; |
| 303 | tl_assert(a1 <= b_start && b_start <= a2); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 304 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 305 | if ((bm2->addr << ADDR0_BITS) + ADDR0_COUNT < a2) |
| 306 | b_end = (bm2->addr << ADDR0_BITS) + ADDR0_COUNT; |
| 307 | else |
| 308 | b_end = a2; |
| 309 | tl_assert(a1 <= b_end && b_end <= a2); |
| 310 | tl_assert(b_start < b_end); |
| 311 | tl_assert((b_start & ADDR0_MASK) <= ((b_end - 1) & ADDR0_MASK)); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 312 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 313 | for (b0 = b_start & ADDR0_MASK; b0 <= ((b_end-1) & ADDR0_MASK); b0++) |
| 314 | { |
| 315 | const UWord mask |
| 316 | = bm0_is_set(p1->bm0_r, b0) | bm0_is_set(p1->bm0_w, b0); |
| 317 | if (mask) |
| 318 | { |
| 319 | return mask; |
| 320 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 321 | } |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 322 | } |
| 323 | } |
| 324 | return 0; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | /** |
| 328 | * Report whether an access of type access_type at address a is recorded in |
| 329 | * bitmap bm. |
| 330 | * @return != 0 means true, and == 0 means false |
| 331 | */ |
| 332 | UWord bm_has_1(const struct bitmap* const bm, |
| 333 | const Addr a, |
| 334 | const BmAccessTypeT access_type) |
| 335 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 336 | struct bitmap2* p2; |
| 337 | struct bitmap1* p1; |
| 338 | UWord* p0; |
| 339 | const UWord a0 = a & ADDR0_MASK; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 340 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 341 | tl_assert(bm); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 342 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 343 | p2 = bm_lookup(bm, a); |
| 344 | if (p2) |
| 345 | { |
| 346 | p1 = &p2->bm1; |
| 347 | p0 = (access_type == eLoad) ? p1->bm0_r : p1->bm0_w; |
| 348 | return bm0_is_set(p0, a0); |
| 349 | } |
| 350 | return 0; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | static __inline__ |
| 354 | void bm1_clear(struct bitmap1* const bm1, const Addr a1, const Addr a2) |
| 355 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 356 | UWord idx; |
| 357 | UWord mask; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 358 | |
| 359 | #if 0 |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 360 | /* Commented out the statements below because of performance reasons. */ |
| 361 | tl_assert(a1); |
| 362 | tl_assert(a1 <= a2); |
| 363 | tl_assert(UWORD_MSB(a1) == UWORD_MSB(a2) |
| 364 | || UWORD_MSB(a1) == UWORD_MSB(a2 - 1)); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 365 | #endif |
| 366 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 367 | idx = (a1 & ADDR0_MASK) >> BITS_PER_BITS_PER_UWORD; |
| 368 | /* mask: a contiguous series of one bits. The first bit set is bit */ |
| 369 | /* UWORD_LSB(a2-1), and the last bit set is UWORD_LSB(a1). */ |
| 370 | mask = UWORD_LSB(a2) ? bm0_mask(a2) - bm0_mask(a1) : - bm0_mask(a1); |
| 371 | bm1->bm0_r[idx] &= ~mask; |
| 372 | bm1->bm0_w[idx] &= ~mask; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | void bm_clear_all(const struct bitmap* const bm) |
| 376 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 377 | struct bitmap2* bm2; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 378 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 379 | VG_(OSetGen_ResetIter)(bm->oset); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 380 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 381 | for ( ; (bm2 = VG_(OSetGen_Next)(bm->oset)) != 0; ) |
| 382 | { |
| 383 | struct bitmap1* const bm1 = &bm2->bm1; |
| 384 | tl_assert(bm1); |
| 385 | VG_(memset)(&bm1->bm0_r[0], 0, sizeof(bm1->bm0_r)); |
| 386 | VG_(memset)(&bm1->bm0_w[0], 0, sizeof(bm1->bm0_w)); |
| 387 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 388 | } |
| 389 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 390 | void bm_clear(const struct bitmap* const bm, |
| 391 | const Addr a1, |
| 392 | const Addr a2) |
| 393 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 394 | Addr b, b_next; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 395 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 396 | tl_assert(bm); |
| 397 | tl_assert(a1); |
| 398 | tl_assert(a1 <= a2); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 399 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 400 | for (b = a1; b < a2; b = b_next) |
| 401 | { |
| 402 | struct bitmap2* const p2 = bm_lookup(bm, b); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 403 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 404 | b_next = (b & ~ADDR0_MASK) + ADDR0_COUNT; |
| 405 | if (b_next > a2) |
| 406 | { |
| 407 | b_next = a2; |
| 408 | } |
| 409 | |
| 410 | if (p2) |
| 411 | { |
| 412 | Addr c = b; |
| 413 | if (UWORD_LSB(c)) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 414 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 415 | Addr c_next = UWORD_MSB(c) + BITS_PER_UWORD; |
| 416 | if (c_next > b_next) |
| 417 | c_next = b_next; |
| 418 | bm1_clear(&p2->bm1, c, c_next); |
| 419 | c = c_next; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 420 | } |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 421 | if (UWORD_LSB(c) == 0) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 422 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 423 | const Addr c_next = UWORD_MSB(b_next); |
| 424 | tl_assert(UWORD_LSB(c) == 0); |
| 425 | tl_assert(UWORD_LSB(c_next) == 0); |
| 426 | tl_assert(c_next <= b_next); |
| 427 | tl_assert(c <= c_next); |
| 428 | if (c_next > c) |
| 429 | { |
| 430 | UWord idx = (c & ADDR0_MASK) >> BITS_PER_BITS_PER_UWORD; |
| 431 | VG_(memset)(&p2->bm1.bm0_r[idx], 0, (c_next - c) / 8); |
| 432 | VG_(memset)(&p2->bm1.bm0_w[idx], 0, (c_next - c) / 8); |
| 433 | c = c_next; |
| 434 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 435 | } |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 436 | if (c != b_next) |
| 437 | { |
| 438 | bm1_clear(&p2->bm1, c, b_next); |
| 439 | } |
| 440 | } |
| 441 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 442 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 443 | |
bart | 3655612 | 2008-03-13 19:24:30 +0000 | [diff] [blame] | 444 | Bool bm_has_conflict_with(const struct bitmap* const bm, |
| 445 | const Addr a1, const Addr a2, |
| 446 | const BmAccessTypeT access_type) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 447 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 448 | Addr b, b_next; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 449 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 450 | tl_assert(bm); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 451 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 452 | for (b = a1; b < a2; b = b_next) |
| 453 | { |
| 454 | struct bitmap2* bm2 = bm_lookup(bm, b); |
bart | 3655612 | 2008-03-13 19:24:30 +0000 | [diff] [blame] | 455 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 456 | b_next = (b & ~ADDR0_MASK) + ADDR0_COUNT; |
| 457 | if (b_next > a2) |
| 458 | { |
| 459 | b_next = a2; |
| 460 | } |
bart | 3655612 | 2008-03-13 19:24:30 +0000 | [diff] [blame] | 461 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 462 | if (bm2) |
| 463 | { |
| 464 | Addr b_start; |
| 465 | Addr b_end; |
| 466 | UWord b0; |
| 467 | const struct bitmap1* const p1 = &bm2->bm1; |
bart | 3655612 | 2008-03-13 19:24:30 +0000 | [diff] [blame] | 468 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 469 | if ((bm2->addr << ADDR0_BITS) < a1) |
| 470 | b_start = a1; |
| 471 | else |
| 472 | if ((bm2->addr << ADDR0_BITS) < a2) |
| 473 | b_start = (bm2->addr << ADDR0_BITS); |
| 474 | else |
| 475 | break; |
| 476 | tl_assert(a1 <= b_start && b_start <= a2); |
bart | 3655612 | 2008-03-13 19:24:30 +0000 | [diff] [blame] | 477 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 478 | if ((bm2->addr << ADDR0_BITS) + ADDR0_COUNT < a2) |
| 479 | b_end = (bm2->addr << ADDR0_BITS) + ADDR0_COUNT; |
| 480 | else |
| 481 | b_end = a2; |
| 482 | tl_assert(a1 <= b_end && b_end <= a2); |
| 483 | tl_assert(b_start < b_end); |
| 484 | tl_assert((b_start & ADDR0_MASK) <= ((b_end - 1) & ADDR0_MASK)); |
bart | 3655612 | 2008-03-13 19:24:30 +0000 | [diff] [blame] | 485 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 486 | for (b0 = b_start & ADDR0_MASK; b0 <= ((b_end-1) & ADDR0_MASK); b0++) |
| 487 | { |
| 488 | if (access_type == eLoad) |
| 489 | { |
| 490 | if (bm0_is_set(p1->bm0_w, b0)) |
| 491 | { |
| 492 | return True; |
| 493 | } |
| 494 | } |
| 495 | else |
| 496 | { |
| 497 | tl_assert(access_type == eStore); |
| 498 | if (bm0_is_set(p1->bm0_r, b0) |
| 499 | | bm0_is_set(p1->bm0_w, b0)) |
| 500 | { |
| 501 | return True; |
| 502 | } |
| 503 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 504 | } |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 505 | } |
| 506 | } |
| 507 | return False; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 508 | } |
| 509 | |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 510 | static inline |
| 511 | Bool bm_aligned_load_has_conflict_with(const struct bitmap* const bm, |
bart | f8bc71d | 2008-03-15 11:42:34 +0000 | [diff] [blame] | 512 | const Addr a1, const SizeT size) |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 513 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 514 | struct bitmap2* bm2; |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 515 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 516 | bm2 = bm_lookup(bm, a1); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 517 | |
bart | f8bc71d | 2008-03-15 11:42:34 +0000 | [diff] [blame] | 518 | return (bm2 && bm0_is_any_set(bm2->bm1.bm0_w, a1 & ADDR0_MASK, size)); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | static inline |
| 522 | Bool bm_aligned_store_has_conflict_with(const struct bitmap* const bm, |
bart | f8bc71d | 2008-03-15 11:42:34 +0000 | [diff] [blame] | 523 | const Addr a1, const SizeT size) |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 524 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 525 | struct bitmap2* bm2; |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 526 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 527 | bm2 = bm_lookup(bm, a1); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 528 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 529 | if (bm2) |
| 530 | { |
bart | f8bc71d | 2008-03-15 11:42:34 +0000 | [diff] [blame] | 531 | if (bm0_is_any_set(bm2->bm1.bm0_r, a1 & ADDR0_MASK, size) |
| 532 | | bm0_is_any_set(bm2->bm1.bm0_w, a1 & ADDR0_MASK, size)) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 533 | { |
| 534 | return True; |
| 535 | } |
| 536 | } |
| 537 | return False; |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 538 | } |
| 539 | |
bart | 3655612 | 2008-03-13 19:24:30 +0000 | [diff] [blame] | 540 | Bool bm_load_has_conflict_with(const struct bitmap* const bm, |
| 541 | const Addr a1, const Addr a2) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 542 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 543 | return bm_has_conflict_with(bm, a1, a2, eLoad); |
bart | 3655612 | 2008-03-13 19:24:30 +0000 | [diff] [blame] | 544 | } |
| 545 | |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 546 | Bool bm_load_1_has_conflict_with(const struct bitmap* const bm, const Addr a1) |
| 547 | { |
bart | f8bc71d | 2008-03-15 11:42:34 +0000 | [diff] [blame] | 548 | return bm_aligned_load_has_conflict_with(bm, a1, 1); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | Bool bm_load_2_has_conflict_with(const struct bitmap* const bm, const Addr a1) |
| 552 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 553 | if ((a1 & 1) == 0) |
bart | f8bc71d | 2008-03-15 11:42:34 +0000 | [diff] [blame] | 554 | return bm_aligned_load_has_conflict_with(bm, a1, 2); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 555 | else |
| 556 | return bm_has_conflict_with(bm, a1, a1 + 2, eLoad); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | Bool bm_load_4_has_conflict_with(const struct bitmap* const bm, const Addr a1) |
| 560 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 561 | if ((a1 & 3) == 0) |
bart | f8bc71d | 2008-03-15 11:42:34 +0000 | [diff] [blame] | 562 | return bm_aligned_load_has_conflict_with(bm, a1, 4); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 563 | else |
| 564 | return bm_has_conflict_with(bm, a1, a1 + 4, eLoad); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | Bool bm_load_8_has_conflict_with(const struct bitmap* const bm, const Addr a1) |
| 568 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 569 | if ((a1 & 7) == 0) |
bart | f8bc71d | 2008-03-15 11:42:34 +0000 | [diff] [blame] | 570 | return bm_aligned_load_has_conflict_with(bm, a1, 8); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 571 | else |
| 572 | return bm_has_conflict_with(bm, a1, a1 + 8, eLoad); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | Bool bm_store_1_has_conflict_with(const struct bitmap* const bm, const Addr a1) |
| 576 | { |
bart | f8bc71d | 2008-03-15 11:42:34 +0000 | [diff] [blame] | 577 | return bm_aligned_store_has_conflict_with(bm, a1, 1); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | Bool bm_store_2_has_conflict_with(const struct bitmap* const bm, const Addr a1) |
| 581 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 582 | if ((a1 & 1) == 0) |
bart | f8bc71d | 2008-03-15 11:42:34 +0000 | [diff] [blame] | 583 | return bm_aligned_store_has_conflict_with(bm, a1, 2); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 584 | else |
| 585 | return bm_has_conflict_with(bm, a1, a1 + 2, eStore); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | Bool bm_store_4_has_conflict_with(const struct bitmap* const bm, const Addr a1) |
| 589 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 590 | if ((a1 & 3) == 0) |
bart | f8bc71d | 2008-03-15 11:42:34 +0000 | [diff] [blame] | 591 | return bm_aligned_store_has_conflict_with(bm, a1, 4); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 592 | else |
| 593 | return bm_has_conflict_with(bm, a1, a1 + 4, eStore); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | Bool bm_store_8_has_conflict_with(const struct bitmap* const bm, const Addr a1) |
| 597 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 598 | if ((a1 & 7) == 0) |
bart | f8bc71d | 2008-03-15 11:42:34 +0000 | [diff] [blame] | 599 | return bm_aligned_store_has_conflict_with(bm, a1, 8); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 600 | else |
| 601 | return bm_has_conflict_with(bm, a1, a1 + 8, eStore); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 602 | } |
| 603 | |
bart | 3655612 | 2008-03-13 19:24:30 +0000 | [diff] [blame] | 604 | Bool bm_store_has_conflict_with(const struct bitmap* const bm, |
| 605 | const Addr a1, const Addr a2) |
| 606 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 607 | return bm_has_conflict_with(bm, a1, a2, eStore); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | void bm_swap(struct bitmap* const bm1, struct bitmap* const bm2) |
| 611 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 612 | OSet* const tmp = bm1->oset; |
| 613 | bm1->oset = bm2->oset; |
| 614 | bm2->oset = tmp; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | void bm_merge2(struct bitmap* const lhs, |
| 618 | const struct bitmap* const rhs) |
| 619 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 620 | struct bitmap2* bm2l; |
| 621 | const struct bitmap2* bm2r; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 622 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 623 | // First step: allocate any missing bitmaps in *lhs. |
| 624 | VG_(OSetGen_ResetIter)(rhs->oset); |
| 625 | for ( ; (bm2r = VG_(OSetGen_Next)(rhs->oset)) != 0; ) |
| 626 | { |
| 627 | bm2_lookup_or_insert(lhs, bm2r->addr); |
| 628 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 629 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 630 | VG_(OSetGen_ResetIter)(lhs->oset); |
| 631 | VG_(OSetGen_ResetIter)(rhs->oset); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 632 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 633 | for ( ; (bm2r = VG_(OSetGen_Next)(rhs->oset)) != 0; ) |
| 634 | { |
| 635 | do |
| 636 | { |
| 637 | bm2l = VG_(OSetGen_Next)(lhs->oset); |
| 638 | } while (bm2l->addr < bm2r->addr); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 639 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 640 | tl_assert(bm2l->addr == bm2r->addr); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 641 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 642 | bm2_merge(bm2l, bm2r); |
| 643 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | /** |
| 647 | * Report whether there are any RW / WR / WW patterns in lhs and rhs. |
| 648 | * @param lhs First bitmap. |
| 649 | * @param rhs Bitmap to be compared with lhs. |
| 650 | * @return !=0 if there are data races, == 0 if there are none. |
| 651 | */ |
| 652 | int bm_has_races(const struct bitmap* const lhs, |
| 653 | const struct bitmap* const rhs) |
| 654 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 655 | VG_(OSetGen_ResetIter)(lhs->oset); |
| 656 | VG_(OSetGen_ResetIter)(rhs->oset); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 657 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 658 | for (;;) |
| 659 | { |
| 660 | const struct bitmap2* bm2l = VG_(OSetGen_Next)(lhs->oset); |
| 661 | const struct bitmap2* bm2r = VG_(OSetGen_Next)(rhs->oset); |
| 662 | const struct bitmap1* bm1l; |
| 663 | const struct bitmap1* bm1r; |
| 664 | unsigned k; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 665 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 666 | while (bm2l && bm2r && bm2l->addr != bm2r->addr) |
| 667 | { |
| 668 | if (bm2l->addr < bm2r->addr) |
| 669 | bm2l = VG_(OSetGen_Next)(lhs->oset); |
| 670 | else |
| 671 | bm2r = VG_(OSetGen_Next)(rhs->oset); |
| 672 | } |
| 673 | if (bm2l == 0 || bm2r == 0) |
| 674 | break; |
| 675 | |
| 676 | bm1l = &bm2l->bm1; |
| 677 | bm1r = &bm2r->bm1; |
| 678 | |
| 679 | for (k = 0; k < BITMAP1_UWORD_COUNT; k++) |
| 680 | { |
| 681 | unsigned b; |
| 682 | for (b = 0; b < BITS_PER_UWORD; b++) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 683 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 684 | UWord const access |
| 685 | = ((bm1l->bm0_r[k] & bm0_mask(b)) ? LHS_R : 0) |
| 686 | | ((bm1l->bm0_w[k] & bm0_mask(b)) ? LHS_W : 0) |
| 687 | | ((bm1r->bm0_r[k] & bm0_mask(b)) ? RHS_R : 0) |
| 688 | | ((bm1r->bm0_w[k] & bm0_mask(b)) ? RHS_W : 0); |
| 689 | Addr const a = MAKE_ADDRESS(bm2l->addr, k * BITS_PER_UWORD | b); |
| 690 | if (HAS_RACE(access) && ! drd_is_suppressed(a, a + 1)) |
| 691 | { |
| 692 | return 1; |
| 693 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 694 | } |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 695 | } |
| 696 | } |
| 697 | return 0; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 698 | } |
| 699 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 700 | void bm_print(const struct bitmap* const bm) |
| 701 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 702 | struct bitmap2* bm2; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 703 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 704 | VG_(OSetGen_ResetIter)(bm->oset); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 705 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 706 | for ( ; (bm2 = VG_(OSetGen_Next)(bm->oset)) != 0; ) |
| 707 | { |
| 708 | const struct bitmap1* const bm1 = &bm2->bm1; |
| 709 | unsigned k; |
| 710 | for (k = 0; k < BITMAP1_UWORD_COUNT; k++) |
| 711 | { |
| 712 | unsigned b; |
| 713 | for (b = 0; b < BITS_PER_UWORD; b++) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 714 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 715 | int const r = bm1->bm0_r[k] & bm0_mask(b); |
| 716 | int const w = bm1->bm0_w[k] & bm0_mask(b); |
| 717 | Addr const a = MAKE_ADDRESS(bm2->addr, k * BITS_PER_UWORD | b); |
| 718 | if (r || w) |
| 719 | { |
| 720 | VG_(printf)("0x%08lx %c %c\n", |
| 721 | (Addr)(a), |
| 722 | w ? 'W' : ' ', r ? 'R' : ' '); |
| 723 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 724 | } |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 725 | } |
| 726 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | ULong bm_get_bitmap_creation_count(void) |
| 730 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 731 | return s_bitmap_creation_count; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | ULong bm_get_bitmap2_creation_count(void) |
| 735 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 736 | return s_bitmap2_creation_count; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | static void bm2_merge(struct bitmap2* const bm2l, |
| 740 | const struct bitmap2* const bm2r) |
| 741 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 742 | unsigned k; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 743 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 744 | tl_assert(bm2l->addr == bm2r->addr); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 745 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 746 | for (k = 0; k < BITMAP1_UWORD_COUNT; k++) |
| 747 | { |
| 748 | bm2l->bm1.bm0_r[k] |= bm2r->bm1.bm0_r[k]; |
| 749 | } |
| 750 | for (k = 0; k < BITMAP1_UWORD_COUNT; k++) |
| 751 | { |
| 752 | bm2l->bm1.bm0_w[k] |= bm2r->bm1.bm0_w[k]; |
| 753 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | #if 0 |
| 757 | |
| 758 | /* Unit test */ |
| 759 | static |
| 760 | struct { Addr address; SizeT size; BmAccessTypeT access_type; } |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 761 | s_args[] = { |
| 762 | { 0, 1, eLoad }, |
| 763 | { 666, 4, eLoad }, |
| 764 | { 667, 2, eStore }, |
| 765 | { 1024, 1, eStore }, |
| 766 | { 0x0000ffff, 1, eLoad }, |
| 767 | { 0x0001ffff, 1, eLoad }, |
| 768 | { 0x00ffffff, 1, eLoad }, |
| 769 | { 0xffffffff, 1, eStore }, |
| 770 | }; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 771 | |
| 772 | void bm_test(void) |
| 773 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 774 | struct bitmap* bm; |
| 775 | struct bitmap* bm2; |
| 776 | int i, j; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 777 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 778 | VG_(printf)("Start of DRD BM unit test.\n"); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 779 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 780 | bm = bm_new(); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 781 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 782 | for (i = 0; i < sizeof(s_args)/sizeof(s_args[0]); i++) |
| 783 | { |
| 784 | bm_access_range(bm, |
| 785 | s_args[i].address, |
| 786 | s_args[i].address + s_args[i].size, |
| 787 | s_args[i].access_type); |
| 788 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 789 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 790 | VG_(printf)("Map contents -- should contain 10 addresses:\n"); |
| 791 | bm_print(bm); |
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 (i = 0; i < sizeof(s_args)/sizeof(s_args[0]); i++) |
| 794 | { |
| 795 | for (j = 0; j < s_args[i].size; j++) |
| 796 | { |
| 797 | tl_assert(bm_has_1(bm, s_args[i].address + j, s_args[i].access_type)); |
| 798 | } |
| 799 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 800 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 801 | VG_(printf)("Merge result:\n"); |
| 802 | bm2 = bm_merge(bm, bm); |
| 803 | bm_print(bm); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 804 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 805 | bm_delete(bm); |
| 806 | bm_delete(bm2); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 807 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 808 | VG_(printf)("End of DRD BM unit test.\n"); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 809 | } |
| 810 | #endif |