File: G4_Operand.h

package info (click to toggle)
intel-graphics-compiler2 2.28.4-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 792,744 kB
  • sloc: cpp: 5,761,745; ansic: 466,928; lisp: 312,143; python: 114,790; asm: 44,736; pascal: 10,930; sh: 8,033; perl: 7,914; ml: 3,625; awk: 3,523; yacc: 2,747; javascript: 2,667; lex: 1,898; f90: 1,028; cs: 573; xml: 474; makefile: 344; objc: 162
file content (1101 lines) | stat: -rw-r--r-- 35,910 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
/*========================== begin_copyright_notice ============================

Copyright (C) 2022 Intel Corporation

SPDX-License-Identifier: MIT

============================= end_copyright_notice ===========================*/

#ifndef G4_OPERAND_H
#define G4_OPERAND_H

#include "Assertions.h"
#include "G4_Declare.h"
#include "G4_Opcode.h"
#include "G4_Register.h"
#include "include/RelocationInfo.h"

#include <functional>
#include <iostream>
#include <vector>

// RegionWH and RegionV are special for the different modes of source register
// indirect addressing RegionWH = <width, horzStride>, we set vertStride to
// UNDEFINED_SHORT RegionV = <horzStride>, we set both vertStride and width to
// UNDEFINED_SHORT
//
// FIXME: Move it into vISA namespace and fix all unscoped references.
struct RegionDesc {
  const uint16_t vertStride;
  const uint16_t width;
  const uint16_t horzStride;

  RegionDesc(uint16_t vs, uint16_t w, uint16_t hs)
      : vertStride(vs), width(w), horzStride(hs) {
    vISA_ASSERT(isLegal(), "illegal region desc");
  }
  void *operator new(size_t sz, vISA::Mem_Manager &m) { return m.alloc(sz); }

  // The legal values for Width are {1, 2, 4, 8, 16}.
  // The legal values for VertStride are {0, 1, 2, 4, 8, 16, 32}.
  // The legal values for HorzStride are {0, 1, 2, 4}.
  bool isLegal() const { return isLegal(vertStride, width, horzStride); }

  static bool isLegal(unsigned vs, unsigned w, unsigned hs);

  enum RegionDescKind {
    RK_Other,   // all others like <4; 2, 1> etc.
    RK_Stride0, // <0;1,0> aka scalar
    RK_Stride1, // <1;1,0> aka contiguous
    RK_Stride2, // <2;1,0>
    RK_Stride4  // <4;1,0>
  };

  // Determine the region description kind. Strided case only.
  static RegionDescKind getRegionDescKind(uint16_t size, uint16_t vstride,
                                          uint16_t width, uint16_t hstride);

  bool isRegionWH() const {
    return vertStride == UNDEFINED_SHORT && width != UNDEFINED_SHORT;
  }
  bool isRegionV() const {
    return vertStride == UNDEFINED_SHORT && width == UNDEFINED_SHORT;
  }
  bool isRegion110() const {
    return vertStride == 1 && width == 1 && horzStride == 0;
  }
  bool isScalar() const {
    return (vertStride == 0 && horzStride == 0) ||
           (width == 1 && vertStride == 0);
  } // to support decompression
  bool isRegionSW() const {
    return vertStride != UNDEFINED_SHORT && width == UNDEFINED_SHORT &&
           horzStride == UNDEFINED_SHORT;
  }
  bool isEqual(const RegionDesc *r) const {
    return vertStride == r->vertStride && width == r->width &&
           horzStride == r->horzStride;
  } // to support re-compression
  void emit(std::ostream &output) const;
  bool isPackedRegion() const {
    return ((horzStride == 0 && vertStride <= 1) ||
            (horzStride == 1 && vertStride <= width));
  }
  bool isFlatRegion() const {
    return (isScalar() || vertStride == horzStride * width);
  }
  bool isRepeatRegion(unsigned short execSize) const {
    return (!isScalar() &&
            (execSize > width && vertStride < horzStride * width));
  }

  // Contiguous regions are:
  // (1) ExSize is 1, or
  // (2) <1; 1, *> with arbitrary ExSize, or
  // (3) <N; N, 1> with arbitrary ExSize, or
  // (4) <*; N, 1> with ExSize == N.
  //
  // A region is contiguous iff sequence
  // { f(0, 0), f(0, 1), ..., f(1, 0), ..., f(ExSize / width - 1, width - 1) }
  // has a common difference 1, where
  //
  // f(i, j) = i x vstride + j x hstride
  //
  // for 0 <= i < ExSize / width and 0 <= j < width
  bool isContiguous(unsigned ExSize) const;
  bool isSingleNonUnitStride(uint32_t execSize, uint16_t &stride) const;
  bool isSingleStride(uint32_t execSize, uint16_t &stride) const;
  bool isSingleStride(uint32_t execSize) const {
    uint16_t stride = 0;
    return isSingleStride(execSize, stride);
  }
};

