Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +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 | |
Mathieu Chartier | b666f48 | 2015-02-18 14:33:14 -0800 | [diff] [blame] | 17 | #include "base/arena_allocator.h" |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 18 | #include "builder.h" |
| 19 | #include "gvn.h" |
| 20 | #include "nodes.h" |
| 21 | #include "optimizing_unit_test.h" |
Nicolas Geoffray | 827eedb | 2015-01-26 15:18:36 +0000 | [diff] [blame] | 22 | #include "side_effects_analysis.h" |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 23 | |
| 24 | #include "gtest/gtest.h" |
| 25 | |
| 26 | namespace art { |
| 27 | |
| 28 | TEST(GVNTest, LocalFieldElimination) { |
| 29 | ArenaPool pool; |
| 30 | ArenaAllocator allocator(&pool); |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 31 | NullHandle<mirror::DexCache> dex_cache; |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 32 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 33 | HGraph* graph = CreateGraph(&allocator); |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 34 | HBasicBlock* entry = new (&allocator) HBasicBlock(graph); |
| 35 | graph->AddBlock(entry); |
| 36 | graph->SetEntryBlock(entry); |
Calin Juravle | 2c1ffc3 | 2015-10-12 15:01:58 +0100 | [diff] [blame^] | 37 | HInstruction* parameter = new (&allocator) HParameterValue(graph->GetDexFile(), |
| 38 | 0, |
| 39 | 0, |
| 40 | Primitive::kPrimNot); |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 41 | entry->AddInstruction(parameter); |
| 42 | |
| 43 | HBasicBlock* block = new (&allocator) HBasicBlock(graph); |
| 44 | graph->AddBlock(block); |
| 45 | entry->AddSuccessor(block); |
| 46 | |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 47 | block->AddInstruction(new (&allocator) HInstanceFieldGet(parameter, |
| 48 | Primitive::kPrimNot, |
| 49 | MemberOffset(42), |
| 50 | false, |
| 51 | kUnknownFieldIndex, |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 52 | graph->GetDexFile(), |
Calin Juravle | 154746b | 2015-10-06 15:46:54 +0100 | [diff] [blame] | 53 | dex_cache, |
| 54 | 0)); |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 55 | block->AddInstruction(new (&allocator) HInstanceFieldGet(parameter, |
| 56 | Primitive::kPrimNot, |
| 57 | MemberOffset(42), |
| 58 | false, |
| 59 | kUnknownFieldIndex, |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 60 | graph->GetDexFile(), |
Calin Juravle | 154746b | 2015-10-06 15:46:54 +0100 | [diff] [blame] | 61 | dex_cache, |
| 62 | 0)); |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 63 | HInstruction* to_remove = block->GetLastInstruction(); |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 64 | block->AddInstruction(new (&allocator) HInstanceFieldGet(parameter, |
| 65 | Primitive::kPrimNot, |
| 66 | MemberOffset(43), |
| 67 | false, |
| 68 | kUnknownFieldIndex, |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 69 | graph->GetDexFile(), |
Calin Juravle | 154746b | 2015-10-06 15:46:54 +0100 | [diff] [blame] | 70 | dex_cache, |
| 71 | 0)); |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 72 | HInstruction* different_offset = block->GetLastInstruction(); |
| 73 | // Kill the value. |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 74 | block->AddInstruction(new (&allocator) HInstanceFieldSet(parameter, |
| 75 | parameter, |
| 76 | Primitive::kPrimNot, |
| 77 | MemberOffset(42), |
| 78 | false, |
| 79 | kUnknownFieldIndex, |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 80 | graph->GetDexFile(), |
Calin Juravle | 154746b | 2015-10-06 15:46:54 +0100 | [diff] [blame] | 81 | dex_cache, |
| 82 | 0)); |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 83 | block->AddInstruction(new (&allocator) HInstanceFieldGet(parameter, |
| 84 | Primitive::kPrimNot, |
| 85 | MemberOffset(42), |
| 86 | false, |
| 87 | kUnknownFieldIndex, |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 88 | graph->GetDexFile(), |
Calin Juravle | 154746b | 2015-10-06 15:46:54 +0100 | [diff] [blame] | 89 | dex_cache, |
| 90 | 0)); |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 91 | HInstruction* use_after_kill = block->GetLastInstruction(); |
| 92 | block->AddInstruction(new (&allocator) HExit()); |
| 93 | |
| 94 | ASSERT_EQ(to_remove->GetBlock(), block); |
| 95 | ASSERT_EQ(different_offset->GetBlock(), block); |
| 96 | ASSERT_EQ(use_after_kill->GetBlock(), block); |
| 97 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 98 | graph->TryBuildingSsa(); |
Nicolas Geoffray | e6f1715 | 2015-01-26 15:13:47 +0000 | [diff] [blame] | 99 | SideEffectsAnalysis side_effects(graph); |
| 100 | side_effects.Run(); |
Nicolas Geoffray | 827eedb | 2015-01-26 15:18:36 +0000 | [diff] [blame] | 101 | GVNOptimization(graph, side_effects).Run(); |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 102 | |
| 103 | ASSERT_TRUE(to_remove->GetBlock() == nullptr); |
| 104 | ASSERT_EQ(different_offset->GetBlock(), block); |
| 105 | ASSERT_EQ(use_after_kill->GetBlock(), block); |
| 106 | } |
| 107 | |
| 108 | TEST(GVNTest, GlobalFieldElimination) { |
| 109 | ArenaPool pool; |
| 110 | ArenaAllocator allocator(&pool); |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 111 | NullHandle<mirror::DexCache> dex_cache; |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 112 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 113 | HGraph* graph = CreateGraph(&allocator); |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 114 | HBasicBlock* entry = new (&allocator) HBasicBlock(graph); |
| 115 | graph->AddBlock(entry); |
| 116 | graph->SetEntryBlock(entry); |
Calin Juravle | 2c1ffc3 | 2015-10-12 15:01:58 +0100 | [diff] [blame^] | 117 | HInstruction* parameter = new (&allocator) HParameterValue(graph->GetDexFile(), |
| 118 | 0, |
| 119 | 0, |
| 120 | Primitive::kPrimNot); |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 121 | entry->AddInstruction(parameter); |
| 122 | |
| 123 | HBasicBlock* block = new (&allocator) HBasicBlock(graph); |
| 124 | graph->AddBlock(block); |
| 125 | entry->AddSuccessor(block); |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 126 | block->AddInstruction(new (&allocator) HInstanceFieldGet(parameter, |
| 127 | Primitive::kPrimBoolean, |
| 128 | MemberOffset(42), |
| 129 | false, |
| 130 | kUnknownFieldIndex, |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 131 | graph->GetDexFile(), |
Calin Juravle | 154746b | 2015-10-06 15:46:54 +0100 | [diff] [blame] | 132 | dex_cache, |
| 133 | 0)); |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 134 | |
| 135 | block->AddInstruction(new (&allocator) HIf(block->GetLastInstruction())); |
| 136 | HBasicBlock* then = new (&allocator) HBasicBlock(graph); |
| 137 | HBasicBlock* else_ = new (&allocator) HBasicBlock(graph); |
| 138 | HBasicBlock* join = new (&allocator) HBasicBlock(graph); |
| 139 | graph->AddBlock(then); |
| 140 | graph->AddBlock(else_); |
| 141 | graph->AddBlock(join); |
| 142 | |
| 143 | block->AddSuccessor(then); |
| 144 | block->AddSuccessor(else_); |
| 145 | then->AddSuccessor(join); |
| 146 | else_->AddSuccessor(join); |
| 147 | |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 148 | then->AddInstruction(new (&allocator) HInstanceFieldGet(parameter, |
| 149 | Primitive::kPrimBoolean, |
| 150 | MemberOffset(42), |
| 151 | false, |
| 152 | kUnknownFieldIndex, |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 153 | graph->GetDexFile(), |
Calin Juravle | 154746b | 2015-10-06 15:46:54 +0100 | [diff] [blame] | 154 | dex_cache, |
| 155 | 0)); |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 156 | then->AddInstruction(new (&allocator) HGoto()); |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 157 | else_->AddInstruction(new (&allocator) HInstanceFieldGet(parameter, |
| 158 | Primitive::kPrimBoolean, |
| 159 | MemberOffset(42), |
| 160 | false, |
| 161 | kUnknownFieldIndex, |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 162 | graph->GetDexFile(), |
Calin Juravle | 154746b | 2015-10-06 15:46:54 +0100 | [diff] [blame] | 163 | dex_cache, |
| 164 | 0)); |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 165 | else_->AddInstruction(new (&allocator) HGoto()); |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 166 | join->AddInstruction(new (&allocator) HInstanceFieldGet(parameter, |
| 167 | Primitive::kPrimBoolean, |
| 168 | MemberOffset(42), |
| 169 | false, |
| 170 | kUnknownFieldIndex, |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 171 | graph->GetDexFile(), |
Calin Juravle | 154746b | 2015-10-06 15:46:54 +0100 | [diff] [blame] | 172 | dex_cache, |
| 173 | 0)); |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 174 | join->AddInstruction(new (&allocator) HExit()); |
| 175 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 176 | graph->TryBuildingSsa(); |
Nicolas Geoffray | e6f1715 | 2015-01-26 15:13:47 +0000 | [diff] [blame] | 177 | SideEffectsAnalysis side_effects(graph); |
| 178 | side_effects.Run(); |
Nicolas Geoffray | 827eedb | 2015-01-26 15:18:36 +0000 | [diff] [blame] | 179 | GVNOptimization(graph, side_effects).Run(); |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 180 | |
| 181 | // Check that all field get instructions have been GVN'ed. |
| 182 | ASSERT_TRUE(then->GetFirstInstruction()->IsGoto()); |
| 183 | ASSERT_TRUE(else_->GetFirstInstruction()->IsGoto()); |
| 184 | ASSERT_TRUE(join->GetFirstInstruction()->IsExit()); |
| 185 | } |
| 186 | |
| 187 | TEST(GVNTest, LoopFieldElimination) { |
| 188 | ArenaPool pool; |
| 189 | ArenaAllocator allocator(&pool); |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 190 | NullHandle<mirror::DexCache> dex_cache; |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 191 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 192 | HGraph* graph = CreateGraph(&allocator); |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 193 | HBasicBlock* entry = new (&allocator) HBasicBlock(graph); |
| 194 | graph->AddBlock(entry); |
| 195 | graph->SetEntryBlock(entry); |
| 196 | |
Calin Juravle | 2c1ffc3 | 2015-10-12 15:01:58 +0100 | [diff] [blame^] | 197 | HInstruction* parameter = new (&allocator) HParameterValue(graph->GetDexFile(), |
| 198 | 0, |
| 199 | 0, |
| 200 | Primitive::kPrimNot); |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 201 | entry->AddInstruction(parameter); |
| 202 | |
| 203 | HBasicBlock* block = new (&allocator) HBasicBlock(graph); |
| 204 | graph->AddBlock(block); |
| 205 | entry->AddSuccessor(block); |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 206 | block->AddInstruction(new (&allocator) HInstanceFieldGet(parameter, |
| 207 | Primitive::kPrimBoolean, |
| 208 | MemberOffset(42), |
| 209 | false, |
| 210 | kUnknownFieldIndex, |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 211 | graph->GetDexFile(), |
Calin Juravle | 154746b | 2015-10-06 15:46:54 +0100 | [diff] [blame] | 212 | dex_cache, |
| 213 | 0)); |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 214 | block->AddInstruction(new (&allocator) HGoto()); |
| 215 | |
| 216 | HBasicBlock* loop_header = new (&allocator) HBasicBlock(graph); |
| 217 | HBasicBlock* loop_body = new (&allocator) HBasicBlock(graph); |
| 218 | HBasicBlock* exit = new (&allocator) HBasicBlock(graph); |
| 219 | |
| 220 | graph->AddBlock(loop_header); |
| 221 | graph->AddBlock(loop_body); |
| 222 | graph->AddBlock(exit); |
| 223 | block->AddSuccessor(loop_header); |
| 224 | loop_header->AddSuccessor(loop_body); |
| 225 | loop_header->AddSuccessor(exit); |
| 226 | loop_body->AddSuccessor(loop_header); |
| 227 | |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 228 | loop_header->AddInstruction(new (&allocator) HInstanceFieldGet(parameter, |
| 229 | Primitive::kPrimBoolean, |
| 230 | MemberOffset(42), |
| 231 | false, |
| 232 | kUnknownFieldIndex, |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 233 | graph->GetDexFile(), |
Calin Juravle | 154746b | 2015-10-06 15:46:54 +0100 | [diff] [blame] | 234 | dex_cache, |
| 235 | 0)); |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 236 | HInstruction* field_get_in_loop_header = loop_header->GetLastInstruction(); |
| 237 | loop_header->AddInstruction(new (&allocator) HIf(block->GetLastInstruction())); |
| 238 | |
| 239 | // Kill inside the loop body to prevent field gets inside the loop header |
| 240 | // and the body to be GVN'ed. |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 241 | loop_body->AddInstruction(new (&allocator) HInstanceFieldSet(parameter, |
| 242 | parameter, |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 243 | Primitive::kPrimBoolean, |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 244 | MemberOffset(42), |
| 245 | false, |
| 246 | kUnknownFieldIndex, |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 247 | graph->GetDexFile(), |
Calin Juravle | 154746b | 2015-10-06 15:46:54 +0100 | [diff] [blame] | 248 | dex_cache, |
| 249 | 0)); |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 250 | HInstruction* field_set = loop_body->GetLastInstruction(); |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 251 | loop_body->AddInstruction(new (&allocator) HInstanceFieldGet(parameter, |
| 252 | Primitive::kPrimBoolean, |
| 253 | MemberOffset(42), |
| 254 | false, |
| 255 | kUnknownFieldIndex, |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 256 | graph->GetDexFile(), |
Calin Juravle | 154746b | 2015-10-06 15:46:54 +0100 | [diff] [blame] | 257 | dex_cache, |
| 258 | 0)); |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 259 | HInstruction* field_get_in_loop_body = loop_body->GetLastInstruction(); |
| 260 | loop_body->AddInstruction(new (&allocator) HGoto()); |
| 261 | |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 262 | exit->AddInstruction(new (&allocator) HInstanceFieldGet(parameter, |
| 263 | Primitive::kPrimBoolean, |
| 264 | MemberOffset(42), |
| 265 | false, |
| 266 | kUnknownFieldIndex, |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 267 | graph->GetDexFile(), |
Calin Juravle | 154746b | 2015-10-06 15:46:54 +0100 | [diff] [blame] | 268 | dex_cache, |
| 269 | 0)); |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 270 | HInstruction* field_get_in_exit = exit->GetLastInstruction(); |
| 271 | exit->AddInstruction(new (&allocator) HExit()); |
| 272 | |
| 273 | ASSERT_EQ(field_get_in_loop_header->GetBlock(), loop_header); |
| 274 | ASSERT_EQ(field_get_in_loop_body->GetBlock(), loop_body); |
| 275 | ASSERT_EQ(field_get_in_exit->GetBlock(), exit); |
| 276 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 277 | graph->TryBuildingSsa(); |
Nicolas Geoffray | e6f1715 | 2015-01-26 15:13:47 +0000 | [diff] [blame] | 278 | { |
| 279 | SideEffectsAnalysis side_effects(graph); |
| 280 | side_effects.Run(); |
Nicolas Geoffray | 827eedb | 2015-01-26 15:18:36 +0000 | [diff] [blame] | 281 | GVNOptimization(graph, side_effects).Run(); |
Nicolas Geoffray | e6f1715 | 2015-01-26 15:13:47 +0000 | [diff] [blame] | 282 | } |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 283 | |
| 284 | // Check that all field get instructions are still there. |
| 285 | ASSERT_EQ(field_get_in_loop_header->GetBlock(), loop_header); |
| 286 | ASSERT_EQ(field_get_in_loop_body->GetBlock(), loop_body); |
| 287 | // The exit block is dominated by the loop header, whose field get |
| 288 | // does not get killed by the loop flags. |
| 289 | ASSERT_TRUE(field_get_in_exit->GetBlock() == nullptr); |
| 290 | |
| 291 | // Now remove the field set, and check that all field get instructions have been GVN'ed. |
| 292 | loop_body->RemoveInstruction(field_set); |
Nicolas Geoffray | e6f1715 | 2015-01-26 15:13:47 +0000 | [diff] [blame] | 293 | { |
| 294 | SideEffectsAnalysis side_effects(graph); |
| 295 | side_effects.Run(); |
Nicolas Geoffray | 827eedb | 2015-01-26 15:18:36 +0000 | [diff] [blame] | 296 | GVNOptimization(graph, side_effects).Run(); |
Nicolas Geoffray | e6f1715 | 2015-01-26 15:13:47 +0000 | [diff] [blame] | 297 | } |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 298 | |
| 299 | ASSERT_TRUE(field_get_in_loop_header->GetBlock() == nullptr); |
| 300 | ASSERT_TRUE(field_get_in_loop_body->GetBlock() == nullptr); |
| 301 | ASSERT_TRUE(field_get_in_exit->GetBlock() == nullptr); |
| 302 | } |
| 303 | |
| 304 | // Test that inner loops affect the side effects of the outer loop. |
| 305 | TEST(GVNTest, LoopSideEffects) { |
| 306 | ArenaPool pool; |
| 307 | ArenaAllocator allocator(&pool); |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 308 | NullHandle<mirror::DexCache> dex_cache; |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 309 | |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 310 | static const SideEffects kCanTriggerGC = SideEffects::CanTriggerGC(); |
| 311 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 312 | HGraph* graph = CreateGraph(&allocator); |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 313 | HBasicBlock* entry = new (&allocator) HBasicBlock(graph); |
| 314 | graph->AddBlock(entry); |
| 315 | graph->SetEntryBlock(entry); |
| 316 | |
| 317 | HBasicBlock* outer_loop_header = new (&allocator) HBasicBlock(graph); |
| 318 | HBasicBlock* outer_loop_body = new (&allocator) HBasicBlock(graph); |
| 319 | HBasicBlock* outer_loop_exit = new (&allocator) HBasicBlock(graph); |
| 320 | HBasicBlock* inner_loop_header = new (&allocator) HBasicBlock(graph); |
| 321 | HBasicBlock* inner_loop_body = new (&allocator) HBasicBlock(graph); |
| 322 | HBasicBlock* inner_loop_exit = new (&allocator) HBasicBlock(graph); |
| 323 | |
| 324 | graph->AddBlock(outer_loop_header); |
| 325 | graph->AddBlock(outer_loop_body); |
| 326 | graph->AddBlock(outer_loop_exit); |
| 327 | graph->AddBlock(inner_loop_header); |
| 328 | graph->AddBlock(inner_loop_body); |
| 329 | graph->AddBlock(inner_loop_exit); |
| 330 | |
| 331 | entry->AddSuccessor(outer_loop_header); |
| 332 | outer_loop_header->AddSuccessor(outer_loop_body); |
| 333 | outer_loop_header->AddSuccessor(outer_loop_exit); |
| 334 | outer_loop_body->AddSuccessor(inner_loop_header); |
| 335 | inner_loop_header->AddSuccessor(inner_loop_body); |
| 336 | inner_loop_header->AddSuccessor(inner_loop_exit); |
| 337 | inner_loop_body->AddSuccessor(inner_loop_header); |
| 338 | inner_loop_exit->AddSuccessor(outer_loop_header); |
| 339 | |
Calin Juravle | 2c1ffc3 | 2015-10-12 15:01:58 +0100 | [diff] [blame^] | 340 | HInstruction* parameter = new (&allocator) HParameterValue(graph->GetDexFile(), |
| 341 | 0, |
| 342 | 0, |
| 343 | Primitive::kPrimBoolean); |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 344 | entry->AddInstruction(parameter); |
| 345 | entry->AddInstruction(new (&allocator) HGoto()); |
| 346 | outer_loop_header->AddInstruction(new (&allocator) HIf(parameter)); |
| 347 | outer_loop_body->AddInstruction(new (&allocator) HGoto()); |
| 348 | inner_loop_header->AddInstruction(new (&allocator) HIf(parameter)); |
| 349 | inner_loop_body->AddInstruction(new (&allocator) HGoto()); |
| 350 | inner_loop_exit->AddInstruction(new (&allocator) HGoto()); |
| 351 | outer_loop_exit->AddInstruction(new (&allocator) HExit()); |
| 352 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 353 | graph->TryBuildingSsa(); |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 354 | |
| 355 | ASSERT_TRUE(inner_loop_header->GetLoopInformation()->IsIn( |
| 356 | *outer_loop_header->GetLoopInformation())); |
| 357 | |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 358 | // Check that the only side effect of loops is to potentially trigger GC. |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 359 | { |
| 360 | // Make one block with a side effect. |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 361 | entry->AddInstruction(new (&allocator) HInstanceFieldSet(parameter, |
| 362 | parameter, |
| 363 | Primitive::kPrimNot, |
| 364 | MemberOffset(42), |
| 365 | false, |
| 366 | kUnknownFieldIndex, |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 367 | graph->GetDexFile(), |
Calin Juravle | 154746b | 2015-10-06 15:46:54 +0100 | [diff] [blame] | 368 | dex_cache, |
| 369 | 0)); |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 370 | |
Nicolas Geoffray | e6f1715 | 2015-01-26 15:13:47 +0000 | [diff] [blame] | 371 | SideEffectsAnalysis side_effects(graph); |
| 372 | side_effects.Run(); |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 373 | |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 374 | ASSERT_TRUE(side_effects.GetBlockEffects(entry).DoesAnyWrite()); |
| 375 | ASSERT_FALSE(side_effects.GetBlockEffects(outer_loop_body).DoesAnyWrite()); |
| 376 | ASSERT_FALSE(side_effects.GetLoopEffects(outer_loop_header).DoesAnyWrite()); |
| 377 | ASSERT_FALSE(side_effects.GetLoopEffects(inner_loop_header).DoesAnyWrite()); |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 378 | ASSERT_TRUE(side_effects.GetLoopEffects(outer_loop_header).Equals(kCanTriggerGC)); |
| 379 | ASSERT_TRUE(side_effects.GetLoopEffects(inner_loop_header).Equals(kCanTriggerGC)); |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | // Check that the side effects of the outer loop does not affect the inner loop. |
| 383 | { |
| 384 | outer_loop_body->InsertInstructionBefore( |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 385 | new (&allocator) HInstanceFieldSet(parameter, |
| 386 | parameter, |
| 387 | Primitive::kPrimNot, |
| 388 | MemberOffset(42), |
| 389 | false, |
| 390 | kUnknownFieldIndex, |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 391 | graph->GetDexFile(), |
Calin Juravle | 154746b | 2015-10-06 15:46:54 +0100 | [diff] [blame] | 392 | dex_cache, |
| 393 | 0), |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 394 | outer_loop_body->GetLastInstruction()); |
| 395 | |
Nicolas Geoffray | e6f1715 | 2015-01-26 15:13:47 +0000 | [diff] [blame] | 396 | SideEffectsAnalysis side_effects(graph); |
| 397 | side_effects.Run(); |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 398 | |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 399 | ASSERT_TRUE(side_effects.GetBlockEffects(entry).DoesAnyWrite()); |
| 400 | ASSERT_TRUE(side_effects.GetBlockEffects(outer_loop_body).DoesAnyWrite()); |
| 401 | ASSERT_TRUE(side_effects.GetLoopEffects(outer_loop_header).DoesAnyWrite()); |
| 402 | ASSERT_FALSE(side_effects.GetLoopEffects(inner_loop_header).DoesAnyWrite()); |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 403 | ASSERT_TRUE(side_effects.GetLoopEffects(inner_loop_header).Equals(kCanTriggerGC)); |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | // Check that the side effects of the inner loop affects the outer loop. |
| 407 | { |
| 408 | outer_loop_body->RemoveInstruction(outer_loop_body->GetFirstInstruction()); |
| 409 | inner_loop_body->InsertInstructionBefore( |
Guillaume "Vermeille" Sanchez | 104fd8a | 2015-05-20 17:52:13 +0100 | [diff] [blame] | 410 | new (&allocator) HInstanceFieldSet(parameter, |
| 411 | parameter, |
| 412 | Primitive::kPrimNot, |
| 413 | MemberOffset(42), |
| 414 | false, |
| 415 | kUnknownFieldIndex, |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 416 | graph->GetDexFile(), |
Calin Juravle | 154746b | 2015-10-06 15:46:54 +0100 | [diff] [blame] | 417 | dex_cache, |
| 418 | 0), |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 419 | inner_loop_body->GetLastInstruction()); |
| 420 | |
Nicolas Geoffray | e6f1715 | 2015-01-26 15:13:47 +0000 | [diff] [blame] | 421 | SideEffectsAnalysis side_effects(graph); |
| 422 | side_effects.Run(); |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 423 | |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 424 | ASSERT_TRUE(side_effects.GetBlockEffects(entry).DoesAnyWrite()); |
| 425 | ASSERT_FALSE(side_effects.GetBlockEffects(outer_loop_body).DoesAnyWrite()); |
| 426 | ASSERT_TRUE(side_effects.GetLoopEffects(outer_loop_header).DoesAnyWrite()); |
| 427 | ASSERT_TRUE(side_effects.GetLoopEffects(inner_loop_header).DoesAnyWrite()); |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 428 | } |
| 429 | } |
| 430 | } // namespace art |