blob: 778f0349d10f66af9df7d701cb41e2281d847ba2 [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();
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000177 return BuildMI(MF, MI->getDebugLoc(), MI->getDesc())
Bill Wendling587daed2009-05-13 21:33:08 +0000178 .addReg(Reg0, RegState::Define | getDeadRegState(Reg0IsDead))
179 .addReg(Reg2, getKillRegState(Reg2IsKill))
180 .addReg(Reg1, getKillRegState(Reg1IsKill))
Evan Cheng58dcb0e2008-06-16 07:33:11 +0000181 .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 {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000200 DebugLoc DL = DebugLoc::getUnknownLoc();
201 if (MI != MBB.end()) DL = MI->getDebugLoc();
202
203 BuildMI(MBB, MI, DL, get(PPC::NOP));
Chris Lattnerbbf1c722006-03-05 23:49:55 +0000204}
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000205
206
207// Branch analysis.
208bool PPCInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
209 MachineBasicBlock *&FBB,
Evan Chengdc54d312009-02-09 07:14:22 +0000210 SmallVectorImpl<MachineOperand> &Cond,
211 bool AllowModify) const {
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000212 // If the block has no terminators, it just falls into the block after it.
213 MachineBasicBlock::iterator I = MBB.end();
Evan Chengbfd2ec42007-06-08 21:59:56 +0000214 if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000215 return false;
216
217 // Get the last instruction in the block.
218 MachineInstr *LastInst = I;
219
220 // If there is only one terminator instruction, process it.
Evan Chengbfd2ec42007-06-08 21:59:56 +0000221 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000222 if (LastInst->getOpcode() == PPC::B) {
Evan Cheng82ae9332009-05-08 23:09:25 +0000223 if (!LastInst->getOperand(0).isMBB())
224 return true;
Chris Lattner8aa797a2007-12-30 23:10:15 +0000225 TBB = LastInst->getOperand(0).getMBB();
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000226 return false;
Chris Lattner289c2d52006-11-17 22:14:47 +0000227 } else if (LastInst->getOpcode() == PPC::BCC) {
Evan Cheng82ae9332009-05-08 23:09:25 +0000228 if (!LastInst->getOperand(2).isMBB())
229 return true;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000230 // Block ends with fall-through condbranch.
Chris Lattner8aa797a2007-12-30 23:10:15 +0000231 TBB = LastInst->getOperand(2).getMBB();
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000232 Cond.push_back(LastInst->getOperand(0));
233 Cond.push_back(LastInst->getOperand(1));
Chris Lattner7c4fe252006-10-21 06:03:11 +0000234 return false;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000235 }
236 // Otherwise, don't know what this is.
237 return true;
238 }
239
240 // Get the instruction before it if it's a terminator.
241 MachineInstr *SecondLastInst = I;
242
243 // If there are three terminators, we don't know what sort of block this is.
244 if (SecondLastInst && I != MBB.begin() &&
Evan Chengbfd2ec42007-06-08 21:59:56 +0000245 isUnpredicatedTerminator(--I))
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000246 return true;
247
Chris Lattner289c2d52006-11-17 22:14:47 +0000248 // If the block ends with PPC::B and PPC:BCC, handle it.
249 if (SecondLastInst->getOpcode() == PPC::BCC &&
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000250 LastInst->getOpcode() == PPC::B) {
Evan Cheng82ae9332009-05-08 23:09:25 +0000251 if (!SecondLastInst->getOperand(2).isMBB() ||
252 !LastInst->getOperand(0).isMBB())
253 return true;
Chris Lattner8aa797a2007-12-30 23:10:15 +0000254 TBB = SecondLastInst->getOperand(2).getMBB();
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000255 Cond.push_back(SecondLastInst->getOperand(0));
256 Cond.push_back(SecondLastInst->getOperand(1));
Chris Lattner8aa797a2007-12-30 23:10:15 +0000257 FBB = LastInst->getOperand(0).getMBB();
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000258 return false;
259 }
260
Dale Johannesen13e8b512007-06-13 17:59:52 +0000261 // If the block ends with two PPC:Bs, handle it. The second one is not
262 // executed, so remove it.
263 if (SecondLastInst->getOpcode() == PPC::B &&
264 LastInst->getOpcode() == PPC::B) {
Evan Cheng82ae9332009-05-08 23:09:25 +0000265 if (!SecondLastInst->getOperand(0).isMBB())
266 return true;
Chris Lattner8aa797a2007-12-30 23:10:15 +0000267 TBB = SecondLastInst->getOperand(0).getMBB();
Dale Johannesen13e8b512007-06-13 17:59:52 +0000268 I = LastInst;
Evan Chengdc54d312009-02-09 07:14:22 +0000269 if (AllowModify)
270 I->eraseFromParent();
Dale Johannesen13e8b512007-06-13 17:59:52 +0000271 return false;
272 }
273
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000274 // Otherwise, can't handle this.
275 return true;
276}
277
Evan Chengb5cdaa22007-05-18 00:05:48 +0000278unsigned PPCInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000279 MachineBasicBlock::iterator I = MBB.end();
Evan Chengb5cdaa22007-05-18 00:05:48 +0000280 if (I == MBB.begin()) return 0;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000281 --I;
Chris Lattner289c2d52006-11-17 22:14:47 +0000282 if (I->getOpcode() != PPC::B && I->getOpcode() != PPC::BCC)
Evan Chengb5cdaa22007-05-18 00:05:48 +0000283 return 0;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000284
285 // Remove the branch.
286 I->eraseFromParent();
287
288 I = MBB.end();
289
Evan Chengb5cdaa22007-05-18 00:05:48 +0000290 if (I == MBB.begin()) return 1;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000291 --I;
Chris Lattner289c2d52006-11-17 22:14:47 +0000292 if (I->getOpcode() != PPC::BCC)
Evan Chengb5cdaa22007-05-18 00:05:48 +0000293 return 1;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000294
295 // Remove the branch.
296 I->eraseFromParent();
Evan Chengb5cdaa22007-05-18 00:05:48 +0000297 return 2;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000298}
299
Evan Chengb5cdaa22007-05-18 00:05:48 +0000300unsigned
301PPCInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
302 MachineBasicBlock *FBB,
Owen Anderson44eb65c2008-08-14 22:49:33 +0000303 const SmallVectorImpl<MachineOperand> &Cond) const {
Dale Johannesen536a2f12009-02-13 02:27:39 +0000304 // FIXME this should probably have a DebugLoc argument
305 DebugLoc dl = DebugLoc::getUnknownLoc();
Chris Lattner2dc77232006-10-17 18:06:55 +0000306 // Shouldn't be a fall through.
307 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Chris Lattner54108062006-10-21 05:36:13 +0000308 assert((Cond.size() == 2 || Cond.size() == 0) &&
309 "PPC branch conditions have two components!");
Chris Lattner2dc77232006-10-17 18:06:55 +0000310
Chris Lattner54108062006-10-21 05:36:13 +0000311 // One-way branch.
Chris Lattner2dc77232006-10-17 18:06:55 +0000312 if (FBB == 0) {
Chris Lattner54108062006-10-21 05:36:13 +0000313 if (Cond.empty()) // Unconditional branch
Dale Johannesen536a2f12009-02-13 02:27:39 +0000314 BuildMI(&MBB, dl, get(PPC::B)).addMBB(TBB);
Chris Lattner54108062006-10-21 05:36:13 +0000315 else // Conditional branch
Dale Johannesen536a2f12009-02-13 02:27:39 +0000316 BuildMI(&MBB, dl, get(PPC::BCC))
Chris Lattner18258c62006-11-17 22:37:34 +0000317 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
Evan Chengb5cdaa22007-05-18 00:05:48 +0000318 return 1;
Chris Lattner2dc77232006-10-17 18:06:55 +0000319 }
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000320
Chris Lattner879d09c2006-10-21 05:42:09 +0000321 // Two-way Conditional Branch.
Dale Johannesen536a2f12009-02-13 02:27:39 +0000322 BuildMI(&MBB, dl, get(PPC::BCC))
Chris Lattner18258c62006-11-17 22:37:34 +0000323 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
Dale Johannesen536a2f12009-02-13 02:27:39 +0000324 BuildMI(&MBB, dl, get(PPC::B)).addMBB(FBB);
Evan Chengb5cdaa22007-05-18 00:05:48 +0000325 return 2;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000326}
327
Owen Anderson940f83e2008-08-26 18:03:31 +0000328bool PPCInstrInfo::copyRegToReg(MachineBasicBlock &MBB,
Owen Andersond10fd972007-12-31 06:32:00 +0000329 MachineBasicBlock::iterator MI,
330 unsigned DestReg, unsigned SrcReg,
331 const TargetRegisterClass *DestRC,
332 const TargetRegisterClass *SrcRC) const {
333 if (DestRC != SrcRC) {
Owen Anderson940f83e2008-08-26 18:03:31 +0000334 // Not yet supported!
335 return false;
Owen Andersond10fd972007-12-31 06:32:00 +0000336 }
337
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000338 DebugLoc DL = DebugLoc::getUnknownLoc();
339 if (MI != MBB.end()) DL = MI->getDebugLoc();
340
Owen Andersond10fd972007-12-31 06:32:00 +0000341 if (DestRC == PPC::GPRCRegisterClass) {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000342 BuildMI(MBB, MI, DL, get(PPC::OR), DestReg).addReg(SrcReg).addReg(SrcReg);
Owen Andersond10fd972007-12-31 06:32:00 +0000343 } else if (DestRC == PPC::G8RCRegisterClass) {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000344 BuildMI(MBB, MI, DL, get(PPC::OR8), DestReg).addReg(SrcReg).addReg(SrcReg);
Owen Andersond10fd972007-12-31 06:32:00 +0000345 } else if (DestRC == PPC::F4RCRegisterClass) {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000346 BuildMI(MBB, MI, DL, get(PPC::FMRS), DestReg).addReg(SrcReg);
Owen Andersond10fd972007-12-31 06:32:00 +0000347 } else if (DestRC == PPC::F8RCRegisterClass) {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000348 BuildMI(MBB, MI, DL, get(PPC::FMRD), DestReg).addReg(SrcReg);
Owen Andersond10fd972007-12-31 06:32:00 +0000349 } else if (DestRC == PPC::CRRCRegisterClass) {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000350 BuildMI(MBB, MI, DL, get(PPC::MCRF), DestReg).addReg(SrcReg);
Owen Andersond10fd972007-12-31 06:32:00 +0000351 } else if (DestRC == PPC::VRRCRegisterClass) {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000352 BuildMI(MBB, MI, DL, get(PPC::VOR), DestReg).addReg(SrcReg).addReg(SrcReg);
Nicolas Geoffray0404cd92008-03-10 14:12:10 +0000353 } else if (DestRC == PPC::CRBITRCRegisterClass) {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000354 BuildMI(MBB, MI, DL, get(PPC::CROR), DestReg).addReg(SrcReg).addReg(SrcReg);
Owen Andersond10fd972007-12-31 06:32:00 +0000355 } else {
Owen Anderson940f83e2008-08-26 18:03:31 +0000356 // Attempt to copy register that is not GPR or FPR
357 return false;
Owen Andersond10fd972007-12-31 06:32:00 +0000358 }
Owen Anderson940f83e2008-08-26 18:03:31 +0000359
360 return true;
Owen Andersond10fd972007-12-31 06:32:00 +0000361}
362
Bill Wendling4a66e9a2008-03-10 22:49:16 +0000363bool
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000364PPCInstrInfo::StoreRegToStackSlot(MachineFunction &MF,
365 unsigned SrcReg, bool isKill,
Bill Wendling4a66e9a2008-03-10 22:49:16 +0000366 int FrameIdx,
367 const TargetRegisterClass *RC,
368 SmallVectorImpl<MachineInstr*> &NewMIs) const{
Dale Johannesen21b55412009-02-12 23:08:38 +0000369 DebugLoc DL = DebugLoc::getUnknownLoc();
Owen Andersonf6372aa2008-01-01 21:11:32 +0000370 if (RC == PPC::GPRCRegisterClass) {
371 if (SrcReg != PPC::LR) {
Dale Johannesen21b55412009-02-12 23:08:38 +0000372 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
Bill Wendling587daed2009-05-13 21:33:08 +0000373 .addReg(SrcReg,
374 getKillRegState(isKill)),
Bill Wendling4a66e9a2008-03-10 22:49:16 +0000375 FrameIdx));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000376 } else {
377 // FIXME: this spills LR immediately to memory in one step. To do this,
378 // we use R11, which we know cannot be used in the prolog/epilog. This is
379 // a hack.
Dale Johannesen21b55412009-02-12 23:08:38 +0000380 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MFLR), PPC::R11));
381 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
Bill Wendling587daed2009-05-13 21:33:08 +0000382 .addReg(PPC::R11,
383 getKillRegState(isKill)),
Bill Wendling4a66e9a2008-03-10 22:49:16 +0000384 FrameIdx));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000385 }
386 } else if (RC == PPC::G8RCRegisterClass) {
387 if (SrcReg != PPC::LR8) {
Dale Johannesen21b55412009-02-12 23:08:38 +0000388 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STD))
Bill Wendling587daed2009-05-13 21:33:08 +0000389 .addReg(SrcReg,
390 getKillRegState(isKill)),
391 FrameIdx));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000392 } else {
393 // FIXME: this spills LR immediately to memory in one step. To do this,
394 // we use R11, which we know cannot be used in the prolog/epilog. This is
395 // a hack.
Dale Johannesen21b55412009-02-12 23:08:38 +0000396 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MFLR8), PPC::X11));
397 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STD))
Bill Wendling587daed2009-05-13 21:33:08 +0000398 .addReg(PPC::X11,
399 getKillRegState(isKill)),
400 FrameIdx));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000401 }
402 } else if (RC == PPC::F8RCRegisterClass) {
Dale Johannesen21b55412009-02-12 23:08:38 +0000403 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFD))
Bill Wendling587daed2009-05-13 21:33:08 +0000404 .addReg(SrcReg,
405 getKillRegState(isKill)),
406 FrameIdx));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000407 } else if (RC == PPC::F4RCRegisterClass) {
Dale Johannesen21b55412009-02-12 23:08:38 +0000408 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFS))
Bill Wendling587daed2009-05-13 21:33:08 +0000409 .addReg(SrcReg,
410 getKillRegState(isKill)),
411 FrameIdx));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000412 } else if (RC == PPC::CRRCRegisterClass) {
Bill Wendling4a66e9a2008-03-10 22:49:16 +0000413 if ((EnablePPC32RS && !TM.getSubtargetImpl()->isPPC64()) ||
414 (EnablePPC64RS && TM.getSubtargetImpl()->isPPC64())) {
415 // FIXME (64-bit): Enable
Dale Johannesen21b55412009-02-12 23:08:38 +0000416 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CR))
Bill Wendling587daed2009-05-13 21:33:08 +0000417 .addReg(SrcReg,
418 getKillRegState(isKill)),
Chris Lattner71a2cb22008-03-20 01:22:40 +0000419 FrameIdx));
Bill Wendling7194aaf2008-03-03 22:19:16 +0000420 return true;
421 } else {
422 // FIXME: We use R0 here, because it isn't available for RA. We need to
423 // store the CR in the low 4-bits of the saved value. First, issue a MFCR
424 // to save all of the CRBits.
Dale Johannesen21b55412009-02-12 23:08:38 +0000425 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MFCR), PPC::R0));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000426
Bill Wendling7194aaf2008-03-03 22:19:16 +0000427 // If the saved register wasn't CR0, shift the bits left so that they are
428 // in CR0's slot.
429 if (SrcReg != PPC::CR0) {
430 unsigned ShiftBits = PPCRegisterInfo::getRegisterNumbering(SrcReg)*4;
431 // rlwinm r0, r0, ShiftBits, 0, 31.
Dale Johannesen21b55412009-02-12 23:08:38 +0000432 NewMIs.push_back(BuildMI(MF, DL, get(PPC::RLWINM), PPC::R0)
Chris Lattnercb341de2008-03-10 18:55:53 +0000433 .addReg(PPC::R0).addImm(ShiftBits).addImm(0).addImm(31));
Bill Wendling7194aaf2008-03-03 22:19:16 +0000434 }
435
Dale Johannesen21b55412009-02-12 23:08:38 +0000436 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
Bill Wendling587daed2009-05-13 21:33:08 +0000437 .addReg(PPC::R0,
438 getKillRegState(isKill)),
Bill Wendling7194aaf2008-03-03 22:19:16 +0000439 FrameIdx));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000440 }
Nicolas Geoffray0404cd92008-03-10 14:12:10 +0000441 } else if (RC == PPC::CRBITRCRegisterClass) {
442 // FIXME: We use CRi here because there is no mtcrf on a bit. Since the
443 // backend currently only uses CR1EQ as an individual bit, this should
444 // not cause any bug. If we need other uses of CR bits, the following
445 // code may be invalid.
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000446 unsigned Reg = 0;
Nicolas Geoffray0404cd92008-03-10 14:12:10 +0000447 if (SrcReg >= PPC::CR0LT || SrcReg <= PPC::CR0UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000448 Reg = PPC::CR0;
Nicolas Geoffray0404cd92008-03-10 14:12:10 +0000449 else if (SrcReg >= PPC::CR1LT || SrcReg <= PPC::CR1UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000450 Reg = PPC::CR1;
451 else if (SrcReg >= PPC::CR2LT || SrcReg <= PPC::CR2UN)
452 Reg = PPC::CR2;
453 else if (SrcReg >= PPC::CR3LT || SrcReg <= PPC::CR3UN)
454 Reg = PPC::CR3;
455 else if (SrcReg >= PPC::CR4LT || SrcReg <= PPC::CR4UN)
456 Reg = PPC::CR4;
457 else if (SrcReg >= PPC::CR5LT || SrcReg <= PPC::CR5UN)
458 Reg = PPC::CR5;
459 else if (SrcReg >= PPC::CR6LT || SrcReg <= PPC::CR6UN)
460 Reg = PPC::CR6;
461 else if (SrcReg >= PPC::CR7LT || SrcReg <= PPC::CR7UN)
462 Reg = PPC::CR7;
463
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000464 return StoreRegToStackSlot(MF, Reg, isKill, FrameIdx,
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000465 PPC::CRRCRegisterClass, NewMIs);
466
Owen Andersonf6372aa2008-01-01 21:11:32 +0000467 } else if (RC == PPC::VRRCRegisterClass) {
468 // We don't have indexed addressing for vector loads. Emit:
469 // R0 = ADDI FI#
470 // STVX VAL, 0, R0
471 //
472 // FIXME: We use R0 here, because it isn't available for RA.
Dale Johannesen21b55412009-02-12 23:08:38 +0000473 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::ADDI), PPC::R0),
Owen Andersonf6372aa2008-01-01 21:11:32 +0000474 FrameIdx, 0, 0));
Dale Johannesen21b55412009-02-12 23:08:38 +0000475 NewMIs.push_back(BuildMI(MF, DL, get(PPC::STVX))
Bill Wendling587daed2009-05-13 21:33:08 +0000476 .addReg(SrcReg, getKillRegState(isKill))
477 .addReg(PPC::R0)
478 .addReg(PPC::R0));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000479 } else {
480 assert(0 && "Unknown regclass!");
481 abort();
482 }
Bill Wendling7194aaf2008-03-03 22:19:16 +0000483
484 return false;
Owen Andersonf6372aa2008-01-01 21:11:32 +0000485}
486
487void
488PPCInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
Bill Wendling7194aaf2008-03-03 22:19:16 +0000489 MachineBasicBlock::iterator MI,
490 unsigned SrcReg, bool isKill, int FrameIdx,
491 const TargetRegisterClass *RC) const {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000492 MachineFunction &MF = *MBB.getParent();
Owen Andersonf6372aa2008-01-01 21:11:32 +0000493 SmallVector<MachineInstr*, 4> NewMIs;
Bill Wendling7194aaf2008-03-03 22:19:16 +0000494
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000495 if (StoreRegToStackSlot(MF, SrcReg, isKill, FrameIdx, RC, NewMIs)) {
496 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Bill Wendling7194aaf2008-03-03 22:19:16 +0000497 FuncInfo->setSpillsCR();
498 }
499
Owen Andersonf6372aa2008-01-01 21:11:32 +0000500 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
501 MBB.insert(MI, NewMIs[i]);
502}
503
504void PPCInstrInfo::storeRegToAddr(MachineFunction &MF, unsigned SrcReg,
Bill Wendling7194aaf2008-03-03 22:19:16 +0000505 bool isKill,
506 SmallVectorImpl<MachineOperand> &Addr,
507 const TargetRegisterClass *RC,
508 SmallVectorImpl<MachineInstr*> &NewMIs) const{
Dan Gohmand735b802008-10-03 15:45:36 +0000509 if (Addr[0].isFI()) {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000510 if (StoreRegToStackSlot(MF, SrcReg, isKill,
511 Addr[0].getIndex(), RC, NewMIs)) {
Bill Wendling7194aaf2008-03-03 22:19:16 +0000512 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
513 FuncInfo->setSpillsCR();
514 }
515
Owen Andersonf6372aa2008-01-01 21:11:32 +0000516 return;
517 }
518
Dale Johannesen21b55412009-02-12 23:08:38 +0000519 DebugLoc DL = DebugLoc::getUnknownLoc();
Owen Andersonf6372aa2008-01-01 21:11:32 +0000520 unsigned Opc = 0;
521 if (RC == PPC::GPRCRegisterClass) {
522 Opc = PPC::STW;
523 } else if (RC == PPC::G8RCRegisterClass) {
524 Opc = PPC::STD;
525 } else if (RC == PPC::F8RCRegisterClass) {
526 Opc = PPC::STFD;
527 } else if (RC == PPC::F4RCRegisterClass) {
528 Opc = PPC::STFS;
529 } else if (RC == PPC::VRRCRegisterClass) {
530 Opc = PPC::STVX;
531 } else {
532 assert(0 && "Unknown regclass!");
533 abort();
534 }
Dale Johannesen21b55412009-02-12 23:08:38 +0000535 MachineInstrBuilder MIB = BuildMI(MF, DL, get(Opc))
Bill Wendling587daed2009-05-13 21:33:08 +0000536 .addReg(SrcReg, getKillRegState(isKill));
Dan Gohman97357612009-02-18 05:45:50 +0000537 for (unsigned i = 0, e = Addr.size(); i != e; ++i)
538 MIB.addOperand(Addr[i]);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000539 NewMIs.push_back(MIB);
540 return;
541}
542
Bill Wendling4a66e9a2008-03-10 22:49:16 +0000543void
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000544PPCInstrInfo::LoadRegFromStackSlot(MachineFunction &MF, DebugLoc DL,
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000545 unsigned DestReg, int FrameIdx,
Bill Wendling4a66e9a2008-03-10 22:49:16 +0000546 const TargetRegisterClass *RC,
547 SmallVectorImpl<MachineInstr*> &NewMIs)const{
Owen Andersonf6372aa2008-01-01 21:11:32 +0000548 if (RC == PPC::GPRCRegisterClass) {
549 if (DestReg != PPC::LR) {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000550 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ),
551 DestReg), FrameIdx));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000552 } else {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000553 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ),
554 PPC::R11), FrameIdx));
555 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MTLR)).addReg(PPC::R11));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000556 }
557 } else if (RC == PPC::G8RCRegisterClass) {
558 if (DestReg != PPC::LR8) {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000559 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LD), DestReg),
Owen Andersonf6372aa2008-01-01 21:11:32 +0000560 FrameIdx));
561 } else {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000562 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LD),
563 PPC::R11), FrameIdx));
564 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MTLR8)).addReg(PPC::R11));
Owen Andersonf6372aa2008-01-01 21:11:32 +0000565 }
566 } else if (RC == PPC::F8RCRegisterClass) {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000567 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFD), DestReg),
Owen Andersonf6372aa2008-01-01 21:11:32 +0000568 FrameIdx));
569 } else if (RC == PPC::F4RCRegisterClass) {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000570 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFS), DestReg),
Owen Andersonf6372aa2008-01-01 21:11:32 +0000571 FrameIdx));
572 } else if (RC == PPC::CRRCRegisterClass) {
573 // FIXME: We use R0 here, because it isn't available for RA.
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000574 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ), PPC::R0),
Owen Andersonf6372aa2008-01-01 21:11:32 +0000575 FrameIdx));
576
577 // If the reloaded register isn't CR0, shift the bits right so that they are
578 // in the right CR's slot.
579 if (DestReg != PPC::CR0) {
580 unsigned ShiftBits = PPCRegisterInfo::getRegisterNumbering(DestReg)*4;
581 // rlwinm r11, r11, 32-ShiftBits, 0, 31.
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000582 NewMIs.push_back(BuildMI(MF, DL, get(PPC::RLWINM), PPC::R0)
Owen Andersonf6372aa2008-01-01 21:11:32 +0000583 .addReg(PPC::R0).addImm(32-ShiftBits).addImm(0).addImm(31));
584 }
585
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000586 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MTCRF), DestReg).addReg(PPC::R0));
Nicolas Geoffray0404cd92008-03-10 14:12:10 +0000587 } else if (RC == PPC::CRBITRCRegisterClass) {
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000588
589 unsigned Reg = 0;
Nicolas Geoffray0404cd92008-03-10 14:12:10 +0000590 if (DestReg >= PPC::CR0LT || DestReg <= PPC::CR0UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000591 Reg = PPC::CR0;
Nicolas Geoffray0404cd92008-03-10 14:12:10 +0000592 else if (DestReg >= PPC::CR1LT || DestReg <= PPC::CR1UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000593 Reg = PPC::CR1;
594 else if (DestReg >= PPC::CR2LT || DestReg <= PPC::CR2UN)
595 Reg = PPC::CR2;
596 else if (DestReg >= PPC::CR3LT || DestReg <= PPC::CR3UN)
597 Reg = PPC::CR3;
598 else if (DestReg >= PPC::CR4LT || DestReg <= PPC::CR4UN)
599 Reg = PPC::CR4;
600 else if (DestReg >= PPC::CR5LT || DestReg <= PPC::CR5UN)
601 Reg = PPC::CR5;
602 else if (DestReg >= PPC::CR6LT || DestReg <= PPC::CR6UN)
603 Reg = PPC::CR6;
604 else if (DestReg >= PPC::CR7LT || DestReg <= PPC::CR7UN)
605 Reg = PPC::CR7;
606
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000607 return LoadRegFromStackSlot(MF, DL, Reg, FrameIdx,
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000608 PPC::CRRCRegisterClass, NewMIs);
609
Owen Andersonf6372aa2008-01-01 21:11:32 +0000610 } else if (RC == PPC::VRRCRegisterClass) {
611 // We don't have indexed addressing for vector loads. Emit:
612 // R0 = ADDI FI#
613 // Dest = LVX 0, R0
614 //
615 // FIXME: We use R0 here, because it isn't available for RA.
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000616 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::ADDI), PPC::R0),
Owen Andersonf6372aa2008-01-01 21:11:32 +0000617 FrameIdx, 0, 0));
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000618 NewMIs.push_back(BuildMI(MF, DL, get(PPC::LVX),DestReg).addReg(PPC::R0)
Owen Andersonf6372aa2008-01-01 21:11:32 +0000619 .addReg(PPC::R0));
620 } else {
621 assert(0 && "Unknown regclass!");
622 abort();
623 }
624}
625
626void
627PPCInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
Bill Wendling7194aaf2008-03-03 22:19:16 +0000628 MachineBasicBlock::iterator MI,
629 unsigned DestReg, int FrameIdx,
630 const TargetRegisterClass *RC) const {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000631 MachineFunction &MF = *MBB.getParent();
Owen Andersonf6372aa2008-01-01 21:11:32 +0000632 SmallVector<MachineInstr*, 4> NewMIs;
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000633 DebugLoc DL = DebugLoc::getUnknownLoc();
634 if (MI != MBB.end()) DL = MI->getDebugLoc();
635 LoadRegFromStackSlot(MF, DL, DestReg, FrameIdx, RC, NewMIs);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000636 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
637 MBB.insert(MI, NewMIs[i]);
638}
639
640void PPCInstrInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
Bill Wendling7194aaf2008-03-03 22:19:16 +0000641 SmallVectorImpl<MachineOperand> &Addr,
642 const TargetRegisterClass *RC,
643 SmallVectorImpl<MachineInstr*> &NewMIs)const{
Dan Gohmand735b802008-10-03 15:45:36 +0000644 if (Addr[0].isFI()) {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000645 LoadRegFromStackSlot(MF, DebugLoc::getUnknownLoc(),
646 DestReg, Addr[0].getIndex(), RC, NewMIs);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000647 return;
648 }
649
650 unsigned Opc = 0;
651 if (RC == PPC::GPRCRegisterClass) {
652 assert(DestReg != PPC::LR && "Can't handle this yet!");
653 Opc = PPC::LWZ;
654 } else if (RC == PPC::G8RCRegisterClass) {
655 assert(DestReg != PPC::LR8 && "Can't handle this yet!");
656 Opc = PPC::LD;
657 } else if (RC == PPC::F8RCRegisterClass) {
658 Opc = PPC::LFD;
659 } else if (RC == PPC::F4RCRegisterClass) {
660 Opc = PPC::LFS;
661 } else if (RC == PPC::VRRCRegisterClass) {
662 Opc = PPC::LVX;
663 } else {
664 assert(0 && "Unknown regclass!");
665 abort();
666 }
Dale Johannesen21b55412009-02-12 23:08:38 +0000667 DebugLoc DL = DebugLoc::getUnknownLoc();
668 MachineInstrBuilder MIB = BuildMI(MF, DL, get(Opc), DestReg);
Dan Gohman97357612009-02-18 05:45:50 +0000669 for (unsigned i = 0, e = Addr.size(); i != e; ++i)
670 MIB.addOperand(Addr[i]);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000671 NewMIs.push_back(MIB);
672 return;
673}
674
Owen Anderson43dbe052008-01-07 01:35:02 +0000675/// foldMemoryOperand - PowerPC (like most RISC's) can only fold spills into
676/// copy instructions, turning them into load/store instructions.
Dan Gohmanc54baa22008-12-03 18:43:12 +0000677MachineInstr *PPCInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
678 MachineInstr *MI,
679 const SmallVectorImpl<unsigned> &Ops,
680 int FrameIndex) const {
Owen Anderson43dbe052008-01-07 01:35:02 +0000681 if (Ops.size() != 1) return NULL;
682
683 // Make sure this is a reg-reg copy. Note that we can't handle MCRF, because
684 // it takes more than one instruction to store it.
685 unsigned Opc = MI->getOpcode();
686 unsigned OpNum = Ops[0];
687
688 MachineInstr *NewMI = NULL;
689 if ((Opc == PPC::OR &&
690 MI->getOperand(1).getReg() == MI->getOperand(2).getReg())) {
691 if (OpNum == 0) { // move -> store
692 unsigned InReg = MI->getOperand(1).getReg();
Evan Cheng9f1c8312008-07-03 09:09:37 +0000693 bool isKill = MI->getOperand(1).isKill();
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000694 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::STW))
Bill Wendling587daed2009-05-13 21:33:08 +0000695 .addReg(InReg, getKillRegState(isKill)),
Owen Anderson43dbe052008-01-07 01:35:02 +0000696 FrameIndex);
697 } else { // move -> load
698 unsigned OutReg = MI->getOperand(0).getReg();
Evan Cheng9f1c8312008-07-03 09:09:37 +0000699 bool isDead = MI->getOperand(0).isDead();
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000700 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::LWZ))
Bill Wendling587daed2009-05-13 21:33:08 +0000701 .addReg(OutReg,
702 RegState::Define |
703 getDeadRegState(isDead)),
Owen Anderson43dbe052008-01-07 01:35:02 +0000704 FrameIndex);
705 }
706 } else if ((Opc == PPC::OR8 &&
707 MI->getOperand(1).getReg() == MI->getOperand(2).getReg())) {
708 if (OpNum == 0) { // move -> store
709 unsigned InReg = MI->getOperand(1).getReg();
Evan Cheng9f1c8312008-07-03 09:09:37 +0000710 bool isKill = MI->getOperand(1).isKill();
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000711 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::STD))
Bill Wendling587daed2009-05-13 21:33:08 +0000712 .addReg(InReg, getKillRegState(isKill)),
Owen Anderson43dbe052008-01-07 01:35:02 +0000713 FrameIndex);
714 } else { // move -> load
715 unsigned OutReg = MI->getOperand(0).getReg();
Evan Cheng9f1c8312008-07-03 09:09:37 +0000716 bool isDead = MI->getOperand(0).isDead();
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000717 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::LD))
Bill Wendling587daed2009-05-13 21:33:08 +0000718 .addReg(OutReg,
719 RegState::Define |
720 getDeadRegState(isDead)),
Evan Cheng9f1c8312008-07-03 09:09:37 +0000721 FrameIndex);
Owen Anderson43dbe052008-01-07 01:35:02 +0000722 }
723 } else if (Opc == PPC::FMRD) {
724 if (OpNum == 0) { // move -> store
725 unsigned InReg = MI->getOperand(1).getReg();
Evan Cheng9f1c8312008-07-03 09:09:37 +0000726 bool isKill = MI->getOperand(1).isKill();
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000727 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::STFD))
Bill Wendling587daed2009-05-13 21:33:08 +0000728 .addReg(InReg, getKillRegState(isKill)),
Owen Anderson43dbe052008-01-07 01:35:02 +0000729 FrameIndex);
730 } else { // move -> load
731 unsigned OutReg = MI->getOperand(0).getReg();
Evan Cheng9f1c8312008-07-03 09:09:37 +0000732 bool isDead = MI->getOperand(0).isDead();
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000733 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::LFD))
Bill Wendling587daed2009-05-13 21:33:08 +0000734 .addReg(OutReg,
735 RegState::Define |
736 getDeadRegState(isDead)),
Evan Cheng9f1c8312008-07-03 09:09:37 +0000737 FrameIndex);
Owen Anderson43dbe052008-01-07 01:35:02 +0000738 }
739 } else if (Opc == PPC::FMRS) {
740 if (OpNum == 0) { // move -> store
741 unsigned InReg = MI->getOperand(1).getReg();
Evan Cheng9f1c8312008-07-03 09:09:37 +0000742 bool isKill = MI->getOperand(1).isKill();
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000743 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::STFS))
Bill Wendling587daed2009-05-13 21:33:08 +0000744 .addReg(InReg, getKillRegState(isKill)),
Owen Anderson43dbe052008-01-07 01:35:02 +0000745 FrameIndex);
746 } else { // move -> load
747 unsigned OutReg = MI->getOperand(0).getReg();
Evan Cheng9f1c8312008-07-03 09:09:37 +0000748 bool isDead = MI->getOperand(0).isDead();
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000749 NewMI = addFrameReference(BuildMI(MF, MI->getDebugLoc(), get(PPC::LFS))
Bill Wendling587daed2009-05-13 21:33:08 +0000750 .addReg(OutReg,
751 RegState::Define |
752 getDeadRegState(isDead)),
Evan Cheng9f1c8312008-07-03 09:09:37 +0000753 FrameIndex);
Owen Anderson43dbe052008-01-07 01:35:02 +0000754 }
755 }
756
Owen Anderson43dbe052008-01-07 01:35:02 +0000757 return NewMI;
758}
759
Dan Gohman8e8b8a22008-10-16 01:49:15 +0000760bool PPCInstrInfo::canFoldMemoryOperand(const MachineInstr *MI,
761 const SmallVectorImpl<unsigned> &Ops) const {
Owen Anderson43dbe052008-01-07 01:35:02 +0000762 if (Ops.size() != 1) return false;
763
764 // Make sure this is a reg-reg copy. Note that we can't handle MCRF, because
765 // it takes more than one instruction to store it.
766 unsigned Opc = MI->getOpcode();
767
768 if ((Opc == PPC::OR &&
769 MI->getOperand(1).getReg() == MI->getOperand(2).getReg()))
770 return true;
771 else if ((Opc == PPC::OR8 &&
772 MI->getOperand(1).getReg() == MI->getOperand(2).getReg()))
773 return true;
774 else if (Opc == PPC::FMRD || Opc == PPC::FMRS)
775 return true;
776
777 return false;
778}
779
Owen Andersonf6372aa2008-01-01 21:11:32 +0000780
Dan Gohman8e8b8a22008-10-16 01:49:15 +0000781bool PPCInstrInfo::BlockHasNoFallThrough(const MachineBasicBlock &MBB) const {
Chris Lattneref139822006-10-28 17:35:02 +0000782 if (MBB.empty()) return false;
783
784 switch (MBB.back().getOpcode()) {
Evan Cheng126f17a2007-05-21 18:44:17 +0000785 case PPC::BLR: // Return.
Chris Lattneref139822006-10-28 17:35:02 +0000786 case PPC::B: // Uncond branch.
787 case PPC::BCTR: // Indirect branch.
788 return true;
789 default: return false;
790 }
791}
792
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000793bool PPCInstrInfo::
Owen Anderson44eb65c2008-08-14 22:49:33 +0000794ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
Chris Lattner7c4fe252006-10-21 06:03:11 +0000795 assert(Cond.size() == 2 && "Invalid PPC branch opcode!");
796 // Leave the CR# the same, but invert the condition.
Chris Lattner18258c62006-11-17 22:37:34 +0000797 Cond[0].setImm(PPC::InvertPredicate((PPC::Predicate)Cond[0].getImm()));
Chris Lattner7c4fe252006-10-21 06:03:11 +0000798 return false;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000799}
Nicolas Geoffray52e724a2008-04-16 20:10:13 +0000800
801/// GetInstSize - Return the number of bytes of code the specified
802/// instruction may be. This returns the maximum number of bytes.
803///
804unsigned PPCInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
805 switch (MI->getOpcode()) {
806 case PPC::INLINEASM: { // Inline Asm: Variable size.
807 const MachineFunction *MF = MI->getParent()->getParent();
808 const char *AsmStr = MI->getOperand(0).getSymbolName();
809 return MF->getTarget().getTargetAsmInfo()->getInlineAsmLength(AsmStr);
810 }
Dan Gohman44066042008-07-01 00:05:16 +0000811 case PPC::DBG_LABEL:
812 case PPC::EH_LABEL:
813 case PPC::GC_LABEL:
Nicolas Geoffray52e724a2008-04-16 20:10:13 +0000814 return 0;
Nicolas Geoffray52e724a2008-04-16 20:10:13 +0000815 default:
816 return 4; // PowerPC instructions are all 4 bytes
817 }
818}