namespace vISA {

// Forward declarations.
class IR_Builder;
// Forward declarations for the instruction classes.
class G4_INST;
class G4_InstSend;
class G4_FillIntrinsic;
class G4_SpillIntrinsic;
class G4_PseudoMovInstrinsic;
class G4_InstDpas;
// Forward declarations for the concrete operand classes. We need them here
// because the base class has APIs that perform downcasts to the concrete type.
class G4_Imm;
class G4_Reloc_Imm;
class G4_Label;
class G4_AddrExp;
class G4_DstRegRegion;
class G4_SrcRegRegion;
class G4_CondMod;
class G4_Predicate;

class G4_Operand {
  // TODO: revisit the decision to allow G4_INST to access internal G4_Operand
  // field (mainly for bound?)
  friend class G4_INST;
  friend class G4_InstSend;
  friend class G4_InstIntrinsic;
  friend class G4_FillIntrinsic;
  friend class G4_SpillIntrinsic;
  friend class G4_PseudoMovInstrinsic;
  friend class G4_InstDpas;

public:
  enum Kind : unsigned char {
    immediate,
    srcRegRegion,
    dstRegRegion,
    predicate, // instruction predicate
    condMod,   // condition modifier
    addrExp,
    label
  };
  virtual ~G4_Operand() {}

protected:
  G4_INST *inst = nullptr;

  // FIXME: It's redundant to keep both top_dcl and base. We should have a union
  // based on operand kind.
  G4_Declare *top_dcl = nullptr;
  G4_VarBase *base = nullptr;

  // TODO: Should we track footprint at word granularity instead?
  uint64_t bitVec[2]; // bit masks at byte granularity (for flags, at bit
                      // granularity)

  // Group byte-sized fields together.
  Kind kind;
  G4_Type type = Type_UNDEF;
  bool rightBoundSet = false;
  G4_AccRegSel accRegSel = ACC_UNDEFINED;

  // [left_bound, right_bound] describes the region in the root variable that
  // this operand touches. for variables and addresses:
  //  lb = offset of the first byte of the first element
  //  rb = offset of the last byte of the last element
  //  for non-send instructions, (rb - lb) < 64 always holds since operand can't
  //  cross 2GRF boundary for send instructions, rb is determined by the
  //  message/response length
  // for flags:
  //  lb = bit offset of the first flag bit
  //  rb = bit offset of the last flag bit
  //  (rb - lb) < 32 always holds for flags
  //  for predicate and conditonal modifiers, the bounds are also effected by the
  //  quarter control
  uint16_t left_bound = 0;
  uint16_t right_bound = 0;
  uint16_t byteOffset = 0;

  explicit G4_Operand(Kind k, G4_Type ty = Type_UNDEF,
                      G4_VarBase *base = nullptr)
      : base(base), kind(k), type(ty){
    bitVec[0] = bitVec[1] = 0;
  }

  G4_Operand(Kind k, G4_VarBase *base)
      : base(base), kind(k) {
    bitVec[0] = bitVec[1] = 0;
  }

public:
  Kind getKind() const { return kind; }
  G4_Type getType() const { return type; }
  unsigned short getTypeSize() const { return TypeSize(getType()); }

  bool isImm() const { return kind == Kind::immediate; }
  bool isVectImm() const {
    return (isImm() && (type == Type_UV || type == Type_V || type == Type_VF));
  }
  bool isSrcRegRegion() const { return kind == Kind::srcRegRegion; }
  bool isDstRegRegion() const { return kind == Kind::dstRegRegion; }
  bool isRegRegion() const {
    return kind == srcRegRegion || kind == dstRegRegion;
  }
  bool isPredicate() const { return kind == predicate; }
  bool isCondMod() const { return kind == condMod; }
  bool isLabel() const { return kind == label; }
  bool isAddrExp() const { return kind == addrExp; }

  const G4_Declare *getTopDcl() const { return top_dcl; }
  G4_Declare *getTopDcl() { return top_dcl; }
  void setTopDcl(G4_Declare *dcl) { top_dcl = dcl; }

  const G4_VarBase *getBase() const { return base; }
  G4_VarBase *getBase() { return base; }
  void setBase(G4_VarBase *b) { base = b; }
  bool isIndirect() const;
  bool isVxHIndirect() const;

  const G4_Declare *getBaseRegVarRootDeclare() const;
  G4_Declare *getBaseRegVarRootDeclare();

  virtual bool isRelocImm() const { return false; }
  virtual void emit(std::ostream &output) = 0;
  void dump() const;
  std::string print() const;

  bool isGreg() const;
  bool isAreg() const;
  bool isNullReg() const;
  bool isIpReg() const;
  bool isNReg() const;
  bool isAccReg() const;
  bool isFlag() const;
  bool isMaskReg() const;
  bool isMsReg() const;
  bool isSrReg() const;
  bool isCrReg() const;
  bool isDbgReg() const;
  bool isTmReg() const;
  bool isTDRReg() const;
  bool isS0() const;

  const G4_AddrExp *asAddrExp() const {
#ifdef _DEBUG
    if (!isAddrExp()) {
      return nullptr;
    }
#endif
    return reinterpret_cast<const G4_AddrExp *>(this);
  }
  G4_AddrExp *asAddrExp() {
    return const_cast<G4_AddrExp *>(((const G4_Operand *)this)->asAddrExp());
  }

  const G4_DstRegRegion *asDstRegRegion() const {
#ifdef _DEBUG
    if (!isDstRegRegion()) {
      return nullptr;
    }
#endif
    return reinterpret_cast<const G4_DstRegRegion *>(this);
  }
  G4_DstRegRegion *asDstRegRegion() {
    return const_cast<G4_DstRegRegion *>(
        ((const G4_Operand *)this)->asDstRegRegion());
  }

