6cc0077d308ca0446e30b940019085b9dabd10b5
[oota-llvm.git] / lib / Target / R600 / MCTargetDesc / SIMCCodeEmitter.cpp
1 //===-- SIMCCodeEmitter.cpp - SI Code Emitter -------------------------------===//
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 /// \file
11 /// \brief The SI code emitter produces machine code that can be executed
12 /// directly on the GPU device.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #include "MCTargetDesc/AMDGPUMCTargetDesc.h"
17 #include "MCTargetDesc/AMDGPUMCCodeEmitter.h"
18 #include "llvm/MC/MCCodeEmitter.h"
19 #include "llvm/MC/MCContext.h"
20 #include "llvm/MC/MCFixup.h"
21 #include "llvm/MC/MCInst.h"
22 #include "llvm/MC/MCInstrInfo.h"
23 #include "llvm/MC/MCRegisterInfo.h"
24 #include "llvm/MC/MCSubtargetInfo.h"
25 #include "llvm/Support/raw_ostream.h"
26
27 using namespace llvm;
28
29 namespace {
30
31 /// \brief Helper type used in encoding
32 typedef union {
33   int32_t I;
34   float F;
35 } IntFloatUnion;
36
37 class SIMCCodeEmitter : public  AMDGPUMCCodeEmitter {
38   SIMCCodeEmitter(const SIMCCodeEmitter &) LLVM_DELETED_FUNCTION;
39   void operator=(const SIMCCodeEmitter &) LLVM_DELETED_FUNCTION;
40   const MCInstrInfo &MCII;
41   const MCRegisterInfo &MRI;
42   const MCSubtargetInfo &STI;
43   MCContext &Ctx;
44
45   /// \brief Encode a sequence of registers with the correct alignment.
46   unsigned GPRAlign(const MCInst &MI, unsigned OpNo, unsigned shift) const;
47
48   /// \brief Can this operand also contain immediate values?
49   bool isSrcOperand(const MCInstrDesc &Desc, unsigned OpNo) const;
50
51   /// \brief Encode an fp or int literal
52   uint32_t getLitEncoding(const MCOperand &MO) const;
53
54 public:
55   SIMCCodeEmitter(const MCInstrInfo &mcii, const MCRegisterInfo &mri,
56                   const MCSubtargetInfo &sti, MCContext &ctx)
57     : MCII(mcii), MRI(mri), STI(sti), Ctx(ctx) { }
58
59   ~SIMCCodeEmitter() { }
60
61   /// \breif Encode the instruction and write it to the OS.
62   virtual void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
63                          SmallVectorImpl<MCFixup> &Fixups) const;
64
65   /// \returns the encoding for an MCOperand.
66   virtual uint64_t getMachineOpValue(const MCInst &MI, const MCOperand &MO,
67                                      SmallVectorImpl<MCFixup> &Fixups) const;
68
69   /// \brief Encoding for when 2 consecutive registers are used
70   virtual unsigned GPR2AlignEncode(const MCInst &MI, unsigned OpNo,
71                                    SmallVectorImpl<MCFixup> &Fixup) const;
72
73   /// \brief Encoding for when 4 consectuive registers are used
74   virtual unsigned GPR4AlignEncode(const MCInst &MI, unsigned OpNo,
75                                    SmallVectorImpl<MCFixup> &Fixup) const;
76 };
77
78 } // End anonymous namespace
79
80 MCCodeEmitter *llvm::createSIMCCodeEmitter(const MCInstrInfo &MCII,
81                                            const MCRegisterInfo &MRI,
82                                            const MCSubtargetInfo &STI,
83                                            MCContext &Ctx) {
84   return new SIMCCodeEmitter(MCII, MRI, STI, Ctx);
85 }
86
87 bool SIMCCodeEmitter::isSrcOperand(const MCInstrDesc &Desc,
88                                    unsigned OpNo) const {
89
90   unsigned RegClass = Desc.OpInfo[OpNo].RegClass;
91   return (AMDGPU::SSrc_32RegClassID == RegClass) ||
92          (AMDGPU::SSrc_64RegClassID == RegClass) ||
93          (AMDGPU::VSrc_32RegClassID == RegClass) ||
94          (AMDGPU::VSrc_64RegClassID == RegClass);
95 }
96
97 uint32_t SIMCCodeEmitter::getLitEncoding(const MCOperand &MO) const {
98
99   IntFloatUnion Imm;
100   if (MO.isImm())
101     Imm.I = MO.getImm();
102   else if (MO.isFPImm())
103     Imm.F = MO.getFPImm();
104   else
105     return ~0;
106
107   if (Imm.I >= 0 && Imm.I <= 64)
108     return 128 + Imm.I;
109
110   if (Imm.I >= -16 && Imm.I <= -1)
111     return 192 + abs(Imm.I);
112
113   if (Imm.F == 0.5f)
114     return 240;
115
116   if (Imm.F == -0.5f)
117     return 241;
118
119   if (Imm.F == 1.0f)
120     return 242;
121
122   if (Imm.F == -1.0f)
123     return 243;
124
125   if (Imm.F == 2.0f)
126     return 244;
127
128   if (Imm.F == -2.0f)
129     return 245;
130
131   if (Imm.F == 4.0f)
132     return 246;
133
134   if (Imm.F == -4.0f)
135     return 247;
136
137   return 255;
138 }
139
140 void SIMCCodeEmitter::EncodeInstruction(const MCInst &MI, raw_ostream &OS,
141                                        SmallVectorImpl<MCFixup> &Fixups) const {
142
143   uint64_t Encoding = getBinaryCodeForInstr(MI, Fixups);
144   const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
145   unsigned bytes = Desc.getSize();
146
147   for (unsigned i = 0; i < bytes; i++) {
148     OS.write((uint8_t) ((Encoding >> (8 * i)) & 0xff));
149   }
150
151   if (bytes > 4)
152     return;
153
154   // Check for additional literals in SRC0/1/2 (Op 1/2/3)
155   for (unsigned i = 0, e = MI.getNumOperands(); i < e; ++i) {
156
157     // Check if this operand should be encoded as [SV]Src
158     if (!isSrcOperand(Desc, i))
159       continue;
160
161     // Is this operand a literal immediate?
162     const MCOperand &Op = MI.getOperand(i);
163     if (getLitEncoding(Op) != 255)
164       continue;
165
166     // Yes! Encode it
167     IntFloatUnion Imm;
168     if (Op.isImm())
169       Imm.I = Op.getImm();
170     else
171       Imm.F = Op.getFPImm();
172
173     for (unsigned j = 0; j < 4; j++) {
174       OS.write((uint8_t) ((Imm.I >> (8 * j)) & 0xff));
175     }
176
177     // Only one literal value allowed
178     break;
179   }
180 }
181
182 uint64_t SIMCCodeEmitter::getMachineOpValue(const MCInst &MI,
183                                             const MCOperand &MO,
184                                        SmallVectorImpl<MCFixup> &Fixups) const {
185   if (MO.isReg())
186     return MRI.getEncodingValue(MO.getReg());
187
188   if (MO.isExpr()) {
189     const MCExpr *Expr = MO.getExpr();
190     MCFixupKind Kind = MCFixupKind(FK_PCRel_4);
191     Fixups.push_back(MCFixup::Create(0, Expr, Kind, MI.getLoc()));
192     return 0;
193   }
194
195   // Figure out the operand number, needed for isSrcOperand check
196   unsigned OpNo = 0;
197   for (unsigned e = MI.getNumOperands(); OpNo < e; ++OpNo) {
198     if (&MO == &MI.getOperand(OpNo))
199       break;
200   }
201
202   const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
203   if (isSrcOperand(Desc, OpNo)) {
204     uint32_t Enc = getLitEncoding(MO);
205     if (Enc != ~0U && (Enc != 255 || Desc.getSize() == 4))
206       return Enc;
207
208   } else if (MO.isImm())
209     return MO.getImm();
210
211   llvm_unreachable("Encoding of this operand type is not supported yet.");
212   return 0;
213 }
214
215 //===----------------------------------------------------------------------===//
216 // Custom Operand Encodings
217 //===----------------------------------------------------------------------===//
218
219 unsigned SIMCCodeEmitter::GPRAlign(const MCInst &MI, unsigned OpNo,
220                                    unsigned shift) const {
221   unsigned regCode = MRI.getEncodingValue(MI.getOperand(OpNo).getReg());
222   return (regCode & 0xff) >> shift;
223 }
224
225 unsigned SIMCCodeEmitter::GPR2AlignEncode(const MCInst &MI,
226                                           unsigned OpNo ,
227                                         SmallVectorImpl<MCFixup> &Fixup) const {
228   return GPRAlign(MI, OpNo, 1);
229 }
230
231 unsigned SIMCCodeEmitter::GPR4AlignEncode(const MCInst &MI,
232                                           unsigned OpNo,
233                                         SmallVectorImpl<MCFixup> &Fixup) const {
234   return GPRAlign(MI, OpNo, 2);
235 }