LLVM 24.0.0git
PISAMCInstLower.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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/// \file
10/// This file implements lowering from PISA MachineInstrs to MCInsts.
11///
12//===----------------------------------------------------------------------===//
13
14#include "PISAMCInstLower.h"
16#include "PISA.h"
17#include "PISARegManager.h"
18#include "PISASubtarget.h"
19#include "PISAUtils.h"
22#include "llvm/IR/Constants.h"
23#include "llvm/MC/MCContext.h"
24#include "llvm/MC/MCExpr.h"
26
27using namespace llvm;
28
29MCOperand PISAMCInstLower::lowerSymbolOperand(const MachineOperand &MO,
30 MCSymbol &Sym) const {
31 const MCExpr *Expr = PISAGlobalAddressMCExpr::create(Sym, OutContext);
32 return MCOperand::createExpr(Expr);
33}
34
36PISAMCInstLower::getGlobalAddressSymbol(const MachineOperand &MO) const {
37 assert(MO.isGlobal());
38 return *AP.getSymbol(MO.getGlobal());
39}
40
41void PISAMCInstLower::lower(const MachineInstr *MI, MCInst &OutMI) const {
42 OutMI.setOpcode(MI->getOpcode());
43 for (unsigned OpNo = 0, E = MI->getNumOperands(); OpNo != E; ++OpNo) {
44 const MachineOperand &MO = MI->getOperand(OpNo);
45 MCOperand MCOp;
46 switch (MO.getType()) {
47 default:
48 llvm_unreachable("unknown operand type");
50 // Skip call to addOperand() outside the switch as MCInst doesn't
51 // support metadata operand types.
52 continue;
53 }
55 // Convert external symbol operand (kernel arg name on loadParam
56 // instructions) to an MCExpr for PISAInstPrinter::printParamMemOperand.
57 MCSymbol *Sym = OutContext.getOrCreateSymbol(MO.getSymbolName());
58 MCOp = MCOperand::createExpr(MCSymbolRefExpr::create(Sym, OutContext));
59 break;
60 }
62 // MachineOperand of type MO_FrameIndex is lowered to immediate operand
63 // with value equal to frame index. Whether an immediate operand is
64 // frame index is indicated by flags that are stored in MCInst instance.
65 // During operand printing, we inspect flag value first and decide if
66 // the operand has to be printed as private variable or a generic
67 // immediate value.
68 unsigned int FrameIndex = MO.getIndex();
69 MCOp = MCOperand::createImm(FrameIndex);
70 setVariableRef(OutMI, OpNo);
71 break;
72 }
74 MCOp = lowerSymbolOperand(MO, getGlobalAddressSymbol(MO));
75 break;
76 }
79 MCSymbolRefExpr::create(MO.getMBB()->getSymbol(), OutContext));
80 break;
82 Register CurReg = MO.getReg();
83 unsigned EncodedVal = CurReg;
84 if (CurReg.isVirtual()) {
86 MI->getParent()->getParent()->getRegInfo();
87 const TargetRegisterClass *RC = MRI.getRegClass(CurReg);
88 unsigned NumElts = TRI.getNumEltsFromRegClass(RC);
89 unsigned EltSize = TRI.getBitSizeFromRegClass(RC);
90 PISA::RegEncoder::RegBank Bank = RegMgr.getRegBank(NumElts, EltSize);
91 EncodedVal = RegMgr.encodeVirtualRegister(Bank, CurReg);
92 }
93 MCOp = MCOperand::createReg(EncodedVal);
94 unsigned SubReg = MO.getSubReg();
95 bool IsMov = MI->isMoveImmediate() || MI->isMoveReg();
96 PISA::Swizzle Swizzle = TRI.getSwizzle(SubReg);
97 if (IsMov && !SubReg && CurReg.isVirtual()) {
98 // vector args in mov instructions always print swizzle
100 MI->getParent()->getParent()->getRegInfo();
101 const TargetRegisterClass *RC = MRI.getRegClass(CurReg);
102 unsigned NumElts = TRI.getNumEltsFromRegClass(RC);
103 if (NumElts == 2)
104 Swizzle = PISA::Swizzle::XY;
105 else if (NumElts == 4)
106 Swizzle = PISA::Swizzle::XYZW;
107 else
108 assert((NumElts == 1) && "unknown swizzle value");
109 }
110 setSwizzle(OutMI, OpNo, Swizzle);
111 break;
112 }
114 MCOp = MCOperand::createImm(MO.getImm());
115 break;
117 // All floating point values are bitcasted
118 // into integer ones. Double- and single-precison ones could be specially
119 // taged in MC as SFP or DFP immediate. But, in general, as each type of
120 // immediate operand has its own methods, general immediate operands
121 // won't loss any semantics.
122 uint64_t ImmVal =
124 // TODO: bfloat16 has a different exponent/mantissa layout than
125 // IEEE half; a separate kBFPImmediate kind may be needed to
126 // distinguish them (see also getFPImmInReg TODO).
127 if (MO.getFPImm()->getType()->isHalfTy() ||
128 MO.getFPImm()->getType()->isBFloatTy())
129 MCOp = MCOperand::createHFPImm(ImmVal);
130 else if (MO.getFPImm()->getType()->isFloatTy())
131 MCOp = MCOperand::createSFPImm(ImmVal);
132 else if (MO.getFPImm()->getType()->isDoubleTy())
133 MCOp = MCOperand::createDFPImm(ImmVal);
134 else
135 MCOp = MCOperand::createImm(ImmVal);
136 break;
137 }
138
139 OutMI.addOperand(MCOp);
140 }
141}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
IRTranslator LLVM IR MI
This file declares PISA-specific MC expressions for floating-point immediates.
This file declares lowering from PISA MachineInstrs to MCInsts.
This file declares management of PISA virtual registers.
This file declares the PISA subtarget.
This file declares utility functions for the PISA backend.
This file declares the top-level interfaces for the PISA target.
MachineRegisterInfo & MRI
APInt bitcastToAPInt() const
Definition APFloat.h:1508
uint64_t getZExtValue() const
Get zero extended value.
Definition APInt.h:1560
const APFloat & getValueAPF() const
Definition Constants.h:463
Instances of this class represent a single low-level machine instruction.
Definition MCInst.h:207
void addOperand(const MCOperand Op)
Definition MCInst.h:234
void setOpcode(unsigned Op)
Definition MCInst.h:220
Instances of this class represent operands of the MCInst class.
Definition MCInst.h:39
static MCOperand createExpr(const MCExpr *Val)
Definition MCInst.h:185
static MCOperand createSFPImm(uint32_t Val)
Definition MCInst.h:171
static MCOperand createReg(MCRegister Reg)
Definition MCInst.h:150
static MCOperand createImm(int64_t Val)
Definition MCInst.h:157
static MCOperand createDFPImm(uint64_t Val)
Definition MCInst.h:178
static MCOperand createHFPImm(uint16_t Val)
Definition MCInst.h:164
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition MCExpr.h:210
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition MCSymbol.h:42
LLVM_ABI MCSymbol * getSymbol() const
Return the MCSymbol for this basic block.
Representation of each machine instruction.
MachineOperand class - Representation of each machine instruction operand.
unsigned getSubReg() const
const GlobalValue * getGlobal() const
int64_t getImm() const
MachineBasicBlock * getMBB() const
bool isGlobal() const
isGlobal - Tests if this is a MO_GlobalAddress operand.
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
const char * getSymbolName() const
Register getReg() const
getReg - Returns the register number.
const ConstantFP * getFPImm() const
@ MO_Immediate
Immediate operand.
@ MO_GlobalAddress
Address of a global value.
@ MO_MachineBasicBlock
MachineBasicBlock reference.
@ MO_FrameIndex
Abstract Stack Frame Index.
@ MO_Register
Register operand.
@ MO_ExternalSymbol
Name of external global symbol.
@ MO_Metadata
Metadata reference (for debug info)
@ MO_FPImmediate
Floating-point immediate operand.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
static const PISAGlobalAddressMCExpr * create(const MCSymbol &Symbol, MCContext &Ctx)
static void setSwizzle(MCInst &MI, unsigned OpNo, PISA::Swizzle Swizzle)
void lower(const MachineInstr *MI, MCInst &OutMI) const
static void setVariableRef(MCInst &MI, unsigned OpNo)
Wrapper class representing virtual and physical registers.
Definition Register.h:20
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
Definition Register.h:79
bool isFloatTy() const
Return true if this is 'float', a 32-bit IEEE fp type.
Definition Type.h:155
bool isBFloatTy() const
Return true if this is 'bfloat', a 16-bit bfloat type.
Definition Type.h:147
bool isHalfTy() const
Return true if this is 'half', a 16-bit IEEE fp type.
Definition Type.h:144
bool isDoubleTy() const
Return true if this is 'double', a 64-bit IEEE fp type.
Definition Type.h:158
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:257
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is an optimization pass for GlobalISel generic memory operations.
MCRegisterClass TargetRegisterClass
Definition FastISel.h:58