Rename
[oota-llvm.git] / lib / Target / TargetInstrInfo.cpp
1 //===-- TargetInstrInfo.cpp - Target Instruction Information --------------===//
2 //
3 //                     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.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the TargetInstrInfo class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Target/TargetInstrInfo.h"
15 #include "llvm/CodeGen/MachineInstr.h"
16 #include "llvm/Constant.h"
17 #include "llvm/DerivedTypes.h"
18 using namespace llvm;
19
20 namespace llvm {
21   // External object describing the machine instructions Initialized only when
22   // the TargetMachine class is created and reset when that class is destroyed.
23   //
24   // FIXME: UGLY SPARCV9 HACK!
25   const TargetInstrDescriptor* TargetInstrDescriptors = 0;
26 }
27
28 TargetInstrInfo::TargetInstrInfo(const TargetInstrDescriptor* Desc,
29                                  unsigned numOpcodes)
30   : desc(Desc), NumOpcodes(numOpcodes) {
31   // FIXME: TargetInstrDescriptors should not be global
32   assert(TargetInstrDescriptors == NULL && desc != NULL
33          && "TargetMachine data structure corrupt; maybe you tried to create another TargetMachine? (only one may exist in a program)");
34   TargetInstrDescriptors = desc; // initialize global variable
35 }
36
37 TargetInstrInfo::~TargetInstrInfo() {
38   TargetInstrDescriptors = NULL; // reset global variable
39 }
40
41 /// findTiedToSrcOperand - Returns the operand that is tied to the specified
42 /// dest operand. Returns -1 if there isn't one.
43 int
44 TargetInstrInfo::findTiedToSrcOperand(MachineOpCode Opc, unsigned OpNum) const {
45   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
55
56 // commuteInstruction - The default implementation of this method just exchanges
57 // operand 1 and 2.
58 MachineInstr *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();
62   unsigned Reg2 = MI->getOperand(2).getReg();
63   MI->getOperand(2).setReg(Reg1);
64   MI->getOperand(1).setReg(Reg2);
65   return MI;
66 }