1894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===- PPCRegisterInfo.cpp - PowerPC 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 PowerPC implementation of the TargetRegisterInfo
11894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// class.
12894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
13894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
14894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
15894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#define DEBUG_TYPE "reginfo"
16894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "PPC.h"
17894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "PPCInstrBuilder.h"
18894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "PPCMachineFunctionInfo.h"
19894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "PPCRegisterInfo.h"
2019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#include "PPCFrameLowering.h"
21894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "PPCSubtarget.h"
22894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/CallingConv.h"
23894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Constants.h"
24894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Function.h"
25894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Type.h"
26894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/CodeGen/ValueTypes.h"
27894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/CodeGen/MachineInstrBuilder.h"
28894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/CodeGen/MachineModuleInfo.h"
29894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/CodeGen/MachineFunction.h"
30894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/CodeGen/MachineFrameInfo.h"
31894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/CodeGen/MachineRegisterInfo.h"
32894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/CodeGen/RegisterScavenging.h"
3319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#include "llvm/Target/TargetFrameLowering.h"
34894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Target/TargetInstrInfo.h"
35894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Target/TargetMachine.h"
36894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Target/TargetOptions.h"
37894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Support/CommandLine.h"
38894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Support/Debug.h"
39894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Support/ErrorHandling.h"
40894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Support/MathExtras.h"
41894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Support/raw_ostream.h"
42894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/ADT/BitVector.h"
43894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/ADT/STLExtras.h"
44894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include <cstdlib>
45894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
4619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#define GET_REGINFO_TARGET_DESC
4719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#include "PPCGenRegisterInfo.inc"
48894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
49894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// FIXME (64-bit): Eventually enable by default.
50894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumannamespace llvm {
51894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumancl::opt<bool> EnablePPC32RS("enable-ppc32-regscavenger",
52894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                   cl::init(false),
53894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                   cl::desc("Enable PPC32 register scavenger"),
54894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                   cl::Hidden);
55894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumancl::opt<bool> EnablePPC64RS("enable-ppc64-regscavenger",
56894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                   cl::init(false),
57894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                   cl::desc("Enable PPC64 register scavenger"),
58894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                   cl::Hidden);
59894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
60894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
61894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanusing namespace llvm;
62894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
63894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// FIXME (64-bit): Should be inlined.
64894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool
65894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanPPCRegisterInfo::requiresRegisterScavenging(const MachineFunction &) const {
6619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return ((EnablePPC32RS && !Subtarget.isPPC64()) ||
6719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          (EnablePPC64RS && Subtarget.isPPC64()));
68894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
69894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
70894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanPPCRegisterInfo::PPCRegisterInfo(const PPCSubtarget &ST,
71894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                 const TargetInstrInfo &tii)
7219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  : PPCGenRegisterInfo(ST.isPPC64() ? PPC::LR8 : PPC::LR,
7319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                       ST.isPPC64() ? 0 : 1,
7419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                       ST.isPPC64() ? 0 : 1),
75894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Subtarget(ST), TII(tii) {
76894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ImmToIdxMap[PPC::LD]   = PPC::LDX;    ImmToIdxMap[PPC::STD]  = PPC::STDX;
77894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ImmToIdxMap[PPC::LBZ]  = PPC::LBZX;   ImmToIdxMap[PPC::STB]  = PPC::STBX;
78894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ImmToIdxMap[PPC::LHZ]  = PPC::LHZX;   ImmToIdxMap[PPC::LHA]  = PPC::LHAX;
79894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ImmToIdxMap[PPC::LWZ]  = PPC::LWZX;   ImmToIdxMap[PPC::LWA]  = PPC::LWAX;
80894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ImmToIdxMap[PPC::LFS]  = PPC::LFSX;   ImmToIdxMap[PPC::LFD]  = PPC::LFDX;
81894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ImmToIdxMap[PPC::STH]  = PPC::STHX;   ImmToIdxMap[PPC::STW]  = PPC::STWX;
82894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ImmToIdxMap[PPC::STFS] = PPC::STFSX;  ImmToIdxMap[PPC::STFD] = PPC::STFDX;
83894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ImmToIdxMap[PPC::ADDI] = PPC::ADD4;
84894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
85894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // 64-bit
86894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ImmToIdxMap[PPC::LHA8] = PPC::LHAX8; ImmToIdxMap[PPC::LBZ8] = PPC::LBZX8;
87894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ImmToIdxMap[PPC::LHZ8] = PPC::LHZX8; ImmToIdxMap[PPC::LWZ8] = PPC::LWZX8;
88894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ImmToIdxMap[PPC::STB8] = PPC::STBX8; ImmToIdxMap[PPC::STH8] = PPC::STHX8;
89894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ImmToIdxMap[PPC::STW8] = PPC::STWX8; ImmToIdxMap[PPC::STDU] = PPC::STDUX;
90894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ImmToIdxMap[PPC::ADDI8] = PPC::ADD8; ImmToIdxMap[PPC::STD_32] = PPC::STDX_32;
91894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
92894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
93894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// getPointerRegClass - Return the register class to use to hold pointers.
94894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// This is used for addressing modes.
95894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanconst TargetRegisterClass *
96894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanPPCRegisterInfo::getPointerRegClass(unsigned Kind) const {
97894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Subtarget.isPPC64())
98894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return &PPC::G8RCRegClass;
99894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return &PPC::GPRCRegClass;
100894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
101894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
102894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanconst unsigned*
103894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanPPCRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
104894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // 32-bit Darwin calling convention.
105894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  static const unsigned Darwin32_CalleeSavedRegs[] = {
106894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman              PPC::R13, PPC::R14, PPC::R15,
107894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PPC::R16, PPC::R17, PPC::R18, PPC::R19,
108894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PPC::R20, PPC::R21, PPC::R22, PPC::R23,
109894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PPC::R24, PPC::R25, PPC::R26, PPC::R27,
110894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PPC::R28, PPC::R29, PPC::R30, PPC::R31,
111894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
112894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PPC::F14, PPC::F15, PPC::F16, PPC::F17,
113894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PPC::F18, PPC::F19, PPC::F20, PPC::F21,
114894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PPC::F22, PPC::F23, PPC::F24, PPC::F25,
115894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PPC::F26, PPC::F27, PPC::F28, PPC::F29,
116894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PPC::F30, PPC::F31,
117894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
118894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PPC::CR2, PPC::CR3, PPC::CR4,
119894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PPC::V20, PPC::V21, PPC::V22, PPC::V23,
120894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PPC::V24, PPC::V25, PPC::V26, PPC::V27,
121894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PPC::V28, PPC::V29, PPC::V30, PPC::V31,
122894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
123894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PPC::CR2LT, PPC::CR2GT, PPC::CR2EQ, PPC::CR2UN,
124894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PPC::CR3LT, PPC::CR3GT, PPC::CR3EQ, PPC::CR3UN,
125894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PPC::CR4LT, PPC::CR4GT, PPC::CR4EQ, PPC::CR4UN,
126894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
127894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PPC::LR,  0
128894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  };
129894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
130894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // 32-bit SVR4 calling convention.
131894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  static const unsigned SVR4_CalleeSavedRegs[] = {
132894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                        PPC::R14, PPC::R15,
133894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PPC::R16, PPC::R17, PPC::R18, PPC::R19,
134894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PPC::R20, PPC::R21, PPC::R22, PPC::R23,
135894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PPC::R24, PPC::R25, PPC::R26, PPC::R27,
136894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PPC::R28, PPC::R29, PPC::R30, PPC::R31,
137894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
138894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PPC::F14, PPC::F15, PPC::F16, PPC::F17,
139894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PPC::F18, PPC::F19, PPC::F20, PPC::F21,
140894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PPC::F22, PPC::F23, PPC::F24, PPC::F25,
141894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PPC::F26, PPC::F27, PPC::F28, PPC::F29,
142894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PPC::F30, PPC::F31,
143894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
144894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PPC::CR2, PPC::CR3, PPC::CR4,
145894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
146894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PPC::VRSAVE,
147894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
148894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PPC::V20, PPC::V21, PPC::V22, PPC::V23,
149894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PPC::V24, PPC::V25, PPC::V26, PPC::V27,
150894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PPC::V28, PPC::V29, PPC::V30, PPC::V31,
151894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
152894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PPC::CR2LT, PPC::CR2GT, PPC::CR2EQ, PPC::CR2UN,
153894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PPC::CR3LT, PPC::CR3GT, PPC::CR3EQ, PPC::CR3UN,
154894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PPC::CR4LT, PPC::CR4GT, PPC::CR4EQ, PPC::CR4UN,
155894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
156894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    0
157894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  };
158894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // 64-bit Darwin calling convention.
159894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  static const unsigned Darwin64_CalleeSavedRegs[] = {
160894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PPC::X14, PPC::X15,
161894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PPC::X16, PPC::X17, PPC::X18, PPC::X19,
162894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PPC::X20, PPC::X21, PPC::X22, PPC::X23,
163894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PPC::X24, PPC::X25, PPC::X26, PPC::X27,
164894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PPC::X28, PPC::X29, PPC::X30, PPC::X31,
165894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
166894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PPC::F14, PPC::F15, PPC::F16, PPC::F17,
167894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PPC::F18, PPC::F19, PPC::F20, PPC::F21,
168894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PPC::F22, PPC::F23, PPC::F24, PPC::F25,
169894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PPC::F26, PPC::F27, PPC::F28, PPC::F29,
170894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PPC::F30, PPC::F31,
171894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
172894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PPC::CR2, PPC::CR3, PPC::CR4,
173894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PPC::V20, PPC::V21, PPC::V22, PPC::V23,
174894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PPC::V24, PPC::V25, PPC::V26, PPC::V27,
175894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PPC::V28, PPC::V29, PPC::V30, PPC::V31,
176894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
177894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PPC::CR2LT, PPC::CR2GT, PPC::CR2EQ, PPC::CR2UN,
178894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PPC::CR3LT, PPC::CR3GT, PPC::CR3EQ, PPC::CR3UN,
179894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PPC::CR4LT, PPC::CR4GT, PPC::CR4EQ, PPC::CR4UN,
180894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
181894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PPC::LR8,  0
182894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  };
183894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
184894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // 64-bit SVR4 calling convention.
185894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  static const unsigned SVR4_64_CalleeSavedRegs[] = {
186894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PPC::X14, PPC::X15,
187894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PPC::X16, PPC::X17, PPC::X18, PPC::X19,
188894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PPC::X20, PPC::X21, PPC::X22, PPC::X23,
189894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PPC::X24, PPC::X25, PPC::X26, PPC::X27,
190894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PPC::X28, PPC::X29, PPC::X30, PPC::X31,
191894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
192894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PPC::F14, PPC::F15, PPC::F16, PPC::F17,
193894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PPC::F18, PPC::F19, PPC::F20, PPC::F21,
194894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PPC::F22, PPC::F23, PPC::F24, PPC::F25,
195894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PPC::F26, PPC::F27, PPC::F28, PPC::F29,
196894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PPC::F30, PPC::F31,
197894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
198894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PPC::CR2, PPC::CR3, PPC::CR4,
199894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
200894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PPC::VRSAVE,
201894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
202894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PPC::V20, PPC::V21, PPC::V22, PPC::V23,
203894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PPC::V24, PPC::V25, PPC::V26, PPC::V27,
204894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PPC::V28, PPC::V29, PPC::V30, PPC::V31,
205894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
206894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PPC::CR2LT, PPC::CR2GT, PPC::CR2EQ, PPC::CR2UN,
207894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PPC::CR3LT, PPC::CR3GT, PPC::CR3EQ, PPC::CR3UN,
208894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PPC::CR4LT, PPC::CR4GT, PPC::CR4EQ, PPC::CR4UN,
209894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
210894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    0
211894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  };
212894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
213894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Subtarget.isDarwinABI())
214894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return Subtarget.isPPC64() ? Darwin64_CalleeSavedRegs :
215894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                 Darwin32_CalleeSavedRegs;
216894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
217894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return Subtarget.isPPC64() ? SVR4_64_CalleeSavedRegs : SVR4_CalleeSavedRegs;
218894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
219894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
220894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanBitVector PPCRegisterInfo::getReservedRegs(const MachineFunction &MF) const {
221894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  BitVector Reserved(getNumRegs());
22219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  const PPCFrameLowering *PPCFI =
22319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    static_cast<const PPCFrameLowering*>(MF.getTarget().getFrameLowering());
22419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
225894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Reserved.set(PPC::R0);
226894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Reserved.set(PPC::R1);
227894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Reserved.set(PPC::LR);
228894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Reserved.set(PPC::LR8);
229894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Reserved.set(PPC::RM);
230894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
231894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // The SVR4 ABI reserves r2 and r13
232894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Subtarget.isSVR4ABI()) {
233894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Reserved.set(PPC::R2);  // System-reserved register
234894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Reserved.set(PPC::R13); // Small Data Area pointer register
235894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
236894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Reserve R2 on Darwin to hack around the problem of save/restore of CR
237894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // when the stack frame is too big to address directly; we need two regs.
238894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // This is a hack.
239894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Subtarget.isDarwinABI()) {
240894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Reserved.set(PPC::R2);
241894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
242894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
243894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // On PPC64, r13 is the thread pointer. Never allocate this register.
244894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Note that this is over conservative, as it also prevents allocation of R31
245894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // when the FP is not needed.
246894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Subtarget.isPPC64()) {
247894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Reserved.set(PPC::R13);
248894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Reserved.set(PPC::R31);
249894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
25019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (!requiresRegisterScavenging(MF))
251894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Reserved.set(PPC::R0);    // FIXME (64-bit): Remove
252894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
253894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Reserved.set(PPC::X0);
254894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Reserved.set(PPC::X1);
255894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Reserved.set(PPC::X13);
256894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Reserved.set(PPC::X31);
257894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
258894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // The 64-bit SVR4 ABI reserves r2 for the TOC pointer.
259894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Subtarget.isSVR4ABI()) {
260894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Reserved.set(PPC::X2);
261894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
262894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Reserve R2 on Darwin to hack around the problem of save/restore of CR
263894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // when the stack frame is too big to address directly; we need two regs.
264894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // This is a hack.
265894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Subtarget.isDarwinABI()) {
266894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Reserved.set(PPC::X2);
267894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
268894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
269894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
27019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (PPCFI->needsFP(MF))
271894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Reserved.set(PPC::R31);
272894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
273894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return Reserved;
274894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
275894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
276894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
277894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Stack Frame Processing methods
278894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
279894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
280894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid PPCRegisterInfo::
281894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumaneliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
282894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                              MachineBasicBlock::iterator I) const {
283894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (GuaranteedTailCallOpt && I->getOpcode() == PPC::ADJCALLSTACKUP) {
284894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Add (actually subtract) back the amount the callee popped on return.
285894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (int CalleeAmt =  I->getOperand(1).getImm()) {
286894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      bool is64Bit = Subtarget.isPPC64();
287894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      CalleeAmt *= -1;
288894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      unsigned StackReg = is64Bit ? PPC::X1 : PPC::R1;
289894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      unsigned TmpReg = is64Bit ? PPC::X0 : PPC::R0;
290894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      unsigned ADDIInstr = is64Bit ? PPC::ADDI8 : PPC::ADDI;
291894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      unsigned ADDInstr = is64Bit ? PPC::ADD8 : PPC::ADD4;
292894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      unsigned LISInstr = is64Bit ? PPC::LIS8 : PPC::LIS;
293894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      unsigned ORIInstr = is64Bit ? PPC::ORI8 : PPC::ORI;
294894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      MachineInstr *MI = I;
295894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      DebugLoc dl = MI->getDebugLoc();
296894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
297894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (isInt<16>(CalleeAmt)) {
298894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        BuildMI(MBB, I, dl, TII.get(ADDIInstr), StackReg).addReg(StackReg).
299894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          addImm(CalleeAmt);
300894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      } else {
301894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        MachineBasicBlock::iterator MBBI = I;
302894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        BuildMI(MBB, MBBI, dl, TII.get(LISInstr), TmpReg)
303894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          .addImm(CalleeAmt >> 16);
304894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        BuildMI(MBB, MBBI, dl, TII.get(ORIInstr), TmpReg)
305894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          .addReg(TmpReg, RegState::Kill)
306894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          .addImm(CalleeAmt & 0xFFFF);
307894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        BuildMI(MBB, MBBI, dl, TII.get(ADDInstr))
308894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          .addReg(StackReg)
309894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          .addReg(StackReg)
310894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          .addReg(TmpReg);
311894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
312894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
313894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
314894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Simply discard ADJCALLSTACKDOWN, ADJCALLSTACKUP instructions.
315894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MBB.erase(I);
316894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
317894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
318894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// findScratchRegister - Find a 'free' PPC register. Try for a call-clobbered
319894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// register first and then a spilled callee-saved register if that fails.
320894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic
321894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanunsigned findScratchRegister(MachineBasicBlock::iterator II, RegScavenger *RS,
322894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                             const TargetRegisterClass *RC, int SPAdj) {
323894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(RS && "Register scavenging must be on");
324894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned Reg = RS->FindUnusedReg(RC);
325894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // FIXME: move ARM callee-saved reg scan to target independent code, then
326894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // search for already spilled CS register here.
327894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Reg == 0)
328894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Reg = RS->scavengeRegister(RC, II, SPAdj);
329894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return Reg;
330894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
331894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
332894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// lowerDynamicAlloc - Generate the code for allocating an object in the
333894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// current frame.  The sequence of code with be in the general form
334894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///
335894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///   addi   R0, SP, \#frameSize ; get the address of the previous frame
336894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///   stwxu  R0, SP, Rnegsize   ; add and update the SP with the negated size
337894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///   addi   Rnew, SP, \#maxCalFrameSize ; get the top of the allocation
338894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///
339894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid PPCRegisterInfo::lowerDynamicAlloc(MachineBasicBlock::iterator II,
340894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                        int SPAdj, RegScavenger *RS) const {
341894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Get the instruction.
342894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineInstr &MI = *II;
343894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Get the instruction's basic block.
344894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineBasicBlock &MBB = *MI.getParent();
345894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Get the basic block's function.
346894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineFunction &MF = *MBB.getParent();
347894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Get the frame info.
348894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineFrameInfo *MFI = MF.getFrameInfo();
349894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Determine whether 64-bit pointers are used.
350894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool LP64 = Subtarget.isPPC64();
351894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc dl = MI.getDebugLoc();
352894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
353894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Get the maximum call stack size.
354894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned maxCallFrameSize = MFI->getMaxCallFrameSize();
355894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Get the total frame size.
356894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned FrameSize = MFI->getStackSize();
357894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
358894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Get stack alignments.
35919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned TargetAlign = MF.getTarget().getFrameLowering()->getStackAlignment();
360894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned MaxAlign = MFI->getMaxAlignment();
361894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (MaxAlign > TargetAlign)
362894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    report_fatal_error("Dynamic alloca with large aligns not supported");
363894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
364894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Determine the previous frame's address.  If FrameSize can't be
365894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // represented as 16 bits or we need special alignment, then we load the
366894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // previous frame's address from 0(SP).  Why not do an addis of the hi?
367894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Because R0 is our only safe tmp register and addi/addis treat R0 as zero.
368894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Constructing the constant and adding would take 3 instructions.
369894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Fortunately, a frame greater than 32K is rare.
370894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
371894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
372894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const TargetRegisterClass *RC = LP64 ? G8RC : GPRC;
373894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
374894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // FIXME (64-bit): Use "findScratchRegister"
375894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned Reg;
37619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (requiresRegisterScavenging(MF))
377894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Reg = findScratchRegister(II, RS, RC, SPAdj);
378894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  else
379894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Reg = PPC::R0;
380894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
381894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (MaxAlign < TargetAlign && isInt<16>(FrameSize)) {
382894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    BuildMI(MBB, II, dl, TII.get(PPC::ADDI), Reg)
383894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      .addReg(PPC::R31)
384894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      .addImm(FrameSize);
385894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (LP64) {
38619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (requiresRegisterScavenging(MF)) // FIXME (64-bit): Use "true" part.
387894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      BuildMI(MBB, II, dl, TII.get(PPC::LD), Reg)
388894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        .addImm(0)
389894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        .addReg(PPC::X1);
390894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else
391894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      BuildMI(MBB, II, dl, TII.get(PPC::LD), PPC::X0)
392894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        .addImm(0)
393894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        .addReg(PPC::X1);
394894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else {
395894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    BuildMI(MBB, II, dl, TII.get(PPC::LWZ), Reg)
396894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      .addImm(0)
397894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      .addReg(PPC::R1);
398894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
399894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
400894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Grow the stack and update the stack pointer link, then determine the
401894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // address of new allocated space.
402894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (LP64) {
40319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (requiresRegisterScavenging(MF)) // FIXME (64-bit): Use "true" part.
404894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      BuildMI(MBB, II, dl, TII.get(PPC::STDUX))
405894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        .addReg(Reg, RegState::Kill)
406894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        .addReg(PPC::X1)
407894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        .addReg(MI.getOperand(1).getReg());
408894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else
409894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      BuildMI(MBB, II, dl, TII.get(PPC::STDUX))
410894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        .addReg(PPC::X0, RegState::Kill)
411894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        .addReg(PPC::X1)
412894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        .addReg(MI.getOperand(1).getReg());
413894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
414894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!MI.getOperand(1).isKill())
415894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      BuildMI(MBB, II, dl, TII.get(PPC::ADDI8), MI.getOperand(0).getReg())
416894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        .addReg(PPC::X1)
417894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        .addImm(maxCallFrameSize);
418894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else
419894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Implicitly kill the register.
420894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      BuildMI(MBB, II, dl, TII.get(PPC::ADDI8), MI.getOperand(0).getReg())
421894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        .addReg(PPC::X1)
422894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        .addImm(maxCallFrameSize)
423894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        .addReg(MI.getOperand(1).getReg(), RegState::ImplicitKill);
424894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else {
425894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    BuildMI(MBB, II, dl, TII.get(PPC::STWUX))
426894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      .addReg(Reg, RegState::Kill)
427894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      .addReg(PPC::R1)
428894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      .addReg(MI.getOperand(1).getReg());
429894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
430894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!MI.getOperand(1).isKill())
431894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      BuildMI(MBB, II, dl, TII.get(PPC::ADDI), MI.getOperand(0).getReg())
432894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        .addReg(PPC::R1)
433894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        .addImm(maxCallFrameSize);
434894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else
435894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Implicitly kill the register.
436894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      BuildMI(MBB, II, dl, TII.get(PPC::ADDI), MI.getOperand(0).getReg())
437894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        .addReg(PPC::R1)
438894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        .addImm(maxCallFrameSize)
439894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        .addReg(MI.getOperand(1).getReg(), RegState::ImplicitKill);
440894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
441894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
442894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Discard the DYNALLOC instruction.
443894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MBB.erase(II);
444894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
445894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
446894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// lowerCRSpilling - Generate the code for spilling a CR register. Instead of
447894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// reserving a whole register (R0), we scrounge for one here. This generates
448894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// code like this:
449894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///
450894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///   mfcr rA                  ; Move the conditional register into GPR rA.
451894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///   rlwinm rA, rA, SB, 0, 31 ; Shift the bits left so they are in CR0's slot.
452894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///   stw rA, FI               ; Store rA to the frame.
453894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///
454894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid PPCRegisterInfo::lowerCRSpilling(MachineBasicBlock::iterator II,
455894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                      unsigned FrameIndex, int SPAdj,
456894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                      RegScavenger *RS) const {
457894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Get the instruction.
458894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineInstr &MI = *II;       // ; SPILL_CR <SrcReg>, <offset>, <FI>
459894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Get the instruction's basic block.
460894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineBasicBlock &MBB = *MI.getParent();
461894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc dl = MI.getDebugLoc();
462894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
463894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
464894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
465894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const TargetRegisterClass *RC = Subtarget.isPPC64() ? G8RC : GPRC;
466894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned Reg = findScratchRegister(II, RS, RC, SPAdj);
467894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned SrcReg = MI.getOperand(0).getReg();
46819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  bool LP64 = Subtarget.isPPC64();
469894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
470894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // We need to store the CR in the low 4-bits of the saved value. First, issue
471894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // an MFCRpsued to save all of the CRBits and, if needed, kill the SrcReg.
472894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  BuildMI(MBB, II, dl, TII.get(PPC::MFCRpseud), Reg)
473894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          .addReg(SrcReg, getKillRegState(MI.getOperand(0).isKill()));
474894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
475894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If the saved register wasn't CR0, shift the bits left so that they are in
476894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // CR0's slot.
477894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (SrcReg != PPC::CR0)
478894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // rlwinm rA, rA, ShiftBits, 0, 31.
479894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    BuildMI(MBB, II, dl, TII.get(PPC::RLWINM), Reg)
480894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      .addReg(Reg, RegState::Kill)
48119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      .addImm(getPPCRegisterNumbering(SrcReg) * 4)
482894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      .addImm(0)
483894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      .addImm(31);
484894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
48519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  addFrameReference(BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::STW8 : PPC::STW))
486894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                    .addReg(Reg, getKillRegState(MI.getOperand(1).getImm())),
487894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                    FrameIndex);
488894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
489894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Discard the pseudo instruction.
490894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MBB.erase(II);
491894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
492894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
49319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanvoid
494894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanPPCRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
49519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                     int SPAdj, RegScavenger *RS) const {
496894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(SPAdj == 0 && "Unexpected");
497894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
498894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Get the instruction.
499894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineInstr &MI = *II;
500894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Get the instruction's basic block.
501894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineBasicBlock &MBB = *MI.getParent();
502894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Get the basic block's function.
503894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineFunction &MF = *MBB.getParent();
504894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Get the frame info.
505894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineFrameInfo *MFI = MF.getFrameInfo();
50619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
507894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc dl = MI.getDebugLoc();
508894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
509894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Find out which operand is the frame index.
510894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned FIOperandNo = 0;
511894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  while (!MI.getOperand(FIOperandNo).isFI()) {
512894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ++FIOperandNo;
513894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    assert(FIOperandNo != MI.getNumOperands() &&
514894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman           "Instr doesn't have FrameIndex operand!");
515894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
516894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Take into account whether it's an add or mem instruction
517894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned OffsetOperandNo = (FIOperandNo == 2) ? 1 : 2;
518894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (MI.isInlineAsm())
519894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    OffsetOperandNo = FIOperandNo-1;
520894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
521894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Get the frame index.
522894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  int FrameIndex = MI.getOperand(FIOperandNo).getIndex();
523894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
524894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Get the frame pointer save index.  Users of this index are primarily
525894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // DYNALLOC instructions.
526894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
527894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  int FPSI = FI->getFramePointerSaveIndex();
528894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Get the instruction opcode.
529894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned OpC = MI.getOpcode();
530894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
531894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Special case for dynamic alloca.
532894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (FPSI && FrameIndex == FPSI &&
533894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      (OpC == PPC::DYNALLOC || OpC == PPC::DYNALLOC8)) {
534894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    lowerDynamicAlloc(II, SPAdj, RS);
53519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return;
536894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
537894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
538894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Special case for pseudo-op SPILL_CR.
53919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (requiresRegisterScavenging(MF)) // FIXME (64-bit): Enable by default.
540894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (OpC == PPC::SPILL_CR) {
541894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      lowerCRSpilling(II, FrameIndex, SPAdj, RS);
54219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return;
543894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
544894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
545894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Replace the FrameIndex with base register with GPR1 (SP) or GPR31 (FP).
54619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  MI.getOperand(FIOperandNo).ChangeToRegister(TFI->hasFP(MF) ?
54719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                              PPC::R31 : PPC::R1,
548894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                              false);
549894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
550894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Figure out if the offset in the instruction is shifted right two bits. This
551894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // is true for instructions like "STD", which the machine implicitly adds two
552894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // low zeros to.
553894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool isIXAddr = false;
554894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  switch (OpC) {
555894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case PPC::LWA:
556894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case PPC::LD:
557894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case PPC::STD:
558894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case PPC::STD_32:
559894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    isIXAddr = true;
560894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
561894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
562894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
563894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Now add the frame object offset to the offset from r1.
564894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  int Offset = MFI->getObjectOffset(FrameIndex);
565894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!isIXAddr)
566894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Offset += MI.getOperand(OffsetOperandNo).getImm();
567894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  else
568894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Offset += MI.getOperand(OffsetOperandNo).getImm() << 2;
569894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
570894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If we're not using a Frame Pointer that has been set to the value of the
571894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // SP before having the stack size subtracted from it, then add the stack size
572894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // to Offset to get the correct offset.
573894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Naked functions have stack size 0, although getStackSize may not reflect that
574894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // because we didn't call all the pieces that compute it for naked functions.
575894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!MF.getFunction()->hasFnAttr(Attribute::Naked))
576894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Offset += MFI->getStackSize();
577894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
578894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If we can, encode the offset directly into the instruction.  If this is a
579894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // normal PPC "ri" instruction, any 16-bit value can be safely encoded.  If
580894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // this is a PPC64 "ix" instruction, only a 16-bit value with the low two bits
581894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // clear can be encoded.  This is extremely uncommon, because normally you
582894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // only "std" to a stack slot that is at least 4-byte aligned, but it can
583894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // happen in invalid code.
584894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (isInt<16>(Offset) && (!isIXAddr || (Offset & 3) == 0)) {
585894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (isIXAddr)
586894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Offset >>= 2;    // The actual encoded value has the low two bits zero.
587894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    MI.getOperand(OffsetOperandNo).ChangeToImmediate(Offset);
58819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return;
589894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
590894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
591894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // The offset doesn't fit into a single register, scavenge one to build the
592894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // offset in.
593894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // FIXME: figure out what SPAdj is doing here.
594894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
595894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // FIXME (64-bit): Use "findScratchRegister".
596894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned SReg;
59719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (requiresRegisterScavenging(MF))
598894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SReg = findScratchRegister(II, RS, &PPC::GPRCRegClass, SPAdj);
599894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  else
600894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SReg = PPC::R0;
601894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
602894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Insert a set of rA with the full offset value before the ld, st, or add
603894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  BuildMI(MBB, II, dl, TII.get(PPC::LIS), SReg)
604894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    .addImm(Offset >> 16);
605894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  BuildMI(MBB, II, dl, TII.get(PPC::ORI), SReg)
606894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    .addReg(SReg, RegState::Kill)
607894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    .addImm(Offset);
608894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
609894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Convert into indexed form of the instruction:
610894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //
611894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //   sth 0:rA, 1:imm 2:(rB) ==> sthx 0:rA, 2:rB, 1:r0
612894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //   addi 0:rA 1:rB, 2, imm ==> add 0:rA, 1:rB, 2:r0
613894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned OperandBase;
614894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
615894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (OpC != TargetOpcode::INLINEASM) {
616894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    assert(ImmToIdxMap.count(OpC) &&
617894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman           "No indexed form of load or store available!");
618894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    unsigned NewOpcode = ImmToIdxMap.find(OpC)->second;
619894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    MI.setDesc(TII.get(NewOpcode));
620894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    OperandBase = 1;
621894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else {
622894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    OperandBase = OffsetOperandNo;
623894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
62419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
625894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned StackReg = MI.getOperand(FIOperandNo).getReg();
626894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MI.getOperand(OperandBase).ChangeToRegister(StackReg, false);
627894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MI.getOperand(OperandBase + 1).ChangeToRegister(SReg, false);
628894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
629894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
630894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanunsigned PPCRegisterInfo::getFrameRegister(const MachineFunction &MF) const {
63119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
63219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
633894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!Subtarget.isPPC64())
63419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return TFI->hasFP(MF) ? PPC::R31 : PPC::R1;
635894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  else
63619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return TFI->hasFP(MF) ? PPC::X31 : PPC::X1;
637894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
638894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
639894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanunsigned PPCRegisterInfo::getEHExceptionRegister() const {
640894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return !Subtarget.isPPC64() ? PPC::R3 : PPC::X3;
641894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
642894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
643894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanunsigned PPCRegisterInfo::getEHHandlerRegister() const {
644894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return !Subtarget.isPPC64() ? PPC::R4 : PPC::X4;
645894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
646