blob: 86d9e19c05474a32aa6f8a8908d7bea57d46798b [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,
35 HexagonAsmPrinter &Printer) {
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;
61 }
62
63 ME = MCSymbolRefExpr::create(Symbol, RelocationType, MC);
Evandro Menezes5cee6212012-04-12 17:55:53 +000064
65 if (!MO.isJTI() && MO.getOffset())
Jim Grosbach13760bd2015-05-30 01:25:56 +000066 ME = MCBinaryExpr::createAdd(ME, MCConstantExpr::create(MO.getOffset(), MC),
Evandro Menezes5cee6212012-04-12 17:55:53 +000067 MC);
68
Krzysztof Parzyszek8d8b2292015-12-02 23:08:29 +000069 return MCOperand::createExpr(ME);
Evandro Menezes5cee6212012-04-12 17:55:53 +000070}
71
72// Create an MCInst from a MachineInstr
Krzysztof Parzyszek8d8b2292015-12-02 23:08:29 +000073void llvm::HexagonLowerToMC(const MCInstrInfo &MCII, const MachineInstr *MI,
74 MCInst &MCB, HexagonAsmPrinter &AP) {
75 if (MI->getOpcode() == Hexagon::ENDLOOP0) {
Colin LeMahieu68d967d2015-05-29 14:44:13 +000076 HexagonMCInstrInfo::setInnerLoop(MCB);
77 return;
78 }
Krzysztof Parzyszek8d8b2292015-12-02 23:08:29 +000079 if (MI->getOpcode() == Hexagon::ENDLOOP1) {
Colin LeMahieu68d967d2015-05-29 14:44:13 +000080 HexagonMCInstrInfo::setOuterLoop(MCB);
81 return;
82 }
Krzysztof Parzyszek8d8b2292015-12-02 23:08:29 +000083 MCInst *MCI = new (AP.OutContext) MCInst;
Colin LeMahieu68d967d2015-05-29 14:44:13 +000084 MCI->setOpcode(MI->getOpcode());
85 assert(MCI->getOpcode() == static_cast<unsigned>(MI->getOpcode()) &&
86 "MCI opcode should have been set on construction");
Krzysztof Parzyszek8d8b2292015-12-02 23:08:29 +000087 bool MustExtend = false;
Evandro Menezes5cee6212012-04-12 17:55:53 +000088
89 for (unsigned i = 0, e = MI->getNumOperands(); i < e; i++) {
90 const MachineOperand &MO = MI->getOperand(i);
91 MCOperand MCO;
Krzysztof Parzyszek8d8b2292015-12-02 23:08:29 +000092 if (MO.getTargetFlags() & HexagonII::HMOTF_ConstExtended)
93 MustExtend = true;
Evandro Menezes5cee6212012-04-12 17:55:53 +000094
95 switch (MO.getType()) {
96 default:
97 MI->dump();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000098 llvm_unreachable("unknown operand type");
Evandro Menezes5cee6212012-04-12 17:55:53 +000099 case MachineOperand::MO_Register:
100 // Ignore all implicit register operands.
Colin LeMahieu52418812014-11-04 00:14:36 +0000101 if (MO.isImplicit()) continue;
Jim Grosbache9119e42015-05-13 18:37:00 +0000102 MCO = MCOperand::createReg(MO.getReg());
Evandro Menezes5cee6212012-04-12 17:55:53 +0000103 break;
104 case MachineOperand::MO_FPImmediate: {
105 APFloat Val = MO.getFPImm()->getValueAPF();
106 // FP immediates are used only when setting GPRs, so they may be dealt
107 // with like regular immediates from this point on.
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000108 MCO = MCOperand::createExpr(
109 MCConstantExpr::create(*Val.bitcastToAPInt().getRawData(),
110 AP.OutContext));
Evandro Menezes5cee6212012-04-12 17:55:53 +0000111 break;
112 }
113 case MachineOperand::MO_Immediate:
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000114 MCO = MCOperand::createExpr(
115 MCConstantExpr::create(MO.getImm(), AP.OutContext));
Evandro Menezes5cee6212012-04-12 17:55:53 +0000116 break;
117 case MachineOperand::MO_MachineBasicBlock:
Jim Grosbache9119e42015-05-13 18:37:00 +0000118 MCO = MCOperand::createExpr
Jim Grosbach13760bd2015-05-30 01:25:56 +0000119 (MCSymbolRefExpr::create(MO.getMBB()->getSymbol(),
Colin LeMahieu52418812014-11-04 00:14:36 +0000120 AP.OutContext));
Evandro Menezes5cee6212012-04-12 17:55:53 +0000121 break;
122 case MachineOperand::MO_GlobalAddress:
Rafael Espindola79858aa2013-10-29 17:07:16 +0000123 MCO = GetSymbolRef(MO, AP.getSymbol(MO.getGlobal()), AP);
Evandro Menezes5cee6212012-04-12 17:55:53 +0000124 break;
125 case MachineOperand::MO_ExternalSymbol:
Colin LeMahieu52418812014-11-04 00:14:36 +0000126 MCO = GetSymbolRef(MO, AP.GetExternalSymbolSymbol(MO.getSymbolName()),
127 AP);
Evandro Menezes5cee6212012-04-12 17:55:53 +0000128 break;
129 case MachineOperand::MO_JumpTableIndex:
130 MCO = GetSymbolRef(MO, AP.GetJTISymbol(MO.getIndex()), AP);
131 break;
132 case MachineOperand::MO_ConstantPoolIndex:
133 MCO = GetSymbolRef(MO, AP.GetCPISymbol(MO.getIndex()), AP);
134 break;
135 case MachineOperand::MO_BlockAddress:
Colin LeMahieu52418812014-11-04 00:14:36 +0000136 MCO = GetSymbolRef(MO, AP.GetBlockAddressSymbol(MO.getBlockAddress()),AP);
Evandro Menezes5cee6212012-04-12 17:55:53 +0000137 break;
138 }
139
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000140 MCI->addOperand(MCO);
Evandro Menezes5cee6212012-04-12 17:55:53 +0000141 }
Krzysztof Parzyszek8d8b2292015-12-02 23:08:29 +0000142 HexagonMCInstrInfo::extendIfNeeded(AP.OutContext, MCII, MCB, *MCI,
143 MustExtend);
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000144 MCB.addOperand(MCOperand::createInst(MCI));
Evandro Menezes5cee6212012-04-12 17:55:53 +0000145}