1 //===----- HexagonShuffler.h - Instruction bundle shuffling ---------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This implements the shuffling of insns inside a bundle according to the
11 // packet formation rules of the Hexagon ISA.
13 //===----------------------------------------------------------------------===//
15 #ifndef HEXAGONSHUFFLER_H
16 #define HEXAGONSHUFFLER_H
19 #include "MCTargetDesc/HexagonMCInstrInfo.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/MC/MCInstrInfo.h"
28 class HexagonResource {
29 // Mask of the slots or units that may execute the insn and
30 // the weight or priority that the insn requires to be assigned a slot.
31 unsigned Slots, Weight;
34 HexagonResource(unsigned s) { setUnits(s); };
36 void setUnits(unsigned s) {
37 Slots = s & ~(-1 << HEXAGON_PACKET_SIZE);
40 unsigned setWeight(unsigned s);
42 unsigned getUnits() const { return (Slots); };
43 unsigned getWeight() const { return (Weight); };
45 // Check if the resources are in ascending slot order.
46 static bool lessUnits(const HexagonResource &A, const HexagonResource &B) {
47 return (countPopulation(A.getUnits()) < countPopulation(B.getUnits()));
49 // Check if the resources are in ascending weight order.
50 static bool lessWeight(const HexagonResource &A, const HexagonResource &B) {
51 return (A.getWeight() < B.getWeight());
55 // Handle to an insn used by the shuffling algorithm.
57 friend class HexagonShuffler;
60 MCInst const *Extender;
65 HexagonInstr(MCInst const *id, MCInst const *Extender, unsigned s,
67 : ID(id), Extender(Extender), Core(s), SoloException(x){};
69 MCInst const *getDesc() const { return (ID); };
71 MCInst const *getExtender() const { return Extender; }
73 unsigned isSoloException() const { return (SoloException); };
75 // Check if the handles are in ascending order for shuffling purposes.
76 bool operator<(const HexagonInstr &B) const {
77 return (HexagonResource::lessWeight(B.Core, Core));
79 // Check if the handles are in ascending order by core slots.
80 static bool lessCore(const HexagonInstr &A, const HexagonInstr &B) {
81 return (HexagonResource::lessUnits(A.Core, B.Core));
86 class HexagonShuffler {
87 typedef SmallVector<HexagonInstr, HEXAGON_PRESHUFFLE_PACKET_SIZE>
90 // Insn handles in a bundle.
93 // Shuffling error code.
98 MCInstrInfo const &MCII;
99 MCSubtargetInfo const &STI;
102 typedef HexagonPacket::iterator iterator;
105 SHUFFLE_SUCCESS = 0, ///< Successful operation.
106 SHUFFLE_ERROR_INVALID, ///< Invalid bundle.
107 SHUFFLE_ERROR_STORES, ///< No free slots for store insns.
108 SHUFFLE_ERROR_LOADS, ///< No free slots for load insns.
109 SHUFFLE_ERROR_BRANCHES, ///< No free slots for branch insns.
110 SHUFFLE_ERROR_NOSLOTS, ///< No free slots for other insns.
111 SHUFFLE_ERROR_SLOTS, ///< Over-subscribed slots.
112 SHUFFLE_ERROR_UNKNOWN ///< Unknown error.
115 explicit HexagonShuffler(MCInstrInfo const &MCII, MCSubtargetInfo const &STI);
117 // Reset to initial state.
119 // Check if the bundle may be validly shuffled.
121 // Reorder the insn handles in the bundle.
124 unsigned size() const { return (Packet.size()); };
126 iterator begin() { return (Packet.begin()); };
127 iterator end() { return (Packet.end()); };
129 // Add insn handle to the bundle .
130 void append(MCInst const *ID, MCInst const *Extender, unsigned S,
133 // Return the error code for the last check or shuffling of the bundle.
134 void setError(unsigned Err) { Error = Err; };
135 unsigned getError() const { return (Error); };
139 #endif // HEXAGONSHUFFLER_H