aa94fbc2ae6ae10147d56bcb23bb85be99f92b1e
[oota-llvm.git] / lib / Target / TargetInstrInfo.cpp
1 //===-- TargetInstrInfo.cpp - Target Instruction Information --------------===//
2 //
3 //
4 //===----------------------------------------------------------------------===//
5
6 #include "llvm/Target/TargetInstrInfo.h"
7 #include "llvm/CodeGen/MachineInstr.h"
8 #include "llvm/Constant.h"
9 #include "llvm/DerivedTypes.h"
10
11 // External object describing the machine instructions
12 // Initialized only when the TargetMachine class is created
13 // and reset when that class is destroyed.
14 // 
15 const TargetInstrDescriptor* TargetInstrDescriptors = 0;
16
17
18 TargetInstrInfo::TargetInstrInfo(const TargetInstrDescriptor* Desc,
19                                  unsigned DescSize,
20                                  unsigned NumRealOpCodes)
21   : desc(Desc), descSize(DescSize), numRealOpCodes(NumRealOpCodes) {
22   // FIXME: TargetInstrDescriptors should not be global
23   assert(TargetInstrDescriptors == NULL && desc != NULL);
24   TargetInstrDescriptors = desc;        // initialize global variable
25 }
26
27 TargetInstrInfo::~TargetInstrInfo() {
28   TargetInstrDescriptors = NULL;        // reset global variable
29 }
30
31 bool TargetInstrInfo::constantFitsInImmedField(MachineOpCode opCode,
32                                                int64_t intValue) const {
33   // First, check if opCode has an immed field.
34   bool isSignExtended;
35   uint64_t maxImmedValue = maxImmedConstant(opCode, isSignExtended);
36   if (maxImmedValue != 0)
37     {
38       // NEED TO HANDLE UNSIGNED VALUES SINCE THEY MAY BECOME MUCH
39       // SMALLER AFTER CASTING TO SIGN-EXTENDED int, short, or char.
40       // See CreateUIntSetInstruction in SparcInstrInfo.cpp.
41       
42       // Now check if the constant fits
43       if (intValue <= (int64_t) maxImmedValue &&
44           intValue >= -((int64_t) maxImmedValue+1))
45         return true;
46     }
47   
48   return false;
49 }
50
51 bool TargetInstrInfo::ConstantTypeMustBeLoaded(const Constant* CV) const {
52   assert(CV->getType()->isPrimitiveType() || isa<PointerType>(CV->getType()));
53   return !(CV->getType()->isIntegral() || isa<PointerType>(CV->getType()));
54 }