  const G4_SrcRegRegion *asSrcRegRegion() const {
#ifdef _DEBUG
    if (!isSrcRegRegion()) {
      return nullptr;
    }
#endif
    return reinterpret_cast<const G4_SrcRegRegion *>(this);
  }
  G4_SrcRegRegion *asSrcRegRegion() {
    return const_cast<G4_SrcRegRegion *>(
        ((const G4_Operand *)this)->asSrcRegRegion());
  }

  const G4_Imm *asImm() const {
#ifdef _DEBUG
    if (!isImm()) {
      return nullptr;
    }
#endif
    return reinterpret_cast<const G4_Imm *>(this);
  }
  G4_Imm *asImm() {
    return const_cast<G4_Imm *>(((const G4_Operand *)this)->asImm());
  }

  const G4_Reloc_Imm *asRelocImm() const {
#ifdef _DEBUG
    if (!isRelocImm()) {
      return nullptr;
    }
#endif
    return reinterpret_cast<const G4_Reloc_Imm *>(this);
  }
  G4_Reloc_Imm *asRelocImm() {
    return const_cast<G4_Reloc_Imm *>(((const G4_Operand *)this)->asRelocImm());
  }

  const G4_Predicate *asPredicate() const {
#ifdef _DEBUG
    if (!isPredicate()) {
      return nullptr;
    }
#endif
    return reinterpret_cast<const G4_Predicate *>(this);
  }
  G4_Predicate *asPredicate() {
    return const_cast<G4_Predicate *>(
        ((const G4_Operand *)this)->asPredicate());
  }

  const G4_CondMod *asCondMod() const {
#ifdef _DEBUG
    if (!isCondMod()) {
      return nullptr;
    }
#endif
    return reinterpret_cast<const G4_CondMod *>(this);
  }

  G4_CondMod *asCondMod() {
    return const_cast<G4_CondMod *>(((const G4_Operand *)this)->asCondMod());
  }

  const G4_Label *asLabel() const {
#ifdef _DEBUG
    if (!isLabel()) {
      return nullptr;
    }
#endif
    return reinterpret_cast<const G4_Label *>(this);
  }
  G4_Label *asLabel() {
    return const_cast<G4_Label *>(((const G4_Operand *)this)->asLabel());
  }

  bool isSrc() const { return isImm() || isAddrExp() || isSrcRegRegion(); }

  bool isScalarSrc() const;

  bool crossGRF(const IR_Builder &builder);

  unsigned getLeftBound();
  unsigned getRightBound();
  bool isRightBoundSet() const { return rightBoundSet; }
  uint64_t getBitVecL();
  uint64_t getBitVecH(const IR_Builder &builder);
  // For operands that do use it, it is computed during left bound compuation.
  unsigned getByteOffset() const { return byteOffset; }

  // ToDo: get rid of this setter
  void setBitVecL(uint64_t bvl) { bitVec[0] = bvl; }

  void setBitVecFromSize(uint32_t NBytes, const IR_Builder &builder);

  void updateFootPrint(BitSet &footprint, bool isSet,
                       const IR_Builder &builder);

  virtual unsigned computeRightBound(uint8_t exec_size) { return left_bound; }
  void setRightBound(unsigned val) {
    rightBoundSet = true;
    right_bound = val;
  }
  void unsetRightBound() { rightBoundSet = false; }
  void setLeftBound(unsigned val) { left_bound = val; }
  const G4_INST *getInst() const { return inst; }
  G4_INST *getInst() { return inst; }
  void setInst(G4_INST *op) { inst = op; }
  void setAccRegSel(G4_AccRegSel value) { accRegSel = value; }
  G4_AccRegSel getAccRegSel() const { return accRegSel; }
  bool isAccRegValid() const { return accRegSel != ACC_UNDEFINED; }
  bool isPhysicallyAllocatedRegVar(bool includeAccRegSel = true) const;

  unsigned getLinearizedStart();
  unsigned getLinearizedEnd();

  // compare if this operand is the same as the input w.r.t physical register in
  // the end
  virtual G4_CmpRelation compareOperand(G4_Operand *opnd,
                                        const IR_Builder &builder) {
    return Rel_disjoint;
  }

  static G4_Type GetNonVectorImmType(G4_Type type) {
    switch (type) {
    case Type_V:
      return Type_W;
    case Type_UV:
      return Type_UW;
    case Type_VF:
      return Type_F;
    default:
      return type;
    }
  }
};

class G4_Imm : public G4_Operand {
  // Requirement for the immediate value 'imm'
  //   Given a value V of type T, and let <V-as-uint> be its bit pattern as
  //   unsigned integer type whose size == sizeof(T). Let 'imm' be the
  //   immediate for V, the following must hold:
  //     (uint64_t)(<V-as-uint>) == (uint64_t)imm.num
  //     i.e.  int16_t v ---> (uint64_t)(*(uint16_t*)&v) == (uint64_t)imm.num
  //           float f   ---> (uint64_t)(*(uint32_t*)&f) == (uint64_t)imm.num
  union {
    int64_t num;
    uint32_t num32;
    double fp;
    float fp32;
  } imm;

public:
  G4_Imm(int64_t i, G4_Type ty) : G4_Operand(G4_Operand::immediate, ty) {
    imm.num = i;
  }

