nethercote | bb1c991 | 2004-01-04 16:43:23 +0000 | [diff] [blame] | 1 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2 | /*--------------------------------------------------------------------*/ |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3 | /*--- Instrument IR to perform memory checking operations. ---*/ |
njn25 | cac76cb | 2002-09-23 11:21:57 +0000 | [diff] [blame] | 4 | /*--- mc_translate.c ---*/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 5 | /*--------------------------------------------------------------------*/ |
njn | c953984 | 2002-10-02 13:26:35 +0000 | [diff] [blame] | 6 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 7 | /* |
nethercote | 137bc55 | 2003-11-14 17:47:54 +0000 | [diff] [blame] | 8 | This file is part of MemCheck, a heavyweight Valgrind tool for |
njn | c953984 | 2002-10-02 13:26:35 +0000 | [diff] [blame] | 9 | detecting memory errors. |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 10 | |
sewardj | 9eecbbb | 2010-05-03 21:37:12 +0000 | [diff] [blame] | 11 | Copyright (C) 2000-2010 Julian Seward |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 12 | jseward@acm.org |
| 13 | |
| 14 | This program is free software; you can redistribute it and/or |
| 15 | modify it under the terms of the GNU General Public License as |
| 16 | published by the Free Software Foundation; either version 2 of the |
| 17 | License, or (at your option) any later version. |
| 18 | |
| 19 | This program is distributed in the hope that it will be useful, but |
| 20 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 22 | General Public License for more details. |
| 23 | |
| 24 | You should have received a copy of the GNU General Public License |
| 25 | along with this program; if not, write to the Free Software |
| 26 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 27 | 02111-1307, USA. |
| 28 | |
| 29 | The GNU General Public License is contained in the file COPYING. |
| 30 | */ |
| 31 | |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 32 | #include "pub_tool_basics.h" |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 33 | #include "pub_tool_hashtable.h" // For mc_include.h |
njn | 132bfcc | 2005-06-04 19:16:06 +0000 | [diff] [blame] | 34 | #include "pub_tool_libcassert.h" |
njn | 36a20fa | 2005-06-03 03:08:39 +0000 | [diff] [blame] | 35 | #include "pub_tool_libcprint.h" |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 36 | #include "pub_tool_tooliface.h" |
sewardj | 53ee1fc | 2005-12-23 02:29:58 +0000 | [diff] [blame] | 37 | #include "pub_tool_machine.h" // VG_(fnptr_to_fnentry) |
sewardj | 81651dc | 2007-08-28 06:05:20 +0000 | [diff] [blame] | 38 | #include "pub_tool_xarray.h" |
| 39 | #include "pub_tool_mallocfree.h" |
| 40 | #include "pub_tool_libcbase.h" |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 41 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 42 | #include "mc_include.h" |
| 43 | |
| 44 | |
sewardj | 7ee7d85 | 2011-06-16 11:37:21 +0000 | [diff] [blame^] | 45 | /* FIXMEs JRS 2011-June-16. |
| 46 | |
| 47 | Check the interpretation for vector narrowing and widening ops, |
| 48 | particularly the saturating ones. I suspect they are either overly |
| 49 | pessimistic and/or wrong. |
| 50 | */ |
| 51 | |
sewardj | 992dff9 | 2005-10-07 11:08:55 +0000 | [diff] [blame] | 52 | /* This file implements the Memcheck instrumentation, and in |
| 53 | particular contains the core of its undefined value detection |
| 54 | machinery. For a comprehensive background of the terminology, |
| 55 | algorithms and rationale used herein, read: |
| 56 | |
| 57 | Using Valgrind to detect undefined value errors with |
| 58 | bit-precision |
| 59 | |
| 60 | Julian Seward and Nicholas Nethercote |
| 61 | |
| 62 | 2005 USENIX Annual Technical Conference (General Track), |
| 63 | Anaheim, CA, USA, April 10-15, 2005. |
njn | 6665ea2 | 2007-05-24 23:14:41 +0000 | [diff] [blame] | 64 | |
| 65 | ---- |
| 66 | |
| 67 | Here is as good a place as any to record exactly when V bits are and |
| 68 | should be checked, why, and what function is responsible. |
| 69 | |
| 70 | |
| 71 | Memcheck complains when an undefined value is used: |
| 72 | |
| 73 | 1. In the condition of a conditional branch. Because it could cause |
| 74 | incorrect control flow, and thus cause incorrect externally-visible |
| 75 | behaviour. [mc_translate.c:complainIfUndefined] |
| 76 | |
| 77 | 2. As an argument to a system call, or as the value that specifies |
| 78 | the system call number. Because it could cause an incorrect |
| 79 | externally-visible side effect. [mc_translate.c:mc_pre_reg_read] |
| 80 | |
| 81 | 3. As the address in a load or store. Because it could cause an |
| 82 | incorrect value to be used later, which could cause externally-visible |
| 83 | behaviour (eg. via incorrect control flow or an incorrect system call |
| 84 | argument) [complainIfUndefined] |
| 85 | |
| 86 | 4. As the target address of a branch. Because it could cause incorrect |
| 87 | control flow. [complainIfUndefined] |
| 88 | |
| 89 | 5. As an argument to setenv, unsetenv, or putenv. Because it could put |
| 90 | an incorrect value into the external environment. |
| 91 | [mc_replace_strmem.c:VG_WRAP_FUNCTION_ZU(*, *env)] |
| 92 | |
| 93 | 6. As the index in a GETI or PUTI operation. I'm not sure why... (njn). |
| 94 | [complainIfUndefined] |
| 95 | |
| 96 | 7. As an argument to the VALGRIND_CHECK_MEM_IS_DEFINED and |
| 97 | VALGRIND_CHECK_VALUE_IS_DEFINED client requests. Because the user |
| 98 | requested it. [in memcheck.h] |
| 99 | |
| 100 | |
| 101 | Memcheck also complains, but should not, when an undefined value is used: |
| 102 | |
| 103 | 8. As the shift value in certain SIMD shift operations (but not in the |
| 104 | standard integer shift operations). This inconsistency is due to |
| 105 | historical reasons.) [complainIfUndefined] |
| 106 | |
| 107 | |
| 108 | Memcheck does not complain, but should, when an undefined value is used: |
| 109 | |
| 110 | 9. As an input to a client request. Because the client request may |
| 111 | affect the visible behaviour -- see bug #144362 for an example |
| 112 | involving the malloc replacements in vg_replace_malloc.c and |
| 113 | VALGRIND_NON_SIMD_CALL* requests, where an uninitialised argument |
| 114 | isn't identified. That bug report also has some info on how to solve |
| 115 | the problem. [valgrind.h:VALGRIND_DO_CLIENT_REQUEST] |
| 116 | |
| 117 | |
| 118 | In practice, 1 and 2 account for the vast majority of cases. |
sewardj | 992dff9 | 2005-10-07 11:08:55 +0000 | [diff] [blame] | 119 | */ |
| 120 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 121 | /*------------------------------------------------------------*/ |
| 122 | /*--- Forward decls ---*/ |
| 123 | /*------------------------------------------------------------*/ |
| 124 | |
| 125 | struct _MCEnv; |
| 126 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 127 | static IRType shadowTypeV ( IRType ty ); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 128 | static IRExpr* expr2vbits ( struct _MCEnv* mce, IRExpr* e ); |
sewardj | afa617b | 2008-07-22 09:59:48 +0000 | [diff] [blame] | 129 | static IRTemp findShadowTmpB ( struct _MCEnv* mce, IRTemp orig ); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 130 | |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 131 | static IRExpr *i128_const_zero(void); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 132 | |
| 133 | /*------------------------------------------------------------*/ |
| 134 | /*--- Memcheck running state, and tmp management. ---*/ |
| 135 | /*------------------------------------------------------------*/ |
| 136 | |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 137 | /* Carries info about a particular tmp. The tmp's number is not |
| 138 | recorded, as this is implied by (equal to) its index in the tmpMap |
| 139 | in MCEnv. The tmp's type is also not recorded, as this is present |
| 140 | in MCEnv.sb->tyenv. |
| 141 | |
| 142 | When .kind is Orig, .shadowV and .shadowB may give the identities |
| 143 | of the temps currently holding the associated definedness (shadowV) |
| 144 | and origin (shadowB) values, or these may be IRTemp_INVALID if code |
| 145 | to compute such values has not yet been emitted. |
| 146 | |
| 147 | When .kind is VSh or BSh then the tmp is holds a V- or B- value, |
| 148 | and so .shadowV and .shadowB must be IRTemp_INVALID, since it is |
| 149 | illogical for a shadow tmp itself to be shadowed. |
| 150 | */ |
| 151 | typedef |
| 152 | enum { Orig=1, VSh=2, BSh=3 } |
| 153 | TempKind; |
| 154 | |
| 155 | typedef |
| 156 | struct { |
| 157 | TempKind kind; |
| 158 | IRTemp shadowV; |
| 159 | IRTemp shadowB; |
| 160 | } |
| 161 | TempMapEnt; |
| 162 | |
| 163 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 164 | /* Carries around state during memcheck instrumentation. */ |
| 165 | typedef |
| 166 | struct _MCEnv { |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 167 | /* MODIFIED: the superblock being constructed. IRStmts are |
| 168 | added. */ |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 169 | IRSB* sb; |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 170 | Bool trace; |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 171 | |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 172 | /* MODIFIED: a table [0 .. #temps_in_sb-1] which gives the |
| 173 | current kind and possibly shadow temps for each temp in the |
| 174 | IRSB being constructed. Note that it does not contain the |
| 175 | type of each tmp. If you want to know the type, look at the |
| 176 | relevant entry in sb->tyenv. It follows that at all times |
| 177 | during the instrumentation process, the valid indices for |
| 178 | tmpMap and sb->tyenv are identical, being 0 .. N-1 where N is |
| 179 | total number of Orig, V- and B- temps allocated so far. |
| 180 | |
| 181 | The reason for this strange split (types in one place, all |
| 182 | other info in another) is that we need the types to be |
| 183 | attached to sb so as to make it possible to do |
| 184 | "typeOfIRExpr(mce->bb->tyenv, ...)" at various places in the |
| 185 | instrumentation process. */ |
| 186 | XArray* /* of TempMapEnt */ tmpMap; |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 187 | |
sewardj | d5204dc | 2004-12-31 01:16:11 +0000 | [diff] [blame] | 188 | /* MODIFIED: indicates whether "bogus" literals have so far been |
| 189 | found. Starts off False, and may change to True. */ |
| 190 | Bool bogusLiterals; |
| 191 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 192 | /* READONLY: the guest layout. This indicates which parts of |
| 193 | the guest state should be regarded as 'always defined'. */ |
| 194 | VexGuestLayout* layout; |
sewardj | 634ba77 | 2006-10-15 12:47:37 +0000 | [diff] [blame] | 195 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 196 | /* READONLY: the host word type. Needed for constructing |
| 197 | arguments of type 'HWord' to be passed to helper functions. |
| 198 | Ity_I32 or Ity_I64 only. */ |
| 199 | IRType hWordTy; |
| 200 | } |
| 201 | MCEnv; |
| 202 | |
| 203 | /* SHADOW TMP MANAGEMENT. Shadow tmps are allocated lazily (on |
| 204 | demand), as they are encountered. This is for two reasons. |
| 205 | |
| 206 | (1) (less important reason): Many original tmps are unused due to |
| 207 | initial IR optimisation, and we do not want to spaces in tables |
| 208 | tracking them. |
| 209 | |
| 210 | Shadow IRTemps are therefore allocated on demand. mce.tmpMap is a |
| 211 | table indexed [0 .. n_types-1], which gives the current shadow for |
| 212 | each original tmp, or INVALID_IRTEMP if none is so far assigned. |
| 213 | It is necessary to support making multiple assignments to a shadow |
| 214 | -- specifically, after testing a shadow for definedness, it needs |
| 215 | to be made defined. But IR's SSA property disallows this. |
| 216 | |
| 217 | (2) (more important reason): Therefore, when a shadow needs to get |
| 218 | a new value, a new temporary is created, the value is assigned to |
| 219 | that, and the tmpMap is updated to reflect the new binding. |
| 220 | |
| 221 | A corollary is that if the tmpMap maps a given tmp to |
sewardj | f1962d3 | 2006-10-19 13:22:16 +0000 | [diff] [blame] | 222 | IRTemp_INVALID and we are hoping to read that shadow tmp, it means |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 223 | there's a read-before-write error in the original tmps. The IR |
| 224 | sanity checker should catch all such anomalies, however. |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 225 | */ |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 226 | |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 227 | /* Create a new IRTemp of type 'ty' and kind 'kind', and add it to |
| 228 | both the table in mce->sb and to our auxiliary mapping. Note that |
| 229 | newTemp may cause mce->tmpMap to resize, hence previous results |
| 230 | from VG_(indexXA)(mce->tmpMap) are invalidated. */ |
| 231 | static IRTemp newTemp ( MCEnv* mce, IRType ty, TempKind kind ) |
| 232 | { |
| 233 | Word newIx; |
| 234 | TempMapEnt ent; |
| 235 | IRTemp tmp = newIRTemp(mce->sb->tyenv, ty); |
| 236 | ent.kind = kind; |
| 237 | ent.shadowV = IRTemp_INVALID; |
| 238 | ent.shadowB = IRTemp_INVALID; |
| 239 | newIx = VG_(addToXA)( mce->tmpMap, &ent ); |
| 240 | tl_assert(newIx == (Word)tmp); |
| 241 | return tmp; |
| 242 | } |
| 243 | |
| 244 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 245 | /* Find the tmp currently shadowing the given original tmp. If none |
| 246 | so far exists, allocate one. */ |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 247 | static IRTemp findShadowTmpV ( MCEnv* mce, IRTemp orig ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 248 | { |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 249 | TempMapEnt* ent; |
| 250 | /* VG_(indexXA) range-checks 'orig', hence no need to check |
| 251 | here. */ |
| 252 | ent = (TempMapEnt*)VG_(indexXA)( mce->tmpMap, (Word)orig ); |
| 253 | tl_assert(ent->kind == Orig); |
| 254 | if (ent->shadowV == IRTemp_INVALID) { |
| 255 | IRTemp tmpV |
| 256 | = newTemp( mce, shadowTypeV(mce->sb->tyenv->types[orig]), VSh ); |
| 257 | /* newTemp may cause mce->tmpMap to resize, hence previous results |
| 258 | from VG_(indexXA) are invalid. */ |
| 259 | ent = (TempMapEnt*)VG_(indexXA)( mce->tmpMap, (Word)orig ); |
| 260 | tl_assert(ent->kind == Orig); |
| 261 | tl_assert(ent->shadowV == IRTemp_INVALID); |
| 262 | ent->shadowV = tmpV; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 263 | } |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 264 | return ent->shadowV; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 265 | } |
| 266 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 267 | /* Allocate a new shadow for the given original tmp. This means any |
| 268 | previous shadow is abandoned. This is needed because it is |
| 269 | necessary to give a new value to a shadow once it has been tested |
| 270 | for undefinedness, but unfortunately IR's SSA property disallows |
| 271 | this. Instead we must abandon the old shadow, allocate a new one |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 272 | and use that instead. |
| 273 | |
| 274 | This is the same as findShadowTmpV, except we don't bother to see |
| 275 | if a shadow temp already existed -- we simply allocate a new one |
| 276 | regardless. */ |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 277 | static void newShadowTmpV ( MCEnv* mce, IRTemp orig ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 278 | { |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 279 | TempMapEnt* ent; |
| 280 | /* VG_(indexXA) range-checks 'orig', hence no need to check |
| 281 | here. */ |
| 282 | ent = (TempMapEnt*)VG_(indexXA)( mce->tmpMap, (Word)orig ); |
| 283 | tl_assert(ent->kind == Orig); |
| 284 | if (1) { |
| 285 | IRTemp tmpV |
| 286 | = newTemp( mce, shadowTypeV(mce->sb->tyenv->types[orig]), VSh ); |
| 287 | /* newTemp may cause mce->tmpMap to resize, hence previous results |
| 288 | from VG_(indexXA) are invalid. */ |
| 289 | ent = (TempMapEnt*)VG_(indexXA)( mce->tmpMap, (Word)orig ); |
| 290 | tl_assert(ent->kind == Orig); |
| 291 | ent->shadowV = tmpV; |
| 292 | } |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | |
| 296 | /*------------------------------------------------------------*/ |
| 297 | /*--- IRAtoms -- a subset of IRExprs ---*/ |
| 298 | /*------------------------------------------------------------*/ |
| 299 | |
| 300 | /* An atom is either an IRExpr_Const or an IRExpr_Tmp, as defined by |
sewardj | 710d6c2 | 2005-03-20 18:55:15 +0000 | [diff] [blame] | 301 | isIRAtom() in libvex_ir.h. Because this instrumenter expects flat |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 302 | input, most of this code deals in atoms. Usefully, a value atom |
| 303 | always has a V-value which is also an atom: constants are shadowed |
| 304 | by constants, and temps are shadowed by the corresponding shadow |
| 305 | temporary. */ |
| 306 | |
| 307 | typedef IRExpr IRAtom; |
| 308 | |
| 309 | /* (used for sanity checks only): is this an atom which looks |
| 310 | like it's from original code? */ |
| 311 | static Bool isOriginalAtom ( MCEnv* mce, IRAtom* a1 ) |
| 312 | { |
| 313 | if (a1->tag == Iex_Const) |
| 314 | return True; |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 315 | if (a1->tag == Iex_RdTmp) { |
| 316 | TempMapEnt* ent = VG_(indexXA)( mce->tmpMap, a1->Iex.RdTmp.tmp ); |
| 317 | return ent->kind == Orig; |
| 318 | } |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 319 | return False; |
| 320 | } |
| 321 | |
| 322 | /* (used for sanity checks only): is this an atom which looks |
| 323 | like it's from shadow code? */ |
| 324 | static Bool isShadowAtom ( MCEnv* mce, IRAtom* a1 ) |
| 325 | { |
| 326 | if (a1->tag == Iex_Const) |
| 327 | return True; |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 328 | if (a1->tag == Iex_RdTmp) { |
| 329 | TempMapEnt* ent = VG_(indexXA)( mce->tmpMap, a1->Iex.RdTmp.tmp ); |
| 330 | return ent->kind == VSh || ent->kind == BSh; |
| 331 | } |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 332 | return False; |
| 333 | } |
| 334 | |
| 335 | /* (used for sanity checks only): check that both args are atoms and |
| 336 | are identically-kinded. */ |
| 337 | static Bool sameKindedAtoms ( IRAtom* a1, IRAtom* a2 ) |
| 338 | { |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 339 | if (a1->tag == Iex_RdTmp && a2->tag == Iex_RdTmp) |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 340 | return True; |
sewardj | bef552a | 2005-08-30 12:54:36 +0000 | [diff] [blame] | 341 | if (a1->tag == Iex_Const && a2->tag == Iex_Const) |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 342 | return True; |
| 343 | return False; |
| 344 | } |
| 345 | |
| 346 | |
| 347 | /*------------------------------------------------------------*/ |
| 348 | /*--- Type management ---*/ |
| 349 | /*------------------------------------------------------------*/ |
| 350 | |
| 351 | /* Shadow state is always accessed using integer types. This returns |
| 352 | an integer type with the same size (as per sizeofIRType) as the |
| 353 | given type. The only valid shadow types are Bit, I8, I16, I32, |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 354 | I64, I128, V128. */ |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 355 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 356 | static IRType shadowTypeV ( IRType ty ) |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 357 | { |
| 358 | switch (ty) { |
| 359 | case Ity_I1: |
| 360 | case Ity_I8: |
| 361 | case Ity_I16: |
| 362 | case Ity_I32: |
sewardj | 6cf40ff | 2005-04-20 22:31:26 +0000 | [diff] [blame] | 363 | case Ity_I64: |
| 364 | case Ity_I128: return ty; |
sewardj | 3245c91 | 2004-12-10 14:58:26 +0000 | [diff] [blame] | 365 | case Ity_F32: return Ity_I32; |
| 366 | case Ity_F64: return Ity_I64; |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 367 | case Ity_F128: return Ity_I128; |
sewardj | 3245c91 | 2004-12-10 14:58:26 +0000 | [diff] [blame] | 368 | case Ity_V128: return Ity_V128; |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 369 | default: ppIRType(ty); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 370 | VG_(tool_panic)("memcheck:shadowTypeV"); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 371 | } |
| 372 | } |
| 373 | |
| 374 | /* Produce a 'defined' value of the given shadow type. Should only be |
| 375 | supplied shadow types (Bit/I8/I16/I32/UI64). */ |
| 376 | static IRExpr* definedOfType ( IRType ty ) { |
| 377 | switch (ty) { |
sewardj | 170ee21 | 2004-12-10 18:57:51 +0000 | [diff] [blame] | 378 | case Ity_I1: return IRExpr_Const(IRConst_U1(False)); |
| 379 | case Ity_I8: return IRExpr_Const(IRConst_U8(0)); |
| 380 | case Ity_I16: return IRExpr_Const(IRConst_U16(0)); |
| 381 | case Ity_I32: return IRExpr_Const(IRConst_U32(0)); |
| 382 | case Ity_I64: return IRExpr_Const(IRConst_U64(0)); |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 383 | case Ity_I128: return i128_const_zero(); |
sewardj | 170ee21 | 2004-12-10 18:57:51 +0000 | [diff] [blame] | 384 | case Ity_V128: return IRExpr_Const(IRConst_V128(0x0000)); |
sewardj | f1962d3 | 2006-10-19 13:22:16 +0000 | [diff] [blame] | 385 | default: VG_(tool_panic)("memcheck:definedOfType"); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 386 | } |
| 387 | } |
| 388 | |
| 389 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 390 | /*------------------------------------------------------------*/ |
| 391 | /*--- Constructing IR fragments ---*/ |
| 392 | /*------------------------------------------------------------*/ |
| 393 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 394 | /* add stmt to a bb */ |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 395 | static inline void stmt ( HChar cat, MCEnv* mce, IRStmt* st ) { |
| 396 | if (mce->trace) { |
| 397 | VG_(printf)(" %c: ", cat); |
| 398 | ppIRStmt(st); |
| 399 | VG_(printf)("\n"); |
| 400 | } |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 401 | addStmtToIRSB(mce->sb, st); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | /* assign value to tmp */ |
| 405 | static inline |
| 406 | void assign ( HChar cat, MCEnv* mce, IRTemp tmp, IRExpr* expr ) { |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 407 | stmt(cat, mce, IRStmt_WrTmp(tmp,expr)); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 408 | } |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 409 | |
| 410 | /* build various kinds of expressions */ |
sewardj | 57f92b0 | 2010-08-22 11:54:14 +0000 | [diff] [blame] | 411 | #define triop(_op, _arg1, _arg2, _arg3) \ |
| 412 | IRExpr_Triop((_op),(_arg1),(_arg2),(_arg3)) |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 413 | #define binop(_op, _arg1, _arg2) IRExpr_Binop((_op),(_arg1),(_arg2)) |
| 414 | #define unop(_op, _arg) IRExpr_Unop((_op),(_arg)) |
| 415 | #define mkU8(_n) IRExpr_Const(IRConst_U8(_n)) |
| 416 | #define mkU16(_n) IRExpr_Const(IRConst_U16(_n)) |
| 417 | #define mkU32(_n) IRExpr_Const(IRConst_U32(_n)) |
| 418 | #define mkU64(_n) IRExpr_Const(IRConst_U64(_n)) |
sewardj | 170ee21 | 2004-12-10 18:57:51 +0000 | [diff] [blame] | 419 | #define mkV128(_n) IRExpr_Const(IRConst_V128(_n)) |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 420 | #define mkexpr(_tmp) IRExpr_RdTmp((_tmp)) |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 421 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 422 | /* Bind the given expression to a new temporary, and return the |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 423 | temporary. This effectively converts an arbitrary expression into |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 424 | an atom. |
| 425 | |
| 426 | 'ty' is the type of 'e' and hence the type that the new temporary |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 427 | needs to be. But passing it in is redundant, since we can deduce |
| 428 | the type merely by inspecting 'e'. So at least use that fact to |
| 429 | assert that the two types agree. */ |
| 430 | static IRAtom* assignNew ( HChar cat, MCEnv* mce, IRType ty, IRExpr* e ) |
| 431 | { |
| 432 | TempKind k; |
| 433 | IRTemp t; |
| 434 | IRType tyE = typeOfIRExpr(mce->sb->tyenv, e); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 435 | tl_assert(tyE == ty); /* so 'ty' is redundant (!) */ |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 436 | switch (cat) { |
| 437 | case 'V': k = VSh; break; |
| 438 | case 'B': k = BSh; break; |
| 439 | case 'C': k = Orig; break; |
| 440 | /* happens when we are making up new "orig" |
| 441 | expressions, for IRCAS handling */ |
| 442 | default: tl_assert(0); |
| 443 | } |
| 444 | t = newTemp(mce, ty, k); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 445 | assign(cat, mce, t, e); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 446 | return mkexpr(t); |
| 447 | } |
| 448 | |
| 449 | |
| 450 | /*------------------------------------------------------------*/ |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 451 | /*--- Helper functions for 128-bit ops ---*/ |
| 452 | /*------------------------------------------------------------*/ |
| 453 | static IRExpr *i128_const_zero(void) |
| 454 | { |
| 455 | return binop(Iop_64HLto128, IRExpr_Const(IRConst_U64(0)), |
| 456 | IRExpr_Const(IRConst_U64(0))); |
| 457 | } |
| 458 | |
| 459 | /* There are no 128-bit loads and/or stores. So we do not need to worry |
| 460 | about that in expr2vbits_Load */ |
| 461 | |
| 462 | /*------------------------------------------------------------*/ |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 463 | /*--- Constructing definedness primitive ops ---*/ |
| 464 | /*------------------------------------------------------------*/ |
| 465 | |
| 466 | /* --------- Defined-if-either-defined --------- */ |
| 467 | |
| 468 | static IRAtom* mkDifD8 ( MCEnv* mce, IRAtom* a1, IRAtom* a2 ) { |
| 469 | tl_assert(isShadowAtom(mce,a1)); |
| 470 | tl_assert(isShadowAtom(mce,a2)); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 471 | return assignNew('V', mce, Ity_I8, binop(Iop_And8, a1, a2)); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | static IRAtom* mkDifD16 ( MCEnv* mce, IRAtom* a1, IRAtom* a2 ) { |
| 475 | tl_assert(isShadowAtom(mce,a1)); |
| 476 | tl_assert(isShadowAtom(mce,a2)); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 477 | return assignNew('V', mce, Ity_I16, binop(Iop_And16, a1, a2)); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | static IRAtom* mkDifD32 ( MCEnv* mce, IRAtom* a1, IRAtom* a2 ) { |
| 481 | tl_assert(isShadowAtom(mce,a1)); |
| 482 | tl_assert(isShadowAtom(mce,a2)); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 483 | return assignNew('V', mce, Ity_I32, binop(Iop_And32, a1, a2)); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 484 | } |
| 485 | |
sewardj | 7010f6e | 2004-12-10 13:35:22 +0000 | [diff] [blame] | 486 | static IRAtom* mkDifD64 ( MCEnv* mce, IRAtom* a1, IRAtom* a2 ) { |
| 487 | tl_assert(isShadowAtom(mce,a1)); |
| 488 | tl_assert(isShadowAtom(mce,a2)); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 489 | return assignNew('V', mce, Ity_I64, binop(Iop_And64, a1, a2)); |
sewardj | 7010f6e | 2004-12-10 13:35:22 +0000 | [diff] [blame] | 490 | } |
| 491 | |
sewardj | 20d38f2 | 2005-02-07 23:50:18 +0000 | [diff] [blame] | 492 | static IRAtom* mkDifDV128 ( MCEnv* mce, IRAtom* a1, IRAtom* a2 ) { |
sewardj | 170ee21 | 2004-12-10 18:57:51 +0000 | [diff] [blame] | 493 | tl_assert(isShadowAtom(mce,a1)); |
| 494 | tl_assert(isShadowAtom(mce,a2)); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 495 | return assignNew('V', mce, Ity_V128, binop(Iop_AndV128, a1, a2)); |
sewardj | 170ee21 | 2004-12-10 18:57:51 +0000 | [diff] [blame] | 496 | } |
| 497 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 498 | /* --------- Undefined-if-either-undefined --------- */ |
| 499 | |
| 500 | static IRAtom* mkUifU8 ( MCEnv* mce, IRAtom* a1, IRAtom* a2 ) { |
| 501 | tl_assert(isShadowAtom(mce,a1)); |
| 502 | tl_assert(isShadowAtom(mce,a2)); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 503 | return assignNew('V', mce, Ity_I8, binop(Iop_Or8, a1, a2)); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | static IRAtom* mkUifU16 ( MCEnv* mce, IRAtom* a1, IRAtom* a2 ) { |
| 507 | tl_assert(isShadowAtom(mce,a1)); |
| 508 | tl_assert(isShadowAtom(mce,a2)); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 509 | return assignNew('V', mce, Ity_I16, binop(Iop_Or16, a1, a2)); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | static IRAtom* mkUifU32 ( MCEnv* mce, IRAtom* a1, IRAtom* a2 ) { |
| 513 | tl_assert(isShadowAtom(mce,a1)); |
| 514 | tl_assert(isShadowAtom(mce,a2)); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 515 | return assignNew('V', mce, Ity_I32, binop(Iop_Or32, a1, a2)); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | static IRAtom* mkUifU64 ( MCEnv* mce, IRAtom* a1, IRAtom* a2 ) { |
| 519 | tl_assert(isShadowAtom(mce,a1)); |
| 520 | tl_assert(isShadowAtom(mce,a2)); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 521 | return assignNew('V', mce, Ity_I64, binop(Iop_Or64, a1, a2)); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 522 | } |
| 523 | |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 524 | static IRAtom* mkUifU128 ( MCEnv* mce, IRAtom* a1, IRAtom* a2 ) { |
| 525 | IRAtom *tmp1, *tmp2, *tmp3, *tmp4, *tmp5, *tmp6; |
| 526 | tl_assert(isShadowAtom(mce,a1)); |
| 527 | tl_assert(isShadowAtom(mce,a2)); |
| 528 | tmp1 = assignNew('V', mce, Ity_I64, unop(Iop_128to64, a1)); |
| 529 | tmp2 = assignNew('V', mce, Ity_I64, unop(Iop_128HIto64, a1)); |
| 530 | tmp3 = assignNew('V', mce, Ity_I64, unop(Iop_128to64, a2)); |
| 531 | tmp4 = assignNew('V', mce, Ity_I64, unop(Iop_128HIto64, a2)); |
| 532 | tmp5 = assignNew('V', mce, Ity_I64, binop(Iop_Or64, tmp1, tmp3)); |
| 533 | tmp6 = assignNew('V', mce, Ity_I64, binop(Iop_Or64, tmp2, tmp4)); |
| 534 | |
| 535 | return assignNew('V', mce, Ity_I128, binop(Iop_64HLto128, tmp6, tmp5)); |
| 536 | } |
| 537 | |
sewardj | 20d38f2 | 2005-02-07 23:50:18 +0000 | [diff] [blame] | 538 | static IRAtom* mkUifUV128 ( MCEnv* mce, IRAtom* a1, IRAtom* a2 ) { |
sewardj | 3245c91 | 2004-12-10 14:58:26 +0000 | [diff] [blame] | 539 | tl_assert(isShadowAtom(mce,a1)); |
| 540 | tl_assert(isShadowAtom(mce,a2)); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 541 | return assignNew('V', mce, Ity_V128, binop(Iop_OrV128, a1, a2)); |
sewardj | 3245c91 | 2004-12-10 14:58:26 +0000 | [diff] [blame] | 542 | } |
| 543 | |
sewardj | e50a1b1 | 2004-12-17 01:24:54 +0000 | [diff] [blame] | 544 | static IRAtom* mkUifU ( MCEnv* mce, IRType vty, IRAtom* a1, IRAtom* a2 ) { |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 545 | switch (vty) { |
sewardj | e50a1b1 | 2004-12-17 01:24:54 +0000 | [diff] [blame] | 546 | case Ity_I8: return mkUifU8(mce, a1, a2); |
sewardj | a1d9330 | 2004-12-12 16:45:06 +0000 | [diff] [blame] | 547 | case Ity_I16: return mkUifU16(mce, a1, a2); |
| 548 | case Ity_I32: return mkUifU32(mce, a1, a2); |
| 549 | case Ity_I64: return mkUifU64(mce, a1, a2); |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 550 | case Ity_I128: return mkUifU128(mce, a1, a2); |
sewardj | 20d38f2 | 2005-02-07 23:50:18 +0000 | [diff] [blame] | 551 | case Ity_V128: return mkUifUV128(mce, a1, a2); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 552 | default: |
| 553 | VG_(printf)("\n"); ppIRType(vty); VG_(printf)("\n"); |
| 554 | VG_(tool_panic)("memcheck:mkUifU"); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 555 | } |
| 556 | } |
| 557 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 558 | /* --------- The Left-family of operations. --------- */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 559 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 560 | static IRAtom* mkLeft8 ( MCEnv* mce, IRAtom* a1 ) { |
| 561 | tl_assert(isShadowAtom(mce,a1)); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 562 | return assignNew('V', mce, Ity_I8, unop(Iop_Left8, a1)); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | static IRAtom* mkLeft16 ( MCEnv* mce, IRAtom* a1 ) { |
| 566 | tl_assert(isShadowAtom(mce,a1)); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 567 | return assignNew('V', mce, Ity_I16, unop(Iop_Left16, a1)); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | static IRAtom* mkLeft32 ( MCEnv* mce, IRAtom* a1 ) { |
| 571 | tl_assert(isShadowAtom(mce,a1)); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 572 | return assignNew('V', mce, Ity_I32, unop(Iop_Left32, a1)); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 573 | } |
| 574 | |
sewardj | 681be30 | 2005-01-15 20:43:58 +0000 | [diff] [blame] | 575 | static IRAtom* mkLeft64 ( MCEnv* mce, IRAtom* a1 ) { |
| 576 | tl_assert(isShadowAtom(mce,a1)); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 577 | return assignNew('V', mce, Ity_I64, unop(Iop_Left64, a1)); |
sewardj | 681be30 | 2005-01-15 20:43:58 +0000 | [diff] [blame] | 578 | } |
| 579 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 580 | /* --------- 'Improvement' functions for AND/OR. --------- */ |
| 581 | |
| 582 | /* ImproveAND(data, vbits) = data OR vbits. Defined (0) data 0s give |
| 583 | defined (0); all other -> undefined (1). |
| 584 | */ |
| 585 | static IRAtom* mkImproveAND8 ( MCEnv* mce, IRAtom* data, IRAtom* vbits ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 586 | { |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 587 | tl_assert(isOriginalAtom(mce, data)); |
| 588 | tl_assert(isShadowAtom(mce, vbits)); |
| 589 | tl_assert(sameKindedAtoms(data, vbits)); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 590 | return assignNew('V', mce, Ity_I8, binop(Iop_Or8, data, vbits)); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 591 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 592 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 593 | static IRAtom* mkImproveAND16 ( MCEnv* mce, IRAtom* data, IRAtom* vbits ) |
| 594 | { |
| 595 | tl_assert(isOriginalAtom(mce, data)); |
| 596 | tl_assert(isShadowAtom(mce, vbits)); |
| 597 | tl_assert(sameKindedAtoms(data, vbits)); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 598 | return assignNew('V', mce, Ity_I16, binop(Iop_Or16, data, vbits)); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 599 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 600 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 601 | static IRAtom* mkImproveAND32 ( MCEnv* mce, IRAtom* data, IRAtom* vbits ) |
| 602 | { |
| 603 | tl_assert(isOriginalAtom(mce, data)); |
| 604 | tl_assert(isShadowAtom(mce, vbits)); |
| 605 | tl_assert(sameKindedAtoms(data, vbits)); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 606 | return assignNew('V', mce, Ity_I32, binop(Iop_Or32, data, vbits)); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 607 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 608 | |
sewardj | 7010f6e | 2004-12-10 13:35:22 +0000 | [diff] [blame] | 609 | static IRAtom* mkImproveAND64 ( MCEnv* mce, IRAtom* data, IRAtom* vbits ) |
| 610 | { |
| 611 | tl_assert(isOriginalAtom(mce, data)); |
| 612 | tl_assert(isShadowAtom(mce, vbits)); |
| 613 | tl_assert(sameKindedAtoms(data, vbits)); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 614 | return assignNew('V', mce, Ity_I64, binop(Iop_Or64, data, vbits)); |
sewardj | 7010f6e | 2004-12-10 13:35:22 +0000 | [diff] [blame] | 615 | } |
| 616 | |
sewardj | 20d38f2 | 2005-02-07 23:50:18 +0000 | [diff] [blame] | 617 | static IRAtom* mkImproveANDV128 ( MCEnv* mce, IRAtom* data, IRAtom* vbits ) |
sewardj | 170ee21 | 2004-12-10 18:57:51 +0000 | [diff] [blame] | 618 | { |
| 619 | tl_assert(isOriginalAtom(mce, data)); |
| 620 | tl_assert(isShadowAtom(mce, vbits)); |
| 621 | tl_assert(sameKindedAtoms(data, vbits)); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 622 | return assignNew('V', mce, Ity_V128, binop(Iop_OrV128, data, vbits)); |
sewardj | 170ee21 | 2004-12-10 18:57:51 +0000 | [diff] [blame] | 623 | } |
| 624 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 625 | /* ImproveOR(data, vbits) = ~data OR vbits. Defined (0) data 1s give |
| 626 | defined (0); all other -> undefined (1). |
| 627 | */ |
| 628 | static IRAtom* mkImproveOR8 ( MCEnv* mce, IRAtom* data, IRAtom* vbits ) |
| 629 | { |
| 630 | tl_assert(isOriginalAtom(mce, data)); |
| 631 | tl_assert(isShadowAtom(mce, vbits)); |
| 632 | tl_assert(sameKindedAtoms(data, vbits)); |
| 633 | return assignNew( |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 634 | 'V', mce, Ity_I8, |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 635 | binop(Iop_Or8, |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 636 | assignNew('V', mce, Ity_I8, unop(Iop_Not8, data)), |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 637 | vbits) ); |
| 638 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 639 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 640 | static IRAtom* mkImproveOR16 ( MCEnv* mce, IRAtom* data, IRAtom* vbits ) |
| 641 | { |
| 642 | tl_assert(isOriginalAtom(mce, data)); |
| 643 | tl_assert(isShadowAtom(mce, vbits)); |
| 644 | tl_assert(sameKindedAtoms(data, vbits)); |
| 645 | return assignNew( |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 646 | 'V', mce, Ity_I16, |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 647 | binop(Iop_Or16, |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 648 | assignNew('V', mce, Ity_I16, unop(Iop_Not16, data)), |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 649 | vbits) ); |
| 650 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 651 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 652 | static IRAtom* mkImproveOR32 ( MCEnv* mce, IRAtom* data, IRAtom* vbits ) |
| 653 | { |
| 654 | tl_assert(isOriginalAtom(mce, data)); |
| 655 | tl_assert(isShadowAtom(mce, vbits)); |
| 656 | tl_assert(sameKindedAtoms(data, vbits)); |
| 657 | return assignNew( |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 658 | 'V', mce, Ity_I32, |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 659 | binop(Iop_Or32, |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 660 | assignNew('V', mce, Ity_I32, unop(Iop_Not32, data)), |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 661 | vbits) ); |
| 662 | } |
| 663 | |
sewardj | 7010f6e | 2004-12-10 13:35:22 +0000 | [diff] [blame] | 664 | static IRAtom* mkImproveOR64 ( MCEnv* mce, IRAtom* data, IRAtom* vbits ) |
| 665 | { |
| 666 | tl_assert(isOriginalAtom(mce, data)); |
| 667 | tl_assert(isShadowAtom(mce, vbits)); |
| 668 | tl_assert(sameKindedAtoms(data, vbits)); |
| 669 | return assignNew( |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 670 | 'V', mce, Ity_I64, |
sewardj | 7010f6e | 2004-12-10 13:35:22 +0000 | [diff] [blame] | 671 | binop(Iop_Or64, |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 672 | assignNew('V', mce, Ity_I64, unop(Iop_Not64, data)), |
sewardj | 7010f6e | 2004-12-10 13:35:22 +0000 | [diff] [blame] | 673 | vbits) ); |
| 674 | } |
| 675 | |
sewardj | 20d38f2 | 2005-02-07 23:50:18 +0000 | [diff] [blame] | 676 | static IRAtom* mkImproveORV128 ( MCEnv* mce, IRAtom* data, IRAtom* vbits ) |
sewardj | 170ee21 | 2004-12-10 18:57:51 +0000 | [diff] [blame] | 677 | { |
| 678 | tl_assert(isOriginalAtom(mce, data)); |
| 679 | tl_assert(isShadowAtom(mce, vbits)); |
| 680 | tl_assert(sameKindedAtoms(data, vbits)); |
| 681 | return assignNew( |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 682 | 'V', mce, Ity_V128, |
sewardj | 20d38f2 | 2005-02-07 23:50:18 +0000 | [diff] [blame] | 683 | binop(Iop_OrV128, |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 684 | assignNew('V', mce, Ity_V128, unop(Iop_NotV128, data)), |
sewardj | 170ee21 | 2004-12-10 18:57:51 +0000 | [diff] [blame] | 685 | vbits) ); |
| 686 | } |
| 687 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 688 | /* --------- Pessimising casts. --------- */ |
| 689 | |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 690 | /* The function returns an expression of type DST_TY. If any of the VBITS |
| 691 | is undefined (value == 1) the resulting expression has all bits set to |
| 692 | 1. Otherwise, all bits are 0. */ |
| 693 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 694 | static IRAtom* mkPCastTo( MCEnv* mce, IRType dst_ty, IRAtom* vbits ) |
| 695 | { |
sewardj | 4cc684b | 2007-08-25 23:09:36 +0000 | [diff] [blame] | 696 | IRType src_ty; |
sewardj | 7cf97ee | 2004-11-28 14:25:01 +0000 | [diff] [blame] | 697 | IRAtom* tmp1; |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 698 | /* Note, dst_ty is a shadow type, not an original type. */ |
| 699 | /* First of all, collapse vbits down to a single bit. */ |
| 700 | tl_assert(isShadowAtom(mce,vbits)); |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 701 | src_ty = typeOfIRExpr(mce->sb->tyenv, vbits); |
sewardj | 4cc684b | 2007-08-25 23:09:36 +0000 | [diff] [blame] | 702 | |
| 703 | /* Fast-track some common cases */ |
| 704 | if (src_ty == Ity_I32 && dst_ty == Ity_I32) |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 705 | return assignNew('V', mce, Ity_I32, unop(Iop_CmpwNEZ32, vbits)); |
sewardj | 4cc684b | 2007-08-25 23:09:36 +0000 | [diff] [blame] | 706 | |
| 707 | if (src_ty == Ity_I64 && dst_ty == Ity_I64) |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 708 | return assignNew('V', mce, Ity_I64, unop(Iop_CmpwNEZ64, vbits)); |
sewardj | 4cc684b | 2007-08-25 23:09:36 +0000 | [diff] [blame] | 709 | |
| 710 | if (src_ty == Ity_I32 && dst_ty == Ity_I64) { |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 711 | IRAtom* tmp = assignNew('V', mce, Ity_I32, unop(Iop_CmpwNEZ32, vbits)); |
| 712 | return assignNew('V', mce, Ity_I64, binop(Iop_32HLto64, tmp, tmp)); |
sewardj | 4cc684b | 2007-08-25 23:09:36 +0000 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | /* Else do it the slow way .. */ |
| 716 | tmp1 = NULL; |
| 717 | switch (src_ty) { |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 718 | case Ity_I1: |
| 719 | tmp1 = vbits; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 720 | break; |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 721 | case Ity_I8: |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 722 | tmp1 = assignNew('V', mce, Ity_I1, unop(Iop_CmpNEZ8, vbits)); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 723 | break; |
| 724 | case Ity_I16: |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 725 | tmp1 = assignNew('V', mce, Ity_I1, unop(Iop_CmpNEZ16, vbits)); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 726 | break; |
| 727 | case Ity_I32: |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 728 | tmp1 = assignNew('V', mce, Ity_I1, unop(Iop_CmpNEZ32, vbits)); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 729 | break; |
| 730 | case Ity_I64: |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 731 | tmp1 = assignNew('V', mce, Ity_I1, unop(Iop_CmpNEZ64, vbits)); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 732 | break; |
sewardj | 69a1332 | 2005-04-23 01:14:51 +0000 | [diff] [blame] | 733 | case Ity_I128: { |
| 734 | /* Gah. Chop it in half, OR the halves together, and compare |
| 735 | that with zero. */ |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 736 | IRAtom* tmp2 = assignNew('V', mce, Ity_I64, unop(Iop_128HIto64, vbits)); |
| 737 | IRAtom* tmp3 = assignNew('V', mce, Ity_I64, unop(Iop_128to64, vbits)); |
| 738 | IRAtom* tmp4 = assignNew('V', mce, Ity_I64, binop(Iop_Or64, tmp2, tmp3)); |
| 739 | tmp1 = assignNew('V', mce, Ity_I1, |
sewardj | 37c31cc | 2005-04-26 23:49:24 +0000 | [diff] [blame] | 740 | unop(Iop_CmpNEZ64, tmp4)); |
sewardj | 69a1332 | 2005-04-23 01:14:51 +0000 | [diff] [blame] | 741 | break; |
| 742 | } |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 743 | default: |
sewardj | 4cc684b | 2007-08-25 23:09:36 +0000 | [diff] [blame] | 744 | ppIRType(src_ty); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 745 | VG_(tool_panic)("mkPCastTo(1)"); |
| 746 | } |
| 747 | tl_assert(tmp1); |
| 748 | /* Now widen up to the dst type. */ |
| 749 | switch (dst_ty) { |
| 750 | case Ity_I1: |
| 751 | return tmp1; |
| 752 | case Ity_I8: |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 753 | return assignNew('V', mce, Ity_I8, unop(Iop_1Sto8, tmp1)); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 754 | case Ity_I16: |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 755 | return assignNew('V', mce, Ity_I16, unop(Iop_1Sto16, tmp1)); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 756 | case Ity_I32: |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 757 | return assignNew('V', mce, Ity_I32, unop(Iop_1Sto32, tmp1)); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 758 | case Ity_I64: |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 759 | return assignNew('V', mce, Ity_I64, unop(Iop_1Sto64, tmp1)); |
sewardj | a1d9330 | 2004-12-12 16:45:06 +0000 | [diff] [blame] | 760 | case Ity_V128: |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 761 | tmp1 = assignNew('V', mce, Ity_I64, unop(Iop_1Sto64, tmp1)); |
| 762 | tmp1 = assignNew('V', mce, Ity_V128, binop(Iop_64HLtoV128, tmp1, tmp1)); |
sewardj | a1d9330 | 2004-12-12 16:45:06 +0000 | [diff] [blame] | 763 | return tmp1; |
sewardj | 69a1332 | 2005-04-23 01:14:51 +0000 | [diff] [blame] | 764 | case Ity_I128: |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 765 | tmp1 = assignNew('V', mce, Ity_I64, unop(Iop_1Sto64, tmp1)); |
| 766 | tmp1 = assignNew('V', mce, Ity_I128, binop(Iop_64HLto128, tmp1, tmp1)); |
sewardj | 69a1332 | 2005-04-23 01:14:51 +0000 | [diff] [blame] | 767 | return tmp1; |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 768 | default: |
| 769 | ppIRType(dst_ty); |
| 770 | VG_(tool_panic)("mkPCastTo(2)"); |
| 771 | } |
| 772 | } |
| 773 | |
sewardj | d5204dc | 2004-12-31 01:16:11 +0000 | [diff] [blame] | 774 | /* --------- Accurate interpretation of CmpEQ/CmpNE. --------- */ |
| 775 | /* |
| 776 | Normally, we can do CmpEQ/CmpNE by doing UifU on the arguments, and |
| 777 | PCasting to Ity_U1. However, sometimes it is necessary to be more |
| 778 | accurate. The insight is that the result is defined if two |
| 779 | corresponding bits can be found, one from each argument, so that |
| 780 | both bits are defined but are different -- that makes EQ say "No" |
| 781 | and NE say "Yes". Hence, we compute an improvement term and DifD |
| 782 | it onto the "normal" (UifU) result. |
| 783 | |
| 784 | The result is: |
| 785 | |
| 786 | PCastTo<1> ( |
sewardj | e6f8af4 | 2005-07-06 18:48:59 +0000 | [diff] [blame] | 787 | -- naive version |
| 788 | PCastTo<sz>( UifU<sz>(vxx, vyy) ) |
| 789 | |
sewardj | d5204dc | 2004-12-31 01:16:11 +0000 | [diff] [blame] | 790 | `DifD<sz>` |
sewardj | e6f8af4 | 2005-07-06 18:48:59 +0000 | [diff] [blame] | 791 | |
| 792 | -- improvement term |
| 793 | PCastTo<sz>( PCast<sz>( CmpEQ<sz> ( vec, 1...1 ) ) ) |
sewardj | d5204dc | 2004-12-31 01:16:11 +0000 | [diff] [blame] | 794 | ) |
sewardj | e6f8af4 | 2005-07-06 18:48:59 +0000 | [diff] [blame] | 795 | |
sewardj | d5204dc | 2004-12-31 01:16:11 +0000 | [diff] [blame] | 796 | where |
| 797 | vec contains 0 (defined) bits where the corresponding arg bits |
sewardj | e6f8af4 | 2005-07-06 18:48:59 +0000 | [diff] [blame] | 798 | are defined but different, and 1 bits otherwise. |
sewardj | d5204dc | 2004-12-31 01:16:11 +0000 | [diff] [blame] | 799 | |
sewardj | e6f8af4 | 2005-07-06 18:48:59 +0000 | [diff] [blame] | 800 | vec = Or<sz>( vxx, // 0 iff bit defined |
| 801 | vyy, // 0 iff bit defined |
| 802 | Not<sz>(Xor<sz>( xx, yy )) // 0 iff bits different |
| 803 | ) |
| 804 | |
| 805 | If any bit of vec is 0, the result is defined and so the |
| 806 | improvement term should produce 0...0, else it should produce |
| 807 | 1...1. |
| 808 | |
| 809 | Hence require for the improvement term: |
| 810 | |
| 811 | if vec == 1...1 then 1...1 else 0...0 |
| 812 | -> |
| 813 | PCast<sz>( CmpEQ<sz> ( vec, 1...1 ) ) |
| 814 | |
| 815 | This was extensively re-analysed and checked on 6 July 05. |
sewardj | d5204dc | 2004-12-31 01:16:11 +0000 | [diff] [blame] | 816 | */ |
| 817 | static IRAtom* expensiveCmpEQorNE ( MCEnv* mce, |
| 818 | IRType ty, |
| 819 | IRAtom* vxx, IRAtom* vyy, |
| 820 | IRAtom* xx, IRAtom* yy ) |
| 821 | { |
sewardj | e6f8af4 | 2005-07-06 18:48:59 +0000 | [diff] [blame] | 822 | IRAtom *naive, *vec, *improvement_term; |
| 823 | IRAtom *improved, *final_cast, *top; |
| 824 | IROp opDIFD, opUIFU, opXOR, opNOT, opCMP, opOR; |
sewardj | d5204dc | 2004-12-31 01:16:11 +0000 | [diff] [blame] | 825 | |
| 826 | tl_assert(isShadowAtom(mce,vxx)); |
| 827 | tl_assert(isShadowAtom(mce,vyy)); |
| 828 | tl_assert(isOriginalAtom(mce,xx)); |
| 829 | tl_assert(isOriginalAtom(mce,yy)); |
| 830 | tl_assert(sameKindedAtoms(vxx,xx)); |
| 831 | tl_assert(sameKindedAtoms(vyy,yy)); |
| 832 | |
| 833 | switch (ty) { |
| 834 | case Ity_I32: |
sewardj | e6f8af4 | 2005-07-06 18:48:59 +0000 | [diff] [blame] | 835 | opOR = Iop_Or32; |
sewardj | d5204dc | 2004-12-31 01:16:11 +0000 | [diff] [blame] | 836 | opDIFD = Iop_And32; |
| 837 | opUIFU = Iop_Or32; |
| 838 | opNOT = Iop_Not32; |
| 839 | opXOR = Iop_Xor32; |
| 840 | opCMP = Iop_CmpEQ32; |
| 841 | top = mkU32(0xFFFFFFFF); |
| 842 | break; |
tom | cd98633 | 2005-04-26 07:44:48 +0000 | [diff] [blame] | 843 | case Ity_I64: |
sewardj | e6f8af4 | 2005-07-06 18:48:59 +0000 | [diff] [blame] | 844 | opOR = Iop_Or64; |
tom | cd98633 | 2005-04-26 07:44:48 +0000 | [diff] [blame] | 845 | opDIFD = Iop_And64; |
| 846 | opUIFU = Iop_Or64; |
| 847 | opNOT = Iop_Not64; |
| 848 | opXOR = Iop_Xor64; |
| 849 | opCMP = Iop_CmpEQ64; |
sewardj | 37c31cc | 2005-04-26 23:49:24 +0000 | [diff] [blame] | 850 | top = mkU64(0xFFFFFFFFFFFFFFFFULL); |
tom | cd98633 | 2005-04-26 07:44:48 +0000 | [diff] [blame] | 851 | break; |
sewardj | d5204dc | 2004-12-31 01:16:11 +0000 | [diff] [blame] | 852 | default: |
| 853 | VG_(tool_panic)("expensiveCmpEQorNE"); |
| 854 | } |
| 855 | |
| 856 | naive |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 857 | = mkPCastTo(mce,ty, |
| 858 | assignNew('V', mce, ty, binop(opUIFU, vxx, vyy))); |
sewardj | d5204dc | 2004-12-31 01:16:11 +0000 | [diff] [blame] | 859 | |
| 860 | vec |
| 861 | = assignNew( |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 862 | 'V', mce,ty, |
sewardj | e6f8af4 | 2005-07-06 18:48:59 +0000 | [diff] [blame] | 863 | binop( opOR, |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 864 | assignNew('V', mce,ty, binop(opOR, vxx, vyy)), |
sewardj | d5204dc | 2004-12-31 01:16:11 +0000 | [diff] [blame] | 865 | assignNew( |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 866 | 'V', mce,ty, |
sewardj | d5204dc | 2004-12-31 01:16:11 +0000 | [diff] [blame] | 867 | unop( opNOT, |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 868 | assignNew('V', mce,ty, binop(opXOR, xx, yy)))))); |
sewardj | d5204dc | 2004-12-31 01:16:11 +0000 | [diff] [blame] | 869 | |
sewardj | e6f8af4 | 2005-07-06 18:48:59 +0000 | [diff] [blame] | 870 | improvement_term |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 871 | = mkPCastTo( mce,ty, |
| 872 | assignNew('V', mce,Ity_I1, binop(opCMP, vec, top))); |
sewardj | d5204dc | 2004-12-31 01:16:11 +0000 | [diff] [blame] | 873 | |
| 874 | improved |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 875 | = assignNew( 'V', mce,ty, binop(opDIFD, naive, improvement_term) ); |
sewardj | d5204dc | 2004-12-31 01:16:11 +0000 | [diff] [blame] | 876 | |
| 877 | final_cast |
| 878 | = mkPCastTo( mce, Ity_I1, improved ); |
| 879 | |
| 880 | return final_cast; |
| 881 | } |
| 882 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 883 | |
sewardj | 992dff9 | 2005-10-07 11:08:55 +0000 | [diff] [blame] | 884 | /* --------- Semi-accurate interpretation of CmpORD. --------- */ |
| 885 | |
| 886 | /* CmpORD32{S,U} does PowerPC-style 3-way comparisons: |
| 887 | |
| 888 | CmpORD32S(x,y) = 1<<3 if x <s y |
| 889 | = 1<<2 if x >s y |
| 890 | = 1<<1 if x == y |
| 891 | |
| 892 | and similarly the unsigned variant. The default interpretation is: |
| 893 | |
| 894 | CmpORD32{S,U}#(x,y,x#,y#) = PCast(x# `UifU` y#) |
sewardj | 1bc8210 | 2005-12-23 00:16:24 +0000 | [diff] [blame] | 895 | & (7<<1) |
sewardj | 992dff9 | 2005-10-07 11:08:55 +0000 | [diff] [blame] | 896 | |
| 897 | The "& (7<<1)" reflects the fact that all result bits except 3,2,1 |
| 898 | are zero and therefore defined (viz, zero). |
sewardj | a9e62a9 | 2005-10-07 12:13:21 +0000 | [diff] [blame] | 899 | |
| 900 | Also deal with a special case better: |
| 901 | |
| 902 | CmpORD32S(x,0) |
| 903 | |
| 904 | Here, bit 3 (LT) of the result is a copy of the top bit of x and |
| 905 | will be defined even if the rest of x isn't. In which case we do: |
| 906 | |
| 907 | CmpORD32S#(x,x#,0,{impliedly 0}#) |
sewardj | 1bc8210 | 2005-12-23 00:16:24 +0000 | [diff] [blame] | 908 | = PCast(x#) & (3<<1) -- standard interp for GT#,EQ# |
| 909 | | (x# >>u 31) << 3 -- LT# = x#[31] |
sewardj | a9e62a9 | 2005-10-07 12:13:21 +0000 | [diff] [blame] | 910 | |
sewardj | 1bc8210 | 2005-12-23 00:16:24 +0000 | [diff] [blame] | 911 | Analogous handling for CmpORD64{S,U}. |
sewardj | 992dff9 | 2005-10-07 11:08:55 +0000 | [diff] [blame] | 912 | */ |
sewardj | a9e62a9 | 2005-10-07 12:13:21 +0000 | [diff] [blame] | 913 | static Bool isZeroU32 ( IRAtom* e ) |
| 914 | { |
| 915 | return |
| 916 | toBool( e->tag == Iex_Const |
| 917 | && e->Iex.Const.con->tag == Ico_U32 |
| 918 | && e->Iex.Const.con->Ico.U32 == 0 ); |
| 919 | } |
| 920 | |
sewardj | 1bc8210 | 2005-12-23 00:16:24 +0000 | [diff] [blame] | 921 | static Bool isZeroU64 ( IRAtom* e ) |
sewardj | 992dff9 | 2005-10-07 11:08:55 +0000 | [diff] [blame] | 922 | { |
sewardj | 1bc8210 | 2005-12-23 00:16:24 +0000 | [diff] [blame] | 923 | return |
| 924 | toBool( e->tag == Iex_Const |
| 925 | && e->Iex.Const.con->tag == Ico_U64 |
| 926 | && e->Iex.Const.con->Ico.U64 == 0 ); |
| 927 | } |
| 928 | |
| 929 | static IRAtom* doCmpORD ( MCEnv* mce, |
| 930 | IROp cmp_op, |
| 931 | IRAtom* xxhash, IRAtom* yyhash, |
| 932 | IRAtom* xx, IRAtom* yy ) |
| 933 | { |
| 934 | Bool m64 = cmp_op == Iop_CmpORD64S || cmp_op == Iop_CmpORD64U; |
| 935 | Bool syned = cmp_op == Iop_CmpORD64S || cmp_op == Iop_CmpORD32S; |
| 936 | IROp opOR = m64 ? Iop_Or64 : Iop_Or32; |
| 937 | IROp opAND = m64 ? Iop_And64 : Iop_And32; |
| 938 | IROp opSHL = m64 ? Iop_Shl64 : Iop_Shl32; |
| 939 | IROp opSHR = m64 ? Iop_Shr64 : Iop_Shr32; |
| 940 | IRType ty = m64 ? Ity_I64 : Ity_I32; |
| 941 | Int width = m64 ? 64 : 32; |
| 942 | |
| 943 | Bool (*isZero)(IRAtom*) = m64 ? isZeroU64 : isZeroU32; |
| 944 | |
| 945 | IRAtom* threeLeft1 = NULL; |
| 946 | IRAtom* sevenLeft1 = NULL; |
| 947 | |
sewardj | 992dff9 | 2005-10-07 11:08:55 +0000 | [diff] [blame] | 948 | tl_assert(isShadowAtom(mce,xxhash)); |
| 949 | tl_assert(isShadowAtom(mce,yyhash)); |
| 950 | tl_assert(isOriginalAtom(mce,xx)); |
| 951 | tl_assert(isOriginalAtom(mce,yy)); |
| 952 | tl_assert(sameKindedAtoms(xxhash,xx)); |
| 953 | tl_assert(sameKindedAtoms(yyhash,yy)); |
sewardj | 1bc8210 | 2005-12-23 00:16:24 +0000 | [diff] [blame] | 954 | tl_assert(cmp_op == Iop_CmpORD32S || cmp_op == Iop_CmpORD32U |
| 955 | || cmp_op == Iop_CmpORD64S || cmp_op == Iop_CmpORD64U); |
sewardj | 992dff9 | 2005-10-07 11:08:55 +0000 | [diff] [blame] | 956 | |
sewardj | a9e62a9 | 2005-10-07 12:13:21 +0000 | [diff] [blame] | 957 | if (0) { |
| 958 | ppIROp(cmp_op); VG_(printf)(" "); |
| 959 | ppIRExpr(xx); VG_(printf)(" "); ppIRExpr( yy ); VG_(printf)("\n"); |
| 960 | } |
| 961 | |
sewardj | 1bc8210 | 2005-12-23 00:16:24 +0000 | [diff] [blame] | 962 | if (syned && isZero(yy)) { |
sewardj | a9e62a9 | 2005-10-07 12:13:21 +0000 | [diff] [blame] | 963 | /* fancy interpretation */ |
| 964 | /* if yy is zero, then it must be fully defined (zero#). */ |
sewardj | 1bc8210 | 2005-12-23 00:16:24 +0000 | [diff] [blame] | 965 | tl_assert(isZero(yyhash)); |
| 966 | threeLeft1 = m64 ? mkU64(3<<1) : mkU32(3<<1); |
sewardj | a9e62a9 | 2005-10-07 12:13:21 +0000 | [diff] [blame] | 967 | return |
| 968 | binop( |
sewardj | 1bc8210 | 2005-12-23 00:16:24 +0000 | [diff] [blame] | 969 | opOR, |
sewardj | a9e62a9 | 2005-10-07 12:13:21 +0000 | [diff] [blame] | 970 | assignNew( |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 971 | 'V', mce,ty, |
sewardj | a9e62a9 | 2005-10-07 12:13:21 +0000 | [diff] [blame] | 972 | binop( |
sewardj | 1bc8210 | 2005-12-23 00:16:24 +0000 | [diff] [blame] | 973 | opAND, |
| 974 | mkPCastTo(mce,ty, xxhash), |
| 975 | threeLeft1 |
sewardj | a9e62a9 | 2005-10-07 12:13:21 +0000 | [diff] [blame] | 976 | )), |
| 977 | assignNew( |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 978 | 'V', mce,ty, |
sewardj | a9e62a9 | 2005-10-07 12:13:21 +0000 | [diff] [blame] | 979 | binop( |
sewardj | 1bc8210 | 2005-12-23 00:16:24 +0000 | [diff] [blame] | 980 | opSHL, |
sewardj | a9e62a9 | 2005-10-07 12:13:21 +0000 | [diff] [blame] | 981 | assignNew( |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 982 | 'V', mce,ty, |
sewardj | 1bc8210 | 2005-12-23 00:16:24 +0000 | [diff] [blame] | 983 | binop(opSHR, xxhash, mkU8(width-1))), |
sewardj | a9e62a9 | 2005-10-07 12:13:21 +0000 | [diff] [blame] | 984 | mkU8(3) |
| 985 | )) |
| 986 | ); |
| 987 | } else { |
| 988 | /* standard interpretation */ |
sewardj | 1bc8210 | 2005-12-23 00:16:24 +0000 | [diff] [blame] | 989 | sevenLeft1 = m64 ? mkU64(7<<1) : mkU32(7<<1); |
sewardj | a9e62a9 | 2005-10-07 12:13:21 +0000 | [diff] [blame] | 990 | return |
| 991 | binop( |
sewardj | 1bc8210 | 2005-12-23 00:16:24 +0000 | [diff] [blame] | 992 | opAND, |
| 993 | mkPCastTo( mce,ty, |
| 994 | mkUifU(mce,ty, xxhash,yyhash)), |
| 995 | sevenLeft1 |
sewardj | a9e62a9 | 2005-10-07 12:13:21 +0000 | [diff] [blame] | 996 | ); |
| 997 | } |
sewardj | 992dff9 | 2005-10-07 11:08:55 +0000 | [diff] [blame] | 998 | } |
| 999 | |
| 1000 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 1001 | /*------------------------------------------------------------*/ |
| 1002 | /*--- Emit a test and complaint if something is undefined. ---*/ |
| 1003 | /*------------------------------------------------------------*/ |
| 1004 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 1005 | static IRAtom* schemeE ( MCEnv* mce, IRExpr* e ); /* fwds */ |
| 1006 | |
| 1007 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 1008 | /* Set the annotations on a dirty helper to indicate that the stack |
| 1009 | pointer and instruction pointers might be read. This is the |
| 1010 | behaviour of all 'emit-a-complaint' style functions we might |
| 1011 | call. */ |
| 1012 | |
| 1013 | static void setHelperAnns ( MCEnv* mce, IRDirty* di ) { |
| 1014 | di->nFxState = 2; |
| 1015 | di->fxState[0].fx = Ifx_Read; |
| 1016 | di->fxState[0].offset = mce->layout->offset_SP; |
| 1017 | di->fxState[0].size = mce->layout->sizeof_SP; |
| 1018 | di->fxState[1].fx = Ifx_Read; |
| 1019 | di->fxState[1].offset = mce->layout->offset_IP; |
| 1020 | di->fxState[1].size = mce->layout->sizeof_IP; |
| 1021 | } |
| 1022 | |
| 1023 | |
| 1024 | /* Check the supplied **original** atom for undefinedness, and emit a |
| 1025 | complaint if so. Once that happens, mark it as defined. This is |
| 1026 | possible because the atom is either a tmp or literal. If it's a |
| 1027 | tmp, it will be shadowed by a tmp, and so we can set the shadow to |
| 1028 | be defined. In fact as mentioned above, we will have to allocate a |
| 1029 | new tmp to carry the new 'defined' shadow value, and update the |
| 1030 | original->tmp mapping accordingly; we cannot simply assign a new |
| 1031 | value to an existing shadow tmp as this breaks SSAness -- resulting |
| 1032 | in the post-instrumentation sanity checker spluttering in disapproval. |
| 1033 | */ |
| 1034 | static void complainIfUndefined ( MCEnv* mce, IRAtom* atom ) |
| 1035 | { |
sewardj | 7cf97ee | 2004-11-28 14:25:01 +0000 | [diff] [blame] | 1036 | IRAtom* vatom; |
| 1037 | IRType ty; |
| 1038 | Int sz; |
| 1039 | IRDirty* di; |
| 1040 | IRAtom* cond; |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 1041 | IRAtom* origin; |
| 1042 | void* fn; |
| 1043 | HChar* nm; |
| 1044 | IRExpr** args; |
| 1045 | Int nargs; |
sewardj | 7cf97ee | 2004-11-28 14:25:01 +0000 | [diff] [blame] | 1046 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1047 | // Don't do V bit tests if we're not reporting undefined value errors. |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 1048 | if (MC_(clo_mc_level) == 1) |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1049 | return; |
| 1050 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 1051 | /* Since the original expression is atomic, there's no duplicated |
| 1052 | work generated by making multiple V-expressions for it. So we |
| 1053 | don't really care about the possibility that someone else may |
| 1054 | also create a V-interpretion for it. */ |
| 1055 | tl_assert(isOriginalAtom(mce, atom)); |
sewardj | 7cf97ee | 2004-11-28 14:25:01 +0000 | [diff] [blame] | 1056 | vatom = expr2vbits( mce, atom ); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 1057 | tl_assert(isShadowAtom(mce, vatom)); |
| 1058 | tl_assert(sameKindedAtoms(atom, vatom)); |
| 1059 | |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 1060 | ty = typeOfIRExpr(mce->sb->tyenv, vatom); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 1061 | |
| 1062 | /* sz is only used for constructing the error message */ |
sewardj | 7cf97ee | 2004-11-28 14:25:01 +0000 | [diff] [blame] | 1063 | sz = ty==Ity_I1 ? 0 : sizeofIRType(ty); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 1064 | |
sewardj | 7cf97ee | 2004-11-28 14:25:01 +0000 | [diff] [blame] | 1065 | cond = mkPCastTo( mce, Ity_I1, vatom ); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 1066 | /* cond will be 0 if all defined, and 1 if any not defined. */ |
| 1067 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 1068 | /* Get the origin info for the value we are about to check. At |
| 1069 | least, if we are doing origin tracking. If not, use a dummy |
| 1070 | zero origin. */ |
| 1071 | if (MC_(clo_mc_level) == 3) { |
| 1072 | origin = schemeE( mce, atom ); |
| 1073 | if (mce->hWordTy == Ity_I64) { |
| 1074 | origin = assignNew( 'B', mce, Ity_I64, unop(Iop_32Uto64, origin) ); |
| 1075 | } |
| 1076 | } else { |
| 1077 | origin = NULL; |
| 1078 | } |
| 1079 | |
| 1080 | fn = NULL; |
| 1081 | nm = NULL; |
| 1082 | args = NULL; |
| 1083 | nargs = -1; |
| 1084 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 1085 | switch (sz) { |
| 1086 | case 0: |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 1087 | if (origin) { |
| 1088 | fn = &MC_(helperc_value_check0_fail_w_o); |
| 1089 | nm = "MC_(helperc_value_check0_fail_w_o)"; |
| 1090 | args = mkIRExprVec_1(origin); |
| 1091 | nargs = 1; |
| 1092 | } else { |
| 1093 | fn = &MC_(helperc_value_check0_fail_no_o); |
| 1094 | nm = "MC_(helperc_value_check0_fail_no_o)"; |
| 1095 | args = mkIRExprVec_0(); |
| 1096 | nargs = 0; |
| 1097 | } |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 1098 | break; |
| 1099 | case 1: |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 1100 | if (origin) { |
| 1101 | fn = &MC_(helperc_value_check1_fail_w_o); |
| 1102 | nm = "MC_(helperc_value_check1_fail_w_o)"; |
| 1103 | args = mkIRExprVec_1(origin); |
| 1104 | nargs = 1; |
| 1105 | } else { |
| 1106 | fn = &MC_(helperc_value_check1_fail_no_o); |
| 1107 | nm = "MC_(helperc_value_check1_fail_no_o)"; |
| 1108 | args = mkIRExprVec_0(); |
| 1109 | nargs = 0; |
| 1110 | } |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 1111 | break; |
| 1112 | case 4: |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 1113 | if (origin) { |
| 1114 | fn = &MC_(helperc_value_check4_fail_w_o); |
| 1115 | nm = "MC_(helperc_value_check4_fail_w_o)"; |
| 1116 | args = mkIRExprVec_1(origin); |
| 1117 | nargs = 1; |
| 1118 | } else { |
| 1119 | fn = &MC_(helperc_value_check4_fail_no_o); |
| 1120 | nm = "MC_(helperc_value_check4_fail_no_o)"; |
| 1121 | args = mkIRExprVec_0(); |
| 1122 | nargs = 0; |
| 1123 | } |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 1124 | break; |
sewardj | 11bcc4e | 2005-04-23 22:38:38 +0000 | [diff] [blame] | 1125 | case 8: |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 1126 | if (origin) { |
| 1127 | fn = &MC_(helperc_value_check8_fail_w_o); |
| 1128 | nm = "MC_(helperc_value_check8_fail_w_o)"; |
| 1129 | args = mkIRExprVec_1(origin); |
| 1130 | nargs = 1; |
| 1131 | } else { |
| 1132 | fn = &MC_(helperc_value_check8_fail_no_o); |
| 1133 | nm = "MC_(helperc_value_check8_fail_no_o)"; |
| 1134 | args = mkIRExprVec_0(); |
| 1135 | nargs = 0; |
| 1136 | } |
sewardj | 11bcc4e | 2005-04-23 22:38:38 +0000 | [diff] [blame] | 1137 | break; |
njn | 4c245e5 | 2009-03-15 23:25:38 +0000 | [diff] [blame] | 1138 | case 2: |
| 1139 | case 16: |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 1140 | if (origin) { |
| 1141 | fn = &MC_(helperc_value_checkN_fail_w_o); |
| 1142 | nm = "MC_(helperc_value_checkN_fail_w_o)"; |
| 1143 | args = mkIRExprVec_2( mkIRExpr_HWord( sz ), origin); |
| 1144 | nargs = 2; |
| 1145 | } else { |
| 1146 | fn = &MC_(helperc_value_checkN_fail_no_o); |
| 1147 | nm = "MC_(helperc_value_checkN_fail_no_o)"; |
| 1148 | args = mkIRExprVec_1( mkIRExpr_HWord( sz ) ); |
| 1149 | nargs = 1; |
| 1150 | } |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 1151 | break; |
njn | 4c245e5 | 2009-03-15 23:25:38 +0000 | [diff] [blame] | 1152 | default: |
| 1153 | VG_(tool_panic)("unexpected szB"); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 1154 | } |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 1155 | |
| 1156 | tl_assert(fn); |
| 1157 | tl_assert(nm); |
| 1158 | tl_assert(args); |
| 1159 | tl_assert(nargs >= 0 && nargs <= 2); |
| 1160 | tl_assert( (MC_(clo_mc_level) == 3 && origin != NULL) |
| 1161 | || (MC_(clo_mc_level) == 2 && origin == NULL) ); |
| 1162 | |
| 1163 | di = unsafeIRDirty_0_N( nargs/*regparms*/, nm, |
| 1164 | VG_(fnptr_to_fnentry)( fn ), args ); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 1165 | di->guard = cond; |
| 1166 | setHelperAnns( mce, di ); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 1167 | stmt( 'V', mce, IRStmt_Dirty(di)); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 1168 | |
| 1169 | /* Set the shadow tmp to be defined. First, update the |
| 1170 | orig->shadow tmp mapping to reflect the fact that this shadow is |
| 1171 | getting a new value. */ |
sewardj | 710d6c2 | 2005-03-20 18:55:15 +0000 | [diff] [blame] | 1172 | tl_assert(isIRAtom(vatom)); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 1173 | /* sameKindedAtoms ... */ |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 1174 | if (vatom->tag == Iex_RdTmp) { |
| 1175 | tl_assert(atom->tag == Iex_RdTmp); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 1176 | newShadowTmpV(mce, atom->Iex.RdTmp.tmp); |
| 1177 | assign('V', mce, findShadowTmpV(mce, atom->Iex.RdTmp.tmp), |
| 1178 | definedOfType(ty)); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 1179 | } |
| 1180 | } |
| 1181 | |
| 1182 | |
| 1183 | /*------------------------------------------------------------*/ |
| 1184 | /*--- Shadowing PUTs/GETs, and indexed variants thereof ---*/ |
| 1185 | /*------------------------------------------------------------*/ |
| 1186 | |
| 1187 | /* Examine the always-defined sections declared in layout to see if |
| 1188 | the (offset,size) section is within one. Note, is is an error to |
| 1189 | partially fall into such a region: (offset,size) should either be |
| 1190 | completely in such a region or completely not-in such a region. |
| 1191 | */ |
| 1192 | static Bool isAlwaysDefd ( MCEnv* mce, Int offset, Int size ) |
| 1193 | { |
| 1194 | Int minoffD, maxoffD, i; |
| 1195 | Int minoff = offset; |
| 1196 | Int maxoff = minoff + size - 1; |
| 1197 | tl_assert((minoff & ~0xFFFF) == 0); |
| 1198 | tl_assert((maxoff & ~0xFFFF) == 0); |
| 1199 | |
| 1200 | for (i = 0; i < mce->layout->n_alwaysDefd; i++) { |
| 1201 | minoffD = mce->layout->alwaysDefd[i].offset; |
| 1202 | maxoffD = minoffD + mce->layout->alwaysDefd[i].size - 1; |
| 1203 | tl_assert((minoffD & ~0xFFFF) == 0); |
| 1204 | tl_assert((maxoffD & ~0xFFFF) == 0); |
| 1205 | |
| 1206 | if (maxoff < minoffD || maxoffD < minoff) |
| 1207 | continue; /* no overlap */ |
| 1208 | if (minoff >= minoffD && maxoff <= maxoffD) |
| 1209 | return True; /* completely contained in an always-defd section */ |
| 1210 | |
| 1211 | VG_(tool_panic)("memcheck:isAlwaysDefd:partial overlap"); |
| 1212 | } |
| 1213 | return False; /* could not find any containing section */ |
| 1214 | } |
| 1215 | |
| 1216 | |
| 1217 | /* Generate into bb suitable actions to shadow this Put. If the state |
| 1218 | slice is marked 'always defined', do nothing. Otherwise, write the |
| 1219 | supplied V bits to the shadow state. We can pass in either an |
| 1220 | original atom or a V-atom, but not both. In the former case the |
| 1221 | relevant V-bits are then generated from the original. |
| 1222 | */ |
| 1223 | static |
| 1224 | void do_shadow_PUT ( MCEnv* mce, Int offset, |
| 1225 | IRAtom* atom, IRAtom* vatom ) |
| 1226 | { |
sewardj | 7cf97ee | 2004-11-28 14:25:01 +0000 | [diff] [blame] | 1227 | IRType ty; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1228 | |
| 1229 | // Don't do shadow PUTs if we're not doing undefined value checking. |
| 1230 | // Their absence lets Vex's optimiser remove all the shadow computation |
| 1231 | // that they depend on, which includes GETs of the shadow registers. |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 1232 | if (MC_(clo_mc_level) == 1) |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1233 | return; |
| 1234 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 1235 | if (atom) { |
| 1236 | tl_assert(!vatom); |
| 1237 | tl_assert(isOriginalAtom(mce, atom)); |
| 1238 | vatom = expr2vbits( mce, atom ); |
| 1239 | } else { |
| 1240 | tl_assert(vatom); |
| 1241 | tl_assert(isShadowAtom(mce, vatom)); |
| 1242 | } |
| 1243 | |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 1244 | ty = typeOfIRExpr(mce->sb->tyenv, vatom); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 1245 | tl_assert(ty != Ity_I1); |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 1246 | tl_assert(ty != Ity_I128); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 1247 | if (isAlwaysDefd(mce, offset, sizeofIRType(ty))) { |
| 1248 | /* later: no ... */ |
| 1249 | /* emit code to emit a complaint if any of the vbits are 1. */ |
| 1250 | /* complainIfUndefined(mce, atom); */ |
| 1251 | } else { |
| 1252 | /* Do a plain shadow Put. */ |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 1253 | stmt( 'V', mce, IRStmt_Put( offset + mce->layout->total_sizeB, vatom ) ); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 1254 | } |
| 1255 | } |
| 1256 | |
| 1257 | |
| 1258 | /* Return an expression which contains the V bits corresponding to the |
| 1259 | given GETI (passed in in pieces). |
| 1260 | */ |
| 1261 | static |
| 1262 | void do_shadow_PUTI ( MCEnv* mce, |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 1263 | IRRegArray* descr, |
| 1264 | IRAtom* ix, Int bias, IRAtom* atom ) |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 1265 | { |
sewardj | 7cf97ee | 2004-11-28 14:25:01 +0000 | [diff] [blame] | 1266 | IRAtom* vatom; |
| 1267 | IRType ty, tyS; |
| 1268 | Int arrSize;; |
| 1269 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1270 | // Don't do shadow PUTIs if we're not doing undefined value checking. |
| 1271 | // Their absence lets Vex's optimiser remove all the shadow computation |
| 1272 | // that they depend on, which includes GETIs of the shadow registers. |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 1273 | if (MC_(clo_mc_level) == 1) |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1274 | return; |
| 1275 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 1276 | tl_assert(isOriginalAtom(mce,atom)); |
sewardj | 7cf97ee | 2004-11-28 14:25:01 +0000 | [diff] [blame] | 1277 | vatom = expr2vbits( mce, atom ); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 1278 | tl_assert(sameKindedAtoms(atom, vatom)); |
sewardj | 7cf97ee | 2004-11-28 14:25:01 +0000 | [diff] [blame] | 1279 | ty = descr->elemTy; |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 1280 | tyS = shadowTypeV(ty); |
sewardj | 7cf97ee | 2004-11-28 14:25:01 +0000 | [diff] [blame] | 1281 | arrSize = descr->nElems * sizeofIRType(ty); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 1282 | tl_assert(ty != Ity_I1); |
| 1283 | tl_assert(isOriginalAtom(mce,ix)); |
| 1284 | complainIfUndefined(mce,ix); |
| 1285 | if (isAlwaysDefd(mce, descr->base, arrSize)) { |
| 1286 | /* later: no ... */ |
| 1287 | /* emit code to emit a complaint if any of the vbits are 1. */ |
| 1288 | /* complainIfUndefined(mce, atom); */ |
| 1289 | } else { |
| 1290 | /* Do a cloned version of the Put that refers to the shadow |
| 1291 | area. */ |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 1292 | IRRegArray* new_descr |
| 1293 | = mkIRRegArray( descr->base + mce->layout->total_sizeB, |
| 1294 | tyS, descr->nElems); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 1295 | stmt( 'V', mce, IRStmt_PutI( new_descr, ix, bias, vatom )); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 1296 | } |
| 1297 | } |
| 1298 | |
| 1299 | |
| 1300 | /* Return an expression which contains the V bits corresponding to the |
| 1301 | given GET (passed in in pieces). |
| 1302 | */ |
| 1303 | static |
| 1304 | IRExpr* shadow_GET ( MCEnv* mce, Int offset, IRType ty ) |
| 1305 | { |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 1306 | IRType tyS = shadowTypeV(ty); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 1307 | tl_assert(ty != Ity_I1); |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 1308 | tl_assert(ty != Ity_I128); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 1309 | if (isAlwaysDefd(mce, offset, sizeofIRType(ty))) { |
| 1310 | /* Always defined, return all zeroes of the relevant type */ |
| 1311 | return definedOfType(tyS); |
| 1312 | } else { |
| 1313 | /* return a cloned version of the Get that refers to the shadow |
| 1314 | area. */ |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 1315 | /* FIXME: this isn't an atom! */ |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 1316 | return IRExpr_Get( offset + mce->layout->total_sizeB, tyS ); |
| 1317 | } |
| 1318 | } |
| 1319 | |
| 1320 | |
| 1321 | /* Return an expression which contains the V bits corresponding to the |
| 1322 | given GETI (passed in in pieces). |
| 1323 | */ |
| 1324 | static |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 1325 | IRExpr* shadow_GETI ( MCEnv* mce, |
| 1326 | IRRegArray* descr, IRAtom* ix, Int bias ) |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 1327 | { |
| 1328 | IRType ty = descr->elemTy; |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 1329 | IRType tyS = shadowTypeV(ty); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 1330 | Int arrSize = descr->nElems * sizeofIRType(ty); |
| 1331 | tl_assert(ty != Ity_I1); |
| 1332 | tl_assert(isOriginalAtom(mce,ix)); |
| 1333 | complainIfUndefined(mce,ix); |
| 1334 | if (isAlwaysDefd(mce, descr->base, arrSize)) { |
| 1335 | /* Always defined, return all zeroes of the relevant type */ |
| 1336 | return definedOfType(tyS); |
| 1337 | } else { |
| 1338 | /* return a cloned version of the Get that refers to the shadow |
| 1339 | area. */ |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 1340 | IRRegArray* new_descr |
| 1341 | = mkIRRegArray( descr->base + mce->layout->total_sizeB, |
| 1342 | tyS, descr->nElems); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 1343 | return IRExpr_GetI( new_descr, ix, bias ); |
| 1344 | } |
| 1345 | } |
| 1346 | |
| 1347 | |
| 1348 | /*------------------------------------------------------------*/ |
| 1349 | /*--- Generating approximations for unknown operations, ---*/ |
| 1350 | /*--- using lazy-propagate semantics ---*/ |
| 1351 | /*------------------------------------------------------------*/ |
| 1352 | |
| 1353 | /* Lazy propagation of undefinedness from two values, resulting in the |
| 1354 | specified shadow type. |
| 1355 | */ |
| 1356 | static |
| 1357 | IRAtom* mkLazy2 ( MCEnv* mce, IRType finalVty, IRAtom* va1, IRAtom* va2 ) |
| 1358 | { |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 1359 | IRAtom* at; |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 1360 | IRType t1 = typeOfIRExpr(mce->sb->tyenv, va1); |
| 1361 | IRType t2 = typeOfIRExpr(mce->sb->tyenv, va2); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 1362 | tl_assert(isShadowAtom(mce,va1)); |
| 1363 | tl_assert(isShadowAtom(mce,va2)); |
sewardj | 37c31cc | 2005-04-26 23:49:24 +0000 | [diff] [blame] | 1364 | |
| 1365 | /* The general case is inefficient because PCast is an expensive |
| 1366 | operation. Here are some special cases which use PCast only |
| 1367 | once rather than twice. */ |
| 1368 | |
| 1369 | /* I64 x I64 -> I64 */ |
| 1370 | if (t1 == Ity_I64 && t2 == Ity_I64 && finalVty == Ity_I64) { |
| 1371 | if (0) VG_(printf)("mkLazy2: I64 x I64 -> I64\n"); |
| 1372 | at = mkUifU(mce, Ity_I64, va1, va2); |
| 1373 | at = mkPCastTo(mce, Ity_I64, at); |
| 1374 | return at; |
| 1375 | } |
| 1376 | |
| 1377 | /* I64 x I64 -> I32 */ |
| 1378 | if (t1 == Ity_I64 && t2 == Ity_I64 && finalVty == Ity_I32) { |
| 1379 | if (0) VG_(printf)("mkLazy2: I64 x I64 -> I32\n"); |
| 1380 | at = mkUifU(mce, Ity_I64, va1, va2); |
| 1381 | at = mkPCastTo(mce, Ity_I32, at); |
| 1382 | return at; |
| 1383 | } |
| 1384 | |
| 1385 | if (0) { |
| 1386 | VG_(printf)("mkLazy2 "); |
| 1387 | ppIRType(t1); |
| 1388 | VG_(printf)("_"); |
| 1389 | ppIRType(t2); |
| 1390 | VG_(printf)("_"); |
| 1391 | ppIRType(finalVty); |
| 1392 | VG_(printf)("\n"); |
| 1393 | } |
| 1394 | |
| 1395 | /* General case: force everything via 32-bit intermediaries. */ |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 1396 | at = mkPCastTo(mce, Ity_I32, va1); |
| 1397 | at = mkUifU(mce, Ity_I32, at, mkPCastTo(mce, Ity_I32, va2)); |
| 1398 | at = mkPCastTo(mce, finalVty, at); |
| 1399 | return at; |
| 1400 | } |
| 1401 | |
| 1402 | |
sewardj | ed69fdb | 2006-02-03 16:12:27 +0000 | [diff] [blame] | 1403 | /* 3-arg version of the above. */ |
| 1404 | static |
| 1405 | IRAtom* mkLazy3 ( MCEnv* mce, IRType finalVty, |
| 1406 | IRAtom* va1, IRAtom* va2, IRAtom* va3 ) |
| 1407 | { |
| 1408 | IRAtom* at; |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 1409 | IRType t1 = typeOfIRExpr(mce->sb->tyenv, va1); |
| 1410 | IRType t2 = typeOfIRExpr(mce->sb->tyenv, va2); |
| 1411 | IRType t3 = typeOfIRExpr(mce->sb->tyenv, va3); |
sewardj | ed69fdb | 2006-02-03 16:12:27 +0000 | [diff] [blame] | 1412 | tl_assert(isShadowAtom(mce,va1)); |
| 1413 | tl_assert(isShadowAtom(mce,va2)); |
| 1414 | tl_assert(isShadowAtom(mce,va3)); |
| 1415 | |
| 1416 | /* The general case is inefficient because PCast is an expensive |
| 1417 | operation. Here are some special cases which use PCast only |
| 1418 | twice rather than three times. */ |
| 1419 | |
| 1420 | /* I32 x I64 x I64 -> I64 */ |
| 1421 | /* Standard FP idiom: rm x FParg1 x FParg2 -> FPresult */ |
| 1422 | if (t1 == Ity_I32 && t2 == Ity_I64 && t3 == Ity_I64 |
| 1423 | && finalVty == Ity_I64) { |
| 1424 | if (0) VG_(printf)("mkLazy3: I32 x I64 x I64 -> I64\n"); |
| 1425 | /* Widen 1st arg to I64. Since 1st arg is typically a rounding |
| 1426 | mode indication which is fully defined, this should get |
| 1427 | folded out later. */ |
| 1428 | at = mkPCastTo(mce, Ity_I64, va1); |
| 1429 | /* Now fold in 2nd and 3rd args. */ |
| 1430 | at = mkUifU(mce, Ity_I64, at, va2); |
| 1431 | at = mkUifU(mce, Ity_I64, at, va3); |
| 1432 | /* and PCast once again. */ |
| 1433 | at = mkPCastTo(mce, Ity_I64, at); |
| 1434 | return at; |
| 1435 | } |
| 1436 | |
sewardj | 453e8f8 | 2006-02-09 03:25:06 +0000 | [diff] [blame] | 1437 | /* I32 x I64 x I64 -> I32 */ |
| 1438 | if (t1 == Ity_I32 && t2 == Ity_I64 && t3 == Ity_I64 |
| 1439 | && finalVty == Ity_I32) { |
sewardj | 59570ff | 2010-01-01 11:59:33 +0000 | [diff] [blame] | 1440 | if (0) VG_(printf)("mkLazy3: I32 x I64 x I64 -> I32\n"); |
sewardj | 453e8f8 | 2006-02-09 03:25:06 +0000 | [diff] [blame] | 1441 | at = mkPCastTo(mce, Ity_I64, va1); |
| 1442 | at = mkUifU(mce, Ity_I64, at, va2); |
| 1443 | at = mkUifU(mce, Ity_I64, at, va3); |
| 1444 | at = mkPCastTo(mce, Ity_I32, at); |
| 1445 | return at; |
| 1446 | } |
| 1447 | |
sewardj | 59570ff | 2010-01-01 11:59:33 +0000 | [diff] [blame] | 1448 | /* I32 x I32 x I32 -> I32 */ |
| 1449 | /* 32-bit FP idiom, as (eg) happens on ARM */ |
| 1450 | if (t1 == Ity_I32 && t2 == Ity_I32 && t3 == Ity_I32 |
| 1451 | && finalVty == Ity_I32) { |
| 1452 | if (0) VG_(printf)("mkLazy3: I32 x I32 x I32 -> I32\n"); |
| 1453 | at = va1; |
| 1454 | at = mkUifU(mce, Ity_I32, at, va2); |
| 1455 | at = mkUifU(mce, Ity_I32, at, va3); |
| 1456 | at = mkPCastTo(mce, Ity_I32, at); |
| 1457 | return at; |
| 1458 | } |
| 1459 | |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 1460 | /* I32 x I128 x I128 -> I128 */ |
| 1461 | /* Standard FP idiom: rm x FParg1 x FParg2 -> FPresult */ |
| 1462 | if (t1 == Ity_I32 && t2 == Ity_I128 && t3 == Ity_I128 |
| 1463 | && finalVty == Ity_I128) { |
| 1464 | if (0) VG_(printf)("mkLazy3: I32 x I128 x I128 -> I128\n"); |
| 1465 | /* Widen 1st arg to I128. Since 1st arg is typically a rounding |
| 1466 | mode indication which is fully defined, this should get |
| 1467 | folded out later. */ |
| 1468 | at = mkPCastTo(mce, Ity_I128, va1); |
| 1469 | /* Now fold in 2nd and 3rd args. */ |
| 1470 | at = mkUifU(mce, Ity_I128, at, va2); |
| 1471 | at = mkUifU(mce, Ity_I128, at, va3); |
| 1472 | /* and PCast once again. */ |
| 1473 | at = mkPCastTo(mce, Ity_I128, at); |
| 1474 | return at; |
| 1475 | } |
sewardj | 453e8f8 | 2006-02-09 03:25:06 +0000 | [diff] [blame] | 1476 | if (1) { |
| 1477 | VG_(printf)("mkLazy3: "); |
sewardj | ed69fdb | 2006-02-03 16:12:27 +0000 | [diff] [blame] | 1478 | ppIRType(t1); |
sewardj | 453e8f8 | 2006-02-09 03:25:06 +0000 | [diff] [blame] | 1479 | VG_(printf)(" x "); |
sewardj | ed69fdb | 2006-02-03 16:12:27 +0000 | [diff] [blame] | 1480 | ppIRType(t2); |
sewardj | 453e8f8 | 2006-02-09 03:25:06 +0000 | [diff] [blame] | 1481 | VG_(printf)(" x "); |
sewardj | ed69fdb | 2006-02-03 16:12:27 +0000 | [diff] [blame] | 1482 | ppIRType(t3); |
sewardj | 453e8f8 | 2006-02-09 03:25:06 +0000 | [diff] [blame] | 1483 | VG_(printf)(" -> "); |
sewardj | ed69fdb | 2006-02-03 16:12:27 +0000 | [diff] [blame] | 1484 | ppIRType(finalVty); |
| 1485 | VG_(printf)("\n"); |
| 1486 | } |
| 1487 | |
sewardj | 453e8f8 | 2006-02-09 03:25:06 +0000 | [diff] [blame] | 1488 | tl_assert(0); |
sewardj | ed69fdb | 2006-02-03 16:12:27 +0000 | [diff] [blame] | 1489 | /* General case: force everything via 32-bit intermediaries. */ |
sewardj | 453e8f8 | 2006-02-09 03:25:06 +0000 | [diff] [blame] | 1490 | /* |
sewardj | ed69fdb | 2006-02-03 16:12:27 +0000 | [diff] [blame] | 1491 | at = mkPCastTo(mce, Ity_I32, va1); |
| 1492 | at = mkUifU(mce, Ity_I32, at, mkPCastTo(mce, Ity_I32, va2)); |
| 1493 | at = mkUifU(mce, Ity_I32, at, mkPCastTo(mce, Ity_I32, va3)); |
| 1494 | at = mkPCastTo(mce, finalVty, at); |
| 1495 | return at; |
sewardj | 453e8f8 | 2006-02-09 03:25:06 +0000 | [diff] [blame] | 1496 | */ |
sewardj | ed69fdb | 2006-02-03 16:12:27 +0000 | [diff] [blame] | 1497 | } |
| 1498 | |
| 1499 | |
sewardj | e91cea7 | 2006-02-08 19:32:02 +0000 | [diff] [blame] | 1500 | /* 4-arg version of the above. */ |
| 1501 | static |
| 1502 | IRAtom* mkLazy4 ( MCEnv* mce, IRType finalVty, |
| 1503 | IRAtom* va1, IRAtom* va2, IRAtom* va3, IRAtom* va4 ) |
| 1504 | { |
| 1505 | IRAtom* at; |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 1506 | IRType t1 = typeOfIRExpr(mce->sb->tyenv, va1); |
| 1507 | IRType t2 = typeOfIRExpr(mce->sb->tyenv, va2); |
| 1508 | IRType t3 = typeOfIRExpr(mce->sb->tyenv, va3); |
| 1509 | IRType t4 = typeOfIRExpr(mce->sb->tyenv, va4); |
sewardj | e91cea7 | 2006-02-08 19:32:02 +0000 | [diff] [blame] | 1510 | tl_assert(isShadowAtom(mce,va1)); |
| 1511 | tl_assert(isShadowAtom(mce,va2)); |
| 1512 | tl_assert(isShadowAtom(mce,va3)); |
| 1513 | tl_assert(isShadowAtom(mce,va4)); |
| 1514 | |
| 1515 | /* The general case is inefficient because PCast is an expensive |
| 1516 | operation. Here are some special cases which use PCast only |
| 1517 | twice rather than three times. */ |
| 1518 | |
| 1519 | /* I32 x I64 x I64 x I64 -> I64 */ |
| 1520 | /* Standard FP idiom: rm x FParg1 x FParg2 x FParg3 -> FPresult */ |
| 1521 | if (t1 == Ity_I32 && t2 == Ity_I64 && t3 == Ity_I64 && t4 == Ity_I64 |
| 1522 | && finalVty == Ity_I64) { |
| 1523 | if (0) VG_(printf)("mkLazy4: I32 x I64 x I64 x I64 -> I64\n"); |
| 1524 | /* Widen 1st arg to I64. Since 1st arg is typically a rounding |
| 1525 | mode indication which is fully defined, this should get |
| 1526 | folded out later. */ |
| 1527 | at = mkPCastTo(mce, Ity_I64, va1); |
| 1528 | /* Now fold in 2nd, 3rd, 4th args. */ |
| 1529 | at = mkUifU(mce, Ity_I64, at, va2); |
| 1530 | at = mkUifU(mce, Ity_I64, at, va3); |
| 1531 | at = mkUifU(mce, Ity_I64, at, va4); |
| 1532 | /* and PCast once again. */ |
| 1533 | at = mkPCastTo(mce, Ity_I64, at); |
| 1534 | return at; |
| 1535 | } |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 1536 | /* I32 x I32 x I32 x I32 -> I32 */ |
| 1537 | /* Standard FP idiom: rm x FParg1 x FParg2 x FParg3 -> FPresult */ |
| 1538 | if (t1 == Ity_I32 && t2 == Ity_I32 && t3 == Ity_I32 && t4 == Ity_I32 |
| 1539 | && finalVty == Ity_I32) { |
| 1540 | if (0) VG_(printf)("mkLazy4: I32 x I32 x I32 x I32 -> I32\n"); |
| 1541 | at = va1; |
| 1542 | /* Now fold in 2nd, 3rd, 4th args. */ |
| 1543 | at = mkUifU(mce, Ity_I32, at, va2); |
| 1544 | at = mkUifU(mce, Ity_I32, at, va3); |
| 1545 | at = mkUifU(mce, Ity_I32, at, va4); |
| 1546 | at = mkPCastTo(mce, Ity_I32, at); |
| 1547 | return at; |
| 1548 | } |
sewardj | e91cea7 | 2006-02-08 19:32:02 +0000 | [diff] [blame] | 1549 | |
| 1550 | if (1) { |
sewardj | 453e8f8 | 2006-02-09 03:25:06 +0000 | [diff] [blame] | 1551 | VG_(printf)("mkLazy4: "); |
sewardj | e91cea7 | 2006-02-08 19:32:02 +0000 | [diff] [blame] | 1552 | ppIRType(t1); |
| 1553 | VG_(printf)(" x "); |
| 1554 | ppIRType(t2); |
| 1555 | VG_(printf)(" x "); |
| 1556 | ppIRType(t3); |
| 1557 | VG_(printf)(" x "); |
| 1558 | ppIRType(t4); |
| 1559 | VG_(printf)(" -> "); |
| 1560 | ppIRType(finalVty); |
| 1561 | VG_(printf)("\n"); |
| 1562 | } |
| 1563 | |
| 1564 | tl_assert(0); |
| 1565 | } |
| 1566 | |
| 1567 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 1568 | /* Do the lazy propagation game from a null-terminated vector of |
| 1569 | atoms. This is presumably the arguments to a helper call, so the |
| 1570 | IRCallee info is also supplied in order that we can know which |
| 1571 | arguments should be ignored (via the .mcx_mask field). |
| 1572 | */ |
| 1573 | static |
| 1574 | IRAtom* mkLazyN ( MCEnv* mce, |
| 1575 | IRAtom** exprvec, IRType finalVtype, IRCallee* cee ) |
| 1576 | { |
sewardj | 4cc684b | 2007-08-25 23:09:36 +0000 | [diff] [blame] | 1577 | Int i; |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 1578 | IRAtom* here; |
sewardj | 4cc684b | 2007-08-25 23:09:36 +0000 | [diff] [blame] | 1579 | IRAtom* curr; |
| 1580 | IRType mergeTy; |
sewardj | 9943003 | 2011-05-04 09:09:31 +0000 | [diff] [blame] | 1581 | Bool mergeTy64 = True; |
sewardj | 4cc684b | 2007-08-25 23:09:36 +0000 | [diff] [blame] | 1582 | |
| 1583 | /* Decide on the type of the merge intermediary. If all relevant |
| 1584 | args are I64, then it's I64. In all other circumstances, use |
| 1585 | I32. */ |
| 1586 | for (i = 0; exprvec[i]; i++) { |
| 1587 | tl_assert(i < 32); |
| 1588 | tl_assert(isOriginalAtom(mce, exprvec[i])); |
| 1589 | if (cee->mcx_mask & (1<<i)) |
| 1590 | continue; |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 1591 | if (typeOfIRExpr(mce->sb->tyenv, exprvec[i]) != Ity_I64) |
sewardj | 4cc684b | 2007-08-25 23:09:36 +0000 | [diff] [blame] | 1592 | mergeTy64 = False; |
| 1593 | } |
| 1594 | |
| 1595 | mergeTy = mergeTy64 ? Ity_I64 : Ity_I32; |
| 1596 | curr = definedOfType(mergeTy); |
| 1597 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 1598 | for (i = 0; exprvec[i]; i++) { |
| 1599 | tl_assert(i < 32); |
| 1600 | tl_assert(isOriginalAtom(mce, exprvec[i])); |
| 1601 | /* Only take notice of this arg if the callee's mc-exclusion |
| 1602 | mask does not say it is to be excluded. */ |
| 1603 | if (cee->mcx_mask & (1<<i)) { |
| 1604 | /* the arg is to be excluded from definedness checking. Do |
| 1605 | nothing. */ |
| 1606 | if (0) VG_(printf)("excluding %s(%d)\n", cee->name, i); |
| 1607 | } else { |
| 1608 | /* calculate the arg's definedness, and pessimistically merge |
| 1609 | it in. */ |
sewardj | 4cc684b | 2007-08-25 23:09:36 +0000 | [diff] [blame] | 1610 | here = mkPCastTo( mce, mergeTy, expr2vbits(mce, exprvec[i]) ); |
| 1611 | curr = mergeTy64 |
| 1612 | ? mkUifU64(mce, here, curr) |
| 1613 | : mkUifU32(mce, here, curr); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 1614 | } |
| 1615 | } |
| 1616 | return mkPCastTo(mce, finalVtype, curr ); |
| 1617 | } |
| 1618 | |
| 1619 | |
| 1620 | /*------------------------------------------------------------*/ |
| 1621 | /*--- Generating expensive sequences for exact carry-chain ---*/ |
| 1622 | /*--- propagation in add/sub and related operations. ---*/ |
| 1623 | /*------------------------------------------------------------*/ |
| 1624 | |
| 1625 | static |
sewardj | d5204dc | 2004-12-31 01:16:11 +0000 | [diff] [blame] | 1626 | IRAtom* expensiveAddSub ( MCEnv* mce, |
| 1627 | Bool add, |
| 1628 | IRType ty, |
| 1629 | IRAtom* qaa, IRAtom* qbb, |
| 1630 | IRAtom* aa, IRAtom* bb ) |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 1631 | { |
sewardj | 7cf97ee | 2004-11-28 14:25:01 +0000 | [diff] [blame] | 1632 | IRAtom *a_min, *b_min, *a_max, *b_max; |
sewardj | d5204dc | 2004-12-31 01:16:11 +0000 | [diff] [blame] | 1633 | IROp opAND, opOR, opXOR, opNOT, opADD, opSUB; |
sewardj | 7cf97ee | 2004-11-28 14:25:01 +0000 | [diff] [blame] | 1634 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 1635 | tl_assert(isShadowAtom(mce,qaa)); |
| 1636 | tl_assert(isShadowAtom(mce,qbb)); |
| 1637 | tl_assert(isOriginalAtom(mce,aa)); |
| 1638 | tl_assert(isOriginalAtom(mce,bb)); |
| 1639 | tl_assert(sameKindedAtoms(qaa,aa)); |
| 1640 | tl_assert(sameKindedAtoms(qbb,bb)); |
| 1641 | |
sewardj | d5204dc | 2004-12-31 01:16:11 +0000 | [diff] [blame] | 1642 | switch (ty) { |
| 1643 | case Ity_I32: |
| 1644 | opAND = Iop_And32; |
| 1645 | opOR = Iop_Or32; |
| 1646 | opXOR = Iop_Xor32; |
| 1647 | opNOT = Iop_Not32; |
| 1648 | opADD = Iop_Add32; |
| 1649 | opSUB = Iop_Sub32; |
| 1650 | break; |
tom | d9774d7 | 2005-06-27 08:11:01 +0000 | [diff] [blame] | 1651 | case Ity_I64: |
| 1652 | opAND = Iop_And64; |
| 1653 | opOR = Iop_Or64; |
| 1654 | opXOR = Iop_Xor64; |
| 1655 | opNOT = Iop_Not64; |
| 1656 | opADD = Iop_Add64; |
| 1657 | opSUB = Iop_Sub64; |
| 1658 | break; |
sewardj | d5204dc | 2004-12-31 01:16:11 +0000 | [diff] [blame] | 1659 | default: |
| 1660 | VG_(tool_panic)("expensiveAddSub"); |
| 1661 | } |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 1662 | |
| 1663 | // a_min = aa & ~qaa |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 1664 | a_min = assignNew('V', mce,ty, |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 1665 | binop(opAND, aa, |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 1666 | assignNew('V', mce,ty, unop(opNOT, qaa)))); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 1667 | |
| 1668 | // b_min = bb & ~qbb |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 1669 | b_min = assignNew('V', mce,ty, |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 1670 | binop(opAND, bb, |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 1671 | assignNew('V', mce,ty, unop(opNOT, qbb)))); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 1672 | |
| 1673 | // a_max = aa | qaa |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 1674 | a_max = assignNew('V', mce,ty, binop(opOR, aa, qaa)); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 1675 | |
| 1676 | // b_max = bb | qbb |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 1677 | b_max = assignNew('V', mce,ty, binop(opOR, bb, qbb)); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 1678 | |
sewardj | d5204dc | 2004-12-31 01:16:11 +0000 | [diff] [blame] | 1679 | if (add) { |
| 1680 | // result = (qaa | qbb) | ((a_min + b_min) ^ (a_max + b_max)) |
| 1681 | return |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 1682 | assignNew('V', mce,ty, |
sewardj | d5204dc | 2004-12-31 01:16:11 +0000 | [diff] [blame] | 1683 | binop( opOR, |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 1684 | assignNew('V', mce,ty, binop(opOR, qaa, qbb)), |
| 1685 | assignNew('V', mce,ty, |
sewardj | d5204dc | 2004-12-31 01:16:11 +0000 | [diff] [blame] | 1686 | binop( opXOR, |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 1687 | assignNew('V', mce,ty, binop(opADD, a_min, b_min)), |
| 1688 | assignNew('V', mce,ty, binop(opADD, a_max, b_max)) |
sewardj | d5204dc | 2004-12-31 01:16:11 +0000 | [diff] [blame] | 1689 | ) |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 1690 | ) |
sewardj | d5204dc | 2004-12-31 01:16:11 +0000 | [diff] [blame] | 1691 | ) |
| 1692 | ); |
| 1693 | } else { |
| 1694 | // result = (qaa | qbb) | ((a_min - b_max) ^ (a_max + b_min)) |
| 1695 | return |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 1696 | assignNew('V', mce,ty, |
sewardj | d5204dc | 2004-12-31 01:16:11 +0000 | [diff] [blame] | 1697 | binop( opOR, |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 1698 | assignNew('V', mce,ty, binop(opOR, qaa, qbb)), |
| 1699 | assignNew('V', mce,ty, |
sewardj | d5204dc | 2004-12-31 01:16:11 +0000 | [diff] [blame] | 1700 | binop( opXOR, |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 1701 | assignNew('V', mce,ty, binop(opSUB, a_min, b_max)), |
| 1702 | assignNew('V', mce,ty, binop(opSUB, a_max, b_min)) |
sewardj | d5204dc | 2004-12-31 01:16:11 +0000 | [diff] [blame] | 1703 | ) |
| 1704 | ) |
| 1705 | ) |
| 1706 | ); |
| 1707 | } |
| 1708 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 1709 | } |
| 1710 | |
| 1711 | |
| 1712 | /*------------------------------------------------------------*/ |
sewardj | aaddbc2 | 2005-10-07 09:49:53 +0000 | [diff] [blame] | 1713 | /*--- Scalar shifts. ---*/ |
| 1714 | /*------------------------------------------------------------*/ |
| 1715 | |
| 1716 | /* Produce an interpretation for (aa << bb) (or >>s, >>u). The basic |
| 1717 | idea is to shift the definedness bits by the original shift amount. |
| 1718 | This introduces 0s ("defined") in new positions for left shifts and |
| 1719 | unsigned right shifts, and copies the top definedness bit for |
| 1720 | signed right shifts. So, conveniently, applying the original shift |
| 1721 | operator to the definedness bits for the left arg is exactly the |
| 1722 | right thing to do: |
| 1723 | |
| 1724 | (qaa << bb) |
| 1725 | |
| 1726 | However if the shift amount is undefined then the whole result |
| 1727 | is undefined. Hence need: |
| 1728 | |
| 1729 | (qaa << bb) `UifU` PCast(qbb) |
| 1730 | |
| 1731 | If the shift amount bb is a literal than qbb will say 'all defined' |
| 1732 | and the UifU and PCast will get folded out by post-instrumentation |
| 1733 | optimisation. |
| 1734 | */ |
| 1735 | static IRAtom* scalarShift ( MCEnv* mce, |
| 1736 | IRType ty, |
| 1737 | IROp original_op, |
| 1738 | IRAtom* qaa, IRAtom* qbb, |
| 1739 | IRAtom* aa, IRAtom* bb ) |
| 1740 | { |
| 1741 | tl_assert(isShadowAtom(mce,qaa)); |
| 1742 | tl_assert(isShadowAtom(mce,qbb)); |
| 1743 | tl_assert(isOriginalAtom(mce,aa)); |
| 1744 | tl_assert(isOriginalAtom(mce,bb)); |
| 1745 | tl_assert(sameKindedAtoms(qaa,aa)); |
| 1746 | tl_assert(sameKindedAtoms(qbb,bb)); |
| 1747 | return |
| 1748 | assignNew( |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 1749 | 'V', mce, ty, |
sewardj | aaddbc2 | 2005-10-07 09:49:53 +0000 | [diff] [blame] | 1750 | mkUifU( mce, ty, |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 1751 | assignNew('V', mce, ty, binop(original_op, qaa, bb)), |
sewardj | aaddbc2 | 2005-10-07 09:49:53 +0000 | [diff] [blame] | 1752 | mkPCastTo(mce, ty, qbb) |
| 1753 | ) |
| 1754 | ); |
| 1755 | } |
| 1756 | |
| 1757 | |
| 1758 | /*------------------------------------------------------------*/ |
| 1759 | /*--- Helpers for dealing with vector primops. ---*/ |
sewardj | 3245c91 | 2004-12-10 14:58:26 +0000 | [diff] [blame] | 1760 | /*------------------------------------------------------------*/ |
| 1761 | |
sewardj | a1d9330 | 2004-12-12 16:45:06 +0000 | [diff] [blame] | 1762 | /* Vector pessimisation -- pessimise within each lane individually. */ |
| 1763 | |
| 1764 | static IRAtom* mkPCast8x16 ( MCEnv* mce, IRAtom* at ) |
| 1765 | { |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 1766 | return assignNew('V', mce, Ity_V128, unop(Iop_CmpNEZ8x16, at)); |
sewardj | a1d9330 | 2004-12-12 16:45:06 +0000 | [diff] [blame] | 1767 | } |
| 1768 | |
| 1769 | static IRAtom* mkPCast16x8 ( MCEnv* mce, IRAtom* at ) |
| 1770 | { |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 1771 | return assignNew('V', mce, Ity_V128, unop(Iop_CmpNEZ16x8, at)); |
sewardj | a1d9330 | 2004-12-12 16:45:06 +0000 | [diff] [blame] | 1772 | } |
| 1773 | |
| 1774 | static IRAtom* mkPCast32x4 ( MCEnv* mce, IRAtom* at ) |
| 1775 | { |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 1776 | return assignNew('V', mce, Ity_V128, unop(Iop_CmpNEZ32x4, at)); |
sewardj | a1d9330 | 2004-12-12 16:45:06 +0000 | [diff] [blame] | 1777 | } |
| 1778 | |
| 1779 | static IRAtom* mkPCast64x2 ( MCEnv* mce, IRAtom* at ) |
| 1780 | { |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 1781 | return assignNew('V', mce, Ity_V128, unop(Iop_CmpNEZ64x2, at)); |
sewardj | a1d9330 | 2004-12-12 16:45:06 +0000 | [diff] [blame] | 1782 | } |
| 1783 | |
sewardj | acd2e91 | 2005-01-13 19:17:06 +0000 | [diff] [blame] | 1784 | static IRAtom* mkPCast32x2 ( MCEnv* mce, IRAtom* at ) |
| 1785 | { |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 1786 | return assignNew('V', mce, Ity_I64, unop(Iop_CmpNEZ32x2, at)); |
sewardj | acd2e91 | 2005-01-13 19:17:06 +0000 | [diff] [blame] | 1787 | } |
| 1788 | |
| 1789 | static IRAtom* mkPCast16x4 ( MCEnv* mce, IRAtom* at ) |
| 1790 | { |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 1791 | return assignNew('V', mce, Ity_I64, unop(Iop_CmpNEZ16x4, at)); |
sewardj | acd2e91 | 2005-01-13 19:17:06 +0000 | [diff] [blame] | 1792 | } |
| 1793 | |
| 1794 | static IRAtom* mkPCast8x8 ( MCEnv* mce, IRAtom* at ) |
| 1795 | { |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 1796 | return assignNew('V', mce, Ity_I64, unop(Iop_CmpNEZ8x8, at)); |
sewardj | acd2e91 | 2005-01-13 19:17:06 +0000 | [diff] [blame] | 1797 | } |
| 1798 | |
sewardj | c678b85 | 2010-09-22 00:58:51 +0000 | [diff] [blame] | 1799 | static IRAtom* mkPCast16x2 ( MCEnv* mce, IRAtom* at ) |
| 1800 | { |
| 1801 | return assignNew('V', mce, Ity_I32, unop(Iop_CmpNEZ16x2, at)); |
| 1802 | } |
| 1803 | |
| 1804 | static IRAtom* mkPCast8x4 ( MCEnv* mce, IRAtom* at ) |
| 1805 | { |
| 1806 | return assignNew('V', mce, Ity_I32, unop(Iop_CmpNEZ8x4, at)); |
| 1807 | } |
| 1808 | |
sewardj | a1d9330 | 2004-12-12 16:45:06 +0000 | [diff] [blame] | 1809 | |
sewardj | 3245c91 | 2004-12-10 14:58:26 +0000 | [diff] [blame] | 1810 | /* Here's a simple scheme capable of handling ops derived from SSE1 |
| 1811 | code and while only generating ops that can be efficiently |
| 1812 | implemented in SSE1. */ |
| 1813 | |
| 1814 | /* All-lanes versions are straightforward: |
| 1815 | |
sewardj | 20d38f2 | 2005-02-07 23:50:18 +0000 | [diff] [blame] | 1816 | binary32Fx4(x,y) ==> PCast32x4(UifUV128(x#,y#)) |
sewardj | 3245c91 | 2004-12-10 14:58:26 +0000 | [diff] [blame] | 1817 | |
| 1818 | unary32Fx4(x,y) ==> PCast32x4(x#) |
| 1819 | |
| 1820 | Lowest-lane-only versions are more complex: |
| 1821 | |
sewardj | 20d38f2 | 2005-02-07 23:50:18 +0000 | [diff] [blame] | 1822 | binary32F0x4(x,y) ==> SetV128lo32( |
sewardj | 3245c91 | 2004-12-10 14:58:26 +0000 | [diff] [blame] | 1823 | x#, |
sewardj | 20d38f2 | 2005-02-07 23:50:18 +0000 | [diff] [blame] | 1824 | PCast32(V128to32(UifUV128(x#,y#))) |
sewardj | 3245c91 | 2004-12-10 14:58:26 +0000 | [diff] [blame] | 1825 | ) |
| 1826 | |
| 1827 | This is perhaps not so obvious. In particular, it's faster to |
sewardj | 20d38f2 | 2005-02-07 23:50:18 +0000 | [diff] [blame] | 1828 | do a V128-bit UifU and then take the bottom 32 bits than the more |
sewardj | 3245c91 | 2004-12-10 14:58:26 +0000 | [diff] [blame] | 1829 | obvious scheme of taking the bottom 32 bits of each operand |
| 1830 | and doing a 32-bit UifU. Basically since UifU is fast and |
| 1831 | chopping lanes off vector values is slow. |
| 1832 | |
| 1833 | Finally: |
| 1834 | |
sewardj | 20d38f2 | 2005-02-07 23:50:18 +0000 | [diff] [blame] | 1835 | unary32F0x4(x) ==> SetV128lo32( |
sewardj | 3245c91 | 2004-12-10 14:58:26 +0000 | [diff] [blame] | 1836 | x#, |
sewardj | 20d38f2 | 2005-02-07 23:50:18 +0000 | [diff] [blame] | 1837 | PCast32(V128to32(x#)) |
sewardj | 3245c91 | 2004-12-10 14:58:26 +0000 | [diff] [blame] | 1838 | ) |
| 1839 | |
| 1840 | Where: |
| 1841 | |
| 1842 | PCast32(v#) = 1Sto32(CmpNE32(v#,0)) |
| 1843 | PCast32x4(v#) = CmpNEZ32x4(v#) |
| 1844 | */ |
| 1845 | |
| 1846 | static |
| 1847 | IRAtom* binary32Fx4 ( MCEnv* mce, IRAtom* vatomX, IRAtom* vatomY ) |
| 1848 | { |
| 1849 | IRAtom* at; |
| 1850 | tl_assert(isShadowAtom(mce, vatomX)); |
| 1851 | tl_assert(isShadowAtom(mce, vatomY)); |
sewardj | 20d38f2 | 2005-02-07 23:50:18 +0000 | [diff] [blame] | 1852 | at = mkUifUV128(mce, vatomX, vatomY); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 1853 | at = assignNew('V', mce, Ity_V128, mkPCast32x4(mce, at)); |
sewardj | 3245c91 | 2004-12-10 14:58:26 +0000 | [diff] [blame] | 1854 | return at; |
| 1855 | } |
| 1856 | |
| 1857 | static |
| 1858 | IRAtom* unary32Fx4 ( MCEnv* mce, IRAtom* vatomX ) |
| 1859 | { |
| 1860 | IRAtom* at; |
| 1861 | tl_assert(isShadowAtom(mce, vatomX)); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 1862 | at = assignNew('V', mce, Ity_V128, mkPCast32x4(mce, vatomX)); |
sewardj | 3245c91 | 2004-12-10 14:58:26 +0000 | [diff] [blame] | 1863 | return at; |
| 1864 | } |
| 1865 | |
| 1866 | static |
| 1867 | IRAtom* binary32F0x4 ( MCEnv* mce, IRAtom* vatomX, IRAtom* vatomY ) |
| 1868 | { |
| 1869 | IRAtom* at; |
| 1870 | tl_assert(isShadowAtom(mce, vatomX)); |
| 1871 | tl_assert(isShadowAtom(mce, vatomY)); |
sewardj | 20d38f2 | 2005-02-07 23:50:18 +0000 | [diff] [blame] | 1872 | at = mkUifUV128(mce, vatomX, vatomY); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 1873 | at = assignNew('V', mce, Ity_I32, unop(Iop_V128to32, at)); |
sewardj | 3245c91 | 2004-12-10 14:58:26 +0000 | [diff] [blame] | 1874 | at = mkPCastTo(mce, Ity_I32, at); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 1875 | at = assignNew('V', mce, Ity_V128, binop(Iop_SetV128lo32, vatomX, at)); |
sewardj | 3245c91 | 2004-12-10 14:58:26 +0000 | [diff] [blame] | 1876 | return at; |
| 1877 | } |
| 1878 | |
| 1879 | static |
| 1880 | IRAtom* unary32F0x4 ( MCEnv* mce, IRAtom* vatomX ) |
| 1881 | { |
| 1882 | IRAtom* at; |
| 1883 | tl_assert(isShadowAtom(mce, vatomX)); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 1884 | at = assignNew('V', mce, Ity_I32, unop(Iop_V128to32, vatomX)); |
sewardj | 3245c91 | 2004-12-10 14:58:26 +0000 | [diff] [blame] | 1885 | at = mkPCastTo(mce, Ity_I32, at); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 1886 | at = assignNew('V', mce, Ity_V128, binop(Iop_SetV128lo32, vatomX, at)); |
sewardj | 3245c91 | 2004-12-10 14:58:26 +0000 | [diff] [blame] | 1887 | return at; |
| 1888 | } |
| 1889 | |
sewardj | 0b07059 | 2004-12-10 21:44:22 +0000 | [diff] [blame] | 1890 | /* --- ... and ... 64Fx2 versions of the same ... --- */ |
| 1891 | |
| 1892 | static |
| 1893 | IRAtom* binary64Fx2 ( MCEnv* mce, IRAtom* vatomX, IRAtom* vatomY ) |
| 1894 | { |
| 1895 | IRAtom* at; |
| 1896 | tl_assert(isShadowAtom(mce, vatomX)); |
| 1897 | tl_assert(isShadowAtom(mce, vatomY)); |
sewardj | 20d38f2 | 2005-02-07 23:50:18 +0000 | [diff] [blame] | 1898 | at = mkUifUV128(mce, vatomX, vatomY); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 1899 | at = assignNew('V', mce, Ity_V128, mkPCast64x2(mce, at)); |
sewardj | 0b07059 | 2004-12-10 21:44:22 +0000 | [diff] [blame] | 1900 | return at; |
| 1901 | } |
| 1902 | |
| 1903 | static |
| 1904 | IRAtom* unary64Fx2 ( MCEnv* mce, IRAtom* vatomX ) |
| 1905 | { |
| 1906 | IRAtom* at; |
| 1907 | tl_assert(isShadowAtom(mce, vatomX)); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 1908 | at = assignNew('V', mce, Ity_V128, mkPCast64x2(mce, vatomX)); |
sewardj | 0b07059 | 2004-12-10 21:44:22 +0000 | [diff] [blame] | 1909 | return at; |
| 1910 | } |
| 1911 | |
| 1912 | static |
| 1913 | IRAtom* binary64F0x2 ( MCEnv* mce, IRAtom* vatomX, IRAtom* vatomY ) |
| 1914 | { |
| 1915 | IRAtom* at; |
| 1916 | tl_assert(isShadowAtom(mce, vatomX)); |
| 1917 | tl_assert(isShadowAtom(mce, vatomY)); |
sewardj | 20d38f2 | 2005-02-07 23:50:18 +0000 | [diff] [blame] | 1918 | at = mkUifUV128(mce, vatomX, vatomY); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 1919 | at = assignNew('V', mce, Ity_I64, unop(Iop_V128to64, at)); |
sewardj | 0b07059 | 2004-12-10 21:44:22 +0000 | [diff] [blame] | 1920 | at = mkPCastTo(mce, Ity_I64, at); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 1921 | at = assignNew('V', mce, Ity_V128, binop(Iop_SetV128lo64, vatomX, at)); |
sewardj | 0b07059 | 2004-12-10 21:44:22 +0000 | [diff] [blame] | 1922 | return at; |
| 1923 | } |
| 1924 | |
| 1925 | static |
| 1926 | IRAtom* unary64F0x2 ( MCEnv* mce, IRAtom* vatomX ) |
| 1927 | { |
| 1928 | IRAtom* at; |
| 1929 | tl_assert(isShadowAtom(mce, vatomX)); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 1930 | at = assignNew('V', mce, Ity_I64, unop(Iop_V128to64, vatomX)); |
sewardj | 0b07059 | 2004-12-10 21:44:22 +0000 | [diff] [blame] | 1931 | at = mkPCastTo(mce, Ity_I64, at); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 1932 | at = assignNew('V', mce, Ity_V128, binop(Iop_SetV128lo64, vatomX, at)); |
sewardj | 0b07059 | 2004-12-10 21:44:22 +0000 | [diff] [blame] | 1933 | return at; |
| 1934 | } |
| 1935 | |
sewardj | 57f92b0 | 2010-08-22 11:54:14 +0000 | [diff] [blame] | 1936 | /* --- --- ... and ... 32Fx2 versions of the same --- --- */ |
| 1937 | |
| 1938 | static |
| 1939 | IRAtom* binary32Fx2 ( MCEnv* mce, IRAtom* vatomX, IRAtom* vatomY ) |
| 1940 | { |
| 1941 | IRAtom* at; |
| 1942 | tl_assert(isShadowAtom(mce, vatomX)); |
| 1943 | tl_assert(isShadowAtom(mce, vatomY)); |
| 1944 | at = mkUifU64(mce, vatomX, vatomY); |
| 1945 | at = assignNew('V', mce, Ity_I64, mkPCast32x2(mce, at)); |
| 1946 | return at; |
| 1947 | } |
| 1948 | |
| 1949 | static |
| 1950 | IRAtom* unary32Fx2 ( MCEnv* mce, IRAtom* vatomX ) |
| 1951 | { |
| 1952 | IRAtom* at; |
| 1953 | tl_assert(isShadowAtom(mce, vatomX)); |
| 1954 | at = assignNew('V', mce, Ity_I64, mkPCast32x2(mce, vatomX)); |
| 1955 | return at; |
| 1956 | } |
| 1957 | |
sewardj | a1d9330 | 2004-12-12 16:45:06 +0000 | [diff] [blame] | 1958 | /* --- --- Vector saturated narrowing --- --- */ |
| 1959 | |
| 1960 | /* This is quite subtle. What to do is simple: |
| 1961 | |
| 1962 | Let the original narrowing op be QNarrowW{S,U}xN. Produce: |
| 1963 | |
| 1964 | the-narrowing-op( PCastWxN(vatom1), PCastWxN(vatom2)) |
| 1965 | |
| 1966 | Why this is right is not so simple. Consider a lane in the args, |
| 1967 | vatom1 or 2, doesn't matter. |
| 1968 | |
| 1969 | After the PCast, that lane is all 0s (defined) or all |
| 1970 | 1s(undefined). |
| 1971 | |
| 1972 | Both signed and unsigned saturating narrowing of all 0s produces |
| 1973 | all 0s, which is what we want. |
| 1974 | |
| 1975 | The all-1s case is more complex. Unsigned narrowing interprets an |
| 1976 | all-1s input as the largest unsigned integer, and so produces all |
| 1977 | 1s as a result since that is the largest unsigned value at the |
| 1978 | smaller width. |
| 1979 | |
| 1980 | Signed narrowing interprets all 1s as -1. Fortunately, -1 narrows |
| 1981 | to -1, so we still wind up with all 1s at the smaller width. |
| 1982 | |
| 1983 | So: In short, pessimise the args, then apply the original narrowing |
| 1984 | op. |
sewardj | 9beeb0a | 2011-06-15 15:11:07 +0000 | [diff] [blame] | 1985 | |
| 1986 | FIXME JRS 2011-Jun-15: figure out if this is still correct |
| 1987 | following today's rationalisation/cleanup of vector narrowing |
| 1988 | primops. |
sewardj | a1d9330 | 2004-12-12 16:45:06 +0000 | [diff] [blame] | 1989 | */ |
| 1990 | static |
sewardj | 7ee7d85 | 2011-06-16 11:37:21 +0000 | [diff] [blame^] | 1991 | IRAtom* vectorNarrowBinV128 ( MCEnv* mce, IROp narrow_op, |
| 1992 | IRAtom* vatom1, IRAtom* vatom2) |
sewardj | a1d9330 | 2004-12-12 16:45:06 +0000 | [diff] [blame] | 1993 | { |
| 1994 | IRAtom *at1, *at2, *at3; |
| 1995 | IRAtom* (*pcast)( MCEnv*, IRAtom* ); |
| 1996 | switch (narrow_op) { |
sewardj | 7ee7d85 | 2011-06-16 11:37:21 +0000 | [diff] [blame^] | 1997 | case Iop_QNarrowBin32Sto16Sx8: pcast = mkPCast32x4; break; |
| 1998 | case Iop_QNarrowBin32Uto16Ux8: pcast = mkPCast32x4; break; |
| 1999 | case Iop_QNarrowBin32Sto16Ux8: pcast = mkPCast32x4; break; |
| 2000 | case Iop_QNarrowBin16Sto8Sx16: pcast = mkPCast16x8; break; |
| 2001 | case Iop_QNarrowBin16Uto8Ux16: pcast = mkPCast16x8; break; |
| 2002 | case Iop_QNarrowBin16Sto8Ux16: pcast = mkPCast16x8; break; |
| 2003 | default: VG_(tool_panic)("vectorNarrowBinV128"); |
sewardj | a1d9330 | 2004-12-12 16:45:06 +0000 | [diff] [blame] | 2004 | } |
| 2005 | tl_assert(isShadowAtom(mce,vatom1)); |
| 2006 | tl_assert(isShadowAtom(mce,vatom2)); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2007 | at1 = assignNew('V', mce, Ity_V128, pcast(mce, vatom1)); |
| 2008 | at2 = assignNew('V', mce, Ity_V128, pcast(mce, vatom2)); |
| 2009 | at3 = assignNew('V', mce, Ity_V128, binop(narrow_op, at1, at2)); |
sewardj | a1d9330 | 2004-12-12 16:45:06 +0000 | [diff] [blame] | 2010 | return at3; |
| 2011 | } |
| 2012 | |
sewardj | acd2e91 | 2005-01-13 19:17:06 +0000 | [diff] [blame] | 2013 | static |
sewardj | 7ee7d85 | 2011-06-16 11:37:21 +0000 | [diff] [blame^] | 2014 | IRAtom* vectorNarrowBin64 ( MCEnv* mce, IROp narrow_op, |
| 2015 | IRAtom* vatom1, IRAtom* vatom2) |
sewardj | acd2e91 | 2005-01-13 19:17:06 +0000 | [diff] [blame] | 2016 | { |
| 2017 | IRAtom *at1, *at2, *at3; |
| 2018 | IRAtom* (*pcast)( MCEnv*, IRAtom* ); |
| 2019 | switch (narrow_op) { |
sewardj | 7ee7d85 | 2011-06-16 11:37:21 +0000 | [diff] [blame^] | 2020 | case Iop_QNarrowBin32Sto16Sx4: pcast = mkPCast32x2; break; |
| 2021 | case Iop_QNarrowBin16Sto8Sx8: pcast = mkPCast16x4; break; |
| 2022 | case Iop_QNarrowBin16Sto8Ux8: pcast = mkPCast16x4; break; |
| 2023 | default: VG_(tool_panic)("vectorNarrowBin64"); |
sewardj | acd2e91 | 2005-01-13 19:17:06 +0000 | [diff] [blame] | 2024 | } |
| 2025 | tl_assert(isShadowAtom(mce,vatom1)); |
| 2026 | tl_assert(isShadowAtom(mce,vatom2)); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2027 | at1 = assignNew('V', mce, Ity_I64, pcast(mce, vatom1)); |
| 2028 | at2 = assignNew('V', mce, Ity_I64, pcast(mce, vatom2)); |
| 2029 | at3 = assignNew('V', mce, Ity_I64, binop(narrow_op, at1, at2)); |
sewardj | acd2e91 | 2005-01-13 19:17:06 +0000 | [diff] [blame] | 2030 | return at3; |
| 2031 | } |
| 2032 | |
sewardj | 57f92b0 | 2010-08-22 11:54:14 +0000 | [diff] [blame] | 2033 | static |
sewardj | 7ee7d85 | 2011-06-16 11:37:21 +0000 | [diff] [blame^] | 2034 | IRAtom* vectorNarrowUnV128 ( MCEnv* mce, IROp shorten_op, |
| 2035 | IRAtom* vatom1) |
sewardj | 57f92b0 | 2010-08-22 11:54:14 +0000 | [diff] [blame] | 2036 | { |
| 2037 | IRAtom *at1, *at2; |
| 2038 | IRAtom* (*pcast)( MCEnv*, IRAtom* ); |
| 2039 | switch (shorten_op) { |
sewardj | 7ee7d85 | 2011-06-16 11:37:21 +0000 | [diff] [blame^] | 2040 | /* FIXME: first 3 are too pessimistic; we can just |
| 2041 | apply them directly to the V bits. */ |
| 2042 | case Iop_NarrowUn16to8x8: pcast = mkPCast16x8; break; |
| 2043 | case Iop_NarrowUn32to16x4: pcast = mkPCast32x4; break; |
| 2044 | case Iop_NarrowUn64to32x2: pcast = mkPCast64x2; break; |
| 2045 | case Iop_QNarrowUn16Sto8Sx8: pcast = mkPCast16x8; break; |
| 2046 | case Iop_QNarrowUn16Sto8Ux8: pcast = mkPCast16x8; break; |
| 2047 | case Iop_QNarrowUn16Uto8Ux8: pcast = mkPCast16x8; break; |
| 2048 | case Iop_QNarrowUn32Sto16Sx4: pcast = mkPCast32x4; break; |
| 2049 | case Iop_QNarrowUn32Sto16Ux4: pcast = mkPCast32x4; break; |
| 2050 | case Iop_QNarrowUn32Uto16Ux4: pcast = mkPCast32x4; break; |
| 2051 | case Iop_QNarrowUn64Sto32Sx2: pcast = mkPCast64x2; break; |
| 2052 | case Iop_QNarrowUn64Sto32Ux2: pcast = mkPCast64x2; break; |
| 2053 | case Iop_QNarrowUn64Uto32Ux2: pcast = mkPCast64x2; break; |
| 2054 | default: VG_(tool_panic)("vectorNarrowUnV128"); |
sewardj | 57f92b0 | 2010-08-22 11:54:14 +0000 | [diff] [blame] | 2055 | } |
| 2056 | tl_assert(isShadowAtom(mce,vatom1)); |
| 2057 | at1 = assignNew('V', mce, Ity_V128, pcast(mce, vatom1)); |
| 2058 | at2 = assignNew('V', mce, Ity_I64, unop(shorten_op, at1)); |
| 2059 | return at2; |
| 2060 | } |
| 2061 | |
| 2062 | static |
sewardj | 7ee7d85 | 2011-06-16 11:37:21 +0000 | [diff] [blame^] | 2063 | IRAtom* vectorWidenI64 ( MCEnv* mce, IROp longen_op, |
| 2064 | IRAtom* vatom1) |
sewardj | 57f92b0 | 2010-08-22 11:54:14 +0000 | [diff] [blame] | 2065 | { |
| 2066 | IRAtom *at1, *at2; |
| 2067 | IRAtom* (*pcast)( MCEnv*, IRAtom* ); |
| 2068 | switch (longen_op) { |
sewardj | 7ee7d85 | 2011-06-16 11:37:21 +0000 | [diff] [blame^] | 2069 | case Iop_Widen8Uto16x8: pcast = mkPCast16x8; break; |
| 2070 | case Iop_Widen8Sto16x8: pcast = mkPCast16x8; break; |
| 2071 | case Iop_Widen16Uto32x4: pcast = mkPCast32x4; break; |
| 2072 | case Iop_Widen16Sto32x4: pcast = mkPCast32x4; break; |
| 2073 | case Iop_Widen32Uto64x2: pcast = mkPCast64x2; break; |
| 2074 | case Iop_Widen32Sto64x2: pcast = mkPCast64x2; break; |
| 2075 | default: VG_(tool_panic)("vectorWidenI64"); |
sewardj | 57f92b0 | 2010-08-22 11:54:14 +0000 | [diff] [blame] | 2076 | } |
| 2077 | tl_assert(isShadowAtom(mce,vatom1)); |
| 2078 | at1 = assignNew('V', mce, Ity_V128, unop(longen_op, vatom1)); |
| 2079 | at2 = assignNew('V', mce, Ity_V128, pcast(mce, at1)); |
| 2080 | return at2; |
| 2081 | } |
| 2082 | |
sewardj | a1d9330 | 2004-12-12 16:45:06 +0000 | [diff] [blame] | 2083 | |
| 2084 | /* --- --- Vector integer arithmetic --- --- */ |
| 2085 | |
| 2086 | /* Simple ... UifU the args and per-lane pessimise the results. */ |
sewardj | acd2e91 | 2005-01-13 19:17:06 +0000 | [diff] [blame] | 2087 | |
sewardj | 20d38f2 | 2005-02-07 23:50:18 +0000 | [diff] [blame] | 2088 | /* --- V128-bit versions --- */ |
sewardj | acd2e91 | 2005-01-13 19:17:06 +0000 | [diff] [blame] | 2089 | |
sewardj | a1d9330 | 2004-12-12 16:45:06 +0000 | [diff] [blame] | 2090 | static |
| 2091 | IRAtom* binary8Ix16 ( MCEnv* mce, IRAtom* vatom1, IRAtom* vatom2 ) |
| 2092 | { |
| 2093 | IRAtom* at; |
sewardj | 20d38f2 | 2005-02-07 23:50:18 +0000 | [diff] [blame] | 2094 | at = mkUifUV128(mce, vatom1, vatom2); |
sewardj | a1d9330 | 2004-12-12 16:45:06 +0000 | [diff] [blame] | 2095 | at = mkPCast8x16(mce, at); |
| 2096 | return at; |
| 2097 | } |
| 2098 | |
| 2099 | static |
| 2100 | IRAtom* binary16Ix8 ( MCEnv* mce, IRAtom* vatom1, IRAtom* vatom2 ) |
| 2101 | { |
| 2102 | IRAtom* at; |
sewardj | 20d38f2 | 2005-02-07 23:50:18 +0000 | [diff] [blame] | 2103 | at = mkUifUV128(mce, vatom1, vatom2); |
sewardj | a1d9330 | 2004-12-12 16:45:06 +0000 | [diff] [blame] | 2104 | at = mkPCast16x8(mce, at); |
| 2105 | return at; |
| 2106 | } |
| 2107 | |
| 2108 | static |
| 2109 | IRAtom* binary32Ix4 ( MCEnv* mce, IRAtom* vatom1, IRAtom* vatom2 ) |
| 2110 | { |
| 2111 | IRAtom* at; |
sewardj | 20d38f2 | 2005-02-07 23:50:18 +0000 | [diff] [blame] | 2112 | at = mkUifUV128(mce, vatom1, vatom2); |
sewardj | a1d9330 | 2004-12-12 16:45:06 +0000 | [diff] [blame] | 2113 | at = mkPCast32x4(mce, at); |
| 2114 | return at; |
| 2115 | } |
| 2116 | |
| 2117 | static |
| 2118 | IRAtom* binary64Ix2 ( MCEnv* mce, IRAtom* vatom1, IRAtom* vatom2 ) |
| 2119 | { |
| 2120 | IRAtom* at; |
sewardj | 20d38f2 | 2005-02-07 23:50:18 +0000 | [diff] [blame] | 2121 | at = mkUifUV128(mce, vatom1, vatom2); |
sewardj | a1d9330 | 2004-12-12 16:45:06 +0000 | [diff] [blame] | 2122 | at = mkPCast64x2(mce, at); |
| 2123 | return at; |
| 2124 | } |
sewardj | 3245c91 | 2004-12-10 14:58:26 +0000 | [diff] [blame] | 2125 | |
sewardj | acd2e91 | 2005-01-13 19:17:06 +0000 | [diff] [blame] | 2126 | /* --- 64-bit versions --- */ |
| 2127 | |
| 2128 | static |
| 2129 | IRAtom* binary8Ix8 ( MCEnv* mce, IRAtom* vatom1, IRAtom* vatom2 ) |
| 2130 | { |
| 2131 | IRAtom* at; |
| 2132 | at = mkUifU64(mce, vatom1, vatom2); |
| 2133 | at = mkPCast8x8(mce, at); |
| 2134 | return at; |
| 2135 | } |
| 2136 | |
| 2137 | static |
| 2138 | IRAtom* binary16Ix4 ( MCEnv* mce, IRAtom* vatom1, IRAtom* vatom2 ) |
| 2139 | { |
| 2140 | IRAtom* at; |
| 2141 | at = mkUifU64(mce, vatom1, vatom2); |
| 2142 | at = mkPCast16x4(mce, at); |
| 2143 | return at; |
| 2144 | } |
| 2145 | |
| 2146 | static |
| 2147 | IRAtom* binary32Ix2 ( MCEnv* mce, IRAtom* vatom1, IRAtom* vatom2 ) |
| 2148 | { |
| 2149 | IRAtom* at; |
| 2150 | at = mkUifU64(mce, vatom1, vatom2); |
| 2151 | at = mkPCast32x2(mce, at); |
| 2152 | return at; |
| 2153 | } |
| 2154 | |
sewardj | 57f92b0 | 2010-08-22 11:54:14 +0000 | [diff] [blame] | 2155 | static |
| 2156 | IRAtom* binary64Ix1 ( MCEnv* mce, IRAtom* vatom1, IRAtom* vatom2 ) |
| 2157 | { |
| 2158 | IRAtom* at; |
| 2159 | at = mkUifU64(mce, vatom1, vatom2); |
| 2160 | at = mkPCastTo(mce, Ity_I64, at); |
| 2161 | return at; |
| 2162 | } |
| 2163 | |
sewardj | c678b85 | 2010-09-22 00:58:51 +0000 | [diff] [blame] | 2164 | /* --- 32-bit versions --- */ |
| 2165 | |
| 2166 | static |
| 2167 | IRAtom* binary8Ix4 ( MCEnv* mce, IRAtom* vatom1, IRAtom* vatom2 ) |
| 2168 | { |
| 2169 | IRAtom* at; |
| 2170 | at = mkUifU32(mce, vatom1, vatom2); |
| 2171 | at = mkPCast8x4(mce, at); |
| 2172 | return at; |
| 2173 | } |
| 2174 | |
| 2175 | static |
| 2176 | IRAtom* binary16Ix2 ( MCEnv* mce, IRAtom* vatom1, IRAtom* vatom2 ) |
| 2177 | { |
| 2178 | IRAtom* at; |
| 2179 | at = mkUifU32(mce, vatom1, vatom2); |
| 2180 | at = mkPCast16x2(mce, at); |
| 2181 | return at; |
| 2182 | } |
| 2183 | |
sewardj | 3245c91 | 2004-12-10 14:58:26 +0000 | [diff] [blame] | 2184 | |
| 2185 | /*------------------------------------------------------------*/ |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 2186 | /*--- Generate shadow values from all kinds of IRExprs. ---*/ |
| 2187 | /*------------------------------------------------------------*/ |
| 2188 | |
| 2189 | static |
sewardj | e91cea7 | 2006-02-08 19:32:02 +0000 | [diff] [blame] | 2190 | IRAtom* expr2vbits_Qop ( MCEnv* mce, |
| 2191 | IROp op, |
| 2192 | IRAtom* atom1, IRAtom* atom2, |
| 2193 | IRAtom* atom3, IRAtom* atom4 ) |
| 2194 | { |
| 2195 | IRAtom* vatom1 = expr2vbits( mce, atom1 ); |
| 2196 | IRAtom* vatom2 = expr2vbits( mce, atom2 ); |
| 2197 | IRAtom* vatom3 = expr2vbits( mce, atom3 ); |
| 2198 | IRAtom* vatom4 = expr2vbits( mce, atom4 ); |
| 2199 | |
| 2200 | tl_assert(isOriginalAtom(mce,atom1)); |
| 2201 | tl_assert(isOriginalAtom(mce,atom2)); |
| 2202 | tl_assert(isOriginalAtom(mce,atom3)); |
| 2203 | tl_assert(isOriginalAtom(mce,atom4)); |
| 2204 | tl_assert(isShadowAtom(mce,vatom1)); |
| 2205 | tl_assert(isShadowAtom(mce,vatom2)); |
| 2206 | tl_assert(isShadowAtom(mce,vatom3)); |
| 2207 | tl_assert(isShadowAtom(mce,vatom4)); |
| 2208 | tl_assert(sameKindedAtoms(atom1,vatom1)); |
| 2209 | tl_assert(sameKindedAtoms(atom2,vatom2)); |
| 2210 | tl_assert(sameKindedAtoms(atom3,vatom3)); |
| 2211 | tl_assert(sameKindedAtoms(atom4,vatom4)); |
| 2212 | switch (op) { |
| 2213 | case Iop_MAddF64: |
| 2214 | case Iop_MAddF64r32: |
| 2215 | case Iop_MSubF64: |
| 2216 | case Iop_MSubF64r32: |
| 2217 | /* I32(rm) x F64 x F64 x F64 -> F64 */ |
| 2218 | return mkLazy4(mce, Ity_I64, vatom1, vatom2, vatom3, vatom4); |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 2219 | |
| 2220 | case Iop_MAddF32: |
| 2221 | case Iop_MSubF32: |
| 2222 | /* I32(rm) x F32 x F32 x F32 -> F32 */ |
| 2223 | return mkLazy4(mce, Ity_I32, vatom1, vatom2, vatom3, vatom4); |
| 2224 | |
sewardj | e91cea7 | 2006-02-08 19:32:02 +0000 | [diff] [blame] | 2225 | default: |
| 2226 | ppIROp(op); |
| 2227 | VG_(tool_panic)("memcheck:expr2vbits_Qop"); |
| 2228 | } |
| 2229 | } |
| 2230 | |
| 2231 | |
| 2232 | static |
sewardj | ed69fdb | 2006-02-03 16:12:27 +0000 | [diff] [blame] | 2233 | IRAtom* expr2vbits_Triop ( MCEnv* mce, |
| 2234 | IROp op, |
| 2235 | IRAtom* atom1, IRAtom* atom2, IRAtom* atom3 ) |
| 2236 | { |
sewardj | ed69fdb | 2006-02-03 16:12:27 +0000 | [diff] [blame] | 2237 | IRAtom* vatom1 = expr2vbits( mce, atom1 ); |
| 2238 | IRAtom* vatom2 = expr2vbits( mce, atom2 ); |
| 2239 | IRAtom* vatom3 = expr2vbits( mce, atom3 ); |
| 2240 | |
| 2241 | tl_assert(isOriginalAtom(mce,atom1)); |
| 2242 | tl_assert(isOriginalAtom(mce,atom2)); |
| 2243 | tl_assert(isOriginalAtom(mce,atom3)); |
| 2244 | tl_assert(isShadowAtom(mce,vatom1)); |
| 2245 | tl_assert(isShadowAtom(mce,vatom2)); |
| 2246 | tl_assert(isShadowAtom(mce,vatom3)); |
| 2247 | tl_assert(sameKindedAtoms(atom1,vatom1)); |
| 2248 | tl_assert(sameKindedAtoms(atom2,vatom2)); |
| 2249 | tl_assert(sameKindedAtoms(atom3,vatom3)); |
| 2250 | switch (op) { |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 2251 | case Iop_AddF128: |
| 2252 | case Iop_SubF128: |
| 2253 | case Iop_MulF128: |
| 2254 | case Iop_DivF128: |
| 2255 | /* I32(rm) x F128 x F128 -> F128 */ |
| 2256 | return mkLazy3(mce, Ity_I128, vatom1, vatom2, vatom3); |
sewardj | ed69fdb | 2006-02-03 16:12:27 +0000 | [diff] [blame] | 2257 | case Iop_AddF64: |
| 2258 | case Iop_AddF64r32: |
| 2259 | case Iop_SubF64: |
| 2260 | case Iop_SubF64r32: |
| 2261 | case Iop_MulF64: |
| 2262 | case Iop_MulF64r32: |
| 2263 | case Iop_DivF64: |
| 2264 | case Iop_DivF64r32: |
sewardj | 22ac5f4 | 2006-02-03 22:55:04 +0000 | [diff] [blame] | 2265 | case Iop_ScaleF64: |
| 2266 | case Iop_Yl2xF64: |
| 2267 | case Iop_Yl2xp1F64: |
| 2268 | case Iop_AtanF64: |
sewardj | d6075eb | 2006-02-04 15:25:23 +0000 | [diff] [blame] | 2269 | case Iop_PRemF64: |
| 2270 | case Iop_PRem1F64: |
sewardj | 22ac5f4 | 2006-02-03 22:55:04 +0000 | [diff] [blame] | 2271 | /* I32(rm) x F64 x F64 -> F64 */ |
sewardj | ed69fdb | 2006-02-03 16:12:27 +0000 | [diff] [blame] | 2272 | return mkLazy3(mce, Ity_I64, vatom1, vatom2, vatom3); |
sewardj | d6075eb | 2006-02-04 15:25:23 +0000 | [diff] [blame] | 2273 | case Iop_PRemC3210F64: |
| 2274 | case Iop_PRem1C3210F64: |
| 2275 | /* I32(rm) x F64 x F64 -> I32 */ |
| 2276 | return mkLazy3(mce, Ity_I32, vatom1, vatom2, vatom3); |
sewardj | 59570ff | 2010-01-01 11:59:33 +0000 | [diff] [blame] | 2277 | case Iop_AddF32: |
| 2278 | case Iop_SubF32: |
| 2279 | case Iop_MulF32: |
| 2280 | case Iop_DivF32: |
| 2281 | /* I32(rm) x F32 x F32 -> I32 */ |
| 2282 | return mkLazy3(mce, Ity_I32, vatom1, vatom2, vatom3); |
sewardj | 57f92b0 | 2010-08-22 11:54:14 +0000 | [diff] [blame] | 2283 | case Iop_ExtractV128: |
| 2284 | complainIfUndefined(mce, atom3); |
| 2285 | return assignNew('V', mce, Ity_V128, triop(op, vatom1, vatom2, atom3)); |
| 2286 | case Iop_Extract64: |
| 2287 | complainIfUndefined(mce, atom3); |
| 2288 | return assignNew('V', mce, Ity_I64, triop(op, vatom1, vatom2, atom3)); |
| 2289 | case Iop_SetElem8x8: |
| 2290 | case Iop_SetElem16x4: |
| 2291 | case Iop_SetElem32x2: |
| 2292 | complainIfUndefined(mce, atom2); |
| 2293 | return assignNew('V', mce, Ity_I64, triop(op, vatom1, atom2, vatom3)); |
sewardj | ed69fdb | 2006-02-03 16:12:27 +0000 | [diff] [blame] | 2294 | default: |
| 2295 | ppIROp(op); |
| 2296 | VG_(tool_panic)("memcheck:expr2vbits_Triop"); |
| 2297 | } |
| 2298 | } |
| 2299 | |
| 2300 | |
| 2301 | static |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 2302 | IRAtom* expr2vbits_Binop ( MCEnv* mce, |
| 2303 | IROp op, |
| 2304 | IRAtom* atom1, IRAtom* atom2 ) |
| 2305 | { |
| 2306 | IRType and_or_ty; |
| 2307 | IRAtom* (*uifu) (MCEnv*, IRAtom*, IRAtom*); |
| 2308 | IRAtom* (*difd) (MCEnv*, IRAtom*, IRAtom*); |
| 2309 | IRAtom* (*improve) (MCEnv*, IRAtom*, IRAtom*); |
| 2310 | |
| 2311 | IRAtom* vatom1 = expr2vbits( mce, atom1 ); |
| 2312 | IRAtom* vatom2 = expr2vbits( mce, atom2 ); |
| 2313 | |
| 2314 | tl_assert(isOriginalAtom(mce,atom1)); |
| 2315 | tl_assert(isOriginalAtom(mce,atom2)); |
| 2316 | tl_assert(isShadowAtom(mce,vatom1)); |
| 2317 | tl_assert(isShadowAtom(mce,vatom2)); |
| 2318 | tl_assert(sameKindedAtoms(atom1,vatom1)); |
| 2319 | tl_assert(sameKindedAtoms(atom2,vatom2)); |
| 2320 | switch (op) { |
| 2321 | |
sewardj | c678b85 | 2010-09-22 00:58:51 +0000 | [diff] [blame] | 2322 | /* 32-bit SIMD */ |
| 2323 | |
| 2324 | case Iop_Add16x2: |
| 2325 | case Iop_HAdd16Ux2: |
| 2326 | case Iop_HAdd16Sx2: |
| 2327 | case Iop_Sub16x2: |
| 2328 | case Iop_HSub16Ux2: |
| 2329 | case Iop_HSub16Sx2: |
| 2330 | case Iop_QAdd16Sx2: |
| 2331 | case Iop_QSub16Sx2: |
| 2332 | return binary16Ix2(mce, vatom1, vatom2); |
| 2333 | |
| 2334 | case Iop_Add8x4: |
| 2335 | case Iop_HAdd8Ux4: |
| 2336 | case Iop_HAdd8Sx4: |
| 2337 | case Iop_Sub8x4: |
| 2338 | case Iop_HSub8Ux4: |
| 2339 | case Iop_HSub8Sx4: |
| 2340 | case Iop_QSub8Ux4: |
| 2341 | case Iop_QAdd8Ux4: |
| 2342 | case Iop_QSub8Sx4: |
| 2343 | case Iop_QAdd8Sx4: |
| 2344 | return binary8Ix4(mce, vatom1, vatom2); |
| 2345 | |
sewardj | acd2e91 | 2005-01-13 19:17:06 +0000 | [diff] [blame] | 2346 | /* 64-bit SIMD */ |
| 2347 | |
sewardj | 57f92b0 | 2010-08-22 11:54:14 +0000 | [diff] [blame] | 2348 | case Iop_ShrN8x8: |
sewardj | acd2e91 | 2005-01-13 19:17:06 +0000 | [diff] [blame] | 2349 | case Iop_ShrN16x4: |
| 2350 | case Iop_ShrN32x2: |
sewardj | 03809ae | 2006-12-27 01:16:58 +0000 | [diff] [blame] | 2351 | case Iop_SarN8x8: |
sewardj | acd2e91 | 2005-01-13 19:17:06 +0000 | [diff] [blame] | 2352 | case Iop_SarN16x4: |
| 2353 | case Iop_SarN32x2: |
| 2354 | case Iop_ShlN16x4: |
| 2355 | case Iop_ShlN32x2: |
sewardj | 114a917 | 2008-02-09 01:49:32 +0000 | [diff] [blame] | 2356 | case Iop_ShlN8x8: |
sewardj | acd2e91 | 2005-01-13 19:17:06 +0000 | [diff] [blame] | 2357 | /* Same scheme as with all other shifts. */ |
| 2358 | complainIfUndefined(mce, atom2); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2359 | return assignNew('V', mce, Ity_I64, binop(op, vatom1, atom2)); |
sewardj | acd2e91 | 2005-01-13 19:17:06 +0000 | [diff] [blame] | 2360 | |
sewardj | 7ee7d85 | 2011-06-16 11:37:21 +0000 | [diff] [blame^] | 2361 | case Iop_QNarrowBin32Sto16Sx4: |
| 2362 | case Iop_QNarrowBin16Sto8Sx8: |
| 2363 | case Iop_QNarrowBin16Sto8Ux8: |
| 2364 | return vectorNarrowBin64(mce, op, vatom1, vatom2); |
sewardj | acd2e91 | 2005-01-13 19:17:06 +0000 | [diff] [blame] | 2365 | |
| 2366 | case Iop_Min8Ux8: |
sewardj | 57f92b0 | 2010-08-22 11:54:14 +0000 | [diff] [blame] | 2367 | case Iop_Min8Sx8: |
sewardj | acd2e91 | 2005-01-13 19:17:06 +0000 | [diff] [blame] | 2368 | case Iop_Max8Ux8: |
sewardj | 57f92b0 | 2010-08-22 11:54:14 +0000 | [diff] [blame] | 2369 | case Iop_Max8Sx8: |
sewardj | acd2e91 | 2005-01-13 19:17:06 +0000 | [diff] [blame] | 2370 | case Iop_Avg8Ux8: |
| 2371 | case Iop_QSub8Sx8: |
| 2372 | case Iop_QSub8Ux8: |
| 2373 | case Iop_Sub8x8: |
| 2374 | case Iop_CmpGT8Sx8: |
sewardj | 57f92b0 | 2010-08-22 11:54:14 +0000 | [diff] [blame] | 2375 | case Iop_CmpGT8Ux8: |
sewardj | acd2e91 | 2005-01-13 19:17:06 +0000 | [diff] [blame] | 2376 | case Iop_CmpEQ8x8: |
| 2377 | case Iop_QAdd8Sx8: |
| 2378 | case Iop_QAdd8Ux8: |
sewardj | 57f92b0 | 2010-08-22 11:54:14 +0000 | [diff] [blame] | 2379 | case Iop_QSal8x8: |
| 2380 | case Iop_QShl8x8: |
sewardj | acd2e91 | 2005-01-13 19:17:06 +0000 | [diff] [blame] | 2381 | case Iop_Add8x8: |
sewardj | 57f92b0 | 2010-08-22 11:54:14 +0000 | [diff] [blame] | 2382 | case Iop_Mul8x8: |
| 2383 | case Iop_PolynomialMul8x8: |
sewardj | acd2e91 | 2005-01-13 19:17:06 +0000 | [diff] [blame] | 2384 | return binary8Ix8(mce, vatom1, vatom2); |
| 2385 | |
| 2386 | case Iop_Min16Sx4: |
sewardj | 57f92b0 | 2010-08-22 11:54:14 +0000 | [diff] [blame] | 2387 | case Iop_Min16Ux4: |
sewardj | acd2e91 | 2005-01-13 19:17:06 +0000 | [diff] [blame] | 2388 | case Iop_Max16Sx4: |
sewardj | 57f92b0 | 2010-08-22 11:54:14 +0000 | [diff] [blame] | 2389 | case Iop_Max16Ux4: |
sewardj | acd2e91 | 2005-01-13 19:17:06 +0000 | [diff] [blame] | 2390 | case Iop_Avg16Ux4: |
| 2391 | case Iop_QSub16Ux4: |
| 2392 | case Iop_QSub16Sx4: |
| 2393 | case Iop_Sub16x4: |
| 2394 | case Iop_Mul16x4: |
| 2395 | case Iop_MulHi16Sx4: |
| 2396 | case Iop_MulHi16Ux4: |
| 2397 | case Iop_CmpGT16Sx4: |
sewardj | 57f92b0 | 2010-08-22 11:54:14 +0000 | [diff] [blame] | 2398 | case Iop_CmpGT16Ux4: |
sewardj | acd2e91 | 2005-01-13 19:17:06 +0000 | [diff] [blame] | 2399 | case Iop_CmpEQ16x4: |
| 2400 | case Iop_QAdd16Sx4: |
| 2401 | case Iop_QAdd16Ux4: |
sewardj | 57f92b0 | 2010-08-22 11:54:14 +0000 | [diff] [blame] | 2402 | case Iop_QSal16x4: |
| 2403 | case Iop_QShl16x4: |
sewardj | acd2e91 | 2005-01-13 19:17:06 +0000 | [diff] [blame] | 2404 | case Iop_Add16x4: |
sewardj | 57f92b0 | 2010-08-22 11:54:14 +0000 | [diff] [blame] | 2405 | case Iop_QDMulHi16Sx4: |
| 2406 | case Iop_QRDMulHi16Sx4: |
sewardj | acd2e91 | 2005-01-13 19:17:06 +0000 | [diff] [blame] | 2407 | return binary16Ix4(mce, vatom1, vatom2); |
| 2408 | |
| 2409 | case Iop_Sub32x2: |
sewardj | 114a917 | 2008-02-09 01:49:32 +0000 | [diff] [blame] | 2410 | case Iop_Mul32x2: |
sewardj | 57f92b0 | 2010-08-22 11:54:14 +0000 | [diff] [blame] | 2411 | case Iop_Max32Sx2: |
| 2412 | case Iop_Max32Ux2: |
| 2413 | case Iop_Min32Sx2: |
| 2414 | case Iop_Min32Ux2: |
sewardj | acd2e91 | 2005-01-13 19:17:06 +0000 | [diff] [blame] | 2415 | case Iop_CmpGT32Sx2: |
sewardj | 57f92b0 | 2010-08-22 11:54:14 +0000 | [diff] [blame] | 2416 | case Iop_CmpGT32Ux2: |
sewardj | acd2e91 | 2005-01-13 19:17:06 +0000 | [diff] [blame] | 2417 | case Iop_CmpEQ32x2: |
| 2418 | case Iop_Add32x2: |
sewardj | 57f92b0 | 2010-08-22 11:54:14 +0000 | [diff] [blame] | 2419 | case Iop_QAdd32Ux2: |
| 2420 | case Iop_QAdd32Sx2: |
| 2421 | case Iop_QSub32Ux2: |
| 2422 | case Iop_QSub32Sx2: |
| 2423 | case Iop_QSal32x2: |
| 2424 | case Iop_QShl32x2: |
| 2425 | case Iop_QDMulHi32Sx2: |
| 2426 | case Iop_QRDMulHi32Sx2: |
sewardj | acd2e91 | 2005-01-13 19:17:06 +0000 | [diff] [blame] | 2427 | return binary32Ix2(mce, vatom1, vatom2); |
| 2428 | |
sewardj | 57f92b0 | 2010-08-22 11:54:14 +0000 | [diff] [blame] | 2429 | case Iop_QSub64Ux1: |
| 2430 | case Iop_QSub64Sx1: |
| 2431 | case Iop_QAdd64Ux1: |
| 2432 | case Iop_QAdd64Sx1: |
| 2433 | case Iop_QSal64x1: |
| 2434 | case Iop_QShl64x1: |
| 2435 | case Iop_Sal64x1: |
| 2436 | return binary64Ix1(mce, vatom1, vatom2); |
| 2437 | |
| 2438 | case Iop_QShlN8Sx8: |
| 2439 | case Iop_QShlN8x8: |
| 2440 | case Iop_QSalN8x8: |
| 2441 | complainIfUndefined(mce, atom2); |
| 2442 | return mkPCast8x8(mce, vatom1); |
| 2443 | |
| 2444 | case Iop_QShlN16Sx4: |
| 2445 | case Iop_QShlN16x4: |
| 2446 | case Iop_QSalN16x4: |
| 2447 | complainIfUndefined(mce, atom2); |
| 2448 | return mkPCast16x4(mce, vatom1); |
| 2449 | |
| 2450 | case Iop_QShlN32Sx2: |
| 2451 | case Iop_QShlN32x2: |
| 2452 | case Iop_QSalN32x2: |
| 2453 | complainIfUndefined(mce, atom2); |
| 2454 | return mkPCast32x2(mce, vatom1); |
| 2455 | |
| 2456 | case Iop_QShlN64Sx1: |
| 2457 | case Iop_QShlN64x1: |
| 2458 | case Iop_QSalN64x1: |
| 2459 | complainIfUndefined(mce, atom2); |
| 2460 | return mkPCast32x2(mce, vatom1); |
| 2461 | |
| 2462 | case Iop_PwMax32Sx2: |
| 2463 | case Iop_PwMax32Ux2: |
| 2464 | case Iop_PwMin32Sx2: |
| 2465 | case Iop_PwMin32Ux2: |
| 2466 | case Iop_PwMax32Fx2: |
| 2467 | case Iop_PwMin32Fx2: |
| 2468 | return assignNew('V', mce, Ity_I64, binop(Iop_PwMax32Ux2, mkPCast32x2(mce, vatom1), |
| 2469 | mkPCast32x2(mce, vatom2))); |
| 2470 | |
| 2471 | case Iop_PwMax16Sx4: |
| 2472 | case Iop_PwMax16Ux4: |
| 2473 | case Iop_PwMin16Sx4: |
| 2474 | case Iop_PwMin16Ux4: |
| 2475 | return assignNew('V', mce, Ity_I64, binop(Iop_PwMax16Ux4, mkPCast16x4(mce, vatom1), |
| 2476 | mkPCast16x4(mce, vatom2))); |
| 2477 | |
| 2478 | case Iop_PwMax8Sx8: |
| 2479 | case Iop_PwMax8Ux8: |
| 2480 | case Iop_PwMin8Sx8: |
| 2481 | case Iop_PwMin8Ux8: |
| 2482 | return assignNew('V', mce, Ity_I64, binop(Iop_PwMax8Ux8, mkPCast8x8(mce, vatom1), |
| 2483 | mkPCast8x8(mce, vatom2))); |
| 2484 | |
| 2485 | case Iop_PwAdd32x2: |
| 2486 | case Iop_PwAdd32Fx2: |
| 2487 | return mkPCast32x2(mce, |
| 2488 | assignNew('V', mce, Ity_I64, binop(Iop_PwAdd32x2, mkPCast32x2(mce, vatom1), |
| 2489 | mkPCast32x2(mce, vatom2)))); |
| 2490 | |
| 2491 | case Iop_PwAdd16x4: |
| 2492 | return mkPCast16x4(mce, |
| 2493 | assignNew('V', mce, Ity_I64, binop(op, mkPCast16x4(mce, vatom1), |
| 2494 | mkPCast16x4(mce, vatom2)))); |
| 2495 | |
| 2496 | case Iop_PwAdd8x8: |
| 2497 | return mkPCast8x8(mce, |
| 2498 | assignNew('V', mce, Ity_I64, binop(op, mkPCast8x8(mce, vatom1), |
| 2499 | mkPCast8x8(mce, vatom2)))); |
| 2500 | |
| 2501 | case Iop_Shl8x8: |
| 2502 | case Iop_Shr8x8: |
| 2503 | case Iop_Sar8x8: |
| 2504 | case Iop_Sal8x8: |
| 2505 | return mkUifU64(mce, |
| 2506 | assignNew('V', mce, Ity_I64, binop(op, vatom1, atom2)), |
| 2507 | mkPCast8x8(mce,vatom2) |
| 2508 | ); |
| 2509 | |
| 2510 | case Iop_Shl16x4: |
| 2511 | case Iop_Shr16x4: |
| 2512 | case Iop_Sar16x4: |
| 2513 | case Iop_Sal16x4: |
| 2514 | return mkUifU64(mce, |
| 2515 | assignNew('V', mce, Ity_I64, binop(op, vatom1, atom2)), |
| 2516 | mkPCast16x4(mce,vatom2) |
| 2517 | ); |
| 2518 | |
| 2519 | case Iop_Shl32x2: |
| 2520 | case Iop_Shr32x2: |
| 2521 | case Iop_Sar32x2: |
| 2522 | case Iop_Sal32x2: |
| 2523 | return mkUifU64(mce, |
| 2524 | assignNew('V', mce, Ity_I64, binop(op, vatom1, atom2)), |
| 2525 | mkPCast32x2(mce,vatom2) |
| 2526 | ); |
| 2527 | |
sewardj | acd2e91 | 2005-01-13 19:17:06 +0000 | [diff] [blame] | 2528 | /* 64-bit data-steering */ |
| 2529 | case Iop_InterleaveLO32x2: |
| 2530 | case Iop_InterleaveLO16x4: |
| 2531 | case Iop_InterleaveLO8x8: |
| 2532 | case Iop_InterleaveHI32x2: |
| 2533 | case Iop_InterleaveHI16x4: |
| 2534 | case Iop_InterleaveHI8x8: |
sewardj | 57f92b0 | 2010-08-22 11:54:14 +0000 | [diff] [blame] | 2535 | case Iop_CatOddLanes8x8: |
| 2536 | case Iop_CatEvenLanes8x8: |
sewardj | 114a917 | 2008-02-09 01:49:32 +0000 | [diff] [blame] | 2537 | case Iop_CatOddLanes16x4: |
| 2538 | case Iop_CatEvenLanes16x4: |
sewardj | 57f92b0 | 2010-08-22 11:54:14 +0000 | [diff] [blame] | 2539 | case Iop_InterleaveOddLanes8x8: |
| 2540 | case Iop_InterleaveEvenLanes8x8: |
| 2541 | case Iop_InterleaveOddLanes16x4: |
| 2542 | case Iop_InterleaveEvenLanes16x4: |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2543 | return assignNew('V', mce, Ity_I64, binop(op, vatom1, vatom2)); |
sewardj | acd2e91 | 2005-01-13 19:17:06 +0000 | [diff] [blame] | 2544 | |
sewardj | 57f92b0 | 2010-08-22 11:54:14 +0000 | [diff] [blame] | 2545 | case Iop_GetElem8x8: |
| 2546 | complainIfUndefined(mce, atom2); |
| 2547 | return assignNew('V', mce, Ity_I8, binop(op, vatom1, atom2)); |
| 2548 | case Iop_GetElem16x4: |
| 2549 | complainIfUndefined(mce, atom2); |
| 2550 | return assignNew('V', mce, Ity_I16, binop(op, vatom1, atom2)); |
| 2551 | case Iop_GetElem32x2: |
| 2552 | complainIfUndefined(mce, atom2); |
| 2553 | return assignNew('V', mce, Ity_I32, binop(op, vatom1, atom2)); |
| 2554 | |
sewardj | 114a917 | 2008-02-09 01:49:32 +0000 | [diff] [blame] | 2555 | /* Perm8x8: rearrange values in left arg using steering values |
| 2556 | from right arg. So rearrange the vbits in the same way but |
| 2557 | pessimise wrt steering values. */ |
| 2558 | case Iop_Perm8x8: |
| 2559 | return mkUifU64( |
| 2560 | mce, |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2561 | assignNew('V', mce, Ity_I64, binop(op, vatom1, atom2)), |
sewardj | 114a917 | 2008-02-09 01:49:32 +0000 | [diff] [blame] | 2562 | mkPCast8x8(mce, vatom2) |
| 2563 | ); |
| 2564 | |
sewardj | 20d38f2 | 2005-02-07 23:50:18 +0000 | [diff] [blame] | 2565 | /* V128-bit SIMD */ |
sewardj | 0b07059 | 2004-12-10 21:44:22 +0000 | [diff] [blame] | 2566 | |
sewardj | 57f92b0 | 2010-08-22 11:54:14 +0000 | [diff] [blame] | 2567 | case Iop_ShrN8x16: |
sewardj | a1d9330 | 2004-12-12 16:45:06 +0000 | [diff] [blame] | 2568 | case Iop_ShrN16x8: |
| 2569 | case Iop_ShrN32x4: |
| 2570 | case Iop_ShrN64x2: |
sewardj | 57f92b0 | 2010-08-22 11:54:14 +0000 | [diff] [blame] | 2571 | case Iop_SarN8x16: |
sewardj | a1d9330 | 2004-12-12 16:45:06 +0000 | [diff] [blame] | 2572 | case Iop_SarN16x8: |
| 2573 | case Iop_SarN32x4: |
sewardj | 57f92b0 | 2010-08-22 11:54:14 +0000 | [diff] [blame] | 2574 | case Iop_SarN64x2: |
| 2575 | case Iop_ShlN8x16: |
sewardj | a1d9330 | 2004-12-12 16:45:06 +0000 | [diff] [blame] | 2576 | case Iop_ShlN16x8: |
| 2577 | case Iop_ShlN32x4: |
| 2578 | case Iop_ShlN64x2: |
sewardj | 620eb5b | 2005-10-22 12:50:43 +0000 | [diff] [blame] | 2579 | /* Same scheme as with all other shifts. Note: 22 Oct 05: |
| 2580 | this is wrong now, scalar shifts are done properly lazily. |
| 2581 | Vector shifts should be fixed too. */ |
sewardj | a1d9330 | 2004-12-12 16:45:06 +0000 | [diff] [blame] | 2582 | complainIfUndefined(mce, atom2); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2583 | return assignNew('V', mce, Ity_V128, binop(op, vatom1, atom2)); |
sewardj | a1d9330 | 2004-12-12 16:45:06 +0000 | [diff] [blame] | 2584 | |
sewardj | cbf8be7 | 2005-11-10 18:34:41 +0000 | [diff] [blame] | 2585 | /* V x V shifts/rotates are done using the standard lazy scheme. */ |
sewardj | 43d6075 | 2005-11-10 18:13:01 +0000 | [diff] [blame] | 2586 | case Iop_Shl8x16: |
| 2587 | case Iop_Shr8x16: |
| 2588 | case Iop_Sar8x16: |
sewardj | 57f92b0 | 2010-08-22 11:54:14 +0000 | [diff] [blame] | 2589 | case Iop_Sal8x16: |
sewardj | cbf8be7 | 2005-11-10 18:34:41 +0000 | [diff] [blame] | 2590 | case Iop_Rol8x16: |
sewardj | 43d6075 | 2005-11-10 18:13:01 +0000 | [diff] [blame] | 2591 | return mkUifUV128(mce, |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2592 | assignNew('V', mce, Ity_V128, binop(op, vatom1, atom2)), |
sewardj | 43d6075 | 2005-11-10 18:13:01 +0000 | [diff] [blame] | 2593 | mkPCast8x16(mce,vatom2) |
| 2594 | ); |
| 2595 | |
| 2596 | case Iop_Shl16x8: |
| 2597 | case Iop_Shr16x8: |
| 2598 | case Iop_Sar16x8: |
sewardj | 57f92b0 | 2010-08-22 11:54:14 +0000 | [diff] [blame] | 2599 | case Iop_Sal16x8: |
sewardj | cbf8be7 | 2005-11-10 18:34:41 +0000 | [diff] [blame] | 2600 | case Iop_Rol16x8: |
sewardj | 43d6075 | 2005-11-10 18:13:01 +0000 | [diff] [blame] | 2601 | return mkUifUV128(mce, |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2602 | assignNew('V', mce, Ity_V128, binop(op, vatom1, atom2)), |
sewardj | 43d6075 | 2005-11-10 18:13:01 +0000 | [diff] [blame] | 2603 | mkPCast16x8(mce,vatom2) |
| 2604 | ); |
| 2605 | |
| 2606 | case Iop_Shl32x4: |
| 2607 | case Iop_Shr32x4: |
| 2608 | case Iop_Sar32x4: |
sewardj | 57f92b0 | 2010-08-22 11:54:14 +0000 | [diff] [blame] | 2609 | case Iop_Sal32x4: |
sewardj | cbf8be7 | 2005-11-10 18:34:41 +0000 | [diff] [blame] | 2610 | case Iop_Rol32x4: |
sewardj | 43d6075 | 2005-11-10 18:13:01 +0000 | [diff] [blame] | 2611 | return mkUifUV128(mce, |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2612 | assignNew('V', mce, Ity_V128, binop(op, vatom1, atom2)), |
sewardj | 43d6075 | 2005-11-10 18:13:01 +0000 | [diff] [blame] | 2613 | mkPCast32x4(mce,vatom2) |
| 2614 | ); |
| 2615 | |
sewardj | 57f92b0 | 2010-08-22 11:54:14 +0000 | [diff] [blame] | 2616 | case Iop_Shl64x2: |
| 2617 | case Iop_Shr64x2: |
| 2618 | case Iop_Sar64x2: |
| 2619 | case Iop_Sal64x2: |
| 2620 | return mkUifUV128(mce, |
| 2621 | assignNew('V', mce, Ity_V128, binop(op, vatom1, atom2)), |
| 2622 | mkPCast64x2(mce,vatom2) |
| 2623 | ); |
| 2624 | |
| 2625 | case Iop_F32ToFixed32Ux4_RZ: |
| 2626 | case Iop_F32ToFixed32Sx4_RZ: |
| 2627 | case Iop_Fixed32UToF32x4_RN: |
| 2628 | case Iop_Fixed32SToF32x4_RN: |
| 2629 | complainIfUndefined(mce, atom2); |
| 2630 | return mkPCast32x4(mce, vatom1); |
| 2631 | |
| 2632 | case Iop_F32ToFixed32Ux2_RZ: |
| 2633 | case Iop_F32ToFixed32Sx2_RZ: |
| 2634 | case Iop_Fixed32UToF32x2_RN: |
| 2635 | case Iop_Fixed32SToF32x2_RN: |
| 2636 | complainIfUndefined(mce, atom2); |
| 2637 | return mkPCast32x2(mce, vatom1); |
| 2638 | |
sewardj | a1d9330 | 2004-12-12 16:45:06 +0000 | [diff] [blame] | 2639 | case Iop_QSub8Ux16: |
| 2640 | case Iop_QSub8Sx16: |
| 2641 | case Iop_Sub8x16: |
| 2642 | case Iop_Min8Ux16: |
sewardj | 43d6075 | 2005-11-10 18:13:01 +0000 | [diff] [blame] | 2643 | case Iop_Min8Sx16: |
sewardj | a1d9330 | 2004-12-12 16:45:06 +0000 | [diff] [blame] | 2644 | case Iop_Max8Ux16: |
sewardj | 43d6075 | 2005-11-10 18:13:01 +0000 | [diff] [blame] | 2645 | case Iop_Max8Sx16: |
sewardj | a1d9330 | 2004-12-12 16:45:06 +0000 | [diff] [blame] | 2646 | case Iop_CmpGT8Sx16: |
sewardj | 43d6075 | 2005-11-10 18:13:01 +0000 | [diff] [blame] | 2647 | case Iop_CmpGT8Ux16: |
sewardj | a1d9330 | 2004-12-12 16:45:06 +0000 | [diff] [blame] | 2648 | case Iop_CmpEQ8x16: |
| 2649 | case Iop_Avg8Ux16: |
sewardj | 43d6075 | 2005-11-10 18:13:01 +0000 | [diff] [blame] | 2650 | case Iop_Avg8Sx16: |
sewardj | a1d9330 | 2004-12-12 16:45:06 +0000 | [diff] [blame] | 2651 | case Iop_QAdd8Ux16: |
| 2652 | case Iop_QAdd8Sx16: |
sewardj | 57f92b0 | 2010-08-22 11:54:14 +0000 | [diff] [blame] | 2653 | case Iop_QSal8x16: |
| 2654 | case Iop_QShl8x16: |
sewardj | a1d9330 | 2004-12-12 16:45:06 +0000 | [diff] [blame] | 2655 | case Iop_Add8x16: |
sewardj | 57f92b0 | 2010-08-22 11:54:14 +0000 | [diff] [blame] | 2656 | case Iop_Mul8x16: |
| 2657 | case Iop_PolynomialMul8x16: |
sewardj | a1d9330 | 2004-12-12 16:45:06 +0000 | [diff] [blame] | 2658 | return binary8Ix16(mce, vatom1, vatom2); |
| 2659 | |
| 2660 | case Iop_QSub16Ux8: |
| 2661 | case Iop_QSub16Sx8: |
| 2662 | case Iop_Sub16x8: |
| 2663 | case Iop_Mul16x8: |
| 2664 | case Iop_MulHi16Sx8: |
| 2665 | case Iop_MulHi16Ux8: |
| 2666 | case Iop_Min16Sx8: |
sewardj | 43d6075 | 2005-11-10 18:13:01 +0000 | [diff] [blame] | 2667 | case Iop_Min16Ux8: |
sewardj | a1d9330 | 2004-12-12 16:45:06 +0000 | [diff] [blame] | 2668 | case Iop_Max16Sx8: |
sewardj | 43d6075 | 2005-11-10 18:13:01 +0000 | [diff] [blame] | 2669 | case Iop_Max16Ux8: |
sewardj | a1d9330 | 2004-12-12 16:45:06 +0000 | [diff] [blame] | 2670 | case Iop_CmpGT16Sx8: |
sewardj | 43d6075 | 2005-11-10 18:13:01 +0000 | [diff] [blame] | 2671 | case Iop_CmpGT16Ux8: |
sewardj | a1d9330 | 2004-12-12 16:45:06 +0000 | [diff] [blame] | 2672 | case Iop_CmpEQ16x8: |
| 2673 | case Iop_Avg16Ux8: |
sewardj | 43d6075 | 2005-11-10 18:13:01 +0000 | [diff] [blame] | 2674 | case Iop_Avg16Sx8: |
sewardj | a1d9330 | 2004-12-12 16:45:06 +0000 | [diff] [blame] | 2675 | case Iop_QAdd16Ux8: |
| 2676 | case Iop_QAdd16Sx8: |
sewardj | 57f92b0 | 2010-08-22 11:54:14 +0000 | [diff] [blame] | 2677 | case Iop_QSal16x8: |
| 2678 | case Iop_QShl16x8: |
sewardj | a1d9330 | 2004-12-12 16:45:06 +0000 | [diff] [blame] | 2679 | case Iop_Add16x8: |
sewardj | 57f92b0 | 2010-08-22 11:54:14 +0000 | [diff] [blame] | 2680 | case Iop_QDMulHi16Sx8: |
| 2681 | case Iop_QRDMulHi16Sx8: |
sewardj | a1d9330 | 2004-12-12 16:45:06 +0000 | [diff] [blame] | 2682 | return binary16Ix8(mce, vatom1, vatom2); |
| 2683 | |
| 2684 | case Iop_Sub32x4: |
| 2685 | case Iop_CmpGT32Sx4: |
sewardj | 43d6075 | 2005-11-10 18:13:01 +0000 | [diff] [blame] | 2686 | case Iop_CmpGT32Ux4: |
sewardj | a1d9330 | 2004-12-12 16:45:06 +0000 | [diff] [blame] | 2687 | case Iop_CmpEQ32x4: |
sewardj | 43d6075 | 2005-11-10 18:13:01 +0000 | [diff] [blame] | 2688 | case Iop_QAdd32Sx4: |
| 2689 | case Iop_QAdd32Ux4: |
| 2690 | case Iop_QSub32Sx4: |
| 2691 | case Iop_QSub32Ux4: |
sewardj | 57f92b0 | 2010-08-22 11:54:14 +0000 | [diff] [blame] | 2692 | case Iop_QSal32x4: |
| 2693 | case Iop_QShl32x4: |
sewardj | 43d6075 | 2005-11-10 18:13:01 +0000 | [diff] [blame] | 2694 | case Iop_Avg32Ux4: |
| 2695 | case Iop_Avg32Sx4: |
sewardj | a1d9330 | 2004-12-12 16:45:06 +0000 | [diff] [blame] | 2696 | case Iop_Add32x4: |
sewardj | 43d6075 | 2005-11-10 18:13:01 +0000 | [diff] [blame] | 2697 | case Iop_Max32Ux4: |
| 2698 | case Iop_Max32Sx4: |
| 2699 | case Iop_Min32Ux4: |
| 2700 | case Iop_Min32Sx4: |
sewardj | b823b85 | 2010-06-18 08:18:38 +0000 | [diff] [blame] | 2701 | case Iop_Mul32x4: |
sewardj | 57f92b0 | 2010-08-22 11:54:14 +0000 | [diff] [blame] | 2702 | case Iop_QDMulHi32Sx4: |
| 2703 | case Iop_QRDMulHi32Sx4: |
sewardj | a1d9330 | 2004-12-12 16:45:06 +0000 | [diff] [blame] | 2704 | return binary32Ix4(mce, vatom1, vatom2); |
| 2705 | |
| 2706 | case Iop_Sub64x2: |
| 2707 | case Iop_Add64x2: |
sewardj | b823b85 | 2010-06-18 08:18:38 +0000 | [diff] [blame] | 2708 | case Iop_CmpGT64Sx2: |
sewardj | 57f92b0 | 2010-08-22 11:54:14 +0000 | [diff] [blame] | 2709 | case Iop_QSal64x2: |
| 2710 | case Iop_QShl64x2: |
| 2711 | case Iop_QAdd64Ux2: |
| 2712 | case Iop_QAdd64Sx2: |
| 2713 | case Iop_QSub64Ux2: |
| 2714 | case Iop_QSub64Sx2: |
sewardj | a1d9330 | 2004-12-12 16:45:06 +0000 | [diff] [blame] | 2715 | return binary64Ix2(mce, vatom1, vatom2); |
| 2716 | |
sewardj | 7ee7d85 | 2011-06-16 11:37:21 +0000 | [diff] [blame^] | 2717 | case Iop_QNarrowBin32Sto16Sx8: |
| 2718 | case Iop_QNarrowBin32Uto16Ux8: |
| 2719 | case Iop_QNarrowBin32Sto16Ux8: |
| 2720 | case Iop_QNarrowBin16Sto8Sx16: |
| 2721 | case Iop_QNarrowBin16Uto8Ux16: |
| 2722 | case Iop_QNarrowBin16Sto8Ux16: |
| 2723 | return vectorNarrowBinV128(mce, op, vatom1, vatom2); |
sewardj | a1d9330 | 2004-12-12 16:45:06 +0000 | [diff] [blame] | 2724 | |
sewardj | 0b07059 | 2004-12-10 21:44:22 +0000 | [diff] [blame] | 2725 | case Iop_Sub64Fx2: |
| 2726 | case Iop_Mul64Fx2: |
| 2727 | case Iop_Min64Fx2: |
| 2728 | case Iop_Max64Fx2: |
| 2729 | case Iop_Div64Fx2: |
| 2730 | case Iop_CmpLT64Fx2: |
| 2731 | case Iop_CmpLE64Fx2: |
| 2732 | case Iop_CmpEQ64Fx2: |
sewardj | 545663e | 2005-11-05 01:55:04 +0000 | [diff] [blame] | 2733 | case Iop_CmpUN64Fx2: |
sewardj | 0b07059 | 2004-12-10 21:44:22 +0000 | [diff] [blame] | 2734 | case Iop_Add64Fx2: |
| 2735 | return binary64Fx2(mce, vatom1, vatom2); |
| 2736 | |
| 2737 | case Iop_Sub64F0x2: |
| 2738 | case Iop_Mul64F0x2: |
| 2739 | case Iop_Min64F0x2: |
| 2740 | case Iop_Max64F0x2: |
| 2741 | case Iop_Div64F0x2: |
| 2742 | case Iop_CmpLT64F0x2: |
| 2743 | case Iop_CmpLE64F0x2: |
| 2744 | case Iop_CmpEQ64F0x2: |
sewardj | 545663e | 2005-11-05 01:55:04 +0000 | [diff] [blame] | 2745 | case Iop_CmpUN64F0x2: |
sewardj | 0b07059 | 2004-12-10 21:44:22 +0000 | [diff] [blame] | 2746 | case Iop_Add64F0x2: |
| 2747 | return binary64F0x2(mce, vatom1, vatom2); |
| 2748 | |
sewardj | 170ee21 | 2004-12-10 18:57:51 +0000 | [diff] [blame] | 2749 | case Iop_Sub32Fx4: |
| 2750 | case Iop_Mul32Fx4: |
| 2751 | case Iop_Min32Fx4: |
| 2752 | case Iop_Max32Fx4: |
| 2753 | case Iop_Div32Fx4: |
| 2754 | case Iop_CmpLT32Fx4: |
| 2755 | case Iop_CmpLE32Fx4: |
| 2756 | case Iop_CmpEQ32Fx4: |
sewardj | 545663e | 2005-11-05 01:55:04 +0000 | [diff] [blame] | 2757 | case Iop_CmpUN32Fx4: |
cerion | e78ba2a | 2005-11-14 03:00:35 +0000 | [diff] [blame] | 2758 | case Iop_CmpGT32Fx4: |
| 2759 | case Iop_CmpGE32Fx4: |
sewardj | 3245c91 | 2004-12-10 14:58:26 +0000 | [diff] [blame] | 2760 | case Iop_Add32Fx4: |
sewardj | 57f92b0 | 2010-08-22 11:54:14 +0000 | [diff] [blame] | 2761 | case Iop_Recps32Fx4: |
| 2762 | case Iop_Rsqrts32Fx4: |
sewardj | 3245c91 | 2004-12-10 14:58:26 +0000 | [diff] [blame] | 2763 | return binary32Fx4(mce, vatom1, vatom2); |
| 2764 | |
sewardj | 57f92b0 | 2010-08-22 11:54:14 +0000 | [diff] [blame] | 2765 | case Iop_Sub32Fx2: |
| 2766 | case Iop_Mul32Fx2: |
| 2767 | case Iop_Min32Fx2: |
| 2768 | case Iop_Max32Fx2: |
| 2769 | case Iop_CmpEQ32Fx2: |
| 2770 | case Iop_CmpGT32Fx2: |
| 2771 | case Iop_CmpGE32Fx2: |
| 2772 | case Iop_Add32Fx2: |
| 2773 | case Iop_Recps32Fx2: |
| 2774 | case Iop_Rsqrts32Fx2: |
| 2775 | return binary32Fx2(mce, vatom1, vatom2); |
| 2776 | |
sewardj | 170ee21 | 2004-12-10 18:57:51 +0000 | [diff] [blame] | 2777 | case Iop_Sub32F0x4: |
| 2778 | case Iop_Mul32F0x4: |
| 2779 | case Iop_Min32F0x4: |
| 2780 | case Iop_Max32F0x4: |
| 2781 | case Iop_Div32F0x4: |
| 2782 | case Iop_CmpLT32F0x4: |
| 2783 | case Iop_CmpLE32F0x4: |
| 2784 | case Iop_CmpEQ32F0x4: |
sewardj | 545663e | 2005-11-05 01:55:04 +0000 | [diff] [blame] | 2785 | case Iop_CmpUN32F0x4: |
sewardj | 170ee21 | 2004-12-10 18:57:51 +0000 | [diff] [blame] | 2786 | case Iop_Add32F0x4: |
| 2787 | return binary32F0x4(mce, vatom1, vatom2); |
| 2788 | |
sewardj | 57f92b0 | 2010-08-22 11:54:14 +0000 | [diff] [blame] | 2789 | case Iop_QShlN8Sx16: |
| 2790 | case Iop_QShlN8x16: |
| 2791 | case Iop_QSalN8x16: |
| 2792 | complainIfUndefined(mce, atom2); |
| 2793 | return mkPCast8x16(mce, vatom1); |
| 2794 | |
| 2795 | case Iop_QShlN16Sx8: |
| 2796 | case Iop_QShlN16x8: |
| 2797 | case Iop_QSalN16x8: |
| 2798 | complainIfUndefined(mce, atom2); |
| 2799 | return mkPCast16x8(mce, vatom1); |
| 2800 | |
| 2801 | case Iop_QShlN32Sx4: |
| 2802 | case Iop_QShlN32x4: |
| 2803 | case Iop_QSalN32x4: |
| 2804 | complainIfUndefined(mce, atom2); |
| 2805 | return mkPCast32x4(mce, vatom1); |
| 2806 | |
| 2807 | case Iop_QShlN64Sx2: |
| 2808 | case Iop_QShlN64x2: |
| 2809 | case Iop_QSalN64x2: |
| 2810 | complainIfUndefined(mce, atom2); |
| 2811 | return mkPCast32x4(mce, vatom1); |
| 2812 | |
| 2813 | case Iop_Mull32Sx2: |
| 2814 | case Iop_Mull32Ux2: |
| 2815 | case Iop_QDMulLong32Sx2: |
sewardj | 7ee7d85 | 2011-06-16 11:37:21 +0000 | [diff] [blame^] | 2816 | return vectorWidenI64(mce, Iop_Widen32Sto64x2, |
| 2817 | mkUifU64(mce, vatom1, vatom2)); |
sewardj | 57f92b0 | 2010-08-22 11:54:14 +0000 | [diff] [blame] | 2818 | |
| 2819 | case Iop_Mull16Sx4: |
| 2820 | case Iop_Mull16Ux4: |
| 2821 | case Iop_QDMulLong16Sx4: |
sewardj | 7ee7d85 | 2011-06-16 11:37:21 +0000 | [diff] [blame^] | 2822 | return vectorWidenI64(mce, Iop_Widen16Sto32x4, |
| 2823 | mkUifU64(mce, vatom1, vatom2)); |
sewardj | 57f92b0 | 2010-08-22 11:54:14 +0000 | [diff] [blame] | 2824 | |
| 2825 | case Iop_Mull8Sx8: |
| 2826 | case Iop_Mull8Ux8: |
| 2827 | case Iop_PolynomialMull8x8: |
sewardj | 7ee7d85 | 2011-06-16 11:37:21 +0000 | [diff] [blame^] | 2828 | return vectorWidenI64(mce, Iop_Widen8Sto16x8, |
| 2829 | mkUifU64(mce, vatom1, vatom2)); |
sewardj | 57f92b0 | 2010-08-22 11:54:14 +0000 | [diff] [blame] | 2830 | |
| 2831 | case Iop_PwAdd32x4: |
| 2832 | return mkPCast32x4(mce, |
| 2833 | assignNew('V', mce, Ity_V128, binop(op, mkPCast32x4(mce, vatom1), |
| 2834 | mkPCast32x4(mce, vatom2)))); |
| 2835 | |
| 2836 | case Iop_PwAdd16x8: |
| 2837 | return mkPCast16x8(mce, |
| 2838 | assignNew('V', mce, Ity_V128, binop(op, mkPCast16x8(mce, vatom1), |
| 2839 | mkPCast16x8(mce, vatom2)))); |
| 2840 | |
| 2841 | case Iop_PwAdd8x16: |
| 2842 | return mkPCast8x16(mce, |
| 2843 | assignNew('V', mce, Ity_V128, binop(op, mkPCast8x16(mce, vatom1), |
| 2844 | mkPCast8x16(mce, vatom2)))); |
| 2845 | |
sewardj | 20d38f2 | 2005-02-07 23:50:18 +0000 | [diff] [blame] | 2846 | /* V128-bit data-steering */ |
| 2847 | case Iop_SetV128lo32: |
| 2848 | case Iop_SetV128lo64: |
| 2849 | case Iop_64HLtoV128: |
sewardj | a1d9330 | 2004-12-12 16:45:06 +0000 | [diff] [blame] | 2850 | case Iop_InterleaveLO64x2: |
| 2851 | case Iop_InterleaveLO32x4: |
| 2852 | case Iop_InterleaveLO16x8: |
| 2853 | case Iop_InterleaveLO8x16: |
| 2854 | case Iop_InterleaveHI64x2: |
| 2855 | case Iop_InterleaveHI32x4: |
| 2856 | case Iop_InterleaveHI16x8: |
| 2857 | case Iop_InterleaveHI8x16: |
sewardj | 57f92b0 | 2010-08-22 11:54:14 +0000 | [diff] [blame] | 2858 | case Iop_CatOddLanes8x16: |
| 2859 | case Iop_CatOddLanes16x8: |
| 2860 | case Iop_CatOddLanes32x4: |
| 2861 | case Iop_CatEvenLanes8x16: |
| 2862 | case Iop_CatEvenLanes16x8: |
| 2863 | case Iop_CatEvenLanes32x4: |
| 2864 | case Iop_InterleaveOddLanes8x16: |
| 2865 | case Iop_InterleaveOddLanes16x8: |
| 2866 | case Iop_InterleaveOddLanes32x4: |
| 2867 | case Iop_InterleaveEvenLanes8x16: |
| 2868 | case Iop_InterleaveEvenLanes16x8: |
| 2869 | case Iop_InterleaveEvenLanes32x4: |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2870 | return assignNew('V', mce, Ity_V128, binop(op, vatom1, vatom2)); |
sewardj | 57f92b0 | 2010-08-22 11:54:14 +0000 | [diff] [blame] | 2871 | |
| 2872 | case Iop_GetElem8x16: |
| 2873 | complainIfUndefined(mce, atom2); |
| 2874 | return assignNew('V', mce, Ity_I8, binop(op, vatom1, atom2)); |
| 2875 | case Iop_GetElem16x8: |
| 2876 | complainIfUndefined(mce, atom2); |
| 2877 | return assignNew('V', mce, Ity_I16, binop(op, vatom1, atom2)); |
| 2878 | case Iop_GetElem32x4: |
| 2879 | complainIfUndefined(mce, atom2); |
| 2880 | return assignNew('V', mce, Ity_I32, binop(op, vatom1, atom2)); |
| 2881 | case Iop_GetElem64x2: |
| 2882 | complainIfUndefined(mce, atom2); |
| 2883 | return assignNew('V', mce, Ity_I64, binop(op, vatom1, atom2)); |
| 2884 | |
sewardj | 620eb5b | 2005-10-22 12:50:43 +0000 | [diff] [blame] | 2885 | /* Perm8x16: rearrange values in left arg using steering values |
| 2886 | from right arg. So rearrange the vbits in the same way but |
| 2887 | pessimise wrt steering values. */ |
| 2888 | case Iop_Perm8x16: |
| 2889 | return mkUifUV128( |
| 2890 | mce, |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2891 | assignNew('V', mce, Ity_V128, binop(op, vatom1, atom2)), |
sewardj | 620eb5b | 2005-10-22 12:50:43 +0000 | [diff] [blame] | 2892 | mkPCast8x16(mce, vatom2) |
| 2893 | ); |
sewardj | 170ee21 | 2004-12-10 18:57:51 +0000 | [diff] [blame] | 2894 | |
sewardj | 43d6075 | 2005-11-10 18:13:01 +0000 | [diff] [blame] | 2895 | /* These two take the lower half of each 16-bit lane, sign/zero |
| 2896 | extend it to 32, and multiply together, producing a 32x4 |
| 2897 | result (and implicitly ignoring half the operand bits). So |
| 2898 | treat it as a bunch of independent 16x8 operations, but then |
| 2899 | do 32-bit shifts left-right to copy the lower half results |
| 2900 | (which are all 0s or all 1s due to PCasting in binary16Ix8) |
| 2901 | into the upper half of each result lane. */ |
| 2902 | case Iop_MullEven16Ux8: |
| 2903 | case Iop_MullEven16Sx8: { |
| 2904 | IRAtom* at; |
| 2905 | at = binary16Ix8(mce,vatom1,vatom2); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2906 | at = assignNew('V', mce, Ity_V128, binop(Iop_ShlN32x4, at, mkU8(16))); |
| 2907 | at = assignNew('V', mce, Ity_V128, binop(Iop_SarN32x4, at, mkU8(16))); |
sewardj | 43d6075 | 2005-11-10 18:13:01 +0000 | [diff] [blame] | 2908 | return at; |
| 2909 | } |
| 2910 | |
| 2911 | /* Same deal as Iop_MullEven16{S,U}x8 */ |
| 2912 | case Iop_MullEven8Ux16: |
| 2913 | case Iop_MullEven8Sx16: { |
| 2914 | IRAtom* at; |
| 2915 | at = binary8Ix16(mce,vatom1,vatom2); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2916 | at = assignNew('V', mce, Ity_V128, binop(Iop_ShlN16x8, at, mkU8(8))); |
| 2917 | at = assignNew('V', mce, Ity_V128, binop(Iop_SarN16x8, at, mkU8(8))); |
sewardj | 43d6075 | 2005-11-10 18:13:01 +0000 | [diff] [blame] | 2918 | return at; |
| 2919 | } |
| 2920 | |
| 2921 | /* narrow 2xV128 into 1xV128, hi half from left arg, in a 2 x |
| 2922 | 32x4 -> 16x8 laneage, discarding the upper half of each lane. |
| 2923 | Simply apply same op to the V bits, since this really no more |
| 2924 | than a data steering operation. */ |
sewardj | 7ee7d85 | 2011-06-16 11:37:21 +0000 | [diff] [blame^] | 2925 | case Iop_NarrowBin32to16x8: |
| 2926 | case Iop_NarrowBin16to8x16: |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2927 | return assignNew('V', mce, Ity_V128, |
| 2928 | binop(op, vatom1, vatom2)); |
sewardj | 43d6075 | 2005-11-10 18:13:01 +0000 | [diff] [blame] | 2929 | |
| 2930 | case Iop_ShrV128: |
| 2931 | case Iop_ShlV128: |
| 2932 | /* Same scheme as with all other shifts. Note: 10 Nov 05: |
| 2933 | this is wrong now, scalar shifts are done properly lazily. |
| 2934 | Vector shifts should be fixed too. */ |
| 2935 | complainIfUndefined(mce, atom2); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2936 | return assignNew('V', mce, Ity_V128, binop(op, vatom1, atom2)); |
sewardj | 43d6075 | 2005-11-10 18:13:01 +0000 | [diff] [blame] | 2937 | |
sewardj | 69a1332 | 2005-04-23 01:14:51 +0000 | [diff] [blame] | 2938 | /* I128-bit data-steering */ |
| 2939 | case Iop_64HLto128: |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 2940 | return assignNew('V', mce, Ity_I128, binop(op, vatom1, vatom2)); |
sewardj | 69a1332 | 2005-04-23 01:14:51 +0000 | [diff] [blame] | 2941 | |
sewardj | 3245c91 | 2004-12-10 14:58:26 +0000 | [diff] [blame] | 2942 | /* Scalar floating point */ |
| 2943 | |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 2944 | case Iop_F32toI64S: |
| 2945 | /* I32(rm) x F32 -> I64 */ |
| 2946 | return mkLazy2(mce, Ity_I64, vatom1, vatom2); |
| 2947 | |
| 2948 | case Iop_I64StoF32: |
| 2949 | /* I32(rm) x I64 -> F32 */ |
| 2950 | return mkLazy2(mce, Ity_I32, vatom1, vatom2); |
| 2951 | |
sewardj | ed69fdb | 2006-02-03 16:12:27 +0000 | [diff] [blame] | 2952 | case Iop_RoundF64toInt: |
| 2953 | case Iop_RoundF64toF32: |
sewardj | 06f96d0 | 2009-12-31 19:24:12 +0000 | [diff] [blame] | 2954 | case Iop_F64toI64S: |
| 2955 | case Iop_I64StoF64: |
sewardj | f34eb49 | 2011-04-15 11:57:05 +0000 | [diff] [blame] | 2956 | case Iop_I64UtoF64: |
sewardj | 22ac5f4 | 2006-02-03 22:55:04 +0000 | [diff] [blame] | 2957 | case Iop_SinF64: |
| 2958 | case Iop_CosF64: |
| 2959 | case Iop_TanF64: |
| 2960 | case Iop_2xm1F64: |
| 2961 | case Iop_SqrtF64: |
| 2962 | /* I32(rm) x I64/F64 -> I64/F64 */ |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 2963 | return mkLazy2(mce, Ity_I64, vatom1, vatom2); |
| 2964 | |
sewardj | d376a76 | 2010-06-27 09:08:54 +0000 | [diff] [blame] | 2965 | case Iop_RoundF32toInt: |
sewardj | aec1be3 | 2010-01-03 22:29:32 +0000 | [diff] [blame] | 2966 | case Iop_SqrtF32: |
| 2967 | /* I32(rm) x I32/F32 -> I32/F32 */ |
| 2968 | return mkLazy2(mce, Ity_I32, vatom1, vatom2); |
| 2969 | |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 2970 | case Iop_SqrtF128: |
| 2971 | /* I32(rm) x F128 -> F128 */ |
| 2972 | return mkLazy2(mce, Ity_I128, vatom1, vatom2); |
| 2973 | |
| 2974 | case Iop_I32StoF32: |
| 2975 | case Iop_F32toI32S: |
| 2976 | /* First arg is I32 (rounding mode), second is F32/I32 (data). */ |
| 2977 | return mkLazy2(mce, Ity_I32, vatom1, vatom2); |
| 2978 | |
| 2979 | case Iop_F128toI32S: /* IRRoundingMode(I32) x F128 -> signed I32 */ |
| 2980 | case Iop_F128toF32: /* IRRoundingMode(I32) x F128 -> F32 */ |
| 2981 | return mkLazy2(mce, Ity_I32, vatom1, vatom2); |
| 2982 | |
| 2983 | case Iop_F128toI64S: /* IRRoundingMode(I32) x F128 -> signed I64 */ |
| 2984 | case Iop_F128toF64: /* IRRoundingMode(I32) x F128 -> F64 */ |
| 2985 | return mkLazy2(mce, Ity_I64, vatom1, vatom2); |
| 2986 | |
| 2987 | case Iop_F64HLtoF128: |
| 2988 | return assignNew('V', mce, Ity_I128, binop(Iop_64HLto128, vatom1, vatom2)); |
| 2989 | |
sewardj | 59570ff | 2010-01-01 11:59:33 +0000 | [diff] [blame] | 2990 | case Iop_F64toI32U: |
sewardj | 06f96d0 | 2009-12-31 19:24:12 +0000 | [diff] [blame] | 2991 | case Iop_F64toI32S: |
sewardj | e9e16d3 | 2004-12-10 13:17:55 +0000 | [diff] [blame] | 2992 | case Iop_F64toF32: |
sewardj | f34eb49 | 2011-04-15 11:57:05 +0000 | [diff] [blame] | 2993 | case Iop_I64UtoF32: |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 2994 | /* First arg is I32 (rounding mode), second is F64 (data). */ |
| 2995 | return mkLazy2(mce, Ity_I32, vatom1, vatom2); |
| 2996 | |
sewardj | 06f96d0 | 2009-12-31 19:24:12 +0000 | [diff] [blame] | 2997 | case Iop_F64toI16S: |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 2998 | /* First arg is I32 (rounding mode), second is F64 (data). */ |
| 2999 | return mkLazy2(mce, Ity_I16, vatom1, vatom2); |
| 3000 | |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 3001 | case Iop_CmpF32: |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3002 | case Iop_CmpF64: |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 3003 | case Iop_CmpF128: |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3004 | return mkLazy2(mce, Ity_I32, vatom1, vatom2); |
| 3005 | |
| 3006 | /* non-FP after here */ |
| 3007 | |
| 3008 | case Iop_DivModU64to32: |
| 3009 | case Iop_DivModS64to32: |
| 3010 | return mkLazy2(mce, Ity_I64, vatom1, vatom2); |
| 3011 | |
sewardj | 69a1332 | 2005-04-23 01:14:51 +0000 | [diff] [blame] | 3012 | case Iop_DivModU128to64: |
| 3013 | case Iop_DivModS128to64: |
| 3014 | return mkLazy2(mce, Ity_I128, vatom1, vatom2); |
| 3015 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3016 | case Iop_16HLto32: |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 3017 | return assignNew('V', mce, Ity_I32, binop(op, vatom1, vatom2)); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3018 | case Iop_32HLto64: |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 3019 | return assignNew('V', mce, Ity_I64, binop(op, vatom1, vatom2)); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3020 | |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 3021 | case Iop_DivModS64to64: |
sewardj | 6cf40ff | 2005-04-20 22:31:26 +0000 | [diff] [blame] | 3022 | case Iop_MullS64: |
| 3023 | case Iop_MullU64: { |
| 3024 | IRAtom* vLo64 = mkLeft64(mce, mkUifU64(mce, vatom1,vatom2)); |
| 3025 | IRAtom* vHi64 = mkPCastTo(mce, Ity_I64, vLo64); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 3026 | return assignNew('V', mce, Ity_I128, binop(Iop_64HLto128, vHi64, vLo64)); |
sewardj | 6cf40ff | 2005-04-20 22:31:26 +0000 | [diff] [blame] | 3027 | } |
| 3028 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3029 | case Iop_MullS32: |
| 3030 | case Iop_MullU32: { |
| 3031 | IRAtom* vLo32 = mkLeft32(mce, mkUifU32(mce, vatom1,vatom2)); |
| 3032 | IRAtom* vHi32 = mkPCastTo(mce, Ity_I32, vLo32); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 3033 | return assignNew('V', mce, Ity_I64, binop(Iop_32HLto64, vHi32, vLo32)); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3034 | } |
| 3035 | |
| 3036 | case Iop_MullS16: |
| 3037 | case Iop_MullU16: { |
| 3038 | IRAtom* vLo16 = mkLeft16(mce, mkUifU16(mce, vatom1,vatom2)); |
| 3039 | IRAtom* vHi16 = mkPCastTo(mce, Ity_I16, vLo16); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 3040 | return assignNew('V', mce, Ity_I32, binop(Iop_16HLto32, vHi16, vLo16)); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3041 | } |
| 3042 | |
| 3043 | case Iop_MullS8: |
| 3044 | case Iop_MullU8: { |
| 3045 | IRAtom* vLo8 = mkLeft8(mce, mkUifU8(mce, vatom1,vatom2)); |
| 3046 | IRAtom* vHi8 = mkPCastTo(mce, Ity_I8, vLo8); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 3047 | return assignNew('V', mce, Ity_I16, binop(Iop_8HLto16, vHi8, vLo8)); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3048 | } |
| 3049 | |
sewardj | 5af0506 | 2010-10-18 16:31:14 +0000 | [diff] [blame] | 3050 | case Iop_Sad8Ux4: /* maybe we could do better? ftm, do mkLazy2. */ |
cerion | 9e59108 | 2005-06-23 15:28:34 +0000 | [diff] [blame] | 3051 | case Iop_DivS32: |
| 3052 | case Iop_DivU32: |
| 3053 | return mkLazy2(mce, Ity_I32, vatom1, vatom2); |
| 3054 | |
sewardj | b00944a | 2005-12-23 12:47:16 +0000 | [diff] [blame] | 3055 | case Iop_DivS64: |
| 3056 | case Iop_DivU64: |
| 3057 | return mkLazy2(mce, Ity_I64, vatom1, vatom2); |
| 3058 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3059 | case Iop_Add32: |
sewardj | d5204dc | 2004-12-31 01:16:11 +0000 | [diff] [blame] | 3060 | if (mce->bogusLiterals) |
| 3061 | return expensiveAddSub(mce,True,Ity_I32, |
| 3062 | vatom1,vatom2, atom1,atom2); |
| 3063 | else |
| 3064 | goto cheap_AddSub32; |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3065 | case Iop_Sub32: |
sewardj | d5204dc | 2004-12-31 01:16:11 +0000 | [diff] [blame] | 3066 | if (mce->bogusLiterals) |
| 3067 | return expensiveAddSub(mce,False,Ity_I32, |
| 3068 | vatom1,vatom2, atom1,atom2); |
| 3069 | else |
| 3070 | goto cheap_AddSub32; |
| 3071 | |
| 3072 | cheap_AddSub32: |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3073 | case Iop_Mul32: |
sewardj | 992dff9 | 2005-10-07 11:08:55 +0000 | [diff] [blame] | 3074 | return mkLeft32(mce, mkUifU32(mce, vatom1,vatom2)); |
| 3075 | |
sewardj | 463b3d9 | 2005-07-18 11:41:15 +0000 | [diff] [blame] | 3076 | case Iop_CmpORD32S: |
| 3077 | case Iop_CmpORD32U: |
sewardj | 1bc8210 | 2005-12-23 00:16:24 +0000 | [diff] [blame] | 3078 | case Iop_CmpORD64S: |
| 3079 | case Iop_CmpORD64U: |
| 3080 | return doCmpORD(mce, op, vatom1,vatom2, atom1,atom2); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3081 | |
sewardj | 681be30 | 2005-01-15 20:43:58 +0000 | [diff] [blame] | 3082 | case Iop_Add64: |
tom | d9774d7 | 2005-06-27 08:11:01 +0000 | [diff] [blame] | 3083 | if (mce->bogusLiterals) |
| 3084 | return expensiveAddSub(mce,True,Ity_I64, |
| 3085 | vatom1,vatom2, atom1,atom2); |
| 3086 | else |
| 3087 | goto cheap_AddSub64; |
sewardj | 681be30 | 2005-01-15 20:43:58 +0000 | [diff] [blame] | 3088 | case Iop_Sub64: |
tom | d9774d7 | 2005-06-27 08:11:01 +0000 | [diff] [blame] | 3089 | if (mce->bogusLiterals) |
| 3090 | return expensiveAddSub(mce,False,Ity_I64, |
| 3091 | vatom1,vatom2, atom1,atom2); |
| 3092 | else |
| 3093 | goto cheap_AddSub64; |
| 3094 | |
| 3095 | cheap_AddSub64: |
| 3096 | case Iop_Mul64: |
sewardj | 681be30 | 2005-01-15 20:43:58 +0000 | [diff] [blame] | 3097 | return mkLeft64(mce, mkUifU64(mce, vatom1,vatom2)); |
| 3098 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3099 | case Iop_Mul16: |
| 3100 | case Iop_Add16: |
| 3101 | case Iop_Sub16: |
| 3102 | return mkLeft16(mce, mkUifU16(mce, vatom1,vatom2)); |
| 3103 | |
| 3104 | case Iop_Sub8: |
| 3105 | case Iop_Add8: |
| 3106 | return mkLeft8(mce, mkUifU8(mce, vatom1,vatom2)); |
| 3107 | |
sewardj | 69a1332 | 2005-04-23 01:14:51 +0000 | [diff] [blame] | 3108 | case Iop_CmpEQ64: |
sewardj | e6f8af4 | 2005-07-06 18:48:59 +0000 | [diff] [blame] | 3109 | case Iop_CmpNE64: |
sewardj | 69a1332 | 2005-04-23 01:14:51 +0000 | [diff] [blame] | 3110 | if (mce->bogusLiterals) |
| 3111 | return expensiveCmpEQorNE(mce,Ity_I64, vatom1,vatom2, atom1,atom2 ); |
| 3112 | else |
| 3113 | goto cheap_cmp64; |
| 3114 | cheap_cmp64: |
tom | cd98633 | 2005-04-26 07:44:48 +0000 | [diff] [blame] | 3115 | case Iop_CmpLE64S: case Iop_CmpLE64U: |
| 3116 | case Iop_CmpLT64U: case Iop_CmpLT64S: |
sewardj | 69a1332 | 2005-04-23 01:14:51 +0000 | [diff] [blame] | 3117 | return mkPCastTo(mce, Ity_I1, mkUifU64(mce, vatom1,vatom2)); |
| 3118 | |
sewardj | d5204dc | 2004-12-31 01:16:11 +0000 | [diff] [blame] | 3119 | case Iop_CmpEQ32: |
sewardj | e6f8af4 | 2005-07-06 18:48:59 +0000 | [diff] [blame] | 3120 | case Iop_CmpNE32: |
sewardj | d5204dc | 2004-12-31 01:16:11 +0000 | [diff] [blame] | 3121 | if (mce->bogusLiterals) |
| 3122 | return expensiveCmpEQorNE(mce,Ity_I32, vatom1,vatom2, atom1,atom2 ); |
| 3123 | else |
| 3124 | goto cheap_cmp32; |
sewardj | d5204dc | 2004-12-31 01:16:11 +0000 | [diff] [blame] | 3125 | cheap_cmp32: |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3126 | case Iop_CmpLE32S: case Iop_CmpLE32U: |
| 3127 | case Iop_CmpLT32U: case Iop_CmpLT32S: |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3128 | return mkPCastTo(mce, Ity_I1, mkUifU32(mce, vatom1,vatom2)); |
| 3129 | |
| 3130 | case Iop_CmpEQ16: case Iop_CmpNE16: |
| 3131 | return mkPCastTo(mce, Ity_I1, mkUifU16(mce, vatom1,vatom2)); |
| 3132 | |
| 3133 | case Iop_CmpEQ8: case Iop_CmpNE8: |
| 3134 | return mkPCastTo(mce, Ity_I1, mkUifU8(mce, vatom1,vatom2)); |
| 3135 | |
sewardj | afed4c5 | 2009-07-12 13:00:17 +0000 | [diff] [blame] | 3136 | case Iop_CasCmpEQ8: case Iop_CasCmpNE8: |
| 3137 | case Iop_CasCmpEQ16: case Iop_CasCmpNE16: |
| 3138 | case Iop_CasCmpEQ32: case Iop_CasCmpNE32: |
| 3139 | case Iop_CasCmpEQ64: case Iop_CasCmpNE64: |
| 3140 | /* Just say these all produce a defined result, regardless |
| 3141 | of their arguments. See COMMENT_ON_CasCmpEQ in this file. */ |
| 3142 | return assignNew('V', mce, Ity_I1, definedOfType(Ity_I1)); |
| 3143 | |
sewardj | aaddbc2 | 2005-10-07 09:49:53 +0000 | [diff] [blame] | 3144 | case Iop_Shl64: case Iop_Shr64: case Iop_Sar64: |
| 3145 | return scalarShift( mce, Ity_I64, op, vatom1,vatom2, atom1,atom2 ); |
| 3146 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3147 | case Iop_Shl32: case Iop_Shr32: case Iop_Sar32: |
sewardj | aaddbc2 | 2005-10-07 09:49:53 +0000 | [diff] [blame] | 3148 | return scalarShift( mce, Ity_I32, op, vatom1,vatom2, atom1,atom2 ); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3149 | |
sewardj | db67f5f | 2004-12-14 01:15:31 +0000 | [diff] [blame] | 3150 | case Iop_Shl16: case Iop_Shr16: case Iop_Sar16: |
sewardj | aaddbc2 | 2005-10-07 09:49:53 +0000 | [diff] [blame] | 3151 | return scalarShift( mce, Ity_I16, op, vatom1,vatom2, atom1,atom2 ); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3152 | |
| 3153 | case Iop_Shl8: case Iop_Shr8: |
sewardj | aaddbc2 | 2005-10-07 09:49:53 +0000 | [diff] [blame] | 3154 | return scalarShift( mce, Ity_I8, op, vatom1,vatom2, atom1,atom2 ); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3155 | |
sewardj | 20d38f2 | 2005-02-07 23:50:18 +0000 | [diff] [blame] | 3156 | case Iop_AndV128: |
| 3157 | uifu = mkUifUV128; difd = mkDifDV128; |
| 3158 | and_or_ty = Ity_V128; improve = mkImproveANDV128; goto do_And_Or; |
sewardj | 7010f6e | 2004-12-10 13:35:22 +0000 | [diff] [blame] | 3159 | case Iop_And64: |
| 3160 | uifu = mkUifU64; difd = mkDifD64; |
| 3161 | and_or_ty = Ity_I64; improve = mkImproveAND64; goto do_And_Or; |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3162 | case Iop_And32: |
| 3163 | uifu = mkUifU32; difd = mkDifD32; |
| 3164 | and_or_ty = Ity_I32; improve = mkImproveAND32; goto do_And_Or; |
| 3165 | case Iop_And16: |
| 3166 | uifu = mkUifU16; difd = mkDifD16; |
| 3167 | and_or_ty = Ity_I16; improve = mkImproveAND16; goto do_And_Or; |
| 3168 | case Iop_And8: |
| 3169 | uifu = mkUifU8; difd = mkDifD8; |
| 3170 | and_or_ty = Ity_I8; improve = mkImproveAND8; goto do_And_Or; |
| 3171 | |
sewardj | 20d38f2 | 2005-02-07 23:50:18 +0000 | [diff] [blame] | 3172 | case Iop_OrV128: |
| 3173 | uifu = mkUifUV128; difd = mkDifDV128; |
| 3174 | and_or_ty = Ity_V128; improve = mkImproveORV128; goto do_And_Or; |
sewardj | 7010f6e | 2004-12-10 13:35:22 +0000 | [diff] [blame] | 3175 | case Iop_Or64: |
| 3176 | uifu = mkUifU64; difd = mkDifD64; |
| 3177 | and_or_ty = Ity_I64; improve = mkImproveOR64; goto do_And_Or; |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3178 | case Iop_Or32: |
| 3179 | uifu = mkUifU32; difd = mkDifD32; |
| 3180 | and_or_ty = Ity_I32; improve = mkImproveOR32; goto do_And_Or; |
| 3181 | case Iop_Or16: |
| 3182 | uifu = mkUifU16; difd = mkDifD16; |
| 3183 | and_or_ty = Ity_I16; improve = mkImproveOR16; goto do_And_Or; |
| 3184 | case Iop_Or8: |
| 3185 | uifu = mkUifU8; difd = mkDifD8; |
| 3186 | and_or_ty = Ity_I8; improve = mkImproveOR8; goto do_And_Or; |
| 3187 | |
| 3188 | do_And_Or: |
| 3189 | return |
| 3190 | assignNew( |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 3191 | 'V', mce, |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3192 | and_or_ty, |
| 3193 | difd(mce, uifu(mce, vatom1, vatom2), |
| 3194 | difd(mce, improve(mce, atom1, vatom1), |
| 3195 | improve(mce, atom2, vatom2) ) ) ); |
| 3196 | |
| 3197 | case Iop_Xor8: |
| 3198 | return mkUifU8(mce, vatom1, vatom2); |
| 3199 | case Iop_Xor16: |
| 3200 | return mkUifU16(mce, vatom1, vatom2); |
| 3201 | case Iop_Xor32: |
| 3202 | return mkUifU32(mce, vatom1, vatom2); |
sewardj | 7010f6e | 2004-12-10 13:35:22 +0000 | [diff] [blame] | 3203 | case Iop_Xor64: |
| 3204 | return mkUifU64(mce, vatom1, vatom2); |
sewardj | 20d38f2 | 2005-02-07 23:50:18 +0000 | [diff] [blame] | 3205 | case Iop_XorV128: |
| 3206 | return mkUifUV128(mce, vatom1, vatom2); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3207 | |
| 3208 | default: |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3209 | ppIROp(op); |
| 3210 | VG_(tool_panic)("memcheck:expr2vbits_Binop"); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3211 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3212 | } |
| 3213 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3214 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3215 | static |
| 3216 | IRExpr* expr2vbits_Unop ( MCEnv* mce, IROp op, IRAtom* atom ) |
| 3217 | { |
| 3218 | IRAtom* vatom = expr2vbits( mce, atom ); |
| 3219 | tl_assert(isOriginalAtom(mce,atom)); |
| 3220 | switch (op) { |
| 3221 | |
sewardj | 0b07059 | 2004-12-10 21:44:22 +0000 | [diff] [blame] | 3222 | case Iop_Sqrt64Fx2: |
| 3223 | return unary64Fx2(mce, vatom); |
| 3224 | |
| 3225 | case Iop_Sqrt64F0x2: |
| 3226 | return unary64F0x2(mce, vatom); |
| 3227 | |
sewardj | 170ee21 | 2004-12-10 18:57:51 +0000 | [diff] [blame] | 3228 | case Iop_Sqrt32Fx4: |
| 3229 | case Iop_RSqrt32Fx4: |
| 3230 | case Iop_Recip32Fx4: |
cerion | 176cb4c | 2005-11-16 17:21:49 +0000 | [diff] [blame] | 3231 | case Iop_I32UtoFx4: |
| 3232 | case Iop_I32StoFx4: |
| 3233 | case Iop_QFtoI32Ux4_RZ: |
| 3234 | case Iop_QFtoI32Sx4_RZ: |
| 3235 | case Iop_RoundF32x4_RM: |
| 3236 | case Iop_RoundF32x4_RP: |
| 3237 | case Iop_RoundF32x4_RN: |
| 3238 | case Iop_RoundF32x4_RZ: |
sewardj | 57f92b0 | 2010-08-22 11:54:14 +0000 | [diff] [blame] | 3239 | case Iop_Recip32x4: |
| 3240 | case Iop_Abs32Fx4: |
| 3241 | case Iop_Neg32Fx4: |
| 3242 | case Iop_Rsqrte32Fx4: |
sewardj | 170ee21 | 2004-12-10 18:57:51 +0000 | [diff] [blame] | 3243 | return unary32Fx4(mce, vatom); |
| 3244 | |
sewardj | 57f92b0 | 2010-08-22 11:54:14 +0000 | [diff] [blame] | 3245 | case Iop_I32UtoFx2: |
| 3246 | case Iop_I32StoFx2: |
| 3247 | case Iop_Recip32Fx2: |
| 3248 | case Iop_Recip32x2: |
| 3249 | case Iop_Abs32Fx2: |
| 3250 | case Iop_Neg32Fx2: |
| 3251 | case Iop_Rsqrte32Fx2: |
| 3252 | return unary32Fx2(mce, vatom); |
| 3253 | |
sewardj | 170ee21 | 2004-12-10 18:57:51 +0000 | [diff] [blame] | 3254 | case Iop_Sqrt32F0x4: |
| 3255 | case Iop_RSqrt32F0x4: |
| 3256 | case Iop_Recip32F0x4: |
| 3257 | return unary32F0x4(mce, vatom); |
| 3258 | |
sewardj | 20d38f2 | 2005-02-07 23:50:18 +0000 | [diff] [blame] | 3259 | case Iop_32UtoV128: |
| 3260 | case Iop_64UtoV128: |
sewardj | 620eb5b | 2005-10-22 12:50:43 +0000 | [diff] [blame] | 3261 | case Iop_Dup8x16: |
| 3262 | case Iop_Dup16x8: |
| 3263 | case Iop_Dup32x4: |
sewardj | 57f92b0 | 2010-08-22 11:54:14 +0000 | [diff] [blame] | 3264 | case Iop_Reverse16_8x16: |
| 3265 | case Iop_Reverse32_8x16: |
| 3266 | case Iop_Reverse32_16x8: |
| 3267 | case Iop_Reverse64_8x16: |
| 3268 | case Iop_Reverse64_16x8: |
| 3269 | case Iop_Reverse64_32x4: |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 3270 | return assignNew('V', mce, Ity_V128, unop(op, vatom)); |
sewardj | 170ee21 | 2004-12-10 18:57:51 +0000 | [diff] [blame] | 3271 | |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 3272 | case Iop_F128HItoF64: /* F128 -> high half of F128 */ |
| 3273 | return assignNew('V', mce, Ity_I64, unop(Iop_128HIto64, vatom)); |
| 3274 | case Iop_F128LOtoF64: /* F128 -> low half of F128 */ |
| 3275 | return assignNew('V', mce, Ity_I64, unop(Iop_128to64, vatom)); |
| 3276 | |
| 3277 | case Iop_NegF128: |
| 3278 | case Iop_AbsF128: |
| 3279 | return mkPCastTo(mce, Ity_I128, vatom); |
| 3280 | |
| 3281 | case Iop_I32StoF128: /* signed I32 -> F128 */ |
| 3282 | case Iop_I64StoF128: /* signed I64 -> F128 */ |
| 3283 | case Iop_F32toF128: /* F32 -> F128 */ |
| 3284 | case Iop_F64toF128: /* F64 -> F128 */ |
| 3285 | return mkPCastTo(mce, Ity_I128, vatom); |
| 3286 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3287 | case Iop_F32toF64: |
sewardj | 06f96d0 | 2009-12-31 19:24:12 +0000 | [diff] [blame] | 3288 | case Iop_I32StoF64: |
sewardj | 59570ff | 2010-01-01 11:59:33 +0000 | [diff] [blame] | 3289 | case Iop_I32UtoF64: |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3290 | case Iop_NegF64: |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3291 | case Iop_AbsF64: |
sewardj | 4f3e0f2 | 2006-01-27 15:11:31 +0000 | [diff] [blame] | 3292 | case Iop_Est5FRSqrt: |
sewardj | dead90a | 2008-08-08 08:38:23 +0000 | [diff] [blame] | 3293 | case Iop_RoundF64toF64_NEAREST: |
| 3294 | case Iop_RoundF64toF64_NegINF: |
| 3295 | case Iop_RoundF64toF64_PosINF: |
| 3296 | case Iop_RoundF64toF64_ZERO: |
sewardj | 39cc735 | 2005-06-09 21:31:55 +0000 | [diff] [blame] | 3297 | case Iop_Clz64: |
| 3298 | case Iop_Ctz64: |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3299 | return mkPCastTo(mce, Ity_I64, vatom); |
| 3300 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3301 | case Iop_Clz32: |
| 3302 | case Iop_Ctz32: |
sewardj | ed69fdb | 2006-02-03 16:12:27 +0000 | [diff] [blame] | 3303 | case Iop_TruncF64asF32: |
sewardj | 59570ff | 2010-01-01 11:59:33 +0000 | [diff] [blame] | 3304 | case Iop_NegF32: |
| 3305 | case Iop_AbsF32: |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3306 | return mkPCastTo(mce, Ity_I32, vatom); |
| 3307 | |
sewardj | d9dbc19 | 2005-04-27 11:40:27 +0000 | [diff] [blame] | 3308 | case Iop_1Uto64: |
| 3309 | case Iop_8Uto64: |
| 3310 | case Iop_8Sto64: |
| 3311 | case Iop_16Uto64: |
| 3312 | case Iop_16Sto64: |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3313 | case Iop_32Sto64: |
| 3314 | case Iop_32Uto64: |
sewardj | 20d38f2 | 2005-02-07 23:50:18 +0000 | [diff] [blame] | 3315 | case Iop_V128to64: |
| 3316 | case Iop_V128HIto64: |
sewardj | 6cf40ff | 2005-04-20 22:31:26 +0000 | [diff] [blame] | 3317 | case Iop_128HIto64: |
| 3318 | case Iop_128to64: |
sewardj | 57f92b0 | 2010-08-22 11:54:14 +0000 | [diff] [blame] | 3319 | case Iop_Dup8x8: |
| 3320 | case Iop_Dup16x4: |
| 3321 | case Iop_Dup32x2: |
| 3322 | case Iop_Reverse16_8x8: |
| 3323 | case Iop_Reverse32_8x8: |
| 3324 | case Iop_Reverse32_16x4: |
| 3325 | case Iop_Reverse64_8x8: |
| 3326 | case Iop_Reverse64_16x4: |
| 3327 | case Iop_Reverse64_32x2: |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 3328 | return assignNew('V', mce, Ity_I64, unop(op, vatom)); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3329 | |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 3330 | case Iop_I16StoF32: |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3331 | case Iop_64to32: |
| 3332 | case Iop_64HIto32: |
| 3333 | case Iop_1Uto32: |
sewardj | 463b3d9 | 2005-07-18 11:41:15 +0000 | [diff] [blame] | 3334 | case Iop_1Sto32: |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3335 | case Iop_8Uto32: |
| 3336 | case Iop_16Uto32: |
| 3337 | case Iop_16Sto32: |
| 3338 | case Iop_8Sto32: |
cerion | fafaa0d | 2005-09-12 22:29:38 +0000 | [diff] [blame] | 3339 | case Iop_V128to32: |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 3340 | return assignNew('V', mce, Ity_I32, unop(op, vatom)); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3341 | |
| 3342 | case Iop_8Sto16: |
| 3343 | case Iop_8Uto16: |
| 3344 | case Iop_32to16: |
| 3345 | case Iop_32HIto16: |
sewardj | d9dbc19 | 2005-04-27 11:40:27 +0000 | [diff] [blame] | 3346 | case Iop_64to16: |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 3347 | return assignNew('V', mce, Ity_I16, unop(op, vatom)); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3348 | |
| 3349 | case Iop_1Uto8: |
| 3350 | case Iop_16to8: |
sewardj | 9a807e0 | 2006-12-17 14:20:31 +0000 | [diff] [blame] | 3351 | case Iop_16HIto8: |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3352 | case Iop_32to8: |
sewardj | d9dbc19 | 2005-04-27 11:40:27 +0000 | [diff] [blame] | 3353 | case Iop_64to8: |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 3354 | return assignNew('V', mce, Ity_I8, unop(op, vatom)); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3355 | |
| 3356 | case Iop_32to1: |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 3357 | return assignNew('V', mce, Ity_I1, unop(Iop_32to1, vatom)); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3358 | |
sewardj | d9dbc19 | 2005-04-27 11:40:27 +0000 | [diff] [blame] | 3359 | case Iop_64to1: |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 3360 | return assignNew('V', mce, Ity_I1, unop(Iop_64to1, vatom)); |
sewardj | d9dbc19 | 2005-04-27 11:40:27 +0000 | [diff] [blame] | 3361 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3362 | case Iop_ReinterpF64asI64: |
| 3363 | case Iop_ReinterpI64asF64: |
sewardj | 0b07059 | 2004-12-10 21:44:22 +0000 | [diff] [blame] | 3364 | case Iop_ReinterpI32asF32: |
sewardj | 59570ff | 2010-01-01 11:59:33 +0000 | [diff] [blame] | 3365 | case Iop_ReinterpF32asI32: |
sewardj | 20d38f2 | 2005-02-07 23:50:18 +0000 | [diff] [blame] | 3366 | case Iop_NotV128: |
sewardj | 7010f6e | 2004-12-10 13:35:22 +0000 | [diff] [blame] | 3367 | case Iop_Not64: |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3368 | case Iop_Not32: |
| 3369 | case Iop_Not16: |
| 3370 | case Iop_Not8: |
| 3371 | case Iop_Not1: |
| 3372 | return vatom; |
sewardj | 7010f6e | 2004-12-10 13:35:22 +0000 | [diff] [blame] | 3373 | |
sewardj | 57f92b0 | 2010-08-22 11:54:14 +0000 | [diff] [blame] | 3374 | case Iop_CmpNEZ8x8: |
| 3375 | case Iop_Cnt8x8: |
| 3376 | case Iop_Clz8Sx8: |
| 3377 | case Iop_Cls8Sx8: |
| 3378 | case Iop_Abs8x8: |
| 3379 | return mkPCast8x8(mce, vatom); |
| 3380 | |
| 3381 | case Iop_CmpNEZ8x16: |
| 3382 | case Iop_Cnt8x16: |
| 3383 | case Iop_Clz8Sx16: |
| 3384 | case Iop_Cls8Sx16: |
| 3385 | case Iop_Abs8x16: |
| 3386 | return mkPCast8x16(mce, vatom); |
| 3387 | |
| 3388 | case Iop_CmpNEZ16x4: |
| 3389 | case Iop_Clz16Sx4: |
| 3390 | case Iop_Cls16Sx4: |
| 3391 | case Iop_Abs16x4: |
| 3392 | return mkPCast16x4(mce, vatom); |
| 3393 | |
| 3394 | case Iop_CmpNEZ16x8: |
| 3395 | case Iop_Clz16Sx8: |
| 3396 | case Iop_Cls16Sx8: |
| 3397 | case Iop_Abs16x8: |
| 3398 | return mkPCast16x8(mce, vatom); |
| 3399 | |
| 3400 | case Iop_CmpNEZ32x2: |
| 3401 | case Iop_Clz32Sx2: |
| 3402 | case Iop_Cls32Sx2: |
| 3403 | case Iop_FtoI32Ux2_RZ: |
| 3404 | case Iop_FtoI32Sx2_RZ: |
| 3405 | case Iop_Abs32x2: |
| 3406 | return mkPCast32x2(mce, vatom); |
| 3407 | |
| 3408 | case Iop_CmpNEZ32x4: |
| 3409 | case Iop_Clz32Sx4: |
| 3410 | case Iop_Cls32Sx4: |
| 3411 | case Iop_FtoI32Ux4_RZ: |
| 3412 | case Iop_FtoI32Sx4_RZ: |
| 3413 | case Iop_Abs32x4: |
| 3414 | return mkPCast32x4(mce, vatom); |
| 3415 | |
| 3416 | case Iop_CmpwNEZ64: |
| 3417 | return mkPCastTo(mce, Ity_I64, vatom); |
| 3418 | |
| 3419 | case Iop_CmpNEZ64x2: |
| 3420 | return mkPCast64x2(mce, vatom); |
| 3421 | |
sewardj | 7ee7d85 | 2011-06-16 11:37:21 +0000 | [diff] [blame^] | 3422 | case Iop_NarrowUn16to8x8: |
| 3423 | case Iop_NarrowUn32to16x4: |
| 3424 | case Iop_NarrowUn64to32x2: |
| 3425 | case Iop_QNarrowUn16Sto8Sx8: |
| 3426 | case Iop_QNarrowUn16Sto8Ux8: |
| 3427 | case Iop_QNarrowUn16Uto8Ux8: |
| 3428 | case Iop_QNarrowUn32Sto16Sx4: |
| 3429 | case Iop_QNarrowUn32Sto16Ux4: |
| 3430 | case Iop_QNarrowUn32Uto16Ux4: |
| 3431 | case Iop_QNarrowUn64Sto32Sx2: |
| 3432 | case Iop_QNarrowUn64Sto32Ux2: |
| 3433 | case Iop_QNarrowUn64Uto32Ux2: |
| 3434 | return vectorNarrowUnV128(mce, op, vatom); |
sewardj | 57f92b0 | 2010-08-22 11:54:14 +0000 | [diff] [blame] | 3435 | |
sewardj | 7ee7d85 | 2011-06-16 11:37:21 +0000 | [diff] [blame^] | 3436 | case Iop_Widen8Sto16x8: |
| 3437 | case Iop_Widen8Uto16x8: |
| 3438 | case Iop_Widen16Sto32x4: |
| 3439 | case Iop_Widen16Uto32x4: |
| 3440 | case Iop_Widen32Sto64x2: |
| 3441 | case Iop_Widen32Uto64x2: |
| 3442 | return vectorWidenI64(mce, op, vatom); |
sewardj | 57f92b0 | 2010-08-22 11:54:14 +0000 | [diff] [blame] | 3443 | |
| 3444 | case Iop_PwAddL32Ux2: |
| 3445 | case Iop_PwAddL32Sx2: |
| 3446 | return mkPCastTo(mce, Ity_I64, |
| 3447 | assignNew('V', mce, Ity_I64, unop(op, mkPCast32x2(mce, vatom)))); |
| 3448 | |
| 3449 | case Iop_PwAddL16Ux4: |
| 3450 | case Iop_PwAddL16Sx4: |
| 3451 | return mkPCast32x2(mce, |
| 3452 | assignNew('V', mce, Ity_I64, unop(op, mkPCast16x4(mce, vatom)))); |
| 3453 | |
| 3454 | case Iop_PwAddL8Ux8: |
| 3455 | case Iop_PwAddL8Sx8: |
| 3456 | return mkPCast16x4(mce, |
| 3457 | assignNew('V', mce, Ity_I64, unop(op, mkPCast8x8(mce, vatom)))); |
| 3458 | |
| 3459 | case Iop_PwAddL32Ux4: |
| 3460 | case Iop_PwAddL32Sx4: |
| 3461 | return mkPCast64x2(mce, |
| 3462 | assignNew('V', mce, Ity_V128, unop(op, mkPCast32x4(mce, vatom)))); |
| 3463 | |
| 3464 | case Iop_PwAddL16Ux8: |
| 3465 | case Iop_PwAddL16Sx8: |
| 3466 | return mkPCast32x4(mce, |
| 3467 | assignNew('V', mce, Ity_V128, unop(op, mkPCast16x8(mce, vatom)))); |
| 3468 | |
| 3469 | case Iop_PwAddL8Ux16: |
| 3470 | case Iop_PwAddL8Sx16: |
| 3471 | return mkPCast16x8(mce, |
| 3472 | assignNew('V', mce, Ity_V128, unop(op, mkPCast8x16(mce, vatom)))); |
| 3473 | |
sewardj | f34eb49 | 2011-04-15 11:57:05 +0000 | [diff] [blame] | 3474 | case Iop_I64UtoF32: |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3475 | default: |
| 3476 | ppIROp(op); |
| 3477 | VG_(tool_panic)("memcheck:expr2vbits_Unop"); |
| 3478 | } |
| 3479 | } |
| 3480 | |
| 3481 | |
sewardj | 170ee21 | 2004-12-10 18:57:51 +0000 | [diff] [blame] | 3482 | /* Worker function; do not call directly. */ |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3483 | static |
sewardj | 2e59585 | 2005-06-30 23:33:37 +0000 | [diff] [blame] | 3484 | IRAtom* expr2vbits_Load_WRK ( MCEnv* mce, |
| 3485 | IREndness end, IRType ty, |
| 3486 | IRAtom* addr, UInt bias ) |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3487 | { |
| 3488 | void* helper; |
| 3489 | Char* hname; |
| 3490 | IRDirty* di; |
| 3491 | IRTemp datavbits; |
| 3492 | IRAtom* addrAct; |
| 3493 | |
| 3494 | tl_assert(isOriginalAtom(mce,addr)); |
sewardj | 2e59585 | 2005-06-30 23:33:37 +0000 | [diff] [blame] | 3495 | tl_assert(end == Iend_LE || end == Iend_BE); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3496 | |
| 3497 | /* First, emit a definedness test for the address. This also sets |
| 3498 | the address (shadow) to 'defined' following the test. */ |
| 3499 | complainIfUndefined( mce, addr ); |
| 3500 | |
| 3501 | /* Now cook up a call to the relevant helper function, to read the |
| 3502 | data V bits from shadow memory. */ |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 3503 | ty = shadowTypeV(ty); |
sewardj | 2e59585 | 2005-06-30 23:33:37 +0000 | [diff] [blame] | 3504 | |
| 3505 | if (end == Iend_LE) { |
| 3506 | switch (ty) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3507 | case Ity_I64: helper = &MC_(helperc_LOADV64le); |
| 3508 | hname = "MC_(helperc_LOADV64le)"; |
sewardj | 2e59585 | 2005-06-30 23:33:37 +0000 | [diff] [blame] | 3509 | break; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3510 | case Ity_I32: helper = &MC_(helperc_LOADV32le); |
| 3511 | hname = "MC_(helperc_LOADV32le)"; |
sewardj | 2e59585 | 2005-06-30 23:33:37 +0000 | [diff] [blame] | 3512 | break; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3513 | case Ity_I16: helper = &MC_(helperc_LOADV16le); |
| 3514 | hname = "MC_(helperc_LOADV16le)"; |
sewardj | 2e59585 | 2005-06-30 23:33:37 +0000 | [diff] [blame] | 3515 | break; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3516 | case Ity_I8: helper = &MC_(helperc_LOADV8); |
| 3517 | hname = "MC_(helperc_LOADV8)"; |
sewardj | 2e59585 | 2005-06-30 23:33:37 +0000 | [diff] [blame] | 3518 | break; |
| 3519 | default: ppIRType(ty); |
| 3520 | VG_(tool_panic)("memcheck:do_shadow_Load(LE)"); |
| 3521 | } |
| 3522 | } else { |
sewardj | 8cf88b7 | 2005-07-08 01:29:33 +0000 | [diff] [blame] | 3523 | switch (ty) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3524 | case Ity_I64: helper = &MC_(helperc_LOADV64be); |
| 3525 | hname = "MC_(helperc_LOADV64be)"; |
sewardj | 8cf88b7 | 2005-07-08 01:29:33 +0000 | [diff] [blame] | 3526 | break; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3527 | case Ity_I32: helper = &MC_(helperc_LOADV32be); |
| 3528 | hname = "MC_(helperc_LOADV32be)"; |
sewardj | 8cf88b7 | 2005-07-08 01:29:33 +0000 | [diff] [blame] | 3529 | break; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3530 | case Ity_I16: helper = &MC_(helperc_LOADV16be); |
| 3531 | hname = "MC_(helperc_LOADV16be)"; |
sewardj | 8cf88b7 | 2005-07-08 01:29:33 +0000 | [diff] [blame] | 3532 | break; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3533 | case Ity_I8: helper = &MC_(helperc_LOADV8); |
| 3534 | hname = "MC_(helperc_LOADV8)"; |
sewardj | 8cf88b7 | 2005-07-08 01:29:33 +0000 | [diff] [blame] | 3535 | break; |
| 3536 | default: ppIRType(ty); |
| 3537 | VG_(tool_panic)("memcheck:do_shadow_Load(BE)"); |
| 3538 | } |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3539 | } |
| 3540 | |
| 3541 | /* Generate the actual address into addrAct. */ |
| 3542 | if (bias == 0) { |
| 3543 | addrAct = addr; |
| 3544 | } else { |
sewardj | 7cf97ee | 2004-11-28 14:25:01 +0000 | [diff] [blame] | 3545 | IROp mkAdd; |
| 3546 | IRAtom* eBias; |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3547 | IRType tyAddr = mce->hWordTy; |
| 3548 | tl_assert( tyAddr == Ity_I32 || tyAddr == Ity_I64 ); |
sewardj | 7cf97ee | 2004-11-28 14:25:01 +0000 | [diff] [blame] | 3549 | mkAdd = tyAddr==Ity_I32 ? Iop_Add32 : Iop_Add64; |
| 3550 | eBias = tyAddr==Ity_I32 ? mkU32(bias) : mkU64(bias); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 3551 | addrAct = assignNew('V', mce, tyAddr, binop(mkAdd, addr, eBias) ); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3552 | } |
| 3553 | |
| 3554 | /* We need to have a place to park the V bits we're just about to |
| 3555 | read. */ |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 3556 | datavbits = newTemp(mce, ty, VSh); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3557 | di = unsafeIRDirty_1_N( datavbits, |
sewardj | 53ee1fc | 2005-12-23 02:29:58 +0000 | [diff] [blame] | 3558 | 1/*regparms*/, |
| 3559 | hname, VG_(fnptr_to_fnentry)( helper ), |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3560 | mkIRExprVec_1( addrAct )); |
| 3561 | setHelperAnns( mce, di ); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 3562 | stmt( 'V', mce, IRStmt_Dirty(di) ); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3563 | |
| 3564 | return mkexpr(datavbits); |
| 3565 | } |
| 3566 | |
| 3567 | |
| 3568 | static |
sewardj | 2e59585 | 2005-06-30 23:33:37 +0000 | [diff] [blame] | 3569 | IRAtom* expr2vbits_Load ( MCEnv* mce, |
| 3570 | IREndness end, IRType ty, |
| 3571 | IRAtom* addr, UInt bias ) |
sewardj | 170ee21 | 2004-12-10 18:57:51 +0000 | [diff] [blame] | 3572 | { |
| 3573 | IRAtom *v64hi, *v64lo; |
sewardj | 2e59585 | 2005-06-30 23:33:37 +0000 | [diff] [blame] | 3574 | tl_assert(end == Iend_LE || end == Iend_BE); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 3575 | switch (shadowTypeV(ty)) { |
sewardj | 170ee21 | 2004-12-10 18:57:51 +0000 | [diff] [blame] | 3576 | case Ity_I8: |
| 3577 | case Ity_I16: |
| 3578 | case Ity_I32: |
| 3579 | case Ity_I64: |
sewardj | 2e59585 | 2005-06-30 23:33:37 +0000 | [diff] [blame] | 3580 | return expr2vbits_Load_WRK(mce, end, ty, addr, bias); |
sewardj | 170ee21 | 2004-12-10 18:57:51 +0000 | [diff] [blame] | 3581 | case Ity_V128: |
sewardj | 2e59585 | 2005-06-30 23:33:37 +0000 | [diff] [blame] | 3582 | if (end == Iend_LE) { |
| 3583 | v64lo = expr2vbits_Load_WRK(mce, end, Ity_I64, addr, bias); |
| 3584 | v64hi = expr2vbits_Load_WRK(mce, end, Ity_I64, addr, bias+8); |
| 3585 | } else { |
sewardj | 2e59585 | 2005-06-30 23:33:37 +0000 | [diff] [blame] | 3586 | v64hi = expr2vbits_Load_WRK(mce, end, Ity_I64, addr, bias); |
| 3587 | v64lo = expr2vbits_Load_WRK(mce, end, Ity_I64, addr, bias+8); |
| 3588 | } |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 3589 | return assignNew( 'V', mce, |
sewardj | 170ee21 | 2004-12-10 18:57:51 +0000 | [diff] [blame] | 3590 | Ity_V128, |
sewardj | 20d38f2 | 2005-02-07 23:50:18 +0000 | [diff] [blame] | 3591 | binop(Iop_64HLtoV128, v64hi, v64lo)); |
sewardj | 170ee21 | 2004-12-10 18:57:51 +0000 | [diff] [blame] | 3592 | default: |
sewardj | 2e59585 | 2005-06-30 23:33:37 +0000 | [diff] [blame] | 3593 | VG_(tool_panic)("expr2vbits_Load"); |
sewardj | 170ee21 | 2004-12-10 18:57:51 +0000 | [diff] [blame] | 3594 | } |
| 3595 | } |
| 3596 | |
| 3597 | |
| 3598 | static |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3599 | IRAtom* expr2vbits_Mux0X ( MCEnv* mce, |
| 3600 | IRAtom* cond, IRAtom* expr0, IRAtom* exprX ) |
| 3601 | { |
| 3602 | IRAtom *vbitsC, *vbits0, *vbitsX; |
| 3603 | IRType ty; |
| 3604 | /* Given Mux0X(cond,expr0,exprX), generate |
| 3605 | Mux0X(cond,expr0#,exprX#) `UifU` PCast(cond#) |
| 3606 | That is, steer the V bits like the originals, but trash the |
| 3607 | result if the steering value is undefined. This gives |
| 3608 | lazy propagation. */ |
| 3609 | tl_assert(isOriginalAtom(mce, cond)); |
| 3610 | tl_assert(isOriginalAtom(mce, expr0)); |
| 3611 | tl_assert(isOriginalAtom(mce, exprX)); |
| 3612 | |
| 3613 | vbitsC = expr2vbits(mce, cond); |
| 3614 | vbits0 = expr2vbits(mce, expr0); |
| 3615 | vbitsX = expr2vbits(mce, exprX); |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 3616 | ty = typeOfIRExpr(mce->sb->tyenv, vbits0); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3617 | |
| 3618 | return |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 3619 | mkUifU(mce, ty, assignNew('V', mce, ty, |
| 3620 | IRExpr_Mux0X(cond, vbits0, vbitsX)), |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3621 | mkPCastTo(mce, ty, vbitsC) ); |
| 3622 | } |
| 3623 | |
| 3624 | /* --------- This is the main expression-handling function. --------- */ |
| 3625 | |
| 3626 | static |
| 3627 | IRExpr* expr2vbits ( MCEnv* mce, IRExpr* e ) |
| 3628 | { |
| 3629 | switch (e->tag) { |
| 3630 | |
| 3631 | case Iex_Get: |
| 3632 | return shadow_GET( mce, e->Iex.Get.offset, e->Iex.Get.ty ); |
| 3633 | |
| 3634 | case Iex_GetI: |
| 3635 | return shadow_GETI( mce, e->Iex.GetI.descr, |
| 3636 | e->Iex.GetI.ix, e->Iex.GetI.bias ); |
| 3637 | |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 3638 | case Iex_RdTmp: |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 3639 | return IRExpr_RdTmp( findShadowTmpV(mce, e->Iex.RdTmp.tmp) ); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3640 | |
| 3641 | case Iex_Const: |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 3642 | return definedOfType(shadowTypeV(typeOfIRExpr(mce->sb->tyenv, e))); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3643 | |
sewardj | e91cea7 | 2006-02-08 19:32:02 +0000 | [diff] [blame] | 3644 | case Iex_Qop: |
| 3645 | return expr2vbits_Qop( |
| 3646 | mce, |
| 3647 | e->Iex.Qop.op, |
| 3648 | e->Iex.Qop.arg1, e->Iex.Qop.arg2, |
| 3649 | e->Iex.Qop.arg3, e->Iex.Qop.arg4 |
| 3650 | ); |
| 3651 | |
sewardj | ed69fdb | 2006-02-03 16:12:27 +0000 | [diff] [blame] | 3652 | case Iex_Triop: |
| 3653 | return expr2vbits_Triop( |
| 3654 | mce, |
| 3655 | e->Iex.Triop.op, |
| 3656 | e->Iex.Triop.arg1, e->Iex.Triop.arg2, e->Iex.Triop.arg3 |
| 3657 | ); |
| 3658 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3659 | case Iex_Binop: |
| 3660 | return expr2vbits_Binop( |
| 3661 | mce, |
| 3662 | e->Iex.Binop.op, |
| 3663 | e->Iex.Binop.arg1, e->Iex.Binop.arg2 |
| 3664 | ); |
| 3665 | |
| 3666 | case Iex_Unop: |
| 3667 | return expr2vbits_Unop( mce, e->Iex.Unop.op, e->Iex.Unop.arg ); |
| 3668 | |
sewardj | 2e59585 | 2005-06-30 23:33:37 +0000 | [diff] [blame] | 3669 | case Iex_Load: |
| 3670 | return expr2vbits_Load( mce, e->Iex.Load.end, |
| 3671 | e->Iex.Load.ty, |
| 3672 | e->Iex.Load.addr, 0/*addr bias*/ ); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3673 | |
| 3674 | case Iex_CCall: |
| 3675 | return mkLazyN( mce, e->Iex.CCall.args, |
| 3676 | e->Iex.CCall.retty, |
| 3677 | e->Iex.CCall.cee ); |
| 3678 | |
| 3679 | case Iex_Mux0X: |
| 3680 | return expr2vbits_Mux0X( mce, e->Iex.Mux0X.cond, e->Iex.Mux0X.expr0, |
| 3681 | e->Iex.Mux0X.exprX); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3682 | |
| 3683 | default: |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3684 | VG_(printf)("\n"); |
| 3685 | ppIRExpr(e); |
| 3686 | VG_(printf)("\n"); |
| 3687 | VG_(tool_panic)("memcheck: expr2vbits"); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3688 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3689 | } |
| 3690 | |
| 3691 | /*------------------------------------------------------------*/ |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3692 | /*--- Generate shadow stmts from all kinds of IRStmts. ---*/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3693 | /*------------------------------------------------------------*/ |
| 3694 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3695 | /* Widen a value to the host word size. */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3696 | |
| 3697 | static |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3698 | IRExpr* zwidenToHostWord ( MCEnv* mce, IRAtom* vatom ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3699 | { |
sewardj | 7cf97ee | 2004-11-28 14:25:01 +0000 | [diff] [blame] | 3700 | IRType ty, tyH; |
| 3701 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3702 | /* vatom is vbits-value and as such can only have a shadow type. */ |
| 3703 | tl_assert(isShadowAtom(mce,vatom)); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3704 | |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 3705 | ty = typeOfIRExpr(mce->sb->tyenv, vatom); |
sewardj | 7cf97ee | 2004-11-28 14:25:01 +0000 | [diff] [blame] | 3706 | tyH = mce->hWordTy; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3707 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3708 | if (tyH == Ity_I32) { |
| 3709 | switch (ty) { |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 3710 | case Ity_I32: |
| 3711 | return vatom; |
| 3712 | case Ity_I16: |
| 3713 | return assignNew('V', mce, tyH, unop(Iop_16Uto32, vatom)); |
| 3714 | case Ity_I8: |
| 3715 | return assignNew('V', mce, tyH, unop(Iop_8Uto32, vatom)); |
| 3716 | default: |
| 3717 | goto unhandled; |
sewardj | 8ec2cfc | 2002-10-13 00:57:26 +0000 | [diff] [blame] | 3718 | } |
sewardj | 6cf40ff | 2005-04-20 22:31:26 +0000 | [diff] [blame] | 3719 | } else |
| 3720 | if (tyH == Ity_I64) { |
| 3721 | switch (ty) { |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 3722 | case Ity_I32: |
| 3723 | return assignNew('V', mce, tyH, unop(Iop_32Uto64, vatom)); |
| 3724 | case Ity_I16: |
| 3725 | return assignNew('V', mce, tyH, unop(Iop_32Uto64, |
| 3726 | assignNew('V', mce, Ity_I32, unop(Iop_16Uto32, vatom)))); |
| 3727 | case Ity_I8: |
| 3728 | return assignNew('V', mce, tyH, unop(Iop_32Uto64, |
| 3729 | assignNew('V', mce, Ity_I32, unop(Iop_8Uto32, vatom)))); |
| 3730 | default: |
| 3731 | goto unhandled; |
sewardj | 6cf40ff | 2005-04-20 22:31:26 +0000 | [diff] [blame] | 3732 | } |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3733 | } else { |
| 3734 | goto unhandled; |
sewardj | 8ec2cfc | 2002-10-13 00:57:26 +0000 | [diff] [blame] | 3735 | } |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3736 | unhandled: |
| 3737 | VG_(printf)("\nty = "); ppIRType(ty); VG_(printf)("\n"); |
| 3738 | VG_(tool_panic)("zwidenToHostWord"); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3739 | } |
| 3740 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3741 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3742 | /* Generate a shadow store. addr is always the original address atom. |
| 3743 | You can pass in either originals or V-bits for the data atom, but |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 3744 | obviously not both. guard :: Ity_I1 controls whether the store |
| 3745 | really happens; NULL means it unconditionally does. Note that |
| 3746 | guard itself is not checked for definedness; the caller of this |
| 3747 | function must do that if necessary. */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3748 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3749 | static |
sewardj | 2e59585 | 2005-06-30 23:33:37 +0000 | [diff] [blame] | 3750 | void do_shadow_Store ( MCEnv* mce, |
| 3751 | IREndness end, |
| 3752 | IRAtom* addr, UInt bias, |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 3753 | IRAtom* data, IRAtom* vdata, |
| 3754 | IRAtom* guard ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3755 | { |
sewardj | 170ee21 | 2004-12-10 18:57:51 +0000 | [diff] [blame] | 3756 | IROp mkAdd; |
| 3757 | IRType ty, tyAddr; |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3758 | void* helper = NULL; |
| 3759 | Char* hname = NULL; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3760 | IRConst* c; |
sewardj | 170ee21 | 2004-12-10 18:57:51 +0000 | [diff] [blame] | 3761 | |
| 3762 | tyAddr = mce->hWordTy; |
| 3763 | mkAdd = tyAddr==Ity_I32 ? Iop_Add32 : Iop_Add64; |
| 3764 | tl_assert( tyAddr == Ity_I32 || tyAddr == Ity_I64 ); |
sewardj | 2e59585 | 2005-06-30 23:33:37 +0000 | [diff] [blame] | 3765 | tl_assert( end == Iend_LE || end == Iend_BE ); |
sewardj | 170ee21 | 2004-12-10 18:57:51 +0000 | [diff] [blame] | 3766 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3767 | if (data) { |
| 3768 | tl_assert(!vdata); |
| 3769 | tl_assert(isOriginalAtom(mce, data)); |
| 3770 | tl_assert(bias == 0); |
| 3771 | vdata = expr2vbits( mce, data ); |
| 3772 | } else { |
| 3773 | tl_assert(vdata); |
| 3774 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3775 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3776 | tl_assert(isOriginalAtom(mce,addr)); |
| 3777 | tl_assert(isShadowAtom(mce,vdata)); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3778 | |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 3779 | if (guard) { |
| 3780 | tl_assert(isOriginalAtom(mce, guard)); |
| 3781 | tl_assert(typeOfIRExpr(mce->sb->tyenv, guard) == Ity_I1); |
| 3782 | } |
| 3783 | |
| 3784 | ty = typeOfIRExpr(mce->sb->tyenv, vdata); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3785 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3786 | // If we're not doing undefined value checking, pretend that this value |
| 3787 | // is "all valid". That lets Vex's optimiser remove some of the V bit |
| 3788 | // shadow computation ops that precede it. |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 3789 | if (MC_(clo_mc_level) == 1) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3790 | switch (ty) { |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 3791 | case Ity_V128: // V128 weirdness |
| 3792 | c = IRConst_V128(V_BITS16_DEFINED); break; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3793 | case Ity_I64: c = IRConst_U64 (V_BITS64_DEFINED); break; |
| 3794 | case Ity_I32: c = IRConst_U32 (V_BITS32_DEFINED); break; |
| 3795 | case Ity_I16: c = IRConst_U16 (V_BITS16_DEFINED); break; |
| 3796 | case Ity_I8: c = IRConst_U8 (V_BITS8_DEFINED); break; |
| 3797 | default: VG_(tool_panic)("memcheck:do_shadow_Store(LE)"); |
| 3798 | } |
| 3799 | vdata = IRExpr_Const( c ); |
| 3800 | } |
| 3801 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3802 | /* First, emit a definedness test for the address. This also sets |
| 3803 | the address (shadow) to 'defined' following the test. */ |
| 3804 | complainIfUndefined( mce, addr ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3805 | |
sewardj | 170ee21 | 2004-12-10 18:57:51 +0000 | [diff] [blame] | 3806 | /* Now decide which helper function to call to write the data V |
| 3807 | bits into shadow memory. */ |
sewardj | 2e59585 | 2005-06-30 23:33:37 +0000 | [diff] [blame] | 3808 | if (end == Iend_LE) { |
| 3809 | switch (ty) { |
| 3810 | case Ity_V128: /* we'll use the helper twice */ |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3811 | case Ity_I64: helper = &MC_(helperc_STOREV64le); |
| 3812 | hname = "MC_(helperc_STOREV64le)"; |
sewardj | 2e59585 | 2005-06-30 23:33:37 +0000 | [diff] [blame] | 3813 | break; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3814 | case Ity_I32: helper = &MC_(helperc_STOREV32le); |
| 3815 | hname = "MC_(helperc_STOREV32le)"; |
sewardj | 2e59585 | 2005-06-30 23:33:37 +0000 | [diff] [blame] | 3816 | break; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3817 | case Ity_I16: helper = &MC_(helperc_STOREV16le); |
| 3818 | hname = "MC_(helperc_STOREV16le)"; |
sewardj | 2e59585 | 2005-06-30 23:33:37 +0000 | [diff] [blame] | 3819 | break; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3820 | case Ity_I8: helper = &MC_(helperc_STOREV8); |
| 3821 | hname = "MC_(helperc_STOREV8)"; |
sewardj | 2e59585 | 2005-06-30 23:33:37 +0000 | [diff] [blame] | 3822 | break; |
| 3823 | default: VG_(tool_panic)("memcheck:do_shadow_Store(LE)"); |
| 3824 | } |
| 3825 | } else { |
sewardj | 8cf88b7 | 2005-07-08 01:29:33 +0000 | [diff] [blame] | 3826 | switch (ty) { |
| 3827 | case Ity_V128: /* we'll use the helper twice */ |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3828 | case Ity_I64: helper = &MC_(helperc_STOREV64be); |
| 3829 | hname = "MC_(helperc_STOREV64be)"; |
sewardj | 8cf88b7 | 2005-07-08 01:29:33 +0000 | [diff] [blame] | 3830 | break; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3831 | case Ity_I32: helper = &MC_(helperc_STOREV32be); |
| 3832 | hname = "MC_(helperc_STOREV32be)"; |
sewardj | 8cf88b7 | 2005-07-08 01:29:33 +0000 | [diff] [blame] | 3833 | break; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3834 | case Ity_I16: helper = &MC_(helperc_STOREV16be); |
| 3835 | hname = "MC_(helperc_STOREV16be)"; |
sewardj | 8cf88b7 | 2005-07-08 01:29:33 +0000 | [diff] [blame] | 3836 | break; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3837 | case Ity_I8: helper = &MC_(helperc_STOREV8); |
| 3838 | hname = "MC_(helperc_STOREV8)"; |
sewardj | 8cf88b7 | 2005-07-08 01:29:33 +0000 | [diff] [blame] | 3839 | break; |
| 3840 | default: VG_(tool_panic)("memcheck:do_shadow_Store(BE)"); |
| 3841 | } |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3842 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3843 | |
sewardj | 170ee21 | 2004-12-10 18:57:51 +0000 | [diff] [blame] | 3844 | if (ty == Ity_V128) { |
| 3845 | |
sewardj | 20d38f2 | 2005-02-07 23:50:18 +0000 | [diff] [blame] | 3846 | /* V128-bit case */ |
sewardj | 170ee21 | 2004-12-10 18:57:51 +0000 | [diff] [blame] | 3847 | /* See comment in next clause re 64-bit regparms */ |
sewardj | 2e59585 | 2005-06-30 23:33:37 +0000 | [diff] [blame] | 3848 | /* also, need to be careful about endianness */ |
| 3849 | |
njn | 4c245e5 | 2009-03-15 23:25:38 +0000 | [diff] [blame] | 3850 | Int offLo64, offHi64; |
| 3851 | IRDirty *diLo64, *diHi64; |
| 3852 | IRAtom *addrLo64, *addrHi64; |
| 3853 | IRAtom *vdataLo64, *vdataHi64; |
| 3854 | IRAtom *eBiasLo64, *eBiasHi64; |
| 3855 | |
sewardj | 2e59585 | 2005-06-30 23:33:37 +0000 | [diff] [blame] | 3856 | if (end == Iend_LE) { |
| 3857 | offLo64 = 0; |
| 3858 | offHi64 = 8; |
| 3859 | } else { |
sewardj | 2e59585 | 2005-06-30 23:33:37 +0000 | [diff] [blame] | 3860 | offLo64 = 8; |
| 3861 | offHi64 = 0; |
| 3862 | } |
| 3863 | |
| 3864 | eBiasLo64 = tyAddr==Ity_I32 ? mkU32(bias+offLo64) : mkU64(bias+offLo64); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 3865 | addrLo64 = assignNew('V', mce, tyAddr, binop(mkAdd, addr, eBiasLo64) ); |
| 3866 | vdataLo64 = assignNew('V', mce, Ity_I64, unop(Iop_V128to64, vdata)); |
sewardj | 170ee21 | 2004-12-10 18:57:51 +0000 | [diff] [blame] | 3867 | diLo64 = unsafeIRDirty_0_N( |
sewardj | 53ee1fc | 2005-12-23 02:29:58 +0000 | [diff] [blame] | 3868 | 1/*regparms*/, |
| 3869 | hname, VG_(fnptr_to_fnentry)( helper ), |
| 3870 | mkIRExprVec_2( addrLo64, vdataLo64 ) |
| 3871 | ); |
sewardj | 2e59585 | 2005-06-30 23:33:37 +0000 | [diff] [blame] | 3872 | eBiasHi64 = tyAddr==Ity_I32 ? mkU32(bias+offHi64) : mkU64(bias+offHi64); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 3873 | addrHi64 = assignNew('V', mce, tyAddr, binop(mkAdd, addr, eBiasHi64) ); |
| 3874 | vdataHi64 = assignNew('V', mce, Ity_I64, unop(Iop_V128HIto64, vdata)); |
sewardj | 170ee21 | 2004-12-10 18:57:51 +0000 | [diff] [blame] | 3875 | diHi64 = unsafeIRDirty_0_N( |
sewardj | 53ee1fc | 2005-12-23 02:29:58 +0000 | [diff] [blame] | 3876 | 1/*regparms*/, |
| 3877 | hname, VG_(fnptr_to_fnentry)( helper ), |
| 3878 | mkIRExprVec_2( addrHi64, vdataHi64 ) |
| 3879 | ); |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 3880 | if (guard) diLo64->guard = guard; |
| 3881 | if (guard) diHi64->guard = guard; |
sewardj | 170ee21 | 2004-12-10 18:57:51 +0000 | [diff] [blame] | 3882 | setHelperAnns( mce, diLo64 ); |
| 3883 | setHelperAnns( mce, diHi64 ); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 3884 | stmt( 'V', mce, IRStmt_Dirty(diLo64) ); |
| 3885 | stmt( 'V', mce, IRStmt_Dirty(diHi64) ); |
sewardj | 170ee21 | 2004-12-10 18:57:51 +0000 | [diff] [blame] | 3886 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3887 | } else { |
sewardj | 170ee21 | 2004-12-10 18:57:51 +0000 | [diff] [blame] | 3888 | |
njn | 4c245e5 | 2009-03-15 23:25:38 +0000 | [diff] [blame] | 3889 | IRDirty *di; |
| 3890 | IRAtom *addrAct; |
| 3891 | |
sewardj | 170ee21 | 2004-12-10 18:57:51 +0000 | [diff] [blame] | 3892 | /* 8/16/32/64-bit cases */ |
| 3893 | /* Generate the actual address into addrAct. */ |
| 3894 | if (bias == 0) { |
| 3895 | addrAct = addr; |
| 3896 | } else { |
njn | 4c245e5 | 2009-03-15 23:25:38 +0000 | [diff] [blame] | 3897 | IRAtom* eBias = tyAddr==Ity_I32 ? mkU32(bias) : mkU64(bias); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 3898 | addrAct = assignNew('V', mce, tyAddr, binop(mkAdd, addr, eBias)); |
sewardj | 170ee21 | 2004-12-10 18:57:51 +0000 | [diff] [blame] | 3899 | } |
| 3900 | |
| 3901 | if (ty == Ity_I64) { |
| 3902 | /* We can't do this with regparm 2 on 32-bit platforms, since |
| 3903 | the back ends aren't clever enough to handle 64-bit |
| 3904 | regparm args. Therefore be different. */ |
| 3905 | di = unsafeIRDirty_0_N( |
sewardj | 53ee1fc | 2005-12-23 02:29:58 +0000 | [diff] [blame] | 3906 | 1/*regparms*/, |
| 3907 | hname, VG_(fnptr_to_fnentry)( helper ), |
| 3908 | mkIRExprVec_2( addrAct, vdata ) |
| 3909 | ); |
sewardj | 170ee21 | 2004-12-10 18:57:51 +0000 | [diff] [blame] | 3910 | } else { |
| 3911 | di = unsafeIRDirty_0_N( |
sewardj | 53ee1fc | 2005-12-23 02:29:58 +0000 | [diff] [blame] | 3912 | 2/*regparms*/, |
| 3913 | hname, VG_(fnptr_to_fnentry)( helper ), |
sewardj | 170ee21 | 2004-12-10 18:57:51 +0000 | [diff] [blame] | 3914 | mkIRExprVec_2( addrAct, |
sewardj | 53ee1fc | 2005-12-23 02:29:58 +0000 | [diff] [blame] | 3915 | zwidenToHostWord( mce, vdata )) |
| 3916 | ); |
sewardj | 170ee21 | 2004-12-10 18:57:51 +0000 | [diff] [blame] | 3917 | } |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 3918 | if (guard) di->guard = guard; |
sewardj | 170ee21 | 2004-12-10 18:57:51 +0000 | [diff] [blame] | 3919 | setHelperAnns( mce, di ); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 3920 | stmt( 'V', mce, IRStmt_Dirty(di) ); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3921 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3922 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3923 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3924 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3925 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3926 | /* Do lazy pessimistic propagation through a dirty helper call, by |
| 3927 | looking at the annotations on it. This is the most complex part of |
| 3928 | Memcheck. */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3929 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3930 | static IRType szToITy ( Int n ) |
| 3931 | { |
| 3932 | switch (n) { |
| 3933 | case 1: return Ity_I8; |
| 3934 | case 2: return Ity_I16; |
| 3935 | case 4: return Ity_I32; |
| 3936 | case 8: return Ity_I64; |
| 3937 | default: VG_(tool_panic)("szToITy(memcheck)"); |
| 3938 | } |
| 3939 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3940 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3941 | static |
| 3942 | void do_shadow_Dirty ( MCEnv* mce, IRDirty* d ) |
| 3943 | { |
njn | 4c245e5 | 2009-03-15 23:25:38 +0000 | [diff] [blame] | 3944 | Int i, n, toDo, gSz, gOff; |
sewardj | 2e59585 | 2005-06-30 23:33:37 +0000 | [diff] [blame] | 3945 | IRAtom *src, *here, *curr; |
njn | 4c245e5 | 2009-03-15 23:25:38 +0000 | [diff] [blame] | 3946 | IRType tySrc, tyDst; |
sewardj | 2e59585 | 2005-06-30 23:33:37 +0000 | [diff] [blame] | 3947 | IRTemp dst; |
| 3948 | IREndness end; |
| 3949 | |
| 3950 | /* What's the native endianness? We need to know this. */ |
sewardj | 6e340c7 | 2005-07-10 00:53:42 +0000 | [diff] [blame] | 3951 | # if defined(VG_BIGENDIAN) |
sewardj | 2e59585 | 2005-06-30 23:33:37 +0000 | [diff] [blame] | 3952 | end = Iend_BE; |
sewardj | 6e340c7 | 2005-07-10 00:53:42 +0000 | [diff] [blame] | 3953 | # elif defined(VG_LITTLEENDIAN) |
sewardj | 2e59585 | 2005-06-30 23:33:37 +0000 | [diff] [blame] | 3954 | end = Iend_LE; |
| 3955 | # else |
| 3956 | # error "Unknown endianness" |
| 3957 | # endif |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3958 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3959 | /* First check the guard. */ |
| 3960 | complainIfUndefined(mce, d->guard); |
| 3961 | |
| 3962 | /* Now round up all inputs and PCast over them. */ |
sewardj | 7cf97ee | 2004-11-28 14:25:01 +0000 | [diff] [blame] | 3963 | curr = definedOfType(Ity_I32); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3964 | |
| 3965 | /* Inputs: unmasked args */ |
| 3966 | for (i = 0; d->args[i]; i++) { |
| 3967 | if (d->cee->mcx_mask & (1<<i)) { |
| 3968 | /* ignore this arg */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3969 | } else { |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3970 | here = mkPCastTo( mce, Ity_I32, expr2vbits(mce, d->args[i]) ); |
| 3971 | curr = mkUifU32(mce, here, curr); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3972 | } |
| 3973 | } |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3974 | |
| 3975 | /* Inputs: guest state that we read. */ |
| 3976 | for (i = 0; i < d->nFxState; i++) { |
| 3977 | tl_assert(d->fxState[i].fx != Ifx_None); |
| 3978 | if (d->fxState[i].fx == Ifx_Write) |
| 3979 | continue; |
sewardj | a720325 | 2004-11-26 19:17:47 +0000 | [diff] [blame] | 3980 | |
| 3981 | /* Ignore any sections marked as 'always defined'. */ |
| 3982 | if (isAlwaysDefd(mce, d->fxState[i].offset, d->fxState[i].size )) { |
sewardj | e9e16d3 | 2004-12-10 13:17:55 +0000 | [diff] [blame] | 3983 | if (0) |
sewardj | a720325 | 2004-11-26 19:17:47 +0000 | [diff] [blame] | 3984 | VG_(printf)("memcheck: Dirty gst: ignored off %d, sz %d\n", |
| 3985 | d->fxState[i].offset, d->fxState[i].size ); |
| 3986 | continue; |
| 3987 | } |
| 3988 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 3989 | /* This state element is read or modified. So we need to |
sewardj | e9e16d3 | 2004-12-10 13:17:55 +0000 | [diff] [blame] | 3990 | consider it. If larger than 8 bytes, deal with it in 8-byte |
| 3991 | chunks. */ |
| 3992 | gSz = d->fxState[i].size; |
| 3993 | gOff = d->fxState[i].offset; |
| 3994 | tl_assert(gSz > 0); |
| 3995 | while (True) { |
| 3996 | if (gSz == 0) break; |
| 3997 | n = gSz <= 8 ? gSz : 8; |
| 3998 | /* update 'curr' with UifU of the state slice |
| 3999 | gOff .. gOff+n-1 */ |
| 4000 | tySrc = szToITy( n ); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 4001 | src = assignNew( 'V', mce, tySrc, |
| 4002 | shadow_GET(mce, gOff, tySrc ) ); |
sewardj | e9e16d3 | 2004-12-10 13:17:55 +0000 | [diff] [blame] | 4003 | here = mkPCastTo( mce, Ity_I32, src ); |
| 4004 | curr = mkUifU32(mce, here, curr); |
| 4005 | gSz -= n; |
| 4006 | gOff += n; |
| 4007 | } |
| 4008 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 4009 | } |
| 4010 | |
| 4011 | /* Inputs: memory. First set up some info needed regardless of |
| 4012 | whether we're doing reads or writes. */ |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 4013 | |
| 4014 | if (d->mFx != Ifx_None) { |
| 4015 | /* Because we may do multiple shadow loads/stores from the same |
| 4016 | base address, it's best to do a single test of its |
| 4017 | definedness right now. Post-instrumentation optimisation |
| 4018 | should remove all but this test. */ |
njn | 4c245e5 | 2009-03-15 23:25:38 +0000 | [diff] [blame] | 4019 | IRType tyAddr; |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 4020 | tl_assert(d->mAddr); |
| 4021 | complainIfUndefined(mce, d->mAddr); |
| 4022 | |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 4023 | tyAddr = typeOfIRExpr(mce->sb->tyenv, d->mAddr); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 4024 | tl_assert(tyAddr == Ity_I32 || tyAddr == Ity_I64); |
| 4025 | tl_assert(tyAddr == mce->hWordTy); /* not really right */ |
| 4026 | } |
| 4027 | |
| 4028 | /* Deal with memory inputs (reads or modifies) */ |
| 4029 | if (d->mFx == Ifx_Read || d->mFx == Ifx_Modify) { |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 4030 | toDo = d->mSize; |
sewardj | 2e59585 | 2005-06-30 23:33:37 +0000 | [diff] [blame] | 4031 | /* chew off 32-bit chunks. We don't care about the endianness |
| 4032 | since it's all going to be condensed down to a single bit, |
| 4033 | but nevertheless choose an endianness which is hopefully |
| 4034 | native to the platform. */ |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 4035 | while (toDo >= 4) { |
| 4036 | here = mkPCastTo( |
| 4037 | mce, Ity_I32, |
sewardj | 2e59585 | 2005-06-30 23:33:37 +0000 | [diff] [blame] | 4038 | expr2vbits_Load ( mce, end, Ity_I32, |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 4039 | d->mAddr, d->mSize - toDo ) |
| 4040 | ); |
| 4041 | curr = mkUifU32(mce, here, curr); |
| 4042 | toDo -= 4; |
| 4043 | } |
| 4044 | /* chew off 16-bit chunks */ |
| 4045 | while (toDo >= 2) { |
| 4046 | here = mkPCastTo( |
| 4047 | mce, Ity_I32, |
sewardj | 2e59585 | 2005-06-30 23:33:37 +0000 | [diff] [blame] | 4048 | expr2vbits_Load ( mce, end, Ity_I16, |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 4049 | d->mAddr, d->mSize - toDo ) |
| 4050 | ); |
| 4051 | curr = mkUifU32(mce, here, curr); |
| 4052 | toDo -= 2; |
| 4053 | } |
| 4054 | tl_assert(toDo == 0); /* also need to handle 1-byte excess */ |
| 4055 | } |
| 4056 | |
| 4057 | /* Whew! So curr is a 32-bit V-value summarising pessimistically |
| 4058 | all the inputs to the helper. Now we need to re-distribute the |
| 4059 | results to all destinations. */ |
| 4060 | |
| 4061 | /* Outputs: the destination temporary, if there is one. */ |
| 4062 | if (d->tmp != IRTemp_INVALID) { |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 4063 | dst = findShadowTmpV(mce, d->tmp); |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 4064 | tyDst = typeOfIRTemp(mce->sb->tyenv, d->tmp); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 4065 | assign( 'V', mce, dst, mkPCastTo( mce, tyDst, curr) ); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 4066 | } |
| 4067 | |
| 4068 | /* Outputs: guest state that we write or modify. */ |
| 4069 | for (i = 0; i < d->nFxState; i++) { |
| 4070 | tl_assert(d->fxState[i].fx != Ifx_None); |
| 4071 | if (d->fxState[i].fx == Ifx_Read) |
| 4072 | continue; |
sewardj | a720325 | 2004-11-26 19:17:47 +0000 | [diff] [blame] | 4073 | /* Ignore any sections marked as 'always defined'. */ |
| 4074 | if (isAlwaysDefd(mce, d->fxState[i].offset, d->fxState[i].size )) |
| 4075 | continue; |
sewardj | e9e16d3 | 2004-12-10 13:17:55 +0000 | [diff] [blame] | 4076 | /* This state element is written or modified. So we need to |
| 4077 | consider it. If larger than 8 bytes, deal with it in 8-byte |
| 4078 | chunks. */ |
| 4079 | gSz = d->fxState[i].size; |
| 4080 | gOff = d->fxState[i].offset; |
| 4081 | tl_assert(gSz > 0); |
| 4082 | while (True) { |
| 4083 | if (gSz == 0) break; |
| 4084 | n = gSz <= 8 ? gSz : 8; |
| 4085 | /* Write suitably-casted 'curr' to the state slice |
| 4086 | gOff .. gOff+n-1 */ |
| 4087 | tyDst = szToITy( n ); |
| 4088 | do_shadow_PUT( mce, gOff, |
| 4089 | NULL, /* original atom */ |
| 4090 | mkPCastTo( mce, tyDst, curr ) ); |
| 4091 | gSz -= n; |
| 4092 | gOff += n; |
| 4093 | } |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 4094 | } |
| 4095 | |
sewardj | 2e59585 | 2005-06-30 23:33:37 +0000 | [diff] [blame] | 4096 | /* Outputs: memory that we write or modify. Same comments about |
| 4097 | endianness as above apply. */ |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 4098 | if (d->mFx == Ifx_Write || d->mFx == Ifx_Modify) { |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 4099 | toDo = d->mSize; |
| 4100 | /* chew off 32-bit chunks */ |
| 4101 | while (toDo >= 4) { |
sewardj | 2e59585 | 2005-06-30 23:33:37 +0000 | [diff] [blame] | 4102 | do_shadow_Store( mce, end, d->mAddr, d->mSize - toDo, |
| 4103 | NULL, /* original data */ |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 4104 | mkPCastTo( mce, Ity_I32, curr ), |
| 4105 | NULL/*guard*/ ); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 4106 | toDo -= 4; |
| 4107 | } |
| 4108 | /* chew off 16-bit chunks */ |
| 4109 | while (toDo >= 2) { |
sewardj | 2e59585 | 2005-06-30 23:33:37 +0000 | [diff] [blame] | 4110 | do_shadow_Store( mce, end, d->mAddr, d->mSize - toDo, |
| 4111 | NULL, /* original data */ |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 4112 | mkPCastTo( mce, Ity_I16, curr ), |
| 4113 | NULL/*guard*/ ); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 4114 | toDo -= 2; |
| 4115 | } |
| 4116 | tl_assert(toDo == 0); /* also need to handle 1-byte excess */ |
| 4117 | } |
| 4118 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 4119 | } |
| 4120 | |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 4121 | |
sewardj | 826ec49 | 2005-05-12 18:05:00 +0000 | [diff] [blame] | 4122 | /* We have an ABI hint telling us that [base .. base+len-1] is to |
| 4123 | become undefined ("writable"). Generate code to call a helper to |
| 4124 | notify the A/V bit machinery of this fact. |
| 4125 | |
| 4126 | We call |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 4127 | void MC_(helperc_MAKE_STACK_UNINIT) ( Addr base, UWord len, |
| 4128 | Addr nia ); |
sewardj | 826ec49 | 2005-05-12 18:05:00 +0000 | [diff] [blame] | 4129 | */ |
| 4130 | static |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 4131 | void do_AbiHint ( MCEnv* mce, IRExpr* base, Int len, IRExpr* nia ) |
sewardj | 826ec49 | 2005-05-12 18:05:00 +0000 | [diff] [blame] | 4132 | { |
| 4133 | IRDirty* di; |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 4134 | /* Minor optimisation: if not doing origin tracking, ignore the |
| 4135 | supplied nia and pass zero instead. This is on the basis that |
| 4136 | MC_(helperc_MAKE_STACK_UNINIT) will ignore it anyway, and we can |
| 4137 | almost always generate a shorter instruction to put zero into a |
| 4138 | register than any other value. */ |
| 4139 | if (MC_(clo_mc_level) < 3) |
| 4140 | nia = mkIRExpr_HWord(0); |
| 4141 | |
sewardj | 826ec49 | 2005-05-12 18:05:00 +0000 | [diff] [blame] | 4142 | di = unsafeIRDirty_0_N( |
| 4143 | 0/*regparms*/, |
| 4144 | "MC_(helperc_MAKE_STACK_UNINIT)", |
sewardj | 53ee1fc | 2005-12-23 02:29:58 +0000 | [diff] [blame] | 4145 | VG_(fnptr_to_fnentry)( &MC_(helperc_MAKE_STACK_UNINIT) ), |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 4146 | mkIRExprVec_3( base, mkIRExpr_HWord( (UInt)len), nia ) |
sewardj | 826ec49 | 2005-05-12 18:05:00 +0000 | [diff] [blame] | 4147 | ); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 4148 | stmt( 'V', mce, IRStmt_Dirty(di) ); |
sewardj | 826ec49 | 2005-05-12 18:05:00 +0000 | [diff] [blame] | 4149 | } |
| 4150 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 4151 | |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 4152 | /* ------ Dealing with IRCAS (big and complex) ------ */ |
| 4153 | |
| 4154 | /* FWDS */ |
| 4155 | static IRAtom* gen_load_b ( MCEnv* mce, Int szB, |
| 4156 | IRAtom* baseaddr, Int offset ); |
| 4157 | static IRAtom* gen_maxU32 ( MCEnv* mce, IRAtom* b1, IRAtom* b2 ); |
| 4158 | static void gen_store_b ( MCEnv* mce, Int szB, |
| 4159 | IRAtom* baseaddr, Int offset, IRAtom* dataB, |
| 4160 | IRAtom* guard ); |
| 4161 | |
| 4162 | static void do_shadow_CAS_single ( MCEnv* mce, IRCAS* cas ); |
| 4163 | static void do_shadow_CAS_double ( MCEnv* mce, IRCAS* cas ); |
| 4164 | |
| 4165 | |
| 4166 | /* Either ORIG and SHADOW are both IRExpr.RdTmps, or they are both |
| 4167 | IRExpr.Consts, else this asserts. If they are both Consts, it |
| 4168 | doesn't do anything. So that just leaves the RdTmp case. |
| 4169 | |
| 4170 | In which case: this assigns the shadow value SHADOW to the IR |
| 4171 | shadow temporary associated with ORIG. That is, ORIG, being an |
| 4172 | original temporary, will have a shadow temporary associated with |
| 4173 | it. However, in the case envisaged here, there will so far have |
| 4174 | been no IR emitted to actually write a shadow value into that |
| 4175 | temporary. What this routine does is to (emit IR to) copy the |
| 4176 | value in SHADOW into said temporary, so that after this call, |
| 4177 | IRExpr.RdTmps of ORIG's shadow temp will correctly pick up the |
| 4178 | value in SHADOW. |
| 4179 | |
| 4180 | Point is to allow callers to compute "by hand" a shadow value for |
| 4181 | ORIG, and force it to be associated with ORIG. |
| 4182 | |
| 4183 | How do we know that that shadow associated with ORIG has not so far |
| 4184 | been assigned to? Well, we don't per se know that, but supposing |
| 4185 | it had. Then this routine would create a second assignment to it, |
| 4186 | and later the IR sanity checker would barf. But that never |
| 4187 | happens. QED. |
| 4188 | */ |
| 4189 | static void bind_shadow_tmp_to_orig ( UChar how, |
| 4190 | MCEnv* mce, |
| 4191 | IRAtom* orig, IRAtom* shadow ) |
| 4192 | { |
| 4193 | tl_assert(isOriginalAtom(mce, orig)); |
| 4194 | tl_assert(isShadowAtom(mce, shadow)); |
| 4195 | switch (orig->tag) { |
| 4196 | case Iex_Const: |
| 4197 | tl_assert(shadow->tag == Iex_Const); |
| 4198 | break; |
| 4199 | case Iex_RdTmp: |
| 4200 | tl_assert(shadow->tag == Iex_RdTmp); |
| 4201 | if (how == 'V') { |
| 4202 | assign('V', mce, findShadowTmpV(mce,orig->Iex.RdTmp.tmp), |
| 4203 | shadow); |
| 4204 | } else { |
| 4205 | tl_assert(how == 'B'); |
| 4206 | assign('B', mce, findShadowTmpB(mce,orig->Iex.RdTmp.tmp), |
| 4207 | shadow); |
| 4208 | } |
| 4209 | break; |
| 4210 | default: |
| 4211 | tl_assert(0); |
| 4212 | } |
| 4213 | } |
| 4214 | |
| 4215 | |
| 4216 | static |
| 4217 | void do_shadow_CAS ( MCEnv* mce, IRCAS* cas ) |
| 4218 | { |
| 4219 | /* Scheme is (both single- and double- cases): |
| 4220 | |
| 4221 | 1. fetch data#,dataB (the proposed new value) |
| 4222 | |
| 4223 | 2. fetch expd#,expdB (what we expect to see at the address) |
| 4224 | |
| 4225 | 3. check definedness of address |
| 4226 | |
| 4227 | 4. load old#,oldB from shadow memory; this also checks |
| 4228 | addressibility of the address |
| 4229 | |
| 4230 | 5. the CAS itself |
| 4231 | |
sewardj | afed4c5 | 2009-07-12 13:00:17 +0000 | [diff] [blame] | 4232 | 6. compute "expected == old". See COMMENT_ON_CasCmpEQ below. |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 4233 | |
sewardj | afed4c5 | 2009-07-12 13:00:17 +0000 | [diff] [blame] | 4234 | 7. if "expected == old" (as computed by (6)) |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 4235 | store data#,dataB to shadow memory |
| 4236 | |
| 4237 | Note that 5 reads 'old' but 4 reads 'old#'. Similarly, 5 stores |
| 4238 | 'data' but 7 stores 'data#'. Hence it is possible for the |
| 4239 | shadow data to be incorrectly checked and/or updated: |
| 4240 | |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 4241 | * 7 is at least gated correctly, since the 'expected == old' |
| 4242 | condition is derived from outputs of 5. However, the shadow |
| 4243 | write could happen too late: imagine after 5 we are |
| 4244 | descheduled, a different thread runs, writes a different |
| 4245 | (shadow) value at the address, and then we resume, hence |
| 4246 | overwriting the shadow value written by the other thread. |
| 4247 | |
| 4248 | Because the original memory access is atomic, there's no way to |
| 4249 | make both the original and shadow accesses into a single atomic |
| 4250 | thing, hence this is unavoidable. |
| 4251 | |
| 4252 | At least as Valgrind stands, I don't think it's a problem, since |
| 4253 | we're single threaded *and* we guarantee that there are no |
| 4254 | context switches during the execution of any specific superblock |
| 4255 | -- context switches can only happen at superblock boundaries. |
| 4256 | |
| 4257 | If Valgrind ever becomes MT in the future, then it might be more |
| 4258 | of a problem. A possible kludge would be to artificially |
| 4259 | associate with the location, a lock, which we must acquire and |
| 4260 | release around the transaction as a whole. Hmm, that probably |
| 4261 | would't work properly since it only guards us against other |
| 4262 | threads doing CASs on the same location, not against other |
| 4263 | threads doing normal reads and writes. |
sewardj | afed4c5 | 2009-07-12 13:00:17 +0000 | [diff] [blame] | 4264 | |
| 4265 | ------------------------------------------------------------ |
| 4266 | |
| 4267 | COMMENT_ON_CasCmpEQ: |
| 4268 | |
| 4269 | Note two things. Firstly, in the sequence above, we compute |
| 4270 | "expected == old", but we don't check definedness of it. Why |
| 4271 | not? Also, the x86 and amd64 front ends use |
| 4272 | Iop_CmpCas{EQ,NE}{8,16,32,64} comparisons to make the equivalent |
| 4273 | determination (expected == old ?) for themselves, and we also |
| 4274 | don't check definedness for those primops; we just say that the |
| 4275 | result is defined. Why? Details follow. |
| 4276 | |
| 4277 | x86/amd64 contains various forms of locked insns: |
| 4278 | * lock prefix before all basic arithmetic insn; |
| 4279 | eg lock xorl %reg1,(%reg2) |
| 4280 | * atomic exchange reg-mem |
| 4281 | * compare-and-swaps |
| 4282 | |
| 4283 | Rather than attempt to represent them all, which would be a |
| 4284 | royal PITA, I used a result from Maurice Herlihy |
| 4285 | (http://en.wikipedia.org/wiki/Maurice_Herlihy), in which he |
| 4286 | demonstrates that compare-and-swap is a primitive more general |
| 4287 | than the other two, and so can be used to represent all of them. |
| 4288 | So the translation scheme for (eg) lock incl (%reg) is as |
| 4289 | follows: |
| 4290 | |
| 4291 | again: |
| 4292 | old = * %reg |
| 4293 | new = old + 1 |
| 4294 | atomically { if (* %reg == old) { * %reg = new } else { goto again } } |
| 4295 | |
| 4296 | The "atomically" is the CAS bit. The scheme is always the same: |
| 4297 | get old value from memory, compute new value, atomically stuff |
| 4298 | new value back in memory iff the old value has not changed (iow, |
| 4299 | no other thread modified it in the meantime). If it has changed |
| 4300 | then we've been out-raced and we have to start over. |
| 4301 | |
| 4302 | Now that's all very neat, but it has the bad side effect of |
| 4303 | introducing an explicit equality test into the translation. |
| 4304 | Consider the behaviour of said code on a memory location which |
| 4305 | is uninitialised. We will wind up doing a comparison on |
| 4306 | uninitialised data, and mc duly complains. |
| 4307 | |
| 4308 | What's difficult about this is, the common case is that the |
| 4309 | location is uncontended, and so we're usually comparing the same |
| 4310 | value (* %reg) with itself. So we shouldn't complain even if it |
| 4311 | is undefined. But mc doesn't know that. |
| 4312 | |
| 4313 | My solution is to mark the == in the IR specially, so as to tell |
| 4314 | mc that it almost certainly compares a value with itself, and we |
| 4315 | should just regard the result as always defined. Rather than |
| 4316 | add a bit to all IROps, I just cloned Iop_CmpEQ{8,16,32,64} into |
| 4317 | Iop_CasCmpEQ{8,16,32,64} so as not to disturb anything else. |
| 4318 | |
| 4319 | So there's always the question of, can this give a false |
| 4320 | negative? eg, imagine that initially, * %reg is defined; and we |
| 4321 | read that; but then in the gap between the read and the CAS, a |
| 4322 | different thread writes an undefined (and different) value at |
| 4323 | the location. Then the CAS in this thread will fail and we will |
| 4324 | go back to "again:", but without knowing that the trip back |
| 4325 | there was based on an undefined comparison. No matter; at least |
| 4326 | the other thread won the race and the location is correctly |
| 4327 | marked as undefined. What if it wrote an uninitialised version |
| 4328 | of the same value that was there originally, though? |
| 4329 | |
| 4330 | etc etc. Seems like there's a small corner case in which we |
| 4331 | might lose the fact that something's defined -- we're out-raced |
| 4332 | in between the "old = * reg" and the "atomically {", _and_ the |
| 4333 | other thread is writing in an undefined version of what's |
| 4334 | already there. Well, that seems pretty unlikely. |
| 4335 | |
| 4336 | --- |
| 4337 | |
| 4338 | If we ever need to reinstate it .. code which generates a |
| 4339 | definedness test for "expected == old" was removed at r10432 of |
| 4340 | this file. |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 4341 | */ |
| 4342 | if (cas->oldHi == IRTemp_INVALID) { |
| 4343 | do_shadow_CAS_single( mce, cas ); |
| 4344 | } else { |
| 4345 | do_shadow_CAS_double( mce, cas ); |
| 4346 | } |
| 4347 | } |
| 4348 | |
| 4349 | |
| 4350 | static void do_shadow_CAS_single ( MCEnv* mce, IRCAS* cas ) |
| 4351 | { |
| 4352 | IRAtom *vdataLo = NULL, *bdataLo = NULL; |
| 4353 | IRAtom *vexpdLo = NULL, *bexpdLo = NULL; |
| 4354 | IRAtom *voldLo = NULL, *boldLo = NULL; |
sewardj | afed4c5 | 2009-07-12 13:00:17 +0000 | [diff] [blame] | 4355 | IRAtom *expd_eq_old = NULL; |
| 4356 | IROp opCasCmpEQ; |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 4357 | Int elemSzB; |
| 4358 | IRType elemTy; |
| 4359 | Bool otrak = MC_(clo_mc_level) >= 3; /* a shorthand */ |
| 4360 | |
| 4361 | /* single CAS */ |
| 4362 | tl_assert(cas->oldHi == IRTemp_INVALID); |
| 4363 | tl_assert(cas->expdHi == NULL); |
| 4364 | tl_assert(cas->dataHi == NULL); |
| 4365 | |
| 4366 | elemTy = typeOfIRExpr(mce->sb->tyenv, cas->expdLo); |
| 4367 | switch (elemTy) { |
sewardj | afed4c5 | 2009-07-12 13:00:17 +0000 | [diff] [blame] | 4368 | case Ity_I8: elemSzB = 1; opCasCmpEQ = Iop_CasCmpEQ8; break; |
| 4369 | case Ity_I16: elemSzB = 2; opCasCmpEQ = Iop_CasCmpEQ16; break; |
| 4370 | case Ity_I32: elemSzB = 4; opCasCmpEQ = Iop_CasCmpEQ32; break; |
| 4371 | case Ity_I64: elemSzB = 8; opCasCmpEQ = Iop_CasCmpEQ64; break; |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 4372 | default: tl_assert(0); /* IR defn disallows any other types */ |
| 4373 | } |
| 4374 | |
| 4375 | /* 1. fetch data# (the proposed new value) */ |
| 4376 | tl_assert(isOriginalAtom(mce, cas->dataLo)); |
| 4377 | vdataLo |
| 4378 | = assignNew('V', mce, elemTy, expr2vbits(mce, cas->dataLo)); |
| 4379 | tl_assert(isShadowAtom(mce, vdataLo)); |
| 4380 | if (otrak) { |
| 4381 | bdataLo |
| 4382 | = assignNew('B', mce, Ity_I32, schemeE(mce, cas->dataLo)); |
| 4383 | tl_assert(isShadowAtom(mce, bdataLo)); |
| 4384 | } |
| 4385 | |
| 4386 | /* 2. fetch expected# (what we expect to see at the address) */ |
| 4387 | tl_assert(isOriginalAtom(mce, cas->expdLo)); |
| 4388 | vexpdLo |
| 4389 | = assignNew('V', mce, elemTy, expr2vbits(mce, cas->expdLo)); |
| 4390 | tl_assert(isShadowAtom(mce, vexpdLo)); |
| 4391 | if (otrak) { |
| 4392 | bexpdLo |
| 4393 | = assignNew('B', mce, Ity_I32, schemeE(mce, cas->expdLo)); |
| 4394 | tl_assert(isShadowAtom(mce, bexpdLo)); |
| 4395 | } |
| 4396 | |
| 4397 | /* 3. check definedness of address */ |
| 4398 | /* 4. fetch old# from shadow memory; this also checks |
| 4399 | addressibility of the address */ |
| 4400 | voldLo |
| 4401 | = assignNew( |
| 4402 | 'V', mce, elemTy, |
| 4403 | expr2vbits_Load( |
| 4404 | mce, |
| 4405 | cas->end, elemTy, cas->addr, 0/*Addr bias*/ |
| 4406 | )); |
sewardj | afed4c5 | 2009-07-12 13:00:17 +0000 | [diff] [blame] | 4407 | bind_shadow_tmp_to_orig('V', mce, mkexpr(cas->oldLo), voldLo); |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 4408 | if (otrak) { |
| 4409 | boldLo |
| 4410 | = assignNew('B', mce, Ity_I32, |
| 4411 | gen_load_b(mce, elemSzB, cas->addr, 0/*addr bias*/)); |
sewardj | afed4c5 | 2009-07-12 13:00:17 +0000 | [diff] [blame] | 4412 | bind_shadow_tmp_to_orig('B', mce, mkexpr(cas->oldLo), boldLo); |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 4413 | } |
| 4414 | |
| 4415 | /* 5. the CAS itself */ |
| 4416 | stmt( 'C', mce, IRStmt_CAS(cas) ); |
| 4417 | |
sewardj | afed4c5 | 2009-07-12 13:00:17 +0000 | [diff] [blame] | 4418 | /* 6. compute "expected == old" */ |
| 4419 | /* See COMMENT_ON_CasCmpEQ in this file background/rationale. */ |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 4420 | /* Note that 'C' is kinda faking it; it is indeed a non-shadow |
| 4421 | tree, but it's not copied from the input block. */ |
| 4422 | expd_eq_old |
| 4423 | = assignNew('C', mce, Ity_I1, |
sewardj | afed4c5 | 2009-07-12 13:00:17 +0000 | [diff] [blame] | 4424 | binop(opCasCmpEQ, cas->expdLo, mkexpr(cas->oldLo))); |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 4425 | |
| 4426 | /* 7. if "expected == old" |
| 4427 | store data# to shadow memory */ |
| 4428 | do_shadow_Store( mce, cas->end, cas->addr, 0/*bias*/, |
| 4429 | NULL/*data*/, vdataLo/*vdata*/, |
| 4430 | expd_eq_old/*guard for store*/ ); |
| 4431 | if (otrak) { |
| 4432 | gen_store_b( mce, elemSzB, cas->addr, 0/*offset*/, |
| 4433 | bdataLo/*bdata*/, |
| 4434 | expd_eq_old/*guard for store*/ ); |
| 4435 | } |
| 4436 | } |
| 4437 | |
| 4438 | |
| 4439 | static void do_shadow_CAS_double ( MCEnv* mce, IRCAS* cas ) |
| 4440 | { |
| 4441 | IRAtom *vdataHi = NULL, *bdataHi = NULL; |
| 4442 | IRAtom *vdataLo = NULL, *bdataLo = NULL; |
| 4443 | IRAtom *vexpdHi = NULL, *bexpdHi = NULL; |
| 4444 | IRAtom *vexpdLo = NULL, *bexpdLo = NULL; |
| 4445 | IRAtom *voldHi = NULL, *boldHi = NULL; |
| 4446 | IRAtom *voldLo = NULL, *boldLo = NULL; |
sewardj | afed4c5 | 2009-07-12 13:00:17 +0000 | [diff] [blame] | 4447 | IRAtom *xHi = NULL, *xLo = NULL, *xHL = NULL; |
| 4448 | IRAtom *expd_eq_old = NULL, *zero = NULL; |
| 4449 | IROp opCasCmpEQ, opOr, opXor; |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 4450 | Int elemSzB, memOffsLo, memOffsHi; |
| 4451 | IRType elemTy; |
| 4452 | Bool otrak = MC_(clo_mc_level) >= 3; /* a shorthand */ |
| 4453 | |
| 4454 | /* double CAS */ |
| 4455 | tl_assert(cas->oldHi != IRTemp_INVALID); |
| 4456 | tl_assert(cas->expdHi != NULL); |
| 4457 | tl_assert(cas->dataHi != NULL); |
| 4458 | |
| 4459 | elemTy = typeOfIRExpr(mce->sb->tyenv, cas->expdLo); |
| 4460 | switch (elemTy) { |
| 4461 | case Ity_I8: |
sewardj | afed4c5 | 2009-07-12 13:00:17 +0000 | [diff] [blame] | 4462 | opCasCmpEQ = Iop_CasCmpEQ8; opOr = Iop_Or8; opXor = Iop_Xor8; |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 4463 | elemSzB = 1; zero = mkU8(0); |
| 4464 | break; |
| 4465 | case Ity_I16: |
sewardj | afed4c5 | 2009-07-12 13:00:17 +0000 | [diff] [blame] | 4466 | opCasCmpEQ = Iop_CasCmpEQ16; opOr = Iop_Or16; opXor = Iop_Xor16; |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 4467 | elemSzB = 2; zero = mkU16(0); |
| 4468 | break; |
| 4469 | case Ity_I32: |
sewardj | afed4c5 | 2009-07-12 13:00:17 +0000 | [diff] [blame] | 4470 | opCasCmpEQ = Iop_CasCmpEQ32; opOr = Iop_Or32; opXor = Iop_Xor32; |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 4471 | elemSzB = 4; zero = mkU32(0); |
| 4472 | break; |
| 4473 | case Ity_I64: |
sewardj | afed4c5 | 2009-07-12 13:00:17 +0000 | [diff] [blame] | 4474 | opCasCmpEQ = Iop_CasCmpEQ64; opOr = Iop_Or64; opXor = Iop_Xor64; |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 4475 | elemSzB = 8; zero = mkU64(0); |
| 4476 | break; |
| 4477 | default: |
| 4478 | tl_assert(0); /* IR defn disallows any other types */ |
| 4479 | } |
| 4480 | |
| 4481 | /* 1. fetch data# (the proposed new value) */ |
| 4482 | tl_assert(isOriginalAtom(mce, cas->dataHi)); |
| 4483 | tl_assert(isOriginalAtom(mce, cas->dataLo)); |
| 4484 | vdataHi |
| 4485 | = assignNew('V', mce, elemTy, expr2vbits(mce, cas->dataHi)); |
| 4486 | vdataLo |
| 4487 | = assignNew('V', mce, elemTy, expr2vbits(mce, cas->dataLo)); |
| 4488 | tl_assert(isShadowAtom(mce, vdataHi)); |
| 4489 | tl_assert(isShadowAtom(mce, vdataLo)); |
| 4490 | if (otrak) { |
| 4491 | bdataHi |
| 4492 | = assignNew('B', mce, Ity_I32, schemeE(mce, cas->dataHi)); |
| 4493 | bdataLo |
| 4494 | = assignNew('B', mce, Ity_I32, schemeE(mce, cas->dataLo)); |
| 4495 | tl_assert(isShadowAtom(mce, bdataHi)); |
| 4496 | tl_assert(isShadowAtom(mce, bdataLo)); |
| 4497 | } |
| 4498 | |
| 4499 | /* 2. fetch expected# (what we expect to see at the address) */ |
| 4500 | tl_assert(isOriginalAtom(mce, cas->expdHi)); |
| 4501 | tl_assert(isOriginalAtom(mce, cas->expdLo)); |
| 4502 | vexpdHi |
| 4503 | = assignNew('V', mce, elemTy, expr2vbits(mce, cas->expdHi)); |
| 4504 | vexpdLo |
| 4505 | = assignNew('V', mce, elemTy, expr2vbits(mce, cas->expdLo)); |
| 4506 | tl_assert(isShadowAtom(mce, vexpdHi)); |
| 4507 | tl_assert(isShadowAtom(mce, vexpdLo)); |
| 4508 | if (otrak) { |
| 4509 | bexpdHi |
| 4510 | = assignNew('B', mce, Ity_I32, schemeE(mce, cas->expdHi)); |
| 4511 | bexpdLo |
| 4512 | = assignNew('B', mce, Ity_I32, schemeE(mce, cas->expdLo)); |
| 4513 | tl_assert(isShadowAtom(mce, bexpdHi)); |
| 4514 | tl_assert(isShadowAtom(mce, bexpdLo)); |
| 4515 | } |
| 4516 | |
| 4517 | /* 3. check definedness of address */ |
| 4518 | /* 4. fetch old# from shadow memory; this also checks |
| 4519 | addressibility of the address */ |
| 4520 | if (cas->end == Iend_LE) { |
| 4521 | memOffsLo = 0; |
| 4522 | memOffsHi = elemSzB; |
| 4523 | } else { |
| 4524 | tl_assert(cas->end == Iend_BE); |
| 4525 | memOffsLo = elemSzB; |
| 4526 | memOffsHi = 0; |
| 4527 | } |
| 4528 | voldHi |
| 4529 | = assignNew( |
| 4530 | 'V', mce, elemTy, |
| 4531 | expr2vbits_Load( |
| 4532 | mce, |
| 4533 | cas->end, elemTy, cas->addr, memOffsHi/*Addr bias*/ |
| 4534 | )); |
| 4535 | voldLo |
| 4536 | = assignNew( |
| 4537 | 'V', mce, elemTy, |
| 4538 | expr2vbits_Load( |
| 4539 | mce, |
| 4540 | cas->end, elemTy, cas->addr, memOffsLo/*Addr bias*/ |
| 4541 | )); |
sewardj | afed4c5 | 2009-07-12 13:00:17 +0000 | [diff] [blame] | 4542 | bind_shadow_tmp_to_orig('V', mce, mkexpr(cas->oldHi), voldHi); |
| 4543 | bind_shadow_tmp_to_orig('V', mce, mkexpr(cas->oldLo), voldLo); |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 4544 | if (otrak) { |
| 4545 | boldHi |
| 4546 | = assignNew('B', mce, Ity_I32, |
| 4547 | gen_load_b(mce, elemSzB, cas->addr, |
| 4548 | memOffsHi/*addr bias*/)); |
| 4549 | boldLo |
| 4550 | = assignNew('B', mce, Ity_I32, |
| 4551 | gen_load_b(mce, elemSzB, cas->addr, |
| 4552 | memOffsLo/*addr bias*/)); |
sewardj | afed4c5 | 2009-07-12 13:00:17 +0000 | [diff] [blame] | 4553 | bind_shadow_tmp_to_orig('B', mce, mkexpr(cas->oldHi), boldHi); |
| 4554 | bind_shadow_tmp_to_orig('B', mce, mkexpr(cas->oldLo), boldLo); |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 4555 | } |
| 4556 | |
| 4557 | /* 5. the CAS itself */ |
| 4558 | stmt( 'C', mce, IRStmt_CAS(cas) ); |
| 4559 | |
sewardj | afed4c5 | 2009-07-12 13:00:17 +0000 | [diff] [blame] | 4560 | /* 6. compute "expected == old" */ |
| 4561 | /* See COMMENT_ON_CasCmpEQ in this file background/rationale. */ |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 4562 | /* Note that 'C' is kinda faking it; it is indeed a non-shadow |
| 4563 | tree, but it's not copied from the input block. */ |
| 4564 | /* |
| 4565 | xHi = oldHi ^ expdHi; |
| 4566 | xLo = oldLo ^ expdLo; |
| 4567 | xHL = xHi | xLo; |
| 4568 | expd_eq_old = xHL == 0; |
| 4569 | */ |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 4570 | xHi = assignNew('C', mce, elemTy, |
| 4571 | binop(opXor, cas->expdHi, mkexpr(cas->oldHi))); |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 4572 | xLo = assignNew('C', mce, elemTy, |
| 4573 | binop(opXor, cas->expdLo, mkexpr(cas->oldLo))); |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 4574 | xHL = assignNew('C', mce, elemTy, |
| 4575 | binop(opOr, xHi, xLo)); |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 4576 | expd_eq_old |
| 4577 | = assignNew('C', mce, Ity_I1, |
sewardj | afed4c5 | 2009-07-12 13:00:17 +0000 | [diff] [blame] | 4578 | binop(opCasCmpEQ, xHL, zero)); |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 4579 | |
| 4580 | /* 7. if "expected == old" |
| 4581 | store data# to shadow memory */ |
| 4582 | do_shadow_Store( mce, cas->end, cas->addr, memOffsHi/*bias*/, |
| 4583 | NULL/*data*/, vdataHi/*vdata*/, |
| 4584 | expd_eq_old/*guard for store*/ ); |
| 4585 | do_shadow_Store( mce, cas->end, cas->addr, memOffsLo/*bias*/, |
| 4586 | NULL/*data*/, vdataLo/*vdata*/, |
| 4587 | expd_eq_old/*guard for store*/ ); |
| 4588 | if (otrak) { |
| 4589 | gen_store_b( mce, elemSzB, cas->addr, memOffsHi/*offset*/, |
| 4590 | bdataHi/*bdata*/, |
| 4591 | expd_eq_old/*guard for store*/ ); |
| 4592 | gen_store_b( mce, elemSzB, cas->addr, memOffsLo/*offset*/, |
| 4593 | bdataLo/*bdata*/, |
| 4594 | expd_eq_old/*guard for store*/ ); |
| 4595 | } |
| 4596 | } |
| 4597 | |
| 4598 | |
sewardj | db5907d | 2009-11-26 17:20:21 +0000 | [diff] [blame] | 4599 | /* ------ Dealing with LL/SC (not difficult) ------ */ |
| 4600 | |
| 4601 | static void do_shadow_LLSC ( MCEnv* mce, |
| 4602 | IREndness stEnd, |
| 4603 | IRTemp stResult, |
| 4604 | IRExpr* stAddr, |
| 4605 | IRExpr* stStoredata ) |
| 4606 | { |
| 4607 | /* In short: treat a load-linked like a normal load followed by an |
| 4608 | assignment of the loaded (shadow) data to the result temporary. |
| 4609 | Treat a store-conditional like a normal store, and mark the |
| 4610 | result temporary as defined. */ |
| 4611 | IRType resTy = typeOfIRTemp(mce->sb->tyenv, stResult); |
| 4612 | IRTemp resTmp = findShadowTmpV(mce, stResult); |
| 4613 | |
| 4614 | tl_assert(isIRAtom(stAddr)); |
| 4615 | if (stStoredata) |
| 4616 | tl_assert(isIRAtom(stStoredata)); |
| 4617 | |
| 4618 | if (stStoredata == NULL) { |
| 4619 | /* Load Linked */ |
| 4620 | /* Just treat this as a normal load, followed by an assignment of |
| 4621 | the value to .result. */ |
| 4622 | /* Stay sane */ |
| 4623 | tl_assert(resTy == Ity_I64 || resTy == Ity_I32 |
| 4624 | || resTy == Ity_I16 || resTy == Ity_I8); |
| 4625 | assign( 'V', mce, resTmp, |
| 4626 | expr2vbits_Load( |
| 4627 | mce, stEnd, resTy, stAddr, 0/*addr bias*/)); |
| 4628 | } else { |
| 4629 | /* Store Conditional */ |
| 4630 | /* Stay sane */ |
| 4631 | IRType dataTy = typeOfIRExpr(mce->sb->tyenv, |
| 4632 | stStoredata); |
| 4633 | tl_assert(dataTy == Ity_I64 || dataTy == Ity_I32 |
| 4634 | || dataTy == Ity_I16 || dataTy == Ity_I8); |
| 4635 | do_shadow_Store( mce, stEnd, |
| 4636 | stAddr, 0/* addr bias */, |
| 4637 | stStoredata, |
| 4638 | NULL /* shadow data */, |
| 4639 | NULL/*guard*/ ); |
| 4640 | /* This is a store conditional, so it writes to .result a value |
| 4641 | indicating whether or not the store succeeded. Just claim |
| 4642 | this value is always defined. In the PowerPC interpretation |
| 4643 | of store-conditional, definedness of the success indication |
| 4644 | depends on whether the address of the store matches the |
| 4645 | reservation address. But we can't tell that here (and |
| 4646 | anyway, we're not being PowerPC-specific). At least we are |
| 4647 | guaranteed that the definedness of the store address, and its |
| 4648 | addressibility, will be checked as per normal. So it seems |
| 4649 | pretty safe to just say that the success indication is always |
| 4650 | defined. |
| 4651 | |
| 4652 | In schemeS, for origin tracking, we must correspondingly set |
| 4653 | a no-origin value for the origin shadow of .result. |
| 4654 | */ |
| 4655 | tl_assert(resTy == Ity_I1); |
| 4656 | assign( 'V', mce, resTmp, definedOfType(resTy) ); |
| 4657 | } |
| 4658 | } |
| 4659 | |
| 4660 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 4661 | /*------------------------------------------------------------*/ |
| 4662 | /*--- Memcheck main ---*/ |
| 4663 | /*------------------------------------------------------------*/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 4664 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 4665 | static void schemeS ( MCEnv* mce, IRStmt* st ); |
| 4666 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 4667 | static Bool isBogusAtom ( IRAtom* at ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 4668 | { |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 4669 | ULong n = 0; |
| 4670 | IRConst* con; |
sewardj | 710d6c2 | 2005-03-20 18:55:15 +0000 | [diff] [blame] | 4671 | tl_assert(isIRAtom(at)); |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 4672 | if (at->tag == Iex_RdTmp) |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 4673 | return False; |
| 4674 | tl_assert(at->tag == Iex_Const); |
| 4675 | con = at->Iex.Const.con; |
| 4676 | switch (con->tag) { |
sewardj | d5204dc | 2004-12-31 01:16:11 +0000 | [diff] [blame] | 4677 | case Ico_U1: return False; |
| 4678 | case Ico_U8: n = (ULong)con->Ico.U8; break; |
| 4679 | case Ico_U16: n = (ULong)con->Ico.U16; break; |
| 4680 | case Ico_U32: n = (ULong)con->Ico.U32; break; |
| 4681 | case Ico_U64: n = (ULong)con->Ico.U64; break; |
| 4682 | case Ico_F64: return False; |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 4683 | case Ico_F32i: return False; |
sewardj | d5204dc | 2004-12-31 01:16:11 +0000 | [diff] [blame] | 4684 | case Ico_F64i: return False; |
| 4685 | case Ico_V128: return False; |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 4686 | default: ppIRExpr(at); tl_assert(0); |
| 4687 | } |
| 4688 | /* VG_(printf)("%llx\n", n); */ |
sewardj | 96a922e | 2005-04-23 23:26:29 +0000 | [diff] [blame] | 4689 | return (/*32*/ n == 0xFEFEFEFFULL |
| 4690 | /*32*/ || n == 0x80808080ULL |
sewardj | 17b4743 | 2008-12-17 01:12:58 +0000 | [diff] [blame] | 4691 | /*32*/ || n == 0x7F7F7F7FULL |
tom | d9774d7 | 2005-06-27 08:11:01 +0000 | [diff] [blame] | 4692 | /*64*/ || n == 0xFFFFFFFFFEFEFEFFULL |
sewardj | 96a922e | 2005-04-23 23:26:29 +0000 | [diff] [blame] | 4693 | /*64*/ || n == 0xFEFEFEFEFEFEFEFFULL |
tom | d9774d7 | 2005-06-27 08:11:01 +0000 | [diff] [blame] | 4694 | /*64*/ || n == 0x0000000000008080ULL |
sewardj | 96a922e | 2005-04-23 23:26:29 +0000 | [diff] [blame] | 4695 | /*64*/ || n == 0x8080808080808080ULL |
sewardj | 17b4743 | 2008-12-17 01:12:58 +0000 | [diff] [blame] | 4696 | /*64*/ || n == 0x0101010101010101ULL |
sewardj | 96a922e | 2005-04-23 23:26:29 +0000 | [diff] [blame] | 4697 | ); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 4698 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 4699 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 4700 | static Bool checkForBogusLiterals ( /*FLAT*/ IRStmt* st ) |
| 4701 | { |
sewardj | d5204dc | 2004-12-31 01:16:11 +0000 | [diff] [blame] | 4702 | Int i; |
| 4703 | IRExpr* e; |
| 4704 | IRDirty* d; |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 4705 | IRCAS* cas; |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 4706 | switch (st->tag) { |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 4707 | case Ist_WrTmp: |
| 4708 | e = st->Ist.WrTmp.data; |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 4709 | switch (e->tag) { |
| 4710 | case Iex_Get: |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 4711 | case Iex_RdTmp: |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 4712 | return False; |
sewardj | d5204dc | 2004-12-31 01:16:11 +0000 | [diff] [blame] | 4713 | case Iex_Const: |
| 4714 | return isBogusAtom(e); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 4715 | case Iex_Unop: |
| 4716 | return isBogusAtom(e->Iex.Unop.arg); |
sewardj | d5204dc | 2004-12-31 01:16:11 +0000 | [diff] [blame] | 4717 | case Iex_GetI: |
| 4718 | return isBogusAtom(e->Iex.GetI.ix); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 4719 | case Iex_Binop: |
| 4720 | return isBogusAtom(e->Iex.Binop.arg1) |
| 4721 | || isBogusAtom(e->Iex.Binop.arg2); |
sewardj | ed69fdb | 2006-02-03 16:12:27 +0000 | [diff] [blame] | 4722 | case Iex_Triop: |
| 4723 | return isBogusAtom(e->Iex.Triop.arg1) |
| 4724 | || isBogusAtom(e->Iex.Triop.arg2) |
| 4725 | || isBogusAtom(e->Iex.Triop.arg3); |
sewardj | e91cea7 | 2006-02-08 19:32:02 +0000 | [diff] [blame] | 4726 | case Iex_Qop: |
| 4727 | return isBogusAtom(e->Iex.Qop.arg1) |
| 4728 | || isBogusAtom(e->Iex.Qop.arg2) |
| 4729 | || isBogusAtom(e->Iex.Qop.arg3) |
| 4730 | || isBogusAtom(e->Iex.Qop.arg4); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 4731 | case Iex_Mux0X: |
| 4732 | return isBogusAtom(e->Iex.Mux0X.cond) |
| 4733 | || isBogusAtom(e->Iex.Mux0X.expr0) |
| 4734 | || isBogusAtom(e->Iex.Mux0X.exprX); |
sewardj | 2e59585 | 2005-06-30 23:33:37 +0000 | [diff] [blame] | 4735 | case Iex_Load: |
| 4736 | return isBogusAtom(e->Iex.Load.addr); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 4737 | case Iex_CCall: |
| 4738 | for (i = 0; e->Iex.CCall.args[i]; i++) |
| 4739 | if (isBogusAtom(e->Iex.CCall.args[i])) |
| 4740 | return True; |
| 4741 | return False; |
| 4742 | default: |
| 4743 | goto unhandled; |
| 4744 | } |
sewardj | d5204dc | 2004-12-31 01:16:11 +0000 | [diff] [blame] | 4745 | case Ist_Dirty: |
| 4746 | d = st->Ist.Dirty.details; |
| 4747 | for (i = 0; d->args[i]; i++) |
| 4748 | if (isBogusAtom(d->args[i])) |
| 4749 | return True; |
| 4750 | if (d->guard && isBogusAtom(d->guard)) |
| 4751 | return True; |
| 4752 | if (d->mAddr && isBogusAtom(d->mAddr)) |
| 4753 | return True; |
| 4754 | return False; |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 4755 | case Ist_Put: |
| 4756 | return isBogusAtom(st->Ist.Put.data); |
sewardj | d5204dc | 2004-12-31 01:16:11 +0000 | [diff] [blame] | 4757 | case Ist_PutI: |
| 4758 | return isBogusAtom(st->Ist.PutI.ix) |
| 4759 | || isBogusAtom(st->Ist.PutI.data); |
sewardj | 2e59585 | 2005-06-30 23:33:37 +0000 | [diff] [blame] | 4760 | case Ist_Store: |
| 4761 | return isBogusAtom(st->Ist.Store.addr) |
| 4762 | || isBogusAtom(st->Ist.Store.data); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 4763 | case Ist_Exit: |
sewardj | d5204dc | 2004-12-31 01:16:11 +0000 | [diff] [blame] | 4764 | return isBogusAtom(st->Ist.Exit.guard); |
sewardj | 826ec49 | 2005-05-12 18:05:00 +0000 | [diff] [blame] | 4765 | case Ist_AbiHint: |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 4766 | return isBogusAtom(st->Ist.AbiHint.base) |
| 4767 | || isBogusAtom(st->Ist.AbiHint.nia); |
sewardj | 21dc345 | 2005-03-21 00:27:41 +0000 | [diff] [blame] | 4768 | case Ist_NoOp: |
sewardj | 29faa50 | 2005-03-16 18:20:21 +0000 | [diff] [blame] | 4769 | case Ist_IMark: |
sewardj | 72d7513 | 2007-11-09 23:06:35 +0000 | [diff] [blame] | 4770 | case Ist_MBE: |
sewardj | bd598e1 | 2005-01-07 12:10:21 +0000 | [diff] [blame] | 4771 | return False; |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 4772 | case Ist_CAS: |
| 4773 | cas = st->Ist.CAS.details; |
| 4774 | return isBogusAtom(cas->addr) |
| 4775 | || (cas->expdHi ? isBogusAtom(cas->expdHi) : False) |
| 4776 | || isBogusAtom(cas->expdLo) |
| 4777 | || (cas->dataHi ? isBogusAtom(cas->dataHi) : False) |
| 4778 | || isBogusAtom(cas->dataLo); |
sewardj | db5907d | 2009-11-26 17:20:21 +0000 | [diff] [blame] | 4779 | case Ist_LLSC: |
| 4780 | return isBogusAtom(st->Ist.LLSC.addr) |
| 4781 | || (st->Ist.LLSC.storedata |
| 4782 | ? isBogusAtom(st->Ist.LLSC.storedata) |
| 4783 | : False); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 4784 | default: |
| 4785 | unhandled: |
| 4786 | ppIRStmt(st); |
| 4787 | VG_(tool_panic)("hasBogusLiterals"); |
| 4788 | } |
| 4789 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 4790 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 4791 | |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 4792 | IRSB* MC_(instrument) ( VgCallbackClosure* closure, |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 4793 | IRSB* sb_in, |
sewardj | 461df9c | 2006-01-17 02:06:39 +0000 | [diff] [blame] | 4794 | VexGuestLayout* layout, |
| 4795 | VexGuestExtents* vge, |
sewardj | d54babf | 2005-03-21 00:55:49 +0000 | [diff] [blame] | 4796 | IRType gWordTy, IRType hWordTy ) |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 4797 | { |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 4798 | Bool verboze = 0||False; |
sewardj | 151b90d | 2005-07-06 19:42:23 +0000 | [diff] [blame] | 4799 | Bool bogus; |
sewardj | d5204dc | 2004-12-31 01:16:11 +0000 | [diff] [blame] | 4800 | Int i, j, first_stmt; |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 4801 | IRStmt* st; |
sewardj | d5204dc | 2004-12-31 01:16:11 +0000 | [diff] [blame] | 4802 | MCEnv mce; |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 4803 | IRSB* sb_out; |
sewardj | d54babf | 2005-03-21 00:55:49 +0000 | [diff] [blame] | 4804 | |
| 4805 | if (gWordTy != hWordTy) { |
| 4806 | /* We don't currently support this case. */ |
| 4807 | VG_(tool_panic)("host/guest word size mismatch"); |
| 4808 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 4809 | |
sewardj | 6cf40ff | 2005-04-20 22:31:26 +0000 | [diff] [blame] | 4810 | /* Check we're not completely nuts */ |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 4811 | tl_assert(sizeof(UWord) == sizeof(void*)); |
| 4812 | tl_assert(sizeof(Word) == sizeof(void*)); |
| 4813 | tl_assert(sizeof(Addr) == sizeof(void*)); |
| 4814 | tl_assert(sizeof(ULong) == 8); |
| 4815 | tl_assert(sizeof(Long) == 8); |
| 4816 | tl_assert(sizeof(Addr64) == 8); |
| 4817 | tl_assert(sizeof(UInt) == 4); |
| 4818 | tl_assert(sizeof(Int) == 4); |
| 4819 | |
| 4820 | tl_assert(MC_(clo_mc_level) >= 1 && MC_(clo_mc_level) <= 3); |
sewardj | 6cf40ff | 2005-04-20 22:31:26 +0000 | [diff] [blame] | 4821 | |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 4822 | /* Set up SB */ |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 4823 | sb_out = deepCopyIRSBExceptStmts(sb_in); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 4824 | |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 4825 | /* Set up the running environment. Both .sb and .tmpMap are |
| 4826 | modified as we go along. Note that tmps are added to both |
| 4827 | .sb->tyenv and .tmpMap together, so the valid index-set for |
| 4828 | those two arrays should always be identical. */ |
| 4829 | VG_(memset)(&mce, 0, sizeof(mce)); |
| 4830 | mce.sb = sb_out; |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 4831 | mce.trace = verboze; |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 4832 | mce.layout = layout; |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 4833 | mce.hWordTy = hWordTy; |
sewardj | d5204dc | 2004-12-31 01:16:11 +0000 | [diff] [blame] | 4834 | mce.bogusLiterals = False; |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 4835 | |
| 4836 | mce.tmpMap = VG_(newXA)( VG_(malloc), "mc.MC_(instrument).1", VG_(free), |
| 4837 | sizeof(TempMapEnt)); |
| 4838 | for (i = 0; i < sb_in->tyenv->types_used; i++) { |
| 4839 | TempMapEnt ent; |
| 4840 | ent.kind = Orig; |
| 4841 | ent.shadowV = IRTemp_INVALID; |
| 4842 | ent.shadowB = IRTemp_INVALID; |
| 4843 | VG_(addToXA)( mce.tmpMap, &ent ); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 4844 | } |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 4845 | tl_assert( VG_(sizeXA)( mce.tmpMap ) == sb_in->tyenv->types_used ); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 4846 | |
sewardj | 151b90d | 2005-07-06 19:42:23 +0000 | [diff] [blame] | 4847 | /* Make a preliminary inspection of the statements, to see if there |
| 4848 | are any dodgy-looking literals. If there are, we generate |
| 4849 | extra-detailed (hence extra-expensive) instrumentation in |
| 4850 | places. Scan the whole bb even if dodgyness is found earlier, |
| 4851 | so that the flatness assertion is applied to all stmts. */ |
| 4852 | |
| 4853 | bogus = False; |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 4854 | |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 4855 | for (i = 0; i < sb_in->stmts_used; i++) { |
sewardj | 151b90d | 2005-07-06 19:42:23 +0000 | [diff] [blame] | 4856 | |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 4857 | st = sb_in->stmts[i]; |
sewardj | 21dc345 | 2005-03-21 00:27:41 +0000 | [diff] [blame] | 4858 | tl_assert(st); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 4859 | tl_assert(isFlatIRStmt(st)); |
| 4860 | |
sewardj | 151b90d | 2005-07-06 19:42:23 +0000 | [diff] [blame] | 4861 | if (!bogus) { |
| 4862 | bogus = checkForBogusLiterals(st); |
| 4863 | if (0 && bogus) { |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 4864 | VG_(printf)("bogus: "); |
| 4865 | ppIRStmt(st); |
| 4866 | VG_(printf)("\n"); |
| 4867 | } |
| 4868 | } |
sewardj | d5204dc | 2004-12-31 01:16:11 +0000 | [diff] [blame] | 4869 | |
sewardj | 151b90d | 2005-07-06 19:42:23 +0000 | [diff] [blame] | 4870 | } |
| 4871 | |
| 4872 | mce.bogusLiterals = bogus; |
| 4873 | |
sewardj | a087148 | 2006-10-18 12:41:55 +0000 | [diff] [blame] | 4874 | /* Copy verbatim any IR preamble preceding the first IMark */ |
sewardj | 151b90d | 2005-07-06 19:42:23 +0000 | [diff] [blame] | 4875 | |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 4876 | tl_assert(mce.sb == sb_out); |
| 4877 | tl_assert(mce.sb != sb_in); |
sewardj | f1962d3 | 2006-10-19 13:22:16 +0000 | [diff] [blame] | 4878 | |
sewardj | a087148 | 2006-10-18 12:41:55 +0000 | [diff] [blame] | 4879 | i = 0; |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 4880 | while (i < sb_in->stmts_used && sb_in->stmts[i]->tag != Ist_IMark) { |
sewardj | a087148 | 2006-10-18 12:41:55 +0000 | [diff] [blame] | 4881 | |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 4882 | st = sb_in->stmts[i]; |
sewardj | a087148 | 2006-10-18 12:41:55 +0000 | [diff] [blame] | 4883 | tl_assert(st); |
| 4884 | tl_assert(isFlatIRStmt(st)); |
| 4885 | |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 4886 | stmt( 'C', &mce, sb_in->stmts[i] ); |
sewardj | a087148 | 2006-10-18 12:41:55 +0000 | [diff] [blame] | 4887 | i++; |
| 4888 | } |
| 4889 | |
sewardj | f1962d3 | 2006-10-19 13:22:16 +0000 | [diff] [blame] | 4890 | /* Nasty problem. IR optimisation of the pre-instrumented IR may |
| 4891 | cause the IR following the preamble to contain references to IR |
| 4892 | temporaries defined in the preamble. Because the preamble isn't |
| 4893 | instrumented, these temporaries don't have any shadows. |
| 4894 | Nevertheless uses of them following the preamble will cause |
| 4895 | memcheck to generate references to their shadows. End effect is |
| 4896 | to cause IR sanity check failures, due to references to |
| 4897 | non-existent shadows. This is only evident for the complex |
| 4898 | preambles used for function wrapping on TOC-afflicted platforms |
| 4899 | (ppc64-linux, ppc32-aix5, ppc64-aix5). |
| 4900 | |
| 4901 | The following loop therefore scans the preamble looking for |
| 4902 | assignments to temporaries. For each one found it creates an |
sewardj | afa617b | 2008-07-22 09:59:48 +0000 | [diff] [blame] | 4903 | assignment to the corresponding (V) shadow temp, marking it as |
sewardj | f1962d3 | 2006-10-19 13:22:16 +0000 | [diff] [blame] | 4904 | 'defined'. This is the same resulting IR as if the main |
| 4905 | instrumentation loop before had been applied to the statement |
| 4906 | 'tmp = CONSTANT'. |
sewardj | afa617b | 2008-07-22 09:59:48 +0000 | [diff] [blame] | 4907 | |
| 4908 | Similarly, if origin tracking is enabled, we must generate an |
| 4909 | assignment for the corresponding origin (B) shadow, claiming |
| 4910 | no-origin, as appropriate for a defined value. |
sewardj | f1962d3 | 2006-10-19 13:22:16 +0000 | [diff] [blame] | 4911 | */ |
| 4912 | for (j = 0; j < i; j++) { |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 4913 | if (sb_in->stmts[j]->tag == Ist_WrTmp) { |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 4914 | /* findShadowTmpV checks its arg is an original tmp; |
sewardj | f1962d3 | 2006-10-19 13:22:16 +0000 | [diff] [blame] | 4915 | no need to assert that here. */ |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 4916 | IRTemp tmp_o = sb_in->stmts[j]->Ist.WrTmp.tmp; |
sewardj | afa617b | 2008-07-22 09:59:48 +0000 | [diff] [blame] | 4917 | IRTemp tmp_v = findShadowTmpV(&mce, tmp_o); |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 4918 | IRType ty_v = typeOfIRTemp(sb_out->tyenv, tmp_v); |
sewardj | afa617b | 2008-07-22 09:59:48 +0000 | [diff] [blame] | 4919 | assign( 'V', &mce, tmp_v, definedOfType( ty_v ) ); |
| 4920 | if (MC_(clo_mc_level) == 3) { |
| 4921 | IRTemp tmp_b = findShadowTmpB(&mce, tmp_o); |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 4922 | tl_assert(typeOfIRTemp(sb_out->tyenv, tmp_b) == Ity_I32); |
sewardj | afa617b | 2008-07-22 09:59:48 +0000 | [diff] [blame] | 4923 | assign( 'B', &mce, tmp_b, mkU32(0)/* UNKNOWN ORIGIN */); |
| 4924 | } |
sewardj | f1962d3 | 2006-10-19 13:22:16 +0000 | [diff] [blame] | 4925 | if (0) { |
sewardj | afa617b | 2008-07-22 09:59:48 +0000 | [diff] [blame] | 4926 | VG_(printf)("create shadow tmp(s) for preamble tmp [%d] ty ", j); |
| 4927 | ppIRType( ty_v ); |
sewardj | f1962d3 | 2006-10-19 13:22:16 +0000 | [diff] [blame] | 4928 | VG_(printf)("\n"); |
| 4929 | } |
| 4930 | } |
| 4931 | } |
| 4932 | |
sewardj | a087148 | 2006-10-18 12:41:55 +0000 | [diff] [blame] | 4933 | /* Iterate over the remaining stmts to generate instrumentation. */ |
| 4934 | |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 4935 | tl_assert(sb_in->stmts_used > 0); |
sewardj | a087148 | 2006-10-18 12:41:55 +0000 | [diff] [blame] | 4936 | tl_assert(i >= 0); |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 4937 | tl_assert(i < sb_in->stmts_used); |
| 4938 | tl_assert(sb_in->stmts[i]->tag == Ist_IMark); |
sewardj | a087148 | 2006-10-18 12:41:55 +0000 | [diff] [blame] | 4939 | |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 4940 | for (/* use current i*/; i < sb_in->stmts_used; i++) { |
sewardj | 151b90d | 2005-07-06 19:42:23 +0000 | [diff] [blame] | 4941 | |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 4942 | st = sb_in->stmts[i]; |
| 4943 | first_stmt = sb_out->stmts_used; |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 4944 | |
| 4945 | if (verboze) { |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 4946 | VG_(printf)("\n"); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 4947 | ppIRStmt(st); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 4948 | VG_(printf)("\n"); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 4949 | } |
| 4950 | |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 4951 | if (MC_(clo_mc_level) == 3) { |
| 4952 | /* See comments on case Ist_CAS below. */ |
| 4953 | if (st->tag != Ist_CAS) |
| 4954 | schemeS( &mce, st ); |
| 4955 | } |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 4956 | |
sewardj | 29faa50 | 2005-03-16 18:20:21 +0000 | [diff] [blame] | 4957 | /* Generate instrumentation code for each stmt ... */ |
| 4958 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 4959 | switch (st->tag) { |
| 4960 | |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 4961 | case Ist_WrTmp: |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 4962 | assign( 'V', &mce, findShadowTmpV(&mce, st->Ist.WrTmp.tmp), |
| 4963 | expr2vbits( &mce, st->Ist.WrTmp.data) ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 4964 | break; |
| 4965 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 4966 | case Ist_Put: |
| 4967 | do_shadow_PUT( &mce, |
| 4968 | st->Ist.Put.offset, |
| 4969 | st->Ist.Put.data, |
| 4970 | NULL /* shadow atom */ ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 4971 | break; |
| 4972 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 4973 | case Ist_PutI: |
| 4974 | do_shadow_PUTI( &mce, |
| 4975 | st->Ist.PutI.descr, |
| 4976 | st->Ist.PutI.ix, |
| 4977 | st->Ist.PutI.bias, |
| 4978 | st->Ist.PutI.data ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 4979 | break; |
| 4980 | |
sewardj | 2e59585 | 2005-06-30 23:33:37 +0000 | [diff] [blame] | 4981 | case Ist_Store: |
| 4982 | do_shadow_Store( &mce, st->Ist.Store.end, |
| 4983 | st->Ist.Store.addr, 0/* addr bias */, |
| 4984 | st->Ist.Store.data, |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 4985 | NULL /* shadow data */, |
| 4986 | NULL/*guard*/ ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 4987 | break; |
| 4988 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 4989 | case Ist_Exit: |
sewardj | d5204dc | 2004-12-31 01:16:11 +0000 | [diff] [blame] | 4990 | complainIfUndefined( &mce, st->Ist.Exit.guard ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 4991 | break; |
| 4992 | |
sewardj | 29faa50 | 2005-03-16 18:20:21 +0000 | [diff] [blame] | 4993 | case Ist_IMark: |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 4994 | break; |
| 4995 | |
| 4996 | case Ist_NoOp: |
sewardj | 72d7513 | 2007-11-09 23:06:35 +0000 | [diff] [blame] | 4997 | case Ist_MBE: |
sewardj | bd598e1 | 2005-01-07 12:10:21 +0000 | [diff] [blame] | 4998 | break; |
| 4999 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 5000 | case Ist_Dirty: |
| 5001 | do_shadow_Dirty( &mce, st->Ist.Dirty.details ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 5002 | break; |
| 5003 | |
sewardj | 826ec49 | 2005-05-12 18:05:00 +0000 | [diff] [blame] | 5004 | case Ist_AbiHint: |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 5005 | do_AbiHint( &mce, st->Ist.AbiHint.base, |
| 5006 | st->Ist.AbiHint.len, |
| 5007 | st->Ist.AbiHint.nia ); |
sewardj | 826ec49 | 2005-05-12 18:05:00 +0000 | [diff] [blame] | 5008 | break; |
| 5009 | |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 5010 | case Ist_CAS: |
| 5011 | do_shadow_CAS( &mce, st->Ist.CAS.details ); |
| 5012 | /* Note, do_shadow_CAS copies the CAS itself to the output |
| 5013 | block, because it needs to add instrumentation both |
| 5014 | before and after it. Hence skip the copy below. Also |
| 5015 | skip the origin-tracking stuff (call to schemeS) above, |
| 5016 | since that's all tangled up with it too; do_shadow_CAS |
| 5017 | does it all. */ |
| 5018 | break; |
| 5019 | |
sewardj | db5907d | 2009-11-26 17:20:21 +0000 | [diff] [blame] | 5020 | case Ist_LLSC: |
| 5021 | do_shadow_LLSC( &mce, |
| 5022 | st->Ist.LLSC.end, |
| 5023 | st->Ist.LLSC.result, |
| 5024 | st->Ist.LLSC.addr, |
| 5025 | st->Ist.LLSC.storedata ); |
| 5026 | break; |
| 5027 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 5028 | default: |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 5029 | VG_(printf)("\n"); |
| 5030 | ppIRStmt(st); |
| 5031 | VG_(printf)("\n"); |
| 5032 | VG_(tool_panic)("memcheck: unhandled IRStmt"); |
| 5033 | |
| 5034 | } /* switch (st->tag) */ |
| 5035 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 5036 | if (0 && verboze) { |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 5037 | for (j = first_stmt; j < sb_out->stmts_used; j++) { |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 5038 | VG_(printf)(" "); |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 5039 | ppIRStmt(sb_out->stmts[j]); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 5040 | VG_(printf)("\n"); |
| 5041 | } |
| 5042 | VG_(printf)("\n"); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 5043 | } |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 5044 | |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 5045 | /* ... and finally copy the stmt itself to the output. Except, |
| 5046 | skip the copy of IRCASs; see comments on case Ist_CAS |
| 5047 | above. */ |
| 5048 | if (st->tag != Ist_CAS) |
| 5049 | stmt('C', &mce, st); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 5050 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 5051 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 5052 | /* Now we need to complain if the jump target is undefined. */ |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 5053 | first_stmt = sb_out->stmts_used; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 5054 | |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 5055 | if (verboze) { |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 5056 | VG_(printf)("sb_in->next = "); |
| 5057 | ppIRExpr(sb_in->next); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 5058 | VG_(printf)("\n\n"); |
| 5059 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 5060 | |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 5061 | complainIfUndefined( &mce, sb_in->next ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 5062 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 5063 | if (0 && verboze) { |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 5064 | for (j = first_stmt; j < sb_out->stmts_used; j++) { |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 5065 | VG_(printf)(" "); |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 5066 | ppIRStmt(sb_out->stmts[j]); |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 5067 | VG_(printf)("\n"); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 5068 | } |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 5069 | VG_(printf)("\n"); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 5070 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 5071 | |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 5072 | /* If this fails, there's been some serious snafu with tmp management, |
| 5073 | that should be investigated. */ |
| 5074 | tl_assert( VG_(sizeXA)( mce.tmpMap ) == mce.sb->tyenv->types_used ); |
| 5075 | VG_(deleteXA)( mce.tmpMap ); |
| 5076 | |
| 5077 | tl_assert(mce.sb == sb_out); |
| 5078 | return sb_out; |
sewardj | 9544807 | 2004-11-22 20:19:51 +0000 | [diff] [blame] | 5079 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 5080 | |
sewardj | 81651dc | 2007-08-28 06:05:20 +0000 | [diff] [blame] | 5081 | /*------------------------------------------------------------*/ |
| 5082 | /*--- Post-tree-build final tidying ---*/ |
| 5083 | /*------------------------------------------------------------*/ |
| 5084 | |
| 5085 | /* This exploits the observation that Memcheck often produces |
| 5086 | repeated conditional calls of the form |
| 5087 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 5088 | Dirty G MC_(helperc_value_check0/1/4/8_fail)(UInt otag) |
sewardj | 81651dc | 2007-08-28 06:05:20 +0000 | [diff] [blame] | 5089 | |
| 5090 | with the same guard expression G guarding the same helper call. |
| 5091 | The second and subsequent calls are redundant. This usually |
| 5092 | results from instrumentation of guest code containing multiple |
| 5093 | memory references at different constant offsets from the same base |
| 5094 | register. After optimisation of the instrumentation, you get a |
| 5095 | test for the definedness of the base register for each memory |
| 5096 | reference, which is kinda pointless. MC_(final_tidy) therefore |
| 5097 | looks for such repeated calls and removes all but the first. */ |
| 5098 | |
| 5099 | /* A struct for recording which (helper, guard) pairs we have already |
| 5100 | seen. */ |
| 5101 | typedef |
| 5102 | struct { void* entry; IRExpr* guard; } |
| 5103 | Pair; |
| 5104 | |
| 5105 | /* Return True if e1 and e2 definitely denote the same value (used to |
| 5106 | compare guards). Return False if unknown; False is the safe |
| 5107 | answer. Since guest registers and guest memory do not have the |
| 5108 | SSA property we must return False if any Gets or Loads appear in |
| 5109 | the expression. */ |
| 5110 | |
| 5111 | static Bool sameIRValue ( IRExpr* e1, IRExpr* e2 ) |
| 5112 | { |
| 5113 | if (e1->tag != e2->tag) |
| 5114 | return False; |
| 5115 | switch (e1->tag) { |
| 5116 | case Iex_Const: |
| 5117 | return eqIRConst( e1->Iex.Const.con, e2->Iex.Const.con ); |
| 5118 | case Iex_Binop: |
| 5119 | return e1->Iex.Binop.op == e2->Iex.Binop.op |
| 5120 | && sameIRValue(e1->Iex.Binop.arg1, e2->Iex.Binop.arg1) |
| 5121 | && sameIRValue(e1->Iex.Binop.arg2, e2->Iex.Binop.arg2); |
| 5122 | case Iex_Unop: |
| 5123 | return e1->Iex.Unop.op == e2->Iex.Unop.op |
| 5124 | && sameIRValue(e1->Iex.Unop.arg, e2->Iex.Unop.arg); |
| 5125 | case Iex_RdTmp: |
| 5126 | return e1->Iex.RdTmp.tmp == e2->Iex.RdTmp.tmp; |
| 5127 | case Iex_Mux0X: |
| 5128 | return sameIRValue( e1->Iex.Mux0X.cond, e2->Iex.Mux0X.cond ) |
| 5129 | && sameIRValue( e1->Iex.Mux0X.expr0, e2->Iex.Mux0X.expr0 ) |
| 5130 | && sameIRValue( e1->Iex.Mux0X.exprX, e2->Iex.Mux0X.exprX ); |
| 5131 | case Iex_Qop: |
| 5132 | case Iex_Triop: |
| 5133 | case Iex_CCall: |
| 5134 | /* be lazy. Could define equality for these, but they never |
| 5135 | appear to be used. */ |
| 5136 | return False; |
| 5137 | case Iex_Get: |
| 5138 | case Iex_GetI: |
| 5139 | case Iex_Load: |
| 5140 | /* be conservative - these may not give the same value each |
| 5141 | time */ |
| 5142 | return False; |
| 5143 | case Iex_Binder: |
| 5144 | /* should never see this */ |
| 5145 | /* fallthrough */ |
| 5146 | default: |
| 5147 | VG_(printf)("mc_translate.c: sameIRValue: unhandled: "); |
| 5148 | ppIRExpr(e1); |
| 5149 | VG_(tool_panic)("memcheck:sameIRValue"); |
| 5150 | return False; |
| 5151 | } |
| 5152 | } |
| 5153 | |
| 5154 | /* See if 'pairs' already has an entry for (entry, guard). Return |
| 5155 | True if so. If not, add an entry. */ |
| 5156 | |
| 5157 | static |
| 5158 | Bool check_or_add ( XArray* /*of Pair*/ pairs, IRExpr* guard, void* entry ) |
| 5159 | { |
| 5160 | Pair p; |
| 5161 | Pair* pp; |
| 5162 | Int i, n = VG_(sizeXA)( pairs ); |
| 5163 | for (i = 0; i < n; i++) { |
| 5164 | pp = VG_(indexXA)( pairs, i ); |
| 5165 | if (pp->entry == entry && sameIRValue(pp->guard, guard)) |
| 5166 | return True; |
| 5167 | } |
| 5168 | p.guard = guard; |
| 5169 | p.entry = entry; |
| 5170 | VG_(addToXA)( pairs, &p ); |
| 5171 | return False; |
| 5172 | } |
| 5173 | |
| 5174 | static Bool is_helperc_value_checkN_fail ( HChar* name ) |
| 5175 | { |
| 5176 | return |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 5177 | 0==VG_(strcmp)(name, "MC_(helperc_value_check0_fail_no_o)") |
| 5178 | || 0==VG_(strcmp)(name, "MC_(helperc_value_check1_fail_no_o)") |
| 5179 | || 0==VG_(strcmp)(name, "MC_(helperc_value_check4_fail_no_o)") |
| 5180 | || 0==VG_(strcmp)(name, "MC_(helperc_value_check8_fail_no_o)") |
| 5181 | || 0==VG_(strcmp)(name, "MC_(helperc_value_check0_fail_w_o)") |
| 5182 | || 0==VG_(strcmp)(name, "MC_(helperc_value_check1_fail_w_o)") |
| 5183 | || 0==VG_(strcmp)(name, "MC_(helperc_value_check4_fail_w_o)") |
| 5184 | || 0==VG_(strcmp)(name, "MC_(helperc_value_check8_fail_w_o)"); |
sewardj | 81651dc | 2007-08-28 06:05:20 +0000 | [diff] [blame] | 5185 | } |
| 5186 | |
| 5187 | IRSB* MC_(final_tidy) ( IRSB* sb_in ) |
| 5188 | { |
| 5189 | Int i; |
| 5190 | IRStmt* st; |
| 5191 | IRDirty* di; |
| 5192 | IRExpr* guard; |
| 5193 | IRCallee* cee; |
| 5194 | Bool alreadyPresent; |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 5195 | XArray* pairs = VG_(newXA)( VG_(malloc), "mc.ft.1", |
| 5196 | VG_(free), sizeof(Pair) ); |
sewardj | 81651dc | 2007-08-28 06:05:20 +0000 | [diff] [blame] | 5197 | /* Scan forwards through the statements. Each time a call to one |
| 5198 | of the relevant helpers is seen, check if we have made a |
| 5199 | previous call to the same helper using the same guard |
| 5200 | expression, and if so, delete the call. */ |
| 5201 | for (i = 0; i < sb_in->stmts_used; i++) { |
| 5202 | st = sb_in->stmts[i]; |
| 5203 | tl_assert(st); |
| 5204 | if (st->tag != Ist_Dirty) |
| 5205 | continue; |
| 5206 | di = st->Ist.Dirty.details; |
| 5207 | guard = di->guard; |
| 5208 | if (!guard) |
| 5209 | continue; |
| 5210 | if (0) { ppIRExpr(guard); VG_(printf)("\n"); } |
| 5211 | cee = di->cee; |
| 5212 | if (!is_helperc_value_checkN_fail( cee->name )) |
| 5213 | continue; |
| 5214 | /* Ok, we have a call to helperc_value_check0/1/4/8_fail with |
| 5215 | guard 'guard'. Check if we have already seen a call to this |
| 5216 | function with the same guard. If so, delete it. If not, |
| 5217 | add it to the set of calls we do know about. */ |
| 5218 | alreadyPresent = check_or_add( pairs, guard, cee->addr ); |
| 5219 | if (alreadyPresent) { |
| 5220 | sb_in->stmts[i] = IRStmt_NoOp(); |
| 5221 | if (0) VG_(printf)("XX\n"); |
| 5222 | } |
| 5223 | } |
| 5224 | VG_(deleteXA)( pairs ); |
| 5225 | return sb_in; |
| 5226 | } |
| 5227 | |
| 5228 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 5229 | /*------------------------------------------------------------*/ |
| 5230 | /*--- Origin tracking stuff ---*/ |
| 5231 | /*------------------------------------------------------------*/ |
| 5232 | |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 5233 | /* Almost identical to findShadowTmpV. */ |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 5234 | static IRTemp findShadowTmpB ( MCEnv* mce, IRTemp orig ) |
| 5235 | { |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 5236 | TempMapEnt* ent; |
| 5237 | /* VG_(indexXA) range-checks 'orig', hence no need to check |
| 5238 | here. */ |
| 5239 | ent = (TempMapEnt*)VG_(indexXA)( mce->tmpMap, (Word)orig ); |
| 5240 | tl_assert(ent->kind == Orig); |
| 5241 | if (ent->shadowB == IRTemp_INVALID) { |
| 5242 | IRTemp tmpB |
| 5243 | = newTemp( mce, Ity_I32, BSh ); |
| 5244 | /* newTemp may cause mce->tmpMap to resize, hence previous results |
| 5245 | from VG_(indexXA) are invalid. */ |
| 5246 | ent = (TempMapEnt*)VG_(indexXA)( mce->tmpMap, (Word)orig ); |
| 5247 | tl_assert(ent->kind == Orig); |
| 5248 | tl_assert(ent->shadowB == IRTemp_INVALID); |
| 5249 | ent->shadowB = tmpB; |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 5250 | } |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 5251 | return ent->shadowB; |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 5252 | } |
| 5253 | |
| 5254 | static IRAtom* gen_maxU32 ( MCEnv* mce, IRAtom* b1, IRAtom* b2 ) |
| 5255 | { |
| 5256 | return assignNew( 'B', mce, Ity_I32, binop(Iop_Max32U, b1, b2) ); |
| 5257 | } |
| 5258 | |
| 5259 | static IRAtom* gen_load_b ( MCEnv* mce, Int szB, |
| 5260 | IRAtom* baseaddr, Int offset ) |
| 5261 | { |
| 5262 | void* hFun; |
| 5263 | HChar* hName; |
| 5264 | IRTemp bTmp; |
| 5265 | IRDirty* di; |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 5266 | IRType aTy = typeOfIRExpr( mce->sb->tyenv, baseaddr ); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 5267 | IROp opAdd = aTy == Ity_I32 ? Iop_Add32 : Iop_Add64; |
| 5268 | IRAtom* ea = baseaddr; |
| 5269 | if (offset != 0) { |
| 5270 | IRAtom* off = aTy == Ity_I32 ? mkU32( offset ) |
| 5271 | : mkU64( (Long)(Int)offset ); |
| 5272 | ea = assignNew( 'B', mce, aTy, binop(opAdd, ea, off)); |
| 5273 | } |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 5274 | bTmp = newTemp(mce, mce->hWordTy, BSh); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 5275 | |
| 5276 | switch (szB) { |
| 5277 | case 1: hFun = (void*)&MC_(helperc_b_load1); |
| 5278 | hName = "MC_(helperc_b_load1)"; |
| 5279 | break; |
| 5280 | case 2: hFun = (void*)&MC_(helperc_b_load2); |
| 5281 | hName = "MC_(helperc_b_load2)"; |
| 5282 | break; |
| 5283 | case 4: hFun = (void*)&MC_(helperc_b_load4); |
| 5284 | hName = "MC_(helperc_b_load4)"; |
| 5285 | break; |
| 5286 | case 8: hFun = (void*)&MC_(helperc_b_load8); |
| 5287 | hName = "MC_(helperc_b_load8)"; |
| 5288 | break; |
| 5289 | case 16: hFun = (void*)&MC_(helperc_b_load16); |
| 5290 | hName = "MC_(helperc_b_load16)"; |
| 5291 | break; |
| 5292 | default: |
| 5293 | VG_(printf)("mc_translate.c: gen_load_b: unhandled szB == %d\n", szB); |
| 5294 | tl_assert(0); |
| 5295 | } |
| 5296 | di = unsafeIRDirty_1_N( |
| 5297 | bTmp, 1/*regparms*/, hName, VG_(fnptr_to_fnentry)( hFun ), |
| 5298 | mkIRExprVec_1( ea ) |
| 5299 | ); |
| 5300 | /* no need to mess with any annotations. This call accesses |
| 5301 | neither guest state nor guest memory. */ |
| 5302 | stmt( 'B', mce, IRStmt_Dirty(di) ); |
| 5303 | if (mce->hWordTy == Ity_I64) { |
| 5304 | /* 64-bit host */ |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 5305 | IRTemp bTmp32 = newTemp(mce, Ity_I32, BSh); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 5306 | assign( 'B', mce, bTmp32, unop(Iop_64to32, mkexpr(bTmp)) ); |
| 5307 | return mkexpr(bTmp32); |
| 5308 | } else { |
| 5309 | /* 32-bit host */ |
| 5310 | return mkexpr(bTmp); |
| 5311 | } |
| 5312 | } |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 5313 | |
| 5314 | /* Generate a shadow store. guard :: Ity_I1 controls whether the |
| 5315 | store really happens; NULL means it unconditionally does. */ |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 5316 | static void gen_store_b ( MCEnv* mce, Int szB, |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 5317 | IRAtom* baseaddr, Int offset, IRAtom* dataB, |
| 5318 | IRAtom* guard ) |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 5319 | { |
| 5320 | void* hFun; |
| 5321 | HChar* hName; |
| 5322 | IRDirty* di; |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 5323 | IRType aTy = typeOfIRExpr( mce->sb->tyenv, baseaddr ); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 5324 | IROp opAdd = aTy == Ity_I32 ? Iop_Add32 : Iop_Add64; |
| 5325 | IRAtom* ea = baseaddr; |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 5326 | if (guard) { |
| 5327 | tl_assert(isOriginalAtom(mce, guard)); |
| 5328 | tl_assert(typeOfIRExpr(mce->sb->tyenv, guard) == Ity_I1); |
| 5329 | } |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 5330 | if (offset != 0) { |
| 5331 | IRAtom* off = aTy == Ity_I32 ? mkU32( offset ) |
| 5332 | : mkU64( (Long)(Int)offset ); |
| 5333 | ea = assignNew( 'B', mce, aTy, binop(opAdd, ea, off)); |
| 5334 | } |
| 5335 | if (mce->hWordTy == Ity_I64) |
| 5336 | dataB = assignNew( 'B', mce, Ity_I64, unop(Iop_32Uto64, dataB)); |
| 5337 | |
| 5338 | switch (szB) { |
| 5339 | case 1: hFun = (void*)&MC_(helperc_b_store1); |
| 5340 | hName = "MC_(helperc_b_store1)"; |
| 5341 | break; |
| 5342 | case 2: hFun = (void*)&MC_(helperc_b_store2); |
| 5343 | hName = "MC_(helperc_b_store2)"; |
| 5344 | break; |
| 5345 | case 4: hFun = (void*)&MC_(helperc_b_store4); |
| 5346 | hName = "MC_(helperc_b_store4)"; |
| 5347 | break; |
| 5348 | case 8: hFun = (void*)&MC_(helperc_b_store8); |
| 5349 | hName = "MC_(helperc_b_store8)"; |
| 5350 | break; |
| 5351 | case 16: hFun = (void*)&MC_(helperc_b_store16); |
| 5352 | hName = "MC_(helperc_b_store16)"; |
| 5353 | break; |
| 5354 | default: |
| 5355 | tl_assert(0); |
| 5356 | } |
| 5357 | di = unsafeIRDirty_0_N( 2/*regparms*/, |
| 5358 | hName, VG_(fnptr_to_fnentry)( hFun ), |
| 5359 | mkIRExprVec_2( ea, dataB ) |
| 5360 | ); |
| 5361 | /* no need to mess with any annotations. This call accesses |
| 5362 | neither guest state nor guest memory. */ |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 5363 | if (guard) di->guard = guard; |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 5364 | stmt( 'B', mce, IRStmt_Dirty(di) ); |
| 5365 | } |
| 5366 | |
| 5367 | static IRAtom* narrowTo32 ( MCEnv* mce, IRAtom* e ) { |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 5368 | IRType eTy = typeOfIRExpr(mce->sb->tyenv, e); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 5369 | if (eTy == Ity_I64) |
| 5370 | return assignNew( 'B', mce, Ity_I32, unop(Iop_64to32, e) ); |
| 5371 | if (eTy == Ity_I32) |
| 5372 | return e; |
| 5373 | tl_assert(0); |
| 5374 | } |
| 5375 | |
| 5376 | static IRAtom* zWidenFrom32 ( MCEnv* mce, IRType dstTy, IRAtom* e ) { |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 5377 | IRType eTy = typeOfIRExpr(mce->sb->tyenv, e); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 5378 | tl_assert(eTy == Ity_I32); |
| 5379 | if (dstTy == Ity_I64) |
| 5380 | return assignNew( 'B', mce, Ity_I64, unop(Iop_32Uto64, e) ); |
| 5381 | tl_assert(0); |
| 5382 | } |
| 5383 | |
sewardj | db5907d | 2009-11-26 17:20:21 +0000 | [diff] [blame] | 5384 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 5385 | static IRAtom* schemeE ( MCEnv* mce, IRExpr* e ) |
| 5386 | { |
| 5387 | tl_assert(MC_(clo_mc_level) == 3); |
| 5388 | |
| 5389 | switch (e->tag) { |
| 5390 | |
| 5391 | case Iex_GetI: { |
| 5392 | IRRegArray* descr_b; |
| 5393 | IRAtom *t1, *t2, *t3, *t4; |
| 5394 | IRRegArray* descr = e->Iex.GetI.descr; |
| 5395 | IRType equivIntTy |
| 5396 | = MC_(get_otrack_reg_array_equiv_int_type)(descr); |
| 5397 | /* If this array is unshadowable for whatever reason, use the |
| 5398 | usual approximation. */ |
| 5399 | if (equivIntTy == Ity_INVALID) |
| 5400 | return mkU32(0); |
| 5401 | tl_assert(sizeofIRType(equivIntTy) >= 4); |
| 5402 | tl_assert(sizeofIRType(equivIntTy) == sizeofIRType(descr->elemTy)); |
| 5403 | descr_b = mkIRRegArray( descr->base + 2*mce->layout->total_sizeB, |
| 5404 | equivIntTy, descr->nElems ); |
| 5405 | /* Do a shadow indexed get of the same size, giving t1. Take |
| 5406 | the bottom 32 bits of it, giving t2. Compute into t3 the |
| 5407 | origin for the index (almost certainly zero, but there's |
| 5408 | no harm in being completely general here, since iropt will |
| 5409 | remove any useless code), and fold it in, giving a final |
| 5410 | value t4. */ |
| 5411 | t1 = assignNew( 'B', mce, equivIntTy, |
| 5412 | IRExpr_GetI( descr_b, e->Iex.GetI.ix, |
| 5413 | e->Iex.GetI.bias )); |
| 5414 | t2 = narrowTo32( mce, t1 ); |
| 5415 | t3 = schemeE( mce, e->Iex.GetI.ix ); |
| 5416 | t4 = gen_maxU32( mce, t2, t3 ); |
| 5417 | return t4; |
| 5418 | } |
| 5419 | case Iex_CCall: { |
| 5420 | Int i; |
| 5421 | IRAtom* here; |
| 5422 | IRExpr** args = e->Iex.CCall.args; |
| 5423 | IRAtom* curr = mkU32(0); |
| 5424 | for (i = 0; args[i]; i++) { |
| 5425 | tl_assert(i < 32); |
| 5426 | tl_assert(isOriginalAtom(mce, args[i])); |
| 5427 | /* Only take notice of this arg if the callee's |
| 5428 | mc-exclusion mask does not say it is to be excluded. */ |
| 5429 | if (e->Iex.CCall.cee->mcx_mask & (1<<i)) { |
| 5430 | /* the arg is to be excluded from definedness checking. |
| 5431 | Do nothing. */ |
| 5432 | if (0) VG_(printf)("excluding %s(%d)\n", |
| 5433 | e->Iex.CCall.cee->name, i); |
| 5434 | } else { |
| 5435 | /* calculate the arg's definedness, and pessimistically |
| 5436 | merge it in. */ |
| 5437 | here = schemeE( mce, args[i] ); |
| 5438 | curr = gen_maxU32( mce, curr, here ); |
| 5439 | } |
| 5440 | } |
| 5441 | return curr; |
| 5442 | } |
| 5443 | case Iex_Load: { |
| 5444 | Int dszB; |
| 5445 | dszB = sizeofIRType(e->Iex.Load.ty); |
| 5446 | /* assert that the B value for the address is already |
| 5447 | available (somewhere) */ |
| 5448 | tl_assert(isIRAtom(e->Iex.Load.addr)); |
| 5449 | tl_assert(mce->hWordTy == Ity_I32 || mce->hWordTy == Ity_I64); |
| 5450 | return gen_load_b( mce, dszB, e->Iex.Load.addr, 0 ); |
| 5451 | } |
| 5452 | case Iex_Mux0X: { |
| 5453 | IRAtom* b1 = schemeE( mce, e->Iex.Mux0X.cond ); |
| 5454 | IRAtom* b2 = schemeE( mce, e->Iex.Mux0X.expr0 ); |
| 5455 | IRAtom* b3 = schemeE( mce, e->Iex.Mux0X.exprX ); |
| 5456 | return gen_maxU32( mce, b1, gen_maxU32( mce, b2, b3 )); |
| 5457 | } |
| 5458 | case Iex_Qop: { |
| 5459 | IRAtom* b1 = schemeE( mce, e->Iex.Qop.arg1 ); |
| 5460 | IRAtom* b2 = schemeE( mce, e->Iex.Qop.arg2 ); |
| 5461 | IRAtom* b3 = schemeE( mce, e->Iex.Qop.arg3 ); |
| 5462 | IRAtom* b4 = schemeE( mce, e->Iex.Qop.arg4 ); |
| 5463 | return gen_maxU32( mce, gen_maxU32( mce, b1, b2 ), |
| 5464 | gen_maxU32( mce, b3, b4 ) ); |
| 5465 | } |
| 5466 | case Iex_Triop: { |
| 5467 | IRAtom* b1 = schemeE( mce, e->Iex.Triop.arg1 ); |
| 5468 | IRAtom* b2 = schemeE( mce, e->Iex.Triop.arg2 ); |
| 5469 | IRAtom* b3 = schemeE( mce, e->Iex.Triop.arg3 ); |
| 5470 | return gen_maxU32( mce, b1, gen_maxU32( mce, b2, b3 ) ); |
| 5471 | } |
| 5472 | case Iex_Binop: { |
sewardj | afed4c5 | 2009-07-12 13:00:17 +0000 | [diff] [blame] | 5473 | switch (e->Iex.Binop.op) { |
| 5474 | case Iop_CasCmpEQ8: case Iop_CasCmpNE8: |
| 5475 | case Iop_CasCmpEQ16: case Iop_CasCmpNE16: |
| 5476 | case Iop_CasCmpEQ32: case Iop_CasCmpNE32: |
| 5477 | case Iop_CasCmpEQ64: case Iop_CasCmpNE64: |
| 5478 | /* Just say these all produce a defined result, |
| 5479 | regardless of their arguments. See |
| 5480 | COMMENT_ON_CasCmpEQ in this file. */ |
| 5481 | return mkU32(0); |
| 5482 | default: { |
| 5483 | IRAtom* b1 = schemeE( mce, e->Iex.Binop.arg1 ); |
| 5484 | IRAtom* b2 = schemeE( mce, e->Iex.Binop.arg2 ); |
| 5485 | return gen_maxU32( mce, b1, b2 ); |
| 5486 | } |
| 5487 | } |
| 5488 | tl_assert(0); |
| 5489 | /*NOTREACHED*/ |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 5490 | } |
| 5491 | case Iex_Unop: { |
| 5492 | IRAtom* b1 = schemeE( mce, e->Iex.Unop.arg ); |
| 5493 | return b1; |
| 5494 | } |
| 5495 | case Iex_Const: |
| 5496 | return mkU32(0); |
| 5497 | case Iex_RdTmp: |
| 5498 | return mkexpr( findShadowTmpB( mce, e->Iex.RdTmp.tmp )); |
| 5499 | case Iex_Get: { |
| 5500 | Int b_offset = MC_(get_otrack_shadow_offset)( |
| 5501 | e->Iex.Get.offset, |
| 5502 | sizeofIRType(e->Iex.Get.ty) |
| 5503 | ); |
| 5504 | tl_assert(b_offset >= -1 |
| 5505 | && b_offset <= mce->layout->total_sizeB -4); |
| 5506 | if (b_offset >= 0) { |
| 5507 | /* FIXME: this isn't an atom! */ |
| 5508 | return IRExpr_Get( b_offset + 2*mce->layout->total_sizeB, |
| 5509 | Ity_I32 ); |
| 5510 | } |
| 5511 | return mkU32(0); |
| 5512 | } |
| 5513 | default: |
| 5514 | VG_(printf)("mc_translate.c: schemeE: unhandled: "); |
| 5515 | ppIRExpr(e); |
| 5516 | VG_(tool_panic)("memcheck:schemeE"); |
| 5517 | } |
| 5518 | } |
| 5519 | |
sewardj | db5907d | 2009-11-26 17:20:21 +0000 | [diff] [blame] | 5520 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 5521 | static void do_origins_Dirty ( MCEnv* mce, IRDirty* d ) |
| 5522 | { |
| 5523 | // This is a hacked version of do_shadow_Dirty |
njn | 4c245e5 | 2009-03-15 23:25:38 +0000 | [diff] [blame] | 5524 | Int i, n, toDo, gSz, gOff; |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 5525 | IRAtom *here, *curr; |
| 5526 | IRTemp dst; |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 5527 | |
| 5528 | /* First check the guard. */ |
| 5529 | curr = schemeE( mce, d->guard ); |
| 5530 | |
| 5531 | /* Now round up all inputs and maxU32 over them. */ |
| 5532 | |
| 5533 | /* Inputs: unmasked args */ |
| 5534 | for (i = 0; d->args[i]; i++) { |
| 5535 | if (d->cee->mcx_mask & (1<<i)) { |
| 5536 | /* ignore this arg */ |
| 5537 | } else { |
| 5538 | here = schemeE( mce, d->args[i] ); |
| 5539 | curr = gen_maxU32( mce, curr, here ); |
| 5540 | } |
| 5541 | } |
| 5542 | |
| 5543 | /* Inputs: guest state that we read. */ |
| 5544 | for (i = 0; i < d->nFxState; i++) { |
| 5545 | tl_assert(d->fxState[i].fx != Ifx_None); |
| 5546 | if (d->fxState[i].fx == Ifx_Write) |
| 5547 | continue; |
| 5548 | |
| 5549 | /* Ignore any sections marked as 'always defined'. */ |
| 5550 | if (isAlwaysDefd(mce, d->fxState[i].offset, d->fxState[i].size )) { |
| 5551 | if (0) |
| 5552 | VG_(printf)("memcheck: Dirty gst: ignored off %d, sz %d\n", |
| 5553 | d->fxState[i].offset, d->fxState[i].size ); |
| 5554 | continue; |
| 5555 | } |
| 5556 | |
| 5557 | /* This state element is read or modified. So we need to |
| 5558 | consider it. If larger than 4 bytes, deal with it in 4-byte |
| 5559 | chunks. */ |
| 5560 | gSz = d->fxState[i].size; |
| 5561 | gOff = d->fxState[i].offset; |
| 5562 | tl_assert(gSz > 0); |
| 5563 | while (True) { |
| 5564 | Int b_offset; |
| 5565 | if (gSz == 0) break; |
| 5566 | n = gSz <= 4 ? gSz : 4; |
| 5567 | /* update 'curr' with maxU32 of the state slice |
| 5568 | gOff .. gOff+n-1 */ |
| 5569 | b_offset = MC_(get_otrack_shadow_offset)(gOff, 4); |
| 5570 | if (b_offset != -1) { |
| 5571 | here = assignNew( 'B',mce, |
| 5572 | Ity_I32, |
| 5573 | IRExpr_Get(b_offset + 2*mce->layout->total_sizeB, |
| 5574 | Ity_I32)); |
| 5575 | curr = gen_maxU32( mce, curr, here ); |
| 5576 | } |
| 5577 | gSz -= n; |
| 5578 | gOff += n; |
| 5579 | } |
| 5580 | |
| 5581 | } |
| 5582 | |
| 5583 | /* Inputs: memory */ |
| 5584 | |
| 5585 | if (d->mFx != Ifx_None) { |
| 5586 | /* Because we may do multiple shadow loads/stores from the same |
| 5587 | base address, it's best to do a single test of its |
| 5588 | definedness right now. Post-instrumentation optimisation |
| 5589 | should remove all but this test. */ |
| 5590 | tl_assert(d->mAddr); |
| 5591 | here = schemeE( mce, d->mAddr ); |
| 5592 | curr = gen_maxU32( mce, curr, here ); |
| 5593 | } |
| 5594 | |
| 5595 | /* Deal with memory inputs (reads or modifies) */ |
| 5596 | if (d->mFx == Ifx_Read || d->mFx == Ifx_Modify) { |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 5597 | toDo = d->mSize; |
| 5598 | /* chew off 32-bit chunks. We don't care about the endianness |
| 5599 | since it's all going to be condensed down to a single bit, |
| 5600 | but nevertheless choose an endianness which is hopefully |
| 5601 | native to the platform. */ |
| 5602 | while (toDo >= 4) { |
| 5603 | here = gen_load_b( mce, 4, d->mAddr, d->mSize - toDo ); |
| 5604 | curr = gen_maxU32( mce, curr, here ); |
| 5605 | toDo -= 4; |
| 5606 | } |
sewardj | 8c93fcc | 2008-10-30 13:08:31 +0000 | [diff] [blame] | 5607 | /* handle possible 16-bit excess */ |
| 5608 | while (toDo >= 2) { |
| 5609 | here = gen_load_b( mce, 2, d->mAddr, d->mSize - toDo ); |
| 5610 | curr = gen_maxU32( mce, curr, here ); |
| 5611 | toDo -= 2; |
| 5612 | } |
| 5613 | tl_assert(toDo == 0); /* also need to handle 1-byte excess */ |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 5614 | } |
| 5615 | |
| 5616 | /* Whew! So curr is a 32-bit B-value which should give an origin |
| 5617 | of some use if any of the inputs to the helper are undefined. |
| 5618 | Now we need to re-distribute the results to all destinations. */ |
| 5619 | |
| 5620 | /* Outputs: the destination temporary, if there is one. */ |
| 5621 | if (d->tmp != IRTemp_INVALID) { |
| 5622 | dst = findShadowTmpB(mce, d->tmp); |
| 5623 | assign( 'V', mce, dst, curr ); |
| 5624 | } |
| 5625 | |
| 5626 | /* Outputs: guest state that we write or modify. */ |
| 5627 | for (i = 0; i < d->nFxState; i++) { |
| 5628 | tl_assert(d->fxState[i].fx != Ifx_None); |
| 5629 | if (d->fxState[i].fx == Ifx_Read) |
| 5630 | continue; |
| 5631 | |
| 5632 | /* Ignore any sections marked as 'always defined'. */ |
| 5633 | if (isAlwaysDefd(mce, d->fxState[i].offset, d->fxState[i].size )) |
| 5634 | continue; |
| 5635 | |
| 5636 | /* This state element is written or modified. So we need to |
| 5637 | consider it. If larger than 4 bytes, deal with it in 4-byte |
| 5638 | chunks. */ |
| 5639 | gSz = d->fxState[i].size; |
| 5640 | gOff = d->fxState[i].offset; |
| 5641 | tl_assert(gSz > 0); |
| 5642 | while (True) { |
| 5643 | Int b_offset; |
| 5644 | if (gSz == 0) break; |
| 5645 | n = gSz <= 4 ? gSz : 4; |
| 5646 | /* Write 'curr' to the state slice gOff .. gOff+n-1 */ |
| 5647 | b_offset = MC_(get_otrack_shadow_offset)(gOff, 4); |
| 5648 | if (b_offset != -1) { |
| 5649 | stmt( 'B', mce, IRStmt_Put(b_offset + 2*mce->layout->total_sizeB, |
| 5650 | curr )); |
| 5651 | } |
| 5652 | gSz -= n; |
| 5653 | gOff += n; |
| 5654 | } |
| 5655 | } |
| 5656 | |
| 5657 | /* Outputs: memory that we write or modify. Same comments about |
| 5658 | endianness as above apply. */ |
| 5659 | if (d->mFx == Ifx_Write || d->mFx == Ifx_Modify) { |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 5660 | toDo = d->mSize; |
| 5661 | /* chew off 32-bit chunks */ |
| 5662 | while (toDo >= 4) { |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 5663 | gen_store_b( mce, 4, d->mAddr, d->mSize - toDo, curr, |
| 5664 | NULL/*guard*/ ); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 5665 | toDo -= 4; |
| 5666 | } |
sewardj | 8c93fcc | 2008-10-30 13:08:31 +0000 | [diff] [blame] | 5667 | /* handle possible 16-bit excess */ |
| 5668 | while (toDo >= 2) { |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 5669 | gen_store_b( mce, 2, d->mAddr, d->mSize - toDo, curr, |
| 5670 | NULL/*guard*/ ); |
sewardj | 8c93fcc | 2008-10-30 13:08:31 +0000 | [diff] [blame] | 5671 | toDo -= 2; |
| 5672 | } |
| 5673 | tl_assert(toDo == 0); /* also need to handle 1-byte excess */ |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 5674 | } |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 5675 | } |
| 5676 | |
sewardj | db5907d | 2009-11-26 17:20:21 +0000 | [diff] [blame] | 5677 | |
| 5678 | static void do_origins_Store ( MCEnv* mce, |
| 5679 | IREndness stEnd, |
| 5680 | IRExpr* stAddr, |
| 5681 | IRExpr* stData ) |
| 5682 | { |
| 5683 | Int dszB; |
| 5684 | IRAtom* dataB; |
| 5685 | /* assert that the B value for the address is already available |
| 5686 | (somewhere), since the call to schemeE will want to see it. |
| 5687 | XXXX how does this actually ensure that?? */ |
| 5688 | tl_assert(isIRAtom(stAddr)); |
| 5689 | tl_assert(isIRAtom(stData)); |
| 5690 | dszB = sizeofIRType( typeOfIRExpr(mce->sb->tyenv, stData ) ); |
| 5691 | dataB = schemeE( mce, stData ); |
| 5692 | gen_store_b( mce, dszB, stAddr, 0/*offset*/, dataB, |
| 5693 | NULL/*guard*/ ); |
| 5694 | } |
| 5695 | |
| 5696 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 5697 | static void schemeS ( MCEnv* mce, IRStmt* st ) |
| 5698 | { |
| 5699 | tl_assert(MC_(clo_mc_level) == 3); |
| 5700 | |
| 5701 | switch (st->tag) { |
| 5702 | |
| 5703 | case Ist_AbiHint: |
| 5704 | /* The value-check instrumenter handles this - by arranging |
| 5705 | to pass the address of the next instruction to |
| 5706 | MC_(helperc_MAKE_STACK_UNINIT). This is all that needs to |
| 5707 | happen for origin tracking w.r.t. AbiHints. So there is |
| 5708 | nothing to do here. */ |
| 5709 | break; |
| 5710 | |
| 5711 | case Ist_PutI: { |
| 5712 | IRRegArray* descr_b; |
| 5713 | IRAtom *t1, *t2, *t3, *t4; |
| 5714 | IRRegArray* descr = st->Ist.PutI.descr; |
| 5715 | IRType equivIntTy |
| 5716 | = MC_(get_otrack_reg_array_equiv_int_type)(descr); |
| 5717 | /* If this array is unshadowable for whatever reason, |
| 5718 | generate no code. */ |
| 5719 | if (equivIntTy == Ity_INVALID) |
| 5720 | break; |
| 5721 | tl_assert(sizeofIRType(equivIntTy) >= 4); |
| 5722 | tl_assert(sizeofIRType(equivIntTy) == sizeofIRType(descr->elemTy)); |
| 5723 | descr_b |
| 5724 | = mkIRRegArray( descr->base + 2*mce->layout->total_sizeB, |
| 5725 | equivIntTy, descr->nElems ); |
| 5726 | /* Compute a value to Put - the conjoinment of the origin for |
| 5727 | the data to be Put-ted (obviously) and of the index value |
| 5728 | (not so obviously). */ |
| 5729 | t1 = schemeE( mce, st->Ist.PutI.data ); |
| 5730 | t2 = schemeE( mce, st->Ist.PutI.ix ); |
| 5731 | t3 = gen_maxU32( mce, t1, t2 ); |
| 5732 | t4 = zWidenFrom32( mce, equivIntTy, t3 ); |
| 5733 | stmt( 'B', mce, IRStmt_PutI( descr_b, st->Ist.PutI.ix, |
| 5734 | st->Ist.PutI.bias, t4 )); |
| 5735 | break; |
| 5736 | } |
sewardj | db5907d | 2009-11-26 17:20:21 +0000 | [diff] [blame] | 5737 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 5738 | case Ist_Dirty: |
| 5739 | do_origins_Dirty( mce, st->Ist.Dirty.details ); |
| 5740 | break; |
sewardj | db5907d | 2009-11-26 17:20:21 +0000 | [diff] [blame] | 5741 | |
| 5742 | case Ist_Store: |
| 5743 | do_origins_Store( mce, st->Ist.Store.end, |
| 5744 | st->Ist.Store.addr, |
| 5745 | st->Ist.Store.data ); |
| 5746 | break; |
| 5747 | |
| 5748 | case Ist_LLSC: { |
| 5749 | /* In short: treat a load-linked like a normal load followed |
| 5750 | by an assignment of the loaded (shadow) data the result |
| 5751 | temporary. Treat a store-conditional like a normal store, |
| 5752 | and mark the result temporary as defined. */ |
| 5753 | if (st->Ist.LLSC.storedata == NULL) { |
| 5754 | /* Load Linked */ |
| 5755 | IRType resTy |
| 5756 | = typeOfIRTemp(mce->sb->tyenv, st->Ist.LLSC.result); |
| 5757 | IRExpr* vanillaLoad |
| 5758 | = IRExpr_Load(st->Ist.LLSC.end, resTy, st->Ist.LLSC.addr); |
| 5759 | tl_assert(resTy == Ity_I64 || resTy == Ity_I32 |
| 5760 | || resTy == Ity_I16 || resTy == Ity_I8); |
| 5761 | assign( 'B', mce, findShadowTmpB(mce, st->Ist.LLSC.result), |
| 5762 | schemeE(mce, vanillaLoad)); |
| 5763 | } else { |
| 5764 | /* Store conditional */ |
| 5765 | do_origins_Store( mce, st->Ist.LLSC.end, |
| 5766 | st->Ist.LLSC.addr, |
| 5767 | st->Ist.LLSC.storedata ); |
| 5768 | /* For the rationale behind this, see comments at the |
| 5769 | place where the V-shadow for .result is constructed, in |
| 5770 | do_shadow_LLSC. In short, we regard .result as |
| 5771 | always-defined. */ |
| 5772 | assign( 'B', mce, findShadowTmpB(mce, st->Ist.LLSC.result), |
| 5773 | mkU32(0) ); |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 5774 | } |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 5775 | break; |
| 5776 | } |
sewardj | db5907d | 2009-11-26 17:20:21 +0000 | [diff] [blame] | 5777 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 5778 | case Ist_Put: { |
| 5779 | Int b_offset |
| 5780 | = MC_(get_otrack_shadow_offset)( |
| 5781 | st->Ist.Put.offset, |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 5782 | sizeofIRType(typeOfIRExpr(mce->sb->tyenv, st->Ist.Put.data)) |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 5783 | ); |
| 5784 | if (b_offset >= 0) { |
| 5785 | /* FIXME: this isn't an atom! */ |
| 5786 | stmt( 'B', mce, IRStmt_Put(b_offset + 2*mce->layout->total_sizeB, |
| 5787 | schemeE( mce, st->Ist.Put.data )) ); |
| 5788 | } |
| 5789 | break; |
| 5790 | } |
sewardj | db5907d | 2009-11-26 17:20:21 +0000 | [diff] [blame] | 5791 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 5792 | case Ist_WrTmp: |
| 5793 | assign( 'B', mce, findShadowTmpB(mce, st->Ist.WrTmp.tmp), |
| 5794 | schemeE(mce, st->Ist.WrTmp.data) ); |
| 5795 | break; |
sewardj | db5907d | 2009-11-26 17:20:21 +0000 | [diff] [blame] | 5796 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 5797 | case Ist_MBE: |
| 5798 | case Ist_NoOp: |
| 5799 | case Ist_Exit: |
| 5800 | case Ist_IMark: |
| 5801 | break; |
sewardj | db5907d | 2009-11-26 17:20:21 +0000 | [diff] [blame] | 5802 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 5803 | default: |
| 5804 | VG_(printf)("mc_translate.c: schemeS: unhandled: "); |
| 5805 | ppIRStmt(st); |
| 5806 | VG_(tool_panic)("memcheck:schemeS"); |
| 5807 | } |
| 5808 | } |
| 5809 | |
| 5810 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 5811 | /*--------------------------------------------------------------------*/ |
njn25 | cac76cb | 2002-09-23 11:21:57 +0000 | [diff] [blame] | 5812 | /*--- end mc_translate.c ---*/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 5813 | /*--------------------------------------------------------------------*/ |