blob: 3db623ab9ab4f452f511cbd3662521c3d6a88226 [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"
Torok Edwindac237e2009-07-08 20:53:28 +000023#include "llvm/Support/ErrorHandling.h"
24#include "llvm/Support/raw_ostream.h"
Chris Lattneraf76e592009-08-22 20:48:53 +000025#include "llvm/MC/MCAsmInfo.h"
Misha Brukmanf2ccb772004-08-17 04:55:41 +000026using namespace llvm;
27
Bill Wendling4a66e9a2008-03-10 22:49:16 +000028extern cl::opt<bool> EnablePPC32RS; // FIXME (64-bit): See PPCRegisterInfo.cpp.
29extern cl::opt<bool> EnablePPC64RS; // FIXME (64-bit): See PPCRegisterInfo.cpp.
Bill Wendling880d0f62008-03-04 23:13:51 +000030
Chris Lattnerb1d26f62006-06-17 00:01:04 +000031PPCInstrInfo::PPCInstrInfo(PPCTargetMachine &tm)
Chris Lattner64105522008-01-01 01:03:04 +000032 : TargetInstrInfoImpl(PPCInsts, array_lengthof(PPCInsts)), TM(tm),
Evan Cheng7ce45782006-11-13 23:36:35 +000033 RI(*TM.getSubtargetImpl(), *this) {}
Chris Lattnerb1d26f62006-06-17 00:01:04 +000034
Nate Begeman21e463b2005-10-16 05:39:50 +000035bool PPCInstrInfo::isMoveInstr(const MachineInstr& MI,
36 unsigned& sourceReg,
Evan Cheng04ee5a12009-01-20 19:12:24 +000037 unsigned& destReg,
38 unsigned& sourceSubIdx,
39 unsigned& destSubIdx) const {
40 sourceSubIdx = destSubIdx = 0; // No sub-registers.
41
Chris Lattnercc8cd0c2008-01-07 02:48:55 +000042 unsigned oc = MI.getOpcode();
Chris Lattnerb410dc92006-06-20 23:18:58 +000043 if (oc == PPC::OR || oc == PPC::OR8 || oc == PPC::VOR ||
Chris Lattner14c09b82005-10-19 01:50:36 +000044 oc == PPC::OR4To8 || oc == PPC::OR8To4) { // or r1, r2, r2
Evan Cheng1e3417292007-04-25 07:12:14 +000045 assert(MI.getNumOperands() >= 3 &&
Dan Gohmand735b802008-10-03 15:45:36 +000046 MI.getOperand(0).isReg() &&
47 MI.getOperand(1).isReg() &&
48 MI.getOperand(2).isReg() &&
Misha Brukmanf2ccb772004-08-17 04:55:41 +000049 "invalid PPC OR instruction!");
50 if (MI.getOperand(1).getReg() == MI.getOperand(2).getReg()) {
51 sourceReg = MI.getOperand(1).getReg();
52 destReg = MI.getOperand(0).getReg();
53 return true;
54 }
55 } else if (oc == PPC::ADDI) { // addi r1, r2, 0
Evan Cheng1e3417292007-04-25 07:12:14 +000056 assert(MI.getNumOperands() >= 3 &&
Dan Gohmand735b802008-10-03 15:45:36 +000057 MI.getOperand(0).isReg() &&
58 MI.getOperand(2).isImm() &&
Misha Brukmanf2ccb772004-08-17 04:55:41 +000059 "invalid PPC ADDI instruction!");
Dan Gohmand735b802008-10-03 15:45:36 +000060 if (MI.getOperand(1).isReg() && MI.getOperand(2).getImm() == 0) {
Misha Brukmanf2ccb772004-08-17 04:55:41 +000061 sourceReg = MI.getOperand(1).getReg();
62 destReg = MI.getOperand(0).getReg();
63 return true;
64 }
Nate Begemancb90de32004-10-07 22:26:12 +000065 } else if (oc == PPC::ORI) { // ori r1, r2, 0
Evan Cheng1e3417292007-04-25 07:12:14 +000066 assert(MI.getNumOperands() >= 3 &&
Dan Gohmand735b802008-10-03 15:45:36 +000067 MI.getOperand(0).isReg() &&
68 MI.getOperand(1).isReg() &&
69 MI.getOperand(2).isImm() &&
Nate Begemancb90de32004-10-07 22:26:12 +000070 "invalid PPC ORI instruction!");
Chris Lattner9a1ceae2007-12-30 20:49:49 +000071 if (MI.getOperand(2).getImm() == 0) {
Nate Begemancb90de32004-10-07 22:26:12 +000072 sourceReg = MI.getOperand(1).getReg();
73 destReg = MI.getOperand(0).getReg();
74 return true;
75 }
Chris Lattnereb5d47d2005-10-07 05:00:52 +000076 } else if (oc == PPC::FMRS || oc == PPC::FMRD ||
77 oc == PPC::FMRSD) { // fmr r1, r2
Evan Cheng1e3417292007-04-25 07:12:14 +000078 assert(MI.getNumOperands() >= 2 &&
Dan Gohmand735b802008-10-03 15:45:36 +000079 MI.getOperand(0).isReg() &&
80 MI.getOperand(1).isReg() &&
Misha Brukmanf2ccb772004-08-17 04:55:41 +000081 "invalid PPC FMR instruction");
82 sourceReg = MI.getOperand(1).getReg();
83 destReg = MI.getOperand(0).getReg();
84 return true;
Nate Begeman7af02482005-04-12 07:04:16 +000085 } else if (oc == PPC::MCRF) { // mcrf cr1, cr2
Evan Cheng1e3417292007-04-25 07:12:14 +000086 assert(MI.getNumOperands() >= 2 &&
Dan Gohmand735b802008-10-03 15:45:36 +000087 MI.getOperand(0).isReg() &&
88 MI.getOperand(1).isReg() &&
Nate Begeman7af02482005-04-12 07:04:16 +000089 "invalid PPC MCRF instruction");
90 sourceReg = MI.getOperand(1).getReg();
91 destReg = MI.getOperand(0).getReg();
92 return true;
Misha Brukmanf2ccb772004-08-17 04:55:41 +000093 }
94 return false;
95}
Chris Lattner043870d2005-09-09 18:17:41 +000096
Dan Gohmancbad42c2008-11-18 19:49:32 +000097unsigned PPCInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
Chris Lattner9c09c9e2006-03-16 22:24:02 +000098 int &FrameIndex) const {
Chris Lattner40839602006-02-02 20:12:32 +000099 switch (MI->getOpcode()) {
100 default: break;
101 case PPC::LD:
102 case PPC::LWZ:
103 case PPC::LFS:
104 case PPC::LFD:
Dan Gohmand735b802008-10-03 15:45:36 +0000105 if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
106 MI->getOperand(2).isFI()) {
Chris Lattner8aa797a2007-12-30 23:10:15 +0000107 FrameIndex = MI->getOperand(2).getIndex();
Chris Lattner40839602006-02-02 20:12:32 +0000108 return MI->getOperand(0).getReg();
109 }
110 break;
111 }
112 return 0;
Chris Lattner65242872006-02-02 20:16:12 +0000113}
Chris Lattner40839602006-02-02 20:12:32 +0000114
Dan Gohmancbad42c2008-11-18 19:49:32 +0000115unsigned PPCInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
Chris Lattner65242872006-02-02 20:16:12 +0000116 int &FrameIndex) const {
117 switch (MI->getOpcode()) {
118 default: break;
Nate Begeman3b478b32006-02-02 21:07:50 +0000119 case PPC::STD:
Chris Lattner65242872006-02-02 20:16:12 +0000120 case PPC::STW:
121 case PPC::STFS:
122 case PPC::STFD:
Dan Gohmand735b802008-10-03 15:45:36 +0000123 if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
124 MI->getOperand(2).isFI()) {
Chris Lattner8aa797a2007-12-30 23:10:15 +0000125 FrameIndex = MI->getOperand(2).getIndex();
Chris Lattner65242872006-02-02 20:16:12 +0000126 return MI->getOperand(0).getReg();
127 }
128 break;
129 }
130 return 0;
131}
Chris Lattner40839602006-02-02 20:12:32 +0000132
Chris Lattner043870d2005-09-09 18:17:41 +0000133// commuteInstruction - We can commute rlwimi instructions, but only if the
134// rotate amt is zero. We also have to munge the immediates a bit.
Evan Cheng58dcb0e2008-06-16 07:33:11 +0000135MachineInstr *
136PPCInstrInfo::commuteInstruction(MachineInstr *MI, bool NewMI) const {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000137 MachineFunction &MF = *MI->getParent()->getParent();
138
Chris Lattner043870d2005-09-09 18:17:41 +0000139 // Normal instructions can be commuted the obvious way.
140 if (MI->getOpcode() != PPC::RLWIMI)
Evan Cheng58dcb0e2008-06-16 07:33:11 +0000141 return TargetInstrInfoImpl::commuteInstruction(MI, NewMI);
Chris Lattner043870d2005-09-09 18:17:41 +0000142
143 // Cannot commute if it has a non-zero rotate count.
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000144 if (MI->getOperand(3).getImm() != 0)
Chris Lattner043870d2005-09-09 18:17:41 +0000145 return 0;
146
147 // If we have a zero rotate count, we have:
148 // M = mask(MB,ME)
149 // Op0 = (Op1 & ~M) | (Op2 & M)
150 // Change this to:
151 // M = mask((ME+1)&31, (MB-1)&31)
152 // Op0 = (Op2 & ~M) | (Op1 & M)
153
154 // Swap op1/op2
Evan Chenga4d16a12008-02-13 02:46:49 +0000155 unsigned Reg0 = MI->getOperand(0).getReg();
Chris Lattner043870d2005-09-09 18:17:41 +0000156 unsigned Reg1 = MI->getOperand(1).getReg();
157 unsigned Reg2 = MI->getOperand(2).getReg();
Evan Cheng6ce7dc22006-11-15 20:58:11 +0000158 bool Reg1IsKill = MI->getOperand(1).isKill();
159 bool Reg2IsKill = MI->getOperand(2).isKill();
Evan Cheng58dcb0e2008-06-16 07:33:11 +0000160 bool ChangeReg0 = false;
Evan Chenga4d16a12008-02-13 02:46:49 +0000161 // If machine instrs are no longer in two-address forms, update
162 // destination register as well.
163 if (Reg0 == Reg1) {
164 // Must be two address instruction!
165 assert(MI->getDesc().getOperandConstraint(0, TOI::TIED_TO) &&
166 "Expecting a two-address instruction!");
Evan Chenga4d16a12008-02-13 02:46:49 +0000167 Reg2IsKill = false;
Evan Cheng58dcb0e2008-06-16 07:33:11 +0000168 ChangeReg0 = true;
Evan Chenga4d16a12008-02-13 02:46:49 +0000169 }
Evan Cheng58dcb0e2008-06-16 07:33:11 +0000170
171 // Masks.
172 unsigned MB = MI->getOperand(4).getImm();
173 unsigned ME = MI->getOperand(5).getImm();
174
175 if (NewMI) {
176 // Create a new instruction.
177 unsigned Reg0 = ChangeReg0 ? Reg2 : MI->getOperand(0).getReg();
178 bool Reg0IsDead = MI->getOperand(0).isDead();
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000179 return BuildMI(MF, MI->getDebugLoc(), MI->getDesc())
Bill Wendling587daed2009-05-13 21:33:08 +0000180 .addReg(Reg0, RegState::Define | getDeadRegState(Reg0IsDead))
181 .addReg(Reg2, getKillRegState(Reg2IsKill))
182 .addReg(Reg1, getKillRegState(Reg1IsKill))
Evan Cheng58dcb0e2008-06-16 07:33:11 +0000183 .addImm((ME+1) & 31)
184 .addImm((MB-1) & 31);
185 }
186
187 if (ChangeReg0)
188 MI->getOperand(0).setReg(Reg2);
Chris Lattnere53f4a02006-05-04 17:52:23 +0000189 MI->getOperand(2).setReg(Reg1);
190 MI->getOperand(1).setReg(Reg2);
Chris Lattnerf7382302007-12-30 21:56:09 +0000191 MI->getOperand(2).setIsKill(Reg1IsKill);
192 MI->getOperand(1).setIsKill(Reg2IsKill);
Chris Lattner043870d2005-09-09 18:17:41 +0000193
194 // Swap the mask around.
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000195 MI->getOperand(4).setImm((ME+1) & 31);
196 MI->getOperand(5).setImm((MB-1) & 31);
Chris Lattner043870d2005-09-09 18:17:41 +0000197 return MI;
198}
Chris Lattnerbbf1c722006-03-05 23:49:55 +0000199
200void PPCInstrInfo::insertNoop(MachineBasicBlock &MBB,
201 MachineBasicBlock::iterator MI) const {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000202 DebugLoc DL = DebugLoc::getUnknownLoc();
203 if (MI != MBB.end()) DL = MI->getDebugLoc();
204
205 BuildMI(MBB, MI, DL, get(PPC::NOP));
Chris Lattnerbbf1c722006-03-05 23:49:55 +0000206}
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000207
208
209// Branch analysis.
210bool PPCInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
211 MachineBasicBlock *&FBB,
Evan Chengdc54d312009-02-09 07:14:22 +0000212 SmallVectorImpl<MachineOperand> &Cond,
213 bool AllowModify) 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) {
Evan Cheng82ae9332009-05-08 23:09:25 +0000225 if (!LastInst->getOperand(0).isMBB())
226 return true;
Chris Lattner8aa797a2007-12-30 23:10:15 +0000227 TBB = LastInst->getOperand(0).getMBB();
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000228 return false;
Chris Lattner289c2d52006-11-17 22:14:47 +0000229 } else if (LastInst->getOpcode() == PPC::BCC) {
Evan Cheng82ae9332009-05-08 23:09:25 +0000230 if (!LastInst->getOperand(2).isMBB())
231 return true;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000232 // Block ends with fall-through condbranch.
Chris Lattner8aa797a2007-12-30 23:10:15 +0000233 TBB = LastInst->getOperand(2).getMBB();
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000234 Cond.push_back(LastInst->getOperand(0));
235 Cond.push_back(LastInst->getOperand(1));
Chris Lattner7c4fe252006-10-21 06:03:11 +0000236 return false;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000237 }
238 // Otherwise, don't know what this is.
239 return true;
240 }
241
242 // Get the instruction before it if it's a terminator.
243 MachineInstr *SecondLastInst = I;
244
245 // If there are three terminators, we don't know what sort of block this is.
246 if (SecondLastInst && I != MBB.begin() &&
Evan Chengbfd2ec42007-06-08 21:59:56 +0000247 isUnpredicatedTerminator(--I))
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000248 return true;
249
Chris Lattner289c2d52006-11-17 22:14:47 +0000250 // If the block ends with PPC::B and PPC:BCC, handle it.
251 if (SecondLastInst->getOpcode() == PPC::BCC &&
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000252 LastInst->getOpcode() == PPC::B) {
Evan Cheng82ae9332009-05-08 23:09:25 +0000253 if (!SecondLastInst->getOperand(2).isMBB() ||
254 !LastInst->getOperand(0).isMBB())
255 return true;
Chris Lattner8aa797a2007-12-30 23:10:15 +0000256 TBB = SecondLastInst->getOperand(2).getMBB();
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000257 Cond.push_back(SecondLastInst->getOperand(0));
258 Cond.push_back(SecondLastInst->getOperand(1));
Chris Lattner8aa797a2007-12-30 23:10:15 +0000259 FBB = LastInst->getOperand(0).getMBB();
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000260 return false;
261 }
262
Dale Johannesen13e8b512007-06-13 17:59:52 +0000263 // If the block ends with two PPC:Bs, handle it. The second one is not
264 // executed, so remove it.
265 if (SecondLastInst->getOpcode() == PPC::B &&
266 LastInst->getOpcode() == PPC::B) {
Evan Cheng82ae9332009-05-08 23:09:25 +0000267 if (!SecondLastInst->getOperand(0).isMBB())
268 return true;
Chris Lattner8aa797a2007-12-30 23:10:15 +0000269 TBB = SecondLastInst->getOperand(0).getMBB();
Dale Johannesen13e8b512007-06-13 17:59:52 +0000270 I = LastInst;
Evan Chengdc54d312009-02-09 07:14:22 +0000271 if (AllowModify)
272 I->eraseFromParent();
Dale Johannesen13e8b512007-06-13 17:59:52 +0000273 return false;
274 }
275
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000276 // Otherwise, can't handle this.
277 return true;
278}
279
Evan Chengb5cdaa22007-05-18 00:05:48 +0000280unsigned PPCInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000281 MachineBasicBlock::iterator I = MBB.end();
Evan Chengb5cdaa22007-05-18 00:05:48 +0000282 if (I == MBB.begin()) return 0;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000283 --I;
Chris Lattner289c2d52006-11-17 22:14:47 +0000284 if (I->getOpcode() != PPC::B && I->getOpcode() != PPC::BCC)
Evan Chengb5cdaa22007-05-18 00:05:48 +0000285 return 0;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000286
287 // Remove the branch.
288 I->eraseFromParent();
289
290 I = MBB.end();
291
Evan Chengb5cdaa22007-05-18 00:05:48 +0000292 if (I == MBB.begin()) return 1;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000293 --I;
Chris Lattner289c2d52006-11-17 22:14:47 +0000294 if (I->getOpcode() != PPC::BCC)
Evan Chengb5cdaa22007-05-18 00:05:48 +0000295 return 1;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000296
297 // Remove the branch.
298 I->eraseFromParent();
Evan Chengb5cdaa22007-05-18 00:05:48 +0000299 return 2;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000300}
301
Evan Chengb5cdaa22007-05-18 00:05:48 +0000302unsigned
303PPCInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
304 MachineBasicBlock *FBB,
Owen Anderson44eb65c2008-08-14 22:49:33 +0000305 const SmallVectorImpl<MachineOperand> &Cond) const {
Dale Johannesen536a2f12009-02-13 02:27:39 +0000306 // FIXME this should probably have a DebugLoc argument
307 DebugLoc dl = DebugLoc::getUnknownLoc();
Chris Lattner2dc77232006-10-17 18:06:55 +0000308 // Shouldn't be a fall through.
309 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Chris Lattner54108062006-10-21 05:36:13 +0000310 assert((Cond.size() == 2 || Cond.size() == 0) &&
311 "PPC branch conditions have two components!");
Chris Lattner2dc77232006-10-17 18:06:55 +0000312
Chris Lattner54108062006-10-21 05:36:13 +0000313 // One-way branch.
Chris Lattner2dc77232006-10-17 18:06:55 +0000314 if (FBB == 0) {
Chris Lattner54108062006-10-21 05:36:13 +0000315 if (Cond.empty()) // Unconditional branch
Dale Johannesen536a2f12009-02-13 02:27:39 +0000316 BuildMI(&MBB, dl, get(PPC::B)).addMBB(TBB);
Chris Lattner54108062006-10-21 05:36:13 +0000317 else // Conditional branch
Dale Johannesen536a2f12009-02-13 02:27:39 +0000318 BuildMI(&MBB, dl, get(PPC::BCC))
Chris Lattner18258c62006-11-17 22:37:34 +0000319 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
Evan Chengb5cdaa22007-05-18 00:05:48 +0000320 return 1;
Chris Lattner2dc77232006-10-17 18:06:55 +0000321 }
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000322
Chris Lattner879d09c2006-10-21 05:42:09 +0000323 // Two-way Conditional Branch.
Dale Johannesen536a2f12009-02-13 02:27:39 +0000324 BuildMI(&MBB, dl, get(PPC::BCC))
Chris Lattner18258c62006-11-17 22:37:34 +0000325 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
Dale Johannesen536a2f12009-02-13 02:27:39 +0000326 BuildMI(&MBB, dl, get(PPC::B)).addMBB(FBB);
Evan Chengb5cdaa22007-05-18 00:05:48 +0000327 return 2;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000328}
329
Owen Anderson940f83e2008-08-26 18:03:31 +0000330bool PPCInstrInfo::copyRegToReg(MachineBasicBlock &MBB,
Owen Andersond10fd972007-12-31 06:32:00 +0000331 MachineBasicBlock::iterator MI,
332 unsigned DestReg, unsigned SrcReg,
333 const TargetRegisterClass *DestRC,
334 const TargetRegisterClass *SrcRC) const {
335 if (DestRC != SrcRC) {
Owen Anderson940f83e2008-08-26 18:03:31 +0000336 // Not yet supported!
337 return false;
Owen Andersond10fd972007-12-31 06:32:00 +0000338 }
339
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000340 DebugLoc DL = DebugLoc::getUnknownLoc();
341 if (MI != MBB.end()) DL = MI->getDebugLoc();
342
Owen Andersond10fd972007-12-31 06:32:00 +0000343 if (DestRC == PPC::GPRCRegisterClass) {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000344 BuildMI(MBB, MI, DL, get(PPC::OR), DestReg).addReg(SrcReg).addReg(SrcReg);
Owen Andersond10fd972007-12-31 06:32:00 +0000345 } else if (DestRC == PPC::G8RCRegisterClass) {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000346 BuildMI(MBB, MI, DL, get(PPC::OR8), DestReg).addReg(SrcReg).addReg(SrcReg);
Owen Andersond10fd972007-12-31 06:32:00 +0000347 } else if (DestRC == PPC::F4RCRegisterClass) {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000348 BuildMI(MBB, MI, DL, get(PPC::FMRS), DestReg).addReg(SrcReg);
Owen Andersond10fd972007-12-31 06:32:00 +0000349 } else if (DestRC == PPC::F8RCRegisterClass) {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000350 BuildMI(MBB, MI, DL, get(PPC::FMRD), DestReg).addReg(SrcReg);
Owen Andersond10fd972007-12-31 06:32:00 +0000351 } else if (DestRC == PPC::CRRCRegisterClass) {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000352 BuildMI(MBB, MI, DL, get(PPC::MCRF), DestReg).addReg(SrcReg);
Owen Andersond10fd972007-12-31 06:32:00 +0000353 } else if (DestRC == PPC::VRRCRegisterClass) {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000354 BuildMI(MBB, MI, DL, get(PPC::VOR), DestReg).addReg(SrcReg).addReg(SrcReg);
Nicolas Geoffray0404cd92008-03-10 14:12:10 +0000355 } else if (DestRC == PPC::CRBITRCRegisterClass) {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000356 BuildMI(MBB, MI, DL, get(PPC::CROR), DestReg).addReg(SrcReg).addReg(SrcReg);
Owen Andersond10fd972007-12-31 06:32:00 +0000357 } else {
Owen Anderson940f83e2008-08-26 18:03:31 +0000358 // Attempt to copy register that is not GPR or FPR
359 return false;
Owen Andersond10fd972007-12-31 06:32:00 +0000360 }
Owen Anderson940f83e2008-08-26 18:03:31 +0000361
362 return true;
Owen Andersond10fd972007-12-31 06:32:00 +0000363}
364
Bill Wendling4a66e9a2008-03-10 22:49:16 +0000365bool
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000366PPCInstrInfo::StoreRegToStackSlot(MachineFunction &MF,
367 unsigned SrcReg, bool isKill,
Bill Wendling4a66e9a2008-03-10 22:49:16 +0000368 int FrameIdx,
369 const TargetRegisterClass *RC,
370 SmallVectorImpl<MachineInstr*> &NewMIs) const{
Dale Johannesen21b55412009-02-12 23:08:38 +0000371 DebugLoc DL = DebugLoc::getUnknownLoc();
Owen Andersonf6372aa2008-01-01 21:11:32 +0000372 if (RC == PPC::GPRCRegisterClass) {
373 if (SrcReg != PPC::LR) {
Dale Johannesen21b55412009-02-12 23:08:38 +0000374 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
Bill Wendling587daed2009-05-13 21:33:08 +0000375 .addReg(SrcReg,
376 getKillRegState(isKill)),
Bill Wendling4a66e9a2008-03-10 22:49:16 +0000377 FrameIdx));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000378 } else {
379 // FIXME: this spills LR immediately to memory in one step. To do this,
380 // we use R11, which we know cannot be used in the prolog/epilog. This is
381 // a hack.
Dale Johannesen21b55412009-02-12 23:08:38 +0000382 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MFLR), PPC::R11));
383 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
Bill Wendling587daed2009-05-13 21:33:08 +0000384 .addReg(PPC::R11,
385 getKillRegState(isKill)),
Bill Wendling4a66e9a2008-03-10 22:49:16 +0000386 FrameIdx));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000387 }
388 } else if (RC == PPC::G8RCRegisterClass) {
389 if (SrcReg != PPC::LR8) {
Dale Johannesen21b55412009-02-12 23:08:38 +0000390 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STD))
Bill Wendling587daed2009-05-13 21:33:08 +0000391 .addReg(SrcReg,
392 getKillRegState(isKill)),
393 FrameIdx));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000394 } else {
395 // FIXME: this spills LR immediately to memory in one step. To do this,
396 // we use R11, which we know cannot be used in the prolog/epilog. This is
397 // a hack.
Dale Johannesen21b55412009-02-12 23:08:38 +0000398 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MFLR8), PPC::X11));
399 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STD))
Bill Wendling587daed2009-05-13 21:33:08 +0000400 .addReg(PPC::X11,
401 getKillRegState(isKill)),
402 FrameIdx));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000403 }
404 } else if (RC == PPC::F8RCRegisterClass) {
Dale Johannesen21b55412009-02-12 23:08:38 +0000405 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFD))
Bill Wendling587daed2009-05-13 21:33:08 +0000406 .addReg(SrcReg,
407 getKillRegState(isKill)),
408 FrameIdx));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000409 } else if (RC == PPC::F4RCRegisterClass) {
Dale Johannesen21b55412009-02-12 23:08:38 +0000410 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFS))
Bill Wendling587daed2009-05-13 21:33:08 +0000411 .addReg(SrcReg,
412 getKillRegState(isKill)),
413 FrameIdx));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000414 } else if (RC == PPC::CRRCRegisterClass) {
Bill Wendling4a66e9a2008-03-10 22:49:16 +0000415 if ((EnablePPC32RS && !TM.getSubtargetImpl()->isPPC64()) ||
416 (EnablePPC64RS && TM.getSubtargetImpl()->isPPC64())) {
417 // FIXME (64-bit): Enable
Dale Johannesen21b55412009-02-12 23:08:38 +0000418 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CR))
Bill Wendling587daed2009-05-13 21:33:08 +0000419 .addReg(SrcReg,
420 getKillRegState(isKill)),
Chris Lattner71a2cb22008-03-20 01:22:40 +0000421 FrameIdx));
Bill Wendling7194aaf2008-03-03 22:19:16 +0000422 return true;
423 } else {
Dale Johannesenc12da8d2010-02-12 21:35:34 +0000424 // FIXME: We need a scatch reg here. The trouble with using R0 is that
425 // it's possible for the stack frame to be so big the save location is
426 // out of range of immediate offsets, necessitating another register.
427 // We hack this on Darwin by reserving R2. It's probably broken on Linux
428 // at the moment.
429
430 // We need to store the CR in the low 4-bits of the saved value. First,
431 // issue a MFCR to save all of the CRBits.
432 unsigned ScratchReg = TM.getSubtargetImpl()->isDarwinABI() ?
433 PPC::R2 : PPC::R0;
434 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MFCR), ScratchReg));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000435
Bill Wendling7194aaf2008-03-03 22:19:16 +0000436 // If the saved register wasn't CR0, shift the bits left so that they are
437 // in CR0's slot.
438 if (SrcReg != PPC::CR0) {
439 unsigned ShiftBits = PPCRegisterInfo::getRegisterNumbering(SrcReg)*4;
Dale Johannesenc12da8d2010-02-12 21:35:34 +0000440 // rlwinm scratch, scratch, ShiftBits, 0, 31.
441 NewMIs.push_back(BuildMI(MF, DL, get(PPC::RLWINM), ScratchReg)
442 .addReg(ScratchReg).addImm(ShiftBits)
443 .addImm(0).addImm(31));
Bill Wendling7194aaf2008-03-03 22:19:16 +0000444 }
445
Dale Johannesen21b55412009-02-12 23:08:38 +0000446 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
Dale Johannesenc12da8d2010-02-12 21:35:34 +0000447 .addReg(ScratchReg,
Bill Wendling587daed2009-05-13 21:33:08 +0000448 getKillRegState(isKill)),
Bill Wendling7194aaf2008-03-03 22:19:16 +0000449 FrameIdx));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000450 }
Nicolas Geoffray0404cd92008-03-10 14:12:10 +0000451 } else if (RC == PPC::CRBITRCRegisterClass) {
452 // FIXME: We use CRi here because there is no mtcrf on a bit. Since the
453 // backend currently only uses CR1EQ as an individual bit, this should
454 // not cause any bug. If we need other uses of CR bits, the following
455 // code may be invalid.
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000456 unsigned Reg = 0;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000457 if (SrcReg == PPC::CR0LT || SrcReg == PPC::CR0GT ||
458 SrcReg == PPC::CR0EQ || SrcReg == PPC::CR0UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000459 Reg = PPC::CR0;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000460 else if (SrcReg == PPC::CR1LT || SrcReg == PPC::CR1GT ||
461 SrcReg == PPC::CR1EQ || SrcReg == PPC::CR1UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000462 Reg = PPC::CR1;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000463 else if (SrcReg == PPC::CR2LT || SrcReg == PPC::CR2GT ||
464 SrcReg == PPC::CR2EQ || SrcReg == PPC::CR2UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000465 Reg = PPC::CR2;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000466 else if (SrcReg == PPC::CR3LT || SrcReg == PPC::CR3GT ||
467 SrcReg == PPC::CR3EQ || SrcReg == PPC::CR3UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000468 Reg = PPC::CR3;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000469 else if (SrcReg == PPC::CR4LT || SrcReg == PPC::CR4GT ||
470 SrcReg == PPC::CR4EQ || SrcReg == PPC::CR4UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000471 Reg = PPC::CR4;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000472 else if (SrcReg == PPC::CR5LT || SrcReg == PPC::CR5GT ||
473 SrcReg == PPC::CR5EQ || SrcReg == PPC::CR5UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000474 Reg = PPC::CR5;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000475 else if (SrcReg == PPC::CR6LT || SrcReg == PPC::CR6GT ||
476 SrcReg == PPC::CR6EQ || SrcReg == PPC::CR6UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000477 Reg = PPC::CR6;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000478 else if (SrcReg == PPC::CR7LT || SrcReg == PPC::CR7GT ||
479 SrcReg == PPC::CR7EQ || SrcReg == PPC::CR7UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000480 Reg = PPC::CR7;
481
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000482 return StoreRegToStackSlot(MF, Reg, isKill, FrameIdx,
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000483 PPC::CRRCRegisterClass, NewMIs);
484
Owen Andersonf6372aa2008-01-01 21:11:32 +0000485 } else if (RC == PPC::VRRCRegisterClass) {
486 // We don't have indexed addressing for vector loads. Emit:
487 // R0 = ADDI FI#
488 // STVX VAL, 0, R0
489 //
490 // FIXME: We use R0 here, because it isn't available for RA.
Dale Johannesen21b55412009-02-12 23:08:38 +0000491 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::ADDI), PPC::R0),
Owen Andersonf6372aa2008-01-01 21:11:32 +0000492 FrameIdx, 0, 0));
Dale Johannesen21b55412009-02-12 23:08:38 +0000493 NewMIs.push_back(BuildMI(MF, DL, get(PPC::STVX))
Bill Wendling587daed2009-05-13 21:33:08 +0000494 .addReg(SrcReg, getKillRegState(isKill))
495 .addReg(PPC::R0)
496 .addReg(PPC::R0));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000497 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000498 llvm_unreachable("Unknown regclass!");
Owen Andersonf6372aa2008-01-01 21:11:32 +0000499 }
Bill Wendling7194aaf2008-03-03 22:19:16 +0000500
501 return false;
Owen Andersonf6372aa2008-01-01 21:11:32 +0000502}
503
504void
505PPCInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
Bill Wendling7194aaf2008-03-03 22:19:16 +0000506 MachineBasicBlock::iterator MI,
507 unsigned SrcReg, bool isKill, int FrameIdx,
508 const TargetRegisterClass *RC) const {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000509 MachineFunction &MF = *MBB.getParent();
Owen Andersonf6372aa2008-01-01 21:11:32 +0000510 SmallVector<MachineInstr*, 4> NewMIs;
Bill Wendling7194aaf2008-03-03 22:19:16 +0000511
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000512 if (StoreRegToStackSlot(MF, SrcReg, isKill, FrameIdx, RC, NewMIs)) {
513 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Bill Wendling7194aaf2008-03-03 22:19:16 +0000514 FuncInfo->setSpillsCR();
515 }
516
Owen Andersonf6372aa2008-01-01 21:11:32 +0000517 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
518 MBB.insert(MI, NewMIs[i]);
519}
520
Bill Wendling4a66e9a2008-03-10 22:49:16 +0000521void
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000522PPCInstrInfo::LoadRegFromStackSlot(MachineFunction &MF, DebugLoc DL,
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000523 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) {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000528 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ),
529 DestReg), FrameIdx));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000530 } else {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000531 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ),
532 PPC::R11), FrameIdx));
533 NewMIs.push_back(BuildMI(MF, DL, 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) {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000537 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LD), DestReg),
Owen Andersonf6372aa2008-01-01 21:11:32 +0000538 FrameIdx));
539 } else {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000540 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LD),
541 PPC::R11), FrameIdx));
542 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MTLR8)).addReg(PPC::R11));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000543 }
544 } else if (RC == PPC::F8RCRegisterClass) {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000545 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFD), DestReg),
Owen Andersonf6372aa2008-01-01 21:11:32 +0000546 FrameIdx));
547 } else if (RC == PPC::F4RCRegisterClass) {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000548 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFS), DestReg),
Owen Andersonf6372aa2008-01-01 21:11:32 +0000549 FrameIdx));
550 } else if (RC == PPC::CRRCRegisterClass) {
Dale Johannesenc12da8d2010-02-12 21:35:34 +0000551 // FIXME: We need a scatch reg here. The trouble with using R0 is that
552 // it's possible for the stack frame to be so big the save location is
553 // out of range of immediate offsets, necessitating another register.
554 // We hack this on Darwin by reserving R2. It's probably broken on Linux
555 // at the moment.
556 unsigned ScratchReg = TM.getSubtargetImpl()->isDarwinABI() ?
557 PPC::R2 : PPC::R0;
558 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ),
559 ScratchReg), FrameIdx));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000560
561 // If the reloaded register isn't CR0, shift the bits right so that they are
562 // in the right CR's slot.
563 if (DestReg != PPC::CR0) {
564 unsigned ShiftBits = PPCRegisterInfo::getRegisterNumbering(DestReg)*4;
565 // rlwinm r11, r11, 32-ShiftBits, 0, 31.
Dale Johannesenc12da8d2010-02-12 21:35:34 +0000566 NewMIs.push_back(BuildMI(MF, DL, get(PPC::RLWINM), ScratchReg)
567 .addReg(ScratchReg).addImm(32-ShiftBits).addImm(0)
568 .addImm(31));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000569 }
570
Dale Johannesenc12da8d2010-02-12 21:35:34 +0000571 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MTCRF), DestReg)
572 .addReg(ScratchReg));
Nicolas Geoffray0404cd92008-03-10 14:12:10 +0000573 } else if (RC == PPC::CRBITRCRegisterClass) {
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000574
575 unsigned Reg = 0;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000576 if (DestReg == PPC::CR0LT || DestReg == PPC::CR0GT ||
577 DestReg == PPC::CR0EQ || DestReg == PPC::CR0UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000578 Reg = PPC::CR0;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000579 else if (DestReg == PPC::CR1LT || DestReg == PPC::CR1GT ||
580 DestReg == PPC::CR1EQ || DestReg == PPC::CR1UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000581 Reg = PPC::CR1;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000582 else if (DestReg == PPC::CR2LT || DestReg == PPC::CR2GT ||
583 DestReg == PPC::CR2EQ || DestReg == PPC::CR2UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000584 Reg = PPC::CR2;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000585 else if (DestReg == PPC::CR3LT || DestReg == PPC::CR3GT ||
586 DestReg == PPC::CR3EQ || DestReg == PPC::CR3UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000587 Reg = PPC::CR3;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000588 else if (DestReg == PPC::CR4LT || DestReg == PPC::CR4GT ||
589 DestReg == PPC::CR4EQ || DestReg == PPC::CR4UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000590 Reg = PPC::CR4;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000591 else if (DestReg == PPC::CR5LT || DestReg == PPC::CR5GT ||
592 DestReg == PPC::CR5EQ || DestReg == PPC::CR5UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000593 Reg = PPC::CR5;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000594 else if (DestReg == PPC::CR6LT || DestReg == PPC::CR6GT ||
595 DestReg == PPC::CR6EQ || DestReg == PPC::CR6UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000596 Reg = PPC::CR6;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000597 else if (DestReg == PPC::CR7LT || DestReg == PPC::CR7GT ||
598 DestReg == PPC::CR7EQ || DestReg == PPC::CR7UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000599 Reg = PPC::CR7;
600
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000601 return LoadRegFromStackSlot(MF, DL, Reg, FrameIdx,
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000602 PPC::CRRCRegisterClass, NewMIs);
603
Owen Andersonf6372aa2008-01-01 21:11:32 +0000604 } else if (RC == PPC::VRRCRegisterClass) {
605 // We don't have indexed addressing for vector loads. Emit:
606 // R0 = ADDI FI#
607 // Dest = LVX 0, R0
608 //
609 // FIXME: We use R0 here, because it isn't available for RA.
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000610 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::ADDI), PPC::R0),
Owen Andersonf6372aa2008-01-01 21:11:32 +0000611 FrameIdx, 0, 0));
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000612 NewMIs.push_back(BuildMI(MF, DL, get(PPC::LVX),DestReg).addReg(PPC::R0)
Owen Andersonf6372aa2008-01-01 21:11:32 +0000613 .addReg(PPC::R0));
614 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000615 llvm_unreachable("Unknown regclass!");
Owen Andersonf6372aa2008-01-01 21:11:32 +0000616 }
617}
618
619void
620PPCInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
Bill Wendling7194aaf2008-03-03 22:19:16 +0000621 MachineBasicBlock::iterator MI,
622 unsigned DestReg, int FrameIdx,
623 const TargetRegisterClass *RC) const {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000624 MachineFunction &MF = *MBB.getParent();
Owen Andersonf6372aa2008-01-01 21:11:32 +0000625 SmallVector<MachineInstr*, 4> NewMIs;
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000626 DebugLoc DL = DebugLoc::getUnknownLoc();
627 if (MI != MBB.end()) DL = MI->getDebugLoc();
628 LoadRegFromStackSlot(MF, DL, DestReg, FrameIdx, RC, NewMIs);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000629 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
630 MBB.insert(MI, NewMIs[i]);
631}
632
Owen Anderson43dbe052008-01-07 01:35:02 +0000633/// foldMemoryOperand - PowerPC (like most RISC's) can only fold spills into
634/// copy instructions, turning them into load/store instructions.
Dan Gohmanc54baa22008-12-03 18:43:12 +0000635MachineInstr *PPCInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
636 MachineInstr *MI,
637 const SmallVectorImpl<unsigned> &Ops,
638 int FrameIndex) const {
Owen Anderson43dbe052008-01-07 01:35:02 +0000639 if (Ops.size() != 1) return NULL;
640
641 // Make sure this is a reg-reg copy. Note that we can't handle MCRF, because
642 // it takes more than one instruction to store it.
643 unsigned Opc = MI->getOpcode();
644 unsigned OpNum = Ops[0];
645
646 MachineInstr *NewMI = NULL;
647 if ((Opc == PPC::OR &&
648 MI->getOperand(1).getReg() == MI->getOperand(2).getReg())) {
649 if (OpNum == 0) { // move -> store
650 unsigned InReg = MI->getOperand(1).getReg();
Evan Cheng9f1c8312008-07-03 09:09:37 +0000651 bool isKill = MI->getOperand(1).isKill();
Evan Cheng2578ba22009-07-01 01:59:31 +0000652 bool isUndef = MI->getOperand(1).isUndef();
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000653 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::STW))
Evan Cheng2578ba22009-07-01 01:59:31 +0000654 .addReg(InReg,
655 getKillRegState(isKill) |
656 getUndefRegState(isUndef)),
Owen Anderson43dbe052008-01-07 01:35:02 +0000657 FrameIndex);
658 } else { // move -> load
659 unsigned OutReg = MI->getOperand(0).getReg();
Evan Cheng9f1c8312008-07-03 09:09:37 +0000660 bool isDead = MI->getOperand(0).isDead();
Evan Cheng2578ba22009-07-01 01:59:31 +0000661 bool isUndef = MI->getOperand(0).isUndef();
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000662 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::LWZ))
Bill Wendling587daed2009-05-13 21:33:08 +0000663 .addReg(OutReg,
664 RegState::Define |
Evan Cheng2578ba22009-07-01 01:59:31 +0000665 getDeadRegState(isDead) |
666 getUndefRegState(isUndef)),
Owen Anderson43dbe052008-01-07 01:35:02 +0000667 FrameIndex);
668 }
669 } else if ((Opc == PPC::OR8 &&
670 MI->getOperand(1).getReg() == MI->getOperand(2).getReg())) {
671 if (OpNum == 0) { // move -> store
672 unsigned InReg = MI->getOperand(1).getReg();
Evan Cheng9f1c8312008-07-03 09:09:37 +0000673 bool isKill = MI->getOperand(1).isKill();
Evan Cheng2578ba22009-07-01 01:59:31 +0000674 bool isUndef = MI->getOperand(1).isUndef();
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000675 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::STD))
Evan Cheng2578ba22009-07-01 01:59:31 +0000676 .addReg(InReg,
677 getKillRegState(isKill) |
678 getUndefRegState(isUndef)),
Owen Anderson43dbe052008-01-07 01:35:02 +0000679 FrameIndex);
680 } else { // move -> load
681 unsigned OutReg = MI->getOperand(0).getReg();
Evan Cheng9f1c8312008-07-03 09:09:37 +0000682 bool isDead = MI->getOperand(0).isDead();
Evan Cheng2578ba22009-07-01 01:59:31 +0000683 bool isUndef = MI->getOperand(0).isUndef();
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000684 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::LD))
Bill Wendling587daed2009-05-13 21:33:08 +0000685 .addReg(OutReg,
686 RegState::Define |
Evan Cheng2578ba22009-07-01 01:59:31 +0000687 getDeadRegState(isDead) |
688 getUndefRegState(isUndef)),
Evan Cheng9f1c8312008-07-03 09:09:37 +0000689 FrameIndex);
Owen Anderson43dbe052008-01-07 01:35:02 +0000690 }
691 } else if (Opc == PPC::FMRD) {
692 if (OpNum == 0) { // move -> store
693 unsigned InReg = MI->getOperand(1).getReg();
Evan Cheng9f1c8312008-07-03 09:09:37 +0000694 bool isKill = MI->getOperand(1).isKill();
Evan Cheng2578ba22009-07-01 01:59:31 +0000695 bool isUndef = MI->getOperand(1).isUndef();
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000696 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::STFD))
Evan Cheng2578ba22009-07-01 01:59:31 +0000697 .addReg(InReg,
698 getKillRegState(isKill) |
699 getUndefRegState(isUndef)),
Owen Anderson43dbe052008-01-07 01:35:02 +0000700 FrameIndex);
701 } else { // move -> load
702 unsigned OutReg = MI->getOperand(0).getReg();
Evan Cheng9f1c8312008-07-03 09:09:37 +0000703 bool isDead = MI->getOperand(0).isDead();
Evan Cheng2578ba22009-07-01 01:59:31 +0000704 bool isUndef = MI->getOperand(0).isUndef();
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000705 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::LFD))
Bill Wendling587daed2009-05-13 21:33:08 +0000706 .addReg(OutReg,
707 RegState::Define |
Evan Cheng2578ba22009-07-01 01:59:31 +0000708 getDeadRegState(isDead) |
709 getUndefRegState(isUndef)),
Evan Cheng9f1c8312008-07-03 09:09:37 +0000710 FrameIndex);
Owen Anderson43dbe052008-01-07 01:35:02 +0000711 }
712 } else if (Opc == PPC::FMRS) {
713 if (OpNum == 0) { // move -> store
714 unsigned InReg = MI->getOperand(1).getReg();
Evan Cheng9f1c8312008-07-03 09:09:37 +0000715 bool isKill = MI->getOperand(1).isKill();
Evan Cheng2578ba22009-07-01 01:59:31 +0000716 bool isUndef = MI->getOperand(1).isUndef();
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000717 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::STFS))
Evan Cheng2578ba22009-07-01 01:59:31 +0000718 .addReg(InReg,
719 getKillRegState(isKill) |
720 getUndefRegState(isUndef)),
Owen Anderson43dbe052008-01-07 01:35:02 +0000721 FrameIndex);
722 } else { // move -> load
723 unsigned OutReg = MI->getOperand(0).getReg();
Evan Cheng9f1c8312008-07-03 09:09:37 +0000724 bool isDead = MI->getOperand(0).isDead();
Evan Cheng2578ba22009-07-01 01:59:31 +0000725 bool isUndef = MI->getOperand(0).isUndef();
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000726 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::LFS))
Bill Wendling587daed2009-05-13 21:33:08 +0000727 .addReg(OutReg,
728 RegState::Define |
Evan Cheng2578ba22009-07-01 01:59:31 +0000729 getDeadRegState(isDead) |
730 getUndefRegState(isUndef)),
Evan Cheng9f1c8312008-07-03 09:09:37 +0000731 FrameIndex);
Owen Anderson43dbe052008-01-07 01:35:02 +0000732 }
733 }
734
Owen Anderson43dbe052008-01-07 01:35:02 +0000735 return NewMI;
736}
737
Dan Gohman8e8b8a22008-10-16 01:49:15 +0000738bool PPCInstrInfo::canFoldMemoryOperand(const MachineInstr *MI,
739 const SmallVectorImpl<unsigned> &Ops) const {
Owen Anderson43dbe052008-01-07 01:35:02 +0000740 if (Ops.size() != 1) return false;
741
742 // Make sure this is a reg-reg copy. Note that we can't handle MCRF, because
743 // it takes more than one instruction to store it.
744 unsigned Opc = MI->getOpcode();
745
746 if ((Opc == PPC::OR &&
747 MI->getOperand(1).getReg() == MI->getOperand(2).getReg()))
748 return true;
749 else if ((Opc == PPC::OR8 &&
750 MI->getOperand(1).getReg() == MI->getOperand(2).getReg()))
751 return true;
752 else if (Opc == PPC::FMRD || Opc == PPC::FMRS)
753 return true;
754
755 return false;
756}
757
Owen Andersonf6372aa2008-01-01 21:11:32 +0000758
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000759bool PPCInstrInfo::
Owen Anderson44eb65c2008-08-14 22:49:33 +0000760ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
Chris Lattner7c4fe252006-10-21 06:03:11 +0000761 assert(Cond.size() == 2 && "Invalid PPC branch opcode!");
762 // Leave the CR# the same, but invert the condition.
Chris Lattner18258c62006-11-17 22:37:34 +0000763 Cond[0].setImm(PPC::InvertPredicate((PPC::Predicate)Cond[0].getImm()));
Chris Lattner7c4fe252006-10-21 06:03:11 +0000764 return false;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000765}
Nicolas Geoffray52e724a2008-04-16 20:10:13 +0000766
767/// GetInstSize - Return the number of bytes of code the specified
768/// instruction may be. This returns the maximum number of bytes.
769///
770unsigned PPCInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
771 switch (MI->getOpcode()) {
772 case PPC::INLINEASM: { // Inline Asm: Variable size.
773 const MachineFunction *MF = MI->getParent()->getParent();
774 const char *AsmStr = MI->getOperand(0).getSymbolName();
Chris Lattneraf76e592009-08-22 20:48:53 +0000775 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
Nicolas Geoffray52e724a2008-04-16 20:10:13 +0000776 }
Dan Gohman44066042008-07-01 00:05:16 +0000777 case PPC::DBG_LABEL:
778 case PPC::EH_LABEL:
779 case PPC::GC_LABEL:
Nicolas Geoffray52e724a2008-04-16 20:10:13 +0000780 return 0;
Nicolas Geoffray52e724a2008-04-16 20:10:13 +0000781 default:
782 return 4; // PowerPC instructions are all 4 bytes
783 }
784}