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