LLVM  9.0.0svn
HexagonShuffler.h
Go to the documentation of this file.
1 //===- HexagonShuffler.h - Instruction bundle shuffling ---------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This implements the shuffling of insns inside a bundle according to the
10 // packet formation rules of the Hexagon ISA.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_LIB_TARGET_HEXAGON_MCTARGETDESC_HEXAGONSHUFFLER_H
15 #define LLVM_LIB_TARGET_HEXAGON_MCTARGETDESC_HEXAGONSHUFFLER_H
16 
17 #include "Hexagon.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/StringRef.h"
23 #include "llvm/Support/SMLoc.h"
24 #include <cstdint>
25 #include <utility>
26 
27 namespace llvm {
28 
29 class MCContext;
30 class MCInst;
31 class MCInstrInfo;
32 class MCSubtargetInfo;
33 
34 // Insn resources.
36  // Mask of the slots or units that may execute the insn and
37  // the weight or priority that the insn requires to be assigned a slot.
38  unsigned Slots, Weight;
39 
40 public:
41  HexagonResource(unsigned s) { setUnits(s); }
42 
43  void setUnits(unsigned s) {
44  Slots = s & ((1u << HEXAGON_PACKET_SIZE) - 1);
45  setWeight(s);
46  }
47 
48  unsigned setWeight(unsigned s);
49 
50  unsigned getUnits() const { return (Slots); }
51  unsigned getWeight() const { return (Weight); }
52 
53  // Check if the resources are in ascending slot order.
54  static bool lessUnits(const HexagonResource &A, const HexagonResource &B) {
55  return (countPopulation(A.getUnits()) < countPopulation(B.getUnits()));
56  }
57 
58  // Check if the resources are in ascending weight order.
59  static bool lessWeight(const HexagonResource &A, const HexagonResource &B) {
60  return (A.getWeight() < B.getWeight());
61  }
62 };
63 
64 // HVX insn resources.
66 public:
67  using UnitsAndLanes = std::pair<unsigned, unsigned>;
69 
70 private:
71  // Available HVX slots.
72  enum {
73  CVI_NONE = 0,
74  CVI_XLANE = 1 << 0,
75  CVI_SHIFT = 1 << 1,
76  CVI_MPY0 = 1 << 2,
77  CVI_MPY1 = 1 << 3,
78  CVI_ZW = 1 << 4
79  };
80 
81  // Count of adjacent slots that the insn requires to be executed.
82  unsigned Lanes;
83  // Flag whether the insn is a load or a store.
84  bool Load, Store;
85  // Flag whether the HVX resources are valid.
86  bool Valid;
87 
88  void setLanes(unsigned l) { Lanes = l; }
89  void setLoad(bool f = true) { Load = f; }
90  void setStore(bool f = true) { Store = f; }
91 
92 public:
94  unsigned s, MCInst const *id);
95 
96  static void SetupTUL(TypeUnitsAndLanes *TUL, StringRef CPU);
97 
98  bool isValid() const { return Valid; }
99  unsigned getLanes() const { return Lanes; }
100  bool mayLoad() const { return Load; }
101  bool mayStore() const { return Store; }
102 };
103 
104 // Handle to an insn used by the shuffling algorithm.
106  friend class HexagonShuffler;
107 
108  MCInst const *ID;
109  MCInst const *Extender;
110  HexagonResource Core;
111  HexagonCVIResource CVI;
112 
113 public:
115  MCInstrInfo const &MCII, MCInst const *id,
116  MCInst const *Extender, unsigned s)
117  : ID(id), Extender(Extender), Core(s), CVI(T, MCII, s, id) {}
118 
119  MCInst const &getDesc() const { return *ID; }
120  MCInst const *getExtender() const { return Extender; }
121 
122  // Check if the handles are in ascending order for shuffling purposes.
123  bool operator<(const HexagonInstr &B) const {
124  return (HexagonResource::lessWeight(B.Core, Core));
125  }
126 
127  // Check if the handles are in ascending order by core slots.
128  static bool lessCore(const HexagonInstr &A, const HexagonInstr &B) {
129  return (HexagonResource::lessUnits(A.Core, B.Core));
130  }
131 
132  // Check if the handles are in ascending order by HVX slots.
133  static bool lessCVI(const HexagonInstr &A, const HexagonInstr &B) {
134  return (HexagonResource::lessUnits(A.CVI, B.CVI));
135  }
136 };
137 
138 // Bundle shuffler.
140  using HexagonPacket =
142 
143  // Insn handles in a bundle.
144  HexagonPacket Packet;
145  HexagonPacket PacketSave;
146 
148 
149 protected:
151  int64_t BundleFlags;
156  std::vector<std::pair<SMLoc, std::string>> AppliedRestrictions;
157  void applySlotRestrictions();
158  void restrictSlot1AOK();
159  void restrictNoSlot1Store();
160 
161 public:
163 
164  HexagonShuffler(MCContext &Context, bool ReportErrors,
165  MCInstrInfo const &MCII, MCSubtargetInfo const &STI);
166 
167  // Reset to initial state.
168  void reset();
169  // Check if the bundle may be validly shuffled.
170  bool check();
171  // Reorder the insn handles in the bundle.
172  bool shuffle();
173 
174  unsigned size() const { return (Packet.size()); }
175 
176  bool isMemReorderDisabled() const {
177  return (BundleFlags & HexagonMCInstrInfo::memReorderDisabledMask) != 0;
178  }
179 
180  iterator begin() { return (Packet.begin()); }
181  iterator end() { return (Packet.end()); }
182 
183  // Add insn handle to the bundle .
184  void append(MCInst const &ID, MCInst const *Extender, unsigned S);
185 
186  // Return the error code for the last check or shuffling of the bundle.
187  void reportError(Twine const &Msg);
188 };
189 
190 } // end namespace llvm
191 
192 #endif // LLVM_LIB_TARGET_HEXAGON_MCTARGETDESC_HEXAGONSHUFFLER_H
static bool lessCore(const HexagonInstr &A, const HexagonInstr &B)
void setUnits(unsigned s)
This class represents lattice values for constants.
Definition: AllocatorList.h:23
HexagonResource(unsigned s)
unsigned getLanes() const
unsigned setWeight(unsigned s)
unsigned size() const
static bool lessWeight(const HexagonResource &A, const HexagonResource &B)
void append(SmallVectorImpl< char > &path, const Twine &a, const Twine &b="", const Twine &c="", const Twine &d="")
Append to path.
Definition: Path.cpp:479
HexagonInstr(HexagonCVIResource::TypeUnitsAndLanes *T, MCInstrInfo const &MCII, MCInst const *id, MCInst const *Extender, unsigned s)
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:80
Context object for machine code objects.
Definition: MCContext.h:62
HexagonPacket::iterator iterator
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:158
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
unsigned getWeight() const
MCSubtargetInfo const & STI
Interface to description of machine instruction set.
Definition: MCInstrInfo.h:23
int64_t const memReorderDisabledMask
bool isMemReorderDisabled() const
size_t size() const
Definition: SmallVector.h:52
unsigned countPopulation(T Value)
Count the number of set bits in a value.
Definition: MathExtras.h:519
MCInstrInfo const & MCII
MCInst const * getExtender() const
typename SuperClass::iterator iterator
Definition: SmallVector.h:319
static int reportError(const char *ProgName, Twine Msg)
Definition: Main.cpp:52
std::pair< unsigned, unsigned > UnitsAndLanes
static bool lessUnits(const HexagonResource &A, const HexagonResource &B)
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
Generic base class for all target subtargets.
unsigned getUnits() const
MCInst const & getDesc() const
#define HEXAGON_PACKET_SIZE
std::vector< std::pair< SMLoc, std::string > > AppliedRestrictions
static bool lessCVI(const HexagonInstr &A, const HexagonInstr &B)
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
Represents a location in source code.
Definition: SMLoc.h:23
bool operator<(const HexagonInstr &B) const