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