paul@154 | 1 | #!/usr/bin/env python |
paul@154 | 2 | |
paul@154 | 3 | """ |
paul@154 | 4 | Optimise code produced by the AST translator. |
paul@154 | 5 | |
paul@450 | 6 | Copyright (C) 2007, 2008, 2009, 2010, 2011 Paul Boddie <paul@boddie.org.uk> |
paul@154 | 7 | |
paul@154 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@154 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@154 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@154 | 11 | version. |
paul@154 | 12 | |
paul@154 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@154 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@154 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@154 | 16 | details. |
paul@154 | 17 | |
paul@154 | 18 | You should have received a copy of the GNU General Public License along with |
paul@154 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@154 | 20 | """ |
paul@154 | 21 | |
paul@154 | 22 | from micropython.common import * |
paul@155 | 23 | from micropython.data import * |
paul@154 | 24 | from micropython.rsvp import * |
paul@154 | 25 | |
paul@154 | 26 | class Optimiser: |
paul@154 | 27 | |
paul@154 | 28 | "A code optimiser." |
paul@154 | 29 | |
paul@154 | 30 | supported_optimisations = [ |
paul@167 | 31 | # Code generation optimisations: |
paul@155 | 32 | "constant_storage", "constant_accessor", "known_target", "self_access", |
paul@450 | 33 | "temp_storage", "no_operations", "unused_results", |
paul@268 | 34 | "unused_handlers", "accesses_by_usage", |
paul@167 | 35 | # Inspection optimisations: |
paul@167 | 36 | "unused_objects" |
paul@154 | 37 | ] |
paul@154 | 38 | |
paul@154 | 39 | def __init__(self, translation, optimisations=None): |
paul@154 | 40 | |
paul@154 | 41 | """ |
paul@154 | 42 | Provide for the given 'translation' an optimiser for the desired |
paul@154 | 43 | 'optimisations'. See the 'supported_optimisations' attribute of this |
paul@154 | 44 | class for permitted values. |
paul@154 | 45 | """ |
paul@154 | 46 | |
paul@154 | 47 | self.translation = translation |
paul@154 | 48 | self.optimisations = set(optimisations or []) |
paul@154 | 49 | |
paul@154 | 50 | # The current "active" instruction. |
paul@154 | 51 | # As a rule, this will become the last instruction, but some |
paul@154 | 52 | # control-flow operations will flush the "active" instruction. |
paul@154 | 53 | |
paul@154 | 54 | self.active = None |
paul@484 | 55 | |
paul@484 | 56 | # Information about instructions providing the active values for |
paul@484 | 57 | # registers. |
paul@154 | 58 | |
paul@484 | 59 | self.saved_value_op = {} |
paul@484 | 60 | self.saved_value_block = {} |
paul@484 | 61 | self.saved_value_pos = {} |
paul@154 | 62 | |
paul@484 | 63 | # Sets of instructions providing the active value for each register. |
paul@484 | 64 | |
paul@484 | 65 | self.active_values = {} |
paul@154 | 66 | |
paul@405 | 67 | def get_attribute_store_instructions(self): |
paul@405 | 68 | |
paul@405 | 69 | """ |
paul@405 | 70 | Return an appropriate set of instructions available when storing |
paul@405 | 71 | attributes. |
paul@405 | 72 | """ |
paul@405 | 73 | |
paul@405 | 74 | ca = self.should_optimise_accesses_by_attribute_usage() |
paul@405 | 75 | |
paul@405 | 76 | return ( |
paul@405 | 77 | ca and StoreAddress or None, # plain assignment |
paul@405 | 78 | ca and StoreAddressContext or None, # assignment via class |
paul@405 | 79 | ca and StoreAddressContext or None, # assignment via class (never via instance) |
paul@405 | 80 | StoreAttr, # assignment via instance |
paul@405 | 81 | StoreAttrIndex, # special assignment via instance |
paul@405 | 82 | None |
paul@405 | 83 | ) |
paul@405 | 84 | |
paul@452 | 85 | def reset(self, block=None, blocks=None): |
paul@452 | 86 | |
paul@452 | 87 | """ |
paul@452 | 88 | Reset the optimiser for the given 'block' (or the current block), |
paul@452 | 89 | clearing the active value instructions if no 'blocks' are given, or |
paul@452 | 90 | collecting the active instructions from each of the blocks otherwise. |
paul@452 | 91 | """ |
paul@452 | 92 | |
paul@484 | 93 | self.store_active_values() |
paul@452 | 94 | self.clear_active() |
paul@452 | 95 | |
paul@452 | 96 | # Make a new collection of instructions for a new block. |
paul@154 | 97 | |
paul@452 | 98 | if block: |
paul@484 | 99 | self.active_values = {} |
paul@452 | 100 | block.set_active_values(self.active_values) |
paul@452 | 101 | |
paul@452 | 102 | # Otherwise, clear the collection for an existing block. |
paul@154 | 103 | |
paul@452 | 104 | else: |
paul@452 | 105 | self.active_values.clear() |
paul@452 | 106 | |
paul@452 | 107 | if blocks: |
paul@452 | 108 | for b in blocks: |
paul@452 | 109 | self.active_values.update(b.get_active_values()) |
paul@154 | 110 | |
paul@484 | 111 | def set_new(self, register, op): |
paul@154 | 112 | |
paul@484 | 113 | "For 'register', set the latest 'op' as the active instruction." |
paul@154 | 114 | |
paul@154 | 115 | self.set_active(op) |
paul@484 | 116 | self.set_active_value(register, op) |
paul@484 | 117 | |
paul@484 | 118 | def set_active_value(self, register, op): |
paul@484 | 119 | |
paul@484 | 120 | "For 'register', set 'op' as the value-providing active instruction." |
paul@154 | 121 | |
paul@484 | 122 | # Since the current value provider may eventually be used, it needs to |
paul@484 | 123 | # store its output if appropriate. |
paul@154 | 124 | |
paul@484 | 125 | self.store_active_value(register) |
paul@484 | 126 | self.active_values[register] = set([op]) |
paul@154 | 127 | |
paul@484 | 128 | def get_active_value(self, register): |
paul@154 | 129 | |
paul@484 | 130 | """ |
paul@484 | 131 | Get any single active value for the given 'register' or None if none or |
paul@484 | 132 | more than one exist. |
paul@484 | 133 | """ |
paul@154 | 134 | |
paul@484 | 135 | if self.active_values.has_key(register) and \ |
paul@484 | 136 | len(self.active_values[register]) == 1: |
paul@154 | 137 | |
paul@484 | 138 | return list(self.active_values[register])[0] |
paul@484 | 139 | |
paul@452 | 140 | else: |
paul@452 | 141 | return None |
paul@154 | 142 | |
paul@484 | 143 | def remove_active_value(self, register): |
paul@154 | 144 | |
paul@156 | 145 | """ |
paul@484 | 146 | Remove the active instruction providing 'register' with its value from |
paul@484 | 147 | the generated code, if appropriate, and return the active instruction. |
paul@156 | 148 | """ |
paul@154 | 149 | |
paul@484 | 150 | if self.active_values.has_key(register) and \ |
paul@484 | 151 | self.active in self.active_values[register]: |
paul@484 | 152 | |
paul@468 | 153 | removed = self.translation.remove_op() |
paul@484 | 154 | self.active_values[register].remove(removed) |
paul@154 | 155 | return removed |
paul@484 | 156 | |
paul@154 | 157 | else: |
paul@154 | 158 | return None |
paul@154 | 159 | |
paul@484 | 160 | def set_target(self, register, target): |
paul@484 | 161 | |
paul@484 | 162 | """ |
paul@484 | 163 | Set the target of the active instruction involving 'register' to |
paul@484 | 164 | 'target'. |
paul@484 | 165 | """ |
paul@484 | 166 | |
paul@484 | 167 | if self.active_values.has_key(register): |
paul@484 | 168 | for expr in self.active_values[register]: |
paul@484 | 169 | expr.target = target |
paul@154 | 170 | |
paul@484 | 171 | # Transfer the active instructions to their new target. |
paul@154 | 172 | |
paul@484 | 173 | if not self.active_values.has_key(target): |
paul@484 | 174 | self.active_values[target] = self.active_values[register] |
paul@484 | 175 | else: |
paul@484 | 176 | self.active_values[target].update(self.active_values[register]) |
paul@484 | 177 | |
paul@484 | 178 | # Remove the association between the instructions and the specified |
paul@484 | 179 | # register. |
paul@484 | 180 | |
paul@484 | 181 | del self.active_values[register] |
paul@154 | 182 | |
paul@154 | 183 | def set_active(self, op): |
paul@154 | 184 | |
paul@154 | 185 | "Set the active instruction." |
paul@154 | 186 | |
paul@154 | 187 | self.active = op |
paul@154 | 188 | |
paul@154 | 189 | def clear_active(self): |
paul@154 | 190 | |
paul@450 | 191 | "Clear the active instructions." |
paul@154 | 192 | |
paul@154 | 193 | self.active = None |
paul@154 | 194 | |
paul@464 | 195 | # Permit the active value to be requested and restored. |
paul@464 | 196 | |
paul@484 | 197 | def request_active_value(self, register, temp, block, pos): |
paul@464 | 198 | |
paul@464 | 199 | """ |
paul@484 | 200 | Request the current active value for 'register' so that if the value is |
paul@484 | 201 | changed, a temporary storage element or equivalent will be allocated. |
paul@464 | 202 | """ |
paul@464 | 203 | |
paul@484 | 204 | # Cause any previously active value for the register to be saved. |
paul@484 | 205 | |
paul@484 | 206 | self.store_active_value(register) |
paul@484 | 207 | |
paul@484 | 208 | # Redefine the active value for the register. |
paul@484 | 209 | |
paul@484 | 210 | self.saved_value_op[register] = temp |
paul@484 | 211 | self.saved_value_block[register] = block |
paul@484 | 212 | self.saved_value_pos[register] = pos |
paul@464 | 213 | |
paul@484 | 214 | def store_active_values(self): |
paul@484 | 215 | |
paul@484 | 216 | "Store all active values." |
paul@464 | 217 | |
paul@484 | 218 | for register in self.active_values.keys(): |
paul@484 | 219 | self.store_active_value(register) |
paul@484 | 220 | |
paul@484 | 221 | def store_active_value(self, register): |
paul@484 | 222 | |
paul@484 | 223 | "Store the requested active value for 'register'." |
paul@464 | 224 | |
paul@484 | 225 | if self.saved_value_op.has_key(register): |
paul@484 | 226 | |
paul@484 | 227 | # Cause an instruction to be inserted to store the active value for |
paul@484 | 228 | # the register, thus keeping it available for any subsequent usage. |
paul@464 | 229 | |
paul@484 | 230 | self.translation.set_temp( |
paul@484 | 231 | self.saved_value_op[register], # cause the storage of the value |
paul@484 | 232 | self.saved_value_block[register], # in the appropriate block |
paul@484 | 233 | self.saved_value_pos[register] # at the appropriate location |
paul@484 | 234 | ) |
paul@464 | 235 | |
paul@484 | 236 | # Discard the existing active value information. |
paul@484 | 237 | |
paul@484 | 238 | self.ignore_active_value(register) |
paul@484 | 239 | |
paul@484 | 240 | def ignore_active_value(self, register): |
paul@464 | 241 | |
paul@484 | 242 | "Ignore the active value in 'register'." |
paul@484 | 243 | |
paul@484 | 244 | if self.saved_value_op.has_key(register): |
paul@484 | 245 | del self.saved_value_op[register] |
paul@484 | 246 | del self.saved_value_block[register] |
paul@484 | 247 | del self.saved_value_pos[register] |
paul@464 | 248 | |
paul@154 | 249 | # Optimisation tests. |
paul@154 | 250 | |
paul@154 | 251 | def should_optimise_constant_storage(self): |
paul@154 | 252 | return "constant_storage" in self.optimisations |
paul@154 | 253 | |
paul@155 | 254 | def should_optimise_constant_accessor(self): |
paul@155 | 255 | return "constant_accessor" in self.optimisations |
paul@155 | 256 | |
paul@154 | 257 | def should_optimise_known_target(self): |
paul@154 | 258 | return "known_target" in self.optimisations |
paul@154 | 259 | |
paul@154 | 260 | def should_optimise_self_access(self): |
paul@154 | 261 | return "self_access" in self.optimisations |
paul@154 | 262 | |
paul@154 | 263 | def should_optimise_temp_storage(self): |
paul@154 | 264 | return "temp_storage" in self.optimisations |
paul@154 | 265 | |
paul@154 | 266 | def should_optimise_away_no_operations(self): |
paul@154 | 267 | return "no_operations" in self.optimisations |
paul@154 | 268 | |
paul@154 | 269 | def should_optimise_unused_results(self): |
paul@154 | 270 | return "unused_results" in self.optimisations |
paul@154 | 271 | |
paul@232 | 272 | def should_optimise_unused_handlers(self): |
paul@232 | 273 | return "unused_handlers" in self.optimisations |
paul@232 | 274 | |
paul@268 | 275 | def should_optimise_accesses_by_attribute_usage(self): |
paul@268 | 276 | return "accesses_by_usage" in self.optimisations |
paul@268 | 277 | |
paul@154 | 278 | # Simple tests. |
paul@154 | 279 | |
paul@154 | 280 | def is_constant_input(self, instruction): |
paul@154 | 281 | |
paul@154 | 282 | "Return whether 'instruction' provides a constant input." |
paul@154 | 283 | |
paul@243 | 284 | return isinstance(instruction, LoadAddress) and instruction.attr.assignments == 1 and \ |
paul@243 | 285 | isinstance(instruction.attr.get_value(), Constant) or \ |
paul@237 | 286 | isinstance(instruction, (LoadConst, LoadClass, LoadFunction)) |
paul@154 | 287 | |
paul@154 | 288 | def is_constant_target(self, instruction): |
paul@154 | 289 | |
paul@154 | 290 | "Return whether 'instruction' provides a constant target." |
paul@154 | 291 | |
paul@202 | 292 | # NOTE: Removed StoreName, since this would then demand population of |
paul@202 | 293 | # NOTE: locals/frames. |
paul@202 | 294 | |
paul@223 | 295 | return isinstance(instruction, (StoreAddress, StoreAddressContext)) and \ |
paul@468 | 296 | instruction.attr.is_constant() and \ |
paul@468 | 297 | instruction.attr.is_strict_constant() |
paul@154 | 298 | |
paul@154 | 299 | def is_simple_input(self, instruction): |
paul@154 | 300 | |
paul@154 | 301 | """ |
paul@154 | 302 | Return whether 'instruction' provides a simple input (typically a load |
paul@154 | 303 | instruction). A simple input is something which would be represented by |
paul@154 | 304 | a load operation from a CPU register or special memory location. |
paul@154 | 305 | """ |
paul@154 | 306 | |
paul@430 | 307 | return isinstance(instruction, (LoadConst, LoadClass, LoadFunction, |
paul@450 | 308 | LoadName, LoadTemp, LoadAddress |
paul@430 | 309 | )) |
paul@154 | 310 | |
paul@464 | 311 | def is_resultant_no_operation(self, instruction, last_op=None): |
paul@154 | 312 | |
paul@154 | 313 | """ |
paul@154 | 314 | Return whether 'instruction' merely stores its input where the input |
paul@154 | 315 | originally came from. |
paul@154 | 316 | """ |
paul@154 | 317 | |
paul@464 | 318 | last_op = last_op or self.translation.last_op() |
paul@464 | 319 | return last_op and last_op.attr == instruction.attr and ( |
paul@464 | 320 | isinstance(instruction, StoreTemp) and isinstance(last_op, LoadTemp) or |
paul@468 | 321 | isinstance(instruction, StoreAddress) and isinstance(last_op, LoadAddress) or |
paul@468 | 322 | last_op.source == instruction.target and ( |
paul@468 | 323 | isinstance(instruction, LoadTemp) and isinstance(last_op, StoreTemp) or |
paul@468 | 324 | isinstance(instruction, LoadAddress) and isinstance(last_op, StoreAddress) |
paul@468 | 325 | )) |
paul@154 | 326 | |
paul@154 | 327 | # Convenience tests on outputs. |
paul@154 | 328 | |
paul@154 | 329 | def have_constant_target(self): |
paul@154 | 330 | |
paul@154 | 331 | "Return whether the active instruction provides a constant target." |
paul@154 | 332 | |
paul@154 | 333 | return self.is_constant_target(self.active) |
paul@154 | 334 | |
paul@450 | 335 | def have_constant_source(self, expr): |
paul@154 | 336 | |
paul@450 | 337 | "Return whether the active assignment source instruction is constant." |
paul@154 | 338 | |
paul@450 | 339 | return self.is_constant_input(expr) |
paul@154 | 340 | |
paul@154 | 341 | # Convenience tests on inputs. |
paul@154 | 342 | |
paul@154 | 343 | def have_constant_input(self): |
paul@154 | 344 | |
paul@154 | 345 | "Return whether the active instruction provides a constant input." |
paul@154 | 346 | |
paul@484 | 347 | return self.get_active_value("working") and self.is_constant_input(self.get_active_value("working")) |
paul@154 | 348 | |
paul@154 | 349 | def have_simple_input(self): |
paul@154 | 350 | |
paul@154 | 351 | "Return whether the active instruction provides a simple input." |
paul@154 | 352 | |
paul@484 | 353 | return self.get_active_value("working") and self.is_simple_input(self.get_active_value("working")) |
paul@154 | 354 | |
paul@453 | 355 | # Indicate whether the active instruction can be used in place of access |
paul@453 | 356 | # to a temporary variable retaining the result of the last instruction. |
paul@453 | 357 | |
paul@453 | 358 | have_temp_compatible_access = have_simple_input |
paul@453 | 359 | |
paul@158 | 360 | def have_self_input(self, unit): |
paul@154 | 361 | |
paul@158 | 362 | """ |
paul@158 | 363 | Return whether the active instruction is a reference to self in the |
paul@158 | 364 | given 'unit'. |
paul@158 | 365 | """ |
paul@154 | 366 | |
paul@452 | 367 | if not (isinstance(unit, Function) and unit.is_method()): |
paul@591 | 368 | return False |
paul@154 | 369 | |
paul@484 | 370 | expr = self.get_active_value("working") |
paul@452 | 371 | return expr and isinstance(expr, LoadName) and expr.attr.name == "self" |
paul@154 | 372 | |
paul@158 | 373 | def have_correct_self_for_target(self, context, unit): |
paul@154 | 374 | |
paul@158 | 375 | "Return whether the 'context' is compatible with the given 'unit'." |
paul@154 | 376 | |
paul@158 | 377 | if context is not None and self.have_self_input(unit): |
paul@154 | 378 | |
paul@158 | 379 | parent = unit.parent |
paul@154 | 380 | if parent is context or parent.has_subclass(context) or context.has_subclass(parent): |
paul@591 | 381 | return True |
paul@154 | 382 | |
paul@591 | 383 | return False |
paul@154 | 384 | |
paul@232 | 385 | def have_empty_handler(self, instruction): |
paul@232 | 386 | |
paul@232 | 387 | """ |
paul@232 | 388 | Return whether the active instruction defined a handler for exceptions |
paul@232 | 389 | which is then discarded by the given 'instruction'. |
paul@232 | 390 | """ |
paul@232 | 391 | |
paul@232 | 392 | return isinstance(self.translation.last_op(), PushHandler) and isinstance(instruction, PopHandler) |
paul@232 | 393 | |
paul@154 | 394 | # Optimisation methods. See the supported_optimisations class attribute. |
paul@154 | 395 | |
paul@468 | 396 | def optimise_constant_storage(self, expr): |
paul@154 | 397 | |
paul@154 | 398 | """ |
paul@154 | 399 | Where the last operation stores a constant into a target which is also |
paul@156 | 400 | constant, indicate that both operations should be optimised away. |
paul@154 | 401 | """ |
paul@154 | 402 | |
paul@156 | 403 | return self.should_optimise_constant_storage() and \ |
paul@154 | 404 | self.have_constant_target() and \ |
paul@450 | 405 | self.have_constant_source(expr) |
paul@154 | 406 | |
paul@155 | 407 | def optimise_constant_accessor(self): |
paul@155 | 408 | |
paul@155 | 409 | """ |
paul@155 | 410 | Where the object whose attribute is being accessed is constant, provide |
paul@401 | 411 | information about the object and its full name. |
paul@155 | 412 | """ |
paul@155 | 413 | |
paul@155 | 414 | if self.should_optimise_constant_accessor() and self.have_constant_input(): |
paul@484 | 415 | value = self.get_active_value("working") |
paul@155 | 416 | |
paul@155 | 417 | # Get the details of the access. |
paul@155 | 418 | |
paul@155 | 419 | if isinstance(value.attr, Const): |
paul@450 | 420 | return value.attr, value.attr.value_type_name() |
paul@155 | 421 | else: |
paul@192 | 422 | target = value.attr.get_value() |
paul@155 | 423 | |
paul@192 | 424 | if target is None: |
paul@192 | 425 | return None # no clearly defined target |
paul@192 | 426 | elif isinstance(target, Const): |
paul@401 | 427 | return target, target.value_type_name() |
paul@155 | 428 | elif isinstance(target, Instance): |
paul@155 | 429 | return None # skip production of optimised code |
paul@155 | 430 | else: |
paul@401 | 431 | return target, target.full_name() |
paul@155 | 432 | |
paul@155 | 433 | else: |
paul@155 | 434 | return None |
paul@155 | 435 | |
paul@154 | 436 | def optimise_known_target(self): |
paul@154 | 437 | |
paul@154 | 438 | """ |
paul@154 | 439 | Where the target of an invocation is known, provide information about it |
paul@154 | 440 | and its context. If a class is being invoked and the conditions are |
paul@154 | 441 | appropriate, get information about the specific initialiser. |
paul@154 | 442 | """ |
paul@154 | 443 | |
paul@452 | 444 | if self.should_optimise_known_target() and self.have_constant_input(): |
paul@484 | 445 | value = self.get_active_value("working") |
paul@192 | 446 | target = value.attr.get_value() |
paul@192 | 447 | context = value.attr.get_context() |
paul@192 | 448 | |
paul@192 | 449 | # Return target details only if this is clearly defined. |
paul@154 | 450 | |
paul@192 | 451 | if target is not None: |
paul@192 | 452 | return target, context |
paul@192 | 453 | |
paul@192 | 454 | return None |
paul@154 | 455 | |
paul@156 | 456 | def optimise_self_access(self, unit, attrname): |
paul@154 | 457 | |
paul@154 | 458 | """ |
paul@156 | 459 | Return whether code in the given 'unit' is able to access the given |
paul@156 | 460 | 'attrname' through the same position in all possible objects which might |
paul@156 | 461 | be accessed. |
paul@154 | 462 | """ |
paul@154 | 463 | |
paul@156 | 464 | return self.should_optimise_self_access() and \ |
paul@158 | 465 | self.have_self_input(unit) and not unit.is_relocated(attrname) |
paul@154 | 466 | |
paul@154 | 467 | def optimise_temp_storage(self): |
paul@154 | 468 | |
paul@154 | 469 | """ |
paul@154 | 470 | Where the next operation would involve storing a value into temporary |
paul@368 | 471 | storage, record and remove any simple instruction which produced the |
paul@368 | 472 | value to be stored such that instead of subsequently accessing the |
paul@368 | 473 | temporary storage, that instruction is substituted. |
paul@154 | 474 | |
paul@464 | 475 | If no optimisation can be achieved, temporary storage is requested |
paul@154 | 476 | and the appropriate LoadTemp instruction is returned. |
paul@154 | 477 | |
paul@154 | 478 | Restriction: for use only in situations where the source of the |
paul@154 | 479 | temporary data will not be disturbed between its first access and its |
paul@154 | 480 | subsequent use. |
paul@154 | 481 | """ |
paul@154 | 482 | |
paul@465 | 483 | if self.should_optimise_temp_storage(): |
paul@465 | 484 | |
paul@465 | 485 | # Emitted instructions can be obtained. |
paul@465 | 486 | |
paul@465 | 487 | if self.have_temp_compatible_access(): |
paul@154 | 488 | |
paul@465 | 489 | # Remove the active value contributor if possible. |
paul@154 | 490 | |
paul@484 | 491 | removed = self.remove_active_value("working") |
paul@465 | 492 | if removed is not None: |
paul@465 | 493 | |
paul@465 | 494 | # Extend the lifetime of any temporary storage location. |
paul@154 | 495 | |
paul@465 | 496 | self.translation.ensure_temp(removed) |
paul@465 | 497 | return removed |
paul@154 | 498 | |
paul@465 | 499 | # Otherwise, just leave it in place, but return the instruction. |
paul@154 | 500 | |
paul@465 | 501 | else: |
paul@484 | 502 | return self.get_active_value("working") |
paul@464 | 503 | |
paul@465 | 504 | # Or provisional temporary instructions. |
paul@465 | 505 | |
paul@484 | 506 | elif self.saved_value_op.has_key("working"): |
paul@484 | 507 | return self.saved_value_op["working"] |
paul@464 | 508 | |
paul@450 | 509 | return self.translation.get_temp() |
paul@154 | 510 | |
paul@464 | 511 | def optimise_away_no_operations(self, instruction, last_op=None): |
paul@154 | 512 | |
paul@154 | 513 | """ |
paul@154 | 514 | Optimise away operations which just store their inputs in the place |
paul@154 | 515 | the inputs originally came from. |
paul@154 | 516 | """ |
paul@154 | 517 | |
paul@156 | 518 | return self.should_optimise_away_no_operations() and \ |
paul@464 | 519 | self.is_resultant_no_operation(instruction, last_op) |
paul@154 | 520 | |
paul@154 | 521 | def optimise_unused_results(self): |
paul@154 | 522 | |
paul@154 | 523 | "Discard results which will not be used." |
paul@154 | 524 | |
paul@154 | 525 | if self.should_optimise_unused_results() and self.have_simple_input(): |
paul@484 | 526 | self.remove_active_value("working") |
paul@154 | 527 | |
paul@232 | 528 | def optimise_unused_handlers(self, instruction): |
paul@232 | 529 | |
paul@232 | 530 | "Discard handlers which will not be used." |
paul@232 | 531 | |
paul@232 | 532 | if self.should_optimise_unused_handlers() and self.have_empty_handler(instruction): |
paul@232 | 533 | self.translation.remove_op() |
paul@591 | 534 | return True |
paul@232 | 535 | else: |
paul@591 | 536 | return False |
paul@232 | 537 | |
paul@154 | 538 | # vim: tabstop=4 expandtab shiftwidth=4 |