blob: 847bd224b6f2d2d2529d96f5bb40a16c2af9cdf6 [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
Hal Finkel860c08c2013-04-18 22:15:08 +000035#define GET_INSTRMAP_INFO
Evan Cheng4db3cff2011-07-01 17:57:27 +000036#define GET_INSTRINFO_CTOR
Evan Cheng22fee2d2011-06-28 20:07:07 +000037#include "PPCGenInstrInfo.inc"
38
Dan Gohman82bcd232010-04-15 17:20:57 +000039using namespace llvm;
Bill Wendling880d0f62008-03-04 23:13:51 +000040
Hal Finkel09fdc7b2012-06-08 15:38:25 +000041static cl::
Hal Finkel7255d2a2012-06-08 19:19:53 +000042opt<bool> DisableCTRLoopAnal("disable-ppc-ctrloop-analysis", cl::Hidden,
43 cl::desc("Disable analysis for CTR loops"));
Hal Finkel09fdc7b2012-06-08 15:38:25 +000044
Hal Finkel87c1e422013-04-19 22:08:38 +000045static cl::opt<bool> DisableCmpOpt("disable-ppc-cmp-opt",
Hal Finkel4029c3f2013-04-18 22:54:25 +000046cl::desc("Disable compare instruction optimization"), cl::Hidden);
47
Chris Lattnerb1d26f62006-06-17 00:01:04 +000048PPCInstrInfo::PPCInstrInfo(PPCTargetMachine &tm)
Evan Cheng4db3cff2011-07-01 17:57:27 +000049 : PPCGenInstrInfo(PPC::ADJCALLSTACKDOWN, PPC::ADJCALLSTACKUP),
Evan Chengd5b03f22011-06-28 21:14:33 +000050 TM(tm), RI(*TM.getSubtargetImpl(), *this) {}
Chris Lattnerb1d26f62006-06-17 00:01:04 +000051
Andrew Trick2da8bc82010-12-24 05:03:26 +000052/// CreateTargetHazardRecognizer - Return the hazard recognizer to use for
53/// this target when scheduling the DAG.
54ScheduleHazardRecognizer *PPCInstrInfo::CreateTargetHazardRecognizer(
55 const TargetMachine *TM,
56 const ScheduleDAG *DAG) const {
Hal Finkelc6d08f12011-10-17 04:03:49 +000057 unsigned Directive = TM->getSubtarget<PPCSubtarget>().getDarwinDirective();
Hal Finkel621b77a2012-08-28 16:12:39 +000058 if (Directive == PPC::DIR_440 || Directive == PPC::DIR_A2 ||
59 Directive == PPC::DIR_E500mc || Directive == PPC::DIR_E5500) {
Hal Finkel768c65f2011-11-22 16:21:04 +000060 const InstrItineraryData *II = TM->getInstrItineraryData();
Hal Finkel5b00cea2012-03-31 14:45:15 +000061 return new PPCScoreboardHazardRecognizer(II, DAG);
Hal Finkelc6d08f12011-10-17 04:03:49 +000062 }
Hal Finkel64c34e22011-12-02 04:58:02 +000063
Jakob Stoklund Olesena9fa4fd2012-11-28 02:35:17 +000064 return TargetInstrInfo::CreateTargetHazardRecognizer(TM, DAG);
Andrew Trick2da8bc82010-12-24 05:03:26 +000065}
66
Hal Finkel64c34e22011-12-02 04:58:02 +000067/// CreateTargetPostRAHazardRecognizer - Return the postRA hazard recognizer
68/// to use for this target when scheduling the DAG.
69ScheduleHazardRecognizer *PPCInstrInfo::CreateTargetPostRAHazardRecognizer(
70 const InstrItineraryData *II,
71 const ScheduleDAG *DAG) const {
72 unsigned Directive = TM.getSubtarget<PPCSubtarget>().getDarwinDirective();
73
74 // Most subtargets use a PPC970 recognizer.
Hal Finkel621b77a2012-08-28 16:12:39 +000075 if (Directive != PPC::DIR_440 && Directive != PPC::DIR_A2 &&
76 Directive != PPC::DIR_E500mc && Directive != PPC::DIR_E5500) {
Hal Finkel64c34e22011-12-02 04:58:02 +000077 const TargetInstrInfo *TII = TM.getInstrInfo();
78 assert(TII && "No InstrInfo?");
79
80 return new PPCHazardRecognizer970(*TII);
81 }
82
Hal Finkel4d989ac2012-04-01 19:22:40 +000083 return new PPCScoreboardHazardRecognizer(II, DAG);
Hal Finkel64c34e22011-12-02 04:58:02 +000084}
Jakob Stoklund Olesen71642882012-06-19 21:14:34 +000085
86// Detect 32 -> 64-bit extensions where we may reuse the low sub-register.
87bool PPCInstrInfo::isCoalescableExtInstr(const MachineInstr &MI,
88 unsigned &SrcReg, unsigned &DstReg,
89 unsigned &SubIdx) const {
90 switch (MI.getOpcode()) {
91 default: return false;
92 case PPC::EXTSW:
93 case PPC::EXTSW_32_64:
94 SrcReg = MI.getOperand(1).getReg();
95 DstReg = MI.getOperand(0).getReg();
96 SubIdx = PPC::sub_32;
97 return true;
98 }
99}
100
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000101unsigned PPCInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
Chris Lattner9c09c9e2006-03-16 22:24:02 +0000102 int &FrameIndex) const {
Hal Finkelf25f93b2013-03-27 21:21:15 +0000103 // Note: This list must be kept consistent with LoadRegFromStackSlot.
Chris Lattner40839602006-02-02 20:12:32 +0000104 switch (MI->getOpcode()) {
105 default: break;
106 case PPC::LD:
107 case PPC::LWZ:
108 case PPC::LFS:
109 case PPC::LFD:
Hal Finkelf25f93b2013-03-27 21:21:15 +0000110 case PPC::RESTORE_CR:
111 case PPC::LVX:
112 case PPC::RESTORE_VRSAVE:
113 // Check for the operands added by addFrameReference (the immediate is the
114 // offset which defaults to 0).
Dan Gohmand735b802008-10-03 15:45:36 +0000115 if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
116 MI->getOperand(2).isFI()) {
Chris Lattner8aa797a2007-12-30 23:10:15 +0000117 FrameIndex = MI->getOperand(2).getIndex();
Chris Lattner40839602006-02-02 20:12:32 +0000118 return MI->getOperand(0).getReg();
119 }
120 break;
121 }
122 return 0;
Chris Lattner65242872006-02-02 20:16:12 +0000123}
Chris Lattner40839602006-02-02 20:12:32 +0000124
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000125unsigned PPCInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
Chris Lattner65242872006-02-02 20:16:12 +0000126 int &FrameIndex) const {
Hal Finkelf25f93b2013-03-27 21:21:15 +0000127 // Note: This list must be kept consistent with StoreRegToStackSlot.
Chris Lattner65242872006-02-02 20:16:12 +0000128 switch (MI->getOpcode()) {
129 default: break;
Nate Begeman3b478b32006-02-02 21:07:50 +0000130 case PPC::STD:
Chris Lattner65242872006-02-02 20:16:12 +0000131 case PPC::STW:
132 case PPC::STFS:
133 case PPC::STFD:
Hal Finkelf25f93b2013-03-27 21:21:15 +0000134 case PPC::SPILL_CR:
135 case PPC::STVX:
136 case PPC::SPILL_VRSAVE:
137 // Check for the operands added by addFrameReference (the immediate is the
138 // offset which defaults to 0).
Dan Gohmand735b802008-10-03 15:45:36 +0000139 if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
140 MI->getOperand(2).isFI()) {
Chris Lattner8aa797a2007-12-30 23:10:15 +0000141 FrameIndex = MI->getOperand(2).getIndex();
Chris Lattner65242872006-02-02 20:16:12 +0000142 return MI->getOperand(0).getReg();
143 }
144 break;
145 }
146 return 0;
147}
Chris Lattner40839602006-02-02 20:12:32 +0000148
Chris Lattner043870d2005-09-09 18:17:41 +0000149// commuteInstruction - We can commute rlwimi instructions, but only if the
150// rotate amt is zero. We also have to munge the immediates a bit.
Evan Cheng58dcb0e2008-06-16 07:33:11 +0000151MachineInstr *
152PPCInstrInfo::commuteInstruction(MachineInstr *MI, bool NewMI) const {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000153 MachineFunction &MF = *MI->getParent()->getParent();
154
Chris Lattner043870d2005-09-09 18:17:41 +0000155 // Normal instructions can be commuted the obvious way.
Hal Finkel171a8ad2013-04-12 02:18:09 +0000156 if (MI->getOpcode() != PPC::RLWIMI &&
157 MI->getOpcode() != PPC::RLWIMIo)
Jakob Stoklund Olesena9fa4fd2012-11-28 02:35:17 +0000158 return TargetInstrInfo::commuteInstruction(MI, NewMI);
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000159
Chris Lattner043870d2005-09-09 18:17:41 +0000160 // Cannot commute if it has a non-zero rotate count.
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000161 if (MI->getOperand(3).getImm() != 0)
Chris Lattner043870d2005-09-09 18:17:41 +0000162 return 0;
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000163
Chris Lattner043870d2005-09-09 18:17:41 +0000164 // If we have a zero rotate count, we have:
165 // M = mask(MB,ME)
166 // Op0 = (Op1 & ~M) | (Op2 & M)
167 // Change this to:
168 // M = mask((ME+1)&31, (MB-1)&31)
169 // Op0 = (Op2 & ~M) | (Op1 & M)
170
171 // Swap op1/op2
Evan Chenga4d16a12008-02-13 02:46:49 +0000172 unsigned Reg0 = MI->getOperand(0).getReg();
Chris Lattner043870d2005-09-09 18:17:41 +0000173 unsigned Reg1 = MI->getOperand(1).getReg();
174 unsigned Reg2 = MI->getOperand(2).getReg();
Evan Cheng6ce7dc22006-11-15 20:58:11 +0000175 bool Reg1IsKill = MI->getOperand(1).isKill();
176 bool Reg2IsKill = MI->getOperand(2).isKill();
Evan Cheng58dcb0e2008-06-16 07:33:11 +0000177 bool ChangeReg0 = false;
Evan Chenga4d16a12008-02-13 02:46:49 +0000178 // If machine instrs are no longer in two-address forms, update
179 // destination register as well.
180 if (Reg0 == Reg1) {
181 // Must be two address instruction!
Evan Chenge837dea2011-06-28 19:10:37 +0000182 assert(MI->getDesc().getOperandConstraint(0, MCOI::TIED_TO) &&
Evan Chenga4d16a12008-02-13 02:46:49 +0000183 "Expecting a two-address instruction!");
Evan Chenga4d16a12008-02-13 02:46:49 +0000184 Reg2IsKill = false;
Evan Cheng58dcb0e2008-06-16 07:33:11 +0000185 ChangeReg0 = true;
Evan Chenga4d16a12008-02-13 02:46:49 +0000186 }
Evan Cheng58dcb0e2008-06-16 07:33:11 +0000187
188 // Masks.
189 unsigned MB = MI->getOperand(4).getImm();
190 unsigned ME = MI->getOperand(5).getImm();
191
192 if (NewMI) {
193 // Create a new instruction.
194 unsigned Reg0 = ChangeReg0 ? Reg2 : MI->getOperand(0).getReg();
195 bool Reg0IsDead = MI->getOperand(0).isDead();
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000196 return BuildMI(MF, MI->getDebugLoc(), MI->getDesc())
Bill Wendling587daed2009-05-13 21:33:08 +0000197 .addReg(Reg0, RegState::Define | getDeadRegState(Reg0IsDead))
198 .addReg(Reg2, getKillRegState(Reg2IsKill))
199 .addReg(Reg1, getKillRegState(Reg1IsKill))
Evan Cheng58dcb0e2008-06-16 07:33:11 +0000200 .addImm((ME+1) & 31)
201 .addImm((MB-1) & 31);
202 }
203
204 if (ChangeReg0)
205 MI->getOperand(0).setReg(Reg2);
Chris Lattnere53f4a02006-05-04 17:52:23 +0000206 MI->getOperand(2).setReg(Reg1);
207 MI->getOperand(1).setReg(Reg2);
Chris Lattnerf7382302007-12-30 21:56:09 +0000208 MI->getOperand(2).setIsKill(Reg1IsKill);
209 MI->getOperand(1).setIsKill(Reg2IsKill);
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000210
Chris Lattner043870d2005-09-09 18:17:41 +0000211 // Swap the mask around.
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000212 MI->getOperand(4).setImm((ME+1) & 31);
213 MI->getOperand(5).setImm((MB-1) & 31);
Chris Lattner043870d2005-09-09 18:17:41 +0000214 return MI;
215}
Chris Lattnerbbf1c722006-03-05 23:49:55 +0000216
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000217void PPCInstrInfo::insertNoop(MachineBasicBlock &MBB,
Chris Lattnerbbf1c722006-03-05 23:49:55 +0000218 MachineBasicBlock::iterator MI) const {
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000219 DebugLoc DL;
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000220 BuildMI(MBB, MI, DL, get(PPC::NOP));
Chris Lattnerbbf1c722006-03-05 23:49:55 +0000221}
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000222
223
224// Branch analysis.
Hal Finkel99f823f2012-06-08 15:38:21 +0000225// Note: If the condition register is set to CTR or CTR8 then this is a
226// BDNZ (imm == 1) or BDZ (imm == 0) branch.
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000227bool PPCInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
228 MachineBasicBlock *&FBB,
Evan Chengdc54d312009-02-09 07:14:22 +0000229 SmallVectorImpl<MachineOperand> &Cond,
230 bool AllowModify) const {
Hal Finkel99f823f2012-06-08 15:38:21 +0000231 bool isPPC64 = TM.getSubtargetImpl()->isPPC64();
232
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000233 // If the block has no terminators, it just falls into the block after it.
234 MachineBasicBlock::iterator I = MBB.end();
Dale Johannesen93d6a7e2010-04-02 01:38:09 +0000235 if (I == MBB.begin())
236 return false;
237 --I;
238 while (I->isDebugValue()) {
239 if (I == MBB.begin())
240 return false;
241 --I;
242 }
243 if (!isUnpredicatedTerminator(I))
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000244 return false;
245
246 // Get the last instruction in the block.
247 MachineInstr *LastInst = I;
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000248
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000249 // If there is only one terminator instruction, process it.
Evan Chengbfd2ec42007-06-08 21:59:56 +0000250 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000251 if (LastInst->getOpcode() == PPC::B) {
Evan Cheng82ae9332009-05-08 23:09:25 +0000252 if (!LastInst->getOperand(0).isMBB())
253 return true;
Chris Lattner8aa797a2007-12-30 23:10:15 +0000254 TBB = LastInst->getOperand(0).getMBB();
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000255 return false;
Chris Lattner289c2d52006-11-17 22:14:47 +0000256 } else if (LastInst->getOpcode() == PPC::BCC) {
Evan Cheng82ae9332009-05-08 23:09:25 +0000257 if (!LastInst->getOperand(2).isMBB())
258 return true;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000259 // Block ends with fall-through condbranch.
Chris Lattner8aa797a2007-12-30 23:10:15 +0000260 TBB = LastInst->getOperand(2).getMBB();
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000261 Cond.push_back(LastInst->getOperand(0));
262 Cond.push_back(LastInst->getOperand(1));
Chris Lattner7c4fe252006-10-21 06:03:11 +0000263 return false;
Hal Finkel99f823f2012-06-08 15:38:21 +0000264 } else if (LastInst->getOpcode() == PPC::BDNZ8 ||
265 LastInst->getOpcode() == PPC::BDNZ) {
266 if (!LastInst->getOperand(0).isMBB())
267 return true;
Hal Finkel7255d2a2012-06-08 19:19:53 +0000268 if (DisableCTRLoopAnal)
Hal Finkel09fdc7b2012-06-08 15:38:25 +0000269 return true;
Hal Finkel99f823f2012-06-08 15:38:21 +0000270 TBB = LastInst->getOperand(0).getMBB();
271 Cond.push_back(MachineOperand::CreateImm(1));
272 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
273 true));
274 return false;
275 } else if (LastInst->getOpcode() == PPC::BDZ8 ||
276 LastInst->getOpcode() == PPC::BDZ) {
277 if (!LastInst->getOperand(0).isMBB())
278 return true;
Hal Finkel7255d2a2012-06-08 19:19:53 +0000279 if (DisableCTRLoopAnal)
Hal Finkel09fdc7b2012-06-08 15:38:25 +0000280 return true;
Hal Finkel99f823f2012-06-08 15:38:21 +0000281 TBB = LastInst->getOperand(0).getMBB();
282 Cond.push_back(MachineOperand::CreateImm(0));
283 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
284 true));
285 return false;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000286 }
Hal Finkel99f823f2012-06-08 15:38:21 +0000287
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000288 // Otherwise, don't know what this is.
289 return true;
290 }
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000291
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000292 // Get the instruction before it if it's a terminator.
293 MachineInstr *SecondLastInst = I;
294
295 // If there are three terminators, we don't know what sort of block this is.
296 if (SecondLastInst && I != MBB.begin() &&
Evan Chengbfd2ec42007-06-08 21:59:56 +0000297 isUnpredicatedTerminator(--I))
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000298 return true;
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000299
Chris Lattner289c2d52006-11-17 22:14:47 +0000300 // If the block ends with PPC::B and PPC:BCC, handle it.
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000301 if (SecondLastInst->getOpcode() == PPC::BCC &&
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000302 LastInst->getOpcode() == PPC::B) {
Evan Cheng82ae9332009-05-08 23:09:25 +0000303 if (!SecondLastInst->getOperand(2).isMBB() ||
304 !LastInst->getOperand(0).isMBB())
305 return true;
Chris Lattner8aa797a2007-12-30 23:10:15 +0000306 TBB = SecondLastInst->getOperand(2).getMBB();
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000307 Cond.push_back(SecondLastInst->getOperand(0));
308 Cond.push_back(SecondLastInst->getOperand(1));
Chris Lattner8aa797a2007-12-30 23:10:15 +0000309 FBB = LastInst->getOperand(0).getMBB();
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000310 return false;
Hal Finkel99f823f2012-06-08 15:38:21 +0000311 } else if ((SecondLastInst->getOpcode() == PPC::BDNZ8 ||
312 SecondLastInst->getOpcode() == PPC::BDNZ) &&
313 LastInst->getOpcode() == PPC::B) {
314 if (!SecondLastInst->getOperand(0).isMBB() ||
315 !LastInst->getOperand(0).isMBB())
316 return true;
Hal Finkel7255d2a2012-06-08 19:19:53 +0000317 if (DisableCTRLoopAnal)
Hal Finkel09fdc7b2012-06-08 15:38:25 +0000318 return true;
Hal Finkel99f823f2012-06-08 15:38:21 +0000319 TBB = SecondLastInst->getOperand(0).getMBB();
320 Cond.push_back(MachineOperand::CreateImm(1));
321 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
322 true));
323 FBB = LastInst->getOperand(0).getMBB();
324 return false;
325 } else if ((SecondLastInst->getOpcode() == PPC::BDZ8 ||
326 SecondLastInst->getOpcode() == PPC::BDZ) &&
327 LastInst->getOpcode() == PPC::B) {
328 if (!SecondLastInst->getOperand(0).isMBB() ||
329 !LastInst->getOperand(0).isMBB())
330 return true;
Hal Finkel7255d2a2012-06-08 19:19:53 +0000331 if (DisableCTRLoopAnal)
Hal Finkel09fdc7b2012-06-08 15:38:25 +0000332 return true;
Hal Finkel99f823f2012-06-08 15:38:21 +0000333 TBB = SecondLastInst->getOperand(0).getMBB();
334 Cond.push_back(MachineOperand::CreateImm(0));
335 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
336 true));
337 FBB = LastInst->getOperand(0).getMBB();
338 return false;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000339 }
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000340
Dale Johannesen13e8b512007-06-13 17:59:52 +0000341 // If the block ends with two PPC:Bs, handle it. The second one is not
342 // executed, so remove it.
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000343 if (SecondLastInst->getOpcode() == PPC::B &&
Dale Johannesen13e8b512007-06-13 17:59:52 +0000344 LastInst->getOpcode() == PPC::B) {
Evan Cheng82ae9332009-05-08 23:09:25 +0000345 if (!SecondLastInst->getOperand(0).isMBB())
346 return true;
Chris Lattner8aa797a2007-12-30 23:10:15 +0000347 TBB = SecondLastInst->getOperand(0).getMBB();
Dale Johannesen13e8b512007-06-13 17:59:52 +0000348 I = LastInst;
Evan Chengdc54d312009-02-09 07:14:22 +0000349 if (AllowModify)
350 I->eraseFromParent();
Dale Johannesen13e8b512007-06-13 17:59:52 +0000351 return false;
352 }
353
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000354 // Otherwise, can't handle this.
355 return true;
356}
357
Evan Chengb5cdaa22007-05-18 00:05:48 +0000358unsigned PPCInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000359 MachineBasicBlock::iterator I = MBB.end();
Evan Chengb5cdaa22007-05-18 00:05:48 +0000360 if (I == MBB.begin()) return 0;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000361 --I;
Dale Johannesen93d6a7e2010-04-02 01:38:09 +0000362 while (I->isDebugValue()) {
363 if (I == MBB.begin())
364 return 0;
365 --I;
366 }
Hal Finkel99f823f2012-06-08 15:38:21 +0000367 if (I->getOpcode() != PPC::B && I->getOpcode() != PPC::BCC &&
368 I->getOpcode() != PPC::BDNZ8 && I->getOpcode() != PPC::BDNZ &&
369 I->getOpcode() != PPC::BDZ8 && I->getOpcode() != PPC::BDZ)
Evan Chengb5cdaa22007-05-18 00:05:48 +0000370 return 0;
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000371
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000372 // Remove the branch.
373 I->eraseFromParent();
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000374
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000375 I = MBB.end();
376
Evan Chengb5cdaa22007-05-18 00:05:48 +0000377 if (I == MBB.begin()) return 1;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000378 --I;
Hal Finkel99f823f2012-06-08 15:38:21 +0000379 if (I->getOpcode() != PPC::BCC &&
380 I->getOpcode() != PPC::BDNZ8 && I->getOpcode() != PPC::BDNZ &&
381 I->getOpcode() != PPC::BDZ8 && I->getOpcode() != PPC::BDZ)
Evan Chengb5cdaa22007-05-18 00:05:48 +0000382 return 1;
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000383
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000384 // Remove the branch.
385 I->eraseFromParent();
Evan Chengb5cdaa22007-05-18 00:05:48 +0000386 return 2;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000387}
388
Evan Chengb5cdaa22007-05-18 00:05:48 +0000389unsigned
390PPCInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
391 MachineBasicBlock *FBB,
Stuart Hastings3bf91252010-06-17 22:43:56 +0000392 const SmallVectorImpl<MachineOperand> &Cond,
393 DebugLoc DL) const {
Chris Lattner2dc77232006-10-17 18:06:55 +0000394 // Shouldn't be a fall through.
395 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000396 assert((Cond.size() == 2 || Cond.size() == 0) &&
Chris Lattner54108062006-10-21 05:36:13 +0000397 "PPC branch conditions have two components!");
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000398
Hal Finkel99f823f2012-06-08 15:38:21 +0000399 bool isPPC64 = TM.getSubtargetImpl()->isPPC64();
400
Chris Lattner54108062006-10-21 05:36:13 +0000401 // One-way branch.
Chris Lattner2dc77232006-10-17 18:06:55 +0000402 if (FBB == 0) {
Chris Lattner54108062006-10-21 05:36:13 +0000403 if (Cond.empty()) // Unconditional branch
Stuart Hastings3bf91252010-06-17 22:43:56 +0000404 BuildMI(&MBB, DL, get(PPC::B)).addMBB(TBB);
Hal Finkel99f823f2012-06-08 15:38:21 +0000405 else if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
406 BuildMI(&MBB, DL, get(Cond[0].getImm() ?
407 (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
408 (isPPC64 ? PPC::BDZ8 : PPC::BDZ))).addMBB(TBB);
Chris Lattner54108062006-10-21 05:36:13 +0000409 else // Conditional branch
Stuart Hastings3bf91252010-06-17 22:43:56 +0000410 BuildMI(&MBB, DL, get(PPC::BCC))
Chris Lattner18258c62006-11-17 22:37:34 +0000411 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
Evan Chengb5cdaa22007-05-18 00:05:48 +0000412 return 1;
Chris Lattner2dc77232006-10-17 18:06:55 +0000413 }
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000414
Chris Lattner879d09c2006-10-21 05:42:09 +0000415 // Two-way Conditional Branch.
Hal Finkel99f823f2012-06-08 15:38:21 +0000416 if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
417 BuildMI(&MBB, DL, get(Cond[0].getImm() ?
418 (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
419 (isPPC64 ? PPC::BDZ8 : PPC::BDZ))).addMBB(TBB);
420 else
421 BuildMI(&MBB, DL, get(PPC::BCC))
422 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
Stuart Hastings3bf91252010-06-17 22:43:56 +0000423 BuildMI(&MBB, DL, get(PPC::B)).addMBB(FBB);
Evan Chengb5cdaa22007-05-18 00:05:48 +0000424 return 2;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000425}
426
Hal Finkelff56d1a2013-04-05 23:29:01 +0000427// Select analysis.
428bool PPCInstrInfo::canInsertSelect(const MachineBasicBlock &MBB,
429 const SmallVectorImpl<MachineOperand> &Cond,
430 unsigned TrueReg, unsigned FalseReg,
431 int &CondCycles, int &TrueCycles, int &FalseCycles) const {
432 if (!TM.getSubtargetImpl()->hasISEL())
433 return false;
434
435 if (Cond.size() != 2)
436 return false;
437
438 // If this is really a bdnz-like condition, then it cannot be turned into a
439 // select.
440 if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
441 return false;
442
443 // Check register classes.
444 const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
445 const TargetRegisterClass *RC =
446 RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg));
447 if (!RC)
448 return false;
449
450 // isel is for regular integer GPRs only.
451 if (!PPC::GPRCRegClass.hasSubClassEq(RC) &&
452 !PPC::G8RCRegClass.hasSubClassEq(RC))
453 return false;
454
455 // FIXME: These numbers are for the A2, how well they work for other cores is
456 // an open question. On the A2, the isel instruction has a 2-cycle latency
457 // but single-cycle throughput. These numbers are used in combination with
458 // the MispredictPenalty setting from the active SchedMachineModel.
459 CondCycles = 1;
460 TrueCycles = 1;
461 FalseCycles = 1;
462
463 return true;
464}
465
466void PPCInstrInfo::insertSelect(MachineBasicBlock &MBB,
467 MachineBasicBlock::iterator MI, DebugLoc dl,
468 unsigned DestReg,
469 const SmallVectorImpl<MachineOperand> &Cond,
470 unsigned TrueReg, unsigned FalseReg) const {
471 assert(Cond.size() == 2 &&
472 "PPC branch conditions have two components!");
473
474 assert(TM.getSubtargetImpl()->hasISEL() &&
475 "Cannot insert select on target without ISEL support");
476
477 // Get the register classes.
478 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
479 const TargetRegisterClass *RC =
480 RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg));
481 assert(RC && "TrueReg and FalseReg must have overlapping register classes");
482 assert((PPC::GPRCRegClass.hasSubClassEq(RC) ||
483 PPC::G8RCRegClass.hasSubClassEq(RC)) &&
484 "isel is for regular integer GPRs only");
485
486 unsigned OpCode =
487 PPC::GPRCRegClass.hasSubClassEq(RC) ? PPC::ISEL : PPC::ISEL8;
488 unsigned SelectPred = Cond[0].getImm();
489
490 unsigned SubIdx;
491 bool SwapOps;
492 switch (SelectPred) {
493 default: llvm_unreachable("invalid predicate for isel");
494 case PPC::PRED_EQ: SubIdx = PPC::sub_eq; SwapOps = false; break;
495 case PPC::PRED_NE: SubIdx = PPC::sub_eq; SwapOps = true; break;
496 case PPC::PRED_LT: SubIdx = PPC::sub_lt; SwapOps = false; break;
497 case PPC::PRED_GE: SubIdx = PPC::sub_lt; SwapOps = true; break;
498 case PPC::PRED_GT: SubIdx = PPC::sub_gt; SwapOps = false; break;
499 case PPC::PRED_LE: SubIdx = PPC::sub_gt; SwapOps = true; break;
500 case PPC::PRED_UN: SubIdx = PPC::sub_un; SwapOps = false; break;
501 case PPC::PRED_NU: SubIdx = PPC::sub_un; SwapOps = true; break;
502 }
503
504 unsigned FirstReg = SwapOps ? FalseReg : TrueReg,
505 SecondReg = SwapOps ? TrueReg : FalseReg;
506
507 // The first input register of isel cannot be r0. If it is a member
508 // of a register class that can be r0, then copy it first (the
509 // register allocator should eliminate the copy).
510 if (MRI.getRegClass(FirstReg)->contains(PPC::R0) ||
511 MRI.getRegClass(FirstReg)->contains(PPC::X0)) {
512 const TargetRegisterClass *FirstRC =
513 MRI.getRegClass(FirstReg)->contains(PPC::X0) ?
514 &PPC::G8RC_NOX0RegClass : &PPC::GPRC_NOR0RegClass;
515 unsigned OldFirstReg = FirstReg;
516 FirstReg = MRI.createVirtualRegister(FirstRC);
517 BuildMI(MBB, MI, dl, get(TargetOpcode::COPY), FirstReg)
518 .addReg(OldFirstReg);
519 }
520
521 BuildMI(MBB, MI, dl, get(OpCode), DestReg)
522 .addReg(FirstReg).addReg(SecondReg)
523 .addReg(Cond[1].getReg(), 0, SubIdx);
524}
525
Jakob Stoklund Olesen27689b02010-07-11 07:31:00 +0000526void PPCInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
527 MachineBasicBlock::iterator I, DebugLoc DL,
528 unsigned DestReg, unsigned SrcReg,
529 bool KillSrc) const {
530 unsigned Opc;
531 if (PPC::GPRCRegClass.contains(DestReg, SrcReg))
532 Opc = PPC::OR;
533 else if (PPC::G8RCRegClass.contains(DestReg, SrcReg))
534 Opc = PPC::OR8;
535 else if (PPC::F4RCRegClass.contains(DestReg, SrcReg))
536 Opc = PPC::FMR;
537 else if (PPC::CRRCRegClass.contains(DestReg, SrcReg))
538 Opc = PPC::MCRF;
539 else if (PPC::VRRCRegClass.contains(DestReg, SrcReg))
540 Opc = PPC::VOR;
541 else if (PPC::CRBITRCRegClass.contains(DestReg, SrcReg))
542 Opc = PPC::CROR;
543 else
544 llvm_unreachable("Impossible reg-to-reg copy");
Owen Andersond10fd972007-12-31 06:32:00 +0000545
Evan Chenge837dea2011-06-28 19:10:37 +0000546 const MCInstrDesc &MCID = get(Opc);
547 if (MCID.getNumOperands() == 3)
548 BuildMI(MBB, I, DL, MCID, DestReg)
Jakob Stoklund Olesen27689b02010-07-11 07:31:00 +0000549 .addReg(SrcReg).addReg(SrcReg, getKillRegState(KillSrc));
550 else
Evan Chenge837dea2011-06-28 19:10:37 +0000551 BuildMI(MBB, I, DL, MCID, DestReg).addReg(SrcReg, getKillRegState(KillSrc));
Owen Andersond10fd972007-12-31 06:32:00 +0000552}
553
Hal Finkel3fd00182011-12-05 17:55:17 +0000554// This function returns true if a CR spill is necessary and false otherwise.
Bill Wendling4a66e9a2008-03-10 22:49:16 +0000555bool
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000556PPCInstrInfo::StoreRegToStackSlot(MachineFunction &MF,
557 unsigned SrcReg, bool isKill,
Bill Wendling4a66e9a2008-03-10 22:49:16 +0000558 int FrameIdx,
559 const TargetRegisterClass *RC,
Hal Finkel32497292013-03-17 04:43:44 +0000560 SmallVectorImpl<MachineInstr*> &NewMIs,
Hal Finkel3f2c0472013-03-23 22:06:03 +0000561 bool &NonRI, bool &SpillsVRS) const{
Hal Finkelf25f93b2013-03-27 21:21:15 +0000562 // Note: If additional store instructions are added here,
563 // update isStoreToStackSlot.
564
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000565 DebugLoc DL;
Craig Topperc9099502012-04-20 06:31:50 +0000566 if (PPC::GPRCRegClass.hasSubClassEq(RC)) {
Hal Finkel7257fda2013-03-23 17:14:27 +0000567 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
568 .addReg(SrcReg,
569 getKillRegState(isKill)),
570 FrameIdx));
Craig Topperc9099502012-04-20 06:31:50 +0000571 } else if (PPC::G8RCRegClass.hasSubClassEq(RC)) {
Hal Finkel7257fda2013-03-23 17:14:27 +0000572 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STD))
573 .addReg(SrcReg,
574 getKillRegState(isKill)),
575 FrameIdx));
Craig Topperc9099502012-04-20 06:31:50 +0000576 } else if (PPC::F8RCRegClass.hasSubClassEq(RC)) {
Dale Johannesen21b55412009-02-12 23:08:38 +0000577 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFD))
Bill Wendling587daed2009-05-13 21:33:08 +0000578 .addReg(SrcReg,
579 getKillRegState(isKill)),
580 FrameIdx));
Craig Topperc9099502012-04-20 06:31:50 +0000581 } else if (PPC::F4RCRegClass.hasSubClassEq(RC)) {
Dale Johannesen21b55412009-02-12 23:08:38 +0000582 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFS))
Bill Wendling587daed2009-05-13 21:33:08 +0000583 .addReg(SrcReg,
584 getKillRegState(isKill)),
585 FrameIdx));
Craig Topperc9099502012-04-20 06:31:50 +0000586 } else if (PPC::CRRCRegClass.hasSubClassEq(RC)) {
Hal Finkel7285e8d2013-03-12 14:12:16 +0000587 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CR))
588 .addReg(SrcReg,
589 getKillRegState(isKill)),
590 FrameIdx));
591 return true;
Craig Topperc9099502012-04-20 06:31:50 +0000592 } else if (PPC::CRBITRCRegClass.hasSubClassEq(RC)) {
Nicolas Geoffray0404cd92008-03-10 14:12:10 +0000593 // FIXME: We use CRi here because there is no mtcrf on a bit. Since the
594 // backend currently only uses CR1EQ as an individual bit, this should
595 // not cause any bug. If we need other uses of CR bits, the following
596 // code may be invalid.
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000597 unsigned Reg = 0;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000598 if (SrcReg == PPC::CR0LT || SrcReg == PPC::CR0GT ||
599 SrcReg == PPC::CR0EQ || SrcReg == PPC::CR0UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000600 Reg = PPC::CR0;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000601 else if (SrcReg == PPC::CR1LT || SrcReg == PPC::CR1GT ||
602 SrcReg == PPC::CR1EQ || SrcReg == PPC::CR1UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000603 Reg = PPC::CR1;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000604 else if (SrcReg == PPC::CR2LT || SrcReg == PPC::CR2GT ||
605 SrcReg == PPC::CR2EQ || SrcReg == PPC::CR2UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000606 Reg = PPC::CR2;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000607 else if (SrcReg == PPC::CR3LT || SrcReg == PPC::CR3GT ||
608 SrcReg == PPC::CR3EQ || SrcReg == PPC::CR3UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000609 Reg = PPC::CR3;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000610 else if (SrcReg == PPC::CR4LT || SrcReg == PPC::CR4GT ||
611 SrcReg == PPC::CR4EQ || SrcReg == PPC::CR4UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000612 Reg = PPC::CR4;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000613 else if (SrcReg == PPC::CR5LT || SrcReg == PPC::CR5GT ||
614 SrcReg == PPC::CR5EQ || SrcReg == PPC::CR5UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000615 Reg = PPC::CR5;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000616 else if (SrcReg == PPC::CR6LT || SrcReg == PPC::CR6GT ||
617 SrcReg == PPC::CR6EQ || SrcReg == PPC::CR6UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000618 Reg = PPC::CR6;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000619 else if (SrcReg == PPC::CR7LT || SrcReg == PPC::CR7GT ||
620 SrcReg == PPC::CR7EQ || SrcReg == PPC::CR7UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000621 Reg = PPC::CR7;
622
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000623 return StoreRegToStackSlot(MF, Reg, isKill, FrameIdx,
Hal Finkel3f2c0472013-03-23 22:06:03 +0000624 &PPC::CRRCRegClass, NewMIs, NonRI, SpillsVRS);
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000625
Craig Topperc9099502012-04-20 06:31:50 +0000626 } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) {
Hal Finkel32497292013-03-17 04:43:44 +0000627 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STVX))
628 .addReg(SrcReg,
629 getKillRegState(isKill)),
630 FrameIdx));
631 NonRI = true;
Hal Finkel10f7f2a2013-03-21 19:03:21 +0000632 } else if (PPC::VRSAVERCRegClass.hasSubClassEq(RC)) {
Hal Finkelb7e11e42013-03-27 00:02:20 +0000633 assert(TM.getSubtargetImpl()->isDarwin() &&
634 "VRSAVE only needs spill/restore on Darwin");
Hal Finkel10f7f2a2013-03-21 19:03:21 +0000635 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_VRSAVE))
636 .addReg(SrcReg,
637 getKillRegState(isKill)),
638 FrameIdx));
Hal Finkel3f2c0472013-03-23 22:06:03 +0000639 SpillsVRS = true;
Owen Andersonf6372aa2008-01-01 21:11:32 +0000640 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000641 llvm_unreachable("Unknown regclass!");
Owen Andersonf6372aa2008-01-01 21:11:32 +0000642 }
Bill Wendling7194aaf2008-03-03 22:19:16 +0000643
644 return false;
Owen Andersonf6372aa2008-01-01 21:11:32 +0000645}
646
647void
648PPCInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
Bill Wendling7194aaf2008-03-03 22:19:16 +0000649 MachineBasicBlock::iterator MI,
650 unsigned SrcReg, bool isKill, int FrameIdx,
Evan Cheng746ad692010-05-06 19:06:44 +0000651 const TargetRegisterClass *RC,
652 const TargetRegisterInfo *TRI) const {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000653 MachineFunction &MF = *MBB.getParent();
Owen Andersonf6372aa2008-01-01 21:11:32 +0000654 SmallVector<MachineInstr*, 4> NewMIs;
Bill Wendling7194aaf2008-03-03 22:19:16 +0000655
Hal Finkel0cfb42a2013-03-15 05:06:04 +0000656 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
657 FuncInfo->setHasSpills();
658
Hal Finkel3f2c0472013-03-23 22:06:03 +0000659 bool NonRI = false, SpillsVRS = false;
660 if (StoreRegToStackSlot(MF, SrcReg, isKill, FrameIdx, RC, NewMIs,
661 NonRI, SpillsVRS))
Bill Wendling7194aaf2008-03-03 22:19:16 +0000662 FuncInfo->setSpillsCR();
Bill Wendling7194aaf2008-03-03 22:19:16 +0000663
Hal Finkel3f2c0472013-03-23 22:06:03 +0000664 if (SpillsVRS)
665 FuncInfo->setSpillsVRSAVE();
666
Hal Finkel32497292013-03-17 04:43:44 +0000667 if (NonRI)
668 FuncInfo->setHasNonRISpills();
669
Owen Andersonf6372aa2008-01-01 21:11:32 +0000670 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
671 MBB.insert(MI, NewMIs[i]);
Jakob Stoklund Olesen7a79fcb2010-07-16 18:22:00 +0000672
673 const MachineFrameInfo &MFI = *MF.getFrameInfo();
674 MachineMemOperand *MMO =
Jay Foad978e0df2011-11-15 07:34:52 +0000675 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx),
Chris Lattner59db5492010-09-21 04:39:43 +0000676 MachineMemOperand::MOStore,
Jakob Stoklund Olesen7a79fcb2010-07-16 18:22:00 +0000677 MFI.getObjectSize(FrameIdx),
678 MFI.getObjectAlignment(FrameIdx));
679 NewMIs.back()->addMemOperand(MF, MMO);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000680}
681
Hal Finkeld21e9302011-12-06 20:55:36 +0000682bool
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000683PPCInstrInfo::LoadRegFromStackSlot(MachineFunction &MF, DebugLoc DL,
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000684 unsigned DestReg, int FrameIdx,
Bill Wendling4a66e9a2008-03-10 22:49:16 +0000685 const TargetRegisterClass *RC,
Hal Finkel32497292013-03-17 04:43:44 +0000686 SmallVectorImpl<MachineInstr*> &NewMIs,
Hal Finkel3f2c0472013-03-23 22:06:03 +0000687 bool &NonRI, bool &SpillsVRS) const{
Hal Finkelf25f93b2013-03-27 21:21:15 +0000688 // Note: If additional load instructions are added here,
689 // update isLoadFromStackSlot.
690
Craig Topperc9099502012-04-20 06:31:50 +0000691 if (PPC::GPRCRegClass.hasSubClassEq(RC)) {
Hal Finkelfc805862013-03-27 19:10:40 +0000692 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ),
693 DestReg), FrameIdx));
Craig Topperc9099502012-04-20 06:31:50 +0000694 } else if (PPC::G8RCRegClass.hasSubClassEq(RC)) {
Hal Finkelfc805862013-03-27 19:10:40 +0000695 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LD), DestReg),
696 FrameIdx));
Craig Topperc9099502012-04-20 06:31:50 +0000697 } else if (PPC::F8RCRegClass.hasSubClassEq(RC)) {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000698 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFD), DestReg),
Owen Andersonf6372aa2008-01-01 21:11:32 +0000699 FrameIdx));
Craig Topperc9099502012-04-20 06:31:50 +0000700 } else if (PPC::F4RCRegClass.hasSubClassEq(RC)) {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000701 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFS), DestReg),
Owen Andersonf6372aa2008-01-01 21:11:32 +0000702 FrameIdx));
Craig Topperc9099502012-04-20 06:31:50 +0000703 } else if (PPC::CRRCRegClass.hasSubClassEq(RC)) {
Hal Finkel7285e8d2013-03-12 14:12:16 +0000704 NewMIs.push_back(addFrameReference(BuildMI(MF, DL,
705 get(PPC::RESTORE_CR), DestReg),
706 FrameIdx));
707 return true;
Craig Topperc9099502012-04-20 06:31:50 +0000708 } else if (PPC::CRBITRCRegClass.hasSubClassEq(RC)) {
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000709
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000710 unsigned Reg = 0;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000711 if (DestReg == PPC::CR0LT || DestReg == PPC::CR0GT ||
712 DestReg == PPC::CR0EQ || DestReg == PPC::CR0UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000713 Reg = PPC::CR0;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000714 else if (DestReg == PPC::CR1LT || DestReg == PPC::CR1GT ||
715 DestReg == PPC::CR1EQ || DestReg == PPC::CR1UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000716 Reg = PPC::CR1;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000717 else if (DestReg == PPC::CR2LT || DestReg == PPC::CR2GT ||
718 DestReg == PPC::CR2EQ || DestReg == PPC::CR2UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000719 Reg = PPC::CR2;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000720 else if (DestReg == PPC::CR3LT || DestReg == PPC::CR3GT ||
721 DestReg == PPC::CR3EQ || DestReg == PPC::CR3UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000722 Reg = PPC::CR3;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000723 else if (DestReg == PPC::CR4LT || DestReg == PPC::CR4GT ||
724 DestReg == PPC::CR4EQ || DestReg == PPC::CR4UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000725 Reg = PPC::CR4;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000726 else if (DestReg == PPC::CR5LT || DestReg == PPC::CR5GT ||
727 DestReg == PPC::CR5EQ || DestReg == PPC::CR5UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000728 Reg = PPC::CR5;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000729 else if (DestReg == PPC::CR6LT || DestReg == PPC::CR6GT ||
730 DestReg == PPC::CR6EQ || DestReg == PPC::CR6UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000731 Reg = PPC::CR6;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000732 else if (DestReg == PPC::CR7LT || DestReg == PPC::CR7GT ||
733 DestReg == PPC::CR7EQ || DestReg == PPC::CR7UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000734 Reg = PPC::CR7;
735
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000736 return LoadRegFromStackSlot(MF, DL, Reg, FrameIdx,
Hal Finkel3f2c0472013-03-23 22:06:03 +0000737 &PPC::CRRCRegClass, NewMIs, NonRI, SpillsVRS);
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000738
Craig Topperc9099502012-04-20 06:31:50 +0000739 } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) {
Hal Finkel32497292013-03-17 04:43:44 +0000740 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LVX), DestReg),
741 FrameIdx));
742 NonRI = true;
Hal Finkel10f7f2a2013-03-21 19:03:21 +0000743 } else if (PPC::VRSAVERCRegClass.hasSubClassEq(RC)) {
Hal Finkelb7e11e42013-03-27 00:02:20 +0000744 assert(TM.getSubtargetImpl()->isDarwin() &&
745 "VRSAVE only needs spill/restore on Darwin");
Hal Finkel10f7f2a2013-03-21 19:03:21 +0000746 NewMIs.push_back(addFrameReference(BuildMI(MF, DL,
747 get(PPC::RESTORE_VRSAVE),
748 DestReg),
749 FrameIdx));
Hal Finkel3f2c0472013-03-23 22:06:03 +0000750 SpillsVRS = true;
Owen Andersonf6372aa2008-01-01 21:11:32 +0000751 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000752 llvm_unreachable("Unknown regclass!");
Owen Andersonf6372aa2008-01-01 21:11:32 +0000753 }
Hal Finkeld21e9302011-12-06 20:55:36 +0000754
755 return false;
Owen Andersonf6372aa2008-01-01 21:11:32 +0000756}
757
758void
759PPCInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
Bill Wendling7194aaf2008-03-03 22:19:16 +0000760 MachineBasicBlock::iterator MI,
761 unsigned DestReg, int FrameIdx,
Evan Cheng746ad692010-05-06 19:06:44 +0000762 const TargetRegisterClass *RC,
763 const TargetRegisterInfo *TRI) const {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000764 MachineFunction &MF = *MBB.getParent();
Owen Andersonf6372aa2008-01-01 21:11:32 +0000765 SmallVector<MachineInstr*, 4> NewMIs;
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000766 DebugLoc DL;
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000767 if (MI != MBB.end()) DL = MI->getDebugLoc();
Hal Finkel32497292013-03-17 04:43:44 +0000768
769 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
770 FuncInfo->setHasSpills();
771
Hal Finkel3f2c0472013-03-23 22:06:03 +0000772 bool NonRI = false, SpillsVRS = false;
773 if (LoadRegFromStackSlot(MF, DL, DestReg, FrameIdx, RC, NewMIs,
774 NonRI, SpillsVRS))
Hal Finkeld21e9302011-12-06 20:55:36 +0000775 FuncInfo->setSpillsCR();
Hal Finkel32497292013-03-17 04:43:44 +0000776
Hal Finkel3f2c0472013-03-23 22:06:03 +0000777 if (SpillsVRS)
778 FuncInfo->setSpillsVRSAVE();
779
Hal Finkel32497292013-03-17 04:43:44 +0000780 if (NonRI)
781 FuncInfo->setHasNonRISpills();
782
Owen Andersonf6372aa2008-01-01 21:11:32 +0000783 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
784 MBB.insert(MI, NewMIs[i]);
Jakob Stoklund Olesen7a79fcb2010-07-16 18:22:00 +0000785
786 const MachineFrameInfo &MFI = *MF.getFrameInfo();
787 MachineMemOperand *MMO =
Jay Foad978e0df2011-11-15 07:34:52 +0000788 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx),
Chris Lattner59db5492010-09-21 04:39:43 +0000789 MachineMemOperand::MOLoad,
Jakob Stoklund Olesen7a79fcb2010-07-16 18:22:00 +0000790 MFI.getObjectSize(FrameIdx),
791 MFI.getObjectAlignment(FrameIdx));
792 NewMIs.back()->addMemOperand(MF, MMO);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000793}
794
Evan Cheng09652172010-04-26 07:39:36 +0000795MachineInstr*
796PPCInstrInfo::emitFrameIndexDebugValue(MachineFunction &MF,
Evan Cheng8601a3d2010-04-29 01:13:30 +0000797 int FrameIx, uint64_t Offset,
Evan Cheng09652172010-04-26 07:39:36 +0000798 const MDNode *MDPtr,
799 DebugLoc DL) const {
800 MachineInstrBuilder MIB = BuildMI(MF, DL, get(PPC::DBG_VALUE));
801 addFrameReference(MIB, FrameIx, 0, false).addImm(Offset).addMetadata(MDPtr);
802 return &*MIB;
803}
804
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000805bool PPCInstrInfo::
Owen Anderson44eb65c2008-08-14 22:49:33 +0000806ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
Chris Lattner7c4fe252006-10-21 06:03:11 +0000807 assert(Cond.size() == 2 && "Invalid PPC branch opcode!");
Hal Finkel99f823f2012-06-08 15:38:21 +0000808 if (Cond[1].getReg() == PPC::CTR8 || Cond[1].getReg() == PPC::CTR)
809 Cond[0].setImm(Cond[0].getImm() == 0 ? 1 : 0);
810 else
811 // Leave the CR# the same, but invert the condition.
812 Cond[0].setImm(PPC::InvertPredicate((PPC::Predicate)Cond[0].getImm()));
Chris Lattner7c4fe252006-10-21 06:03:11 +0000813 return false;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000814}
Nicolas Geoffray52e724a2008-04-16 20:10:13 +0000815
Hal Finkel839b9092013-04-06 19:30:30 +0000816bool PPCInstrInfo::FoldImmediate(MachineInstr *UseMI, MachineInstr *DefMI,
817 unsigned Reg, MachineRegisterInfo *MRI) const {
818 // For some instructions, it is legal to fold ZERO into the RA register field.
819 // A zero immediate should always be loaded with a single li.
820 unsigned DefOpc = DefMI->getOpcode();
821 if (DefOpc != PPC::LI && DefOpc != PPC::LI8)
822 return false;
823 if (!DefMI->getOperand(1).isImm())
824 return false;
825 if (DefMI->getOperand(1).getImm() != 0)
826 return false;
827
828 // Note that we cannot here invert the arguments of an isel in order to fold
829 // a ZERO into what is presented as the second argument. All we have here
830 // is the condition bit, and that might come from a CR-logical bit operation.
831
832 const MCInstrDesc &UseMCID = UseMI->getDesc();
833
834 // Only fold into real machine instructions.
835 if (UseMCID.isPseudo())
836 return false;
837
838 unsigned UseIdx;
839 for (UseIdx = 0; UseIdx < UseMI->getNumOperands(); ++UseIdx)
840 if (UseMI->getOperand(UseIdx).isReg() &&
841 UseMI->getOperand(UseIdx).getReg() == Reg)
842 break;
843
844 assert(UseIdx < UseMI->getNumOperands() && "Cannot find Reg in UseMI");
845 assert(UseIdx < UseMCID.getNumOperands() && "No operand description for Reg");
846
847 const MCOperandInfo *UseInfo = &UseMCID.OpInfo[UseIdx];
848
849 // We can fold the zero if this register requires a GPRC_NOR0/G8RC_NOX0
850 // register (which might also be specified as a pointer class kind).
851 if (UseInfo->isLookupPtrRegClass()) {
852 if (UseInfo->RegClass /* Kind */ != 1)
853 return false;
854 } else {
855 if (UseInfo->RegClass != PPC::GPRC_NOR0RegClassID &&
856 UseInfo->RegClass != PPC::G8RC_NOX0RegClassID)
857 return false;
858 }
859
860 // Make sure this is not tied to an output register (or otherwise
861 // constrained). This is true for ST?UX registers, for example, which
862 // are tied to their output registers.
863 if (UseInfo->Constraints != 0)
864 return false;
865
866 unsigned ZeroReg;
867 if (UseInfo->isLookupPtrRegClass()) {
868 bool isPPC64 = TM.getSubtargetImpl()->isPPC64();
869 ZeroReg = isPPC64 ? PPC::ZERO8 : PPC::ZERO;
870 } else {
871 ZeroReg = UseInfo->RegClass == PPC::G8RC_NOX0RegClassID ?
872 PPC::ZERO8 : PPC::ZERO;
873 }
874
875 bool DeleteDef = MRI->hasOneNonDBGUse(Reg);
876 UseMI->getOperand(UseIdx).setReg(ZeroReg);
877
878 if (DeleteDef)
879 DefMI->eraseFromParent();
880
881 return true;
882}
883
Hal Finkelda47e172013-04-10 18:30:16 +0000884static bool MBBDefinesCTR(MachineBasicBlock &MBB) {
885 for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end();
886 I != IE; ++I)
887 if (I->definesRegister(PPC::CTR) || I->definesRegister(PPC::CTR8))
888 return true;
889 return false;
890}
891
892// We should make sure that, if we're going to predicate both sides of a
893// condition (a diamond), that both sides don't define the counter register. We
894// can predicate counter-decrement-based branches, but while that predicates
895// the branching, it does not predicate the counter decrement. If we tried to
896// merge the triangle into one predicated block, we'd decrement the counter
897// twice.
898bool PPCInstrInfo::isProfitableToIfCvt(MachineBasicBlock &TMBB,
899 unsigned NumT, unsigned ExtraT,
900 MachineBasicBlock &FMBB,
901 unsigned NumF, unsigned ExtraF,
902 const BranchProbability &Probability) const {
903 return !(MBBDefinesCTR(TMBB) && MBBDefinesCTR(FMBB));
904}
905
906
Hal Finkel7eb0d812013-04-09 22:58:37 +0000907bool PPCInstrInfo::isPredicated(const MachineInstr *MI) const {
Hal Finkel4b040292013-04-11 01:23:34 +0000908 // The predicated branches are identified by their type, not really by the
909 // explicit presence of a predicate. Furthermore, some of them can be
910 // predicated more than once. Because if conversion won't try to predicate
911 // any instruction which already claims to be predicated (by returning true
912 // here), always return false. In doing so, we let isPredicable() be the
913 // final word on whether not the instruction can be (further) predicated.
914
915 return false;
Hal Finkel7eb0d812013-04-09 22:58:37 +0000916}
917
918bool PPCInstrInfo::isUnpredicatedTerminator(const MachineInstr *MI) const {
919 if (!MI->isTerminator())
920 return false;
921
922 // Conditional branch is a special case.
923 if (MI->isBranch() && !MI->isBarrier())
924 return true;
925
926 return !isPredicated(MI);
927}
928
929bool PPCInstrInfo::PredicateInstruction(
930 MachineInstr *MI,
931 const SmallVectorImpl<MachineOperand> &Pred) const {
932 unsigned OpC = MI->getOpcode();
933 if (OpC == PPC::BLR) {
934 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) {
935 bool isPPC64 = TM.getSubtargetImpl()->isPPC64();
936 MI->setDesc(get(Pred[0].getImm() ?
937 (isPPC64 ? PPC::BDNZLR8 : PPC::BDNZLR) :
938 (isPPC64 ? PPC::BDZLR8 : PPC::BDZLR)));
939 } else {
940 MI->setDesc(get(PPC::BCLR));
941 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
942 .addImm(Pred[0].getImm())
943 .addReg(Pred[1].getReg());
944 }
945
946 return true;
947 } else if (OpC == PPC::B) {
948 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) {
949 bool isPPC64 = TM.getSubtargetImpl()->isPPC64();
950 MI->setDesc(get(Pred[0].getImm() ?
951 (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
952 (isPPC64 ? PPC::BDZ8 : PPC::BDZ)));
953 } else {
954 MachineBasicBlock *MBB = MI->getOperand(0).getMBB();
955 MI->RemoveOperand(0);
956
957 MI->setDesc(get(PPC::BCC));
958 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
959 .addImm(Pred[0].getImm())
960 .addReg(Pred[1].getReg())
961 .addMBB(MBB);
962 }
963
964 return true;
Hal Finkel90dd7fd2013-04-10 06:42:34 +0000965 } else if (OpC == PPC::BCTR || OpC == PPC::BCTR8 ||
966 OpC == PPC::BCTRL || OpC == PPC::BCTRL8) {
967 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR)
968 llvm_unreachable("Cannot predicate bctr[l] on the ctr register");
969
970 bool setLR = OpC == PPC::BCTRL || OpC == PPC::BCTRL8;
971 bool isPPC64 = TM.getSubtargetImpl()->isPPC64();
972 MI->setDesc(get(isPPC64 ? (setLR ? PPC::BCCTRL8 : PPC::BCCTR8) :
973 (setLR ? PPC::BCCTRL : PPC::BCCTR)));
974 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
975 .addImm(Pred[0].getImm())
976 .addReg(Pred[1].getReg());
977 return true;
Hal Finkel7eb0d812013-04-09 22:58:37 +0000978 }
979
980 return false;
981}
982
983bool PPCInstrInfo::SubsumesPredicate(
984 const SmallVectorImpl<MachineOperand> &Pred1,
985 const SmallVectorImpl<MachineOperand> &Pred2) const {
986 assert(Pred1.size() == 2 && "Invalid PPC first predicate");
987 assert(Pred2.size() == 2 && "Invalid PPC second predicate");
988
989 if (Pred1[1].getReg() == PPC::CTR8 || Pred1[1].getReg() == PPC::CTR)
990 return false;
991 if (Pred2[1].getReg() == PPC::CTR8 || Pred2[1].getReg() == PPC::CTR)
992 return false;
993
994 PPC::Predicate P1 = (PPC::Predicate) Pred1[0].getImm();
995 PPC::Predicate P2 = (PPC::Predicate) Pred2[0].getImm();
996
997 if (P1 == P2)
998 return true;
999
1000 // Does P1 subsume P2, e.g. GE subsumes GT.
1001 if (P1 == PPC::PRED_LE &&
1002 (P2 == PPC::PRED_LT || P2 == PPC::PRED_EQ))
1003 return true;
1004 if (P1 == PPC::PRED_GE &&
1005 (P2 == PPC::PRED_GT || P2 == PPC::PRED_EQ))
1006 return true;
1007
1008 return false;
1009}
1010
1011bool PPCInstrInfo::DefinesPredicate(MachineInstr *MI,
1012 std::vector<MachineOperand> &Pred) const {
1013 // Note: At the present time, the contents of Pred from this function is
1014 // unused by IfConversion. This implementation follows ARM by pushing the
1015 // CR-defining operand. Because the 'DZ' and 'DNZ' count as types of
1016 // predicate, instructions defining CTR or CTR8 are also included as
1017 // predicate-defining instructions.
1018
1019 const TargetRegisterClass *RCs[] =
1020 { &PPC::CRRCRegClass, &PPC::CRBITRCRegClass,
1021 &PPC::CTRRCRegClass, &PPC::CTRRC8RegClass };
1022
1023 bool Found = false;
1024 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1025 const MachineOperand &MO = MI->getOperand(i);
Hal Finkel4e317282013-04-10 07:17:47 +00001026 for (unsigned c = 0; c < array_lengthof(RCs) && !Found; ++c) {
Hal Finkel7eb0d812013-04-09 22:58:37 +00001027 const TargetRegisterClass *RC = RCs[c];
Hal Finkel4e317282013-04-10 07:17:47 +00001028 if (MO.isReg()) {
1029 if (MO.isDef() && RC->contains(MO.getReg())) {
Hal Finkel7eb0d812013-04-09 22:58:37 +00001030 Pred.push_back(MO);
1031 Found = true;
1032 }
Hal Finkel4e317282013-04-10 07:17:47 +00001033 } else if (MO.isRegMask()) {
1034 for (TargetRegisterClass::iterator I = RC->begin(),
1035 IE = RC->end(); I != IE; ++I)
1036 if (MO.clobbersPhysReg(*I)) {
1037 Pred.push_back(MO);
1038 Found = true;
1039 }
Hal Finkel7eb0d812013-04-09 22:58:37 +00001040 }
1041 }
1042 }
1043
1044 return Found;
1045}
1046
1047bool PPCInstrInfo::isPredicable(MachineInstr *MI) const {
1048 unsigned OpC = MI->getOpcode();
1049 switch (OpC) {
1050 default:
1051 return false;
1052 case PPC::B:
1053 case PPC::BLR:
Hal Finkel90dd7fd2013-04-10 06:42:34 +00001054 case PPC::BCTR:
1055 case PPC::BCTR8:
1056 case PPC::BCTRL:
1057 case PPC::BCTRL8:
Hal Finkel7eb0d812013-04-09 22:58:37 +00001058 return true;
1059 }
1060}
1061
Hal Finkel860c08c2013-04-18 22:15:08 +00001062bool PPCInstrInfo::analyzeCompare(const MachineInstr *MI,
1063 unsigned &SrcReg, unsigned &SrcReg2,
1064 int &Mask, int &Value) const {
1065 unsigned Opc = MI->getOpcode();
1066
1067 switch (Opc) {
1068 default: return false;
1069 case PPC::CMPWI:
1070 case PPC::CMPLWI:
1071 case PPC::CMPDI:
1072 case PPC::CMPLDI:
1073 SrcReg = MI->getOperand(1).getReg();
1074 SrcReg2 = 0;
1075 Value = MI->getOperand(2).getImm();
1076 Mask = 0xFFFF;
1077 return true;
1078 case PPC::CMPW:
1079 case PPC::CMPLW:
1080 case PPC::CMPD:
1081 case PPC::CMPLD:
1082 case PPC::FCMPUS:
1083 case PPC::FCMPUD:
1084 SrcReg = MI->getOperand(1).getReg();
1085 SrcReg2 = MI->getOperand(2).getReg();
1086 return true;
1087 }
1088}
Hal Finkel87c1e422013-04-19 22:08:38 +00001089
Hal Finkel860c08c2013-04-18 22:15:08 +00001090bool PPCInstrInfo::optimizeCompareInstr(MachineInstr *CmpInstr,
1091 unsigned SrcReg, unsigned SrcReg2,
1092 int Mask, int Value,
1093 const MachineRegisterInfo *MRI) const {
Hal Finkel4029c3f2013-04-18 22:54:25 +00001094 if (DisableCmpOpt)
1095 return false;
1096
Hal Finkel860c08c2013-04-18 22:15:08 +00001097 int OpC = CmpInstr->getOpcode();
1098 unsigned CRReg = CmpInstr->getOperand(0).getReg();
1099 bool isFP = OpC == PPC::FCMPUS || OpC == PPC::FCMPUD;
1100 unsigned CRRecReg = isFP ? PPC::CR1 : PPC::CR0;
1101
1102 // The record forms set the condition register based on a signed comparison
1103 // with zero (so says the ISA manual). This is not as straightforward as it
1104 // seems, however, because this is always a 64-bit comparison on PPC64, even
1105 // for instructions that are 32-bit in nature (like slw for example).
1106 // So, on PPC32, for unsigned comparisons, we can use the record forms only
1107 // for equality checks (as those don't depend on the sign). On PPC64,
1108 // we are restricted to equality for unsigned 64-bit comparisons and for
1109 // signed 32-bit comparisons the applicability is more restricted.
1110 bool isPPC64 = TM.getSubtargetImpl()->isPPC64();
1111 bool is32BitSignedCompare = OpC == PPC::CMPWI || OpC == PPC::CMPW;
1112 bool is32BitUnsignedCompare = OpC == PPC::CMPLWI || OpC == PPC::CMPLW;
1113 bool is64BitUnsignedCompare = OpC == PPC::CMPLDI || OpC == PPC::CMPLD;
1114
1115 // Get the unique definition of SrcReg.
1116 MachineInstr *MI = MRI->getUniqueVRegDef(SrcReg);
1117 if (!MI) return false;
1118 int MIOpC = MI->getOpcode();
1119
1120 bool equalityOnly = false;
1121 bool noSub = false;
1122 if (isPPC64) {
1123 if (is32BitSignedCompare) {
1124 // We can perform this optimization only if MI is sign-extending.
1125 if (MIOpC == PPC::SRAW || MIOpC == PPC::SRAWo ||
1126 MIOpC == PPC::SRAWI || MIOpC == PPC::SRAWIo ||
1127 MIOpC == PPC::EXTSB || MIOpC == PPC::EXTSBo ||
1128 MIOpC == PPC::EXTSH || MIOpC == PPC::EXTSHo ||
1129 MIOpC == PPC::EXTSW || MIOpC == PPC::EXTSWo) {
1130 noSub = true;
1131 } else
1132 return false;
1133 } else if (is32BitUnsignedCompare) {
1134 // We can perform this optimization, equality only, if MI is
1135 // zero-extending.
1136 if (MIOpC == PPC::CNTLZW || MIOpC == PPC::CNTLZWo ||
1137 MIOpC == PPC::SLW || MIOpC == PPC::SLWo ||
1138 MIOpC == PPC::SRW || MIOpC == PPC::SRWo) {
1139 noSub = true;
1140 equalityOnly = true;
1141 } else
1142 return false;
1143 } else if (!isFP)
1144 equalityOnly = is64BitUnsignedCompare;
1145 } else if (!isFP)
1146 equalityOnly = is32BitUnsignedCompare;
1147
1148 if (equalityOnly) {
1149 // We need to check the uses of the condition register in order to reject
1150 // non-equality comparisons.
1151 for (MachineRegisterInfo::use_iterator I = MRI->use_begin(CRReg),
1152 IE = MRI->use_end(); I != IE; ++I) {
1153 MachineInstr *UseMI = &*I;
1154 if (UseMI->getOpcode() == PPC::BCC) {
1155 unsigned Pred = UseMI->getOperand(0).getImm();
1156 if (Pred == PPC::PRED_EQ || Pred == PPC::PRED_NE)
1157 continue;
1158
1159 return false;
1160 } else if (UseMI->getOpcode() == PPC::ISEL ||
1161 UseMI->getOpcode() == PPC::ISEL8) {
1162 unsigned SubIdx = UseMI->getOperand(3).getSubReg();
1163 if (SubIdx == PPC::sub_eq)
1164 continue;
1165
1166 return false;
1167 } else
1168 return false;
1169 }
1170 }
1171
1172 // Get ready to iterate backward from CmpInstr.
1173 MachineBasicBlock::iterator I = CmpInstr, E = MI,
1174 B = CmpInstr->getParent()->begin();
1175
1176 // Scan forward to find the first use of the compare.
1177 for (MachineBasicBlock::iterator EL = CmpInstr->getParent()->end();
1178 I != EL; ++I) {
1179 bool FoundUse = false;
1180 for (MachineRegisterInfo::use_iterator J = MRI->use_begin(CRReg),
1181 JE = MRI->use_end(); J != JE; ++J)
1182 if (&*J == &*I) {
1183 FoundUse = true;
1184 break;
1185 }
1186
1187 if (FoundUse)
1188 break;
1189 }
1190
1191 // Early exit if we're at the beginning of the BB.
1192 if (I == B) return false;
1193
1194 // There are two possible candidates which can be changed to set CR[01].
1195 // One is MI, the other is a SUB instruction.
1196 // For CMPrr(r1,r2), we are looking for SUB(r1,r2) or SUB(r2,r1).
1197 MachineInstr *Sub = NULL;
1198 if (SrcReg2 != 0)
1199 // MI is not a candidate for CMPrr.
1200 MI = NULL;
1201 // FIXME: Conservatively refuse to convert an instruction which isn't in the
1202 // same BB as the comparison. This is to allow the check below to avoid calls
1203 // (and other explicit clobbers); instead we should really check for these
1204 // more explicitly (in at least a few predecessors).
1205 else if (MI->getParent() != CmpInstr->getParent() || Value != 0) {
1206 // PPC does not have a record-form SUBri.
1207 return false;
1208 }
1209
1210 // Search for Sub.
1211 const TargetRegisterInfo *TRI = &getRegisterInfo();
1212 --I;
1213 for (; I != E && !noSub; --I) {
1214 const MachineInstr &Instr = *I;
1215 unsigned IOpC = Instr.getOpcode();
1216
1217 if (&*I != CmpInstr && (
1218 Instr.modifiesRegister(CRRecReg, TRI) ||
1219 Instr.readsRegister(CRRecReg, TRI)))
1220 // This instruction modifies or uses the record condition register after
1221 // the one we want to change. While we could do this transformation, it
1222 // would likely not be profitable. This transformation removes one
1223 // instruction, and so even forcing RA to generate one move probably
1224 // makes it unprofitable.
1225 return false;
1226
1227 // Check whether CmpInstr can be made redundant by the current instruction.
1228 if ((OpC == PPC::CMPW || OpC == PPC::CMPLW ||
1229 OpC == PPC::CMPD || OpC == PPC::CMPLD) &&
1230 (IOpC == PPC::SUBF || IOpC == PPC::SUBF8) &&
1231 ((Instr.getOperand(1).getReg() == SrcReg &&
1232 Instr.getOperand(2).getReg() == SrcReg2) ||
1233 (Instr.getOperand(1).getReg() == SrcReg2 &&
1234 Instr.getOperand(2).getReg() == SrcReg))) {
1235 Sub = &*I;
1236 break;
1237 }
1238
1239 if (isFP && (IOpC == PPC::FSUB || IOpC == PPC::FSUBS) &&
1240 ((Instr.getOperand(1).getReg() == SrcReg &&
1241 Instr.getOperand(2).getReg() == SrcReg2) ||
1242 (Instr.getOperand(1).getReg() == SrcReg2 &&
1243 Instr.getOperand(2).getReg() == SrcReg))) {
1244 Sub = &*I;
1245 break;
1246 }
1247
1248 if (I == B)
1249 // The 'and' is below the comparison instruction.
1250 return false;
1251 }
1252
1253 // Return false if no candidates exist.
1254 if (!MI && !Sub)
1255 return false;
1256
1257 // The single candidate is called MI.
1258 if (!MI) MI = Sub;
1259
1260 int NewOpC = -1;
1261 MIOpC = MI->getOpcode();
1262 if (MIOpC == PPC::ANDIo || MIOpC == PPC::ANDIo8)
1263 NewOpC = MIOpC;
1264 else {
1265 NewOpC = PPC::getRecordFormOpcode(MIOpC);
1266 if (NewOpC == -1 && PPC::getNonRecordFormOpcode(MIOpC) != -1)
1267 NewOpC = MIOpC;
1268 }
1269
1270 // FIXME: On the non-embedded POWER architectures, only some of the record
1271 // forms are fast, and we should use only the fast ones.
1272
1273 // The defining instruction has a record form (or is already a record
1274 // form). It is possible, however, that we'll need to reverse the condition
1275 // code of the users.
1276 if (NewOpC == -1)
1277 return false;
1278
Hal Finkel87c1e422013-04-19 22:08:38 +00001279 SmallVector<std::pair<MachineOperand*, PPC::Predicate>, 4> PredsToUpdate;
1280 SmallVector<std::pair<MachineOperand*, unsigned>, 4> SubRegsToUpdate;
Hal Finkel860c08c2013-04-18 22:15:08 +00001281
1282 // If we have SUB(r1, r2) and CMP(r2, r1), the condition code based on CMP
1283 // needs to be updated to be based on SUB. Push the condition code
1284 // operands to OperandsToUpdate. If it is safe to remove CmpInstr, the
1285 // condition code of these operands will be modified.
1286 bool ShouldSwap = false;
1287 if (Sub) {
1288 ShouldSwap = SrcReg2 != 0 && Sub->getOperand(1).getReg() == SrcReg2 &&
1289 Sub->getOperand(2).getReg() == SrcReg;
1290
1291 // The operands to subf are the opposite of sub, so only in the fixed-point
1292 // case, invert the order.
1293 if (!isFP)
1294 ShouldSwap = !ShouldSwap;
1295 }
1296
1297 if (ShouldSwap)
1298 for (MachineRegisterInfo::use_iterator I = MRI->use_begin(CRReg),
1299 IE = MRI->use_end(); I != IE; ++I) {
1300 MachineInstr *UseMI = &*I;
1301 if (UseMI->getOpcode() == PPC::BCC) {
1302 PPC::Predicate Pred = (PPC::Predicate) UseMI->getOperand(0).getImm();
Hal Finkel87c1e422013-04-19 22:08:38 +00001303 assert((!equalityOnly ||
1304 Pred == PPC::PRED_EQ || Pred == PPC::PRED_NE) &&
1305 "Invalid predicate for equality-only optimization");
1306 PredsToUpdate.push_back(std::make_pair(&((*I).getOperand(0)),
Hal Finkelabe64dc2013-04-20 05:16:26 +00001307 PPC::getSwappedPredicate(Pred)));
Hal Finkel860c08c2013-04-18 22:15:08 +00001308 } else if (UseMI->getOpcode() == PPC::ISEL ||
1309 UseMI->getOpcode() == PPC::ISEL8) {
Hal Finkel87c1e422013-04-19 22:08:38 +00001310 unsigned NewSubReg = UseMI->getOperand(3).getSubReg();
1311 assert((!equalityOnly || NewSubReg == PPC::sub_eq) &&
1312 "Invalid CR bit for equality-only optimization");
1313
1314 if (NewSubReg == PPC::sub_lt)
1315 NewSubReg = PPC::sub_gt;
1316 else if (NewSubReg == PPC::sub_gt)
1317 NewSubReg = PPC::sub_lt;
1318
1319 SubRegsToUpdate.push_back(std::make_pair(&((*I).getOperand(3)),
1320 NewSubReg));
Hal Finkel860c08c2013-04-18 22:15:08 +00001321 } else // We need to abort on a user we don't understand.
1322 return false;
1323 }
1324
1325 // Create a new virtual register to hold the value of the CR set by the
1326 // record-form instruction. If the instruction was not previously in
1327 // record form, then set the kill flag on the CR.
1328 CmpInstr->eraseFromParent();
1329
1330 MachineBasicBlock::iterator MII = MI;
1331 BuildMI(*MI->getParent(), llvm::next(MII), MI->getDebugLoc(),
1332 get(TargetOpcode::COPY), CRReg)
1333 .addReg(CRRecReg, MIOpC != NewOpC ? RegState::Kill : 0);
1334
1335 if (MIOpC != NewOpC) {
1336 // We need to be careful here: we're replacing one instruction with
1337 // another, and we need to make sure that we get all of the right
1338 // implicit uses and defs. On the other hand, the caller may be holding
1339 // an iterator to this instruction, and so we can't delete it (this is
1340 // specifically the case if this is the instruction directly after the
1341 // compare).
1342
1343 const MCInstrDesc &NewDesc = get(NewOpC);
1344 MI->setDesc(NewDesc);
1345
1346 if (NewDesc.ImplicitDefs)
1347 for (const uint16_t *ImpDefs = NewDesc.getImplicitDefs();
1348 *ImpDefs; ++ImpDefs)
1349 if (!MI->definesRegister(*ImpDefs))
1350 MI->addOperand(*MI->getParent()->getParent(),
1351 MachineOperand::CreateReg(*ImpDefs, true, true));
1352 if (NewDesc.ImplicitUses)
1353 for (const uint16_t *ImpUses = NewDesc.getImplicitUses();
1354 *ImpUses; ++ImpUses)
1355 if (!MI->readsRegister(*ImpUses))
1356 MI->addOperand(*MI->getParent()->getParent(),
1357 MachineOperand::CreateReg(*ImpUses, false, true));
1358 }
1359
1360 // Modify the condition code of operands in OperandsToUpdate.
1361 // Since we have SUB(r1, r2) and CMP(r2, r1), the condition code needs to
1362 // be changed from r2 > r1 to r1 < r2, from r2 < r1 to r1 > r2, etc.
Hal Finkel87c1e422013-04-19 22:08:38 +00001363 for (unsigned i = 0, e = PredsToUpdate.size(); i < e; i++)
1364 PredsToUpdate[i].first->setImm(PredsToUpdate[i].second);
Hal Finkel860c08c2013-04-18 22:15:08 +00001365
Hal Finkel87c1e422013-04-19 22:08:38 +00001366 for (unsigned i = 0, e = SubRegsToUpdate.size(); i < e; i++)
1367 SubRegsToUpdate[i].first->setSubReg(SubRegsToUpdate[i].second);
Hal Finkel860c08c2013-04-18 22:15:08 +00001368
1369 return true;
1370}
1371
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00001372/// GetInstSize - Return the number of bytes of code the specified
1373/// instruction may be. This returns the maximum number of bytes.
1374///
1375unsigned PPCInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
1376 switch (MI->getOpcode()) {
1377 case PPC::INLINEASM: { // Inline Asm: Variable size.
1378 const MachineFunction *MF = MI->getParent()->getParent();
1379 const char *AsmStr = MI->getOperand(0).getSymbolName();
Chris Lattneraf76e592009-08-22 20:48:53 +00001380 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00001381 }
Bill Wendling7431bea2010-07-16 22:20:36 +00001382 case PPC::PROLOG_LABEL:
Dan Gohman44066042008-07-01 00:05:16 +00001383 case PPC::EH_LABEL:
1384 case PPC::GC_LABEL:
Dale Johannesen375be772010-04-07 19:51:44 +00001385 case PPC::DBG_VALUE:
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00001386 return 0;
Ulrich Weigand86765fb2013-03-22 15:24:13 +00001387 case PPC::BL8_NOP:
1388 case PPC::BLA8_NOP:
Hal Finkel5b00cea2012-03-31 14:45:15 +00001389 return 8;
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00001390 default:
1391 return 4; // PowerPC instructions are all 4 bytes
1392 }
1393}
Hal Finkel5ee67e82013-04-08 16:24:03 +00001394
1395#undef DEBUG_TYPE
1396#define DEBUG_TYPE "ppc-early-ret"
1397STATISTIC(NumBCLR, "Number of early conditional returns");
1398STATISTIC(NumBLR, "Number of early returns");
1399
1400namespace llvm {
1401 void initializePPCEarlyReturnPass(PassRegistry&);
1402}
1403
1404namespace {
1405 // PPCEarlyReturn pass - For simple functions without epilogue code, move
1406 // returns up, and create conditional returns, to avoid unnecessary
1407 // branch-to-blr sequences.
1408 struct PPCEarlyReturn : public MachineFunctionPass {
1409 static char ID;
1410 PPCEarlyReturn() : MachineFunctionPass(ID) {
1411 initializePPCEarlyReturnPass(*PassRegistry::getPassRegistry());
1412 }
1413
1414 const PPCTargetMachine *TM;
1415 const PPCInstrInfo *TII;
1416
1417protected:
Hal Finkel13049ae2013-04-09 18:25:18 +00001418 bool processBlock(MachineBasicBlock &ReturnMBB) {
Hal Finkel5ee67e82013-04-08 16:24:03 +00001419 bool Changed = false;
1420
Hal Finkel13049ae2013-04-09 18:25:18 +00001421 MachineBasicBlock::iterator I = ReturnMBB.begin();
1422 I = ReturnMBB.SkipPHIsAndLabels(I);
Hal Finkel5ee67e82013-04-08 16:24:03 +00001423
1424 // The block must be essentially empty except for the blr.
Hal Finkel13049ae2013-04-09 18:25:18 +00001425 if (I == ReturnMBB.end() || I->getOpcode() != PPC::BLR ||
1426 I != ReturnMBB.getLastNonDebugInstr())
Hal Finkel5ee67e82013-04-08 16:24:03 +00001427 return Changed;
1428
1429 SmallVector<MachineBasicBlock*, 8> PredToRemove;
Hal Finkel13049ae2013-04-09 18:25:18 +00001430 for (MachineBasicBlock::pred_iterator PI = ReturnMBB.pred_begin(),
1431 PIE = ReturnMBB.pred_end(); PI != PIE; ++PI) {
Hal Finkel5ee67e82013-04-08 16:24:03 +00001432 bool OtherReference = false, BlockChanged = false;
Hal Finkel13049ae2013-04-09 18:25:18 +00001433 for (MachineBasicBlock::iterator J = (*PI)->getLastNonDebugInstr();;) {
Hal Finkel5ee67e82013-04-08 16:24:03 +00001434 if (J->getOpcode() == PPC::B) {
Hal Finkel13049ae2013-04-09 18:25:18 +00001435 if (J->getOperand(0).getMBB() == &ReturnMBB) {
Hal Finkel5ee67e82013-04-08 16:24:03 +00001436 // This is an unconditional branch to the return. Replace the
1437 // branch with a blr.
1438 BuildMI(**PI, J, J->getDebugLoc(), TII->get(PPC::BLR));
Hal Finkel13049ae2013-04-09 18:25:18 +00001439 MachineBasicBlock::iterator K = J--;
Hal Finkel5ee67e82013-04-08 16:24:03 +00001440 K->eraseFromParent();
1441 BlockChanged = true;
1442 ++NumBLR;
1443 continue;
1444 }
1445 } else if (J->getOpcode() == PPC::BCC) {
Hal Finkel13049ae2013-04-09 18:25:18 +00001446 if (J->getOperand(2).getMBB() == &ReturnMBB) {
Hal Finkel5ee67e82013-04-08 16:24:03 +00001447 // This is a conditional branch to the return. Replace the branch
1448 // with a bclr.
1449 BuildMI(**PI, J, J->getDebugLoc(), TII->get(PPC::BCLR))
1450 .addImm(J->getOperand(0).getImm())
1451 .addReg(J->getOperand(1).getReg());
Hal Finkel13049ae2013-04-09 18:25:18 +00001452 MachineBasicBlock::iterator K = J--;
Hal Finkel5ee67e82013-04-08 16:24:03 +00001453 K->eraseFromParent();
1454 BlockChanged = true;
1455 ++NumBCLR;
1456 continue;
1457 }
1458 } else if (J->isBranch()) {
1459 if (J->isIndirectBranch()) {
Hal Finkel13049ae2013-04-09 18:25:18 +00001460 if (ReturnMBB.hasAddressTaken())
Hal Finkel5ee67e82013-04-08 16:24:03 +00001461 OtherReference = true;
1462 } else
1463 for (unsigned i = 0; i < J->getNumOperands(); ++i)
1464 if (J->getOperand(i).isMBB() &&
Hal Finkel13049ae2013-04-09 18:25:18 +00001465 J->getOperand(i).getMBB() == &ReturnMBB)
Hal Finkel5ee67e82013-04-08 16:24:03 +00001466 OtherReference = true;
Hal Finkel13049ae2013-04-09 18:25:18 +00001467 } else if (!J->isTerminator() && !J->isDebugValue())
1468 break;
Hal Finkel5ee67e82013-04-08 16:24:03 +00001469
Hal Finkel13049ae2013-04-09 18:25:18 +00001470 if (J == (*PI)->begin())
1471 break;
1472
1473 --J;
Hal Finkel5ee67e82013-04-08 16:24:03 +00001474 }
1475
Hal Finkel13049ae2013-04-09 18:25:18 +00001476 if ((*PI)->canFallThrough() && (*PI)->isLayoutSuccessor(&ReturnMBB))
Hal Finkel5ee67e82013-04-08 16:24:03 +00001477 OtherReference = true;
1478
1479 // Predecessors are stored in a vector and can't be removed here.
1480 if (!OtherReference && BlockChanged) {
1481 PredToRemove.push_back(*PI);
1482 }
1483
1484 if (BlockChanged)
1485 Changed = true;
1486 }
1487
1488 for (unsigned i = 0, ie = PredToRemove.size(); i != ie; ++i)
Hal Finkel13049ae2013-04-09 18:25:18 +00001489 PredToRemove[i]->removeSuccessor(&ReturnMBB);
Hal Finkel5ee67e82013-04-08 16:24:03 +00001490
Hal Finkel13049ae2013-04-09 18:25:18 +00001491 if (Changed && !ReturnMBB.hasAddressTaken()) {
Hal Finkel5ee67e82013-04-08 16:24:03 +00001492 // We now might be able to merge this blr-only block into its
1493 // by-layout predecessor.
Hal Finkel13049ae2013-04-09 18:25:18 +00001494 if (ReturnMBB.pred_size() == 1 &&
1495 (*ReturnMBB.pred_begin())->isLayoutSuccessor(&ReturnMBB)) {
Hal Finkel5ee67e82013-04-08 16:24:03 +00001496 // Move the blr into the preceding block.
Hal Finkel13049ae2013-04-09 18:25:18 +00001497 MachineBasicBlock &PrevMBB = **ReturnMBB.pred_begin();
1498 PrevMBB.splice(PrevMBB.end(), &ReturnMBB, I);
1499 PrevMBB.removeSuccessor(&ReturnMBB);
Hal Finkel5ee67e82013-04-08 16:24:03 +00001500 }
1501
Hal Finkel13049ae2013-04-09 18:25:18 +00001502 if (ReturnMBB.pred_empty())
1503 ReturnMBB.eraseFromParent();
Hal Finkel5ee67e82013-04-08 16:24:03 +00001504 }
1505
1506 return Changed;
1507 }
1508
1509public:
1510 virtual bool runOnMachineFunction(MachineFunction &MF) {
1511 TM = static_cast<const PPCTargetMachine *>(&MF.getTarget());
1512 TII = TM->getInstrInfo();
1513
1514 bool Changed = false;
1515
Hal Finkel13049ae2013-04-09 18:25:18 +00001516 // If the function does not have at least two blocks, then there is
Hal Finkel5ee67e82013-04-08 16:24:03 +00001517 // nothing to do.
1518 if (MF.size() < 2)
1519 return Changed;
1520
1521 for (MachineFunction::iterator I = MF.begin(); I != MF.end();) {
1522 MachineBasicBlock &B = *I++;
1523 if (processBlock(B))
1524 Changed = true;
1525 }
1526
1527 return Changed;
1528 }
1529
1530 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
1531 MachineFunctionPass::getAnalysisUsage(AU);
1532 }
1533 };
1534}
1535
1536INITIALIZE_PASS(PPCEarlyReturn, DEBUG_TYPE,
1537 "PowerPC Early-Return Creation", false, false)
1538
1539char PPCEarlyReturn::ID = 0;
1540FunctionPass*
1541llvm::createPPCEarlyReturnPass() { return new PPCEarlyReturn(); }
1542