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