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