blob: 68ea976c344fe494f775f9bb8118cdc9886b2813 [file] [log] [blame]
Jia Liu31d157a2012-02-18 12:03:15 +00001//===-- PPCInstrInfo.cpp - PowerPC Instruction Information ----------------===//
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"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000015#include "MCTargetDesc/PPCPredicates.h"
Evan Cheng59ee62d2011-07-11 03:57:24 +000016#include "PPC.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000017#include "PPCHazardRecognizers.h"
Owen Andersonf6372aa2008-01-01 21:11:32 +000018#include "PPCInstrBuilder.h"
Bill Wendling7194aaf2008-03-03 22:19:16 +000019#include "PPCMachineFunctionInfo.h"
Chris Lattnerb1d26f62006-06-17 00:01:04 +000020#include "PPCTargetMachine.h"
Hal Finkel5ee67e82013-04-08 16:24:03 +000021#include "llvm/ADT/Statistic.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000022#include "llvm/ADT/STLExtras.h"
Jakob Stoklund Olesen7a79fcb2010-07-16 18:22:00 +000023#include "llvm/CodeGen/MachineFrameInfo.h"
Hal Finkel5ee67e82013-04-08 16:24:03 +000024#include "llvm/CodeGen/MachineFunctionPass.h"
Misha Brukmanf2ccb772004-08-17 04:55:41 +000025#include "llvm/CodeGen/MachineInstrBuilder.h"
Jakob Stoklund Olesen7a79fcb2010-07-16 18:22:00 +000026#include "llvm/CodeGen/MachineMemOperand.h"
Jakob Stoklund Olesen24329662010-02-26 21:09:24 +000027#include "llvm/CodeGen/MachineRegisterInfo.h"
Hal Finkel4d989ac2012-04-01 19:22:40 +000028#include "llvm/CodeGen/PseudoSourceValue.h"
Evan Cheng59ee62d2011-07-11 03:57:24 +000029#include "llvm/MC/MCAsmInfo.h"
Bill Wendling880d0f62008-03-04 23:13:51 +000030#include "llvm/Support/CommandLine.h"
Torok Edwindac237e2009-07-08 20:53:28 +000031#include "llvm/Support/ErrorHandling.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000032#include "llvm/Support/TargetRegistry.h"
Torok Edwindac237e2009-07-08 20:53:28 +000033#include "llvm/Support/raw_ostream.h"
Misha Brukmanf2ccb772004-08-17 04:55:41 +000034
Evan Cheng4db3cff2011-07-01 17:57:27 +000035#define GET_INSTRINFO_CTOR
Evan Cheng22fee2d2011-06-28 20:07:07 +000036#include "PPCGenInstrInfo.inc"
37
Dan Gohman82bcd232010-04-15 17:20:57 +000038using namespace llvm;
Bill Wendling880d0f62008-03-04 23:13:51 +000039
Hal Finkel09fdc7b2012-06-08 15:38:25 +000040static cl::
Hal Finkel7255d2a2012-06-08 19:19:53 +000041opt<bool> DisableCTRLoopAnal("disable-ppc-ctrloop-analysis", cl::Hidden,
42 cl::desc("Disable analysis for CTR loops"));
Hal Finkel09fdc7b2012-06-08 15:38:25 +000043
Chris Lattnerb1d26f62006-06-17 00:01:04 +000044PPCInstrInfo::PPCInstrInfo(PPCTargetMachine &tm)
Evan Cheng4db3cff2011-07-01 17:57:27 +000045 : PPCGenInstrInfo(PPC::ADJCALLSTACKDOWN, PPC::ADJCALLSTACKUP),
Evan Chengd5b03f22011-06-28 21:14:33 +000046 TM(tm), RI(*TM.getSubtargetImpl(), *this) {}
Chris Lattnerb1d26f62006-06-17 00:01:04 +000047
Andrew Trick2da8bc82010-12-24 05:03:26 +000048/// CreateTargetHazardRecognizer - Return the hazard recognizer to use for
49/// this target when scheduling the DAG.
50ScheduleHazardRecognizer *PPCInstrInfo::CreateTargetHazardRecognizer(
51 const TargetMachine *TM,
52 const ScheduleDAG *DAG) const {
Hal Finkelc6d08f12011-10-17 04:03:49 +000053 unsigned Directive = TM->getSubtarget<PPCSubtarget>().getDarwinDirective();
Hal Finkel621b77a2012-08-28 16:12:39 +000054 if (Directive == PPC::DIR_440 || Directive == PPC::DIR_A2 ||
55 Directive == PPC::DIR_E500mc || Directive == PPC::DIR_E5500) {
Hal Finkel768c65f2011-11-22 16:21:04 +000056 const InstrItineraryData *II = TM->getInstrItineraryData();
Hal Finkel5b00cea2012-03-31 14:45:15 +000057 return new PPCScoreboardHazardRecognizer(II, DAG);
Hal Finkelc6d08f12011-10-17 04:03:49 +000058 }
Hal Finkel64c34e22011-12-02 04:58:02 +000059
Jakob Stoklund Olesena9fa4fd2012-11-28 02:35:17 +000060 return TargetInstrInfo::CreateTargetHazardRecognizer(TM, DAG);
Andrew Trick2da8bc82010-12-24 05:03:26 +000061}
62
Hal Finkel64c34e22011-12-02 04:58:02 +000063/// CreateTargetPostRAHazardRecognizer - Return the postRA hazard recognizer
64/// to use for this target when scheduling the DAG.
65ScheduleHazardRecognizer *PPCInstrInfo::CreateTargetPostRAHazardRecognizer(
66 const InstrItineraryData *II,
67 const ScheduleDAG *DAG) const {
68 unsigned Directive = TM.getSubtarget<PPCSubtarget>().getDarwinDirective();
69
70 // Most subtargets use a PPC970 recognizer.
Hal Finkel621b77a2012-08-28 16:12:39 +000071 if (Directive != PPC::DIR_440 && Directive != PPC::DIR_A2 &&
72 Directive != PPC::DIR_E500mc && Directive != PPC::DIR_E5500) {
Hal Finkel64c34e22011-12-02 04:58:02 +000073 const TargetInstrInfo *TII = TM.getInstrInfo();
74 assert(TII && "No InstrInfo?");
75
76 return new PPCHazardRecognizer970(*TII);
77 }
78
Hal Finkel4d989ac2012-04-01 19:22:40 +000079 return new PPCScoreboardHazardRecognizer(II, DAG);
Hal Finkel64c34e22011-12-02 04:58:02 +000080}
Jakob Stoklund Olesen71642882012-06-19 21:14:34 +000081
82// Detect 32 -> 64-bit extensions where we may reuse the low sub-register.
83bool PPCInstrInfo::isCoalescableExtInstr(const MachineInstr &MI,
84 unsigned &SrcReg, unsigned &DstReg,
85 unsigned &SubIdx) const {
86 switch (MI.getOpcode()) {
87 default: return false;
88 case PPC::EXTSW:
89 case PPC::EXTSW_32_64:
90 SrcReg = MI.getOperand(1).getReg();
91 DstReg = MI.getOperand(0).getReg();
92 SubIdx = PPC::sub_32;
93 return true;
94 }
95}
96
Andrew Trick6e8f4c42010-12-24 04:28:06 +000097unsigned PPCInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
Chris Lattner9c09c9e2006-03-16 22:24:02 +000098 int &FrameIndex) const {
Hal Finkelf25f93b2013-03-27 21:21:15 +000099 // Note: This list must be kept consistent with LoadRegFromStackSlot.
Chris Lattner40839602006-02-02 20:12:32 +0000100 switch (MI->getOpcode()) {
101 default: break;
102 case PPC::LD:
103 case PPC::LWZ:
104 case PPC::LFS:
105 case PPC::LFD:
Hal Finkelf25f93b2013-03-27 21:21:15 +0000106 case PPC::RESTORE_CR:
107 case PPC::LVX:
108 case PPC::RESTORE_VRSAVE:
109 // Check for the operands added by addFrameReference (the immediate is the
110 // offset which defaults to 0).
Dan Gohmand735b802008-10-03 15:45:36 +0000111 if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
112 MI->getOperand(2).isFI()) {
Chris Lattner8aa797a2007-12-30 23:10:15 +0000113 FrameIndex = MI->getOperand(2).getIndex();
Chris Lattner40839602006-02-02 20:12:32 +0000114 return MI->getOperand(0).getReg();
115 }
116 break;
117 }
118 return 0;
Chris Lattner65242872006-02-02 20:16:12 +0000119}
Chris Lattner40839602006-02-02 20:12:32 +0000120
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000121unsigned PPCInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
Chris Lattner65242872006-02-02 20:16:12 +0000122 int &FrameIndex) const {
Hal Finkelf25f93b2013-03-27 21:21:15 +0000123 // Note: This list must be kept consistent with StoreRegToStackSlot.
Chris Lattner65242872006-02-02 20:16:12 +0000124 switch (MI->getOpcode()) {
125 default: break;
Nate Begeman3b478b32006-02-02 21:07:50 +0000126 case PPC::STD:
Chris Lattner65242872006-02-02 20:16:12 +0000127 case PPC::STW:
128 case PPC::STFS:
129 case PPC::STFD:
Hal Finkelf25f93b2013-03-27 21:21:15 +0000130 case PPC::SPILL_CR:
131 case PPC::STVX:
132 case PPC::SPILL_VRSAVE:
133 // Check for the operands added by addFrameReference (the immediate is the
134 // offset which defaults to 0).
Dan Gohmand735b802008-10-03 15:45:36 +0000135 if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
136 MI->getOperand(2).isFI()) {
Chris Lattner8aa797a2007-12-30 23:10:15 +0000137 FrameIndex = MI->getOperand(2).getIndex();
Chris Lattner65242872006-02-02 20:16:12 +0000138 return MI->getOperand(0).getReg();
139 }
140 break;
141 }
142 return 0;
143}
Chris Lattner40839602006-02-02 20:12:32 +0000144
Chris Lattner043870d2005-09-09 18:17:41 +0000145// commuteInstruction - We can commute rlwimi instructions, but only if the
146// rotate amt is zero. We also have to munge the immediates a bit.
Evan Cheng58dcb0e2008-06-16 07:33:11 +0000147MachineInstr *
148PPCInstrInfo::commuteInstruction(MachineInstr *MI, bool NewMI) const {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000149 MachineFunction &MF = *MI->getParent()->getParent();
150
Chris Lattner043870d2005-09-09 18:17:41 +0000151 // Normal instructions can be commuted the obvious way.
Hal Finkel171a8ad2013-04-12 02:18:09 +0000152 if (MI->getOpcode() != PPC::RLWIMI &&
153 MI->getOpcode() != PPC::RLWIMIo)
Jakob Stoklund Olesena9fa4fd2012-11-28 02:35:17 +0000154 return TargetInstrInfo::commuteInstruction(MI, NewMI);
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000155
Chris Lattner043870d2005-09-09 18:17:41 +0000156 // Cannot commute if it has a non-zero rotate count.
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000157 if (MI->getOperand(3).getImm() != 0)
Chris Lattner043870d2005-09-09 18:17:41 +0000158 return 0;
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000159
Chris Lattner043870d2005-09-09 18:17:41 +0000160 // If we have a zero rotate count, we have:
161 // M = mask(MB,ME)
162 // Op0 = (Op1 & ~M) | (Op2 & M)
163 // Change this to:
164 // M = mask((ME+1)&31, (MB-1)&31)
165 // Op0 = (Op2 & ~M) | (Op1 & M)
166
167 // Swap op1/op2
Evan Chenga4d16a12008-02-13 02:46:49 +0000168 unsigned Reg0 = MI->getOperand(0).getReg();
Chris Lattner043870d2005-09-09 18:17:41 +0000169 unsigned Reg1 = MI->getOperand(1).getReg();
170 unsigned Reg2 = MI->getOperand(2).getReg();
Evan Cheng6ce7dc22006-11-15 20:58:11 +0000171 bool Reg1IsKill = MI->getOperand(1).isKill();
172 bool Reg2IsKill = MI->getOperand(2).isKill();
Evan Cheng58dcb0e2008-06-16 07:33:11 +0000173 bool ChangeReg0 = false;
Evan Chenga4d16a12008-02-13 02:46:49 +0000174 // If machine instrs are no longer in two-address forms, update
175 // destination register as well.
176 if (Reg0 == Reg1) {
177 // Must be two address instruction!
Evan Chenge837dea2011-06-28 19:10:37 +0000178 assert(MI->getDesc().getOperandConstraint(0, MCOI::TIED_TO) &&
Evan Chenga4d16a12008-02-13 02:46:49 +0000179 "Expecting a two-address instruction!");
Evan Chenga4d16a12008-02-13 02:46:49 +0000180 Reg2IsKill = false;
Evan Cheng58dcb0e2008-06-16 07:33:11 +0000181 ChangeReg0 = true;
Evan Chenga4d16a12008-02-13 02:46:49 +0000182 }
Evan Cheng58dcb0e2008-06-16 07:33:11 +0000183
184 // Masks.
185 unsigned MB = MI->getOperand(4).getImm();
186 unsigned ME = MI->getOperand(5).getImm();
187
188 if (NewMI) {
189 // Create a new instruction.
190 unsigned Reg0 = ChangeReg0 ? Reg2 : MI->getOperand(0).getReg();
191 bool Reg0IsDead = MI->getOperand(0).isDead();
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000192 return BuildMI(MF, MI->getDebugLoc(), MI->getDesc())
Bill Wendling587daed2009-05-13 21:33:08 +0000193 .addReg(Reg0, RegState::Define | getDeadRegState(Reg0IsDead))
194 .addReg(Reg2, getKillRegState(Reg2IsKill))
195 .addReg(Reg1, getKillRegState(Reg1IsKill))
Evan Cheng58dcb0e2008-06-16 07:33:11 +0000196 .addImm((ME+1) & 31)
197 .addImm((MB-1) & 31);
198 }
199
200 if (ChangeReg0)
201 MI->getOperand(0).setReg(Reg2);
Chris Lattnere53f4a02006-05-04 17:52:23 +0000202 MI->getOperand(2).setReg(Reg1);
203 MI->getOperand(1).setReg(Reg2);
Chris Lattnerf7382302007-12-30 21:56:09 +0000204 MI->getOperand(2).setIsKill(Reg1IsKill);
205 MI->getOperand(1).setIsKill(Reg2IsKill);
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000206
Chris Lattner043870d2005-09-09 18:17:41 +0000207 // Swap the mask around.
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000208 MI->getOperand(4).setImm((ME+1) & 31);
209 MI->getOperand(5).setImm((MB-1) & 31);
Chris Lattner043870d2005-09-09 18:17:41 +0000210 return MI;
211}
Chris Lattnerbbf1c722006-03-05 23:49:55 +0000212
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000213void PPCInstrInfo::insertNoop(MachineBasicBlock &MBB,
Chris Lattnerbbf1c722006-03-05 23:49:55 +0000214 MachineBasicBlock::iterator MI) const {
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000215 DebugLoc DL;
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000216 BuildMI(MBB, MI, DL, get(PPC::NOP));
Chris Lattnerbbf1c722006-03-05 23:49:55 +0000217}
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000218
219
220// Branch analysis.
Hal Finkel99f823f2012-06-08 15:38:21 +0000221// Note: If the condition register is set to CTR or CTR8 then this is a
222// BDNZ (imm == 1) or BDZ (imm == 0) branch.
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000223bool PPCInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
224 MachineBasicBlock *&FBB,
Evan Chengdc54d312009-02-09 07:14:22 +0000225 SmallVectorImpl<MachineOperand> &Cond,
226 bool AllowModify) const {
Hal Finkel99f823f2012-06-08 15:38:21 +0000227 bool isPPC64 = TM.getSubtargetImpl()->isPPC64();
228
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000229 // If the block has no terminators, it just falls into the block after it.
230 MachineBasicBlock::iterator I = MBB.end();
Dale Johannesen93d6a7e2010-04-02 01:38:09 +0000231 if (I == MBB.begin())
232 return false;
233 --I;
234 while (I->isDebugValue()) {
235 if (I == MBB.begin())
236 return false;
237 --I;
238 }
239 if (!isUnpredicatedTerminator(I))
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000240 return false;
241
242 // Get the last instruction in the block.
243 MachineInstr *LastInst = I;
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000244
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000245 // If there is only one terminator instruction, process it.
Evan Chengbfd2ec42007-06-08 21:59:56 +0000246 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000247 if (LastInst->getOpcode() == PPC::B) {
Evan Cheng82ae9332009-05-08 23:09:25 +0000248 if (!LastInst->getOperand(0).isMBB())
249 return true;
Chris Lattner8aa797a2007-12-30 23:10:15 +0000250 TBB = LastInst->getOperand(0).getMBB();
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000251 return false;
Chris Lattner289c2d52006-11-17 22:14:47 +0000252 } else if (LastInst->getOpcode() == PPC::BCC) {
Evan Cheng82ae9332009-05-08 23:09:25 +0000253 if (!LastInst->getOperand(2).isMBB())
254 return true;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000255 // Block ends with fall-through condbranch.
Chris Lattner8aa797a2007-12-30 23:10:15 +0000256 TBB = LastInst->getOperand(2).getMBB();
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000257 Cond.push_back(LastInst->getOperand(0));
258 Cond.push_back(LastInst->getOperand(1));
Chris Lattner7c4fe252006-10-21 06:03:11 +0000259 return false;
Hal Finkel99f823f2012-06-08 15:38:21 +0000260 } else if (LastInst->getOpcode() == PPC::BDNZ8 ||
261 LastInst->getOpcode() == PPC::BDNZ) {
262 if (!LastInst->getOperand(0).isMBB())
263 return true;
Hal Finkel7255d2a2012-06-08 19:19:53 +0000264 if (DisableCTRLoopAnal)
Hal Finkel09fdc7b2012-06-08 15:38:25 +0000265 return true;
Hal Finkel99f823f2012-06-08 15:38:21 +0000266 TBB = LastInst->getOperand(0).getMBB();
267 Cond.push_back(MachineOperand::CreateImm(1));
268 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
269 true));
270 return false;
271 } else if (LastInst->getOpcode() == PPC::BDZ8 ||
272 LastInst->getOpcode() == PPC::BDZ) {
273 if (!LastInst->getOperand(0).isMBB())
274 return true;
Hal Finkel7255d2a2012-06-08 19:19:53 +0000275 if (DisableCTRLoopAnal)
Hal Finkel09fdc7b2012-06-08 15:38:25 +0000276 return true;
Hal Finkel99f823f2012-06-08 15:38:21 +0000277 TBB = LastInst->getOperand(0).getMBB();
278 Cond.push_back(MachineOperand::CreateImm(0));
279 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
280 true));
281 return false;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000282 }
Hal Finkel99f823f2012-06-08 15:38:21 +0000283
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000284 // Otherwise, don't know what this is.
285 return true;
286 }
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000287
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000288 // Get the instruction before it if it's a terminator.
289 MachineInstr *SecondLastInst = I;
290
291 // If there are three terminators, we don't know what sort of block this is.
292 if (SecondLastInst && I != MBB.begin() &&
Evan Chengbfd2ec42007-06-08 21:59:56 +0000293 isUnpredicatedTerminator(--I))
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000294 return true;
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000295
Chris Lattner289c2d52006-11-17 22:14:47 +0000296 // If the block ends with PPC::B and PPC:BCC, handle it.
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000297 if (SecondLastInst->getOpcode() == PPC::BCC &&
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000298 LastInst->getOpcode() == PPC::B) {
Evan Cheng82ae9332009-05-08 23:09:25 +0000299 if (!SecondLastInst->getOperand(2).isMBB() ||
300 !LastInst->getOperand(0).isMBB())
301 return true;
Chris Lattner8aa797a2007-12-30 23:10:15 +0000302 TBB = SecondLastInst->getOperand(2).getMBB();
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000303 Cond.push_back(SecondLastInst->getOperand(0));
304 Cond.push_back(SecondLastInst->getOperand(1));
Chris Lattner8aa797a2007-12-30 23:10:15 +0000305 FBB = LastInst->getOperand(0).getMBB();
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000306 return false;
Hal Finkel99f823f2012-06-08 15:38:21 +0000307 } else if ((SecondLastInst->getOpcode() == PPC::BDNZ8 ||
308 SecondLastInst->getOpcode() == PPC::BDNZ) &&
309 LastInst->getOpcode() == PPC::B) {
310 if (!SecondLastInst->getOperand(0).isMBB() ||
311 !LastInst->getOperand(0).isMBB())
312 return true;
Hal Finkel7255d2a2012-06-08 19:19:53 +0000313 if (DisableCTRLoopAnal)
Hal Finkel09fdc7b2012-06-08 15:38:25 +0000314 return true;
Hal Finkel99f823f2012-06-08 15:38:21 +0000315 TBB = SecondLastInst->getOperand(0).getMBB();
316 Cond.push_back(MachineOperand::CreateImm(1));
317 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
318 true));
319 FBB = LastInst->getOperand(0).getMBB();
320 return false;
321 } else if ((SecondLastInst->getOpcode() == PPC::BDZ8 ||
322 SecondLastInst->getOpcode() == PPC::BDZ) &&
323 LastInst->getOpcode() == PPC::B) {
324 if (!SecondLastInst->getOperand(0).isMBB() ||
325 !LastInst->getOperand(0).isMBB())
326 return true;
Hal Finkel7255d2a2012-06-08 19:19:53 +0000327 if (DisableCTRLoopAnal)
Hal Finkel09fdc7b2012-06-08 15:38:25 +0000328 return true;
Hal Finkel99f823f2012-06-08 15:38:21 +0000329 TBB = SecondLastInst->getOperand(0).getMBB();
330 Cond.push_back(MachineOperand::CreateImm(0));
331 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
332 true));
333 FBB = LastInst->getOperand(0).getMBB();
334 return false;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000335 }
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000336
Dale Johannesen13e8b512007-06-13 17:59:52 +0000337 // If the block ends with two PPC:Bs, handle it. The second one is not
338 // executed, so remove it.
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000339 if (SecondLastInst->getOpcode() == PPC::B &&
Dale Johannesen13e8b512007-06-13 17:59:52 +0000340 LastInst->getOpcode() == PPC::B) {
Evan Cheng82ae9332009-05-08 23:09:25 +0000341 if (!SecondLastInst->getOperand(0).isMBB())
342 return true;
Chris Lattner8aa797a2007-12-30 23:10:15 +0000343 TBB = SecondLastInst->getOperand(0).getMBB();
Dale Johannesen13e8b512007-06-13 17:59:52 +0000344 I = LastInst;
Evan Chengdc54d312009-02-09 07:14:22 +0000345 if (AllowModify)
346 I->eraseFromParent();
Dale Johannesen13e8b512007-06-13 17:59:52 +0000347 return false;
348 }
349
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000350 // Otherwise, can't handle this.
351 return true;
352}
353
Evan Chengb5cdaa22007-05-18 00:05:48 +0000354unsigned PPCInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000355 MachineBasicBlock::iterator I = MBB.end();
Evan Chengb5cdaa22007-05-18 00:05:48 +0000356 if (I == MBB.begin()) return 0;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000357 --I;
Dale Johannesen93d6a7e2010-04-02 01:38:09 +0000358 while (I->isDebugValue()) {
359 if (I == MBB.begin())
360 return 0;
361 --I;
362 }
Hal Finkel99f823f2012-06-08 15:38:21 +0000363 if (I->getOpcode() != PPC::B && I->getOpcode() != PPC::BCC &&
364 I->getOpcode() != PPC::BDNZ8 && I->getOpcode() != PPC::BDNZ &&
365 I->getOpcode() != PPC::BDZ8 && I->getOpcode() != PPC::BDZ)
Evan Chengb5cdaa22007-05-18 00:05:48 +0000366 return 0;
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000367
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000368 // Remove the branch.
369 I->eraseFromParent();
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000370
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000371 I = MBB.end();
372
Evan Chengb5cdaa22007-05-18 00:05:48 +0000373 if (I == MBB.begin()) return 1;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000374 --I;
Hal Finkel99f823f2012-06-08 15:38:21 +0000375 if (I->getOpcode() != PPC::BCC &&
376 I->getOpcode() != PPC::BDNZ8 && I->getOpcode() != PPC::BDNZ &&
377 I->getOpcode() != PPC::BDZ8 && I->getOpcode() != PPC::BDZ)
Evan Chengb5cdaa22007-05-18 00:05:48 +0000378 return 1;
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000379
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000380 // Remove the branch.
381 I->eraseFromParent();
Evan Chengb5cdaa22007-05-18 00:05:48 +0000382 return 2;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000383}
384
Evan Chengb5cdaa22007-05-18 00:05:48 +0000385unsigned
386PPCInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
387 MachineBasicBlock *FBB,
Stuart Hastings3bf91252010-06-17 22:43:56 +0000388 const SmallVectorImpl<MachineOperand> &Cond,
389 DebugLoc DL) const {
Chris Lattner2dc77232006-10-17 18:06:55 +0000390 // Shouldn't be a fall through.
391 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000392 assert((Cond.size() == 2 || Cond.size() == 0) &&
Chris Lattner54108062006-10-21 05:36:13 +0000393 "PPC branch conditions have two components!");
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000394
Hal Finkel99f823f2012-06-08 15:38:21 +0000395 bool isPPC64 = TM.getSubtargetImpl()->isPPC64();
396
Chris Lattner54108062006-10-21 05:36:13 +0000397 // One-way branch.
Chris Lattner2dc77232006-10-17 18:06:55 +0000398 if (FBB == 0) {
Chris Lattner54108062006-10-21 05:36:13 +0000399 if (Cond.empty()) // Unconditional branch
Stuart Hastings3bf91252010-06-17 22:43:56 +0000400 BuildMI(&MBB, DL, get(PPC::B)).addMBB(TBB);
Hal Finkel99f823f2012-06-08 15:38:21 +0000401 else if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
402 BuildMI(&MBB, DL, get(Cond[0].getImm() ?
403 (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
404 (isPPC64 ? PPC::BDZ8 : PPC::BDZ))).addMBB(TBB);
Chris Lattner54108062006-10-21 05:36:13 +0000405 else // Conditional branch
Stuart Hastings3bf91252010-06-17 22:43:56 +0000406 BuildMI(&MBB, DL, get(PPC::BCC))
Chris Lattner18258c62006-11-17 22:37:34 +0000407 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
Evan Chengb5cdaa22007-05-18 00:05:48 +0000408 return 1;
Chris Lattner2dc77232006-10-17 18:06:55 +0000409 }
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000410
Chris Lattner879d09c2006-10-21 05:42:09 +0000411 // Two-way Conditional Branch.
Hal Finkel99f823f2012-06-08 15:38:21 +0000412 if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
413 BuildMI(&MBB, DL, get(Cond[0].getImm() ?
414 (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
415 (isPPC64 ? PPC::BDZ8 : PPC::BDZ))).addMBB(TBB);
416 else
417 BuildMI(&MBB, DL, get(PPC::BCC))
418 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
Stuart Hastings3bf91252010-06-17 22:43:56 +0000419 BuildMI(&MBB, DL, get(PPC::B)).addMBB(FBB);
Evan Chengb5cdaa22007-05-18 00:05:48 +0000420 return 2;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000421}
422
Hal Finkelff56d1a2013-04-05 23:29:01 +0000423// Select analysis.
424bool PPCInstrInfo::canInsertSelect(const MachineBasicBlock &MBB,
425 const SmallVectorImpl<MachineOperand> &Cond,
426 unsigned TrueReg, unsigned FalseReg,
427 int &CondCycles, int &TrueCycles, int &FalseCycles) const {
428 if (!TM.getSubtargetImpl()->hasISEL())
429 return false;
430
431 if (Cond.size() != 2)
432 return false;
433
434 // If this is really a bdnz-like condition, then it cannot be turned into a
435 // select.
436 if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
437 return false;
438
439 // Check register classes.
440 const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
441 const TargetRegisterClass *RC =
442 RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg));
443 if (!RC)
444 return false;
445
446 // isel is for regular integer GPRs only.
447 if (!PPC::GPRCRegClass.hasSubClassEq(RC) &&
448 !PPC::G8RCRegClass.hasSubClassEq(RC))
449 return false;
450
451 // FIXME: These numbers are for the A2, how well they work for other cores is
452 // an open question. On the A2, the isel instruction has a 2-cycle latency
453 // but single-cycle throughput. These numbers are used in combination with
454 // the MispredictPenalty setting from the active SchedMachineModel.
455 CondCycles = 1;
456 TrueCycles = 1;
457 FalseCycles = 1;
458
459 return true;
460}
461
462void PPCInstrInfo::insertSelect(MachineBasicBlock &MBB,
463 MachineBasicBlock::iterator MI, DebugLoc dl,
464 unsigned DestReg,
465 const SmallVectorImpl<MachineOperand> &Cond,
466 unsigned TrueReg, unsigned FalseReg) const {
467 assert(Cond.size() == 2 &&
468 "PPC branch conditions have two components!");
469
470 assert(TM.getSubtargetImpl()->hasISEL() &&
471 "Cannot insert select on target without ISEL support");
472
473 // Get the register classes.
474 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
475 const TargetRegisterClass *RC =
476 RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg));
477 assert(RC && "TrueReg and FalseReg must have overlapping register classes");
478 assert((PPC::GPRCRegClass.hasSubClassEq(RC) ||
479 PPC::G8RCRegClass.hasSubClassEq(RC)) &&
480 "isel is for regular integer GPRs only");
481
482 unsigned OpCode =
483 PPC::GPRCRegClass.hasSubClassEq(RC) ? PPC::ISEL : PPC::ISEL8;
484 unsigned SelectPred = Cond[0].getImm();
485
486 unsigned SubIdx;
487 bool SwapOps;
488 switch (SelectPred) {
489 default: llvm_unreachable("invalid predicate for isel");
490 case PPC::PRED_EQ: SubIdx = PPC::sub_eq; SwapOps = false; break;
491 case PPC::PRED_NE: SubIdx = PPC::sub_eq; SwapOps = true; break;
492 case PPC::PRED_LT: SubIdx = PPC::sub_lt; SwapOps = false; break;
493 case PPC::PRED_GE: SubIdx = PPC::sub_lt; SwapOps = true; break;
494 case PPC::PRED_GT: SubIdx = PPC::sub_gt; SwapOps = false; break;
495 case PPC::PRED_LE: SubIdx = PPC::sub_gt; SwapOps = true; break;
496 case PPC::PRED_UN: SubIdx = PPC::sub_un; SwapOps = false; break;
497 case PPC::PRED_NU: SubIdx = PPC::sub_un; SwapOps = true; break;
498 }
499
500 unsigned FirstReg = SwapOps ? FalseReg : TrueReg,
501 SecondReg = SwapOps ? TrueReg : FalseReg;
502
503 // The first input register of isel cannot be r0. If it is a member
504 // of a register class that can be r0, then copy it first (the
505 // register allocator should eliminate the copy).
506 if (MRI.getRegClass(FirstReg)->contains(PPC::R0) ||
507 MRI.getRegClass(FirstReg)->contains(PPC::X0)) {
508 const TargetRegisterClass *FirstRC =
509 MRI.getRegClass(FirstReg)->contains(PPC::X0) ?
510 &PPC::G8RC_NOX0RegClass : &PPC::GPRC_NOR0RegClass;
511 unsigned OldFirstReg = FirstReg;
512 FirstReg = MRI.createVirtualRegister(FirstRC);
513 BuildMI(MBB, MI, dl, get(TargetOpcode::COPY), FirstReg)
514 .addReg(OldFirstReg);
515 }
516
517 BuildMI(MBB, MI, dl, get(OpCode), DestReg)
518 .addReg(FirstReg).addReg(SecondReg)
519 .addReg(Cond[1].getReg(), 0, SubIdx);
520}
521
Jakob Stoklund Olesen27689b02010-07-11 07:31:00 +0000522void PPCInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
523 MachineBasicBlock::iterator I, DebugLoc DL,
524 unsigned DestReg, unsigned SrcReg,
525 bool KillSrc) const {
526 unsigned Opc;
527 if (PPC::GPRCRegClass.contains(DestReg, SrcReg))
528 Opc = PPC::OR;
529 else if (PPC::G8RCRegClass.contains(DestReg, SrcReg))
530 Opc = PPC::OR8;
531 else if (PPC::F4RCRegClass.contains(DestReg, SrcReg))
532 Opc = PPC::FMR;
533 else if (PPC::CRRCRegClass.contains(DestReg, SrcReg))
534 Opc = PPC::MCRF;
535 else if (PPC::VRRCRegClass.contains(DestReg, SrcReg))
536 Opc = PPC::VOR;
537 else if (PPC::CRBITRCRegClass.contains(DestReg, SrcReg))
538 Opc = PPC::CROR;
539 else
540 llvm_unreachable("Impossible reg-to-reg copy");
Owen Andersond10fd972007-12-31 06:32:00 +0000541
Evan Chenge837dea2011-06-28 19:10:37 +0000542 const MCInstrDesc &MCID = get(Opc);
543 if (MCID.getNumOperands() == 3)
544 BuildMI(MBB, I, DL, MCID, DestReg)
Jakob Stoklund Olesen27689b02010-07-11 07:31:00 +0000545 .addReg(SrcReg).addReg(SrcReg, getKillRegState(KillSrc));
546 else
Evan Chenge837dea2011-06-28 19:10:37 +0000547 BuildMI(MBB, I, DL, MCID, DestReg).addReg(SrcReg, getKillRegState(KillSrc));
Owen Andersond10fd972007-12-31 06:32:00 +0000548}
549
Hal Finkel3fd00182011-12-05 17:55:17 +0000550// This function returns true if a CR spill is necessary and false otherwise.
Bill Wendling4a66e9a2008-03-10 22:49:16 +0000551bool
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000552PPCInstrInfo::StoreRegToStackSlot(MachineFunction &MF,
553 unsigned SrcReg, bool isKill,
Bill Wendling4a66e9a2008-03-10 22:49:16 +0000554 int FrameIdx,
555 const TargetRegisterClass *RC,
Hal Finkel32497292013-03-17 04:43:44 +0000556 SmallVectorImpl<MachineInstr*> &NewMIs,
Hal Finkel3f2c0472013-03-23 22:06:03 +0000557 bool &NonRI, bool &SpillsVRS) const{
Hal Finkelf25f93b2013-03-27 21:21:15 +0000558 // Note: If additional store instructions are added here,
559 // update isStoreToStackSlot.
560
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000561 DebugLoc DL;
Craig Topperc9099502012-04-20 06:31:50 +0000562 if (PPC::GPRCRegClass.hasSubClassEq(RC)) {
Hal Finkel7257fda2013-03-23 17:14:27 +0000563 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
564 .addReg(SrcReg,
565 getKillRegState(isKill)),
566 FrameIdx));
Craig Topperc9099502012-04-20 06:31:50 +0000567 } else if (PPC::G8RCRegClass.hasSubClassEq(RC)) {
Hal Finkel7257fda2013-03-23 17:14:27 +0000568 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STD))
569 .addReg(SrcReg,
570 getKillRegState(isKill)),
571 FrameIdx));
Craig Topperc9099502012-04-20 06:31:50 +0000572 } else if (PPC::F8RCRegClass.hasSubClassEq(RC)) {
Dale Johannesen21b55412009-02-12 23:08:38 +0000573 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFD))
Bill Wendling587daed2009-05-13 21:33:08 +0000574 .addReg(SrcReg,
575 getKillRegState(isKill)),
576 FrameIdx));
Craig Topperc9099502012-04-20 06:31:50 +0000577 } else if (PPC::F4RCRegClass.hasSubClassEq(RC)) {
Dale Johannesen21b55412009-02-12 23:08:38 +0000578 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFS))
Bill Wendling587daed2009-05-13 21:33:08 +0000579 .addReg(SrcReg,
580 getKillRegState(isKill)),
581 FrameIdx));
Craig Topperc9099502012-04-20 06:31:50 +0000582 } else if (PPC::CRRCRegClass.hasSubClassEq(RC)) {
Hal Finkel7285e8d2013-03-12 14:12:16 +0000583 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CR))
584 .addReg(SrcReg,
585 getKillRegState(isKill)),
586 FrameIdx));
587 return true;
Craig Topperc9099502012-04-20 06:31:50 +0000588 } else if (PPC::CRBITRCRegClass.hasSubClassEq(RC)) {
Nicolas Geoffray0404cd92008-03-10 14:12:10 +0000589 // FIXME: We use CRi here because there is no mtcrf on a bit. Since the
590 // backend currently only uses CR1EQ as an individual bit, this should
591 // not cause any bug. If we need other uses of CR bits, the following
592 // code may be invalid.
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000593 unsigned Reg = 0;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000594 if (SrcReg == PPC::CR0LT || SrcReg == PPC::CR0GT ||
595 SrcReg == PPC::CR0EQ || SrcReg == PPC::CR0UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000596 Reg = PPC::CR0;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000597 else if (SrcReg == PPC::CR1LT || SrcReg == PPC::CR1GT ||
598 SrcReg == PPC::CR1EQ || SrcReg == PPC::CR1UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000599 Reg = PPC::CR1;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000600 else if (SrcReg == PPC::CR2LT || SrcReg == PPC::CR2GT ||
601 SrcReg == PPC::CR2EQ || SrcReg == PPC::CR2UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000602 Reg = PPC::CR2;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000603 else if (SrcReg == PPC::CR3LT || SrcReg == PPC::CR3GT ||
604 SrcReg == PPC::CR3EQ || SrcReg == PPC::CR3UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000605 Reg = PPC::CR3;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000606 else if (SrcReg == PPC::CR4LT || SrcReg == PPC::CR4GT ||
607 SrcReg == PPC::CR4EQ || SrcReg == PPC::CR4UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000608 Reg = PPC::CR4;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000609 else if (SrcReg == PPC::CR5LT || SrcReg == PPC::CR5GT ||
610 SrcReg == PPC::CR5EQ || SrcReg == PPC::CR5UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000611 Reg = PPC::CR5;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000612 else if (SrcReg == PPC::CR6LT || SrcReg == PPC::CR6GT ||
613 SrcReg == PPC::CR6EQ || SrcReg == PPC::CR6UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000614 Reg = PPC::CR6;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000615 else if (SrcReg == PPC::CR7LT || SrcReg == PPC::CR7GT ||
616 SrcReg == PPC::CR7EQ || SrcReg == PPC::CR7UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000617 Reg = PPC::CR7;
618
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000619 return StoreRegToStackSlot(MF, Reg, isKill, FrameIdx,
Hal Finkel3f2c0472013-03-23 22:06:03 +0000620 &PPC::CRRCRegClass, NewMIs, NonRI, SpillsVRS);
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000621
Craig Topperc9099502012-04-20 06:31:50 +0000622 } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) {
Hal Finkel32497292013-03-17 04:43:44 +0000623 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STVX))
624 .addReg(SrcReg,
625 getKillRegState(isKill)),
626 FrameIdx));
627 NonRI = true;
Hal Finkel10f7f2a2013-03-21 19:03:21 +0000628 } else if (PPC::VRSAVERCRegClass.hasSubClassEq(RC)) {
Hal Finkelb7e11e42013-03-27 00:02:20 +0000629 assert(TM.getSubtargetImpl()->isDarwin() &&
630 "VRSAVE only needs spill/restore on Darwin");
Hal Finkel10f7f2a2013-03-21 19:03:21 +0000631 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_VRSAVE))
632 .addReg(SrcReg,
633 getKillRegState(isKill)),
634 FrameIdx));
Hal Finkel3f2c0472013-03-23 22:06:03 +0000635 SpillsVRS = true;
Owen Andersonf6372aa2008-01-01 21:11:32 +0000636 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000637 llvm_unreachable("Unknown regclass!");
Owen Andersonf6372aa2008-01-01 21:11:32 +0000638 }
Bill Wendling7194aaf2008-03-03 22:19:16 +0000639
640 return false;
Owen Andersonf6372aa2008-01-01 21:11:32 +0000641}
642
643void
644PPCInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
Bill Wendling7194aaf2008-03-03 22:19:16 +0000645 MachineBasicBlock::iterator MI,
646 unsigned SrcReg, bool isKill, int FrameIdx,
Evan Cheng746ad692010-05-06 19:06:44 +0000647 const TargetRegisterClass *RC,
648 const TargetRegisterInfo *TRI) const {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000649 MachineFunction &MF = *MBB.getParent();
Owen Andersonf6372aa2008-01-01 21:11:32 +0000650 SmallVector<MachineInstr*, 4> NewMIs;
Bill Wendling7194aaf2008-03-03 22:19:16 +0000651
Hal Finkel0cfb42a2013-03-15 05:06:04 +0000652 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
653 FuncInfo->setHasSpills();
654
Hal Finkel3f2c0472013-03-23 22:06:03 +0000655 bool NonRI = false, SpillsVRS = false;
656 if (StoreRegToStackSlot(MF, SrcReg, isKill, FrameIdx, RC, NewMIs,
657 NonRI, SpillsVRS))
Bill Wendling7194aaf2008-03-03 22:19:16 +0000658 FuncInfo->setSpillsCR();
Bill Wendling7194aaf2008-03-03 22:19:16 +0000659
Hal Finkel3f2c0472013-03-23 22:06:03 +0000660 if (SpillsVRS)
661 FuncInfo->setSpillsVRSAVE();
662
Hal Finkel32497292013-03-17 04:43:44 +0000663 if (NonRI)
664 FuncInfo->setHasNonRISpills();
665
Owen Andersonf6372aa2008-01-01 21:11:32 +0000666 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
667 MBB.insert(MI, NewMIs[i]);
Jakob Stoklund Olesen7a79fcb2010-07-16 18:22:00 +0000668
669 const MachineFrameInfo &MFI = *MF.getFrameInfo();
670 MachineMemOperand *MMO =
Jay Foad978e0df2011-11-15 07:34:52 +0000671 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx),
Chris Lattner59db5492010-09-21 04:39:43 +0000672 MachineMemOperand::MOStore,
Jakob Stoklund Olesen7a79fcb2010-07-16 18:22:00 +0000673 MFI.getObjectSize(FrameIdx),
674 MFI.getObjectAlignment(FrameIdx));
675 NewMIs.back()->addMemOperand(MF, MMO);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000676}
677
Hal Finkeld21e9302011-12-06 20:55:36 +0000678bool
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000679PPCInstrInfo::LoadRegFromStackSlot(MachineFunction &MF, DebugLoc DL,
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000680 unsigned DestReg, int FrameIdx,
Bill Wendling4a66e9a2008-03-10 22:49:16 +0000681 const TargetRegisterClass *RC,
Hal Finkel32497292013-03-17 04:43:44 +0000682 SmallVectorImpl<MachineInstr*> &NewMIs,
Hal Finkel3f2c0472013-03-23 22:06:03 +0000683 bool &NonRI, bool &SpillsVRS) const{
Hal Finkelf25f93b2013-03-27 21:21:15 +0000684 // Note: If additional load instructions are added here,
685 // update isLoadFromStackSlot.
686
Craig Topperc9099502012-04-20 06:31:50 +0000687 if (PPC::GPRCRegClass.hasSubClassEq(RC)) {
Hal Finkelfc805862013-03-27 19:10:40 +0000688 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ),
689 DestReg), FrameIdx));
Craig Topperc9099502012-04-20 06:31:50 +0000690 } else if (PPC::G8RCRegClass.hasSubClassEq(RC)) {
Hal Finkelfc805862013-03-27 19:10:40 +0000691 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LD), DestReg),
692 FrameIdx));
Craig Topperc9099502012-04-20 06:31:50 +0000693 } else if (PPC::F8RCRegClass.hasSubClassEq(RC)) {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000694 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFD), DestReg),
Owen Andersonf6372aa2008-01-01 21:11:32 +0000695 FrameIdx));
Craig Topperc9099502012-04-20 06:31:50 +0000696 } else if (PPC::F4RCRegClass.hasSubClassEq(RC)) {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000697 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFS), DestReg),
Owen Andersonf6372aa2008-01-01 21:11:32 +0000698 FrameIdx));
Craig Topperc9099502012-04-20 06:31:50 +0000699 } else if (PPC::CRRCRegClass.hasSubClassEq(RC)) {
Hal Finkel7285e8d2013-03-12 14:12:16 +0000700 NewMIs.push_back(addFrameReference(BuildMI(MF, DL,
701 get(PPC::RESTORE_CR), DestReg),
702 FrameIdx));
703 return true;
Craig Topperc9099502012-04-20 06:31:50 +0000704 } else if (PPC::CRBITRCRegClass.hasSubClassEq(RC)) {
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000705
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000706 unsigned Reg = 0;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000707 if (DestReg == PPC::CR0LT || DestReg == PPC::CR0GT ||
708 DestReg == PPC::CR0EQ || DestReg == PPC::CR0UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000709 Reg = PPC::CR0;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000710 else if (DestReg == PPC::CR1LT || DestReg == PPC::CR1GT ||
711 DestReg == PPC::CR1EQ || DestReg == PPC::CR1UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000712 Reg = PPC::CR1;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000713 else if (DestReg == PPC::CR2LT || DestReg == PPC::CR2GT ||
714 DestReg == PPC::CR2EQ || DestReg == PPC::CR2UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000715 Reg = PPC::CR2;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000716 else if (DestReg == PPC::CR3LT || DestReg == PPC::CR3GT ||
717 DestReg == PPC::CR3EQ || DestReg == PPC::CR3UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000718 Reg = PPC::CR3;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000719 else if (DestReg == PPC::CR4LT || DestReg == PPC::CR4GT ||
720 DestReg == PPC::CR4EQ || DestReg == PPC::CR4UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000721 Reg = PPC::CR4;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000722 else if (DestReg == PPC::CR5LT || DestReg == PPC::CR5GT ||
723 DestReg == PPC::CR5EQ || DestReg == PPC::CR5UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000724 Reg = PPC::CR5;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000725 else if (DestReg == PPC::CR6LT || DestReg == PPC::CR6GT ||
726 DestReg == PPC::CR6EQ || DestReg == PPC::CR6UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000727 Reg = PPC::CR6;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000728 else if (DestReg == PPC::CR7LT || DestReg == PPC::CR7GT ||
729 DestReg == PPC::CR7EQ || DestReg == PPC::CR7UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000730 Reg = PPC::CR7;
731
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000732 return LoadRegFromStackSlot(MF, DL, Reg, FrameIdx,
Hal Finkel3f2c0472013-03-23 22:06:03 +0000733 &PPC::CRRCRegClass, NewMIs, NonRI, SpillsVRS);
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000734
Craig Topperc9099502012-04-20 06:31:50 +0000735 } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) {
Hal Finkel32497292013-03-17 04:43:44 +0000736 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LVX), DestReg),
737 FrameIdx));
738 NonRI = true;
Hal Finkel10f7f2a2013-03-21 19:03:21 +0000739 } else if (PPC::VRSAVERCRegClass.hasSubClassEq(RC)) {
Hal Finkelb7e11e42013-03-27 00:02:20 +0000740 assert(TM.getSubtargetImpl()->isDarwin() &&
741 "VRSAVE only needs spill/restore on Darwin");
Hal Finkel10f7f2a2013-03-21 19:03:21 +0000742 NewMIs.push_back(addFrameReference(BuildMI(MF, DL,
743 get(PPC::RESTORE_VRSAVE),
744 DestReg),
745 FrameIdx));
Hal Finkel3f2c0472013-03-23 22:06:03 +0000746 SpillsVRS = true;
Owen Andersonf6372aa2008-01-01 21:11:32 +0000747 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000748 llvm_unreachable("Unknown regclass!");
Owen Andersonf6372aa2008-01-01 21:11:32 +0000749 }
Hal Finkeld21e9302011-12-06 20:55:36 +0000750
751 return false;
Owen Andersonf6372aa2008-01-01 21:11:32 +0000752}
753
754void
755PPCInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
Bill Wendling7194aaf2008-03-03 22:19:16 +0000756 MachineBasicBlock::iterator MI,
757 unsigned DestReg, int FrameIdx,
Evan Cheng746ad692010-05-06 19:06:44 +0000758 const TargetRegisterClass *RC,
759 const TargetRegisterInfo *TRI) const {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000760 MachineFunction &MF = *MBB.getParent();
Owen Andersonf6372aa2008-01-01 21:11:32 +0000761 SmallVector<MachineInstr*, 4> NewMIs;
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000762 DebugLoc DL;
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000763 if (MI != MBB.end()) DL = MI->getDebugLoc();
Hal Finkel32497292013-03-17 04:43:44 +0000764
765 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
766 FuncInfo->setHasSpills();
767
Hal Finkel3f2c0472013-03-23 22:06:03 +0000768 bool NonRI = false, SpillsVRS = false;
769 if (LoadRegFromStackSlot(MF, DL, DestReg, FrameIdx, RC, NewMIs,
770 NonRI, SpillsVRS))
Hal Finkeld21e9302011-12-06 20:55:36 +0000771 FuncInfo->setSpillsCR();
Hal Finkel32497292013-03-17 04:43:44 +0000772
Hal Finkel3f2c0472013-03-23 22:06:03 +0000773 if (SpillsVRS)
774 FuncInfo->setSpillsVRSAVE();
775
Hal Finkel32497292013-03-17 04:43:44 +0000776 if (NonRI)
777 FuncInfo->setHasNonRISpills();
778
Owen Andersonf6372aa2008-01-01 21:11:32 +0000779 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
780 MBB.insert(MI, NewMIs[i]);
Jakob Stoklund Olesen7a79fcb2010-07-16 18:22:00 +0000781
782 const MachineFrameInfo &MFI = *MF.getFrameInfo();
783 MachineMemOperand *MMO =
Jay Foad978e0df2011-11-15 07:34:52 +0000784 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx),
Chris Lattner59db5492010-09-21 04:39:43 +0000785 MachineMemOperand::MOLoad,
Jakob Stoklund Olesen7a79fcb2010-07-16 18:22:00 +0000786 MFI.getObjectSize(FrameIdx),
787 MFI.getObjectAlignment(FrameIdx));
788 NewMIs.back()->addMemOperand(MF, MMO);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000789}
790
Evan Cheng09652172010-04-26 07:39:36 +0000791MachineInstr*
792PPCInstrInfo::emitFrameIndexDebugValue(MachineFunction &MF,
Evan Cheng8601a3d2010-04-29 01:13:30 +0000793 int FrameIx, uint64_t Offset,
Evan Cheng09652172010-04-26 07:39:36 +0000794 const MDNode *MDPtr,
795 DebugLoc DL) const {
796 MachineInstrBuilder MIB = BuildMI(MF, DL, get(PPC::DBG_VALUE));
797 addFrameReference(MIB, FrameIx, 0, false).addImm(Offset).addMetadata(MDPtr);
798 return &*MIB;
799}
800
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000801bool PPCInstrInfo::
Owen Anderson44eb65c2008-08-14 22:49:33 +0000802ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
Chris Lattner7c4fe252006-10-21 06:03:11 +0000803 assert(Cond.size() == 2 && "Invalid PPC branch opcode!");
Hal Finkel99f823f2012-06-08 15:38:21 +0000804 if (Cond[1].getReg() == PPC::CTR8 || Cond[1].getReg() == PPC::CTR)
805 Cond[0].setImm(Cond[0].getImm() == 0 ? 1 : 0);
806 else
807 // Leave the CR# the same, but invert the condition.
808 Cond[0].setImm(PPC::InvertPredicate((PPC::Predicate)Cond[0].getImm()));
Chris Lattner7c4fe252006-10-21 06:03:11 +0000809 return false;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000810}
Nicolas Geoffray52e724a2008-04-16 20:10:13 +0000811
Hal Finkel839b9092013-04-06 19:30:30 +0000812bool PPCInstrInfo::FoldImmediate(MachineInstr *UseMI, MachineInstr *DefMI,
813 unsigned Reg, MachineRegisterInfo *MRI) const {
814 // For some instructions, it is legal to fold ZERO into the RA register field.
815 // A zero immediate should always be loaded with a single li.
816 unsigned DefOpc = DefMI->getOpcode();
817 if (DefOpc != PPC::LI && DefOpc != PPC::LI8)
818 return false;
819 if (!DefMI->getOperand(1).isImm())
820 return false;
821 if (DefMI->getOperand(1).getImm() != 0)
822 return false;
823
824 // Note that we cannot here invert the arguments of an isel in order to fold
825 // a ZERO into what is presented as the second argument. All we have here
826 // is the condition bit, and that might come from a CR-logical bit operation.
827
828 const MCInstrDesc &UseMCID = UseMI->getDesc();
829
830 // Only fold into real machine instructions.
831 if (UseMCID.isPseudo())
832 return false;
833
834 unsigned UseIdx;
835 for (UseIdx = 0; UseIdx < UseMI->getNumOperands(); ++UseIdx)
836 if (UseMI->getOperand(UseIdx).isReg() &&
837 UseMI->getOperand(UseIdx).getReg() == Reg)
838 break;
839
840 assert(UseIdx < UseMI->getNumOperands() && "Cannot find Reg in UseMI");
841 assert(UseIdx < UseMCID.getNumOperands() && "No operand description for Reg");
842
843 const MCOperandInfo *UseInfo = &UseMCID.OpInfo[UseIdx];
844
845 // We can fold the zero if this register requires a GPRC_NOR0/G8RC_NOX0
846 // register (which might also be specified as a pointer class kind).
847 if (UseInfo->isLookupPtrRegClass()) {
848 if (UseInfo->RegClass /* Kind */ != 1)
849 return false;
850 } else {
851 if (UseInfo->RegClass != PPC::GPRC_NOR0RegClassID &&
852 UseInfo->RegClass != PPC::G8RC_NOX0RegClassID)
853 return false;
854 }
855
856 // Make sure this is not tied to an output register (or otherwise
857 // constrained). This is true for ST?UX registers, for example, which
858 // are tied to their output registers.
859 if (UseInfo->Constraints != 0)
860 return false;
861
862 unsigned ZeroReg;
863 if (UseInfo->isLookupPtrRegClass()) {
864 bool isPPC64 = TM.getSubtargetImpl()->isPPC64();
865 ZeroReg = isPPC64 ? PPC::ZERO8 : PPC::ZERO;
866 } else {
867 ZeroReg = UseInfo->RegClass == PPC::G8RC_NOX0RegClassID ?
868 PPC::ZERO8 : PPC::ZERO;
869 }
870
871 bool DeleteDef = MRI->hasOneNonDBGUse(Reg);
872 UseMI->getOperand(UseIdx).setReg(ZeroReg);
873
874 if (DeleteDef)
875 DefMI->eraseFromParent();
876
877 return true;
878}
879
Hal Finkelda47e172013-04-10 18:30:16 +0000880static bool MBBDefinesCTR(MachineBasicBlock &MBB) {
881 for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end();
882 I != IE; ++I)
883 if (I->definesRegister(PPC::CTR) || I->definesRegister(PPC::CTR8))
884 return true;
885 return false;
886}
887
888// We should make sure that, if we're going to predicate both sides of a
889// condition (a diamond), that both sides don't define the counter register. We
890// can predicate counter-decrement-based branches, but while that predicates
891// the branching, it does not predicate the counter decrement. If we tried to
892// merge the triangle into one predicated block, we'd decrement the counter
893// twice.
894bool PPCInstrInfo::isProfitableToIfCvt(MachineBasicBlock &TMBB,
895 unsigned NumT, unsigned ExtraT,
896 MachineBasicBlock &FMBB,
897 unsigned NumF, unsigned ExtraF,
898 const BranchProbability &Probability) const {
899 return !(MBBDefinesCTR(TMBB) && MBBDefinesCTR(FMBB));
900}
901
902
Hal Finkel7eb0d812013-04-09 22:58:37 +0000903bool PPCInstrInfo::isPredicated(const MachineInstr *MI) const {
Hal Finkel4b040292013-04-11 01:23:34 +0000904 // The predicated branches are identified by their type, not really by the
905 // explicit presence of a predicate. Furthermore, some of them can be
906 // predicated more than once. Because if conversion won't try to predicate
907 // any instruction which already claims to be predicated (by returning true
908 // here), always return false. In doing so, we let isPredicable() be the
909 // final word on whether not the instruction can be (further) predicated.
910
911 return false;
Hal Finkel7eb0d812013-04-09 22:58:37 +0000912}
913
914bool PPCInstrInfo::isUnpredicatedTerminator(const MachineInstr *MI) const {
915 if (!MI->isTerminator())
916 return false;
917
918 // Conditional branch is a special case.
919 if (MI->isBranch() && !MI->isBarrier())
920 return true;
921
922 return !isPredicated(MI);
923}
924
925bool PPCInstrInfo::PredicateInstruction(
926 MachineInstr *MI,
927 const SmallVectorImpl<MachineOperand> &Pred) const {
928 unsigned OpC = MI->getOpcode();
929 if (OpC == PPC::BLR) {
930 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) {
931 bool isPPC64 = TM.getSubtargetImpl()->isPPC64();
932 MI->setDesc(get(Pred[0].getImm() ?
933 (isPPC64 ? PPC::BDNZLR8 : PPC::BDNZLR) :
934 (isPPC64 ? PPC::BDZLR8 : PPC::BDZLR)));
935 } else {
936 MI->setDesc(get(PPC::BCLR));
937 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
938 .addImm(Pred[0].getImm())
939 .addReg(Pred[1].getReg());
940 }
941
942 return true;
943 } else if (OpC == PPC::B) {
944 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) {
945 bool isPPC64 = TM.getSubtargetImpl()->isPPC64();
946 MI->setDesc(get(Pred[0].getImm() ?
947 (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
948 (isPPC64 ? PPC::BDZ8 : PPC::BDZ)));
949 } else {
950 MachineBasicBlock *MBB = MI->getOperand(0).getMBB();
951 MI->RemoveOperand(0);
952
953 MI->setDesc(get(PPC::BCC));
954 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
955 .addImm(Pred[0].getImm())
956 .addReg(Pred[1].getReg())
957 .addMBB(MBB);
958 }
959
960 return true;
Hal Finkel90dd7fd2013-04-10 06:42:34 +0000961 } else if (OpC == PPC::BCTR || OpC == PPC::BCTR8 ||
962 OpC == PPC::BCTRL || OpC == PPC::BCTRL8) {
963 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR)
964 llvm_unreachable("Cannot predicate bctr[l] on the ctr register");
965
966 bool setLR = OpC == PPC::BCTRL || OpC == PPC::BCTRL8;
967 bool isPPC64 = TM.getSubtargetImpl()->isPPC64();
968 MI->setDesc(get(isPPC64 ? (setLR ? PPC::BCCTRL8 : PPC::BCCTR8) :
969 (setLR ? PPC::BCCTRL : PPC::BCCTR)));
970 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
971 .addImm(Pred[0].getImm())
972 .addReg(Pred[1].getReg());
973 return true;
Hal Finkel7eb0d812013-04-09 22:58:37 +0000974 }
975
976 return false;
977}
978
979bool PPCInstrInfo::SubsumesPredicate(
980 const SmallVectorImpl<MachineOperand> &Pred1,
981 const SmallVectorImpl<MachineOperand> &Pred2) const {
982 assert(Pred1.size() == 2 && "Invalid PPC first predicate");
983 assert(Pred2.size() == 2 && "Invalid PPC second predicate");
984
985 if (Pred1[1].getReg() == PPC::CTR8 || Pred1[1].getReg() == PPC::CTR)
986 return false;
987 if (Pred2[1].getReg() == PPC::CTR8 || Pred2[1].getReg() == PPC::CTR)
988 return false;
989
990 PPC::Predicate P1 = (PPC::Predicate) Pred1[0].getImm();
991 PPC::Predicate P2 = (PPC::Predicate) Pred2[0].getImm();
992
993 if (P1 == P2)
994 return true;
995
996 // Does P1 subsume P2, e.g. GE subsumes GT.
997 if (P1 == PPC::PRED_LE &&
998 (P2 == PPC::PRED_LT || P2 == PPC::PRED_EQ))
999 return true;
1000 if (P1 == PPC::PRED_GE &&
1001 (P2 == PPC::PRED_GT || P2 == PPC::PRED_EQ))
1002 return true;
1003
1004 return false;
1005}
1006
1007bool PPCInstrInfo::DefinesPredicate(MachineInstr *MI,
1008 std::vector<MachineOperand> &Pred) const {
1009 // Note: At the present time, the contents of Pred from this function is
1010 // unused by IfConversion. This implementation follows ARM by pushing the
1011 // CR-defining operand. Because the 'DZ' and 'DNZ' count as types of
1012 // predicate, instructions defining CTR or CTR8 are also included as
1013 // predicate-defining instructions.
1014
1015 const TargetRegisterClass *RCs[] =
1016 { &PPC::CRRCRegClass, &PPC::CRBITRCRegClass,
1017 &PPC::CTRRCRegClass, &PPC::CTRRC8RegClass };
1018
1019 bool Found = false;
1020 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1021 const MachineOperand &MO = MI->getOperand(i);
Hal Finkel4e317282013-04-10 07:17:47 +00001022 for (unsigned c = 0; c < array_lengthof(RCs) && !Found; ++c) {
Hal Finkel7eb0d812013-04-09 22:58:37 +00001023 const TargetRegisterClass *RC = RCs[c];
Hal Finkel4e317282013-04-10 07:17:47 +00001024 if (MO.isReg()) {
1025 if (MO.isDef() && RC->contains(MO.getReg())) {
Hal Finkel7eb0d812013-04-09 22:58:37 +00001026 Pred.push_back(MO);
1027 Found = true;
1028 }
Hal Finkel4e317282013-04-10 07:17:47 +00001029 } else if (MO.isRegMask()) {
1030 for (TargetRegisterClass::iterator I = RC->begin(),
1031 IE = RC->end(); I != IE; ++I)
1032 if (MO.clobbersPhysReg(*I)) {
1033 Pred.push_back(MO);
1034 Found = true;
1035 }
Hal Finkel7eb0d812013-04-09 22:58:37 +00001036 }
1037 }
1038 }
1039
1040 return Found;
1041}
1042
1043bool PPCInstrInfo::isPredicable(MachineInstr *MI) const {
1044 unsigned OpC = MI->getOpcode();
1045 switch (OpC) {
1046 default:
1047 return false;
1048 case PPC::B:
1049 case PPC::BLR:
Hal Finkel90dd7fd2013-04-10 06:42:34 +00001050 case PPC::BCTR:
1051 case PPC::BCTR8:
1052 case PPC::BCTRL:
1053 case PPC::BCTRL8:
Hal Finkel7eb0d812013-04-09 22:58:37 +00001054 return true;
1055 }
1056}
1057
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00001058/// GetInstSize - Return the number of bytes of code the specified
1059/// instruction may be. This returns the maximum number of bytes.
1060///
1061unsigned PPCInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
1062 switch (MI->getOpcode()) {
1063 case PPC::INLINEASM: { // Inline Asm: Variable size.
1064 const MachineFunction *MF = MI->getParent()->getParent();
1065 const char *AsmStr = MI->getOperand(0).getSymbolName();
Chris Lattneraf76e592009-08-22 20:48:53 +00001066 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00001067 }
Bill Wendling7431bea2010-07-16 22:20:36 +00001068 case PPC::PROLOG_LABEL:
Dan Gohman44066042008-07-01 00:05:16 +00001069 case PPC::EH_LABEL:
1070 case PPC::GC_LABEL:
Dale Johannesen375be772010-04-07 19:51:44 +00001071 case PPC::DBG_VALUE:
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00001072 return 0;
Ulrich Weigand86765fb2013-03-22 15:24:13 +00001073 case PPC::BL8_NOP:
1074 case PPC::BLA8_NOP:
Hal Finkel5b00cea2012-03-31 14:45:15 +00001075 return 8;
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00001076 default:
1077 return 4; // PowerPC instructions are all 4 bytes
1078 }
1079}
Hal Finkel5ee67e82013-04-08 16:24:03 +00001080
1081#undef DEBUG_TYPE
1082#define DEBUG_TYPE "ppc-early-ret"
1083STATISTIC(NumBCLR, "Number of early conditional returns");
1084STATISTIC(NumBLR, "Number of early returns");
1085
1086namespace llvm {
1087 void initializePPCEarlyReturnPass(PassRegistry&);
1088}
1089
1090namespace {
1091 // PPCEarlyReturn pass - For simple functions without epilogue code, move
1092 // returns up, and create conditional returns, to avoid unnecessary
1093 // branch-to-blr sequences.
1094 struct PPCEarlyReturn : public MachineFunctionPass {
1095 static char ID;
1096 PPCEarlyReturn() : MachineFunctionPass(ID) {
1097 initializePPCEarlyReturnPass(*PassRegistry::getPassRegistry());
1098 }
1099
1100 const PPCTargetMachine *TM;
1101 const PPCInstrInfo *TII;
1102
1103protected:
Hal Finkel13049ae2013-04-09 18:25:18 +00001104 bool processBlock(MachineBasicBlock &ReturnMBB) {
Hal Finkel5ee67e82013-04-08 16:24:03 +00001105 bool Changed = false;
1106
Hal Finkel13049ae2013-04-09 18:25:18 +00001107 MachineBasicBlock::iterator I = ReturnMBB.begin();
1108 I = ReturnMBB.SkipPHIsAndLabels(I);
Hal Finkel5ee67e82013-04-08 16:24:03 +00001109
1110 // The block must be essentially empty except for the blr.
Hal Finkel13049ae2013-04-09 18:25:18 +00001111 if (I == ReturnMBB.end() || I->getOpcode() != PPC::BLR ||
1112 I != ReturnMBB.getLastNonDebugInstr())
Hal Finkel5ee67e82013-04-08 16:24:03 +00001113 return Changed;
1114
1115 SmallVector<MachineBasicBlock*, 8> PredToRemove;
Hal Finkel13049ae2013-04-09 18:25:18 +00001116 for (MachineBasicBlock::pred_iterator PI = ReturnMBB.pred_begin(),
1117 PIE = ReturnMBB.pred_end(); PI != PIE; ++PI) {
Hal Finkel5ee67e82013-04-08 16:24:03 +00001118 bool OtherReference = false, BlockChanged = false;
Hal Finkel13049ae2013-04-09 18:25:18 +00001119 for (MachineBasicBlock::iterator J = (*PI)->getLastNonDebugInstr();;) {
Hal Finkel5ee67e82013-04-08 16:24:03 +00001120 if (J->getOpcode() == PPC::B) {
Hal Finkel13049ae2013-04-09 18:25:18 +00001121 if (J->getOperand(0).getMBB() == &ReturnMBB) {
Hal Finkel5ee67e82013-04-08 16:24:03 +00001122 // This is an unconditional branch to the return. Replace the
1123 // branch with a blr.
1124 BuildMI(**PI, J, J->getDebugLoc(), TII->get(PPC::BLR));
Hal Finkel13049ae2013-04-09 18:25:18 +00001125 MachineBasicBlock::iterator K = J--;
Hal Finkel5ee67e82013-04-08 16:24:03 +00001126 K->eraseFromParent();
1127 BlockChanged = true;
1128 ++NumBLR;
1129 continue;
1130 }
1131 } else if (J->getOpcode() == PPC::BCC) {
Hal Finkel13049ae2013-04-09 18:25:18 +00001132 if (J->getOperand(2).getMBB() == &ReturnMBB) {
Hal Finkel5ee67e82013-04-08 16:24:03 +00001133 // This is a conditional branch to the return. Replace the branch
1134 // with a bclr.
1135 BuildMI(**PI, J, J->getDebugLoc(), TII->get(PPC::BCLR))
1136 .addImm(J->getOperand(0).getImm())
1137 .addReg(J->getOperand(1).getReg());
Hal Finkel13049ae2013-04-09 18:25:18 +00001138 MachineBasicBlock::iterator K = J--;
Hal Finkel5ee67e82013-04-08 16:24:03 +00001139 K->eraseFromParent();
1140 BlockChanged = true;
1141 ++NumBCLR;
1142 continue;
1143 }
1144 } else if (J->isBranch()) {
1145 if (J->isIndirectBranch()) {
Hal Finkel13049ae2013-04-09 18:25:18 +00001146 if (ReturnMBB.hasAddressTaken())
Hal Finkel5ee67e82013-04-08 16:24:03 +00001147 OtherReference = true;
1148 } else
1149 for (unsigned i = 0; i < J->getNumOperands(); ++i)
1150 if (J->getOperand(i).isMBB() &&
Hal Finkel13049ae2013-04-09 18:25:18 +00001151 J->getOperand(i).getMBB() == &ReturnMBB)
Hal Finkel5ee67e82013-04-08 16:24:03 +00001152 OtherReference = true;
Hal Finkel13049ae2013-04-09 18:25:18 +00001153 } else if (!J->isTerminator() && !J->isDebugValue())
1154 break;
Hal Finkel5ee67e82013-04-08 16:24:03 +00001155
Hal Finkel13049ae2013-04-09 18:25:18 +00001156 if (J == (*PI)->begin())
1157 break;
1158
1159 --J;
Hal Finkel5ee67e82013-04-08 16:24:03 +00001160 }
1161
Hal Finkel13049ae2013-04-09 18:25:18 +00001162 if ((*PI)->canFallThrough() && (*PI)->isLayoutSuccessor(&ReturnMBB))
Hal Finkel5ee67e82013-04-08 16:24:03 +00001163 OtherReference = true;
1164
1165 // Predecessors are stored in a vector and can't be removed here.
1166 if (!OtherReference && BlockChanged) {
1167 PredToRemove.push_back(*PI);
1168 }
1169
1170 if (BlockChanged)
1171 Changed = true;
1172 }
1173
1174 for (unsigned i = 0, ie = PredToRemove.size(); i != ie; ++i)
Hal Finkel13049ae2013-04-09 18:25:18 +00001175 PredToRemove[i]->removeSuccessor(&ReturnMBB);
Hal Finkel5ee67e82013-04-08 16:24:03 +00001176
Hal Finkel13049ae2013-04-09 18:25:18 +00001177 if (Changed && !ReturnMBB.hasAddressTaken()) {
Hal Finkel5ee67e82013-04-08 16:24:03 +00001178 // We now might be able to merge this blr-only block into its
1179 // by-layout predecessor.
Hal Finkel13049ae2013-04-09 18:25:18 +00001180 if (ReturnMBB.pred_size() == 1 &&
1181 (*ReturnMBB.pred_begin())->isLayoutSuccessor(&ReturnMBB)) {
Hal Finkel5ee67e82013-04-08 16:24:03 +00001182 // Move the blr into the preceding block.
Hal Finkel13049ae2013-04-09 18:25:18 +00001183 MachineBasicBlock &PrevMBB = **ReturnMBB.pred_begin();
1184 PrevMBB.splice(PrevMBB.end(), &ReturnMBB, I);
1185 PrevMBB.removeSuccessor(&ReturnMBB);
Hal Finkel5ee67e82013-04-08 16:24:03 +00001186 }
1187
Hal Finkel13049ae2013-04-09 18:25:18 +00001188 if (ReturnMBB.pred_empty())
1189 ReturnMBB.eraseFromParent();
Hal Finkel5ee67e82013-04-08 16:24:03 +00001190 }
1191
1192 return Changed;
1193 }
1194
1195public:
1196 virtual bool runOnMachineFunction(MachineFunction &MF) {
1197 TM = static_cast<const PPCTargetMachine *>(&MF.getTarget());
1198 TII = TM->getInstrInfo();
1199
1200 bool Changed = false;
1201
Hal Finkel13049ae2013-04-09 18:25:18 +00001202 // If the function does not have at least two blocks, then there is
Hal Finkel5ee67e82013-04-08 16:24:03 +00001203 // nothing to do.
1204 if (MF.size() < 2)
1205 return Changed;
1206
1207 for (MachineFunction::iterator I = MF.begin(); I != MF.end();) {
1208 MachineBasicBlock &B = *I++;
1209 if (processBlock(B))
1210 Changed = true;
1211 }
1212
1213 return Changed;
1214 }
1215
1216 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
1217 MachineFunctionPass::getAnalysisUsage(AU);
1218 }
1219 };
1220}
1221
1222INITIALIZE_PASS(PPCEarlyReturn, DEBUG_TYPE,
1223 "PowerPC Early-Return Creation", false, false)
1224
1225char PPCEarlyReturn::ID = 0;
1226FunctionPass*
1227llvm::createPPCEarlyReturnPass() { return new PPCEarlyReturn(); }
1228