LLVM  5.0.0
AMDGPUAsmBackend.cpp
Go to the documentation of this file.
1 //===-- AMDGPUAsmBackend.cpp - AMDGPU Assembler Backend -------------------===//
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 /// \file
9 //===----------------------------------------------------------------------===//
10 
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/MC/MCAsmBackend.h"
15 #include "llvm/MC/MCAssembler.h"
16 #include "llvm/MC/MCContext.h"
18 #include "llvm/MC/MCObjectWriter.h"
19 #include "llvm/MC/MCValue.h"
21 
22 using namespace llvm;
23 
24 namespace {
25 
26 class AMDGPUAsmBackend : public MCAsmBackend {
27 public:
28  AMDGPUAsmBackend(const Target &T)
29  : MCAsmBackend() {}
30 
31  unsigned getNumFixupKinds() const override { return AMDGPU::NumTargetFixupKinds; };
32 
33  void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
35  uint64_t Value, bool IsResolved) const override;
36  bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value,
37  const MCRelaxableFragment *DF,
38  const MCAsmLayout &Layout) const override {
39  return false;
40  }
41  void relaxInstruction(const MCInst &Inst, const MCSubtargetInfo &STI,
42  MCInst &Res) const override {
43  llvm_unreachable("Not implemented");
44  }
45  bool mayNeedRelaxation(const MCInst &Inst) const override { return false; }
46  bool writeNopData(uint64_t Count, MCObjectWriter *OW) const override;
47 
48  const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override;
49 };
50 
51 } //End anonymous namespace
52 
53 static unsigned getFixupKindNumBytes(unsigned Kind) {
54  switch (Kind) {
56  return 2;
57  case FK_SecRel_1:
58  case FK_Data_1:
59  return 1;
60  case FK_SecRel_2:
61  case FK_Data_2:
62  return 2;
63  case FK_SecRel_4:
64  case FK_Data_4:
65  case FK_PCRel_4:
66  return 4;
67  case FK_SecRel_8:
68  case FK_Data_8:
69  return 8;
70  default:
71  llvm_unreachable("Unknown fixup kind!");
72  }
73 }
74 
75 static uint64_t adjustFixupValue(const MCFixup &Fixup, uint64_t Value,
76  MCContext *Ctx) {
77  int64_t SignedValue = static_cast<int64_t>(Value);
78 
79  switch (Fixup.getKind()) {
81  int64_t BrImm = (SignedValue - 4) / 4;
82 
83  if (Ctx && !isInt<16>(BrImm))
84  Ctx->reportError(Fixup.getLoc(), "branch size exceeds simm16");
85 
86  return BrImm;
87  }
88  case FK_Data_1:
89  case FK_Data_2:
90  case FK_Data_4:
91  case FK_Data_8:
92  case FK_PCRel_4:
93  case FK_SecRel_4:
94  return Value;
95  default:
96  llvm_unreachable("unhandled fixup kind");
97  }
98 }
99 
100 void AMDGPUAsmBackend::applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
101  const MCValue &Target,
102  MutableArrayRef<char> Data, uint64_t Value,
103  bool IsResolved) const {
104  Value = adjustFixupValue(Fixup, Value, &Asm.getContext());
105  if (!Value)
106  return; // Doesn't change encoding.
107 
108  MCFixupKindInfo Info = getFixupKindInfo(Fixup.getKind());
109 
110  // Shift the value into position.
111  Value <<= Info.TargetOffset;
112 
113  unsigned NumBytes = getFixupKindNumBytes(Fixup.getKind());
114  uint32_t Offset = Fixup.getOffset();
115  assert(Offset + NumBytes <= Data.size() && "Invalid fixup offset!");
116 
117  // For each byte of the fragment that the fixup touches, mask in the bits from
118  // the fixup value.
119  for (unsigned i = 0; i != NumBytes; ++i)
120  Data[Offset + i] |= static_cast<uint8_t>((Value >> (i * 8)) & 0xff);
121 }
122 
123 const MCFixupKindInfo &AMDGPUAsmBackend::getFixupKindInfo(
124  MCFixupKind Kind) const {
125  const static MCFixupKindInfo Infos[AMDGPU::NumTargetFixupKinds] = {
126  // name offset bits flags
127  { "fixup_si_sopp_br", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
128  };
129 
130  if (Kind < FirstTargetFixupKind)
131  return MCAsmBackend::getFixupKindInfo(Kind);
132 
133  return Infos[Kind - FirstTargetFixupKind];
134 }
135 
136 bool AMDGPUAsmBackend::writeNopData(uint64_t Count, MCObjectWriter *OW) const {
137  // If the count is not 4-byte aligned, we must be writing data into the text
138  // section (otherwise we have unaligned instructions, and thus have far
139  // bigger problems), so just write zeros instead.
140  OW->WriteZeros(Count % 4);
141 
142  // We are properly aligned, so write NOPs as requested.
143  Count /= 4;
144 
145  // FIXME: R600 support.
146  // s_nop 0
147  const uint32_t Encoded_S_NOP_0 = 0xbf800000;
148 
149  for (uint64_t I = 0; I != Count; ++I)
150  OW->write32(Encoded_S_NOP_0);
151 
152  return true;
153 }
154 
155 //===----------------------------------------------------------------------===//
156 // ELFAMDGPUAsmBackend class
157 //===----------------------------------------------------------------------===//
158 
159 namespace {
160 
161 class ELFAMDGPUAsmBackend : public AMDGPUAsmBackend {
162  bool Is64Bit;
163  bool HasRelocationAddend;
164 
165 public:
166  ELFAMDGPUAsmBackend(const Target &T, const Triple &TT) :
167  AMDGPUAsmBackend(T), Is64Bit(TT.getArch() == Triple::amdgcn),
168  HasRelocationAddend(TT.getOS() == Triple::AMDHSA) { }
169 
170  MCObjectWriter *createObjectWriter(raw_pwrite_stream &OS) const override {
171  return createAMDGPUELFObjectWriter(Is64Bit, HasRelocationAddend, OS);
172  }
173 };
174 
175 } // end anonymous namespace
176 
178  const MCRegisterInfo &MRI,
179  const Triple &TT, StringRef CPU,
180  const MCTargetOptions &Options) {
181  // Use 64-bit ELF for amdgcn
182  return new ELFAMDGPUAsmBackend(T, TT);
183 }
void WriteZeros(unsigned N)
SMLoc getLoc() const
Definition: MCFixup.h:112
This represents an "assembler immediate".
Definition: MCValue.h:40
constexpr bool isInt< 16 >(int64_t x)
Definition: MathExtras.h:298
MCContext & getContext() const
Definition: MCAssembler.h:257
Defines the object file and target independent interfaces used by the assembler backend to write nati...
unsigned TargetOffset
The bit offset to write the relocation into.
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:66
Is this fixup kind PCrelative? This is used by the assembler backend to evaluate fixup values in a ta...
Encapsulates the layout of an assembly file at a particular point in time.
Definition: MCAsmLayout.h:29
A four-byte section relative fixup.
Definition: MCFixup.h:42
A four-byte fixup.
Definition: MCFixup.h:26
Context object for machine code objects.
Definition: MCContext.h:59
A two-byte section relative fixup.
Definition: MCFixup.h:41
MCAsmBackend * createAMDGPUAsmBackend(const Target &T, const MCRegisterInfo &MRI, const Triple &TT, StringRef CPU, const MCTargetOptions &Options)
uint32_t getOffset() const
Definition: MCFixup.h:95
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:149
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:159
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
A relaxable fragment holds on to its MCInst, since it may need to be relaxed during the assembler lay...
Definition: MCFragment.h:255
void write32(uint32_t Value)
MCObjectWriter * createAMDGPUELFObjectWriter(bool Is64Bit, bool HasRelocationAddend, raw_pwrite_stream &OS)
unsigned const MachineRegisterInfo * MRI
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:291
MCFixupKind
Extensible enumeration to represent the type of a fixup.
Definition: MCFixup.h:23
void reportError(SMLoc L, const Twine &Msg)
Definition: MCContext.cpp:608
MCFixupKind getKind() const
Definition: MCFixup.h:93
A one-byte fixup.
Definition: MCFixup.h:24
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
A four-byte pc relative fixup.
Definition: MCFixup.h:30
Target - Wrapper for Target specific information.
A one-byte section relative fixup.
Definition: MCFixup.h:40
llvm_unreachable("Unknown RegBank!")
static uint64_t adjustFixupValue(const MCFixup &Fixup, uint64_t Value, MCContext *Ctx)
Provides AMDGPU specific target descriptions.
static unsigned getFixupKindNumBytes(unsigned Kind)
A eight-byte section relative fixup.
Definition: MCFixup.h:43
#define I(x, y, z)
Definition: MD5.cpp:58
16-bit PC relative fixup for SOPP branch instructions.
MCSubtargetInfo - Generic base class for all target subtargets.
A eight-byte fixup.
Definition: MCFixup.h:27
Target independent information on a fixup kind.
An abstract base class for streams implementations that also support a pwrite operation.
Definition: raw_ostream.h:333
const unsigned Kind
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:73
Generic interface to target specific assembler backends.
Definition: MCAsmBackend.h:35
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
virtual const MCFixupKindInfo & getFixupKindInfo(MCFixupKind Kind) const
Get information on a fixup kind.
A two-byte fixup.
Definition: MCFixup.h:25