blob: fd996eddfb28f68f2f808e4d313796ab0d1938cd [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"
Jakob Stoklund Olesen24329662010-02-26 21:09:24 +000022#include "llvm/CodeGen/MachineRegisterInfo.h"
Bill Wendling880d0f62008-03-04 23:13:51 +000023#include "llvm/Support/CommandLine.h"
Torok Edwindac237e2009-07-08 20:53:28 +000024#include "llvm/Support/ErrorHandling.h"
25#include "llvm/Support/raw_ostream.h"
Chris Lattneraf76e592009-08-22 20:48:53 +000026#include "llvm/MC/MCAsmInfo.h"
Misha Brukmanf2ccb772004-08-17 04:55:41 +000027
Dan Gohman82bcd232010-04-15 17:20:57 +000028namespace llvm {
Bill Wendling4a66e9a2008-03-10 22:49:16 +000029extern cl::opt<bool> EnablePPC32RS; // FIXME (64-bit): See PPCRegisterInfo.cpp.
30extern cl::opt<bool> EnablePPC64RS; // FIXME (64-bit): See PPCRegisterInfo.cpp.
Dan Gohman82bcd232010-04-15 17:20:57 +000031}
32
33using namespace llvm;
Bill Wendling880d0f62008-03-04 23:13:51 +000034
Chris Lattnerb1d26f62006-06-17 00:01:04 +000035PPCInstrInfo::PPCInstrInfo(PPCTargetMachine &tm)
Chris Lattner64105522008-01-01 01:03:04 +000036 : TargetInstrInfoImpl(PPCInsts, array_lengthof(PPCInsts)), TM(tm),
Evan Cheng7ce45782006-11-13 23:36:35 +000037 RI(*TM.getSubtargetImpl(), *this) {}
Chris Lattnerb1d26f62006-06-17 00:01:04 +000038
Nate Begeman21e463b2005-10-16 05:39:50 +000039bool PPCInstrInfo::isMoveInstr(const MachineInstr& MI,
40 unsigned& sourceReg,
Evan Cheng04ee5a12009-01-20 19:12:24 +000041 unsigned& destReg,
42 unsigned& sourceSubIdx,
43 unsigned& destSubIdx) const {
44 sourceSubIdx = destSubIdx = 0; // No sub-registers.
45
Chris Lattnercc8cd0c2008-01-07 02:48:55 +000046 unsigned oc = MI.getOpcode();
Chris Lattnerb410dc92006-06-20 23:18:58 +000047 if (oc == PPC::OR || oc == PPC::OR8 || oc == PPC::VOR ||
Chris Lattner14c09b82005-10-19 01:50:36 +000048 oc == PPC::OR4To8 || oc == PPC::OR8To4) { // or r1, r2, r2
Evan Cheng1e3417292007-04-25 07:12:14 +000049 assert(MI.getNumOperands() >= 3 &&
Dan Gohmand735b802008-10-03 15:45:36 +000050 MI.getOperand(0).isReg() &&
51 MI.getOperand(1).isReg() &&
52 MI.getOperand(2).isReg() &&
Misha Brukmanf2ccb772004-08-17 04:55:41 +000053 "invalid PPC OR instruction!");
54 if (MI.getOperand(1).getReg() == MI.getOperand(2).getReg()) {
55 sourceReg = MI.getOperand(1).getReg();
56 destReg = MI.getOperand(0).getReg();
57 return true;
58 }
59 } else if (oc == PPC::ADDI) { // addi r1, r2, 0
Evan Cheng1e3417292007-04-25 07:12:14 +000060 assert(MI.getNumOperands() >= 3 &&
Dan Gohmand735b802008-10-03 15:45:36 +000061 MI.getOperand(0).isReg() &&
62 MI.getOperand(2).isImm() &&
Misha Brukmanf2ccb772004-08-17 04:55:41 +000063 "invalid PPC ADDI instruction!");
Dan Gohmand735b802008-10-03 15:45:36 +000064 if (MI.getOperand(1).isReg() && MI.getOperand(2).getImm() == 0) {
Misha Brukmanf2ccb772004-08-17 04:55:41 +000065 sourceReg = MI.getOperand(1).getReg();
66 destReg = MI.getOperand(0).getReg();
67 return true;
68 }
Nate Begemancb90de32004-10-07 22:26:12 +000069 } else if (oc == PPC::ORI) { // ori r1, r2, 0
Evan Cheng1e3417292007-04-25 07:12:14 +000070 assert(MI.getNumOperands() >= 3 &&
Dan Gohmand735b802008-10-03 15:45:36 +000071 MI.getOperand(0).isReg() &&
72 MI.getOperand(1).isReg() &&
73 MI.getOperand(2).isImm() &&
Nate Begemancb90de32004-10-07 22:26:12 +000074 "invalid PPC ORI instruction!");
Chris Lattner9a1ceae2007-12-30 20:49:49 +000075 if (MI.getOperand(2).getImm() == 0) {
Nate Begemancb90de32004-10-07 22:26:12 +000076 sourceReg = MI.getOperand(1).getReg();
77 destReg = MI.getOperand(0).getReg();
78 return true;
79 }
Jakob Stoklund Olesenbaafcbb42010-02-26 21:53:24 +000080 } else if (oc == PPC::FMR || oc == PPC::FMRSD) { // fmr r1, r2
Evan Cheng1e3417292007-04-25 07:12:14 +000081 assert(MI.getNumOperands() >= 2 &&
Dan Gohmand735b802008-10-03 15:45:36 +000082 MI.getOperand(0).isReg() &&
83 MI.getOperand(1).isReg() &&
Misha Brukmanf2ccb772004-08-17 04:55:41 +000084 "invalid PPC FMR instruction");
85 sourceReg = MI.getOperand(1).getReg();
86 destReg = MI.getOperand(0).getReg();
87 return true;
Nate Begeman7af02482005-04-12 07:04:16 +000088 } else if (oc == PPC::MCRF) { // mcrf cr1, cr2
Evan Cheng1e3417292007-04-25 07:12:14 +000089 assert(MI.getNumOperands() >= 2 &&
Dan Gohmand735b802008-10-03 15:45:36 +000090 MI.getOperand(0).isReg() &&
91 MI.getOperand(1).isReg() &&
Nate Begeman7af02482005-04-12 07:04:16 +000092 "invalid PPC MCRF instruction");
93 sourceReg = MI.getOperand(1).getReg();
94 destReg = MI.getOperand(0).getReg();
95 return true;
Misha Brukmanf2ccb772004-08-17 04:55:41 +000096 }
97 return false;
98}
Chris Lattner043870d2005-09-09 18:17:41 +000099
Dan Gohmancbad42c2008-11-18 19:49:32 +0000100unsigned PPCInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
Chris Lattner9c09c9e2006-03-16 22:24:02 +0000101 int &FrameIndex) const {
Chris Lattner40839602006-02-02 20:12:32 +0000102 switch (MI->getOpcode()) {
103 default: break;
104 case PPC::LD:
105 case PPC::LWZ:
106 case PPC::LFS:
107 case PPC::LFD:
Dan Gohmand735b802008-10-03 15:45:36 +0000108 if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
109 MI->getOperand(2).isFI()) {
Chris Lattner8aa797a2007-12-30 23:10:15 +0000110 FrameIndex = MI->getOperand(2).getIndex();
Chris Lattner40839602006-02-02 20:12:32 +0000111 return MI->getOperand(0).getReg();
112 }
113 break;
114 }
115 return 0;
Chris Lattner65242872006-02-02 20:16:12 +0000116}
Chris Lattner40839602006-02-02 20:12:32 +0000117
Dan Gohmancbad42c2008-11-18 19:49:32 +0000118unsigned PPCInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
Chris Lattner65242872006-02-02 20:16:12 +0000119 int &FrameIndex) const {
120 switch (MI->getOpcode()) {
121 default: break;
Nate Begeman3b478b32006-02-02 21:07:50 +0000122 case PPC::STD:
Chris Lattner65242872006-02-02 20:16:12 +0000123 case PPC::STW:
124 case PPC::STFS:
125 case PPC::STFD:
Dan Gohmand735b802008-10-03 15:45:36 +0000126 if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
127 MI->getOperand(2).isFI()) {
Chris Lattner8aa797a2007-12-30 23:10:15 +0000128 FrameIndex = MI->getOperand(2).getIndex();
Chris Lattner65242872006-02-02 20:16:12 +0000129 return MI->getOperand(0).getReg();
130 }
131 break;
132 }
133 return 0;
134}
Chris Lattner40839602006-02-02 20:12:32 +0000135
Chris Lattner043870d2005-09-09 18:17:41 +0000136// commuteInstruction - We can commute rlwimi instructions, but only if the
137// rotate amt is zero. We also have to munge the immediates a bit.
Evan Cheng58dcb0e2008-06-16 07:33:11 +0000138MachineInstr *
139PPCInstrInfo::commuteInstruction(MachineInstr *MI, bool NewMI) const {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000140 MachineFunction &MF = *MI->getParent()->getParent();
141
Chris Lattner043870d2005-09-09 18:17:41 +0000142 // Normal instructions can be commuted the obvious way.
143 if (MI->getOpcode() != PPC::RLWIMI)
Evan Cheng58dcb0e2008-06-16 07:33:11 +0000144 return TargetInstrInfoImpl::commuteInstruction(MI, NewMI);
Chris Lattner043870d2005-09-09 18:17:41 +0000145
146 // Cannot commute if it has a non-zero rotate count.
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000147 if (MI->getOperand(3).getImm() != 0)
Chris Lattner043870d2005-09-09 18:17:41 +0000148 return 0;
149
150 // If we have a zero rotate count, we have:
151 // M = mask(MB,ME)
152 // Op0 = (Op1 & ~M) | (Op2 & M)
153 // Change this to:
154 // M = mask((ME+1)&31, (MB-1)&31)
155 // Op0 = (Op2 & ~M) | (Op1 & M)
156
157 // Swap op1/op2
Evan Chenga4d16a12008-02-13 02:46:49 +0000158 unsigned Reg0 = MI->getOperand(0).getReg();
Chris Lattner043870d2005-09-09 18:17:41 +0000159 unsigned Reg1 = MI->getOperand(1).getReg();
160 unsigned Reg2 = MI->getOperand(2).getReg();
Evan Cheng6ce7dc22006-11-15 20:58:11 +0000161 bool Reg1IsKill = MI->getOperand(1).isKill();
162 bool Reg2IsKill = MI->getOperand(2).isKill();
Evan Cheng58dcb0e2008-06-16 07:33:11 +0000163 bool ChangeReg0 = false;
Evan Chenga4d16a12008-02-13 02:46:49 +0000164 // If machine instrs are no longer in two-address forms, update
165 // destination register as well.
166 if (Reg0 == Reg1) {
167 // Must be two address instruction!
168 assert(MI->getDesc().getOperandConstraint(0, TOI::TIED_TO) &&
169 "Expecting a two-address instruction!");
Evan Chenga4d16a12008-02-13 02:46:49 +0000170 Reg2IsKill = false;
Evan Cheng58dcb0e2008-06-16 07:33:11 +0000171 ChangeReg0 = true;
Evan Chenga4d16a12008-02-13 02:46:49 +0000172 }
Evan Cheng58dcb0e2008-06-16 07:33:11 +0000173
174 // Masks.
175 unsigned MB = MI->getOperand(4).getImm();
176 unsigned ME = MI->getOperand(5).getImm();
177
178 if (NewMI) {
179 // Create a new instruction.
180 unsigned Reg0 = ChangeReg0 ? Reg2 : MI->getOperand(0).getReg();
181 bool Reg0IsDead = MI->getOperand(0).isDead();
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000182 return BuildMI(MF, MI->getDebugLoc(), MI->getDesc())
Bill Wendling587daed2009-05-13 21:33:08 +0000183 .addReg(Reg0, RegState::Define | getDeadRegState(Reg0IsDead))
184 .addReg(Reg2, getKillRegState(Reg2IsKill))
185 .addReg(Reg1, getKillRegState(Reg1IsKill))
Evan Cheng58dcb0e2008-06-16 07:33:11 +0000186 .addImm((ME+1) & 31)
187 .addImm((MB-1) & 31);
188 }
189
190 if (ChangeReg0)
191 MI->getOperand(0).setReg(Reg2);
Chris Lattnere53f4a02006-05-04 17:52:23 +0000192 MI->getOperand(2).setReg(Reg1);
193 MI->getOperand(1).setReg(Reg2);
Chris Lattnerf7382302007-12-30 21:56:09 +0000194 MI->getOperand(2).setIsKill(Reg1IsKill);
195 MI->getOperand(1).setIsKill(Reg2IsKill);
Chris Lattner043870d2005-09-09 18:17:41 +0000196
197 // Swap the mask around.
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000198 MI->getOperand(4).setImm((ME+1) & 31);
199 MI->getOperand(5).setImm((MB-1) & 31);
Chris Lattner043870d2005-09-09 18:17:41 +0000200 return MI;
201}
Chris Lattnerbbf1c722006-03-05 23:49:55 +0000202
203void PPCInstrInfo::insertNoop(MachineBasicBlock &MBB,
204 MachineBasicBlock::iterator MI) const {
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000205 DebugLoc DL;
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000206 BuildMI(MBB, MI, DL, get(PPC::NOP));
Chris Lattnerbbf1c722006-03-05 23:49:55 +0000207}
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000208
209
210// Branch analysis.
211bool PPCInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
212 MachineBasicBlock *&FBB,
Evan Chengdc54d312009-02-09 07:14:22 +0000213 SmallVectorImpl<MachineOperand> &Cond,
214 bool AllowModify) const {
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000215 // If the block has no terminators, it just falls into the block after it.
216 MachineBasicBlock::iterator I = MBB.end();
Dale Johannesen93d6a7e2010-04-02 01:38:09 +0000217 if (I == MBB.begin())
218 return false;
219 --I;
220 while (I->isDebugValue()) {
221 if (I == MBB.begin())
222 return false;
223 --I;
224 }
225 if (!isUnpredicatedTerminator(I))
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000226 return false;
227
228 // Get the last instruction in the block.
229 MachineInstr *LastInst = I;
230
231 // If there is only one terminator instruction, process it.
Evan Chengbfd2ec42007-06-08 21:59:56 +0000232 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000233 if (LastInst->getOpcode() == PPC::B) {
Evan Cheng82ae9332009-05-08 23:09:25 +0000234 if (!LastInst->getOperand(0).isMBB())
235 return true;
Chris Lattner8aa797a2007-12-30 23:10:15 +0000236 TBB = LastInst->getOperand(0).getMBB();
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000237 return false;
Chris Lattner289c2d52006-11-17 22:14:47 +0000238 } else if (LastInst->getOpcode() == PPC::BCC) {
Evan Cheng82ae9332009-05-08 23:09:25 +0000239 if (!LastInst->getOperand(2).isMBB())
240 return true;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000241 // Block ends with fall-through condbranch.
Chris Lattner8aa797a2007-12-30 23:10:15 +0000242 TBB = LastInst->getOperand(2).getMBB();
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000243 Cond.push_back(LastInst->getOperand(0));
244 Cond.push_back(LastInst->getOperand(1));
Chris Lattner7c4fe252006-10-21 06:03:11 +0000245 return false;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000246 }
247 // Otherwise, don't know what this is.
248 return true;
249 }
250
251 // Get the instruction before it if it's a terminator.
252 MachineInstr *SecondLastInst = I;
253
254 // If there are three terminators, we don't know what sort of block this is.
255 if (SecondLastInst && I != MBB.begin() &&
Evan Chengbfd2ec42007-06-08 21:59:56 +0000256 isUnpredicatedTerminator(--I))
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000257 return true;
258
Chris Lattner289c2d52006-11-17 22:14:47 +0000259 // If the block ends with PPC::B and PPC:BCC, handle it.
260 if (SecondLastInst->getOpcode() == PPC::BCC &&
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000261 LastInst->getOpcode() == PPC::B) {
Evan Cheng82ae9332009-05-08 23:09:25 +0000262 if (!SecondLastInst->getOperand(2).isMBB() ||
263 !LastInst->getOperand(0).isMBB())
264 return true;
Chris Lattner8aa797a2007-12-30 23:10:15 +0000265 TBB = SecondLastInst->getOperand(2).getMBB();
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000266 Cond.push_back(SecondLastInst->getOperand(0));
267 Cond.push_back(SecondLastInst->getOperand(1));
Chris Lattner8aa797a2007-12-30 23:10:15 +0000268 FBB = LastInst->getOperand(0).getMBB();
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000269 return false;
270 }
271
Dale Johannesen13e8b512007-06-13 17:59:52 +0000272 // If the block ends with two PPC:Bs, handle it. The second one is not
273 // executed, so remove it.
274 if (SecondLastInst->getOpcode() == PPC::B &&
275 LastInst->getOpcode() == PPC::B) {
Evan Cheng82ae9332009-05-08 23:09:25 +0000276 if (!SecondLastInst->getOperand(0).isMBB())
277 return true;
Chris Lattner8aa797a2007-12-30 23:10:15 +0000278 TBB = SecondLastInst->getOperand(0).getMBB();
Dale Johannesen13e8b512007-06-13 17:59:52 +0000279 I = LastInst;
Evan Chengdc54d312009-02-09 07:14:22 +0000280 if (AllowModify)
281 I->eraseFromParent();
Dale Johannesen13e8b512007-06-13 17:59:52 +0000282 return false;
283 }
284
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000285 // Otherwise, can't handle this.
286 return true;
287}
288
Evan Chengb5cdaa22007-05-18 00:05:48 +0000289unsigned PPCInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000290 MachineBasicBlock::iterator I = MBB.end();
Evan Chengb5cdaa22007-05-18 00:05:48 +0000291 if (I == MBB.begin()) return 0;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000292 --I;
Dale Johannesen93d6a7e2010-04-02 01:38:09 +0000293 while (I->isDebugValue()) {
294 if (I == MBB.begin())
295 return 0;
296 --I;
297 }
Chris Lattner289c2d52006-11-17 22:14:47 +0000298 if (I->getOpcode() != PPC::B && I->getOpcode() != PPC::BCC)
Evan Chengb5cdaa22007-05-18 00:05:48 +0000299 return 0;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000300
301 // Remove the branch.
302 I->eraseFromParent();
303
304 I = MBB.end();
305
Evan Chengb5cdaa22007-05-18 00:05:48 +0000306 if (I == MBB.begin()) return 1;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000307 --I;
Chris Lattner289c2d52006-11-17 22:14:47 +0000308 if (I->getOpcode() != PPC::BCC)
Evan Chengb5cdaa22007-05-18 00:05:48 +0000309 return 1;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000310
311 // Remove the branch.
312 I->eraseFromParent();
Evan Chengb5cdaa22007-05-18 00:05:48 +0000313 return 2;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000314}
315
Evan Chengb5cdaa22007-05-18 00:05:48 +0000316unsigned
317PPCInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
318 MachineBasicBlock *FBB,
Stuart Hastings3bf91252010-06-17 22:43:56 +0000319 const SmallVectorImpl<MachineOperand> &Cond,
320 DebugLoc DL) const {
Chris Lattner2dc77232006-10-17 18:06:55 +0000321 // Shouldn't be a fall through.
322 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Chris Lattner54108062006-10-21 05:36:13 +0000323 assert((Cond.size() == 2 || Cond.size() == 0) &&
324 "PPC branch conditions have two components!");
Chris Lattner2dc77232006-10-17 18:06:55 +0000325
Chris Lattner54108062006-10-21 05:36:13 +0000326 // One-way branch.
Chris Lattner2dc77232006-10-17 18:06:55 +0000327 if (FBB == 0) {
Chris Lattner54108062006-10-21 05:36:13 +0000328 if (Cond.empty()) // Unconditional branch
Stuart Hastings3bf91252010-06-17 22:43:56 +0000329 BuildMI(&MBB, DL, get(PPC::B)).addMBB(TBB);
Chris Lattner54108062006-10-21 05:36:13 +0000330 else // Conditional branch
Stuart Hastings3bf91252010-06-17 22:43:56 +0000331 BuildMI(&MBB, DL, get(PPC::BCC))
Chris Lattner18258c62006-11-17 22:37:34 +0000332 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
Evan Chengb5cdaa22007-05-18 00:05:48 +0000333 return 1;
Chris Lattner2dc77232006-10-17 18:06:55 +0000334 }
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000335
Chris Lattner879d09c2006-10-21 05:42:09 +0000336 // Two-way Conditional Branch.
Stuart Hastings3bf91252010-06-17 22:43:56 +0000337 BuildMI(&MBB, DL, get(PPC::BCC))
Chris Lattner18258c62006-11-17 22:37:34 +0000338 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
Stuart Hastings3bf91252010-06-17 22:43:56 +0000339 BuildMI(&MBB, DL, get(PPC::B)).addMBB(FBB);
Evan Chengb5cdaa22007-05-18 00:05:48 +0000340 return 2;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000341}
342
Jakob Stoklund Olesen27689b02010-07-11 07:31:00 +0000343void PPCInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
344 MachineBasicBlock::iterator I, DebugLoc DL,
345 unsigned DestReg, unsigned SrcReg,
346 bool KillSrc) const {
347 unsigned Opc;
348 if (PPC::GPRCRegClass.contains(DestReg, SrcReg))
349 Opc = PPC::OR;
350 else if (PPC::G8RCRegClass.contains(DestReg, SrcReg))
351 Opc = PPC::OR8;
352 else if (PPC::F4RCRegClass.contains(DestReg, SrcReg))
353 Opc = PPC::FMR;
354 else if (PPC::CRRCRegClass.contains(DestReg, SrcReg))
355 Opc = PPC::MCRF;
356 else if (PPC::VRRCRegClass.contains(DestReg, SrcReg))
357 Opc = PPC::VOR;
358 else if (PPC::CRBITRCRegClass.contains(DestReg, SrcReg))
359 Opc = PPC::CROR;
360 else
361 llvm_unreachable("Impossible reg-to-reg copy");
Owen Andersond10fd972007-12-31 06:32:00 +0000362
Jakob Stoklund Olesen27689b02010-07-11 07:31:00 +0000363 const TargetInstrDesc &TID = get(Opc);
364 if (TID.getNumOperands() == 3)
365 BuildMI(MBB, I, DL, TID, DestReg)
366 .addReg(SrcReg).addReg(SrcReg, getKillRegState(KillSrc));
367 else
368 BuildMI(MBB, I, DL, TID, DestReg).addReg(SrcReg, getKillRegState(KillSrc));
Owen Andersond10fd972007-12-31 06:32:00 +0000369}
370
Bill Wendling4a66e9a2008-03-10 22:49:16 +0000371bool
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000372PPCInstrInfo::StoreRegToStackSlot(MachineFunction &MF,
373 unsigned SrcReg, bool isKill,
Bill Wendling4a66e9a2008-03-10 22:49:16 +0000374 int FrameIdx,
375 const TargetRegisterClass *RC,
376 SmallVectorImpl<MachineInstr*> &NewMIs) const{
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000377 DebugLoc DL;
Owen Andersonf6372aa2008-01-01 21:11:32 +0000378 if (RC == PPC::GPRCRegisterClass) {
379 if (SrcReg != PPC::LR) {
Dale Johannesen21b55412009-02-12 23:08:38 +0000380 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
Bill Wendling587daed2009-05-13 21:33:08 +0000381 .addReg(SrcReg,
382 getKillRegState(isKill)),
Bill Wendling4a66e9a2008-03-10 22:49:16 +0000383 FrameIdx));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000384 } else {
385 // FIXME: this spills LR immediately to memory in one step. To do this,
386 // we use R11, which we know cannot be used in the prolog/epilog. This is
387 // a hack.
Dale Johannesen21b55412009-02-12 23:08:38 +0000388 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MFLR), PPC::R11));
389 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
Bill Wendling587daed2009-05-13 21:33:08 +0000390 .addReg(PPC::R11,
391 getKillRegState(isKill)),
Bill Wendling4a66e9a2008-03-10 22:49:16 +0000392 FrameIdx));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000393 }
394 } else if (RC == PPC::G8RCRegisterClass) {
395 if (SrcReg != PPC::LR8) {
Dale Johannesen21b55412009-02-12 23:08:38 +0000396 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STD))
Bill Wendling587daed2009-05-13 21:33:08 +0000397 .addReg(SrcReg,
398 getKillRegState(isKill)),
399 FrameIdx));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000400 } else {
401 // FIXME: this spills LR immediately to memory in one step. To do this,
402 // we use R11, which we know cannot be used in the prolog/epilog. This is
403 // a hack.
Dale Johannesen21b55412009-02-12 23:08:38 +0000404 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MFLR8), PPC::X11));
405 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STD))
Bill Wendling587daed2009-05-13 21:33:08 +0000406 .addReg(PPC::X11,
407 getKillRegState(isKill)),
408 FrameIdx));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000409 }
410 } else if (RC == PPC::F8RCRegisterClass) {
Dale Johannesen21b55412009-02-12 23:08:38 +0000411 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFD))
Bill Wendling587daed2009-05-13 21:33:08 +0000412 .addReg(SrcReg,
413 getKillRegState(isKill)),
414 FrameIdx));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000415 } else if (RC == PPC::F4RCRegisterClass) {
Dale Johannesen21b55412009-02-12 23:08:38 +0000416 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFS))
Bill Wendling587daed2009-05-13 21:33:08 +0000417 .addReg(SrcReg,
418 getKillRegState(isKill)),
419 FrameIdx));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000420 } else if (RC == PPC::CRRCRegisterClass) {
Bill Wendling4a66e9a2008-03-10 22:49:16 +0000421 if ((EnablePPC32RS && !TM.getSubtargetImpl()->isPPC64()) ||
422 (EnablePPC64RS && TM.getSubtargetImpl()->isPPC64())) {
423 // FIXME (64-bit): Enable
Dale Johannesen21b55412009-02-12 23:08:38 +0000424 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CR))
Bill Wendling587daed2009-05-13 21:33:08 +0000425 .addReg(SrcReg,
426 getKillRegState(isKill)),
Chris Lattner71a2cb22008-03-20 01:22:40 +0000427 FrameIdx));
Bill Wendling7194aaf2008-03-03 22:19:16 +0000428 return true;
429 } else {
Dale Johannesenc12da8d2010-02-12 21:35:34 +0000430 // FIXME: We need a scatch reg here. The trouble with using R0 is that
431 // it's possible for the stack frame to be so big the save location is
432 // out of range of immediate offsets, necessitating another register.
433 // We hack this on Darwin by reserving R2. It's probably broken on Linux
434 // at the moment.
435
436 // We need to store the CR in the low 4-bits of the saved value. First,
437 // issue a MFCR to save all of the CRBits.
438 unsigned ScratchReg = TM.getSubtargetImpl()->isDarwinABI() ?
439 PPC::R2 : PPC::R0;
Dale Johannesen5f07d522010-05-20 17:48:26 +0000440 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MFCRpseud), ScratchReg)
441 .addReg(SrcReg, getKillRegState(isKill)));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000442
Bill Wendling7194aaf2008-03-03 22:19:16 +0000443 // If the saved register wasn't CR0, shift the bits left so that they are
444 // in CR0's slot.
445 if (SrcReg != PPC::CR0) {
446 unsigned ShiftBits = PPCRegisterInfo::getRegisterNumbering(SrcReg)*4;
Dale Johannesenc12da8d2010-02-12 21:35:34 +0000447 // rlwinm scratch, scratch, ShiftBits, 0, 31.
448 NewMIs.push_back(BuildMI(MF, DL, get(PPC::RLWINM), ScratchReg)
449 .addReg(ScratchReg).addImm(ShiftBits)
450 .addImm(0).addImm(31));
Bill Wendling7194aaf2008-03-03 22:19:16 +0000451 }
452
Dale Johannesen21b55412009-02-12 23:08:38 +0000453 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
Dale Johannesenc12da8d2010-02-12 21:35:34 +0000454 .addReg(ScratchReg,
Bill Wendling587daed2009-05-13 21:33:08 +0000455 getKillRegState(isKill)),
Bill Wendling7194aaf2008-03-03 22:19:16 +0000456 FrameIdx));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000457 }
Nicolas Geoffray0404cd92008-03-10 14:12:10 +0000458 } else if (RC == PPC::CRBITRCRegisterClass) {
459 // FIXME: We use CRi here because there is no mtcrf on a bit. Since the
460 // backend currently only uses CR1EQ as an individual bit, this should
461 // not cause any bug. If we need other uses of CR bits, the following
462 // code may be invalid.
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000463 unsigned Reg = 0;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000464 if (SrcReg == PPC::CR0LT || SrcReg == PPC::CR0GT ||
465 SrcReg == PPC::CR0EQ || SrcReg == PPC::CR0UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000466 Reg = PPC::CR0;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000467 else if (SrcReg == PPC::CR1LT || SrcReg == PPC::CR1GT ||
468 SrcReg == PPC::CR1EQ || SrcReg == PPC::CR1UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000469 Reg = PPC::CR1;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000470 else if (SrcReg == PPC::CR2LT || SrcReg == PPC::CR2GT ||
471 SrcReg == PPC::CR2EQ || SrcReg == PPC::CR2UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000472 Reg = PPC::CR2;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000473 else if (SrcReg == PPC::CR3LT || SrcReg == PPC::CR3GT ||
474 SrcReg == PPC::CR3EQ || SrcReg == PPC::CR3UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000475 Reg = PPC::CR3;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000476 else if (SrcReg == PPC::CR4LT || SrcReg == PPC::CR4GT ||
477 SrcReg == PPC::CR4EQ || SrcReg == PPC::CR4UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000478 Reg = PPC::CR4;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000479 else if (SrcReg == PPC::CR5LT || SrcReg == PPC::CR5GT ||
480 SrcReg == PPC::CR5EQ || SrcReg == PPC::CR5UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000481 Reg = PPC::CR5;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000482 else if (SrcReg == PPC::CR6LT || SrcReg == PPC::CR6GT ||
483 SrcReg == PPC::CR6EQ || SrcReg == PPC::CR6UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000484 Reg = PPC::CR6;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000485 else if (SrcReg == PPC::CR7LT || SrcReg == PPC::CR7GT ||
486 SrcReg == PPC::CR7EQ || SrcReg == PPC::CR7UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000487 Reg = PPC::CR7;
488
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000489 return StoreRegToStackSlot(MF, Reg, isKill, FrameIdx,
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000490 PPC::CRRCRegisterClass, NewMIs);
491
Owen Andersonf6372aa2008-01-01 21:11:32 +0000492 } else if (RC == PPC::VRRCRegisterClass) {
493 // We don't have indexed addressing for vector loads. Emit:
494 // R0 = ADDI FI#
495 // STVX VAL, 0, R0
496 //
497 // FIXME: We use R0 here, because it isn't available for RA.
Dale Johannesen21b55412009-02-12 23:08:38 +0000498 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::ADDI), PPC::R0),
Owen Andersonf6372aa2008-01-01 21:11:32 +0000499 FrameIdx, 0, 0));
Dale Johannesen21b55412009-02-12 23:08:38 +0000500 NewMIs.push_back(BuildMI(MF, DL, get(PPC::STVX))
Bill Wendling587daed2009-05-13 21:33:08 +0000501 .addReg(SrcReg, getKillRegState(isKill))
502 .addReg(PPC::R0)
503 .addReg(PPC::R0));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000504 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000505 llvm_unreachable("Unknown regclass!");
Owen Andersonf6372aa2008-01-01 21:11:32 +0000506 }
Bill Wendling7194aaf2008-03-03 22:19:16 +0000507
508 return false;
Owen Andersonf6372aa2008-01-01 21:11:32 +0000509}
510
511void
512PPCInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
Bill Wendling7194aaf2008-03-03 22:19:16 +0000513 MachineBasicBlock::iterator MI,
514 unsigned SrcReg, bool isKill, int FrameIdx,
Evan Cheng746ad692010-05-06 19:06:44 +0000515 const TargetRegisterClass *RC,
516 const TargetRegisterInfo *TRI) const {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000517 MachineFunction &MF = *MBB.getParent();
Owen Andersonf6372aa2008-01-01 21:11:32 +0000518 SmallVector<MachineInstr*, 4> NewMIs;
Bill Wendling7194aaf2008-03-03 22:19:16 +0000519
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000520 if (StoreRegToStackSlot(MF, SrcReg, isKill, FrameIdx, RC, NewMIs)) {
521 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Bill Wendling7194aaf2008-03-03 22:19:16 +0000522 FuncInfo->setSpillsCR();
523 }
524
Owen Andersonf6372aa2008-01-01 21:11:32 +0000525 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
526 MBB.insert(MI, NewMIs[i]);
527}
528
Bill Wendling4a66e9a2008-03-10 22:49:16 +0000529void
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000530PPCInstrInfo::LoadRegFromStackSlot(MachineFunction &MF, DebugLoc DL,
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000531 unsigned DestReg, int FrameIdx,
Bill Wendling4a66e9a2008-03-10 22:49:16 +0000532 const TargetRegisterClass *RC,
533 SmallVectorImpl<MachineInstr*> &NewMIs)const{
Owen Andersonf6372aa2008-01-01 21:11:32 +0000534 if (RC == PPC::GPRCRegisterClass) {
535 if (DestReg != PPC::LR) {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000536 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ),
537 DestReg), FrameIdx));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000538 } else {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000539 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ),
540 PPC::R11), FrameIdx));
541 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MTLR)).addReg(PPC::R11));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000542 }
543 } else if (RC == PPC::G8RCRegisterClass) {
544 if (DestReg != PPC::LR8) {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000545 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LD), DestReg),
Owen Andersonf6372aa2008-01-01 21:11:32 +0000546 FrameIdx));
547 } else {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000548 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LD),
549 PPC::R11), FrameIdx));
550 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MTLR8)).addReg(PPC::R11));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000551 }
552 } else if (RC == PPC::F8RCRegisterClass) {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000553 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFD), DestReg),
Owen Andersonf6372aa2008-01-01 21:11:32 +0000554 FrameIdx));
555 } else if (RC == PPC::F4RCRegisterClass) {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000556 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFS), DestReg),
Owen Andersonf6372aa2008-01-01 21:11:32 +0000557 FrameIdx));
558 } else if (RC == PPC::CRRCRegisterClass) {
Dale Johannesenc12da8d2010-02-12 21:35:34 +0000559 // FIXME: We need a scatch reg here. The trouble with using R0 is that
560 // it's possible for the stack frame to be so big the save location is
561 // out of range of immediate offsets, necessitating another register.
562 // We hack this on Darwin by reserving R2. It's probably broken on Linux
563 // at the moment.
564 unsigned ScratchReg = TM.getSubtargetImpl()->isDarwinABI() ?
565 PPC::R2 : PPC::R0;
566 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ),
567 ScratchReg), FrameIdx));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000568
569 // If the reloaded register isn't CR0, shift the bits right so that they are
570 // in the right CR's slot.
571 if (DestReg != PPC::CR0) {
572 unsigned ShiftBits = PPCRegisterInfo::getRegisterNumbering(DestReg)*4;
573 // rlwinm r11, r11, 32-ShiftBits, 0, 31.
Dale Johannesenc12da8d2010-02-12 21:35:34 +0000574 NewMIs.push_back(BuildMI(MF, DL, get(PPC::RLWINM), ScratchReg)
575 .addReg(ScratchReg).addImm(32-ShiftBits).addImm(0)
576 .addImm(31));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000577 }
578
Dale Johannesenc12da8d2010-02-12 21:35:34 +0000579 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MTCRF), DestReg)
580 .addReg(ScratchReg));
Nicolas Geoffray0404cd92008-03-10 14:12:10 +0000581 } else if (RC == PPC::CRBITRCRegisterClass) {
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000582
583 unsigned Reg = 0;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000584 if (DestReg == PPC::CR0LT || DestReg == PPC::CR0GT ||
585 DestReg == PPC::CR0EQ || DestReg == PPC::CR0UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000586 Reg = PPC::CR0;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000587 else if (DestReg == PPC::CR1LT || DestReg == PPC::CR1GT ||
588 DestReg == PPC::CR1EQ || DestReg == PPC::CR1UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000589 Reg = PPC::CR1;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000590 else if (DestReg == PPC::CR2LT || DestReg == PPC::CR2GT ||
591 DestReg == PPC::CR2EQ || DestReg == PPC::CR2UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000592 Reg = PPC::CR2;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000593 else if (DestReg == PPC::CR3LT || DestReg == PPC::CR3GT ||
594 DestReg == PPC::CR3EQ || DestReg == PPC::CR3UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000595 Reg = PPC::CR3;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000596 else if (DestReg == PPC::CR4LT || DestReg == PPC::CR4GT ||
597 DestReg == PPC::CR4EQ || DestReg == PPC::CR4UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000598 Reg = PPC::CR4;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000599 else if (DestReg == PPC::CR5LT || DestReg == PPC::CR5GT ||
600 DestReg == PPC::CR5EQ || DestReg == PPC::CR5UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000601 Reg = PPC::CR5;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000602 else if (DestReg == PPC::CR6LT || DestReg == PPC::CR6GT ||
603 DestReg == PPC::CR6EQ || DestReg == PPC::CR6UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000604 Reg = PPC::CR6;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000605 else if (DestReg == PPC::CR7LT || DestReg == PPC::CR7GT ||
606 DestReg == PPC::CR7EQ || DestReg == PPC::CR7UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000607 Reg = PPC::CR7;
608
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000609 return LoadRegFromStackSlot(MF, DL, Reg, FrameIdx,
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000610 PPC::CRRCRegisterClass, NewMIs);
611
Owen Andersonf6372aa2008-01-01 21:11:32 +0000612 } else if (RC == PPC::VRRCRegisterClass) {
613 // We don't have indexed addressing for vector loads. Emit:
614 // R0 = ADDI FI#
615 // Dest = LVX 0, R0
616 //
617 // FIXME: We use R0 here, because it isn't available for RA.
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000618 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::ADDI), PPC::R0),
Owen Andersonf6372aa2008-01-01 21:11:32 +0000619 FrameIdx, 0, 0));
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000620 NewMIs.push_back(BuildMI(MF, DL, get(PPC::LVX),DestReg).addReg(PPC::R0)
Owen Andersonf6372aa2008-01-01 21:11:32 +0000621 .addReg(PPC::R0));
622 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000623 llvm_unreachable("Unknown regclass!");
Owen Andersonf6372aa2008-01-01 21:11:32 +0000624 }
625}
626
627void
628PPCInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
Bill Wendling7194aaf2008-03-03 22:19:16 +0000629 MachineBasicBlock::iterator MI,
630 unsigned DestReg, int FrameIdx,
Evan Cheng746ad692010-05-06 19:06:44 +0000631 const TargetRegisterClass *RC,
632 const TargetRegisterInfo *TRI) const {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000633 MachineFunction &MF = *MBB.getParent();
Owen Andersonf6372aa2008-01-01 21:11:32 +0000634 SmallVector<MachineInstr*, 4> NewMIs;
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000635 DebugLoc DL;
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000636 if (MI != MBB.end()) DL = MI->getDebugLoc();
637 LoadRegFromStackSlot(MF, DL, DestReg, FrameIdx, RC, NewMIs);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000638 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
639 MBB.insert(MI, NewMIs[i]);
640}
641
Evan Cheng09652172010-04-26 07:39:36 +0000642MachineInstr*
643PPCInstrInfo::emitFrameIndexDebugValue(MachineFunction &MF,
Evan Cheng8601a3d2010-04-29 01:13:30 +0000644 int FrameIx, uint64_t Offset,
Evan Cheng09652172010-04-26 07:39:36 +0000645 const MDNode *MDPtr,
646 DebugLoc DL) const {
647 MachineInstrBuilder MIB = BuildMI(MF, DL, get(PPC::DBG_VALUE));
648 addFrameReference(MIB, FrameIx, 0, false).addImm(Offset).addMetadata(MDPtr);
649 return &*MIB;
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();
Evan Cheng2578ba22009-07-01 01:59:31 +0000671 bool isUndef = MI->getOperand(1).isUndef();
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000672 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::STW))
Evan Cheng2578ba22009-07-01 01:59:31 +0000673 .addReg(InReg,
674 getKillRegState(isKill) |
675 getUndefRegState(isUndef)),
Owen Anderson43dbe052008-01-07 01:35:02 +0000676 FrameIndex);
677 } else { // move -> load
678 unsigned OutReg = MI->getOperand(0).getReg();
Evan Cheng9f1c8312008-07-03 09:09:37 +0000679 bool isDead = MI->getOperand(0).isDead();
Evan Cheng2578ba22009-07-01 01:59:31 +0000680 bool isUndef = MI->getOperand(0).isUndef();
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000681 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::LWZ))
Bill Wendling587daed2009-05-13 21:33:08 +0000682 .addReg(OutReg,
683 RegState::Define |
Evan Cheng2578ba22009-07-01 01:59:31 +0000684 getDeadRegState(isDead) |
685 getUndefRegState(isUndef)),
Owen Anderson43dbe052008-01-07 01:35:02 +0000686 FrameIndex);
687 }
688 } else if ((Opc == PPC::OR8 &&
689 MI->getOperand(1).getReg() == MI->getOperand(2).getReg())) {
690 if (OpNum == 0) { // move -> store
691 unsigned InReg = MI->getOperand(1).getReg();
Evan Cheng9f1c8312008-07-03 09:09:37 +0000692 bool isKill = MI->getOperand(1).isKill();
Evan Cheng2578ba22009-07-01 01:59:31 +0000693 bool isUndef = MI->getOperand(1).isUndef();
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000694 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::STD))
Evan Cheng2578ba22009-07-01 01:59:31 +0000695 .addReg(InReg,
696 getKillRegState(isKill) |
697 getUndefRegState(isUndef)),
Owen Anderson43dbe052008-01-07 01:35:02 +0000698 FrameIndex);
699 } else { // move -> load
700 unsigned OutReg = MI->getOperand(0).getReg();
Evan Cheng9f1c8312008-07-03 09:09:37 +0000701 bool isDead = MI->getOperand(0).isDead();
Evan Cheng2578ba22009-07-01 01:59:31 +0000702 bool isUndef = MI->getOperand(0).isUndef();
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000703 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::LD))
Bill Wendling587daed2009-05-13 21:33:08 +0000704 .addReg(OutReg,
705 RegState::Define |
Evan Cheng2578ba22009-07-01 01:59:31 +0000706 getDeadRegState(isDead) |
707 getUndefRegState(isUndef)),
Evan Cheng9f1c8312008-07-03 09:09:37 +0000708 FrameIndex);
Owen Anderson43dbe052008-01-07 01:35:02 +0000709 }
Jakob Stoklund Olesenbaafcbb42010-02-26 21:53:24 +0000710 } else if (Opc == PPC::FMR || Opc == PPC::FMRSD) {
Jakob Stoklund Olesen24329662010-02-26 21:09:24 +0000711 // The register may be F4RC or F8RC, and that determines the memory op.
712 unsigned OrigReg = MI->getOperand(OpNum).getReg();
713 // We cannot tell the register class from a physreg alone.
714 if (TargetRegisterInfo::isPhysicalRegister(OrigReg))
715 return NULL;
716 const TargetRegisterClass *RC = MF.getRegInfo().getRegClass(OrigReg);
717 const bool is64 = RC == PPC::F8RCRegisterClass;
718
Owen Anderson43dbe052008-01-07 01:35:02 +0000719 if (OpNum == 0) { // move -> store
720 unsigned InReg = MI->getOperand(1).getReg();
Evan Cheng9f1c8312008-07-03 09:09:37 +0000721 bool isKill = MI->getOperand(1).isKill();
Evan Cheng2578ba22009-07-01 01:59:31 +0000722 bool isUndef = MI->getOperand(1).isUndef();
Jakob Stoklund Olesen24329662010-02-26 21:09:24 +0000723 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(),
724 get(is64 ? PPC::STFD : PPC::STFS))
Evan Cheng2578ba22009-07-01 01:59:31 +0000725 .addReg(InReg,
726 getKillRegState(isKill) |
727 getUndefRegState(isUndef)),
Owen Anderson43dbe052008-01-07 01:35:02 +0000728 FrameIndex);
729 } else { // move -> load
730 unsigned OutReg = MI->getOperand(0).getReg();
Evan Cheng9f1c8312008-07-03 09:09:37 +0000731 bool isDead = MI->getOperand(0).isDead();
Evan Cheng2578ba22009-07-01 01:59:31 +0000732 bool isUndef = MI->getOperand(0).isUndef();
Jakob Stoklund Olesen24329662010-02-26 21:09:24 +0000733 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(),
734 get(is64 ? PPC::LFD : PPC::LFS))
Bill Wendling587daed2009-05-13 21:33:08 +0000735 .addReg(OutReg,
736 RegState::Define |
Evan Cheng2578ba22009-07-01 01:59:31 +0000737 getDeadRegState(isDead) |
738 getUndefRegState(isUndef)),
Evan Cheng9f1c8312008-07-03 09:09:37 +0000739 FrameIndex);
Owen Anderson43dbe052008-01-07 01:35:02 +0000740 }
741 }
742
Owen Anderson43dbe052008-01-07 01:35:02 +0000743 return NewMI;
744}
745
Dan Gohman8e8b8a22008-10-16 01:49:15 +0000746bool PPCInstrInfo::canFoldMemoryOperand(const MachineInstr *MI,
747 const SmallVectorImpl<unsigned> &Ops) const {
Owen Anderson43dbe052008-01-07 01:35:02 +0000748 if (Ops.size() != 1) return false;
749
750 // Make sure this is a reg-reg copy. Note that we can't handle MCRF, because
751 // it takes more than one instruction to store it.
752 unsigned Opc = MI->getOpcode();
753
754 if ((Opc == PPC::OR &&
755 MI->getOperand(1).getReg() == MI->getOperand(2).getReg()))
756 return true;
757 else if ((Opc == PPC::OR8 &&
758 MI->getOperand(1).getReg() == MI->getOperand(2).getReg()))
759 return true;
Jakob Stoklund Olesenbaafcbb42010-02-26 21:53:24 +0000760 else if (Opc == PPC::FMR || Opc == PPC::FMRSD)
Owen Anderson43dbe052008-01-07 01:35:02 +0000761 return true;
762
763 return false;
764}
765
Owen Andersonf6372aa2008-01-01 21:11:32 +0000766
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000767bool PPCInstrInfo::
Owen Anderson44eb65c2008-08-14 22:49:33 +0000768ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
Chris Lattner7c4fe252006-10-21 06:03:11 +0000769 assert(Cond.size() == 2 && "Invalid PPC branch opcode!");
770 // Leave the CR# the same, but invert the condition.
Chris Lattner18258c62006-11-17 22:37:34 +0000771 Cond[0].setImm(PPC::InvertPredicate((PPC::Predicate)Cond[0].getImm()));
Chris Lattner7c4fe252006-10-21 06:03:11 +0000772 return false;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000773}
Nicolas Geoffray52e724a2008-04-16 20:10:13 +0000774
775/// GetInstSize - Return the number of bytes of code the specified
776/// instruction may be. This returns the maximum number of bytes.
777///
778unsigned PPCInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
779 switch (MI->getOpcode()) {
780 case PPC::INLINEASM: { // Inline Asm: Variable size.
781 const MachineFunction *MF = MI->getParent()->getParent();
782 const char *AsmStr = MI->getOperand(0).getSymbolName();
Chris Lattneraf76e592009-08-22 20:48:53 +0000783 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
Nicolas Geoffray52e724a2008-04-16 20:10:13 +0000784 }
Dan Gohman44066042008-07-01 00:05:16 +0000785 case PPC::DBG_LABEL:
786 case PPC::EH_LABEL:
787 case PPC::GC_LABEL:
Dale Johannesen375be772010-04-07 19:51:44 +0000788 case PPC::DBG_VALUE:
Nicolas Geoffray52e724a2008-04-16 20:10:13 +0000789 return 0;
Nicolas Geoffray52e724a2008-04-16 20:10:13 +0000790 default:
791 return 4; // PowerPC instructions are all 4 bytes
792 }
793}