21 #define DEBUG_TYPE "regalloc"
26 void LiveRangeCalc::resetLiveOutMap() {
59 assert(MRI && Indexes &&
"call reset() first");
67 if (!MO.isDef() && !MO.readsReg())
70 unsigned SubReg = MO.getSubReg();
71 if (LI.
hasSubRanges() || (SubReg != 0 && TrackSubRegs)) {
103 SubLRC.
reset(MF, Indexes, DomTree, Alloc);
104 SubLRC.extendToUses(S, Reg, S.LaneMask, &LI);
118 "Expect empty main liverange");
121 for (
const VNInfo *VNI : SR.valnos) {
131 assert(MRI && Indexes &&
"call reset() first");
147 bool IsSubRange = !Mask.
all();
158 if (!MO.readsReg() || (IsSubRange && MO.isDef()))
161 unsigned SubReg = MO.getSubReg();
167 if ((SLM & Mask).none())
176 assert(!MO.isDef() &&
"Cannot handle PHI def of partial register.");
182 bool isEarlyClobber =
false;
185 isEarlyClobber = MO.isEarlyClobber();
196 extend(LR, UseIdx, Reg, Undefs);
201 void LiveRangeCalc::updateFromLiveIns() {
203 for (
const LiveInBlock &
I : LiveIn) {
207 assert(
I.Value &&
"No live-in value found");
211 if (
I.Kill.isValid())
218 Map[MBB] = LiveOutPair(
I.Value,
nullptr);
221 Updater.
add(Start, End,
I.Value);
229 assert(Indexes &&
"Missing SlotIndexes");
230 assert(DomTree &&
"Missing dominator tree");
233 assert(UseMBB &&
"No MBB at Use");
237 if (EP.first !=
nullptr || EP.second)
244 if (findReachingDefs(LR, *UseMBB, Use, PhysReg, Undefs))
256 assert(Indexes &&
"Missing SlotIndexes");
257 assert(DomTree &&
"Missing dominator tree");
269 if (UndefOnEntry[BN])
274 DefOnEntry[S->getNumber()] =
true;
275 DefOnEntry[BN] =
true;
283 WorkList.
insert(
P->getNumber());
285 for (
unsigned i = 0; i != WorkList.
size(); ++i) {
287 unsigned N = WorkList[i];
290 const LiveOutPair &LOB = Map[&
B];
291 if (LOB.first !=
nullptr && LOB.first != &
UndefVNI)
292 return MarkDefined(B);
302 if (UB != LR.
begin()) {
304 if (Seg.
end > Begin) {
311 return MarkDefined(B);
317 if (UndefOnEntry[N] || LR.
isUndefIn(Undefs, Begin, End)) {
318 UndefOnEntry[
N] =
true;
322 return MarkDefined(B);
326 WorkList.
insert(
P->getNumber());
329 UndefOnEntry[BN] =
true;
342 bool UniqueVNI =
true;
345 bool FoundUndef =
false;
348 for (
unsigned i = 0; i != WorkList.
size(); ++i) {
355 <<
" does not have a corresponding definition on every path:\n";
358 errs() << Use <<
" " << *
MI;
367 <<
" needs to be live in to BB#" << MBB->
getNumber()
368 <<
", but is missing from the live-in list.\n";
376 if (Seen.
test(Pred->getNumber())) {
378 if (TheVNI && TheVNI != VNI)
392 FoundUndef |= EP.second;
395 if (TheVNI && TheVNI != VNI)
399 if (VNI || EP.second)
404 WorkList.push_back(Pred->getNumber());
412 FoundUndef |= (TheVNI ==
nullptr || TheVNI == &
UndefVNI);
413 if (Undefs.
size() > 0 && FoundUndef)
418 if (WorkList.
size() > 4)
425 for (
unsigned BN : WorkList) {
429 if (BN == UseMBBNum && Use.
isValid())
433 Updater.
add(Start, End, TheVNI);
441 std::tie(Entry, DidInsert) = EntryInfos.
insert(
446 Entry->second.first.resize(N);
447 Entry->second.second.resize(N);
449 BitVector &DefOnEntry = Entry->second.first;
450 BitVector &UndefOnEntry = Entry->second.second;
454 LiveIn.
reserve(WorkList.size());
455 for (
unsigned BN : WorkList) {
457 if (Undefs.
size() > 0 &&
458 !isDefOnEntry(LR, Undefs, *MBB, DefOnEntry, UndefOnEntry))
462 LiveIn.back().Kill = Use;
471 void LiveRangeCalc::updateSSA() {
472 assert(Indexes &&
"Missing SlotIndexes");
473 assert(DomTree &&
"Missing dominator tree");
481 for (LiveInBlock &
I : LiveIn) {
488 LiveOutPair IDomValue;
492 bool needPHI = !IDom || !Seen.
test(IDom->
getBlock()->getNumber());
501 if (IDomValue.first && IDomValue.first != &
UndefVNI &&
503 Map[IDom->
getBlock()].second = IDomValue.second =
508 LiveOutPair &
Value = Map[Pred];
509 if (!Value.first || Value.first == IDomValue.first)
524 if (DomTree->
dominates(IDom, Value.second)) {
534 LiveOutPair &LOP = Map[MBB];
539 assert(Alloc &&
"Need VNInfo allocator to create PHI-defs");
549 if (
I.Kill.isValid()) {
551 LR.
addSegment(LiveInterval::Segment(Start,
I.Kill, VNI));
554 LR.
addSegment(LiveInterval::Segment(Start, End, VNI));
555 LOP = LiveOutPair(VNI, Node);
557 }
else if (IDomValue.first && IDomValue.first != &
UndefVNI) {
559 I.Value = IDomValue.first;
562 if (
I.Kill.isValid())
567 if (LOP.first == IDomValue.first)
void add(LiveRange::Segment)
Add a segment to LR and coalesce when possible, just like LR.addSegment().
void resize(unsigned N, bool t=false)
resize - Grow or shrink the bitvector.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
bool verify(Pass *p=nullptr, const char *Banner=nullptr, bool AbortOnError=true) const
Run the current MachineFunction through the machine code verifier, useful for debugger use...
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
MachineInstr * getParent()
getParent - Return the instruction that this operand belongs to.
SlotIndex getInstructionIndex(const MachineInstr &MI) const
Returns the base index for the given instruction.
void refineSubRanges(BumpPtrAllocator &Allocator, LaneBitmask LaneMask, std::function< void(LiveInterval::SubRange &)> Mod)
Refines the subranges to support LaneMask.
SlotIndex def
The index of the defining instruction.
void createDeadDefs(LiveRange &LR, unsigned Reg)
createDeadDefs - Create a dead def in LI for every def operand of Reg.
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
MachineBasicBlock * getMBB() const
int getNumber() const
MachineBasicBlocks are uniquely numbered at the function level, unless they're not in a MachineFuncti...
void setLiveOutValue(MachineBasicBlock *MBB, VNInfo *VNI)
setLiveOutValue - Indicate that VNI is live out from MBB.
Segments::iterator iterator
static LaneBitmask getAll()
LiveInterval - This class represents the liveness of a register, or stack slot.
void constructMainRangeFromSubranges(LiveInterval &LI)
For live interval LI with correct SubRanges construct matching information for the main live range...
A live range for subregisters.
This represents a simple continuous liveness interval for a value.
LaneBitmask getSubRegIndexLaneMask(unsigned SubIdx) const
Return a bitmask representing the parts of a register that are covered by SubIdx. ...
SubRange * createSubRangeFrom(BumpPtrAllocator &Allocator, LaneBitmask LaneMask, const LiveRange &CopyFrom)
Like createSubRange() but the new range is filled with a copy of the liveness information in CopyFrom...
VNInfo - Value Number Information.
iterator end()
Get an iterator to the end of the SetVector.
size_type size() const
Determine the number of elements in the SetVector.
This class represents the liveness of a register, stack slot, etc.
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
void removeEmptySubRanges()
Removes all subranges without any segments (subranges without segments are not considered valid and s...
void clear()
clear - Removes all bits from the bitvector. Does not change capacity.
unsigned getNumBlockIDs() const
getNumBlockIDs - Return the number of MBB ID's allocated.
const TargetRegisterInfo * getTargetRegisterInfo() const
A Use represents the edge between a Value definition and its users.
iterator_range< subrange_iterator > subranges()
Reg
All possible values of the reg field in the ModR/M byte.
LLVM_NODISCARD bool empty() const
bool isUnused() const
Returns true if this value is unused.
MachineDomTreeNode * getNode(MachineBasicBlock *BB) const
getNode - return the (Post)DominatorTree node for the specified basic block.
bool insert(const value_type &X)
Insert a new element into the SetVector.
void addLiveInBlock(LiveRange &LR, MachineDomTreeNode *DomNode, SlotIndex Kill=SlotIndex())
addLiveInBlock - Add a block with an unknown live-in value.
iterator addSegment(Segment S)
Add the specified Segment to this range, merging segments as appropriate.
DomTreeNodeBase * getIDom() const
iterator begin()
Get an iterator to the beginning of the SetVector.
Base class for the actual dominator tree node.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
void extend(LiveRange &LR, SlotIndex Use, unsigned PhysReg, ArrayRef< SlotIndex > Undefs)
Extend the live range of LR to reach Use.
Printable PrintReg(unsigned Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubRegIdx=0)
Prints virtual and physical registers with or without a TRI instance.
size_t size() const
size - Get the array size.
SlotIndex getPrevSlot() const
Returns the previous slot in the index list.
iterator_range< def_iterator > def_operands(unsigned Reg) const
bool isEarlyClobber() const
void reset(const MachineFunction *MF, SlotIndexes *, MachineDominatorTree *, VNInfo::Allocator *)
reset - Prepare caches for a new set of non-overlapping live ranges.
void array_pod_sort(IteratorTy Start, IteratorTy End)
array_pod_sort - This sorts an array with the specified start and end extent.
bool isValid() const
Returns true if this is a valid index.
Allocate memory in an ever growing pool, as if by bump-pointer.
std::pair< VNInfo *, bool > extendInBlock(ArrayRef< SlotIndex > Undefs, SlotIndex StartIdx, SlotIndex Use)
Attempt to extend a value defined after StartIdx to include Use.
const MachineOperand & getOperand(unsigned i) const
void setDest(LiveRange *lr)
Select a different destination live range.
void resize(typename StorageT::size_type s)
static const unsigned End
iterator_range< pred_iterator > predecessors()
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
bool isPHIDef() const
Returns true if this value is defined by a PHI instruction (or was, PHI instructions may have been el...
MachineInstr * getInstructionFromIndex(SlotIndex index) const
Returns the instruction for the given index, or null if the given index has no instruction associated...
bool isUndefIn(ArrayRef< SlotIndex > Undefs, SlotIndex Begin, SlotIndex End) const
Returns true if there is an explicit "undef" between Begin End.
MachineOperand class - Representation of each machine instruction operand.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
bool test(unsigned Idx) const
MachineBasicBlock * getMBBFromIndex(SlotIndex index) const
Returns the basic block which the given index falls in.
MachineBasicBlock * getBlockNumbered(unsigned N) const
getBlockNumbered - MachineBasicBlocks are automatically numbered when they are inserted into the mach...
SlotIndex getMBBEndIdx(unsigned Num) const
Returns the last index in the given basic block number.
void calculate(LiveInterval &LI, bool TrackSubRegs)
Calculates liveness for the register specified in live interval LI.
Representation of each machine instruction.
static bool isPhysicalRegister(unsigned Reg)
Return true if the specified register number is in the physical register namespace.
LaneBitmask getMaxLaneMaskForVReg(unsigned Reg) const
Returns a mask covering all bits that can appear in lane masks of subregisters of the virtual registe...
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
VNInfo * createDeadDef(SlotIndex Def, VNInfo::Allocator &VNInfoAllocator)
createDeadDef - Make sure the range has a value defined at Def.
llvm::DenseMapBase< DenseMap< LiveRange *, std::pair< BitVector, BitVector >, DenseMapInfo< LiveRange * >, detail::DenseMapPair< LiveRange *, std::pair< BitVector, BitVector > > >, LiveRange *, std::pair< BitVector, BitVector >, DenseMapInfo< LiveRange * >, detail::DenseMapPair< LiveRange *, std::pair< BitVector, BitVector > > >::iterator DenseMapIterator< LiveRange *, std::pair< BitVector, BitVector >, DenseMapInfo< LiveRange * >, detail::DenseMapPair< LiveRange *, std::pair< BitVector, BitVector > > > iterator
constexpr bool all() const
static void createDeadDef(SlotIndexes &Indexes, VNInfo::Allocator &Alloc, LiveRange &LR, const MachineOperand &MO)
SlotIndex getMBBStartIdx(unsigned Num) const
Returns the first index in the given basic block number.
bool isLiveIn(MCPhysReg Reg, LaneBitmask LaneMask=LaneBitmask::getAll()) const
Return true if the specified register is in the live in set.
SlotIndex getRegSlot(bool EC=false) const
Returns the register use/def slot in the current instruction for a normal or early-clobber def...
Helper class for performant LiveRange bulk updates.
VNInfo * getNextValue(SlotIndex def, VNInfo::Allocator &VNInfoAllocator)
getNextValue - Create a new value number and return it.
iterator_range< reg_nodbg_iterator > reg_nodbg_operands(unsigned Reg) const
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static VNInfo UndefVNI(0xbad, SlotIndex())
LLVM Value Representation.
A vector that has set insertion semantics.
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
void computeSubRangeUndefs(SmallVectorImpl< SlotIndex > &Undefs, LaneBitmask LaneMask, const MachineRegisterInfo &MRI, const SlotIndexes &Indexes) const
For a given lane mask LaneMask, compute indexes at which the lane is marked undefined by subregister ...
bool isRegTiedToDefOperand(unsigned UseOpIdx, unsigned *DefOpIdx=nullptr) const
Return true if the use operand of the specified index is tied to a def operand.
bool hasSubRanges() const
Returns true if subregister liveness information is available.
bool dominates(const MachineDomTreeNode *A, const MachineDomTreeNode *B) const
SlotIndex - An opaque wrapper around machine indexes.
void calculateValues()
calculateValues - Calculate the value that will be live-in to each block added with addLiveInBlock...
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...
const std::pair< SlotIndex, SlotIndex > & getMBBRange(unsigned Num) const
Return the (start,end) range of the given basic block number.