blob: 5a1d9b488fa03aacedc0eb6487f595232c280575 [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
19namespace art {
20
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +000021// This visitor tries to simplify operations that yield a constant. For example
22// `input * 0` is replaced by a null constant.
23class InstructionWithAbsorbingInputSimplifier : public HGraphVisitor {
24 public:
25 explicit InstructionWithAbsorbingInputSimplifier(HGraph* graph) : HGraphVisitor(graph) {}
26
27 private:
28 void VisitShift(HBinaryOperation* shift);
29
30 void VisitAnd(HAnd* instruction) OVERRIDE;
Roland Levillain3b55ebb2015-05-08 13:13:19 +010031 void VisitCompare(HCompare* instruction) OVERRIDE;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +000032 void VisitMul(HMul* instruction) OVERRIDE;
33 void VisitOr(HOr* instruction) OVERRIDE;
34 void VisitRem(HRem* instruction) OVERRIDE;
35 void VisitShl(HShl* instruction) OVERRIDE;
36 void VisitShr(HShr* instruction) OVERRIDE;
37 void VisitSub(HSub* instruction) OVERRIDE;
38 void VisitUShr(HUShr* instruction) OVERRIDE;
39 void VisitXor(HXor* instruction) OVERRIDE;
40};
41
Roland Levillain75be2832014-10-17 17:02:00 +010042void HConstantFolding::Run() {
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +000043 InstructionWithAbsorbingInputSimplifier simplifier(graph_);
Roland Levillain556c3d12014-09-18 15:25:07 +010044 // Process basic blocks in reverse post-order in the dominator tree,
45 // so that an instruction turned into a constant, used as input of
46 // another instruction, may possibly be used to turn that second
47 // instruction into a constant as well.
48 for (HReversePostOrderIterator it(*graph_); !it.Done(); it.Advance()) {
49 HBasicBlock* block = it.Current();
50 // Traverse this block's instructions in (forward) order and
51 // replace the ones that can be statically evaluated by a
52 // compile-time counterpart.
Andreas Gampe277ccbd2014-11-03 21:36:10 -080053 for (HInstructionIterator inst_it(block->GetInstructions());
54 !inst_it.Done(); inst_it.Advance()) {
55 HInstruction* inst = inst_it.Current();
Roland Levillain556c3d12014-09-18 15:25:07 +010056 if (inst->IsBinaryOperation()) {
Roland Levillain9240d6a2014-10-20 16:47:04 +010057 // Constant folding: replace `op(a, b)' with a constant at
58 // compile time if `a' and `b' are both constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +000059 HConstant* constant = inst->AsBinaryOperation()->TryStaticEvaluation();
Roland Levillain9240d6a2014-10-20 16:47:04 +010060 if (constant != nullptr) {
David Brazdil8d5b8b22015-03-24 10:51:52 +000061 inst->ReplaceWith(constant);
62 inst->GetBlock()->RemoveInstruction(inst);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +000063 } else {
64 inst->Accept(&simplifier);
Roland Levillain9240d6a2014-10-20 16:47:04 +010065 }
66 } else if (inst->IsUnaryOperation()) {
67 // Constant folding: replace `op(a)' with a constant at compile
68 // time if `a' is a constant.
David Brazdil8d5b8b22015-03-24 10:51:52 +000069 HConstant* constant = inst->AsUnaryOperation()->TryStaticEvaluation();
Roland Levillain556c3d12014-09-18 15:25:07 +010070 if (constant != nullptr) {
David Brazdil8d5b8b22015-03-24 10:51:52 +000071 inst->ReplaceWith(constant);
72 inst->GetBlock()->RemoveInstruction(inst);
Roland Levillain556c3d12014-09-18 15:25:07 +010073 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +000074 } else if (inst->IsDivZeroCheck()) {
75 // We can safely remove the check if the input is a non-null constant.
76 HDivZeroCheck* check = inst->AsDivZeroCheck();
77 HInstruction* check_input = check->InputAt(0);
78 if (check_input->IsConstant() && !check_input->AsConstant()->IsZero()) {
79 check->ReplaceWith(check_input);
80 check->GetBlock()->RemoveInstruction(check);
81 }
Roland Levillain556c3d12014-09-18 15:25:07 +010082 }
83 }
84 }
85}
86
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +000087void InstructionWithAbsorbingInputSimplifier::VisitShift(HBinaryOperation* instruction) {
88 DCHECK(instruction->IsShl() || instruction->IsShr() || instruction->IsUShr());
89 HInstruction* left = instruction->GetLeft();
90 if (left->IsConstant() && left->AsConstant()->IsZero()) {
91 // Replace code looking like
92 // SHL dst, 0, shift_amount
93 // with
94 // CONSTANT 0
95 instruction->ReplaceWith(left);
96 instruction->GetBlock()->RemoveInstruction(instruction);
97 }
98}
99
100void InstructionWithAbsorbingInputSimplifier::VisitAnd(HAnd* instruction) {
101 HConstant* input_cst = instruction->GetConstantRight();
102 if ((input_cst != nullptr) && input_cst->IsZero()) {
103 // Replace code looking like
104 // AND dst, src, 0
105 // with
106 // CONSTANT 0
107 instruction->ReplaceWith(input_cst);
108 instruction->GetBlock()->RemoveInstruction(instruction);
109 }
110}
111
Roland Levillain3b55ebb2015-05-08 13:13:19 +0100112void InstructionWithAbsorbingInputSimplifier::VisitCompare(HCompare* instruction) {
113 HConstant* input_cst = instruction->GetConstantRight();
114 if (input_cst != nullptr) {
115 HInstruction* input_value = instruction->GetLeastConstantLeft();
116 if (Primitive::IsFloatingPointType(input_value->GetType()) &&
117 ((input_cst->IsFloatConstant() && input_cst->AsFloatConstant()->IsNaN()) ||
118 (input_cst->IsDoubleConstant() && input_cst->AsDoubleConstant()->IsNaN()))) {
119 // Replace code looking like
120 // CMP{G,L} dst, src, NaN
121 // with
122 // CONSTANT +1 (gt bias)
123 // or
124 // CONSTANT -1 (lt bias)
125 instruction->ReplaceWith(GetGraph()->GetConstant(Primitive::kPrimInt,
126 (instruction->IsGtBias() ? 1 : -1)));
127 instruction->GetBlock()->RemoveInstruction(instruction);
128 }
129 }
130}
131
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000132void InstructionWithAbsorbingInputSimplifier::VisitMul(HMul* instruction) {
133 HConstant* input_cst = instruction->GetConstantRight();
134 Primitive::Type type = instruction->GetType();
135 if (Primitive::IsIntOrLongType(type) &&
136 (input_cst != nullptr) && input_cst->IsZero()) {
137 // Replace code looking like
138 // MUL dst, src, 0
139 // with
140 // CONSTANT 0
141 // Integral multiplication by zero always yields zero, but floating-point
142 // multiplication by zero does not always do. For example `Infinity * 0.0`
143 // should yield a NaN.
144 instruction->ReplaceWith(input_cst);
145 instruction->GetBlock()->RemoveInstruction(instruction);
146 }
147}
148
149void InstructionWithAbsorbingInputSimplifier::VisitOr(HOr* instruction) {
150 HConstant* input_cst = instruction->GetConstantRight();
151
152 if (input_cst == nullptr) {
153 return;
154 }
155
156 if (Int64FromConstant(input_cst) == -1) {
157 // Replace code looking like
158 // OR dst, src, 0xFFF...FF
159 // with
160 // CONSTANT 0xFFF...FF
161 instruction->ReplaceWith(input_cst);
162 instruction->GetBlock()->RemoveInstruction(instruction);
163 }
164}
165
166void InstructionWithAbsorbingInputSimplifier::VisitRem(HRem* instruction) {
167 Primitive::Type type = instruction->GetType();
168
169 if (!Primitive::IsIntegralType(type)) {
170 return;
171 }
172
173 HBasicBlock* block = instruction->GetBlock();
174
175 if (instruction->GetLeft()->IsConstant() &&
176 instruction->GetLeft()->AsConstant()->IsZero()) {
177 // Replace code looking like
178 // REM dst, 0, src
179 // with
180 // CONSTANT 0
181 instruction->ReplaceWith(instruction->GetLeft());
182 block->RemoveInstruction(instruction);
183 }
184
185 HConstant* cst_right = instruction->GetRight()->AsConstant();
186 if (((cst_right != nullptr) &&
187 (cst_right->IsOne() || cst_right->IsMinusOne())) ||
188 (instruction->GetLeft() == instruction->GetRight())) {
189 // Replace code looking like
190 // REM dst, src, 1
191 // or
192 // REM dst, src, -1
193 // or
194 // REM dst, src, src
195 // with
196 // CONSTANT 0
David Brazdil8d5b8b22015-03-24 10:51:52 +0000197 instruction->ReplaceWith(GetGraph()->GetConstant(type, 0));
198 block->RemoveInstruction(instruction);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000199 }
200}
201
202void InstructionWithAbsorbingInputSimplifier::VisitShl(HShl* instruction) {
203 VisitShift(instruction);
204}
205
206void InstructionWithAbsorbingInputSimplifier::VisitShr(HShr* instruction) {
207 VisitShift(instruction);
208}
209
210void InstructionWithAbsorbingInputSimplifier::VisitSub(HSub* instruction) {
211 Primitive::Type type = instruction->GetType();
212
213 if (!Primitive::IsIntegralType(type)) {
214 return;
215 }
216
217 HBasicBlock* block = instruction->GetBlock();
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000218
219 // We assume that GVN has run before, so we only perform a pointer
220 // comparison. If for some reason the values are equal but the pointers are
221 // different, we are still correct and only miss an optimisation
222 // opportunity.
223 if (instruction->GetLeft() == instruction->GetRight()) {
224 // Replace code looking like
225 // SUB dst, src, src
226 // with
227 // CONSTANT 0
228 // Note that we cannot optimise `x - x` to `0` for floating-point. It does
229 // not work when `x` is an infinity.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000230 instruction->ReplaceWith(GetGraph()->GetConstant(type, 0));
231 block->RemoveInstruction(instruction);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000232 }
233}
234
235void InstructionWithAbsorbingInputSimplifier::VisitUShr(HUShr* instruction) {
236 VisitShift(instruction);
237}
238
239void InstructionWithAbsorbingInputSimplifier::VisitXor(HXor* instruction) {
240 if (instruction->GetLeft() == instruction->GetRight()) {
241 // Replace code looking like
242 // XOR dst, src, src
243 // with
244 // CONSTANT 0
245 Primitive::Type type = instruction->GetType();
246 HBasicBlock* block = instruction->GetBlock();
David Brazdil8d5b8b22015-03-24 10:51:52 +0000247 instruction->ReplaceWith(GetGraph()->GetConstant(type, 0));
248 block->RemoveInstruction(instruction);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000249 }
250}
251
Roland Levillain556c3d12014-09-18 15:25:07 +0100252} // namespace art