ba318023c243cc6a802b42d9a82edaf401fa0643
[oota-llvm.git] / lib / Target / SystemZ / SystemZRegisterInfo.cpp
1 //===-- SystemZRegisterInfo.cpp - SystemZ register information ------------===//
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 #include "SystemZInstrInfo.h"
11 #include "SystemZRegisterInfo.h"
12 #include "SystemZSubtarget.h"
13 #include "llvm/CodeGen/MachineInstrBuilder.h"
14 #include "llvm/CodeGen/MachineRegisterInfo.h"
15 #include "llvm/Target/TargetFrameLowering.h"
16
17 using namespace llvm;
18
19 #define GET_REGINFO_TARGET_DESC
20 #include "SystemZGenRegisterInfo.inc"
21
22 SystemZRegisterInfo::SystemZRegisterInfo()
23     : SystemZGenRegisterInfo(SystemZ::R14D) {}
24
25 const MCPhysReg *
26 SystemZRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
27   return CSR_SystemZ_SaveList;
28 }
29
30 const uint32_t *
31 SystemZRegisterInfo::getCallPreservedMask(CallingConv::ID CC) const {
32   return CSR_SystemZ_RegMask;
33 }
34
35 BitVector
36 SystemZRegisterInfo::getReservedRegs(const MachineFunction &MF) const {
37   BitVector Reserved(getNumRegs());
38   const TargetFrameLowering *TFI =
39       MF.getTarget().getSubtargetImpl()->getFrameLowering();
40
41   if (TFI->hasFP(MF)) {
42     // R11D is the frame pointer.  Reserve all aliases.
43     Reserved.set(SystemZ::R11D);
44     Reserved.set(SystemZ::R11L);
45     Reserved.set(SystemZ::R11H);
46     Reserved.set(SystemZ::R10Q);
47   }
48
49   // R15D is the stack pointer.  Reserve all aliases.
50   Reserved.set(SystemZ::R15D);
51   Reserved.set(SystemZ::R15L);
52   Reserved.set(SystemZ::R15H);
53   Reserved.set(SystemZ::R14Q);
54   return Reserved;
55 }
56
57 void
58 SystemZRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator MI,
59                                          int SPAdj, unsigned FIOperandNum,
60                                          RegScavenger *RS) const {
61   assert(SPAdj == 0 && "Outgoing arguments should be part of the frame");
62
63   MachineBasicBlock &MBB = *MI->getParent();
64   MachineFunction &MF = *MBB.getParent();
65   auto *TII = static_cast<const SystemZInstrInfo *>(
66       MF.getTarget().getSubtargetImpl()->getInstrInfo());
67   const TargetFrameLowering *TFI =
68       MF.getTarget().getSubtargetImpl()->getFrameLowering();
69   DebugLoc DL = MI->getDebugLoc();
70
71   // Decompose the frame index into a base and offset.
72   int FrameIndex = MI->getOperand(FIOperandNum).getIndex();
73   unsigned BasePtr = getFrameRegister(MF);
74   int64_t Offset = (TFI->getFrameIndexOffset(MF, FrameIndex) +
75                     MI->getOperand(FIOperandNum + 1).getImm());
76
77   // Special handling of dbg_value instructions.
78   if (MI->isDebugValue()) {
79     MI->getOperand(FIOperandNum).ChangeToRegister(BasePtr, /*isDef*/ false);
80     MI->getOperand(FIOperandNum + 1).ChangeToImmediate(Offset);
81     return;
82   }
83
84   // See if the offset is in range, or if an equivalent instruction that
85   // accepts the offset exists.
86   unsigned Opcode = MI->getOpcode();
87   unsigned OpcodeForOffset = TII->getOpcodeForOffset(Opcode, Offset);
88   if (OpcodeForOffset)
89     MI->getOperand(FIOperandNum).ChangeToRegister(BasePtr, false);
90   else {
91     // Create an anchor point that is in range.  Start at 0xffff so that
92     // can use LLILH to load the immediate.
93     int64_t OldOffset = Offset;
94     int64_t Mask = 0xffff;
95     do {
96       Offset = OldOffset & Mask;
97       OpcodeForOffset = TII->getOpcodeForOffset(Opcode, Offset);
98       Mask >>= 1;
99       assert(Mask && "One offset must be OK");
100     } while (!OpcodeForOffset);
101
102     unsigned ScratchReg =
103       MF.getRegInfo().createVirtualRegister(&SystemZ::ADDR64BitRegClass);
104     int64_t HighOffset = OldOffset - Offset;
105
106     if (MI->getDesc().TSFlags & SystemZII::HasIndex
107         && MI->getOperand(FIOperandNum + 2).getReg() == 0) {
108       // Load the offset into the scratch register and use it as an index.
109       // The scratch register then dies here.
110       TII->loadImmediate(MBB, MI, ScratchReg, HighOffset);
111       MI->getOperand(FIOperandNum).ChangeToRegister(BasePtr, false);
112       MI->getOperand(FIOperandNum + 2).ChangeToRegister(ScratchReg,
113                                                         false, false, true);
114     } else {
115       // Load the anchor address into a scratch register.
116       unsigned LAOpcode = TII->getOpcodeForOffset(SystemZ::LA, HighOffset);
117       if (LAOpcode)
118         BuildMI(MBB, MI, DL, TII->get(LAOpcode),ScratchReg)
119           .addReg(BasePtr).addImm(HighOffset).addReg(0);
120       else {
121         // Load the high offset into the scratch register and use it as
122         // an index.
123         TII->loadImmediate(MBB, MI, ScratchReg, HighOffset);
124         BuildMI(MBB, MI, DL, TII->get(SystemZ::AGR),ScratchReg)
125           .addReg(ScratchReg, RegState::Kill).addReg(BasePtr);
126       }
127
128       // Use the scratch register as the base.  It then dies here.
129       MI->getOperand(FIOperandNum).ChangeToRegister(ScratchReg,
130                                                     false, false, true);
131     }
132   }
133   MI->setDesc(TII->get(OpcodeForOffset));
134   MI->getOperand(FIOperandNum + 1).ChangeToImmediate(Offset);
135 }
136
137 unsigned
138 SystemZRegisterInfo::getFrameRegister(const MachineFunction &MF) const {
139   const TargetFrameLowering *TFI =
140       MF.getTarget().getSubtargetImpl()->getFrameLowering();
141   return TFI->hasFP(MF) ? SystemZ::R11D : SystemZ::R15D;
142 }