LLVM API Documentation

LiveRegMatrix.h
Go to the documentation of this file.
00001 //===-- LiveRegMatrix.h - Track register interference ---------*- C++ -*---===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // The LiveRegMatrix analysis pass keeps track of virtual register interference
00011 // along two dimensions: Slot indexes and register units. The matrix is used by
00012 // register allocators to ensure that no interfering virtual registers get
00013 // assigned to overlapping physical registers.
00014 //
00015 // Register units are defined in MCRegisterInfo.h, they represent the smallest
00016 // unit of interference when dealing with overlapping physical registers. The
00017 // LiveRegMatrix is represented as a LiveIntervalUnion per register unit. When
00018 // a virtual register is assigned to a physical register, the live range for
00019 // the virtual register is inserted into the LiveIntervalUnion for each regunit
00020 // in the physreg.
00021 //
00022 //===----------------------------------------------------------------------===//
00023 
00024 #ifndef LLVM_CODEGEN_LIVEREGMATRIX_H
00025 #define LLVM_CODEGEN_LIVEREGMATRIX_H
00026 
00027 #include "llvm/ADT/BitVector.h"
00028 #include "llvm/CodeGen/LiveIntervalUnion.h"
00029 #include "llvm/CodeGen/MachineFunctionPass.h"
00030 
00031 namespace llvm {
00032 
00033 class LiveInterval;
00034 class LiveIntervalAnalysis;
00035 class MachineRegisterInfo;
00036 class TargetRegisterInfo;
00037 class VirtRegMap;
00038 
00039 class LiveRegMatrix : public MachineFunctionPass {
00040   const TargetRegisterInfo *TRI;
00041   MachineRegisterInfo *MRI;
00042   LiveIntervals *LIS;
00043   VirtRegMap *VRM;
00044 
00045   // UserTag changes whenever virtual registers have been modified.
00046   unsigned UserTag;
00047 
00048   // The matrix is represented as a LiveIntervalUnion per register unit.
00049   LiveIntervalUnion::Allocator LIUAlloc;
00050   LiveIntervalUnion::Array Matrix;
00051 
00052   // Cached queries per register unit.
00053   std::unique_ptr<LiveIntervalUnion::Query[]> Queries;
00054 
00055   // Cached register mask interference info.
00056   unsigned RegMaskTag;
00057   unsigned RegMaskVirtReg;
00058   BitVector RegMaskUsable;
00059 
00060   // MachineFunctionPass boilerplate.
00061   void getAnalysisUsage(AnalysisUsage&) const override;
00062   bool runOnMachineFunction(MachineFunction&) override;
00063   void releaseMemory() override;
00064 public:
00065   static char ID;
00066   LiveRegMatrix();
00067 
00068   //===--------------------------------------------------------------------===//
00069   // High-level interface.
00070   //===--------------------------------------------------------------------===//
00071   //
00072   // Check for interference before assigning virtual registers to physical
00073   // registers.
00074   //
00075 
00076   /// Invalidate cached interference queries after modifying virtual register
00077   /// live ranges. Interference checks may return stale information unless
00078   /// caches are invalidated.
00079   void invalidateVirtRegs() { ++UserTag; }
00080 
00081   enum InterferenceKind {
00082     /// No interference, go ahead and assign.
00083     IK_Free = 0,
00084 
00085     /// Virtual register interference. There are interfering virtual registers
00086     /// assigned to PhysReg or its aliases. This interference could be resolved
00087     /// by unassigning those other virtual registers.
00088     IK_VirtReg,
00089 
00090     /// Register unit interference. A fixed live range is in the way, typically
00091     /// argument registers for a call. This can't be resolved by unassigning
00092     /// other virtual registers.
00093     IK_RegUnit,
00094 
00095     /// RegMask interference. The live range is crossing an instruction with a
00096     /// regmask operand that doesn't preserve PhysReg. This typically means
00097     /// VirtReg is live across a call, and PhysReg isn't call-preserved.
00098     IK_RegMask
00099   };
00100 
00101   /// Check for interference before assigning VirtReg to PhysReg.
00102   /// If this function returns IK_Free, it is legal to assign(VirtReg, PhysReg).
00103   /// When there is more than one kind of interference, the InterferenceKind
00104   /// with the highest enum value is returned.
00105   InterferenceKind checkInterference(LiveInterval &VirtReg, unsigned PhysReg);
00106 
00107   /// Assign VirtReg to PhysReg.
00108   /// This will mark VirtReg's live range as occupied in the LiveRegMatrix and
00109   /// update VirtRegMap. The live range is expected to be available in PhysReg.
00110   void assign(LiveInterval &VirtReg, unsigned PhysReg);
00111 
00112   /// Unassign VirtReg from its PhysReg.
00113   /// Assuming that VirtReg was previously assigned to a PhysReg, this undoes
00114   /// the assignment and updates VirtRegMap accordingly.
00115   void unassign(LiveInterval &VirtReg);
00116 
00117   //===--------------------------------------------------------------------===//
00118   // Low-level interface.
00119   //===--------------------------------------------------------------------===//
00120   //
00121   // Provide access to the underlying LiveIntervalUnions.
00122   //
00123 
00124   /// Check for regmask interference only.
00125   /// Return true if VirtReg crosses a regmask operand that clobbers PhysReg.
00126   /// If PhysReg is null, check if VirtReg crosses any regmask operands.
00127   bool checkRegMaskInterference(LiveInterval &VirtReg, unsigned PhysReg = 0);
00128 
00129   /// Check for regunit interference only.
00130   /// Return true if VirtReg overlaps a fixed assignment of one of PhysRegs's
00131   /// register units.
00132   bool checkRegUnitInterference(LiveInterval &VirtReg, unsigned PhysReg);
00133 
00134   /// Query a line of the assigned virtual register matrix directly.
00135   /// Use MCRegUnitIterator to enumerate all regunits in the desired PhysReg.
00136   /// This returns a reference to an internal Query data structure that is only
00137   /// valid until the next query() call.
00138   LiveIntervalUnion::Query &query(LiveInterval &VirtReg, unsigned RegUnit);
00139 
00140   /// Directly access the live interval unions per regunit.
00141   /// This returns an array indexed by the regunit number.
00142   LiveIntervalUnion *getLiveUnions() { return &Matrix[0]; }
00143 };
00144 
00145 } // end namespace llvm
00146 
00147 #endif // LLVM_CODEGEN_LIVEREGMATRIX_H