xref: /aosp_15_r20/external/llvm/lib/Target/Hexagon/HexagonMCInstLower.cpp (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===- HexagonMCInstLower.cpp - Convert Hexagon MachineInstr to an MCInst -===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This file contains code to lower Hexagon MachineInstrs to their corresponding
11*9880d681SAndroid Build Coastguard Worker // MCInst records.
12*9880d681SAndroid Build Coastguard Worker //
13*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
14*9880d681SAndroid Build Coastguard Worker 
15*9880d681SAndroid Build Coastguard Worker #include "Hexagon.h"
16*9880d681SAndroid Build Coastguard Worker #include "HexagonAsmPrinter.h"
17*9880d681SAndroid Build Coastguard Worker #include "HexagonMachineFunctionInfo.h"
18*9880d681SAndroid Build Coastguard Worker #include "MCTargetDesc/HexagonMCInstrInfo.h"
19*9880d681SAndroid Build Coastguard Worker 
20*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineBasicBlock.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Constants.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Mangler.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/MC/MCContext.h"
24*9880d681SAndroid Build Coastguard Worker #include "llvm/MC/MCExpr.h"
25*9880d681SAndroid Build Coastguard Worker #include "llvm/MC/MCInst.h"
26*9880d681SAndroid Build Coastguard Worker 
27*9880d681SAndroid Build Coastguard Worker using namespace llvm;
28*9880d681SAndroid Build Coastguard Worker 
29*9880d681SAndroid Build Coastguard Worker namespace llvm {
30*9880d681SAndroid Build Coastguard Worker   void HexagonLowerToMC(const MCInstrInfo &MCII, const MachineInstr *MI,
31*9880d681SAndroid Build Coastguard Worker                         MCInst &MCB, HexagonAsmPrinter &AP);
32*9880d681SAndroid Build Coastguard Worker }
33*9880d681SAndroid Build Coastguard Worker 
GetSymbolRef(const MachineOperand & MO,const MCSymbol * Symbol,HexagonAsmPrinter & Printer,bool MustExtend)34*9880d681SAndroid Build Coastguard Worker static MCOperand GetSymbolRef(const MachineOperand &MO, const MCSymbol *Symbol,
35*9880d681SAndroid Build Coastguard Worker                               HexagonAsmPrinter &Printer, bool MustExtend) {
36*9880d681SAndroid Build Coastguard Worker   MCContext &MC = Printer.OutContext;
37*9880d681SAndroid Build Coastguard Worker   const MCExpr *ME;
38*9880d681SAndroid Build Coastguard Worker 
39*9880d681SAndroid Build Coastguard Worker   // Populate the relocation type based on Hexagon target flags
40*9880d681SAndroid Build Coastguard Worker   // set on an operand
41*9880d681SAndroid Build Coastguard Worker   MCSymbolRefExpr::VariantKind RelocationType;
42*9880d681SAndroid Build Coastguard Worker   switch (MO.getTargetFlags()) {
43*9880d681SAndroid Build Coastguard Worker   default:
44*9880d681SAndroid Build Coastguard Worker     RelocationType = MCSymbolRefExpr::VK_None;
45*9880d681SAndroid Build Coastguard Worker     break;
46*9880d681SAndroid Build Coastguard Worker   case HexagonII::MO_PCREL:
47*9880d681SAndroid Build Coastguard Worker     RelocationType = MCSymbolRefExpr::VK_Hexagon_PCREL;
48*9880d681SAndroid Build Coastguard Worker     break;
49*9880d681SAndroid Build Coastguard Worker   case HexagonII::MO_GOT:
50*9880d681SAndroid Build Coastguard Worker     RelocationType = MCSymbolRefExpr::VK_GOT;
51*9880d681SAndroid Build Coastguard Worker     break;
52*9880d681SAndroid Build Coastguard Worker   case HexagonII::MO_LO16:
53*9880d681SAndroid Build Coastguard Worker     RelocationType = MCSymbolRefExpr::VK_Hexagon_LO16;
54*9880d681SAndroid Build Coastguard Worker     break;
55*9880d681SAndroid Build Coastguard Worker   case HexagonII::MO_HI16:
56*9880d681SAndroid Build Coastguard Worker     RelocationType = MCSymbolRefExpr::VK_Hexagon_HI16;
57*9880d681SAndroid Build Coastguard Worker     break;
58*9880d681SAndroid Build Coastguard Worker   case HexagonII::MO_GPREL:
59*9880d681SAndroid Build Coastguard Worker     RelocationType = MCSymbolRefExpr::VK_Hexagon_GPREL;
60*9880d681SAndroid Build Coastguard Worker     break;
61*9880d681SAndroid Build Coastguard Worker   case HexagonII::MO_GDGOT:
62*9880d681SAndroid Build Coastguard Worker     RelocationType = MCSymbolRefExpr::VK_Hexagon_GD_GOT;
63*9880d681SAndroid Build Coastguard Worker     break;
64*9880d681SAndroid Build Coastguard Worker   case HexagonII::MO_GDPLT:
65*9880d681SAndroid Build Coastguard Worker     RelocationType = MCSymbolRefExpr::VK_Hexagon_GD_PLT;
66*9880d681SAndroid Build Coastguard Worker     break;
67*9880d681SAndroid Build Coastguard Worker   case HexagonII::MO_IE:
68*9880d681SAndroid Build Coastguard Worker     RelocationType = MCSymbolRefExpr::VK_Hexagon_IE;
69*9880d681SAndroid Build Coastguard Worker     break;
70*9880d681SAndroid Build Coastguard Worker   case HexagonII::MO_IEGOT:
71*9880d681SAndroid Build Coastguard Worker     RelocationType = MCSymbolRefExpr::VK_Hexagon_IE_GOT;
72*9880d681SAndroid Build Coastguard Worker     break;
73*9880d681SAndroid Build Coastguard Worker   case HexagonII::MO_TPREL:
74*9880d681SAndroid Build Coastguard Worker     RelocationType = MCSymbolRefExpr::VK_TPREL;
75*9880d681SAndroid Build Coastguard Worker     break;
76*9880d681SAndroid Build Coastguard Worker   }
77*9880d681SAndroid Build Coastguard Worker 
78*9880d681SAndroid Build Coastguard Worker   ME = MCSymbolRefExpr::create(Symbol, RelocationType, MC);
79*9880d681SAndroid Build Coastguard Worker 
80*9880d681SAndroid Build Coastguard Worker   if (!MO.isJTI() && MO.getOffset())
81*9880d681SAndroid Build Coastguard Worker     ME = MCBinaryExpr::createAdd(ME, MCConstantExpr::create(MO.getOffset(), MC),
82*9880d681SAndroid Build Coastguard Worker                                  MC);
83*9880d681SAndroid Build Coastguard Worker 
84*9880d681SAndroid Build Coastguard Worker   ME = HexagonMCExpr::create(ME, MC);
85*9880d681SAndroid Build Coastguard Worker   HexagonMCInstrInfo::setMustExtend(*ME, MustExtend);
86*9880d681SAndroid Build Coastguard Worker   return MCOperand::createExpr(ME);
87*9880d681SAndroid Build Coastguard Worker }
88*9880d681SAndroid Build Coastguard Worker 
89*9880d681SAndroid Build Coastguard Worker // Create an MCInst from a MachineInstr
HexagonLowerToMC(const MCInstrInfo & MCII,const MachineInstr * MI,MCInst & MCB,HexagonAsmPrinter & AP)90*9880d681SAndroid Build Coastguard Worker void llvm::HexagonLowerToMC(const MCInstrInfo &MCII, const MachineInstr *MI,
91*9880d681SAndroid Build Coastguard Worker                             MCInst &MCB, HexagonAsmPrinter &AP) {
92*9880d681SAndroid Build Coastguard Worker   if (MI->getOpcode() == Hexagon::ENDLOOP0) {
93*9880d681SAndroid Build Coastguard Worker     HexagonMCInstrInfo::setInnerLoop(MCB);
94*9880d681SAndroid Build Coastguard Worker     return;
95*9880d681SAndroid Build Coastguard Worker   }
96*9880d681SAndroid Build Coastguard Worker   if (MI->getOpcode() == Hexagon::ENDLOOP1) {
97*9880d681SAndroid Build Coastguard Worker     HexagonMCInstrInfo::setOuterLoop(MCB);
98*9880d681SAndroid Build Coastguard Worker     return;
99*9880d681SAndroid Build Coastguard Worker   }
100*9880d681SAndroid Build Coastguard Worker   MCInst *MCI = new (AP.OutContext) MCInst;
101*9880d681SAndroid Build Coastguard Worker   MCI->setOpcode(MI->getOpcode());
102*9880d681SAndroid Build Coastguard Worker   assert(MCI->getOpcode() == static_cast<unsigned>(MI->getOpcode()) &&
103*9880d681SAndroid Build Coastguard Worker          "MCI opcode should have been set on construction");
104*9880d681SAndroid Build Coastguard Worker 
105*9880d681SAndroid Build Coastguard Worker   for (unsigned i = 0, e = MI->getNumOperands(); i < e; i++) {
106*9880d681SAndroid Build Coastguard Worker     const MachineOperand &MO = MI->getOperand(i);
107*9880d681SAndroid Build Coastguard Worker     MCOperand MCO;
108*9880d681SAndroid Build Coastguard Worker     bool MustExtend = MO.getTargetFlags() & HexagonII::HMOTF_ConstExtended;
109*9880d681SAndroid Build Coastguard Worker 
110*9880d681SAndroid Build Coastguard Worker     switch (MO.getType()) {
111*9880d681SAndroid Build Coastguard Worker     default:
112*9880d681SAndroid Build Coastguard Worker       MI->dump();
113*9880d681SAndroid Build Coastguard Worker       llvm_unreachable("unknown operand type");
114*9880d681SAndroid Build Coastguard Worker     case MachineOperand::MO_Register:
115*9880d681SAndroid Build Coastguard Worker       // Ignore all implicit register operands.
116*9880d681SAndroid Build Coastguard Worker       if (MO.isImplicit()) continue;
117*9880d681SAndroid Build Coastguard Worker       MCO = MCOperand::createReg(MO.getReg());
118*9880d681SAndroid Build Coastguard Worker       break;
119*9880d681SAndroid Build Coastguard Worker     case MachineOperand::MO_FPImmediate: {
120*9880d681SAndroid Build Coastguard Worker       APFloat Val = MO.getFPImm()->getValueAPF();
121*9880d681SAndroid Build Coastguard Worker       // FP immediates are used only when setting GPRs, so they may be dealt
122*9880d681SAndroid Build Coastguard Worker       // with like regular immediates from this point on.
123*9880d681SAndroid Build Coastguard Worker       auto Expr = HexagonMCExpr::create(
124*9880d681SAndroid Build Coastguard Worker           MCConstantExpr::create(*Val.bitcastToAPInt().getRawData(),
125*9880d681SAndroid Build Coastguard Worker                                  AP.OutContext),
126*9880d681SAndroid Build Coastguard Worker           AP.OutContext);
127*9880d681SAndroid Build Coastguard Worker       HexagonMCInstrInfo::setMustExtend(*Expr, MustExtend);
128*9880d681SAndroid Build Coastguard Worker       MCO = MCOperand::createExpr(Expr);
129*9880d681SAndroid Build Coastguard Worker       break;
130*9880d681SAndroid Build Coastguard Worker     }
131*9880d681SAndroid Build Coastguard Worker     case MachineOperand::MO_Immediate: {
132*9880d681SAndroid Build Coastguard Worker       auto Expr = HexagonMCExpr::create(
133*9880d681SAndroid Build Coastguard Worker           MCConstantExpr::create(MO.getImm(), AP.OutContext), AP.OutContext);
134*9880d681SAndroid Build Coastguard Worker       HexagonMCInstrInfo::setMustExtend(*Expr, MustExtend);
135*9880d681SAndroid Build Coastguard Worker       MCO = MCOperand::createExpr(Expr);
136*9880d681SAndroid Build Coastguard Worker       break;
137*9880d681SAndroid Build Coastguard Worker     }
138*9880d681SAndroid Build Coastguard Worker     case MachineOperand::MO_MachineBasicBlock: {
139*9880d681SAndroid Build Coastguard Worker       MCExpr const *Expr = MCSymbolRefExpr::create(MO.getMBB()->getSymbol(),
140*9880d681SAndroid Build Coastguard Worker                                                    AP.OutContext);
141*9880d681SAndroid Build Coastguard Worker       Expr = HexagonMCExpr::create(Expr, AP.OutContext);
142*9880d681SAndroid Build Coastguard Worker       HexagonMCInstrInfo::setMustExtend(*Expr, MustExtend);
143*9880d681SAndroid Build Coastguard Worker       MCO = MCOperand::createExpr(Expr);
144*9880d681SAndroid Build Coastguard Worker       break;
145*9880d681SAndroid Build Coastguard Worker     }
146*9880d681SAndroid Build Coastguard Worker     case MachineOperand::MO_GlobalAddress:
147*9880d681SAndroid Build Coastguard Worker       MCO = GetSymbolRef(MO, AP.getSymbol(MO.getGlobal()), AP, MustExtend);
148*9880d681SAndroid Build Coastguard Worker       break;
149*9880d681SAndroid Build Coastguard Worker     case MachineOperand::MO_ExternalSymbol:
150*9880d681SAndroid Build Coastguard Worker       MCO = GetSymbolRef(MO, AP.GetExternalSymbolSymbol(MO.getSymbolName()),
151*9880d681SAndroid Build Coastguard Worker                          AP, MustExtend);
152*9880d681SAndroid Build Coastguard Worker       break;
153*9880d681SAndroid Build Coastguard Worker     case MachineOperand::MO_JumpTableIndex:
154*9880d681SAndroid Build Coastguard Worker       MCO = GetSymbolRef(MO, AP.GetJTISymbol(MO.getIndex()), AP, MustExtend);
155*9880d681SAndroid Build Coastguard Worker       break;
156*9880d681SAndroid Build Coastguard Worker     case MachineOperand::MO_ConstantPoolIndex:
157*9880d681SAndroid Build Coastguard Worker       MCO = GetSymbolRef(MO, AP.GetCPISymbol(MO.getIndex()), AP, MustExtend);
158*9880d681SAndroid Build Coastguard Worker       break;
159*9880d681SAndroid Build Coastguard Worker     case MachineOperand::MO_BlockAddress:
160*9880d681SAndroid Build Coastguard Worker       MCO = GetSymbolRef(MO, AP.GetBlockAddressSymbol(MO.getBlockAddress()), AP,
161*9880d681SAndroid Build Coastguard Worker                          MustExtend);
162*9880d681SAndroid Build Coastguard Worker       break;
163*9880d681SAndroid Build Coastguard Worker     }
164*9880d681SAndroid Build Coastguard Worker 
165*9880d681SAndroid Build Coastguard Worker     MCI->addOperand(MCO);
166*9880d681SAndroid Build Coastguard Worker   }
167*9880d681SAndroid Build Coastguard Worker   AP.HexagonProcessInstruction(*MCI, *MI);
168*9880d681SAndroid Build Coastguard Worker   HexagonMCInstrInfo::extendIfNeeded(AP.OutContext, MCII, MCB, *MCI);
169*9880d681SAndroid Build Coastguard Worker   MCB.addOperand(MCOperand::createInst(MCI));
170*9880d681SAndroid Build Coastguard Worker }
171