2017-08-02 05:20:10 +08:00
|
|
|
//===- HexagonShuffler.h - Instruction bundle shuffling ---------*- C++ -*-===//
|
2015-06-01 05:57:09 +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
|
2015-06-01 05:57:09 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This implements the shuffling of insns inside a bundle according to the
|
|
|
|
// packet formation rules of the Hexagon ISA.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-08-02 05:20:10 +08:00
|
|
|
#ifndef LLVM_LIB_TARGET_HEXAGON_MCTARGETDESC_HEXAGONSHUFFLER_H
|
|
|
|
#define LLVM_LIB_TARGET_HEXAGON_MCTARGETDESC_HEXAGONSHUFFLER_H
|
2015-06-01 05:57:09 +08:00
|
|
|
|
2017-12-12 02:57:54 +08:00
|
|
|
#include "MCTargetDesc/HexagonMCInstrInfo.h"
|
2019-06-03 17:33:48 +08:00
|
|
|
#include "MCTargetDesc/HexagonMCTargetDesc.h"
|
2017-08-02 05:20:10 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2020-01-18 02:18:59 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2015-06-01 05:57:09 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2017-08-02 05:20:10 +08:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/Support/MathExtras.h"
|
|
|
|
#include "llvm/Support/SMLoc.h"
|
|
|
|
#include <cstdint>
|
2020-01-18 02:18:59 +08:00
|
|
|
#include <functional>
|
2017-08-02 05:20:10 +08:00
|
|
|
#include <utility>
|
2015-06-01 05:57:09 +08:00
|
|
|
|
|
|
|
namespace llvm {
|
2017-08-02 05:20:10 +08:00
|
|
|
|
|
|
|
class MCContext;
|
|
|
|
class MCInst;
|
|
|
|
class MCInstrInfo;
|
|
|
|
class MCSubtargetInfo;
|
|
|
|
|
2015-06-01 05:57:09 +08:00
|
|
|
// Insn resources.
|
|
|
|
class HexagonResource {
|
|
|
|
// Mask of the slots or units that may execute the insn and
|
|
|
|
// the weight or priority that the insn requires to be assigned a slot.
|
|
|
|
unsigned Slots, Weight;
|
|
|
|
|
|
|
|
public:
|
2017-08-02 05:20:10 +08:00
|
|
|
HexagonResource(unsigned s) { setUnits(s); }
|
2015-06-01 05:57:09 +08:00
|
|
|
|
|
|
|
void setUnits(unsigned s) {
|
2017-02-07 10:31:53 +08:00
|
|
|
Slots = s & ((1u << HEXAGON_PACKET_SIZE) - 1);
|
2017-02-07 03:35:46 +08:00
|
|
|
setWeight(s);
|
2017-08-02 05:20:10 +08:00
|
|
|
}
|
|
|
|
|
2015-06-01 05:57:09 +08:00
|
|
|
unsigned setWeight(unsigned s);
|
|
|
|
|
2017-08-02 05:20:10 +08:00
|
|
|
unsigned getUnits() const { return (Slots); }
|
|
|
|
unsigned getWeight() const { return (Weight); }
|
2015-06-01 05:57:09 +08:00
|
|
|
|
|
|
|
// Check if the resources are in ascending slot order.
|
|
|
|
static bool lessUnits(const HexagonResource &A, const HexagonResource &B) {
|
2017-05-02 03:41:43 +08:00
|
|
|
return (countPopulation(A.getUnits()) < countPopulation(B.getUnits()));
|
2017-08-02 05:20:10 +08:00
|
|
|
}
|
|
|
|
|
2015-06-01 05:57:09 +08:00
|
|
|
// Check if the resources are in ascending weight order.
|
|
|
|
static bool lessWeight(const HexagonResource &A, const HexagonResource &B) {
|
|
|
|
return (A.getWeight() < B.getWeight());
|
2017-08-02 05:20:10 +08:00
|
|
|
}
|
2015-06-01 05:57:09 +08:00
|
|
|
};
|
|
|
|
|
2015-12-04 05:44:28 +08:00
|
|
|
// HVX insn resources.
|
|
|
|
class HexagonCVIResource : public HexagonResource {
|
2016-01-09 06:07:25 +08:00
|
|
|
public:
|
2017-08-02 05:20:10 +08:00
|
|
|
using UnitsAndLanes = std::pair<unsigned, unsigned>;
|
2015-12-04 05:44:28 +08:00
|
|
|
|
2016-01-09 06:07:25 +08:00
|
|
|
private:
|
2015-12-04 05:44:28 +08:00
|
|
|
// Available HVX slots.
|
|
|
|
enum {
|
|
|
|
CVI_NONE = 0,
|
|
|
|
CVI_XLANE = 1 << 0,
|
|
|
|
CVI_SHIFT = 1 << 1,
|
|
|
|
CVI_MPY0 = 1 << 2,
|
2018-12-06 04:18:09 +08:00
|
|
|
CVI_MPY1 = 1 << 3,
|
|
|
|
CVI_ZW = 1 << 4
|
2015-12-04 05:44:28 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// Count of adjacent slots that the insn requires to be executed.
|
|
|
|
unsigned Lanes;
|
|
|
|
// Flag whether the insn is a load or a store.
|
|
|
|
bool Load, Store;
|
|
|
|
// Flag whether the HVX resources are valid.
|
|
|
|
bool Valid;
|
|
|
|
|
2017-08-02 05:20:10 +08:00
|
|
|
void setLanes(unsigned l) { Lanes = l; }
|
|
|
|
void setLoad(bool f = true) { Load = f; }
|
|
|
|
void setStore(bool f = true) { Store = f; }
|
2015-12-04 05:44:28 +08:00
|
|
|
|
|
|
|
public:
|
2020-01-18 03:13:28 +08:00
|
|
|
HexagonCVIResource(MCInstrInfo const &MCII,
|
|
|
|
MCSubtargetInfo const &STI,
|
2016-01-09 06:07:25 +08:00
|
|
|
unsigned s, MCInst const *id);
|
2017-08-02 05:20:10 +08:00
|
|
|
|
|
|
|
bool isValid() const { return Valid; }
|
|
|
|
unsigned getLanes() const { return Lanes; }
|
|
|
|
bool mayLoad() const { return Load; }
|
|
|
|
bool mayStore() const { return Store; }
|
2015-12-04 05:44:28 +08:00
|
|
|
};
|
|
|
|
|
2015-06-01 05:57:09 +08:00
|
|
|
// Handle to an insn used by the shuffling algorithm.
|
|
|
|
class HexagonInstr {
|
|
|
|
friend class HexagonShuffler;
|
|
|
|
|
|
|
|
MCInst const *ID;
|
|
|
|
MCInst const *Extender;
|
|
|
|
HexagonResource Core;
|
2015-12-04 05:44:28 +08:00
|
|
|
HexagonCVIResource CVI;
|
2015-06-01 05:57:09 +08:00
|
|
|
|
|
|
|
public:
|
2020-01-18 03:13:28 +08:00
|
|
|
HexagonInstr(MCInstrInfo const &MCII,
|
|
|
|
MCSubtargetInfo const &STI, MCInst const *id,
|
2017-05-03 01:58:52 +08:00
|
|
|
MCInst const *Extender, unsigned s)
|
2020-01-18 03:13:28 +08:00
|
|
|
: ID(id), Extender(Extender), Core(s), CVI(MCII, STI, s, id){};
|
2015-06-01 05:57:09 +08:00
|
|
|
|
2017-08-02 05:20:10 +08:00
|
|
|
MCInst const &getDesc() const { return *ID; }
|
2015-06-01 05:57:09 +08:00
|
|
|
MCInst const *getExtender() const { return Extender; }
|
|
|
|
|
|
|
|
// Check if the handles are in ascending order for shuffling purposes.
|
|
|
|
bool operator<(const HexagonInstr &B) const {
|
|
|
|
return (HexagonResource::lessWeight(B.Core, Core));
|
2017-08-02 05:20:10 +08:00
|
|
|
}
|
|
|
|
|
2015-06-01 05:57:09 +08:00
|
|
|
// Check if the handles are in ascending order by core slots.
|
|
|
|
static bool lessCore(const HexagonInstr &A, const HexagonInstr &B) {
|
|
|
|
return (HexagonResource::lessUnits(A.Core, B.Core));
|
2017-08-02 05:20:10 +08:00
|
|
|
}
|
|
|
|
|
2015-12-04 05:44:28 +08:00
|
|
|
// Check if the handles are in ascending order by HVX slots.
|
|
|
|
static bool lessCVI(const HexagonInstr &A, const HexagonInstr &B) {
|
|
|
|
return (HexagonResource::lessUnits(A.CVI, B.CVI));
|
2017-08-02 05:20:10 +08:00
|
|
|
}
|
2015-06-01 05:57:09 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// Bundle shuffler.
|
|
|
|
class HexagonShuffler {
|
2017-08-02 05:20:10 +08:00
|
|
|
using HexagonPacket =
|
|
|
|
SmallVector<HexagonInstr, HEXAGON_PRESHUFFLE_PACKET_SIZE>;
|
2015-06-01 05:57:09 +08:00
|
|
|
|
2020-01-18 02:18:59 +08:00
|
|
|
struct HexagonPacketSummary {
|
|
|
|
// Number of memory operations, loads, solo loads, stores, solo stores,
|
|
|
|
// single stores.
|
|
|
|
unsigned memory;
|
|
|
|
unsigned loads;
|
|
|
|
unsigned load0;
|
|
|
|
unsigned stores;
|
|
|
|
unsigned store0;
|
|
|
|
unsigned store1;
|
|
|
|
unsigned NonZCVIloads;
|
|
|
|
unsigned AllCVIloads;
|
|
|
|
unsigned CVIstores;
|
|
|
|
// Number of duplex insns
|
|
|
|
unsigned duplex;
|
|
|
|
unsigned pSlot3Cnt;
|
|
|
|
Optional<HexagonInstr *> PrefSlot3Inst;
|
|
|
|
unsigned memops;
|
|
|
|
unsigned ReservedSlotMask;
|
|
|
|
SmallVector<HexagonInstr *, HEXAGON_PRESHUFFLE_PACKET_SIZE> branchInsts;
|
|
|
|
Optional<SMLoc> Slot1AOKLoc;
|
|
|
|
Optional<SMLoc> NoSlot1StoreLoc;
|
|
|
|
};
|
2015-06-01 05:57:09 +08:00
|
|
|
// Insn handles in a bundle.
|
|
|
|
HexagonPacket Packet;
|
|
|
|
|
|
|
|
protected:
|
2017-05-02 03:41:43 +08:00
|
|
|
MCContext &Context;
|
2015-06-01 05:57:09 +08:00
|
|
|
int64_t BundleFlags;
|
|
|
|
MCInstrInfo const &MCII;
|
|
|
|
MCSubtargetInfo const &STI;
|
2017-05-02 03:41:43 +08:00
|
|
|
SMLoc Loc;
|
|
|
|
bool ReportErrors;
|
2020-01-18 02:18:59 +08:00
|
|
|
bool CheckFailure;
|
2017-12-12 02:57:54 +08:00
|
|
|
std::vector<std::pair<SMLoc, std::string>> AppliedRestrictions;
|
2020-01-18 02:18:59 +08:00
|
|
|
bool applySlotRestrictions(HexagonPacketSummary const &Summary);
|
|
|
|
void restrictSlot1AOK(HexagonPacketSummary const &Summary);
|
|
|
|
void restrictNoSlot1Store(HexagonPacketSummary const &Summary);
|
|
|
|
void restrictNoSlot1();
|
|
|
|
|
|
|
|
Optional<HexagonPacket> tryAuction(HexagonPacketSummary const &Summary) const;
|
|
|
|
|
|
|
|
bool restrictStoreLoadOrder(HexagonPacketSummary const &Summary);
|
|
|
|
void restrictBranchOrder(HexagonPacketSummary const &Summary);
|
|
|
|
void restrictPreferSlot3(HexagonPacketSummary const &Summary);
|
|
|
|
HexagonPacketSummary GetPacketSummary();
|
|
|
|
bool ValidPacketMemoryOps(HexagonPacketSummary const &Summary) const;
|
|
|
|
bool ValidResourceUsage(HexagonPacketSummary const &Summary);
|
|
|
|
bool validPacketInsts() const;
|
2015-06-01 05:57:09 +08:00
|
|
|
|
|
|
|
public:
|
2017-08-02 05:20:10 +08:00
|
|
|
using iterator = HexagonPacket::iterator;
|
2020-01-18 02:18:59 +08:00
|
|
|
using const_iterator = HexagonPacket::const_iterator;
|
|
|
|
using packet_range = iterator_range<HexagonPacket::iterator>;
|
|
|
|
using const_packet_range = iterator_range<HexagonPacket::const_iterator>;
|
2015-06-01 05:57:09 +08:00
|
|
|
|
2017-05-02 03:41:43 +08:00
|
|
|
HexagonShuffler(MCContext &Context, bool ReportErrors,
|
|
|
|
MCInstrInfo const &MCII, MCSubtargetInfo const &STI);
|
2015-06-01 05:57:09 +08:00
|
|
|
|
|
|
|
// Reset to initial state.
|
|
|
|
void reset();
|
|
|
|
// Check if the bundle may be validly shuffled.
|
|
|
|
bool check();
|
|
|
|
// Reorder the insn handles in the bundle.
|
|
|
|
bool shuffle();
|
|
|
|
|
2017-08-02 05:20:10 +08:00
|
|
|
unsigned size() const { return (Packet.size()); }
|
2015-06-01 05:57:09 +08:00
|
|
|
|
2017-12-12 02:57:54 +08:00
|
|
|
bool isMemReorderDisabled() const {
|
|
|
|
return (BundleFlags & HexagonMCInstrInfo::memReorderDisabledMask) != 0;
|
|
|
|
}
|
|
|
|
|
2017-08-02 05:20:10 +08:00
|
|
|
iterator begin() { return (Packet.begin()); }
|
|
|
|
iterator end() { return (Packet.end()); }
|
2020-01-18 02:18:59 +08:00
|
|
|
const_iterator cbegin() const { return (Packet.begin()); }
|
|
|
|
const_iterator cend() const { return (Packet.end()); }
|
|
|
|
packet_range insts(HexagonPacket &P) {
|
|
|
|
return make_range(P.begin(), P.end());
|
|
|
|
}
|
|
|
|
const_packet_range insts(HexagonPacket const &P) const {
|
|
|
|
return make_range(P.begin(), P.end());
|
|
|
|
}
|
|
|
|
packet_range insts() { return make_range(begin(), end()); }
|
|
|
|
const_packet_range insts() const { return make_range(cbegin(), cend()); }
|
|
|
|
|
|
|
|
using InstPredicate = bool (*)(MCInstrInfo const &, MCInst const &);
|
|
|
|
|
|
|
|
bool HasInstWith(InstPredicate Pred) const {
|
|
|
|
return llvm::any_of(make_range(cbegin(), cend()),
|
|
|
|
[&](HexagonInstr const &I) {
|
|
|
|
MCInst const &Inst = I.getDesc();
|
|
|
|
return (*Pred)(MCII, Inst);
|
|
|
|
});
|
|
|
|
}
|
2015-06-01 05:57:09 +08:00
|
|
|
|
|
|
|
// Add insn handle to the bundle .
|
2017-02-07 03:35:46 +08:00
|
|
|
void append(MCInst const &ID, MCInst const *Extender, unsigned S);
|
2015-06-01 05:57:09 +08:00
|
|
|
|
|
|
|
// Return the error code for the last check or shuffling of the bundle.
|
2017-08-02 05:20:10 +08:00
|
|
|
void reportError(Twine const &Msg);
|
2015-06-01 05:57:09 +08:00
|
|
|
};
|
|
|
|
|
2017-08-02 05:20:10 +08:00
|
|
|
} // end namespace llvm
|
|
|
|
|
|
|
|
#endif // LLVM_LIB_TARGET_HEXAGON_MCTARGETDESC_HEXAGONSHUFFLER_H
|