1 /* 2 * Copyright (C) 2023 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 BERBERIS_BACKEND_COMMON_LIFETIME_ANALYSIS_H_ 18 #define BERBERIS_BACKEND_COMMON_LIFETIME_ANALYSIS_H_ 19 20 #include "berberis/backend/common/lifetime.h" 21 #include "berberis/backend/common/machine_ir.h" 22 #include "berberis/base/arena_alloc.h" 23 #include "berberis/base/arena_vector.h" 24 #include "berberis/base/logging.h" 25 26 namespace berberis { 27 28 // Helper class for building sorted list of vreg lifetimes. 29 class VRegLifetimeAnalysis { 30 public: VRegLifetimeAnalysis(Arena * arena,int num_vreg,VRegLifetimeList * lifetimes)31 VRegLifetimeAnalysis(Arena* arena, int num_vreg, VRegLifetimeList* lifetimes) 32 : arena_(arena), 33 lifetimes_(lifetimes), 34 tick_(0), 35 bb_tick_(0), 36 vreg_lifetimes_(num_vreg, nullptr, arena) {} 37 38 void AddInsn(const MachineInsnListPosition& pos); 39 SetLiveIn(MachineReg r)40 void SetLiveIn(MachineReg r) { 41 // Ensure lifetime exists and includes current tick. 42 CHECK_EQ(tick_, bb_tick_); 43 GetVRegLifetime(r, tick_); 44 } 45 SetLiveOut(MachineReg r)46 void SetLiveOut(MachineReg r) { 47 // Lifetime must exist, extend to current tick. 48 auto* lifetime = vreg_lifetimes_[r.GetVRegIndex()]; 49 CHECK(lifetime); 50 lifetime->set_end(tick_); 51 } 52 EndBasicBlock()53 void EndBasicBlock() { bb_tick_ = tick_; } 54 55 private: 56 // Ensure lifetime exists and includes 'begin'. 57 VRegLifetime* GetVRegLifetime(MachineReg r, int begin); 58 59 void AppendUse(const VRegUse& use); 60 61 void TrySetMoveHint(const MachineInsn* insn); 62 63 Arena* arena_; 64 65 VRegLifetimeList* lifetimes_; 66 67 int tick_; 68 69 int bb_tick_; 70 71 // Map vreg index -> lifetime. 72 ArenaVector<VRegLifetime*> vreg_lifetimes_; 73 }; 74 75 } // namespace berberis 76 77 #endif // BERBERIS_BACKEND_COMMON_LIFETIME_ANALYSIS_H_ 78