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 | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 17 | #include <functional> |
| 18 | |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 19 | #include "constant_folding.h" |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 20 | #include "dead_code_elimination.h" |
Calin Juravle | cd6dffe | 2015-01-08 17:35:35 +0000 | [diff] [blame] | 21 | #include "driver/compiler_options.h" |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 22 | #include "graph_checker.h" |
| 23 | #include "optimizing_unit_test.h" |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 24 | #include "pretty_printer.h" |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 25 | |
| 26 | #include "gtest/gtest.h" |
| 27 | |
Vladimir Marko | 0a51605 | 2019-10-14 13:00:44 +0000 | [diff] [blame] | 28 | namespace art { |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 29 | |
Aart Bik | 96709f1 | 2015-10-28 17:49:07 -0700 | [diff] [blame] | 30 | /** |
| 31 | * Fixture class for the constant folding and dce tests. |
| 32 | */ |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 33 | class ConstantFoldingTest : public OptimizingUnitTest { |
Aart Bik | 96709f1 | 2015-10-28 17:49:07 -0700 | [diff] [blame] | 34 | public: |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 35 | ConstantFoldingTest() : graph_(nullptr) { } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 36 | |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 37 | void TestCode(const std::vector<uint16_t>& data, |
Aart Bik | 96709f1 | 2015-10-28 17:49:07 -0700 | [diff] [blame] | 38 | const std::string& expected_before, |
| 39 | const std::string& expected_after_cf, |
| 40 | const std::string& expected_after_dce, |
Andreas Gampe | ca620d7 | 2016-11-08 08:09:33 -0800 | [diff] [blame] | 41 | const std::function<void(HGraph*)>& check_after_cf, |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 42 | DataType::Type return_type = DataType::Type::kInt32) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 43 | graph_ = CreateCFG(data, return_type); |
Aart Bik | 96709f1 | 2015-10-28 17:49:07 -0700 | [diff] [blame] | 44 | TestCodeOnReadyGraph(expected_before, |
| 45 | expected_after_cf, |
| 46 | expected_after_dce, |
| 47 | check_after_cf); |
| 48 | } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 49 | |
Aart Bik | 96709f1 | 2015-10-28 17:49:07 -0700 | [diff] [blame] | 50 | void TestCodeOnReadyGraph(const std::string& expected_before, |
| 51 | const std::string& expected_after_cf, |
| 52 | const std::string& expected_after_dce, |
Andreas Gampe | ca620d7 | 2016-11-08 08:09:33 -0800 | [diff] [blame] | 53 | const std::function<void(HGraph*)>& check_after_cf) { |
Aart Bik | 96709f1 | 2015-10-28 17:49:07 -0700 | [diff] [blame] | 54 | ASSERT_NE(graph_, nullptr); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 55 | |
Aart Bik | 96709f1 | 2015-10-28 17:49:07 -0700 | [diff] [blame] | 56 | StringPrettyPrinter printer_before(graph_); |
| 57 | printer_before.VisitInsertionOrder(); |
| 58 | std::string actual_before = printer_before.str(); |
| 59 | EXPECT_EQ(expected_before, actual_before); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 60 | |
Andreas Gampe | ca620d7 | 2016-11-08 08:09:33 -0800 | [diff] [blame] | 61 | HConstantFolding(graph_, "constant_folding").Run(); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 62 | GraphChecker graph_checker_cf(graph_); |
| 63 | graph_checker_cf.Run(); |
| 64 | ASSERT_TRUE(graph_checker_cf.IsValid()); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 65 | |
Aart Bik | 96709f1 | 2015-10-28 17:49:07 -0700 | [diff] [blame] | 66 | StringPrettyPrinter printer_after_cf(graph_); |
| 67 | printer_after_cf.VisitInsertionOrder(); |
| 68 | std::string actual_after_cf = printer_after_cf.str(); |
| 69 | EXPECT_EQ(expected_after_cf, actual_after_cf); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 70 | |
Aart Bik | 96709f1 | 2015-10-28 17:49:07 -0700 | [diff] [blame] | 71 | check_after_cf(graph_); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 72 | |
Andreas Gampe | 3db7068 | 2018-12-26 15:12:03 -0800 | [diff] [blame] | 73 | HDeadCodeElimination(graph_, /* stats= */ nullptr, "dead_code_elimination").Run(); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 74 | GraphChecker graph_checker_dce(graph_); |
| 75 | graph_checker_dce.Run(); |
| 76 | ASSERT_TRUE(graph_checker_dce.IsValid()); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 77 | |
Aart Bik | 96709f1 | 2015-10-28 17:49:07 -0700 | [diff] [blame] | 78 | StringPrettyPrinter printer_after_dce(graph_); |
| 79 | printer_after_dce.VisitInsertionOrder(); |
| 80 | std::string actual_after_dce = printer_after_dce.str(); |
| 81 | EXPECT_EQ(expected_after_dce, actual_after_dce); |
| 82 | } |
| 83 | |
Aart Bik | 96709f1 | 2015-10-28 17:49:07 -0700 | [diff] [blame] | 84 | HGraph* graph_; |
| 85 | }; |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 86 | |
| 87 | /** |
Roland Levillain | 9240d6a | 2014-10-20 16:47:04 +0100 | [diff] [blame] | 88 | * Tiny three-register program exercising int constant folding on negation. |
| 89 | * |
| 90 | * 16-bit |
| 91 | * offset |
| 92 | * ------ |
| 93 | * v0 <- 1 0. const/4 v0, #+1 |
Roland Levillain | c90bc7c | 2014-12-11 12:14:33 +0000 | [diff] [blame] | 94 | * v1 <- -v0 1. neg-int v1, v0 |
Roland Levillain | 9240d6a | 2014-10-20 16:47:04 +0100 | [diff] [blame] | 95 | * return v1 2. return v1 |
| 96 | */ |
Aart Bik | 96709f1 | 2015-10-28 17:49:07 -0700 | [diff] [blame] | 97 | TEST_F(ConstantFoldingTest, IntConstantFoldingNegation) { |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 98 | const std::vector<uint16_t> data = TWO_REGISTERS_CODE_ITEM( |
Roland Levillain | 9240d6a | 2014-10-20 16:47:04 +0100 | [diff] [blame] | 99 | Instruction::CONST_4 | 0 << 8 | 1 << 12, |
| 100 | Instruction::NEG_INT | 1 << 8 | 0 << 12, |
| 101 | Instruction::RETURN | 1 << 8); |
| 102 | |
| 103 | std::string expected_before = |
| 104 | "BasicBlock 0, succ: 1\n" |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 105 | " 2: IntConstant [3]\n" |
| 106 | " 0: SuspendCheck\n" |
| 107 | " 1: Goto 1\n" |
Roland Levillain | 9240d6a | 2014-10-20 16:47:04 +0100 | [diff] [blame] | 108 | "BasicBlock 1, pred: 0, succ: 2\n" |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 109 | " 3: Neg(2) [4]\n" |
| 110 | " 4: Return(3)\n" |
Roland Levillain | 9240d6a | 2014-10-20 16:47:04 +0100 | [diff] [blame] | 111 | "BasicBlock 2, pred: 1\n" |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 112 | " 5: Exit\n"; |
Roland Levillain | 9240d6a | 2014-10-20 16:47:04 +0100 | [diff] [blame] | 113 | |
| 114 | // Expected difference after constant folding. |
| 115 | diff_t expected_cf_diff = { |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 116 | { " 2: IntConstant [3]\n", " 2: IntConstant\n" |
| 117 | " 6: IntConstant [4]\n" }, |
| 118 | { " 3: Neg(2) [4]\n", removed }, |
| 119 | { " 4: Return(3)\n", " 4: Return(6)\n" } |
Roland Levillain | 9240d6a | 2014-10-20 16:47:04 +0100 | [diff] [blame] | 120 | }; |
| 121 | std::string expected_after_cf = Patch(expected_before, expected_cf_diff); |
| 122 | |
| 123 | // Check the value of the computed constant. |
| 124 | auto check_after_cf = [](HGraph* graph) { |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 125 | HInstruction* inst = graph->GetBlocks()[1]->GetFirstInstruction()->InputAt(0); |
Roland Levillain | 9240d6a | 2014-10-20 16:47:04 +0100 | [diff] [blame] | 126 | ASSERT_TRUE(inst->IsIntConstant()); |
| 127 | ASSERT_EQ(inst->AsIntConstant()->GetValue(), -1); |
| 128 | }; |
| 129 | |
| 130 | // Expected difference after dead code elimination. |
| 131 | diff_t expected_dce_diff = { |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 132 | { " 2: IntConstant\n", removed }, |
Roland Levillain | 9240d6a | 2014-10-20 16:47:04 +0100 | [diff] [blame] | 133 | }; |
| 134 | std::string expected_after_dce = Patch(expected_after_cf, expected_dce_diff); |
| 135 | |
| 136 | TestCode(data, |
| 137 | expected_before, |
| 138 | expected_after_cf, |
| 139 | expected_after_dce, |
| 140 | check_after_cf); |
| 141 | } |
| 142 | |
| 143 | /** |
Roland Levillain | c90bc7c | 2014-12-11 12:14:33 +0000 | [diff] [blame] | 144 | * Tiny three-register program exercising long constant folding on negation. |
| 145 | * |
| 146 | * 16-bit |
| 147 | * offset |
| 148 | * ------ |
| 149 | * (v0, v1) <- 4294967296 0. const-wide v0 #+4294967296 |
| 150 | * (v2, v3) <- -(v0, v1) 1. neg-long v2, v0 |
| 151 | * return (v2, v3) 2. return-wide v2 |
| 152 | */ |
Aart Bik | 96709f1 | 2015-10-28 17:49:07 -0700 | [diff] [blame] | 153 | TEST_F(ConstantFoldingTest, LongConstantFoldingNegation) { |
Roland Levillain | c90bc7c | 2014-12-11 12:14:33 +0000 | [diff] [blame] | 154 | const int64_t input = INT64_C(4294967296); // 2^32 |
| 155 | const uint16_t word0 = Low16Bits(Low32Bits(input)); // LSW. |
| 156 | const uint16_t word1 = High16Bits(Low32Bits(input)); |
| 157 | const uint16_t word2 = Low16Bits(High32Bits(input)); |
| 158 | const uint16_t word3 = High16Bits(High32Bits(input)); // MSW. |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 159 | const std::vector<uint16_t> data = FOUR_REGISTERS_CODE_ITEM( |
Roland Levillain | c90bc7c | 2014-12-11 12:14:33 +0000 | [diff] [blame] | 160 | Instruction::CONST_WIDE | 0 << 8, word0, word1, word2, word3, |
| 161 | Instruction::NEG_LONG | 2 << 8 | 0 << 12, |
| 162 | Instruction::RETURN_WIDE | 2 << 8); |
| 163 | |
| 164 | std::string expected_before = |
| 165 | "BasicBlock 0, succ: 1\n" |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 166 | " 2: LongConstant [3]\n" |
| 167 | " 0: SuspendCheck\n" |
| 168 | " 1: Goto 1\n" |
Roland Levillain | c90bc7c | 2014-12-11 12:14:33 +0000 | [diff] [blame] | 169 | "BasicBlock 1, pred: 0, succ: 2\n" |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 170 | " 3: Neg(2) [4]\n" |
| 171 | " 4: Return(3)\n" |
Roland Levillain | c90bc7c | 2014-12-11 12:14:33 +0000 | [diff] [blame] | 172 | "BasicBlock 2, pred: 1\n" |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 173 | " 5: Exit\n"; |
Roland Levillain | c90bc7c | 2014-12-11 12:14:33 +0000 | [diff] [blame] | 174 | |
| 175 | // Expected difference after constant folding. |
| 176 | diff_t expected_cf_diff = { |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 177 | { " 2: LongConstant [3]\n", " 2: LongConstant\n" |
| 178 | " 6: LongConstant [4]\n" }, |
| 179 | { " 3: Neg(2) [4]\n", removed }, |
| 180 | { " 4: Return(3)\n", " 4: Return(6)\n" } |
Roland Levillain | c90bc7c | 2014-12-11 12:14:33 +0000 | [diff] [blame] | 181 | }; |
| 182 | std::string expected_after_cf = Patch(expected_before, expected_cf_diff); |
| 183 | |
| 184 | // Check the value of the computed constant. |
| 185 | auto check_after_cf = [](HGraph* graph) { |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 186 | HInstruction* inst = graph->GetBlocks()[1]->GetFirstInstruction()->InputAt(0); |
Roland Levillain | c90bc7c | 2014-12-11 12:14:33 +0000 | [diff] [blame] | 187 | ASSERT_TRUE(inst->IsLongConstant()); |
| 188 | ASSERT_EQ(inst->AsLongConstant()->GetValue(), INT64_C(-4294967296)); |
| 189 | }; |
| 190 | |
| 191 | // Expected difference after dead code elimination. |
| 192 | diff_t expected_dce_diff = { |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 193 | { " 2: LongConstant\n", removed }, |
Roland Levillain | c90bc7c | 2014-12-11 12:14:33 +0000 | [diff] [blame] | 194 | }; |
| 195 | std::string expected_after_dce = Patch(expected_after_cf, expected_dce_diff); |
| 196 | |
| 197 | TestCode(data, |
| 198 | expected_before, |
| 199 | expected_after_cf, |
| 200 | expected_after_dce, |
| 201 | check_after_cf, |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 202 | DataType::Type::kInt64); |
Roland Levillain | c90bc7c | 2014-12-11 12:14:33 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | /** |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 206 | * Tiny three-register program exercising int constant folding on addition. |
| 207 | * |
| 208 | * 16-bit |
| 209 | * offset |
| 210 | * ------ |
| 211 | * v0 <- 1 0. const/4 v0, #+1 |
| 212 | * v1 <- 2 1. const/4 v1, #+2 |
| 213 | * v2 <- v0 + v1 2. add-int v2, v0, v1 |
| 214 | * return v2 4. return v2 |
| 215 | */ |
Aart Bik | 96709f1 | 2015-10-28 17:49:07 -0700 | [diff] [blame] | 216 | TEST_F(ConstantFoldingTest, IntConstantFoldingOnAddition1) { |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 217 | const std::vector<uint16_t> data = THREE_REGISTERS_CODE_ITEM( |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 218 | Instruction::CONST_4 | 0 << 8 | 1 << 12, |
| 219 | Instruction::CONST_4 | 1 << 8 | 2 << 12, |
| 220 | Instruction::ADD_INT | 2 << 8, 0 | 1 << 8, |
| 221 | Instruction::RETURN | 2 << 8); |
| 222 | |
| 223 | std::string expected_before = |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 224 | "BasicBlock 0, succ: 1\n" |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 225 | " 2: IntConstant [4]\n" |
| 226 | " 3: IntConstant [4]\n" |
| 227 | " 0: SuspendCheck\n" |
| 228 | " 1: Goto 1\n" |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 229 | "BasicBlock 1, pred: 0, succ: 2\n" |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 230 | " 4: Add(2, 3) [5]\n" |
| 231 | " 5: Return(4)\n" |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 232 | "BasicBlock 2, pred: 1\n" |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 233 | " 6: Exit\n"; |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 234 | |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 235 | // Expected difference after constant folding. |
| 236 | diff_t expected_cf_diff = { |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 237 | { " 2: IntConstant [4]\n", " 2: IntConstant\n" }, |
| 238 | { " 3: IntConstant [4]\n", " 3: IntConstant\n" |
| 239 | " 7: IntConstant [5]\n" }, |
| 240 | { " 4: Add(2, 3) [5]\n", removed }, |
| 241 | { " 5: Return(4)\n", " 5: Return(7)\n" } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 242 | }; |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 243 | std::string expected_after_cf = Patch(expected_before, expected_cf_diff); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 244 | |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 245 | // Check the value of the computed constant. |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 246 | auto check_after_cf = [](HGraph* graph) { |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 247 | HInstruction* inst = graph->GetBlocks()[1]->GetFirstInstruction()->InputAt(0); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 248 | ASSERT_TRUE(inst->IsIntConstant()); |
| 249 | ASSERT_EQ(inst->AsIntConstant()->GetValue(), 3); |
| 250 | }; |
| 251 | |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 252 | // Expected difference after dead code elimination. |
| 253 | diff_t expected_dce_diff = { |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 254 | { " 2: IntConstant\n", removed }, |
| 255 | { " 3: IntConstant\n", removed } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 256 | }; |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 257 | std::string expected_after_dce = Patch(expected_after_cf, expected_dce_diff); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 258 | |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 259 | TestCode(data, |
| 260 | expected_before, |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 261 | expected_after_cf, |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 262 | expected_after_dce, |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 263 | check_after_cf); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Small three-register program exercising int constant folding on addition. |
| 268 | * |
| 269 | * 16-bit |
| 270 | * offset |
| 271 | * ------ |
| 272 | * v0 <- 1 0. const/4 v0, #+1 |
| 273 | * v1 <- 2 1. const/4 v1, #+2 |
| 274 | * v0 <- v0 + v1 2. add-int/2addr v0, v1 |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 275 | * v1 <- 4 3. const/4 v1, #+4 |
| 276 | * v2 <- 5 4. const/4 v2, #+5 |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 277 | * v1 <- v1 + v2 5. add-int/2addr v1, v2 |
| 278 | * v2 <- v0 + v1 6. add-int v2, v0, v1 |
| 279 | * return v2 8. return v2 |
| 280 | */ |
Aart Bik | 96709f1 | 2015-10-28 17:49:07 -0700 | [diff] [blame] | 281 | TEST_F(ConstantFoldingTest, IntConstantFoldingOnAddition2) { |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 282 | const std::vector<uint16_t> data = THREE_REGISTERS_CODE_ITEM( |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 283 | Instruction::CONST_4 | 0 << 8 | 1 << 12, |
| 284 | Instruction::CONST_4 | 1 << 8 | 2 << 12, |
| 285 | Instruction::ADD_INT_2ADDR | 0 << 8 | 1 << 12, |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 286 | Instruction::CONST_4 | 1 << 8 | 4 << 12, |
| 287 | Instruction::CONST_4 | 2 << 8 | 5 << 12, |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 288 | Instruction::ADD_INT_2ADDR | 1 << 8 | 2 << 12, |
| 289 | Instruction::ADD_INT | 2 << 8, 0 | 1 << 8, |
| 290 | Instruction::RETURN | 2 << 8); |
| 291 | |
| 292 | std::string expected_before = |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 293 | "BasicBlock 0, succ: 1\n" |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 294 | " 2: IntConstant [4]\n" |
| 295 | " 3: IntConstant [4]\n" |
| 296 | " 5: IntConstant [7]\n" |
| 297 | " 6: IntConstant [7]\n" |
| 298 | " 0: SuspendCheck\n" |
| 299 | " 1: Goto 1\n" |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 300 | "BasicBlock 1, pred: 0, succ: 2\n" |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 301 | " 4: Add(2, 3) [8]\n" |
| 302 | " 7: Add(5, 6) [8]\n" |
| 303 | " 8: Add(4, 7) [9]\n" |
| 304 | " 9: Return(8)\n" |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 305 | "BasicBlock 2, pred: 1\n" |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 306 | " 10: Exit\n"; |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 307 | |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 308 | // Expected difference after constant folding. |
| 309 | diff_t expected_cf_diff = { |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 310 | { " 2: IntConstant [4]\n", " 2: IntConstant\n" }, |
| 311 | { " 3: IntConstant [4]\n", " 3: IntConstant\n" }, |
| 312 | { " 5: IntConstant [7]\n", " 5: IntConstant\n" }, |
| 313 | { " 6: IntConstant [7]\n", " 6: IntConstant\n" |
| 314 | " 11: IntConstant\n" |
| 315 | " 12: IntConstant\n" |
| 316 | " 13: IntConstant [9]\n" }, |
| 317 | { " 4: Add(2, 3) [8]\n", removed }, |
| 318 | { " 7: Add(5, 6) [8]\n", removed }, |
| 319 | { " 8: Add(4, 7) [9]\n", removed }, |
| 320 | { " 9: Return(8)\n", " 9: Return(13)\n" } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 321 | }; |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 322 | std::string expected_after_cf = Patch(expected_before, expected_cf_diff); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 323 | |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 324 | // Check the values of the computed constants. |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 325 | auto check_after_cf = [](HGraph* graph) { |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 326 | HInstruction* inst1 = graph->GetBlocks()[1]->GetFirstInstruction()->InputAt(0); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 327 | ASSERT_TRUE(inst1->IsIntConstant()); |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 328 | ASSERT_EQ(inst1->AsIntConstant()->GetValue(), 12); |
| 329 | HInstruction* inst2 = inst1->GetPrevious(); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 330 | ASSERT_TRUE(inst2->IsIntConstant()); |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 331 | ASSERT_EQ(inst2->AsIntConstant()->GetValue(), 9); |
| 332 | HInstruction* inst3 = inst2->GetPrevious(); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 333 | ASSERT_TRUE(inst3->IsIntConstant()); |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 334 | ASSERT_EQ(inst3->AsIntConstant()->GetValue(), 3); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 335 | }; |
| 336 | |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 337 | // Expected difference after dead code elimination. |
| 338 | diff_t expected_dce_diff = { |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 339 | { " 2: IntConstant\n", removed }, |
| 340 | { " 3: IntConstant\n", removed }, |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 341 | { " 5: IntConstant\n", removed }, |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 342 | { " 6: IntConstant\n", removed }, |
| 343 | { " 11: IntConstant\n", removed }, |
| 344 | { " 12: IntConstant\n", removed } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 345 | }; |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 346 | std::string expected_after_dce = Patch(expected_after_cf, expected_dce_diff); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 347 | |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 348 | TestCode(data, |
| 349 | expected_before, |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 350 | expected_after_cf, |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 351 | expected_after_dce, |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 352 | check_after_cf); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | /** |
| 356 | * Tiny three-register program exercising int constant folding on subtraction. |
| 357 | * |
| 358 | * 16-bit |
| 359 | * offset |
| 360 | * ------ |
| 361 | * v0 <- 3 0. const/4 v0, #+3 |
| 362 | * v1 <- 2 1. const/4 v1, #+2 |
| 363 | * v2 <- v0 - v1 2. sub-int v2, v0, v1 |
| 364 | * return v2 4. return v2 |
| 365 | */ |
Aart Bik | 96709f1 | 2015-10-28 17:49:07 -0700 | [diff] [blame] | 366 | TEST_F(ConstantFoldingTest, IntConstantFoldingOnSubtraction) { |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 367 | const std::vector<uint16_t> data = THREE_REGISTERS_CODE_ITEM( |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 368 | Instruction::CONST_4 | 0 << 8 | 3 << 12, |
| 369 | Instruction::CONST_4 | 1 << 8 | 2 << 12, |
| 370 | Instruction::SUB_INT | 2 << 8, 0 | 1 << 8, |
| 371 | Instruction::RETURN | 2 << 8); |
| 372 | |
| 373 | std::string expected_before = |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 374 | "BasicBlock 0, succ: 1\n" |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 375 | " 2: IntConstant [4]\n" |
| 376 | " 3: IntConstant [4]\n" |
| 377 | " 0: SuspendCheck\n" |
| 378 | " 1: Goto 1\n" |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 379 | "BasicBlock 1, pred: 0, succ: 2\n" |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 380 | " 4: Sub(2, 3) [5]\n" |
| 381 | " 5: Return(4)\n" |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 382 | "BasicBlock 2, pred: 1\n" |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 383 | " 6: Exit\n"; |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 384 | |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 385 | // Expected difference after constant folding. |
| 386 | diff_t expected_cf_diff = { |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 387 | { " 2: IntConstant [4]\n", " 2: IntConstant\n" }, |
| 388 | { " 3: IntConstant [4]\n", " 3: IntConstant\n" |
| 389 | " 7: IntConstant [5]\n" }, |
| 390 | { " 4: Sub(2, 3) [5]\n", removed }, |
| 391 | { " 5: Return(4)\n", " 5: Return(7)\n" } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 392 | }; |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 393 | std::string expected_after_cf = Patch(expected_before, expected_cf_diff); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 394 | |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 395 | // Check the value of the computed constant. |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 396 | auto check_after_cf = [](HGraph* graph) { |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 397 | HInstruction* inst = graph->GetBlocks()[1]->GetFirstInstruction()->InputAt(0); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 398 | ASSERT_TRUE(inst->IsIntConstant()); |
| 399 | ASSERT_EQ(inst->AsIntConstant()->GetValue(), 1); |
| 400 | }; |
| 401 | |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 402 | // Expected difference after dead code elimination. |
| 403 | diff_t expected_dce_diff = { |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 404 | { " 2: IntConstant\n", removed }, |
| 405 | { " 3: IntConstant\n", removed } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 406 | }; |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 407 | std::string expected_after_dce = Patch(expected_after_cf, expected_dce_diff); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 408 | |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 409 | TestCode(data, |
| 410 | expected_before, |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 411 | expected_after_cf, |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 412 | expected_after_dce, |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 413 | check_after_cf); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 414 | } |
| 415 | |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 416 | /** |
| 417 | * Tiny three-register-pair program exercising long constant folding |
| 418 | * on addition. |
| 419 | * |
| 420 | * 16-bit |
| 421 | * offset |
| 422 | * ------ |
| 423 | * (v0, v1) <- 1 0. const-wide/16 v0, #+1 |
| 424 | * (v2, v3) <- 2 2. const-wide/16 v2, #+2 |
| 425 | * (v4, v5) <- |
| 426 | * (v0, v1) + (v1, v2) 4. add-long v4, v0, v2 |
| 427 | * return (v4, v5) 6. return-wide v4 |
| 428 | */ |
Aart Bik | 96709f1 | 2015-10-28 17:49:07 -0700 | [diff] [blame] | 429 | TEST_F(ConstantFoldingTest, LongConstantFoldingOnAddition) { |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 430 | const std::vector<uint16_t> data = SIX_REGISTERS_CODE_ITEM( |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 431 | Instruction::CONST_WIDE_16 | 0 << 8, 1, |
| 432 | Instruction::CONST_WIDE_16 | 2 << 8, 2, |
| 433 | Instruction::ADD_LONG | 4 << 8, 0 | 2 << 8, |
| 434 | Instruction::RETURN_WIDE | 4 << 8); |
| 435 | |
| 436 | std::string expected_before = |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 437 | "BasicBlock 0, succ: 1\n" |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 438 | " 2: LongConstant [4]\n" |
| 439 | " 3: LongConstant [4]\n" |
| 440 | " 0: SuspendCheck\n" |
| 441 | " 1: Goto 1\n" |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 442 | "BasicBlock 1, pred: 0, succ: 2\n" |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 443 | " 4: Add(2, 3) [5]\n" |
| 444 | " 5: Return(4)\n" |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 445 | "BasicBlock 2, pred: 1\n" |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 446 | " 6: Exit\n"; |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 447 | |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 448 | // Expected difference after constant folding. |
| 449 | diff_t expected_cf_diff = { |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 450 | { " 2: LongConstant [4]\n", " 2: LongConstant\n" }, |
| 451 | { " 3: LongConstant [4]\n", " 3: LongConstant\n" |
| 452 | " 7: LongConstant [5]\n" }, |
| 453 | { " 4: Add(2, 3) [5]\n", removed }, |
| 454 | { " 5: Return(4)\n", " 5: Return(7)\n" } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 455 | }; |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 456 | std::string expected_after_cf = Patch(expected_before, expected_cf_diff); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 457 | |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 458 | // Check the value of the computed constant. |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 459 | auto check_after_cf = [](HGraph* graph) { |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 460 | HInstruction* inst = graph->GetBlocks()[1]->GetFirstInstruction()->InputAt(0); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 461 | ASSERT_TRUE(inst->IsLongConstant()); |
| 462 | ASSERT_EQ(inst->AsLongConstant()->GetValue(), 3); |
| 463 | }; |
| 464 | |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 465 | // Expected difference after dead code elimination. |
| 466 | diff_t expected_dce_diff = { |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 467 | { " 2: LongConstant\n", removed }, |
| 468 | { " 3: LongConstant\n", removed } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 469 | }; |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 470 | std::string expected_after_dce = Patch(expected_after_cf, expected_dce_diff); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 471 | |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 472 | TestCode(data, |
| 473 | expected_before, |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 474 | expected_after_cf, |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 475 | expected_after_dce, |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 476 | check_after_cf, |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 477 | DataType::Type::kInt64); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | /** |
| 481 | * Tiny three-register-pair program exercising long constant folding |
| 482 | * on subtraction. |
| 483 | * |
| 484 | * 16-bit |
| 485 | * offset |
| 486 | * ------ |
| 487 | * (v0, v1) <- 3 0. const-wide/16 v0, #+3 |
| 488 | * (v2, v3) <- 2 2. const-wide/16 v2, #+2 |
| 489 | * (v4, v5) <- |
| 490 | * (v0, v1) - (v1, v2) 4. sub-long v4, v0, v2 |
| 491 | * return (v4, v5) 6. return-wide v4 |
| 492 | */ |
Aart Bik | 96709f1 | 2015-10-28 17:49:07 -0700 | [diff] [blame] | 493 | TEST_F(ConstantFoldingTest, LongConstantFoldingOnSubtraction) { |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 494 | const std::vector<uint16_t> data = SIX_REGISTERS_CODE_ITEM( |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 495 | Instruction::CONST_WIDE_16 | 0 << 8, 3, |
| 496 | Instruction::CONST_WIDE_16 | 2 << 8, 2, |
| 497 | Instruction::SUB_LONG | 4 << 8, 0 | 2 << 8, |
| 498 | Instruction::RETURN_WIDE | 4 << 8); |
| 499 | |
| 500 | std::string expected_before = |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 501 | "BasicBlock 0, succ: 1\n" |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 502 | " 2: LongConstant [4]\n" |
| 503 | " 3: LongConstant [4]\n" |
| 504 | " 0: SuspendCheck\n" |
| 505 | " 1: Goto 1\n" |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 506 | "BasicBlock 1, pred: 0, succ: 2\n" |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 507 | " 4: Sub(2, 3) [5]\n" |
| 508 | " 5: Return(4)\n" |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 509 | "BasicBlock 2, pred: 1\n" |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 510 | " 6: Exit\n"; |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 511 | |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 512 | // Expected difference after constant folding. |
| 513 | diff_t expected_cf_diff = { |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 514 | { " 2: LongConstant [4]\n", " 2: LongConstant\n" }, |
| 515 | { " 3: LongConstant [4]\n", " 3: LongConstant\n" |
| 516 | " 7: LongConstant [5]\n" }, |
| 517 | { " 4: Sub(2, 3) [5]\n", removed }, |
| 518 | { " 5: Return(4)\n", " 5: Return(7)\n" } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 519 | }; |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 520 | std::string expected_after_cf = Patch(expected_before, expected_cf_diff); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 521 | |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 522 | // Check the value of the computed constant. |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 523 | auto check_after_cf = [](HGraph* graph) { |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 524 | HInstruction* inst = graph->GetBlocks()[1]->GetFirstInstruction()->InputAt(0); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 525 | ASSERT_TRUE(inst->IsLongConstant()); |
| 526 | ASSERT_EQ(inst->AsLongConstant()->GetValue(), 1); |
| 527 | }; |
| 528 | |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 529 | // Expected difference after dead code elimination. |
| 530 | diff_t expected_dce_diff = { |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 531 | { " 2: LongConstant\n", removed }, |
| 532 | { " 3: LongConstant\n", removed } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 533 | }; |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 534 | std::string expected_after_dce = Patch(expected_after_cf, expected_dce_diff); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 535 | |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 536 | TestCode(data, |
| 537 | expected_before, |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 538 | expected_after_cf, |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 539 | expected_after_dce, |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 540 | check_after_cf, |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 541 | DataType::Type::kInt64); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | /** |
| 545 | * Three-register program with jumps leading to the creation of many |
| 546 | * blocks. |
| 547 | * |
| 548 | * The intent of this test is to ensure that all constant expressions |
| 549 | * are actually evaluated at compile-time, thanks to the reverse |
| 550 | * (forward) post-order traversal of the the dominator tree. |
| 551 | * |
| 552 | * 16-bit |
| 553 | * offset |
| 554 | * ------ |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 555 | * v0 <- 1 0. const/4 v0, #+1 |
| 556 | * v1 <- 2 1. const/4 v1, #+2 |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 557 | * v2 <- v0 + v1 2. add-int v2, v0, v1 |
| 558 | * goto L2 4. goto +4 |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 559 | * L1: v1 <- v0 + 5 5. add-int/lit16 v1, v0, #+5 |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 560 | * goto L3 7. goto +4 |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 561 | * L2: v0 <- v2 + 4 8. add-int/lit16 v0, v2, #+4 |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 562 | * goto L1 10. goto +(-5) |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 563 | * L3: v2 <- v1 + 8 11. add-int/lit16 v2, v1, #+8 |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 564 | * return v2 13. return v2 |
| 565 | */ |
Aart Bik | 96709f1 | 2015-10-28 17:49:07 -0700 | [diff] [blame] | 566 | TEST_F(ConstantFoldingTest, IntConstantFoldingAndJumps) { |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 567 | const std::vector<uint16_t> data = THREE_REGISTERS_CODE_ITEM( |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 568 | Instruction::CONST_4 | 0 << 8 | 1 << 12, |
| 569 | Instruction::CONST_4 | 1 << 8 | 2 << 12, |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 570 | Instruction::ADD_INT | 2 << 8, 0 | 1 << 8, |
| 571 | Instruction::GOTO | 4 << 8, |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 572 | Instruction::ADD_INT_LIT16 | 1 << 8 | 0 << 12, 5, |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 573 | Instruction::GOTO | 4 << 8, |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 574 | Instruction::ADD_INT_LIT16 | 0 << 8 | 2 << 12, 4, |
Andreas Gampe | 58554b7 | 2015-10-20 21:08:52 -0700 | [diff] [blame] | 575 | static_cast<uint16_t>(Instruction::GOTO | 0xFFFFFFFB << 8), |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 576 | Instruction::ADD_INT_LIT16 | 2 << 8 | 1 << 12, 8, |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 577 | Instruction::RETURN | 2 << 8); |
| 578 | |
| 579 | std::string expected_before = |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 580 | "BasicBlock 0, succ: 1\n" |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 581 | " 2: IntConstant [4]\n" // v0 <- 1 |
| 582 | " 3: IntConstant [4]\n" // v1 <- 2 |
| 583 | " 6: IntConstant [7]\n" // const 5 |
| 584 | " 9: IntConstant [10]\n" // const 4 |
| 585 | " 12: IntConstant [13]\n" // const 8 |
| 586 | " 0: SuspendCheck\n" |
| 587 | " 1: Goto 1\n" |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 588 | "BasicBlock 1, pred: 0, succ: 3\n" |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 589 | " 4: Add(2, 3) [7]\n" // v2 <- v0 + v1 = 1 + 2 = 3 |
| 590 | " 5: Goto 3\n" // goto L2 |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 591 | "BasicBlock 2, pred: 3, succ: 4\n" // L1: |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 592 | " 10: Add(7, 9) [13]\n" // v1 <- v0 + 3 = 7 + 5 = 12 |
| 593 | " 11: Goto 4\n" // goto L3 |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 594 | "BasicBlock 3, pred: 1, succ: 2\n" // L2: |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 595 | " 7: Add(4, 6) [10]\n" // v0 <- v2 + 2 = 3 + 4 = 7 |
| 596 | " 8: Goto 2\n" // goto L1 |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 597 | "BasicBlock 4, pred: 2, succ: 5\n" // L3: |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 598 | " 13: Add(10, 12) [14]\n" // v2 <- v1 + 4 = 12 + 8 = 20 |
| 599 | " 14: Return(13)\n" // return v2 |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 600 | "BasicBlock 5, pred: 4\n" |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 601 | " 15: Exit\n"; |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 602 | |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 603 | // Expected difference after constant folding. |
| 604 | diff_t expected_cf_diff = { |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 605 | { " 2: IntConstant [4]\n", " 2: IntConstant\n" }, |
| 606 | { " 3: IntConstant [4]\n", " 3: IntConstant\n" }, |
| 607 | { " 6: IntConstant [7]\n", " 6: IntConstant\n" }, |
| 608 | { " 9: IntConstant [10]\n", " 9: IntConstant\n" }, |
| 609 | { " 12: IntConstant [13]\n", " 12: IntConstant\n" |
| 610 | " 16: IntConstant\n" |
| 611 | " 17: IntConstant\n" |
| 612 | " 18: IntConstant\n" |
| 613 | " 19: IntConstant [14]\n" }, |
| 614 | { " 4: Add(2, 3) [7]\n", removed }, |
| 615 | { " 10: Add(7, 9) [13]\n", removed }, |
| 616 | { " 7: Add(4, 6) [10]\n", removed }, |
| 617 | { " 13: Add(10, 12) [14]\n", removed }, |
| 618 | { " 14: Return(13)\n", " 14: Return(19)\n"} |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 619 | }; |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 620 | std::string expected_after_cf = Patch(expected_before, expected_cf_diff); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 621 | |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 622 | // Check the values of the computed constants. |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 623 | auto check_after_cf = [](HGraph* graph) { |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 624 | HInstruction* inst1 = graph->GetBlocks()[4]->GetFirstInstruction()->InputAt(0); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 625 | ASSERT_TRUE(inst1->IsIntConstant()); |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 626 | ASSERT_EQ(inst1->AsIntConstant()->GetValue(), 20); |
| 627 | HInstruction* inst2 = inst1->GetPrevious(); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 628 | ASSERT_TRUE(inst2->IsIntConstant()); |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 629 | ASSERT_EQ(inst2->AsIntConstant()->GetValue(), 12); |
| 630 | HInstruction* inst3 = inst2->GetPrevious(); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 631 | ASSERT_TRUE(inst3->IsIntConstant()); |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 632 | ASSERT_EQ(inst3->AsIntConstant()->GetValue(), 7); |
| 633 | HInstruction* inst4 = inst3->GetPrevious(); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 634 | ASSERT_TRUE(inst4->IsIntConstant()); |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 635 | ASSERT_EQ(inst4->AsIntConstant()->GetValue(), 3); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 636 | }; |
| 637 | |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 638 | // Expected difference after dead code elimination. |
David Brazdil | 1c533c1 | 2015-04-24 17:04:38 +0100 | [diff] [blame] | 639 | std::string expected_after_dce = |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 640 | "BasicBlock 0, succ: 1\n" |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 641 | " 19: IntConstant [14]\n" |
| 642 | " 0: SuspendCheck\n" |
| 643 | " 1: Goto 1\n" |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 644 | "BasicBlock 1, pred: 0, succ: 5\n" |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 645 | " 14: Return(19)\n" |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 646 | "BasicBlock 5, pred: 1\n" |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 647 | " 15: Exit\n"; |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 648 | |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 649 | TestCode(data, |
| 650 | expected_before, |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 651 | expected_after_cf, |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 652 | expected_after_dce, |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 653 | check_after_cf); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 654 | } |
| 655 | |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 656 | /** |
| 657 | * Three-register program with a constant (static) condition. |
| 658 | * |
| 659 | * 16-bit |
| 660 | * offset |
| 661 | * ------ |
| 662 | * v1 <- 1 0. const/4 v1, #+1 |
| 663 | * v0 <- 0 1. const/4 v0, #+0 |
| 664 | * if v1 >= 0 goto L1 2. if-gez v1, +3 |
| 665 | * v0 <- v1 4. move v0, v1 |
| 666 | * L1: v2 <- v0 + v1 5. add-int v2, v0, v1 |
| 667 | * return-void 7. return |
| 668 | */ |
Aart Bik | 96709f1 | 2015-10-28 17:49:07 -0700 | [diff] [blame] | 669 | TEST_F(ConstantFoldingTest, ConstantCondition) { |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 670 | const std::vector<uint16_t> data = THREE_REGISTERS_CODE_ITEM( |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 671 | Instruction::CONST_4 | 1 << 8 | 1 << 12, |
| 672 | Instruction::CONST_4 | 0 << 8 | 0 << 12, |
| 673 | Instruction::IF_GEZ | 1 << 8, 3, |
| 674 | Instruction::MOVE | 0 << 8 | 1 << 12, |
| 675 | Instruction::ADD_INT | 2 << 8, 0 | 1 << 8, |
| 676 | Instruction::RETURN_VOID); |
| 677 | |
| 678 | std::string expected_before = |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 679 | "BasicBlock 0, succ: 1\n" |
| 680 | " 3: IntConstant [9, 8, 5]\n" |
| 681 | " 4: IntConstant [8, 5]\n" |
| 682 | " 1: SuspendCheck\n" |
| 683 | " 2: Goto 1\n" |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 684 | "BasicBlock 1, pred: 0, succ: 5, 2\n" |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 685 | " 5: GreaterThanOrEqual(3, 4) [6]\n" |
| 686 | " 6: If(5)\n" |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 687 | "BasicBlock 2, pred: 1, succ: 3\n" |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 688 | " 7: Goto 3\n" |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 689 | "BasicBlock 3, pred: 5, 2, succ: 4\n" |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 690 | " 8: Phi(4, 3) [9]\n" |
| 691 | " 9: Add(8, 3)\n" |
| 692 | " 10: ReturnVoid\n" |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 693 | "BasicBlock 4, pred: 3\n" |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 694 | " 11: Exit\n" |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 695 | "BasicBlock 5, pred: 1, succ: 3\n" |
| 696 | " 0: Goto 3\n"; |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 697 | |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 698 | // Expected difference after constant folding. |
| 699 | diff_t expected_cf_diff = { |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 700 | { " 3: IntConstant [9, 8, 5]\n", " 3: IntConstant [6, 9, 8]\n" }, |
| 701 | { " 4: IntConstant [8, 5]\n", " 4: IntConstant [8]\n" }, |
| 702 | { " 5: GreaterThanOrEqual(3, 4) [6]\n", removed }, |
| 703 | { " 6: If(5)\n", " 6: If(3)\n" } |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 704 | }; |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 705 | std::string expected_after_cf = Patch(expected_before, expected_cf_diff); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 706 | |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 707 | // Check the values of the computed constants. |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 708 | auto check_after_cf = [](HGraph* graph) { |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 709 | HInstruction* inst = graph->GetBlocks()[1]->GetFirstInstruction()->InputAt(0); |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 710 | ASSERT_TRUE(inst->IsIntConstant()); |
| 711 | ASSERT_EQ(inst->AsIntConstant()->GetValue(), 1); |
| 712 | }; |
| 713 | |
David Brazdil | 1c533c1 | 2015-04-24 17:04:38 +0100 | [diff] [blame] | 714 | // Expected graph after dead code elimination. |
| 715 | std::string expected_after_dce = |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 716 | "BasicBlock 0, succ: 1\n" |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 717 | " 1: SuspendCheck\n" |
| 718 | " 2: Goto 1\n" |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 719 | "BasicBlock 1, pred: 0, succ: 4\n" |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 720 | " 10: ReturnVoid\n" |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 721 | "BasicBlock 4, pred: 1\n" |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 722 | " 11: Exit\n"; |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 723 | |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 724 | TestCode(data, |
| 725 | expected_before, |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 726 | expected_after_cf, |
Roland Levillain | 9344568 | 2014-10-06 19:24:02 +0100 | [diff] [blame] | 727 | expected_after_dce, |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 728 | check_after_cf); |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 729 | } |
| 730 | |
Aart Bik | 96709f1 | 2015-10-28 17:49:07 -0700 | [diff] [blame] | 731 | /** |
| 732 | * Unsigned comparisons with zero. Since these instructions are not present |
| 733 | * in the bytecode, we need to set up the graph explicitly. |
| 734 | */ |
| 735 | TEST_F(ConstantFoldingTest, UnsignedComparisonsWithZero) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 736 | graph_ = CreateGraph(); |
| 737 | HBasicBlock* entry_block = new (GetAllocator()) HBasicBlock(graph_); |
Aart Bik | 96709f1 | 2015-10-28 17:49:07 -0700 | [diff] [blame] | 738 | graph_->AddBlock(entry_block); |
| 739 | graph_->SetEntryBlock(entry_block); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 740 | HBasicBlock* block = new (GetAllocator()) HBasicBlock(graph_); |
Aart Bik | 96709f1 | 2015-10-28 17:49:07 -0700 | [diff] [blame] | 741 | graph_->AddBlock(block); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 742 | HBasicBlock* exit_block = new (GetAllocator()) HBasicBlock(graph_); |
Aart Bik | 96709f1 | 2015-10-28 17:49:07 -0700 | [diff] [blame] | 743 | graph_->AddBlock(exit_block); |
| 744 | graph_->SetExitBlock(exit_block); |
| 745 | entry_block->AddSuccessor(block); |
| 746 | block->AddSuccessor(exit_block); |
| 747 | |
| 748 | // Make various unsigned comparisons with zero against a parameter. |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 749 | HInstruction* parameter = new (GetAllocator()) HParameterValue( |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 750 | graph_->GetDexFile(), dex::TypeIndex(0), 0, DataType::Type::kInt32, true); |
Aart Bik | 96709f1 | 2015-10-28 17:49:07 -0700 | [diff] [blame] | 751 | entry_block->AddInstruction(parameter); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 752 | entry_block->AddInstruction(new (GetAllocator()) HGoto()); |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 753 | |
Aart Bik | 96709f1 | 2015-10-28 17:49:07 -0700 | [diff] [blame] | 754 | HInstruction* zero = graph_->GetIntConstant(0); |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 755 | |
Aart Bik | 96709f1 | 2015-10-28 17:49:07 -0700 | [diff] [blame] | 756 | HInstruction* last; |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 757 | block->AddInstruction(last = new (GetAllocator()) HAbove(zero, parameter)); |
| 758 | block->AddInstruction(new (GetAllocator()) HSelect(last, parameter, parameter, 0)); |
| 759 | block->AddInstruction(last = new (GetAllocator()) HAbove(parameter, zero)); |
| 760 | block->AddInstruction(new (GetAllocator()) HSelect(last, parameter, parameter, 0)); |
| 761 | block->AddInstruction(last = new (GetAllocator()) HAboveOrEqual(zero, parameter)); |
| 762 | block->AddInstruction(new (GetAllocator()) HSelect(last, parameter, parameter, 0)); |
| 763 | block->AddInstruction(last = new (GetAllocator()) HAboveOrEqual(parameter, zero)); |
| 764 | block->AddInstruction(new (GetAllocator()) HSelect(last, parameter, parameter, 0)); |
| 765 | block->AddInstruction(last = new (GetAllocator()) HBelow(zero, parameter)); |
| 766 | block->AddInstruction(new (GetAllocator()) HSelect(last, parameter, parameter, 0)); |
| 767 | block->AddInstruction(last = new (GetAllocator()) HBelow(parameter, zero)); |
| 768 | block->AddInstruction(new (GetAllocator()) HSelect(last, parameter, parameter, 0)); |
| 769 | block->AddInstruction(last = new (GetAllocator()) HBelowOrEqual(zero, parameter)); |
| 770 | block->AddInstruction(new (GetAllocator()) HSelect(last, parameter, parameter, 0)); |
| 771 | block->AddInstruction(last = new (GetAllocator()) HBelowOrEqual(parameter, zero)); |
| 772 | block->AddInstruction(new (GetAllocator()) HSelect(last, parameter, parameter, 0)); |
| 773 | block->AddInstruction(new (GetAllocator()) HReturn(zero)); |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 774 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 775 | exit_block->AddInstruction(new (GetAllocator()) HExit()); |
Aart Bik | 96709f1 | 2015-10-28 17:49:07 -0700 | [diff] [blame] | 776 | |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 777 | graph_->BuildDominatorTree(); |
| 778 | |
Aart Bik | 96709f1 | 2015-10-28 17:49:07 -0700 | [diff] [blame] | 779 | const std::string expected_before = |
| 780 | "BasicBlock 0, succ: 1\n" |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 781 | " 0: ParameterValue [18, 18, 17, 16, 16, 15, 14, 14, 13, 12, 12, 11, 10, 10, 9, " |
| 782 | "8, 8, 7, 6, 6, 5, 4, 4, 3]\n" |
| 783 | " 2: IntConstant [19, 17, 15, 13, 11, 9, 7, 5, 3]\n" |
| 784 | " 1: Goto 1\n" |
Aart Bik | 96709f1 | 2015-10-28 17:49:07 -0700 | [diff] [blame] | 785 | "BasicBlock 1, pred: 0, succ: 2\n" |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 786 | " 3: Above(2, 0) [4]\n" |
| 787 | " 4: Select(0, 0, 3)\n" |
| 788 | " 5: Above(0, 2) [6]\n" |
| 789 | " 6: Select(0, 0, 5)\n" |
| 790 | " 7: AboveOrEqual(2, 0) [8]\n" |
| 791 | " 8: Select(0, 0, 7)\n" |
| 792 | " 9: AboveOrEqual(0, 2) [10]\n" |
| 793 | " 10: Select(0, 0, 9)\n" |
| 794 | " 11: Below(2, 0) [12]\n" |
| 795 | " 12: Select(0, 0, 11)\n" |
| 796 | " 13: Below(0, 2) [14]\n" |
| 797 | " 14: Select(0, 0, 13)\n" |
| 798 | " 15: BelowOrEqual(2, 0) [16]\n" |
| 799 | " 16: Select(0, 0, 15)\n" |
| 800 | " 17: BelowOrEqual(0, 2) [18]\n" |
| 801 | " 18: Select(0, 0, 17)\n" |
| 802 | " 19: Return(2)\n" |
Aart Bik | 96709f1 | 2015-10-28 17:49:07 -0700 | [diff] [blame] | 803 | "BasicBlock 2, pred: 1\n" |
| 804 | " 20: Exit\n"; |
| 805 | |
| 806 | const std::string expected_after_cf = |
| 807 | "BasicBlock 0, succ: 1\n" |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 808 | " 0: ParameterValue [18, 18, 17, 16, 16, 14, 14, 12, 12, 11, 10, 10, " |
| 809 | "8, 8, 7, 6, 6, 5, 4, 4]\n" |
| 810 | " 2: IntConstant [14, 4, 19, 17, 11, 7, 5]\n" |
| 811 | " 21: IntConstant [16, 10]\n" |
| 812 | " 1: Goto 1\n" |
Aart Bik | 96709f1 | 2015-10-28 17:49:07 -0700 | [diff] [blame] | 813 | "BasicBlock 1, pred: 0, succ: 2\n" |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 814 | " 4: Select(0, 0, 2)\n" |
| 815 | " 5: Above(0, 2) [6]\n" |
| 816 | " 6: Select(0, 0, 5)\n" |
| 817 | " 7: AboveOrEqual(2, 0) [8]\n" |
| 818 | " 8: Select(0, 0, 7)\n" |
| 819 | " 10: Select(0, 0, 21)\n" |
| 820 | " 11: Below(2, 0) [12]\n" |
| 821 | " 12: Select(0, 0, 11)\n" |
| 822 | " 14: Select(0, 0, 2)\n" |
| 823 | " 16: Select(0, 0, 21)\n" |
| 824 | " 17: BelowOrEqual(0, 2) [18]\n" |
| 825 | " 18: Select(0, 0, 17)\n" |
| 826 | " 19: Return(2)\n" |
Aart Bik | 96709f1 | 2015-10-28 17:49:07 -0700 | [diff] [blame] | 827 | "BasicBlock 2, pred: 1\n" |
| 828 | " 20: Exit\n"; |
| 829 | |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 830 | const std::string expected_after_dce = |
| 831 | "BasicBlock 0, succ: 1\n" |
| 832 | " 0: ParameterValue\n" |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 833 | " 2: IntConstant [19]\n" |
| 834 | " 1: Goto 1\n" |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 835 | "BasicBlock 1, pred: 0, succ: 2\n" |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 836 | " 19: Return(2)\n" |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 837 | "BasicBlock 2, pred: 1\n" |
| 838 | " 20: Exit\n"; |
Aart Bik | 96709f1 | 2015-10-28 17:49:07 -0700 | [diff] [blame] | 839 | |
| 840 | auto check_after_cf = [](HGraph* graph) { |
| 841 | CHECK(graph != nullptr); |
| 842 | }; |
| 843 | |
| 844 | TestCodeOnReadyGraph(expected_before, |
| 845 | expected_after_cf, |
| 846 | expected_after_dce, |
| 847 | check_after_cf); |
| 848 | } |
| 849 | |
Roland Levillain | 556c3d1 | 2014-09-18 15:25:07 +0100 | [diff] [blame] | 850 | } // namespace art |