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. |
| 31 | for (HInstructionIterator it(block->GetInstructions()); |
| 32 | !it.Done(); it.Advance()) { |
| 33 | HInstruction* inst = it.Current(); |
| 34 | // Constant folding: replace `c <- a op b' with a compile-time |
| 35 | // evaluation of `a op b' if `a' and `b' are constant. |
| 36 | if (inst->IsBinaryOperation()) { |
| 37 | HConstant* constant = |
| 38 | inst->AsBinaryOperation()->TryStaticEvaluation(graph_->GetArena()); |
| 39 | if (constant != nullptr) { |
| 40 | inst->GetBlock()->ReplaceAndRemoveInstructionWith(inst, constant); |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | } // namespace art |