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