Chris Lattner | 0808414 | 2003-01-13 00:26:36 +0000 | [diff] [blame] | 1 | //===-- TargetInstrInfo.cpp - Target Instruction Information --------------===// |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 93fa705 | 2002-10-28 23:55:33 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | 167b10c | 2005-01-19 06:53:34 +0000 | [diff] [blame] | 10 | // This file implements the TargetInstrInfo class. |
Chris Lattner | 93fa705 | 2002-10-28 23:55:33 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chris Lattner | 3501fea | 2003-01-14 22:00:31 +0000 | [diff] [blame] | 14 | #include "llvm/Target/TargetInstrInfo.h" |
Evan Cheng | d923fc6 | 2009-05-05 00:30:09 +0000 | [diff] [blame] | 15 | #include "llvm/Target/TargetRegisterInfo.h" |
Evan Cheng | a0792de | 2010-10-06 06:27:31 +0000 | [diff] [blame] | 16 | #include "llvm/MC/MCAsmInfo.h" |
Evan Cheng | ab8be96 | 2011-06-29 01:14:12 +0000 | [diff] [blame] | 17 | #include "llvm/MC/MCInstrItineraries.h" |
Chris Lattner | b6bbfebd | 2009-08-02 04:58:19 +0000 | [diff] [blame] | 18 | #include "llvm/Support/ErrorHandling.h" |
Nick Lewycky | 476b242 | 2010-12-19 20:43:38 +0000 | [diff] [blame] | 19 | #include <cctype> |
Chris Lattner | 167b10c | 2005-01-19 06:53:34 +0000 | [diff] [blame] | 20 | using namespace llvm; |
Chris Lattner | 93fa705 | 2002-10-28 23:55:33 +0000 | [diff] [blame] | 21 | |
Chris Lattner | d90183d | 2009-08-02 05:20:37 +0000 | [diff] [blame] | 22 | //===----------------------------------------------------------------------===// |
Chris Lattner | d90183d | 2009-08-02 05:20:37 +0000 | [diff] [blame] | 23 | // TargetInstrInfo |
| 24 | //===----------------------------------------------------------------------===// |
| 25 | |
Chris Lattner | 0808414 | 2003-01-13 00:26:36 +0000 | [diff] [blame] | 26 | TargetInstrInfo::~TargetInstrInfo() { |
Chris Lattner | 93fa705 | 2002-10-28 23:55:33 +0000 | [diff] [blame] | 27 | } |
| 28 | |
Evan Cheng | 15993f8 | 2011-06-27 21:26:13 +0000 | [diff] [blame] | 29 | const TargetRegisterClass* |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 30 | TargetInstrInfo::getRegClass(const MCInstrDesc &MCID, unsigned OpNum, |
Jakob Stoklund Olesen | 397fc48 | 2012-05-07 22:10:26 +0000 | [diff] [blame^] | 31 | const TargetRegisterInfo *TRI, |
| 32 | const MachineFunction &MF) const { |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 33 | if (OpNum >= MCID.getNumOperands()) |
Evan Cheng | 15993f8 | 2011-06-27 21:26:13 +0000 | [diff] [blame] | 34 | return 0; |
| 35 | |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 36 | short RegClass = MCID.OpInfo[OpNum].RegClass; |
| 37 | if (MCID.OpInfo[OpNum].isLookupPtrRegClass()) |
Jakob Stoklund Olesen | 397fc48 | 2012-05-07 22:10:26 +0000 | [diff] [blame^] | 38 | return TRI->getPointerRegClass(MF, RegClass); |
Evan Cheng | 15993f8 | 2011-06-27 21:26:13 +0000 | [diff] [blame] | 39 | |
| 40 | // Instructions like INSERT_SUBREG do not have fixed register classes. |
| 41 | if (RegClass < 0) |
| 42 | return 0; |
| 43 | |
| 44 | // Otherwise just look it up normally. |
| 45 | return TRI->getRegClass(RegClass); |
| 46 | } |
| 47 | |
Evan Cheng | 5f54ce3 | 2010-09-09 18:18:55 +0000 | [diff] [blame] | 48 | unsigned |
Evan Cheng | 8239daf | 2010-11-03 00:45:17 +0000 | [diff] [blame] | 49 | TargetInstrInfo::getNumMicroOps(const InstrItineraryData *ItinData, |
| 50 | const MachineInstr *MI) const { |
Evan Cheng | 3ef1c87 | 2010-09-10 01:29:16 +0000 | [diff] [blame] | 51 | if (!ItinData || ItinData->isEmpty()) |
Evan Cheng | 5f54ce3 | 2010-09-09 18:18:55 +0000 | [diff] [blame] | 52 | return 1; |
| 53 | |
| 54 | unsigned Class = MI->getDesc().getSchedClass(); |
Bob Wilson | 064312d | 2010-09-15 16:28:21 +0000 | [diff] [blame] | 55 | unsigned UOps = ItinData->Itineraries[Class].NumMicroOps; |
Evan Cheng | 5f54ce3 | 2010-09-09 18:18:55 +0000 | [diff] [blame] | 56 | if (UOps) |
| 57 | return UOps; |
| 58 | |
| 59 | // The # of u-ops is dynamically determined. The specific target should |
| 60 | // override this function to return the right number. |
| 61 | return 1; |
| 62 | } |
| 63 | |
Evan Cheng | a0792de | 2010-10-06 06:27:31 +0000 | [diff] [blame] | 64 | int |
| 65 | TargetInstrInfo::getOperandLatency(const InstrItineraryData *ItinData, |
| 66 | const MachineInstr *DefMI, unsigned DefIdx, |
| 67 | const MachineInstr *UseMI, unsigned UseIdx) const { |
| 68 | if (!ItinData || ItinData->isEmpty()) |
| 69 | return -1; |
| 70 | |
| 71 | unsigned DefClass = DefMI->getDesc().getSchedClass(); |
| 72 | unsigned UseClass = UseMI->getDesc().getSchedClass(); |
| 73 | return ItinData->getOperandLatency(DefClass, DefIdx, UseClass, UseIdx); |
| 74 | } |
| 75 | |
Evan Cheng | 8239daf | 2010-11-03 00:45:17 +0000 | [diff] [blame] | 76 | int TargetInstrInfo::getInstrLatency(const InstrItineraryData *ItinData, |
| 77 | const MachineInstr *MI, |
| 78 | unsigned *PredCost) const { |
| 79 | if (!ItinData || ItinData->isEmpty()) |
| 80 | return 1; |
| 81 | |
| 82 | return ItinData->getStageLatency(MI->getDesc().getSchedClass()); |
| 83 | } |
| 84 | |
Evan Cheng | c8141df | 2010-10-26 02:08:50 +0000 | [diff] [blame] | 85 | bool TargetInstrInfo::hasLowDefLatency(const InstrItineraryData *ItinData, |
| 86 | const MachineInstr *DefMI, |
| 87 | unsigned DefIdx) const { |
| 88 | if (!ItinData || ItinData->isEmpty()) |
| 89 | return false; |
| 90 | |
| 91 | unsigned DefClass = DefMI->getDesc().getSchedClass(); |
| 92 | int DefCycle = ItinData->getOperandCycle(DefClass, DefIdx); |
| 93 | return (DefCycle != -1 && DefCycle <= 1); |
| 94 | } |
Evan Cheng | a0792de | 2010-10-06 06:27:31 +0000 | [diff] [blame] | 95 | |
Chris Lattner | b6bbfebd | 2009-08-02 04:58:19 +0000 | [diff] [blame] | 96 | /// insertNoop - Insert a noop into the instruction stream at the specified |
| 97 | /// point. |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 98 | void TargetInstrInfo::insertNoop(MachineBasicBlock &MBB, |
Chris Lattner | b6bbfebd | 2009-08-02 04:58:19 +0000 | [diff] [blame] | 99 | MachineBasicBlock::iterator MI) const { |
| 100 | llvm_unreachable("Target didn't implement insertNoop!"); |
| 101 | } |
| 102 | |
| 103 | |
Chris Lattner | d90183d | 2009-08-02 05:20:37 +0000 | [diff] [blame] | 104 | /// Measure the specified inline asm to determine an approximation of its |
| 105 | /// length. |
Jim Grosbach | d31d304 | 2011-03-24 18:46:34 +0000 | [diff] [blame] | 106 | /// Comments (which run till the next SeparatorString or newline) do not |
Chris Lattner | d90183d | 2009-08-02 05:20:37 +0000 | [diff] [blame] | 107 | /// count as an instruction. |
| 108 | /// Any other non-whitespace text is considered an instruction, with |
Jim Grosbach | d31d304 | 2011-03-24 18:46:34 +0000 | [diff] [blame] | 109 | /// multiple instructions separated by SeparatorString or newlines. |
Chris Lattner | d90183d | 2009-08-02 05:20:37 +0000 | [diff] [blame] | 110 | /// Variable-length instructions are not handled here; this function |
| 111 | /// may be overloaded in the target code to do that. |
| 112 | unsigned TargetInstrInfo::getInlineAsmLength(const char *Str, |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 113 | const MCAsmInfo &MAI) const { |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 114 | |
| 115 | |
Chris Lattner | d90183d | 2009-08-02 05:20:37 +0000 | [diff] [blame] | 116 | // Count the number of instructions in the asm. |
| 117 | bool atInsnStart = true; |
| 118 | unsigned Length = 0; |
| 119 | for (; *Str; ++Str) { |
Jim Grosbach | d31d304 | 2011-03-24 18:46:34 +0000 | [diff] [blame] | 120 | if (*Str == '\n' || strncmp(Str, MAI.getSeparatorString(), |
| 121 | strlen(MAI.getSeparatorString())) == 0) |
Chris Lattner | d90183d | 2009-08-02 05:20:37 +0000 | [diff] [blame] | 122 | atInsnStart = true; |
Nick Lewycky | 2402123 | 2010-12-19 20:42:43 +0000 | [diff] [blame] | 123 | if (atInsnStart && !std::isspace(*Str)) { |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 124 | Length += MAI.getMaxInstLength(); |
Chris Lattner | d90183d | 2009-08-02 05:20:37 +0000 | [diff] [blame] | 125 | atInsnStart = false; |
| 126 | } |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 127 | if (atInsnStart && strncmp(Str, MAI.getCommentString(), |
| 128 | strlen(MAI.getCommentString())) == 0) |
Chris Lattner | d90183d | 2009-08-02 05:20:37 +0000 | [diff] [blame] | 129 | atInsnStart = false; |
| 130 | } |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 131 | |
Chris Lattner | d90183d | 2009-08-02 05:20:37 +0000 | [diff] [blame] | 132 | return Length; |
| 133 | } |