blob: 5a64a4428c680721eabd1c4ae4e3540db2d06782 [file] [log] [blame]
Chris Lattner7c90f732006-02-05 05:50:24 +00001//===- SparcInstrInfo.cpp - Sparc Instruction Information -------*- C++ -*-===//
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002//
Brian Gaekee785e532004-02-25 19:28:19 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00007//
Brian Gaekee785e532004-02-25 19:28:19 +00008//===----------------------------------------------------------------------===//
9//
Chris Lattner7c90f732006-02-05 05:50:24 +000010// This file contains the Sparc implementation of the TargetInstrInfo class.
Brian Gaekee785e532004-02-25 19:28:19 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner7c90f732006-02-05 05:50:24 +000014#include "SparcInstrInfo.h"
Owen Andersond10fd972007-12-31 06:32:00 +000015#include "SparcSubtarget.h"
Chris Lattner7c90f732006-02-05 05:50:24 +000016#include "Sparc.h"
Owen Anderson718cb662007-09-07 04:06:50 +000017#include "llvm/ADT/STLExtras.h"
Brian Gaekee785e532004-02-25 19:28:19 +000018#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner7c90f732006-02-05 05:50:24 +000019#include "SparcGenInstrInfo.inc"
Chris Lattner1ddf4752004-02-29 05:59:33 +000020using namespace llvm;
Brian Gaekee785e532004-02-25 19:28:19 +000021
Chris Lattner7c90f732006-02-05 05:50:24 +000022SparcInstrInfo::SparcInstrInfo(SparcSubtarget &ST)
Owen Anderson718cb662007-09-07 04:06:50 +000023 : TargetInstrInfo(SparcInsts, array_lengthof(SparcInsts)),
Owen Andersond10fd972007-12-31 06:32:00 +000024 RI(ST, *this), Subtarget(ST) {
Brian Gaekee785e532004-02-25 19:28:19 +000025}
26
Chris Lattner69d39092006-02-04 06:58:46 +000027static bool isZeroImm(const MachineOperand &op) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +000028 return op.isImmediate() && op.getImm() == 0;
Brian Gaeke4658ba12004-12-11 05:19:03 +000029}
30
Chris Lattner1d6dc972004-07-25 06:19:04 +000031/// Return true if the instruction is a register to register move and
32/// leave the source and dest operands in the passed parameters.
33///
Chris Lattner7c90f732006-02-05 05:50:24 +000034bool SparcInstrInfo::isMoveInstr(const MachineInstr &MI,
35 unsigned &SrcReg, unsigned &DstReg) const {
Brian Gaeke4658ba12004-12-11 05:19:03 +000036 // We look for 3 kinds of patterns here:
37 // or with G0 or 0
38 // add with G0 or 0
39 // fmovs or FpMOVD (pseudo double move).
Chris Lattner7c90f732006-02-05 05:50:24 +000040 if (MI.getOpcode() == SP::ORrr || MI.getOpcode() == SP::ADDrr) {
41 if (MI.getOperand(1).getReg() == SP::G0) {
Chris Lattner1d6dc972004-07-25 06:19:04 +000042 DstReg = MI.getOperand(0).getReg();
43 SrcReg = MI.getOperand(2).getReg();
Brian Gaeke9b8ed0e2004-09-29 03:28:15 +000044 return true;
Chris Lattner7c90f732006-02-05 05:50:24 +000045 } else if (MI.getOperand(2).getReg() == SP::G0) {
Brian Gaeke4658ba12004-12-11 05:19:03 +000046 DstReg = MI.getOperand(0).getReg();
47 SrcReg = MI.getOperand(1).getReg();
48 return true;
49 }
Chris Lattner7c90f732006-02-05 05:50:24 +000050 } else if ((MI.getOpcode() == SP::ORri || MI.getOpcode() == SP::ADDri) &&
Chris Lattner69d39092006-02-04 06:58:46 +000051 isZeroImm(MI.getOperand(2)) && MI.getOperand(1).isRegister()) {
52 DstReg = MI.getOperand(0).getReg();
53 SrcReg = MI.getOperand(1).getReg();
54 return true;
Chris Lattner7c90f732006-02-05 05:50:24 +000055 } else if (MI.getOpcode() == SP::FMOVS || MI.getOpcode() == SP::FpMOVD ||
56 MI.getOpcode() == SP::FMOVD) {
Chris Lattner1d6dc972004-07-25 06:19:04 +000057 SrcReg = MI.getOperand(1).getReg();
58 DstReg = MI.getOperand(0).getReg();
59 return true;
60 }
61 return false;
62}
Chris Lattner5ccc7222006-02-03 06:44:54 +000063
64/// isLoadFromStackSlot - If the specified machine instruction is a direct
65/// load from a stack slot, return the virtual or physical register number of
66/// the destination along with the FrameIndex of the loaded stack slot. If
67/// not, return 0. This predicate must return 0 if the instruction has
68/// any side effects other than loading from the stack slot.
Chris Lattner7c90f732006-02-05 05:50:24 +000069unsigned SparcInstrInfo::isLoadFromStackSlot(MachineInstr *MI,
70 int &FrameIndex) const {
71 if (MI->getOpcode() == SP::LDri ||
72 MI->getOpcode() == SP::LDFri ||
73 MI->getOpcode() == SP::LDDFri) {
Chris Lattner5ccc7222006-02-03 06:44:54 +000074 if (MI->getOperand(1).isFrameIndex() && MI->getOperand(2).isImmediate() &&
Chris Lattner9a1ceae2007-12-30 20:49:49 +000075 MI->getOperand(2).getImm() == 0) {
Chris Lattner8aa797a2007-12-30 23:10:15 +000076 FrameIndex = MI->getOperand(1).getIndex();
Chris Lattner5ccc7222006-02-03 06:44:54 +000077 return MI->getOperand(0).getReg();
78 }
79 }
80 return 0;
81}
82
83/// isStoreToStackSlot - If the specified machine instruction is a direct
84/// store to a stack slot, return the virtual or physical register number of
85/// the source reg along with the FrameIndex of the loaded stack slot. If
86/// not, return 0. This predicate must return 0 if the instruction has
87/// any side effects other than storing to the stack slot.
Chris Lattner7c90f732006-02-05 05:50:24 +000088unsigned SparcInstrInfo::isStoreToStackSlot(MachineInstr *MI,
89 int &FrameIndex) const {
90 if (MI->getOpcode() == SP::STri ||
91 MI->getOpcode() == SP::STFri ||
92 MI->getOpcode() == SP::STDFri) {
Chris Lattner5ccc7222006-02-03 06:44:54 +000093 if (MI->getOperand(0).isFrameIndex() && MI->getOperand(1).isImmediate() &&
Chris Lattner9a1ceae2007-12-30 20:49:49 +000094 MI->getOperand(1).getImm() == 0) {
Chris Lattner8aa797a2007-12-30 23:10:15 +000095 FrameIndex = MI->getOperand(0).getIndex();
Chris Lattner5ccc7222006-02-03 06:44:54 +000096 return MI->getOperand(2).getReg();
97 }
98 }
99 return 0;
100}
Chris Lattnere87146a2006-10-24 16:39:19 +0000101
Evan Cheng6ae36262007-05-18 00:18:17 +0000102unsigned
103SparcInstrInfo::InsertBranch(MachineBasicBlock &MBB,MachineBasicBlock *TBB,
104 MachineBasicBlock *FBB,
105 const std::vector<MachineOperand> &Cond)const{
Chris Lattnere87146a2006-10-24 16:39:19 +0000106 // Can only insert uncond branches so far.
107 assert(Cond.empty() && !FBB && TBB && "Can only handle uncond branches!");
Evan Chengc0f64ff2006-11-27 23:37:22 +0000108 BuildMI(&MBB, get(SP::BA)).addMBB(TBB);
Evan Cheng6ae36262007-05-18 00:18:17 +0000109 return 1;
Rafael Espindola3d7d39a2006-10-24 17:07:11 +0000110}
Owen Andersond10fd972007-12-31 06:32:00 +0000111
112void SparcInstrInfo::copyRegToReg(MachineBasicBlock &MBB,
113 MachineBasicBlock::iterator I,
114 unsigned DestReg, unsigned SrcReg,
115 const TargetRegisterClass *DestRC,
116 const TargetRegisterClass *SrcRC) const {
117 if (DestRC != SrcRC) {
118 cerr << "Not yet supported!";
119 abort();
120 }
121
122 if (DestRC == SP::IntRegsRegisterClass)
123 BuildMI(MBB, I, get(SP::ORrr), DestReg).addReg(SP::G0).addReg(SrcReg);
124 else if (DestRC == SP::FPRegsRegisterClass)
125 BuildMI(MBB, I, get(SP::FMOVS), DestReg).addReg(SrcReg);
126 else if (DestRC == SP::DFPRegsRegisterClass)
127 BuildMI(MBB, I, get(Subtarget.isV9() ? SP::FMOVD : SP::FpMOVD),DestReg)
128 .addReg(SrcReg);
129 else
130 assert (0 && "Can't copy this register");
131}