  G4_Imm(double fp, G4_Type ty) : G4_Operand(G4_Operand::immediate, ty) {
    imm.fp = fp;
  }

  G4_Imm(float fp) : G4_Operand(G4_Operand::immediate, Type_F) {
    imm.num = 0; // make sure to clear all the bits
    imm.fp32 = fp;
  }

  void *operator new(size_t sz, Mem_Manager &m) { return m.alloc(sz); }
  int64_t getImm() const { return imm.num; } // Get bits of imm AS integer.
  int64_t getInt() const {
    vISA_ASSERT(!IS_TYPE_F32_F64(type), ERROR_UNKNOWN);
    return imm.num;
  }
  float getFloat() const {
    // if fp32 is sNAN, it will return qNAN. Be careful!
    vISA_ASSERT(IS_FTYPE(type), ERROR_UNKNOWN);
    return imm.fp32;
  }
  double getDouble() const {
    vISA_ASSERT(IS_DFTYPE(type), ERROR_UNKNOWN);
    return imm.fp;
  }
  bool isZero() const;
  // True if this is a signed integer and its sign bit(s) are 0.
  bool isSignBitZero() const;
  void emit(std::ostream &output) override;
  void emitAutoFmt(std::ostream &output);

  bool isEqualTo(G4_Imm &imm1) const;
  bool isEqualTo(G4_Imm *imm1) const { return isEqualTo(*imm1); }

  G4_CmpRelation compareOperand(G4_Operand *opnd,
                                const IR_Builder &builder) override;
  G4_RegFileKind getRegFile() const { return G4_UndefinedRF; }

  static bool isInTypeRange(int64_t imm, G4_Type ty);

  static int64_t typecastVals(int64_t value, G4_Type type);
};

class G4_Reloc_Imm : public G4_Imm {
  friend class IR_Builder;

  GenRelocType relocKind;
  const char  *symbol;

  // G4_Reloc_Imm is a relocation target field.
  // Use Build_IR::createRelocImm to construct one of these operands.
  G4_Reloc_Imm(GenRelocType rt, const char *sym, int64_t val, G4_Type ty)
    : G4_Imm(val, ty), relocKind(rt), symbol(sym) {}

  void *operator new(size_t sz, Mem_Manager &m) { return m.alloc(sz); }
public:

  G4_CmpRelation compareOperand(G4_Operand *opnd,
                                const IR_Builder &builder) override;

  bool isRelocImm() const override { return true; }

  void emit(std::ostream &output) override;

  // magic value used when no default relocation value is given
  static const uint32_t DEFAULT_MAGIC = 0x6e10ca2e;
};

class G4_Label : public G4_Operand {
  friend class IR_Builder;

  const char *label;
  VISA_Label_Kind kind;

  G4_Label(const char *l, VISA_Label_Kind k)
      : G4_Operand(G4_Operand::label), label(l), kind(k) {}

public:
  const char *getLabelName() const { return label; }
  void *operator new(size_t sz, Mem_Manager &m) { return m.alloc(sz); }
  void emit(std::ostream &output) override;
  bool isStackFunction() const { return kind == LABEL_FUNCTION; }
  bool isSubroutine() const { return kind == LABEL_SUBROUTINE; }
  bool isFCLabel() const { return kind == LABEL_FC; }
  bool isBlock() const {
    return kind == LABEL_BLOCK || kind == LABEL_DIVERGENT_RESOURCE_LOOP;
  }
  bool isDivergentResourceLoop() const {
    return kind == LABEL_DIVERGENT_RESOURCE_LOOP;
  }
  VISA_Label_Kind getLabelKind() const { return kind; }
};

class G4_SrcRegRegion final : public G4_Operand {
  friend class IR_Builder;

  const RegionDesc *desc;
  const short regOff;    // base+regOff is the starting register of the region
  const short subRegOff; // sub reg offset related to the regVar in "base"
  // FIXME: regOff (direct) and immAddrOff (indriect) should be mutually
  // exclusive. We should place them in a union.
  short immAddrOff; // imm addr offset
  G4_SrcModifier mod;
  const G4_RegAccess acc;

  G4_SrcRegRegion(const IR_Builder &builder, G4_SrcModifier m, G4_RegAccess a,
                  G4_VarBase *b, short roff, short sroff, const RegionDesc *rd,
                  G4_Type ty, G4_AccRegSel regSel = ACC_UNDEFINED)
      : G4_Operand(G4_Operand::srcRegRegion, ty, b), desc(rd), regOff(roff),
        subRegOff(sroff), mod(m), acc(a) {
    immAddrOff = 0;
    accRegSel = regSel;

    computeLeftBound(builder);
    right_bound = 0;
  }

  void setSrcBitVec(uint8_t exec_size, const IR_Builder &irb);

public:
  G4_SrcRegRegion(G4_SrcRegRegion &rgn);
  G4_SrcRegRegion& operator=(const G4_SrcRegRegion&) = delete;
  void *operator new(size_t sz, Mem_Manager &m) { return m.alloc(sz); }

