2007-06-08 17:18:56 +00:00
|
|
|
//===-- SimpleRegisterCoalescing.h - Register Coalescing --------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-06-08 17:18:56 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements a simple register copy coalescing phase.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_CODEGEN_SIMPLE_REGISTER_COALESCING_H
|
|
|
|
#define LLVM_CODEGEN_SIMPLE_REGISTER_COALESCING_H
|
|
|
|
|
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
|
|
|
#include "llvm/CodeGen/LiveIntervalAnalysis.h"
|
2007-09-06 16:18:45 +00:00
|
|
|
#include "llvm/CodeGen/RegisterCoalescer.h"
|
2007-06-08 17:18:56 +00:00
|
|
|
#include "llvm/ADT/BitVector.h"
|
|
|
|
|
|
|
|
namespace llvm {
|
2007-11-06 08:52:21 +00:00
|
|
|
class SimpleRegisterCoalescing;
|
2007-06-08 17:18:56 +00:00
|
|
|
class LiveVariables;
|
2008-02-10 18:45:23 +00:00
|
|
|
class TargetRegisterInfo;
|
2007-06-08 17:18:56 +00:00
|
|
|
class TargetInstrInfo;
|
|
|
|
class VirtRegMap;
|
2007-12-11 02:09:15 +00:00
|
|
|
class MachineLoopInfo;
|
2007-11-06 08:52:21 +00:00
|
|
|
|
|
|
|
/// CopyRec - Representation for copy instructions in coalescer queue.
|
|
|
|
///
|
|
|
|
struct CopyRec {
|
|
|
|
MachineInstr *MI;
|
|
|
|
unsigned LoopDepth;
|
2009-09-12 02:14:41 +00:00
|
|
|
CopyRec(MachineInstr *mi, unsigned depth)
|
|
|
|
: MI(mi), LoopDepth(depth) {};
|
2007-11-06 08:52:21 +00:00
|
|
|
};
|
2007-06-08 17:18:56 +00:00
|
|
|
|
2007-09-06 16:18:45 +00:00
|
|
|
class SimpleRegisterCoalescing : public MachineFunctionPass,
|
|
|
|
public RegisterCoalescer {
|
2007-06-08 17:18:56 +00:00
|
|
|
MachineFunction* mf_;
|
2008-02-15 18:24:29 +00:00
|
|
|
MachineRegisterInfo* mri_;
|
2007-06-08 17:18:56 +00:00
|
|
|
const TargetMachine* tm_;
|
2008-02-10 18:45:23 +00:00
|
|
|
const TargetRegisterInfo* tri_;
|
2007-06-08 17:18:56 +00:00
|
|
|
const TargetInstrInfo* tii_;
|
|
|
|
LiveIntervals *li_;
|
2007-12-11 02:09:15 +00:00
|
|
|
const MachineLoopInfo* loopInfo;
|
2007-06-08 17:18:56 +00:00
|
|
|
|
|
|
|
BitVector allocatableRegs_;
|
|
|
|
DenseMap<const TargetRegisterClass*, BitVector> allocatableRCRegs_;
|
|
|
|
|
2007-11-05 17:41:38 +00:00
|
|
|
/// JoinedCopies - Keep track of copies eliminated due to coalescing.
|
|
|
|
///
|
|
|
|
SmallPtrSet<MachineInstr*, 32> JoinedCopies;
|
|
|
|
|
2008-08-30 09:09:33 +00:00
|
|
|
/// ReMatCopies - Keep track of copies eliminated due to remat.
|
|
|
|
///
|
|
|
|
SmallPtrSet<MachineInstr*, 32> ReMatCopies;
|
|
|
|
|
2008-09-19 17:38:47 +00:00
|
|
|
/// ReMatDefs - Keep track of definition instructions which have
|
|
|
|
/// been remat'ed.
|
|
|
|
SmallPtrSet<MachineInstr*, 8> ReMatDefs;
|
|
|
|
|
2007-06-08 17:18:56 +00:00
|
|
|
public:
|
|
|
|
static char ID; // Pass identifcation, replacement for typeid
|
2008-09-04 17:05:41 +00:00
|
|
|
SimpleRegisterCoalescing() : MachineFunctionPass(&ID) {}
|
2007-06-08 17:18:56 +00:00
|
|
|
|
|
|
|
struct InstrSlots {
|
|
|
|
enum {
|
|
|
|
LOAD = 0,
|
|
|
|
USE = 1,
|
|
|
|
DEF = 2,
|
|
|
|
STORE = 3,
|
|
|
|
NUM = 4
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const;
|
|
|
|
virtual void releaseMemory();
|
|
|
|
|
|
|
|
/// runOnMachineFunction - pass entry point
|
|
|
|
virtual bool runOnMachineFunction(MachineFunction&);
|
|
|
|
|
2007-09-06 16:18:45 +00:00
|
|
|
bool coalesceFunction(MachineFunction &mf, RegallocQuery &) {
|
|
|
|
// This runs as an independent pass, so don't do anything.
|
2007-11-06 08:52:21 +00:00
|
|
|
return false;
|
2007-09-06 16:18:45 +00:00
|
|
|
};
|
|
|
|
|
2007-06-08 17:18:56 +00:00
|
|
|
/// print - Implement the dump method.
|
2009-08-23 06:03:38 +00:00
|
|
|
virtual void print(raw_ostream &O, const Module* = 0) const;
|
2007-06-08 17:18:56 +00:00
|
|
|
|
2007-11-06 08:52:21 +00:00
|
|
|
private:
|
2007-06-08 17:18:56 +00:00
|
|
|
/// joinIntervals - join compatible live intervals
|
|
|
|
void joinIntervals();
|
|
|
|
|
2007-07-09 12:20:30 +00:00
|
|
|
/// CopyCoalesceInMBB - Coalesce copies in the specified MBB, putting
|
2007-07-09 12:00:59 +00:00
|
|
|
/// copies that cannot yet be coalesced into the "TryAgain" list.
|
|
|
|
void CopyCoalesceInMBB(MachineBasicBlock *MBB,
|
2007-10-16 08:04:24 +00:00
|
|
|
std::vector<CopyRec> &TryAgain);
|
2007-06-08 17:18:56 +00:00
|
|
|
|
|
|
|
/// JoinCopy - Attempt to join intervals corresponding to SrcReg/DstReg,
|
|
|
|
/// which are the src/dst of the copy instruction CopyMI. This returns true
|
2007-11-01 06:22:48 +00:00
|
|
|
/// if the copy was successfully coalesced away. If it is not currently
|
|
|
|
/// possible to coalesce this interval, but it may be possible if other
|
|
|
|
/// things get coalesced, then it returns true by reference in 'Again'.
|
2008-02-13 03:01:43 +00:00
|
|
|
bool JoinCopy(CopyRec &TheCopy, bool &Again);
|
2007-06-08 17:18:56 +00:00
|
|
|
|
|
|
|
/// JoinIntervals - Attempt to join these two intervals. On failure, this
|
|
|
|
/// returns false. Otherwise, if one of the intervals being joined is a
|
|
|
|
/// physreg, this method always canonicalizes DestInt to be it. The output
|
|
|
|
/// "SrcInt" will not have been modified, so we can use this information
|
|
|
|
/// below to update aliases.
|
2007-08-28 08:28:51 +00:00
|
|
|
bool JoinIntervals(LiveInterval &LHS, LiveInterval &RHS, bool &Swapped);
|
2007-06-08 17:18:56 +00:00
|
|
|
|
|
|
|
/// SimpleJoin - Attempt to join the specified interval into this one. The
|
|
|
|
/// caller of this method must guarantee that the RHS only contains a single
|
|
|
|
/// value number and that the RHS is not defined by a copy from this
|
|
|
|
/// interval. This returns false if the intervals are not joinable, or it
|
|
|
|
/// joins them and returns true.
|
|
|
|
bool SimpleJoin(LiveInterval &LHS, LiveInterval &RHS);
|
|
|
|
|
2008-06-19 01:39:21 +00:00
|
|
|
/// Return true if the two specified registers belong to different register
|
2009-01-23 02:15:19 +00:00
|
|
|
/// classes. The registers may be either phys or virt regs.
|
|
|
|
bool differingRegisterClasses(unsigned RegA, unsigned RegB) const;
|
2007-06-08 17:18:56 +00:00
|
|
|
|
|
|
|
|
2008-02-15 18:24:29 +00:00
|
|
|
/// AdjustCopiesBackFrom - We found a non-trivially-coalescable copy. If
|
|
|
|
/// the source value number is defined by a copy from the destination reg
|
|
|
|
/// see if we can merge these two destination reg valno# into a single
|
|
|
|
/// value number, eliminating a copy.
|
2007-06-08 17:18:56 +00:00
|
|
|
bool AdjustCopiesBackFrom(LiveInterval &IntA, LiveInterval &IntB,
|
|
|
|
MachineInstr *CopyMI);
|
|
|
|
|
2008-02-16 02:32:17 +00:00
|
|
|
/// HasOtherReachingDefs - Return true if there are definitions of IntB
|
|
|
|
/// other than BValNo val# that can reach uses of AValno val# of IntA.
|
|
|
|
bool HasOtherReachingDefs(LiveInterval &IntA, LiveInterval &IntB,
|
|
|
|
VNInfo *AValNo, VNInfo *BValNo);
|
|
|
|
|
2008-02-15 18:24:29 +00:00
|
|
|
/// RemoveCopyByCommutingDef - We found a non-trivially-coalescable copy.
|
|
|
|
/// If the source value number is defined by a commutable instruction and
|
|
|
|
/// its other operand is coalesced to the copy dest register, see if we
|
|
|
|
/// can transform the copy into a noop by commuting the definition.
|
2008-02-13 03:01:43 +00:00
|
|
|
bool RemoveCopyByCommutingDef(LiveInterval &IntA, LiveInterval &IntB,
|
|
|
|
MachineInstr *CopyMI);
|
|
|
|
|
2009-02-05 08:45:04 +00:00
|
|
|
/// TrimLiveIntervalToLastUse - If there is a last use in the same basic
|
|
|
|
/// block as the copy instruction, trim the ive interval to the last use
|
|
|
|
/// and return true.
|
2009-09-04 20:41:11 +00:00
|
|
|
bool TrimLiveIntervalToLastUse(MachineInstrIndex CopyIdx,
|
2009-02-05 08:45:04 +00:00
|
|
|
MachineBasicBlock *CopyMBB,
|
|
|
|
LiveInterval &li, const LiveRange *LR);
|
|
|
|
|
|
|
|
/// ReMaterializeTrivialDef - If the source of a copy is defined by a trivial
|
|
|
|
/// computation, replace the copy by rematerialize the definition.
|
2008-08-30 09:09:33 +00:00
|
|
|
bool ReMaterializeTrivialDef(LiveInterval &SrcInt, unsigned DstReg,
|
2009-07-16 09:20:10 +00:00
|
|
|
unsigned DstSubIdx, MachineInstr *CopyMI);
|
2008-08-30 09:09:33 +00:00
|
|
|
|
2008-04-09 20:57:25 +00:00
|
|
|
/// CanCoalesceWithImpDef - Returns true if the specified copy instruction
|
|
|
|
/// from an implicit def to another register can be coalesced away.
|
|
|
|
bool CanCoalesceWithImpDef(MachineInstr *CopyMI,
|
|
|
|
LiveInterval &li, LiveInterval &ImpLi) const;
|
|
|
|
|
2009-06-16 07:15:05 +00:00
|
|
|
/// TurnCopiesFromValNoToImpDefs - The specified value# is defined by an
|
|
|
|
/// implicit_def and it is being removed. Turn all copies from this value#
|
|
|
|
/// into implicit_defs.
|
|
|
|
void TurnCopiesFromValNoToImpDefs(LiveInterval &li, VNInfo *VNI);
|
2008-04-09 20:57:25 +00:00
|
|
|
|
2009-04-30 18:39:57 +00:00
|
|
|
/// isWinToJoinVRWithSrcPhysReg - Return true if it's worth while to join a
|
|
|
|
/// a virtual destination register with physical source register.
|
|
|
|
bool isWinToJoinVRWithSrcPhysReg(MachineInstr *CopyMI,
|
|
|
|
MachineBasicBlock *CopyMBB,
|
|
|
|
LiveInterval &DstInt, LiveInterval &SrcInt);
|
|
|
|
|
|
|
|
/// isWinToJoinVRWithDstPhysReg - Return true if it's worth while to join a
|
|
|
|
/// copy from a virtual source register to a physical destination register.
|
|
|
|
bool isWinToJoinVRWithDstPhysReg(MachineInstr *CopyMI,
|
|
|
|
MachineBasicBlock *CopyMBB,
|
|
|
|
LiveInterval &DstInt, LiveInterval &SrcInt);
|
|
|
|
|
2009-01-23 02:15:19 +00:00
|
|
|
/// isWinToJoinCrossClass - Return true if it's profitable to coalesce
|
|
|
|
/// two virtual registers from different register classes.
|
|
|
|
bool isWinToJoinCrossClass(unsigned LargeReg, unsigned SmallReg,
|
|
|
|
unsigned Threshold);
|
2008-06-19 01:39:21 +00:00
|
|
|
|
2008-09-11 20:07:10 +00:00
|
|
|
/// HasIncompatibleSubRegDefUse - If we are trying to coalesce a virtual
|
|
|
|
/// register with a physical register, check if any of the virtual register
|
|
|
|
/// operand is a sub-register use or def. If so, make sure it won't result
|
|
|
|
/// in an illegal extract_subreg or insert_subreg instruction.
|
|
|
|
bool HasIncompatibleSubRegDefUse(MachineInstr *CopyMI,
|
|
|
|
unsigned VirtReg, unsigned PhysReg);
|
|
|
|
|
2009-01-20 06:44:16 +00:00
|
|
|
/// CanJoinExtractSubRegToPhysReg - Return true if it's possible to coalesce
|
|
|
|
/// an extract_subreg where dst is a physical register, e.g.
|
|
|
|
/// cl = EXTRACT_SUBREG reg1024, 1
|
2009-01-23 02:15:19 +00:00
|
|
|
bool CanJoinExtractSubRegToPhysReg(unsigned DstReg, unsigned SrcReg,
|
2009-01-20 06:44:16 +00:00
|
|
|
unsigned SubIdx, unsigned &RealDstReg);
|
|
|
|
|
|
|
|
/// CanJoinInsertSubRegToPhysReg - Return true if it's possible to coalesce
|
|
|
|
/// an insert_subreg where src is a physical register, e.g.
|
|
|
|
/// reg1024 = INSERT_SUBREG reg1024, c1, 0
|
2009-01-23 02:15:19 +00:00
|
|
|
bool CanJoinInsertSubRegToPhysReg(unsigned DstReg, unsigned SrcReg,
|
2009-01-20 06:44:16 +00:00
|
|
|
unsigned SubIdx, unsigned &RealDstReg);
|
|
|
|
|
2008-04-09 20:57:25 +00:00
|
|
|
/// RangeIsDefinedByCopyFromReg - Return true if the specified live range of
|
|
|
|
/// the specified live interval is defined by a copy from the specified
|
|
|
|
/// register.
|
|
|
|
bool RangeIsDefinedByCopyFromReg(LiveInterval &li, LiveRange *LR,
|
|
|
|
unsigned Reg);
|
|
|
|
|
2008-02-15 18:24:29 +00:00
|
|
|
/// UpdateRegDefsUses - Replace all defs and uses of SrcReg to DstReg and
|
|
|
|
/// update the subregister number if it is not zero. If DstReg is a
|
|
|
|
/// physical register and the existing subregister number of the def / use
|
|
|
|
/// being updated is not zero, make sure to set it to the correct physical
|
|
|
|
/// subregister.
|
|
|
|
void UpdateRegDefsUses(unsigned SrcReg, unsigned DstReg, unsigned SubIdx);
|
|
|
|
|
2008-03-10 08:11:32 +00:00
|
|
|
/// RemoveUnnecessaryKills - Remove kill markers that are no longer accurate
|
|
|
|
/// due to live range lengthening as the result of coalescing.
|
|
|
|
void RemoveUnnecessaryKills(unsigned Reg, LiveInterval &LI);
|
|
|
|
|
2008-03-18 08:26:47 +00:00
|
|
|
/// ShortenDeadCopyLiveRange - Shorten a live range defined by a dead copy.
|
2008-04-16 20:24:25 +00:00
|
|
|
/// Return true if live interval is removed.
|
|
|
|
bool ShortenDeadCopyLiveRange(LiveInterval &li, MachineInstr *CopyMI);
|
2008-03-18 08:26:47 +00:00
|
|
|
|
2008-03-05 22:09:42 +00:00
|
|
|
/// ShortenDeadCopyLiveRange - Shorten a live range as it's artificially
|
|
|
|
/// extended by a dead copy. Mark the last use (if any) of the val# as kill
|
|
|
|
/// as ends the live range there. If there isn't another use, then this
|
2008-04-16 20:24:25 +00:00
|
|
|
/// live range is dead. Return true if live interval is removed.
|
|
|
|
bool ShortenDeadCopySrcLiveRange(LiveInterval &li, MachineInstr *CopyMI);
|
2008-03-05 22:09:42 +00:00
|
|
|
|
2008-10-27 23:21:01 +00:00
|
|
|
/// RemoveDeadDef - If a def of a live interval is now determined dead,
|
|
|
|
/// remove the val# it defines. If the live interval becomes empty, remove
|
|
|
|
/// it as well.
|
|
|
|
bool RemoveDeadDef(LiveInterval &li, MachineInstr *DefMI);
|
|
|
|
|
2007-06-08 17:18:56 +00:00
|
|
|
/// lastRegisterUse - Returns the last use of the specific register between
|
2008-02-15 18:24:29 +00:00
|
|
|
/// cycles Start and End or NULL if there are no uses.
|
2009-09-04 20:41:11 +00:00
|
|
|
MachineOperand *lastRegisterUse(MachineInstrIndex Start, MachineInstrIndex End,
|
|
|
|
unsigned Reg, MachineInstrIndex &LastUseIdx) const;
|
2007-06-08 17:18:56 +00:00
|
|
|
|
|
|
|
void printRegName(unsigned reg) const;
|
2009-09-04 20:41:11 +00:00
|
|
|
|
|
|
|
/// Returns true if the given live interval is zero length.
|
|
|
|
bool isZeroLengthInterval(LiveInterval *li) const;
|
2007-06-08 17:18:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // End llvm namespace
|
|
|
|
|
|
|
|
#endif
|