blob: f1d1d07c38aef048793903f90f9516679825148c [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
Andrew Trickc36d0332012-06-08 17:23:27 +000024//
25// Methods that depend on CodeGen are implemented in
26// TargetInstrInfoImpl.cpp. Invoking them without linking libCodeGen raises a
27// link error.
28// ===----------------------------------------------------------------------===//
Chris Lattnerd90183d2009-08-02 05:20:37 +000029
Chris Lattner08084142003-01-13 00:26:36 +000030TargetInstrInfo::~TargetInstrInfo() {
Chris Lattner93fa7052002-10-28 23:55:33 +000031}
32
Evan Cheng15993f82011-06-27 21:26:13 +000033const TargetRegisterClass*
Evan Chenge837dea2011-06-28 19:10:37 +000034TargetInstrInfo::getRegClass(const MCInstrDesc &MCID, unsigned OpNum,
Jakob Stoklund Olesen397fc482012-05-07 22:10:26 +000035 const TargetRegisterInfo *TRI,
36 const MachineFunction &MF) const {
Evan Chenge837dea2011-06-28 19:10:37 +000037 if (OpNum >= MCID.getNumOperands())
Evan Cheng15993f82011-06-27 21:26:13 +000038 return 0;
39
Evan Chenge837dea2011-06-28 19:10:37 +000040 short RegClass = MCID.OpInfo[OpNum].RegClass;
41 if (MCID.OpInfo[OpNum].isLookupPtrRegClass())
Jakob Stoklund Olesen397fc482012-05-07 22:10:26 +000042 return TRI->getPointerRegClass(MF, RegClass);
Evan Cheng15993f82011-06-27 21:26:13 +000043
44 // Instructions like INSERT_SUBREG do not have fixed register classes.
45 if (RegClass < 0)
46 return 0;
47
48 // Otherwise just look it up normally.
49 return TRI->getRegClass(RegClass);
50}
51
Chris Lattnerb6bbfebd2009-08-02 04:58:19 +000052/// insertNoop - Insert a noop into the instruction stream at the specified
53/// point.
Andrew Trick6e8f4c42010-12-24 04:28:06 +000054void TargetInstrInfo::insertNoop(MachineBasicBlock &MBB,
Chris Lattnerb6bbfebd2009-08-02 04:58:19 +000055 MachineBasicBlock::iterator MI) const {
56 llvm_unreachable("Target didn't implement insertNoop!");
57}
58
Chris Lattnerd90183d2009-08-02 05:20:37 +000059/// Measure the specified inline asm to determine an approximation of its
60/// length.
Jim Grosbachd31d3042011-03-24 18:46:34 +000061/// Comments (which run till the next SeparatorString or newline) do not
Chris Lattnerd90183d2009-08-02 05:20:37 +000062/// count as an instruction.
63/// Any other non-whitespace text is considered an instruction, with
Jim Grosbachd31d3042011-03-24 18:46:34 +000064/// multiple instructions separated by SeparatorString or newlines.
Chris Lattnerd90183d2009-08-02 05:20:37 +000065/// Variable-length instructions are not handled here; this function
66/// may be overloaded in the target code to do that.
67unsigned TargetInstrInfo::getInlineAsmLength(const char *Str,
Chris Lattner33adcfb2009-08-22 21:43:10 +000068 const MCAsmInfo &MAI) const {
Andrew Trick6e8f4c42010-12-24 04:28:06 +000069
70
Chris Lattnerd90183d2009-08-02 05:20:37 +000071 // Count the number of instructions in the asm.
72 bool atInsnStart = true;
73 unsigned Length = 0;
74 for (; *Str; ++Str) {
Jim Grosbachd31d3042011-03-24 18:46:34 +000075 if (*Str == '\n' || strncmp(Str, MAI.getSeparatorString(),
76 strlen(MAI.getSeparatorString())) == 0)
Chris Lattnerd90183d2009-08-02 05:20:37 +000077 atInsnStart = true;
Nick Lewycky24021232010-12-19 20:42:43 +000078 if (atInsnStart && !std::isspace(*Str)) {
Chris Lattner33adcfb2009-08-22 21:43:10 +000079 Length += MAI.getMaxInstLength();
Chris Lattnerd90183d2009-08-02 05:20:37 +000080 atInsnStart = false;
81 }
Chris Lattner33adcfb2009-08-22 21:43:10 +000082 if (atInsnStart && strncmp(Str, MAI.getCommentString(),
83 strlen(MAI.getCommentString())) == 0)
Chris Lattnerd90183d2009-08-02 05:20:37 +000084 atInsnStart = false;
85 }
Andrew Trick6e8f4c42010-12-24 04:28:06 +000086
Chris Lattnerd90183d2009-08-02 05:20:37 +000087 return Length;
88}