1 //===-- ARMMCInstLower.cpp - Convert ARM MachineInstr to an MCInst --------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file contains code to lower ARM MachineInstrs to their corresponding
12 //===----------------------------------------------------------------------===//
15 #include "ARMAsmPrinter.h"
16 #include "ARMBaseInstrInfo.h"
17 #include "ARMMachineFunctionInfo.h"
18 #include "ARMSubtarget.h"
19 #include "MCTargetDesc/ARMAddressingModes.h"
20 #include "MCTargetDesc/ARMBaseInfo.h"
21 #include "MCTargetDesc/ARMMCExpr.h"
22 #include "llvm/ADT/APFloat.h"
23 #include "llvm/CodeGen/MachineBasicBlock.h"
24 #include "llvm/CodeGen/MachineInstr.h"
25 #include "llvm/CodeGen/MachineOperand.h"
26 #include "llvm/IR/Constants.h"
27 #include "llvm/MC/MCContext.h"
28 #include "llvm/MC/MCExpr.h"
29 #include "llvm/MC/MCInst.h"
30 #include "llvm/MC/MCInstBuilder.h"
31 #include "llvm/MC/MCStreamer.h"
32 #include "llvm/Support/ErrorHandling.h"
38 MCOperand
ARMAsmPrinter::GetSymbolRef(const MachineOperand
&MO
,
39 const MCSymbol
*Symbol
) {
40 MCSymbolRefExpr::VariantKind SymbolVariant
= MCSymbolRefExpr::VK_None
;
41 if (MO
.getTargetFlags() & ARMII::MO_SBREL
)
42 SymbolVariant
= MCSymbolRefExpr::VK_ARM_SBREL
;
45 MCSymbolRefExpr::create(Symbol
, SymbolVariant
, OutContext
);
46 switch (MO
.getTargetFlags() & ARMII::MO_OPTION_MASK
) {
48 llvm_unreachable("Unknown target flag on symbol operand");
49 case ARMII::MO_NO_FLAG
:
53 MCSymbolRefExpr::create(Symbol
, SymbolVariant
, OutContext
);
54 Expr
= ARMMCExpr::createLower16(Expr
, OutContext
);
58 MCSymbolRefExpr::create(Symbol
, SymbolVariant
, OutContext
);
59 Expr
= ARMMCExpr::createUpper16(Expr
, OutContext
);
61 case ARMII::MO_LO_0_7
:
62 Expr
= MCSymbolRefExpr::create(Symbol
, SymbolVariant
, OutContext
);
63 Expr
= ARMMCExpr::createLower0_7(Expr
, OutContext
);
65 case ARMII::MO_LO_8_15
:
66 Expr
= MCSymbolRefExpr::create(Symbol
, SymbolVariant
, OutContext
);
67 Expr
= ARMMCExpr::createLower8_15(Expr
, OutContext
);
69 case ARMII::MO_HI_0_7
:
70 Expr
= MCSymbolRefExpr::create(Symbol
, SymbolVariant
, OutContext
);
71 Expr
= ARMMCExpr::createUpper0_7(Expr
, OutContext
);
73 case ARMII::MO_HI_8_15
:
74 Expr
= MCSymbolRefExpr::create(Symbol
, SymbolVariant
, OutContext
);
75 Expr
= ARMMCExpr::createUpper8_15(Expr
, OutContext
);
79 if (!MO
.isJTI() && MO
.getOffset())
80 Expr
= MCBinaryExpr::createAdd(Expr
,
81 MCConstantExpr::create(MO
.getOffset(),
84 return MCOperand::createExpr(Expr
);
88 bool ARMAsmPrinter::lowerOperand(const MachineOperand
&MO
,
90 switch (MO
.getType()) {
91 default: llvm_unreachable("unknown operand type");
92 case MachineOperand::MO_Register
:
93 // Ignore all implicit register operands.
96 assert(!MO
.getSubReg() && "Subregs should be eliminated!");
97 MCOp
= MCOperand::createReg(MO
.getReg());
99 case MachineOperand::MO_Immediate
:
100 MCOp
= MCOperand::createImm(MO
.getImm());
102 case MachineOperand::MO_MachineBasicBlock
:
103 MCOp
= MCOperand::createExpr(MCSymbolRefExpr::create(
104 MO
.getMBB()->getSymbol(), OutContext
));
106 case MachineOperand::MO_GlobalAddress
:
107 MCOp
= GetSymbolRef(MO
,
108 GetARMGVSymbol(MO
.getGlobal(), MO
.getTargetFlags()));
110 case MachineOperand::MO_ExternalSymbol
:
111 MCOp
= GetSymbolRef(MO
,
112 GetExternalSymbolSymbol(MO
.getSymbolName()));
114 case MachineOperand::MO_JumpTableIndex
:
115 MCOp
= GetSymbolRef(MO
, GetJTISymbol(MO
.getIndex()));
117 case MachineOperand::MO_ConstantPoolIndex
:
118 if (Subtarget
->genExecuteOnly())
119 llvm_unreachable("execute-only should not generate constant pools");
120 MCOp
= GetSymbolRef(MO
, GetCPISymbol(MO
.getIndex()));
122 case MachineOperand::MO_BlockAddress
:
123 MCOp
= GetSymbolRef(MO
, GetBlockAddressSymbol(MO
.getBlockAddress()));
125 case MachineOperand::MO_FPImmediate
: {
126 APFloat Val
= MO
.getFPImm()->getValueAPF();
128 Val
.convert(APFloat::IEEEdouble(), APFloat::rmTowardZero
, &ignored
);
129 MCOp
= MCOperand::createDFPImm(bit_cast
<uint64_t>(Val
.convertToDouble()));
132 case MachineOperand::MO_RegisterMask
:
133 // Ignore call clobbers.
139 void llvm::LowerARMMachineInstrToMCInst(const MachineInstr
*MI
, MCInst
&OutMI
,
141 OutMI
.setOpcode(MI
->getOpcode());
143 // In the MC layer, we keep modified immediates in their encoded form
144 bool EncodeImms
= false;
145 switch (MI
->getOpcode()) {
171 for (const MachineOperand
&MO
: MI
->operands()) {
173 if (AP
.lowerOperand(MO
, MCOp
)) {
174 if (MCOp
.isImm() && EncodeImms
) {
175 int32_t Enc
= ARM_AM::getSOImmVal(MCOp
.getImm());
179 OutMI
.addOperand(MCOp
);
184 void ARMAsmPrinter::EmitSled(const MachineInstr
&MI
, SledKind Kind
)
186 const MachineFunction
*MF
= MI
.getParent()->getParent();
187 if (MF
->getInfo
<ARMFunctionInfo
>()->isThumbFunction()) {
188 const Function
&Fn
= MF
->getFunction();
189 DiagnosticInfoUnsupported
Unsupported(
191 "An attempt to perform XRay instrumentation for a"
192 " Thumb function (not supported). Detected when emitting a sled.",
194 Fn
.getContext().diagnose(Unsupported
);
197 static const int8_t NoopsInSledCount
= 6;
198 // We want to emit the following pattern:
203 // ; 6 NOP instructions (24 bytes)
206 // We need the 24 bytes (6 instructions) because at runtime, we'd be patching
207 // over the full 28 bytes (7 instructions) with the following pattern:
210 // MOVW r0, #<lower 16 bits of function ID>
211 // MOVT r0, #<higher 16 bits of function ID>
212 // MOVW ip, #<lower 16 bits of address of __xray_FunctionEntry/Exit>
213 // MOVT ip, #<higher 16 bits of address of __xray_FunctionEntry/Exit>
217 OutStreamer
->emitCodeAlignment(Align(4), &getSubtargetInfo());
218 auto CurSled
= OutContext
.createTempSymbol("xray_sled_", true);
219 OutStreamer
->emitLabel(CurSled
);
220 auto Target
= OutContext
.createTempSymbol();
222 // Emit "B #20" instruction, which jumps over the next 24 bytes (because
223 // register pc is 8 bytes ahead of the jump instruction by the moment CPU
225 // By analogy to ARMAsmPrinter::lowerPseudoInstExpansion() |case ARM::B|.
226 // It is not clear why |addReg(0)| is needed (the last operand).
227 EmitToStreamer(*OutStreamer
, MCInstBuilder(ARM::Bcc
).addImm(20)
228 .addImm(ARMCC::AL
).addReg(0));
230 emitNops(NoopsInSledCount
);
232 OutStreamer
->emitLabel(Target
);
233 recordSled(CurSled
, MI
, Kind
, 2);
236 void ARMAsmPrinter::LowerPATCHABLE_FUNCTION_ENTER(const MachineInstr
&MI
)
238 EmitSled(MI
, SledKind::FUNCTION_ENTER
);
241 void ARMAsmPrinter::LowerPATCHABLE_FUNCTION_EXIT(const MachineInstr
&MI
)
243 EmitSled(MI
, SledKind::FUNCTION_EXIT
);
246 void ARMAsmPrinter::LowerPATCHABLE_TAIL_CALL(const MachineInstr
&MI
)
248 EmitSled(MI
, SledKind::TAIL_CALL
);