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