blob: c0b9e92c2d0a83c3dd8f9bd589a26a0106f2b545 [file] [log] [blame]
Jia Liub22310f2012-02-18 12:03:15 +00001//===-- PPCInstrInfo.cpp - PowerPC Instruction Information ----------------===//
Misha Brukmanb4402432005-04-21 23:30:14 +00002//
Misha Brukman116f9272004-08-17 04:55:41 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Brukmanb4402432005-04-21 23:30:14 +00007//
Misha Brukman116f9272004-08-17 04:55:41 +00008//===----------------------------------------------------------------------===//
9//
10// This file contains the PowerPC implementation of the TargetInstrInfo class.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner6f3b9542005-10-14 23:59:06 +000014#include "PPCInstrInfo.h"
Evan Chengc5e6d2f2011-07-11 03:57:24 +000015#include "PPC.h"
Owen Andersoneee14602008-01-01 21:11:32 +000016#include "PPCInstrBuilder.h"
Bill Wendling632ea652008-03-03 22:19:16 +000017#include "PPCMachineFunctionInfo.h"
Chris Lattner49cadab2006-06-17 00:01:04 +000018#include "PPCTargetMachine.h"
Andrew Trick10ffc2b2010-12-24 05:03:26 +000019#include "PPCHazardRecognizers.h"
Evan Cheng11424442011-07-26 00:24:13 +000020#include "MCTargetDesc/PPCPredicates.h"
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +000021#include "llvm/CodeGen/MachineFrameInfo.h"
Misha Brukman116f9272004-08-17 04:55:41 +000022#include "llvm/CodeGen/MachineInstrBuilder.h"
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +000023#include "llvm/CodeGen/MachineMemOperand.h"
Jakob Stoklund Olesenddbf7a82010-02-26 21:09:24 +000024#include "llvm/CodeGen/MachineRegisterInfo.h"
Hal Finkel9f9f8922012-04-01 19:22:40 +000025#include "llvm/CodeGen/PseudoSourceValue.h"
Evan Chengc5e6d2f2011-07-11 03:57:24 +000026#include "llvm/MC/MCAsmInfo.h"
Bill Wendling1af20ad2008-03-04 23:13:51 +000027#include "llvm/Support/CommandLine.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000028#include "llvm/Support/ErrorHandling.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000029#include "llvm/Support/TargetRegistry.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000030#include "llvm/Support/raw_ostream.h"
Evan Chengc5e6d2f2011-07-11 03:57:24 +000031#include "llvm/ADT/STLExtras.h"
Misha Brukman116f9272004-08-17 04:55:41 +000032
Evan Cheng703a0fb2011-07-01 17:57:27 +000033#define GET_INSTRINFO_CTOR
Evan Cheng1e210d02011-06-28 20:07:07 +000034#include "PPCGenInstrInfo.inc"
35
Dan Gohman20857192010-04-15 17:20:57 +000036namespace llvm {
Hal Finkel8f6834d2011-12-05 17:55:17 +000037extern cl::opt<bool> DisablePPC32RS;
38extern cl::opt<bool> DisablePPC64RS;
Dan Gohman20857192010-04-15 17:20:57 +000039}
40
41using namespace llvm;
Bill Wendling1af20ad2008-03-04 23:13:51 +000042
Chris Lattner49cadab2006-06-17 00:01:04 +000043PPCInstrInfo::PPCInstrInfo(PPCTargetMachine &tm)
Evan Cheng703a0fb2011-07-01 17:57:27 +000044 : PPCGenInstrInfo(PPC::ADJCALLSTACKDOWN, PPC::ADJCALLSTACKUP),
Evan Cheng194c3dc2011-06-28 21:14:33 +000045 TM(tm), RI(*TM.getSubtargetImpl(), *this) {}
Chris Lattner49cadab2006-06-17 00:01:04 +000046
Andrew Trick10ffc2b2010-12-24 05:03:26 +000047/// CreateTargetHazardRecognizer - Return the hazard recognizer to use for
48/// this target when scheduling the DAG.
49ScheduleHazardRecognizer *PPCInstrInfo::CreateTargetHazardRecognizer(
50 const TargetMachine *TM,
51 const ScheduleDAG *DAG) const {
Hal Finkel6fa56972011-10-17 04:03:49 +000052 unsigned Directive = TM->getSubtarget<PPCSubtarget>().getDarwinDirective();
Hal Finkel9f9f8922012-04-01 19:22:40 +000053 if (Directive == PPC::DIR_440 || Directive == PPC::DIR_A2) {
Hal Finkel6f0ae782011-11-22 16:21:04 +000054 const InstrItineraryData *II = TM->getInstrItineraryData();
Hal Finkel51861b42012-03-31 14:45:15 +000055 return new PPCScoreboardHazardRecognizer(II, DAG);
Hal Finkel6fa56972011-10-17 04:03:49 +000056 }
Hal Finkel58ca3602011-12-02 04:58:02 +000057
58 return TargetInstrInfoImpl::CreateTargetHazardRecognizer(TM, DAG);
Andrew Trick10ffc2b2010-12-24 05:03:26 +000059}
60
Hal Finkel58ca3602011-12-02 04:58:02 +000061/// CreateTargetPostRAHazardRecognizer - Return the postRA hazard recognizer
62/// to use for this target when scheduling the DAG.
63ScheduleHazardRecognizer *PPCInstrInfo::CreateTargetPostRAHazardRecognizer(
64 const InstrItineraryData *II,
65 const ScheduleDAG *DAG) const {
66 unsigned Directive = TM.getSubtarget<PPCSubtarget>().getDarwinDirective();
67
68 // Most subtargets use a PPC970 recognizer.
Hal Finkel9f9f8922012-04-01 19:22:40 +000069 if (Directive != PPC::DIR_440 && Directive != PPC::DIR_A2) {
Hal Finkel58ca3602011-12-02 04:58:02 +000070 const TargetInstrInfo *TII = TM.getInstrInfo();
71 assert(TII && "No InstrInfo?");
72
73 return new PPCHazardRecognizer970(*TII);
74 }
75
Hal Finkel9f9f8922012-04-01 19:22:40 +000076 return new PPCScoreboardHazardRecognizer(II, DAG);
Hal Finkel58ca3602011-12-02 04:58:02 +000077}
Andrew Trickc416ba62010-12-24 04:28:06 +000078unsigned PPCInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
Chris Lattner91400bd2006-03-16 22:24:02 +000079 int &FrameIndex) const {
Chris Lattnerbb53acd2006-02-02 20:12:32 +000080 switch (MI->getOpcode()) {
81 default: break;
82 case PPC::LD:
83 case PPC::LWZ:
84 case PPC::LFS:
85 case PPC::LFD:
Dan Gohman0d1e9a82008-10-03 15:45:36 +000086 if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
87 MI->getOperand(2).isFI()) {
Chris Lattnera5bb3702007-12-30 23:10:15 +000088 FrameIndex = MI->getOperand(2).getIndex();
Chris Lattnerbb53acd2006-02-02 20:12:32 +000089 return MI->getOperand(0).getReg();
90 }
91 break;
92 }
93 return 0;
Chris Lattnerc327d712006-02-02 20:16:12 +000094}
Chris Lattnerbb53acd2006-02-02 20:12:32 +000095
Andrew Trickc416ba62010-12-24 04:28:06 +000096unsigned PPCInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
Chris Lattnerc327d712006-02-02 20:16:12 +000097 int &FrameIndex) const {
98 switch (MI->getOpcode()) {
99 default: break;
Nate Begeman4efb3282006-02-02 21:07:50 +0000100 case PPC::STD:
Chris Lattnerc327d712006-02-02 20:16:12 +0000101 case PPC::STW:
102 case PPC::STFS:
103 case PPC::STFD:
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000104 if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
105 MI->getOperand(2).isFI()) {
Chris Lattnera5bb3702007-12-30 23:10:15 +0000106 FrameIndex = MI->getOperand(2).getIndex();
Chris Lattnerc327d712006-02-02 20:16:12 +0000107 return MI->getOperand(0).getReg();
108 }
109 break;
110 }
111 return 0;
112}
Chris Lattnerbb53acd2006-02-02 20:12:32 +0000113
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000114// commuteInstruction - We can commute rlwimi instructions, but only if the
115// rotate amt is zero. We also have to munge the immediates a bit.
Evan Cheng03553bb2008-06-16 07:33:11 +0000116MachineInstr *
117PPCInstrInfo::commuteInstruction(MachineInstr *MI, bool NewMI) const {
Dan Gohman3b460302008-07-07 23:14:23 +0000118 MachineFunction &MF = *MI->getParent()->getParent();
119
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000120 // Normal instructions can be commuted the obvious way.
121 if (MI->getOpcode() != PPC::RLWIMI)
Evan Cheng03553bb2008-06-16 07:33:11 +0000122 return TargetInstrInfoImpl::commuteInstruction(MI, NewMI);
Andrew Trickc416ba62010-12-24 04:28:06 +0000123
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000124 // Cannot commute if it has a non-zero rotate count.
Chris Lattner5c463782007-12-30 20:49:49 +0000125 if (MI->getOperand(3).getImm() != 0)
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000126 return 0;
Andrew Trickc416ba62010-12-24 04:28:06 +0000127
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000128 // If we have a zero rotate count, we have:
129 // M = mask(MB,ME)
130 // Op0 = (Op1 & ~M) | (Op2 & M)
131 // Change this to:
132 // M = mask((ME+1)&31, (MB-1)&31)
133 // Op0 = (Op2 & ~M) | (Op1 & M)
134
135 // Swap op1/op2
Evan Cheng244183e2008-02-13 02:46:49 +0000136 unsigned Reg0 = MI->getOperand(0).getReg();
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000137 unsigned Reg1 = MI->getOperand(1).getReg();
138 unsigned Reg2 = MI->getOperand(2).getReg();
Evan Chengdc2c8742006-11-15 20:58:11 +0000139 bool Reg1IsKill = MI->getOperand(1).isKill();
140 bool Reg2IsKill = MI->getOperand(2).isKill();
Evan Cheng03553bb2008-06-16 07:33:11 +0000141 bool ChangeReg0 = false;
Evan Cheng244183e2008-02-13 02:46:49 +0000142 // If machine instrs are no longer in two-address forms, update
143 // destination register as well.
144 if (Reg0 == Reg1) {
145 // Must be two address instruction!
Evan Cheng6cc775f2011-06-28 19:10:37 +0000146 assert(MI->getDesc().getOperandConstraint(0, MCOI::TIED_TO) &&
Evan Cheng244183e2008-02-13 02:46:49 +0000147 "Expecting a two-address instruction!");
Evan Cheng244183e2008-02-13 02:46:49 +0000148 Reg2IsKill = false;
Evan Cheng03553bb2008-06-16 07:33:11 +0000149 ChangeReg0 = true;
Evan Cheng244183e2008-02-13 02:46:49 +0000150 }
Evan Cheng03553bb2008-06-16 07:33:11 +0000151
152 // Masks.
153 unsigned MB = MI->getOperand(4).getImm();
154 unsigned ME = MI->getOperand(5).getImm();
155
156 if (NewMI) {
157 // Create a new instruction.
158 unsigned Reg0 = ChangeReg0 ? Reg2 : MI->getOperand(0).getReg();
159 bool Reg0IsDead = MI->getOperand(0).isDead();
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000160 return BuildMI(MF, MI->getDebugLoc(), MI->getDesc())
Bill Wendlingf7b83c72009-05-13 21:33:08 +0000161 .addReg(Reg0, RegState::Define | getDeadRegState(Reg0IsDead))
162 .addReg(Reg2, getKillRegState(Reg2IsKill))
163 .addReg(Reg1, getKillRegState(Reg1IsKill))
Evan Cheng03553bb2008-06-16 07:33:11 +0000164 .addImm((ME+1) & 31)
165 .addImm((MB-1) & 31);
166 }
167
168 if (ChangeReg0)
169 MI->getOperand(0).setReg(Reg2);
Chris Lattner10d63412006-05-04 17:52:23 +0000170 MI->getOperand(2).setReg(Reg1);
171 MI->getOperand(1).setReg(Reg2);
Chris Lattner60055892007-12-30 21:56:09 +0000172 MI->getOperand(2).setIsKill(Reg1IsKill);
173 MI->getOperand(1).setIsKill(Reg2IsKill);
Andrew Trickc416ba62010-12-24 04:28:06 +0000174
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000175 // Swap the mask around.
Chris Lattner5c463782007-12-30 20:49:49 +0000176 MI->getOperand(4).setImm((ME+1) & 31);
177 MI->getOperand(5).setImm((MB-1) & 31);
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000178 return MI;
179}
Chris Lattnerea79d9fd732006-03-05 23:49:55 +0000180
Andrew Trickc416ba62010-12-24 04:28:06 +0000181void PPCInstrInfo::insertNoop(MachineBasicBlock &MBB,
Chris Lattnerea79d9fd732006-03-05 23:49:55 +0000182 MachineBasicBlock::iterator MI) const {
Chris Lattner6f306d72010-04-02 20:16:16 +0000183 DebugLoc DL;
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000184 BuildMI(MBB, MI, DL, get(PPC::NOP));
Chris Lattnerea79d9fd732006-03-05 23:49:55 +0000185}
Chris Lattnera47294ed2006-10-13 21:21:17 +0000186
187
188// Branch analysis.
189bool PPCInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
190 MachineBasicBlock *&FBB,
Evan Cheng64dfcac2009-02-09 07:14:22 +0000191 SmallVectorImpl<MachineOperand> &Cond,
192 bool AllowModify) const {
Chris Lattnera47294ed2006-10-13 21:21:17 +0000193 // If the block has no terminators, it just falls into the block after it.
194 MachineBasicBlock::iterator I = MBB.end();
Dale Johannesen4244d122010-04-02 01:38:09 +0000195 if (I == MBB.begin())
196 return false;
197 --I;
198 while (I->isDebugValue()) {
199 if (I == MBB.begin())
200 return false;
201 --I;
202 }
203 if (!isUnpredicatedTerminator(I))
Chris Lattnera47294ed2006-10-13 21:21:17 +0000204 return false;
205
206 // Get the last instruction in the block.
207 MachineInstr *LastInst = I;
Andrew Trickc416ba62010-12-24 04:28:06 +0000208
Chris Lattnera47294ed2006-10-13 21:21:17 +0000209 // If there is only one terminator instruction, process it.
Evan Cheng5514bbe2007-06-08 21:59:56 +0000210 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
Chris Lattnera47294ed2006-10-13 21:21:17 +0000211 if (LastInst->getOpcode() == PPC::B) {
Evan Cheng8f43afd2009-05-08 23:09:25 +0000212 if (!LastInst->getOperand(0).isMBB())
213 return true;
Chris Lattnera5bb3702007-12-30 23:10:15 +0000214 TBB = LastInst->getOperand(0).getMBB();
Chris Lattnera47294ed2006-10-13 21:21:17 +0000215 return false;
Chris Lattnere0263792006-11-17 22:14:47 +0000216 } else if (LastInst->getOpcode() == PPC::BCC) {
Evan Cheng8f43afd2009-05-08 23:09:25 +0000217 if (!LastInst->getOperand(2).isMBB())
218 return true;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000219 // Block ends with fall-through condbranch.
Chris Lattnera5bb3702007-12-30 23:10:15 +0000220 TBB = LastInst->getOperand(2).getMBB();
Chris Lattnera47294ed2006-10-13 21:21:17 +0000221 Cond.push_back(LastInst->getOperand(0));
222 Cond.push_back(LastInst->getOperand(1));
Chris Lattner23f22de2006-10-21 06:03:11 +0000223 return false;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000224 }
225 // Otherwise, don't know what this is.
226 return true;
227 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000228
Chris Lattnera47294ed2006-10-13 21:21:17 +0000229 // Get the instruction before it if it's a terminator.
230 MachineInstr *SecondLastInst = I;
231
232 // If there are three terminators, we don't know what sort of block this is.
233 if (SecondLastInst && I != MBB.begin() &&
Evan Cheng5514bbe2007-06-08 21:59:56 +0000234 isUnpredicatedTerminator(--I))
Chris Lattnera47294ed2006-10-13 21:21:17 +0000235 return true;
Andrew Trickc416ba62010-12-24 04:28:06 +0000236
Chris Lattnere0263792006-11-17 22:14:47 +0000237 // If the block ends with PPC::B and PPC:BCC, handle it.
Andrew Trickc416ba62010-12-24 04:28:06 +0000238 if (SecondLastInst->getOpcode() == PPC::BCC &&
Chris Lattnera47294ed2006-10-13 21:21:17 +0000239 LastInst->getOpcode() == PPC::B) {
Evan Cheng8f43afd2009-05-08 23:09:25 +0000240 if (!SecondLastInst->getOperand(2).isMBB() ||
241 !LastInst->getOperand(0).isMBB())
242 return true;
Chris Lattnera5bb3702007-12-30 23:10:15 +0000243 TBB = SecondLastInst->getOperand(2).getMBB();
Chris Lattnera47294ed2006-10-13 21:21:17 +0000244 Cond.push_back(SecondLastInst->getOperand(0));
245 Cond.push_back(SecondLastInst->getOperand(1));
Chris Lattnera5bb3702007-12-30 23:10:15 +0000246 FBB = LastInst->getOperand(0).getMBB();
Chris Lattnera47294ed2006-10-13 21:21:17 +0000247 return false;
248 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000249
Dale Johannesenc6855462007-06-13 17:59:52 +0000250 // If the block ends with two PPC:Bs, handle it. The second one is not
251 // executed, so remove it.
Andrew Trickc416ba62010-12-24 04:28:06 +0000252 if (SecondLastInst->getOpcode() == PPC::B &&
Dale Johannesenc6855462007-06-13 17:59:52 +0000253 LastInst->getOpcode() == PPC::B) {
Evan Cheng8f43afd2009-05-08 23:09:25 +0000254 if (!SecondLastInst->getOperand(0).isMBB())
255 return true;
Chris Lattnera5bb3702007-12-30 23:10:15 +0000256 TBB = SecondLastInst->getOperand(0).getMBB();
Dale Johannesenc6855462007-06-13 17:59:52 +0000257 I = LastInst;
Evan Cheng64dfcac2009-02-09 07:14:22 +0000258 if (AllowModify)
259 I->eraseFromParent();
Dale Johannesenc6855462007-06-13 17:59:52 +0000260 return false;
261 }
262
Chris Lattnera47294ed2006-10-13 21:21:17 +0000263 // Otherwise, can't handle this.
264 return true;
265}
266
Evan Cheng99be49d2007-05-18 00:05:48 +0000267unsigned PPCInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
Chris Lattnera47294ed2006-10-13 21:21:17 +0000268 MachineBasicBlock::iterator I = MBB.end();
Evan Cheng99be49d2007-05-18 00:05:48 +0000269 if (I == MBB.begin()) return 0;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000270 --I;
Dale Johannesen4244d122010-04-02 01:38:09 +0000271 while (I->isDebugValue()) {
272 if (I == MBB.begin())
273 return 0;
274 --I;
275 }
Chris Lattnere0263792006-11-17 22:14:47 +0000276 if (I->getOpcode() != PPC::B && I->getOpcode() != PPC::BCC)
Evan Cheng99be49d2007-05-18 00:05:48 +0000277 return 0;
Andrew Trickc416ba62010-12-24 04:28:06 +0000278
Chris Lattnera47294ed2006-10-13 21:21:17 +0000279 // Remove the branch.
280 I->eraseFromParent();
Andrew Trickc416ba62010-12-24 04:28:06 +0000281
Chris Lattnera47294ed2006-10-13 21:21:17 +0000282 I = MBB.end();
283
Evan Cheng99be49d2007-05-18 00:05:48 +0000284 if (I == MBB.begin()) return 1;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000285 --I;
Chris Lattnere0263792006-11-17 22:14:47 +0000286 if (I->getOpcode() != PPC::BCC)
Evan Cheng99be49d2007-05-18 00:05:48 +0000287 return 1;
Andrew Trickc416ba62010-12-24 04:28:06 +0000288
Chris Lattnera47294ed2006-10-13 21:21:17 +0000289 // Remove the branch.
290 I->eraseFromParent();
Evan Cheng99be49d2007-05-18 00:05:48 +0000291 return 2;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000292}
293
Evan Cheng99be49d2007-05-18 00:05:48 +0000294unsigned
295PPCInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
296 MachineBasicBlock *FBB,
Stuart Hastings0125b642010-06-17 22:43:56 +0000297 const SmallVectorImpl<MachineOperand> &Cond,
298 DebugLoc DL) const {
Chris Lattnera61f0102006-10-17 18:06:55 +0000299 // Shouldn't be a fall through.
300 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Andrew Trickc416ba62010-12-24 04:28:06 +0000301 assert((Cond.size() == 2 || Cond.size() == 0) &&
Chris Lattner94e04442006-10-21 05:36:13 +0000302 "PPC branch conditions have two components!");
Andrew Trickc416ba62010-12-24 04:28:06 +0000303
Chris Lattner94e04442006-10-21 05:36:13 +0000304 // One-way branch.
Chris Lattnera61f0102006-10-17 18:06:55 +0000305 if (FBB == 0) {
Chris Lattner94e04442006-10-21 05:36:13 +0000306 if (Cond.empty()) // Unconditional branch
Stuart Hastings0125b642010-06-17 22:43:56 +0000307 BuildMI(&MBB, DL, get(PPC::B)).addMBB(TBB);
Chris Lattner94e04442006-10-21 05:36:13 +0000308 else // Conditional branch
Stuart Hastings0125b642010-06-17 22:43:56 +0000309 BuildMI(&MBB, DL, get(PPC::BCC))
Chris Lattnerbe9377a2006-11-17 22:37:34 +0000310 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
Evan Cheng99be49d2007-05-18 00:05:48 +0000311 return 1;
Chris Lattnera61f0102006-10-17 18:06:55 +0000312 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000313
Chris Lattnerd8816602006-10-21 05:42:09 +0000314 // Two-way Conditional Branch.
Stuart Hastings0125b642010-06-17 22:43:56 +0000315 BuildMI(&MBB, DL, get(PPC::BCC))
Chris Lattnerbe9377a2006-11-17 22:37:34 +0000316 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
Stuart Hastings0125b642010-06-17 22:43:56 +0000317 BuildMI(&MBB, DL, get(PPC::B)).addMBB(FBB);
Evan Cheng99be49d2007-05-18 00:05:48 +0000318 return 2;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000319}
320
Jakob Stoklund Olesen0d611972010-07-11 07:31:00 +0000321void PPCInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
322 MachineBasicBlock::iterator I, DebugLoc DL,
323 unsigned DestReg, unsigned SrcReg,
324 bool KillSrc) const {
325 unsigned Opc;
326 if (PPC::GPRCRegClass.contains(DestReg, SrcReg))
327 Opc = PPC::OR;
328 else if (PPC::G8RCRegClass.contains(DestReg, SrcReg))
329 Opc = PPC::OR8;
330 else if (PPC::F4RCRegClass.contains(DestReg, SrcReg))
331 Opc = PPC::FMR;
332 else if (PPC::CRRCRegClass.contains(DestReg, SrcReg))
333 Opc = PPC::MCRF;
334 else if (PPC::VRRCRegClass.contains(DestReg, SrcReg))
335 Opc = PPC::VOR;
336 else if (PPC::CRBITRCRegClass.contains(DestReg, SrcReg))
337 Opc = PPC::CROR;
338 else
339 llvm_unreachable("Impossible reg-to-reg copy");
Owen Anderson7a73ae92007-12-31 06:32:00 +0000340
Evan Cheng6cc775f2011-06-28 19:10:37 +0000341 const MCInstrDesc &MCID = get(Opc);
342 if (MCID.getNumOperands() == 3)
343 BuildMI(MBB, I, DL, MCID, DestReg)
Jakob Stoklund Olesen0d611972010-07-11 07:31:00 +0000344 .addReg(SrcReg).addReg(SrcReg, getKillRegState(KillSrc));
345 else
Evan Cheng6cc775f2011-06-28 19:10:37 +0000346 BuildMI(MBB, I, DL, MCID, DestReg).addReg(SrcReg, getKillRegState(KillSrc));
Owen Anderson7a73ae92007-12-31 06:32:00 +0000347}
348
Hal Finkel8f6834d2011-12-05 17:55:17 +0000349// This function returns true if a CR spill is necessary and false otherwise.
Bill Wendlingc6c48fc2008-03-10 22:49:16 +0000350bool
Dan Gohman3b460302008-07-07 23:14:23 +0000351PPCInstrInfo::StoreRegToStackSlot(MachineFunction &MF,
352 unsigned SrcReg, bool isKill,
Bill Wendlingc6c48fc2008-03-10 22:49:16 +0000353 int FrameIdx,
354 const TargetRegisterClass *RC,
355 SmallVectorImpl<MachineInstr*> &NewMIs) const{
Chris Lattner6f306d72010-04-02 20:16:16 +0000356 DebugLoc DL;
Craig Topperabadc662012-04-20 06:31:50 +0000357 if (PPC::GPRCRegClass.hasSubClassEq(RC)) {
Owen Andersoneee14602008-01-01 21:11:32 +0000358 if (SrcReg != PPC::LR) {
Dale Johannesen6b8c76a2009-02-12 23:08:38 +0000359 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
Bill Wendlingf7b83c72009-05-13 21:33:08 +0000360 .addReg(SrcReg,
361 getKillRegState(isKill)),
Bill Wendlingc6c48fc2008-03-10 22:49:16 +0000362 FrameIdx));
Owen Andersoneee14602008-01-01 21:11:32 +0000363 } else {
364 // FIXME: this spills LR immediately to memory in one step. To do this,
365 // we use R11, which we know cannot be used in the prolog/epilog. This is
366 // a hack.
Dale Johannesen6b8c76a2009-02-12 23:08:38 +0000367 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MFLR), PPC::R11));
368 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
Bill Wendlingf7b83c72009-05-13 21:33:08 +0000369 .addReg(PPC::R11,
370 getKillRegState(isKill)),
Bill Wendlingc6c48fc2008-03-10 22:49:16 +0000371 FrameIdx));
Owen Andersoneee14602008-01-01 21:11:32 +0000372 }
Craig Topperabadc662012-04-20 06:31:50 +0000373 } else if (PPC::G8RCRegClass.hasSubClassEq(RC)) {
Owen Andersoneee14602008-01-01 21:11:32 +0000374 if (SrcReg != PPC::LR8) {
Dale Johannesen6b8c76a2009-02-12 23:08:38 +0000375 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STD))
Bill Wendlingf7b83c72009-05-13 21:33:08 +0000376 .addReg(SrcReg,
377 getKillRegState(isKill)),
378 FrameIdx));
Owen Andersoneee14602008-01-01 21:11:32 +0000379 } else {
380 // FIXME: this spills LR immediately to memory in one step. To do this,
Hal Finkel2ba61e42011-12-07 06:32:37 +0000381 // we use X11, which we know cannot be used in the prolog/epilog. This is
Owen Andersoneee14602008-01-01 21:11:32 +0000382 // a hack.
Dale Johannesen6b8c76a2009-02-12 23:08:38 +0000383 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MFLR8), PPC::X11));
384 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STD))
Bill Wendlingf7b83c72009-05-13 21:33:08 +0000385 .addReg(PPC::X11,
386 getKillRegState(isKill)),
387 FrameIdx));
Owen Andersoneee14602008-01-01 21:11:32 +0000388 }
Craig Topperabadc662012-04-20 06:31:50 +0000389 } else if (PPC::F8RCRegClass.hasSubClassEq(RC)) {
Dale Johannesen6b8c76a2009-02-12 23:08:38 +0000390 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFD))
Bill Wendlingf7b83c72009-05-13 21:33:08 +0000391 .addReg(SrcReg,
392 getKillRegState(isKill)),
393 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000394 } else if (PPC::F4RCRegClass.hasSubClassEq(RC)) {
Dale Johannesen6b8c76a2009-02-12 23:08:38 +0000395 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFS))
Bill Wendlingf7b83c72009-05-13 21:33:08 +0000396 .addReg(SrcReg,
397 getKillRegState(isKill)),
398 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000399 } else if (PPC::CRRCRegClass.hasSubClassEq(RC)) {
Hal Finkel8f6834d2011-12-05 17:55:17 +0000400 if ((!DisablePPC32RS && !TM.getSubtargetImpl()->isPPC64()) ||
401 (!DisablePPC64RS && TM.getSubtargetImpl()->isPPC64())) {
Dale Johannesen6b8c76a2009-02-12 23:08:38 +0000402 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CR))
Bill Wendlingf7b83c72009-05-13 21:33:08 +0000403 .addReg(SrcReg,
404 getKillRegState(isKill)),
Chris Lattnera7cca362008-03-20 01:22:40 +0000405 FrameIdx));
Bill Wendling632ea652008-03-03 22:19:16 +0000406 return true;
407 } else {
Dale Johannesen26062152010-02-12 21:35:34 +0000408 // FIXME: We need a scatch reg here. The trouble with using R0 is that
409 // it's possible for the stack frame to be so big the save location is
410 // out of range of immediate offsets, necessitating another register.
411 // We hack this on Darwin by reserving R2. It's probably broken on Linux
412 // at the moment.
413
Hal Finkelac9df3d2011-12-07 06:34:06 +0000414 bool is64Bit = TM.getSubtargetImpl()->isPPC64();
Dale Johannesen26062152010-02-12 21:35:34 +0000415 // We need to store the CR in the low 4-bits of the saved value. First,
416 // issue a MFCR to save all of the CRBits.
Andrew Trickc416ba62010-12-24 04:28:06 +0000417 unsigned ScratchReg = TM.getSubtargetImpl()->isDarwinABI() ?
Hal Finkelac9df3d2011-12-07 06:34:06 +0000418 (is64Bit ? PPC::X2 : PPC::R2) :
419 (is64Bit ? PPC::X0 : PPC::R0);
420 NewMIs.push_back(BuildMI(MF, DL, get(is64Bit ? PPC::MFCR8pseud :
421 PPC::MFCRpseud), ScratchReg)
Dale Johannesend7d66382010-05-20 17:48:26 +0000422 .addReg(SrcReg, getKillRegState(isKill)));
Andrew Trickc416ba62010-12-24 04:28:06 +0000423
Bill Wendling632ea652008-03-03 22:19:16 +0000424 // If the saved register wasn't CR0, shift the bits left so that they are
425 // in CR0's slot.
426 if (SrcReg != PPC::CR0) {
Evan Cheng61d4a202011-07-25 19:53:23 +0000427 unsigned ShiftBits = getPPCRegisterNumbering(SrcReg)*4;
Dale Johannesen26062152010-02-12 21:35:34 +0000428 // rlwinm scratch, scratch, ShiftBits, 0, 31.
Hal Finkelac9df3d2011-12-07 06:34:06 +0000429 NewMIs.push_back(BuildMI(MF, DL, get(is64Bit ? PPC::RLWINM8 :
430 PPC::RLWINM), ScratchReg)
Dale Johannesen26062152010-02-12 21:35:34 +0000431 .addReg(ScratchReg).addImm(ShiftBits)
432 .addImm(0).addImm(31));
Bill Wendling632ea652008-03-03 22:19:16 +0000433 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000434
Hal Finkelac9df3d2011-12-07 06:34:06 +0000435 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(is64Bit ?
436 PPC::STW8 : PPC::STW))
Dale Johannesen26062152010-02-12 21:35:34 +0000437 .addReg(ScratchReg,
Bill Wendlingf7b83c72009-05-13 21:33:08 +0000438 getKillRegState(isKill)),
Bill Wendling632ea652008-03-03 22:19:16 +0000439 FrameIdx));
Owen Andersoneee14602008-01-01 21:11:32 +0000440 }
Craig Topperabadc662012-04-20 06:31:50 +0000441 } else if (PPC::CRBITRCRegClass.hasSubClassEq(RC)) {
Nicolas Geoffrayb1de7a32008-03-10 14:12:10 +0000442 // 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 Geoffray708784e2008-03-10 17:46:45 +0000446 unsigned Reg = 0;
Tilmann Scheller9db3e702009-07-03 06:47:55 +0000447 if (SrcReg == PPC::CR0LT || SrcReg == PPC::CR0GT ||
448 SrcReg == PPC::CR0EQ || SrcReg == PPC::CR0UN)
Nicolas Geoffray708784e2008-03-10 17:46:45 +0000449 Reg = PPC::CR0;
Tilmann Scheller9db3e702009-07-03 06:47:55 +0000450 else if (SrcReg == PPC::CR1LT || SrcReg == PPC::CR1GT ||
451 SrcReg == PPC::CR1EQ || SrcReg == PPC::CR1UN)
Nicolas Geoffray708784e2008-03-10 17:46:45 +0000452 Reg = PPC::CR1;
Tilmann Scheller9db3e702009-07-03 06:47:55 +0000453 else if (SrcReg == PPC::CR2LT || SrcReg == PPC::CR2GT ||
454 SrcReg == PPC::CR2EQ || SrcReg == PPC::CR2UN)
Nicolas Geoffray708784e2008-03-10 17:46:45 +0000455 Reg = PPC::CR2;
Tilmann Scheller9db3e702009-07-03 06:47:55 +0000456 else if (SrcReg == PPC::CR3LT || SrcReg == PPC::CR3GT ||
457 SrcReg == PPC::CR3EQ || SrcReg == PPC::CR3UN)
Nicolas Geoffray708784e2008-03-10 17:46:45 +0000458 Reg = PPC::CR3;
Tilmann Scheller9db3e702009-07-03 06:47:55 +0000459 else if (SrcReg == PPC::CR4LT || SrcReg == PPC::CR4GT ||
460 SrcReg == PPC::CR4EQ || SrcReg == PPC::CR4UN)
Nicolas Geoffray708784e2008-03-10 17:46:45 +0000461 Reg = PPC::CR4;
Tilmann Scheller9db3e702009-07-03 06:47:55 +0000462 else if (SrcReg == PPC::CR5LT || SrcReg == PPC::CR5GT ||
463 SrcReg == PPC::CR5EQ || SrcReg == PPC::CR5UN)
Nicolas Geoffray708784e2008-03-10 17:46:45 +0000464 Reg = PPC::CR5;
Tilmann Scheller9db3e702009-07-03 06:47:55 +0000465 else if (SrcReg == PPC::CR6LT || SrcReg == PPC::CR6GT ||
466 SrcReg == PPC::CR6EQ || SrcReg == PPC::CR6UN)
Nicolas Geoffray708784e2008-03-10 17:46:45 +0000467 Reg = PPC::CR6;
Tilmann Scheller9db3e702009-07-03 06:47:55 +0000468 else if (SrcReg == PPC::CR7LT || SrcReg == PPC::CR7GT ||
469 SrcReg == PPC::CR7EQ || SrcReg == PPC::CR7UN)
Nicolas Geoffray708784e2008-03-10 17:46:45 +0000470 Reg = PPC::CR7;
471
Andrew Trickc416ba62010-12-24 04:28:06 +0000472 return StoreRegToStackSlot(MF, Reg, isKill, FrameIdx,
Craig Topperabadc662012-04-20 06:31:50 +0000473 &PPC::CRRCRegClass, NewMIs);
Nicolas Geoffray708784e2008-03-10 17:46:45 +0000474
Craig Topperabadc662012-04-20 06:31:50 +0000475 } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) {
Owen Andersoneee14602008-01-01 21:11:32 +0000476 // We don't have indexed addressing for vector loads. Emit:
477 // R0 = ADDI FI#
478 // STVX VAL, 0, R0
Andrew Trickc416ba62010-12-24 04:28:06 +0000479 //
Owen Andersoneee14602008-01-01 21:11:32 +0000480 // FIXME: We use R0 here, because it isn't available for RA.
Dale Johannesen6b8c76a2009-02-12 23:08:38 +0000481 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::ADDI), PPC::R0),
Owen Andersoneee14602008-01-01 21:11:32 +0000482 FrameIdx, 0, 0));
Dale Johannesen6b8c76a2009-02-12 23:08:38 +0000483 NewMIs.push_back(BuildMI(MF, DL, get(PPC::STVX))
Bill Wendlingf7b83c72009-05-13 21:33:08 +0000484 .addReg(SrcReg, getKillRegState(isKill))
485 .addReg(PPC::R0)
486 .addReg(PPC::R0));
Owen Andersoneee14602008-01-01 21:11:32 +0000487 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000488 llvm_unreachable("Unknown regclass!");
Owen Andersoneee14602008-01-01 21:11:32 +0000489 }
Bill Wendling632ea652008-03-03 22:19:16 +0000490
491 return false;
Owen Andersoneee14602008-01-01 21:11:32 +0000492}
493
494void
495PPCInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
Bill Wendling632ea652008-03-03 22:19:16 +0000496 MachineBasicBlock::iterator MI,
497 unsigned SrcReg, bool isKill, int FrameIdx,
Evan Chengefb126a2010-05-06 19:06:44 +0000498 const TargetRegisterClass *RC,
499 const TargetRegisterInfo *TRI) const {
Dan Gohman3b460302008-07-07 23:14:23 +0000500 MachineFunction &MF = *MBB.getParent();
Owen Andersoneee14602008-01-01 21:11:32 +0000501 SmallVector<MachineInstr*, 4> NewMIs;
Bill Wendling632ea652008-03-03 22:19:16 +0000502
Dan Gohman3b460302008-07-07 23:14:23 +0000503 if (StoreRegToStackSlot(MF, SrcReg, isKill, FrameIdx, RC, NewMIs)) {
504 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Bill Wendling632ea652008-03-03 22:19:16 +0000505 FuncInfo->setSpillsCR();
506 }
507
Owen Andersoneee14602008-01-01 21:11:32 +0000508 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
509 MBB.insert(MI, NewMIs[i]);
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +0000510
511 const MachineFrameInfo &MFI = *MF.getFrameInfo();
512 MachineMemOperand *MMO =
Jay Foad465101b2011-11-15 07:34:52 +0000513 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx),
Chris Lattnere3d864b2010-09-21 04:39:43 +0000514 MachineMemOperand::MOStore,
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +0000515 MFI.getObjectSize(FrameIdx),
516 MFI.getObjectAlignment(FrameIdx));
517 NewMIs.back()->addMemOperand(MF, MMO);
Owen Andersoneee14602008-01-01 21:11:32 +0000518}
519
Hal Finkelbde7f8f2011-12-06 20:55:36 +0000520bool
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000521PPCInstrInfo::LoadRegFromStackSlot(MachineFunction &MF, DebugLoc DL,
Dan Gohman3b460302008-07-07 23:14:23 +0000522 unsigned DestReg, int FrameIdx,
Bill Wendlingc6c48fc2008-03-10 22:49:16 +0000523 const TargetRegisterClass *RC,
524 SmallVectorImpl<MachineInstr*> &NewMIs)const{
Craig Topperabadc662012-04-20 06:31:50 +0000525 if (PPC::GPRCRegClass.hasSubClassEq(RC)) {
Owen Andersoneee14602008-01-01 21:11:32 +0000526 if (DestReg != PPC::LR) {
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000527 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ),
528 DestReg), FrameIdx));
Owen Andersoneee14602008-01-01 21:11:32 +0000529 } else {
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000530 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ),
531 PPC::R11), FrameIdx));
532 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MTLR)).addReg(PPC::R11));
Owen Andersoneee14602008-01-01 21:11:32 +0000533 }
Craig Topperabadc662012-04-20 06:31:50 +0000534 } else if (PPC::G8RCRegClass.hasSubClassEq(RC)) {
Owen Andersoneee14602008-01-01 21:11:32 +0000535 if (DestReg != PPC::LR8) {
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000536 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LD), DestReg),
Owen Andersoneee14602008-01-01 21:11:32 +0000537 FrameIdx));
538 } else {
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000539 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LD),
Hal Finkel2ba61e42011-12-07 06:32:37 +0000540 PPC::X11), FrameIdx));
541 NewMIs.push_back(BuildMI(MF, DL, get(PPC::MTLR8)).addReg(PPC::X11));
Owen Andersoneee14602008-01-01 21:11:32 +0000542 }
Craig Topperabadc662012-04-20 06:31:50 +0000543 } else if (PPC::F8RCRegClass.hasSubClassEq(RC)) {
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000544 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFD), DestReg),
Owen Andersoneee14602008-01-01 21:11:32 +0000545 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000546 } else if (PPC::F4RCRegClass.hasSubClassEq(RC)) {
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000547 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFS), DestReg),
Owen Andersoneee14602008-01-01 21:11:32 +0000548 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000549 } else if (PPC::CRRCRegClass.hasSubClassEq(RC)) {
Hal Finkelbde7f8f2011-12-06 20:55:36 +0000550 if ((!DisablePPC32RS && !TM.getSubtargetImpl()->isPPC64()) ||
551 (!DisablePPC64RS && TM.getSubtargetImpl()->isPPC64())) {
552 NewMIs.push_back(addFrameReference(BuildMI(MF, DL,
553 get(PPC::RESTORE_CR), DestReg)
554 , FrameIdx));
555 return true;
556 } else {
557 // FIXME: We need a scatch reg here. The trouble with using R0 is that
558 // it's possible for the stack frame to be so big the save location is
559 // out of range of immediate offsets, necessitating another register.
560 // We hack this on Darwin by reserving R2. It's probably broken on Linux
561 // at the moment.
562 unsigned ScratchReg = TM.getSubtargetImpl()->isDarwinABI() ?
563 PPC::R2 : PPC::R0;
564 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ),
565 ScratchReg), FrameIdx));
566
567 // If the reloaded register isn't CR0, shift the bits right so that they are
568 // in the right CR's slot.
569 if (DestReg != PPC::CR0) {
570 unsigned ShiftBits = getPPCRegisterNumbering(DestReg)*4;
571 // rlwinm r11, r11, 32-ShiftBits, 0, 31.
572 NewMIs.push_back(BuildMI(MF, DL, get(PPC::RLWINM), ScratchReg)
573 .addReg(ScratchReg).addImm(32-ShiftBits).addImm(0)
574 .addImm(31));
575 }
576
Hal Finkelac9df3d2011-12-07 06:34:06 +0000577 NewMIs.push_back(BuildMI(MF, DL, get(TM.getSubtargetImpl()->isPPC64() ?
578 PPC::MTCRF8 : PPC::MTCRF), DestReg)
Hal Finkelbde7f8f2011-12-06 20:55:36 +0000579 .addReg(ScratchReg));
Owen Andersoneee14602008-01-01 21:11:32 +0000580 }
Craig Topperabadc662012-04-20 06:31:50 +0000581 } else if (PPC::CRBITRCRegClass.hasSubClassEq(RC)) {
Andrew Trickc416ba62010-12-24 04:28:06 +0000582
Nicolas Geoffray708784e2008-03-10 17:46:45 +0000583 unsigned Reg = 0;
Tilmann Scheller9db3e702009-07-03 06:47:55 +0000584 if (DestReg == PPC::CR0LT || DestReg == PPC::CR0GT ||
585 DestReg == PPC::CR0EQ || DestReg == PPC::CR0UN)
Nicolas Geoffray708784e2008-03-10 17:46:45 +0000586 Reg = PPC::CR0;
Tilmann Scheller9db3e702009-07-03 06:47:55 +0000587 else if (DestReg == PPC::CR1LT || DestReg == PPC::CR1GT ||
588 DestReg == PPC::CR1EQ || DestReg == PPC::CR1UN)
Nicolas Geoffray708784e2008-03-10 17:46:45 +0000589 Reg = PPC::CR1;
Tilmann Scheller9db3e702009-07-03 06:47:55 +0000590 else if (DestReg == PPC::CR2LT || DestReg == PPC::CR2GT ||
591 DestReg == PPC::CR2EQ || DestReg == PPC::CR2UN)
Nicolas Geoffray708784e2008-03-10 17:46:45 +0000592 Reg = PPC::CR2;
Tilmann Scheller9db3e702009-07-03 06:47:55 +0000593 else if (DestReg == PPC::CR3LT || DestReg == PPC::CR3GT ||
594 DestReg == PPC::CR3EQ || DestReg == PPC::CR3UN)
Nicolas Geoffray708784e2008-03-10 17:46:45 +0000595 Reg = PPC::CR3;
Tilmann Scheller9db3e702009-07-03 06:47:55 +0000596 else if (DestReg == PPC::CR4LT || DestReg == PPC::CR4GT ||
597 DestReg == PPC::CR4EQ || DestReg == PPC::CR4UN)
Nicolas Geoffray708784e2008-03-10 17:46:45 +0000598 Reg = PPC::CR4;
Tilmann Scheller9db3e702009-07-03 06:47:55 +0000599 else if (DestReg == PPC::CR5LT || DestReg == PPC::CR5GT ||
600 DestReg == PPC::CR5EQ || DestReg == PPC::CR5UN)
Nicolas Geoffray708784e2008-03-10 17:46:45 +0000601 Reg = PPC::CR5;
Tilmann Scheller9db3e702009-07-03 06:47:55 +0000602 else if (DestReg == PPC::CR6LT || DestReg == PPC::CR6GT ||
603 DestReg == PPC::CR6EQ || DestReg == PPC::CR6UN)
Nicolas Geoffray708784e2008-03-10 17:46:45 +0000604 Reg = PPC::CR6;
Tilmann Scheller9db3e702009-07-03 06:47:55 +0000605 else if (DestReg == PPC::CR7LT || DestReg == PPC::CR7GT ||
606 DestReg == PPC::CR7EQ || DestReg == PPC::CR7UN)
Nicolas Geoffray708784e2008-03-10 17:46:45 +0000607 Reg = PPC::CR7;
608
Andrew Trickc416ba62010-12-24 04:28:06 +0000609 return LoadRegFromStackSlot(MF, DL, Reg, FrameIdx,
Craig Topperabadc662012-04-20 06:31:50 +0000610 &PPC::CRRCRegClass, NewMIs);
Nicolas Geoffray708784e2008-03-10 17:46:45 +0000611
Craig Topperabadc662012-04-20 06:31:50 +0000612 } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) {
Owen Andersoneee14602008-01-01 21:11:32 +0000613 // We don't have indexed addressing for vector loads. Emit:
614 // R0 = ADDI FI#
615 // Dest = LVX 0, R0
Andrew Trickc416ba62010-12-24 04:28:06 +0000616 //
Owen Andersoneee14602008-01-01 21:11:32 +0000617 // FIXME: We use R0 here, because it isn't available for RA.
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000618 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::ADDI), PPC::R0),
Owen Andersoneee14602008-01-01 21:11:32 +0000619 FrameIdx, 0, 0));
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000620 NewMIs.push_back(BuildMI(MF, DL, get(PPC::LVX),DestReg).addReg(PPC::R0)
Owen Andersoneee14602008-01-01 21:11:32 +0000621 .addReg(PPC::R0));
622 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000623 llvm_unreachable("Unknown regclass!");
Owen Andersoneee14602008-01-01 21:11:32 +0000624 }
Hal Finkelbde7f8f2011-12-06 20:55:36 +0000625
626 return false;
Owen Andersoneee14602008-01-01 21:11:32 +0000627}
628
629void
630PPCInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
Bill Wendling632ea652008-03-03 22:19:16 +0000631 MachineBasicBlock::iterator MI,
632 unsigned DestReg, int FrameIdx,
Evan Chengefb126a2010-05-06 19:06:44 +0000633 const TargetRegisterClass *RC,
634 const TargetRegisterInfo *TRI) const {
Dan Gohman3b460302008-07-07 23:14:23 +0000635 MachineFunction &MF = *MBB.getParent();
Owen Andersoneee14602008-01-01 21:11:32 +0000636 SmallVector<MachineInstr*, 4> NewMIs;
Chris Lattner6f306d72010-04-02 20:16:16 +0000637 DebugLoc DL;
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000638 if (MI != MBB.end()) DL = MI->getDebugLoc();
Hal Finkelbde7f8f2011-12-06 20:55:36 +0000639 if (LoadRegFromStackSlot(MF, DL, DestReg, FrameIdx, RC, NewMIs)) {
640 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
641 FuncInfo->setSpillsCR();
642 }
Owen Andersoneee14602008-01-01 21:11:32 +0000643 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
644 MBB.insert(MI, NewMIs[i]);
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +0000645
646 const MachineFrameInfo &MFI = *MF.getFrameInfo();
647 MachineMemOperand *MMO =
Jay Foad465101b2011-11-15 07:34:52 +0000648 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx),
Chris Lattnere3d864b2010-09-21 04:39:43 +0000649 MachineMemOperand::MOLoad,
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +0000650 MFI.getObjectSize(FrameIdx),
651 MFI.getObjectAlignment(FrameIdx));
652 NewMIs.back()->addMemOperand(MF, MMO);
Owen Andersoneee14602008-01-01 21:11:32 +0000653}
654
Evan Chengf19bd4e2010-04-26 07:39:36 +0000655MachineInstr*
656PPCInstrInfo::emitFrameIndexDebugValue(MachineFunction &MF,
Evan Cheng250e9172010-04-29 01:13:30 +0000657 int FrameIx, uint64_t Offset,
Evan Chengf19bd4e2010-04-26 07:39:36 +0000658 const MDNode *MDPtr,
659 DebugLoc DL) const {
660 MachineInstrBuilder MIB = BuildMI(MF, DL, get(PPC::DBG_VALUE));
661 addFrameReference(MIB, FrameIx, 0, false).addImm(Offset).addMetadata(MDPtr);
662 return &*MIB;
663}
664
Chris Lattnera47294ed2006-10-13 21:21:17 +0000665bool PPCInstrInfo::
Owen Anderson4f6bf042008-08-14 22:49:33 +0000666ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
Chris Lattner23f22de2006-10-21 06:03:11 +0000667 assert(Cond.size() == 2 && "Invalid PPC branch opcode!");
668 // Leave the CR# the same, but invert the condition.
Chris Lattnerbe9377a2006-11-17 22:37:34 +0000669 Cond[0].setImm(PPC::InvertPredicate((PPC::Predicate)Cond[0].getImm()));
Chris Lattner23f22de2006-10-21 06:03:11 +0000670 return false;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000671}
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +0000672
673/// GetInstSize - Return the number of bytes of code the specified
674/// instruction may be. This returns the maximum number of bytes.
675///
676unsigned PPCInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
677 switch (MI->getOpcode()) {
678 case PPC::INLINEASM: { // Inline Asm: Variable size.
679 const MachineFunction *MF = MI->getParent()->getParent();
680 const char *AsmStr = MI->getOperand(0).getSymbolName();
Chris Lattner7b26fce2009-08-22 20:48:53 +0000681 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +0000682 }
Bill Wendling499f7972010-07-16 22:20:36 +0000683 case PPC::PROLOG_LABEL:
Dan Gohmanfb19f942008-07-01 00:05:16 +0000684 case PPC::EH_LABEL:
685 case PPC::GC_LABEL:
Dale Johannesen60b28972010-04-07 19:51:44 +0000686 case PPC::DBG_VALUE:
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +0000687 return 0;
Hal Finkel51861b42012-03-31 14:45:15 +0000688 case PPC::BL8_NOP_ELF:
689 case PPC::BLA8_NOP_ELF:
690 return 8;
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +0000691 default:
692 return 4; // PowerPC instructions are all 4 bytes
693 }
694}