[Hexagon] Adding skeleton of HVX extension instructions.
[oota-llvm.git] / lib / Target / Hexagon / HexagonRegisterInfo.cpp
1 //===-- HexagonRegisterInfo.cpp - Hexagon 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 // This file contains the Hexagon implementation of the TargetRegisterInfo
11 // class.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "HexagonRegisterInfo.h"
16 #include "Hexagon.h"
17 #include "HexagonMachineFunctionInfo.h"
18 #include "HexagonSubtarget.h"
19 #include "HexagonTargetMachine.h"
20 #include "llvm/ADT/BitVector.h"
21 #include "llvm/ADT/STLExtras.h"
22 #include "llvm/CodeGen/MachineFrameInfo.h"
23 #include "llvm/CodeGen/MachineFunction.h"
24 #include "llvm/CodeGen/MachineFunctionPass.h"
25 #include "llvm/CodeGen/MachineInstrBuilder.h"
26 #include "llvm/CodeGen/MachineRegisterInfo.h"
27 #include "llvm/CodeGen/PseudoSourceValue.h"
28 #include "llvm/CodeGen/RegisterScavenging.h"
29 #include "llvm/IR/Function.h"
30 #include "llvm/IR/Type.h"
31 #include "llvm/MC/MachineLocation.h"
32 #include "llvm/Support/CommandLine.h"
33 #include "llvm/Support/Debug.h"
34 #include "llvm/Support/ErrorHandling.h"
35 #include "llvm/Support/raw_ostream.h"
36 #include "llvm/Target/TargetInstrInfo.h"
37 #include "llvm/Target/TargetMachine.h"
38 #include "llvm/Target/TargetOptions.h"
39
40 using namespace llvm;
41
42 HexagonRegisterInfo::HexagonRegisterInfo()
43     : HexagonGenRegisterInfo(Hexagon::R31) {}
44
45
46 bool HexagonRegisterInfo::isEHReturnCalleeSaveReg(unsigned R) const {
47   return R == Hexagon::R0 || R == Hexagon::R1 || R == Hexagon::R2 ||
48          R == Hexagon::R3 || R == Hexagon::D0 || R == Hexagon::D1;
49 }
50
51 bool HexagonRegisterInfo::isCalleeSaveReg(unsigned Reg) const {
52   return Hexagon::R16 <= Reg && Reg <= Hexagon::R27;
53 }
54
55
56 const MCPhysReg *
57 HexagonRegisterInfo::getCallerSavedRegs(const MachineFunction *MF) const {
58   static const MCPhysReg CallerSavedRegsV4[] = {
59     Hexagon::R0, Hexagon::R1, Hexagon::R2, Hexagon::R3, Hexagon::R4,
60     Hexagon::R5, Hexagon::R6, Hexagon::R7, Hexagon::R8, Hexagon::R9,
61     Hexagon::R10, Hexagon::R11, Hexagon::R12, Hexagon::R13, Hexagon::R14,
62     Hexagon::R15, 0
63   };
64
65   auto &HST = static_cast<const HexagonSubtarget&>(MF->getSubtarget());
66   switch (HST.getHexagonArchVersion()) {
67   case HexagonSubtarget::V4:
68   case HexagonSubtarget::V5:
69   case HexagonSubtarget::V55:
70   case HexagonSubtarget::V60:
71     return CallerSavedRegsV4;
72   }
73   llvm_unreachable(
74     "Callee saved registers requested for unknown archtecture version");
75 }
76
77
78 const MCPhysReg *
79 HexagonRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
80   static const MCPhysReg CalleeSavedRegsV3[] = {
81     Hexagon::R16,   Hexagon::R17,   Hexagon::R18,   Hexagon::R19,
82     Hexagon::R20,   Hexagon::R21,   Hexagon::R22,   Hexagon::R23,
83     Hexagon::R24,   Hexagon::R25,   Hexagon::R26,   Hexagon::R27, 0
84   };
85
86   switch (MF->getSubtarget<HexagonSubtarget>().getHexagonArchVersion()) {
87   case HexagonSubtarget::V4:
88   case HexagonSubtarget::V5:
89   case HexagonSubtarget::V55:
90   case HexagonSubtarget::V60:
91     return CalleeSavedRegsV3;
92   }
93   llvm_unreachable("Callee saved registers requested for unknown architecture "
94                    "version");
95 }
96
97 BitVector HexagonRegisterInfo::getReservedRegs(const MachineFunction &MF)
98   const {
99   BitVector Reserved(getNumRegs());
100   Reserved.set(HEXAGON_RESERVED_REG_1);
101   Reserved.set(HEXAGON_RESERVED_REG_2);
102   Reserved.set(Hexagon::R29);
103   Reserved.set(Hexagon::R30);
104   Reserved.set(Hexagon::R31);
105   Reserved.set(Hexagon::D14);
106   Reserved.set(Hexagon::D15);
107   Reserved.set(Hexagon::LC0);
108   Reserved.set(Hexagon::LC1);
109   Reserved.set(Hexagon::SA0);
110   Reserved.set(Hexagon::SA1);
111   return Reserved;
112 }
113
114
115 void HexagonRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
116                                               int SPAdj, unsigned FIOp,
117                                               RegScavenger *RS) const {
118   //
119   // Hexagon_TODO: Do we need to enforce this for Hexagon?
120   assert(SPAdj == 0 && "Unexpected");
121
122   MachineInstr &MI = *II;
123
124   MachineBasicBlock &MB = *MI.getParent();
125   MachineFunction &MF = *MB.getParent();
126   MachineFrameInfo &MFI = *MF.getFrameInfo();
127   auto &HST = static_cast<const HexagonSubtarget&>(MF.getSubtarget());
128   auto &HII = *HST.getInstrInfo();
129   auto &HFI = *HST.getFrameLowering();
130
131   int FI = MI.getOperand(FIOp).getIndex();
132   int Offset = MFI.getObjectOffset(FI) + MI.getOperand(FIOp+1).getImm();
133   bool HasAlloca = MFI.hasVarSizedObjects();
134   bool HasAlign = needsStackRealignment(MF);
135
136   // XXX: Fixed objects cannot be accessed through SP if there are aligned
137   // objects in the local frame, or if there are dynamically allocated objects.
138   // In such cases, there has to be FP available.
139   if (!HFI.hasFP(MF)) {
140     assert(!HasAlloca && !HasAlign && "This function must have frame pointer");
141     // We will not reserve space on the stack for the lr and fp registers.
142     Offset -= 8;
143   }
144
145   unsigned SP = getStackRegister(), FP = getFrameRegister();
146   unsigned AP = 0;
147   if (MachineInstr *AI = HFI.getAlignaInstr(MF))
148     AP = AI->getOperand(0).getReg();
149   unsigned FrameSize = MFI.getStackSize();
150
151   // Special handling of dbg_value instructions and INLINEASM.
152   if (MI.isDebugValue() || MI.isInlineAsm()) {
153     MI.getOperand(FIOp).ChangeToRegister(SP, false /*isDef*/);
154     MI.getOperand(FIOp+1).ChangeToImmediate(Offset+FrameSize);
155     return;
156   }
157
158   bool UseFP = false, UseAP = false;  // Default: use SP.
159   if (MFI.isFixedObjectIndex(FI) || MFI.isObjectPreAllocated(FI)) {
160     UseFP = HasAlloca || HasAlign;
161   } else {
162     if (HasAlloca) {
163       if (HasAlign)
164         UseAP = true;
165       else
166         UseFP = true;
167     }
168   }
169
170   unsigned Opc = MI.getOpcode();
171   bool ValidSP = HII.isValidOffset(Opc, FrameSize+Offset);
172   bool ValidFP = HII.isValidOffset(Opc, Offset);
173
174   // Calculate the actual offset in the instruction.
175   int64_t RealOffset = Offset;
176   if (!UseFP && !UseAP)
177     RealOffset = FrameSize+Offset;
178
179   switch (Opc) {
180     case Hexagon::TFR_FIA:
181       MI.setDesc(HII.get(Hexagon::A2_addi));
182       MI.getOperand(FIOp).ChangeToImmediate(RealOffset);
183       MI.RemoveOperand(FIOp+1);
184       return;
185     case Hexagon::TFR_FI:
186       // Set up the instruction for updating below.
187       MI.setDesc(HII.get(Hexagon::A2_addi));
188       break;
189   }
190
191   unsigned BP = 0;
192   bool Valid = false;
193   if (UseFP) {
194     BP = FP;
195     Valid = ValidFP;
196   } else if (UseAP) {
197     BP = AP;
198     Valid = ValidFP;
199   } else {
200     BP = SP;
201     Valid = ValidSP;
202   }
203
204   if (Valid) {
205     MI.getOperand(FIOp).ChangeToRegister(BP, false);
206     MI.getOperand(FIOp+1).ChangeToImmediate(RealOffset);
207     return;
208   }
209
210 #ifndef NDEBUG
211   const Function *F = MF.getFunction();
212   dbgs() << "In function ";
213   if (F) dbgs() << F->getName();
214   else   dbgs() << "<?>";
215   dbgs() << ", BB#" << MB.getNumber() << "\n" << MI;
216 #endif
217   llvm_unreachable("Unhandled instruction");
218 }
219
220
221 unsigned HexagonRegisterInfo::getRARegister() const {
222   return Hexagon::R31;
223 }
224
225
226 unsigned HexagonRegisterInfo::getFrameRegister(const MachineFunction
227                                                &MF) const {
228   const HexagonFrameLowering *TFI = getFrameLowering(MF);
229   if (TFI->hasFP(MF))
230     return Hexagon::R30;
231   return Hexagon::R29;
232 }
233
234
235 unsigned HexagonRegisterInfo::getFrameRegister() const {
236   return Hexagon::R30;
237 }
238
239
240 unsigned HexagonRegisterInfo::getStackRegister() const {
241   return Hexagon::R29;
242 }
243
244
245 bool
246 HexagonRegisterInfo::useFPForScavengingIndex(const MachineFunction &MF) const {
247   const HexagonFrameLowering *TFI = getFrameLowering(MF);
248   return TFI->hasFP(MF);
249 }
250
251
252 unsigned HexagonRegisterInfo::getFirstCallerSavedNonParamReg() const {
253   return Hexagon::R6;
254 }
255
256
257 #define GET_REGINFO_TARGET_DESC
258 #include "HexagonGenRegisterInfo.inc"