blob: 440f9ad00de9facc0296dd98d17da8de63a915f1 [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,
Evan Cheng15993f82011-06-27 21:26:13 +000031 const TargetRegisterInfo *TRI) const {
Evan Chenge837dea2011-06-28 19:10:37 +000032 if (OpNum >= MCID.getNumOperands())
Evan Cheng15993f82011-06-27 21:26:13 +000033 return 0;
34
Evan Chenge837dea2011-06-28 19:10:37 +000035 short RegClass = MCID.OpInfo[OpNum].RegClass;
36 if (MCID.OpInfo[OpNum].isLookupPtrRegClass())
Evan Cheng15993f82011-06-27 21:26:13 +000037 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 Cheng5f54ce32010-09-09 18:18:55 +000047unsigned
Evan Cheng8239daf2010-11-03 00:45:17 +000048TargetInstrInfo::getNumMicroOps(const InstrItineraryData *ItinData,
49 const MachineInstr *MI) const {
Evan Cheng3ef1c872010-09-10 01:29:16 +000050 if (!ItinData || ItinData->isEmpty())
Evan Cheng5f54ce32010-09-09 18:18:55 +000051 return 1;
52
53 unsigned Class = MI->getDesc().getSchedClass();
Bob Wilson064312d2010-09-15 16:28:21 +000054 unsigned UOps = ItinData->Itineraries[Class].NumMicroOps;
Evan Cheng5f54ce32010-09-09 18:18:55 +000055 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 Chenga0792de2010-10-06 06:27:31 +000063int
64TargetInstrInfo::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 Cheng8239daf2010-11-03 00:45:17 +000075int 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 Chengc8141df2010-10-26 02:08:50 +000084bool 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 Chenga0792de2010-10-06 06:27:31 +000094
Chris Lattnerb6bbfebd2009-08-02 04:58:19 +000095/// insertNoop - Insert a noop into the instruction stream at the specified
96/// point.
Andrew Trick6e8f4c42010-12-24 04:28:06 +000097void TargetInstrInfo::insertNoop(MachineBasicBlock &MBB,
Chris Lattnerb6bbfebd2009-08-02 04:58:19 +000098 MachineBasicBlock::iterator MI) const {
99 llvm_unreachable("Target didn't implement insertNoop!");
100}
101
102
Chris Lattnerd90183d2009-08-02 05:20:37 +0000103/// Measure the specified inline asm to determine an approximation of its
104/// length.
Jim Grosbachd31d3042011-03-24 18:46:34 +0000105/// Comments (which run till the next SeparatorString or newline) do not
Chris Lattnerd90183d2009-08-02 05:20:37 +0000106/// count as an instruction.
107/// Any other non-whitespace text is considered an instruction, with
Jim Grosbachd31d3042011-03-24 18:46:34 +0000108/// multiple instructions separated by SeparatorString or newlines.
Chris Lattnerd90183d2009-08-02 05:20:37 +0000109/// Variable-length instructions are not handled here; this function
110/// may be overloaded in the target code to do that.
111unsigned TargetInstrInfo::getInlineAsmLength(const char *Str,
Chris Lattner33adcfb2009-08-22 21:43:10 +0000112 const MCAsmInfo &MAI) const {
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000113
114
Chris Lattnerd90183d2009-08-02 05:20:37 +0000115 // Count the number of instructions in the asm.
116 bool atInsnStart = true;
117 unsigned Length = 0;
118 for (; *Str; ++Str) {
Jim Grosbachd31d3042011-03-24 18:46:34 +0000119 if (*Str == '\n' || strncmp(Str, MAI.getSeparatorString(),
120 strlen(MAI.getSeparatorString())) == 0)
Chris Lattnerd90183d2009-08-02 05:20:37 +0000121 atInsnStart = true;
Nick Lewycky24021232010-12-19 20:42:43 +0000122 if (atInsnStart && !std::isspace(*Str)) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000123 Length += MAI.getMaxInstLength();
Chris Lattnerd90183d2009-08-02 05:20:37 +0000124 atInsnStart = false;
125 }
Chris Lattner33adcfb2009-08-22 21:43:10 +0000126 if (atInsnStart && strncmp(Str, MAI.getCommentString(),
127 strlen(MAI.getCommentString())) == 0)
Chris Lattnerd90183d2009-08-02 05:20:37 +0000128 atInsnStart = false;
129 }
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000130
Chris Lattnerd90183d2009-08-02 05:20:37 +0000131 return Length;
132}