2009-11-03 23:52:08 +00:00
|
|
|
//===- llvm/CodeGen/SlotIndexes.h - Slot indexes representation -*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2011-09-29 21:07:46 +00:00
|
|
|
// This file implements SlotIndex and related classes. The purpose of SlotIndex
|
2009-11-03 23:52:08 +00:00
|
|
|
// is to describe a position at which a register can become live, or cease to
|
|
|
|
// be live.
|
|
|
|
//
|
|
|
|
// SlotIndex is mostly a proxy for entries of the SlotIndexList, a class which
|
|
|
|
// is held is LiveIntervals and provides the real numbering. This allows
|
2010-09-25 00:45:18 +00:00
|
|
|
// LiveIntervals to perform largely transparent renumbering.
|
2009-11-03 23:52:08 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_CODEGEN_SLOTINDEXES_H
|
|
|
|
#define LLVM_CODEGEN_SLOTINDEXES_H
|
|
|
|
|
2012-12-03 17:02:12 +00:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2012-12-27 11:29:17 +00:00
|
|
|
#include "llvm/ADT/IntervalMap.h"
|
2010-04-02 20:20:04 +00:00
|
|
|
#include "llvm/ADT/PointerIntPair.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2012-12-03 17:02:12 +00:00
|
|
|
#include "llvm/ADT/ilist.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstrBundle.h"
|
2009-11-03 23:52:08 +00:00
|
|
|
#include "llvm/Support/Allocator.h"
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
/// This class represents an entry in the slot index list held in the
|
|
|
|
/// SlotIndexes pass. It should not be used directly. See the
|
|
|
|
/// SlotIndex & SlotIndexes classes for the public interface to this
|
|
|
|
/// information.
|
2012-04-17 04:15:51 +00:00
|
|
|
class IndexListEntry : public ilist_node<IndexListEntry> {
|
2009-11-03 23:52:08 +00:00
|
|
|
MachineInstr *mi;
|
|
|
|
unsigned index;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
2011-03-03 05:40:04 +00:00
|
|
|
IndexListEntry(MachineInstr *mi, unsigned index) : mi(mi), index(index) {}
|
2010-02-09 00:41:23 +00:00
|
|
|
|
2009-11-03 23:52:08 +00:00
|
|
|
MachineInstr* getInstr() const { return mi; }
|
2009-11-07 05:50:28 +00:00
|
|
|
void setInstr(MachineInstr *mi) {
|
|
|
|
this->mi = mi;
|
|
|
|
}
|
2009-11-03 23:52:08 +00:00
|
|
|
|
|
|
|
unsigned getIndex() const { return index; }
|
2009-11-07 05:50:28 +00:00
|
|
|
void setIndex(unsigned index) {
|
|
|
|
this->index = index;
|
|
|
|
}
|
2009-11-03 23:52:08 +00:00
|
|
|
|
2013-04-19 04:31:49 +00:00
|
|
|
#ifdef EXPENSIVE_CHECKS
|
|
|
|
// When EXPENSIVE_CHECKS is defined, "erased" index list entries will
|
|
|
|
// actually be moved to a "graveyard" list, and have their pointers
|
|
|
|
// poisoned, so that dangling SlotIndex access can be reliably detected.
|
|
|
|
void setPoison() {
|
|
|
|
intptr_t tmp = reinterpret_cast<intptr_t>(mi);
|
2013-07-30 19:59:15 +00:00
|
|
|
assert(((tmp & 0x1) == 0x0) && "Pointer already poisoned?");
|
2013-04-19 04:31:49 +00:00
|
|
|
tmp |= 0x1;
|
|
|
|
mi = reinterpret_cast<MachineInstr*>(tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isPoisoned() const { return (reinterpret_cast<intptr_t>(mi) & 0x1) == 0x1; }
|
|
|
|
#endif // EXPENSIVE_CHECKS
|
|
|
|
|
2012-04-17 04:15:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct ilist_traits<IndexListEntry> : public ilist_default_traits<IndexListEntry> {
|
|
|
|
private:
|
|
|
|
mutable ilist_half_node<IndexListEntry> Sentinel;
|
|
|
|
public:
|
|
|
|
IndexListEntry *createSentinel() const {
|
|
|
|
return static_cast<IndexListEntry*>(&Sentinel);
|
2009-11-07 05:50:28 +00:00
|
|
|
}
|
2012-04-17 04:15:51 +00:00
|
|
|
void destroySentinel(IndexListEntry *) const {}
|
|
|
|
|
|
|
|
IndexListEntry *provideInitialHead() const { return createSentinel(); }
|
|
|
|
IndexListEntry *ensureHead(IndexListEntry*) const { return createSentinel(); }
|
|
|
|
static void noteHead(IndexListEntry*, IndexListEntry*) {}
|
|
|
|
void deleteNode(IndexListEntry *N) {}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void createNode(const IndexListEntry &);
|
2009-11-03 23:52:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/// SlotIndex - An opaque wrapper around machine indexes.
|
|
|
|
class SlotIndex {
|
|
|
|
friend class SlotIndexes;
|
|
|
|
|
2011-11-13 20:45:27 +00:00
|
|
|
enum Slot {
|
|
|
|
/// Basic block boundary. Used for live ranges entering and leaving a
|
|
|
|
/// block without being live in the layout neighbor. Also used as the
|
|
|
|
/// def slot of PHI-defs.
|
|
|
|
Slot_Block,
|
|
|
|
|
|
|
|
/// Early-clobber register use/def slot. A live range defined at
|
|
|
|
/// Slot_EarlyCLobber interferes with normal live ranges killed at
|
|
|
|
/// Slot_Register. Also used as the kill slot for live ranges tied to an
|
|
|
|
/// early-clobber def.
|
|
|
|
Slot_EarlyClobber,
|
|
|
|
|
|
|
|
/// Normal register use/def slot. Normal instructions kill and define
|
|
|
|
/// register live ranges at this slot.
|
|
|
|
Slot_Register,
|
|
|
|
|
|
|
|
/// Dead def kill point. Kill slot for a live range that is defined by
|
|
|
|
/// the same instruction (Slot_Register or Slot_EarlyClobber), but isn't
|
|
|
|
/// used anywhere.
|
|
|
|
Slot_Dead,
|
|
|
|
|
|
|
|
Slot_Count
|
|
|
|
};
|
2010-08-11 16:50:17 +00:00
|
|
|
|
2010-09-25 00:45:18 +00:00
|
|
|
PointerIntPair<IndexListEntry*, 2, unsigned> lie;
|
2009-11-03 23:52:08 +00:00
|
|
|
|
2010-09-25 00:45:18 +00:00
|
|
|
SlotIndex(IndexListEntry *entry, unsigned slot)
|
2011-03-03 05:40:04 +00:00
|
|
|
: lie(entry, slot) {}
|
2009-11-03 23:52:08 +00:00
|
|
|
|
2012-04-17 04:15:51 +00:00
|
|
|
IndexListEntry* listEntry() const {
|
2011-03-03 05:18:19 +00:00
|
|
|
assert(isValid() && "Attempt to compare reserved index.");
|
2013-04-19 04:31:49 +00:00
|
|
|
#ifdef EXPENSIVE_CHECKS
|
|
|
|
assert(!lie.getPointer()->isPoisoned() &&
|
|
|
|
"Attempt to access deleted list-entry.");
|
|
|
|
#endif // EXPENSIVE_CHECKS
|
2012-04-17 04:15:51 +00:00
|
|
|
return lie.getPointer();
|
2009-11-03 23:52:08 +00:00
|
|
|
}
|
|
|
|
|
2013-02-20 06:46:39 +00:00
|
|
|
unsigned getIndex() const {
|
2012-04-17 04:15:51 +00:00
|
|
|
return listEntry()->getIndex() | getSlot();
|
2009-11-03 23:52:08 +00:00
|
|
|
}
|
|
|
|
|
2010-08-11 16:50:17 +00:00
|
|
|
/// Returns the slot for this SlotIndex.
|
|
|
|
Slot getSlot() const {
|
2010-09-25 00:45:18 +00:00
|
|
|
return static_cast<Slot>(lie.getInt());
|
2010-08-11 16:50:17 +00:00
|
|
|
}
|
|
|
|
|
2009-11-03 23:52:08 +00:00
|
|
|
public:
|
2011-03-04 18:36:51 +00:00
|
|
|
enum {
|
|
|
|
/// The default distance between instructions as returned by distance().
|
|
|
|
/// This may vary as instructions are inserted and removed.
|
2011-11-13 20:45:27 +00:00
|
|
|
InstrDist = 4 * Slot_Count
|
2011-03-04 18:36:51 +00:00
|
|
|
};
|
|
|
|
|
2009-11-03 23:52:08 +00:00
|
|
|
/// Construct an invalid index.
|
2011-03-03 05:40:04 +00:00
|
|
|
SlotIndex() : lie(0, 0) {}
|
2009-11-03 23:52:08 +00:00
|
|
|
|
2010-09-25 00:45:18 +00:00
|
|
|
// Construct a new slot index from the given one, and set the slot.
|
2012-04-17 04:15:51 +00:00
|
|
|
SlotIndex(const SlotIndex &li, Slot s) : lie(li.listEntry(), unsigned(s)) {
|
2009-11-03 23:52:08 +00:00
|
|
|
assert(lie.getPointer() != 0 &&
|
|
|
|
"Attempt to construct index with 0 pointer.");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns true if this is a valid index. Invalid indicies do
|
|
|
|
/// not point into an index table, and cannot be compared.
|
|
|
|
bool isValid() const {
|
2011-03-03 05:40:04 +00:00
|
|
|
return lie.getPointer();
|
2009-11-03 23:52:08 +00:00
|
|
|
}
|
|
|
|
|
2011-07-15 21:47:57 +00:00
|
|
|
/// Return true for a valid index.
|
2013-05-15 07:36:59 +00:00
|
|
|
LLVM_EXPLICIT operator bool() const { return isValid(); }
|
2011-07-15 21:47:57 +00:00
|
|
|
|
2009-11-03 23:52:08 +00:00
|
|
|
/// Print this index to the given raw_ostream.
|
|
|
|
void print(raw_ostream &os) const;
|
|
|
|
|
|
|
|
/// Dump this index to stderr.
|
|
|
|
void dump() const;
|
|
|
|
|
|
|
|
/// Compare two SlotIndex objects for equality.
|
|
|
|
bool operator==(SlotIndex other) const {
|
2011-03-03 05:18:15 +00:00
|
|
|
return lie == other.lie;
|
2009-11-03 23:52:08 +00:00
|
|
|
}
|
|
|
|
/// Compare two SlotIndex objects for inequality.
|
|
|
|
bool operator!=(SlotIndex other) const {
|
2011-03-03 05:18:15 +00:00
|
|
|
return lie != other.lie;
|
2009-11-03 23:52:08 +00:00
|
|
|
}
|
2012-04-17 04:15:51 +00:00
|
|
|
|
2009-11-03 23:52:08 +00:00
|
|
|
/// Compare two SlotIndex objects. Return true if the first index
|
|
|
|
/// is strictly lower than the second.
|
|
|
|
bool operator<(SlotIndex other) const {
|
|
|
|
return getIndex() < other.getIndex();
|
|
|
|
}
|
|
|
|
/// Compare two SlotIndex objects. Return true if the first index
|
|
|
|
/// is lower than, or equal to, the second.
|
|
|
|
bool operator<=(SlotIndex other) const {
|
|
|
|
return getIndex() <= other.getIndex();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Compare two SlotIndex objects. Return true if the first index
|
|
|
|
/// is greater than the second.
|
|
|
|
bool operator>(SlotIndex other) const {
|
|
|
|
return getIndex() > other.getIndex();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Compare two SlotIndex objects. Return true if the first index
|
|
|
|
/// is greater than, or equal to, the second.
|
|
|
|
bool operator>=(SlotIndex other) const {
|
|
|
|
return getIndex() >= other.getIndex();
|
|
|
|
}
|
|
|
|
|
2011-04-05 15:18:18 +00:00
|
|
|
/// isSameInstr - Return true if A and B refer to the same instruction.
|
|
|
|
static bool isSameInstr(SlotIndex A, SlotIndex B) {
|
|
|
|
return A.lie.getPointer() == B.lie.getPointer();
|
|
|
|
}
|
|
|
|
|
2012-01-11 02:07:05 +00:00
|
|
|
/// isEarlierInstr - Return true if A refers to an instruction earlier than
|
|
|
|
/// B. This is equivalent to A < B && !isSameInstr(A, B).
|
|
|
|
static bool isEarlierInstr(SlotIndex A, SlotIndex B) {
|
2012-04-17 04:15:51 +00:00
|
|
|
return A.listEntry()->getIndex() < B.listEntry()->getIndex();
|
2012-01-11 02:07:05 +00:00
|
|
|
}
|
|
|
|
|
2009-11-03 23:52:08 +00:00
|
|
|
/// Return the distance from this index to the given one.
|
|
|
|
int distance(SlotIndex other) const {
|
|
|
|
return other.getIndex() - getIndex();
|
|
|
|
}
|
|
|
|
|
2013-07-30 19:59:19 +00:00
|
|
|
/// Return the scaled distance from this index to the given one, where all
|
|
|
|
/// slots on the same instruction have zero distance.
|
|
|
|
int getInstrDistance(SlotIndex other) const {
|
|
|
|
return (other.listEntry()->getIndex() - listEntry()->getIndex())
|
|
|
|
/ Slot_Count;
|
|
|
|
}
|
|
|
|
|
2011-11-13 20:45:27 +00:00
|
|
|
/// isBlock - Returns true if this is a block boundary slot.
|
|
|
|
bool isBlock() const { return getSlot() == Slot_Block; }
|
2010-08-11 16:50:17 +00:00
|
|
|
|
2011-11-13 20:45:27 +00:00
|
|
|
/// isEarlyClobber - Returns true if this is an early-clobber slot.
|
|
|
|
bool isEarlyClobber() const { return getSlot() == Slot_EarlyClobber; }
|
2010-08-11 16:50:17 +00:00
|
|
|
|
2011-11-13 20:45:27 +00:00
|
|
|
/// isRegister - Returns true if this is a normal register use/def slot.
|
|
|
|
/// Note that early-clobber slots may also be used for uses and defs.
|
|
|
|
bool isRegister() const { return getSlot() == Slot_Register; }
|
2010-08-11 16:50:17 +00:00
|
|
|
|
2011-11-13 20:45:27 +00:00
|
|
|
/// isDead - Returns true if this is a dead def kill slot.
|
|
|
|
bool isDead() const { return getSlot() == Slot_Dead; }
|
2010-08-11 16:50:17 +00:00
|
|
|
|
2009-11-03 23:52:08 +00:00
|
|
|
/// Returns the base index for associated with this index. The base index
|
2011-11-13 20:45:27 +00:00
|
|
|
/// is the one associated with the Slot_Block slot for the instruction
|
|
|
|
/// pointed to by this index.
|
2009-11-03 23:52:08 +00:00
|
|
|
SlotIndex getBaseIndex() const {
|
2012-04-17 04:15:51 +00:00
|
|
|
return SlotIndex(listEntry(), Slot_Block);
|
2009-11-03 23:52:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the boundary index for associated with this index. The boundary
|
2011-11-13 20:45:27 +00:00
|
|
|
/// index is the one associated with the Slot_Block slot for the instruction
|
2009-11-03 23:52:08 +00:00
|
|
|
/// pointed to by this index.
|
|
|
|
SlotIndex getBoundaryIndex() const {
|
2012-04-17 04:15:51 +00:00
|
|
|
return SlotIndex(listEntry(), Slot_Dead);
|
2009-11-03 23:52:08 +00:00
|
|
|
}
|
|
|
|
|
2011-11-13 20:45:27 +00:00
|
|
|
/// Returns the register use/def slot in the current instruction for a
|
|
|
|
/// normal or early-clobber def.
|
|
|
|
SlotIndex getRegSlot(bool EC = false) const {
|
2012-04-17 04:15:51 +00:00
|
|
|
return SlotIndex(listEntry(), EC ? Slot_EarlyClobber : Slot_Register);
|
2009-11-03 23:52:08 +00:00
|
|
|
}
|
|
|
|
|
2011-11-13 20:45:27 +00:00
|
|
|
/// Returns the dead def kill slot for the current instruction.
|
|
|
|
SlotIndex getDeadSlot() const {
|
2012-04-17 04:15:51 +00:00
|
|
|
return SlotIndex(listEntry(), Slot_Dead);
|
2009-11-03 23:52:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the next slot in the index list. This could be either the
|
|
|
|
/// next slot for the instruction pointed to by this index or, if this
|
|
|
|
/// index is a STORE, the first slot for the next instruction.
|
|
|
|
/// WARNING: This method is considerably more expensive than the methods
|
|
|
|
/// that return specific slots (getUseIndex(), etc). If you can - please
|
|
|
|
/// use one of those methods.
|
|
|
|
SlotIndex getNextSlot() const {
|
|
|
|
Slot s = getSlot();
|
2011-11-13 20:45:27 +00:00
|
|
|
if (s == Slot_Dead) {
|
2012-04-17 04:15:51 +00:00
|
|
|
return SlotIndex(listEntry()->getNextNode(), Slot_Block);
|
2009-11-03 23:52:08 +00:00
|
|
|
}
|
2012-04-17 04:15:51 +00:00
|
|
|
return SlotIndex(listEntry(), s + 1);
|
2009-11-03 23:52:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the next index. This is the index corresponding to the this
|
|
|
|
/// index's slot, but for the next instruction.
|
|
|
|
SlotIndex getNextIndex() const {
|
2012-04-17 04:15:51 +00:00
|
|
|
return SlotIndex(listEntry()->getNextNode(), getSlot());
|
2009-11-03 23:52:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the previous slot in the index list. This could be either the
|
|
|
|
/// previous slot for the instruction pointed to by this index or, if this
|
2011-11-13 20:45:27 +00:00
|
|
|
/// index is a Slot_Block, the last slot for the previous instruction.
|
2009-11-03 23:52:08 +00:00
|
|
|
/// WARNING: This method is considerably more expensive than the methods
|
|
|
|
/// that return specific slots (getUseIndex(), etc). If you can - please
|
|
|
|
/// use one of those methods.
|
|
|
|
SlotIndex getPrevSlot() const {
|
|
|
|
Slot s = getSlot();
|
2011-11-13 20:45:27 +00:00
|
|
|
if (s == Slot_Block) {
|
2012-04-17 04:15:51 +00:00
|
|
|
return SlotIndex(listEntry()->getPrevNode(), Slot_Dead);
|
2009-11-03 23:52:08 +00:00
|
|
|
}
|
2012-04-17 04:15:51 +00:00
|
|
|
return SlotIndex(listEntry(), s - 1);
|
2009-11-03 23:52:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the previous index. This is the index corresponding to this
|
|
|
|
/// index's slot, but for the previous instruction.
|
|
|
|
SlotIndex getPrevIndex() const {
|
2012-04-17 04:15:51 +00:00
|
|
|
return SlotIndex(listEntry()->getPrevNode(), getSlot());
|
2009-11-03 23:52:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2009-12-15 07:27:58 +00:00
|
|
|
template <> struct isPodLike<SlotIndex> { static const bool value = true; };
|
|
|
|
|
2009-11-03 23:52:08 +00:00
|
|
|
inline raw_ostream& operator<<(raw_ostream &os, SlotIndex li) {
|
|
|
|
li.print(os);
|
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef std::pair<SlotIndex, MachineBasicBlock*> IdxMBBPair;
|
|
|
|
|
|
|
|
inline bool operator<(SlotIndex V, const IdxMBBPair &IM) {
|
|
|
|
return V < IM.first;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool operator<(const IdxMBBPair &IM, SlotIndex V) {
|
|
|
|
return IM.first < V;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Idx2MBBCompare {
|
|
|
|
bool operator()(const IdxMBBPair &LHS, const IdxMBBPair &RHS) const {
|
|
|
|
return LHS.first < RHS.first;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// SlotIndexes pass.
|
|
|
|
///
|
|
|
|
/// This pass assigns indexes to each instruction.
|
|
|
|
class SlotIndexes : public MachineFunctionPass {
|
|
|
|
private:
|
|
|
|
|
2012-04-17 04:15:51 +00:00
|
|
|
typedef ilist<IndexListEntry> IndexList;
|
|
|
|
IndexList indexList;
|
|
|
|
|
2013-04-19 04:31:49 +00:00
|
|
|
#ifdef EXPENSIVE_CHECKS
|
|
|
|
IndexList graveyardList;
|
|
|
|
#endif // EXPENSIVE_CHECKS
|
|
|
|
|
2009-11-03 23:52:08 +00:00
|
|
|
MachineFunction *mf;
|
|
|
|
|
|
|
|
typedef DenseMap<const MachineInstr*, SlotIndex> Mi2IndexMap;
|
|
|
|
Mi2IndexMap mi2iMap;
|
|
|
|
|
2011-04-02 06:03:31 +00:00
|
|
|
/// MBBRanges - Map MBB number to (start, stop) indexes.
|
|
|
|
SmallVector<std::pair<SlotIndex, SlotIndex>, 8> MBBRanges;
|
2009-11-03 23:52:08 +00:00
|
|
|
|
|
|
|
/// Idx2MBBMap - Sorted list of pairs of index of first instruction
|
|
|
|
/// and MBB id.
|
2011-04-02 06:03:31 +00:00
|
|
|
SmallVector<IdxMBBPair, 8> idx2MBBMap;
|
2009-11-03 23:52:08 +00:00
|
|
|
|
|
|
|
// IndexListEntry allocator.
|
|
|
|
BumpPtrAllocator ileAllocator;
|
|
|
|
|
|
|
|
IndexListEntry* createEntry(MachineInstr *mi, unsigned index) {
|
|
|
|
IndexListEntry *entry =
|
|
|
|
static_cast<IndexListEntry*>(
|
|
|
|
ileAllocator.Allocate(sizeof(IndexListEntry),
|
2010-10-30 05:14:01 +00:00
|
|
|
alignOf<IndexListEntry>()));
|
2009-11-03 23:52:08 +00:00
|
|
|
|
|
|
|
new (entry) IndexListEntry(mi, index);
|
|
|
|
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
2012-04-17 04:15:51 +00:00
|
|
|
/// Renumber locally after inserting curItr.
|
|
|
|
void renumberIndexes(IndexList::iterator curItr);
|
2011-03-04 19:43:38 +00:00
|
|
|
|
2009-11-03 23:52:08 +00:00
|
|
|
public:
|
|
|
|
static char ID;
|
|
|
|
|
2012-04-17 04:15:51 +00:00
|
|
|
SlotIndexes() : MachineFunctionPass(ID) {
|
2010-10-19 17:21:58 +00:00
|
|
|
initializeSlotIndexesPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2009-11-03 23:52:08 +00:00
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &au) const;
|
2012-04-17 04:15:51 +00:00
|
|
|
virtual void releaseMemory();
|
2009-11-03 23:52:08 +00:00
|
|
|
|
|
|
|
virtual bool runOnMachineFunction(MachineFunction &fn);
|
|
|
|
|
|
|
|
/// Dump the indexes.
|
|
|
|
void dump() const;
|
|
|
|
|
|
|
|
/// Renumber the index list, providing space for new instructions.
|
2009-11-14 00:02:51 +00:00
|
|
|
void renumberIndexes();
|
2009-11-03 23:52:08 +00:00
|
|
|
|
2013-02-20 06:46:41 +00:00
|
|
|
/// Repair indexes after adding and removing instructions.
|
|
|
|
void repairIndexesInRange(MachineBasicBlock *MBB,
|
|
|
|
MachineBasicBlock::iterator Begin,
|
|
|
|
MachineBasicBlock::iterator End);
|
|
|
|
|
2009-11-03 23:52:08 +00:00
|
|
|
/// Returns the zero index for this analysis.
|
|
|
|
SlotIndex getZeroIndex() {
|
2012-04-17 04:15:51 +00:00
|
|
|
assert(indexList.front().getIndex() == 0 && "First index is not 0?");
|
|
|
|
return SlotIndex(&indexList.front(), 0);
|
2009-11-03 23:52:08 +00:00
|
|
|
}
|
|
|
|
|
2010-07-19 15:22:28 +00:00
|
|
|
/// Returns the base index of the last slot in this analysis.
|
|
|
|
SlotIndex getLastIndex() {
|
2012-04-17 04:15:51 +00:00
|
|
|
return SlotIndex(&indexList.back(), 0);
|
2010-07-19 15:22:28 +00:00
|
|
|
}
|
|
|
|
|
2009-11-03 23:52:08 +00:00
|
|
|
/// Returns true if the given machine instr is mapped to an index,
|
|
|
|
/// otherwise returns false.
|
|
|
|
bool hasIndex(const MachineInstr *instr) const {
|
2012-01-04 21:41:24 +00:00
|
|
|
return mi2iMap.count(instr);
|
2009-11-03 23:52:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the base index for the given instruction.
|
2012-02-28 23:52:31 +00:00
|
|
|
SlotIndex getInstructionIndex(const MachineInstr *MI) const {
|
|
|
|
// Instructions inside a bundle have the same number as the bundle itself.
|
2012-03-01 01:26:01 +00:00
|
|
|
Mi2IndexMap::const_iterator itr = mi2iMap.find(getBundleStart(MI));
|
2009-11-03 23:52:08 +00:00
|
|
|
assert(itr != mi2iMap.end() && "Instruction not found in maps.");
|
|
|
|
return itr->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the instruction for the given index, or null if the given
|
|
|
|
/// index has no instruction associated with it.
|
|
|
|
MachineInstr* getInstructionFromIndex(SlotIndex index) const {
|
2012-04-17 04:15:51 +00:00
|
|
|
return index.isValid() ? index.listEntry()->getInstr() : 0;
|
2009-11-03 23:52:08 +00:00
|
|
|
}
|
|
|
|
|
2013-03-08 18:08:54 +00:00
|
|
|
/// Returns the next non-null index, if one exists.
|
|
|
|
/// Otherwise returns getLastIndex().
|
|
|
|
SlotIndex getNextNonNullIndex(SlotIndex Index) {
|
|
|
|
IndexList::iterator I = Index.listEntry();
|
|
|
|
IndexList::iterator E = indexList.end();
|
2013-03-08 18:36:36 +00:00
|
|
|
while (++I != E)
|
|
|
|
if (I->getInstr())
|
2013-03-08 18:08:54 +00:00
|
|
|
return SlotIndex(I, Index.getSlot());
|
|
|
|
// We reached the end of the function.
|
|
|
|
return getLastIndex();
|
2009-11-03 23:52:08 +00:00
|
|
|
}
|
|
|
|
|
2011-05-02 05:29:56 +00:00
|
|
|
/// getIndexBefore - Returns the index of the last indexed instruction
|
2012-07-23 08:51:15 +00:00
|
|
|
/// before MI, or the start index of its basic block.
|
2011-05-02 05:29:56 +00:00
|
|
|
/// MI is not required to have an index.
|
|
|
|
SlotIndex getIndexBefore(const MachineInstr *MI) const {
|
|
|
|
const MachineBasicBlock *MBB = MI->getParent();
|
|
|
|
assert(MBB && "MI must be inserted inna basic block");
|
|
|
|
MachineBasicBlock::const_iterator I = MI, B = MBB->begin();
|
|
|
|
for (;;) {
|
|
|
|
if (I == B)
|
|
|
|
return getMBBStartIdx(MBB);
|
|
|
|
--I;
|
|
|
|
Mi2IndexMap::const_iterator MapItr = mi2iMap.find(I);
|
|
|
|
if (MapItr != mi2iMap.end())
|
|
|
|
return MapItr->second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// getIndexAfter - Returns the index of the first indexed instruction
|
|
|
|
/// after MI, or the end index of its basic block.
|
|
|
|
/// MI is not required to have an index.
|
|
|
|
SlotIndex getIndexAfter(const MachineInstr *MI) const {
|
|
|
|
const MachineBasicBlock *MBB = MI->getParent();
|
|
|
|
assert(MBB && "MI must be inserted inna basic block");
|
|
|
|
MachineBasicBlock::const_iterator I = MI, E = MBB->end();
|
|
|
|
for (;;) {
|
|
|
|
++I;
|
|
|
|
if (I == E)
|
|
|
|
return getMBBEndIdx(MBB);
|
|
|
|
Mi2IndexMap::const_iterator MapItr = mi2iMap.find(I);
|
|
|
|
if (MapItr != mi2iMap.end())
|
|
|
|
return MapItr->second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-02 06:03:31 +00:00
|
|
|
/// Return the (start,end) range of the given basic block number.
|
|
|
|
const std::pair<SlotIndex, SlotIndex> &
|
|
|
|
getMBBRange(unsigned Num) const {
|
|
|
|
return MBBRanges[Num];
|
|
|
|
}
|
|
|
|
|
2010-12-15 20:40:22 +00:00
|
|
|
/// Return the (start,end) range of the given basic block.
|
|
|
|
const std::pair<SlotIndex, SlotIndex> &
|
2011-04-02 06:03:31 +00:00
|
|
|
getMBBRange(const MachineBasicBlock *MBB) const {
|
|
|
|
return getMBBRange(MBB->getNumber());
|
2010-12-15 20:40:22 +00:00
|
|
|
}
|
|
|
|
|
2011-04-04 15:32:15 +00:00
|
|
|
/// Returns the first index in the given basic block number.
|
|
|
|
SlotIndex getMBBStartIdx(unsigned Num) const {
|
|
|
|
return getMBBRange(Num).first;
|
|
|
|
}
|
|
|
|
|
2010-12-15 20:40:22 +00:00
|
|
|
/// Returns the first index in the given basic block.
|
|
|
|
SlotIndex getMBBStartIdx(const MachineBasicBlock *mbb) const {
|
|
|
|
return getMBBRange(mbb).first;
|
2009-11-03 23:52:08 +00:00
|
|
|
}
|
|
|
|
|
2011-04-04 15:32:15 +00:00
|
|
|
/// Returns the last index in the given basic block number.
|
|
|
|
SlotIndex getMBBEndIdx(unsigned Num) const {
|
|
|
|
return getMBBRange(Num).second;
|
|
|
|
}
|
|
|
|
|
2009-11-03 23:52:08 +00:00
|
|
|
/// Returns the last index in the given basic block.
|
|
|
|
SlotIndex getMBBEndIdx(const MachineBasicBlock *mbb) const {
|
2010-12-15 20:40:22 +00:00
|
|
|
return getMBBRange(mbb).second;
|
2009-11-03 23:52:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the basic block which the given index falls in.
|
|
|
|
MachineBasicBlock* getMBBFromIndex(SlotIndex index) const {
|
2011-04-11 20:01:44 +00:00
|
|
|
if (MachineInstr *MI = getInstructionFromIndex(index))
|
|
|
|
return MI->getParent();
|
2011-04-02 06:03:31 +00:00
|
|
|
SmallVectorImpl<IdxMBBPair>::const_iterator I =
|
2009-11-03 23:52:08 +00:00
|
|
|
std::lower_bound(idx2MBBMap.begin(), idx2MBBMap.end(), index);
|
|
|
|
// Take the pair containing the index
|
2011-04-02 06:03:31 +00:00
|
|
|
SmallVectorImpl<IdxMBBPair>::const_iterator J =
|
2009-11-03 23:52:08 +00:00
|
|
|
((I != idx2MBBMap.end() && I->first > index) ||
|
|
|
|
(I == idx2MBBMap.end() && idx2MBBMap.size()>0)) ? (I-1): I;
|
|
|
|
|
|
|
|
assert(J != idx2MBBMap.end() && J->first <= index &&
|
2009-12-22 00:11:50 +00:00
|
|
|
index < getMBBEndIdx(J->second) &&
|
2009-11-03 23:52:08 +00:00
|
|
|
"index does not correspond to an MBB");
|
|
|
|
return J->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool findLiveInMBBs(SlotIndex start, SlotIndex end,
|
|
|
|
SmallVectorImpl<MachineBasicBlock*> &mbbs) const {
|
2011-04-02 06:03:31 +00:00
|
|
|
SmallVectorImpl<IdxMBBPair>::const_iterator itr =
|
2009-11-03 23:52:08 +00:00
|
|
|
std::lower_bound(idx2MBBMap.begin(), idx2MBBMap.end(), start);
|
|
|
|
bool resVal = false;
|
|
|
|
|
|
|
|
while (itr != idx2MBBMap.end()) {
|
|
|
|
if (itr->first >= end)
|
|
|
|
break;
|
|
|
|
mbbs.push_back(itr->second);
|
|
|
|
resVal = true;
|
|
|
|
++itr;
|
|
|
|
}
|
|
|
|
return resVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the MBB covering the given range, or null if the range covers
|
|
|
|
/// more than one basic block.
|
|
|
|
MachineBasicBlock* getMBBCoveringRange(SlotIndex start, SlotIndex end) const {
|
|
|
|
|
|
|
|
assert(start < end && "Backwards ranges not allowed.");
|
|
|
|
|
2011-04-02 06:03:31 +00:00
|
|
|
SmallVectorImpl<IdxMBBPair>::const_iterator itr =
|
2009-11-03 23:52:08 +00:00
|
|
|
std::lower_bound(idx2MBBMap.begin(), idx2MBBMap.end(), start);
|
|
|
|
|
|
|
|
if (itr == idx2MBBMap.end()) {
|
|
|
|
itr = prior(itr);
|
|
|
|
return itr->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that we don't cross the boundary into this block.
|
|
|
|
if (itr->first < end)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
itr = prior(itr);
|
|
|
|
|
|
|
|
if (itr->first <= start)
|
|
|
|
return itr->second;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-11-14 00:02:51 +00:00
|
|
|
/// Insert the given machine instruction into the mapping. Returns the
|
|
|
|
/// assigned index.
|
2011-05-02 05:29:56 +00:00
|
|
|
/// If Late is set and there are null indexes between mi's neighboring
|
|
|
|
/// instructions, create the new index after the null indexes instead of
|
|
|
|
/// before them.
|
|
|
|
SlotIndex insertMachineInstrInMaps(MachineInstr *mi, bool Late = false) {
|
2012-02-15 04:33:48 +00:00
|
|
|
assert(!mi->isInsideBundle() &&
|
|
|
|
"Instructions inside bundles should use bundle start's slot.");
|
2009-11-14 00:02:51 +00:00
|
|
|
assert(mi2iMap.find(mi) == mi2iMap.end() && "Instr already indexed.");
|
2011-01-12 21:27:45 +00:00
|
|
|
// Numbering DBG_VALUE instructions could cause code generation to be
|
|
|
|
// affected by debug information.
|
|
|
|
assert(!mi->isDebugValue() && "Cannot number DBG_VALUE instructions.");
|
2009-11-03 23:52:08 +00:00
|
|
|
|
2011-05-02 05:49:01 +00:00
|
|
|
assert(mi->getParent() != 0 && "Instr must be added to function.");
|
2009-11-03 23:52:08 +00:00
|
|
|
|
2011-05-02 05:29:56 +00:00
|
|
|
// Get the entries where mi should be inserted.
|
2012-04-17 04:15:51 +00:00
|
|
|
IndexList::iterator prevItr, nextItr;
|
2011-05-02 05:29:56 +00:00
|
|
|
if (Late) {
|
|
|
|
// Insert mi's index immediately before the following instruction.
|
2012-04-17 04:15:51 +00:00
|
|
|
nextItr = getIndexAfter(mi).listEntry();
|
|
|
|
prevItr = prior(nextItr);
|
2011-05-02 05:29:56 +00:00
|
|
|
} else {
|
2012-06-02 10:20:22 +00:00
|
|
|
// Insert mi's index immediately after the preceding instruction.
|
2012-04-17 04:15:51 +00:00
|
|
|
prevItr = getIndexBefore(mi).listEntry();
|
2012-04-17 09:37:32 +00:00
|
|
|
nextItr = llvm::next(prevItr);
|
2009-11-03 23:52:08 +00:00
|
|
|
}
|
|
|
|
|
2009-11-14 00:02:51 +00:00
|
|
|
// Get a number for the new instr, or 0 if there's no room currently.
|
|
|
|
// In the latter case we'll force a renumber later.
|
2012-04-17 04:15:51 +00:00
|
|
|
unsigned dist = ((nextItr->getIndex() - prevItr->getIndex())/2) & ~3u;
|
|
|
|
unsigned newNumber = prevItr->getIndex() + dist;
|
2009-11-03 23:52:08 +00:00
|
|
|
|
2009-11-14 00:02:51 +00:00
|
|
|
// Insert a new list entry for mi.
|
2012-04-17 04:15:51 +00:00
|
|
|
IndexList::iterator newItr =
|
|
|
|
indexList.insert(nextItr, createEntry(mi, newNumber));
|
2009-11-03 23:52:08 +00:00
|
|
|
|
2011-03-04 19:43:38 +00:00
|
|
|
// Renumber locally if we need to.
|
|
|
|
if (dist == 0)
|
2012-04-17 04:15:51 +00:00
|
|
|
renumberIndexes(newItr);
|
2009-11-03 23:52:08 +00:00
|
|
|
|
2012-04-17 04:15:51 +00:00
|
|
|
SlotIndex newIndex(&*newItr, SlotIndex::Slot_Block);
|
2011-03-04 19:43:38 +00:00
|
|
|
mi2iMap.insert(std::make_pair(mi, newIndex));
|
2009-11-14 00:02:51 +00:00
|
|
|
return newIndex;
|
2009-11-03 23:52:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Remove the given machine instruction from the mapping.
|
|
|
|
void removeMachineInstrFromMaps(MachineInstr *mi) {
|
|
|
|
// remove index -> MachineInstr and
|
|
|
|
// MachineInstr -> index mappings
|
|
|
|
Mi2IndexMap::iterator mi2iItr = mi2iMap.find(mi);
|
|
|
|
if (mi2iItr != mi2iMap.end()) {
|
2012-04-17 04:15:51 +00:00
|
|
|
IndexListEntry *miEntry(mi2iItr->second.listEntry());
|
2009-11-03 23:52:08 +00:00
|
|
|
assert(miEntry->getInstr() == mi && "Instruction indexes broken.");
|
|
|
|
// FIXME: Eventually we want to actually delete these indexes.
|
|
|
|
miEntry->setInstr(0);
|
|
|
|
mi2iMap.erase(mi2iItr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// ReplaceMachineInstrInMaps - Replacing a machine instr with a new one in
|
|
|
|
/// maps used by register allocator.
|
|
|
|
void replaceMachineInstrInMaps(MachineInstr *mi, MachineInstr *newMI) {
|
|
|
|
Mi2IndexMap::iterator mi2iItr = mi2iMap.find(mi);
|
|
|
|
if (mi2iItr == mi2iMap.end())
|
|
|
|
return;
|
|
|
|
SlotIndex replaceBaseIndex = mi2iItr->second;
|
2012-04-17 04:15:51 +00:00
|
|
|
IndexListEntry *miEntry(replaceBaseIndex.listEntry());
|
2009-11-03 23:52:08 +00:00
|
|
|
assert(miEntry->getInstr() == mi &&
|
|
|
|
"Mismatched instruction in index tables.");
|
|
|
|
miEntry->setInstr(newMI);
|
|
|
|
mi2iMap.erase(mi2iItr);
|
|
|
|
mi2iMap.insert(std::make_pair(newMI, replaceBaseIndex));
|
|
|
|
}
|
|
|
|
|
2010-07-09 09:19:23 +00:00
|
|
|
/// Add the given MachineBasicBlock into the maps.
|
|
|
|
void insertMBBInMaps(MachineBasicBlock *mbb) {
|
|
|
|
MachineFunction::iterator nextMBB =
|
|
|
|
llvm::next(MachineFunction::iterator(mbb));
|
|
|
|
|
2013-02-11 09:24:42 +00:00
|
|
|
IndexListEntry *startEntry = 0;
|
|
|
|
IndexListEntry *endEntry = 0;
|
2013-02-12 03:49:22 +00:00
|
|
|
IndexList::iterator newItr;
|
2013-02-11 09:24:42 +00:00
|
|
|
if (nextMBB == mbb->getParent()->end()) {
|
|
|
|
startEntry = &indexList.back();
|
|
|
|
endEntry = createEntry(0, 0);
|
2013-02-12 03:49:22 +00:00
|
|
|
newItr = indexList.insertAfter(startEntry, endEntry);
|
2013-02-11 09:24:42 +00:00
|
|
|
} else {
|
|
|
|
startEntry = createEntry(0, 0);
|
|
|
|
endEntry = getMBBStartIdx(nextMBB).listEntry();
|
2013-02-12 03:49:22 +00:00
|
|
|
newItr = indexList.insert(endEntry, startEntry);
|
2013-02-11 09:24:42 +00:00
|
|
|
}
|
2010-07-09 09:19:23 +00:00
|
|
|
|
2011-11-13 20:45:27 +00:00
|
|
|
SlotIndex startIdx(startEntry, SlotIndex::Slot_Block);
|
2013-02-11 09:24:42 +00:00
|
|
|
SlotIndex endIdx(endEntry, SlotIndex::Slot_Block);
|
|
|
|
|
|
|
|
MachineFunction::iterator prevMBB(mbb);
|
|
|
|
assert(prevMBB != mbb->getParent()->end() &&
|
|
|
|
"Can't insert a new block at the beginning of a function.");
|
|
|
|
--prevMBB;
|
|
|
|
MBBRanges[prevMBB->getNumber()].second = startIdx;
|
2010-07-09 09:19:23 +00:00
|
|
|
|
2011-04-02 06:03:31 +00:00
|
|
|
assert(unsigned(mbb->getNumber()) == MBBRanges.size() &&
|
|
|
|
"Blocks must be added in order");
|
|
|
|
MBBRanges.push_back(std::make_pair(startIdx, endIdx));
|
2010-07-09 09:19:23 +00:00
|
|
|
idx2MBBMap.push_back(IdxMBBPair(startIdx, mbb));
|
|
|
|
|
2013-02-12 03:49:22 +00:00
|
|
|
renumberIndexes(newItr);
|
2010-07-09 09:19:23 +00:00
|
|
|
std::sort(idx2MBBMap.begin(), idx2MBBMap.end(), Idx2MBBCompare());
|
|
|
|
}
|
|
|
|
|
2013-04-19 04:31:49 +00:00
|
|
|
/// \brief Free the resources that were required to maintain a SlotIndex.
|
|
|
|
///
|
|
|
|
/// Once an index is no longer needed (for instance because the instruction
|
|
|
|
/// at that index has been moved), the resources required to maintain the
|
|
|
|
/// index can be relinquished to reduce memory use and improve renumbering
|
|
|
|
/// performance. Any remaining SlotIndex objects that point to the same
|
|
|
|
/// index are left 'dangling' (much the same as a dangling pointer to a
|
|
|
|
/// freed object) and should not be accessed, except to destruct them.
|
2013-07-30 19:59:15 +00:00
|
|
|
///
|
2013-04-19 04:31:49 +00:00
|
|
|
/// Like dangling pointers, access to dangling SlotIndexes can cause
|
|
|
|
/// painful-to-track-down bugs, especially if the memory for the index
|
|
|
|
/// previously pointed to has been re-used. To detect dangling SlotIndex
|
|
|
|
/// bugs, build with EXPENSIVE_CHECKS=1. This will cause "erased" indexes to
|
|
|
|
/// be retained in a graveyard instead of being freed. Operations on indexes
|
|
|
|
/// in the graveyard will trigger an assertion.
|
|
|
|
void eraseIndex(SlotIndex index) {
|
|
|
|
IndexListEntry *entry = index.listEntry();
|
|
|
|
#ifdef EXPENSIVE_CHECKS
|
|
|
|
indexList.remove(entry);
|
|
|
|
graveyardList.push_back(entry);
|
|
|
|
entry->setPoison();
|
|
|
|
#else
|
|
|
|
indexList.erase(entry);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-11-03 23:52:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-12-02 00:37:37 +00:00
|
|
|
// Specialize IntervalMapInfo for half-open slot index intervals.
|
2012-12-27 11:29:17 +00:00
|
|
|
template <>
|
|
|
|
struct IntervalMapInfo<SlotIndex> : IntervalMapHalfOpenInfo<SlotIndex> {
|
2010-12-02 00:37:37 +00:00
|
|
|
};
|
|
|
|
|
2009-11-03 23:52:08 +00:00
|
|
|
}
|
|
|
|
|
2012-04-17 04:15:51 +00:00
|
|
|
#endif // LLVM_CODEGEN_SLOTINDEXES_H
|