blob: d1a153920e5e0556b5369ddedbb634a47cc954e1 [file] [log] [blame]
Evandro Menezes5cee6212012-04-12 17:55:53 +00001//===- HexagonMCInstLower.cpp - Convert Hexagon MachineInstr to an MCInst -===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Evandro Menezes5cee6212012-04-12 17:55:53 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file contains code to lower Hexagon MachineInstrs to their corresponding
10// MCInst records.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Hexagon.h"
15#include "HexagonAsmPrinter.h"
Eugene Zelenko52889212017-08-01 21:20:10 +000016#include "MCTargetDesc/HexagonMCExpr.h"
Colin LeMahieu68d967d2015-05-29 14:44:13 +000017#include "MCTargetDesc/HexagonMCInstrInfo.h"
Eugene Zelenko52889212017-08-01 21:20:10 +000018#include "MCTargetDesc/HexagonMCTargetDesc.h"
19#include "llvm/ADT/APFloat.h"
20#include "llvm/ADT/APInt.h"
Evandro Menezes5cee6212012-04-12 17:55:53 +000021#include "llvm/CodeGen/MachineBasicBlock.h"
Eugene Zelenko52889212017-08-01 21:20:10 +000022#include "llvm/CodeGen/MachineInstr.h"
23#include "llvm/CodeGen/MachineOperand.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000024#include "llvm/IR/Constants.h"
Colin LeMahieu68d967d2015-05-29 14:44:13 +000025#include "llvm/MC/MCContext.h"
Evandro Menezes5cee6212012-04-12 17:55:53 +000026#include "llvm/MC/MCExpr.h"
27#include "llvm/MC/MCInst.h"
Eugene Zelenko52889212017-08-01 21:20:10 +000028#include "llvm/Support/ErrorHandling.h"
29#include "llvm/Support/raw_ostream.h"
30#include <cassert>
Evandro Menezes5cee6212012-04-12 17:55:53 +000031
32using namespace llvm;
33
Krzysztof Parzyszek8d8b2292015-12-02 23:08:29 +000034namespace llvm {
Eugene Zelenko52889212017-08-01 21:20:10 +000035
36void HexagonLowerToMC(const MCInstrInfo &MCII, const MachineInstr *MI,
37 MCInst &MCB, HexagonAsmPrinter &AP);
38
39} // end namespace llvm
Krzysztof Parzyszek8d8b2292015-12-02 23:08:29 +000040
41static MCOperand GetSymbolRef(const MachineOperand &MO, const MCSymbol *Symbol,
Colin LeMahieu73cd6862016-02-29 18:39:51 +000042 HexagonAsmPrinter &Printer, bool MustExtend) {
Evandro Menezes5cee6212012-04-12 17:55:53 +000043 MCContext &MC = Printer.OutContext;
44 const MCExpr *ME;
45
Krzysztof Parzyszek8d8b2292015-12-02 23:08:29 +000046 // Populate the relocation type based on Hexagon target flags
47 // set on an operand
48 MCSymbolRefExpr::VariantKind RelocationType;
Krzysztof Parzyszeka7503832017-05-02 18:15:33 +000049 switch (MO.getTargetFlags() & ~HexagonII::HMOTF_ConstExtended) {
Krzysztof Parzyszek8d8b2292015-12-02 23:08:29 +000050 default:
51 RelocationType = MCSymbolRefExpr::VK_None;
52 break;
53 case HexagonII::MO_PCREL:
54 RelocationType = MCSymbolRefExpr::VK_Hexagon_PCREL;
55 break;
56 case HexagonII::MO_GOT:
57 RelocationType = MCSymbolRefExpr::VK_GOT;
58 break;
59 case HexagonII::MO_LO16:
60 RelocationType = MCSymbolRefExpr::VK_Hexagon_LO16;
61 break;
62 case HexagonII::MO_HI16:
63 RelocationType = MCSymbolRefExpr::VK_Hexagon_HI16;
64 break;
65 case HexagonII::MO_GPREL:
66 RelocationType = MCSymbolRefExpr::VK_Hexagon_GPREL;
67 break;
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +000068 case HexagonII::MO_GDGOT:
69 RelocationType = MCSymbolRefExpr::VK_Hexagon_GD_GOT;
70 break;
71 case HexagonII::MO_GDPLT:
72 RelocationType = MCSymbolRefExpr::VK_Hexagon_GD_PLT;
73 break;
74 case HexagonII::MO_IE:
75 RelocationType = MCSymbolRefExpr::VK_Hexagon_IE;
76 break;
77 case HexagonII::MO_IEGOT:
78 RelocationType = MCSymbolRefExpr::VK_Hexagon_IE_GOT;
79 break;
80 case HexagonII::MO_TPREL:
81 RelocationType = MCSymbolRefExpr::VK_TPREL;
82 break;
Krzysztof Parzyszek8d8b2292015-12-02 23:08:29 +000083 }
84
85 ME = MCSymbolRefExpr::create(Symbol, RelocationType, MC);
Evandro Menezes5cee6212012-04-12 17:55:53 +000086
87 if (!MO.isJTI() && MO.getOffset())
Jim Grosbach13760bd2015-05-30 01:25:56 +000088 ME = MCBinaryExpr::createAdd(ME, MCConstantExpr::create(MO.getOffset(), MC),
Evandro Menezes5cee6212012-04-12 17:55:53 +000089 MC);
90
Colin LeMahieu73cd6862016-02-29 18:39:51 +000091 ME = HexagonMCExpr::create(ME, MC);
92 HexagonMCInstrInfo::setMustExtend(*ME, MustExtend);
93 return MCOperand::createExpr(ME);
Evandro Menezes5cee6212012-04-12 17:55:53 +000094}
95
96// Create an MCInst from a MachineInstr
Krzysztof Parzyszek8d8b2292015-12-02 23:08:29 +000097void llvm::HexagonLowerToMC(const MCInstrInfo &MCII, const MachineInstr *MI,
98 MCInst &MCB, HexagonAsmPrinter &AP) {
99 if (MI->getOpcode() == Hexagon::ENDLOOP0) {
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000100 HexagonMCInstrInfo::setInnerLoop(MCB);
101 return;
102 }
Krzysztof Parzyszek8d8b2292015-12-02 23:08:29 +0000103 if (MI->getOpcode() == Hexagon::ENDLOOP1) {
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000104 HexagonMCInstrInfo::setOuterLoop(MCB);
105 return;
106 }
Krzysztof Parzyszek8d8b2292015-12-02 23:08:29 +0000107 MCInst *MCI = new (AP.OutContext) MCInst;
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000108 MCI->setOpcode(MI->getOpcode());
109 assert(MCI->getOpcode() == static_cast<unsigned>(MI->getOpcode()) &&
110 "MCI opcode should have been set on construction");
Evandro Menezes5cee6212012-04-12 17:55:53 +0000111
112 for (unsigned i = 0, e = MI->getNumOperands(); i < e; i++) {
113 const MachineOperand &MO = MI->getOperand(i);
114 MCOperand MCO;
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000115 bool MustExtend = MO.getTargetFlags() & HexagonII::HMOTF_ConstExtended;
Evandro Menezes5cee6212012-04-12 17:55:53 +0000116
117 switch (MO.getType()) {
118 default:
Matthias Braun8c209aa2017-01-28 02:02:38 +0000119 MI->print(errs());
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000120 llvm_unreachable("unknown operand type");
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000121 case MachineOperand::MO_RegisterMask:
122 continue;
Evandro Menezes5cee6212012-04-12 17:55:53 +0000123 case MachineOperand::MO_Register:
124 // Ignore all implicit register operands.
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000125 if (MO.isImplicit())
126 continue;
Jim Grosbache9119e42015-05-13 18:37:00 +0000127 MCO = MCOperand::createReg(MO.getReg());
Evandro Menezes5cee6212012-04-12 17:55:53 +0000128 break;
129 case MachineOperand::MO_FPImmediate: {
130 APFloat Val = MO.getFPImm()->getValueAPF();
131 // FP immediates are used only when setting GPRs, so they may be dealt
132 // with like regular immediates from this point on.
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000133 auto Expr = HexagonMCExpr::create(
Renato Golin4abfb3d2017-04-23 12:15:30 +0000134 MCConstantExpr::create(*Val.bitcastToAPInt().getRawData(),
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000135 AP.OutContext),
136 AP.OutContext);
137 HexagonMCInstrInfo::setMustExtend(*Expr, MustExtend);
138 MCO = MCOperand::createExpr(Expr);
Evandro Menezes5cee6212012-04-12 17:55:53 +0000139 break;
140 }
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000141 case MachineOperand::MO_Immediate: {
142 auto Expr = HexagonMCExpr::create(
143 MCConstantExpr::create(MO.getImm(), AP.OutContext), AP.OutContext);
144 HexagonMCInstrInfo::setMustExtend(*Expr, MustExtend);
145 MCO = MCOperand::createExpr(Expr);
Evandro Menezes5cee6212012-04-12 17:55:53 +0000146 break;
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000147 }
148 case MachineOperand::MO_MachineBasicBlock: {
149 MCExpr const *Expr = MCSymbolRefExpr::create(MO.getMBB()->getSymbol(),
150 AP.OutContext);
151 Expr = HexagonMCExpr::create(Expr, AP.OutContext);
152 HexagonMCInstrInfo::setMustExtend(*Expr, MustExtend);
153 MCO = MCOperand::createExpr(Expr);
Evandro Menezes5cee6212012-04-12 17:55:53 +0000154 break;
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000155 }
Evandro Menezes5cee6212012-04-12 17:55:53 +0000156 case MachineOperand::MO_GlobalAddress:
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000157 MCO = GetSymbolRef(MO, AP.getSymbol(MO.getGlobal()), AP, MustExtend);
Evandro Menezes5cee6212012-04-12 17:55:53 +0000158 break;
159 case MachineOperand::MO_ExternalSymbol:
Colin LeMahieu52418812014-11-04 00:14:36 +0000160 MCO = GetSymbolRef(MO, AP.GetExternalSymbolSymbol(MO.getSymbolName()),
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000161 AP, MustExtend);
Evandro Menezes5cee6212012-04-12 17:55:53 +0000162 break;
163 case MachineOperand::MO_JumpTableIndex:
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000164 MCO = GetSymbolRef(MO, AP.GetJTISymbol(MO.getIndex()), AP, MustExtend);
Evandro Menezes5cee6212012-04-12 17:55:53 +0000165 break;
166 case MachineOperand::MO_ConstantPoolIndex:
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000167 MCO = GetSymbolRef(MO, AP.GetCPISymbol(MO.getIndex()), AP, MustExtend);
Evandro Menezes5cee6212012-04-12 17:55:53 +0000168 break;
169 case MachineOperand::MO_BlockAddress:
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000170 MCO = GetSymbolRef(MO, AP.GetBlockAddressSymbol(MO.getBlockAddress()), AP,
171 MustExtend);
Evandro Menezes5cee6212012-04-12 17:55:53 +0000172 break;
173 }
174
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000175 MCI->addOperand(MCO);
Evandro Menezes5cee6212012-04-12 17:55:53 +0000176 }
Krzysztof Parzyszek372bd802015-12-15 17:05:45 +0000177 AP.HexagonProcessInstruction(*MCI, *MI);
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000178 HexagonMCInstrInfo::extendIfNeeded(AP.OutContext, MCII, MCB, *MCI);
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000179 MCB.addOperand(MCOperand::createInst(MCI));
Evandro Menezes5cee6212012-04-12 17:55:53 +0000180}