  bool operator==(const G4_SrcRegRegion &other) const {
    if (base != other.base || regOff != other.regOff ||
        subRegOff != other.subRegOff ||
        desc->vertStride != other.desc->vertStride ||
        desc->horzStride != other.desc->horzStride ||
        desc->width != other.desc->width || mod != other.mod ||
        acc != other.acc || type != other.type) {
      return false;
    }

    if (acc == IndirGRF && immAddrOff != other.immAddrOff) {
      return false;
    }

    return true;
  }

  void computeLeftBound(const IR_Builder &builder);
  short getRegOff() const { return regOff; }
  short getSubRegOff() const { return subRegOff; }

  G4_SrcModifier getModifier() const { return mod; }
  bool hasModifier() const { return mod != Mod_src_undef; }
  const RegionDesc *getRegion() const { return desc; }
  G4_RegAccess getRegAccess() const { return acc; }
  short getAddrImm() const { return immAddrOff; }
  unsigned short getElemSize() const { return TypeSize(type); }

  void setImmAddrOff(short off) { immAddrOff = off; }
  void setModifier(G4_SrcModifier m) { mod = m; }

  bool sameSrcRegRegion(G4_SrcRegRegion &rgn);

  void emit(std::ostream &output) override;
  void emitRegVarOff(std::ostream &output);
  void emitRegVarOffNoRegion(std::ostream &output);

  bool isAreg() const { return base->isAreg(); }
  bool isNullReg() const { return base->isNullReg(); }
  bool isIpReg() const { return base->isIpReg(); }
  bool isFlag() const { return base->isFlag(); }
  bool isNReg() const { return base->isNReg(); }
  bool isAccReg() const { return base->isAccReg(); }
  bool isMaskReg() const { return base->isMaskReg(); }
  bool isMsReg() const { return base->isMsReg(); }
  bool isSrReg() const { return base->isSrReg(); }
  bool isCrReg() const { return base->isCrReg(); }
  bool isDbgReg() const { return base->isDbgReg(); }
  bool isTmReg() const { return base->isTmReg(); }
  bool isTDRReg() const { return base->isTDRReg(); }
  bool isGreg() const { return base->isGreg(); }
  // Returns true if this operand is a direct address operand (e.g.,
  // a0.n<0;1,0>:uw)
  // The difference between the two versions happens when base is an address
  // variable; isDirectA0() returns false before RA when the declare is not
  // assigned a physical AddrReg, while isDirectAddress() always returns true.
  // Having two versions is unfortunate but we keep the behavior to avoid
  // potential regressions in legacy code.
  bool isDirectA0() const { return acc == Direct && base->isA0(); }
  bool isDirectAddress() const { return acc == Direct && base->isAddress(); }
  bool isScalar() const;
  bool isS0() const { return base->isS0(); }
  bool isFlatRegRegion(
      uint8_t exChannelWidth,
      std::function<bool(uint8_t dstStrideInBytes, uint8_t dstSubRegOffInBytes,
                         uint8_t srcStrideInBytes, uint8_t srcSubRegOffInBytes,
                         uint8_t exChannelWidth)> checkFlatRegRegionFunc);

  unsigned short ExRegNum(bool &) const;
  unsigned short ExSubRegNum(bool &);
  unsigned short ExIndSubRegNum(bool &);
  short ExIndImmVal(void);

  bool isIndirect() const { return acc != Direct; }

  bool isVxHIndirect() const {
    return isIndirect() && getRegion()->isRegionWH() && getRegion()->width == 1;
  }

  unsigned computeRightBound(uint8_t exec_size) override;
  G4_CmpRelation compareOperand(G4_Operand *opnd,
                                const IR_Builder &builder) override;

  void setType(const IR_Builder &builder, G4_Type ty) {
    // FIXME: we should forbid setType() where ty has a different size than old
    // type
    bool recomputeLeftBound = false;

    if (TypeSize(type) != TypeSize(ty)) {
      unsetRightBound();
      recomputeLeftBound = true;
    }

    type = ty;

    if (recomputeLeftBound) {
      computeLeftBound(builder);
    }
  }

  void setRegion(const IR_Builder &builder, const RegionDesc *rd,
                 bool isInvariant = false) {
    if (!isInvariant && !desc->isEqual(rd)) {
      unsetRightBound();
      desc = rd;
      computeLeftBound(builder);
    } else {
      desc = rd;
    }
  }

  bool isNativeType() const;
  bool isNativePackedRowRegion() const;
  bool isNativePackedRegion() const;
  bool evenlySplitCrossGRF(const IR_Builder &builder, uint8_t execSize,
                           bool &sameSubRegOff, bool &vertCrossGRF,
                           bool &contRegion, uint8_t &eleInFirstGRF);
  bool evenlySplitCrossGRF(const IR_Builder &builder, uint8_t execSize);
  bool coverTwoGRF(const IR_Builder &builder);
  bool checkGRFAlign(const IR_Builder &builder);
  bool hasFixedSubregOffset(const IR_Builder &builder, uint32_t &offset);
  bool isNativePackedSrcRegion();
  uint8_t getMaxExecSize(const IR_Builder &builder, int pos, uint8_t maxExSize,
                         bool allowCrossGRF, uint16_t &vs, uint16_t &wd,
                         bool &twoGRFsrc);

  bool isSpilled() const {
    if (getBase() && getBase()->isRegVar()) {
      return getBase()->asRegVar()->isSpilled();
    }

    return false;
  }

