LLVM API Documentation
00001 //===-- SimpleRegisterCoalescing.h - Register Coalescing --------*- 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 a simple register copy coalescing phase. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #ifndef LLVM_CODEGEN_SIMPLE_REGISTER_COALESCING_H 00015 #define LLVM_CODEGEN_SIMPLE_REGISTER_COALESCING_H 00016 00017 #include "llvm/CodeGen/MachineFunctionPass.h" 00018 #include "llvm/CodeGen/LiveIntervalAnalysis.h" 00019 #include "llvm/CodeGen/RegisterCoalescer.h" 00020 #include "llvm/ADT/BitVector.h" 00021 #include <queue> 00022 00023 namespace llvm { 00024 class SimpleRegisterCoalescing; 00025 class LiveVariables; 00026 class TargetRegisterInfo; 00027 class TargetInstrInfo; 00028 class VirtRegMap; 00029 class MachineLoopInfo; 00030 00031 /// CopyRec - Representation for copy instructions in coalescer queue. 00032 /// 00033 struct CopyRec { 00034 MachineInstr *MI; 00035 unsigned LoopDepth; 00036 bool isBackEdge; 00037 CopyRec(MachineInstr *mi, unsigned depth, bool be) 00038 : MI(mi), LoopDepth(depth), isBackEdge(be) {}; 00039 }; 00040 00041 template<class SF> class JoinPriorityQueue; 00042 00043 /// CopyRecSort - Sorting function for coalescer queue. 00044 /// 00045 struct CopyRecSort : public std::binary_function<CopyRec,CopyRec,bool> { 00046 JoinPriorityQueue<CopyRecSort> *JPQ; 00047 explicit CopyRecSort(JoinPriorityQueue<CopyRecSort> *jpq) : JPQ(jpq) {} 00048 CopyRecSort(const CopyRecSort &RHS) : JPQ(RHS.JPQ) {} 00049 bool operator()(CopyRec left, CopyRec right) const; 00050 }; 00051 00052 /// JoinQueue - A priority queue of copy instructions the coalescer is 00053 /// going to process. 00054 template<class SF> 00055 class JoinPriorityQueue { 00056 SimpleRegisterCoalescing *Rc; 00057 std::priority_queue<CopyRec, std::vector<CopyRec>, SF> Queue; 00058 00059 public: 00060 explicit JoinPriorityQueue(SimpleRegisterCoalescing *rc) 00061 : Rc(rc), Queue(SF(this)) {} 00062 00063 bool empty() const { return Queue.empty(); } 00064 void push(CopyRec R) { Queue.push(R); } 00065 CopyRec pop() { 00066 if (empty()) return CopyRec(0, 0, false); 00067 CopyRec R = Queue.top(); 00068 Queue.pop(); 00069 return R; 00070 } 00071 00072 // Callbacks to SimpleRegisterCoalescing. 00073 unsigned getRepIntervalSize(unsigned Reg); 00074 }; 00075 00076 class SimpleRegisterCoalescing : public MachineFunctionPass, 00077 public RegisterCoalescer { 00078 MachineFunction* mf_; 00079 MachineRegisterInfo* mri_; 00080 const TargetMachine* tm_; 00081 const TargetRegisterInfo* tri_; 00082 const TargetInstrInfo* tii_; 00083 LiveIntervals *li_; 00084 const MachineLoopInfo* loopInfo; 00085 00086 BitVector allocatableRegs_; 00087 DenseMap<const TargetRegisterClass*, BitVector> allocatableRCRegs_; 00088 00089 /// JoinQueue - A priority queue of copy instructions the coalescer is 00090 /// going to process. 00091 JoinPriorityQueue<CopyRecSort> *JoinQueue; 00092 00093 /// JoinedCopies - Keep track of copies eliminated due to coalescing. 00094 /// 00095 SmallPtrSet<MachineInstr*, 32> JoinedCopies; 00096 00097 /// ReMatCopies - Keep track of copies eliminated due to remat. 00098 /// 00099 SmallPtrSet<MachineInstr*, 32> ReMatCopies; 00100 00101 /// ReMatDefs - Keep track of definition instructions which have 00102 /// been remat'ed. 00103 SmallPtrSet<MachineInstr*, 8> ReMatDefs; 00104 00105 public: 00106 static char ID; // Pass identifcation, replacement for typeid 00107 SimpleRegisterCoalescing() : MachineFunctionPass(&ID) {} 00108 00109 struct InstrSlots { 00110 enum { 00111 LOAD = 0, 00112 USE = 1, 00113 DEF = 2, 00114 STORE = 3, 00115 NUM = 4 00116 }; 00117 }; 00118 00119 virtual void getAnalysisUsage(AnalysisUsage &AU) const; 00120 virtual void releaseMemory(); 00121 00122 /// runOnMachineFunction - pass entry point 00123 virtual bool runOnMachineFunction(MachineFunction&); 00124 00125 bool coalesceFunction(MachineFunction &mf, RegallocQuery &) { 00126 // This runs as an independent pass, so don't do anything. 00127 return false; 00128 }; 00129 00130 /// getRepIntervalSize - Called from join priority queue sorting function. 00131 /// It returns the size of the interval that represent the given register. 00132 unsigned getRepIntervalSize(unsigned Reg) { 00133 if (!li_->hasInterval(Reg)) 00134 return 0; 00135 return li_->getApproximateInstructionCount(li_->getInterval(Reg)) * 00136 LiveIntervals::InstrSlots::NUM; 00137 } 00138 00139 /// print - Implement the dump method. 00140 virtual void print(std::ostream &O, const Module* = 0) const; 00141 void print(std::ostream *O, const Module* M = 0) const { 00142 if (O) print(*O, M); 00143 } 00144 00145 private: 00146 /// joinIntervals - join compatible live intervals 00147 void joinIntervals(); 00148 00149 /// CopyCoalesceInMBB - Coalesce copies in the specified MBB, putting 00150 /// copies that cannot yet be coalesced into the "TryAgain" list. 00151 void CopyCoalesceInMBB(MachineBasicBlock *MBB, 00152 std::vector<CopyRec> &TryAgain); 00153 00154 /// JoinCopy - Attempt to join intervals corresponding to SrcReg/DstReg, 00155 /// which are the src/dst of the copy instruction CopyMI. This returns true 00156 /// if the copy was successfully coalesced away. If it is not currently 00157 /// possible to coalesce this interval, but it may be possible if other 00158 /// things get coalesced, then it returns true by reference in 'Again'. 00159 bool JoinCopy(CopyRec &TheCopy, bool &Again); 00160 00161 /// JoinIntervals - Attempt to join these two intervals. On failure, this 00162 /// returns false. Otherwise, if one of the intervals being joined is a 00163 /// physreg, this method always canonicalizes DestInt to be it. The output 00164 /// "SrcInt" will not have been modified, so we can use this information 00165 /// below to update aliases. 00166 bool JoinIntervals(LiveInterval &LHS, LiveInterval &RHS, bool &Swapped); 00167 00168 /// SimpleJoin - Attempt to join the specified interval into this one. The 00169 /// caller of this method must guarantee that the RHS only contains a single 00170 /// value number and that the RHS is not defined by a copy from this 00171 /// interval. This returns false if the intervals are not joinable, or it 00172 /// joins them and returns true. 00173 bool SimpleJoin(LiveInterval &LHS, LiveInterval &RHS); 00174 00175 /// Return true if the two specified registers belong to different register 00176 /// classes. The registers may be either phys or virt regs. 00177 bool differingRegisterClasses(unsigned RegA, unsigned RegB) const; 00178 00179 00180 /// AdjustCopiesBackFrom - We found a non-trivially-coalescable copy. If 00181 /// the source value number is defined by a copy from the destination reg 00182 /// see if we can merge these two destination reg valno# into a single 00183 /// value number, eliminating a copy. 00184 bool AdjustCopiesBackFrom(LiveInterval &IntA, LiveInterval &IntB, 00185 MachineInstr *CopyMI); 00186 00187 /// HasOtherReachingDefs - Return true if there are definitions of IntB 00188 /// other than BValNo val# that can reach uses of AValno val# of IntA. 00189 bool HasOtherReachingDefs(LiveInterval &IntA, LiveInterval &IntB, 00190 VNInfo *AValNo, VNInfo *BValNo); 00191 00192 /// RemoveCopyByCommutingDef - We found a non-trivially-coalescable copy. 00193 /// If the source value number is defined by a commutable instruction and 00194 /// its other operand is coalesced to the copy dest register, see if we 00195 /// can transform the copy into a noop by commuting the definition. 00196 bool RemoveCopyByCommutingDef(LiveInterval &IntA, LiveInterval &IntB, 00197 MachineInstr *CopyMI); 00198 00199 /// TrimLiveIntervalToLastUse - If there is a last use in the same basic 00200 /// block as the copy instruction, trim the ive interval to the last use 00201 /// and return true. 00202 bool TrimLiveIntervalToLastUse(unsigned CopyIdx, 00203 MachineBasicBlock *CopyMBB, 00204 LiveInterval &li, const LiveRange *LR); 00205 00206 /// ReMaterializeTrivialDef - If the source of a copy is defined by a trivial 00207 /// computation, replace the copy by rematerialize the definition. 00208 bool ReMaterializeTrivialDef(LiveInterval &SrcInt, unsigned DstReg, 00209 MachineInstr *CopyMI); 00210 00211 /// TurnCopyIntoImpDef - If source of the specified copy is an implicit def, 00212 /// turn the copy into an implicit def. 00213 bool TurnCopyIntoImpDef(MachineBasicBlock::iterator &I, 00214 MachineBasicBlock *MBB, 00215 unsigned DstReg, unsigned SrcReg); 00216 00217 /// CanCoalesceWithImpDef - Returns true if the specified copy instruction 00218 /// from an implicit def to another register can be coalesced away. 00219 bool CanCoalesceWithImpDef(MachineInstr *CopyMI, 00220 LiveInterval &li, LiveInterval &ImpLi) const; 00221 00222 /// RemoveCopiesFromValNo - The specified value# is defined by an implicit 00223 /// def and it is being removed. Turn all copies from this value# into 00224 /// identity copies so they will be removed. 00225 void RemoveCopiesFromValNo(LiveInterval &li, VNInfo *VNI); 00226 00227 /// isWinToJoinCrossClass - Return true if it's profitable to coalesce 00228 /// two virtual registers from different register classes. 00229 bool isWinToJoinCrossClass(unsigned LargeReg, unsigned SmallReg, 00230 unsigned Threshold); 00231 00232 /// HasIncompatibleSubRegDefUse - If we are trying to coalesce a virtual 00233 /// register with a physical register, check if any of the virtual register 00234 /// operand is a sub-register use or def. If so, make sure it won't result 00235 /// in an illegal extract_subreg or insert_subreg instruction. 00236 bool HasIncompatibleSubRegDefUse(MachineInstr *CopyMI, 00237 unsigned VirtReg, unsigned PhysReg); 00238 00239 /// CanJoinExtractSubRegToPhysReg - Return true if it's possible to coalesce 00240 /// an extract_subreg where dst is a physical register, e.g. 00241 /// cl = EXTRACT_SUBREG reg1024, 1 00242 bool CanJoinExtractSubRegToPhysReg(unsigned DstReg, unsigned SrcReg, 00243 unsigned SubIdx, unsigned &RealDstReg); 00244 00245 /// CanJoinInsertSubRegToPhysReg - Return true if it's possible to coalesce 00246 /// an insert_subreg where src is a physical register, e.g. 00247 /// reg1024 = INSERT_SUBREG reg1024, c1, 0 00248 bool CanJoinInsertSubRegToPhysReg(unsigned DstReg, unsigned SrcReg, 00249 unsigned SubIdx, unsigned &RealDstReg); 00250 00251 /// RangeIsDefinedByCopyFromReg - Return true if the specified live range of 00252 /// the specified live interval is defined by a copy from the specified 00253 /// register. 00254 bool RangeIsDefinedByCopyFromReg(LiveInterval &li, LiveRange *LR, 00255 unsigned Reg); 00256 00257 /// isBackEdgeCopy - Return true if CopyMI is a back edge copy. 00258 /// 00259 bool isBackEdgeCopy(MachineInstr *CopyMI, unsigned DstReg) const; 00260 00261 /// UpdateRegDefsUses - Replace all defs and uses of SrcReg to DstReg and 00262 /// update the subregister number if it is not zero. If DstReg is a 00263 /// physical register and the existing subregister number of the def / use 00264 /// being updated is not zero, make sure to set it to the correct physical 00265 /// subregister. 00266 void UpdateRegDefsUses(unsigned SrcReg, unsigned DstReg, unsigned SubIdx); 00267 00268 /// RemoveDeadImpDef - Remove implicit_def instructions which are 00269 /// "re-defining" registers due to insert_subreg coalescing. e.g. 00270 void RemoveDeadImpDef(unsigned Reg, LiveInterval &LI); 00271 00272 /// RemoveUnnecessaryKills - Remove kill markers that are no longer accurate 00273 /// due to live range lengthening as the result of coalescing. 00274 void RemoveUnnecessaryKills(unsigned Reg, LiveInterval &LI); 00275 00276 /// ShortenDeadCopyLiveRange - Shorten a live range defined by a dead copy. 00277 /// Return true if live interval is removed. 00278 bool ShortenDeadCopyLiveRange(LiveInterval &li, MachineInstr *CopyMI); 00279 00280 /// ShortenDeadCopyLiveRange - Shorten a live range as it's artificially 00281 /// extended by a dead copy. Mark the last use (if any) of the val# as kill 00282 /// as ends the live range there. If there isn't another use, then this 00283 /// live range is dead. Return true if live interval is removed. 00284 bool ShortenDeadCopySrcLiveRange(LiveInterval &li, MachineInstr *CopyMI); 00285 00286 /// RemoveDeadDef - If a def of a live interval is now determined dead, 00287 /// remove the val# it defines. If the live interval becomes empty, remove 00288 /// it as well. 00289 bool RemoveDeadDef(LiveInterval &li, MachineInstr *DefMI); 00290 00291 /// lastRegisterUse - Returns the last use of the specific register between 00292 /// cycles Start and End or NULL if there are no uses. 00293 MachineOperand *lastRegisterUse(unsigned Start, unsigned End, unsigned Reg, 00294 unsigned &LastUseIdx) const; 00295 00296 void printRegName(unsigned reg) const; 00297 }; 00298 00299 } // End llvm namespace 00300 00301 #endif