1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ART_COMPILER_OPTIMIZING_WRITE_BARRIER_ELIMINATION_H_
18 #define ART_COMPILER_OPTIMIZING_WRITE_BARRIER_ELIMINATION_H_
19 
20 #include "base/macros.h"
21 #include "optimization.h"
22 
23 namespace art HIDDEN {
24 
25 // Eliminates unnecessary write barriers from InstanceFieldSet, StaticFieldSet, and ArraySet.
26 //
27 // We can eliminate redundant write barriers as we don't need several for the same receiver. For
28 // example:
29 //   MyObject o;
30 //   o.inner_obj = io;
31 //   o.inner_obj2 = io2;
32 //   o.inner_obj3 = io3;
33 // We can keep the write barrier for `inner_obj` and remove the other two.
34 //
35 // In order to do this, we set the WriteBarrierKind of the instruction. The instruction's kind are
36 // set to kEmitBeingReliedOn (if this write barrier coalesced other write barriers, we don't want to
37 // perform the null check optimization), or to kDontEmit (if the write barrier as a whole is not
38 // needed).
39 class WriteBarrierElimination : public HOptimization {
40  public:
41   WriteBarrierElimination(HGraph* graph,
42                           OptimizingCompilerStats* stats,
43                           const char* name = kWBEPassName)
HOptimization(graph,name,stats)44       : HOptimization(graph, name, stats) {}
45 
46   bool Run() override;
47 
48   static constexpr const char* kWBEPassName = "write_barrier_elimination";
49 
50  private:
51   DISALLOW_COPY_AND_ASSIGN(WriteBarrierElimination);
52 };
53 
54 }  // namespace art
55 
56 #endif  // ART_COMPILER_OPTIMIZING_WRITE_BARRIER_ELIMINATION_H_
57