  // return the byte offset from the region start for the element at "pos"
  int getByteOffset(int pos) const {
    int rowIdx = pos / desc->width;
    int colIdx = pos % desc->width;
    return rowIdx * desc->vertStride * getElemSize() +
           colIdx * desc->horzStride * getElemSize();
  }

  void rewriteContiguousRegion(IR_Builder &builder, uint16_t opNum);
};

class G4_DstRegRegion final : public G4_Operand {
  friend class IR_Builder;

  G4_RegAccess acc; // direct, indirect GenReg or indirect MsgReg
  short regOff;     // base+regOff is the starting register of the region
  short subRegOff;  // sub reg offset related to the regVar in "base"
  // FIXME: regOff (direct) and immAddrOff (indriect) should be mutually
  // exclusive. We should place them in a union.
  short immAddrOff;          // imm addr offset for indirect dst
  unsigned short horzStride; // <DstRegion> has only horzStride

  G4_DstRegRegion(const IR_Builder &builder, G4_RegAccess a, G4_VarBase *b,
                  short roff, short sroff, unsigned short hstride, G4_Type ty,
                  G4_AccRegSel regSel = ACC_UNDEFINED)
      : G4_Operand(G4_Operand::dstRegRegion, ty, b), acc(a),
        horzStride(hstride) {
    immAddrOff = 0;
    accRegSel = regSel;

    regOff = (roff == ((short)UNDEFINED_SHORT)) ? 0 : roff;
    subRegOff = sroff;

    computeLeftBound(builder);
    right_bound = 0;
  }

  // DstRegRegion should only be constructed through IR_Builder
  void *operator new(size_t sz, Mem_Manager &m) { return m.alloc(sz); }

public:
  G4_DstRegRegion(G4_DstRegRegion &rgn);
  G4_DstRegRegion(const IR_Builder &builder, G4_DstRegRegion &rgn,
                  G4_VarBase *new_base);
  G4_DstRegRegion& operator=(const G4_DstRegRegion&) = delete;

  void computeLeftBound(const IR_Builder &builder);

  G4_RegAccess getRegAccess() const { return acc; }
  short getRegOff() const { return regOff; }
  short getSubRegOff() const { return subRegOff; }

  bool isCrossGRFDst(const IR_Builder &builder);
  unsigned short getHorzStride() const { return horzStride; }
  short getAddrImm() const { return immAddrOff; }
  unsigned short getElemSize() const { return getTypeSize(); }
  unsigned short getExecTypeSize() const { return horzStride * getElemSize(); }

  void setImmAddrOff(short off) { immAddrOff = off; }
  void emit(std::ostream &output) override;
  void emitRegVarOff(std::ostream &output);

  bool isAreg() const { return base->isAreg(); }
  bool isNullReg() const { return base->isNullReg(); }
  bool isIpReg() const { return base->isIpReg(); }
  bool isFlag() const { return base->isFlag(); }
  bool isNReg() const { return base->isNReg(); }
  bool isAccReg() const { return base->isAccReg(); }
  bool isMaskReg() const { return base->isMaskReg(); }
  bool isMsReg() const { return base->isMsReg(); }
  bool isSrReg() const { return base->isSrReg(); }
  bool isCrReg() const { return base->isCrReg(); }
  bool isDbgReg() const { return base->isDbgReg(); }
  bool isTmReg() const { return base->isTmReg(); }
  bool isTDRReg() const { return base->isTDRReg(); }
  bool isGreg() const { return base->isGreg(); }
  // Returns true if this operand is a direct address operand (e.g.,
  // a0.n<1>:uw)
  // The difference between the two versions happens when base is an address
  // variable; isDirectA0() returns false before RA when the declare is not
  // assigned a physical AddrReg, while isDirectAddress() always returns true.
  // Having two versions is unfortunate but we keep the behavior to avoid
  // potential regressions in legacy code.
  bool isDirectA0() const { return acc == Direct && base->isA0(); }
  bool isDirectAddress() const { return acc == Direct && base->isAddress(); }
  bool isS0() const { return base->isS0(); }

  unsigned short ExRegNum(bool &);
  unsigned short ExSubRegNum(bool &);
  unsigned short ExIndSubRegNum(bool &);
  short ExIndImmVal(void);

  bool isIndirect() const { return acc != Direct; }

  void setType(const IR_Builder &builder, G4_Type ty);

