blob: 134003b9ce32cc87ea5215795b96e074d76512dc [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
Owen Anderson940f83e2008-08-26 18:03:31 +0000343bool PPCInstrInfo::copyRegToReg(MachineBasicBlock &MBB,
Owen Andersond10fd972007-12-31 06:32:00 +0000344 MachineBasicBlock::iterator MI,
345 unsigned DestReg, unsigned SrcReg,
346 const TargetRegisterClass *DestRC,
Dan Gohman34dcc6f2010-05-06 20:33:48 +0000347 const TargetRegisterClass *SrcRC,
348 DebugLoc DL) const {
Owen Andersond10fd972007-12-31 06:32:00 +0000349 if (DestRC != SrcRC) {
Owen Anderson940f83e2008-08-26 18:03:31 +0000350 // Not yet supported!
351 return false;
Owen Andersond10fd972007-12-31 06:32:00 +0000352 }
353
354 if (DestRC == PPC::GPRCRegisterClass) {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000355 BuildMI(MBB, MI, DL, get(PPC::OR), DestReg).addReg(SrcReg).addReg(SrcReg);
Owen Andersond10fd972007-12-31 06:32:00 +0000356 } else if (DestRC == PPC::G8RCRegisterClass) {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000357 BuildMI(MBB, MI, DL, get(PPC::OR8), DestReg).addReg(SrcReg).addReg(SrcReg);
Jakob Stoklund Olesenbaafcbb42010-02-26 21:53:24 +0000358 } else if (DestRC == PPC::F4RCRegisterClass ||
359 DestRC == PPC::F8RCRegisterClass) {
360 BuildMI(MBB, MI, DL, get(PPC::FMR), DestReg).addReg(SrcReg);
Owen Andersond10fd972007-12-31 06:32:00 +0000361 } else if (DestRC == PPC::CRRCRegisterClass) {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000362 BuildMI(MBB, MI, DL, get(PPC::MCRF), DestReg).addReg(SrcReg);
Owen Andersond10fd972007-12-31 06:32:00 +0000363 } else if (DestRC == PPC::VRRCRegisterClass) {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000364 BuildMI(MBB, MI, DL, get(PPC::VOR), DestReg).addReg(SrcReg).addReg(SrcReg);
Nicolas Geoffray0404cd92008-03-10 14:12:10 +0000365 } else if (DestRC == PPC::CRBITRCRegisterClass) {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000366 BuildMI(MBB, MI, DL, get(PPC::CROR), DestReg).addReg(SrcReg).addReg(SrcReg);
Owen Andersond10fd972007-12-31 06:32:00 +0000367 } else {
Owen Anderson940f83e2008-08-26 18:03:31 +0000368 // Attempt to copy register that is not GPR or FPR
369 return false;
Owen Andersond10fd972007-12-31 06:32:00 +0000370 }
Owen Anderson940f83e2008-08-26 18:03:31 +0000371
372 return true;
Owen Andersond10fd972007-12-31 06:32:00 +0000373}
374
Bill Wendling4a66e9a2008-03-10 22:49:16 +0000375bool
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000376PPCInstrInfo::StoreRegToStackSlot(MachineFunction &MF,
377 unsigned SrcReg, bool isKill,
Bill Wendling4a66e9a2008-03-10 22:49:16 +0000378 int FrameIdx,
379 const TargetRegisterClass *RC,
380 SmallVectorImpl<MachineInstr*> &NewMIs) const{
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000381 DebugLoc DL;
Owen Andersonf6372aa2008-01-01 21:11:32 +0000382 if (RC == PPC::GPRCRegisterClass) {
383 if (SrcReg != PPC::LR) {
Dale Johannesen21b55412009-02-12 23:08:38 +0000384 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
Bill Wendling587daed2009-05-13 21:33:08 +0000385 .addReg(SrcReg,
386 getKillRegState(isKill)),
Bill Wendling4a66e9a2008-03-10 22:49:16 +0000387 FrameIdx));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000388 } else {
389 // FIXME: this spills LR immediately to memory in one step. To do this,
390 // we use R11, which we know cannot be used in the prolog/epilog. This is
391 // a hack.
Dale Johannesen21b55412009-02-12 23:08:38 +0000392 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MFLR), PPC::R11));
393 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
Bill Wendling587daed2009-05-13 21:33:08 +0000394 .addReg(PPC::R11,
395 getKillRegState(isKill)),
Bill Wendling4a66e9a2008-03-10 22:49:16 +0000396 FrameIdx));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000397 }
398 } else if (RC == PPC::G8RCRegisterClass) {
399 if (SrcReg != PPC::LR8) {
Dale Johannesen21b55412009-02-12 23:08:38 +0000400 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STD))
Bill Wendling587daed2009-05-13 21:33:08 +0000401 .addReg(SrcReg,
402 getKillRegState(isKill)),
403 FrameIdx));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000404 } else {
405 // FIXME: this spills LR immediately to memory in one step. To do this,
406 // we use R11, which we know cannot be used in the prolog/epilog. This is
407 // a hack.
Dale Johannesen21b55412009-02-12 23:08:38 +0000408 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MFLR8), PPC::X11));
409 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STD))
Bill Wendling587daed2009-05-13 21:33:08 +0000410 .addReg(PPC::X11,
411 getKillRegState(isKill)),
412 FrameIdx));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000413 }
414 } else if (RC == PPC::F8RCRegisterClass) {
Dale Johannesen21b55412009-02-12 23:08:38 +0000415 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFD))
Bill Wendling587daed2009-05-13 21:33:08 +0000416 .addReg(SrcReg,
417 getKillRegState(isKill)),
418 FrameIdx));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000419 } else if (RC == PPC::F4RCRegisterClass) {
Dale Johannesen21b55412009-02-12 23:08:38 +0000420 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFS))
Bill Wendling587daed2009-05-13 21:33:08 +0000421 .addReg(SrcReg,
422 getKillRegState(isKill)),
423 FrameIdx));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000424 } else if (RC == PPC::CRRCRegisterClass) {
Bill Wendling4a66e9a2008-03-10 22:49:16 +0000425 if ((EnablePPC32RS && !TM.getSubtargetImpl()->isPPC64()) ||
426 (EnablePPC64RS && TM.getSubtargetImpl()->isPPC64())) {
427 // FIXME (64-bit): Enable
Dale Johannesen21b55412009-02-12 23:08:38 +0000428 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CR))
Bill Wendling587daed2009-05-13 21:33:08 +0000429 .addReg(SrcReg,
430 getKillRegState(isKill)),
Chris Lattner71a2cb22008-03-20 01:22:40 +0000431 FrameIdx));
Bill Wendling7194aaf2008-03-03 22:19:16 +0000432 return true;
433 } else {
Dale Johannesenc12da8d2010-02-12 21:35:34 +0000434 // FIXME: We need a scatch reg here. The trouble with using R0 is that
435 // it's possible for the stack frame to be so big the save location is
436 // out of range of immediate offsets, necessitating another register.
437 // We hack this on Darwin by reserving R2. It's probably broken on Linux
438 // at the moment.
439
440 // We need to store the CR in the low 4-bits of the saved value. First,
441 // issue a MFCR to save all of the CRBits.
442 unsigned ScratchReg = TM.getSubtargetImpl()->isDarwinABI() ?
443 PPC::R2 : PPC::R0;
Dale Johannesen5f07d522010-05-20 17:48:26 +0000444 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MFCRpseud), ScratchReg)
445 .addReg(SrcReg, getKillRegState(isKill)));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000446
Bill Wendling7194aaf2008-03-03 22:19:16 +0000447 // If the saved register wasn't CR0, shift the bits left so that they are
448 // in CR0's slot.
449 if (SrcReg != PPC::CR0) {
450 unsigned ShiftBits = PPCRegisterInfo::getRegisterNumbering(SrcReg)*4;
Dale Johannesenc12da8d2010-02-12 21:35:34 +0000451 // rlwinm scratch, scratch, ShiftBits, 0, 31.
452 NewMIs.push_back(BuildMI(MF, DL, get(PPC::RLWINM), ScratchReg)
453 .addReg(ScratchReg).addImm(ShiftBits)
454 .addImm(0).addImm(31));
Bill Wendling7194aaf2008-03-03 22:19:16 +0000455 }
456
Dale Johannesen21b55412009-02-12 23:08:38 +0000457 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
Dale Johannesenc12da8d2010-02-12 21:35:34 +0000458 .addReg(ScratchReg,
Bill Wendling587daed2009-05-13 21:33:08 +0000459 getKillRegState(isKill)),
Bill Wendling7194aaf2008-03-03 22:19:16 +0000460 FrameIdx));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000461 }
Nicolas Geoffray0404cd92008-03-10 14:12:10 +0000462 } else if (RC == PPC::CRBITRCRegisterClass) {
463 // FIXME: We use CRi here because there is no mtcrf on a bit. Since the
464 // backend currently only uses CR1EQ as an individual bit, this should
465 // not cause any bug. If we need other uses of CR bits, the following
466 // code may be invalid.
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000467 unsigned Reg = 0;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000468 if (SrcReg == PPC::CR0LT || SrcReg == PPC::CR0GT ||
469 SrcReg == PPC::CR0EQ || SrcReg == PPC::CR0UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000470 Reg = PPC::CR0;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000471 else if (SrcReg == PPC::CR1LT || SrcReg == PPC::CR1GT ||
472 SrcReg == PPC::CR1EQ || SrcReg == PPC::CR1UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000473 Reg = PPC::CR1;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000474 else if (SrcReg == PPC::CR2LT || SrcReg == PPC::CR2GT ||
475 SrcReg == PPC::CR2EQ || SrcReg == PPC::CR2UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000476 Reg = PPC::CR2;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000477 else if (SrcReg == PPC::CR3LT || SrcReg == PPC::CR3GT ||
478 SrcReg == PPC::CR3EQ || SrcReg == PPC::CR3UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000479 Reg = PPC::CR3;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000480 else if (SrcReg == PPC::CR4LT || SrcReg == PPC::CR4GT ||
481 SrcReg == PPC::CR4EQ || SrcReg == PPC::CR4UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000482 Reg = PPC::CR4;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000483 else if (SrcReg == PPC::CR5LT || SrcReg == PPC::CR5GT ||
484 SrcReg == PPC::CR5EQ || SrcReg == PPC::CR5UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000485 Reg = PPC::CR5;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000486 else if (SrcReg == PPC::CR6LT || SrcReg == PPC::CR6GT ||
487 SrcReg == PPC::CR6EQ || SrcReg == PPC::CR6UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000488 Reg = PPC::CR6;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000489 else if (SrcReg == PPC::CR7LT || SrcReg == PPC::CR7GT ||
490 SrcReg == PPC::CR7EQ || SrcReg == PPC::CR7UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000491 Reg = PPC::CR7;
492
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000493 return StoreRegToStackSlot(MF, Reg, isKill, FrameIdx,
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000494 PPC::CRRCRegisterClass, NewMIs);
495
Owen Andersonf6372aa2008-01-01 21:11:32 +0000496 } else if (RC == PPC::VRRCRegisterClass) {
497 // We don't have indexed addressing for vector loads. Emit:
498 // R0 = ADDI FI#
499 // STVX VAL, 0, R0
500 //
501 // FIXME: We use R0 here, because it isn't available for RA.
Dale Johannesen21b55412009-02-12 23:08:38 +0000502 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::ADDI), PPC::R0),
Owen Andersonf6372aa2008-01-01 21:11:32 +0000503 FrameIdx, 0, 0));
Dale Johannesen21b55412009-02-12 23:08:38 +0000504 NewMIs.push_back(BuildMI(MF, DL, get(PPC::STVX))
Bill Wendling587daed2009-05-13 21:33:08 +0000505 .addReg(SrcReg, getKillRegState(isKill))
506 .addReg(PPC::R0)
507 .addReg(PPC::R0));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000508 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000509 llvm_unreachable("Unknown regclass!");
Owen Andersonf6372aa2008-01-01 21:11:32 +0000510 }
Bill Wendling7194aaf2008-03-03 22:19:16 +0000511
512 return false;
Owen Andersonf6372aa2008-01-01 21:11:32 +0000513}
514
515void
516PPCInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
Bill Wendling7194aaf2008-03-03 22:19:16 +0000517 MachineBasicBlock::iterator MI,
518 unsigned SrcReg, bool isKill, int FrameIdx,
Evan Cheng746ad692010-05-06 19:06:44 +0000519 const TargetRegisterClass *RC,
520 const TargetRegisterInfo *TRI) const {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000521 MachineFunction &MF = *MBB.getParent();
Owen Andersonf6372aa2008-01-01 21:11:32 +0000522 SmallVector<MachineInstr*, 4> NewMIs;
Bill Wendling7194aaf2008-03-03 22:19:16 +0000523
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000524 if (StoreRegToStackSlot(MF, SrcReg, isKill, FrameIdx, RC, NewMIs)) {
525 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Bill Wendling7194aaf2008-03-03 22:19:16 +0000526 FuncInfo->setSpillsCR();
527 }
528
Owen Andersonf6372aa2008-01-01 21:11:32 +0000529 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
530 MBB.insert(MI, NewMIs[i]);
531}
532
Bill Wendling4a66e9a2008-03-10 22:49:16 +0000533void
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000534PPCInstrInfo::LoadRegFromStackSlot(MachineFunction &MF, DebugLoc DL,
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000535 unsigned DestReg, int FrameIdx,
Bill Wendling4a66e9a2008-03-10 22:49:16 +0000536 const TargetRegisterClass *RC,
537 SmallVectorImpl<MachineInstr*> &NewMIs)const{
Owen Andersonf6372aa2008-01-01 21:11:32 +0000538 if (RC == PPC::GPRCRegisterClass) {
539 if (DestReg != PPC::LR) {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000540 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ),
541 DestReg), FrameIdx));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000542 } else {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000543 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ),
544 PPC::R11), FrameIdx));
545 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MTLR)).addReg(PPC::R11));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000546 }
547 } else if (RC == PPC::G8RCRegisterClass) {
548 if (DestReg != PPC::LR8) {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000549 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LD), DestReg),
Owen Andersonf6372aa2008-01-01 21:11:32 +0000550 FrameIdx));
551 } else {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000552 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LD),
553 PPC::R11), FrameIdx));
554 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MTLR8)).addReg(PPC::R11));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000555 }
556 } else if (RC == PPC::F8RCRegisterClass) {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000557 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFD), DestReg),
Owen Andersonf6372aa2008-01-01 21:11:32 +0000558 FrameIdx));
559 } else if (RC == PPC::F4RCRegisterClass) {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000560 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFS), DestReg),
Owen Andersonf6372aa2008-01-01 21:11:32 +0000561 FrameIdx));
562 } else if (RC == PPC::CRRCRegisterClass) {
Dale Johannesenc12da8d2010-02-12 21:35:34 +0000563 // FIXME: We need a scatch reg here. The trouble with using R0 is that
564 // it's possible for the stack frame to be so big the save location is
565 // out of range of immediate offsets, necessitating another register.
566 // We hack this on Darwin by reserving R2. It's probably broken on Linux
567 // at the moment.
568 unsigned ScratchReg = TM.getSubtargetImpl()->isDarwinABI() ?
569 PPC::R2 : PPC::R0;
570 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ),
571 ScratchReg), FrameIdx));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000572
573 // If the reloaded register isn't CR0, shift the bits right so that they are
574 // in the right CR's slot.
575 if (DestReg != PPC::CR0) {
576 unsigned ShiftBits = PPCRegisterInfo::getRegisterNumbering(DestReg)*4;
577 // rlwinm r11, r11, 32-ShiftBits, 0, 31.
Dale Johannesenc12da8d2010-02-12 21:35:34 +0000578 NewMIs.push_back(BuildMI(MF, DL, get(PPC::RLWINM), ScratchReg)
579 .addReg(ScratchReg).addImm(32-ShiftBits).addImm(0)
580 .addImm(31));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000581 }
582
Dale Johannesenc12da8d2010-02-12 21:35:34 +0000583 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MTCRF), DestReg)
584 .addReg(ScratchReg));
Nicolas Geoffray0404cd92008-03-10 14:12:10 +0000585 } else if (RC == PPC::CRBITRCRegisterClass) {
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000586
587 unsigned Reg = 0;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000588 if (DestReg == PPC::CR0LT || DestReg == PPC::CR0GT ||
589 DestReg == PPC::CR0EQ || DestReg == PPC::CR0UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000590 Reg = PPC::CR0;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000591 else if (DestReg == PPC::CR1LT || DestReg == PPC::CR1GT ||
592 DestReg == PPC::CR1EQ || DestReg == PPC::CR1UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000593 Reg = PPC::CR1;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000594 else if (DestReg == PPC::CR2LT || DestReg == PPC::CR2GT ||
595 DestReg == PPC::CR2EQ || DestReg == PPC::CR2UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000596 Reg = PPC::CR2;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000597 else if (DestReg == PPC::CR3LT || DestReg == PPC::CR3GT ||
598 DestReg == PPC::CR3EQ || DestReg == PPC::CR3UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000599 Reg = PPC::CR3;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000600 else if (DestReg == PPC::CR4LT || DestReg == PPC::CR4GT ||
601 DestReg == PPC::CR4EQ || DestReg == PPC::CR4UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000602 Reg = PPC::CR4;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000603 else if (DestReg == PPC::CR5LT || DestReg == PPC::CR5GT ||
604 DestReg == PPC::CR5EQ || DestReg == PPC::CR5UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000605 Reg = PPC::CR5;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000606 else if (DestReg == PPC::CR6LT || DestReg == PPC::CR6GT ||
607 DestReg == PPC::CR6EQ || DestReg == PPC::CR6UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000608 Reg = PPC::CR6;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000609 else if (DestReg == PPC::CR7LT || DestReg == PPC::CR7GT ||
610 DestReg == PPC::CR7EQ || DestReg == PPC::CR7UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000611 Reg = PPC::CR7;
612
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000613 return LoadRegFromStackSlot(MF, DL, Reg, FrameIdx,
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000614 PPC::CRRCRegisterClass, NewMIs);
615
Owen Andersonf6372aa2008-01-01 21:11:32 +0000616 } else if (RC == PPC::VRRCRegisterClass) {
617 // We don't have indexed addressing for vector loads. Emit:
618 // R0 = ADDI FI#
619 // Dest = LVX 0, R0
620 //
621 // FIXME: We use R0 here, because it isn't available for RA.
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000622 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::ADDI), PPC::R0),
Owen Andersonf6372aa2008-01-01 21:11:32 +0000623 FrameIdx, 0, 0));
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000624 NewMIs.push_back(BuildMI(MF, DL, get(PPC::LVX),DestReg).addReg(PPC::R0)
Owen Andersonf6372aa2008-01-01 21:11:32 +0000625 .addReg(PPC::R0));
626 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000627 llvm_unreachable("Unknown regclass!");
Owen Andersonf6372aa2008-01-01 21:11:32 +0000628 }
629}
630
631void
632PPCInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
Bill Wendling7194aaf2008-03-03 22:19:16 +0000633 MachineBasicBlock::iterator MI,
634 unsigned DestReg, int FrameIdx,
Evan Cheng746ad692010-05-06 19:06:44 +0000635 const TargetRegisterClass *RC,
636 const TargetRegisterInfo *TRI) const {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000637 MachineFunction &MF = *MBB.getParent();
Owen Andersonf6372aa2008-01-01 21:11:32 +0000638 SmallVector<MachineInstr*, 4> NewMIs;
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000639 DebugLoc DL;
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000640 if (MI != MBB.end()) DL = MI->getDebugLoc();
641 LoadRegFromStackSlot(MF, DL, DestReg, FrameIdx, RC, NewMIs);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000642 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
643 MBB.insert(MI, NewMIs[i]);
644}
645
Evan Cheng09652172010-04-26 07:39:36 +0000646MachineInstr*
647PPCInstrInfo::emitFrameIndexDebugValue(MachineFunction &MF,
Evan Cheng8601a3d2010-04-29 01:13:30 +0000648 int FrameIx, uint64_t Offset,
Evan Cheng09652172010-04-26 07:39:36 +0000649 const MDNode *MDPtr,
650 DebugLoc DL) const {
651 MachineInstrBuilder MIB = BuildMI(MF, DL, get(PPC::DBG_VALUE));
652 addFrameReference(MIB, FrameIx, 0, false).addImm(Offset).addMetadata(MDPtr);
653 return &*MIB;
654}
655
Owen Anderson43dbe052008-01-07 01:35:02 +0000656/// foldMemoryOperand - PowerPC (like most RISC's) can only fold spills into
657/// copy instructions, turning them into load/store instructions.
Dan Gohmanc54baa22008-12-03 18:43:12 +0000658MachineInstr *PPCInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
659 MachineInstr *MI,
660 const SmallVectorImpl<unsigned> &Ops,
661 int FrameIndex) const {
Owen Anderson43dbe052008-01-07 01:35:02 +0000662 if (Ops.size() != 1) return NULL;
663
664 // Make sure this is a reg-reg copy. Note that we can't handle MCRF, because
665 // it takes more than one instruction to store it.
666 unsigned Opc = MI->getOpcode();
667 unsigned OpNum = Ops[0];
668
669 MachineInstr *NewMI = NULL;
670 if ((Opc == PPC::OR &&
671 MI->getOperand(1).getReg() == MI->getOperand(2).getReg())) {
672 if (OpNum == 0) { // move -> store
673 unsigned InReg = MI->getOperand(1).getReg();
Evan Cheng9f1c8312008-07-03 09:09:37 +0000674 bool isKill = MI->getOperand(1).isKill();
Evan Cheng2578ba22009-07-01 01:59:31 +0000675 bool isUndef = MI->getOperand(1).isUndef();
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000676 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::STW))
Evan Cheng2578ba22009-07-01 01:59:31 +0000677 .addReg(InReg,
678 getKillRegState(isKill) |
679 getUndefRegState(isUndef)),
Owen Anderson43dbe052008-01-07 01:35:02 +0000680 FrameIndex);
681 } else { // move -> load
682 unsigned OutReg = MI->getOperand(0).getReg();
Evan Cheng9f1c8312008-07-03 09:09:37 +0000683 bool isDead = MI->getOperand(0).isDead();
Evan Cheng2578ba22009-07-01 01:59:31 +0000684 bool isUndef = MI->getOperand(0).isUndef();
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000685 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::LWZ))
Bill Wendling587daed2009-05-13 21:33:08 +0000686 .addReg(OutReg,
687 RegState::Define |
Evan Cheng2578ba22009-07-01 01:59:31 +0000688 getDeadRegState(isDead) |
689 getUndefRegState(isUndef)),
Owen Anderson43dbe052008-01-07 01:35:02 +0000690 FrameIndex);
691 }
692 } else if ((Opc == PPC::OR8 &&
693 MI->getOperand(1).getReg() == MI->getOperand(2).getReg())) {
694 if (OpNum == 0) { // move -> store
695 unsigned InReg = MI->getOperand(1).getReg();
Evan Cheng9f1c8312008-07-03 09:09:37 +0000696 bool isKill = MI->getOperand(1).isKill();
Evan Cheng2578ba22009-07-01 01:59:31 +0000697 bool isUndef = MI->getOperand(1).isUndef();
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000698 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::STD))
Evan Cheng2578ba22009-07-01 01:59:31 +0000699 .addReg(InReg,
700 getKillRegState(isKill) |
701 getUndefRegState(isUndef)),
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();
Evan Cheng2578ba22009-07-01 01:59:31 +0000706 bool isUndef = MI->getOperand(0).isUndef();
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000707 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::LD))
Bill Wendling587daed2009-05-13 21:33:08 +0000708 .addReg(OutReg,
709 RegState::Define |
Evan Cheng2578ba22009-07-01 01:59:31 +0000710 getDeadRegState(isDead) |
711 getUndefRegState(isUndef)),
Evan Cheng9f1c8312008-07-03 09:09:37 +0000712 FrameIndex);
Owen Anderson43dbe052008-01-07 01:35:02 +0000713 }
Jakob Stoklund Olesenbaafcbb42010-02-26 21:53:24 +0000714 } else if (Opc == PPC::FMR || Opc == PPC::FMRSD) {
Jakob Stoklund Olesen24329662010-02-26 21:09:24 +0000715 // The register may be F4RC or F8RC, and that determines the memory op.
716 unsigned OrigReg = MI->getOperand(OpNum).getReg();
717 // We cannot tell the register class from a physreg alone.
718 if (TargetRegisterInfo::isPhysicalRegister(OrigReg))
719 return NULL;
720 const TargetRegisterClass *RC = MF.getRegInfo().getRegClass(OrigReg);
721 const bool is64 = RC == PPC::F8RCRegisterClass;
722
Owen Anderson43dbe052008-01-07 01:35:02 +0000723 if (OpNum == 0) { // move -> store
724 unsigned InReg = MI->getOperand(1).getReg();
Evan Cheng9f1c8312008-07-03 09:09:37 +0000725 bool isKill = MI->getOperand(1).isKill();
Evan Cheng2578ba22009-07-01 01:59:31 +0000726 bool isUndef = MI->getOperand(1).isUndef();
Jakob Stoklund Olesen24329662010-02-26 21:09:24 +0000727 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(),
728 get(is64 ? PPC::STFD : PPC::STFS))
Evan Cheng2578ba22009-07-01 01:59:31 +0000729 .addReg(InReg,
730 getKillRegState(isKill) |
731 getUndefRegState(isUndef)),
Owen Anderson43dbe052008-01-07 01:35:02 +0000732 FrameIndex);
733 } else { // move -> load
734 unsigned OutReg = MI->getOperand(0).getReg();
Evan Cheng9f1c8312008-07-03 09:09:37 +0000735 bool isDead = MI->getOperand(0).isDead();
Evan Cheng2578ba22009-07-01 01:59:31 +0000736 bool isUndef = MI->getOperand(0).isUndef();
Jakob Stoklund Olesen24329662010-02-26 21:09:24 +0000737 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(),
738 get(is64 ? PPC::LFD : PPC::LFS))
Bill Wendling587daed2009-05-13 21:33:08 +0000739 .addReg(OutReg,
740 RegState::Define |
Evan Cheng2578ba22009-07-01 01:59:31 +0000741 getDeadRegState(isDead) |
742 getUndefRegState(isUndef)),
Evan Cheng9f1c8312008-07-03 09:09:37 +0000743 FrameIndex);
Owen Anderson43dbe052008-01-07 01:35:02 +0000744 }
745 }
746
Owen Anderson43dbe052008-01-07 01:35:02 +0000747 return NewMI;
748}
749
Dan Gohman8e8b8a22008-10-16 01:49:15 +0000750bool PPCInstrInfo::canFoldMemoryOperand(const MachineInstr *MI,
751 const SmallVectorImpl<unsigned> &Ops) const {
Owen Anderson43dbe052008-01-07 01:35:02 +0000752 if (Ops.size() != 1) return false;
753
754 // Make sure this is a reg-reg copy. Note that we can't handle MCRF, because
755 // it takes more than one instruction to store it.
756 unsigned Opc = MI->getOpcode();
757
758 if ((Opc == PPC::OR &&
759 MI->getOperand(1).getReg() == MI->getOperand(2).getReg()))
760 return true;
761 else if ((Opc == PPC::OR8 &&
762 MI->getOperand(1).getReg() == MI->getOperand(2).getReg()))
763 return true;
Jakob Stoklund Olesenbaafcbb42010-02-26 21:53:24 +0000764 else if (Opc == PPC::FMR || Opc == PPC::FMRSD)
Owen Anderson43dbe052008-01-07 01:35:02 +0000765 return true;
766
767 return false;
768}
769
Owen Andersonf6372aa2008-01-01 21:11:32 +0000770
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000771bool PPCInstrInfo::
Owen Anderson44eb65c2008-08-14 22:49:33 +0000772ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
Chris Lattner7c4fe252006-10-21 06:03:11 +0000773 assert(Cond.size() == 2 && "Invalid PPC branch opcode!");
774 // Leave the CR# the same, but invert the condition.
Chris Lattner18258c62006-11-17 22:37:34 +0000775 Cond[0].setImm(PPC::InvertPredicate((PPC::Predicate)Cond[0].getImm()));
Chris Lattner7c4fe252006-10-21 06:03:11 +0000776 return false;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000777}
Nicolas Geoffray52e724a2008-04-16 20:10:13 +0000778
779/// GetInstSize - Return the number of bytes of code the specified
780/// instruction may be. This returns the maximum number of bytes.
781///
782unsigned PPCInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
783 switch (MI->getOpcode()) {
784 case PPC::INLINEASM: { // Inline Asm: Variable size.
785 const MachineFunction *MF = MI->getParent()->getParent();
786 const char *AsmStr = MI->getOperand(0).getSymbolName();
Chris Lattneraf76e592009-08-22 20:48:53 +0000787 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
Nicolas Geoffray52e724a2008-04-16 20:10:13 +0000788 }
Dan Gohman44066042008-07-01 00:05:16 +0000789 case PPC::DBG_LABEL:
790 case PPC::EH_LABEL:
791 case PPC::GC_LABEL:
Dale Johannesen375be772010-04-07 19:51:44 +0000792 case PPC::DBG_VALUE:
Nicolas Geoffray52e724a2008-04-16 20:10:13 +0000793 return 0;
Nicolas Geoffray52e724a2008-04-16 20:10:13 +0000794 default:
795 return 4; // PowerPC instructions are all 4 bytes
796 }
797}