blob: 7f103226daf949866fd139650e8fe8e00cab31c9 [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 Cheng5f54ce32010-09-09 18:18:55 +000015#include "llvm/Target/TargetInstrItineraries.h"
Evan Chengd923fc62009-05-05 00:30:09 +000016#include "llvm/Target/TargetRegisterInfo.h"
Evan Chenga0792de2010-10-06 06:27:31 +000017#include "llvm/CodeGen/SelectionDAGNodes.h"
18#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerb6bbfebd2009-08-02 04:58:19 +000019#include "llvm/Support/ErrorHandling.h"
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//===----------------------------------------------------------------------===//
23// TargetOperandInfo
24//===----------------------------------------------------------------------===//
25
26/// getRegClass - Get the register class for the operand, handling resolution
27/// of "symbolic" pointer register classes etc. If this is not a register
28/// operand, this returns null.
29const TargetRegisterClass *
30TargetOperandInfo::getRegClass(const TargetRegisterInfo *TRI) const {
31 if (isLookupPtrRegClass())
32 return TRI->getPointerRegClass(RegClass);
Dan Gohmana606d952010-06-18 18:13:55 +000033 // Instructions like INSERT_SUBREG do not have fixed register classes.
34 if (RegClass < 0)
35 return 0;
36 // Otherwise just look it up normally.
Chris Lattnerd90183d2009-08-02 05:20:37 +000037 return TRI->getRegClass(RegClass);
38}
39
40//===----------------------------------------------------------------------===//
41// TargetInstrInfo
42//===----------------------------------------------------------------------===//
43
Chris Lattner749c6f62008-01-07 07:27:27 +000044TargetInstrInfo::TargetInstrInfo(const TargetInstrDesc* Desc,
Misha Brukman7847fca2005-04-22 17:54:37 +000045 unsigned numOpcodes)
Chris Lattner749c6f62008-01-07 07:27:27 +000046 : Descriptors(Desc), NumOpcodes(numOpcodes) {
Chris Lattner93fa7052002-10-28 23:55:33 +000047}
48
Chris Lattner08084142003-01-13 00:26:36 +000049TargetInstrInfo::~TargetInstrInfo() {
Chris Lattner93fa7052002-10-28 23:55:33 +000050}
51
Evan Cheng5f54ce32010-09-09 18:18:55 +000052unsigned
53TargetInstrInfo::getNumMicroOps(const MachineInstr *MI,
Evan Cheng3ef1c872010-09-10 01:29:16 +000054 const InstrItineraryData *ItinData) const {
55 if (!ItinData || ItinData->isEmpty())
Evan Cheng5f54ce32010-09-09 18:18:55 +000056 return 1;
57
58 unsigned Class = MI->getDesc().getSchedClass();
Bob Wilson064312d2010-09-15 16:28:21 +000059 unsigned UOps = ItinData->Itineraries[Class].NumMicroOps;
Evan Cheng5f54ce32010-09-09 18:18:55 +000060 if (UOps)
61 return UOps;
62
63 // The # of u-ops is dynamically determined. The specific target should
64 // override this function to return the right number.
65 return 1;
66}
67
Evan Chenga0792de2010-10-06 06:27:31 +000068int
69TargetInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
70 const MachineInstr *DefMI, unsigned DefIdx,
71 const MachineInstr *UseMI, unsigned UseIdx) const {
72 if (!ItinData || ItinData->isEmpty())
73 return -1;
74
75 unsigned DefClass = DefMI->getDesc().getSchedClass();
76 unsigned UseClass = UseMI->getDesc().getSchedClass();
77 return ItinData->getOperandLatency(DefClass, DefIdx, UseClass, UseIdx);
78}
79
80int
81TargetInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
82 SDNode *DefNode, unsigned DefIdx,
83 SDNode *UseNode, unsigned UseIdx) const {
84 if (!ItinData || ItinData->isEmpty())
85 return -1;
86
87 if (!DefNode->isMachineOpcode())
88 return -1;
89
90 unsigned DefClass = get(DefNode->getMachineOpcode()).getSchedClass();
91 if (!UseNode->isMachineOpcode())
92 return ItinData->getOperandCycle(DefClass, DefIdx);
93 unsigned UseClass = get(UseNode->getMachineOpcode()).getSchedClass();
94 return ItinData->getOperandLatency(DefClass, DefIdx, UseClass, UseIdx);
95}
96
97
Chris Lattnerb6bbfebd2009-08-02 04:58:19 +000098/// insertNoop - Insert a noop into the instruction stream at the specified
99/// point.
100void TargetInstrInfo::insertNoop(MachineBasicBlock &MBB,
101 MachineBasicBlock::iterator MI) const {
102 llvm_unreachable("Target didn't implement insertNoop!");
103}
104
105
Evan Chengbfd2ec42007-06-08 21:59:56 +0000106bool TargetInstrInfo::isUnpredicatedTerminator(const MachineInstr *MI) const {
Chris Lattner749c6f62008-01-07 07:27:27 +0000107 const TargetInstrDesc &TID = MI->getDesc();
108 if (!TID.isTerminator()) return false;
Chris Lattner69244302008-01-07 01:56:04 +0000109
110 // Conditional branch is a special case.
Chris Lattner749c6f62008-01-07 07:27:27 +0000111 if (TID.isBranch() && !TID.isBarrier())
Chris Lattner69244302008-01-07 01:56:04 +0000112 return true;
Chris Lattner749c6f62008-01-07 07:27:27 +0000113 if (!TID.isPredicable())
Chris Lattner69244302008-01-07 01:56:04 +0000114 return true;
115 return !isPredicated(MI);
Evan Chengbfd2ec42007-06-08 21:59:56 +0000116}
Evan Chengd923fc62009-05-05 00:30:09 +0000117
Chris Lattnercb778a82009-07-29 21:10:12 +0000118
Chris Lattnerd90183d2009-08-02 05:20:37 +0000119/// Measure the specified inline asm to determine an approximation of its
120/// length.
121/// Comments (which run till the next SeparatorChar or newline) do not
122/// count as an instruction.
123/// Any other non-whitespace text is considered an instruction, with
124/// multiple instructions separated by SeparatorChar or newlines.
125/// Variable-length instructions are not handled here; this function
126/// may be overloaded in the target code to do that.
127unsigned TargetInstrInfo::getInlineAsmLength(const char *Str,
Chris Lattner33adcfb2009-08-22 21:43:10 +0000128 const MCAsmInfo &MAI) const {
Chris Lattnerd90183d2009-08-02 05:20:37 +0000129
130
131 // Count the number of instructions in the asm.
132 bool atInsnStart = true;
133 unsigned Length = 0;
134 for (; *Str; ++Str) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000135 if (*Str == '\n' || *Str == MAI.getSeparatorChar())
Chris Lattnerd90183d2009-08-02 05:20:37 +0000136 atInsnStart = true;
137 if (atInsnStart && !isspace(*Str)) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000138 Length += MAI.getMaxInstLength();
Chris Lattnerd90183d2009-08-02 05:20:37 +0000139 atInsnStart = false;
140 }
Chris Lattner33adcfb2009-08-22 21:43:10 +0000141 if (atInsnStart && strncmp(Str, MAI.getCommentString(),
142 strlen(MAI.getCommentString())) == 0)
Chris Lattnerd90183d2009-08-02 05:20:37 +0000143 atInsnStart = false;
144 }
145
146 return Length;
147}