Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 17 | #include "constant_folding.h" |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 18 | |
| 19 | namespace art { |
| 20 | |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 21 | void HConstantFolding::Run() { |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 22 | // Process basic blocks in reverse post-order in the dominator tree, |
| 23 | // so that an instruction turned into a constant, used as input of |
| 24 | // another instruction, may possibly be used to turn that second |
| 25 | // instruction into a constant as well. |
| 26 | for (HReversePostOrderIterator it(*graph_); !it.Done(); it.Advance()) { |
| 27 | HBasicBlock* block = it.Current(); |
| 28 | // Traverse this block's instructions in (forward) order and |
| 29 | // replace the ones that can be statically evaluated by a |
| 30 | // compile-time counterpart. |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame^] | 31 | for (HInstructionIterator inst_it(block->GetInstructions()); |
| 32 | !inst_it.Done(); inst_it.Advance()) { |
| 33 | HInstruction* inst = inst_it.Current(); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 34 | if (inst->IsBinaryOperation()) { |
Roland Levillain | 9240d6a | 2014-10-20 16:47:04 +0100 | [diff] [blame] | 35 | // Constant folding: replace `op(a, b)' with a constant at |
| 36 | // compile time if `a' and `b' are both constants. |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 37 | HConstant* constant = |
Roland Levillain | 9240d6a | 2014-10-20 16:47:04 +0100 | [diff] [blame] | 38 | inst->AsBinaryOperation()->TryStaticEvaluation(); |
| 39 | if (constant != nullptr) { |
| 40 | inst->GetBlock()->ReplaceAndRemoveInstructionWith(inst, constant); |
| 41 | } |
| 42 | } else if (inst->IsUnaryOperation()) { |
| 43 | // Constant folding: replace `op(a)' with a constant at compile |
| 44 | // time if `a' is a constant. |
| 45 | HConstant* constant = |
| 46 | inst->AsUnaryOperation()->TryStaticEvaluation(); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 47 | if (constant != nullptr) { |
| 48 | inst->GetBlock()->ReplaceAndRemoveInstructionWith(inst, constant); |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | } // namespace art |