2010-10-14 23:49:52 +00:00
|
|
|
//===---- LiveRangeEdit.h - Basic tools for split and spill -----*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LiveRangeEdit class represents changes done to a virtual register when it
|
|
|
|
// is spilled or split.
|
|
|
|
//
|
|
|
|
// The parent register is never changed. Instead, a number of new virtual
|
|
|
|
// registers are created and added to the newRegs vector.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_CODEGEN_LIVERANGEEDIT_H
|
|
|
|
#define LLVM_CODEGEN_LIVERANGEEDIT_H
|
|
|
|
|
2011-05-06 18:00:02 +00:00
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
2013-06-21 18:33:17 +00:00
|
|
|
#include "llvm/ADT/SetVector.h"
|
2010-10-20 22:00:51 +00:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2011-05-06 18:00:02 +00:00
|
|
|
#include "llvm/CodeGen/LiveInterval.h"
|
2013-08-14 23:50:09 +00:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2012-04-02 22:22:53 +00:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2010-10-14 23:49:52 +00:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
2010-10-20 22:00:51 +00:00
|
|
|
class AliasAnalysis;
|
2010-10-14 23:49:52 +00:00
|
|
|
class LiveIntervals;
|
2013-06-17 19:00:36 +00:00
|
|
|
class MachineBlockFrequencyInfo;
|
2011-03-29 21:20:19 +00:00
|
|
|
class MachineLoopInfo;
|
2010-10-14 23:49:52 +00:00
|
|
|
class VirtRegMap;
|
|
|
|
|
2013-08-14 23:50:09 +00:00
|
|
|
class LiveRangeEdit : private MachineRegisterInfo::Delegate {
|
2011-03-09 00:57:29 +00:00
|
|
|
public:
|
|
|
|
/// Callback methods for LiveRangeEdit owners.
|
2011-12-20 02:50:00 +00:00
|
|
|
class Delegate {
|
|
|
|
virtual void anchor();
|
|
|
|
public:
|
2011-03-09 00:57:29 +00:00
|
|
|
/// Called immediately before erasing a dead machine instruction.
|
|
|
|
virtual void LRE_WillEraseInstruction(MachineInstr *MI) {}
|
2011-03-13 01:23:11 +00:00
|
|
|
|
|
|
|
/// Called when a virtual register is no longer used. Return false to defer
|
|
|
|
/// its deletion from LiveIntervals.
|
|
|
|
virtual bool LRE_CanEraseVirtReg(unsigned) { return true; }
|
|
|
|
|
2011-03-16 22:56:16 +00:00
|
|
|
/// Called before shrinking the live range of a virtual register.
|
|
|
|
virtual void LRE_WillShrinkVirtReg(unsigned) {}
|
2011-03-30 02:52:39 +00:00
|
|
|
|
|
|
|
/// Called after cloning a virtual register.
|
|
|
|
/// This is used for new registers representing connected components of Old.
|
|
|
|
virtual void LRE_DidCloneVirtReg(unsigned New, unsigned Old) {}
|
2011-03-16 22:56:16 +00:00
|
|
|
|
2011-03-09 04:02:15 +00:00
|
|
|
virtual ~Delegate() {}
|
2011-03-09 00:57:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
2012-05-19 05:25:46 +00:00
|
|
|
LiveInterval *Parent;
|
2013-08-14 23:50:04 +00:00
|
|
|
SmallVectorImpl<unsigned> &NewRegs;
|
2012-04-02 22:22:53 +00:00
|
|
|
MachineRegisterInfo &MRI;
|
|
|
|
LiveIntervals &LIS;
|
|
|
|
VirtRegMap *VRM;
|
|
|
|
const TargetInstrInfo &TII;
|
2012-05-18 22:10:15 +00:00
|
|
|
Delegate *const TheDelegate;
|
2010-10-14 23:49:52 +00:00
|
|
|
|
2012-05-18 22:10:15 +00:00
|
|
|
/// FirstNew - Index of the first register added to NewRegs.
|
|
|
|
const unsigned FirstNew;
|
2010-10-14 23:49:52 +00:00
|
|
|
|
2012-05-18 22:10:15 +00:00
|
|
|
/// ScannedRemattable - true when remattable values have been identified.
|
|
|
|
bool ScannedRemattable;
|
2010-10-20 22:00:51 +00:00
|
|
|
|
2012-05-18 22:10:15 +00:00
|
|
|
/// Remattable - Values defined by remattable instructions as identified by
|
2010-10-20 22:00:51 +00:00
|
|
|
/// tii.isTriviallyReMaterializable().
|
2012-05-18 22:10:15 +00:00
|
|
|
SmallPtrSet<const VNInfo*,4> Remattable;
|
2010-10-20 22:00:51 +00:00
|
|
|
|
2012-05-18 22:10:15 +00:00
|
|
|
/// Rematted - Values that were actually rematted, and so need to have their
|
2010-10-20 22:00:51 +00:00
|
|
|
/// live range trimmed or entirely removed.
|
2012-05-18 22:10:15 +00:00
|
|
|
SmallPtrSet<const VNInfo*,4> Rematted;
|
2010-10-20 22:00:51 +00:00
|
|
|
|
2012-05-18 22:10:15 +00:00
|
|
|
/// scanRemattable - Identify the Parent values that may rematerialize.
|
2012-04-02 22:22:53 +00:00
|
|
|
void scanRemattable(AliasAnalysis *aa);
|
2010-10-20 22:00:51 +00:00
|
|
|
|
|
|
|
/// allUsesAvailableAt - Return true if all registers used by OrigMI at
|
|
|
|
/// OrigIdx are also available with the same value at UseIdx.
|
|
|
|
bool allUsesAvailableAt(const MachineInstr *OrigMI, SlotIndex OrigIdx,
|
2013-03-18 23:40:46 +00:00
|
|
|
SlotIndex UseIdx) const;
|
2010-10-20 22:00:51 +00:00
|
|
|
|
2011-04-05 20:20:26 +00:00
|
|
|
/// foldAsLoad - If LI has a single use and a single def that can be folded as
|
|
|
|
/// a load, eliminate the register by folding the def into the use.
|
2012-04-02 22:22:53 +00:00
|
|
|
bool foldAsLoad(LiveInterval *LI, SmallVectorImpl<MachineInstr*> &Dead);
|
2011-04-05 20:20:26 +00:00
|
|
|
|
2013-06-21 18:33:17 +00:00
|
|
|
typedef SetVector<LiveInterval*,
|
|
|
|
SmallVector<LiveInterval*, 8>,
|
|
|
|
SmallPtrSet<LiveInterval*, 8> > ToShrinkSet;
|
|
|
|
/// Helper for eliminateDeadDefs.
|
|
|
|
void eliminateDeadDef(MachineInstr *MI, ToShrinkSet &ToShrink);
|
|
|
|
|
2013-08-14 23:50:09 +00:00
|
|
|
/// MachineRegisterInfo callback to notify when new virtual
|
|
|
|
/// registers are created.
|
2014-03-07 09:26:03 +00:00
|
|
|
void MRI_NoteNewVirtualRegister(unsigned VReg) override;
|
2013-08-14 23:50:09 +00:00
|
|
|
|
2010-10-14 23:49:52 +00:00
|
|
|
public:
|
|
|
|
/// Create a LiveRangeEdit for breaking down parent into smaller pieces.
|
|
|
|
/// @param parent The register being spilled or split.
|
|
|
|
/// @param newRegs List to receive any new registers created. This needn't be
|
|
|
|
/// empty initially, any existing registers are ignored.
|
2012-04-03 00:28:46 +00:00
|
|
|
/// @param MF The MachineFunction the live range edit is taking place in.
|
|
|
|
/// @param lis The collection of all live intervals in this function.
|
|
|
|
/// @param vrm Map of virtual registers to physical registers for this
|
|
|
|
/// function. If NULL, no virtual register map updates will
|
|
|
|
/// be done. This could be the case if called before Regalloc.
|
2012-05-19 05:25:46 +00:00
|
|
|
LiveRangeEdit(LiveInterval *parent,
|
2013-08-14 23:50:04 +00:00
|
|
|
SmallVectorImpl<unsigned> &newRegs,
|
2012-04-02 22:22:53 +00:00
|
|
|
MachineFunction &MF,
|
|
|
|
LiveIntervals &lis,
|
|
|
|
VirtRegMap *vrm,
|
2014-04-14 00:51:57 +00:00
|
|
|
Delegate *delegate = nullptr)
|
2012-05-18 22:10:15 +00:00
|
|
|
: Parent(parent), NewRegs(newRegs),
|
2012-04-02 22:22:53 +00:00
|
|
|
MRI(MF.getRegInfo()), LIS(lis), VRM(vrm),
|
|
|
|
TII(*MF.getTarget().getInstrInfo()),
|
2012-05-18 22:10:15 +00:00
|
|
|
TheDelegate(delegate),
|
|
|
|
FirstNew(newRegs.size()),
|
2013-08-14 23:50:09 +00:00
|
|
|
ScannedRemattable(false) { MRI.setDelegate(this); }
|
|
|
|
|
|
|
|
~LiveRangeEdit() { MRI.resetDelegate(this); }
|
2010-10-14 23:49:52 +00:00
|
|
|
|
2012-05-19 05:25:46 +00:00
|
|
|
LiveInterval &getParent() const {
|
|
|
|
assert(Parent && "No parent LiveInterval");
|
|
|
|
return *Parent;
|
|
|
|
}
|
|
|
|
unsigned getReg() const { return getParent().reg; }
|
2010-10-14 23:49:52 +00:00
|
|
|
|
|
|
|
/// Iterator for accessing the new registers added by this edit.
|
2013-08-14 23:50:04 +00:00
|
|
|
typedef SmallVectorImpl<unsigned>::const_iterator iterator;
|
2012-05-18 22:10:15 +00:00
|
|
|
iterator begin() const { return NewRegs.begin()+FirstNew; }
|
|
|
|
iterator end() const { return NewRegs.end(); }
|
|
|
|
unsigned size() const { return NewRegs.size()-FirstNew; }
|
2011-02-03 06:18:29 +00:00
|
|
|
bool empty() const { return size() == 0; }
|
2013-08-14 23:50:04 +00:00
|
|
|
unsigned get(unsigned idx) const { return NewRegs[idx+FirstNew]; }
|
2010-10-14 23:49:52 +00:00
|
|
|
|
2013-08-14 23:50:04 +00:00
|
|
|
ArrayRef<unsigned> regs() const {
|
2012-05-18 22:10:15 +00:00
|
|
|
return makeArrayRef(NewRegs).slice(FirstNew);
|
2011-05-06 18:00:02 +00:00
|
|
|
}
|
|
|
|
|
2013-08-14 23:50:16 +00:00
|
|
|
/// createEmptyIntervalFrom - Create a new empty interval based on OldReg.
|
|
|
|
LiveInterval &createEmptyIntervalFrom(unsigned OldReg);
|
|
|
|
|
2011-03-26 22:16:41 +00:00
|
|
|
/// createFrom - Create a new virtual register based on OldReg.
|
2013-08-14 23:50:16 +00:00
|
|
|
unsigned createFrom(unsigned OldReg);
|
2011-03-26 22:16:41 +00:00
|
|
|
|
2011-03-17 20:37:07 +00:00
|
|
|
/// create - Create a new register with the same class and original slot as
|
2010-10-15 00:16:55 +00:00
|
|
|
/// parent.
|
2013-08-14 23:50:16 +00:00
|
|
|
LiveInterval &createEmptyInterval() {
|
|
|
|
return createEmptyIntervalFrom(getReg());
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned create() {
|
2012-04-02 22:22:53 +00:00
|
|
|
return createFrom(getReg());
|
2011-03-17 20:37:07 +00:00
|
|
|
}
|
2010-10-14 23:49:52 +00:00
|
|
|
|
2010-10-20 22:00:51 +00:00
|
|
|
/// anyRematerializable - Return true if any parent values may be
|
|
|
|
/// rematerializable.
|
2011-03-07 22:42:16 +00:00
|
|
|
/// This function must be called before any rematerialization is attempted.
|
2012-04-02 22:22:53 +00:00
|
|
|
bool anyRematerializable(AliasAnalysis*);
|
2010-10-20 22:00:51 +00:00
|
|
|
|
2011-03-29 03:12:02 +00:00
|
|
|
/// checkRematerializable - Manually add VNI to the list of rematerializable
|
|
|
|
/// values if DefMI may be rematerializable.
|
2011-04-20 22:14:20 +00:00
|
|
|
bool checkRematerializable(VNInfo *VNI, const MachineInstr *DefMI,
|
2012-04-02 22:22:53 +00:00
|
|
|
AliasAnalysis*);
|
2011-03-29 03:12:02 +00:00
|
|
|
|
2010-10-20 22:00:51 +00:00
|
|
|
/// Remat - Information needed to rematerialize at a specific location.
|
|
|
|
struct Remat {
|
|
|
|
VNInfo *ParentVNI; // parent_'s value at the remat location.
|
|
|
|
MachineInstr *OrigMI; // Instruction defining ParentVNI.
|
2014-04-14 00:51:57 +00:00
|
|
|
explicit Remat(VNInfo *ParentVNI) : ParentVNI(ParentVNI), OrigMI(nullptr) {}
|
2010-10-20 22:00:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/// canRematerializeAt - Determine if ParentVNI can be rematerialized at
|
|
|
|
/// UseIdx. It is assumed that parent_.getVNINfoAt(UseIdx) == ParentVNI.
|
|
|
|
/// When cheapAsAMove is set, only cheap remats are allowed.
|
2010-11-10 01:05:12 +00:00
|
|
|
bool canRematerializeAt(Remat &RM,
|
|
|
|
SlotIndex UseIdx,
|
2012-04-02 22:22:53 +00:00
|
|
|
bool cheapAsAMove);
|
2010-10-20 22:00:51 +00:00
|
|
|
|
|
|
|
/// rematerializeAt - Rematerialize RM.ParentVNI into DestReg by inserting an
|
|
|
|
/// instruction into MBB before MI. The new instruction is mapped, but
|
|
|
|
/// liveness is not updated.
|
|
|
|
/// Return the SlotIndex of the new instruction.
|
|
|
|
SlotIndex rematerializeAt(MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator MI,
|
|
|
|
unsigned DestReg,
|
|
|
|
const Remat &RM,
|
2011-05-02 05:29:58 +00:00
|
|
|
const TargetRegisterInfo&,
|
|
|
|
bool Late = false);
|
2010-10-14 23:49:52 +00:00
|
|
|
|
2010-12-18 03:04:14 +00:00
|
|
|
/// markRematerialized - explicitly mark a value as rematerialized after doing
|
|
|
|
/// it manually.
|
2011-03-02 23:05:19 +00:00
|
|
|
void markRematerialized(const VNInfo *ParentVNI) {
|
2012-05-18 22:10:15 +00:00
|
|
|
Rematted.insert(ParentVNI);
|
2010-12-18 03:04:14 +00:00
|
|
|
}
|
|
|
|
|
2010-10-20 22:00:51 +00:00
|
|
|
/// didRematerialize - Return true if ParentVNI was rematerialized anywhere.
|
2011-03-02 23:05:19 +00:00
|
|
|
bool didRematerialize(const VNInfo *ParentVNI) const {
|
2012-05-18 22:10:15 +00:00
|
|
|
return Rematted.count(ParentVNI);
|
2010-10-20 22:00:51 +00:00
|
|
|
}
|
2011-03-08 22:46:11 +00:00
|
|
|
|
2011-03-13 01:23:11 +00:00
|
|
|
/// eraseVirtReg - Notify the delegate that Reg is no longer in use, and try
|
|
|
|
/// to erase it from LIS.
|
2012-04-02 22:22:53 +00:00
|
|
|
void eraseVirtReg(unsigned Reg);
|
2011-03-13 01:23:11 +00:00
|
|
|
|
2011-03-08 22:46:11 +00:00
|
|
|
/// eliminateDeadDefs - Try to delete machine instructions that are now dead
|
|
|
|
/// (allDefsAreDead returns true). This may cause live intervals to be trimmed
|
|
|
|
/// and further dead efs to be eliminated.
|
2011-12-12 22:16:27 +00:00
|
|
|
/// RegsBeingSpilled lists registers currently being spilled by the register
|
|
|
|
/// allocator. These registers should not be split into new intervals
|
|
|
|
/// as currently those new intervals are not guaranteed to spill.
|
2011-03-08 22:46:11 +00:00
|
|
|
void eliminateDeadDefs(SmallVectorImpl<MachineInstr*> &Dead,
|
2013-05-05 00:40:33 +00:00
|
|
|
ArrayRef<unsigned> RegsBeingSpilled = None);
|
2011-03-08 22:46:11 +00:00
|
|
|
|
2011-03-29 21:20:19 +00:00
|
|
|
/// calculateRegClassAndHint - Recompute register class and hint for each new
|
|
|
|
/// register.
|
2012-04-02 22:22:53 +00:00
|
|
|
void calculateRegClassAndHint(MachineFunction&,
|
2013-06-17 19:00:36 +00:00
|
|
|
const MachineLoopInfo&,
|
|
|
|
const MachineBlockFrequencyInfo&);
|
2010-10-14 23:49:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|