blob: f1d1d07c38aef048793903f90f9516679825148c [file] [log] [blame]
Chris Lattner0d5644b2003-01-13 00:26:36 +00001//===-- TargetInstrInfo.cpp - Target Instruction Information --------------===//
Misha Brukman10468d82005-04-21 22:55:34 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Brukman10468d82005-04-21 22:55:34 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner910b82f2002-10-28 23:55:33 +00009//
Chris Lattnerf6932b72005-01-19 06:53:34 +000010// This file implements the TargetInstrInfo class.
Chris Lattner910b82f2002-10-28 23:55:33 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattnerb4d58d72003-01-14 22:00:31 +000014#include "llvm/Target/TargetInstrInfo.h"
Evan Cheng1ff27272009-05-05 00:30:09 +000015#include "llvm/Target/TargetRegisterInfo.h"
Evan Cheng49d4c0b2010-10-06 06:27:31 +000016#include "llvm/MC/MCAsmInfo.h"
Evan Cheng8264e272011-06-29 01:14:12 +000017#include "llvm/MC/MCInstrItineraries.h"
Chris Lattner01614192009-08-02 04:58:19 +000018#include "llvm/Support/ErrorHandling.h"
Nick Lewycky0de20af2010-12-19 20:43:38 +000019#include <cctype>
Chris Lattnerf6932b72005-01-19 06:53:34 +000020using namespace llvm;
Chris Lattner910b82f2002-10-28 23:55:33 +000021
Chris Lattnere98a3c32009-08-02 05:20:37 +000022//===----------------------------------------------------------------------===//
Chris Lattnere98a3c32009-08-02 05:20:37 +000023// TargetInstrInfo
Andrew Trick596af1b2012-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 Lattnere98a3c32009-08-02 05:20:37 +000029
Chris Lattner0d5644b2003-01-13 00:26:36 +000030TargetInstrInfo::~TargetInstrInfo() {
Chris Lattner910b82f2002-10-28 23:55:33 +000031}
32
Evan Cheng8d71a752011-06-27 21:26:13 +000033const TargetRegisterClass*
Evan Cheng6cc775f2011-06-28 19:10:37 +000034TargetInstrInfo::getRegClass(const MCInstrDesc &MCID, unsigned OpNum,
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +000035 const TargetRegisterInfo *TRI,
36 const MachineFunction &MF) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +000037 if (OpNum >= MCID.getNumOperands())
Evan Cheng8d71a752011-06-27 21:26:13 +000038 return 0;
39
Evan Cheng6cc775f2011-06-28 19:10:37 +000040 short RegClass = MCID.OpInfo[OpNum].RegClass;
41 if (MCID.OpInfo[OpNum].isLookupPtrRegClass())
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +000042 return TRI->getPointerRegClass(MF, RegClass);
Evan Cheng8d71a752011-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 Lattner01614192009-08-02 04:58:19 +000052/// insertNoop - Insert a noop into the instruction stream at the specified
53/// point.
Andrew Trickc416ba62010-12-24 04:28:06 +000054void TargetInstrInfo::insertNoop(MachineBasicBlock &MBB,
Chris Lattner01614192009-08-02 04:58:19 +000055 MachineBasicBlock::iterator MI) const {
56 llvm_unreachable("Target didn't implement insertNoop!");
57}
58
Chris Lattnere98a3c32009-08-02 05:20:37 +000059/// Measure the specified inline asm to determine an approximation of its
60/// length.
Jim Grosbacha3df87f2011-03-24 18:46:34 +000061/// Comments (which run till the next SeparatorString or newline) do not
Chris Lattnere98a3c32009-08-02 05:20:37 +000062/// count as an instruction.
63/// Any other non-whitespace text is considered an instruction, with
Jim Grosbacha3df87f2011-03-24 18:46:34 +000064/// multiple instructions separated by SeparatorString or newlines.
Chris Lattnere98a3c32009-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 Lattnere9a75a62009-08-22 21:43:10 +000068 const MCAsmInfo &MAI) const {
Andrew Trickc416ba62010-12-24 04:28:06 +000069
70
Chris Lattnere98a3c32009-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 Grosbacha3df87f2011-03-24 18:46:34 +000075 if (*Str == '\n' || strncmp(Str, MAI.getSeparatorString(),
76 strlen(MAI.getSeparatorString())) == 0)
Chris Lattnere98a3c32009-08-02 05:20:37 +000077 atInsnStart = true;
Nick Lewyckyb71afe82010-12-19 20:42:43 +000078 if (atInsnStart && !std::isspace(*Str)) {
Chris Lattnere9a75a62009-08-22 21:43:10 +000079 Length += MAI.getMaxInstLength();
Chris Lattnere98a3c32009-08-02 05:20:37 +000080 atInsnStart = false;
81 }
Chris Lattnere9a75a62009-08-22 21:43:10 +000082 if (atInsnStart && strncmp(Str, MAI.getCommentString(),
83 strlen(MAI.getCommentString())) == 0)
Chris Lattnere98a3c32009-08-02 05:20:37 +000084 atInsnStart = false;
85 }
Andrew Trickc416ba62010-12-24 04:28:06 +000086
Chris Lattnere98a3c32009-08-02 05:20:37 +000087 return Length;
88}