xref: /aosp_15_r20/art/compiler/optimizing/write_barrier_elimination.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2022 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 "write_barrier_elimination.h"
18*795d594fSAndroid Build Coastguard Worker 
19*795d594fSAndroid Build Coastguard Worker #include "base/arena_allocator.h"
20*795d594fSAndroid Build Coastguard Worker #include "base/scoped_arena_allocator.h"
21*795d594fSAndroid Build Coastguard Worker #include "base/scoped_arena_containers.h"
22*795d594fSAndroid Build Coastguard Worker #include "optimizing/nodes.h"
23*795d594fSAndroid Build Coastguard Worker 
24*795d594fSAndroid Build Coastguard Worker // TODO(b/310755375, solanes): Enable WBE with the fixes.
25*795d594fSAndroid Build Coastguard Worker constexpr bool kWBEEnabled = true;
26*795d594fSAndroid Build Coastguard Worker 
27*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
28*795d594fSAndroid Build Coastguard Worker 
29*795d594fSAndroid Build Coastguard Worker class WBEVisitor final : public HGraphVisitor {
30*795d594fSAndroid Build Coastguard Worker  public:
WBEVisitor(HGraph * graph,OptimizingCompilerStats * stats)31*795d594fSAndroid Build Coastguard Worker   WBEVisitor(HGraph* graph, OptimizingCompilerStats* stats)
32*795d594fSAndroid Build Coastguard Worker       : HGraphVisitor(graph),
33*795d594fSAndroid Build Coastguard Worker         scoped_allocator_(graph->GetArenaStack()),
34*795d594fSAndroid Build Coastguard Worker         current_write_barriers_(scoped_allocator_.Adapter(kArenaAllocWBE)),
35*795d594fSAndroid Build Coastguard Worker         stats_(stats) {}
36*795d594fSAndroid Build Coastguard Worker 
VisitBasicBlock(HBasicBlock * block)37*795d594fSAndroid Build Coastguard Worker   void VisitBasicBlock(HBasicBlock* block) override {
38*795d594fSAndroid Build Coastguard Worker     // We clear the map to perform this optimization only in the same block. Doing it across blocks
39*795d594fSAndroid Build Coastguard Worker     // would entail non-trivial merging of states.
40*795d594fSAndroid Build Coastguard Worker     current_write_barriers_.clear();
41*795d594fSAndroid Build Coastguard Worker     VisitNonPhiInstructions(block);
42*795d594fSAndroid Build Coastguard Worker   }
43*795d594fSAndroid Build Coastguard Worker 
VisitInstanceFieldSet(HInstanceFieldSet * instruction)44*795d594fSAndroid Build Coastguard Worker   void VisitInstanceFieldSet(HInstanceFieldSet* instruction) override {
45*795d594fSAndroid Build Coastguard Worker     DCHECK(!instruction->GetSideEffects().Includes(SideEffects::CanTriggerGC()));
46*795d594fSAndroid Build Coastguard Worker 
47*795d594fSAndroid Build Coastguard Worker     if (instruction->GetFieldType() != DataType::Type::kReference ||
48*795d594fSAndroid Build Coastguard Worker         HuntForOriginalReference(instruction->GetValue())->IsNullConstant()) {
49*795d594fSAndroid Build Coastguard Worker       instruction->SetWriteBarrierKind(WriteBarrierKind::kDontEmit);
50*795d594fSAndroid Build Coastguard Worker       return;
51*795d594fSAndroid Build Coastguard Worker     }
52*795d594fSAndroid Build Coastguard Worker 
53*795d594fSAndroid Build Coastguard Worker     MaybeRecordStat(stats_, MethodCompilationStat::kPossibleWriteBarrier);
54*795d594fSAndroid Build Coastguard Worker     HInstruction* obj = HuntForOriginalReference(instruction->InputAt(0));
55*795d594fSAndroid Build Coastguard Worker     auto it = current_write_barriers_.find(obj);
56*795d594fSAndroid Build Coastguard Worker     if (it != current_write_barriers_.end()) {
57*795d594fSAndroid Build Coastguard Worker       DCHECK(it->second->IsInstanceFieldSet());
58*795d594fSAndroid Build Coastguard Worker       DCHECK(it->second->AsInstanceFieldSet()->GetWriteBarrierKind() !=
59*795d594fSAndroid Build Coastguard Worker              WriteBarrierKind::kDontEmit);
60*795d594fSAndroid Build Coastguard Worker       DCHECK_EQ(it->second->GetBlock(), instruction->GetBlock());
61*795d594fSAndroid Build Coastguard Worker       it->second->AsInstanceFieldSet()->SetWriteBarrierKind(WriteBarrierKind::kEmitBeingReliedOn);
62*795d594fSAndroid Build Coastguard Worker       instruction->SetWriteBarrierKind(WriteBarrierKind::kDontEmit);
63*795d594fSAndroid Build Coastguard Worker       MaybeRecordStat(stats_, MethodCompilationStat::kRemovedWriteBarrier);
64*795d594fSAndroid Build Coastguard Worker     } else {
65*795d594fSAndroid Build Coastguard Worker       const bool inserted = current_write_barriers_.insert({obj, instruction}).second;
66*795d594fSAndroid Build Coastguard Worker       DCHECK(inserted);
67*795d594fSAndroid Build Coastguard Worker       DCHECK(instruction->GetWriteBarrierKind() != WriteBarrierKind::kDontEmit);
68*795d594fSAndroid Build Coastguard Worker     }
69*795d594fSAndroid Build Coastguard Worker   }
70*795d594fSAndroid Build Coastguard Worker 
VisitStaticFieldSet(HStaticFieldSet * instruction)71*795d594fSAndroid Build Coastguard Worker   void VisitStaticFieldSet(HStaticFieldSet* instruction) override {
72*795d594fSAndroid Build Coastguard Worker     DCHECK(!instruction->GetSideEffects().Includes(SideEffects::CanTriggerGC()));
73*795d594fSAndroid Build Coastguard Worker 
74*795d594fSAndroid Build Coastguard Worker     if (instruction->GetFieldType() != DataType::Type::kReference ||
75*795d594fSAndroid Build Coastguard Worker         HuntForOriginalReference(instruction->GetValue())->IsNullConstant()) {
76*795d594fSAndroid Build Coastguard Worker       instruction->SetWriteBarrierKind(WriteBarrierKind::kDontEmit);
77*795d594fSAndroid Build Coastguard Worker       return;
78*795d594fSAndroid Build Coastguard Worker     }
79*795d594fSAndroid Build Coastguard Worker 
80*795d594fSAndroid Build Coastguard Worker     MaybeRecordStat(stats_, MethodCompilationStat::kPossibleWriteBarrier);
81*795d594fSAndroid Build Coastguard Worker     HInstruction* cls = HuntForOriginalReference(instruction->InputAt(0));
82*795d594fSAndroid Build Coastguard Worker     auto it = current_write_barriers_.find(cls);
83*795d594fSAndroid Build Coastguard Worker     if (it != current_write_barriers_.end()) {
84*795d594fSAndroid Build Coastguard Worker       DCHECK(it->second->IsStaticFieldSet());
85*795d594fSAndroid Build Coastguard Worker       DCHECK(it->second->AsStaticFieldSet()->GetWriteBarrierKind() != WriteBarrierKind::kDontEmit);
86*795d594fSAndroid Build Coastguard Worker       DCHECK_EQ(it->second->GetBlock(), instruction->GetBlock());
87*795d594fSAndroid Build Coastguard Worker       it->second->AsStaticFieldSet()->SetWriteBarrierKind(WriteBarrierKind::kEmitBeingReliedOn);
88*795d594fSAndroid Build Coastguard Worker       instruction->SetWriteBarrierKind(WriteBarrierKind::kDontEmit);
89*795d594fSAndroid Build Coastguard Worker       MaybeRecordStat(stats_, MethodCompilationStat::kRemovedWriteBarrier);
90*795d594fSAndroid Build Coastguard Worker     } else {
91*795d594fSAndroid Build Coastguard Worker       const bool inserted = current_write_barriers_.insert({cls, instruction}).second;
92*795d594fSAndroid Build Coastguard Worker       DCHECK(inserted);
93*795d594fSAndroid Build Coastguard Worker       DCHECK(instruction->GetWriteBarrierKind() != WriteBarrierKind::kDontEmit);
94*795d594fSAndroid Build Coastguard Worker     }
95*795d594fSAndroid Build Coastguard Worker   }
96*795d594fSAndroid Build Coastguard Worker 
VisitArraySet(HArraySet * instruction)97*795d594fSAndroid Build Coastguard Worker   void VisitArraySet(HArraySet* instruction) override {
98*795d594fSAndroid Build Coastguard Worker     if (instruction->GetSideEffects().Includes(SideEffects::CanTriggerGC())) {
99*795d594fSAndroid Build Coastguard Worker       ClearCurrentValues();
100*795d594fSAndroid Build Coastguard Worker     }
101*795d594fSAndroid Build Coastguard Worker 
102*795d594fSAndroid Build Coastguard Worker     if (instruction->GetComponentType() != DataType::Type::kReference ||
103*795d594fSAndroid Build Coastguard Worker         HuntForOriginalReference(instruction->GetValue())->IsNullConstant()) {
104*795d594fSAndroid Build Coastguard Worker       instruction->SetWriteBarrierKind(WriteBarrierKind::kDontEmit);
105*795d594fSAndroid Build Coastguard Worker       return;
106*795d594fSAndroid Build Coastguard Worker     }
107*795d594fSAndroid Build Coastguard Worker 
108*795d594fSAndroid Build Coastguard Worker     HInstruction* arr = HuntForOriginalReference(instruction->InputAt(0));
109*795d594fSAndroid Build Coastguard Worker     MaybeRecordStat(stats_, MethodCompilationStat::kPossibleWriteBarrier);
110*795d594fSAndroid Build Coastguard Worker     auto it = current_write_barriers_.find(arr);
111*795d594fSAndroid Build Coastguard Worker     if (it != current_write_barriers_.end()) {
112*795d594fSAndroid Build Coastguard Worker       DCHECK(it->second->IsArraySet());
113*795d594fSAndroid Build Coastguard Worker       DCHECK(it->second->AsArraySet()->GetWriteBarrierKind() != WriteBarrierKind::kDontEmit);
114*795d594fSAndroid Build Coastguard Worker       DCHECK_EQ(it->second->GetBlock(), instruction->GetBlock());
115*795d594fSAndroid Build Coastguard Worker       it->second->AsArraySet()->SetWriteBarrierKind(WriteBarrierKind::kEmitBeingReliedOn);
116*795d594fSAndroid Build Coastguard Worker       instruction->SetWriteBarrierKind(WriteBarrierKind::kDontEmit);
117*795d594fSAndroid Build Coastguard Worker       MaybeRecordStat(stats_, MethodCompilationStat::kRemovedWriteBarrier);
118*795d594fSAndroid Build Coastguard Worker     } else {
119*795d594fSAndroid Build Coastguard Worker       const bool inserted = current_write_barriers_.insert({arr, instruction}).second;
120*795d594fSAndroid Build Coastguard Worker       DCHECK(inserted);
121*795d594fSAndroid Build Coastguard Worker       DCHECK(instruction->GetWriteBarrierKind() != WriteBarrierKind::kDontEmit);
122*795d594fSAndroid Build Coastguard Worker     }
123*795d594fSAndroid Build Coastguard Worker   }
124*795d594fSAndroid Build Coastguard Worker 
VisitInstruction(HInstruction * instruction)125*795d594fSAndroid Build Coastguard Worker   void VisitInstruction(HInstruction* instruction) override {
126*795d594fSAndroid Build Coastguard Worker     if (instruction->GetSideEffects().Includes(SideEffects::CanTriggerGC())) {
127*795d594fSAndroid Build Coastguard Worker       ClearCurrentValues();
128*795d594fSAndroid Build Coastguard Worker     }
129*795d594fSAndroid Build Coastguard Worker   }
130*795d594fSAndroid Build Coastguard Worker 
131*795d594fSAndroid Build Coastguard Worker  private:
ClearCurrentValues()132*795d594fSAndroid Build Coastguard Worker   void ClearCurrentValues() { current_write_barriers_.clear(); }
133*795d594fSAndroid Build Coastguard Worker 
HuntForOriginalReference(HInstruction * ref) const134*795d594fSAndroid Build Coastguard Worker   HInstruction* HuntForOriginalReference(HInstruction* ref) const {
135*795d594fSAndroid Build Coastguard Worker     // An original reference can be transformed by instructions like:
136*795d594fSAndroid Build Coastguard Worker     //   i0 NewArray
137*795d594fSAndroid Build Coastguard Worker     //   i1 HInstruction(i0)  <-- NullCheck, BoundType, IntermediateAddress.
138*795d594fSAndroid Build Coastguard Worker     //   i2 ArraySet(i1, index, value)
139*795d594fSAndroid Build Coastguard Worker     DCHECK(ref != nullptr);
140*795d594fSAndroid Build Coastguard Worker     while (ref->IsNullCheck() || ref->IsBoundType() || ref->IsIntermediateAddress()) {
141*795d594fSAndroid Build Coastguard Worker       ref = ref->InputAt(0);
142*795d594fSAndroid Build Coastguard Worker     }
143*795d594fSAndroid Build Coastguard Worker     return ref;
144*795d594fSAndroid Build Coastguard Worker   }
145*795d594fSAndroid Build Coastguard Worker 
146*795d594fSAndroid Build Coastguard Worker   ScopedArenaAllocator scoped_allocator_;
147*795d594fSAndroid Build Coastguard Worker 
148*795d594fSAndroid Build Coastguard Worker   // Stores a map of <Receiver, InstructionWhereTheWriteBarrierIs>.
149*795d594fSAndroid Build Coastguard Worker   // `InstructionWhereTheWriteBarrierIs` is used for DCHECKs only.
150*795d594fSAndroid Build Coastguard Worker   ScopedArenaHashMap<HInstruction*, HInstruction*> current_write_barriers_;
151*795d594fSAndroid Build Coastguard Worker 
152*795d594fSAndroid Build Coastguard Worker   OptimizingCompilerStats* const stats_;
153*795d594fSAndroid Build Coastguard Worker 
154*795d594fSAndroid Build Coastguard Worker   DISALLOW_COPY_AND_ASSIGN(WBEVisitor);
155*795d594fSAndroid Build Coastguard Worker };
156*795d594fSAndroid Build Coastguard Worker 
Run()157*795d594fSAndroid Build Coastguard Worker bool WriteBarrierElimination::Run() {
158*795d594fSAndroid Build Coastguard Worker   if (kWBEEnabled) {
159*795d594fSAndroid Build Coastguard Worker     WBEVisitor wbe_visitor(graph_, stats_);
160*795d594fSAndroid Build Coastguard Worker     wbe_visitor.VisitReversePostOrder();
161*795d594fSAndroid Build Coastguard Worker   }
162*795d594fSAndroid Build Coastguard Worker   return true;
163*795d594fSAndroid Build Coastguard Worker }
164*795d594fSAndroid Build Coastguard Worker 
165*795d594fSAndroid Build Coastguard Worker }  // namespace art
166