Mark a static array as const.
[oota-llvm.git] / lib / Target / TargetInstrInfo.cpp
1 //===-- TargetInstrInfo.cpp - Target Instruction Information --------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the TargetInstrInfo class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Target/TargetInstrInfo.h"
15 #include "llvm/Target/TargetRegisterInfo.h"
16 #include "llvm/MC/MCAsmInfo.h"
17 #include "llvm/MC/MCInstrItineraries.h"
18 #include "llvm/Support/ErrorHandling.h"
19 #include <cctype>
20 using namespace llvm;
21
22 //===----------------------------------------------------------------------===//
23 //  TargetInstrInfo
24 //===----------------------------------------------------------------------===//
25
26 TargetInstrInfo::~TargetInstrInfo() {
27 }
28
29 const TargetRegisterClass*
30 TargetInstrInfo::getRegClass(const MCInstrDesc &MCID, unsigned OpNum,
31                              const TargetRegisterInfo *TRI,
32                              const MachineFunction &MF) const {
33   if (OpNum >= MCID.getNumOperands())
34     return 0;
35
36   short RegClass = MCID.OpInfo[OpNum].RegClass;
37   if (MCID.OpInfo[OpNum].isLookupPtrRegClass())
38     return TRI->getPointerRegClass(MF, RegClass);
39
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
48 unsigned
49 TargetInstrInfo::getNumMicroOps(const InstrItineraryData *ItinData,
50                                 const MachineInstr *MI) const {
51   if (!ItinData || ItinData->isEmpty())
52     return 1;
53
54   unsigned Class = MI->getDesc().getSchedClass();
55   unsigned UOps = ItinData->Itineraries[Class].NumMicroOps;
56   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
64 int
65 TargetInstrInfo::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
76 int 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
85 bool 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 }
95
96 /// insertNoop - Insert a noop into the instruction stream at the specified
97 /// point.
98 void TargetInstrInfo::insertNoop(MachineBasicBlock &MBB,
99                                  MachineBasicBlock::iterator MI) const {
100   llvm_unreachable("Target didn't implement insertNoop!");
101 }
102
103
104 /// Measure the specified inline asm to determine an approximation of its
105 /// length.
106 /// Comments (which run till the next SeparatorString or newline) do not
107 /// count as an instruction.
108 /// Any other non-whitespace text is considered an instruction, with
109 /// multiple instructions separated by SeparatorString or newlines.
110 /// Variable-length instructions are not handled here; this function
111 /// may be overloaded in the target code to do that.
112 unsigned TargetInstrInfo::getInlineAsmLength(const char *Str,
113                                              const MCAsmInfo &MAI) const {
114
115
116   // Count the number of instructions in the asm.
117   bool atInsnStart = true;
118   unsigned Length = 0;
119   for (; *Str; ++Str) {
120     if (*Str == '\n' || strncmp(Str, MAI.getSeparatorString(),
121                                 strlen(MAI.getSeparatorString())) == 0)
122       atInsnStart = true;
123     if (atInsnStart && !std::isspace(*Str)) {
124       Length += MAI.getMaxInstLength();
125       atInsnStart = false;
126     }
127     if (atInsnStart && strncmp(Str, MAI.getCommentString(),
128                                strlen(MAI.getCommentString())) == 0)
129       atInsnStart = false;
130   }
131
132   return Length;
133 }