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(const MachineFunction &MF,
32                                           CallingConv::ID CC) const {
33   return CSR_SystemZ_RegMask;
34 }
35 
36 BitVector
37 SystemZRegisterInfo::getReservedRegs(const MachineFunction &MF) const {
38   BitVector Reserved(getNumRegs());
39   const SystemZFrameLowering *TFI = getFrameLowering(MF);
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 =
66       static_cast<const SystemZInstrInfo *>(MF.getSubtarget().getInstrInfo());
67   const SystemZFrameLowering *TFI = getFrameLowering(MF);
68   DebugLoc DL = MI->getDebugLoc();
69 
70   // Decompose the frame index into a base and offset.
71   int FrameIndex = MI->getOperand(FIOperandNum).getIndex();
72   unsigned BasePtr;
73   int64_t Offset = (TFI->getFrameIndexReference(MF, FrameIndex, BasePtr) +
74                     MI->getOperand(FIOperandNum + 1).getImm());
75 
76   // Special handling of dbg_value instructions.
77   if (MI->isDebugValue()) {
78     MI->getOperand(FIOperandNum).ChangeToRegister(BasePtr, /*isDef*/ false);
79     MI->getOperand(FIOperandNum + 1).ChangeToImmediate(Offset);
80     return;
81   }
82 
83   // See if the offset is in range, or if an equivalent instruction that
84   // accepts the offset exists.
85   unsigned Opcode = MI->getOpcode();
86   unsigned OpcodeForOffset = TII->getOpcodeForOffset(Opcode, Offset);
87   if (OpcodeForOffset) {
88     if (OpcodeForOffset == SystemZ::LE &&
89         MF.getSubtarget<SystemZSubtarget>().hasVector()) {
90       // If LE is ok for offset, use LDE instead on z13.
91       OpcodeForOffset = SystemZ::LDE32;
92     }
93     MI->getOperand(FIOperandNum).ChangeToRegister(BasePtr, false);
94   }
95   else {
96     // Create an anchor point that is in range.  Start at 0xffff so that
97     // can use LLILH to load the immediate.
98     int64_t OldOffset = Offset;
99     int64_t Mask = 0xffff;
100     do {
101       Offset = OldOffset & Mask;
102       OpcodeForOffset = TII->getOpcodeForOffset(Opcode, Offset);
103       Mask >>= 1;
104       assert(Mask && "One offset must be OK");
105     } while (!OpcodeForOffset);
106 
107     unsigned ScratchReg =
108       MF.getRegInfo().createVirtualRegister(&SystemZ::ADDR64BitRegClass);
109     int64_t HighOffset = OldOffset - Offset;
110 
111     if (MI->getDesc().TSFlags & SystemZII::HasIndex
112         && MI->getOperand(FIOperandNum + 2).getReg() == 0) {
113       // Load the offset into the scratch register and use it as an index.
114       // The scratch register then dies here.
115       TII->loadImmediate(MBB, MI, ScratchReg, HighOffset);
116       MI->getOperand(FIOperandNum).ChangeToRegister(BasePtr, false);
117       MI->getOperand(FIOperandNum + 2).ChangeToRegister(ScratchReg,
118                                                         false, false, true);
119     } else {
120       // Load the anchor address into a scratch register.
121       unsigned LAOpcode = TII->getOpcodeForOffset(SystemZ::LA, HighOffset);
122       if (LAOpcode)
123         BuildMI(MBB, MI, DL, TII->get(LAOpcode),ScratchReg)
124           .addReg(BasePtr).addImm(HighOffset).addReg(0);
125       else {
126         // Load the high offset into the scratch register and use it as
127         // an index.
128         TII->loadImmediate(MBB, MI, ScratchReg, HighOffset);
129         BuildMI(MBB, MI, DL, TII->get(SystemZ::AGR),ScratchReg)
130           .addReg(ScratchReg, RegState::Kill).addReg(BasePtr);
131       }
132 
133       // Use the scratch register as the base.  It then dies here.
134       MI->getOperand(FIOperandNum).ChangeToRegister(ScratchReg,
135                                                     false, false, true);
136     }
137   }
138   MI->setDesc(TII->get(OpcodeForOffset));
139   MI->getOperand(FIOperandNum + 1).ChangeToImmediate(Offset);
140 }
141 
142 unsigned
143 SystemZRegisterInfo::getFrameRegister(const MachineFunction &MF) const {
144   const SystemZFrameLowering *TFI = getFrameLowering(MF);
145   return TFI->hasFP(MF) ? SystemZ::R11D : SystemZ::R15D;
146 }
147