1 //===-- SystemZRegisterInfo.cpp - SystemZ register information ------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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"
19 #define GET_REGINFO_TARGET_DESC
20 #include "SystemZGenRegisterInfo.inc"
22 SystemZRegisterInfo::SystemZRegisterInfo()
23 : SystemZGenRegisterInfo(SystemZ::R14D) {}
26 SystemZRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
27 return CSR_SystemZ_SaveList;
31 SystemZRegisterInfo::getCallPreservedMask(CallingConv::ID CC) const {
32 return CSR_SystemZ_RegMask;
36 SystemZRegisterInfo::getReservedRegs(const MachineFunction &MF) const {
37 BitVector Reserved(getNumRegs());
38 const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
41 // R11D is the frame pointer. Reserve all aliases.
42 Reserved.set(SystemZ::R11D);
43 Reserved.set(SystemZ::R11L);
44 Reserved.set(SystemZ::R11H);
45 Reserved.set(SystemZ::R10Q);
48 // R15D is the stack pointer. Reserve all aliases.
49 Reserved.set(SystemZ::R15D);
50 Reserved.set(SystemZ::R15L);
51 Reserved.set(SystemZ::R15H);
52 Reserved.set(SystemZ::R14Q);
57 SystemZRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator MI,
58 int SPAdj, unsigned FIOperandNum,
59 RegScavenger *RS) const {
60 assert(SPAdj == 0 && "Outgoing arguments should be part of the frame");
62 MachineBasicBlock &MBB = *MI->getParent();
63 MachineFunction &MF = *MBB.getParent();
65 static_cast<const SystemZInstrInfo *>(MF.getSubtarget().getInstrInfo());
66 const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
67 DebugLoc DL = MI->getDebugLoc();
69 // Decompose the frame index into a base and offset.
70 int FrameIndex = MI->getOperand(FIOperandNum).getIndex();
71 unsigned BasePtr = getFrameRegister(MF);
72 int64_t Offset = (TFI->getFrameIndexOffset(MF, FrameIndex) +
73 MI->getOperand(FIOperandNum + 1).getImm());
75 // Special handling of dbg_value instructions.
76 if (MI->isDebugValue()) {
77 MI->getOperand(FIOperandNum).ChangeToRegister(BasePtr, /*isDef*/ false);
78 MI->getOperand(FIOperandNum + 1).ChangeToImmediate(Offset);
82 // See if the offset is in range, or if an equivalent instruction that
83 // accepts the offset exists.
84 unsigned Opcode = MI->getOpcode();
85 unsigned OpcodeForOffset = TII->getOpcodeForOffset(Opcode, Offset);
87 MI->getOperand(FIOperandNum).ChangeToRegister(BasePtr, false);
89 // Create an anchor point that is in range. Start at 0xffff so that
90 // can use LLILH to load the immediate.
91 int64_t OldOffset = Offset;
92 int64_t Mask = 0xffff;
94 Offset = OldOffset & Mask;
95 OpcodeForOffset = TII->getOpcodeForOffset(Opcode, Offset);
97 assert(Mask && "One offset must be OK");
98 } while (!OpcodeForOffset);
100 unsigned ScratchReg =
101 MF.getRegInfo().createVirtualRegister(&SystemZ::ADDR64BitRegClass);
102 int64_t HighOffset = OldOffset - Offset;
104 if (MI->getDesc().TSFlags & SystemZII::HasIndex
105 && MI->getOperand(FIOperandNum + 2).getReg() == 0) {
106 // Load the offset into the scratch register and use it as an index.
107 // The scratch register then dies here.
108 TII->loadImmediate(MBB, MI, ScratchReg, HighOffset);
109 MI->getOperand(FIOperandNum).ChangeToRegister(BasePtr, false);
110 MI->getOperand(FIOperandNum + 2).ChangeToRegister(ScratchReg,
113 // Load the anchor address into a scratch register.
114 unsigned LAOpcode = TII->getOpcodeForOffset(SystemZ::LA, HighOffset);
116 BuildMI(MBB, MI, DL, TII->get(LAOpcode),ScratchReg)
117 .addReg(BasePtr).addImm(HighOffset).addReg(0);
119 // Load the high offset into the scratch register and use it as
121 TII->loadImmediate(MBB, MI, ScratchReg, HighOffset);
122 BuildMI(MBB, MI, DL, TII->get(SystemZ::AGR),ScratchReg)
123 .addReg(ScratchReg, RegState::Kill).addReg(BasePtr);
126 // Use the scratch register as the base. It then dies here.
127 MI->getOperand(FIOperandNum).ChangeToRegister(ScratchReg,
131 MI->setDesc(TII->get(OpcodeForOffset));
132 MI->getOperand(FIOperandNum + 1).ChangeToImmediate(Offset);
136 SystemZRegisterInfo::getFrameRegister(const MachineFunction &MF) const {
137 const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
138 return TFI->hasFP(MF) ? SystemZ::R11D : SystemZ::R15D;