blob: 2931416febb076415451f4d83debbf848030d9f9 [file] [log] [blame]
Chris Lattner08084142003-01-13 00:26:36 +00001//===-- TargetInstrInfo.cpp - Target Instruction Information --------------===//
Misha Brukmanf976c852005-04-21 22:55:34 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanf976c852005-04-21 22:55:34 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner93fa7052002-10-28 23:55:33 +00009//
Chris Lattner167b10c2005-01-19 06:53:34 +000010// This file implements the TargetInstrInfo class.
Chris Lattner93fa7052002-10-28 23:55:33 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner3501fea2003-01-14 22:00:31 +000014#include "llvm/Target/TargetInstrInfo.h"
Evan Chengd923fc62009-05-05 00:30:09 +000015#include "llvm/Target/TargetRegisterInfo.h"
Evan Chenga0792de2010-10-06 06:27:31 +000016#include "llvm/CodeGen/SelectionDAGNodes.h"
17#include "llvm/MC/MCAsmInfo.h"
Evan Chengab8be962011-06-29 01:14:12 +000018#include "llvm/MC/MCInstrItineraries.h"
Chris Lattnerb6bbfebd2009-08-02 04:58:19 +000019#include "llvm/Support/ErrorHandling.h"
Nick Lewycky476b2422010-12-19 20:43:38 +000020#include <cctype>
Chris Lattner167b10c2005-01-19 06:53:34 +000021using namespace llvm;
Chris Lattner93fa7052002-10-28 23:55:33 +000022
Chris Lattnerd90183d2009-08-02 05:20:37 +000023//===----------------------------------------------------------------------===//
Chris Lattnerd90183d2009-08-02 05:20:37 +000024// TargetInstrInfo
25//===----------------------------------------------------------------------===//
26
Evan Chengd5b03f22011-06-28 21:14:33 +000027TargetInstrInfo::TargetInstrInfo(const MCInstrDesc* Desc, unsigned numOpcodes,
28 int CFSetupOpcode, int CFDestroyOpcode)
29 : CallFrameSetupOpcode(CFSetupOpcode),
30 CallFrameDestroyOpcode(CFDestroyOpcode) {
Evan Chenge837dea2011-06-28 19:10:37 +000031 InitMCInstrInfo(Desc, numOpcodes);
Chris Lattner93fa7052002-10-28 23:55:33 +000032}
33
Chris Lattner08084142003-01-13 00:26:36 +000034TargetInstrInfo::~TargetInstrInfo() {
Chris Lattner93fa7052002-10-28 23:55:33 +000035}
36
Evan Cheng15993f82011-06-27 21:26:13 +000037const TargetRegisterClass*
Evan Chenge837dea2011-06-28 19:10:37 +000038TargetInstrInfo::getRegClass(const MCInstrDesc &MCID, unsigned OpNum,
Evan Cheng15993f82011-06-27 21:26:13 +000039 const TargetRegisterInfo *TRI) const {
Evan Chenge837dea2011-06-28 19:10:37 +000040 if (OpNum >= MCID.getNumOperands())
Evan Cheng15993f82011-06-27 21:26:13 +000041 return 0;
42
Evan Chenge837dea2011-06-28 19:10:37 +000043 short RegClass = MCID.OpInfo[OpNum].RegClass;
44 if (MCID.OpInfo[OpNum].isLookupPtrRegClass())
Evan Cheng15993f82011-06-27 21:26:13 +000045 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 Cheng5f54ce32010-09-09 18:18:55 +000055unsigned
Evan Cheng8239daf2010-11-03 00:45:17 +000056TargetInstrInfo::getNumMicroOps(const InstrItineraryData *ItinData,
57 const MachineInstr *MI) const {
Evan Cheng3ef1c872010-09-10 01:29:16 +000058 if (!ItinData || ItinData->isEmpty())
Evan Cheng5f54ce32010-09-09 18:18:55 +000059 return 1;
60
61 unsigned Class = MI->getDesc().getSchedClass();
Bob Wilson064312d2010-09-15 16:28:21 +000062 unsigned UOps = ItinData->Itineraries[Class].NumMicroOps;
Evan Cheng5f54ce32010-09-09 18:18:55 +000063 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 Chenga0792de2010-10-06 06:27:31 +000071int
72TargetInstrInfo::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
83int
84TargetInstrInfo::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 Cheng8239daf2010-11-03 00:45:17 +0000100int 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
109int 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 Chengc8141df2010-10-26 02:08:50 +0000120bool 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 Chenga0792de2010-10-06 06:27:31 +0000130
Chris Lattnerb6bbfebd2009-08-02 04:58:19 +0000131/// insertNoop - Insert a noop into the instruction stream at the specified
132/// point.
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000133void TargetInstrInfo::insertNoop(MachineBasicBlock &MBB,
Chris Lattnerb6bbfebd2009-08-02 04:58:19 +0000134 MachineBasicBlock::iterator MI) const {
135 llvm_unreachable("Target didn't implement insertNoop!");
136}
137
138
Evan Chengbfd2ec42007-06-08 21:59:56 +0000139bool TargetInstrInfo::isUnpredicatedTerminator(const MachineInstr *MI) const {
Evan Chenge837dea2011-06-28 19:10:37 +0000140 const MCInstrDesc &MCID = MI->getDesc();
141 if (!MCID.isTerminator()) return false;
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000142
Chris Lattner69244302008-01-07 01:56:04 +0000143 // Conditional branch is a special case.
Evan Chenge837dea2011-06-28 19:10:37 +0000144 if (MCID.isBranch() && !MCID.isBarrier())
Chris Lattner69244302008-01-07 01:56:04 +0000145 return true;
Evan Chenge837dea2011-06-28 19:10:37 +0000146 if (!MCID.isPredicable())
Chris Lattner69244302008-01-07 01:56:04 +0000147 return true;
148 return !isPredicated(MI);
Evan Chengbfd2ec42007-06-08 21:59:56 +0000149}
Evan Chengd923fc62009-05-05 00:30:09 +0000150
Chris Lattnercb778a82009-07-29 21:10:12 +0000151
Chris Lattnerd90183d2009-08-02 05:20:37 +0000152/// Measure the specified inline asm to determine an approximation of its
153/// length.
Jim Grosbachd31d3042011-03-24 18:46:34 +0000154/// Comments (which run till the next SeparatorString or newline) do not
Chris Lattnerd90183d2009-08-02 05:20:37 +0000155/// count as an instruction.
156/// Any other non-whitespace text is considered an instruction, with
Jim Grosbachd31d3042011-03-24 18:46:34 +0000157/// multiple instructions separated by SeparatorString or newlines.
Chris Lattnerd90183d2009-08-02 05:20:37 +0000158/// Variable-length instructions are not handled here; this function
159/// may be overloaded in the target code to do that.
160unsigned TargetInstrInfo::getInlineAsmLength(const char *Str,
Chris Lattner33adcfb2009-08-22 21:43:10 +0000161 const MCAsmInfo &MAI) const {
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000162
163
Chris Lattnerd90183d2009-08-02 05:20:37 +0000164 // Count the number of instructions in the asm.
165 bool atInsnStart = true;
166 unsigned Length = 0;
167 for (; *Str; ++Str) {
Jim Grosbachd31d3042011-03-24 18:46:34 +0000168 if (*Str == '\n' || strncmp(Str, MAI.getSeparatorString(),
169 strlen(MAI.getSeparatorString())) == 0)
Chris Lattnerd90183d2009-08-02 05:20:37 +0000170 atInsnStart = true;
Nick Lewycky24021232010-12-19 20:42:43 +0000171 if (atInsnStart && !std::isspace(*Str)) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000172 Length += MAI.getMaxInstLength();
Chris Lattnerd90183d2009-08-02 05:20:37 +0000173 atInsnStart = false;
174 }
Chris Lattner33adcfb2009-08-22 21:43:10 +0000175 if (atInsnStart && strncmp(Str, MAI.getCommentString(),
176 strlen(MAI.getCommentString())) == 0)
Chris Lattnerd90183d2009-08-02 05:20:37 +0000177 atInsnStart = false;
178 }
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000179
Chris Lattnerd90183d2009-08-02 05:20:37 +0000180 return Length;
181}