florian | f6402ab | 2012-01-29 02:19:43 +0000 | [diff] [blame] | 1 | /* -*- mode: C; c-basic-offset: 3; -*- */ |
sewardj | a1a370f | 2004-08-17 13:31:55 +0000 | [diff] [blame] | 2 | |
| 3 | /*---------------------------------------------------------------*/ |
sewardj | 752f906 | 2010-05-03 21:38:49 +0000 | [diff] [blame] | 4 | /*--- begin ir_opt.c ---*/ |
sewardj | a1a370f | 2004-08-17 13:31:55 +0000 | [diff] [blame] | 5 | /*---------------------------------------------------------------*/ |
| 6 | |
sewardj | f8ed9d8 | 2004-11-12 17:40:23 +0000 | [diff] [blame] | 7 | /* |
sewardj | 752f906 | 2010-05-03 21:38:49 +0000 | [diff] [blame] | 8 | This file is part of Valgrind, a dynamic binary instrumentation |
| 9 | framework. |
sewardj | f8ed9d8 | 2004-11-12 17:40:23 +0000 | [diff] [blame] | 10 | |
sewardj | 89ae847 | 2013-10-18 14:12:58 +0000 | [diff] [blame] | 11 | Copyright (C) 2004-2013 OpenWorks LLP |
sewardj | 752f906 | 2010-05-03 21:38:49 +0000 | [diff] [blame] | 12 | info@open-works.net |
sewardj | f8ed9d8 | 2004-11-12 17:40:23 +0000 | [diff] [blame] | 13 | |
sewardj | 752f906 | 2010-05-03 21:38:49 +0000 | [diff] [blame] | 14 | This program is free software; you can redistribute it and/or |
| 15 | modify it under the terms of the GNU General Public License as |
| 16 | published by the Free Software Foundation; either version 2 of the |
| 17 | License, or (at your option) any later version. |
sewardj | f8ed9d8 | 2004-11-12 17:40:23 +0000 | [diff] [blame] | 18 | |
sewardj | 752f906 | 2010-05-03 21:38:49 +0000 | [diff] [blame] | 19 | This program is distributed in the hope that it will be useful, but |
| 20 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 22 | General Public License for more details. |
| 23 | |
| 24 | You should have received a copy of the GNU General Public License |
| 25 | along with this program; if not, write to the Free Software |
| 26 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
sewardj | 7bd6ffe | 2005-08-03 16:07:36 +0000 | [diff] [blame] | 27 | 02110-1301, USA. |
| 28 | |
sewardj | 752f906 | 2010-05-03 21:38:49 +0000 | [diff] [blame] | 29 | The GNU General Public License is contained in the file COPYING. |
sewardj | f8ed9d8 | 2004-11-12 17:40:23 +0000 | [diff] [blame] | 30 | |
| 31 | Neither the names of the U.S. Department of Energy nor the |
| 32 | University of California nor the names of its contributors may be |
| 33 | used to endorse or promote products derived from this software |
| 34 | without prior written permission. |
sewardj | f8ed9d8 | 2004-11-12 17:40:23 +0000 | [diff] [blame] | 35 | */ |
| 36 | |
sewardj | a1a370f | 2004-08-17 13:31:55 +0000 | [diff] [blame] | 37 | #include "libvex_basictypes.h" |
sewardj | edf4d69 | 2004-08-17 13:52:58 +0000 | [diff] [blame] | 38 | #include "libvex_ir.h" |
sewardj | a1a370f | 2004-08-17 13:31:55 +0000 | [diff] [blame] | 39 | #include "libvex.h" |
| 40 | |
sewardj | cef7d3e | 2009-07-02 12:21:59 +0000 | [diff] [blame] | 41 | #include "main_util.h" |
| 42 | #include "main_globals.h" |
| 43 | #include "ir_opt.h" |
sewardj | a1a370f | 2004-08-17 13:31:55 +0000 | [diff] [blame] | 44 | |
sewardj | d0863ff | 2004-10-23 00:22:32 +0000 | [diff] [blame] | 45 | |
sewardj | 088bcb4 | 2004-08-19 17:16:52 +0000 | [diff] [blame] | 46 | /* Set to 1 for lots of debugging output. */ |
| 47 | #define DEBUG_IROPT 0 |
| 48 | |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 49 | /* Set to 1 to gather some statistics. Currently only for sameIRExprs. */ |
| 50 | #define STATS_IROPT 0 |
| 51 | |
sewardj | a1a370f | 2004-08-17 13:31:55 +0000 | [diff] [blame] | 52 | |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 53 | /* What iropt does, 29 Dec 04. |
| 54 | |
sewardj | f6c8ebf | 2007-02-06 01:52:52 +0000 | [diff] [blame] | 55 | It takes an IRSB and produces a new one with the same meaning, |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 56 | defined thus: |
| 57 | |
| 58 | After execution of the new BB, all guest state and guest memory is |
| 59 | the same as after execution of the original. This is true |
| 60 | regardless of how the block was exited (at the end vs side exit). |
| 61 | |
| 62 | In addition, parts of the guest state will be identical to that |
| 63 | created by execution of the original at the following observation |
| 64 | points: |
| 65 | |
| 66 | * In a dirty helper call, any parts of the guest state that the |
| 67 | helper states that it reads or modifies will be up to date. |
| 68 | Also, guest memory will be up to date. Parts of the guest state |
| 69 | not marked as being read or modified by the helper cannot be |
| 70 | assumed to be up-to-date at the point where the helper is called. |
| 71 | |
philippe | 6c46bef | 2012-08-14 22:29:01 +0000 | [diff] [blame] | 72 | * If iropt_register_updates == VexRegUpdSpAtMemAccess : |
| 73 | The guest state is only up to date only as explained above |
| 74 | (i.e. at SB exits and as specified by dirty helper call). |
| 75 | Also, the stack pointer register is up to date at memory |
| 76 | exception points (as this is needed for the stack extension |
| 77 | logic in m_signals.c). |
| 78 | |
philippe | c8e2f98 | 2012-08-01 22:04:13 +0000 | [diff] [blame] | 79 | * If iropt_register_updates == VexRegUpdUnwindregsAtMemAccess : |
| 80 | Immediately prior to any load or store, those parts of the guest |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 81 | state marked as requiring precise exceptions will be up to date. |
| 82 | Also, guest memory will be up to date. Parts of the guest state |
| 83 | not marked as requiring precise exceptions cannot be assumed to |
| 84 | be up-to-date at the point of the load/store. |
| 85 | |
philippe | 6c46bef | 2012-08-14 22:29:01 +0000 | [diff] [blame] | 86 | * If iropt_register_updates == VexRegUpdAllregsAtMemAccess: |
philippe | c8e2f98 | 2012-08-01 22:04:13 +0000 | [diff] [blame] | 87 | Same as minimal, but all the guest state is up to date at memory |
| 88 | exception points. |
| 89 | |
philippe | 6c46bef | 2012-08-14 22:29:01 +0000 | [diff] [blame] | 90 | * If iropt_register_updates == VexRegUpdAllregsAtEachInsn : |
philippe | c8e2f98 | 2012-08-01 22:04:13 +0000 | [diff] [blame] | 91 | Guest state is up to date at each instruction. |
| 92 | |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 93 | The relative order of loads and stores (including loads/stores of |
| 94 | guest memory done by dirty helpers annotated as such) is not |
| 95 | changed. However, the relative order of loads with no intervening |
| 96 | stores/modifies may be changed. |
| 97 | |
| 98 | Transformation order |
| 99 | ~~~~~~~~~~~~~~~~~~~~ |
| 100 | |
| 101 | There are three levels of optimisation, controlled by |
| 102 | vex_control.iropt_level. Define first: |
| 103 | |
| 104 | "Cheap transformations" are the following sequence: |
| 105 | * Redundant-Get removal |
| 106 | * Redundant-Put removal |
| 107 | * Constant propagation/folding |
| 108 | * Dead code removal |
| 109 | * Specialisation of clean helper functions |
| 110 | * Dead code removal |
| 111 | |
| 112 | "Expensive transformations" are the following sequence: |
| 113 | * CSE |
| 114 | * Folding of add/sub chains |
| 115 | * Redundant-GetI removal |
| 116 | * Redundant-PutI removal |
| 117 | * Dead code removal |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 118 | |
| 119 | Then the transformations are as follows, as defined by |
| 120 | vex_control.iropt_level: |
| 121 | |
| 122 | Level 0: |
| 123 | * Flatten into atomic form. |
| 124 | |
| 125 | Level 1: the following sequence: |
| 126 | * Flatten into atomic form. |
| 127 | * Cheap transformations. |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 128 | |
| 129 | Level 2: the following sequence |
| 130 | * Flatten into atomic form. |
| 131 | * Cheap transformations. |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 132 | * If block contains any floating or vector types, CSE. |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 133 | * If block contains GetI or PutI, Expensive transformations. |
| 134 | * Try unrolling loops. Three possible outcomes: |
| 135 | - No effect: do nothing more. |
| 136 | - Unrolled a loop, and block does not contain GetI or PutI: |
| 137 | Do: * CSE |
| 138 | * Dead code removal |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 139 | - Unrolled a loop, and block contains GetI or PutI: |
| 140 | Do: * Expensive transformations |
| 141 | * Cheap transformations |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 142 | */ |
| 143 | |
sewardj | 9843029 | 2004-12-29 17:34:50 +0000 | [diff] [blame] | 144 | /* Implementation notes, 29 Dec 04. |
| 145 | |
| 146 | TODO (important): I think rPutI removal ignores precise exceptions |
| 147 | and is therefore in a sense, wrong. In the sense that PutIs are |
| 148 | assumed not to write parts of the guest state that we need to have |
| 149 | up-to-date at loads/stores. So far on x86 guest that has not |
| 150 | mattered since indeed only the x87 FP registers and tags are |
| 151 | accessed using GetI/PutI, and there is no need so far for them to |
| 152 | be up to date at mem exception points. The rPutI pass should be |
| 153 | fixed. |
sewardj | fb44d55 | 2004-10-25 09:48:47 +0000 | [diff] [blame] | 154 | |
sewardj | 4c5f6d5 | 2004-10-26 13:25:33 +0000 | [diff] [blame] | 155 | TODO: improve pessimistic handling of precise exceptions |
| 156 | in the tree builder. |
| 157 | |
sewardj | fb44d55 | 2004-10-25 09:48:47 +0000 | [diff] [blame] | 158 | TODO: check interaction of rGetI and dirty helpers. |
sewardj | c0b4228 | 2004-10-12 13:44:12 +0000 | [diff] [blame] | 159 | |
| 160 | F64i constants are treated differently from other constants. |
| 161 | They are not regarded as atoms, and instead lifted off and |
| 162 | bound to temps. This allows them to participate in CSE, which |
| 163 | is important for getting good performance for x86 guest code. |
sewardj | 695cff9 | 2004-10-13 14:50:14 +0000 | [diff] [blame] | 164 | |
sewardj | a5aa9cf | 2004-10-15 22:56:38 +0000 | [diff] [blame] | 165 | CSE up F64 literals (already doing F64is) |
sewardj | 4c5f6d5 | 2004-10-26 13:25:33 +0000 | [diff] [blame] | 166 | |
| 167 | CSE: consider carefully the requirement for precise exns |
sewardj | 9843029 | 2004-12-29 17:34:50 +0000 | [diff] [blame] | 168 | prior to making CSE any more aggressive. */ |
sewardj | c0b4228 | 2004-10-12 13:44:12 +0000 | [diff] [blame] | 169 | |
| 170 | |
sewardj | a1a370f | 2004-08-17 13:31:55 +0000 | [diff] [blame] | 171 | /*---------------------------------------------------------------*/ |
| 172 | /*--- Finite mappery, of a sort ---*/ |
| 173 | /*---------------------------------------------------------------*/ |
| 174 | |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 175 | /* General map from HWord-sized thing HWord-sized thing. Could be by |
| 176 | hashing, but it's not clear whether or not this would really be any |
| 177 | faster. */ |
sewardj | a1a370f | 2004-08-17 13:31:55 +0000 | [diff] [blame] | 178 | |
| 179 | typedef |
| 180 | struct { |
| 181 | Bool* inuse; |
sewardj | ea602bc | 2004-10-14 21:40:12 +0000 | [diff] [blame] | 182 | HWord* key; |
| 183 | HWord* val; |
sewardj | a1a370f | 2004-08-17 13:31:55 +0000 | [diff] [blame] | 184 | Int size; |
| 185 | Int used; |
| 186 | } |
sewardj | ea602bc | 2004-10-14 21:40:12 +0000 | [diff] [blame] | 187 | HashHW; |
sewardj | a1a370f | 2004-08-17 13:31:55 +0000 | [diff] [blame] | 188 | |
sewardj | ea602bc | 2004-10-14 21:40:12 +0000 | [diff] [blame] | 189 | static HashHW* newHHW ( void ) |
sewardj | a1a370f | 2004-08-17 13:31:55 +0000 | [diff] [blame] | 190 | { |
sewardj | ea602bc | 2004-10-14 21:40:12 +0000 | [diff] [blame] | 191 | HashHW* h = LibVEX_Alloc(sizeof(HashHW)); |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 192 | h->size = 8; |
sewardj | a1a370f | 2004-08-17 13:31:55 +0000 | [diff] [blame] | 193 | h->used = 0; |
| 194 | h->inuse = LibVEX_Alloc(h->size * sizeof(Bool)); |
sewardj | ea602bc | 2004-10-14 21:40:12 +0000 | [diff] [blame] | 195 | h->key = LibVEX_Alloc(h->size * sizeof(HWord)); |
| 196 | h->val = LibVEX_Alloc(h->size * sizeof(HWord)); |
sewardj | a1a370f | 2004-08-17 13:31:55 +0000 | [diff] [blame] | 197 | return h; |
| 198 | } |
| 199 | |
| 200 | |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 201 | /* Look up key in the map. */ |
sewardj | a1a370f | 2004-08-17 13:31:55 +0000 | [diff] [blame] | 202 | |
sewardj | ea602bc | 2004-10-14 21:40:12 +0000 | [diff] [blame] | 203 | static Bool lookupHHW ( HashHW* h, /*OUT*/HWord* val, HWord key ) |
sewardj | a1a370f | 2004-08-17 13:31:55 +0000 | [diff] [blame] | 204 | { |
| 205 | Int i; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 206 | /* vex_printf("lookupHHW(%llx)\n", key ); */ |
sewardj | a1a370f | 2004-08-17 13:31:55 +0000 | [diff] [blame] | 207 | for (i = 0; i < h->used; i++) { |
| 208 | if (h->inuse[i] && h->key[i] == key) { |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 209 | if (val) |
| 210 | *val = h->val[i]; |
sewardj | a1a370f | 2004-08-17 13:31:55 +0000 | [diff] [blame] | 211 | return True; |
| 212 | } |
| 213 | } |
| 214 | return False; |
| 215 | } |
| 216 | |
| 217 | |
sewardj | a1a370f | 2004-08-17 13:31:55 +0000 | [diff] [blame] | 218 | /* Add key->val to the map. Replaces any existing binding for key. */ |
| 219 | |
sewardj | ea602bc | 2004-10-14 21:40:12 +0000 | [diff] [blame] | 220 | static void addToHHW ( HashHW* h, HWord key, HWord val ) |
sewardj | a1a370f | 2004-08-17 13:31:55 +0000 | [diff] [blame] | 221 | { |
| 222 | Int i, j; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 223 | /* vex_printf("addToHHW(%llx, %llx)\n", key, val); */ |
sewardj | a1a370f | 2004-08-17 13:31:55 +0000 | [diff] [blame] | 224 | |
| 225 | /* Find and replace existing binding, if any. */ |
| 226 | for (i = 0; i < h->used; i++) { |
| 227 | if (h->inuse[i] && h->key[i] == key) { |
| 228 | h->val[i] = val; |
| 229 | return; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | /* Ensure a space is available. */ |
| 234 | if (h->used == h->size) { |
| 235 | /* Copy into arrays twice the size. */ |
| 236 | Bool* inuse2 = LibVEX_Alloc(2 * h->size * sizeof(Bool)); |
sewardj | ea602bc | 2004-10-14 21:40:12 +0000 | [diff] [blame] | 237 | HWord* key2 = LibVEX_Alloc(2 * h->size * sizeof(HWord)); |
| 238 | HWord* val2 = LibVEX_Alloc(2 * h->size * sizeof(HWord)); |
sewardj | a1a370f | 2004-08-17 13:31:55 +0000 | [diff] [blame] | 239 | for (i = j = 0; i < h->size; i++) { |
| 240 | if (!h->inuse[i]) continue; |
| 241 | inuse2[j] = True; |
| 242 | key2[j] = h->key[i]; |
| 243 | val2[j] = h->val[i]; |
| 244 | j++; |
| 245 | } |
| 246 | h->used = j; |
| 247 | h->size *= 2; |
| 248 | h->inuse = inuse2; |
| 249 | h->key = key2; |
| 250 | h->val = val2; |
| 251 | } |
| 252 | |
| 253 | /* Finally, add it. */ |
| 254 | vassert(h->used < h->size); |
| 255 | h->inuse[h->used] = True; |
| 256 | h->key[h->used] = key; |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 257 | h->val[h->used] = val; |
sewardj | a1a370f | 2004-08-17 13:31:55 +0000 | [diff] [blame] | 258 | h->used++; |
| 259 | } |
| 260 | |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 261 | |
sewardj | d7cb853 | 2004-08-17 23:59:23 +0000 | [diff] [blame] | 262 | /*---------------------------------------------------------------*/ |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 263 | /*--- Flattening out a BB into atomic SSA form ---*/ |
sewardj | d7cb853 | 2004-08-17 23:59:23 +0000 | [diff] [blame] | 264 | /*---------------------------------------------------------------*/ |
| 265 | |
sewardj | e80679a | 2004-09-21 23:00:11 +0000 | [diff] [blame] | 266 | /* Non-critical helper, heuristic for reducing the number of tmp-tmp |
| 267 | copies made by flattening. If in doubt return False. */ |
| 268 | |
| 269 | static Bool isFlat ( IRExpr* e ) |
| 270 | { |
sewardj | 695cff9 | 2004-10-13 14:50:14 +0000 | [diff] [blame] | 271 | if (e->tag == Iex_Get) |
| 272 | return True; |
sewardj | e80679a | 2004-09-21 23:00:11 +0000 | [diff] [blame] | 273 | if (e->tag == Iex_Binop) |
sewardj | 496a58d | 2005-03-20 18:44:44 +0000 | [diff] [blame] | 274 | return toBool( isIRAtom(e->Iex.Binop.arg1) |
| 275 | && isIRAtom(e->Iex.Binop.arg2) ); |
sewardj | af1ceca | 2005-06-30 23:31:27 +0000 | [diff] [blame] | 276 | if (e->tag == Iex_Load) |
| 277 | return isIRAtom(e->Iex.Load.addr); |
sewardj | e80679a | 2004-09-21 23:00:11 +0000 | [diff] [blame] | 278 | return False; |
| 279 | } |
| 280 | |
sewardj | d7cb853 | 2004-08-17 23:59:23 +0000 | [diff] [blame] | 281 | /* Flatten out 'ex' so it is atomic, returning a new expression with |
| 282 | the same value, after having appended extra IRTemp assignments to |
| 283 | the end of 'bb'. */ |
| 284 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 285 | static IRExpr* flatten_Expr ( IRSB* bb, IRExpr* ex ) |
sewardj | d7cb853 | 2004-08-17 23:59:23 +0000 | [diff] [blame] | 286 | { |
| 287 | Int i; |
| 288 | IRExpr** newargs; |
| 289 | IRType ty = typeOfIRExpr(bb->tyenv, ex); |
| 290 | IRTemp t1; |
| 291 | |
| 292 | switch (ex->tag) { |
| 293 | |
sewardj | d721703 | 2004-08-19 10:49:10 +0000 | [diff] [blame] | 294 | case Iex_GetI: |
| 295 | t1 = newIRTemp(bb->tyenv, ty); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 296 | addStmtToIRSB(bb, IRStmt_WrTmp(t1, |
sewardj | 2d3f77c | 2004-09-22 23:49:09 +0000 | [diff] [blame] | 297 | IRExpr_GetI(ex->Iex.GetI.descr, |
sewardj | eeac841 | 2004-11-02 00:26:55 +0000 | [diff] [blame] | 298 | flatten_Expr(bb, ex->Iex.GetI.ix), |
sewardj | 2d3f77c | 2004-09-22 23:49:09 +0000 | [diff] [blame] | 299 | ex->Iex.GetI.bias))); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 300 | return IRExpr_RdTmp(t1); |
sewardj | d721703 | 2004-08-19 10:49:10 +0000 | [diff] [blame] | 301 | |
sewardj | d7cb853 | 2004-08-17 23:59:23 +0000 | [diff] [blame] | 302 | case Iex_Get: |
| 303 | t1 = newIRTemp(bb->tyenv, ty); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 304 | addStmtToIRSB(bb, |
| 305 | IRStmt_WrTmp(t1, ex)); |
| 306 | return IRExpr_RdTmp(t1); |
sewardj | d7cb853 | 2004-08-17 23:59:23 +0000 | [diff] [blame] | 307 | |
florian | 96d7cc3 | 2012-06-01 20:41:24 +0000 | [diff] [blame] | 308 | case Iex_Qop: { |
| 309 | IRQop* qop = ex->Iex.Qop.details; |
sewardj | 40c8026 | 2006-02-08 19:30:46 +0000 | [diff] [blame] | 310 | t1 = newIRTemp(bb->tyenv, ty); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 311 | addStmtToIRSB(bb, IRStmt_WrTmp(t1, |
florian | 96d7cc3 | 2012-06-01 20:41:24 +0000 | [diff] [blame] | 312 | IRExpr_Qop(qop->op, |
| 313 | flatten_Expr(bb, qop->arg1), |
| 314 | flatten_Expr(bb, qop->arg2), |
| 315 | flatten_Expr(bb, qop->arg3), |
| 316 | flatten_Expr(bb, qop->arg4)))); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 317 | return IRExpr_RdTmp(t1); |
florian | 96d7cc3 | 2012-06-01 20:41:24 +0000 | [diff] [blame] | 318 | } |
sewardj | 40c8026 | 2006-02-08 19:30:46 +0000 | [diff] [blame] | 319 | |
florian | 420bfa9 | 2012-06-02 20:29:22 +0000 | [diff] [blame] | 320 | case Iex_Triop: { |
| 321 | IRTriop* triop = ex->Iex.Triop.details; |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 322 | t1 = newIRTemp(bb->tyenv, ty); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 323 | addStmtToIRSB(bb, IRStmt_WrTmp(t1, |
florian | 420bfa9 | 2012-06-02 20:29:22 +0000 | [diff] [blame] | 324 | IRExpr_Triop(triop->op, |
| 325 | flatten_Expr(bb, triop->arg1), |
| 326 | flatten_Expr(bb, triop->arg2), |
| 327 | flatten_Expr(bb, triop->arg3)))); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 328 | return IRExpr_RdTmp(t1); |
florian | 420bfa9 | 2012-06-02 20:29:22 +0000 | [diff] [blame] | 329 | } |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 330 | |
sewardj | d7cb853 | 2004-08-17 23:59:23 +0000 | [diff] [blame] | 331 | case Iex_Binop: |
| 332 | t1 = newIRTemp(bb->tyenv, ty); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 333 | addStmtToIRSB(bb, IRStmt_WrTmp(t1, |
sewardj | d7cb853 | 2004-08-17 23:59:23 +0000 | [diff] [blame] | 334 | IRExpr_Binop(ex->Iex.Binop.op, |
| 335 | flatten_Expr(bb, ex->Iex.Binop.arg1), |
| 336 | flatten_Expr(bb, ex->Iex.Binop.arg2)))); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 337 | return IRExpr_RdTmp(t1); |
sewardj | d7cb853 | 2004-08-17 23:59:23 +0000 | [diff] [blame] | 338 | |
| 339 | case Iex_Unop: |
| 340 | t1 = newIRTemp(bb->tyenv, ty); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 341 | addStmtToIRSB(bb, IRStmt_WrTmp(t1, |
sewardj | d7cb853 | 2004-08-17 23:59:23 +0000 | [diff] [blame] | 342 | IRExpr_Unop(ex->Iex.Unop.op, |
| 343 | flatten_Expr(bb, ex->Iex.Unop.arg)))); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 344 | return IRExpr_RdTmp(t1); |
sewardj | d7cb853 | 2004-08-17 23:59:23 +0000 | [diff] [blame] | 345 | |
sewardj | af1ceca | 2005-06-30 23:31:27 +0000 | [diff] [blame] | 346 | case Iex_Load: |
sewardj | d7cb853 | 2004-08-17 23:59:23 +0000 | [diff] [blame] | 347 | t1 = newIRTemp(bb->tyenv, ty); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 348 | addStmtToIRSB(bb, IRStmt_WrTmp(t1, |
sewardj | e768e92 | 2009-11-26 17:17:37 +0000 | [diff] [blame] | 349 | IRExpr_Load(ex->Iex.Load.end, |
sewardj | af1ceca | 2005-06-30 23:31:27 +0000 | [diff] [blame] | 350 | ex->Iex.Load.ty, |
| 351 | flatten_Expr(bb, ex->Iex.Load.addr)))); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 352 | return IRExpr_RdTmp(t1); |
sewardj | d7cb853 | 2004-08-17 23:59:23 +0000 | [diff] [blame] | 353 | |
| 354 | case Iex_CCall: |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 355 | newargs = shallowCopyIRExprVec(ex->Iex.CCall.args); |
sewardj | d7cb853 | 2004-08-17 23:59:23 +0000 | [diff] [blame] | 356 | for (i = 0; newargs[i]; i++) |
| 357 | newargs[i] = flatten_Expr(bb, newargs[i]); |
| 358 | t1 = newIRTemp(bb->tyenv, ty); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 359 | addStmtToIRSB(bb, IRStmt_WrTmp(t1, |
sewardj | 8ea867b | 2004-10-30 19:03:02 +0000 | [diff] [blame] | 360 | IRExpr_CCall(ex->Iex.CCall.cee, |
sewardj | d7cb853 | 2004-08-17 23:59:23 +0000 | [diff] [blame] | 361 | ex->Iex.CCall.retty, |
| 362 | newargs))); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 363 | return IRExpr_RdTmp(t1); |
sewardj | d7cb853 | 2004-08-17 23:59:23 +0000 | [diff] [blame] | 364 | |
florian | 99dd03e | 2013-01-29 03:56:06 +0000 | [diff] [blame] | 365 | case Iex_ITE: |
sewardj | d7cb853 | 2004-08-17 23:59:23 +0000 | [diff] [blame] | 366 | t1 = newIRTemp(bb->tyenv, ty); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 367 | addStmtToIRSB(bb, IRStmt_WrTmp(t1, |
florian | 99dd03e | 2013-01-29 03:56:06 +0000 | [diff] [blame] | 368 | IRExpr_ITE(flatten_Expr(bb, ex->Iex.ITE.cond), |
| 369 | flatten_Expr(bb, ex->Iex.ITE.iftrue), |
| 370 | flatten_Expr(bb, ex->Iex.ITE.iffalse)))); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 371 | return IRExpr_RdTmp(t1); |
sewardj | d7cb853 | 2004-08-17 23:59:23 +0000 | [diff] [blame] | 372 | |
| 373 | case Iex_Const: |
sewardj | c0b4228 | 2004-10-12 13:44:12 +0000 | [diff] [blame] | 374 | /* Lift F64i constants out onto temps so they can be CSEd |
| 375 | later. */ |
| 376 | if (ex->Iex.Const.con->tag == Ico_F64i) { |
| 377 | t1 = newIRTemp(bb->tyenv, ty); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 378 | addStmtToIRSB(bb, IRStmt_WrTmp(t1, |
sewardj | c0b4228 | 2004-10-12 13:44:12 +0000 | [diff] [blame] | 379 | IRExpr_Const(ex->Iex.Const.con))); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 380 | return IRExpr_RdTmp(t1); |
sewardj | c0b4228 | 2004-10-12 13:44:12 +0000 | [diff] [blame] | 381 | } else { |
| 382 | /* Leave all other constants alone. */ |
| 383 | return ex; |
| 384 | } |
| 385 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 386 | case Iex_RdTmp: |
sewardj | d7cb853 | 2004-08-17 23:59:23 +0000 | [diff] [blame] | 387 | return ex; |
| 388 | |
| 389 | default: |
| 390 | vex_printf("\n"); |
| 391 | ppIRExpr(ex); |
| 392 | vex_printf("\n"); |
| 393 | vpanic("flatten_Expr"); |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | |
| 398 | /* Append a completely flattened form of 'st' to the end of 'bb'. */ |
| 399 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 400 | static void flatten_Stmt ( IRSB* bb, IRStmt* st ) |
sewardj | d7cb853 | 2004-08-17 23:59:23 +0000 | [diff] [blame] | 401 | { |
sewardj | 17442fe | 2004-09-20 14:54:28 +0000 | [diff] [blame] | 402 | Int i; |
sewardj | cfe046e | 2013-01-17 14:23:53 +0000 | [diff] [blame] | 403 | IRExpr *e1, *e2, *e3, *e4, *e5; |
| 404 | IRDirty *d, *d2; |
| 405 | IRCAS *cas, *cas2; |
| 406 | IRPutI *puti, *puti2; |
| 407 | IRLoadG *lg; |
| 408 | IRStoreG *sg; |
sewardj | d7cb853 | 2004-08-17 23:59:23 +0000 | [diff] [blame] | 409 | switch (st->tag) { |
| 410 | case Ist_Put: |
sewardj | 496a58d | 2005-03-20 18:44:44 +0000 | [diff] [blame] | 411 | if (isIRAtom(st->Ist.Put.data)) { |
sewardj | 49651f4 | 2004-10-28 22:11:04 +0000 | [diff] [blame] | 412 | /* optimisation to reduce the amount of heap wasted |
| 413 | by the flattener */ |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 414 | addStmtToIRSB(bb, st); |
sewardj | 49651f4 | 2004-10-28 22:11:04 +0000 | [diff] [blame] | 415 | } else { |
| 416 | /* general case, always correct */ |
| 417 | e1 = flatten_Expr(bb, st->Ist.Put.data); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 418 | addStmtToIRSB(bb, IRStmt_Put(st->Ist.Put.offset, e1)); |
sewardj | 49651f4 | 2004-10-28 22:11:04 +0000 | [diff] [blame] | 419 | } |
sewardj | d7cb853 | 2004-08-17 23:59:23 +0000 | [diff] [blame] | 420 | break; |
sewardj | d7cb853 | 2004-08-17 23:59:23 +0000 | [diff] [blame] | 421 | case Ist_PutI: |
florian | d6f38b3 | 2012-05-31 15:46:18 +0000 | [diff] [blame] | 422 | puti = st->Ist.PutI.details; |
| 423 | e1 = flatten_Expr(bb, puti->ix); |
| 424 | e2 = flatten_Expr(bb, puti->data); |
| 425 | puti2 = mkIRPutI(puti->descr, e1, puti->bias, e2); |
| 426 | addStmtToIRSB(bb, IRStmt_PutI(puti2)); |
sewardj | d721703 | 2004-08-19 10:49:10 +0000 | [diff] [blame] | 427 | break; |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 428 | case Ist_WrTmp: |
| 429 | if (isFlat(st->Ist.WrTmp.data)) { |
sewardj | e80679a | 2004-09-21 23:00:11 +0000 | [diff] [blame] | 430 | /* optimisation, to reduce the number of tmp-tmp |
| 431 | copies generated */ |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 432 | addStmtToIRSB(bb, st); |
sewardj | e80679a | 2004-09-21 23:00:11 +0000 | [diff] [blame] | 433 | } else { |
| 434 | /* general case, always correct */ |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 435 | e1 = flatten_Expr(bb, st->Ist.WrTmp.data); |
| 436 | addStmtToIRSB(bb, IRStmt_WrTmp(st->Ist.WrTmp.tmp, e1)); |
sewardj | e80679a | 2004-09-21 23:00:11 +0000 | [diff] [blame] | 437 | } |
sewardj | d7cb853 | 2004-08-17 23:59:23 +0000 | [diff] [blame] | 438 | break; |
sewardj | af1ceca | 2005-06-30 23:31:27 +0000 | [diff] [blame] | 439 | case Ist_Store: |
| 440 | e1 = flatten_Expr(bb, st->Ist.Store.addr); |
| 441 | e2 = flatten_Expr(bb, st->Ist.Store.data); |
sewardj | e768e92 | 2009-11-26 17:17:37 +0000 | [diff] [blame] | 442 | addStmtToIRSB(bb, IRStmt_Store(st->Ist.Store.end, e1,e2)); |
sewardj | e9d8a26 | 2009-07-01 08:06:34 +0000 | [diff] [blame] | 443 | break; |
sewardj | cfe046e | 2013-01-17 14:23:53 +0000 | [diff] [blame] | 444 | case Ist_StoreG: |
| 445 | sg = st->Ist.StoreG.details; |
| 446 | e1 = flatten_Expr(bb, sg->addr); |
| 447 | e2 = flatten_Expr(bb, sg->data); |
| 448 | e3 = flatten_Expr(bb, sg->guard); |
| 449 | addStmtToIRSB(bb, IRStmt_StoreG(sg->end, e1, e2, e3)); |
| 450 | break; |
| 451 | case Ist_LoadG: |
| 452 | lg = st->Ist.LoadG.details; |
| 453 | e1 = flatten_Expr(bb, lg->addr); |
| 454 | e2 = flatten_Expr(bb, lg->alt); |
| 455 | e3 = flatten_Expr(bb, lg->guard); |
| 456 | addStmtToIRSB(bb, IRStmt_LoadG(lg->end, lg->cvt, lg->dst, |
| 457 | e1, e2, e3)); |
| 458 | break; |
sewardj | e9d8a26 | 2009-07-01 08:06:34 +0000 | [diff] [blame] | 459 | case Ist_CAS: |
| 460 | cas = st->Ist.CAS.details; |
| 461 | e1 = flatten_Expr(bb, cas->addr); |
| 462 | e2 = cas->expdHi ? flatten_Expr(bb, cas->expdHi) : NULL; |
| 463 | e3 = flatten_Expr(bb, cas->expdLo); |
| 464 | e4 = cas->dataHi ? flatten_Expr(bb, cas->dataHi) : NULL; |
| 465 | e5 = flatten_Expr(bb, cas->dataLo); |
| 466 | cas2 = mkIRCAS( cas->oldHi, cas->oldLo, cas->end, |
| 467 | e1, e2, e3, e4, e5 ); |
| 468 | addStmtToIRSB(bb, IRStmt_CAS(cas2)); |
sewardj | d7cb853 | 2004-08-17 23:59:23 +0000 | [diff] [blame] | 469 | break; |
sewardj | e768e92 | 2009-11-26 17:17:37 +0000 | [diff] [blame] | 470 | case Ist_LLSC: |
| 471 | e1 = flatten_Expr(bb, st->Ist.LLSC.addr); |
| 472 | e2 = st->Ist.LLSC.storedata |
| 473 | ? flatten_Expr(bb, st->Ist.LLSC.storedata) |
| 474 | : NULL; |
| 475 | addStmtToIRSB(bb, IRStmt_LLSC(st->Ist.LLSC.end, |
| 476 | st->Ist.LLSC.result, e1, e2)); |
| 477 | break; |
sewardj | 17442fe | 2004-09-20 14:54:28 +0000 | [diff] [blame] | 478 | case Ist_Dirty: |
| 479 | d = st->Ist.Dirty.details; |
| 480 | d2 = emptyIRDirty(); |
| 481 | *d2 = *d; |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 482 | d2->args = shallowCopyIRExprVec(d2->args); |
sewardj | 17442fe | 2004-09-20 14:54:28 +0000 | [diff] [blame] | 483 | if (d2->mFx != Ifx_None) { |
| 484 | d2->mAddr = flatten_Expr(bb, d2->mAddr); |
| 485 | } else { |
| 486 | vassert(d2->mAddr == NULL); |
| 487 | } |
sewardj | b8385d8 | 2004-11-02 01:34:15 +0000 | [diff] [blame] | 488 | d2->guard = flatten_Expr(bb, d2->guard); |
sewardj | 74142b8 | 2013-08-08 10:28:59 +0000 | [diff] [blame] | 489 | for (i = 0; d2->args[i]; i++) { |
| 490 | IRExpr* arg = d2->args[i]; |
florian | 9041956 | 2013-08-15 20:54:52 +0000 | [diff] [blame] | 491 | if (LIKELY(!is_IRExpr_VECRET_or_BBPTR(arg))) |
sewardj | 74142b8 | 2013-08-08 10:28:59 +0000 | [diff] [blame] | 492 | d2->args[i] = flatten_Expr(bb, arg); |
| 493 | } |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 494 | addStmtToIRSB(bb, IRStmt_Dirty(d2)); |
sewardj | 17442fe | 2004-09-20 14:54:28 +0000 | [diff] [blame] | 495 | break; |
sewardj | d2445f6 | 2005-03-21 00:15:53 +0000 | [diff] [blame] | 496 | case Ist_NoOp: |
sewardj | c4356f0 | 2007-11-09 21:15:04 +0000 | [diff] [blame] | 497 | case Ist_MBE: |
sewardj | d2445f6 | 2005-03-21 00:15:53 +0000 | [diff] [blame] | 498 | case Ist_IMark: |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 499 | addStmtToIRSB(bb, st); |
sewardj | 3e83893 | 2005-01-07 12:09:15 +0000 | [diff] [blame] | 500 | break; |
sewardj | 5a9ffab | 2005-05-12 17:55:01 +0000 | [diff] [blame] | 501 | case Ist_AbiHint: |
| 502 | e1 = flatten_Expr(bb, st->Ist.AbiHint.base); |
sewardj | 478646f | 2008-05-01 20:13:04 +0000 | [diff] [blame] | 503 | e2 = flatten_Expr(bb, st->Ist.AbiHint.nia); |
| 504 | addStmtToIRSB(bb, IRStmt_AbiHint(e1, st->Ist.AbiHint.len, e2)); |
sewardj | 5a9ffab | 2005-05-12 17:55:01 +0000 | [diff] [blame] | 505 | break; |
sewardj | d7cb853 | 2004-08-17 23:59:23 +0000 | [diff] [blame] | 506 | case Ist_Exit: |
sewardj | 0276d4b | 2004-11-15 15:30:21 +0000 | [diff] [blame] | 507 | e1 = flatten_Expr(bb, st->Ist.Exit.guard); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 508 | addStmtToIRSB(bb, IRStmt_Exit(e1, st->Ist.Exit.jk, |
sewardj | c6f970f | 2012-04-02 21:54:49 +0000 | [diff] [blame] | 509 | st->Ist.Exit.dst, |
| 510 | st->Ist.Exit.offsIP)); |
sewardj | d7cb853 | 2004-08-17 23:59:23 +0000 | [diff] [blame] | 511 | break; |
| 512 | default: |
| 513 | vex_printf("\n"); |
| 514 | ppIRStmt(st); |
| 515 | vex_printf("\n"); |
| 516 | vpanic("flatten_Stmt"); |
| 517 | } |
| 518 | } |
| 519 | |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 520 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 521 | static IRSB* flatten_BB ( IRSB* in ) |
sewardj | d7cb853 | 2004-08-17 23:59:23 +0000 | [diff] [blame] | 522 | { |
| 523 | Int i; |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 524 | IRSB* out; |
| 525 | out = emptyIRSB(); |
| 526 | out->tyenv = deepCopyIRTypeEnv( in->tyenv ); |
sewardj | d7cb853 | 2004-08-17 23:59:23 +0000 | [diff] [blame] | 527 | for (i = 0; i < in->stmts_used; i++) |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 528 | if (in->stmts[i]) |
| 529 | flatten_Stmt( out, in->stmts[i] ); |
sewardj | d7cb853 | 2004-08-17 23:59:23 +0000 | [diff] [blame] | 530 | out->next = flatten_Expr( out, in->next ); |
| 531 | out->jumpkind = in->jumpkind; |
sewardj | c6f970f | 2012-04-02 21:54:49 +0000 | [diff] [blame] | 532 | out->offsIP = in->offsIP; |
sewardj | d7cb853 | 2004-08-17 23:59:23 +0000 | [diff] [blame] | 533 | return out; |
| 534 | } |
| 535 | |
sewardj | edf4d69 | 2004-08-17 13:52:58 +0000 | [diff] [blame] | 536 | |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 537 | /*---------------------------------------------------------------*/ |
| 538 | /*--- In-place removal of redundant GETs ---*/ |
| 539 | /*---------------------------------------------------------------*/ |
| 540 | |
| 541 | /* Scan forwards, building up an environment binding (min offset, max |
| 542 | offset) pairs to values, which will either be temps or constants. |
| 543 | |
| 544 | On seeing 't = Get(minoff,maxoff)', look up (minoff,maxoff) in the |
| 545 | env and if it matches, replace the Get with the stored value. If |
| 546 | there is no match, add a (minoff,maxoff) :-> t binding. |
| 547 | |
| 548 | On seeing 'Put (minoff,maxoff) = t or c', first remove in the env |
| 549 | any binding which fully or partially overlaps with (minoff,maxoff). |
| 550 | Then add a new (minoff,maxoff) :-> t or c binding. */ |
| 551 | |
| 552 | /* Extract the min/max offsets from a guest state array descriptor. */ |
| 553 | |
| 554 | inline |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 555 | static void getArrayBounds ( IRRegArray* descr, |
| 556 | UInt* minoff, UInt* maxoff ) |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 557 | { |
| 558 | *minoff = descr->base; |
| 559 | *maxoff = *minoff + descr->nElems*sizeofIRType(descr->elemTy) - 1; |
| 560 | vassert((*minoff & ~0xFFFF) == 0); |
| 561 | vassert((*maxoff & ~0xFFFF) == 0); |
| 562 | vassert(*minoff <= *maxoff); |
| 563 | } |
| 564 | |
| 565 | /* Create keys, of the form ((minoffset << 16) | maxoffset). */ |
| 566 | |
| 567 | static UInt mk_key_GetPut ( Int offset, IRType ty ) |
| 568 | { |
| 569 | /* offset should fit in 16 bits. */ |
| 570 | UInt minoff = offset; |
| 571 | UInt maxoff = minoff + sizeofIRType(ty) - 1; |
| 572 | vassert((minoff & ~0xFFFF) == 0); |
| 573 | vassert((maxoff & ~0xFFFF) == 0); |
| 574 | return (minoff << 16) | maxoff; |
| 575 | } |
| 576 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 577 | static UInt mk_key_GetIPutI ( IRRegArray* descr ) |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 578 | { |
| 579 | UInt minoff, maxoff; |
| 580 | getArrayBounds( descr, &minoff, &maxoff ); |
| 581 | vassert((minoff & ~0xFFFF) == 0); |
| 582 | vassert((maxoff & ~0xFFFF) == 0); |
| 583 | return (minoff << 16) | maxoff; |
| 584 | } |
| 585 | |
| 586 | /* Supposing h has keys of the form generated by mk_key_GetPut and |
| 587 | mk_key_GetIPutI, invalidate any key which overlaps (k_lo |
| 588 | .. k_hi). |
| 589 | */ |
| 590 | static void invalidateOverlaps ( HashHW* h, UInt k_lo, UInt k_hi ) |
| 591 | { |
| 592 | Int j; |
| 593 | UInt e_lo, e_hi; |
| 594 | vassert(k_lo <= k_hi); |
| 595 | /* invalidate any env entries which in any way overlap (k_lo |
| 596 | .. k_hi) */ |
| 597 | /* vex_printf("invalidate %d .. %d\n", k_lo, k_hi ); */ |
| 598 | |
| 599 | for (j = 0; j < h->used; j++) { |
| 600 | if (!h->inuse[j]) |
| 601 | continue; |
| 602 | e_lo = (((UInt)h->key[j]) >> 16) & 0xFFFF; |
| 603 | e_hi = ((UInt)h->key[j]) & 0xFFFF; |
| 604 | vassert(e_lo <= e_hi); |
| 605 | if (e_hi < k_lo || k_hi < e_lo) |
| 606 | continue; /* no overlap possible */ |
| 607 | else |
| 608 | /* overlap; invalidate */ |
| 609 | h->inuse[j] = False; |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 614 | static void redundant_get_removal_BB ( IRSB* bb ) |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 615 | { |
| 616 | HashHW* env = newHHW(); |
| 617 | UInt key = 0; /* keep gcc -O happy */ |
| 618 | Int i, j; |
| 619 | HWord val; |
| 620 | |
| 621 | for (i = 0; i < bb->stmts_used; i++) { |
| 622 | IRStmt* st = bb->stmts[i]; |
| 623 | |
sewardj | 8bee6d1 | 2005-03-22 02:24:05 +0000 | [diff] [blame] | 624 | if (st->tag == Ist_NoOp) |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 625 | continue; |
| 626 | |
| 627 | /* Deal with Gets */ |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 628 | if (st->tag == Ist_WrTmp |
| 629 | && st->Ist.WrTmp.data->tag == Iex_Get) { |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 630 | /* st is 't = Get(...)'. Look up in the environment and see |
| 631 | if the Get can be replaced. */ |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 632 | IRExpr* get = st->Ist.WrTmp.data; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 633 | key = (HWord)mk_key_GetPut( get->Iex.Get.offset, |
| 634 | get->Iex.Get.ty ); |
| 635 | if (lookupHHW(env, &val, (HWord)key)) { |
| 636 | /* found it */ |
| 637 | /* Note, we could do better here. If the types are |
| 638 | different we don't do the substitution, since doing so |
| 639 | could lead to invalidly-typed IR. An improvement would |
| 640 | be to stick in a reinterpret-style cast, although that |
| 641 | would make maintaining flatness more difficult. */ |
| 642 | IRExpr* valE = (IRExpr*)val; |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 643 | Bool typesOK = toBool( typeOfIRExpr(bb->tyenv,valE) |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 644 | == st->Ist.WrTmp.data->Iex.Get.ty ); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 645 | if (typesOK && DEBUG_IROPT) { |
| 646 | vex_printf("rGET: "); ppIRExpr(get); |
| 647 | vex_printf(" -> "); ppIRExpr(valE); |
| 648 | vex_printf("\n"); |
| 649 | } |
| 650 | if (typesOK) |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 651 | bb->stmts[i] = IRStmt_WrTmp(st->Ist.WrTmp.tmp, valE); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 652 | } else { |
| 653 | /* Not found, but at least we know that t and the Get(...) |
| 654 | are now associated. So add a binding to reflect that |
| 655 | fact. */ |
| 656 | addToHHW( env, (HWord)key, |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 657 | (HWord)(void*)(IRExpr_RdTmp(st->Ist.WrTmp.tmp)) ); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 658 | } |
| 659 | } |
| 660 | |
| 661 | /* Deal with Puts: invalidate any env entries overlapped by this |
| 662 | Put */ |
| 663 | if (st->tag == Ist_Put || st->tag == Ist_PutI) { |
| 664 | UInt k_lo, k_hi; |
| 665 | if (st->tag == Ist_Put) { |
| 666 | key = mk_key_GetPut( st->Ist.Put.offset, |
| 667 | typeOfIRExpr(bb->tyenv,st->Ist.Put.data) ); |
| 668 | } else { |
| 669 | vassert(st->tag == Ist_PutI); |
florian | d6f38b3 | 2012-05-31 15:46:18 +0000 | [diff] [blame] | 670 | key = mk_key_GetIPutI( st->Ist.PutI.details->descr ); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | k_lo = (key >> 16) & 0xFFFF; |
| 674 | k_hi = key & 0xFFFF; |
| 675 | invalidateOverlaps(env, k_lo, k_hi); |
| 676 | } |
| 677 | else |
| 678 | if (st->tag == Ist_Dirty) { |
| 679 | /* Deal with dirty helpers which write or modify guest state. |
| 680 | Invalidate the entire env. We could do a lot better |
| 681 | here. */ |
| 682 | IRDirty* d = st->Ist.Dirty.details; |
| 683 | Bool writes = False; |
| 684 | for (j = 0; j < d->nFxState; j++) { |
| 685 | if (d->fxState[j].fx == Ifx_Modify |
| 686 | || d->fxState[j].fx == Ifx_Write) |
| 687 | writes = True; |
| 688 | } |
| 689 | if (writes) { |
| 690 | /* dump the entire env (not clever, but correct ...) */ |
| 691 | for (j = 0; j < env->used; j++) |
| 692 | env->inuse[j] = False; |
| 693 | if (0) vex_printf("rGET: trash env due to dirty helper\n"); |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | /* add this one to the env, if appropriate */ |
| 698 | if (st->tag == Ist_Put) { |
sewardj | 496a58d | 2005-03-20 18:44:44 +0000 | [diff] [blame] | 699 | vassert(isIRAtom(st->Ist.Put.data)); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 700 | addToHHW( env, (HWord)key, (HWord)(st->Ist.Put.data)); |
| 701 | } |
| 702 | |
| 703 | } /* for (i = 0; i < bb->stmts_used; i++) */ |
| 704 | |
| 705 | } |
| 706 | |
| 707 | |
| 708 | /*---------------------------------------------------------------*/ |
| 709 | /*--- In-place removal of redundant PUTs ---*/ |
| 710 | /*---------------------------------------------------------------*/ |
| 711 | |
| 712 | /* Find any Get uses in st and invalidate any partially or fully |
| 713 | overlapping ranges listed in env. Due to the flattening phase, the |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 714 | only stmt kind we expect to find a Get on is IRStmt_WrTmp. */ |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 715 | |
| 716 | static void handle_gets_Stmt ( |
| 717 | HashHW* env, |
| 718 | IRStmt* st, |
| 719 | Bool (*preciseMemExnsFn)(Int,Int) |
| 720 | ) |
| 721 | { |
| 722 | Int j; |
| 723 | UInt key = 0; /* keep gcc -O happy */ |
| 724 | Bool isGet; |
| 725 | Bool memRW = False; |
| 726 | IRExpr* e; |
| 727 | |
| 728 | switch (st->tag) { |
| 729 | |
| 730 | /* This is the only interesting case. Deal with Gets in the RHS |
| 731 | expression. */ |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 732 | case Ist_WrTmp: |
| 733 | e = st->Ist.WrTmp.data; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 734 | switch (e->tag) { |
| 735 | case Iex_Get: |
| 736 | isGet = True; |
| 737 | key = mk_key_GetPut ( e->Iex.Get.offset, e->Iex.Get.ty ); |
| 738 | break; |
| 739 | case Iex_GetI: |
| 740 | isGet = True; |
| 741 | key = mk_key_GetIPutI ( e->Iex.GetI.descr ); |
| 742 | break; |
sewardj | af1ceca | 2005-06-30 23:31:27 +0000 | [diff] [blame] | 743 | case Iex_Load: |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 744 | isGet = False; |
| 745 | memRW = True; |
| 746 | break; |
| 747 | default: |
| 748 | isGet = False; |
| 749 | } |
| 750 | if (isGet) { |
| 751 | UInt k_lo, k_hi; |
| 752 | k_lo = (key >> 16) & 0xFFFF; |
| 753 | k_hi = key & 0xFFFF; |
| 754 | invalidateOverlaps(env, k_lo, k_hi); |
| 755 | } |
| 756 | break; |
| 757 | |
| 758 | /* Be very conservative for dirty helper calls; dump the entire |
| 759 | environment. The helper might read guest state, in which |
| 760 | case it needs to be flushed first. Also, the helper might |
| 761 | access guest memory, in which case all parts of the guest |
| 762 | state requiring precise exceptions needs to be flushed. The |
| 763 | crude solution is just to flush everything; we could easily |
| 764 | enough do a lot better if needed. */ |
sewardj | 3e83893 | 2005-01-07 12:09:15 +0000 | [diff] [blame] | 765 | /* Probably also overly-conservative, but also dump everything |
sewardj | c4356f0 | 2007-11-09 21:15:04 +0000 | [diff] [blame] | 766 | if we hit a memory bus event (fence, lock, unlock). Ditto |
sewardj | e768e92 | 2009-11-26 17:17:37 +0000 | [diff] [blame] | 767 | AbiHints, CASs, LLs and SCs. */ |
sewardj | 5a9ffab | 2005-05-12 17:55:01 +0000 | [diff] [blame] | 768 | case Ist_AbiHint: |
| 769 | vassert(isIRAtom(st->Ist.AbiHint.base)); |
sewardj | 478646f | 2008-05-01 20:13:04 +0000 | [diff] [blame] | 770 | vassert(isIRAtom(st->Ist.AbiHint.nia)); |
sewardj | 5a9ffab | 2005-05-12 17:55:01 +0000 | [diff] [blame] | 771 | /* fall through */ |
sewardj | c4356f0 | 2007-11-09 21:15:04 +0000 | [diff] [blame] | 772 | case Ist_MBE: |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 773 | case Ist_Dirty: |
sewardj | e9d8a26 | 2009-07-01 08:06:34 +0000 | [diff] [blame] | 774 | case Ist_CAS: |
sewardj | e768e92 | 2009-11-26 17:17:37 +0000 | [diff] [blame] | 775 | case Ist_LLSC: |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 776 | for (j = 0; j < env->used; j++) |
| 777 | env->inuse[j] = False; |
| 778 | break; |
| 779 | |
| 780 | /* all other cases are boring. */ |
sewardj | af1ceca | 2005-06-30 23:31:27 +0000 | [diff] [blame] | 781 | case Ist_Store: |
| 782 | vassert(isIRAtom(st->Ist.Store.addr)); |
| 783 | vassert(isIRAtom(st->Ist.Store.data)); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 784 | memRW = True; |
| 785 | break; |
sewardj | cfe046e | 2013-01-17 14:23:53 +0000 | [diff] [blame] | 786 | case Ist_StoreG: { |
| 787 | IRStoreG* sg = st->Ist.StoreG.details; |
| 788 | vassert(isIRAtom(sg->addr)); |
| 789 | vassert(isIRAtom(sg->data)); |
| 790 | vassert(isIRAtom(sg->guard)); |
| 791 | memRW = True; |
| 792 | break; |
| 793 | } |
| 794 | case Ist_LoadG: { |
| 795 | IRLoadG* lg = st->Ist.LoadG.details; |
| 796 | vassert(isIRAtom(lg->addr)); |
| 797 | vassert(isIRAtom(lg->alt)); |
| 798 | vassert(isIRAtom(lg->guard)); |
| 799 | memRW = True; |
| 800 | break; |
| 801 | } |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 802 | case Ist_Exit: |
sewardj | 496a58d | 2005-03-20 18:44:44 +0000 | [diff] [blame] | 803 | vassert(isIRAtom(st->Ist.Exit.guard)); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 804 | break; |
| 805 | |
| 806 | case Ist_PutI: |
florian | d6f38b3 | 2012-05-31 15:46:18 +0000 | [diff] [blame] | 807 | vassert(isIRAtom(st->Ist.PutI.details->ix)); |
| 808 | vassert(isIRAtom(st->Ist.PutI.details->data)); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 809 | break; |
| 810 | |
sewardj | d2445f6 | 2005-03-21 00:15:53 +0000 | [diff] [blame] | 811 | case Ist_NoOp: |
sewardj | f168931 | 2005-03-16 18:19:10 +0000 | [diff] [blame] | 812 | case Ist_IMark: |
| 813 | break; |
| 814 | |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 815 | default: |
| 816 | vex_printf("\n"); |
| 817 | ppIRStmt(st); |
| 818 | vex_printf("\n"); |
| 819 | vpanic("handle_gets_Stmt"); |
| 820 | } |
| 821 | |
| 822 | if (memRW) { |
philippe | 6c46bef | 2012-08-14 22:29:01 +0000 | [diff] [blame] | 823 | /* This statement accesses memory. So we might need to dump all parts |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 824 | of the environment corresponding to guest state that may not |
| 825 | be reordered with respect to memory references. That means |
| 826 | at least the stack pointer. */ |
philippe | c8e2f98 | 2012-08-01 22:04:13 +0000 | [diff] [blame] | 827 | switch (vex_control.iropt_register_updates) { |
| 828 | case VexRegUpdAllregsAtMemAccess: |
| 829 | /* Precise exceptions required at mem access. |
| 830 | Flush all guest state. */ |
| 831 | for (j = 0; j < env->used; j++) |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 832 | env->inuse[j] = False; |
philippe | c8e2f98 | 2012-08-01 22:04:13 +0000 | [diff] [blame] | 833 | break; |
philippe | 6c46bef | 2012-08-14 22:29:01 +0000 | [diff] [blame] | 834 | case VexRegUpdSpAtMemAccess: |
| 835 | /* We need to dump the stack pointer |
| 836 | (needed for stack extension in m_signals.c). |
| 837 | preciseMemExnsFn will use vex_control.iropt_register_updates |
| 838 | to verify only the sp is to be checked. */ |
| 839 | /* fallthrough */ |
philippe | c8e2f98 | 2012-08-01 22:04:13 +0000 | [diff] [blame] | 840 | case VexRegUpdUnwindregsAtMemAccess: |
| 841 | for (j = 0; j < env->used; j++) { |
| 842 | if (!env->inuse[j]) |
| 843 | continue; |
| 844 | /* Just flush the minimal amount required, as computed by |
| 845 | preciseMemExnsFn. */ |
| 846 | HWord k_lo = (env->key[j] >> 16) & 0xFFFF; |
| 847 | HWord k_hi = env->key[j] & 0xFFFF; |
| 848 | if (preciseMemExnsFn( k_lo, k_hi )) |
| 849 | env->inuse[j] = False; |
| 850 | } |
| 851 | break; |
| 852 | default: |
| 853 | // VexRegUpdAllregsAtEachInsn cannot happen here. |
| 854 | // Neither any rubbish other value. |
| 855 | vassert(0); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 856 | } |
| 857 | } /* if (memRW) */ |
| 858 | |
| 859 | } |
| 860 | |
| 861 | |
| 862 | /* Scan backwards, building up a set of (min offset, max |
| 863 | offset) pairs, indicating those parts of the guest state |
| 864 | for which the next event is a write. |
| 865 | |
| 866 | On seeing a conditional exit, empty the set. |
| 867 | |
| 868 | On seeing 'Put (minoff,maxoff) = t or c', if (minoff,maxoff) is |
| 869 | completely within the set, remove the Put. Otherwise, add |
| 870 | (minoff,maxoff) to the set. |
| 871 | |
| 872 | On seeing 'Get (minoff,maxoff)', remove any part of the set |
sewardj | 9843029 | 2004-12-29 17:34:50 +0000 | [diff] [blame] | 873 | overlapping (minoff,maxoff). The same has to happen for any events |
| 874 | which implicitly read parts of the guest state: dirty helper calls |
| 875 | and loads/stores. |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 876 | */ |
| 877 | |
| 878 | static void redundant_put_removal_BB ( |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 879 | IRSB* bb, |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 880 | Bool (*preciseMemExnsFn)(Int,Int) |
| 881 | ) |
| 882 | { |
| 883 | Int i, j; |
| 884 | Bool isPut; |
| 885 | IRStmt* st; |
| 886 | UInt key = 0; /* keep gcc -O happy */ |
| 887 | |
philippe | 6c46bef | 2012-08-14 22:29:01 +0000 | [diff] [blame] | 888 | vassert(vex_control.iropt_register_updates < VexRegUpdAllregsAtEachInsn); |
philippe | c8e2f98 | 2012-08-01 22:04:13 +0000 | [diff] [blame] | 889 | |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 890 | HashHW* env = newHHW(); |
sewardj | c6f970f | 2012-04-02 21:54:49 +0000 | [diff] [blame] | 891 | |
| 892 | /* Initialise the running env with the fact that the final exit |
| 893 | writes the IP (or, whatever it claims to write. We don't |
| 894 | care.) */ |
| 895 | key = mk_key_GetPut(bb->offsIP, typeOfIRExpr(bb->tyenv, bb->next)); |
| 896 | addToHHW(env, (HWord)key, 0); |
| 897 | |
| 898 | /* And now scan backwards through the statements. */ |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 899 | for (i = bb->stmts_used-1; i >= 0; i--) { |
| 900 | st = bb->stmts[i]; |
sewardj | 8bee6d1 | 2005-03-22 02:24:05 +0000 | [diff] [blame] | 901 | |
| 902 | if (st->tag == Ist_NoOp) |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 903 | continue; |
| 904 | |
| 905 | /* Deal with conditional exits. */ |
| 906 | if (st->tag == Ist_Exit) { |
sewardj | c6f970f | 2012-04-02 21:54:49 +0000 | [diff] [blame] | 907 | //Bool re_add; |
| 908 | /* Need to throw out from the env, any part of it which |
| 909 | doesn't overlap with the guest state written by this exit. |
| 910 | Since the exit only writes one section, it's simplest to |
| 911 | do this: (1) check whether env contains a write that |
| 912 | completely overlaps the write done by this exit; (2) empty |
| 913 | out env; and (3) if (1) was true, add the write done by |
| 914 | this exit. |
| 915 | |
| 916 | To make (1) a bit simpler, merely search for a write that |
| 917 | exactly matches the one done by this exit. That's safe |
| 918 | because it will fail as often or more often than a full |
| 919 | overlap check, and failure to find an overlapping write in |
| 920 | env is the safe case (we just nuke env if that |
| 921 | happens). */ |
| 922 | //vassert(isIRAtom(st->Ist.Exit.guard)); |
| 923 | /* (1) */ |
| 924 | //key = mk_key_GetPut(st->Ist.Exit.offsIP, |
| 925 | // typeOfIRConst(st->Ist.Exit.dst)); |
| 926 | //re_add = lookupHHW(env, NULL, key); |
| 927 | /* (2) */ |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 928 | for (j = 0; j < env->used; j++) |
| 929 | env->inuse[j] = False; |
sewardj | c6f970f | 2012-04-02 21:54:49 +0000 | [diff] [blame] | 930 | /* (3) */ |
| 931 | //if (0 && re_add) |
| 932 | // addToHHW(env, (HWord)key, 0); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 933 | continue; |
| 934 | } |
| 935 | |
| 936 | /* Deal with Puts */ |
| 937 | switch (st->tag) { |
| 938 | case Ist_Put: |
| 939 | isPut = True; |
| 940 | key = mk_key_GetPut( st->Ist.Put.offset, |
| 941 | typeOfIRExpr(bb->tyenv,st->Ist.Put.data) ); |
sewardj | 496a58d | 2005-03-20 18:44:44 +0000 | [diff] [blame] | 942 | vassert(isIRAtom(st->Ist.Put.data)); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 943 | break; |
| 944 | case Ist_PutI: |
| 945 | isPut = True; |
florian | d6f38b3 | 2012-05-31 15:46:18 +0000 | [diff] [blame] | 946 | key = mk_key_GetIPutI( st->Ist.PutI.details->descr ); |
| 947 | vassert(isIRAtom(st->Ist.PutI.details->ix)); |
| 948 | vassert(isIRAtom(st->Ist.PutI.details->data)); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 949 | break; |
| 950 | default: |
| 951 | isPut = False; |
| 952 | } |
| 953 | if (isPut && st->tag != Ist_PutI) { |
| 954 | /* See if any single entry in env overlaps this Put. This is |
| 955 | simplistic in that the transformation is valid if, say, two |
| 956 | or more entries in the env overlap this Put, but the use of |
| 957 | lookupHHW will only find a single entry which exactly |
| 958 | overlaps this Put. This is suboptimal but safe. */ |
| 959 | if (lookupHHW(env, NULL, (HWord)key)) { |
| 960 | /* This Put is redundant because a later one will overwrite |
| 961 | it. So NULL (nop) it out. */ |
| 962 | if (DEBUG_IROPT) { |
| 963 | vex_printf("rPUT: "); ppIRStmt(st); |
| 964 | vex_printf("\n"); |
| 965 | } |
sewardj | d2445f6 | 2005-03-21 00:15:53 +0000 | [diff] [blame] | 966 | bb->stmts[i] = IRStmt_NoOp(); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 967 | } else { |
| 968 | /* We can't demonstrate that this Put is redundant, so add it |
| 969 | to the running collection. */ |
| 970 | addToHHW(env, (HWord)key, 0); |
| 971 | } |
| 972 | continue; |
| 973 | } |
| 974 | |
| 975 | /* Deal with Gets. These remove bits of the environment since |
| 976 | appearance of a Get means that the next event for that slice |
sewardj | 9843029 | 2004-12-29 17:34:50 +0000 | [diff] [blame] | 977 | of the guest state is no longer a write, but a read. Also |
| 978 | deals with implicit reads of guest state needed to maintain |
| 979 | precise exceptions. */ |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 980 | handle_gets_Stmt( env, st, preciseMemExnsFn ); |
| 981 | } |
| 982 | } |
| 983 | |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 984 | |
| 985 | /*---------------------------------------------------------------*/ |
| 986 | /*--- Constant propagation and folding ---*/ |
| 987 | /*---------------------------------------------------------------*/ |
| 988 | |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 989 | #if STATS_IROPT |
| 990 | /* How often sameIRExprs was invoked */ |
| 991 | static UInt invocation_count; |
| 992 | /* How often sameIRExprs recursed through IRTemp assignments */ |
| 993 | static UInt recursion_count; |
| 994 | /* How often sameIRExprs found identical IRExprs */ |
| 995 | static UInt success_count; |
| 996 | /* How often recursing through assignments to IRTemps helped |
| 997 | establishing equality. */ |
| 998 | static UInt recursion_success_count; |
| 999 | /* Whether or not recursing through an IRTemp assignment helped |
| 1000 | establishing IRExpr equality for a given sameIRExprs invocation. */ |
| 1001 | static Bool recursion_helped; |
| 1002 | /* Whether or not a given sameIRExprs invocation recursed through an |
| 1003 | IRTemp assignment */ |
| 1004 | static Bool recursed; |
| 1005 | /* Maximum number of nodes ever visited when comparing two IRExprs. */ |
| 1006 | static UInt max_nodes_visited; |
| 1007 | #endif /* STATS_IROPT */ |
| 1008 | |
| 1009 | /* Count the number of nodes visited for a given sameIRExprs invocation. */ |
| 1010 | static UInt num_nodes_visited; |
| 1011 | |
| 1012 | /* Do not visit more than NODE_LIMIT nodes when comparing two IRExprs. |
| 1013 | This is to guard against performance degradation by visiting large |
| 1014 | trees without success. */ |
| 1015 | #define NODE_LIMIT 30 |
| 1016 | |
| 1017 | |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 1018 | /* The env in this section is a map from IRTemp to IRExpr*, |
| 1019 | that is, an array indexed by IRTemp. */ |
sewardj | f650199 | 2004-08-27 11:58:24 +0000 | [diff] [blame] | 1020 | |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 1021 | /* Do both expressions compute the same value? The answer is generally |
| 1022 | conservative, i.e. it will report that the expressions do not compute |
| 1023 | the same value when in fact they do. The reason is that we do not |
| 1024 | keep track of changes in the guest state and memory. Thusly, two |
| 1025 | Get's, GetI's or Load's, even when accessing the same location, will be |
| 1026 | assumed to compute different values. After all the accesses may happen |
| 1027 | at different times and the guest state / memory can have changed in |
sewardj | a7e9638 | 2012-06-29 16:26:17 +0000 | [diff] [blame] | 1028 | the meantime. |
| 1029 | |
| 1030 | XXX IMPORTANT XXX the two expressions must have the same IR type. |
| 1031 | DO NOT CALL HERE WITH DIFFERENTLY-TYPED EXPRESSIONS. */ |
sewardj | c6f970f | 2012-04-02 21:54:49 +0000 | [diff] [blame] | 1032 | |
| 1033 | /* JRS 20-Mar-2012: split sameIRExprs_aux into a fast inlineable |
| 1034 | wrapper that deals with the common tags-don't-match case, and a |
| 1035 | slower out of line general case. Saves a few insns. */ |
| 1036 | |
| 1037 | __attribute__((noinline)) |
| 1038 | static Bool sameIRExprs_aux2 ( IRExpr** env, IRExpr* e1, IRExpr* e2 ); |
| 1039 | |
| 1040 | inline |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 1041 | static Bool sameIRExprs_aux ( IRExpr** env, IRExpr* e1, IRExpr* e2 ) |
sewardj | f672901 | 2004-08-25 12:45:13 +0000 | [diff] [blame] | 1042 | { |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 1043 | if (e1->tag != e2->tag) return False; |
sewardj | c6f970f | 2012-04-02 21:54:49 +0000 | [diff] [blame] | 1044 | return sameIRExprs_aux2(env, e1, e2); |
| 1045 | } |
sewardj | f672901 | 2004-08-25 12:45:13 +0000 | [diff] [blame] | 1046 | |
sewardj | c6f970f | 2012-04-02 21:54:49 +0000 | [diff] [blame] | 1047 | __attribute__((noinline)) |
| 1048 | static Bool sameIRExprs_aux2 ( IRExpr** env, IRExpr* e1, IRExpr* e2 ) |
| 1049 | { |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 1050 | if (num_nodes_visited++ > NODE_LIMIT) return False; |
sewardj | 6c299f3 | 2009-12-31 18:00:12 +0000 | [diff] [blame] | 1051 | |
sewardj | 6c299f3 | 2009-12-31 18:00:12 +0000 | [diff] [blame] | 1052 | switch (e1->tag) { |
| 1053 | case Iex_RdTmp: |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 1054 | if (e1->Iex.RdTmp.tmp == e2->Iex.RdTmp.tmp) return True; |
| 1055 | |
| 1056 | if (env[e1->Iex.RdTmp.tmp] && env[e2->Iex.RdTmp.tmp]) { |
| 1057 | Bool same = sameIRExprs_aux(env, env[e1->Iex.RdTmp.tmp], |
| 1058 | env[e2->Iex.RdTmp.tmp]); |
| 1059 | #if STATS_IROPT |
| 1060 | recursed = True; |
| 1061 | if (same) recursion_helped = True; |
| 1062 | #endif |
| 1063 | return same; |
| 1064 | } |
sewardj | 6c299f3 | 2009-12-31 18:00:12 +0000 | [diff] [blame] | 1065 | return False; |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 1066 | |
| 1067 | case Iex_Get: |
| 1068 | case Iex_GetI: |
| 1069 | case Iex_Load: |
| 1070 | /* Guest state / memory could have changed in the meantime. */ |
| 1071 | return False; |
| 1072 | |
| 1073 | case Iex_Binop: |
| 1074 | return toBool( e1->Iex.Binop.op == e2->Iex.Binop.op |
sewardj | 6399f81 | 2012-06-29 15:36:44 +0000 | [diff] [blame] | 1075 | && sameIRExprs_aux( env, e1->Iex.Binop.arg1, |
| 1076 | e2->Iex.Binop.arg1 ) |
| 1077 | && sameIRExprs_aux( env, e1->Iex.Binop.arg2, |
| 1078 | e2->Iex.Binop.arg2 )); |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 1079 | |
| 1080 | case Iex_Unop: |
| 1081 | return toBool( e1->Iex.Unop.op == e2->Iex.Unop.op |
sewardj | 6399f81 | 2012-06-29 15:36:44 +0000 | [diff] [blame] | 1082 | && sameIRExprs_aux( env, e1->Iex.Unop.arg, |
| 1083 | e2->Iex.Unop.arg )); |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 1084 | |
| 1085 | case Iex_Const: { |
| 1086 | IRConst *c1 = e1->Iex.Const.con; |
| 1087 | IRConst *c2 = e2->Iex.Const.con; |
| 1088 | vassert(c1->tag == c2->tag); |
| 1089 | switch (c1->tag) { |
| 1090 | case Ico_U1: return toBool( c1->Ico.U1 == c2->Ico.U1 ); |
| 1091 | case Ico_U8: return toBool( c1->Ico.U8 == c2->Ico.U8 ); |
| 1092 | case Ico_U16: return toBool( c1->Ico.U16 == c2->Ico.U16 ); |
| 1093 | case Ico_U32: return toBool( c1->Ico.U32 == c2->Ico.U32 ); |
| 1094 | case Ico_U64: return toBool( c1->Ico.U64 == c2->Ico.U64 ); |
| 1095 | default: break; |
| 1096 | } |
| 1097 | return False; |
| 1098 | } |
| 1099 | |
florian | 420bfa9 | 2012-06-02 20:29:22 +0000 | [diff] [blame] | 1100 | case Iex_Triop: { |
| 1101 | IRTriop *tri1 = e1->Iex.Triop.details; |
| 1102 | IRTriop *tri2 = e2->Iex.Triop.details; |
| 1103 | return toBool( tri1->op == tri2->op |
| 1104 | && sameIRExprs_aux( env, tri1->arg1, tri2->arg1 ) |
| 1105 | && sameIRExprs_aux( env, tri1->arg2, tri2->arg2 ) |
| 1106 | && sameIRExprs_aux( env, tri1->arg3, tri2->arg3 )); |
| 1107 | } |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 1108 | |
florian | 99dd03e | 2013-01-29 03:56:06 +0000 | [diff] [blame] | 1109 | case Iex_ITE: |
| 1110 | return toBool( sameIRExprs_aux( env, e1->Iex.ITE.cond, |
| 1111 | e2->Iex.ITE.cond ) |
| 1112 | && sameIRExprs_aux( env, e1->Iex.ITE.iftrue, |
| 1113 | e2->Iex.ITE.iftrue ) |
| 1114 | && sameIRExprs_aux( env, e1->Iex.ITE.iffalse, |
| 1115 | e2->Iex.ITE.iffalse )); |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 1116 | |
| 1117 | default: |
| 1118 | /* Not very likely to be "same". */ |
| 1119 | break; |
sewardj | 6c299f3 | 2009-12-31 18:00:12 +0000 | [diff] [blame] | 1120 | } |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 1121 | |
| 1122 | return False; |
sewardj | 6c299f3 | 2009-12-31 18:00:12 +0000 | [diff] [blame] | 1123 | } |
| 1124 | |
sewardj | c6f970f | 2012-04-02 21:54:49 +0000 | [diff] [blame] | 1125 | inline |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 1126 | static Bool sameIRExprs ( IRExpr** env, IRExpr* e1, IRExpr* e2 ) |
| 1127 | { |
| 1128 | Bool same; |
| 1129 | |
| 1130 | num_nodes_visited = 0; |
| 1131 | same = sameIRExprs_aux(env, e1, e2); |
| 1132 | |
| 1133 | #if STATS_IROPT |
| 1134 | ++invocation_count; |
| 1135 | if (recursed) ++recursion_count; |
| 1136 | success_count += same; |
| 1137 | if (same && recursion_helped) |
| 1138 | ++recursion_success_count; |
| 1139 | if (num_nodes_visited > max_nodes_visited) |
| 1140 | max_nodes_visited = num_nodes_visited; |
| 1141 | recursed = False; /* reset */ |
| 1142 | recursion_helped = False; /* reset */ |
| 1143 | #endif /* STATS_IROPT */ |
| 1144 | |
| 1145 | return same; |
| 1146 | } |
| 1147 | |
| 1148 | |
sewardj | a7e9638 | 2012-06-29 16:26:17 +0000 | [diff] [blame] | 1149 | /* Debugging-only hack (not used in production runs): make a guess |
| 1150 | whether sameIRExprs might assert due to the two args being of |
| 1151 | different types. If in doubt return False. Is only used when |
| 1152 | --vex-iropt-level > 0, that is, vex_control.iropt_verbosity > 0. |
| 1153 | Bad because it duplicates functionality from typeOfIRExpr. See |
| 1154 | comment on the single use point below for rationale. */ |
| 1155 | static |
| 1156 | Bool debug_only_hack_sameIRExprs_might_assert ( IRExpr* e1, IRExpr* e2 ) |
| 1157 | { |
| 1158 | if (e1->tag != e2->tag) return False; |
| 1159 | switch (e1->tag) { |
| 1160 | case Iex_Const: { |
| 1161 | /* The only interesting case */ |
| 1162 | IRConst *c1 = e1->Iex.Const.con; |
| 1163 | IRConst *c2 = e2->Iex.Const.con; |
| 1164 | return c1->tag != c2->tag; |
| 1165 | } |
| 1166 | default: |
| 1167 | break; |
| 1168 | } |
| 1169 | return False; |
| 1170 | } |
| 1171 | |
| 1172 | |
florian | ea7eab7 | 2011-07-21 16:21:58 +0000 | [diff] [blame] | 1173 | /* Is this literally IRExpr_Const(IRConst_U32(0)) ? */ |
| 1174 | static Bool isZeroU32 ( IRExpr* e ) |
| 1175 | { |
| 1176 | return toBool( e->tag == Iex_Const |
| 1177 | && e->Iex.Const.con->tag == Ico_U32 |
| 1178 | && e->Iex.Const.con->Ico.U32 == 0); |
| 1179 | } |
| 1180 | |
florian | 40226d1 | 2014-07-16 20:29:38 +0000 | [diff] [blame] | 1181 | /* Is this literally IRExpr_Const(IRConst_U64(0)) ? |
| 1182 | Currently unused; commented out to avoid compiler warning */ |
| 1183 | #if 0 |
sewardj | bbcf188 | 2014-01-12 12:49:10 +0000 | [diff] [blame] | 1184 | static Bool isZeroU64 ( IRExpr* e ) |
| 1185 | { |
| 1186 | return toBool( e->tag == Iex_Const |
| 1187 | && e->Iex.Const.con->tag == Ico_U64 |
| 1188 | && e->Iex.Const.con->Ico.U64 == 0); |
| 1189 | } |
florian | 40226d1 | 2014-07-16 20:29:38 +0000 | [diff] [blame] | 1190 | #endif |
sewardj | bbcf188 | 2014-01-12 12:49:10 +0000 | [diff] [blame] | 1191 | |
sewardj | 9571dc0 | 2014-01-26 18:34:23 +0000 | [diff] [blame] | 1192 | /* Is this literally IRExpr_Const(IRConst_V128(0)) ? */ |
| 1193 | static Bool isZeroV128 ( IRExpr* e ) |
| 1194 | { |
| 1195 | return toBool( e->tag == Iex_Const |
| 1196 | && e->Iex.Const.con->tag == Ico_V128 |
| 1197 | && e->Iex.Const.con->Ico.V128 == 0x0000); |
| 1198 | } |
| 1199 | |
| 1200 | /* Is this literally IRExpr_Const(IRConst_V256(0)) ? */ |
| 1201 | static Bool isZeroV256 ( IRExpr* e ) |
| 1202 | { |
| 1203 | return toBool( e->tag == Iex_Const |
| 1204 | && e->Iex.Const.con->tag == Ico_V256 |
| 1205 | && e->Iex.Const.con->Ico.V256 == 0x00000000); |
| 1206 | } |
| 1207 | |
florian | f6402ab | 2012-01-29 02:19:43 +0000 | [diff] [blame] | 1208 | /* Is this an integer constant with value 0 ? */ |
| 1209 | static Bool isZeroU ( IRExpr* e ) |
| 1210 | { |
| 1211 | if (e->tag != Iex_Const) return False; |
florian | f6402ab | 2012-01-29 02:19:43 +0000 | [diff] [blame] | 1212 | switch (e->Iex.Const.con->tag) { |
| 1213 | case Ico_U1: return toBool( e->Iex.Const.con->Ico.U1 == 0); |
| 1214 | case Ico_U8: return toBool( e->Iex.Const.con->Ico.U8 == 0); |
| 1215 | case Ico_U16: return toBool( e->Iex.Const.con->Ico.U16 == 0); |
| 1216 | case Ico_U32: return toBool( e->Iex.Const.con->Ico.U32 == 0); |
| 1217 | case Ico_U64: return toBool( e->Iex.Const.con->Ico.U64 == 0); |
| 1218 | default: vpanic("isZeroU"); |
| 1219 | } |
| 1220 | } |
| 1221 | |
sewardj | cf4be4a | 2012-03-26 09:44:39 +0000 | [diff] [blame] | 1222 | /* Is this an integer constant with value 1---1b ? */ |
| 1223 | static Bool isOnesU ( IRExpr* e ) |
| 1224 | { |
| 1225 | if (e->tag != Iex_Const) return False; |
| 1226 | switch (e->Iex.Const.con->tag) { |
| 1227 | case Ico_U8: return toBool( e->Iex.Const.con->Ico.U8 == 0xFF); |
| 1228 | case Ico_U16: return toBool( e->Iex.Const.con->Ico.U16 == 0xFFFF); |
| 1229 | case Ico_U32: return toBool( e->Iex.Const.con->Ico.U32 |
| 1230 | == 0xFFFFFFFF); |
| 1231 | case Ico_U64: return toBool( e->Iex.Const.con->Ico.U64 |
| 1232 | == 0xFFFFFFFFFFFFFFFFULL); |
| 1233 | default: ppIRExpr(e); vpanic("isOnesU"); |
| 1234 | } |
| 1235 | } |
| 1236 | |
sewardj | e1d45da | 2004-11-12 00:13:21 +0000 | [diff] [blame] | 1237 | static Bool notBool ( Bool b ) |
| 1238 | { |
| 1239 | if (b == True) return False; |
| 1240 | if (b == False) return True; |
| 1241 | vpanic("notBool"); |
| 1242 | } |
| 1243 | |
sewardj | 0033ddc | 2005-04-26 23:34:34 +0000 | [diff] [blame] | 1244 | /* Make a zero which has the same type as the result of the given |
| 1245 | primop. */ |
sewardj | 64d776c | 2010-10-01 14:06:22 +0000 | [diff] [blame] | 1246 | static IRExpr* mkZeroOfPrimopResultType ( IROp op ) |
sewardj | 0033ddc | 2005-04-26 23:34:34 +0000 | [diff] [blame] | 1247 | { |
| 1248 | switch (op) { |
sewardj | a7e9638 | 2012-06-29 16:26:17 +0000 | [diff] [blame] | 1249 | case Iop_CmpNE32: return IRExpr_Const(IRConst_U1(toBool(0))); |
sewardj | 0033ddc | 2005-04-26 23:34:34 +0000 | [diff] [blame] | 1250 | case Iop_Xor8: return IRExpr_Const(IRConst_U8(0)); |
| 1251 | case Iop_Xor16: return IRExpr_Const(IRConst_U16(0)); |
sewardj | be91791 | 2010-08-22 12:38:53 +0000 | [diff] [blame] | 1252 | case Iop_Sub32: |
sewardj | 0033ddc | 2005-04-26 23:34:34 +0000 | [diff] [blame] | 1253 | case Iop_Xor32: return IRExpr_Const(IRConst_U32(0)); |
sewardj | bbcf188 | 2014-01-12 12:49:10 +0000 | [diff] [blame] | 1254 | case Iop_And64: |
sewardj | 64d776c | 2010-10-01 14:06:22 +0000 | [diff] [blame] | 1255 | case Iop_Sub64: |
sewardj | 0033ddc | 2005-04-26 23:34:34 +0000 | [diff] [blame] | 1256 | case Iop_Xor64: return IRExpr_Const(IRConst_U64(0)); |
sewardj | 36a911a | 2014-04-03 13:48:21 +0000 | [diff] [blame] | 1257 | case Iop_XorV128: |
| 1258 | case Iop_AndV128: return IRExpr_Const(IRConst_V128(0)); |
florian | 1b7c471 | 2014-07-16 20:17:49 +0000 | [diff] [blame] | 1259 | case Iop_AndV256: return IRExpr_Const(IRConst_V256(0)); |
sewardj | 64d776c | 2010-10-01 14:06:22 +0000 | [diff] [blame] | 1260 | default: vpanic("mkZeroOfPrimopResultType: bad primop"); |
| 1261 | } |
| 1262 | } |
| 1263 | |
| 1264 | /* Make a value containing all 1-bits, which has the same type as the |
| 1265 | result of the given primop. */ |
| 1266 | static IRExpr* mkOnesOfPrimopResultType ( IROp op ) |
| 1267 | { |
| 1268 | switch (op) { |
sewardj | a7e9638 | 2012-06-29 16:26:17 +0000 | [diff] [blame] | 1269 | case Iop_CmpEQ32: |
sewardj | 64d776c | 2010-10-01 14:06:22 +0000 | [diff] [blame] | 1270 | case Iop_CmpEQ64: |
| 1271 | return IRExpr_Const(IRConst_U1(toBool(1))); |
sewardj | cf4be4a | 2012-03-26 09:44:39 +0000 | [diff] [blame] | 1272 | case Iop_Or8: |
| 1273 | return IRExpr_Const(IRConst_U8(0xFF)); |
| 1274 | case Iop_Or16: |
| 1275 | return IRExpr_Const(IRConst_U16(0xFFFF)); |
| 1276 | case Iop_Or32: |
| 1277 | return IRExpr_Const(IRConst_U32(0xFFFFFFFF)); |
sewardj | 64d776c | 2010-10-01 14:06:22 +0000 | [diff] [blame] | 1278 | case Iop_CmpEQ8x8: |
sewardj | cf4be4a | 2012-03-26 09:44:39 +0000 | [diff] [blame] | 1279 | case Iop_Or64: |
sewardj | 64d776c | 2010-10-01 14:06:22 +0000 | [diff] [blame] | 1280 | return IRExpr_Const(IRConst_U64(0xFFFFFFFFFFFFFFFFULL)); |
| 1281 | case Iop_CmpEQ8x16: |
sewardj | 899d183 | 2012-07-10 21:41:01 +0000 | [diff] [blame] | 1282 | case Iop_CmpEQ16x8: |
sewardj | ffccf2b | 2012-06-29 15:33:09 +0000 | [diff] [blame] | 1283 | case Iop_CmpEQ32x4: |
sewardj | 64d776c | 2010-10-01 14:06:22 +0000 | [diff] [blame] | 1284 | return IRExpr_Const(IRConst_V128(0xFFFF)); |
| 1285 | default: |
sewardj | cf4be4a | 2012-03-26 09:44:39 +0000 | [diff] [blame] | 1286 | ppIROp(op); |
sewardj | 64d776c | 2010-10-01 14:06:22 +0000 | [diff] [blame] | 1287 | vpanic("mkOnesOfPrimopResultType: bad primop"); |
sewardj | 0033ddc | 2005-04-26 23:34:34 +0000 | [diff] [blame] | 1288 | } |
| 1289 | } |
| 1290 | |
sewardj | 4cba9f4 | 2011-03-07 18:34:34 +0000 | [diff] [blame] | 1291 | /* Helpers for folding Clz32/64. */ |
| 1292 | static UInt fold_Clz64 ( ULong value ) |
| 1293 | { |
| 1294 | UInt i; |
| 1295 | vassert(value != 0ULL); /* no defined semantics for arg==0 */ |
| 1296 | for (i = 0; i < 64; ++i) { |
sewardj | 7f6330d | 2011-04-05 11:06:02 +0000 | [diff] [blame] | 1297 | if (0ULL != (value & (((ULong)1) << (63 - i)))) return i; |
sewardj | 4cba9f4 | 2011-03-07 18:34:34 +0000 | [diff] [blame] | 1298 | } |
| 1299 | vassert(0); |
| 1300 | /*NOTREACHED*/ |
| 1301 | return 0; |
| 1302 | } |
| 1303 | |
| 1304 | static UInt fold_Clz32 ( UInt value ) |
| 1305 | { |
| 1306 | UInt i; |
| 1307 | vassert(value != 0); /* no defined semantics for arg==0 */ |
| 1308 | for (i = 0; i < 32; ++i) { |
sewardj | 7f6330d | 2011-04-05 11:06:02 +0000 | [diff] [blame] | 1309 | if (0 != (value & (((UInt)1) << (31 - i)))) return i; |
sewardj | 4cba9f4 | 2011-03-07 18:34:34 +0000 | [diff] [blame] | 1310 | } |
| 1311 | vassert(0); |
| 1312 | /*NOTREACHED*/ |
| 1313 | return 0; |
| 1314 | } |
| 1315 | |
sewardj | 4a0bee0 | 2012-06-29 14:44:44 +0000 | [diff] [blame] | 1316 | /* V64 holds 8 summary-constant bits in V128/V256 style. Convert to |
| 1317 | the corresponding real constant. */ |
| 1318 | //XXX re-check this before use |
| 1319 | //static ULong de_summarise_V64 ( UChar v64 ) |
| 1320 | //{ |
| 1321 | // ULong r = 0; |
| 1322 | // if (v64 & (1<<0)) r |= 0x00000000000000FFULL; |
| 1323 | // if (v64 & (1<<1)) r |= 0x000000000000FF00ULL; |
| 1324 | // if (v64 & (1<<2)) r |= 0x0000000000FF0000ULL; |
| 1325 | // if (v64 & (1<<3)) r |= 0x00000000FF000000ULL; |
| 1326 | // if (v64 & (1<<4)) r |= 0x000000FF00000000ULL; |
| 1327 | // if (v64 & (1<<5)) r |= 0x0000FF0000000000ULL; |
| 1328 | // if (v64 & (1<<6)) r |= 0x00FF000000000000ULL; |
| 1329 | // if (v64 & (1<<7)) r |= 0xFF00000000000000ULL; |
| 1330 | // return r; |
| 1331 | //} |
sewardj | 0033ddc | 2005-04-26 23:34:34 +0000 | [diff] [blame] | 1332 | |
sewardj | 009230b | 2013-01-26 11:47:55 +0000 | [diff] [blame] | 1333 | /* Helper for arbitrary expression pattern matching in flat IR. If |
| 1334 | 'e' is a reference to a tmp, look it up in env -- repeatedly, if |
| 1335 | necessary -- until it resolves to a non-tmp. Note that this can |
| 1336 | return NULL if it can't resolve 'e' to a new expression, which will |
| 1337 | be the case if 'e' is instead defined by an IRStmt (IRDirty or |
| 1338 | LLSC). */ |
| 1339 | static IRExpr* chase ( IRExpr** env, IRExpr* e ) |
| 1340 | { |
| 1341 | /* Why is this loop guaranteed to terminate? Because all tmps must |
| 1342 | have definitions before use, hence a tmp cannot be bound |
| 1343 | (directly or indirectly) to itself. */ |
| 1344 | while (e->tag == Iex_RdTmp) { |
| 1345 | if (0) { vex_printf("chase "); ppIRExpr(e); vex_printf("\n"); } |
| 1346 | e = env[(Int)e->Iex.RdTmp.tmp]; |
| 1347 | if (e == NULL) break; |
| 1348 | } |
| 1349 | return e; |
| 1350 | } |
| 1351 | |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 1352 | static IRExpr* fold_Expr ( IRExpr** env, IRExpr* e ) |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 1353 | { |
sewardj | 278c44c | 2004-08-20 00:28:13 +0000 | [diff] [blame] | 1354 | Int shift; |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 1355 | IRExpr* e2 = e; /* e2 is the result of folding e, if possible */ |
| 1356 | |
florian | 708417d | 2012-02-15 00:43:36 +0000 | [diff] [blame] | 1357 | switch (e->tag) { |
| 1358 | case Iex_Unop: |
| 1359 | /* UNARY ops */ |
| 1360 | if (e->Iex.Unop.arg->tag == Iex_Const) { |
| 1361 | switch (e->Iex.Unop.op) { |
sewardj | ae27ab6 | 2004-10-15 21:21:46 +0000 | [diff] [blame] | 1362 | case Iop_1Uto8: |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1363 | e2 = IRExpr_Const(IRConst_U8(toUChar( |
sewardj | ba99931 | 2004-11-15 15:21:17 +0000 | [diff] [blame] | 1364 | e->Iex.Unop.arg->Iex.Const.con->Ico.U1 |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1365 | ? 1 : 0))); |
sewardj | ae27ab6 | 2004-10-15 21:21:46 +0000 | [diff] [blame] | 1366 | break; |
sewardj | f4a821d | 2004-10-09 00:58:19 +0000 | [diff] [blame] | 1367 | case Iop_1Uto32: |
| 1368 | e2 = IRExpr_Const(IRConst_U32( |
sewardj | ba99931 | 2004-11-15 15:21:17 +0000 | [diff] [blame] | 1369 | e->Iex.Unop.arg->Iex.Const.con->Ico.U1 |
sewardj | f4a821d | 2004-10-09 00:58:19 +0000 | [diff] [blame] | 1370 | ? 1 : 0)); |
| 1371 | break; |
sewardj | 2716ff1 | 2005-05-20 19:21:45 +0000 | [diff] [blame] | 1372 | case Iop_1Uto64: |
| 1373 | e2 = IRExpr_Const(IRConst_U64( |
| 1374 | e->Iex.Unop.arg->Iex.Const.con->Ico.U1 |
| 1375 | ? 1 : 0)); |
| 1376 | break; |
sewardj | e6b3993 | 2004-11-06 17:01:15 +0000 | [diff] [blame] | 1377 | |
sewardj | 1bee561 | 2005-11-10 18:10:58 +0000 | [diff] [blame] | 1378 | case Iop_1Sto8: |
| 1379 | e2 = IRExpr_Const(IRConst_U8(toUChar( |
| 1380 | e->Iex.Unop.arg->Iex.Const.con->Ico.U1 |
| 1381 | ? 0xFF : 0))); |
| 1382 | break; |
sewardj | 68884ef | 2005-07-18 13:58:49 +0000 | [diff] [blame] | 1383 | case Iop_1Sto16: |
sewardj | 743d8f0 | 2005-07-27 00:22:37 +0000 | [diff] [blame] | 1384 | e2 = IRExpr_Const(IRConst_U16(toUShort( |
sewardj | 68884ef | 2005-07-18 13:58:49 +0000 | [diff] [blame] | 1385 | e->Iex.Unop.arg->Iex.Const.con->Ico.U1 |
sewardj | 743d8f0 | 2005-07-27 00:22:37 +0000 | [diff] [blame] | 1386 | ? 0xFFFF : 0))); |
sewardj | 68884ef | 2005-07-18 13:58:49 +0000 | [diff] [blame] | 1387 | break; |
sewardj | d999788 | 2004-11-04 19:42:59 +0000 | [diff] [blame] | 1388 | case Iop_1Sto32: |
| 1389 | e2 = IRExpr_Const(IRConst_U32( |
sewardj | ba99931 | 2004-11-15 15:21:17 +0000 | [diff] [blame] | 1390 | e->Iex.Unop.arg->Iex.Const.con->Ico.U1 |
sewardj | d999788 | 2004-11-04 19:42:59 +0000 | [diff] [blame] | 1391 | ? 0xFFFFFFFF : 0)); |
| 1392 | break; |
sewardj | e6b3993 | 2004-11-06 17:01:15 +0000 | [diff] [blame] | 1393 | case Iop_1Sto64: |
| 1394 | e2 = IRExpr_Const(IRConst_U64( |
sewardj | ba99931 | 2004-11-15 15:21:17 +0000 | [diff] [blame] | 1395 | e->Iex.Unop.arg->Iex.Const.con->Ico.U1 |
sewardj | e6b3993 | 2004-11-06 17:01:15 +0000 | [diff] [blame] | 1396 | ? 0xFFFFFFFFFFFFFFFFULL : 0)); |
| 1397 | break; |
| 1398 | |
sewardj | 883b00b | 2004-09-11 09:30:24 +0000 | [diff] [blame] | 1399 | case Iop_8Sto32: { |
| 1400 | /* signed */ Int s32 = e->Iex.Unop.arg->Iex.Const.con->Ico.U8; |
| 1401 | s32 <<= 24; |
| 1402 | s32 >>= 24; |
| 1403 | e2 = IRExpr_Const(IRConst_U32((UInt)s32)); |
| 1404 | break; |
| 1405 | } |
sewardj | 7f6330d | 2011-04-05 11:06:02 +0000 | [diff] [blame] | 1406 | case Iop_16Sto32: { |
| 1407 | /* signed */ Int s32 = e->Iex.Unop.arg->Iex.Const.con->Ico.U16; |
| 1408 | s32 <<= 16; |
| 1409 | s32 >>= 16; |
| 1410 | e2 = IRExpr_Const(IRConst_U32( (UInt)s32) ); |
| 1411 | break; |
| 1412 | } |
sewardj | 291a7e8 | 2005-04-27 11:42:44 +0000 | [diff] [blame] | 1413 | case Iop_8Uto64: |
| 1414 | e2 = IRExpr_Const(IRConst_U64( |
| 1415 | 0xFFULL & e->Iex.Unop.arg->Iex.Const.con->Ico.U8)); |
| 1416 | break; |
| 1417 | case Iop_16Uto64: |
| 1418 | e2 = IRExpr_Const(IRConst_U64( |
| 1419 | 0xFFFFULL & e->Iex.Unop.arg->Iex.Const.con->Ico.U16)); |
| 1420 | break; |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 1421 | case Iop_8Uto32: |
| 1422 | e2 = IRExpr_Const(IRConst_U32( |
| 1423 | 0xFF & e->Iex.Unop.arg->Iex.Const.con->Ico.U8)); |
| 1424 | break; |
sewardj | 7f6330d | 2011-04-05 11:06:02 +0000 | [diff] [blame] | 1425 | case Iop_8Sto16: { |
| 1426 | /* signed */ Short s16 = e->Iex.Unop.arg->Iex.Const.con->Ico.U8; |
| 1427 | s16 <<= 8; |
| 1428 | s16 >>= 8; |
| 1429 | e2 = IRExpr_Const(IRConst_U16( (UShort)s16) ); |
| 1430 | break; |
| 1431 | } |
| 1432 | case Iop_8Uto16: |
| 1433 | e2 = IRExpr_Const(IRConst_U16( |
| 1434 | 0xFF & e->Iex.Unop.arg->Iex.Const.con->Ico.U8)); |
| 1435 | break; |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 1436 | case Iop_16Uto32: |
| 1437 | e2 = IRExpr_Const(IRConst_U32( |
| 1438 | 0xFFFF & e->Iex.Unop.arg->Iex.Const.con->Ico.U16)); |
| 1439 | break; |
sewardj | 7301743 | 2004-10-14 19:33:25 +0000 | [diff] [blame] | 1440 | case Iop_32to16: |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1441 | e2 = IRExpr_Const(IRConst_U16(toUShort( |
| 1442 | 0xFFFF & e->Iex.Unop.arg->Iex.Const.con->Ico.U32))); |
sewardj | 7301743 | 2004-10-14 19:33:25 +0000 | [diff] [blame] | 1443 | break; |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 1444 | case Iop_32to8: |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1445 | e2 = IRExpr_Const(IRConst_U8(toUChar( |
| 1446 | 0xFF & e->Iex.Unop.arg->Iex.Const.con->Ico.U32))); |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 1447 | break; |
sewardj | 7447b5b | 2004-10-16 11:30:42 +0000 | [diff] [blame] | 1448 | case Iop_32to1: |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1449 | e2 = IRExpr_Const(IRConst_U1(toBool( |
| 1450 | 1 == (1 & e->Iex.Unop.arg->Iex.Const.con->Ico.U32) |
| 1451 | ))); |
sewardj | 7447b5b | 2004-10-16 11:30:42 +0000 | [diff] [blame] | 1452 | break; |
sewardj | 291a7e8 | 2005-04-27 11:42:44 +0000 | [diff] [blame] | 1453 | case Iop_64to1: |
| 1454 | e2 = IRExpr_Const(IRConst_U1(toBool( |
| 1455 | 1 == (1 & e->Iex.Unop.arg->Iex.Const.con->Ico.U64) |
| 1456 | ))); |
| 1457 | break; |
sewardj | e6b3993 | 2004-11-06 17:01:15 +0000 | [diff] [blame] | 1458 | |
sewardj | 4a0bee0 | 2012-06-29 14:44:44 +0000 | [diff] [blame] | 1459 | case Iop_NotV128: |
| 1460 | e2 = IRExpr_Const(IRConst_V128( |
| 1461 | ~ (e->Iex.Unop.arg->Iex.Const.con->Ico.V128))); |
| 1462 | break; |
sewardj | f057afb | 2005-02-27 13:35:41 +0000 | [diff] [blame] | 1463 | case Iop_Not64: |
| 1464 | e2 = IRExpr_Const(IRConst_U64( |
| 1465 | ~ (e->Iex.Unop.arg->Iex.Const.con->Ico.U64))); |
| 1466 | break; |
sewardj | 883b00b | 2004-09-11 09:30:24 +0000 | [diff] [blame] | 1467 | case Iop_Not32: |
| 1468 | e2 = IRExpr_Const(IRConst_U32( |
| 1469 | ~ (e->Iex.Unop.arg->Iex.Const.con->Ico.U32))); |
| 1470 | break; |
sewardj | e6b3993 | 2004-11-06 17:01:15 +0000 | [diff] [blame] | 1471 | case Iop_Not16: |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1472 | e2 = IRExpr_Const(IRConst_U16(toUShort( |
| 1473 | ~ (e->Iex.Unop.arg->Iex.Const.con->Ico.U16)))); |
sewardj | e6b3993 | 2004-11-06 17:01:15 +0000 | [diff] [blame] | 1474 | break; |
sewardj | d999788 | 2004-11-04 19:42:59 +0000 | [diff] [blame] | 1475 | case Iop_Not8: |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1476 | e2 = IRExpr_Const(IRConst_U8(toUChar( |
| 1477 | ~ (e->Iex.Unop.arg->Iex.Const.con->Ico.U8)))); |
sewardj | d999788 | 2004-11-04 19:42:59 +0000 | [diff] [blame] | 1478 | break; |
sewardj | e6b3993 | 2004-11-06 17:01:15 +0000 | [diff] [blame] | 1479 | |
sewardj | e1d45da | 2004-11-12 00:13:21 +0000 | [diff] [blame] | 1480 | case Iop_Not1: |
sewardj | ba99931 | 2004-11-15 15:21:17 +0000 | [diff] [blame] | 1481 | e2 = IRExpr_Const(IRConst_U1( |
| 1482 | notBool(e->Iex.Unop.arg->Iex.Const.con->Ico.U1))); |
sewardj | e1d45da | 2004-11-12 00:13:21 +0000 | [diff] [blame] | 1483 | break; |
| 1484 | |
sewardj | 291a7e8 | 2005-04-27 11:42:44 +0000 | [diff] [blame] | 1485 | case Iop_64to8: { |
| 1486 | ULong w64 = e->Iex.Unop.arg->Iex.Const.con->Ico.U64; |
| 1487 | w64 &= 0xFFULL; |
| 1488 | e2 = IRExpr_Const(IRConst_U8( (UChar)w64 )); |
| 1489 | break; |
| 1490 | } |
| 1491 | case Iop_64to16: { |
| 1492 | ULong w64 = e->Iex.Unop.arg->Iex.Const.con->Ico.U64; |
| 1493 | w64 &= 0xFFFFULL; |
sewardj | e85bc40 | 2005-05-06 16:29:26 +0000 | [diff] [blame] | 1494 | e2 = IRExpr_Const(IRConst_U16( (UShort)w64 )); |
sewardj | 291a7e8 | 2005-04-27 11:42:44 +0000 | [diff] [blame] | 1495 | break; |
| 1496 | } |
sewardj | 1d8ce20 | 2004-12-13 14:14:16 +0000 | [diff] [blame] | 1497 | case Iop_64to32: { |
| 1498 | ULong w64 = e->Iex.Unop.arg->Iex.Const.con->Ico.U64; |
| 1499 | w64 &= 0x00000000FFFFFFFFULL; |
| 1500 | e2 = IRExpr_Const(IRConst_U32( (UInt)w64 )); |
sewardj | 3701059 | 2004-12-13 10:47:15 +0000 | [diff] [blame] | 1501 | break; |
sewardj | 1d8ce20 | 2004-12-13 14:14:16 +0000 | [diff] [blame] | 1502 | } |
sewardj | 1d8ce20 | 2004-12-13 14:14:16 +0000 | [diff] [blame] | 1503 | case Iop_64HIto32: { |
| 1504 | ULong w64 = e->Iex.Unop.arg->Iex.Const.con->Ico.U64; |
| 1505 | w64 >>= 32; |
| 1506 | e2 = IRExpr_Const(IRConst_U32( (UInt)w64 )); |
| 1507 | break; |
| 1508 | } |
sewardj | b5710b8 | 2005-01-27 16:05:09 +0000 | [diff] [blame] | 1509 | case Iop_32Uto64: |
| 1510 | e2 = IRExpr_Const(IRConst_U64( |
| 1511 | 0xFFFFFFFFULL |
| 1512 | & e->Iex.Unop.arg->Iex.Const.con->Ico.U32)); |
| 1513 | break; |
sewardj | 7f6330d | 2011-04-05 11:06:02 +0000 | [diff] [blame] | 1514 | case Iop_16Sto64: { |
| 1515 | /* signed */ Long s64 = e->Iex.Unop.arg->Iex.Const.con->Ico.U16; |
| 1516 | s64 <<= 48; |
| 1517 | s64 >>= 48; |
| 1518 | e2 = IRExpr_Const(IRConst_U64((ULong)s64)); |
| 1519 | break; |
| 1520 | } |
sewardj | 287e9bb | 2010-07-29 16:12:41 +0000 | [diff] [blame] | 1521 | case Iop_32Sto64: { |
| 1522 | /* signed */ Long s64 = e->Iex.Unop.arg->Iex.Const.con->Ico.U32; |
| 1523 | s64 <<= 32; |
| 1524 | s64 >>= 32; |
| 1525 | e2 = IRExpr_Const(IRConst_U64((ULong)s64)); |
| 1526 | break; |
| 1527 | } |
sewardj | 7f6330d | 2011-04-05 11:06:02 +0000 | [diff] [blame] | 1528 | |
| 1529 | case Iop_16to8: { |
| 1530 | UShort w16 = e->Iex.Unop.arg->Iex.Const.con->Ico.U16; |
| 1531 | w16 &= 0xFF; |
| 1532 | e2 = IRExpr_Const(IRConst_U8( (UChar)w16 )); |
| 1533 | break; |
| 1534 | } |
| 1535 | case Iop_16HIto8: { |
| 1536 | UShort w16 = e->Iex.Unop.arg->Iex.Const.con->Ico.U16; |
| 1537 | w16 >>= 8; |
| 1538 | w16 &= 0xFF; |
| 1539 | e2 = IRExpr_Const(IRConst_U8( (UChar)w16 )); |
| 1540 | break; |
| 1541 | } |
| 1542 | |
sewardj | 0033ddc | 2005-04-26 23:34:34 +0000 | [diff] [blame] | 1543 | case Iop_CmpNEZ8: |
| 1544 | e2 = IRExpr_Const(IRConst_U1(toBool( |
| 1545 | 0 != |
| 1546 | (0xFF & e->Iex.Unop.arg->Iex.Const.con->Ico.U8) |
| 1547 | ))); |
| 1548 | break; |
| 1549 | case Iop_CmpNEZ32: |
| 1550 | e2 = IRExpr_Const(IRConst_U1(toBool( |
| 1551 | 0 != |
| 1552 | (0xFFFFFFFF & e->Iex.Unop.arg->Iex.Const.con->Ico.U32) |
| 1553 | ))); |
| 1554 | break; |
| 1555 | case Iop_CmpNEZ64: |
| 1556 | e2 = IRExpr_Const(IRConst_U1(toBool( |
| 1557 | 0ULL != e->Iex.Unop.arg->Iex.Const.con->Ico.U64 |
| 1558 | ))); |
| 1559 | break; |
| 1560 | |
sewardj | eb17e49 | 2007-08-25 23:07:44 +0000 | [diff] [blame] | 1561 | case Iop_CmpwNEZ32: { |
| 1562 | UInt w32 = e->Iex.Unop.arg->Iex.Const.con->Ico.U32; |
| 1563 | if (w32 == 0) |
| 1564 | e2 = IRExpr_Const(IRConst_U32( 0 )); |
| 1565 | else |
| 1566 | e2 = IRExpr_Const(IRConst_U32( 0xFFFFFFFF )); |
| 1567 | break; |
| 1568 | } |
| 1569 | case Iop_CmpwNEZ64: { |
| 1570 | ULong w64 = e->Iex.Unop.arg->Iex.Const.con->Ico.U64; |
| 1571 | if (w64 == 0) |
| 1572 | e2 = IRExpr_Const(IRConst_U64( 0 )); |
| 1573 | else |
| 1574 | e2 = IRExpr_Const(IRConst_U64( 0xFFFFFFFFFFFFFFFFULL )); |
| 1575 | break; |
| 1576 | } |
| 1577 | |
| 1578 | case Iop_Left32: { |
| 1579 | UInt u32 = e->Iex.Unop.arg->Iex.Const.con->Ico.U32; |
| 1580 | Int s32 = (Int)(u32 & 0xFFFFFFFF); |
| 1581 | s32 = (s32 | (-s32)); |
| 1582 | e2 = IRExpr_Const( IRConst_U32( (UInt)s32 )); |
| 1583 | break; |
| 1584 | } |
| 1585 | |
| 1586 | case Iop_Left64: { |
| 1587 | ULong u64 = e->Iex.Unop.arg->Iex.Const.con->Ico.U64; |
| 1588 | Long s64 = (Long)u64; |
| 1589 | s64 = (s64 | (-s64)); |
| 1590 | e2 = IRExpr_Const( IRConst_U64( (ULong)s64 )); |
| 1591 | break; |
| 1592 | } |
| 1593 | |
sewardj | 4cba9f4 | 2011-03-07 18:34:34 +0000 | [diff] [blame] | 1594 | case Iop_Clz32: { |
| 1595 | UInt u32 = e->Iex.Unop.arg->Iex.Const.con->Ico.U32; |
| 1596 | if (u32 != 0) |
| 1597 | e2 = IRExpr_Const(IRConst_U32(fold_Clz32(u32))); |
| 1598 | break; |
| 1599 | } |
| 1600 | case Iop_Clz64: { |
| 1601 | ULong u64 = e->Iex.Unop.arg->Iex.Const.con->Ico.U64; |
| 1602 | if (u64 != 0ULL) |
| 1603 | e2 = IRExpr_Const(IRConst_U64(fold_Clz64(u64))); |
| 1604 | break; |
| 1605 | } |
| 1606 | |
sewardj | 4a0bee0 | 2012-06-29 14:44:44 +0000 | [diff] [blame] | 1607 | /* For these vector ones, can't fold all cases, but at least |
| 1608 | do the most obvious one. Could do better here using |
| 1609 | summarise/desummarise of vector constants, but too |
| 1610 | difficult to verify; hence just handle the zero cases. */ |
| 1611 | case Iop_32UtoV128: { |
| 1612 | UInt u32 = e->Iex.Unop.arg->Iex.Const.con->Ico.U32; |
| 1613 | if (0 == u32) { |
| 1614 | e2 = IRExpr_Const(IRConst_V128(0x0000)); |
| 1615 | } else { |
| 1616 | goto unhandled; |
| 1617 | } |
| 1618 | break; |
| 1619 | } |
| 1620 | case Iop_V128to64: { |
| 1621 | UShort v128 = e->Iex.Unop.arg->Iex.Const.con->Ico.V128; |
| 1622 | if (0 == ((v128 >> 0) & 0xFF)) { |
| 1623 | e2 = IRExpr_Const(IRConst_U64(0)); |
| 1624 | } else { |
| 1625 | goto unhandled; |
| 1626 | } |
| 1627 | break; |
| 1628 | } |
| 1629 | case Iop_V128HIto64: { |
| 1630 | UShort v128 = e->Iex.Unop.arg->Iex.Const.con->Ico.V128; |
| 1631 | if (0 == ((v128 >> 8) & 0xFF)) { |
| 1632 | e2 = IRExpr_Const(IRConst_U64(0)); |
| 1633 | } else { |
| 1634 | goto unhandled; |
| 1635 | } |
| 1636 | break; |
| 1637 | } |
| 1638 | case Iop_64UtoV128: { |
| 1639 | ULong u64 = e->Iex.Unop.arg->Iex.Const.con->Ico.U64; |
| 1640 | if (0 == u64) { |
| 1641 | e2 = IRExpr_Const(IRConst_V128(0x0000)); |
| 1642 | } else { |
| 1643 | goto unhandled; |
| 1644 | } |
| 1645 | break; |
| 1646 | } |
| 1647 | |
sewardj | ffccf2b | 2012-06-29 15:33:09 +0000 | [diff] [blame] | 1648 | /* Even stupider (although still correct ..) */ |
| 1649 | case Iop_V256to64_0: case Iop_V256to64_1: |
| 1650 | case Iop_V256to64_2: case Iop_V256to64_3: { |
| 1651 | UInt v256 = e->Iex.Unop.arg->Iex.Const.con->Ico.V256; |
| 1652 | if (v256 == 0x00000000) { |
| 1653 | e2 = IRExpr_Const(IRConst_U64(0)); |
| 1654 | } else { |
| 1655 | goto unhandled; |
| 1656 | } |
| 1657 | break; |
| 1658 | } |
| 1659 | |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 1660 | default: |
| 1661 | goto unhandled; |
| 1662 | } |
florian | 708417d | 2012-02-15 00:43:36 +0000 | [diff] [blame] | 1663 | } |
| 1664 | break; |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 1665 | |
florian | 708417d | 2012-02-15 00:43:36 +0000 | [diff] [blame] | 1666 | case Iex_Binop: |
| 1667 | /* BINARY ops */ |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 1668 | if (e->Iex.Binop.arg1->tag == Iex_Const |
| 1669 | && e->Iex.Binop.arg2->tag == Iex_Const) { |
| 1670 | /* cases where both args are consts */ |
| 1671 | switch (e->Iex.Binop.op) { |
sewardj | e6b3993 | 2004-11-06 17:01:15 +0000 | [diff] [blame] | 1672 | |
sewardj | 855dc72 | 2005-02-17 09:26:05 +0000 | [diff] [blame] | 1673 | /* -- Or -- */ |
sewardj | d999788 | 2004-11-04 19:42:59 +0000 | [diff] [blame] | 1674 | case Iop_Or8: |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1675 | e2 = IRExpr_Const(IRConst_U8(toUChar( |
sewardj | d999788 | 2004-11-04 19:42:59 +0000 | [diff] [blame] | 1676 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U8 |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1677 | | e->Iex.Binop.arg2->Iex.Const.con->Ico.U8)))); |
sewardj | d999788 | 2004-11-04 19:42:59 +0000 | [diff] [blame] | 1678 | break; |
sewardj | e6b3993 | 2004-11-06 17:01:15 +0000 | [diff] [blame] | 1679 | case Iop_Or16: |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1680 | e2 = IRExpr_Const(IRConst_U16(toUShort( |
sewardj | e6b3993 | 2004-11-06 17:01:15 +0000 | [diff] [blame] | 1681 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U16 |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1682 | | e->Iex.Binop.arg2->Iex.Const.con->Ico.U16)))); |
sewardj | e6b3993 | 2004-11-06 17:01:15 +0000 | [diff] [blame] | 1683 | break; |
| 1684 | case Iop_Or32: |
| 1685 | e2 = IRExpr_Const(IRConst_U32( |
| 1686 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U32 |
| 1687 | | e->Iex.Binop.arg2->Iex.Const.con->Ico.U32))); |
| 1688 | break; |
sewardj | f057afb | 2005-02-27 13:35:41 +0000 | [diff] [blame] | 1689 | case Iop_Or64: |
| 1690 | e2 = IRExpr_Const(IRConst_U64( |
| 1691 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U64 |
| 1692 | | e->Iex.Binop.arg2->Iex.Const.con->Ico.U64))); |
| 1693 | break; |
sewardj | 4a0bee0 | 2012-06-29 14:44:44 +0000 | [diff] [blame] | 1694 | case Iop_OrV128: |
| 1695 | e2 = IRExpr_Const(IRConst_V128( |
| 1696 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.V128 |
| 1697 | | e->Iex.Binop.arg2->Iex.Const.con->Ico.V128))); |
| 1698 | break; |
sewardj | e6b3993 | 2004-11-06 17:01:15 +0000 | [diff] [blame] | 1699 | |
sewardj | 855dc72 | 2005-02-17 09:26:05 +0000 | [diff] [blame] | 1700 | /* -- Xor -- */ |
sewardj | 883b00b | 2004-09-11 09:30:24 +0000 | [diff] [blame] | 1701 | case Iop_Xor8: |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1702 | e2 = IRExpr_Const(IRConst_U8(toUChar( |
sewardj | 883b00b | 2004-09-11 09:30:24 +0000 | [diff] [blame] | 1703 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U8 |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1704 | ^ e->Iex.Binop.arg2->Iex.Const.con->Ico.U8)))); |
sewardj | 883b00b | 2004-09-11 09:30:24 +0000 | [diff] [blame] | 1705 | break; |
sewardj | 82c9f2f | 2005-03-02 16:05:13 +0000 | [diff] [blame] | 1706 | case Iop_Xor16: |
sewardj | c7c098f | 2005-03-21 01:06:20 +0000 | [diff] [blame] | 1707 | e2 = IRExpr_Const(IRConst_U16(toUShort( |
sewardj | 82c9f2f | 2005-03-02 16:05:13 +0000 | [diff] [blame] | 1708 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U16 |
sewardj | c7c098f | 2005-03-21 01:06:20 +0000 | [diff] [blame] | 1709 | ^ e->Iex.Binop.arg2->Iex.Const.con->Ico.U16)))); |
sewardj | 82c9f2f | 2005-03-02 16:05:13 +0000 | [diff] [blame] | 1710 | break; |
sewardj | 855dc72 | 2005-02-17 09:26:05 +0000 | [diff] [blame] | 1711 | case Iop_Xor32: |
| 1712 | e2 = IRExpr_Const(IRConst_U32( |
| 1713 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U32 |
| 1714 | ^ e->Iex.Binop.arg2->Iex.Const.con->Ico.U32))); |
| 1715 | break; |
sewardj | f057afb | 2005-02-27 13:35:41 +0000 | [diff] [blame] | 1716 | case Iop_Xor64: |
| 1717 | e2 = IRExpr_Const(IRConst_U64( |
| 1718 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U64 |
| 1719 | ^ e->Iex.Binop.arg2->Iex.Const.con->Ico.U64))); |
| 1720 | break; |
sewardj | 4a0bee0 | 2012-06-29 14:44:44 +0000 | [diff] [blame] | 1721 | case Iop_XorV128: |
| 1722 | e2 = IRExpr_Const(IRConst_V128( |
| 1723 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.V128 |
| 1724 | ^ e->Iex.Binop.arg2->Iex.Const.con->Ico.V128))); |
| 1725 | break; |
sewardj | 855dc72 | 2005-02-17 09:26:05 +0000 | [diff] [blame] | 1726 | |
| 1727 | /* -- And -- */ |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 1728 | case Iop_And8: |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1729 | e2 = IRExpr_Const(IRConst_U8(toUChar( |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 1730 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U8 |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1731 | & e->Iex.Binop.arg2->Iex.Const.con->Ico.U8)))); |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 1732 | break; |
sewardj | 7f6330d | 2011-04-05 11:06:02 +0000 | [diff] [blame] | 1733 | case Iop_And16: |
| 1734 | e2 = IRExpr_Const(IRConst_U16(toUShort( |
| 1735 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U16 |
| 1736 | & e->Iex.Binop.arg2->Iex.Const.con->Ico.U16)))); |
| 1737 | break; |
sewardj | 855dc72 | 2005-02-17 09:26:05 +0000 | [diff] [blame] | 1738 | case Iop_And32: |
| 1739 | e2 = IRExpr_Const(IRConst_U32( |
| 1740 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U32 |
| 1741 | & e->Iex.Binop.arg2->Iex.Const.con->Ico.U32))); |
| 1742 | break; |
| 1743 | case Iop_And64: |
| 1744 | e2 = IRExpr_Const(IRConst_U64( |
| 1745 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U64 |
| 1746 | & e->Iex.Binop.arg2->Iex.Const.con->Ico.U64))); |
| 1747 | break; |
sewardj | 4a0bee0 | 2012-06-29 14:44:44 +0000 | [diff] [blame] | 1748 | case Iop_AndV128: |
| 1749 | e2 = IRExpr_Const(IRConst_V128( |
| 1750 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.V128 |
| 1751 | & e->Iex.Binop.arg2->Iex.Const.con->Ico.V128))); |
| 1752 | break; |
sewardj | 855dc72 | 2005-02-17 09:26:05 +0000 | [diff] [blame] | 1753 | |
| 1754 | /* -- Add -- */ |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 1755 | case Iop_Add8: |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1756 | e2 = IRExpr_Const(IRConst_U8(toUChar( |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 1757 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U8 |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1758 | + e->Iex.Binop.arg2->Iex.Const.con->Ico.U8)))); |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 1759 | break; |
sewardj | 855dc72 | 2005-02-17 09:26:05 +0000 | [diff] [blame] | 1760 | case Iop_Add32: |
| 1761 | e2 = IRExpr_Const(IRConst_U32( |
| 1762 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U32 |
| 1763 | + e->Iex.Binop.arg2->Iex.Const.con->Ico.U32))); |
| 1764 | break; |
| 1765 | case Iop_Add64: |
| 1766 | e2 = IRExpr_Const(IRConst_U64( |
| 1767 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U64 |
| 1768 | + e->Iex.Binop.arg2->Iex.Const.con->Ico.U64))); |
| 1769 | break; |
| 1770 | |
| 1771 | /* -- Sub -- */ |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 1772 | case Iop_Sub8: |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1773 | e2 = IRExpr_Const(IRConst_U8(toUChar( |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 1774 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U8 |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1775 | - e->Iex.Binop.arg2->Iex.Const.con->Ico.U8)))); |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 1776 | break; |
sewardj | d721703 | 2004-08-19 10:49:10 +0000 | [diff] [blame] | 1777 | case Iop_Sub32: |
| 1778 | e2 = IRExpr_Const(IRConst_U32( |
| 1779 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U32 |
| 1780 | - e->Iex.Binop.arg2->Iex.Const.con->Ico.U32))); |
| 1781 | break; |
sewardj | 70a8ddf | 2005-02-13 02:24:26 +0000 | [diff] [blame] | 1782 | case Iop_Sub64: |
| 1783 | e2 = IRExpr_Const(IRConst_U64( |
| 1784 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U64 |
| 1785 | - e->Iex.Binop.arg2->Iex.Const.con->Ico.U64))); |
| 1786 | break; |
sewardj | c2bcb6f | 2005-02-07 00:17:12 +0000 | [diff] [blame] | 1787 | |
sewardj | 478646f | 2008-05-01 20:13:04 +0000 | [diff] [blame] | 1788 | /* -- Max32U -- */ |
| 1789 | case Iop_Max32U: { |
| 1790 | UInt u32a = e->Iex.Binop.arg1->Iex.Const.con->Ico.U32; |
| 1791 | UInt u32b = e->Iex.Binop.arg2->Iex.Const.con->Ico.U32; |
| 1792 | UInt res = u32a > u32b ? u32a : u32b; |
| 1793 | e2 = IRExpr_Const(IRConst_U32(res)); |
| 1794 | break; |
| 1795 | } |
| 1796 | |
sewardj | 855dc72 | 2005-02-17 09:26:05 +0000 | [diff] [blame] | 1797 | /* -- Mul -- */ |
sewardj | b9c5cf6 | 2004-08-24 15:10:38 +0000 | [diff] [blame] | 1798 | case Iop_Mul32: |
| 1799 | e2 = IRExpr_Const(IRConst_U32( |
| 1800 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U32 |
| 1801 | * e->Iex.Binop.arg2->Iex.Const.con->Ico.U32))); |
| 1802 | break; |
sewardj | a34c071 | 2005-03-30 23:19:46 +0000 | [diff] [blame] | 1803 | case Iop_Mul64: |
| 1804 | e2 = IRExpr_Const(IRConst_U64( |
| 1805 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U64 |
| 1806 | * e->Iex.Binop.arg2->Iex.Const.con->Ico.U64))); |
| 1807 | break; |
| 1808 | |
sewardj | ea6bccb | 2005-03-01 10:19:23 +0000 | [diff] [blame] | 1809 | case Iop_MullS32: { |
| 1810 | /* very paranoid */ |
| 1811 | UInt u32a = e->Iex.Binop.arg1->Iex.Const.con->Ico.U32; |
| 1812 | UInt u32b = e->Iex.Binop.arg2->Iex.Const.con->Ico.U32; |
| 1813 | Int s32a = (Int)u32a; |
| 1814 | Int s32b = (Int)u32b; |
| 1815 | Long s64a = (Long)s32a; |
| 1816 | Long s64b = (Long)s32b; |
| 1817 | Long sres = s64a * s64b; |
| 1818 | ULong ures = (ULong)sres; |
| 1819 | e2 = IRExpr_Const(IRConst_U64(ures)); |
| 1820 | break; |
| 1821 | } |
sewardj | b095fba | 2005-02-13 14:13:04 +0000 | [diff] [blame] | 1822 | |
sewardj | 855dc72 | 2005-02-17 09:26:05 +0000 | [diff] [blame] | 1823 | /* -- Shl -- */ |
sewardj | d721703 | 2004-08-19 10:49:10 +0000 | [diff] [blame] | 1824 | case Iop_Shl32: |
sewardj | 6134847 | 2004-08-20 01:01:04 +0000 | [diff] [blame] | 1825 | vassert(e->Iex.Binop.arg2->Iex.Const.con->tag == Ico_U8); |
| 1826 | shift = (Int)(e->Iex.Binop.arg2->Iex.Const.con->Ico.U8); |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 1827 | if (shift >= 0 && shift <= 31) |
sewardj | 278c44c | 2004-08-20 00:28:13 +0000 | [diff] [blame] | 1828 | e2 = IRExpr_Const(IRConst_U32( |
| 1829 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U32 |
| 1830 | << shift))); |
sewardj | d721703 | 2004-08-19 10:49:10 +0000 | [diff] [blame] | 1831 | break; |
sewardj | b095fba | 2005-02-13 14:13:04 +0000 | [diff] [blame] | 1832 | case Iop_Shl64: |
| 1833 | vassert(e->Iex.Binop.arg2->Iex.Const.con->tag == Ico_U8); |
| 1834 | shift = (Int)(e->Iex.Binop.arg2->Iex.Const.con->Ico.U8); |
| 1835 | if (shift >= 0 && shift <= 63) |
| 1836 | e2 = IRExpr_Const(IRConst_U64( |
| 1837 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U64 |
| 1838 | << shift))); |
| 1839 | break; |
| 1840 | |
sewardj | 855dc72 | 2005-02-17 09:26:05 +0000 | [diff] [blame] | 1841 | /* -- Sar -- */ |
sewardj | 278c44c | 2004-08-20 00:28:13 +0000 | [diff] [blame] | 1842 | case Iop_Sar32: { |
| 1843 | /* paranoid ... */ |
| 1844 | /*signed*/ Int s32; |
sewardj | 6134847 | 2004-08-20 01:01:04 +0000 | [diff] [blame] | 1845 | vassert(e->Iex.Binop.arg2->Iex.Const.con->tag == Ico_U8); |
sewardj | 278c44c | 2004-08-20 00:28:13 +0000 | [diff] [blame] | 1846 | s32 = (Int)(e->Iex.Binop.arg1->Iex.Const.con->Ico.U32); |
sewardj | 6134847 | 2004-08-20 01:01:04 +0000 | [diff] [blame] | 1847 | shift = (Int)(e->Iex.Binop.arg2->Iex.Const.con->Ico.U8); |
sewardj | 278c44c | 2004-08-20 00:28:13 +0000 | [diff] [blame] | 1848 | if (shift >= 0 && shift <= 31) { |
| 1849 | s32 >>=/*signed*/ shift; |
| 1850 | e2 = IRExpr_Const(IRConst_U32((UInt)s32)); |
| 1851 | } |
| 1852 | break; |
| 1853 | } |
sewardj | 855dc72 | 2005-02-17 09:26:05 +0000 | [diff] [blame] | 1854 | case Iop_Sar64: { |
| 1855 | /* paranoid ... */ |
| 1856 | /*signed*/ Long s64; |
| 1857 | vassert(e->Iex.Binop.arg2->Iex.Const.con->tag == Ico_U8); |
| 1858 | s64 = (Long)(e->Iex.Binop.arg1->Iex.Const.con->Ico.U64); |
| 1859 | shift = (Int)(e->Iex.Binop.arg2->Iex.Const.con->Ico.U8); |
| 1860 | if (shift >= 0 && shift <= 63) { |
| 1861 | s64 >>=/*signed*/ shift; |
| 1862 | e2 = IRExpr_Const(IRConst_U64((ULong)s64)); |
| 1863 | } |
| 1864 | break; |
| 1865 | } |
| 1866 | |
| 1867 | /* -- Shr -- */ |
sewardj | 6134847 | 2004-08-20 01:01:04 +0000 | [diff] [blame] | 1868 | case Iop_Shr32: { |
| 1869 | /* paranoid ... */ |
sewardj | 4add714 | 2005-02-21 08:20:22 +0000 | [diff] [blame] | 1870 | /*unsigned*/ UInt u32; |
sewardj | 6134847 | 2004-08-20 01:01:04 +0000 | [diff] [blame] | 1871 | vassert(e->Iex.Binop.arg2->Iex.Const.con->tag == Ico_U8); |
sewardj | 4add714 | 2005-02-21 08:20:22 +0000 | [diff] [blame] | 1872 | u32 = (UInt)(e->Iex.Binop.arg1->Iex.Const.con->Ico.U32); |
sewardj | 6134847 | 2004-08-20 01:01:04 +0000 | [diff] [blame] | 1873 | shift = (Int)(e->Iex.Binop.arg2->Iex.Const.con->Ico.U8); |
| 1874 | if (shift >= 0 && shift <= 31) { |
sewardj | 4add714 | 2005-02-21 08:20:22 +0000 | [diff] [blame] | 1875 | u32 >>=/*unsigned*/ shift; |
| 1876 | e2 = IRExpr_Const(IRConst_U32(u32)); |
| 1877 | } |
| 1878 | break; |
| 1879 | } |
| 1880 | case Iop_Shr64: { |
| 1881 | /* paranoid ... */ |
| 1882 | /*unsigned*/ ULong u64; |
| 1883 | vassert(e->Iex.Binop.arg2->Iex.Const.con->tag == Ico_U8); |
| 1884 | u64 = (ULong)(e->Iex.Binop.arg1->Iex.Const.con->Ico.U64); |
| 1885 | shift = (Int)(e->Iex.Binop.arg2->Iex.Const.con->Ico.U8); |
| 1886 | if (shift >= 0 && shift <= 63) { |
| 1887 | u64 >>=/*unsigned*/ shift; |
| 1888 | e2 = IRExpr_Const(IRConst_U64(u64)); |
sewardj | 6134847 | 2004-08-20 01:01:04 +0000 | [diff] [blame] | 1889 | } |
| 1890 | break; |
| 1891 | } |
sewardj | 855dc72 | 2005-02-17 09:26:05 +0000 | [diff] [blame] | 1892 | |
| 1893 | /* -- CmpEQ -- */ |
sewardj | b8e7586 | 2004-08-19 17:58:45 +0000 | [diff] [blame] | 1894 | case Iop_CmpEQ32: |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1895 | e2 = IRExpr_Const(IRConst_U1(toBool( |
sewardj | b8e7586 | 2004-08-19 17:58:45 +0000 | [diff] [blame] | 1896 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U32 |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1897 | == e->Iex.Binop.arg2->Iex.Const.con->Ico.U32)))); |
sewardj | b8e7586 | 2004-08-19 17:58:45 +0000 | [diff] [blame] | 1898 | break; |
sewardj | 855dc72 | 2005-02-17 09:26:05 +0000 | [diff] [blame] | 1899 | case Iop_CmpEQ64: |
| 1900 | e2 = IRExpr_Const(IRConst_U1(toBool( |
| 1901 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U64 |
| 1902 | == e->Iex.Binop.arg2->Iex.Const.con->Ico.U64)))); |
| 1903 | break; |
| 1904 | |
| 1905 | /* -- CmpNE -- */ |
| 1906 | case Iop_CmpNE8: |
sewardj | e13074c | 2012-11-08 10:57:08 +0000 | [diff] [blame] | 1907 | case Iop_CasCmpNE8: |
| 1908 | case Iop_ExpCmpNE8: |
sewardj | 855dc72 | 2005-02-17 09:26:05 +0000 | [diff] [blame] | 1909 | e2 = IRExpr_Const(IRConst_U1(toBool( |
| 1910 | ((0xFF & e->Iex.Binop.arg1->Iex.Const.con->Ico.U8) |
| 1911 | != (0xFF & e->Iex.Binop.arg2->Iex.Const.con->Ico.U8))))); |
| 1912 | break; |
sewardj | ae27ab6 | 2004-10-15 21:21:46 +0000 | [diff] [blame] | 1913 | case Iop_CmpNE32: |
sewardj | e13074c | 2012-11-08 10:57:08 +0000 | [diff] [blame] | 1914 | case Iop_CasCmpNE32: |
| 1915 | case Iop_ExpCmpNE32: |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1916 | e2 = IRExpr_Const(IRConst_U1(toBool( |
sewardj | ae27ab6 | 2004-10-15 21:21:46 +0000 | [diff] [blame] | 1917 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U32 |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1918 | != e->Iex.Binop.arg2->Iex.Const.con->Ico.U32)))); |
sewardj | ae27ab6 | 2004-10-15 21:21:46 +0000 | [diff] [blame] | 1919 | break; |
sewardj | e6b3993 | 2004-11-06 17:01:15 +0000 | [diff] [blame] | 1920 | case Iop_CmpNE64: |
sewardj | e13074c | 2012-11-08 10:57:08 +0000 | [diff] [blame] | 1921 | case Iop_CasCmpNE64: |
| 1922 | case Iop_ExpCmpNE64: |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1923 | e2 = IRExpr_Const(IRConst_U1(toBool( |
sewardj | e6b3993 | 2004-11-06 17:01:15 +0000 | [diff] [blame] | 1924 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U64 |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1925 | != e->Iex.Binop.arg2->Iex.Const.con->Ico.U64)))); |
sewardj | e6b3993 | 2004-11-06 17:01:15 +0000 | [diff] [blame] | 1926 | break; |
| 1927 | |
sewardj | 855dc72 | 2005-02-17 09:26:05 +0000 | [diff] [blame] | 1928 | /* -- CmpLEU -- */ |
sewardj | 7447b5b | 2004-10-16 11:30:42 +0000 | [diff] [blame] | 1929 | case Iop_CmpLE32U: |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1930 | e2 = IRExpr_Const(IRConst_U1(toBool( |
sewardj | 7447b5b | 2004-10-16 11:30:42 +0000 | [diff] [blame] | 1931 | ((UInt)(e->Iex.Binop.arg1->Iex.Const.con->Ico.U32) |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1932 | <= (UInt)(e->Iex.Binop.arg2->Iex.Const.con->Ico.U32))))); |
sewardj | 7447b5b | 2004-10-16 11:30:42 +0000 | [diff] [blame] | 1933 | break; |
sewardj | 7f6330d | 2011-04-05 11:06:02 +0000 | [diff] [blame] | 1934 | case Iop_CmpLE64U: |
| 1935 | e2 = IRExpr_Const(IRConst_U1(toBool( |
| 1936 | ((ULong)(e->Iex.Binop.arg1->Iex.Const.con->Ico.U64) |
| 1937 | <= (ULong)(e->Iex.Binop.arg2->Iex.Const.con->Ico.U64))))); |
| 1938 | break; |
sewardj | 855dc72 | 2005-02-17 09:26:05 +0000 | [diff] [blame] | 1939 | |
| 1940 | /* -- CmpLES -- */ |
sewardj | 088e4f7 | 2004-10-19 01:25:02 +0000 | [diff] [blame] | 1941 | case Iop_CmpLE32S: |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1942 | e2 = IRExpr_Const(IRConst_U1(toBool( |
sewardj | 088e4f7 | 2004-10-19 01:25:02 +0000 | [diff] [blame] | 1943 | ((Int)(e->Iex.Binop.arg1->Iex.Const.con->Ico.U32) |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1944 | <= (Int)(e->Iex.Binop.arg2->Iex.Const.con->Ico.U32))))); |
sewardj | 088e4f7 | 2004-10-19 01:25:02 +0000 | [diff] [blame] | 1945 | break; |
sewardj | 7f6330d | 2011-04-05 11:06:02 +0000 | [diff] [blame] | 1946 | case Iop_CmpLE64S: |
| 1947 | e2 = IRExpr_Const(IRConst_U1(toBool( |
| 1948 | ((Long)(e->Iex.Binop.arg1->Iex.Const.con->Ico.U64) |
| 1949 | <= (Long)(e->Iex.Binop.arg2->Iex.Const.con->Ico.U64))))); |
| 1950 | break; |
sewardj | e1d45da | 2004-11-12 00:13:21 +0000 | [diff] [blame] | 1951 | |
sewardj | 855dc72 | 2005-02-17 09:26:05 +0000 | [diff] [blame] | 1952 | /* -- CmpLTS -- */ |
sewardj | 9bdd265 | 2004-10-19 12:56:33 +0000 | [diff] [blame] | 1953 | case Iop_CmpLT32S: |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1954 | e2 = IRExpr_Const(IRConst_U1(toBool( |
sewardj | 9bdd265 | 2004-10-19 12:56:33 +0000 | [diff] [blame] | 1955 | ((Int)(e->Iex.Binop.arg1->Iex.Const.con->Ico.U32) |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1956 | < (Int)(e->Iex.Binop.arg2->Iex.Const.con->Ico.U32))))); |
sewardj | 9bdd265 | 2004-10-19 12:56:33 +0000 | [diff] [blame] | 1957 | break; |
sewardj | 7f6330d | 2011-04-05 11:06:02 +0000 | [diff] [blame] | 1958 | case Iop_CmpLT64S: |
| 1959 | e2 = IRExpr_Const(IRConst_U1(toBool( |
| 1960 | ((Long)(e->Iex.Binop.arg1->Iex.Const.con->Ico.U64) |
| 1961 | < (Long)(e->Iex.Binop.arg2->Iex.Const.con->Ico.U64))))); |
| 1962 | break; |
sewardj | 855dc72 | 2005-02-17 09:26:05 +0000 | [diff] [blame] | 1963 | |
| 1964 | /* -- CmpLTU -- */ |
sewardj | e1d45da | 2004-11-12 00:13:21 +0000 | [diff] [blame] | 1965 | case Iop_CmpLT32U: |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1966 | e2 = IRExpr_Const(IRConst_U1(toBool( |
sewardj | e1d45da | 2004-11-12 00:13:21 +0000 | [diff] [blame] | 1967 | ((UInt)(e->Iex.Binop.arg1->Iex.Const.con->Ico.U32) |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1968 | < (UInt)(e->Iex.Binop.arg2->Iex.Const.con->Ico.U32))))); |
sewardj | e1d45da | 2004-11-12 00:13:21 +0000 | [diff] [blame] | 1969 | break; |
sewardj | 7f6330d | 2011-04-05 11:06:02 +0000 | [diff] [blame] | 1970 | case Iop_CmpLT64U: |
| 1971 | e2 = IRExpr_Const(IRConst_U1(toBool( |
| 1972 | ((ULong)(e->Iex.Binop.arg1->Iex.Const.con->Ico.U64) |
| 1973 | < (ULong)(e->Iex.Binop.arg2->Iex.Const.con->Ico.U64))))); |
| 1974 | break; |
sewardj | 7447b5b | 2004-10-16 11:30:42 +0000 | [diff] [blame] | 1975 | |
sewardj | b51f0f4 | 2005-07-18 11:38:02 +0000 | [diff] [blame] | 1976 | /* -- CmpORD -- */ |
| 1977 | case Iop_CmpORD32S: { |
| 1978 | /* very paranoid */ |
| 1979 | UInt u32a = e->Iex.Binop.arg1->Iex.Const.con->Ico.U32; |
| 1980 | UInt u32b = e->Iex.Binop.arg2->Iex.Const.con->Ico.U32; |
| 1981 | Int s32a = (Int)u32a; |
| 1982 | Int s32b = (Int)u32b; |
| 1983 | Int r = 0x2; /* EQ */ |
| 1984 | if (s32a < s32b) { |
| 1985 | r = 0x8; /* LT */ |
| 1986 | } |
| 1987 | else if (s32a > s32b) { |
| 1988 | r = 0x4; /* GT */ |
| 1989 | } |
| 1990 | e2 = IRExpr_Const(IRConst_U32(r)); |
| 1991 | break; |
| 1992 | } |
| 1993 | |
sewardj | 855dc72 | 2005-02-17 09:26:05 +0000 | [diff] [blame] | 1994 | /* -- nHLto2n -- */ |
sewardj | 088bcb4 | 2004-08-19 17:16:52 +0000 | [diff] [blame] | 1995 | case Iop_32HLto64: |
| 1996 | e2 = IRExpr_Const(IRConst_U64( |
sewardj | 4a0bee0 | 2012-06-29 14:44:44 +0000 | [diff] [blame] | 1997 | (((ULong)(e->Iex.Binop.arg1 |
| 1998 | ->Iex.Const.con->Ico.U32)) << 32) |
sewardj | 088bcb4 | 2004-08-19 17:16:52 +0000 | [diff] [blame] | 1999 | | ((ULong)(e->Iex.Binop.arg2->Iex.Const.con->Ico.U32)) |
| 2000 | )); |
| 2001 | break; |
sewardj | 3bc8a59 | 2005-05-02 10:47:22 +0000 | [diff] [blame] | 2002 | case Iop_64HLto128: |
| 2003 | /* We can't fold this, because there is no way to |
| 2004 | express he result in IR, but at least pretend to |
| 2005 | handle it, so as to stop getting blasted with |
| 2006 | no-rule-for-this-primop messages. */ |
| 2007 | break; |
sewardj | 4a0bee0 | 2012-06-29 14:44:44 +0000 | [diff] [blame] | 2008 | /* For this vector one, can't fold all cases, but at |
| 2009 | least do the most obvious one. Could do better here |
| 2010 | using summarise/desummarise of vector constants, but |
| 2011 | too difficult to verify; hence just handle the zero |
| 2012 | cases. */ |
| 2013 | case Iop_64HLtoV128: { |
| 2014 | ULong argHi = e->Iex.Binop.arg1->Iex.Const.con->Ico.U64; |
| 2015 | ULong argLo = e->Iex.Binop.arg2->Iex.Const.con->Ico.U64; |
| 2016 | if (0 == argHi && 0 == argLo) { |
| 2017 | e2 = IRExpr_Const(IRConst_V128(0)); |
| 2018 | } else { |
| 2019 | goto unhandled; |
| 2020 | } |
| 2021 | break; |
| 2022 | } |
sewardj | 9571dc0 | 2014-01-26 18:34:23 +0000 | [diff] [blame] | 2023 | /* Same reasoning for the 256-bit version. */ |
| 2024 | case Iop_V128HLtoV256: { |
| 2025 | IRExpr* argHi = e->Iex.Binop.arg1; |
| 2026 | IRExpr* argLo = e->Iex.Binop.arg2; |
| 2027 | if (isZeroV128(argHi) && isZeroV128(argLo)) { |
| 2028 | e2 = IRExpr_Const(IRConst_V256(0)); |
| 2029 | } else { |
| 2030 | goto unhandled; |
| 2031 | } |
| 2032 | break; |
| 2033 | } |
sewardj | 4a0bee0 | 2012-06-29 14:44:44 +0000 | [diff] [blame] | 2034 | |
| 2035 | /* -- V128 stuff -- */ |
| 2036 | case Iop_InterleaveLO8x16: { |
| 2037 | /* This turns up a lot in Memcheck instrumentation of |
| 2038 | Icc generated code. I don't know why. */ |
| 2039 | UShort arg1 = e->Iex.Binop.arg1->Iex.Const.con->Ico.V128; |
| 2040 | UShort arg2 = e->Iex.Binop.arg2->Iex.Const.con->Ico.V128; |
| 2041 | if (0 == arg1 && 0 == arg2) { |
| 2042 | e2 = IRExpr_Const(IRConst_V128(0)); |
| 2043 | } else { |
| 2044 | goto unhandled; |
| 2045 | } |
| 2046 | break; |
| 2047 | } |
sewardj | 855dc72 | 2005-02-17 09:26:05 +0000 | [diff] [blame] | 2048 | |
sewardj | 607dd4f | 2004-09-08 18:20:19 +0000 | [diff] [blame] | 2049 | default: |
| 2050 | goto unhandled; |
sewardj | d721703 | 2004-08-19 10:49:10 +0000 | [diff] [blame] | 2051 | } |
sewardj | f672901 | 2004-08-25 12:45:13 +0000 | [diff] [blame] | 2052 | |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 2053 | } else { |
sewardj | f672901 | 2004-08-25 12:45:13 +0000 | [diff] [blame] | 2054 | |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 2055 | /* other cases (identities, etc) */ |
sewardj | 64d776c | 2010-10-01 14:06:22 +0000 | [diff] [blame] | 2056 | switch (e->Iex.Binop.op) { |
florian | f6402ab | 2012-01-29 02:19:43 +0000 | [diff] [blame] | 2057 | |
| 2058 | case Iop_Shl32: |
| 2059 | case Iop_Shl64: |
| 2060 | case Iop_Shr64: |
| 2061 | /* Shl32/Shl64/Shr64(x,0) ==> x */ |
| 2062 | if (isZeroU(e->Iex.Binop.arg2)) { |
sewardj | 64d776c | 2010-10-01 14:06:22 +0000 | [diff] [blame] | 2063 | e2 = e->Iex.Binop.arg1; |
florian | f6402ab | 2012-01-29 02:19:43 +0000 | [diff] [blame] | 2064 | break; |
| 2065 | } |
| 2066 | /* Shl32/Shl64/Shr64(0,x) ==> 0 */ |
| 2067 | if (isZeroU(e->Iex.Binop.arg1)) { |
| 2068 | e2 = e->Iex.Binop.arg1; |
| 2069 | break; |
| 2070 | } |
sewardj | 64d776c | 2010-10-01 14:06:22 +0000 | [diff] [blame] | 2071 | break; |
sewardj | f672901 | 2004-08-25 12:45:13 +0000 | [diff] [blame] | 2072 | |
florian | f6402ab | 2012-01-29 02:19:43 +0000 | [diff] [blame] | 2073 | case Iop_Shr32: |
| 2074 | /* Shr32(x,0) ==> x */ |
| 2075 | if (isZeroU(e->Iex.Binop.arg2)) { |
| 2076 | e2 = e->Iex.Binop.arg1; |
| 2077 | break; |
| 2078 | } |
| 2079 | break; |
| 2080 | |
| 2081 | case Iop_Or8: |
| 2082 | case Iop_Or16: |
| 2083 | case Iop_Or32: |
| 2084 | case Iop_Or64: |
| 2085 | case Iop_Max32U: |
sewardj | cf4be4a | 2012-03-26 09:44:39 +0000 | [diff] [blame] | 2086 | /* Or8/Or16/Or32/Or64/Max32U(x,0) ==> x */ |
florian | f6402ab | 2012-01-29 02:19:43 +0000 | [diff] [blame] | 2087 | if (isZeroU(e->Iex.Binop.arg2)) { |
| 2088 | e2 = e->Iex.Binop.arg1; |
| 2089 | break; |
| 2090 | } |
sewardj | cf4be4a | 2012-03-26 09:44:39 +0000 | [diff] [blame] | 2091 | /* Or8/Or16/Or32/Or64/Max32U(0,x) ==> x */ |
florian | f6402ab | 2012-01-29 02:19:43 +0000 | [diff] [blame] | 2092 | if (isZeroU(e->Iex.Binop.arg1)) { |
| 2093 | e2 = e->Iex.Binop.arg2; |
| 2094 | break; |
| 2095 | } |
sewardj | cf4be4a | 2012-03-26 09:44:39 +0000 | [diff] [blame] | 2096 | /* Or8/Or16/Or32/Or64/Max32U(x,1---1b) ==> 1---1b */ |
| 2097 | /* Or8/Or16/Or32/Or64/Max32U(1---1b,x) ==> 1---1b */ |
| 2098 | if (isOnesU(e->Iex.Binop.arg1) || isOnesU(e->Iex.Binop.arg2)) { |
| 2099 | e2 = mkOnesOfPrimopResultType(e->Iex.Binop.op); |
| 2100 | break; |
| 2101 | } |
| 2102 | /* Or8/Or16/Or32/Or64/Max32U(t,t) ==> t, for some IRTemp t */ |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 2103 | if (sameIRExprs(env, e->Iex.Binop.arg1, e->Iex.Binop.arg2)) { |
florian | f6402ab | 2012-01-29 02:19:43 +0000 | [diff] [blame] | 2104 | e2 = e->Iex.Binop.arg1; |
| 2105 | break; |
| 2106 | } |
| 2107 | break; |
| 2108 | |
| 2109 | case Iop_Add8: |
| 2110 | /* Add8(t,t) ==> t << 1. |
| 2111 | Memcheck doesn't understand that |
| 2112 | x+x produces a defined least significant bit, and it seems |
| 2113 | simplest just to get rid of the problem by rewriting it |
| 2114 | out, since the opportunity to do so exists. */ |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 2115 | if (sameIRExprs(env, e->Iex.Binop.arg1, e->Iex.Binop.arg2)) { |
florian | f6402ab | 2012-01-29 02:19:43 +0000 | [diff] [blame] | 2116 | e2 = IRExpr_Binop(Iop_Shl8, e->Iex.Binop.arg1, |
| 2117 | IRExpr_Const(IRConst_U8(1))); |
| 2118 | break; |
| 2119 | } |
| 2120 | break; |
| 2121 | |
| 2122 | /* NB no Add16(t,t) case yet as no known test case exists */ |
| 2123 | |
| 2124 | case Iop_Add32: |
| 2125 | case Iop_Add64: |
| 2126 | /* Add32/Add64(x,0) ==> x */ |
| 2127 | if (isZeroU(e->Iex.Binop.arg2)) { |
| 2128 | e2 = e->Iex.Binop.arg1; |
| 2129 | break; |
| 2130 | } |
| 2131 | /* Add32/Add64(0,x) ==> x */ |
| 2132 | if (isZeroU(e->Iex.Binop.arg1)) { |
| 2133 | e2 = e->Iex.Binop.arg2; |
| 2134 | break; |
| 2135 | } |
| 2136 | /* Add32/Add64(t,t) ==> t << 1. Same rationale as for Add8. */ |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 2137 | if (sameIRExprs(env, e->Iex.Binop.arg1, e->Iex.Binop.arg2)) { |
sewardj | 6399f81 | 2012-06-29 15:36:44 +0000 | [diff] [blame] | 2138 | e2 = IRExpr_Binop( |
| 2139 | e->Iex.Binop.op == Iop_Add32 ? Iop_Shl32 : Iop_Shl64, |
| 2140 | e->Iex.Binop.arg1, IRExpr_Const(IRConst_U8(1))); |
florian | f6402ab | 2012-01-29 02:19:43 +0000 | [diff] [blame] | 2141 | break; |
| 2142 | } |
| 2143 | break; |
| 2144 | |
sewardj | 1beb433 | 2012-12-19 15:28:43 +0000 | [diff] [blame] | 2145 | case Iop_Sub32: |
florian | f6402ab | 2012-01-29 02:19:43 +0000 | [diff] [blame] | 2146 | case Iop_Sub64: |
sewardj | 1beb433 | 2012-12-19 15:28:43 +0000 | [diff] [blame] | 2147 | /* Sub32/Sub64(x,0) ==> x */ |
| 2148 | if (isZeroU(e->Iex.Binop.arg2)) { |
florian | f6402ab | 2012-01-29 02:19:43 +0000 | [diff] [blame] | 2149 | e2 = e->Iex.Binop.arg1; |
| 2150 | break; |
| 2151 | } |
sewardj | 1beb433 | 2012-12-19 15:28:43 +0000 | [diff] [blame] | 2152 | /* Sub32/Sub64(t,t) ==> 0, for some IRTemp t */ |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 2153 | if (sameIRExprs(env, e->Iex.Binop.arg1, e->Iex.Binop.arg2)) { |
sewardj | 64d776c | 2010-10-01 14:06:22 +0000 | [diff] [blame] | 2154 | e2 = mkZeroOfPrimopResultType(e->Iex.Binop.op); |
florian | f6402ab | 2012-01-29 02:19:43 +0000 | [diff] [blame] | 2155 | break; |
| 2156 | } |
sewardj | 64d776c | 2010-10-01 14:06:22 +0000 | [diff] [blame] | 2157 | break; |
sewardj | 36a911a | 2014-04-03 13:48:21 +0000 | [diff] [blame] | 2158 | case Iop_Sub8x16: |
| 2159 | /* Sub8x16(x,0) ==> x */ |
| 2160 | if (isZeroV128(e->Iex.Binop.arg2)) { |
| 2161 | e2 = e->Iex.Binop.arg1; |
| 2162 | break; |
| 2163 | } |
| 2164 | break; |
sewardj | 64d776c | 2010-10-01 14:06:22 +0000 | [diff] [blame] | 2165 | |
florian | 2782d21 | 2014-07-18 21:23:46 +0000 | [diff] [blame] | 2166 | case Iop_And8: |
| 2167 | case Iop_And16: |
florian | f6402ab | 2012-01-29 02:19:43 +0000 | [diff] [blame] | 2168 | case Iop_And32: |
florian | 2782d21 | 2014-07-18 21:23:46 +0000 | [diff] [blame] | 2169 | case Iop_And64: |
| 2170 | /* And8/And16/And32/And64(x,1---1b) ==> x */ |
florian | 55ce7b7 | 2013-09-01 14:22:05 +0000 | [diff] [blame] | 2171 | if (isOnesU(e->Iex.Binop.arg2)) { |
florian | f6402ab | 2012-01-29 02:19:43 +0000 | [diff] [blame] | 2172 | e2 = e->Iex.Binop.arg1; |
| 2173 | break; |
| 2174 | } |
florian | 2782d21 | 2014-07-18 21:23:46 +0000 | [diff] [blame] | 2175 | /* And8/And16/And32/And64(1---1b,x) ==> x */ |
| 2176 | if (isOnesU(e->Iex.Binop.arg1)) { |
| 2177 | e2 = e->Iex.Binop.arg2; |
| 2178 | break; |
| 2179 | } |
| 2180 | /* And8/And16/And32/And64(x,0) ==> 0 */ |
florian | 55ce7b7 | 2013-09-01 14:22:05 +0000 | [diff] [blame] | 2181 | if (isZeroU(e->Iex.Binop.arg2)) { |
florian | f6402ab | 2012-01-29 02:19:43 +0000 | [diff] [blame] | 2182 | e2 = e->Iex.Binop.arg2; |
| 2183 | break; |
| 2184 | } |
florian | 2782d21 | 2014-07-18 21:23:46 +0000 | [diff] [blame] | 2185 | /* And8/And16/And32/And64(0,x) ==> 0 */ |
florian | 55ce7b7 | 2013-09-01 14:22:05 +0000 | [diff] [blame] | 2186 | if (isZeroU(e->Iex.Binop.arg1)) { |
florian | f6402ab | 2012-01-29 02:19:43 +0000 | [diff] [blame] | 2187 | e2 = e->Iex.Binop.arg1; |
| 2188 | break; |
| 2189 | } |
florian | 2782d21 | 2014-07-18 21:23:46 +0000 | [diff] [blame] | 2190 | /* And8/And16/And32/And64(t,t) ==> t, for some IRTemp t */ |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 2191 | if (sameIRExprs(env, e->Iex.Binop.arg1, e->Iex.Binop.arg2)) { |
florian | f6402ab | 2012-01-29 02:19:43 +0000 | [diff] [blame] | 2192 | e2 = e->Iex.Binop.arg1; |
| 2193 | break; |
| 2194 | } |
| 2195 | break; |
| 2196 | |
sewardj | 4a0bee0 | 2012-06-29 14:44:44 +0000 | [diff] [blame] | 2197 | case Iop_AndV128: |
| 2198 | case Iop_AndV256: |
florian | 55ce7b7 | 2013-09-01 14:22:05 +0000 | [diff] [blame] | 2199 | /* And8/And16/AndV128/AndV256(t,t) |
sewardj | 4a0bee0 | 2012-06-29 14:44:44 +0000 | [diff] [blame] | 2200 | ==> t, for some IRTemp t */ |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 2201 | if (sameIRExprs(env, e->Iex.Binop.arg1, e->Iex.Binop.arg2)) { |
florian | f6402ab | 2012-01-29 02:19:43 +0000 | [diff] [blame] | 2202 | e2 = e->Iex.Binop.arg1; |
| 2203 | break; |
| 2204 | } |
sewardj | 36a911a | 2014-04-03 13:48:21 +0000 | [diff] [blame] | 2205 | /* Deal with either arg zero. Could handle other And |
| 2206 | cases here too. */ |
florian | 1b7c471 | 2014-07-16 20:17:49 +0000 | [diff] [blame] | 2207 | if (e->Iex.Binop.op == Iop_AndV256 |
| 2208 | && (isZeroV256(e->Iex.Binop.arg1) |
| 2209 | || isZeroV256(e->Iex.Binop.arg2))) { |
sewardj | bbcf188 | 2014-01-12 12:49:10 +0000 | [diff] [blame] | 2210 | e2 = mkZeroOfPrimopResultType(e->Iex.Binop.op); |
| 2211 | break; |
sewardj | 36a911a | 2014-04-03 13:48:21 +0000 | [diff] [blame] | 2212 | } else if (e->Iex.Binop.op == Iop_AndV128 |
| 2213 | && (isZeroV128(e->Iex.Binop.arg1) |
| 2214 | || isZeroV128(e->Iex.Binop.arg2))) { |
| 2215 | e2 = mkZeroOfPrimopResultType(e->Iex.Binop.op); |
| 2216 | break; |
sewardj | bbcf188 | 2014-01-12 12:49:10 +0000 | [diff] [blame] | 2217 | } |
florian | f6402ab | 2012-01-29 02:19:43 +0000 | [diff] [blame] | 2218 | break; |
| 2219 | |
| 2220 | case Iop_OrV128: |
sewardj | 4a0bee0 | 2012-06-29 14:44:44 +0000 | [diff] [blame] | 2221 | case Iop_OrV256: |
| 2222 | /* V128/V256(t,t) ==> t, for some IRTemp t */ |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 2223 | if (sameIRExprs(env, e->Iex.Binop.arg1, e->Iex.Binop.arg2)) { |
florian | f6402ab | 2012-01-29 02:19:43 +0000 | [diff] [blame] | 2224 | e2 = e->Iex.Binop.arg1; |
| 2225 | break; |
| 2226 | } |
sewardj | 9571dc0 | 2014-01-26 18:34:23 +0000 | [diff] [blame] | 2227 | /* OrV128(t,0) ==> t */ |
| 2228 | if (e->Iex.Binop.op == Iop_OrV128) { |
| 2229 | if (isZeroV128(e->Iex.Binop.arg2)) { |
| 2230 | e2 = e->Iex.Binop.arg1; |
| 2231 | break; |
| 2232 | } |
| 2233 | if (isZeroV128(e->Iex.Binop.arg1)) { |
| 2234 | e2 = e->Iex.Binop.arg2; |
| 2235 | break; |
| 2236 | } |
| 2237 | } |
| 2238 | /* OrV256(t,0) ==> t */ |
| 2239 | if (e->Iex.Binop.op == Iop_OrV256) { |
| 2240 | if (isZeroV256(e->Iex.Binop.arg2)) { |
| 2241 | e2 = e->Iex.Binop.arg1; |
| 2242 | break; |
| 2243 | } |
| 2244 | //Disabled because there's no known test case right now. |
| 2245 | //if (isZeroV256(e->Iex.Binop.arg1)) { |
| 2246 | // e2 = e->Iex.Binop.arg2; |
| 2247 | // break; |
| 2248 | //} |
| 2249 | } |
florian | f6402ab | 2012-01-29 02:19:43 +0000 | [diff] [blame] | 2250 | break; |
| 2251 | |
| 2252 | case Iop_Xor8: |
| 2253 | case Iop_Xor16: |
| 2254 | case Iop_Xor32: |
| 2255 | case Iop_Xor64: |
| 2256 | case Iop_XorV128: |
| 2257 | /* Xor8/16/32/64/V128(t,t) ==> 0, for some IRTemp t */ |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 2258 | if (sameIRExprs(env, e->Iex.Binop.arg1, e->Iex.Binop.arg2)) { |
florian | f6402ab | 2012-01-29 02:19:43 +0000 | [diff] [blame] | 2259 | e2 = mkZeroOfPrimopResultType(e->Iex.Binop.op); |
| 2260 | break; |
| 2261 | } |
sewardj | b01ff40 | 2014-08-04 08:09:23 +0000 | [diff] [blame] | 2262 | /* XorV128(t,0) ==> t */ |
| 2263 | if (e->Iex.Binop.op == Iop_XorV128) { |
| 2264 | if (isZeroV128(e->Iex.Binop.arg2)) { |
| 2265 | e2 = e->Iex.Binop.arg1; |
| 2266 | break; |
| 2267 | } |
| 2268 | //Disabled because there's no known test case right now. |
| 2269 | //if (isZeroV128(e->Iex.Binop.arg1)) { |
| 2270 | // e2 = e->Iex.Binop.arg2; |
| 2271 | // break; |
| 2272 | //} |
| 2273 | } |
florian | f6402ab | 2012-01-29 02:19:43 +0000 | [diff] [blame] | 2274 | break; |
| 2275 | |
sewardj | a7e9638 | 2012-06-29 16:26:17 +0000 | [diff] [blame] | 2276 | case Iop_CmpNE32: |
sewardj | 1beb433 | 2012-12-19 15:28:43 +0000 | [diff] [blame] | 2277 | /* CmpNE32(t,t) ==> 0, for some IRTemp t */ |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 2278 | if (sameIRExprs(env, e->Iex.Binop.arg1, e->Iex.Binop.arg2)) { |
florian | f6402ab | 2012-01-29 02:19:43 +0000 | [diff] [blame] | 2279 | e2 = mkZeroOfPrimopResultType(e->Iex.Binop.op); |
| 2280 | break; |
| 2281 | } |
sewardj | 009230b | 2013-01-26 11:47:55 +0000 | [diff] [blame] | 2282 | /* CmpNE32(1Uto32(b), 0) ==> b */ |
| 2283 | if (isZeroU32(e->Iex.Binop.arg2)) { |
| 2284 | IRExpr* a1 = chase(env, e->Iex.Binop.arg1); |
| 2285 | if (a1 && a1->tag == Iex_Unop |
| 2286 | && a1->Iex.Unop.op == Iop_1Uto32) { |
| 2287 | e2 = a1->Iex.Unop.arg; |
| 2288 | break; |
| 2289 | } |
| 2290 | } |
florian | f6402ab | 2012-01-29 02:19:43 +0000 | [diff] [blame] | 2291 | break; |
| 2292 | |
sewardj | a7e9638 | 2012-06-29 16:26:17 +0000 | [diff] [blame] | 2293 | case Iop_CmpEQ32: |
sewardj | 64d776c | 2010-10-01 14:06:22 +0000 | [diff] [blame] | 2294 | case Iop_CmpEQ64: |
| 2295 | case Iop_CmpEQ8x8: |
| 2296 | case Iop_CmpEQ8x16: |
sewardj | 899d183 | 2012-07-10 21:41:01 +0000 | [diff] [blame] | 2297 | case Iop_CmpEQ16x8: |
sewardj | ffccf2b | 2012-06-29 15:33:09 +0000 | [diff] [blame] | 2298 | case Iop_CmpEQ32x4: |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 2299 | if (sameIRExprs(env, e->Iex.Binop.arg1, e->Iex.Binop.arg2)) { |
sewardj | 64d776c | 2010-10-01 14:06:22 +0000 | [diff] [blame] | 2300 | e2 = mkOnesOfPrimopResultType(e->Iex.Binop.op); |
florian | f6402ab | 2012-01-29 02:19:43 +0000 | [diff] [blame] | 2301 | break; |
| 2302 | } |
sewardj | 64d776c | 2010-10-01 14:06:22 +0000 | [diff] [blame] | 2303 | break; |
florian | f6402ab | 2012-01-29 02:19:43 +0000 | [diff] [blame] | 2304 | |
sewardj | 64d776c | 2010-10-01 14:06:22 +0000 | [diff] [blame] | 2305 | default: |
| 2306 | break; |
sewardj | 0033ddc | 2005-04-26 23:34:34 +0000 | [diff] [blame] | 2307 | } |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 2308 | } |
florian | 708417d | 2012-02-15 00:43:36 +0000 | [diff] [blame] | 2309 | break; |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 2310 | |
florian | 99dd03e | 2013-01-29 03:56:06 +0000 | [diff] [blame] | 2311 | case Iex_ITE: |
| 2312 | /* ITE */ |
sewardj | 6c299f3 | 2009-12-31 18:00:12 +0000 | [diff] [blame] | 2313 | /* is the discriminant is a constant? */ |
florian | 99dd03e | 2013-01-29 03:56:06 +0000 | [diff] [blame] | 2314 | if (e->Iex.ITE.cond->tag == Iex_Const) { |
sewardj | 6c299f3 | 2009-12-31 18:00:12 +0000 | [diff] [blame] | 2315 | /* assured us by the IR type rules */ |
florian | 99dd03e | 2013-01-29 03:56:06 +0000 | [diff] [blame] | 2316 | vassert(e->Iex.ITE.cond->Iex.Const.con->tag == Ico_U1); |
| 2317 | e2 = e->Iex.ITE.cond->Iex.Const.con->Ico.U1 |
| 2318 | ? e->Iex.ITE.iftrue : e->Iex.ITE.iffalse; |
sewardj | 6c299f3 | 2009-12-31 18:00:12 +0000 | [diff] [blame] | 2319 | } |
| 2320 | else |
| 2321 | /* are the arms identical? (pretty weedy test) */ |
florian | 99dd03e | 2013-01-29 03:56:06 +0000 | [diff] [blame] | 2322 | if (sameIRExprs(env, e->Iex.ITE.iftrue, |
| 2323 | e->Iex.ITE.iffalse)) { |
| 2324 | e2 = e->Iex.ITE.iffalse; |
sewardj | 6c299f3 | 2009-12-31 18:00:12 +0000 | [diff] [blame] | 2325 | } |
florian | 708417d | 2012-02-15 00:43:36 +0000 | [diff] [blame] | 2326 | break; |
| 2327 | |
| 2328 | default: |
| 2329 | /* not considered */ |
| 2330 | break; |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 2331 | } |
| 2332 | |
sewardj | a7e9638 | 2012-06-29 16:26:17 +0000 | [diff] [blame] | 2333 | /* Show cases where we've found but not folded 'op(t,t)'. Be |
| 2334 | careful not to call sameIRExprs with values of different types, |
| 2335 | though, else it will assert (and so it should!). We can't |
| 2336 | conveniently call typeOfIRExpr on the two args without a whole |
| 2337 | bunch of extra plumbing to pass in a type env, so just use a |
| 2338 | hacky test to check the arguments are not anything that might |
| 2339 | sameIRExprs to assert. This is only OK because this kludge is |
| 2340 | only used for debug printing, not for "real" operation. For |
| 2341 | "real" operation (ie, all other calls to sameIRExprs), it is |
| 2342 | essential that the to args have the same type. |
| 2343 | |
| 2344 | The "right" solution is to plumb the containing block's |
| 2345 | IRTypeEnv through to here and use typeOfIRExpr to be sure. But |
| 2346 | that's a bunch of extra parameter passing which will just slow |
| 2347 | down the normal case, for no purpose. */ |
| 2348 | if (vex_control.iropt_verbosity > 0 |
| 2349 | && e == e2 |
| 2350 | && e->tag == Iex_Binop |
| 2351 | && !debug_only_hack_sameIRExprs_might_assert(e->Iex.Binop.arg1, |
| 2352 | e->Iex.Binop.arg2) |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 2353 | && sameIRExprs(env, e->Iex.Binop.arg1, e->Iex.Binop.arg2)) { |
sewardj | 4a0bee0 | 2012-06-29 14:44:44 +0000 | [diff] [blame] | 2354 | vex_printf("vex iropt: fold_Expr: no ident rule for: "); |
| 2355 | ppIRExpr(e); |
| 2356 | vex_printf("\n"); |
sewardj | 64d776c | 2010-10-01 14:06:22 +0000 | [diff] [blame] | 2357 | } |
| 2358 | |
| 2359 | /* Show the overall results of folding. */ |
sewardj | 088bcb4 | 2004-08-19 17:16:52 +0000 | [diff] [blame] | 2360 | if (DEBUG_IROPT && e2 != e) { |
| 2361 | vex_printf("FOLD: "); |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 2362 | ppIRExpr(e); vex_printf(" -> "); |
| 2363 | ppIRExpr(e2); vex_printf("\n"); |
| 2364 | } |
| 2365 | |
| 2366 | return e2; |
| 2367 | |
| 2368 | unhandled: |
sewardj | 883b00b | 2004-09-11 09:30:24 +0000 | [diff] [blame] | 2369 | # if 0 |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 2370 | vex_printf("\n\n"); |
| 2371 | ppIRExpr(e); |
| 2372 | vpanic("fold_Expr: no rule for the above"); |
sewardj | 883b00b | 2004-09-11 09:30:24 +0000 | [diff] [blame] | 2373 | # else |
sewardj | 328b54b | 2005-06-13 16:30:18 +0000 | [diff] [blame] | 2374 | if (vex_control.iropt_verbosity > 0) { |
sewardj | 4a0bee0 | 2012-06-29 14:44:44 +0000 | [diff] [blame] | 2375 | vex_printf("vex iropt: fold_Expr: no const rule for: "); |
sewardj | 328b54b | 2005-06-13 16:30:18 +0000 | [diff] [blame] | 2376 | ppIRExpr(e); |
| 2377 | vex_printf("\n"); |
| 2378 | } |
sewardj | 883b00b | 2004-09-11 09:30:24 +0000 | [diff] [blame] | 2379 | return e2; |
| 2380 | # endif |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 2381 | } |
| 2382 | |
| 2383 | |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 2384 | /* Apply the subst to a simple 1-level expression -- guaranteed to be |
| 2385 | 1-level due to previous flattening pass. */ |
| 2386 | |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2387 | static IRExpr* subst_Expr ( IRExpr** env, IRExpr* ex ) |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 2388 | { |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2389 | switch (ex->tag) { |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2390 | case Iex_RdTmp: |
| 2391 | if (env[(Int)ex->Iex.RdTmp.tmp] != NULL) { |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 2392 | IRExpr *rhs = env[(Int)ex->Iex.RdTmp.tmp]; |
| 2393 | if (rhs->tag == Iex_RdTmp) |
| 2394 | return rhs; |
| 2395 | if (rhs->tag == Iex_Const |
| 2396 | && rhs->Iex.Const.con->tag != Ico_F64i) |
| 2397 | return rhs; |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2398 | } |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 2399 | /* not bound in env */ |
| 2400 | return ex; |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2401 | |
| 2402 | case Iex_Const: |
| 2403 | case Iex_Get: |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 2404 | return ex; |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2405 | |
| 2406 | case Iex_GetI: |
sewardj | 496a58d | 2005-03-20 18:44:44 +0000 | [diff] [blame] | 2407 | vassert(isIRAtom(ex->Iex.GetI.ix)); |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2408 | return IRExpr_GetI( |
| 2409 | ex->Iex.GetI.descr, |
sewardj | eeac841 | 2004-11-02 00:26:55 +0000 | [diff] [blame] | 2410 | subst_Expr(env, ex->Iex.GetI.ix), |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2411 | ex->Iex.GetI.bias |
| 2412 | ); |
| 2413 | |
florian | 96d7cc3 | 2012-06-01 20:41:24 +0000 | [diff] [blame] | 2414 | case Iex_Qop: { |
| 2415 | IRQop* qop = ex->Iex.Qop.details; |
| 2416 | vassert(isIRAtom(qop->arg1)); |
| 2417 | vassert(isIRAtom(qop->arg2)); |
| 2418 | vassert(isIRAtom(qop->arg3)); |
| 2419 | vassert(isIRAtom(qop->arg4)); |
sewardj | 40c8026 | 2006-02-08 19:30:46 +0000 | [diff] [blame] | 2420 | return IRExpr_Qop( |
florian | 96d7cc3 | 2012-06-01 20:41:24 +0000 | [diff] [blame] | 2421 | qop->op, |
| 2422 | subst_Expr(env, qop->arg1), |
| 2423 | subst_Expr(env, qop->arg2), |
| 2424 | subst_Expr(env, qop->arg3), |
| 2425 | subst_Expr(env, qop->arg4) |
sewardj | 40c8026 | 2006-02-08 19:30:46 +0000 | [diff] [blame] | 2426 | ); |
florian | 96d7cc3 | 2012-06-01 20:41:24 +0000 | [diff] [blame] | 2427 | } |
sewardj | 40c8026 | 2006-02-08 19:30:46 +0000 | [diff] [blame] | 2428 | |
florian | 420bfa9 | 2012-06-02 20:29:22 +0000 | [diff] [blame] | 2429 | case Iex_Triop: { |
| 2430 | IRTriop* triop = ex->Iex.Triop.details; |
| 2431 | vassert(isIRAtom(triop->arg1)); |
| 2432 | vassert(isIRAtom(triop->arg2)); |
| 2433 | vassert(isIRAtom(triop->arg3)); |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 2434 | return IRExpr_Triop( |
florian | 420bfa9 | 2012-06-02 20:29:22 +0000 | [diff] [blame] | 2435 | triop->op, |
| 2436 | subst_Expr(env, triop->arg1), |
| 2437 | subst_Expr(env, triop->arg2), |
| 2438 | subst_Expr(env, triop->arg3) |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 2439 | ); |
florian | 420bfa9 | 2012-06-02 20:29:22 +0000 | [diff] [blame] | 2440 | } |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 2441 | |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2442 | case Iex_Binop: |
sewardj | 496a58d | 2005-03-20 18:44:44 +0000 | [diff] [blame] | 2443 | vassert(isIRAtom(ex->Iex.Binop.arg1)); |
| 2444 | vassert(isIRAtom(ex->Iex.Binop.arg2)); |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2445 | return IRExpr_Binop( |
| 2446 | ex->Iex.Binop.op, |
| 2447 | subst_Expr(env, ex->Iex.Binop.arg1), |
| 2448 | subst_Expr(env, ex->Iex.Binop.arg2) |
| 2449 | ); |
| 2450 | |
| 2451 | case Iex_Unop: |
sewardj | 496a58d | 2005-03-20 18:44:44 +0000 | [diff] [blame] | 2452 | vassert(isIRAtom(ex->Iex.Unop.arg)); |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2453 | return IRExpr_Unop( |
| 2454 | ex->Iex.Unop.op, |
| 2455 | subst_Expr(env, ex->Iex.Unop.arg) |
| 2456 | ); |
| 2457 | |
sewardj | af1ceca | 2005-06-30 23:31:27 +0000 | [diff] [blame] | 2458 | case Iex_Load: |
| 2459 | vassert(isIRAtom(ex->Iex.Load.addr)); |
| 2460 | return IRExpr_Load( |
| 2461 | ex->Iex.Load.end, |
| 2462 | ex->Iex.Load.ty, |
| 2463 | subst_Expr(env, ex->Iex.Load.addr) |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2464 | ); |
| 2465 | |
| 2466 | case Iex_CCall: { |
| 2467 | Int i; |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2468 | IRExpr** args2 = shallowCopyIRExprVec(ex->Iex.CCall.args); |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2469 | for (i = 0; args2[i]; i++) { |
sewardj | 496a58d | 2005-03-20 18:44:44 +0000 | [diff] [blame] | 2470 | vassert(isIRAtom(args2[i])); |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2471 | args2[i] = subst_Expr(env, args2[i]); |
| 2472 | } |
| 2473 | return IRExpr_CCall( |
sewardj | 8ea867b | 2004-10-30 19:03:02 +0000 | [diff] [blame] | 2474 | ex->Iex.CCall.cee, |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2475 | ex->Iex.CCall.retty, |
| 2476 | args2 |
| 2477 | ); |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 2478 | } |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2479 | |
florian | 99dd03e | 2013-01-29 03:56:06 +0000 | [diff] [blame] | 2480 | case Iex_ITE: |
| 2481 | vassert(isIRAtom(ex->Iex.ITE.cond)); |
| 2482 | vassert(isIRAtom(ex->Iex.ITE.iftrue)); |
| 2483 | vassert(isIRAtom(ex->Iex.ITE.iffalse)); |
| 2484 | return IRExpr_ITE( |
| 2485 | subst_Expr(env, ex->Iex.ITE.cond), |
| 2486 | subst_Expr(env, ex->Iex.ITE.iftrue), |
| 2487 | subst_Expr(env, ex->Iex.ITE.iffalse) |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2488 | ); |
| 2489 | |
| 2490 | default: |
| 2491 | vex_printf("\n\n"); ppIRExpr(ex); |
| 2492 | vpanic("subst_Expr"); |
| 2493 | |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 2494 | } |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 2495 | } |
| 2496 | |
| 2497 | |
| 2498 | /* Apply the subst to stmt, then fold the result as much as possible. |
sewardj | d2445f6 | 2005-03-21 00:15:53 +0000 | [diff] [blame] | 2499 | Much simplified due to stmt being previously flattened. As a |
| 2500 | result of this, the stmt may wind up being turned into a no-op. |
| 2501 | */ |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2502 | static IRStmt* subst_and_fold_Stmt ( IRExpr** env, IRStmt* st ) |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 2503 | { |
| 2504 | # if 0 |
| 2505 | vex_printf("\nsubst and fold stmt\n"); |
| 2506 | ppIRStmt(st); |
| 2507 | vex_printf("\n"); |
| 2508 | # endif |
| 2509 | |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2510 | switch (st->tag) { |
sewardj | 5a9ffab | 2005-05-12 17:55:01 +0000 | [diff] [blame] | 2511 | case Ist_AbiHint: |
| 2512 | vassert(isIRAtom(st->Ist.AbiHint.base)); |
sewardj | 478646f | 2008-05-01 20:13:04 +0000 | [diff] [blame] | 2513 | vassert(isIRAtom(st->Ist.AbiHint.nia)); |
sewardj | 5a9ffab | 2005-05-12 17:55:01 +0000 | [diff] [blame] | 2514 | return IRStmt_AbiHint( |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 2515 | fold_Expr(env, subst_Expr(env, st->Ist.AbiHint.base)), |
sewardj | 478646f | 2008-05-01 20:13:04 +0000 | [diff] [blame] | 2516 | st->Ist.AbiHint.len, |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 2517 | fold_Expr(env, subst_Expr(env, st->Ist.AbiHint.nia)) |
sewardj | 5a9ffab | 2005-05-12 17:55:01 +0000 | [diff] [blame] | 2518 | ); |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2519 | case Ist_Put: |
sewardj | 496a58d | 2005-03-20 18:44:44 +0000 | [diff] [blame] | 2520 | vassert(isIRAtom(st->Ist.Put.data)); |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2521 | return IRStmt_Put( |
| 2522 | st->Ist.Put.offset, |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 2523 | fold_Expr(env, subst_Expr(env, st->Ist.Put.data)) |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2524 | ); |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 2525 | |
florian | d6f38b3 | 2012-05-31 15:46:18 +0000 | [diff] [blame] | 2526 | case Ist_PutI: { |
| 2527 | IRPutI *puti, *puti2; |
| 2528 | puti = st->Ist.PutI.details; |
| 2529 | vassert(isIRAtom(puti->ix)); |
| 2530 | vassert(isIRAtom(puti->data)); |
| 2531 | puti2 = mkIRPutI(puti->descr, |
| 2532 | fold_Expr(env, subst_Expr(env, puti->ix)), |
| 2533 | puti->bias, |
| 2534 | fold_Expr(env, subst_Expr(env, puti->data))); |
| 2535 | return IRStmt_PutI(puti2); |
| 2536 | } |
sewardj | d721703 | 2004-08-19 10:49:10 +0000 | [diff] [blame] | 2537 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2538 | case Ist_WrTmp: |
| 2539 | /* This is the one place where an expr (st->Ist.WrTmp.data) is |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2540 | allowed to be more than just a constant or a tmp. */ |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2541 | return IRStmt_WrTmp( |
| 2542 | st->Ist.WrTmp.tmp, |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 2543 | fold_Expr(env, subst_Expr(env, st->Ist.WrTmp.data)) |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2544 | ); |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 2545 | |
sewardj | af1ceca | 2005-06-30 23:31:27 +0000 | [diff] [blame] | 2546 | case Ist_Store: |
| 2547 | vassert(isIRAtom(st->Ist.Store.addr)); |
| 2548 | vassert(isIRAtom(st->Ist.Store.data)); |
| 2549 | return IRStmt_Store( |
| 2550 | st->Ist.Store.end, |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 2551 | fold_Expr(env, subst_Expr(env, st->Ist.Store.addr)), |
| 2552 | fold_Expr(env, subst_Expr(env, st->Ist.Store.data)) |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2553 | ); |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 2554 | |
sewardj | cfe046e | 2013-01-17 14:23:53 +0000 | [diff] [blame] | 2555 | case Ist_StoreG: { |
| 2556 | IRStoreG* sg = st->Ist.StoreG.details; |
| 2557 | vassert(isIRAtom(sg->addr)); |
| 2558 | vassert(isIRAtom(sg->data)); |
| 2559 | vassert(isIRAtom(sg->guard)); |
| 2560 | IRExpr* faddr = fold_Expr(env, subst_Expr(env, sg->addr)); |
| 2561 | IRExpr* fdata = fold_Expr(env, subst_Expr(env, sg->data)); |
| 2562 | IRExpr* fguard = fold_Expr(env, subst_Expr(env, sg->guard)); |
| 2563 | if (fguard->tag == Iex_Const) { |
| 2564 | /* The condition on this store has folded down to a constant. */ |
| 2565 | vassert(fguard->Iex.Const.con->tag == Ico_U1); |
| 2566 | if (fguard->Iex.Const.con->Ico.U1 == False) { |
| 2567 | return IRStmt_NoOp(); |
| 2568 | } else { |
| 2569 | vassert(fguard->Iex.Const.con->Ico.U1 == True); |
| 2570 | return IRStmt_Store(sg->end, faddr, fdata); |
| 2571 | } |
| 2572 | } |
| 2573 | return IRStmt_StoreG(sg->end, faddr, fdata, fguard); |
| 2574 | } |
| 2575 | |
| 2576 | case Ist_LoadG: { |
| 2577 | /* This is complicated. If the guard folds down to 'false', |
| 2578 | we can replace it with an assignment 'dst := alt', but if |
| 2579 | the guard folds down to 'true', we can't conveniently |
| 2580 | replace it with an unconditional load, because doing so |
| 2581 | requires generating a new temporary, and that is not easy |
| 2582 | to do at this point. */ |
| 2583 | IRLoadG* lg = st->Ist.LoadG.details; |
| 2584 | vassert(isIRAtom(lg->addr)); |
| 2585 | vassert(isIRAtom(lg->alt)); |
| 2586 | vassert(isIRAtom(lg->guard)); |
| 2587 | IRExpr* faddr = fold_Expr(env, subst_Expr(env, lg->addr)); |
| 2588 | IRExpr* falt = fold_Expr(env, subst_Expr(env, lg->alt)); |
| 2589 | IRExpr* fguard = fold_Expr(env, subst_Expr(env, lg->guard)); |
| 2590 | if (fguard->tag == Iex_Const) { |
| 2591 | /* The condition on this load has folded down to a constant. */ |
| 2592 | vassert(fguard->Iex.Const.con->tag == Ico_U1); |
| 2593 | if (fguard->Iex.Const.con->Ico.U1 == False) { |
| 2594 | /* The load is not going to happen -- instead 'alt' is |
| 2595 | assigned to 'dst'. */ |
| 2596 | return IRStmt_WrTmp(lg->dst, falt); |
| 2597 | } else { |
| 2598 | vassert(fguard->Iex.Const.con->Ico.U1 == True); |
| 2599 | /* The load is always going to happen. We want to |
| 2600 | convert to an unconditional load and assign to 'dst' |
| 2601 | (IRStmt_WrTmp). Problem is we need an extra temp to |
| 2602 | hold the loaded value, but none is available. |
| 2603 | Instead, reconstitute the conditional load (with |
| 2604 | folded args, of course) and let the caller of this |
| 2605 | routine deal with the problem. */ |
| 2606 | } |
| 2607 | } |
| 2608 | return IRStmt_LoadG(lg->end, lg->cvt, lg->dst, faddr, falt, fguard); |
| 2609 | } |
| 2610 | |
sewardj | e9d8a26 | 2009-07-01 08:06:34 +0000 | [diff] [blame] | 2611 | case Ist_CAS: { |
| 2612 | IRCAS *cas, *cas2; |
| 2613 | cas = st->Ist.CAS.details; |
| 2614 | vassert(isIRAtom(cas->addr)); |
| 2615 | vassert(cas->expdHi == NULL || isIRAtom(cas->expdHi)); |
| 2616 | vassert(isIRAtom(cas->expdLo)); |
| 2617 | vassert(cas->dataHi == NULL || isIRAtom(cas->dataHi)); |
| 2618 | vassert(isIRAtom(cas->dataLo)); |
| 2619 | cas2 = mkIRCAS( |
| 2620 | cas->oldHi, cas->oldLo, cas->end, |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 2621 | fold_Expr(env, subst_Expr(env, cas->addr)), |
sewardj | 6399f81 | 2012-06-29 15:36:44 +0000 | [diff] [blame] | 2622 | cas->expdHi ? fold_Expr(env, subst_Expr(env, cas->expdHi)) |
| 2623 | : NULL, |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 2624 | fold_Expr(env, subst_Expr(env, cas->expdLo)), |
sewardj | 6399f81 | 2012-06-29 15:36:44 +0000 | [diff] [blame] | 2625 | cas->dataHi ? fold_Expr(env, subst_Expr(env, cas->dataHi)) |
| 2626 | : NULL, |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 2627 | fold_Expr(env, subst_Expr(env, cas->dataLo)) |
sewardj | e9d8a26 | 2009-07-01 08:06:34 +0000 | [diff] [blame] | 2628 | ); |
| 2629 | return IRStmt_CAS(cas2); |
| 2630 | } |
| 2631 | |
sewardj | e768e92 | 2009-11-26 17:17:37 +0000 | [diff] [blame] | 2632 | case Ist_LLSC: |
| 2633 | vassert(isIRAtom(st->Ist.LLSC.addr)); |
| 2634 | if (st->Ist.LLSC.storedata) |
| 2635 | vassert(isIRAtom(st->Ist.LLSC.storedata)); |
| 2636 | return IRStmt_LLSC( |
| 2637 | st->Ist.LLSC.end, |
| 2638 | st->Ist.LLSC.result, |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 2639 | fold_Expr(env, subst_Expr(env, st->Ist.LLSC.addr)), |
sewardj | e768e92 | 2009-11-26 17:17:37 +0000 | [diff] [blame] | 2640 | st->Ist.LLSC.storedata |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 2641 | ? fold_Expr(env, subst_Expr(env, st->Ist.LLSC.storedata)) |
sewardj | e768e92 | 2009-11-26 17:17:37 +0000 | [diff] [blame] | 2642 | : NULL |
| 2643 | ); |
| 2644 | |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2645 | case Ist_Dirty: { |
| 2646 | Int i; |
| 2647 | IRDirty *d, *d2; |
| 2648 | d = st->Ist.Dirty.details; |
| 2649 | d2 = emptyIRDirty(); |
| 2650 | *d2 = *d; |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2651 | d2->args = shallowCopyIRExprVec(d2->args); |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2652 | if (d2->mFx != Ifx_None) { |
sewardj | 496a58d | 2005-03-20 18:44:44 +0000 | [diff] [blame] | 2653 | vassert(isIRAtom(d2->mAddr)); |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 2654 | d2->mAddr = fold_Expr(env, subst_Expr(env, d2->mAddr)); |
sewardj | b8e7586 | 2004-08-19 17:58:45 +0000 | [diff] [blame] | 2655 | } |
sewardj | 496a58d | 2005-03-20 18:44:44 +0000 | [diff] [blame] | 2656 | vassert(isIRAtom(d2->guard)); |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 2657 | d2->guard = fold_Expr(env, subst_Expr(env, d2->guard)); |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2658 | for (i = 0; d2->args[i]; i++) { |
sewardj | 74142b8 | 2013-08-08 10:28:59 +0000 | [diff] [blame] | 2659 | IRExpr* arg = d2->args[i]; |
florian | 9041956 | 2013-08-15 20:54:52 +0000 | [diff] [blame] | 2660 | if (LIKELY(!is_IRExpr_VECRET_or_BBPTR(arg))) { |
sewardj | 74142b8 | 2013-08-08 10:28:59 +0000 | [diff] [blame] | 2661 | vassert(isIRAtom(arg)); |
| 2662 | d2->args[i] = fold_Expr(env, subst_Expr(env, arg)); |
| 2663 | } |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2664 | } |
| 2665 | return IRStmt_Dirty(d2); |
sewardj | b8e7586 | 2004-08-19 17:58:45 +0000 | [diff] [blame] | 2666 | } |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 2667 | |
sewardj | f168931 | 2005-03-16 18:19:10 +0000 | [diff] [blame] | 2668 | case Ist_IMark: |
sewardj | 2f10aa6 | 2011-05-27 13:20:56 +0000 | [diff] [blame] | 2669 | return IRStmt_IMark(st->Ist.IMark.addr, |
| 2670 | st->Ist.IMark.len, |
| 2671 | st->Ist.IMark.delta); |
sewardj | f168931 | 2005-03-16 18:19:10 +0000 | [diff] [blame] | 2672 | |
sewardj | d2445f6 | 2005-03-21 00:15:53 +0000 | [diff] [blame] | 2673 | case Ist_NoOp: |
| 2674 | return IRStmt_NoOp(); |
| 2675 | |
sewardj | c4356f0 | 2007-11-09 21:15:04 +0000 | [diff] [blame] | 2676 | case Ist_MBE: |
| 2677 | return IRStmt_MBE(st->Ist.MBE.event); |
sewardj | 3e83893 | 2005-01-07 12:09:15 +0000 | [diff] [blame] | 2678 | |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2679 | case Ist_Exit: { |
| 2680 | IRExpr* fcond; |
sewardj | 496a58d | 2005-03-20 18:44:44 +0000 | [diff] [blame] | 2681 | vassert(isIRAtom(st->Ist.Exit.guard)); |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 2682 | fcond = fold_Expr(env, subst_Expr(env, st->Ist.Exit.guard)); |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2683 | if (fcond->tag == Iex_Const) { |
| 2684 | /* Interesting. The condition on this exit has folded down to |
| 2685 | a constant. */ |
sewardj | ba99931 | 2004-11-15 15:21:17 +0000 | [diff] [blame] | 2686 | vassert(fcond->Iex.Const.con->tag == Ico_U1); |
| 2687 | if (fcond->Iex.Const.con->Ico.U1 == False) { |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2688 | /* exit is never going to happen, so dump the statement. */ |
sewardj | d2445f6 | 2005-03-21 00:15:53 +0000 | [diff] [blame] | 2689 | return IRStmt_NoOp(); |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2690 | } else { |
sewardj | ba99931 | 2004-11-15 15:21:17 +0000 | [diff] [blame] | 2691 | vassert(fcond->Iex.Const.con->Ico.U1 == True); |
sewardj | e810c19 | 2005-09-09 08:33:03 +0000 | [diff] [blame] | 2692 | /* Hmmm. The exit has become unconditional. Leave it |
| 2693 | as it is for now, since we'd have to truncate the BB |
| 2694 | at this point, which is tricky. Such truncation is |
| 2695 | done later by the dead-code elimination pass. */ |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2696 | /* fall out into the reconstruct-the-exit code. */ |
sewardj | 0861374 | 2004-10-25 13:01:45 +0000 | [diff] [blame] | 2697 | if (vex_control.iropt_verbosity > 0) |
| 2698 | /* really a misuse of vex_control.iropt_verbosity */ |
sewardj | 8c2c10b | 2004-10-16 20:51:52 +0000 | [diff] [blame] | 2699 | vex_printf("vex iropt: IRStmt_Exit became unconditional\n"); |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2700 | } |
| 2701 | } |
sewardj | c6f970f | 2012-04-02 21:54:49 +0000 | [diff] [blame] | 2702 | return IRStmt_Exit(fcond, st->Ist.Exit.jk, |
| 2703 | st->Ist.Exit.dst, st->Ist.Exit.offsIP); |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2704 | } |
| 2705 | |
| 2706 | default: |
| 2707 | vex_printf("\n"); ppIRStmt(st); |
| 2708 | vpanic("subst_and_fold_Stmt"); |
| 2709 | } |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 2710 | } |
| 2711 | |
| 2712 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2713 | IRSB* cprop_BB ( IRSB* in ) |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 2714 | { |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2715 | Int i; |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2716 | IRSB* out; |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2717 | IRStmt* st2; |
| 2718 | Int n_tmps = in->tyenv->types_used; |
| 2719 | IRExpr** env = LibVEX_Alloc(n_tmps * sizeof(IRExpr*)); |
sewardj | cfe046e | 2013-01-17 14:23:53 +0000 | [diff] [blame] | 2720 | /* Keep track of IRStmt_LoadGs that we need to revisit after |
| 2721 | processing all the other statements. */ |
| 2722 | const Int N_FIXUPS = 16; |
| 2723 | Int fixups[N_FIXUPS]; /* indices in the stmt array of 'out' */ |
| 2724 | Int n_fixups = 0; |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 2725 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2726 | out = emptyIRSB(); |
| 2727 | out->tyenv = deepCopyIRTypeEnv( in->tyenv ); |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 2728 | |
| 2729 | /* Set up the env with which travels forward. This holds a |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 2730 | substitution, mapping IRTemps to IRExprs. The environment |
| 2731 | is to be applied as we move along. Keys are IRTemps. |
| 2732 | Values are IRExpr*s. |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 2733 | */ |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2734 | for (i = 0; i < n_tmps; i++) |
| 2735 | env[i] = NULL; |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 2736 | |
| 2737 | /* For each original SSA-form stmt ... */ |
| 2738 | for (i = 0; i < in->stmts_used; i++) { |
| 2739 | |
| 2740 | /* First apply the substitution to the current stmt. This |
| 2741 | propagates in any constants and tmp-tmp assignments |
| 2742 | accumulated prior to this point. As part of the subst_Stmt |
| 2743 | call, also then fold any constant expressions resulting. */ |
| 2744 | |
sewardj | f0e4316 | 2004-08-20 00:11:12 +0000 | [diff] [blame] | 2745 | st2 = in->stmts[i]; |
| 2746 | |
| 2747 | /* perhaps st2 is already a no-op? */ |
sewardj | 8bee6d1 | 2005-03-22 02:24:05 +0000 | [diff] [blame] | 2748 | if (st2->tag == Ist_NoOp) continue; |
sewardj | f0e4316 | 2004-08-20 00:11:12 +0000 | [diff] [blame] | 2749 | |
| 2750 | st2 = subst_and_fold_Stmt( env, st2 ); |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 2751 | |
sewardj | cfe046e | 2013-01-17 14:23:53 +0000 | [diff] [blame] | 2752 | /* Deal with some post-folding special cases. */ |
| 2753 | switch (st2->tag) { |
sewardj | b8e7586 | 2004-08-19 17:58:45 +0000 | [diff] [blame] | 2754 | |
sewardj | cfe046e | 2013-01-17 14:23:53 +0000 | [diff] [blame] | 2755 | /* If the statement has been folded into a no-op, forget |
| 2756 | it. */ |
| 2757 | case Ist_NoOp: |
| 2758 | continue; |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 2759 | |
sewardj | cfe046e | 2013-01-17 14:23:53 +0000 | [diff] [blame] | 2760 | /* If the statement assigns to an IRTemp add it to the |
| 2761 | running environment. This is for the benefit of copy |
| 2762 | propagation and to allow sameIRExpr look through |
| 2763 | IRTemps. */ |
| 2764 | case Ist_WrTmp: { |
| 2765 | vassert(env[(Int)(st2->Ist.WrTmp.tmp)] == NULL); |
| 2766 | env[(Int)(st2->Ist.WrTmp.tmp)] = st2->Ist.WrTmp.data; |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 2767 | |
sewardj | cfe046e | 2013-01-17 14:23:53 +0000 | [diff] [blame] | 2768 | /* 't1 = t2' -- don't add to BB; will be optimized out */ |
| 2769 | if (st2->Ist.WrTmp.data->tag == Iex_RdTmp) |
| 2770 | continue; |
| 2771 | |
| 2772 | /* 't = const' && 'const != F64i' -- don't add to BB |
| 2773 | Note, we choose not to propagate const when const is an |
| 2774 | F64i, so that F64i literals can be CSE'd later. This |
| 2775 | helps x86 floating point code generation. */ |
| 2776 | if (st2->Ist.WrTmp.data->tag == Iex_Const |
| 2777 | && st2->Ist.WrTmp.data->Iex.Const.con->tag != Ico_F64i) { |
| 2778 | continue; |
| 2779 | } |
| 2780 | /* else add it to the output, as normal */ |
| 2781 | break; |
| 2782 | } |
| 2783 | |
| 2784 | case Ist_LoadG: { |
| 2785 | IRLoadG* lg = st2->Ist.LoadG.details; |
| 2786 | IRExpr* guard = lg->guard; |
| 2787 | if (guard->tag == Iex_Const) { |
| 2788 | /* The guard has folded to a constant, and that |
| 2789 | constant must be 1:I1, since subst_and_fold_Stmt |
| 2790 | folds out the case 0:I1 by itself. */ |
| 2791 | vassert(guard->Iex.Const.con->tag == Ico_U1); |
| 2792 | vassert(guard->Iex.Const.con->Ico.U1 == True); |
| 2793 | /* Add a NoOp here as a placeholder, and make a note of |
| 2794 | where it is in the output block. Afterwards we'll |
| 2795 | come back here and transform the NoOp and the LoadG |
| 2796 | into a load-convert pair. The fixups[] entry |
| 2797 | refers to the inserted NoOp, and we expect to find |
| 2798 | the relevant LoadG immediately after it. */ |
| 2799 | vassert(n_fixups >= 0 && n_fixups <= N_FIXUPS); |
| 2800 | if (n_fixups < N_FIXUPS) { |
| 2801 | fixups[n_fixups++] = out->stmts_used; |
| 2802 | addStmtToIRSB( out, IRStmt_NoOp() ); |
| 2803 | } |
| 2804 | } |
| 2805 | /* And always add the LoadG to the output, regardless. */ |
| 2806 | break; |
| 2807 | } |
| 2808 | |
| 2809 | default: |
| 2810 | break; |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 2811 | } |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 2812 | |
| 2813 | /* Not interesting, copy st2 into the output block. */ |
| 2814 | addStmtToIRSB( out, st2 ); |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 2815 | } |
| 2816 | |
sewardj | cfe046e | 2013-01-17 14:23:53 +0000 | [diff] [blame] | 2817 | # if STATS_IROPT |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 2818 | vex_printf("sameIRExpr: invoked = %u/%u equal = %u/%u max_nodes = %u\n", |
| 2819 | invocation_count, recursion_count, success_count, |
| 2820 | recursion_success_count, max_nodes_visited); |
sewardj | cfe046e | 2013-01-17 14:23:53 +0000 | [diff] [blame] | 2821 | # endif |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 2822 | |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 2823 | out->next = subst_Expr( env, in->next ); |
| 2824 | out->jumpkind = in->jumpkind; |
sewardj | c6f970f | 2012-04-02 21:54:49 +0000 | [diff] [blame] | 2825 | out->offsIP = in->offsIP; |
sewardj | cfe046e | 2013-01-17 14:23:53 +0000 | [diff] [blame] | 2826 | |
| 2827 | /* Process any leftover unconditional LoadGs that we noticed |
| 2828 | in the main pass. */ |
| 2829 | vassert(n_fixups >= 0 && n_fixups <= N_FIXUPS); |
| 2830 | for (i = 0; i < n_fixups; i++) { |
| 2831 | Int ix = fixups[i]; |
| 2832 | /* Carefully verify that the LoadG has the expected form. */ |
| 2833 | vassert(ix >= 0 && ix+1 < out->stmts_used); |
| 2834 | IRStmt* nop = out->stmts[ix]; |
| 2835 | IRStmt* lgu = out->stmts[ix+1]; |
| 2836 | vassert(nop->tag == Ist_NoOp); |
| 2837 | vassert(lgu->tag == Ist_LoadG); |
| 2838 | IRLoadG* lg = lgu->Ist.LoadG.details; |
| 2839 | IRExpr* guard = lg->guard; |
| 2840 | vassert(guard->Iex.Const.con->tag == Ico_U1); |
| 2841 | vassert(guard->Iex.Const.con->Ico.U1 == True); |
| 2842 | /* Figure out the load and result types, and the implied |
| 2843 | conversion operation. */ |
| 2844 | IRType cvtRes = Ity_INVALID, cvtArg = Ity_INVALID; |
| 2845 | typeOfIRLoadGOp(lg->cvt, &cvtRes, &cvtArg); |
| 2846 | IROp cvtOp = Iop_INVALID; |
| 2847 | switch (lg->cvt) { |
| 2848 | case ILGop_Ident32: break; |
| 2849 | case ILGop_8Uto32: cvtOp = Iop_8Uto32; break; |
| 2850 | case ILGop_8Sto32: cvtOp = Iop_8Sto32; break; |
| 2851 | case ILGop_16Uto32: cvtOp = Iop_16Uto32; break; |
| 2852 | case ILGop_16Sto32: cvtOp = Iop_16Sto32; break; |
| 2853 | default: vpanic("cprop_BB: unhandled ILGOp"); |
| 2854 | } |
| 2855 | /* Replace the placeholder NoOp by the required unconditional |
| 2856 | load. */ |
| 2857 | IRTemp tLoaded = newIRTemp(out->tyenv, cvtArg); |
| 2858 | out->stmts[ix] |
| 2859 | = IRStmt_WrTmp(tLoaded, |
| 2860 | IRExpr_Load(lg->end, cvtArg, lg->addr)); |
| 2861 | /* Replace the LoadG by a conversion from the loaded value's |
| 2862 | type to the required result type. */ |
| 2863 | out->stmts[ix+1] |
| 2864 | = IRStmt_WrTmp( |
| 2865 | lg->dst, cvtOp == Iop_INVALID |
| 2866 | ? IRExpr_RdTmp(tLoaded) |
| 2867 | : IRExpr_Unop(cvtOp, IRExpr_RdTmp(tLoaded))); |
| 2868 | } |
| 2869 | |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 2870 | return out; |
| 2871 | } |
| 2872 | |
| 2873 | |
sewardj | edf4d69 | 2004-08-17 13:52:58 +0000 | [diff] [blame] | 2874 | /*---------------------------------------------------------------*/ |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 2875 | /*--- Dead code (t = E) removal ---*/ |
| 2876 | /*---------------------------------------------------------------*/ |
| 2877 | |
sewardj | e810c19 | 2005-09-09 08:33:03 +0000 | [diff] [blame] | 2878 | /* As a side effect, also removes all code following an unconditional |
| 2879 | side exit. */ |
| 2880 | |
sewardj | ea602bc | 2004-10-14 21:40:12 +0000 | [diff] [blame] | 2881 | /* The type of the HashHW map is: a map from IRTemp to nothing |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 2882 | -- really just operating a set or IRTemps. |
| 2883 | */ |
| 2884 | |
sewardj | d503a32 | 2004-10-13 22:41:16 +0000 | [diff] [blame] | 2885 | inline |
| 2886 | static void addUses_Temp ( Bool* set, IRTemp tmp ) |
sewardj | 17442fe | 2004-09-20 14:54:28 +0000 | [diff] [blame] | 2887 | { |
sewardj | d503a32 | 2004-10-13 22:41:16 +0000 | [diff] [blame] | 2888 | set[(Int)tmp] = True; |
sewardj | 17442fe | 2004-09-20 14:54:28 +0000 | [diff] [blame] | 2889 | } |
| 2890 | |
sewardj | d503a32 | 2004-10-13 22:41:16 +0000 | [diff] [blame] | 2891 | static void addUses_Expr ( Bool* set, IRExpr* e ) |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 2892 | { |
| 2893 | Int i; |
| 2894 | switch (e->tag) { |
sewardj | d721703 | 2004-08-19 10:49:10 +0000 | [diff] [blame] | 2895 | case Iex_GetI: |
sewardj | eeac841 | 2004-11-02 00:26:55 +0000 | [diff] [blame] | 2896 | addUses_Expr(set, e->Iex.GetI.ix); |
sewardj | d721703 | 2004-08-19 10:49:10 +0000 | [diff] [blame] | 2897 | return; |
florian | 99dd03e | 2013-01-29 03:56:06 +0000 | [diff] [blame] | 2898 | case Iex_ITE: |
| 2899 | addUses_Expr(set, e->Iex.ITE.cond); |
| 2900 | addUses_Expr(set, e->Iex.ITE.iftrue); |
| 2901 | addUses_Expr(set, e->Iex.ITE.iffalse); |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 2902 | return; |
| 2903 | case Iex_CCall: |
| 2904 | for (i = 0; e->Iex.CCall.args[i]; i++) |
| 2905 | addUses_Expr(set, e->Iex.CCall.args[i]); |
| 2906 | return; |
sewardj | af1ceca | 2005-06-30 23:31:27 +0000 | [diff] [blame] | 2907 | case Iex_Load: |
| 2908 | addUses_Expr(set, e->Iex.Load.addr); |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 2909 | return; |
sewardj | 40c8026 | 2006-02-08 19:30:46 +0000 | [diff] [blame] | 2910 | case Iex_Qop: |
florian | 96d7cc3 | 2012-06-01 20:41:24 +0000 | [diff] [blame] | 2911 | addUses_Expr(set, e->Iex.Qop.details->arg1); |
| 2912 | addUses_Expr(set, e->Iex.Qop.details->arg2); |
| 2913 | addUses_Expr(set, e->Iex.Qop.details->arg3); |
| 2914 | addUses_Expr(set, e->Iex.Qop.details->arg4); |
sewardj | 40c8026 | 2006-02-08 19:30:46 +0000 | [diff] [blame] | 2915 | return; |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 2916 | case Iex_Triop: |
florian | 420bfa9 | 2012-06-02 20:29:22 +0000 | [diff] [blame] | 2917 | addUses_Expr(set, e->Iex.Triop.details->arg1); |
| 2918 | addUses_Expr(set, e->Iex.Triop.details->arg2); |
| 2919 | addUses_Expr(set, e->Iex.Triop.details->arg3); |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 2920 | return; |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 2921 | case Iex_Binop: |
| 2922 | addUses_Expr(set, e->Iex.Binop.arg1); |
| 2923 | addUses_Expr(set, e->Iex.Binop.arg2); |
| 2924 | return; |
| 2925 | case Iex_Unop: |
| 2926 | addUses_Expr(set, e->Iex.Unop.arg); |
| 2927 | return; |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2928 | case Iex_RdTmp: |
| 2929 | addUses_Temp(set, e->Iex.RdTmp.tmp); |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 2930 | return; |
| 2931 | case Iex_Const: |
| 2932 | case Iex_Get: |
| 2933 | return; |
| 2934 | default: |
| 2935 | vex_printf("\n"); |
| 2936 | ppIRExpr(e); |
| 2937 | vpanic("addUses_Expr"); |
| 2938 | } |
| 2939 | } |
| 2940 | |
sewardj | d503a32 | 2004-10-13 22:41:16 +0000 | [diff] [blame] | 2941 | static void addUses_Stmt ( Bool* set, IRStmt* st ) |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 2942 | { |
sewardj | 17442fe | 2004-09-20 14:54:28 +0000 | [diff] [blame] | 2943 | Int i; |
| 2944 | IRDirty* d; |
sewardj | e9d8a26 | 2009-07-01 08:06:34 +0000 | [diff] [blame] | 2945 | IRCAS* cas; |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 2946 | switch (st->tag) { |
sewardj | 5a9ffab | 2005-05-12 17:55:01 +0000 | [diff] [blame] | 2947 | case Ist_AbiHint: |
| 2948 | addUses_Expr(set, st->Ist.AbiHint.base); |
sewardj | 478646f | 2008-05-01 20:13:04 +0000 | [diff] [blame] | 2949 | addUses_Expr(set, st->Ist.AbiHint.nia); |
sewardj | 5a9ffab | 2005-05-12 17:55:01 +0000 | [diff] [blame] | 2950 | return; |
sewardj | d721703 | 2004-08-19 10:49:10 +0000 | [diff] [blame] | 2951 | case Ist_PutI: |
florian | d6f38b3 | 2012-05-31 15:46:18 +0000 | [diff] [blame] | 2952 | addUses_Expr(set, st->Ist.PutI.details->ix); |
| 2953 | addUses_Expr(set, st->Ist.PutI.details->data); |
sewardj | d721703 | 2004-08-19 10:49:10 +0000 | [diff] [blame] | 2954 | return; |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2955 | case Ist_WrTmp: |
| 2956 | addUses_Expr(set, st->Ist.WrTmp.data); |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 2957 | return; |
| 2958 | case Ist_Put: |
sewardj | 6d07636 | 2004-09-23 11:06:17 +0000 | [diff] [blame] | 2959 | addUses_Expr(set, st->Ist.Put.data); |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 2960 | return; |
sewardj | af1ceca | 2005-06-30 23:31:27 +0000 | [diff] [blame] | 2961 | case Ist_Store: |
| 2962 | addUses_Expr(set, st->Ist.Store.addr); |
| 2963 | addUses_Expr(set, st->Ist.Store.data); |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 2964 | return; |
sewardj | cfe046e | 2013-01-17 14:23:53 +0000 | [diff] [blame] | 2965 | case Ist_StoreG: { |
| 2966 | IRStoreG* sg = st->Ist.StoreG.details; |
| 2967 | addUses_Expr(set, sg->addr); |
| 2968 | addUses_Expr(set, sg->data); |
| 2969 | addUses_Expr(set, sg->guard); |
| 2970 | return; |
| 2971 | } |
| 2972 | case Ist_LoadG: { |
| 2973 | IRLoadG* lg = st->Ist.LoadG.details; |
| 2974 | addUses_Expr(set, lg->addr); |
| 2975 | addUses_Expr(set, lg->alt); |
| 2976 | addUses_Expr(set, lg->guard); |
| 2977 | return; |
| 2978 | } |
sewardj | e9d8a26 | 2009-07-01 08:06:34 +0000 | [diff] [blame] | 2979 | case Ist_CAS: |
| 2980 | cas = st->Ist.CAS.details; |
| 2981 | addUses_Expr(set, cas->addr); |
| 2982 | if (cas->expdHi) |
| 2983 | addUses_Expr(set, cas->expdHi); |
| 2984 | addUses_Expr(set, cas->expdLo); |
| 2985 | if (cas->dataHi) |
| 2986 | addUses_Expr(set, cas->dataHi); |
| 2987 | addUses_Expr(set, cas->dataLo); |
| 2988 | return; |
sewardj | e768e92 | 2009-11-26 17:17:37 +0000 | [diff] [blame] | 2989 | case Ist_LLSC: |
| 2990 | addUses_Expr(set, st->Ist.LLSC.addr); |
| 2991 | if (st->Ist.LLSC.storedata) |
| 2992 | addUses_Expr(set, st->Ist.LLSC.storedata); |
| 2993 | return; |
sewardj | 17442fe | 2004-09-20 14:54:28 +0000 | [diff] [blame] | 2994 | case Ist_Dirty: |
| 2995 | d = st->Ist.Dirty.details; |
| 2996 | if (d->mFx != Ifx_None) |
| 2997 | addUses_Expr(set, d->mAddr); |
sewardj | b8385d8 | 2004-11-02 01:34:15 +0000 | [diff] [blame] | 2998 | addUses_Expr(set, d->guard); |
sewardj | 74142b8 | 2013-08-08 10:28:59 +0000 | [diff] [blame] | 2999 | for (i = 0; d->args[i] != NULL; i++) { |
| 3000 | IRExpr* arg = d->args[i]; |
florian | 9041956 | 2013-08-15 20:54:52 +0000 | [diff] [blame] | 3001 | if (LIKELY(!is_IRExpr_VECRET_or_BBPTR(arg))) |
sewardj | 74142b8 | 2013-08-08 10:28:59 +0000 | [diff] [blame] | 3002 | addUses_Expr(set, arg); |
| 3003 | } |
sewardj | 17442fe | 2004-09-20 14:54:28 +0000 | [diff] [blame] | 3004 | return; |
sewardj | d2445f6 | 2005-03-21 00:15:53 +0000 | [diff] [blame] | 3005 | case Ist_NoOp: |
sewardj | f168931 | 2005-03-16 18:19:10 +0000 | [diff] [blame] | 3006 | case Ist_IMark: |
sewardj | c4356f0 | 2007-11-09 21:15:04 +0000 | [diff] [blame] | 3007 | case Ist_MBE: |
sewardj | 3e83893 | 2005-01-07 12:09:15 +0000 | [diff] [blame] | 3008 | return; |
sewardj | 17442fe | 2004-09-20 14:54:28 +0000 | [diff] [blame] | 3009 | case Ist_Exit: |
sewardj | 0276d4b | 2004-11-15 15:30:21 +0000 | [diff] [blame] | 3010 | addUses_Expr(set, st->Ist.Exit.guard); |
sewardj | 17442fe | 2004-09-20 14:54:28 +0000 | [diff] [blame] | 3011 | return; |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 3012 | default: |
| 3013 | vex_printf("\n"); |
| 3014 | ppIRStmt(st); |
| 3015 | vpanic("addUses_Stmt"); |
sewardj | d503a32 | 2004-10-13 22:41:16 +0000 | [diff] [blame] | 3016 | } |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 3017 | } |
| 3018 | |
| 3019 | |
sewardj | ba99931 | 2004-11-15 15:21:17 +0000 | [diff] [blame] | 3020 | /* Is this literally IRExpr_Const(IRConst_U1(False)) ? */ |
| 3021 | static Bool isZeroU1 ( IRExpr* e ) |
sewardj | d999788 | 2004-11-04 19:42:59 +0000 | [diff] [blame] | 3022 | { |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 3023 | return toBool( e->tag == Iex_Const |
| 3024 | && e->Iex.Const.con->tag == Ico_U1 |
| 3025 | && e->Iex.Const.con->Ico.U1 == False ); |
sewardj | d999788 | 2004-11-04 19:42:59 +0000 | [diff] [blame] | 3026 | } |
| 3027 | |
sewardj | e810c19 | 2005-09-09 08:33:03 +0000 | [diff] [blame] | 3028 | /* Is this literally IRExpr_Const(IRConst_U1(True)) ? */ |
| 3029 | static Bool isOneU1 ( IRExpr* e ) |
| 3030 | { |
| 3031 | return toBool( e->tag == Iex_Const |
| 3032 | && e->Iex.Const.con->tag == Ico_U1 |
| 3033 | && e->Iex.Const.con->Ico.U1 == True ); |
| 3034 | } |
| 3035 | |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 3036 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3037 | /* Note, this destructively modifies the given IRSB. */ |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 3038 | |
| 3039 | /* Scan backwards through statements, carrying a set of IRTemps which |
| 3040 | are known to be used after the current point. On encountering 't = |
| 3041 | E', delete the binding if it is not used. Otherwise, add any temp |
sewardj | e810c19 | 2005-09-09 08:33:03 +0000 | [diff] [blame] | 3042 | uses to the set and keep on moving backwards. |
| 3043 | |
| 3044 | As an enhancement, the first (backwards) pass searches for IR exits |
| 3045 | with always-taken conditions and notes the location of the earliest |
| 3046 | one in the block. If any such are found, a second pass copies the |
| 3047 | exit destination and jump kind to the bb-end. Then, the exit and |
| 3048 | all statements following it are turned into no-ops. |
| 3049 | */ |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 3050 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3051 | /* notstatic */ void do_deadcode_BB ( IRSB* bb ) |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 3052 | { |
sewardj | e810c19 | 2005-09-09 08:33:03 +0000 | [diff] [blame] | 3053 | Int i, i_unconditional_exit; |
sewardj | d503a32 | 2004-10-13 22:41:16 +0000 | [diff] [blame] | 3054 | Int n_tmps = bb->tyenv->types_used; |
| 3055 | Bool* set = LibVEX_Alloc(n_tmps * sizeof(Bool)); |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 3056 | IRStmt* st; |
| 3057 | |
sewardj | d503a32 | 2004-10-13 22:41:16 +0000 | [diff] [blame] | 3058 | for (i = 0; i < n_tmps; i++) |
| 3059 | set[i] = False; |
| 3060 | |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 3061 | /* start off by recording IRTemp uses in the next field. */ |
| 3062 | addUses_Expr(set, bb->next); |
| 3063 | |
sewardj | e810c19 | 2005-09-09 08:33:03 +0000 | [diff] [blame] | 3064 | /* First pass */ |
| 3065 | |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 3066 | /* Work backwards through the stmts */ |
sewardj | e810c19 | 2005-09-09 08:33:03 +0000 | [diff] [blame] | 3067 | i_unconditional_exit = -1; |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 3068 | for (i = bb->stmts_used-1; i >= 0; i--) { |
| 3069 | st = bb->stmts[i]; |
sewardj | 8bee6d1 | 2005-03-22 02:24:05 +0000 | [diff] [blame] | 3070 | if (st->tag == Ist_NoOp) |
sewardj | 84ff065 | 2004-08-23 16:16:08 +0000 | [diff] [blame] | 3071 | continue; |
sewardj | e810c19 | 2005-09-09 08:33:03 +0000 | [diff] [blame] | 3072 | /* take note of any unconditional exits */ |
| 3073 | if (st->tag == Ist_Exit |
| 3074 | && isOneU1(st->Ist.Exit.guard)) |
| 3075 | i_unconditional_exit = i; |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3076 | if (st->tag == Ist_WrTmp |
| 3077 | && set[(Int)(st->Ist.WrTmp.tmp)] == False) { |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 3078 | /* it's an IRTemp which never got used. Delete it. */ |
sewardj | 088bcb4 | 2004-08-19 17:16:52 +0000 | [diff] [blame] | 3079 | if (DEBUG_IROPT) { |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 3080 | vex_printf("DEAD: "); |
| 3081 | ppIRStmt(st); |
| 3082 | vex_printf("\n"); |
| 3083 | } |
sewardj | d2445f6 | 2005-03-21 00:15:53 +0000 | [diff] [blame] | 3084 | bb->stmts[i] = IRStmt_NoOp(); |
sewardj | d999788 | 2004-11-04 19:42:59 +0000 | [diff] [blame] | 3085 | } |
| 3086 | else |
| 3087 | if (st->tag == Ist_Dirty |
| 3088 | && st->Ist.Dirty.details->guard |
sewardj | ba99931 | 2004-11-15 15:21:17 +0000 | [diff] [blame] | 3089 | && isZeroU1(st->Ist.Dirty.details->guard)) { |
sewardj | d2445f6 | 2005-03-21 00:15:53 +0000 | [diff] [blame] | 3090 | /* This is a dirty helper which will never get called. |
| 3091 | Delete it. */ |
| 3092 | bb->stmts[i] = IRStmt_NoOp(); |
sewardj | d999788 | 2004-11-04 19:42:59 +0000 | [diff] [blame] | 3093 | } |
| 3094 | else { |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 3095 | /* Note any IRTemp uses made by the current statement. */ |
| 3096 | addUses_Stmt(set, st); |
| 3097 | } |
| 3098 | } |
sewardj | e810c19 | 2005-09-09 08:33:03 +0000 | [diff] [blame] | 3099 | |
| 3100 | /* Optional second pass: if any unconditional exits were found, |
| 3101 | delete them and all following statements. */ |
| 3102 | |
| 3103 | if (i_unconditional_exit != -1) { |
| 3104 | if (0) vex_printf("ZAPPING ALL FORWARDS from %d\n", |
| 3105 | i_unconditional_exit); |
| 3106 | vassert(i_unconditional_exit >= 0 |
| 3107 | && i_unconditional_exit < bb->stmts_used); |
| 3108 | bb->next |
| 3109 | = IRExpr_Const( bb->stmts[i_unconditional_exit]->Ist.Exit.dst ); |
| 3110 | bb->jumpkind |
| 3111 | = bb->stmts[i_unconditional_exit]->Ist.Exit.jk; |
sewardj | c6f970f | 2012-04-02 21:54:49 +0000 | [diff] [blame] | 3112 | bb->offsIP |
| 3113 | = bb->stmts[i_unconditional_exit]->Ist.Exit.offsIP; |
sewardj | e810c19 | 2005-09-09 08:33:03 +0000 | [diff] [blame] | 3114 | for (i = i_unconditional_exit; i < bb->stmts_used; i++) |
| 3115 | bb->stmts[i] = IRStmt_NoOp(); |
| 3116 | } |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 3117 | } |
| 3118 | |
sewardj | e810c19 | 2005-09-09 08:33:03 +0000 | [diff] [blame] | 3119 | |
sewardj | 84ff065 | 2004-08-23 16:16:08 +0000 | [diff] [blame] | 3120 | /*---------------------------------------------------------------*/ |
| 3121 | /*--- Specialisation of helper function calls, in ---*/ |
| 3122 | /*--- collaboration with the front end ---*/ |
| 3123 | /*---------------------------------------------------------------*/ |
| 3124 | |
| 3125 | static |
sewardj | be91791 | 2010-08-22 12:38:53 +0000 | [diff] [blame] | 3126 | IRSB* spec_helpers_BB( |
| 3127 | IRSB* bb, |
florian | 1ff4756 | 2012-10-21 02:09:51 +0000 | [diff] [blame] | 3128 | IRExpr* (*specHelper) (const HChar*, IRExpr**, IRStmt**, Int) |
sewardj | be91791 | 2010-08-22 12:38:53 +0000 | [diff] [blame] | 3129 | ) |
sewardj | 84ff065 | 2004-08-23 16:16:08 +0000 | [diff] [blame] | 3130 | { |
sewardj | b923075 | 2004-12-29 19:25:06 +0000 | [diff] [blame] | 3131 | Int i; |
sewardj | 84ff065 | 2004-08-23 16:16:08 +0000 | [diff] [blame] | 3132 | IRStmt* st; |
| 3133 | IRExpr* ex; |
sewardj | b923075 | 2004-12-29 19:25:06 +0000 | [diff] [blame] | 3134 | Bool any = False; |
sewardj | 84ff065 | 2004-08-23 16:16:08 +0000 | [diff] [blame] | 3135 | |
| 3136 | for (i = bb->stmts_used-1; i >= 0; i--) { |
| 3137 | st = bb->stmts[i]; |
| 3138 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3139 | if (st->tag != Ist_WrTmp |
| 3140 | || st->Ist.WrTmp.data->tag != Iex_CCall) |
sewardj | 8bee6d1 | 2005-03-22 02:24:05 +0000 | [diff] [blame] | 3141 | continue; |
sewardj | 84ff065 | 2004-08-23 16:16:08 +0000 | [diff] [blame] | 3142 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3143 | ex = (*specHelper)( st->Ist.WrTmp.data->Iex.CCall.cee->name, |
sewardj | be91791 | 2010-08-22 12:38:53 +0000 | [diff] [blame] | 3144 | st->Ist.WrTmp.data->Iex.CCall.args, |
| 3145 | &bb->stmts[0], i ); |
sewardj | 84ff065 | 2004-08-23 16:16:08 +0000 | [diff] [blame] | 3146 | if (!ex) |
sewardj | aba4fff | 2004-10-08 21:37:15 +0000 | [diff] [blame] | 3147 | /* the front end can't think of a suitable replacement */ |
| 3148 | continue; |
sewardj | 84ff065 | 2004-08-23 16:16:08 +0000 | [diff] [blame] | 3149 | |
| 3150 | /* We got something better. Install it in the bb. */ |
sewardj | b923075 | 2004-12-29 19:25:06 +0000 | [diff] [blame] | 3151 | any = True; |
sewardj | 84ff065 | 2004-08-23 16:16:08 +0000 | [diff] [blame] | 3152 | bb->stmts[i] |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3153 | = IRStmt_WrTmp(st->Ist.WrTmp.tmp, ex); |
sewardj | 84ff065 | 2004-08-23 16:16:08 +0000 | [diff] [blame] | 3154 | |
| 3155 | if (0) { |
| 3156 | vex_printf("SPEC: "); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3157 | ppIRExpr(st->Ist.WrTmp.data); |
sewardj | 84ff065 | 2004-08-23 16:16:08 +0000 | [diff] [blame] | 3158 | vex_printf(" --> "); |
| 3159 | ppIRExpr(ex); |
| 3160 | vex_printf("\n"); |
| 3161 | } |
| 3162 | } |
sewardj | b923075 | 2004-12-29 19:25:06 +0000 | [diff] [blame] | 3163 | |
| 3164 | if (any) |
| 3165 | bb = flatten_BB(bb); |
| 3166 | return bb; |
sewardj | 84ff065 | 2004-08-23 16:16:08 +0000 | [diff] [blame] | 3167 | } |
| 3168 | |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 3169 | |
| 3170 | /*---------------------------------------------------------------*/ |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 3171 | /*--- Determination of guest state aliasing relationships ---*/ |
| 3172 | /*---------------------------------------------------------------*/ |
| 3173 | |
| 3174 | /* These are helper functions for CSE and GetI/PutI transformations. |
| 3175 | |
| 3176 | Determine, to the extent possible, the relationship between two |
| 3177 | guest state accesses. The possible outcomes are: |
| 3178 | |
| 3179 | * Exact alias. These two accesses denote precisely the same |
| 3180 | piece of the guest state. |
| 3181 | |
| 3182 | * Definitely no alias. These two accesses are guaranteed not to |
| 3183 | overlap any part of the guest state. |
| 3184 | |
| 3185 | * Unknown -- if neither of the above can be established. |
| 3186 | |
| 3187 | If in doubt, return Unknown. */ |
| 3188 | |
| 3189 | typedef |
| 3190 | enum { ExactAlias, NoAlias, UnknownAlias } |
| 3191 | GSAliasing; |
| 3192 | |
| 3193 | |
| 3194 | /* Produces the alias relation between an indexed guest |
| 3195 | state access and a non-indexed access. */ |
| 3196 | |
| 3197 | static |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3198 | GSAliasing getAliasingRelation_IC ( IRRegArray* descr1, IRExpr* ix1, |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 3199 | Int offset2, IRType ty2 ) |
| 3200 | { |
| 3201 | UInt minoff1, maxoff1, minoff2, maxoff2; |
| 3202 | |
| 3203 | getArrayBounds( descr1, &minoff1, &maxoff1 ); |
| 3204 | minoff2 = offset2; |
| 3205 | maxoff2 = minoff2 + sizeofIRType(ty2) - 1; |
| 3206 | |
| 3207 | if (maxoff1 < minoff2 || maxoff2 < minoff1) |
| 3208 | return NoAlias; |
| 3209 | |
| 3210 | /* Could probably do better here if required. For the moment |
| 3211 | however just claim not to know anything more. */ |
| 3212 | return UnknownAlias; |
| 3213 | } |
| 3214 | |
| 3215 | |
| 3216 | /* Produces the alias relation between two indexed guest state |
| 3217 | accesses. */ |
| 3218 | |
| 3219 | static |
| 3220 | GSAliasing getAliasingRelation_II ( |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3221 | IRRegArray* descr1, IRExpr* ix1, Int bias1, |
| 3222 | IRRegArray* descr2, IRExpr* ix2, Int bias2 |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 3223 | ) |
| 3224 | { |
| 3225 | UInt minoff1, maxoff1, minoff2, maxoff2; |
| 3226 | Int iters; |
| 3227 | |
| 3228 | /* First try hard to show they don't alias. */ |
| 3229 | getArrayBounds( descr1, &minoff1, &maxoff1 ); |
| 3230 | getArrayBounds( descr2, &minoff2, &maxoff2 ); |
| 3231 | if (maxoff1 < minoff2 || maxoff2 < minoff1) |
| 3232 | return NoAlias; |
| 3233 | |
| 3234 | /* So the two arrays at least partially overlap. To get any |
| 3235 | further we'll have to be sure that the descriptors are |
| 3236 | identical. */ |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3237 | if (!eqIRRegArray(descr1, descr2)) |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 3238 | return UnknownAlias; |
| 3239 | |
| 3240 | /* The descriptors are identical. Now the only difference can be |
| 3241 | in the index expressions. If they cannot be shown to be |
| 3242 | identical, we have to say we don't know what the aliasing |
| 3243 | relation will be. Now, since the IR is flattened, the index |
| 3244 | expressions should be atoms -- either consts or tmps. So that |
| 3245 | makes the comparison simple. */ |
| 3246 | vassert(isIRAtom(ix1)); |
| 3247 | vassert(isIRAtom(ix2)); |
| 3248 | if (!eqIRAtom(ix1,ix2)) |
| 3249 | return UnknownAlias; |
| 3250 | |
| 3251 | /* Ok, the index expressions are identical. So now the only way |
| 3252 | they can be different is in the bias. Normalise this |
| 3253 | paranoidly, to reliably establish equality/non-equality. */ |
| 3254 | |
| 3255 | /* So now we know that the GetI and PutI index the same array |
| 3256 | with the same base. Are the offsets the same, modulo the |
| 3257 | array size? Do this paranoidly. */ |
| 3258 | vassert(descr1->nElems == descr2->nElems); |
| 3259 | vassert(descr1->elemTy == descr2->elemTy); |
| 3260 | vassert(descr1->base == descr2->base); |
| 3261 | iters = 0; |
| 3262 | while (bias1 < 0 || bias2 < 0) { |
| 3263 | bias1 += descr1->nElems; |
| 3264 | bias2 += descr1->nElems; |
| 3265 | iters++; |
| 3266 | if (iters > 10) |
| 3267 | vpanic("getAliasingRelation: iters"); |
| 3268 | } |
| 3269 | vassert(bias1 >= 0 && bias2 >= 0); |
| 3270 | bias1 %= descr1->nElems; |
| 3271 | bias2 %= descr1->nElems; |
| 3272 | vassert(bias1 >= 0 && bias1 < descr1->nElems); |
| 3273 | vassert(bias2 >= 0 && bias2 < descr1->nElems); |
| 3274 | |
| 3275 | /* Finally, biasP and biasG are normalised into the range |
| 3276 | 0 .. descrP/G->nElems - 1. And so we can establish |
| 3277 | equality/non-equality. */ |
| 3278 | |
| 3279 | return bias1==bias2 ? ExactAlias : NoAlias; |
| 3280 | } |
| 3281 | |
| 3282 | |
| 3283 | /*---------------------------------------------------------------*/ |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3284 | /*--- Common Subexpression Elimination ---*/ |
| 3285 | /*---------------------------------------------------------------*/ |
| 3286 | |
| 3287 | /* Expensive in time and space. */ |
| 3288 | |
| 3289 | /* Uses two environments: |
| 3290 | a IRTemp -> IRTemp mapping |
| 3291 | a mapping from AvailExpr* to IRTemp |
| 3292 | */ |
| 3293 | |
| 3294 | typedef |
| 3295 | struct { |
sewardj | e857804 | 2012-04-13 11:42:50 +0000 | [diff] [blame] | 3296 | enum { TCc, TCt } tag; |
| 3297 | union { IRTemp tmp; IRConst* con; } u; |
| 3298 | } |
| 3299 | TmpOrConst; |
| 3300 | |
| 3301 | static Bool eqTmpOrConst ( TmpOrConst* tc1, TmpOrConst* tc2 ) |
| 3302 | { |
| 3303 | if (tc1->tag != tc2->tag) |
| 3304 | return False; |
| 3305 | switch (tc1->tag) { |
| 3306 | case TCc: |
| 3307 | return eqIRConst(tc1->u.con, tc2->u.con); |
| 3308 | case TCt: |
| 3309 | return tc1->u.tmp == tc2->u.tmp; |
| 3310 | default: |
| 3311 | vpanic("eqTmpOrConst"); |
| 3312 | } |
| 3313 | } |
| 3314 | |
| 3315 | static Bool eqIRCallee ( IRCallee* cee1, IRCallee* cee2 ) |
| 3316 | { |
| 3317 | Bool eq = cee1->addr == cee2->addr; |
| 3318 | if (eq) { |
| 3319 | vassert(cee1->regparms == cee2->regparms); |
| 3320 | vassert(cee1->mcx_mask == cee2->mcx_mask); |
| 3321 | /* Names should be the same too, but we don't bother to |
| 3322 | check. */ |
| 3323 | } |
| 3324 | return eq; |
| 3325 | } |
| 3326 | |
| 3327 | /* Convert a NULL terminated IRExpr* vector to an array of |
| 3328 | TmpOrConsts, and a length. */ |
| 3329 | static void irExprVec_to_TmpOrConsts ( /*OUT*/TmpOrConst** outs, |
| 3330 | /*OUT*/Int* nOuts, |
| 3331 | IRExpr** ins ) |
| 3332 | { |
| 3333 | Int i, n; |
| 3334 | /* We have to make two passes, one to count, one to copy. */ |
| 3335 | for (n = 0; ins[n]; n++) |
| 3336 | ; |
| 3337 | *outs = LibVEX_Alloc(n * sizeof(TmpOrConst)); |
| 3338 | *nOuts = n; |
| 3339 | /* and now copy .. */ |
| 3340 | for (i = 0; i < n; i++) { |
| 3341 | IRExpr* arg = ins[i]; |
| 3342 | TmpOrConst* dst = &(*outs)[i]; |
| 3343 | if (arg->tag == Iex_RdTmp) { |
| 3344 | dst->tag = TCt; |
| 3345 | dst->u.tmp = arg->Iex.RdTmp.tmp; |
| 3346 | } |
| 3347 | else if (arg->tag == Iex_Const) { |
| 3348 | dst->tag = TCc; |
| 3349 | dst->u.con = arg->Iex.Const.con; |
| 3350 | } |
| 3351 | else { |
| 3352 | /* Failure of this is serious; it means that the presented arg |
| 3353 | isn't an IR atom, as it should be. */ |
| 3354 | vpanic("irExprVec_to_TmpOrConsts"); |
| 3355 | } |
| 3356 | } |
| 3357 | } |
| 3358 | |
| 3359 | typedef |
| 3360 | struct { |
florian | b05c20c | 2013-01-31 02:04:02 +0000 | [diff] [blame] | 3361 | enum { Ut, Btt, Btc, Bct, Cf64i, Ittt, Itct, Ittc, Itcc, GetIt, |
florian | da25edd | 2012-09-12 16:40:54 +0000 | [diff] [blame] | 3362 | CCall |
| 3363 | } tag; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3364 | union { |
| 3365 | /* unop(tmp) */ |
| 3366 | struct { |
| 3367 | IROp op; |
| 3368 | IRTemp arg; |
| 3369 | } Ut; |
| 3370 | /* binop(tmp,tmp) */ |
| 3371 | struct { |
| 3372 | IROp op; |
| 3373 | IRTemp arg1; |
| 3374 | IRTemp arg2; |
| 3375 | } Btt; |
| 3376 | /* binop(tmp,const) */ |
| 3377 | struct { |
| 3378 | IROp op; |
| 3379 | IRTemp arg1; |
| 3380 | IRConst con2; |
| 3381 | } Btc; |
| 3382 | /* binop(const,tmp) */ |
| 3383 | struct { |
| 3384 | IROp op; |
| 3385 | IRConst con1; |
| 3386 | IRTemp arg2; |
| 3387 | } Bct; |
| 3388 | /* F64i-style const */ |
| 3389 | struct { |
| 3390 | ULong f64i; |
| 3391 | } Cf64i; |
florian | 99dd03e | 2013-01-29 03:56:06 +0000 | [diff] [blame] | 3392 | /* ITE(tmp,tmp,tmp) */ |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 3393 | struct { |
| 3394 | IRTemp co; |
florian | b05c20c | 2013-01-31 02:04:02 +0000 | [diff] [blame] | 3395 | IRTemp e1; |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 3396 | IRTemp e0; |
florian | b05c20c | 2013-01-31 02:04:02 +0000 | [diff] [blame] | 3397 | } Ittt; |
florian | 99dd03e | 2013-01-29 03:56:06 +0000 | [diff] [blame] | 3398 | /* ITE(tmp,tmp,const) */ |
florian | da25edd | 2012-09-12 16:40:54 +0000 | [diff] [blame] | 3399 | struct { |
| 3400 | IRTemp co; |
florian | b05c20c | 2013-01-31 02:04:02 +0000 | [diff] [blame] | 3401 | IRTemp e1; |
florian | da25edd | 2012-09-12 16:40:54 +0000 | [diff] [blame] | 3402 | IRConst con0; |
florian | b05c20c | 2013-01-31 02:04:02 +0000 | [diff] [blame] | 3403 | } Ittc; |
florian | 99dd03e | 2013-01-29 03:56:06 +0000 | [diff] [blame] | 3404 | /* ITE(tmp,const,tmp) */ |
florian | da25edd | 2012-09-12 16:40:54 +0000 | [diff] [blame] | 3405 | struct { |
| 3406 | IRTemp co; |
florian | b05c20c | 2013-01-31 02:04:02 +0000 | [diff] [blame] | 3407 | IRConst con1; |
florian | da25edd | 2012-09-12 16:40:54 +0000 | [diff] [blame] | 3408 | IRTemp e0; |
florian | b05c20c | 2013-01-31 02:04:02 +0000 | [diff] [blame] | 3409 | } Itct; |
florian | 99dd03e | 2013-01-29 03:56:06 +0000 | [diff] [blame] | 3410 | /* ITE(tmp,const,const) */ |
florian | da25edd | 2012-09-12 16:40:54 +0000 | [diff] [blame] | 3411 | struct { |
| 3412 | IRTemp co; |
florian | b05c20c | 2013-01-31 02:04:02 +0000 | [diff] [blame] | 3413 | IRConst con1; |
florian | da25edd | 2012-09-12 16:40:54 +0000 | [diff] [blame] | 3414 | IRConst con0; |
florian | b05c20c | 2013-01-31 02:04:02 +0000 | [diff] [blame] | 3415 | } Itcc; |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 3416 | /* GetI(descr,tmp,bias)*/ |
| 3417 | struct { |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3418 | IRRegArray* descr; |
| 3419 | IRTemp ix; |
| 3420 | Int bias; |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 3421 | } GetIt; |
sewardj | e857804 | 2012-04-13 11:42:50 +0000 | [diff] [blame] | 3422 | /* Clean helper call */ |
| 3423 | struct { |
| 3424 | IRCallee* cee; |
| 3425 | TmpOrConst* args; |
| 3426 | Int nArgs; |
| 3427 | IRType retty; |
| 3428 | } CCall; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3429 | } u; |
| 3430 | } |
| 3431 | AvailExpr; |
| 3432 | |
| 3433 | static Bool eq_AvailExpr ( AvailExpr* a1, AvailExpr* a2 ) |
| 3434 | { |
sewardj | e857804 | 2012-04-13 11:42:50 +0000 | [diff] [blame] | 3435 | if (LIKELY(a1->tag != a2->tag)) |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3436 | return False; |
| 3437 | switch (a1->tag) { |
| 3438 | case Ut: |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 3439 | return toBool( |
| 3440 | a1->u.Ut.op == a2->u.Ut.op |
| 3441 | && a1->u.Ut.arg == a2->u.Ut.arg); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3442 | case Btt: |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 3443 | return toBool( |
| 3444 | a1->u.Btt.op == a2->u.Btt.op |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3445 | && a1->u.Btt.arg1 == a2->u.Btt.arg1 |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 3446 | && a1->u.Btt.arg2 == a2->u.Btt.arg2); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3447 | case Btc: |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 3448 | return toBool( |
| 3449 | a1->u.Btc.op == a2->u.Btc.op |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3450 | && a1->u.Btc.arg1 == a2->u.Btc.arg1 |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 3451 | && eqIRConst(&a1->u.Btc.con2, &a2->u.Btc.con2)); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3452 | case Bct: |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 3453 | return toBool( |
| 3454 | a1->u.Bct.op == a2->u.Bct.op |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3455 | && a1->u.Bct.arg2 == a2->u.Bct.arg2 |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 3456 | && eqIRConst(&a1->u.Bct.con1, &a2->u.Bct.con1)); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3457 | case Cf64i: |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 3458 | return toBool(a1->u.Cf64i.f64i == a2->u.Cf64i.f64i); |
florian | b05c20c | 2013-01-31 02:04:02 +0000 | [diff] [blame] | 3459 | case Ittt: |
| 3460 | return toBool(a1->u.Ittt.co == a2->u.Ittt.co |
| 3461 | && a1->u.Ittt.e1 == a2->u.Ittt.e1 |
| 3462 | && a1->u.Ittt.e0 == a2->u.Ittt.e0); |
| 3463 | case Ittc: |
| 3464 | return toBool(a1->u.Ittc.co == a2->u.Ittc.co |
| 3465 | && a1->u.Ittc.e1 == a2->u.Ittc.e1 |
| 3466 | && eqIRConst(&a1->u.Ittc.con0, &a2->u.Ittc.con0)); |
| 3467 | case Itct: |
| 3468 | return toBool(a1->u.Itct.co == a2->u.Itct.co |
| 3469 | && eqIRConst(&a1->u.Itct.con1, &a2->u.Itct.con1) |
| 3470 | && a1->u.Itct.e0 == a2->u.Itct.e0); |
| 3471 | case Itcc: |
| 3472 | return toBool(a1->u.Itcc.co == a2->u.Itcc.co |
| 3473 | && eqIRConst(&a1->u.Itcc.con1, &a2->u.Itcc.con1) |
| 3474 | && eqIRConst(&a1->u.Itcc.con0, &a2->u.Itcc.con0)); |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 3475 | case GetIt: |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3476 | return toBool(eqIRRegArray(a1->u.GetIt.descr, a2->u.GetIt.descr) |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 3477 | && a1->u.GetIt.ix == a2->u.GetIt.ix |
| 3478 | && a1->u.GetIt.bias == a2->u.GetIt.bias); |
sewardj | e857804 | 2012-04-13 11:42:50 +0000 | [diff] [blame] | 3479 | case CCall: { |
| 3480 | Int i, n; |
| 3481 | Bool eq = a1->u.CCall.nArgs == a2->u.CCall.nArgs |
| 3482 | && eqIRCallee(a1->u.CCall.cee, a2->u.CCall.cee); |
| 3483 | if (eq) { |
| 3484 | n = a1->u.CCall.nArgs; |
| 3485 | for (i = 0; i < n; i++) { |
| 3486 | if (!eqTmpOrConst( &a1->u.CCall.args[i], |
| 3487 | &a2->u.CCall.args[i] )) { |
| 3488 | eq = False; |
| 3489 | break; |
| 3490 | } |
| 3491 | } |
| 3492 | } |
| 3493 | if (eq) vassert(a1->u.CCall.retty == a2->u.CCall.retty); |
| 3494 | return eq; |
| 3495 | } |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3496 | default: vpanic("eq_AvailExpr"); |
| 3497 | } |
| 3498 | } |
| 3499 | |
| 3500 | static IRExpr* availExpr_to_IRExpr ( AvailExpr* ae ) |
| 3501 | { |
florian | b05c20c | 2013-01-31 02:04:02 +0000 | [diff] [blame] | 3502 | IRConst *con, *con0, *con1; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3503 | switch (ae->tag) { |
| 3504 | case Ut: |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3505 | return IRExpr_Unop( ae->u.Ut.op, IRExpr_RdTmp(ae->u.Ut.arg) ); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3506 | case Btt: |
| 3507 | return IRExpr_Binop( ae->u.Btt.op, |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3508 | IRExpr_RdTmp(ae->u.Btt.arg1), |
| 3509 | IRExpr_RdTmp(ae->u.Btt.arg2) ); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3510 | case Btc: |
| 3511 | con = LibVEX_Alloc(sizeof(IRConst)); |
| 3512 | *con = ae->u.Btc.con2; |
| 3513 | return IRExpr_Binop( ae->u.Btc.op, |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3514 | IRExpr_RdTmp(ae->u.Btc.arg1), |
| 3515 | IRExpr_Const(con) ); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3516 | case Bct: |
| 3517 | con = LibVEX_Alloc(sizeof(IRConst)); |
| 3518 | *con = ae->u.Bct.con1; |
| 3519 | return IRExpr_Binop( ae->u.Bct.op, |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3520 | IRExpr_Const(con), |
| 3521 | IRExpr_RdTmp(ae->u.Bct.arg2) ); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3522 | case Cf64i: |
| 3523 | return IRExpr_Const(IRConst_F64i(ae->u.Cf64i.f64i)); |
florian | b05c20c | 2013-01-31 02:04:02 +0000 | [diff] [blame] | 3524 | case Ittt: |
| 3525 | return IRExpr_ITE(IRExpr_RdTmp(ae->u.Ittt.co), |
| 3526 | IRExpr_RdTmp(ae->u.Ittt.e1), |
| 3527 | IRExpr_RdTmp(ae->u.Ittt.e0)); |
| 3528 | case Ittc: |
florian | da25edd | 2012-09-12 16:40:54 +0000 | [diff] [blame] | 3529 | con0 = LibVEX_Alloc(sizeof(IRConst)); |
florian | b05c20c | 2013-01-31 02:04:02 +0000 | [diff] [blame] | 3530 | *con0 = ae->u.Ittc.con0; |
| 3531 | return IRExpr_ITE(IRExpr_RdTmp(ae->u.Ittc.co), |
| 3532 | IRExpr_RdTmp(ae->u.Ittc.e1), |
florian | 99dd03e | 2013-01-29 03:56:06 +0000 | [diff] [blame] | 3533 | IRExpr_Const(con0)); |
florian | b05c20c | 2013-01-31 02:04:02 +0000 | [diff] [blame] | 3534 | case Itct: |
| 3535 | con1 = LibVEX_Alloc(sizeof(IRConst)); |
| 3536 | *con1 = ae->u.Itct.con1; |
| 3537 | return IRExpr_ITE(IRExpr_RdTmp(ae->u.Itct.co), |
| 3538 | IRExpr_Const(con1), |
| 3539 | IRExpr_RdTmp(ae->u.Itct.e0)); |
florian | 99dd03e | 2013-01-29 03:56:06 +0000 | [diff] [blame] | 3540 | |
florian | b05c20c | 2013-01-31 02:04:02 +0000 | [diff] [blame] | 3541 | case Itcc: |
florian | da25edd | 2012-09-12 16:40:54 +0000 | [diff] [blame] | 3542 | con0 = LibVEX_Alloc(sizeof(IRConst)); |
florian | b05c20c | 2013-01-31 02:04:02 +0000 | [diff] [blame] | 3543 | con1 = LibVEX_Alloc(sizeof(IRConst)); |
| 3544 | *con0 = ae->u.Itcc.con0; |
| 3545 | *con1 = ae->u.Itcc.con1; |
| 3546 | return IRExpr_ITE(IRExpr_RdTmp(ae->u.Itcc.co), |
| 3547 | IRExpr_Const(con1), |
florian | 99dd03e | 2013-01-29 03:56:06 +0000 | [diff] [blame] | 3548 | IRExpr_Const(con0)); |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 3549 | case GetIt: |
| 3550 | return IRExpr_GetI(ae->u.GetIt.descr, |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3551 | IRExpr_RdTmp(ae->u.GetIt.ix), |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 3552 | ae->u.GetIt.bias); |
sewardj | e857804 | 2012-04-13 11:42:50 +0000 | [diff] [blame] | 3553 | case CCall: { |
| 3554 | Int i, n = ae->u.CCall.nArgs; |
| 3555 | vassert(n >= 0); |
| 3556 | IRExpr** vec = LibVEX_Alloc((n+1) * sizeof(IRExpr*)); |
| 3557 | vec[n] = NULL; |
| 3558 | for (i = 0; i < n; i++) { |
| 3559 | TmpOrConst* tc = &ae->u.CCall.args[i]; |
| 3560 | if (tc->tag == TCc) { |
| 3561 | vec[i] = IRExpr_Const(tc->u.con); |
| 3562 | } |
| 3563 | else if (tc->tag == TCt) { |
| 3564 | vec[i] = IRExpr_RdTmp(tc->u.tmp); |
| 3565 | } |
| 3566 | else vpanic("availExpr_to_IRExpr:CCall-arg"); |
| 3567 | } |
| 3568 | return IRExpr_CCall(ae->u.CCall.cee, |
| 3569 | ae->u.CCall.retty, |
| 3570 | vec); |
| 3571 | } |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3572 | default: |
| 3573 | vpanic("availExpr_to_IRExpr"); |
| 3574 | } |
| 3575 | } |
| 3576 | |
| 3577 | inline |
| 3578 | static IRTemp subst_AvailExpr_Temp ( HashHW* env, IRTemp tmp ) |
| 3579 | { |
| 3580 | HWord res; |
| 3581 | /* env :: IRTemp -> IRTemp */ |
| 3582 | if (lookupHHW( env, &res, (HWord)tmp )) |
| 3583 | return (IRTemp)res; |
| 3584 | else |
| 3585 | return tmp; |
| 3586 | } |
| 3587 | |
| 3588 | static void subst_AvailExpr ( HashHW* env, AvailExpr* ae ) |
| 3589 | { |
| 3590 | /* env :: IRTemp -> IRTemp */ |
| 3591 | switch (ae->tag) { |
| 3592 | case Ut: |
| 3593 | ae->u.Ut.arg = subst_AvailExpr_Temp( env, ae->u.Ut.arg ); |
| 3594 | break; |
| 3595 | case Btt: |
| 3596 | ae->u.Btt.arg1 = subst_AvailExpr_Temp( env, ae->u.Btt.arg1 ); |
| 3597 | ae->u.Btt.arg2 = subst_AvailExpr_Temp( env, ae->u.Btt.arg2 ); |
| 3598 | break; |
| 3599 | case Btc: |
| 3600 | ae->u.Btc.arg1 = subst_AvailExpr_Temp( env, ae->u.Btc.arg1 ); |
| 3601 | break; |
| 3602 | case Bct: |
| 3603 | ae->u.Bct.arg2 = subst_AvailExpr_Temp( env, ae->u.Bct.arg2 ); |
| 3604 | break; |
| 3605 | case Cf64i: |
| 3606 | break; |
florian | b05c20c | 2013-01-31 02:04:02 +0000 | [diff] [blame] | 3607 | case Ittt: |
| 3608 | ae->u.Ittt.co = subst_AvailExpr_Temp( env, ae->u.Ittt.co ); |
| 3609 | ae->u.Ittt.e1 = subst_AvailExpr_Temp( env, ae->u.Ittt.e1 ); |
| 3610 | ae->u.Ittt.e0 = subst_AvailExpr_Temp( env, ae->u.Ittt.e0 ); |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 3611 | break; |
florian | b05c20c | 2013-01-31 02:04:02 +0000 | [diff] [blame] | 3612 | case Ittc: |
| 3613 | ae->u.Ittc.co = subst_AvailExpr_Temp( env, ae->u.Ittc.co ); |
| 3614 | ae->u.Ittc.e1 = subst_AvailExpr_Temp( env, ae->u.Ittc.e1 ); |
florian | da25edd | 2012-09-12 16:40:54 +0000 | [diff] [blame] | 3615 | break; |
florian | b05c20c | 2013-01-31 02:04:02 +0000 | [diff] [blame] | 3616 | case Itct: |
| 3617 | ae->u.Itct.co = subst_AvailExpr_Temp( env, ae->u.Itct.co ); |
| 3618 | ae->u.Itct.e0 = subst_AvailExpr_Temp( env, ae->u.Itct.e0 ); |
florian | da25edd | 2012-09-12 16:40:54 +0000 | [diff] [blame] | 3619 | break; |
florian | b05c20c | 2013-01-31 02:04:02 +0000 | [diff] [blame] | 3620 | case Itcc: |
| 3621 | ae->u.Itcc.co = subst_AvailExpr_Temp( env, ae->u.Itcc.co ); |
florian | da25edd | 2012-09-12 16:40:54 +0000 | [diff] [blame] | 3622 | break; |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 3623 | case GetIt: |
| 3624 | ae->u.GetIt.ix = subst_AvailExpr_Temp( env, ae->u.GetIt.ix ); |
| 3625 | break; |
sewardj | e857804 | 2012-04-13 11:42:50 +0000 | [diff] [blame] | 3626 | case CCall: { |
| 3627 | Int i, n = ae->u.CCall.nArgs;; |
| 3628 | for (i = 0; i < n; i++) { |
| 3629 | TmpOrConst* tc = &ae->u.CCall.args[i]; |
| 3630 | if (tc->tag == TCt) { |
| 3631 | tc->u.tmp = subst_AvailExpr_Temp( env, tc->u.tmp ); |
| 3632 | } |
| 3633 | } |
| 3634 | break; |
| 3635 | } |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3636 | default: |
| 3637 | vpanic("subst_AvailExpr"); |
| 3638 | } |
| 3639 | } |
| 3640 | |
| 3641 | static AvailExpr* irExpr_to_AvailExpr ( IRExpr* e ) |
| 3642 | { |
| 3643 | AvailExpr* ae; |
| 3644 | |
florian | c1b9e39 | 2012-09-20 02:40:57 +0000 | [diff] [blame] | 3645 | switch (e->tag) { |
| 3646 | case Iex_Unop: |
| 3647 | if (e->Iex.Unop.arg->tag == Iex_RdTmp) { |
| 3648 | ae = LibVEX_Alloc(sizeof(AvailExpr)); |
| 3649 | ae->tag = Ut; |
| 3650 | ae->u.Ut.op = e->Iex.Unop.op; |
| 3651 | ae->u.Ut.arg = e->Iex.Unop.arg->Iex.RdTmp.tmp; |
| 3652 | return ae; |
| 3653 | } |
| 3654 | break; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3655 | |
florian | c1b9e39 | 2012-09-20 02:40:57 +0000 | [diff] [blame] | 3656 | case Iex_Binop: |
| 3657 | if (e->Iex.Binop.arg1->tag == Iex_RdTmp) { |
| 3658 | if (e->Iex.Binop.arg2->tag == Iex_RdTmp) { |
| 3659 | ae = LibVEX_Alloc(sizeof(AvailExpr)); |
| 3660 | ae->tag = Btt; |
| 3661 | ae->u.Btt.op = e->Iex.Binop.op; |
| 3662 | ae->u.Btt.arg1 = e->Iex.Binop.arg1->Iex.RdTmp.tmp; |
| 3663 | ae->u.Btt.arg2 = e->Iex.Binop.arg2->Iex.RdTmp.tmp; |
| 3664 | return ae; |
| 3665 | } |
| 3666 | if (e->Iex.Binop.arg2->tag == Iex_Const) { |
| 3667 | ae = LibVEX_Alloc(sizeof(AvailExpr)); |
| 3668 | ae->tag = Btc; |
| 3669 | ae->u.Btc.op = e->Iex.Binop.op; |
| 3670 | ae->u.Btc.arg1 = e->Iex.Binop.arg1->Iex.RdTmp.tmp; |
| 3671 | ae->u.Btc.con2 = *(e->Iex.Binop.arg2->Iex.Const.con); |
| 3672 | return ae; |
| 3673 | } |
| 3674 | } else if (e->Iex.Binop.arg1->tag == Iex_Const |
| 3675 | && e->Iex.Binop.arg2->tag == Iex_RdTmp) { |
| 3676 | ae = LibVEX_Alloc(sizeof(AvailExpr)); |
| 3677 | ae->tag = Bct; |
| 3678 | ae->u.Bct.op = e->Iex.Binop.op; |
| 3679 | ae->u.Bct.arg2 = e->Iex.Binop.arg2->Iex.RdTmp.tmp; |
| 3680 | ae->u.Bct.con1 = *(e->Iex.Binop.arg1->Iex.Const.con); |
| 3681 | return ae; |
| 3682 | } |
| 3683 | break; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3684 | |
florian | c1b9e39 | 2012-09-20 02:40:57 +0000 | [diff] [blame] | 3685 | case Iex_Const: |
| 3686 | if (e->Iex.Const.con->tag == Ico_F64i) { |
| 3687 | ae = LibVEX_Alloc(sizeof(AvailExpr)); |
| 3688 | ae->tag = Cf64i; |
| 3689 | ae->u.Cf64i.f64i = e->Iex.Const.con->Ico.F64i; |
| 3690 | return ae; |
| 3691 | } |
| 3692 | break; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3693 | |
florian | 99dd03e | 2013-01-29 03:56:06 +0000 | [diff] [blame] | 3694 | case Iex_ITE: |
| 3695 | if (e->Iex.ITE.cond->tag == Iex_RdTmp) { |
| 3696 | if (e->Iex.ITE.iffalse->tag == Iex_RdTmp) { |
| 3697 | if (e->Iex.ITE.iftrue->tag == Iex_RdTmp) { |
florian | c1b9e39 | 2012-09-20 02:40:57 +0000 | [diff] [blame] | 3698 | ae = LibVEX_Alloc(sizeof(AvailExpr)); |
florian | b05c20c | 2013-01-31 02:04:02 +0000 | [diff] [blame] | 3699 | ae->tag = Ittt; |
| 3700 | ae->u.Ittt.co = e->Iex.ITE.cond->Iex.RdTmp.tmp; |
| 3701 | ae->u.Ittt.e1 = e->Iex.ITE.iftrue->Iex.RdTmp.tmp; |
| 3702 | ae->u.Ittt.e0 = e->Iex.ITE.iffalse->Iex.RdTmp.tmp; |
florian | c1b9e39 | 2012-09-20 02:40:57 +0000 | [diff] [blame] | 3703 | return ae; |
| 3704 | } |
florian | 99dd03e | 2013-01-29 03:56:06 +0000 | [diff] [blame] | 3705 | if (e->Iex.ITE.iftrue->tag == Iex_Const) { |
florian | c1b9e39 | 2012-09-20 02:40:57 +0000 | [diff] [blame] | 3706 | ae = LibVEX_Alloc(sizeof(AvailExpr)); |
florian | b05c20c | 2013-01-31 02:04:02 +0000 | [diff] [blame] | 3707 | ae->tag = Itct; |
| 3708 | ae->u.Itct.co = e->Iex.ITE.cond->Iex.RdTmp.tmp; |
| 3709 | ae->u.Itct.con1 = *(e->Iex.ITE.iftrue->Iex.Const.con); |
| 3710 | ae->u.Itct.e0 = e->Iex.ITE.iffalse->Iex.RdTmp.tmp; |
florian | c1b9e39 | 2012-09-20 02:40:57 +0000 | [diff] [blame] | 3711 | return ae; |
| 3712 | } |
florian | 99dd03e | 2013-01-29 03:56:06 +0000 | [diff] [blame] | 3713 | } else if (e->Iex.ITE.iffalse->tag == Iex_Const) { |
| 3714 | if (e->Iex.ITE.iftrue->tag == Iex_RdTmp) { |
florian | c1b9e39 | 2012-09-20 02:40:57 +0000 | [diff] [blame] | 3715 | ae = LibVEX_Alloc(sizeof(AvailExpr)); |
florian | b05c20c | 2013-01-31 02:04:02 +0000 | [diff] [blame] | 3716 | ae->tag = Ittc; |
| 3717 | ae->u.Ittc.co = e->Iex.ITE.cond->Iex.RdTmp.tmp; |
| 3718 | ae->u.Ittc.e1 = e->Iex.ITE.iftrue->Iex.RdTmp.tmp; |
| 3719 | ae->u.Ittc.con0 = *(e->Iex.ITE.iffalse->Iex.Const.con); |
florian | c1b9e39 | 2012-09-20 02:40:57 +0000 | [diff] [blame] | 3720 | return ae; |
| 3721 | } |
florian | 99dd03e | 2013-01-29 03:56:06 +0000 | [diff] [blame] | 3722 | if (e->Iex.ITE.iftrue->tag == Iex_Const) { |
florian | c1b9e39 | 2012-09-20 02:40:57 +0000 | [diff] [blame] | 3723 | ae = LibVEX_Alloc(sizeof(AvailExpr)); |
florian | b05c20c | 2013-01-31 02:04:02 +0000 | [diff] [blame] | 3724 | ae->tag = Itcc; |
| 3725 | ae->u.Itcc.co = e->Iex.ITE.cond->Iex.RdTmp.tmp; |
| 3726 | ae->u.Itcc.con1 = *(e->Iex.ITE.iftrue->Iex.Const.con); |
| 3727 | ae->u.Itcc.con0 = *(e->Iex.ITE.iffalse->Iex.Const.con); |
florian | c1b9e39 | 2012-09-20 02:40:57 +0000 | [diff] [blame] | 3728 | return ae; |
| 3729 | } |
| 3730 | } |
| 3731 | } |
| 3732 | break; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3733 | |
florian | c1b9e39 | 2012-09-20 02:40:57 +0000 | [diff] [blame] | 3734 | case Iex_GetI: |
| 3735 | if (e->Iex.GetI.ix->tag == Iex_RdTmp) { |
| 3736 | ae = LibVEX_Alloc(sizeof(AvailExpr)); |
| 3737 | ae->tag = GetIt; |
| 3738 | ae->u.GetIt.descr = e->Iex.GetI.descr; |
| 3739 | ae->u.GetIt.ix = e->Iex.GetI.ix->Iex.RdTmp.tmp; |
| 3740 | ae->u.GetIt.bias = e->Iex.GetI.bias; |
| 3741 | return ae; |
| 3742 | } |
| 3743 | break; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3744 | |
florian | c1b9e39 | 2012-09-20 02:40:57 +0000 | [diff] [blame] | 3745 | case Iex_CCall: |
| 3746 | ae = LibVEX_Alloc(sizeof(AvailExpr)); |
| 3747 | ae->tag = CCall; |
| 3748 | /* Ok to share only the cee, since it is immutable. */ |
| 3749 | ae->u.CCall.cee = e->Iex.CCall.cee; |
| 3750 | ae->u.CCall.retty = e->Iex.CCall.retty; |
| 3751 | /* irExprVec_to_TmpOrConsts will assert if the args are |
| 3752 | neither tmps nor constants, but that's ok .. that's all they |
| 3753 | should be. */ |
| 3754 | irExprVec_to_TmpOrConsts( |
| 3755 | &ae->u.CCall.args, &ae->u.CCall.nArgs, |
| 3756 | e->Iex.CCall.args |
| 3757 | ); |
| 3758 | return ae; |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 3759 | |
florian | c1b9e39 | 2012-09-20 02:40:57 +0000 | [diff] [blame] | 3760 | default: |
| 3761 | break; |
sewardj | e857804 | 2012-04-13 11:42:50 +0000 | [diff] [blame] | 3762 | } |
| 3763 | |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3764 | return NULL; |
| 3765 | } |
| 3766 | |
| 3767 | |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 3768 | /* The BB is modified in-place. Returns True if any changes were |
| 3769 | made. */ |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3770 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3771 | static Bool do_cse_BB ( IRSB* bb ) |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3772 | { |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 3773 | Int i, j, paranoia; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3774 | IRTemp t, q; |
| 3775 | IRStmt* st; |
| 3776 | AvailExpr* eprime; |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 3777 | AvailExpr* ae; |
| 3778 | Bool invalidate; |
| 3779 | Bool anyDone = False; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3780 | |
| 3781 | HashHW* tenv = newHHW(); /* :: IRTemp -> IRTemp */ |
| 3782 | HashHW* aenv = newHHW(); /* :: AvailExpr* -> IRTemp */ |
| 3783 | |
| 3784 | vassert(sizeof(IRTemp) <= sizeof(HWord)); |
| 3785 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3786 | if (0) { ppIRSB(bb); vex_printf("\n\n"); } |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3787 | |
| 3788 | /* Iterate forwards over the stmts. |
sewardj | cfe046e | 2013-01-17 14:23:53 +0000 | [diff] [blame] | 3789 | On seeing "t = E", where E is one of the AvailExpr forms: |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3790 | let E' = apply tenv substitution to E |
| 3791 | search aenv for E' |
| 3792 | if a mapping E' -> q is found, |
| 3793 | replace this stmt by "t = q" |
| 3794 | and add binding t -> q to tenv |
| 3795 | else |
| 3796 | add binding E' -> t to aenv |
| 3797 | replace this stmt by "t = E'" |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 3798 | |
| 3799 | Other statements are only interesting to the extent that they |
| 3800 | might invalidate some of the expressions in aenv. So there is |
| 3801 | an invalidate-bindings check for each statement seen. |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3802 | */ |
| 3803 | for (i = 0; i < bb->stmts_used; i++) { |
| 3804 | st = bb->stmts[i]; |
| 3805 | |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 3806 | /* ------ BEGIN invalidate aenv bindings ------ */ |
| 3807 | /* This is critical: remove from aenv any E' -> .. bindings |
| 3808 | which might be invalidated by this statement. The only |
sewardj | c4356f0 | 2007-11-09 21:15:04 +0000 | [diff] [blame] | 3809 | vulnerable kind of bindings are the GetI kind. |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 3810 | Dirty call - dump (paranoia level -> 2) |
| 3811 | Store - dump (ditto) |
| 3812 | Put, PutI - dump unless no-overlap is proven (.. -> 1) |
| 3813 | Uses getAliasingRelation_IC and getAliasingRelation_II |
| 3814 | to do the no-overlap assessments needed for Put/PutI. |
| 3815 | */ |
| 3816 | switch (st->tag) { |
sewardj | e768e92 | 2009-11-26 17:17:37 +0000 | [diff] [blame] | 3817 | case Ist_Dirty: case Ist_Store: case Ist_MBE: |
| 3818 | case Ist_CAS: case Ist_LLSC: |
sewardj | cfe046e | 2013-01-17 14:23:53 +0000 | [diff] [blame] | 3819 | case Ist_StoreG: |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 3820 | paranoia = 2; break; |
| 3821 | case Ist_Put: case Ist_PutI: |
| 3822 | paranoia = 1; break; |
| 3823 | case Ist_NoOp: case Ist_IMark: case Ist_AbiHint: |
sewardj | cfe046e | 2013-01-17 14:23:53 +0000 | [diff] [blame] | 3824 | case Ist_WrTmp: case Ist_Exit: case Ist_LoadG: |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 3825 | paranoia = 0; break; |
| 3826 | default: |
| 3827 | vpanic("do_cse_BB(1)"); |
| 3828 | } |
| 3829 | |
| 3830 | if (paranoia > 0) { |
| 3831 | for (j = 0; j < aenv->used; j++) { |
| 3832 | if (!aenv->inuse[j]) |
| 3833 | continue; |
| 3834 | ae = (AvailExpr*)aenv->key[j]; |
| 3835 | if (ae->tag != GetIt) |
| 3836 | continue; |
| 3837 | invalidate = False; |
| 3838 | if (paranoia >= 2) { |
| 3839 | invalidate = True; |
| 3840 | } else { |
| 3841 | vassert(paranoia == 1); |
| 3842 | if (st->tag == Ist_Put) { |
| 3843 | if (getAliasingRelation_IC( |
| 3844 | ae->u.GetIt.descr, |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3845 | IRExpr_RdTmp(ae->u.GetIt.ix), |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 3846 | st->Ist.Put.offset, |
| 3847 | typeOfIRExpr(bb->tyenv,st->Ist.Put.data) |
| 3848 | ) != NoAlias) |
| 3849 | invalidate = True; |
| 3850 | } |
| 3851 | else |
| 3852 | if (st->tag == Ist_PutI) { |
florian | d6f38b3 | 2012-05-31 15:46:18 +0000 | [diff] [blame] | 3853 | IRPutI *puti = st->Ist.PutI.details; |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 3854 | if (getAliasingRelation_II( |
| 3855 | ae->u.GetIt.descr, |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3856 | IRExpr_RdTmp(ae->u.GetIt.ix), |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 3857 | ae->u.GetIt.bias, |
florian | d6f38b3 | 2012-05-31 15:46:18 +0000 | [diff] [blame] | 3858 | puti->descr, |
| 3859 | puti->ix, |
| 3860 | puti->bias |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 3861 | ) != NoAlias) |
| 3862 | invalidate = True; |
| 3863 | } |
| 3864 | else |
| 3865 | vpanic("do_cse_BB(2)"); |
| 3866 | } |
| 3867 | |
| 3868 | if (invalidate) { |
| 3869 | aenv->inuse[j] = False; |
| 3870 | aenv->key[j] = (HWord)NULL; /* be sure */ |
| 3871 | } |
| 3872 | } /* for j */ |
| 3873 | } /* paranoia > 0 */ |
| 3874 | |
| 3875 | /* ------ ENV invalidate aenv bindings ------ */ |
| 3876 | |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3877 | /* ignore not-interestings */ |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3878 | if (st->tag != Ist_WrTmp) |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3879 | continue; |
| 3880 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3881 | t = st->Ist.WrTmp.tmp; |
| 3882 | eprime = irExpr_to_AvailExpr(st->Ist.WrTmp.data); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3883 | /* ignore if not of AvailExpr form */ |
| 3884 | if (!eprime) |
| 3885 | continue; |
| 3886 | |
| 3887 | /* vex_printf("considering: " ); ppIRStmt(st); vex_printf("\n"); */ |
| 3888 | |
| 3889 | /* apply tenv */ |
| 3890 | subst_AvailExpr( tenv, eprime ); |
| 3891 | |
| 3892 | /* search aenv for eprime, unfortunately the hard way */ |
| 3893 | for (j = 0; j < aenv->used; j++) |
| 3894 | if (aenv->inuse[j] && eq_AvailExpr(eprime, (AvailExpr*)aenv->key[j])) |
| 3895 | break; |
| 3896 | |
| 3897 | if (j < aenv->used) { |
| 3898 | /* A binding E' -> q was found. Replace stmt by "t = q" and |
| 3899 | note the t->q binding in tenv. */ |
| 3900 | /* (this is the core of the CSE action) */ |
| 3901 | q = (IRTemp)aenv->val[j]; |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3902 | bb->stmts[i] = IRStmt_WrTmp( t, IRExpr_RdTmp(q) ); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3903 | addToHHW( tenv, (HWord)t, (HWord)q ); |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 3904 | anyDone = True; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3905 | } else { |
| 3906 | /* No binding was found, so instead we add E' -> t to our |
| 3907 | collection of available expressions, replace this stmt |
| 3908 | with "t = E'", and move on. */ |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3909 | bb->stmts[i] = IRStmt_WrTmp( t, availExpr_to_IRExpr(eprime) ); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3910 | addToHHW( aenv, (HWord)eprime, (HWord)t ); |
| 3911 | } |
| 3912 | } |
| 3913 | |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 3914 | /* |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3915 | ppIRSB(bb); |
| 3916 | sanityCheckIRSB(bb, Ity_I32); |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 3917 | vex_printf("\n\n"); |
| 3918 | */ |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 3919 | return anyDone; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3920 | } |
| 3921 | |
| 3922 | |
| 3923 | /*---------------------------------------------------------------*/ |
| 3924 | /*--- Add32/Sub32 chain collapsing ---*/ |
| 3925 | /*---------------------------------------------------------------*/ |
| 3926 | |
| 3927 | /* ----- Helper functions for Add32/Sub32 chain collapsing ----- */ |
| 3928 | |
| 3929 | /* Is this expression "Add32(tmp,const)" or "Sub32(tmp,const)" ? If |
| 3930 | yes, set *tmp and *i32 appropriately. *i32 is set as if the |
| 3931 | root node is Add32, not Sub32. */ |
| 3932 | |
| 3933 | static Bool isAdd32OrSub32 ( IRExpr* e, IRTemp* tmp, Int* i32 ) |
| 3934 | { |
| 3935 | if (e->tag != Iex_Binop) |
| 3936 | return False; |
| 3937 | if (e->Iex.Binop.op != Iop_Add32 && e->Iex.Binop.op != Iop_Sub32) |
| 3938 | return False; |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3939 | if (e->Iex.Binop.arg1->tag != Iex_RdTmp) |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3940 | return False; |
| 3941 | if (e->Iex.Binop.arg2->tag != Iex_Const) |
| 3942 | return False; |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3943 | *tmp = e->Iex.Binop.arg1->Iex.RdTmp.tmp; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3944 | *i32 = (Int)(e->Iex.Binop.arg2->Iex.Const.con->Ico.U32); |
| 3945 | if (e->Iex.Binop.op == Iop_Sub32) |
| 3946 | *i32 = -*i32; |
| 3947 | return True; |
| 3948 | } |
| 3949 | |
| 3950 | |
| 3951 | /* Figure out if tmp can be expressed as tmp2 +32 const, for some |
| 3952 | other tmp2. Scan backwards from the specified start point -- an |
| 3953 | optimisation. */ |
| 3954 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3955 | static Bool collapseChain ( IRSB* bb, Int startHere, |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3956 | IRTemp tmp, |
| 3957 | IRTemp* tmp2, Int* i32 ) |
| 3958 | { |
| 3959 | Int j, ii; |
| 3960 | IRTemp vv; |
| 3961 | IRStmt* st; |
| 3962 | IRExpr* e; |
| 3963 | |
| 3964 | /* the (var, con) pair contain the current 'representation' for |
| 3965 | 'tmp'. We start with 'tmp + 0'. */ |
| 3966 | IRTemp var = tmp; |
| 3967 | Int con = 0; |
| 3968 | |
| 3969 | /* Scan backwards to see if tmp can be replaced by some other tmp |
| 3970 | +/- a constant. */ |
| 3971 | for (j = startHere; j >= 0; j--) { |
| 3972 | st = bb->stmts[j]; |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3973 | if (st->tag != Ist_WrTmp) |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3974 | continue; |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3975 | if (st->Ist.WrTmp.tmp != var) |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3976 | continue; |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3977 | e = st->Ist.WrTmp.data; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3978 | if (!isAdd32OrSub32(e, &vv, &ii)) |
| 3979 | break; |
| 3980 | var = vv; |
| 3981 | con += ii; |
| 3982 | } |
| 3983 | if (j == -1) |
| 3984 | /* no earlier binding for var .. ill-formed IR */ |
| 3985 | vpanic("collapseChain"); |
| 3986 | |
| 3987 | /* so, did we find anything interesting? */ |
| 3988 | if (var == tmp) |
| 3989 | return False; /* no .. */ |
| 3990 | |
| 3991 | *tmp2 = var; |
| 3992 | *i32 = con; |
| 3993 | return True; |
| 3994 | } |
| 3995 | |
| 3996 | |
| 3997 | /* ------- Main function for Add32/Sub32 chain collapsing ------ */ |
| 3998 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3999 | static void collapse_AddSub_chains_BB ( IRSB* bb ) |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4000 | { |
| 4001 | IRStmt *st; |
| 4002 | IRTemp var, var2; |
| 4003 | Int i, con, con2; |
| 4004 | |
| 4005 | for (i = bb->stmts_used-1; i >= 0; i--) { |
| 4006 | st = bb->stmts[i]; |
sewardj | 8bee6d1 | 2005-03-22 02:24:05 +0000 | [diff] [blame] | 4007 | if (st->tag == Ist_NoOp) |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4008 | continue; |
| 4009 | |
| 4010 | /* Try to collapse 't1 = Add32/Sub32(t2, con)'. */ |
| 4011 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4012 | if (st->tag == Ist_WrTmp |
| 4013 | && isAdd32OrSub32(st->Ist.WrTmp.data, &var, &con)) { |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4014 | |
| 4015 | /* So e1 is of the form Add32(var,con) or Sub32(var,-con). |
| 4016 | Find out if var can be expressed as var2 + con2. */ |
| 4017 | if (collapseChain(bb, i-1, var, &var2, &con2)) { |
| 4018 | if (DEBUG_IROPT) { |
| 4019 | vex_printf("replacing1 "); |
| 4020 | ppIRStmt(st); |
| 4021 | vex_printf(" with "); |
| 4022 | } |
| 4023 | con2 += con; |
| 4024 | bb->stmts[i] |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4025 | = IRStmt_WrTmp( |
| 4026 | st->Ist.WrTmp.tmp, |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4027 | (con2 >= 0) |
| 4028 | ? IRExpr_Binop(Iop_Add32, |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4029 | IRExpr_RdTmp(var2), |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4030 | IRExpr_Const(IRConst_U32(con2))) |
| 4031 | : IRExpr_Binop(Iop_Sub32, |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4032 | IRExpr_RdTmp(var2), |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4033 | IRExpr_Const(IRConst_U32(-con2))) |
| 4034 | ); |
| 4035 | if (DEBUG_IROPT) { |
| 4036 | ppIRStmt(bb->stmts[i]); |
| 4037 | vex_printf("\n"); |
| 4038 | } |
| 4039 | } |
| 4040 | |
| 4041 | continue; |
| 4042 | } |
| 4043 | |
| 4044 | /* Try to collapse 't1 = GetI[t2, con]'. */ |
| 4045 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4046 | if (st->tag == Ist_WrTmp |
| 4047 | && st->Ist.WrTmp.data->tag == Iex_GetI |
| 4048 | && st->Ist.WrTmp.data->Iex.GetI.ix->tag == Iex_RdTmp |
| 4049 | && collapseChain(bb, i-1, st->Ist.WrTmp.data->Iex.GetI.ix |
| 4050 | ->Iex.RdTmp.tmp, &var2, &con2)) { |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4051 | if (DEBUG_IROPT) { |
| 4052 | vex_printf("replacing3 "); |
| 4053 | ppIRStmt(st); |
| 4054 | vex_printf(" with "); |
| 4055 | } |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4056 | con2 += st->Ist.WrTmp.data->Iex.GetI.bias; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4057 | bb->stmts[i] |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4058 | = IRStmt_WrTmp( |
| 4059 | st->Ist.WrTmp.tmp, |
| 4060 | IRExpr_GetI(st->Ist.WrTmp.data->Iex.GetI.descr, |
| 4061 | IRExpr_RdTmp(var2), |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4062 | con2)); |
| 4063 | if (DEBUG_IROPT) { |
| 4064 | ppIRStmt(bb->stmts[i]); |
| 4065 | vex_printf("\n"); |
| 4066 | } |
| 4067 | continue; |
| 4068 | } |
| 4069 | |
| 4070 | /* Perhaps st is PutI[t, con] ? */ |
florian | d6f38b3 | 2012-05-31 15:46:18 +0000 | [diff] [blame] | 4071 | IRPutI *puti = st->Ist.PutI.details; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4072 | if (st->tag == Ist_PutI |
florian | d6f38b3 | 2012-05-31 15:46:18 +0000 | [diff] [blame] | 4073 | && puti->ix->tag == Iex_RdTmp |
| 4074 | && collapseChain(bb, i-1, puti->ix->Iex.RdTmp.tmp, |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4075 | &var2, &con2)) { |
| 4076 | if (DEBUG_IROPT) { |
| 4077 | vex_printf("replacing2 "); |
| 4078 | ppIRStmt(st); |
| 4079 | vex_printf(" with "); |
| 4080 | } |
florian | d6f38b3 | 2012-05-31 15:46:18 +0000 | [diff] [blame] | 4081 | con2 += puti->bias; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4082 | bb->stmts[i] |
florian | d6f38b3 | 2012-05-31 15:46:18 +0000 | [diff] [blame] | 4083 | = IRStmt_PutI(mkIRPutI(puti->descr, |
| 4084 | IRExpr_RdTmp(var2), |
| 4085 | con2, |
| 4086 | puti->data)); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4087 | if (DEBUG_IROPT) { |
| 4088 | ppIRStmt(bb->stmts[i]); |
| 4089 | vex_printf("\n"); |
| 4090 | } |
| 4091 | continue; |
| 4092 | } |
| 4093 | |
| 4094 | } /* for */ |
| 4095 | } |
| 4096 | |
| 4097 | |
| 4098 | /*---------------------------------------------------------------*/ |
| 4099 | /*--- PutI/GetI transformations ---*/ |
| 4100 | /*---------------------------------------------------------------*/ |
| 4101 | |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4102 | /* Given the parts (descr, tmp, bias) for a GetI, scan backwards from |
| 4103 | the given starting point to find, if any, a PutI which writes |
| 4104 | exactly the same piece of guest state, and so return the expression |
| 4105 | that the PutI writes. This is the core of PutI-GetI forwarding. */ |
| 4106 | |
| 4107 | static |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4108 | IRExpr* findPutI ( IRSB* bb, Int startHere, |
| 4109 | IRRegArray* descrG, IRExpr* ixG, Int biasG ) |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4110 | { |
| 4111 | Int j; |
| 4112 | IRStmt* st; |
| 4113 | GSAliasing relation; |
| 4114 | |
| 4115 | if (0) { |
| 4116 | vex_printf("\nfindPutI "); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4117 | ppIRRegArray(descrG); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4118 | vex_printf(" "); |
| 4119 | ppIRExpr(ixG); |
| 4120 | vex_printf(" %d\n", biasG); |
| 4121 | } |
| 4122 | |
| 4123 | /* Scan backwards in bb from startHere to find a suitable PutI |
| 4124 | binding for (descrG, ixG, biasG), if any. */ |
| 4125 | |
| 4126 | for (j = startHere; j >= 0; j--) { |
| 4127 | st = bb->stmts[j]; |
sewardj | 8bee6d1 | 2005-03-22 02:24:05 +0000 | [diff] [blame] | 4128 | if (st->tag == Ist_NoOp) |
| 4129 | continue; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4130 | |
| 4131 | if (st->tag == Ist_Put) { |
| 4132 | /* Non-indexed Put. This can't give a binding, but we do |
| 4133 | need to check it doesn't invalidate the search by |
| 4134 | overlapping any part of the indexed guest state. */ |
| 4135 | |
| 4136 | relation |
| 4137 | = getAliasingRelation_IC( |
| 4138 | descrG, ixG, |
| 4139 | st->Ist.Put.offset, |
| 4140 | typeOfIRExpr(bb->tyenv,st->Ist.Put.data) ); |
| 4141 | |
| 4142 | if (relation == NoAlias) { |
| 4143 | /* we're OK; keep going */ |
| 4144 | continue; |
| 4145 | } else { |
| 4146 | /* relation == UnknownAlias || relation == ExactAlias */ |
| 4147 | /* If this assertion fails, we've found a Put which writes |
| 4148 | an area of guest state which is read by a GetI. Which |
| 4149 | is unlikely (although not per se wrong). */ |
| 4150 | vassert(relation != ExactAlias); |
| 4151 | /* This Put potentially writes guest state that the GetI |
| 4152 | reads; we must fail. */ |
| 4153 | return NULL; |
| 4154 | } |
| 4155 | } |
| 4156 | |
| 4157 | if (st->tag == Ist_PutI) { |
florian | d6f38b3 | 2012-05-31 15:46:18 +0000 | [diff] [blame] | 4158 | IRPutI *puti = st->Ist.PutI.details; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4159 | |
| 4160 | relation = getAliasingRelation_II( |
| 4161 | descrG, ixG, biasG, |
florian | d6f38b3 | 2012-05-31 15:46:18 +0000 | [diff] [blame] | 4162 | puti->descr, |
| 4163 | puti->ix, |
| 4164 | puti->bias |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4165 | ); |
| 4166 | |
| 4167 | if (relation == NoAlias) { |
| 4168 | /* This PutI definitely doesn't overlap. Ignore it and |
| 4169 | keep going. */ |
| 4170 | continue; /* the for j loop */ |
| 4171 | } |
| 4172 | |
| 4173 | if (relation == UnknownAlias) { |
| 4174 | /* We don't know if this PutI writes to the same guest |
| 4175 | state that the GetI, or not. So we have to give up. */ |
| 4176 | return NULL; |
| 4177 | } |
| 4178 | |
| 4179 | /* Otherwise, we've found what we're looking for. */ |
| 4180 | vassert(relation == ExactAlias); |
florian | d6f38b3 | 2012-05-31 15:46:18 +0000 | [diff] [blame] | 4181 | return puti->data; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4182 | |
| 4183 | } /* if (st->tag == Ist_PutI) */ |
| 4184 | |
| 4185 | if (st->tag == Ist_Dirty) { |
| 4186 | /* Be conservative. If the dirty call has any guest effects at |
| 4187 | all, give up. We could do better -- only give up if there |
| 4188 | are any guest writes/modifies. */ |
| 4189 | if (st->Ist.Dirty.details->nFxState > 0) |
| 4190 | return NULL; |
| 4191 | } |
| 4192 | |
| 4193 | } /* for */ |
| 4194 | |
| 4195 | /* No valid replacement was found. */ |
| 4196 | return NULL; |
| 4197 | } |
| 4198 | |
| 4199 | |
| 4200 | |
| 4201 | /* Assuming pi is a PutI stmt, is s2 identical to it (in the sense |
| 4202 | that it writes exactly the same piece of guest state) ? Safe |
| 4203 | answer: False. */ |
| 4204 | |
| 4205 | static Bool identicalPutIs ( IRStmt* pi, IRStmt* s2 ) |
| 4206 | { |
| 4207 | vassert(pi->tag == Ist_PutI); |
| 4208 | if (s2->tag != Ist_PutI) |
| 4209 | return False; |
| 4210 | |
florian | d6f38b3 | 2012-05-31 15:46:18 +0000 | [diff] [blame] | 4211 | IRPutI *p1 = pi->Ist.PutI.details; |
| 4212 | IRPutI *p2 = s2->Ist.PutI.details; |
| 4213 | |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 4214 | return toBool( |
| 4215 | getAliasingRelation_II( |
florian | d6f38b3 | 2012-05-31 15:46:18 +0000 | [diff] [blame] | 4216 | p1->descr, p1->ix, p1->bias, |
| 4217 | p2->descr, p2->ix, p2->bias |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4218 | ) |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 4219 | == ExactAlias |
| 4220 | ); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4221 | } |
| 4222 | |
| 4223 | |
| 4224 | /* Assuming pi is a PutI stmt, is s2 a Get/GetI/Put/PutI which might |
| 4225 | overlap it? Safe answer: True. Note, we could do a lot better |
| 4226 | than this if needed. */ |
| 4227 | |
| 4228 | static |
| 4229 | Bool guestAccessWhichMightOverlapPutI ( |
| 4230 | IRTypeEnv* tyenv, IRStmt* pi, IRStmt* s2 |
| 4231 | ) |
| 4232 | { |
| 4233 | GSAliasing relation; |
| 4234 | UInt minoffP, maxoffP; |
| 4235 | |
| 4236 | vassert(pi->tag == Ist_PutI); |
florian | d6f38b3 | 2012-05-31 15:46:18 +0000 | [diff] [blame] | 4237 | |
| 4238 | IRPutI *p1 = pi->Ist.PutI.details; |
| 4239 | |
| 4240 | getArrayBounds(p1->descr, &minoffP, &maxoffP); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4241 | switch (s2->tag) { |
| 4242 | |
sewardj | d2445f6 | 2005-03-21 00:15:53 +0000 | [diff] [blame] | 4243 | case Ist_NoOp: |
sewardj | f168931 | 2005-03-16 18:19:10 +0000 | [diff] [blame] | 4244 | case Ist_IMark: |
| 4245 | return False; |
| 4246 | |
sewardj | c4356f0 | 2007-11-09 21:15:04 +0000 | [diff] [blame] | 4247 | case Ist_MBE: |
sewardj | 5a9ffab | 2005-05-12 17:55:01 +0000 | [diff] [blame] | 4248 | case Ist_AbiHint: |
| 4249 | /* just be paranoid ... these should be rare. */ |
sewardj | bb3f52d | 2005-01-07 14:14:50 +0000 | [diff] [blame] | 4250 | return True; |
| 4251 | |
sewardj | e9d8a26 | 2009-07-01 08:06:34 +0000 | [diff] [blame] | 4252 | case Ist_CAS: |
| 4253 | /* This is unbelievably lame, but it's probably not |
| 4254 | significant from a performance point of view. Really, a |
| 4255 | CAS is a load-store op, so it should be safe to say False. |
| 4256 | However .. */ |
| 4257 | return True; |
| 4258 | |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4259 | case Ist_Dirty: |
| 4260 | /* If the dirty call has any guest effects at all, give up. |
| 4261 | Probably could do better. */ |
| 4262 | if (s2->Ist.Dirty.details->nFxState > 0) |
| 4263 | return True; |
| 4264 | return False; |
| 4265 | |
| 4266 | case Ist_Put: |
sewardj | 496a58d | 2005-03-20 18:44:44 +0000 | [diff] [blame] | 4267 | vassert(isIRAtom(s2->Ist.Put.data)); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4268 | relation |
| 4269 | = getAliasingRelation_IC( |
florian | d6f38b3 | 2012-05-31 15:46:18 +0000 | [diff] [blame] | 4270 | p1->descr, p1->ix, |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4271 | s2->Ist.Put.offset, |
| 4272 | typeOfIRExpr(tyenv,s2->Ist.Put.data) |
| 4273 | ); |
| 4274 | goto have_relation; |
| 4275 | |
florian | d6f38b3 | 2012-05-31 15:46:18 +0000 | [diff] [blame] | 4276 | case Ist_PutI: { |
| 4277 | IRPutI *p2 = s2->Ist.PutI.details; |
| 4278 | |
| 4279 | vassert(isIRAtom(p2->ix)); |
| 4280 | vassert(isIRAtom(p2->data)); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4281 | relation |
| 4282 | = getAliasingRelation_II( |
florian | d6f38b3 | 2012-05-31 15:46:18 +0000 | [diff] [blame] | 4283 | p1->descr, p1->ix, p1->bias, |
| 4284 | p2->descr, p2->ix, p2->bias |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4285 | ); |
| 4286 | goto have_relation; |
florian | d6f38b3 | 2012-05-31 15:46:18 +0000 | [diff] [blame] | 4287 | } |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4288 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4289 | case Ist_WrTmp: |
| 4290 | if (s2->Ist.WrTmp.data->tag == Iex_GetI) { |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4291 | relation |
| 4292 | = getAliasingRelation_II( |
florian | d6f38b3 | 2012-05-31 15:46:18 +0000 | [diff] [blame] | 4293 | p1->descr, p1->ix, p1->bias, |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4294 | s2->Ist.WrTmp.data->Iex.GetI.descr, |
| 4295 | s2->Ist.WrTmp.data->Iex.GetI.ix, |
| 4296 | s2->Ist.WrTmp.data->Iex.GetI.bias |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4297 | ); |
| 4298 | goto have_relation; |
| 4299 | } |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4300 | if (s2->Ist.WrTmp.data->tag == Iex_Get) { |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4301 | relation |
| 4302 | = getAliasingRelation_IC( |
florian | d6f38b3 | 2012-05-31 15:46:18 +0000 | [diff] [blame] | 4303 | p1->descr, p1->ix, |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4304 | s2->Ist.WrTmp.data->Iex.Get.offset, |
| 4305 | s2->Ist.WrTmp.data->Iex.Get.ty |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4306 | ); |
| 4307 | goto have_relation; |
| 4308 | } |
| 4309 | return False; |
| 4310 | |
sewardj | af1ceca | 2005-06-30 23:31:27 +0000 | [diff] [blame] | 4311 | case Ist_Store: |
| 4312 | vassert(isIRAtom(s2->Ist.Store.addr)); |
| 4313 | vassert(isIRAtom(s2->Ist.Store.data)); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4314 | return False; |
| 4315 | |
| 4316 | default: |
| 4317 | vex_printf("\n"); ppIRStmt(s2); vex_printf("\n"); |
| 4318 | vpanic("guestAccessWhichMightOverlapPutI"); |
| 4319 | } |
| 4320 | |
| 4321 | have_relation: |
| 4322 | if (relation == NoAlias) |
| 4323 | return False; |
| 4324 | else |
| 4325 | return True; /* ExactAlias or UnknownAlias */ |
| 4326 | } |
| 4327 | |
| 4328 | |
| 4329 | |
| 4330 | /* ---------- PutI/GetI transformations main functions --------- */ |
| 4331 | |
| 4332 | /* Remove redundant GetIs, to the extent that they can be detected. |
| 4333 | bb is modified in-place. */ |
| 4334 | |
| 4335 | static |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4336 | void do_redundant_GetI_elimination ( IRSB* bb ) |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4337 | { |
| 4338 | Int i; |
| 4339 | IRStmt* st; |
| 4340 | |
| 4341 | for (i = bb->stmts_used-1; i >= 0; i--) { |
| 4342 | st = bb->stmts[i]; |
sewardj | 8bee6d1 | 2005-03-22 02:24:05 +0000 | [diff] [blame] | 4343 | if (st->tag == Ist_NoOp) |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4344 | continue; |
| 4345 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4346 | if (st->tag == Ist_WrTmp |
| 4347 | && st->Ist.WrTmp.data->tag == Iex_GetI |
| 4348 | && st->Ist.WrTmp.data->Iex.GetI.ix->tag == Iex_RdTmp) { |
| 4349 | IRRegArray* descr = st->Ist.WrTmp.data->Iex.GetI.descr; |
| 4350 | IRExpr* ix = st->Ist.WrTmp.data->Iex.GetI.ix; |
| 4351 | Int bias = st->Ist.WrTmp.data->Iex.GetI.bias; |
| 4352 | IRExpr* replacement = findPutI(bb, i-1, descr, ix, bias); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4353 | if (replacement |
sewardj | 496a58d | 2005-03-20 18:44:44 +0000 | [diff] [blame] | 4354 | && isIRAtom(replacement) |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4355 | /* Make sure we're doing a type-safe transformation! */ |
| 4356 | && typeOfIRExpr(bb->tyenv, replacement) == descr->elemTy) { |
| 4357 | if (DEBUG_IROPT) { |
| 4358 | vex_printf("rGI: "); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4359 | ppIRExpr(st->Ist.WrTmp.data); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4360 | vex_printf(" -> "); |
| 4361 | ppIRExpr(replacement); |
| 4362 | vex_printf("\n"); |
| 4363 | } |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4364 | bb->stmts[i] = IRStmt_WrTmp(st->Ist.WrTmp.tmp, replacement); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4365 | } |
| 4366 | } |
| 4367 | } |
| 4368 | |
| 4369 | } |
| 4370 | |
| 4371 | |
| 4372 | /* Remove redundant PutIs, to the extent which they can be detected. |
| 4373 | bb is modified in-place. */ |
| 4374 | |
| 4375 | static |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4376 | void do_redundant_PutI_elimination ( IRSB* bb ) |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4377 | { |
| 4378 | Int i, j; |
| 4379 | Bool delete; |
| 4380 | IRStmt *st, *stj; |
| 4381 | |
philippe | 6c46bef | 2012-08-14 22:29:01 +0000 | [diff] [blame] | 4382 | vassert(vex_control.iropt_register_updates < VexRegUpdAllregsAtEachInsn); |
philippe | c8e2f98 | 2012-08-01 22:04:13 +0000 | [diff] [blame] | 4383 | |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4384 | for (i = 0; i < bb->stmts_used; i++) { |
| 4385 | st = bb->stmts[i]; |
sewardj | 8bee6d1 | 2005-03-22 02:24:05 +0000 | [diff] [blame] | 4386 | if (st->tag != Ist_PutI) |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4387 | continue; |
| 4388 | /* Ok, search forwards from here to see if we can find another |
| 4389 | PutI which makes this one redundant, and dodging various |
| 4390 | hazards. Search forwards: |
| 4391 | * If conditional exit, give up (because anything after that |
| 4392 | does not postdominate this put). |
| 4393 | * If a Get which might overlap, give up (because this PutI |
| 4394 | not necessarily dead). |
| 4395 | * If a Put which is identical, stop with success. |
| 4396 | * If a Put which might overlap, but is not identical, give up. |
| 4397 | * If a dirty helper call which might write guest state, give up. |
| 4398 | * If a Put which definitely doesn't overlap, or any other |
| 4399 | kind of stmt, continue. |
| 4400 | */ |
| 4401 | delete = False; |
| 4402 | for (j = i+1; j < bb->stmts_used; j++) { |
| 4403 | stj = bb->stmts[j]; |
sewardj | 8bee6d1 | 2005-03-22 02:24:05 +0000 | [diff] [blame] | 4404 | if (stj->tag == Ist_NoOp) |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4405 | continue; |
| 4406 | if (identicalPutIs(st, stj)) { |
| 4407 | /* success! */ |
| 4408 | delete = True; |
| 4409 | break; |
| 4410 | } |
| 4411 | if (stj->tag == Ist_Exit) |
| 4412 | /* give up */ |
| 4413 | break; |
| 4414 | if (st->tag == Ist_Dirty) |
| 4415 | /* give up; could do better here */ |
| 4416 | break; |
| 4417 | if (guestAccessWhichMightOverlapPutI(bb->tyenv, st, stj)) |
| 4418 | /* give up */ |
| 4419 | break; |
| 4420 | } |
| 4421 | |
| 4422 | if (delete) { |
| 4423 | if (DEBUG_IROPT) { |
| 4424 | vex_printf("rPI: "); |
| 4425 | ppIRStmt(st); |
| 4426 | vex_printf("\n"); |
| 4427 | } |
sewardj | d2445f6 | 2005-03-21 00:15:53 +0000 | [diff] [blame] | 4428 | bb->stmts[i] = IRStmt_NoOp(); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4429 | } |
| 4430 | |
| 4431 | } |
| 4432 | } |
| 4433 | |
| 4434 | |
| 4435 | /*---------------------------------------------------------------*/ |
| 4436 | /*--- Loop unrolling ---*/ |
| 4437 | /*---------------------------------------------------------------*/ |
| 4438 | |
| 4439 | /* Adjust all tmp values (names) in e by delta. e is destructively |
| 4440 | modified. */ |
| 4441 | |
| 4442 | static void deltaIRExpr ( IRExpr* e, Int delta ) |
| 4443 | { |
| 4444 | Int i; |
| 4445 | switch (e->tag) { |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4446 | case Iex_RdTmp: |
| 4447 | e->Iex.RdTmp.tmp += delta; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4448 | break; |
| 4449 | case Iex_Get: |
| 4450 | case Iex_Const: |
| 4451 | break; |
| 4452 | case Iex_GetI: |
| 4453 | deltaIRExpr(e->Iex.GetI.ix, delta); |
| 4454 | break; |
sewardj | 1a866b4 | 2006-02-09 02:54:03 +0000 | [diff] [blame] | 4455 | case Iex_Qop: |
florian | 96d7cc3 | 2012-06-01 20:41:24 +0000 | [diff] [blame] | 4456 | deltaIRExpr(e->Iex.Qop.details->arg1, delta); |
| 4457 | deltaIRExpr(e->Iex.Qop.details->arg2, delta); |
| 4458 | deltaIRExpr(e->Iex.Qop.details->arg3, delta); |
| 4459 | deltaIRExpr(e->Iex.Qop.details->arg4, delta); |
sewardj | 1a866b4 | 2006-02-09 02:54:03 +0000 | [diff] [blame] | 4460 | break; |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 4461 | case Iex_Triop: |
florian | 420bfa9 | 2012-06-02 20:29:22 +0000 | [diff] [blame] | 4462 | deltaIRExpr(e->Iex.Triop.details->arg1, delta); |
| 4463 | deltaIRExpr(e->Iex.Triop.details->arg2, delta); |
| 4464 | deltaIRExpr(e->Iex.Triop.details->arg3, delta); |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 4465 | break; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4466 | case Iex_Binop: |
| 4467 | deltaIRExpr(e->Iex.Binop.arg1, delta); |
| 4468 | deltaIRExpr(e->Iex.Binop.arg2, delta); |
| 4469 | break; |
| 4470 | case Iex_Unop: |
| 4471 | deltaIRExpr(e->Iex.Unop.arg, delta); |
| 4472 | break; |
sewardj | af1ceca | 2005-06-30 23:31:27 +0000 | [diff] [blame] | 4473 | case Iex_Load: |
| 4474 | deltaIRExpr(e->Iex.Load.addr, delta); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4475 | break; |
| 4476 | case Iex_CCall: |
| 4477 | for (i = 0; e->Iex.CCall.args[i]; i++) |
| 4478 | deltaIRExpr(e->Iex.CCall.args[i], delta); |
| 4479 | break; |
florian | 99dd03e | 2013-01-29 03:56:06 +0000 | [diff] [blame] | 4480 | case Iex_ITE: |
| 4481 | deltaIRExpr(e->Iex.ITE.cond, delta); |
| 4482 | deltaIRExpr(e->Iex.ITE.iftrue, delta); |
| 4483 | deltaIRExpr(e->Iex.ITE.iffalse, delta); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4484 | break; |
| 4485 | default: |
| 4486 | vex_printf("\n"); ppIRExpr(e); vex_printf("\n"); |
| 4487 | vpanic("deltaIRExpr"); |
| 4488 | } |
| 4489 | } |
| 4490 | |
| 4491 | /* Adjust all tmp values (names) in st by delta. st is destructively |
| 4492 | modified. */ |
| 4493 | |
| 4494 | static void deltaIRStmt ( IRStmt* st, Int delta ) |
| 4495 | { |
| 4496 | Int i; |
| 4497 | IRDirty* d; |
| 4498 | switch (st->tag) { |
sewardj | d2445f6 | 2005-03-21 00:15:53 +0000 | [diff] [blame] | 4499 | case Ist_NoOp: |
sewardj | f168931 | 2005-03-16 18:19:10 +0000 | [diff] [blame] | 4500 | case Ist_IMark: |
sewardj | c4356f0 | 2007-11-09 21:15:04 +0000 | [diff] [blame] | 4501 | case Ist_MBE: |
sewardj | f168931 | 2005-03-16 18:19:10 +0000 | [diff] [blame] | 4502 | break; |
sewardj | 5a9ffab | 2005-05-12 17:55:01 +0000 | [diff] [blame] | 4503 | case Ist_AbiHint: |
| 4504 | deltaIRExpr(st->Ist.AbiHint.base, delta); |
sewardj | 478646f | 2008-05-01 20:13:04 +0000 | [diff] [blame] | 4505 | deltaIRExpr(st->Ist.AbiHint.nia, delta); |
sewardj | 5a9ffab | 2005-05-12 17:55:01 +0000 | [diff] [blame] | 4506 | break; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4507 | case Ist_Put: |
| 4508 | deltaIRExpr(st->Ist.Put.data, delta); |
| 4509 | break; |
| 4510 | case Ist_PutI: |
florian | d6f38b3 | 2012-05-31 15:46:18 +0000 | [diff] [blame] | 4511 | deltaIRExpr(st->Ist.PutI.details->ix, delta); |
| 4512 | deltaIRExpr(st->Ist.PutI.details->data, delta); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4513 | break; |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4514 | case Ist_WrTmp: |
| 4515 | st->Ist.WrTmp.tmp += delta; |
| 4516 | deltaIRExpr(st->Ist.WrTmp.data, delta); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4517 | break; |
| 4518 | case Ist_Exit: |
| 4519 | deltaIRExpr(st->Ist.Exit.guard, delta); |
| 4520 | break; |
sewardj | af1ceca | 2005-06-30 23:31:27 +0000 | [diff] [blame] | 4521 | case Ist_Store: |
| 4522 | deltaIRExpr(st->Ist.Store.addr, delta); |
| 4523 | deltaIRExpr(st->Ist.Store.data, delta); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4524 | break; |
sewardj | cfe046e | 2013-01-17 14:23:53 +0000 | [diff] [blame] | 4525 | case Ist_StoreG: { |
| 4526 | IRStoreG* sg = st->Ist.StoreG.details; |
| 4527 | deltaIRExpr(sg->addr, delta); |
| 4528 | deltaIRExpr(sg->data, delta); |
| 4529 | deltaIRExpr(sg->guard, delta); |
| 4530 | break; |
| 4531 | } |
| 4532 | case Ist_LoadG: { |
| 4533 | IRLoadG* lg = st->Ist.LoadG.details; |
| 4534 | lg->dst += delta; |
| 4535 | deltaIRExpr(lg->addr, delta); |
| 4536 | deltaIRExpr(lg->alt, delta); |
| 4537 | deltaIRExpr(lg->guard, delta); |
| 4538 | break; |
| 4539 | } |
sewardj | e9d8a26 | 2009-07-01 08:06:34 +0000 | [diff] [blame] | 4540 | case Ist_CAS: |
| 4541 | if (st->Ist.CAS.details->oldHi != IRTemp_INVALID) |
| 4542 | st->Ist.CAS.details->oldHi += delta; |
| 4543 | st->Ist.CAS.details->oldLo += delta; |
| 4544 | deltaIRExpr(st->Ist.CAS.details->addr, delta); |
| 4545 | if (st->Ist.CAS.details->expdHi) |
| 4546 | deltaIRExpr(st->Ist.CAS.details->expdHi, delta); |
| 4547 | deltaIRExpr(st->Ist.CAS.details->expdLo, delta); |
| 4548 | if (st->Ist.CAS.details->dataHi) |
| 4549 | deltaIRExpr(st->Ist.CAS.details->dataHi, delta); |
| 4550 | deltaIRExpr(st->Ist.CAS.details->dataLo, delta); |
| 4551 | break; |
sewardj | e768e92 | 2009-11-26 17:17:37 +0000 | [diff] [blame] | 4552 | case Ist_LLSC: |
| 4553 | st->Ist.LLSC.result += delta; |
| 4554 | deltaIRExpr(st->Ist.LLSC.addr, delta); |
| 4555 | if (st->Ist.LLSC.storedata) |
| 4556 | deltaIRExpr(st->Ist.LLSC.storedata, delta); |
| 4557 | break; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4558 | case Ist_Dirty: |
| 4559 | d = st->Ist.Dirty.details; |
| 4560 | deltaIRExpr(d->guard, delta); |
florian | 2accb5e | 2013-09-03 21:48:02 +0000 | [diff] [blame] | 4561 | for (i = 0; d->args[i]; i++) { |
| 4562 | IRExpr* arg = d->args[i]; |
| 4563 | if (LIKELY(!is_IRExpr_VECRET_or_BBPTR(arg))) |
| 4564 | deltaIRExpr(arg, delta); |
| 4565 | } |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4566 | if (d->tmp != IRTemp_INVALID) |
| 4567 | d->tmp += delta; |
| 4568 | if (d->mAddr) |
| 4569 | deltaIRExpr(d->mAddr, delta); |
| 4570 | break; |
| 4571 | default: |
| 4572 | vex_printf("\n"); ppIRStmt(st); vex_printf("\n"); |
| 4573 | vpanic("deltaIRStmt"); |
| 4574 | } |
| 4575 | } |
| 4576 | |
| 4577 | |
| 4578 | /* If possible, return a loop-unrolled version of bb0. The original |
| 4579 | is changed. If not possible, return NULL. */ |
| 4580 | |
| 4581 | /* The two schemas considered are: |
| 4582 | |
| 4583 | X: BODY; goto X |
| 4584 | |
| 4585 | which unrolls to (eg) X: BODY;BODY; goto X |
| 4586 | |
| 4587 | and |
| 4588 | |
| 4589 | X: BODY; if (c) goto X; goto Y |
| 4590 | which trivially transforms to |
| 4591 | X: BODY; if (!c) goto Y; goto X; |
| 4592 | so it falls in the scope of the first case. |
| 4593 | |
| 4594 | X and Y must be literal (guest) addresses. |
| 4595 | */ |
| 4596 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4597 | static Int calc_unroll_factor( IRSB* bb ) |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4598 | { |
| 4599 | Int n_stmts, i; |
| 4600 | |
| 4601 | n_stmts = 0; |
sewardj | 0ddbf79 | 2005-04-04 23:12:54 +0000 | [diff] [blame] | 4602 | for (i = 0; i < bb->stmts_used; i++) { |
| 4603 | if (bb->stmts[i]->tag != Ist_NoOp) |
| 4604 | n_stmts++; |
| 4605 | } |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4606 | |
| 4607 | if (n_stmts <= vex_control.iropt_unroll_thresh/8) { |
| 4608 | if (vex_control.iropt_verbosity > 0) |
| 4609 | vex_printf("vex iropt: 8 x unrolling (%d sts -> %d sts)\n", |
| 4610 | n_stmts, 8* n_stmts); |
| 4611 | return 8; |
| 4612 | } |
| 4613 | if (n_stmts <= vex_control.iropt_unroll_thresh/4) { |
| 4614 | if (vex_control.iropt_verbosity > 0) |
| 4615 | vex_printf("vex iropt: 4 x unrolling (%d sts -> %d sts)\n", |
| 4616 | n_stmts, 4* n_stmts); |
| 4617 | return 4; |
| 4618 | } |
| 4619 | |
| 4620 | if (n_stmts <= vex_control.iropt_unroll_thresh/2) { |
| 4621 | if (vex_control.iropt_verbosity > 0) |
| 4622 | vex_printf("vex iropt: 2 x unrolling (%d sts -> %d sts)\n", |
| 4623 | n_stmts, 2* n_stmts); |
| 4624 | return 2; |
| 4625 | } |
| 4626 | |
| 4627 | if (vex_control.iropt_verbosity > 0) |
| 4628 | vex_printf("vex iropt: not unrolling (%d sts)\n", n_stmts); |
| 4629 | |
| 4630 | return 1; |
| 4631 | } |
| 4632 | |
| 4633 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4634 | static IRSB* maybe_loop_unroll_BB ( IRSB* bb0, Addr64 my_addr ) |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4635 | { |
| 4636 | Int i, j, jmax, n_vars; |
| 4637 | Bool xxx_known; |
| 4638 | Addr64 xxx_value, yyy_value; |
| 4639 | IRExpr* udst; |
| 4640 | IRStmt* st; |
| 4641 | IRConst* con; |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4642 | IRSB *bb1, *bb2; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4643 | Int unroll_factor; |
| 4644 | |
| 4645 | if (vex_control.iropt_unroll_thresh <= 0) |
| 4646 | return NULL; |
| 4647 | |
| 4648 | /* First off, figure out if we can unroll this loop. Do this |
| 4649 | without modifying bb0. */ |
| 4650 | |
| 4651 | if (bb0->jumpkind != Ijk_Boring) |
| 4652 | return NULL; |
| 4653 | |
| 4654 | xxx_known = False; |
| 4655 | xxx_value = 0; |
| 4656 | |
| 4657 | /* Extract the next-guest address. If it isn't a literal, we |
| 4658 | have to give up. */ |
| 4659 | |
| 4660 | udst = bb0->next; |
| 4661 | if (udst->tag == Iex_Const |
| 4662 | && (udst->Iex.Const.con->tag == Ico_U32 |
| 4663 | || udst->Iex.Const.con->tag == Ico_U64)) { |
| 4664 | /* The BB ends in a jump to a literal location. */ |
| 4665 | xxx_known = True; |
| 4666 | xxx_value = udst->Iex.Const.con->tag == Ico_U64 |
| 4667 | ? udst->Iex.Const.con->Ico.U64 |
| 4668 | : (Addr64)(udst->Iex.Const.con->Ico.U32); |
| 4669 | } |
| 4670 | |
| 4671 | if (!xxx_known) |
| 4672 | return NULL; |
| 4673 | |
| 4674 | /* Now we know the BB ends to a jump to a literal location. If |
| 4675 | it's a jump to itself (viz, idiom #1), move directly to the |
| 4676 | unrolling stage, first cloning the bb so the original isn't |
| 4677 | modified. */ |
| 4678 | if (xxx_value == my_addr) { |
| 4679 | unroll_factor = calc_unroll_factor( bb0 ); |
| 4680 | if (unroll_factor < 2) |
| 4681 | return NULL; |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4682 | bb1 = deepCopyIRSB( bb0 ); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4683 | bb0 = NULL; |
| 4684 | udst = NULL; /* is now invalid */ |
| 4685 | goto do_unroll; |
| 4686 | } |
| 4687 | |
| 4688 | /* Search for the second idiomatic form: |
| 4689 | X: BODY; if (c) goto X; goto Y |
| 4690 | We know Y, but need to establish that the last stmt |
| 4691 | is 'if (c) goto X'. |
| 4692 | */ |
| 4693 | yyy_value = xxx_value; |
| 4694 | for (i = bb0->stmts_used-1; i >= 0; i--) |
| 4695 | if (bb0->stmts[i]) |
| 4696 | break; |
| 4697 | |
| 4698 | if (i < 0) |
| 4699 | return NULL; /* block with no stmts. Strange. */ |
| 4700 | |
| 4701 | st = bb0->stmts[i]; |
| 4702 | if (st->tag != Ist_Exit) |
| 4703 | return NULL; |
| 4704 | if (st->Ist.Exit.jk != Ijk_Boring) |
| 4705 | return NULL; |
| 4706 | |
| 4707 | con = st->Ist.Exit.dst; |
| 4708 | vassert(con->tag == Ico_U32 || con->tag == Ico_U64); |
| 4709 | |
| 4710 | xxx_value = con->tag == Ico_U64 |
| 4711 | ? st->Ist.Exit.dst->Ico.U64 |
| 4712 | : (Addr64)(st->Ist.Exit.dst->Ico.U32); |
| 4713 | |
| 4714 | /* If this assertion fails, we have some kind of type error. */ |
| 4715 | vassert(con->tag == udst->Iex.Const.con->tag); |
| 4716 | |
| 4717 | if (xxx_value != my_addr) |
| 4718 | /* We didn't find either idiom. Give up. */ |
| 4719 | return NULL; |
| 4720 | |
| 4721 | /* Ok, we found idiom #2. Copy the BB, switch around the xxx and |
| 4722 | yyy values (which makes it look like idiom #1), and go into |
| 4723 | unrolling proper. This means finding (again) the last stmt, in |
| 4724 | the copied BB. */ |
| 4725 | |
| 4726 | unroll_factor = calc_unroll_factor( bb0 ); |
| 4727 | if (unroll_factor < 2) |
| 4728 | return NULL; |
| 4729 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4730 | bb1 = deepCopyIRSB( bb0 ); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4731 | bb0 = NULL; |
| 4732 | udst = NULL; /* is now invalid */ |
| 4733 | for (i = bb1->stmts_used-1; i >= 0; i--) |
| 4734 | if (bb1->stmts[i]) |
| 4735 | break; |
| 4736 | |
| 4737 | /* The next bunch of assertions should be true since we already |
| 4738 | found and checked the last stmt in the original bb. */ |
| 4739 | |
| 4740 | vassert(i >= 0); |
| 4741 | |
| 4742 | st = bb1->stmts[i]; |
| 4743 | vassert(st->tag == Ist_Exit); |
| 4744 | |
| 4745 | con = st->Ist.Exit.dst; |
| 4746 | vassert(con->tag == Ico_U32 || con->tag == Ico_U64); |
| 4747 | |
| 4748 | udst = bb1->next; |
| 4749 | vassert(udst->tag == Iex_Const); |
| 4750 | vassert(udst->Iex.Const.con->tag == Ico_U32 |
| 4751 | || udst->Iex.Const.con->tag == Ico_U64); |
| 4752 | vassert(con->tag == udst->Iex.Const.con->tag); |
| 4753 | |
| 4754 | /* switch the xxx and yyy fields around */ |
| 4755 | if (con->tag == Ico_U64) { |
| 4756 | udst->Iex.Const.con->Ico.U64 = xxx_value; |
| 4757 | con->Ico.U64 = yyy_value; |
| 4758 | } else { |
| 4759 | udst->Iex.Const.con->Ico.U32 = (UInt)xxx_value; |
cerion | 57b4c6d | 2005-02-22 11:07:35 +0000 | [diff] [blame] | 4760 | con->Ico.U32 = (UInt)yyy_value; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4761 | } |
| 4762 | |
| 4763 | /* negate the test condition */ |
| 4764 | st->Ist.Exit.guard |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4765 | = IRExpr_Unop(Iop_Not1,deepCopyIRExpr(st->Ist.Exit.guard)); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4766 | |
| 4767 | /* --- The unroller proper. Both idioms are by now --- */ |
| 4768 | /* --- now converted to idiom 1. --- */ |
| 4769 | |
| 4770 | do_unroll: |
| 4771 | |
| 4772 | vassert(unroll_factor == 2 |
| 4773 | || unroll_factor == 4 |
| 4774 | || unroll_factor == 8); |
| 4775 | |
| 4776 | jmax = unroll_factor==8 ? 3 : (unroll_factor==4 ? 2 : 1); |
| 4777 | for (j = 1; j <= jmax; j++) { |
| 4778 | |
| 4779 | n_vars = bb1->tyenv->types_used; |
| 4780 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4781 | bb2 = deepCopyIRSB(bb1); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4782 | for (i = 0; i < n_vars; i++) |
| 4783 | (void)newIRTemp(bb1->tyenv, bb2->tyenv->types[i]); |
| 4784 | |
| 4785 | for (i = 0; i < bb2->stmts_used; i++) { |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4786 | /* deltaIRStmt destructively modifies the stmt, but |
| 4787 | that's OK since bb2 is a complete fresh copy of bb1. */ |
| 4788 | deltaIRStmt(bb2->stmts[i], n_vars); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4789 | addStmtToIRSB(bb1, bb2->stmts[i]); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4790 | } |
| 4791 | } |
| 4792 | |
| 4793 | if (DEBUG_IROPT) { |
| 4794 | vex_printf("\nUNROLLED (%llx)\n", my_addr); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4795 | ppIRSB(bb1); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4796 | vex_printf("\n"); |
| 4797 | } |
| 4798 | |
| 4799 | /* Flattening; sigh. The unroller succeeds in breaking flatness |
| 4800 | by negating the test condition. This should be fixed properly. |
| 4801 | For the moment use this shotgun approach. */ |
| 4802 | return flatten_BB(bb1); |
| 4803 | } |
| 4804 | |
| 4805 | |
| 4806 | /*---------------------------------------------------------------*/ |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 4807 | /*--- The tree builder ---*/ |
| 4808 | /*---------------------------------------------------------------*/ |
| 4809 | |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4810 | /* This isn't part of IR optimisation. Really it's a pass done prior |
| 4811 | to instruction selection, which improves the code that the |
| 4812 | instruction selector can produce. */ |
| 4813 | |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 4814 | /* --- The 'tmp' environment is the central data structure here --- */ |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 4815 | |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 4816 | /* The number of outstanding bindings we're prepared to track. |
| 4817 | The number of times the env becomes full and we have to dump |
| 4818 | the oldest binding (hence reducing code quality) falls very |
| 4819 | rapidly as the env size increases. 8 gives reasonable performance |
| 4820 | under most circumstances. */ |
| 4821 | #define A_NENV 10 |
| 4822 | |
florian | 740da72 | 2013-09-11 17:58:32 +0000 | [diff] [blame] | 4823 | /* An interval. Used to record the bytes in the guest state accessed |
| 4824 | by a Put[I] statement or by (one or more) Get[I] expression(s). In |
| 4825 | case of several Get[I] expressions, the lower/upper bounds are recorded. |
| 4826 | This is conservative but cheap. |
| 4827 | E.g. a Put of 8 bytes at address 100 would be recorded as [100,107]. |
| 4828 | E.g. an expression that reads 8 bytes at offset 100 and 4 bytes at |
| 4829 | offset 200 would be recorded as [100,203] */ |
| 4830 | typedef |
| 4831 | struct { |
| 4832 | Bool present; |
| 4833 | Int low; |
| 4834 | Int high; |
| 4835 | } |
| 4836 | Interval; |
| 4837 | |
| 4838 | static inline Bool |
| 4839 | intervals_overlap(Interval i1, Interval i2) |
| 4840 | { |
| 4841 | return (i1.low >= i2.low && i1.low <= i2.high) || |
| 4842 | (i2.low >= i1.low && i2.low <= i1.high); |
| 4843 | } |
| 4844 | |
| 4845 | static inline void |
| 4846 | update_interval(Interval *i, Int low, Int high) |
| 4847 | { |
| 4848 | vassert(low <= high); |
| 4849 | |
| 4850 | if (i->present) { |
| 4851 | if (low < i->low) i->low = low; |
| 4852 | if (high > i->high) i->high = high; |
| 4853 | } else { |
| 4854 | i->present = True; |
| 4855 | i->low = low; |
| 4856 | i->high = high; |
| 4857 | } |
| 4858 | } |
| 4859 | |
| 4860 | |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 4861 | /* bindee == NULL === slot is not in use |
| 4862 | bindee != NULL === slot is in use |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 4863 | */ |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 4864 | typedef |
| 4865 | struct { |
| 4866 | IRTemp binder; |
| 4867 | IRExpr* bindee; |
| 4868 | Bool doesLoad; |
florian | 740da72 | 2013-09-11 17:58:32 +0000 | [diff] [blame] | 4869 | /* Record the bytes of the guest state BINDEE reads from. */ |
| 4870 | Interval getInterval; |
sewardj | 17442fe | 2004-09-20 14:54:28 +0000 | [diff] [blame] | 4871 | } |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 4872 | ATmpInfo; |
sewardj | 17442fe | 2004-09-20 14:54:28 +0000 | [diff] [blame] | 4873 | |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 4874 | __attribute__((unused)) |
| 4875 | static void ppAEnv ( ATmpInfo* env ) |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 4876 | { |
sewardj | 17442fe | 2004-09-20 14:54:28 +0000 | [diff] [blame] | 4877 | Int i; |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 4878 | for (i = 0; i < A_NENV; i++) { |
| 4879 | vex_printf("%d tmp %d val ", i, (Int)env[i].binder); |
| 4880 | if (env[i].bindee) |
| 4881 | ppIRExpr(env[i].bindee); |
| 4882 | else |
| 4883 | vex_printf("(null)"); |
| 4884 | vex_printf("\n"); |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 4885 | } |
| 4886 | } |
| 4887 | |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 4888 | /* --- Tree-traversal fns --- */ |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 4889 | |
sewardj | 37d3801 | 2004-08-24 00:37:04 +0000 | [diff] [blame] | 4890 | /* Traverse an expr, and detect if any part of it reads memory or does |
| 4891 | a Get. Be careful ... this really controls how much the |
| 4892 | tree-builder can reorder the code, so getting it right is critical. |
| 4893 | */ |
florian | 740da72 | 2013-09-11 17:58:32 +0000 | [diff] [blame] | 4894 | static void setHints_Expr (Bool* doesLoad, Interval* getInterval, IRExpr* e ) |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 4895 | { |
sewardj | c41f1fb | 2004-08-22 09:48:08 +0000 | [diff] [blame] | 4896 | Int i; |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 4897 | switch (e->tag) { |
sewardj | c41f1fb | 2004-08-22 09:48:08 +0000 | [diff] [blame] | 4898 | case Iex_CCall: |
| 4899 | for (i = 0; e->Iex.CCall.args[i]; i++) |
florian | 740da72 | 2013-09-11 17:58:32 +0000 | [diff] [blame] | 4900 | setHints_Expr(doesLoad, getInterval, e->Iex.CCall.args[i]); |
sewardj | c41f1fb | 2004-08-22 09:48:08 +0000 | [diff] [blame] | 4901 | return; |
florian | 99dd03e | 2013-01-29 03:56:06 +0000 | [diff] [blame] | 4902 | case Iex_ITE: |
florian | 740da72 | 2013-09-11 17:58:32 +0000 | [diff] [blame] | 4903 | setHints_Expr(doesLoad, getInterval, e->Iex.ITE.cond); |
| 4904 | setHints_Expr(doesLoad, getInterval, e->Iex.ITE.iftrue); |
| 4905 | setHints_Expr(doesLoad, getInterval, e->Iex.ITE.iffalse); |
sewardj | c41f1fb | 2004-08-22 09:48:08 +0000 | [diff] [blame] | 4906 | return; |
sewardj | 40c8026 | 2006-02-08 19:30:46 +0000 | [diff] [blame] | 4907 | case Iex_Qop: |
florian | 740da72 | 2013-09-11 17:58:32 +0000 | [diff] [blame] | 4908 | setHints_Expr(doesLoad, getInterval, e->Iex.Qop.details->arg1); |
| 4909 | setHints_Expr(doesLoad, getInterval, e->Iex.Qop.details->arg2); |
| 4910 | setHints_Expr(doesLoad, getInterval, e->Iex.Qop.details->arg3); |
| 4911 | setHints_Expr(doesLoad, getInterval, e->Iex.Qop.details->arg4); |
sewardj | 40c8026 | 2006-02-08 19:30:46 +0000 | [diff] [blame] | 4912 | return; |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 4913 | case Iex_Triop: |
florian | 740da72 | 2013-09-11 17:58:32 +0000 | [diff] [blame] | 4914 | setHints_Expr(doesLoad, getInterval, e->Iex.Triop.details->arg1); |
| 4915 | setHints_Expr(doesLoad, getInterval, e->Iex.Triop.details->arg2); |
| 4916 | setHints_Expr(doesLoad, getInterval, e->Iex.Triop.details->arg3); |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 4917 | return; |
sewardj | c41f1fb | 2004-08-22 09:48:08 +0000 | [diff] [blame] | 4918 | case Iex_Binop: |
florian | 740da72 | 2013-09-11 17:58:32 +0000 | [diff] [blame] | 4919 | setHints_Expr(doesLoad, getInterval, e->Iex.Binop.arg1); |
| 4920 | setHints_Expr(doesLoad, getInterval, e->Iex.Binop.arg2); |
sewardj | c41f1fb | 2004-08-22 09:48:08 +0000 | [diff] [blame] | 4921 | return; |
| 4922 | case Iex_Unop: |
florian | 740da72 | 2013-09-11 17:58:32 +0000 | [diff] [blame] | 4923 | setHints_Expr(doesLoad, getInterval, e->Iex.Unop.arg); |
sewardj | c41f1fb | 2004-08-22 09:48:08 +0000 | [diff] [blame] | 4924 | return; |
sewardj | af1ceca | 2005-06-30 23:31:27 +0000 | [diff] [blame] | 4925 | case Iex_Load: |
sewardj | c9ad115 | 2004-10-14 00:08:21 +0000 | [diff] [blame] | 4926 | *doesLoad = True; |
florian | 740da72 | 2013-09-11 17:58:32 +0000 | [diff] [blame] | 4927 | setHints_Expr(doesLoad, getInterval, e->Iex.Load.addr); |
sewardj | c41f1fb | 2004-08-22 09:48:08 +0000 | [diff] [blame] | 4928 | return; |
florian | 740da72 | 2013-09-11 17:58:32 +0000 | [diff] [blame] | 4929 | case Iex_Get: { |
| 4930 | Int low = e->Iex.Get.offset; |
| 4931 | Int high = low + sizeofIRType(e->Iex.Get.ty) - 1; |
| 4932 | update_interval(getInterval, low, high); |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 4933 | return; |
florian | 740da72 | 2013-09-11 17:58:32 +0000 | [diff] [blame] | 4934 | } |
| 4935 | case Iex_GetI: { |
| 4936 | IRRegArray *descr = e->Iex.GetI.descr; |
| 4937 | Int size = sizeofIRType(descr->elemTy); |
| 4938 | Int low = descr->base; |
| 4939 | Int high = low + descr->nElems * size - 1; |
| 4940 | update_interval(getInterval, low, high); |
| 4941 | setHints_Expr(doesLoad, getInterval, e->Iex.GetI.ix); |
sewardj | f650199 | 2004-08-27 11:58:24 +0000 | [diff] [blame] | 4942 | return; |
florian | 740da72 | 2013-09-11 17:58:32 +0000 | [diff] [blame] | 4943 | } |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4944 | case Iex_RdTmp: |
sewardj | c41f1fb | 2004-08-22 09:48:08 +0000 | [diff] [blame] | 4945 | case Iex_Const: |
| 4946 | return; |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 4947 | default: |
| 4948 | vex_printf("\n"); ppIRExpr(e); vex_printf("\n"); |
| 4949 | vpanic("setHints_Expr"); |
| 4950 | } |
| 4951 | } |
| 4952 | |
| 4953 | |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 4954 | /* Add a binding to the front of the env and slide all the rest |
| 4955 | backwards. It should be the case that the last slot is free. */ |
| 4956 | static void addToEnvFront ( ATmpInfo* env, IRTemp binder, IRExpr* bindee ) |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 4957 | { |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 4958 | Int i; |
| 4959 | vassert(env[A_NENV-1].bindee == NULL); |
| 4960 | for (i = A_NENV-1; i >= 1; i--) |
| 4961 | env[i] = env[i-1]; |
| 4962 | env[0].binder = binder; |
| 4963 | env[0].bindee = bindee; |
| 4964 | env[0].doesLoad = False; /* filled in later */ |
florian | 740da72 | 2013-09-11 17:58:32 +0000 | [diff] [blame] | 4965 | env[0].getInterval.present = False; /* filled in later */ |
| 4966 | env[0].getInterval.low = -1; /* filled in later */ |
| 4967 | env[0].getInterval.high = -1; /* filled in later */ |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 4968 | } |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 4969 | |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 4970 | /* Given uses :: array of UShort, indexed by IRTemp |
| 4971 | Add the use-occurrences of temps in this expression |
| 4972 | to the env. |
| 4973 | */ |
| 4974 | static void aoccCount_Expr ( UShort* uses, IRExpr* e ) |
| 4975 | { |
| 4976 | Int i; |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 4977 | |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 4978 | switch (e->tag) { |
| 4979 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4980 | case Iex_RdTmp: /* the only interesting case */ |
| 4981 | uses[e->Iex.RdTmp.tmp]++; |
sewardj | 8bee6d1 | 2005-03-22 02:24:05 +0000 | [diff] [blame] | 4982 | return; |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 4983 | |
florian | 99dd03e | 2013-01-29 03:56:06 +0000 | [diff] [blame] | 4984 | case Iex_ITE: |
| 4985 | aoccCount_Expr(uses, e->Iex.ITE.cond); |
| 4986 | aoccCount_Expr(uses, e->Iex.ITE.iftrue); |
| 4987 | aoccCount_Expr(uses, e->Iex.ITE.iffalse); |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 4988 | return; |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 4989 | |
sewardj | 40c8026 | 2006-02-08 19:30:46 +0000 | [diff] [blame] | 4990 | case Iex_Qop: |
florian | 96d7cc3 | 2012-06-01 20:41:24 +0000 | [diff] [blame] | 4991 | aoccCount_Expr(uses, e->Iex.Qop.details->arg1); |
| 4992 | aoccCount_Expr(uses, e->Iex.Qop.details->arg2); |
| 4993 | aoccCount_Expr(uses, e->Iex.Qop.details->arg3); |
| 4994 | aoccCount_Expr(uses, e->Iex.Qop.details->arg4); |
sewardj | 40c8026 | 2006-02-08 19:30:46 +0000 | [diff] [blame] | 4995 | return; |
| 4996 | |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 4997 | case Iex_Triop: |
florian | 420bfa9 | 2012-06-02 20:29:22 +0000 | [diff] [blame] | 4998 | aoccCount_Expr(uses, e->Iex.Triop.details->arg1); |
| 4999 | aoccCount_Expr(uses, e->Iex.Triop.details->arg2); |
| 5000 | aoccCount_Expr(uses, e->Iex.Triop.details->arg3); |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 5001 | return; |
| 5002 | |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5003 | case Iex_Binop: |
| 5004 | aoccCount_Expr(uses, e->Iex.Binop.arg1); |
| 5005 | aoccCount_Expr(uses, e->Iex.Binop.arg2); |
| 5006 | return; |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 5007 | |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5008 | case Iex_Unop: |
| 5009 | aoccCount_Expr(uses, e->Iex.Unop.arg); |
| 5010 | return; |
| 5011 | |
| 5012 | case Iex_Load: |
| 5013 | aoccCount_Expr(uses, e->Iex.Load.addr); |
| 5014 | return; |
| 5015 | |
| 5016 | case Iex_CCall: |
| 5017 | for (i = 0; e->Iex.CCall.args[i]; i++) |
| 5018 | aoccCount_Expr(uses, e->Iex.CCall.args[i]); |
| 5019 | return; |
| 5020 | |
| 5021 | case Iex_GetI: |
| 5022 | aoccCount_Expr(uses, e->Iex.GetI.ix); |
| 5023 | return; |
| 5024 | |
| 5025 | case Iex_Const: |
| 5026 | case Iex_Get: |
| 5027 | return; |
| 5028 | |
| 5029 | default: |
| 5030 | vex_printf("\n"); ppIRExpr(e); vex_printf("\n"); |
| 5031 | vpanic("aoccCount_Expr"); |
| 5032 | } |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 5033 | } |
| 5034 | |
| 5035 | |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5036 | /* Given uses :: array of UShort, indexed by IRTemp |
| 5037 | Add the use-occurrences of temps in this statement |
| 5038 | to the env. |
| 5039 | */ |
| 5040 | static void aoccCount_Stmt ( UShort* uses, IRStmt* st ) |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 5041 | { |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5042 | Int i; |
| 5043 | IRDirty* d; |
sewardj | e9d8a26 | 2009-07-01 08:06:34 +0000 | [diff] [blame] | 5044 | IRCAS* cas; |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5045 | switch (st->tag) { |
| 5046 | case Ist_AbiHint: |
| 5047 | aoccCount_Expr(uses, st->Ist.AbiHint.base); |
sewardj | 478646f | 2008-05-01 20:13:04 +0000 | [diff] [blame] | 5048 | aoccCount_Expr(uses, st->Ist.AbiHint.nia); |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5049 | return; |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 5050 | case Ist_WrTmp: |
| 5051 | aoccCount_Expr(uses, st->Ist.WrTmp.data); |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5052 | return; |
| 5053 | case Ist_Put: |
| 5054 | aoccCount_Expr(uses, st->Ist.Put.data); |
| 5055 | return; |
| 5056 | case Ist_PutI: |
florian | d6f38b3 | 2012-05-31 15:46:18 +0000 | [diff] [blame] | 5057 | aoccCount_Expr(uses, st->Ist.PutI.details->ix); |
| 5058 | aoccCount_Expr(uses, st->Ist.PutI.details->data); |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5059 | return; |
| 5060 | case Ist_Store: |
| 5061 | aoccCount_Expr(uses, st->Ist.Store.addr); |
| 5062 | aoccCount_Expr(uses, st->Ist.Store.data); |
| 5063 | return; |
sewardj | cfe046e | 2013-01-17 14:23:53 +0000 | [diff] [blame] | 5064 | case Ist_StoreG: { |
| 5065 | IRStoreG* sg = st->Ist.StoreG.details; |
| 5066 | aoccCount_Expr(uses, sg->addr); |
| 5067 | aoccCount_Expr(uses, sg->data); |
| 5068 | aoccCount_Expr(uses, sg->guard); |
| 5069 | return; |
| 5070 | } |
| 5071 | case Ist_LoadG: { |
| 5072 | IRLoadG* lg = st->Ist.LoadG.details; |
| 5073 | aoccCount_Expr(uses, lg->addr); |
| 5074 | aoccCount_Expr(uses, lg->alt); |
| 5075 | aoccCount_Expr(uses, lg->guard); |
| 5076 | return; |
| 5077 | } |
sewardj | e9d8a26 | 2009-07-01 08:06:34 +0000 | [diff] [blame] | 5078 | case Ist_CAS: |
| 5079 | cas = st->Ist.CAS.details; |
| 5080 | aoccCount_Expr(uses, cas->addr); |
| 5081 | if (cas->expdHi) |
| 5082 | aoccCount_Expr(uses, cas->expdHi); |
| 5083 | aoccCount_Expr(uses, cas->expdLo); |
| 5084 | if (cas->dataHi) |
| 5085 | aoccCount_Expr(uses, cas->dataHi); |
| 5086 | aoccCount_Expr(uses, cas->dataLo); |
| 5087 | return; |
sewardj | e768e92 | 2009-11-26 17:17:37 +0000 | [diff] [blame] | 5088 | case Ist_LLSC: |
| 5089 | aoccCount_Expr(uses, st->Ist.LLSC.addr); |
| 5090 | if (st->Ist.LLSC.storedata) |
| 5091 | aoccCount_Expr(uses, st->Ist.LLSC.storedata); |
| 5092 | return; |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5093 | case Ist_Dirty: |
| 5094 | d = st->Ist.Dirty.details; |
| 5095 | if (d->mFx != Ifx_None) |
| 5096 | aoccCount_Expr(uses, d->mAddr); |
| 5097 | aoccCount_Expr(uses, d->guard); |
sewardj | 74142b8 | 2013-08-08 10:28:59 +0000 | [diff] [blame] | 5098 | for (i = 0; d->args[i]; i++) { |
| 5099 | IRExpr* arg = d->args[i]; |
florian | 9041956 | 2013-08-15 20:54:52 +0000 | [diff] [blame] | 5100 | if (LIKELY(!is_IRExpr_VECRET_or_BBPTR(arg))) |
sewardj | 74142b8 | 2013-08-08 10:28:59 +0000 | [diff] [blame] | 5101 | aoccCount_Expr(uses, arg); |
| 5102 | } |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5103 | return; |
| 5104 | case Ist_NoOp: |
| 5105 | case Ist_IMark: |
sewardj | c4356f0 | 2007-11-09 21:15:04 +0000 | [diff] [blame] | 5106 | case Ist_MBE: |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5107 | return; |
| 5108 | case Ist_Exit: |
| 5109 | aoccCount_Expr(uses, st->Ist.Exit.guard); |
| 5110 | return; |
| 5111 | default: |
| 5112 | vex_printf("\n"); ppIRStmt(st); vex_printf("\n"); |
| 5113 | vpanic("aoccCount_Stmt"); |
| 5114 | } |
| 5115 | } |
| 5116 | |
| 5117 | /* Look up a binding for tmp in the env. If found, return the bound |
| 5118 | expression, and set the env's binding to NULL so it is marked as |
| 5119 | used. If not found, return NULL. */ |
| 5120 | |
| 5121 | static IRExpr* atbSubst_Temp ( ATmpInfo* env, IRTemp tmp ) |
| 5122 | { |
| 5123 | Int i; |
| 5124 | for (i = 0; i < A_NENV; i++) { |
| 5125 | if (env[i].binder == tmp && env[i].bindee != NULL) { |
| 5126 | IRExpr* bindee = env[i].bindee; |
| 5127 | env[i].bindee = NULL; |
| 5128 | return bindee; |
| 5129 | } |
| 5130 | } |
| 5131 | return NULL; |
| 5132 | } |
| 5133 | |
| 5134 | /* Traverse e, looking for temps. For each observed temp, see if env |
| 5135 | contains a binding for the temp, and if so return the bound value. |
| 5136 | The env has the property that any binding it holds is |
| 5137 | 'single-shot', so once a binding is used, it is marked as no longer |
| 5138 | available, by setting its .bindee field to NULL. */ |
| 5139 | |
sewardj | eb17e49 | 2007-08-25 23:07:44 +0000 | [diff] [blame] | 5140 | static inline Bool is_Unop ( IRExpr* e, IROp op ) { |
| 5141 | return e->tag == Iex_Unop && e->Iex.Unop.op == op; |
| 5142 | } |
| 5143 | static inline Bool is_Binop ( IRExpr* e, IROp op ) { |
| 5144 | return e->tag == Iex_Binop && e->Iex.Binop.op == op; |
| 5145 | } |
| 5146 | |
| 5147 | static IRExpr* fold_IRExpr_Binop ( IROp op, IRExpr* a1, IRExpr* a2 ) |
| 5148 | { |
| 5149 | switch (op) { |
| 5150 | case Iop_Or32: |
| 5151 | /* Or32( CmpwNEZ32(x), CmpwNEZ32(y) ) --> CmpwNEZ32( Or32( x, y ) ) */ |
| 5152 | if (is_Unop(a1, Iop_CmpwNEZ32) && is_Unop(a2, Iop_CmpwNEZ32)) |
| 5153 | return IRExpr_Unop( Iop_CmpwNEZ32, |
| 5154 | IRExpr_Binop( Iop_Or32, a1->Iex.Unop.arg, |
| 5155 | a2->Iex.Unop.arg ) ); |
| 5156 | break; |
florian | 51d26fd | 2011-07-23 00:23:02 +0000 | [diff] [blame] | 5157 | |
| 5158 | case Iop_CmpNE32: |
| 5159 | /* Since X has type Ity_I1 we can simplify: |
| 5160 | CmpNE32(1Uto32(X),0)) ==> X */ |
| 5161 | if (is_Unop(a1, Iop_1Uto32) && isZeroU32(a2)) |
| 5162 | return a1->Iex.Unop.arg; |
| 5163 | break; |
| 5164 | |
sewardj | eb17e49 | 2007-08-25 23:07:44 +0000 | [diff] [blame] | 5165 | default: |
| 5166 | break; |
| 5167 | } |
| 5168 | /* no reduction rule applies */ |
| 5169 | return IRExpr_Binop( op, a1, a2 ); |
| 5170 | } |
| 5171 | |
| 5172 | static IRExpr* fold_IRExpr_Unop ( IROp op, IRExpr* aa ) |
| 5173 | { |
| 5174 | switch (op) { |
| 5175 | case Iop_CmpwNEZ64: |
florian | de6f710 | 2013-09-06 23:13:39 +0000 | [diff] [blame] | 5176 | /* CmpwNEZ64( CmpwNEZ64 ( x ) ) --> CmpwNEZ64 ( x ) */ |
| 5177 | if (is_Unop(aa, Iop_CmpwNEZ64)) |
| 5178 | return IRExpr_Unop( Iop_CmpwNEZ64, aa->Iex.Unop.arg ); |
sewardj | eb17e49 | 2007-08-25 23:07:44 +0000 | [diff] [blame] | 5179 | /* CmpwNEZ64( Or64 ( CmpwNEZ64(x), y ) ) --> CmpwNEZ64( Or64( x, y ) ) */ |
| 5180 | if (is_Binop(aa, Iop_Or64) |
| 5181 | && is_Unop(aa->Iex.Binop.arg1, Iop_CmpwNEZ64)) |
| 5182 | return fold_IRExpr_Unop( |
| 5183 | Iop_CmpwNEZ64, |
| 5184 | IRExpr_Binop(Iop_Or64, |
| 5185 | aa->Iex.Binop.arg1->Iex.Unop.arg, |
| 5186 | aa->Iex.Binop.arg2)); |
| 5187 | /* CmpwNEZ64( Or64 ( x, CmpwNEZ64(y) ) ) --> CmpwNEZ64( Or64( x, y ) ) */ |
| 5188 | if (is_Binop(aa, Iop_Or64) |
| 5189 | && is_Unop(aa->Iex.Binop.arg2, Iop_CmpwNEZ64)) |
| 5190 | return fold_IRExpr_Unop( |
| 5191 | Iop_CmpwNEZ64, |
| 5192 | IRExpr_Binop(Iop_Or64, |
| 5193 | aa->Iex.Binop.arg1, |
| 5194 | aa->Iex.Binop.arg2->Iex.Unop.arg)); |
| 5195 | break; |
| 5196 | case Iop_CmpNEZ64: |
| 5197 | /* CmpNEZ64( Left64(x) ) --> CmpNEZ64(x) */ |
| 5198 | if (is_Unop(aa, Iop_Left64)) |
| 5199 | return IRExpr_Unop(Iop_CmpNEZ64, aa->Iex.Unop.arg); |
florian | de6f710 | 2013-09-06 23:13:39 +0000 | [diff] [blame] | 5200 | /* CmpNEZ64( 1Uto64(X) ) --> X */ |
| 5201 | if (is_Unop(aa, Iop_1Uto64)) |
| 5202 | return aa->Iex.Unop.arg; |
sewardj | eb17e49 | 2007-08-25 23:07:44 +0000 | [diff] [blame] | 5203 | break; |
| 5204 | case Iop_CmpwNEZ32: |
| 5205 | /* CmpwNEZ32( CmpwNEZ32 ( x ) ) --> CmpwNEZ32 ( x ) */ |
| 5206 | if (is_Unop(aa, Iop_CmpwNEZ32)) |
| 5207 | return IRExpr_Unop( Iop_CmpwNEZ32, aa->Iex.Unop.arg ); |
| 5208 | break; |
| 5209 | case Iop_CmpNEZ32: |
| 5210 | /* CmpNEZ32( Left32(x) ) --> CmpNEZ32(x) */ |
| 5211 | if (is_Unop(aa, Iop_Left32)) |
| 5212 | return IRExpr_Unop(Iop_CmpNEZ32, aa->Iex.Unop.arg); |
florian | 51d26fd | 2011-07-23 00:23:02 +0000 | [diff] [blame] | 5213 | /* CmpNEZ32( 1Uto32(X) ) --> X */ |
| 5214 | if (is_Unop(aa, Iop_1Uto32)) |
| 5215 | return aa->Iex.Unop.arg; |
florian | de6f710 | 2013-09-06 23:13:39 +0000 | [diff] [blame] | 5216 | /* CmpNEZ32( 64to32( CmpwNEZ64(X) ) ) --> CmpNEZ64(X) */ |
| 5217 | if (is_Unop(aa, Iop_64to32) && is_Unop(aa->Iex.Unop.arg, Iop_CmpwNEZ64)) |
| 5218 | return IRExpr_Unop(Iop_CmpNEZ64, aa->Iex.Unop.arg->Iex.Unop.arg); |
florian | 51d26fd | 2011-07-23 00:23:02 +0000 | [diff] [blame] | 5219 | break; |
| 5220 | case Iop_CmpNEZ8: |
| 5221 | /* CmpNEZ8( 1Uto8(X) ) --> X */ |
| 5222 | if (is_Unop(aa, Iop_1Uto8)) |
| 5223 | return aa->Iex.Unop.arg; |
sewardj | eb17e49 | 2007-08-25 23:07:44 +0000 | [diff] [blame] | 5224 | break; |
| 5225 | case Iop_Left32: |
| 5226 | /* Left32( Left32(x) ) --> Left32(x) */ |
| 5227 | if (is_Unop(aa, Iop_Left32)) |
| 5228 | return IRExpr_Unop( Iop_Left32, aa->Iex.Unop.arg ); |
| 5229 | break; |
florian | 854912a | 2013-09-01 20:17:23 +0000 | [diff] [blame] | 5230 | case Iop_Left64: |
| 5231 | /* Left64( Left64(x) ) --> Left64(x) */ |
| 5232 | if (is_Unop(aa, Iop_Left64)) |
| 5233 | return IRExpr_Unop( Iop_Left64, aa->Iex.Unop.arg ); |
| 5234 | break; |
sewardj | 0caa0c0 | 2014-08-11 14:01:00 +0000 | [diff] [blame^] | 5235 | case Iop_ZeroHI64ofV128: |
| 5236 | /* ZeroHI64ofV128( ZeroHI64ofV128(x) ) --> ZeroHI64ofV128(x) */ |
| 5237 | if (is_Unop(aa, Iop_ZeroHI64ofV128)) |
| 5238 | return IRExpr_Unop( Iop_ZeroHI64ofV128, aa->Iex.Unop.arg ); |
| 5239 | break; |
sewardj | eb17e49 | 2007-08-25 23:07:44 +0000 | [diff] [blame] | 5240 | case Iop_32to1: |
| 5241 | /* 32to1( 1Uto32 ( x ) ) --> x */ |
| 5242 | if (is_Unop(aa, Iop_1Uto32)) |
| 5243 | return aa->Iex.Unop.arg; |
| 5244 | /* 32to1( CmpwNEZ32 ( x )) --> CmpNEZ32(x) */ |
| 5245 | if (is_Unop(aa, Iop_CmpwNEZ32)) |
| 5246 | return IRExpr_Unop( Iop_CmpNEZ32, aa->Iex.Unop.arg ); |
| 5247 | break; |
| 5248 | case Iop_64to1: |
| 5249 | /* 64to1( 1Uto64 ( x ) ) --> x */ |
| 5250 | if (is_Unop(aa, Iop_1Uto64)) |
| 5251 | return aa->Iex.Unop.arg; |
| 5252 | /* 64to1( CmpwNEZ64 ( x )) --> CmpNEZ64(x) */ |
| 5253 | if (is_Unop(aa, Iop_CmpwNEZ64)) |
| 5254 | return IRExpr_Unop( Iop_CmpNEZ64, aa->Iex.Unop.arg ); |
| 5255 | break; |
sewardj | ef425db | 2010-01-11 10:46:18 +0000 | [diff] [blame] | 5256 | case Iop_64to32: |
| 5257 | /* 64to32( 32Uto64 ( x )) --> x */ |
| 5258 | if (is_Unop(aa, Iop_32Uto64)) |
| 5259 | return aa->Iex.Unop.arg; |
sewardj | ca257bc | 2010-09-08 08:34:52 +0000 | [diff] [blame] | 5260 | /* 64to32( 8Uto64 ( x )) --> 8Uto32(x) */ |
| 5261 | if (is_Unop(aa, Iop_8Uto64)) |
| 5262 | return IRExpr_Unop(Iop_8Uto32, aa->Iex.Unop.arg); |
| 5263 | break; |
| 5264 | |
| 5265 | case Iop_32Uto64: |
| 5266 | /* 32Uto64( 8Uto32( x )) --> 8Uto64(x) */ |
| 5267 | if (is_Unop(aa, Iop_8Uto32)) |
| 5268 | return IRExpr_Unop(Iop_8Uto64, aa->Iex.Unop.arg); |
| 5269 | /* 32Uto64( 16Uto32( x )) --> 16Uto64(x) */ |
| 5270 | if (is_Unop(aa, Iop_16Uto32)) |
| 5271 | return IRExpr_Unop(Iop_16Uto64, aa->Iex.Unop.arg); |
sewardj | bba356d | 2012-04-25 16:47:53 +0000 | [diff] [blame] | 5272 | /* 32Uto64(64to32( Shr64( 32Uto64(64to32(x)), sh )) |
| 5273 | --> Shr64( 32Uto64(64to32(x)), sh )) */ |
| 5274 | if (is_Unop(aa, Iop_64to32) |
| 5275 | && is_Binop(aa->Iex.Unop.arg, Iop_Shr64) |
| 5276 | && is_Unop(aa->Iex.Unop.arg->Iex.Binop.arg1, Iop_32Uto64) |
| 5277 | && is_Unop(aa->Iex.Unop.arg->Iex.Binop.arg1->Iex.Unop.arg, |
| 5278 | Iop_64to32)) { |
| 5279 | return aa->Iex.Unop.arg; |
| 5280 | } |
| 5281 | /* 32Uto64(64to32( Shl64( 32Uto64(64to32(x)), sh )) |
| 5282 | --> 32Uto64(64to32( Shl64( x, sh )) */ |
| 5283 | if (is_Unop(aa, Iop_64to32) |
| 5284 | && is_Binop(aa->Iex.Unop.arg, Iop_Shl64) |
| 5285 | && is_Unop(aa->Iex.Unop.arg->Iex.Binop.arg1, Iop_32Uto64) |
| 5286 | && is_Unop(aa->Iex.Unop.arg->Iex.Binop.arg1->Iex.Unop.arg, |
| 5287 | Iop_64to32)) { |
| 5288 | return |
| 5289 | IRExpr_Unop( |
| 5290 | Iop_32Uto64, |
| 5291 | IRExpr_Unop( |
| 5292 | Iop_64to32, |
| 5293 | IRExpr_Binop( |
| 5294 | Iop_Shl64, |
| 5295 | aa->Iex.Unop.arg->Iex.Binop.arg1->Iex.Unop.arg->Iex.Unop.arg, |
| 5296 | aa->Iex.Unop.arg->Iex.Binop.arg2 |
| 5297 | ))); |
| 5298 | } |
sewardj | ef425db | 2010-01-11 10:46:18 +0000 | [diff] [blame] | 5299 | break; |
sewardj | 6c299f3 | 2009-12-31 18:00:12 +0000 | [diff] [blame] | 5300 | |
| 5301 | case Iop_1Sto32: |
| 5302 | /* 1Sto32( CmpNEZ8( 32to8( 1Uto32( CmpNEZ32( x ))))) -> CmpwNEZ32(x) */ |
| 5303 | if (is_Unop(aa, Iop_CmpNEZ8) |
| 5304 | && is_Unop(aa->Iex.Unop.arg, Iop_32to8) |
| 5305 | && is_Unop(aa->Iex.Unop.arg->Iex.Unop.arg, Iop_1Uto32) |
| 5306 | && is_Unop(aa->Iex.Unop.arg->Iex.Unop.arg->Iex.Unop.arg, |
| 5307 | Iop_CmpNEZ32)) { |
| 5308 | return IRExpr_Unop( Iop_CmpwNEZ32, |
| 5309 | aa->Iex.Unop.arg->Iex.Unop.arg |
| 5310 | ->Iex.Unop.arg->Iex.Unop.arg); |
| 5311 | } |
| 5312 | break; |
| 5313 | |
sewardj | eb17e49 | 2007-08-25 23:07:44 +0000 | [diff] [blame] | 5314 | default: |
| 5315 | break; |
| 5316 | } |
| 5317 | /* no reduction rule applies */ |
| 5318 | return IRExpr_Unop( op, aa ); |
| 5319 | } |
| 5320 | |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5321 | static IRExpr* atbSubst_Expr ( ATmpInfo* env, IRExpr* e ) |
| 5322 | { |
| 5323 | IRExpr* e2; |
| 5324 | IRExpr** args2; |
| 5325 | Int i; |
| 5326 | |
| 5327 | switch (e->tag) { |
| 5328 | |
| 5329 | case Iex_CCall: |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 5330 | args2 = shallowCopyIRExprVec(e->Iex.CCall.args); |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5331 | for (i = 0; args2[i]; i++) |
| 5332 | args2[i] = atbSubst_Expr(env,args2[i]); |
| 5333 | return IRExpr_CCall( |
| 5334 | e->Iex.CCall.cee, |
| 5335 | e->Iex.CCall.retty, |
| 5336 | args2 |
| 5337 | ); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 5338 | case Iex_RdTmp: |
| 5339 | e2 = atbSubst_Temp(env, e->Iex.RdTmp.tmp); |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5340 | return e2 ? e2 : e; |
florian | 99dd03e | 2013-01-29 03:56:06 +0000 | [diff] [blame] | 5341 | case Iex_ITE: |
| 5342 | return IRExpr_ITE( |
| 5343 | atbSubst_Expr(env, e->Iex.ITE.cond), |
| 5344 | atbSubst_Expr(env, e->Iex.ITE.iftrue), |
| 5345 | atbSubst_Expr(env, e->Iex.ITE.iffalse) |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5346 | ); |
sewardj | 40c8026 | 2006-02-08 19:30:46 +0000 | [diff] [blame] | 5347 | case Iex_Qop: |
| 5348 | return IRExpr_Qop( |
florian | 96d7cc3 | 2012-06-01 20:41:24 +0000 | [diff] [blame] | 5349 | e->Iex.Qop.details->op, |
| 5350 | atbSubst_Expr(env, e->Iex.Qop.details->arg1), |
| 5351 | atbSubst_Expr(env, e->Iex.Qop.details->arg2), |
| 5352 | atbSubst_Expr(env, e->Iex.Qop.details->arg3), |
| 5353 | atbSubst_Expr(env, e->Iex.Qop.details->arg4) |
sewardj | 40c8026 | 2006-02-08 19:30:46 +0000 | [diff] [blame] | 5354 | ); |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 5355 | case Iex_Triop: |
| 5356 | return IRExpr_Triop( |
florian | 420bfa9 | 2012-06-02 20:29:22 +0000 | [diff] [blame] | 5357 | e->Iex.Triop.details->op, |
| 5358 | atbSubst_Expr(env, e->Iex.Triop.details->arg1), |
| 5359 | atbSubst_Expr(env, e->Iex.Triop.details->arg2), |
| 5360 | atbSubst_Expr(env, e->Iex.Triop.details->arg3) |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 5361 | ); |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5362 | case Iex_Binop: |
sewardj | eb17e49 | 2007-08-25 23:07:44 +0000 | [diff] [blame] | 5363 | return fold_IRExpr_Binop( |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5364 | e->Iex.Binop.op, |
| 5365 | atbSubst_Expr(env, e->Iex.Binop.arg1), |
| 5366 | atbSubst_Expr(env, e->Iex.Binop.arg2) |
| 5367 | ); |
| 5368 | case Iex_Unop: |
sewardj | eb17e49 | 2007-08-25 23:07:44 +0000 | [diff] [blame] | 5369 | return fold_IRExpr_Unop( |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5370 | e->Iex.Unop.op, |
| 5371 | atbSubst_Expr(env, e->Iex.Unop.arg) |
| 5372 | ); |
| 5373 | case Iex_Load: |
| 5374 | return IRExpr_Load( |
| 5375 | e->Iex.Load.end, |
| 5376 | e->Iex.Load.ty, |
| 5377 | atbSubst_Expr(env, e->Iex.Load.addr) |
| 5378 | ); |
| 5379 | case Iex_GetI: |
| 5380 | return IRExpr_GetI( |
| 5381 | e->Iex.GetI.descr, |
| 5382 | atbSubst_Expr(env, e->Iex.GetI.ix), |
| 5383 | e->Iex.GetI.bias |
| 5384 | ); |
| 5385 | case Iex_Const: |
| 5386 | case Iex_Get: |
| 5387 | return e; |
| 5388 | default: |
| 5389 | vex_printf("\n"); ppIRExpr(e); vex_printf("\n"); |
| 5390 | vpanic("atbSubst_Expr"); |
| 5391 | } |
| 5392 | } |
| 5393 | |
| 5394 | /* Same deal as atbSubst_Expr, except for stmts. */ |
| 5395 | |
| 5396 | static IRStmt* atbSubst_Stmt ( ATmpInfo* env, IRStmt* st ) |
| 5397 | { |
sewardj | e9d8a26 | 2009-07-01 08:06:34 +0000 | [diff] [blame] | 5398 | Int i; |
| 5399 | IRDirty *d, *d2; |
| 5400 | IRCAS *cas, *cas2; |
florian | d6f38b3 | 2012-05-31 15:46:18 +0000 | [diff] [blame] | 5401 | IRPutI *puti, *puti2; |
| 5402 | |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5403 | switch (st->tag) { |
| 5404 | case Ist_AbiHint: |
| 5405 | return IRStmt_AbiHint( |
| 5406 | atbSubst_Expr(env, st->Ist.AbiHint.base), |
sewardj | 478646f | 2008-05-01 20:13:04 +0000 | [diff] [blame] | 5407 | st->Ist.AbiHint.len, |
| 5408 | atbSubst_Expr(env, st->Ist.AbiHint.nia) |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5409 | ); |
| 5410 | case Ist_Store: |
| 5411 | return IRStmt_Store( |
| 5412 | st->Ist.Store.end, |
| 5413 | atbSubst_Expr(env, st->Ist.Store.addr), |
| 5414 | atbSubst_Expr(env, st->Ist.Store.data) |
| 5415 | ); |
sewardj | cfe046e | 2013-01-17 14:23:53 +0000 | [diff] [blame] | 5416 | case Ist_StoreG: { |
| 5417 | IRStoreG* sg = st->Ist.StoreG.details; |
| 5418 | return IRStmt_StoreG(sg->end, |
| 5419 | atbSubst_Expr(env, sg->addr), |
| 5420 | atbSubst_Expr(env, sg->data), |
| 5421 | atbSubst_Expr(env, sg->guard)); |
| 5422 | } |
| 5423 | case Ist_LoadG: { |
| 5424 | IRLoadG* lg = st->Ist.LoadG.details; |
| 5425 | return IRStmt_LoadG(lg->end, lg->cvt, lg->dst, |
| 5426 | atbSubst_Expr(env, lg->addr), |
| 5427 | atbSubst_Expr(env, lg->alt), |
| 5428 | atbSubst_Expr(env, lg->guard)); |
| 5429 | } |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 5430 | case Ist_WrTmp: |
| 5431 | return IRStmt_WrTmp( |
| 5432 | st->Ist.WrTmp.tmp, |
| 5433 | atbSubst_Expr(env, st->Ist.WrTmp.data) |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5434 | ); |
| 5435 | case Ist_Put: |
| 5436 | return IRStmt_Put( |
| 5437 | st->Ist.Put.offset, |
| 5438 | atbSubst_Expr(env, st->Ist.Put.data) |
| 5439 | ); |
| 5440 | case Ist_PutI: |
florian | d6f38b3 | 2012-05-31 15:46:18 +0000 | [diff] [blame] | 5441 | puti = st->Ist.PutI.details; |
| 5442 | puti2 = mkIRPutI(puti->descr, |
| 5443 | atbSubst_Expr(env, puti->ix), |
| 5444 | puti->bias, |
| 5445 | atbSubst_Expr(env, puti->data)); |
| 5446 | return IRStmt_PutI(puti2); |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5447 | |
| 5448 | case Ist_Exit: |
| 5449 | return IRStmt_Exit( |
| 5450 | atbSubst_Expr(env, st->Ist.Exit.guard), |
| 5451 | st->Ist.Exit.jk, |
sewardj | c6f970f | 2012-04-02 21:54:49 +0000 | [diff] [blame] | 5452 | st->Ist.Exit.dst, |
| 5453 | st->Ist.Exit.offsIP |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5454 | ); |
| 5455 | case Ist_IMark: |
sewardj | 2f10aa6 | 2011-05-27 13:20:56 +0000 | [diff] [blame] | 5456 | return IRStmt_IMark(st->Ist.IMark.addr, |
| 5457 | st->Ist.IMark.len, |
| 5458 | st->Ist.IMark.delta); |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5459 | case Ist_NoOp: |
| 5460 | return IRStmt_NoOp(); |
sewardj | c4356f0 | 2007-11-09 21:15:04 +0000 | [diff] [blame] | 5461 | case Ist_MBE: |
| 5462 | return IRStmt_MBE(st->Ist.MBE.event); |
sewardj | e9d8a26 | 2009-07-01 08:06:34 +0000 | [diff] [blame] | 5463 | case Ist_CAS: |
| 5464 | cas = st->Ist.CAS.details; |
| 5465 | cas2 = mkIRCAS( |
| 5466 | cas->oldHi, cas->oldLo, cas->end, |
| 5467 | atbSubst_Expr(env, cas->addr), |
| 5468 | cas->expdHi ? atbSubst_Expr(env, cas->expdHi) : NULL, |
| 5469 | atbSubst_Expr(env, cas->expdLo), |
| 5470 | cas->dataHi ? atbSubst_Expr(env, cas->dataHi) : NULL, |
| 5471 | atbSubst_Expr(env, cas->dataLo) |
| 5472 | ); |
| 5473 | return IRStmt_CAS(cas2); |
sewardj | e768e92 | 2009-11-26 17:17:37 +0000 | [diff] [blame] | 5474 | case Ist_LLSC: |
| 5475 | return IRStmt_LLSC( |
| 5476 | st->Ist.LLSC.end, |
| 5477 | st->Ist.LLSC.result, |
| 5478 | atbSubst_Expr(env, st->Ist.LLSC.addr), |
| 5479 | st->Ist.LLSC.storedata |
| 5480 | ? atbSubst_Expr(env, st->Ist.LLSC.storedata) : NULL |
| 5481 | ); |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5482 | case Ist_Dirty: |
| 5483 | d = st->Ist.Dirty.details; |
| 5484 | d2 = emptyIRDirty(); |
| 5485 | *d2 = *d; |
| 5486 | if (d2->mFx != Ifx_None) |
| 5487 | d2->mAddr = atbSubst_Expr(env, d2->mAddr); |
| 5488 | d2->guard = atbSubst_Expr(env, d2->guard); |
sewardj | 74142b8 | 2013-08-08 10:28:59 +0000 | [diff] [blame] | 5489 | for (i = 0; d2->args[i]; i++) { |
| 5490 | IRExpr* arg = d2->args[i]; |
florian | 9041956 | 2013-08-15 20:54:52 +0000 | [diff] [blame] | 5491 | if (LIKELY(!is_IRExpr_VECRET_or_BBPTR(arg))) |
sewardj | 74142b8 | 2013-08-08 10:28:59 +0000 | [diff] [blame] | 5492 | d2->args[i] = atbSubst_Expr(env, arg); |
| 5493 | } |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5494 | return IRStmt_Dirty(d2); |
| 5495 | default: |
| 5496 | vex_printf("\n"); ppIRStmt(st); vex_printf("\n"); |
| 5497 | vpanic("atbSubst_Stmt"); |
| 5498 | } |
| 5499 | } |
| 5500 | |
florian | 088809c | 2012-12-30 18:17:18 +0000 | [diff] [blame] | 5501 | inline |
| 5502 | static Bool dirty_helper_stores ( const IRDirty *d ) |
| 5503 | { |
| 5504 | return d->mFx == Ifx_Write || d->mFx == Ifx_Modify; |
| 5505 | } |
| 5506 | |
| 5507 | inline |
florian | 740da72 | 2013-09-11 17:58:32 +0000 | [diff] [blame] | 5508 | static Interval dirty_helper_puts ( const IRDirty *d, |
| 5509 | Bool (*preciseMemExnsFn)(Int, Int), |
| 5510 | Bool *requiresPreciseMemExns ) |
florian | 088809c | 2012-12-30 18:17:18 +0000 | [diff] [blame] | 5511 | { |
| 5512 | Int i; |
florian | 740da72 | 2013-09-11 17:58:32 +0000 | [diff] [blame] | 5513 | Interval interval; |
florian | 088809c | 2012-12-30 18:17:18 +0000 | [diff] [blame] | 5514 | |
| 5515 | /* Passing the guest state pointer opens the door to modifying the |
sewardj | 74142b8 | 2013-08-08 10:28:59 +0000 | [diff] [blame] | 5516 | guest state under the covers. It's not allowed, but let's be |
florian | 088809c | 2012-12-30 18:17:18 +0000 | [diff] [blame] | 5517 | extra conservative and assume the worst. */ |
sewardj | 74142b8 | 2013-08-08 10:28:59 +0000 | [diff] [blame] | 5518 | for (i = 0; d->args[i]; i++) { |
florian | 9041956 | 2013-08-15 20:54:52 +0000 | [diff] [blame] | 5519 | if (UNLIKELY(d->args[i]->tag == Iex_BBPTR)) { |
sewardj | 74142b8 | 2013-08-08 10:28:59 +0000 | [diff] [blame] | 5520 | *requiresPreciseMemExns = True; |
florian | 740da72 | 2013-09-11 17:58:32 +0000 | [diff] [blame] | 5521 | /* Assume all guest state is written. */ |
| 5522 | interval.present = True; |
| 5523 | interval.low = 0; |
| 5524 | interval.high = 0x7FFFFFFF; |
| 5525 | return interval; |
sewardj | 74142b8 | 2013-08-08 10:28:59 +0000 | [diff] [blame] | 5526 | } |
florian | 088809c | 2012-12-30 18:17:18 +0000 | [diff] [blame] | 5527 | } |
| 5528 | |
florian | 62140c1 | 2013-01-20 03:51:04 +0000 | [diff] [blame] | 5529 | /* Check the side effects on the guest state */ |
florian | 740da72 | 2013-09-11 17:58:32 +0000 | [diff] [blame] | 5530 | interval.present = False; |
| 5531 | interval.low = interval.high = -1; |
florian | 62140c1 | 2013-01-20 03:51:04 +0000 | [diff] [blame] | 5532 | *requiresPreciseMemExns = False; |
| 5533 | |
| 5534 | for (i = 0; i < d->nFxState; ++i) { |
| 5535 | if (d->fxState[i].fx != Ifx_Read) { |
| 5536 | Int offset = d->fxState[i].offset; |
| 5537 | Int size = d->fxState[i].size; |
| 5538 | Int nRepeats = d->fxState[i].nRepeats; |
| 5539 | Int repeatLen = d->fxState[i].repeatLen; |
| 5540 | |
sewardj | 74142b8 | 2013-08-08 10:28:59 +0000 | [diff] [blame] | 5541 | if (preciseMemExnsFn(offset, |
| 5542 | offset + nRepeats * repeatLen + size - 1)) { |
florian | 62140c1 | 2013-01-20 03:51:04 +0000 | [diff] [blame] | 5543 | *requiresPreciseMemExns = True; |
florian | 62140c1 | 2013-01-20 03:51:04 +0000 | [diff] [blame] | 5544 | } |
florian | 740da72 | 2013-09-11 17:58:32 +0000 | [diff] [blame] | 5545 | update_interval(&interval, offset, |
| 5546 | offset + nRepeats * repeatLen + size - 1); |
florian | 62140c1 | 2013-01-20 03:51:04 +0000 | [diff] [blame] | 5547 | } |
| 5548 | } |
| 5549 | |
florian | 740da72 | 2013-09-11 17:58:32 +0000 | [diff] [blame] | 5550 | return interval; |
florian | 088809c | 2012-12-30 18:17:18 +0000 | [diff] [blame] | 5551 | } |
| 5552 | |
florian | 740da72 | 2013-09-11 17:58:32 +0000 | [diff] [blame] | 5553 | /* Return an interval if st modifies the guest state. Via requiresPreciseMemExns |
florian | 62140c1 | 2013-01-20 03:51:04 +0000 | [diff] [blame] | 5554 | return whether or not that modification requires precise exceptions. */ |
florian | 740da72 | 2013-09-11 17:58:32 +0000 | [diff] [blame] | 5555 | static Interval stmt_modifies_guest_state ( IRSB *bb, const IRStmt *st, |
| 5556 | Bool (*preciseMemExnsFn)(Int,Int), |
| 5557 | Bool *requiresPreciseMemExns ) |
florian | 62140c1 | 2013-01-20 03:51:04 +0000 | [diff] [blame] | 5558 | { |
florian | 740da72 | 2013-09-11 17:58:32 +0000 | [diff] [blame] | 5559 | Interval interval; |
| 5560 | |
florian | 62140c1 | 2013-01-20 03:51:04 +0000 | [diff] [blame] | 5561 | switch (st->tag) { |
| 5562 | case Ist_Put: { |
| 5563 | Int offset = st->Ist.Put.offset; |
| 5564 | Int size = sizeofIRType(typeOfIRExpr(bb->tyenv, st->Ist.Put.data)); |
| 5565 | |
| 5566 | *requiresPreciseMemExns = preciseMemExnsFn(offset, offset + size - 1); |
florian | 740da72 | 2013-09-11 17:58:32 +0000 | [diff] [blame] | 5567 | interval.present = True; |
| 5568 | interval.low = offset; |
| 5569 | interval.high = offset + size - 1; |
| 5570 | return interval; |
florian | 62140c1 | 2013-01-20 03:51:04 +0000 | [diff] [blame] | 5571 | } |
| 5572 | |
| 5573 | case Ist_PutI: { |
| 5574 | IRRegArray *descr = st->Ist.PutI.details->descr; |
| 5575 | Int offset = descr->base; |
| 5576 | Int size = sizeofIRType(descr->elemTy); |
| 5577 | |
| 5578 | /* We quietly assume here that all segments are contiguous and there |
| 5579 | are no holes. This is to avoid a loop. The assumption is conservative |
| 5580 | in the sense that we might report that precise memory exceptions are |
| 5581 | needed when in fact they are not. */ |
| 5582 | *requiresPreciseMemExns = |
| 5583 | preciseMemExnsFn(offset, offset + descr->nElems * size - 1); |
florian | 740da72 | 2013-09-11 17:58:32 +0000 | [diff] [blame] | 5584 | interval.present = True; |
| 5585 | interval.low = offset; |
| 5586 | interval.high = offset + descr->nElems * size - 1; |
| 5587 | return interval; |
florian | 62140c1 | 2013-01-20 03:51:04 +0000 | [diff] [blame] | 5588 | } |
| 5589 | |
| 5590 | case Ist_Dirty: |
| 5591 | return dirty_helper_puts(st->Ist.Dirty.details, preciseMemExnsFn, |
| 5592 | requiresPreciseMemExns); |
| 5593 | |
| 5594 | default: |
| 5595 | *requiresPreciseMemExns = False; |
florian | 740da72 | 2013-09-11 17:58:32 +0000 | [diff] [blame] | 5596 | interval.present = False; |
| 5597 | interval.low = -1; |
| 5598 | interval.high = -1; |
| 5599 | return interval; |
florian | 62140c1 | 2013-01-20 03:51:04 +0000 | [diff] [blame] | 5600 | } |
| 5601 | } |
| 5602 | |
| 5603 | /* notstatic */ Addr64 ado_treebuild_BB ( IRSB* bb, |
| 5604 | Bool (*preciseMemExnsFn)(Int,Int) ) |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5605 | { |
| 5606 | Int i, j, k, m; |
florian | 740da72 | 2013-09-11 17:58:32 +0000 | [diff] [blame] | 5607 | Bool stmtStores, invalidateMe; |
| 5608 | Interval putInterval; |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 5609 | IRStmt* st; |
| 5610 | IRStmt* st2; |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5611 | ATmpInfo env[A_NENV]; |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 5612 | |
sewardj | c6f970f | 2012-04-02 21:54:49 +0000 | [diff] [blame] | 5613 | Bool max_ga_known = False; |
| 5614 | Addr64 max_ga = 0; |
| 5615 | |
sewardj | c9ad115 | 2004-10-14 00:08:21 +0000 | [diff] [blame] | 5616 | Int n_tmps = bb->tyenv->types_used; |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5617 | UShort* uses = LibVEX_Alloc(n_tmps * sizeof(UShort)); |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 5618 | |
| 5619 | /* Phase 1. Scan forwards in bb, counting use occurrences of each |
sewardj | c6f970f | 2012-04-02 21:54:49 +0000 | [diff] [blame] | 5620 | temp. Also count occurrences in the bb->next field. Take the |
| 5621 | opportunity to also find the maximum guest address in the block, |
| 5622 | since that will be needed later for deciding when we can safely |
| 5623 | elide event checks. */ |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 5624 | |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5625 | for (i = 0; i < n_tmps; i++) |
| 5626 | uses[i] = 0; |
| 5627 | |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 5628 | for (i = 0; i < bb->stmts_used; i++) { |
| 5629 | st = bb->stmts[i]; |
sewardj | c6f970f | 2012-04-02 21:54:49 +0000 | [diff] [blame] | 5630 | switch (st->tag) { |
| 5631 | case Ist_NoOp: |
| 5632 | continue; |
| 5633 | case Ist_IMark: { |
| 5634 | Int len = st->Ist.IMark.len; |
| 5635 | Addr64 mga = st->Ist.IMark.addr + (len < 1 ? 1 : len) - 1; |
| 5636 | max_ga_known = True; |
| 5637 | if (mga > max_ga) |
| 5638 | max_ga = mga; |
| 5639 | break; |
| 5640 | } |
| 5641 | default: |
| 5642 | break; |
| 5643 | } |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5644 | aoccCount_Stmt( uses, st ); |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 5645 | } |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5646 | aoccCount_Expr(uses, bb->next ); |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 5647 | |
sewardj | c9ad115 | 2004-10-14 00:08:21 +0000 | [diff] [blame] | 5648 | # if 0 |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5649 | for (i = 0; i < n_tmps; i++) { |
| 5650 | if (uses[i] == 0) |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 5651 | continue; |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5652 | ppIRTemp( (IRTemp)i ); |
| 5653 | vex_printf(" used %d\n", (Int)uses[i] ); |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 5654 | } |
sewardj | c9ad115 | 2004-10-14 00:08:21 +0000 | [diff] [blame] | 5655 | # endif |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 5656 | |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5657 | /* Phase 2. Scan forwards in bb. For each statement in turn: |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 5658 | |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5659 | If the env is full, emit the end element. This guarantees |
| 5660 | there is at least one free slot in the following. |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 5661 | |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5662 | On seeing 't = E', occ(t)==1, |
| 5663 | let E'=env(E) |
| 5664 | delete this stmt |
| 5665 | add t -> E' to the front of the env |
| 5666 | Examine E' and set the hints for E' appropriately |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 5667 | (doesLoad? doesGet?) |
| 5668 | |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5669 | On seeing any other stmt, |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 5670 | let stmt' = env(stmt) |
| 5671 | remove from env any 't=E' binds invalidated by stmt |
| 5672 | emit the invalidated stmts |
| 5673 | emit stmt' |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5674 | compact any holes in env |
| 5675 | by sliding entries towards the front |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 5676 | |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5677 | Finally, apply env to bb->next. |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 5678 | */ |
| 5679 | |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5680 | for (i = 0; i < A_NENV; i++) { |
| 5681 | env[i].bindee = NULL; |
| 5682 | env[i].binder = IRTemp_INVALID; |
| 5683 | } |
| 5684 | |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 5685 | /* The stmts in bb are being reordered, and we are guaranteed to |
| 5686 | end up with no more than the number we started with. Use i to |
| 5687 | be the cursor of the current stmt examined and j <= i to be that |
| 5688 | for the current stmt being written. |
| 5689 | */ |
| 5690 | j = 0; |
| 5691 | for (i = 0; i < bb->stmts_used; i++) { |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5692 | |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 5693 | st = bb->stmts[i]; |
sewardj | 8bee6d1 | 2005-03-22 02:24:05 +0000 | [diff] [blame] | 5694 | if (st->tag == Ist_NoOp) |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 5695 | continue; |
| 5696 | |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5697 | /* Ensure there's at least one space in the env, by emitting |
| 5698 | the oldest binding if necessary. */ |
| 5699 | if (env[A_NENV-1].bindee != NULL) { |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 5700 | bb->stmts[j] = IRStmt_WrTmp( env[A_NENV-1].binder, |
| 5701 | env[A_NENV-1].bindee ); |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5702 | j++; |
| 5703 | vassert(j <= i); |
| 5704 | env[A_NENV-1].bindee = NULL; |
| 5705 | } |
| 5706 | |
| 5707 | /* Consider current stmt. */ |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 5708 | if (st->tag == Ist_WrTmp && uses[st->Ist.WrTmp.tmp] <= 1) { |
sewardj | 6332740 | 2006-01-25 03:26:27 +0000 | [diff] [blame] | 5709 | IRExpr *e, *e2; |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5710 | |
| 5711 | /* optional extra: dump dead bindings as we find them. |
| 5712 | Removes the need for a prior dead-code removal pass. */ |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 5713 | if (uses[st->Ist.WrTmp.tmp] == 0) { |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 5714 | if (0) vex_printf("DEAD binding\n"); |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5715 | continue; /* for (i = 0; i < bb->stmts_used; i++) loop */ |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 5716 | } |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 5717 | vassert(uses[st->Ist.WrTmp.tmp] == 1); |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5718 | |
| 5719 | /* ok, we have 't = E', occ(t)==1. Do the abovementioned |
| 5720 | actions. */ |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 5721 | e = st->Ist.WrTmp.data; |
sewardj | 6332740 | 2006-01-25 03:26:27 +0000 | [diff] [blame] | 5722 | e2 = atbSubst_Expr(env, e); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 5723 | addToEnvFront(env, st->Ist.WrTmp.tmp, e2); |
florian | 740da72 | 2013-09-11 17:58:32 +0000 | [diff] [blame] | 5724 | setHints_Expr(&env[0].doesLoad, &env[0].getInterval, e2); |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5725 | /* don't advance j, as we are deleting this stmt and instead |
| 5726 | holding it temporarily in the env. */ |
| 5727 | continue; /* for (i = 0; i < bb->stmts_used; i++) loop */ |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 5728 | } |
| 5729 | |
| 5730 | /* we get here for any other kind of statement. */ |
| 5731 | /* 'use up' any bindings required by the current statement. */ |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5732 | st2 = atbSubst_Stmt(env, st); |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 5733 | |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5734 | /* Now, before this stmt, dump any bindings in env that it |
| 5735 | invalidates. These need to be dumped in the order in which |
| 5736 | they originally entered env -- that means from oldest to |
| 5737 | youngest. */ |
sewardj | 3e83893 | 2005-01-07 12:09:15 +0000 | [diff] [blame] | 5738 | |
florian | 740da72 | 2013-09-11 17:58:32 +0000 | [diff] [blame] | 5739 | /* putInterval/stmtStores characterise what the stmt under |
sewardj | e9d8a26 | 2009-07-01 08:06:34 +0000 | [diff] [blame] | 5740 | consideration does, or might do (sidely safe @ True). */ |
florian | 62140c1 | 2013-01-20 03:51:04 +0000 | [diff] [blame] | 5741 | |
| 5742 | Bool putRequiresPreciseMemExns; |
florian | 740da72 | 2013-09-11 17:58:32 +0000 | [diff] [blame] | 5743 | putInterval = stmt_modifies_guest_state( bb, st, preciseMemExnsFn, |
| 5744 | &putRequiresPreciseMemExns); |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 5745 | |
sewardj | e9d8a26 | 2009-07-01 08:06:34 +0000 | [diff] [blame] | 5746 | /* be True if this stmt writes memory or might do (==> we don't |
| 5747 | want to reorder other loads or stores relative to it). Also, |
sewardj | e768e92 | 2009-11-26 17:17:37 +0000 | [diff] [blame] | 5748 | both LL and SC fall under this classification, since we |
sewardj | e9d8a26 | 2009-07-01 08:06:34 +0000 | [diff] [blame] | 5749 | really ought to be conservative and not reorder any other |
sewardj | e768e92 | 2009-11-26 17:17:37 +0000 | [diff] [blame] | 5750 | memory transactions relative to them. */ |
sewardj | e9d8a26 | 2009-07-01 08:06:34 +0000 | [diff] [blame] | 5751 | stmtStores |
| 5752 | = toBool( st->tag == Ist_Store |
florian | 088809c | 2012-12-30 18:17:18 +0000 | [diff] [blame] | 5753 | || (st->tag == Ist_Dirty |
| 5754 | && dirty_helper_stores(st->Ist.Dirty.details)) |
sewardj | 0f62198 | 2012-04-12 21:05:16 +0000 | [diff] [blame] | 5755 | || st->tag == Ist_LLSC |
| 5756 | || st->tag == Ist_CAS ); |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 5757 | |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5758 | for (k = A_NENV-1; k >= 0; k--) { |
| 5759 | if (env[k].bindee == NULL) |
| 5760 | continue; |
| 5761 | /* Compare the actions of this stmt with the actions of |
| 5762 | binding 'k', to see if they invalidate the binding. */ |
| 5763 | invalidateMe |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 5764 | = toBool( |
| 5765 | /* a store invalidates loaded data */ |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5766 | (env[k].doesLoad && stmtStores) |
florian | 740da72 | 2013-09-11 17:58:32 +0000 | [diff] [blame] | 5767 | /* a put invalidates get'd data, if they overlap */ |
| 5768 | || ((env[k].getInterval.present && putInterval.present) && |
| 5769 | intervals_overlap(env[k].getInterval, putInterval)) |
florian | 62140c1 | 2013-01-20 03:51:04 +0000 | [diff] [blame] | 5770 | /* a put invalidates loaded data. That means, in essense, that |
| 5771 | a load expression cannot be substituted into a statement |
| 5772 | that follows the put. But there is nothing wrong doing so |
| 5773 | except when the put statement requries precise exceptions. |
| 5774 | Think of a load that is moved past a put where the put |
| 5775 | updates the IP in the guest state. If the load generates |
| 5776 | a segfault, the wrong address (line number) would be |
| 5777 | reported. */ |
florian | 740da72 | 2013-09-11 17:58:32 +0000 | [diff] [blame] | 5778 | || (env[k].doesLoad && putInterval.present && |
| 5779 | putRequiresPreciseMemExns) |
sewardj | c4356f0 | 2007-11-09 21:15:04 +0000 | [diff] [blame] | 5780 | /* probably overly conservative: a memory bus event |
sewardj | 3e83893 | 2005-01-07 12:09:15 +0000 | [diff] [blame] | 5781 | invalidates absolutely everything, so that all |
| 5782 | computation prior to it is forced to complete before |
sewardj | c4356f0 | 2007-11-09 21:15:04 +0000 | [diff] [blame] | 5783 | proceeding with the event (fence,lock,unlock). */ |
| 5784 | || st->tag == Ist_MBE |
sewardj | 5a9ffab | 2005-05-12 17:55:01 +0000 | [diff] [blame] | 5785 | /* also be (probably overly) paranoid re AbiHints */ |
| 5786 | || st->tag == Ist_AbiHint |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 5787 | ); |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5788 | if (invalidateMe) { |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 5789 | bb->stmts[j] = IRStmt_WrTmp( env[k].binder, env[k].bindee ); |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5790 | j++; |
| 5791 | vassert(j <= i); |
| 5792 | env[k].bindee = NULL; |
| 5793 | } |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 5794 | } |
| 5795 | |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5796 | /* Slide in-use entries in env up to the front */ |
| 5797 | m = 0; |
| 5798 | for (k = 0; k < A_NENV; k++) { |
| 5799 | if (env[k].bindee != NULL) { |
| 5800 | env[m] = env[k]; |
| 5801 | m++; |
| 5802 | } |
| 5803 | } |
| 5804 | for (m = m; m < A_NENV; m++) { |
| 5805 | env[m].bindee = NULL; |
| 5806 | } |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 5807 | |
| 5808 | /* finally, emit the substituted statement */ |
| 5809 | bb->stmts[j] = st2; |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5810 | /* vex_printf("**2 "); ppIRStmt(bb->stmts[j]); vex_printf("\n"); */ |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 5811 | j++; |
| 5812 | |
| 5813 | vassert(j <= i+1); |
| 5814 | } /* for each stmt in the original bb ... */ |
| 5815 | |
| 5816 | /* Finally ... substitute the ->next field as much as possible, and |
| 5817 | dump any left-over bindings. Hmm. Perhaps there should be no |
| 5818 | left over bindings? Or any left-over bindings are |
| 5819 | by definition dead? */ |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5820 | bb->next = atbSubst_Expr(env, bb->next); |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 5821 | bb->stmts_used = j; |
sewardj | c6f970f | 2012-04-02 21:54:49 +0000 | [diff] [blame] | 5822 | |
| 5823 | return max_ga_known ? max_ga : ~(Addr64)0; |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 5824 | } |
| 5825 | |
| 5826 | |
sewardj | 695cff9 | 2004-10-13 14:50:14 +0000 | [diff] [blame] | 5827 | /*---------------------------------------------------------------*/ |
sewardj | edf4d69 | 2004-08-17 13:52:58 +0000 | [diff] [blame] | 5828 | /*--- iropt main ---*/ |
| 5829 | /*---------------------------------------------------------------*/ |
| 5830 | |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 5831 | static Bool iropt_verbose = False; /* True; */ |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 5832 | |
| 5833 | |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 5834 | /* Do a simple cleanup pass on bb. This is: redundant Get removal, |
| 5835 | redundant Put removal, constant propagation, dead code removal, |
| 5836 | clean helper specialisation, and dead code removal (again). |
sewardj | b923075 | 2004-12-29 19:25:06 +0000 | [diff] [blame] | 5837 | */ |
sewardj | 695cff9 | 2004-10-13 14:50:14 +0000 | [diff] [blame] | 5838 | |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 5839 | |
| 5840 | static |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 5841 | IRSB* cheap_transformations ( |
| 5842 | IRSB* bb, |
florian | 1ff4756 | 2012-10-21 02:09:51 +0000 | [diff] [blame] | 5843 | IRExpr* (*specHelper) (const HChar*, IRExpr**, IRStmt**, Int), |
sewardj | 8d2291c | 2004-10-25 14:50:21 +0000 | [diff] [blame] | 5844 | Bool (*preciseMemExnsFn)(Int,Int) |
| 5845 | ) |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 5846 | { |
| 5847 | redundant_get_removal_BB ( bb ); |
| 5848 | if (iropt_verbose) { |
| 5849 | vex_printf("\n========= REDUNDANT GET\n\n" ); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 5850 | ppIRSB(bb); |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 5851 | } |
sewardj | 044a215 | 2004-10-21 10:22:10 +0000 | [diff] [blame] | 5852 | |
philippe | 6c46bef | 2012-08-14 22:29:01 +0000 | [diff] [blame] | 5853 | if (vex_control.iropt_register_updates < VexRegUpdAllregsAtEachInsn) { |
philippe | c8e2f98 | 2012-08-01 22:04:13 +0000 | [diff] [blame] | 5854 | redundant_put_removal_BB ( bb, preciseMemExnsFn ); |
| 5855 | } |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 5856 | if (iropt_verbose) { |
| 5857 | vex_printf("\n========= REDUNDANT PUT\n\n" ); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 5858 | ppIRSB(bb); |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 5859 | } |
sewardj | 044a215 | 2004-10-21 10:22:10 +0000 | [diff] [blame] | 5860 | |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 5861 | bb = cprop_BB ( bb ); |
| 5862 | if (iropt_verbose) { |
| 5863 | vex_printf("\n========= CPROPD\n\n" ); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 5864 | ppIRSB(bb); |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 5865 | } |
| 5866 | |
sewardj | 49651f4 | 2004-10-28 22:11:04 +0000 | [diff] [blame] | 5867 | do_deadcode_BB ( bb ); |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 5868 | if (iropt_verbose) { |
| 5869 | vex_printf("\n========= DEAD\n\n" ); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 5870 | ppIRSB(bb); |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 5871 | } |
| 5872 | |
sewardj | b923075 | 2004-12-29 19:25:06 +0000 | [diff] [blame] | 5873 | bb = spec_helpers_BB ( bb, specHelper ); |
sewardj | 49651f4 | 2004-10-28 22:11:04 +0000 | [diff] [blame] | 5874 | do_deadcode_BB ( bb ); |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 5875 | if (iropt_verbose) { |
| 5876 | vex_printf("\n========= SPECd \n\n" ); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 5877 | ppIRSB(bb); |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 5878 | } |
| 5879 | |
| 5880 | return bb; |
| 5881 | } |
| 5882 | |
sewardj | 695cff9 | 2004-10-13 14:50:14 +0000 | [diff] [blame] | 5883 | |
| 5884 | /* Do some more expensive transformations on bb, which are aimed at |
| 5885 | optimising as much as possible in the presence of GetI and PutI. */ |
| 5886 | |
| 5887 | static |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 5888 | IRSB* expensive_transformations( IRSB* bb ) |
sewardj | 695cff9 | 2004-10-13 14:50:14 +0000 | [diff] [blame] | 5889 | { |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 5890 | (void)do_cse_BB( bb ); |
sewardj | 695cff9 | 2004-10-13 14:50:14 +0000 | [diff] [blame] | 5891 | collapse_AddSub_chains_BB( bb ); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 5892 | do_redundant_GetI_elimination( bb ); |
philippe | 6c46bef | 2012-08-14 22:29:01 +0000 | [diff] [blame] | 5893 | if (vex_control.iropt_register_updates < VexRegUpdAllregsAtEachInsn) { |
philippe | c8e2f98 | 2012-08-01 22:04:13 +0000 | [diff] [blame] | 5894 | do_redundant_PutI_elimination( bb ); |
| 5895 | } |
sewardj | 49651f4 | 2004-10-28 22:11:04 +0000 | [diff] [blame] | 5896 | do_deadcode_BB( bb ); |
sewardj | b923075 | 2004-12-29 19:25:06 +0000 | [diff] [blame] | 5897 | return bb; |
sewardj | 695cff9 | 2004-10-13 14:50:14 +0000 | [diff] [blame] | 5898 | } |
| 5899 | |
| 5900 | |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 5901 | /* Scan a flattened BB to look for signs that more expensive |
| 5902 | optimisations might be useful: |
| 5903 | - find out if there are any GetIs and PutIs |
| 5904 | - find out if there are any floating or vector-typed temporaries |
| 5905 | */ |
sewardj | 695cff9 | 2004-10-13 14:50:14 +0000 | [diff] [blame] | 5906 | |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 5907 | static void considerExpensives ( /*OUT*/Bool* hasGetIorPutI, |
| 5908 | /*OUT*/Bool* hasVorFtemps, |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 5909 | IRSB* bb ) |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 5910 | { |
sewardj | e9d8a26 | 2009-07-01 08:06:34 +0000 | [diff] [blame] | 5911 | Int i, j; |
| 5912 | IRStmt* st; |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 5913 | IRDirty* d; |
sewardj | e9d8a26 | 2009-07-01 08:06:34 +0000 | [diff] [blame] | 5914 | IRCAS* cas; |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 5915 | |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 5916 | *hasGetIorPutI = False; |
| 5917 | *hasVorFtemps = False; |
| 5918 | |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 5919 | for (i = 0; i < bb->stmts_used; i++) { |
| 5920 | st = bb->stmts[i]; |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 5921 | switch (st->tag) { |
sewardj | 5a9ffab | 2005-05-12 17:55:01 +0000 | [diff] [blame] | 5922 | case Ist_AbiHint: |
| 5923 | vassert(isIRAtom(st->Ist.AbiHint.base)); |
sewardj | 478646f | 2008-05-01 20:13:04 +0000 | [diff] [blame] | 5924 | vassert(isIRAtom(st->Ist.AbiHint.nia)); |
sewardj | 5a9ffab | 2005-05-12 17:55:01 +0000 | [diff] [blame] | 5925 | break; |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 5926 | case Ist_PutI: |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 5927 | *hasGetIorPutI = True; |
| 5928 | break; |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 5929 | case Ist_WrTmp: |
| 5930 | if (st->Ist.WrTmp.data->tag == Iex_GetI) |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 5931 | *hasGetIorPutI = True; |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 5932 | switch (typeOfIRTemp(bb->tyenv, st->Ist.WrTmp.tmp)) { |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 5933 | case Ity_I1: case Ity_I8: case Ity_I16: |
| 5934 | case Ity_I32: case Ity_I64: case Ity_I128: |
| 5935 | break; |
sewardj | c4530ae | 2012-05-21 10:18:49 +0000 | [diff] [blame] | 5936 | case Ity_F32: case Ity_F64: case Ity_F128: |
| 5937 | case Ity_V128: case Ity_V256: |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 5938 | *hasVorFtemps = True; |
| 5939 | break; |
sewardj | c6bbd47 | 2012-04-02 10:20:48 +0000 | [diff] [blame] | 5940 | case Ity_D32: case Ity_D64: case Ity_D128: |
| 5941 | *hasVorFtemps = True; |
| 5942 | break; |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 5943 | default: |
| 5944 | goto bad; |
| 5945 | } |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 5946 | break; |
| 5947 | case Ist_Put: |
sewardj | 496a58d | 2005-03-20 18:44:44 +0000 | [diff] [blame] | 5948 | vassert(isIRAtom(st->Ist.Put.data)); |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 5949 | break; |
sewardj | af1ceca | 2005-06-30 23:31:27 +0000 | [diff] [blame] | 5950 | case Ist_Store: |
| 5951 | vassert(isIRAtom(st->Ist.Store.addr)); |
| 5952 | vassert(isIRAtom(st->Ist.Store.data)); |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 5953 | break; |
sewardj | cfe046e | 2013-01-17 14:23:53 +0000 | [diff] [blame] | 5954 | case Ist_StoreG: { |
| 5955 | IRStoreG* sg = st->Ist.StoreG.details; |
| 5956 | vassert(isIRAtom(sg->addr)); |
| 5957 | vassert(isIRAtom(sg->data)); |
| 5958 | vassert(isIRAtom(sg->guard)); |
| 5959 | break; |
| 5960 | } |
| 5961 | case Ist_LoadG: { |
| 5962 | IRLoadG* lg = st->Ist.LoadG.details; |
| 5963 | vassert(isIRAtom(lg->addr)); |
| 5964 | vassert(isIRAtom(lg->alt)); |
| 5965 | vassert(isIRAtom(lg->guard)); |
| 5966 | break; |
| 5967 | } |
sewardj | e9d8a26 | 2009-07-01 08:06:34 +0000 | [diff] [blame] | 5968 | case Ist_CAS: |
| 5969 | cas = st->Ist.CAS.details; |
| 5970 | vassert(isIRAtom(cas->addr)); |
| 5971 | vassert(cas->expdHi == NULL || isIRAtom(cas->expdHi)); |
| 5972 | vassert(isIRAtom(cas->expdLo)); |
| 5973 | vassert(cas->dataHi == NULL || isIRAtom(cas->dataHi)); |
| 5974 | vassert(isIRAtom(cas->dataLo)); |
| 5975 | break; |
sewardj | e768e92 | 2009-11-26 17:17:37 +0000 | [diff] [blame] | 5976 | case Ist_LLSC: |
| 5977 | vassert(isIRAtom(st->Ist.LLSC.addr)); |
| 5978 | if (st->Ist.LLSC.storedata) |
| 5979 | vassert(isIRAtom(st->Ist.LLSC.storedata)); |
| 5980 | break; |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 5981 | case Ist_Dirty: |
| 5982 | d = st->Ist.Dirty.details; |
sewardj | 496a58d | 2005-03-20 18:44:44 +0000 | [diff] [blame] | 5983 | vassert(isIRAtom(d->guard)); |
sewardj | 74142b8 | 2013-08-08 10:28:59 +0000 | [diff] [blame] | 5984 | for (j = 0; d->args[j]; j++) { |
| 5985 | IRExpr* arg = d->args[j]; |
florian | 9041956 | 2013-08-15 20:54:52 +0000 | [diff] [blame] | 5986 | if (LIKELY(!is_IRExpr_VECRET_or_BBPTR(arg))) |
sewardj | 74142b8 | 2013-08-08 10:28:59 +0000 | [diff] [blame] | 5987 | vassert(isIRAtom(arg)); |
| 5988 | } |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 5989 | if (d->mFx != Ifx_None) |
sewardj | 496a58d | 2005-03-20 18:44:44 +0000 | [diff] [blame] | 5990 | vassert(isIRAtom(d->mAddr)); |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 5991 | break; |
sewardj | d2445f6 | 2005-03-21 00:15:53 +0000 | [diff] [blame] | 5992 | case Ist_NoOp: |
sewardj | f168931 | 2005-03-16 18:19:10 +0000 | [diff] [blame] | 5993 | case Ist_IMark: |
sewardj | c4356f0 | 2007-11-09 21:15:04 +0000 | [diff] [blame] | 5994 | case Ist_MBE: |
sewardj | 3e83893 | 2005-01-07 12:09:15 +0000 | [diff] [blame] | 5995 | break; |
| 5996 | case Ist_Exit: |
sewardj | 496a58d | 2005-03-20 18:44:44 +0000 | [diff] [blame] | 5997 | vassert(isIRAtom(st->Ist.Exit.guard)); |
sewardj | 3e83893 | 2005-01-07 12:09:15 +0000 | [diff] [blame] | 5998 | break; |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 5999 | default: |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 6000 | bad: |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 6001 | ppIRStmt(st); |
sewardj | e768e92 | 2009-11-26 17:17:37 +0000 | [diff] [blame] | 6002 | vpanic("considerExpensives"); |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 6003 | } |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 6004 | } |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 6005 | } |
| 6006 | |
| 6007 | |
sewardj | 695cff9 | 2004-10-13 14:50:14 +0000 | [diff] [blame] | 6008 | /* ---------------- The main iropt entry point. ---------------- */ |
| 6009 | |
sewardj | edf4d69 | 2004-08-17 13:52:58 +0000 | [diff] [blame] | 6010 | /* exported from this file */ |
sewardj | 695cff9 | 2004-10-13 14:50:14 +0000 | [diff] [blame] | 6011 | /* Rules of the game: |
| 6012 | |
| 6013 | - IRExpr/IRStmt trees should be treated as immutable, as they |
| 6014 | may get shared. So never change a field of such a tree node; |
| 6015 | instead construct and return a new one if needed. |
| 6016 | */ |
| 6017 | |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 6018 | |
sewardj | be91791 | 2010-08-22 12:38:53 +0000 | [diff] [blame] | 6019 | IRSB* do_iropt_BB( |
| 6020 | IRSB* bb0, |
florian | 1ff4756 | 2012-10-21 02:09:51 +0000 | [diff] [blame] | 6021 | IRExpr* (*specHelper) (const HChar*, IRExpr**, IRStmt**, Int), |
sewardj | be91791 | 2010-08-22 12:38:53 +0000 | [diff] [blame] | 6022 | Bool (*preciseMemExnsFn)(Int,Int), |
| 6023 | Addr64 guest_addr, |
| 6024 | VexArch guest_arch |
| 6025 | ) |
sewardj | edf4d69 | 2004-08-17 13:52:58 +0000 | [diff] [blame] | 6026 | { |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 6027 | static Int n_total = 0; |
| 6028 | static Int n_expensive = 0; |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 6029 | |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 6030 | Bool hasGetIorPutI, hasVorFtemps; |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 6031 | IRSB *bb, *bb2; |
sewardj | 8c2c10b | 2004-10-16 20:51:52 +0000 | [diff] [blame] | 6032 | |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 6033 | n_total++; |
| 6034 | |
| 6035 | /* First flatten the block out, since all other |
| 6036 | phases assume flat code. */ |
| 6037 | |
| 6038 | bb = flatten_BB ( bb0 ); |
| 6039 | |
| 6040 | if (iropt_verbose) { |
| 6041 | vex_printf("\n========= FLAT\n\n" ); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 6042 | ppIRSB(bb); |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 6043 | } |
sewardj | d721703 | 2004-08-19 10:49:10 +0000 | [diff] [blame] | 6044 | |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 6045 | /* If at level 0, stop now. */ |
| 6046 | if (vex_control.iropt_level <= 0) return bb; |
| 6047 | |
sewardj | 695cff9 | 2004-10-13 14:50:14 +0000 | [diff] [blame] | 6048 | /* Now do a preliminary cleanup pass, and figure out if we also |
| 6049 | need to do 'expensive' optimisations. Expensive optimisations |
| 6050 | are deemed necessary if the block contains any GetIs or PutIs. |
| 6051 | If needed, do expensive transformations and then another cheap |
| 6052 | cleanup pass. */ |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 6053 | |
sewardj | 8d2291c | 2004-10-25 14:50:21 +0000 | [diff] [blame] | 6054 | bb = cheap_transformations( bb, specHelper, preciseMemExnsFn ); |
sewardj | 695cff9 | 2004-10-13 14:50:14 +0000 | [diff] [blame] | 6055 | |
sewardj | be91791 | 2010-08-22 12:38:53 +0000 | [diff] [blame] | 6056 | if (guest_arch == VexArchARM) { |
| 6057 | /* Translating Thumb2 code produces a lot of chaff. We have to |
| 6058 | work extra hard to get rid of it. */ |
| 6059 | bb = cprop_BB(bb); |
| 6060 | bb = spec_helpers_BB ( bb, specHelper ); |
philippe | 6c46bef | 2012-08-14 22:29:01 +0000 | [diff] [blame] | 6061 | if (vex_control.iropt_register_updates < VexRegUpdAllregsAtEachInsn) { |
philippe | c8e2f98 | 2012-08-01 22:04:13 +0000 | [diff] [blame] | 6062 | redundant_put_removal_BB ( bb, preciseMemExnsFn ); |
| 6063 | } |
sewardj | d5436ce | 2011-05-01 18:36:51 +0000 | [diff] [blame] | 6064 | do_cse_BB( bb ); |
sewardj | be91791 | 2010-08-22 12:38:53 +0000 | [diff] [blame] | 6065 | do_deadcode_BB( bb ); |
| 6066 | } |
| 6067 | |
sewardj | 0861374 | 2004-10-25 13:01:45 +0000 | [diff] [blame] | 6068 | if (vex_control.iropt_level > 1) { |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 6069 | |
| 6070 | /* Peer at what we have, to decide how much more effort to throw |
| 6071 | at it. */ |
| 6072 | considerExpensives( &hasGetIorPutI, &hasVorFtemps, bb ); |
| 6073 | |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 6074 | if (hasVorFtemps && !hasGetIorPutI) { |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 6075 | /* If any evidence of FP or Vector activity, CSE, as that |
| 6076 | tends to mop up all manner of lardy code to do with |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 6077 | rounding modes. Don't bother if hasGetIorPutI since that |
| 6078 | case leads into the expensive transformations, which do |
| 6079 | CSE anyway. */ |
| 6080 | (void)do_cse_BB( bb ); |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 6081 | do_deadcode_BB( bb ); |
| 6082 | } |
| 6083 | |
| 6084 | if (hasGetIorPutI) { |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 6085 | Bool cses; |
sewardj | 39555aa | 2004-10-24 22:29:19 +0000 | [diff] [blame] | 6086 | n_expensive++; |
sewardj | 39555aa | 2004-10-24 22:29:19 +0000 | [diff] [blame] | 6087 | if (DEBUG_IROPT) |
| 6088 | vex_printf("***** EXPENSIVE %d %d\n", n_total, n_expensive); |
sewardj | 695cff9 | 2004-10-13 14:50:14 +0000 | [diff] [blame] | 6089 | bb = expensive_transformations( bb ); |
sewardj | 8d2291c | 2004-10-25 14:50:21 +0000 | [diff] [blame] | 6090 | bb = cheap_transformations( bb, specHelper, preciseMemExnsFn ); |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 6091 | /* Potentially common up GetIs */ |
| 6092 | cses = do_cse_BB( bb ); |
| 6093 | if (cses) |
| 6094 | bb = cheap_transformations( bb, specHelper, preciseMemExnsFn ); |
sewardj | 695cff9 | 2004-10-13 14:50:14 +0000 | [diff] [blame] | 6095 | } |
sewardj | 39555aa | 2004-10-24 22:29:19 +0000 | [diff] [blame] | 6096 | |
| 6097 | /* Now have a go at unrolling simple (single-BB) loops. If |
| 6098 | successful, clean up the results as much as possible. */ |
| 6099 | |
| 6100 | bb2 = maybe_loop_unroll_BB( bb, guest_addr ); |
| 6101 | if (bb2) { |
sewardj | 8d2291c | 2004-10-25 14:50:21 +0000 | [diff] [blame] | 6102 | bb = cheap_transformations( bb2, specHelper, preciseMemExnsFn ); |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 6103 | if (hasGetIorPutI) { |
sewardj | 39555aa | 2004-10-24 22:29:19 +0000 | [diff] [blame] | 6104 | bb = expensive_transformations( bb ); |
sewardj | 8d2291c | 2004-10-25 14:50:21 +0000 | [diff] [blame] | 6105 | bb = cheap_transformations( bb, specHelper, preciseMemExnsFn ); |
sewardj | 39555aa | 2004-10-24 22:29:19 +0000 | [diff] [blame] | 6106 | } else { |
| 6107 | /* at least do CSE and dead code removal */ |
sewardj | fe1ccfc | 2004-11-11 02:14:45 +0000 | [diff] [blame] | 6108 | do_cse_BB( bb ); |
sewardj | 49651f4 | 2004-10-28 22:11:04 +0000 | [diff] [blame] | 6109 | do_deadcode_BB( bb ); |
sewardj | 39555aa | 2004-10-24 22:29:19 +0000 | [diff] [blame] | 6110 | } |
| 6111 | if (0) vex_printf("vex iropt: unrolled a loop\n"); |
| 6112 | } |
| 6113 | |
sewardj | d721703 | 2004-08-19 10:49:10 +0000 | [diff] [blame] | 6114 | } |
| 6115 | |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 6116 | return bb; |
sewardj | edf4d69 | 2004-08-17 13:52:58 +0000 | [diff] [blame] | 6117 | } |
| 6118 | |
| 6119 | |
sewardj | a1a370f | 2004-08-17 13:31:55 +0000 | [diff] [blame] | 6120 | /*---------------------------------------------------------------*/ |
sewardj | cef7d3e | 2009-07-02 12:21:59 +0000 | [diff] [blame] | 6121 | /*--- end ir_opt.c ---*/ |
sewardj | a1a370f | 2004-08-17 13:31:55 +0000 | [diff] [blame] | 6122 | /*---------------------------------------------------------------*/ |