blob: b008a1d2e8061dd2e20c2cc5488d5089ab546f07 [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
Nate Begeman21e463b2005-10-16 05:39:50 +000033bool PPCInstrInfo::isMoveInstr(const MachineInstr& MI,
34 unsigned& sourceReg,
Evan Cheng04ee5a12009-01-20 19:12:24 +000035 unsigned& destReg,
36 unsigned& sourceSubIdx,
37 unsigned& destSubIdx) const {
38 sourceSubIdx = destSubIdx = 0; // No sub-registers.
39
Chris Lattnercc8cd0c2008-01-07 02:48:55 +000040 unsigned oc = MI.getOpcode();
Chris Lattnerb410dc92006-06-20 23:18:58 +000041 if (oc == PPC::OR || oc == PPC::OR8 || oc == PPC::VOR ||
Chris Lattner14c09b82005-10-19 01:50:36 +000042 oc == PPC::OR4To8 || oc == PPC::OR8To4) { // or r1, r2, r2
Evan Cheng1e3417292007-04-25 07:12:14 +000043 assert(MI.getNumOperands() >= 3 &&
Dan Gohmand735b802008-10-03 15:45:36 +000044 MI.getOperand(0).isReg() &&
45 MI.getOperand(1).isReg() &&
46 MI.getOperand(2).isReg() &&
Misha Brukmanf2ccb772004-08-17 04:55:41 +000047 "invalid PPC OR instruction!");
48 if (MI.getOperand(1).getReg() == MI.getOperand(2).getReg()) {
49 sourceReg = MI.getOperand(1).getReg();
50 destReg = MI.getOperand(0).getReg();
51 return true;
52 }
53 } else if (oc == PPC::ADDI) { // addi r1, r2, 0
Evan Cheng1e3417292007-04-25 07:12:14 +000054 assert(MI.getNumOperands() >= 3 &&
Dan Gohmand735b802008-10-03 15:45:36 +000055 MI.getOperand(0).isReg() &&
56 MI.getOperand(2).isImm() &&
Misha Brukmanf2ccb772004-08-17 04:55:41 +000057 "invalid PPC ADDI instruction!");
Dan Gohmand735b802008-10-03 15:45:36 +000058 if (MI.getOperand(1).isReg() && MI.getOperand(2).getImm() == 0) {
Misha Brukmanf2ccb772004-08-17 04:55:41 +000059 sourceReg = MI.getOperand(1).getReg();
60 destReg = MI.getOperand(0).getReg();
61 return true;
62 }
Nate Begemancb90de32004-10-07 22:26:12 +000063 } else if (oc == PPC::ORI) { // ori r1, r2, 0
Evan Cheng1e3417292007-04-25 07:12:14 +000064 assert(MI.getNumOperands() >= 3 &&
Dan Gohmand735b802008-10-03 15:45:36 +000065 MI.getOperand(0).isReg() &&
66 MI.getOperand(1).isReg() &&
67 MI.getOperand(2).isImm() &&
Nate Begemancb90de32004-10-07 22:26:12 +000068 "invalid PPC ORI instruction!");
Chris Lattner9a1ceae2007-12-30 20:49:49 +000069 if (MI.getOperand(2).getImm() == 0) {
Nate Begemancb90de32004-10-07 22:26:12 +000070 sourceReg = MI.getOperand(1).getReg();
71 destReg = MI.getOperand(0).getReg();
72 return true;
73 }
Chris Lattnereb5d47d2005-10-07 05:00:52 +000074 } else if (oc == PPC::FMRS || oc == PPC::FMRD ||
75 oc == PPC::FMRSD) { // fmr r1, r2
Evan Cheng1e3417292007-04-25 07:12:14 +000076 assert(MI.getNumOperands() >= 2 &&
Dan Gohmand735b802008-10-03 15:45:36 +000077 MI.getOperand(0).isReg() &&
78 MI.getOperand(1).isReg() &&
Misha Brukmanf2ccb772004-08-17 04:55:41 +000079 "invalid PPC FMR instruction");
80 sourceReg = MI.getOperand(1).getReg();
81 destReg = MI.getOperand(0).getReg();
82 return true;
Nate Begeman7af02482005-04-12 07:04:16 +000083 } else if (oc == PPC::MCRF) { // mcrf cr1, cr2
Evan Cheng1e3417292007-04-25 07:12:14 +000084 assert(MI.getNumOperands() >= 2 &&
Dan Gohmand735b802008-10-03 15:45:36 +000085 MI.getOperand(0).isReg() &&
86 MI.getOperand(1).isReg() &&
Nate Begeman7af02482005-04-12 07:04:16 +000087 "invalid PPC MCRF instruction");
88 sourceReg = MI.getOperand(1).getReg();
89 destReg = MI.getOperand(0).getReg();
90 return true;
Misha Brukmanf2ccb772004-08-17 04:55:41 +000091 }
92 return false;
93}
Chris Lattner043870d2005-09-09 18:17:41 +000094
Dan Gohmancbad42c2008-11-18 19:49:32 +000095unsigned PPCInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
Chris Lattner9c09c9e2006-03-16 22:24:02 +000096 int &FrameIndex) const {
Chris Lattner40839602006-02-02 20:12:32 +000097 switch (MI->getOpcode()) {
98 default: break;
99 case PPC::LD:
100 case PPC::LWZ:
101 case PPC::LFS:
102 case PPC::LFD:
Dan Gohmand735b802008-10-03 15:45:36 +0000103 if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
104 MI->getOperand(2).isFI()) {
Chris Lattner8aa797a2007-12-30 23:10:15 +0000105 FrameIndex = MI->getOperand(2).getIndex();
Chris Lattner40839602006-02-02 20:12:32 +0000106 return MI->getOperand(0).getReg();
107 }
108 break;
109 }
110 return 0;
Chris Lattner65242872006-02-02 20:16:12 +0000111}
Chris Lattner40839602006-02-02 20:12:32 +0000112
Dan Gohmancbad42c2008-11-18 19:49:32 +0000113unsigned PPCInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
Chris Lattner65242872006-02-02 20:16:12 +0000114 int &FrameIndex) const {
115 switch (MI->getOpcode()) {
116 default: break;
Nate Begeman3b478b32006-02-02 21:07:50 +0000117 case PPC::STD:
Chris Lattner65242872006-02-02 20:16:12 +0000118 case PPC::STW:
119 case PPC::STFS:
120 case PPC::STFD:
Dan Gohmand735b802008-10-03 15:45:36 +0000121 if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
122 MI->getOperand(2).isFI()) {
Chris Lattner8aa797a2007-12-30 23:10:15 +0000123 FrameIndex = MI->getOperand(2).getIndex();
Chris Lattner65242872006-02-02 20:16:12 +0000124 return MI->getOperand(0).getReg();
125 }
126 break;
127 }
128 return 0;
129}
Chris Lattner40839602006-02-02 20:12:32 +0000130
Chris Lattner043870d2005-09-09 18:17:41 +0000131// commuteInstruction - We can commute rlwimi instructions, but only if the
132// rotate amt is zero. We also have to munge the immediates a bit.
Evan Cheng58dcb0e2008-06-16 07:33:11 +0000133MachineInstr *
134PPCInstrInfo::commuteInstruction(MachineInstr *MI, bool NewMI) const {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000135 MachineFunction &MF = *MI->getParent()->getParent();
136
Chris Lattner043870d2005-09-09 18:17:41 +0000137 // Normal instructions can be commuted the obvious way.
138 if (MI->getOpcode() != PPC::RLWIMI)
Evan Cheng58dcb0e2008-06-16 07:33:11 +0000139 return TargetInstrInfoImpl::commuteInstruction(MI, NewMI);
Chris Lattner043870d2005-09-09 18:17:41 +0000140
141 // Cannot commute if it has a non-zero rotate count.
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000142 if (MI->getOperand(3).getImm() != 0)
Chris Lattner043870d2005-09-09 18:17:41 +0000143 return 0;
144
145 // If we have a zero rotate count, we have:
146 // M = mask(MB,ME)
147 // Op0 = (Op1 & ~M) | (Op2 & M)
148 // Change this to:
149 // M = mask((ME+1)&31, (MB-1)&31)
150 // Op0 = (Op2 & ~M) | (Op1 & M)
151
152 // Swap op1/op2
Evan Chenga4d16a12008-02-13 02:46:49 +0000153 unsigned Reg0 = MI->getOperand(0).getReg();
Chris Lattner043870d2005-09-09 18:17:41 +0000154 unsigned Reg1 = MI->getOperand(1).getReg();
155 unsigned Reg2 = MI->getOperand(2).getReg();
Evan Cheng6ce7dc22006-11-15 20:58:11 +0000156 bool Reg1IsKill = MI->getOperand(1).isKill();
157 bool Reg2IsKill = MI->getOperand(2).isKill();
Evan Cheng58dcb0e2008-06-16 07:33:11 +0000158 bool ChangeReg0 = false;
Evan Chenga4d16a12008-02-13 02:46:49 +0000159 // If machine instrs are no longer in two-address forms, update
160 // destination register as well.
161 if (Reg0 == Reg1) {
162 // Must be two address instruction!
163 assert(MI->getDesc().getOperandConstraint(0, TOI::TIED_TO) &&
164 "Expecting a two-address instruction!");
Evan Chenga4d16a12008-02-13 02:46:49 +0000165 Reg2IsKill = false;
Evan Cheng58dcb0e2008-06-16 07:33:11 +0000166 ChangeReg0 = true;
Evan Chenga4d16a12008-02-13 02:46:49 +0000167 }
Evan Cheng58dcb0e2008-06-16 07:33:11 +0000168
169 // Masks.
170 unsigned MB = MI->getOperand(4).getImm();
171 unsigned ME = MI->getOperand(5).getImm();
172
173 if (NewMI) {
174 // Create a new instruction.
175 unsigned Reg0 = ChangeReg0 ? Reg2 : MI->getOperand(0).getReg();
176 bool Reg0IsDead = MI->getOperand(0).isDead();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000177 return BuildMI(MF, MI->getDesc())
178 .addReg(Reg0, true, false, false, Reg0IsDead)
Evan Cheng58dcb0e2008-06-16 07:33:11 +0000179 .addReg(Reg2, false, false, Reg2IsKill)
180 .addReg(Reg1, false, false, Reg1IsKill)
181 .addImm((ME+1) & 31)
182 .addImm((MB-1) & 31);
183 }
184
185 if (ChangeReg0)
186 MI->getOperand(0).setReg(Reg2);
Chris Lattnere53f4a02006-05-04 17:52:23 +0000187 MI->getOperand(2).setReg(Reg1);
188 MI->getOperand(1).setReg(Reg2);
Chris Lattnerf7382302007-12-30 21:56:09 +0000189 MI->getOperand(2).setIsKill(Reg1IsKill);
190 MI->getOperand(1).setIsKill(Reg2IsKill);
Chris Lattner043870d2005-09-09 18:17:41 +0000191
192 // Swap the mask around.
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000193 MI->getOperand(4).setImm((ME+1) & 31);
194 MI->getOperand(5).setImm((MB-1) & 31);
Chris Lattner043870d2005-09-09 18:17:41 +0000195 return MI;
196}
Chris Lattnerbbf1c722006-03-05 23:49:55 +0000197
198void PPCInstrInfo::insertNoop(MachineBasicBlock &MBB,
199 MachineBasicBlock::iterator MI) const {
Evan Chengc0f64ff2006-11-27 23:37:22 +0000200 BuildMI(MBB, MI, get(PPC::NOP));
Chris Lattnerbbf1c722006-03-05 23:49:55 +0000201}
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000202
203
204// Branch analysis.
205bool PPCInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
206 MachineBasicBlock *&FBB,
Owen Anderson44eb65c2008-08-14 22:49:33 +0000207 SmallVectorImpl<MachineOperand> &Cond) const {
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000208 // If the block has no terminators, it just falls into the block after it.
209 MachineBasicBlock::iterator I = MBB.end();
Evan Chengbfd2ec42007-06-08 21:59:56 +0000210 if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000211 return false;
212
213 // Get the last instruction in the block.
214 MachineInstr *LastInst = I;
215
216 // If there is only one terminator instruction, process it.
Evan Chengbfd2ec42007-06-08 21:59:56 +0000217 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000218 if (LastInst->getOpcode() == PPC::B) {
Chris Lattner8aa797a2007-12-30 23:10:15 +0000219 TBB = LastInst->getOperand(0).getMBB();
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000220 return false;
Chris Lattner289c2d52006-11-17 22:14:47 +0000221 } else if (LastInst->getOpcode() == PPC::BCC) {
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000222 // Block ends with fall-through condbranch.
Chris Lattner8aa797a2007-12-30 23:10:15 +0000223 TBB = LastInst->getOperand(2).getMBB();
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000224 Cond.push_back(LastInst->getOperand(0));
225 Cond.push_back(LastInst->getOperand(1));
Chris Lattner7c4fe252006-10-21 06:03:11 +0000226 return false;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000227 }
228 // Otherwise, don't know what this is.
229 return true;
230 }
231
232 // Get the instruction before it if it's a terminator.
233 MachineInstr *SecondLastInst = I;
234
235 // If there are three terminators, we don't know what sort of block this is.
236 if (SecondLastInst && I != MBB.begin() &&
Evan Chengbfd2ec42007-06-08 21:59:56 +0000237 isUnpredicatedTerminator(--I))
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000238 return true;
239
Chris Lattner289c2d52006-11-17 22:14:47 +0000240 // If the block ends with PPC::B and PPC:BCC, handle it.
241 if (SecondLastInst->getOpcode() == PPC::BCC &&
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000242 LastInst->getOpcode() == PPC::B) {
Chris Lattner8aa797a2007-12-30 23:10:15 +0000243 TBB = SecondLastInst->getOperand(2).getMBB();
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000244 Cond.push_back(SecondLastInst->getOperand(0));
245 Cond.push_back(SecondLastInst->getOperand(1));
Chris Lattner8aa797a2007-12-30 23:10:15 +0000246 FBB = LastInst->getOperand(0).getMBB();
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000247 return false;
248 }
249
Dale Johannesen13e8b512007-06-13 17:59:52 +0000250 // If the block ends with two PPC:Bs, handle it. The second one is not
251 // executed, so remove it.
252 if (SecondLastInst->getOpcode() == PPC::B &&
253 LastInst->getOpcode() == PPC::B) {
Chris Lattner8aa797a2007-12-30 23:10:15 +0000254 TBB = SecondLastInst->getOperand(0).getMBB();
Dale Johannesen13e8b512007-06-13 17:59:52 +0000255 I = LastInst;
256 I->eraseFromParent();
257 return false;
258 }
259
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000260 // Otherwise, can't handle this.
261 return true;
262}
263
Evan Chengb5cdaa22007-05-18 00:05:48 +0000264unsigned PPCInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000265 MachineBasicBlock::iterator I = MBB.end();
Evan Chengb5cdaa22007-05-18 00:05:48 +0000266 if (I == MBB.begin()) return 0;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000267 --I;
Chris Lattner289c2d52006-11-17 22:14:47 +0000268 if (I->getOpcode() != PPC::B && I->getOpcode() != PPC::BCC)
Evan Chengb5cdaa22007-05-18 00:05:48 +0000269 return 0;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000270
271 // Remove the branch.
272 I->eraseFromParent();
273
274 I = MBB.end();
275
Evan Chengb5cdaa22007-05-18 00:05:48 +0000276 if (I == MBB.begin()) return 1;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000277 --I;
Chris Lattner289c2d52006-11-17 22:14:47 +0000278 if (I->getOpcode() != PPC::BCC)
Evan Chengb5cdaa22007-05-18 00:05:48 +0000279 return 1;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000280
281 // Remove the branch.
282 I->eraseFromParent();
Evan Chengb5cdaa22007-05-18 00:05:48 +0000283 return 2;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000284}
285
Evan Chengb5cdaa22007-05-18 00:05:48 +0000286unsigned
287PPCInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
288 MachineBasicBlock *FBB,
Owen Anderson44eb65c2008-08-14 22:49:33 +0000289 const SmallVectorImpl<MachineOperand> &Cond) const {
Chris Lattner2dc77232006-10-17 18:06:55 +0000290 // Shouldn't be a fall through.
291 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Chris Lattner54108062006-10-21 05:36:13 +0000292 assert((Cond.size() == 2 || Cond.size() == 0) &&
293 "PPC branch conditions have two components!");
Chris Lattner2dc77232006-10-17 18:06:55 +0000294
Chris Lattner54108062006-10-21 05:36:13 +0000295 // One-way branch.
Chris Lattner2dc77232006-10-17 18:06:55 +0000296 if (FBB == 0) {
Chris Lattner54108062006-10-21 05:36:13 +0000297 if (Cond.empty()) // Unconditional branch
Evan Chengc0f64ff2006-11-27 23:37:22 +0000298 BuildMI(&MBB, get(PPC::B)).addMBB(TBB);
Chris Lattner54108062006-10-21 05:36:13 +0000299 else // Conditional branch
Evan Chengc0f64ff2006-11-27 23:37:22 +0000300 BuildMI(&MBB, get(PPC::BCC))
Chris Lattner18258c62006-11-17 22:37:34 +0000301 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
Evan Chengb5cdaa22007-05-18 00:05:48 +0000302 return 1;
Chris Lattner2dc77232006-10-17 18:06:55 +0000303 }
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000304
Chris Lattner879d09c2006-10-21 05:42:09 +0000305 // Two-way 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 Chengc0f64ff2006-11-27 23:37:22 +0000308 BuildMI(&MBB, get(PPC::B)).addMBB(FBB);
Evan Chengb5cdaa22007-05-18 00:05:48 +0000309 return 2;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000310}
311
Owen Anderson940f83e2008-08-26 18:03:31 +0000312bool PPCInstrInfo::copyRegToReg(MachineBasicBlock &MBB,
Owen Andersond10fd972007-12-31 06:32:00 +0000313 MachineBasicBlock::iterator MI,
314 unsigned DestReg, unsigned SrcReg,
315 const TargetRegisterClass *DestRC,
316 const TargetRegisterClass *SrcRC) const {
317 if (DestRC != SrcRC) {
Owen Anderson940f83e2008-08-26 18:03:31 +0000318 // Not yet supported!
319 return false;
Owen Andersond10fd972007-12-31 06:32:00 +0000320 }
321
322 if (DestRC == PPC::GPRCRegisterClass) {
323 BuildMI(MBB, MI, get(PPC::OR), DestReg).addReg(SrcReg).addReg(SrcReg);
324 } else if (DestRC == PPC::G8RCRegisterClass) {
325 BuildMI(MBB, MI, get(PPC::OR8), DestReg).addReg(SrcReg).addReg(SrcReg);
326 } else if (DestRC == PPC::F4RCRegisterClass) {
327 BuildMI(MBB, MI, get(PPC::FMRS), DestReg).addReg(SrcReg);
328 } else if (DestRC == PPC::F8RCRegisterClass) {
329 BuildMI(MBB, MI, get(PPC::FMRD), DestReg).addReg(SrcReg);
330 } else if (DestRC == PPC::CRRCRegisterClass) {
331 BuildMI(MBB, MI, get(PPC::MCRF), DestReg).addReg(SrcReg);
332 } else if (DestRC == PPC::VRRCRegisterClass) {
333 BuildMI(MBB, MI, get(PPC::VOR), DestReg).addReg(SrcReg).addReg(SrcReg);
Nicolas Geoffray0404cd92008-03-10 14:12:10 +0000334 } else if (DestRC == PPC::CRBITRCRegisterClass) {
335 BuildMI(MBB, MI, get(PPC::CROR), DestReg).addReg(SrcReg).addReg(SrcReg);
Owen Andersond10fd972007-12-31 06:32:00 +0000336 } else {
Owen Anderson940f83e2008-08-26 18:03:31 +0000337 // Attempt to copy register that is not GPR or FPR
338 return false;
Owen Andersond10fd972007-12-31 06:32:00 +0000339 }
Owen Anderson940f83e2008-08-26 18:03:31 +0000340
341 return true;
Owen Andersond10fd972007-12-31 06:32:00 +0000342}
343
Bill Wendling4a66e9a2008-03-10 22:49:16 +0000344bool
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000345PPCInstrInfo::StoreRegToStackSlot(MachineFunction &MF,
346 unsigned SrcReg, bool isKill,
Bill Wendling4a66e9a2008-03-10 22:49:16 +0000347 int FrameIdx,
348 const TargetRegisterClass *RC,
349 SmallVectorImpl<MachineInstr*> &NewMIs) const{
Owen Andersonf6372aa2008-01-01 21:11:32 +0000350 if (RC == PPC::GPRCRegisterClass) {
351 if (SrcReg != PPC::LR) {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000352 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::STW))
Bill Wendling4a66e9a2008-03-10 22:49:16 +0000353 .addReg(SrcReg, false, false, isKill),
354 FrameIdx));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000355 } else {
356 // FIXME: this spills LR immediately to memory in one step. To do this,
357 // we use R11, which we know cannot be used in the prolog/epilog. This is
358 // a hack.
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000359 NewMIs.push_back(BuildMI(MF, get(PPC::MFLR), PPC::R11));
360 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::STW))
Bill Wendling4a66e9a2008-03-10 22:49:16 +0000361 .addReg(PPC::R11, false, false, isKill),
362 FrameIdx));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000363 }
364 } else if (RC == PPC::G8RCRegisterClass) {
365 if (SrcReg != PPC::LR8) {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000366 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::STD))
Chris Lattnercb341de2008-03-10 18:55:53 +0000367 .addReg(SrcReg, false, false, isKill), FrameIdx));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000368 } else {
369 // FIXME: this spills LR immediately to memory in one step. To do this,
370 // we use R11, which we know cannot be used in the prolog/epilog. This is
371 // a hack.
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000372 NewMIs.push_back(BuildMI(MF, get(PPC::MFLR8), PPC::X11));
373 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::STD))
Chris Lattnercb341de2008-03-10 18:55:53 +0000374 .addReg(PPC::X11, false, false, isKill), FrameIdx));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000375 }
376 } else if (RC == PPC::F8RCRegisterClass) {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000377 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::STFD))
Chris Lattnercb341de2008-03-10 18:55:53 +0000378 .addReg(SrcReg, false, false, isKill), FrameIdx));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000379 } else if (RC == PPC::F4RCRegisterClass) {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000380 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::STFS))
Chris Lattnercb341de2008-03-10 18:55:53 +0000381 .addReg(SrcReg, false, false, isKill), FrameIdx));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000382 } else if (RC == PPC::CRRCRegisterClass) {
Bill Wendling4a66e9a2008-03-10 22:49:16 +0000383 if ((EnablePPC32RS && !TM.getSubtargetImpl()->isPPC64()) ||
384 (EnablePPC64RS && TM.getSubtargetImpl()->isPPC64())) {
385 // FIXME (64-bit): Enable
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000386 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::SPILL_CR))
Bill Wendling7194aaf2008-03-03 22:19:16 +0000387 .addReg(SrcReg, false, false, isKill),
Chris Lattner71a2cb22008-03-20 01:22:40 +0000388 FrameIdx));
Bill Wendling7194aaf2008-03-03 22:19:16 +0000389 return true;
390 } else {
391 // FIXME: We use R0 here, because it isn't available for RA. We need to
392 // store the CR in the low 4-bits of the saved value. First, issue a MFCR
393 // to save all of the CRBits.
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000394 NewMIs.push_back(BuildMI(MF, get(PPC::MFCR), PPC::R0));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000395
Bill Wendling7194aaf2008-03-03 22:19:16 +0000396 // If the saved register wasn't CR0, shift the bits left so that they are
397 // in CR0's slot.
398 if (SrcReg != PPC::CR0) {
399 unsigned ShiftBits = PPCRegisterInfo::getRegisterNumbering(SrcReg)*4;
400 // rlwinm r0, r0, ShiftBits, 0, 31.
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000401 NewMIs.push_back(BuildMI(MF, get(PPC::RLWINM), PPC::R0)
Chris Lattnercb341de2008-03-10 18:55:53 +0000402 .addReg(PPC::R0).addImm(ShiftBits).addImm(0).addImm(31));
Bill Wendling7194aaf2008-03-03 22:19:16 +0000403 }
404
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000405 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::STW))
Bill Wendling7194aaf2008-03-03 22:19:16 +0000406 .addReg(PPC::R0, false, false, isKill),
407 FrameIdx));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000408 }
Nicolas Geoffray0404cd92008-03-10 14:12:10 +0000409 } else if (RC == PPC::CRBITRCRegisterClass) {
410 // FIXME: We use CRi here because there is no mtcrf on a bit. Since the
411 // backend currently only uses CR1EQ as an individual bit, this should
412 // not cause any bug. If we need other uses of CR bits, the following
413 // code may be invalid.
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000414 unsigned Reg = 0;
Nicolas Geoffray0404cd92008-03-10 14:12:10 +0000415 if (SrcReg >= PPC::CR0LT || SrcReg <= PPC::CR0UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000416 Reg = PPC::CR0;
Nicolas Geoffray0404cd92008-03-10 14:12:10 +0000417 else if (SrcReg >= PPC::CR1LT || SrcReg <= PPC::CR1UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000418 Reg = PPC::CR1;
419 else if (SrcReg >= PPC::CR2LT || SrcReg <= PPC::CR2UN)
420 Reg = PPC::CR2;
421 else if (SrcReg >= PPC::CR3LT || SrcReg <= PPC::CR3UN)
422 Reg = PPC::CR3;
423 else if (SrcReg >= PPC::CR4LT || SrcReg <= PPC::CR4UN)
424 Reg = PPC::CR4;
425 else if (SrcReg >= PPC::CR5LT || SrcReg <= PPC::CR5UN)
426 Reg = PPC::CR5;
427 else if (SrcReg >= PPC::CR6LT || SrcReg <= PPC::CR6UN)
428 Reg = PPC::CR6;
429 else if (SrcReg >= PPC::CR7LT || SrcReg <= PPC::CR7UN)
430 Reg = PPC::CR7;
431
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000432 return StoreRegToStackSlot(MF, Reg, isKill, FrameIdx,
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000433 PPC::CRRCRegisterClass, NewMIs);
434
Owen Andersonf6372aa2008-01-01 21:11:32 +0000435 } else if (RC == PPC::VRRCRegisterClass) {
436 // We don't have indexed addressing for vector loads. Emit:
437 // R0 = ADDI FI#
438 // STVX VAL, 0, R0
439 //
440 // FIXME: We use R0 here, because it isn't available for RA.
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000441 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::ADDI), PPC::R0),
Owen Andersonf6372aa2008-01-01 21:11:32 +0000442 FrameIdx, 0, 0));
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000443 NewMIs.push_back(BuildMI(MF, get(PPC::STVX))
Chris Lattnercb341de2008-03-10 18:55:53 +0000444 .addReg(SrcReg, false, false, isKill).addReg(PPC::R0).addReg(PPC::R0));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000445 } else {
446 assert(0 && "Unknown regclass!");
447 abort();
448 }
Bill Wendling7194aaf2008-03-03 22:19:16 +0000449
450 return false;
Owen Andersonf6372aa2008-01-01 21:11:32 +0000451}
452
453void
454PPCInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
Bill Wendling7194aaf2008-03-03 22:19:16 +0000455 MachineBasicBlock::iterator MI,
456 unsigned SrcReg, bool isKill, int FrameIdx,
457 const TargetRegisterClass *RC) const {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000458 MachineFunction &MF = *MBB.getParent();
Owen Andersonf6372aa2008-01-01 21:11:32 +0000459 SmallVector<MachineInstr*, 4> NewMIs;
Bill Wendling7194aaf2008-03-03 22:19:16 +0000460
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000461 if (StoreRegToStackSlot(MF, SrcReg, isKill, FrameIdx, RC, NewMIs)) {
462 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Bill Wendling7194aaf2008-03-03 22:19:16 +0000463 FuncInfo->setSpillsCR();
464 }
465
Owen Andersonf6372aa2008-01-01 21:11:32 +0000466 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
467 MBB.insert(MI, NewMIs[i]);
468}
469
470void PPCInstrInfo::storeRegToAddr(MachineFunction &MF, unsigned SrcReg,
Bill Wendling7194aaf2008-03-03 22:19:16 +0000471 bool isKill,
472 SmallVectorImpl<MachineOperand> &Addr,
473 const TargetRegisterClass *RC,
474 SmallVectorImpl<MachineInstr*> &NewMIs) const{
Dan Gohmand735b802008-10-03 15:45:36 +0000475 if (Addr[0].isFI()) {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000476 if (StoreRegToStackSlot(MF, SrcReg, isKill,
477 Addr[0].getIndex(), RC, NewMIs)) {
Bill Wendling7194aaf2008-03-03 22:19:16 +0000478 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
479 FuncInfo->setSpillsCR();
480 }
481
Owen Andersonf6372aa2008-01-01 21:11:32 +0000482 return;
483 }
484
485 unsigned Opc = 0;
486 if (RC == PPC::GPRCRegisterClass) {
487 Opc = PPC::STW;
488 } else if (RC == PPC::G8RCRegisterClass) {
489 Opc = PPC::STD;
490 } else if (RC == PPC::F8RCRegisterClass) {
491 Opc = PPC::STFD;
492 } else if (RC == PPC::F4RCRegisterClass) {
493 Opc = PPC::STFS;
494 } else if (RC == PPC::VRRCRegisterClass) {
495 Opc = PPC::STVX;
496 } else {
497 assert(0 && "Unknown regclass!");
498 abort();
499 }
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000500 MachineInstrBuilder MIB = BuildMI(MF, get(Opc))
Owen Andersonf6372aa2008-01-01 21:11:32 +0000501 .addReg(SrcReg, false, false, isKill);
502 for (unsigned i = 0, e = Addr.size(); i != e; ++i) {
503 MachineOperand &MO = Addr[i];
Dan Gohmand735b802008-10-03 15:45:36 +0000504 if (MO.isReg())
Owen Andersonf6372aa2008-01-01 21:11:32 +0000505 MIB.addReg(MO.getReg());
Dan Gohmand735b802008-10-03 15:45:36 +0000506 else if (MO.isImm())
Owen Andersonf6372aa2008-01-01 21:11:32 +0000507 MIB.addImm(MO.getImm());
508 else
509 MIB.addFrameIndex(MO.getIndex());
510 }
511 NewMIs.push_back(MIB);
512 return;
513}
514
Bill Wendling4a66e9a2008-03-10 22:49:16 +0000515void
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000516PPCInstrInfo::LoadRegFromStackSlot(MachineFunction &MF,
517 unsigned DestReg, int FrameIdx,
Bill Wendling4a66e9a2008-03-10 22:49:16 +0000518 const TargetRegisterClass *RC,
519 SmallVectorImpl<MachineInstr*> &NewMIs)const{
Owen Andersonf6372aa2008-01-01 21:11:32 +0000520 if (RC == PPC::GPRCRegisterClass) {
521 if (DestReg != PPC::LR) {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000522 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::LWZ), DestReg),
Owen Andersonf6372aa2008-01-01 21:11:32 +0000523 FrameIdx));
524 } else {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000525 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::LWZ), PPC::R11),
Owen Andersonf6372aa2008-01-01 21:11:32 +0000526 FrameIdx));
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000527 NewMIs.push_back(BuildMI(MF, get(PPC::MTLR)).addReg(PPC::R11));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000528 }
529 } else if (RC == PPC::G8RCRegisterClass) {
530 if (DestReg != PPC::LR8) {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000531 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::LD), DestReg),
Owen Andersonf6372aa2008-01-01 21:11:32 +0000532 FrameIdx));
533 } else {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000534 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::LD), PPC::R11),
Owen Andersonf6372aa2008-01-01 21:11:32 +0000535 FrameIdx));
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000536 NewMIs.push_back(BuildMI(MF, get(PPC::MTLR8)).addReg(PPC::R11));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000537 }
538 } else if (RC == PPC::F8RCRegisterClass) {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000539 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::LFD), DestReg),
Owen Andersonf6372aa2008-01-01 21:11:32 +0000540 FrameIdx));
541 } else if (RC == PPC::F4RCRegisterClass) {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000542 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::LFS), DestReg),
Owen Andersonf6372aa2008-01-01 21:11:32 +0000543 FrameIdx));
544 } else if (RC == PPC::CRRCRegisterClass) {
545 // FIXME: We use R0 here, because it isn't available for RA.
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000546 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::LWZ), PPC::R0),
Owen Andersonf6372aa2008-01-01 21:11:32 +0000547 FrameIdx));
548
549 // If the reloaded register isn't CR0, shift the bits right so that they are
550 // in the right CR's slot.
551 if (DestReg != PPC::CR0) {
552 unsigned ShiftBits = PPCRegisterInfo::getRegisterNumbering(DestReg)*4;
553 // rlwinm r11, r11, 32-ShiftBits, 0, 31.
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000554 NewMIs.push_back(BuildMI(MF, get(PPC::RLWINM), PPC::R0)
Owen Andersonf6372aa2008-01-01 21:11:32 +0000555 .addReg(PPC::R0).addImm(32-ShiftBits).addImm(0).addImm(31));
556 }
557
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000558 NewMIs.push_back(BuildMI(MF, get(PPC::MTCRF), DestReg).addReg(PPC::R0));
Nicolas Geoffray0404cd92008-03-10 14:12:10 +0000559 } else if (RC == PPC::CRBITRCRegisterClass) {
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000560
561 unsigned Reg = 0;
Nicolas Geoffray0404cd92008-03-10 14:12:10 +0000562 if (DestReg >= PPC::CR0LT || DestReg <= PPC::CR0UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000563 Reg = PPC::CR0;
Nicolas Geoffray0404cd92008-03-10 14:12:10 +0000564 else if (DestReg >= PPC::CR1LT || DestReg <= PPC::CR1UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000565 Reg = PPC::CR1;
566 else if (DestReg >= PPC::CR2LT || DestReg <= PPC::CR2UN)
567 Reg = PPC::CR2;
568 else if (DestReg >= PPC::CR3LT || DestReg <= PPC::CR3UN)
569 Reg = PPC::CR3;
570 else if (DestReg >= PPC::CR4LT || DestReg <= PPC::CR4UN)
571 Reg = PPC::CR4;
572 else if (DestReg >= PPC::CR5LT || DestReg <= PPC::CR5UN)
573 Reg = PPC::CR5;
574 else if (DestReg >= PPC::CR6LT || DestReg <= PPC::CR6UN)
575 Reg = PPC::CR6;
576 else if (DestReg >= PPC::CR7LT || DestReg <= PPC::CR7UN)
577 Reg = PPC::CR7;
578
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000579 return LoadRegFromStackSlot(MF, Reg, FrameIdx,
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000580 PPC::CRRCRegisterClass, NewMIs);
581
Owen Andersonf6372aa2008-01-01 21:11:32 +0000582 } else if (RC == PPC::VRRCRegisterClass) {
583 // We don't have indexed addressing for vector loads. Emit:
584 // R0 = ADDI FI#
585 // Dest = LVX 0, R0
586 //
587 // FIXME: We use R0 here, because it isn't available for RA.
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000588 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::ADDI), PPC::R0),
Owen Andersonf6372aa2008-01-01 21:11:32 +0000589 FrameIdx, 0, 0));
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000590 NewMIs.push_back(BuildMI(MF, get(PPC::LVX),DestReg).addReg(PPC::R0)
Owen Andersonf6372aa2008-01-01 21:11:32 +0000591 .addReg(PPC::R0));
592 } else {
593 assert(0 && "Unknown regclass!");
594 abort();
595 }
596}
597
598void
599PPCInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
Bill Wendling7194aaf2008-03-03 22:19:16 +0000600 MachineBasicBlock::iterator MI,
601 unsigned DestReg, int FrameIdx,
602 const TargetRegisterClass *RC) const {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000603 MachineFunction &MF = *MBB.getParent();
Owen Andersonf6372aa2008-01-01 21:11:32 +0000604 SmallVector<MachineInstr*, 4> NewMIs;
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000605 LoadRegFromStackSlot(MF, DestReg, FrameIdx, RC, NewMIs);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000606 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
607 MBB.insert(MI, NewMIs[i]);
608}
609
610void PPCInstrInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
Bill Wendling7194aaf2008-03-03 22:19:16 +0000611 SmallVectorImpl<MachineOperand> &Addr,
612 const TargetRegisterClass *RC,
613 SmallVectorImpl<MachineInstr*> &NewMIs)const{
Dan Gohmand735b802008-10-03 15:45:36 +0000614 if (Addr[0].isFI()) {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000615 LoadRegFromStackSlot(MF, DestReg, Addr[0].getIndex(), RC, NewMIs);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000616 return;
617 }
618
619 unsigned Opc = 0;
620 if (RC == PPC::GPRCRegisterClass) {
621 assert(DestReg != PPC::LR && "Can't handle this yet!");
622 Opc = PPC::LWZ;
623 } else if (RC == PPC::G8RCRegisterClass) {
624 assert(DestReg != PPC::LR8 && "Can't handle this yet!");
625 Opc = PPC::LD;
626 } else if (RC == PPC::F8RCRegisterClass) {
627 Opc = PPC::LFD;
628 } else if (RC == PPC::F4RCRegisterClass) {
629 Opc = PPC::LFS;
630 } else if (RC == PPC::VRRCRegisterClass) {
631 Opc = PPC::LVX;
632 } else {
633 assert(0 && "Unknown regclass!");
634 abort();
635 }
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000636 MachineInstrBuilder MIB = BuildMI(MF, get(Opc), DestReg);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000637 for (unsigned i = 0, e = Addr.size(); i != e; ++i) {
638 MachineOperand &MO = Addr[i];
Dan Gohmand735b802008-10-03 15:45:36 +0000639 if (MO.isReg())
Owen Andersonf6372aa2008-01-01 21:11:32 +0000640 MIB.addReg(MO.getReg());
Dan Gohmand735b802008-10-03 15:45:36 +0000641 else if (MO.isImm())
Owen Andersonf6372aa2008-01-01 21:11:32 +0000642 MIB.addImm(MO.getImm());
643 else
644 MIB.addFrameIndex(MO.getIndex());
645 }
646 NewMIs.push_back(MIB);
647 return;
648}
649
Owen Anderson43dbe052008-01-07 01:35:02 +0000650/// foldMemoryOperand - PowerPC (like most RISC's) can only fold spills into
651/// copy instructions, turning them into load/store instructions.
Dan Gohmanc54baa22008-12-03 18:43:12 +0000652MachineInstr *PPCInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
653 MachineInstr *MI,
654 const SmallVectorImpl<unsigned> &Ops,
655 int FrameIndex) const {
Owen Anderson43dbe052008-01-07 01:35:02 +0000656 if (Ops.size() != 1) return NULL;
657
658 // Make sure this is a reg-reg copy. Note that we can't handle MCRF, because
659 // it takes more than one instruction to store it.
660 unsigned Opc = MI->getOpcode();
661 unsigned OpNum = Ops[0];
662
663 MachineInstr *NewMI = NULL;
664 if ((Opc == PPC::OR &&
665 MI->getOperand(1).getReg() == MI->getOperand(2).getReg())) {
666 if (OpNum == 0) { // move -> store
667 unsigned InReg = MI->getOperand(1).getReg();
Evan Cheng9f1c8312008-07-03 09:09:37 +0000668 bool isKill = MI->getOperand(1).isKill();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000669 NewMI = addFrameReference(BuildMI(MF, get(PPC::STW))
Evan Cheng9f1c8312008-07-03 09:09:37 +0000670 .addReg(InReg, false, false, isKill),
Owen Anderson43dbe052008-01-07 01:35:02 +0000671 FrameIndex);
672 } else { // move -> load
673 unsigned OutReg = MI->getOperand(0).getReg();
Evan Cheng9f1c8312008-07-03 09:09:37 +0000674 bool isDead = MI->getOperand(0).isDead();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000675 NewMI = addFrameReference(BuildMI(MF, get(PPC::LWZ))
Evan Cheng9f1c8312008-07-03 09:09:37 +0000676 .addReg(OutReg, true, false, false, isDead),
Owen Anderson43dbe052008-01-07 01:35:02 +0000677 FrameIndex);
678 }
679 } else if ((Opc == PPC::OR8 &&
680 MI->getOperand(1).getReg() == MI->getOperand(2).getReg())) {
681 if (OpNum == 0) { // move -> store
682 unsigned InReg = MI->getOperand(1).getReg();
Evan Cheng9f1c8312008-07-03 09:09:37 +0000683 bool isKill = MI->getOperand(1).isKill();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000684 NewMI = addFrameReference(BuildMI(MF, get(PPC::STD))
Evan Cheng9f1c8312008-07-03 09:09:37 +0000685 .addReg(InReg, false, false, isKill),
Owen Anderson43dbe052008-01-07 01:35:02 +0000686 FrameIndex);
687 } else { // move -> load
688 unsigned OutReg = MI->getOperand(0).getReg();
Evan Cheng9f1c8312008-07-03 09:09:37 +0000689 bool isDead = MI->getOperand(0).isDead();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000690 NewMI = addFrameReference(BuildMI(MF, get(PPC::LD))
Evan Cheng9f1c8312008-07-03 09:09:37 +0000691 .addReg(OutReg, true, false, false, isDead),
692 FrameIndex);
Owen Anderson43dbe052008-01-07 01:35:02 +0000693 }
694 } else if (Opc == PPC::FMRD) {
695 if (OpNum == 0) { // move -> store
696 unsigned InReg = MI->getOperand(1).getReg();
Evan Cheng9f1c8312008-07-03 09:09:37 +0000697 bool isKill = MI->getOperand(1).isKill();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000698 NewMI = addFrameReference(BuildMI(MF, get(PPC::STFD))
Evan Cheng9f1c8312008-07-03 09:09:37 +0000699 .addReg(InReg, false, false, isKill),
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();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000704 NewMI = addFrameReference(BuildMI(MF, get(PPC::LFD))
Evan Cheng9f1c8312008-07-03 09:09:37 +0000705 .addReg(OutReg, true, false, false, isDead),
706 FrameIndex);
Owen Anderson43dbe052008-01-07 01:35:02 +0000707 }
708 } else if (Opc == PPC::FMRS) {
709 if (OpNum == 0) { // move -> store
710 unsigned InReg = MI->getOperand(1).getReg();
Evan Cheng9f1c8312008-07-03 09:09:37 +0000711 bool isKill = MI->getOperand(1).isKill();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000712 NewMI = addFrameReference(BuildMI(MF, get(PPC::STFS))
Evan Cheng9f1c8312008-07-03 09:09:37 +0000713 .addReg(InReg, false, false, isKill),
Owen Anderson43dbe052008-01-07 01:35:02 +0000714 FrameIndex);
715 } else { // move -> load
716 unsigned OutReg = MI->getOperand(0).getReg();
Evan Cheng9f1c8312008-07-03 09:09:37 +0000717 bool isDead = MI->getOperand(0).isDead();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000718 NewMI = addFrameReference(BuildMI(MF, get(PPC::LFS))
Evan Cheng9f1c8312008-07-03 09:09:37 +0000719 .addReg(OutReg, true, false, false, isDead),
720 FrameIndex);
Owen Anderson43dbe052008-01-07 01:35:02 +0000721 }
722 }
723
Owen Anderson43dbe052008-01-07 01:35:02 +0000724 return NewMI;
725}
726
Dan Gohman8e8b8a22008-10-16 01:49:15 +0000727bool PPCInstrInfo::canFoldMemoryOperand(const MachineInstr *MI,
728 const SmallVectorImpl<unsigned> &Ops) const {
Owen Anderson43dbe052008-01-07 01:35:02 +0000729 if (Ops.size() != 1) return false;
730
731 // Make sure this is a reg-reg copy. Note that we can't handle MCRF, because
732 // it takes more than one instruction to store it.
733 unsigned Opc = MI->getOpcode();
734
735 if ((Opc == PPC::OR &&
736 MI->getOperand(1).getReg() == MI->getOperand(2).getReg()))
737 return true;
738 else if ((Opc == PPC::OR8 &&
739 MI->getOperand(1).getReg() == MI->getOperand(2).getReg()))
740 return true;
741 else if (Opc == PPC::FMRD || Opc == PPC::FMRS)
742 return true;
743
744 return false;
745}
746
Owen Andersonf6372aa2008-01-01 21:11:32 +0000747
Dan Gohman8e8b8a22008-10-16 01:49:15 +0000748bool PPCInstrInfo::BlockHasNoFallThrough(const MachineBasicBlock &MBB) const {
Chris Lattneref139822006-10-28 17:35:02 +0000749 if (MBB.empty()) return false;
750
751 switch (MBB.back().getOpcode()) {
Evan Cheng126f17a2007-05-21 18:44:17 +0000752 case PPC::BLR: // Return.
Chris Lattneref139822006-10-28 17:35:02 +0000753 case PPC::B: // Uncond branch.
754 case PPC::BCTR: // Indirect branch.
755 return true;
756 default: return false;
757 }
758}
759
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000760bool PPCInstrInfo::
Owen Anderson44eb65c2008-08-14 22:49:33 +0000761ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
Chris Lattner7c4fe252006-10-21 06:03:11 +0000762 assert(Cond.size() == 2 && "Invalid PPC branch opcode!");
763 // Leave the CR# the same, but invert the condition.
Chris Lattner18258c62006-11-17 22:37:34 +0000764 Cond[0].setImm(PPC::InvertPredicate((PPC::Predicate)Cond[0].getImm()));
Chris Lattner7c4fe252006-10-21 06:03:11 +0000765 return false;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000766}
Nicolas Geoffray52e724a2008-04-16 20:10:13 +0000767
768/// GetInstSize - Return the number of bytes of code the specified
769/// instruction may be. This returns the maximum number of bytes.
770///
771unsigned PPCInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
772 switch (MI->getOpcode()) {
773 case PPC::INLINEASM: { // Inline Asm: Variable size.
774 const MachineFunction *MF = MI->getParent()->getParent();
775 const char *AsmStr = MI->getOperand(0).getSymbolName();
776 return MF->getTarget().getTargetAsmInfo()->getInlineAsmLength(AsmStr);
777 }
Dan Gohman44066042008-07-01 00:05:16 +0000778 case PPC::DBG_LABEL:
779 case PPC::EH_LABEL:
780 case PPC::GC_LABEL:
Nicolas Geoffray52e724a2008-04-16 20:10:13 +0000781 return 0;
Nicolas Geoffray52e724a2008-04-16 20:10:13 +0000782 default:
783 return 4; // PowerPC instructions are all 4 bytes
784 }
785}