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 | |
Vladimir Marko | 0a51605 | 2019-10-14 13:00:44 +0000 | [diff] [blame] | 19 | namespace art { |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 20 | |
Roland Levillain | 1252e97 | 2015-08-06 15:46:02 +0100 | [diff] [blame] | 21 | // This visitor tries to simplify instructions that can be evaluated |
| 22 | // as constants. |
| 23 | class HConstantFoldingVisitor : public HGraphDelegateVisitor { |
| 24 | public: |
| 25 | explicit HConstantFoldingVisitor(HGraph* graph) |
| 26 | : HGraphDelegateVisitor(graph) {} |
| 27 | |
| 28 | private: |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 29 | void VisitBasicBlock(HBasicBlock* block) override; |
Roland Levillain | 1252e97 | 2015-08-06 15:46:02 +0100 | [diff] [blame] | 30 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 31 | void VisitUnaryOperation(HUnaryOperation* inst) override; |
| 32 | void VisitBinaryOperation(HBinaryOperation* inst) override; |
Roland Levillain | 1252e97 | 2015-08-06 15:46:02 +0100 | [diff] [blame] | 33 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 34 | void VisitTypeConversion(HTypeConversion* inst) override; |
| 35 | void VisitDivZeroCheck(HDivZeroCheck* inst) override; |
Roland Levillain | 1252e97 | 2015-08-06 15:46:02 +0100 | [diff] [blame] | 36 | |
| 37 | DISALLOW_COPY_AND_ASSIGN(HConstantFoldingVisitor); |
| 38 | }; |
| 39 | |
| 40 | // This visitor tries to simplify operations with an absorbing input, |
| 41 | // yielding a constant. For example `input * 0` is replaced by a |
| 42 | // null constant. |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 43 | class InstructionWithAbsorbingInputSimplifier : public HGraphVisitor { |
| 44 | public: |
| 45 | explicit InstructionWithAbsorbingInputSimplifier(HGraph* graph) : HGraphVisitor(graph) {} |
| 46 | |
| 47 | private: |
| 48 | void VisitShift(HBinaryOperation* shift); |
| 49 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 50 | void VisitEqual(HEqual* instruction) override; |
| 51 | void VisitNotEqual(HNotEqual* instruction) override; |
Vladimir Marko | a341f35 | 2016-08-31 12:18:20 +0100 | [diff] [blame] | 52 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 53 | void VisitAbove(HAbove* instruction) override; |
| 54 | void VisitAboveOrEqual(HAboveOrEqual* instruction) override; |
| 55 | void VisitBelow(HBelow* instruction) override; |
| 56 | void VisitBelowOrEqual(HBelowOrEqual* instruction) override; |
Aart Bik | 96709f1 | 2015-10-28 17:49:07 -0700 | [diff] [blame] | 57 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 58 | void VisitAnd(HAnd* instruction) override; |
| 59 | void VisitCompare(HCompare* instruction) override; |
| 60 | void VisitMul(HMul* instruction) override; |
| 61 | void VisitOr(HOr* instruction) override; |
| 62 | void VisitRem(HRem* instruction) override; |
| 63 | void VisitShl(HShl* instruction) override; |
| 64 | void VisitShr(HShr* instruction) override; |
| 65 | void VisitSub(HSub* instruction) override; |
| 66 | void VisitUShr(HUShr* instruction) override; |
| 67 | void VisitXor(HXor* instruction) override; |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 68 | }; |
| 69 | |
Roland Levillain | 1252e97 | 2015-08-06 15:46:02 +0100 | [diff] [blame] | 70 | |
Aart Bik | 2477320 | 2018-04-26 10:28:51 -0700 | [diff] [blame] | 71 | bool HConstantFolding::Run() { |
Roland Levillain | 1252e97 | 2015-08-06 15:46:02 +0100 | [diff] [blame] | 72 | HConstantFoldingVisitor visitor(graph_); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 73 | // Process basic blocks in reverse post-order in the dominator tree, |
| 74 | // so that an instruction turned into a constant, used as input of |
| 75 | // another instruction, may possibly be used to turn that second |
| 76 | // instruction into a constant as well. |
Roland Levillain | 1252e97 | 2015-08-06 15:46:02 +0100 | [diff] [blame] | 77 | visitor.VisitReversePostOrder(); |
Aart Bik | 2477320 | 2018-04-26 10:28:51 -0700 | [diff] [blame] | 78 | return true; |
Roland Levillain | 1252e97 | 2015-08-06 15:46:02 +0100 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | |
| 82 | void HConstantFoldingVisitor::VisitBasicBlock(HBasicBlock* block) { |
| 83 | // Traverse this block's instructions (phis don't need to be |
| 84 | // processed) in (forward) order and replace the ones that can be |
| 85 | // statically evaluated by a compile-time counterpart. |
| 86 | for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) { |
| 87 | it.Current()->Accept(this); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
Roland Levillain | 1252e97 | 2015-08-06 15:46:02 +0100 | [diff] [blame] | 91 | void HConstantFoldingVisitor::VisitUnaryOperation(HUnaryOperation* inst) { |
| 92 | // Constant folding: replace `op(a)' with a constant at compile |
| 93 | // time if `a' is a constant. |
| 94 | HConstant* constant = inst->TryStaticEvaluation(); |
| 95 | if (constant != nullptr) { |
| 96 | inst->ReplaceWith(constant); |
| 97 | inst->GetBlock()->RemoveInstruction(inst); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | void HConstantFoldingVisitor::VisitBinaryOperation(HBinaryOperation* inst) { |
| 102 | // Constant folding: replace `op(a, b)' with a constant at |
| 103 | // compile time if `a' and `b' are both constants. |
| 104 | HConstant* constant = inst->TryStaticEvaluation(); |
| 105 | if (constant != nullptr) { |
| 106 | inst->ReplaceWith(constant); |
| 107 | inst->GetBlock()->RemoveInstruction(inst); |
| 108 | } else { |
| 109 | InstructionWithAbsorbingInputSimplifier simplifier(GetGraph()); |
| 110 | inst->Accept(&simplifier); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | void HConstantFoldingVisitor::VisitTypeConversion(HTypeConversion* inst) { |
| 115 | // Constant folding: replace `TypeConversion(a)' with a constant at |
| 116 | // compile time if `a' is a constant. |
Mingyao Yang | 75bb2f3 | 2017-11-30 14:45:44 -0800 | [diff] [blame] | 117 | HConstant* constant = inst->TryStaticEvaluation(); |
Roland Levillain | 1252e97 | 2015-08-06 15:46:02 +0100 | [diff] [blame] | 118 | if (constant != nullptr) { |
| 119 | inst->ReplaceWith(constant); |
| 120 | inst->GetBlock()->RemoveInstruction(inst); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | void HConstantFoldingVisitor::VisitDivZeroCheck(HDivZeroCheck* inst) { |
| 125 | // We can safely remove the check if the input is a non-null constant. |
| 126 | HInstruction* check_input = inst->InputAt(0); |
Roland Levillain | 1a65388 | 2016-03-18 18:05:57 +0000 | [diff] [blame] | 127 | if (check_input->IsConstant() && !check_input->AsConstant()->IsArithmeticZero()) { |
Roland Levillain | 1252e97 | 2015-08-06 15:46:02 +0100 | [diff] [blame] | 128 | inst->ReplaceWith(check_input); |
| 129 | inst->GetBlock()->RemoveInstruction(inst); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 134 | void InstructionWithAbsorbingInputSimplifier::VisitShift(HBinaryOperation* instruction) { |
| 135 | DCHECK(instruction->IsShl() || instruction->IsShr() || instruction->IsUShr()); |
| 136 | HInstruction* left = instruction->GetLeft(); |
Roland Levillain | 1a65388 | 2016-03-18 18:05:57 +0000 | [diff] [blame] | 137 | if (left->IsConstant() && left->AsConstant()->IsArithmeticZero()) { |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 138 | // Replace code looking like |
| 139 | // SHL dst, 0, shift_amount |
| 140 | // with |
| 141 | // CONSTANT 0 |
| 142 | instruction->ReplaceWith(left); |
| 143 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 144 | } |
| 145 | } |
| 146 | |
Vladimir Marko | a341f35 | 2016-08-31 12:18:20 +0100 | [diff] [blame] | 147 | void InstructionWithAbsorbingInputSimplifier::VisitEqual(HEqual* instruction) { |
| 148 | if ((instruction->GetLeft()->IsNullConstant() && !instruction->GetRight()->CanBeNull()) || |
| 149 | (instruction->GetRight()->IsNullConstant() && !instruction->GetLeft()->CanBeNull())) { |
| 150 | // Replace code looking like |
| 151 | // EQUAL lhs, null |
| 152 | // where lhs cannot be null with |
| 153 | // CONSTANT false |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 154 | instruction->ReplaceWith(GetGraph()->GetConstant(DataType::Type::kBool, 0)); |
Vladimir Marko | a341f35 | 2016-08-31 12:18:20 +0100 | [diff] [blame] | 155 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | void InstructionWithAbsorbingInputSimplifier::VisitNotEqual(HNotEqual* instruction) { |
| 160 | if ((instruction->GetLeft()->IsNullConstant() && !instruction->GetRight()->CanBeNull()) || |
| 161 | (instruction->GetRight()->IsNullConstant() && !instruction->GetLeft()->CanBeNull())) { |
| 162 | // Replace code looking like |
| 163 | // NOT_EQUAL lhs, null |
| 164 | // where lhs cannot be null with |
| 165 | // CONSTANT true |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 166 | instruction->ReplaceWith(GetGraph()->GetConstant(DataType::Type::kBool, 1)); |
Vladimir Marko | a341f35 | 2016-08-31 12:18:20 +0100 | [diff] [blame] | 167 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 168 | } |
| 169 | } |
| 170 | |
Aart Bik | 96709f1 | 2015-10-28 17:49:07 -0700 | [diff] [blame] | 171 | void InstructionWithAbsorbingInputSimplifier::VisitAbove(HAbove* instruction) { |
| 172 | if (instruction->GetLeft()->IsConstant() && |
Roland Levillain | 1a65388 | 2016-03-18 18:05:57 +0000 | [diff] [blame] | 173 | instruction->GetLeft()->AsConstant()->IsArithmeticZero()) { |
Aart Bik | 96709f1 | 2015-10-28 17:49:07 -0700 | [diff] [blame] | 174 | // Replace code looking like |
| 175 | // ABOVE dst, 0, src // unsigned 0 > src is always false |
| 176 | // with |
| 177 | // CONSTANT false |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 178 | instruction->ReplaceWith(GetGraph()->GetConstant(DataType::Type::kBool, 0)); |
Aart Bik | 96709f1 | 2015-10-28 17:49:07 -0700 | [diff] [blame] | 179 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | void InstructionWithAbsorbingInputSimplifier::VisitAboveOrEqual(HAboveOrEqual* instruction) { |
| 184 | if (instruction->GetRight()->IsConstant() && |
Roland Levillain | 1a65388 | 2016-03-18 18:05:57 +0000 | [diff] [blame] | 185 | instruction->GetRight()->AsConstant()->IsArithmeticZero()) { |
Aart Bik | 96709f1 | 2015-10-28 17:49:07 -0700 | [diff] [blame] | 186 | // Replace code looking like |
| 187 | // ABOVE_OR_EQUAL dst, src, 0 // unsigned src >= 0 is always true |
| 188 | // with |
| 189 | // CONSTANT true |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 190 | instruction->ReplaceWith(GetGraph()->GetConstant(DataType::Type::kBool, 1)); |
Aart Bik | 96709f1 | 2015-10-28 17:49:07 -0700 | [diff] [blame] | 191 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | void InstructionWithAbsorbingInputSimplifier::VisitBelow(HBelow* instruction) { |
| 196 | if (instruction->GetRight()->IsConstant() && |
Roland Levillain | 1a65388 | 2016-03-18 18:05:57 +0000 | [diff] [blame] | 197 | instruction->GetRight()->AsConstant()->IsArithmeticZero()) { |
Aart Bik | 96709f1 | 2015-10-28 17:49:07 -0700 | [diff] [blame] | 198 | // Replace code looking like |
| 199 | // BELOW dst, src, 0 // unsigned src < 0 is always false |
| 200 | // with |
| 201 | // CONSTANT false |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 202 | instruction->ReplaceWith(GetGraph()->GetConstant(DataType::Type::kBool, 0)); |
Aart Bik | 96709f1 | 2015-10-28 17:49:07 -0700 | [diff] [blame] | 203 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | void InstructionWithAbsorbingInputSimplifier::VisitBelowOrEqual(HBelowOrEqual* instruction) { |
| 208 | if (instruction->GetLeft()->IsConstant() && |
Roland Levillain | 1a65388 | 2016-03-18 18:05:57 +0000 | [diff] [blame] | 209 | instruction->GetLeft()->AsConstant()->IsArithmeticZero()) { |
Aart Bik | 96709f1 | 2015-10-28 17:49:07 -0700 | [diff] [blame] | 210 | // Replace code looking like |
| 211 | // BELOW_OR_EQUAL dst, 0, src // unsigned 0 <= src is always true |
| 212 | // with |
| 213 | // CONSTANT true |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 214 | instruction->ReplaceWith(GetGraph()->GetConstant(DataType::Type::kBool, 1)); |
Aart Bik | 96709f1 | 2015-10-28 17:49:07 -0700 | [diff] [blame] | 215 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 216 | } |
| 217 | } |
| 218 | |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 219 | void InstructionWithAbsorbingInputSimplifier::VisitAnd(HAnd* instruction) { |
Balaram Makam | 4eb6eb4 | 2019-09-10 09:41:29 -0500 | [diff] [blame] | 220 | DataType::Type type = instruction->GetType(); |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 221 | HConstant* input_cst = instruction->GetConstantRight(); |
Roland Levillain | 1a65388 | 2016-03-18 18:05:57 +0000 | [diff] [blame] | 222 | if ((input_cst != nullptr) && input_cst->IsZeroBitPattern()) { |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 223 | // Replace code looking like |
| 224 | // AND dst, src, 0 |
| 225 | // with |
| 226 | // CONSTANT 0 |
| 227 | instruction->ReplaceWith(input_cst); |
| 228 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 229 | } |
Balaram Makam | 4eb6eb4 | 2019-09-10 09:41:29 -0500 | [diff] [blame] | 230 | |
| 231 | HInstruction* left = instruction->GetLeft(); |
| 232 | HInstruction* right = instruction->GetRight(); |
| 233 | |
| 234 | if (left->IsNot() ^ right->IsNot()) { |
| 235 | // Replace code looking like |
| 236 | // NOT notsrc, src |
| 237 | // AND dst, notsrc, src |
| 238 | // with |
| 239 | // CONSTANT 0 |
| 240 | HInstruction* hnot = (left->IsNot() ? left : right); |
| 241 | HInstruction* hother = (left->IsNot() ? right : left); |
| 242 | HInstruction* src = hnot->AsNot()->GetInput(); |
| 243 | |
| 244 | if (src == hother) { |
| 245 | instruction->ReplaceWith(GetGraph()->GetConstant(type, 0)); |
| 246 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 247 | } |
| 248 | } |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Roland Levillain | 3b55ebb | 2015-05-08 13:13:19 +0100 | [diff] [blame] | 251 | void InstructionWithAbsorbingInputSimplifier::VisitCompare(HCompare* instruction) { |
| 252 | HConstant* input_cst = instruction->GetConstantRight(); |
| 253 | if (input_cst != nullptr) { |
| 254 | HInstruction* input_value = instruction->GetLeastConstantLeft(); |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 255 | if (DataType::IsFloatingPointType(input_value->GetType()) && |
Roland Levillain | 3b55ebb | 2015-05-08 13:13:19 +0100 | [diff] [blame] | 256 | ((input_cst->IsFloatConstant() && input_cst->AsFloatConstant()->IsNaN()) || |
| 257 | (input_cst->IsDoubleConstant() && input_cst->AsDoubleConstant()->IsNaN()))) { |
| 258 | // Replace code looking like |
Roland Levillain | 31dd3d6 | 2016-02-16 12:21:02 +0000 | [diff] [blame] | 259 | // CMP{G,L}-{FLOAT,DOUBLE} dst, src, NaN |
Roland Levillain | 3b55ebb | 2015-05-08 13:13:19 +0100 | [diff] [blame] | 260 | // with |
| 261 | // CONSTANT +1 (gt bias) |
| 262 | // or |
| 263 | // CONSTANT -1 (lt bias) |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 264 | instruction->ReplaceWith(GetGraph()->GetConstant(DataType::Type::kInt32, |
Roland Levillain | 3b55ebb | 2015-05-08 13:13:19 +0100 | [diff] [blame] | 265 | (instruction->IsGtBias() ? 1 : -1))); |
| 266 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 271 | void InstructionWithAbsorbingInputSimplifier::VisitMul(HMul* instruction) { |
| 272 | HConstant* input_cst = instruction->GetConstantRight(); |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 273 | DataType::Type type = instruction->GetType(); |
| 274 | if (DataType::IsIntOrLongType(type) && |
Roland Levillain | 1a65388 | 2016-03-18 18:05:57 +0000 | [diff] [blame] | 275 | (input_cst != nullptr) && input_cst->IsArithmeticZero()) { |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 276 | // Replace code looking like |
| 277 | // MUL dst, src, 0 |
| 278 | // with |
| 279 | // CONSTANT 0 |
| 280 | // Integral multiplication by zero always yields zero, but floating-point |
| 281 | // multiplication by zero does not always do. For example `Infinity * 0.0` |
| 282 | // should yield a NaN. |
| 283 | instruction->ReplaceWith(input_cst); |
| 284 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | void InstructionWithAbsorbingInputSimplifier::VisitOr(HOr* instruction) { |
| 289 | HConstant* input_cst = instruction->GetConstantRight(); |
| 290 | |
| 291 | if (input_cst == nullptr) { |
| 292 | return; |
| 293 | } |
| 294 | |
| 295 | if (Int64FromConstant(input_cst) == -1) { |
| 296 | // Replace code looking like |
| 297 | // OR dst, src, 0xFFF...FF |
| 298 | // with |
| 299 | // CONSTANT 0xFFF...FF |
| 300 | instruction->ReplaceWith(input_cst); |
| 301 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | void InstructionWithAbsorbingInputSimplifier::VisitRem(HRem* instruction) { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 306 | DataType::Type type = instruction->GetType(); |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 307 | |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 308 | if (!DataType::IsIntegralType(type)) { |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 309 | return; |
| 310 | } |
| 311 | |
| 312 | HBasicBlock* block = instruction->GetBlock(); |
| 313 | |
| 314 | if (instruction->GetLeft()->IsConstant() && |
Roland Levillain | 1a65388 | 2016-03-18 18:05:57 +0000 | [diff] [blame] | 315 | instruction->GetLeft()->AsConstant()->IsArithmeticZero()) { |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 316 | // Replace code looking like |
| 317 | // REM dst, 0, src |
| 318 | // with |
| 319 | // CONSTANT 0 |
| 320 | instruction->ReplaceWith(instruction->GetLeft()); |
| 321 | block->RemoveInstruction(instruction); |
| 322 | } |
| 323 | |
| 324 | HConstant* cst_right = instruction->GetRight()->AsConstant(); |
| 325 | if (((cst_right != nullptr) && |
| 326 | (cst_right->IsOne() || cst_right->IsMinusOne())) || |
| 327 | (instruction->GetLeft() == instruction->GetRight())) { |
| 328 | // Replace code looking like |
| 329 | // REM dst, src, 1 |
| 330 | // or |
| 331 | // REM dst, src, -1 |
| 332 | // or |
| 333 | // REM dst, src, src |
| 334 | // with |
| 335 | // CONSTANT 0 |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 336 | instruction->ReplaceWith(GetGraph()->GetConstant(type, 0)); |
| 337 | block->RemoveInstruction(instruction); |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 338 | } |
| 339 | } |
| 340 | |
| 341 | void InstructionWithAbsorbingInputSimplifier::VisitShl(HShl* instruction) { |
| 342 | VisitShift(instruction); |
| 343 | } |
| 344 | |
| 345 | void InstructionWithAbsorbingInputSimplifier::VisitShr(HShr* instruction) { |
| 346 | VisitShift(instruction); |
| 347 | } |
| 348 | |
| 349 | void InstructionWithAbsorbingInputSimplifier::VisitSub(HSub* instruction) { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 350 | DataType::Type type = instruction->GetType(); |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 351 | |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 352 | if (!DataType::IsIntegralType(type)) { |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 353 | return; |
| 354 | } |
| 355 | |
| 356 | HBasicBlock* block = instruction->GetBlock(); |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 357 | |
| 358 | // We assume that GVN has run before, so we only perform a pointer |
| 359 | // 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] | 360 | // different, we are still correct and only miss an optimization |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 361 | // opportunity. |
| 362 | if (instruction->GetLeft() == instruction->GetRight()) { |
| 363 | // Replace code looking like |
| 364 | // SUB dst, src, src |
| 365 | // with |
| 366 | // CONSTANT 0 |
Kenny Root | 00d597a | 2015-09-30 13:09:51 -0700 | [diff] [blame] | 367 | // 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] | 368 | // not work when `x` is an infinity. |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 369 | instruction->ReplaceWith(GetGraph()->GetConstant(type, 0)); |
| 370 | block->RemoveInstruction(instruction); |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 371 | } |
| 372 | } |
| 373 | |
| 374 | void InstructionWithAbsorbingInputSimplifier::VisitUShr(HUShr* instruction) { |
| 375 | VisitShift(instruction); |
| 376 | } |
| 377 | |
| 378 | void InstructionWithAbsorbingInputSimplifier::VisitXor(HXor* instruction) { |
| 379 | if (instruction->GetLeft() == instruction->GetRight()) { |
| 380 | // Replace code looking like |
| 381 | // XOR dst, src, src |
| 382 | // with |
| 383 | // CONSTANT 0 |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 384 | DataType::Type type = instruction->GetType(); |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 385 | HBasicBlock* block = instruction->GetBlock(); |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 386 | instruction->ReplaceWith(GetGraph()->GetConstant(type, 0)); |
| 387 | block->RemoveInstruction(instruction); |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 388 | } |
| 389 | } |
| 390 | |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 391 | } // namespace art |