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)
|
2009-12-19 07:05:23 +00:00
|
|
|
: 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;
|
2009-10-09 23:27:56 +00:00
|
|
|
AliasAnalysis *AA;
|
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;
|
2009-12-19 07:05:23 +00:00
|
|
|
}
|
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);
|
2010-06-24 18:15:01 +00:00
|
|
|
|
2007-06-08 17:18:56 +00:00
|
|
|
/// JoinIntervals - Attempt to join these two intervals. On failure, this
|
2010-06-24 18:15:01 +00:00
|
|
|
/// returns false. The output "SrcInt" will not have been modified, so we can
|
|
|
|
/// use this information below to update aliases.
|
|
|
|
bool JoinIntervals(CoalescerPair &CP);
|
2010-06-22 16:13:57 +00:00
|
|
|
|
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-11-03 23:52:08 +00:00
|
|
|
bool TrimLiveIntervalToLastUse(SlotIndex 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
|
|
|
|
2009-01-23 02:15:19 +00:00
|
|
|
/// isWinToJoinCrossClass - Return true if it's profitable to coalesce
|
|
|
|
/// two virtual registers from different register classes.
|
2010-04-21 00:44:22 +00:00
|
|
|
bool isWinToJoinCrossClass(unsigned SrcReg,
|
|
|
|
unsigned DstReg,
|
|
|
|
const TargetRegisterClass *SrcRC,
|
|
|
|
const TargetRegisterClass *DstRC,
|
|
|
|
const TargetRegisterClass *NewRC);
|
2008-06-19 01:39:21 +00:00
|
|
|
|
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.
|
2010-06-24 18:15:01 +00:00
|
|
|
void UpdateRegDefsUses(const CoalescerPair &CP);
|
2008-02-15 18:24:29 +00:00
|
|
|
|
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);
|
|
|
|
|
2010-06-24 18:15:01 +00:00
|
|
|
/// RemoveCopyFlag - If DstReg is no longer defined by CopyMI, clear the
|
|
|
|
/// VNInfo copy flag for DstReg and all aliases.
|
|
|
|
void RemoveCopyFlag(unsigned DstReg, const MachineInstr *CopyMI);
|
|
|
|
|
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-11-03 23:52:08 +00:00
|
|
|
MachineOperand *lastRegisterUse(SlotIndex Start, SlotIndex End,
|
|
|
|
unsigned Reg, SlotIndex &LastUseIdx) const;
|
2007-06-08 17:18:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // End llvm namespace
|
|
|
|
|
|
|
|
#endif
|