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 | |
florian | e74ce2e | 2014-09-16 22:33:52 +0000 | [diff] [blame] | 806 | case Ist_Put: |
| 807 | vassert(isIRAtom(st->Ist.Put.data)); |
| 808 | break; |
| 809 | |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 810 | case Ist_PutI: |
florian | d6f38b3 | 2012-05-31 15:46:18 +0000 | [diff] [blame] | 811 | vassert(isIRAtom(st->Ist.PutI.details->ix)); |
| 812 | vassert(isIRAtom(st->Ist.PutI.details->data)); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 813 | break; |
| 814 | |
sewardj | d2445f6 | 2005-03-21 00:15:53 +0000 | [diff] [blame] | 815 | case Ist_NoOp: |
sewardj | f168931 | 2005-03-16 18:19:10 +0000 | [diff] [blame] | 816 | case Ist_IMark: |
| 817 | break; |
| 818 | |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 819 | default: |
| 820 | vex_printf("\n"); |
| 821 | ppIRStmt(st); |
| 822 | vex_printf("\n"); |
| 823 | vpanic("handle_gets_Stmt"); |
| 824 | } |
| 825 | |
| 826 | if (memRW) { |
philippe | 6c46bef | 2012-08-14 22:29:01 +0000 | [diff] [blame] | 827 | /* This statement accesses memory. So we might need to dump all parts |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 828 | of the environment corresponding to guest state that may not |
| 829 | be reordered with respect to memory references. That means |
| 830 | at least the stack pointer. */ |
philippe | c8e2f98 | 2012-08-01 22:04:13 +0000 | [diff] [blame] | 831 | switch (vex_control.iropt_register_updates) { |
| 832 | case VexRegUpdAllregsAtMemAccess: |
| 833 | /* Precise exceptions required at mem access. |
| 834 | Flush all guest state. */ |
| 835 | for (j = 0; j < env->used; j++) |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 836 | env->inuse[j] = False; |
philippe | c8e2f98 | 2012-08-01 22:04:13 +0000 | [diff] [blame] | 837 | break; |
philippe | 6c46bef | 2012-08-14 22:29:01 +0000 | [diff] [blame] | 838 | case VexRegUpdSpAtMemAccess: |
| 839 | /* We need to dump the stack pointer |
| 840 | (needed for stack extension in m_signals.c). |
| 841 | preciseMemExnsFn will use vex_control.iropt_register_updates |
| 842 | to verify only the sp is to be checked. */ |
| 843 | /* fallthrough */ |
philippe | c8e2f98 | 2012-08-01 22:04:13 +0000 | [diff] [blame] | 844 | case VexRegUpdUnwindregsAtMemAccess: |
| 845 | for (j = 0; j < env->used; j++) { |
| 846 | if (!env->inuse[j]) |
| 847 | continue; |
| 848 | /* Just flush the minimal amount required, as computed by |
| 849 | preciseMemExnsFn. */ |
| 850 | HWord k_lo = (env->key[j] >> 16) & 0xFFFF; |
| 851 | HWord k_hi = env->key[j] & 0xFFFF; |
| 852 | if (preciseMemExnsFn( k_lo, k_hi )) |
| 853 | env->inuse[j] = False; |
| 854 | } |
| 855 | break; |
florian | e74ce2e | 2014-09-16 22:33:52 +0000 | [diff] [blame] | 856 | case VexRegUpdAllregsAtEachInsn: |
philippe | c8e2f98 | 2012-08-01 22:04:13 +0000 | [diff] [blame] | 857 | // VexRegUpdAllregsAtEachInsn cannot happen here. |
florian | e74ce2e | 2014-09-16 22:33:52 +0000 | [diff] [blame] | 858 | // fall through |
| 859 | default: |
philippe | c8e2f98 | 2012-08-01 22:04:13 +0000 | [diff] [blame] | 860 | vassert(0); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 861 | } |
| 862 | } /* if (memRW) */ |
| 863 | |
| 864 | } |
| 865 | |
| 866 | |
| 867 | /* Scan backwards, building up a set of (min offset, max |
| 868 | offset) pairs, indicating those parts of the guest state |
| 869 | for which the next event is a write. |
| 870 | |
| 871 | On seeing a conditional exit, empty the set. |
| 872 | |
| 873 | On seeing 'Put (minoff,maxoff) = t or c', if (minoff,maxoff) is |
| 874 | completely within the set, remove the Put. Otherwise, add |
| 875 | (minoff,maxoff) to the set. |
| 876 | |
| 877 | On seeing 'Get (minoff,maxoff)', remove any part of the set |
sewardj | 9843029 | 2004-12-29 17:34:50 +0000 | [diff] [blame] | 878 | overlapping (minoff,maxoff). The same has to happen for any events |
| 879 | which implicitly read parts of the guest state: dirty helper calls |
| 880 | and loads/stores. |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 881 | */ |
| 882 | |
| 883 | static void redundant_put_removal_BB ( |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 884 | IRSB* bb, |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 885 | Bool (*preciseMemExnsFn)(Int,Int) |
| 886 | ) |
| 887 | { |
| 888 | Int i, j; |
| 889 | Bool isPut; |
| 890 | IRStmt* st; |
| 891 | UInt key = 0; /* keep gcc -O happy */ |
| 892 | |
philippe | 6c46bef | 2012-08-14 22:29:01 +0000 | [diff] [blame] | 893 | vassert(vex_control.iropt_register_updates < VexRegUpdAllregsAtEachInsn); |
philippe | c8e2f98 | 2012-08-01 22:04:13 +0000 | [diff] [blame] | 894 | |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 895 | HashHW* env = newHHW(); |
sewardj | c6f970f | 2012-04-02 21:54:49 +0000 | [diff] [blame] | 896 | |
| 897 | /* Initialise the running env with the fact that the final exit |
| 898 | writes the IP (or, whatever it claims to write. We don't |
| 899 | care.) */ |
| 900 | key = mk_key_GetPut(bb->offsIP, typeOfIRExpr(bb->tyenv, bb->next)); |
| 901 | addToHHW(env, (HWord)key, 0); |
| 902 | |
| 903 | /* And now scan backwards through the statements. */ |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 904 | for (i = bb->stmts_used-1; i >= 0; i--) { |
| 905 | st = bb->stmts[i]; |
sewardj | 8bee6d1 | 2005-03-22 02:24:05 +0000 | [diff] [blame] | 906 | |
| 907 | if (st->tag == Ist_NoOp) |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 908 | continue; |
| 909 | |
| 910 | /* Deal with conditional exits. */ |
| 911 | if (st->tag == Ist_Exit) { |
sewardj | c6f970f | 2012-04-02 21:54:49 +0000 | [diff] [blame] | 912 | //Bool re_add; |
| 913 | /* Need to throw out from the env, any part of it which |
| 914 | doesn't overlap with the guest state written by this exit. |
| 915 | Since the exit only writes one section, it's simplest to |
| 916 | do this: (1) check whether env contains a write that |
| 917 | completely overlaps the write done by this exit; (2) empty |
| 918 | out env; and (3) if (1) was true, add the write done by |
| 919 | this exit. |
| 920 | |
| 921 | To make (1) a bit simpler, merely search for a write that |
| 922 | exactly matches the one done by this exit. That's safe |
| 923 | because it will fail as often or more often than a full |
| 924 | overlap check, and failure to find an overlapping write in |
| 925 | env is the safe case (we just nuke env if that |
| 926 | happens). */ |
| 927 | //vassert(isIRAtom(st->Ist.Exit.guard)); |
| 928 | /* (1) */ |
| 929 | //key = mk_key_GetPut(st->Ist.Exit.offsIP, |
| 930 | // typeOfIRConst(st->Ist.Exit.dst)); |
| 931 | //re_add = lookupHHW(env, NULL, key); |
| 932 | /* (2) */ |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 933 | for (j = 0; j < env->used; j++) |
| 934 | env->inuse[j] = False; |
sewardj | c6f970f | 2012-04-02 21:54:49 +0000 | [diff] [blame] | 935 | /* (3) */ |
| 936 | //if (0 && re_add) |
| 937 | // addToHHW(env, (HWord)key, 0); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 938 | continue; |
| 939 | } |
| 940 | |
| 941 | /* Deal with Puts */ |
| 942 | switch (st->tag) { |
| 943 | case Ist_Put: |
| 944 | isPut = True; |
| 945 | key = mk_key_GetPut( st->Ist.Put.offset, |
| 946 | typeOfIRExpr(bb->tyenv,st->Ist.Put.data) ); |
sewardj | 496a58d | 2005-03-20 18:44:44 +0000 | [diff] [blame] | 947 | vassert(isIRAtom(st->Ist.Put.data)); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 948 | break; |
| 949 | case Ist_PutI: |
| 950 | isPut = True; |
florian | d6f38b3 | 2012-05-31 15:46:18 +0000 | [diff] [blame] | 951 | key = mk_key_GetIPutI( st->Ist.PutI.details->descr ); |
| 952 | vassert(isIRAtom(st->Ist.PutI.details->ix)); |
| 953 | vassert(isIRAtom(st->Ist.PutI.details->data)); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 954 | break; |
| 955 | default: |
| 956 | isPut = False; |
| 957 | } |
| 958 | if (isPut && st->tag != Ist_PutI) { |
| 959 | /* See if any single entry in env overlaps this Put. This is |
| 960 | simplistic in that the transformation is valid if, say, two |
| 961 | or more entries in the env overlap this Put, but the use of |
| 962 | lookupHHW will only find a single entry which exactly |
| 963 | overlaps this Put. This is suboptimal but safe. */ |
| 964 | if (lookupHHW(env, NULL, (HWord)key)) { |
| 965 | /* This Put is redundant because a later one will overwrite |
| 966 | it. So NULL (nop) it out. */ |
| 967 | if (DEBUG_IROPT) { |
| 968 | vex_printf("rPUT: "); ppIRStmt(st); |
| 969 | vex_printf("\n"); |
| 970 | } |
sewardj | d2445f6 | 2005-03-21 00:15:53 +0000 | [diff] [blame] | 971 | bb->stmts[i] = IRStmt_NoOp(); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 972 | } else { |
| 973 | /* We can't demonstrate that this Put is redundant, so add it |
| 974 | to the running collection. */ |
| 975 | addToHHW(env, (HWord)key, 0); |
| 976 | } |
| 977 | continue; |
| 978 | } |
| 979 | |
| 980 | /* Deal with Gets. These remove bits of the environment since |
| 981 | appearance of a Get means that the next event for that slice |
sewardj | 9843029 | 2004-12-29 17:34:50 +0000 | [diff] [blame] | 982 | of the guest state is no longer a write, but a read. Also |
| 983 | deals with implicit reads of guest state needed to maintain |
| 984 | precise exceptions. */ |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 985 | handle_gets_Stmt( env, st, preciseMemExnsFn ); |
| 986 | } |
| 987 | } |
| 988 | |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 989 | |
| 990 | /*---------------------------------------------------------------*/ |
| 991 | /*--- Constant propagation and folding ---*/ |
| 992 | /*---------------------------------------------------------------*/ |
| 993 | |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 994 | #if STATS_IROPT |
| 995 | /* How often sameIRExprs was invoked */ |
| 996 | static UInt invocation_count; |
| 997 | /* How often sameIRExprs recursed through IRTemp assignments */ |
| 998 | static UInt recursion_count; |
| 999 | /* How often sameIRExprs found identical IRExprs */ |
| 1000 | static UInt success_count; |
| 1001 | /* How often recursing through assignments to IRTemps helped |
| 1002 | establishing equality. */ |
| 1003 | static UInt recursion_success_count; |
| 1004 | /* Whether or not recursing through an IRTemp assignment helped |
| 1005 | establishing IRExpr equality for a given sameIRExprs invocation. */ |
| 1006 | static Bool recursion_helped; |
| 1007 | /* Whether or not a given sameIRExprs invocation recursed through an |
| 1008 | IRTemp assignment */ |
| 1009 | static Bool recursed; |
| 1010 | /* Maximum number of nodes ever visited when comparing two IRExprs. */ |
| 1011 | static UInt max_nodes_visited; |
| 1012 | #endif /* STATS_IROPT */ |
| 1013 | |
| 1014 | /* Count the number of nodes visited for a given sameIRExprs invocation. */ |
| 1015 | static UInt num_nodes_visited; |
| 1016 | |
| 1017 | /* Do not visit more than NODE_LIMIT nodes when comparing two IRExprs. |
| 1018 | This is to guard against performance degradation by visiting large |
| 1019 | trees without success. */ |
| 1020 | #define NODE_LIMIT 30 |
| 1021 | |
| 1022 | |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 1023 | /* The env in this section is a map from IRTemp to IRExpr*, |
| 1024 | that is, an array indexed by IRTemp. */ |
sewardj | f650199 | 2004-08-27 11:58:24 +0000 | [diff] [blame] | 1025 | |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 1026 | /* Do both expressions compute the same value? The answer is generally |
| 1027 | conservative, i.e. it will report that the expressions do not compute |
| 1028 | the same value when in fact they do. The reason is that we do not |
| 1029 | keep track of changes in the guest state and memory. Thusly, two |
| 1030 | Get's, GetI's or Load's, even when accessing the same location, will be |
| 1031 | assumed to compute different values. After all the accesses may happen |
| 1032 | at different times and the guest state / memory can have changed in |
sewardj | a7e9638 | 2012-06-29 16:26:17 +0000 | [diff] [blame] | 1033 | the meantime. |
| 1034 | |
| 1035 | XXX IMPORTANT XXX the two expressions must have the same IR type. |
| 1036 | DO NOT CALL HERE WITH DIFFERENTLY-TYPED EXPRESSIONS. */ |
sewardj | c6f970f | 2012-04-02 21:54:49 +0000 | [diff] [blame] | 1037 | |
| 1038 | /* JRS 20-Mar-2012: split sameIRExprs_aux into a fast inlineable |
| 1039 | wrapper that deals with the common tags-don't-match case, and a |
| 1040 | slower out of line general case. Saves a few insns. */ |
| 1041 | |
| 1042 | __attribute__((noinline)) |
| 1043 | static Bool sameIRExprs_aux2 ( IRExpr** env, IRExpr* e1, IRExpr* e2 ); |
| 1044 | |
| 1045 | inline |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 1046 | static Bool sameIRExprs_aux ( IRExpr** env, IRExpr* e1, IRExpr* e2 ) |
sewardj | f672901 | 2004-08-25 12:45:13 +0000 | [diff] [blame] | 1047 | { |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 1048 | if (e1->tag != e2->tag) return False; |
sewardj | c6f970f | 2012-04-02 21:54:49 +0000 | [diff] [blame] | 1049 | return sameIRExprs_aux2(env, e1, e2); |
| 1050 | } |
sewardj | f672901 | 2004-08-25 12:45:13 +0000 | [diff] [blame] | 1051 | |
sewardj | c6f970f | 2012-04-02 21:54:49 +0000 | [diff] [blame] | 1052 | __attribute__((noinline)) |
| 1053 | static Bool sameIRExprs_aux2 ( IRExpr** env, IRExpr* e1, IRExpr* e2 ) |
| 1054 | { |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 1055 | if (num_nodes_visited++ > NODE_LIMIT) return False; |
sewardj | 6c299f3 | 2009-12-31 18:00:12 +0000 | [diff] [blame] | 1056 | |
sewardj | 6c299f3 | 2009-12-31 18:00:12 +0000 | [diff] [blame] | 1057 | switch (e1->tag) { |
| 1058 | case Iex_RdTmp: |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 1059 | if (e1->Iex.RdTmp.tmp == e2->Iex.RdTmp.tmp) return True; |
| 1060 | |
| 1061 | if (env[e1->Iex.RdTmp.tmp] && env[e2->Iex.RdTmp.tmp]) { |
| 1062 | Bool same = sameIRExprs_aux(env, env[e1->Iex.RdTmp.tmp], |
| 1063 | env[e2->Iex.RdTmp.tmp]); |
| 1064 | #if STATS_IROPT |
| 1065 | recursed = True; |
| 1066 | if (same) recursion_helped = True; |
| 1067 | #endif |
| 1068 | return same; |
| 1069 | } |
sewardj | 6c299f3 | 2009-12-31 18:00:12 +0000 | [diff] [blame] | 1070 | return False; |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 1071 | |
| 1072 | case Iex_Get: |
| 1073 | case Iex_GetI: |
| 1074 | case Iex_Load: |
| 1075 | /* Guest state / memory could have changed in the meantime. */ |
| 1076 | return False; |
| 1077 | |
| 1078 | case Iex_Binop: |
| 1079 | return toBool( e1->Iex.Binop.op == e2->Iex.Binop.op |
sewardj | 6399f81 | 2012-06-29 15:36:44 +0000 | [diff] [blame] | 1080 | && sameIRExprs_aux( env, e1->Iex.Binop.arg1, |
| 1081 | e2->Iex.Binop.arg1 ) |
| 1082 | && sameIRExprs_aux( env, e1->Iex.Binop.arg2, |
| 1083 | e2->Iex.Binop.arg2 )); |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 1084 | |
| 1085 | case Iex_Unop: |
| 1086 | return toBool( e1->Iex.Unop.op == e2->Iex.Unop.op |
sewardj | 6399f81 | 2012-06-29 15:36:44 +0000 | [diff] [blame] | 1087 | && sameIRExprs_aux( env, e1->Iex.Unop.arg, |
| 1088 | e2->Iex.Unop.arg )); |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 1089 | |
| 1090 | case Iex_Const: { |
| 1091 | IRConst *c1 = e1->Iex.Const.con; |
| 1092 | IRConst *c2 = e2->Iex.Const.con; |
| 1093 | vassert(c1->tag == c2->tag); |
| 1094 | switch (c1->tag) { |
| 1095 | case Ico_U1: return toBool( c1->Ico.U1 == c2->Ico.U1 ); |
| 1096 | case Ico_U8: return toBool( c1->Ico.U8 == c2->Ico.U8 ); |
| 1097 | case Ico_U16: return toBool( c1->Ico.U16 == c2->Ico.U16 ); |
| 1098 | case Ico_U32: return toBool( c1->Ico.U32 == c2->Ico.U32 ); |
| 1099 | case Ico_U64: return toBool( c1->Ico.U64 == c2->Ico.U64 ); |
| 1100 | default: break; |
| 1101 | } |
| 1102 | return False; |
| 1103 | } |
| 1104 | |
florian | 420bfa9 | 2012-06-02 20:29:22 +0000 | [diff] [blame] | 1105 | case Iex_Triop: { |
| 1106 | IRTriop *tri1 = e1->Iex.Triop.details; |
| 1107 | IRTriop *tri2 = e2->Iex.Triop.details; |
| 1108 | return toBool( tri1->op == tri2->op |
| 1109 | && sameIRExprs_aux( env, tri1->arg1, tri2->arg1 ) |
| 1110 | && sameIRExprs_aux( env, tri1->arg2, tri2->arg2 ) |
| 1111 | && sameIRExprs_aux( env, tri1->arg3, tri2->arg3 )); |
| 1112 | } |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 1113 | |
florian | 99dd03e | 2013-01-29 03:56:06 +0000 | [diff] [blame] | 1114 | case Iex_ITE: |
| 1115 | return toBool( sameIRExprs_aux( env, e1->Iex.ITE.cond, |
| 1116 | e2->Iex.ITE.cond ) |
| 1117 | && sameIRExprs_aux( env, e1->Iex.ITE.iftrue, |
| 1118 | e2->Iex.ITE.iftrue ) |
| 1119 | && sameIRExprs_aux( env, e1->Iex.ITE.iffalse, |
| 1120 | e2->Iex.ITE.iffalse )); |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 1121 | |
| 1122 | default: |
| 1123 | /* Not very likely to be "same". */ |
| 1124 | break; |
sewardj | 6c299f3 | 2009-12-31 18:00:12 +0000 | [diff] [blame] | 1125 | } |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 1126 | |
| 1127 | return False; |
sewardj | 6c299f3 | 2009-12-31 18:00:12 +0000 | [diff] [blame] | 1128 | } |
| 1129 | |
sewardj | c6f970f | 2012-04-02 21:54:49 +0000 | [diff] [blame] | 1130 | inline |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 1131 | static Bool sameIRExprs ( IRExpr** env, IRExpr* e1, IRExpr* e2 ) |
| 1132 | { |
| 1133 | Bool same; |
| 1134 | |
| 1135 | num_nodes_visited = 0; |
| 1136 | same = sameIRExprs_aux(env, e1, e2); |
| 1137 | |
| 1138 | #if STATS_IROPT |
| 1139 | ++invocation_count; |
| 1140 | if (recursed) ++recursion_count; |
| 1141 | success_count += same; |
| 1142 | if (same && recursion_helped) |
| 1143 | ++recursion_success_count; |
| 1144 | if (num_nodes_visited > max_nodes_visited) |
| 1145 | max_nodes_visited = num_nodes_visited; |
| 1146 | recursed = False; /* reset */ |
| 1147 | recursion_helped = False; /* reset */ |
| 1148 | #endif /* STATS_IROPT */ |
| 1149 | |
| 1150 | return same; |
| 1151 | } |
| 1152 | |
| 1153 | |
sewardj | a7e9638 | 2012-06-29 16:26:17 +0000 | [diff] [blame] | 1154 | /* Debugging-only hack (not used in production runs): make a guess |
| 1155 | whether sameIRExprs might assert due to the two args being of |
| 1156 | different types. If in doubt return False. Is only used when |
| 1157 | --vex-iropt-level > 0, that is, vex_control.iropt_verbosity > 0. |
| 1158 | Bad because it duplicates functionality from typeOfIRExpr. See |
| 1159 | comment on the single use point below for rationale. */ |
| 1160 | static |
| 1161 | Bool debug_only_hack_sameIRExprs_might_assert ( IRExpr* e1, IRExpr* e2 ) |
| 1162 | { |
| 1163 | if (e1->tag != e2->tag) return False; |
| 1164 | switch (e1->tag) { |
| 1165 | case Iex_Const: { |
| 1166 | /* The only interesting case */ |
| 1167 | IRConst *c1 = e1->Iex.Const.con; |
| 1168 | IRConst *c2 = e2->Iex.Const.con; |
| 1169 | return c1->tag != c2->tag; |
| 1170 | } |
| 1171 | default: |
| 1172 | break; |
| 1173 | } |
| 1174 | return False; |
| 1175 | } |
| 1176 | |
| 1177 | |
florian | ea7eab7 | 2011-07-21 16:21:58 +0000 | [diff] [blame] | 1178 | /* Is this literally IRExpr_Const(IRConst_U32(0)) ? */ |
| 1179 | static Bool isZeroU32 ( IRExpr* e ) |
| 1180 | { |
| 1181 | return toBool( e->tag == Iex_Const |
| 1182 | && e->Iex.Const.con->tag == Ico_U32 |
| 1183 | && e->Iex.Const.con->Ico.U32 == 0); |
| 1184 | } |
| 1185 | |
florian | 40226d1 | 2014-07-16 20:29:38 +0000 | [diff] [blame] | 1186 | /* Is this literally IRExpr_Const(IRConst_U64(0)) ? |
| 1187 | Currently unused; commented out to avoid compiler warning */ |
| 1188 | #if 0 |
sewardj | bbcf188 | 2014-01-12 12:49:10 +0000 | [diff] [blame] | 1189 | static Bool isZeroU64 ( IRExpr* e ) |
| 1190 | { |
| 1191 | return toBool( e->tag == Iex_Const |
| 1192 | && e->Iex.Const.con->tag == Ico_U64 |
| 1193 | && e->Iex.Const.con->Ico.U64 == 0); |
| 1194 | } |
florian | 40226d1 | 2014-07-16 20:29:38 +0000 | [diff] [blame] | 1195 | #endif |
sewardj | bbcf188 | 2014-01-12 12:49:10 +0000 | [diff] [blame] | 1196 | |
sewardj | 9571dc0 | 2014-01-26 18:34:23 +0000 | [diff] [blame] | 1197 | /* Is this literally IRExpr_Const(IRConst_V128(0)) ? */ |
| 1198 | static Bool isZeroV128 ( IRExpr* e ) |
| 1199 | { |
| 1200 | return toBool( e->tag == Iex_Const |
| 1201 | && e->Iex.Const.con->tag == Ico_V128 |
| 1202 | && e->Iex.Const.con->Ico.V128 == 0x0000); |
| 1203 | } |
| 1204 | |
| 1205 | /* Is this literally IRExpr_Const(IRConst_V256(0)) ? */ |
| 1206 | static Bool isZeroV256 ( IRExpr* e ) |
| 1207 | { |
| 1208 | return toBool( e->tag == Iex_Const |
| 1209 | && e->Iex.Const.con->tag == Ico_V256 |
| 1210 | && e->Iex.Const.con->Ico.V256 == 0x00000000); |
| 1211 | } |
| 1212 | |
florian | f6402ab | 2012-01-29 02:19:43 +0000 | [diff] [blame] | 1213 | /* Is this an integer constant with value 0 ? */ |
| 1214 | static Bool isZeroU ( IRExpr* e ) |
| 1215 | { |
| 1216 | if (e->tag != Iex_Const) return False; |
florian | f6402ab | 2012-01-29 02:19:43 +0000 | [diff] [blame] | 1217 | switch (e->Iex.Const.con->tag) { |
| 1218 | case Ico_U1: return toBool( e->Iex.Const.con->Ico.U1 == 0); |
| 1219 | case Ico_U8: return toBool( e->Iex.Const.con->Ico.U8 == 0); |
| 1220 | case Ico_U16: return toBool( e->Iex.Const.con->Ico.U16 == 0); |
| 1221 | case Ico_U32: return toBool( e->Iex.Const.con->Ico.U32 == 0); |
| 1222 | case Ico_U64: return toBool( e->Iex.Const.con->Ico.U64 == 0); |
| 1223 | default: vpanic("isZeroU"); |
| 1224 | } |
| 1225 | } |
| 1226 | |
sewardj | cf4be4a | 2012-03-26 09:44:39 +0000 | [diff] [blame] | 1227 | /* Is this an integer constant with value 1---1b ? */ |
| 1228 | static Bool isOnesU ( IRExpr* e ) |
| 1229 | { |
| 1230 | if (e->tag != Iex_Const) return False; |
| 1231 | switch (e->Iex.Const.con->tag) { |
| 1232 | case Ico_U8: return toBool( e->Iex.Const.con->Ico.U8 == 0xFF); |
| 1233 | case Ico_U16: return toBool( e->Iex.Const.con->Ico.U16 == 0xFFFF); |
| 1234 | case Ico_U32: return toBool( e->Iex.Const.con->Ico.U32 |
| 1235 | == 0xFFFFFFFF); |
| 1236 | case Ico_U64: return toBool( e->Iex.Const.con->Ico.U64 |
| 1237 | == 0xFFFFFFFFFFFFFFFFULL); |
| 1238 | default: ppIRExpr(e); vpanic("isOnesU"); |
| 1239 | } |
| 1240 | } |
| 1241 | |
sewardj | e1d45da | 2004-11-12 00:13:21 +0000 | [diff] [blame] | 1242 | static Bool notBool ( Bool b ) |
| 1243 | { |
| 1244 | if (b == True) return False; |
| 1245 | if (b == False) return True; |
| 1246 | vpanic("notBool"); |
| 1247 | } |
| 1248 | |
sewardj | 0033ddc | 2005-04-26 23:34:34 +0000 | [diff] [blame] | 1249 | /* Make a zero which has the same type as the result of the given |
| 1250 | primop. */ |
sewardj | 64d776c | 2010-10-01 14:06:22 +0000 | [diff] [blame] | 1251 | static IRExpr* mkZeroOfPrimopResultType ( IROp op ) |
sewardj | 0033ddc | 2005-04-26 23:34:34 +0000 | [diff] [blame] | 1252 | { |
| 1253 | switch (op) { |
sewardj | a7e9638 | 2012-06-29 16:26:17 +0000 | [diff] [blame] | 1254 | case Iop_CmpNE32: return IRExpr_Const(IRConst_U1(toBool(0))); |
sewardj | 0033ddc | 2005-04-26 23:34:34 +0000 | [diff] [blame] | 1255 | case Iop_Xor8: return IRExpr_Const(IRConst_U8(0)); |
| 1256 | case Iop_Xor16: return IRExpr_Const(IRConst_U16(0)); |
sewardj | be91791 | 2010-08-22 12:38:53 +0000 | [diff] [blame] | 1257 | case Iop_Sub32: |
sewardj | 0033ddc | 2005-04-26 23:34:34 +0000 | [diff] [blame] | 1258 | case Iop_Xor32: return IRExpr_Const(IRConst_U32(0)); |
sewardj | bbcf188 | 2014-01-12 12:49:10 +0000 | [diff] [blame] | 1259 | case Iop_And64: |
sewardj | 64d776c | 2010-10-01 14:06:22 +0000 | [diff] [blame] | 1260 | case Iop_Sub64: |
sewardj | 0033ddc | 2005-04-26 23:34:34 +0000 | [diff] [blame] | 1261 | case Iop_Xor64: return IRExpr_Const(IRConst_U64(0)); |
sewardj | 36a911a | 2014-04-03 13:48:21 +0000 | [diff] [blame] | 1262 | case Iop_XorV128: |
| 1263 | case Iop_AndV128: return IRExpr_Const(IRConst_V128(0)); |
florian | 1b7c471 | 2014-07-16 20:17:49 +0000 | [diff] [blame] | 1264 | case Iop_AndV256: return IRExpr_Const(IRConst_V256(0)); |
sewardj | 64d776c | 2010-10-01 14:06:22 +0000 | [diff] [blame] | 1265 | default: vpanic("mkZeroOfPrimopResultType: bad primop"); |
| 1266 | } |
| 1267 | } |
| 1268 | |
| 1269 | /* Make a value containing all 1-bits, which has the same type as the |
| 1270 | result of the given primop. */ |
| 1271 | static IRExpr* mkOnesOfPrimopResultType ( IROp op ) |
| 1272 | { |
| 1273 | switch (op) { |
sewardj | a7e9638 | 2012-06-29 16:26:17 +0000 | [diff] [blame] | 1274 | case Iop_CmpEQ32: |
sewardj | 64d776c | 2010-10-01 14:06:22 +0000 | [diff] [blame] | 1275 | case Iop_CmpEQ64: |
| 1276 | return IRExpr_Const(IRConst_U1(toBool(1))); |
sewardj | cf4be4a | 2012-03-26 09:44:39 +0000 | [diff] [blame] | 1277 | case Iop_Or8: |
| 1278 | return IRExpr_Const(IRConst_U8(0xFF)); |
| 1279 | case Iop_Or16: |
| 1280 | return IRExpr_Const(IRConst_U16(0xFFFF)); |
| 1281 | case Iop_Or32: |
| 1282 | return IRExpr_Const(IRConst_U32(0xFFFFFFFF)); |
sewardj | 64d776c | 2010-10-01 14:06:22 +0000 | [diff] [blame] | 1283 | case Iop_CmpEQ8x8: |
sewardj | cf4be4a | 2012-03-26 09:44:39 +0000 | [diff] [blame] | 1284 | case Iop_Or64: |
sewardj | 64d776c | 2010-10-01 14:06:22 +0000 | [diff] [blame] | 1285 | return IRExpr_Const(IRConst_U64(0xFFFFFFFFFFFFFFFFULL)); |
| 1286 | case Iop_CmpEQ8x16: |
sewardj | 899d183 | 2012-07-10 21:41:01 +0000 | [diff] [blame] | 1287 | case Iop_CmpEQ16x8: |
sewardj | ffccf2b | 2012-06-29 15:33:09 +0000 | [diff] [blame] | 1288 | case Iop_CmpEQ32x4: |
sewardj | 64d776c | 2010-10-01 14:06:22 +0000 | [diff] [blame] | 1289 | return IRExpr_Const(IRConst_V128(0xFFFF)); |
| 1290 | default: |
sewardj | cf4be4a | 2012-03-26 09:44:39 +0000 | [diff] [blame] | 1291 | ppIROp(op); |
sewardj | 64d776c | 2010-10-01 14:06:22 +0000 | [diff] [blame] | 1292 | vpanic("mkOnesOfPrimopResultType: bad primop"); |
sewardj | 0033ddc | 2005-04-26 23:34:34 +0000 | [diff] [blame] | 1293 | } |
| 1294 | } |
| 1295 | |
sewardj | 4cba9f4 | 2011-03-07 18:34:34 +0000 | [diff] [blame] | 1296 | /* Helpers for folding Clz32/64. */ |
| 1297 | static UInt fold_Clz64 ( ULong value ) |
| 1298 | { |
| 1299 | UInt i; |
| 1300 | vassert(value != 0ULL); /* no defined semantics for arg==0 */ |
| 1301 | for (i = 0; i < 64; ++i) { |
sewardj | 7f6330d | 2011-04-05 11:06:02 +0000 | [diff] [blame] | 1302 | if (0ULL != (value & (((ULong)1) << (63 - i)))) return i; |
sewardj | 4cba9f4 | 2011-03-07 18:34:34 +0000 | [diff] [blame] | 1303 | } |
| 1304 | vassert(0); |
| 1305 | /*NOTREACHED*/ |
| 1306 | return 0; |
| 1307 | } |
| 1308 | |
| 1309 | static UInt fold_Clz32 ( UInt value ) |
| 1310 | { |
| 1311 | UInt i; |
| 1312 | vassert(value != 0); /* no defined semantics for arg==0 */ |
| 1313 | for (i = 0; i < 32; ++i) { |
sewardj | 7f6330d | 2011-04-05 11:06:02 +0000 | [diff] [blame] | 1314 | if (0 != (value & (((UInt)1) << (31 - i)))) return i; |
sewardj | 4cba9f4 | 2011-03-07 18:34:34 +0000 | [diff] [blame] | 1315 | } |
| 1316 | vassert(0); |
| 1317 | /*NOTREACHED*/ |
| 1318 | return 0; |
| 1319 | } |
| 1320 | |
sewardj | 4a0bee0 | 2012-06-29 14:44:44 +0000 | [diff] [blame] | 1321 | /* V64 holds 8 summary-constant bits in V128/V256 style. Convert to |
| 1322 | the corresponding real constant. */ |
| 1323 | //XXX re-check this before use |
| 1324 | //static ULong de_summarise_V64 ( UChar v64 ) |
| 1325 | //{ |
| 1326 | // ULong r = 0; |
| 1327 | // if (v64 & (1<<0)) r |= 0x00000000000000FFULL; |
| 1328 | // if (v64 & (1<<1)) r |= 0x000000000000FF00ULL; |
| 1329 | // if (v64 & (1<<2)) r |= 0x0000000000FF0000ULL; |
| 1330 | // if (v64 & (1<<3)) r |= 0x00000000FF000000ULL; |
| 1331 | // if (v64 & (1<<4)) r |= 0x000000FF00000000ULL; |
| 1332 | // if (v64 & (1<<5)) r |= 0x0000FF0000000000ULL; |
| 1333 | // if (v64 & (1<<6)) r |= 0x00FF000000000000ULL; |
| 1334 | // if (v64 & (1<<7)) r |= 0xFF00000000000000ULL; |
| 1335 | // return r; |
| 1336 | //} |
sewardj | 0033ddc | 2005-04-26 23:34:34 +0000 | [diff] [blame] | 1337 | |
sewardj | 009230b | 2013-01-26 11:47:55 +0000 | [diff] [blame] | 1338 | /* Helper for arbitrary expression pattern matching in flat IR. If |
| 1339 | 'e' is a reference to a tmp, look it up in env -- repeatedly, if |
| 1340 | necessary -- until it resolves to a non-tmp. Note that this can |
| 1341 | return NULL if it can't resolve 'e' to a new expression, which will |
| 1342 | be the case if 'e' is instead defined by an IRStmt (IRDirty or |
| 1343 | LLSC). */ |
| 1344 | static IRExpr* chase ( IRExpr** env, IRExpr* e ) |
| 1345 | { |
| 1346 | /* Why is this loop guaranteed to terminate? Because all tmps must |
| 1347 | have definitions before use, hence a tmp cannot be bound |
| 1348 | (directly or indirectly) to itself. */ |
| 1349 | while (e->tag == Iex_RdTmp) { |
| 1350 | if (0) { vex_printf("chase "); ppIRExpr(e); vex_printf("\n"); } |
| 1351 | e = env[(Int)e->Iex.RdTmp.tmp]; |
| 1352 | if (e == NULL) break; |
| 1353 | } |
| 1354 | return e; |
| 1355 | } |
| 1356 | |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 1357 | static IRExpr* fold_Expr ( IRExpr** env, IRExpr* e ) |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 1358 | { |
sewardj | 278c44c | 2004-08-20 00:28:13 +0000 | [diff] [blame] | 1359 | Int shift; |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 1360 | IRExpr* e2 = e; /* e2 is the result of folding e, if possible */ |
| 1361 | |
florian | 708417d | 2012-02-15 00:43:36 +0000 | [diff] [blame] | 1362 | switch (e->tag) { |
| 1363 | case Iex_Unop: |
| 1364 | /* UNARY ops */ |
| 1365 | if (e->Iex.Unop.arg->tag == Iex_Const) { |
| 1366 | switch (e->Iex.Unop.op) { |
sewardj | ae27ab6 | 2004-10-15 21:21:46 +0000 | [diff] [blame] | 1367 | case Iop_1Uto8: |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1368 | e2 = IRExpr_Const(IRConst_U8(toUChar( |
sewardj | ba99931 | 2004-11-15 15:21:17 +0000 | [diff] [blame] | 1369 | e->Iex.Unop.arg->Iex.Const.con->Ico.U1 |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1370 | ? 1 : 0))); |
sewardj | ae27ab6 | 2004-10-15 21:21:46 +0000 | [diff] [blame] | 1371 | break; |
sewardj | f4a821d | 2004-10-09 00:58:19 +0000 | [diff] [blame] | 1372 | case Iop_1Uto32: |
| 1373 | e2 = IRExpr_Const(IRConst_U32( |
sewardj | ba99931 | 2004-11-15 15:21:17 +0000 | [diff] [blame] | 1374 | e->Iex.Unop.arg->Iex.Const.con->Ico.U1 |
sewardj | f4a821d | 2004-10-09 00:58:19 +0000 | [diff] [blame] | 1375 | ? 1 : 0)); |
| 1376 | break; |
sewardj | 2716ff1 | 2005-05-20 19:21:45 +0000 | [diff] [blame] | 1377 | case Iop_1Uto64: |
| 1378 | e2 = IRExpr_Const(IRConst_U64( |
| 1379 | e->Iex.Unop.arg->Iex.Const.con->Ico.U1 |
| 1380 | ? 1 : 0)); |
| 1381 | break; |
sewardj | e6b3993 | 2004-11-06 17:01:15 +0000 | [diff] [blame] | 1382 | |
sewardj | 1bee561 | 2005-11-10 18:10:58 +0000 | [diff] [blame] | 1383 | case Iop_1Sto8: |
| 1384 | e2 = IRExpr_Const(IRConst_U8(toUChar( |
| 1385 | e->Iex.Unop.arg->Iex.Const.con->Ico.U1 |
| 1386 | ? 0xFF : 0))); |
| 1387 | break; |
sewardj | 68884ef | 2005-07-18 13:58:49 +0000 | [diff] [blame] | 1388 | case Iop_1Sto16: |
sewardj | 743d8f0 | 2005-07-27 00:22:37 +0000 | [diff] [blame] | 1389 | e2 = IRExpr_Const(IRConst_U16(toUShort( |
sewardj | 68884ef | 2005-07-18 13:58:49 +0000 | [diff] [blame] | 1390 | e->Iex.Unop.arg->Iex.Const.con->Ico.U1 |
sewardj | 743d8f0 | 2005-07-27 00:22:37 +0000 | [diff] [blame] | 1391 | ? 0xFFFF : 0))); |
sewardj | 68884ef | 2005-07-18 13:58:49 +0000 | [diff] [blame] | 1392 | break; |
sewardj | d999788 | 2004-11-04 19:42:59 +0000 | [diff] [blame] | 1393 | case Iop_1Sto32: |
| 1394 | e2 = IRExpr_Const(IRConst_U32( |
sewardj | ba99931 | 2004-11-15 15:21:17 +0000 | [diff] [blame] | 1395 | e->Iex.Unop.arg->Iex.Const.con->Ico.U1 |
sewardj | d999788 | 2004-11-04 19:42:59 +0000 | [diff] [blame] | 1396 | ? 0xFFFFFFFF : 0)); |
| 1397 | break; |
sewardj | e6b3993 | 2004-11-06 17:01:15 +0000 | [diff] [blame] | 1398 | case Iop_1Sto64: |
| 1399 | e2 = IRExpr_Const(IRConst_U64( |
sewardj | ba99931 | 2004-11-15 15:21:17 +0000 | [diff] [blame] | 1400 | e->Iex.Unop.arg->Iex.Const.con->Ico.U1 |
sewardj | e6b3993 | 2004-11-06 17:01:15 +0000 | [diff] [blame] | 1401 | ? 0xFFFFFFFFFFFFFFFFULL : 0)); |
| 1402 | break; |
| 1403 | |
sewardj | 883b00b | 2004-09-11 09:30:24 +0000 | [diff] [blame] | 1404 | case Iop_8Sto32: { |
| 1405 | /* signed */ Int s32 = e->Iex.Unop.arg->Iex.Const.con->Ico.U8; |
| 1406 | s32 <<= 24; |
| 1407 | s32 >>= 24; |
| 1408 | e2 = IRExpr_Const(IRConst_U32((UInt)s32)); |
| 1409 | break; |
| 1410 | } |
sewardj | 7f6330d | 2011-04-05 11:06:02 +0000 | [diff] [blame] | 1411 | case Iop_16Sto32: { |
| 1412 | /* signed */ Int s32 = e->Iex.Unop.arg->Iex.Const.con->Ico.U16; |
| 1413 | s32 <<= 16; |
| 1414 | s32 >>= 16; |
| 1415 | e2 = IRExpr_Const(IRConst_U32( (UInt)s32) ); |
| 1416 | break; |
| 1417 | } |
sewardj | 291a7e8 | 2005-04-27 11:42:44 +0000 | [diff] [blame] | 1418 | case Iop_8Uto64: |
| 1419 | e2 = IRExpr_Const(IRConst_U64( |
| 1420 | 0xFFULL & e->Iex.Unop.arg->Iex.Const.con->Ico.U8)); |
| 1421 | break; |
| 1422 | case Iop_16Uto64: |
| 1423 | e2 = IRExpr_Const(IRConst_U64( |
| 1424 | 0xFFFFULL & e->Iex.Unop.arg->Iex.Const.con->Ico.U16)); |
| 1425 | break; |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 1426 | case Iop_8Uto32: |
| 1427 | e2 = IRExpr_Const(IRConst_U32( |
| 1428 | 0xFF & e->Iex.Unop.arg->Iex.Const.con->Ico.U8)); |
| 1429 | break; |
sewardj | 7f6330d | 2011-04-05 11:06:02 +0000 | [diff] [blame] | 1430 | case Iop_8Sto16: { |
| 1431 | /* signed */ Short s16 = e->Iex.Unop.arg->Iex.Const.con->Ico.U8; |
| 1432 | s16 <<= 8; |
| 1433 | s16 >>= 8; |
| 1434 | e2 = IRExpr_Const(IRConst_U16( (UShort)s16) ); |
| 1435 | break; |
| 1436 | } |
| 1437 | case Iop_8Uto16: |
| 1438 | e2 = IRExpr_Const(IRConst_U16( |
| 1439 | 0xFF & e->Iex.Unop.arg->Iex.Const.con->Ico.U8)); |
| 1440 | break; |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 1441 | case Iop_16Uto32: |
| 1442 | e2 = IRExpr_Const(IRConst_U32( |
| 1443 | 0xFFFF & e->Iex.Unop.arg->Iex.Const.con->Ico.U16)); |
| 1444 | break; |
sewardj | 7301743 | 2004-10-14 19:33:25 +0000 | [diff] [blame] | 1445 | case Iop_32to16: |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1446 | e2 = IRExpr_Const(IRConst_U16(toUShort( |
| 1447 | 0xFFFF & e->Iex.Unop.arg->Iex.Const.con->Ico.U32))); |
sewardj | 7301743 | 2004-10-14 19:33:25 +0000 | [diff] [blame] | 1448 | break; |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 1449 | case Iop_32to8: |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1450 | e2 = IRExpr_Const(IRConst_U8(toUChar( |
| 1451 | 0xFF & e->Iex.Unop.arg->Iex.Const.con->Ico.U32))); |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 1452 | break; |
sewardj | 7447b5b | 2004-10-16 11:30:42 +0000 | [diff] [blame] | 1453 | case Iop_32to1: |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1454 | e2 = IRExpr_Const(IRConst_U1(toBool( |
| 1455 | 1 == (1 & e->Iex.Unop.arg->Iex.Const.con->Ico.U32) |
| 1456 | ))); |
sewardj | 7447b5b | 2004-10-16 11:30:42 +0000 | [diff] [blame] | 1457 | break; |
sewardj | 291a7e8 | 2005-04-27 11:42:44 +0000 | [diff] [blame] | 1458 | case Iop_64to1: |
| 1459 | e2 = IRExpr_Const(IRConst_U1(toBool( |
| 1460 | 1 == (1 & e->Iex.Unop.arg->Iex.Const.con->Ico.U64) |
| 1461 | ))); |
| 1462 | break; |
sewardj | e6b3993 | 2004-11-06 17:01:15 +0000 | [diff] [blame] | 1463 | |
sewardj | 4a0bee0 | 2012-06-29 14:44:44 +0000 | [diff] [blame] | 1464 | case Iop_NotV128: |
| 1465 | e2 = IRExpr_Const(IRConst_V128( |
| 1466 | ~ (e->Iex.Unop.arg->Iex.Const.con->Ico.V128))); |
| 1467 | break; |
sewardj | f057afb | 2005-02-27 13:35:41 +0000 | [diff] [blame] | 1468 | case Iop_Not64: |
| 1469 | e2 = IRExpr_Const(IRConst_U64( |
| 1470 | ~ (e->Iex.Unop.arg->Iex.Const.con->Ico.U64))); |
| 1471 | break; |
sewardj | 883b00b | 2004-09-11 09:30:24 +0000 | [diff] [blame] | 1472 | case Iop_Not32: |
| 1473 | e2 = IRExpr_Const(IRConst_U32( |
| 1474 | ~ (e->Iex.Unop.arg->Iex.Const.con->Ico.U32))); |
| 1475 | break; |
sewardj | e6b3993 | 2004-11-06 17:01:15 +0000 | [diff] [blame] | 1476 | case Iop_Not16: |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1477 | e2 = IRExpr_Const(IRConst_U16(toUShort( |
| 1478 | ~ (e->Iex.Unop.arg->Iex.Const.con->Ico.U16)))); |
sewardj | e6b3993 | 2004-11-06 17:01:15 +0000 | [diff] [blame] | 1479 | break; |
sewardj | d999788 | 2004-11-04 19:42:59 +0000 | [diff] [blame] | 1480 | case Iop_Not8: |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1481 | e2 = IRExpr_Const(IRConst_U8(toUChar( |
| 1482 | ~ (e->Iex.Unop.arg->Iex.Const.con->Ico.U8)))); |
sewardj | d999788 | 2004-11-04 19:42:59 +0000 | [diff] [blame] | 1483 | break; |
sewardj | e6b3993 | 2004-11-06 17:01:15 +0000 | [diff] [blame] | 1484 | |
sewardj | e1d45da | 2004-11-12 00:13:21 +0000 | [diff] [blame] | 1485 | case Iop_Not1: |
sewardj | ba99931 | 2004-11-15 15:21:17 +0000 | [diff] [blame] | 1486 | e2 = IRExpr_Const(IRConst_U1( |
| 1487 | notBool(e->Iex.Unop.arg->Iex.Const.con->Ico.U1))); |
sewardj | e1d45da | 2004-11-12 00:13:21 +0000 | [diff] [blame] | 1488 | break; |
| 1489 | |
sewardj | 291a7e8 | 2005-04-27 11:42:44 +0000 | [diff] [blame] | 1490 | case Iop_64to8: { |
| 1491 | ULong w64 = e->Iex.Unop.arg->Iex.Const.con->Ico.U64; |
| 1492 | w64 &= 0xFFULL; |
| 1493 | e2 = IRExpr_Const(IRConst_U8( (UChar)w64 )); |
| 1494 | break; |
| 1495 | } |
| 1496 | case Iop_64to16: { |
| 1497 | ULong w64 = e->Iex.Unop.arg->Iex.Const.con->Ico.U64; |
| 1498 | w64 &= 0xFFFFULL; |
sewardj | e85bc40 | 2005-05-06 16:29:26 +0000 | [diff] [blame] | 1499 | e2 = IRExpr_Const(IRConst_U16( (UShort)w64 )); |
sewardj | 291a7e8 | 2005-04-27 11:42:44 +0000 | [diff] [blame] | 1500 | break; |
| 1501 | } |
sewardj | 1d8ce20 | 2004-12-13 14:14:16 +0000 | [diff] [blame] | 1502 | case Iop_64to32: { |
| 1503 | ULong w64 = e->Iex.Unop.arg->Iex.Const.con->Ico.U64; |
| 1504 | w64 &= 0x00000000FFFFFFFFULL; |
| 1505 | e2 = IRExpr_Const(IRConst_U32( (UInt)w64 )); |
sewardj | 3701059 | 2004-12-13 10:47:15 +0000 | [diff] [blame] | 1506 | break; |
sewardj | 1d8ce20 | 2004-12-13 14:14:16 +0000 | [diff] [blame] | 1507 | } |
sewardj | 1d8ce20 | 2004-12-13 14:14:16 +0000 | [diff] [blame] | 1508 | case Iop_64HIto32: { |
| 1509 | ULong w64 = e->Iex.Unop.arg->Iex.Const.con->Ico.U64; |
| 1510 | w64 >>= 32; |
| 1511 | e2 = IRExpr_Const(IRConst_U32( (UInt)w64 )); |
| 1512 | break; |
| 1513 | } |
sewardj | b5710b8 | 2005-01-27 16:05:09 +0000 | [diff] [blame] | 1514 | case Iop_32Uto64: |
| 1515 | e2 = IRExpr_Const(IRConst_U64( |
| 1516 | 0xFFFFFFFFULL |
| 1517 | & e->Iex.Unop.arg->Iex.Const.con->Ico.U32)); |
| 1518 | break; |
sewardj | 7f6330d | 2011-04-05 11:06:02 +0000 | [diff] [blame] | 1519 | case Iop_16Sto64: { |
| 1520 | /* signed */ Long s64 = e->Iex.Unop.arg->Iex.Const.con->Ico.U16; |
| 1521 | s64 <<= 48; |
| 1522 | s64 >>= 48; |
| 1523 | e2 = IRExpr_Const(IRConst_U64((ULong)s64)); |
| 1524 | break; |
| 1525 | } |
sewardj | 287e9bb | 2010-07-29 16:12:41 +0000 | [diff] [blame] | 1526 | case Iop_32Sto64: { |
| 1527 | /* signed */ Long s64 = e->Iex.Unop.arg->Iex.Const.con->Ico.U32; |
| 1528 | s64 <<= 32; |
| 1529 | s64 >>= 32; |
| 1530 | e2 = IRExpr_Const(IRConst_U64((ULong)s64)); |
| 1531 | break; |
| 1532 | } |
sewardj | 7f6330d | 2011-04-05 11:06:02 +0000 | [diff] [blame] | 1533 | |
| 1534 | case Iop_16to8: { |
| 1535 | UShort w16 = e->Iex.Unop.arg->Iex.Const.con->Ico.U16; |
| 1536 | w16 &= 0xFF; |
| 1537 | e2 = IRExpr_Const(IRConst_U8( (UChar)w16 )); |
| 1538 | break; |
| 1539 | } |
| 1540 | case Iop_16HIto8: { |
| 1541 | UShort w16 = e->Iex.Unop.arg->Iex.Const.con->Ico.U16; |
| 1542 | w16 >>= 8; |
| 1543 | w16 &= 0xFF; |
| 1544 | e2 = IRExpr_Const(IRConst_U8( (UChar)w16 )); |
| 1545 | break; |
| 1546 | } |
| 1547 | |
sewardj | 0033ddc | 2005-04-26 23:34:34 +0000 | [diff] [blame] | 1548 | case Iop_CmpNEZ8: |
| 1549 | e2 = IRExpr_Const(IRConst_U1(toBool( |
| 1550 | 0 != |
| 1551 | (0xFF & e->Iex.Unop.arg->Iex.Const.con->Ico.U8) |
| 1552 | ))); |
| 1553 | break; |
| 1554 | case Iop_CmpNEZ32: |
| 1555 | e2 = IRExpr_Const(IRConst_U1(toBool( |
| 1556 | 0 != |
| 1557 | (0xFFFFFFFF & e->Iex.Unop.arg->Iex.Const.con->Ico.U32) |
| 1558 | ))); |
| 1559 | break; |
| 1560 | case Iop_CmpNEZ64: |
| 1561 | e2 = IRExpr_Const(IRConst_U1(toBool( |
| 1562 | 0ULL != e->Iex.Unop.arg->Iex.Const.con->Ico.U64 |
| 1563 | ))); |
| 1564 | break; |
| 1565 | |
sewardj | eb17e49 | 2007-08-25 23:07:44 +0000 | [diff] [blame] | 1566 | case Iop_CmpwNEZ32: { |
| 1567 | UInt w32 = e->Iex.Unop.arg->Iex.Const.con->Ico.U32; |
| 1568 | if (w32 == 0) |
| 1569 | e2 = IRExpr_Const(IRConst_U32( 0 )); |
| 1570 | else |
| 1571 | e2 = IRExpr_Const(IRConst_U32( 0xFFFFFFFF )); |
| 1572 | break; |
| 1573 | } |
| 1574 | case Iop_CmpwNEZ64: { |
| 1575 | ULong w64 = e->Iex.Unop.arg->Iex.Const.con->Ico.U64; |
| 1576 | if (w64 == 0) |
| 1577 | e2 = IRExpr_Const(IRConst_U64( 0 )); |
| 1578 | else |
| 1579 | e2 = IRExpr_Const(IRConst_U64( 0xFFFFFFFFFFFFFFFFULL )); |
| 1580 | break; |
| 1581 | } |
| 1582 | |
| 1583 | case Iop_Left32: { |
| 1584 | UInt u32 = e->Iex.Unop.arg->Iex.Const.con->Ico.U32; |
| 1585 | Int s32 = (Int)(u32 & 0xFFFFFFFF); |
| 1586 | s32 = (s32 | (-s32)); |
| 1587 | e2 = IRExpr_Const( IRConst_U32( (UInt)s32 )); |
| 1588 | break; |
| 1589 | } |
| 1590 | |
| 1591 | case Iop_Left64: { |
| 1592 | ULong u64 = e->Iex.Unop.arg->Iex.Const.con->Ico.U64; |
| 1593 | Long s64 = (Long)u64; |
| 1594 | s64 = (s64 | (-s64)); |
| 1595 | e2 = IRExpr_Const( IRConst_U64( (ULong)s64 )); |
| 1596 | break; |
| 1597 | } |
| 1598 | |
sewardj | 4cba9f4 | 2011-03-07 18:34:34 +0000 | [diff] [blame] | 1599 | case Iop_Clz32: { |
| 1600 | UInt u32 = e->Iex.Unop.arg->Iex.Const.con->Ico.U32; |
| 1601 | if (u32 != 0) |
| 1602 | e2 = IRExpr_Const(IRConst_U32(fold_Clz32(u32))); |
| 1603 | break; |
| 1604 | } |
| 1605 | case Iop_Clz64: { |
| 1606 | ULong u64 = e->Iex.Unop.arg->Iex.Const.con->Ico.U64; |
| 1607 | if (u64 != 0ULL) |
| 1608 | e2 = IRExpr_Const(IRConst_U64(fold_Clz64(u64))); |
| 1609 | break; |
| 1610 | } |
| 1611 | |
sewardj | 4a0bee0 | 2012-06-29 14:44:44 +0000 | [diff] [blame] | 1612 | /* For these vector ones, can't fold all cases, but at least |
| 1613 | do the most obvious one. Could do better here using |
| 1614 | summarise/desummarise of vector constants, but too |
| 1615 | difficult to verify; hence just handle the zero cases. */ |
| 1616 | case Iop_32UtoV128: { |
| 1617 | UInt u32 = e->Iex.Unop.arg->Iex.Const.con->Ico.U32; |
| 1618 | if (0 == u32) { |
| 1619 | e2 = IRExpr_Const(IRConst_V128(0x0000)); |
| 1620 | } else { |
| 1621 | goto unhandled; |
| 1622 | } |
| 1623 | break; |
| 1624 | } |
| 1625 | case Iop_V128to64: { |
| 1626 | UShort v128 = e->Iex.Unop.arg->Iex.Const.con->Ico.V128; |
| 1627 | if (0 == ((v128 >> 0) & 0xFF)) { |
| 1628 | e2 = IRExpr_Const(IRConst_U64(0)); |
| 1629 | } else { |
| 1630 | goto unhandled; |
| 1631 | } |
| 1632 | break; |
| 1633 | } |
| 1634 | case Iop_V128HIto64: { |
| 1635 | UShort v128 = e->Iex.Unop.arg->Iex.Const.con->Ico.V128; |
| 1636 | if (0 == ((v128 >> 8) & 0xFF)) { |
| 1637 | e2 = IRExpr_Const(IRConst_U64(0)); |
| 1638 | } else { |
| 1639 | goto unhandled; |
| 1640 | } |
| 1641 | break; |
| 1642 | } |
| 1643 | case Iop_64UtoV128: { |
| 1644 | ULong u64 = e->Iex.Unop.arg->Iex.Const.con->Ico.U64; |
| 1645 | if (0 == u64) { |
| 1646 | e2 = IRExpr_Const(IRConst_V128(0x0000)); |
| 1647 | } else { |
| 1648 | goto unhandled; |
| 1649 | } |
| 1650 | break; |
| 1651 | } |
| 1652 | |
sewardj | ffccf2b | 2012-06-29 15:33:09 +0000 | [diff] [blame] | 1653 | /* Even stupider (although still correct ..) */ |
| 1654 | case Iop_V256to64_0: case Iop_V256to64_1: |
| 1655 | case Iop_V256to64_2: case Iop_V256to64_3: { |
| 1656 | UInt v256 = e->Iex.Unop.arg->Iex.Const.con->Ico.V256; |
| 1657 | if (v256 == 0x00000000) { |
| 1658 | e2 = IRExpr_Const(IRConst_U64(0)); |
| 1659 | } else { |
| 1660 | goto unhandled; |
| 1661 | } |
| 1662 | break; |
| 1663 | } |
| 1664 | |
sewardj | fbe569d | 2014-08-14 22:25:31 +0000 | [diff] [blame] | 1665 | case Iop_ZeroHI64ofV128: { |
| 1666 | /* Could do better here -- only need to look at the bottom 64 bits |
| 1667 | of the argument, really. */ |
| 1668 | UShort v128 = e->Iex.Unop.arg->Iex.Const.con->Ico.V128; |
| 1669 | if (v128 == 0x0000) { |
| 1670 | e2 = IRExpr_Const(IRConst_V128(0x0000)); |
| 1671 | } else { |
| 1672 | goto unhandled; |
| 1673 | } |
| 1674 | break; |
| 1675 | } |
| 1676 | |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 1677 | default: |
| 1678 | goto unhandled; |
| 1679 | } |
florian | 708417d | 2012-02-15 00:43:36 +0000 | [diff] [blame] | 1680 | } |
| 1681 | break; |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 1682 | |
florian | 708417d | 2012-02-15 00:43:36 +0000 | [diff] [blame] | 1683 | case Iex_Binop: |
| 1684 | /* BINARY ops */ |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 1685 | if (e->Iex.Binop.arg1->tag == Iex_Const |
| 1686 | && e->Iex.Binop.arg2->tag == Iex_Const) { |
| 1687 | /* cases where both args are consts */ |
| 1688 | switch (e->Iex.Binop.op) { |
sewardj | e6b3993 | 2004-11-06 17:01:15 +0000 | [diff] [blame] | 1689 | |
sewardj | 855dc72 | 2005-02-17 09:26:05 +0000 | [diff] [blame] | 1690 | /* -- Or -- */ |
sewardj | d999788 | 2004-11-04 19:42:59 +0000 | [diff] [blame] | 1691 | case Iop_Or8: |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1692 | e2 = IRExpr_Const(IRConst_U8(toUChar( |
sewardj | d999788 | 2004-11-04 19:42:59 +0000 | [diff] [blame] | 1693 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U8 |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1694 | | e->Iex.Binop.arg2->Iex.Const.con->Ico.U8)))); |
sewardj | d999788 | 2004-11-04 19:42:59 +0000 | [diff] [blame] | 1695 | break; |
sewardj | e6b3993 | 2004-11-06 17:01:15 +0000 | [diff] [blame] | 1696 | case Iop_Or16: |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1697 | e2 = IRExpr_Const(IRConst_U16(toUShort( |
sewardj | e6b3993 | 2004-11-06 17:01:15 +0000 | [diff] [blame] | 1698 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U16 |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1699 | | e->Iex.Binop.arg2->Iex.Const.con->Ico.U16)))); |
sewardj | e6b3993 | 2004-11-06 17:01:15 +0000 | [diff] [blame] | 1700 | break; |
| 1701 | case Iop_Or32: |
| 1702 | e2 = IRExpr_Const(IRConst_U32( |
| 1703 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U32 |
| 1704 | | e->Iex.Binop.arg2->Iex.Const.con->Ico.U32))); |
| 1705 | break; |
sewardj | f057afb | 2005-02-27 13:35:41 +0000 | [diff] [blame] | 1706 | case Iop_Or64: |
| 1707 | e2 = IRExpr_Const(IRConst_U64( |
| 1708 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U64 |
| 1709 | | e->Iex.Binop.arg2->Iex.Const.con->Ico.U64))); |
| 1710 | break; |
sewardj | 4a0bee0 | 2012-06-29 14:44:44 +0000 | [diff] [blame] | 1711 | case Iop_OrV128: |
| 1712 | e2 = IRExpr_Const(IRConst_V128( |
| 1713 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.V128 |
| 1714 | | e->Iex.Binop.arg2->Iex.Const.con->Ico.V128))); |
| 1715 | break; |
sewardj | e6b3993 | 2004-11-06 17:01:15 +0000 | [diff] [blame] | 1716 | |
sewardj | 855dc72 | 2005-02-17 09:26:05 +0000 | [diff] [blame] | 1717 | /* -- Xor -- */ |
sewardj | 883b00b | 2004-09-11 09:30:24 +0000 | [diff] [blame] | 1718 | case Iop_Xor8: |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1719 | e2 = IRExpr_Const(IRConst_U8(toUChar( |
sewardj | 883b00b | 2004-09-11 09:30:24 +0000 | [diff] [blame] | 1720 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U8 |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1721 | ^ e->Iex.Binop.arg2->Iex.Const.con->Ico.U8)))); |
sewardj | 883b00b | 2004-09-11 09:30:24 +0000 | [diff] [blame] | 1722 | break; |
sewardj | 82c9f2f | 2005-03-02 16:05:13 +0000 | [diff] [blame] | 1723 | case Iop_Xor16: |
sewardj | c7c098f | 2005-03-21 01:06:20 +0000 | [diff] [blame] | 1724 | e2 = IRExpr_Const(IRConst_U16(toUShort( |
sewardj | 82c9f2f | 2005-03-02 16:05:13 +0000 | [diff] [blame] | 1725 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U16 |
sewardj | c7c098f | 2005-03-21 01:06:20 +0000 | [diff] [blame] | 1726 | ^ e->Iex.Binop.arg2->Iex.Const.con->Ico.U16)))); |
sewardj | 82c9f2f | 2005-03-02 16:05:13 +0000 | [diff] [blame] | 1727 | break; |
sewardj | 855dc72 | 2005-02-17 09:26:05 +0000 | [diff] [blame] | 1728 | case Iop_Xor32: |
| 1729 | e2 = IRExpr_Const(IRConst_U32( |
| 1730 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U32 |
| 1731 | ^ e->Iex.Binop.arg2->Iex.Const.con->Ico.U32))); |
| 1732 | break; |
sewardj | f057afb | 2005-02-27 13:35:41 +0000 | [diff] [blame] | 1733 | case Iop_Xor64: |
| 1734 | e2 = IRExpr_Const(IRConst_U64( |
| 1735 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U64 |
| 1736 | ^ e->Iex.Binop.arg2->Iex.Const.con->Ico.U64))); |
| 1737 | break; |
sewardj | 4a0bee0 | 2012-06-29 14:44:44 +0000 | [diff] [blame] | 1738 | case Iop_XorV128: |
| 1739 | e2 = IRExpr_Const(IRConst_V128( |
| 1740 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.V128 |
| 1741 | ^ e->Iex.Binop.arg2->Iex.Const.con->Ico.V128))); |
| 1742 | break; |
sewardj | 855dc72 | 2005-02-17 09:26:05 +0000 | [diff] [blame] | 1743 | |
| 1744 | /* -- And -- */ |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 1745 | case Iop_And8: |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1746 | e2 = IRExpr_Const(IRConst_U8(toUChar( |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 1747 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U8 |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1748 | & e->Iex.Binop.arg2->Iex.Const.con->Ico.U8)))); |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 1749 | break; |
sewardj | 7f6330d | 2011-04-05 11:06:02 +0000 | [diff] [blame] | 1750 | case Iop_And16: |
| 1751 | e2 = IRExpr_Const(IRConst_U16(toUShort( |
| 1752 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U16 |
| 1753 | & e->Iex.Binop.arg2->Iex.Const.con->Ico.U16)))); |
| 1754 | break; |
sewardj | 855dc72 | 2005-02-17 09:26:05 +0000 | [diff] [blame] | 1755 | case Iop_And32: |
| 1756 | e2 = IRExpr_Const(IRConst_U32( |
| 1757 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U32 |
| 1758 | & e->Iex.Binop.arg2->Iex.Const.con->Ico.U32))); |
| 1759 | break; |
| 1760 | case Iop_And64: |
| 1761 | e2 = IRExpr_Const(IRConst_U64( |
| 1762 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U64 |
| 1763 | & e->Iex.Binop.arg2->Iex.Const.con->Ico.U64))); |
| 1764 | break; |
sewardj | 4a0bee0 | 2012-06-29 14:44:44 +0000 | [diff] [blame] | 1765 | case Iop_AndV128: |
| 1766 | e2 = IRExpr_Const(IRConst_V128( |
| 1767 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.V128 |
| 1768 | & e->Iex.Binop.arg2->Iex.Const.con->Ico.V128))); |
| 1769 | break; |
sewardj | 855dc72 | 2005-02-17 09:26:05 +0000 | [diff] [blame] | 1770 | |
| 1771 | /* -- Add -- */ |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 1772 | case Iop_Add8: |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1773 | e2 = IRExpr_Const(IRConst_U8(toUChar( |
sewardj | 4345f7a | 2004-09-22 19:49:27 +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 | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 1776 | break; |
sewardj | 855dc72 | 2005-02-17 09:26:05 +0000 | [diff] [blame] | 1777 | case Iop_Add32: |
| 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; |
| 1782 | case Iop_Add64: |
| 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; |
| 1787 | |
| 1788 | /* -- Sub -- */ |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 1789 | case Iop_Sub8: |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1790 | e2 = IRExpr_Const(IRConst_U8(toUChar( |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 1791 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U8 |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1792 | - e->Iex.Binop.arg2->Iex.Const.con->Ico.U8)))); |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 1793 | break; |
sewardj | d721703 | 2004-08-19 10:49:10 +0000 | [diff] [blame] | 1794 | case Iop_Sub32: |
| 1795 | e2 = IRExpr_Const(IRConst_U32( |
| 1796 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U32 |
| 1797 | - e->Iex.Binop.arg2->Iex.Const.con->Ico.U32))); |
| 1798 | break; |
sewardj | 70a8ddf | 2005-02-13 02:24:26 +0000 | [diff] [blame] | 1799 | case Iop_Sub64: |
| 1800 | e2 = IRExpr_Const(IRConst_U64( |
| 1801 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U64 |
| 1802 | - e->Iex.Binop.arg2->Iex.Const.con->Ico.U64))); |
| 1803 | break; |
sewardj | c2bcb6f | 2005-02-07 00:17:12 +0000 | [diff] [blame] | 1804 | |
sewardj | 478646f | 2008-05-01 20:13:04 +0000 | [diff] [blame] | 1805 | /* -- Max32U -- */ |
| 1806 | case Iop_Max32U: { |
| 1807 | UInt u32a = e->Iex.Binop.arg1->Iex.Const.con->Ico.U32; |
| 1808 | UInt u32b = e->Iex.Binop.arg2->Iex.Const.con->Ico.U32; |
| 1809 | UInt res = u32a > u32b ? u32a : u32b; |
| 1810 | e2 = IRExpr_Const(IRConst_U32(res)); |
| 1811 | break; |
| 1812 | } |
| 1813 | |
sewardj | 855dc72 | 2005-02-17 09:26:05 +0000 | [diff] [blame] | 1814 | /* -- Mul -- */ |
sewardj | b9c5cf6 | 2004-08-24 15:10:38 +0000 | [diff] [blame] | 1815 | case Iop_Mul32: |
| 1816 | e2 = IRExpr_Const(IRConst_U32( |
| 1817 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U32 |
| 1818 | * e->Iex.Binop.arg2->Iex.Const.con->Ico.U32))); |
| 1819 | break; |
sewardj | a34c071 | 2005-03-30 23:19:46 +0000 | [diff] [blame] | 1820 | case Iop_Mul64: |
| 1821 | e2 = IRExpr_Const(IRConst_U64( |
| 1822 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U64 |
| 1823 | * e->Iex.Binop.arg2->Iex.Const.con->Ico.U64))); |
| 1824 | break; |
| 1825 | |
sewardj | ea6bccb | 2005-03-01 10:19:23 +0000 | [diff] [blame] | 1826 | case Iop_MullS32: { |
| 1827 | /* very paranoid */ |
| 1828 | UInt u32a = e->Iex.Binop.arg1->Iex.Const.con->Ico.U32; |
| 1829 | UInt u32b = e->Iex.Binop.arg2->Iex.Const.con->Ico.U32; |
| 1830 | Int s32a = (Int)u32a; |
| 1831 | Int s32b = (Int)u32b; |
| 1832 | Long s64a = (Long)s32a; |
| 1833 | Long s64b = (Long)s32b; |
| 1834 | Long sres = s64a * s64b; |
| 1835 | ULong ures = (ULong)sres; |
| 1836 | e2 = IRExpr_Const(IRConst_U64(ures)); |
| 1837 | break; |
| 1838 | } |
sewardj | b095fba | 2005-02-13 14:13:04 +0000 | [diff] [blame] | 1839 | |
sewardj | 855dc72 | 2005-02-17 09:26:05 +0000 | [diff] [blame] | 1840 | /* -- Shl -- */ |
sewardj | d721703 | 2004-08-19 10:49:10 +0000 | [diff] [blame] | 1841 | case Iop_Shl32: |
sewardj | 6134847 | 2004-08-20 01:01:04 +0000 | [diff] [blame] | 1842 | vassert(e->Iex.Binop.arg2->Iex.Const.con->tag == Ico_U8); |
| 1843 | shift = (Int)(e->Iex.Binop.arg2->Iex.Const.con->Ico.U8); |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 1844 | if (shift >= 0 && shift <= 31) |
sewardj | 278c44c | 2004-08-20 00:28:13 +0000 | [diff] [blame] | 1845 | e2 = IRExpr_Const(IRConst_U32( |
| 1846 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U32 |
| 1847 | << shift))); |
sewardj | d721703 | 2004-08-19 10:49:10 +0000 | [diff] [blame] | 1848 | break; |
sewardj | b095fba | 2005-02-13 14:13:04 +0000 | [diff] [blame] | 1849 | case Iop_Shl64: |
| 1850 | vassert(e->Iex.Binop.arg2->Iex.Const.con->tag == Ico_U8); |
| 1851 | shift = (Int)(e->Iex.Binop.arg2->Iex.Const.con->Ico.U8); |
| 1852 | if (shift >= 0 && shift <= 63) |
| 1853 | e2 = IRExpr_Const(IRConst_U64( |
| 1854 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U64 |
| 1855 | << shift))); |
| 1856 | break; |
| 1857 | |
sewardj | 855dc72 | 2005-02-17 09:26:05 +0000 | [diff] [blame] | 1858 | /* -- Sar -- */ |
sewardj | 278c44c | 2004-08-20 00:28:13 +0000 | [diff] [blame] | 1859 | case Iop_Sar32: { |
| 1860 | /* paranoid ... */ |
| 1861 | /*signed*/ Int s32; |
sewardj | 6134847 | 2004-08-20 01:01:04 +0000 | [diff] [blame] | 1862 | vassert(e->Iex.Binop.arg2->Iex.Const.con->tag == Ico_U8); |
sewardj | 278c44c | 2004-08-20 00:28:13 +0000 | [diff] [blame] | 1863 | s32 = (Int)(e->Iex.Binop.arg1->Iex.Const.con->Ico.U32); |
sewardj | 6134847 | 2004-08-20 01:01:04 +0000 | [diff] [blame] | 1864 | shift = (Int)(e->Iex.Binop.arg2->Iex.Const.con->Ico.U8); |
sewardj | 278c44c | 2004-08-20 00:28:13 +0000 | [diff] [blame] | 1865 | if (shift >= 0 && shift <= 31) { |
| 1866 | s32 >>=/*signed*/ shift; |
| 1867 | e2 = IRExpr_Const(IRConst_U32((UInt)s32)); |
| 1868 | } |
| 1869 | break; |
| 1870 | } |
sewardj | 855dc72 | 2005-02-17 09:26:05 +0000 | [diff] [blame] | 1871 | case Iop_Sar64: { |
| 1872 | /* paranoid ... */ |
| 1873 | /*signed*/ Long s64; |
| 1874 | vassert(e->Iex.Binop.arg2->Iex.Const.con->tag == Ico_U8); |
| 1875 | s64 = (Long)(e->Iex.Binop.arg1->Iex.Const.con->Ico.U64); |
| 1876 | shift = (Int)(e->Iex.Binop.arg2->Iex.Const.con->Ico.U8); |
| 1877 | if (shift >= 0 && shift <= 63) { |
| 1878 | s64 >>=/*signed*/ shift; |
| 1879 | e2 = IRExpr_Const(IRConst_U64((ULong)s64)); |
| 1880 | } |
| 1881 | break; |
| 1882 | } |
| 1883 | |
| 1884 | /* -- Shr -- */ |
sewardj | 6134847 | 2004-08-20 01:01:04 +0000 | [diff] [blame] | 1885 | case Iop_Shr32: { |
| 1886 | /* paranoid ... */ |
sewardj | 4add714 | 2005-02-21 08:20:22 +0000 | [diff] [blame] | 1887 | /*unsigned*/ UInt u32; |
sewardj | 6134847 | 2004-08-20 01:01:04 +0000 | [diff] [blame] | 1888 | vassert(e->Iex.Binop.arg2->Iex.Const.con->tag == Ico_U8); |
sewardj | 4add714 | 2005-02-21 08:20:22 +0000 | [diff] [blame] | 1889 | u32 = (UInt)(e->Iex.Binop.arg1->Iex.Const.con->Ico.U32); |
sewardj | 6134847 | 2004-08-20 01:01:04 +0000 | [diff] [blame] | 1890 | shift = (Int)(e->Iex.Binop.arg2->Iex.Const.con->Ico.U8); |
| 1891 | if (shift >= 0 && shift <= 31) { |
sewardj | 4add714 | 2005-02-21 08:20:22 +0000 | [diff] [blame] | 1892 | u32 >>=/*unsigned*/ shift; |
| 1893 | e2 = IRExpr_Const(IRConst_U32(u32)); |
| 1894 | } |
| 1895 | break; |
| 1896 | } |
| 1897 | case Iop_Shr64: { |
| 1898 | /* paranoid ... */ |
| 1899 | /*unsigned*/ ULong u64; |
| 1900 | vassert(e->Iex.Binop.arg2->Iex.Const.con->tag == Ico_U8); |
| 1901 | u64 = (ULong)(e->Iex.Binop.arg1->Iex.Const.con->Ico.U64); |
| 1902 | shift = (Int)(e->Iex.Binop.arg2->Iex.Const.con->Ico.U8); |
| 1903 | if (shift >= 0 && shift <= 63) { |
| 1904 | u64 >>=/*unsigned*/ shift; |
| 1905 | e2 = IRExpr_Const(IRConst_U64(u64)); |
sewardj | 6134847 | 2004-08-20 01:01:04 +0000 | [diff] [blame] | 1906 | } |
| 1907 | break; |
| 1908 | } |
sewardj | 855dc72 | 2005-02-17 09:26:05 +0000 | [diff] [blame] | 1909 | |
| 1910 | /* -- CmpEQ -- */ |
sewardj | b8e7586 | 2004-08-19 17:58:45 +0000 | [diff] [blame] | 1911 | case Iop_CmpEQ32: |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1912 | e2 = IRExpr_Const(IRConst_U1(toBool( |
sewardj | b8e7586 | 2004-08-19 17:58:45 +0000 | [diff] [blame] | 1913 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U32 |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1914 | == e->Iex.Binop.arg2->Iex.Const.con->Ico.U32)))); |
sewardj | b8e7586 | 2004-08-19 17:58:45 +0000 | [diff] [blame] | 1915 | break; |
sewardj | 855dc72 | 2005-02-17 09:26:05 +0000 | [diff] [blame] | 1916 | case Iop_CmpEQ64: |
| 1917 | e2 = IRExpr_Const(IRConst_U1(toBool( |
| 1918 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U64 |
| 1919 | == e->Iex.Binop.arg2->Iex.Const.con->Ico.U64)))); |
| 1920 | break; |
| 1921 | |
| 1922 | /* -- CmpNE -- */ |
| 1923 | case Iop_CmpNE8: |
sewardj | e13074c | 2012-11-08 10:57:08 +0000 | [diff] [blame] | 1924 | case Iop_CasCmpNE8: |
| 1925 | case Iop_ExpCmpNE8: |
sewardj | 855dc72 | 2005-02-17 09:26:05 +0000 | [diff] [blame] | 1926 | e2 = IRExpr_Const(IRConst_U1(toBool( |
| 1927 | ((0xFF & e->Iex.Binop.arg1->Iex.Const.con->Ico.U8) |
| 1928 | != (0xFF & e->Iex.Binop.arg2->Iex.Const.con->Ico.U8))))); |
| 1929 | break; |
sewardj | ae27ab6 | 2004-10-15 21:21:46 +0000 | [diff] [blame] | 1930 | case Iop_CmpNE32: |
sewardj | e13074c | 2012-11-08 10:57:08 +0000 | [diff] [blame] | 1931 | case Iop_CasCmpNE32: |
| 1932 | case Iop_ExpCmpNE32: |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1933 | e2 = IRExpr_Const(IRConst_U1(toBool( |
sewardj | ae27ab6 | 2004-10-15 21:21:46 +0000 | [diff] [blame] | 1934 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U32 |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1935 | != e->Iex.Binop.arg2->Iex.Const.con->Ico.U32)))); |
sewardj | ae27ab6 | 2004-10-15 21:21:46 +0000 | [diff] [blame] | 1936 | break; |
sewardj | e6b3993 | 2004-11-06 17:01:15 +0000 | [diff] [blame] | 1937 | case Iop_CmpNE64: |
sewardj | e13074c | 2012-11-08 10:57:08 +0000 | [diff] [blame] | 1938 | case Iop_CasCmpNE64: |
| 1939 | case Iop_ExpCmpNE64: |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1940 | e2 = IRExpr_Const(IRConst_U1(toBool( |
sewardj | e6b3993 | 2004-11-06 17:01:15 +0000 | [diff] [blame] | 1941 | (e->Iex.Binop.arg1->Iex.Const.con->Ico.U64 |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1942 | != e->Iex.Binop.arg2->Iex.Const.con->Ico.U64)))); |
sewardj | e6b3993 | 2004-11-06 17:01:15 +0000 | [diff] [blame] | 1943 | break; |
| 1944 | |
sewardj | 855dc72 | 2005-02-17 09:26:05 +0000 | [diff] [blame] | 1945 | /* -- CmpLEU -- */ |
sewardj | 7447b5b | 2004-10-16 11:30:42 +0000 | [diff] [blame] | 1946 | case Iop_CmpLE32U: |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1947 | e2 = IRExpr_Const(IRConst_U1(toBool( |
sewardj | 7447b5b | 2004-10-16 11:30:42 +0000 | [diff] [blame] | 1948 | ((UInt)(e->Iex.Binop.arg1->Iex.Const.con->Ico.U32) |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1949 | <= (UInt)(e->Iex.Binop.arg2->Iex.Const.con->Ico.U32))))); |
sewardj | 7447b5b | 2004-10-16 11:30:42 +0000 | [diff] [blame] | 1950 | break; |
sewardj | 7f6330d | 2011-04-05 11:06:02 +0000 | [diff] [blame] | 1951 | case Iop_CmpLE64U: |
| 1952 | e2 = IRExpr_Const(IRConst_U1(toBool( |
| 1953 | ((ULong)(e->Iex.Binop.arg1->Iex.Const.con->Ico.U64) |
| 1954 | <= (ULong)(e->Iex.Binop.arg2->Iex.Const.con->Ico.U64))))); |
| 1955 | break; |
sewardj | 855dc72 | 2005-02-17 09:26:05 +0000 | [diff] [blame] | 1956 | |
| 1957 | /* -- CmpLES -- */ |
sewardj | 088e4f7 | 2004-10-19 01:25:02 +0000 | [diff] [blame] | 1958 | case Iop_CmpLE32S: |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1959 | e2 = IRExpr_Const(IRConst_U1(toBool( |
sewardj | 088e4f7 | 2004-10-19 01:25:02 +0000 | [diff] [blame] | 1960 | ((Int)(e->Iex.Binop.arg1->Iex.Const.con->Ico.U32) |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1961 | <= (Int)(e->Iex.Binop.arg2->Iex.Const.con->Ico.U32))))); |
sewardj | 088e4f7 | 2004-10-19 01:25:02 +0000 | [diff] [blame] | 1962 | break; |
sewardj | 7f6330d | 2011-04-05 11:06:02 +0000 | [diff] [blame] | 1963 | case Iop_CmpLE64S: |
| 1964 | e2 = IRExpr_Const(IRConst_U1(toBool( |
| 1965 | ((Long)(e->Iex.Binop.arg1->Iex.Const.con->Ico.U64) |
| 1966 | <= (Long)(e->Iex.Binop.arg2->Iex.Const.con->Ico.U64))))); |
| 1967 | break; |
sewardj | e1d45da | 2004-11-12 00:13:21 +0000 | [diff] [blame] | 1968 | |
sewardj | 855dc72 | 2005-02-17 09:26:05 +0000 | [diff] [blame] | 1969 | /* -- CmpLTS -- */ |
sewardj | 9bdd265 | 2004-10-19 12:56:33 +0000 | [diff] [blame] | 1970 | case Iop_CmpLT32S: |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1971 | e2 = IRExpr_Const(IRConst_U1(toBool( |
sewardj | 9bdd265 | 2004-10-19 12:56:33 +0000 | [diff] [blame] | 1972 | ((Int)(e->Iex.Binop.arg1->Iex.Const.con->Ico.U32) |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1973 | < (Int)(e->Iex.Binop.arg2->Iex.Const.con->Ico.U32))))); |
sewardj | 9bdd265 | 2004-10-19 12:56:33 +0000 | [diff] [blame] | 1974 | break; |
sewardj | 7f6330d | 2011-04-05 11:06:02 +0000 | [diff] [blame] | 1975 | case Iop_CmpLT64S: |
| 1976 | e2 = IRExpr_Const(IRConst_U1(toBool( |
| 1977 | ((Long)(e->Iex.Binop.arg1->Iex.Const.con->Ico.U64) |
| 1978 | < (Long)(e->Iex.Binop.arg2->Iex.Const.con->Ico.U64))))); |
| 1979 | break; |
sewardj | 855dc72 | 2005-02-17 09:26:05 +0000 | [diff] [blame] | 1980 | |
| 1981 | /* -- CmpLTU -- */ |
sewardj | e1d45da | 2004-11-12 00:13:21 +0000 | [diff] [blame] | 1982 | case Iop_CmpLT32U: |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1983 | e2 = IRExpr_Const(IRConst_U1(toBool( |
sewardj | e1d45da | 2004-11-12 00:13:21 +0000 | [diff] [blame] | 1984 | ((UInt)(e->Iex.Binop.arg1->Iex.Const.con->Ico.U32) |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 1985 | < (UInt)(e->Iex.Binop.arg2->Iex.Const.con->Ico.U32))))); |
sewardj | e1d45da | 2004-11-12 00:13:21 +0000 | [diff] [blame] | 1986 | break; |
sewardj | 7f6330d | 2011-04-05 11:06:02 +0000 | [diff] [blame] | 1987 | case Iop_CmpLT64U: |
| 1988 | e2 = IRExpr_Const(IRConst_U1(toBool( |
| 1989 | ((ULong)(e->Iex.Binop.arg1->Iex.Const.con->Ico.U64) |
| 1990 | < (ULong)(e->Iex.Binop.arg2->Iex.Const.con->Ico.U64))))); |
| 1991 | break; |
sewardj | 7447b5b | 2004-10-16 11:30:42 +0000 | [diff] [blame] | 1992 | |
sewardj | b51f0f4 | 2005-07-18 11:38:02 +0000 | [diff] [blame] | 1993 | /* -- CmpORD -- */ |
| 1994 | case Iop_CmpORD32S: { |
| 1995 | /* very paranoid */ |
| 1996 | UInt u32a = e->Iex.Binop.arg1->Iex.Const.con->Ico.U32; |
| 1997 | UInt u32b = e->Iex.Binop.arg2->Iex.Const.con->Ico.U32; |
| 1998 | Int s32a = (Int)u32a; |
| 1999 | Int s32b = (Int)u32b; |
| 2000 | Int r = 0x2; /* EQ */ |
| 2001 | if (s32a < s32b) { |
| 2002 | r = 0x8; /* LT */ |
| 2003 | } |
| 2004 | else if (s32a > s32b) { |
| 2005 | r = 0x4; /* GT */ |
| 2006 | } |
| 2007 | e2 = IRExpr_Const(IRConst_U32(r)); |
| 2008 | break; |
| 2009 | } |
| 2010 | |
sewardj | 855dc72 | 2005-02-17 09:26:05 +0000 | [diff] [blame] | 2011 | /* -- nHLto2n -- */ |
sewardj | 088bcb4 | 2004-08-19 17:16:52 +0000 | [diff] [blame] | 2012 | case Iop_32HLto64: |
| 2013 | e2 = IRExpr_Const(IRConst_U64( |
sewardj | 4a0bee0 | 2012-06-29 14:44:44 +0000 | [diff] [blame] | 2014 | (((ULong)(e->Iex.Binop.arg1 |
| 2015 | ->Iex.Const.con->Ico.U32)) << 32) |
sewardj | 088bcb4 | 2004-08-19 17:16:52 +0000 | [diff] [blame] | 2016 | | ((ULong)(e->Iex.Binop.arg2->Iex.Const.con->Ico.U32)) |
| 2017 | )); |
| 2018 | break; |
sewardj | 3bc8a59 | 2005-05-02 10:47:22 +0000 | [diff] [blame] | 2019 | case Iop_64HLto128: |
| 2020 | /* We can't fold this, because there is no way to |
| 2021 | express he result in IR, but at least pretend to |
| 2022 | handle it, so as to stop getting blasted with |
| 2023 | no-rule-for-this-primop messages. */ |
| 2024 | break; |
sewardj | 4a0bee0 | 2012-06-29 14:44:44 +0000 | [diff] [blame] | 2025 | /* For this vector one, can't fold all cases, but at |
| 2026 | least do the most obvious one. Could do better here |
| 2027 | using summarise/desummarise of vector constants, but |
| 2028 | too difficult to verify; hence just handle the zero |
| 2029 | cases. */ |
| 2030 | case Iop_64HLtoV128: { |
| 2031 | ULong argHi = e->Iex.Binop.arg1->Iex.Const.con->Ico.U64; |
| 2032 | ULong argLo = e->Iex.Binop.arg2->Iex.Const.con->Ico.U64; |
| 2033 | if (0 == argHi && 0 == argLo) { |
| 2034 | e2 = IRExpr_Const(IRConst_V128(0)); |
| 2035 | } else { |
| 2036 | goto unhandled; |
| 2037 | } |
| 2038 | break; |
| 2039 | } |
sewardj | 9571dc0 | 2014-01-26 18:34:23 +0000 | [diff] [blame] | 2040 | /* Same reasoning for the 256-bit version. */ |
| 2041 | case Iop_V128HLtoV256: { |
| 2042 | IRExpr* argHi = e->Iex.Binop.arg1; |
| 2043 | IRExpr* argLo = e->Iex.Binop.arg2; |
| 2044 | if (isZeroV128(argHi) && isZeroV128(argLo)) { |
| 2045 | e2 = IRExpr_Const(IRConst_V256(0)); |
| 2046 | } else { |
| 2047 | goto unhandled; |
| 2048 | } |
| 2049 | break; |
| 2050 | } |
sewardj | 4a0bee0 | 2012-06-29 14:44:44 +0000 | [diff] [blame] | 2051 | |
| 2052 | /* -- V128 stuff -- */ |
| 2053 | case Iop_InterleaveLO8x16: { |
| 2054 | /* This turns up a lot in Memcheck instrumentation of |
| 2055 | Icc generated code. I don't know why. */ |
| 2056 | UShort arg1 = e->Iex.Binop.arg1->Iex.Const.con->Ico.V128; |
| 2057 | UShort arg2 = e->Iex.Binop.arg2->Iex.Const.con->Ico.V128; |
| 2058 | if (0 == arg1 && 0 == arg2) { |
| 2059 | e2 = IRExpr_Const(IRConst_V128(0)); |
| 2060 | } else { |
| 2061 | goto unhandled; |
| 2062 | } |
| 2063 | break; |
| 2064 | } |
sewardj | 855dc72 | 2005-02-17 09:26:05 +0000 | [diff] [blame] | 2065 | |
sewardj | 607dd4f | 2004-09-08 18:20:19 +0000 | [diff] [blame] | 2066 | default: |
| 2067 | goto unhandled; |
sewardj | d721703 | 2004-08-19 10:49:10 +0000 | [diff] [blame] | 2068 | } |
sewardj | f672901 | 2004-08-25 12:45:13 +0000 | [diff] [blame] | 2069 | |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 2070 | } else { |
sewardj | f672901 | 2004-08-25 12:45:13 +0000 | [diff] [blame] | 2071 | |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 2072 | /* other cases (identities, etc) */ |
sewardj | 64d776c | 2010-10-01 14:06:22 +0000 | [diff] [blame] | 2073 | switch (e->Iex.Binop.op) { |
florian | f6402ab | 2012-01-29 02:19:43 +0000 | [diff] [blame] | 2074 | |
| 2075 | case Iop_Shl32: |
| 2076 | case Iop_Shl64: |
| 2077 | case Iop_Shr64: |
sewardj | b979d7a | 2014-10-02 16:13:20 +0000 | [diff] [blame] | 2078 | case Iop_Sar64: |
| 2079 | /* Shl32/Shl64/Shr64/Sar64(x,0) ==> x */ |
florian | f6402ab | 2012-01-29 02:19:43 +0000 | [diff] [blame] | 2080 | if (isZeroU(e->Iex.Binop.arg2)) { |
sewardj | 64d776c | 2010-10-01 14:06:22 +0000 | [diff] [blame] | 2081 | e2 = e->Iex.Binop.arg1; |
florian | f6402ab | 2012-01-29 02:19:43 +0000 | [diff] [blame] | 2082 | break; |
| 2083 | } |
| 2084 | /* Shl32/Shl64/Shr64(0,x) ==> 0 */ |
| 2085 | if (isZeroU(e->Iex.Binop.arg1)) { |
| 2086 | e2 = e->Iex.Binop.arg1; |
| 2087 | break; |
| 2088 | } |
sewardj | 64d776c | 2010-10-01 14:06:22 +0000 | [diff] [blame] | 2089 | break; |
sewardj | f672901 | 2004-08-25 12:45:13 +0000 | [diff] [blame] | 2090 | |
sewardj | b979d7a | 2014-10-02 16:13:20 +0000 | [diff] [blame] | 2091 | case Iop_Sar32: |
florian | f6402ab | 2012-01-29 02:19:43 +0000 | [diff] [blame] | 2092 | case Iop_Shr32: |
sewardj | b979d7a | 2014-10-02 16:13:20 +0000 | [diff] [blame] | 2093 | /* Shr32/Sar32(x,0) ==> x */ |
florian | f6402ab | 2012-01-29 02:19:43 +0000 | [diff] [blame] | 2094 | if (isZeroU(e->Iex.Binop.arg2)) { |
| 2095 | e2 = e->Iex.Binop.arg1; |
| 2096 | break; |
| 2097 | } |
| 2098 | break; |
| 2099 | |
| 2100 | case Iop_Or8: |
| 2101 | case Iop_Or16: |
| 2102 | case Iop_Or32: |
| 2103 | case Iop_Or64: |
| 2104 | case Iop_Max32U: |
sewardj | cf4be4a | 2012-03-26 09:44:39 +0000 | [diff] [blame] | 2105 | /* Or8/Or16/Or32/Or64/Max32U(x,0) ==> x */ |
florian | f6402ab | 2012-01-29 02:19:43 +0000 | [diff] [blame] | 2106 | if (isZeroU(e->Iex.Binop.arg2)) { |
| 2107 | e2 = e->Iex.Binop.arg1; |
| 2108 | break; |
| 2109 | } |
sewardj | cf4be4a | 2012-03-26 09:44:39 +0000 | [diff] [blame] | 2110 | /* Or8/Or16/Or32/Or64/Max32U(0,x) ==> x */ |
florian | f6402ab | 2012-01-29 02:19:43 +0000 | [diff] [blame] | 2111 | if (isZeroU(e->Iex.Binop.arg1)) { |
| 2112 | e2 = e->Iex.Binop.arg2; |
| 2113 | break; |
| 2114 | } |
sewardj | cf4be4a | 2012-03-26 09:44:39 +0000 | [diff] [blame] | 2115 | /* Or8/Or16/Or32/Or64/Max32U(x,1---1b) ==> 1---1b */ |
| 2116 | /* Or8/Or16/Or32/Or64/Max32U(1---1b,x) ==> 1---1b */ |
| 2117 | if (isOnesU(e->Iex.Binop.arg1) || isOnesU(e->Iex.Binop.arg2)) { |
| 2118 | e2 = mkOnesOfPrimopResultType(e->Iex.Binop.op); |
| 2119 | break; |
| 2120 | } |
| 2121 | /* Or8/Or16/Or32/Or64/Max32U(t,t) ==> t, for some IRTemp t */ |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 2122 | if (sameIRExprs(env, e->Iex.Binop.arg1, e->Iex.Binop.arg2)) { |
florian | f6402ab | 2012-01-29 02:19:43 +0000 | [diff] [blame] | 2123 | e2 = e->Iex.Binop.arg1; |
| 2124 | break; |
| 2125 | } |
| 2126 | break; |
| 2127 | |
| 2128 | case Iop_Add8: |
| 2129 | /* Add8(t,t) ==> t << 1. |
| 2130 | Memcheck doesn't understand that |
| 2131 | x+x produces a defined least significant bit, and it seems |
| 2132 | simplest just to get rid of the problem by rewriting it |
| 2133 | out, since the opportunity to do so exists. */ |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 2134 | if (sameIRExprs(env, e->Iex.Binop.arg1, e->Iex.Binop.arg2)) { |
florian | f6402ab | 2012-01-29 02:19:43 +0000 | [diff] [blame] | 2135 | e2 = IRExpr_Binop(Iop_Shl8, e->Iex.Binop.arg1, |
| 2136 | IRExpr_Const(IRConst_U8(1))); |
| 2137 | break; |
| 2138 | } |
| 2139 | break; |
| 2140 | |
| 2141 | /* NB no Add16(t,t) case yet as no known test case exists */ |
| 2142 | |
| 2143 | case Iop_Add32: |
| 2144 | case Iop_Add64: |
| 2145 | /* Add32/Add64(x,0) ==> x */ |
| 2146 | if (isZeroU(e->Iex.Binop.arg2)) { |
| 2147 | e2 = e->Iex.Binop.arg1; |
| 2148 | break; |
| 2149 | } |
| 2150 | /* Add32/Add64(0,x) ==> x */ |
| 2151 | if (isZeroU(e->Iex.Binop.arg1)) { |
| 2152 | e2 = e->Iex.Binop.arg2; |
| 2153 | break; |
| 2154 | } |
| 2155 | /* Add32/Add64(t,t) ==> t << 1. Same rationale as for Add8. */ |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 2156 | if (sameIRExprs(env, e->Iex.Binop.arg1, e->Iex.Binop.arg2)) { |
sewardj | 6399f81 | 2012-06-29 15:36:44 +0000 | [diff] [blame] | 2157 | e2 = IRExpr_Binop( |
| 2158 | e->Iex.Binop.op == Iop_Add32 ? Iop_Shl32 : Iop_Shl64, |
| 2159 | e->Iex.Binop.arg1, IRExpr_Const(IRConst_U8(1))); |
florian | f6402ab | 2012-01-29 02:19:43 +0000 | [diff] [blame] | 2160 | break; |
| 2161 | } |
| 2162 | break; |
| 2163 | |
sewardj | 1beb433 | 2012-12-19 15:28:43 +0000 | [diff] [blame] | 2164 | case Iop_Sub32: |
florian | f6402ab | 2012-01-29 02:19:43 +0000 | [diff] [blame] | 2165 | case Iop_Sub64: |
sewardj | 1beb433 | 2012-12-19 15:28:43 +0000 | [diff] [blame] | 2166 | /* Sub32/Sub64(x,0) ==> x */ |
| 2167 | if (isZeroU(e->Iex.Binop.arg2)) { |
florian | f6402ab | 2012-01-29 02:19:43 +0000 | [diff] [blame] | 2168 | e2 = e->Iex.Binop.arg1; |
| 2169 | break; |
| 2170 | } |
sewardj | 1beb433 | 2012-12-19 15:28:43 +0000 | [diff] [blame] | 2171 | /* Sub32/Sub64(t,t) ==> 0, for some IRTemp t */ |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 2172 | if (sameIRExprs(env, e->Iex.Binop.arg1, e->Iex.Binop.arg2)) { |
sewardj | 64d776c | 2010-10-01 14:06:22 +0000 | [diff] [blame] | 2173 | e2 = mkZeroOfPrimopResultType(e->Iex.Binop.op); |
florian | f6402ab | 2012-01-29 02:19:43 +0000 | [diff] [blame] | 2174 | break; |
| 2175 | } |
sewardj | 64d776c | 2010-10-01 14:06:22 +0000 | [diff] [blame] | 2176 | break; |
sewardj | 36a911a | 2014-04-03 13:48:21 +0000 | [diff] [blame] | 2177 | case Iop_Sub8x16: |
| 2178 | /* Sub8x16(x,0) ==> x */ |
| 2179 | if (isZeroV128(e->Iex.Binop.arg2)) { |
| 2180 | e2 = e->Iex.Binop.arg1; |
| 2181 | break; |
| 2182 | } |
| 2183 | break; |
sewardj | 64d776c | 2010-10-01 14:06:22 +0000 | [diff] [blame] | 2184 | |
florian | 2782d21 | 2014-07-18 21:23:46 +0000 | [diff] [blame] | 2185 | case Iop_And8: |
| 2186 | case Iop_And16: |
florian | f6402ab | 2012-01-29 02:19:43 +0000 | [diff] [blame] | 2187 | case Iop_And32: |
florian | 2782d21 | 2014-07-18 21:23:46 +0000 | [diff] [blame] | 2188 | case Iop_And64: |
| 2189 | /* And8/And16/And32/And64(x,1---1b) ==> x */ |
florian | 55ce7b7 | 2013-09-01 14:22:05 +0000 | [diff] [blame] | 2190 | if (isOnesU(e->Iex.Binop.arg2)) { |
florian | f6402ab | 2012-01-29 02:19:43 +0000 | [diff] [blame] | 2191 | e2 = e->Iex.Binop.arg1; |
| 2192 | break; |
| 2193 | } |
florian | 2782d21 | 2014-07-18 21:23:46 +0000 | [diff] [blame] | 2194 | /* And8/And16/And32/And64(1---1b,x) ==> x */ |
| 2195 | if (isOnesU(e->Iex.Binop.arg1)) { |
| 2196 | e2 = e->Iex.Binop.arg2; |
| 2197 | break; |
| 2198 | } |
| 2199 | /* And8/And16/And32/And64(x,0) ==> 0 */ |
florian | 55ce7b7 | 2013-09-01 14:22:05 +0000 | [diff] [blame] | 2200 | if (isZeroU(e->Iex.Binop.arg2)) { |
florian | f6402ab | 2012-01-29 02:19:43 +0000 | [diff] [blame] | 2201 | e2 = e->Iex.Binop.arg2; |
| 2202 | break; |
| 2203 | } |
florian | 2782d21 | 2014-07-18 21:23:46 +0000 | [diff] [blame] | 2204 | /* And8/And16/And32/And64(0,x) ==> 0 */ |
florian | 55ce7b7 | 2013-09-01 14:22:05 +0000 | [diff] [blame] | 2205 | if (isZeroU(e->Iex.Binop.arg1)) { |
florian | f6402ab | 2012-01-29 02:19:43 +0000 | [diff] [blame] | 2206 | e2 = e->Iex.Binop.arg1; |
| 2207 | break; |
| 2208 | } |
florian | 2782d21 | 2014-07-18 21:23:46 +0000 | [diff] [blame] | 2209 | /* And8/And16/And32/And64(t,t) ==> t, for some IRTemp t */ |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 2210 | if (sameIRExprs(env, e->Iex.Binop.arg1, e->Iex.Binop.arg2)) { |
florian | f6402ab | 2012-01-29 02:19:43 +0000 | [diff] [blame] | 2211 | e2 = e->Iex.Binop.arg1; |
| 2212 | break; |
| 2213 | } |
| 2214 | break; |
| 2215 | |
sewardj | 4a0bee0 | 2012-06-29 14:44:44 +0000 | [diff] [blame] | 2216 | case Iop_AndV128: |
| 2217 | case Iop_AndV256: |
florian | 55ce7b7 | 2013-09-01 14:22:05 +0000 | [diff] [blame] | 2218 | /* And8/And16/AndV128/AndV256(t,t) |
sewardj | 4a0bee0 | 2012-06-29 14:44:44 +0000 | [diff] [blame] | 2219 | ==> t, for some IRTemp t */ |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 2220 | if (sameIRExprs(env, e->Iex.Binop.arg1, e->Iex.Binop.arg2)) { |
florian | f6402ab | 2012-01-29 02:19:43 +0000 | [diff] [blame] | 2221 | e2 = e->Iex.Binop.arg1; |
| 2222 | break; |
| 2223 | } |
sewardj | 36a911a | 2014-04-03 13:48:21 +0000 | [diff] [blame] | 2224 | /* Deal with either arg zero. Could handle other And |
| 2225 | cases here too. */ |
florian | 1b7c471 | 2014-07-16 20:17:49 +0000 | [diff] [blame] | 2226 | if (e->Iex.Binop.op == Iop_AndV256 |
| 2227 | && (isZeroV256(e->Iex.Binop.arg1) |
| 2228 | || isZeroV256(e->Iex.Binop.arg2))) { |
sewardj | bbcf188 | 2014-01-12 12:49:10 +0000 | [diff] [blame] | 2229 | e2 = mkZeroOfPrimopResultType(e->Iex.Binop.op); |
| 2230 | break; |
sewardj | 36a911a | 2014-04-03 13:48:21 +0000 | [diff] [blame] | 2231 | } else if (e->Iex.Binop.op == Iop_AndV128 |
| 2232 | && (isZeroV128(e->Iex.Binop.arg1) |
| 2233 | || isZeroV128(e->Iex.Binop.arg2))) { |
| 2234 | e2 = mkZeroOfPrimopResultType(e->Iex.Binop.op); |
| 2235 | break; |
sewardj | bbcf188 | 2014-01-12 12:49:10 +0000 | [diff] [blame] | 2236 | } |
florian | f6402ab | 2012-01-29 02:19:43 +0000 | [diff] [blame] | 2237 | break; |
| 2238 | |
| 2239 | case Iop_OrV128: |
sewardj | 4a0bee0 | 2012-06-29 14:44:44 +0000 | [diff] [blame] | 2240 | case Iop_OrV256: |
| 2241 | /* V128/V256(t,t) ==> t, for some IRTemp t */ |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 2242 | if (sameIRExprs(env, e->Iex.Binop.arg1, e->Iex.Binop.arg2)) { |
florian | f6402ab | 2012-01-29 02:19:43 +0000 | [diff] [blame] | 2243 | e2 = e->Iex.Binop.arg1; |
| 2244 | break; |
| 2245 | } |
sewardj | 9571dc0 | 2014-01-26 18:34:23 +0000 | [diff] [blame] | 2246 | /* OrV128(t,0) ==> t */ |
| 2247 | if (e->Iex.Binop.op == Iop_OrV128) { |
| 2248 | if (isZeroV128(e->Iex.Binop.arg2)) { |
| 2249 | e2 = e->Iex.Binop.arg1; |
| 2250 | break; |
| 2251 | } |
| 2252 | if (isZeroV128(e->Iex.Binop.arg1)) { |
| 2253 | e2 = e->Iex.Binop.arg2; |
| 2254 | break; |
| 2255 | } |
| 2256 | } |
| 2257 | /* OrV256(t,0) ==> t */ |
| 2258 | if (e->Iex.Binop.op == Iop_OrV256) { |
| 2259 | if (isZeroV256(e->Iex.Binop.arg2)) { |
| 2260 | e2 = e->Iex.Binop.arg1; |
| 2261 | break; |
| 2262 | } |
| 2263 | //Disabled because there's no known test case right now. |
| 2264 | //if (isZeroV256(e->Iex.Binop.arg1)) { |
| 2265 | // e2 = e->Iex.Binop.arg2; |
| 2266 | // break; |
| 2267 | //} |
| 2268 | } |
florian | f6402ab | 2012-01-29 02:19:43 +0000 | [diff] [blame] | 2269 | break; |
| 2270 | |
| 2271 | case Iop_Xor8: |
| 2272 | case Iop_Xor16: |
| 2273 | case Iop_Xor32: |
| 2274 | case Iop_Xor64: |
| 2275 | case Iop_XorV128: |
| 2276 | /* Xor8/16/32/64/V128(t,t) ==> 0, for some IRTemp t */ |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 2277 | if (sameIRExprs(env, e->Iex.Binop.arg1, e->Iex.Binop.arg2)) { |
florian | f6402ab | 2012-01-29 02:19:43 +0000 | [diff] [blame] | 2278 | e2 = mkZeroOfPrimopResultType(e->Iex.Binop.op); |
| 2279 | break; |
| 2280 | } |
sewardj | b01ff40 | 2014-08-04 08:09:23 +0000 | [diff] [blame] | 2281 | /* XorV128(t,0) ==> t */ |
| 2282 | if (e->Iex.Binop.op == Iop_XorV128) { |
| 2283 | if (isZeroV128(e->Iex.Binop.arg2)) { |
| 2284 | e2 = e->Iex.Binop.arg1; |
| 2285 | break; |
| 2286 | } |
| 2287 | //Disabled because there's no known test case right now. |
| 2288 | //if (isZeroV128(e->Iex.Binop.arg1)) { |
| 2289 | // e2 = e->Iex.Binop.arg2; |
| 2290 | // break; |
| 2291 | //} |
| 2292 | } |
florian | f6402ab | 2012-01-29 02:19:43 +0000 | [diff] [blame] | 2293 | break; |
| 2294 | |
sewardj | a7e9638 | 2012-06-29 16:26:17 +0000 | [diff] [blame] | 2295 | case Iop_CmpNE32: |
sewardj | 1beb433 | 2012-12-19 15:28:43 +0000 | [diff] [blame] | 2296 | /* CmpNE32(t,t) ==> 0, for some IRTemp t */ |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 2297 | if (sameIRExprs(env, e->Iex.Binop.arg1, e->Iex.Binop.arg2)) { |
florian | f6402ab | 2012-01-29 02:19:43 +0000 | [diff] [blame] | 2298 | e2 = mkZeroOfPrimopResultType(e->Iex.Binop.op); |
| 2299 | break; |
| 2300 | } |
sewardj | 009230b | 2013-01-26 11:47:55 +0000 | [diff] [blame] | 2301 | /* CmpNE32(1Uto32(b), 0) ==> b */ |
| 2302 | if (isZeroU32(e->Iex.Binop.arg2)) { |
| 2303 | IRExpr* a1 = chase(env, e->Iex.Binop.arg1); |
| 2304 | if (a1 && a1->tag == Iex_Unop |
| 2305 | && a1->Iex.Unop.op == Iop_1Uto32) { |
| 2306 | e2 = a1->Iex.Unop.arg; |
| 2307 | break; |
| 2308 | } |
| 2309 | } |
florian | f6402ab | 2012-01-29 02:19:43 +0000 | [diff] [blame] | 2310 | break; |
| 2311 | |
sewardj | a7e9638 | 2012-06-29 16:26:17 +0000 | [diff] [blame] | 2312 | case Iop_CmpEQ32: |
sewardj | 64d776c | 2010-10-01 14:06:22 +0000 | [diff] [blame] | 2313 | case Iop_CmpEQ64: |
| 2314 | case Iop_CmpEQ8x8: |
| 2315 | case Iop_CmpEQ8x16: |
sewardj | 899d183 | 2012-07-10 21:41:01 +0000 | [diff] [blame] | 2316 | case Iop_CmpEQ16x8: |
sewardj | ffccf2b | 2012-06-29 15:33:09 +0000 | [diff] [blame] | 2317 | case Iop_CmpEQ32x4: |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 2318 | if (sameIRExprs(env, e->Iex.Binop.arg1, e->Iex.Binop.arg2)) { |
sewardj | 64d776c | 2010-10-01 14:06:22 +0000 | [diff] [blame] | 2319 | e2 = mkOnesOfPrimopResultType(e->Iex.Binop.op); |
florian | f6402ab | 2012-01-29 02:19:43 +0000 | [diff] [blame] | 2320 | break; |
| 2321 | } |
sewardj | 64d776c | 2010-10-01 14:06:22 +0000 | [diff] [blame] | 2322 | break; |
florian | f6402ab | 2012-01-29 02:19:43 +0000 | [diff] [blame] | 2323 | |
sewardj | 64d776c | 2010-10-01 14:06:22 +0000 | [diff] [blame] | 2324 | default: |
| 2325 | break; |
sewardj | 0033ddc | 2005-04-26 23:34:34 +0000 | [diff] [blame] | 2326 | } |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 2327 | } |
florian | 708417d | 2012-02-15 00:43:36 +0000 | [diff] [blame] | 2328 | break; |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 2329 | |
florian | 99dd03e | 2013-01-29 03:56:06 +0000 | [diff] [blame] | 2330 | case Iex_ITE: |
| 2331 | /* ITE */ |
sewardj | 6c299f3 | 2009-12-31 18:00:12 +0000 | [diff] [blame] | 2332 | /* is the discriminant is a constant? */ |
florian | 99dd03e | 2013-01-29 03:56:06 +0000 | [diff] [blame] | 2333 | if (e->Iex.ITE.cond->tag == Iex_Const) { |
sewardj | 6c299f3 | 2009-12-31 18:00:12 +0000 | [diff] [blame] | 2334 | /* assured us by the IR type rules */ |
florian | 99dd03e | 2013-01-29 03:56:06 +0000 | [diff] [blame] | 2335 | vassert(e->Iex.ITE.cond->Iex.Const.con->tag == Ico_U1); |
| 2336 | e2 = e->Iex.ITE.cond->Iex.Const.con->Ico.U1 |
| 2337 | ? e->Iex.ITE.iftrue : e->Iex.ITE.iffalse; |
sewardj | 6c299f3 | 2009-12-31 18:00:12 +0000 | [diff] [blame] | 2338 | } |
| 2339 | else |
| 2340 | /* are the arms identical? (pretty weedy test) */ |
florian | 99dd03e | 2013-01-29 03:56:06 +0000 | [diff] [blame] | 2341 | if (sameIRExprs(env, e->Iex.ITE.iftrue, |
| 2342 | e->Iex.ITE.iffalse)) { |
| 2343 | e2 = e->Iex.ITE.iffalse; |
sewardj | 6c299f3 | 2009-12-31 18:00:12 +0000 | [diff] [blame] | 2344 | } |
florian | 708417d | 2012-02-15 00:43:36 +0000 | [diff] [blame] | 2345 | break; |
| 2346 | |
| 2347 | default: |
| 2348 | /* not considered */ |
| 2349 | break; |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 2350 | } |
| 2351 | |
sewardj | a7e9638 | 2012-06-29 16:26:17 +0000 | [diff] [blame] | 2352 | /* Show cases where we've found but not folded 'op(t,t)'. Be |
| 2353 | careful not to call sameIRExprs with values of different types, |
| 2354 | though, else it will assert (and so it should!). We can't |
| 2355 | conveniently call typeOfIRExpr on the two args without a whole |
| 2356 | bunch of extra plumbing to pass in a type env, so just use a |
| 2357 | hacky test to check the arguments are not anything that might |
| 2358 | sameIRExprs to assert. This is only OK because this kludge is |
| 2359 | only used for debug printing, not for "real" operation. For |
| 2360 | "real" operation (ie, all other calls to sameIRExprs), it is |
| 2361 | essential that the to args have the same type. |
| 2362 | |
| 2363 | The "right" solution is to plumb the containing block's |
| 2364 | IRTypeEnv through to here and use typeOfIRExpr to be sure. But |
| 2365 | that's a bunch of extra parameter passing which will just slow |
| 2366 | down the normal case, for no purpose. */ |
| 2367 | if (vex_control.iropt_verbosity > 0 |
| 2368 | && e == e2 |
| 2369 | && e->tag == Iex_Binop |
| 2370 | && !debug_only_hack_sameIRExprs_might_assert(e->Iex.Binop.arg1, |
| 2371 | e->Iex.Binop.arg2) |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 2372 | && sameIRExprs(env, e->Iex.Binop.arg1, e->Iex.Binop.arg2)) { |
sewardj | 4a0bee0 | 2012-06-29 14:44:44 +0000 | [diff] [blame] | 2373 | vex_printf("vex iropt: fold_Expr: no ident rule for: "); |
| 2374 | ppIRExpr(e); |
| 2375 | vex_printf("\n"); |
sewardj | 64d776c | 2010-10-01 14:06:22 +0000 | [diff] [blame] | 2376 | } |
| 2377 | |
| 2378 | /* Show the overall results of folding. */ |
sewardj | 088bcb4 | 2004-08-19 17:16:52 +0000 | [diff] [blame] | 2379 | if (DEBUG_IROPT && e2 != e) { |
| 2380 | vex_printf("FOLD: "); |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 2381 | ppIRExpr(e); vex_printf(" -> "); |
| 2382 | ppIRExpr(e2); vex_printf("\n"); |
| 2383 | } |
| 2384 | |
| 2385 | return e2; |
| 2386 | |
| 2387 | unhandled: |
sewardj | 883b00b | 2004-09-11 09:30:24 +0000 | [diff] [blame] | 2388 | # if 0 |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 2389 | vex_printf("\n\n"); |
| 2390 | ppIRExpr(e); |
| 2391 | vpanic("fold_Expr: no rule for the above"); |
sewardj | 883b00b | 2004-09-11 09:30:24 +0000 | [diff] [blame] | 2392 | # else |
sewardj | 328b54b | 2005-06-13 16:30:18 +0000 | [diff] [blame] | 2393 | if (vex_control.iropt_verbosity > 0) { |
sewardj | 4a0bee0 | 2012-06-29 14:44:44 +0000 | [diff] [blame] | 2394 | vex_printf("vex iropt: fold_Expr: no const rule for: "); |
sewardj | 328b54b | 2005-06-13 16:30:18 +0000 | [diff] [blame] | 2395 | ppIRExpr(e); |
| 2396 | vex_printf("\n"); |
| 2397 | } |
sewardj | 883b00b | 2004-09-11 09:30:24 +0000 | [diff] [blame] | 2398 | return e2; |
| 2399 | # endif |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 2400 | } |
| 2401 | |
| 2402 | |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 2403 | /* Apply the subst to a simple 1-level expression -- guaranteed to be |
| 2404 | 1-level due to previous flattening pass. */ |
| 2405 | |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2406 | static IRExpr* subst_Expr ( IRExpr** env, IRExpr* ex ) |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 2407 | { |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2408 | switch (ex->tag) { |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2409 | case Iex_RdTmp: |
| 2410 | if (env[(Int)ex->Iex.RdTmp.tmp] != NULL) { |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 2411 | IRExpr *rhs = env[(Int)ex->Iex.RdTmp.tmp]; |
| 2412 | if (rhs->tag == Iex_RdTmp) |
| 2413 | return rhs; |
| 2414 | if (rhs->tag == Iex_Const |
| 2415 | && rhs->Iex.Const.con->tag != Ico_F64i) |
| 2416 | return rhs; |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2417 | } |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 2418 | /* not bound in env */ |
| 2419 | return ex; |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2420 | |
| 2421 | case Iex_Const: |
| 2422 | case Iex_Get: |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 2423 | return ex; |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2424 | |
| 2425 | case Iex_GetI: |
sewardj | 496a58d | 2005-03-20 18:44:44 +0000 | [diff] [blame] | 2426 | vassert(isIRAtom(ex->Iex.GetI.ix)); |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2427 | return IRExpr_GetI( |
| 2428 | ex->Iex.GetI.descr, |
sewardj | eeac841 | 2004-11-02 00:26:55 +0000 | [diff] [blame] | 2429 | subst_Expr(env, ex->Iex.GetI.ix), |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2430 | ex->Iex.GetI.bias |
| 2431 | ); |
| 2432 | |
florian | 96d7cc3 | 2012-06-01 20:41:24 +0000 | [diff] [blame] | 2433 | case Iex_Qop: { |
| 2434 | IRQop* qop = ex->Iex.Qop.details; |
| 2435 | vassert(isIRAtom(qop->arg1)); |
| 2436 | vassert(isIRAtom(qop->arg2)); |
| 2437 | vassert(isIRAtom(qop->arg3)); |
| 2438 | vassert(isIRAtom(qop->arg4)); |
sewardj | 40c8026 | 2006-02-08 19:30:46 +0000 | [diff] [blame] | 2439 | return IRExpr_Qop( |
florian | 96d7cc3 | 2012-06-01 20:41:24 +0000 | [diff] [blame] | 2440 | qop->op, |
| 2441 | subst_Expr(env, qop->arg1), |
| 2442 | subst_Expr(env, qop->arg2), |
| 2443 | subst_Expr(env, qop->arg3), |
| 2444 | subst_Expr(env, qop->arg4) |
sewardj | 40c8026 | 2006-02-08 19:30:46 +0000 | [diff] [blame] | 2445 | ); |
florian | 96d7cc3 | 2012-06-01 20:41:24 +0000 | [diff] [blame] | 2446 | } |
sewardj | 40c8026 | 2006-02-08 19:30:46 +0000 | [diff] [blame] | 2447 | |
florian | 420bfa9 | 2012-06-02 20:29:22 +0000 | [diff] [blame] | 2448 | case Iex_Triop: { |
| 2449 | IRTriop* triop = ex->Iex.Triop.details; |
| 2450 | vassert(isIRAtom(triop->arg1)); |
| 2451 | vassert(isIRAtom(triop->arg2)); |
| 2452 | vassert(isIRAtom(triop->arg3)); |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 2453 | return IRExpr_Triop( |
florian | 420bfa9 | 2012-06-02 20:29:22 +0000 | [diff] [blame] | 2454 | triop->op, |
| 2455 | subst_Expr(env, triop->arg1), |
| 2456 | subst_Expr(env, triop->arg2), |
| 2457 | subst_Expr(env, triop->arg3) |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 2458 | ); |
florian | 420bfa9 | 2012-06-02 20:29:22 +0000 | [diff] [blame] | 2459 | } |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 2460 | |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2461 | case Iex_Binop: |
sewardj | 496a58d | 2005-03-20 18:44:44 +0000 | [diff] [blame] | 2462 | vassert(isIRAtom(ex->Iex.Binop.arg1)); |
| 2463 | vassert(isIRAtom(ex->Iex.Binop.arg2)); |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2464 | return IRExpr_Binop( |
| 2465 | ex->Iex.Binop.op, |
| 2466 | subst_Expr(env, ex->Iex.Binop.arg1), |
| 2467 | subst_Expr(env, ex->Iex.Binop.arg2) |
| 2468 | ); |
| 2469 | |
| 2470 | case Iex_Unop: |
sewardj | 496a58d | 2005-03-20 18:44:44 +0000 | [diff] [blame] | 2471 | vassert(isIRAtom(ex->Iex.Unop.arg)); |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2472 | return IRExpr_Unop( |
| 2473 | ex->Iex.Unop.op, |
| 2474 | subst_Expr(env, ex->Iex.Unop.arg) |
| 2475 | ); |
| 2476 | |
sewardj | af1ceca | 2005-06-30 23:31:27 +0000 | [diff] [blame] | 2477 | case Iex_Load: |
| 2478 | vassert(isIRAtom(ex->Iex.Load.addr)); |
| 2479 | return IRExpr_Load( |
| 2480 | ex->Iex.Load.end, |
| 2481 | ex->Iex.Load.ty, |
| 2482 | subst_Expr(env, ex->Iex.Load.addr) |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2483 | ); |
| 2484 | |
| 2485 | case Iex_CCall: { |
| 2486 | Int i; |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2487 | IRExpr** args2 = shallowCopyIRExprVec(ex->Iex.CCall.args); |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2488 | for (i = 0; args2[i]; i++) { |
sewardj | 496a58d | 2005-03-20 18:44:44 +0000 | [diff] [blame] | 2489 | vassert(isIRAtom(args2[i])); |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2490 | args2[i] = subst_Expr(env, args2[i]); |
| 2491 | } |
| 2492 | return IRExpr_CCall( |
sewardj | 8ea867b | 2004-10-30 19:03:02 +0000 | [diff] [blame] | 2493 | ex->Iex.CCall.cee, |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2494 | ex->Iex.CCall.retty, |
| 2495 | args2 |
| 2496 | ); |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 2497 | } |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2498 | |
florian | 99dd03e | 2013-01-29 03:56:06 +0000 | [diff] [blame] | 2499 | case Iex_ITE: |
| 2500 | vassert(isIRAtom(ex->Iex.ITE.cond)); |
| 2501 | vassert(isIRAtom(ex->Iex.ITE.iftrue)); |
| 2502 | vassert(isIRAtom(ex->Iex.ITE.iffalse)); |
| 2503 | return IRExpr_ITE( |
| 2504 | subst_Expr(env, ex->Iex.ITE.cond), |
| 2505 | subst_Expr(env, ex->Iex.ITE.iftrue), |
| 2506 | subst_Expr(env, ex->Iex.ITE.iffalse) |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2507 | ); |
| 2508 | |
| 2509 | default: |
| 2510 | vex_printf("\n\n"); ppIRExpr(ex); |
| 2511 | vpanic("subst_Expr"); |
| 2512 | |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 2513 | } |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 2514 | } |
| 2515 | |
| 2516 | |
| 2517 | /* Apply the subst to stmt, then fold the result as much as possible. |
sewardj | d2445f6 | 2005-03-21 00:15:53 +0000 | [diff] [blame] | 2518 | Much simplified due to stmt being previously flattened. As a |
| 2519 | result of this, the stmt may wind up being turned into a no-op. |
| 2520 | */ |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2521 | static IRStmt* subst_and_fold_Stmt ( IRExpr** env, IRStmt* st ) |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 2522 | { |
| 2523 | # if 0 |
| 2524 | vex_printf("\nsubst and fold stmt\n"); |
| 2525 | ppIRStmt(st); |
| 2526 | vex_printf("\n"); |
| 2527 | # endif |
| 2528 | |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2529 | switch (st->tag) { |
sewardj | 5a9ffab | 2005-05-12 17:55:01 +0000 | [diff] [blame] | 2530 | case Ist_AbiHint: |
| 2531 | vassert(isIRAtom(st->Ist.AbiHint.base)); |
sewardj | 478646f | 2008-05-01 20:13:04 +0000 | [diff] [blame] | 2532 | vassert(isIRAtom(st->Ist.AbiHint.nia)); |
sewardj | 5a9ffab | 2005-05-12 17:55:01 +0000 | [diff] [blame] | 2533 | return IRStmt_AbiHint( |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 2534 | fold_Expr(env, subst_Expr(env, st->Ist.AbiHint.base)), |
sewardj | 478646f | 2008-05-01 20:13:04 +0000 | [diff] [blame] | 2535 | st->Ist.AbiHint.len, |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 2536 | fold_Expr(env, subst_Expr(env, st->Ist.AbiHint.nia)) |
sewardj | 5a9ffab | 2005-05-12 17:55:01 +0000 | [diff] [blame] | 2537 | ); |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2538 | case Ist_Put: |
sewardj | 496a58d | 2005-03-20 18:44:44 +0000 | [diff] [blame] | 2539 | vassert(isIRAtom(st->Ist.Put.data)); |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2540 | return IRStmt_Put( |
| 2541 | st->Ist.Put.offset, |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 2542 | fold_Expr(env, subst_Expr(env, st->Ist.Put.data)) |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2543 | ); |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 2544 | |
florian | d6f38b3 | 2012-05-31 15:46:18 +0000 | [diff] [blame] | 2545 | case Ist_PutI: { |
| 2546 | IRPutI *puti, *puti2; |
| 2547 | puti = st->Ist.PutI.details; |
| 2548 | vassert(isIRAtom(puti->ix)); |
| 2549 | vassert(isIRAtom(puti->data)); |
| 2550 | puti2 = mkIRPutI(puti->descr, |
| 2551 | fold_Expr(env, subst_Expr(env, puti->ix)), |
| 2552 | puti->bias, |
| 2553 | fold_Expr(env, subst_Expr(env, puti->data))); |
| 2554 | return IRStmt_PutI(puti2); |
| 2555 | } |
sewardj | d721703 | 2004-08-19 10:49:10 +0000 | [diff] [blame] | 2556 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2557 | case Ist_WrTmp: |
| 2558 | /* This is the one place where an expr (st->Ist.WrTmp.data) is |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2559 | allowed to be more than just a constant or a tmp. */ |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2560 | return IRStmt_WrTmp( |
| 2561 | st->Ist.WrTmp.tmp, |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 2562 | fold_Expr(env, subst_Expr(env, st->Ist.WrTmp.data)) |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2563 | ); |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 2564 | |
sewardj | af1ceca | 2005-06-30 23:31:27 +0000 | [diff] [blame] | 2565 | case Ist_Store: |
| 2566 | vassert(isIRAtom(st->Ist.Store.addr)); |
| 2567 | vassert(isIRAtom(st->Ist.Store.data)); |
| 2568 | return IRStmt_Store( |
| 2569 | st->Ist.Store.end, |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 2570 | fold_Expr(env, subst_Expr(env, st->Ist.Store.addr)), |
| 2571 | fold_Expr(env, subst_Expr(env, st->Ist.Store.data)) |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2572 | ); |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 2573 | |
sewardj | cfe046e | 2013-01-17 14:23:53 +0000 | [diff] [blame] | 2574 | case Ist_StoreG: { |
| 2575 | IRStoreG* sg = st->Ist.StoreG.details; |
| 2576 | vassert(isIRAtom(sg->addr)); |
| 2577 | vassert(isIRAtom(sg->data)); |
| 2578 | vassert(isIRAtom(sg->guard)); |
| 2579 | IRExpr* faddr = fold_Expr(env, subst_Expr(env, sg->addr)); |
| 2580 | IRExpr* fdata = fold_Expr(env, subst_Expr(env, sg->data)); |
| 2581 | IRExpr* fguard = fold_Expr(env, subst_Expr(env, sg->guard)); |
| 2582 | if (fguard->tag == Iex_Const) { |
| 2583 | /* The condition on this store has folded down to a constant. */ |
| 2584 | vassert(fguard->Iex.Const.con->tag == Ico_U1); |
| 2585 | if (fguard->Iex.Const.con->Ico.U1 == False) { |
| 2586 | return IRStmt_NoOp(); |
| 2587 | } else { |
| 2588 | vassert(fguard->Iex.Const.con->Ico.U1 == True); |
| 2589 | return IRStmt_Store(sg->end, faddr, fdata); |
| 2590 | } |
| 2591 | } |
| 2592 | return IRStmt_StoreG(sg->end, faddr, fdata, fguard); |
| 2593 | } |
| 2594 | |
| 2595 | case Ist_LoadG: { |
| 2596 | /* This is complicated. If the guard folds down to 'false', |
| 2597 | we can replace it with an assignment 'dst := alt', but if |
| 2598 | the guard folds down to 'true', we can't conveniently |
| 2599 | replace it with an unconditional load, because doing so |
| 2600 | requires generating a new temporary, and that is not easy |
| 2601 | to do at this point. */ |
| 2602 | IRLoadG* lg = st->Ist.LoadG.details; |
| 2603 | vassert(isIRAtom(lg->addr)); |
| 2604 | vassert(isIRAtom(lg->alt)); |
| 2605 | vassert(isIRAtom(lg->guard)); |
| 2606 | IRExpr* faddr = fold_Expr(env, subst_Expr(env, lg->addr)); |
| 2607 | IRExpr* falt = fold_Expr(env, subst_Expr(env, lg->alt)); |
| 2608 | IRExpr* fguard = fold_Expr(env, subst_Expr(env, lg->guard)); |
| 2609 | if (fguard->tag == Iex_Const) { |
| 2610 | /* The condition on this load has folded down to a constant. */ |
| 2611 | vassert(fguard->Iex.Const.con->tag == Ico_U1); |
| 2612 | if (fguard->Iex.Const.con->Ico.U1 == False) { |
| 2613 | /* The load is not going to happen -- instead 'alt' is |
| 2614 | assigned to 'dst'. */ |
| 2615 | return IRStmt_WrTmp(lg->dst, falt); |
| 2616 | } else { |
| 2617 | vassert(fguard->Iex.Const.con->Ico.U1 == True); |
| 2618 | /* The load is always going to happen. We want to |
| 2619 | convert to an unconditional load and assign to 'dst' |
| 2620 | (IRStmt_WrTmp). Problem is we need an extra temp to |
| 2621 | hold the loaded value, but none is available. |
| 2622 | Instead, reconstitute the conditional load (with |
| 2623 | folded args, of course) and let the caller of this |
| 2624 | routine deal with the problem. */ |
| 2625 | } |
| 2626 | } |
| 2627 | return IRStmt_LoadG(lg->end, lg->cvt, lg->dst, faddr, falt, fguard); |
| 2628 | } |
| 2629 | |
sewardj | e9d8a26 | 2009-07-01 08:06:34 +0000 | [diff] [blame] | 2630 | case Ist_CAS: { |
| 2631 | IRCAS *cas, *cas2; |
| 2632 | cas = st->Ist.CAS.details; |
| 2633 | vassert(isIRAtom(cas->addr)); |
| 2634 | vassert(cas->expdHi == NULL || isIRAtom(cas->expdHi)); |
| 2635 | vassert(isIRAtom(cas->expdLo)); |
| 2636 | vassert(cas->dataHi == NULL || isIRAtom(cas->dataHi)); |
| 2637 | vassert(isIRAtom(cas->dataLo)); |
| 2638 | cas2 = mkIRCAS( |
| 2639 | cas->oldHi, cas->oldLo, cas->end, |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 2640 | fold_Expr(env, subst_Expr(env, cas->addr)), |
sewardj | 6399f81 | 2012-06-29 15:36:44 +0000 | [diff] [blame] | 2641 | cas->expdHi ? fold_Expr(env, subst_Expr(env, cas->expdHi)) |
| 2642 | : NULL, |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 2643 | fold_Expr(env, subst_Expr(env, cas->expdLo)), |
sewardj | 6399f81 | 2012-06-29 15:36:44 +0000 | [diff] [blame] | 2644 | cas->dataHi ? fold_Expr(env, subst_Expr(env, cas->dataHi)) |
| 2645 | : NULL, |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 2646 | fold_Expr(env, subst_Expr(env, cas->dataLo)) |
sewardj | e9d8a26 | 2009-07-01 08:06:34 +0000 | [diff] [blame] | 2647 | ); |
| 2648 | return IRStmt_CAS(cas2); |
| 2649 | } |
| 2650 | |
sewardj | e768e92 | 2009-11-26 17:17:37 +0000 | [diff] [blame] | 2651 | case Ist_LLSC: |
| 2652 | vassert(isIRAtom(st->Ist.LLSC.addr)); |
| 2653 | if (st->Ist.LLSC.storedata) |
| 2654 | vassert(isIRAtom(st->Ist.LLSC.storedata)); |
| 2655 | return IRStmt_LLSC( |
| 2656 | st->Ist.LLSC.end, |
| 2657 | st->Ist.LLSC.result, |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 2658 | fold_Expr(env, subst_Expr(env, st->Ist.LLSC.addr)), |
sewardj | e768e92 | 2009-11-26 17:17:37 +0000 | [diff] [blame] | 2659 | st->Ist.LLSC.storedata |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 2660 | ? fold_Expr(env, subst_Expr(env, st->Ist.LLSC.storedata)) |
sewardj | e768e92 | 2009-11-26 17:17:37 +0000 | [diff] [blame] | 2661 | : NULL |
| 2662 | ); |
| 2663 | |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2664 | case Ist_Dirty: { |
| 2665 | Int i; |
| 2666 | IRDirty *d, *d2; |
| 2667 | d = st->Ist.Dirty.details; |
| 2668 | d2 = emptyIRDirty(); |
| 2669 | *d2 = *d; |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2670 | d2->args = shallowCopyIRExprVec(d2->args); |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2671 | if (d2->mFx != Ifx_None) { |
sewardj | 496a58d | 2005-03-20 18:44:44 +0000 | [diff] [blame] | 2672 | vassert(isIRAtom(d2->mAddr)); |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 2673 | d2->mAddr = fold_Expr(env, subst_Expr(env, d2->mAddr)); |
sewardj | b8e7586 | 2004-08-19 17:58:45 +0000 | [diff] [blame] | 2674 | } |
sewardj | 496a58d | 2005-03-20 18:44:44 +0000 | [diff] [blame] | 2675 | vassert(isIRAtom(d2->guard)); |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 2676 | d2->guard = fold_Expr(env, subst_Expr(env, d2->guard)); |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2677 | for (i = 0; d2->args[i]; i++) { |
sewardj | 74142b8 | 2013-08-08 10:28:59 +0000 | [diff] [blame] | 2678 | IRExpr* arg = d2->args[i]; |
florian | 9041956 | 2013-08-15 20:54:52 +0000 | [diff] [blame] | 2679 | if (LIKELY(!is_IRExpr_VECRET_or_BBPTR(arg))) { |
sewardj | 74142b8 | 2013-08-08 10:28:59 +0000 | [diff] [blame] | 2680 | vassert(isIRAtom(arg)); |
| 2681 | d2->args[i] = fold_Expr(env, subst_Expr(env, arg)); |
| 2682 | } |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2683 | } |
| 2684 | return IRStmt_Dirty(d2); |
sewardj | b8e7586 | 2004-08-19 17:58:45 +0000 | [diff] [blame] | 2685 | } |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 2686 | |
sewardj | f168931 | 2005-03-16 18:19:10 +0000 | [diff] [blame] | 2687 | case Ist_IMark: |
sewardj | 2f10aa6 | 2011-05-27 13:20:56 +0000 | [diff] [blame] | 2688 | return IRStmt_IMark(st->Ist.IMark.addr, |
| 2689 | st->Ist.IMark.len, |
| 2690 | st->Ist.IMark.delta); |
sewardj | f168931 | 2005-03-16 18:19:10 +0000 | [diff] [blame] | 2691 | |
sewardj | d2445f6 | 2005-03-21 00:15:53 +0000 | [diff] [blame] | 2692 | case Ist_NoOp: |
| 2693 | return IRStmt_NoOp(); |
| 2694 | |
sewardj | c4356f0 | 2007-11-09 21:15:04 +0000 | [diff] [blame] | 2695 | case Ist_MBE: |
| 2696 | return IRStmt_MBE(st->Ist.MBE.event); |
sewardj | 3e83893 | 2005-01-07 12:09:15 +0000 | [diff] [blame] | 2697 | |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2698 | case Ist_Exit: { |
| 2699 | IRExpr* fcond; |
sewardj | 496a58d | 2005-03-20 18:44:44 +0000 | [diff] [blame] | 2700 | vassert(isIRAtom(st->Ist.Exit.guard)); |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 2701 | fcond = fold_Expr(env, subst_Expr(env, st->Ist.Exit.guard)); |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2702 | if (fcond->tag == Iex_Const) { |
| 2703 | /* Interesting. The condition on this exit has folded down to |
| 2704 | a constant. */ |
sewardj | ba99931 | 2004-11-15 15:21:17 +0000 | [diff] [blame] | 2705 | vassert(fcond->Iex.Const.con->tag == Ico_U1); |
| 2706 | if (fcond->Iex.Const.con->Ico.U1 == False) { |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2707 | /* exit is never going to happen, so dump the statement. */ |
sewardj | d2445f6 | 2005-03-21 00:15:53 +0000 | [diff] [blame] | 2708 | return IRStmt_NoOp(); |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2709 | } else { |
sewardj | ba99931 | 2004-11-15 15:21:17 +0000 | [diff] [blame] | 2710 | vassert(fcond->Iex.Const.con->Ico.U1 == True); |
sewardj | e810c19 | 2005-09-09 08:33:03 +0000 | [diff] [blame] | 2711 | /* Hmmm. The exit has become unconditional. Leave it |
| 2712 | as it is for now, since we'd have to truncate the BB |
| 2713 | at this point, which is tricky. Such truncation is |
| 2714 | done later by the dead-code elimination pass. */ |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2715 | /* fall out into the reconstruct-the-exit code. */ |
sewardj | 0861374 | 2004-10-25 13:01:45 +0000 | [diff] [blame] | 2716 | if (vex_control.iropt_verbosity > 0) |
| 2717 | /* really a misuse of vex_control.iropt_verbosity */ |
sewardj | 8c2c10b | 2004-10-16 20:51:52 +0000 | [diff] [blame] | 2718 | vex_printf("vex iropt: IRStmt_Exit became unconditional\n"); |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2719 | } |
| 2720 | } |
sewardj | c6f970f | 2012-04-02 21:54:49 +0000 | [diff] [blame] | 2721 | return IRStmt_Exit(fcond, st->Ist.Exit.jk, |
| 2722 | st->Ist.Exit.dst, st->Ist.Exit.offsIP); |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2723 | } |
| 2724 | |
| 2725 | default: |
| 2726 | vex_printf("\n"); ppIRStmt(st); |
| 2727 | vpanic("subst_and_fold_Stmt"); |
| 2728 | } |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 2729 | } |
| 2730 | |
| 2731 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2732 | IRSB* cprop_BB ( IRSB* in ) |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 2733 | { |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2734 | Int i; |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2735 | IRSB* out; |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2736 | IRStmt* st2; |
| 2737 | Int n_tmps = in->tyenv->types_used; |
| 2738 | IRExpr** env = LibVEX_Alloc(n_tmps * sizeof(IRExpr*)); |
sewardj | cfe046e | 2013-01-17 14:23:53 +0000 | [diff] [blame] | 2739 | /* Keep track of IRStmt_LoadGs that we need to revisit after |
| 2740 | processing all the other statements. */ |
| 2741 | const Int N_FIXUPS = 16; |
| 2742 | Int fixups[N_FIXUPS]; /* indices in the stmt array of 'out' */ |
| 2743 | Int n_fixups = 0; |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 2744 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2745 | out = emptyIRSB(); |
| 2746 | out->tyenv = deepCopyIRTypeEnv( in->tyenv ); |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 2747 | |
| 2748 | /* Set up the env with which travels forward. This holds a |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 2749 | substitution, mapping IRTemps to IRExprs. The environment |
| 2750 | is to be applied as we move along. Keys are IRTemps. |
| 2751 | Values are IRExpr*s. |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 2752 | */ |
sewardj | 62617ef | 2004-10-13 23:29:22 +0000 | [diff] [blame] | 2753 | for (i = 0; i < n_tmps; i++) |
| 2754 | env[i] = NULL; |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 2755 | |
| 2756 | /* For each original SSA-form stmt ... */ |
| 2757 | for (i = 0; i < in->stmts_used; i++) { |
| 2758 | |
| 2759 | /* First apply the substitution to the current stmt. This |
| 2760 | propagates in any constants and tmp-tmp assignments |
| 2761 | accumulated prior to this point. As part of the subst_Stmt |
| 2762 | call, also then fold any constant expressions resulting. */ |
| 2763 | |
sewardj | f0e4316 | 2004-08-20 00:11:12 +0000 | [diff] [blame] | 2764 | st2 = in->stmts[i]; |
| 2765 | |
| 2766 | /* perhaps st2 is already a no-op? */ |
sewardj | 8bee6d1 | 2005-03-22 02:24:05 +0000 | [diff] [blame] | 2767 | if (st2->tag == Ist_NoOp) continue; |
sewardj | f0e4316 | 2004-08-20 00:11:12 +0000 | [diff] [blame] | 2768 | |
| 2769 | st2 = subst_and_fold_Stmt( env, st2 ); |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 2770 | |
sewardj | cfe046e | 2013-01-17 14:23:53 +0000 | [diff] [blame] | 2771 | /* Deal with some post-folding special cases. */ |
| 2772 | switch (st2->tag) { |
sewardj | b8e7586 | 2004-08-19 17:58:45 +0000 | [diff] [blame] | 2773 | |
sewardj | cfe046e | 2013-01-17 14:23:53 +0000 | [diff] [blame] | 2774 | /* If the statement has been folded into a no-op, forget |
| 2775 | it. */ |
| 2776 | case Ist_NoOp: |
| 2777 | continue; |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 2778 | |
sewardj | cfe046e | 2013-01-17 14:23:53 +0000 | [diff] [blame] | 2779 | /* If the statement assigns to an IRTemp add it to the |
| 2780 | running environment. This is for the benefit of copy |
| 2781 | propagation and to allow sameIRExpr look through |
| 2782 | IRTemps. */ |
| 2783 | case Ist_WrTmp: { |
| 2784 | vassert(env[(Int)(st2->Ist.WrTmp.tmp)] == NULL); |
| 2785 | env[(Int)(st2->Ist.WrTmp.tmp)] = st2->Ist.WrTmp.data; |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 2786 | |
sewardj | cfe046e | 2013-01-17 14:23:53 +0000 | [diff] [blame] | 2787 | /* 't1 = t2' -- don't add to BB; will be optimized out */ |
| 2788 | if (st2->Ist.WrTmp.data->tag == Iex_RdTmp) |
| 2789 | continue; |
| 2790 | |
| 2791 | /* 't = const' && 'const != F64i' -- don't add to BB |
| 2792 | Note, we choose not to propagate const when const is an |
| 2793 | F64i, so that F64i literals can be CSE'd later. This |
| 2794 | helps x86 floating point code generation. */ |
| 2795 | if (st2->Ist.WrTmp.data->tag == Iex_Const |
| 2796 | && st2->Ist.WrTmp.data->Iex.Const.con->tag != Ico_F64i) { |
| 2797 | continue; |
| 2798 | } |
| 2799 | /* else add it to the output, as normal */ |
| 2800 | break; |
| 2801 | } |
| 2802 | |
| 2803 | case Ist_LoadG: { |
| 2804 | IRLoadG* lg = st2->Ist.LoadG.details; |
| 2805 | IRExpr* guard = lg->guard; |
| 2806 | if (guard->tag == Iex_Const) { |
| 2807 | /* The guard has folded to a constant, and that |
| 2808 | constant must be 1:I1, since subst_and_fold_Stmt |
| 2809 | folds out the case 0:I1 by itself. */ |
| 2810 | vassert(guard->Iex.Const.con->tag == Ico_U1); |
| 2811 | vassert(guard->Iex.Const.con->Ico.U1 == True); |
| 2812 | /* Add a NoOp here as a placeholder, and make a note of |
| 2813 | where it is in the output block. Afterwards we'll |
| 2814 | come back here and transform the NoOp and the LoadG |
| 2815 | into a load-convert pair. The fixups[] entry |
| 2816 | refers to the inserted NoOp, and we expect to find |
| 2817 | the relevant LoadG immediately after it. */ |
| 2818 | vassert(n_fixups >= 0 && n_fixups <= N_FIXUPS); |
| 2819 | if (n_fixups < N_FIXUPS) { |
| 2820 | fixups[n_fixups++] = out->stmts_used; |
| 2821 | addStmtToIRSB( out, IRStmt_NoOp() ); |
| 2822 | } |
| 2823 | } |
| 2824 | /* And always add the LoadG to the output, regardless. */ |
| 2825 | break; |
| 2826 | } |
| 2827 | |
| 2828 | default: |
| 2829 | break; |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 2830 | } |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 2831 | |
| 2832 | /* Not interesting, copy st2 into the output block. */ |
| 2833 | addStmtToIRSB( out, st2 ); |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 2834 | } |
| 2835 | |
sewardj | cfe046e | 2013-01-17 14:23:53 +0000 | [diff] [blame] | 2836 | # if STATS_IROPT |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 2837 | vex_printf("sameIRExpr: invoked = %u/%u equal = %u/%u max_nodes = %u\n", |
| 2838 | invocation_count, recursion_count, success_count, |
| 2839 | recursion_success_count, max_nodes_visited); |
sewardj | cfe046e | 2013-01-17 14:23:53 +0000 | [diff] [blame] | 2840 | # endif |
florian | cdb5fee | 2012-02-13 00:06:29 +0000 | [diff] [blame] | 2841 | |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 2842 | out->next = subst_Expr( env, in->next ); |
| 2843 | out->jumpkind = in->jumpkind; |
sewardj | c6f970f | 2012-04-02 21:54:49 +0000 | [diff] [blame] | 2844 | out->offsIP = in->offsIP; |
sewardj | cfe046e | 2013-01-17 14:23:53 +0000 | [diff] [blame] | 2845 | |
| 2846 | /* Process any leftover unconditional LoadGs that we noticed |
| 2847 | in the main pass. */ |
| 2848 | vassert(n_fixups >= 0 && n_fixups <= N_FIXUPS); |
| 2849 | for (i = 0; i < n_fixups; i++) { |
| 2850 | Int ix = fixups[i]; |
| 2851 | /* Carefully verify that the LoadG has the expected form. */ |
| 2852 | vassert(ix >= 0 && ix+1 < out->stmts_used); |
| 2853 | IRStmt* nop = out->stmts[ix]; |
| 2854 | IRStmt* lgu = out->stmts[ix+1]; |
| 2855 | vassert(nop->tag == Ist_NoOp); |
| 2856 | vassert(lgu->tag == Ist_LoadG); |
| 2857 | IRLoadG* lg = lgu->Ist.LoadG.details; |
| 2858 | IRExpr* guard = lg->guard; |
| 2859 | vassert(guard->Iex.Const.con->tag == Ico_U1); |
| 2860 | vassert(guard->Iex.Const.con->Ico.U1 == True); |
| 2861 | /* Figure out the load and result types, and the implied |
| 2862 | conversion operation. */ |
| 2863 | IRType cvtRes = Ity_INVALID, cvtArg = Ity_INVALID; |
| 2864 | typeOfIRLoadGOp(lg->cvt, &cvtRes, &cvtArg); |
| 2865 | IROp cvtOp = Iop_INVALID; |
| 2866 | switch (lg->cvt) { |
| 2867 | case ILGop_Ident32: break; |
| 2868 | case ILGop_8Uto32: cvtOp = Iop_8Uto32; break; |
| 2869 | case ILGop_8Sto32: cvtOp = Iop_8Sto32; break; |
| 2870 | case ILGop_16Uto32: cvtOp = Iop_16Uto32; break; |
| 2871 | case ILGop_16Sto32: cvtOp = Iop_16Sto32; break; |
| 2872 | default: vpanic("cprop_BB: unhandled ILGOp"); |
| 2873 | } |
| 2874 | /* Replace the placeholder NoOp by the required unconditional |
| 2875 | load. */ |
| 2876 | IRTemp tLoaded = newIRTemp(out->tyenv, cvtArg); |
| 2877 | out->stmts[ix] |
| 2878 | = IRStmt_WrTmp(tLoaded, |
| 2879 | IRExpr_Load(lg->end, cvtArg, lg->addr)); |
| 2880 | /* Replace the LoadG by a conversion from the loaded value's |
| 2881 | type to the required result type. */ |
| 2882 | out->stmts[ix+1] |
| 2883 | = IRStmt_WrTmp( |
| 2884 | lg->dst, cvtOp == Iop_INVALID |
| 2885 | ? IRExpr_RdTmp(tLoaded) |
| 2886 | : IRExpr_Unop(cvtOp, IRExpr_RdTmp(tLoaded))); |
| 2887 | } |
| 2888 | |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 2889 | return out; |
| 2890 | } |
| 2891 | |
| 2892 | |
sewardj | edf4d69 | 2004-08-17 13:52:58 +0000 | [diff] [blame] | 2893 | /*---------------------------------------------------------------*/ |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 2894 | /*--- Dead code (t = E) removal ---*/ |
| 2895 | /*---------------------------------------------------------------*/ |
| 2896 | |
sewardj | e810c19 | 2005-09-09 08:33:03 +0000 | [diff] [blame] | 2897 | /* As a side effect, also removes all code following an unconditional |
| 2898 | side exit. */ |
| 2899 | |
sewardj | ea602bc | 2004-10-14 21:40:12 +0000 | [diff] [blame] | 2900 | /* The type of the HashHW map is: a map from IRTemp to nothing |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 2901 | -- really just operating a set or IRTemps. |
| 2902 | */ |
| 2903 | |
sewardj | d503a32 | 2004-10-13 22:41:16 +0000 | [diff] [blame] | 2904 | inline |
| 2905 | static void addUses_Temp ( Bool* set, IRTemp tmp ) |
sewardj | 17442fe | 2004-09-20 14:54:28 +0000 | [diff] [blame] | 2906 | { |
sewardj | d503a32 | 2004-10-13 22:41:16 +0000 | [diff] [blame] | 2907 | set[(Int)tmp] = True; |
sewardj | 17442fe | 2004-09-20 14:54:28 +0000 | [diff] [blame] | 2908 | } |
| 2909 | |
sewardj | d503a32 | 2004-10-13 22:41:16 +0000 | [diff] [blame] | 2910 | static void addUses_Expr ( Bool* set, IRExpr* e ) |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 2911 | { |
| 2912 | Int i; |
| 2913 | switch (e->tag) { |
sewardj | d721703 | 2004-08-19 10:49:10 +0000 | [diff] [blame] | 2914 | case Iex_GetI: |
sewardj | eeac841 | 2004-11-02 00:26:55 +0000 | [diff] [blame] | 2915 | addUses_Expr(set, e->Iex.GetI.ix); |
sewardj | d721703 | 2004-08-19 10:49:10 +0000 | [diff] [blame] | 2916 | return; |
florian | 99dd03e | 2013-01-29 03:56:06 +0000 | [diff] [blame] | 2917 | case Iex_ITE: |
| 2918 | addUses_Expr(set, e->Iex.ITE.cond); |
| 2919 | addUses_Expr(set, e->Iex.ITE.iftrue); |
| 2920 | addUses_Expr(set, e->Iex.ITE.iffalse); |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 2921 | return; |
| 2922 | case Iex_CCall: |
| 2923 | for (i = 0; e->Iex.CCall.args[i]; i++) |
| 2924 | addUses_Expr(set, e->Iex.CCall.args[i]); |
| 2925 | return; |
sewardj | af1ceca | 2005-06-30 23:31:27 +0000 | [diff] [blame] | 2926 | case Iex_Load: |
| 2927 | addUses_Expr(set, e->Iex.Load.addr); |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 2928 | return; |
sewardj | 40c8026 | 2006-02-08 19:30:46 +0000 | [diff] [blame] | 2929 | case Iex_Qop: |
florian | 96d7cc3 | 2012-06-01 20:41:24 +0000 | [diff] [blame] | 2930 | addUses_Expr(set, e->Iex.Qop.details->arg1); |
| 2931 | addUses_Expr(set, e->Iex.Qop.details->arg2); |
| 2932 | addUses_Expr(set, e->Iex.Qop.details->arg3); |
| 2933 | addUses_Expr(set, e->Iex.Qop.details->arg4); |
sewardj | 40c8026 | 2006-02-08 19:30:46 +0000 | [diff] [blame] | 2934 | return; |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 2935 | case Iex_Triop: |
florian | 420bfa9 | 2012-06-02 20:29:22 +0000 | [diff] [blame] | 2936 | addUses_Expr(set, e->Iex.Triop.details->arg1); |
| 2937 | addUses_Expr(set, e->Iex.Triop.details->arg2); |
| 2938 | addUses_Expr(set, e->Iex.Triop.details->arg3); |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 2939 | return; |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 2940 | case Iex_Binop: |
| 2941 | addUses_Expr(set, e->Iex.Binop.arg1); |
| 2942 | addUses_Expr(set, e->Iex.Binop.arg2); |
| 2943 | return; |
| 2944 | case Iex_Unop: |
| 2945 | addUses_Expr(set, e->Iex.Unop.arg); |
| 2946 | return; |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2947 | case Iex_RdTmp: |
| 2948 | addUses_Temp(set, e->Iex.RdTmp.tmp); |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 2949 | return; |
| 2950 | case Iex_Const: |
| 2951 | case Iex_Get: |
| 2952 | return; |
| 2953 | default: |
| 2954 | vex_printf("\n"); |
| 2955 | ppIRExpr(e); |
| 2956 | vpanic("addUses_Expr"); |
| 2957 | } |
| 2958 | } |
| 2959 | |
sewardj | d503a32 | 2004-10-13 22:41:16 +0000 | [diff] [blame] | 2960 | static void addUses_Stmt ( Bool* set, IRStmt* st ) |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 2961 | { |
sewardj | 17442fe | 2004-09-20 14:54:28 +0000 | [diff] [blame] | 2962 | Int i; |
| 2963 | IRDirty* d; |
sewardj | e9d8a26 | 2009-07-01 08:06:34 +0000 | [diff] [blame] | 2964 | IRCAS* cas; |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 2965 | switch (st->tag) { |
sewardj | 5a9ffab | 2005-05-12 17:55:01 +0000 | [diff] [blame] | 2966 | case Ist_AbiHint: |
| 2967 | addUses_Expr(set, st->Ist.AbiHint.base); |
sewardj | 478646f | 2008-05-01 20:13:04 +0000 | [diff] [blame] | 2968 | addUses_Expr(set, st->Ist.AbiHint.nia); |
sewardj | 5a9ffab | 2005-05-12 17:55:01 +0000 | [diff] [blame] | 2969 | return; |
sewardj | d721703 | 2004-08-19 10:49:10 +0000 | [diff] [blame] | 2970 | case Ist_PutI: |
florian | d6f38b3 | 2012-05-31 15:46:18 +0000 | [diff] [blame] | 2971 | addUses_Expr(set, st->Ist.PutI.details->ix); |
| 2972 | addUses_Expr(set, st->Ist.PutI.details->data); |
sewardj | d721703 | 2004-08-19 10:49:10 +0000 | [diff] [blame] | 2973 | return; |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 2974 | case Ist_WrTmp: |
| 2975 | addUses_Expr(set, st->Ist.WrTmp.data); |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 2976 | return; |
| 2977 | case Ist_Put: |
sewardj | 6d07636 | 2004-09-23 11:06:17 +0000 | [diff] [blame] | 2978 | addUses_Expr(set, st->Ist.Put.data); |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 2979 | return; |
sewardj | af1ceca | 2005-06-30 23:31:27 +0000 | [diff] [blame] | 2980 | case Ist_Store: |
| 2981 | addUses_Expr(set, st->Ist.Store.addr); |
| 2982 | addUses_Expr(set, st->Ist.Store.data); |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 2983 | return; |
sewardj | cfe046e | 2013-01-17 14:23:53 +0000 | [diff] [blame] | 2984 | case Ist_StoreG: { |
| 2985 | IRStoreG* sg = st->Ist.StoreG.details; |
| 2986 | addUses_Expr(set, sg->addr); |
| 2987 | addUses_Expr(set, sg->data); |
| 2988 | addUses_Expr(set, sg->guard); |
| 2989 | return; |
| 2990 | } |
| 2991 | case Ist_LoadG: { |
| 2992 | IRLoadG* lg = st->Ist.LoadG.details; |
| 2993 | addUses_Expr(set, lg->addr); |
| 2994 | addUses_Expr(set, lg->alt); |
| 2995 | addUses_Expr(set, lg->guard); |
| 2996 | return; |
| 2997 | } |
sewardj | e9d8a26 | 2009-07-01 08:06:34 +0000 | [diff] [blame] | 2998 | case Ist_CAS: |
| 2999 | cas = st->Ist.CAS.details; |
| 3000 | addUses_Expr(set, cas->addr); |
| 3001 | if (cas->expdHi) |
| 3002 | addUses_Expr(set, cas->expdHi); |
| 3003 | addUses_Expr(set, cas->expdLo); |
| 3004 | if (cas->dataHi) |
| 3005 | addUses_Expr(set, cas->dataHi); |
| 3006 | addUses_Expr(set, cas->dataLo); |
| 3007 | return; |
sewardj | e768e92 | 2009-11-26 17:17:37 +0000 | [diff] [blame] | 3008 | case Ist_LLSC: |
| 3009 | addUses_Expr(set, st->Ist.LLSC.addr); |
| 3010 | if (st->Ist.LLSC.storedata) |
| 3011 | addUses_Expr(set, st->Ist.LLSC.storedata); |
| 3012 | return; |
sewardj | 17442fe | 2004-09-20 14:54:28 +0000 | [diff] [blame] | 3013 | case Ist_Dirty: |
| 3014 | d = st->Ist.Dirty.details; |
| 3015 | if (d->mFx != Ifx_None) |
| 3016 | addUses_Expr(set, d->mAddr); |
sewardj | b8385d8 | 2004-11-02 01:34:15 +0000 | [diff] [blame] | 3017 | addUses_Expr(set, d->guard); |
sewardj | 74142b8 | 2013-08-08 10:28:59 +0000 | [diff] [blame] | 3018 | for (i = 0; d->args[i] != NULL; i++) { |
| 3019 | IRExpr* arg = d->args[i]; |
florian | 9041956 | 2013-08-15 20:54:52 +0000 | [diff] [blame] | 3020 | if (LIKELY(!is_IRExpr_VECRET_or_BBPTR(arg))) |
sewardj | 74142b8 | 2013-08-08 10:28:59 +0000 | [diff] [blame] | 3021 | addUses_Expr(set, arg); |
| 3022 | } |
sewardj | 17442fe | 2004-09-20 14:54:28 +0000 | [diff] [blame] | 3023 | return; |
sewardj | d2445f6 | 2005-03-21 00:15:53 +0000 | [diff] [blame] | 3024 | case Ist_NoOp: |
sewardj | f168931 | 2005-03-16 18:19:10 +0000 | [diff] [blame] | 3025 | case Ist_IMark: |
sewardj | c4356f0 | 2007-11-09 21:15:04 +0000 | [diff] [blame] | 3026 | case Ist_MBE: |
sewardj | 3e83893 | 2005-01-07 12:09:15 +0000 | [diff] [blame] | 3027 | return; |
sewardj | 17442fe | 2004-09-20 14:54:28 +0000 | [diff] [blame] | 3028 | case Ist_Exit: |
sewardj | 0276d4b | 2004-11-15 15:30:21 +0000 | [diff] [blame] | 3029 | addUses_Expr(set, st->Ist.Exit.guard); |
sewardj | 17442fe | 2004-09-20 14:54:28 +0000 | [diff] [blame] | 3030 | return; |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 3031 | default: |
| 3032 | vex_printf("\n"); |
| 3033 | ppIRStmt(st); |
| 3034 | vpanic("addUses_Stmt"); |
sewardj | d503a32 | 2004-10-13 22:41:16 +0000 | [diff] [blame] | 3035 | } |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 3036 | } |
| 3037 | |
| 3038 | |
sewardj | ba99931 | 2004-11-15 15:21:17 +0000 | [diff] [blame] | 3039 | /* Is this literally IRExpr_Const(IRConst_U1(False)) ? */ |
| 3040 | static Bool isZeroU1 ( IRExpr* e ) |
sewardj | d999788 | 2004-11-04 19:42:59 +0000 | [diff] [blame] | 3041 | { |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 3042 | return toBool( e->tag == Iex_Const |
| 3043 | && e->Iex.Const.con->tag == Ico_U1 |
| 3044 | && e->Iex.Const.con->Ico.U1 == False ); |
sewardj | d999788 | 2004-11-04 19:42:59 +0000 | [diff] [blame] | 3045 | } |
| 3046 | |
sewardj | e810c19 | 2005-09-09 08:33:03 +0000 | [diff] [blame] | 3047 | /* Is this literally IRExpr_Const(IRConst_U1(True)) ? */ |
| 3048 | static Bool isOneU1 ( IRExpr* e ) |
| 3049 | { |
| 3050 | return toBool( e->tag == Iex_Const |
| 3051 | && e->Iex.Const.con->tag == Ico_U1 |
| 3052 | && e->Iex.Const.con->Ico.U1 == True ); |
| 3053 | } |
| 3054 | |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 3055 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3056 | /* Note, this destructively modifies the given IRSB. */ |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 3057 | |
| 3058 | /* Scan backwards through statements, carrying a set of IRTemps which |
| 3059 | are known to be used after the current point. On encountering 't = |
| 3060 | E', delete the binding if it is not used. Otherwise, add any temp |
sewardj | e810c19 | 2005-09-09 08:33:03 +0000 | [diff] [blame] | 3061 | uses to the set and keep on moving backwards. |
| 3062 | |
| 3063 | As an enhancement, the first (backwards) pass searches for IR exits |
| 3064 | with always-taken conditions and notes the location of the earliest |
| 3065 | one in the block. If any such are found, a second pass copies the |
| 3066 | exit destination and jump kind to the bb-end. Then, the exit and |
| 3067 | all statements following it are turned into no-ops. |
| 3068 | */ |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 3069 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3070 | /* notstatic */ void do_deadcode_BB ( IRSB* bb ) |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 3071 | { |
sewardj | e810c19 | 2005-09-09 08:33:03 +0000 | [diff] [blame] | 3072 | Int i, i_unconditional_exit; |
sewardj | d503a32 | 2004-10-13 22:41:16 +0000 | [diff] [blame] | 3073 | Int n_tmps = bb->tyenv->types_used; |
| 3074 | Bool* set = LibVEX_Alloc(n_tmps * sizeof(Bool)); |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 3075 | IRStmt* st; |
| 3076 | |
sewardj | d503a32 | 2004-10-13 22:41:16 +0000 | [diff] [blame] | 3077 | for (i = 0; i < n_tmps; i++) |
| 3078 | set[i] = False; |
| 3079 | |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 3080 | /* start off by recording IRTemp uses in the next field. */ |
| 3081 | addUses_Expr(set, bb->next); |
| 3082 | |
sewardj | e810c19 | 2005-09-09 08:33:03 +0000 | [diff] [blame] | 3083 | /* First pass */ |
| 3084 | |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 3085 | /* Work backwards through the stmts */ |
sewardj | e810c19 | 2005-09-09 08:33:03 +0000 | [diff] [blame] | 3086 | i_unconditional_exit = -1; |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 3087 | for (i = bb->stmts_used-1; i >= 0; i--) { |
| 3088 | st = bb->stmts[i]; |
sewardj | 8bee6d1 | 2005-03-22 02:24:05 +0000 | [diff] [blame] | 3089 | if (st->tag == Ist_NoOp) |
sewardj | 84ff065 | 2004-08-23 16:16:08 +0000 | [diff] [blame] | 3090 | continue; |
sewardj | e810c19 | 2005-09-09 08:33:03 +0000 | [diff] [blame] | 3091 | /* take note of any unconditional exits */ |
| 3092 | if (st->tag == Ist_Exit |
| 3093 | && isOneU1(st->Ist.Exit.guard)) |
| 3094 | i_unconditional_exit = i; |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3095 | if (st->tag == Ist_WrTmp |
| 3096 | && set[(Int)(st->Ist.WrTmp.tmp)] == False) { |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 3097 | /* it's an IRTemp which never got used. Delete it. */ |
sewardj | 088bcb4 | 2004-08-19 17:16:52 +0000 | [diff] [blame] | 3098 | if (DEBUG_IROPT) { |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 3099 | vex_printf("DEAD: "); |
| 3100 | ppIRStmt(st); |
| 3101 | vex_printf("\n"); |
| 3102 | } |
sewardj | d2445f6 | 2005-03-21 00:15:53 +0000 | [diff] [blame] | 3103 | bb->stmts[i] = IRStmt_NoOp(); |
sewardj | d999788 | 2004-11-04 19:42:59 +0000 | [diff] [blame] | 3104 | } |
| 3105 | else |
| 3106 | if (st->tag == Ist_Dirty |
| 3107 | && st->Ist.Dirty.details->guard |
sewardj | ba99931 | 2004-11-15 15:21:17 +0000 | [diff] [blame] | 3108 | && isZeroU1(st->Ist.Dirty.details->guard)) { |
sewardj | d2445f6 | 2005-03-21 00:15:53 +0000 | [diff] [blame] | 3109 | /* This is a dirty helper which will never get called. |
| 3110 | Delete it. */ |
| 3111 | bb->stmts[i] = IRStmt_NoOp(); |
sewardj | d999788 | 2004-11-04 19:42:59 +0000 | [diff] [blame] | 3112 | } |
| 3113 | else { |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 3114 | /* Note any IRTemp uses made by the current statement. */ |
| 3115 | addUses_Stmt(set, st); |
| 3116 | } |
| 3117 | } |
sewardj | e810c19 | 2005-09-09 08:33:03 +0000 | [diff] [blame] | 3118 | |
| 3119 | /* Optional second pass: if any unconditional exits were found, |
| 3120 | delete them and all following statements. */ |
| 3121 | |
| 3122 | if (i_unconditional_exit != -1) { |
| 3123 | if (0) vex_printf("ZAPPING ALL FORWARDS from %d\n", |
| 3124 | i_unconditional_exit); |
| 3125 | vassert(i_unconditional_exit >= 0 |
| 3126 | && i_unconditional_exit < bb->stmts_used); |
| 3127 | bb->next |
| 3128 | = IRExpr_Const( bb->stmts[i_unconditional_exit]->Ist.Exit.dst ); |
| 3129 | bb->jumpkind |
| 3130 | = bb->stmts[i_unconditional_exit]->Ist.Exit.jk; |
sewardj | c6f970f | 2012-04-02 21:54:49 +0000 | [diff] [blame] | 3131 | bb->offsIP |
| 3132 | = bb->stmts[i_unconditional_exit]->Ist.Exit.offsIP; |
sewardj | e810c19 | 2005-09-09 08:33:03 +0000 | [diff] [blame] | 3133 | for (i = i_unconditional_exit; i < bb->stmts_used; i++) |
| 3134 | bb->stmts[i] = IRStmt_NoOp(); |
| 3135 | } |
sewardj | 39e3f24 | 2004-08-18 16:54:52 +0000 | [diff] [blame] | 3136 | } |
| 3137 | |
sewardj | e810c19 | 2005-09-09 08:33:03 +0000 | [diff] [blame] | 3138 | |
sewardj | 84ff065 | 2004-08-23 16:16:08 +0000 | [diff] [blame] | 3139 | /*---------------------------------------------------------------*/ |
| 3140 | /*--- Specialisation of helper function calls, in ---*/ |
| 3141 | /*--- collaboration with the front end ---*/ |
| 3142 | /*---------------------------------------------------------------*/ |
| 3143 | |
| 3144 | static |
sewardj | be91791 | 2010-08-22 12:38:53 +0000 | [diff] [blame] | 3145 | IRSB* spec_helpers_BB( |
| 3146 | IRSB* bb, |
florian | 1ff4756 | 2012-10-21 02:09:51 +0000 | [diff] [blame] | 3147 | IRExpr* (*specHelper) (const HChar*, IRExpr**, IRStmt**, Int) |
sewardj | be91791 | 2010-08-22 12:38:53 +0000 | [diff] [blame] | 3148 | ) |
sewardj | 84ff065 | 2004-08-23 16:16:08 +0000 | [diff] [blame] | 3149 | { |
sewardj | b923075 | 2004-12-29 19:25:06 +0000 | [diff] [blame] | 3150 | Int i; |
sewardj | 84ff065 | 2004-08-23 16:16:08 +0000 | [diff] [blame] | 3151 | IRStmt* st; |
| 3152 | IRExpr* ex; |
sewardj | b923075 | 2004-12-29 19:25:06 +0000 | [diff] [blame] | 3153 | Bool any = False; |
sewardj | 84ff065 | 2004-08-23 16:16:08 +0000 | [diff] [blame] | 3154 | |
| 3155 | for (i = bb->stmts_used-1; i >= 0; i--) { |
| 3156 | st = bb->stmts[i]; |
| 3157 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3158 | if (st->tag != Ist_WrTmp |
| 3159 | || st->Ist.WrTmp.data->tag != Iex_CCall) |
sewardj | 8bee6d1 | 2005-03-22 02:24:05 +0000 | [diff] [blame] | 3160 | continue; |
sewardj | 84ff065 | 2004-08-23 16:16:08 +0000 | [diff] [blame] | 3161 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3162 | ex = (*specHelper)( st->Ist.WrTmp.data->Iex.CCall.cee->name, |
sewardj | be91791 | 2010-08-22 12:38:53 +0000 | [diff] [blame] | 3163 | st->Ist.WrTmp.data->Iex.CCall.args, |
| 3164 | &bb->stmts[0], i ); |
sewardj | 84ff065 | 2004-08-23 16:16:08 +0000 | [diff] [blame] | 3165 | if (!ex) |
sewardj | aba4fff | 2004-10-08 21:37:15 +0000 | [diff] [blame] | 3166 | /* the front end can't think of a suitable replacement */ |
| 3167 | continue; |
sewardj | 84ff065 | 2004-08-23 16:16:08 +0000 | [diff] [blame] | 3168 | |
| 3169 | /* We got something better. Install it in the bb. */ |
sewardj | b923075 | 2004-12-29 19:25:06 +0000 | [diff] [blame] | 3170 | any = True; |
sewardj | 84ff065 | 2004-08-23 16:16:08 +0000 | [diff] [blame] | 3171 | bb->stmts[i] |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3172 | = IRStmt_WrTmp(st->Ist.WrTmp.tmp, ex); |
sewardj | 84ff065 | 2004-08-23 16:16:08 +0000 | [diff] [blame] | 3173 | |
| 3174 | if (0) { |
| 3175 | vex_printf("SPEC: "); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3176 | ppIRExpr(st->Ist.WrTmp.data); |
sewardj | 84ff065 | 2004-08-23 16:16:08 +0000 | [diff] [blame] | 3177 | vex_printf(" --> "); |
| 3178 | ppIRExpr(ex); |
| 3179 | vex_printf("\n"); |
| 3180 | } |
| 3181 | } |
sewardj | b923075 | 2004-12-29 19:25:06 +0000 | [diff] [blame] | 3182 | |
| 3183 | if (any) |
| 3184 | bb = flatten_BB(bb); |
| 3185 | return bb; |
sewardj | 84ff065 | 2004-08-23 16:16:08 +0000 | [diff] [blame] | 3186 | } |
| 3187 | |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 3188 | |
| 3189 | /*---------------------------------------------------------------*/ |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 3190 | /*--- Determination of guest state aliasing relationships ---*/ |
| 3191 | /*---------------------------------------------------------------*/ |
| 3192 | |
| 3193 | /* These are helper functions for CSE and GetI/PutI transformations. |
| 3194 | |
| 3195 | Determine, to the extent possible, the relationship between two |
| 3196 | guest state accesses. The possible outcomes are: |
| 3197 | |
| 3198 | * Exact alias. These two accesses denote precisely the same |
| 3199 | piece of the guest state. |
| 3200 | |
| 3201 | * Definitely no alias. These two accesses are guaranteed not to |
| 3202 | overlap any part of the guest state. |
| 3203 | |
| 3204 | * Unknown -- if neither of the above can be established. |
| 3205 | |
| 3206 | If in doubt, return Unknown. */ |
| 3207 | |
| 3208 | typedef |
| 3209 | enum { ExactAlias, NoAlias, UnknownAlias } |
| 3210 | GSAliasing; |
| 3211 | |
| 3212 | |
| 3213 | /* Produces the alias relation between an indexed guest |
| 3214 | state access and a non-indexed access. */ |
| 3215 | |
| 3216 | static |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3217 | GSAliasing getAliasingRelation_IC ( IRRegArray* descr1, IRExpr* ix1, |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 3218 | Int offset2, IRType ty2 ) |
| 3219 | { |
| 3220 | UInt minoff1, maxoff1, minoff2, maxoff2; |
| 3221 | |
| 3222 | getArrayBounds( descr1, &minoff1, &maxoff1 ); |
| 3223 | minoff2 = offset2; |
| 3224 | maxoff2 = minoff2 + sizeofIRType(ty2) - 1; |
| 3225 | |
| 3226 | if (maxoff1 < minoff2 || maxoff2 < minoff1) |
| 3227 | return NoAlias; |
| 3228 | |
| 3229 | /* Could probably do better here if required. For the moment |
| 3230 | however just claim not to know anything more. */ |
| 3231 | return UnknownAlias; |
| 3232 | } |
| 3233 | |
| 3234 | |
| 3235 | /* Produces the alias relation between two indexed guest state |
| 3236 | accesses. */ |
| 3237 | |
| 3238 | static |
| 3239 | GSAliasing getAliasingRelation_II ( |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3240 | IRRegArray* descr1, IRExpr* ix1, Int bias1, |
| 3241 | IRRegArray* descr2, IRExpr* ix2, Int bias2 |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 3242 | ) |
| 3243 | { |
| 3244 | UInt minoff1, maxoff1, minoff2, maxoff2; |
| 3245 | Int iters; |
| 3246 | |
| 3247 | /* First try hard to show they don't alias. */ |
| 3248 | getArrayBounds( descr1, &minoff1, &maxoff1 ); |
| 3249 | getArrayBounds( descr2, &minoff2, &maxoff2 ); |
| 3250 | if (maxoff1 < minoff2 || maxoff2 < minoff1) |
| 3251 | return NoAlias; |
| 3252 | |
| 3253 | /* So the two arrays at least partially overlap. To get any |
| 3254 | further we'll have to be sure that the descriptors are |
| 3255 | identical. */ |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3256 | if (!eqIRRegArray(descr1, descr2)) |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 3257 | return UnknownAlias; |
| 3258 | |
| 3259 | /* The descriptors are identical. Now the only difference can be |
| 3260 | in the index expressions. If they cannot be shown to be |
| 3261 | identical, we have to say we don't know what the aliasing |
| 3262 | relation will be. Now, since the IR is flattened, the index |
| 3263 | expressions should be atoms -- either consts or tmps. So that |
| 3264 | makes the comparison simple. */ |
| 3265 | vassert(isIRAtom(ix1)); |
| 3266 | vassert(isIRAtom(ix2)); |
| 3267 | if (!eqIRAtom(ix1,ix2)) |
| 3268 | return UnknownAlias; |
| 3269 | |
| 3270 | /* Ok, the index expressions are identical. So now the only way |
| 3271 | they can be different is in the bias. Normalise this |
| 3272 | paranoidly, to reliably establish equality/non-equality. */ |
| 3273 | |
| 3274 | /* So now we know that the GetI and PutI index the same array |
| 3275 | with the same base. Are the offsets the same, modulo the |
| 3276 | array size? Do this paranoidly. */ |
| 3277 | vassert(descr1->nElems == descr2->nElems); |
| 3278 | vassert(descr1->elemTy == descr2->elemTy); |
| 3279 | vassert(descr1->base == descr2->base); |
| 3280 | iters = 0; |
| 3281 | while (bias1 < 0 || bias2 < 0) { |
| 3282 | bias1 += descr1->nElems; |
| 3283 | bias2 += descr1->nElems; |
| 3284 | iters++; |
| 3285 | if (iters > 10) |
| 3286 | vpanic("getAliasingRelation: iters"); |
| 3287 | } |
| 3288 | vassert(bias1 >= 0 && bias2 >= 0); |
| 3289 | bias1 %= descr1->nElems; |
| 3290 | bias2 %= descr1->nElems; |
| 3291 | vassert(bias1 >= 0 && bias1 < descr1->nElems); |
| 3292 | vassert(bias2 >= 0 && bias2 < descr1->nElems); |
| 3293 | |
| 3294 | /* Finally, biasP and biasG are normalised into the range |
| 3295 | 0 .. descrP/G->nElems - 1. And so we can establish |
| 3296 | equality/non-equality. */ |
| 3297 | |
| 3298 | return bias1==bias2 ? ExactAlias : NoAlias; |
| 3299 | } |
| 3300 | |
| 3301 | |
| 3302 | /*---------------------------------------------------------------*/ |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3303 | /*--- Common Subexpression Elimination ---*/ |
| 3304 | /*---------------------------------------------------------------*/ |
| 3305 | |
| 3306 | /* Expensive in time and space. */ |
| 3307 | |
| 3308 | /* Uses two environments: |
| 3309 | a IRTemp -> IRTemp mapping |
| 3310 | a mapping from AvailExpr* to IRTemp |
| 3311 | */ |
| 3312 | |
| 3313 | typedef |
| 3314 | struct { |
sewardj | e857804 | 2012-04-13 11:42:50 +0000 | [diff] [blame] | 3315 | enum { TCc, TCt } tag; |
| 3316 | union { IRTemp tmp; IRConst* con; } u; |
| 3317 | } |
| 3318 | TmpOrConst; |
| 3319 | |
| 3320 | static Bool eqTmpOrConst ( TmpOrConst* tc1, TmpOrConst* tc2 ) |
| 3321 | { |
| 3322 | if (tc1->tag != tc2->tag) |
| 3323 | return False; |
| 3324 | switch (tc1->tag) { |
| 3325 | case TCc: |
| 3326 | return eqIRConst(tc1->u.con, tc2->u.con); |
| 3327 | case TCt: |
| 3328 | return tc1->u.tmp == tc2->u.tmp; |
| 3329 | default: |
| 3330 | vpanic("eqTmpOrConst"); |
| 3331 | } |
| 3332 | } |
| 3333 | |
| 3334 | static Bool eqIRCallee ( IRCallee* cee1, IRCallee* cee2 ) |
| 3335 | { |
| 3336 | Bool eq = cee1->addr == cee2->addr; |
| 3337 | if (eq) { |
| 3338 | vassert(cee1->regparms == cee2->regparms); |
| 3339 | vassert(cee1->mcx_mask == cee2->mcx_mask); |
| 3340 | /* Names should be the same too, but we don't bother to |
| 3341 | check. */ |
| 3342 | } |
| 3343 | return eq; |
| 3344 | } |
| 3345 | |
| 3346 | /* Convert a NULL terminated IRExpr* vector to an array of |
| 3347 | TmpOrConsts, and a length. */ |
| 3348 | static void irExprVec_to_TmpOrConsts ( /*OUT*/TmpOrConst** outs, |
| 3349 | /*OUT*/Int* nOuts, |
| 3350 | IRExpr** ins ) |
| 3351 | { |
| 3352 | Int i, n; |
| 3353 | /* We have to make two passes, one to count, one to copy. */ |
| 3354 | for (n = 0; ins[n]; n++) |
| 3355 | ; |
| 3356 | *outs = LibVEX_Alloc(n * sizeof(TmpOrConst)); |
| 3357 | *nOuts = n; |
| 3358 | /* and now copy .. */ |
| 3359 | for (i = 0; i < n; i++) { |
| 3360 | IRExpr* arg = ins[i]; |
| 3361 | TmpOrConst* dst = &(*outs)[i]; |
| 3362 | if (arg->tag == Iex_RdTmp) { |
| 3363 | dst->tag = TCt; |
| 3364 | dst->u.tmp = arg->Iex.RdTmp.tmp; |
| 3365 | } |
| 3366 | else if (arg->tag == Iex_Const) { |
| 3367 | dst->tag = TCc; |
| 3368 | dst->u.con = arg->Iex.Const.con; |
| 3369 | } |
| 3370 | else { |
| 3371 | /* Failure of this is serious; it means that the presented arg |
| 3372 | isn't an IR atom, as it should be. */ |
| 3373 | vpanic("irExprVec_to_TmpOrConsts"); |
| 3374 | } |
| 3375 | } |
| 3376 | } |
| 3377 | |
| 3378 | typedef |
| 3379 | struct { |
florian | b05c20c | 2013-01-31 02:04:02 +0000 | [diff] [blame] | 3380 | enum { Ut, Btt, Btc, Bct, Cf64i, Ittt, Itct, Ittc, Itcc, GetIt, |
florian | da25edd | 2012-09-12 16:40:54 +0000 | [diff] [blame] | 3381 | CCall |
| 3382 | } tag; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3383 | union { |
| 3384 | /* unop(tmp) */ |
| 3385 | struct { |
| 3386 | IROp op; |
| 3387 | IRTemp arg; |
| 3388 | } Ut; |
| 3389 | /* binop(tmp,tmp) */ |
| 3390 | struct { |
| 3391 | IROp op; |
| 3392 | IRTemp arg1; |
| 3393 | IRTemp arg2; |
| 3394 | } Btt; |
| 3395 | /* binop(tmp,const) */ |
| 3396 | struct { |
| 3397 | IROp op; |
| 3398 | IRTemp arg1; |
| 3399 | IRConst con2; |
| 3400 | } Btc; |
| 3401 | /* binop(const,tmp) */ |
| 3402 | struct { |
| 3403 | IROp op; |
| 3404 | IRConst con1; |
| 3405 | IRTemp arg2; |
| 3406 | } Bct; |
| 3407 | /* F64i-style const */ |
| 3408 | struct { |
| 3409 | ULong f64i; |
| 3410 | } Cf64i; |
florian | 99dd03e | 2013-01-29 03:56:06 +0000 | [diff] [blame] | 3411 | /* ITE(tmp,tmp,tmp) */ |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 3412 | struct { |
| 3413 | IRTemp co; |
florian | b05c20c | 2013-01-31 02:04:02 +0000 | [diff] [blame] | 3414 | IRTemp e1; |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 3415 | IRTemp e0; |
florian | b05c20c | 2013-01-31 02:04:02 +0000 | [diff] [blame] | 3416 | } Ittt; |
florian | 99dd03e | 2013-01-29 03:56:06 +0000 | [diff] [blame] | 3417 | /* ITE(tmp,tmp,const) */ |
florian | da25edd | 2012-09-12 16:40:54 +0000 | [diff] [blame] | 3418 | struct { |
| 3419 | IRTemp co; |
florian | b05c20c | 2013-01-31 02:04:02 +0000 | [diff] [blame] | 3420 | IRTemp e1; |
florian | da25edd | 2012-09-12 16:40:54 +0000 | [diff] [blame] | 3421 | IRConst con0; |
florian | b05c20c | 2013-01-31 02:04:02 +0000 | [diff] [blame] | 3422 | } Ittc; |
florian | 99dd03e | 2013-01-29 03:56:06 +0000 | [diff] [blame] | 3423 | /* ITE(tmp,const,tmp) */ |
florian | da25edd | 2012-09-12 16:40:54 +0000 | [diff] [blame] | 3424 | struct { |
| 3425 | IRTemp co; |
florian | b05c20c | 2013-01-31 02:04:02 +0000 | [diff] [blame] | 3426 | IRConst con1; |
florian | da25edd | 2012-09-12 16:40:54 +0000 | [diff] [blame] | 3427 | IRTemp e0; |
florian | b05c20c | 2013-01-31 02:04:02 +0000 | [diff] [blame] | 3428 | } Itct; |
florian | 99dd03e | 2013-01-29 03:56:06 +0000 | [diff] [blame] | 3429 | /* ITE(tmp,const,const) */ |
florian | da25edd | 2012-09-12 16:40:54 +0000 | [diff] [blame] | 3430 | struct { |
| 3431 | IRTemp co; |
florian | b05c20c | 2013-01-31 02:04:02 +0000 | [diff] [blame] | 3432 | IRConst con1; |
florian | da25edd | 2012-09-12 16:40:54 +0000 | [diff] [blame] | 3433 | IRConst con0; |
florian | b05c20c | 2013-01-31 02:04:02 +0000 | [diff] [blame] | 3434 | } Itcc; |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 3435 | /* GetI(descr,tmp,bias)*/ |
| 3436 | struct { |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3437 | IRRegArray* descr; |
| 3438 | IRTemp ix; |
| 3439 | Int bias; |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 3440 | } GetIt; |
sewardj | e857804 | 2012-04-13 11:42:50 +0000 | [diff] [blame] | 3441 | /* Clean helper call */ |
| 3442 | struct { |
| 3443 | IRCallee* cee; |
| 3444 | TmpOrConst* args; |
| 3445 | Int nArgs; |
| 3446 | IRType retty; |
| 3447 | } CCall; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3448 | } u; |
| 3449 | } |
| 3450 | AvailExpr; |
| 3451 | |
| 3452 | static Bool eq_AvailExpr ( AvailExpr* a1, AvailExpr* a2 ) |
| 3453 | { |
sewardj | e857804 | 2012-04-13 11:42:50 +0000 | [diff] [blame] | 3454 | if (LIKELY(a1->tag != a2->tag)) |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3455 | return False; |
| 3456 | switch (a1->tag) { |
| 3457 | case Ut: |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 3458 | return toBool( |
| 3459 | a1->u.Ut.op == a2->u.Ut.op |
| 3460 | && a1->u.Ut.arg == a2->u.Ut.arg); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3461 | case Btt: |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 3462 | return toBool( |
| 3463 | a1->u.Btt.op == a2->u.Btt.op |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3464 | && a1->u.Btt.arg1 == a2->u.Btt.arg1 |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 3465 | && a1->u.Btt.arg2 == a2->u.Btt.arg2); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3466 | case Btc: |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 3467 | return toBool( |
| 3468 | a1->u.Btc.op == a2->u.Btc.op |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3469 | && a1->u.Btc.arg1 == a2->u.Btc.arg1 |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 3470 | && eqIRConst(&a1->u.Btc.con2, &a2->u.Btc.con2)); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3471 | case Bct: |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 3472 | return toBool( |
| 3473 | a1->u.Bct.op == a2->u.Bct.op |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3474 | && a1->u.Bct.arg2 == a2->u.Bct.arg2 |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 3475 | && eqIRConst(&a1->u.Bct.con1, &a2->u.Bct.con1)); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3476 | case Cf64i: |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 3477 | return toBool(a1->u.Cf64i.f64i == a2->u.Cf64i.f64i); |
florian | b05c20c | 2013-01-31 02:04:02 +0000 | [diff] [blame] | 3478 | case Ittt: |
| 3479 | return toBool(a1->u.Ittt.co == a2->u.Ittt.co |
| 3480 | && a1->u.Ittt.e1 == a2->u.Ittt.e1 |
| 3481 | && a1->u.Ittt.e0 == a2->u.Ittt.e0); |
| 3482 | case Ittc: |
| 3483 | return toBool(a1->u.Ittc.co == a2->u.Ittc.co |
| 3484 | && a1->u.Ittc.e1 == a2->u.Ittc.e1 |
| 3485 | && eqIRConst(&a1->u.Ittc.con0, &a2->u.Ittc.con0)); |
| 3486 | case Itct: |
| 3487 | return toBool(a1->u.Itct.co == a2->u.Itct.co |
| 3488 | && eqIRConst(&a1->u.Itct.con1, &a2->u.Itct.con1) |
| 3489 | && a1->u.Itct.e0 == a2->u.Itct.e0); |
| 3490 | case Itcc: |
| 3491 | return toBool(a1->u.Itcc.co == a2->u.Itcc.co |
| 3492 | && eqIRConst(&a1->u.Itcc.con1, &a2->u.Itcc.con1) |
| 3493 | && eqIRConst(&a1->u.Itcc.con0, &a2->u.Itcc.con0)); |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 3494 | case GetIt: |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3495 | return toBool(eqIRRegArray(a1->u.GetIt.descr, a2->u.GetIt.descr) |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 3496 | && a1->u.GetIt.ix == a2->u.GetIt.ix |
| 3497 | && a1->u.GetIt.bias == a2->u.GetIt.bias); |
sewardj | e857804 | 2012-04-13 11:42:50 +0000 | [diff] [blame] | 3498 | case CCall: { |
| 3499 | Int i, n; |
| 3500 | Bool eq = a1->u.CCall.nArgs == a2->u.CCall.nArgs |
| 3501 | && eqIRCallee(a1->u.CCall.cee, a2->u.CCall.cee); |
| 3502 | if (eq) { |
| 3503 | n = a1->u.CCall.nArgs; |
| 3504 | for (i = 0; i < n; i++) { |
| 3505 | if (!eqTmpOrConst( &a1->u.CCall.args[i], |
| 3506 | &a2->u.CCall.args[i] )) { |
| 3507 | eq = False; |
| 3508 | break; |
| 3509 | } |
| 3510 | } |
| 3511 | } |
| 3512 | if (eq) vassert(a1->u.CCall.retty == a2->u.CCall.retty); |
| 3513 | return eq; |
| 3514 | } |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3515 | default: vpanic("eq_AvailExpr"); |
| 3516 | } |
| 3517 | } |
| 3518 | |
| 3519 | static IRExpr* availExpr_to_IRExpr ( AvailExpr* ae ) |
| 3520 | { |
florian | b05c20c | 2013-01-31 02:04:02 +0000 | [diff] [blame] | 3521 | IRConst *con, *con0, *con1; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3522 | switch (ae->tag) { |
| 3523 | case Ut: |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3524 | return IRExpr_Unop( ae->u.Ut.op, IRExpr_RdTmp(ae->u.Ut.arg) ); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3525 | case Btt: |
| 3526 | return IRExpr_Binop( ae->u.Btt.op, |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3527 | IRExpr_RdTmp(ae->u.Btt.arg1), |
| 3528 | IRExpr_RdTmp(ae->u.Btt.arg2) ); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3529 | case Btc: |
| 3530 | con = LibVEX_Alloc(sizeof(IRConst)); |
| 3531 | *con = ae->u.Btc.con2; |
| 3532 | return IRExpr_Binop( ae->u.Btc.op, |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3533 | IRExpr_RdTmp(ae->u.Btc.arg1), |
| 3534 | IRExpr_Const(con) ); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3535 | case Bct: |
| 3536 | con = LibVEX_Alloc(sizeof(IRConst)); |
| 3537 | *con = ae->u.Bct.con1; |
| 3538 | return IRExpr_Binop( ae->u.Bct.op, |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3539 | IRExpr_Const(con), |
| 3540 | IRExpr_RdTmp(ae->u.Bct.arg2) ); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3541 | case Cf64i: |
| 3542 | return IRExpr_Const(IRConst_F64i(ae->u.Cf64i.f64i)); |
florian | b05c20c | 2013-01-31 02:04:02 +0000 | [diff] [blame] | 3543 | case Ittt: |
| 3544 | return IRExpr_ITE(IRExpr_RdTmp(ae->u.Ittt.co), |
| 3545 | IRExpr_RdTmp(ae->u.Ittt.e1), |
| 3546 | IRExpr_RdTmp(ae->u.Ittt.e0)); |
| 3547 | case Ittc: |
florian | da25edd | 2012-09-12 16:40:54 +0000 | [diff] [blame] | 3548 | con0 = LibVEX_Alloc(sizeof(IRConst)); |
florian | b05c20c | 2013-01-31 02:04:02 +0000 | [diff] [blame] | 3549 | *con0 = ae->u.Ittc.con0; |
| 3550 | return IRExpr_ITE(IRExpr_RdTmp(ae->u.Ittc.co), |
| 3551 | IRExpr_RdTmp(ae->u.Ittc.e1), |
florian | 99dd03e | 2013-01-29 03:56:06 +0000 | [diff] [blame] | 3552 | IRExpr_Const(con0)); |
florian | b05c20c | 2013-01-31 02:04:02 +0000 | [diff] [blame] | 3553 | case Itct: |
| 3554 | con1 = LibVEX_Alloc(sizeof(IRConst)); |
| 3555 | *con1 = ae->u.Itct.con1; |
| 3556 | return IRExpr_ITE(IRExpr_RdTmp(ae->u.Itct.co), |
| 3557 | IRExpr_Const(con1), |
| 3558 | IRExpr_RdTmp(ae->u.Itct.e0)); |
florian | 99dd03e | 2013-01-29 03:56:06 +0000 | [diff] [blame] | 3559 | |
florian | b05c20c | 2013-01-31 02:04:02 +0000 | [diff] [blame] | 3560 | case Itcc: |
florian | da25edd | 2012-09-12 16:40:54 +0000 | [diff] [blame] | 3561 | con0 = LibVEX_Alloc(sizeof(IRConst)); |
florian | b05c20c | 2013-01-31 02:04:02 +0000 | [diff] [blame] | 3562 | con1 = LibVEX_Alloc(sizeof(IRConst)); |
| 3563 | *con0 = ae->u.Itcc.con0; |
| 3564 | *con1 = ae->u.Itcc.con1; |
| 3565 | return IRExpr_ITE(IRExpr_RdTmp(ae->u.Itcc.co), |
| 3566 | IRExpr_Const(con1), |
florian | 99dd03e | 2013-01-29 03:56:06 +0000 | [diff] [blame] | 3567 | IRExpr_Const(con0)); |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 3568 | case GetIt: |
| 3569 | return IRExpr_GetI(ae->u.GetIt.descr, |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3570 | IRExpr_RdTmp(ae->u.GetIt.ix), |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 3571 | ae->u.GetIt.bias); |
sewardj | e857804 | 2012-04-13 11:42:50 +0000 | [diff] [blame] | 3572 | case CCall: { |
| 3573 | Int i, n = ae->u.CCall.nArgs; |
| 3574 | vassert(n >= 0); |
| 3575 | IRExpr** vec = LibVEX_Alloc((n+1) * sizeof(IRExpr*)); |
| 3576 | vec[n] = NULL; |
| 3577 | for (i = 0; i < n; i++) { |
| 3578 | TmpOrConst* tc = &ae->u.CCall.args[i]; |
| 3579 | if (tc->tag == TCc) { |
| 3580 | vec[i] = IRExpr_Const(tc->u.con); |
| 3581 | } |
| 3582 | else if (tc->tag == TCt) { |
| 3583 | vec[i] = IRExpr_RdTmp(tc->u.tmp); |
| 3584 | } |
| 3585 | else vpanic("availExpr_to_IRExpr:CCall-arg"); |
| 3586 | } |
| 3587 | return IRExpr_CCall(ae->u.CCall.cee, |
| 3588 | ae->u.CCall.retty, |
| 3589 | vec); |
| 3590 | } |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3591 | default: |
| 3592 | vpanic("availExpr_to_IRExpr"); |
| 3593 | } |
| 3594 | } |
| 3595 | |
| 3596 | inline |
| 3597 | static IRTemp subst_AvailExpr_Temp ( HashHW* env, IRTemp tmp ) |
| 3598 | { |
| 3599 | HWord res; |
| 3600 | /* env :: IRTemp -> IRTemp */ |
| 3601 | if (lookupHHW( env, &res, (HWord)tmp )) |
| 3602 | return (IRTemp)res; |
| 3603 | else |
| 3604 | return tmp; |
| 3605 | } |
| 3606 | |
| 3607 | static void subst_AvailExpr ( HashHW* env, AvailExpr* ae ) |
| 3608 | { |
| 3609 | /* env :: IRTemp -> IRTemp */ |
| 3610 | switch (ae->tag) { |
| 3611 | case Ut: |
| 3612 | ae->u.Ut.arg = subst_AvailExpr_Temp( env, ae->u.Ut.arg ); |
| 3613 | break; |
| 3614 | case Btt: |
| 3615 | ae->u.Btt.arg1 = subst_AvailExpr_Temp( env, ae->u.Btt.arg1 ); |
| 3616 | ae->u.Btt.arg2 = subst_AvailExpr_Temp( env, ae->u.Btt.arg2 ); |
| 3617 | break; |
| 3618 | case Btc: |
| 3619 | ae->u.Btc.arg1 = subst_AvailExpr_Temp( env, ae->u.Btc.arg1 ); |
| 3620 | break; |
| 3621 | case Bct: |
| 3622 | ae->u.Bct.arg2 = subst_AvailExpr_Temp( env, ae->u.Bct.arg2 ); |
| 3623 | break; |
| 3624 | case Cf64i: |
| 3625 | break; |
florian | b05c20c | 2013-01-31 02:04:02 +0000 | [diff] [blame] | 3626 | case Ittt: |
| 3627 | ae->u.Ittt.co = subst_AvailExpr_Temp( env, ae->u.Ittt.co ); |
| 3628 | ae->u.Ittt.e1 = subst_AvailExpr_Temp( env, ae->u.Ittt.e1 ); |
| 3629 | ae->u.Ittt.e0 = subst_AvailExpr_Temp( env, ae->u.Ittt.e0 ); |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 3630 | break; |
florian | b05c20c | 2013-01-31 02:04:02 +0000 | [diff] [blame] | 3631 | case Ittc: |
| 3632 | ae->u.Ittc.co = subst_AvailExpr_Temp( env, ae->u.Ittc.co ); |
| 3633 | ae->u.Ittc.e1 = subst_AvailExpr_Temp( env, ae->u.Ittc.e1 ); |
florian | da25edd | 2012-09-12 16:40:54 +0000 | [diff] [blame] | 3634 | break; |
florian | b05c20c | 2013-01-31 02:04:02 +0000 | [diff] [blame] | 3635 | case Itct: |
| 3636 | ae->u.Itct.co = subst_AvailExpr_Temp( env, ae->u.Itct.co ); |
| 3637 | ae->u.Itct.e0 = subst_AvailExpr_Temp( env, ae->u.Itct.e0 ); |
florian | da25edd | 2012-09-12 16:40:54 +0000 | [diff] [blame] | 3638 | break; |
florian | b05c20c | 2013-01-31 02:04:02 +0000 | [diff] [blame] | 3639 | case Itcc: |
| 3640 | ae->u.Itcc.co = subst_AvailExpr_Temp( env, ae->u.Itcc.co ); |
florian | da25edd | 2012-09-12 16:40:54 +0000 | [diff] [blame] | 3641 | break; |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 3642 | case GetIt: |
| 3643 | ae->u.GetIt.ix = subst_AvailExpr_Temp( env, ae->u.GetIt.ix ); |
| 3644 | break; |
sewardj | e857804 | 2012-04-13 11:42:50 +0000 | [diff] [blame] | 3645 | case CCall: { |
| 3646 | Int i, n = ae->u.CCall.nArgs;; |
| 3647 | for (i = 0; i < n; i++) { |
| 3648 | TmpOrConst* tc = &ae->u.CCall.args[i]; |
| 3649 | if (tc->tag == TCt) { |
| 3650 | tc->u.tmp = subst_AvailExpr_Temp( env, tc->u.tmp ); |
| 3651 | } |
| 3652 | } |
| 3653 | break; |
| 3654 | } |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3655 | default: |
| 3656 | vpanic("subst_AvailExpr"); |
| 3657 | } |
| 3658 | } |
| 3659 | |
| 3660 | static AvailExpr* irExpr_to_AvailExpr ( IRExpr* e ) |
| 3661 | { |
| 3662 | AvailExpr* ae; |
| 3663 | |
florian | c1b9e39 | 2012-09-20 02:40:57 +0000 | [diff] [blame] | 3664 | switch (e->tag) { |
| 3665 | case Iex_Unop: |
| 3666 | if (e->Iex.Unop.arg->tag == Iex_RdTmp) { |
| 3667 | ae = LibVEX_Alloc(sizeof(AvailExpr)); |
| 3668 | ae->tag = Ut; |
| 3669 | ae->u.Ut.op = e->Iex.Unop.op; |
| 3670 | ae->u.Ut.arg = e->Iex.Unop.arg->Iex.RdTmp.tmp; |
| 3671 | return ae; |
| 3672 | } |
| 3673 | break; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3674 | |
florian | c1b9e39 | 2012-09-20 02:40:57 +0000 | [diff] [blame] | 3675 | case Iex_Binop: |
| 3676 | if (e->Iex.Binop.arg1->tag == Iex_RdTmp) { |
| 3677 | if (e->Iex.Binop.arg2->tag == Iex_RdTmp) { |
| 3678 | ae = LibVEX_Alloc(sizeof(AvailExpr)); |
| 3679 | ae->tag = Btt; |
| 3680 | ae->u.Btt.op = e->Iex.Binop.op; |
| 3681 | ae->u.Btt.arg1 = e->Iex.Binop.arg1->Iex.RdTmp.tmp; |
| 3682 | ae->u.Btt.arg2 = e->Iex.Binop.arg2->Iex.RdTmp.tmp; |
| 3683 | return ae; |
| 3684 | } |
| 3685 | if (e->Iex.Binop.arg2->tag == Iex_Const) { |
| 3686 | ae = LibVEX_Alloc(sizeof(AvailExpr)); |
| 3687 | ae->tag = Btc; |
| 3688 | ae->u.Btc.op = e->Iex.Binop.op; |
| 3689 | ae->u.Btc.arg1 = e->Iex.Binop.arg1->Iex.RdTmp.tmp; |
| 3690 | ae->u.Btc.con2 = *(e->Iex.Binop.arg2->Iex.Const.con); |
| 3691 | return ae; |
| 3692 | } |
| 3693 | } else if (e->Iex.Binop.arg1->tag == Iex_Const |
| 3694 | && e->Iex.Binop.arg2->tag == Iex_RdTmp) { |
| 3695 | ae = LibVEX_Alloc(sizeof(AvailExpr)); |
| 3696 | ae->tag = Bct; |
| 3697 | ae->u.Bct.op = e->Iex.Binop.op; |
| 3698 | ae->u.Bct.arg2 = e->Iex.Binop.arg2->Iex.RdTmp.tmp; |
| 3699 | ae->u.Bct.con1 = *(e->Iex.Binop.arg1->Iex.Const.con); |
| 3700 | return ae; |
| 3701 | } |
| 3702 | break; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3703 | |
florian | c1b9e39 | 2012-09-20 02:40:57 +0000 | [diff] [blame] | 3704 | case Iex_Const: |
| 3705 | if (e->Iex.Const.con->tag == Ico_F64i) { |
| 3706 | ae = LibVEX_Alloc(sizeof(AvailExpr)); |
| 3707 | ae->tag = Cf64i; |
| 3708 | ae->u.Cf64i.f64i = e->Iex.Const.con->Ico.F64i; |
| 3709 | return ae; |
| 3710 | } |
| 3711 | break; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3712 | |
florian | 99dd03e | 2013-01-29 03:56:06 +0000 | [diff] [blame] | 3713 | case Iex_ITE: |
| 3714 | if (e->Iex.ITE.cond->tag == Iex_RdTmp) { |
| 3715 | if (e->Iex.ITE.iffalse->tag == Iex_RdTmp) { |
| 3716 | if (e->Iex.ITE.iftrue->tag == Iex_RdTmp) { |
florian | c1b9e39 | 2012-09-20 02:40:57 +0000 | [diff] [blame] | 3717 | ae = LibVEX_Alloc(sizeof(AvailExpr)); |
florian | b05c20c | 2013-01-31 02:04:02 +0000 | [diff] [blame] | 3718 | ae->tag = Ittt; |
| 3719 | ae->u.Ittt.co = e->Iex.ITE.cond->Iex.RdTmp.tmp; |
| 3720 | ae->u.Ittt.e1 = e->Iex.ITE.iftrue->Iex.RdTmp.tmp; |
| 3721 | ae->u.Ittt.e0 = e->Iex.ITE.iffalse->Iex.RdTmp.tmp; |
florian | c1b9e39 | 2012-09-20 02:40:57 +0000 | [diff] [blame] | 3722 | return ae; |
| 3723 | } |
florian | 99dd03e | 2013-01-29 03:56:06 +0000 | [diff] [blame] | 3724 | if (e->Iex.ITE.iftrue->tag == Iex_Const) { |
florian | c1b9e39 | 2012-09-20 02:40:57 +0000 | [diff] [blame] | 3725 | ae = LibVEX_Alloc(sizeof(AvailExpr)); |
florian | b05c20c | 2013-01-31 02:04:02 +0000 | [diff] [blame] | 3726 | ae->tag = Itct; |
| 3727 | ae->u.Itct.co = e->Iex.ITE.cond->Iex.RdTmp.tmp; |
| 3728 | ae->u.Itct.con1 = *(e->Iex.ITE.iftrue->Iex.Const.con); |
| 3729 | ae->u.Itct.e0 = e->Iex.ITE.iffalse->Iex.RdTmp.tmp; |
florian | c1b9e39 | 2012-09-20 02:40:57 +0000 | [diff] [blame] | 3730 | return ae; |
| 3731 | } |
florian | 99dd03e | 2013-01-29 03:56:06 +0000 | [diff] [blame] | 3732 | } else if (e->Iex.ITE.iffalse->tag == Iex_Const) { |
| 3733 | if (e->Iex.ITE.iftrue->tag == Iex_RdTmp) { |
florian | c1b9e39 | 2012-09-20 02:40:57 +0000 | [diff] [blame] | 3734 | ae = LibVEX_Alloc(sizeof(AvailExpr)); |
florian | b05c20c | 2013-01-31 02:04:02 +0000 | [diff] [blame] | 3735 | ae->tag = Ittc; |
| 3736 | ae->u.Ittc.co = e->Iex.ITE.cond->Iex.RdTmp.tmp; |
| 3737 | ae->u.Ittc.e1 = e->Iex.ITE.iftrue->Iex.RdTmp.tmp; |
| 3738 | ae->u.Ittc.con0 = *(e->Iex.ITE.iffalse->Iex.Const.con); |
florian | c1b9e39 | 2012-09-20 02:40:57 +0000 | [diff] [blame] | 3739 | return ae; |
| 3740 | } |
florian | 99dd03e | 2013-01-29 03:56:06 +0000 | [diff] [blame] | 3741 | if (e->Iex.ITE.iftrue->tag == Iex_Const) { |
florian | c1b9e39 | 2012-09-20 02:40:57 +0000 | [diff] [blame] | 3742 | ae = LibVEX_Alloc(sizeof(AvailExpr)); |
florian | b05c20c | 2013-01-31 02:04:02 +0000 | [diff] [blame] | 3743 | ae->tag = Itcc; |
| 3744 | ae->u.Itcc.co = e->Iex.ITE.cond->Iex.RdTmp.tmp; |
| 3745 | ae->u.Itcc.con1 = *(e->Iex.ITE.iftrue->Iex.Const.con); |
| 3746 | ae->u.Itcc.con0 = *(e->Iex.ITE.iffalse->Iex.Const.con); |
florian | c1b9e39 | 2012-09-20 02:40:57 +0000 | [diff] [blame] | 3747 | return ae; |
| 3748 | } |
| 3749 | } |
| 3750 | } |
| 3751 | break; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3752 | |
florian | c1b9e39 | 2012-09-20 02:40:57 +0000 | [diff] [blame] | 3753 | case Iex_GetI: |
| 3754 | if (e->Iex.GetI.ix->tag == Iex_RdTmp) { |
| 3755 | ae = LibVEX_Alloc(sizeof(AvailExpr)); |
| 3756 | ae->tag = GetIt; |
| 3757 | ae->u.GetIt.descr = e->Iex.GetI.descr; |
| 3758 | ae->u.GetIt.ix = e->Iex.GetI.ix->Iex.RdTmp.tmp; |
| 3759 | ae->u.GetIt.bias = e->Iex.GetI.bias; |
| 3760 | return ae; |
| 3761 | } |
| 3762 | break; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3763 | |
florian | c1b9e39 | 2012-09-20 02:40:57 +0000 | [diff] [blame] | 3764 | case Iex_CCall: |
| 3765 | ae = LibVEX_Alloc(sizeof(AvailExpr)); |
| 3766 | ae->tag = CCall; |
| 3767 | /* Ok to share only the cee, since it is immutable. */ |
| 3768 | ae->u.CCall.cee = e->Iex.CCall.cee; |
| 3769 | ae->u.CCall.retty = e->Iex.CCall.retty; |
| 3770 | /* irExprVec_to_TmpOrConsts will assert if the args are |
| 3771 | neither tmps nor constants, but that's ok .. that's all they |
| 3772 | should be. */ |
| 3773 | irExprVec_to_TmpOrConsts( |
| 3774 | &ae->u.CCall.args, &ae->u.CCall.nArgs, |
| 3775 | e->Iex.CCall.args |
| 3776 | ); |
| 3777 | return ae; |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 3778 | |
florian | c1b9e39 | 2012-09-20 02:40:57 +0000 | [diff] [blame] | 3779 | default: |
| 3780 | break; |
sewardj | e857804 | 2012-04-13 11:42:50 +0000 | [diff] [blame] | 3781 | } |
| 3782 | |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3783 | return NULL; |
| 3784 | } |
| 3785 | |
| 3786 | |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 3787 | /* The BB is modified in-place. Returns True if any changes were |
| 3788 | made. */ |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3789 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3790 | static Bool do_cse_BB ( IRSB* bb ) |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3791 | { |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 3792 | Int i, j, paranoia; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3793 | IRTemp t, q; |
| 3794 | IRStmt* st; |
| 3795 | AvailExpr* eprime; |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 3796 | AvailExpr* ae; |
| 3797 | Bool invalidate; |
| 3798 | Bool anyDone = False; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3799 | |
| 3800 | HashHW* tenv = newHHW(); /* :: IRTemp -> IRTemp */ |
| 3801 | HashHW* aenv = newHHW(); /* :: AvailExpr* -> IRTemp */ |
| 3802 | |
| 3803 | vassert(sizeof(IRTemp) <= sizeof(HWord)); |
| 3804 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3805 | if (0) { ppIRSB(bb); vex_printf("\n\n"); } |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3806 | |
| 3807 | /* Iterate forwards over the stmts. |
sewardj | cfe046e | 2013-01-17 14:23:53 +0000 | [diff] [blame] | 3808 | On seeing "t = E", where E is one of the AvailExpr forms: |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3809 | let E' = apply tenv substitution to E |
| 3810 | search aenv for E' |
| 3811 | if a mapping E' -> q is found, |
| 3812 | replace this stmt by "t = q" |
| 3813 | and add binding t -> q to tenv |
| 3814 | else |
| 3815 | add binding E' -> t to aenv |
| 3816 | replace this stmt by "t = E'" |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 3817 | |
| 3818 | Other statements are only interesting to the extent that they |
| 3819 | might invalidate some of the expressions in aenv. So there is |
| 3820 | an invalidate-bindings check for each statement seen. |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3821 | */ |
| 3822 | for (i = 0; i < bb->stmts_used; i++) { |
| 3823 | st = bb->stmts[i]; |
| 3824 | |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 3825 | /* ------ BEGIN invalidate aenv bindings ------ */ |
| 3826 | /* This is critical: remove from aenv any E' -> .. bindings |
| 3827 | which might be invalidated by this statement. The only |
sewardj | c4356f0 | 2007-11-09 21:15:04 +0000 | [diff] [blame] | 3828 | vulnerable kind of bindings are the GetI kind. |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 3829 | Dirty call - dump (paranoia level -> 2) |
| 3830 | Store - dump (ditto) |
| 3831 | Put, PutI - dump unless no-overlap is proven (.. -> 1) |
| 3832 | Uses getAliasingRelation_IC and getAliasingRelation_II |
| 3833 | to do the no-overlap assessments needed for Put/PutI. |
| 3834 | */ |
| 3835 | switch (st->tag) { |
sewardj | e768e92 | 2009-11-26 17:17:37 +0000 | [diff] [blame] | 3836 | case Ist_Dirty: case Ist_Store: case Ist_MBE: |
| 3837 | case Ist_CAS: case Ist_LLSC: |
sewardj | cfe046e | 2013-01-17 14:23:53 +0000 | [diff] [blame] | 3838 | case Ist_StoreG: |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 3839 | paranoia = 2; break; |
| 3840 | case Ist_Put: case Ist_PutI: |
| 3841 | paranoia = 1; break; |
| 3842 | case Ist_NoOp: case Ist_IMark: case Ist_AbiHint: |
sewardj | cfe046e | 2013-01-17 14:23:53 +0000 | [diff] [blame] | 3843 | case Ist_WrTmp: case Ist_Exit: case Ist_LoadG: |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 3844 | paranoia = 0; break; |
| 3845 | default: |
| 3846 | vpanic("do_cse_BB(1)"); |
| 3847 | } |
| 3848 | |
| 3849 | if (paranoia > 0) { |
| 3850 | for (j = 0; j < aenv->used; j++) { |
| 3851 | if (!aenv->inuse[j]) |
| 3852 | continue; |
| 3853 | ae = (AvailExpr*)aenv->key[j]; |
| 3854 | if (ae->tag != GetIt) |
| 3855 | continue; |
| 3856 | invalidate = False; |
| 3857 | if (paranoia >= 2) { |
| 3858 | invalidate = True; |
| 3859 | } else { |
| 3860 | vassert(paranoia == 1); |
| 3861 | if (st->tag == Ist_Put) { |
| 3862 | if (getAliasingRelation_IC( |
| 3863 | ae->u.GetIt.descr, |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3864 | IRExpr_RdTmp(ae->u.GetIt.ix), |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 3865 | st->Ist.Put.offset, |
| 3866 | typeOfIRExpr(bb->tyenv,st->Ist.Put.data) |
| 3867 | ) != NoAlias) |
| 3868 | invalidate = True; |
| 3869 | } |
| 3870 | else |
| 3871 | if (st->tag == Ist_PutI) { |
florian | d6f38b3 | 2012-05-31 15:46:18 +0000 | [diff] [blame] | 3872 | IRPutI *puti = st->Ist.PutI.details; |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 3873 | if (getAliasingRelation_II( |
| 3874 | ae->u.GetIt.descr, |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3875 | IRExpr_RdTmp(ae->u.GetIt.ix), |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 3876 | ae->u.GetIt.bias, |
florian | d6f38b3 | 2012-05-31 15:46:18 +0000 | [diff] [blame] | 3877 | puti->descr, |
| 3878 | puti->ix, |
| 3879 | puti->bias |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 3880 | ) != NoAlias) |
| 3881 | invalidate = True; |
| 3882 | } |
| 3883 | else |
| 3884 | vpanic("do_cse_BB(2)"); |
| 3885 | } |
| 3886 | |
| 3887 | if (invalidate) { |
| 3888 | aenv->inuse[j] = False; |
| 3889 | aenv->key[j] = (HWord)NULL; /* be sure */ |
| 3890 | } |
| 3891 | } /* for j */ |
| 3892 | } /* paranoia > 0 */ |
| 3893 | |
| 3894 | /* ------ ENV invalidate aenv bindings ------ */ |
| 3895 | |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3896 | /* ignore not-interestings */ |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3897 | if (st->tag != Ist_WrTmp) |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3898 | continue; |
| 3899 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3900 | t = st->Ist.WrTmp.tmp; |
| 3901 | eprime = irExpr_to_AvailExpr(st->Ist.WrTmp.data); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3902 | /* ignore if not of AvailExpr form */ |
| 3903 | if (!eprime) |
| 3904 | continue; |
| 3905 | |
| 3906 | /* vex_printf("considering: " ); ppIRStmt(st); vex_printf("\n"); */ |
| 3907 | |
| 3908 | /* apply tenv */ |
| 3909 | subst_AvailExpr( tenv, eprime ); |
| 3910 | |
| 3911 | /* search aenv for eprime, unfortunately the hard way */ |
| 3912 | for (j = 0; j < aenv->used; j++) |
| 3913 | if (aenv->inuse[j] && eq_AvailExpr(eprime, (AvailExpr*)aenv->key[j])) |
| 3914 | break; |
| 3915 | |
| 3916 | if (j < aenv->used) { |
| 3917 | /* A binding E' -> q was found. Replace stmt by "t = q" and |
| 3918 | note the t->q binding in tenv. */ |
| 3919 | /* (this is the core of the CSE action) */ |
| 3920 | q = (IRTemp)aenv->val[j]; |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3921 | bb->stmts[i] = IRStmt_WrTmp( t, IRExpr_RdTmp(q) ); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3922 | addToHHW( tenv, (HWord)t, (HWord)q ); |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 3923 | anyDone = True; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3924 | } else { |
| 3925 | /* No binding was found, so instead we add E' -> t to our |
| 3926 | collection of available expressions, replace this stmt |
| 3927 | with "t = E'", and move on. */ |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3928 | bb->stmts[i] = IRStmt_WrTmp( t, availExpr_to_IRExpr(eprime) ); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3929 | addToHHW( aenv, (HWord)eprime, (HWord)t ); |
| 3930 | } |
| 3931 | } |
| 3932 | |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 3933 | /* |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3934 | ppIRSB(bb); |
| 3935 | sanityCheckIRSB(bb, Ity_I32); |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 3936 | vex_printf("\n\n"); |
| 3937 | */ |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 3938 | return anyDone; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3939 | } |
| 3940 | |
| 3941 | |
| 3942 | /*---------------------------------------------------------------*/ |
| 3943 | /*--- Add32/Sub32 chain collapsing ---*/ |
| 3944 | /*---------------------------------------------------------------*/ |
| 3945 | |
| 3946 | /* ----- Helper functions for Add32/Sub32 chain collapsing ----- */ |
| 3947 | |
| 3948 | /* Is this expression "Add32(tmp,const)" or "Sub32(tmp,const)" ? If |
| 3949 | yes, set *tmp and *i32 appropriately. *i32 is set as if the |
| 3950 | root node is Add32, not Sub32. */ |
| 3951 | |
| 3952 | static Bool isAdd32OrSub32 ( IRExpr* e, IRTemp* tmp, Int* i32 ) |
| 3953 | { |
| 3954 | if (e->tag != Iex_Binop) |
| 3955 | return False; |
| 3956 | if (e->Iex.Binop.op != Iop_Add32 && e->Iex.Binop.op != Iop_Sub32) |
| 3957 | return False; |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3958 | if (e->Iex.Binop.arg1->tag != Iex_RdTmp) |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3959 | return False; |
| 3960 | if (e->Iex.Binop.arg2->tag != Iex_Const) |
| 3961 | return False; |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3962 | *tmp = e->Iex.Binop.arg1->Iex.RdTmp.tmp; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3963 | *i32 = (Int)(e->Iex.Binop.arg2->Iex.Const.con->Ico.U32); |
| 3964 | if (e->Iex.Binop.op == Iop_Sub32) |
| 3965 | *i32 = -*i32; |
| 3966 | return True; |
| 3967 | } |
| 3968 | |
| 3969 | |
| 3970 | /* Figure out if tmp can be expressed as tmp2 +32 const, for some |
| 3971 | other tmp2. Scan backwards from the specified start point -- an |
| 3972 | optimisation. */ |
| 3973 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3974 | static Bool collapseChain ( IRSB* bb, Int startHere, |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3975 | IRTemp tmp, |
| 3976 | IRTemp* tmp2, Int* i32 ) |
| 3977 | { |
| 3978 | Int j, ii; |
| 3979 | IRTemp vv; |
| 3980 | IRStmt* st; |
| 3981 | IRExpr* e; |
| 3982 | |
| 3983 | /* the (var, con) pair contain the current 'representation' for |
| 3984 | 'tmp'. We start with 'tmp + 0'. */ |
| 3985 | IRTemp var = tmp; |
| 3986 | Int con = 0; |
| 3987 | |
| 3988 | /* Scan backwards to see if tmp can be replaced by some other tmp |
| 3989 | +/- a constant. */ |
| 3990 | for (j = startHere; j >= 0; j--) { |
| 3991 | st = bb->stmts[j]; |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3992 | if (st->tag != Ist_WrTmp) |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3993 | continue; |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3994 | if (st->Ist.WrTmp.tmp != var) |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3995 | continue; |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 3996 | e = st->Ist.WrTmp.data; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 3997 | if (!isAdd32OrSub32(e, &vv, &ii)) |
| 3998 | break; |
| 3999 | var = vv; |
| 4000 | con += ii; |
| 4001 | } |
| 4002 | if (j == -1) |
| 4003 | /* no earlier binding for var .. ill-formed IR */ |
| 4004 | vpanic("collapseChain"); |
| 4005 | |
| 4006 | /* so, did we find anything interesting? */ |
| 4007 | if (var == tmp) |
| 4008 | return False; /* no .. */ |
| 4009 | |
| 4010 | *tmp2 = var; |
| 4011 | *i32 = con; |
| 4012 | return True; |
| 4013 | } |
| 4014 | |
| 4015 | |
| 4016 | /* ------- Main function for Add32/Sub32 chain collapsing ------ */ |
| 4017 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4018 | static void collapse_AddSub_chains_BB ( IRSB* bb ) |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4019 | { |
| 4020 | IRStmt *st; |
| 4021 | IRTemp var, var2; |
| 4022 | Int i, con, con2; |
| 4023 | |
| 4024 | for (i = bb->stmts_used-1; i >= 0; i--) { |
| 4025 | st = bb->stmts[i]; |
sewardj | 8bee6d1 | 2005-03-22 02:24:05 +0000 | [diff] [blame] | 4026 | if (st->tag == Ist_NoOp) |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4027 | continue; |
| 4028 | |
| 4029 | /* Try to collapse 't1 = Add32/Sub32(t2, con)'. */ |
| 4030 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4031 | if (st->tag == Ist_WrTmp |
| 4032 | && isAdd32OrSub32(st->Ist.WrTmp.data, &var, &con)) { |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4033 | |
| 4034 | /* So e1 is of the form Add32(var,con) or Sub32(var,-con). |
| 4035 | Find out if var can be expressed as var2 + con2. */ |
| 4036 | if (collapseChain(bb, i-1, var, &var2, &con2)) { |
| 4037 | if (DEBUG_IROPT) { |
| 4038 | vex_printf("replacing1 "); |
| 4039 | ppIRStmt(st); |
| 4040 | vex_printf(" with "); |
| 4041 | } |
| 4042 | con2 += con; |
| 4043 | bb->stmts[i] |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4044 | = IRStmt_WrTmp( |
| 4045 | st->Ist.WrTmp.tmp, |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4046 | (con2 >= 0) |
| 4047 | ? IRExpr_Binop(Iop_Add32, |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4048 | IRExpr_RdTmp(var2), |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4049 | IRExpr_Const(IRConst_U32(con2))) |
| 4050 | : IRExpr_Binop(Iop_Sub32, |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4051 | IRExpr_RdTmp(var2), |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4052 | IRExpr_Const(IRConst_U32(-con2))) |
| 4053 | ); |
| 4054 | if (DEBUG_IROPT) { |
| 4055 | ppIRStmt(bb->stmts[i]); |
| 4056 | vex_printf("\n"); |
| 4057 | } |
| 4058 | } |
| 4059 | |
| 4060 | continue; |
| 4061 | } |
| 4062 | |
| 4063 | /* Try to collapse 't1 = GetI[t2, con]'. */ |
| 4064 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4065 | if (st->tag == Ist_WrTmp |
| 4066 | && st->Ist.WrTmp.data->tag == Iex_GetI |
| 4067 | && st->Ist.WrTmp.data->Iex.GetI.ix->tag == Iex_RdTmp |
| 4068 | && collapseChain(bb, i-1, st->Ist.WrTmp.data->Iex.GetI.ix |
| 4069 | ->Iex.RdTmp.tmp, &var2, &con2)) { |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4070 | if (DEBUG_IROPT) { |
| 4071 | vex_printf("replacing3 "); |
| 4072 | ppIRStmt(st); |
| 4073 | vex_printf(" with "); |
| 4074 | } |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4075 | con2 += st->Ist.WrTmp.data->Iex.GetI.bias; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4076 | bb->stmts[i] |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4077 | = IRStmt_WrTmp( |
| 4078 | st->Ist.WrTmp.tmp, |
| 4079 | IRExpr_GetI(st->Ist.WrTmp.data->Iex.GetI.descr, |
| 4080 | IRExpr_RdTmp(var2), |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4081 | con2)); |
| 4082 | if (DEBUG_IROPT) { |
| 4083 | ppIRStmt(bb->stmts[i]); |
| 4084 | vex_printf("\n"); |
| 4085 | } |
| 4086 | continue; |
| 4087 | } |
| 4088 | |
| 4089 | /* Perhaps st is PutI[t, con] ? */ |
florian | d6f38b3 | 2012-05-31 15:46:18 +0000 | [diff] [blame] | 4090 | IRPutI *puti = st->Ist.PutI.details; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4091 | if (st->tag == Ist_PutI |
florian | d6f38b3 | 2012-05-31 15:46:18 +0000 | [diff] [blame] | 4092 | && puti->ix->tag == Iex_RdTmp |
| 4093 | && collapseChain(bb, i-1, puti->ix->Iex.RdTmp.tmp, |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4094 | &var2, &con2)) { |
| 4095 | if (DEBUG_IROPT) { |
| 4096 | vex_printf("replacing2 "); |
| 4097 | ppIRStmt(st); |
| 4098 | vex_printf(" with "); |
| 4099 | } |
florian | d6f38b3 | 2012-05-31 15:46:18 +0000 | [diff] [blame] | 4100 | con2 += puti->bias; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4101 | bb->stmts[i] |
florian | d6f38b3 | 2012-05-31 15:46:18 +0000 | [diff] [blame] | 4102 | = IRStmt_PutI(mkIRPutI(puti->descr, |
| 4103 | IRExpr_RdTmp(var2), |
| 4104 | con2, |
| 4105 | puti->data)); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4106 | if (DEBUG_IROPT) { |
| 4107 | ppIRStmt(bb->stmts[i]); |
| 4108 | vex_printf("\n"); |
| 4109 | } |
| 4110 | continue; |
| 4111 | } |
| 4112 | |
| 4113 | } /* for */ |
| 4114 | } |
| 4115 | |
| 4116 | |
| 4117 | /*---------------------------------------------------------------*/ |
| 4118 | /*--- PutI/GetI transformations ---*/ |
| 4119 | /*---------------------------------------------------------------*/ |
| 4120 | |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4121 | /* Given the parts (descr, tmp, bias) for a GetI, scan backwards from |
| 4122 | the given starting point to find, if any, a PutI which writes |
| 4123 | exactly the same piece of guest state, and so return the expression |
| 4124 | that the PutI writes. This is the core of PutI-GetI forwarding. */ |
| 4125 | |
| 4126 | static |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4127 | IRExpr* findPutI ( IRSB* bb, Int startHere, |
| 4128 | IRRegArray* descrG, IRExpr* ixG, Int biasG ) |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4129 | { |
| 4130 | Int j; |
| 4131 | IRStmt* st; |
| 4132 | GSAliasing relation; |
| 4133 | |
| 4134 | if (0) { |
| 4135 | vex_printf("\nfindPutI "); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4136 | ppIRRegArray(descrG); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4137 | vex_printf(" "); |
| 4138 | ppIRExpr(ixG); |
| 4139 | vex_printf(" %d\n", biasG); |
| 4140 | } |
| 4141 | |
| 4142 | /* Scan backwards in bb from startHere to find a suitable PutI |
| 4143 | binding for (descrG, ixG, biasG), if any. */ |
| 4144 | |
| 4145 | for (j = startHere; j >= 0; j--) { |
| 4146 | st = bb->stmts[j]; |
sewardj | 8bee6d1 | 2005-03-22 02:24:05 +0000 | [diff] [blame] | 4147 | if (st->tag == Ist_NoOp) |
| 4148 | continue; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4149 | |
| 4150 | if (st->tag == Ist_Put) { |
| 4151 | /* Non-indexed Put. This can't give a binding, but we do |
| 4152 | need to check it doesn't invalidate the search by |
| 4153 | overlapping any part of the indexed guest state. */ |
| 4154 | |
| 4155 | relation |
| 4156 | = getAliasingRelation_IC( |
| 4157 | descrG, ixG, |
| 4158 | st->Ist.Put.offset, |
| 4159 | typeOfIRExpr(bb->tyenv,st->Ist.Put.data) ); |
| 4160 | |
| 4161 | if (relation == NoAlias) { |
| 4162 | /* we're OK; keep going */ |
| 4163 | continue; |
| 4164 | } else { |
| 4165 | /* relation == UnknownAlias || relation == ExactAlias */ |
| 4166 | /* If this assertion fails, we've found a Put which writes |
| 4167 | an area of guest state which is read by a GetI. Which |
| 4168 | is unlikely (although not per se wrong). */ |
| 4169 | vassert(relation != ExactAlias); |
| 4170 | /* This Put potentially writes guest state that the GetI |
| 4171 | reads; we must fail. */ |
| 4172 | return NULL; |
| 4173 | } |
| 4174 | } |
| 4175 | |
| 4176 | if (st->tag == Ist_PutI) { |
florian | d6f38b3 | 2012-05-31 15:46:18 +0000 | [diff] [blame] | 4177 | IRPutI *puti = st->Ist.PutI.details; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4178 | |
| 4179 | relation = getAliasingRelation_II( |
| 4180 | descrG, ixG, biasG, |
florian | d6f38b3 | 2012-05-31 15:46:18 +0000 | [diff] [blame] | 4181 | puti->descr, |
| 4182 | puti->ix, |
| 4183 | puti->bias |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4184 | ); |
| 4185 | |
| 4186 | if (relation == NoAlias) { |
| 4187 | /* This PutI definitely doesn't overlap. Ignore it and |
| 4188 | keep going. */ |
| 4189 | continue; /* the for j loop */ |
| 4190 | } |
| 4191 | |
| 4192 | if (relation == UnknownAlias) { |
| 4193 | /* We don't know if this PutI writes to the same guest |
| 4194 | state that the GetI, or not. So we have to give up. */ |
| 4195 | return NULL; |
| 4196 | } |
| 4197 | |
| 4198 | /* Otherwise, we've found what we're looking for. */ |
| 4199 | vassert(relation == ExactAlias); |
florian | d6f38b3 | 2012-05-31 15:46:18 +0000 | [diff] [blame] | 4200 | return puti->data; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4201 | |
| 4202 | } /* if (st->tag == Ist_PutI) */ |
| 4203 | |
| 4204 | if (st->tag == Ist_Dirty) { |
| 4205 | /* Be conservative. If the dirty call has any guest effects at |
| 4206 | all, give up. We could do better -- only give up if there |
| 4207 | are any guest writes/modifies. */ |
| 4208 | if (st->Ist.Dirty.details->nFxState > 0) |
| 4209 | return NULL; |
| 4210 | } |
| 4211 | |
| 4212 | } /* for */ |
| 4213 | |
| 4214 | /* No valid replacement was found. */ |
| 4215 | return NULL; |
| 4216 | } |
| 4217 | |
| 4218 | |
| 4219 | |
| 4220 | /* Assuming pi is a PutI stmt, is s2 identical to it (in the sense |
| 4221 | that it writes exactly the same piece of guest state) ? Safe |
| 4222 | answer: False. */ |
| 4223 | |
| 4224 | static Bool identicalPutIs ( IRStmt* pi, IRStmt* s2 ) |
| 4225 | { |
| 4226 | vassert(pi->tag == Ist_PutI); |
| 4227 | if (s2->tag != Ist_PutI) |
| 4228 | return False; |
| 4229 | |
florian | d6f38b3 | 2012-05-31 15:46:18 +0000 | [diff] [blame] | 4230 | IRPutI *p1 = pi->Ist.PutI.details; |
| 4231 | IRPutI *p2 = s2->Ist.PutI.details; |
| 4232 | |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 4233 | return toBool( |
| 4234 | getAliasingRelation_II( |
florian | d6f38b3 | 2012-05-31 15:46:18 +0000 | [diff] [blame] | 4235 | p1->descr, p1->ix, p1->bias, |
| 4236 | p2->descr, p2->ix, p2->bias |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4237 | ) |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 4238 | == ExactAlias |
| 4239 | ); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4240 | } |
| 4241 | |
| 4242 | |
| 4243 | /* Assuming pi is a PutI stmt, is s2 a Get/GetI/Put/PutI which might |
| 4244 | overlap it? Safe answer: True. Note, we could do a lot better |
| 4245 | than this if needed. */ |
| 4246 | |
| 4247 | static |
| 4248 | Bool guestAccessWhichMightOverlapPutI ( |
| 4249 | IRTypeEnv* tyenv, IRStmt* pi, IRStmt* s2 |
| 4250 | ) |
| 4251 | { |
| 4252 | GSAliasing relation; |
| 4253 | UInt minoffP, maxoffP; |
| 4254 | |
| 4255 | vassert(pi->tag == Ist_PutI); |
florian | d6f38b3 | 2012-05-31 15:46:18 +0000 | [diff] [blame] | 4256 | |
| 4257 | IRPutI *p1 = pi->Ist.PutI.details; |
| 4258 | |
| 4259 | getArrayBounds(p1->descr, &minoffP, &maxoffP); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4260 | switch (s2->tag) { |
| 4261 | |
sewardj | d2445f6 | 2005-03-21 00:15:53 +0000 | [diff] [blame] | 4262 | case Ist_NoOp: |
sewardj | f168931 | 2005-03-16 18:19:10 +0000 | [diff] [blame] | 4263 | case Ist_IMark: |
| 4264 | return False; |
| 4265 | |
sewardj | c4356f0 | 2007-11-09 21:15:04 +0000 | [diff] [blame] | 4266 | case Ist_MBE: |
sewardj | 5a9ffab | 2005-05-12 17:55:01 +0000 | [diff] [blame] | 4267 | case Ist_AbiHint: |
| 4268 | /* just be paranoid ... these should be rare. */ |
sewardj | bb3f52d | 2005-01-07 14:14:50 +0000 | [diff] [blame] | 4269 | return True; |
| 4270 | |
sewardj | e9d8a26 | 2009-07-01 08:06:34 +0000 | [diff] [blame] | 4271 | case Ist_CAS: |
| 4272 | /* This is unbelievably lame, but it's probably not |
| 4273 | significant from a performance point of view. Really, a |
| 4274 | CAS is a load-store op, so it should be safe to say False. |
| 4275 | However .. */ |
| 4276 | return True; |
| 4277 | |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4278 | case Ist_Dirty: |
| 4279 | /* If the dirty call has any guest effects at all, give up. |
| 4280 | Probably could do better. */ |
| 4281 | if (s2->Ist.Dirty.details->nFxState > 0) |
| 4282 | return True; |
| 4283 | return False; |
| 4284 | |
| 4285 | case Ist_Put: |
sewardj | 496a58d | 2005-03-20 18:44:44 +0000 | [diff] [blame] | 4286 | vassert(isIRAtom(s2->Ist.Put.data)); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4287 | relation |
| 4288 | = getAliasingRelation_IC( |
florian | d6f38b3 | 2012-05-31 15:46:18 +0000 | [diff] [blame] | 4289 | p1->descr, p1->ix, |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4290 | s2->Ist.Put.offset, |
| 4291 | typeOfIRExpr(tyenv,s2->Ist.Put.data) |
| 4292 | ); |
| 4293 | goto have_relation; |
| 4294 | |
florian | d6f38b3 | 2012-05-31 15:46:18 +0000 | [diff] [blame] | 4295 | case Ist_PutI: { |
| 4296 | IRPutI *p2 = s2->Ist.PutI.details; |
| 4297 | |
| 4298 | vassert(isIRAtom(p2->ix)); |
| 4299 | vassert(isIRAtom(p2->data)); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4300 | relation |
| 4301 | = getAliasingRelation_II( |
florian | d6f38b3 | 2012-05-31 15:46:18 +0000 | [diff] [blame] | 4302 | p1->descr, p1->ix, p1->bias, |
| 4303 | p2->descr, p2->ix, p2->bias |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4304 | ); |
| 4305 | goto have_relation; |
florian | d6f38b3 | 2012-05-31 15:46:18 +0000 | [diff] [blame] | 4306 | } |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4307 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4308 | case Ist_WrTmp: |
| 4309 | if (s2->Ist.WrTmp.data->tag == Iex_GetI) { |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4310 | relation |
| 4311 | = getAliasingRelation_II( |
florian | d6f38b3 | 2012-05-31 15:46:18 +0000 | [diff] [blame] | 4312 | p1->descr, p1->ix, p1->bias, |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4313 | s2->Ist.WrTmp.data->Iex.GetI.descr, |
| 4314 | s2->Ist.WrTmp.data->Iex.GetI.ix, |
| 4315 | s2->Ist.WrTmp.data->Iex.GetI.bias |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4316 | ); |
| 4317 | goto have_relation; |
| 4318 | } |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4319 | if (s2->Ist.WrTmp.data->tag == Iex_Get) { |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4320 | relation |
| 4321 | = getAliasingRelation_IC( |
florian | d6f38b3 | 2012-05-31 15:46:18 +0000 | [diff] [blame] | 4322 | p1->descr, p1->ix, |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4323 | s2->Ist.WrTmp.data->Iex.Get.offset, |
| 4324 | s2->Ist.WrTmp.data->Iex.Get.ty |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4325 | ); |
| 4326 | goto have_relation; |
| 4327 | } |
| 4328 | return False; |
| 4329 | |
sewardj | af1ceca | 2005-06-30 23:31:27 +0000 | [diff] [blame] | 4330 | case Ist_Store: |
| 4331 | vassert(isIRAtom(s2->Ist.Store.addr)); |
| 4332 | vassert(isIRAtom(s2->Ist.Store.data)); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4333 | return False; |
| 4334 | |
| 4335 | default: |
| 4336 | vex_printf("\n"); ppIRStmt(s2); vex_printf("\n"); |
| 4337 | vpanic("guestAccessWhichMightOverlapPutI"); |
| 4338 | } |
| 4339 | |
| 4340 | have_relation: |
| 4341 | if (relation == NoAlias) |
| 4342 | return False; |
| 4343 | else |
| 4344 | return True; /* ExactAlias or UnknownAlias */ |
| 4345 | } |
| 4346 | |
| 4347 | |
| 4348 | |
| 4349 | /* ---------- PutI/GetI transformations main functions --------- */ |
| 4350 | |
| 4351 | /* Remove redundant GetIs, to the extent that they can be detected. |
| 4352 | bb is modified in-place. */ |
| 4353 | |
| 4354 | static |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4355 | void do_redundant_GetI_elimination ( IRSB* bb ) |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4356 | { |
| 4357 | Int i; |
| 4358 | IRStmt* st; |
| 4359 | |
| 4360 | for (i = bb->stmts_used-1; i >= 0; i--) { |
| 4361 | st = bb->stmts[i]; |
sewardj | 8bee6d1 | 2005-03-22 02:24:05 +0000 | [diff] [blame] | 4362 | if (st->tag == Ist_NoOp) |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4363 | continue; |
| 4364 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4365 | if (st->tag == Ist_WrTmp |
| 4366 | && st->Ist.WrTmp.data->tag == Iex_GetI |
| 4367 | && st->Ist.WrTmp.data->Iex.GetI.ix->tag == Iex_RdTmp) { |
| 4368 | IRRegArray* descr = st->Ist.WrTmp.data->Iex.GetI.descr; |
| 4369 | IRExpr* ix = st->Ist.WrTmp.data->Iex.GetI.ix; |
| 4370 | Int bias = st->Ist.WrTmp.data->Iex.GetI.bias; |
| 4371 | IRExpr* replacement = findPutI(bb, i-1, descr, ix, bias); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4372 | if (replacement |
sewardj | 496a58d | 2005-03-20 18:44:44 +0000 | [diff] [blame] | 4373 | && isIRAtom(replacement) |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4374 | /* Make sure we're doing a type-safe transformation! */ |
| 4375 | && typeOfIRExpr(bb->tyenv, replacement) == descr->elemTy) { |
| 4376 | if (DEBUG_IROPT) { |
| 4377 | vex_printf("rGI: "); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4378 | ppIRExpr(st->Ist.WrTmp.data); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4379 | vex_printf(" -> "); |
| 4380 | ppIRExpr(replacement); |
| 4381 | vex_printf("\n"); |
| 4382 | } |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4383 | bb->stmts[i] = IRStmt_WrTmp(st->Ist.WrTmp.tmp, replacement); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4384 | } |
| 4385 | } |
| 4386 | } |
| 4387 | |
| 4388 | } |
| 4389 | |
| 4390 | |
| 4391 | /* Remove redundant PutIs, to the extent which they can be detected. |
| 4392 | bb is modified in-place. */ |
| 4393 | |
| 4394 | static |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4395 | void do_redundant_PutI_elimination ( IRSB* bb ) |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4396 | { |
| 4397 | Int i, j; |
| 4398 | Bool delete; |
| 4399 | IRStmt *st, *stj; |
| 4400 | |
philippe | 6c46bef | 2012-08-14 22:29:01 +0000 | [diff] [blame] | 4401 | vassert(vex_control.iropt_register_updates < VexRegUpdAllregsAtEachInsn); |
philippe | c8e2f98 | 2012-08-01 22:04:13 +0000 | [diff] [blame] | 4402 | |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4403 | for (i = 0; i < bb->stmts_used; i++) { |
| 4404 | st = bb->stmts[i]; |
sewardj | 8bee6d1 | 2005-03-22 02:24:05 +0000 | [diff] [blame] | 4405 | if (st->tag != Ist_PutI) |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4406 | continue; |
| 4407 | /* Ok, search forwards from here to see if we can find another |
| 4408 | PutI which makes this one redundant, and dodging various |
| 4409 | hazards. Search forwards: |
| 4410 | * If conditional exit, give up (because anything after that |
| 4411 | does not postdominate this put). |
| 4412 | * If a Get which might overlap, give up (because this PutI |
| 4413 | not necessarily dead). |
| 4414 | * If a Put which is identical, stop with success. |
| 4415 | * If a Put which might overlap, but is not identical, give up. |
| 4416 | * If a dirty helper call which might write guest state, give up. |
| 4417 | * If a Put which definitely doesn't overlap, or any other |
| 4418 | kind of stmt, continue. |
| 4419 | */ |
| 4420 | delete = False; |
| 4421 | for (j = i+1; j < bb->stmts_used; j++) { |
| 4422 | stj = bb->stmts[j]; |
sewardj | 8bee6d1 | 2005-03-22 02:24:05 +0000 | [diff] [blame] | 4423 | if (stj->tag == Ist_NoOp) |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4424 | continue; |
| 4425 | if (identicalPutIs(st, stj)) { |
| 4426 | /* success! */ |
| 4427 | delete = True; |
| 4428 | break; |
| 4429 | } |
| 4430 | if (stj->tag == Ist_Exit) |
| 4431 | /* give up */ |
| 4432 | break; |
| 4433 | if (st->tag == Ist_Dirty) |
| 4434 | /* give up; could do better here */ |
| 4435 | break; |
| 4436 | if (guestAccessWhichMightOverlapPutI(bb->tyenv, st, stj)) |
| 4437 | /* give up */ |
| 4438 | break; |
| 4439 | } |
| 4440 | |
| 4441 | if (delete) { |
| 4442 | if (DEBUG_IROPT) { |
| 4443 | vex_printf("rPI: "); |
| 4444 | ppIRStmt(st); |
| 4445 | vex_printf("\n"); |
| 4446 | } |
sewardj | d2445f6 | 2005-03-21 00:15:53 +0000 | [diff] [blame] | 4447 | bb->stmts[i] = IRStmt_NoOp(); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4448 | } |
| 4449 | |
| 4450 | } |
| 4451 | } |
| 4452 | |
| 4453 | |
| 4454 | /*---------------------------------------------------------------*/ |
| 4455 | /*--- Loop unrolling ---*/ |
| 4456 | /*---------------------------------------------------------------*/ |
| 4457 | |
| 4458 | /* Adjust all tmp values (names) in e by delta. e is destructively |
| 4459 | modified. */ |
| 4460 | |
| 4461 | static void deltaIRExpr ( IRExpr* e, Int delta ) |
| 4462 | { |
| 4463 | Int i; |
| 4464 | switch (e->tag) { |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4465 | case Iex_RdTmp: |
| 4466 | e->Iex.RdTmp.tmp += delta; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4467 | break; |
| 4468 | case Iex_Get: |
| 4469 | case Iex_Const: |
| 4470 | break; |
| 4471 | case Iex_GetI: |
| 4472 | deltaIRExpr(e->Iex.GetI.ix, delta); |
| 4473 | break; |
sewardj | 1a866b4 | 2006-02-09 02:54:03 +0000 | [diff] [blame] | 4474 | case Iex_Qop: |
florian | 96d7cc3 | 2012-06-01 20:41:24 +0000 | [diff] [blame] | 4475 | deltaIRExpr(e->Iex.Qop.details->arg1, delta); |
| 4476 | deltaIRExpr(e->Iex.Qop.details->arg2, delta); |
| 4477 | deltaIRExpr(e->Iex.Qop.details->arg3, delta); |
| 4478 | deltaIRExpr(e->Iex.Qop.details->arg4, delta); |
sewardj | 1a866b4 | 2006-02-09 02:54:03 +0000 | [diff] [blame] | 4479 | break; |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 4480 | case Iex_Triop: |
florian | 420bfa9 | 2012-06-02 20:29:22 +0000 | [diff] [blame] | 4481 | deltaIRExpr(e->Iex.Triop.details->arg1, delta); |
| 4482 | deltaIRExpr(e->Iex.Triop.details->arg2, delta); |
| 4483 | deltaIRExpr(e->Iex.Triop.details->arg3, delta); |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 4484 | break; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4485 | case Iex_Binop: |
| 4486 | deltaIRExpr(e->Iex.Binop.arg1, delta); |
| 4487 | deltaIRExpr(e->Iex.Binop.arg2, delta); |
| 4488 | break; |
| 4489 | case Iex_Unop: |
| 4490 | deltaIRExpr(e->Iex.Unop.arg, delta); |
| 4491 | break; |
sewardj | af1ceca | 2005-06-30 23:31:27 +0000 | [diff] [blame] | 4492 | case Iex_Load: |
| 4493 | deltaIRExpr(e->Iex.Load.addr, delta); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4494 | break; |
| 4495 | case Iex_CCall: |
| 4496 | for (i = 0; e->Iex.CCall.args[i]; i++) |
| 4497 | deltaIRExpr(e->Iex.CCall.args[i], delta); |
| 4498 | break; |
florian | 99dd03e | 2013-01-29 03:56:06 +0000 | [diff] [blame] | 4499 | case Iex_ITE: |
| 4500 | deltaIRExpr(e->Iex.ITE.cond, delta); |
| 4501 | deltaIRExpr(e->Iex.ITE.iftrue, delta); |
| 4502 | deltaIRExpr(e->Iex.ITE.iffalse, delta); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4503 | break; |
| 4504 | default: |
| 4505 | vex_printf("\n"); ppIRExpr(e); vex_printf("\n"); |
| 4506 | vpanic("deltaIRExpr"); |
| 4507 | } |
| 4508 | } |
| 4509 | |
| 4510 | /* Adjust all tmp values (names) in st by delta. st is destructively |
| 4511 | modified. */ |
| 4512 | |
| 4513 | static void deltaIRStmt ( IRStmt* st, Int delta ) |
| 4514 | { |
| 4515 | Int i; |
| 4516 | IRDirty* d; |
| 4517 | switch (st->tag) { |
sewardj | d2445f6 | 2005-03-21 00:15:53 +0000 | [diff] [blame] | 4518 | case Ist_NoOp: |
sewardj | f168931 | 2005-03-16 18:19:10 +0000 | [diff] [blame] | 4519 | case Ist_IMark: |
sewardj | c4356f0 | 2007-11-09 21:15:04 +0000 | [diff] [blame] | 4520 | case Ist_MBE: |
sewardj | f168931 | 2005-03-16 18:19:10 +0000 | [diff] [blame] | 4521 | break; |
sewardj | 5a9ffab | 2005-05-12 17:55:01 +0000 | [diff] [blame] | 4522 | case Ist_AbiHint: |
| 4523 | deltaIRExpr(st->Ist.AbiHint.base, delta); |
sewardj | 478646f | 2008-05-01 20:13:04 +0000 | [diff] [blame] | 4524 | deltaIRExpr(st->Ist.AbiHint.nia, delta); |
sewardj | 5a9ffab | 2005-05-12 17:55:01 +0000 | [diff] [blame] | 4525 | break; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4526 | case Ist_Put: |
| 4527 | deltaIRExpr(st->Ist.Put.data, delta); |
| 4528 | break; |
| 4529 | case Ist_PutI: |
florian | d6f38b3 | 2012-05-31 15:46:18 +0000 | [diff] [blame] | 4530 | deltaIRExpr(st->Ist.PutI.details->ix, delta); |
| 4531 | deltaIRExpr(st->Ist.PutI.details->data, delta); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4532 | break; |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4533 | case Ist_WrTmp: |
| 4534 | st->Ist.WrTmp.tmp += delta; |
| 4535 | deltaIRExpr(st->Ist.WrTmp.data, delta); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4536 | break; |
| 4537 | case Ist_Exit: |
| 4538 | deltaIRExpr(st->Ist.Exit.guard, delta); |
| 4539 | break; |
sewardj | af1ceca | 2005-06-30 23:31:27 +0000 | [diff] [blame] | 4540 | case Ist_Store: |
| 4541 | deltaIRExpr(st->Ist.Store.addr, delta); |
| 4542 | deltaIRExpr(st->Ist.Store.data, delta); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4543 | break; |
sewardj | cfe046e | 2013-01-17 14:23:53 +0000 | [diff] [blame] | 4544 | case Ist_StoreG: { |
| 4545 | IRStoreG* sg = st->Ist.StoreG.details; |
| 4546 | deltaIRExpr(sg->addr, delta); |
| 4547 | deltaIRExpr(sg->data, delta); |
| 4548 | deltaIRExpr(sg->guard, delta); |
| 4549 | break; |
| 4550 | } |
| 4551 | case Ist_LoadG: { |
| 4552 | IRLoadG* lg = st->Ist.LoadG.details; |
| 4553 | lg->dst += delta; |
| 4554 | deltaIRExpr(lg->addr, delta); |
| 4555 | deltaIRExpr(lg->alt, delta); |
| 4556 | deltaIRExpr(lg->guard, delta); |
| 4557 | break; |
| 4558 | } |
sewardj | e9d8a26 | 2009-07-01 08:06:34 +0000 | [diff] [blame] | 4559 | case Ist_CAS: |
| 4560 | if (st->Ist.CAS.details->oldHi != IRTemp_INVALID) |
| 4561 | st->Ist.CAS.details->oldHi += delta; |
| 4562 | st->Ist.CAS.details->oldLo += delta; |
| 4563 | deltaIRExpr(st->Ist.CAS.details->addr, delta); |
| 4564 | if (st->Ist.CAS.details->expdHi) |
| 4565 | deltaIRExpr(st->Ist.CAS.details->expdHi, delta); |
| 4566 | deltaIRExpr(st->Ist.CAS.details->expdLo, delta); |
| 4567 | if (st->Ist.CAS.details->dataHi) |
| 4568 | deltaIRExpr(st->Ist.CAS.details->dataHi, delta); |
| 4569 | deltaIRExpr(st->Ist.CAS.details->dataLo, delta); |
| 4570 | break; |
sewardj | e768e92 | 2009-11-26 17:17:37 +0000 | [diff] [blame] | 4571 | case Ist_LLSC: |
| 4572 | st->Ist.LLSC.result += delta; |
| 4573 | deltaIRExpr(st->Ist.LLSC.addr, delta); |
| 4574 | if (st->Ist.LLSC.storedata) |
| 4575 | deltaIRExpr(st->Ist.LLSC.storedata, delta); |
| 4576 | break; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4577 | case Ist_Dirty: |
| 4578 | d = st->Ist.Dirty.details; |
| 4579 | deltaIRExpr(d->guard, delta); |
florian | 2accb5e | 2013-09-03 21:48:02 +0000 | [diff] [blame] | 4580 | for (i = 0; d->args[i]; i++) { |
| 4581 | IRExpr* arg = d->args[i]; |
| 4582 | if (LIKELY(!is_IRExpr_VECRET_or_BBPTR(arg))) |
| 4583 | deltaIRExpr(arg, delta); |
| 4584 | } |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4585 | if (d->tmp != IRTemp_INVALID) |
| 4586 | d->tmp += delta; |
| 4587 | if (d->mAddr) |
| 4588 | deltaIRExpr(d->mAddr, delta); |
| 4589 | break; |
| 4590 | default: |
| 4591 | vex_printf("\n"); ppIRStmt(st); vex_printf("\n"); |
| 4592 | vpanic("deltaIRStmt"); |
| 4593 | } |
| 4594 | } |
| 4595 | |
| 4596 | |
| 4597 | /* If possible, return a loop-unrolled version of bb0. The original |
| 4598 | is changed. If not possible, return NULL. */ |
| 4599 | |
| 4600 | /* The two schemas considered are: |
| 4601 | |
| 4602 | X: BODY; goto X |
| 4603 | |
| 4604 | which unrolls to (eg) X: BODY;BODY; goto X |
| 4605 | |
| 4606 | and |
| 4607 | |
| 4608 | X: BODY; if (c) goto X; goto Y |
| 4609 | which trivially transforms to |
| 4610 | X: BODY; if (!c) goto Y; goto X; |
| 4611 | so it falls in the scope of the first case. |
| 4612 | |
| 4613 | X and Y must be literal (guest) addresses. |
| 4614 | */ |
| 4615 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4616 | static Int calc_unroll_factor( IRSB* bb ) |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4617 | { |
| 4618 | Int n_stmts, i; |
| 4619 | |
| 4620 | n_stmts = 0; |
sewardj | 0ddbf79 | 2005-04-04 23:12:54 +0000 | [diff] [blame] | 4621 | for (i = 0; i < bb->stmts_used; i++) { |
| 4622 | if (bb->stmts[i]->tag != Ist_NoOp) |
| 4623 | n_stmts++; |
| 4624 | } |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4625 | |
| 4626 | if (n_stmts <= vex_control.iropt_unroll_thresh/8) { |
| 4627 | if (vex_control.iropt_verbosity > 0) |
| 4628 | vex_printf("vex iropt: 8 x unrolling (%d sts -> %d sts)\n", |
| 4629 | n_stmts, 8* n_stmts); |
| 4630 | return 8; |
| 4631 | } |
| 4632 | if (n_stmts <= vex_control.iropt_unroll_thresh/4) { |
| 4633 | if (vex_control.iropt_verbosity > 0) |
| 4634 | vex_printf("vex iropt: 4 x unrolling (%d sts -> %d sts)\n", |
| 4635 | n_stmts, 4* n_stmts); |
| 4636 | return 4; |
| 4637 | } |
| 4638 | |
| 4639 | if (n_stmts <= vex_control.iropt_unroll_thresh/2) { |
| 4640 | if (vex_control.iropt_verbosity > 0) |
| 4641 | vex_printf("vex iropt: 2 x unrolling (%d sts -> %d sts)\n", |
| 4642 | n_stmts, 2* n_stmts); |
| 4643 | return 2; |
| 4644 | } |
| 4645 | |
| 4646 | if (vex_control.iropt_verbosity > 0) |
| 4647 | vex_printf("vex iropt: not unrolling (%d sts)\n", n_stmts); |
| 4648 | |
| 4649 | return 1; |
| 4650 | } |
| 4651 | |
| 4652 | |
florian | d4cc0de | 2015-01-02 11:44:12 +0000 | [diff] [blame] | 4653 | static IRSB* maybe_loop_unroll_BB ( IRSB* bb0, Addr my_addr ) |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4654 | { |
| 4655 | Int i, j, jmax, n_vars; |
| 4656 | Bool xxx_known; |
| 4657 | Addr64 xxx_value, yyy_value; |
| 4658 | IRExpr* udst; |
| 4659 | IRStmt* st; |
| 4660 | IRConst* con; |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4661 | IRSB *bb1, *bb2; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4662 | Int unroll_factor; |
| 4663 | |
| 4664 | if (vex_control.iropt_unroll_thresh <= 0) |
| 4665 | return NULL; |
| 4666 | |
| 4667 | /* First off, figure out if we can unroll this loop. Do this |
| 4668 | without modifying bb0. */ |
| 4669 | |
| 4670 | if (bb0->jumpkind != Ijk_Boring) |
| 4671 | return NULL; |
| 4672 | |
| 4673 | xxx_known = False; |
| 4674 | xxx_value = 0; |
| 4675 | |
| 4676 | /* Extract the next-guest address. If it isn't a literal, we |
| 4677 | have to give up. */ |
| 4678 | |
| 4679 | udst = bb0->next; |
| 4680 | if (udst->tag == Iex_Const |
| 4681 | && (udst->Iex.Const.con->tag == Ico_U32 |
| 4682 | || udst->Iex.Const.con->tag == Ico_U64)) { |
| 4683 | /* The BB ends in a jump to a literal location. */ |
| 4684 | xxx_known = True; |
| 4685 | xxx_value = udst->Iex.Const.con->tag == Ico_U64 |
| 4686 | ? udst->Iex.Const.con->Ico.U64 |
| 4687 | : (Addr64)(udst->Iex.Const.con->Ico.U32); |
| 4688 | } |
| 4689 | |
| 4690 | if (!xxx_known) |
| 4691 | return NULL; |
| 4692 | |
| 4693 | /* Now we know the BB ends to a jump to a literal location. If |
| 4694 | it's a jump to itself (viz, idiom #1), move directly to the |
| 4695 | unrolling stage, first cloning the bb so the original isn't |
| 4696 | modified. */ |
| 4697 | if (xxx_value == my_addr) { |
| 4698 | unroll_factor = calc_unroll_factor( bb0 ); |
| 4699 | if (unroll_factor < 2) |
| 4700 | return NULL; |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4701 | bb1 = deepCopyIRSB( bb0 ); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4702 | bb0 = NULL; |
| 4703 | udst = NULL; /* is now invalid */ |
| 4704 | goto do_unroll; |
| 4705 | } |
| 4706 | |
| 4707 | /* Search for the second idiomatic form: |
| 4708 | X: BODY; if (c) goto X; goto Y |
| 4709 | We know Y, but need to establish that the last stmt |
| 4710 | is 'if (c) goto X'. |
| 4711 | */ |
| 4712 | yyy_value = xxx_value; |
| 4713 | for (i = bb0->stmts_used-1; i >= 0; i--) |
| 4714 | if (bb0->stmts[i]) |
| 4715 | break; |
| 4716 | |
| 4717 | if (i < 0) |
| 4718 | return NULL; /* block with no stmts. Strange. */ |
| 4719 | |
| 4720 | st = bb0->stmts[i]; |
| 4721 | if (st->tag != Ist_Exit) |
| 4722 | return NULL; |
| 4723 | if (st->Ist.Exit.jk != Ijk_Boring) |
| 4724 | return NULL; |
| 4725 | |
| 4726 | con = st->Ist.Exit.dst; |
| 4727 | vassert(con->tag == Ico_U32 || con->tag == Ico_U64); |
| 4728 | |
| 4729 | xxx_value = con->tag == Ico_U64 |
| 4730 | ? st->Ist.Exit.dst->Ico.U64 |
| 4731 | : (Addr64)(st->Ist.Exit.dst->Ico.U32); |
| 4732 | |
| 4733 | /* If this assertion fails, we have some kind of type error. */ |
| 4734 | vassert(con->tag == udst->Iex.Const.con->tag); |
| 4735 | |
| 4736 | if (xxx_value != my_addr) |
| 4737 | /* We didn't find either idiom. Give up. */ |
| 4738 | return NULL; |
| 4739 | |
| 4740 | /* Ok, we found idiom #2. Copy the BB, switch around the xxx and |
| 4741 | yyy values (which makes it look like idiom #1), and go into |
| 4742 | unrolling proper. This means finding (again) the last stmt, in |
| 4743 | the copied BB. */ |
| 4744 | |
| 4745 | unroll_factor = calc_unroll_factor( bb0 ); |
| 4746 | if (unroll_factor < 2) |
| 4747 | return NULL; |
| 4748 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4749 | bb1 = deepCopyIRSB( bb0 ); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4750 | bb0 = NULL; |
| 4751 | udst = NULL; /* is now invalid */ |
| 4752 | for (i = bb1->stmts_used-1; i >= 0; i--) |
| 4753 | if (bb1->stmts[i]) |
| 4754 | break; |
| 4755 | |
| 4756 | /* The next bunch of assertions should be true since we already |
| 4757 | found and checked the last stmt in the original bb. */ |
| 4758 | |
| 4759 | vassert(i >= 0); |
| 4760 | |
| 4761 | st = bb1->stmts[i]; |
| 4762 | vassert(st->tag == Ist_Exit); |
| 4763 | |
| 4764 | con = st->Ist.Exit.dst; |
| 4765 | vassert(con->tag == Ico_U32 || con->tag == Ico_U64); |
| 4766 | |
| 4767 | udst = bb1->next; |
| 4768 | vassert(udst->tag == Iex_Const); |
| 4769 | vassert(udst->Iex.Const.con->tag == Ico_U32 |
| 4770 | || udst->Iex.Const.con->tag == Ico_U64); |
| 4771 | vassert(con->tag == udst->Iex.Const.con->tag); |
| 4772 | |
| 4773 | /* switch the xxx and yyy fields around */ |
| 4774 | if (con->tag == Ico_U64) { |
| 4775 | udst->Iex.Const.con->Ico.U64 = xxx_value; |
| 4776 | con->Ico.U64 = yyy_value; |
| 4777 | } else { |
| 4778 | udst->Iex.Const.con->Ico.U32 = (UInt)xxx_value; |
cerion | 57b4c6d | 2005-02-22 11:07:35 +0000 | [diff] [blame] | 4779 | con->Ico.U32 = (UInt)yyy_value; |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4780 | } |
| 4781 | |
| 4782 | /* negate the test condition */ |
| 4783 | st->Ist.Exit.guard |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4784 | = IRExpr_Unop(Iop_Not1,deepCopyIRExpr(st->Ist.Exit.guard)); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4785 | |
| 4786 | /* --- The unroller proper. Both idioms are by now --- */ |
| 4787 | /* --- now converted to idiom 1. --- */ |
| 4788 | |
| 4789 | do_unroll: |
| 4790 | |
| 4791 | vassert(unroll_factor == 2 |
| 4792 | || unroll_factor == 4 |
| 4793 | || unroll_factor == 8); |
| 4794 | |
| 4795 | jmax = unroll_factor==8 ? 3 : (unroll_factor==4 ? 2 : 1); |
| 4796 | for (j = 1; j <= jmax; j++) { |
| 4797 | |
| 4798 | n_vars = bb1->tyenv->types_used; |
| 4799 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4800 | bb2 = deepCopyIRSB(bb1); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4801 | for (i = 0; i < n_vars; i++) |
| 4802 | (void)newIRTemp(bb1->tyenv, bb2->tyenv->types[i]); |
| 4803 | |
| 4804 | for (i = 0; i < bb2->stmts_used; i++) { |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4805 | /* deltaIRStmt destructively modifies the stmt, but |
| 4806 | that's OK since bb2 is a complete fresh copy of bb1. */ |
| 4807 | deltaIRStmt(bb2->stmts[i], n_vars); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4808 | addStmtToIRSB(bb1, bb2->stmts[i]); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4809 | } |
| 4810 | } |
| 4811 | |
| 4812 | if (DEBUG_IROPT) { |
florian | d4cc0de | 2015-01-02 11:44:12 +0000 | [diff] [blame] | 4813 | vex_printf("\nUNROLLED (%lx)\n", my_addr); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4814 | ppIRSB(bb1); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4815 | vex_printf("\n"); |
| 4816 | } |
| 4817 | |
| 4818 | /* Flattening; sigh. The unroller succeeds in breaking flatness |
| 4819 | by negating the test condition. This should be fixed properly. |
| 4820 | For the moment use this shotgun approach. */ |
| 4821 | return flatten_BB(bb1); |
| 4822 | } |
| 4823 | |
| 4824 | |
| 4825 | /*---------------------------------------------------------------*/ |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 4826 | /*--- The tree builder ---*/ |
| 4827 | /*---------------------------------------------------------------*/ |
| 4828 | |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 4829 | /* This isn't part of IR optimisation. Really it's a pass done prior |
| 4830 | to instruction selection, which improves the code that the |
| 4831 | instruction selector can produce. */ |
| 4832 | |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 4833 | /* --- The 'tmp' environment is the central data structure here --- */ |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 4834 | |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 4835 | /* The number of outstanding bindings we're prepared to track. |
| 4836 | The number of times the env becomes full and we have to dump |
| 4837 | the oldest binding (hence reducing code quality) falls very |
| 4838 | rapidly as the env size increases. 8 gives reasonable performance |
| 4839 | under most circumstances. */ |
| 4840 | #define A_NENV 10 |
| 4841 | |
florian | 740da72 | 2013-09-11 17:58:32 +0000 | [diff] [blame] | 4842 | /* An interval. Used to record the bytes in the guest state accessed |
| 4843 | by a Put[I] statement or by (one or more) Get[I] expression(s). In |
| 4844 | case of several Get[I] expressions, the lower/upper bounds are recorded. |
| 4845 | This is conservative but cheap. |
| 4846 | E.g. a Put of 8 bytes at address 100 would be recorded as [100,107]. |
| 4847 | E.g. an expression that reads 8 bytes at offset 100 and 4 bytes at |
| 4848 | offset 200 would be recorded as [100,203] */ |
| 4849 | typedef |
| 4850 | struct { |
| 4851 | Bool present; |
| 4852 | Int low; |
| 4853 | Int high; |
| 4854 | } |
| 4855 | Interval; |
| 4856 | |
| 4857 | static inline Bool |
| 4858 | intervals_overlap(Interval i1, Interval i2) |
| 4859 | { |
| 4860 | return (i1.low >= i2.low && i1.low <= i2.high) || |
| 4861 | (i2.low >= i1.low && i2.low <= i1.high); |
| 4862 | } |
| 4863 | |
| 4864 | static inline void |
| 4865 | update_interval(Interval *i, Int low, Int high) |
| 4866 | { |
| 4867 | vassert(low <= high); |
| 4868 | |
| 4869 | if (i->present) { |
| 4870 | if (low < i->low) i->low = low; |
| 4871 | if (high > i->high) i->high = high; |
| 4872 | } else { |
| 4873 | i->present = True; |
| 4874 | i->low = low; |
| 4875 | i->high = high; |
| 4876 | } |
| 4877 | } |
| 4878 | |
| 4879 | |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 4880 | /* bindee == NULL === slot is not in use |
| 4881 | bindee != NULL === slot is in use |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 4882 | */ |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 4883 | typedef |
| 4884 | struct { |
| 4885 | IRTemp binder; |
| 4886 | IRExpr* bindee; |
| 4887 | Bool doesLoad; |
florian | 740da72 | 2013-09-11 17:58:32 +0000 | [diff] [blame] | 4888 | /* Record the bytes of the guest state BINDEE reads from. */ |
| 4889 | Interval getInterval; |
sewardj | 17442fe | 2004-09-20 14:54:28 +0000 | [diff] [blame] | 4890 | } |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 4891 | ATmpInfo; |
sewardj | 17442fe | 2004-09-20 14:54:28 +0000 | [diff] [blame] | 4892 | |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 4893 | __attribute__((unused)) |
| 4894 | static void ppAEnv ( ATmpInfo* env ) |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 4895 | { |
sewardj | 17442fe | 2004-09-20 14:54:28 +0000 | [diff] [blame] | 4896 | Int i; |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 4897 | for (i = 0; i < A_NENV; i++) { |
| 4898 | vex_printf("%d tmp %d val ", i, (Int)env[i].binder); |
| 4899 | if (env[i].bindee) |
| 4900 | ppIRExpr(env[i].bindee); |
| 4901 | else |
| 4902 | vex_printf("(null)"); |
| 4903 | vex_printf("\n"); |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 4904 | } |
| 4905 | } |
| 4906 | |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 4907 | /* --- Tree-traversal fns --- */ |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 4908 | |
sewardj | 37d3801 | 2004-08-24 00:37:04 +0000 | [diff] [blame] | 4909 | /* Traverse an expr, and detect if any part of it reads memory or does |
| 4910 | a Get. Be careful ... this really controls how much the |
| 4911 | tree-builder can reorder the code, so getting it right is critical. |
| 4912 | */ |
florian | 740da72 | 2013-09-11 17:58:32 +0000 | [diff] [blame] | 4913 | static void setHints_Expr (Bool* doesLoad, Interval* getInterval, IRExpr* e ) |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 4914 | { |
sewardj | c41f1fb | 2004-08-22 09:48:08 +0000 | [diff] [blame] | 4915 | Int i; |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 4916 | switch (e->tag) { |
sewardj | c41f1fb | 2004-08-22 09:48:08 +0000 | [diff] [blame] | 4917 | case Iex_CCall: |
| 4918 | for (i = 0; e->Iex.CCall.args[i]; i++) |
florian | 740da72 | 2013-09-11 17:58:32 +0000 | [diff] [blame] | 4919 | setHints_Expr(doesLoad, getInterval, e->Iex.CCall.args[i]); |
sewardj | c41f1fb | 2004-08-22 09:48:08 +0000 | [diff] [blame] | 4920 | return; |
florian | 99dd03e | 2013-01-29 03:56:06 +0000 | [diff] [blame] | 4921 | case Iex_ITE: |
florian | 740da72 | 2013-09-11 17:58:32 +0000 | [diff] [blame] | 4922 | setHints_Expr(doesLoad, getInterval, e->Iex.ITE.cond); |
| 4923 | setHints_Expr(doesLoad, getInterval, e->Iex.ITE.iftrue); |
| 4924 | setHints_Expr(doesLoad, getInterval, e->Iex.ITE.iffalse); |
sewardj | c41f1fb | 2004-08-22 09:48:08 +0000 | [diff] [blame] | 4925 | return; |
sewardj | 40c8026 | 2006-02-08 19:30:46 +0000 | [diff] [blame] | 4926 | case Iex_Qop: |
florian | 740da72 | 2013-09-11 17:58:32 +0000 | [diff] [blame] | 4927 | setHints_Expr(doesLoad, getInterval, e->Iex.Qop.details->arg1); |
| 4928 | setHints_Expr(doesLoad, getInterval, e->Iex.Qop.details->arg2); |
| 4929 | setHints_Expr(doesLoad, getInterval, e->Iex.Qop.details->arg3); |
| 4930 | setHints_Expr(doesLoad, getInterval, e->Iex.Qop.details->arg4); |
sewardj | 40c8026 | 2006-02-08 19:30:46 +0000 | [diff] [blame] | 4931 | return; |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 4932 | case Iex_Triop: |
florian | 740da72 | 2013-09-11 17:58:32 +0000 | [diff] [blame] | 4933 | setHints_Expr(doesLoad, getInterval, e->Iex.Triop.details->arg1); |
| 4934 | setHints_Expr(doesLoad, getInterval, e->Iex.Triop.details->arg2); |
| 4935 | setHints_Expr(doesLoad, getInterval, e->Iex.Triop.details->arg3); |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 4936 | return; |
sewardj | c41f1fb | 2004-08-22 09:48:08 +0000 | [diff] [blame] | 4937 | case Iex_Binop: |
florian | 740da72 | 2013-09-11 17:58:32 +0000 | [diff] [blame] | 4938 | setHints_Expr(doesLoad, getInterval, e->Iex.Binop.arg1); |
| 4939 | setHints_Expr(doesLoad, getInterval, e->Iex.Binop.arg2); |
sewardj | c41f1fb | 2004-08-22 09:48:08 +0000 | [diff] [blame] | 4940 | return; |
| 4941 | case Iex_Unop: |
florian | 740da72 | 2013-09-11 17:58:32 +0000 | [diff] [blame] | 4942 | setHints_Expr(doesLoad, getInterval, e->Iex.Unop.arg); |
sewardj | c41f1fb | 2004-08-22 09:48:08 +0000 | [diff] [blame] | 4943 | return; |
sewardj | af1ceca | 2005-06-30 23:31:27 +0000 | [diff] [blame] | 4944 | case Iex_Load: |
sewardj | c9ad115 | 2004-10-14 00:08:21 +0000 | [diff] [blame] | 4945 | *doesLoad = True; |
florian | 740da72 | 2013-09-11 17:58:32 +0000 | [diff] [blame] | 4946 | setHints_Expr(doesLoad, getInterval, e->Iex.Load.addr); |
sewardj | c41f1fb | 2004-08-22 09:48:08 +0000 | [diff] [blame] | 4947 | return; |
florian | 740da72 | 2013-09-11 17:58:32 +0000 | [diff] [blame] | 4948 | case Iex_Get: { |
| 4949 | Int low = e->Iex.Get.offset; |
| 4950 | Int high = low + sizeofIRType(e->Iex.Get.ty) - 1; |
| 4951 | update_interval(getInterval, low, high); |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 4952 | return; |
florian | 740da72 | 2013-09-11 17:58:32 +0000 | [diff] [blame] | 4953 | } |
| 4954 | case Iex_GetI: { |
| 4955 | IRRegArray *descr = e->Iex.GetI.descr; |
| 4956 | Int size = sizeofIRType(descr->elemTy); |
| 4957 | Int low = descr->base; |
| 4958 | Int high = low + descr->nElems * size - 1; |
| 4959 | update_interval(getInterval, low, high); |
| 4960 | setHints_Expr(doesLoad, getInterval, e->Iex.GetI.ix); |
sewardj | f650199 | 2004-08-27 11:58:24 +0000 | [diff] [blame] | 4961 | return; |
florian | 740da72 | 2013-09-11 17:58:32 +0000 | [diff] [blame] | 4962 | } |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4963 | case Iex_RdTmp: |
sewardj | c41f1fb | 2004-08-22 09:48:08 +0000 | [diff] [blame] | 4964 | case Iex_Const: |
| 4965 | return; |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 4966 | default: |
| 4967 | vex_printf("\n"); ppIRExpr(e); vex_printf("\n"); |
| 4968 | vpanic("setHints_Expr"); |
| 4969 | } |
| 4970 | } |
| 4971 | |
| 4972 | |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 4973 | /* Add a binding to the front of the env and slide all the rest |
| 4974 | backwards. It should be the case that the last slot is free. */ |
| 4975 | static void addToEnvFront ( ATmpInfo* env, IRTemp binder, IRExpr* bindee ) |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 4976 | { |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 4977 | Int i; |
| 4978 | vassert(env[A_NENV-1].bindee == NULL); |
| 4979 | for (i = A_NENV-1; i >= 1; i--) |
| 4980 | env[i] = env[i-1]; |
| 4981 | env[0].binder = binder; |
| 4982 | env[0].bindee = bindee; |
| 4983 | env[0].doesLoad = False; /* filled in later */ |
florian | 740da72 | 2013-09-11 17:58:32 +0000 | [diff] [blame] | 4984 | env[0].getInterval.present = False; /* filled in later */ |
| 4985 | env[0].getInterval.low = -1; /* filled in later */ |
| 4986 | env[0].getInterval.high = -1; /* filled in later */ |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 4987 | } |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 4988 | |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 4989 | /* Given uses :: array of UShort, indexed by IRTemp |
| 4990 | Add the use-occurrences of temps in this expression |
| 4991 | to the env. |
| 4992 | */ |
| 4993 | static void aoccCount_Expr ( UShort* uses, IRExpr* e ) |
| 4994 | { |
| 4995 | Int i; |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 4996 | |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 4997 | switch (e->tag) { |
| 4998 | |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 4999 | case Iex_RdTmp: /* the only interesting case */ |
| 5000 | uses[e->Iex.RdTmp.tmp]++; |
sewardj | 8bee6d1 | 2005-03-22 02:24:05 +0000 | [diff] [blame] | 5001 | return; |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 5002 | |
florian | 99dd03e | 2013-01-29 03:56:06 +0000 | [diff] [blame] | 5003 | case Iex_ITE: |
| 5004 | aoccCount_Expr(uses, e->Iex.ITE.cond); |
| 5005 | aoccCount_Expr(uses, e->Iex.ITE.iftrue); |
| 5006 | aoccCount_Expr(uses, e->Iex.ITE.iffalse); |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5007 | return; |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 5008 | |
sewardj | 40c8026 | 2006-02-08 19:30:46 +0000 | [diff] [blame] | 5009 | case Iex_Qop: |
florian | 96d7cc3 | 2012-06-01 20:41:24 +0000 | [diff] [blame] | 5010 | aoccCount_Expr(uses, e->Iex.Qop.details->arg1); |
| 5011 | aoccCount_Expr(uses, e->Iex.Qop.details->arg2); |
| 5012 | aoccCount_Expr(uses, e->Iex.Qop.details->arg3); |
| 5013 | aoccCount_Expr(uses, e->Iex.Qop.details->arg4); |
sewardj | 40c8026 | 2006-02-08 19:30:46 +0000 | [diff] [blame] | 5014 | return; |
| 5015 | |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 5016 | case Iex_Triop: |
florian | 420bfa9 | 2012-06-02 20:29:22 +0000 | [diff] [blame] | 5017 | aoccCount_Expr(uses, e->Iex.Triop.details->arg1); |
| 5018 | aoccCount_Expr(uses, e->Iex.Triop.details->arg2); |
| 5019 | aoccCount_Expr(uses, e->Iex.Triop.details->arg3); |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 5020 | return; |
| 5021 | |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5022 | case Iex_Binop: |
| 5023 | aoccCount_Expr(uses, e->Iex.Binop.arg1); |
| 5024 | aoccCount_Expr(uses, e->Iex.Binop.arg2); |
| 5025 | return; |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 5026 | |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5027 | case Iex_Unop: |
| 5028 | aoccCount_Expr(uses, e->Iex.Unop.arg); |
| 5029 | return; |
| 5030 | |
| 5031 | case Iex_Load: |
| 5032 | aoccCount_Expr(uses, e->Iex.Load.addr); |
| 5033 | return; |
| 5034 | |
| 5035 | case Iex_CCall: |
| 5036 | for (i = 0; e->Iex.CCall.args[i]; i++) |
| 5037 | aoccCount_Expr(uses, e->Iex.CCall.args[i]); |
| 5038 | return; |
| 5039 | |
| 5040 | case Iex_GetI: |
| 5041 | aoccCount_Expr(uses, e->Iex.GetI.ix); |
| 5042 | return; |
| 5043 | |
| 5044 | case Iex_Const: |
| 5045 | case Iex_Get: |
| 5046 | return; |
| 5047 | |
| 5048 | default: |
| 5049 | vex_printf("\n"); ppIRExpr(e); vex_printf("\n"); |
| 5050 | vpanic("aoccCount_Expr"); |
| 5051 | } |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 5052 | } |
| 5053 | |
| 5054 | |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5055 | /* Given uses :: array of UShort, indexed by IRTemp |
| 5056 | Add the use-occurrences of temps in this statement |
| 5057 | to the env. |
| 5058 | */ |
| 5059 | static void aoccCount_Stmt ( UShort* uses, IRStmt* st ) |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 5060 | { |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5061 | Int i; |
| 5062 | IRDirty* d; |
sewardj | e9d8a26 | 2009-07-01 08:06:34 +0000 | [diff] [blame] | 5063 | IRCAS* cas; |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5064 | switch (st->tag) { |
| 5065 | case Ist_AbiHint: |
| 5066 | aoccCount_Expr(uses, st->Ist.AbiHint.base); |
sewardj | 478646f | 2008-05-01 20:13:04 +0000 | [diff] [blame] | 5067 | aoccCount_Expr(uses, st->Ist.AbiHint.nia); |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5068 | return; |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 5069 | case Ist_WrTmp: |
| 5070 | aoccCount_Expr(uses, st->Ist.WrTmp.data); |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5071 | return; |
| 5072 | case Ist_Put: |
| 5073 | aoccCount_Expr(uses, st->Ist.Put.data); |
| 5074 | return; |
| 5075 | case Ist_PutI: |
florian | d6f38b3 | 2012-05-31 15:46:18 +0000 | [diff] [blame] | 5076 | aoccCount_Expr(uses, st->Ist.PutI.details->ix); |
| 5077 | aoccCount_Expr(uses, st->Ist.PutI.details->data); |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5078 | return; |
| 5079 | case Ist_Store: |
| 5080 | aoccCount_Expr(uses, st->Ist.Store.addr); |
| 5081 | aoccCount_Expr(uses, st->Ist.Store.data); |
| 5082 | return; |
sewardj | cfe046e | 2013-01-17 14:23:53 +0000 | [diff] [blame] | 5083 | case Ist_StoreG: { |
| 5084 | IRStoreG* sg = st->Ist.StoreG.details; |
| 5085 | aoccCount_Expr(uses, sg->addr); |
| 5086 | aoccCount_Expr(uses, sg->data); |
| 5087 | aoccCount_Expr(uses, sg->guard); |
| 5088 | return; |
| 5089 | } |
| 5090 | case Ist_LoadG: { |
| 5091 | IRLoadG* lg = st->Ist.LoadG.details; |
| 5092 | aoccCount_Expr(uses, lg->addr); |
| 5093 | aoccCount_Expr(uses, lg->alt); |
| 5094 | aoccCount_Expr(uses, lg->guard); |
| 5095 | return; |
| 5096 | } |
sewardj | e9d8a26 | 2009-07-01 08:06:34 +0000 | [diff] [blame] | 5097 | case Ist_CAS: |
| 5098 | cas = st->Ist.CAS.details; |
| 5099 | aoccCount_Expr(uses, cas->addr); |
| 5100 | if (cas->expdHi) |
| 5101 | aoccCount_Expr(uses, cas->expdHi); |
| 5102 | aoccCount_Expr(uses, cas->expdLo); |
| 5103 | if (cas->dataHi) |
| 5104 | aoccCount_Expr(uses, cas->dataHi); |
| 5105 | aoccCount_Expr(uses, cas->dataLo); |
| 5106 | return; |
sewardj | e768e92 | 2009-11-26 17:17:37 +0000 | [diff] [blame] | 5107 | case Ist_LLSC: |
| 5108 | aoccCount_Expr(uses, st->Ist.LLSC.addr); |
| 5109 | if (st->Ist.LLSC.storedata) |
| 5110 | aoccCount_Expr(uses, st->Ist.LLSC.storedata); |
| 5111 | return; |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5112 | case Ist_Dirty: |
| 5113 | d = st->Ist.Dirty.details; |
| 5114 | if (d->mFx != Ifx_None) |
| 5115 | aoccCount_Expr(uses, d->mAddr); |
| 5116 | aoccCount_Expr(uses, d->guard); |
sewardj | 74142b8 | 2013-08-08 10:28:59 +0000 | [diff] [blame] | 5117 | for (i = 0; d->args[i]; i++) { |
| 5118 | IRExpr* arg = d->args[i]; |
florian | 9041956 | 2013-08-15 20:54:52 +0000 | [diff] [blame] | 5119 | if (LIKELY(!is_IRExpr_VECRET_or_BBPTR(arg))) |
sewardj | 74142b8 | 2013-08-08 10:28:59 +0000 | [diff] [blame] | 5120 | aoccCount_Expr(uses, arg); |
| 5121 | } |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5122 | return; |
| 5123 | case Ist_NoOp: |
| 5124 | case Ist_IMark: |
sewardj | c4356f0 | 2007-11-09 21:15:04 +0000 | [diff] [blame] | 5125 | case Ist_MBE: |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5126 | return; |
| 5127 | case Ist_Exit: |
| 5128 | aoccCount_Expr(uses, st->Ist.Exit.guard); |
| 5129 | return; |
| 5130 | default: |
| 5131 | vex_printf("\n"); ppIRStmt(st); vex_printf("\n"); |
| 5132 | vpanic("aoccCount_Stmt"); |
| 5133 | } |
| 5134 | } |
| 5135 | |
| 5136 | /* Look up a binding for tmp in the env. If found, return the bound |
| 5137 | expression, and set the env's binding to NULL so it is marked as |
| 5138 | used. If not found, return NULL. */ |
| 5139 | |
| 5140 | static IRExpr* atbSubst_Temp ( ATmpInfo* env, IRTemp tmp ) |
| 5141 | { |
| 5142 | Int i; |
| 5143 | for (i = 0; i < A_NENV; i++) { |
| 5144 | if (env[i].binder == tmp && env[i].bindee != NULL) { |
| 5145 | IRExpr* bindee = env[i].bindee; |
| 5146 | env[i].bindee = NULL; |
| 5147 | return bindee; |
| 5148 | } |
| 5149 | } |
| 5150 | return NULL; |
| 5151 | } |
| 5152 | |
| 5153 | /* Traverse e, looking for temps. For each observed temp, see if env |
| 5154 | contains a binding for the temp, and if so return the bound value. |
| 5155 | The env has the property that any binding it holds is |
| 5156 | 'single-shot', so once a binding is used, it is marked as no longer |
| 5157 | available, by setting its .bindee field to NULL. */ |
| 5158 | |
sewardj | eb17e49 | 2007-08-25 23:07:44 +0000 | [diff] [blame] | 5159 | static inline Bool is_Unop ( IRExpr* e, IROp op ) { |
| 5160 | return e->tag == Iex_Unop && e->Iex.Unop.op == op; |
| 5161 | } |
| 5162 | static inline Bool is_Binop ( IRExpr* e, IROp op ) { |
| 5163 | return e->tag == Iex_Binop && e->Iex.Binop.op == op; |
| 5164 | } |
| 5165 | |
| 5166 | static IRExpr* fold_IRExpr_Binop ( IROp op, IRExpr* a1, IRExpr* a2 ) |
| 5167 | { |
| 5168 | switch (op) { |
| 5169 | case Iop_Or32: |
| 5170 | /* Or32( CmpwNEZ32(x), CmpwNEZ32(y) ) --> CmpwNEZ32( Or32( x, y ) ) */ |
| 5171 | if (is_Unop(a1, Iop_CmpwNEZ32) && is_Unop(a2, Iop_CmpwNEZ32)) |
| 5172 | return IRExpr_Unop( Iop_CmpwNEZ32, |
| 5173 | IRExpr_Binop( Iop_Or32, a1->Iex.Unop.arg, |
| 5174 | a2->Iex.Unop.arg ) ); |
| 5175 | break; |
florian | 51d26fd | 2011-07-23 00:23:02 +0000 | [diff] [blame] | 5176 | |
| 5177 | case Iop_CmpNE32: |
| 5178 | /* Since X has type Ity_I1 we can simplify: |
| 5179 | CmpNE32(1Uto32(X),0)) ==> X */ |
| 5180 | if (is_Unop(a1, Iop_1Uto32) && isZeroU32(a2)) |
| 5181 | return a1->Iex.Unop.arg; |
| 5182 | break; |
| 5183 | |
sewardj | eb17e49 | 2007-08-25 23:07:44 +0000 | [diff] [blame] | 5184 | default: |
| 5185 | break; |
| 5186 | } |
| 5187 | /* no reduction rule applies */ |
| 5188 | return IRExpr_Binop( op, a1, a2 ); |
| 5189 | } |
| 5190 | |
| 5191 | static IRExpr* fold_IRExpr_Unop ( IROp op, IRExpr* aa ) |
| 5192 | { |
| 5193 | switch (op) { |
| 5194 | case Iop_CmpwNEZ64: |
florian | de6f710 | 2013-09-06 23:13:39 +0000 | [diff] [blame] | 5195 | /* CmpwNEZ64( CmpwNEZ64 ( x ) ) --> CmpwNEZ64 ( x ) */ |
| 5196 | if (is_Unop(aa, Iop_CmpwNEZ64)) |
| 5197 | return IRExpr_Unop( Iop_CmpwNEZ64, aa->Iex.Unop.arg ); |
sewardj | eb17e49 | 2007-08-25 23:07:44 +0000 | [diff] [blame] | 5198 | /* CmpwNEZ64( Or64 ( CmpwNEZ64(x), y ) ) --> CmpwNEZ64( Or64( x, y ) ) */ |
| 5199 | if (is_Binop(aa, Iop_Or64) |
| 5200 | && is_Unop(aa->Iex.Binop.arg1, Iop_CmpwNEZ64)) |
| 5201 | return fold_IRExpr_Unop( |
| 5202 | Iop_CmpwNEZ64, |
| 5203 | IRExpr_Binop(Iop_Or64, |
| 5204 | aa->Iex.Binop.arg1->Iex.Unop.arg, |
| 5205 | aa->Iex.Binop.arg2)); |
| 5206 | /* CmpwNEZ64( Or64 ( x, CmpwNEZ64(y) ) ) --> CmpwNEZ64( Or64( x, y ) ) */ |
| 5207 | if (is_Binop(aa, Iop_Or64) |
| 5208 | && is_Unop(aa->Iex.Binop.arg2, Iop_CmpwNEZ64)) |
| 5209 | return fold_IRExpr_Unop( |
| 5210 | Iop_CmpwNEZ64, |
| 5211 | IRExpr_Binop(Iop_Or64, |
| 5212 | aa->Iex.Binop.arg1, |
| 5213 | aa->Iex.Binop.arg2->Iex.Unop.arg)); |
| 5214 | break; |
| 5215 | case Iop_CmpNEZ64: |
| 5216 | /* CmpNEZ64( Left64(x) ) --> CmpNEZ64(x) */ |
| 5217 | if (is_Unop(aa, Iop_Left64)) |
| 5218 | return IRExpr_Unop(Iop_CmpNEZ64, aa->Iex.Unop.arg); |
florian | de6f710 | 2013-09-06 23:13:39 +0000 | [diff] [blame] | 5219 | /* CmpNEZ64( 1Uto64(X) ) --> X */ |
| 5220 | if (is_Unop(aa, Iop_1Uto64)) |
| 5221 | return aa->Iex.Unop.arg; |
sewardj | eb17e49 | 2007-08-25 23:07:44 +0000 | [diff] [blame] | 5222 | break; |
| 5223 | case Iop_CmpwNEZ32: |
| 5224 | /* CmpwNEZ32( CmpwNEZ32 ( x ) ) --> CmpwNEZ32 ( x ) */ |
| 5225 | if (is_Unop(aa, Iop_CmpwNEZ32)) |
| 5226 | return IRExpr_Unop( Iop_CmpwNEZ32, aa->Iex.Unop.arg ); |
| 5227 | break; |
| 5228 | case Iop_CmpNEZ32: |
| 5229 | /* CmpNEZ32( Left32(x) ) --> CmpNEZ32(x) */ |
| 5230 | if (is_Unop(aa, Iop_Left32)) |
| 5231 | return IRExpr_Unop(Iop_CmpNEZ32, aa->Iex.Unop.arg); |
florian | 51d26fd | 2011-07-23 00:23:02 +0000 | [diff] [blame] | 5232 | /* CmpNEZ32( 1Uto32(X) ) --> X */ |
| 5233 | if (is_Unop(aa, Iop_1Uto32)) |
| 5234 | return aa->Iex.Unop.arg; |
florian | de6f710 | 2013-09-06 23:13:39 +0000 | [diff] [blame] | 5235 | /* CmpNEZ32( 64to32( CmpwNEZ64(X) ) ) --> CmpNEZ64(X) */ |
| 5236 | if (is_Unop(aa, Iop_64to32) && is_Unop(aa->Iex.Unop.arg, Iop_CmpwNEZ64)) |
| 5237 | return IRExpr_Unop(Iop_CmpNEZ64, aa->Iex.Unop.arg->Iex.Unop.arg); |
florian | 51d26fd | 2011-07-23 00:23:02 +0000 | [diff] [blame] | 5238 | break; |
| 5239 | case Iop_CmpNEZ8: |
| 5240 | /* CmpNEZ8( 1Uto8(X) ) --> X */ |
| 5241 | if (is_Unop(aa, Iop_1Uto8)) |
| 5242 | return aa->Iex.Unop.arg; |
sewardj | eb17e49 | 2007-08-25 23:07:44 +0000 | [diff] [blame] | 5243 | break; |
| 5244 | case Iop_Left32: |
| 5245 | /* Left32( Left32(x) ) --> Left32(x) */ |
| 5246 | if (is_Unop(aa, Iop_Left32)) |
| 5247 | return IRExpr_Unop( Iop_Left32, aa->Iex.Unop.arg ); |
| 5248 | break; |
florian | 854912a | 2013-09-01 20:17:23 +0000 | [diff] [blame] | 5249 | case Iop_Left64: |
| 5250 | /* Left64( Left64(x) ) --> Left64(x) */ |
| 5251 | if (is_Unop(aa, Iop_Left64)) |
| 5252 | return IRExpr_Unop( Iop_Left64, aa->Iex.Unop.arg ); |
| 5253 | break; |
sewardj | 0caa0c0 | 2014-08-11 14:01:00 +0000 | [diff] [blame] | 5254 | case Iop_ZeroHI64ofV128: |
| 5255 | /* ZeroHI64ofV128( ZeroHI64ofV128(x) ) --> ZeroHI64ofV128(x) */ |
| 5256 | if (is_Unop(aa, Iop_ZeroHI64ofV128)) |
| 5257 | return IRExpr_Unop( Iop_ZeroHI64ofV128, aa->Iex.Unop.arg ); |
| 5258 | break; |
sewardj | eb17e49 | 2007-08-25 23:07:44 +0000 | [diff] [blame] | 5259 | case Iop_32to1: |
| 5260 | /* 32to1( 1Uto32 ( x ) ) --> x */ |
| 5261 | if (is_Unop(aa, Iop_1Uto32)) |
| 5262 | return aa->Iex.Unop.arg; |
| 5263 | /* 32to1( CmpwNEZ32 ( x )) --> CmpNEZ32(x) */ |
| 5264 | if (is_Unop(aa, Iop_CmpwNEZ32)) |
| 5265 | return IRExpr_Unop( Iop_CmpNEZ32, aa->Iex.Unop.arg ); |
| 5266 | break; |
| 5267 | case Iop_64to1: |
| 5268 | /* 64to1( 1Uto64 ( x ) ) --> x */ |
| 5269 | if (is_Unop(aa, Iop_1Uto64)) |
| 5270 | return aa->Iex.Unop.arg; |
| 5271 | /* 64to1( CmpwNEZ64 ( x )) --> CmpNEZ64(x) */ |
| 5272 | if (is_Unop(aa, Iop_CmpwNEZ64)) |
| 5273 | return IRExpr_Unop( Iop_CmpNEZ64, aa->Iex.Unop.arg ); |
| 5274 | break; |
sewardj | ef425db | 2010-01-11 10:46:18 +0000 | [diff] [blame] | 5275 | case Iop_64to32: |
| 5276 | /* 64to32( 32Uto64 ( x )) --> x */ |
| 5277 | if (is_Unop(aa, Iop_32Uto64)) |
| 5278 | return aa->Iex.Unop.arg; |
sewardj | ca257bc | 2010-09-08 08:34:52 +0000 | [diff] [blame] | 5279 | /* 64to32( 8Uto64 ( x )) --> 8Uto32(x) */ |
| 5280 | if (is_Unop(aa, Iop_8Uto64)) |
| 5281 | return IRExpr_Unop(Iop_8Uto32, aa->Iex.Unop.arg); |
| 5282 | break; |
| 5283 | |
| 5284 | case Iop_32Uto64: |
| 5285 | /* 32Uto64( 8Uto32( x )) --> 8Uto64(x) */ |
| 5286 | if (is_Unop(aa, Iop_8Uto32)) |
| 5287 | return IRExpr_Unop(Iop_8Uto64, aa->Iex.Unop.arg); |
| 5288 | /* 32Uto64( 16Uto32( x )) --> 16Uto64(x) */ |
| 5289 | if (is_Unop(aa, Iop_16Uto32)) |
| 5290 | return IRExpr_Unop(Iop_16Uto64, aa->Iex.Unop.arg); |
sewardj | bba356d | 2012-04-25 16:47:53 +0000 | [diff] [blame] | 5291 | /* 32Uto64(64to32( Shr64( 32Uto64(64to32(x)), sh )) |
| 5292 | --> Shr64( 32Uto64(64to32(x)), sh )) */ |
| 5293 | if (is_Unop(aa, Iop_64to32) |
| 5294 | && is_Binop(aa->Iex.Unop.arg, Iop_Shr64) |
| 5295 | && is_Unop(aa->Iex.Unop.arg->Iex.Binop.arg1, Iop_32Uto64) |
| 5296 | && is_Unop(aa->Iex.Unop.arg->Iex.Binop.arg1->Iex.Unop.arg, |
| 5297 | Iop_64to32)) { |
| 5298 | return aa->Iex.Unop.arg; |
| 5299 | } |
| 5300 | /* 32Uto64(64to32( Shl64( 32Uto64(64to32(x)), sh )) |
| 5301 | --> 32Uto64(64to32( Shl64( x, sh )) */ |
| 5302 | if (is_Unop(aa, Iop_64to32) |
| 5303 | && is_Binop(aa->Iex.Unop.arg, Iop_Shl64) |
| 5304 | && is_Unop(aa->Iex.Unop.arg->Iex.Binop.arg1, Iop_32Uto64) |
| 5305 | && is_Unop(aa->Iex.Unop.arg->Iex.Binop.arg1->Iex.Unop.arg, |
| 5306 | Iop_64to32)) { |
| 5307 | return |
| 5308 | IRExpr_Unop( |
| 5309 | Iop_32Uto64, |
| 5310 | IRExpr_Unop( |
| 5311 | Iop_64to32, |
| 5312 | IRExpr_Binop( |
| 5313 | Iop_Shl64, |
| 5314 | aa->Iex.Unop.arg->Iex.Binop.arg1->Iex.Unop.arg->Iex.Unop.arg, |
| 5315 | aa->Iex.Unop.arg->Iex.Binop.arg2 |
| 5316 | ))); |
| 5317 | } |
sewardj | ef425db | 2010-01-11 10:46:18 +0000 | [diff] [blame] | 5318 | break; |
sewardj | 6c299f3 | 2009-12-31 18:00:12 +0000 | [diff] [blame] | 5319 | |
| 5320 | case Iop_1Sto32: |
| 5321 | /* 1Sto32( CmpNEZ8( 32to8( 1Uto32( CmpNEZ32( x ))))) -> CmpwNEZ32(x) */ |
| 5322 | if (is_Unop(aa, Iop_CmpNEZ8) |
| 5323 | && is_Unop(aa->Iex.Unop.arg, Iop_32to8) |
| 5324 | && is_Unop(aa->Iex.Unop.arg->Iex.Unop.arg, Iop_1Uto32) |
| 5325 | && is_Unop(aa->Iex.Unop.arg->Iex.Unop.arg->Iex.Unop.arg, |
| 5326 | Iop_CmpNEZ32)) { |
| 5327 | return IRExpr_Unop( Iop_CmpwNEZ32, |
| 5328 | aa->Iex.Unop.arg->Iex.Unop.arg |
| 5329 | ->Iex.Unop.arg->Iex.Unop.arg); |
| 5330 | } |
| 5331 | break; |
| 5332 | |
sewardj | eb17e49 | 2007-08-25 23:07:44 +0000 | [diff] [blame] | 5333 | default: |
| 5334 | break; |
| 5335 | } |
| 5336 | /* no reduction rule applies */ |
| 5337 | return IRExpr_Unop( op, aa ); |
| 5338 | } |
| 5339 | |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5340 | static IRExpr* atbSubst_Expr ( ATmpInfo* env, IRExpr* e ) |
| 5341 | { |
| 5342 | IRExpr* e2; |
| 5343 | IRExpr** args2; |
| 5344 | Int i; |
| 5345 | |
| 5346 | switch (e->tag) { |
| 5347 | |
| 5348 | case Iex_CCall: |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 5349 | args2 = shallowCopyIRExprVec(e->Iex.CCall.args); |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5350 | for (i = 0; args2[i]; i++) |
| 5351 | args2[i] = atbSubst_Expr(env,args2[i]); |
| 5352 | return IRExpr_CCall( |
| 5353 | e->Iex.CCall.cee, |
| 5354 | e->Iex.CCall.retty, |
| 5355 | args2 |
| 5356 | ); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 5357 | case Iex_RdTmp: |
| 5358 | e2 = atbSubst_Temp(env, e->Iex.RdTmp.tmp); |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5359 | return e2 ? e2 : e; |
florian | 99dd03e | 2013-01-29 03:56:06 +0000 | [diff] [blame] | 5360 | case Iex_ITE: |
| 5361 | return IRExpr_ITE( |
| 5362 | atbSubst_Expr(env, e->Iex.ITE.cond), |
| 5363 | atbSubst_Expr(env, e->Iex.ITE.iftrue), |
| 5364 | atbSubst_Expr(env, e->Iex.ITE.iffalse) |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5365 | ); |
sewardj | 40c8026 | 2006-02-08 19:30:46 +0000 | [diff] [blame] | 5366 | case Iex_Qop: |
| 5367 | return IRExpr_Qop( |
florian | 96d7cc3 | 2012-06-01 20:41:24 +0000 | [diff] [blame] | 5368 | e->Iex.Qop.details->op, |
| 5369 | atbSubst_Expr(env, e->Iex.Qop.details->arg1), |
| 5370 | atbSubst_Expr(env, e->Iex.Qop.details->arg2), |
| 5371 | atbSubst_Expr(env, e->Iex.Qop.details->arg3), |
| 5372 | atbSubst_Expr(env, e->Iex.Qop.details->arg4) |
sewardj | 40c8026 | 2006-02-08 19:30:46 +0000 | [diff] [blame] | 5373 | ); |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 5374 | case Iex_Triop: |
| 5375 | return IRExpr_Triop( |
florian | 420bfa9 | 2012-06-02 20:29:22 +0000 | [diff] [blame] | 5376 | e->Iex.Triop.details->op, |
| 5377 | atbSubst_Expr(env, e->Iex.Triop.details->arg1), |
| 5378 | atbSubst_Expr(env, e->Iex.Triop.details->arg2), |
| 5379 | atbSubst_Expr(env, e->Iex.Triop.details->arg3) |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 5380 | ); |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5381 | case Iex_Binop: |
sewardj | eb17e49 | 2007-08-25 23:07:44 +0000 | [diff] [blame] | 5382 | return fold_IRExpr_Binop( |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5383 | e->Iex.Binop.op, |
| 5384 | atbSubst_Expr(env, e->Iex.Binop.arg1), |
| 5385 | atbSubst_Expr(env, e->Iex.Binop.arg2) |
| 5386 | ); |
| 5387 | case Iex_Unop: |
sewardj | eb17e49 | 2007-08-25 23:07:44 +0000 | [diff] [blame] | 5388 | return fold_IRExpr_Unop( |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5389 | e->Iex.Unop.op, |
| 5390 | atbSubst_Expr(env, e->Iex.Unop.arg) |
| 5391 | ); |
| 5392 | case Iex_Load: |
| 5393 | return IRExpr_Load( |
| 5394 | e->Iex.Load.end, |
| 5395 | e->Iex.Load.ty, |
| 5396 | atbSubst_Expr(env, e->Iex.Load.addr) |
| 5397 | ); |
| 5398 | case Iex_GetI: |
| 5399 | return IRExpr_GetI( |
| 5400 | e->Iex.GetI.descr, |
| 5401 | atbSubst_Expr(env, e->Iex.GetI.ix), |
| 5402 | e->Iex.GetI.bias |
| 5403 | ); |
| 5404 | case Iex_Const: |
| 5405 | case Iex_Get: |
| 5406 | return e; |
| 5407 | default: |
| 5408 | vex_printf("\n"); ppIRExpr(e); vex_printf("\n"); |
| 5409 | vpanic("atbSubst_Expr"); |
| 5410 | } |
| 5411 | } |
| 5412 | |
| 5413 | /* Same deal as atbSubst_Expr, except for stmts. */ |
| 5414 | |
| 5415 | static IRStmt* atbSubst_Stmt ( ATmpInfo* env, IRStmt* st ) |
| 5416 | { |
sewardj | e9d8a26 | 2009-07-01 08:06:34 +0000 | [diff] [blame] | 5417 | Int i; |
| 5418 | IRDirty *d, *d2; |
| 5419 | IRCAS *cas, *cas2; |
florian | d6f38b3 | 2012-05-31 15:46:18 +0000 | [diff] [blame] | 5420 | IRPutI *puti, *puti2; |
| 5421 | |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5422 | switch (st->tag) { |
| 5423 | case Ist_AbiHint: |
| 5424 | return IRStmt_AbiHint( |
| 5425 | atbSubst_Expr(env, st->Ist.AbiHint.base), |
sewardj | 478646f | 2008-05-01 20:13:04 +0000 | [diff] [blame] | 5426 | st->Ist.AbiHint.len, |
| 5427 | atbSubst_Expr(env, st->Ist.AbiHint.nia) |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5428 | ); |
| 5429 | case Ist_Store: |
| 5430 | return IRStmt_Store( |
| 5431 | st->Ist.Store.end, |
| 5432 | atbSubst_Expr(env, st->Ist.Store.addr), |
| 5433 | atbSubst_Expr(env, st->Ist.Store.data) |
| 5434 | ); |
sewardj | cfe046e | 2013-01-17 14:23:53 +0000 | [diff] [blame] | 5435 | case Ist_StoreG: { |
| 5436 | IRStoreG* sg = st->Ist.StoreG.details; |
| 5437 | return IRStmt_StoreG(sg->end, |
| 5438 | atbSubst_Expr(env, sg->addr), |
| 5439 | atbSubst_Expr(env, sg->data), |
| 5440 | atbSubst_Expr(env, sg->guard)); |
| 5441 | } |
| 5442 | case Ist_LoadG: { |
| 5443 | IRLoadG* lg = st->Ist.LoadG.details; |
| 5444 | return IRStmt_LoadG(lg->end, lg->cvt, lg->dst, |
| 5445 | atbSubst_Expr(env, lg->addr), |
| 5446 | atbSubst_Expr(env, lg->alt), |
| 5447 | atbSubst_Expr(env, lg->guard)); |
| 5448 | } |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 5449 | case Ist_WrTmp: |
| 5450 | return IRStmt_WrTmp( |
| 5451 | st->Ist.WrTmp.tmp, |
| 5452 | atbSubst_Expr(env, st->Ist.WrTmp.data) |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5453 | ); |
| 5454 | case Ist_Put: |
| 5455 | return IRStmt_Put( |
| 5456 | st->Ist.Put.offset, |
| 5457 | atbSubst_Expr(env, st->Ist.Put.data) |
| 5458 | ); |
| 5459 | case Ist_PutI: |
florian | d6f38b3 | 2012-05-31 15:46:18 +0000 | [diff] [blame] | 5460 | puti = st->Ist.PutI.details; |
| 5461 | puti2 = mkIRPutI(puti->descr, |
| 5462 | atbSubst_Expr(env, puti->ix), |
| 5463 | puti->bias, |
| 5464 | atbSubst_Expr(env, puti->data)); |
| 5465 | return IRStmt_PutI(puti2); |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5466 | |
| 5467 | case Ist_Exit: |
| 5468 | return IRStmt_Exit( |
| 5469 | atbSubst_Expr(env, st->Ist.Exit.guard), |
| 5470 | st->Ist.Exit.jk, |
sewardj | c6f970f | 2012-04-02 21:54:49 +0000 | [diff] [blame] | 5471 | st->Ist.Exit.dst, |
| 5472 | st->Ist.Exit.offsIP |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5473 | ); |
| 5474 | case Ist_IMark: |
sewardj | 2f10aa6 | 2011-05-27 13:20:56 +0000 | [diff] [blame] | 5475 | return IRStmt_IMark(st->Ist.IMark.addr, |
| 5476 | st->Ist.IMark.len, |
| 5477 | st->Ist.IMark.delta); |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5478 | case Ist_NoOp: |
| 5479 | return IRStmt_NoOp(); |
sewardj | c4356f0 | 2007-11-09 21:15:04 +0000 | [diff] [blame] | 5480 | case Ist_MBE: |
| 5481 | return IRStmt_MBE(st->Ist.MBE.event); |
sewardj | e9d8a26 | 2009-07-01 08:06:34 +0000 | [diff] [blame] | 5482 | case Ist_CAS: |
| 5483 | cas = st->Ist.CAS.details; |
| 5484 | cas2 = mkIRCAS( |
| 5485 | cas->oldHi, cas->oldLo, cas->end, |
| 5486 | atbSubst_Expr(env, cas->addr), |
| 5487 | cas->expdHi ? atbSubst_Expr(env, cas->expdHi) : NULL, |
| 5488 | atbSubst_Expr(env, cas->expdLo), |
| 5489 | cas->dataHi ? atbSubst_Expr(env, cas->dataHi) : NULL, |
| 5490 | atbSubst_Expr(env, cas->dataLo) |
| 5491 | ); |
| 5492 | return IRStmt_CAS(cas2); |
sewardj | e768e92 | 2009-11-26 17:17:37 +0000 | [diff] [blame] | 5493 | case Ist_LLSC: |
| 5494 | return IRStmt_LLSC( |
| 5495 | st->Ist.LLSC.end, |
| 5496 | st->Ist.LLSC.result, |
| 5497 | atbSubst_Expr(env, st->Ist.LLSC.addr), |
| 5498 | st->Ist.LLSC.storedata |
| 5499 | ? atbSubst_Expr(env, st->Ist.LLSC.storedata) : NULL |
| 5500 | ); |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5501 | case Ist_Dirty: |
| 5502 | d = st->Ist.Dirty.details; |
| 5503 | d2 = emptyIRDirty(); |
| 5504 | *d2 = *d; |
| 5505 | if (d2->mFx != Ifx_None) |
| 5506 | d2->mAddr = atbSubst_Expr(env, d2->mAddr); |
| 5507 | d2->guard = atbSubst_Expr(env, d2->guard); |
sewardj | 74142b8 | 2013-08-08 10:28:59 +0000 | [diff] [blame] | 5508 | for (i = 0; d2->args[i]; i++) { |
| 5509 | IRExpr* arg = d2->args[i]; |
florian | 9041956 | 2013-08-15 20:54:52 +0000 | [diff] [blame] | 5510 | if (LIKELY(!is_IRExpr_VECRET_or_BBPTR(arg))) |
sewardj | 74142b8 | 2013-08-08 10:28:59 +0000 | [diff] [blame] | 5511 | d2->args[i] = atbSubst_Expr(env, arg); |
| 5512 | } |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5513 | return IRStmt_Dirty(d2); |
| 5514 | default: |
| 5515 | vex_printf("\n"); ppIRStmt(st); vex_printf("\n"); |
| 5516 | vpanic("atbSubst_Stmt"); |
| 5517 | } |
| 5518 | } |
| 5519 | |
florian | 088809c | 2012-12-30 18:17:18 +0000 | [diff] [blame] | 5520 | inline |
| 5521 | static Bool dirty_helper_stores ( const IRDirty *d ) |
| 5522 | { |
| 5523 | return d->mFx == Ifx_Write || d->mFx == Ifx_Modify; |
| 5524 | } |
| 5525 | |
| 5526 | inline |
florian | 740da72 | 2013-09-11 17:58:32 +0000 | [diff] [blame] | 5527 | static Interval dirty_helper_puts ( const IRDirty *d, |
| 5528 | Bool (*preciseMemExnsFn)(Int, Int), |
| 5529 | Bool *requiresPreciseMemExns ) |
florian | 088809c | 2012-12-30 18:17:18 +0000 | [diff] [blame] | 5530 | { |
| 5531 | Int i; |
florian | 740da72 | 2013-09-11 17:58:32 +0000 | [diff] [blame] | 5532 | Interval interval; |
florian | 088809c | 2012-12-30 18:17:18 +0000 | [diff] [blame] | 5533 | |
| 5534 | /* Passing the guest state pointer opens the door to modifying the |
sewardj | 74142b8 | 2013-08-08 10:28:59 +0000 | [diff] [blame] | 5535 | guest state under the covers. It's not allowed, but let's be |
florian | 088809c | 2012-12-30 18:17:18 +0000 | [diff] [blame] | 5536 | extra conservative and assume the worst. */ |
sewardj | 74142b8 | 2013-08-08 10:28:59 +0000 | [diff] [blame] | 5537 | for (i = 0; d->args[i]; i++) { |
florian | 9041956 | 2013-08-15 20:54:52 +0000 | [diff] [blame] | 5538 | if (UNLIKELY(d->args[i]->tag == Iex_BBPTR)) { |
sewardj | 74142b8 | 2013-08-08 10:28:59 +0000 | [diff] [blame] | 5539 | *requiresPreciseMemExns = True; |
florian | 740da72 | 2013-09-11 17:58:32 +0000 | [diff] [blame] | 5540 | /* Assume all guest state is written. */ |
| 5541 | interval.present = True; |
| 5542 | interval.low = 0; |
| 5543 | interval.high = 0x7FFFFFFF; |
| 5544 | return interval; |
sewardj | 74142b8 | 2013-08-08 10:28:59 +0000 | [diff] [blame] | 5545 | } |
florian | 088809c | 2012-12-30 18:17:18 +0000 | [diff] [blame] | 5546 | } |
| 5547 | |
florian | 62140c1 | 2013-01-20 03:51:04 +0000 | [diff] [blame] | 5548 | /* Check the side effects on the guest state */ |
florian | 740da72 | 2013-09-11 17:58:32 +0000 | [diff] [blame] | 5549 | interval.present = False; |
| 5550 | interval.low = interval.high = -1; |
florian | 62140c1 | 2013-01-20 03:51:04 +0000 | [diff] [blame] | 5551 | *requiresPreciseMemExns = False; |
| 5552 | |
| 5553 | for (i = 0; i < d->nFxState; ++i) { |
| 5554 | if (d->fxState[i].fx != Ifx_Read) { |
| 5555 | Int offset = d->fxState[i].offset; |
| 5556 | Int size = d->fxState[i].size; |
| 5557 | Int nRepeats = d->fxState[i].nRepeats; |
| 5558 | Int repeatLen = d->fxState[i].repeatLen; |
| 5559 | |
sewardj | 74142b8 | 2013-08-08 10:28:59 +0000 | [diff] [blame] | 5560 | if (preciseMemExnsFn(offset, |
| 5561 | offset + nRepeats * repeatLen + size - 1)) { |
florian | 62140c1 | 2013-01-20 03:51:04 +0000 | [diff] [blame] | 5562 | *requiresPreciseMemExns = True; |
florian | 62140c1 | 2013-01-20 03:51:04 +0000 | [diff] [blame] | 5563 | } |
florian | 740da72 | 2013-09-11 17:58:32 +0000 | [diff] [blame] | 5564 | update_interval(&interval, offset, |
| 5565 | offset + nRepeats * repeatLen + size - 1); |
florian | 62140c1 | 2013-01-20 03:51:04 +0000 | [diff] [blame] | 5566 | } |
| 5567 | } |
| 5568 | |
florian | 740da72 | 2013-09-11 17:58:32 +0000 | [diff] [blame] | 5569 | return interval; |
florian | 088809c | 2012-12-30 18:17:18 +0000 | [diff] [blame] | 5570 | } |
| 5571 | |
florian | 740da72 | 2013-09-11 17:58:32 +0000 | [diff] [blame] | 5572 | /* Return an interval if st modifies the guest state. Via requiresPreciseMemExns |
florian | 62140c1 | 2013-01-20 03:51:04 +0000 | [diff] [blame] | 5573 | return whether or not that modification requires precise exceptions. */ |
florian | 740da72 | 2013-09-11 17:58:32 +0000 | [diff] [blame] | 5574 | static Interval stmt_modifies_guest_state ( IRSB *bb, const IRStmt *st, |
| 5575 | Bool (*preciseMemExnsFn)(Int,Int), |
| 5576 | Bool *requiresPreciseMemExns ) |
florian | 62140c1 | 2013-01-20 03:51:04 +0000 | [diff] [blame] | 5577 | { |
florian | 740da72 | 2013-09-11 17:58:32 +0000 | [diff] [blame] | 5578 | Interval interval; |
| 5579 | |
florian | 62140c1 | 2013-01-20 03:51:04 +0000 | [diff] [blame] | 5580 | switch (st->tag) { |
| 5581 | case Ist_Put: { |
| 5582 | Int offset = st->Ist.Put.offset; |
| 5583 | Int size = sizeofIRType(typeOfIRExpr(bb->tyenv, st->Ist.Put.data)); |
| 5584 | |
| 5585 | *requiresPreciseMemExns = preciseMemExnsFn(offset, offset + size - 1); |
florian | 740da72 | 2013-09-11 17:58:32 +0000 | [diff] [blame] | 5586 | interval.present = True; |
| 5587 | interval.low = offset; |
| 5588 | interval.high = offset + size - 1; |
| 5589 | return interval; |
florian | 62140c1 | 2013-01-20 03:51:04 +0000 | [diff] [blame] | 5590 | } |
| 5591 | |
| 5592 | case Ist_PutI: { |
| 5593 | IRRegArray *descr = st->Ist.PutI.details->descr; |
| 5594 | Int offset = descr->base; |
| 5595 | Int size = sizeofIRType(descr->elemTy); |
| 5596 | |
| 5597 | /* We quietly assume here that all segments are contiguous and there |
| 5598 | are no holes. This is to avoid a loop. The assumption is conservative |
| 5599 | in the sense that we might report that precise memory exceptions are |
| 5600 | needed when in fact they are not. */ |
| 5601 | *requiresPreciseMemExns = |
| 5602 | preciseMemExnsFn(offset, offset + descr->nElems * size - 1); |
florian | 740da72 | 2013-09-11 17:58:32 +0000 | [diff] [blame] | 5603 | interval.present = True; |
| 5604 | interval.low = offset; |
| 5605 | interval.high = offset + descr->nElems * size - 1; |
| 5606 | return interval; |
florian | 62140c1 | 2013-01-20 03:51:04 +0000 | [diff] [blame] | 5607 | } |
| 5608 | |
| 5609 | case Ist_Dirty: |
| 5610 | return dirty_helper_puts(st->Ist.Dirty.details, preciseMemExnsFn, |
| 5611 | requiresPreciseMemExns); |
| 5612 | |
| 5613 | default: |
| 5614 | *requiresPreciseMemExns = False; |
florian | 740da72 | 2013-09-11 17:58:32 +0000 | [diff] [blame] | 5615 | interval.present = False; |
| 5616 | interval.low = -1; |
| 5617 | interval.high = -1; |
| 5618 | return interval; |
florian | 62140c1 | 2013-01-20 03:51:04 +0000 | [diff] [blame] | 5619 | } |
| 5620 | } |
| 5621 | |
florian | dcd6d23 | 2015-01-02 17:32:21 +0000 | [diff] [blame] | 5622 | /* notstatic */ Addr ado_treebuild_BB ( IRSB* bb, |
| 5623 | Bool (*preciseMemExnsFn)(Int,Int) ) |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5624 | { |
| 5625 | Int i, j, k, m; |
florian | 740da72 | 2013-09-11 17:58:32 +0000 | [diff] [blame] | 5626 | Bool stmtStores, invalidateMe; |
| 5627 | Interval putInterval; |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 5628 | IRStmt* st; |
| 5629 | IRStmt* st2; |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5630 | ATmpInfo env[A_NENV]; |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 5631 | |
sewardj | c6f970f | 2012-04-02 21:54:49 +0000 | [diff] [blame] | 5632 | Bool max_ga_known = False; |
florian | dcd6d23 | 2015-01-02 17:32:21 +0000 | [diff] [blame] | 5633 | Addr max_ga = 0; |
sewardj | c6f970f | 2012-04-02 21:54:49 +0000 | [diff] [blame] | 5634 | |
sewardj | c9ad115 | 2004-10-14 00:08:21 +0000 | [diff] [blame] | 5635 | Int n_tmps = bb->tyenv->types_used; |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5636 | UShort* uses = LibVEX_Alloc(n_tmps * sizeof(UShort)); |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 5637 | |
| 5638 | /* Phase 1. Scan forwards in bb, counting use occurrences of each |
sewardj | c6f970f | 2012-04-02 21:54:49 +0000 | [diff] [blame] | 5639 | temp. Also count occurrences in the bb->next field. Take the |
| 5640 | opportunity to also find the maximum guest address in the block, |
| 5641 | since that will be needed later for deciding when we can safely |
| 5642 | elide event checks. */ |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 5643 | |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5644 | for (i = 0; i < n_tmps; i++) |
| 5645 | uses[i] = 0; |
| 5646 | |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 5647 | for (i = 0; i < bb->stmts_used; i++) { |
| 5648 | st = bb->stmts[i]; |
sewardj | c6f970f | 2012-04-02 21:54:49 +0000 | [diff] [blame] | 5649 | switch (st->tag) { |
| 5650 | case Ist_NoOp: |
| 5651 | continue; |
| 5652 | case Ist_IMark: { |
florian | dcd6d23 | 2015-01-02 17:32:21 +0000 | [diff] [blame] | 5653 | UInt len = st->Ist.IMark.len; |
| 5654 | Addr mga = st->Ist.IMark.addr + (len < 1 ? 1 : len) - 1; |
sewardj | c6f970f | 2012-04-02 21:54:49 +0000 | [diff] [blame] | 5655 | max_ga_known = True; |
| 5656 | if (mga > max_ga) |
| 5657 | max_ga = mga; |
| 5658 | break; |
| 5659 | } |
| 5660 | default: |
| 5661 | break; |
| 5662 | } |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5663 | aoccCount_Stmt( uses, st ); |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 5664 | } |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5665 | aoccCount_Expr(uses, bb->next ); |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 5666 | |
sewardj | c9ad115 | 2004-10-14 00:08:21 +0000 | [diff] [blame] | 5667 | # if 0 |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5668 | for (i = 0; i < n_tmps; i++) { |
| 5669 | if (uses[i] == 0) |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 5670 | continue; |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5671 | ppIRTemp( (IRTemp)i ); |
| 5672 | vex_printf(" used %d\n", (Int)uses[i] ); |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 5673 | } |
sewardj | c9ad115 | 2004-10-14 00:08:21 +0000 | [diff] [blame] | 5674 | # endif |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 5675 | |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5676 | /* Phase 2. Scan forwards in bb. For each statement in turn: |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 5677 | |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5678 | If the env is full, emit the end element. This guarantees |
| 5679 | there is at least one free slot in the following. |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 5680 | |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5681 | On seeing 't = E', occ(t)==1, |
| 5682 | let E'=env(E) |
| 5683 | delete this stmt |
| 5684 | add t -> E' to the front of the env |
| 5685 | Examine E' and set the hints for E' appropriately |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 5686 | (doesLoad? doesGet?) |
| 5687 | |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5688 | On seeing any other stmt, |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 5689 | let stmt' = env(stmt) |
| 5690 | remove from env any 't=E' binds invalidated by stmt |
| 5691 | emit the invalidated stmts |
| 5692 | emit stmt' |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5693 | compact any holes in env |
| 5694 | by sliding entries towards the front |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 5695 | |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5696 | Finally, apply env to bb->next. |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 5697 | */ |
| 5698 | |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5699 | for (i = 0; i < A_NENV; i++) { |
| 5700 | env[i].bindee = NULL; |
| 5701 | env[i].binder = IRTemp_INVALID; |
| 5702 | } |
| 5703 | |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 5704 | /* The stmts in bb are being reordered, and we are guaranteed to |
| 5705 | end up with no more than the number we started with. Use i to |
| 5706 | be the cursor of the current stmt examined and j <= i to be that |
| 5707 | for the current stmt being written. |
| 5708 | */ |
| 5709 | j = 0; |
| 5710 | for (i = 0; i < bb->stmts_used; i++) { |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5711 | |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 5712 | st = bb->stmts[i]; |
sewardj | 8bee6d1 | 2005-03-22 02:24:05 +0000 | [diff] [blame] | 5713 | if (st->tag == Ist_NoOp) |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 5714 | continue; |
| 5715 | |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5716 | /* Ensure there's at least one space in the env, by emitting |
| 5717 | the oldest binding if necessary. */ |
| 5718 | if (env[A_NENV-1].bindee != NULL) { |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 5719 | bb->stmts[j] = IRStmt_WrTmp( env[A_NENV-1].binder, |
| 5720 | env[A_NENV-1].bindee ); |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5721 | j++; |
| 5722 | vassert(j <= i); |
| 5723 | env[A_NENV-1].bindee = NULL; |
| 5724 | } |
| 5725 | |
| 5726 | /* Consider current stmt. */ |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 5727 | if (st->tag == Ist_WrTmp && uses[st->Ist.WrTmp.tmp] <= 1) { |
sewardj | 6332740 | 2006-01-25 03:26:27 +0000 | [diff] [blame] | 5728 | IRExpr *e, *e2; |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5729 | |
| 5730 | /* optional extra: dump dead bindings as we find them. |
| 5731 | Removes the need for a prior dead-code removal pass. */ |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 5732 | if (uses[st->Ist.WrTmp.tmp] == 0) { |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 5733 | if (0) vex_printf("DEAD binding\n"); |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5734 | continue; /* for (i = 0; i < bb->stmts_used; i++) loop */ |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 5735 | } |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 5736 | vassert(uses[st->Ist.WrTmp.tmp] == 1); |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5737 | |
| 5738 | /* ok, we have 't = E', occ(t)==1. Do the abovementioned |
| 5739 | actions. */ |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 5740 | e = st->Ist.WrTmp.data; |
sewardj | 6332740 | 2006-01-25 03:26:27 +0000 | [diff] [blame] | 5741 | e2 = atbSubst_Expr(env, e); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 5742 | addToEnvFront(env, st->Ist.WrTmp.tmp, e2); |
florian | 740da72 | 2013-09-11 17:58:32 +0000 | [diff] [blame] | 5743 | setHints_Expr(&env[0].doesLoad, &env[0].getInterval, e2); |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5744 | /* don't advance j, as we are deleting this stmt and instead |
| 5745 | holding it temporarily in the env. */ |
| 5746 | continue; /* for (i = 0; i < bb->stmts_used; i++) loop */ |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 5747 | } |
| 5748 | |
| 5749 | /* we get here for any other kind of statement. */ |
| 5750 | /* 'use up' any bindings required by the current statement. */ |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5751 | st2 = atbSubst_Stmt(env, st); |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 5752 | |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5753 | /* Now, before this stmt, dump any bindings in env that it |
| 5754 | invalidates. These need to be dumped in the order in which |
| 5755 | they originally entered env -- that means from oldest to |
| 5756 | youngest. */ |
sewardj | 3e83893 | 2005-01-07 12:09:15 +0000 | [diff] [blame] | 5757 | |
florian | 740da72 | 2013-09-11 17:58:32 +0000 | [diff] [blame] | 5758 | /* putInterval/stmtStores characterise what the stmt under |
sewardj | e9d8a26 | 2009-07-01 08:06:34 +0000 | [diff] [blame] | 5759 | consideration does, or might do (sidely safe @ True). */ |
florian | 62140c1 | 2013-01-20 03:51:04 +0000 | [diff] [blame] | 5760 | |
| 5761 | Bool putRequiresPreciseMemExns; |
florian | 740da72 | 2013-09-11 17:58:32 +0000 | [diff] [blame] | 5762 | putInterval = stmt_modifies_guest_state( bb, st, preciseMemExnsFn, |
| 5763 | &putRequiresPreciseMemExns); |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 5764 | |
sewardj | e9d8a26 | 2009-07-01 08:06:34 +0000 | [diff] [blame] | 5765 | /* be True if this stmt writes memory or might do (==> we don't |
| 5766 | want to reorder other loads or stores relative to it). Also, |
sewardj | e768e92 | 2009-11-26 17:17:37 +0000 | [diff] [blame] | 5767 | both LL and SC fall under this classification, since we |
sewardj | e9d8a26 | 2009-07-01 08:06:34 +0000 | [diff] [blame] | 5768 | really ought to be conservative and not reorder any other |
sewardj | e768e92 | 2009-11-26 17:17:37 +0000 | [diff] [blame] | 5769 | memory transactions relative to them. */ |
sewardj | e9d8a26 | 2009-07-01 08:06:34 +0000 | [diff] [blame] | 5770 | stmtStores |
| 5771 | = toBool( st->tag == Ist_Store |
florian | 088809c | 2012-12-30 18:17:18 +0000 | [diff] [blame] | 5772 | || (st->tag == Ist_Dirty |
| 5773 | && dirty_helper_stores(st->Ist.Dirty.details)) |
sewardj | 0f62198 | 2012-04-12 21:05:16 +0000 | [diff] [blame] | 5774 | || st->tag == Ist_LLSC |
| 5775 | || st->tag == Ist_CAS ); |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 5776 | |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5777 | for (k = A_NENV-1; k >= 0; k--) { |
| 5778 | if (env[k].bindee == NULL) |
| 5779 | continue; |
| 5780 | /* Compare the actions of this stmt with the actions of |
| 5781 | binding 'k', to see if they invalidate the binding. */ |
| 5782 | invalidateMe |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 5783 | = toBool( |
| 5784 | /* a store invalidates loaded data */ |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5785 | (env[k].doesLoad && stmtStores) |
florian | 740da72 | 2013-09-11 17:58:32 +0000 | [diff] [blame] | 5786 | /* a put invalidates get'd data, if they overlap */ |
| 5787 | || ((env[k].getInterval.present && putInterval.present) && |
| 5788 | intervals_overlap(env[k].getInterval, putInterval)) |
florian | 62140c1 | 2013-01-20 03:51:04 +0000 | [diff] [blame] | 5789 | /* a put invalidates loaded data. That means, in essense, that |
| 5790 | a load expression cannot be substituted into a statement |
| 5791 | that follows the put. But there is nothing wrong doing so |
| 5792 | except when the put statement requries precise exceptions. |
| 5793 | Think of a load that is moved past a put where the put |
| 5794 | updates the IP in the guest state. If the load generates |
| 5795 | a segfault, the wrong address (line number) would be |
| 5796 | reported. */ |
florian | 740da72 | 2013-09-11 17:58:32 +0000 | [diff] [blame] | 5797 | || (env[k].doesLoad && putInterval.present && |
| 5798 | putRequiresPreciseMemExns) |
sewardj | c4356f0 | 2007-11-09 21:15:04 +0000 | [diff] [blame] | 5799 | /* probably overly conservative: a memory bus event |
sewardj | 3e83893 | 2005-01-07 12:09:15 +0000 | [diff] [blame] | 5800 | invalidates absolutely everything, so that all |
| 5801 | computation prior to it is forced to complete before |
sewardj | c4356f0 | 2007-11-09 21:15:04 +0000 | [diff] [blame] | 5802 | proceeding with the event (fence,lock,unlock). */ |
| 5803 | || st->tag == Ist_MBE |
sewardj | 5a9ffab | 2005-05-12 17:55:01 +0000 | [diff] [blame] | 5804 | /* also be (probably overly) paranoid re AbiHints */ |
| 5805 | || st->tag == Ist_AbiHint |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 5806 | ); |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5807 | if (invalidateMe) { |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 5808 | bb->stmts[j] = IRStmt_WrTmp( env[k].binder, env[k].bindee ); |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5809 | j++; |
| 5810 | vassert(j <= i); |
| 5811 | env[k].bindee = NULL; |
| 5812 | } |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 5813 | } |
| 5814 | |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5815 | /* Slide in-use entries in env up to the front */ |
| 5816 | m = 0; |
| 5817 | for (k = 0; k < A_NENV; k++) { |
| 5818 | if (env[k].bindee != NULL) { |
| 5819 | env[m] = env[k]; |
| 5820 | m++; |
| 5821 | } |
| 5822 | } |
| 5823 | for (m = m; m < A_NENV; m++) { |
| 5824 | env[m].bindee = NULL; |
| 5825 | } |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 5826 | |
| 5827 | /* finally, emit the substituted statement */ |
| 5828 | bb->stmts[j] = st2; |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5829 | /* vex_printf("**2 "); ppIRStmt(bb->stmts[j]); vex_printf("\n"); */ |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 5830 | j++; |
| 5831 | |
| 5832 | vassert(j <= i+1); |
| 5833 | } /* for each stmt in the original bb ... */ |
| 5834 | |
| 5835 | /* Finally ... substitute the ->next field as much as possible, and |
| 5836 | dump any left-over bindings. Hmm. Perhaps there should be no |
| 5837 | left over bindings? Or any left-over bindings are |
| 5838 | by definition dead? */ |
sewardj | f9517d0 | 2005-11-28 13:39:37 +0000 | [diff] [blame] | 5839 | bb->next = atbSubst_Expr(env, bb->next); |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 5840 | bb->stmts_used = j; |
sewardj | c6f970f | 2012-04-02 21:54:49 +0000 | [diff] [blame] | 5841 | |
florian | dcd6d23 | 2015-01-02 17:32:21 +0000 | [diff] [blame] | 5842 | return max_ga_known ? max_ga : ~(Addr)0; |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 5843 | } |
| 5844 | |
| 5845 | |
sewardj | 695cff9 | 2004-10-13 14:50:14 +0000 | [diff] [blame] | 5846 | /*---------------------------------------------------------------*/ |
sewardj | edf4d69 | 2004-08-17 13:52:58 +0000 | [diff] [blame] | 5847 | /*--- iropt main ---*/ |
| 5848 | /*---------------------------------------------------------------*/ |
| 5849 | |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 5850 | static Bool iropt_verbose = False; /* True; */ |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 5851 | |
| 5852 | |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 5853 | /* Do a simple cleanup pass on bb. This is: redundant Get removal, |
| 5854 | redundant Put removal, constant propagation, dead code removal, |
| 5855 | clean helper specialisation, and dead code removal (again). |
sewardj | b923075 | 2004-12-29 19:25:06 +0000 | [diff] [blame] | 5856 | */ |
sewardj | 695cff9 | 2004-10-13 14:50:14 +0000 | [diff] [blame] | 5857 | |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 5858 | |
| 5859 | static |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 5860 | IRSB* cheap_transformations ( |
| 5861 | IRSB* bb, |
florian | 1ff4756 | 2012-10-21 02:09:51 +0000 | [diff] [blame] | 5862 | IRExpr* (*specHelper) (const HChar*, IRExpr**, IRStmt**, Int), |
sewardj | 8d2291c | 2004-10-25 14:50:21 +0000 | [diff] [blame] | 5863 | Bool (*preciseMemExnsFn)(Int,Int) |
| 5864 | ) |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 5865 | { |
| 5866 | redundant_get_removal_BB ( bb ); |
| 5867 | if (iropt_verbose) { |
| 5868 | vex_printf("\n========= REDUNDANT GET\n\n" ); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 5869 | ppIRSB(bb); |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 5870 | } |
sewardj | 044a215 | 2004-10-21 10:22:10 +0000 | [diff] [blame] | 5871 | |
philippe | 6c46bef | 2012-08-14 22:29:01 +0000 | [diff] [blame] | 5872 | if (vex_control.iropt_register_updates < VexRegUpdAllregsAtEachInsn) { |
philippe | c8e2f98 | 2012-08-01 22:04:13 +0000 | [diff] [blame] | 5873 | redundant_put_removal_BB ( bb, preciseMemExnsFn ); |
| 5874 | } |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 5875 | if (iropt_verbose) { |
| 5876 | vex_printf("\n========= REDUNDANT PUT\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 | } |
sewardj | 044a215 | 2004-10-21 10:22:10 +0000 | [diff] [blame] | 5879 | |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 5880 | bb = cprop_BB ( bb ); |
| 5881 | if (iropt_verbose) { |
| 5882 | vex_printf("\n========= CPROPD\n\n" ); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 5883 | ppIRSB(bb); |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 5884 | } |
| 5885 | |
sewardj | 49651f4 | 2004-10-28 22:11:04 +0000 | [diff] [blame] | 5886 | do_deadcode_BB ( bb ); |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 5887 | if (iropt_verbose) { |
| 5888 | vex_printf("\n========= DEAD\n\n" ); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 5889 | ppIRSB(bb); |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 5890 | } |
| 5891 | |
sewardj | b923075 | 2004-12-29 19:25:06 +0000 | [diff] [blame] | 5892 | bb = spec_helpers_BB ( bb, specHelper ); |
sewardj | 49651f4 | 2004-10-28 22:11:04 +0000 | [diff] [blame] | 5893 | do_deadcode_BB ( bb ); |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 5894 | if (iropt_verbose) { |
| 5895 | vex_printf("\n========= SPECd \n\n" ); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 5896 | ppIRSB(bb); |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 5897 | } |
| 5898 | |
| 5899 | return bb; |
| 5900 | } |
| 5901 | |
sewardj | 695cff9 | 2004-10-13 14:50:14 +0000 | [diff] [blame] | 5902 | |
| 5903 | /* Do some more expensive transformations on bb, which are aimed at |
| 5904 | optimising as much as possible in the presence of GetI and PutI. */ |
| 5905 | |
| 5906 | static |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 5907 | IRSB* expensive_transformations( IRSB* bb ) |
sewardj | 695cff9 | 2004-10-13 14:50:14 +0000 | [diff] [blame] | 5908 | { |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 5909 | (void)do_cse_BB( bb ); |
sewardj | 695cff9 | 2004-10-13 14:50:14 +0000 | [diff] [blame] | 5910 | collapse_AddSub_chains_BB( bb ); |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 5911 | do_redundant_GetI_elimination( bb ); |
philippe | 6c46bef | 2012-08-14 22:29:01 +0000 | [diff] [blame] | 5912 | if (vex_control.iropt_register_updates < VexRegUpdAllregsAtEachInsn) { |
philippe | c8e2f98 | 2012-08-01 22:04:13 +0000 | [diff] [blame] | 5913 | do_redundant_PutI_elimination( bb ); |
| 5914 | } |
sewardj | 49651f4 | 2004-10-28 22:11:04 +0000 | [diff] [blame] | 5915 | do_deadcode_BB( bb ); |
sewardj | b923075 | 2004-12-29 19:25:06 +0000 | [diff] [blame] | 5916 | return bb; |
sewardj | 695cff9 | 2004-10-13 14:50:14 +0000 | [diff] [blame] | 5917 | } |
| 5918 | |
| 5919 | |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 5920 | /* Scan a flattened BB to look for signs that more expensive |
| 5921 | optimisations might be useful: |
| 5922 | - find out if there are any GetIs and PutIs |
| 5923 | - find out if there are any floating or vector-typed temporaries |
| 5924 | */ |
sewardj | 695cff9 | 2004-10-13 14:50:14 +0000 | [diff] [blame] | 5925 | |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 5926 | static void considerExpensives ( /*OUT*/Bool* hasGetIorPutI, |
| 5927 | /*OUT*/Bool* hasVorFtemps, |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 5928 | IRSB* bb ) |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 5929 | { |
sewardj | e9d8a26 | 2009-07-01 08:06:34 +0000 | [diff] [blame] | 5930 | Int i, j; |
| 5931 | IRStmt* st; |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 5932 | IRDirty* d; |
sewardj | e9d8a26 | 2009-07-01 08:06:34 +0000 | [diff] [blame] | 5933 | IRCAS* cas; |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 5934 | |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 5935 | *hasGetIorPutI = False; |
| 5936 | *hasVorFtemps = False; |
| 5937 | |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 5938 | for (i = 0; i < bb->stmts_used; i++) { |
| 5939 | st = bb->stmts[i]; |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 5940 | switch (st->tag) { |
sewardj | 5a9ffab | 2005-05-12 17:55:01 +0000 | [diff] [blame] | 5941 | case Ist_AbiHint: |
| 5942 | vassert(isIRAtom(st->Ist.AbiHint.base)); |
sewardj | 478646f | 2008-05-01 20:13:04 +0000 | [diff] [blame] | 5943 | vassert(isIRAtom(st->Ist.AbiHint.nia)); |
sewardj | 5a9ffab | 2005-05-12 17:55:01 +0000 | [diff] [blame] | 5944 | break; |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 5945 | case Ist_PutI: |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 5946 | *hasGetIorPutI = True; |
| 5947 | break; |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 5948 | case Ist_WrTmp: |
| 5949 | if (st->Ist.WrTmp.data->tag == Iex_GetI) |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 5950 | *hasGetIorPutI = True; |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 5951 | switch (typeOfIRTemp(bb->tyenv, st->Ist.WrTmp.tmp)) { |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 5952 | case Ity_I1: case Ity_I8: case Ity_I16: |
| 5953 | case Ity_I32: case Ity_I64: case Ity_I128: |
| 5954 | break; |
sewardj | c4530ae | 2012-05-21 10:18:49 +0000 | [diff] [blame] | 5955 | case Ity_F32: case Ity_F64: case Ity_F128: |
| 5956 | case Ity_V128: case Ity_V256: |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 5957 | *hasVorFtemps = True; |
| 5958 | break; |
sewardj | c6bbd47 | 2012-04-02 10:20:48 +0000 | [diff] [blame] | 5959 | case Ity_D32: case Ity_D64: case Ity_D128: |
| 5960 | *hasVorFtemps = True; |
| 5961 | break; |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 5962 | default: |
| 5963 | goto bad; |
| 5964 | } |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 5965 | break; |
| 5966 | case Ist_Put: |
sewardj | 496a58d | 2005-03-20 18:44:44 +0000 | [diff] [blame] | 5967 | vassert(isIRAtom(st->Ist.Put.data)); |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 5968 | break; |
sewardj | af1ceca | 2005-06-30 23:31:27 +0000 | [diff] [blame] | 5969 | case Ist_Store: |
| 5970 | vassert(isIRAtom(st->Ist.Store.addr)); |
| 5971 | vassert(isIRAtom(st->Ist.Store.data)); |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 5972 | break; |
sewardj | cfe046e | 2013-01-17 14:23:53 +0000 | [diff] [blame] | 5973 | case Ist_StoreG: { |
| 5974 | IRStoreG* sg = st->Ist.StoreG.details; |
| 5975 | vassert(isIRAtom(sg->addr)); |
| 5976 | vassert(isIRAtom(sg->data)); |
| 5977 | vassert(isIRAtom(sg->guard)); |
| 5978 | break; |
| 5979 | } |
| 5980 | case Ist_LoadG: { |
| 5981 | IRLoadG* lg = st->Ist.LoadG.details; |
| 5982 | vassert(isIRAtom(lg->addr)); |
| 5983 | vassert(isIRAtom(lg->alt)); |
| 5984 | vassert(isIRAtom(lg->guard)); |
| 5985 | break; |
| 5986 | } |
sewardj | e9d8a26 | 2009-07-01 08:06:34 +0000 | [diff] [blame] | 5987 | case Ist_CAS: |
| 5988 | cas = st->Ist.CAS.details; |
| 5989 | vassert(isIRAtom(cas->addr)); |
| 5990 | vassert(cas->expdHi == NULL || isIRAtom(cas->expdHi)); |
| 5991 | vassert(isIRAtom(cas->expdLo)); |
| 5992 | vassert(cas->dataHi == NULL || isIRAtom(cas->dataHi)); |
| 5993 | vassert(isIRAtom(cas->dataLo)); |
| 5994 | break; |
sewardj | e768e92 | 2009-11-26 17:17:37 +0000 | [diff] [blame] | 5995 | case Ist_LLSC: |
| 5996 | vassert(isIRAtom(st->Ist.LLSC.addr)); |
| 5997 | if (st->Ist.LLSC.storedata) |
| 5998 | vassert(isIRAtom(st->Ist.LLSC.storedata)); |
| 5999 | break; |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 6000 | case Ist_Dirty: |
| 6001 | d = st->Ist.Dirty.details; |
sewardj | 496a58d | 2005-03-20 18:44:44 +0000 | [diff] [blame] | 6002 | vassert(isIRAtom(d->guard)); |
sewardj | 74142b8 | 2013-08-08 10:28:59 +0000 | [diff] [blame] | 6003 | for (j = 0; d->args[j]; j++) { |
| 6004 | IRExpr* arg = d->args[j]; |
florian | 9041956 | 2013-08-15 20:54:52 +0000 | [diff] [blame] | 6005 | if (LIKELY(!is_IRExpr_VECRET_or_BBPTR(arg))) |
sewardj | 74142b8 | 2013-08-08 10:28:59 +0000 | [diff] [blame] | 6006 | vassert(isIRAtom(arg)); |
| 6007 | } |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 6008 | if (d->mFx != Ifx_None) |
sewardj | 496a58d | 2005-03-20 18:44:44 +0000 | [diff] [blame] | 6009 | vassert(isIRAtom(d->mAddr)); |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 6010 | break; |
sewardj | d2445f6 | 2005-03-21 00:15:53 +0000 | [diff] [blame] | 6011 | case Ist_NoOp: |
sewardj | f168931 | 2005-03-16 18:19:10 +0000 | [diff] [blame] | 6012 | case Ist_IMark: |
sewardj | c4356f0 | 2007-11-09 21:15:04 +0000 | [diff] [blame] | 6013 | case Ist_MBE: |
sewardj | 3e83893 | 2005-01-07 12:09:15 +0000 | [diff] [blame] | 6014 | break; |
| 6015 | case Ist_Exit: |
sewardj | 496a58d | 2005-03-20 18:44:44 +0000 | [diff] [blame] | 6016 | vassert(isIRAtom(st->Ist.Exit.guard)); |
sewardj | 3e83893 | 2005-01-07 12:09:15 +0000 | [diff] [blame] | 6017 | break; |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 6018 | default: |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 6019 | bad: |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 6020 | ppIRStmt(st); |
sewardj | e768e92 | 2009-11-26 17:17:37 +0000 | [diff] [blame] | 6021 | vpanic("considerExpensives"); |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 6022 | } |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 6023 | } |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 6024 | } |
| 6025 | |
| 6026 | |
sewardj | 695cff9 | 2004-10-13 14:50:14 +0000 | [diff] [blame] | 6027 | /* ---------------- The main iropt entry point. ---------------- */ |
| 6028 | |
sewardj | edf4d69 | 2004-08-17 13:52:58 +0000 | [diff] [blame] | 6029 | /* exported from this file */ |
sewardj | 695cff9 | 2004-10-13 14:50:14 +0000 | [diff] [blame] | 6030 | /* Rules of the game: |
| 6031 | |
| 6032 | - IRExpr/IRStmt trees should be treated as immutable, as they |
| 6033 | may get shared. So never change a field of such a tree node; |
| 6034 | instead construct and return a new one if needed. |
| 6035 | */ |
| 6036 | |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 6037 | |
sewardj | be91791 | 2010-08-22 12:38:53 +0000 | [diff] [blame] | 6038 | IRSB* do_iropt_BB( |
| 6039 | IRSB* bb0, |
florian | 1ff4756 | 2012-10-21 02:09:51 +0000 | [diff] [blame] | 6040 | IRExpr* (*specHelper) (const HChar*, IRExpr**, IRStmt**, Int), |
sewardj | be91791 | 2010-08-22 12:38:53 +0000 | [diff] [blame] | 6041 | Bool (*preciseMemExnsFn)(Int,Int), |
florian | d4cc0de | 2015-01-02 11:44:12 +0000 | [diff] [blame] | 6042 | Addr guest_addr, |
sewardj | be91791 | 2010-08-22 12:38:53 +0000 | [diff] [blame] | 6043 | VexArch guest_arch |
| 6044 | ) |
sewardj | edf4d69 | 2004-08-17 13:52:58 +0000 | [diff] [blame] | 6045 | { |
sewardj | 9d2e769 | 2005-02-07 01:11:31 +0000 | [diff] [blame] | 6046 | static Int n_total = 0; |
| 6047 | static Int n_expensive = 0; |
sewardj | 2963239 | 2004-08-22 02:38:11 +0000 | [diff] [blame] | 6048 | |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 6049 | Bool hasGetIorPutI, hasVorFtemps; |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 6050 | IRSB *bb, *bb2; |
sewardj | 8c2c10b | 2004-10-16 20:51:52 +0000 | [diff] [blame] | 6051 | |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 6052 | n_total++; |
| 6053 | |
| 6054 | /* First flatten the block out, since all other |
| 6055 | phases assume flat code. */ |
| 6056 | |
| 6057 | bb = flatten_BB ( bb0 ); |
| 6058 | |
| 6059 | if (iropt_verbose) { |
| 6060 | vex_printf("\n========= FLAT\n\n" ); |
sewardj | dd40fdf | 2006-12-24 02:20:24 +0000 | [diff] [blame] | 6061 | ppIRSB(bb); |
sewardj | 84be737 | 2004-08-18 13:59:33 +0000 | [diff] [blame] | 6062 | } |
sewardj | d721703 | 2004-08-19 10:49:10 +0000 | [diff] [blame] | 6063 | |
sewardj | 0821053 | 2004-12-29 17:09:11 +0000 | [diff] [blame] | 6064 | /* If at level 0, stop now. */ |
| 6065 | if (vex_control.iropt_level <= 0) return bb; |
| 6066 | |
sewardj | 695cff9 | 2004-10-13 14:50:14 +0000 | [diff] [blame] | 6067 | /* Now do a preliminary cleanup pass, and figure out if we also |
| 6068 | need to do 'expensive' optimisations. Expensive optimisations |
| 6069 | are deemed necessary if the block contains any GetIs or PutIs. |
| 6070 | If needed, do expensive transformations and then another cheap |
| 6071 | cleanup pass. */ |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 6072 | |
sewardj | 8d2291c | 2004-10-25 14:50:21 +0000 | [diff] [blame] | 6073 | bb = cheap_transformations( bb, specHelper, preciseMemExnsFn ); |
sewardj | 695cff9 | 2004-10-13 14:50:14 +0000 | [diff] [blame] | 6074 | |
sewardj | be91791 | 2010-08-22 12:38:53 +0000 | [diff] [blame] | 6075 | if (guest_arch == VexArchARM) { |
| 6076 | /* Translating Thumb2 code produces a lot of chaff. We have to |
| 6077 | work extra hard to get rid of it. */ |
| 6078 | bb = cprop_BB(bb); |
| 6079 | bb = spec_helpers_BB ( bb, specHelper ); |
philippe | 6c46bef | 2012-08-14 22:29:01 +0000 | [diff] [blame] | 6080 | if (vex_control.iropt_register_updates < VexRegUpdAllregsAtEachInsn) { |
philippe | c8e2f98 | 2012-08-01 22:04:13 +0000 | [diff] [blame] | 6081 | redundant_put_removal_BB ( bb, preciseMemExnsFn ); |
| 6082 | } |
sewardj | d5436ce | 2011-05-01 18:36:51 +0000 | [diff] [blame] | 6083 | do_cse_BB( bb ); |
sewardj | be91791 | 2010-08-22 12:38:53 +0000 | [diff] [blame] | 6084 | do_deadcode_BB( bb ); |
| 6085 | } |
| 6086 | |
sewardj | 0861374 | 2004-10-25 13:01:45 +0000 | [diff] [blame] | 6087 | if (vex_control.iropt_level > 1) { |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 6088 | |
| 6089 | /* Peer at what we have, to decide how much more effort to throw |
| 6090 | at it. */ |
| 6091 | considerExpensives( &hasGetIorPutI, &hasVorFtemps, bb ); |
| 6092 | |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 6093 | if (hasVorFtemps && !hasGetIorPutI) { |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 6094 | /* If any evidence of FP or Vector activity, CSE, as that |
| 6095 | tends to mop up all manner of lardy code to do with |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 6096 | rounding modes. Don't bother if hasGetIorPutI since that |
| 6097 | case leads into the expensive transformations, which do |
| 6098 | CSE anyway. */ |
| 6099 | (void)do_cse_BB( bb ); |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 6100 | do_deadcode_BB( bb ); |
| 6101 | } |
| 6102 | |
| 6103 | if (hasGetIorPutI) { |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 6104 | Bool cses; |
sewardj | 39555aa | 2004-10-24 22:29:19 +0000 | [diff] [blame] | 6105 | n_expensive++; |
sewardj | 39555aa | 2004-10-24 22:29:19 +0000 | [diff] [blame] | 6106 | if (DEBUG_IROPT) |
| 6107 | vex_printf("***** EXPENSIVE %d %d\n", n_total, n_expensive); |
sewardj | 695cff9 | 2004-10-13 14:50:14 +0000 | [diff] [blame] | 6108 | bb = expensive_transformations( bb ); |
sewardj | 8d2291c | 2004-10-25 14:50:21 +0000 | [diff] [blame] | 6109 | bb = cheap_transformations( bb, specHelper, preciseMemExnsFn ); |
sewardj | 9b0cc58 | 2006-02-04 15:24:00 +0000 | [diff] [blame] | 6110 | /* Potentially common up GetIs */ |
| 6111 | cses = do_cse_BB( bb ); |
| 6112 | if (cses) |
| 6113 | bb = cheap_transformations( bb, specHelper, preciseMemExnsFn ); |
sewardj | 695cff9 | 2004-10-13 14:50:14 +0000 | [diff] [blame] | 6114 | } |
sewardj | 39555aa | 2004-10-24 22:29:19 +0000 | [diff] [blame] | 6115 | |
| 6116 | /* Now have a go at unrolling simple (single-BB) loops. If |
| 6117 | successful, clean up the results as much as possible. */ |
| 6118 | |
| 6119 | bb2 = maybe_loop_unroll_BB( bb, guest_addr ); |
| 6120 | if (bb2) { |
sewardj | 8d2291c | 2004-10-25 14:50:21 +0000 | [diff] [blame] | 6121 | bb = cheap_transformations( bb2, specHelper, preciseMemExnsFn ); |
sewardj | b183b85 | 2006-02-03 16:08:03 +0000 | [diff] [blame] | 6122 | if (hasGetIorPutI) { |
sewardj | 39555aa | 2004-10-24 22:29:19 +0000 | [diff] [blame] | 6123 | bb = expensive_transformations( bb ); |
sewardj | 8d2291c | 2004-10-25 14:50:21 +0000 | [diff] [blame] | 6124 | bb = cheap_transformations( bb, specHelper, preciseMemExnsFn ); |
sewardj | 39555aa | 2004-10-24 22:29:19 +0000 | [diff] [blame] | 6125 | } else { |
| 6126 | /* at least do CSE and dead code removal */ |
sewardj | fe1ccfc | 2004-11-11 02:14:45 +0000 | [diff] [blame] | 6127 | do_cse_BB( bb ); |
sewardj | 49651f4 | 2004-10-28 22:11:04 +0000 | [diff] [blame] | 6128 | do_deadcode_BB( bb ); |
sewardj | 39555aa | 2004-10-24 22:29:19 +0000 | [diff] [blame] | 6129 | } |
| 6130 | if (0) vex_printf("vex iropt: unrolled a loop\n"); |
| 6131 | } |
| 6132 | |
sewardj | d721703 | 2004-08-19 10:49:10 +0000 | [diff] [blame] | 6133 | } |
| 6134 | |
sewardj | 4345f7a | 2004-09-22 19:49:27 +0000 | [diff] [blame] | 6135 | return bb; |
sewardj | edf4d69 | 2004-08-17 13:52:58 +0000 | [diff] [blame] | 6136 | } |
| 6137 | |
| 6138 | |
sewardj | a1a370f | 2004-08-17 13:31:55 +0000 | [diff] [blame] | 6139 | /*---------------------------------------------------------------*/ |
sewardj | cef7d3e | 2009-07-02 12:21:59 +0000 | [diff] [blame] | 6140 | /*--- end ir_opt.c ---*/ |
sewardj | a1a370f | 2004-08-17 13:31:55 +0000 | [diff] [blame] | 6141 | /*---------------------------------------------------------------*/ |