2017-09-23 07:46:57 +08:00
|
|
|
//===- InterferenceCache.cpp - Caching per-block interference -------------===//
|
2011-04-02 14:03:35 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// InterferenceCache remembers per-block interference in LiveIntervalUnions.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "InterferenceCache.h"
|
2017-09-23 07:46:57 +08:00
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
|
|
|
#include "llvm/CodeGen/LiveInterval.h"
|
|
|
|
#include "llvm/CodeGen/LiveIntervalUnion.h"
|
2017-12-13 10:51:04 +08:00
|
|
|
#include "llvm/CodeGen/LiveIntervals.h"
|
2017-09-23 07:46:57 +08:00
|
|
|
#include "llvm/CodeGen/MachineBasicBlock.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
|
|
|
#include "llvm/CodeGen/MachineOperand.h"
|
|
|
|
#include "llvm/CodeGen/SlotIndexes.h"
|
2017-11-17 09:07:10 +08:00
|
|
|
#include "llvm/CodeGen/TargetRegisterInfo.h"
|
2017-09-23 07:46:57 +08:00
|
|
|
#include "llvm/MC/MCRegisterInfo.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2017-09-23 07:46:57 +08:00
|
|
|
#include <cassert>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <tuple>
|
2011-04-02 14:03:35 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 10:02:50 +08:00
|
|
|
#define DEBUG_TYPE "regalloc"
|
|
|
|
|
2011-07-23 11:10:17 +08:00
|
|
|
// Static member used for null interference cursors.
|
2015-03-09 00:07:39 +08:00
|
|
|
const InterferenceCache::BlockInterference
|
|
|
|
InterferenceCache::Cursor::NoInterference;
|
2011-07-23 11:10:17 +08:00
|
|
|
|
2014-02-06 17:23:24 +08:00
|
|
|
// Initializes PhysRegEntries (instead of a SmallVector, PhysRegEntries is a
|
|
|
|
// buffer of size NumPhysRegs to speed up alloc/clear for targets with large
|
|
|
|
// reg files). Calloced memory is used for good form, and quites tools like
|
|
|
|
// Valgrind too, but zero initialized memory is not required by the algorithm:
|
|
|
|
// this is because PhysRegEntries works like a SparseSet and its entries are
|
|
|
|
// only valid when there is a corresponding CacheEntries assignment. There is
|
|
|
|
// also support for when pass managers are reused for targets with different
|
|
|
|
// numbers of PhysRegs: in this case PhysRegEntries is freed and reinitialized.
|
|
|
|
void InterferenceCache::reinitPhysRegEntries() {
|
|
|
|
if (PhysRegEntriesCount == TRI->getNumRegs()) return;
|
|
|
|
free(PhysRegEntries);
|
|
|
|
PhysRegEntriesCount = TRI->getNumRegs();
|
Report fatal error in the case of out of memory
This is the second part of recommit of r325224. The previous part was
committed in r325426, which deals with C++ memory allocation. Solution
for C memory allocation involved functions `llvm::malloc` and similar.
This was a fragile solution because it caused ambiguity errors in some
cases. In this commit the new functions have names like `llvm::safe_malloc`.
The relevant part of original comment is below, updated for new function
names.
Analysis of fails in the case of out of memory errors can be tricky on
Windows. Such error emerges at the point where memory allocation function
fails, but manifests itself when null pointer is used. These two points
may be distant from each other. Besides, next runs may not exhibit
allocation error.
In some cases memory is allocated by a call to some of C allocation
functions, malloc, calloc and realloc. They are used for interoperability
with C code, when allocated object has variable size and when it is
necessary to avoid call of constructors. In many calls the result is not
checked for null pointer. To simplify checks, new functions are defined
in the namespace 'llvm': `safe_malloc`, `safe_calloc` and `safe_realloc`.
They behave as corresponding standard functions but produce fatal error if
allocation fails. This change replaces the standard functions like 'malloc'
in the cases when the result of the allocation function is not checked
for null pointer.
Finally, there are plain C code, that uses malloc and similar functions. If
the result is not checked, assert statement is added.
Differential Revision: https://reviews.llvm.org/D43010
llvm-svn: 325551
2018-02-20 13:41:26 +08:00
|
|
|
PhysRegEntries = static_cast<unsigned char*>(
|
|
|
|
safe_calloc(PhysRegEntriesCount, sizeof(unsigned char)));
|
2014-02-06 17:23:24 +08:00
|
|
|
}
|
|
|
|
|
2011-04-02 14:03:35 +08:00
|
|
|
void InterferenceCache::init(MachineFunction *mf,
|
|
|
|
LiveIntervalUnion *liuarray,
|
|
|
|
SlotIndexes *indexes,
|
2012-02-11 02:58:34 +08:00
|
|
|
LiveIntervals *lis,
|
2011-07-23 11:10:17 +08:00
|
|
|
const TargetRegisterInfo *tri) {
|
2011-04-02 14:03:35 +08:00
|
|
|
MF = mf;
|
|
|
|
LIUArray = liuarray;
|
|
|
|
TRI = tri;
|
2014-02-06 17:23:24 +08:00
|
|
|
reinitPhysRegEntries();
|
2011-04-02 14:03:35 +08:00
|
|
|
for (unsigned i = 0; i != CacheEntries; ++i)
|
2012-02-11 02:58:34 +08:00
|
|
|
Entries[i].clear(mf, indexes, lis);
|
2011-04-02 14:03:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
InterferenceCache::Entry *InterferenceCache::get(unsigned PhysReg) {
|
|
|
|
unsigned E = PhysRegEntries[PhysReg];
|
|
|
|
if (E < CacheEntries && Entries[E].getPhysReg() == PhysReg) {
|
|
|
|
if (!Entries[E].valid(LIUArray, TRI))
|
2012-06-21 06:52:26 +08:00
|
|
|
Entries[E].revalidate(LIUArray, TRI);
|
2011-04-02 14:03:35 +08:00
|
|
|
return &Entries[E];
|
|
|
|
}
|
|
|
|
// No valid entry exists, pick the next round-robin entry.
|
|
|
|
E = RoundRobin;
|
|
|
|
if (++RoundRobin == CacheEntries)
|
|
|
|
RoundRobin = 0;
|
2011-07-14 13:35:11 +08:00
|
|
|
for (unsigned i = 0; i != CacheEntries; ++i) {
|
|
|
|
// Skip entries that are in use.
|
|
|
|
if (Entries[E].hasRefs()) {
|
|
|
|
if (++E == CacheEntries)
|
|
|
|
E = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Entries[E].reset(PhysReg, LIUArray, TRI, MF);
|
|
|
|
PhysRegEntries[PhysReg] = E;
|
|
|
|
return &Entries[E];
|
|
|
|
}
|
|
|
|
llvm_unreachable("Ran out of interference cache entries.");
|
2011-04-02 14:03:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// revalidate - LIU contents have changed, update tags.
|
2012-06-21 06:52:26 +08:00
|
|
|
void InterferenceCache::Entry::revalidate(LiveIntervalUnion *LIUArray,
|
|
|
|
const TargetRegisterInfo *TRI) {
|
2011-04-02 14:03:35 +08:00
|
|
|
// Invalidate all block entries.
|
|
|
|
++Tag;
|
|
|
|
// Invalidate all iterators.
|
|
|
|
PrevPos = SlotIndex();
|
2012-06-21 06:52:26 +08:00
|
|
|
unsigned i = 0;
|
|
|
|
for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units, ++i)
|
|
|
|
RegUnits[i].VirtTag = LIUArray[*Units].getTag();
|
2011-04-02 14:03:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void InterferenceCache::Entry::reset(unsigned physReg,
|
|
|
|
LiveIntervalUnion *LIUArray,
|
|
|
|
const TargetRegisterInfo *TRI,
|
|
|
|
const MachineFunction *MF) {
|
2011-07-14 13:35:11 +08:00
|
|
|
assert(!hasRefs() && "Cannot reset cache entry with references");
|
2011-04-02 14:03:35 +08:00
|
|
|
// LIU's changed, invalidate cache.
|
|
|
|
++Tag;
|
|
|
|
PhysReg = physReg;
|
|
|
|
Blocks.resize(MF->getNumBlockIDs());
|
|
|
|
|
|
|
|
// Reset iterators.
|
|
|
|
PrevPos = SlotIndex();
|
2012-06-21 06:52:26 +08:00
|
|
|
RegUnits.clear();
|
|
|
|
for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units) {
|
|
|
|
RegUnits.push_back(LIUArray[*Units]);
|
|
|
|
RegUnits.back().Fixed = &LIS->getRegUnit(*Units);
|
|
|
|
}
|
2011-04-02 14:03:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool InterferenceCache::Entry::valid(LiveIntervalUnion *LIUArray,
|
|
|
|
const TargetRegisterInfo *TRI) {
|
2012-06-21 06:52:26 +08:00
|
|
|
unsigned i = 0, e = RegUnits.size();
|
|
|
|
for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units, ++i) {
|
|
|
|
if (i == e)
|
2011-04-02 14:03:35 +08:00
|
|
|
return false;
|
2012-06-21 06:52:26 +08:00
|
|
|
if (LIUArray[*Units].changedSince(RegUnits[i].VirtTag))
|
2011-04-02 14:03:35 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return i == e;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InterferenceCache::Entry::update(unsigned MBBNum) {
|
|
|
|
SlotIndex Start, Stop;
|
2014-03-02 21:30:33 +08:00
|
|
|
std::tie(Start, Stop) = Indexes->getMBBRange(MBBNum);
|
2011-04-02 14:03:35 +08:00
|
|
|
|
|
|
|
// Use advanceTo only when possible.
|
2011-04-08 01:27:50 +08:00
|
|
|
if (PrevPos != Start) {
|
2012-06-21 06:52:26 +08:00
|
|
|
if (!PrevPos.isValid() || Start < PrevPos) {
|
|
|
|
for (unsigned i = 0, e = RegUnits.size(); i != e; ++i) {
|
|
|
|
RegUnitInfo &RUI = RegUnits[i];
|
|
|
|
RUI.VirtI.find(Start);
|
|
|
|
RUI.FixedI = RUI.Fixed->find(Start);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (unsigned i = 0, e = RegUnits.size(); i != e; ++i) {
|
|
|
|
RegUnitInfo &RUI = RegUnits[i];
|
|
|
|
RUI.VirtI.advanceTo(Start);
|
|
|
|
if (RUI.FixedI != RUI.Fixed->end())
|
|
|
|
RUI.FixedI = RUI.Fixed->advanceTo(RUI.FixedI, Start);
|
|
|
|
}
|
|
|
|
}
|
2011-04-08 01:27:50 +08:00
|
|
|
PrevPos = Start;
|
|
|
|
}
|
2011-04-02 14:03:35 +08:00
|
|
|
|
2015-10-10 03:13:58 +08:00
|
|
|
MachineFunction::const_iterator MFI =
|
|
|
|
MF->getBlockNumbered(MBBNum)->getIterator();
|
2011-04-09 10:59:05 +08:00
|
|
|
BlockInterference *BI = &Blocks[MBBNum];
|
2012-02-11 02:58:34 +08:00
|
|
|
ArrayRef<SlotIndex> RegMaskSlots;
|
|
|
|
ArrayRef<const uint32_t*> RegMaskBits;
|
2017-09-23 07:46:57 +08:00
|
|
|
while (true) {
|
2011-04-09 10:59:05 +08:00
|
|
|
BI->Tag = Tag;
|
|
|
|
BI->First = BI->Last = SlotIndex();
|
|
|
|
|
2012-06-21 06:52:26 +08:00
|
|
|
// Check for first interference from virtregs.
|
|
|
|
for (unsigned i = 0, e = RegUnits.size(); i != e; ++i) {
|
|
|
|
LiveIntervalUnion::SegmentIter &I = RegUnits[i].VirtI;
|
2011-04-09 10:59:05 +08:00
|
|
|
if (!I.valid())
|
|
|
|
continue;
|
|
|
|
SlotIndex StartI = I.start();
|
|
|
|
if (StartI >= Stop)
|
|
|
|
continue;
|
|
|
|
if (!BI->First.isValid() || StartI < BI->First)
|
|
|
|
BI->First = StartI;
|
|
|
|
}
|
2011-04-02 14:03:35 +08:00
|
|
|
|
2012-06-21 06:52:26 +08:00
|
|
|
// Same thing for fixed interference.
|
|
|
|
for (unsigned i = 0, e = RegUnits.size(); i != e; ++i) {
|
|
|
|
LiveInterval::const_iterator I = RegUnits[i].FixedI;
|
|
|
|
LiveInterval::const_iterator E = RegUnits[i].Fixed->end();
|
|
|
|
if (I == E)
|
|
|
|
continue;
|
|
|
|
SlotIndex StartI = I->start;
|
|
|
|
if (StartI >= Stop)
|
|
|
|
continue;
|
|
|
|
if (!BI->First.isValid() || StartI < BI->First)
|
|
|
|
BI->First = StartI;
|
|
|
|
}
|
|
|
|
|
2012-02-11 02:58:34 +08:00
|
|
|
// Also check for register mask interference.
|
|
|
|
RegMaskSlots = LIS->getRegMaskSlotsInBlock(MBBNum);
|
|
|
|
RegMaskBits = LIS->getRegMaskBitsInBlock(MBBNum);
|
|
|
|
SlotIndex Limit = BI->First.isValid() ? BI->First : Stop;
|
|
|
|
for (unsigned i = 0, e = RegMaskSlots.size();
|
|
|
|
i != e && RegMaskSlots[i] < Limit; ++i)
|
2012-02-11 03:23:53 +08:00
|
|
|
if (MachineOperand::clobbersPhysReg(RegMaskBits[i], PhysReg)) {
|
2012-02-11 02:58:34 +08:00
|
|
|
// Register mask i clobbers PhysReg before the LIU interference.
|
|
|
|
BI->First = RegMaskSlots[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-04-09 10:59:05 +08:00
|
|
|
PrevPos = Stop;
|
|
|
|
if (BI->First.isValid())
|
|
|
|
break;
|
|
|
|
|
|
|
|
// No interference in this block? Go ahead and precompute the next block.
|
|
|
|
if (++MFI == MF->end())
|
|
|
|
return;
|
|
|
|
MBBNum = MFI->getNumber();
|
|
|
|
BI = &Blocks[MBBNum];
|
|
|
|
if (BI->Tag == Tag)
|
|
|
|
return;
|
2014-03-02 21:30:33 +08:00
|
|
|
std::tie(Start, Stop) = Indexes->getMBBRange(MBBNum);
|
2011-04-09 10:59:05 +08:00
|
|
|
}
|
2011-04-02 14:03:35 +08:00
|
|
|
|
2011-04-09 10:59:05 +08:00
|
|
|
// Check for last interference in block.
|
2012-06-21 06:52:26 +08:00
|
|
|
for (unsigned i = 0, e = RegUnits.size(); i != e; ++i) {
|
|
|
|
LiveIntervalUnion::SegmentIter &I = RegUnits[i].VirtI;
|
2011-04-02 14:03:35 +08:00
|
|
|
if (!I.valid() || I.start() >= Stop)
|
|
|
|
continue;
|
|
|
|
I.advanceTo(Stop);
|
2011-04-08 01:27:50 +08:00
|
|
|
bool Backup = !I.valid() || I.start() >= Stop;
|
|
|
|
if (Backup)
|
2011-04-02 14:03:35 +08:00
|
|
|
--I;
|
|
|
|
SlotIndex StopI = I.stop();
|
|
|
|
if (!BI->Last.isValid() || StopI > BI->Last)
|
|
|
|
BI->Last = StopI;
|
2011-04-08 01:27:50 +08:00
|
|
|
if (Backup)
|
|
|
|
++I;
|
2011-04-02 14:03:35 +08:00
|
|
|
}
|
2012-02-11 02:58:34 +08:00
|
|
|
|
2012-06-21 06:52:26 +08:00
|
|
|
// Fixed interference.
|
|
|
|
for (unsigned i = 0, e = RegUnits.size(); i != e; ++i) {
|
|
|
|
LiveInterval::iterator &I = RegUnits[i].FixedI;
|
2013-10-11 05:29:02 +08:00
|
|
|
LiveRange *LR = RegUnits[i].Fixed;
|
|
|
|
if (I == LR->end() || I->start >= Stop)
|
2012-06-21 06:52:26 +08:00
|
|
|
continue;
|
2013-10-11 05:29:02 +08:00
|
|
|
I = LR->advanceTo(I, Stop);
|
|
|
|
bool Backup = I == LR->end() || I->start >= Stop;
|
2012-06-21 06:52:26 +08:00
|
|
|
if (Backup)
|
|
|
|
--I;
|
|
|
|
SlotIndex StopI = I->end;
|
|
|
|
if (!BI->Last.isValid() || StopI > BI->Last)
|
|
|
|
BI->Last = StopI;
|
|
|
|
if (Backup)
|
|
|
|
++I;
|
|
|
|
}
|
|
|
|
|
2012-02-11 02:58:34 +08:00
|
|
|
// Also check for register mask interference.
|
|
|
|
SlotIndex Limit = BI->Last.isValid() ? BI->Last : Start;
|
2012-02-15 07:53:23 +08:00
|
|
|
for (unsigned i = RegMaskSlots.size();
|
|
|
|
i && RegMaskSlots[i-1].getDeadSlot() > Limit; --i)
|
2012-02-11 03:23:53 +08:00
|
|
|
if (MachineOperand::clobbersPhysReg(RegMaskBits[i-1], PhysReg)) {
|
2012-02-11 02:58:34 +08:00
|
|
|
// Register mask i-1 clobbers PhysReg after the LIU interference.
|
|
|
|
// Model the regmask clobber as a dead def.
|
|
|
|
BI->Last = RegMaskSlots[i-1].getDeadSlot();
|
|
|
|
break;
|
|
|
|
}
|
2011-04-02 14:03:35 +08:00
|
|
|
}
|