blob: 677f36d91ba4c591c927ef24b3f07d63dacfd91c [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,
Chris Lattnerdce363d2004-02-29 06:31:44 +000029 unsigned numOpcodes)
30 : 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)");
Chris Lattner93fa7052002-10-28 23:55:33 +000034 TargetInstrDescriptors = desc; // initialize global variable
35}
36
Chris Lattner08084142003-01-13 00:26:36 +000037TargetInstrInfo::~TargetInstrInfo() {
Chris Lattner93fa7052002-10-28 23:55:33 +000038 TargetInstrDescriptors = NULL; // reset global variable
39}
40
Chris Lattner167b10c2005-01-19 06:53:34 +000041// FIXME: SPARCV9 SPECIFIC!
Chris Lattner08084142003-01-13 00:26:36 +000042bool TargetInstrInfo::constantFitsInImmedField(MachineOpCode opCode,
43 int64_t intValue) const {
Chris Lattner93fa7052002-10-28 23:55:33 +000044 // 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.
Misha Brukmanf976c852005-04-21 22:55:34 +000052
Chris Lattner93fa7052002-10-28 23:55:33 +000053 // Now check if the constant fits
54 if (intValue <= (int64_t) maxImmedValue &&
55 intValue >= -((int64_t) maxImmedValue+1))
56 return true;
57 }
Misha Brukmanf976c852005-04-21 22:55:34 +000058
Chris Lattner93fa7052002-10-28 23:55:33 +000059 return false;
60}
61
Chris Lattner167b10c2005-01-19 06:53:34 +000062// commuteInstruction - The default implementation of this method just exchanges
63// operand 1 and 2.
64MachineInstr *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}