Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +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 | |
| 17 | #include "builder.h" |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 18 | #include "code_generator.h" |
Nicolas Geoffray | 8a16d97 | 2014-09-11 10:30:02 +0100 | [diff] [blame] | 19 | #include "code_generator_x86.h" |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 20 | #include "dex_file.h" |
| 21 | #include "dex_instruction.h" |
| 22 | #include "nodes.h" |
| 23 | #include "optimizing_unit_test.h" |
| 24 | #include "ssa_liveness_analysis.h" |
| 25 | #include "utils/arena_allocator.h" |
| 26 | |
| 27 | #include "gtest/gtest.h" |
| 28 | |
| 29 | namespace art { |
| 30 | |
Nicolas Geoffray | 26066f2 | 2014-06-03 10:36:16 +0000 | [diff] [blame] | 31 | static void DumpBitVector(BitVector* vector, |
| 32 | std::ostream& buffer, |
| 33 | size_t count, |
| 34 | const char* prefix) { |
| 35 | buffer << prefix; |
| 36 | buffer << '('; |
| 37 | for (size_t i = 0; i < count; ++i) { |
| 38 | buffer << vector->IsBitSet(i); |
| 39 | } |
| 40 | buffer << ")\n"; |
| 41 | } |
| 42 | |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 43 | static void TestCode(const uint16_t* data, const char* expected) { |
| 44 | ArenaPool pool; |
| 45 | ArenaAllocator allocator(&pool); |
| 46 | HGraphBuilder builder(&allocator); |
| 47 | const DexFile::CodeItem* item = reinterpret_cast<const DexFile::CodeItem*>(data); |
| 48 | HGraph* graph = builder.BuildGraph(*item); |
| 49 | ASSERT_NE(graph, nullptr); |
| 50 | graph->BuildDominatorTree(); |
| 51 | graph->TransformToSSA(); |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 52 | graph->FindNaturalLoops(); |
Nicolas Geoffray | 8a16d97 | 2014-09-11 10:30:02 +0100 | [diff] [blame] | 53 | x86::CodeGeneratorX86 codegen(graph); |
| 54 | SsaLivenessAnalysis liveness(*graph, &codegen); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 55 | liveness.Analyze(); |
| 56 | |
| 57 | std::ostringstream buffer; |
| 58 | for (HInsertionOrderIterator it(*graph); !it.Done(); it.Advance()) { |
| 59 | HBasicBlock* block = it.Current(); |
| 60 | buffer << "Block " << block->GetBlockId() << std::endl; |
Nicolas Geoffray | 26066f2 | 2014-06-03 10:36:16 +0000 | [diff] [blame] | 61 | size_t ssa_values = liveness.GetNumberOfSsaValues(); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 62 | BitVector* live_in = liveness.GetLiveInSet(*block); |
Nicolas Geoffray | 26066f2 | 2014-06-03 10:36:16 +0000 | [diff] [blame] | 63 | DumpBitVector(live_in, buffer, ssa_values, " live in: "); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 64 | BitVector* live_out = liveness.GetLiveOutSet(*block); |
Nicolas Geoffray | 26066f2 | 2014-06-03 10:36:16 +0000 | [diff] [blame] | 65 | DumpBitVector(live_out, buffer, ssa_values, " live out: "); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 66 | BitVector* kill = liveness.GetKillSet(*block); |
Nicolas Geoffray | 26066f2 | 2014-06-03 10:36:16 +0000 | [diff] [blame] | 67 | DumpBitVector(kill, buffer, ssa_values, " kill: "); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 68 | } |
| 69 | ASSERT_STREQ(expected, buffer.str().c_str()); |
| 70 | } |
| 71 | |
| 72 | TEST(LivenessTest, CFG1) { |
| 73 | const char* expected = |
| 74 | "Block 0\n" |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 75 | " live in: (0)\n" |
| 76 | " live out: (0)\n" |
| 77 | " kill: (1)\n" |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 78 | "Block 1\n" |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 79 | " live in: (0)\n" |
| 80 | " live out: (0)\n" |
| 81 | " kill: (0)\n" |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 82 | "Block 2\n" |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 83 | " live in: (0)\n" |
| 84 | " live out: (0)\n" |
| 85 | " kill: (0)\n"; |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 86 | |
| 87 | // Constant is not used. |
| 88 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 89 | Instruction::CONST_4 | 0 | 0, |
| 90 | Instruction::RETURN_VOID); |
| 91 | |
| 92 | TestCode(data, expected); |
| 93 | } |
| 94 | |
| 95 | TEST(LivenessTest, CFG2) { |
| 96 | const char* expected = |
| 97 | "Block 0\n" |
Nicolas Geoffray | 26066f2 | 2014-06-03 10:36:16 +0000 | [diff] [blame] | 98 | " live in: (0)\n" |
| 99 | " live out: (1)\n" |
| 100 | " kill: (1)\n" |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 101 | "Block 1\n" |
Nicolas Geoffray | 26066f2 | 2014-06-03 10:36:16 +0000 | [diff] [blame] | 102 | " live in: (1)\n" |
| 103 | " live out: (0)\n" |
| 104 | " kill: (0)\n" |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 105 | "Block 2\n" |
Nicolas Geoffray | 26066f2 | 2014-06-03 10:36:16 +0000 | [diff] [blame] | 106 | " live in: (0)\n" |
| 107 | " live out: (0)\n" |
| 108 | " kill: (0)\n"; |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 109 | |
| 110 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 111 | Instruction::CONST_4 | 0 | 0, |
| 112 | Instruction::RETURN); |
| 113 | |
| 114 | TestCode(data, expected); |
| 115 | } |
| 116 | |
| 117 | TEST(LivenessTest, CFG3) { |
| 118 | const char* expected = |
| 119 | "Block 0\n" // entry block |
Nicolas Geoffray | 26066f2 | 2014-06-03 10:36:16 +0000 | [diff] [blame] | 120 | " live in: (000)\n" |
| 121 | " live out: (110)\n" |
| 122 | " kill: (110)\n" |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 123 | "Block 1\n" // block with add |
Nicolas Geoffray | 26066f2 | 2014-06-03 10:36:16 +0000 | [diff] [blame] | 124 | " live in: (110)\n" |
| 125 | " live out: (001)\n" |
| 126 | " kill: (001)\n" |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 127 | "Block 2\n" // block with return |
Nicolas Geoffray | 26066f2 | 2014-06-03 10:36:16 +0000 | [diff] [blame] | 128 | " live in: (001)\n" |
| 129 | " live out: (000)\n" |
| 130 | " kill: (000)\n" |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 131 | "Block 3\n" // exit block |
Nicolas Geoffray | 26066f2 | 2014-06-03 10:36:16 +0000 | [diff] [blame] | 132 | " live in: (000)\n" |
| 133 | " live out: (000)\n" |
| 134 | " kill: (000)\n"; |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 135 | |
| 136 | const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( |
| 137 | Instruction::CONST_4 | 3 << 12 | 0, |
| 138 | Instruction::CONST_4 | 4 << 12 | 1 << 8, |
| 139 | Instruction::ADD_INT_2ADDR | 1 << 12, |
| 140 | Instruction::GOTO | 0x100, |
| 141 | Instruction::RETURN); |
| 142 | |
| 143 | TestCode(data, expected); |
| 144 | } |
| 145 | |
| 146 | TEST(LivenessTest, CFG4) { |
| 147 | // var a; |
| 148 | // if (0 == 0) { |
| 149 | // a = 5; |
| 150 | // } else { |
| 151 | // a = 4; |
| 152 | // } |
| 153 | // return a; |
| 154 | // |
| 155 | // Bitsets are made of: |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 156 | // (constant0, constant4, constant5, phi) |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 157 | const char* expected = |
| 158 | "Block 0\n" // entry block |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 159 | " live in: (0000)\n" |
| 160 | " live out: (1110)\n" |
| 161 | " kill: (1110)\n" |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 162 | "Block 1\n" // block with if |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 163 | " live in: (1110)\n" |
| 164 | " live out: (0110)\n" |
| 165 | " kill: (0000)\n" |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 166 | "Block 2\n" // else block |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 167 | " live in: (0100)\n" |
| 168 | " live out: (0000)\n" |
| 169 | " kill: (0000)\n" |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 170 | "Block 3\n" // then block |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 171 | " live in: (0010)\n" |
| 172 | " live out: (0000)\n" |
| 173 | " kill: (0000)\n" |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 174 | "Block 4\n" // return block |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 175 | " live in: (0000)\n" |
| 176 | " live out: (0000)\n" |
| 177 | " kill: (0001)\n" |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 178 | "Block 5\n" // exit block |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 179 | " live in: (0000)\n" |
| 180 | " live out: (0000)\n" |
| 181 | " kill: (0000)\n"; |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 182 | |
| 183 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 184 | Instruction::CONST_4 | 0 | 0, |
| 185 | Instruction::IF_EQ, 4, |
| 186 | Instruction::CONST_4 | 4 << 12 | 0, |
| 187 | Instruction::GOTO | 0x200, |
| 188 | Instruction::CONST_4 | 5 << 12 | 0, |
| 189 | Instruction::RETURN | 0 << 8); |
| 190 | |
| 191 | TestCode(data, expected); |
| 192 | } |
| 193 | |
| 194 | TEST(LivenessTest, CFG5) { |
| 195 | // var a = 0; |
| 196 | // if (0 == 0) { |
| 197 | // } else { |
| 198 | // a = 4; |
| 199 | // } |
| 200 | // return a; |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 201 | // |
| 202 | // Bitsets are made of: |
| 203 | // (constant0, constant4, phi) |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 204 | const char* expected = |
| 205 | "Block 0\n" // entry block |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 206 | " live in: (000)\n" |
| 207 | " live out: (110)\n" |
| 208 | " kill: (110)\n" |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 209 | "Block 1\n" // block with if |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 210 | " live in: (110)\n" |
| 211 | " live out: (110)\n" |
| 212 | " kill: (000)\n" |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 213 | "Block 2\n" // else block |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 214 | " live in: (010)\n" |
| 215 | " live out: (000)\n" |
| 216 | " kill: (000)\n" |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 217 | "Block 3\n" // return block |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 218 | " live in: (000)\n" |
| 219 | " live out: (000)\n" |
| 220 | " kill: (001)\n" |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 221 | "Block 4\n" // exit block |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 222 | " live in: (000)\n" |
| 223 | " live out: (000)\n" |
| 224 | " kill: (000)\n" |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 225 | "Block 5\n" // block to avoid critical edge. Predecessor is 1, successor is 3. |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 226 | " live in: (100)\n" |
| 227 | " live out: (000)\n" |
| 228 | " kill: (000)\n"; |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 229 | |
| 230 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 231 | Instruction::CONST_4 | 0 | 0, |
| 232 | Instruction::IF_EQ, 3, |
| 233 | Instruction::CONST_4 | 4 << 12 | 0, |
| 234 | Instruction::RETURN | 0 << 8); |
| 235 | |
| 236 | TestCode(data, expected); |
| 237 | } |
| 238 | |
| 239 | TEST(LivenessTest, Loop1) { |
| 240 | // Simple loop with one preheader and one back edge. |
| 241 | // var a = 0; |
| 242 | // while (a == a) { |
| 243 | // a = 4; |
| 244 | // } |
| 245 | // return; |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 246 | // Bitsets are made of: |
| 247 | // (constant0, constant4, phi) |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 248 | const char* expected = |
| 249 | "Block 0\n" // entry block |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 250 | " live in: (000)\n" |
| 251 | " live out: (110)\n" |
| 252 | " kill: (110)\n" |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 253 | "Block 1\n" // pre header |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 254 | " live in: (110)\n" |
| 255 | " live out: (010)\n" |
| 256 | " kill: (000)\n" |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 257 | "Block 2\n" // loop header |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 258 | " live in: (010)\n" |
| 259 | " live out: (010)\n" |
| 260 | " kill: (001)\n" |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 261 | "Block 3\n" // back edge |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 262 | " live in: (010)\n" |
| 263 | " live out: (010)\n" |
| 264 | " kill: (000)\n" |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 265 | "Block 4\n" // return block |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 266 | " live in: (000)\n" |
| 267 | " live out: (000)\n" |
| 268 | " kill: (000)\n" |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 269 | "Block 5\n" // exit block |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 270 | " live in: (000)\n" |
| 271 | " live out: (000)\n" |
| 272 | " kill: (000)\n"; |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 273 | |
| 274 | |
| 275 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 276 | Instruction::CONST_4 | 0 | 0, |
| 277 | Instruction::IF_EQ, 4, |
| 278 | Instruction::CONST_4 | 4 << 12 | 0, |
| 279 | Instruction::GOTO | 0xFD00, |
| 280 | Instruction::RETURN_VOID); |
| 281 | |
| 282 | TestCode(data, expected); |
| 283 | } |
| 284 | |
| 285 | TEST(LivenessTest, Loop3) { |
| 286 | // Test that the returned value stays live in a preceding loop. |
| 287 | // var a = 0; |
| 288 | // while (a == a) { |
| 289 | // a = 4; |
| 290 | // } |
| 291 | // return 5; |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 292 | // Bitsets are made of: |
| 293 | // (constant0, constant4, constant5, phi) |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 294 | const char* expected = |
| 295 | "Block 0\n" |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 296 | " live in: (0000)\n" |
| 297 | " live out: (1110)\n" |
| 298 | " kill: (1110)\n" |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 299 | "Block 1\n" |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 300 | " live in: (1110)\n" |
| 301 | " live out: (0110)\n" |
| 302 | " kill: (0000)\n" |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 303 | "Block 2\n" // loop header |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 304 | " live in: (0110)\n" |
| 305 | " live out: (0110)\n" |
| 306 | " kill: (0001)\n" |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 307 | "Block 3\n" // back edge |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 308 | " live in: (0110)\n" |
| 309 | " live out: (0110)\n" |
| 310 | " kill: (0000)\n" |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 311 | "Block 4\n" // return block |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 312 | " live in: (0010)\n" |
| 313 | " live out: (0000)\n" |
| 314 | " kill: (0000)\n" |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 315 | "Block 5\n" // exit block |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 316 | " live in: (0000)\n" |
| 317 | " live out: (0000)\n" |
| 318 | " kill: (0000)\n"; |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 319 | |
| 320 | const uint16_t data[] = TWO_REGISTERS_CODE_ITEM( |
| 321 | Instruction::CONST_4 | 0 | 0, |
| 322 | Instruction::IF_EQ, 4, |
| 323 | Instruction::CONST_4 | 4 << 12 | 0, |
| 324 | Instruction::GOTO | 0xFD00, |
| 325 | Instruction::CONST_4 | 5 << 12 | 1 << 8, |
| 326 | Instruction::RETURN | 1 << 8); |
| 327 | |
| 328 | TestCode(data, expected); |
| 329 | } |
| 330 | |
| 331 | |
| 332 | TEST(LivenessTest, Loop4) { |
| 333 | // Make sure we support a preheader of a loop not being the first predecessor |
| 334 | // in the predecessor list of the header. |
| 335 | // var a = 0; |
| 336 | // while (a == a) { |
| 337 | // a = 4; |
| 338 | // } |
| 339 | // return a; |
| 340 | // Bitsets are made of: |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 341 | // (constant0, constant4, phi) |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 342 | const char* expected = |
| 343 | "Block 0\n" |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 344 | " live in: (000)\n" |
| 345 | " live out: (110)\n" |
| 346 | " kill: (110)\n" |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 347 | "Block 1\n" |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 348 | " live in: (110)\n" |
| 349 | " live out: (110)\n" |
| 350 | " kill: (000)\n" |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 351 | "Block 2\n" // loop header |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 352 | " live in: (010)\n" |
| 353 | " live out: (011)\n" |
| 354 | " kill: (001)\n" |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 355 | "Block 3\n" // back edge |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 356 | " live in: (010)\n" |
| 357 | " live out: (010)\n" |
| 358 | " kill: (000)\n" |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 359 | "Block 4\n" // pre loop header |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 360 | " live in: (110)\n" |
| 361 | " live out: (010)\n" |
| 362 | " kill: (000)\n" |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 363 | "Block 5\n" // return block |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 364 | " live in: (001)\n" |
| 365 | " live out: (000)\n" |
| 366 | " kill: (000)\n" |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 367 | "Block 6\n" // exit block |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 368 | " live in: (000)\n" |
| 369 | " live out: (000)\n" |
| 370 | " kill: (000)\n"; |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 371 | |
| 372 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 373 | Instruction::CONST_4 | 0 | 0, |
| 374 | Instruction::GOTO | 0x500, |
| 375 | Instruction::IF_EQ, 5, |
| 376 | Instruction::CONST_4 | 4 << 12 | 0, |
| 377 | Instruction::GOTO | 0xFD00, |
| 378 | Instruction::GOTO | 0xFC00, |
| 379 | Instruction::RETURN | 0 << 8); |
| 380 | |
| 381 | TestCode(data, expected); |
| 382 | } |
| 383 | |
| 384 | TEST(LivenessTest, Loop5) { |
| 385 | // Make sure we create a preheader of a loop when a header originally has two |
| 386 | // incoming blocks and one back edge. |
| 387 | // Bitsets are made of: |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 388 | // (constant0, constant4, constant5, phi in block 8, phi in block 4) |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 389 | const char* expected = |
| 390 | "Block 0\n" |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 391 | " live in: (00000)\n" |
| 392 | " live out: (11100)\n" |
| 393 | " kill: (11100)\n" |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 394 | "Block 1\n" |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 395 | " live in: (11100)\n" |
| 396 | " live out: (01100)\n" |
| 397 | " kill: (00000)\n" |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 398 | "Block 2\n" |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 399 | " live in: (01000)\n" |
| 400 | " live out: (00000)\n" |
| 401 | " kill: (00000)\n" |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 402 | "Block 3\n" |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 403 | " live in: (00100)\n" |
| 404 | " live out: (00000)\n" |
| 405 | " kill: (00000)\n" |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 406 | "Block 4\n" // loop header |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 407 | " live in: (00000)\n" |
| 408 | " live out: (00001)\n" |
| 409 | " kill: (00001)\n" |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 410 | "Block 5\n" // back edge |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 411 | " live in: (00001)\n" |
| 412 | " live out: (00000)\n" |
| 413 | " kill: (00000)\n" |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 414 | "Block 6\n" // return block |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 415 | " live in: (00001)\n" |
| 416 | " live out: (00000)\n" |
| 417 | " kill: (00000)\n" |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 418 | "Block 7\n" // exit block |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 419 | " live in: (00000)\n" |
| 420 | " live out: (00000)\n" |
| 421 | " kill: (00000)\n" |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 422 | "Block 8\n" // synthesized pre header |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 423 | " live in: (00000)\n" |
| 424 | " live out: (00000)\n" |
| 425 | " kill: (00010)\n"; |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 426 | |
| 427 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 428 | Instruction::CONST_4 | 0 | 0, |
| 429 | Instruction::IF_EQ, 4, |
| 430 | Instruction::CONST_4 | 4 << 12 | 0, |
| 431 | Instruction::GOTO | 0x200, |
| 432 | Instruction::CONST_4 | 5 << 12 | 0, |
| 433 | Instruction::IF_EQ, 3, |
| 434 | Instruction::GOTO | 0xFE00, |
| 435 | Instruction::RETURN | 0 << 8); |
| 436 | |
| 437 | TestCode(data, expected); |
| 438 | } |
| 439 | |
| 440 | TEST(LivenessTest, Loop6) { |
| 441 | // Bitsets are made of: |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 442 | // (constant0, constant4, constant5, phi in block 2, phi in block 8) |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 443 | const char* expected = |
| 444 | "Block 0\n" |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 445 | " live in: (00000)\n" |
| 446 | " live out: (11100)\n" |
| 447 | " kill: (11100)\n" |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 448 | "Block 1\n" |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 449 | " live in: (11100)\n" |
| 450 | " live out: (01100)\n" |
| 451 | " kill: (00000)\n" |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 452 | "Block 2\n" // loop header |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 453 | " live in: (01100)\n" |
| 454 | " live out: (01110)\n" |
| 455 | " kill: (00010)\n" |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 456 | "Block 3\n" |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 457 | " live in: (01100)\n" |
| 458 | " live out: (01100)\n" |
| 459 | " kill: (00000)\n" |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 460 | "Block 4\n" // original back edge |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 461 | " live in: (01100)\n" |
| 462 | " live out: (01100)\n" |
| 463 | " kill: (00000)\n" |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 464 | "Block 5\n" // original back edge |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 465 | " live in: (01100)\n" |
| 466 | " live out: (01100)\n" |
| 467 | " kill: (00000)\n" |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 468 | "Block 6\n" // return block |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 469 | " live in: (00010)\n" |
| 470 | " live out: (00000)\n" |
| 471 | " kill: (00000)\n" |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 472 | "Block 7\n" // exit block |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 473 | " live in: (00000)\n" |
| 474 | " live out: (00000)\n" |
| 475 | " kill: (00000)\n" |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 476 | "Block 8\n" // synthesized back edge |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 477 | " live in: (01100)\n" |
| 478 | " live out: (01100)\n" |
| 479 | " kill: (00001)\n"; |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 480 | |
| 481 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 482 | Instruction::CONST_4 | 0 | 0, |
| 483 | Instruction::IF_EQ, 8, |
| 484 | Instruction::CONST_4 | 4 << 12 | 0, |
| 485 | Instruction::IF_EQ, 4, |
| 486 | Instruction::CONST_4 | 5 << 12 | 0, |
| 487 | Instruction::GOTO | 0xFA00, |
| 488 | Instruction::GOTO | 0xF900, |
| 489 | Instruction::RETURN | 0 << 8); |
| 490 | |
| 491 | TestCode(data, expected); |
| 492 | } |
| 493 | |
| 494 | |
| 495 | TEST(LivenessTest, Loop7) { |
| 496 | // Bitsets are made of: |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 497 | // (constant0, constant4, constant5, phi in block 2, phi in block 6) |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 498 | const char* expected = |
| 499 | "Block 0\n" |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 500 | " live in: (00000)\n" |
| 501 | " live out: (11100)\n" |
| 502 | " kill: (11100)\n" |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 503 | "Block 1\n" |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 504 | " live in: (11100)\n" |
| 505 | " live out: (01100)\n" |
| 506 | " kill: (00000)\n" |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 507 | "Block 2\n" // loop header |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 508 | " live in: (01100)\n" |
| 509 | " live out: (01110)\n" |
| 510 | " kill: (00010)\n" |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 511 | "Block 3\n" |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 512 | " live in: (01100)\n" |
| 513 | " live out: (01100)\n" |
| 514 | " kill: (00000)\n" |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 515 | "Block 4\n" // loop exit |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 516 | " live in: (00100)\n" |
| 517 | " live out: (00000)\n" |
| 518 | " kill: (00000)\n" |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 519 | "Block 5\n" // back edge |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 520 | " live in: (01100)\n" |
| 521 | " live out: (01100)\n" |
| 522 | " kill: (00000)\n" |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 523 | "Block 6\n" // return block |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 524 | " live in: (00000)\n" |
| 525 | " live out: (00000)\n" |
| 526 | " kill: (00001)\n" |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 527 | "Block 7\n" // exit block |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 528 | " live in: (00000)\n" |
| 529 | " live out: (00000)\n" |
| 530 | " kill: (00000)\n" |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 531 | "Block 8\n" // synthesized block to avoid critical edge. |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 532 | " live in: (00010)\n" |
| 533 | " live out: (00000)\n" |
| 534 | " kill: (00000)\n"; |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 535 | |
| 536 | const uint16_t data[] = ONE_REGISTER_CODE_ITEM( |
| 537 | Instruction::CONST_4 | 0 | 0, |
| 538 | Instruction::IF_EQ, 8, |
| 539 | Instruction::CONST_4 | 4 << 12 | 0, |
| 540 | Instruction::IF_EQ, 4, |
| 541 | Instruction::CONST_4 | 5 << 12 | 0, |
| 542 | Instruction::GOTO | 0x0200, |
| 543 | Instruction::GOTO | 0xF900, |
| 544 | Instruction::RETURN | 0 << 8); |
| 545 | |
| 546 | TestCode(data, expected); |
| 547 | } |
| 548 | |
| 549 | } // namespace art |