1 //===- HexagonMCInstLower.cpp - Convert Hexagon MachineInstr to an MCInst -===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains code to lower Hexagon MachineInstrs to their corresponding
13 //===----------------------------------------------------------------------===//
16 #include "HexagonAsmPrinter.h"
17 #include "MCTargetDesc/HexagonMCExpr.h"
18 #include "MCTargetDesc/HexagonMCInstrInfo.h"
19 #include "MCTargetDesc/HexagonMCTargetDesc.h"
20 #include "llvm/ADT/APFloat.h"
21 #include "llvm/ADT/APInt.h"
22 #include "llvm/CodeGen/MachineBasicBlock.h"
23 #include "llvm/CodeGen/MachineInstr.h"
24 #include "llvm/CodeGen/MachineOperand.h"
25 #include "llvm/IR/Constants.h"
26 #include "llvm/MC/MCContext.h"
27 #include "llvm/MC/MCExpr.h"
28 #include "llvm/MC/MCInst.h"
29 #include "llvm/Support/ErrorHandling.h"
30 #include "llvm/Support/raw_ostream.h"
37 void HexagonLowerToMC(const MCInstrInfo &MCII, const MachineInstr *MI,
38 MCInst &MCB, HexagonAsmPrinter &AP);
40 } // end namespace llvm
42 static MCOperand GetSymbolRef(const MachineOperand &MO, const MCSymbol *Symbol,
43 HexagonAsmPrinter &Printer, bool MustExtend) {
44 MCContext &MC = Printer.OutContext;
47 // Populate the relocation type based on Hexagon target flags
49 MCSymbolRefExpr::VariantKind RelocationType;
50 switch (MO.getTargetFlags() & ~HexagonII::HMOTF_ConstExtended) {
52 RelocationType = MCSymbolRefExpr::VK_None;
54 case HexagonII::MO_PCREL:
55 RelocationType = MCSymbolRefExpr::VK_Hexagon_PCREL;
57 case HexagonII::MO_GOT:
58 RelocationType = MCSymbolRefExpr::VK_GOT;
60 case HexagonII::MO_LO16:
61 RelocationType = MCSymbolRefExpr::VK_Hexagon_LO16;
63 case HexagonII::MO_HI16:
64 RelocationType = MCSymbolRefExpr::VK_Hexagon_HI16;
66 case HexagonII::MO_GPREL:
67 RelocationType = MCSymbolRefExpr::VK_Hexagon_GPREL;
69 case HexagonII::MO_GDGOT:
70 RelocationType = MCSymbolRefExpr::VK_Hexagon_GD_GOT;
72 case HexagonII::MO_GDPLT:
73 RelocationType = MCSymbolRefExpr::VK_Hexagon_GD_PLT;
75 case HexagonII::MO_IE:
76 RelocationType = MCSymbolRefExpr::VK_Hexagon_IE;
78 case HexagonII::MO_IEGOT:
79 RelocationType = MCSymbolRefExpr::VK_Hexagon_IE_GOT;
81 case HexagonII::MO_TPREL:
82 RelocationType = MCSymbolRefExpr::VK_TPREL;
86 ME = MCSymbolRefExpr::create(Symbol, RelocationType, MC);
88 if (!MO.isJTI() && MO.getOffset())
89 ME = MCBinaryExpr::createAdd(ME, MCConstantExpr::create(MO.getOffset(), MC),
92 ME = HexagonMCExpr::create(ME, MC);
93 HexagonMCInstrInfo::setMustExtend(*ME, MustExtend);
94 return MCOperand::createExpr(ME);
97 // Create an MCInst from a MachineInstr
98 void llvm::HexagonLowerToMC(const MCInstrInfo &MCII, const MachineInstr *MI,
99 MCInst &MCB, HexagonAsmPrinter &AP) {
100 if (MI->getOpcode() == Hexagon::ENDLOOP0) {
101 HexagonMCInstrInfo::setInnerLoop(MCB);
104 if (MI->getOpcode() == Hexagon::ENDLOOP1) {
105 HexagonMCInstrInfo::setOuterLoop(MCB);
108 MCInst *MCI = new (AP.OutContext) MCInst;
109 MCI->setOpcode(MI->getOpcode());
110 assert(MCI->getOpcode() == static_cast<unsigned>(MI->getOpcode()) &&
111 "MCI opcode should have been set on construction");
113 for (unsigned i = 0, e = MI->getNumOperands(); i < e; i++) {
114 const MachineOperand &MO = MI->getOperand(i);
116 bool MustExtend = MO.getTargetFlags() & HexagonII::HMOTF_ConstExtended;
118 switch (MO.getType()) {
121 llvm_unreachable("unknown operand type");
122 case MachineOperand::MO_RegisterMask:
124 case MachineOperand::MO_Register:
125 // Ignore all implicit register operands.
128 MCO = MCOperand::createReg(MO.getReg());
130 case MachineOperand::MO_FPImmediate: {
131 APFloat Val = MO.getFPImm()->getValueAPF();
132 // FP immediates are used only when setting GPRs, so they may be dealt
133 // with like regular immediates from this point on.
134 auto Expr = HexagonMCExpr::create(
135 MCConstantExpr::create(*Val.bitcastToAPInt().getRawData(),
138 HexagonMCInstrInfo::setMustExtend(*Expr, MustExtend);
139 MCO = MCOperand::createExpr(Expr);
142 case MachineOperand::MO_Immediate: {
143 auto Expr = HexagonMCExpr::create(
144 MCConstantExpr::create(MO.getImm(), AP.OutContext), AP.OutContext);
145 HexagonMCInstrInfo::setMustExtend(*Expr, MustExtend);
146 MCO = MCOperand::createExpr(Expr);
149 case MachineOperand::MO_MachineBasicBlock: {
150 MCExpr const *Expr = MCSymbolRefExpr::create(MO.getMBB()->getSymbol(),
152 Expr = HexagonMCExpr::create(Expr, AP.OutContext);
153 HexagonMCInstrInfo::setMustExtend(*Expr, MustExtend);
154 MCO = MCOperand::createExpr(Expr);
157 case MachineOperand::MO_GlobalAddress:
158 MCO = GetSymbolRef(MO, AP.getSymbol(MO.getGlobal()), AP, MustExtend);
160 case MachineOperand::MO_ExternalSymbol:
161 MCO = GetSymbolRef(MO, AP.GetExternalSymbolSymbol(MO.getSymbolName()),
164 case MachineOperand::MO_JumpTableIndex:
165 MCO = GetSymbolRef(MO, AP.GetJTISymbol(MO.getIndex()), AP, MustExtend);
167 case MachineOperand::MO_ConstantPoolIndex:
168 MCO = GetSymbolRef(MO, AP.GetCPISymbol(MO.getIndex()), AP, MustExtend);
170 case MachineOperand::MO_BlockAddress:
171 MCO = GetSymbolRef(MO, AP.GetBlockAddressSymbol(MO.getBlockAddress()), AP,
176 MCI->addOperand(MCO);
178 AP.HexagonProcessInstruction(*MCI, *MI);
179 HexagonMCInstrInfo::extendIfNeeded(AP.OutContext, MCII, MCB, *MCI);
180 MCB.addOperand(MCOperand::createInst(MCI));