1f4a2713aSLionel Sambuc //===-- MipsSERegisterInfo.cpp - MIPS32/64 Register Information -== -------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // This file contains the MIPS32/64 implementation of the TargetRegisterInfo
11f4a2713aSLionel Sambuc // class.
12f4a2713aSLionel Sambuc //
13f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
14f4a2713aSLionel Sambuc
15f4a2713aSLionel Sambuc #include "MipsSERegisterInfo.h"
16f4a2713aSLionel Sambuc #include "Mips.h"
17f4a2713aSLionel Sambuc #include "MipsAnalyzeImmediate.h"
18f4a2713aSLionel Sambuc #include "MipsMachineFunction.h"
19f4a2713aSLionel Sambuc #include "MipsSEInstrInfo.h"
20f4a2713aSLionel Sambuc #include "MipsSubtarget.h"
21f4a2713aSLionel Sambuc #include "llvm/ADT/BitVector.h"
22f4a2713aSLionel Sambuc #include "llvm/ADT/STLExtras.h"
23f4a2713aSLionel Sambuc #include "llvm/CodeGen/MachineFrameInfo.h"
24f4a2713aSLionel Sambuc #include "llvm/CodeGen/MachineFunction.h"
25f4a2713aSLionel Sambuc #include "llvm/CodeGen/MachineInstrBuilder.h"
26f4a2713aSLionel Sambuc #include "llvm/CodeGen/MachineRegisterInfo.h"
27f4a2713aSLionel Sambuc #include "llvm/IR/Constants.h"
28*0a6a1f1dSLionel Sambuc #include "llvm/IR/DebugInfo.h"
29f4a2713aSLionel Sambuc #include "llvm/IR/Function.h"
30f4a2713aSLionel Sambuc #include "llvm/IR/Type.h"
31f4a2713aSLionel Sambuc #include "llvm/Support/CommandLine.h"
32f4a2713aSLionel Sambuc #include "llvm/Support/Debug.h"
33f4a2713aSLionel Sambuc #include "llvm/Support/ErrorHandling.h"
34f4a2713aSLionel Sambuc #include "llvm/Support/raw_ostream.h"
35f4a2713aSLionel Sambuc #include "llvm/Target/TargetFrameLowering.h"
36f4a2713aSLionel Sambuc #include "llvm/Target/TargetInstrInfo.h"
37f4a2713aSLionel Sambuc #include "llvm/Target/TargetMachine.h"
38f4a2713aSLionel Sambuc #include "llvm/Target/TargetOptions.h"
39f4a2713aSLionel Sambuc
40f4a2713aSLionel Sambuc using namespace llvm;
41f4a2713aSLionel Sambuc
42*0a6a1f1dSLionel Sambuc #define DEBUG_TYPE "mips-reg-info"
43*0a6a1f1dSLionel Sambuc
MipsSERegisterInfo(const MipsSubtarget & ST)44f4a2713aSLionel Sambuc MipsSERegisterInfo::MipsSERegisterInfo(const MipsSubtarget &ST)
45f4a2713aSLionel Sambuc : MipsRegisterInfo(ST) {}
46f4a2713aSLionel Sambuc
47f4a2713aSLionel Sambuc bool MipsSERegisterInfo::
requiresRegisterScavenging(const MachineFunction & MF) const48f4a2713aSLionel Sambuc requiresRegisterScavenging(const MachineFunction &MF) const {
49f4a2713aSLionel Sambuc return true;
50f4a2713aSLionel Sambuc }
51f4a2713aSLionel Sambuc
52f4a2713aSLionel Sambuc bool MipsSERegisterInfo::
requiresFrameIndexScavenging(const MachineFunction & MF) const53f4a2713aSLionel Sambuc requiresFrameIndexScavenging(const MachineFunction &MF) const {
54f4a2713aSLionel Sambuc return true;
55f4a2713aSLionel Sambuc }
56f4a2713aSLionel Sambuc
57f4a2713aSLionel Sambuc const TargetRegisterClass *
intRegClass(unsigned Size) const58f4a2713aSLionel Sambuc MipsSERegisterInfo::intRegClass(unsigned Size) const {
59f4a2713aSLionel Sambuc if (Size == 4)
60f4a2713aSLionel Sambuc return &Mips::GPR32RegClass;
61f4a2713aSLionel Sambuc
62f4a2713aSLionel Sambuc assert(Size == 8);
63f4a2713aSLionel Sambuc return &Mips::GPR64RegClass;
64f4a2713aSLionel Sambuc }
65f4a2713aSLionel Sambuc
66*0a6a1f1dSLionel Sambuc /// Get the size of the offset supported by the given load/store.
67*0a6a1f1dSLionel Sambuc /// The result includes the effects of any scale factors applied to the
68*0a6a1f1dSLionel Sambuc /// instruction immediate.
getLoadStoreOffsetSizeInBits(const unsigned Opcode)69*0a6a1f1dSLionel Sambuc static inline unsigned getLoadStoreOffsetSizeInBits(const unsigned Opcode) {
70*0a6a1f1dSLionel Sambuc switch (Opcode) {
71*0a6a1f1dSLionel Sambuc case Mips::LD_B:
72*0a6a1f1dSLionel Sambuc case Mips::ST_B:
73*0a6a1f1dSLionel Sambuc return 10;
74*0a6a1f1dSLionel Sambuc case Mips::LD_H:
75*0a6a1f1dSLionel Sambuc case Mips::ST_H:
76*0a6a1f1dSLionel Sambuc return 10 + 1 /* scale factor */;
77*0a6a1f1dSLionel Sambuc case Mips::LD_W:
78*0a6a1f1dSLionel Sambuc case Mips::ST_W:
79*0a6a1f1dSLionel Sambuc return 10 + 2 /* scale factor */;
80*0a6a1f1dSLionel Sambuc case Mips::LD_D:
81*0a6a1f1dSLionel Sambuc case Mips::ST_D:
82*0a6a1f1dSLionel Sambuc return 10 + 3 /* scale factor */;
83*0a6a1f1dSLionel Sambuc default:
84*0a6a1f1dSLionel Sambuc return 16;
85*0a6a1f1dSLionel Sambuc }
86*0a6a1f1dSLionel Sambuc }
87*0a6a1f1dSLionel Sambuc
88*0a6a1f1dSLionel Sambuc /// Get the scale factor applied to the immediate in the given load/store.
getLoadStoreOffsetAlign(const unsigned Opcode)89*0a6a1f1dSLionel Sambuc static inline unsigned getLoadStoreOffsetAlign(const unsigned Opcode) {
90*0a6a1f1dSLionel Sambuc switch (Opcode) {
91*0a6a1f1dSLionel Sambuc case Mips::LD_H:
92*0a6a1f1dSLionel Sambuc case Mips::ST_H:
93*0a6a1f1dSLionel Sambuc return 2;
94*0a6a1f1dSLionel Sambuc case Mips::LD_W:
95*0a6a1f1dSLionel Sambuc case Mips::ST_W:
96*0a6a1f1dSLionel Sambuc return 4;
97*0a6a1f1dSLionel Sambuc case Mips::LD_D:
98*0a6a1f1dSLionel Sambuc case Mips::ST_D:
99*0a6a1f1dSLionel Sambuc return 8;
100*0a6a1f1dSLionel Sambuc default:
101*0a6a1f1dSLionel Sambuc return 1;
102*0a6a1f1dSLionel Sambuc }
103*0a6a1f1dSLionel Sambuc }
104*0a6a1f1dSLionel Sambuc
eliminateFI(MachineBasicBlock::iterator II,unsigned OpNo,int FrameIndex,uint64_t StackSize,int64_t SPOffset) const105f4a2713aSLionel Sambuc void MipsSERegisterInfo::eliminateFI(MachineBasicBlock::iterator II,
106f4a2713aSLionel Sambuc unsigned OpNo, int FrameIndex,
107f4a2713aSLionel Sambuc uint64_t StackSize,
108f4a2713aSLionel Sambuc int64_t SPOffset) const {
109f4a2713aSLionel Sambuc MachineInstr &MI = *II;
110f4a2713aSLionel Sambuc MachineFunction &MF = *MI.getParent()->getParent();
111f4a2713aSLionel Sambuc MachineFrameInfo *MFI = MF.getFrameInfo();
112f4a2713aSLionel Sambuc MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
113f4a2713aSLionel Sambuc
114f4a2713aSLionel Sambuc const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
115f4a2713aSLionel Sambuc int MinCSFI = 0;
116f4a2713aSLionel Sambuc int MaxCSFI = -1;
117f4a2713aSLionel Sambuc
118f4a2713aSLionel Sambuc if (CSI.size()) {
119f4a2713aSLionel Sambuc MinCSFI = CSI[0].getFrameIdx();
120f4a2713aSLionel Sambuc MaxCSFI = CSI[CSI.size() - 1].getFrameIdx();
121f4a2713aSLionel Sambuc }
122f4a2713aSLionel Sambuc
123f4a2713aSLionel Sambuc bool EhDataRegFI = MipsFI->isEhDataRegFI(FrameIndex);
124f4a2713aSLionel Sambuc
125f4a2713aSLionel Sambuc // The following stack frame objects are always referenced relative to $sp:
126f4a2713aSLionel Sambuc // 1. Outgoing arguments.
127f4a2713aSLionel Sambuc // 2. Pointer to dynamically allocated stack space.
128f4a2713aSLionel Sambuc // 3. Locations for callee-saved registers.
129f4a2713aSLionel Sambuc // 4. Locations for eh data registers.
130f4a2713aSLionel Sambuc // Everything else is referenced relative to whatever register
131f4a2713aSLionel Sambuc // getFrameRegister() returns.
132f4a2713aSLionel Sambuc unsigned FrameReg;
133f4a2713aSLionel Sambuc
134f4a2713aSLionel Sambuc if ((FrameIndex >= MinCSFI && FrameIndex <= MaxCSFI) || EhDataRegFI)
135f4a2713aSLionel Sambuc FrameReg = Subtarget.isABI_N64() ? Mips::SP_64 : Mips::SP;
136f4a2713aSLionel Sambuc else
137f4a2713aSLionel Sambuc FrameReg = getFrameRegister(MF);
138f4a2713aSLionel Sambuc
139f4a2713aSLionel Sambuc // Calculate final offset.
140f4a2713aSLionel Sambuc // - There is no need to change the offset if the frame object is one of the
141f4a2713aSLionel Sambuc // following: an outgoing argument, pointer to a dynamically allocated
142f4a2713aSLionel Sambuc // stack space or a $gp restore location,
143f4a2713aSLionel Sambuc // - If the frame object is any of the following, its offset must be adjusted
144f4a2713aSLionel Sambuc // by adding the size of the stack:
145f4a2713aSLionel Sambuc // incoming argument, callee-saved register location or local variable.
146f4a2713aSLionel Sambuc bool IsKill = false;
147f4a2713aSLionel Sambuc int64_t Offset;
148f4a2713aSLionel Sambuc
149f4a2713aSLionel Sambuc Offset = SPOffset + (int64_t)StackSize;
150f4a2713aSLionel Sambuc Offset += MI.getOperand(OpNo + 1).getImm();
151f4a2713aSLionel Sambuc
152f4a2713aSLionel Sambuc DEBUG(errs() << "Offset : " << Offset << "\n" << "<--------->\n");
153f4a2713aSLionel Sambuc
154*0a6a1f1dSLionel Sambuc if (!MI.isDebugValue()) {
155*0a6a1f1dSLionel Sambuc // Make sure Offset fits within the field available.
156*0a6a1f1dSLionel Sambuc // For MSA instructions, this is a 10-bit signed immediate (scaled by
157*0a6a1f1dSLionel Sambuc // element size), otherwise it is a 16-bit signed immediate.
158*0a6a1f1dSLionel Sambuc unsigned OffsetBitSize = getLoadStoreOffsetSizeInBits(MI.getOpcode());
159*0a6a1f1dSLionel Sambuc unsigned OffsetAlign = getLoadStoreOffsetAlign(MI.getOpcode());
160*0a6a1f1dSLionel Sambuc
161*0a6a1f1dSLionel Sambuc if (OffsetBitSize < 16 && isInt<16>(Offset) &&
162*0a6a1f1dSLionel Sambuc (!isIntN(OffsetBitSize, Offset) ||
163*0a6a1f1dSLionel Sambuc OffsetToAlignment(Offset, OffsetAlign) != 0)) {
164*0a6a1f1dSLionel Sambuc // If we have an offset that needs to fit into a signed n-bit immediate
165*0a6a1f1dSLionel Sambuc // (where n < 16) and doesn't, but does fit into 16-bits then use an ADDiu
166*0a6a1f1dSLionel Sambuc MachineBasicBlock &MBB = *MI.getParent();
167*0a6a1f1dSLionel Sambuc DebugLoc DL = II->getDebugLoc();
168*0a6a1f1dSLionel Sambuc unsigned ADDiu = Subtarget.isABI_N64() ? Mips::DADDiu : Mips::ADDiu;
169*0a6a1f1dSLionel Sambuc const TargetRegisterClass *RC =
170*0a6a1f1dSLionel Sambuc Subtarget.isABI_N64() ? &Mips::GPR64RegClass : &Mips::GPR32RegClass;
171*0a6a1f1dSLionel Sambuc MachineRegisterInfo &RegInfo = MBB.getParent()->getRegInfo();
172*0a6a1f1dSLionel Sambuc unsigned Reg = RegInfo.createVirtualRegister(RC);
173*0a6a1f1dSLionel Sambuc const MipsSEInstrInfo &TII =
174*0a6a1f1dSLionel Sambuc *static_cast<const MipsSEInstrInfo *>(
175*0a6a1f1dSLionel Sambuc MBB.getParent()->getSubtarget().getInstrInfo());
176*0a6a1f1dSLionel Sambuc BuildMI(MBB, II, DL, TII.get(ADDiu), Reg).addReg(FrameReg).addImm(Offset);
177*0a6a1f1dSLionel Sambuc
178*0a6a1f1dSLionel Sambuc FrameReg = Reg;
179*0a6a1f1dSLionel Sambuc Offset = 0;
180*0a6a1f1dSLionel Sambuc IsKill = true;
181*0a6a1f1dSLionel Sambuc } else if (!isInt<16>(Offset)) {
182*0a6a1f1dSLionel Sambuc // Otherwise split the offset into 16-bit pieces and add it in multiple
183*0a6a1f1dSLionel Sambuc // instructions.
184f4a2713aSLionel Sambuc MachineBasicBlock &MBB = *MI.getParent();
185f4a2713aSLionel Sambuc DebugLoc DL = II->getDebugLoc();
186f4a2713aSLionel Sambuc unsigned ADDu = Subtarget.isABI_N64() ? Mips::DADDu : Mips::ADDu;
187*0a6a1f1dSLionel Sambuc unsigned NewImm = 0;
188f4a2713aSLionel Sambuc const MipsSEInstrInfo &TII =
189f4a2713aSLionel Sambuc *static_cast<const MipsSEInstrInfo *>(
190*0a6a1f1dSLionel Sambuc MBB.getParent()->getSubtarget().getInstrInfo());
191*0a6a1f1dSLionel Sambuc unsigned Reg = TII.loadImmediate(Offset, MBB, II, DL,
192*0a6a1f1dSLionel Sambuc OffsetBitSize == 16 ? &NewImm : nullptr);
193f4a2713aSLionel Sambuc BuildMI(MBB, II, DL, TII.get(ADDu), Reg).addReg(FrameReg)
194f4a2713aSLionel Sambuc .addReg(Reg, RegState::Kill);
195f4a2713aSLionel Sambuc
196f4a2713aSLionel Sambuc FrameReg = Reg;
197f4a2713aSLionel Sambuc Offset = SignExtend64<16>(NewImm);
198f4a2713aSLionel Sambuc IsKill = true;
199f4a2713aSLionel Sambuc }
200*0a6a1f1dSLionel Sambuc }
201f4a2713aSLionel Sambuc
202f4a2713aSLionel Sambuc MI.getOperand(OpNo).ChangeToRegister(FrameReg, false, false, IsKill);
203f4a2713aSLionel Sambuc MI.getOperand(OpNo + 1).ChangeToImmediate(Offset);
204f4a2713aSLionel Sambuc }
205