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 | 5f54ce3 | 2010-09-09 18:18:55 +0000 | [diff] [blame] | 15 | #include "llvm/Target/TargetInstrItineraries.h" |
Evan Cheng | d923fc6 | 2009-05-05 00:30:09 +0000 | [diff] [blame] | 16 | #include "llvm/Target/TargetRegisterInfo.h" |
Evan Cheng | a0792de | 2010-10-06 06:27:31 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/SelectionDAGNodes.h" |
| 18 | #include "llvm/MC/MCAsmInfo.h" |
Chris Lattner | b6bbfebd | 2009-08-02 04:58:19 +0000 | [diff] [blame] | 19 | #include "llvm/Support/ErrorHandling.h" |
Nick Lewycky | 476b242 | 2010-12-19 20:43:38 +0000 | [diff] [blame] | 20 | #include <cctype> |
Chris Lattner | 167b10c | 2005-01-19 06:53:34 +0000 | [diff] [blame] | 21 | using namespace llvm; |
Chris Lattner | 93fa705 | 2002-10-28 23:55:33 +0000 | [diff] [blame] | 22 | |
Chris Lattner | d90183d | 2009-08-02 05:20:37 +0000 | [diff] [blame] | 23 | //===----------------------------------------------------------------------===// |
| 24 | // TargetOperandInfo |
| 25 | //===----------------------------------------------------------------------===// |
| 26 | |
| 27 | /// getRegClass - Get the register class for the operand, handling resolution |
| 28 | /// of "symbolic" pointer register classes etc. If this is not a register |
| 29 | /// operand, this returns null. |
| 30 | const TargetRegisterClass * |
| 31 | TargetOperandInfo::getRegClass(const TargetRegisterInfo *TRI) const { |
| 32 | if (isLookupPtrRegClass()) |
| 33 | return TRI->getPointerRegClass(RegClass); |
Dan Gohman | a606d95 | 2010-06-18 18:13:55 +0000 | [diff] [blame] | 34 | // Instructions like INSERT_SUBREG do not have fixed register classes. |
| 35 | if (RegClass < 0) |
| 36 | return 0; |
| 37 | // Otherwise just look it up normally. |
Chris Lattner | d90183d | 2009-08-02 05:20:37 +0000 | [diff] [blame] | 38 | return TRI->getRegClass(RegClass); |
| 39 | } |
| 40 | |
| 41 | //===----------------------------------------------------------------------===// |
| 42 | // TargetInstrInfo |
| 43 | //===----------------------------------------------------------------------===// |
| 44 | |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 45 | TargetInstrInfo::TargetInstrInfo(const TargetInstrDesc* Desc, |
Misha Brukman | 7847fca | 2005-04-22 17:54:37 +0000 | [diff] [blame] | 46 | unsigned numOpcodes) |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 47 | : Descriptors(Desc), NumOpcodes(numOpcodes) { |
Chris Lattner | 93fa705 | 2002-10-28 23:55:33 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Chris Lattner | 0808414 | 2003-01-13 00:26:36 +0000 | [diff] [blame] | 50 | TargetInstrInfo::~TargetInstrInfo() { |
Chris Lattner | 93fa705 | 2002-10-28 23:55:33 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Evan Cheng | 5f54ce3 | 2010-09-09 18:18:55 +0000 | [diff] [blame] | 53 | unsigned |
Evan Cheng | 8239daf | 2010-11-03 00:45:17 +0000 | [diff] [blame] | 54 | TargetInstrInfo::getNumMicroOps(const InstrItineraryData *ItinData, |
| 55 | const MachineInstr *MI) const { |
Evan Cheng | 3ef1c87 | 2010-09-10 01:29:16 +0000 | [diff] [blame] | 56 | if (!ItinData || ItinData->isEmpty()) |
Evan Cheng | 5f54ce3 | 2010-09-09 18:18:55 +0000 | [diff] [blame] | 57 | return 1; |
| 58 | |
| 59 | unsigned Class = MI->getDesc().getSchedClass(); |
Bob Wilson | 064312d | 2010-09-15 16:28:21 +0000 | [diff] [blame] | 60 | unsigned UOps = ItinData->Itineraries[Class].NumMicroOps; |
Evan Cheng | 5f54ce3 | 2010-09-09 18:18:55 +0000 | [diff] [blame] | 61 | if (UOps) |
| 62 | return UOps; |
| 63 | |
| 64 | // The # of u-ops is dynamically determined. The specific target should |
| 65 | // override this function to return the right number. |
| 66 | return 1; |
| 67 | } |
| 68 | |
Evan Cheng | a0792de | 2010-10-06 06:27:31 +0000 | [diff] [blame] | 69 | int |
| 70 | TargetInstrInfo::getOperandLatency(const InstrItineraryData *ItinData, |
| 71 | const MachineInstr *DefMI, unsigned DefIdx, |
| 72 | const MachineInstr *UseMI, unsigned UseIdx) const { |
| 73 | if (!ItinData || ItinData->isEmpty()) |
| 74 | return -1; |
| 75 | |
| 76 | unsigned DefClass = DefMI->getDesc().getSchedClass(); |
| 77 | unsigned UseClass = UseMI->getDesc().getSchedClass(); |
| 78 | return ItinData->getOperandLatency(DefClass, DefIdx, UseClass, UseIdx); |
| 79 | } |
| 80 | |
| 81 | int |
| 82 | TargetInstrInfo::getOperandLatency(const InstrItineraryData *ItinData, |
| 83 | SDNode *DefNode, unsigned DefIdx, |
| 84 | SDNode *UseNode, unsigned UseIdx) const { |
| 85 | if (!ItinData || ItinData->isEmpty()) |
| 86 | return -1; |
| 87 | |
| 88 | if (!DefNode->isMachineOpcode()) |
| 89 | return -1; |
| 90 | |
| 91 | unsigned DefClass = get(DefNode->getMachineOpcode()).getSchedClass(); |
| 92 | if (!UseNode->isMachineOpcode()) |
| 93 | return ItinData->getOperandCycle(DefClass, DefIdx); |
| 94 | unsigned UseClass = get(UseNode->getMachineOpcode()).getSchedClass(); |
| 95 | return ItinData->getOperandLatency(DefClass, DefIdx, UseClass, UseIdx); |
| 96 | } |
| 97 | |
Evan Cheng | 8239daf | 2010-11-03 00:45:17 +0000 | [diff] [blame] | 98 | int TargetInstrInfo::getInstrLatency(const InstrItineraryData *ItinData, |
| 99 | const MachineInstr *MI, |
| 100 | unsigned *PredCost) const { |
| 101 | if (!ItinData || ItinData->isEmpty()) |
| 102 | return 1; |
| 103 | |
| 104 | return ItinData->getStageLatency(MI->getDesc().getSchedClass()); |
| 105 | } |
| 106 | |
| 107 | int TargetInstrInfo::getInstrLatency(const InstrItineraryData *ItinData, |
| 108 | SDNode *N) const { |
| 109 | if (!ItinData || ItinData->isEmpty()) |
| 110 | return 1; |
| 111 | |
| 112 | if (!N->isMachineOpcode()) |
| 113 | return 1; |
| 114 | |
| 115 | return ItinData->getStageLatency(get(N->getMachineOpcode()).getSchedClass()); |
| 116 | } |
| 117 | |
Evan Cheng | c8141df | 2010-10-26 02:08:50 +0000 | [diff] [blame] | 118 | bool TargetInstrInfo::hasLowDefLatency(const InstrItineraryData *ItinData, |
| 119 | const MachineInstr *DefMI, |
| 120 | unsigned DefIdx) const { |
| 121 | if (!ItinData || ItinData->isEmpty()) |
| 122 | return false; |
| 123 | |
| 124 | unsigned DefClass = DefMI->getDesc().getSchedClass(); |
| 125 | int DefCycle = ItinData->getOperandCycle(DefClass, DefIdx); |
| 126 | return (DefCycle != -1 && DefCycle <= 1); |
| 127 | } |
Evan Cheng | a0792de | 2010-10-06 06:27:31 +0000 | [diff] [blame] | 128 | |
Chris Lattner | b6bbfebd | 2009-08-02 04:58:19 +0000 | [diff] [blame] | 129 | /// insertNoop - Insert a noop into the instruction stream at the specified |
| 130 | /// point. |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame^] | 131 | void TargetInstrInfo::insertNoop(MachineBasicBlock &MBB, |
Chris Lattner | b6bbfebd | 2009-08-02 04:58:19 +0000 | [diff] [blame] | 132 | MachineBasicBlock::iterator MI) const { |
| 133 | llvm_unreachable("Target didn't implement insertNoop!"); |
| 134 | } |
| 135 | |
| 136 | |
Evan Cheng | bfd2ec4 | 2007-06-08 21:59:56 +0000 | [diff] [blame] | 137 | bool TargetInstrInfo::isUnpredicatedTerminator(const MachineInstr *MI) const { |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 138 | const TargetInstrDesc &TID = MI->getDesc(); |
| 139 | if (!TID.isTerminator()) return false; |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame^] | 140 | |
Chris Lattner | 6924430 | 2008-01-07 01:56:04 +0000 | [diff] [blame] | 141 | // Conditional branch is a special case. |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 142 | if (TID.isBranch() && !TID.isBarrier()) |
Chris Lattner | 6924430 | 2008-01-07 01:56:04 +0000 | [diff] [blame] | 143 | return true; |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 144 | if (!TID.isPredicable()) |
Chris Lattner | 6924430 | 2008-01-07 01:56:04 +0000 | [diff] [blame] | 145 | return true; |
| 146 | return !isPredicated(MI); |
Evan Cheng | bfd2ec4 | 2007-06-08 21:59:56 +0000 | [diff] [blame] | 147 | } |
Evan Cheng | d923fc6 | 2009-05-05 00:30:09 +0000 | [diff] [blame] | 148 | |
Chris Lattner | cb778a8 | 2009-07-29 21:10:12 +0000 | [diff] [blame] | 149 | |
Chris Lattner | d90183d | 2009-08-02 05:20:37 +0000 | [diff] [blame] | 150 | /// Measure the specified inline asm to determine an approximation of its |
| 151 | /// length. |
| 152 | /// Comments (which run till the next SeparatorChar or newline) do not |
| 153 | /// count as an instruction. |
| 154 | /// Any other non-whitespace text is considered an instruction, with |
| 155 | /// multiple instructions separated by SeparatorChar or newlines. |
| 156 | /// Variable-length instructions are not handled here; this function |
| 157 | /// may be overloaded in the target code to do that. |
| 158 | unsigned TargetInstrInfo::getInlineAsmLength(const char *Str, |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 159 | const MCAsmInfo &MAI) const { |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame^] | 160 | |
| 161 | |
Chris Lattner | d90183d | 2009-08-02 05:20:37 +0000 | [diff] [blame] | 162 | // Count the number of instructions in the asm. |
| 163 | bool atInsnStart = true; |
| 164 | unsigned Length = 0; |
| 165 | for (; *Str; ++Str) { |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 166 | if (*Str == '\n' || *Str == MAI.getSeparatorChar()) |
Chris Lattner | d90183d | 2009-08-02 05:20:37 +0000 | [diff] [blame] | 167 | atInsnStart = true; |
Nick Lewycky | 2402123 | 2010-12-19 20:42:43 +0000 | [diff] [blame] | 168 | if (atInsnStart && !std::isspace(*Str)) { |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 169 | Length += MAI.getMaxInstLength(); |
Chris Lattner | d90183d | 2009-08-02 05:20:37 +0000 | [diff] [blame] | 170 | atInsnStart = false; |
| 171 | } |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 172 | if (atInsnStart && strncmp(Str, MAI.getCommentString(), |
| 173 | strlen(MAI.getCommentString())) == 0) |
Chris Lattner | d90183d | 2009-08-02 05:20:37 +0000 | [diff] [blame] | 174 | atInsnStart = false; |
| 175 | } |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame^] | 176 | |
Chris Lattner | d90183d | 2009-08-02 05:20:37 +0000 | [diff] [blame] | 177 | return Length; |
| 178 | } |