Two changes:
[oota-llvm.git] / lib / Target / Sparc / SparcInstrInfo.cpp
1 //===- SparcV8InstrInfo.cpp - SparcV8 Instruction Information ---*- C++ -*-===//
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 contains the SparcV8 implementation of the TargetInstrInfo class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "SparcV8InstrInfo.h"
15 #include "SparcV8.h"
16 #include "llvm/CodeGen/MachineInstrBuilder.h"
17 #include "SparcV8GenInstrInfo.inc"
18 using namespace llvm;
19
20 SparcV8InstrInfo::SparcV8InstrInfo(SparcV8Subtarget &ST)
21   : TargetInstrInfo(SparcV8Insts, sizeof(SparcV8Insts)/sizeof(SparcV8Insts[0])),
22     RI(ST) {
23 }
24
25 static bool isZeroImm(const MachineOperand &op) {
26   return op.isImmediate() && op.getImmedValue() == 0;
27 }
28
29 /// Return true if the instruction is a register to register move and
30 /// leave the source and dest operands in the passed parameters.
31 ///
32 bool SparcV8InstrInfo::isMoveInstr(const MachineInstr &MI,
33                                    unsigned &SrcReg, unsigned &DstReg) const {
34   // We look for 3 kinds of patterns here:
35   // or with G0 or 0
36   // add with G0 or 0
37   // fmovs or FpMOVD (pseudo double move).
38   if (MI.getOpcode() == V8::ORrr || MI.getOpcode() == V8::ADDrr) {
39     if (MI.getOperand(1).getReg() == V8::G0) {
40       DstReg = MI.getOperand(0).getReg();
41       SrcReg = MI.getOperand(2).getReg();
42       return true;
43     } else if (MI.getOperand (2).getReg() == V8::G0) {
44       DstReg = MI.getOperand(0).getReg();
45       SrcReg = MI.getOperand(1).getReg();
46       return true;
47     }
48   } else if (MI.getOpcode() == V8::ORri || MI.getOpcode() == V8::ADDri &&
49              isZeroImm(MI.getOperand(2)) && MI.getOperand(1).isRegister()) {
50     DstReg = MI.getOperand(0).getReg();
51     SrcReg = MI.getOperand(1).getReg();
52     return true;
53   } else if (MI.getOpcode() == V8::FMOVS || MI.getOpcode() == V8::FpMOVD ||
54              MI.getOpcode() == V8::FMOVD) {
55     SrcReg = MI.getOperand(1).getReg();
56     DstReg = MI.getOperand(0).getReg();
57     return true;
58   }
59   return false;
60 }
61
62 /// isLoadFromStackSlot - If the specified machine instruction is a direct
63 /// load from a stack slot, return the virtual or physical register number of
64 /// the destination along with the FrameIndex of the loaded stack slot.  If
65 /// not, return 0.  This predicate must return 0 if the instruction has
66 /// any side effects other than loading from the stack slot.
67 unsigned SparcV8InstrInfo::isLoadFromStackSlot(MachineInstr *MI,
68                                                int &FrameIndex) const {
69   if (MI->getOpcode() == V8::LDri ||
70       MI->getOpcode() == V8::LDFri ||
71       MI->getOpcode() == V8::LDDFri) {
72     if (MI->getOperand(1).isFrameIndex() && MI->getOperand(2).isImmediate() &&
73         MI->getOperand(2).getImmedValue() == 0) {
74       FrameIndex = MI->getOperand(1).getFrameIndex();
75       return MI->getOperand(0).getReg();
76     }
77   }
78   return 0;
79 }
80
81 /// isStoreToStackSlot - If the specified machine instruction is a direct
82 /// store to a stack slot, return the virtual or physical register number of
83 /// the source reg along with the FrameIndex of the loaded stack slot.  If
84 /// not, return 0.  This predicate must return 0 if the instruction has
85 /// any side effects other than storing to the stack slot.
86 unsigned SparcV8InstrInfo::isStoreToStackSlot(MachineInstr *MI,
87                                               int &FrameIndex) const {
88   if (MI->getOpcode() == V8::STri ||
89       MI->getOpcode() == V8::STFri ||
90       MI->getOpcode() == V8::STDFri) {
91     if (MI->getOperand(0).isFrameIndex() && MI->getOperand(1).isImmediate() &&
92         MI->getOperand(1).getImmedValue() == 0) {
93       FrameIndex = MI->getOperand(0).getFrameIndex();
94       return MI->getOperand(2).getReg();
95     }
96   }
97   return 0;
98 }