LLVM 19.0.0git
AMDGPUMCExpr.cpp
Go to the documentation of this file.
1//===- AMDGPUMCExpr.cpp - AMDGPU specific MC expression classes -----------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "AMDGPUMCExpr.h"
10#include "llvm/MC/MCContext.h"
11#include "llvm/MC/MCStreamer.h"
12#include "llvm/MC/MCSymbol.h"
13#include "llvm/MC/MCValue.h"
16#include <optional>
17
18using namespace llvm;
19
20AMDGPUVariadicMCExpr::AMDGPUVariadicMCExpr(VariadicKind Kind,
22 MCContext &Ctx)
23 : Kind(Kind), Ctx(Ctx) {
24 assert(Args.size() >= 1 && "Needs a minimum of one expression.");
25 assert(Kind != AGVK_None &&
26 "Cannot construct AMDGPUVariadicMCExpr of kind none.");
27
28 // Allocating the variadic arguments through the same allocation mechanism
29 // that the object itself is allocated with so they end up in the same memory.
30 //
31 // Will result in an asan failure if allocated on the heap through standard
32 // allocation (e.g., through SmallVector's grow).
33 RawArgs = static_cast<const MCExpr **>(
34 Ctx.allocate(sizeof(const MCExpr *) * Args.size()));
35 std::uninitialized_copy(Args.begin(), Args.end(), RawArgs);
36 this->Args = ArrayRef<const MCExpr *>(RawArgs, Args.size());
37}
38
39AMDGPUVariadicMCExpr::~AMDGPUVariadicMCExpr() { Ctx.deallocate(RawArgs); }
40
43 MCContext &Ctx) {
44 return new (Ctx) AMDGPUVariadicMCExpr(Kind, Args, Ctx);
45}
46
48 assert(Index < Args.size() &&
49 "Indexing out of bounds AMDGPUVariadicMCExpr sub-expr");
50 return Args[Index];
51}
52
54 const MCAsmInfo *MAI) const {
55 switch (Kind) {
56 default:
57 llvm_unreachable("Unknown AMDGPUVariadicMCExpr kind.");
58 case AGVK_Or:
59 OS << "or(";
60 break;
61 case AGVK_Max:
62 OS << "max(";
63 break;
64 }
65 for (auto It = Args.begin(); It != Args.end(); ++It) {
66 (*It)->print(OS, MAI, /*InParens=*/false);
67 if ((It + 1) != Args.end())
68 OS << ", ";
69 }
70 OS << ')';
71}
72
73static int64_t op(AMDGPUVariadicMCExpr::VariadicKind Kind, int64_t Arg1,
74 int64_t Arg2) {
75 switch (Kind) {
76 default:
77 llvm_unreachable("Unknown AMDGPUVariadicMCExpr kind.");
79 return std::max(Arg1, Arg2);
81 return Arg1 | Arg2;
82 }
83}
84
86 MCValue &Res, const MCAsmLayout *Layout, const MCFixup *Fixup) const {
87 std::optional<int64_t> Total;
88
89 for (const MCExpr *Arg : Args) {
90 MCValue ArgRes;
91 if (!Arg->evaluateAsRelocatable(ArgRes, Layout, Fixup) ||
92 !ArgRes.isAbsolute())
93 return false;
94
95 if (!Total.has_value())
96 Total = ArgRes.getConstant();
97 Total = op(Kind, *Total, ArgRes.getConstant());
98 }
99
100 Res = MCValue::get(*Total);
101 return true;
102}
103
105 for (const MCExpr *Arg : Args)
106 Streamer.visitUsedExpr(*Arg);
107}
108
110 for (const MCExpr *Arg : Args) {
111 if (Arg->findAssociatedFragment())
112 return Arg->findAssociatedFragment();
113 }
114 return nullptr;
115}
This file defines the BumpPtrAllocator interface.
#define op(i)
PowerPC TLS Dynamic Call Fixup
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
AMDGPU target specific variadic MCExpr operations.
Definition: AMDGPUMCExpr.h:27
static const AMDGPUVariadicMCExpr * create(VariadicKind Kind, ArrayRef< const MCExpr * > Args, MCContext &Ctx)
bool evaluateAsRelocatableImpl(MCValue &Res, const MCAsmLayout *Layout, const MCFixup *Fixup) const override
void printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const override
MCFragment * findAssociatedFragment() const override
const MCExpr * getSubExpr(size_t Index) const
void visitUsedExpr(MCStreamer &Streamer) const override
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition: MCAsmInfo.h:56
Encapsulates the layout of an assembly file at a particular point in time.
Definition: MCAsmLayout.h:28
Context object for machine code objects.
Definition: MCContext.h:81
void * allocate(unsigned Size, unsigned Align=8)
Definition: MCContext.h:858
void deallocate(void *Ptr)
Definition: MCContext.h:862
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:71
Streaming machine code generation interface.
Definition: MCStreamer.h:212
void visitUsedExpr(const MCExpr &Expr)
This represents an "assembler immediate".
Definition: MCValue.h:36
int64_t getConstant() const
Definition: MCValue.h:43
static MCValue get(const MCSymbolRefExpr *SymA, const MCSymbolRefExpr *SymB=nullptr, int64_t Val=0, uint32_t RefKind=0)
Definition: MCValue.h:59
bool isAbsolute() const
Is this an absolute (as opposed to relocatable) value.
Definition: MCValue.h:49
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18