2017-09-23 07:46:57 +08:00
|
|
|
//===- InterferenceCache.h - Caching per-block interference ----*- C++ -*--===//
|
2011-04-02 14:03:35 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2011-04-02 14:03:35 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2012-06-21 06:52:26 +08:00
|
|
|
// InterferenceCache remembers per-block interference from LiveIntervalUnions,
|
|
|
|
// fixed RegUnit interference, and register masks.
|
2011-04-02 14:03:35 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-08-14 00:26:38 +08:00
|
|
|
#ifndef LLVM_LIB_CODEGEN_INTERFERENCECACHE_H
|
|
|
|
#define LLVM_LIB_CODEGEN_INTERFERENCECACHE_H
|
2011-04-02 14:03:35 +08:00
|
|
|
|
2017-09-23 07:46:57 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/CodeGen/LiveInterval.h"
|
2012-11-29 03:13:06 +08:00
|
|
|
#include "llvm/CodeGen/LiveIntervalUnion.h"
|
2017-09-23 07:46:57 +08:00
|
|
|
#include "llvm/CodeGen/SlotIndexes.h"
|
|
|
|
#include "llvm/Support/Compiler.h"
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdlib>
|
2011-04-02 14:03:35 +08:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
2012-02-11 02:58:34 +08:00
|
|
|
class LiveIntervals;
|
2017-09-23 07:46:57 +08:00
|
|
|
class MachineFunction;
|
|
|
|
class TargetRegisterInfo;
|
2012-02-11 02:58:34 +08:00
|
|
|
|
2015-07-01 22:47:39 +08:00
|
|
|
class LLVM_LIBRARY_VISIBILITY InterferenceCache {
|
2011-04-02 14:03:35 +08:00
|
|
|
/// BlockInterference - information about the interference in a single basic
|
|
|
|
/// block.
|
|
|
|
struct BlockInterference {
|
2017-09-23 07:46:57 +08:00
|
|
|
unsigned Tag = 0;
|
2011-04-02 14:03:35 +08:00
|
|
|
SlotIndex First;
|
|
|
|
SlotIndex Last;
|
2017-09-23 07:46:57 +08:00
|
|
|
|
2017-09-23 07:55:32 +08:00
|
|
|
BlockInterference() {}
|
2011-04-02 14:03:35 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Entry - A cache entry containing interference information for all aliases
|
|
|
|
/// of PhysReg in all basic blocks.
|
|
|
|
class Entry {
|
|
|
|
/// PhysReg - The register currently represented.
|
2017-09-23 07:46:57 +08:00
|
|
|
unsigned PhysReg = 0;
|
2011-04-02 14:03:35 +08:00
|
|
|
|
|
|
|
/// Tag - Cache tag is changed when any of the underlying LiveIntervalUnions
|
|
|
|
/// change.
|
2017-09-23 07:46:57 +08:00
|
|
|
unsigned Tag = 0;
|
2011-04-02 14:03:35 +08:00
|
|
|
|
2011-07-14 13:35:11 +08:00
|
|
|
/// RefCount - The total number of Cursor instances referring to this Entry.
|
2017-09-23 07:46:57 +08:00
|
|
|
unsigned RefCount = 0;
|
2011-07-14 13:35:11 +08:00
|
|
|
|
2011-04-09 10:59:05 +08:00
|
|
|
/// MF - The current function.
|
|
|
|
MachineFunction *MF;
|
|
|
|
|
2011-04-02 14:03:35 +08:00
|
|
|
/// Indexes - Mapping block numbers to SlotIndex ranges.
|
2017-09-23 07:46:57 +08:00
|
|
|
SlotIndexes *Indexes = nullptr;
|
2011-04-02 14:03:35 +08:00
|
|
|
|
2012-02-11 02:58:34 +08:00
|
|
|
/// LIS - Used for accessing register mask interference maps.
|
2017-09-23 07:46:57 +08:00
|
|
|
LiveIntervals *LIS = nullptr;
|
2012-02-11 02:58:34 +08:00
|
|
|
|
2011-04-02 14:03:35 +08:00
|
|
|
/// PrevPos - The previous position the iterators were moved to.
|
|
|
|
SlotIndex PrevPos;
|
|
|
|
|
2012-06-21 06:52:26 +08:00
|
|
|
/// RegUnitInfo - Information tracked about each RegUnit in PhysReg.
|
|
|
|
/// When PrevPos is set, the iterators are valid as if advanceTo(PrevPos)
|
|
|
|
/// had just been called.
|
|
|
|
struct RegUnitInfo {
|
|
|
|
/// Iterator pointing into the LiveIntervalUnion containing virtual
|
|
|
|
/// register interference.
|
|
|
|
LiveIntervalUnion::SegmentIter VirtI;
|
2011-04-02 14:03:35 +08:00
|
|
|
|
2012-06-21 06:52:26 +08:00
|
|
|
/// Tag of the LIU last time we looked.
|
|
|
|
unsigned VirtTag;
|
2011-04-02 14:03:35 +08:00
|
|
|
|
2012-06-21 06:52:26 +08:00
|
|
|
/// Fixed interference in RegUnit.
|
2017-09-23 07:46:57 +08:00
|
|
|
LiveRange *Fixed = nullptr;
|
2012-06-21 06:52:26 +08:00
|
|
|
|
|
|
|
/// Iterator pointing into the fixed RegUnit interference.
|
|
|
|
LiveInterval::iterator FixedI;
|
|
|
|
|
2017-09-23 07:46:57 +08:00
|
|
|
RegUnitInfo(LiveIntervalUnion &LIU) : VirtTag(LIU.getTag()) {
|
2012-06-21 06:52:26 +08:00
|
|
|
VirtI.setMap(LIU.getMap());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Info for each RegUnit in PhysReg. It is very rare ofr a PHysReg to have
|
|
|
|
/// more than 4 RegUnits.
|
|
|
|
SmallVector<RegUnitInfo, 4> RegUnits;
|
2011-04-02 14:03:35 +08:00
|
|
|
|
|
|
|
/// Blocks - Interference for each block in the function.
|
|
|
|
SmallVector<BlockInterference, 8> Blocks;
|
|
|
|
|
|
|
|
/// update - Recompute Blocks[MBBNum]
|
|
|
|
void update(unsigned MBBNum);
|
|
|
|
|
|
|
|
public:
|
2017-09-23 07:46:57 +08:00
|
|
|
Entry() = default;
|
2011-04-02 14:03:35 +08:00
|
|
|
|
2012-02-11 02:58:34 +08:00
|
|
|
void clear(MachineFunction *mf, SlotIndexes *indexes, LiveIntervals *lis) {
|
2011-07-14 13:35:11 +08:00
|
|
|
assert(!hasRefs() && "Cannot clear cache entry with references");
|
2011-04-02 14:03:35 +08:00
|
|
|
PhysReg = 0;
|
2011-04-09 10:59:05 +08:00
|
|
|
MF = mf;
|
2011-04-02 14:03:35 +08:00
|
|
|
Indexes = indexes;
|
2012-02-11 02:58:34 +08:00
|
|
|
LIS = lis;
|
2011-04-02 14:03:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned getPhysReg() const { return PhysReg; }
|
|
|
|
|
2011-07-14 13:35:11 +08:00
|
|
|
void addRef(int Delta) { RefCount += Delta; }
|
|
|
|
|
|
|
|
bool hasRefs() const { return RefCount > 0; }
|
|
|
|
|
2012-06-21 06:52:26 +08:00
|
|
|
void revalidate(LiveIntervalUnion *LIUArray, const TargetRegisterInfo *TRI);
|
2011-04-02 14:03:35 +08:00
|
|
|
|
|
|
|
/// valid - Return true if this is a valid entry for physReg.
|
|
|
|
bool valid(LiveIntervalUnion *LIUArray, const TargetRegisterInfo *TRI);
|
|
|
|
|
|
|
|
/// reset - Initialize entry to represent physReg's aliases.
|
|
|
|
void reset(unsigned physReg,
|
|
|
|
LiveIntervalUnion *LIUArray,
|
|
|
|
const TargetRegisterInfo *TRI,
|
|
|
|
const MachineFunction *MF);
|
|
|
|
|
|
|
|
/// get - Return an up to date BlockInterference.
|
|
|
|
BlockInterference *get(unsigned MBBNum) {
|
|
|
|
if (Blocks[MBBNum].Tag != Tag)
|
|
|
|
update(MBBNum);
|
|
|
|
return &Blocks[MBBNum];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// We don't keep a cache entry for every physical register, that would use too
|
|
|
|
// much memory. Instead, a fixed number of cache entries are used in a round-
|
|
|
|
// robin manner.
|
|
|
|
enum { CacheEntries = 32 };
|
|
|
|
|
2017-09-23 07:46:57 +08:00
|
|
|
const TargetRegisterInfo *TRI = nullptr;
|
|
|
|
LiveIntervalUnion *LIUArray = nullptr;
|
|
|
|
MachineFunction *MF = nullptr;
|
|
|
|
|
2011-04-02 14:03:35 +08:00
|
|
|
// Point to an entry for each physreg. The entry pointed to may not be up to
|
|
|
|
// date, and it may have been reused for a different physreg.
|
2017-09-23 07:46:57 +08:00
|
|
|
unsigned char* PhysRegEntries = nullptr;
|
|
|
|
size_t PhysRegEntriesCount = 0;
|
2011-04-02 14:03:35 +08:00
|
|
|
|
|
|
|
// Next round-robin entry to be picked.
|
2017-09-23 07:46:57 +08:00
|
|
|
unsigned RoundRobin = 0;
|
2011-04-02 14:03:35 +08:00
|
|
|
|
|
|
|
// The actual cache entries.
|
|
|
|
Entry Entries[CacheEntries];
|
|
|
|
|
|
|
|
// get - Get a valid entry for PhysReg.
|
|
|
|
Entry *get(unsigned PhysReg);
|
|
|
|
|
|
|
|
public:
|
2017-09-23 07:46:57 +08:00
|
|
|
InterferenceCache() = default;
|
2014-02-06 17:23:24 +08:00
|
|
|
|
|
|
|
~InterferenceCache() {
|
|
|
|
free(PhysRegEntries);
|
|
|
|
}
|
|
|
|
|
|
|
|
void reinitPhysRegEntries();
|
2011-04-02 14:03:35 +08:00
|
|
|
|
|
|
|
/// init - Prepare cache for a new function.
|
2017-09-23 07:46:57 +08:00
|
|
|
void init(MachineFunction *mf, LiveIntervalUnion *liuarray,
|
|
|
|
SlotIndexes *indexes, LiveIntervals *lis,
|
|
|
|
const TargetRegisterInfo *tri);
|
2011-04-02 14:03:35 +08:00
|
|
|
|
2011-07-14 13:35:11 +08:00
|
|
|
/// getMaxCursors - Return the maximum number of concurrent cursors that can
|
|
|
|
/// be supported.
|
|
|
|
unsigned getMaxCursors() const { return CacheEntries; }
|
|
|
|
|
2011-04-02 14:03:35 +08:00
|
|
|
/// Cursor - The primary query interface for the block interference cache.
|
|
|
|
class Cursor {
|
2017-09-23 07:46:57 +08:00
|
|
|
Entry *CacheEntry = nullptr;
|
|
|
|
const BlockInterference *Current = nullptr;
|
2015-03-09 00:07:39 +08:00
|
|
|
static const BlockInterference NoInterference;
|
2011-07-14 13:35:11 +08:00
|
|
|
|
|
|
|
void setEntry(Entry *E) {
|
2014-04-16 12:21:27 +08:00
|
|
|
Current = nullptr;
|
2011-07-14 13:35:11 +08:00
|
|
|
// Update reference counts. Nothing happens when RefCount reaches 0, so
|
|
|
|
// we don't have to check for E == CacheEntry etc.
|
|
|
|
if (CacheEntry)
|
|
|
|
CacheEntry->addRef(-1);
|
|
|
|
CacheEntry = E;
|
|
|
|
if (CacheEntry)
|
|
|
|
CacheEntry->addRef(+1);
|
|
|
|
}
|
|
|
|
|
2011-04-02 14:03:35 +08:00
|
|
|
public:
|
2011-07-14 08:17:10 +08:00
|
|
|
/// Cursor - Create a dangling cursor.
|
2017-09-23 07:46:57 +08:00
|
|
|
Cursor() = default;
|
2011-07-14 13:35:11 +08:00
|
|
|
|
2017-09-23 07:46:57 +08:00
|
|
|
Cursor(const Cursor &O) {
|
2011-07-14 13:35:11 +08:00
|
|
|
setEntry(O.CacheEntry);
|
|
|
|
}
|
|
|
|
|
|
|
|
Cursor &operator=(const Cursor &O) {
|
|
|
|
setEntry(O.CacheEntry);
|
|
|
|
return *this;
|
|
|
|
}
|
2011-07-14 08:17:10 +08:00
|
|
|
|
2017-09-23 07:46:57 +08:00
|
|
|
~Cursor() { setEntry(nullptr); }
|
|
|
|
|
2011-07-14 08:17:10 +08:00
|
|
|
/// setPhysReg - Point this cursor to PhysReg's interference.
|
|
|
|
void setPhysReg(InterferenceCache &Cache, unsigned PhysReg) {
|
2011-07-14 13:35:11 +08:00
|
|
|
// Release reference before getting a new one. That guarantees we can
|
|
|
|
// actually have CacheEntries live cursors.
|
2014-04-16 12:21:27 +08:00
|
|
|
setEntry(nullptr);
|
2011-07-14 13:35:11 +08:00
|
|
|
if (PhysReg)
|
|
|
|
setEntry(Cache.get(PhysReg));
|
2011-07-14 08:17:10 +08:00
|
|
|
}
|
2011-04-02 14:03:35 +08:00
|
|
|
|
|
|
|
/// moveTo - Move cursor to basic block MBBNum.
|
|
|
|
void moveToBlock(unsigned MBBNum) {
|
2011-07-23 11:10:17 +08:00
|
|
|
Current = CacheEntry ? CacheEntry->get(MBBNum) : &NoInterference;
|
2011-04-02 14:03:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// hasInterference - Return true if the current block has any interference.
|
|
|
|
bool hasInterference() {
|
|
|
|
return Current->First.isValid();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// first - Return the starting index of the first interfering range in the
|
|
|
|
/// current block.
|
|
|
|
SlotIndex first() {
|
|
|
|
return Current->First;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// last - Return the ending index of the last interfering range in the
|
|
|
|
/// current block.
|
|
|
|
SlotIndex last() {
|
|
|
|
return Current->Last;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2017-09-23 07:46:57 +08:00
|
|
|
} // end namespace llvm
|
2011-04-02 14:03:35 +08:00
|
|
|
|
2017-09-23 07:46:57 +08:00
|
|
|
#endif // LLVM_LIB_CODEGEN_INTERFERENCECACHE_H
|