blob: d28f1e7c50531ac8985a6bd889e4e598281053ad [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//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source 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"
Chris Lattnera187ed92002-11-17 22:53:03 +000015#include "llvm/CodeGen/MachineInstr.h"
Chris Lattner93fa7052002-10-28 23:55:33 +000016#include "llvm/Constant.h"
17#include "llvm/DerivedTypes.h"
Chris Lattner167b10c2005-01-19 06:53:34 +000018using namespace llvm;
Chris Lattner93fa7052002-10-28 23:55:33 +000019
Brian Gaeked0fde302003-11-11 22:41:34 +000020namespace llvm {
Chris Lattner167b10c2005-01-19 06:53:34 +000021 // External object describing the machine instructions Initialized only when
22 // the TargetMachine class is created and reset when that class is destroyed.
Misha Brukmanf976c852005-04-21 22:55:34 +000023 //
Chris Lattner167b10c2005-01-19 06:53:34 +000024 // FIXME: UGLY SPARCV9 HACK!
25 const TargetInstrDescriptor* TargetInstrDescriptors = 0;
26}
Chris Lattner93fa7052002-10-28 23:55:33 +000027
Chris Lattner08084142003-01-13 00:26:36 +000028TargetInstrInfo::TargetInstrInfo(const TargetInstrDescriptor* Desc,
Misha Brukman7847fca2005-04-22 17:54:37 +000029 unsigned numOpcodes)
Chris Lattnerdce363d2004-02-29 06:31:44 +000030 : desc(Desc), NumOpcodes(numOpcodes) {
Chris Lattner93fa7052002-10-28 23:55:33 +000031 // FIXME: TargetInstrDescriptors should not be global
Brian Gaeke0f5918a2004-01-15 18:15:58 +000032 assert(TargetInstrDescriptors == NULL && desc != NULL
33 && "TargetMachine data structure corrupt; maybe you tried to create another TargetMachine? (only one may exist in a program)");
Misha Brukman7847fca2005-04-22 17:54:37 +000034 TargetInstrDescriptors = desc; // initialize global variable
Chris Lattner93fa7052002-10-28 23:55:33 +000035}
36
Chris Lattner08084142003-01-13 00:26:36 +000037TargetInstrInfo::~TargetInstrInfo() {
Misha Brukman7847fca2005-04-22 17:54:37 +000038 TargetInstrDescriptors = NULL; // reset global variable
Chris Lattner93fa7052002-10-28 23:55:33 +000039}
40
Evan Chenge6ae14e2006-11-01 23:18:32 +000041/// findTiedToSrcOperand - Returns the operand that is tied to the specified
42/// dest operand. Returns -1 if there isn't one.
Evan Cheng981b5bd2006-11-01 23:00:31 +000043int
Evan Chenge6ae14e2006-11-01 23:18:32 +000044TargetInstrInfo::findTiedToSrcOperand(MachineOpCode Opc, unsigned OpNum) const {
Evan Cheng981b5bd2006-11-01 23:00:31 +000045 for (unsigned i = 0, e = getNumOperands(Opc); i != e; ++i) {
46 if (i == OpNum)
47 continue;
48 int ti = getOperandConstraint(Opc, i, TIED_TO);
49 if (ti == (int)OpNum)
50 return i;
51 }
52 return -1;
53}
54
Chris Lattner93fa7052002-10-28 23:55:33 +000055
Chris Lattner167b10c2005-01-19 06:53:34 +000056// commuteInstruction - The default implementation of this method just exchanges
57// operand 1 and 2.
58MachineInstr *TargetInstrInfo::commuteInstruction(MachineInstr *MI) const {
59 assert(MI->getOperand(1).isRegister() && MI->getOperand(2).isRegister() &&
60 "This only knows how to commute register operands so far");
61 unsigned Reg1 = MI->getOperand(1).getReg();
Evan Cheng31ff1ff2006-05-12 01:46:26 +000062 unsigned Reg2 = MI->getOperand(2).getReg();
Chris Lattnere53f4a02006-05-04 17:52:23 +000063 MI->getOperand(2).setReg(Reg1);
64 MI->getOperand(1).setReg(Reg2);
Chris Lattner167b10c2005-01-19 06:53:34 +000065 return MI;
66}