sewardj | a1a370f | 2004-08-17 13:31:55 +0000 | [diff] [blame] | 1 | |
| 2 | /*---------------------------------------------------------------*/ |
| 3 | /*--- ---*/ |
| 4 | /*--- This file (ir/iropt.c) is ---*/ |
sewardj | dbcfae7 | 2005-08-02 11:14:04 +0000 | [diff] [blame] | 5 | /*--- Copyright (C) OpenWorks LLP. All rights reserved. ---*/ |
sewardj | a1a370f | 2004-08-17 13:31:55 +0000 | [diff] [blame] | 6 | /*--- ---*/ |
| 7 | /*---------------------------------------------------------------*/ |
| 8 | |
sewardj | f8ed9d8 | 2004-11-12 17:40:23 +0000 | [diff] [blame] | 9 | /* |
| 10 | This file is part of LibVEX, a library for dynamic binary |
| 11 | instrumentation and translation. |
| 12 | |
sewardj | e744153 | 2007-01-08 05:51:05 +0000 | [diff] [blame] | 13 | Copyright (C) 2004-2007 OpenWorks LLP. All rights reserved. |
sewardj | f8ed9d8 | 2004-11-12 17:40:23 +0000 | [diff] [blame] | 14 | |
sewardj | 7bd6ffe | 2005-08-03 16:07:36 +0000 | [diff] [blame] | 15 | This library is made available under a dual licensing scheme. |
sewardj | f8ed9d8 | 2004-11-12 17:40:23 +0000 | [diff] [blame] | 16 | |
sewardj | 7bd6ffe | 2005-08-03 16:07:36 +0000 | [diff] [blame] | 17 | If you link LibVEX against other code all of which is itself |
| 18 | licensed under the GNU General Public License, version 2 dated June |
| 19 | 1991 ("GPL v2"), then you may use LibVEX under the terms of the GPL |
| 20 | v2, as appearing in the file LICENSE.GPL. If the file LICENSE.GPL |
| 21 | is missing, you can obtain a copy of the GPL v2 from the Free |
| 22 | Software Foundation Inc., 51 Franklin St, Fifth Floor, Boston, MA |
| 23 | 02110-1301, USA. |
| 24 | |
| 25 | For any other uses of LibVEX, you must first obtain a commercial |
| 26 | license from OpenWorks LLP. Please contact info@open-works.co.uk |
| 27 | for information about commercial licensing. |
| 28 | |
| 29 | This software is provided by OpenWorks LLP "as is" and any express |
| 30 | or implied warranties, including, but not limited to, the implied |
| 31 | warranties of merchantability and fitness for a particular purpose |
| 32 | are disclaimed. In no event shall OpenWorks LLP be liable for any |
| 33 | direct, indirect, incidental, special, exemplary, or consequential |
| 34 | damages (including, but not limited to, procurement of substitute |
| 35 | goods or services; loss of use, data, or profits; or business |
| 36 | interruption) however caused and on any theory of liability, |
| 37 | whether in contract, strict liability, or tort (including |
| 38 | negligence or otherwise) arising in any way out of the use of this |
| 39 | software, even if advised of the possibility of such damage. |
sewardj | f8ed9d8 | 2004-11-12 17:40:23 +0000 | [diff] [blame] | 40 | |
| 41 | Neither the names of the U.S. Department of Energy nor the |
| 42 | University of California nor the names of its contributors may be |
| 43 | used to endorse or promote products derived from this software |
| 44 | without prior written permission. |
sewardj | f8ed9d8 | 2004-11-12 17:40:23 +0000 | [diff] [blame] | 45 | */ |
| 46 | |
sewardj | a1a370f | 2004-08-17 13:31:55 +0000 | [diff] [blame] | 47 | #include "libvex_basictypes.h" |
sewardj | edf4d69 | 2004-08-17 13:52:58 +0000 | [diff] [blame] | 48 | #include "libvex_ir.h" |
sewardj | a1a370f | 2004-08-17 13:31:55 +0000 | [diff] [blame] | 49 | #include "libvex.h" |
| 50 | |
| 51 | #include "main/vex_util.h" |
sewardj | 0861374 | 2004-10-25 13:01:45 +0000 | [diff] [blame] | 52 | #include "main/vex_globals.h" |
sewardj | edf4d69 | 2004-08-17 13:52:58 +0000 | [diff] [blame] | 53 | #include "ir/iropt.h" |
sewardj | a1a370f | 2004-08-17 13:31:55 +0000 | [diff] [blame] | 54 | |
sewardj | d0863ff | 2004-10-23 00:22:32 +0000 | [diff] [blame] | 55 | |
sewardj | 088bcb4 | 2004-08-19 17:16:52 +0000 | [diff] [blame] | 56 | /* Set to 1 for lots of debugging output. */ |
| 57 | #define DEBUG_IROPT 0 |
| 58 | |
sewardj | a1a370f | 2004-08-17 13:31:55 +0000 | [diff] [blame] | 59 | |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 60 | /* What iropt does, 29 Dec 04. |
| 61 | |
sewardj | f6c8ebf | 2007-02-06 01:52:52 +0000 | [diff] [blame^] | 62 | It takes an IRSB and produces a new one with the same meaning, |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 63 | defined thus: |
| 64 | |
| 65 | After execution of the new BB, all guest state and guest memory is |
| 66 | the same as after execution of the original. This is true |
| 67 | regardless of how the block was exited (at the end vs side exit). |
| 68 | |
| 69 | In addition, parts of the guest state will be identical to that |
| 70 | created by execution of the original at the following observation |
| 71 | points: |
| 72 | |
| 73 | * In a dirty helper call, any parts of the guest state that the |
| 74 | helper states that it reads or modifies will be up to date. |
| 75 | Also, guest memory will be up to date. Parts of the guest state |
| 76 | not marked as being read or modified by the helper cannot be |
| 77 | assumed to be up-to-date at the point where the helper is called. |
| 78 | |
| 79 | * Immediately prior to any load or store, those parts of the guest |
| 80 | state marked as requiring precise exceptions will be up to date. |
| 81 | Also, guest memory will be up to date. Parts of the guest state |
| 82 | not marked as requiring precise exceptions cannot be assumed to |
| 83 | be up-to-date at the point of the load/store. |
| 84 | |
| 85 | The relative order of loads and stores (including loads/stores of |
| 86 | guest memory done by dirty helpers annotated as such) is not |
| 87 | changed. However, the relative order of loads with no intervening |
| 88 | stores/modifies may be changed. |
| 89 | |
| 90 | Transformation order |
| 91 | ~~~~~~~~~~~~~~~~~~~~ |
| 92 | |
| 93 | There are three levels of optimisation, controlled by |
| 94 | vex_control.iropt_level. Define first: |
| 95 | |
| 96 | "Cheap transformations" are the following sequence: |
| 97 | * Redundant-Get removal |
| 98 | * Redundant-Put removal |
| 99 | * Constant propagation/folding |
| 100 | * Dead code removal |
| 101 | * Specialisation of clean helper functions |
| 102 | * Dead code removal |
| 103 | |
| 104 | "Expensive transformations" are the following sequence: |
| 105 | * CSE |
| 106 | * Folding of add/sub chains |
| 107 | * Redundant-GetI removal |
| 108 | * Redundant-PutI removal |
| 109 | * Dead code removal |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 110 | |
| 111 | Then the transformations are as follows, as defined by |
| 112 | vex_control.iropt_level: |
| 113 | |
| 114 | Level 0: |
| 115 | * Flatten into atomic form. |
| 116 | |
| 117 | Level 1: the following sequence: |
| 118 | * Flatten into atomic form. |
| 119 | * Cheap transformations. |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 120 | |
| 121 | Level 2: the following sequence |
| 122 | * Flatten into atomic form. |
| 123 | * Cheap transformations. |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 124 | * If block contains any floating or vector types, CSE. |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 125 | * If block contains GetI or PutI, Expensive transformations. |
| 126 | * Try unrolling loops. Three possible outcomes: |
| 127 | - No effect: do nothing more. |
| 128 | - Unrolled a loop, and block does not contain GetI or PutI: |
| 129 | Do: * CSE |
| 130 | * Dead code removal |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 131 | - Unrolled a loop, and block contains GetI or PutI: |
| 132 | Do: * Expensive transformations |
| 133 | * Cheap transformations |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 134 | */ |
| 135 | |
sewardj | 9843029 | 2004-12-29 17:34:50 +0000 | [diff] [blame] | 136 | /* Implementation notes, 29 Dec 04. |
| 137 | |
| 138 | TODO (important): I think rPutI removal ignores precise exceptions |
| 139 | and is therefore in a sense, wrong. In the sense that PutIs are |
| 140 | assumed not to write parts of the guest state that we need to have |
| 141 | up-to-date at loads/stores. So far on x86 guest that has not |
| 142 | mattered since indeed only the x87 FP registers and tags are |
| 143 | accessed using GetI/PutI, and there is no need so far for them to |
| 144 | be up to date at mem exception points. The rPutI pass should be |
| 145 | fixed. |
sewardj | fb44d55 | 2004-10-25 09:48:47 +0000 | [diff] [blame] | 146 | |
sewardj | 4c5f6d5 | 2004-10-26 13:25:33 +0000 | [diff] [blame] | 147 | TODO: improve pessimistic handling of precise exceptions |
| 148 | in the tree builder. |
| 149 | |
sewardj | fb44d55 | 2004-10-25 09:48:47 +0000 | [diff] [blame] | 150 | TODO: check interaction of rGetI and dirty helpers. |
sewardj | c0b4228 | 2004-10-12 13:44:12 +0000 | [diff] [blame] | 151 | |
| 152 | F64i constants are treated differently from other constants. |
| 153 | They are not regarded as atoms, and instead lifted off and |
| 154 | bound to temps. This allows them to participate in CSE, which |
| 155 | is important for getting good performance for x86 guest code. |
sewardj | 695cff9 | 2004-10-13 14:50:14 +0000 | [diff] [blame] | 156 | |
sewardj | a5aa9cf | 2004-10-15 22:56:38 +0000 | [diff] [blame] | 157 | CSE up F64 literals (already doing F64is) |
sewardj | 4c5f6d5 | 2004-10-26 13:25:33 +0000 | [diff] [blame] | 158 | |
| 159 | CSE: consider carefully the requirement for precise exns |
sewardj | 9843029 | 2004-12-29 17:34:50 +0000 | [diff] [blame] | 160 | prior to making CSE any more aggressive. */ |
sewardj | c0b4228 | 2004-10-12 13:44:12 +0000 | [diff] [blame] | 161 | |
| 162 | |
sewardj | a1a370f | 2004-08-17 13:31:55 +0000 | [diff] [blame] | 163 | /*---------------------------------------------------------------*/ |
| 164 | /*--- Finite mappery, of a sort ---*/ |
| 165 | /*---------------------------------------------------------------*/ |
| 166 | |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 167 | /* General map from HWord-sized thing HWord-sized thing. Could be by |
| 168 | hashing, but it's not clear whether or not this would really be any |
| 169 | faster. */ |
sewardj | a1a370f | 2004-08-17 13:31:55 +0000 | [diff] [blame] | 170 | |
| 171 | typedef |
| 172 | struct { |
| 173 | Bool* inuse; |
sewardj | ea602bc | 2004-10-14 21:40:12 +0000 | [diff] [blame] | 174 | HWord* key; |
| 175 | HWord* val; |
sewardj | a1a370f | 2004-08-17 13:31:55 +0000 | [diff] [blame] | 176 | Int size; |
| 177 | Int used; |
| 178 | } |
sewardj | ea602bc | 2004-10-14 21:40:12 +0000 | [diff] [blame] | 179 | HashHW; |
sewardj | a1a370f | 2004-08-17 13:31:55 +0000 | [diff] [blame] | 180 | |
sewardj | ea602bc | 2004-10-14 21:40:12 +0000 | [diff] [blame] | 181 | static HashHW* newHHW ( void ) |
sewardj | a1a370f | 2004-08-17 13:31:55 +0000 | [diff] [blame] | 182 | { |
sewardj | ea602bc | 2004-10-14 21:40:12 +0000 | [diff] [blame] | 183 | HashHW* h = LibVEX_Alloc(sizeof(HashHW)); |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 184 | h->size = 8; |
sewardj | a1a370f | 2004-08-17 13:31:55 +0000 | [diff] [blame] | 185 | h->used = 0; |
| 186 | h->inuse = LibVEX_Alloc(h->size * sizeof(Bool)); |
sewardj | ea602bc | 2004-10-14 21:40:12 +0000 | [diff] [blame] | 187 | h->key = LibVEX_Alloc(h->size * sizeof(HWord)); |
| 188 | h->val = LibVEX_Alloc(h->size * sizeof(HWord)); |
sewardj | a1a370f | 2004-08-17 13:31:55 +0000 | [diff] [blame] | 189 | return h; |
| 190 | } |
| 191 | |
| 192 | |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 193 | /* Look up key in the map. */ |
sewardj | a1a370f | 2004-08-17 13:31:55 +0000 | [diff] [blame] | 194 | |
sewardj | ea602bc | 2004-10-14 21:40:12 +0000 | [diff] [blame] | 195 | static Bool lookupHHW ( HashHW* h, /*OUT*/HWord* val, HWord key ) |
sewardj | a1a370f | 2004-08-17 13:31:55 +0000 | [diff] [blame] | 196 | { |
| 197 | Int i; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 198 | /* vex_printf("lookupHHW(%llx)\n", key ); */ |
sewardj | a1a370f | 2004-08-17 13:31:55 +0000 | [diff] [blame] | 199 | for (i = 0; i < h->used; i++) { |
| 200 | if (h->inuse[i] && h->key[i] == key) { |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 201 | if (val) |
| 202 | *val = h->val[i]; |
sewardj | a1a370f | 2004-08-17 13:31:55 +0000 | [diff] [blame] | 203 | return True; |
| 204 | } |
| 205 | } |
| 206 | return False; |
| 207 | } |
| 208 | |
| 209 | |
sewardj | a1a370f | 2004-08-17 13:31:55 +0000 | [diff] [blame] | 210 | /* Add key->val to the map. Replaces any existing binding for key. */ |
| 211 | |
sewardj | ea602bc | 2004-10-14 21:40:12 +0000 | [diff] [blame] | 212 | static void addToHHW ( HashHW* h, HWord key, HWord val ) |
sewardj | a1a370f | 2004-08-17 13:31:55 +0000 | [diff] [blame] | 213 | { |
| 214 | Int i, j; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 215 | /* vex_printf("addToHHW(%llx, %llx)\n", key, val); */ |
sewardj | a1a370f | 2004-08-17 13:31:55 +0000 | [diff] [blame] | 216 | |
| 217 | /* Find and replace existing binding, if any. */ |
| 218 | for (i = 0; i < h->used; i++) { |
| 219 | if (h->inuse[i] && h->key[i] == key) { |
| 220 | h->val[i] = val; |
| 221 | return; |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | /* Ensure a space is available. */ |
| 226 | if (h->used == h->size) { |
| 227 | /* Copy into arrays twice the size. */ |
| 228 | Bool* inuse2 = LibVEX_Alloc(2 * h->size * sizeof(Bool)); |
sewardj | ea602bc | 2004-10-14 21:40:12 +0000 | [diff] [blame] | 229 | HWord* key2 = LibVEX_Alloc(2 * h->size * sizeof(HWord)); |
| 230 | HWord* val2 = LibVEX_Alloc(2 * h->size * sizeof(HWord)); |
sewardj | a1a370f | 2004-08-17 13:31:55 +0000 | [diff] [blame] | 231 | for (i = j = 0; i < h->size; i++) { |
| 232 | if (!h->inuse[i]) continue; |
| 233 | inuse2[j] = True; |
| 234 | key2[j] = h->key[i]; |
| 235 | val2[j] = h->val[i]; |
| 236 | j++; |
| 237 | } |
| 238 | h->used = j; |
| 239 | h->size *= 2; |
| 240 | h->inuse = inuse2; |
| 241 | h->key = key2; |
| 242 | h->val = val2; |
| 243 | } |
| 244 | |
| 245 | /* Finally, add it. */ |
| 246 | vassert(h->used < h->size); |
| 247 | h->inuse[h->used] = True; |
| 248 | h->key[h->used] = key; |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 249 | h->val[h->used] = val; |
sewardj | a1a370f | 2004-08-17 13:31:55 +0000 | [diff] [blame] | 250 | h->used++; |
| 251 | } |
| 252 | |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 253 | |
sewardj | d7cb853 | 2004-08-17 23:59:23 +0000 | [diff] [blame] | 254 | /*---------------------------------------------------------------*/ |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 255 | /*--- Flattening out a BB into atomic SSA form ---*/ |
sewardj | d7cb853 | 2004-08-17 23:59:23 +0000 | [diff] [blame] | 256 | /*---------------------------------------------------------------*/ |
| 257 | |
sewardj | e80679a | 2004-09-21 23:00:11 +0000 | [diff] [blame] | 258 | /* Non-critical helper, heuristic for reducing the number of tmp-tmp |
| 259 | copies made by flattening. If in doubt return False. */ |
| 260 | |
| 261 | static Bool isFlat ( IRExpr* e ) |
| 262 | { |
sewardj | 695cff9 | 2004-10-13 14:50:14 +0000 | [diff] [blame] | 263 | if (e->tag == Iex_Get) |
| 264 | return True; |
sewardj | e80679a | 2004-09-21 23:00:11 +0000 | [diff] [blame] | 265 | if (e->tag == Iex_Binop) |
sewardj | 496a58d | 2005-03-20 18:44:44 +0000 | [diff] [blame] | 266 | return toBool( isIRAtom(e->Iex.Binop.arg1) |
| 267 | && isIRAtom(e->Iex.Binop.arg2) ); |
sewardj | af1ceca | 2005-06-30 23:31:27 +0000 | [diff] [blame] | 268 | if (e->tag == Iex_Load) |
| 269 | return isIRAtom(e->Iex.Load.addr); |
sewardj | e80679a | 2004-09-21 23:00:11 +0000 | [diff] [blame] | 270 | return False; |
| 271 | } |
| 272 | |
sewardj | d7cb853 | 2004-08-17 23:59:23 +0000 | [diff] [blame] | 273 | /* Flatten out 'ex' so it is atomic, returning a new expression with |
| 274 | the same value, after having appended extra IRTemp assignments to |
| 275 | the end of 'bb'. */ |
| 276 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 277 | static IRExpr* flatten_Expr ( IRSB* bb, IRExpr* ex ) |
sewardj | d7cb853 | 2004-08-17 23:59:23 +0000 | [diff] [blame] | 278 | { |
| 279 | Int i; |
| 280 | IRExpr** newargs; |
| 281 | IRType ty = typeOfIRExpr(bb->tyenv, ex); |
| 282 | IRTemp t1; |
| 283 | |
| 284 | switch (ex->tag) { |
| 285 | |
sewardj | d721703 | 2004-08-19 10:49:10 +0000 | [diff] [blame] | 286 | case Iex_GetI: |
| 287 | t1 = newIRTemp(bb->tyenv, ty); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 288 | addStmtToIRSB(bb, IRStmt_WrTmp(t1, |
sewardj | 2d3f77c | 2004-09-22 23:49:09 +0000 | [diff] [blame] | 289 | IRExpr_GetI(ex->Iex.GetI.descr, |
sewardj | eeac841 | 2004-11-02 00:26:55 +0000 | [diff] [blame] | 290 | flatten_Expr(bb, ex->Iex.GetI.ix), |
sewardj | 2d3f77c | 2004-09-22 23:49:09 +0000 | [diff] [blame] | 291 | ex->Iex.GetI.bias))); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 292 | return IRExpr_RdTmp(t1); |
sewardj | d721703 | 2004-08-19 10:49:10 +0000 | [diff] [blame] | 293 | |
sewardj | d7cb853 | 2004-08-17 23:59:23 +0000 | [diff] [blame] | 294 | case Iex_Get: |
| 295 | t1 = newIRTemp(bb->tyenv, ty); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 296 | addStmtToIRSB(bb, |
| 297 | IRStmt_WrTmp(t1, ex)); |
| 298 | return IRExpr_RdTmp(t1); |
sewardj | d7cb853 | 2004-08-17 23:59:23 +0000 | [diff] [blame] | 299 | |
sewardj | 40c8026 | 2006-02-08 19:30:46 +0000 | [diff] [blame] | 300 | case Iex_Qop: |
| 301 | t1 = newIRTemp(bb->tyenv, ty); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 302 | addStmtToIRSB(bb, IRStmt_WrTmp(t1, |
sewardj | 40c8026 | 2006-02-08 19:30:46 +0000 | [diff] [blame] | 303 | IRExpr_Qop(ex->Iex.Qop.op, |
| 304 | flatten_Expr(bb, ex->Iex.Qop.arg1), |
| 305 | flatten_Expr(bb, ex->Iex.Qop.arg2), |
| 306 | flatten_Expr(bb, ex->Iex.Qop.arg3), |
| 307 | flatten_Expr(bb, ex->Iex.Qop.arg4)))); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 308 | return IRExpr_RdTmp(t1); |
sewardj | 40c8026 | 2006-02-08 19:30:46 +0000 | [diff] [blame] | 309 | |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 310 | case Iex_Triop: |
| 311 | t1 = newIRTemp(bb->tyenv, ty); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 312 | addStmtToIRSB(bb, IRStmt_WrTmp(t1, |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 313 | IRExpr_Triop(ex->Iex.Triop.op, |
| 314 | flatten_Expr(bb, ex->Iex.Triop.arg1), |
| 315 | flatten_Expr(bb, ex->Iex.Triop.arg2), |
| 316 | flatten_Expr(bb, ex->Iex.Triop.arg3)))); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 317 | return IRExpr_RdTmp(t1); |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 318 | |
sewardj | d7cb853 | 2004-08-17 23:59:23 +0000 | [diff] [blame] | 319 | case Iex_Binop: |
| 320 | t1 = newIRTemp(bb->tyenv, ty); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 321 | addStmtToIRSB(bb, IRStmt_WrTmp(t1, |
sewardj | d7cb853 | 2004-08-17 23:59:23 +0000 | [diff] [blame] | 322 | IRExpr_Binop(ex->Iex.Binop.op, |
| 323 | flatten_Expr(bb, ex->Iex.Binop.arg1), |
| 324 | flatten_Expr(bb, ex->Iex.Binop.arg2)))); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 325 | return IRExpr_RdTmp(t1); |
sewardj | d7cb853 | 2004-08-17 23:59:23 +0000 | [diff] [blame] | 326 | |
| 327 | case Iex_Unop: |
| 328 | t1 = newIRTemp(bb->tyenv, ty); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 329 | addStmtToIRSB(bb, IRStmt_WrTmp(t1, |
sewardj | d7cb853 | 2004-08-17 23:59:23 +0000 | [diff] [blame] | 330 | IRExpr_Unop(ex->Iex.Unop.op, |
| 331 | flatten_Expr(bb, ex->Iex.Unop.arg)))); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 332 | return IRExpr_RdTmp(t1); |
sewardj | d7cb853 | 2004-08-17 23:59:23 +0000 | [diff] [blame] | 333 | |
sewardj | af1ceca | 2005-06-30 23:31:27 +0000 | [diff] [blame] | 334 | case Iex_Load: |
sewardj | d7cb853 | 2004-08-17 23:59:23 +0000 | [diff] [blame] | 335 | t1 = newIRTemp(bb->tyenv, ty); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 336 | addStmtToIRSB(bb, IRStmt_WrTmp(t1, |
sewardj | af1ceca | 2005-06-30 23:31:27 +0000 | [diff] [blame] | 337 | IRExpr_Load(ex->Iex.Load.end, |
| 338 | ex->Iex.Load.ty, |
| 339 | flatten_Expr(bb, ex->Iex.Load.addr)))); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 340 | return IRExpr_RdTmp(t1); |
sewardj | d7cb853 | 2004-08-17 23:59:23 +0000 | [diff] [blame] | 341 | |
| 342 | case Iex_CCall: |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 343 | newargs = shallowCopyIRExprVec(ex->Iex.CCall.args); |
sewardj | d7cb853 | 2004-08-17 23:59:23 +0000 | [diff] [blame] | 344 | for (i = 0; newargs[i]; i++) |
| 345 | newargs[i] = flatten_Expr(bb, newargs[i]); |
| 346 | t1 = newIRTemp(bb->tyenv, ty); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 347 | addStmtToIRSB(bb, IRStmt_WrTmp(t1, |
sewardj | 8ea867b | 2004-10-30 19:03:02 +0000 | [diff] [blame] | 348 | IRExpr_CCall(ex->Iex.CCall.cee, |
sewardj | d7cb853 | 2004-08-17 23:59:23 +0000 | [diff] [blame] | 349 | ex->Iex.CCall.retty, |
| 350 | newargs))); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 351 | return IRExpr_RdTmp(t1); |
sewardj | d7cb853 | 2004-08-17 23:59:23 +0000 | [diff] [blame] | 352 | |
| 353 | case Iex_Mux0X: |
| 354 | t1 = newIRTemp(bb->tyenv, ty); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 355 | addStmtToIRSB(bb, IRStmt_WrTmp(t1, |
sewardj | d7cb853 | 2004-08-17 23:59:23 +0000 | [diff] [blame] | 356 | IRExpr_Mux0X(flatten_Expr(bb, ex->Iex.Mux0X.cond), |
| 357 | flatten_Expr(bb, ex->Iex.Mux0X.expr0), |
| 358 | flatten_Expr(bb, ex->Iex.Mux0X.exprX)))); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 359 | return IRExpr_RdTmp(t1); |
sewardj | d7cb853 | 2004-08-17 23:59:23 +0000 | [diff] [blame] | 360 | |
| 361 | case Iex_Const: |
sewardj | c0b4228 | 2004-10-12 13:44:12 +0000 | [diff] [blame] | 362 | /* Lift F64i constants out onto temps so they can be CSEd |
| 363 | later. */ |
| 364 | if (ex->Iex.Const.con->tag == Ico_F64i) { |
| 365 | t1 = newIRTemp(bb->tyenv, ty); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 366 | addStmtToIRSB(bb, IRStmt_WrTmp(t1, |
sewardj | c0b4228 | 2004-10-12 13:44:12 +0000 | [diff] [blame] | 367 | IRExpr_Const(ex->Iex.Const.con))); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 368 | return IRExpr_RdTmp(t1); |
sewardj | c0b4228 | 2004-10-12 13:44:12 +0000 | [diff] [blame] | 369 | } else { |
| 370 | /* Leave all other constants alone. */ |
| 371 | return ex; |
| 372 | } |
| 373 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 374 | case Iex_RdTmp: |
sewardj | d7cb853 | 2004-08-17 23:59:23 +0000 | [diff] [blame] | 375 | return ex; |
| 376 | |
| 377 | default: |
| 378 | vex_printf("\n"); |
| 379 | ppIRExpr(ex); |
| 380 | vex_printf("\n"); |
| 381 | vpanic("flatten_Expr"); |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | |
| 386 | /* Append a completely flattened form of 'st' to the end of 'bb'. */ |
| 387 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 388 | static void flatten_Stmt ( IRSB* bb, IRStmt* st ) |
sewardj | d7cb853 | 2004-08-17 23:59:23 +0000 | [diff] [blame] | 389 | { |
sewardj | 17442fe | 2004-09-20 14:54:28 +0000 | [diff] [blame] | 390 | Int i; |
| 391 | IRExpr *e1, *e2; |
| 392 | IRDirty *d, *d2; |
sewardj | d7cb853 | 2004-08-17 23:59:23 +0000 | [diff] [blame] | 393 | switch (st->tag) { |
| 394 | case Ist_Put: |
sewardj | 496a58d | 2005-03-20 18:44:44 +0000 | [diff] [blame] | 395 | if (isIRAtom(st->Ist.Put.data)) { |
sewardj | 49651f4 | 2004-10-28 22:11:04 +0000 | [diff] [blame] | 396 | /* optimisation to reduce the amount of heap wasted |
| 397 | by the flattener */ |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 398 | addStmtToIRSB(bb, st); |
sewardj | 49651f4 | 2004-10-28 22:11:04 +0000 | [diff] [blame] | 399 | } else { |
| 400 | /* general case, always correct */ |
| 401 | e1 = flatten_Expr(bb, st->Ist.Put.data); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 402 | addStmtToIRSB(bb, IRStmt_Put(st->Ist.Put.offset, e1)); |
sewardj | 49651f4 | 2004-10-28 22:11:04 +0000 | [diff] [blame] | 403 | } |
sewardj | d7cb853 | 2004-08-17 23:59:23 +0000 | [diff] [blame] | 404 | break; |
sewardj | d7cb853 | 2004-08-17 23:59:23 +0000 | [diff] [blame] | 405 | case Ist_PutI: |
sewardj | eeac841 | 2004-11-02 00:26:55 +0000 | [diff] [blame] | 406 | e1 = flatten_Expr(bb, st->Ist.PutI.ix); |
sewardj | 2d3f77c | 2004-09-22 23:49:09 +0000 | [diff] [blame] | 407 | e2 = flatten_Expr(bb, st->Ist.PutI.data); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 408 | addStmtToIRSB(bb, IRStmt_PutI(st->Ist.PutI.descr, |
sewardj | 2d3f77c | 2004-09-22 23:49:09 +0000 | [diff] [blame] | 409 | e1, |
| 410 | st->Ist.PutI.bias, |
| 411 | e2)); |
sewardj | d721703 | 2004-08-19 10:49:10 +0000 | [diff] [blame] | 412 | break; |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 413 | case Ist_WrTmp: |
| 414 | if (isFlat(st->Ist.WrTmp.data)) { |
sewardj | e80679a | 2004-09-21 23:00:11 +0000 | [diff] [blame] | 415 | /* optimisation, to reduce the number of tmp-tmp |
| 416 | copies generated */ |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 417 | addStmtToIRSB(bb, st); |
sewardj | e80679a | 2004-09-21 23:00:11 +0000 | [diff] [blame] | 418 | } else { |
| 419 | /* general case, always correct */ |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 420 | e1 = flatten_Expr(bb, st->Ist.WrTmp.data); |
| 421 | addStmtToIRSB(bb, IRStmt_WrTmp(st->Ist.WrTmp.tmp, e1)); |
sewardj | e80679a | 2004-09-21 23:00:11 +0000 | [diff] [blame] | 422 | } |
sewardj | d7cb853 | 2004-08-17 23:59:23 +0000 | [diff] [blame] | 423 | break; |
sewardj | af1ceca | 2005-06-30 23:31:27 +0000 | [diff] [blame] | 424 | case Ist_Store: |
| 425 | e1 = flatten_Expr(bb, st->Ist.Store.addr); |
| 426 | e2 = flatten_Expr(bb, st->Ist.Store.data); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 427 | addStmtToIRSB(bb, IRStmt_Store(st->Ist.Store.end, e1,e2)); |
sewardj | d7cb853 | 2004-08-17 23:59:23 +0000 | [diff] [blame] | 428 | break; |
sewardj | 17442fe | 2004-09-20 14:54:28 +0000 | [diff] [blame] | 429 | case Ist_Dirty: |
| 430 | d = st->Ist.Dirty.details; |
| 431 | d2 = emptyIRDirty(); |
| 432 | *d2 = *d; |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 433 | d2->args = shallowCopyIRExprVec(d2->args); |
sewardj | 17442fe | 2004-09-20 14:54:28 +0000 | [diff] [blame] | 434 | if (d2->mFx != Ifx_None) { |
| 435 | d2->mAddr = flatten_Expr(bb, d2->mAddr); |
| 436 | } else { |
| 437 | vassert(d2->mAddr == NULL); |
| 438 | } |
sewardj | b8385d8 | 2004-11-02 01:34:15 +0000 | [diff] [blame] | 439 | d2->guard = flatten_Expr(bb, d2->guard); |
sewardj | 17442fe | 2004-09-20 14:54:28 +0000 | [diff] [blame] | 440 | for (i = 0; d2->args[i]; i++) |
| 441 | d2->args[i] = flatten_Expr(bb, d2->args[i]); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 442 | addStmtToIRSB(bb, IRStmt_Dirty(d2)); |
sewardj | 17442fe | 2004-09-20 14:54:28 +0000 | [diff] [blame] | 443 | break; |
sewardj | d2445f6 | 2005-03-21 00:15:53 +0000 | [diff] [blame] | 444 | case Ist_NoOp: |
sewardj | 3e83893 | 2005-01-07 12:09:15 +0000 | [diff] [blame] | 445 | case Ist_MFence: |
sewardj | d2445f6 | 2005-03-21 00:15:53 +0000 | [diff] [blame] | 446 | case Ist_IMark: |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 447 | addStmtToIRSB(bb, st); |
sewardj | 3e83893 | 2005-01-07 12:09:15 +0000 | [diff] [blame] | 448 | break; |
sewardj | 5a9ffab | 2005-05-12 17:55:01 +0000 | [diff] [blame] | 449 | case Ist_AbiHint: |
| 450 | e1 = flatten_Expr(bb, st->Ist.AbiHint.base); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 451 | addStmtToIRSB(bb, IRStmt_AbiHint(e1, st->Ist.AbiHint.len)); |
sewardj | 5a9ffab | 2005-05-12 17:55:01 +0000 | [diff] [blame] | 452 | break; |
sewardj | d7cb853 | 2004-08-17 23:59:23 +0000 | [diff] [blame] | 453 | case Ist_Exit: |
sewardj | 0276d4b | 2004-11-15 15:30:21 +0000 | [diff] [blame] | 454 | e1 = flatten_Expr(bb, st->Ist.Exit.guard); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 455 | addStmtToIRSB(bb, IRStmt_Exit(e1, st->Ist.Exit.jk, |
sewardj | 893aada | 2004-11-29 19:57:54 +0000 | [diff] [blame] | 456 | st->Ist.Exit.dst)); |
sewardj | d7cb853 | 2004-08-17 23:59:23 +0000 | [diff] [blame] | 457 | break; |
| 458 | default: |
| 459 | vex_printf("\n"); |
| 460 | ppIRStmt(st); |
| 461 | vex_printf("\n"); |
| 462 | vpanic("flatten_Stmt"); |
| 463 | } |
| 464 | } |
| 465 | |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 466 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 467 | static IRSB* flatten_BB ( IRSB* in ) |
sewardj | d7cb853 | 2004-08-17 23:59:23 +0000 | [diff] [blame] | 468 | { |
| 469 | Int i; |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 470 | IRSB* out; |
| 471 | out = emptyIRSB(); |
| 472 | out->tyenv = deepCopyIRTypeEnv( in->tyenv ); |
sewardj | d7cb853 | 2004-08-17 23:59:23 +0000 | [diff] [blame] | 473 | for (i = 0; i < in->stmts_used; i++) |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 474 | if (in->stmts[i]) |
| 475 | flatten_Stmt( out, in->stmts[i] ); |
sewardj | d7cb853 | 2004-08-17 23:59:23 +0000 | [diff] [blame] | 476 | out->next = flatten_Expr( out, in->next ); |
| 477 | out->jumpkind = in->jumpkind; |
| 478 | return out; |
| 479 | } |
| 480 | |
sewardj | edf4d69 | 2004-08-17 13:52:58 +0000 | [diff] [blame] | 481 | |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 482 | /*---------------------------------------------------------------*/ |
| 483 | /*--- In-place removal of redundant GETs ---*/ |
| 484 | /*---------------------------------------------------------------*/ |
| 485 | |
| 486 | /* Scan forwards, building up an environment binding (min offset, max |
| 487 | offset) pairs to values, which will either be temps or constants. |
| 488 | |
| 489 | On seeing 't = Get(minoff,maxoff)', look up (minoff,maxoff) in the |
| 490 | env and if it matches, replace the Get with the stored value. If |
| 491 | there is no match, add a (minoff,maxoff) :-> t binding. |
| 492 | |
| 493 | On seeing 'Put (minoff,maxoff) = t or c', first remove in the env |
| 494 | any binding which fully or partially overlaps with (minoff,maxoff). |
| 495 | Then add a new (minoff,maxoff) :-> t or c binding. */ |
| 496 | |
| 497 | /* Extract the min/max offsets from a guest state array descriptor. */ |
| 498 | |
| 499 | inline |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 500 | static void getArrayBounds ( IRRegArray* descr, |
| 501 | UInt* minoff, UInt* maxoff ) |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 502 | { |
| 503 | *minoff = descr->base; |
| 504 | *maxoff = *minoff + descr->nElems*sizeofIRType(descr->elemTy) - 1; |
| 505 | vassert((*minoff & ~0xFFFF) == 0); |
| 506 | vassert((*maxoff & ~0xFFFF) == 0); |
| 507 | vassert(*minoff <= *maxoff); |
| 508 | } |
| 509 | |
| 510 | /* Create keys, of the form ((minoffset << 16) | maxoffset). */ |
| 511 | |
| 512 | static UInt mk_key_GetPut ( Int offset, IRType ty ) |
| 513 | { |
| 514 | /* offset should fit in 16 bits. */ |
| 515 | UInt minoff = offset; |
| 516 | UInt maxoff = minoff + sizeofIRType(ty) - 1; |
| 517 | vassert((minoff & ~0xFFFF) == 0); |
| 518 | vassert((maxoff & ~0xFFFF) == 0); |
| 519 | return (minoff << 16) | maxoff; |
| 520 | } |
| 521 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 522 | static UInt mk_key_GetIPutI ( IRRegArray* descr ) |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 523 | { |
| 524 | UInt minoff, maxoff; |
| 525 | getArrayBounds( descr, &minoff, &maxoff ); |
| 526 | vassert((minoff & ~0xFFFF) == 0); |
| 527 | vassert((maxoff & ~0xFFFF) == 0); |
| 528 | return (minoff << 16) | maxoff; |
| 529 | } |
| 530 | |
| 531 | /* Supposing h has keys of the form generated by mk_key_GetPut and |
| 532 | mk_key_GetIPutI, invalidate any key which overlaps (k_lo |
| 533 | .. k_hi). |
| 534 | */ |
| 535 | static void invalidateOverlaps ( HashHW* h, UInt k_lo, UInt k_hi ) |
| 536 | { |
| 537 | Int j; |
| 538 | UInt e_lo, e_hi; |
| 539 | vassert(k_lo <= k_hi); |
| 540 | /* invalidate any env entries which in any way overlap (k_lo |
| 541 | .. k_hi) */ |
| 542 | /* vex_printf("invalidate %d .. %d\n", k_lo, k_hi ); */ |
| 543 | |
| 544 | for (j = 0; j < h->used; j++) { |
| 545 | if (!h->inuse[j]) |
| 546 | continue; |
| 547 | e_lo = (((UInt)h->key[j]) >> 16) & 0xFFFF; |
| 548 | e_hi = ((UInt)h->key[j]) & 0xFFFF; |
| 549 | vassert(e_lo <= e_hi); |
| 550 | if (e_hi < k_lo || k_hi < e_lo) |
| 551 | continue; /* no overlap possible */ |
| 552 | else |
| 553 | /* overlap; invalidate */ |
| 554 | h->inuse[j] = False; |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 559 | static void redundant_get_removal_BB ( IRSB* bb ) |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 560 | { |
| 561 | HashHW* env = newHHW(); |
| 562 | UInt key = 0; /* keep gcc -O happy */ |
| 563 | Int i, j; |
| 564 | HWord val; |
| 565 | |
| 566 | for (i = 0; i < bb->stmts_used; i++) { |
| 567 | IRStmt* st = bb->stmts[i]; |
| 568 | |
sewardj | 8bee6d1 | 2005-03-22 02:24:05 +0000 | [diff] [blame] | 569 | if (st->tag == Ist_NoOp) |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 570 | continue; |
| 571 | |
| 572 | /* Deal with Gets */ |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 573 | if (st->tag == Ist_WrTmp |
| 574 | && st->Ist.WrTmp.data->tag == Iex_Get) { |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 575 | /* st is 't = Get(...)'. Look up in the environment and see |
| 576 | if the Get can be replaced. */ |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 577 | IRExpr* get = st->Ist.WrTmp.data; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 578 | key = (HWord)mk_key_GetPut( get->Iex.Get.offset, |
| 579 | get->Iex.Get.ty ); |
| 580 | if (lookupHHW(env, &val, (HWord)key)) { |
| 581 | /* found it */ |
| 582 | /* Note, we could do better here. If the types are |
| 583 | different we don't do the substitution, since doing so |
| 584 | could lead to invalidly-typed IR. An improvement would |
| 585 | be to stick in a reinterpret-style cast, although that |
| 586 | would make maintaining flatness more difficult. */ |
| 587 | IRExpr* valE = (IRExpr*)val; |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 588 | Bool typesOK = toBool( typeOfIRExpr(bb->tyenv,valE) |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 589 | == st->Ist.WrTmp.data->Iex.Get.ty ); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 590 | if (typesOK && DEBUG_IROPT) { |
| 591 | vex_printf("rGET: "); ppIRExpr(get); |
| 592 | vex_printf(" -> "); ppIRExpr(valE); |
| 593 | vex_printf("\n"); |
| 594 | } |
| 595 | if (typesOK) |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 596 | bb->stmts[i] = IRStmt_WrTmp(st->Ist.WrTmp.tmp, valE); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 597 | } else { |
| 598 | /* Not found, but at least we know that t and the Get(...) |
| 599 | are now associated. So add a binding to reflect that |
| 600 | fact. */ |
| 601 | addToHHW( env, (HWord)key, |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 602 | (HWord)(void*)(IRExpr_RdTmp(st->Ist.WrTmp.tmp)) ); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 603 | } |
| 604 | } |
| 605 | |
| 606 | /* Deal with Puts: invalidate any env entries overlapped by this |
| 607 | Put */ |
| 608 | if (st->tag == Ist_Put || st->tag == Ist_PutI) { |
| 609 | UInt k_lo, k_hi; |
| 610 | if (st->tag == Ist_Put) { |
| 611 | key = mk_key_GetPut( st->Ist.Put.offset, |
| 612 | typeOfIRExpr(bb->tyenv,st->Ist.Put.data) ); |
| 613 | } else { |
| 614 | vassert(st->tag == Ist_PutI); |
| 615 | key = mk_key_GetIPutI( st->Ist.PutI.descr ); |
| 616 | } |
| 617 | |
| 618 | k_lo = (key >> 16) & 0xFFFF; |
| 619 | k_hi = key & 0xFFFF; |
| 620 | invalidateOverlaps(env, k_lo, k_hi); |
| 621 | } |
| 622 | else |
| 623 | if (st->tag == Ist_Dirty) { |
| 624 | /* Deal with dirty helpers which write or modify guest state. |
| 625 | Invalidate the entire env. We could do a lot better |
| 626 | here. */ |
| 627 | IRDirty* d = st->Ist.Dirty.details; |
| 628 | Bool writes = False; |
| 629 | for (j = 0; j < d->nFxState; j++) { |
| 630 | if (d->fxState[j].fx == Ifx_Modify |
| 631 | || d->fxState[j].fx == Ifx_Write) |
| 632 | writes = True; |
| 633 | } |
| 634 | if (writes) { |
| 635 | /* dump the entire env (not clever, but correct ...) */ |
| 636 | for (j = 0; j < env->used; j++) |
| 637 | env->inuse[j] = False; |
| 638 | if (0) vex_printf("rGET: trash env due to dirty helper\n"); |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | /* add this one to the env, if appropriate */ |
| 643 | if (st->tag == Ist_Put) { |
sewardj | 496a58d | 2005-03-20 18:44:44 +0000 | [diff] [blame] | 644 | vassert(isIRAtom(st->Ist.Put.data)); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 645 | addToHHW( env, (HWord)key, (HWord)(st->Ist.Put.data)); |
| 646 | } |
| 647 | |
| 648 | } /* for (i = 0; i < bb->stmts_used; i++) */ |
| 649 | |
| 650 | } |
| 651 | |
| 652 | |
| 653 | /*---------------------------------------------------------------*/ |
| 654 | /*--- In-place removal of redundant PUTs ---*/ |
| 655 | /*---------------------------------------------------------------*/ |
| 656 | |
| 657 | /* Find any Get uses in st and invalidate any partially or fully |
| 658 | overlapping ranges listed in env. Due to the flattening phase, the |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 659 | only stmt kind we expect to find a Get on is IRStmt_WrTmp. */ |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 660 | |
| 661 | static void handle_gets_Stmt ( |
| 662 | HashHW* env, |
| 663 | IRStmt* st, |
| 664 | Bool (*preciseMemExnsFn)(Int,Int) |
| 665 | ) |
| 666 | { |
| 667 | Int j; |
| 668 | UInt key = 0; /* keep gcc -O happy */ |
| 669 | Bool isGet; |
| 670 | Bool memRW = False; |
| 671 | IRExpr* e; |
| 672 | |
| 673 | switch (st->tag) { |
| 674 | |
| 675 | /* This is the only interesting case. Deal with Gets in the RHS |
| 676 | expression. */ |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 677 | case Ist_WrTmp: |
| 678 | e = st->Ist.WrTmp.data; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 679 | switch (e->tag) { |
| 680 | case Iex_Get: |
| 681 | isGet = True; |
| 682 | key = mk_key_GetPut ( e->Iex.Get.offset, e->Iex.Get.ty ); |
| 683 | break; |
| 684 | case Iex_GetI: |
| 685 | isGet = True; |
| 686 | key = mk_key_GetIPutI ( e->Iex.GetI.descr ); |
| 687 | break; |
sewardj | af1ceca | 2005-06-30 23:31:27 +0000 | [diff] [blame] | 688 | case Iex_Load: |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 689 | isGet = False; |
| 690 | memRW = True; |
| 691 | break; |
| 692 | default: |
| 693 | isGet = False; |
| 694 | } |
| 695 | if (isGet) { |
| 696 | UInt k_lo, k_hi; |
| 697 | k_lo = (key >> 16) & 0xFFFF; |
| 698 | k_hi = key & 0xFFFF; |
| 699 | invalidateOverlaps(env, k_lo, k_hi); |
| 700 | } |
| 701 | break; |
| 702 | |
| 703 | /* Be very conservative for dirty helper calls; dump the entire |
| 704 | environment. The helper might read guest state, in which |
| 705 | case it needs to be flushed first. Also, the helper might |
| 706 | access guest memory, in which case all parts of the guest |
| 707 | state requiring precise exceptions needs to be flushed. The |
| 708 | crude solution is just to flush everything; we could easily |
| 709 | enough do a lot better if needed. */ |
sewardj | 3e83893 | 2005-01-07 12:09:15 +0000 | [diff] [blame] | 710 | /* Probably also overly-conservative, but also dump everything |
sewardj | 5a9ffab | 2005-05-12 17:55:01 +0000 | [diff] [blame] | 711 | if we hit a memory fence. Ditto AbiHints.*/ |
| 712 | case Ist_AbiHint: |
| 713 | vassert(isIRAtom(st->Ist.AbiHint.base)); |
| 714 | /* fall through */ |
sewardj | 3e83893 | 2005-01-07 12:09:15 +0000 | [diff] [blame] | 715 | case Ist_MFence: |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 716 | case Ist_Dirty: |
| 717 | for (j = 0; j < env->used; j++) |
| 718 | env->inuse[j] = False; |
| 719 | break; |
| 720 | |
| 721 | /* all other cases are boring. */ |
sewardj | af1ceca | 2005-06-30 23:31:27 +0000 | [diff] [blame] | 722 | case Ist_Store: |
| 723 | vassert(isIRAtom(st->Ist.Store.addr)); |
| 724 | vassert(isIRAtom(st->Ist.Store.data)); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 725 | memRW = True; |
| 726 | break; |
| 727 | |
| 728 | case Ist_Exit: |
sewardj | 496a58d | 2005-03-20 18:44:44 +0000 | [diff] [blame] | 729 | vassert(isIRAtom(st->Ist.Exit.guard)); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 730 | break; |
| 731 | |
| 732 | case Ist_PutI: |
sewardj | 496a58d | 2005-03-20 18:44:44 +0000 | [diff] [blame] | 733 | vassert(isIRAtom(st->Ist.PutI.ix)); |
| 734 | vassert(isIRAtom(st->Ist.PutI.data)); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 735 | break; |
| 736 | |
sewardj | d2445f6 | 2005-03-21 00:15:53 +0000 | [diff] [blame] | 737 | case Ist_NoOp: |
sewardj | f168931 | 2005-03-16 18:19:10 +0000 | [diff] [blame] | 738 | case Ist_IMark: |
| 739 | break; |
| 740 | |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 741 | default: |
| 742 | vex_printf("\n"); |
| 743 | ppIRStmt(st); |
| 744 | vex_printf("\n"); |
| 745 | vpanic("handle_gets_Stmt"); |
| 746 | } |
| 747 | |
| 748 | if (memRW) { |
| 749 | /* This statement accesses memory. So we need to dump all parts |
| 750 | of the environment corresponding to guest state that may not |
| 751 | be reordered with respect to memory references. That means |
| 752 | at least the stack pointer. */ |
| 753 | for (j = 0; j < env->used; j++) { |
| 754 | if (!env->inuse[j]) |
| 755 | continue; |
| 756 | if (vex_control.iropt_precise_memory_exns) { |
| 757 | /* Precise exceptions required. Flush all guest state. */ |
| 758 | env->inuse[j] = False; |
| 759 | } else { |
| 760 | /* Just flush the minimal amount required, as computed by |
| 761 | preciseMemExnsFn. */ |
| 762 | HWord k_lo = (env->key[j] >> 16) & 0xFFFF; |
| 763 | HWord k_hi = env->key[j] & 0xFFFF; |
| 764 | if (preciseMemExnsFn( k_lo, k_hi )) |
| 765 | env->inuse[j] = False; |
| 766 | } |
| 767 | } |
| 768 | } /* if (memRW) */ |
| 769 | |
| 770 | } |
| 771 | |
| 772 | |
| 773 | /* Scan backwards, building up a set of (min offset, max |
| 774 | offset) pairs, indicating those parts of the guest state |
| 775 | for which the next event is a write. |
| 776 | |
| 777 | On seeing a conditional exit, empty the set. |
| 778 | |
| 779 | On seeing 'Put (minoff,maxoff) = t or c', if (minoff,maxoff) is |
| 780 | completely within the set, remove the Put. Otherwise, add |
| 781 | (minoff,maxoff) to the set. |
| 782 | |
| 783 | On seeing 'Get (minoff,maxoff)', remove any part of the set |
sewardj | 9843029 | 2004-12-29 17:34:50 +0000 | [diff] [blame] | 784 | overlapping (minoff,maxoff). The same has to happen for any events |
| 785 | which implicitly read parts of the guest state: dirty helper calls |
| 786 | and loads/stores. |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 787 | */ |
| 788 | |
| 789 | static void redundant_put_removal_BB ( |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 790 | IRSB* bb, |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 791 | Bool (*preciseMemExnsFn)(Int,Int) |
| 792 | ) |
| 793 | { |
| 794 | Int i, j; |
| 795 | Bool isPut; |
| 796 | IRStmt* st; |
| 797 | UInt key = 0; /* keep gcc -O happy */ |
| 798 | |
| 799 | HashHW* env = newHHW(); |
| 800 | for (i = bb->stmts_used-1; i >= 0; i--) { |
| 801 | st = bb->stmts[i]; |
sewardj | 8bee6d1 | 2005-03-22 02:24:05 +0000 | [diff] [blame] | 802 | |
| 803 | if (st->tag == Ist_NoOp) |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 804 | continue; |
| 805 | |
| 806 | /* Deal with conditional exits. */ |
| 807 | if (st->tag == Ist_Exit) { |
| 808 | /* Since control may not get beyond this point, we must empty |
| 809 | out the set, since we can no longer claim that the next |
| 810 | event for any part of the guest state is definitely a |
| 811 | write. */ |
sewardj | 496a58d | 2005-03-20 18:44:44 +0000 | [diff] [blame] | 812 | vassert(isIRAtom(st->Ist.Exit.guard)); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 813 | for (j = 0; j < env->used; j++) |
| 814 | env->inuse[j] = False; |
| 815 | continue; |
| 816 | } |
| 817 | |
| 818 | /* Deal with Puts */ |
| 819 | switch (st->tag) { |
| 820 | case Ist_Put: |
| 821 | isPut = True; |
| 822 | key = mk_key_GetPut( st->Ist.Put.offset, |
| 823 | typeOfIRExpr(bb->tyenv,st->Ist.Put.data) ); |
sewardj | 496a58d | 2005-03-20 18:44:44 +0000 | [diff] [blame] | 824 | vassert(isIRAtom(st->Ist.Put.data)); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 825 | break; |
| 826 | case Ist_PutI: |
| 827 | isPut = True; |
| 828 | key = mk_key_GetIPutI( st->Ist.PutI.descr ); |
sewardj | 496a58d | 2005-03-20 18:44:44 +0000 | [diff] [blame] | 829 | vassert(isIRAtom(st->Ist.PutI.ix)); |
| 830 | vassert(isIRAtom(st->Ist.PutI.data)); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 831 | break; |
| 832 | default: |
| 833 | isPut = False; |
| 834 | } |
| 835 | if (isPut && st->tag != Ist_PutI) { |
| 836 | /* See if any single entry in env overlaps this Put. This is |
| 837 | simplistic in that the transformation is valid if, say, two |
| 838 | or more entries in the env overlap this Put, but the use of |
| 839 | lookupHHW will only find a single entry which exactly |
| 840 | overlaps this Put. This is suboptimal but safe. */ |
| 841 | if (lookupHHW(env, NULL, (HWord)key)) { |
| 842 | /* This Put is redundant because a later one will overwrite |
| 843 | it. So NULL (nop) it out. */ |
| 844 | if (DEBUG_IROPT) { |
| 845 | vex_printf("rPUT: "); ppIRStmt(st); |
| 846 | vex_printf("\n"); |
| 847 | } |
sewardj | d2445f6 | 2005-03-21 00:15:53 +0000 | [diff] [blame] | 848 | bb->stmts[i] = IRStmt_NoOp(); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 849 | } else { |
| 850 | /* We can't demonstrate that this Put is redundant, so add it |
| 851 | to the running collection. */ |
| 852 | addToHHW(env, (HWord)key, 0); |
| 853 | } |
| 854 | continue; |
| 855 | } |
| 856 | |
| 857 | /* Deal with Gets. These remove bits of the environment since |
| 858 | appearance of a Get means that the next event for that slice |
sewardj | 9843029 | 2004-12-29 17:34:50 +0000 | [diff] [blame] | 859 | of the guest state is no longer a write, but a read. Also |
| 860 | deals with implicit reads of guest state needed to maintain |
| 861 | precise exceptions. */ |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 862 | handle_gets_Stmt( env, st, preciseMemExnsFn ); |
| 863 | } |
| 864 | } |
| 865 | |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 866 | |
| 867 | /*---------------------------------------------------------------*/ |
| 868 | /*--- Constant propagation and folding ---*/ |
| 869 | /*---------------------------------------------------------------*/ |
| 870 | |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 871 | /* The env in this section is a map from IRTemp to IRExpr*, |
| 872 | that is, an array indexed by IRTemp. */ |
sewardj | f650199 | 2004-08-27 11:58:24 +0000 | [diff] [blame] | 873 | |
sewardj | f672901 | 2004-08-25 12:45:13 +0000 | [diff] [blame] | 874 | /* Are both expressions simply the same IRTemp ? */ |
| 875 | static Bool sameIRTemps ( IRExpr* e1, IRExpr* e2 ) |
| 876 | { |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 877 | return toBool( e1->tag == Iex_RdTmp |
| 878 | && e2->tag == Iex_RdTmp |
| 879 | && e1->Iex.RdTmp.tmp == e2->Iex.RdTmp.tmp ); |
sewardj | f672901 | 2004-08-25 12:45:13 +0000 | [diff] [blame] | 880 | } |
| 881 | |
sewardj | e1d45da | 2004-11-12 00:13:21 +0000 | [diff] [blame] | 882 | static Bool notBool ( Bool b ) |
| 883 | { |
| 884 | if (b == True) return False; |
| 885 | if (b == False) return True; |
| 886 | vpanic("notBool"); |
| 887 | } |
| 888 | |
sewardj | 0033ddc | 2005-04-26 23:34:34 +0000 | [diff] [blame] | 889 | /* Make a zero which has the same type as the result of the given |
| 890 | primop. */ |
| 891 | static IRExpr* mkZeroForXor ( IROp op ) |
| 892 | { |
| 893 | switch (op) { |
| 894 | case Iop_Xor8: return IRExpr_Const(IRConst_U8(0)); |
| 895 | case Iop_Xor16: return IRExpr_Const(IRConst_U16(0)); |
| 896 | case Iop_Xor32: return IRExpr_Const(IRConst_U32(0)); |
| 897 | case Iop_Xor64: return IRExpr_Const(IRConst_U64(0)); |
sewardj | 0474427 | 2007-01-16 19:19:55 +0000 | [diff] [blame] | 898 | case Iop_XorV128: return IRExpr_Const(IRConst_V128(0)); |
sewardj | 0033ddc | 2005-04-26 23:34:34 +0000 | [diff] [blame] | 899 | default: vpanic("mkZeroForXor: bad primop"); |
| 900 | } |
| 901 | } |
| 902 | |
| 903 | |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 904 | static IRExpr* fold_Expr ( IRExpr* e ) |
| 905 | { |
sewardj | 278c44c | 2004-08-20 00:28:13 +0000 | [diff] [blame] | 906 | Int shift; |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 907 | IRExpr* e2 = e; /* e2 is the result of folding e, if possible */ |
| 908 | |
| 909 | /* UNARY ops */ |
| 910 | if (e->tag == Iex_Unop |
| 911 | && e->Iex.Unop.arg->tag == Iex_Const) { |
| 912 | switch (e->Iex.Unop.op) { |
sewardj | ae27ab6 | 2004-10-15 21:21:46 +0000 | [diff] [blame] | 913 | case Iop_1Uto8: |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 914 | e2 = IRExpr_Const(IRConst_U8(toUChar( |
sewardj | ba99931 | 2004-11-15 15:21:17 +0000 | [diff] [blame] | 915 | e->Iex.Unop.arg->Iex.Const.con->Ico.U1 |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 916 | ? 1 : 0))); |
sewardj | ae27ab6 | 2004-10-15 21:21:46 +0000 | [diff] [blame] | 917 | break; |
sewardj | f4a821d | 2004-10-09 00:58:19 +0000 | [diff] [blame] | 918 | case Iop_1Uto32: |
| 919 | e2 = IRExpr_Const(IRConst_U32( |
sewardj | ba99931 | 2004-11-15 15:21:17 +0000 | [diff] [blame] | 920 | e->Iex.Unop.arg->Iex.Const.con->Ico.U1 |
sewardj | f4a821d | 2004-10-09 00:58:19 +0000 | [diff] [blame] | 921 | ? 1 : 0)); |
| 922 | break; |
sewardj | 2716ff1 | 2005-05-20 19:21:45 +0000 | [diff] [blame] | 923 | case Iop_1Uto64: |
| 924 | e2 = IRExpr_Const(IRConst_U64( |
| 925 | e->Iex.Unop.arg->Iex.Const.con->Ico.U1 |
| 926 | ? 1 : 0)); |
| 927 | break; |
sewardj | e6b3993 | 2004-11-06 17:01:15 +0000 | [diff] [blame] | 928 | |
sewardj | 1bee561 | 2005-11-10 18:10:58 +0000 | [diff] [blame] | 929 | case Iop_1Sto8: |
| 930 | e2 = IRExpr_Const(IRConst_U8(toUChar( |
| 931 | e->Iex.Unop.arg->Iex.Const.con->Ico.U1 |
| 932 | ? 0xFF : 0))); |
| 933 | break; |
sewardj | 68884ef | 2005-07-18 13:58:49 +0000 | [diff] [blame] | 934 | case Iop_1Sto16: |
sewardj | 743d8f0 | 2005-07-27 00:22:37 +0000 | [diff] [blame] | 935 | e2 = IRExpr_Const(IRConst_U16(toUShort( |
sewardj | 68884ef | 2005-07-18 13:58:49 +0000 | [diff] [blame] | 936 | e->Iex.Unop.arg->Iex.Const.con->Ico.U1 |
sewardj | 743d8f0 | 2005-07-27 00:22:37 +0000 | [diff] [blame] | 937 | ? 0xFFFF : 0))); |
sewardj | 68884ef | 2005-07-18 13:58:49 +0000 | [diff] [blame] | 938 | break; |
sewardj | d999788 | 2004-11-04 19:42:59 +0000 | [diff] [blame] | 939 | case Iop_1Sto32: |
| 940 | e2 = IRExpr_Const(IRConst_U32( |
sewardj | ba99931 | 2004-11-15 15:21:17 +0000 | [diff] [blame] | 941 | e->Iex.Unop.arg->Iex.Const.con->Ico.U1 |
sewardj | d999788 | 2004-11-04 19:42:59 +0000 | [diff] [blame] | 942 | ? 0xFFFFFFFF : 0)); |
| 943 | break; |
sewardj | e6b3993 | 2004-11-06 17:01:15 +0000 | [diff] [blame] | 944 | case Iop_1Sto64: |
| 945 | e2 = IRExpr_Const(IRConst_U64( |
sewardj | ba99931 | 2004-11-15 15:21:17 +0000 | [diff] [blame] | 946 | e->Iex.Unop.arg->Iex.Const.con->Ico.U1 |
sewardj | e6b3993 | 2004-11-06 17:01:15 +0000 | [diff] [blame] | 947 | ? 0xFFFFFFFFFFFFFFFFULL : 0)); |
| 948 | break; |
| 949 | |
sewardj | 883b00b | 2004-09-11 09:30:24 +0000 | [diff] [blame] | 950 | case Iop_8Sto32: { |
| 951 | /* signed */ Int s32 = e->Iex.Unop.arg->Iex.Const.con->Ico.U8; |
| 952 | s32 <<= 24; |
| 953 | s32 >>= 24; |
| 954 | e2 = IRExpr_Const(IRConst_U32((UInt)s32)); |
| 955 | break; |
| 956 | } |
sewardj | 291a7e8 | 2005-04-27 11:42:44 +0000 | [diff] [blame] | 957 | case Iop_8Uto64: |
| 958 | e2 = IRExpr_Const(IRConst_U64( |
| 959 | 0xFFULL & e->Iex.Unop.arg->Iex.Const.con->Ico.U8)); |
| 960 | break; |
| 961 | case Iop_16Uto64: |
| 962 | e2 = IRExpr_Const(IRConst_U64( |
| 963 | 0xFFFFULL & e->Iex.Unop.arg->Iex.Const.con->Ico.U16)); |
| 964 | break; |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 965 | case Iop_8Uto32: |
| 966 | e2 = IRExpr_Const(IRConst_U32( |
| 967 | 0xFF & e->Iex.Unop.arg->Iex.Const.con->Ico.U8)); |
| 968 | break; |
| 969 | case Iop_16Uto32: |
| 970 | e2 = IRExpr_Const(IRConst_U32( |
| 971 | 0xFFFF & e->Iex.Unop.arg->Iex.Const.con->Ico.U16)); |
| 972 | break; |
sewardj | 7301743 | 2004-10-14 19:33:25 +0000 | [diff] [blame] | 973 | case Iop_32to16: |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 974 | e2 = IRExpr_Const(IRConst_U16(toUShort( |
| 975 | 0xFFFF & e->Iex.Unop.arg->Iex.Const.con->Ico.U32))); |
sewardj | 7301743 | 2004-10-14 19:33:25 +0000 | [diff] [blame] | 976 | break; |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 977 | case Iop_32to8: |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 978 | e2 = IRExpr_Const(IRConst_U8(toUChar( |
| 979 | 0xFF & e->Iex.Unop.arg->Iex.Const.con->Ico.U32))); |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 980 | break; |
sewardj | 7447b5b | 2004-10-16 11:30:42 +0000 | [diff] [blame] | 981 | case Iop_32to1: |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 982 | e2 = IRExpr_Const(IRConst_U1(toBool( |
| 983 | 1 == (1 & e->Iex.Unop.arg->Iex.Const.con->Ico.U32) |
| 984 | ))); |
sewardj | 7447b5b | 2004-10-16 11:30:42 +0000 | [diff] [blame] | 985 | break; |
sewardj | 291a7e8 | 2005-04-27 11:42:44 +0000 | [diff] [blame] | 986 | case Iop_64to1: |
| 987 | e2 = IRExpr_Const(IRConst_U1(toBool( |
| 988 | 1 == (1 & e->Iex.Unop.arg->Iex.Const.con->Ico.U64) |
| 989 | ))); |
| 990 | break; |
sewardj | e6b3993 | 2004-11-06 17:01:15 +0000 | [diff] [blame] | 991 | |
sewardj | f057afb | 2005-02-27 13:35:41 +0000 | [diff] [blame] | 992 | case Iop_Not64: |
| 993 | e2 = IRExpr_Const(IRConst_U64( |
| 994 | ~ (e->Iex.Unop.arg->Iex.Const.con->Ico.U64))); |
| 995 | break; |
sewardj | 883b00b | 2004-09-11 09:30:24 +0000 | [diff] [blame] | 996 | case Iop_Not32: |
| 997 | e2 = IRExpr_Const(IRConst_U32( |
| 998 | ~ (e->Iex.Unop.arg->Iex.Const.con->Ico.U32))); |
| 999 | break; |
sewardj | e6b3993 | 2004-11-06 17:01:15 +0000 | [diff] [blame] | 1000 | case Iop_Not16: |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1001 | e2 = IRExpr_Const(IRConst_U16(toUShort( |
| 1002 | ~ (e->Iex.Unop.arg->Iex.Const.con->Ico.U16)))); |
sewardj | e6b3993 | 2004-11-06 17:01:15 +0000 | [diff] [blame] | 1003 | break; |
sewardj | d999788 | 2004-11-04 19:42:59 +0000 | [diff] [blame] | 1004 | case Iop_Not8: |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1005 | e2 = IRExpr_Const(IRConst_U8(toUChar( |
| 1006 | ~ (e->Iex.Unop.arg->Iex.Const.con->Ico.U8)))); |
sewardj | d999788 | 2004-11-04 19:42:59 +0000 | [diff] [blame] | 1007 | break; |
sewardj | e6b3993 | 2004-11-06 17:01:15 +0000 | [diff] [blame] | 1008 | |
sewardj | e1d45da | 2004-11-12 00:13:21 +0000 | [diff] [blame] | 1009 | case Iop_Not1: |
sewardj | ba99931 | 2004-11-15 15:21:17 +0000 | [diff] [blame] | 1010 | e2 = IRExpr_Const(IRConst_U1( |
| 1011 | notBool(e->Iex.Unop.arg->Iex.Const.con->Ico.U1))); |
sewardj | e1d45da | 2004-11-12 00:13:21 +0000 | [diff] [blame] | 1012 | break; |
| 1013 | |
sewardj | 291a7e8 | 2005-04-27 11:42:44 +0000 | [diff] [blame] | 1014 | case Iop_Neg64: |
| 1015 | e2 = IRExpr_Const(IRConst_U64( |
| 1016 | - (e->Iex.Unop.arg->Iex.Const.con->Ico.U64))); |
| 1017 | break; |
sewardj | 0033ddc | 2005-04-26 23:34:34 +0000 | [diff] [blame] | 1018 | case Iop_Neg32: |
| 1019 | e2 = IRExpr_Const(IRConst_U32( |
| 1020 | - (e->Iex.Unop.arg->Iex.Const.con->Ico.U32))); |
| 1021 | break; |
| 1022 | case Iop_Neg8: |
sewardj | 65b17c6 | 2005-05-02 15:52:44 +0000 | [diff] [blame] | 1023 | e2 = IRExpr_Const(IRConst_U8(toUChar( |
| 1024 | - (e->Iex.Unop.arg->Iex.Const.con->Ico.U8)))); |
sewardj | 0033ddc | 2005-04-26 23:34:34 +0000 | [diff] [blame] | 1025 | break; |
| 1026 | |
sewardj | 291a7e8 | 2005-04-27 11:42:44 +0000 | [diff] [blame] | 1027 | case Iop_64to8: { |
| 1028 | ULong w64 = e->Iex.Unop.arg->Iex.Const.con->Ico.U64; |
| 1029 | w64 &= 0xFFULL; |
| 1030 | e2 = IRExpr_Const(IRConst_U8( (UChar)w64 )); |
| 1031 | break; |
| 1032 | } |
| 1033 | case Iop_64to16: { |
| 1034 | ULong w64 = e->Iex.Unop.arg->Iex.Const.con->Ico.U64; |
| 1035 | w64 &= 0xFFFFULL; |
sewardj | e85bc40 | 2005-05-06 16:29:26 +0000 | [diff] [blame] | 1036 | e2 = IRExpr_Const(IRConst_U16( (UShort)w64 )); |
sewardj | 291a7e8 | 2005-04-27 11:42:44 +0000 | [diff] [blame] | 1037 | break; |
| 1038 | } |
sewardj | 1d8ce20 | 2004-12-13 14:14:16 +0000 | [diff] [blame] | 1039 | case Iop_64to32: { |
| 1040 | ULong w64 = e->Iex.Unop.arg->Iex.Const.con->Ico.U64; |
| 1041 | w64 &= 0x00000000FFFFFFFFULL; |
| 1042 | e2 = IRExpr_Const(IRConst_U32( (UInt)w64 )); |
sewardj | 3701059 | 2004-12-13 10:47:15 +0000 | [diff] [blame] | 1043 | break; |
sewardj | 1d8ce20 | 2004-12-13 14:14:16 +0000 | [diff] [blame] | 1044 | } |
sewardj | 1d8ce20 | 2004-12-13 14:14:16 +0000 | [diff] [blame] | 1045 | case Iop_64HIto32: { |
| 1046 | ULong w64 = e->Iex.Unop.arg->Iex.Const.con->Ico.U64; |
| 1047 | w64 >>= 32; |
| 1048 | e2 = IRExpr_Const(IRConst_U32( (UInt)w64 )); |
| 1049 | break; |
| 1050 | } |
sewardj | b5710b8 | 2005-01-27 16:05:09 +0000 | [diff] [blame] | 1051 | case Iop_32Uto64: |
| 1052 | e2 = IRExpr_Const(IRConst_U64( |
| 1053 | 0xFFFFFFFFULL |
| 1054 | & e->Iex.Unop.arg->Iex.Const.con->Ico.U32)); |
| 1055 | break; |
sewardj | 3701059 | 2004-12-13 10:47:15 +0000 | [diff] [blame] | 1056 | |
sewardj | 0033ddc | 2005-04-26 23:34:34 +0000 | [diff] [blame] | 1057 | case Iop_CmpNEZ8: |
| 1058 | e2 = IRExpr_Const(IRConst_U1(toBool( |
| 1059 | 0 != |
| 1060 | (0xFF & e->Iex.Unop.arg->Iex.Const.con->Ico.U8) |
| 1061 | ))); |
| 1062 | break; |
| 1063 | case Iop_CmpNEZ32: |
| 1064 | e2 = IRExpr_Const(IRConst_U1(toBool( |
| 1065 | 0 != |
| 1066 | (0xFFFFFFFF & e->Iex.Unop.arg->Iex.Const.con->Ico.U32) |
| 1067 | ))); |
| 1068 | break; |
| 1069 | case Iop_CmpNEZ64: |
| 1070 | e2 = IRExpr_Const(IRConst_U1(toBool( |
| 1071 | 0ULL != e->Iex.Unop.arg->Iex.Const.con->Ico.U64 |
| 1072 | ))); |
| 1073 | break; |
| 1074 | |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 1075 | default: |
| 1076 | goto unhandled; |
| 1077 | } |
| 1078 | } |
| 1079 | |
| 1080 | /* BINARY ops */ |
| 1081 | if (e->tag == Iex_Binop) { |
| 1082 | if (e->Iex.Binop.arg1->tag == Iex_Const |
| 1083 | && e->Iex.Binop.arg2->tag == Iex_Const) { |
| 1084 | /* cases where both args are consts */ |
| 1085 | switch (e->Iex.Binop.op) { |
sewardj | e6b3993 | 2004-11-06 17:01:15 +0000 | [diff] [blame] | 1086 | |
sewardj | 855dc72 | 2005-02-17 09:26:05 +0000 | [diff] [blame] | 1087 | /* -- Or -- */ |
sewardj | d999788 | 2004-11-04 19:42:59 +0000 | [diff] [blame] | 1088 | case Iop_Or8: |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1089 | e2 = IRExpr_Const(IRConst_U8(toUChar( |
sewardj | d999788 | 2004-11-04 19:42:59 +0000 | [diff] [blame] | 1090 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U8 |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1091 | | e->Iex.Binop.arg2->Iex.Const.con->Ico.U8)))); |
sewardj | d999788 | 2004-11-04 19:42:59 +0000 | [diff] [blame] | 1092 | break; |
sewardj | e6b3993 | 2004-11-06 17:01:15 +0000 | [diff] [blame] | 1093 | case Iop_Or16: |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1094 | e2 = IRExpr_Const(IRConst_U16(toUShort( |
sewardj | e6b3993 | 2004-11-06 17:01:15 +0000 | [diff] [blame] | 1095 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U16 |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1096 | | e->Iex.Binop.arg2->Iex.Const.con->Ico.U16)))); |
sewardj | e6b3993 | 2004-11-06 17:01:15 +0000 | [diff] [blame] | 1097 | break; |
| 1098 | case Iop_Or32: |
| 1099 | e2 = IRExpr_Const(IRConst_U32( |
| 1100 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U32 |
| 1101 | | e->Iex.Binop.arg2->Iex.Const.con->Ico.U32))); |
| 1102 | break; |
sewardj | f057afb | 2005-02-27 13:35:41 +0000 | [diff] [blame] | 1103 | case Iop_Or64: |
| 1104 | e2 = IRExpr_Const(IRConst_U64( |
| 1105 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U64 |
| 1106 | | e->Iex.Binop.arg2->Iex.Const.con->Ico.U64))); |
| 1107 | break; |
sewardj | e6b3993 | 2004-11-06 17:01:15 +0000 | [diff] [blame] | 1108 | |
sewardj | 855dc72 | 2005-02-17 09:26:05 +0000 | [diff] [blame] | 1109 | /* -- Xor -- */ |
sewardj | 883b00b | 2004-09-11 09:30:24 +0000 | [diff] [blame] | 1110 | case Iop_Xor8: |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1111 | e2 = IRExpr_Const(IRConst_U8(toUChar( |
sewardj | 883b00b | 2004-09-11 09:30:24 +0000 | [diff] [blame] | 1112 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U8 |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1113 | ^ e->Iex.Binop.arg2->Iex.Const.con->Ico.U8)))); |
sewardj | 883b00b | 2004-09-11 09:30:24 +0000 | [diff] [blame] | 1114 | break; |
sewardj | 82c9f2f | 2005-03-02 16:05:13 +0000 | [diff] [blame] | 1115 | case Iop_Xor16: |
sewardj | c7c098f | 2005-03-21 01:06:20 +0000 | [diff] [blame] | 1116 | e2 = IRExpr_Const(IRConst_U16(toUShort( |
sewardj | 82c9f2f | 2005-03-02 16:05:13 +0000 | [diff] [blame] | 1117 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U16 |
sewardj | c7c098f | 2005-03-21 01:06:20 +0000 | [diff] [blame] | 1118 | ^ e->Iex.Binop.arg2->Iex.Const.con->Ico.U16)))); |
sewardj | 82c9f2f | 2005-03-02 16:05:13 +0000 | [diff] [blame] | 1119 | break; |
sewardj | 855dc72 | 2005-02-17 09:26:05 +0000 | [diff] [blame] | 1120 | case Iop_Xor32: |
| 1121 | e2 = IRExpr_Const(IRConst_U32( |
| 1122 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U32 |
| 1123 | ^ e->Iex.Binop.arg2->Iex.Const.con->Ico.U32))); |
| 1124 | break; |
sewardj | f057afb | 2005-02-27 13:35:41 +0000 | [diff] [blame] | 1125 | case Iop_Xor64: |
| 1126 | e2 = IRExpr_Const(IRConst_U64( |
| 1127 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U64 |
| 1128 | ^ e->Iex.Binop.arg2->Iex.Const.con->Ico.U64))); |
| 1129 | break; |
sewardj | 855dc72 | 2005-02-17 09:26:05 +0000 | [diff] [blame] | 1130 | |
| 1131 | /* -- And -- */ |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 1132 | case Iop_And8: |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1133 | e2 = IRExpr_Const(IRConst_U8(toUChar( |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 1134 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U8 |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1135 | & e->Iex.Binop.arg2->Iex.Const.con->Ico.U8)))); |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 1136 | break; |
sewardj | 855dc72 | 2005-02-17 09:26:05 +0000 | [diff] [blame] | 1137 | case Iop_And32: |
| 1138 | e2 = IRExpr_Const(IRConst_U32( |
| 1139 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U32 |
| 1140 | & e->Iex.Binop.arg2->Iex.Const.con->Ico.U32))); |
| 1141 | break; |
| 1142 | case Iop_And64: |
| 1143 | e2 = IRExpr_Const(IRConst_U64( |
| 1144 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U64 |
| 1145 | & e->Iex.Binop.arg2->Iex.Const.con->Ico.U64))); |
| 1146 | break; |
| 1147 | |
| 1148 | /* -- Add -- */ |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 1149 | case Iop_Add8: |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1150 | e2 = IRExpr_Const(IRConst_U8(toUChar( |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 1151 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U8 |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1152 | + e->Iex.Binop.arg2->Iex.Const.con->Ico.U8)))); |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 1153 | break; |
sewardj | 855dc72 | 2005-02-17 09:26:05 +0000 | [diff] [blame] | 1154 | case Iop_Add32: |
| 1155 | e2 = IRExpr_Const(IRConst_U32( |
| 1156 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U32 |
| 1157 | + e->Iex.Binop.arg2->Iex.Const.con->Ico.U32))); |
| 1158 | break; |
| 1159 | case Iop_Add64: |
| 1160 | e2 = IRExpr_Const(IRConst_U64( |
| 1161 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U64 |
| 1162 | + e->Iex.Binop.arg2->Iex.Const.con->Ico.U64))); |
| 1163 | break; |
| 1164 | |
| 1165 | /* -- Sub -- */ |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 1166 | case Iop_Sub8: |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1167 | e2 = IRExpr_Const(IRConst_U8(toUChar( |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 1168 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U8 |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1169 | - e->Iex.Binop.arg2->Iex.Const.con->Ico.U8)))); |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 1170 | break; |
sewardj | d721703 | 2004-08-19 10:49:10 +0000 | [diff] [blame] | 1171 | case Iop_Sub32: |
| 1172 | e2 = IRExpr_Const(IRConst_U32( |
| 1173 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U32 |
| 1174 | - e->Iex.Binop.arg2->Iex.Const.con->Ico.U32))); |
| 1175 | break; |
sewardj | 70a8ddf | 2005-02-13 02:24:26 +0000 | [diff] [blame] | 1176 | case Iop_Sub64: |
| 1177 | e2 = IRExpr_Const(IRConst_U64( |
| 1178 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U64 |
| 1179 | - e->Iex.Binop.arg2->Iex.Const.con->Ico.U64))); |
| 1180 | break; |
sewardj | c2bcb6f | 2005-02-07 00:17:12 +0000 | [diff] [blame] | 1181 | |
sewardj | 855dc72 | 2005-02-17 09:26:05 +0000 | [diff] [blame] | 1182 | /* -- Mul -- */ |
sewardj | b9c5cf6 | 2004-08-24 15:10:38 +0000 | [diff] [blame] | 1183 | case Iop_Mul32: |
| 1184 | e2 = IRExpr_Const(IRConst_U32( |
| 1185 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U32 |
| 1186 | * e->Iex.Binop.arg2->Iex.Const.con->Ico.U32))); |
| 1187 | break; |
sewardj | a34c071 | 2005-03-30 23:19:46 +0000 | [diff] [blame] | 1188 | case Iop_Mul64: |
| 1189 | e2 = IRExpr_Const(IRConst_U64( |
| 1190 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U64 |
| 1191 | * e->Iex.Binop.arg2->Iex.Const.con->Ico.U64))); |
| 1192 | break; |
| 1193 | |
sewardj | ea6bccb | 2005-03-01 10:19:23 +0000 | [diff] [blame] | 1194 | case Iop_MullS32: { |
| 1195 | /* very paranoid */ |
| 1196 | UInt u32a = e->Iex.Binop.arg1->Iex.Const.con->Ico.U32; |
| 1197 | UInt u32b = e->Iex.Binop.arg2->Iex.Const.con->Ico.U32; |
| 1198 | Int s32a = (Int)u32a; |
| 1199 | Int s32b = (Int)u32b; |
| 1200 | Long s64a = (Long)s32a; |
| 1201 | Long s64b = (Long)s32b; |
| 1202 | Long sres = s64a * s64b; |
| 1203 | ULong ures = (ULong)sres; |
| 1204 | e2 = IRExpr_Const(IRConst_U64(ures)); |
| 1205 | break; |
| 1206 | } |
sewardj | b095fba | 2005-02-13 14:13:04 +0000 | [diff] [blame] | 1207 | |
sewardj | 855dc72 | 2005-02-17 09:26:05 +0000 | [diff] [blame] | 1208 | /* -- Shl -- */ |
sewardj | d721703 | 2004-08-19 10:49:10 +0000 | [diff] [blame] | 1209 | case Iop_Shl32: |
sewardj | 6134847 | 2004-08-20 01:01:04 +0000 | [diff] [blame] | 1210 | vassert(e->Iex.Binop.arg2->Iex.Const.con->tag == Ico_U8); |
| 1211 | shift = (Int)(e->Iex.Binop.arg2->Iex.Const.con->Ico.U8); |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 1212 | if (shift >= 0 && shift <= 31) |
sewardj | 278c44c | 2004-08-20 00:28:13 +0000 | [diff] [blame] | 1213 | e2 = IRExpr_Const(IRConst_U32( |
| 1214 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U32 |
| 1215 | << shift))); |
sewardj | d721703 | 2004-08-19 10:49:10 +0000 | [diff] [blame] | 1216 | break; |
sewardj | b095fba | 2005-02-13 14:13:04 +0000 | [diff] [blame] | 1217 | case Iop_Shl64: |
| 1218 | vassert(e->Iex.Binop.arg2->Iex.Const.con->tag == Ico_U8); |
| 1219 | shift = (Int)(e->Iex.Binop.arg2->Iex.Const.con->Ico.U8); |
| 1220 | if (shift >= 0 && shift <= 63) |
| 1221 | e2 = IRExpr_Const(IRConst_U64( |
| 1222 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U64 |
| 1223 | << shift))); |
| 1224 | break; |
| 1225 | |
sewardj | 855dc72 | 2005-02-17 09:26:05 +0000 | [diff] [blame] | 1226 | /* -- Sar -- */ |
sewardj | 278c44c | 2004-08-20 00:28:13 +0000 | [diff] [blame] | 1227 | case Iop_Sar32: { |
| 1228 | /* paranoid ... */ |
| 1229 | /*signed*/ Int s32; |
sewardj | 6134847 | 2004-08-20 01:01:04 +0000 | [diff] [blame] | 1230 | vassert(e->Iex.Binop.arg2->Iex.Const.con->tag == Ico_U8); |
sewardj | 278c44c | 2004-08-20 00:28:13 +0000 | [diff] [blame] | 1231 | s32 = (Int)(e->Iex.Binop.arg1->Iex.Const.con->Ico.U32); |
sewardj | 6134847 | 2004-08-20 01:01:04 +0000 | [diff] [blame] | 1232 | shift = (Int)(e->Iex.Binop.arg2->Iex.Const.con->Ico.U8); |
sewardj | 278c44c | 2004-08-20 00:28:13 +0000 | [diff] [blame] | 1233 | if (shift >= 0 && shift <= 31) { |
| 1234 | s32 >>=/*signed*/ shift; |
| 1235 | e2 = IRExpr_Const(IRConst_U32((UInt)s32)); |
| 1236 | } |
| 1237 | break; |
| 1238 | } |
sewardj | 855dc72 | 2005-02-17 09:26:05 +0000 | [diff] [blame] | 1239 | case Iop_Sar64: { |
| 1240 | /* paranoid ... */ |
| 1241 | /*signed*/ Long s64; |
| 1242 | vassert(e->Iex.Binop.arg2->Iex.Const.con->tag == Ico_U8); |
| 1243 | s64 = (Long)(e->Iex.Binop.arg1->Iex.Const.con->Ico.U64); |
| 1244 | shift = (Int)(e->Iex.Binop.arg2->Iex.Const.con->Ico.U8); |
| 1245 | if (shift >= 0 && shift <= 63) { |
| 1246 | s64 >>=/*signed*/ shift; |
| 1247 | e2 = IRExpr_Const(IRConst_U64((ULong)s64)); |
| 1248 | } |
| 1249 | break; |
| 1250 | } |
| 1251 | |
| 1252 | /* -- Shr -- */ |
sewardj | 6134847 | 2004-08-20 01:01:04 +0000 | [diff] [blame] | 1253 | case Iop_Shr32: { |
| 1254 | /* paranoid ... */ |
sewardj | 4add714 | 2005-02-21 08:20:22 +0000 | [diff] [blame] | 1255 | /*unsigned*/ UInt u32; |
sewardj | 6134847 | 2004-08-20 01:01:04 +0000 | [diff] [blame] | 1256 | vassert(e->Iex.Binop.arg2->Iex.Const.con->tag == Ico_U8); |
sewardj | 4add714 | 2005-02-21 08:20:22 +0000 | [diff] [blame] | 1257 | u32 = (UInt)(e->Iex.Binop.arg1->Iex.Const.con->Ico.U32); |
sewardj | 6134847 | 2004-08-20 01:01:04 +0000 | [diff] [blame] | 1258 | shift = (Int)(e->Iex.Binop.arg2->Iex.Const.con->Ico.U8); |
| 1259 | if (shift >= 0 && shift <= 31) { |
sewardj | 4add714 | 2005-02-21 08:20:22 +0000 | [diff] [blame] | 1260 | u32 >>=/*unsigned*/ shift; |
| 1261 | e2 = IRExpr_Const(IRConst_U32(u32)); |
| 1262 | } |
| 1263 | break; |
| 1264 | } |
| 1265 | case Iop_Shr64: { |
| 1266 | /* paranoid ... */ |
| 1267 | /*unsigned*/ ULong u64; |
| 1268 | vassert(e->Iex.Binop.arg2->Iex.Const.con->tag == Ico_U8); |
| 1269 | u64 = (ULong)(e->Iex.Binop.arg1->Iex.Const.con->Ico.U64); |
| 1270 | shift = (Int)(e->Iex.Binop.arg2->Iex.Const.con->Ico.U8); |
| 1271 | if (shift >= 0 && shift <= 63) { |
| 1272 | u64 >>=/*unsigned*/ shift; |
| 1273 | e2 = IRExpr_Const(IRConst_U64(u64)); |
sewardj | 6134847 | 2004-08-20 01:01:04 +0000 | [diff] [blame] | 1274 | } |
| 1275 | break; |
| 1276 | } |
sewardj | 855dc72 | 2005-02-17 09:26:05 +0000 | [diff] [blame] | 1277 | |
| 1278 | /* -- CmpEQ -- */ |
sewardj | b8e7586 | 2004-08-19 17:58:45 +0000 | [diff] [blame] | 1279 | case Iop_CmpEQ32: |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1280 | e2 = IRExpr_Const(IRConst_U1(toBool( |
sewardj | b8e7586 | 2004-08-19 17:58:45 +0000 | [diff] [blame] | 1281 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U32 |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1282 | == e->Iex.Binop.arg2->Iex.Const.con->Ico.U32)))); |
sewardj | b8e7586 | 2004-08-19 17:58:45 +0000 | [diff] [blame] | 1283 | break; |
sewardj | 855dc72 | 2005-02-17 09:26:05 +0000 | [diff] [blame] | 1284 | case Iop_CmpEQ64: |
| 1285 | e2 = IRExpr_Const(IRConst_U1(toBool( |
| 1286 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U64 |
| 1287 | == e->Iex.Binop.arg2->Iex.Const.con->Ico.U64)))); |
| 1288 | break; |
| 1289 | |
| 1290 | /* -- CmpNE -- */ |
| 1291 | case Iop_CmpNE8: |
| 1292 | e2 = IRExpr_Const(IRConst_U1(toBool( |
| 1293 | ((0xFF & e->Iex.Binop.arg1->Iex.Const.con->Ico.U8) |
| 1294 | != (0xFF & e->Iex.Binop.arg2->Iex.Const.con->Ico.U8))))); |
| 1295 | break; |
sewardj | ae27ab6 | 2004-10-15 21:21:46 +0000 | [diff] [blame] | 1296 | case Iop_CmpNE32: |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1297 | e2 = IRExpr_Const(IRConst_U1(toBool( |
sewardj | ae27ab6 | 2004-10-15 21:21:46 +0000 | [diff] [blame] | 1298 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U32 |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1299 | != e->Iex.Binop.arg2->Iex.Const.con->Ico.U32)))); |
sewardj | ae27ab6 | 2004-10-15 21:21:46 +0000 | [diff] [blame] | 1300 | break; |
sewardj | e6b3993 | 2004-11-06 17:01:15 +0000 | [diff] [blame] | 1301 | case Iop_CmpNE64: |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1302 | e2 = IRExpr_Const(IRConst_U1(toBool( |
sewardj | e6b3993 | 2004-11-06 17:01:15 +0000 | [diff] [blame] | 1303 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U64 |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1304 | != e->Iex.Binop.arg2->Iex.Const.con->Ico.U64)))); |
sewardj | e6b3993 | 2004-11-06 17:01:15 +0000 | [diff] [blame] | 1305 | break; |
| 1306 | |
sewardj | 855dc72 | 2005-02-17 09:26:05 +0000 | [diff] [blame] | 1307 | /* -- CmpLEU -- */ |
sewardj | 7447b5b | 2004-10-16 11:30:42 +0000 | [diff] [blame] | 1308 | case Iop_CmpLE32U: |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1309 | e2 = IRExpr_Const(IRConst_U1(toBool( |
sewardj | 7447b5b | 2004-10-16 11:30:42 +0000 | [diff] [blame] | 1310 | ((UInt)(e->Iex.Binop.arg1->Iex.Const.con->Ico.U32) |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1311 | <= (UInt)(e->Iex.Binop.arg2->Iex.Const.con->Ico.U32))))); |
sewardj | 7447b5b | 2004-10-16 11:30:42 +0000 | [diff] [blame] | 1312 | break; |
sewardj | 855dc72 | 2005-02-17 09:26:05 +0000 | [diff] [blame] | 1313 | |
| 1314 | /* -- CmpLES -- */ |
sewardj | 088e4f7 | 2004-10-19 01:25:02 +0000 | [diff] [blame] | 1315 | case Iop_CmpLE32S: |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1316 | e2 = IRExpr_Const(IRConst_U1(toBool( |
sewardj | 088e4f7 | 2004-10-19 01:25:02 +0000 | [diff] [blame] | 1317 | ((Int)(e->Iex.Binop.arg1->Iex.Const.con->Ico.U32) |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1318 | <= (Int)(e->Iex.Binop.arg2->Iex.Const.con->Ico.U32))))); |
sewardj | 088e4f7 | 2004-10-19 01:25:02 +0000 | [diff] [blame] | 1319 | break; |
sewardj | e1d45da | 2004-11-12 00:13:21 +0000 | [diff] [blame] | 1320 | |
sewardj | 855dc72 | 2005-02-17 09:26:05 +0000 | [diff] [blame] | 1321 | /* -- CmpLTS -- */ |
sewardj | 9bdd265 | 2004-10-19 12:56:33 +0000 | [diff] [blame] | 1322 | case Iop_CmpLT32S: |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1323 | e2 = IRExpr_Const(IRConst_U1(toBool( |
sewardj | 9bdd265 | 2004-10-19 12:56:33 +0000 | [diff] [blame] | 1324 | ((Int)(e->Iex.Binop.arg1->Iex.Const.con->Ico.U32) |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1325 | < (Int)(e->Iex.Binop.arg2->Iex.Const.con->Ico.U32))))); |
sewardj | 9bdd265 | 2004-10-19 12:56:33 +0000 | [diff] [blame] | 1326 | break; |
sewardj | 855dc72 | 2005-02-17 09:26:05 +0000 | [diff] [blame] | 1327 | |
| 1328 | /* -- CmpLTU -- */ |
sewardj | e1d45da | 2004-11-12 00:13:21 +0000 | [diff] [blame] | 1329 | case Iop_CmpLT32U: |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1330 | e2 = IRExpr_Const(IRConst_U1(toBool( |
sewardj | e1d45da | 2004-11-12 00:13:21 +0000 | [diff] [blame] | 1331 | ((UInt)(e->Iex.Binop.arg1->Iex.Const.con->Ico.U32) |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1332 | < (UInt)(e->Iex.Binop.arg2->Iex.Const.con->Ico.U32))))); |
sewardj | e1d45da | 2004-11-12 00:13:21 +0000 | [diff] [blame] | 1333 | break; |
sewardj | 7447b5b | 2004-10-16 11:30:42 +0000 | [diff] [blame] | 1334 | |
sewardj | b51f0f4 | 2005-07-18 11:38:02 +0000 | [diff] [blame] | 1335 | /* -- CmpORD -- */ |
| 1336 | case Iop_CmpORD32S: { |
| 1337 | /* very paranoid */ |
| 1338 | UInt u32a = e->Iex.Binop.arg1->Iex.Const.con->Ico.U32; |
| 1339 | UInt u32b = e->Iex.Binop.arg2->Iex.Const.con->Ico.U32; |
| 1340 | Int s32a = (Int)u32a; |
| 1341 | Int s32b = (Int)u32b; |
| 1342 | Int r = 0x2; /* EQ */ |
| 1343 | if (s32a < s32b) { |
| 1344 | r = 0x8; /* LT */ |
| 1345 | } |
| 1346 | else if (s32a > s32b) { |
| 1347 | r = 0x4; /* GT */ |
| 1348 | } |
| 1349 | e2 = IRExpr_Const(IRConst_U32(r)); |
| 1350 | break; |
| 1351 | } |
| 1352 | |
sewardj | 855dc72 | 2005-02-17 09:26:05 +0000 | [diff] [blame] | 1353 | /* -- nHLto2n -- */ |
sewardj | 088bcb4 | 2004-08-19 17:16:52 +0000 | [diff] [blame] | 1354 | case Iop_32HLto64: |
| 1355 | e2 = IRExpr_Const(IRConst_U64( |
| 1356 | (((ULong)(e->Iex.Binop.arg1->Iex.Const.con->Ico.U32)) << 32) |
| 1357 | | ((ULong)(e->Iex.Binop.arg2->Iex.Const.con->Ico.U32)) |
| 1358 | )); |
| 1359 | break; |
sewardj | 3bc8a59 | 2005-05-02 10:47:22 +0000 | [diff] [blame] | 1360 | case Iop_64HLto128: |
| 1361 | /* We can't fold this, because there is no way to |
| 1362 | express he result in IR, but at least pretend to |
| 1363 | handle it, so as to stop getting blasted with |
| 1364 | no-rule-for-this-primop messages. */ |
| 1365 | break; |
sewardj | 855dc72 | 2005-02-17 09:26:05 +0000 | [diff] [blame] | 1366 | |
sewardj | 607dd4f | 2004-09-08 18:20:19 +0000 | [diff] [blame] | 1367 | default: |
| 1368 | goto unhandled; |
sewardj | d721703 | 2004-08-19 10:49:10 +0000 | [diff] [blame] | 1369 | } |
sewardj | f672901 | 2004-08-25 12:45:13 +0000 | [diff] [blame] | 1370 | |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 1371 | } else { |
sewardj | f672901 | 2004-08-25 12:45:13 +0000 | [diff] [blame] | 1372 | |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 1373 | /* other cases (identities, etc) */ |
sewardj | 291a7e8 | 2005-04-27 11:42:44 +0000 | [diff] [blame] | 1374 | |
| 1375 | /* Shl64/Shr64(x,0) ==> x */ |
| 1376 | if ((e->Iex.Binop.op == Iop_Shl64 || e->Iex.Binop.op == Iop_Shr64) |
| 1377 | && e->Iex.Binop.arg2->tag == Iex_Const |
| 1378 | && e->Iex.Binop.arg2->Iex.Const.con->Ico.U8 == 0) { |
| 1379 | e2 = e->Iex.Binop.arg1; |
| 1380 | } else |
| 1381 | |
sewardj | 4afab82 | 2005-01-20 10:04:05 +0000 | [diff] [blame] | 1382 | /* Shl32/Shr32(x,0) ==> x */ |
| 1383 | if ((e->Iex.Binop.op == Iop_Shl32 || e->Iex.Binop.op == Iop_Shr32) |
sewardj | 5234540 | 2004-10-13 16:08:14 +0000 | [diff] [blame] | 1384 | && e->Iex.Binop.arg2->tag == Iex_Const |
sewardj | 4980c6b | 2004-10-13 16:16:27 +0000 | [diff] [blame] | 1385 | && e->Iex.Binop.arg2->Iex.Const.con->Ico.U8 == 0) { |
sewardj | 5234540 | 2004-10-13 16:08:14 +0000 | [diff] [blame] | 1386 | e2 = e->Iex.Binop.arg1; |
| 1387 | } else |
| 1388 | |
sewardj | 8ac9bc9 | 2005-04-21 01:35:48 +0000 | [diff] [blame] | 1389 | /* Or8(x,0) ==> x */ |
| 1390 | if ((e->Iex.Binop.op == Iop_Or8) |
| 1391 | && e->Iex.Binop.arg2->tag == Iex_Const |
| 1392 | && e->Iex.Binop.arg2->Iex.Const.con->Ico.U8 == 0) { |
| 1393 | e2 = e->Iex.Binop.arg1; |
| 1394 | } else |
| 1395 | |
sewardj | 0033ddc | 2005-04-26 23:34:34 +0000 | [diff] [blame] | 1396 | /* Or16(x,0) ==> x */ |
| 1397 | if ((e->Iex.Binop.op == Iop_Or16) |
| 1398 | && e->Iex.Binop.arg2->tag == Iex_Const |
| 1399 | && e->Iex.Binop.arg2->Iex.Const.con->Ico.U16 == 0) { |
| 1400 | e2 = e->Iex.Binop.arg1; |
| 1401 | } else |
| 1402 | |
sewardj | d999788 | 2004-11-04 19:42:59 +0000 | [diff] [blame] | 1403 | /* Or32/Add32(x,0) ==> x */ |
| 1404 | if ((e->Iex.Binop.op == Iop_Add32 || e->Iex.Binop.op == Iop_Or32) |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 1405 | && e->Iex.Binop.arg2->tag == Iex_Const |
| 1406 | && e->Iex.Binop.arg2->Iex.Const.con->Ico.U32 == 0) { |
| 1407 | e2 = e->Iex.Binop.arg1; |
sewardj | f672901 | 2004-08-25 12:45:13 +0000 | [diff] [blame] | 1408 | } else |
| 1409 | |
sewardj | 89d89e9 | 2006-05-14 18:46:55 +0000 | [diff] [blame] | 1410 | /* Add32(t,t) ==> t << 1. Memcheck doesn't understand that |
| 1411 | x+x produces a defined least significant bit, and it seems |
| 1412 | simplest just to get rid of the problem by rewriting it |
| 1413 | out, since the opportunity to do so exists. */ |
| 1414 | if (e->Iex.Binop.op == Iop_Add32 |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 1415 | && e->Iex.Binop.arg1->tag == Iex_RdTmp |
| 1416 | && e->Iex.Binop.arg2->tag == Iex_RdTmp |
| 1417 | && e->Iex.Binop.arg1->Iex.RdTmp.tmp |
| 1418 | == e->Iex.Binop.arg2->Iex.RdTmp.tmp) { |
sewardj | 89d89e9 | 2006-05-14 18:46:55 +0000 | [diff] [blame] | 1419 | e2 = IRExpr_Binop(Iop_Shl32, |
| 1420 | e->Iex.Binop.arg1, |
| 1421 | IRExpr_Const(IRConst_U8(1))); |
| 1422 | } else |
| 1423 | |
sewardj | 346d9a1 | 2006-05-21 01:02:31 +0000 | [diff] [blame] | 1424 | /* Add64(t,t) ==> t << 1; rationale as for Add32(t,t) above. */ |
| 1425 | if (e->Iex.Binop.op == Iop_Add64 |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 1426 | && e->Iex.Binop.arg1->tag == Iex_RdTmp |
| 1427 | && e->Iex.Binop.arg2->tag == Iex_RdTmp |
| 1428 | && e->Iex.Binop.arg1->Iex.RdTmp.tmp |
| 1429 | == e->Iex.Binop.arg2->Iex.RdTmp.tmp) { |
sewardj | 346d9a1 | 2006-05-21 01:02:31 +0000 | [diff] [blame] | 1430 | e2 = IRExpr_Binop(Iop_Shl64, |
| 1431 | e->Iex.Binop.arg1, |
| 1432 | IRExpr_Const(IRConst_U8(1))); |
| 1433 | } else |
| 1434 | |
sewardj | dcd6c88 | 2004-12-16 11:41:06 +0000 | [diff] [blame] | 1435 | /* Or64/Add64(x,0) ==> x */ |
| 1436 | if ((e->Iex.Binop.op == Iop_Add64 || e->Iex.Binop.op == Iop_Or64) |
| 1437 | && e->Iex.Binop.arg2->tag == Iex_Const |
| 1438 | && e->Iex.Binop.arg2->Iex.Const.con->Ico.U64 == 0) { |
| 1439 | e2 = e->Iex.Binop.arg1; |
| 1440 | } else |
| 1441 | |
sewardj | 832853b | 2004-11-06 12:10:04 +0000 | [diff] [blame] | 1442 | /* And32(x,0xFFFFFFFF) ==> x */ |
| 1443 | if (e->Iex.Binop.op == Iop_And32 |
| 1444 | && e->Iex.Binop.arg2->tag == Iex_Const |
| 1445 | && e->Iex.Binop.arg2->Iex.Const.con->Ico.U32 == 0xFFFFFFFF) { |
| 1446 | e2 = e->Iex.Binop.arg1; |
| 1447 | } else |
| 1448 | |
sewardj | 2325699 | 2005-07-01 10:51:24 +0000 | [diff] [blame] | 1449 | /* And32(x,0) ==> 0 */ |
| 1450 | if (e->Iex.Binop.op == Iop_And32 |
| 1451 | && e->Iex.Binop.arg2->tag == Iex_Const |
| 1452 | && e->Iex.Binop.arg2->Iex.Const.con->Ico.U32 == 0) { |
| 1453 | e2 = IRExpr_Const(IRConst_U32(0)); |
| 1454 | } else |
| 1455 | |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 1456 | /* And32(0,x) ==> 0 */ |
| 1457 | if (e->Iex.Binop.op == Iop_And32 |
| 1458 | && e->Iex.Binop.arg1->tag == Iex_Const |
| 1459 | && e->Iex.Binop.arg1->Iex.Const.con->Ico.U32 == 0) { |
| 1460 | e2 = IRExpr_Const(IRConst_U32(0)); |
| 1461 | } else |
| 1462 | |
sewardj | d999788 | 2004-11-04 19:42:59 +0000 | [diff] [blame] | 1463 | /* Or32(0,x) ==> x */ |
| 1464 | if (e->Iex.Binop.op == Iop_Or32 |
| 1465 | && e->Iex.Binop.arg1->tag == Iex_Const |
| 1466 | && e->Iex.Binop.arg1->Iex.Const.con->Ico.U32 == 0) { |
| 1467 | e2 = e->Iex.Binop.arg2; |
| 1468 | } else |
| 1469 | |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 1470 | /* Or64(0,x) ==> x */ |
| 1471 | if (e->Iex.Binop.op == Iop_Or64 |
| 1472 | && e->Iex.Binop.arg1->tag == Iex_Const |
| 1473 | && e->Iex.Binop.arg1->Iex.Const.con->Ico.U64 == 0) { |
| 1474 | e2 = e->Iex.Binop.arg2; |
| 1475 | } else |
| 1476 | |
sewardj | dcd6c88 | 2004-12-16 11:41:06 +0000 | [diff] [blame] | 1477 | /* Or8/16/32/64(t,t) ==> t, for some IRTemp t */ |
| 1478 | /* And8/16/32/64(t,t) ==> t, for some IRTemp t */ |
| 1479 | if ( (e->Iex.Binop.op == Iop_And64 |
| 1480 | || e->Iex.Binop.op == Iop_And32 |
sewardj | f672901 | 2004-08-25 12:45:13 +0000 | [diff] [blame] | 1481 | || e->Iex.Binop.op == Iop_And16 |
sewardj | dcd6c88 | 2004-12-16 11:41:06 +0000 | [diff] [blame] | 1482 | || e->Iex.Binop.op == Iop_And8 |
| 1483 | || e->Iex.Binop.op == Iop_Or64 |
| 1484 | || e->Iex.Binop.op == Iop_Or32 |
| 1485 | || e->Iex.Binop.op == Iop_Or16 |
| 1486 | || e->Iex.Binop.op == Iop_Or8) |
sewardj | f672901 | 2004-08-25 12:45:13 +0000 | [diff] [blame] | 1487 | && sameIRTemps(e->Iex.Binop.arg1, e->Iex.Binop.arg2)) { |
| 1488 | e2 = e->Iex.Binop.arg1; |
sewardj | aba4fff | 2004-10-08 21:37:15 +0000 | [diff] [blame] | 1489 | } |
sewardj | f672901 | 2004-08-25 12:45:13 +0000 | [diff] [blame] | 1490 | |
sewardj | 0474427 | 2007-01-16 19:19:55 +0000 | [diff] [blame] | 1491 | /* Xor8/16/32/64/V128(t,t) ==> 0, for some IRTemp t */ |
sewardj | 0033ddc | 2005-04-26 23:34:34 +0000 | [diff] [blame] | 1492 | if ( (e->Iex.Binop.op == Iop_Xor64 |
| 1493 | || e->Iex.Binop.op == Iop_Xor32 |
| 1494 | || e->Iex.Binop.op == Iop_Xor16 |
sewardj | 0474427 | 2007-01-16 19:19:55 +0000 | [diff] [blame] | 1495 | || e->Iex.Binop.op == Iop_Xor8 |
| 1496 | || e->Iex.Binop.op == Iop_XorV128) |
sewardj | 0033ddc | 2005-04-26 23:34:34 +0000 | [diff] [blame] | 1497 | && sameIRTemps(e->Iex.Binop.arg1, e->Iex.Binop.arg2)) { |
sewardj | 0033ddc | 2005-04-26 23:34:34 +0000 | [diff] [blame] | 1498 | e2 = mkZeroForXor(e->Iex.Binop.op); |
| 1499 | } |
| 1500 | |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 1501 | } |
| 1502 | } |
| 1503 | |
| 1504 | /* Mux0X */ |
| 1505 | if (e->tag == Iex_Mux0X |
| 1506 | && e->Iex.Mux0X.cond->tag == Iex_Const) { |
| 1507 | Bool zero; |
| 1508 | /* assured us by the IR type rules */ |
| 1509 | vassert(e->Iex.Mux0X.cond->Iex.Const.con->tag == Ico_U8); |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1510 | zero = toBool(0 == (0xFF & e->Iex.Mux0X.cond |
| 1511 | ->Iex.Const.con->Ico.U8)); |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 1512 | e2 = zero ? e->Iex.Mux0X.expr0 : e->Iex.Mux0X.exprX; |
| 1513 | } |
| 1514 | |
sewardj | 088bcb4 | 2004-08-19 17:16:52 +0000 | [diff] [blame] | 1515 | if (DEBUG_IROPT && e2 != e) { |
| 1516 | vex_printf("FOLD: "); |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 1517 | ppIRExpr(e); vex_printf(" -> "); |
| 1518 | ppIRExpr(e2); vex_printf("\n"); |
| 1519 | } |
| 1520 | |
| 1521 | return e2; |
| 1522 | |
| 1523 | unhandled: |
sewardj | 883b00b | 2004-09-11 09:30:24 +0000 | [diff] [blame] | 1524 | # if 0 |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 1525 | vex_printf("\n\n"); |
| 1526 | ppIRExpr(e); |
| 1527 | vpanic("fold_Expr: no rule for the above"); |
sewardj | 883b00b | 2004-09-11 09:30:24 +0000 | [diff] [blame] | 1528 | # else |
sewardj | 328b54b | 2005-06-13 16:30:18 +0000 | [diff] [blame] | 1529 | if (vex_control.iropt_verbosity > 0) { |
| 1530 | vex_printf("vex iropt: fold_Expr: no rule for: "); |
| 1531 | ppIRExpr(e); |
| 1532 | vex_printf("\n"); |
| 1533 | } |
sewardj | 883b00b | 2004-09-11 09:30:24 +0000 | [diff] [blame] | 1534 | return e2; |
| 1535 | # endif |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 1536 | } |
| 1537 | |
| 1538 | |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 1539 | /* Apply the subst to a simple 1-level expression -- guaranteed to be |
| 1540 | 1-level due to previous flattening pass. */ |
| 1541 | |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 1542 | static IRExpr* subst_Expr ( IRExpr** env, IRExpr* ex ) |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 1543 | { |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 1544 | switch (ex->tag) { |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 1545 | case Iex_RdTmp: |
| 1546 | if (env[(Int)ex->Iex.RdTmp.tmp] != NULL) { |
| 1547 | return env[(Int)ex->Iex.RdTmp.tmp]; |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 1548 | } else { |
| 1549 | /* not bound in env */ |
| 1550 | return ex; |
| 1551 | } |
| 1552 | |
| 1553 | case Iex_Const: |
| 1554 | case Iex_Get: |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 1555 | return ex; |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 1556 | |
| 1557 | case Iex_GetI: |
sewardj | 496a58d | 2005-03-20 18:44:44 +0000 | [diff] [blame] | 1558 | vassert(isIRAtom(ex->Iex.GetI.ix)); |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 1559 | return IRExpr_GetI( |
| 1560 | ex->Iex.GetI.descr, |
sewardj | eeac841 | 2004-11-02 00:26:55 +0000 | [diff] [blame] | 1561 | subst_Expr(env, ex->Iex.GetI.ix), |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 1562 | ex->Iex.GetI.bias |
| 1563 | ); |
| 1564 | |
sewardj | 40c8026 | 2006-02-08 19:30:46 +0000 | [diff] [blame] | 1565 | case Iex_Qop: |
| 1566 | vassert(isIRAtom(ex->Iex.Qop.arg1)); |
| 1567 | vassert(isIRAtom(ex->Iex.Qop.arg2)); |
| 1568 | vassert(isIRAtom(ex->Iex.Qop.arg3)); |
| 1569 | vassert(isIRAtom(ex->Iex.Qop.arg4)); |
| 1570 | return IRExpr_Qop( |
| 1571 | ex->Iex.Qop.op, |
| 1572 | subst_Expr(env, ex->Iex.Qop.arg1), |
| 1573 | subst_Expr(env, ex->Iex.Qop.arg2), |
| 1574 | subst_Expr(env, ex->Iex.Qop.arg3), |
| 1575 | subst_Expr(env, ex->Iex.Qop.arg4) |
| 1576 | ); |
| 1577 | |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 1578 | case Iex_Triop: |
| 1579 | vassert(isIRAtom(ex->Iex.Triop.arg1)); |
| 1580 | vassert(isIRAtom(ex->Iex.Triop.arg2)); |
| 1581 | vassert(isIRAtom(ex->Iex.Triop.arg3)); |
| 1582 | return IRExpr_Triop( |
| 1583 | ex->Iex.Triop.op, |
| 1584 | subst_Expr(env, ex->Iex.Triop.arg1), |
| 1585 | subst_Expr(env, ex->Iex.Triop.arg2), |
| 1586 | subst_Expr(env, ex->Iex.Triop.arg3) |
| 1587 | ); |
| 1588 | |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 1589 | case Iex_Binop: |
sewardj | 496a58d | 2005-03-20 18:44:44 +0000 | [diff] [blame] | 1590 | vassert(isIRAtom(ex->Iex.Binop.arg1)); |
| 1591 | vassert(isIRAtom(ex->Iex.Binop.arg2)); |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 1592 | return IRExpr_Binop( |
| 1593 | ex->Iex.Binop.op, |
| 1594 | subst_Expr(env, ex->Iex.Binop.arg1), |
| 1595 | subst_Expr(env, ex->Iex.Binop.arg2) |
| 1596 | ); |
| 1597 | |
| 1598 | case Iex_Unop: |
sewardj | 496a58d | 2005-03-20 18:44:44 +0000 | [diff] [blame] | 1599 | vassert(isIRAtom(ex->Iex.Unop.arg)); |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 1600 | return IRExpr_Unop( |
| 1601 | ex->Iex.Unop.op, |
| 1602 | subst_Expr(env, ex->Iex.Unop.arg) |
| 1603 | ); |
| 1604 | |
sewardj | af1ceca | 2005-06-30 23:31:27 +0000 | [diff] [blame] | 1605 | case Iex_Load: |
| 1606 | vassert(isIRAtom(ex->Iex.Load.addr)); |
| 1607 | return IRExpr_Load( |
| 1608 | ex->Iex.Load.end, |
| 1609 | ex->Iex.Load.ty, |
| 1610 | subst_Expr(env, ex->Iex.Load.addr) |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 1611 | ); |
| 1612 | |
| 1613 | case Iex_CCall: { |
| 1614 | Int i; |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 1615 | IRExpr** args2 = shallowCopyIRExprVec(ex->Iex.CCall.args); |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 1616 | for (i = 0; args2[i]; i++) { |
sewardj | 496a58d | 2005-03-20 18:44:44 +0000 | [diff] [blame] | 1617 | vassert(isIRAtom(args2[i])); |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 1618 | args2[i] = subst_Expr(env, args2[i]); |
| 1619 | } |
| 1620 | return IRExpr_CCall( |
sewardj | 8ea867b | 2004-10-30 19:03:02 +0000 | [diff] [blame] | 1621 | ex->Iex.CCall.cee, |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 1622 | ex->Iex.CCall.retty, |
| 1623 | args2 |
| 1624 | ); |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 1625 | } |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 1626 | |
| 1627 | case Iex_Mux0X: |
sewardj | 496a58d | 2005-03-20 18:44:44 +0000 | [diff] [blame] | 1628 | vassert(isIRAtom(ex->Iex.Mux0X.cond)); |
| 1629 | vassert(isIRAtom(ex->Iex.Mux0X.expr0)); |
| 1630 | vassert(isIRAtom(ex->Iex.Mux0X.exprX)); |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 1631 | return IRExpr_Mux0X( |
| 1632 | subst_Expr(env, ex->Iex.Mux0X.cond), |
| 1633 | subst_Expr(env, ex->Iex.Mux0X.expr0), |
| 1634 | subst_Expr(env, ex->Iex.Mux0X.exprX) |
| 1635 | ); |
| 1636 | |
| 1637 | default: |
| 1638 | vex_printf("\n\n"); ppIRExpr(ex); |
| 1639 | vpanic("subst_Expr"); |
| 1640 | |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 1641 | } |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 1642 | } |
| 1643 | |
| 1644 | |
| 1645 | /* Apply the subst to stmt, then fold the result as much as possible. |
sewardj | d2445f6 | 2005-03-21 00:15:53 +0000 | [diff] [blame] | 1646 | Much simplified due to stmt being previously flattened. As a |
| 1647 | result of this, the stmt may wind up being turned into a no-op. |
| 1648 | */ |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 1649 | static IRStmt* subst_and_fold_Stmt ( IRExpr** env, IRStmt* st ) |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 1650 | { |
| 1651 | # if 0 |
| 1652 | vex_printf("\nsubst and fold stmt\n"); |
| 1653 | ppIRStmt(st); |
| 1654 | vex_printf("\n"); |
| 1655 | # endif |
| 1656 | |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 1657 | switch (st->tag) { |
sewardj | 5a9ffab | 2005-05-12 17:55:01 +0000 | [diff] [blame] | 1658 | case Ist_AbiHint: |
| 1659 | vassert(isIRAtom(st->Ist.AbiHint.base)); |
| 1660 | return IRStmt_AbiHint( |
| 1661 | fold_Expr(subst_Expr(env, st->Ist.AbiHint.base)), |
| 1662 | st->Ist.AbiHint.len |
| 1663 | ); |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 1664 | case Ist_Put: |
sewardj | 496a58d | 2005-03-20 18:44:44 +0000 | [diff] [blame] | 1665 | vassert(isIRAtom(st->Ist.Put.data)); |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 1666 | return IRStmt_Put( |
| 1667 | st->Ist.Put.offset, |
| 1668 | fold_Expr(subst_Expr(env, st->Ist.Put.data)) |
| 1669 | ); |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 1670 | |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 1671 | case Ist_PutI: |
sewardj | 496a58d | 2005-03-20 18:44:44 +0000 | [diff] [blame] | 1672 | vassert(isIRAtom(st->Ist.PutI.ix)); |
| 1673 | vassert(isIRAtom(st->Ist.PutI.data)); |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 1674 | return IRStmt_PutI( |
| 1675 | st->Ist.PutI.descr, |
sewardj | eeac841 | 2004-11-02 00:26:55 +0000 | [diff] [blame] | 1676 | fold_Expr(subst_Expr(env, st->Ist.PutI.ix)), |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 1677 | st->Ist.PutI.bias, |
| 1678 | fold_Expr(subst_Expr(env, st->Ist.PutI.data)) |
| 1679 | ); |
sewardj | d721703 | 2004-08-19 10:49:10 +0000 | [diff] [blame] | 1680 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 1681 | case Ist_WrTmp: |
| 1682 | /* This is the one place where an expr (st->Ist.WrTmp.data) is |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 1683 | allowed to be more than just a constant or a tmp. */ |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 1684 | return IRStmt_WrTmp( |
| 1685 | st->Ist.WrTmp.tmp, |
| 1686 | fold_Expr(subst_Expr(env, st->Ist.WrTmp.data)) |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 1687 | ); |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 1688 | |
sewardj | af1ceca | 2005-06-30 23:31:27 +0000 | [diff] [blame] | 1689 | case Ist_Store: |
| 1690 | vassert(isIRAtom(st->Ist.Store.addr)); |
| 1691 | vassert(isIRAtom(st->Ist.Store.data)); |
| 1692 | return IRStmt_Store( |
| 1693 | st->Ist.Store.end, |
| 1694 | fold_Expr(subst_Expr(env, st->Ist.Store.addr)), |
| 1695 | fold_Expr(subst_Expr(env, st->Ist.Store.data)) |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 1696 | ); |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 1697 | |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 1698 | case Ist_Dirty: { |
| 1699 | Int i; |
| 1700 | IRDirty *d, *d2; |
| 1701 | d = st->Ist.Dirty.details; |
| 1702 | d2 = emptyIRDirty(); |
| 1703 | *d2 = *d; |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 1704 | d2->args = shallowCopyIRExprVec(d2->args); |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 1705 | if (d2->mFx != Ifx_None) { |
sewardj | 496a58d | 2005-03-20 18:44:44 +0000 | [diff] [blame] | 1706 | vassert(isIRAtom(d2->mAddr)); |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 1707 | d2->mAddr = fold_Expr(subst_Expr(env, d2->mAddr)); |
sewardj | b8e7586 | 2004-08-19 17:58:45 +0000 | [diff] [blame] | 1708 | } |
sewardj | 496a58d | 2005-03-20 18:44:44 +0000 | [diff] [blame] | 1709 | vassert(isIRAtom(d2->guard)); |
sewardj | b8385d8 | 2004-11-02 01:34:15 +0000 | [diff] [blame] | 1710 | d2->guard = fold_Expr(subst_Expr(env, d2->guard)); |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 1711 | for (i = 0; d2->args[i]; i++) { |
sewardj | 496a58d | 2005-03-20 18:44:44 +0000 | [diff] [blame] | 1712 | vassert(isIRAtom(d2->args[i])); |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 1713 | d2->args[i] = fold_Expr(subst_Expr(env, d2->args[i])); |
| 1714 | } |
| 1715 | return IRStmt_Dirty(d2); |
sewardj | b8e7586 | 2004-08-19 17:58:45 +0000 | [diff] [blame] | 1716 | } |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 1717 | |
sewardj | f168931 | 2005-03-16 18:19:10 +0000 | [diff] [blame] | 1718 | case Ist_IMark: |
| 1719 | return IRStmt_IMark(st->Ist.IMark.addr, st->Ist.IMark.len); |
| 1720 | |
sewardj | d2445f6 | 2005-03-21 00:15:53 +0000 | [diff] [blame] | 1721 | case Ist_NoOp: |
| 1722 | return IRStmt_NoOp(); |
| 1723 | |
sewardj | 3e83893 | 2005-01-07 12:09:15 +0000 | [diff] [blame] | 1724 | case Ist_MFence: |
| 1725 | return IRStmt_MFence(); |
| 1726 | |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 1727 | case Ist_Exit: { |
| 1728 | IRExpr* fcond; |
sewardj | 496a58d | 2005-03-20 18:44:44 +0000 | [diff] [blame] | 1729 | vassert(isIRAtom(st->Ist.Exit.guard)); |
sewardj | 0276d4b | 2004-11-15 15:30:21 +0000 | [diff] [blame] | 1730 | fcond = fold_Expr(subst_Expr(env, st->Ist.Exit.guard)); |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 1731 | if (fcond->tag == Iex_Const) { |
| 1732 | /* Interesting. The condition on this exit has folded down to |
| 1733 | a constant. */ |
sewardj | ba99931 | 2004-11-15 15:21:17 +0000 | [diff] [blame] | 1734 | vassert(fcond->Iex.Const.con->tag == Ico_U1); |
sewardj | 9843029 | 2004-12-29 17:34:50 +0000 | [diff] [blame] | 1735 | vassert(fcond->Iex.Const.con->Ico.U1 == False |
| 1736 | || fcond->Iex.Const.con->Ico.U1 == True); |
sewardj | ba99931 | 2004-11-15 15:21:17 +0000 | [diff] [blame] | 1737 | if (fcond->Iex.Const.con->Ico.U1 == False) { |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 1738 | /* exit is never going to happen, so dump the statement. */ |
sewardj | d2445f6 | 2005-03-21 00:15:53 +0000 | [diff] [blame] | 1739 | return IRStmt_NoOp(); |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 1740 | } else { |
sewardj | ba99931 | 2004-11-15 15:21:17 +0000 | [diff] [blame] | 1741 | vassert(fcond->Iex.Const.con->Ico.U1 == True); |
sewardj | e810c19 | 2005-09-09 08:33:03 +0000 | [diff] [blame] | 1742 | /* Hmmm. The exit has become unconditional. Leave it |
| 1743 | as it is for now, since we'd have to truncate the BB |
| 1744 | at this point, which is tricky. Such truncation is |
| 1745 | done later by the dead-code elimination pass. */ |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 1746 | /* fall out into the reconstruct-the-exit code. */ |
sewardj | 0861374 | 2004-10-25 13:01:45 +0000 | [diff] [blame] | 1747 | if (vex_control.iropt_verbosity > 0) |
| 1748 | /* really a misuse of vex_control.iropt_verbosity */ |
sewardj | 8c2c10b | 2004-10-16 20:51:52 +0000 | [diff] [blame] | 1749 | vex_printf("vex iropt: IRStmt_Exit became unconditional\n"); |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 1750 | } |
| 1751 | } |
sewardj | 893aada | 2004-11-29 19:57:54 +0000 | [diff] [blame] | 1752 | return IRStmt_Exit(fcond, st->Ist.Exit.jk, st->Ist.Exit.dst); |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 1753 | } |
| 1754 | |
| 1755 | default: |
| 1756 | vex_printf("\n"); ppIRStmt(st); |
| 1757 | vpanic("subst_and_fold_Stmt"); |
| 1758 | } |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 1759 | } |
| 1760 | |
| 1761 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 1762 | IRSB* cprop_BB ( IRSB* in ) |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 1763 | { |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 1764 | Int i; |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 1765 | IRSB* out; |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 1766 | IRStmt* st2; |
| 1767 | Int n_tmps = in->tyenv->types_used; |
| 1768 | IRExpr** env = LibVEX_Alloc(n_tmps * sizeof(IRExpr*)); |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 1769 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 1770 | out = emptyIRSB(); |
| 1771 | out->tyenv = deepCopyIRTypeEnv( in->tyenv ); |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 1772 | |
| 1773 | /* Set up the env with which travels forward. This holds a |
| 1774 | substitution, mapping IRTemps to atoms, that is, IRExprs which |
| 1775 | are either IRTemps or IRConsts. Thus, copy and constant |
| 1776 | propagation is done. The environment is to be applied as we |
| 1777 | move along. Keys are IRTemps. Values are IRExpr*s. |
| 1778 | */ |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 1779 | for (i = 0; i < n_tmps; i++) |
| 1780 | env[i] = NULL; |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 1781 | |
| 1782 | /* For each original SSA-form stmt ... */ |
| 1783 | for (i = 0; i < in->stmts_used; i++) { |
| 1784 | |
| 1785 | /* First apply the substitution to the current stmt. This |
| 1786 | propagates in any constants and tmp-tmp assignments |
| 1787 | accumulated prior to this point. As part of the subst_Stmt |
| 1788 | call, also then fold any constant expressions resulting. */ |
| 1789 | |
sewardj | f0e4316 | 2004-08-20 00:11:12 +0000 | [diff] [blame] | 1790 | st2 = in->stmts[i]; |
| 1791 | |
| 1792 | /* perhaps st2 is already a no-op? */ |
sewardj | 8bee6d1 | 2005-03-22 02:24:05 +0000 | [diff] [blame] | 1793 | if (st2->tag == Ist_NoOp) continue; |
sewardj | f0e4316 | 2004-08-20 00:11:12 +0000 | [diff] [blame] | 1794 | |
| 1795 | st2 = subst_and_fold_Stmt( env, st2 ); |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 1796 | |
sewardj | b8e7586 | 2004-08-19 17:58:45 +0000 | [diff] [blame] | 1797 | /* If the statement has been folded into a no-op, forget it. */ |
sewardj | 8bee6d1 | 2005-03-22 02:24:05 +0000 | [diff] [blame] | 1798 | if (st2->tag == Ist_NoOp) continue; |
sewardj | b8e7586 | 2004-08-19 17:58:45 +0000 | [diff] [blame] | 1799 | |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 1800 | /* Now consider what the stmt looks like. If it's of the form |
| 1801 | 't = const' or 't1 = t2', add it to the running environment |
| 1802 | and not to the output BB. Otherwise, add it to the output |
sewardj | c0b4228 | 2004-10-12 13:44:12 +0000 | [diff] [blame] | 1803 | BB. Note, we choose not to propagate const when const is an |
| 1804 | F64i, so that F64i literals can be CSE'd later. This helps |
| 1805 | x86 floating point code generation. */ |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 1806 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 1807 | if (st2->tag == Ist_WrTmp |
| 1808 | && st2->Ist.WrTmp.data->tag == Iex_Const |
| 1809 | && st2->Ist.WrTmp.data->Iex.Const.con->tag != Ico_F64i) { |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 1810 | /* 't = const' -- add to env. |
| 1811 | The pair (IRTemp, IRExpr*) is added. */ |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 1812 | env[(Int)(st2->Ist.WrTmp.tmp)] = st2->Ist.WrTmp.data; |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 1813 | } |
| 1814 | else |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 1815 | if (st2->tag == Ist_WrTmp && st2->Ist.WrTmp.data->tag == Iex_RdTmp) { |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 1816 | /* 't1 = t2' -- add to env. |
| 1817 | The pair (IRTemp, IRExpr*) is added. */ |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 1818 | env[(Int)(st2->Ist.WrTmp.tmp)] = st2->Ist.WrTmp.data; |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 1819 | } |
| 1820 | else { |
| 1821 | /* Not interesting, copy st2 into the output block. */ |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 1822 | addStmtToIRSB( out, st2 ); |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 1823 | } |
| 1824 | } |
| 1825 | |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 1826 | out->next = subst_Expr( env, in->next ); |
| 1827 | out->jumpkind = in->jumpkind; |
| 1828 | return out; |
| 1829 | } |
| 1830 | |
| 1831 | |
sewardj | edf4d69 | 2004-08-17 13:52:58 +0000 | [diff] [blame] | 1832 | /*---------------------------------------------------------------*/ |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 1833 | /*--- Dead code (t = E) removal ---*/ |
| 1834 | /*---------------------------------------------------------------*/ |
| 1835 | |
sewardj | e810c19 | 2005-09-09 08:33:03 +0000 | [diff] [blame] | 1836 | /* As a side effect, also removes all code following an unconditional |
| 1837 | side exit. */ |
| 1838 | |
sewardj | ea602bc | 2004-10-14 21:40:12 +0000 | [diff] [blame] | 1839 | /* The type of the HashHW map is: a map from IRTemp to nothing |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 1840 | -- really just operating a set or IRTemps. |
| 1841 | */ |
| 1842 | |
sewardj | d503a32 | 2004-10-13 22:41:16 +0000 | [diff] [blame] | 1843 | inline |
| 1844 | static void addUses_Temp ( Bool* set, IRTemp tmp ) |
sewardj | 17442fe | 2004-09-20 14:54:28 +0000 | [diff] [blame] | 1845 | { |
sewardj | d503a32 | 2004-10-13 22:41:16 +0000 | [diff] [blame] | 1846 | set[(Int)tmp] = True; |
sewardj | 17442fe | 2004-09-20 14:54:28 +0000 | [diff] [blame] | 1847 | } |
| 1848 | |
sewardj | d503a32 | 2004-10-13 22:41:16 +0000 | [diff] [blame] | 1849 | static void addUses_Expr ( Bool* set, IRExpr* e ) |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 1850 | { |
| 1851 | Int i; |
| 1852 | switch (e->tag) { |
sewardj | d721703 | 2004-08-19 10:49:10 +0000 | [diff] [blame] | 1853 | case Iex_GetI: |
sewardj | eeac841 | 2004-11-02 00:26:55 +0000 | [diff] [blame] | 1854 | addUses_Expr(set, e->Iex.GetI.ix); |
sewardj | d721703 | 2004-08-19 10:49:10 +0000 | [diff] [blame] | 1855 | return; |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 1856 | case Iex_Mux0X: |
| 1857 | addUses_Expr(set, e->Iex.Mux0X.cond); |
| 1858 | addUses_Expr(set, e->Iex.Mux0X.expr0); |
| 1859 | addUses_Expr(set, e->Iex.Mux0X.exprX); |
| 1860 | return; |
| 1861 | case Iex_CCall: |
| 1862 | for (i = 0; e->Iex.CCall.args[i]; i++) |
| 1863 | addUses_Expr(set, e->Iex.CCall.args[i]); |
| 1864 | return; |
sewardj | af1ceca | 2005-06-30 23:31:27 +0000 | [diff] [blame] | 1865 | case Iex_Load: |
| 1866 | addUses_Expr(set, e->Iex.Load.addr); |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 1867 | return; |
sewardj | 40c8026 | 2006-02-08 19:30:46 +0000 | [diff] [blame] | 1868 | case Iex_Qop: |
| 1869 | addUses_Expr(set, e->Iex.Qop.arg1); |
| 1870 | addUses_Expr(set, e->Iex.Qop.arg2); |
| 1871 | addUses_Expr(set, e->Iex.Qop.arg3); |
| 1872 | addUses_Expr(set, e->Iex.Qop.arg4); |
| 1873 | return; |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 1874 | case Iex_Triop: |
| 1875 | addUses_Expr(set, e->Iex.Triop.arg1); |
| 1876 | addUses_Expr(set, e->Iex.Triop.arg2); |
| 1877 | addUses_Expr(set, e->Iex.Triop.arg3); |
| 1878 | return; |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 1879 | case Iex_Binop: |
| 1880 | addUses_Expr(set, e->Iex.Binop.arg1); |
| 1881 | addUses_Expr(set, e->Iex.Binop.arg2); |
| 1882 | return; |
| 1883 | case Iex_Unop: |
| 1884 | addUses_Expr(set, e->Iex.Unop.arg); |
| 1885 | return; |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 1886 | case Iex_RdTmp: |
| 1887 | addUses_Temp(set, e->Iex.RdTmp.tmp); |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 1888 | return; |
| 1889 | case Iex_Const: |
| 1890 | case Iex_Get: |
| 1891 | return; |
| 1892 | default: |
| 1893 | vex_printf("\n"); |
| 1894 | ppIRExpr(e); |
| 1895 | vpanic("addUses_Expr"); |
| 1896 | } |
| 1897 | } |
| 1898 | |
sewardj | d503a32 | 2004-10-13 22:41:16 +0000 | [diff] [blame] | 1899 | static void addUses_Stmt ( Bool* set, IRStmt* st ) |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 1900 | { |
sewardj | 17442fe | 2004-09-20 14:54:28 +0000 | [diff] [blame] | 1901 | Int i; |
| 1902 | IRDirty* d; |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 1903 | switch (st->tag) { |
sewardj | 5a9ffab | 2005-05-12 17:55:01 +0000 | [diff] [blame] | 1904 | case Ist_AbiHint: |
| 1905 | addUses_Expr(set, st->Ist.AbiHint.base); |
| 1906 | return; |
sewardj | d721703 | 2004-08-19 10:49:10 +0000 | [diff] [blame] | 1907 | case Ist_PutI: |
sewardj | eeac841 | 2004-11-02 00:26:55 +0000 | [diff] [blame] | 1908 | addUses_Expr(set, st->Ist.PutI.ix); |
sewardj | 2d3f77c | 2004-09-22 23:49:09 +0000 | [diff] [blame] | 1909 | addUses_Expr(set, st->Ist.PutI.data); |
sewardj | d721703 | 2004-08-19 10:49:10 +0000 | [diff] [blame] | 1910 | return; |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 1911 | case Ist_WrTmp: |
| 1912 | addUses_Expr(set, st->Ist.WrTmp.data); |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 1913 | return; |
| 1914 | case Ist_Put: |
sewardj | 6d07636 | 2004-09-23 11:06:17 +0000 | [diff] [blame] | 1915 | addUses_Expr(set, st->Ist.Put.data); |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 1916 | return; |
sewardj | af1ceca | 2005-06-30 23:31:27 +0000 | [diff] [blame] | 1917 | case Ist_Store: |
| 1918 | addUses_Expr(set, st->Ist.Store.addr); |
| 1919 | addUses_Expr(set, st->Ist.Store.data); |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 1920 | return; |
sewardj | 17442fe | 2004-09-20 14:54:28 +0000 | [diff] [blame] | 1921 | case Ist_Dirty: |
| 1922 | d = st->Ist.Dirty.details; |
| 1923 | if (d->mFx != Ifx_None) |
| 1924 | addUses_Expr(set, d->mAddr); |
sewardj | b8385d8 | 2004-11-02 01:34:15 +0000 | [diff] [blame] | 1925 | addUses_Expr(set, d->guard); |
sewardj | 17442fe | 2004-09-20 14:54:28 +0000 | [diff] [blame] | 1926 | for (i = 0; d->args[i] != NULL; i++) |
| 1927 | addUses_Expr(set, d->args[i]); |
| 1928 | return; |
sewardj | d2445f6 | 2005-03-21 00:15:53 +0000 | [diff] [blame] | 1929 | case Ist_NoOp: |
sewardj | f168931 | 2005-03-16 18:19:10 +0000 | [diff] [blame] | 1930 | case Ist_IMark: |
sewardj | 3e83893 | 2005-01-07 12:09:15 +0000 | [diff] [blame] | 1931 | case Ist_MFence: |
| 1932 | return; |
sewardj | 17442fe | 2004-09-20 14:54:28 +0000 | [diff] [blame] | 1933 | case Ist_Exit: |
sewardj | 0276d4b | 2004-11-15 15:30:21 +0000 | [diff] [blame] | 1934 | addUses_Expr(set, st->Ist.Exit.guard); |
sewardj | 17442fe | 2004-09-20 14:54:28 +0000 | [diff] [blame] | 1935 | return; |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 1936 | default: |
| 1937 | vex_printf("\n"); |
| 1938 | ppIRStmt(st); |
| 1939 | vpanic("addUses_Stmt"); |
sewardj | d503a32 | 2004-10-13 22:41:16 +0000 | [diff] [blame] | 1940 | } |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 1941 | } |
| 1942 | |
| 1943 | |
sewardj | ba99931 | 2004-11-15 15:21:17 +0000 | [diff] [blame] | 1944 | /* Is this literally IRExpr_Const(IRConst_U1(False)) ? */ |
| 1945 | static Bool isZeroU1 ( IRExpr* e ) |
sewardj | d999788 | 2004-11-04 19:42:59 +0000 | [diff] [blame] | 1946 | { |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1947 | return toBool( e->tag == Iex_Const |
| 1948 | && e->Iex.Const.con->tag == Ico_U1 |
| 1949 | && e->Iex.Const.con->Ico.U1 == False ); |
sewardj | d999788 | 2004-11-04 19:42:59 +0000 | [diff] [blame] | 1950 | } |
| 1951 | |
sewardj | e810c19 | 2005-09-09 08:33:03 +0000 | [diff] [blame] | 1952 | /* Is this literally IRExpr_Const(IRConst_U1(True)) ? */ |
| 1953 | static Bool isOneU1 ( IRExpr* e ) |
| 1954 | { |
| 1955 | return toBool( e->tag == Iex_Const |
| 1956 | && e->Iex.Const.con->tag == Ico_U1 |
| 1957 | && e->Iex.Const.con->Ico.U1 == True ); |
| 1958 | } |
| 1959 | |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 1960 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 1961 | /* Note, this destructively modifies the given IRSB. */ |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 1962 | |
| 1963 | /* Scan backwards through statements, carrying a set of IRTemps which |
| 1964 | are known to be used after the current point. On encountering 't = |
| 1965 | E', delete the binding if it is not used. Otherwise, add any temp |
sewardj | e810c19 | 2005-09-09 08:33:03 +0000 | [diff] [blame] | 1966 | uses to the set and keep on moving backwards. |
| 1967 | |
| 1968 | As an enhancement, the first (backwards) pass searches for IR exits |
| 1969 | with always-taken conditions and notes the location of the earliest |
| 1970 | one in the block. If any such are found, a second pass copies the |
| 1971 | exit destination and jump kind to the bb-end. Then, the exit and |
| 1972 | all statements following it are turned into no-ops. |
| 1973 | */ |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 1974 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 1975 | /* notstatic */ void do_deadcode_BB ( IRSB* bb ) |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 1976 | { |
sewardj | e810c19 | 2005-09-09 08:33:03 +0000 | [diff] [blame] | 1977 | Int i, i_unconditional_exit; |
sewardj | d503a32 | 2004-10-13 22:41:16 +0000 | [diff] [blame] | 1978 | Int n_tmps = bb->tyenv->types_used; |
| 1979 | Bool* set = LibVEX_Alloc(n_tmps * sizeof(Bool)); |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 1980 | IRStmt* st; |
| 1981 | |
sewardj | d503a32 | 2004-10-13 22:41:16 +0000 | [diff] [blame] | 1982 | for (i = 0; i < n_tmps; i++) |
| 1983 | set[i] = False; |
| 1984 | |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 1985 | /* start off by recording IRTemp uses in the next field. */ |
| 1986 | addUses_Expr(set, bb->next); |
| 1987 | |
sewardj | e810c19 | 2005-09-09 08:33:03 +0000 | [diff] [blame] | 1988 | /* First pass */ |
| 1989 | |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 1990 | /* Work backwards through the stmts */ |
sewardj | e810c19 | 2005-09-09 08:33:03 +0000 | [diff] [blame] | 1991 | i_unconditional_exit = -1; |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 1992 | for (i = bb->stmts_used-1; i >= 0; i--) { |
| 1993 | st = bb->stmts[i]; |
sewardj | 8bee6d1 | 2005-03-22 02:24:05 +0000 | [diff] [blame] | 1994 | if (st->tag == Ist_NoOp) |
sewardj | 84ff065 | 2004-08-23 16:16:08 +0000 | [diff] [blame] | 1995 | continue; |
sewardj | e810c19 | 2005-09-09 08:33:03 +0000 | [diff] [blame] | 1996 | /* take note of any unconditional exits */ |
| 1997 | if (st->tag == Ist_Exit |
| 1998 | && isOneU1(st->Ist.Exit.guard)) |
| 1999 | i_unconditional_exit = i; |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2000 | if (st->tag == Ist_WrTmp |
| 2001 | && set[(Int)(st->Ist.WrTmp.tmp)] == False) { |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 2002 | /* it's an IRTemp which never got used. Delete it. */ |
sewardj | 088bcb4 | 2004-08-19 17:16:52 +0000 | [diff] [blame] | 2003 | if (DEBUG_IROPT) { |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 2004 | vex_printf("DEAD: "); |
| 2005 | ppIRStmt(st); |
| 2006 | vex_printf("\n"); |
| 2007 | } |
sewardj | d2445f6 | 2005-03-21 00:15:53 +0000 | [diff] [blame] | 2008 | bb->stmts[i] = IRStmt_NoOp(); |
sewardj | d999788 | 2004-11-04 19:42:59 +0000 | [diff] [blame] | 2009 | } |
| 2010 | else |
| 2011 | if (st->tag == Ist_Dirty |
| 2012 | && st->Ist.Dirty.details->guard |
sewardj | ba99931 | 2004-11-15 15:21:17 +0000 | [diff] [blame] | 2013 | && isZeroU1(st->Ist.Dirty.details->guard)) { |
sewardj | d2445f6 | 2005-03-21 00:15:53 +0000 | [diff] [blame] | 2014 | /* This is a dirty helper which will never get called. |
| 2015 | Delete it. */ |
| 2016 | bb->stmts[i] = IRStmt_NoOp(); |
sewardj | d999788 | 2004-11-04 19:42:59 +0000 | [diff] [blame] | 2017 | } |
| 2018 | else { |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 2019 | /* Note any IRTemp uses made by the current statement. */ |
| 2020 | addUses_Stmt(set, st); |
| 2021 | } |
| 2022 | } |
sewardj | e810c19 | 2005-09-09 08:33:03 +0000 | [diff] [blame] | 2023 | |
| 2024 | /* Optional second pass: if any unconditional exits were found, |
| 2025 | delete them and all following statements. */ |
| 2026 | |
| 2027 | if (i_unconditional_exit != -1) { |
| 2028 | if (0) vex_printf("ZAPPING ALL FORWARDS from %d\n", |
| 2029 | i_unconditional_exit); |
| 2030 | vassert(i_unconditional_exit >= 0 |
| 2031 | && i_unconditional_exit < bb->stmts_used); |
| 2032 | bb->next |
| 2033 | = IRExpr_Const( bb->stmts[i_unconditional_exit]->Ist.Exit.dst ); |
| 2034 | bb->jumpkind |
| 2035 | = bb->stmts[i_unconditional_exit]->Ist.Exit.jk; |
| 2036 | for (i = i_unconditional_exit; i < bb->stmts_used; i++) |
| 2037 | bb->stmts[i] = IRStmt_NoOp(); |
| 2038 | } |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 2039 | } |
| 2040 | |
sewardj | e810c19 | 2005-09-09 08:33:03 +0000 | [diff] [blame] | 2041 | |
sewardj | 84ff065 | 2004-08-23 16:16:08 +0000 | [diff] [blame] | 2042 | /*---------------------------------------------------------------*/ |
| 2043 | /*--- Specialisation of helper function calls, in ---*/ |
| 2044 | /*--- collaboration with the front end ---*/ |
| 2045 | /*---------------------------------------------------------------*/ |
| 2046 | |
| 2047 | static |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2048 | IRSB* spec_helpers_BB ( IRSB* bb, |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 2049 | IRExpr* (*specHelper) ( HChar*, IRExpr**) ) |
sewardj | 84ff065 | 2004-08-23 16:16:08 +0000 | [diff] [blame] | 2050 | { |
sewardj | b923075 | 2004-12-29 19:25:06 +0000 | [diff] [blame] | 2051 | Int i; |
sewardj | 84ff065 | 2004-08-23 16:16:08 +0000 | [diff] [blame] | 2052 | IRStmt* st; |
| 2053 | IRExpr* ex; |
sewardj | b923075 | 2004-12-29 19:25:06 +0000 | [diff] [blame] | 2054 | Bool any = False; |
sewardj | 84ff065 | 2004-08-23 16:16:08 +0000 | [diff] [blame] | 2055 | |
| 2056 | for (i = bb->stmts_used-1; i >= 0; i--) { |
| 2057 | st = bb->stmts[i]; |
| 2058 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2059 | if (st->tag != Ist_WrTmp |
| 2060 | || st->Ist.WrTmp.data->tag != Iex_CCall) |
sewardj | 8bee6d1 | 2005-03-22 02:24:05 +0000 | [diff] [blame] | 2061 | continue; |
sewardj | 84ff065 | 2004-08-23 16:16:08 +0000 | [diff] [blame] | 2062 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2063 | ex = (*specHelper)( st->Ist.WrTmp.data->Iex.CCall.cee->name, |
| 2064 | st->Ist.WrTmp.data->Iex.CCall.args ); |
sewardj | 84ff065 | 2004-08-23 16:16:08 +0000 | [diff] [blame] | 2065 | if (!ex) |
sewardj | aba4fff | 2004-10-08 21:37:15 +0000 | [diff] [blame] | 2066 | /* the front end can't think of a suitable replacement */ |
| 2067 | continue; |
sewardj | 84ff065 | 2004-08-23 16:16:08 +0000 | [diff] [blame] | 2068 | |
| 2069 | /* We got something better. Install it in the bb. */ |
sewardj | b923075 | 2004-12-29 19:25:06 +0000 | [diff] [blame] | 2070 | any = True; |
sewardj | 84ff065 | 2004-08-23 16:16:08 +0000 | [diff] [blame] | 2071 | bb->stmts[i] |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2072 | = IRStmt_WrTmp(st->Ist.WrTmp.tmp, ex); |
sewardj | 84ff065 | 2004-08-23 16:16:08 +0000 | [diff] [blame] | 2073 | |
| 2074 | if (0) { |
| 2075 | vex_printf("SPEC: "); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2076 | ppIRExpr(st->Ist.WrTmp.data); |
sewardj | 84ff065 | 2004-08-23 16:16:08 +0000 | [diff] [blame] | 2077 | vex_printf(" --> "); |
| 2078 | ppIRExpr(ex); |
| 2079 | vex_printf("\n"); |
| 2080 | } |
| 2081 | } |
sewardj | b923075 | 2004-12-29 19:25:06 +0000 | [diff] [blame] | 2082 | |
| 2083 | if (any) |
| 2084 | bb = flatten_BB(bb); |
| 2085 | return bb; |
sewardj | 84ff065 | 2004-08-23 16:16:08 +0000 | [diff] [blame] | 2086 | } |
| 2087 | |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 2088 | |
| 2089 | /*---------------------------------------------------------------*/ |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 2090 | /*--- Determination of guest state aliasing relationships ---*/ |
| 2091 | /*---------------------------------------------------------------*/ |
| 2092 | |
| 2093 | /* These are helper functions for CSE and GetI/PutI transformations. |
| 2094 | |
| 2095 | Determine, to the extent possible, the relationship between two |
| 2096 | guest state accesses. The possible outcomes are: |
| 2097 | |
| 2098 | * Exact alias. These two accesses denote precisely the same |
| 2099 | piece of the guest state. |
| 2100 | |
| 2101 | * Definitely no alias. These two accesses are guaranteed not to |
| 2102 | overlap any part of the guest state. |
| 2103 | |
| 2104 | * Unknown -- if neither of the above can be established. |
| 2105 | |
| 2106 | If in doubt, return Unknown. */ |
| 2107 | |
| 2108 | typedef |
| 2109 | enum { ExactAlias, NoAlias, UnknownAlias } |
| 2110 | GSAliasing; |
| 2111 | |
| 2112 | |
| 2113 | /* Produces the alias relation between an indexed guest |
| 2114 | state access and a non-indexed access. */ |
| 2115 | |
| 2116 | static |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2117 | GSAliasing getAliasingRelation_IC ( IRRegArray* descr1, IRExpr* ix1, |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 2118 | Int offset2, IRType ty2 ) |
| 2119 | { |
| 2120 | UInt minoff1, maxoff1, minoff2, maxoff2; |
| 2121 | |
| 2122 | getArrayBounds( descr1, &minoff1, &maxoff1 ); |
| 2123 | minoff2 = offset2; |
| 2124 | maxoff2 = minoff2 + sizeofIRType(ty2) - 1; |
| 2125 | |
| 2126 | if (maxoff1 < minoff2 || maxoff2 < minoff1) |
| 2127 | return NoAlias; |
| 2128 | |
| 2129 | /* Could probably do better here if required. For the moment |
| 2130 | however just claim not to know anything more. */ |
| 2131 | return UnknownAlias; |
| 2132 | } |
| 2133 | |
| 2134 | |
| 2135 | /* Produces the alias relation between two indexed guest state |
| 2136 | accesses. */ |
| 2137 | |
| 2138 | static |
| 2139 | GSAliasing getAliasingRelation_II ( |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2140 | IRRegArray* descr1, IRExpr* ix1, Int bias1, |
| 2141 | IRRegArray* descr2, IRExpr* ix2, Int bias2 |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 2142 | ) |
| 2143 | { |
| 2144 | UInt minoff1, maxoff1, minoff2, maxoff2; |
| 2145 | Int iters; |
| 2146 | |
| 2147 | /* First try hard to show they don't alias. */ |
| 2148 | getArrayBounds( descr1, &minoff1, &maxoff1 ); |
| 2149 | getArrayBounds( descr2, &minoff2, &maxoff2 ); |
| 2150 | if (maxoff1 < minoff2 || maxoff2 < minoff1) |
| 2151 | return NoAlias; |
| 2152 | |
| 2153 | /* So the two arrays at least partially overlap. To get any |
| 2154 | further we'll have to be sure that the descriptors are |
| 2155 | identical. */ |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2156 | if (!eqIRRegArray(descr1, descr2)) |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 2157 | return UnknownAlias; |
| 2158 | |
| 2159 | /* The descriptors are identical. Now the only difference can be |
| 2160 | in the index expressions. If they cannot be shown to be |
| 2161 | identical, we have to say we don't know what the aliasing |
| 2162 | relation will be. Now, since the IR is flattened, the index |
| 2163 | expressions should be atoms -- either consts or tmps. So that |
| 2164 | makes the comparison simple. */ |
| 2165 | vassert(isIRAtom(ix1)); |
| 2166 | vassert(isIRAtom(ix2)); |
| 2167 | if (!eqIRAtom(ix1,ix2)) |
| 2168 | return UnknownAlias; |
| 2169 | |
| 2170 | /* Ok, the index expressions are identical. So now the only way |
| 2171 | they can be different is in the bias. Normalise this |
| 2172 | paranoidly, to reliably establish equality/non-equality. */ |
| 2173 | |
| 2174 | /* So now we know that the GetI and PutI index the same array |
| 2175 | with the same base. Are the offsets the same, modulo the |
| 2176 | array size? Do this paranoidly. */ |
| 2177 | vassert(descr1->nElems == descr2->nElems); |
| 2178 | vassert(descr1->elemTy == descr2->elemTy); |
| 2179 | vassert(descr1->base == descr2->base); |
| 2180 | iters = 0; |
| 2181 | while (bias1 < 0 || bias2 < 0) { |
| 2182 | bias1 += descr1->nElems; |
| 2183 | bias2 += descr1->nElems; |
| 2184 | iters++; |
| 2185 | if (iters > 10) |
| 2186 | vpanic("getAliasingRelation: iters"); |
| 2187 | } |
| 2188 | vassert(bias1 >= 0 && bias2 >= 0); |
| 2189 | bias1 %= descr1->nElems; |
| 2190 | bias2 %= descr1->nElems; |
| 2191 | vassert(bias1 >= 0 && bias1 < descr1->nElems); |
| 2192 | vassert(bias2 >= 0 && bias2 < descr1->nElems); |
| 2193 | |
| 2194 | /* Finally, biasP and biasG are normalised into the range |
| 2195 | 0 .. descrP/G->nElems - 1. And so we can establish |
| 2196 | equality/non-equality. */ |
| 2197 | |
| 2198 | return bias1==bias2 ? ExactAlias : NoAlias; |
| 2199 | } |
| 2200 | |
| 2201 | |
| 2202 | /*---------------------------------------------------------------*/ |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 2203 | /*--- Common Subexpression Elimination ---*/ |
| 2204 | /*---------------------------------------------------------------*/ |
| 2205 | |
| 2206 | /* Expensive in time and space. */ |
| 2207 | |
| 2208 | /* Uses two environments: |
| 2209 | a IRTemp -> IRTemp mapping |
| 2210 | a mapping from AvailExpr* to IRTemp |
| 2211 | */ |
| 2212 | |
| 2213 | typedef |
| 2214 | struct { |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 2215 | enum { Ut, Btt, Btc, Bct, Cf64i, Mttt, GetIt } tag; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 2216 | union { |
| 2217 | /* unop(tmp) */ |
| 2218 | struct { |
| 2219 | IROp op; |
| 2220 | IRTemp arg; |
| 2221 | } Ut; |
| 2222 | /* binop(tmp,tmp) */ |
| 2223 | struct { |
| 2224 | IROp op; |
| 2225 | IRTemp arg1; |
| 2226 | IRTemp arg2; |
| 2227 | } Btt; |
| 2228 | /* binop(tmp,const) */ |
| 2229 | struct { |
| 2230 | IROp op; |
| 2231 | IRTemp arg1; |
| 2232 | IRConst con2; |
| 2233 | } Btc; |
| 2234 | /* binop(const,tmp) */ |
| 2235 | struct { |
| 2236 | IROp op; |
| 2237 | IRConst con1; |
| 2238 | IRTemp arg2; |
| 2239 | } Bct; |
| 2240 | /* F64i-style const */ |
| 2241 | struct { |
| 2242 | ULong f64i; |
| 2243 | } Cf64i; |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 2244 | /* Mux0X(tmp,tmp,tmp) */ |
| 2245 | struct { |
| 2246 | IRTemp co; |
| 2247 | IRTemp e0; |
| 2248 | IRTemp eX; |
| 2249 | } Mttt; |
| 2250 | /* GetI(descr,tmp,bias)*/ |
| 2251 | struct { |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2252 | IRRegArray* descr; |
| 2253 | IRTemp ix; |
| 2254 | Int bias; |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 2255 | } GetIt; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 2256 | } u; |
| 2257 | } |
| 2258 | AvailExpr; |
| 2259 | |
| 2260 | static Bool eq_AvailExpr ( AvailExpr* a1, AvailExpr* a2 ) |
| 2261 | { |
| 2262 | if (a1->tag != a2->tag) |
| 2263 | return False; |
| 2264 | switch (a1->tag) { |
| 2265 | case Ut: |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 2266 | return toBool( |
| 2267 | a1->u.Ut.op == a2->u.Ut.op |
| 2268 | && a1->u.Ut.arg == a2->u.Ut.arg); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 2269 | case Btt: |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 2270 | return toBool( |
| 2271 | a1->u.Btt.op == a2->u.Btt.op |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 2272 | && a1->u.Btt.arg1 == a2->u.Btt.arg1 |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 2273 | && a1->u.Btt.arg2 == a2->u.Btt.arg2); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 2274 | case Btc: |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 2275 | return toBool( |
| 2276 | a1->u.Btc.op == a2->u.Btc.op |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 2277 | && a1->u.Btc.arg1 == a2->u.Btc.arg1 |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 2278 | && eqIRConst(&a1->u.Btc.con2, &a2->u.Btc.con2)); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 2279 | case Bct: |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 2280 | return toBool( |
| 2281 | a1->u.Bct.op == a2->u.Bct.op |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 2282 | && a1->u.Bct.arg2 == a2->u.Bct.arg2 |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 2283 | && eqIRConst(&a1->u.Bct.con1, &a2->u.Bct.con1)); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 2284 | case Cf64i: |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 2285 | return toBool(a1->u.Cf64i.f64i == a2->u.Cf64i.f64i); |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 2286 | case Mttt: |
| 2287 | return toBool(a1->u.Mttt.co == a2->u.Mttt.co |
| 2288 | && a1->u.Mttt.e0 == a2->u.Mttt.e0 |
| 2289 | && a1->u.Mttt.eX == a2->u.Mttt.eX); |
| 2290 | case GetIt: |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2291 | return toBool(eqIRRegArray(a1->u.GetIt.descr, a2->u.GetIt.descr) |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 2292 | && a1->u.GetIt.ix == a2->u.GetIt.ix |
| 2293 | && a1->u.GetIt.bias == a2->u.GetIt.bias); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 2294 | default: vpanic("eq_AvailExpr"); |
| 2295 | } |
| 2296 | } |
| 2297 | |
| 2298 | static IRExpr* availExpr_to_IRExpr ( AvailExpr* ae ) |
| 2299 | { |
| 2300 | IRConst* con; |
| 2301 | switch (ae->tag) { |
| 2302 | case Ut: |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2303 | return IRExpr_Unop( ae->u.Ut.op, IRExpr_RdTmp(ae->u.Ut.arg) ); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 2304 | case Btt: |
| 2305 | return IRExpr_Binop( ae->u.Btt.op, |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2306 | IRExpr_RdTmp(ae->u.Btt.arg1), |
| 2307 | IRExpr_RdTmp(ae->u.Btt.arg2) ); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 2308 | case Btc: |
| 2309 | con = LibVEX_Alloc(sizeof(IRConst)); |
| 2310 | *con = ae->u.Btc.con2; |
| 2311 | return IRExpr_Binop( ae->u.Btc.op, |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2312 | IRExpr_RdTmp(ae->u.Btc.arg1), |
| 2313 | IRExpr_Const(con) ); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 2314 | case Bct: |
| 2315 | con = LibVEX_Alloc(sizeof(IRConst)); |
| 2316 | *con = ae->u.Bct.con1; |
| 2317 | return IRExpr_Binop( ae->u.Bct.op, |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2318 | IRExpr_Const(con), |
| 2319 | IRExpr_RdTmp(ae->u.Bct.arg2) ); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 2320 | case Cf64i: |
| 2321 | return IRExpr_Const(IRConst_F64i(ae->u.Cf64i.f64i)); |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 2322 | case Mttt: |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2323 | return IRExpr_Mux0X(IRExpr_RdTmp(ae->u.Mttt.co), |
| 2324 | IRExpr_RdTmp(ae->u.Mttt.e0), |
| 2325 | IRExpr_RdTmp(ae->u.Mttt.eX)); |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 2326 | case GetIt: |
| 2327 | return IRExpr_GetI(ae->u.GetIt.descr, |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2328 | IRExpr_RdTmp(ae->u.GetIt.ix), |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 2329 | ae->u.GetIt.bias); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 2330 | default: |
| 2331 | vpanic("availExpr_to_IRExpr"); |
| 2332 | } |
| 2333 | } |
| 2334 | |
| 2335 | inline |
| 2336 | static IRTemp subst_AvailExpr_Temp ( HashHW* env, IRTemp tmp ) |
| 2337 | { |
| 2338 | HWord res; |
| 2339 | /* env :: IRTemp -> IRTemp */ |
| 2340 | if (lookupHHW( env, &res, (HWord)tmp )) |
| 2341 | return (IRTemp)res; |
| 2342 | else |
| 2343 | return tmp; |
| 2344 | } |
| 2345 | |
| 2346 | static void subst_AvailExpr ( HashHW* env, AvailExpr* ae ) |
| 2347 | { |
| 2348 | /* env :: IRTemp -> IRTemp */ |
| 2349 | switch (ae->tag) { |
| 2350 | case Ut: |
| 2351 | ae->u.Ut.arg = subst_AvailExpr_Temp( env, ae->u.Ut.arg ); |
| 2352 | break; |
| 2353 | case Btt: |
| 2354 | ae->u.Btt.arg1 = subst_AvailExpr_Temp( env, ae->u.Btt.arg1 ); |
| 2355 | ae->u.Btt.arg2 = subst_AvailExpr_Temp( env, ae->u.Btt.arg2 ); |
| 2356 | break; |
| 2357 | case Btc: |
| 2358 | ae->u.Btc.arg1 = subst_AvailExpr_Temp( env, ae->u.Btc.arg1 ); |
| 2359 | break; |
| 2360 | case Bct: |
| 2361 | ae->u.Bct.arg2 = subst_AvailExpr_Temp( env, ae->u.Bct.arg2 ); |
| 2362 | break; |
| 2363 | case Cf64i: |
| 2364 | break; |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 2365 | case Mttt: |
| 2366 | ae->u.Mttt.co = subst_AvailExpr_Temp( env, ae->u.Mttt.co ); |
| 2367 | ae->u.Mttt.e0 = subst_AvailExpr_Temp( env, ae->u.Mttt.e0 ); |
| 2368 | ae->u.Mttt.eX = subst_AvailExpr_Temp( env, ae->u.Mttt.eX ); |
| 2369 | break; |
| 2370 | case GetIt: |
| 2371 | ae->u.GetIt.ix = subst_AvailExpr_Temp( env, ae->u.GetIt.ix ); |
| 2372 | break; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 2373 | default: |
| 2374 | vpanic("subst_AvailExpr"); |
| 2375 | } |
| 2376 | } |
| 2377 | |
| 2378 | static AvailExpr* irExpr_to_AvailExpr ( IRExpr* e ) |
| 2379 | { |
| 2380 | AvailExpr* ae; |
| 2381 | |
| 2382 | if (e->tag == Iex_Unop |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2383 | && e->Iex.Unop.arg->tag == Iex_RdTmp) { |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 2384 | ae = LibVEX_Alloc(sizeof(AvailExpr)); |
| 2385 | ae->tag = Ut; |
| 2386 | ae->u.Ut.op = e->Iex.Unop.op; |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2387 | ae->u.Ut.arg = e->Iex.Unop.arg->Iex.RdTmp.tmp; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 2388 | return ae; |
| 2389 | } |
| 2390 | |
| 2391 | if (e->tag == Iex_Binop |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2392 | && e->Iex.Binop.arg1->tag == Iex_RdTmp |
| 2393 | && e->Iex.Binop.arg2->tag == Iex_RdTmp) { |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 2394 | ae = LibVEX_Alloc(sizeof(AvailExpr)); |
| 2395 | ae->tag = Btt; |
| 2396 | ae->u.Btt.op = e->Iex.Binop.op; |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2397 | ae->u.Btt.arg1 = e->Iex.Binop.arg1->Iex.RdTmp.tmp; |
| 2398 | ae->u.Btt.arg2 = e->Iex.Binop.arg2->Iex.RdTmp.tmp; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 2399 | return ae; |
| 2400 | } |
| 2401 | |
| 2402 | if (e->tag == Iex_Binop |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2403 | && e->Iex.Binop.arg1->tag == Iex_RdTmp |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 2404 | && e->Iex.Binop.arg2->tag == Iex_Const) { |
| 2405 | ae = LibVEX_Alloc(sizeof(AvailExpr)); |
| 2406 | ae->tag = Btc; |
| 2407 | ae->u.Btc.op = e->Iex.Binop.op; |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2408 | ae->u.Btc.arg1 = e->Iex.Binop.arg1->Iex.RdTmp.tmp; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 2409 | ae->u.Btc.con2 = *(e->Iex.Binop.arg2->Iex.Const.con); |
| 2410 | return ae; |
| 2411 | } |
| 2412 | |
| 2413 | if (e->tag == Iex_Binop |
| 2414 | && e->Iex.Binop.arg1->tag == Iex_Const |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2415 | && e->Iex.Binop.arg2->tag == Iex_RdTmp) { |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 2416 | ae = LibVEX_Alloc(sizeof(AvailExpr)); |
| 2417 | ae->tag = Bct; |
| 2418 | ae->u.Bct.op = e->Iex.Binop.op; |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2419 | ae->u.Bct.arg2 = e->Iex.Binop.arg2->Iex.RdTmp.tmp; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 2420 | ae->u.Bct.con1 = *(e->Iex.Binop.arg1->Iex.Const.con); |
| 2421 | return ae; |
| 2422 | } |
| 2423 | |
| 2424 | if (e->tag == Iex_Const |
| 2425 | && e->Iex.Const.con->tag == Ico_F64i) { |
| 2426 | ae = LibVEX_Alloc(sizeof(AvailExpr)); |
| 2427 | ae->tag = Cf64i; |
| 2428 | ae->u.Cf64i.f64i = e->Iex.Const.con->Ico.F64i; |
| 2429 | return ae; |
| 2430 | } |
| 2431 | |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 2432 | if (e->tag == Iex_Mux0X |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2433 | && e->Iex.Mux0X.cond->tag == Iex_RdTmp |
| 2434 | && e->Iex.Mux0X.expr0->tag == Iex_RdTmp |
| 2435 | && e->Iex.Mux0X.exprX->tag == Iex_RdTmp) { |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 2436 | ae = LibVEX_Alloc(sizeof(AvailExpr)); |
| 2437 | ae->tag = Mttt; |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2438 | ae->u.Mttt.co = e->Iex.Mux0X.cond->Iex.RdTmp.tmp; |
| 2439 | ae->u.Mttt.e0 = e->Iex.Mux0X.expr0->Iex.RdTmp.tmp; |
| 2440 | ae->u.Mttt.eX = e->Iex.Mux0X.exprX->Iex.RdTmp.tmp; |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 2441 | return ae; |
| 2442 | } |
| 2443 | |
| 2444 | if (e->tag == Iex_GetI |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2445 | && e->Iex.GetI.ix->tag == Iex_RdTmp) { |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 2446 | ae = LibVEX_Alloc(sizeof(AvailExpr)); |
| 2447 | ae->tag = GetIt; |
| 2448 | ae->u.GetIt.descr = e->Iex.GetI.descr; |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2449 | ae->u.GetIt.ix = e->Iex.GetI.ix->Iex.RdTmp.tmp; |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 2450 | ae->u.GetIt.bias = e->Iex.GetI.bias; |
| 2451 | return ae; |
| 2452 | } |
| 2453 | |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 2454 | return NULL; |
| 2455 | } |
| 2456 | |
| 2457 | |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 2458 | /* The BB is modified in-place. Returns True if any changes were |
| 2459 | made. */ |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 2460 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2461 | static Bool do_cse_BB ( IRSB* bb ) |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 2462 | { |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 2463 | Int i, j, paranoia; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 2464 | IRTemp t, q; |
| 2465 | IRStmt* st; |
| 2466 | AvailExpr* eprime; |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 2467 | AvailExpr* ae; |
| 2468 | Bool invalidate; |
| 2469 | Bool anyDone = False; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 2470 | |
| 2471 | HashHW* tenv = newHHW(); /* :: IRTemp -> IRTemp */ |
| 2472 | HashHW* aenv = newHHW(); /* :: AvailExpr* -> IRTemp */ |
| 2473 | |
| 2474 | vassert(sizeof(IRTemp) <= sizeof(HWord)); |
| 2475 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2476 | if (0) { ppIRSB(bb); vex_printf("\n\n"); } |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 2477 | |
| 2478 | /* Iterate forwards over the stmts. |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 2479 | On seeing "t = E", where E is one of the 5 AvailExpr forms: |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 2480 | let E' = apply tenv substitution to E |
| 2481 | search aenv for E' |
| 2482 | if a mapping E' -> q is found, |
| 2483 | replace this stmt by "t = q" |
| 2484 | and add binding t -> q to tenv |
| 2485 | else |
| 2486 | add binding E' -> t to aenv |
| 2487 | replace this stmt by "t = E'" |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 2488 | |
| 2489 | Other statements are only interesting to the extent that they |
| 2490 | might invalidate some of the expressions in aenv. So there is |
| 2491 | an invalidate-bindings check for each statement seen. |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 2492 | */ |
| 2493 | for (i = 0; i < bb->stmts_used; i++) { |
| 2494 | st = bb->stmts[i]; |
| 2495 | |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 2496 | /* ------ BEGIN invalidate aenv bindings ------ */ |
| 2497 | /* This is critical: remove from aenv any E' -> .. bindings |
| 2498 | which might be invalidated by this statement. The only |
| 2499 | vulnerable kind of bindings are the GetIt kind. |
| 2500 | Dirty call - dump (paranoia level -> 2) |
| 2501 | Store - dump (ditto) |
| 2502 | Put, PutI - dump unless no-overlap is proven (.. -> 1) |
| 2503 | Uses getAliasingRelation_IC and getAliasingRelation_II |
| 2504 | to do the no-overlap assessments needed for Put/PutI. |
| 2505 | */ |
| 2506 | switch (st->tag) { |
| 2507 | case Ist_Dirty: case Ist_Store: |
| 2508 | paranoia = 2; break; |
| 2509 | case Ist_Put: case Ist_PutI: |
| 2510 | paranoia = 1; break; |
| 2511 | case Ist_NoOp: case Ist_IMark: case Ist_AbiHint: |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2512 | case Ist_WrTmp: case Ist_MFence: case Ist_Exit: |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 2513 | paranoia = 0; break; |
| 2514 | default: |
| 2515 | vpanic("do_cse_BB(1)"); |
| 2516 | } |
| 2517 | |
| 2518 | if (paranoia > 0) { |
| 2519 | for (j = 0; j < aenv->used; j++) { |
| 2520 | if (!aenv->inuse[j]) |
| 2521 | continue; |
| 2522 | ae = (AvailExpr*)aenv->key[j]; |
| 2523 | if (ae->tag != GetIt) |
| 2524 | continue; |
| 2525 | invalidate = False; |
| 2526 | if (paranoia >= 2) { |
| 2527 | invalidate = True; |
| 2528 | } else { |
| 2529 | vassert(paranoia == 1); |
| 2530 | if (st->tag == Ist_Put) { |
| 2531 | if (getAliasingRelation_IC( |
| 2532 | ae->u.GetIt.descr, |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2533 | IRExpr_RdTmp(ae->u.GetIt.ix), |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 2534 | st->Ist.Put.offset, |
| 2535 | typeOfIRExpr(bb->tyenv,st->Ist.Put.data) |
| 2536 | ) != NoAlias) |
| 2537 | invalidate = True; |
| 2538 | } |
| 2539 | else |
| 2540 | if (st->tag == Ist_PutI) { |
| 2541 | if (getAliasingRelation_II( |
| 2542 | ae->u.GetIt.descr, |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2543 | IRExpr_RdTmp(ae->u.GetIt.ix), |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 2544 | ae->u.GetIt.bias, |
| 2545 | st->Ist.PutI.descr, |
| 2546 | st->Ist.PutI.ix, |
| 2547 | st->Ist.PutI.bias |
| 2548 | ) != NoAlias) |
| 2549 | invalidate = True; |
| 2550 | } |
| 2551 | else |
| 2552 | vpanic("do_cse_BB(2)"); |
| 2553 | } |
| 2554 | |
| 2555 | if (invalidate) { |
| 2556 | aenv->inuse[j] = False; |
| 2557 | aenv->key[j] = (HWord)NULL; /* be sure */ |
| 2558 | } |
| 2559 | } /* for j */ |
| 2560 | } /* paranoia > 0 */ |
| 2561 | |
| 2562 | /* ------ ENV invalidate aenv bindings ------ */ |
| 2563 | |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 2564 | /* ignore not-interestings */ |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2565 | if (st->tag != Ist_WrTmp) |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 2566 | continue; |
| 2567 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2568 | t = st->Ist.WrTmp.tmp; |
| 2569 | eprime = irExpr_to_AvailExpr(st->Ist.WrTmp.data); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 2570 | /* ignore if not of AvailExpr form */ |
| 2571 | if (!eprime) |
| 2572 | continue; |
| 2573 | |
| 2574 | /* vex_printf("considering: " ); ppIRStmt(st); vex_printf("\n"); */ |
| 2575 | |
| 2576 | /* apply tenv */ |
| 2577 | subst_AvailExpr( tenv, eprime ); |
| 2578 | |
| 2579 | /* search aenv for eprime, unfortunately the hard way */ |
| 2580 | for (j = 0; j < aenv->used; j++) |
| 2581 | if (aenv->inuse[j] && eq_AvailExpr(eprime, (AvailExpr*)aenv->key[j])) |
| 2582 | break; |
| 2583 | |
| 2584 | if (j < aenv->used) { |
| 2585 | /* A binding E' -> q was found. Replace stmt by "t = q" and |
| 2586 | note the t->q binding in tenv. */ |
| 2587 | /* (this is the core of the CSE action) */ |
| 2588 | q = (IRTemp)aenv->val[j]; |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2589 | bb->stmts[i] = IRStmt_WrTmp( t, IRExpr_RdTmp(q) ); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 2590 | addToHHW( tenv, (HWord)t, (HWord)q ); |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 2591 | anyDone = True; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 2592 | } else { |
| 2593 | /* No binding was found, so instead we add E' -> t to our |
| 2594 | collection of available expressions, replace this stmt |
| 2595 | with "t = E'", and move on. */ |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2596 | bb->stmts[i] = IRStmt_WrTmp( t, availExpr_to_IRExpr(eprime) ); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 2597 | addToHHW( aenv, (HWord)eprime, (HWord)t ); |
| 2598 | } |
| 2599 | } |
| 2600 | |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 2601 | /* |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2602 | ppIRSB(bb); |
| 2603 | sanityCheckIRSB(bb, Ity_I32); |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 2604 | vex_printf("\n\n"); |
| 2605 | */ |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 2606 | return anyDone; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 2607 | } |
| 2608 | |
| 2609 | |
| 2610 | /*---------------------------------------------------------------*/ |
| 2611 | /*--- Add32/Sub32 chain collapsing ---*/ |
| 2612 | /*---------------------------------------------------------------*/ |
| 2613 | |
| 2614 | /* ----- Helper functions for Add32/Sub32 chain collapsing ----- */ |
| 2615 | |
| 2616 | /* Is this expression "Add32(tmp,const)" or "Sub32(tmp,const)" ? If |
| 2617 | yes, set *tmp and *i32 appropriately. *i32 is set as if the |
| 2618 | root node is Add32, not Sub32. */ |
| 2619 | |
| 2620 | static Bool isAdd32OrSub32 ( IRExpr* e, IRTemp* tmp, Int* i32 ) |
| 2621 | { |
| 2622 | if (e->tag != Iex_Binop) |
| 2623 | return False; |
| 2624 | if (e->Iex.Binop.op != Iop_Add32 && e->Iex.Binop.op != Iop_Sub32) |
| 2625 | return False; |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2626 | if (e->Iex.Binop.arg1->tag != Iex_RdTmp) |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 2627 | return False; |
| 2628 | if (e->Iex.Binop.arg2->tag != Iex_Const) |
| 2629 | return False; |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2630 | *tmp = e->Iex.Binop.arg1->Iex.RdTmp.tmp; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 2631 | *i32 = (Int)(e->Iex.Binop.arg2->Iex.Const.con->Ico.U32); |
| 2632 | if (e->Iex.Binop.op == Iop_Sub32) |
| 2633 | *i32 = -*i32; |
| 2634 | return True; |
| 2635 | } |
| 2636 | |
| 2637 | |
| 2638 | /* Figure out if tmp can be expressed as tmp2 +32 const, for some |
| 2639 | other tmp2. Scan backwards from the specified start point -- an |
| 2640 | optimisation. */ |
| 2641 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2642 | static Bool collapseChain ( IRSB* bb, Int startHere, |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 2643 | IRTemp tmp, |
| 2644 | IRTemp* tmp2, Int* i32 ) |
| 2645 | { |
| 2646 | Int j, ii; |
| 2647 | IRTemp vv; |
| 2648 | IRStmt* st; |
| 2649 | IRExpr* e; |
| 2650 | |
| 2651 | /* the (var, con) pair contain the current 'representation' for |
| 2652 | 'tmp'. We start with 'tmp + 0'. */ |
| 2653 | IRTemp var = tmp; |
| 2654 | Int con = 0; |
| 2655 | |
| 2656 | /* Scan backwards to see if tmp can be replaced by some other tmp |
| 2657 | +/- a constant. */ |
| 2658 | for (j = startHere; j >= 0; j--) { |
| 2659 | st = bb->stmts[j]; |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2660 | if (st->tag != Ist_WrTmp) |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 2661 | continue; |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2662 | if (st->Ist.WrTmp.tmp != var) |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 2663 | continue; |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2664 | e = st->Ist.WrTmp.data; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 2665 | if (!isAdd32OrSub32(e, &vv, &ii)) |
| 2666 | break; |
| 2667 | var = vv; |
| 2668 | con += ii; |
| 2669 | } |
| 2670 | if (j == -1) |
| 2671 | /* no earlier binding for var .. ill-formed IR */ |
| 2672 | vpanic("collapseChain"); |
| 2673 | |
| 2674 | /* so, did we find anything interesting? */ |
| 2675 | if (var == tmp) |
| 2676 | return False; /* no .. */ |
| 2677 | |
| 2678 | *tmp2 = var; |
| 2679 | *i32 = con; |
| 2680 | return True; |
| 2681 | } |
| 2682 | |
| 2683 | |
| 2684 | /* ------- Main function for Add32/Sub32 chain collapsing ------ */ |
| 2685 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2686 | static void collapse_AddSub_chains_BB ( IRSB* bb ) |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 2687 | { |
| 2688 | IRStmt *st; |
| 2689 | IRTemp var, var2; |
| 2690 | Int i, con, con2; |
| 2691 | |
| 2692 | for (i = bb->stmts_used-1; i >= 0; i--) { |
| 2693 | st = bb->stmts[i]; |
sewardj | 8bee6d1 | 2005-03-22 02:24:05 +0000 | [diff] [blame] | 2694 | if (st->tag == Ist_NoOp) |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 2695 | continue; |
| 2696 | |
| 2697 | /* Try to collapse 't1 = Add32/Sub32(t2, con)'. */ |
| 2698 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2699 | if (st->tag == Ist_WrTmp |
| 2700 | && isAdd32OrSub32(st->Ist.WrTmp.data, &var, &con)) { |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 2701 | |
| 2702 | /* So e1 is of the form Add32(var,con) or Sub32(var,-con). |
| 2703 | Find out if var can be expressed as var2 + con2. */ |
| 2704 | if (collapseChain(bb, i-1, var, &var2, &con2)) { |
| 2705 | if (DEBUG_IROPT) { |
| 2706 | vex_printf("replacing1 "); |
| 2707 | ppIRStmt(st); |
| 2708 | vex_printf(" with "); |
| 2709 | } |
| 2710 | con2 += con; |
| 2711 | bb->stmts[i] |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2712 | = IRStmt_WrTmp( |
| 2713 | st->Ist.WrTmp.tmp, |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 2714 | (con2 >= 0) |
| 2715 | ? IRExpr_Binop(Iop_Add32, |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2716 | IRExpr_RdTmp(var2), |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 2717 | IRExpr_Const(IRConst_U32(con2))) |
| 2718 | : IRExpr_Binop(Iop_Sub32, |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2719 | IRExpr_RdTmp(var2), |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 2720 | IRExpr_Const(IRConst_U32(-con2))) |
| 2721 | ); |
| 2722 | if (DEBUG_IROPT) { |
| 2723 | ppIRStmt(bb->stmts[i]); |
| 2724 | vex_printf("\n"); |
| 2725 | } |
| 2726 | } |
| 2727 | |
| 2728 | continue; |
| 2729 | } |
| 2730 | |
| 2731 | /* Try to collapse 't1 = GetI[t2, con]'. */ |
| 2732 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2733 | if (st->tag == Ist_WrTmp |
| 2734 | && st->Ist.WrTmp.data->tag == Iex_GetI |
| 2735 | && st->Ist.WrTmp.data->Iex.GetI.ix->tag == Iex_RdTmp |
| 2736 | && collapseChain(bb, i-1, st->Ist.WrTmp.data->Iex.GetI.ix |
| 2737 | ->Iex.RdTmp.tmp, &var2, &con2)) { |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 2738 | if (DEBUG_IROPT) { |
| 2739 | vex_printf("replacing3 "); |
| 2740 | ppIRStmt(st); |
| 2741 | vex_printf(" with "); |
| 2742 | } |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2743 | con2 += st->Ist.WrTmp.data->Iex.GetI.bias; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 2744 | bb->stmts[i] |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2745 | = IRStmt_WrTmp( |
| 2746 | st->Ist.WrTmp.tmp, |
| 2747 | IRExpr_GetI(st->Ist.WrTmp.data->Iex.GetI.descr, |
| 2748 | IRExpr_RdTmp(var2), |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 2749 | con2)); |
| 2750 | if (DEBUG_IROPT) { |
| 2751 | ppIRStmt(bb->stmts[i]); |
| 2752 | vex_printf("\n"); |
| 2753 | } |
| 2754 | continue; |
| 2755 | } |
| 2756 | |
| 2757 | /* Perhaps st is PutI[t, con] ? */ |
| 2758 | |
| 2759 | if (st->tag == Ist_PutI |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2760 | && st->Ist.PutI.ix->tag == Iex_RdTmp |
| 2761 | && collapseChain(bb, i-1, st->Ist.PutI.ix->Iex.RdTmp.tmp, |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 2762 | &var2, &con2)) { |
| 2763 | if (DEBUG_IROPT) { |
| 2764 | vex_printf("replacing2 "); |
| 2765 | ppIRStmt(st); |
| 2766 | vex_printf(" with "); |
| 2767 | } |
| 2768 | con2 += st->Ist.PutI.bias; |
| 2769 | bb->stmts[i] |
| 2770 | = IRStmt_PutI(st->Ist.PutI.descr, |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2771 | IRExpr_RdTmp(var2), |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 2772 | con2, |
| 2773 | st->Ist.PutI.data); |
| 2774 | if (DEBUG_IROPT) { |
| 2775 | ppIRStmt(bb->stmts[i]); |
| 2776 | vex_printf("\n"); |
| 2777 | } |
| 2778 | continue; |
| 2779 | } |
| 2780 | |
| 2781 | } /* for */ |
| 2782 | } |
| 2783 | |
| 2784 | |
| 2785 | /*---------------------------------------------------------------*/ |
| 2786 | /*--- PutI/GetI transformations ---*/ |
| 2787 | /*---------------------------------------------------------------*/ |
| 2788 | |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 2789 | /* Given the parts (descr, tmp, bias) for a GetI, scan backwards from |
| 2790 | the given starting point to find, if any, a PutI which writes |
| 2791 | exactly the same piece of guest state, and so return the expression |
| 2792 | that the PutI writes. This is the core of PutI-GetI forwarding. */ |
| 2793 | |
| 2794 | static |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2795 | IRExpr* findPutI ( IRSB* bb, Int startHere, |
| 2796 | IRRegArray* descrG, IRExpr* ixG, Int biasG ) |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 2797 | { |
| 2798 | Int j; |
| 2799 | IRStmt* st; |
| 2800 | GSAliasing relation; |
| 2801 | |
| 2802 | if (0) { |
| 2803 | vex_printf("\nfindPutI "); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2804 | ppIRRegArray(descrG); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 2805 | vex_printf(" "); |
| 2806 | ppIRExpr(ixG); |
| 2807 | vex_printf(" %d\n", biasG); |
| 2808 | } |
| 2809 | |
| 2810 | /* Scan backwards in bb from startHere to find a suitable PutI |
| 2811 | binding for (descrG, ixG, biasG), if any. */ |
| 2812 | |
| 2813 | for (j = startHere; j >= 0; j--) { |
| 2814 | st = bb->stmts[j]; |
sewardj | 8bee6d1 | 2005-03-22 02:24:05 +0000 | [diff] [blame] | 2815 | if (st->tag == Ist_NoOp) |
| 2816 | continue; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 2817 | |
| 2818 | if (st->tag == Ist_Put) { |
| 2819 | /* Non-indexed Put. This can't give a binding, but we do |
| 2820 | need to check it doesn't invalidate the search by |
| 2821 | overlapping any part of the indexed guest state. */ |
| 2822 | |
| 2823 | relation |
| 2824 | = getAliasingRelation_IC( |
| 2825 | descrG, ixG, |
| 2826 | st->Ist.Put.offset, |
| 2827 | typeOfIRExpr(bb->tyenv,st->Ist.Put.data) ); |
| 2828 | |
| 2829 | if (relation == NoAlias) { |
| 2830 | /* we're OK; keep going */ |
| 2831 | continue; |
| 2832 | } else { |
| 2833 | /* relation == UnknownAlias || relation == ExactAlias */ |
| 2834 | /* If this assertion fails, we've found a Put which writes |
| 2835 | an area of guest state which is read by a GetI. Which |
| 2836 | is unlikely (although not per se wrong). */ |
| 2837 | vassert(relation != ExactAlias); |
| 2838 | /* This Put potentially writes guest state that the GetI |
| 2839 | reads; we must fail. */ |
| 2840 | return NULL; |
| 2841 | } |
| 2842 | } |
| 2843 | |
| 2844 | if (st->tag == Ist_PutI) { |
| 2845 | |
| 2846 | relation = getAliasingRelation_II( |
| 2847 | descrG, ixG, biasG, |
| 2848 | st->Ist.PutI.descr, |
| 2849 | st->Ist.PutI.ix, |
| 2850 | st->Ist.PutI.bias |
| 2851 | ); |
| 2852 | |
| 2853 | if (relation == NoAlias) { |
| 2854 | /* This PutI definitely doesn't overlap. Ignore it and |
| 2855 | keep going. */ |
| 2856 | continue; /* the for j loop */ |
| 2857 | } |
| 2858 | |
| 2859 | if (relation == UnknownAlias) { |
| 2860 | /* We don't know if this PutI writes to the same guest |
| 2861 | state that the GetI, or not. So we have to give up. */ |
| 2862 | return NULL; |
| 2863 | } |
| 2864 | |
| 2865 | /* Otherwise, we've found what we're looking for. */ |
| 2866 | vassert(relation == ExactAlias); |
| 2867 | return st->Ist.PutI.data; |
| 2868 | |
| 2869 | } /* if (st->tag == Ist_PutI) */ |
| 2870 | |
| 2871 | if (st->tag == Ist_Dirty) { |
| 2872 | /* Be conservative. If the dirty call has any guest effects at |
| 2873 | all, give up. We could do better -- only give up if there |
| 2874 | are any guest writes/modifies. */ |
| 2875 | if (st->Ist.Dirty.details->nFxState > 0) |
| 2876 | return NULL; |
| 2877 | } |
| 2878 | |
| 2879 | } /* for */ |
| 2880 | |
| 2881 | /* No valid replacement was found. */ |
| 2882 | return NULL; |
| 2883 | } |
| 2884 | |
| 2885 | |
| 2886 | |
| 2887 | /* Assuming pi is a PutI stmt, is s2 identical to it (in the sense |
| 2888 | that it writes exactly the same piece of guest state) ? Safe |
| 2889 | answer: False. */ |
| 2890 | |
| 2891 | static Bool identicalPutIs ( IRStmt* pi, IRStmt* s2 ) |
| 2892 | { |
| 2893 | vassert(pi->tag == Ist_PutI); |
| 2894 | if (s2->tag != Ist_PutI) |
| 2895 | return False; |
| 2896 | |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 2897 | return toBool( |
| 2898 | getAliasingRelation_II( |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 2899 | pi->Ist.PutI.descr, pi->Ist.PutI.ix, pi->Ist.PutI.bias, |
| 2900 | s2->Ist.PutI.descr, s2->Ist.PutI.ix, s2->Ist.PutI.bias |
| 2901 | ) |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 2902 | == ExactAlias |
| 2903 | ); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 2904 | } |
| 2905 | |
| 2906 | |
| 2907 | /* Assuming pi is a PutI stmt, is s2 a Get/GetI/Put/PutI which might |
| 2908 | overlap it? Safe answer: True. Note, we could do a lot better |
| 2909 | than this if needed. */ |
| 2910 | |
| 2911 | static |
| 2912 | Bool guestAccessWhichMightOverlapPutI ( |
| 2913 | IRTypeEnv* tyenv, IRStmt* pi, IRStmt* s2 |
| 2914 | ) |
| 2915 | { |
| 2916 | GSAliasing relation; |
| 2917 | UInt minoffP, maxoffP; |
| 2918 | |
| 2919 | vassert(pi->tag == Ist_PutI); |
| 2920 | getArrayBounds(pi->Ist.PutI.descr, &minoffP, &maxoffP); |
| 2921 | switch (s2->tag) { |
| 2922 | |
sewardj | d2445f6 | 2005-03-21 00:15:53 +0000 | [diff] [blame] | 2923 | case Ist_NoOp: |
sewardj | f168931 | 2005-03-16 18:19:10 +0000 | [diff] [blame] | 2924 | case Ist_IMark: |
| 2925 | return False; |
| 2926 | |
sewardj | bb3f52d | 2005-01-07 14:14:50 +0000 | [diff] [blame] | 2927 | case Ist_MFence: |
sewardj | 5a9ffab | 2005-05-12 17:55:01 +0000 | [diff] [blame] | 2928 | case Ist_AbiHint: |
| 2929 | /* just be paranoid ... these should be rare. */ |
sewardj | bb3f52d | 2005-01-07 14:14:50 +0000 | [diff] [blame] | 2930 | return True; |
| 2931 | |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 2932 | case Ist_Dirty: |
| 2933 | /* If the dirty call has any guest effects at all, give up. |
| 2934 | Probably could do better. */ |
| 2935 | if (s2->Ist.Dirty.details->nFxState > 0) |
| 2936 | return True; |
| 2937 | return False; |
| 2938 | |
| 2939 | case Ist_Put: |
sewardj | 496a58d | 2005-03-20 18:44:44 +0000 | [diff] [blame] | 2940 | vassert(isIRAtom(s2->Ist.Put.data)); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 2941 | relation |
| 2942 | = getAliasingRelation_IC( |
| 2943 | pi->Ist.PutI.descr, pi->Ist.PutI.ix, |
| 2944 | s2->Ist.Put.offset, |
| 2945 | typeOfIRExpr(tyenv,s2->Ist.Put.data) |
| 2946 | ); |
| 2947 | goto have_relation; |
| 2948 | |
| 2949 | case Ist_PutI: |
sewardj | 496a58d | 2005-03-20 18:44:44 +0000 | [diff] [blame] | 2950 | vassert(isIRAtom(s2->Ist.PutI.ix)); |
| 2951 | vassert(isIRAtom(s2->Ist.PutI.data)); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 2952 | relation |
| 2953 | = getAliasingRelation_II( |
| 2954 | pi->Ist.PutI.descr, pi->Ist.PutI.ix, pi->Ist.PutI.bias, |
| 2955 | s2->Ist.PutI.descr, s2->Ist.PutI.ix, s2->Ist.PutI.bias |
| 2956 | ); |
| 2957 | goto have_relation; |
| 2958 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2959 | case Ist_WrTmp: |
| 2960 | if (s2->Ist.WrTmp.data->tag == Iex_GetI) { |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 2961 | relation |
| 2962 | = getAliasingRelation_II( |
| 2963 | pi->Ist.PutI.descr, pi->Ist.PutI.ix, |
| 2964 | pi->Ist.PutI.bias, |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2965 | s2->Ist.WrTmp.data->Iex.GetI.descr, |
| 2966 | s2->Ist.WrTmp.data->Iex.GetI.ix, |
| 2967 | s2->Ist.WrTmp.data->Iex.GetI.bias |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 2968 | ); |
| 2969 | goto have_relation; |
| 2970 | } |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2971 | if (s2->Ist.WrTmp.data->tag == Iex_Get) { |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 2972 | relation |
| 2973 | = getAliasingRelation_IC( |
| 2974 | pi->Ist.PutI.descr, pi->Ist.PutI.ix, |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2975 | s2->Ist.WrTmp.data->Iex.Get.offset, |
| 2976 | s2->Ist.WrTmp.data->Iex.Get.ty |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 2977 | ); |
| 2978 | goto have_relation; |
| 2979 | } |
| 2980 | return False; |
| 2981 | |
sewardj | af1ceca | 2005-06-30 23:31:27 +0000 | [diff] [blame] | 2982 | case Ist_Store: |
| 2983 | vassert(isIRAtom(s2->Ist.Store.addr)); |
| 2984 | vassert(isIRAtom(s2->Ist.Store.data)); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 2985 | return False; |
| 2986 | |
| 2987 | default: |
| 2988 | vex_printf("\n"); ppIRStmt(s2); vex_printf("\n"); |
| 2989 | vpanic("guestAccessWhichMightOverlapPutI"); |
| 2990 | } |
| 2991 | |
| 2992 | have_relation: |
| 2993 | if (relation == NoAlias) |
| 2994 | return False; |
| 2995 | else |
| 2996 | return True; /* ExactAlias or UnknownAlias */ |
| 2997 | } |
| 2998 | |
| 2999 | |
| 3000 | |
| 3001 | /* ---------- PutI/GetI transformations main functions --------- */ |
| 3002 | |
| 3003 | /* Remove redundant GetIs, to the extent that they can be detected. |
| 3004 | bb is modified in-place. */ |
| 3005 | |
| 3006 | static |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3007 | void do_redundant_GetI_elimination ( IRSB* bb ) |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3008 | { |
| 3009 | Int i; |
| 3010 | IRStmt* st; |
| 3011 | |
| 3012 | for (i = bb->stmts_used-1; i >= 0; i--) { |
| 3013 | st = bb->stmts[i]; |
sewardj | 8bee6d1 | 2005-03-22 02:24:05 +0000 | [diff] [blame] | 3014 | if (st->tag == Ist_NoOp) |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3015 | continue; |
| 3016 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3017 | if (st->tag == Ist_WrTmp |
| 3018 | && st->Ist.WrTmp.data->tag == Iex_GetI |
| 3019 | && st->Ist.WrTmp.data->Iex.GetI.ix->tag == Iex_RdTmp) { |
| 3020 | IRRegArray* descr = st->Ist.WrTmp.data->Iex.GetI.descr; |
| 3021 | IRExpr* ix = st->Ist.WrTmp.data->Iex.GetI.ix; |
| 3022 | Int bias = st->Ist.WrTmp.data->Iex.GetI.bias; |
| 3023 | IRExpr* replacement = findPutI(bb, i-1, descr, ix, bias); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3024 | if (replacement |
sewardj | 496a58d | 2005-03-20 18:44:44 +0000 | [diff] [blame] | 3025 | && isIRAtom(replacement) |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3026 | /* Make sure we're doing a type-safe transformation! */ |
| 3027 | && typeOfIRExpr(bb->tyenv, replacement) == descr->elemTy) { |
| 3028 | if (DEBUG_IROPT) { |
| 3029 | vex_printf("rGI: "); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3030 | ppIRExpr(st->Ist.WrTmp.data); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3031 | vex_printf(" -> "); |
| 3032 | ppIRExpr(replacement); |
| 3033 | vex_printf("\n"); |
| 3034 | } |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3035 | bb->stmts[i] = IRStmt_WrTmp(st->Ist.WrTmp.tmp, replacement); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3036 | } |
| 3037 | } |
| 3038 | } |
| 3039 | |
| 3040 | } |
| 3041 | |
| 3042 | |
| 3043 | /* Remove redundant PutIs, to the extent which they can be detected. |
| 3044 | bb is modified in-place. */ |
| 3045 | |
| 3046 | static |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3047 | void do_redundant_PutI_elimination ( IRSB* bb ) |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3048 | { |
| 3049 | Int i, j; |
| 3050 | Bool delete; |
| 3051 | IRStmt *st, *stj; |
| 3052 | |
| 3053 | for (i = 0; i < bb->stmts_used; i++) { |
| 3054 | st = bb->stmts[i]; |
sewardj | 8bee6d1 | 2005-03-22 02:24:05 +0000 | [diff] [blame] | 3055 | if (st->tag != Ist_PutI) |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3056 | continue; |
| 3057 | /* Ok, search forwards from here to see if we can find another |
| 3058 | PutI which makes this one redundant, and dodging various |
| 3059 | hazards. Search forwards: |
| 3060 | * If conditional exit, give up (because anything after that |
| 3061 | does not postdominate this put). |
| 3062 | * If a Get which might overlap, give up (because this PutI |
| 3063 | not necessarily dead). |
| 3064 | * If a Put which is identical, stop with success. |
| 3065 | * If a Put which might overlap, but is not identical, give up. |
| 3066 | * If a dirty helper call which might write guest state, give up. |
| 3067 | * If a Put which definitely doesn't overlap, or any other |
| 3068 | kind of stmt, continue. |
| 3069 | */ |
| 3070 | delete = False; |
| 3071 | for (j = i+1; j < bb->stmts_used; j++) { |
| 3072 | stj = bb->stmts[j]; |
sewardj | 8bee6d1 | 2005-03-22 02:24:05 +0000 | [diff] [blame] | 3073 | if (stj->tag == Ist_NoOp) |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3074 | continue; |
| 3075 | if (identicalPutIs(st, stj)) { |
| 3076 | /* success! */ |
| 3077 | delete = True; |
| 3078 | break; |
| 3079 | } |
| 3080 | if (stj->tag == Ist_Exit) |
| 3081 | /* give up */ |
| 3082 | break; |
| 3083 | if (st->tag == Ist_Dirty) |
| 3084 | /* give up; could do better here */ |
| 3085 | break; |
| 3086 | if (guestAccessWhichMightOverlapPutI(bb->tyenv, st, stj)) |
| 3087 | /* give up */ |
| 3088 | break; |
| 3089 | } |
| 3090 | |
| 3091 | if (delete) { |
| 3092 | if (DEBUG_IROPT) { |
| 3093 | vex_printf("rPI: "); |
| 3094 | ppIRStmt(st); |
| 3095 | vex_printf("\n"); |
| 3096 | } |
sewardj | d2445f6 | 2005-03-21 00:15:53 +0000 | [diff] [blame] | 3097 | bb->stmts[i] = IRStmt_NoOp(); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3098 | } |
| 3099 | |
| 3100 | } |
| 3101 | } |
| 3102 | |
| 3103 | |
| 3104 | /*---------------------------------------------------------------*/ |
| 3105 | /*--- Loop unrolling ---*/ |
| 3106 | /*---------------------------------------------------------------*/ |
| 3107 | |
| 3108 | /* Adjust all tmp values (names) in e by delta. e is destructively |
| 3109 | modified. */ |
| 3110 | |
| 3111 | static void deltaIRExpr ( IRExpr* e, Int delta ) |
| 3112 | { |
| 3113 | Int i; |
| 3114 | switch (e->tag) { |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3115 | case Iex_RdTmp: |
| 3116 | e->Iex.RdTmp.tmp += delta; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3117 | break; |
| 3118 | case Iex_Get: |
| 3119 | case Iex_Const: |
| 3120 | break; |
| 3121 | case Iex_GetI: |
| 3122 | deltaIRExpr(e->Iex.GetI.ix, delta); |
| 3123 | break; |
sewardj | 1a866b4 | 2006-02-09 02:54:03 +0000 | [diff] [blame] | 3124 | case Iex_Qop: |
| 3125 | deltaIRExpr(e->Iex.Qop.arg1, delta); |
| 3126 | deltaIRExpr(e->Iex.Qop.arg2, delta); |
| 3127 | deltaIRExpr(e->Iex.Qop.arg3, delta); |
| 3128 | deltaIRExpr(e->Iex.Qop.arg4, delta); |
| 3129 | break; |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 3130 | case Iex_Triop: |
| 3131 | deltaIRExpr(e->Iex.Triop.arg1, delta); |
| 3132 | deltaIRExpr(e->Iex.Triop.arg2, delta); |
| 3133 | deltaIRExpr(e->Iex.Triop.arg3, delta); |
| 3134 | break; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3135 | case Iex_Binop: |
| 3136 | deltaIRExpr(e->Iex.Binop.arg1, delta); |
| 3137 | deltaIRExpr(e->Iex.Binop.arg2, delta); |
| 3138 | break; |
| 3139 | case Iex_Unop: |
| 3140 | deltaIRExpr(e->Iex.Unop.arg, delta); |
| 3141 | break; |
sewardj | af1ceca | 2005-06-30 23:31:27 +0000 | [diff] [blame] | 3142 | case Iex_Load: |
| 3143 | deltaIRExpr(e->Iex.Load.addr, delta); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3144 | break; |
| 3145 | case Iex_CCall: |
| 3146 | for (i = 0; e->Iex.CCall.args[i]; i++) |
| 3147 | deltaIRExpr(e->Iex.CCall.args[i], delta); |
| 3148 | break; |
| 3149 | case Iex_Mux0X: |
| 3150 | deltaIRExpr(e->Iex.Mux0X.cond, delta); |
| 3151 | deltaIRExpr(e->Iex.Mux0X.expr0, delta); |
| 3152 | deltaIRExpr(e->Iex.Mux0X.exprX, delta); |
| 3153 | break; |
| 3154 | default: |
| 3155 | vex_printf("\n"); ppIRExpr(e); vex_printf("\n"); |
| 3156 | vpanic("deltaIRExpr"); |
| 3157 | } |
| 3158 | } |
| 3159 | |
| 3160 | /* Adjust all tmp values (names) in st by delta. st is destructively |
| 3161 | modified. */ |
| 3162 | |
| 3163 | static void deltaIRStmt ( IRStmt* st, Int delta ) |
| 3164 | { |
| 3165 | Int i; |
| 3166 | IRDirty* d; |
| 3167 | switch (st->tag) { |
sewardj | d2445f6 | 2005-03-21 00:15:53 +0000 | [diff] [blame] | 3168 | case Ist_NoOp: |
sewardj | f168931 | 2005-03-16 18:19:10 +0000 | [diff] [blame] | 3169 | case Ist_IMark: |
sewardj | f486035 | 2005-06-30 13:38:38 +0000 | [diff] [blame] | 3170 | case Ist_MFence: |
sewardj | f168931 | 2005-03-16 18:19:10 +0000 | [diff] [blame] | 3171 | break; |
sewardj | 5a9ffab | 2005-05-12 17:55:01 +0000 | [diff] [blame] | 3172 | case Ist_AbiHint: |
| 3173 | deltaIRExpr(st->Ist.AbiHint.base, delta); |
| 3174 | break; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3175 | case Ist_Put: |
| 3176 | deltaIRExpr(st->Ist.Put.data, delta); |
| 3177 | break; |
| 3178 | case Ist_PutI: |
| 3179 | deltaIRExpr(st->Ist.PutI.ix, delta); |
| 3180 | deltaIRExpr(st->Ist.PutI.data, delta); |
| 3181 | break; |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3182 | case Ist_WrTmp: |
| 3183 | st->Ist.WrTmp.tmp += delta; |
| 3184 | deltaIRExpr(st->Ist.WrTmp.data, delta); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3185 | break; |
| 3186 | case Ist_Exit: |
| 3187 | deltaIRExpr(st->Ist.Exit.guard, delta); |
| 3188 | break; |
sewardj | af1ceca | 2005-06-30 23:31:27 +0000 | [diff] [blame] | 3189 | case Ist_Store: |
| 3190 | deltaIRExpr(st->Ist.Store.addr, delta); |
| 3191 | deltaIRExpr(st->Ist.Store.data, delta); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3192 | break; |
| 3193 | case Ist_Dirty: |
| 3194 | d = st->Ist.Dirty.details; |
| 3195 | deltaIRExpr(d->guard, delta); |
| 3196 | for (i = 0; d->args[i]; i++) |
| 3197 | deltaIRExpr(d->args[i], delta); |
| 3198 | if (d->tmp != IRTemp_INVALID) |
| 3199 | d->tmp += delta; |
| 3200 | if (d->mAddr) |
| 3201 | deltaIRExpr(d->mAddr, delta); |
| 3202 | break; |
| 3203 | default: |
| 3204 | vex_printf("\n"); ppIRStmt(st); vex_printf("\n"); |
| 3205 | vpanic("deltaIRStmt"); |
| 3206 | } |
| 3207 | } |
| 3208 | |
| 3209 | |
| 3210 | /* If possible, return a loop-unrolled version of bb0. The original |
| 3211 | is changed. If not possible, return NULL. */ |
| 3212 | |
| 3213 | /* The two schemas considered are: |
| 3214 | |
| 3215 | X: BODY; goto X |
| 3216 | |
| 3217 | which unrolls to (eg) X: BODY;BODY; goto X |
| 3218 | |
| 3219 | and |
| 3220 | |
| 3221 | X: BODY; if (c) goto X; goto Y |
| 3222 | which trivially transforms to |
| 3223 | X: BODY; if (!c) goto Y; goto X; |
| 3224 | so it falls in the scope of the first case. |
| 3225 | |
| 3226 | X and Y must be literal (guest) addresses. |
| 3227 | */ |
| 3228 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3229 | static Int calc_unroll_factor( IRSB* bb ) |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3230 | { |
| 3231 | Int n_stmts, i; |
| 3232 | |
| 3233 | n_stmts = 0; |
sewardj | 0ddbf79 | 2005-04-04 23:12:54 +0000 | [diff] [blame] | 3234 | for (i = 0; i < bb->stmts_used; i++) { |
| 3235 | if (bb->stmts[i]->tag != Ist_NoOp) |
| 3236 | n_stmts++; |
| 3237 | } |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3238 | |
| 3239 | if (n_stmts <= vex_control.iropt_unroll_thresh/8) { |
| 3240 | if (vex_control.iropt_verbosity > 0) |
| 3241 | vex_printf("vex iropt: 8 x unrolling (%d sts -> %d sts)\n", |
| 3242 | n_stmts, 8* n_stmts); |
| 3243 | return 8; |
| 3244 | } |
| 3245 | if (n_stmts <= vex_control.iropt_unroll_thresh/4) { |
| 3246 | if (vex_control.iropt_verbosity > 0) |
| 3247 | vex_printf("vex iropt: 4 x unrolling (%d sts -> %d sts)\n", |
| 3248 | n_stmts, 4* n_stmts); |
| 3249 | return 4; |
| 3250 | } |
| 3251 | |
| 3252 | if (n_stmts <= vex_control.iropt_unroll_thresh/2) { |
| 3253 | if (vex_control.iropt_verbosity > 0) |
| 3254 | vex_printf("vex iropt: 2 x unrolling (%d sts -> %d sts)\n", |
| 3255 | n_stmts, 2* n_stmts); |
| 3256 | return 2; |
| 3257 | } |
| 3258 | |
| 3259 | if (vex_control.iropt_verbosity > 0) |
| 3260 | vex_printf("vex iropt: not unrolling (%d sts)\n", n_stmts); |
| 3261 | |
| 3262 | return 1; |
| 3263 | } |
| 3264 | |
| 3265 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3266 | static IRSB* maybe_loop_unroll_BB ( IRSB* bb0, Addr64 my_addr ) |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3267 | { |
| 3268 | Int i, j, jmax, n_vars; |
| 3269 | Bool xxx_known; |
| 3270 | Addr64 xxx_value, yyy_value; |
| 3271 | IRExpr* udst; |
| 3272 | IRStmt* st; |
| 3273 | IRConst* con; |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3274 | IRSB *bb1, *bb2; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3275 | Int unroll_factor; |
| 3276 | |
| 3277 | if (vex_control.iropt_unroll_thresh <= 0) |
| 3278 | return NULL; |
| 3279 | |
| 3280 | /* First off, figure out if we can unroll this loop. Do this |
| 3281 | without modifying bb0. */ |
| 3282 | |
| 3283 | if (bb0->jumpkind != Ijk_Boring) |
| 3284 | return NULL; |
| 3285 | |
| 3286 | xxx_known = False; |
| 3287 | xxx_value = 0; |
| 3288 | |
| 3289 | /* Extract the next-guest address. If it isn't a literal, we |
| 3290 | have to give up. */ |
| 3291 | |
| 3292 | udst = bb0->next; |
| 3293 | if (udst->tag == Iex_Const |
| 3294 | && (udst->Iex.Const.con->tag == Ico_U32 |
| 3295 | || udst->Iex.Const.con->tag == Ico_U64)) { |
| 3296 | /* The BB ends in a jump to a literal location. */ |
| 3297 | xxx_known = True; |
| 3298 | xxx_value = udst->Iex.Const.con->tag == Ico_U64 |
| 3299 | ? udst->Iex.Const.con->Ico.U64 |
| 3300 | : (Addr64)(udst->Iex.Const.con->Ico.U32); |
| 3301 | } |
| 3302 | |
| 3303 | if (!xxx_known) |
| 3304 | return NULL; |
| 3305 | |
| 3306 | /* Now we know the BB ends to a jump to a literal location. If |
| 3307 | it's a jump to itself (viz, idiom #1), move directly to the |
| 3308 | unrolling stage, first cloning the bb so the original isn't |
| 3309 | modified. */ |
| 3310 | if (xxx_value == my_addr) { |
| 3311 | unroll_factor = calc_unroll_factor( bb0 ); |
| 3312 | if (unroll_factor < 2) |
| 3313 | return NULL; |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3314 | bb1 = deepCopyIRSB( bb0 ); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3315 | bb0 = NULL; |
| 3316 | udst = NULL; /* is now invalid */ |
| 3317 | goto do_unroll; |
| 3318 | } |
| 3319 | |
| 3320 | /* Search for the second idiomatic form: |
| 3321 | X: BODY; if (c) goto X; goto Y |
| 3322 | We know Y, but need to establish that the last stmt |
| 3323 | is 'if (c) goto X'. |
| 3324 | */ |
| 3325 | yyy_value = xxx_value; |
| 3326 | for (i = bb0->stmts_used-1; i >= 0; i--) |
| 3327 | if (bb0->stmts[i]) |
| 3328 | break; |
| 3329 | |
| 3330 | if (i < 0) |
| 3331 | return NULL; /* block with no stmts. Strange. */ |
| 3332 | |
| 3333 | st = bb0->stmts[i]; |
| 3334 | if (st->tag != Ist_Exit) |
| 3335 | return NULL; |
| 3336 | if (st->Ist.Exit.jk != Ijk_Boring) |
| 3337 | return NULL; |
| 3338 | |
| 3339 | con = st->Ist.Exit.dst; |
| 3340 | vassert(con->tag == Ico_U32 || con->tag == Ico_U64); |
| 3341 | |
| 3342 | xxx_value = con->tag == Ico_U64 |
| 3343 | ? st->Ist.Exit.dst->Ico.U64 |
| 3344 | : (Addr64)(st->Ist.Exit.dst->Ico.U32); |
| 3345 | |
| 3346 | /* If this assertion fails, we have some kind of type error. */ |
| 3347 | vassert(con->tag == udst->Iex.Const.con->tag); |
| 3348 | |
| 3349 | if (xxx_value != my_addr) |
| 3350 | /* We didn't find either idiom. Give up. */ |
| 3351 | return NULL; |
| 3352 | |
| 3353 | /* Ok, we found idiom #2. Copy the BB, switch around the xxx and |
| 3354 | yyy values (which makes it look like idiom #1), and go into |
| 3355 | unrolling proper. This means finding (again) the last stmt, in |
| 3356 | the copied BB. */ |
| 3357 | |
| 3358 | unroll_factor = calc_unroll_factor( bb0 ); |
| 3359 | if (unroll_factor < 2) |
| 3360 | return NULL; |
| 3361 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3362 | bb1 = deepCopyIRSB( bb0 ); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3363 | bb0 = NULL; |
| 3364 | udst = NULL; /* is now invalid */ |
| 3365 | for (i = bb1->stmts_used-1; i >= 0; i--) |
| 3366 | if (bb1->stmts[i]) |
| 3367 | break; |
| 3368 | |
| 3369 | /* The next bunch of assertions should be true since we already |
| 3370 | found and checked the last stmt in the original bb. */ |
| 3371 | |
| 3372 | vassert(i >= 0); |
| 3373 | |
| 3374 | st = bb1->stmts[i]; |
| 3375 | vassert(st->tag == Ist_Exit); |
| 3376 | |
| 3377 | con = st->Ist.Exit.dst; |
| 3378 | vassert(con->tag == Ico_U32 || con->tag == Ico_U64); |
| 3379 | |
| 3380 | udst = bb1->next; |
| 3381 | vassert(udst->tag == Iex_Const); |
| 3382 | vassert(udst->Iex.Const.con->tag == Ico_U32 |
| 3383 | || udst->Iex.Const.con->tag == Ico_U64); |
| 3384 | vassert(con->tag == udst->Iex.Const.con->tag); |
| 3385 | |
| 3386 | /* switch the xxx and yyy fields around */ |
| 3387 | if (con->tag == Ico_U64) { |
| 3388 | udst->Iex.Const.con->Ico.U64 = xxx_value; |
| 3389 | con->Ico.U64 = yyy_value; |
| 3390 | } else { |
| 3391 | udst->Iex.Const.con->Ico.U32 = (UInt)xxx_value; |
cerion | 57b4c6d | 2005-02-22 11:07:35 +0000 | [diff] [blame] | 3392 | con->Ico.U32 = (UInt)yyy_value; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3393 | } |
| 3394 | |
| 3395 | /* negate the test condition */ |
| 3396 | st->Ist.Exit.guard |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3397 | = IRExpr_Unop(Iop_Not1,deepCopyIRExpr(st->Ist.Exit.guard)); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3398 | |
| 3399 | /* --- The unroller proper. Both idioms are by now --- */ |
| 3400 | /* --- now converted to idiom 1. --- */ |
| 3401 | |
| 3402 | do_unroll: |
| 3403 | |
| 3404 | vassert(unroll_factor == 2 |
| 3405 | || unroll_factor == 4 |
| 3406 | || unroll_factor == 8); |
| 3407 | |
| 3408 | jmax = unroll_factor==8 ? 3 : (unroll_factor==4 ? 2 : 1); |
| 3409 | for (j = 1; j <= jmax; j++) { |
| 3410 | |
| 3411 | n_vars = bb1->tyenv->types_used; |
| 3412 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3413 | bb2 = deepCopyIRSB(bb1); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3414 | for (i = 0; i < n_vars; i++) |
| 3415 | (void)newIRTemp(bb1->tyenv, bb2->tyenv->types[i]); |
| 3416 | |
| 3417 | for (i = 0; i < bb2->stmts_used; i++) { |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3418 | /* deltaIRStmt destructively modifies the stmt, but |
| 3419 | that's OK since bb2 is a complete fresh copy of bb1. */ |
| 3420 | deltaIRStmt(bb2->stmts[i], n_vars); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3421 | addStmtToIRSB(bb1, bb2->stmts[i]); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3422 | } |
| 3423 | } |
| 3424 | |
| 3425 | if (DEBUG_IROPT) { |
| 3426 | vex_printf("\nUNROLLED (%llx)\n", my_addr); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3427 | ppIRSB(bb1); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3428 | vex_printf("\n"); |
| 3429 | } |
| 3430 | |
| 3431 | /* Flattening; sigh. The unroller succeeds in breaking flatness |
| 3432 | by negating the test condition. This should be fixed properly. |
| 3433 | For the moment use this shotgun approach. */ |
| 3434 | return flatten_BB(bb1); |
| 3435 | } |
| 3436 | |
| 3437 | |
| 3438 | /*---------------------------------------------------------------*/ |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 3439 | /*--- The tree builder ---*/ |
| 3440 | /*---------------------------------------------------------------*/ |
| 3441 | |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3442 | /* This isn't part of IR optimisation. Really it's a pass done prior |
| 3443 | to instruction selection, which improves the code that the |
| 3444 | instruction selector can produce. */ |
| 3445 | |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 3446 | /* --- The 'tmp' environment is the central data structure here --- */ |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 3447 | |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 3448 | /* The number of outstanding bindings we're prepared to track. |
| 3449 | The number of times the env becomes full and we have to dump |
| 3450 | the oldest binding (hence reducing code quality) falls very |
| 3451 | rapidly as the env size increases. 8 gives reasonable performance |
| 3452 | under most circumstances. */ |
| 3453 | #define A_NENV 10 |
| 3454 | |
| 3455 | /* bindee == NULL === slot is not in use |
| 3456 | bindee != NULL === slot is in use |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 3457 | */ |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 3458 | typedef |
| 3459 | struct { |
| 3460 | IRTemp binder; |
| 3461 | IRExpr* bindee; |
| 3462 | Bool doesLoad; |
| 3463 | Bool doesGet; |
sewardj | 17442fe | 2004-09-20 14:54:28 +0000 | [diff] [blame] | 3464 | } |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 3465 | ATmpInfo; |
sewardj | 17442fe | 2004-09-20 14:54:28 +0000 | [diff] [blame] | 3466 | |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 3467 | __attribute__((unused)) |
| 3468 | static void ppAEnv ( ATmpInfo* env ) |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 3469 | { |
sewardj | 17442fe | 2004-09-20 14:54:28 +0000 | [diff] [blame] | 3470 | Int i; |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 3471 | for (i = 0; i < A_NENV; i++) { |
| 3472 | vex_printf("%d tmp %d val ", i, (Int)env[i].binder); |
| 3473 | if (env[i].bindee) |
| 3474 | ppIRExpr(env[i].bindee); |
| 3475 | else |
| 3476 | vex_printf("(null)"); |
| 3477 | vex_printf("\n"); |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 3478 | } |
| 3479 | } |
| 3480 | |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 3481 | /* --- Tree-traversal fns --- */ |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 3482 | |
sewardj | 37d3801 | 2004-08-24 00:37:04 +0000 | [diff] [blame] | 3483 | /* Traverse an expr, and detect if any part of it reads memory or does |
| 3484 | a Get. Be careful ... this really controls how much the |
| 3485 | tree-builder can reorder the code, so getting it right is critical. |
| 3486 | */ |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 3487 | static void setHints_Expr (Bool* doesLoad, Bool* doesGet, IRExpr* e ) |
| 3488 | { |
sewardj | c41f1fb | 2004-08-22 09:48:08 +0000 | [diff] [blame] | 3489 | Int i; |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 3490 | switch (e->tag) { |
sewardj | c41f1fb | 2004-08-22 09:48:08 +0000 | [diff] [blame] | 3491 | case Iex_CCall: |
| 3492 | for (i = 0; e->Iex.CCall.args[i]; i++) |
| 3493 | setHints_Expr(doesLoad, doesGet, e->Iex.CCall.args[i]); |
| 3494 | return; |
| 3495 | case Iex_Mux0X: |
| 3496 | setHints_Expr(doesLoad, doesGet, e->Iex.Mux0X.cond); |
| 3497 | setHints_Expr(doesLoad, doesGet, e->Iex.Mux0X.expr0); |
| 3498 | setHints_Expr(doesLoad, doesGet, e->Iex.Mux0X.exprX); |
| 3499 | return; |
sewardj | 40c8026 | 2006-02-08 19:30:46 +0000 | [diff] [blame] | 3500 | case Iex_Qop: |
| 3501 | setHints_Expr(doesLoad, doesGet, e->Iex.Qop.arg1); |
| 3502 | setHints_Expr(doesLoad, doesGet, e->Iex.Qop.arg2); |
| 3503 | setHints_Expr(doesLoad, doesGet, e->Iex.Qop.arg3); |
| 3504 | setHints_Expr(doesLoad, doesGet, e->Iex.Qop.arg4); |
| 3505 | return; |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 3506 | case Iex_Triop: |
| 3507 | setHints_Expr(doesLoad, doesGet, e->Iex.Triop.arg1); |
| 3508 | setHints_Expr(doesLoad, doesGet, e->Iex.Triop.arg2); |
| 3509 | setHints_Expr(doesLoad, doesGet, e->Iex.Triop.arg3); |
| 3510 | return; |
sewardj | c41f1fb | 2004-08-22 09:48:08 +0000 | [diff] [blame] | 3511 | case Iex_Binop: |
| 3512 | setHints_Expr(doesLoad, doesGet, e->Iex.Binop.arg1); |
| 3513 | setHints_Expr(doesLoad, doesGet, e->Iex.Binop.arg2); |
| 3514 | return; |
| 3515 | case Iex_Unop: |
| 3516 | setHints_Expr(doesLoad, doesGet, e->Iex.Unop.arg); |
| 3517 | return; |
sewardj | af1ceca | 2005-06-30 23:31:27 +0000 | [diff] [blame] | 3518 | case Iex_Load: |
sewardj | c9ad115 | 2004-10-14 00:08:21 +0000 | [diff] [blame] | 3519 | *doesLoad = True; |
sewardj | af1ceca | 2005-06-30 23:31:27 +0000 | [diff] [blame] | 3520 | setHints_Expr(doesLoad, doesGet, e->Iex.Load.addr); |
sewardj | c41f1fb | 2004-08-22 09:48:08 +0000 | [diff] [blame] | 3521 | return; |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 3522 | case Iex_Get: |
sewardj | c9ad115 | 2004-10-14 00:08:21 +0000 | [diff] [blame] | 3523 | *doesGet = True; |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 3524 | return; |
sewardj | f650199 | 2004-08-27 11:58:24 +0000 | [diff] [blame] | 3525 | case Iex_GetI: |
sewardj | c9ad115 | 2004-10-14 00:08:21 +0000 | [diff] [blame] | 3526 | *doesGet = True; |
sewardj | eeac841 | 2004-11-02 00:26:55 +0000 | [diff] [blame] | 3527 | setHints_Expr(doesLoad, doesGet, e->Iex.GetI.ix); |
sewardj | f650199 | 2004-08-27 11:58:24 +0000 | [diff] [blame] | 3528 | return; |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3529 | case Iex_RdTmp: |
sewardj | c41f1fb | 2004-08-22 09:48:08 +0000 | [diff] [blame] | 3530 | case Iex_Const: |
| 3531 | return; |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 3532 | default: |
| 3533 | vex_printf("\n"); ppIRExpr(e); vex_printf("\n"); |
| 3534 | vpanic("setHints_Expr"); |
| 3535 | } |
| 3536 | } |
| 3537 | |
| 3538 | |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 3539 | /* Add a binding to the front of the env and slide all the rest |
| 3540 | backwards. It should be the case that the last slot is free. */ |
| 3541 | static void addToEnvFront ( ATmpInfo* env, IRTemp binder, IRExpr* bindee ) |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 3542 | { |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 3543 | Int i; |
| 3544 | vassert(env[A_NENV-1].bindee == NULL); |
| 3545 | for (i = A_NENV-1; i >= 1; i--) |
| 3546 | env[i] = env[i-1]; |
| 3547 | env[0].binder = binder; |
| 3548 | env[0].bindee = bindee; |
| 3549 | env[0].doesLoad = False; /* filled in later */ |
| 3550 | env[0].doesGet = False; /* filled in later */ |
| 3551 | } |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 3552 | |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 3553 | /* Given uses :: array of UShort, indexed by IRTemp |
| 3554 | Add the use-occurrences of temps in this expression |
| 3555 | to the env. |
| 3556 | */ |
| 3557 | static void aoccCount_Expr ( UShort* uses, IRExpr* e ) |
| 3558 | { |
| 3559 | Int i; |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 3560 | |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 3561 | switch (e->tag) { |
| 3562 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3563 | case Iex_RdTmp: /* the only interesting case */ |
| 3564 | uses[e->Iex.RdTmp.tmp]++; |
sewardj | 8bee6d1 | 2005-03-22 02:24:05 +0000 | [diff] [blame] | 3565 | return; |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 3566 | |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 3567 | case Iex_Mux0X: |
| 3568 | aoccCount_Expr(uses, e->Iex.Mux0X.cond); |
| 3569 | aoccCount_Expr(uses, e->Iex.Mux0X.expr0); |
| 3570 | aoccCount_Expr(uses, e->Iex.Mux0X.exprX); |
| 3571 | return; |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 3572 | |
sewardj | 40c8026 | 2006-02-08 19:30:46 +0000 | [diff] [blame] | 3573 | case Iex_Qop: |
| 3574 | aoccCount_Expr(uses, e->Iex.Qop.arg1); |
| 3575 | aoccCount_Expr(uses, e->Iex.Qop.arg2); |
| 3576 | aoccCount_Expr(uses, e->Iex.Qop.arg3); |
| 3577 | aoccCount_Expr(uses, e->Iex.Qop.arg4); |
| 3578 | return; |
| 3579 | |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 3580 | case Iex_Triop: |
| 3581 | aoccCount_Expr(uses, e->Iex.Triop.arg1); |
| 3582 | aoccCount_Expr(uses, e->Iex.Triop.arg2); |
| 3583 | aoccCount_Expr(uses, e->Iex.Triop.arg3); |
| 3584 | return; |
| 3585 | |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 3586 | case Iex_Binop: |
| 3587 | aoccCount_Expr(uses, e->Iex.Binop.arg1); |
| 3588 | aoccCount_Expr(uses, e->Iex.Binop.arg2); |
| 3589 | return; |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 3590 | |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 3591 | case Iex_Unop: |
| 3592 | aoccCount_Expr(uses, e->Iex.Unop.arg); |
| 3593 | return; |
| 3594 | |
| 3595 | case Iex_Load: |
| 3596 | aoccCount_Expr(uses, e->Iex.Load.addr); |
| 3597 | return; |
| 3598 | |
| 3599 | case Iex_CCall: |
| 3600 | for (i = 0; e->Iex.CCall.args[i]; i++) |
| 3601 | aoccCount_Expr(uses, e->Iex.CCall.args[i]); |
| 3602 | return; |
| 3603 | |
| 3604 | case Iex_GetI: |
| 3605 | aoccCount_Expr(uses, e->Iex.GetI.ix); |
| 3606 | return; |
| 3607 | |
| 3608 | case Iex_Const: |
| 3609 | case Iex_Get: |
| 3610 | return; |
| 3611 | |
| 3612 | default: |
| 3613 | vex_printf("\n"); ppIRExpr(e); vex_printf("\n"); |
| 3614 | vpanic("aoccCount_Expr"); |
| 3615 | } |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 3616 | } |
| 3617 | |
| 3618 | |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 3619 | /* Given uses :: array of UShort, indexed by IRTemp |
| 3620 | Add the use-occurrences of temps in this statement |
| 3621 | to the env. |
| 3622 | */ |
| 3623 | static void aoccCount_Stmt ( UShort* uses, IRStmt* st ) |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 3624 | { |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 3625 | Int i; |
| 3626 | IRDirty* d; |
| 3627 | switch (st->tag) { |
| 3628 | case Ist_AbiHint: |
| 3629 | aoccCount_Expr(uses, st->Ist.AbiHint.base); |
| 3630 | return; |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3631 | case Ist_WrTmp: |
| 3632 | aoccCount_Expr(uses, st->Ist.WrTmp.data); |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 3633 | return; |
| 3634 | case Ist_Put: |
| 3635 | aoccCount_Expr(uses, st->Ist.Put.data); |
| 3636 | return; |
| 3637 | case Ist_PutI: |
| 3638 | aoccCount_Expr(uses, st->Ist.PutI.ix); |
| 3639 | aoccCount_Expr(uses, st->Ist.PutI.data); |
| 3640 | return; |
| 3641 | case Ist_Store: |
| 3642 | aoccCount_Expr(uses, st->Ist.Store.addr); |
| 3643 | aoccCount_Expr(uses, st->Ist.Store.data); |
| 3644 | return; |
| 3645 | case Ist_Dirty: |
| 3646 | d = st->Ist.Dirty.details; |
| 3647 | if (d->mFx != Ifx_None) |
| 3648 | aoccCount_Expr(uses, d->mAddr); |
| 3649 | aoccCount_Expr(uses, d->guard); |
| 3650 | for (i = 0; d->args[i]; i++) |
| 3651 | aoccCount_Expr(uses, d->args[i]); |
| 3652 | return; |
| 3653 | case Ist_NoOp: |
| 3654 | case Ist_IMark: |
| 3655 | case Ist_MFence: |
| 3656 | return; |
| 3657 | case Ist_Exit: |
| 3658 | aoccCount_Expr(uses, st->Ist.Exit.guard); |
| 3659 | return; |
| 3660 | default: |
| 3661 | vex_printf("\n"); ppIRStmt(st); vex_printf("\n"); |
| 3662 | vpanic("aoccCount_Stmt"); |
| 3663 | } |
| 3664 | } |
| 3665 | |
| 3666 | /* Look up a binding for tmp in the env. If found, return the bound |
| 3667 | expression, and set the env's binding to NULL so it is marked as |
| 3668 | used. If not found, return NULL. */ |
| 3669 | |
| 3670 | static IRExpr* atbSubst_Temp ( ATmpInfo* env, IRTemp tmp ) |
| 3671 | { |
| 3672 | Int i; |
| 3673 | for (i = 0; i < A_NENV; i++) { |
| 3674 | if (env[i].binder == tmp && env[i].bindee != NULL) { |
| 3675 | IRExpr* bindee = env[i].bindee; |
| 3676 | env[i].bindee = NULL; |
| 3677 | return bindee; |
| 3678 | } |
| 3679 | } |
| 3680 | return NULL; |
| 3681 | } |
| 3682 | |
| 3683 | /* Traverse e, looking for temps. For each observed temp, see if env |
| 3684 | contains a binding for the temp, and if so return the bound value. |
| 3685 | The env has the property that any binding it holds is |
| 3686 | 'single-shot', so once a binding is used, it is marked as no longer |
| 3687 | available, by setting its .bindee field to NULL. */ |
| 3688 | |
| 3689 | static IRExpr* atbSubst_Expr ( ATmpInfo* env, IRExpr* e ) |
| 3690 | { |
| 3691 | IRExpr* e2; |
| 3692 | IRExpr** args2; |
| 3693 | Int i; |
| 3694 | |
| 3695 | switch (e->tag) { |
| 3696 | |
| 3697 | case Iex_CCall: |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3698 | args2 = shallowCopyIRExprVec(e->Iex.CCall.args); |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 3699 | for (i = 0; args2[i]; i++) |
| 3700 | args2[i] = atbSubst_Expr(env,args2[i]); |
| 3701 | return IRExpr_CCall( |
| 3702 | e->Iex.CCall.cee, |
| 3703 | e->Iex.CCall.retty, |
| 3704 | args2 |
| 3705 | ); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3706 | case Iex_RdTmp: |
| 3707 | e2 = atbSubst_Temp(env, e->Iex.RdTmp.tmp); |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 3708 | return e2 ? e2 : e; |
| 3709 | case Iex_Mux0X: |
| 3710 | return IRExpr_Mux0X( |
| 3711 | atbSubst_Expr(env, e->Iex.Mux0X.cond), |
| 3712 | atbSubst_Expr(env, e->Iex.Mux0X.expr0), |
| 3713 | atbSubst_Expr(env, e->Iex.Mux0X.exprX) |
| 3714 | ); |
sewardj | 40c8026 | 2006-02-08 19:30:46 +0000 | [diff] [blame] | 3715 | case Iex_Qop: |
| 3716 | return IRExpr_Qop( |
| 3717 | e->Iex.Qop.op, |
| 3718 | atbSubst_Expr(env, e->Iex.Qop.arg1), |
| 3719 | atbSubst_Expr(env, e->Iex.Qop.arg2), |
| 3720 | atbSubst_Expr(env, e->Iex.Qop.arg3), |
| 3721 | atbSubst_Expr(env, e->Iex.Qop.arg4) |
| 3722 | ); |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 3723 | case Iex_Triop: |
| 3724 | return IRExpr_Triop( |
| 3725 | e->Iex.Triop.op, |
| 3726 | atbSubst_Expr(env, e->Iex.Triop.arg1), |
| 3727 | atbSubst_Expr(env, e->Iex.Triop.arg2), |
| 3728 | atbSubst_Expr(env, e->Iex.Triop.arg3) |
| 3729 | ); |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 3730 | case Iex_Binop: |
| 3731 | return IRExpr_Binop( |
| 3732 | e->Iex.Binop.op, |
| 3733 | atbSubst_Expr(env, e->Iex.Binop.arg1), |
| 3734 | atbSubst_Expr(env, e->Iex.Binop.arg2) |
| 3735 | ); |
| 3736 | case Iex_Unop: |
| 3737 | return IRExpr_Unop( |
| 3738 | e->Iex.Unop.op, |
| 3739 | atbSubst_Expr(env, e->Iex.Unop.arg) |
| 3740 | ); |
| 3741 | case Iex_Load: |
| 3742 | return IRExpr_Load( |
| 3743 | e->Iex.Load.end, |
| 3744 | e->Iex.Load.ty, |
| 3745 | atbSubst_Expr(env, e->Iex.Load.addr) |
| 3746 | ); |
| 3747 | case Iex_GetI: |
| 3748 | return IRExpr_GetI( |
| 3749 | e->Iex.GetI.descr, |
| 3750 | atbSubst_Expr(env, e->Iex.GetI.ix), |
| 3751 | e->Iex.GetI.bias |
| 3752 | ); |
| 3753 | case Iex_Const: |
| 3754 | case Iex_Get: |
| 3755 | return e; |
| 3756 | default: |
| 3757 | vex_printf("\n"); ppIRExpr(e); vex_printf("\n"); |
| 3758 | vpanic("atbSubst_Expr"); |
| 3759 | } |
| 3760 | } |
| 3761 | |
| 3762 | /* Same deal as atbSubst_Expr, except for stmts. */ |
| 3763 | |
| 3764 | static IRStmt* atbSubst_Stmt ( ATmpInfo* env, IRStmt* st ) |
| 3765 | { |
| 3766 | Int i; |
| 3767 | IRDirty* d; |
| 3768 | IRDirty* d2; |
| 3769 | switch (st->tag) { |
| 3770 | case Ist_AbiHint: |
| 3771 | return IRStmt_AbiHint( |
| 3772 | atbSubst_Expr(env, st->Ist.AbiHint.base), |
| 3773 | st->Ist.AbiHint.len |
| 3774 | ); |
| 3775 | case Ist_Store: |
| 3776 | return IRStmt_Store( |
| 3777 | st->Ist.Store.end, |
| 3778 | atbSubst_Expr(env, st->Ist.Store.addr), |
| 3779 | atbSubst_Expr(env, st->Ist.Store.data) |
| 3780 | ); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3781 | case Ist_WrTmp: |
| 3782 | return IRStmt_WrTmp( |
| 3783 | st->Ist.WrTmp.tmp, |
| 3784 | atbSubst_Expr(env, st->Ist.WrTmp.data) |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 3785 | ); |
| 3786 | case Ist_Put: |
| 3787 | return IRStmt_Put( |
| 3788 | st->Ist.Put.offset, |
| 3789 | atbSubst_Expr(env, st->Ist.Put.data) |
| 3790 | ); |
| 3791 | case Ist_PutI: |
| 3792 | return IRStmt_PutI( |
| 3793 | st->Ist.PutI.descr, |
| 3794 | atbSubst_Expr(env, st->Ist.PutI.ix), |
| 3795 | st->Ist.PutI.bias, |
| 3796 | atbSubst_Expr(env, st->Ist.PutI.data) |
| 3797 | ); |
| 3798 | |
| 3799 | case Ist_Exit: |
| 3800 | return IRStmt_Exit( |
| 3801 | atbSubst_Expr(env, st->Ist.Exit.guard), |
| 3802 | st->Ist.Exit.jk, |
| 3803 | st->Ist.Exit.dst |
| 3804 | ); |
| 3805 | case Ist_IMark: |
| 3806 | return IRStmt_IMark(st->Ist.IMark.addr, st->Ist.IMark.len); |
| 3807 | case Ist_NoOp: |
| 3808 | return IRStmt_NoOp(); |
| 3809 | case Ist_MFence: |
| 3810 | return IRStmt_MFence(); |
| 3811 | case Ist_Dirty: |
| 3812 | d = st->Ist.Dirty.details; |
| 3813 | d2 = emptyIRDirty(); |
| 3814 | *d2 = *d; |
| 3815 | if (d2->mFx != Ifx_None) |
| 3816 | d2->mAddr = atbSubst_Expr(env, d2->mAddr); |
| 3817 | d2->guard = atbSubst_Expr(env, d2->guard); |
| 3818 | for (i = 0; d2->args[i]; i++) |
| 3819 | d2->args[i] = atbSubst_Expr(env, d2->args[i]); |
| 3820 | return IRStmt_Dirty(d2); |
| 3821 | default: |
| 3822 | vex_printf("\n"); ppIRStmt(st); vex_printf("\n"); |
| 3823 | vpanic("atbSubst_Stmt"); |
| 3824 | } |
| 3825 | } |
| 3826 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3827 | /* notstatic */ void ado_treebuild_BB ( IRSB* bb ) |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 3828 | { |
| 3829 | Int i, j, k, m; |
| 3830 | Bool stmtPuts, stmtStores, invalidateMe; |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 3831 | IRStmt* st; |
| 3832 | IRStmt* st2; |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 3833 | ATmpInfo env[A_NENV]; |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 3834 | |
sewardj | c9ad115 | 2004-10-14 00:08:21 +0000 | [diff] [blame] | 3835 | Int n_tmps = bb->tyenv->types_used; |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 3836 | UShort* uses = LibVEX_Alloc(n_tmps * sizeof(UShort)); |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 3837 | |
| 3838 | /* Phase 1. Scan forwards in bb, counting use occurrences of each |
| 3839 | temp. Also count occurrences in the bb->next field. */ |
| 3840 | |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 3841 | for (i = 0; i < n_tmps; i++) |
| 3842 | uses[i] = 0; |
| 3843 | |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 3844 | for (i = 0; i < bb->stmts_used; i++) { |
| 3845 | st = bb->stmts[i]; |
sewardj | 8bee6d1 | 2005-03-22 02:24:05 +0000 | [diff] [blame] | 3846 | if (st->tag == Ist_NoOp) |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 3847 | continue; |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 3848 | aoccCount_Stmt( uses, st ); |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 3849 | } |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 3850 | aoccCount_Expr(uses, bb->next ); |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 3851 | |
sewardj | c9ad115 | 2004-10-14 00:08:21 +0000 | [diff] [blame] | 3852 | # if 0 |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 3853 | for (i = 0; i < n_tmps; i++) { |
| 3854 | if (uses[i] == 0) |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 3855 | continue; |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 3856 | ppIRTemp( (IRTemp)i ); |
| 3857 | vex_printf(" used %d\n", (Int)uses[i] ); |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 3858 | } |
sewardj | c9ad115 | 2004-10-14 00:08:21 +0000 | [diff] [blame] | 3859 | # endif |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 3860 | |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 3861 | /* Phase 2. Scan forwards in bb. For each statement in turn: |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 3862 | |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 3863 | If the env is full, emit the end element. This guarantees |
| 3864 | there is at least one free slot in the following. |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 3865 | |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 3866 | On seeing 't = E', occ(t)==1, |
| 3867 | let E'=env(E) |
| 3868 | delete this stmt |
| 3869 | add t -> E' to the front of the env |
| 3870 | Examine E' and set the hints for E' appropriately |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 3871 | (doesLoad? doesGet?) |
| 3872 | |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 3873 | On seeing any other stmt, |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 3874 | let stmt' = env(stmt) |
| 3875 | remove from env any 't=E' binds invalidated by stmt |
| 3876 | emit the invalidated stmts |
| 3877 | emit stmt' |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 3878 | compact any holes in env |
| 3879 | by sliding entries towards the front |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 3880 | |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 3881 | Finally, apply env to bb->next. |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 3882 | */ |
| 3883 | |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 3884 | for (i = 0; i < A_NENV; i++) { |
| 3885 | env[i].bindee = NULL; |
| 3886 | env[i].binder = IRTemp_INVALID; |
| 3887 | } |
| 3888 | |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 3889 | /* The stmts in bb are being reordered, and we are guaranteed to |
| 3890 | end up with no more than the number we started with. Use i to |
| 3891 | be the cursor of the current stmt examined and j <= i to be that |
| 3892 | for the current stmt being written. |
| 3893 | */ |
| 3894 | j = 0; |
| 3895 | for (i = 0; i < bb->stmts_used; i++) { |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 3896 | |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 3897 | st = bb->stmts[i]; |
sewardj | 8bee6d1 | 2005-03-22 02:24:05 +0000 | [diff] [blame] | 3898 | if (st->tag == Ist_NoOp) |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 3899 | continue; |
| 3900 | |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 3901 | /* Ensure there's at least one space in the env, by emitting |
| 3902 | the oldest binding if necessary. */ |
| 3903 | if (env[A_NENV-1].bindee != NULL) { |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3904 | bb->stmts[j] = IRStmt_WrTmp( env[A_NENV-1].binder, |
| 3905 | env[A_NENV-1].bindee ); |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 3906 | j++; |
| 3907 | vassert(j <= i); |
| 3908 | env[A_NENV-1].bindee = NULL; |
| 3909 | } |
| 3910 | |
| 3911 | /* Consider current stmt. */ |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3912 | if (st->tag == Ist_WrTmp && uses[st->Ist.WrTmp.tmp] <= 1) { |
sewardj | 6332740 | 2006-01-25 03:26:27 +0000 | [diff] [blame] | 3913 | IRExpr *e, *e2; |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 3914 | |
| 3915 | /* optional extra: dump dead bindings as we find them. |
| 3916 | Removes the need for a prior dead-code removal pass. */ |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3917 | if (uses[st->Ist.WrTmp.tmp] == 0) { |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 3918 | if (0) vex_printf("DEAD binding\n"); |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 3919 | continue; /* for (i = 0; i < bb->stmts_used; i++) loop */ |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 3920 | } |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3921 | vassert(uses[st->Ist.WrTmp.tmp] == 1); |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 3922 | |
| 3923 | /* ok, we have 't = E', occ(t)==1. Do the abovementioned |
| 3924 | actions. */ |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3925 | e = st->Ist.WrTmp.data; |
sewardj | 6332740 | 2006-01-25 03:26:27 +0000 | [diff] [blame] | 3926 | e2 = atbSubst_Expr(env, e); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3927 | addToEnvFront(env, st->Ist.WrTmp.tmp, e2); |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 3928 | setHints_Expr(&env[0].doesLoad, &env[0].doesGet, e2); |
| 3929 | /* don't advance j, as we are deleting this stmt and instead |
| 3930 | holding it temporarily in the env. */ |
| 3931 | continue; /* for (i = 0; i < bb->stmts_used; i++) loop */ |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 3932 | } |
| 3933 | |
| 3934 | /* we get here for any other kind of statement. */ |
| 3935 | /* 'use up' any bindings required by the current statement. */ |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 3936 | st2 = atbSubst_Stmt(env, st); |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 3937 | |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 3938 | /* Now, before this stmt, dump any bindings in env that it |
| 3939 | invalidates. These need to be dumped in the order in which |
| 3940 | they originally entered env -- that means from oldest to |
| 3941 | youngest. */ |
sewardj | 3e83893 | 2005-01-07 12:09:15 +0000 | [diff] [blame] | 3942 | |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 3943 | /* stmtPuts/stmtStores characterise what the stmt under |
| 3944 | consideration does. */ |
| 3945 | stmtPuts = toBool(st->tag == Ist_Put |
| 3946 | || st->tag == Ist_PutI |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 3947 | || st->tag == Ist_Dirty); |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 3948 | |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 3949 | stmtStores = toBool(st->tag == Ist_Store |
| 3950 | || st->tag == Ist_Dirty); |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 3951 | |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 3952 | for (k = A_NENV-1; k >= 0; k--) { |
| 3953 | if (env[k].bindee == NULL) |
| 3954 | continue; |
| 3955 | /* Compare the actions of this stmt with the actions of |
| 3956 | binding 'k', to see if they invalidate the binding. */ |
| 3957 | invalidateMe |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 3958 | = toBool( |
| 3959 | /* a store invalidates loaded data */ |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 3960 | (env[k].doesLoad && stmtStores) |
sewardj | 4c5f6d5 | 2004-10-26 13:25:33 +0000 | [diff] [blame] | 3961 | /* a put invalidates get'd data */ |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 3962 | || (env[k].doesGet && stmtPuts) |
sewardj | 4c5f6d5 | 2004-10-26 13:25:33 +0000 | [diff] [blame] | 3963 | /* a put invalidates loaded data. Note, we could do |
| 3964 | much better here in the sense that we only need to |
| 3965 | invalidate trees containing loads if the Put in |
| 3966 | question is marked as requiring precise |
| 3967 | exceptions. */ |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 3968 | || (env[k].doesLoad && stmtPuts) |
sewardj | 3e83893 | 2005-01-07 12:09:15 +0000 | [diff] [blame] | 3969 | /* probably overly conservative: a memory fence |
| 3970 | invalidates absolutely everything, so that all |
| 3971 | computation prior to it is forced to complete before |
| 3972 | proceeding with the fence. */ |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 3973 | || st->tag == Ist_MFence |
sewardj | 5a9ffab | 2005-05-12 17:55:01 +0000 | [diff] [blame] | 3974 | /* also be (probably overly) paranoid re AbiHints */ |
| 3975 | || st->tag == Ist_AbiHint |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 3976 | ); |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 3977 | if (invalidateMe) { |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3978 | bb->stmts[j] = IRStmt_WrTmp( env[k].binder, env[k].bindee ); |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 3979 | j++; |
| 3980 | vassert(j <= i); |
| 3981 | env[k].bindee = NULL; |
| 3982 | } |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 3983 | } |
| 3984 | |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 3985 | /* Slide in-use entries in env up to the front */ |
| 3986 | m = 0; |
| 3987 | for (k = 0; k < A_NENV; k++) { |
| 3988 | if (env[k].bindee != NULL) { |
| 3989 | env[m] = env[k]; |
| 3990 | m++; |
| 3991 | } |
| 3992 | } |
| 3993 | for (m = m; m < A_NENV; m++) { |
| 3994 | env[m].bindee = NULL; |
| 3995 | } |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 3996 | |
| 3997 | /* finally, emit the substituted statement */ |
| 3998 | bb->stmts[j] = st2; |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 3999 | /* vex_printf("**2 "); ppIRStmt(bb->stmts[j]); vex_printf("\n"); */ |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 4000 | j++; |
| 4001 | |
| 4002 | vassert(j <= i+1); |
| 4003 | } /* for each stmt in the original bb ... */ |
| 4004 | |
| 4005 | /* Finally ... substitute the ->next field as much as possible, and |
| 4006 | dump any left-over bindings. Hmm. Perhaps there should be no |
| 4007 | left over bindings? Or any left-over bindings are |
| 4008 | by definition dead? */ |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 4009 | bb->next = atbSubst_Expr(env, bb->next); |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 4010 | bb->stmts_used = j; |
| 4011 | } |
| 4012 | |
| 4013 | |
sewardj | 695cff9 | 2004-10-13 14:50:14 +0000 | [diff] [blame] | 4014 | /*---------------------------------------------------------------*/ |
sewardj | edf4d69 | 2004-08-17 13:52:58 +0000 | [diff] [blame] | 4015 | /*--- iropt main ---*/ |
| 4016 | /*---------------------------------------------------------------*/ |
| 4017 | |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 4018 | static Bool iropt_verbose = False; /* True; */ |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 4019 | |
| 4020 | |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 4021 | /* Do a simple cleanup pass on bb. This is: redundant Get removal, |
| 4022 | redundant Put removal, constant propagation, dead code removal, |
| 4023 | clean helper specialisation, and dead code removal (again). |
sewardj | b923075 | 2004-12-29 19:25:06 +0000 | [diff] [blame] | 4024 | */ |
sewardj | 695cff9 | 2004-10-13 14:50:14 +0000 | [diff] [blame] | 4025 | |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 4026 | |
| 4027 | static |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4028 | IRSB* cheap_transformations ( |
| 4029 | IRSB* bb, |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 4030 | IRExpr* (*specHelper) (HChar*, IRExpr**), |
sewardj | 8d2291c | 2004-10-25 14:50:21 +0000 | [diff] [blame] | 4031 | Bool (*preciseMemExnsFn)(Int,Int) |
| 4032 | ) |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 4033 | { |
| 4034 | redundant_get_removal_BB ( bb ); |
| 4035 | if (iropt_verbose) { |
| 4036 | vex_printf("\n========= REDUNDANT GET\n\n" ); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4037 | ppIRSB(bb); |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 4038 | } |
sewardj | 044a215 | 2004-10-21 10:22:10 +0000 | [diff] [blame] | 4039 | |
sewardj | 8d2291c | 2004-10-25 14:50:21 +0000 | [diff] [blame] | 4040 | redundant_put_removal_BB ( bb, preciseMemExnsFn ); |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 4041 | if (iropt_verbose) { |
| 4042 | vex_printf("\n========= REDUNDANT PUT\n\n" ); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4043 | ppIRSB(bb); |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 4044 | } |
sewardj | 044a215 | 2004-10-21 10:22:10 +0000 | [diff] [blame] | 4045 | |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 4046 | bb = cprop_BB ( bb ); |
| 4047 | if (iropt_verbose) { |
| 4048 | vex_printf("\n========= CPROPD\n\n" ); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4049 | ppIRSB(bb); |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 4050 | } |
| 4051 | |
sewardj | 49651f4 | 2004-10-28 22:11:04 +0000 | [diff] [blame] | 4052 | do_deadcode_BB ( bb ); |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 4053 | if (iropt_verbose) { |
| 4054 | vex_printf("\n========= DEAD\n\n" ); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4055 | ppIRSB(bb); |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 4056 | } |
| 4057 | |
sewardj | b923075 | 2004-12-29 19:25:06 +0000 | [diff] [blame] | 4058 | bb = spec_helpers_BB ( bb, specHelper ); |
sewardj | 49651f4 | 2004-10-28 22:11:04 +0000 | [diff] [blame] | 4059 | do_deadcode_BB ( bb ); |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 4060 | if (iropt_verbose) { |
| 4061 | vex_printf("\n========= SPECd \n\n" ); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4062 | ppIRSB(bb); |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 4063 | } |
| 4064 | |
| 4065 | return bb; |
| 4066 | } |
| 4067 | |
sewardj | 695cff9 | 2004-10-13 14:50:14 +0000 | [diff] [blame] | 4068 | |
| 4069 | /* Do some more expensive transformations on bb, which are aimed at |
| 4070 | optimising as much as possible in the presence of GetI and PutI. */ |
| 4071 | |
| 4072 | static |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4073 | IRSB* expensive_transformations( IRSB* bb ) |
sewardj | 695cff9 | 2004-10-13 14:50:14 +0000 | [diff] [blame] | 4074 | { |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 4075 | (void)do_cse_BB( bb ); |
sewardj | 695cff9 | 2004-10-13 14:50:14 +0000 | [diff] [blame] | 4076 | collapse_AddSub_chains_BB( bb ); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4077 | do_redundant_GetI_elimination( bb ); |
sewardj | 695cff9 | 2004-10-13 14:50:14 +0000 | [diff] [blame] | 4078 | do_redundant_PutI_elimination( bb ); |
sewardj | 49651f4 | 2004-10-28 22:11:04 +0000 | [diff] [blame] | 4079 | do_deadcode_BB( bb ); |
sewardj | b923075 | 2004-12-29 19:25:06 +0000 | [diff] [blame] | 4080 | return bb; |
sewardj | 695cff9 | 2004-10-13 14:50:14 +0000 | [diff] [blame] | 4081 | } |
| 4082 | |
| 4083 | |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 4084 | /* Scan a flattened BB to look for signs that more expensive |
| 4085 | optimisations might be useful: |
| 4086 | - find out if there are any GetIs and PutIs |
| 4087 | - find out if there are any floating or vector-typed temporaries |
| 4088 | */ |
sewardj | 695cff9 | 2004-10-13 14:50:14 +0000 | [diff] [blame] | 4089 | |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 4090 | static void considerExpensives ( /*OUT*/Bool* hasGetIorPutI, |
| 4091 | /*OUT*/Bool* hasVorFtemps, |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4092 | IRSB* bb ) |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 4093 | { |
| 4094 | Int i, j; |
| 4095 | IRStmt* st; |
| 4096 | IRDirty* d; |
| 4097 | |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 4098 | *hasGetIorPutI = False; |
| 4099 | *hasVorFtemps = False; |
| 4100 | |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 4101 | for (i = 0; i < bb->stmts_used; i++) { |
| 4102 | st = bb->stmts[i]; |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 4103 | switch (st->tag) { |
sewardj | 5a9ffab | 2005-05-12 17:55:01 +0000 | [diff] [blame] | 4104 | case Ist_AbiHint: |
| 4105 | vassert(isIRAtom(st->Ist.AbiHint.base)); |
| 4106 | break; |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 4107 | case Ist_PutI: |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 4108 | *hasGetIorPutI = True; |
| 4109 | break; |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4110 | case Ist_WrTmp: |
| 4111 | if (st->Ist.WrTmp.data->tag == Iex_GetI) |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 4112 | *hasGetIorPutI = True; |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4113 | switch (typeOfIRTemp(bb->tyenv, st->Ist.WrTmp.tmp)) { |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 4114 | case Ity_I1: case Ity_I8: case Ity_I16: |
| 4115 | case Ity_I32: case Ity_I64: case Ity_I128: |
| 4116 | break; |
| 4117 | case Ity_F32: case Ity_F64: case Ity_V128: |
| 4118 | *hasVorFtemps = True; |
| 4119 | break; |
| 4120 | default: |
| 4121 | goto bad; |
| 4122 | } |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 4123 | break; |
| 4124 | case Ist_Put: |
sewardj | 496a58d | 2005-03-20 18:44:44 +0000 | [diff] [blame] | 4125 | vassert(isIRAtom(st->Ist.Put.data)); |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 4126 | break; |
sewardj | af1ceca | 2005-06-30 23:31:27 +0000 | [diff] [blame] | 4127 | case Ist_Store: |
| 4128 | vassert(isIRAtom(st->Ist.Store.addr)); |
| 4129 | vassert(isIRAtom(st->Ist.Store.data)); |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 4130 | break; |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 4131 | case Ist_Dirty: |
| 4132 | d = st->Ist.Dirty.details; |
sewardj | 496a58d | 2005-03-20 18:44:44 +0000 | [diff] [blame] | 4133 | vassert(isIRAtom(d->guard)); |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 4134 | for (j = 0; d->args[j]; j++) |
sewardj | 496a58d | 2005-03-20 18:44:44 +0000 | [diff] [blame] | 4135 | vassert(isIRAtom(d->args[j])); |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 4136 | if (d->mFx != Ifx_None) |
sewardj | 496a58d | 2005-03-20 18:44:44 +0000 | [diff] [blame] | 4137 | vassert(isIRAtom(d->mAddr)); |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 4138 | break; |
sewardj | d2445f6 | 2005-03-21 00:15:53 +0000 | [diff] [blame] | 4139 | case Ist_NoOp: |
sewardj | f168931 | 2005-03-16 18:19:10 +0000 | [diff] [blame] | 4140 | case Ist_IMark: |
sewardj | 3e83893 | 2005-01-07 12:09:15 +0000 | [diff] [blame] | 4141 | case Ist_MFence: |
| 4142 | break; |
| 4143 | case Ist_Exit: |
sewardj | 496a58d | 2005-03-20 18:44:44 +0000 | [diff] [blame] | 4144 | vassert(isIRAtom(st->Ist.Exit.guard)); |
sewardj | 3e83893 | 2005-01-07 12:09:15 +0000 | [diff] [blame] | 4145 | break; |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 4146 | default: |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 4147 | bad: |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 4148 | ppIRStmt(st); |
| 4149 | vpanic("hasGetIorPutI"); |
| 4150 | } |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 4151 | } |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 4152 | } |
| 4153 | |
| 4154 | |
sewardj | 695cff9 | 2004-10-13 14:50:14 +0000 | [diff] [blame] | 4155 | /* ---------------- The main iropt entry point. ---------------- */ |
| 4156 | |
sewardj | edf4d69 | 2004-08-17 13:52:58 +0000 | [diff] [blame] | 4157 | /* exported from this file */ |
sewardj | 695cff9 | 2004-10-13 14:50:14 +0000 | [diff] [blame] | 4158 | /* Rules of the game: |
| 4159 | |
| 4160 | - IRExpr/IRStmt trees should be treated as immutable, as they |
| 4161 | may get shared. So never change a field of such a tree node; |
| 4162 | instead construct and return a new one if needed. |
| 4163 | */ |
| 4164 | |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 4165 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4166 | IRSB* do_iropt_BB ( IRSB* bb0, |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 4167 | IRExpr* (*specHelper) (HChar*, IRExpr**), |
sewardj | 8d2291c | 2004-10-25 14:50:21 +0000 | [diff] [blame] | 4168 | Bool (*preciseMemExnsFn)(Int,Int), |
sewardj | 695cff9 | 2004-10-13 14:50:14 +0000 | [diff] [blame] | 4169 | Addr64 guest_addr ) |
sewardj | edf4d69 | 2004-08-17 13:52:58 +0000 | [diff] [blame] | 4170 | { |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 4171 | static Int n_total = 0; |
| 4172 | static Int n_expensive = 0; |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 4173 | |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 4174 | Bool hasGetIorPutI, hasVorFtemps; |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4175 | IRSB *bb, *bb2; |
sewardj | 8c2c10b | 2004-10-16 20:51:52 +0000 | [diff] [blame] | 4176 | |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 4177 | n_total++; |
| 4178 | |
| 4179 | /* First flatten the block out, since all other |
| 4180 | phases assume flat code. */ |
| 4181 | |
| 4182 | bb = flatten_BB ( bb0 ); |
| 4183 | |
| 4184 | if (iropt_verbose) { |
| 4185 | vex_printf("\n========= FLAT\n\n" ); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4186 | ppIRSB(bb); |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 4187 | } |
sewardj | d721703 | 2004-08-19 10:49:10 +0000 | [diff] [blame] | 4188 | |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4189 | /* If at level 0, stop now. */ |
| 4190 | if (vex_control.iropt_level <= 0) return bb; |
| 4191 | |
sewardj | 695cff9 | 2004-10-13 14:50:14 +0000 | [diff] [blame] | 4192 | /* Now do a preliminary cleanup pass, and figure out if we also |
| 4193 | need to do 'expensive' optimisations. Expensive optimisations |
| 4194 | are deemed necessary if the block contains any GetIs or PutIs. |
| 4195 | If needed, do expensive transformations and then another cheap |
| 4196 | cleanup pass. */ |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 4197 | |
sewardj | 8d2291c | 2004-10-25 14:50:21 +0000 | [diff] [blame] | 4198 | bb = cheap_transformations( bb, specHelper, preciseMemExnsFn ); |
sewardj | 695cff9 | 2004-10-13 14:50:14 +0000 | [diff] [blame] | 4199 | |
sewardj | 0861374 | 2004-10-25 13:01:45 +0000 | [diff] [blame] | 4200 | if (vex_control.iropt_level > 1) { |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 4201 | |
| 4202 | /* Peer at what we have, to decide how much more effort to throw |
| 4203 | at it. */ |
| 4204 | considerExpensives( &hasGetIorPutI, &hasVorFtemps, bb ); |
| 4205 | |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 4206 | if (hasVorFtemps && !hasGetIorPutI) { |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 4207 | /* If any evidence of FP or Vector activity, CSE, as that |
| 4208 | tends to mop up all manner of lardy code to do with |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 4209 | rounding modes. Don't bother if hasGetIorPutI since that |
| 4210 | case leads into the expensive transformations, which do |
| 4211 | CSE anyway. */ |
| 4212 | (void)do_cse_BB( bb ); |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 4213 | do_deadcode_BB( bb ); |
| 4214 | } |
| 4215 | |
| 4216 | if (hasGetIorPutI) { |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 4217 | Bool cses; |
sewardj | 39555aa | 2004-10-24 22:29:19 +0000 | [diff] [blame] | 4218 | n_expensive++; |
sewardj | 39555aa | 2004-10-24 22:29:19 +0000 | [diff] [blame] | 4219 | if (DEBUG_IROPT) |
| 4220 | vex_printf("***** EXPENSIVE %d %d\n", n_total, n_expensive); |
sewardj | 695cff9 | 2004-10-13 14:50:14 +0000 | [diff] [blame] | 4221 | bb = expensive_transformations( bb ); |
sewardj | 8d2291c | 2004-10-25 14:50:21 +0000 | [diff] [blame] | 4222 | bb = cheap_transformations( bb, specHelper, preciseMemExnsFn ); |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 4223 | /* Potentially common up GetIs */ |
| 4224 | cses = do_cse_BB( bb ); |
| 4225 | if (cses) |
| 4226 | bb = cheap_transformations( bb, specHelper, preciseMemExnsFn ); |
sewardj | 695cff9 | 2004-10-13 14:50:14 +0000 | [diff] [blame] | 4227 | } |
sewardj | 39555aa | 2004-10-24 22:29:19 +0000 | [diff] [blame] | 4228 | |
| 4229 | /* Now have a go at unrolling simple (single-BB) loops. If |
| 4230 | successful, clean up the results as much as possible. */ |
| 4231 | |
| 4232 | bb2 = maybe_loop_unroll_BB( bb, guest_addr ); |
| 4233 | if (bb2) { |
sewardj | 8d2291c | 2004-10-25 14:50:21 +0000 | [diff] [blame] | 4234 | bb = cheap_transformations( bb2, specHelper, preciseMemExnsFn ); |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 4235 | if (hasGetIorPutI) { |
sewardj | 39555aa | 2004-10-24 22:29:19 +0000 | [diff] [blame] | 4236 | bb = expensive_transformations( bb ); |
sewardj | 8d2291c | 2004-10-25 14:50:21 +0000 | [diff] [blame] | 4237 | bb = cheap_transformations( bb, specHelper, preciseMemExnsFn ); |
sewardj | 39555aa | 2004-10-24 22:29:19 +0000 | [diff] [blame] | 4238 | } else { |
| 4239 | /* at least do CSE and dead code removal */ |
sewardj | fe1ccfc | 2004-11-11 02:14:45 +0000 | [diff] [blame] | 4240 | do_cse_BB( bb ); |
sewardj | 49651f4 | 2004-10-28 22:11:04 +0000 | [diff] [blame] | 4241 | do_deadcode_BB( bb ); |
sewardj | 39555aa | 2004-10-24 22:29:19 +0000 | [diff] [blame] | 4242 | } |
| 4243 | if (0) vex_printf("vex iropt: unrolled a loop\n"); |
| 4244 | } |
| 4245 | |
sewardj | d721703 | 2004-08-19 10:49:10 +0000 | [diff] [blame] | 4246 | } |
| 4247 | |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 4248 | return bb; |
sewardj | edf4d69 | 2004-08-17 13:52:58 +0000 | [diff] [blame] | 4249 | } |
| 4250 | |
| 4251 | |
sewardj | a1a370f | 2004-08-17 13:31:55 +0000 | [diff] [blame] | 4252 | /*---------------------------------------------------------------*/ |
| 4253 | /*--- end ir/iropt.c ---*/ |
| 4254 | /*---------------------------------------------------------------*/ |