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