xref: /aosp_15_r20/art/compiler/optimizing/ssa_liveness_analysis_test.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2017 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #include "ssa_liveness_analysis.h"
18*795d594fSAndroid Build Coastguard Worker 
19*795d594fSAndroid Build Coastguard Worker #include "arch/instruction_set.h"
20*795d594fSAndroid Build Coastguard Worker #include "arch/instruction_set_features.h"
21*795d594fSAndroid Build Coastguard Worker #include "base/arena_allocator.h"
22*795d594fSAndroid Build Coastguard Worker #include "base/arena_containers.h"
23*795d594fSAndroid Build Coastguard Worker #include "base/macros.h"
24*795d594fSAndroid Build Coastguard Worker #include "code_generator.h"
25*795d594fSAndroid Build Coastguard Worker #include "driver/compiler_options.h"
26*795d594fSAndroid Build Coastguard Worker #include "nodes.h"
27*795d594fSAndroid Build Coastguard Worker #include "optimizing_unit_test.h"
28*795d594fSAndroid Build Coastguard Worker 
29*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
30*795d594fSAndroid Build Coastguard Worker 
31*795d594fSAndroid Build Coastguard Worker class SsaLivenessAnalysisTest : public OptimizingUnitTest {
32*795d594fSAndroid Build Coastguard Worker  protected:
SetUp()33*795d594fSAndroid Build Coastguard Worker   void SetUp() override {
34*795d594fSAndroid Build Coastguard Worker     OptimizingUnitTest::SetUp();
35*795d594fSAndroid Build Coastguard Worker     graph_ = CreateGraph();
36*795d594fSAndroid Build Coastguard Worker     compiler_options_ = CommonCompilerTest::CreateCompilerOptions(kRuntimeISA, "default");
37*795d594fSAndroid Build Coastguard Worker     codegen_ = CodeGenerator::Create(graph_, *compiler_options_);
38*795d594fSAndroid Build Coastguard Worker     CHECK(codegen_ != nullptr);
39*795d594fSAndroid Build Coastguard Worker     // Create entry block.
40*795d594fSAndroid Build Coastguard Worker     entry_ = new (GetAllocator()) HBasicBlock(graph_);
41*795d594fSAndroid Build Coastguard Worker     graph_->AddBlock(entry_);
42*795d594fSAndroid Build Coastguard Worker     graph_->SetEntryBlock(entry_);
43*795d594fSAndroid Build Coastguard Worker   }
44*795d594fSAndroid Build Coastguard Worker 
45*795d594fSAndroid Build Coastguard Worker  protected:
CreateSuccessor(HBasicBlock * block)46*795d594fSAndroid Build Coastguard Worker   HBasicBlock* CreateSuccessor(HBasicBlock* block) {
47*795d594fSAndroid Build Coastguard Worker     HGraph* graph = block->GetGraph();
48*795d594fSAndroid Build Coastguard Worker     HBasicBlock* successor = new (GetAllocator()) HBasicBlock(graph);
49*795d594fSAndroid Build Coastguard Worker     graph->AddBlock(successor);
50*795d594fSAndroid Build Coastguard Worker     block->AddSuccessor(successor);
51*795d594fSAndroid Build Coastguard Worker     return successor;
52*795d594fSAndroid Build Coastguard Worker   }
53*795d594fSAndroid Build Coastguard Worker 
54*795d594fSAndroid Build Coastguard Worker   HGraph* graph_;
55*795d594fSAndroid Build Coastguard Worker   std::unique_ptr<CompilerOptions> compiler_options_;
56*795d594fSAndroid Build Coastguard Worker   std::unique_ptr<CodeGenerator> codegen_;
57*795d594fSAndroid Build Coastguard Worker   HBasicBlock* entry_;
58*795d594fSAndroid Build Coastguard Worker };
59*795d594fSAndroid Build Coastguard Worker 
TEST_F(SsaLivenessAnalysisTest,TestReturnArg)60*795d594fSAndroid Build Coastguard Worker TEST_F(SsaLivenessAnalysisTest, TestReturnArg) {
61*795d594fSAndroid Build Coastguard Worker   HInstruction* arg = MakeParam(DataType::Type::kInt32);
62*795d594fSAndroid Build Coastguard Worker 
63*795d594fSAndroid Build Coastguard Worker   HBasicBlock* block = CreateSuccessor(entry_);
64*795d594fSAndroid Build Coastguard Worker   MakeReturn(block, arg);
65*795d594fSAndroid Build Coastguard Worker   MakeExit(block);
66*795d594fSAndroid Build Coastguard Worker 
67*795d594fSAndroid Build Coastguard Worker   graph_->BuildDominatorTree();
68*795d594fSAndroid Build Coastguard Worker   SsaLivenessAnalysis ssa_analysis(graph_, codegen_.get(), GetScopedAllocator());
69*795d594fSAndroid Build Coastguard Worker   ssa_analysis.Analyze();
70*795d594fSAndroid Build Coastguard Worker 
71*795d594fSAndroid Build Coastguard Worker   std::ostringstream arg_dump;
72*795d594fSAndroid Build Coastguard Worker   arg->GetLiveInterval()->Dump(arg_dump);
73*795d594fSAndroid Build Coastguard Worker   EXPECT_STREQ("ranges: { [2,6) }, uses: { 6 }, { } is_fixed: 0, is_split: 0 is_low: 0 is_high: 0",
74*795d594fSAndroid Build Coastguard Worker                arg_dump.str().c_str());
75*795d594fSAndroid Build Coastguard Worker }
76*795d594fSAndroid Build Coastguard Worker 
TEST_F(SsaLivenessAnalysisTest,TestAput)77*795d594fSAndroid Build Coastguard Worker TEST_F(SsaLivenessAnalysisTest, TestAput) {
78*795d594fSAndroid Build Coastguard Worker   HInstruction* array = MakeParam(DataType::Type::kReference);
79*795d594fSAndroid Build Coastguard Worker   HInstruction* index = MakeParam(DataType::Type::kInt32);
80*795d594fSAndroid Build Coastguard Worker   HInstruction* value = MakeParam(DataType::Type::kInt32);
81*795d594fSAndroid Build Coastguard Worker   HInstruction* extra_arg1 = MakeParam(DataType::Type::kInt32);
82*795d594fSAndroid Build Coastguard Worker   HInstruction* extra_arg2 = MakeParam(DataType::Type::kReference);
83*795d594fSAndroid Build Coastguard Worker   std::initializer_list<HInstruction*> args{array, index, value, extra_arg1, extra_arg2};
84*795d594fSAndroid Build Coastguard Worker 
85*795d594fSAndroid Build Coastguard Worker   HBasicBlock* block = CreateSuccessor(entry_);
86*795d594fSAndroid Build Coastguard Worker   HInstruction* null_check = MakeNullCheck(block, array, /*env=*/ args);
87*795d594fSAndroid Build Coastguard Worker   HInstruction* length = MakeArrayLength(block, array);
88*795d594fSAndroid Build Coastguard Worker   HInstruction* bounds_check = MakeBoundsCheck(block, index, length, /*env=*/ args);
89*795d594fSAndroid Build Coastguard Worker   MakeArraySet(block, array, index, value, DataType::Type::kInt32);
90*795d594fSAndroid Build Coastguard Worker 
91*795d594fSAndroid Build Coastguard Worker   graph_->BuildDominatorTree();
92*795d594fSAndroid Build Coastguard Worker   SsaLivenessAnalysis ssa_analysis(graph_, codegen_.get(), GetScopedAllocator());
93*795d594fSAndroid Build Coastguard Worker   ssa_analysis.Analyze();
94*795d594fSAndroid Build Coastguard Worker 
95*795d594fSAndroid Build Coastguard Worker   EXPECT_FALSE(graph_->IsDebuggable());
96*795d594fSAndroid Build Coastguard Worker   EXPECT_EQ(18u, bounds_check->GetLifetimePosition());
97*795d594fSAndroid Build Coastguard Worker   static const char* const expected[] = {
98*795d594fSAndroid Build Coastguard Worker       "ranges: { [2,21) }, uses: { 15 17 21 }, { 15 19 } is_fixed: 0, is_split: 0 is_low: 0 "
99*795d594fSAndroid Build Coastguard Worker           "is_high: 0",
100*795d594fSAndroid Build Coastguard Worker       "ranges: { [4,21) }, uses: { 19 21 }, { } is_fixed: 0, is_split: 0 is_low: 0 "
101*795d594fSAndroid Build Coastguard Worker           "is_high: 0",
102*795d594fSAndroid Build Coastguard Worker       "ranges: { [6,21) }, uses: { 21 }, { } is_fixed: 0, is_split: 0 is_low: 0 "
103*795d594fSAndroid Build Coastguard Worker           "is_high: 0",
104*795d594fSAndroid Build Coastguard Worker       // Environment uses do not keep the non-reference argument alive.
105*795d594fSAndroid Build Coastguard Worker       "ranges: { [8,10) }, uses: { }, { } is_fixed: 0, is_split: 0 is_low: 0 is_high: 0",
106*795d594fSAndroid Build Coastguard Worker       // Environment uses keep the reference argument alive.
107*795d594fSAndroid Build Coastguard Worker       "ranges: { [10,19) }, uses: { }, { 15 19 } is_fixed: 0, is_split: 0 is_low: 0 is_high: 0",
108*795d594fSAndroid Build Coastguard Worker   };
109*795d594fSAndroid Build Coastguard Worker   CHECK_EQ(arraysize(expected), args.size());
110*795d594fSAndroid Build Coastguard Worker   size_t arg_index = 0u;
111*795d594fSAndroid Build Coastguard Worker   for (HInstruction* arg : args) {
112*795d594fSAndroid Build Coastguard Worker     std::ostringstream arg_dump;
113*795d594fSAndroid Build Coastguard Worker     arg->GetLiveInterval()->Dump(arg_dump);
114*795d594fSAndroid Build Coastguard Worker     EXPECT_STREQ(expected[arg_index], arg_dump.str().c_str()) << arg_index;
115*795d594fSAndroid Build Coastguard Worker     ++arg_index;
116*795d594fSAndroid Build Coastguard Worker   }
117*795d594fSAndroid Build Coastguard Worker }
118*795d594fSAndroid Build Coastguard Worker 
TEST_F(SsaLivenessAnalysisTest,TestDeoptimize)119*795d594fSAndroid Build Coastguard Worker TEST_F(SsaLivenessAnalysisTest, TestDeoptimize) {
120*795d594fSAndroid Build Coastguard Worker   HInstruction* array = MakeParam(DataType::Type::kReference);
121*795d594fSAndroid Build Coastguard Worker   HInstruction* index = MakeParam(DataType::Type::kInt32);
122*795d594fSAndroid Build Coastguard Worker   HInstruction* value = MakeParam(DataType::Type::kInt32);
123*795d594fSAndroid Build Coastguard Worker   HInstruction* extra_arg1 = MakeParam(DataType::Type::kInt32);
124*795d594fSAndroid Build Coastguard Worker   HInstruction* extra_arg2 = MakeParam(DataType::Type::kReference);
125*795d594fSAndroid Build Coastguard Worker   std::initializer_list<HInstruction*> args{array, index, value, extra_arg1, extra_arg2};
126*795d594fSAndroid Build Coastguard Worker 
127*795d594fSAndroid Build Coastguard Worker   HBasicBlock* block = CreateSuccessor(entry_);
128*795d594fSAndroid Build Coastguard Worker   HInstruction* null_check = MakeNullCheck(block, array, /*env=*/ args);
129*795d594fSAndroid Build Coastguard Worker   HInstruction* length = MakeArrayLength(block, array);
130*795d594fSAndroid Build Coastguard Worker   // Use HAboveOrEqual+HDeoptimize as the bounds check.
131*795d594fSAndroid Build Coastguard Worker   HInstruction* ae = MakeCondition(block, kCondAE, index, length);
132*795d594fSAndroid Build Coastguard Worker   HInstruction* deoptimize = new(GetAllocator()) HDeoptimize(
133*795d594fSAndroid Build Coastguard Worker       GetAllocator(), ae, DeoptimizationKind::kBlockBCE, /* dex_pc= */ 0u);
134*795d594fSAndroid Build Coastguard Worker   block->AddInstruction(deoptimize);
135*795d594fSAndroid Build Coastguard Worker   ManuallyBuildEnvFor(deoptimize, /*env=*/ args);
136*795d594fSAndroid Build Coastguard Worker   MakeArraySet(block, array, index, value, DataType::Type::kInt32);
137*795d594fSAndroid Build Coastguard Worker 
138*795d594fSAndroid Build Coastguard Worker   graph_->BuildDominatorTree();
139*795d594fSAndroid Build Coastguard Worker   SsaLivenessAnalysis ssa_analysis(graph_, codegen_.get(), GetScopedAllocator());
140*795d594fSAndroid Build Coastguard Worker   ssa_analysis.Analyze();
141*795d594fSAndroid Build Coastguard Worker 
142*795d594fSAndroid Build Coastguard Worker   EXPECT_FALSE(graph_->IsDebuggable());
143*795d594fSAndroid Build Coastguard Worker   EXPECT_EQ(20u, deoptimize->GetLifetimePosition());
144*795d594fSAndroid Build Coastguard Worker   static const char* const expected[] = {
145*795d594fSAndroid Build Coastguard Worker       "ranges: { [2,23) }, uses: { 15 17 23 }, { 15 21 } is_fixed: 0, is_split: 0 is_low: 0 "
146*795d594fSAndroid Build Coastguard Worker           "is_high: 0",
147*795d594fSAndroid Build Coastguard Worker       "ranges: { [4,23) }, uses: { 19 23 }, { 21 } is_fixed: 0, is_split: 0 is_low: 0 "
148*795d594fSAndroid Build Coastguard Worker           "is_high: 0",
149*795d594fSAndroid Build Coastguard Worker       "ranges: { [6,23) }, uses: { 23 }, { 21 } is_fixed: 0, is_split: 0 is_low: 0 is_high: 0",
150*795d594fSAndroid Build Coastguard Worker       // Environment use in HDeoptimize keeps even the non-reference argument alive.
151*795d594fSAndroid Build Coastguard Worker       "ranges: { [8,21) }, uses: { }, { 21 } is_fixed: 0, is_split: 0 is_low: 0 is_high: 0",
152*795d594fSAndroid Build Coastguard Worker       // Environment uses keep the reference argument alive.
153*795d594fSAndroid Build Coastguard Worker       "ranges: { [10,21) }, uses: { }, { 15 21 } is_fixed: 0, is_split: 0 is_low: 0 is_high: 0",
154*795d594fSAndroid Build Coastguard Worker   };
155*795d594fSAndroid Build Coastguard Worker   CHECK_EQ(arraysize(expected), args.size());
156*795d594fSAndroid Build Coastguard Worker   size_t arg_index = 0u;
157*795d594fSAndroid Build Coastguard Worker   for (HInstruction* arg : args) {
158*795d594fSAndroid Build Coastguard Worker     std::ostringstream arg_dump;
159*795d594fSAndroid Build Coastguard Worker     arg->GetLiveInterval()->Dump(arg_dump);
160*795d594fSAndroid Build Coastguard Worker     EXPECT_STREQ(expected[arg_index], arg_dump.str().c_str()) << arg_index;
161*795d594fSAndroid Build Coastguard Worker     ++arg_index;
162*795d594fSAndroid Build Coastguard Worker   }
163*795d594fSAndroid Build Coastguard Worker }
164*795d594fSAndroid Build Coastguard Worker 
165*795d594fSAndroid Build Coastguard Worker }  // namespace art
166