Nate Begeman | 21e463b | 2005-10-16 05:39:50 +0000 | [diff] [blame] | 1 | //===- PPCRegisterInfo.cpp - PowerPC Register Information -------*- C++ -*-===// |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 2 | // |
Misha Brukman | f2ccb77 | 2004-08-17 04:55:41 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 7 | // |
Misha Brukman | f2ccb77 | 2004-08-17 04:55:41 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Nate Begeman | 21e463b | 2005-10-16 05:39:50 +0000 | [diff] [blame] | 10 | // This file contains the PowerPC implementation of the MRegisterInfo class. |
Misha Brukman | f2ccb77 | 2004-08-17 04:55:41 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #define DEBUG_TYPE "reginfo" |
Chris Lattner | 2668959 | 2005-10-14 23:51:18 +0000 | [diff] [blame] | 15 | #include "PPC.h" |
Chris Lattner | 26bd0d4 | 2005-10-14 23:45:43 +0000 | [diff] [blame] | 16 | #include "PPCInstrBuilder.h" |
Chris Lattner | 16e71f2 | 2005-10-14 23:59:06 +0000 | [diff] [blame] | 17 | #include "PPCRegisterInfo.h" |
Misha Brukman | f2ccb77 | 2004-08-17 04:55:41 +0000 | [diff] [blame] | 18 | #include "llvm/Constants.h" |
| 19 | #include "llvm/Type.h" |
| 20 | #include "llvm/CodeGen/ValueTypes.h" |
| 21 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 22 | #include "llvm/CodeGen/MachineFunction.h" |
| 23 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 24 | #include "llvm/Target/TargetFrameInfo.h" |
| 25 | #include "llvm/Target/TargetMachine.h" |
| 26 | #include "llvm/Target/TargetOptions.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 27 | #include "llvm/Support/CommandLine.h" |
| 28 | #include "llvm/Support/Debug.h" |
Nate Begeman | ae232e7 | 2005-11-06 09:00:38 +0000 | [diff] [blame] | 29 | #include "llvm/Support/MathExtras.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/STLExtras.h" |
Misha Brukman | f2ccb77 | 2004-08-17 04:55:41 +0000 | [diff] [blame] | 31 | #include <cstdlib> |
| 32 | #include <iostream> |
| 33 | using namespace llvm; |
| 34 | |
Nate Begeman | 21e463b | 2005-10-16 05:39:50 +0000 | [diff] [blame] | 35 | PPCRegisterInfo::PPCRegisterInfo() |
Chris Lattner | 4c7b43b | 2005-10-14 23:37:35 +0000 | [diff] [blame] | 36 | : PPCGenRegisterInfo(PPC::ADJCALLSTACKDOWN, PPC::ADJCALLSTACKUP) { |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 37 | ImmToIdxMap[PPC::LD] = PPC::LDX; ImmToIdxMap[PPC::STD] = PPC::STDX; |
Misha Brukman | f2ccb77 | 2004-08-17 04:55:41 +0000 | [diff] [blame] | 38 | ImmToIdxMap[PPC::LBZ] = PPC::LBZX; ImmToIdxMap[PPC::STB] = PPC::STBX; |
| 39 | ImmToIdxMap[PPC::LHZ] = PPC::LHZX; ImmToIdxMap[PPC::LHA] = PPC::LHAX; |
| 40 | ImmToIdxMap[PPC::LWZ] = PPC::LWZX; ImmToIdxMap[PPC::LWA] = PPC::LWAX; |
| 41 | ImmToIdxMap[PPC::LFS] = PPC::LFSX; ImmToIdxMap[PPC::LFD] = PPC::LFDX; |
| 42 | ImmToIdxMap[PPC::STH] = PPC::STHX; ImmToIdxMap[PPC::STW] = PPC::STWX; |
| 43 | ImmToIdxMap[PPC::STFS] = PPC::STFSX; ImmToIdxMap[PPC::STFD] = PPC::STFDX; |
Nate Begeman | 1d9d742 | 2005-10-18 00:28:58 +0000 | [diff] [blame] | 44 | ImmToIdxMap[PPC::ADDI] = PPC::ADD4; |
Misha Brukman | f2ccb77 | 2004-08-17 04:55:41 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 47 | void |
Nate Begeman | 21e463b | 2005-10-16 05:39:50 +0000 | [diff] [blame] | 48 | PPCRegisterInfo::storeRegToStackSlot(MachineBasicBlock &MBB, |
| 49 | MachineBasicBlock::iterator MI, |
| 50 | unsigned SrcReg, int FrameIdx, |
| 51 | const TargetRegisterClass *RC) const { |
Misha Brukman | f2ccb77 | 2004-08-17 04:55:41 +0000 | [diff] [blame] | 52 | if (SrcReg == PPC::LR) { |
Chris Lattner | 3f852b4 | 2005-08-18 23:24:50 +0000 | [diff] [blame] | 53 | BuildMI(MBB, MI, PPC::MFLR, 1, PPC::R11); |
Chris Lattner | 919c032 | 2005-10-01 01:35:02 +0000 | [diff] [blame] | 54 | addFrameReference(BuildMI(MBB, MI, PPC::STW, 3).addReg(PPC::R11), FrameIdx); |
Nate Begeman | 1d9d742 | 2005-10-18 00:28:58 +0000 | [diff] [blame] | 55 | } else if (RC == PPC::CRRCRegisterClass) { |
Nate Begeman | 7af0248 | 2005-04-12 07:04:16 +0000 | [diff] [blame] | 56 | BuildMI(MBB, MI, PPC::MFCR, 0, PPC::R11); |
Chris Lattner | 919c032 | 2005-10-01 01:35:02 +0000 | [diff] [blame] | 57 | addFrameReference(BuildMI(MBB, MI, PPC::STW, 3).addReg(PPC::R11), FrameIdx); |
Nate Begeman | 1d9d742 | 2005-10-18 00:28:58 +0000 | [diff] [blame] | 58 | } else if (RC == PPC::GPRCRegisterClass) { |
Chris Lattner | 919c032 | 2005-10-01 01:35:02 +0000 | [diff] [blame] | 59 | addFrameReference(BuildMI(MBB, MI, PPC::STW, 3).addReg(SrcReg),FrameIdx); |
Nate Begeman | 1d9d742 | 2005-10-18 00:28:58 +0000 | [diff] [blame] | 60 | } else if (RC == PPC::G8RCRegisterClass) { |
| 61 | addFrameReference(BuildMI(MBB, MI, PPC::STD, 3).addReg(SrcReg),FrameIdx); |
| 62 | } else if (RC == PPC::F8RCRegisterClass) { |
Chris Lattner | 919c032 | 2005-10-01 01:35:02 +0000 | [diff] [blame] | 63 | addFrameReference(BuildMI(MBB, MI, PPC::STFD, 3).addReg(SrcReg),FrameIdx); |
Nate Begeman | 1d9d742 | 2005-10-18 00:28:58 +0000 | [diff] [blame] | 64 | } else if (RC == PPC::F4RCRegisterClass) { |
Chris Lattner | 919c032 | 2005-10-01 01:35:02 +0000 | [diff] [blame] | 65 | addFrameReference(BuildMI(MBB, MI, PPC::STFS, 3).addReg(SrcReg),FrameIdx); |
Misha Brukman | f2ccb77 | 2004-08-17 04:55:41 +0000 | [diff] [blame] | 66 | } else { |
Chris Lattner | 919c032 | 2005-10-01 01:35:02 +0000 | [diff] [blame] | 67 | assert(0 && "Unknown regclass!"); |
| 68 | abort(); |
Misha Brukman | f2ccb77 | 2004-08-17 04:55:41 +0000 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | |
| 72 | void |
Nate Begeman | 21e463b | 2005-10-16 05:39:50 +0000 | [diff] [blame] | 73 | PPCRegisterInfo::loadRegFromStackSlot(MachineBasicBlock &MBB, |
Misha Brukman | f2ccb77 | 2004-08-17 04:55:41 +0000 | [diff] [blame] | 74 | MachineBasicBlock::iterator MI, |
Chris Lattner | b48d2cf | 2005-09-30 01:31:52 +0000 | [diff] [blame] | 75 | unsigned DestReg, int FrameIdx, |
| 76 | const TargetRegisterClass *RC) const { |
Misha Brukman | f2ccb77 | 2004-08-17 04:55:41 +0000 | [diff] [blame] | 77 | if (DestReg == PPC::LR) { |
Chris Lattner | 919c032 | 2005-10-01 01:35:02 +0000 | [diff] [blame] | 78 | addFrameReference(BuildMI(MBB, MI, PPC::LWZ, 2, PPC::R11), FrameIdx); |
Misha Brukman | f2ccb77 | 2004-08-17 04:55:41 +0000 | [diff] [blame] | 79 | BuildMI(MBB, MI, PPC::MTLR, 1).addReg(PPC::R11); |
Nate Begeman | 1d9d742 | 2005-10-18 00:28:58 +0000 | [diff] [blame] | 80 | } else if (RC == PPC::CRRCRegisterClass) { |
Chris Lattner | 919c032 | 2005-10-01 01:35:02 +0000 | [diff] [blame] | 81 | addFrameReference(BuildMI(MBB, MI, PPC::LWZ, 2, PPC::R11), FrameIdx); |
Nate Begeman | 7af0248 | 2005-04-12 07:04:16 +0000 | [diff] [blame] | 82 | BuildMI(MBB, MI, PPC::MTCRF, 1, DestReg).addReg(PPC::R11); |
Nate Begeman | 1d9d742 | 2005-10-18 00:28:58 +0000 | [diff] [blame] | 83 | } else if (RC == PPC::GPRCRegisterClass) { |
Chris Lattner | 919c032 | 2005-10-01 01:35:02 +0000 | [diff] [blame] | 84 | addFrameReference(BuildMI(MBB, MI, PPC::LWZ, 2, DestReg), FrameIdx); |
Nate Begeman | 1d9d742 | 2005-10-18 00:28:58 +0000 | [diff] [blame] | 85 | } else if (RC == PPC::G8RCRegisterClass) { |
| 86 | addFrameReference(BuildMI(MBB, MI, PPC::LD, 2, DestReg), FrameIdx); |
| 87 | } else if (RC == PPC::F8RCRegisterClass) { |
Chris Lattner | 919c032 | 2005-10-01 01:35:02 +0000 | [diff] [blame] | 88 | addFrameReference(BuildMI(MBB, MI, PPC::LFD, 2, DestReg), FrameIdx); |
Nate Begeman | 1d9d742 | 2005-10-18 00:28:58 +0000 | [diff] [blame] | 89 | } else if (RC == PPC::F4RCRegisterClass) { |
Chris Lattner | 919c032 | 2005-10-01 01:35:02 +0000 | [diff] [blame] | 90 | addFrameReference(BuildMI(MBB, MI, PPC::LFS, 2, DestReg), FrameIdx); |
Misha Brukman | f2ccb77 | 2004-08-17 04:55:41 +0000 | [diff] [blame] | 91 | } else { |
Chris Lattner | 919c032 | 2005-10-01 01:35:02 +0000 | [diff] [blame] | 92 | assert(0 && "Unknown regclass!"); |
| 93 | abort(); |
Misha Brukman | f2ccb77 | 2004-08-17 04:55:41 +0000 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | |
Nate Begeman | 21e463b | 2005-10-16 05:39:50 +0000 | [diff] [blame] | 97 | void PPCRegisterInfo::copyRegToReg(MachineBasicBlock &MBB, |
| 98 | MachineBasicBlock::iterator MI, |
| 99 | unsigned DestReg, unsigned SrcReg, |
| 100 | const TargetRegisterClass *RC) const { |
Misha Brukman | f2ccb77 | 2004-08-17 04:55:41 +0000 | [diff] [blame] | 101 | MachineInstr *I; |
| 102 | |
Nate Begeman | 1d9d742 | 2005-10-18 00:28:58 +0000 | [diff] [blame] | 103 | if (RC == PPC::GPRCRegisterClass) { |
| 104 | BuildMI(MBB, MI, PPC::OR4, 2, DestReg).addReg(SrcReg).addReg(SrcReg); |
| 105 | } else if (RC == PPC::G8RCRegisterClass) { |
| 106 | BuildMI(MBB, MI, PPC::OR8, 2, DestReg).addReg(SrcReg).addReg(SrcReg); |
| 107 | } else if (RC == PPC::F4RCRegisterClass) { |
Chris Lattner | 919c032 | 2005-10-01 01:35:02 +0000 | [diff] [blame] | 108 | BuildMI(MBB, MI, PPC::FMRS, 1, DestReg).addReg(SrcReg); |
Nate Begeman | 1d9d742 | 2005-10-18 00:28:58 +0000 | [diff] [blame] | 109 | } else if (RC == PPC::F8RCRegisterClass) { |
Chris Lattner | 919c032 | 2005-10-01 01:35:02 +0000 | [diff] [blame] | 110 | BuildMI(MBB, MI, PPC::FMRD, 1, DestReg).addReg(SrcReg); |
Nate Begeman | 1d9d742 | 2005-10-18 00:28:58 +0000 | [diff] [blame] | 111 | } else if (RC == PPC::CRRCRegisterClass) { |
Nate Begeman | 7af0248 | 2005-04-12 07:04:16 +0000 | [diff] [blame] | 112 | BuildMI(MBB, MI, PPC::MCRF, 1, DestReg).addReg(SrcReg); |
| 113 | } else { |
Misha Brukman | f2ccb77 | 2004-08-17 04:55:41 +0000 | [diff] [blame] | 114 | std::cerr << "Attempt to copy register that is not GPR or FPR"; |
| 115 | abort(); |
| 116 | } |
| 117 | } |
| 118 | |
Nate Begeman | 21e463b | 2005-10-16 05:39:50 +0000 | [diff] [blame] | 119 | unsigned PPCRegisterInfo::isLoadFromStackSlot(MachineInstr *MI, |
| 120 | int &FrameIndex) const { |
Chris Lattner | 6a78c21 | 2005-09-17 01:03:26 +0000 | [diff] [blame] | 121 | switch (MI->getOpcode()) { |
| 122 | default: break; |
Nate Begeman | 1d9d742 | 2005-10-18 00:28:58 +0000 | [diff] [blame] | 123 | case PPC::LD: |
Chris Lattner | 6a78c21 | 2005-09-17 01:03:26 +0000 | [diff] [blame] | 124 | case PPC::LWZ: |
Chris Lattner | b91956e | 2005-10-01 23:02:40 +0000 | [diff] [blame] | 125 | case PPC::LFS: |
Chris Lattner | 6a78c21 | 2005-09-17 01:03:26 +0000 | [diff] [blame] | 126 | case PPC::LFD: |
| 127 | if (MI->getOperand(1).isImmediate() && !MI->getOperand(1).getImmedValue() && |
| 128 | MI->getOperand(2).isFrameIndex()) { |
| 129 | FrameIndex = MI->getOperand(2).getFrameIndex(); |
| 130 | return MI->getOperand(0).getReg(); |
| 131 | } |
| 132 | break; |
| 133 | } |
| 134 | return 0; |
| 135 | } |
| 136 | |
Chris Lattner | f38df04 | 2005-09-09 21:46:49 +0000 | [diff] [blame] | 137 | /// foldMemoryOperand - PowerPC (like most RISC's) can only fold spills into |
| 138 | /// copy instructions, turning them into load/store instructions. |
Nate Begeman | 21e463b | 2005-10-16 05:39:50 +0000 | [diff] [blame] | 139 | MachineInstr *PPCRegisterInfo::foldMemoryOperand(MachineInstr *MI, |
| 140 | unsigned OpNum, |
| 141 | int FrameIndex) const { |
Chris Lattner | f38df04 | 2005-09-09 21:46:49 +0000 | [diff] [blame] | 142 | // Make sure this is a reg-reg copy. Note that we can't handle MCRF, because |
| 143 | // it takes more than one instruction to store it. |
| 144 | unsigned Opc = MI->getOpcode(); |
| 145 | |
Nate Begeman | 1d9d742 | 2005-10-18 00:28:58 +0000 | [diff] [blame] | 146 | if ((Opc == PPC::OR4 && |
Chris Lattner | f38df04 | 2005-09-09 21:46:49 +0000 | [diff] [blame] | 147 | MI->getOperand(1).getReg() == MI->getOperand(2).getReg())) { |
| 148 | if (OpNum == 0) { // move -> store |
| 149 | unsigned InReg = MI->getOperand(1).getReg(); |
| 150 | return addFrameReference(BuildMI(PPC::STW, |
| 151 | 3).addReg(InReg), FrameIndex); |
Chris Lattner | c9fe750 | 2005-09-09 21:59:44 +0000 | [diff] [blame] | 152 | } else { // move -> load |
Chris Lattner | f38df04 | 2005-09-09 21:46:49 +0000 | [diff] [blame] | 153 | unsigned OutReg = MI->getOperand(0).getReg(); |
| 154 | return addFrameReference(BuildMI(PPC::LWZ, 2, OutReg), FrameIndex); |
| 155 | } |
Nate Begeman | 1d9d742 | 2005-10-18 00:28:58 +0000 | [diff] [blame] | 156 | } else if ((Opc == PPC::OR8 && |
| 157 | MI->getOperand(1).getReg() == MI->getOperand(2).getReg())) { |
| 158 | if (OpNum == 0) { // move -> store |
| 159 | unsigned InReg = MI->getOperand(1).getReg(); |
| 160 | return addFrameReference(BuildMI(PPC::STD, |
| 161 | 3).addReg(InReg), FrameIndex); |
| 162 | } else { // move -> load |
| 163 | unsigned OutReg = MI->getOperand(0).getReg(); |
| 164 | return addFrameReference(BuildMI(PPC::LD, 2, OutReg), FrameIndex); |
| 165 | } |
Chris Lattner | 919c032 | 2005-10-01 01:35:02 +0000 | [diff] [blame] | 166 | } else if (Opc == PPC::FMRD) { |
Chris Lattner | c9fe750 | 2005-09-09 21:59:44 +0000 | [diff] [blame] | 167 | if (OpNum == 0) { // move -> store |
| 168 | unsigned InReg = MI->getOperand(1).getReg(); |
| 169 | return addFrameReference(BuildMI(PPC::STFD, |
| 170 | 3).addReg(InReg), FrameIndex); |
| 171 | } else { // move -> load |
| 172 | unsigned OutReg = MI->getOperand(0).getReg(); |
| 173 | return addFrameReference(BuildMI(PPC::LFD, 2, OutReg), FrameIndex); |
| 174 | } |
Chris Lattner | 919c032 | 2005-10-01 01:35:02 +0000 | [diff] [blame] | 175 | } else if (Opc == PPC::FMRS) { |
| 176 | if (OpNum == 0) { // move -> store |
| 177 | unsigned InReg = MI->getOperand(1).getReg(); |
| 178 | return addFrameReference(BuildMI(PPC::STFS, |
| 179 | 3).addReg(InReg), FrameIndex); |
| 180 | } else { // move -> load |
| 181 | unsigned OutReg = MI->getOperand(0).getReg(); |
| 182 | return addFrameReference(BuildMI(PPC::LFS, 2, OutReg), FrameIndex); |
| 183 | } |
Chris Lattner | f38df04 | 2005-09-09 21:46:49 +0000 | [diff] [blame] | 184 | } |
| 185 | return 0; |
| 186 | } |
| 187 | |
Misha Brukman | f2ccb77 | 2004-08-17 04:55:41 +0000 | [diff] [blame] | 188 | //===----------------------------------------------------------------------===// |
| 189 | // Stack Frame Processing methods |
| 190 | //===----------------------------------------------------------------------===// |
| 191 | |
| 192 | // hasFP - Return true if the specified function should have a dedicated frame |
| 193 | // pointer register. This is true if the function has variable sized allocas or |
| 194 | // if frame pointer elimination is disabled. |
| 195 | // |
| 196 | static bool hasFP(MachineFunction &MF) { |
Chris Lattner | 5087daa | 2005-08-26 21:49:18 +0000 | [diff] [blame] | 197 | return NoFramePointerElim || MF.getFrameInfo()->hasVarSizedObjects(); |
Misha Brukman | f2ccb77 | 2004-08-17 04:55:41 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Nate Begeman | 21e463b | 2005-10-16 05:39:50 +0000 | [diff] [blame] | 200 | void PPCRegisterInfo:: |
Misha Brukman | f2ccb77 | 2004-08-17 04:55:41 +0000 | [diff] [blame] | 201 | eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB, |
| 202 | MachineBasicBlock::iterator I) const { |
| 203 | if (hasFP(MF)) { |
| 204 | // If we have a frame pointer, convert as follows: |
| 205 | // ADJCALLSTACKDOWN -> addi, r1, r1, -amount |
| 206 | // ADJCALLSTACKUP -> addi, r1, r1, amount |
| 207 | MachineInstr *Old = I; |
| 208 | unsigned Amount = Old->getOperand(0).getImmedValue(); |
| 209 | if (Amount != 0) { |
| 210 | // We need to keep the stack aligned properly. To do this, we round the |
| 211 | // amount of space needed for the outgoing arguments up to the next |
| 212 | // alignment boundary. |
| 213 | unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment(); |
| 214 | Amount = (Amount+Align-1)/Align*Align; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 215 | |
Misha Brukman | f2ccb77 | 2004-08-17 04:55:41 +0000 | [diff] [blame] | 216 | // Replace the pseudo instruction with a new instruction... |
| 217 | if (Old->getOpcode() == PPC::ADJCALLSTACKDOWN) { |
| 218 | MBB.insert(I, BuildMI(PPC::ADDI, 2, PPC::R1).addReg(PPC::R1) |
| 219 | .addSImm(-Amount)); |
| 220 | } else { |
| 221 | assert(Old->getOpcode() == PPC::ADJCALLSTACKUP); |
| 222 | MBB.insert(I, BuildMI(PPC::ADDI, 2, PPC::R1).addReg(PPC::R1) |
| 223 | .addSImm(Amount)); |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | MBB.erase(I); |
| 228 | } |
| 229 | |
| 230 | void |
Nate Begeman | 21e463b | 2005-10-16 05:39:50 +0000 | [diff] [blame] | 231 | PPCRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II) const { |
Misha Brukman | f2ccb77 | 2004-08-17 04:55:41 +0000 | [diff] [blame] | 232 | unsigned i = 0; |
| 233 | MachineInstr &MI = *II; |
| 234 | MachineBasicBlock &MBB = *MI.getParent(); |
| 235 | MachineFunction &MF = *MBB.getParent(); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 236 | |
Misha Brukman | f2ccb77 | 2004-08-17 04:55:41 +0000 | [diff] [blame] | 237 | while (!MI.getOperand(i).isFrameIndex()) { |
| 238 | ++i; |
| 239 | assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!"); |
| 240 | } |
| 241 | |
| 242 | int FrameIndex = MI.getOperand(i).getFrameIndex(); |
| 243 | |
| 244 | // Replace the FrameIndex with base register with GPR1 (SP) or GPR31 (FP). |
| 245 | MI.SetMachineOperandReg(i, hasFP(MF) ? PPC::R31 : PPC::R1); |
| 246 | |
| 247 | // Take into account whether it's an add or mem instruction |
| 248 | unsigned OffIdx = (i == 2) ? 1 : 2; |
| 249 | |
| 250 | // Now add the frame object offset to the offset from r1. |
| 251 | int Offset = MF.getFrameInfo()->getObjectOffset(FrameIndex) + |
| 252 | MI.getOperand(OffIdx).getImmedValue(); |
| 253 | |
| 254 | // If we're not using a Frame Pointer that has been set to the value of the |
| 255 | // SP before having the stack size subtracted from it, then add the stack size |
| 256 | // to Offset to get the correct offset. |
| 257 | Offset += MF.getFrameInfo()->getStackSize(); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 258 | |
Misha Brukman | f2ccb77 | 2004-08-17 04:55:41 +0000 | [diff] [blame] | 259 | if (Offset > 32767 || Offset < -32768) { |
| 260 | // Insert a set of r0 with the full offset value before the ld, st, or add |
| 261 | MachineBasicBlock *MBB = MI.getParent(); |
| 262 | MBB->insert(II, BuildMI(PPC::LIS, 1, PPC::R0).addSImm(Offset >> 16)); |
| 263 | MBB->insert(II, BuildMI(PPC::ORI, 2, PPC::R0).addReg(PPC::R0) |
| 264 | .addImm(Offset)); |
| 265 | // convert into indexed form of the instruction |
| 266 | // sth 0:rA, 1:imm 2:(rB) ==> sthx 0:rA, 2:rB, 1:r0 |
| 267 | // addi 0:rA 1:rB, 2, imm ==> add 0:rA, 1:rB, 2:r0 |
Chris Lattner | 1463019 | 2005-09-09 20:51:08 +0000 | [diff] [blame] | 268 | assert(ImmToIdxMap.count(MI.getOpcode()) && |
| 269 | "No indexed form of load or store available!"); |
| 270 | unsigned NewOpcode = ImmToIdxMap.find(MI.getOpcode())->second; |
Misha Brukman | f2ccb77 | 2004-08-17 04:55:41 +0000 | [diff] [blame] | 271 | MI.setOpcode(NewOpcode); |
| 272 | MI.SetMachineOperandReg(1, MI.getOperand(i).getReg()); |
| 273 | MI.SetMachineOperandReg(2, PPC::R0); |
| 274 | } else { |
Chris Lattner | 841d12d | 2005-10-18 16:51:22 +0000 | [diff] [blame] | 275 | switch (MI.getOpcode()) { |
| 276 | case PPC::LWA: |
| 277 | case PPC::LD: |
| 278 | case PPC::STD: |
| 279 | case PPC::STDU: |
| 280 | assert((Offset & 3) == 0 && "Invalid frame offset!"); |
| 281 | Offset >>= 2; // The actual encoded value has the low two bits zero. |
| 282 | break; |
| 283 | } |
Chris Lattner | 919c032 | 2005-10-01 01:35:02 +0000 | [diff] [blame] | 284 | MI.SetMachineOperandConst(OffIdx, MachineOperand::MO_SignExtendedImmed, |
| 285 | Offset); |
Misha Brukman | f2ccb77 | 2004-08-17 04:55:41 +0000 | [diff] [blame] | 286 | } |
| 287 | } |
| 288 | |
| 289 | |
Nate Begeman | 21e463b | 2005-10-16 05:39:50 +0000 | [diff] [blame] | 290 | void PPCRegisterInfo::emitPrologue(MachineFunction &MF) const { |
Misha Brukman | f2ccb77 | 2004-08-17 04:55:41 +0000 | [diff] [blame] | 291 | MachineBasicBlock &MBB = MF.front(); // Prolog goes in entry BB |
| 292 | MachineBasicBlock::iterator MBBI = MBB.begin(); |
| 293 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 294 | MachineInstr *MI; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 295 | |
Misha Brukman | f2ccb77 | 2004-08-17 04:55:41 +0000 | [diff] [blame] | 296 | // Get the number of bytes to allocate from the FrameInfo |
| 297 | unsigned NumBytes = MFI->getStackSize(); |
Nate Begeman | ae232e7 | 2005-11-06 09:00:38 +0000 | [diff] [blame] | 298 | |
| 299 | // Get the alignments provided by the target, and the maximum alignment |
| 300 | // (if any) of the fixed frame objects. |
| 301 | unsigned TargetAlign = MF.getTarget().getFrameInfo()->getStackAlignment(); |
| 302 | unsigned MaxAlign = MFI->getMaxAlignment(); |
Misha Brukman | f2ccb77 | 2004-08-17 04:55:41 +0000 | [diff] [blame] | 303 | |
| 304 | // If we have calls, we cannot use the red zone to store callee save registers |
| 305 | // and we must set up a stack frame, so calculate the necessary size here. |
| 306 | if (MFI->hasCalls()) { |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 307 | // We reserve argument space for call sites in the function immediately on |
| 308 | // entry to the current function. This eliminates the need for add/sub |
Misha Brukman | f2ccb77 | 2004-08-17 04:55:41 +0000 | [diff] [blame] | 309 | // brackets around call sites. |
| 310 | NumBytes += MFI->getMaxCallFrameSize(); |
| 311 | } |
| 312 | |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 313 | // If we are a leaf function, and use up to 224 bytes of stack space, |
Nate Begeman | 54eed36 | 2005-07-27 06:06:29 +0000 | [diff] [blame] | 314 | // and don't have a frame pointer, then we do not need to adjust the stack |
| 315 | // pointer (we fit in the Red Zone). |
Nate Begeman | ae232e7 | 2005-11-06 09:00:38 +0000 | [diff] [blame] | 316 | if ((NumBytes == 0) || (NumBytes <= 224 && !hasFP(MF) && !MFI->hasCalls() && |
| 317 | MaxAlign <= TargetAlign)) { |
Nate Begeman | 54eed36 | 2005-07-27 06:06:29 +0000 | [diff] [blame] | 318 | MFI->setStackSize(0); |
| 319 | return; |
| 320 | } |
Misha Brukman | f2ccb77 | 2004-08-17 04:55:41 +0000 | [diff] [blame] | 321 | |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 322 | // Add the size of R1 to NumBytes size for the store of R1 to the bottom |
Misha Brukman | f2ccb77 | 2004-08-17 04:55:41 +0000 | [diff] [blame] | 323 | // of the stack and round the size to a multiple of the alignment. |
Nate Begeman | ae232e7 | 2005-11-06 09:00:38 +0000 | [diff] [blame] | 324 | unsigned Align = std::max(TargetAlign, MaxAlign); |
Chris Lattner | 5802be1 | 2005-09-30 17:16:59 +0000 | [diff] [blame] | 325 | unsigned GPRSize = 4; |
Chris Lattner | 8edcd84 | 2004-08-21 19:51:17 +0000 | [diff] [blame] | 326 | unsigned Size = hasFP(MF) ? GPRSize + GPRSize : GPRSize; |
Misha Brukman | f2ccb77 | 2004-08-17 04:55:41 +0000 | [diff] [blame] | 327 | NumBytes = (NumBytes+Size+Align-1)/Align*Align; |
| 328 | |
| 329 | // Update frame info to pretend that this is part of the stack... |
| 330 | MFI->setStackSize(NumBytes); |
| 331 | |
Nate Begeman | 3dee175 | 2005-07-27 23:11:27 +0000 | [diff] [blame] | 332 | // Adjust stack pointer: r1 -= numbytes. |
Misha Brukman | f2ccb77 | 2004-08-17 04:55:41 +0000 | [diff] [blame] | 333 | if (NumBytes <= 32768) { |
| 334 | MI=BuildMI(PPC::STWU,3).addReg(PPC::R1).addSImm(-NumBytes).addReg(PPC::R1); |
| 335 | MBB.insert(MBBI, MI); |
| 336 | } else { |
| 337 | int NegNumbytes = -NumBytes; |
| 338 | MI = BuildMI(PPC::LIS, 1, PPC::R0).addSImm(NegNumbytes >> 16); |
| 339 | MBB.insert(MBBI, MI); |
| 340 | MI = BuildMI(PPC::ORI, 2, PPC::R0).addReg(PPC::R0) |
| 341 | .addImm(NegNumbytes & 0xFFFF); |
| 342 | MBB.insert(MBBI, MI); |
| 343 | MI = BuildMI(PPC::STWUX, 3).addReg(PPC::R1).addReg(PPC::R1).addReg(PPC::R0); |
| 344 | MBB.insert(MBBI, MI); |
| 345 | } |
Nate Begeman | ae232e7 | 2005-11-06 09:00:38 +0000 | [diff] [blame] | 346 | |
| 347 | // If there is a preferred stack alignment, align R1 now |
| 348 | // FIXME: If this ever matters, this could be made more efficient by folding |
| 349 | // this into the code above, so that we don't issue two store+update |
| 350 | // instructions. |
| 351 | if (MaxAlign > TargetAlign) { |
| 352 | assert(isPowerOf2_32(MaxAlign) && MaxAlign < 32767 && "Invalid alignment!"); |
| 353 | MI = BuildMI(PPC::RLWINM, 4, PPC::R0).addReg(PPC::R1).addImm(0) |
| 354 | .addImm(32-Log2_32(MaxAlign)).addImm(31); |
| 355 | MBB.insert(MBBI, MI); |
| 356 | MI = BuildMI(PPC::SUBFIC, 2, PPC::R0).addReg(PPC::R0).addImm(MaxAlign); |
| 357 | MBB.insert(MBBI, MI); |
| 358 | MI = BuildMI(PPC::STWUX, 3).addReg(PPC::R1).addReg(PPC::R1).addReg(PPC::R0); |
| 359 | MBB.insert(MBBI, MI); |
| 360 | } |
| 361 | |
| 362 | // If there is a frame pointer, copy R1 (SP) into R31 (FP) |
Misha Brukman | f2ccb77 | 2004-08-17 04:55:41 +0000 | [diff] [blame] | 363 | if (hasFP(MF)) { |
Chris Lattner | 8edcd84 | 2004-08-21 19:51:17 +0000 | [diff] [blame] | 364 | MI = BuildMI(PPC::STW, 3).addReg(PPC::R31).addSImm(GPRSize).addReg(PPC::R1); |
Misha Brukman | f2ccb77 | 2004-08-17 04:55:41 +0000 | [diff] [blame] | 365 | MBB.insert(MBBI, MI); |
Nate Begeman | 1d9d742 | 2005-10-18 00:28:58 +0000 | [diff] [blame] | 366 | MI = BuildMI(PPC::OR4, 2, PPC::R31).addReg(PPC::R1).addReg(PPC::R1); |
Misha Brukman | f2ccb77 | 2004-08-17 04:55:41 +0000 | [diff] [blame] | 367 | MBB.insert(MBBI, MI); |
| 368 | } |
| 369 | } |
| 370 | |
Nate Begeman | 21e463b | 2005-10-16 05:39:50 +0000 | [diff] [blame] | 371 | void PPCRegisterInfo::emitEpilogue(MachineFunction &MF, |
| 372 | MachineBasicBlock &MBB) const { |
Misha Brukman | f2ccb77 | 2004-08-17 04:55:41 +0000 | [diff] [blame] | 373 | const MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 374 | MachineBasicBlock::iterator MBBI = prior(MBB.end()); |
| 375 | MachineInstr *MI; |
Evan Cheng | 6da8d99 | 2006-01-09 18:28:21 +0000 | [diff] [blame^] | 376 | assert(MBBI->getOpcode() == PPC::BLR && |
Misha Brukman | f2ccb77 | 2004-08-17 04:55:41 +0000 | [diff] [blame] | 377 | "Can only insert epilog into returning blocks"); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 378 | |
Misha Brukman | f2ccb77 | 2004-08-17 04:55:41 +0000 | [diff] [blame] | 379 | // Get the number of bytes allocated from the FrameInfo... |
| 380 | unsigned NumBytes = MFI->getStackSize(); |
Chris Lattner | 5802be1 | 2005-09-30 17:16:59 +0000 | [diff] [blame] | 381 | unsigned GPRSize = 4; |
Misha Brukman | f2ccb77 | 2004-08-17 04:55:41 +0000 | [diff] [blame] | 382 | |
| 383 | if (NumBytes != 0) { |
| 384 | if (hasFP(MF)) { |
Nate Begeman | 1f4a132 | 2004-08-22 08:09:17 +0000 | [diff] [blame] | 385 | MI = BuildMI(PPC::LWZ, 2, PPC::R31).addSImm(GPRSize).addReg(PPC::R31); |
Misha Brukman | f2ccb77 | 2004-08-17 04:55:41 +0000 | [diff] [blame] | 386 | MBB.insert(MBBI, MI); |
| 387 | } |
| 388 | MI = BuildMI(PPC::LWZ, 2, PPC::R1).addSImm(0).addReg(PPC::R1); |
| 389 | MBB.insert(MBBI, MI); |
| 390 | } |
| 391 | } |
| 392 | |
Chris Lattner | 4c7b43b | 2005-10-14 23:37:35 +0000 | [diff] [blame] | 393 | #include "PPCGenRegisterInfo.inc" |
Misha Brukman | f2ccb77 | 2004-08-17 04:55:41 +0000 | [diff] [blame] | 394 | |