  void setHorzStride(unsigned short hs) {
    if (horzStride != hs) {
      unsetRightBound();
    }

    horzStride = hs;
  }
  void setDstBitVec(uint8_t exec_size);
  unsigned computeRightBound(uint8_t exec_size) override;
  G4_CmpRelation compareOperand(G4_Operand *opnd,
                                const IR_Builder &builder) override;
  bool isNativeType() const;
  bool isNativePackedRowRegion() const;
  bool isNativePackedRegion() const;
  bool coverGRF(const IR_Builder &builder, uint16_t numGRF, uint8_t execSize);
  bool goodOneGRFDst(const IR_Builder &builder, uint8_t execSize);
  bool goodtwoGRFDst(const IR_Builder &builder, uint8_t execSize);
  bool evenlySplitCrossGRF(const IR_Builder &builder, uint8_t execSize);
  bool checkGRFAlign(const IR_Builder &builder) const;
  bool hasFixedSubregOffset(const IR_Builder &builder, uint32_t &offset);
  uint8_t getMaxExecSize(const IR_Builder &builder, int pos, uint8_t maxExSize,
                         bool twoGRFsrc);
  bool isSpilled() const {
    if (getBase() && getBase()->isRegVar()) {
      return getBase()->asRegVar()->isSpilled();
    }

    return false;
  }
};

typedef enum : unsigned char {
  PRED_DEFAULT,
  PRED_ANY2H,
  PRED_ANY4H,
  PRED_ANY8H,
  PRED_ANY16H,
  PRED_ANY32H,
  PRED_ALL2H,
  PRED_ALL4H,
  PRED_ALL8H,
  PRED_ALL16H,
  PRED_ALL32H,
  PRED_ANYV,
  PRED_ALLV,
  PRED_ANY_WHOLE, // any of the flag-bits
  PRED_ALL_WHOLE  // all of the flag-bits
} G4_Predicate_Control;

//
// predicate control for inst
//
class G4_Predicate final : public G4_Operand {
  friend class IR_Builder;

  G4_PredState state; // + or -
  G4_Predicate_Control control;
  unsigned short subRegOff;

  G4_Predicate(G4_PredState s, G4_VarBase *flag, unsigned short srOff,
               G4_Predicate_Control ctrl)
      : G4_Operand(G4_Operand::predicate, flag), state(s), control(ctrl),
        subRegOff(srOff) {
    top_dcl = getBase()->asRegVar()->getDeclare();
    vISA_ASSERT(flag->isFlag(), ERROR_INTERNAL_ARGUMENT);
    if (getBase()->asRegVar()->getPhyReg()) {
      left_bound = srOff * 16;

      byteOffset = srOff * 2;

      auto flagNum = getBase()->asRegVar()->getPhyReg()->asAreg()->getFlagNum();
      left_bound += flagNum * 32;
      byteOffset += flagNum * 4;
    } else {
      left_bound = 0;
      byteOffset = 0;
    }
  }

public:
  G4_Predicate(G4_Predicate &prd);
  G4_Predicate& operator=(const G4_Predicate&) = delete;

  void *operator new(size_t sz, Mem_Manager &m) { return m.alloc(sz); }
  unsigned short getSubRegOff() const { return subRegOff; }
  unsigned short getRegOff() const {
    vISA_ASSERT(getBase()->isAreg(), ERROR_INTERNAL_ARGUMENT);
    return getBase()->asRegVar()->getPhyReg()->asAreg()->getFlagNum();
  }

  G4_PredState getState() const { return state; }
  void setState(G4_PredState s) { state = s; }
  G4_Predicate_Control getControl() const { return control; }
  void setControl(G4_Predicate_Control PredCtrl) { control = PredCtrl; }
  bool samePredicate(const G4_Predicate &prd) const;
  void emit(std::ostream &output) override;
  void emit_body(std::ostream &output);

  unsigned computeRightBound(uint8_t exec_size) override;
  G4_CmpRelation compareOperand(G4_Operand *opnd,
                                const IR_Builder &builder) override;
  void splitPred();
  unsigned getPredCtrlGroupSize() const {
    switch (control) {
    case PRED_ANY2H:
    case PRED_ALL2H:
      return 2;
    case PRED_ANY4H:
    case PRED_ALL4H:
      return 4;
    case PRED_ANY8H:
    case PRED_ALL8H:
      return 8;
    case PRED_ANY16H:
    case PRED_ALL16H:
      return 16;
    case PRED_ANY32H:
    case PRED_ALL32H:
      return 32;
    default:
      return 1;
    }
  }
  static bool isAnyH(G4_Predicate_Control Ctrl) {
    switch (Ctrl) {
    default:
      break;
    case PRED_ANY2H:
    case PRED_ANY4H:
    case PRED_ANY8H:
    case PRED_ANY16H:
    case PRED_ANY32H:
      return true;
    }
    return false;
  }
  static bool isAllH(G4_Predicate_Control Ctrl) {
    switch (Ctrl) {
    default:
      break;
    case PRED_ALL2H:
    case PRED_ALL4H:
    case PRED_ALL8H:
    case PRED_ALL16H:
    case PRED_ALL32H:
      return true;
    }
    return false;
  }
};

//
// condition modifier for inst
//
class G4_CondMod final : public G4_Operand {
  friend class IR_Builder;
  G4_CondModifier mod;
  unsigned short subRegOff;

  G4_CondMod(G4_CondModifier m, G4_VarBase *flag, unsigned short off)
      : G4_Operand(G4_Operand::condMod, flag), mod(m), subRegOff(off) {
    if (flag != nullptr) {
      top_dcl = getBase()->asRegVar()->getDeclare();
      vISA_ASSERT(flag->isFlag(), ERROR_INTERNAL_ARGUMENT);
      if (getBase()->asRegVar()->getPhyReg()) {
        left_bound = off * 16;
        byteOffset = off * 2;

        auto flagNum =
            getBase()->asRegVar()->getPhyReg()->asAreg()->getFlagNum();
        left_bound += flagNum * 32;
        byteOffset += flagNum * 4;
      } else {
        left_bound = 0;
        byteOffset = 0;
      }
    }
  }

public:
  G4_CondMod(G4_CondMod &cMod);
  G4_CondMod(const G4_CondMod&) = delete;
  G4_CondMod& operator=(G4_CondMod cMod) = delete;
  ~G4_CondMod() = default;
  void *operator new(size_t sz, Mem_Manager &m) { return m.alloc(sz); }
  G4_CondModifier getMod() const { return mod; }
  unsigned short getRegOff() const {
    vISA_ASSERT(getBase()->isAreg(), ERROR_INTERNAL_ARGUMENT);
    vISA_ASSERT(getBase()->asRegVar()->getPhyReg(),
                 "getRegOff is called for non-PhyReg");
    return getBase()->asRegVar()->getPhyReg()->asAreg()->getFlagNum();
  }
  unsigned short getSubRegOff() const { return subRegOff; }
  bool sameCondMod(const G4_CondMod &prd) const;
  void emit(std::ostream &output) override;

