1 //===-- X86InstrFMA3Info.h - X86 FMA3 Instruction Information -------------===//
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 file contains the implementation of the classes providing information
11 // about existing X86 FMA3 opcodes, classifying and grouping them.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_LIB_TARGET_X86_UTILS_X86INSTRFMA3INFO_H
16 #define LLVM_LIB_TARGET_X86_UTILS_X86INSTRFMA3INFO_H
19 #include "llvm/ADT/DenseMap.h"
24 /// This class is used to group {132, 213, 231} forms of FMA opcodes together.
25 /// Each of the groups has either 3 register opcodes, 3 memory opcodes,
26 /// or 6 register and memory opcodes. Also, each group has an attrubutes field
28 class X86InstrFMA3Group {
30 /// Reference to an array holding 3 forms of register FMA opcodes.
31 /// It may be set to nullptr if the group of FMA opcodes does not have
32 /// any register form opcodes.
33 const uint16_t *RegOpcodes;
35 /// Reference to an array holding 3 forms of memory FMA opcodes.
36 /// It may be set to nullptr if the group of FMA opcodes does not have
37 /// any register form opcodes.
38 const uint16_t *MemOpcodes;
40 /// This bitfield specifies the attributes associated with the created
41 /// FMA groups of opcodes.
44 static const unsigned Form132 = 0;
45 static const unsigned Form213 = 1;
46 static const unsigned Form231 = 2;
49 /// This bit must be set in the 'Attributes' field of FMA group if such
50 /// group of FMA opcodes consists of FMA intrinsic opcodes.
51 static const unsigned X86FMA3Intrinsic = 0x1;
53 /// This bit must be set in the 'Attributes' field of FMA group if such
54 /// group of FMA opcodes consists of AVX512 opcodes accepting a k-mask and
55 /// passing the elements from the 1st operand to the result of the operation
56 /// when the correpondings bits in the k-mask are unset.
57 static const unsigned X86FMA3KMergeMasked = 0x2;
59 /// This bit must be set in the 'Attributes' field of FMA group if such
60 /// group of FMA opcodes consists of AVX512 opcodes accepting a k-zeromask.
61 static const unsigned X86FMA3KZeroMasked = 0x4;
63 /// Constructor. Creates a new group of FMA opcodes with three register form
64 /// FMA opcodes \p RegOpcodes and three memory form FMA opcodes \p MemOpcodes.
65 /// The parameters \p RegOpcodes and \p MemOpcodes may be set to nullptr,
66 /// which means that the created group of FMA opcodes does not have the
67 /// corresponding (register or memory) opcodes.
68 /// The parameter \p Attr specifies the attributes describing the created
70 X86InstrFMA3Group(const uint16_t *RegOpcodes, const uint16_t *MemOpcodes,
72 : RegOpcodes(RegOpcodes), MemOpcodes(MemOpcodes), Attributes(Attr) {
73 assert((RegOpcodes || MemOpcodes) &&
74 "Cannot create a group not having any opcodes.");
77 /// Returns a memory form opcode that is the equivalent of the given register
78 /// form opcode \p RegOpcode. 0 is returned if the group does not have
79 /// either register of memory opcodes.
80 unsigned getMemOpcode(unsigned RegOpcode) const {
81 if (!RegOpcodes || !MemOpcodes)
83 for (unsigned Form = 0; Form < 3; Form++)
84 if (RegOpcodes[Form] == RegOpcode)
85 return MemOpcodes[Form];
89 /// Returns the 132 form of FMA register opcode.
90 unsigned getReg132Opcode() const {
91 assert(RegOpcodes && "The group does not have register opcodes.");
92 return RegOpcodes[Form132];
95 /// Returns the 213 form of FMA register opcode.
96 unsigned getReg213Opcode() const {
97 assert(RegOpcodes && "The group does not have register opcodes.");
98 return RegOpcodes[Form213];
101 /// Returns the 231 form of FMA register opcode.
102 unsigned getReg231Opcode() const {
103 assert(RegOpcodes && "The group does not have register opcodes.");
104 return RegOpcodes[Form231];
107 /// Returns the 132 form of FMA memory opcode.
108 unsigned getMem132Opcode() const {
109 assert(MemOpcodes && "The group does not have memory opcodes.");
110 return MemOpcodes[Form132];
113 /// Returns the 213 form of FMA memory opcode.
114 unsigned getMem213Opcode() const {
115 assert(MemOpcodes && "The group does not have memory opcodes.");
116 return MemOpcodes[Form213];
119 /// Returns the 231 form of FMA memory opcode.
120 unsigned getMem231Opcode() const {
121 assert(MemOpcodes && "The group does not have memory opcodes.");
122 return MemOpcodes[Form231];
125 /// Returns true iff the group of FMA opcodes holds intrinsic opcodes.
126 bool isIntrinsic() const { return (Attributes & X86FMA3Intrinsic) != 0; }
128 /// Returns true iff the group of FMA opcodes holds k-merge-masked opcodes.
129 bool isKMergeMasked() const {
130 return (Attributes & X86FMA3KMergeMasked) != 0;
133 /// Returns true iff the group of FMA opcodes holds k-zero-masked opcodes.
134 bool isKZeroMasked() const { return (Attributes & X86FMA3KZeroMasked) != 0; }
136 /// Returns true iff the group of FMA opcodes holds any of k-masked opcodes.
137 bool isKMasked() const {
138 return (Attributes & (X86FMA3KMergeMasked | X86FMA3KZeroMasked)) != 0;
141 /// Returns true iff the given \p Opcode is a register opcode from the
142 /// groups of FMA opcodes.
143 bool isRegOpcodeFromGroup(unsigned Opcode) const {
146 for (unsigned Form = 0; Form < 3; Form++)
147 if (Opcode == RegOpcodes[Form])
152 /// Returns true iff the given \p Opcode is a memory opcode from the
153 /// groups of FMA opcodes.
154 bool isMemOpcodeFromGroup(unsigned Opcode) const {
157 for (unsigned Form = 0; Form < 3; Form++)
158 if (Opcode == MemOpcodes[Form])
164 /// This class provides information about all existing FMA3 opcodes
166 class X86InstrFMA3Info {
168 /// A map that is used to find the group of FMA opcodes using any FMA opcode
170 DenseMap<unsigned, const X86InstrFMA3Group *> OpcodeToGroup;
172 /// Creates groups of FMA opcodes and initializes Opcode-to-Group map.
173 /// This method can be called many times, but the actual initialization is
174 /// called only once.
175 static void initGroupsOnce();
177 /// Creates groups of FMA opcodes and initializes Opcode-to-Group map.
178 /// This method must be called ONLY from initGroupsOnce(). Otherwise, such
179 /// call is not thread safe.
180 void initGroupsOnceImpl();
182 /// Creates one group of FMA opcodes having the register opcodes
183 /// \p RegOpcodes and memory opcodes \p MemOpcodes. The parameter \p Attr
184 /// specifies the attributes describing the created group.
185 void initRMGroup(const uint16_t *RegOpcodes,
186 const uint16_t *MemOpcodes, unsigned Attr = 0);
188 /// Creates one group of FMA opcodes having only the register opcodes
189 /// \p RegOpcodes. The parameter \p Attr specifies the attributes describing
190 /// the created group.
191 void initRGroup(const uint16_t *RegOpcodes, unsigned Attr = 0);
193 /// Creates one group of FMA opcodes having only the memory opcodes
194 /// \p MemOpcodes. The parameter \p Attr specifies the attributes describing
195 /// the created group.
196 void initMGroup(const uint16_t *MemOpcodes, unsigned Attr = 0);
199 /// Returns the reference to an object of this class. It is assumed that
200 /// only one object may exist.
201 static X86InstrFMA3Info *getX86InstrFMA3Info();
203 /// Constructor. Just creates an object of the class.
204 X86InstrFMA3Info() {}
206 /// Destructor. Deallocates the memory used for FMA3 Groups.
207 ~X86InstrFMA3Info() {
208 std::set<const X86InstrFMA3Group *> DeletedGroups;
209 auto E = OpcodeToGroup.end();
210 for (auto I = OpcodeToGroup.begin(); I != E; I++) {
211 const X86InstrFMA3Group *G = I->second;
212 if (DeletedGroups.find(G) == DeletedGroups.end()) {
213 DeletedGroups.insert(G);
219 /// Returns a reference to a group of FMA3 opcodes to where the given
220 /// \p Opcode is included. If the given \p Opcode is not recognized as FMA3
221 /// and not included into any FMA3 group, then nullptr is returned.
222 static const X86InstrFMA3Group *getFMA3Group(unsigned Opcode) {
223 // Ensure that the groups of opcodes are initialized.
226 // Find the group including the given opcode.
227 const X86InstrFMA3Info *FMA3Info = getX86InstrFMA3Info();
228 auto I = FMA3Info->OpcodeToGroup.find(Opcode);
229 if (I == FMA3Info->OpcodeToGroup.end())
235 /// Returns true iff the given \p Opcode is recognized as FMA3 by this class.
236 static bool isFMA3(unsigned Opcode) {
237 return getFMA3Group(Opcode) != nullptr;
240 /// Iterator that is used to walk on FMA register opcodes having memory
241 /// form equivalents.
244 /// Iterator associated with the OpcodeToGroup map. It must always be
245 /// initialized with an entry from OpcodeToGroup for which I->first
246 /// points to a register FMA opcode and I->second points to a group of
247 /// FMA opcodes having memory form equivalent of I->first.
248 DenseMap<unsigned, const X86InstrFMA3Group *>::const_iterator I;
251 /// Constructor. Creates rm_iterator. The parameter \p I must be an
252 /// iterator to OpcodeToGroup map entry having I->first pointing to
253 /// register form FMA opcode and I->second pointing to a group of FMA
254 /// opcodes holding memory form equivalent for I->fist.
255 rm_iterator(DenseMap<unsigned, const X86InstrFMA3Group *>::const_iterator I)
258 /// Returns the register form FMA opcode.
259 unsigned getRegOpcode() const { return I->first; };
261 /// Returns the memory form equivalent opcode for FMA register opcode
262 /// referenced by I->first.
263 unsigned getMemOpcode() const {
264 unsigned Opcode = I->first;
265 const X86InstrFMA3Group *Group = I->second;
266 return Group->getMemOpcode(Opcode);
269 /// Returns a reference to a group of FMA opcodes.
270 const X86InstrFMA3Group *getGroup() const { return I->second; }
272 bool operator==(const rm_iterator &OtherIt) const { return I == OtherIt.I; }
273 bool operator!=(const rm_iterator &OtherIt) const { return I != OtherIt.I; }
275 /// Increment. Advances the 'I' iterator to the next OpcodeToGroup entry
276 /// having I->first pointing to register form FMA and I->second pointing
277 /// to a group of FMA opcodes holding memory form equivalen for I->first.
278 rm_iterator &operator++() {
279 auto E = getX86InstrFMA3Info()->OpcodeToGroup.end();
280 for (++I; I != E; ++I) {
281 unsigned RegOpcode = I->first;
282 const X86InstrFMA3Group *Group = I->second;
283 if (Group->getMemOpcode(RegOpcode) != 0)
290 /// Returns rm_iterator pointing to the first entry of OpcodeToGroup map
291 /// with a register FMA opcode having memory form opcode equivalent.
292 static rm_iterator rm_begin() {
294 const X86InstrFMA3Info *FMA3Info = getX86InstrFMA3Info();
295 auto I = FMA3Info->OpcodeToGroup.begin();
296 auto E = FMA3Info->OpcodeToGroup.end();
298 unsigned Opcode = I->first;
299 const X86InstrFMA3Group *G = I->second;
300 if (G->getMemOpcode(Opcode) != 0)
304 return rm_iterator(I);
307 /// Returns the last rm_iterator.
308 static rm_iterator rm_end() {
310 return rm_iterator(getX86InstrFMA3Info()->OpcodeToGroup.end());