blob: 2031707759acf3f538474c437e0523fc6e048477 [file] [log] [blame]
Roland Levillain556c3d12014-09-18 15:25:07 +01001/*
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 Levillain75be2832014-10-17 17:02:00 +010017#include "constant_folding.h"
Roland Levillain556c3d12014-09-18 15:25:07 +010018
Vladimir Marko0a516052019-10-14 13:00:44 +000019namespace art {
Roland Levillain556c3d12014-09-18 15:25:07 +010020
Roland Levillain1252e972015-08-06 15:46:02 +010021// This visitor tries to simplify instructions that can be evaluated
22// as constants.
23class HConstantFoldingVisitor : public HGraphDelegateVisitor {
24 public:
25 explicit HConstantFoldingVisitor(HGraph* graph)
26 : HGraphDelegateVisitor(graph) {}
27
28 private:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010029 void VisitBasicBlock(HBasicBlock* block) override;
Roland Levillain1252e972015-08-06 15:46:02 +010030
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010031 void VisitUnaryOperation(HUnaryOperation* inst) override;
32 void VisitBinaryOperation(HBinaryOperation* inst) override;
Roland Levillain1252e972015-08-06 15:46:02 +010033
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010034 void VisitTypeConversion(HTypeConversion* inst) override;
35 void VisitDivZeroCheck(HDivZeroCheck* inst) override;
Roland Levillain1252e972015-08-06 15:46:02 +010036
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 Ramesb2fd7bc2015-03-11 16:48:16 +000043class InstructionWithAbsorbingInputSimplifier : public HGraphVisitor {
44 public:
45 explicit InstructionWithAbsorbingInputSimplifier(HGraph* graph) : HGraphVisitor(graph) {}
46
47 private:
48 void VisitShift(HBinaryOperation* shift);
49
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010050 void VisitEqual(HEqual* instruction) override;
51 void VisitNotEqual(HNotEqual* instruction) override;
Vladimir Markoa341f352016-08-31 12:18:20 +010052
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010053 void VisitAbove(HAbove* instruction) override;
54 void VisitAboveOrEqual(HAboveOrEqual* instruction) override;
55 void VisitBelow(HBelow* instruction) override;
56 void VisitBelowOrEqual(HBelowOrEqual* instruction) override;
Aart Bik96709f12015-10-28 17:49:07 -070057
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010058 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 Ramesb2fd7bc2015-03-11 16:48:16 +000068};
69
Roland Levillain1252e972015-08-06 15:46:02 +010070
Aart Bik24773202018-04-26 10:28:51 -070071bool HConstantFolding::Run() {
Roland Levillain1252e972015-08-06 15:46:02 +010072 HConstantFoldingVisitor visitor(graph_);
Roland Levillain556c3d12014-09-18 15:25:07 +010073 // 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 Levillain1252e972015-08-06 15:46:02 +010077 visitor.VisitReversePostOrder();
Aart Bik24773202018-04-26 10:28:51 -070078 return true;
Roland Levillain1252e972015-08-06 15:46:02 +010079}
80
81
82void 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 Levillain556c3d12014-09-18 15:25:07 +010088 }
89}
90
Roland Levillain1252e972015-08-06 15:46:02 +010091void 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
101void 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
114void HConstantFoldingVisitor::VisitTypeConversion(HTypeConversion* inst) {
115 // Constant folding: replace `TypeConversion(a)' with a constant at
116 // compile time if `a' is a constant.
Mingyao Yang75bb2f32017-11-30 14:45:44 -0800117 HConstant* constant = inst->TryStaticEvaluation();
Roland Levillain1252e972015-08-06 15:46:02 +0100118 if (constant != nullptr) {
119 inst->ReplaceWith(constant);
120 inst->GetBlock()->RemoveInstruction(inst);
121 }
122}
123
124void 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 Levillain1a653882016-03-18 18:05:57 +0000127 if (check_input->IsConstant() && !check_input->AsConstant()->IsArithmeticZero()) {
Roland Levillain1252e972015-08-06 15:46:02 +0100128 inst->ReplaceWith(check_input);
129 inst->GetBlock()->RemoveInstruction(inst);
130 }
131}
132
133
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000134void InstructionWithAbsorbingInputSimplifier::VisitShift(HBinaryOperation* instruction) {
135 DCHECK(instruction->IsShl() || instruction->IsShr() || instruction->IsUShr());
136 HInstruction* left = instruction->GetLeft();
Roland Levillain1a653882016-03-18 18:05:57 +0000137 if (left->IsConstant() && left->AsConstant()->IsArithmeticZero()) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000138 // 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 Markoa341f352016-08-31 12:18:20 +0100147void 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 Marko0ebe0d82017-09-21 22:50:39 +0100154 instruction->ReplaceWith(GetGraph()->GetConstant(DataType::Type::kBool, 0));
Vladimir Markoa341f352016-08-31 12:18:20 +0100155 instruction->GetBlock()->RemoveInstruction(instruction);
156 }
157}
158
159void 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 Marko0ebe0d82017-09-21 22:50:39 +0100166 instruction->ReplaceWith(GetGraph()->GetConstant(DataType::Type::kBool, 1));
Vladimir Markoa341f352016-08-31 12:18:20 +0100167 instruction->GetBlock()->RemoveInstruction(instruction);
168 }
169}
170
Aart Bik96709f12015-10-28 17:49:07 -0700171void InstructionWithAbsorbingInputSimplifier::VisitAbove(HAbove* instruction) {
172 if (instruction->GetLeft()->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +0000173 instruction->GetLeft()->AsConstant()->IsArithmeticZero()) {
Aart Bik96709f12015-10-28 17:49:07 -0700174 // Replace code looking like
175 // ABOVE dst, 0, src // unsigned 0 > src is always false
176 // with
177 // CONSTANT false
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100178 instruction->ReplaceWith(GetGraph()->GetConstant(DataType::Type::kBool, 0));
Aart Bik96709f12015-10-28 17:49:07 -0700179 instruction->GetBlock()->RemoveInstruction(instruction);
180 }
181}
182
183void InstructionWithAbsorbingInputSimplifier::VisitAboveOrEqual(HAboveOrEqual* instruction) {
184 if (instruction->GetRight()->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +0000185 instruction->GetRight()->AsConstant()->IsArithmeticZero()) {
Aart Bik96709f12015-10-28 17:49:07 -0700186 // Replace code looking like
187 // ABOVE_OR_EQUAL dst, src, 0 // unsigned src >= 0 is always true
188 // with
189 // CONSTANT true
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100190 instruction->ReplaceWith(GetGraph()->GetConstant(DataType::Type::kBool, 1));
Aart Bik96709f12015-10-28 17:49:07 -0700191 instruction->GetBlock()->RemoveInstruction(instruction);
192 }
193}
194
195void InstructionWithAbsorbingInputSimplifier::VisitBelow(HBelow* instruction) {
196 if (instruction->GetRight()->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +0000197 instruction->GetRight()->AsConstant()->IsArithmeticZero()) {
Aart Bik96709f12015-10-28 17:49:07 -0700198 // Replace code looking like
199 // BELOW dst, src, 0 // unsigned src < 0 is always false
200 // with
201 // CONSTANT false
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100202 instruction->ReplaceWith(GetGraph()->GetConstant(DataType::Type::kBool, 0));
Aart Bik96709f12015-10-28 17:49:07 -0700203 instruction->GetBlock()->RemoveInstruction(instruction);
204 }
205}
206
207void InstructionWithAbsorbingInputSimplifier::VisitBelowOrEqual(HBelowOrEqual* instruction) {
208 if (instruction->GetLeft()->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +0000209 instruction->GetLeft()->AsConstant()->IsArithmeticZero()) {
Aart Bik96709f12015-10-28 17:49:07 -0700210 // Replace code looking like
211 // BELOW_OR_EQUAL dst, 0, src // unsigned 0 <= src is always true
212 // with
213 // CONSTANT true
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100214 instruction->ReplaceWith(GetGraph()->GetConstant(DataType::Type::kBool, 1));
Aart Bik96709f12015-10-28 17:49:07 -0700215 instruction->GetBlock()->RemoveInstruction(instruction);
216 }
217}
218
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000219void InstructionWithAbsorbingInputSimplifier::VisitAnd(HAnd* instruction) {
Balaram Makam4eb6eb42019-09-10 09:41:29 -0500220 DataType::Type type = instruction->GetType();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000221 HConstant* input_cst = instruction->GetConstantRight();
Roland Levillain1a653882016-03-18 18:05:57 +0000222 if ((input_cst != nullptr) && input_cst->IsZeroBitPattern()) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000223 // 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 Makam4eb6eb42019-09-10 09:41:29 -0500230
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 Ramesb2fd7bc2015-03-11 16:48:16 +0000249}
250
Roland Levillain3b55ebb2015-05-08 13:13:19 +0100251void InstructionWithAbsorbingInputSimplifier::VisitCompare(HCompare* instruction) {
252 HConstant* input_cst = instruction->GetConstantRight();
253 if (input_cst != nullptr) {
254 HInstruction* input_value = instruction->GetLeastConstantLeft();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100255 if (DataType::IsFloatingPointType(input_value->GetType()) &&
Roland Levillain3b55ebb2015-05-08 13:13:19 +0100256 ((input_cst->IsFloatConstant() && input_cst->AsFloatConstant()->IsNaN()) ||
257 (input_cst->IsDoubleConstant() && input_cst->AsDoubleConstant()->IsNaN()))) {
258 // Replace code looking like
Roland Levillain31dd3d62016-02-16 12:21:02 +0000259 // CMP{G,L}-{FLOAT,DOUBLE} dst, src, NaN
Roland Levillain3b55ebb2015-05-08 13:13:19 +0100260 // with
261 // CONSTANT +1 (gt bias)
262 // or
263 // CONSTANT -1 (lt bias)
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100264 instruction->ReplaceWith(GetGraph()->GetConstant(DataType::Type::kInt32,
Roland Levillain3b55ebb2015-05-08 13:13:19 +0100265 (instruction->IsGtBias() ? 1 : -1)));
266 instruction->GetBlock()->RemoveInstruction(instruction);
267 }
268 }
269}
270
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000271void InstructionWithAbsorbingInputSimplifier::VisitMul(HMul* instruction) {
272 HConstant* input_cst = instruction->GetConstantRight();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100273 DataType::Type type = instruction->GetType();
274 if (DataType::IsIntOrLongType(type) &&
Roland Levillain1a653882016-03-18 18:05:57 +0000275 (input_cst != nullptr) && input_cst->IsArithmeticZero()) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000276 // 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
288void 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
305void InstructionWithAbsorbingInputSimplifier::VisitRem(HRem* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100306 DataType::Type type = instruction->GetType();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000307
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100308 if (!DataType::IsIntegralType(type)) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000309 return;
310 }
311
312 HBasicBlock* block = instruction->GetBlock();
313
314 if (instruction->GetLeft()->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +0000315 instruction->GetLeft()->AsConstant()->IsArithmeticZero()) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000316 // 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 Brazdil8d5b8b22015-03-24 10:51:52 +0000336 instruction->ReplaceWith(GetGraph()->GetConstant(type, 0));
337 block->RemoveInstruction(instruction);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000338 }
339}
340
341void InstructionWithAbsorbingInputSimplifier::VisitShl(HShl* instruction) {
342 VisitShift(instruction);
343}
344
345void InstructionWithAbsorbingInputSimplifier::VisitShr(HShr* instruction) {
346 VisitShift(instruction);
347}
348
349void InstructionWithAbsorbingInputSimplifier::VisitSub(HSub* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100350 DataType::Type type = instruction->GetType();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000351
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100352 if (!DataType::IsIntegralType(type)) {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000353 return;
354 }
355
356 HBasicBlock* block = instruction->GetBlock();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000357
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 Root00d597a2015-09-30 13:09:51 -0700360 // different, we are still correct and only miss an optimization
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000361 // opportunity.
362 if (instruction->GetLeft() == instruction->GetRight()) {
363 // Replace code looking like
364 // SUB dst, src, src
365 // with
366 // CONSTANT 0
Kenny Root00d597a2015-09-30 13:09:51 -0700367 // Note that we cannot optimize `x - x` to `0` for floating-point. It does
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000368 // not work when `x` is an infinity.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000369 instruction->ReplaceWith(GetGraph()->GetConstant(type, 0));
370 block->RemoveInstruction(instruction);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000371 }
372}
373
374void InstructionWithAbsorbingInputSimplifier::VisitUShr(HUShr* instruction) {
375 VisitShift(instruction);
376}
377
378void 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 Marko0ebe0d82017-09-21 22:50:39 +0100384 DataType::Type type = instruction->GetType();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000385 HBasicBlock* block = instruction->GetBlock();
David Brazdil8d5b8b22015-03-24 10:51:52 +0000386 instruction->ReplaceWith(GetGraph()->GetConstant(type, 0));
387 block->RemoveInstruction(instruction);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000388 }
389}
390
Roland Levillain556c3d12014-09-18 15:25:07 +0100391} // namespace art