Finegrainify namespacification
[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 // FIXME: SPARCV9 SPECIFIC!
42 bool TargetInstrInfo::constantFitsInImmedField(MachineOpCode opCode,
43                                                int64_t intValue) const {
44   // First, check if opCode has an immed field.
45   bool isSignExtended;
46   uint64_t maxImmedValue = maxImmedConstant(opCode, isSignExtended);
47   if (maxImmedValue != 0)
48     {
49       // NEED TO HANDLE UNSIGNED VALUES SINCE THEY MAY BECOME MUCH
50       // SMALLER AFTER CASTING TO SIGN-EXTENDED int, short, or char.
51       // See CreateUIntSetInstruction in SparcInstrInfo.cpp.
52       
53       // Now check if the constant fits
54       if (intValue <= (int64_t) maxImmedValue &&
55           intValue >= -((int64_t) maxImmedValue+1))
56         return true;
57     }
58   
59   return false;
60 }
61
62 // commuteInstruction - The default implementation of this method just exchanges
63 // operand 1 and 2.
64 MachineInstr *TargetInstrInfo::commuteInstruction(MachineInstr *MI) const {
65   assert(MI->getOperand(1).isRegister() && MI->getOperand(2).isRegister() &&
66          "This only knows how to commute register operands so far");
67   unsigned Reg1 = MI->getOperand(1).getReg();
68   unsigned Reg2 = MI->getOperand(1).getReg();
69   MI->SetMachineOperandReg(2, Reg1);
70   MI->SetMachineOperandReg(1, Reg2);
71   return MI;
72 }