blob: fb5752ade1debd8fa7549c028c31d5cfb6e24ce1 [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"
Eugene Zelenko52889212017-08-01 21:20:10 +000017#include "MCTargetDesc/HexagonMCExpr.h"
Colin LeMahieu68d967d2015-05-29 14:44:13 +000018#include "MCTargetDesc/HexagonMCInstrInfo.h"
Eugene Zelenko52889212017-08-01 21:20:10 +000019#include "MCTargetDesc/HexagonMCTargetDesc.h"
20#include "llvm/ADT/APFloat.h"
21#include "llvm/ADT/APInt.h"
Evandro Menezes5cee6212012-04-12 17:55:53 +000022#include "llvm/CodeGen/MachineBasicBlock.h"
Eugene Zelenko52889212017-08-01 21:20:10 +000023#include "llvm/CodeGen/MachineInstr.h"
24#include "llvm/CodeGen/MachineOperand.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000025#include "llvm/IR/Constants.h"
Colin LeMahieu68d967d2015-05-29 14:44:13 +000026#include "llvm/MC/MCContext.h"
Evandro Menezes5cee6212012-04-12 17:55:53 +000027#include "llvm/MC/MCExpr.h"
28#include "llvm/MC/MCInst.h"
Eugene Zelenko52889212017-08-01 21:20:10 +000029#include "llvm/Support/ErrorHandling.h"
30#include "llvm/Support/raw_ostream.h"
31#include <cassert>
Evandro Menezes5cee6212012-04-12 17:55:53 +000032
33using namespace llvm;
34
Krzysztof Parzyszek8d8b2292015-12-02 23:08:29 +000035namespace llvm {
Eugene Zelenko52889212017-08-01 21:20:10 +000036
37void HexagonLowerToMC(const MCInstrInfo &MCII, const MachineInstr *MI,
38 MCInst &MCB, HexagonAsmPrinter &AP);
39
40} // end namespace llvm
Krzysztof Parzyszek8d8b2292015-12-02 23:08:29 +000041
42static MCOperand GetSymbolRef(const MachineOperand &MO, const MCSymbol *Symbol,
Colin LeMahieu73cd6862016-02-29 18:39:51 +000043 HexagonAsmPrinter &Printer, bool MustExtend) {
Evandro Menezes5cee6212012-04-12 17:55:53 +000044 MCContext &MC = Printer.OutContext;
45 const MCExpr *ME;
46
Krzysztof Parzyszek8d8b2292015-12-02 23:08:29 +000047 // Populate the relocation type based on Hexagon target flags
48 // set on an operand
49 MCSymbolRefExpr::VariantKind RelocationType;
Krzysztof Parzyszeka7503832017-05-02 18:15:33 +000050 switch (MO.getTargetFlags() & ~HexagonII::HMOTF_ConstExtended) {
Krzysztof Parzyszek8d8b2292015-12-02 23:08:29 +000051 default:
52 RelocationType = MCSymbolRefExpr::VK_None;
53 break;
54 case HexagonII::MO_PCREL:
55 RelocationType = MCSymbolRefExpr::VK_Hexagon_PCREL;
56 break;
57 case HexagonII::MO_GOT:
58 RelocationType = MCSymbolRefExpr::VK_GOT;
59 break;
60 case HexagonII::MO_LO16:
61 RelocationType = MCSymbolRefExpr::VK_Hexagon_LO16;
62 break;
63 case HexagonII::MO_HI16:
64 RelocationType = MCSymbolRefExpr::VK_Hexagon_HI16;
65 break;
66 case HexagonII::MO_GPREL:
67 RelocationType = MCSymbolRefExpr::VK_Hexagon_GPREL;
68 break;
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +000069 case HexagonII::MO_GDGOT:
70 RelocationType = MCSymbolRefExpr::VK_Hexagon_GD_GOT;
71 break;
72 case HexagonII::MO_GDPLT:
73 RelocationType = MCSymbolRefExpr::VK_Hexagon_GD_PLT;
74 break;
75 case HexagonII::MO_IE:
76 RelocationType = MCSymbolRefExpr::VK_Hexagon_IE;
77 break;
78 case HexagonII::MO_IEGOT:
79 RelocationType = MCSymbolRefExpr::VK_Hexagon_IE_GOT;
80 break;
81 case HexagonII::MO_TPREL:
82 RelocationType = MCSymbolRefExpr::VK_TPREL;
83 break;
Krzysztof Parzyszek8d8b2292015-12-02 23:08:29 +000084 }
85
86 ME = MCSymbolRefExpr::create(Symbol, RelocationType, MC);
Evandro Menezes5cee6212012-04-12 17:55:53 +000087
88 if (!MO.isJTI() && MO.getOffset())
Jim Grosbach13760bd2015-05-30 01:25:56 +000089 ME = MCBinaryExpr::createAdd(ME, MCConstantExpr::create(MO.getOffset(), MC),
Evandro Menezes5cee6212012-04-12 17:55:53 +000090 MC);
91
Colin LeMahieu73cd6862016-02-29 18:39:51 +000092 ME = HexagonMCExpr::create(ME, MC);
93 HexagonMCInstrInfo::setMustExtend(*ME, MustExtend);
94 return MCOperand::createExpr(ME);
Evandro Menezes5cee6212012-04-12 17:55:53 +000095}
96
97// Create an MCInst from a MachineInstr
Krzysztof Parzyszek8d8b2292015-12-02 23:08:29 +000098void llvm::HexagonLowerToMC(const MCInstrInfo &MCII, const MachineInstr *MI,
99 MCInst &MCB, HexagonAsmPrinter &AP) {
100 if (MI->getOpcode() == Hexagon::ENDLOOP0) {
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000101 HexagonMCInstrInfo::setInnerLoop(MCB);
102 return;
103 }
Krzysztof Parzyszek8d8b2292015-12-02 23:08:29 +0000104 if (MI->getOpcode() == Hexagon::ENDLOOP1) {
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000105 HexagonMCInstrInfo::setOuterLoop(MCB);
106 return;
107 }
Krzysztof Parzyszek8d8b2292015-12-02 23:08:29 +0000108 MCInst *MCI = new (AP.OutContext) MCInst;
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000109 MCI->setOpcode(MI->getOpcode());
110 assert(MCI->getOpcode() == static_cast<unsigned>(MI->getOpcode()) &&
111 "MCI opcode should have been set on construction");
Evandro Menezes5cee6212012-04-12 17:55:53 +0000112
113 for (unsigned i = 0, e = MI->getNumOperands(); i < e; i++) {
114 const MachineOperand &MO = MI->getOperand(i);
115 MCOperand MCO;
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000116 bool MustExtend = MO.getTargetFlags() & HexagonII::HMOTF_ConstExtended;
Evandro Menezes5cee6212012-04-12 17:55:53 +0000117
118 switch (MO.getType()) {
119 default:
Matthias Braun8c209aa2017-01-28 02:02:38 +0000120 MI->print(errs());
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000121 llvm_unreachable("unknown operand type");
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000122 case MachineOperand::MO_RegisterMask:
123 continue;
Evandro Menezes5cee6212012-04-12 17:55:53 +0000124 case MachineOperand::MO_Register:
125 // Ignore all implicit register operands.
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000126 if (MO.isImplicit())
127 continue;
Jim Grosbache9119e42015-05-13 18:37:00 +0000128 MCO = MCOperand::createReg(MO.getReg());
Evandro Menezes5cee6212012-04-12 17:55:53 +0000129 break;
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.
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000134 auto Expr = HexagonMCExpr::create(
Renato Golin4abfb3d2017-04-23 12:15:30 +0000135 MCConstantExpr::create(*Val.bitcastToAPInt().getRawData(),
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000136 AP.OutContext),
137 AP.OutContext);
138 HexagonMCInstrInfo::setMustExtend(*Expr, MustExtend);
139 MCO = MCOperand::createExpr(Expr);
Evandro Menezes5cee6212012-04-12 17:55:53 +0000140 break;
141 }
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000142 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);
Evandro Menezes5cee6212012-04-12 17:55:53 +0000147 break;
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000148 }
149 case MachineOperand::MO_MachineBasicBlock: {
150 MCExpr const *Expr = MCSymbolRefExpr::create(MO.getMBB()->getSymbol(),
151 AP.OutContext);
152 Expr = HexagonMCExpr::create(Expr, AP.OutContext);
153 HexagonMCInstrInfo::setMustExtend(*Expr, MustExtend);
154 MCO = MCOperand::createExpr(Expr);
Evandro Menezes5cee6212012-04-12 17:55:53 +0000155 break;
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000156 }
Evandro Menezes5cee6212012-04-12 17:55:53 +0000157 case MachineOperand::MO_GlobalAddress:
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000158 MCO = GetSymbolRef(MO, AP.getSymbol(MO.getGlobal()), AP, MustExtend);
Evandro Menezes5cee6212012-04-12 17:55:53 +0000159 break;
160 case MachineOperand::MO_ExternalSymbol:
Colin LeMahieu52418812014-11-04 00:14:36 +0000161 MCO = GetSymbolRef(MO, AP.GetExternalSymbolSymbol(MO.getSymbolName()),
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000162 AP, MustExtend);
Evandro Menezes5cee6212012-04-12 17:55:53 +0000163 break;
164 case MachineOperand::MO_JumpTableIndex:
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000165 MCO = GetSymbolRef(MO, AP.GetJTISymbol(MO.getIndex()), AP, MustExtend);
Evandro Menezes5cee6212012-04-12 17:55:53 +0000166 break;
167 case MachineOperand::MO_ConstantPoolIndex:
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000168 MCO = GetSymbolRef(MO, AP.GetCPISymbol(MO.getIndex()), AP, MustExtend);
Evandro Menezes5cee6212012-04-12 17:55:53 +0000169 break;
170 case MachineOperand::MO_BlockAddress:
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000171 MCO = GetSymbolRef(MO, AP.GetBlockAddressSymbol(MO.getBlockAddress()), AP,
172 MustExtend);
Evandro Menezes5cee6212012-04-12 17:55:53 +0000173 break;
174 }
175
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000176 MCI->addOperand(MCO);
Evandro Menezes5cee6212012-04-12 17:55:53 +0000177 }
Krzysztof Parzyszek372bd802015-12-15 17:05:45 +0000178 AP.HexagonProcessInstruction(*MCI, *MI);
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000179 HexagonMCInstrInfo::extendIfNeeded(AP.OutContext, MCII, MCB, *MCI);
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000180 MCB.addOperand(MCOperand::createInst(MCI));
Evandro Menezes5cee6212012-04-12 17:55:53 +0000181}