blob: a5dc002642c841fdeb53b243eac0184533ed9939 [file] [log] [blame]
Evandro Menezes5cee6212012-04-12 17:55:53 +00001//===- HexagonMCInstLower.cpp - Convert Hexagon MachineInstr to an MCInst -===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains code to lower Hexagon MachineInstrs to their corresponding
11// MCInst records.
12//
13//===----------------------------------------------------------------------===//
14
15#include "Hexagon.h"
16#include "HexagonAsmPrinter.h"
17#include "HexagonMachineFunctionInfo.h"
Colin LeMahieu68d967d2015-05-29 14:44:13 +000018#include "MCTargetDesc/HexagonMCInstrInfo.h"
19
Evandro Menezes5cee6212012-04-12 17:55:53 +000020#include "llvm/CodeGen/MachineBasicBlock.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000021#include "llvm/IR/Constants.h"
Rafael Espindola894843c2014-01-07 21:19:40 +000022#include "llvm/IR/Mangler.h"
Colin LeMahieu68d967d2015-05-29 14:44:13 +000023#include "llvm/MC/MCContext.h"
Evandro Menezes5cee6212012-04-12 17:55:53 +000024#include "llvm/MC/MCExpr.h"
25#include "llvm/MC/MCInst.h"
Evandro Menezes5cee6212012-04-12 17:55:53 +000026
27using namespace llvm;
28
Krzysztof Parzyszek8d8b2292015-12-02 23:08:29 +000029namespace llvm {
30 void HexagonLowerToMC(const MCInstrInfo &MCII, const MachineInstr *MI,
31 MCInst &MCB, HexagonAsmPrinter &AP);
32}
33
34static MCOperand GetSymbolRef(const MachineOperand &MO, const MCSymbol *Symbol,
Colin LeMahieu73cd6862016-02-29 18:39:51 +000035 HexagonAsmPrinter &Printer, bool MustExtend) {
Evandro Menezes5cee6212012-04-12 17:55:53 +000036 MCContext &MC = Printer.OutContext;
37 const MCExpr *ME;
38
Krzysztof Parzyszek8d8b2292015-12-02 23:08:29 +000039 // Populate the relocation type based on Hexagon target flags
40 // set on an operand
41 MCSymbolRefExpr::VariantKind RelocationType;
42 switch (MO.getTargetFlags()) {
43 default:
44 RelocationType = MCSymbolRefExpr::VK_None;
45 break;
46 case HexagonII::MO_PCREL:
47 RelocationType = MCSymbolRefExpr::VK_Hexagon_PCREL;
48 break;
49 case HexagonII::MO_GOT:
50 RelocationType = MCSymbolRefExpr::VK_GOT;
51 break;
52 case HexagonII::MO_LO16:
53 RelocationType = MCSymbolRefExpr::VK_Hexagon_LO16;
54 break;
55 case HexagonII::MO_HI16:
56 RelocationType = MCSymbolRefExpr::VK_Hexagon_HI16;
57 break;
58 case HexagonII::MO_GPREL:
59 RelocationType = MCSymbolRefExpr::VK_Hexagon_GPREL;
60 break;
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +000061 case HexagonII::MO_GDGOT:
62 RelocationType = MCSymbolRefExpr::VK_Hexagon_GD_GOT;
63 break;
64 case HexagonII::MO_GDPLT:
65 RelocationType = MCSymbolRefExpr::VK_Hexagon_GD_PLT;
66 break;
67 case HexagonII::MO_IE:
68 RelocationType = MCSymbolRefExpr::VK_Hexagon_IE;
69 break;
70 case HexagonII::MO_IEGOT:
71 RelocationType = MCSymbolRefExpr::VK_Hexagon_IE_GOT;
72 break;
73 case HexagonII::MO_TPREL:
74 RelocationType = MCSymbolRefExpr::VK_TPREL;
75 break;
Krzysztof Parzyszek8d8b2292015-12-02 23:08:29 +000076 }
77
78 ME = MCSymbolRefExpr::create(Symbol, RelocationType, MC);
Evandro Menezes5cee6212012-04-12 17:55:53 +000079
80 if (!MO.isJTI() && MO.getOffset())
Jim Grosbach13760bd2015-05-30 01:25:56 +000081 ME = MCBinaryExpr::createAdd(ME, MCConstantExpr::create(MO.getOffset(), MC),
Evandro Menezes5cee6212012-04-12 17:55:53 +000082 MC);
83
Colin LeMahieu73cd6862016-02-29 18:39:51 +000084 ME = HexagonMCExpr::create(ME, MC);
85 HexagonMCInstrInfo::setMustExtend(*ME, MustExtend);
86 return MCOperand::createExpr(ME);
Evandro Menezes5cee6212012-04-12 17:55:53 +000087}
88
89// Create an MCInst from a MachineInstr
Krzysztof Parzyszek8d8b2292015-12-02 23:08:29 +000090void llvm::HexagonLowerToMC(const MCInstrInfo &MCII, const MachineInstr *MI,
91 MCInst &MCB, HexagonAsmPrinter &AP) {
92 if (MI->getOpcode() == Hexagon::ENDLOOP0) {
Colin LeMahieu68d967d2015-05-29 14:44:13 +000093 HexagonMCInstrInfo::setInnerLoop(MCB);
94 return;
95 }
Krzysztof Parzyszek8d8b2292015-12-02 23:08:29 +000096 if (MI->getOpcode() == Hexagon::ENDLOOP1) {
Colin LeMahieu68d967d2015-05-29 14:44:13 +000097 HexagonMCInstrInfo::setOuterLoop(MCB);
98 return;
99 }
Krzysztof Parzyszek8d8b2292015-12-02 23:08:29 +0000100 MCInst *MCI = new (AP.OutContext) MCInst;
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000101 MCI->setOpcode(MI->getOpcode());
102 assert(MCI->getOpcode() == static_cast<unsigned>(MI->getOpcode()) &&
103 "MCI opcode should have been set on construction");
Evandro Menezes5cee6212012-04-12 17:55:53 +0000104
105 for (unsigned i = 0, e = MI->getNumOperands(); i < e; i++) {
106 const MachineOperand &MO = MI->getOperand(i);
107 MCOperand MCO;
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000108 bool MustExtend = MO.getTargetFlags() & HexagonII::HMOTF_ConstExtended;
Evandro Menezes5cee6212012-04-12 17:55:53 +0000109
110 switch (MO.getType()) {
111 default:
112 MI->dump();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000113 llvm_unreachable("unknown operand type");
Evandro Menezes5cee6212012-04-12 17:55:53 +0000114 case MachineOperand::MO_Register:
115 // Ignore all implicit register operands.
Colin LeMahieu52418812014-11-04 00:14:36 +0000116 if (MO.isImplicit()) continue;
Jim Grosbache9119e42015-05-13 18:37:00 +0000117 MCO = MCOperand::createReg(MO.getReg());
Evandro Menezes5cee6212012-04-12 17:55:53 +0000118 break;
119 case MachineOperand::MO_FPImmediate: {
120 APFloat Val = MO.getFPImm()->getValueAPF();
121 // FP immediates are used only when setting GPRs, so they may be dealt
122 // with like regular immediates from this point on.
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000123 auto Expr = HexagonMCExpr::create(
124 MCConstantExpr::create(*Val.bitcastToAPInt().getRawData(),
125 AP.OutContext),
126 AP.OutContext);
127 HexagonMCInstrInfo::setMustExtend(*Expr, MustExtend);
128 MCO = MCOperand::createExpr(Expr);
Evandro Menezes5cee6212012-04-12 17:55:53 +0000129 break;
130 }
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000131 case MachineOperand::MO_Immediate: {
132 auto Expr = HexagonMCExpr::create(
133 MCConstantExpr::create(MO.getImm(), AP.OutContext), AP.OutContext);
134 HexagonMCInstrInfo::setMustExtend(*Expr, MustExtend);
135 MCO = MCOperand::createExpr(Expr);
Evandro Menezes5cee6212012-04-12 17:55:53 +0000136 break;
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000137 }
138 case MachineOperand::MO_MachineBasicBlock: {
139 MCExpr const *Expr = MCSymbolRefExpr::create(MO.getMBB()->getSymbol(),
140 AP.OutContext);
141 Expr = HexagonMCExpr::create(Expr, AP.OutContext);
142 HexagonMCInstrInfo::setMustExtend(*Expr, MustExtend);
143 MCO = MCOperand::createExpr(Expr);
Evandro Menezes5cee6212012-04-12 17:55:53 +0000144 break;
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000145 }
Evandro Menezes5cee6212012-04-12 17:55:53 +0000146 case MachineOperand::MO_GlobalAddress:
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000147 MCO = GetSymbolRef(MO, AP.getSymbol(MO.getGlobal()), AP, MustExtend);
Evandro Menezes5cee6212012-04-12 17:55:53 +0000148 break;
149 case MachineOperand::MO_ExternalSymbol:
Colin LeMahieu52418812014-11-04 00:14:36 +0000150 MCO = GetSymbolRef(MO, AP.GetExternalSymbolSymbol(MO.getSymbolName()),
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000151 AP, MustExtend);
Evandro Menezes5cee6212012-04-12 17:55:53 +0000152 break;
153 case MachineOperand::MO_JumpTableIndex:
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000154 MCO = GetSymbolRef(MO, AP.GetJTISymbol(MO.getIndex()), AP, MustExtend);
Evandro Menezes5cee6212012-04-12 17:55:53 +0000155 break;
156 case MachineOperand::MO_ConstantPoolIndex:
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000157 MCO = GetSymbolRef(MO, AP.GetCPISymbol(MO.getIndex()), AP, MustExtend);
Evandro Menezes5cee6212012-04-12 17:55:53 +0000158 break;
159 case MachineOperand::MO_BlockAddress:
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000160 MCO = GetSymbolRef(MO, AP.GetBlockAddressSymbol(MO.getBlockAddress()), AP,
161 MustExtend);
Evandro Menezes5cee6212012-04-12 17:55:53 +0000162 break;
163 }
164
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000165 MCI->addOperand(MCO);
Evandro Menezes5cee6212012-04-12 17:55:53 +0000166 }
Krzysztof Parzyszek372bd802015-12-15 17:05:45 +0000167 AP.HexagonProcessInstruction(*MCI, *MI);
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000168 HexagonMCInstrInfo::extendIfNeeded(AP.OutContext, MCII, MCB, *MCI);
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000169 MCB.addOperand(MCOperand::createInst(MCI));
Evandro Menezes5cee6212012-04-12 17:55:53 +0000170}