blob: 87f7b8a3840758714f0a2d7dc3e9fe7c1aa36040 [file] [log] [blame]
Nate Begeman21e463b2005-10-16 05:39:50 +00001//===- PPCInstrInfo.cpp - PowerPC32 Instruction Information -----*- C++ -*-===//
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002//
Misha Brukmanf2ccb772004-08-17 04:55:41 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00007//
Misha Brukmanf2ccb772004-08-17 04:55:41 +00008//===----------------------------------------------------------------------===//
9//
10// This file contains the PowerPC implementation of the TargetInstrInfo class.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner16e71f22005-10-14 23:59:06 +000014#include "PPCInstrInfo.h"
Owen Andersonf6372aa2008-01-01 21:11:32 +000015#include "PPCInstrBuilder.h"
Bill Wendling7194aaf2008-03-03 22:19:16 +000016#include "PPCMachineFunctionInfo.h"
Chris Lattnerdf4ed632006-11-17 22:10:59 +000017#include "PPCPredicates.h"
Chris Lattner4c7b43b2005-10-14 23:37:35 +000018#include "PPCGenInstrInfo.inc"
Chris Lattnerb1d26f62006-06-17 00:01:04 +000019#include "PPCTargetMachine.h"
Owen Anderson718cb662007-09-07 04:06:50 +000020#include "llvm/ADT/STLExtras.h"
Misha Brukmanf2ccb772004-08-17 04:55:41 +000021#include "llvm/CodeGen/MachineInstrBuilder.h"
Bill Wendling880d0f62008-03-04 23:13:51 +000022#include "llvm/Support/CommandLine.h"
Nicolas Geoffray52e724a2008-04-16 20:10:13 +000023#include "llvm/Target/TargetAsmInfo.h"
Misha Brukmanf2ccb772004-08-17 04:55:41 +000024using namespace llvm;
25
Bill Wendling4a66e9a2008-03-10 22:49:16 +000026extern cl::opt<bool> EnablePPC32RS; // FIXME (64-bit): See PPCRegisterInfo.cpp.
27extern cl::opt<bool> EnablePPC64RS; // FIXME (64-bit): See PPCRegisterInfo.cpp.
Bill Wendling880d0f62008-03-04 23:13:51 +000028
Chris Lattnerb1d26f62006-06-17 00:01:04 +000029PPCInstrInfo::PPCInstrInfo(PPCTargetMachine &tm)
Chris Lattner64105522008-01-01 01:03:04 +000030 : TargetInstrInfoImpl(PPCInsts, array_lengthof(PPCInsts)), TM(tm),
Evan Cheng7ce45782006-11-13 23:36:35 +000031 RI(*TM.getSubtargetImpl(), *this) {}
Chris Lattnerb1d26f62006-06-17 00:01:04 +000032
33/// getPointerRegClass - Return the register class to use to hold pointers.
34/// This is used for addressing modes.
35const TargetRegisterClass *PPCInstrInfo::getPointerRegClass() const {
36 if (TM.getSubtargetImpl()->isPPC64())
37 return &PPC::G8RCRegClass;
38 else
39 return &PPC::GPRCRegClass;
40}
41
Misha Brukmanf2ccb772004-08-17 04:55:41 +000042
Nate Begeman21e463b2005-10-16 05:39:50 +000043bool PPCInstrInfo::isMoveInstr(const MachineInstr& MI,
44 unsigned& sourceReg,
45 unsigned& destReg) const {
Chris Lattnercc8cd0c2008-01-07 02:48:55 +000046 unsigned oc = MI.getOpcode();
Chris Lattnerb410dc92006-06-20 23:18:58 +000047 if (oc == PPC::OR || oc == PPC::OR8 || oc == PPC::VOR ||
Chris Lattner14c09b82005-10-19 01:50:36 +000048 oc == PPC::OR4To8 || oc == PPC::OR8To4) { // or r1, r2, r2
Evan Cheng1e3417292007-04-25 07:12:14 +000049 assert(MI.getNumOperands() >= 3 &&
Dan Gohmand735b802008-10-03 15:45:36 +000050 MI.getOperand(0).isReg() &&
51 MI.getOperand(1).isReg() &&
52 MI.getOperand(2).isReg() &&
Misha Brukmanf2ccb772004-08-17 04:55:41 +000053 "invalid PPC OR instruction!");
54 if (MI.getOperand(1).getReg() == MI.getOperand(2).getReg()) {
55 sourceReg = MI.getOperand(1).getReg();
56 destReg = MI.getOperand(0).getReg();
57 return true;
58 }
59 } else if (oc == PPC::ADDI) { // addi r1, r2, 0
Evan Cheng1e3417292007-04-25 07:12:14 +000060 assert(MI.getNumOperands() >= 3 &&
Dan Gohmand735b802008-10-03 15:45:36 +000061 MI.getOperand(0).isReg() &&
62 MI.getOperand(2).isImm() &&
Misha Brukmanf2ccb772004-08-17 04:55:41 +000063 "invalid PPC ADDI instruction!");
Dan Gohmand735b802008-10-03 15:45:36 +000064 if (MI.getOperand(1).isReg() && MI.getOperand(2).getImm() == 0) {
Misha Brukmanf2ccb772004-08-17 04:55:41 +000065 sourceReg = MI.getOperand(1).getReg();
66 destReg = MI.getOperand(0).getReg();
67 return true;
68 }
Nate Begemancb90de32004-10-07 22:26:12 +000069 } else if (oc == PPC::ORI) { // ori r1, r2, 0
Evan Cheng1e3417292007-04-25 07:12:14 +000070 assert(MI.getNumOperands() >= 3 &&
Dan Gohmand735b802008-10-03 15:45:36 +000071 MI.getOperand(0).isReg() &&
72 MI.getOperand(1).isReg() &&
73 MI.getOperand(2).isImm() &&
Nate Begemancb90de32004-10-07 22:26:12 +000074 "invalid PPC ORI instruction!");
Chris Lattner9a1ceae2007-12-30 20:49:49 +000075 if (MI.getOperand(2).getImm() == 0) {
Nate Begemancb90de32004-10-07 22:26:12 +000076 sourceReg = MI.getOperand(1).getReg();
77 destReg = MI.getOperand(0).getReg();
78 return true;
79 }
Chris Lattnereb5d47d2005-10-07 05:00:52 +000080 } else if (oc == PPC::FMRS || oc == PPC::FMRD ||
81 oc == PPC::FMRSD) { // fmr r1, r2
Evan Cheng1e3417292007-04-25 07:12:14 +000082 assert(MI.getNumOperands() >= 2 &&
Dan Gohmand735b802008-10-03 15:45:36 +000083 MI.getOperand(0).isReg() &&
84 MI.getOperand(1).isReg() &&
Misha Brukmanf2ccb772004-08-17 04:55:41 +000085 "invalid PPC FMR instruction");
86 sourceReg = MI.getOperand(1).getReg();
87 destReg = MI.getOperand(0).getReg();
88 return true;
Nate Begeman7af02482005-04-12 07:04:16 +000089 } else if (oc == PPC::MCRF) { // mcrf cr1, cr2
Evan Cheng1e3417292007-04-25 07:12:14 +000090 assert(MI.getNumOperands() >= 2 &&
Dan Gohmand735b802008-10-03 15:45:36 +000091 MI.getOperand(0).isReg() &&
92 MI.getOperand(1).isReg() &&
Nate Begeman7af02482005-04-12 07:04:16 +000093 "invalid PPC MCRF instruction");
94 sourceReg = MI.getOperand(1).getReg();
95 destReg = MI.getOperand(0).getReg();
96 return true;
Misha Brukmanf2ccb772004-08-17 04:55:41 +000097 }
98 return false;
99}
Chris Lattner043870d2005-09-09 18:17:41 +0000100
Dan Gohmancbad42c2008-11-18 19:49:32 +0000101unsigned PPCInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
Chris Lattner9c09c9e2006-03-16 22:24:02 +0000102 int &FrameIndex) const {
Chris Lattner40839602006-02-02 20:12:32 +0000103 switch (MI->getOpcode()) {
104 default: break;
105 case PPC::LD:
106 case PPC::LWZ:
107 case PPC::LFS:
108 case PPC::LFD:
Dan Gohmand735b802008-10-03 15:45:36 +0000109 if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
110 MI->getOperand(2).isFI()) {
Chris Lattner8aa797a2007-12-30 23:10:15 +0000111 FrameIndex = MI->getOperand(2).getIndex();
Chris Lattner40839602006-02-02 20:12:32 +0000112 return MI->getOperand(0).getReg();
113 }
114 break;
115 }
116 return 0;
Chris Lattner65242872006-02-02 20:16:12 +0000117}
Chris Lattner40839602006-02-02 20:12:32 +0000118
Dan Gohmancbad42c2008-11-18 19:49:32 +0000119unsigned PPCInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
Chris Lattner65242872006-02-02 20:16:12 +0000120 int &FrameIndex) const {
121 switch (MI->getOpcode()) {
122 default: break;
Nate Begeman3b478b32006-02-02 21:07:50 +0000123 case PPC::STD:
Chris Lattner65242872006-02-02 20:16:12 +0000124 case PPC::STW:
125 case PPC::STFS:
126 case PPC::STFD:
Dan Gohmand735b802008-10-03 15:45:36 +0000127 if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
128 MI->getOperand(2).isFI()) {
Chris Lattner8aa797a2007-12-30 23:10:15 +0000129 FrameIndex = MI->getOperand(2).getIndex();
Chris Lattner65242872006-02-02 20:16:12 +0000130 return MI->getOperand(0).getReg();
131 }
132 break;
133 }
134 return 0;
135}
Chris Lattner40839602006-02-02 20:12:32 +0000136
Chris Lattner043870d2005-09-09 18:17:41 +0000137// commuteInstruction - We can commute rlwimi instructions, but only if the
138// rotate amt is zero. We also have to munge the immediates a bit.
Evan Cheng58dcb0e2008-06-16 07:33:11 +0000139MachineInstr *
140PPCInstrInfo::commuteInstruction(MachineInstr *MI, bool NewMI) const {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000141 MachineFunction &MF = *MI->getParent()->getParent();
142
Chris Lattner043870d2005-09-09 18:17:41 +0000143 // Normal instructions can be commuted the obvious way.
144 if (MI->getOpcode() != PPC::RLWIMI)
Evan Cheng58dcb0e2008-06-16 07:33:11 +0000145 return TargetInstrInfoImpl::commuteInstruction(MI, NewMI);
Chris Lattner043870d2005-09-09 18:17:41 +0000146
147 // Cannot commute if it has a non-zero rotate count.
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000148 if (MI->getOperand(3).getImm() != 0)
Chris Lattner043870d2005-09-09 18:17:41 +0000149 return 0;
150
151 // If we have a zero rotate count, we have:
152 // M = mask(MB,ME)
153 // Op0 = (Op1 & ~M) | (Op2 & M)
154 // Change this to:
155 // M = mask((ME+1)&31, (MB-1)&31)
156 // Op0 = (Op2 & ~M) | (Op1 & M)
157
158 // Swap op1/op2
Evan Chenga4d16a12008-02-13 02:46:49 +0000159 unsigned Reg0 = MI->getOperand(0).getReg();
Chris Lattner043870d2005-09-09 18:17:41 +0000160 unsigned Reg1 = MI->getOperand(1).getReg();
161 unsigned Reg2 = MI->getOperand(2).getReg();
Evan Cheng6ce7dc22006-11-15 20:58:11 +0000162 bool Reg1IsKill = MI->getOperand(1).isKill();
163 bool Reg2IsKill = MI->getOperand(2).isKill();
Evan Cheng58dcb0e2008-06-16 07:33:11 +0000164 bool ChangeReg0 = false;
Evan Chenga4d16a12008-02-13 02:46:49 +0000165 // If machine instrs are no longer in two-address forms, update
166 // destination register as well.
167 if (Reg0 == Reg1) {
168 // Must be two address instruction!
169 assert(MI->getDesc().getOperandConstraint(0, TOI::TIED_TO) &&
170 "Expecting a two-address instruction!");
Evan Chenga4d16a12008-02-13 02:46:49 +0000171 Reg2IsKill = false;
Evan Cheng58dcb0e2008-06-16 07:33:11 +0000172 ChangeReg0 = true;
Evan Chenga4d16a12008-02-13 02:46:49 +0000173 }
Evan Cheng58dcb0e2008-06-16 07:33:11 +0000174
175 // Masks.
176 unsigned MB = MI->getOperand(4).getImm();
177 unsigned ME = MI->getOperand(5).getImm();
178
179 if (NewMI) {
180 // Create a new instruction.
181 unsigned Reg0 = ChangeReg0 ? Reg2 : MI->getOperand(0).getReg();
182 bool Reg0IsDead = MI->getOperand(0).isDead();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000183 return BuildMI(MF, MI->getDesc())
184 .addReg(Reg0, true, false, false, Reg0IsDead)
Evan Cheng58dcb0e2008-06-16 07:33:11 +0000185 .addReg(Reg2, false, false, Reg2IsKill)
186 .addReg(Reg1, false, false, Reg1IsKill)
187 .addImm((ME+1) & 31)
188 .addImm((MB-1) & 31);
189 }
190
191 if (ChangeReg0)
192 MI->getOperand(0).setReg(Reg2);
Chris Lattnere53f4a02006-05-04 17:52:23 +0000193 MI->getOperand(2).setReg(Reg1);
194 MI->getOperand(1).setReg(Reg2);
Chris Lattnerf7382302007-12-30 21:56:09 +0000195 MI->getOperand(2).setIsKill(Reg1IsKill);
196 MI->getOperand(1).setIsKill(Reg2IsKill);
Chris Lattner043870d2005-09-09 18:17:41 +0000197
198 // Swap the mask around.
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000199 MI->getOperand(4).setImm((ME+1) & 31);
200 MI->getOperand(5).setImm((MB-1) & 31);
Chris Lattner043870d2005-09-09 18:17:41 +0000201 return MI;
202}
Chris Lattnerbbf1c722006-03-05 23:49:55 +0000203
204void PPCInstrInfo::insertNoop(MachineBasicBlock &MBB,
205 MachineBasicBlock::iterator MI) const {
Evan Chengc0f64ff2006-11-27 23:37:22 +0000206 BuildMI(MBB, MI, get(PPC::NOP));
Chris Lattnerbbf1c722006-03-05 23:49:55 +0000207}
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000208
209
210// Branch analysis.
211bool PPCInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
212 MachineBasicBlock *&FBB,
Owen Anderson44eb65c2008-08-14 22:49:33 +0000213 SmallVectorImpl<MachineOperand> &Cond) const {
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000214 // If the block has no terminators, it just falls into the block after it.
215 MachineBasicBlock::iterator I = MBB.end();
Evan Chengbfd2ec42007-06-08 21:59:56 +0000216 if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000217 return false;
218
219 // Get the last instruction in the block.
220 MachineInstr *LastInst = I;
221
222 // If there is only one terminator instruction, process it.
Evan Chengbfd2ec42007-06-08 21:59:56 +0000223 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000224 if (LastInst->getOpcode() == PPC::B) {
Chris Lattner8aa797a2007-12-30 23:10:15 +0000225 TBB = LastInst->getOperand(0).getMBB();
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000226 return false;
Chris Lattner289c2d52006-11-17 22:14:47 +0000227 } else if (LastInst->getOpcode() == PPC::BCC) {
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000228 // Block ends with fall-through condbranch.
Chris Lattner8aa797a2007-12-30 23:10:15 +0000229 TBB = LastInst->getOperand(2).getMBB();
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000230 Cond.push_back(LastInst->getOperand(0));
231 Cond.push_back(LastInst->getOperand(1));
Chris Lattner7c4fe252006-10-21 06:03:11 +0000232 return false;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000233 }
234 // Otherwise, don't know what this is.
235 return true;
236 }
237
238 // Get the instruction before it if it's a terminator.
239 MachineInstr *SecondLastInst = I;
240
241 // If there are three terminators, we don't know what sort of block this is.
242 if (SecondLastInst && I != MBB.begin() &&
Evan Chengbfd2ec42007-06-08 21:59:56 +0000243 isUnpredicatedTerminator(--I))
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000244 return true;
245
Chris Lattner289c2d52006-11-17 22:14:47 +0000246 // If the block ends with PPC::B and PPC:BCC, handle it.
247 if (SecondLastInst->getOpcode() == PPC::BCC &&
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000248 LastInst->getOpcode() == PPC::B) {
Chris Lattner8aa797a2007-12-30 23:10:15 +0000249 TBB = SecondLastInst->getOperand(2).getMBB();
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000250 Cond.push_back(SecondLastInst->getOperand(0));
251 Cond.push_back(SecondLastInst->getOperand(1));
Chris Lattner8aa797a2007-12-30 23:10:15 +0000252 FBB = LastInst->getOperand(0).getMBB();
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000253 return false;
254 }
255
Dale Johannesen13e8b512007-06-13 17:59:52 +0000256 // If the block ends with two PPC:Bs, handle it. The second one is not
257 // executed, so remove it.
258 if (SecondLastInst->getOpcode() == PPC::B &&
259 LastInst->getOpcode() == PPC::B) {
Chris Lattner8aa797a2007-12-30 23:10:15 +0000260 TBB = SecondLastInst->getOperand(0).getMBB();
Dale Johannesen13e8b512007-06-13 17:59:52 +0000261 I = LastInst;
262 I->eraseFromParent();
263 return false;
264 }
265
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000266 // Otherwise, can't handle this.
267 return true;
268}
269
Evan Chengb5cdaa22007-05-18 00:05:48 +0000270unsigned PPCInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000271 MachineBasicBlock::iterator I = MBB.end();
Evan Chengb5cdaa22007-05-18 00:05:48 +0000272 if (I == MBB.begin()) return 0;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000273 --I;
Chris Lattner289c2d52006-11-17 22:14:47 +0000274 if (I->getOpcode() != PPC::B && I->getOpcode() != PPC::BCC)
Evan Chengb5cdaa22007-05-18 00:05:48 +0000275 return 0;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000276
277 // Remove the branch.
278 I->eraseFromParent();
279
280 I = MBB.end();
281
Evan Chengb5cdaa22007-05-18 00:05:48 +0000282 if (I == MBB.begin()) return 1;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000283 --I;
Chris Lattner289c2d52006-11-17 22:14:47 +0000284 if (I->getOpcode() != PPC::BCC)
Evan Chengb5cdaa22007-05-18 00:05:48 +0000285 return 1;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000286
287 // Remove the branch.
288 I->eraseFromParent();
Evan Chengb5cdaa22007-05-18 00:05:48 +0000289 return 2;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000290}
291
Evan Chengb5cdaa22007-05-18 00:05:48 +0000292unsigned
293PPCInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
294 MachineBasicBlock *FBB,
Owen Anderson44eb65c2008-08-14 22:49:33 +0000295 const SmallVectorImpl<MachineOperand> &Cond) const {
Chris Lattner2dc77232006-10-17 18:06:55 +0000296 // Shouldn't be a fall through.
297 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Chris Lattner54108062006-10-21 05:36:13 +0000298 assert((Cond.size() == 2 || Cond.size() == 0) &&
299 "PPC branch conditions have two components!");
Chris Lattner2dc77232006-10-17 18:06:55 +0000300
Chris Lattner54108062006-10-21 05:36:13 +0000301 // One-way branch.
Chris Lattner2dc77232006-10-17 18:06:55 +0000302 if (FBB == 0) {
Chris Lattner54108062006-10-21 05:36:13 +0000303 if (Cond.empty()) // Unconditional branch
Evan Chengc0f64ff2006-11-27 23:37:22 +0000304 BuildMI(&MBB, get(PPC::B)).addMBB(TBB);
Chris Lattner54108062006-10-21 05:36:13 +0000305 else // Conditional branch
Evan Chengc0f64ff2006-11-27 23:37:22 +0000306 BuildMI(&MBB, get(PPC::BCC))
Chris Lattner18258c62006-11-17 22:37:34 +0000307 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
Evan Chengb5cdaa22007-05-18 00:05:48 +0000308 return 1;
Chris Lattner2dc77232006-10-17 18:06:55 +0000309 }
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000310
Chris Lattner879d09c2006-10-21 05:42:09 +0000311 // Two-way Conditional Branch.
Evan Chengc0f64ff2006-11-27 23:37:22 +0000312 BuildMI(&MBB, get(PPC::BCC))
Chris Lattner18258c62006-11-17 22:37:34 +0000313 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
Evan Chengc0f64ff2006-11-27 23:37:22 +0000314 BuildMI(&MBB, get(PPC::B)).addMBB(FBB);
Evan Chengb5cdaa22007-05-18 00:05:48 +0000315 return 2;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000316}
317
Owen Anderson940f83e2008-08-26 18:03:31 +0000318bool PPCInstrInfo::copyRegToReg(MachineBasicBlock &MBB,
Owen Andersond10fd972007-12-31 06:32:00 +0000319 MachineBasicBlock::iterator MI,
320 unsigned DestReg, unsigned SrcReg,
321 const TargetRegisterClass *DestRC,
322 const TargetRegisterClass *SrcRC) const {
323 if (DestRC != SrcRC) {
Owen Anderson940f83e2008-08-26 18:03:31 +0000324 // Not yet supported!
325 return false;
Owen Andersond10fd972007-12-31 06:32:00 +0000326 }
327
328 if (DestRC == PPC::GPRCRegisterClass) {
329 BuildMI(MBB, MI, get(PPC::OR), DestReg).addReg(SrcReg).addReg(SrcReg);
330 } else if (DestRC == PPC::G8RCRegisterClass) {
331 BuildMI(MBB, MI, get(PPC::OR8), DestReg).addReg(SrcReg).addReg(SrcReg);
332 } else if (DestRC == PPC::F4RCRegisterClass) {
333 BuildMI(MBB, MI, get(PPC::FMRS), DestReg).addReg(SrcReg);
334 } else if (DestRC == PPC::F8RCRegisterClass) {
335 BuildMI(MBB, MI, get(PPC::FMRD), DestReg).addReg(SrcReg);
336 } else if (DestRC == PPC::CRRCRegisterClass) {
337 BuildMI(MBB, MI, get(PPC::MCRF), DestReg).addReg(SrcReg);
338 } else if (DestRC == PPC::VRRCRegisterClass) {
339 BuildMI(MBB, MI, get(PPC::VOR), DestReg).addReg(SrcReg).addReg(SrcReg);
Nicolas Geoffray0404cd92008-03-10 14:12:10 +0000340 } else if (DestRC == PPC::CRBITRCRegisterClass) {
341 BuildMI(MBB, MI, get(PPC::CROR), DestReg).addReg(SrcReg).addReg(SrcReg);
Owen Andersond10fd972007-12-31 06:32:00 +0000342 } else {
Owen Anderson940f83e2008-08-26 18:03:31 +0000343 // Attempt to copy register that is not GPR or FPR
344 return false;
Owen Andersond10fd972007-12-31 06:32:00 +0000345 }
Owen Anderson940f83e2008-08-26 18:03:31 +0000346
347 return true;
Owen Andersond10fd972007-12-31 06:32:00 +0000348}
349
Bill Wendling4a66e9a2008-03-10 22:49:16 +0000350bool
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000351PPCInstrInfo::StoreRegToStackSlot(MachineFunction &MF,
352 unsigned SrcReg, bool isKill,
Bill Wendling4a66e9a2008-03-10 22:49:16 +0000353 int FrameIdx,
354 const TargetRegisterClass *RC,
355 SmallVectorImpl<MachineInstr*> &NewMIs) const{
Owen Andersonf6372aa2008-01-01 21:11:32 +0000356 if (RC == PPC::GPRCRegisterClass) {
357 if (SrcReg != PPC::LR) {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000358 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::STW))
Bill Wendling4a66e9a2008-03-10 22:49:16 +0000359 .addReg(SrcReg, false, false, isKill),
360 FrameIdx));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000361 } else {
362 // FIXME: this spills LR immediately to memory in one step. To do this,
363 // we use R11, which we know cannot be used in the prolog/epilog. This is
364 // a hack.
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000365 NewMIs.push_back(BuildMI(MF, get(PPC::MFLR), PPC::R11));
366 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::STW))
Bill Wendling4a66e9a2008-03-10 22:49:16 +0000367 .addReg(PPC::R11, false, false, isKill),
368 FrameIdx));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000369 }
370 } else if (RC == PPC::G8RCRegisterClass) {
371 if (SrcReg != PPC::LR8) {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000372 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::STD))
Chris Lattnercb341de2008-03-10 18:55:53 +0000373 .addReg(SrcReg, false, false, isKill), FrameIdx));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000374 } else {
375 // FIXME: this spills LR immediately to memory in one step. To do this,
376 // we use R11, which we know cannot be used in the prolog/epilog. This is
377 // a hack.
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000378 NewMIs.push_back(BuildMI(MF, get(PPC::MFLR8), PPC::X11));
379 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::STD))
Chris Lattnercb341de2008-03-10 18:55:53 +0000380 .addReg(PPC::X11, false, false, isKill), FrameIdx));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000381 }
382 } else if (RC == PPC::F8RCRegisterClass) {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000383 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::STFD))
Chris Lattnercb341de2008-03-10 18:55:53 +0000384 .addReg(SrcReg, false, false, isKill), FrameIdx));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000385 } else if (RC == PPC::F4RCRegisterClass) {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000386 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::STFS))
Chris Lattnercb341de2008-03-10 18:55:53 +0000387 .addReg(SrcReg, false, false, isKill), FrameIdx));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000388 } else if (RC == PPC::CRRCRegisterClass) {
Bill Wendling4a66e9a2008-03-10 22:49:16 +0000389 if ((EnablePPC32RS && !TM.getSubtargetImpl()->isPPC64()) ||
390 (EnablePPC64RS && TM.getSubtargetImpl()->isPPC64())) {
391 // FIXME (64-bit): Enable
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000392 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::SPILL_CR))
Bill Wendling7194aaf2008-03-03 22:19:16 +0000393 .addReg(SrcReg, false, false, isKill),
Chris Lattner71a2cb22008-03-20 01:22:40 +0000394 FrameIdx));
Bill Wendling7194aaf2008-03-03 22:19:16 +0000395 return true;
396 } else {
397 // FIXME: We use R0 here, because it isn't available for RA. We need to
398 // store the CR in the low 4-bits of the saved value. First, issue a MFCR
399 // to save all of the CRBits.
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000400 NewMIs.push_back(BuildMI(MF, get(PPC::MFCR), PPC::R0));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000401
Bill Wendling7194aaf2008-03-03 22:19:16 +0000402 // If the saved register wasn't CR0, shift the bits left so that they are
403 // in CR0's slot.
404 if (SrcReg != PPC::CR0) {
405 unsigned ShiftBits = PPCRegisterInfo::getRegisterNumbering(SrcReg)*4;
406 // rlwinm r0, r0, ShiftBits, 0, 31.
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000407 NewMIs.push_back(BuildMI(MF, get(PPC::RLWINM), PPC::R0)
Chris Lattnercb341de2008-03-10 18:55:53 +0000408 .addReg(PPC::R0).addImm(ShiftBits).addImm(0).addImm(31));
Bill Wendling7194aaf2008-03-03 22:19:16 +0000409 }
410
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000411 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::STW))
Bill Wendling7194aaf2008-03-03 22:19:16 +0000412 .addReg(PPC::R0, false, false, isKill),
413 FrameIdx));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000414 }
Nicolas Geoffray0404cd92008-03-10 14:12:10 +0000415 } else if (RC == PPC::CRBITRCRegisterClass) {
416 // FIXME: We use CRi here because there is no mtcrf on a bit. Since the
417 // backend currently only uses CR1EQ as an individual bit, this should
418 // not cause any bug. If we need other uses of CR bits, the following
419 // code may be invalid.
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000420 unsigned Reg = 0;
Nicolas Geoffray0404cd92008-03-10 14:12:10 +0000421 if (SrcReg >= PPC::CR0LT || SrcReg <= PPC::CR0UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000422 Reg = PPC::CR0;
Nicolas Geoffray0404cd92008-03-10 14:12:10 +0000423 else if (SrcReg >= PPC::CR1LT || SrcReg <= PPC::CR1UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000424 Reg = PPC::CR1;
425 else if (SrcReg >= PPC::CR2LT || SrcReg <= PPC::CR2UN)
426 Reg = PPC::CR2;
427 else if (SrcReg >= PPC::CR3LT || SrcReg <= PPC::CR3UN)
428 Reg = PPC::CR3;
429 else if (SrcReg >= PPC::CR4LT || SrcReg <= PPC::CR4UN)
430 Reg = PPC::CR4;
431 else if (SrcReg >= PPC::CR5LT || SrcReg <= PPC::CR5UN)
432 Reg = PPC::CR5;
433 else if (SrcReg >= PPC::CR6LT || SrcReg <= PPC::CR6UN)
434 Reg = PPC::CR6;
435 else if (SrcReg >= PPC::CR7LT || SrcReg <= PPC::CR7UN)
436 Reg = PPC::CR7;
437
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000438 return StoreRegToStackSlot(MF, Reg, isKill, FrameIdx,
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000439 PPC::CRRCRegisterClass, NewMIs);
440
Owen Andersonf6372aa2008-01-01 21:11:32 +0000441 } else if (RC == PPC::VRRCRegisterClass) {
442 // We don't have indexed addressing for vector loads. Emit:
443 // R0 = ADDI FI#
444 // STVX VAL, 0, R0
445 //
446 // FIXME: We use R0 here, because it isn't available for RA.
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000447 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::ADDI), PPC::R0),
Owen Andersonf6372aa2008-01-01 21:11:32 +0000448 FrameIdx, 0, 0));
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000449 NewMIs.push_back(BuildMI(MF, get(PPC::STVX))
Chris Lattnercb341de2008-03-10 18:55:53 +0000450 .addReg(SrcReg, false, false, isKill).addReg(PPC::R0).addReg(PPC::R0));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000451 } else {
452 assert(0 && "Unknown regclass!");
453 abort();
454 }
Bill Wendling7194aaf2008-03-03 22:19:16 +0000455
456 return false;
Owen Andersonf6372aa2008-01-01 21:11:32 +0000457}
458
459void
460PPCInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
Bill Wendling7194aaf2008-03-03 22:19:16 +0000461 MachineBasicBlock::iterator MI,
462 unsigned SrcReg, bool isKill, int FrameIdx,
463 const TargetRegisterClass *RC) const {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000464 MachineFunction &MF = *MBB.getParent();
Owen Andersonf6372aa2008-01-01 21:11:32 +0000465 SmallVector<MachineInstr*, 4> NewMIs;
Bill Wendling7194aaf2008-03-03 22:19:16 +0000466
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000467 if (StoreRegToStackSlot(MF, SrcReg, isKill, FrameIdx, RC, NewMIs)) {
468 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Bill Wendling7194aaf2008-03-03 22:19:16 +0000469 FuncInfo->setSpillsCR();
470 }
471
Owen Andersonf6372aa2008-01-01 21:11:32 +0000472 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
473 MBB.insert(MI, NewMIs[i]);
474}
475
476void PPCInstrInfo::storeRegToAddr(MachineFunction &MF, unsigned SrcReg,
Bill Wendling7194aaf2008-03-03 22:19:16 +0000477 bool isKill,
478 SmallVectorImpl<MachineOperand> &Addr,
479 const TargetRegisterClass *RC,
480 SmallVectorImpl<MachineInstr*> &NewMIs) const{
Dan Gohmand735b802008-10-03 15:45:36 +0000481 if (Addr[0].isFI()) {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000482 if (StoreRegToStackSlot(MF, SrcReg, isKill,
483 Addr[0].getIndex(), RC, NewMIs)) {
Bill Wendling7194aaf2008-03-03 22:19:16 +0000484 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
485 FuncInfo->setSpillsCR();
486 }
487
Owen Andersonf6372aa2008-01-01 21:11:32 +0000488 return;
489 }
490
491 unsigned Opc = 0;
492 if (RC == PPC::GPRCRegisterClass) {
493 Opc = PPC::STW;
494 } else if (RC == PPC::G8RCRegisterClass) {
495 Opc = PPC::STD;
496 } else if (RC == PPC::F8RCRegisterClass) {
497 Opc = PPC::STFD;
498 } else if (RC == PPC::F4RCRegisterClass) {
499 Opc = PPC::STFS;
500 } else if (RC == PPC::VRRCRegisterClass) {
501 Opc = PPC::STVX;
502 } else {
503 assert(0 && "Unknown regclass!");
504 abort();
505 }
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000506 MachineInstrBuilder MIB = BuildMI(MF, get(Opc))
Owen Andersonf6372aa2008-01-01 21:11:32 +0000507 .addReg(SrcReg, false, false, isKill);
508 for (unsigned i = 0, e = Addr.size(); i != e; ++i) {
509 MachineOperand &MO = Addr[i];
Dan Gohmand735b802008-10-03 15:45:36 +0000510 if (MO.isReg())
Owen Andersonf6372aa2008-01-01 21:11:32 +0000511 MIB.addReg(MO.getReg());
Dan Gohmand735b802008-10-03 15:45:36 +0000512 else if (MO.isImm())
Owen Andersonf6372aa2008-01-01 21:11:32 +0000513 MIB.addImm(MO.getImm());
514 else
515 MIB.addFrameIndex(MO.getIndex());
516 }
517 NewMIs.push_back(MIB);
518 return;
519}
520
Bill Wendling4a66e9a2008-03-10 22:49:16 +0000521void
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000522PPCInstrInfo::LoadRegFromStackSlot(MachineFunction &MF,
523 unsigned DestReg, int FrameIdx,
Bill Wendling4a66e9a2008-03-10 22:49:16 +0000524 const TargetRegisterClass *RC,
525 SmallVectorImpl<MachineInstr*> &NewMIs)const{
Owen Andersonf6372aa2008-01-01 21:11:32 +0000526 if (RC == PPC::GPRCRegisterClass) {
527 if (DestReg != PPC::LR) {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000528 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::LWZ), DestReg),
Owen Andersonf6372aa2008-01-01 21:11:32 +0000529 FrameIdx));
530 } else {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000531 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::LWZ), PPC::R11),
Owen Andersonf6372aa2008-01-01 21:11:32 +0000532 FrameIdx));
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000533 NewMIs.push_back(BuildMI(MF, get(PPC::MTLR)).addReg(PPC::R11));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000534 }
535 } else if (RC == PPC::G8RCRegisterClass) {
536 if (DestReg != PPC::LR8) {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000537 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::LD), DestReg),
Owen Andersonf6372aa2008-01-01 21:11:32 +0000538 FrameIdx));
539 } else {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000540 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::LD), PPC::R11),
Owen Andersonf6372aa2008-01-01 21:11:32 +0000541 FrameIdx));
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000542 NewMIs.push_back(BuildMI(MF, get(PPC::MTLR8)).addReg(PPC::R11));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000543 }
544 } else if (RC == PPC::F8RCRegisterClass) {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000545 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::LFD), DestReg),
Owen Andersonf6372aa2008-01-01 21:11:32 +0000546 FrameIdx));
547 } else if (RC == PPC::F4RCRegisterClass) {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000548 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::LFS), DestReg),
Owen Andersonf6372aa2008-01-01 21:11:32 +0000549 FrameIdx));
550 } else if (RC == PPC::CRRCRegisterClass) {
551 // FIXME: We use R0 here, because it isn't available for RA.
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000552 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::LWZ), PPC::R0),
Owen Andersonf6372aa2008-01-01 21:11:32 +0000553 FrameIdx));
554
555 // If the reloaded register isn't CR0, shift the bits right so that they are
556 // in the right CR's slot.
557 if (DestReg != PPC::CR0) {
558 unsigned ShiftBits = PPCRegisterInfo::getRegisterNumbering(DestReg)*4;
559 // rlwinm r11, r11, 32-ShiftBits, 0, 31.
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000560 NewMIs.push_back(BuildMI(MF, get(PPC::RLWINM), PPC::R0)
Owen Andersonf6372aa2008-01-01 21:11:32 +0000561 .addReg(PPC::R0).addImm(32-ShiftBits).addImm(0).addImm(31));
562 }
563
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000564 NewMIs.push_back(BuildMI(MF, get(PPC::MTCRF), DestReg).addReg(PPC::R0));
Nicolas Geoffray0404cd92008-03-10 14:12:10 +0000565 } else if (RC == PPC::CRBITRCRegisterClass) {
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000566
567 unsigned Reg = 0;
Nicolas Geoffray0404cd92008-03-10 14:12:10 +0000568 if (DestReg >= PPC::CR0LT || DestReg <= PPC::CR0UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000569 Reg = PPC::CR0;
Nicolas Geoffray0404cd92008-03-10 14:12:10 +0000570 else if (DestReg >= PPC::CR1LT || DestReg <= PPC::CR1UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000571 Reg = PPC::CR1;
572 else if (DestReg >= PPC::CR2LT || DestReg <= PPC::CR2UN)
573 Reg = PPC::CR2;
574 else if (DestReg >= PPC::CR3LT || DestReg <= PPC::CR3UN)
575 Reg = PPC::CR3;
576 else if (DestReg >= PPC::CR4LT || DestReg <= PPC::CR4UN)
577 Reg = PPC::CR4;
578 else if (DestReg >= PPC::CR5LT || DestReg <= PPC::CR5UN)
579 Reg = PPC::CR5;
580 else if (DestReg >= PPC::CR6LT || DestReg <= PPC::CR6UN)
581 Reg = PPC::CR6;
582 else if (DestReg >= PPC::CR7LT || DestReg <= PPC::CR7UN)
583 Reg = PPC::CR7;
584
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000585 return LoadRegFromStackSlot(MF, Reg, FrameIdx,
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000586 PPC::CRRCRegisterClass, NewMIs);
587
Owen Andersonf6372aa2008-01-01 21:11:32 +0000588 } else if (RC == PPC::VRRCRegisterClass) {
589 // We don't have indexed addressing for vector loads. Emit:
590 // R0 = ADDI FI#
591 // Dest = LVX 0, R0
592 //
593 // FIXME: We use R0 here, because it isn't available for RA.
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000594 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::ADDI), PPC::R0),
Owen Andersonf6372aa2008-01-01 21:11:32 +0000595 FrameIdx, 0, 0));
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000596 NewMIs.push_back(BuildMI(MF, get(PPC::LVX),DestReg).addReg(PPC::R0)
Owen Andersonf6372aa2008-01-01 21:11:32 +0000597 .addReg(PPC::R0));
598 } else {
599 assert(0 && "Unknown regclass!");
600 abort();
601 }
602}
603
604void
605PPCInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
Bill Wendling7194aaf2008-03-03 22:19:16 +0000606 MachineBasicBlock::iterator MI,
607 unsigned DestReg, int FrameIdx,
608 const TargetRegisterClass *RC) const {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000609 MachineFunction &MF = *MBB.getParent();
Owen Andersonf6372aa2008-01-01 21:11:32 +0000610 SmallVector<MachineInstr*, 4> NewMIs;
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000611 LoadRegFromStackSlot(MF, DestReg, FrameIdx, RC, NewMIs);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000612 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
613 MBB.insert(MI, NewMIs[i]);
614}
615
616void PPCInstrInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
Bill Wendling7194aaf2008-03-03 22:19:16 +0000617 SmallVectorImpl<MachineOperand> &Addr,
618 const TargetRegisterClass *RC,
619 SmallVectorImpl<MachineInstr*> &NewMIs)const{
Dan Gohmand735b802008-10-03 15:45:36 +0000620 if (Addr[0].isFI()) {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000621 LoadRegFromStackSlot(MF, DestReg, Addr[0].getIndex(), RC, NewMIs);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000622 return;
623 }
624
625 unsigned Opc = 0;
626 if (RC == PPC::GPRCRegisterClass) {
627 assert(DestReg != PPC::LR && "Can't handle this yet!");
628 Opc = PPC::LWZ;
629 } else if (RC == PPC::G8RCRegisterClass) {
630 assert(DestReg != PPC::LR8 && "Can't handle this yet!");
631 Opc = PPC::LD;
632 } else if (RC == PPC::F8RCRegisterClass) {
633 Opc = PPC::LFD;
634 } else if (RC == PPC::F4RCRegisterClass) {
635 Opc = PPC::LFS;
636 } else if (RC == PPC::VRRCRegisterClass) {
637 Opc = PPC::LVX;
638 } else {
639 assert(0 && "Unknown regclass!");
640 abort();
641 }
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000642 MachineInstrBuilder MIB = BuildMI(MF, get(Opc), DestReg);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000643 for (unsigned i = 0, e = Addr.size(); i != e; ++i) {
644 MachineOperand &MO = Addr[i];
Dan Gohmand735b802008-10-03 15:45:36 +0000645 if (MO.isReg())
Owen Andersonf6372aa2008-01-01 21:11:32 +0000646 MIB.addReg(MO.getReg());
Dan Gohmand735b802008-10-03 15:45:36 +0000647 else if (MO.isImm())
Owen Andersonf6372aa2008-01-01 21:11:32 +0000648 MIB.addImm(MO.getImm());
649 else
650 MIB.addFrameIndex(MO.getIndex());
651 }
652 NewMIs.push_back(MIB);
653 return;
654}
655
Owen Anderson43dbe052008-01-07 01:35:02 +0000656/// foldMemoryOperand - PowerPC (like most RISC's) can only fold spills into
657/// copy instructions, turning them into load/store instructions.
Evan Cheng5fd79d02008-02-08 21:20:40 +0000658MachineInstr *PPCInstrInfo::foldMemoryOperand(MachineFunction &MF,
659 MachineInstr *MI,
Dan Gohman8e8b8a22008-10-16 01:49:15 +0000660 const SmallVectorImpl<unsigned> &Ops,
Owen Anderson43dbe052008-01-07 01:35:02 +0000661 int FrameIndex) const {
662 if (Ops.size() != 1) return NULL;
663
664 // Make sure this is a reg-reg copy. Note that we can't handle MCRF, because
665 // it takes more than one instruction to store it.
666 unsigned Opc = MI->getOpcode();
667 unsigned OpNum = Ops[0];
668
669 MachineInstr *NewMI = NULL;
670 if ((Opc == PPC::OR &&
671 MI->getOperand(1).getReg() == MI->getOperand(2).getReg())) {
672 if (OpNum == 0) { // move -> store
673 unsigned InReg = MI->getOperand(1).getReg();
Evan Cheng9f1c8312008-07-03 09:09:37 +0000674 bool isKill = MI->getOperand(1).isKill();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000675 NewMI = addFrameReference(BuildMI(MF, get(PPC::STW))
Evan Cheng9f1c8312008-07-03 09:09:37 +0000676 .addReg(InReg, false, false, isKill),
Owen Anderson43dbe052008-01-07 01:35:02 +0000677 FrameIndex);
678 } else { // move -> load
679 unsigned OutReg = MI->getOperand(0).getReg();
Evan Cheng9f1c8312008-07-03 09:09:37 +0000680 bool isDead = MI->getOperand(0).isDead();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000681 NewMI = addFrameReference(BuildMI(MF, get(PPC::LWZ))
Evan Cheng9f1c8312008-07-03 09:09:37 +0000682 .addReg(OutReg, true, false, false, isDead),
Owen Anderson43dbe052008-01-07 01:35:02 +0000683 FrameIndex);
684 }
685 } else if ((Opc == PPC::OR8 &&
686 MI->getOperand(1).getReg() == MI->getOperand(2).getReg())) {
687 if (OpNum == 0) { // move -> store
688 unsigned InReg = MI->getOperand(1).getReg();
Evan Cheng9f1c8312008-07-03 09:09:37 +0000689 bool isKill = MI->getOperand(1).isKill();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000690 NewMI = addFrameReference(BuildMI(MF, get(PPC::STD))
Evan Cheng9f1c8312008-07-03 09:09:37 +0000691 .addReg(InReg, false, false, isKill),
Owen Anderson43dbe052008-01-07 01:35:02 +0000692 FrameIndex);
693 } else { // move -> load
694 unsigned OutReg = MI->getOperand(0).getReg();
Evan Cheng9f1c8312008-07-03 09:09:37 +0000695 bool isDead = MI->getOperand(0).isDead();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000696 NewMI = addFrameReference(BuildMI(MF, get(PPC::LD))
Evan Cheng9f1c8312008-07-03 09:09:37 +0000697 .addReg(OutReg, true, false, false, isDead),
698 FrameIndex);
Owen Anderson43dbe052008-01-07 01:35:02 +0000699 }
700 } else if (Opc == PPC::FMRD) {
701 if (OpNum == 0) { // move -> store
702 unsigned InReg = MI->getOperand(1).getReg();
Evan Cheng9f1c8312008-07-03 09:09:37 +0000703 bool isKill = MI->getOperand(1).isKill();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000704 NewMI = addFrameReference(BuildMI(MF, get(PPC::STFD))
Evan Cheng9f1c8312008-07-03 09:09:37 +0000705 .addReg(InReg, false, false, isKill),
Owen Anderson43dbe052008-01-07 01:35:02 +0000706 FrameIndex);
707 } else { // move -> load
708 unsigned OutReg = MI->getOperand(0).getReg();
Evan Cheng9f1c8312008-07-03 09:09:37 +0000709 bool isDead = MI->getOperand(0).isDead();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000710 NewMI = addFrameReference(BuildMI(MF, get(PPC::LFD))
Evan Cheng9f1c8312008-07-03 09:09:37 +0000711 .addReg(OutReg, true, false, false, isDead),
712 FrameIndex);
Owen Anderson43dbe052008-01-07 01:35:02 +0000713 }
714 } else if (Opc == PPC::FMRS) {
715 if (OpNum == 0) { // move -> store
716 unsigned InReg = MI->getOperand(1).getReg();
Evan Cheng9f1c8312008-07-03 09:09:37 +0000717 bool isKill = MI->getOperand(1).isKill();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000718 NewMI = addFrameReference(BuildMI(MF, get(PPC::STFS))
Evan Cheng9f1c8312008-07-03 09:09:37 +0000719 .addReg(InReg, false, false, isKill),
Owen Anderson43dbe052008-01-07 01:35:02 +0000720 FrameIndex);
721 } else { // move -> load
722 unsigned OutReg = MI->getOperand(0).getReg();
Evan Cheng9f1c8312008-07-03 09:09:37 +0000723 bool isDead = MI->getOperand(0).isDead();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000724 NewMI = addFrameReference(BuildMI(MF, get(PPC::LFS))
Evan Cheng9f1c8312008-07-03 09:09:37 +0000725 .addReg(OutReg, true, false, false, isDead),
726 FrameIndex);
Owen Anderson43dbe052008-01-07 01:35:02 +0000727 }
728 }
729
Owen Anderson43dbe052008-01-07 01:35:02 +0000730 return NewMI;
731}
732
Dan Gohman8e8b8a22008-10-16 01:49:15 +0000733bool PPCInstrInfo::canFoldMemoryOperand(const MachineInstr *MI,
734 const SmallVectorImpl<unsigned> &Ops) const {
Owen Anderson43dbe052008-01-07 01:35:02 +0000735 if (Ops.size() != 1) return false;
736
737 // Make sure this is a reg-reg copy. Note that we can't handle MCRF, because
738 // it takes more than one instruction to store it.
739 unsigned Opc = MI->getOpcode();
740
741 if ((Opc == PPC::OR &&
742 MI->getOperand(1).getReg() == MI->getOperand(2).getReg()))
743 return true;
744 else if ((Opc == PPC::OR8 &&
745 MI->getOperand(1).getReg() == MI->getOperand(2).getReg()))
746 return true;
747 else if (Opc == PPC::FMRD || Opc == PPC::FMRS)
748 return true;
749
750 return false;
751}
752
Owen Andersonf6372aa2008-01-01 21:11:32 +0000753
Dan Gohman8e8b8a22008-10-16 01:49:15 +0000754bool PPCInstrInfo::BlockHasNoFallThrough(const MachineBasicBlock &MBB) const {
Chris Lattneref139822006-10-28 17:35:02 +0000755 if (MBB.empty()) return false;
756
757 switch (MBB.back().getOpcode()) {
Evan Cheng126f17a2007-05-21 18:44:17 +0000758 case PPC::BLR: // Return.
Chris Lattneref139822006-10-28 17:35:02 +0000759 case PPC::B: // Uncond branch.
760 case PPC::BCTR: // Indirect branch.
761 return true;
762 default: return false;
763 }
764}
765
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000766bool PPCInstrInfo::
Owen Anderson44eb65c2008-08-14 22:49:33 +0000767ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
Chris Lattner7c4fe252006-10-21 06:03:11 +0000768 assert(Cond.size() == 2 && "Invalid PPC branch opcode!");
769 // Leave the CR# the same, but invert the condition.
Chris Lattner18258c62006-11-17 22:37:34 +0000770 Cond[0].setImm(PPC::InvertPredicate((PPC::Predicate)Cond[0].getImm()));
Chris Lattner7c4fe252006-10-21 06:03:11 +0000771 return false;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000772}
Nicolas Geoffray52e724a2008-04-16 20:10:13 +0000773
774/// GetInstSize - Return the number of bytes of code the specified
775/// instruction may be. This returns the maximum number of bytes.
776///
777unsigned PPCInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
778 switch (MI->getOpcode()) {
779 case PPC::INLINEASM: { // Inline Asm: Variable size.
780 const MachineFunction *MF = MI->getParent()->getParent();
781 const char *AsmStr = MI->getOperand(0).getSymbolName();
782 return MF->getTarget().getTargetAsmInfo()->getInlineAsmLength(AsmStr);
783 }
Dan Gohman44066042008-07-01 00:05:16 +0000784 case PPC::DBG_LABEL:
785 case PPC::EH_LABEL:
786 case PPC::GC_LABEL:
Nicolas Geoffray52e724a2008-04-16 20:10:13 +0000787 return 0;
Nicolas Geoffray52e724a2008-04-16 20:10:13 +0000788 default:
789 return 4; // PowerPC instructions are all 4 bytes
790 }
791}