  // Get condition modifier when operands are reversed.
  static G4_CondModifier getReverseCondMod(G4_CondModifier mod) {
    switch (mod) {
    default:
      break;
    case Mod_g:
      return Mod_le;
    case Mod_ge:
      return Mod_l;
    case Mod_l:
      return Mod_ge;
    case Mod_le:
      return Mod_g;
    }

    return mod;
  }

  unsigned computeRightBound(uint8_t exec_size) override;
  G4_CmpRelation compareOperand(G4_Operand *opnd,
                                const IR_Builder &builder) override;
  void splitCondMod();
};

class G4_AddrExp final : public G4_Operand {
  G4_RegVar *m_addressedReg;
  int m_offset; // current implementation: byte offset

public:
  G4_AddrExp(G4_RegVar *reg, int offset, G4_Type ty)
      : G4_Operand(G4_Operand::addrExp, ty), m_addressedReg(reg),
        m_offset(offset) {}

  void *operator new(size_t sz, Mem_Manager &m) { return m.alloc(sz); }

  const G4_RegVar *getRegVar() const { return m_addressedReg; }
  G4_RegVar *getRegVar() { return m_addressedReg; }
  void setRegVar(G4_RegVar *var) { m_addressedReg = var; }
  int getOffset() const { return m_offset; }
  void setOffset(int tOffset) { m_offset = tOffset; }

  int eval(const IR_Builder &builder);
  bool isRegAllocPartaker() const {
    return m_addressedReg->isRegAllocPartaker();
  }

  void emit(std::ostream &output);
};

// Inlined functions for G4_Operand class.
// TODO: Move them inside the class.
inline bool G4_Operand::isGreg() const {
  return isRegRegion() && const_cast<G4_VarBase *>(getBase())->isGreg();
}
inline bool G4_Operand::isAreg() const {
  return isRegRegion() && const_cast<G4_VarBase *>(getBase())->isAreg();
}
inline bool G4_Operand::isNullReg() const {
  return isRegRegion() && const_cast<G4_VarBase *>(getBase())->isNullReg();
}
inline bool G4_Operand::isIpReg() const {
  return isRegRegion() && const_cast<G4_VarBase *>(getBase())->isIpReg();
}
inline bool G4_Operand::isNReg() const {
  return isRegRegion() && const_cast<G4_VarBase *>(getBase())->isNReg();
}
inline bool G4_Operand::isAccReg() const {
  return isRegRegion() && const_cast<G4_VarBase *>(getBase())->isAccReg();
}
inline bool G4_Operand::isFlag() const {
  if (isRegRegion() && const_cast<G4_VarBase *>(getBase())->isFlag())
    return true;
  return isPredicate() || isCondMod();
}
inline bool G4_Operand::isMaskReg() const {
  return isRegRegion() && const_cast<G4_VarBase *>(getBase())->isMaskReg();
}
inline bool G4_Operand::isMsReg() const {
  return isRegRegion() && const_cast<G4_VarBase *>(getBase())->isMsReg();
}
inline bool G4_Operand::isSrReg() const {
  return isRegRegion() && const_cast<G4_VarBase *>(getBase())->isSrReg();
}
inline bool G4_Operand::isCrReg() const {
  return isRegRegion() && const_cast<G4_VarBase *>(getBase())->isCrReg();
}
inline bool G4_Operand::isDbgReg() const {
  return isRegRegion() && const_cast<G4_VarBase *>(getBase())->isDbgReg();
}
inline bool G4_Operand::isTmReg() const {
  return isRegRegion() && const_cast<G4_VarBase *>(getBase())->isTmReg();
}
inline bool G4_Operand::isTDRReg() const {
  return isRegRegion() && const_cast<G4_VarBase *>(getBase())->isTDRReg();
}
inline bool G4_Operand::isS0() const {
  return isRegRegion() && const_cast<G4_VarBase *>(getBase())->isS0();
}

inline bool G4_Operand::isScalarSrc() const {
  return isImm() || isAddrExp() ||
         (isSrcRegRegion() && asSrcRegRegion()->isScalar());
}

inline const G4_Declare *G4_Operand::getBaseRegVarRootDeclare() const {
  return getBase()->asRegVar()->getDeclare()->getRootDeclare();
}
inline G4_Declare *G4_Operand::getBaseRegVarRootDeclare() {
  return getBase()->asRegVar()->getDeclare()->getRootDeclare();
}

} // namespace vISA

#endif // G4_OPERAND_H