LLVM API Documentation

AggressiveAntiDepBreaker.h
Go to the documentation of this file.
00001 //=- llvm/CodeGen/AggressiveAntiDepBreaker.h - Anti-Dep Support -*- 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 // This file implements the AggressiveAntiDepBreaker class, which
00011 // implements register anti-dependence breaking during post-RA
00012 // scheduling. It attempts to break all anti-dependencies within a
00013 // block.
00014 //
00015 //===----------------------------------------------------------------------===//
00016 
00017 #ifndef LLVM_LIB_CODEGEN_AGGRESSIVEANTIDEPBREAKER_H
00018 #define LLVM_LIB_CODEGEN_AGGRESSIVEANTIDEPBREAKER_H
00019 
00020 #include "AntiDepBreaker.h"
00021 #include "llvm/ADT/BitVector.h"
00022 #include "llvm/ADT/SmallSet.h"
00023 #include "llvm/CodeGen/MachineBasicBlock.h"
00024 #include "llvm/CodeGen/MachineFrameInfo.h"
00025 #include "llvm/CodeGen/MachineFunction.h"
00026 #include "llvm/CodeGen/MachineRegisterInfo.h"
00027 #include "llvm/CodeGen/ScheduleDAG.h"
00028 #include "llvm/Target/TargetRegisterInfo.h"
00029 #include "llvm/Target/TargetSubtargetInfo.h"
00030 #include <map>
00031 
00032 namespace llvm {
00033 class RegisterClassInfo;
00034 
00035   /// Class AggressiveAntiDepState
00036   /// Contains all the state necessary for anti-dep breaking.
00037   class AggressiveAntiDepState {
00038   public:
00039     /// RegisterReference - Information about a register reference
00040     /// within a liverange
00041     typedef struct {
00042       /// Operand - The registers operand
00043       MachineOperand *Operand;
00044       /// RC - The register class
00045       const TargetRegisterClass *RC;
00046     } RegisterReference;
00047 
00048   private:
00049     /// NumTargetRegs - Number of non-virtual target registers
00050     /// (i.e. TRI->getNumRegs()).
00051     const unsigned NumTargetRegs;
00052 
00053     /// GroupNodes - Implements a disjoint-union data structure to
00054     /// form register groups. A node is represented by an index into
00055     /// the vector. A node can "point to" itself to indicate that it
00056     /// is the parent of a group, or point to another node to indicate
00057     /// that it is a member of the same group as that node.
00058     std::vector<unsigned> GroupNodes;
00059 
00060     /// GroupNodeIndices - For each register, the index of the GroupNode
00061     /// currently representing the group that the register belongs to.
00062     /// Register 0 is always represented by the 0 group, a group
00063     /// composed of registers that are not eligible for anti-aliasing.
00064     std::vector<unsigned> GroupNodeIndices;
00065 
00066     /// RegRefs - Map registers to all their references within a live range.
00067     std::multimap<unsigned, RegisterReference> RegRefs;
00068 
00069     /// KillIndices - The index of the most recent kill (proceding bottom-up),
00070     /// or ~0u if the register is not live.
00071     std::vector<unsigned> KillIndices;
00072 
00073     /// DefIndices - The index of the most recent complete def (proceding bottom
00074     /// up), or ~0u if the register is live.
00075     std::vector<unsigned> DefIndices;
00076 
00077   public:
00078     AggressiveAntiDepState(const unsigned TargetRegs, MachineBasicBlock *BB);
00079 
00080     /// GetKillIndices - Return the kill indices.
00081     std::vector<unsigned> &GetKillIndices() { return KillIndices; }
00082 
00083     /// GetDefIndices - Return the define indices.
00084     std::vector<unsigned> &GetDefIndices() { return DefIndices; }
00085 
00086     /// GetRegRefs - Return the RegRefs map.
00087     std::multimap<unsigned, RegisterReference>& GetRegRefs() { return RegRefs; }
00088 
00089     // GetGroup - Get the group for a register. The returned value is
00090     // the index of the GroupNode representing the group.
00091     unsigned GetGroup(unsigned Reg);
00092 
00093     // GetGroupRegs - Return a vector of the registers belonging to a
00094     // group. If RegRefs is non-NULL then only included referenced registers.
00095     void GetGroupRegs(
00096        unsigned Group,
00097        std::vector<unsigned> &Regs,
00098        std::multimap<unsigned,
00099          AggressiveAntiDepState::RegisterReference> *RegRefs);
00100 
00101     // UnionGroups - Union Reg1's and Reg2's groups to form a new
00102     // group. Return the index of the GroupNode representing the
00103     // group.
00104     unsigned UnionGroups(unsigned Reg1, unsigned Reg2);
00105 
00106     // LeaveGroup - Remove a register from its current group and place
00107     // it alone in its own group. Return the index of the GroupNode
00108     // representing the registers new group.
00109     unsigned LeaveGroup(unsigned Reg);
00110 
00111     /// IsLive - Return true if Reg is live
00112     bool IsLive(unsigned Reg);
00113   };
00114 
00115 
00116   /// Class AggressiveAntiDepBreaker
00117   class AggressiveAntiDepBreaker : public AntiDepBreaker {
00118     MachineFunction& MF;
00119     MachineRegisterInfo &MRI;
00120     const TargetInstrInfo *TII;
00121     const TargetRegisterInfo *TRI;
00122     const RegisterClassInfo &RegClassInfo;
00123 
00124     /// CriticalPathSet - The set of registers that should only be
00125     /// renamed if they are on the critical path.
00126     BitVector CriticalPathSet;
00127 
00128     /// State - The state used to identify and rename anti-dependence
00129     /// registers.
00130     AggressiveAntiDepState *State;
00131 
00132   public:
00133     AggressiveAntiDepBreaker(MachineFunction& MFi,
00134                           const RegisterClassInfo &RCI,
00135                           TargetSubtargetInfo::RegClassVector& CriticalPathRCs);
00136     ~AggressiveAntiDepBreaker();
00137 
00138     /// Start - Initialize anti-dep breaking for a new basic block.
00139     void StartBlock(MachineBasicBlock *BB) override;
00140 
00141     /// BreakAntiDependencies - Identifiy anti-dependencies along the critical
00142     /// path
00143     /// of the ScheduleDAG and break them by renaming registers.
00144     ///
00145     unsigned BreakAntiDependencies(const std::vector<SUnit>& SUnits,
00146                                    MachineBasicBlock::iterator Begin,
00147                                    MachineBasicBlock::iterator End,
00148                                    unsigned InsertPosIndex,
00149                                    DbgValueVector &DbgValues) override;
00150 
00151     /// Observe - Update liveness information to account for the current
00152     /// instruction, which will not be scheduled.
00153     ///
00154     void Observe(MachineInstr *MI, unsigned Count,
00155                  unsigned InsertPosIndex) override;
00156 
00157     /// Finish - Finish anti-dep breaking for a basic block.
00158     void FinishBlock() override;
00159 
00160   private:
00161     /// Keep track of a position in the allocation order for each regclass.
00162     typedef std::map<const TargetRegisterClass *, unsigned> RenameOrderType;
00163 
00164     /// IsImplicitDefUse - Return true if MO represents a register
00165     /// that is both implicitly used and defined in MI
00166     bool IsImplicitDefUse(MachineInstr *MI, MachineOperand& MO);
00167 
00168     /// GetPassthruRegs - If MI implicitly def/uses a register, then
00169     /// return that register and all subregisters.
00170     void GetPassthruRegs(MachineInstr *MI, std::set<unsigned>& PassthruRegs);
00171 
00172     void HandleLastUse(unsigned Reg, unsigned KillIdx, const char *tag,
00173                        const char *header = nullptr,
00174                        const char *footer = nullptr);
00175 
00176     void PrescanInstruction(MachineInstr *MI, unsigned Count,
00177                             std::set<unsigned>& PassthruRegs);
00178     void ScanInstruction(MachineInstr *MI, unsigned Count);
00179     BitVector GetRenameRegisters(unsigned Reg);
00180     bool FindSuitableFreeRegisters(unsigned AntiDepGroupIndex,
00181                                    RenameOrderType& RenameOrder,
00182                                    std::map<unsigned, unsigned> &RenameMap);
00183   };
00184 }
00185 
00186 #endif