blob: 3e0c7e73ac34d836789d908bf4abc295b45103fc [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,
Evan Chengdc54d312009-02-09 07:14:22 +0000207 SmallVectorImpl<MachineOperand> &Cond,
208 bool AllowModify) const {
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000209 // If the block has no terminators, it just falls into the block after it.
210 MachineBasicBlock::iterator I = MBB.end();
Evan Chengbfd2ec42007-06-08 21:59:56 +0000211 if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000212 return false;
213
214 // Get the last instruction in the block.
215 MachineInstr *LastInst = I;
216
217 // If there is only one terminator instruction, process it.
Evan Chengbfd2ec42007-06-08 21:59:56 +0000218 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000219 if (LastInst->getOpcode() == PPC::B) {
Chris Lattner8aa797a2007-12-30 23:10:15 +0000220 TBB = LastInst->getOperand(0).getMBB();
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000221 return false;
Chris Lattner289c2d52006-11-17 22:14:47 +0000222 } else if (LastInst->getOpcode() == PPC::BCC) {
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000223 // Block ends with fall-through condbranch.
Chris Lattner8aa797a2007-12-30 23:10:15 +0000224 TBB = LastInst->getOperand(2).getMBB();
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000225 Cond.push_back(LastInst->getOperand(0));
226 Cond.push_back(LastInst->getOperand(1));
Chris Lattner7c4fe252006-10-21 06:03:11 +0000227 return false;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000228 }
229 // Otherwise, don't know what this is.
230 return true;
231 }
232
233 // Get the instruction before it if it's a terminator.
234 MachineInstr *SecondLastInst = I;
235
236 // If there are three terminators, we don't know what sort of block this is.
237 if (SecondLastInst && I != MBB.begin() &&
Evan Chengbfd2ec42007-06-08 21:59:56 +0000238 isUnpredicatedTerminator(--I))
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000239 return true;
240
Chris Lattner289c2d52006-11-17 22:14:47 +0000241 // If the block ends with PPC::B and PPC:BCC, handle it.
242 if (SecondLastInst->getOpcode() == PPC::BCC &&
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000243 LastInst->getOpcode() == PPC::B) {
Chris Lattner8aa797a2007-12-30 23:10:15 +0000244 TBB = SecondLastInst->getOperand(2).getMBB();
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000245 Cond.push_back(SecondLastInst->getOperand(0));
246 Cond.push_back(SecondLastInst->getOperand(1));
Chris Lattner8aa797a2007-12-30 23:10:15 +0000247 FBB = LastInst->getOperand(0).getMBB();
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000248 return false;
249 }
250
Dale Johannesen13e8b512007-06-13 17:59:52 +0000251 // If the block ends with two PPC:Bs, handle it. The second one is not
252 // executed, so remove it.
253 if (SecondLastInst->getOpcode() == PPC::B &&
254 LastInst->getOpcode() == PPC::B) {
Chris Lattner8aa797a2007-12-30 23:10:15 +0000255 TBB = SecondLastInst->getOperand(0).getMBB();
Dale Johannesen13e8b512007-06-13 17:59:52 +0000256 I = LastInst;
Evan Chengdc54d312009-02-09 07:14:22 +0000257 if (AllowModify)
258 I->eraseFromParent();
Dale Johannesen13e8b512007-06-13 17:59:52 +0000259 return false;
260 }
261
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000262 // Otherwise, can't handle this.
263 return true;
264}
265
Evan Chengb5cdaa22007-05-18 00:05:48 +0000266unsigned PPCInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000267 MachineBasicBlock::iterator I = MBB.end();
Evan Chengb5cdaa22007-05-18 00:05:48 +0000268 if (I == MBB.begin()) return 0;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000269 --I;
Chris Lattner289c2d52006-11-17 22:14:47 +0000270 if (I->getOpcode() != PPC::B && I->getOpcode() != PPC::BCC)
Evan Chengb5cdaa22007-05-18 00:05:48 +0000271 return 0;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000272
273 // Remove the branch.
274 I->eraseFromParent();
275
276 I = MBB.end();
277
Evan Chengb5cdaa22007-05-18 00:05:48 +0000278 if (I == MBB.begin()) return 1;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000279 --I;
Chris Lattner289c2d52006-11-17 22:14:47 +0000280 if (I->getOpcode() != PPC::BCC)
Evan Chengb5cdaa22007-05-18 00:05:48 +0000281 return 1;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000282
283 // Remove the branch.
284 I->eraseFromParent();
Evan Chengb5cdaa22007-05-18 00:05:48 +0000285 return 2;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000286}
287
Evan Chengb5cdaa22007-05-18 00:05:48 +0000288unsigned
289PPCInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
290 MachineBasicBlock *FBB,
Owen Anderson44eb65c2008-08-14 22:49:33 +0000291 const SmallVectorImpl<MachineOperand> &Cond) const {
Chris Lattner2dc77232006-10-17 18:06:55 +0000292 // Shouldn't be a fall through.
293 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Chris Lattner54108062006-10-21 05:36:13 +0000294 assert((Cond.size() == 2 || Cond.size() == 0) &&
295 "PPC branch conditions have two components!");
Chris Lattner2dc77232006-10-17 18:06:55 +0000296
Chris Lattner54108062006-10-21 05:36:13 +0000297 // One-way branch.
Chris Lattner2dc77232006-10-17 18:06:55 +0000298 if (FBB == 0) {
Chris Lattner54108062006-10-21 05:36:13 +0000299 if (Cond.empty()) // Unconditional branch
Evan Chengc0f64ff2006-11-27 23:37:22 +0000300 BuildMI(&MBB, get(PPC::B)).addMBB(TBB);
Chris Lattner54108062006-10-21 05:36:13 +0000301 else // Conditional branch
Evan Chengc0f64ff2006-11-27 23:37:22 +0000302 BuildMI(&MBB, get(PPC::BCC))
Chris Lattner18258c62006-11-17 22:37:34 +0000303 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
Evan Chengb5cdaa22007-05-18 00:05:48 +0000304 return 1;
Chris Lattner2dc77232006-10-17 18:06:55 +0000305 }
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000306
Chris Lattner879d09c2006-10-21 05:42:09 +0000307 // Two-way Conditional Branch.
Evan Chengc0f64ff2006-11-27 23:37:22 +0000308 BuildMI(&MBB, get(PPC::BCC))
Chris Lattner18258c62006-11-17 22:37:34 +0000309 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
Evan Chengc0f64ff2006-11-27 23:37:22 +0000310 BuildMI(&MBB, get(PPC::B)).addMBB(FBB);
Evan Chengb5cdaa22007-05-18 00:05:48 +0000311 return 2;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000312}
313
Owen Anderson940f83e2008-08-26 18:03:31 +0000314bool PPCInstrInfo::copyRegToReg(MachineBasicBlock &MBB,
Owen Andersond10fd972007-12-31 06:32:00 +0000315 MachineBasicBlock::iterator MI,
316 unsigned DestReg, unsigned SrcReg,
317 const TargetRegisterClass *DestRC,
318 const TargetRegisterClass *SrcRC) const {
319 if (DestRC != SrcRC) {
Owen Anderson940f83e2008-08-26 18:03:31 +0000320 // Not yet supported!
321 return false;
Owen Andersond10fd972007-12-31 06:32:00 +0000322 }
323
324 if (DestRC == PPC::GPRCRegisterClass) {
325 BuildMI(MBB, MI, get(PPC::OR), DestReg).addReg(SrcReg).addReg(SrcReg);
326 } else if (DestRC == PPC::G8RCRegisterClass) {
327 BuildMI(MBB, MI, get(PPC::OR8), DestReg).addReg(SrcReg).addReg(SrcReg);
328 } else if (DestRC == PPC::F4RCRegisterClass) {
329 BuildMI(MBB, MI, get(PPC::FMRS), DestReg).addReg(SrcReg);
330 } else if (DestRC == PPC::F8RCRegisterClass) {
331 BuildMI(MBB, MI, get(PPC::FMRD), DestReg).addReg(SrcReg);
332 } else if (DestRC == PPC::CRRCRegisterClass) {
333 BuildMI(MBB, MI, get(PPC::MCRF), DestReg).addReg(SrcReg);
334 } else if (DestRC == PPC::VRRCRegisterClass) {
335 BuildMI(MBB, MI, get(PPC::VOR), DestReg).addReg(SrcReg).addReg(SrcReg);
Nicolas Geoffray0404cd92008-03-10 14:12:10 +0000336 } else if (DestRC == PPC::CRBITRCRegisterClass) {
337 BuildMI(MBB, MI, get(PPC::CROR), DestReg).addReg(SrcReg).addReg(SrcReg);
Owen Andersond10fd972007-12-31 06:32:00 +0000338 } else {
Owen Anderson940f83e2008-08-26 18:03:31 +0000339 // Attempt to copy register that is not GPR or FPR
340 return false;
Owen Andersond10fd972007-12-31 06:32:00 +0000341 }
Owen Anderson940f83e2008-08-26 18:03:31 +0000342
343 return true;
Owen Andersond10fd972007-12-31 06:32:00 +0000344}
345
Bill Wendling4a66e9a2008-03-10 22:49:16 +0000346bool
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000347PPCInstrInfo::StoreRegToStackSlot(MachineFunction &MF,
348 unsigned SrcReg, bool isKill,
Bill Wendling4a66e9a2008-03-10 22:49:16 +0000349 int FrameIdx,
350 const TargetRegisterClass *RC,
351 SmallVectorImpl<MachineInstr*> &NewMIs) const{
Owen Andersonf6372aa2008-01-01 21:11:32 +0000352 if (RC == PPC::GPRCRegisterClass) {
353 if (SrcReg != PPC::LR) {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000354 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::STW))
Bill Wendling4a66e9a2008-03-10 22:49:16 +0000355 .addReg(SrcReg, false, false, isKill),
356 FrameIdx));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000357 } else {
358 // FIXME: this spills LR immediately to memory in one step. To do this,
359 // we use R11, which we know cannot be used in the prolog/epilog. This is
360 // a hack.
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000361 NewMIs.push_back(BuildMI(MF, get(PPC::MFLR), PPC::R11));
362 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::STW))
Bill Wendling4a66e9a2008-03-10 22:49:16 +0000363 .addReg(PPC::R11, false, false, isKill),
364 FrameIdx));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000365 }
366 } else if (RC == PPC::G8RCRegisterClass) {
367 if (SrcReg != PPC::LR8) {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000368 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::STD))
Chris Lattnercb341de2008-03-10 18:55:53 +0000369 .addReg(SrcReg, false, false, isKill), FrameIdx));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000370 } else {
371 // FIXME: this spills LR immediately to memory in one step. To do this,
372 // we use R11, which we know cannot be used in the prolog/epilog. This is
373 // a hack.
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000374 NewMIs.push_back(BuildMI(MF, get(PPC::MFLR8), PPC::X11));
375 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::STD))
Chris Lattnercb341de2008-03-10 18:55:53 +0000376 .addReg(PPC::X11, false, false, isKill), FrameIdx));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000377 }
378 } else if (RC == PPC::F8RCRegisterClass) {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000379 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::STFD))
Chris Lattnercb341de2008-03-10 18:55:53 +0000380 .addReg(SrcReg, false, false, isKill), FrameIdx));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000381 } else if (RC == PPC::F4RCRegisterClass) {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000382 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::STFS))
Chris Lattnercb341de2008-03-10 18:55:53 +0000383 .addReg(SrcReg, false, false, isKill), FrameIdx));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000384 } else if (RC == PPC::CRRCRegisterClass) {
Bill Wendling4a66e9a2008-03-10 22:49:16 +0000385 if ((EnablePPC32RS && !TM.getSubtargetImpl()->isPPC64()) ||
386 (EnablePPC64RS && TM.getSubtargetImpl()->isPPC64())) {
387 // FIXME (64-bit): Enable
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000388 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::SPILL_CR))
Bill Wendling7194aaf2008-03-03 22:19:16 +0000389 .addReg(SrcReg, false, false, isKill),
Chris Lattner71a2cb22008-03-20 01:22:40 +0000390 FrameIdx));
Bill Wendling7194aaf2008-03-03 22:19:16 +0000391 return true;
392 } else {
393 // FIXME: We use R0 here, because it isn't available for RA. We need to
394 // store the CR in the low 4-bits of the saved value. First, issue a MFCR
395 // to save all of the CRBits.
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000396 NewMIs.push_back(BuildMI(MF, get(PPC::MFCR), PPC::R0));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000397
Bill Wendling7194aaf2008-03-03 22:19:16 +0000398 // If the saved register wasn't CR0, shift the bits left so that they are
399 // in CR0's slot.
400 if (SrcReg != PPC::CR0) {
401 unsigned ShiftBits = PPCRegisterInfo::getRegisterNumbering(SrcReg)*4;
402 // rlwinm r0, r0, ShiftBits, 0, 31.
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000403 NewMIs.push_back(BuildMI(MF, get(PPC::RLWINM), PPC::R0)
Chris Lattnercb341de2008-03-10 18:55:53 +0000404 .addReg(PPC::R0).addImm(ShiftBits).addImm(0).addImm(31));
Bill Wendling7194aaf2008-03-03 22:19:16 +0000405 }
406
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000407 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::STW))
Bill Wendling7194aaf2008-03-03 22:19:16 +0000408 .addReg(PPC::R0, false, false, isKill),
409 FrameIdx));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000410 }
Nicolas Geoffray0404cd92008-03-10 14:12:10 +0000411 } else if (RC == PPC::CRBITRCRegisterClass) {
412 // FIXME: We use CRi here because there is no mtcrf on a bit. Since the
413 // backend currently only uses CR1EQ as an individual bit, this should
414 // not cause any bug. If we need other uses of CR bits, the following
415 // code may be invalid.
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000416 unsigned Reg = 0;
Nicolas Geoffray0404cd92008-03-10 14:12:10 +0000417 if (SrcReg >= PPC::CR0LT || SrcReg <= PPC::CR0UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000418 Reg = PPC::CR0;
Nicolas Geoffray0404cd92008-03-10 14:12:10 +0000419 else if (SrcReg >= PPC::CR1LT || SrcReg <= PPC::CR1UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000420 Reg = PPC::CR1;
421 else if (SrcReg >= PPC::CR2LT || SrcReg <= PPC::CR2UN)
422 Reg = PPC::CR2;
423 else if (SrcReg >= PPC::CR3LT || SrcReg <= PPC::CR3UN)
424 Reg = PPC::CR3;
425 else if (SrcReg >= PPC::CR4LT || SrcReg <= PPC::CR4UN)
426 Reg = PPC::CR4;
427 else if (SrcReg >= PPC::CR5LT || SrcReg <= PPC::CR5UN)
428 Reg = PPC::CR5;
429 else if (SrcReg >= PPC::CR6LT || SrcReg <= PPC::CR6UN)
430 Reg = PPC::CR6;
431 else if (SrcReg >= PPC::CR7LT || SrcReg <= PPC::CR7UN)
432 Reg = PPC::CR7;
433
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000434 return StoreRegToStackSlot(MF, Reg, isKill, FrameIdx,
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000435 PPC::CRRCRegisterClass, NewMIs);
436
Owen Andersonf6372aa2008-01-01 21:11:32 +0000437 } else if (RC == PPC::VRRCRegisterClass) {
438 // We don't have indexed addressing for vector loads. Emit:
439 // R0 = ADDI FI#
440 // STVX VAL, 0, R0
441 //
442 // FIXME: We use R0 here, because it isn't available for RA.
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000443 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::ADDI), PPC::R0),
Owen Andersonf6372aa2008-01-01 21:11:32 +0000444 FrameIdx, 0, 0));
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000445 NewMIs.push_back(BuildMI(MF, get(PPC::STVX))
Chris Lattnercb341de2008-03-10 18:55:53 +0000446 .addReg(SrcReg, false, false, isKill).addReg(PPC::R0).addReg(PPC::R0));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000447 } else {
448 assert(0 && "Unknown regclass!");
449 abort();
450 }
Bill Wendling7194aaf2008-03-03 22:19:16 +0000451
452 return false;
Owen Andersonf6372aa2008-01-01 21:11:32 +0000453}
454
455void
456PPCInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
Bill Wendling7194aaf2008-03-03 22:19:16 +0000457 MachineBasicBlock::iterator MI,
458 unsigned SrcReg, bool isKill, int FrameIdx,
459 const TargetRegisterClass *RC) const {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000460 MachineFunction &MF = *MBB.getParent();
Owen Andersonf6372aa2008-01-01 21:11:32 +0000461 SmallVector<MachineInstr*, 4> NewMIs;
Bill Wendling7194aaf2008-03-03 22:19:16 +0000462
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000463 if (StoreRegToStackSlot(MF, SrcReg, isKill, FrameIdx, RC, NewMIs)) {
464 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Bill Wendling7194aaf2008-03-03 22:19:16 +0000465 FuncInfo->setSpillsCR();
466 }
467
Owen Andersonf6372aa2008-01-01 21:11:32 +0000468 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
469 MBB.insert(MI, NewMIs[i]);
470}
471
472void PPCInstrInfo::storeRegToAddr(MachineFunction &MF, unsigned SrcReg,
Bill Wendling7194aaf2008-03-03 22:19:16 +0000473 bool isKill,
474 SmallVectorImpl<MachineOperand> &Addr,
475 const TargetRegisterClass *RC,
476 SmallVectorImpl<MachineInstr*> &NewMIs) const{
Dan Gohmand735b802008-10-03 15:45:36 +0000477 if (Addr[0].isFI()) {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000478 if (StoreRegToStackSlot(MF, SrcReg, isKill,
479 Addr[0].getIndex(), RC, NewMIs)) {
Bill Wendling7194aaf2008-03-03 22:19:16 +0000480 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
481 FuncInfo->setSpillsCR();
482 }
483
Owen Andersonf6372aa2008-01-01 21:11:32 +0000484 return;
485 }
486
487 unsigned Opc = 0;
488 if (RC == PPC::GPRCRegisterClass) {
489 Opc = PPC::STW;
490 } else if (RC == PPC::G8RCRegisterClass) {
491 Opc = PPC::STD;
492 } else if (RC == PPC::F8RCRegisterClass) {
493 Opc = PPC::STFD;
494 } else if (RC == PPC::F4RCRegisterClass) {
495 Opc = PPC::STFS;
496 } else if (RC == PPC::VRRCRegisterClass) {
497 Opc = PPC::STVX;
498 } else {
499 assert(0 && "Unknown regclass!");
500 abort();
501 }
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000502 MachineInstrBuilder MIB = BuildMI(MF, get(Opc))
Owen Andersonf6372aa2008-01-01 21:11:32 +0000503 .addReg(SrcReg, false, false, isKill);
504 for (unsigned i = 0, e = Addr.size(); i != e; ++i) {
505 MachineOperand &MO = Addr[i];
Dan Gohmand735b802008-10-03 15:45:36 +0000506 if (MO.isReg())
Owen Andersonf6372aa2008-01-01 21:11:32 +0000507 MIB.addReg(MO.getReg());
Dan Gohmand735b802008-10-03 15:45:36 +0000508 else if (MO.isImm())
Owen Andersonf6372aa2008-01-01 21:11:32 +0000509 MIB.addImm(MO.getImm());
510 else
511 MIB.addFrameIndex(MO.getIndex());
512 }
513 NewMIs.push_back(MIB);
514 return;
515}
516
Bill Wendling4a66e9a2008-03-10 22:49:16 +0000517void
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000518PPCInstrInfo::LoadRegFromStackSlot(MachineFunction &MF,
519 unsigned DestReg, int FrameIdx,
Bill Wendling4a66e9a2008-03-10 22:49:16 +0000520 const TargetRegisterClass *RC,
521 SmallVectorImpl<MachineInstr*> &NewMIs)const{
Owen Andersonf6372aa2008-01-01 21:11:32 +0000522 if (RC == PPC::GPRCRegisterClass) {
523 if (DestReg != PPC::LR) {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000524 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::LWZ), DestReg),
Owen Andersonf6372aa2008-01-01 21:11:32 +0000525 FrameIdx));
526 } else {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000527 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::LWZ), PPC::R11),
Owen Andersonf6372aa2008-01-01 21:11:32 +0000528 FrameIdx));
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000529 NewMIs.push_back(BuildMI(MF, get(PPC::MTLR)).addReg(PPC::R11));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000530 }
531 } else if (RC == PPC::G8RCRegisterClass) {
532 if (DestReg != PPC::LR8) {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000533 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::LD), DestReg),
Owen Andersonf6372aa2008-01-01 21:11:32 +0000534 FrameIdx));
535 } else {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000536 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::LD), PPC::R11),
Owen Andersonf6372aa2008-01-01 21:11:32 +0000537 FrameIdx));
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000538 NewMIs.push_back(BuildMI(MF, get(PPC::MTLR8)).addReg(PPC::R11));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000539 }
540 } else if (RC == PPC::F8RCRegisterClass) {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000541 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::LFD), DestReg),
Owen Andersonf6372aa2008-01-01 21:11:32 +0000542 FrameIdx));
543 } else if (RC == PPC::F4RCRegisterClass) {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000544 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::LFS), DestReg),
Owen Andersonf6372aa2008-01-01 21:11:32 +0000545 FrameIdx));
546 } else if (RC == PPC::CRRCRegisterClass) {
547 // FIXME: We use R0 here, because it isn't available for RA.
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000548 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::LWZ), PPC::R0),
Owen Andersonf6372aa2008-01-01 21:11:32 +0000549 FrameIdx));
550
551 // If the reloaded register isn't CR0, shift the bits right so that they are
552 // in the right CR's slot.
553 if (DestReg != PPC::CR0) {
554 unsigned ShiftBits = PPCRegisterInfo::getRegisterNumbering(DestReg)*4;
555 // rlwinm r11, r11, 32-ShiftBits, 0, 31.
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000556 NewMIs.push_back(BuildMI(MF, get(PPC::RLWINM), PPC::R0)
Owen Andersonf6372aa2008-01-01 21:11:32 +0000557 .addReg(PPC::R0).addImm(32-ShiftBits).addImm(0).addImm(31));
558 }
559
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000560 NewMIs.push_back(BuildMI(MF, get(PPC::MTCRF), DestReg).addReg(PPC::R0));
Nicolas Geoffray0404cd92008-03-10 14:12:10 +0000561 } else if (RC == PPC::CRBITRCRegisterClass) {
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000562
563 unsigned Reg = 0;
Nicolas Geoffray0404cd92008-03-10 14:12:10 +0000564 if (DestReg >= PPC::CR0LT || DestReg <= PPC::CR0UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000565 Reg = PPC::CR0;
Nicolas Geoffray0404cd92008-03-10 14:12:10 +0000566 else if (DestReg >= PPC::CR1LT || DestReg <= PPC::CR1UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000567 Reg = PPC::CR1;
568 else if (DestReg >= PPC::CR2LT || DestReg <= PPC::CR2UN)
569 Reg = PPC::CR2;
570 else if (DestReg >= PPC::CR3LT || DestReg <= PPC::CR3UN)
571 Reg = PPC::CR3;
572 else if (DestReg >= PPC::CR4LT || DestReg <= PPC::CR4UN)
573 Reg = PPC::CR4;
574 else if (DestReg >= PPC::CR5LT || DestReg <= PPC::CR5UN)
575 Reg = PPC::CR5;
576 else if (DestReg >= PPC::CR6LT || DestReg <= PPC::CR6UN)
577 Reg = PPC::CR6;
578 else if (DestReg >= PPC::CR7LT || DestReg <= PPC::CR7UN)
579 Reg = PPC::CR7;
580
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000581 return LoadRegFromStackSlot(MF, Reg, FrameIdx,
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000582 PPC::CRRCRegisterClass, NewMIs);
583
Owen Andersonf6372aa2008-01-01 21:11:32 +0000584 } else if (RC == PPC::VRRCRegisterClass) {
585 // We don't have indexed addressing for vector loads. Emit:
586 // R0 = ADDI FI#
587 // Dest = LVX 0, R0
588 //
589 // FIXME: We use R0 here, because it isn't available for RA.
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000590 NewMIs.push_back(addFrameReference(BuildMI(MF, get(PPC::ADDI), PPC::R0),
Owen Andersonf6372aa2008-01-01 21:11:32 +0000591 FrameIdx, 0, 0));
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000592 NewMIs.push_back(BuildMI(MF, get(PPC::LVX),DestReg).addReg(PPC::R0)
Owen Andersonf6372aa2008-01-01 21:11:32 +0000593 .addReg(PPC::R0));
594 } else {
595 assert(0 && "Unknown regclass!");
596 abort();
597 }
598}
599
600void
601PPCInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
Bill Wendling7194aaf2008-03-03 22:19:16 +0000602 MachineBasicBlock::iterator MI,
603 unsigned DestReg, int FrameIdx,
604 const TargetRegisterClass *RC) const {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000605 MachineFunction &MF = *MBB.getParent();
Owen Andersonf6372aa2008-01-01 21:11:32 +0000606 SmallVector<MachineInstr*, 4> NewMIs;
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000607 LoadRegFromStackSlot(MF, DestReg, FrameIdx, RC, NewMIs);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000608 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
609 MBB.insert(MI, NewMIs[i]);
610}
611
612void PPCInstrInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
Bill Wendling7194aaf2008-03-03 22:19:16 +0000613 SmallVectorImpl<MachineOperand> &Addr,
614 const TargetRegisterClass *RC,
615 SmallVectorImpl<MachineInstr*> &NewMIs)const{
Dan Gohmand735b802008-10-03 15:45:36 +0000616 if (Addr[0].isFI()) {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000617 LoadRegFromStackSlot(MF, DestReg, Addr[0].getIndex(), RC, NewMIs);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000618 return;
619 }
620
621 unsigned Opc = 0;
622 if (RC == PPC::GPRCRegisterClass) {
623 assert(DestReg != PPC::LR && "Can't handle this yet!");
624 Opc = PPC::LWZ;
625 } else if (RC == PPC::G8RCRegisterClass) {
626 assert(DestReg != PPC::LR8 && "Can't handle this yet!");
627 Opc = PPC::LD;
628 } else if (RC == PPC::F8RCRegisterClass) {
629 Opc = PPC::LFD;
630 } else if (RC == PPC::F4RCRegisterClass) {
631 Opc = PPC::LFS;
632 } else if (RC == PPC::VRRCRegisterClass) {
633 Opc = PPC::LVX;
634 } else {
635 assert(0 && "Unknown regclass!");
636 abort();
637 }
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000638 MachineInstrBuilder MIB = BuildMI(MF, get(Opc), DestReg);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000639 for (unsigned i = 0, e = Addr.size(); i != e; ++i) {
640 MachineOperand &MO = Addr[i];
Dan Gohmand735b802008-10-03 15:45:36 +0000641 if (MO.isReg())
Owen Andersonf6372aa2008-01-01 21:11:32 +0000642 MIB.addReg(MO.getReg());
Dan Gohmand735b802008-10-03 15:45:36 +0000643 else if (MO.isImm())
Owen Andersonf6372aa2008-01-01 21:11:32 +0000644 MIB.addImm(MO.getImm());
645 else
646 MIB.addFrameIndex(MO.getIndex());
647 }
648 NewMIs.push_back(MIB);
649 return;
650}
651
Owen Anderson43dbe052008-01-07 01:35:02 +0000652/// foldMemoryOperand - PowerPC (like most RISC's) can only fold spills into
653/// copy instructions, turning them into load/store instructions.
Dan Gohmanc54baa22008-12-03 18:43:12 +0000654MachineInstr *PPCInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
655 MachineInstr *MI,
656 const SmallVectorImpl<unsigned> &Ops,
657 int FrameIndex) const {
Owen Anderson43dbe052008-01-07 01:35:02 +0000658 if (Ops.size() != 1) return NULL;
659
660 // Make sure this is a reg-reg copy. Note that we can't handle MCRF, because
661 // it takes more than one instruction to store it.
662 unsigned Opc = MI->getOpcode();
663 unsigned OpNum = Ops[0];
664
665 MachineInstr *NewMI = NULL;
666 if ((Opc == PPC::OR &&
667 MI->getOperand(1).getReg() == MI->getOperand(2).getReg())) {
668 if (OpNum == 0) { // move -> store
669 unsigned InReg = MI->getOperand(1).getReg();
Evan Cheng9f1c8312008-07-03 09:09:37 +0000670 bool isKill = MI->getOperand(1).isKill();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000671 NewMI = addFrameReference(BuildMI(MF, get(PPC::STW))
Evan Cheng9f1c8312008-07-03 09:09:37 +0000672 .addReg(InReg, false, false, isKill),
Owen Anderson43dbe052008-01-07 01:35:02 +0000673 FrameIndex);
674 } else { // move -> load
675 unsigned OutReg = MI->getOperand(0).getReg();
Evan Cheng9f1c8312008-07-03 09:09:37 +0000676 bool isDead = MI->getOperand(0).isDead();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000677 NewMI = addFrameReference(BuildMI(MF, get(PPC::LWZ))
Evan Cheng9f1c8312008-07-03 09:09:37 +0000678 .addReg(OutReg, true, false, false, isDead),
Owen Anderson43dbe052008-01-07 01:35:02 +0000679 FrameIndex);
680 }
681 } else if ((Opc == PPC::OR8 &&
682 MI->getOperand(1).getReg() == MI->getOperand(2).getReg())) {
683 if (OpNum == 0) { // move -> store
684 unsigned InReg = MI->getOperand(1).getReg();
Evan Cheng9f1c8312008-07-03 09:09:37 +0000685 bool isKill = MI->getOperand(1).isKill();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000686 NewMI = addFrameReference(BuildMI(MF, get(PPC::STD))
Evan Cheng9f1c8312008-07-03 09:09:37 +0000687 .addReg(InReg, false, false, isKill),
Owen Anderson43dbe052008-01-07 01:35:02 +0000688 FrameIndex);
689 } else { // move -> load
690 unsigned OutReg = MI->getOperand(0).getReg();
Evan Cheng9f1c8312008-07-03 09:09:37 +0000691 bool isDead = MI->getOperand(0).isDead();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000692 NewMI = addFrameReference(BuildMI(MF, get(PPC::LD))
Evan Cheng9f1c8312008-07-03 09:09:37 +0000693 .addReg(OutReg, true, false, false, isDead),
694 FrameIndex);
Owen Anderson43dbe052008-01-07 01:35:02 +0000695 }
696 } else if (Opc == PPC::FMRD) {
697 if (OpNum == 0) { // move -> store
698 unsigned InReg = MI->getOperand(1).getReg();
Evan Cheng9f1c8312008-07-03 09:09:37 +0000699 bool isKill = MI->getOperand(1).isKill();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000700 NewMI = addFrameReference(BuildMI(MF, get(PPC::STFD))
Evan Cheng9f1c8312008-07-03 09:09:37 +0000701 .addReg(InReg, false, false, isKill),
Owen Anderson43dbe052008-01-07 01:35:02 +0000702 FrameIndex);
703 } else { // move -> load
704 unsigned OutReg = MI->getOperand(0).getReg();
Evan Cheng9f1c8312008-07-03 09:09:37 +0000705 bool isDead = MI->getOperand(0).isDead();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000706 NewMI = addFrameReference(BuildMI(MF, get(PPC::LFD))
Evan Cheng9f1c8312008-07-03 09:09:37 +0000707 .addReg(OutReg, true, false, false, isDead),
708 FrameIndex);
Owen Anderson43dbe052008-01-07 01:35:02 +0000709 }
710 } else if (Opc == PPC::FMRS) {
711 if (OpNum == 0) { // move -> store
712 unsigned InReg = MI->getOperand(1).getReg();
Evan Cheng9f1c8312008-07-03 09:09:37 +0000713 bool isKill = MI->getOperand(1).isKill();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000714 NewMI = addFrameReference(BuildMI(MF, get(PPC::STFS))
Evan Cheng9f1c8312008-07-03 09:09:37 +0000715 .addReg(InReg, false, false, isKill),
Owen Anderson43dbe052008-01-07 01:35:02 +0000716 FrameIndex);
717 } else { // move -> load
718 unsigned OutReg = MI->getOperand(0).getReg();
Evan Cheng9f1c8312008-07-03 09:09:37 +0000719 bool isDead = MI->getOperand(0).isDead();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000720 NewMI = addFrameReference(BuildMI(MF, get(PPC::LFS))
Evan Cheng9f1c8312008-07-03 09:09:37 +0000721 .addReg(OutReg, true, false, false, isDead),
722 FrameIndex);
Owen Anderson43dbe052008-01-07 01:35:02 +0000723 }
724 }
725
Owen Anderson43dbe052008-01-07 01:35:02 +0000726 return NewMI;
727}
728
Dan Gohman8e8b8a22008-10-16 01:49:15 +0000729bool PPCInstrInfo::canFoldMemoryOperand(const MachineInstr *MI,
730 const SmallVectorImpl<unsigned> &Ops) const {
Owen Anderson43dbe052008-01-07 01:35:02 +0000731 if (Ops.size() != 1) return false;
732
733 // Make sure this is a reg-reg copy. Note that we can't handle MCRF, because
734 // it takes more than one instruction to store it.
735 unsigned Opc = MI->getOpcode();
736
737 if ((Opc == PPC::OR &&
738 MI->getOperand(1).getReg() == MI->getOperand(2).getReg()))
739 return true;
740 else if ((Opc == PPC::OR8 &&
741 MI->getOperand(1).getReg() == MI->getOperand(2).getReg()))
742 return true;
743 else if (Opc == PPC::FMRD || Opc == PPC::FMRS)
744 return true;
745
746 return false;
747}
748
Owen Andersonf6372aa2008-01-01 21:11:32 +0000749
Dan Gohman8e8b8a22008-10-16 01:49:15 +0000750bool PPCInstrInfo::BlockHasNoFallThrough(const MachineBasicBlock &MBB) const {
Chris Lattneref139822006-10-28 17:35:02 +0000751 if (MBB.empty()) return false;
752
753 switch (MBB.back().getOpcode()) {
Evan Cheng126f17a2007-05-21 18:44:17 +0000754 case PPC::BLR: // Return.
Chris Lattneref139822006-10-28 17:35:02 +0000755 case PPC::B: // Uncond branch.
756 case PPC::BCTR: // Indirect branch.
757 return true;
758 default: return false;
759 }
760}
761
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000762bool PPCInstrInfo::
Owen Anderson44eb65c2008-08-14 22:49:33 +0000763ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
Chris Lattner7c4fe252006-10-21 06:03:11 +0000764 assert(Cond.size() == 2 && "Invalid PPC branch opcode!");
765 // Leave the CR# the same, but invert the condition.
Chris Lattner18258c62006-11-17 22:37:34 +0000766 Cond[0].setImm(PPC::InvertPredicate((PPC::Predicate)Cond[0].getImm()));
Chris Lattner7c4fe252006-10-21 06:03:11 +0000767 return false;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000768}
Nicolas Geoffray52e724a2008-04-16 20:10:13 +0000769
770/// GetInstSize - Return the number of bytes of code the specified
771/// instruction may be. This returns the maximum number of bytes.
772///
773unsigned PPCInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
774 switch (MI->getOpcode()) {
775 case PPC::INLINEASM: { // Inline Asm: Variable size.
776 const MachineFunction *MF = MI->getParent()->getParent();
777 const char *AsmStr = MI->getOperand(0).getSymbolName();
778 return MF->getTarget().getTargetAsmInfo()->getInlineAsmLength(AsmStr);
779 }
Dan Gohman44066042008-07-01 00:05:16 +0000780 case PPC::DBG_LABEL:
781 case PPC::EH_LABEL:
782 case PPC::GC_LABEL:
Nicolas Geoffray52e724a2008-04-16 20:10:13 +0000783 return 0;
Nicolas Geoffray52e724a2008-04-16 20:10:13 +0000784 default:
785 return 4; // PowerPC instructions are all 4 bytes
786 }
787}