]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/Target/AMDGPU/AsmParser/AMDGPUAsmParser.cpp
Merge ^/head r319251 through r319479.
[FreeBSD/FreeBSD.git] / contrib / llvm / lib / Target / AMDGPU / AsmParser / AMDGPUAsmParser.cpp
1 //===-- AMDGPUAsmParser.cpp - Parse SI asm to MCInst instructions ---------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "AMDKernelCodeT.h"
11 #include "MCTargetDesc/AMDGPUMCTargetDesc.h"
12 #include "MCTargetDesc/AMDGPUTargetStreamer.h"
13 #include "SIDefines.h"
14 #include "Utils/AMDGPUBaseInfo.h"
15 #include "Utils/AMDKernelCodeTUtils.h"
16 #include "Utils/AMDGPUAsmUtils.h"
17 #include "llvm/ADT/APFloat.h"
18 #include "llvm/ADT/APInt.h"
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/ADT/SmallBitVector.h"
21 #include "llvm/ADT/SmallString.h"
22 #include "llvm/ADT/STLExtras.h"
23 #include "llvm/ADT/StringRef.h"
24 #include "llvm/ADT/StringSwitch.h"
25 #include "llvm/ADT/Twine.h"
26 #include "llvm/CodeGen/MachineValueType.h"
27 #include "llvm/MC/MCAsmInfo.h"
28 #include "llvm/MC/MCContext.h"
29 #include "llvm/MC/MCExpr.h"
30 #include "llvm/MC/MCInst.h"
31 #include "llvm/MC/MCInstrDesc.h"
32 #include "llvm/MC/MCInstrInfo.h"
33 #include "llvm/MC/MCParser/MCAsmLexer.h"
34 #include "llvm/MC/MCParser/MCAsmParser.h"
35 #include "llvm/MC/MCParser/MCAsmParserExtension.h"
36 #include "llvm/MC/MCParser/MCParsedAsmOperand.h"
37 #include "llvm/MC/MCParser/MCTargetAsmParser.h"
38 #include "llvm/MC/MCRegisterInfo.h"
39 #include "llvm/MC/MCStreamer.h"
40 #include "llvm/MC/MCSubtargetInfo.h"
41 #include "llvm/MC/MCSymbol.h"
42 #include "llvm/Support/Casting.h"
43 #include "llvm/Support/ELF.h"
44 #include "llvm/Support/ErrorHandling.h"
45 #include "llvm/Support/MathExtras.h"
46 #include "llvm/Support/raw_ostream.h"
47 #include "llvm/Support/SMLoc.h"
48 #include "llvm/Support/TargetRegistry.h"
49 #include <algorithm>
50 #include <cassert>
51 #include <cstdint>
52 #include <cstring>
53 #include <iterator>
54 #include <map>
55 #include <memory>
56 #include <string>
57
58 using namespace llvm;
59 using namespace llvm::AMDGPU;
60
61 namespace {
62
63 class AMDGPUAsmParser;
64
65 enum RegisterKind { IS_UNKNOWN, IS_VGPR, IS_SGPR, IS_TTMP, IS_SPECIAL };
66
67 //===----------------------------------------------------------------------===//
68 // Operand
69 //===----------------------------------------------------------------------===//
70
71 class AMDGPUOperand : public MCParsedAsmOperand {
72   enum KindTy {
73     Token,
74     Immediate,
75     Register,
76     Expression
77   } Kind;
78
79   SMLoc StartLoc, EndLoc;
80   const AMDGPUAsmParser *AsmParser;
81
82 public:
83   AMDGPUOperand(KindTy Kind_, const AMDGPUAsmParser *AsmParser_)
84     : MCParsedAsmOperand(), Kind(Kind_), AsmParser(AsmParser_) {}
85
86   typedef std::unique_ptr<AMDGPUOperand> Ptr;
87
88   struct Modifiers {
89     bool Abs = false;
90     bool Neg = false;
91     bool Sext = false;
92
93     bool hasFPModifiers() const { return Abs || Neg; }
94     bool hasIntModifiers() const { return Sext; }
95     bool hasModifiers() const { return hasFPModifiers() || hasIntModifiers(); }
96
97     int64_t getFPModifiersOperand() const {
98       int64_t Operand = 0;
99       Operand |= Abs ? SISrcMods::ABS : 0;
100       Operand |= Neg ? SISrcMods::NEG : 0;
101       return Operand;
102     }
103
104     int64_t getIntModifiersOperand() const {
105       int64_t Operand = 0;
106       Operand |= Sext ? SISrcMods::SEXT : 0;
107       return Operand;
108     }
109
110     int64_t getModifiersOperand() const {
111       assert(!(hasFPModifiers() && hasIntModifiers())
112            && "fp and int modifiers should not be used simultaneously");
113       if (hasFPModifiers()) {
114         return getFPModifiersOperand();
115       } else if (hasIntModifiers()) {
116         return getIntModifiersOperand();
117       } else {
118         return 0;
119       }
120     }
121
122     friend raw_ostream &operator <<(raw_ostream &OS, AMDGPUOperand::Modifiers Mods);
123   };
124
125   enum ImmTy {
126     ImmTyNone,
127     ImmTyGDS,
128     ImmTyOffen,
129     ImmTyIdxen,
130     ImmTyAddr64,
131     ImmTyOffset,
132     ImmTyOffset0,
133     ImmTyOffset1,
134     ImmTyGLC,
135     ImmTySLC,
136     ImmTyTFE,
137     ImmTyClampSI,
138     ImmTyOModSI,
139     ImmTyDppCtrl,
140     ImmTyDppRowMask,
141     ImmTyDppBankMask,
142     ImmTyDppBoundCtrl,
143     ImmTySdwaDstSel,
144     ImmTySdwaSrc0Sel,
145     ImmTySdwaSrc1Sel,
146     ImmTySdwaDstUnused,
147     ImmTyDMask,
148     ImmTyUNorm,
149     ImmTyDA,
150     ImmTyR128,
151     ImmTyLWE,
152     ImmTyExpTgt,
153     ImmTyExpCompr,
154     ImmTyExpVM,
155     ImmTyHwreg,
156     ImmTyOff,
157     ImmTySendMsg,
158     ImmTyInterpSlot,
159     ImmTyInterpAttr,
160     ImmTyAttrChan,
161     ImmTyOpSel,
162     ImmTyOpSelHi,
163     ImmTyNegLo,
164     ImmTyNegHi,
165     ImmTySwizzle
166   };
167
168   struct TokOp {
169     const char *Data;
170     unsigned Length;
171   };
172
173   struct ImmOp {
174     int64_t Val;
175     ImmTy Type;
176     bool IsFPImm;
177     Modifiers Mods;
178   };
179
180   struct RegOp {
181     unsigned RegNo;
182     bool IsForcedVOP3;
183     Modifiers Mods;
184   };
185
186   union {
187     TokOp Tok;
188     ImmOp Imm;
189     RegOp Reg;
190     const MCExpr *Expr;
191   };
192
193   bool isToken() const override {
194     if (Kind == Token)
195       return true;
196
197     if (Kind != Expression || !Expr)
198       return false;
199
200     // When parsing operands, we can't always tell if something was meant to be
201     // a token, like 'gds', or an expression that references a global variable.
202     // In this case, we assume the string is an expression, and if we need to
203     // interpret is a token, then we treat the symbol name as the token.
204     return isa<MCSymbolRefExpr>(Expr);
205   }
206
207   bool isImm() const override {
208     return Kind == Immediate;
209   }
210
211   bool isInlinableImm(MVT type) const;
212   bool isLiteralImm(MVT type) const;
213
214   bool isRegKind() const {
215     return Kind == Register;
216   }
217
218   bool isReg() const override {
219     return isRegKind() && !hasModifiers();
220   }
221
222   bool isRegOrImmWithInputMods(MVT type) const {
223     return isRegKind() || isInlinableImm(type);
224   }
225
226   bool isRegOrImmWithInt16InputMods() const {
227     return isRegOrImmWithInputMods(MVT::i16);
228   }
229
230   bool isRegOrImmWithInt32InputMods() const {
231     return isRegOrImmWithInputMods(MVT::i32);
232   }
233
234   bool isRegOrImmWithInt64InputMods() const {
235     return isRegOrImmWithInputMods(MVT::i64);
236   }
237
238   bool isRegOrImmWithFP16InputMods() const {
239     return isRegOrImmWithInputMods(MVT::f16);
240   }
241
242   bool isRegOrImmWithFP32InputMods() const {
243     return isRegOrImmWithInputMods(MVT::f32);
244   }
245
246   bool isRegOrImmWithFP64InputMods() const {
247     return isRegOrImmWithInputMods(MVT::f64);
248   }
249
250   bool isVReg() const {
251     return isRegClass(AMDGPU::VGPR_32RegClassID) ||
252            isRegClass(AMDGPU::VReg_64RegClassID) ||
253            isRegClass(AMDGPU::VReg_96RegClassID) ||
254            isRegClass(AMDGPU::VReg_128RegClassID) ||
255            isRegClass(AMDGPU::VReg_256RegClassID) ||
256            isRegClass(AMDGPU::VReg_512RegClassID);
257   }
258
259   bool isVReg32OrOff() const {
260     return isOff() || isRegClass(AMDGPU::VGPR_32RegClassID);
261   }
262
263   bool isImmTy(ImmTy ImmT) const {
264     return isImm() && Imm.Type == ImmT;
265   }
266
267   bool isImmModifier() const {
268     return isImm() && Imm.Type != ImmTyNone;
269   }
270
271   bool isClampSI() const { return isImmTy(ImmTyClampSI); }
272   bool isOModSI() const { return isImmTy(ImmTyOModSI); }
273   bool isDMask() const { return isImmTy(ImmTyDMask); }
274   bool isUNorm() const { return isImmTy(ImmTyUNorm); }
275   bool isDA() const { return isImmTy(ImmTyDA); }
276   bool isR128() const { return isImmTy(ImmTyUNorm); }
277   bool isLWE() const { return isImmTy(ImmTyLWE); }
278   bool isOff() const { return isImmTy(ImmTyOff); }
279   bool isExpTgt() const { return isImmTy(ImmTyExpTgt); }
280   bool isExpVM() const { return isImmTy(ImmTyExpVM); }
281   bool isExpCompr() const { return isImmTy(ImmTyExpCompr); }
282   bool isOffen() const { return isImmTy(ImmTyOffen); }
283   bool isIdxen() const { return isImmTy(ImmTyIdxen); }
284   bool isAddr64() const { return isImmTy(ImmTyAddr64); }
285   bool isOffset() const { return isImmTy(ImmTyOffset) && isUInt<16>(getImm()); }
286   bool isOffset0() const { return isImmTy(ImmTyOffset0) && isUInt<16>(getImm()); }
287   bool isOffset1() const { return isImmTy(ImmTyOffset1) && isUInt<8>(getImm()); }
288   bool isGDS() const { return isImmTy(ImmTyGDS); }
289   bool isGLC() const { return isImmTy(ImmTyGLC); }
290   bool isSLC() const { return isImmTy(ImmTySLC); }
291   bool isTFE() const { return isImmTy(ImmTyTFE); }
292   bool isBankMask() const { return isImmTy(ImmTyDppBankMask); }
293   bool isRowMask() const { return isImmTy(ImmTyDppRowMask); }
294   bool isBoundCtrl() const { return isImmTy(ImmTyDppBoundCtrl); }
295   bool isSDWADstSel() const { return isImmTy(ImmTySdwaDstSel); }
296   bool isSDWASrc0Sel() const { return isImmTy(ImmTySdwaSrc0Sel); }
297   bool isSDWASrc1Sel() const { return isImmTy(ImmTySdwaSrc1Sel); }
298   bool isSDWADstUnused() const { return isImmTy(ImmTySdwaDstUnused); }
299   bool isInterpSlot() const { return isImmTy(ImmTyInterpSlot); }
300   bool isInterpAttr() const { return isImmTy(ImmTyInterpAttr); }
301   bool isAttrChan() const { return isImmTy(ImmTyAttrChan); }
302   bool isOpSel() const { return isImmTy(ImmTyOpSel); }
303   bool isOpSelHi() const { return isImmTy(ImmTyOpSelHi); }
304   bool isNegLo() const { return isImmTy(ImmTyNegLo); }
305   bool isNegHi() const { return isImmTy(ImmTyNegHi); }
306
307   bool isMod() const {
308     return isClampSI() || isOModSI();
309   }
310
311   bool isRegOrImm() const {
312     return isReg() || isImm();
313   }
314
315   bool isRegClass(unsigned RCID) const;
316
317   bool isRegOrInlineNoMods(unsigned RCID, MVT type) const {
318     return (isRegClass(RCID) || isInlinableImm(type)) && !hasModifiers();
319   }
320
321   bool isSCSrcB16() const {
322     return isRegOrInlineNoMods(AMDGPU::SReg_32RegClassID, MVT::i16);
323   }
324
325   bool isSCSrcV2B16() const {
326     return isSCSrcB16();
327   }
328
329   bool isSCSrcB32() const {
330     return isRegOrInlineNoMods(AMDGPU::SReg_32RegClassID, MVT::i32);
331   }
332
333   bool isSCSrcB64() const {
334     return isRegOrInlineNoMods(AMDGPU::SReg_64RegClassID, MVT::i64);
335   }
336
337   bool isSCSrcF16() const {
338     return isRegOrInlineNoMods(AMDGPU::SReg_32RegClassID, MVT::f16);
339   }
340
341   bool isSCSrcV2F16() const {
342     return isSCSrcF16();
343   }
344
345   bool isSCSrcF32() const {
346     return isRegOrInlineNoMods(AMDGPU::SReg_32RegClassID, MVT::f32);
347   }
348
349   bool isSCSrcF64() const {
350     return isRegOrInlineNoMods(AMDGPU::SReg_64RegClassID, MVT::f64);
351   }
352
353   bool isSSrcB32() const {
354     return isSCSrcB32() || isLiteralImm(MVT::i32) || isExpr();
355   }
356
357   bool isSSrcB16() const {
358     return isSCSrcB16() || isLiteralImm(MVT::i16);
359   }
360
361   bool isSSrcV2B16() const {
362     llvm_unreachable("cannot happen");
363     return isSSrcB16();
364   }
365
366   bool isSSrcB64() const {
367     // TODO: Find out how SALU supports extension of 32-bit literals to 64 bits.
368     // See isVSrc64().
369     return isSCSrcB64() || isLiteralImm(MVT::i64);
370   }
371
372   bool isSSrcF32() const {
373     return isSCSrcB32() || isLiteralImm(MVT::f32) || isExpr();
374   }
375
376   bool isSSrcF64() const {
377     return isSCSrcB64() || isLiteralImm(MVT::f64);
378   }
379
380   bool isSSrcF16() const {
381     return isSCSrcB16() || isLiteralImm(MVT::f16);
382   }
383
384   bool isSSrcV2F16() const {
385     llvm_unreachable("cannot happen");
386     return isSSrcF16();
387   }
388
389   bool isVCSrcB32() const {
390     return isRegOrInlineNoMods(AMDGPU::VS_32RegClassID, MVT::i32);
391   }
392
393   bool isVCSrcB64() const {
394     return isRegOrInlineNoMods(AMDGPU::VS_64RegClassID, MVT::i64);
395   }
396
397   bool isVCSrcB16() const {
398     return isRegOrInlineNoMods(AMDGPU::VS_32RegClassID, MVT::i16);
399   }
400
401   bool isVCSrcV2B16() const {
402     return isVCSrcB16();
403   }
404
405   bool isVCSrcF32() const {
406     return isRegOrInlineNoMods(AMDGPU::VS_32RegClassID, MVT::f32);
407   }
408
409   bool isVCSrcF64() const {
410     return isRegOrInlineNoMods(AMDGPU::VS_64RegClassID, MVT::f64);
411   }
412
413   bool isVCSrcF16() const {
414     return isRegOrInlineNoMods(AMDGPU::VS_32RegClassID, MVT::f16);
415   }
416
417   bool isVCSrcV2F16() const {
418     return isVCSrcF16();
419   }
420
421   bool isVSrcB32() const {
422     return isVCSrcF32() || isLiteralImm(MVT::i32);
423   }
424
425   bool isVSrcB64() const {
426     return isVCSrcF64() || isLiteralImm(MVT::i64);
427   }
428
429   bool isVSrcB16() const {
430     return isVCSrcF16() || isLiteralImm(MVT::i16);
431   }
432
433   bool isVSrcV2B16() const {
434     llvm_unreachable("cannot happen");
435     return isVSrcB16();
436   }
437
438   bool isVSrcF32() const {
439     return isVCSrcF32() || isLiteralImm(MVT::f32);
440   }
441
442   bool isVSrcF64() const {
443     return isVCSrcF64() || isLiteralImm(MVT::f64);
444   }
445
446   bool isVSrcF16() const {
447     return isVCSrcF16() || isLiteralImm(MVT::f16);
448   }
449
450   bool isVSrcV2F16() const {
451     llvm_unreachable("cannot happen");
452     return isVSrcF16();
453   }
454
455   bool isKImmFP32() const {
456     return isLiteralImm(MVT::f32);
457   }
458
459   bool isKImmFP16() const {
460     return isLiteralImm(MVT::f16);
461   }
462
463   bool isMem() const override {
464     return false;
465   }
466
467   bool isExpr() const {
468     return Kind == Expression;
469   }
470
471   bool isSoppBrTarget() const {
472     return isExpr() || isImm();
473   }
474
475   bool isSWaitCnt() const;
476   bool isHwreg() const;
477   bool isSendMsg() const;
478   bool isSwizzle() const;
479   bool isSMRDOffset8() const;
480   bool isSMRDOffset20() const;
481   bool isSMRDLiteralOffset() const;
482   bool isDPPCtrl() const;
483   bool isGPRIdxMode() const;
484   bool isS16Imm() const;
485   bool isU16Imm() const;
486
487   StringRef getExpressionAsToken() const {
488     assert(isExpr());
489     const MCSymbolRefExpr *S = cast<MCSymbolRefExpr>(Expr);
490     return S->getSymbol().getName();
491   }
492
493   StringRef getToken() const {
494     assert(isToken());
495
496     if (Kind == Expression)
497       return getExpressionAsToken();
498
499     return StringRef(Tok.Data, Tok.Length);
500   }
501
502   int64_t getImm() const {
503     assert(isImm());
504     return Imm.Val;
505   }
506
507   ImmTy getImmTy() const {
508     assert(isImm());
509     return Imm.Type;
510   }
511
512   unsigned getReg() const override {
513     return Reg.RegNo;
514   }
515
516   SMLoc getStartLoc() const override {
517     return StartLoc;
518   }
519
520   SMLoc getEndLoc() const override {
521     return EndLoc;
522   }
523
524   Modifiers getModifiers() const {
525     assert(isRegKind() || isImmTy(ImmTyNone));
526     return isRegKind() ? Reg.Mods : Imm.Mods;
527   }
528
529   void setModifiers(Modifiers Mods) {
530     assert(isRegKind() || isImmTy(ImmTyNone));
531     if (isRegKind())
532       Reg.Mods = Mods;
533     else
534       Imm.Mods = Mods;
535   }
536
537   bool hasModifiers() const {
538     return getModifiers().hasModifiers();
539   }
540
541   bool hasFPModifiers() const {
542     return getModifiers().hasFPModifiers();
543   }
544
545   bool hasIntModifiers() const {
546     return getModifiers().hasIntModifiers();
547   }
548
549   uint64_t applyInputFPModifiers(uint64_t Val, unsigned Size) const;
550
551   void addImmOperands(MCInst &Inst, unsigned N, bool ApplyModifiers = true) const;
552
553   void addLiteralImmOperand(MCInst &Inst, int64_t Val, bool ApplyModifiers) const;
554
555   template <unsigned Bitwidth>
556   void addKImmFPOperands(MCInst &Inst, unsigned N) const;
557
558   void addKImmFP16Operands(MCInst &Inst, unsigned N) const {
559     addKImmFPOperands<16>(Inst, N);
560   }
561
562   void addKImmFP32Operands(MCInst &Inst, unsigned N) const {
563     addKImmFPOperands<32>(Inst, N);
564   }
565
566   void addRegOperands(MCInst &Inst, unsigned N) const;
567
568   void addRegOrImmOperands(MCInst &Inst, unsigned N) const {
569     if (isRegKind())
570       addRegOperands(Inst, N);
571     else if (isExpr())
572       Inst.addOperand(MCOperand::createExpr(Expr));
573     else
574       addImmOperands(Inst, N);
575   }
576
577   void addRegOrImmWithInputModsOperands(MCInst &Inst, unsigned N) const {
578     Modifiers Mods = getModifiers();
579     Inst.addOperand(MCOperand::createImm(Mods.getModifiersOperand()));
580     if (isRegKind()) {
581       addRegOperands(Inst, N);
582     } else {
583       addImmOperands(Inst, N, false);
584     }
585   }
586
587   void addRegOrImmWithFPInputModsOperands(MCInst &Inst, unsigned N) const {
588     assert(!hasIntModifiers());
589     addRegOrImmWithInputModsOperands(Inst, N);
590   }
591
592   void addRegOrImmWithIntInputModsOperands(MCInst &Inst, unsigned N) const {
593     assert(!hasFPModifiers());
594     addRegOrImmWithInputModsOperands(Inst, N);
595   }
596
597   void addRegWithInputModsOperands(MCInst &Inst, unsigned N) const {
598     Modifiers Mods = getModifiers();
599     Inst.addOperand(MCOperand::createImm(Mods.getModifiersOperand()));
600     assert(isRegKind());
601     addRegOperands(Inst, N);
602   }
603
604   void addRegWithFPInputModsOperands(MCInst &Inst, unsigned N) const {
605     assert(!hasIntModifiers());
606     addRegWithInputModsOperands(Inst, N);
607   }
608
609   void addRegWithIntInputModsOperands(MCInst &Inst, unsigned N) const {
610     assert(!hasFPModifiers());
611     addRegWithInputModsOperands(Inst, N);
612   }
613
614   void addSoppBrTargetOperands(MCInst &Inst, unsigned N) const {
615     if (isImm())
616       addImmOperands(Inst, N);
617     else {
618       assert(isExpr());
619       Inst.addOperand(MCOperand::createExpr(Expr));
620     }
621   }
622
623   static void printImmTy(raw_ostream& OS, ImmTy Type) {
624     switch (Type) {
625     case ImmTyNone: OS << "None"; break;
626     case ImmTyGDS: OS << "GDS"; break;
627     case ImmTyOffen: OS << "Offen"; break;
628     case ImmTyIdxen: OS << "Idxen"; break;
629     case ImmTyAddr64: OS << "Addr64"; break;
630     case ImmTyOffset: OS << "Offset"; break;
631     case ImmTyOffset0: OS << "Offset0"; break;
632     case ImmTyOffset1: OS << "Offset1"; break;
633     case ImmTyGLC: OS << "GLC"; break;
634     case ImmTySLC: OS << "SLC"; break;
635     case ImmTyTFE: OS << "TFE"; break;
636     case ImmTyClampSI: OS << "ClampSI"; break;
637     case ImmTyOModSI: OS << "OModSI"; break;
638     case ImmTyDppCtrl: OS << "DppCtrl"; break;
639     case ImmTyDppRowMask: OS << "DppRowMask"; break;
640     case ImmTyDppBankMask: OS << "DppBankMask"; break;
641     case ImmTyDppBoundCtrl: OS << "DppBoundCtrl"; break;
642     case ImmTySdwaDstSel: OS << "SdwaDstSel"; break;
643     case ImmTySdwaSrc0Sel: OS << "SdwaSrc0Sel"; break;
644     case ImmTySdwaSrc1Sel: OS << "SdwaSrc1Sel"; break;
645     case ImmTySdwaDstUnused: OS << "SdwaDstUnused"; break;
646     case ImmTyDMask: OS << "DMask"; break;
647     case ImmTyUNorm: OS << "UNorm"; break;
648     case ImmTyDA: OS << "DA"; break;
649     case ImmTyR128: OS << "R128"; break;
650     case ImmTyLWE: OS << "LWE"; break;
651     case ImmTyOff: OS << "Off"; break;
652     case ImmTyExpTgt: OS << "ExpTgt"; break;
653     case ImmTyExpCompr: OS << "ExpCompr"; break;
654     case ImmTyExpVM: OS << "ExpVM"; break;
655     case ImmTyHwreg: OS << "Hwreg"; break;
656     case ImmTySendMsg: OS << "SendMsg"; break;
657     case ImmTyInterpSlot: OS << "InterpSlot"; break;
658     case ImmTyInterpAttr: OS << "InterpAttr"; break;
659     case ImmTyAttrChan: OS << "AttrChan"; break;
660     case ImmTyOpSel: OS << "OpSel"; break;
661     case ImmTyOpSelHi: OS << "OpSelHi"; break;
662     case ImmTyNegLo: OS << "NegLo"; break;
663     case ImmTyNegHi: OS << "NegHi"; break;
664     case ImmTySwizzle: OS << "Swizzle"; break;
665     }
666   }
667
668   void print(raw_ostream &OS) const override {
669     switch (Kind) {
670     case Register:
671       OS << "<register " << getReg() << " mods: " << Reg.Mods << '>';
672       break;
673     case Immediate:
674       OS << '<' << getImm();
675       if (getImmTy() != ImmTyNone) {
676         OS << " type: "; printImmTy(OS, getImmTy());
677       }
678       OS << " mods: " << Imm.Mods << '>';
679       break;
680     case Token:
681       OS << '\'' << getToken() << '\'';
682       break;
683     case Expression:
684       OS << "<expr " << *Expr << '>';
685       break;
686     }
687   }
688
689   static AMDGPUOperand::Ptr CreateImm(const AMDGPUAsmParser *AsmParser,
690                                       int64_t Val, SMLoc Loc,
691                                       ImmTy Type = ImmTyNone,
692                                       bool IsFPImm = false) {
693     auto Op = llvm::make_unique<AMDGPUOperand>(Immediate, AsmParser);
694     Op->Imm.Val = Val;
695     Op->Imm.IsFPImm = IsFPImm;
696     Op->Imm.Type = Type;
697     Op->Imm.Mods = Modifiers();
698     Op->StartLoc = Loc;
699     Op->EndLoc = Loc;
700     return Op;
701   }
702
703   static AMDGPUOperand::Ptr CreateToken(const AMDGPUAsmParser *AsmParser,
704                                         StringRef Str, SMLoc Loc,
705                                         bool HasExplicitEncodingSize = true) {
706     auto Res = llvm::make_unique<AMDGPUOperand>(Token, AsmParser);
707     Res->Tok.Data = Str.data();
708     Res->Tok.Length = Str.size();
709     Res->StartLoc = Loc;
710     Res->EndLoc = Loc;
711     return Res;
712   }
713
714   static AMDGPUOperand::Ptr CreateReg(const AMDGPUAsmParser *AsmParser,
715                                       unsigned RegNo, SMLoc S,
716                                       SMLoc E,
717                                       bool ForceVOP3) {
718     auto Op = llvm::make_unique<AMDGPUOperand>(Register, AsmParser);
719     Op->Reg.RegNo = RegNo;
720     Op->Reg.Mods = Modifiers();
721     Op->Reg.IsForcedVOP3 = ForceVOP3;
722     Op->StartLoc = S;
723     Op->EndLoc = E;
724     return Op;
725   }
726
727   static AMDGPUOperand::Ptr CreateExpr(const AMDGPUAsmParser *AsmParser,
728                                        const class MCExpr *Expr, SMLoc S) {
729     auto Op = llvm::make_unique<AMDGPUOperand>(Expression, AsmParser);
730     Op->Expr = Expr;
731     Op->StartLoc = S;
732     Op->EndLoc = S;
733     return Op;
734   }
735 };
736
737 raw_ostream &operator <<(raw_ostream &OS, AMDGPUOperand::Modifiers Mods) {
738   OS << "abs:" << Mods.Abs << " neg: " << Mods.Neg << " sext:" << Mods.Sext;
739   return OS;
740 }
741
742 //===----------------------------------------------------------------------===//
743 // AsmParser
744 //===----------------------------------------------------------------------===//
745
746 // Holds info related to the current kernel, e.g. count of SGPRs used.
747 // Kernel scope begins at .amdgpu_hsa_kernel directive, ends at next
748 // .amdgpu_hsa_kernel or at EOF.
749 class KernelScopeInfo {
750   int SgprIndexUnusedMin = -1;
751   int VgprIndexUnusedMin = -1;
752   MCContext *Ctx = nullptr;
753
754   void usesSgprAt(int i) {
755     if (i >= SgprIndexUnusedMin) {
756       SgprIndexUnusedMin = ++i;
757       if (Ctx) {
758         MCSymbol * const Sym = Ctx->getOrCreateSymbol(Twine(".kernel.sgpr_count"));
759         Sym->setVariableValue(MCConstantExpr::create(SgprIndexUnusedMin, *Ctx));
760       }
761     }
762   }
763
764   void usesVgprAt(int i) {
765     if (i >= VgprIndexUnusedMin) {
766       VgprIndexUnusedMin = ++i;
767       if (Ctx) {
768         MCSymbol * const Sym = Ctx->getOrCreateSymbol(Twine(".kernel.vgpr_count"));
769         Sym->setVariableValue(MCConstantExpr::create(VgprIndexUnusedMin, *Ctx));
770       }
771     }
772   }
773
774 public:
775   KernelScopeInfo() = default;
776
777   void initialize(MCContext &Context) {
778     Ctx = &Context;
779     usesSgprAt(SgprIndexUnusedMin = -1);
780     usesVgprAt(VgprIndexUnusedMin = -1);
781   }
782
783   void usesRegister(RegisterKind RegKind, unsigned DwordRegIndex, unsigned RegWidth) {
784     switch (RegKind) {
785       case IS_SGPR: usesSgprAt(DwordRegIndex + RegWidth - 1); break;
786       case IS_VGPR: usesVgprAt(DwordRegIndex + RegWidth - 1); break;
787       default: break;
788     }
789   }
790 };
791
792 class AMDGPUAsmParser : public MCTargetAsmParser {
793   const MCInstrInfo &MII;
794   MCAsmParser &Parser;
795
796   unsigned ForcedEncodingSize = 0;
797   bool ForcedDPP = false;
798   bool ForcedSDWA = false;
799   KernelScopeInfo KernelScope;
800
801   /// @name Auto-generated Match Functions
802   /// {
803
804 #define GET_ASSEMBLER_HEADER
805 #include "AMDGPUGenAsmMatcher.inc"
806
807   /// }
808
809 private:
810   bool ParseAsAbsoluteExpression(uint32_t &Ret);
811   bool ParseDirectiveMajorMinor(uint32_t &Major, uint32_t &Minor);
812   bool ParseDirectiveHSACodeObjectVersion();
813   bool ParseDirectiveHSACodeObjectISA();
814   bool ParseDirectiveCodeObjectMetadata();
815   bool ParseAMDKernelCodeTValue(StringRef ID, amd_kernel_code_t &Header);
816   bool ParseDirectiveAMDKernelCodeT();
817   bool ParseSectionDirectiveHSAText();
818   bool subtargetHasRegister(const MCRegisterInfo &MRI, unsigned RegNo) const;
819   bool ParseDirectiveAMDGPUHsaKernel();
820   bool ParseDirectiveAMDGPUHsaModuleGlobal();
821   bool ParseDirectiveAMDGPUHsaProgramGlobal();
822   bool ParseSectionDirectiveHSADataGlobalAgent();
823   bool ParseSectionDirectiveHSADataGlobalProgram();
824   bool ParseSectionDirectiveHSARodataReadonlyAgent();
825   bool AddNextRegisterToList(unsigned& Reg, unsigned& RegWidth,
826                              RegisterKind RegKind, unsigned Reg1,
827                              unsigned RegNum);
828   bool ParseAMDGPURegister(RegisterKind& RegKind, unsigned& Reg,
829                            unsigned& RegNum, unsigned& RegWidth,
830                            unsigned *DwordRegIndex);
831   void cvtMubufImpl(MCInst &Inst, const OperandVector &Operands,
832                     bool IsAtomic, bool IsAtomicReturn);
833   void cvtDSImpl(MCInst &Inst, const OperandVector &Operands,
834                  bool IsGdsHardcoded);
835
836 public:
837   enum AMDGPUMatchResultTy {
838     Match_PreferE32 = FIRST_TARGET_MATCH_RESULT_TY
839   };
840
841   typedef std::map<AMDGPUOperand::ImmTy, unsigned> OptionalImmIndexMap;
842
843   AMDGPUAsmParser(const MCSubtargetInfo &STI, MCAsmParser &_Parser,
844                const MCInstrInfo &MII,
845                const MCTargetOptions &Options)
846       : MCTargetAsmParser(Options, STI), MII(MII), Parser(_Parser) {
847     MCAsmParserExtension::Initialize(Parser);
848
849     if (getFeatureBits().none()) {
850       // Set default features.
851       copySTI().ToggleFeature("SOUTHERN_ISLANDS");
852     }
853
854     setAvailableFeatures(ComputeAvailableFeatures(getFeatureBits()));
855
856     {
857       // TODO: make those pre-defined variables read-only.
858       // Currently there is none suitable machinery in the core llvm-mc for this.
859       // MCSymbol::isRedefinable is intended for another purpose, and
860       // AsmParser::parseDirectiveSet() cannot be specialized for specific target.
861       AMDGPU::IsaInfo::IsaVersion ISA =
862           AMDGPU::IsaInfo::getIsaVersion(getFeatureBits());
863       MCContext &Ctx = getContext();
864       MCSymbol *Sym =
865           Ctx.getOrCreateSymbol(Twine(".option.machine_version_major"));
866       Sym->setVariableValue(MCConstantExpr::create(ISA.Major, Ctx));
867       Sym = Ctx.getOrCreateSymbol(Twine(".option.machine_version_minor"));
868       Sym->setVariableValue(MCConstantExpr::create(ISA.Minor, Ctx));
869       Sym = Ctx.getOrCreateSymbol(Twine(".option.machine_version_stepping"));
870       Sym->setVariableValue(MCConstantExpr::create(ISA.Stepping, Ctx));
871     }
872     KernelScope.initialize(getContext());
873   }
874
875   bool isSI() const {
876     return AMDGPU::isSI(getSTI());
877   }
878
879   bool isCI() const {
880     return AMDGPU::isCI(getSTI());
881   }
882
883   bool isVI() const {
884     return AMDGPU::isVI(getSTI());
885   }
886
887   bool isGFX9() const {
888     return AMDGPU::isGFX9(getSTI());
889   }
890
891   bool hasInv2PiInlineImm() const {
892     return getFeatureBits()[AMDGPU::FeatureInv2PiInlineImm];
893   }
894
895   bool hasSGPR102_SGPR103() const {
896     return !isVI();
897   }
898
899   AMDGPUTargetStreamer &getTargetStreamer() {
900     MCTargetStreamer &TS = *getParser().getStreamer().getTargetStreamer();
901     return static_cast<AMDGPUTargetStreamer &>(TS);
902   }
903
904   const MCRegisterInfo *getMRI() const {
905     // We need this const_cast because for some reason getContext() is not const
906     // in MCAsmParser.
907     return const_cast<AMDGPUAsmParser*>(this)->getContext().getRegisterInfo();
908   }
909
910   const MCInstrInfo *getMII() const {
911     return &MII;
912   }
913
914   const FeatureBitset &getFeatureBits() const {
915     return getSTI().getFeatureBits();
916   }
917
918   void setForcedEncodingSize(unsigned Size) { ForcedEncodingSize = Size; }
919   void setForcedDPP(bool ForceDPP_) { ForcedDPP = ForceDPP_; }
920   void setForcedSDWA(bool ForceSDWA_) { ForcedSDWA = ForceSDWA_; }
921
922   unsigned getForcedEncodingSize() const { return ForcedEncodingSize; }
923   bool isForcedVOP3() const { return ForcedEncodingSize == 64; }
924   bool isForcedDPP() const { return ForcedDPP; }
925   bool isForcedSDWA() const { return ForcedSDWA; }
926   ArrayRef<unsigned> getMatchedVariants() const;
927
928   std::unique_ptr<AMDGPUOperand> parseRegister();
929   bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) override;
930   unsigned checkTargetMatchPredicate(MCInst &Inst) override;
931   unsigned validateTargetOperandClass(MCParsedAsmOperand &Op,
932                                       unsigned Kind) override;
933   bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
934                                OperandVector &Operands, MCStreamer &Out,
935                                uint64_t &ErrorInfo,
936                                bool MatchingInlineAsm) override;
937   bool ParseDirective(AsmToken DirectiveID) override;
938   OperandMatchResultTy parseOperand(OperandVector &Operands, StringRef Mnemonic);
939   StringRef parseMnemonicSuffix(StringRef Name);
940   bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
941                         SMLoc NameLoc, OperandVector &Operands) override;
942   //bool ProcessInstruction(MCInst &Inst);
943
944   OperandMatchResultTy parseIntWithPrefix(const char *Prefix, int64_t &Int);
945
946   OperandMatchResultTy
947   parseIntWithPrefix(const char *Prefix, OperandVector &Operands,
948                      AMDGPUOperand::ImmTy ImmTy = AMDGPUOperand::ImmTyNone,
949                      bool (*ConvertResult)(int64_t &) = nullptr);
950
951   OperandMatchResultTy parseOperandArrayWithPrefix(
952     const char *Prefix,
953     OperandVector &Operands,
954     AMDGPUOperand::ImmTy ImmTy = AMDGPUOperand::ImmTyNone,
955     bool (*ConvertResult)(int64_t&) = nullptr);
956
957   OperandMatchResultTy
958   parseNamedBit(const char *Name, OperandVector &Operands,
959                 AMDGPUOperand::ImmTy ImmTy = AMDGPUOperand::ImmTyNone);
960   OperandMatchResultTy parseStringWithPrefix(StringRef Prefix,
961                                              StringRef &Value);
962
963   bool parseAbsoluteExpr(int64_t &Val, bool AbsMod = false);
964   OperandMatchResultTy parseImm(OperandVector &Operands, bool AbsMod = false);
965   OperandMatchResultTy parseReg(OperandVector &Operands);
966   OperandMatchResultTy parseRegOrImm(OperandVector &Operands, bool AbsMod = false);
967   OperandMatchResultTy parseRegOrImmWithFPInputMods(OperandVector &Operands, bool AllowImm = true);
968   OperandMatchResultTy parseRegOrImmWithIntInputMods(OperandVector &Operands, bool AllowImm = true);
969   OperandMatchResultTy parseRegWithFPInputMods(OperandVector &Operands);
970   OperandMatchResultTy parseRegWithIntInputMods(OperandVector &Operands);
971   OperandMatchResultTy parseVReg32OrOff(OperandVector &Operands);
972
973   void cvtDSOffset01(MCInst &Inst, const OperandVector &Operands);
974   void cvtDS(MCInst &Inst, const OperandVector &Operands) { cvtDSImpl(Inst, Operands, false); }
975   void cvtDSGds(MCInst &Inst, const OperandVector &Operands) { cvtDSImpl(Inst, Operands, true); }
976   void cvtExp(MCInst &Inst, const OperandVector &Operands);
977
978   bool parseCnt(int64_t &IntVal);
979   OperandMatchResultTy parseSWaitCntOps(OperandVector &Operands);
980   OperandMatchResultTy parseHwreg(OperandVector &Operands);
981
982 private:
983   struct OperandInfoTy {
984     int64_t Id;
985     bool IsSymbolic;
986     OperandInfoTy(int64_t Id_) : Id(Id_), IsSymbolic(false) { }
987   };
988
989   bool parseSendMsgConstruct(OperandInfoTy &Msg, OperandInfoTy &Operation, int64_t &StreamId);
990   bool parseHwregConstruct(OperandInfoTy &HwReg, int64_t &Offset, int64_t &Width);
991
992   void errorExpTgt();
993   OperandMatchResultTy parseExpTgtImpl(StringRef Str, uint8_t &Val);
994
995   bool validateOperandLimitations(const MCInst &Inst);
996   bool usesConstantBus(const MCInst &Inst, unsigned OpIdx);
997   bool isInlineConstant(const MCInst &Inst, unsigned OpIdx) const;
998   unsigned findImplicitSGPRReadInVOP(const MCInst &Inst) const;
999
1000   bool trySkipId(const StringRef Id);
1001   bool trySkipToken(const AsmToken::TokenKind Kind);
1002   bool skipToken(const AsmToken::TokenKind Kind, const StringRef ErrMsg);
1003   bool parseString(StringRef &Val, const StringRef ErrMsg = "expected a string");
1004   bool parseExpr(int64_t &Imm);
1005
1006 public:
1007   OperandMatchResultTy parseOptionalOperand(OperandVector &Operands);
1008
1009   OperandMatchResultTy parseExpTgt(OperandVector &Operands);
1010   OperandMatchResultTy parseSendMsgOp(OperandVector &Operands);
1011   OperandMatchResultTy parseInterpSlot(OperandVector &Operands);
1012   OperandMatchResultTy parseInterpAttr(OperandVector &Operands);
1013   OperandMatchResultTy parseSOppBrTarget(OperandVector &Operands);
1014
1015   bool parseSwizzleOperands(const unsigned OpNum, int64_t* Op,
1016                             const unsigned MinVal,
1017                             const unsigned MaxVal,
1018                             const StringRef ErrMsg);
1019   OperandMatchResultTy parseSwizzleOp(OperandVector &Operands);
1020   bool parseSwizzleOffset(int64_t &Imm);
1021   bool parseSwizzleMacro(int64_t &Imm);
1022   bool parseSwizzleQuadPerm(int64_t &Imm);
1023   bool parseSwizzleBitmaskPerm(int64_t &Imm);
1024   bool parseSwizzleBroadcast(int64_t &Imm);
1025   bool parseSwizzleSwap(int64_t &Imm);
1026   bool parseSwizzleReverse(int64_t &Imm);
1027
1028   void cvtMubuf(MCInst &Inst, const OperandVector &Operands) { cvtMubufImpl(Inst, Operands, false, false); }
1029   void cvtMubufAtomic(MCInst &Inst, const OperandVector &Operands) { cvtMubufImpl(Inst, Operands, true, false); }
1030   void cvtMubufAtomicReturn(MCInst &Inst, const OperandVector &Operands) { cvtMubufImpl(Inst, Operands, true, true); }
1031   AMDGPUOperand::Ptr defaultGLC() const;
1032   AMDGPUOperand::Ptr defaultSLC() const;
1033   AMDGPUOperand::Ptr defaultTFE() const;
1034
1035   AMDGPUOperand::Ptr defaultDMask() const;
1036   AMDGPUOperand::Ptr defaultUNorm() const;
1037   AMDGPUOperand::Ptr defaultDA() const;
1038   AMDGPUOperand::Ptr defaultR128() const;
1039   AMDGPUOperand::Ptr defaultLWE() const;
1040   AMDGPUOperand::Ptr defaultSMRDOffset8() const;
1041   AMDGPUOperand::Ptr defaultSMRDOffset20() const;
1042   AMDGPUOperand::Ptr defaultSMRDLiteralOffset() const;
1043
1044   OperandMatchResultTy parseOModOperand(OperandVector &Operands);
1045
1046   void cvtId(MCInst &Inst, const OperandVector &Operands);
1047   void cvtVOP3_2_mod(MCInst &Inst, const OperandVector &Operands);
1048
1049   void cvtVOP3Impl(MCInst &Inst,
1050                    const OperandVector &Operands,
1051                    OptionalImmIndexMap &OptionalIdx);
1052   void cvtVOP3(MCInst &Inst, const OperandVector &Operands);
1053   void cvtVOP3OMod(MCInst &Inst, const OperandVector &Operands);
1054   void cvtVOP3P(MCInst &Inst, const OperandVector &Operands);
1055
1056   void cvtMIMG(MCInst &Inst, const OperandVector &Operands);
1057   void cvtMIMGAtomic(MCInst &Inst, const OperandVector &Operands);
1058
1059   OperandMatchResultTy parseDPPCtrl(OperandVector &Operands);
1060   AMDGPUOperand::Ptr defaultRowMask() const;
1061   AMDGPUOperand::Ptr defaultBankMask() const;
1062   AMDGPUOperand::Ptr defaultBoundCtrl() const;
1063   void cvtDPP(MCInst &Inst, const OperandVector &Operands);
1064
1065   OperandMatchResultTy parseSDWASel(OperandVector &Operands, StringRef Prefix,
1066                                     AMDGPUOperand::ImmTy Type);
1067   OperandMatchResultTy parseSDWADstUnused(OperandVector &Operands);
1068   void cvtSdwaVOP1(MCInst &Inst, const OperandVector &Operands);
1069   void cvtSdwaVOP2(MCInst &Inst, const OperandVector &Operands);
1070   void cvtSdwaVOP2b(MCInst &Inst, const OperandVector &Operands);
1071   void cvtSdwaVOPC(MCInst &Inst, const OperandVector &Operands);
1072   void cvtSDWA(MCInst &Inst, const OperandVector &Operands,
1073                 uint64_t BasicInstType, bool skipVcc = false);
1074 };
1075
1076 struct OptionalOperand {
1077   const char *Name;
1078   AMDGPUOperand::ImmTy Type;
1079   bool IsBit;
1080   bool (*ConvertResult)(int64_t&);
1081 };
1082
1083 } // end anonymous namespace
1084
1085 // May be called with integer type with equivalent bitwidth.
1086 static const fltSemantics *getFltSemantics(unsigned Size) {
1087   switch (Size) {
1088   case 4:
1089     return &APFloat::IEEEsingle();
1090   case 8:
1091     return &APFloat::IEEEdouble();
1092   case 2:
1093     return &APFloat::IEEEhalf();
1094   default:
1095     llvm_unreachable("unsupported fp type");
1096   }
1097 }
1098
1099 static const fltSemantics *getFltSemantics(MVT VT) {
1100   return getFltSemantics(VT.getSizeInBits() / 8);
1101 }
1102
1103 static const fltSemantics *getOpFltSemantics(uint8_t OperandType) {
1104   switch (OperandType) {
1105   case AMDGPU::OPERAND_REG_IMM_INT32:
1106   case AMDGPU::OPERAND_REG_IMM_FP32:
1107   case AMDGPU::OPERAND_REG_INLINE_C_INT32:
1108   case AMDGPU::OPERAND_REG_INLINE_C_FP32:
1109     return &APFloat::IEEEsingle();
1110   case AMDGPU::OPERAND_REG_IMM_INT64:
1111   case AMDGPU::OPERAND_REG_IMM_FP64:
1112   case AMDGPU::OPERAND_REG_INLINE_C_INT64:
1113   case AMDGPU::OPERAND_REG_INLINE_C_FP64:
1114     return &APFloat::IEEEdouble();
1115   case AMDGPU::OPERAND_REG_IMM_INT16:
1116   case AMDGPU::OPERAND_REG_IMM_FP16:
1117   case AMDGPU::OPERAND_REG_INLINE_C_INT16:
1118   case AMDGPU::OPERAND_REG_INLINE_C_FP16:
1119   case AMDGPU::OPERAND_REG_INLINE_C_V2INT16:
1120   case AMDGPU::OPERAND_REG_INLINE_C_V2FP16:
1121     return &APFloat::IEEEhalf();
1122   default:
1123     llvm_unreachable("unsupported fp type");
1124   }
1125 }
1126
1127 //===----------------------------------------------------------------------===//
1128 // Operand
1129 //===----------------------------------------------------------------------===//
1130
1131 static bool canLosslesslyConvertToFPType(APFloat &FPLiteral, MVT VT) {
1132   bool Lost;
1133
1134   // Convert literal to single precision
1135   APFloat::opStatus Status = FPLiteral.convert(*getFltSemantics(VT),
1136                                                APFloat::rmNearestTiesToEven,
1137                                                &Lost);
1138   // We allow precision lost but not overflow or underflow
1139   if (Status != APFloat::opOK &&
1140       Lost &&
1141       ((Status & APFloat::opOverflow)  != 0 ||
1142        (Status & APFloat::opUnderflow) != 0)) {
1143     return false;
1144   }
1145
1146   return true;
1147 }
1148
1149 bool AMDGPUOperand::isInlinableImm(MVT type) const {
1150   if (!isImmTy(ImmTyNone)) {
1151     // Only plain immediates are inlinable (e.g. "clamp" attribute is not)
1152     return false;
1153   }
1154   // TODO: We should avoid using host float here. It would be better to
1155   // check the float bit values which is what a few other places do.
1156   // We've had bot failures before due to weird NaN support on mips hosts.
1157
1158   APInt Literal(64, Imm.Val);
1159
1160   if (Imm.IsFPImm) { // We got fp literal token
1161     if (type == MVT::f64 || type == MVT::i64) { // Expected 64-bit operand
1162       return AMDGPU::isInlinableLiteral64(Imm.Val,
1163                                           AsmParser->hasInv2PiInlineImm());
1164     }
1165
1166     APFloat FPLiteral(APFloat::IEEEdouble(), APInt(64, Imm.Val));
1167     if (!canLosslesslyConvertToFPType(FPLiteral, type))
1168       return false;
1169
1170     if (type.getScalarSizeInBits() == 16) {
1171       return AMDGPU::isInlinableLiteral16(
1172         static_cast<int16_t>(FPLiteral.bitcastToAPInt().getZExtValue()),
1173         AsmParser->hasInv2PiInlineImm());
1174     }
1175
1176     // Check if single precision literal is inlinable
1177     return AMDGPU::isInlinableLiteral32(
1178       static_cast<int32_t>(FPLiteral.bitcastToAPInt().getZExtValue()),
1179       AsmParser->hasInv2PiInlineImm());
1180   }
1181
1182   // We got int literal token.
1183   if (type == MVT::f64 || type == MVT::i64) { // Expected 64-bit operand
1184     return AMDGPU::isInlinableLiteral64(Imm.Val,
1185                                         AsmParser->hasInv2PiInlineImm());
1186   }
1187
1188   if (type.getScalarSizeInBits() == 16) {
1189     return AMDGPU::isInlinableLiteral16(
1190       static_cast<int16_t>(Literal.getLoBits(16).getSExtValue()),
1191       AsmParser->hasInv2PiInlineImm());
1192   }
1193
1194   return AMDGPU::isInlinableLiteral32(
1195     static_cast<int32_t>(Literal.getLoBits(32).getZExtValue()),
1196     AsmParser->hasInv2PiInlineImm());
1197 }
1198
1199 bool AMDGPUOperand::isLiteralImm(MVT type) const {
1200   // Check that this imediate can be added as literal
1201   if (!isImmTy(ImmTyNone)) {
1202     return false;
1203   }
1204
1205   if (!Imm.IsFPImm) {
1206     // We got int literal token.
1207
1208     if (type == MVT::f64 && hasFPModifiers()) {
1209       // Cannot apply fp modifiers to int literals preserving the same semantics
1210       // for VOP1/2/C and VOP3 because of integer truncation. To avoid ambiguity,
1211       // disable these cases.
1212       return false;
1213     }
1214
1215     unsigned Size = type.getSizeInBits();
1216     if (Size == 64)
1217       Size = 32;
1218
1219     // FIXME: 64-bit operands can zero extend, sign extend, or pad zeroes for FP
1220     // types.
1221     return isUIntN(Size, Imm.Val) || isIntN(Size, Imm.Val);
1222   }
1223
1224   // We got fp literal token
1225   if (type == MVT::f64) { // Expected 64-bit fp operand
1226     // We would set low 64-bits of literal to zeroes but we accept this literals
1227     return true;
1228   }
1229
1230   if (type == MVT::i64) { // Expected 64-bit int operand
1231     // We don't allow fp literals in 64-bit integer instructions. It is
1232     // unclear how we should encode them.
1233     return false;
1234   }
1235
1236   APFloat FPLiteral(APFloat::IEEEdouble(), APInt(64, Imm.Val));
1237   return canLosslesslyConvertToFPType(FPLiteral, type);
1238 }
1239
1240 bool AMDGPUOperand::isRegClass(unsigned RCID) const {
1241   return isRegKind() && AsmParser->getMRI()->getRegClass(RCID).contains(getReg());
1242 }
1243
1244 uint64_t AMDGPUOperand::applyInputFPModifiers(uint64_t Val, unsigned Size) const
1245 {
1246   assert(isImmTy(ImmTyNone) && Imm.Mods.hasFPModifiers());
1247   assert(Size == 2 || Size == 4 || Size == 8);
1248
1249   const uint64_t FpSignMask = (1ULL << (Size * 8 - 1));
1250
1251   if (Imm.Mods.Abs) {
1252     Val &= ~FpSignMask;
1253   }
1254   if (Imm.Mods.Neg) {
1255     Val ^= FpSignMask;
1256   }
1257
1258   return Val;
1259 }
1260
1261 void AMDGPUOperand::addImmOperands(MCInst &Inst, unsigned N, bool ApplyModifiers) const {
1262
1263   if (AMDGPU::isSISrcOperand(AsmParser->getMII()->get(Inst.getOpcode()),
1264                              Inst.getNumOperands())) {
1265     addLiteralImmOperand(Inst, Imm.Val,
1266                          ApplyModifiers &
1267                          isImmTy(ImmTyNone) && Imm.Mods.hasFPModifiers());
1268   } else {
1269     assert(!isImmTy(ImmTyNone) || !hasModifiers());
1270     Inst.addOperand(MCOperand::createImm(Imm.Val));
1271   }
1272 }
1273
1274 void AMDGPUOperand::addLiteralImmOperand(MCInst &Inst, int64_t Val, bool ApplyModifiers) const {
1275   const auto& InstDesc = AsmParser->getMII()->get(Inst.getOpcode());
1276   auto OpNum = Inst.getNumOperands();
1277   // Check that this operand accepts literals
1278   assert(AMDGPU::isSISrcOperand(InstDesc, OpNum));
1279
1280   if (ApplyModifiers) {
1281     assert(AMDGPU::isSISrcFPOperand(InstDesc, OpNum));
1282     const unsigned Size = Imm.IsFPImm ? sizeof(double) : getOperandSize(InstDesc, OpNum);
1283     Val = applyInputFPModifiers(Val, Size);
1284   }
1285
1286   APInt Literal(64, Val);
1287   uint8_t OpTy = InstDesc.OpInfo[OpNum].OperandType;
1288
1289   if (Imm.IsFPImm) { // We got fp literal token
1290     switch (OpTy) {
1291     case AMDGPU::OPERAND_REG_IMM_INT64:
1292     case AMDGPU::OPERAND_REG_IMM_FP64:
1293     case AMDGPU::OPERAND_REG_INLINE_C_INT64:
1294     case AMDGPU::OPERAND_REG_INLINE_C_FP64: {
1295       if (AMDGPU::isInlinableLiteral64(Literal.getZExtValue(),
1296                                        AsmParser->hasInv2PiInlineImm())) {
1297         Inst.addOperand(MCOperand::createImm(Literal.getZExtValue()));
1298         return;
1299       }
1300
1301       // Non-inlineable
1302       if (AMDGPU::isSISrcFPOperand(InstDesc, OpNum)) { // Expected 64-bit fp operand
1303         // For fp operands we check if low 32 bits are zeros
1304         if (Literal.getLoBits(32) != 0) {
1305           const_cast<AMDGPUAsmParser *>(AsmParser)->Warning(Inst.getLoc(),
1306           "Can't encode literal as exact 64-bit floating-point operand. "
1307           "Low 32-bits will be set to zero");
1308         }
1309
1310         Inst.addOperand(MCOperand::createImm(Literal.lshr(32).getZExtValue()));
1311         return;
1312       }
1313
1314       // We don't allow fp literals in 64-bit integer instructions. It is
1315       // unclear how we should encode them. This case should be checked earlier
1316       // in predicate methods (isLiteralImm())
1317       llvm_unreachable("fp literal in 64-bit integer instruction.");
1318     }
1319     case AMDGPU::OPERAND_REG_IMM_INT32:
1320     case AMDGPU::OPERAND_REG_IMM_FP32:
1321     case AMDGPU::OPERAND_REG_INLINE_C_INT32:
1322     case AMDGPU::OPERAND_REG_INLINE_C_FP32:
1323     case AMDGPU::OPERAND_REG_IMM_INT16:
1324     case AMDGPU::OPERAND_REG_IMM_FP16:
1325     case AMDGPU::OPERAND_REG_INLINE_C_INT16:
1326     case AMDGPU::OPERAND_REG_INLINE_C_FP16:
1327     case AMDGPU::OPERAND_REG_INLINE_C_V2INT16:
1328     case AMDGPU::OPERAND_REG_INLINE_C_V2FP16: {
1329       bool lost;
1330       APFloat FPLiteral(APFloat::IEEEdouble(), Literal);
1331       // Convert literal to single precision
1332       FPLiteral.convert(*getOpFltSemantics(OpTy),
1333                         APFloat::rmNearestTiesToEven, &lost);
1334       // We allow precision lost but not overflow or underflow. This should be
1335       // checked earlier in isLiteralImm()
1336
1337       uint64_t ImmVal = FPLiteral.bitcastToAPInt().getZExtValue();
1338       if (OpTy == AMDGPU::OPERAND_REG_INLINE_C_V2INT16 ||
1339           OpTy == AMDGPU::OPERAND_REG_INLINE_C_V2FP16) {
1340         ImmVal |= (ImmVal << 16);
1341       }
1342
1343       Inst.addOperand(MCOperand::createImm(ImmVal));
1344       return;
1345     }
1346     default:
1347       llvm_unreachable("invalid operand size");
1348     }
1349
1350     return;
1351   }
1352
1353    // We got int literal token.
1354   // Only sign extend inline immediates.
1355   // FIXME: No errors on truncation
1356   switch (OpTy) {
1357   case AMDGPU::OPERAND_REG_IMM_INT32:
1358   case AMDGPU::OPERAND_REG_IMM_FP32:
1359   case AMDGPU::OPERAND_REG_INLINE_C_INT32:
1360   case AMDGPU::OPERAND_REG_INLINE_C_FP32: {
1361     if (isInt<32>(Val) &&
1362         AMDGPU::isInlinableLiteral32(static_cast<int32_t>(Val),
1363                                      AsmParser->hasInv2PiInlineImm())) {
1364       Inst.addOperand(MCOperand::createImm(Val));
1365       return;
1366     }
1367
1368     Inst.addOperand(MCOperand::createImm(Val & 0xffffffff));
1369     return;
1370   }
1371   case AMDGPU::OPERAND_REG_IMM_INT64:
1372   case AMDGPU::OPERAND_REG_IMM_FP64:
1373   case AMDGPU::OPERAND_REG_INLINE_C_INT64:
1374   case AMDGPU::OPERAND_REG_INLINE_C_FP64: {
1375     if (AMDGPU::isInlinableLiteral64(Val, AsmParser->hasInv2PiInlineImm())) {
1376       Inst.addOperand(MCOperand::createImm(Val));
1377       return;
1378     }
1379
1380     Inst.addOperand(MCOperand::createImm(Lo_32(Val)));
1381     return;
1382   }
1383   case AMDGPU::OPERAND_REG_IMM_INT16:
1384   case AMDGPU::OPERAND_REG_IMM_FP16:
1385   case AMDGPU::OPERAND_REG_INLINE_C_INT16:
1386   case AMDGPU::OPERAND_REG_INLINE_C_FP16: {
1387     if (isInt<16>(Val) &&
1388         AMDGPU::isInlinableLiteral16(static_cast<int16_t>(Val),
1389                                      AsmParser->hasInv2PiInlineImm())) {
1390       Inst.addOperand(MCOperand::createImm(Val));
1391       return;
1392     }
1393
1394     Inst.addOperand(MCOperand::createImm(Val & 0xffff));
1395     return;
1396   }
1397   case AMDGPU::OPERAND_REG_INLINE_C_V2INT16:
1398   case AMDGPU::OPERAND_REG_INLINE_C_V2FP16: {
1399     auto LiteralVal = static_cast<uint16_t>(Literal.getLoBits(16).getZExtValue());
1400     assert(AMDGPU::isInlinableLiteral16(LiteralVal,
1401                                         AsmParser->hasInv2PiInlineImm()));
1402
1403     uint32_t ImmVal = static_cast<uint32_t>(LiteralVal) << 16 |
1404                       static_cast<uint32_t>(LiteralVal);
1405     Inst.addOperand(MCOperand::createImm(ImmVal));
1406     return;
1407   }
1408   default:
1409     llvm_unreachable("invalid operand size");
1410   }
1411 }
1412
1413 template <unsigned Bitwidth>
1414 void AMDGPUOperand::addKImmFPOperands(MCInst &Inst, unsigned N) const {
1415   APInt Literal(64, Imm.Val);
1416
1417   if (!Imm.IsFPImm) {
1418     // We got int literal token.
1419     Inst.addOperand(MCOperand::createImm(Literal.getLoBits(Bitwidth).getZExtValue()));
1420     return;
1421   }
1422
1423   bool Lost;
1424   APFloat FPLiteral(APFloat::IEEEdouble(), Literal);
1425   FPLiteral.convert(*getFltSemantics(Bitwidth / 8),
1426                     APFloat::rmNearestTiesToEven, &Lost);
1427   Inst.addOperand(MCOperand::createImm(FPLiteral.bitcastToAPInt().getZExtValue()));
1428 }
1429
1430 void AMDGPUOperand::addRegOperands(MCInst &Inst, unsigned N) const {
1431   Inst.addOperand(MCOperand::createReg(AMDGPU::getMCReg(getReg(), AsmParser->getSTI())));
1432 }
1433
1434 //===----------------------------------------------------------------------===//
1435 // AsmParser
1436 //===----------------------------------------------------------------------===//
1437
1438 static int getRegClass(RegisterKind Is, unsigned RegWidth) {
1439   if (Is == IS_VGPR) {
1440     switch (RegWidth) {
1441       default: return -1;
1442       case 1: return AMDGPU::VGPR_32RegClassID;
1443       case 2: return AMDGPU::VReg_64RegClassID;
1444       case 3: return AMDGPU::VReg_96RegClassID;
1445       case 4: return AMDGPU::VReg_128RegClassID;
1446       case 8: return AMDGPU::VReg_256RegClassID;
1447       case 16: return AMDGPU::VReg_512RegClassID;
1448     }
1449   } else if (Is == IS_TTMP) {
1450     switch (RegWidth) {
1451       default: return -1;
1452       case 1: return AMDGPU::TTMP_32RegClassID;
1453       case 2: return AMDGPU::TTMP_64RegClassID;
1454       case 4: return AMDGPU::TTMP_128RegClassID;
1455     }
1456   } else if (Is == IS_SGPR) {
1457     switch (RegWidth) {
1458       default: return -1;
1459       case 1: return AMDGPU::SGPR_32RegClassID;
1460       case 2: return AMDGPU::SGPR_64RegClassID;
1461       case 4: return AMDGPU::SGPR_128RegClassID;
1462       case 8: return AMDGPU::SReg_256RegClassID;
1463       case 16: return AMDGPU::SReg_512RegClassID;
1464     }
1465   }
1466   return -1;
1467 }
1468
1469 static unsigned getSpecialRegForName(StringRef RegName) {
1470   return StringSwitch<unsigned>(RegName)
1471     .Case("exec", AMDGPU::EXEC)
1472     .Case("vcc", AMDGPU::VCC)
1473     .Case("flat_scratch", AMDGPU::FLAT_SCR)
1474     .Case("m0", AMDGPU::M0)
1475     .Case("scc", AMDGPU::SCC)
1476     .Case("tba", AMDGPU::TBA)
1477     .Case("tma", AMDGPU::TMA)
1478     .Case("flat_scratch_lo", AMDGPU::FLAT_SCR_LO)
1479     .Case("flat_scratch_hi", AMDGPU::FLAT_SCR_HI)
1480     .Case("vcc_lo", AMDGPU::VCC_LO)
1481     .Case("vcc_hi", AMDGPU::VCC_HI)
1482     .Case("exec_lo", AMDGPU::EXEC_LO)
1483     .Case("exec_hi", AMDGPU::EXEC_HI)
1484     .Case("tma_lo", AMDGPU::TMA_LO)
1485     .Case("tma_hi", AMDGPU::TMA_HI)
1486     .Case("tba_lo", AMDGPU::TBA_LO)
1487     .Case("tba_hi", AMDGPU::TBA_HI)
1488     .Default(0);
1489 }
1490
1491 bool AMDGPUAsmParser::ParseRegister(unsigned &RegNo, SMLoc &StartLoc,
1492                                     SMLoc &EndLoc) {
1493   auto R = parseRegister();
1494   if (!R) return true;
1495   assert(R->isReg());
1496   RegNo = R->getReg();
1497   StartLoc = R->getStartLoc();
1498   EndLoc = R->getEndLoc();
1499   return false;
1500 }
1501
1502 bool AMDGPUAsmParser::AddNextRegisterToList(unsigned &Reg, unsigned &RegWidth,
1503                                             RegisterKind RegKind, unsigned Reg1,
1504                                             unsigned RegNum) {
1505   switch (RegKind) {
1506   case IS_SPECIAL:
1507     if (Reg == AMDGPU::EXEC_LO && Reg1 == AMDGPU::EXEC_HI) {
1508       Reg = AMDGPU::EXEC;
1509       RegWidth = 2;
1510       return true;
1511     }
1512     if (Reg == AMDGPU::FLAT_SCR_LO && Reg1 == AMDGPU::FLAT_SCR_HI) {
1513       Reg = AMDGPU::FLAT_SCR;
1514       RegWidth = 2;
1515       return true;
1516     }
1517     if (Reg == AMDGPU::VCC_LO && Reg1 == AMDGPU::VCC_HI) {
1518       Reg = AMDGPU::VCC;
1519       RegWidth = 2;
1520       return true;
1521     }
1522     if (Reg == AMDGPU::TBA_LO && Reg1 == AMDGPU::TBA_HI) {
1523       Reg = AMDGPU::TBA;
1524       RegWidth = 2;
1525       return true;
1526     }
1527     if (Reg == AMDGPU::TMA_LO && Reg1 == AMDGPU::TMA_HI) {
1528       Reg = AMDGPU::TMA;
1529       RegWidth = 2;
1530       return true;
1531     }
1532     return false;
1533   case IS_VGPR:
1534   case IS_SGPR:
1535   case IS_TTMP:
1536     if (Reg1 != Reg + RegWidth) {
1537       return false;
1538     }
1539     RegWidth++;
1540     return true;
1541   default:
1542     llvm_unreachable("unexpected register kind");
1543   }
1544 }
1545
1546 bool AMDGPUAsmParser::ParseAMDGPURegister(RegisterKind &RegKind, unsigned &Reg,
1547                                           unsigned &RegNum, unsigned &RegWidth,
1548                                           unsigned *DwordRegIndex) {
1549   if (DwordRegIndex) { *DwordRegIndex = 0; }
1550   const MCRegisterInfo *TRI = getContext().getRegisterInfo();
1551   if (getLexer().is(AsmToken::Identifier)) {
1552     StringRef RegName = Parser.getTok().getString();
1553     if ((Reg = getSpecialRegForName(RegName))) {
1554       Parser.Lex();
1555       RegKind = IS_SPECIAL;
1556     } else {
1557       unsigned RegNumIndex = 0;
1558       if (RegName[0] == 'v') {
1559         RegNumIndex = 1;
1560         RegKind = IS_VGPR;
1561       } else if (RegName[0] == 's') {
1562         RegNumIndex = 1;
1563         RegKind = IS_SGPR;
1564       } else if (RegName.startswith("ttmp")) {
1565         RegNumIndex = strlen("ttmp");
1566         RegKind = IS_TTMP;
1567       } else {
1568         return false;
1569       }
1570       if (RegName.size() > RegNumIndex) {
1571         // Single 32-bit register: vXX.
1572         if (RegName.substr(RegNumIndex).getAsInteger(10, RegNum))
1573           return false;
1574         Parser.Lex();
1575         RegWidth = 1;
1576       } else {
1577         // Range of registers: v[XX:YY]. ":YY" is optional.
1578         Parser.Lex();
1579         int64_t RegLo, RegHi;
1580         if (getLexer().isNot(AsmToken::LBrac))
1581           return false;
1582         Parser.Lex();
1583
1584         if (getParser().parseAbsoluteExpression(RegLo))
1585           return false;
1586
1587         const bool isRBrace = getLexer().is(AsmToken::RBrac);
1588         if (!isRBrace && getLexer().isNot(AsmToken::Colon))
1589           return false;
1590         Parser.Lex();
1591
1592         if (isRBrace) {
1593           RegHi = RegLo;
1594         } else {
1595           if (getParser().parseAbsoluteExpression(RegHi))
1596             return false;
1597
1598           if (getLexer().isNot(AsmToken::RBrac))
1599             return false;
1600           Parser.Lex();
1601         }
1602         RegNum = (unsigned) RegLo;
1603         RegWidth = (RegHi - RegLo) + 1;
1604       }
1605     }
1606   } else if (getLexer().is(AsmToken::LBrac)) {
1607     // List of consecutive registers: [s0,s1,s2,s3]
1608     Parser.Lex();
1609     if (!ParseAMDGPURegister(RegKind, Reg, RegNum, RegWidth, nullptr))
1610       return false;
1611     if (RegWidth != 1)
1612       return false;
1613     RegisterKind RegKind1;
1614     unsigned Reg1, RegNum1, RegWidth1;
1615     do {
1616       if (getLexer().is(AsmToken::Comma)) {
1617         Parser.Lex();
1618       } else if (getLexer().is(AsmToken::RBrac)) {
1619         Parser.Lex();
1620         break;
1621       } else if (ParseAMDGPURegister(RegKind1, Reg1, RegNum1, RegWidth1, nullptr)) {
1622         if (RegWidth1 != 1) {
1623           return false;
1624         }
1625         if (RegKind1 != RegKind) {
1626           return false;
1627         }
1628         if (!AddNextRegisterToList(Reg, RegWidth, RegKind1, Reg1, RegNum1)) {
1629           return false;
1630         }
1631       } else {
1632         return false;
1633       }
1634     } while (true);
1635   } else {
1636     return false;
1637   }
1638   switch (RegKind) {
1639   case IS_SPECIAL:
1640     RegNum = 0;
1641     RegWidth = 1;
1642     break;
1643   case IS_VGPR:
1644   case IS_SGPR:
1645   case IS_TTMP:
1646   {
1647     unsigned Size = 1;
1648     if (RegKind == IS_SGPR || RegKind == IS_TTMP) {
1649       // SGPR and TTMP registers must be aligned. Max required alignment is 4 dwords.
1650       Size = std::min(RegWidth, 4u);
1651     }
1652     if (RegNum % Size != 0)
1653       return false;
1654     if (DwordRegIndex) { *DwordRegIndex = RegNum; }
1655     RegNum = RegNum / Size;
1656     int RCID = getRegClass(RegKind, RegWidth);
1657     if (RCID == -1)
1658       return false;
1659     const MCRegisterClass RC = TRI->getRegClass(RCID);
1660     if (RegNum >= RC.getNumRegs())
1661       return false;
1662     Reg = RC.getRegister(RegNum);
1663     break;
1664   }
1665
1666   default:
1667     llvm_unreachable("unexpected register kind");
1668   }
1669
1670   if (!subtargetHasRegister(*TRI, Reg))
1671     return false;
1672   return true;
1673 }
1674
1675 std::unique_ptr<AMDGPUOperand> AMDGPUAsmParser::parseRegister() {
1676   const auto &Tok = Parser.getTok();
1677   SMLoc StartLoc = Tok.getLoc();
1678   SMLoc EndLoc = Tok.getEndLoc();
1679   RegisterKind RegKind;
1680   unsigned Reg, RegNum, RegWidth, DwordRegIndex;
1681
1682   if (!ParseAMDGPURegister(RegKind, Reg, RegNum, RegWidth, &DwordRegIndex)) {
1683     return nullptr;
1684   }
1685   KernelScope.usesRegister(RegKind, DwordRegIndex, RegWidth);
1686   return AMDGPUOperand::CreateReg(this, Reg, StartLoc, EndLoc, false);
1687 }
1688
1689 bool
1690 AMDGPUAsmParser::parseAbsoluteExpr(int64_t &Val, bool AbsMod) {
1691   if (AbsMod && getLexer().peekTok().is(AsmToken::Pipe) &&
1692       (getLexer().getKind() == AsmToken::Integer ||
1693        getLexer().getKind() == AsmToken::Real)) {
1694
1695     // This is a workaround for handling operands like these:
1696     //     |1.0|
1697     //     |-1|
1698     // This syntax is not compatible with syntax of standard
1699     // MC expressions (due to the trailing '|').
1700
1701     SMLoc EndLoc;
1702     const MCExpr *Expr;
1703
1704     if (getParser().parsePrimaryExpr(Expr, EndLoc)) {
1705       return true;
1706     }
1707
1708     return !Expr->evaluateAsAbsolute(Val);
1709   }
1710
1711   return getParser().parseAbsoluteExpression(Val);
1712 }
1713
1714 OperandMatchResultTy
1715 AMDGPUAsmParser::parseImm(OperandVector &Operands, bool AbsMod) {
1716   // TODO: add syntactic sugar for 1/(2*PI)
1717   bool Minus = false;
1718   if (getLexer().getKind() == AsmToken::Minus) {
1719     Minus = true;
1720     Parser.Lex();
1721   }
1722
1723   SMLoc S = Parser.getTok().getLoc();
1724   switch(getLexer().getKind()) {
1725   case AsmToken::Integer: {
1726     int64_t IntVal;
1727     if (parseAbsoluteExpr(IntVal, AbsMod))
1728       return MatchOperand_ParseFail;
1729     if (Minus)
1730       IntVal *= -1;
1731     Operands.push_back(AMDGPUOperand::CreateImm(this, IntVal, S));
1732     return MatchOperand_Success;
1733   }
1734   case AsmToken::Real: {
1735     int64_t IntVal;
1736     if (parseAbsoluteExpr(IntVal, AbsMod))
1737       return MatchOperand_ParseFail;
1738
1739     APFloat F(BitsToDouble(IntVal));
1740     if (Minus)
1741       F.changeSign();
1742     Operands.push_back(
1743         AMDGPUOperand::CreateImm(this, F.bitcastToAPInt().getZExtValue(), S,
1744                                  AMDGPUOperand::ImmTyNone, true));
1745     return MatchOperand_Success;
1746   }
1747   default:
1748     return Minus ? MatchOperand_ParseFail : MatchOperand_NoMatch;
1749   }
1750 }
1751
1752 OperandMatchResultTy
1753 AMDGPUAsmParser::parseReg(OperandVector &Operands) {
1754   if (auto R = parseRegister()) {
1755     assert(R->isReg());
1756     R->Reg.IsForcedVOP3 = isForcedVOP3();
1757     Operands.push_back(std::move(R));
1758     return MatchOperand_Success;
1759   }
1760   return MatchOperand_NoMatch;
1761 }
1762
1763 OperandMatchResultTy
1764 AMDGPUAsmParser::parseRegOrImm(OperandVector &Operands, bool AbsMod) {
1765   auto res = parseImm(Operands, AbsMod);
1766   if (res != MatchOperand_NoMatch) {
1767     return res;
1768   }
1769
1770   return parseReg(Operands);
1771 }
1772
1773 OperandMatchResultTy
1774 AMDGPUAsmParser::parseRegOrImmWithFPInputMods(OperandVector &Operands,
1775                                               bool AllowImm) {
1776   bool Negate = false, Negate2 = false, Abs = false, Abs2 = false;
1777
1778   if (getLexer().getKind()== AsmToken::Minus) {
1779     const AsmToken NextToken = getLexer().peekTok();
1780
1781     // Disable ambiguous constructs like '--1' etc. Should use neg(-1) instead.
1782     if (NextToken.is(AsmToken::Minus)) {
1783       Error(Parser.getTok().getLoc(), "invalid syntax, expected 'neg' modifier");
1784       return MatchOperand_ParseFail;
1785     }
1786
1787     // '-' followed by an integer literal N should be interpreted as integer
1788     // negation rather than a floating-point NEG modifier applied to N.
1789     // Beside being contr-intuitive, such use of floating-point NEG modifier
1790     // results in different meaning of integer literals used with VOP1/2/C
1791     // and VOP3, for example:
1792     //    v_exp_f32_e32 v5, -1 // VOP1: src0 = 0xFFFFFFFF
1793     //    v_exp_f32_e64 v5, -1 // VOP3: src0 = 0x80000001
1794     // Negative fp literals should be handled likewise for unifomtity
1795     if (!NextToken.is(AsmToken::Integer) && !NextToken.is(AsmToken::Real)) {
1796       Parser.Lex();
1797       Negate = true;
1798     }
1799   }
1800
1801   if (getLexer().getKind() == AsmToken::Identifier &&
1802       Parser.getTok().getString() == "neg") {
1803     if (Negate) {
1804       Error(Parser.getTok().getLoc(), "expected register or immediate");
1805       return MatchOperand_ParseFail;
1806     }
1807     Parser.Lex();
1808     Negate2 = true;
1809     if (getLexer().isNot(AsmToken::LParen)) {
1810       Error(Parser.getTok().getLoc(), "expected left paren after neg");
1811       return MatchOperand_ParseFail;
1812     }
1813     Parser.Lex();
1814   }
1815
1816   if (getLexer().getKind() == AsmToken::Identifier &&
1817       Parser.getTok().getString() == "abs") {
1818     Parser.Lex();
1819     Abs2 = true;
1820     if (getLexer().isNot(AsmToken::LParen)) {
1821       Error(Parser.getTok().getLoc(), "expected left paren after abs");
1822       return MatchOperand_ParseFail;
1823     }
1824     Parser.Lex();
1825   }
1826
1827   if (getLexer().getKind() == AsmToken::Pipe) {
1828     if (Abs2) {
1829       Error(Parser.getTok().getLoc(), "expected register or immediate");
1830       return MatchOperand_ParseFail;
1831     }
1832     Parser.Lex();
1833     Abs = true;
1834   }
1835
1836   OperandMatchResultTy Res;
1837   if (AllowImm) {
1838     Res = parseRegOrImm(Operands, Abs);
1839   } else {
1840     Res = parseReg(Operands);
1841   }
1842   if (Res != MatchOperand_Success) {
1843     return Res;
1844   }
1845
1846   AMDGPUOperand::Modifiers Mods;
1847   if (Abs) {
1848     if (getLexer().getKind() != AsmToken::Pipe) {
1849       Error(Parser.getTok().getLoc(), "expected vertical bar");
1850       return MatchOperand_ParseFail;
1851     }
1852     Parser.Lex();
1853     Mods.Abs = true;
1854   }
1855   if (Abs2) {
1856     if (getLexer().isNot(AsmToken::RParen)) {
1857       Error(Parser.getTok().getLoc(), "expected closing parentheses");
1858       return MatchOperand_ParseFail;
1859     }
1860     Parser.Lex();
1861     Mods.Abs = true;
1862   }
1863
1864   if (Negate) {
1865     Mods.Neg = true;
1866   } else if (Negate2) {
1867     if (getLexer().isNot(AsmToken::RParen)) {
1868       Error(Parser.getTok().getLoc(), "expected closing parentheses");
1869       return MatchOperand_ParseFail;
1870     }
1871     Parser.Lex();
1872     Mods.Neg = true;
1873   }
1874
1875   if (Mods.hasFPModifiers()) {
1876     AMDGPUOperand &Op = static_cast<AMDGPUOperand &>(*Operands.back());
1877     Op.setModifiers(Mods);
1878   }
1879   return MatchOperand_Success;
1880 }
1881
1882 OperandMatchResultTy
1883 AMDGPUAsmParser::parseRegOrImmWithIntInputMods(OperandVector &Operands,
1884                                                bool AllowImm) {
1885   bool Sext = false;
1886
1887   if (getLexer().getKind() == AsmToken::Identifier &&
1888       Parser.getTok().getString() == "sext") {
1889     Parser.Lex();
1890     Sext = true;
1891     if (getLexer().isNot(AsmToken::LParen)) {
1892       Error(Parser.getTok().getLoc(), "expected left paren after sext");
1893       return MatchOperand_ParseFail;
1894     }
1895     Parser.Lex();
1896   }
1897
1898   OperandMatchResultTy Res;
1899   if (AllowImm) {
1900     Res = parseRegOrImm(Operands);
1901   } else {
1902     Res = parseReg(Operands);
1903   }
1904   if (Res != MatchOperand_Success) {
1905     return Res;
1906   }
1907
1908   AMDGPUOperand::Modifiers Mods;
1909   if (Sext) {
1910     if (getLexer().isNot(AsmToken::RParen)) {
1911       Error(Parser.getTok().getLoc(), "expected closing parentheses");
1912       return MatchOperand_ParseFail;
1913     }
1914     Parser.Lex();
1915     Mods.Sext = true;
1916   }
1917
1918   if (Mods.hasIntModifiers()) {
1919     AMDGPUOperand &Op = static_cast<AMDGPUOperand &>(*Operands.back());
1920     Op.setModifiers(Mods);
1921   }
1922
1923   return MatchOperand_Success;
1924 }
1925
1926 OperandMatchResultTy
1927 AMDGPUAsmParser::parseRegWithFPInputMods(OperandVector &Operands) {
1928   return parseRegOrImmWithFPInputMods(Operands, false);
1929 }
1930
1931 OperandMatchResultTy
1932 AMDGPUAsmParser::parseRegWithIntInputMods(OperandVector &Operands) {
1933   return parseRegOrImmWithIntInputMods(Operands, false);
1934 }
1935
1936 OperandMatchResultTy AMDGPUAsmParser::parseVReg32OrOff(OperandVector &Operands) {
1937   std::unique_ptr<AMDGPUOperand> Reg = parseRegister();
1938   if (Reg) {
1939     Operands.push_back(std::move(Reg));
1940     return MatchOperand_Success;
1941   }
1942
1943   const AsmToken &Tok = Parser.getTok();
1944   if (Tok.getString() == "off") {
1945     Operands.push_back(AMDGPUOperand::CreateImm(this, 0, Tok.getLoc(),
1946                                                 AMDGPUOperand::ImmTyOff, false));
1947     Parser.Lex();
1948     return MatchOperand_Success;
1949   }
1950
1951   return MatchOperand_NoMatch;
1952 }
1953
1954 unsigned AMDGPUAsmParser::checkTargetMatchPredicate(MCInst &Inst) {
1955   uint64_t TSFlags = MII.get(Inst.getOpcode()).TSFlags;
1956
1957   if ((getForcedEncodingSize() == 32 && (TSFlags & SIInstrFlags::VOP3)) ||
1958       (getForcedEncodingSize() == 64 && !(TSFlags & SIInstrFlags::VOP3)) ||
1959       (isForcedDPP() && !(TSFlags & SIInstrFlags::DPP)) ||
1960       (isForcedSDWA() && !(TSFlags & SIInstrFlags::SDWA)) )
1961     return Match_InvalidOperand;
1962
1963   if ((TSFlags & SIInstrFlags::VOP3) &&
1964       (TSFlags & SIInstrFlags::VOPAsmPrefer32Bit) &&
1965       getForcedEncodingSize() != 64)
1966     return Match_PreferE32;
1967
1968   if (Inst.getOpcode() == AMDGPU::V_MAC_F32_sdwa_vi ||
1969       Inst.getOpcode() == AMDGPU::V_MAC_F16_sdwa_vi) {
1970     // v_mac_f32/16 allow only dst_sel == DWORD;
1971     auto OpNum =
1972         AMDGPU::getNamedOperandIdx(Inst.getOpcode(), AMDGPU::OpName::dst_sel);
1973     const auto &Op = Inst.getOperand(OpNum);
1974     if (!Op.isImm() || Op.getImm() != AMDGPU::SDWA::SdwaSel::DWORD) {
1975       return Match_InvalidOperand;
1976     }
1977   }
1978
1979   return Match_Success;
1980 }
1981
1982 // What asm variants we should check
1983 ArrayRef<unsigned> AMDGPUAsmParser::getMatchedVariants() const {
1984   if (getForcedEncodingSize() == 32) {
1985     static const unsigned Variants[] = {AMDGPUAsmVariants::DEFAULT};
1986     return makeArrayRef(Variants);
1987   }
1988
1989   if (isForcedVOP3()) {
1990     static const unsigned Variants[] = {AMDGPUAsmVariants::VOP3};
1991     return makeArrayRef(Variants);
1992   }
1993
1994   if (isForcedSDWA()) {
1995     static const unsigned Variants[] = {AMDGPUAsmVariants::SDWA,
1996                                         AMDGPUAsmVariants::SDWA9};
1997     return makeArrayRef(Variants);
1998   }
1999
2000   if (isForcedDPP()) {
2001     static const unsigned Variants[] = {AMDGPUAsmVariants::DPP};
2002     return makeArrayRef(Variants);
2003   }
2004
2005   static const unsigned Variants[] = {
2006     AMDGPUAsmVariants::DEFAULT, AMDGPUAsmVariants::VOP3,
2007     AMDGPUAsmVariants::SDWA, AMDGPUAsmVariants::SDWA9, AMDGPUAsmVariants::DPP
2008   };
2009
2010   return makeArrayRef(Variants);
2011 }
2012
2013 unsigned AMDGPUAsmParser::findImplicitSGPRReadInVOP(const MCInst &Inst) const {
2014   const MCInstrDesc &Desc = MII.get(Inst.getOpcode());
2015   const unsigned Num = Desc.getNumImplicitUses();
2016   for (unsigned i = 0; i < Num; ++i) {
2017     unsigned Reg = Desc.ImplicitUses[i];
2018     switch (Reg) {
2019     case AMDGPU::FLAT_SCR:
2020     case AMDGPU::VCC:
2021     case AMDGPU::M0:
2022       return Reg;
2023     default:
2024       break;
2025     }
2026   }
2027   return AMDGPU::NoRegister;
2028 }
2029
2030 // NB: This code is correct only when used to check constant
2031 // bus limitations because GFX7 support no f16 inline constants.
2032 // Note that there are no cases when a GFX7 opcode violates
2033 // constant bus limitations due to the use of an f16 constant.
2034 bool AMDGPUAsmParser::isInlineConstant(const MCInst &Inst,
2035                                        unsigned OpIdx) const {
2036   const MCInstrDesc &Desc = MII.get(Inst.getOpcode());
2037
2038   if (!AMDGPU::isSISrcOperand(Desc, OpIdx)) {
2039     return false;
2040   }
2041
2042   const MCOperand &MO = Inst.getOperand(OpIdx);
2043
2044   int64_t Val = MO.getImm();
2045   auto OpSize = AMDGPU::getOperandSize(Desc, OpIdx);
2046
2047   switch (OpSize) { // expected operand size
2048   case 8:
2049     return AMDGPU::isInlinableLiteral64(Val, hasInv2PiInlineImm());
2050   case 4:
2051     return AMDGPU::isInlinableLiteral32(Val, hasInv2PiInlineImm());
2052   case 2: {
2053     const unsigned OperandType = Desc.OpInfo[OpIdx].OperandType;
2054     if (OperandType == AMDGPU::OPERAND_REG_INLINE_C_V2INT16 ||
2055         OperandType == AMDGPU::OPERAND_REG_INLINE_C_V2FP16) {
2056       return AMDGPU::isInlinableLiteralV216(Val, hasInv2PiInlineImm());
2057     } else {
2058       return AMDGPU::isInlinableLiteral16(Val, hasInv2PiInlineImm());
2059     }
2060   }
2061   default:
2062     llvm_unreachable("invalid operand size");
2063   }
2064 }
2065
2066 bool AMDGPUAsmParser::usesConstantBus(const MCInst &Inst, unsigned OpIdx) {
2067   const MCOperand &MO = Inst.getOperand(OpIdx);
2068   if (MO.isImm()) {
2069     return !isInlineConstant(Inst, OpIdx);
2070   }
2071   return !MO.isReg() ||
2072          isSGPR(mc2PseudoReg(MO.getReg()), getContext().getRegisterInfo());
2073 }
2074
2075 bool AMDGPUAsmParser::validateOperandLimitations(const MCInst &Inst) {
2076   const unsigned Opcode = Inst.getOpcode();
2077   const MCInstrDesc &Desc = MII.get(Opcode);
2078   unsigned ConstantBusUseCount = 0;
2079
2080   if (Desc.TSFlags &
2081       (SIInstrFlags::VOPC |
2082        SIInstrFlags::VOP1 | SIInstrFlags::VOP2 |
2083        SIInstrFlags::VOP3 | SIInstrFlags::VOP3P |
2084        SIInstrFlags::SDWA)) {
2085
2086     // Check special imm operands (used by madmk, etc)
2087     if (AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::imm) != -1) {
2088       ++ConstantBusUseCount;
2089     }
2090
2091     unsigned SGPRUsed = findImplicitSGPRReadInVOP(Inst);
2092     if (SGPRUsed != AMDGPU::NoRegister) {
2093       ++ConstantBusUseCount;
2094     }
2095
2096     const int Src0Idx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::src0);
2097     const int Src1Idx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::src1);
2098     const int Src2Idx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::src2);
2099
2100     const int OpIndices[] = { Src0Idx, Src1Idx, Src2Idx };
2101
2102     for (int OpIdx : OpIndices) {
2103       if (OpIdx == -1) break;
2104
2105       const MCOperand &MO = Inst.getOperand(OpIdx);
2106       if (usesConstantBus(Inst, OpIdx)) {
2107         if (MO.isReg()) {
2108           const unsigned Reg = mc2PseudoReg(MO.getReg());
2109           // Pairs of registers with a partial intersections like these
2110           //   s0, s[0:1]
2111           //   flat_scratch_lo, flat_scratch
2112           //   flat_scratch_lo, flat_scratch_hi
2113           // are theoretically valid but they are disabled anyway.
2114           // Note that this code mimics SIInstrInfo::verifyInstruction
2115           if (Reg != SGPRUsed) {
2116             ++ConstantBusUseCount;
2117           }
2118           SGPRUsed = Reg;
2119         } else { // Expression or a literal
2120           ++ConstantBusUseCount;
2121         }
2122       }
2123     }
2124   }
2125
2126   return ConstantBusUseCount <= 1;
2127 }
2128
2129 bool AMDGPUAsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
2130                                               OperandVector &Operands,
2131                                               MCStreamer &Out,
2132                                               uint64_t &ErrorInfo,
2133                                               bool MatchingInlineAsm) {
2134   MCInst Inst;
2135   unsigned Result = Match_Success;
2136   for (auto Variant : getMatchedVariants()) {
2137     uint64_t EI;
2138     auto R = MatchInstructionImpl(Operands, Inst, EI, MatchingInlineAsm,
2139                                   Variant);
2140     // We order match statuses from least to most specific. We use most specific
2141     // status as resulting
2142     // Match_MnemonicFail < Match_InvalidOperand < Match_MissingFeature < Match_PreferE32
2143     if ((R == Match_Success) ||
2144         (R == Match_PreferE32) ||
2145         (R == Match_MissingFeature && Result != Match_PreferE32) ||
2146         (R == Match_InvalidOperand && Result != Match_MissingFeature
2147                                    && Result != Match_PreferE32) ||
2148         (R == Match_MnemonicFail   && Result != Match_InvalidOperand
2149                                    && Result != Match_MissingFeature
2150                                    && Result != Match_PreferE32)) {
2151       Result = R;
2152       ErrorInfo = EI;
2153     }
2154     if (R == Match_Success)
2155       break;
2156   }
2157
2158   switch (Result) {
2159   default: break;
2160   case Match_Success:
2161     if (!validateOperandLimitations(Inst)) {
2162       return Error(IDLoc,
2163                    "invalid operand (violates constant bus restrictions)");
2164     }
2165     Inst.setLoc(IDLoc);
2166     Out.EmitInstruction(Inst, getSTI());
2167     return false;
2168
2169   case Match_MissingFeature:
2170     return Error(IDLoc, "instruction not supported on this GPU");
2171
2172   case Match_MnemonicFail:
2173     return Error(IDLoc, "unrecognized instruction mnemonic");
2174
2175   case Match_InvalidOperand: {
2176     SMLoc ErrorLoc = IDLoc;
2177     if (ErrorInfo != ~0ULL) {
2178       if (ErrorInfo >= Operands.size()) {
2179         return Error(IDLoc, "too few operands for instruction");
2180       }
2181       ErrorLoc = ((AMDGPUOperand &)*Operands[ErrorInfo]).getStartLoc();
2182       if (ErrorLoc == SMLoc())
2183         ErrorLoc = IDLoc;
2184     }
2185     return Error(ErrorLoc, "invalid operand for instruction");
2186   }
2187
2188   case Match_PreferE32:
2189     return Error(IDLoc, "internal error: instruction without _e64 suffix "
2190                         "should be encoded as e32");
2191   }
2192   llvm_unreachable("Implement any new match types added!");
2193 }
2194
2195 bool AMDGPUAsmParser::ParseAsAbsoluteExpression(uint32_t &Ret) {
2196   int64_t Tmp = -1;
2197   if (getLexer().isNot(AsmToken::Integer) && getLexer().isNot(AsmToken::Identifier)) {
2198     return true;
2199   }
2200   if (getParser().parseAbsoluteExpression(Tmp)) {
2201     return true;
2202   }
2203   Ret = static_cast<uint32_t>(Tmp);
2204   return false;
2205 }
2206
2207 bool AMDGPUAsmParser::ParseDirectiveMajorMinor(uint32_t &Major,
2208                                                uint32_t &Minor) {
2209   if (ParseAsAbsoluteExpression(Major))
2210     return TokError("invalid major version");
2211
2212   if (getLexer().isNot(AsmToken::Comma))
2213     return TokError("minor version number required, comma expected");
2214   Lex();
2215
2216   if (ParseAsAbsoluteExpression(Minor))
2217     return TokError("invalid minor version");
2218
2219   return false;
2220 }
2221
2222 bool AMDGPUAsmParser::ParseDirectiveHSACodeObjectVersion() {
2223   uint32_t Major;
2224   uint32_t Minor;
2225
2226   if (ParseDirectiveMajorMinor(Major, Minor))
2227     return true;
2228
2229   getTargetStreamer().EmitDirectiveHSACodeObjectVersion(Major, Minor);
2230   return false;
2231 }
2232
2233 bool AMDGPUAsmParser::ParseDirectiveHSACodeObjectISA() {
2234   uint32_t Major;
2235   uint32_t Minor;
2236   uint32_t Stepping;
2237   StringRef VendorName;
2238   StringRef ArchName;
2239
2240   // If this directive has no arguments, then use the ISA version for the
2241   // targeted GPU.
2242   if (getLexer().is(AsmToken::EndOfStatement)) {
2243     AMDGPU::IsaInfo::IsaVersion ISA =
2244         AMDGPU::IsaInfo::getIsaVersion(getFeatureBits());
2245     getTargetStreamer().EmitDirectiveHSACodeObjectISA(ISA.Major, ISA.Minor,
2246                                                       ISA.Stepping,
2247                                                       "AMD", "AMDGPU");
2248     return false;
2249   }
2250
2251   if (ParseDirectiveMajorMinor(Major, Minor))
2252     return true;
2253
2254   if (getLexer().isNot(AsmToken::Comma))
2255     return TokError("stepping version number required, comma expected");
2256   Lex();
2257
2258   if (ParseAsAbsoluteExpression(Stepping))
2259     return TokError("invalid stepping version");
2260
2261   if (getLexer().isNot(AsmToken::Comma))
2262     return TokError("vendor name required, comma expected");
2263   Lex();
2264
2265   if (getLexer().isNot(AsmToken::String))
2266     return TokError("invalid vendor name");
2267
2268   VendorName = getLexer().getTok().getStringContents();
2269   Lex();
2270
2271   if (getLexer().isNot(AsmToken::Comma))
2272     return TokError("arch name required, comma expected");
2273   Lex();
2274
2275   if (getLexer().isNot(AsmToken::String))
2276     return TokError("invalid arch name");
2277
2278   ArchName = getLexer().getTok().getStringContents();
2279   Lex();
2280
2281   getTargetStreamer().EmitDirectiveHSACodeObjectISA(Major, Minor, Stepping,
2282                                                     VendorName, ArchName);
2283   return false;
2284 }
2285
2286 bool AMDGPUAsmParser::ParseDirectiveCodeObjectMetadata() {
2287   std::string YamlString;
2288   raw_string_ostream YamlStream(YamlString);
2289
2290   getLexer().setSkipSpace(false);
2291
2292   bool FoundEnd = false;
2293   while (!getLexer().is(AsmToken::Eof)) {
2294     while (getLexer().is(AsmToken::Space)) {
2295       YamlStream << getLexer().getTok().getString();
2296       Lex();
2297     }
2298
2299     if (getLexer().is(AsmToken::Identifier)) {
2300       StringRef ID = getLexer().getTok().getIdentifier();
2301       if (ID == AMDGPU::CodeObject::MetadataAssemblerDirectiveEnd) {
2302         Lex();
2303         FoundEnd = true;
2304         break;
2305       }
2306     }
2307
2308     YamlStream << Parser.parseStringToEndOfStatement()
2309                << getContext().getAsmInfo()->getSeparatorString();
2310
2311     Parser.eatToEndOfStatement();
2312   }
2313
2314   getLexer().setSkipSpace(true);
2315
2316   if (getLexer().is(AsmToken::Eof) && !FoundEnd) {
2317     return TokError(
2318         "expected directive .end_amdgpu_code_object_metadata not found");
2319   }
2320
2321   YamlStream.flush();
2322
2323   if (!getTargetStreamer().EmitCodeObjectMetadata(YamlString))
2324     return Error(getParser().getTok().getLoc(), "invalid code object metadata");
2325
2326   return false;
2327 }
2328
2329 bool AMDGPUAsmParser::ParseAMDKernelCodeTValue(StringRef ID,
2330                                                amd_kernel_code_t &Header) {
2331   SmallString<40> ErrStr;
2332   raw_svector_ostream Err(ErrStr);
2333   if (!parseAmdKernelCodeField(ID, getParser(), Header, Err)) {
2334     return TokError(Err.str());
2335   }
2336   Lex();
2337   return false;
2338 }
2339
2340 bool AMDGPUAsmParser::ParseDirectiveAMDKernelCodeT() {
2341   amd_kernel_code_t Header;
2342   AMDGPU::initDefaultAMDKernelCodeT(Header, getFeatureBits());
2343
2344   while (true) {
2345     // Lex EndOfStatement.  This is in a while loop, because lexing a comment
2346     // will set the current token to EndOfStatement.
2347     while(getLexer().is(AsmToken::EndOfStatement))
2348       Lex();
2349
2350     if (getLexer().isNot(AsmToken::Identifier))
2351       return TokError("expected value identifier or .end_amd_kernel_code_t");
2352
2353     StringRef ID = getLexer().getTok().getIdentifier();
2354     Lex();
2355
2356     if (ID == ".end_amd_kernel_code_t")
2357       break;
2358
2359     if (ParseAMDKernelCodeTValue(ID, Header))
2360       return true;
2361   }
2362
2363   getTargetStreamer().EmitAMDKernelCodeT(Header);
2364
2365   return false;
2366 }
2367
2368 bool AMDGPUAsmParser::ParseSectionDirectiveHSAText() {
2369   getParser().getStreamer().SwitchSection(
2370       AMDGPU::getHSATextSection(getContext()));
2371   return false;
2372 }
2373
2374 bool AMDGPUAsmParser::ParseDirectiveAMDGPUHsaKernel() {
2375   if (getLexer().isNot(AsmToken::Identifier))
2376     return TokError("expected symbol name");
2377
2378   StringRef KernelName = Parser.getTok().getString();
2379
2380   getTargetStreamer().EmitAMDGPUSymbolType(KernelName,
2381                                            ELF::STT_AMDGPU_HSA_KERNEL);
2382   Lex();
2383   KernelScope.initialize(getContext());
2384   return false;
2385 }
2386
2387 bool AMDGPUAsmParser::ParseDirectiveAMDGPUHsaModuleGlobal() {
2388   if (getLexer().isNot(AsmToken::Identifier))
2389     return TokError("expected symbol name");
2390
2391   StringRef GlobalName = Parser.getTok().getIdentifier();
2392
2393   getTargetStreamer().EmitAMDGPUHsaModuleScopeGlobal(GlobalName);
2394   Lex();
2395   return false;
2396 }
2397
2398 bool AMDGPUAsmParser::ParseDirectiveAMDGPUHsaProgramGlobal() {
2399   if (getLexer().isNot(AsmToken::Identifier))
2400     return TokError("expected symbol name");
2401
2402   StringRef GlobalName = Parser.getTok().getIdentifier();
2403
2404   getTargetStreamer().EmitAMDGPUHsaProgramScopeGlobal(GlobalName);
2405   Lex();
2406   return false;
2407 }
2408
2409 bool AMDGPUAsmParser::ParseSectionDirectiveHSADataGlobalAgent() {
2410   getParser().getStreamer().SwitchSection(
2411       AMDGPU::getHSADataGlobalAgentSection(getContext()));
2412   return false;
2413 }
2414
2415 bool AMDGPUAsmParser::ParseSectionDirectiveHSADataGlobalProgram() {
2416   getParser().getStreamer().SwitchSection(
2417       AMDGPU::getHSADataGlobalProgramSection(getContext()));
2418   return false;
2419 }
2420
2421 bool AMDGPUAsmParser::ParseSectionDirectiveHSARodataReadonlyAgent() {
2422   getParser().getStreamer().SwitchSection(
2423       AMDGPU::getHSARodataReadonlyAgentSection(getContext()));
2424   return false;
2425 }
2426
2427 bool AMDGPUAsmParser::ParseDirective(AsmToken DirectiveID) {
2428   StringRef IDVal = DirectiveID.getString();
2429
2430   if (IDVal == ".hsa_code_object_version")
2431     return ParseDirectiveHSACodeObjectVersion();
2432
2433   if (IDVal == ".hsa_code_object_isa")
2434     return ParseDirectiveHSACodeObjectISA();
2435
2436   if (IDVal == AMDGPU::CodeObject::MetadataAssemblerDirectiveBegin)
2437     return ParseDirectiveCodeObjectMetadata();
2438
2439   if (IDVal == ".amd_kernel_code_t")
2440     return ParseDirectiveAMDKernelCodeT();
2441
2442   if (IDVal == ".hsatext")
2443     return ParseSectionDirectiveHSAText();
2444
2445   if (IDVal == ".amdgpu_hsa_kernel")
2446     return ParseDirectiveAMDGPUHsaKernel();
2447
2448   if (IDVal == ".amdgpu_hsa_module_global")
2449     return ParseDirectiveAMDGPUHsaModuleGlobal();
2450
2451   if (IDVal == ".amdgpu_hsa_program_global")
2452     return ParseDirectiveAMDGPUHsaProgramGlobal();
2453
2454   if (IDVal == ".hsadata_global_agent")
2455     return ParseSectionDirectiveHSADataGlobalAgent();
2456
2457   if (IDVal == ".hsadata_global_program")
2458     return ParseSectionDirectiveHSADataGlobalProgram();
2459
2460   if (IDVal == ".hsarodata_readonly_agent")
2461     return ParseSectionDirectiveHSARodataReadonlyAgent();
2462
2463   return true;
2464 }
2465
2466 bool AMDGPUAsmParser::subtargetHasRegister(const MCRegisterInfo &MRI,
2467                                            unsigned RegNo) const {
2468   if (isCI())
2469     return true;
2470
2471   if (isSI()) {
2472     // No flat_scr
2473     switch (RegNo) {
2474     case AMDGPU::FLAT_SCR:
2475     case AMDGPU::FLAT_SCR_LO:
2476     case AMDGPU::FLAT_SCR_HI:
2477       return false;
2478     default:
2479       return true;
2480     }
2481   }
2482
2483   // VI only has 102 SGPRs, so make sure we aren't trying to use the 2 more that
2484   // SI/CI have.
2485   for (MCRegAliasIterator R(AMDGPU::SGPR102_SGPR103, &MRI, true);
2486        R.isValid(); ++R) {
2487     if (*R == RegNo)
2488       return false;
2489   }
2490
2491   return true;
2492 }
2493
2494 OperandMatchResultTy
2495 AMDGPUAsmParser::parseOperand(OperandVector &Operands, StringRef Mnemonic) {
2496   // Try to parse with a custom parser
2497   OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
2498
2499   // If we successfully parsed the operand or if there as an error parsing,
2500   // we are done.
2501   //
2502   // If we are parsing after we reach EndOfStatement then this means we
2503   // are appending default values to the Operands list.  This is only done
2504   // by custom parser, so we shouldn't continue on to the generic parsing.
2505   if (ResTy == MatchOperand_Success || ResTy == MatchOperand_ParseFail ||
2506       getLexer().is(AsmToken::EndOfStatement))
2507     return ResTy;
2508
2509   ResTy = parseRegOrImm(Operands);
2510
2511   if (ResTy == MatchOperand_Success)
2512     return ResTy;
2513
2514   if (getLexer().getKind() == AsmToken::Identifier) {
2515     // If this identifier is a symbol, we want to create an expression for it.
2516     // It is a little difficult to distinguish between a symbol name, and
2517     // an instruction flag like 'gds'.  In order to do this, we parse
2518     // all tokens as expressions and then treate the symbol name as the token
2519     // string when we want to interpret the operand as a token.
2520     const auto &Tok = Parser.getTok();
2521     SMLoc S = Tok.getLoc();
2522     const MCExpr *Expr = nullptr;
2523     if (!Parser.parseExpression(Expr)) {
2524       Operands.push_back(AMDGPUOperand::CreateExpr(this, Expr, S));
2525       return MatchOperand_Success;
2526     }
2527
2528     Operands.push_back(AMDGPUOperand::CreateToken(this, Tok.getString(), Tok.getLoc()));
2529     Parser.Lex();
2530     return MatchOperand_Success;
2531   }
2532   return MatchOperand_NoMatch;
2533 }
2534
2535 StringRef AMDGPUAsmParser::parseMnemonicSuffix(StringRef Name) {
2536   // Clear any forced encodings from the previous instruction.
2537   setForcedEncodingSize(0);
2538   setForcedDPP(false);
2539   setForcedSDWA(false);
2540
2541   if (Name.endswith("_e64")) {
2542     setForcedEncodingSize(64);
2543     return Name.substr(0, Name.size() - 4);
2544   } else if (Name.endswith("_e32")) {
2545     setForcedEncodingSize(32);
2546     return Name.substr(0, Name.size() - 4);
2547   } else if (Name.endswith("_dpp")) {
2548     setForcedDPP(true);
2549     return Name.substr(0, Name.size() - 4);
2550   } else if (Name.endswith("_sdwa")) {
2551     setForcedSDWA(true);
2552     return Name.substr(0, Name.size() - 5);
2553   }
2554   return Name;
2555 }
2556
2557 bool AMDGPUAsmParser::ParseInstruction(ParseInstructionInfo &Info,
2558                                        StringRef Name,
2559                                        SMLoc NameLoc, OperandVector &Operands) {
2560   // Add the instruction mnemonic
2561   Name = parseMnemonicSuffix(Name);
2562   Operands.push_back(AMDGPUOperand::CreateToken(this, Name, NameLoc));
2563
2564   while (!getLexer().is(AsmToken::EndOfStatement)) {
2565     OperandMatchResultTy Res = parseOperand(Operands, Name);
2566
2567     // Eat the comma or space if there is one.
2568     if (getLexer().is(AsmToken::Comma))
2569       Parser.Lex();
2570
2571     switch (Res) {
2572       case MatchOperand_Success: break;
2573       case MatchOperand_ParseFail:
2574         Error(getLexer().getLoc(), "failed parsing operand.");
2575         while (!getLexer().is(AsmToken::EndOfStatement)) {
2576           Parser.Lex();
2577         }
2578         return true;
2579       case MatchOperand_NoMatch:
2580         Error(getLexer().getLoc(), "not a valid operand.");
2581         while (!getLexer().is(AsmToken::EndOfStatement)) {
2582           Parser.Lex();
2583         }
2584         return true;
2585     }
2586   }
2587
2588   return false;
2589 }
2590
2591 //===----------------------------------------------------------------------===//
2592 // Utility functions
2593 //===----------------------------------------------------------------------===//
2594
2595 OperandMatchResultTy
2596 AMDGPUAsmParser::parseIntWithPrefix(const char *Prefix, int64_t &Int) {
2597   switch(getLexer().getKind()) {
2598     default: return MatchOperand_NoMatch;
2599     case AsmToken::Identifier: {
2600       StringRef Name = Parser.getTok().getString();
2601       if (!Name.equals(Prefix)) {
2602         return MatchOperand_NoMatch;
2603       }
2604
2605       Parser.Lex();
2606       if (getLexer().isNot(AsmToken::Colon))
2607         return MatchOperand_ParseFail;
2608
2609       Parser.Lex();
2610       if (getLexer().isNot(AsmToken::Integer))
2611         return MatchOperand_ParseFail;
2612
2613       if (getParser().parseAbsoluteExpression(Int))
2614         return MatchOperand_ParseFail;
2615       break;
2616     }
2617   }
2618   return MatchOperand_Success;
2619 }
2620
2621 OperandMatchResultTy
2622 AMDGPUAsmParser::parseIntWithPrefix(const char *Prefix, OperandVector &Operands,
2623                                     AMDGPUOperand::ImmTy ImmTy,
2624                                     bool (*ConvertResult)(int64_t&)) {
2625   SMLoc S = Parser.getTok().getLoc();
2626   int64_t Value = 0;
2627
2628   OperandMatchResultTy Res = parseIntWithPrefix(Prefix, Value);
2629   if (Res != MatchOperand_Success)
2630     return Res;
2631
2632   if (ConvertResult && !ConvertResult(Value)) {
2633     return MatchOperand_ParseFail;
2634   }
2635
2636   Operands.push_back(AMDGPUOperand::CreateImm(this, Value, S, ImmTy));
2637   return MatchOperand_Success;
2638 }
2639
2640 OperandMatchResultTy AMDGPUAsmParser::parseOperandArrayWithPrefix(
2641   const char *Prefix,
2642   OperandVector &Operands,
2643   AMDGPUOperand::ImmTy ImmTy,
2644   bool (*ConvertResult)(int64_t&)) {
2645   StringRef Name = Parser.getTok().getString();
2646   if (!Name.equals(Prefix))
2647     return MatchOperand_NoMatch;
2648
2649   Parser.Lex();
2650   if (getLexer().isNot(AsmToken::Colon))
2651     return MatchOperand_ParseFail;
2652
2653   Parser.Lex();
2654   if (getLexer().isNot(AsmToken::LBrac))
2655     return MatchOperand_ParseFail;
2656   Parser.Lex();
2657
2658   unsigned Val = 0;
2659   SMLoc S = Parser.getTok().getLoc();
2660
2661   // FIXME: How to verify the number of elements matches the number of src
2662   // operands?
2663   for (int I = 0; I < 3; ++I) {
2664     if (I != 0) {
2665       if (getLexer().is(AsmToken::RBrac))
2666         break;
2667
2668       if (getLexer().isNot(AsmToken::Comma))
2669         return MatchOperand_ParseFail;
2670       Parser.Lex();
2671     }
2672
2673     if (getLexer().isNot(AsmToken::Integer))
2674       return MatchOperand_ParseFail;
2675
2676     int64_t Op;
2677     if (getParser().parseAbsoluteExpression(Op))
2678       return MatchOperand_ParseFail;
2679
2680     if (Op != 0 && Op != 1)
2681       return MatchOperand_ParseFail;
2682     Val |= (Op << I);
2683   }
2684
2685   Parser.Lex();
2686   Operands.push_back(AMDGPUOperand::CreateImm(this, Val, S, ImmTy));
2687   return MatchOperand_Success;
2688 }
2689
2690 OperandMatchResultTy
2691 AMDGPUAsmParser::parseNamedBit(const char *Name, OperandVector &Operands,
2692                                AMDGPUOperand::ImmTy ImmTy) {
2693   int64_t Bit = 0;
2694   SMLoc S = Parser.getTok().getLoc();
2695
2696   // We are at the end of the statement, and this is a default argument, so
2697   // use a default value.
2698   if (getLexer().isNot(AsmToken::EndOfStatement)) {
2699     switch(getLexer().getKind()) {
2700       case AsmToken::Identifier: {
2701         StringRef Tok = Parser.getTok().getString();
2702         if (Tok == Name) {
2703           Bit = 1;
2704           Parser.Lex();
2705         } else if (Tok.startswith("no") && Tok.endswith(Name)) {
2706           Bit = 0;
2707           Parser.Lex();
2708         } else {
2709           return MatchOperand_NoMatch;
2710         }
2711         break;
2712       }
2713       default:
2714         return MatchOperand_NoMatch;
2715     }
2716   }
2717
2718   Operands.push_back(AMDGPUOperand::CreateImm(this, Bit, S, ImmTy));
2719   return MatchOperand_Success;
2720 }
2721
2722 static void addOptionalImmOperand(
2723   MCInst& Inst, const OperandVector& Operands,
2724   AMDGPUAsmParser::OptionalImmIndexMap& OptionalIdx,
2725   AMDGPUOperand::ImmTy ImmT,
2726   int64_t Default = 0) {
2727   auto i = OptionalIdx.find(ImmT);
2728   if (i != OptionalIdx.end()) {
2729     unsigned Idx = i->second;
2730     ((AMDGPUOperand &)*Operands[Idx]).addImmOperands(Inst, 1);
2731   } else {
2732     Inst.addOperand(MCOperand::createImm(Default));
2733   }
2734 }
2735
2736 OperandMatchResultTy
2737 AMDGPUAsmParser::parseStringWithPrefix(StringRef Prefix, StringRef &Value) {
2738   if (getLexer().isNot(AsmToken::Identifier)) {
2739     return MatchOperand_NoMatch;
2740   }
2741   StringRef Tok = Parser.getTok().getString();
2742   if (Tok != Prefix) {
2743     return MatchOperand_NoMatch;
2744   }
2745
2746   Parser.Lex();
2747   if (getLexer().isNot(AsmToken::Colon)) {
2748     return MatchOperand_ParseFail;
2749   }
2750
2751   Parser.Lex();
2752   if (getLexer().isNot(AsmToken::Identifier)) {
2753     return MatchOperand_ParseFail;
2754   }
2755
2756   Value = Parser.getTok().getString();
2757   return MatchOperand_Success;
2758 }
2759
2760 //===----------------------------------------------------------------------===//
2761 // ds
2762 //===----------------------------------------------------------------------===//
2763
2764 void AMDGPUAsmParser::cvtDSOffset01(MCInst &Inst,
2765                                     const OperandVector &Operands) {
2766   OptionalImmIndexMap OptionalIdx;
2767
2768   for (unsigned i = 1, e = Operands.size(); i != e; ++i) {
2769     AMDGPUOperand &Op = ((AMDGPUOperand &)*Operands[i]);
2770
2771     // Add the register arguments
2772     if (Op.isReg()) {
2773       Op.addRegOperands(Inst, 1);
2774       continue;
2775     }
2776
2777     // Handle optional arguments
2778     OptionalIdx[Op.getImmTy()] = i;
2779   }
2780
2781   addOptionalImmOperand(Inst, Operands, OptionalIdx, AMDGPUOperand::ImmTyOffset0);
2782   addOptionalImmOperand(Inst, Operands, OptionalIdx, AMDGPUOperand::ImmTyOffset1);
2783   addOptionalImmOperand(Inst, Operands, OptionalIdx, AMDGPUOperand::ImmTyGDS);
2784
2785   Inst.addOperand(MCOperand::createReg(AMDGPU::M0)); // m0
2786 }
2787
2788 void AMDGPUAsmParser::cvtDSImpl(MCInst &Inst, const OperandVector &Operands,
2789                                 bool IsGdsHardcoded) {
2790   OptionalImmIndexMap OptionalIdx;
2791
2792   for (unsigned i = 1, e = Operands.size(); i != e; ++i) {
2793     AMDGPUOperand &Op = ((AMDGPUOperand &)*Operands[i]);
2794
2795     // Add the register arguments
2796     if (Op.isReg()) {
2797       Op.addRegOperands(Inst, 1);
2798       continue;
2799     }
2800
2801     if (Op.isToken() && Op.getToken() == "gds") {
2802       IsGdsHardcoded = true;
2803       continue;
2804     }
2805
2806     // Handle optional arguments
2807     OptionalIdx[Op.getImmTy()] = i;
2808   }
2809
2810   AMDGPUOperand::ImmTy OffsetType =
2811     (Inst.getOpcode() == AMDGPU::DS_SWIZZLE_B32_si ||
2812      Inst.getOpcode() == AMDGPU::DS_SWIZZLE_B32_vi) ? AMDGPUOperand::ImmTySwizzle :
2813                                                       AMDGPUOperand::ImmTyOffset;
2814
2815   addOptionalImmOperand(Inst, Operands, OptionalIdx, OffsetType);
2816
2817   if (!IsGdsHardcoded) {
2818     addOptionalImmOperand(Inst, Operands, OptionalIdx, AMDGPUOperand::ImmTyGDS);
2819   }
2820   Inst.addOperand(MCOperand::createReg(AMDGPU::M0)); // m0
2821 }
2822
2823 void AMDGPUAsmParser::cvtExp(MCInst &Inst, const OperandVector &Operands) {
2824   OptionalImmIndexMap OptionalIdx;
2825
2826   unsigned OperandIdx[4];
2827   unsigned EnMask = 0;
2828   int SrcIdx = 0;
2829
2830   for (unsigned i = 1, e = Operands.size(); i != e; ++i) {
2831     AMDGPUOperand &Op = ((AMDGPUOperand &)*Operands[i]);
2832
2833     // Add the register arguments
2834     if (Op.isReg()) {
2835       assert(SrcIdx < 4);
2836       OperandIdx[SrcIdx] = Inst.size();
2837       Op.addRegOperands(Inst, 1);
2838       ++SrcIdx;
2839       continue;
2840     }
2841
2842     if (Op.isOff()) {
2843       assert(SrcIdx < 4);
2844       OperandIdx[SrcIdx] = Inst.size();
2845       Inst.addOperand(MCOperand::createReg(AMDGPU::NoRegister));
2846       ++SrcIdx;
2847       continue;
2848     }
2849
2850     if (Op.isImm() && Op.getImmTy() == AMDGPUOperand::ImmTyExpTgt) {
2851       Op.addImmOperands(Inst, 1);
2852       continue;
2853     }
2854
2855     if (Op.isToken() && Op.getToken() == "done")
2856       continue;
2857
2858     // Handle optional arguments
2859     OptionalIdx[Op.getImmTy()] = i;
2860   }
2861
2862   assert(SrcIdx == 4);
2863
2864   bool Compr = false;
2865   if (OptionalIdx.find(AMDGPUOperand::ImmTyExpCompr) != OptionalIdx.end()) {
2866     Compr = true;
2867     Inst.getOperand(OperandIdx[1]) = Inst.getOperand(OperandIdx[2]);
2868     Inst.getOperand(OperandIdx[2]).setReg(AMDGPU::NoRegister);
2869     Inst.getOperand(OperandIdx[3]).setReg(AMDGPU::NoRegister);
2870   }
2871
2872   for (auto i = 0; i < SrcIdx; ++i) {
2873     if (Inst.getOperand(OperandIdx[i]).getReg() != AMDGPU::NoRegister) {
2874       EnMask |= Compr? (0x3 << i * 2) : (0x1 << i);
2875     }
2876   }
2877
2878   addOptionalImmOperand(Inst, Operands, OptionalIdx, AMDGPUOperand::ImmTyExpVM);
2879   addOptionalImmOperand(Inst, Operands, OptionalIdx, AMDGPUOperand::ImmTyExpCompr);
2880
2881   Inst.addOperand(MCOperand::createImm(EnMask));
2882 }
2883
2884 //===----------------------------------------------------------------------===//
2885 // s_waitcnt
2886 //===----------------------------------------------------------------------===//
2887
2888 static bool
2889 encodeCnt(
2890   const AMDGPU::IsaInfo::IsaVersion ISA,
2891   int64_t &IntVal,
2892   int64_t CntVal,
2893   bool Saturate,
2894   unsigned (*encode)(const IsaInfo::IsaVersion &Version, unsigned, unsigned),
2895   unsigned (*decode)(const IsaInfo::IsaVersion &Version, unsigned))
2896 {
2897   bool Failed = false;
2898
2899   IntVal = encode(ISA, IntVal, CntVal);
2900   if (CntVal != decode(ISA, IntVal)) {
2901     if (Saturate) {
2902       IntVal = encode(ISA, IntVal, -1);
2903     } else {
2904       Failed = true;
2905     }
2906   }
2907   return Failed;
2908 }
2909
2910 bool AMDGPUAsmParser::parseCnt(int64_t &IntVal) {
2911   StringRef CntName = Parser.getTok().getString();
2912   int64_t CntVal;
2913
2914   Parser.Lex();
2915   if (getLexer().isNot(AsmToken::LParen))
2916     return true;
2917
2918   Parser.Lex();
2919   if (getLexer().isNot(AsmToken::Integer))
2920     return true;
2921
2922   if (getParser().parseAbsoluteExpression(CntVal))
2923     return true;
2924
2925   AMDGPU::IsaInfo::IsaVersion ISA =
2926       AMDGPU::IsaInfo::getIsaVersion(getFeatureBits());
2927
2928   bool Failed = true;
2929   bool Sat = CntName.endswith("_sat");
2930
2931   if (CntName == "vmcnt" || CntName == "vmcnt_sat") {
2932     Failed = encodeCnt(ISA, IntVal, CntVal, Sat, encodeVmcnt, decodeVmcnt);
2933   } else if (CntName == "expcnt" || CntName == "expcnt_sat") {
2934     Failed = encodeCnt(ISA, IntVal, CntVal, Sat, encodeExpcnt, decodeExpcnt);
2935   } else if (CntName == "lgkmcnt" || CntName == "lgkmcnt_sat") {
2936     Failed = encodeCnt(ISA, IntVal, CntVal, Sat, encodeLgkmcnt, decodeLgkmcnt);
2937   }
2938
2939   // To improve diagnostics, do not skip delimiters on errors
2940   if (!Failed) {
2941     if (getLexer().isNot(AsmToken::RParen)) {
2942       return true;
2943     }
2944     Parser.Lex();
2945     if (getLexer().is(AsmToken::Amp) || getLexer().is(AsmToken::Comma)) {
2946       const AsmToken NextToken = getLexer().peekTok();
2947       if (NextToken.is(AsmToken::Identifier)) {
2948         Parser.Lex();
2949       }
2950     }
2951   }
2952
2953   return Failed;
2954 }
2955
2956 OperandMatchResultTy
2957 AMDGPUAsmParser::parseSWaitCntOps(OperandVector &Operands) {
2958   AMDGPU::IsaInfo::IsaVersion ISA =
2959       AMDGPU::IsaInfo::getIsaVersion(getFeatureBits());
2960   int64_t Waitcnt = getWaitcntBitMask(ISA);
2961   SMLoc S = Parser.getTok().getLoc();
2962
2963   switch(getLexer().getKind()) {
2964     default: return MatchOperand_ParseFail;
2965     case AsmToken::Integer:
2966       // The operand can be an integer value.
2967       if (getParser().parseAbsoluteExpression(Waitcnt))
2968         return MatchOperand_ParseFail;
2969       break;
2970
2971     case AsmToken::Identifier:
2972       do {
2973         if (parseCnt(Waitcnt))
2974           return MatchOperand_ParseFail;
2975       } while(getLexer().isNot(AsmToken::EndOfStatement));
2976       break;
2977   }
2978   Operands.push_back(AMDGPUOperand::CreateImm(this, Waitcnt, S));
2979   return MatchOperand_Success;
2980 }
2981
2982 bool AMDGPUAsmParser::parseHwregConstruct(OperandInfoTy &HwReg, int64_t &Offset,
2983                                           int64_t &Width) {
2984   using namespace llvm::AMDGPU::Hwreg;
2985
2986   if (Parser.getTok().getString() != "hwreg")
2987     return true;
2988   Parser.Lex();
2989
2990   if (getLexer().isNot(AsmToken::LParen))
2991     return true;
2992   Parser.Lex();
2993
2994   if (getLexer().is(AsmToken::Identifier)) {
2995     HwReg.IsSymbolic = true;
2996     HwReg.Id = ID_UNKNOWN_;
2997     const StringRef tok = Parser.getTok().getString();
2998     for (int i = ID_SYMBOLIC_FIRST_; i < ID_SYMBOLIC_LAST_; ++i) {
2999       if (tok == IdSymbolic[i]) {
3000         HwReg.Id = i;
3001         break;
3002       }
3003     }
3004     Parser.Lex();
3005   } else {
3006     HwReg.IsSymbolic = false;
3007     if (getLexer().isNot(AsmToken::Integer))
3008       return true;
3009     if (getParser().parseAbsoluteExpression(HwReg.Id))
3010       return true;
3011   }
3012
3013   if (getLexer().is(AsmToken::RParen)) {
3014     Parser.Lex();
3015     return false;
3016   }
3017
3018   // optional params
3019   if (getLexer().isNot(AsmToken::Comma))
3020     return true;
3021   Parser.Lex();
3022
3023   if (getLexer().isNot(AsmToken::Integer))
3024     return true;
3025   if (getParser().parseAbsoluteExpression(Offset))
3026     return true;
3027
3028   if (getLexer().isNot(AsmToken::Comma))
3029     return true;
3030   Parser.Lex();
3031
3032   if (getLexer().isNot(AsmToken::Integer))
3033     return true;
3034   if (getParser().parseAbsoluteExpression(Width))
3035     return true;
3036
3037   if (getLexer().isNot(AsmToken::RParen))
3038     return true;
3039   Parser.Lex();
3040
3041   return false;
3042 }
3043
3044 OperandMatchResultTy AMDGPUAsmParser::parseHwreg(OperandVector &Operands) {
3045   using namespace llvm::AMDGPU::Hwreg;
3046
3047   int64_t Imm16Val = 0;
3048   SMLoc S = Parser.getTok().getLoc();
3049
3050   switch(getLexer().getKind()) {
3051     default: return MatchOperand_NoMatch;
3052     case AsmToken::Integer:
3053       // The operand can be an integer value.
3054       if (getParser().parseAbsoluteExpression(Imm16Val))
3055         return MatchOperand_NoMatch;
3056       if (Imm16Val < 0 || !isUInt<16>(Imm16Val)) {
3057         Error(S, "invalid immediate: only 16-bit values are legal");
3058         // Do not return error code, but create an imm operand anyway and proceed
3059         // to the next operand, if any. That avoids unneccessary error messages.
3060       }
3061       break;
3062
3063     case AsmToken::Identifier: {
3064         OperandInfoTy HwReg(ID_UNKNOWN_);
3065         int64_t Offset = OFFSET_DEFAULT_;
3066         int64_t Width = WIDTH_M1_DEFAULT_ + 1;
3067         if (parseHwregConstruct(HwReg, Offset, Width))
3068           return MatchOperand_ParseFail;
3069         if (HwReg.Id < 0 || !isUInt<ID_WIDTH_>(HwReg.Id)) {
3070           if (HwReg.IsSymbolic)
3071             Error(S, "invalid symbolic name of hardware register");
3072           else
3073             Error(S, "invalid code of hardware register: only 6-bit values are legal");
3074         }
3075         if (Offset < 0 || !isUInt<OFFSET_WIDTH_>(Offset))
3076           Error(S, "invalid bit offset: only 5-bit values are legal");
3077         if ((Width-1) < 0 || !isUInt<WIDTH_M1_WIDTH_>(Width-1))
3078           Error(S, "invalid bitfield width: only values from 1 to 32 are legal");
3079         Imm16Val = (HwReg.Id << ID_SHIFT_) | (Offset << OFFSET_SHIFT_) | ((Width-1) << WIDTH_M1_SHIFT_);
3080       }
3081       break;
3082   }
3083   Operands.push_back(AMDGPUOperand::CreateImm(this, Imm16Val, S, AMDGPUOperand::ImmTyHwreg));
3084   return MatchOperand_Success;
3085 }
3086
3087 bool AMDGPUOperand::isSWaitCnt() const {
3088   return isImm();
3089 }
3090
3091 bool AMDGPUOperand::isHwreg() const {
3092   return isImmTy(ImmTyHwreg);
3093 }
3094
3095 bool AMDGPUAsmParser::parseSendMsgConstruct(OperandInfoTy &Msg, OperandInfoTy &Operation, int64_t &StreamId) {
3096   using namespace llvm::AMDGPU::SendMsg;
3097
3098   if (Parser.getTok().getString() != "sendmsg")
3099     return true;
3100   Parser.Lex();
3101
3102   if (getLexer().isNot(AsmToken::LParen))
3103     return true;
3104   Parser.Lex();
3105
3106   if (getLexer().is(AsmToken::Identifier)) {
3107     Msg.IsSymbolic = true;
3108     Msg.Id = ID_UNKNOWN_;
3109     const std::string tok = Parser.getTok().getString();
3110     for (int i = ID_GAPS_FIRST_; i < ID_GAPS_LAST_; ++i) {
3111       switch(i) {
3112         default: continue; // Omit gaps.
3113         case ID_INTERRUPT: case ID_GS: case ID_GS_DONE:  case ID_SYSMSG: break;
3114       }
3115       if (tok == IdSymbolic[i]) {
3116         Msg.Id = i;
3117         break;
3118       }
3119     }
3120     Parser.Lex();
3121   } else {
3122     Msg.IsSymbolic = false;
3123     if (getLexer().isNot(AsmToken::Integer))
3124       return true;
3125     if (getParser().parseAbsoluteExpression(Msg.Id))
3126       return true;
3127     if (getLexer().is(AsmToken::Integer))
3128       if (getParser().parseAbsoluteExpression(Msg.Id))
3129         Msg.Id = ID_UNKNOWN_;
3130   }
3131   if (Msg.Id == ID_UNKNOWN_) // Don't know how to parse the rest.
3132     return false;
3133
3134   if (!(Msg.Id == ID_GS || Msg.Id == ID_GS_DONE || Msg.Id == ID_SYSMSG)) {
3135     if (getLexer().isNot(AsmToken::RParen))
3136       return true;
3137     Parser.Lex();
3138     return false;
3139   }
3140
3141   if (getLexer().isNot(AsmToken::Comma))
3142     return true;
3143   Parser.Lex();
3144
3145   assert(Msg.Id == ID_GS || Msg.Id == ID_GS_DONE || Msg.Id == ID_SYSMSG);
3146   Operation.Id = ID_UNKNOWN_;
3147   if (getLexer().is(AsmToken::Identifier)) {
3148     Operation.IsSymbolic = true;
3149     const char* const *S = (Msg.Id == ID_SYSMSG) ? OpSysSymbolic : OpGsSymbolic;
3150     const int F = (Msg.Id == ID_SYSMSG) ? OP_SYS_FIRST_ : OP_GS_FIRST_;
3151     const int L = (Msg.Id == ID_SYSMSG) ? OP_SYS_LAST_ : OP_GS_LAST_;
3152     const StringRef Tok = Parser.getTok().getString();
3153     for (int i = F; i < L; ++i) {
3154       if (Tok == S[i]) {
3155         Operation.Id = i;
3156         break;
3157       }
3158     }
3159     Parser.Lex();
3160   } else {
3161     Operation.IsSymbolic = false;
3162     if (getLexer().isNot(AsmToken::Integer))
3163       return true;
3164     if (getParser().parseAbsoluteExpression(Operation.Id))
3165       return true;
3166   }
3167
3168   if ((Msg.Id == ID_GS || Msg.Id == ID_GS_DONE) && Operation.Id != OP_GS_NOP) {
3169     // Stream id is optional.
3170     if (getLexer().is(AsmToken::RParen)) {
3171       Parser.Lex();
3172       return false;
3173     }
3174
3175     if (getLexer().isNot(AsmToken::Comma))
3176       return true;
3177     Parser.Lex();
3178
3179     if (getLexer().isNot(AsmToken::Integer))
3180       return true;
3181     if (getParser().parseAbsoluteExpression(StreamId))
3182       return true;
3183   }
3184
3185   if (getLexer().isNot(AsmToken::RParen))
3186     return true;
3187   Parser.Lex();
3188   return false;
3189 }
3190
3191 OperandMatchResultTy AMDGPUAsmParser::parseInterpSlot(OperandVector &Operands) {
3192   if (getLexer().getKind() != AsmToken::Identifier)
3193     return MatchOperand_NoMatch;
3194
3195   StringRef Str = Parser.getTok().getString();
3196   int Slot = StringSwitch<int>(Str)
3197     .Case("p10", 0)
3198     .Case("p20", 1)
3199     .Case("p0", 2)
3200     .Default(-1);
3201
3202   SMLoc S = Parser.getTok().getLoc();
3203   if (Slot == -1)
3204     return MatchOperand_ParseFail;
3205
3206   Parser.Lex();
3207   Operands.push_back(AMDGPUOperand::CreateImm(this, Slot, S,
3208                                               AMDGPUOperand::ImmTyInterpSlot));
3209   return MatchOperand_Success;
3210 }
3211
3212 OperandMatchResultTy AMDGPUAsmParser::parseInterpAttr(OperandVector &Operands) {
3213   if (getLexer().getKind() != AsmToken::Identifier)
3214     return MatchOperand_NoMatch;
3215
3216   StringRef Str = Parser.getTok().getString();
3217   if (!Str.startswith("attr"))
3218     return MatchOperand_NoMatch;
3219
3220   StringRef Chan = Str.take_back(2);
3221   int AttrChan = StringSwitch<int>(Chan)
3222     .Case(".x", 0)
3223     .Case(".y", 1)
3224     .Case(".z", 2)
3225     .Case(".w", 3)
3226     .Default(-1);
3227   if (AttrChan == -1)
3228     return MatchOperand_ParseFail;
3229
3230   Str = Str.drop_back(2).drop_front(4);
3231
3232   uint8_t Attr;
3233   if (Str.getAsInteger(10, Attr))
3234     return MatchOperand_ParseFail;
3235
3236   SMLoc S = Parser.getTok().getLoc();
3237   Parser.Lex();
3238   if (Attr > 63) {
3239     Error(S, "out of bounds attr");
3240     return MatchOperand_Success;
3241   }
3242
3243   SMLoc SChan = SMLoc::getFromPointer(Chan.data());
3244
3245   Operands.push_back(AMDGPUOperand::CreateImm(this, Attr, S,
3246                                               AMDGPUOperand::ImmTyInterpAttr));
3247   Operands.push_back(AMDGPUOperand::CreateImm(this, AttrChan, SChan,
3248                                               AMDGPUOperand::ImmTyAttrChan));
3249   return MatchOperand_Success;
3250 }
3251
3252 void AMDGPUAsmParser::errorExpTgt() {
3253   Error(Parser.getTok().getLoc(), "invalid exp target");
3254 }
3255
3256 OperandMatchResultTy AMDGPUAsmParser::parseExpTgtImpl(StringRef Str,
3257                                                       uint8_t &Val) {
3258   if (Str == "null") {
3259     Val = 9;
3260     return MatchOperand_Success;
3261   }
3262
3263   if (Str.startswith("mrt")) {
3264     Str = Str.drop_front(3);
3265     if (Str == "z") { // == mrtz
3266       Val = 8;
3267       return MatchOperand_Success;
3268     }
3269
3270     if (Str.getAsInteger(10, Val))
3271       return MatchOperand_ParseFail;
3272
3273     if (Val > 7)
3274       errorExpTgt();
3275
3276     return MatchOperand_Success;
3277   }
3278
3279   if (Str.startswith("pos")) {
3280     Str = Str.drop_front(3);
3281     if (Str.getAsInteger(10, Val))
3282       return MatchOperand_ParseFail;
3283
3284     if (Val > 3)
3285       errorExpTgt();
3286
3287     Val += 12;
3288     return MatchOperand_Success;
3289   }
3290
3291   if (Str.startswith("param")) {
3292     Str = Str.drop_front(5);
3293     if (Str.getAsInteger(10, Val))
3294       return MatchOperand_ParseFail;
3295
3296     if (Val >= 32)
3297       errorExpTgt();
3298
3299     Val += 32;
3300     return MatchOperand_Success;
3301   }
3302
3303   if (Str.startswith("invalid_target_")) {
3304     Str = Str.drop_front(15);
3305     if (Str.getAsInteger(10, Val))
3306       return MatchOperand_ParseFail;
3307
3308     errorExpTgt();
3309     return MatchOperand_Success;
3310   }
3311
3312   return MatchOperand_NoMatch;
3313 }
3314
3315 OperandMatchResultTy AMDGPUAsmParser::parseExpTgt(OperandVector &Operands) {
3316   uint8_t Val;
3317   StringRef Str = Parser.getTok().getString();
3318
3319   auto Res = parseExpTgtImpl(Str, Val);
3320   if (Res != MatchOperand_Success)
3321     return Res;
3322
3323   SMLoc S = Parser.getTok().getLoc();
3324   Parser.Lex();
3325
3326   Operands.push_back(AMDGPUOperand::CreateImm(this, Val, S,
3327                                               AMDGPUOperand::ImmTyExpTgt));
3328   return MatchOperand_Success;
3329 }
3330
3331 OperandMatchResultTy
3332 AMDGPUAsmParser::parseSendMsgOp(OperandVector &Operands) {
3333   using namespace llvm::AMDGPU::SendMsg;
3334
3335   int64_t Imm16Val = 0;
3336   SMLoc S = Parser.getTok().getLoc();
3337
3338   switch(getLexer().getKind()) {
3339   default:
3340     return MatchOperand_NoMatch;
3341   case AsmToken::Integer:
3342     // The operand can be an integer value.
3343     if (getParser().parseAbsoluteExpression(Imm16Val))
3344       return MatchOperand_NoMatch;
3345     if (Imm16Val < 0 || !isUInt<16>(Imm16Val)) {
3346       Error(S, "invalid immediate: only 16-bit values are legal");
3347       // Do not return error code, but create an imm operand anyway and proceed
3348       // to the next operand, if any. That avoids unneccessary error messages.
3349     }
3350     break;
3351   case AsmToken::Identifier: {
3352       OperandInfoTy Msg(ID_UNKNOWN_);
3353       OperandInfoTy Operation(OP_UNKNOWN_);
3354       int64_t StreamId = STREAM_ID_DEFAULT_;
3355       if (parseSendMsgConstruct(Msg, Operation, StreamId))
3356         return MatchOperand_ParseFail;
3357       do {
3358         // Validate and encode message ID.
3359         if (! ((ID_INTERRUPT <= Msg.Id && Msg.Id <= ID_GS_DONE)
3360                 || Msg.Id == ID_SYSMSG)) {
3361           if (Msg.IsSymbolic)
3362             Error(S, "invalid/unsupported symbolic name of message");
3363           else
3364             Error(S, "invalid/unsupported code of message");
3365           break;
3366         }
3367         Imm16Val = (Msg.Id << ID_SHIFT_);
3368         // Validate and encode operation ID.
3369         if (Msg.Id == ID_GS || Msg.Id == ID_GS_DONE) {
3370           if (! (OP_GS_FIRST_ <= Operation.Id && Operation.Id < OP_GS_LAST_)) {
3371             if (Operation.IsSymbolic)
3372               Error(S, "invalid symbolic name of GS_OP");
3373             else
3374               Error(S, "invalid code of GS_OP: only 2-bit values are legal");
3375             break;
3376           }
3377           if (Operation.Id == OP_GS_NOP
3378               && Msg.Id != ID_GS_DONE) {
3379             Error(S, "invalid GS_OP: NOP is for GS_DONE only");
3380             break;
3381           }
3382           Imm16Val |= (Operation.Id << OP_SHIFT_);
3383         }
3384         if (Msg.Id == ID_SYSMSG) {
3385           if (! (OP_SYS_FIRST_ <= Operation.Id && Operation.Id < OP_SYS_LAST_)) {
3386             if (Operation.IsSymbolic)
3387               Error(S, "invalid/unsupported symbolic name of SYSMSG_OP");
3388             else
3389               Error(S, "invalid/unsupported code of SYSMSG_OP");
3390             break;
3391           }
3392           Imm16Val |= (Operation.Id << OP_SHIFT_);
3393         }
3394         // Validate and encode stream ID.
3395         if ((Msg.Id == ID_GS || Msg.Id == ID_GS_DONE) && Operation.Id != OP_GS_NOP) {
3396           if (! (STREAM_ID_FIRST_ <= StreamId && StreamId < STREAM_ID_LAST_)) {
3397             Error(S, "invalid stream id: only 2-bit values are legal");
3398             break;
3399           }
3400           Imm16Val |= (StreamId << STREAM_ID_SHIFT_);
3401         }
3402       } while (false);
3403     }
3404     break;
3405   }
3406   Operands.push_back(AMDGPUOperand::CreateImm(this, Imm16Val, S, AMDGPUOperand::ImmTySendMsg));
3407   return MatchOperand_Success;
3408 }
3409
3410 bool AMDGPUOperand::isSendMsg() const {
3411   return isImmTy(ImmTySendMsg);
3412 }
3413
3414 //===----------------------------------------------------------------------===//
3415 // parser helpers
3416 //===----------------------------------------------------------------------===//
3417
3418 bool
3419 AMDGPUAsmParser::trySkipId(const StringRef Id) {
3420   if (getLexer().getKind() == AsmToken::Identifier &&
3421       Parser.getTok().getString() == Id) {
3422     Parser.Lex();
3423     return true;
3424   }
3425   return false;
3426 }
3427
3428 bool
3429 AMDGPUAsmParser::trySkipToken(const AsmToken::TokenKind Kind) {
3430   if (getLexer().getKind() == Kind) {
3431     Parser.Lex();
3432     return true;
3433   }
3434   return false;
3435 }
3436
3437 bool
3438 AMDGPUAsmParser::skipToken(const AsmToken::TokenKind Kind,
3439                            const StringRef ErrMsg) {
3440   if (!trySkipToken(Kind)) {
3441     Error(Parser.getTok().getLoc(), ErrMsg);
3442     return false;
3443   }
3444   return true;
3445 }
3446
3447 bool
3448 AMDGPUAsmParser::parseExpr(int64_t &Imm) {
3449   return !getParser().parseAbsoluteExpression(Imm);
3450 }
3451
3452 bool
3453 AMDGPUAsmParser::parseString(StringRef &Val, const StringRef ErrMsg) {
3454   SMLoc S = Parser.getTok().getLoc();
3455   if (getLexer().getKind() == AsmToken::String) {
3456     Val = Parser.getTok().getStringContents();
3457     Parser.Lex();
3458     return true;
3459   } else {
3460     Error(S, ErrMsg);
3461     return false;
3462   }
3463 }
3464
3465 //===----------------------------------------------------------------------===//
3466 // swizzle
3467 //===----------------------------------------------------------------------===//
3468
3469 LLVM_READNONE
3470 static unsigned
3471 encodeBitmaskPerm(const unsigned AndMask,
3472                   const unsigned OrMask,
3473                   const unsigned XorMask) {
3474   using namespace llvm::AMDGPU::Swizzle;
3475
3476   return BITMASK_PERM_ENC |
3477          (AndMask << BITMASK_AND_SHIFT) |
3478          (OrMask  << BITMASK_OR_SHIFT)  |
3479          (XorMask << BITMASK_XOR_SHIFT);
3480 }
3481
3482 bool
3483 AMDGPUAsmParser::parseSwizzleOperands(const unsigned OpNum, int64_t* Op,
3484                                       const unsigned MinVal,
3485                                       const unsigned MaxVal,
3486                                       const StringRef ErrMsg) {
3487   for (unsigned i = 0; i < OpNum; ++i) {
3488     if (!skipToken(AsmToken::Comma, "expected a comma")){
3489       return false;
3490     }
3491     SMLoc ExprLoc = Parser.getTok().getLoc();
3492     if (!parseExpr(Op[i])) {
3493       return false;
3494     }
3495     if (Op[i] < MinVal || Op[i] > MaxVal) {
3496       Error(ExprLoc, ErrMsg);
3497       return false;
3498     }
3499   }
3500
3501   return true;
3502 }
3503
3504 bool
3505 AMDGPUAsmParser::parseSwizzleQuadPerm(int64_t &Imm) {
3506   using namespace llvm::AMDGPU::Swizzle;
3507
3508   int64_t Lane[LANE_NUM];
3509   if (parseSwizzleOperands(LANE_NUM, Lane, 0, LANE_MAX,
3510                            "expected a 2-bit lane id")) {
3511     Imm = QUAD_PERM_ENC;
3512     for (auto i = 0; i < LANE_NUM; ++i) {
3513       Imm |= Lane[i] << (LANE_SHIFT * i);
3514     }
3515     return true;
3516   }
3517   return false;
3518 }
3519
3520 bool
3521 AMDGPUAsmParser::parseSwizzleBroadcast(int64_t &Imm) {
3522   using namespace llvm::AMDGPU::Swizzle;
3523
3524   SMLoc S = Parser.getTok().getLoc();
3525   int64_t GroupSize;
3526   int64_t LaneIdx;
3527
3528   if (!parseSwizzleOperands(1, &GroupSize,
3529                             2, 32,
3530                             "group size must be in the interval [2,32]")) {
3531     return false;
3532   }
3533   if (!isPowerOf2_64(GroupSize)) {
3534     Error(S, "group size must be a power of two");
3535     return false;
3536   }
3537   if (parseSwizzleOperands(1, &LaneIdx,
3538                            0, GroupSize - 1,
3539                            "lane id must be in the interval [0,group size - 1]")) {
3540     Imm = encodeBitmaskPerm(BITMASK_MAX - GroupSize + 1, LaneIdx, 0);
3541     return true;
3542   }
3543   return false;
3544 }
3545
3546 bool
3547 AMDGPUAsmParser::parseSwizzleReverse(int64_t &Imm) {
3548   using namespace llvm::AMDGPU::Swizzle;
3549
3550   SMLoc S = Parser.getTok().getLoc();
3551   int64_t GroupSize;
3552
3553   if (!parseSwizzleOperands(1, &GroupSize,
3554       2, 32, "group size must be in the interval [2,32]")) {
3555     return false;
3556   }
3557   if (!isPowerOf2_64(GroupSize)) {
3558     Error(S, "group size must be a power of two");
3559     return false;
3560   }
3561
3562   Imm = encodeBitmaskPerm(BITMASK_MAX, 0, GroupSize - 1);
3563   return true;
3564 }
3565
3566 bool
3567 AMDGPUAsmParser::parseSwizzleSwap(int64_t &Imm) {
3568   using namespace llvm::AMDGPU::Swizzle;
3569
3570   SMLoc S = Parser.getTok().getLoc();
3571   int64_t GroupSize;
3572
3573   if (!parseSwizzleOperands(1, &GroupSize,
3574       1, 16, "group size must be in the interval [1,16]")) {
3575     return false;
3576   }
3577   if (!isPowerOf2_64(GroupSize)) {
3578     Error(S, "group size must be a power of two");
3579     return false;
3580   }
3581
3582   Imm = encodeBitmaskPerm(BITMASK_MAX, 0, GroupSize);
3583   return true;
3584 }
3585
3586 bool
3587 AMDGPUAsmParser::parseSwizzleBitmaskPerm(int64_t &Imm) {
3588   using namespace llvm::AMDGPU::Swizzle;
3589
3590   if (!skipToken(AsmToken::Comma, "expected a comma")) {
3591     return false;
3592   }
3593
3594   StringRef Ctl;
3595   SMLoc StrLoc = Parser.getTok().getLoc();
3596   if (!parseString(Ctl)) {
3597     return false;
3598   }
3599   if (Ctl.size() != BITMASK_WIDTH) {
3600     Error(StrLoc, "expected a 5-character mask");
3601     return false;
3602   }
3603
3604   unsigned AndMask = 0;
3605   unsigned OrMask = 0;
3606   unsigned XorMask = 0;
3607
3608   for (size_t i = 0; i < Ctl.size(); ++i) {
3609     unsigned Mask = 1 << (BITMASK_WIDTH - 1 - i);
3610     switch(Ctl[i]) {
3611     default:
3612       Error(StrLoc, "invalid mask");
3613       return false;
3614     case '0':
3615       break;
3616     case '1':
3617       OrMask |= Mask;
3618       break;
3619     case 'p':
3620       AndMask |= Mask;
3621       break;
3622     case 'i':
3623       AndMask |= Mask;
3624       XorMask |= Mask;
3625       break;
3626     }
3627   }
3628
3629   Imm = encodeBitmaskPerm(AndMask, OrMask, XorMask);
3630   return true;
3631 }
3632
3633 bool
3634 AMDGPUAsmParser::parseSwizzleOffset(int64_t &Imm) {
3635
3636   SMLoc OffsetLoc = Parser.getTok().getLoc();
3637
3638   if (!parseExpr(Imm)) {
3639     return false;
3640   }
3641   if (!isUInt<16>(Imm)) {
3642     Error(OffsetLoc, "expected a 16-bit offset");
3643     return false;
3644   }
3645   return true;
3646 }
3647
3648 bool
3649 AMDGPUAsmParser::parseSwizzleMacro(int64_t &Imm) {
3650   using namespace llvm::AMDGPU::Swizzle;
3651
3652   if (skipToken(AsmToken::LParen, "expected a left parentheses")) {
3653
3654     SMLoc ModeLoc = Parser.getTok().getLoc();
3655     bool Ok = false;
3656
3657     if (trySkipId(IdSymbolic[ID_QUAD_PERM])) {
3658       Ok = parseSwizzleQuadPerm(Imm);
3659     } else if (trySkipId(IdSymbolic[ID_BITMASK_PERM])) {
3660       Ok = parseSwizzleBitmaskPerm(Imm);
3661     } else if (trySkipId(IdSymbolic[ID_BROADCAST])) {
3662       Ok = parseSwizzleBroadcast(Imm);
3663     } else if (trySkipId(IdSymbolic[ID_SWAP])) {
3664       Ok = parseSwizzleSwap(Imm);
3665     } else if (trySkipId(IdSymbolic[ID_REVERSE])) {
3666       Ok = parseSwizzleReverse(Imm);
3667     } else {
3668       Error(ModeLoc, "expected a swizzle mode");
3669     }
3670
3671     return Ok && skipToken(AsmToken::RParen, "expected a closing parentheses");
3672   }
3673
3674   return false;
3675 }
3676
3677 OperandMatchResultTy
3678 AMDGPUAsmParser::parseSwizzleOp(OperandVector &Operands) {
3679   SMLoc S = Parser.getTok().getLoc();
3680   int64_t Imm = 0;
3681
3682   if (trySkipId("offset")) {
3683
3684     bool Ok = false;
3685     if (skipToken(AsmToken::Colon, "expected a colon")) {
3686       if (trySkipId("swizzle")) {
3687         Ok = parseSwizzleMacro(Imm);
3688       } else {
3689         Ok = parseSwizzleOffset(Imm);
3690       }
3691     }
3692
3693     Operands.push_back(AMDGPUOperand::CreateImm(this, Imm, S, AMDGPUOperand::ImmTySwizzle));
3694
3695     return Ok? MatchOperand_Success : MatchOperand_ParseFail;
3696   } else {
3697     return MatchOperand_NoMatch;
3698   }
3699 }
3700
3701 bool
3702 AMDGPUOperand::isSwizzle() const {
3703   return isImmTy(ImmTySwizzle);
3704 }
3705
3706 //===----------------------------------------------------------------------===//
3707 // sopp branch targets
3708 //===----------------------------------------------------------------------===//
3709
3710 OperandMatchResultTy
3711 AMDGPUAsmParser::parseSOppBrTarget(OperandVector &Operands) {
3712   SMLoc S = Parser.getTok().getLoc();
3713
3714   switch (getLexer().getKind()) {
3715     default: return MatchOperand_ParseFail;
3716     case AsmToken::Integer: {
3717       int64_t Imm;
3718       if (getParser().parseAbsoluteExpression(Imm))
3719         return MatchOperand_ParseFail;
3720       Operands.push_back(AMDGPUOperand::CreateImm(this, Imm, S));
3721       return MatchOperand_Success;
3722     }
3723
3724     case AsmToken::Identifier:
3725       Operands.push_back(AMDGPUOperand::CreateExpr(this,
3726           MCSymbolRefExpr::create(getContext().getOrCreateSymbol(
3727                                   Parser.getTok().getString()), getContext()), S));
3728       Parser.Lex();
3729       return MatchOperand_Success;
3730   }
3731 }
3732
3733 //===----------------------------------------------------------------------===//
3734 // mubuf
3735 //===----------------------------------------------------------------------===//
3736
3737 AMDGPUOperand::Ptr AMDGPUAsmParser::defaultGLC() const {
3738   return AMDGPUOperand::CreateImm(this, 0, SMLoc(), AMDGPUOperand::ImmTyGLC);
3739 }
3740
3741 AMDGPUOperand::Ptr AMDGPUAsmParser::defaultSLC() const {
3742   return AMDGPUOperand::CreateImm(this, 0, SMLoc(), AMDGPUOperand::ImmTySLC);
3743 }
3744
3745 AMDGPUOperand::Ptr AMDGPUAsmParser::defaultTFE() const {
3746   return AMDGPUOperand::CreateImm(this, 0, SMLoc(), AMDGPUOperand::ImmTyTFE);
3747 }
3748
3749 void AMDGPUAsmParser::cvtMubufImpl(MCInst &Inst,
3750                                const OperandVector &Operands,
3751                                bool IsAtomic, bool IsAtomicReturn) {
3752   OptionalImmIndexMap OptionalIdx;
3753   assert(IsAtomicReturn ? IsAtomic : true);
3754
3755   for (unsigned i = 1, e = Operands.size(); i != e; ++i) {
3756     AMDGPUOperand &Op = ((AMDGPUOperand &)*Operands[i]);
3757
3758     // Add the register arguments
3759     if (Op.isReg()) {
3760       Op.addRegOperands(Inst, 1);
3761       continue;
3762     }
3763
3764     // Handle the case where soffset is an immediate
3765     if (Op.isImm() && Op.getImmTy() == AMDGPUOperand::ImmTyNone) {
3766       Op.addImmOperands(Inst, 1);
3767       continue;
3768     }
3769
3770     // Handle tokens like 'offen' which are sometimes hard-coded into the
3771     // asm string.  There are no MCInst operands for these.
3772     if (Op.isToken()) {
3773       continue;
3774     }
3775     assert(Op.isImm());
3776
3777     // Handle optional arguments
3778     OptionalIdx[Op.getImmTy()] = i;
3779   }
3780
3781   // Copy $vdata_in operand and insert as $vdata for MUBUF_Atomic RTN insns.
3782   if (IsAtomicReturn) {
3783     MCInst::iterator I = Inst.begin(); // $vdata_in is always at the beginning.
3784     Inst.insert(I, *I);
3785   }
3786
3787   addOptionalImmOperand(Inst, Operands, OptionalIdx, AMDGPUOperand::ImmTyOffset);
3788   if (!IsAtomic) { // glc is hard-coded.
3789     addOptionalImmOperand(Inst, Operands, OptionalIdx, AMDGPUOperand::ImmTyGLC);
3790   }
3791   addOptionalImmOperand(Inst, Operands, OptionalIdx, AMDGPUOperand::ImmTySLC);
3792   addOptionalImmOperand(Inst, Operands, OptionalIdx, AMDGPUOperand::ImmTyTFE);
3793 }
3794
3795 //===----------------------------------------------------------------------===//
3796 // mimg
3797 //===----------------------------------------------------------------------===//
3798
3799 void AMDGPUAsmParser::cvtMIMG(MCInst &Inst, const OperandVector &Operands) {
3800   unsigned I = 1;
3801   const MCInstrDesc &Desc = MII.get(Inst.getOpcode());
3802   for (unsigned J = 0; J < Desc.getNumDefs(); ++J) {
3803     ((AMDGPUOperand &)*Operands[I++]).addRegOperands(Inst, 1);
3804   }
3805
3806   OptionalImmIndexMap OptionalIdx;
3807
3808   for (unsigned E = Operands.size(); I != E; ++I) {
3809     AMDGPUOperand &Op = ((AMDGPUOperand &)*Operands[I]);
3810
3811     // Add the register arguments
3812     if (Op.isRegOrImm()) {
3813       Op.addRegOrImmOperands(Inst, 1);
3814       continue;
3815     } else if (Op.isImmModifier()) {
3816       OptionalIdx[Op.getImmTy()] = I;
3817     } else {
3818       llvm_unreachable("unexpected operand type");
3819     }
3820   }
3821
3822   addOptionalImmOperand(Inst, Operands, OptionalIdx, AMDGPUOperand::ImmTyDMask);
3823   addOptionalImmOperand(Inst, Operands, OptionalIdx, AMDGPUOperand::ImmTyUNorm);
3824   addOptionalImmOperand(Inst, Operands, OptionalIdx, AMDGPUOperand::ImmTyGLC);
3825   addOptionalImmOperand(Inst, Operands, OptionalIdx, AMDGPUOperand::ImmTyDA);
3826   addOptionalImmOperand(Inst, Operands, OptionalIdx, AMDGPUOperand::ImmTyR128);
3827   addOptionalImmOperand(Inst, Operands, OptionalIdx, AMDGPUOperand::ImmTyTFE);
3828   addOptionalImmOperand(Inst, Operands, OptionalIdx, AMDGPUOperand::ImmTyLWE);
3829   addOptionalImmOperand(Inst, Operands, OptionalIdx, AMDGPUOperand::ImmTySLC);
3830 }
3831
3832 void AMDGPUAsmParser::cvtMIMGAtomic(MCInst &Inst, const OperandVector &Operands) {
3833   unsigned I = 1;
3834   const MCInstrDesc &Desc = MII.get(Inst.getOpcode());
3835   for (unsigned J = 0; J < Desc.getNumDefs(); ++J) {
3836     ((AMDGPUOperand &)*Operands[I++]).addRegOperands(Inst, 1);
3837   }
3838
3839   // Add src, same as dst
3840   ((AMDGPUOperand &)*Operands[I]).addRegOperands(Inst, 1);
3841
3842   OptionalImmIndexMap OptionalIdx;
3843
3844   for (unsigned E = Operands.size(); I != E; ++I) {
3845     AMDGPUOperand &Op = ((AMDGPUOperand &)*Operands[I]);
3846
3847     // Add the register arguments
3848     if (Op.isRegOrImm()) {
3849       Op.addRegOrImmOperands(Inst, 1);
3850       continue;
3851     } else if (Op.isImmModifier()) {
3852       OptionalIdx[Op.getImmTy()] = I;
3853     } else {
3854       llvm_unreachable("unexpected operand type");
3855     }
3856   }
3857
3858   addOptionalImmOperand(Inst, Operands, OptionalIdx, AMDGPUOperand::ImmTyDMask);
3859   addOptionalImmOperand(Inst, Operands, OptionalIdx, AMDGPUOperand::ImmTyUNorm);
3860   addOptionalImmOperand(Inst, Operands, OptionalIdx, AMDGPUOperand::ImmTyGLC);
3861   addOptionalImmOperand(Inst, Operands, OptionalIdx, AMDGPUOperand::ImmTyDA);
3862   addOptionalImmOperand(Inst, Operands, OptionalIdx, AMDGPUOperand::ImmTyR128);
3863   addOptionalImmOperand(Inst, Operands, OptionalIdx, AMDGPUOperand::ImmTyTFE);
3864   addOptionalImmOperand(Inst, Operands, OptionalIdx, AMDGPUOperand::ImmTyLWE);
3865   addOptionalImmOperand(Inst, Operands, OptionalIdx, AMDGPUOperand::ImmTySLC);
3866 }
3867
3868 AMDGPUOperand::Ptr AMDGPUAsmParser::defaultDMask() const {
3869   return AMDGPUOperand::CreateImm(this, 0, SMLoc(), AMDGPUOperand::ImmTyDMask);
3870 }
3871
3872 AMDGPUOperand::Ptr AMDGPUAsmParser::defaultUNorm() const {
3873   return AMDGPUOperand::CreateImm(this, 0, SMLoc(), AMDGPUOperand::ImmTyUNorm);
3874 }
3875
3876 AMDGPUOperand::Ptr AMDGPUAsmParser::defaultDA() const {
3877   return AMDGPUOperand::CreateImm(this, 0, SMLoc(), AMDGPUOperand::ImmTyDA);
3878 }
3879
3880 AMDGPUOperand::Ptr AMDGPUAsmParser::defaultR128() const {
3881   return AMDGPUOperand::CreateImm(this, 0, SMLoc(), AMDGPUOperand::ImmTyR128);
3882 }
3883
3884 AMDGPUOperand::Ptr AMDGPUAsmParser::defaultLWE() const {
3885   return AMDGPUOperand::CreateImm(this, 0, SMLoc(), AMDGPUOperand::ImmTyLWE);
3886 }
3887
3888 //===----------------------------------------------------------------------===//
3889 // smrd
3890 //===----------------------------------------------------------------------===//
3891
3892 bool AMDGPUOperand::isSMRDOffset8() const {
3893   return isImm() && isUInt<8>(getImm());
3894 }
3895
3896 bool AMDGPUOperand::isSMRDOffset20() const {
3897   return isImm() && isUInt<20>(getImm());
3898 }
3899
3900 bool AMDGPUOperand::isSMRDLiteralOffset() const {
3901   // 32-bit literals are only supported on CI and we only want to use them
3902   // when the offset is > 8-bits.
3903   return isImm() && !isUInt<8>(getImm()) && isUInt<32>(getImm());
3904 }
3905
3906 AMDGPUOperand::Ptr AMDGPUAsmParser::defaultSMRDOffset8() const {
3907   return AMDGPUOperand::CreateImm(this, 0, SMLoc(), AMDGPUOperand::ImmTyOffset);
3908 }
3909
3910 AMDGPUOperand::Ptr AMDGPUAsmParser::defaultSMRDOffset20() const {
3911   return AMDGPUOperand::CreateImm(this, 0, SMLoc(), AMDGPUOperand::ImmTyOffset);
3912 }
3913
3914 AMDGPUOperand::Ptr AMDGPUAsmParser::defaultSMRDLiteralOffset() const {
3915   return AMDGPUOperand::CreateImm(this, 0, SMLoc(), AMDGPUOperand::ImmTyOffset);
3916 }
3917
3918 //===----------------------------------------------------------------------===//
3919 // vop3
3920 //===----------------------------------------------------------------------===//
3921
3922 static bool ConvertOmodMul(int64_t &Mul) {
3923   if (Mul != 1 && Mul != 2 && Mul != 4)
3924     return false;
3925
3926   Mul >>= 1;
3927   return true;
3928 }
3929
3930 static bool ConvertOmodDiv(int64_t &Div) {
3931   if (Div == 1) {
3932     Div = 0;
3933     return true;
3934   }
3935
3936   if (Div == 2) {
3937     Div = 3;
3938     return true;
3939   }
3940
3941   return false;
3942 }
3943
3944 static bool ConvertBoundCtrl(int64_t &BoundCtrl) {
3945   if (BoundCtrl == 0) {
3946     BoundCtrl = 1;
3947     return true;
3948   }
3949
3950   if (BoundCtrl == -1) {
3951     BoundCtrl = 0;
3952     return true;
3953   }
3954
3955   return false;
3956 }
3957
3958 // Note: the order in this table matches the order of operands in AsmString.
3959 static const OptionalOperand AMDGPUOptionalOperandTable[] = {
3960   {"offen",   AMDGPUOperand::ImmTyOffen, true, nullptr},
3961   {"idxen",   AMDGPUOperand::ImmTyIdxen, true, nullptr},
3962   {"addr64",  AMDGPUOperand::ImmTyAddr64, true, nullptr},
3963   {"offset0", AMDGPUOperand::ImmTyOffset0, false, nullptr},
3964   {"offset1", AMDGPUOperand::ImmTyOffset1, false, nullptr},
3965   {"gds",     AMDGPUOperand::ImmTyGDS, true, nullptr},
3966   {"offset",  AMDGPUOperand::ImmTyOffset, false, nullptr},
3967   {"glc",     AMDGPUOperand::ImmTyGLC, true, nullptr},
3968   {"slc",     AMDGPUOperand::ImmTySLC, true, nullptr},
3969   {"tfe",     AMDGPUOperand::ImmTyTFE, true, nullptr},
3970   {"clamp",   AMDGPUOperand::ImmTyClampSI, true, nullptr},
3971   {"omod",    AMDGPUOperand::ImmTyOModSI, false, ConvertOmodMul},
3972   {"unorm",   AMDGPUOperand::ImmTyUNorm, true, nullptr},
3973   {"da",      AMDGPUOperand::ImmTyDA,    true, nullptr},
3974   {"r128",    AMDGPUOperand::ImmTyR128,  true, nullptr},
3975   {"lwe",     AMDGPUOperand::ImmTyLWE,   true, nullptr},
3976   {"dmask",   AMDGPUOperand::ImmTyDMask, false, nullptr},
3977   {"row_mask",   AMDGPUOperand::ImmTyDppRowMask, false, nullptr},
3978   {"bank_mask",  AMDGPUOperand::ImmTyDppBankMask, false, nullptr},
3979   {"bound_ctrl", AMDGPUOperand::ImmTyDppBoundCtrl, false, ConvertBoundCtrl},
3980   {"dst_sel",    AMDGPUOperand::ImmTySdwaDstSel, false, nullptr},
3981   {"src0_sel",   AMDGPUOperand::ImmTySdwaSrc0Sel, false, nullptr},
3982   {"src1_sel",   AMDGPUOperand::ImmTySdwaSrc1Sel, false, nullptr},
3983   {"dst_unused", AMDGPUOperand::ImmTySdwaDstUnused, false, nullptr},
3984   {"compr", AMDGPUOperand::ImmTyExpCompr, true, nullptr },
3985   {"vm", AMDGPUOperand::ImmTyExpVM, true, nullptr},
3986   {"op_sel", AMDGPUOperand::ImmTyOpSel, false, nullptr},
3987   {"op_sel_hi", AMDGPUOperand::ImmTyOpSelHi, false, nullptr},
3988   {"neg_lo", AMDGPUOperand::ImmTyNegLo, false, nullptr},
3989   {"neg_hi", AMDGPUOperand::ImmTyNegHi, false, nullptr}
3990 };
3991
3992 OperandMatchResultTy AMDGPUAsmParser::parseOptionalOperand(OperandVector &Operands) {
3993   OperandMatchResultTy res;
3994   for (const OptionalOperand &Op : AMDGPUOptionalOperandTable) {
3995     // try to parse any optional operand here
3996     if (Op.IsBit) {
3997       res = parseNamedBit(Op.Name, Operands, Op.Type);
3998     } else if (Op.Type == AMDGPUOperand::ImmTyOModSI) {
3999       res = parseOModOperand(Operands);
4000     } else if (Op.Type == AMDGPUOperand::ImmTySdwaDstSel ||
4001                Op.Type == AMDGPUOperand::ImmTySdwaSrc0Sel ||
4002                Op.Type == AMDGPUOperand::ImmTySdwaSrc1Sel) {
4003       res = parseSDWASel(Operands, Op.Name, Op.Type);
4004     } else if (Op.Type == AMDGPUOperand::ImmTySdwaDstUnused) {
4005       res = parseSDWADstUnused(Operands);
4006     } else if (Op.Type == AMDGPUOperand::ImmTyOpSel ||
4007                Op.Type == AMDGPUOperand::ImmTyOpSelHi ||
4008                Op.Type == AMDGPUOperand::ImmTyNegLo ||
4009                Op.Type == AMDGPUOperand::ImmTyNegHi) {
4010       res = parseOperandArrayWithPrefix(Op.Name, Operands, Op.Type,
4011                                         Op.ConvertResult);
4012     } else {
4013       res = parseIntWithPrefix(Op.Name, Operands, Op.Type, Op.ConvertResult);
4014     }
4015     if (res != MatchOperand_NoMatch) {
4016       return res;
4017     }
4018   }
4019   return MatchOperand_NoMatch;
4020 }
4021
4022 OperandMatchResultTy AMDGPUAsmParser::parseOModOperand(OperandVector &Operands) {
4023   StringRef Name = Parser.getTok().getString();
4024   if (Name == "mul") {
4025     return parseIntWithPrefix("mul", Operands,
4026                               AMDGPUOperand::ImmTyOModSI, ConvertOmodMul);
4027   }
4028
4029   if (Name == "div") {
4030     return parseIntWithPrefix("div", Operands,
4031                               AMDGPUOperand::ImmTyOModSI, ConvertOmodDiv);
4032   }
4033
4034   return MatchOperand_NoMatch;
4035 }
4036
4037 void AMDGPUAsmParser::cvtId(MCInst &Inst, const OperandVector &Operands) {
4038   unsigned I = 1;
4039   const MCInstrDesc &Desc = MII.get(Inst.getOpcode());
4040   for (unsigned J = 0; J < Desc.getNumDefs(); ++J) {
4041     ((AMDGPUOperand &)*Operands[I++]).addRegOperands(Inst, 1);
4042   }
4043   for (unsigned E = Operands.size(); I != E; ++I)
4044     ((AMDGPUOperand &)*Operands[I]).addRegOrImmOperands(Inst, 1);
4045 }
4046
4047 void AMDGPUAsmParser::cvtVOP3_2_mod(MCInst &Inst, const OperandVector &Operands) {
4048   uint64_t TSFlags = MII.get(Inst.getOpcode()).TSFlags;
4049   if (TSFlags & SIInstrFlags::VOP3) {
4050     cvtVOP3(Inst, Operands);
4051   } else {
4052     cvtId(Inst, Operands);
4053   }
4054 }
4055
4056 static bool isRegOrImmWithInputMods(const MCInstrDesc &Desc, unsigned OpNum) {
4057       // 1. This operand is input modifiers
4058   return Desc.OpInfo[OpNum].OperandType == AMDGPU::OPERAND_INPUT_MODS
4059       // 2. This is not last operand
4060       && Desc.NumOperands > (OpNum + 1)
4061       // 3. Next operand is register class
4062       && Desc.OpInfo[OpNum + 1].RegClass != -1
4063       // 4. Next register is not tied to any other operand
4064       && Desc.getOperandConstraint(OpNum + 1, MCOI::OperandConstraint::TIED_TO) == -1;
4065 }
4066
4067 void AMDGPUAsmParser::cvtVOP3Impl(MCInst &Inst, const OperandVector &Operands,
4068                                   OptionalImmIndexMap &OptionalIdx) {
4069   unsigned I = 1;
4070   const MCInstrDesc &Desc = MII.get(Inst.getOpcode());
4071   for (unsigned J = 0; J < Desc.getNumDefs(); ++J) {
4072     ((AMDGPUOperand &)*Operands[I++]).addRegOperands(Inst, 1);
4073   }
4074
4075   for (unsigned E = Operands.size(); I != E; ++I) {
4076     AMDGPUOperand &Op = ((AMDGPUOperand &)*Operands[I]);
4077     if (isRegOrImmWithInputMods(Desc, Inst.getNumOperands())) {
4078       Op.addRegOrImmWithFPInputModsOperands(Inst, 2);
4079     } else if (Op.isImmModifier()) {
4080       OptionalIdx[Op.getImmTy()] = I;
4081     } else if (Op.isRegOrImm()) {
4082       Op.addRegOrImmOperands(Inst, 1);
4083     } else {
4084       llvm_unreachable("unhandled operand type");
4085     }
4086   }
4087 }
4088
4089 void AMDGPUAsmParser::cvtVOP3(MCInst &Inst, const OperandVector &Operands) {
4090   OptionalImmIndexMap OptionalIdx;
4091
4092   cvtVOP3Impl(Inst, Operands, OptionalIdx);
4093
4094   addOptionalImmOperand(Inst, Operands, OptionalIdx, AMDGPUOperand::ImmTyClampSI);
4095   addOptionalImmOperand(Inst, Operands, OptionalIdx, AMDGPUOperand::ImmTyOModSI);
4096
4097   // special case v_mac_{f16, f32}:
4098   // it has src2 register operand that is tied to dst operand
4099   // we don't allow modifiers for this operand in assembler so src2_modifiers
4100   // should be 0
4101   if (Inst.getOpcode() == AMDGPU::V_MAC_F32_e64_si ||
4102       Inst.getOpcode() == AMDGPU::V_MAC_F32_e64_vi ||
4103       Inst.getOpcode() == AMDGPU::V_MAC_F16_e64_vi) {
4104     auto it = Inst.begin();
4105     std::advance(
4106       it,
4107       AMDGPU::getNamedOperandIdx(Inst.getOpcode() == AMDGPU::V_MAC_F16_e64_vi ?
4108                                      AMDGPU::V_MAC_F16_e64 :
4109                                      AMDGPU::V_MAC_F32_e64,
4110                                  AMDGPU::OpName::src2_modifiers));
4111     it = Inst.insert(it, MCOperand::createImm(0)); // no modifiers for src2
4112     ++it;
4113     Inst.insert(it, Inst.getOperand(0)); // src2 = dst
4114   }
4115 }
4116
4117 void AMDGPUAsmParser::cvtVOP3OMod(MCInst &Inst, const OperandVector &Operands) {
4118   OptionalImmIndexMap OptionalIdx;
4119
4120   unsigned I = 1;
4121   const MCInstrDesc &Desc = MII.get(Inst.getOpcode());
4122   for (unsigned J = 0; J < Desc.getNumDefs(); ++J) {
4123     ((AMDGPUOperand &)*Operands[I++]).addRegOperands(Inst, 1);
4124   }
4125
4126   for (unsigned E = Operands.size(); I != E; ++I) {
4127     AMDGPUOperand &Op = ((AMDGPUOperand &)*Operands[I]);
4128     if (Op.isMod()) {
4129       OptionalIdx[Op.getImmTy()] = I;
4130     } else {
4131       Op.addRegOrImmOperands(Inst, 1);
4132     }
4133   }
4134
4135   addOptionalImmOperand(Inst, Operands, OptionalIdx, AMDGPUOperand::ImmTyClampSI);
4136   addOptionalImmOperand(Inst, Operands, OptionalIdx, AMDGPUOperand::ImmTyOModSI);
4137 }
4138
4139 void AMDGPUAsmParser::cvtVOP3P(MCInst &Inst, const OperandVector &Operands) {
4140   OptionalImmIndexMap OptIdx;
4141
4142   cvtVOP3Impl(Inst, Operands, OptIdx);
4143
4144   // FIXME: This is messy. Parse the modifiers as if it was a normal VOP3
4145   // instruction, and then figure out where to actually put the modifiers
4146   int Opc = Inst.getOpcode();
4147
4148   if (AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::clamp) != -1) {
4149     addOptionalImmOperand(Inst, Operands, OptIdx, AMDGPUOperand::ImmTyClampSI);
4150   }
4151
4152   addOptionalImmOperand(Inst, Operands, OptIdx, AMDGPUOperand::ImmTyOpSel);
4153   addOptionalImmOperand(Inst, Operands, OptIdx, AMDGPUOperand::ImmTyOpSelHi, -1);
4154
4155   int NegLoIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::neg_lo);
4156   if (NegLoIdx != -1) {
4157     addOptionalImmOperand(Inst, Operands, OptIdx, AMDGPUOperand::ImmTyNegLo);
4158     addOptionalImmOperand(Inst, Operands, OptIdx, AMDGPUOperand::ImmTyNegHi);
4159   }
4160
4161   const int Ops[] = { AMDGPU::OpName::src0,
4162                       AMDGPU::OpName::src1,
4163                       AMDGPU::OpName::src2 };
4164   const int ModOps[] = { AMDGPU::OpName::src0_modifiers,
4165                          AMDGPU::OpName::src1_modifiers,
4166                          AMDGPU::OpName::src2_modifiers };
4167
4168   int OpSelIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::op_sel);
4169   int OpSelHiIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::op_sel_hi);
4170
4171   unsigned OpSel = Inst.getOperand(OpSelIdx).getImm();
4172   unsigned OpSelHi = Inst.getOperand(OpSelHiIdx).getImm();
4173   unsigned NegLo = 0;
4174   unsigned NegHi = 0;
4175
4176   if (NegLoIdx != -1) {
4177     int NegHiIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::neg_hi);
4178     NegLo = Inst.getOperand(NegLoIdx).getImm();
4179     NegHi = Inst.getOperand(NegHiIdx).getImm();
4180   }
4181
4182   for (int J = 0; J < 3; ++J) {
4183     int OpIdx = AMDGPU::getNamedOperandIdx(Opc, Ops[J]);
4184     if (OpIdx == -1)
4185       break;
4186
4187     uint32_t ModVal = 0;
4188
4189     if ((OpSel & (1 << J)) != 0)
4190       ModVal |= SISrcMods::OP_SEL_0;
4191
4192     if ((OpSelHi & (1 << J)) != 0)
4193       ModVal |= SISrcMods::OP_SEL_1;
4194
4195     if ((NegLo & (1 << J)) != 0)
4196       ModVal |= SISrcMods::NEG;
4197
4198     if ((NegHi & (1 << J)) != 0)
4199       ModVal |= SISrcMods::NEG_HI;
4200
4201     int ModIdx = AMDGPU::getNamedOperandIdx(Opc, ModOps[J]);
4202
4203     Inst.getOperand(ModIdx).setImm(ModVal);
4204   }
4205 }
4206
4207 //===----------------------------------------------------------------------===//
4208 // dpp
4209 //===----------------------------------------------------------------------===//
4210
4211 bool AMDGPUOperand::isDPPCtrl() const {
4212   bool result = isImm() && getImmTy() == ImmTyDppCtrl && isUInt<9>(getImm());
4213   if (result) {
4214     int64_t Imm = getImm();
4215     return ((Imm >= 0x000) && (Imm <= 0x0ff)) ||
4216            ((Imm >= 0x101) && (Imm <= 0x10f)) ||
4217            ((Imm >= 0x111) && (Imm <= 0x11f)) ||
4218            ((Imm >= 0x121) && (Imm <= 0x12f)) ||
4219            (Imm == 0x130) ||
4220            (Imm == 0x134) ||
4221            (Imm == 0x138) ||
4222            (Imm == 0x13c) ||
4223            (Imm == 0x140) ||
4224            (Imm == 0x141) ||
4225            (Imm == 0x142) ||
4226            (Imm == 0x143);
4227   }
4228   return false;
4229 }
4230
4231 bool AMDGPUOperand::isGPRIdxMode() const {
4232   return isImm() && isUInt<4>(getImm());
4233 }
4234
4235 bool AMDGPUOperand::isS16Imm() const {
4236   return isImm() && (isInt<16>(getImm()) || isUInt<16>(getImm()));
4237 }
4238
4239 bool AMDGPUOperand::isU16Imm() const {
4240   return isImm() && isUInt<16>(getImm());
4241 }
4242
4243 OperandMatchResultTy
4244 AMDGPUAsmParser::parseDPPCtrl(OperandVector &Operands) {
4245   SMLoc S = Parser.getTok().getLoc();
4246   StringRef Prefix;
4247   int64_t Int;
4248
4249   if (getLexer().getKind() == AsmToken::Identifier) {
4250     Prefix = Parser.getTok().getString();
4251   } else {
4252     return MatchOperand_NoMatch;
4253   }
4254
4255   if (Prefix == "row_mirror") {
4256     Int = 0x140;
4257     Parser.Lex();
4258   } else if (Prefix == "row_half_mirror") {
4259     Int = 0x141;
4260     Parser.Lex();
4261   } else {
4262     // Check to prevent parseDPPCtrlOps from eating invalid tokens
4263     if (Prefix != "quad_perm"
4264         && Prefix != "row_shl"
4265         && Prefix != "row_shr"
4266         && Prefix != "row_ror"
4267         && Prefix != "wave_shl"
4268         && Prefix != "wave_rol"
4269         && Prefix != "wave_shr"
4270         && Prefix != "wave_ror"
4271         && Prefix != "row_bcast") {
4272       return MatchOperand_NoMatch;
4273     }
4274
4275     Parser.Lex();
4276     if (getLexer().isNot(AsmToken::Colon))
4277       return MatchOperand_ParseFail;
4278
4279     if (Prefix == "quad_perm") {
4280       // quad_perm:[%d,%d,%d,%d]
4281       Parser.Lex();
4282       if (getLexer().isNot(AsmToken::LBrac))
4283         return MatchOperand_ParseFail;
4284       Parser.Lex();
4285
4286       if (getParser().parseAbsoluteExpression(Int) || !(0 <= Int && Int <=3))
4287         return MatchOperand_ParseFail;
4288
4289       for (int i = 0; i < 3; ++i) {
4290         if (getLexer().isNot(AsmToken::Comma))
4291           return MatchOperand_ParseFail;
4292         Parser.Lex();
4293
4294         int64_t Temp;
4295         if (getParser().parseAbsoluteExpression(Temp) || !(0 <= Temp && Temp <=3))
4296           return MatchOperand_ParseFail;
4297         const int shift = i*2 + 2;
4298         Int += (Temp << shift);
4299       }
4300
4301       if (getLexer().isNot(AsmToken::RBrac))
4302         return MatchOperand_ParseFail;
4303       Parser.Lex();
4304
4305     } else {
4306       // sel:%d
4307       Parser.Lex();
4308       if (getParser().parseAbsoluteExpression(Int))
4309         return MatchOperand_ParseFail;
4310
4311       if (Prefix == "row_shl" && 1 <= Int && Int <= 15) {
4312         Int |= 0x100;
4313       } else if (Prefix == "row_shr" && 1 <= Int && Int <= 15) {
4314         Int |= 0x110;
4315       } else if (Prefix == "row_ror" && 1 <= Int && Int <= 15) {
4316         Int |= 0x120;
4317       } else if (Prefix == "wave_shl" && 1 == Int) {
4318         Int = 0x130;
4319       } else if (Prefix == "wave_rol" && 1 == Int) {
4320         Int = 0x134;
4321       } else if (Prefix == "wave_shr" && 1 == Int) {
4322         Int = 0x138;
4323       } else if (Prefix == "wave_ror" && 1 == Int) {
4324         Int = 0x13C;
4325       } else if (Prefix == "row_bcast") {
4326         if (Int == 15) {
4327           Int = 0x142;
4328         } else if (Int == 31) {
4329           Int = 0x143;
4330         } else {
4331           return MatchOperand_ParseFail;
4332         }
4333       } else {
4334         return MatchOperand_ParseFail;
4335       }
4336     }
4337   }
4338
4339   Operands.push_back(AMDGPUOperand::CreateImm(this, Int, S, AMDGPUOperand::ImmTyDppCtrl));
4340   return MatchOperand_Success;
4341 }
4342
4343 AMDGPUOperand::Ptr AMDGPUAsmParser::defaultRowMask() const {
4344   return AMDGPUOperand::CreateImm(this, 0xf, SMLoc(), AMDGPUOperand::ImmTyDppRowMask);
4345 }
4346
4347 AMDGPUOperand::Ptr AMDGPUAsmParser::defaultBankMask() const {
4348   return AMDGPUOperand::CreateImm(this, 0xf, SMLoc(), AMDGPUOperand::ImmTyDppBankMask);
4349 }
4350
4351 AMDGPUOperand::Ptr AMDGPUAsmParser::defaultBoundCtrl() const {
4352   return AMDGPUOperand::CreateImm(this, 0, SMLoc(), AMDGPUOperand::ImmTyDppBoundCtrl);
4353 }
4354
4355 void AMDGPUAsmParser::cvtDPP(MCInst &Inst, const OperandVector &Operands) {
4356   OptionalImmIndexMap OptionalIdx;
4357
4358   unsigned I = 1;
4359   const MCInstrDesc &Desc = MII.get(Inst.getOpcode());
4360   for (unsigned J = 0; J < Desc.getNumDefs(); ++J) {
4361     ((AMDGPUOperand &)*Operands[I++]).addRegOperands(Inst, 1);
4362   }
4363
4364   for (unsigned E = Operands.size(); I != E; ++I) {
4365     AMDGPUOperand &Op = ((AMDGPUOperand &)*Operands[I]);
4366     // Add the register arguments
4367     if (Op.isReg() && Op.Reg.RegNo == AMDGPU::VCC) {
4368       // VOP2b (v_add_u32, v_sub_u32 ...) dpp use "vcc" token.
4369       // Skip it.
4370       continue;
4371     } if (isRegOrImmWithInputMods(Desc, Inst.getNumOperands())) {
4372       Op.addRegWithFPInputModsOperands(Inst, 2);
4373     } else if (Op.isDPPCtrl()) {
4374       Op.addImmOperands(Inst, 1);
4375     } else if (Op.isImm()) {
4376       // Handle optional arguments
4377       OptionalIdx[Op.getImmTy()] = I;
4378     } else {
4379       llvm_unreachable("Invalid operand type");
4380     }
4381   }
4382
4383   addOptionalImmOperand(Inst, Operands, OptionalIdx, AMDGPUOperand::ImmTyDppRowMask, 0xf);
4384   addOptionalImmOperand(Inst, Operands, OptionalIdx, AMDGPUOperand::ImmTyDppBankMask, 0xf);
4385   addOptionalImmOperand(Inst, Operands, OptionalIdx, AMDGPUOperand::ImmTyDppBoundCtrl);
4386
4387   // special case v_mac_{f16, f32}:
4388   // it has src2 register operand that is tied to dst operand
4389   if (Inst.getOpcode() == AMDGPU::V_MAC_F32_dpp ||
4390       Inst.getOpcode() == AMDGPU::V_MAC_F16_dpp) {
4391     auto it = Inst.begin();
4392     std::advance(
4393         it, AMDGPU::getNamedOperandIdx(Inst.getOpcode(), AMDGPU::OpName::src2));
4394     Inst.insert(it, Inst.getOperand(0)); // src2 = dst
4395   }
4396 }
4397
4398 //===----------------------------------------------------------------------===//
4399 // sdwa
4400 //===----------------------------------------------------------------------===//
4401
4402 OperandMatchResultTy
4403 AMDGPUAsmParser::parseSDWASel(OperandVector &Operands, StringRef Prefix,
4404                               AMDGPUOperand::ImmTy Type) {
4405   using namespace llvm::AMDGPU::SDWA;
4406
4407   SMLoc S = Parser.getTok().getLoc();
4408   StringRef Value;
4409   OperandMatchResultTy res;
4410
4411   res = parseStringWithPrefix(Prefix, Value);
4412   if (res != MatchOperand_Success) {
4413     return res;
4414   }
4415
4416   int64_t Int;
4417   Int = StringSwitch<int64_t>(Value)
4418         .Case("BYTE_0", SdwaSel::BYTE_0)
4419         .Case("BYTE_1", SdwaSel::BYTE_1)
4420         .Case("BYTE_2", SdwaSel::BYTE_2)
4421         .Case("BYTE_3", SdwaSel::BYTE_3)
4422         .Case("WORD_0", SdwaSel::WORD_0)
4423         .Case("WORD_1", SdwaSel::WORD_1)
4424         .Case("DWORD", SdwaSel::DWORD)
4425         .Default(0xffffffff);
4426   Parser.Lex(); // eat last token
4427
4428   if (Int == 0xffffffff) {
4429     return MatchOperand_ParseFail;
4430   }
4431
4432   Operands.push_back(AMDGPUOperand::CreateImm(this, Int, S, Type));
4433   return MatchOperand_Success;
4434 }
4435
4436 OperandMatchResultTy
4437 AMDGPUAsmParser::parseSDWADstUnused(OperandVector &Operands) {
4438   using namespace llvm::AMDGPU::SDWA;
4439
4440   SMLoc S = Parser.getTok().getLoc();
4441   StringRef Value;
4442   OperandMatchResultTy res;
4443
4444   res = parseStringWithPrefix("dst_unused", Value);
4445   if (res != MatchOperand_Success) {
4446     return res;
4447   }
4448
4449   int64_t Int;
4450   Int = StringSwitch<int64_t>(Value)
4451         .Case("UNUSED_PAD", DstUnused::UNUSED_PAD)
4452         .Case("UNUSED_SEXT", DstUnused::UNUSED_SEXT)
4453         .Case("UNUSED_PRESERVE", DstUnused::UNUSED_PRESERVE)
4454         .Default(0xffffffff);
4455   Parser.Lex(); // eat last token
4456
4457   if (Int == 0xffffffff) {
4458     return MatchOperand_ParseFail;
4459   }
4460
4461   Operands.push_back(AMDGPUOperand::CreateImm(this, Int, S, AMDGPUOperand::ImmTySdwaDstUnused));
4462   return MatchOperand_Success;
4463 }
4464
4465 void AMDGPUAsmParser::cvtSdwaVOP1(MCInst &Inst, const OperandVector &Operands) {
4466   cvtSDWA(Inst, Operands, SIInstrFlags::VOP1);
4467 }
4468
4469 void AMDGPUAsmParser::cvtSdwaVOP2(MCInst &Inst, const OperandVector &Operands) {
4470   cvtSDWA(Inst, Operands, SIInstrFlags::VOP2);
4471 }
4472
4473 void AMDGPUAsmParser::cvtSdwaVOP2b(MCInst &Inst, const OperandVector &Operands) {
4474   cvtSDWA(Inst, Operands, SIInstrFlags::VOP2, true);
4475 }
4476
4477 void AMDGPUAsmParser::cvtSdwaVOPC(MCInst &Inst, const OperandVector &Operands) {
4478   cvtSDWA(Inst, Operands, SIInstrFlags::VOPC, isVI());
4479 }
4480
4481 void AMDGPUAsmParser::cvtSDWA(MCInst &Inst, const OperandVector &Operands,
4482                               uint64_t BasicInstType, bool skipVcc) {
4483   using namespace llvm::AMDGPU::SDWA;
4484   OptionalImmIndexMap OptionalIdx;
4485   bool skippedVcc = false;
4486
4487   unsigned I = 1;
4488   const MCInstrDesc &Desc = MII.get(Inst.getOpcode());
4489   for (unsigned J = 0; J < Desc.getNumDefs(); ++J) {
4490     ((AMDGPUOperand &)*Operands[I++]).addRegOperands(Inst, 1);
4491   }
4492
4493   for (unsigned E = Operands.size(); I != E; ++I) {
4494     AMDGPUOperand &Op = ((AMDGPUOperand &)*Operands[I]);
4495     if (skipVcc && !skippedVcc && Op.isReg() && Op.Reg.RegNo == AMDGPU::VCC) {
4496       // VOP2b (v_add_u32, v_sub_u32 ...) sdwa use "vcc" token as dst.
4497       // Skip it if it's 2nd (e.g. v_add_i32_sdwa v1, vcc, v2, v3)
4498       // or 4th (v_addc_u32_sdwa v1, vcc, v2, v3, vcc) operand.
4499       // Skip VCC only if we didn't skip it on previous iteration.
4500       if (BasicInstType == SIInstrFlags::VOP2 &&
4501           (Inst.getNumOperands() == 1 || Inst.getNumOperands() == 5)) {
4502         skippedVcc = true;
4503         continue;
4504       } else if (BasicInstType == SIInstrFlags::VOPC &&
4505                  Inst.getNumOperands() == 0) {
4506         skippedVcc = true;
4507         continue;
4508       }
4509     }
4510     if (isRegOrImmWithInputMods(Desc, Inst.getNumOperands())) {
4511       Op.addRegWithInputModsOperands(Inst, 2);
4512     } else if (Op.isImm()) {
4513       // Handle optional arguments
4514       OptionalIdx[Op.getImmTy()] = I;
4515     } else {
4516       llvm_unreachable("Invalid operand type");
4517     }
4518     skippedVcc = false;
4519   }
4520
4521   if (Inst.getOpcode() != AMDGPU::V_NOP_sdwa_gfx9 &&
4522       Inst.getOpcode() != AMDGPU::V_NOP_sdwa_vi) {
4523     // V_NOP_sdwa_vi has no optional sdwa arguments
4524     switch (BasicInstType) {
4525     case SIInstrFlags::VOP1:
4526       addOptionalImmOperand(Inst, Operands, OptionalIdx, AMDGPUOperand::ImmTyClampSI, 0);
4527       if (isGFX9() &&
4528           AMDGPU::getNamedOperandIdx(Inst.getOpcode(), AMDGPU::OpName::omod) != -1) {
4529         addOptionalImmOperand(Inst, Operands, OptionalIdx, AMDGPUOperand::ImmTyOModSI, 0);
4530       }
4531       addOptionalImmOperand(Inst, Operands, OptionalIdx, AMDGPUOperand::ImmTySdwaDstSel, SdwaSel::DWORD);
4532       addOptionalImmOperand(Inst, Operands, OptionalIdx, AMDGPUOperand::ImmTySdwaDstUnused, DstUnused::UNUSED_PRESERVE);
4533       addOptionalImmOperand(Inst, Operands, OptionalIdx, AMDGPUOperand::ImmTySdwaSrc0Sel, SdwaSel::DWORD);
4534       break;
4535
4536     case SIInstrFlags::VOP2:
4537       addOptionalImmOperand(Inst, Operands, OptionalIdx, AMDGPUOperand::ImmTyClampSI, 0);
4538       if (isGFX9() &&
4539           AMDGPU::getNamedOperandIdx(Inst.getOpcode(), AMDGPU::OpName::omod) != -1) {
4540         addOptionalImmOperand(Inst, Operands, OptionalIdx, AMDGPUOperand::ImmTyOModSI, 0);
4541       }
4542       addOptionalImmOperand(Inst, Operands, OptionalIdx, AMDGPUOperand::ImmTySdwaDstSel, SdwaSel::DWORD);
4543       addOptionalImmOperand(Inst, Operands, OptionalIdx, AMDGPUOperand::ImmTySdwaDstUnused, DstUnused::UNUSED_PRESERVE);
4544       addOptionalImmOperand(Inst, Operands, OptionalIdx, AMDGPUOperand::ImmTySdwaSrc0Sel, SdwaSel::DWORD);
4545       addOptionalImmOperand(Inst, Operands, OptionalIdx, AMDGPUOperand::ImmTySdwaSrc1Sel, SdwaSel::DWORD);
4546       break;
4547
4548     case SIInstrFlags::VOPC:
4549       if (isVI()) {
4550         addOptionalImmOperand(Inst, Operands, OptionalIdx, AMDGPUOperand::ImmTyClampSI, 0);
4551       }
4552       addOptionalImmOperand(Inst, Operands, OptionalIdx, AMDGPUOperand::ImmTySdwaSrc0Sel, SdwaSel::DWORD);
4553       addOptionalImmOperand(Inst, Operands, OptionalIdx, AMDGPUOperand::ImmTySdwaSrc1Sel, SdwaSel::DWORD);
4554       break;
4555
4556     default:
4557       llvm_unreachable("Invalid instruction type. Only VOP1, VOP2 and VOPC allowed");
4558     }
4559   }
4560
4561   // special case v_mac_{f16, f32}:
4562   // it has src2 register operand that is tied to dst operand
4563   if (Inst.getOpcode() == AMDGPU::V_MAC_F32_sdwa_vi ||
4564       Inst.getOpcode() == AMDGPU::V_MAC_F16_sdwa_vi)  {
4565     auto it = Inst.begin();
4566     std::advance(
4567       it, AMDGPU::getNamedOperandIdx(Inst.getOpcode(), AMDGPU::OpName::src2));
4568     Inst.insert(it, Inst.getOperand(0)); // src2 = dst
4569   }
4570 }
4571
4572 /// Force static initialization.
4573 extern "C" void LLVMInitializeAMDGPUAsmParser() {
4574   RegisterMCAsmParser<AMDGPUAsmParser> A(getTheAMDGPUTarget());
4575   RegisterMCAsmParser<AMDGPUAsmParser> B(getTheGCNTarget());
4576 }
4577
4578 #define GET_REGISTER_MATCHER
4579 #define GET_MATCHER_IMPLEMENTATION
4580 #include "AMDGPUGenAsmMatcher.inc"
4581
4582 // This fuction should be defined after auto-generated include so that we have
4583 // MatchClassKind enum defined
4584 unsigned AMDGPUAsmParser::validateTargetOperandClass(MCParsedAsmOperand &Op,
4585                                                      unsigned Kind) {
4586   // Tokens like "glc" would be parsed as immediate operands in ParseOperand().
4587   // But MatchInstructionImpl() expects to meet token and fails to validate
4588   // operand. This method checks if we are given immediate operand but expect to
4589   // get corresponding token.
4590   AMDGPUOperand &Operand = (AMDGPUOperand&)Op;
4591   switch (Kind) {
4592   case MCK_addr64:
4593     return Operand.isAddr64() ? Match_Success : Match_InvalidOperand;
4594   case MCK_gds:
4595     return Operand.isGDS() ? Match_Success : Match_InvalidOperand;
4596   case MCK_glc:
4597     return Operand.isGLC() ? Match_Success : Match_InvalidOperand;
4598   case MCK_idxen:
4599     return Operand.isIdxen() ? Match_Success : Match_InvalidOperand;
4600   case MCK_offen:
4601     return Operand.isOffen() ? Match_Success : Match_InvalidOperand;
4602   case MCK_SSrcB32:
4603     // When operands have expression values, they will return true for isToken,
4604     // because it is not possible to distinguish between a token and an
4605     // expression at parse time. MatchInstructionImpl() will always try to
4606     // match an operand as a token, when isToken returns true, and when the
4607     // name of the expression is not a valid token, the match will fail,
4608     // so we need to handle it here.
4609     return Operand.isSSrcB32() ? Match_Success : Match_InvalidOperand;
4610   case MCK_SSrcF32:
4611     return Operand.isSSrcF32() ? Match_Success : Match_InvalidOperand;
4612   case MCK_SoppBrTarget:
4613     return Operand.isSoppBrTarget() ? Match_Success : Match_InvalidOperand;
4614   case MCK_VReg32OrOff:
4615     return Operand.isVReg32OrOff() ? Match_Success : Match_InvalidOperand;
4616   case MCK_InterpSlot:
4617     return Operand.isInterpSlot() ? Match_Success : Match_InvalidOperand;
4618   case MCK_Attr:
4619     return Operand.isInterpAttr() ? Match_Success : Match_InvalidOperand;
4620   case MCK_AttrChan:
4621     return Operand.isAttrChan() ? Match_Success : Match_InvalidOperand;
4622   default:
4623     return Match_InvalidOperand;
4624   }
4625 }