2010-12-11 02:36:02 +08:00
|
|
|
//===-- llvm/CodeGen/AllocationOrder.h - Allocation Order -*- C++ -*-------===//
|
|
|
|
//
|
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
|
2010-12-11 02:36:02 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements an allocation order for virtual registers.
|
|
|
|
//
|
|
|
|
// The preferred allocation order for a virtual register depends on allocation
|
|
|
|
// hints and target hooks. The AllocationOrder class encapsulates all of that.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-08-14 00:26:38 +08:00
|
|
|
#ifndef LLVM_LIB_CODEGEN_ALLOCATIONORDER_H
|
|
|
|
#define LLVM_LIB_CODEGEN_ALLOCATIONORDER_H
|
2010-12-11 02:36:02 +08:00
|
|
|
|
2012-12-04 06:51:04 +08:00
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
2016-08-12 06:21:41 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2020-09-24 12:58:45 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2020-10-23 01:30:30 +08:00
|
|
|
#include "llvm/CodeGen/Register.h"
|
2012-11-29 11:34:17 +08:00
|
|
|
|
2010-12-11 02:36:02 +08:00
|
|
|
namespace llvm {
|
|
|
|
|
2011-06-04 04:34:53 +08:00
|
|
|
class RegisterClassInfo;
|
2010-12-11 02:36:02 +08:00
|
|
|
class VirtRegMap;
|
2015-07-16 06:16:00 +08:00
|
|
|
class LiveRegMatrix;
|
2010-12-11 02:36:02 +08:00
|
|
|
|
2015-07-01 22:47:39 +08:00
|
|
|
class LLVM_LIBRARY_VISIBILITY AllocationOrder {
|
2020-09-29 07:41:28 +08:00
|
|
|
const SmallVector<MCPhysReg, 16> Hints;
|
2012-12-04 06:51:04 +08:00
|
|
|
ArrayRef<MCPhysReg> Order;
|
2020-09-24 12:58:45 +08:00
|
|
|
// How far into the Order we can iterate. This is 0 if the AllocationOrder is
|
|
|
|
// constructed with HardHints = true, Order.size() otherwise. While
|
|
|
|
// technically a size_t, it will participate in comparisons with the
|
|
|
|
// Iterator's Pos, which must be signed, so it's typed here as signed, too, to
|
|
|
|
// avoid warnings and under the assumption that the size of Order is
|
|
|
|
// relatively small.
|
|
|
|
// IterationLimit defines an invalid iterator position.
|
|
|
|
const int IterationLimit;
|
2017-11-10 16:46:26 +08:00
|
|
|
|
2012-12-04 06:51:04 +08:00
|
|
|
public:
|
2020-09-24 12:58:45 +08:00
|
|
|
/// Forward iterator for an AllocationOrder.
|
|
|
|
class Iterator final {
|
|
|
|
const AllocationOrder &AO;
|
|
|
|
int Pos = 0;
|
|
|
|
|
|
|
|
public:
|
|
|
|
Iterator(const AllocationOrder &AO, int Pos) : AO(AO), Pos(Pos) {}
|
|
|
|
|
|
|
|
/// Return true if the curent position is that of a preferred register.
|
|
|
|
bool isHint() const { return Pos < 0; }
|
|
|
|
|
|
|
|
/// Return the next physical register in the allocation order.
|
|
|
|
MCRegister operator*() const {
|
|
|
|
if (Pos < 0)
|
|
|
|
return AO.Hints.end()[Pos];
|
|
|
|
assert(Pos < AO.IterationLimit);
|
|
|
|
return AO.Order[Pos];
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Advance the iterator to the next position. If that's past the Hints
|
|
|
|
/// list, advance to the first value that's not also in the Hints list.
|
|
|
|
Iterator &operator++() {
|
|
|
|
if (Pos < AO.IterationLimit)
|
|
|
|
++Pos;
|
|
|
|
while (Pos >= 0 && Pos < AO.IterationLimit && AO.isHint(AO.Order[Pos]))
|
|
|
|
++Pos;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(const Iterator &Other) const {
|
|
|
|
assert(&AO == &Other.AO);
|
|
|
|
return Pos == Other.Pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const Iterator &Other) const { return !(*this == Other); }
|
|
|
|
};
|
2017-11-10 16:46:26 +08:00
|
|
|
|
2012-12-04 06:51:04 +08:00
|
|
|
/// Create a new AllocationOrder for VirtReg.
|
2010-12-11 02:36:02 +08:00
|
|
|
/// @param VirtReg Virtual register to allocate for.
|
|
|
|
/// @param VRM Virtual register map for function.
|
2012-01-25 02:09:18 +08:00
|
|
|
/// @param RegClassInfo Information about reserved and allocatable registers.
|
2020-09-29 07:41:28 +08:00
|
|
|
static AllocationOrder create(unsigned VirtReg, const VirtRegMap &VRM,
|
|
|
|
const RegisterClassInfo &RegClassInfo,
|
|
|
|
const LiveRegMatrix *Matrix);
|
|
|
|
|
|
|
|
/// Create an AllocationOrder given the Hits, Order, and HardHits values.
|
|
|
|
/// Use the create method above - the ctor is for unittests.
|
|
|
|
AllocationOrder(SmallVector<MCPhysReg, 16> &&Hints, ArrayRef<MCPhysReg> Order,
|
|
|
|
bool HardHints)
|
|
|
|
: Hints(std::move(Hints)), Order(Order),
|
2020-09-24 12:58:45 +08:00
|
|
|
IterationLimit(HardHints ? 0 : static_cast<int>(Order.size())) {}
|
2010-12-11 02:36:02 +08:00
|
|
|
|
2020-09-24 12:58:45 +08:00
|
|
|
Iterator begin() const {
|
|
|
|
return Iterator(*this, -(static_cast<int>(Hints.size())));
|
2012-12-05 06:25:16 +08:00
|
|
|
}
|
2011-06-07 05:02:04 +08:00
|
|
|
|
2020-09-24 12:58:45 +08:00
|
|
|
Iterator end() const { return Iterator(*this, IterationLimit); }
|
2010-12-11 02:36:02 +08:00
|
|
|
|
2020-09-24 12:58:45 +08:00
|
|
|
Iterator getOrderLimitEnd(unsigned OrderLimit) const {
|
|
|
|
assert(OrderLimit <= Order.size());
|
|
|
|
if (OrderLimit == 0)
|
|
|
|
return end();
|
|
|
|
Iterator Ret(*this,
|
|
|
|
std::min(static_cast<int>(OrderLimit) - 1, IterationLimit));
|
|
|
|
return ++Ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the allocation order without reordered hints.
|
|
|
|
ArrayRef<MCPhysReg> getOrder() const { return Order; }
|
2012-12-04 06:51:04 +08:00
|
|
|
|
2020-10-23 01:30:30 +08:00
|
|
|
/// Return true if Reg is a preferred physical register.
|
|
|
|
bool isHint(Register Reg) const {
|
|
|
|
assert(!Reg.isPhysical() ||
|
|
|
|
Reg.id() <
|
|
|
|
static_cast<uint32_t>(std::numeric_limits<MCPhysReg>::max()));
|
|
|
|
return Reg.isPhysical() && is_contained(Hints, Reg.id());
|
|
|
|
}
|
2010-12-11 02:36:02 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace llvm
|
|
|
|
|
|
|
|
#endif
|