Move some more hooks to TargetFrameInfo
[oota-llvm.git] / lib / Target / SystemZ / SystemZRegisterInfo.cpp
1 //===- SystemZRegisterInfo.cpp - SystemZ Register Information -------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the SystemZ implementation of the TargetRegisterInfo class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "SystemZ.h"
15 #include "SystemZInstrInfo.h"
16 #include "SystemZMachineFunctionInfo.h"
17 #include "SystemZRegisterInfo.h"
18 #include "SystemZSubtarget.h"
19 #include "llvm/CodeGen/MachineInstrBuilder.h"
20 #include "llvm/CodeGen/MachineFrameInfo.h"
21 #include "llvm/CodeGen/MachineFunction.h"
22 #include "llvm/CodeGen/MachineRegisterInfo.h"
23 #include "llvm/Target/TargetFrameInfo.h"
24 #include "llvm/Target/TargetInstrInfo.h"
25 #include "llvm/Target/TargetMachine.h"
26 #include "llvm/Target/TargetOptions.h"
27 #include "llvm/ADT/BitVector.h"
28 using namespace llvm;
29
30 SystemZRegisterInfo::SystemZRegisterInfo(SystemZTargetMachine &tm,
31                                          const SystemZInstrInfo &tii)
32   : SystemZGenRegisterInfo(SystemZ::ADJCALLSTACKUP, SystemZ::ADJCALLSTACKDOWN),
33     TM(tm), TII(tii) {
34 }
35
36 const unsigned*
37 SystemZRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
38   static const unsigned CalleeSavedRegs[] = {
39     SystemZ::R6D,  SystemZ::R7D,  SystemZ::R8D,  SystemZ::R9D,
40     SystemZ::R10D, SystemZ::R11D, SystemZ::R12D, SystemZ::R13D,
41     SystemZ::R14D, SystemZ::R15D,
42     SystemZ::F8L,  SystemZ::F9L,  SystemZ::F10L, SystemZ::F11L,
43     SystemZ::F12L, SystemZ::F13L, SystemZ::F14L, SystemZ::F15L,
44     0
45   };
46
47   return CalleeSavedRegs;
48 }
49
50 BitVector SystemZRegisterInfo::getReservedRegs(const MachineFunction &MF) const {
51   BitVector Reserved(getNumRegs());
52   const TargetFrameInfo *TFI = MF.getTarget().getFrameInfo();
53
54   if (TFI->hasFP(MF))
55     Reserved.set(SystemZ::R11D);
56   Reserved.set(SystemZ::R14D);
57   Reserved.set(SystemZ::R15D);
58   return Reserved;
59 }
60
61 void SystemZRegisterInfo::
62 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
63                               MachineBasicBlock::iterator I) const {
64   MBB.erase(I);
65 }
66
67 void
68 SystemZRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
69                                          int SPAdj, RegScavenger *RS) const {
70   assert(SPAdj == 0 && "Unxpected");
71
72   unsigned i = 0;
73   MachineInstr &MI = *II;
74   MachineFunction &MF = *MI.getParent()->getParent();
75   const TargetFrameInfo *TFI = MF.getTarget().getFrameInfo();
76
77   while (!MI.getOperand(i).isFI()) {
78     ++i;
79     assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
80   }
81
82   int FrameIndex = MI.getOperand(i).getIndex();
83
84   unsigned BasePtr = (TFI->hasFP(MF) ? SystemZ::R11D : SystemZ::R15D);
85
86   // This must be part of a rri or ri operand memory reference.  Replace the
87   // FrameIndex with base register with BasePtr.  Add an offset to the
88   // displacement field.
89   MI.getOperand(i).ChangeToRegister(BasePtr, false);
90
91   // Offset is a either 12-bit unsigned or 20-bit signed integer.
92   // FIXME: handle "too long" displacements.
93   int Offset =
94     TFI->getFrameIndexOffset(MF, FrameIndex) + MI.getOperand(i+1).getImm();
95
96   // Check whether displacement is too long to fit into 12 bit zext field.
97   MI.setDesc(TII.getMemoryInstr(MI.getOpcode(), Offset));
98
99   MI.getOperand(i+1).ChangeToImmediate(Offset);
100 }
101
102 void
103 SystemZRegisterInfo::processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
104                                                        RegScavenger *RS) const {
105   // Determine whether R15/R14 will ever be clobbered inside the function. And
106   // if yes - mark it as 'callee' saved.
107   MachineFrameInfo *FFI = MF.getFrameInfo();
108   MachineRegisterInfo &MRI = MF.getRegInfo();
109
110   // Check whether high FPRs are ever used, if yes - we need to save R15 as
111   // well.
112   static const unsigned HighFPRs[] = {
113     SystemZ::F8L,  SystemZ::F9L,  SystemZ::F10L, SystemZ::F11L,
114     SystemZ::F12L, SystemZ::F13L, SystemZ::F14L, SystemZ::F15L,
115     SystemZ::F8S,  SystemZ::F9S,  SystemZ::F10S, SystemZ::F11S,
116     SystemZ::F12S, SystemZ::F13S, SystemZ::F14S, SystemZ::F15S,
117   };
118
119   bool HighFPRsUsed = false;
120   for (unsigned i = 0, e = array_lengthof(HighFPRs); i != e; ++i)
121     HighFPRsUsed |= MRI.isPhysRegUsed(HighFPRs[i]);
122
123   if (FFI->hasCalls())
124     /* FIXME: function is varargs */
125     /* FIXME: function grabs RA */
126     /* FIXME: function calls eh_return */
127     MRI.setPhysRegUsed(SystemZ::R14D);
128
129   if (HighFPRsUsed ||
130       FFI->hasCalls() ||
131       FFI->getObjectIndexEnd() != 0 || // Contains automatic variables
132       FFI->hasVarSizedObjects() // Function calls dynamic alloca's
133       /* FIXME: function is varargs */)
134     MRI.setPhysRegUsed(SystemZ::R15D);
135 }
136
137 unsigned SystemZRegisterInfo::getRARegister() const {
138   assert(0 && "What is the return address register");
139   return 0;
140 }
141
142 unsigned
143 SystemZRegisterInfo::getFrameRegister(const MachineFunction &MF) const {
144   assert(0 && "What is the frame register");
145   return 0;
146 }
147
148 unsigned SystemZRegisterInfo::getEHExceptionRegister() const {
149   assert(0 && "What is the exception register");
150   return 0;
151 }
152
153 unsigned SystemZRegisterInfo::getEHHandlerRegister() const {
154   assert(0 && "What is the exception handler register");
155   return 0;
156 }
157
158 int SystemZRegisterInfo::getDwarfRegNum(unsigned RegNum, bool isEH) const {
159   assert(0 && "What is the dwarf register number");
160   return -1;
161 }
162
163 #include "SystemZGenRegisterInfo.inc"