1894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===- MipsRegisterInfo.cpp - MIPS Register Information -== -----*- C++ -*-===//
2894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
3894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//                     The LLVM Compiler Infrastructure
4894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
5894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This file is distributed under the University of Illinois Open Source
6894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// License. See LICENSE.TXT for details.
7894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
8894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
9894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
10894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This file contains the MIPS implementation of the TargetRegisterInfo class.
11894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
12894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
13894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
14894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#define DEBUG_TYPE "mips-reg-info"
15894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
16894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "Mips.h"
17894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "MipsSubtarget.h"
18894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "MipsRegisterInfo.h"
19894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "MipsMachineFunction.h"
20894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Constants.h"
21894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Type.h"
22894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Function.h"
23894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/CodeGen/ValueTypes.h"
24894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/CodeGen/MachineInstrBuilder.h"
25894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/CodeGen/MachineFunction.h"
26894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/CodeGen/MachineFrameInfo.h"
2719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#include "llvm/Target/TargetFrameLowering.h"
28894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Target/TargetMachine.h"
29894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Target/TargetOptions.h"
30894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Target/TargetInstrInfo.h"
31894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Support/CommandLine.h"
32894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Support/Debug.h"
33894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Support/ErrorHandling.h"
34894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Support/raw_ostream.h"
35894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/ADT/BitVector.h"
36894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/ADT/STLExtras.h"
3719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#include "llvm/Analysis/DebugInfo.h"
3819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
3919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#define GET_REGINFO_TARGET_DESC
4019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#include "MipsGenRegisterInfo.inc"
41894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
42894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanusing namespace llvm;
43894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
4419bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanMipsRegisterInfo::MipsRegisterInfo(const MipsSubtarget &ST,
45894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                   const TargetInstrInfo &tii)
4619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  : MipsGenRegisterInfo(Mips::RA), Subtarget(ST), TII(tii) {}
47894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
48894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// getRegisterNumbering - Given the enum value for some register, e.g.
49894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// Mips::RA, return the number that it corresponds to (e.g. 31).
50894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanunsigned MipsRegisterInfo::
5119bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumangetRegisterNumbering(unsigned RegEnum)
52894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman{
53894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  switch (RegEnum) {
5419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case Mips::ZERO: case Mips::ZERO_64: case Mips::F0: case Mips::D0_64:
5519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case Mips::D0:
5619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return 0;
5719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case Mips::AT: case Mips::AT_64: case Mips::F1: case Mips::D1_64:
5819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return 1;
5919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case Mips::V0: case Mips::V0_64: case Mips::F2: case Mips::D2_64:
6019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case Mips::D1:
6119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return 2;
6219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case Mips::V1: case Mips::V1_64: case Mips::F3: case Mips::D3_64:
6319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return 3;
6419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case Mips::A0: case Mips::A0_64: case Mips::F4: case Mips::D4_64:
6519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case Mips::D2:
6619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return 4;
6719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case Mips::A1: case Mips::A1_64: case Mips::F5: case Mips::D5_64:
6819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return 5;
6919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case Mips::A2: case Mips::A2_64: case Mips::F6: case Mips::D6_64:
7019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case Mips::D3:
7119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return 6;
7219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case Mips::A3: case Mips::A3_64: case Mips::F7: case Mips::D7_64:
7319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return 7;
7419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case Mips::T0: case Mips::T0_64: case Mips::F8: case Mips::D8_64:
7519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case Mips::D4:
7619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return 8;
7719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case Mips::T1: case Mips::T1_64: case Mips::F9: case Mips::D9_64:
7819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return 9;
7919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case Mips::T2: case Mips::T2_64: case Mips::F10: case Mips::D10_64:
8019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case Mips::D5:
8119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return 10;
8219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case Mips::T3: case Mips::T3_64: case Mips::F11: case Mips::D11_64:
8319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return 11;
8419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case Mips::T4: case Mips::T4_64: case Mips::F12: case Mips::D12_64:
8519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case Mips::D6:
8619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return 12;
8719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case Mips::T5: case Mips::T5_64: case Mips::F13: case Mips::D13_64:
8819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return 13;
8919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case Mips::T6: case Mips::T6_64: case Mips::F14: case Mips::D14_64:
9019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case Mips::D7:
9119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return 14;
9219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case Mips::T7: case Mips::T7_64: case Mips::F15: case Mips::D15_64:
9319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return 15;
9419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case Mips::S0: case Mips::S0_64: case Mips::F16: case Mips::D16_64:
9519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case Mips::D8:
9619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return 16;
9719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case Mips::S1: case Mips::S1_64: case Mips::F17: case Mips::D17_64:
9819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return 17;
9919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case Mips::S2: case Mips::S2_64: case Mips::F18: case Mips::D18_64:
10019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case Mips::D9:
10119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return 18;
10219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case Mips::S3: case Mips::S3_64: case Mips::F19: case Mips::D19_64:
10319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return 19;
10419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case Mips::S4: case Mips::S4_64: case Mips::F20: case Mips::D20_64:
10519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case Mips::D10:
10619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return 20;
10719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case Mips::S5: case Mips::S5_64: case Mips::F21: case Mips::D21_64:
10819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return 21;
10919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case Mips::S6: case Mips::S6_64: case Mips::F22: case Mips::D22_64:
11019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case Mips::D11:
11119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return 22;
11219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case Mips::S7: case Mips::S7_64: case Mips::F23: case Mips::D23_64:
11319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return 23;
11419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case Mips::T8: case Mips::T8_64: case Mips::F24: case Mips::D24_64:
11519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case Mips::D12:
11619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return 24;
11719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case Mips::T9: case Mips::T9_64: case Mips::F25: case Mips::D25_64:
11819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return 25;
11919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case Mips::K0: case Mips::K0_64: case Mips::F26: case Mips::D26_64:
12019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case Mips::D13:
12119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return 26;
12219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case Mips::K1: case Mips::K1_64: case Mips::F27: case Mips::D27_64:
12319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return 27;
12419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case Mips::GP: case Mips::GP_64: case Mips::F28: case Mips::D28_64:
12519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case Mips::D14:
12619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return 28;
12719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case Mips::SP: case Mips::SP_64: case Mips::F29: case Mips::D29_64:
12819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return 29;
12919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case Mips::FP: case Mips::FP_64: case Mips::F30: case Mips::D30_64:
13019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case Mips::D15:
13119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return 30;
13219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case Mips::RA: case Mips::RA_64: case Mips::F31: case Mips::D31_64:
13319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return 31;
13419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  default: llvm_unreachable("Unknown register number!");
13519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
136894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return 0; // Not reached
137894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
138894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
139894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanunsigned MipsRegisterInfo::getPICCallReg() { return Mips::T9; }
140894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
141894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
14219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// Callee Saved Registers methods
143894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
144894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
145894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// Mips Callee Saved Registers
146894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanconst unsigned* MipsRegisterInfo::
14719bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumangetCalleeSavedRegs(const MachineFunction *MF) const
148894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman{
149894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Mips callee-save register range is $16-$23, $f20-$f30
150894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  static const unsigned SingleFloatOnlyCalleeSavedRegs[] = {
15119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Mips::F31, Mips::F30, Mips::F29, Mips::F28, Mips::F27, Mips::F26,
15219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Mips::F25, Mips::F24, Mips::F23, Mips::F22, Mips::F21, Mips::F20,
15319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Mips::RA, Mips::FP, Mips::S7, Mips::S6, Mips::S5, Mips::S4,
15419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Mips::S3, Mips::S2, Mips::S1, Mips::S0, 0
15519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  };
15619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
15719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  static const unsigned Mips32CalleeSavedRegs[] = {
15819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Mips::D15, Mips::D14, Mips::D13, Mips::D12, Mips::D11, Mips::D10,
15919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Mips::RA, Mips::FP, Mips::S7, Mips::S6, Mips::S5, Mips::S4,
16019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Mips::S3, Mips::S2, Mips::S1, Mips::S0, 0
16119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  };
16219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
16319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  static const unsigned N32CalleeSavedRegs[] = {
16419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Mips::D31_64, Mips::D29_64, Mips::D27_64, Mips::D25_64, Mips::D23_64,
16519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Mips::D21_64,
16619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Mips::RA_64, Mips::FP_64, Mips::GP_64, Mips::S7_64, Mips::S6_64,
16719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Mips::S5_64, Mips::S4_64, Mips::S3_64, Mips::S2_64, Mips::S1_64,
16819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Mips::S0_64, 0
169894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  };
170894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
17119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  static const unsigned N64CalleeSavedRegs[] = {
17219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Mips::D31_64, Mips::D30_64, Mips::D29_64, Mips::D28_64, Mips::D27_64,
17319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Mips::D26_64, Mips::D25_64, Mips::D24_64,
17419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Mips::RA_64, Mips::FP_64, Mips::GP_64, Mips::S7_64, Mips::S6_64,
17519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Mips::S5_64, Mips::S4_64, Mips::S3_64, Mips::S2_64, Mips::S1_64,
17619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Mips::S0_64, 0
177894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  };
178894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
179894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Subtarget.isSingleFloat())
180894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return SingleFloatOnlyCalleeSavedRegs;
18119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  else if (!Subtarget.hasMips64())
18219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return Mips32CalleeSavedRegs;
18319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  else if (Subtarget.isABI_N32())
18419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return N32CalleeSavedRegs;
18519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
18619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  assert(Subtarget.isABI_N64());
18719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return N64CalleeSavedRegs;
188894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
189894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
190894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanBitVector MipsRegisterInfo::
19119bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumangetReservedRegs(const MachineFunction &MF) const {
19219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  static const unsigned ReservedCPURegs[] = {
19319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Mips::ZERO, Mips::AT, Mips::K0, Mips::K1,
19419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Mips::GP, Mips::SP, Mips::FP, Mips::RA, 0
19519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  };
19619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
19719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  static const unsigned ReservedCPU64Regs[] = {
19819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Mips::ZERO_64, Mips::AT_64, Mips::K0_64, Mips::K1_64,
19919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Mips::GP_64, Mips::SP_64, Mips::FP_64, Mips::RA_64, 0
20019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  };
20119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
202894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  BitVector Reserved(getNumRegs());
20319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  typedef TargetRegisterClass::iterator RegIter;
204894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
20519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  for (const unsigned *Reg = ReservedCPURegs; *Reg; ++Reg)
20619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Reserved.set(*Reg);
207894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
20819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Subtarget.hasMips64()) {
20919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    for (const unsigned *Reg = ReservedCPU64Regs; *Reg; ++Reg)
21019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Reserved.set(*Reg);
211894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
21219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Reserve all registers in AFGR64.
21319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    for (RegIter Reg = Mips::AFGR64RegisterClass->begin();
21419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman         Reg != Mips::AFGR64RegisterClass->end(); ++Reg)
21519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Reserved.set(*Reg);
216894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
21719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  else {
21819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Reserve all registers in CPU64Regs & FGR64.
21919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    for (RegIter Reg = Mips::CPU64RegsRegisterClass->begin();
22019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman         Reg != Mips::CPU64RegsRegisterClass->end(); ++Reg)
22119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Reserved.set(*Reg);
22219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
22319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    for (RegIter Reg = Mips::FGR64RegisterClass->begin();
22419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman         Reg != Mips::FGR64RegisterClass->end(); ++Reg)
22519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Reserved.set(*Reg);
226894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
227894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
22819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return Reserved;
229894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
230894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
23119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// This function eliminate ADJCALLSTACKDOWN,
232894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// ADJCALLSTACKUP pseudo instructions
233894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid MipsRegisterInfo::
234894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumaneliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
235894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                              MachineBasicBlock::iterator I) const {
236894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Simply discard ADJCALLSTACKDOWN, ADJCALLSTACKUP instructions.
237894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MBB.erase(I);
238894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
239894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
240894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// FrameIndex represent objects inside a abstract stack.
241894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// We must replace FrameIndex with an stack/frame pointer
242894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// direct reference.
24319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanvoid MipsRegisterInfo::
244894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumaneliminateFrameIndex(MachineBasicBlock::iterator II, int SPAdj,
24519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                    RegScavenger *RS) const {
246894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineInstr &MI = *II;
247894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineFunction &MF = *MI.getParent()->getParent();
24819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  MachineFrameInfo *MFI = MF.getFrameInfo();
24919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
250894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
251894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned i = 0;
252894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  while (!MI.getOperand(i).isFI()) {
253894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ++i;
25419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    assert(i < MI.getNumOperands() &&
255894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman           "Instr doesn't have FrameIndex operand!");
256894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
257894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
258894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DEBUG(errs() << "\nFunction : " << MF.getFunction()->getName() << "\n";
259894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        errs() << "<--------->\n" << MI);
260894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
261894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  int FrameIndex = MI.getOperand(i).getIndex();
262894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  int stackSize  = MF.getFrameInfo()->getStackSize();
263894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  int spOffset   = MF.getFrameInfo()->getObjectOffset(FrameIndex);
264894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
265894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DEBUG(errs() << "FrameIndex : " << FrameIndex << "\n"
266894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman               << "spOffset   : " << spOffset << "\n"
267894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman               << "stackSize  : " << stackSize << "\n");
268894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
26919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
27019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  int MinCSFI = 0;
27119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  int MaxCSFI = -1;
272894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
27319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (CSI.size()) {
27419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    MinCSFI = CSI[0].getFrameIdx();
27519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    MaxCSFI = CSI[CSI.size() - 1].getFrameIdx();
276894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
277894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
27819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // The following stack frame objects are always referenced relative to $sp:
27919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //  1. Outgoing arguments.
28019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //  2. Pointer to dynamically allocated stack space.
28119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //  3. Locations for callee-saved registers.
28219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Everything else is referenced relative to whatever register
28319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // getFrameRegister() returns.
28419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned FrameReg;
28519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
28619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (MipsFI->isOutArgFI(FrameIndex) || MipsFI->isDynAllocFI(FrameIndex) ||
28719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      (FrameIndex >= MinCSFI && FrameIndex <= MaxCSFI))
28819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    FrameReg = Mips::SP;
28919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  else
29019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    FrameReg = getFrameRegister(MF);
29119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
29219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Calculate final offset.
29319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // - There is no need to change the offset if the frame object is one of the
29419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //   following: an outgoing argument, pointer to a dynamically allocated
29519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //   stack space or a $gp restore location,
29619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // - If the frame object is any of the following, its offset must be adjusted
29719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //   by adding the size of the stack:
29819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //   incoming argument, callee-saved register location or local variable.
29919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  int Offset;
30019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
30119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (MipsFI->isOutArgFI(FrameIndex) || MipsFI->isGPFI(FrameIndex) ||
30219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      MipsFI->isDynAllocFI(FrameIndex))
30319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Offset = spOffset;
30419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  else
30519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Offset = spOffset + stackSize;
306894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
30719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Offset    += MI.getOperand(i+1).getImm();
308894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
30919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  DEBUG(errs() << "Offset     : " << Offset << "\n" << "<--------->\n");
310894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
31119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // If MI is not a debug value, make sure Offset fits in the 16-bit immediate
31219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // field.
31319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!MI.isDebugValue() && (Offset >= 0x8000 || Offset < -0x8000)) {
31419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    MachineBasicBlock &MBB = *MI.getParent();
31519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    DebugLoc DL = II->getDebugLoc();
31619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    int ImmHi = (((unsigned)Offset & 0xffff0000) >> 16) +
31719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                ((Offset & 0x8000) != 0);
31819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
31919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // FIXME: change this when mips goes MC".
32019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    BuildMI(MBB, II, DL, TII.get(Mips::NOAT));
32119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    BuildMI(MBB, II, DL, TII.get(Mips::LUi), Mips::AT).addImm(ImmHi);
32219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    BuildMI(MBB, II, DL, TII.get(Mips::ADDu), Mips::AT).addReg(FrameReg)
32319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                                       .addReg(Mips::AT);
32419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    FrameReg = Mips::AT;
32519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Offset = (short)(Offset & 0xffff);
32619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
32719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    BuildMI(MBB, ++II, MI.getDebugLoc(), TII.get(Mips::ATMACRO));
328894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
329894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
33019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  MI.getOperand(i).ChangeToRegister(FrameReg, false);
33119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  MI.getOperand(i+1).ChangeToImmediate(Offset);
332894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
333894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
334894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanunsigned MipsRegisterInfo::
335894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumangetFrameRegister(const MachineFunction &MF) const {
33619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
33719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
33819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return TFI->hasFP(MF) ? Mips::FP : Mips::SP;
339894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
340894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
341894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanunsigned MipsRegisterInfo::
342894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumangetEHExceptionRegister() const {
343894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  llvm_unreachable("What is the exception register");
344894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return 0;
345894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
346894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
347894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanunsigned MipsRegisterInfo::
348894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumangetEHHandlerRegister() const {
349894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  llvm_unreachable("What is the exception handler register");
350894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return 0;
351894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
352