Jia Liu | b22310f | 2012-02-18 12:03:15 +0000 | [diff] [blame] | 1 | //===-- HexagonAsmPrinter.cpp - Print machine instrs to Hexagon assembly --===// |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 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 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 Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 16 | #include "Hexagon.h" |
Jyotsna Verma | 7503a62 | 2013-02-20 16:13:27 +0000 | [diff] [blame] | 17 | #include "HexagonAsmPrinter.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 18 | #include "HexagonMachineFunctionInfo.h" |
Jyotsna Verma | 7503a62 | 2013-02-20 16:13:27 +0000 | [diff] [blame] | 19 | #include "HexagonSubtarget.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 20 | #include "HexagonTargetMachine.h" |
Colin LeMahieu | ff06261 | 2014-11-20 21:56:35 +0000 | [diff] [blame] | 21 | #include "MCTargetDesc/HexagonInstPrinter.h" |
Colin LeMahieu | 1174fea | 2015-02-19 21:10:50 +0000 | [diff] [blame] | 22 | #include "MCTargetDesc/HexagonMCInstrInfo.h" |
Colin LeMahieu | be8c453 | 2015-06-05 16:00:11 +0000 | [diff] [blame] | 23 | #include "MCTargetDesc/HexagonMCShuffler.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/SmallString.h" |
| 25 | #include "llvm/ADT/SmallVector.h" |
| 26 | #include "llvm/ADT/StringExtras.h" |
Evandro Menezes | 5cee621 | 2012-04-12 17:55:53 +0000 | [diff] [blame] | 27 | #include "llvm/Analysis/ConstantFolding.h" |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/AsmPrinter.h" |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 30 | #include "llvm/CodeGen/MachineInstr.h" |
| 31 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 32 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 33 | #include "llvm/IR/Constants.h" |
| 34 | #include "llvm/IR/DataLayout.h" |
| 35 | #include "llvm/IR/DerivedTypes.h" |
Rafael Espindola | 894843c | 2014-01-07 21:19:40 +0000 | [diff] [blame] | 36 | #include "llvm/IR/Mangler.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 37 | #include "llvm/IR/Module.h" |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 38 | #include "llvm/MC/MCAsmInfo.h" |
Evandro Menezes | 5cee621 | 2012-04-12 17:55:53 +0000 | [diff] [blame] | 39 | #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 Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 44 | #include "llvm/MC/MCSymbol.h" |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 45 | #include "llvm/Support/CommandLine.h" |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 46 | #include "llvm/Support/Compiler.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 47 | #include "llvm/Support/Debug.h" |
Evandro Menezes | 5cee621 | 2012-04-12 17:55:53 +0000 | [diff] [blame] | 48 | #include "llvm/Support/Format.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 49 | #include "llvm/Support/MathExtras.h" |
Craig Topper | b25fda9 | 2012-03-17 18:46:09 +0000 | [diff] [blame] | 50 | #include "llvm/Support/TargetRegistry.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 51 | #include "llvm/Support/raw_ostream.h" |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 52 | #include "llvm/Target/TargetInstrInfo.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 53 | #include "llvm/Target/TargetLoweringObjectFile.h" |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 54 | #include "llvm/Target/TargetOptions.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 55 | #include "llvm/Target/TargetRegisterInfo.h" |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 56 | |
| 57 | using namespace llvm; |
| 58 | |
Krzysztof Parzyszek | 8d8b229 | 2015-12-02 23:08:29 +0000 | [diff] [blame^] | 59 | namespace llvm { |
| 60 | void HexagonLowerToMC(const MCInstrInfo &MCII, const MachineInstr *MI, |
| 61 | MCInst &MCB, HexagonAsmPrinter &AP); |
| 62 | } |
| 63 | |
Chandler Carruth | 84e68b2 | 2014-04-22 02:41:26 +0000 | [diff] [blame] | 64 | #define DEBUG_TYPE "asm-printer" |
| 65 | |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 66 | static cl::opt<bool> AlignCalls( |
| 67 | "hexagon-align-calls", cl::Hidden, cl::init(true), |
| 68 | cl::desc("Insert falign after call instruction for Hexagon target")); |
| 69 | |
David Blaikie | 9459832 | 2015-01-18 20:29:04 +0000 | [diff] [blame] | 70 | HexagonAsmPrinter::HexagonAsmPrinter(TargetMachine &TM, |
| 71 | std::unique_ptr<MCStreamer> Streamer) |
Eric Christopher | 8f276db | 2015-02-03 06:40:22 +0000 | [diff] [blame] | 72 | : AsmPrinter(TM, std::move(Streamer)), Subtarget(nullptr) {} |
David Blaikie | 9459832 | 2015-01-18 20:29:04 +0000 | [diff] [blame] | 73 | |
Evandro Menezes | 5cee621 | 2012-04-12 17:55:53 +0000 | [diff] [blame] | 74 | void HexagonAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo, |
| 75 | raw_ostream &O) { |
| 76 | const MachineOperand &MO = MI->getOperand(OpNo); |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 77 | |
Evandro Menezes | 5cee621 | 2012-04-12 17:55:53 +0000 | [diff] [blame] | 78 | switch (MO.getType()) { |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 79 | default: llvm_unreachable ("<unknown operand type>"); |
Evandro Menezes | 5cee621 | 2012-04-12 17:55:53 +0000 | [diff] [blame] | 80 | case MachineOperand::MO_Register: |
| 81 | O << HexagonInstPrinter::getRegisterName(MO.getReg()); |
| 82 | return; |
| 83 | case MachineOperand::MO_Immediate: |
| 84 | O << MO.getImm(); |
| 85 | return; |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 86 | case MachineOperand::MO_MachineBasicBlock: |
Matt Arsenault | 8b64355 | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 87 | MO.getMBB()->getSymbol()->print(O, MAI); |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 88 | return; |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 89 | case MachineOperand::MO_ConstantPoolIndex: |
Matt Arsenault | 8b64355 | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 90 | GetCPISymbol(MO.getIndex())->print(O, MAI); |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 91 | return; |
Evandro Menezes | 5cee621 | 2012-04-12 17:55:53 +0000 | [diff] [blame] | 92 | case MachineOperand::MO_GlobalAddress: |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 93 | // Computing the address of a global symbol, not calling it. |
Matt Arsenault | 8b64355 | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 94 | getSymbol(MO.getGlobal())->print(O, MAI); |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 95 | printOffset(MO.getOffset(), O); |
| 96 | return; |
| 97 | } |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 100 | // |
| 101 | // isBlockOnlyReachableByFallthrough - We need to override this since the |
| 102 | // default AsmPrinter does not print labels for any basic block that |
| 103 | // is only reachable by a fall through. That works for all cases except |
| 104 | // for the case in which the basic block is reachable by a fall through but |
| 105 | // through an indirect from a jump table. In this case, the jump table |
| 106 | // will contain a label not defined by AsmPrinter. |
| 107 | // |
| 108 | bool HexagonAsmPrinter:: |
| 109 | isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const { |
| 110 | if (MBB->hasAddressTaken()) { |
| 111 | return false; |
| 112 | } |
| 113 | return AsmPrinter::isBlockOnlyReachableByFallthrough(MBB); |
| 114 | } |
| 115 | |
| 116 | |
| 117 | /// PrintAsmOperand - Print out an operand for an inline asm expression. |
| 118 | /// |
| 119 | bool HexagonAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, |
| 120 | unsigned AsmVariant, |
| 121 | const char *ExtraCode, |
Evandro Menezes | 5cee621 | 2012-04-12 17:55:53 +0000 | [diff] [blame] | 122 | raw_ostream &OS) { |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 123 | // Does this asm operand have a single letter operand modifier? |
| 124 | if (ExtraCode && ExtraCode[0]) { |
| 125 | if (ExtraCode[1] != 0) return true; // Unknown modifier. |
| 126 | |
| 127 | switch (ExtraCode[0]) { |
Jack Carter | 5e69cff | 2012-06-26 13:49:27 +0000 | [diff] [blame] | 128 | default: |
| 129 | // See if this is a generic print operand |
| 130 | return AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, OS); |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 131 | case 'c': // Don't print "$" before a global var name or constant. |
| 132 | // Hexagon never has a prefix. |
| 133 | printOperand(MI, OpNo, OS); |
| 134 | return false; |
| 135 | case 'L': // Write second word of DImode reference. |
| 136 | // Verify that this operand has two consecutive registers. |
| 137 | if (!MI->getOperand(OpNo).isReg() || |
| 138 | OpNo+1 == MI->getNumOperands() || |
| 139 | !MI->getOperand(OpNo+1).isReg()) |
| 140 | return true; |
| 141 | ++OpNo; // Return the high-part. |
| 142 | break; |
| 143 | case 'I': |
| 144 | // Write 'i' if an integer constant, otherwise nothing. Used to print |
| 145 | // addi vs add, etc. |
| 146 | if (MI->getOperand(OpNo).isImm()) |
| 147 | OS << "i"; |
| 148 | return false; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | printOperand(MI, OpNo, OS); |
| 153 | return false; |
| 154 | } |
| 155 | |
| 156 | bool HexagonAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, |
| 157 | unsigned OpNo, unsigned AsmVariant, |
| 158 | const char *ExtraCode, |
| 159 | raw_ostream &O) { |
| 160 | if (ExtraCode && ExtraCode[0]) |
| 161 | return true; // Unknown modifier. |
| 162 | |
| 163 | const MachineOperand &Base = MI->getOperand(OpNo); |
| 164 | const MachineOperand &Offset = MI->getOperand(OpNo+1); |
| 165 | |
| 166 | if (Base.isReg()) |
| 167 | printOperand(MI, OpNo, O); |
| 168 | else |
Craig Topper | e55c556 | 2012-02-07 02:50:20 +0000 | [diff] [blame] | 169 | llvm_unreachable("Unimplemented"); |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 170 | |
| 171 | if (Offset.isImm()) { |
| 172 | if (Offset.getImm()) |
| 173 | O << " + #" << Offset.getImm(); |
| 174 | } |
| 175 | else |
Craig Topper | e55c556 | 2012-02-07 02:50:20 +0000 | [diff] [blame] | 176 | llvm_unreachable("Unimplemented"); |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 177 | |
| 178 | return false; |
| 179 | } |
| 180 | |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 181 | |
| 182 | /// printMachineInstruction -- Print out a single Hexagon MI in Darwin syntax to |
| 183 | /// the current output stream. |
| 184 | /// |
| 185 | void HexagonAsmPrinter::EmitInstruction(const MachineInstr *MI) { |
Colin LeMahieu | f0af6e5 | 2015-11-13 17:42:46 +0000 | [diff] [blame] | 186 | MCInst MCB = HexagonMCInstrInfo::createBundle(); |
Krzysztof Parzyszek | 8d8b229 | 2015-12-02 23:08:29 +0000 | [diff] [blame^] | 187 | const MCInstrInfo &MCII = *Subtarget->getInstrInfo(); |
Colin LeMahieu | 68d967d | 2015-05-29 14:44:13 +0000 | [diff] [blame] | 188 | |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 189 | if (MI->isBundle()) { |
Colin LeMahieu | 68d967d | 2015-05-29 14:44:13 +0000 | [diff] [blame] | 190 | const MachineBasicBlock* MBB = MI->getParent(); |
Duncan P. N. Exon Smith | a72c6e2 | 2015-10-20 00:46:39 +0000 | [diff] [blame] | 191 | MachineBasicBlock::const_instr_iterator MII = MI->getIterator(); |
Colin LeMahieu | 68d967d | 2015-05-29 14:44:13 +0000 | [diff] [blame] | 192 | unsigned IgnoreCount = 0; |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 193 | |
Duncan P. N. Exon Smith | a72c6e2 | 2015-10-20 00:46:39 +0000 | [diff] [blame] | 194 | for (++MII; MII != MBB->instr_end() && MII->isInsideBundle(); ++MII) { |
Colin LeMahieu | 68d967d | 2015-05-29 14:44:13 +0000 | [diff] [blame] | 195 | if (MII->getOpcode() == TargetOpcode::DBG_VALUE || |
| 196 | MII->getOpcode() == TargetOpcode::IMPLICIT_DEF) |
| 197 | ++IgnoreCount; |
| 198 | else { |
Krzysztof Parzyszek | 8d8b229 | 2015-12-02 23:08:29 +0000 | [diff] [blame^] | 199 | HexagonLowerToMC(MCII, &*MII, MCB, *this); |
Colin LeMahieu | 68d967d | 2015-05-29 14:44:13 +0000 | [diff] [blame] | 200 | } |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | else { |
Krzysztof Parzyszek | 8d8b229 | 2015-12-02 23:08:29 +0000 | [diff] [blame^] | 204 | HexagonLowerToMC(MCII, MI, MCB, *this); |
Colin LeMahieu | b3c9727 | 2015-11-13 07:58:06 +0000 | [diff] [blame] | 205 | HexagonMCInstrInfo::padEndloop(OutStreamer->getContext(), MCB); |
Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 206 | } |
Colin LeMahieu | 6aca6f0 | 2015-06-08 16:34:47 +0000 | [diff] [blame] | 207 | // Examine the packet and try to find instructions that can be converted |
| 208 | // to compounds. |
Krzysztof Parzyszek | 8d8b229 | 2015-12-02 23:08:29 +0000 | [diff] [blame^] | 209 | HexagonMCInstrInfo::tryCompound(MCII, OutStreamer->getContext(), MCB); |
Colin LeMahieu | be8c453 | 2015-06-05 16:00:11 +0000 | [diff] [blame] | 210 | // Examine the packet and convert pairs of instructions to duplex |
| 211 | // instructions when possible. |
| 212 | SmallVector<DuplexCandidate, 8> possibleDuplexes; |
Krzysztof Parzyszek | 8d8b229 | 2015-12-02 23:08:29 +0000 | [diff] [blame^] | 213 | possibleDuplexes = HexagonMCInstrInfo::getDuplexPossibilties(MCII, MCB); |
| 214 | HexagonMCShuffle(MCII, *Subtarget, OutStreamer->getContext(), MCB, |
| 215 | possibleDuplexes); |
Colin LeMahieu | 68d967d | 2015-05-29 14:44:13 +0000 | [diff] [blame] | 216 | EmitToStreamer(*OutStreamer, MCB); |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 219 | extern "C" void LLVMInitializeHexagonAsmPrinter() { |
| 220 | RegisterAsmPrinter<HexagonAsmPrinter> X(TheHexagonTarget); |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 221 | } |