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 | |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 21 | // This visitor tries to simplify operations that yield a constant. For example |
| 22 | // `input * 0` is replaced by a null constant. |
| 23 | class InstructionWithAbsorbingInputSimplifier : public HGraphVisitor { |
| 24 | public: |
| 25 | explicit InstructionWithAbsorbingInputSimplifier(HGraph* graph) : HGraphVisitor(graph) {} |
| 26 | |
| 27 | private: |
| 28 | void VisitShift(HBinaryOperation* shift); |
| 29 | |
Aart Bik | 96709f1 | 2015-10-28 17:49:07 -0700 | [diff] [blame] | 30 | void VisitAbove(HAbove* instruction) OVERRIDE; |
| 31 | void VisitAboveOrEqual(HAboveOrEqual* instruction) OVERRIDE; |
| 32 | void VisitBelow(HBelow* instruction) OVERRIDE; |
| 33 | void VisitBelowOrEqual(HBelowOrEqual* instruction) OVERRIDE; |
| 34 | |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 35 | void VisitAnd(HAnd* instruction) OVERRIDE; |
Roland Levillain | 3b55ebb | 2015-05-08 13:13:19 +0100 | [diff] [blame] | 36 | void VisitCompare(HCompare* instruction) OVERRIDE; |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 37 | void VisitMul(HMul* instruction) OVERRIDE; |
| 38 | void VisitOr(HOr* instruction) OVERRIDE; |
| 39 | void VisitRem(HRem* instruction) OVERRIDE; |
| 40 | void VisitShl(HShl* instruction) OVERRIDE; |
| 41 | void VisitShr(HShr* instruction) OVERRIDE; |
| 42 | void VisitSub(HSub* instruction) OVERRIDE; |
| 43 | void VisitUShr(HUShr* instruction) OVERRIDE; |
| 44 | void VisitXor(HXor* instruction) OVERRIDE; |
| 45 | }; |
| 46 | |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 47 | void HConstantFolding::Run() { |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 48 | InstructionWithAbsorbingInputSimplifier simplifier(graph_); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 49 | // Process basic blocks in reverse post-order in the dominator tree, |
| 50 | // so that an instruction turned into a constant, used as input of |
| 51 | // another instruction, may possibly be used to turn that second |
| 52 | // instruction into a constant as well. |
| 53 | for (HReversePostOrderIterator it(*graph_); !it.Done(); it.Advance()) { |
| 54 | HBasicBlock* block = it.Current(); |
| 55 | // Traverse this block's instructions in (forward) order and |
| 56 | // replace the ones that can be statically evaluated by a |
| 57 | // compile-time counterpart. |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 58 | for (HInstructionIterator inst_it(block->GetInstructions()); |
| 59 | !inst_it.Done(); inst_it.Advance()) { |
| 60 | HInstruction* inst = inst_it.Current(); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 61 | if (inst->IsBinaryOperation()) { |
Roland Levillain | 9240d6a | 2014-10-20 16:47:04 +0100 | [diff] [blame] | 62 | // Constant folding: replace `op(a, b)' with a constant at |
| 63 | // compile time if `a' and `b' are both constants. |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 64 | HConstant* constant = inst->AsBinaryOperation()->TryStaticEvaluation(); |
Roland Levillain | 9240d6a | 2014-10-20 16:47:04 +0100 | [diff] [blame] | 65 | if (constant != nullptr) { |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 66 | inst->ReplaceWith(constant); |
| 67 | inst->GetBlock()->RemoveInstruction(inst); |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 68 | } else { |
| 69 | inst->Accept(&simplifier); |
Roland Levillain | 9240d6a | 2014-10-20 16:47:04 +0100 | [diff] [blame] | 70 | } |
| 71 | } else if (inst->IsUnaryOperation()) { |
| 72 | // Constant folding: replace `op(a)' with a constant at compile |
| 73 | // time if `a' is a constant. |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 74 | HConstant* constant = inst->AsUnaryOperation()->TryStaticEvaluation(); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 75 | if (constant != nullptr) { |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 76 | inst->ReplaceWith(constant); |
| 77 | inst->GetBlock()->RemoveInstruction(inst); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 78 | } |
Mark Mendell | e82549b | 2015-05-06 10:55:34 -0400 | [diff] [blame] | 79 | } else if (inst->IsTypeConversion()) { |
| 80 | // Constant folding: replace `TypeConversion(a)' with a constant at |
| 81 | // compile time if `a' is a constant. |
| 82 | HConstant* constant = inst->AsTypeConversion()->TryStaticEvaluation(); |
| 83 | if (constant != nullptr) { |
| 84 | inst->ReplaceWith(constant); |
| 85 | inst->GetBlock()->RemoveInstruction(inst); |
| 86 | } |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 87 | } else if (inst->IsDivZeroCheck()) { |
| 88 | // We can safely remove the check if the input is a non-null constant. |
| 89 | HDivZeroCheck* check = inst->AsDivZeroCheck(); |
| 90 | HInstruction* check_input = check->InputAt(0); |
| 91 | if (check_input->IsConstant() && !check_input->AsConstant()->IsZero()) { |
| 92 | check->ReplaceWith(check_input); |
| 93 | check->GetBlock()->RemoveInstruction(check); |
| 94 | } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 100 | void InstructionWithAbsorbingInputSimplifier::VisitShift(HBinaryOperation* instruction) { |
| 101 | DCHECK(instruction->IsShl() || instruction->IsShr() || instruction->IsUShr()); |
| 102 | HInstruction* left = instruction->GetLeft(); |
| 103 | if (left->IsConstant() && left->AsConstant()->IsZero()) { |
| 104 | // Replace code looking like |
| 105 | // SHL dst, 0, shift_amount |
| 106 | // with |
| 107 | // CONSTANT 0 |
| 108 | instruction->ReplaceWith(left); |
| 109 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 110 | } |
| 111 | } |
| 112 | |
Aart Bik | 96709f1 | 2015-10-28 17:49:07 -0700 | [diff] [blame] | 113 | void InstructionWithAbsorbingInputSimplifier::VisitAbove(HAbove* instruction) { |
| 114 | if (instruction->GetLeft()->IsConstant() && |
| 115 | instruction->GetLeft()->AsConstant()->IsZero()) { |
| 116 | // Replace code looking like |
| 117 | // ABOVE dst, 0, src // unsigned 0 > src is always false |
| 118 | // with |
| 119 | // CONSTANT false |
| 120 | instruction->ReplaceWith(GetGraph()->GetConstant(Primitive::kPrimBoolean, 0)); |
| 121 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | void InstructionWithAbsorbingInputSimplifier::VisitAboveOrEqual(HAboveOrEqual* instruction) { |
| 126 | if (instruction->GetRight()->IsConstant() && |
| 127 | instruction->GetRight()->AsConstant()->IsZero()) { |
| 128 | // Replace code looking like |
| 129 | // ABOVE_OR_EQUAL dst, src, 0 // unsigned src >= 0 is always true |
| 130 | // with |
| 131 | // CONSTANT true |
| 132 | instruction->ReplaceWith(GetGraph()->GetConstant(Primitive::kPrimBoolean, 1)); |
| 133 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | void InstructionWithAbsorbingInputSimplifier::VisitBelow(HBelow* instruction) { |
| 138 | if (instruction->GetRight()->IsConstant() && |
| 139 | instruction->GetRight()->AsConstant()->IsZero()) { |
| 140 | // Replace code looking like |
| 141 | // BELOW dst, src, 0 // unsigned src < 0 is always false |
| 142 | // with |
| 143 | // CONSTANT false |
| 144 | instruction->ReplaceWith(GetGraph()->GetConstant(Primitive::kPrimBoolean, 0)); |
| 145 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | void InstructionWithAbsorbingInputSimplifier::VisitBelowOrEqual(HBelowOrEqual* instruction) { |
| 150 | if (instruction->GetLeft()->IsConstant() && |
| 151 | instruction->GetLeft()->AsConstant()->IsZero()) { |
| 152 | // Replace code looking like |
| 153 | // BELOW_OR_EQUAL dst, 0, src // unsigned 0 <= src is always true |
| 154 | // with |
| 155 | // CONSTANT true |
| 156 | instruction->ReplaceWith(GetGraph()->GetConstant(Primitive::kPrimBoolean, 1)); |
| 157 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 158 | } |
| 159 | } |
| 160 | |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 161 | void InstructionWithAbsorbingInputSimplifier::VisitAnd(HAnd* instruction) { |
| 162 | HConstant* input_cst = instruction->GetConstantRight(); |
| 163 | if ((input_cst != nullptr) && input_cst->IsZero()) { |
| 164 | // Replace code looking like |
| 165 | // AND dst, src, 0 |
| 166 | // with |
| 167 | // CONSTANT 0 |
| 168 | instruction->ReplaceWith(input_cst); |
| 169 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 170 | } |
| 171 | } |
| 172 | |
Roland Levillain | 3b55ebb | 2015-05-08 13:13:19 +0100 | [diff] [blame] | 173 | void InstructionWithAbsorbingInputSimplifier::VisitCompare(HCompare* instruction) { |
| 174 | HConstant* input_cst = instruction->GetConstantRight(); |
| 175 | if (input_cst != nullptr) { |
| 176 | HInstruction* input_value = instruction->GetLeastConstantLeft(); |
| 177 | if (Primitive::IsFloatingPointType(input_value->GetType()) && |
| 178 | ((input_cst->IsFloatConstant() && input_cst->AsFloatConstant()->IsNaN()) || |
| 179 | (input_cst->IsDoubleConstant() && input_cst->AsDoubleConstant()->IsNaN()))) { |
| 180 | // Replace code looking like |
| 181 | // CMP{G,L} dst, src, NaN |
| 182 | // with |
| 183 | // CONSTANT +1 (gt bias) |
| 184 | // or |
| 185 | // CONSTANT -1 (lt bias) |
| 186 | instruction->ReplaceWith(GetGraph()->GetConstant(Primitive::kPrimInt, |
| 187 | (instruction->IsGtBias() ? 1 : -1))); |
| 188 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 193 | void InstructionWithAbsorbingInputSimplifier::VisitMul(HMul* instruction) { |
| 194 | HConstant* input_cst = instruction->GetConstantRight(); |
| 195 | Primitive::Type type = instruction->GetType(); |
| 196 | if (Primitive::IsIntOrLongType(type) && |
| 197 | (input_cst != nullptr) && input_cst->IsZero()) { |
| 198 | // Replace code looking like |
| 199 | // MUL dst, src, 0 |
| 200 | // with |
| 201 | // CONSTANT 0 |
| 202 | // Integral multiplication by zero always yields zero, but floating-point |
| 203 | // multiplication by zero does not always do. For example `Infinity * 0.0` |
| 204 | // should yield a NaN. |
| 205 | instruction->ReplaceWith(input_cst); |
| 206 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | void InstructionWithAbsorbingInputSimplifier::VisitOr(HOr* instruction) { |
| 211 | HConstant* input_cst = instruction->GetConstantRight(); |
| 212 | |
| 213 | if (input_cst == nullptr) { |
| 214 | return; |
| 215 | } |
| 216 | |
| 217 | if (Int64FromConstant(input_cst) == -1) { |
| 218 | // Replace code looking like |
| 219 | // OR dst, src, 0xFFF...FF |
| 220 | // with |
| 221 | // CONSTANT 0xFFF...FF |
| 222 | instruction->ReplaceWith(input_cst); |
| 223 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | void InstructionWithAbsorbingInputSimplifier::VisitRem(HRem* instruction) { |
| 228 | Primitive::Type type = instruction->GetType(); |
| 229 | |
| 230 | if (!Primitive::IsIntegralType(type)) { |
| 231 | return; |
| 232 | } |
| 233 | |
| 234 | HBasicBlock* block = instruction->GetBlock(); |
| 235 | |
| 236 | if (instruction->GetLeft()->IsConstant() && |
| 237 | instruction->GetLeft()->AsConstant()->IsZero()) { |
| 238 | // Replace code looking like |
| 239 | // REM dst, 0, src |
| 240 | // with |
| 241 | // CONSTANT 0 |
| 242 | instruction->ReplaceWith(instruction->GetLeft()); |
| 243 | block->RemoveInstruction(instruction); |
| 244 | } |
| 245 | |
| 246 | HConstant* cst_right = instruction->GetRight()->AsConstant(); |
| 247 | if (((cst_right != nullptr) && |
| 248 | (cst_right->IsOne() || cst_right->IsMinusOne())) || |
| 249 | (instruction->GetLeft() == instruction->GetRight())) { |
| 250 | // Replace code looking like |
| 251 | // REM dst, src, 1 |
| 252 | // or |
| 253 | // REM dst, src, -1 |
| 254 | // or |
| 255 | // REM dst, src, src |
| 256 | // with |
| 257 | // CONSTANT 0 |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 258 | instruction->ReplaceWith(GetGraph()->GetConstant(type, 0)); |
| 259 | block->RemoveInstruction(instruction); |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 260 | } |
| 261 | } |
| 262 | |
| 263 | void InstructionWithAbsorbingInputSimplifier::VisitShl(HShl* instruction) { |
| 264 | VisitShift(instruction); |
| 265 | } |
| 266 | |
| 267 | void InstructionWithAbsorbingInputSimplifier::VisitShr(HShr* instruction) { |
| 268 | VisitShift(instruction); |
| 269 | } |
| 270 | |
| 271 | void InstructionWithAbsorbingInputSimplifier::VisitSub(HSub* instruction) { |
| 272 | Primitive::Type type = instruction->GetType(); |
| 273 | |
| 274 | if (!Primitive::IsIntegralType(type)) { |
| 275 | return; |
| 276 | } |
| 277 | |
| 278 | HBasicBlock* block = instruction->GetBlock(); |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 279 | |
| 280 | // We assume that GVN has run before, so we only perform a pointer |
| 281 | // comparison. If for some reason the values are equal but the pointers are |
Kenny Root | 00d597a | 2015-09-30 13:09:51 -0700 | [diff] [blame] | 282 | // different, we are still correct and only miss an optimization |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 283 | // opportunity. |
| 284 | if (instruction->GetLeft() == instruction->GetRight()) { |
| 285 | // Replace code looking like |
| 286 | // SUB dst, src, src |
| 287 | // with |
| 288 | // CONSTANT 0 |
Kenny Root | 00d597a | 2015-09-30 13:09:51 -0700 | [diff] [blame] | 289 | // Note that we cannot optimize `x - x` to `0` for floating-point. It does |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 290 | // not work when `x` is an infinity. |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 291 | instruction->ReplaceWith(GetGraph()->GetConstant(type, 0)); |
| 292 | block->RemoveInstruction(instruction); |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | |
| 296 | void InstructionWithAbsorbingInputSimplifier::VisitUShr(HUShr* instruction) { |
| 297 | VisitShift(instruction); |
| 298 | } |
| 299 | |
| 300 | void InstructionWithAbsorbingInputSimplifier::VisitXor(HXor* instruction) { |
| 301 | if (instruction->GetLeft() == instruction->GetRight()) { |
| 302 | // Replace code looking like |
| 303 | // XOR dst, src, src |
| 304 | // with |
| 305 | // CONSTANT 0 |
| 306 | Primitive::Type type = instruction->GetType(); |
| 307 | HBasicBlock* block = instruction->GetBlock(); |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 308 | instruction->ReplaceWith(GetGraph()->GetConstant(type, 0)); |
| 309 | block->RemoveInstruction(instruction); |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 313 | } // namespace art |