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