blob: 6088ba5cc35f8b468e8ccd2d54d46d8b4a8fecda [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/MC/MCAsmInfo.h"
Evan Chengab8be962011-06-29 01:14:12 +000017#include "llvm/MC/MCInstrItineraries.h"
Chris Lattnerb6bbfebd2009-08-02 04:58:19 +000018#include "llvm/Support/ErrorHandling.h"
Nick Lewycky476b2422010-12-19 20:43:38 +000019#include <cctype>
Chris Lattner167b10c2005-01-19 06:53:34 +000020using namespace llvm;
Chris Lattner93fa7052002-10-28 23:55:33 +000021
Chris Lattnerd90183d2009-08-02 05:20:37 +000022//===----------------------------------------------------------------------===//
Chris Lattnerd90183d2009-08-02 05:20:37 +000023// TargetInstrInfo
24//===----------------------------------------------------------------------===//
25
Chris Lattner08084142003-01-13 00:26:36 +000026TargetInstrInfo::~TargetInstrInfo() {
Chris Lattner93fa7052002-10-28 23:55:33 +000027}
28
Evan Cheng15993f82011-06-27 21:26:13 +000029const TargetRegisterClass*
Evan Chenge837dea2011-06-28 19:10:37 +000030TargetInstrInfo::getRegClass(const MCInstrDesc &MCID, unsigned OpNum,
Jakob Stoklund Olesen397fc482012-05-07 22:10:26 +000031 const TargetRegisterInfo *TRI,
32 const MachineFunction &MF) const {
Evan Chenge837dea2011-06-28 19:10:37 +000033 if (OpNum >= MCID.getNumOperands())
Evan Cheng15993f82011-06-27 21:26:13 +000034 return 0;
35
Evan Chenge837dea2011-06-28 19:10:37 +000036 short RegClass = MCID.OpInfo[OpNum].RegClass;
37 if (MCID.OpInfo[OpNum].isLookupPtrRegClass())
Jakob Stoklund Olesen397fc482012-05-07 22:10:26 +000038 return TRI->getPointerRegClass(MF, RegClass);
Evan Cheng15993f82011-06-27 21:26:13 +000039
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 Cheng5f54ce32010-09-09 18:18:55 +000048unsigned
Evan Cheng8239daf2010-11-03 00:45:17 +000049TargetInstrInfo::getNumMicroOps(const InstrItineraryData *ItinData,
50 const MachineInstr *MI) const {
Evan Cheng3ef1c872010-09-10 01:29:16 +000051 if (!ItinData || ItinData->isEmpty())
Evan Cheng5f54ce32010-09-09 18:18:55 +000052 return 1;
53
54 unsigned Class = MI->getDesc().getSchedClass();
Bob Wilson064312d2010-09-15 16:28:21 +000055 unsigned UOps = ItinData->Itineraries[Class].NumMicroOps;
Evan Cheng5f54ce32010-09-09 18:18:55 +000056 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 Chenga0792de2010-10-06 06:27:31 +000064int
65TargetInstrInfo::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 Cheng8239daf2010-11-03 00:45:17 +000076int 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 Chengc8141df2010-10-26 02:08:50 +000085bool 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 Chenga0792de2010-10-06 06:27:31 +000095
Chris Lattnerb6bbfebd2009-08-02 04:58:19 +000096/// insertNoop - Insert a noop into the instruction stream at the specified
97/// point.
Andrew Trick6e8f4c42010-12-24 04:28:06 +000098void TargetInstrInfo::insertNoop(MachineBasicBlock &MBB,
Chris Lattnerb6bbfebd2009-08-02 04:58:19 +000099 MachineBasicBlock::iterator MI) const {
100 llvm_unreachable("Target didn't implement insertNoop!");
101}
102
103
Chris Lattnerd90183d2009-08-02 05:20:37 +0000104/// Measure the specified inline asm to determine an approximation of its
105/// length.
Jim Grosbachd31d3042011-03-24 18:46:34 +0000106/// Comments (which run till the next SeparatorString or newline) do not
Chris Lattnerd90183d2009-08-02 05:20:37 +0000107/// count as an instruction.
108/// Any other non-whitespace text is considered an instruction, with
Jim Grosbachd31d3042011-03-24 18:46:34 +0000109/// multiple instructions separated by SeparatorString or newlines.
Chris Lattnerd90183d2009-08-02 05:20:37 +0000110/// Variable-length instructions are not handled here; this function
111/// may be overloaded in the target code to do that.
112unsigned TargetInstrInfo::getInlineAsmLength(const char *Str,
Chris Lattner33adcfb2009-08-22 21:43:10 +0000113 const MCAsmInfo &MAI) const {
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000114
115
Chris Lattnerd90183d2009-08-02 05:20:37 +0000116 // Count the number of instructions in the asm.
117 bool atInsnStart = true;
118 unsigned Length = 0;
119 for (; *Str; ++Str) {
Jim Grosbachd31d3042011-03-24 18:46:34 +0000120 if (*Str == '\n' || strncmp(Str, MAI.getSeparatorString(),
121 strlen(MAI.getSeparatorString())) == 0)
Chris Lattnerd90183d2009-08-02 05:20:37 +0000122 atInsnStart = true;
Nick Lewycky24021232010-12-19 20:42:43 +0000123 if (atInsnStart && !std::isspace(*Str)) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000124 Length += MAI.getMaxInstLength();
Chris Lattnerd90183d2009-08-02 05:20:37 +0000125 atInsnStart = false;
126 }
Chris Lattner33adcfb2009-08-22 21:43:10 +0000127 if (atInsnStart && strncmp(Str, MAI.getCommentString(),
128 strlen(MAI.getCommentString())) == 0)
Chris Lattnerd90183d2009-08-02 05:20:37 +0000129 atInsnStart = false;
130 }
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000131
Chris Lattnerd90183d2009-08-02 05:20:37 +0000132 return Length;
133}