1 //===- ARCRegisterInfo.cpp - ARC Register Information -----------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file contains the ARC implementation of the MRegisterInfo class.
11 //===----------------------------------------------------------------------===//
13 #include "ARCRegisterInfo.h"
15 #include "ARCInstrInfo.h"
16 #include "ARCMachineFunctionInfo.h"
17 #include "ARCSubtarget.h"
18 #include "llvm/ADT/BitVector.h"
19 #include "llvm/CodeGen/MachineFrameInfo.h"
20 #include "llvm/CodeGen/MachineFunction.h"
21 #include "llvm/CodeGen/MachineInstrBuilder.h"
22 #include "llvm/CodeGen/MachineModuleInfo.h"
23 #include "llvm/CodeGen/MachineRegisterInfo.h"
24 #include "llvm/CodeGen/RegisterScavenging.h"
25 #include "llvm/IR/Function.h"
26 #include "llvm/Support/Debug.h"
27 #include "llvm/CodeGen/TargetFrameLowering.h"
28 #include "llvm/Target/TargetMachine.h"
29 #include "llvm/Target/TargetOptions.h"
33 #define DEBUG_TYPE "arc-reg-info"
35 #define GET_REGINFO_TARGET_DESC
36 #include "ARCGenRegisterInfo.inc"
38 static void ReplaceFrameIndex(MachineBasicBlock::iterator II
,
39 const ARCInstrInfo
&TII
, unsigned Reg
,
40 unsigned FrameReg
, int Offset
, int StackSize
,
41 int ObjSize
, RegScavenger
*RS
, int SPAdj
) {
42 assert(RS
&& "Need register scavenger.");
43 MachineInstr
&MI
= *II
;
44 MachineBasicBlock
&MBB
= *MI
.getParent();
45 DebugLoc dl
= MI
.getDebugLoc();
46 unsigned BaseReg
= FrameReg
;
47 unsigned KillState
= 0;
48 if (MI
.getOpcode() == ARC::LD_rs9
&& (Offset
>= 256 || Offset
< -256)) {
49 // Loads can always be reached with LD_rlimm.
50 BuildMI(MBB
, II
, dl
, TII
.get(ARC::LD_rlimm
), Reg
)
53 .addMemOperand(*MI
.memoperands_begin());
58 if (MI
.getOpcode() != ARC::GETFI
&& (Offset
>= 256 || Offset
< -256)) {
59 // We need to use a scratch register to reach the far-away frame indexes.
60 BaseReg
= RS
->FindUnusedReg(&ARC::GPR32RegClass
);
62 // We can be sure that the scavenged-register slot is within the range
63 // of the load offset.
64 const TargetRegisterInfo
*TRI
=
65 MBB
.getParent()->getSubtarget().getRegisterInfo();
66 BaseReg
= RS
->scavengeRegister(&ARC::GPR32RegClass
, II
, SPAdj
);
67 assert(BaseReg
&& "Register scavenging failed.");
68 LLVM_DEBUG(dbgs() << "Scavenged register " << printReg(BaseReg
, TRI
)
69 << " for FrameReg=" << printReg(FrameReg
, TRI
)
70 << "+Offset=" << Offset
<< "\n");
72 RS
->setRegUsed(BaseReg
);
74 unsigned AddOpc
= isUInt
<6>(Offset
) ? ARC::ADD_rru6
: ARC::ADD_rrlimm
;
75 BuildMI(MBB
, II
, dl
, TII
.get(AddOpc
))
76 .addReg(BaseReg
, RegState::Define
)
80 KillState
= RegState::Kill
;
82 switch (MI
.getOpcode()) {
84 assert((Offset
% 4 == 0) && "LD needs 4 byte alignment.");
87 assert((Offset
% 2 == 0) && "LDH needs 2 byte alignment.");
90 LLVM_DEBUG(dbgs() << "Building LDFI\n");
91 BuildMI(MBB
, II
, dl
, TII
.get(MI
.getOpcode()), Reg
)
92 .addReg(BaseReg
, KillState
)
94 .addMemOperand(*MI
.memoperands_begin());
97 assert((Offset
% 4 == 0) && "ST needs 4 byte alignment.");
99 assert((Offset
% 2 == 0) && "STH needs 2 byte alignment.");
101 LLVM_DEBUG(dbgs() << "Building STFI\n");
102 BuildMI(MBB
, II
, dl
, TII
.get(MI
.getOpcode()))
103 .addReg(Reg
, getKillRegState(MI
.getOperand(0).isKill()))
104 .addReg(BaseReg
, KillState
)
106 .addMemOperand(*MI
.memoperands_begin());
109 LLVM_DEBUG(dbgs() << "Building GETFI\n");
111 TII
.get(isUInt
<6>(Offset
) ? ARC::ADD_rru6
: ARC::ADD_rrlimm
))
112 .addReg(Reg
, RegState::Define
)
117 llvm_unreachable("Unhandled opcode.");
120 // Erase old instruction.
124 ARCRegisterInfo::ARCRegisterInfo() : ARCGenRegisterInfo(ARC::BLINK
) {}
126 bool ARCRegisterInfo::needsFrameMoves(const MachineFunction
&MF
) {
127 return MF
.getMMI().hasDebugInfo() || MF
.getFunction().needsUnwindTableEntry();
131 ARCRegisterInfo::getCalleeSavedRegs(const MachineFunction
*MF
) const {
132 return CSR_ARC_SaveList
;
135 BitVector
ARCRegisterInfo::getReservedRegs(const MachineFunction
&MF
) const {
136 BitVector
Reserved(getNumRegs());
138 Reserved
.set(ARC::ILINK
);
139 Reserved
.set(ARC::SP
);
140 Reserved
.set(ARC::GP
);
141 Reserved
.set(ARC::R25
);
142 Reserved
.set(ARC::BLINK
);
143 Reserved
.set(ARC::FP
);
147 bool ARCRegisterInfo::requiresRegisterScavenging(
148 const MachineFunction
&MF
) const {
152 bool ARCRegisterInfo::trackLivenessAfterRegAlloc(
153 const MachineFunction
&MF
) const {
157 bool ARCRegisterInfo::useFPForScavengingIndex(const MachineFunction
&MF
) const {
161 void ARCRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II
,
162 int SPAdj
, unsigned FIOperandNum
,
163 RegScavenger
*RS
) const {
164 assert(SPAdj
== 0 && "Unexpected");
165 MachineInstr
&MI
= *II
;
166 MachineOperand
&FrameOp
= MI
.getOperand(FIOperandNum
);
167 int FrameIndex
= FrameOp
.getIndex();
169 MachineFunction
&MF
= *MI
.getParent()->getParent();
170 const ARCInstrInfo
&TII
= *MF
.getSubtarget
<ARCSubtarget
>().getInstrInfo();
171 const ARCFrameLowering
*TFI
= getFrameLowering(MF
);
172 int Offset
= MF
.getFrameInfo().getObjectOffset(FrameIndex
);
173 int ObjSize
= MF
.getFrameInfo().getObjectSize(FrameIndex
);
174 int StackSize
= MF
.getFrameInfo().getStackSize();
175 int LocalFrameSize
= MF
.getFrameInfo().getLocalFrameSize();
177 LLVM_DEBUG(dbgs() << "\nFunction : " << MF
.getName() << "\n");
178 LLVM_DEBUG(dbgs() << "<--------->\n");
179 LLVM_DEBUG(dbgs() << MI
<< "\n");
180 LLVM_DEBUG(dbgs() << "FrameIndex : " << FrameIndex
<< "\n");
181 LLVM_DEBUG(dbgs() << "ObjSize : " << ObjSize
<< "\n");
182 LLVM_DEBUG(dbgs() << "FrameOffset : " << Offset
<< "\n");
183 LLVM_DEBUG(dbgs() << "StackSize : " << StackSize
<< "\n");
184 LLVM_DEBUG(dbgs() << "LocalFrameSize : " << LocalFrameSize
<< "\n");
185 (void)LocalFrameSize
;
187 // Special handling of DBG_VALUE instructions.
188 if (MI
.isDebugValue()) {
189 unsigned FrameReg
= getFrameRegister(MF
);
190 MI
.getOperand(FIOperandNum
).ChangeToRegister(FrameReg
, false /*isDef*/);
191 MI
.getOperand(FIOperandNum
+ 1).ChangeToImmediate(Offset
);
195 // fold constant into offset.
196 Offset
+= MI
.getOperand(FIOperandNum
+ 1).getImm();
198 // TODO: assert based on the load type:
199 // ldb needs no alignment,
200 // ldh needs 2 byte alignment
201 // ld needs 4 byte alignment
202 LLVM_DEBUG(dbgs() << "Offset : " << Offset
<< "\n"
205 unsigned Reg
= MI
.getOperand(0).getReg();
206 assert(ARC::GPR32RegClass
.contains(Reg
) && "Unexpected register operand");
208 if (!TFI
->hasFP(MF
)) {
209 Offset
= StackSize
+ Offset
;
211 assert((Offset
>= 0 && Offset
< StackSize
) && "SP Offset not in bounds.");
213 if (FrameIndex
>= 0) {
214 assert((Offset
< 0 && -Offset
<= StackSize
) &&
215 "FP Offset not in bounds.");
218 ReplaceFrameIndex(II
, TII
, Reg
, getFrameRegister(MF
), Offset
, StackSize
,
222 unsigned ARCRegisterInfo::getFrameRegister(const MachineFunction
&MF
) const {
223 const ARCFrameLowering
*TFI
= getFrameLowering(MF
);
224 return TFI
->hasFP(MF
) ? ARC::FP
: ARC::SP
;
228 ARCRegisterInfo::getCallPreservedMask(const MachineFunction
&MF
,
229 CallingConv::ID CC
) const {
230 return CSR_ARC_RegMask
;