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