blob: 93ae7ef876cb85f76ed9d11a5d306c6244dc3c08 [file] [log] [blame]
Jia Liub22310f2012-02-18 12:03:15 +00001//===-- HexagonAsmPrinter.cpp - Print machine instrs to Hexagon assembly --===//
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002//
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 a printer that converts from our internal representation
11// of machine-dependent LLVM code to Hexagon assembly language. This printer is
12// the output mechanism used by `llc'.
13//
Tony Linthicum1213a7a2011-12-12 21:14:40 +000014//===----------------------------------------------------------------------===//
15
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "Hexagon.h"
Jyotsna Verma7503a622013-02-20 16:13:27 +000017#include "HexagonAsmPrinter.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "HexagonMachineFunctionInfo.h"
Jyotsna Verma7503a622013-02-20 16:13:27 +000019#include "HexagonSubtarget.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000020#include "HexagonTargetMachine.h"
Colin LeMahieuff062612014-11-20 21:56:35 +000021#include "MCTargetDesc/HexagonInstPrinter.h"
Colin LeMahieu1174fea2015-02-19 21:10:50 +000022#include "MCTargetDesc/HexagonMCInstrInfo.h"
Colin LeMahieube8c4532015-06-05 16:00:11 +000023#include "MCTargetDesc/HexagonMCShuffler.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include "llvm/ADT/SmallString.h"
25#include "llvm/ADT/SmallVector.h"
26#include "llvm/ADT/StringExtras.h"
Evandro Menezes5cee6212012-04-12 17:55:53 +000027#include "llvm/Analysis/ConstantFolding.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000028#include "llvm/CodeGen/AsmPrinter.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000029#include "llvm/CodeGen/MachineFunctionPass.h"
30#include "llvm/CodeGen/MachineInstr.h"
31#include "llvm/CodeGen/MachineInstrBuilder.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000032#include "llvm/CodeGen/MachineModuleInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000033#include "llvm/IR/Constants.h"
34#include "llvm/IR/DataLayout.h"
35#include "llvm/IR/DerivedTypes.h"
Rafael Espindola894843c2014-01-07 21:19:40 +000036#include "llvm/IR/Mangler.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000037#include "llvm/IR/Module.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000038#include "llvm/MC/MCAsmInfo.h"
Evandro Menezes5cee6212012-04-12 17:55:53 +000039#include "llvm/MC/MCContext.h"
40#include "llvm/MC/MCExpr.h"
41#include "llvm/MC/MCInst.h"
42#include "llvm/MC/MCSection.h"
43#include "llvm/MC/MCStreamer.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000044#include "llvm/MC/MCSymbol.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000045#include "llvm/Support/CommandLine.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000046#include "llvm/Support/Compiler.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000047#include "llvm/Support/Debug.h"
Evandro Menezes5cee6212012-04-12 17:55:53 +000048#include "llvm/Support/Format.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000049#include "llvm/Support/MathExtras.h"
Craig Topperb25fda92012-03-17 18:46:09 +000050#include "llvm/Support/TargetRegistry.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000051#include "llvm/Support/raw_ostream.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000052#include "llvm/Target/TargetInstrInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000053#include "llvm/Target/TargetLoweringObjectFile.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000054#include "llvm/Target/TargetOptions.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000055#include "llvm/Target/TargetRegisterInfo.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000056
57using namespace llvm;
58
Chandler Carruth84e68b22014-04-22 02:41:26 +000059#define DEBUG_TYPE "asm-printer"
60
Tony Linthicum1213a7a2011-12-12 21:14:40 +000061static cl::opt<bool> AlignCalls(
62 "hexagon-align-calls", cl::Hidden, cl::init(true),
63 cl::desc("Insert falign after call instruction for Hexagon target"));
64
David Blaikie94598322015-01-18 20:29:04 +000065HexagonAsmPrinter::HexagonAsmPrinter(TargetMachine &TM,
66 std::unique_ptr<MCStreamer> Streamer)
Eric Christopher8f276db2015-02-03 06:40:22 +000067 : AsmPrinter(TM, std::move(Streamer)), Subtarget(nullptr) {}
David Blaikie94598322015-01-18 20:29:04 +000068
Evandro Menezes5cee6212012-04-12 17:55:53 +000069void HexagonAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
70 raw_ostream &O) {
71 const MachineOperand &MO = MI->getOperand(OpNo);
Tony Linthicum1213a7a2011-12-12 21:14:40 +000072
Evandro Menezes5cee6212012-04-12 17:55:53 +000073 switch (MO.getType()) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000074 default: llvm_unreachable ("<unknown operand type>");
Evandro Menezes5cee6212012-04-12 17:55:53 +000075 case MachineOperand::MO_Register:
76 O << HexagonInstPrinter::getRegisterName(MO.getReg());
77 return;
78 case MachineOperand::MO_Immediate:
79 O << MO.getImm();
80 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000081 case MachineOperand::MO_MachineBasicBlock:
82 O << *MO.getMBB()->getSymbol();
83 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000084 case MachineOperand::MO_ConstantPoolIndex:
85 O << *GetCPISymbol(MO.getIndex());
86 return;
Evandro Menezes5cee6212012-04-12 17:55:53 +000087 case MachineOperand::MO_GlobalAddress:
Tony Linthicum1213a7a2011-12-12 21:14:40 +000088 // Computing the address of a global symbol, not calling it.
Rafael Espindola79858aa2013-10-29 17:07:16 +000089 O << *getSymbol(MO.getGlobal());
Tony Linthicum1213a7a2011-12-12 21:14:40 +000090 printOffset(MO.getOffset(), O);
91 return;
92 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +000093}
94
Tony Linthicum1213a7a2011-12-12 21:14:40 +000095//
96// isBlockOnlyReachableByFallthrough - We need to override this since the
97// default AsmPrinter does not print labels for any basic block that
98// is only reachable by a fall through. That works for all cases except
99// for the case in which the basic block is reachable by a fall through but
100// through an indirect from a jump table. In this case, the jump table
101// will contain a label not defined by AsmPrinter.
102//
103bool HexagonAsmPrinter::
104isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const {
105 if (MBB->hasAddressTaken()) {
106 return false;
107 }
108 return AsmPrinter::isBlockOnlyReachableByFallthrough(MBB);
109}
110
111
112/// PrintAsmOperand - Print out an operand for an inline asm expression.
113///
114bool HexagonAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
115 unsigned AsmVariant,
116 const char *ExtraCode,
Evandro Menezes5cee6212012-04-12 17:55:53 +0000117 raw_ostream &OS) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000118 // Does this asm operand have a single letter operand modifier?
119 if (ExtraCode && ExtraCode[0]) {
120 if (ExtraCode[1] != 0) return true; // Unknown modifier.
121
122 switch (ExtraCode[0]) {
Jack Carter5e69cff2012-06-26 13:49:27 +0000123 default:
124 // See if this is a generic print operand
125 return AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, OS);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000126 case 'c': // Don't print "$" before a global var name or constant.
127 // Hexagon never has a prefix.
128 printOperand(MI, OpNo, OS);
129 return false;
130 case 'L': // Write second word of DImode reference.
131 // Verify that this operand has two consecutive registers.
132 if (!MI->getOperand(OpNo).isReg() ||
133 OpNo+1 == MI->getNumOperands() ||
134 !MI->getOperand(OpNo+1).isReg())
135 return true;
136 ++OpNo; // Return the high-part.
137 break;
138 case 'I':
139 // Write 'i' if an integer constant, otherwise nothing. Used to print
140 // addi vs add, etc.
141 if (MI->getOperand(OpNo).isImm())
142 OS << "i";
143 return false;
144 }
145 }
146
147 printOperand(MI, OpNo, OS);
148 return false;
149}
150
151bool HexagonAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
152 unsigned OpNo, unsigned AsmVariant,
153 const char *ExtraCode,
154 raw_ostream &O) {
155 if (ExtraCode && ExtraCode[0])
156 return true; // Unknown modifier.
157
158 const MachineOperand &Base = MI->getOperand(OpNo);
159 const MachineOperand &Offset = MI->getOperand(OpNo+1);
160
161 if (Base.isReg())
162 printOperand(MI, OpNo, O);
163 else
Craig Toppere55c5562012-02-07 02:50:20 +0000164 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000165
166 if (Offset.isImm()) {
167 if (Offset.getImm())
168 O << " + #" << Offset.getImm();
169 }
170 else
Craig Toppere55c5562012-02-07 02:50:20 +0000171 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000172
173 return false;
174}
175
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000176
177/// printMachineInstruction -- Print out a single Hexagon MI in Darwin syntax to
178/// the current output stream.
179///
180void HexagonAsmPrinter::EmitInstruction(const MachineInstr *MI) {
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000181 MCInst MCB;
182 MCB.setOpcode(Hexagon::BUNDLE);
183 MCB.addOperand(MCOperand::createImm(0));
184
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000185 if (MI->isBundle()) {
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000186 const MachineBasicBlock* MBB = MI->getParent();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000187 MachineBasicBlock::const_instr_iterator MII = MI;
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000188 unsigned IgnoreCount = 0;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000189
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000190 for (++MII; MII != MBB->end() && MII->isInsideBundle(); ++MII) {
191 if (MII->getOpcode() == TargetOpcode::DBG_VALUE ||
192 MII->getOpcode() == TargetOpcode::IMPLICIT_DEF)
193 ++IgnoreCount;
194 else {
195 HexagonLowerToMC(MII, MCB, *this);
196 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000197 }
198 }
199 else {
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000200 HexagonLowerToMC(MI, MCB, *this);
201 HexagonMCInstrInfo::padEndloop(MCB);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000202 }
Colin LeMahieu6aca6f02015-06-08 16:34:47 +0000203 // Examine the packet and try to find instructions that can be converted
204 // to compounds.
205 HexagonMCInstrInfo::tryCompound(*Subtarget->getInstrInfo(),
206 OutStreamer->getContext(), MCB);
Colin LeMahieube8c4532015-06-05 16:00:11 +0000207 // Examine the packet and convert pairs of instructions to duplex
208 // instructions when possible.
209 SmallVector<DuplexCandidate, 8> possibleDuplexes;
Colin LeMahieu6aca6f02015-06-08 16:34:47 +0000210 possibleDuplexes = HexagonMCInstrInfo::getDuplexPossibilties(
211 *Subtarget->getInstrInfo(), MCB);
212 HexagonMCShuffle(*Subtarget->getInstrInfo(), *Subtarget,
213 OutStreamer->getContext(), MCB, possibleDuplexes);
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000214 EmitToStreamer(*OutStreamer, MCB);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000215}
216
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000217extern "C" void LLVMInitializeHexagonAsmPrinter() {
218 RegisterAsmPrinter<HexagonAsmPrinter> X(TheHexagonTarget);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000219}