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