blob: 375daee59e9cae7a19a12b0ba7684f0d73ffde0e [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),
Bill Wendling80ada582013-06-07 07:55:53 +000050 TM(tm), RI(*TM.getSubtargetImpl()) {}
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) {
Benjamin Kramer041399a2013-06-07 11:23:35 +000077 assert(TM.getInstrInfo() && "No InstrInfo?");
Hal Finkel64c34e22011-12-02 04:58:02 +000078
Bill Wendling80ada582013-06-07 07:55:53 +000079 return new PPCHazardRecognizer970(TM);
Hal Finkel64c34e22011-12-02 04:58:02 +000080 }
81
Hal Finkel4d989ac2012-04-01 19:22:40 +000082 return new PPCScoreboardHazardRecognizer(II, DAG);
Hal Finkel64c34e22011-12-02 04:58:02 +000083}
Jakob Stoklund Olesen71642882012-06-19 21:14:34 +000084
85// Detect 32 -> 64-bit extensions where we may reuse the low sub-register.
86bool PPCInstrInfo::isCoalescableExtInstr(const MachineInstr &MI,
87 unsigned &SrcReg, unsigned &DstReg,
88 unsigned &SubIdx) const {
89 switch (MI.getOpcode()) {
90 default: return false;
91 case PPC::EXTSW:
92 case PPC::EXTSW_32_64:
93 SrcReg = MI.getOperand(1).getReg();
94 DstReg = MI.getOperand(0).getReg();
95 SubIdx = PPC::sub_32;
96 return true;
97 }
98}
99
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000100unsigned PPCInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
Chris Lattner9c09c9e2006-03-16 22:24:02 +0000101 int &FrameIndex) const {
Hal Finkelf25f93b2013-03-27 21:21:15 +0000102 // Note: This list must be kept consistent with LoadRegFromStackSlot.
Chris Lattner40839602006-02-02 20:12:32 +0000103 switch (MI->getOpcode()) {
104 default: break;
105 case PPC::LD:
106 case PPC::LWZ:
107 case PPC::LFS:
108 case PPC::LFD:
Hal Finkelf25f93b2013-03-27 21:21:15 +0000109 case PPC::RESTORE_CR:
110 case PPC::LVX:
111 case PPC::RESTORE_VRSAVE:
112 // Check for the operands added by addFrameReference (the immediate is the
113 // offset which defaults to 0).
Dan Gohmand735b802008-10-03 15:45:36 +0000114 if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
115 MI->getOperand(2).isFI()) {
Chris Lattner8aa797a2007-12-30 23:10:15 +0000116 FrameIndex = MI->getOperand(2).getIndex();
Chris Lattner40839602006-02-02 20:12:32 +0000117 return MI->getOperand(0).getReg();
118 }
119 break;
120 }
121 return 0;
Chris Lattner65242872006-02-02 20:16:12 +0000122}
Chris Lattner40839602006-02-02 20:12:32 +0000123
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000124unsigned PPCInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
Chris Lattner65242872006-02-02 20:16:12 +0000125 int &FrameIndex) const {
Hal Finkelf25f93b2013-03-27 21:21:15 +0000126 // Note: This list must be kept consistent with StoreRegToStackSlot.
Chris Lattner65242872006-02-02 20:16:12 +0000127 switch (MI->getOpcode()) {
128 default: break;
Nate Begeman3b478b32006-02-02 21:07:50 +0000129 case PPC::STD:
Chris Lattner65242872006-02-02 20:16:12 +0000130 case PPC::STW:
131 case PPC::STFS:
132 case PPC::STFD:
Hal Finkelf25f93b2013-03-27 21:21:15 +0000133 case PPC::SPILL_CR:
134 case PPC::STVX:
135 case PPC::SPILL_VRSAVE:
136 // Check for the operands added by addFrameReference (the immediate is the
137 // offset which defaults to 0).
Dan Gohmand735b802008-10-03 15:45:36 +0000138 if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
139 MI->getOperand(2).isFI()) {
Chris Lattner8aa797a2007-12-30 23:10:15 +0000140 FrameIndex = MI->getOperand(2).getIndex();
Chris Lattner65242872006-02-02 20:16:12 +0000141 return MI->getOperand(0).getReg();
142 }
143 break;
144 }
145 return 0;
146}
Chris Lattner40839602006-02-02 20:12:32 +0000147
Chris Lattner043870d2005-09-09 18:17:41 +0000148// commuteInstruction - We can commute rlwimi instructions, but only if the
149// rotate amt is zero. We also have to munge the immediates a bit.
Evan Cheng58dcb0e2008-06-16 07:33:11 +0000150MachineInstr *
151PPCInstrInfo::commuteInstruction(MachineInstr *MI, bool NewMI) const {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000152 MachineFunction &MF = *MI->getParent()->getParent();
153
Chris Lattner043870d2005-09-09 18:17:41 +0000154 // Normal instructions can be commuted the obvious way.
Hal Finkel171a8ad2013-04-12 02:18:09 +0000155 if (MI->getOpcode() != PPC::RLWIMI &&
156 MI->getOpcode() != PPC::RLWIMIo)
Jakob Stoklund Olesena9fa4fd2012-11-28 02:35:17 +0000157 return TargetInstrInfo::commuteInstruction(MI, NewMI);
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000158
Chris Lattner043870d2005-09-09 18:17:41 +0000159 // Cannot commute if it has a non-zero rotate count.
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000160 if (MI->getOperand(3).getImm() != 0)
Chris Lattner043870d2005-09-09 18:17:41 +0000161 return 0;
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000162
Chris Lattner043870d2005-09-09 18:17:41 +0000163 // If we have a zero rotate count, we have:
164 // M = mask(MB,ME)
165 // Op0 = (Op1 & ~M) | (Op2 & M)
166 // Change this to:
167 // M = mask((ME+1)&31, (MB-1)&31)
168 // Op0 = (Op2 & ~M) | (Op1 & M)
169
170 // Swap op1/op2
Evan Chenga4d16a12008-02-13 02:46:49 +0000171 unsigned Reg0 = MI->getOperand(0).getReg();
Chris Lattner043870d2005-09-09 18:17:41 +0000172 unsigned Reg1 = MI->getOperand(1).getReg();
173 unsigned Reg2 = MI->getOperand(2).getReg();
Evan Cheng6ce7dc22006-11-15 20:58:11 +0000174 bool Reg1IsKill = MI->getOperand(1).isKill();
175 bool Reg2IsKill = MI->getOperand(2).isKill();
Evan Cheng58dcb0e2008-06-16 07:33:11 +0000176 bool ChangeReg0 = false;
Evan Chenga4d16a12008-02-13 02:46:49 +0000177 // If machine instrs are no longer in two-address forms, update
178 // destination register as well.
179 if (Reg0 == Reg1) {
180 // Must be two address instruction!
Evan Chenge837dea2011-06-28 19:10:37 +0000181 assert(MI->getDesc().getOperandConstraint(0, MCOI::TIED_TO) &&
Evan Chenga4d16a12008-02-13 02:46:49 +0000182 "Expecting a two-address instruction!");
Evan Chenga4d16a12008-02-13 02:46:49 +0000183 Reg2IsKill = false;
Evan Cheng58dcb0e2008-06-16 07:33:11 +0000184 ChangeReg0 = true;
Evan Chenga4d16a12008-02-13 02:46:49 +0000185 }
Evan Cheng58dcb0e2008-06-16 07:33:11 +0000186
187 // Masks.
188 unsigned MB = MI->getOperand(4).getImm();
189 unsigned ME = MI->getOperand(5).getImm();
190
191 if (NewMI) {
192 // Create a new instruction.
193 unsigned Reg0 = ChangeReg0 ? Reg2 : MI->getOperand(0).getReg();
194 bool Reg0IsDead = MI->getOperand(0).isDead();
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000195 return BuildMI(MF, MI->getDebugLoc(), MI->getDesc())
Bill Wendling587daed2009-05-13 21:33:08 +0000196 .addReg(Reg0, RegState::Define | getDeadRegState(Reg0IsDead))
197 .addReg(Reg2, getKillRegState(Reg2IsKill))
198 .addReg(Reg1, getKillRegState(Reg1IsKill))
Evan Cheng58dcb0e2008-06-16 07:33:11 +0000199 .addImm((ME+1) & 31)
200 .addImm((MB-1) & 31);
201 }
202
203 if (ChangeReg0)
204 MI->getOperand(0).setReg(Reg2);
Chris Lattnere53f4a02006-05-04 17:52:23 +0000205 MI->getOperand(2).setReg(Reg1);
206 MI->getOperand(1).setReg(Reg2);
Chris Lattnerf7382302007-12-30 21:56:09 +0000207 MI->getOperand(2).setIsKill(Reg1IsKill);
208 MI->getOperand(1).setIsKill(Reg2IsKill);
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000209
Chris Lattner043870d2005-09-09 18:17:41 +0000210 // Swap the mask around.
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000211 MI->getOperand(4).setImm((ME+1) & 31);
212 MI->getOperand(5).setImm((MB-1) & 31);
Chris Lattner043870d2005-09-09 18:17:41 +0000213 return MI;
214}
Chris Lattnerbbf1c722006-03-05 23:49:55 +0000215
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000216void PPCInstrInfo::insertNoop(MachineBasicBlock &MBB,
Chris Lattnerbbf1c722006-03-05 23:49:55 +0000217 MachineBasicBlock::iterator MI) const {
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000218 DebugLoc DL;
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000219 BuildMI(MBB, MI, DL, get(PPC::NOP));
Chris Lattnerbbf1c722006-03-05 23:49:55 +0000220}
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000221
222
223// Branch analysis.
Hal Finkel99f823f2012-06-08 15:38:21 +0000224// Note: If the condition register is set to CTR or CTR8 then this is a
225// BDNZ (imm == 1) or BDZ (imm == 0) branch.
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000226bool PPCInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
227 MachineBasicBlock *&FBB,
Evan Chengdc54d312009-02-09 07:14:22 +0000228 SmallVectorImpl<MachineOperand> &Cond,
229 bool AllowModify) const {
Hal Finkel99f823f2012-06-08 15:38:21 +0000230 bool isPPC64 = TM.getSubtargetImpl()->isPPC64();
231
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000232 // If the block has no terminators, it just falls into the block after it.
233 MachineBasicBlock::iterator I = MBB.end();
Dale Johannesen93d6a7e2010-04-02 01:38:09 +0000234 if (I == MBB.begin())
235 return false;
236 --I;
237 while (I->isDebugValue()) {
238 if (I == MBB.begin())
239 return false;
240 --I;
241 }
242 if (!isUnpredicatedTerminator(I))
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000243 return false;
244
245 // Get the last instruction in the block.
246 MachineInstr *LastInst = I;
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000247
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000248 // If there is only one terminator instruction, process it.
Evan Chengbfd2ec42007-06-08 21:59:56 +0000249 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000250 if (LastInst->getOpcode() == PPC::B) {
Evan Cheng82ae9332009-05-08 23:09:25 +0000251 if (!LastInst->getOperand(0).isMBB())
252 return true;
Chris Lattner8aa797a2007-12-30 23:10:15 +0000253 TBB = LastInst->getOperand(0).getMBB();
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000254 return false;
Chris Lattner289c2d52006-11-17 22:14:47 +0000255 } else if (LastInst->getOpcode() == PPC::BCC) {
Evan Cheng82ae9332009-05-08 23:09:25 +0000256 if (!LastInst->getOperand(2).isMBB())
257 return true;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000258 // Block ends with fall-through condbranch.
Chris Lattner8aa797a2007-12-30 23:10:15 +0000259 TBB = LastInst->getOperand(2).getMBB();
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000260 Cond.push_back(LastInst->getOperand(0));
261 Cond.push_back(LastInst->getOperand(1));
Chris Lattner7c4fe252006-10-21 06:03:11 +0000262 return false;
Hal Finkel99f823f2012-06-08 15:38:21 +0000263 } else if (LastInst->getOpcode() == PPC::BDNZ8 ||
264 LastInst->getOpcode() == PPC::BDNZ) {
265 if (!LastInst->getOperand(0).isMBB())
266 return true;
Hal Finkel7255d2a2012-06-08 19:19:53 +0000267 if (DisableCTRLoopAnal)
Hal Finkel09fdc7b2012-06-08 15:38:25 +0000268 return true;
Hal Finkel99f823f2012-06-08 15:38:21 +0000269 TBB = LastInst->getOperand(0).getMBB();
270 Cond.push_back(MachineOperand::CreateImm(1));
271 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
272 true));
273 return false;
274 } else if (LastInst->getOpcode() == PPC::BDZ8 ||
275 LastInst->getOpcode() == PPC::BDZ) {
276 if (!LastInst->getOperand(0).isMBB())
277 return true;
Hal Finkel7255d2a2012-06-08 19:19:53 +0000278 if (DisableCTRLoopAnal)
Hal Finkel09fdc7b2012-06-08 15:38:25 +0000279 return true;
Hal Finkel99f823f2012-06-08 15:38:21 +0000280 TBB = LastInst->getOperand(0).getMBB();
281 Cond.push_back(MachineOperand::CreateImm(0));
282 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
283 true));
284 return false;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000285 }
Hal Finkel99f823f2012-06-08 15:38:21 +0000286
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000287 // Otherwise, don't know what this is.
288 return true;
289 }
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000290
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000291 // Get the instruction before it if it's a terminator.
292 MachineInstr *SecondLastInst = I;
293
294 // If there are three terminators, we don't know what sort of block this is.
295 if (SecondLastInst && I != MBB.begin() &&
Evan Chengbfd2ec42007-06-08 21:59:56 +0000296 isUnpredicatedTerminator(--I))
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000297 return true;
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000298
Chris Lattner289c2d52006-11-17 22:14:47 +0000299 // If the block ends with PPC::B and PPC:BCC, handle it.
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000300 if (SecondLastInst->getOpcode() == PPC::BCC &&
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000301 LastInst->getOpcode() == PPC::B) {
Evan Cheng82ae9332009-05-08 23:09:25 +0000302 if (!SecondLastInst->getOperand(2).isMBB() ||
303 !LastInst->getOperand(0).isMBB())
304 return true;
Chris Lattner8aa797a2007-12-30 23:10:15 +0000305 TBB = SecondLastInst->getOperand(2).getMBB();
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000306 Cond.push_back(SecondLastInst->getOperand(0));
307 Cond.push_back(SecondLastInst->getOperand(1));
Chris Lattner8aa797a2007-12-30 23:10:15 +0000308 FBB = LastInst->getOperand(0).getMBB();
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000309 return false;
Hal Finkel99f823f2012-06-08 15:38:21 +0000310 } else if ((SecondLastInst->getOpcode() == PPC::BDNZ8 ||
311 SecondLastInst->getOpcode() == PPC::BDNZ) &&
312 LastInst->getOpcode() == PPC::B) {
313 if (!SecondLastInst->getOperand(0).isMBB() ||
314 !LastInst->getOperand(0).isMBB())
315 return true;
Hal Finkel7255d2a2012-06-08 19:19:53 +0000316 if (DisableCTRLoopAnal)
Hal Finkel09fdc7b2012-06-08 15:38:25 +0000317 return true;
Hal Finkel99f823f2012-06-08 15:38:21 +0000318 TBB = SecondLastInst->getOperand(0).getMBB();
319 Cond.push_back(MachineOperand::CreateImm(1));
320 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
321 true));
322 FBB = LastInst->getOperand(0).getMBB();
323 return false;
324 } else if ((SecondLastInst->getOpcode() == PPC::BDZ8 ||
325 SecondLastInst->getOpcode() == PPC::BDZ) &&
326 LastInst->getOpcode() == PPC::B) {
327 if (!SecondLastInst->getOperand(0).isMBB() ||
328 !LastInst->getOperand(0).isMBB())
329 return true;
Hal Finkel7255d2a2012-06-08 19:19:53 +0000330 if (DisableCTRLoopAnal)
Hal Finkel09fdc7b2012-06-08 15:38:25 +0000331 return true;
Hal Finkel99f823f2012-06-08 15:38:21 +0000332 TBB = SecondLastInst->getOperand(0).getMBB();
333 Cond.push_back(MachineOperand::CreateImm(0));
334 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
335 true));
336 FBB = LastInst->getOperand(0).getMBB();
337 return false;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000338 }
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000339
Dale Johannesen13e8b512007-06-13 17:59:52 +0000340 // If the block ends with two PPC:Bs, handle it. The second one is not
341 // executed, so remove it.
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000342 if (SecondLastInst->getOpcode() == PPC::B &&
Dale Johannesen13e8b512007-06-13 17:59:52 +0000343 LastInst->getOpcode() == PPC::B) {
Evan Cheng82ae9332009-05-08 23:09:25 +0000344 if (!SecondLastInst->getOperand(0).isMBB())
345 return true;
Chris Lattner8aa797a2007-12-30 23:10:15 +0000346 TBB = SecondLastInst->getOperand(0).getMBB();
Dale Johannesen13e8b512007-06-13 17:59:52 +0000347 I = LastInst;
Evan Chengdc54d312009-02-09 07:14:22 +0000348 if (AllowModify)
349 I->eraseFromParent();
Dale Johannesen13e8b512007-06-13 17:59:52 +0000350 return false;
351 }
352
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000353 // Otherwise, can't handle this.
354 return true;
355}
356
Evan Chengb5cdaa22007-05-18 00:05:48 +0000357unsigned PPCInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000358 MachineBasicBlock::iterator I = MBB.end();
Evan Chengb5cdaa22007-05-18 00:05:48 +0000359 if (I == MBB.begin()) return 0;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000360 --I;
Dale Johannesen93d6a7e2010-04-02 01:38:09 +0000361 while (I->isDebugValue()) {
362 if (I == MBB.begin())
363 return 0;
364 --I;
365 }
Hal Finkel99f823f2012-06-08 15:38:21 +0000366 if (I->getOpcode() != PPC::B && I->getOpcode() != PPC::BCC &&
367 I->getOpcode() != PPC::BDNZ8 && I->getOpcode() != PPC::BDNZ &&
368 I->getOpcode() != PPC::BDZ8 && I->getOpcode() != PPC::BDZ)
Evan Chengb5cdaa22007-05-18 00:05:48 +0000369 return 0;
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000370
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000371 // Remove the branch.
372 I->eraseFromParent();
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000373
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000374 I = MBB.end();
375
Evan Chengb5cdaa22007-05-18 00:05:48 +0000376 if (I == MBB.begin()) return 1;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000377 --I;
Hal Finkel99f823f2012-06-08 15:38:21 +0000378 if (I->getOpcode() != PPC::BCC &&
379 I->getOpcode() != PPC::BDNZ8 && I->getOpcode() != PPC::BDNZ &&
380 I->getOpcode() != PPC::BDZ8 && I->getOpcode() != PPC::BDZ)
Evan Chengb5cdaa22007-05-18 00:05:48 +0000381 return 1;
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000382
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000383 // Remove the branch.
384 I->eraseFromParent();
Evan Chengb5cdaa22007-05-18 00:05:48 +0000385 return 2;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000386}
387
Evan Chengb5cdaa22007-05-18 00:05:48 +0000388unsigned
389PPCInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
390 MachineBasicBlock *FBB,
Stuart Hastings3bf91252010-06-17 22:43:56 +0000391 const SmallVectorImpl<MachineOperand> &Cond,
392 DebugLoc DL) const {
Chris Lattner2dc77232006-10-17 18:06:55 +0000393 // Shouldn't be a fall through.
394 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000395 assert((Cond.size() == 2 || Cond.size() == 0) &&
Chris Lattner54108062006-10-21 05:36:13 +0000396 "PPC branch conditions have two components!");
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000397
Hal Finkel99f823f2012-06-08 15:38:21 +0000398 bool isPPC64 = TM.getSubtargetImpl()->isPPC64();
399
Chris Lattner54108062006-10-21 05:36:13 +0000400 // One-way branch.
Chris Lattner2dc77232006-10-17 18:06:55 +0000401 if (FBB == 0) {
Chris Lattner54108062006-10-21 05:36:13 +0000402 if (Cond.empty()) // Unconditional branch
Stuart Hastings3bf91252010-06-17 22:43:56 +0000403 BuildMI(&MBB, DL, get(PPC::B)).addMBB(TBB);
Hal Finkel99f823f2012-06-08 15:38:21 +0000404 else if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
405 BuildMI(&MBB, DL, get(Cond[0].getImm() ?
406 (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
407 (isPPC64 ? PPC::BDZ8 : PPC::BDZ))).addMBB(TBB);
Chris Lattner54108062006-10-21 05:36:13 +0000408 else // Conditional branch
Stuart Hastings3bf91252010-06-17 22:43:56 +0000409 BuildMI(&MBB, DL, get(PPC::BCC))
Chris Lattner18258c62006-11-17 22:37:34 +0000410 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
Evan Chengb5cdaa22007-05-18 00:05:48 +0000411 return 1;
Chris Lattner2dc77232006-10-17 18:06:55 +0000412 }
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000413
Chris Lattner879d09c2006-10-21 05:42:09 +0000414 // Two-way Conditional Branch.
Hal Finkel99f823f2012-06-08 15:38:21 +0000415 if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
416 BuildMI(&MBB, DL, get(Cond[0].getImm() ?
417 (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
418 (isPPC64 ? PPC::BDZ8 : PPC::BDZ))).addMBB(TBB);
419 else
420 BuildMI(&MBB, DL, get(PPC::BCC))
421 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
Stuart Hastings3bf91252010-06-17 22:43:56 +0000422 BuildMI(&MBB, DL, get(PPC::B)).addMBB(FBB);
Evan Chengb5cdaa22007-05-18 00:05:48 +0000423 return 2;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000424}
425
Hal Finkelff56d1a2013-04-05 23:29:01 +0000426// Select analysis.
427bool PPCInstrInfo::canInsertSelect(const MachineBasicBlock &MBB,
428 const SmallVectorImpl<MachineOperand> &Cond,
429 unsigned TrueReg, unsigned FalseReg,
430 int &CondCycles, int &TrueCycles, int &FalseCycles) const {
431 if (!TM.getSubtargetImpl()->hasISEL())
432 return false;
433
434 if (Cond.size() != 2)
435 return false;
436
437 // If this is really a bdnz-like condition, then it cannot be turned into a
438 // select.
439 if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
440 return false;
441
442 // Check register classes.
443 const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
444 const TargetRegisterClass *RC =
445 RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg));
446 if (!RC)
447 return false;
448
449 // isel is for regular integer GPRs only.
450 if (!PPC::GPRCRegClass.hasSubClassEq(RC) &&
Hal Finkelae4f3f62013-07-15 20:22:58 +0000451 !PPC::GPRC_NOR0RegClass.hasSubClassEq(RC) &&
452 !PPC::G8RCRegClass.hasSubClassEq(RC) &&
453 !PPC::G8RC_NOX0RegClass.hasSubClassEq(RC))
Hal Finkelff56d1a2013-04-05 23:29:01 +0000454 return false;
455
456 // FIXME: These numbers are for the A2, how well they work for other cores is
457 // an open question. On the A2, the isel instruction has a 2-cycle latency
458 // but single-cycle throughput. These numbers are used in combination with
459 // the MispredictPenalty setting from the active SchedMachineModel.
460 CondCycles = 1;
461 TrueCycles = 1;
462 FalseCycles = 1;
463
464 return true;
465}
466
467void PPCInstrInfo::insertSelect(MachineBasicBlock &MBB,
468 MachineBasicBlock::iterator MI, DebugLoc dl,
469 unsigned DestReg,
470 const SmallVectorImpl<MachineOperand> &Cond,
471 unsigned TrueReg, unsigned FalseReg) const {
472 assert(Cond.size() == 2 &&
473 "PPC branch conditions have two components!");
474
475 assert(TM.getSubtargetImpl()->hasISEL() &&
476 "Cannot insert select on target without ISEL support");
477
478 // Get the register classes.
479 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
480 const TargetRegisterClass *RC =
481 RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg));
482 assert(RC && "TrueReg and FalseReg must have overlapping register classes");
Hal Finkelae4f3f62013-07-15 20:22:58 +0000483
484 bool Is64Bit = PPC::G8RCRegClass.hasSubClassEq(RC) ||
485 PPC::G8RC_NOX0RegClass.hasSubClassEq(RC);
486 assert((Is64Bit ||
487 PPC::GPRCRegClass.hasSubClassEq(RC) ||
488 PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) &&
Hal Finkelff56d1a2013-04-05 23:29:01 +0000489 "isel is for regular integer GPRs only");
490
Hal Finkelae4f3f62013-07-15 20:22:58 +0000491 unsigned OpCode = Is64Bit ? PPC::ISEL8 : PPC::ISEL;
Hal Finkelff56d1a2013-04-05 23:29:01 +0000492 unsigned SelectPred = Cond[0].getImm();
493
494 unsigned SubIdx;
495 bool SwapOps;
496 switch (SelectPred) {
497 default: llvm_unreachable("invalid predicate for isel");
498 case PPC::PRED_EQ: SubIdx = PPC::sub_eq; SwapOps = false; break;
499 case PPC::PRED_NE: SubIdx = PPC::sub_eq; SwapOps = true; break;
500 case PPC::PRED_LT: SubIdx = PPC::sub_lt; SwapOps = false; break;
501 case PPC::PRED_GE: SubIdx = PPC::sub_lt; SwapOps = true; break;
502 case PPC::PRED_GT: SubIdx = PPC::sub_gt; SwapOps = false; break;
503 case PPC::PRED_LE: SubIdx = PPC::sub_gt; SwapOps = true; break;
504 case PPC::PRED_UN: SubIdx = PPC::sub_un; SwapOps = false; break;
505 case PPC::PRED_NU: SubIdx = PPC::sub_un; SwapOps = true; break;
506 }
507
508 unsigned FirstReg = SwapOps ? FalseReg : TrueReg,
509 SecondReg = SwapOps ? TrueReg : FalseReg;
510
511 // The first input register of isel cannot be r0. If it is a member
512 // of a register class that can be r0, then copy it first (the
513 // register allocator should eliminate the copy).
514 if (MRI.getRegClass(FirstReg)->contains(PPC::R0) ||
515 MRI.getRegClass(FirstReg)->contains(PPC::X0)) {
516 const TargetRegisterClass *FirstRC =
517 MRI.getRegClass(FirstReg)->contains(PPC::X0) ?
518 &PPC::G8RC_NOX0RegClass : &PPC::GPRC_NOR0RegClass;
519 unsigned OldFirstReg = FirstReg;
520 FirstReg = MRI.createVirtualRegister(FirstRC);
521 BuildMI(MBB, MI, dl, get(TargetOpcode::COPY), FirstReg)
522 .addReg(OldFirstReg);
523 }
524
525 BuildMI(MBB, MI, dl, get(OpCode), DestReg)
526 .addReg(FirstReg).addReg(SecondReg)
527 .addReg(Cond[1].getReg(), 0, SubIdx);
528}
529
Jakob Stoklund Olesen27689b02010-07-11 07:31:00 +0000530void PPCInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
531 MachineBasicBlock::iterator I, DebugLoc DL,
532 unsigned DestReg, unsigned SrcReg,
533 bool KillSrc) const {
534 unsigned Opc;
535 if (PPC::GPRCRegClass.contains(DestReg, SrcReg))
536 Opc = PPC::OR;
537 else if (PPC::G8RCRegClass.contains(DestReg, SrcReg))
538 Opc = PPC::OR8;
539 else if (PPC::F4RCRegClass.contains(DestReg, SrcReg))
540 Opc = PPC::FMR;
541 else if (PPC::CRRCRegClass.contains(DestReg, SrcReg))
542 Opc = PPC::MCRF;
543 else if (PPC::VRRCRegClass.contains(DestReg, SrcReg))
544 Opc = PPC::VOR;
545 else if (PPC::CRBITRCRegClass.contains(DestReg, SrcReg))
546 Opc = PPC::CROR;
547 else
548 llvm_unreachable("Impossible reg-to-reg copy");
Owen Andersond10fd972007-12-31 06:32:00 +0000549
Evan Chenge837dea2011-06-28 19:10:37 +0000550 const MCInstrDesc &MCID = get(Opc);
551 if (MCID.getNumOperands() == 3)
552 BuildMI(MBB, I, DL, MCID, DestReg)
Jakob Stoklund Olesen27689b02010-07-11 07:31:00 +0000553 .addReg(SrcReg).addReg(SrcReg, getKillRegState(KillSrc));
554 else
Evan Chenge837dea2011-06-28 19:10:37 +0000555 BuildMI(MBB, I, DL, MCID, DestReg).addReg(SrcReg, getKillRegState(KillSrc));
Owen Andersond10fd972007-12-31 06:32:00 +0000556}
557
Hal Finkel3fd00182011-12-05 17:55:17 +0000558// This function returns true if a CR spill is necessary and false otherwise.
Bill Wendling4a66e9a2008-03-10 22:49:16 +0000559bool
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000560PPCInstrInfo::StoreRegToStackSlot(MachineFunction &MF,
561 unsigned SrcReg, bool isKill,
Bill Wendling4a66e9a2008-03-10 22:49:16 +0000562 int FrameIdx,
563 const TargetRegisterClass *RC,
Hal Finkel32497292013-03-17 04:43:44 +0000564 SmallVectorImpl<MachineInstr*> &NewMIs,
Hal Finkel3f2c0472013-03-23 22:06:03 +0000565 bool &NonRI, bool &SpillsVRS) const{
Hal Finkelf25f93b2013-03-27 21:21:15 +0000566 // Note: If additional store instructions are added here,
567 // update isStoreToStackSlot.
568
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000569 DebugLoc DL;
Craig Topperc9099502012-04-20 06:31:50 +0000570 if (PPC::GPRCRegClass.hasSubClassEq(RC)) {
Hal Finkel7257fda2013-03-23 17:14:27 +0000571 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
572 .addReg(SrcReg,
573 getKillRegState(isKill)),
574 FrameIdx));
Craig Topperc9099502012-04-20 06:31:50 +0000575 } else if (PPC::G8RCRegClass.hasSubClassEq(RC)) {
Hal Finkel7257fda2013-03-23 17:14:27 +0000576 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STD))
577 .addReg(SrcReg,
578 getKillRegState(isKill)),
579 FrameIdx));
Craig Topperc9099502012-04-20 06:31:50 +0000580 } else if (PPC::F8RCRegClass.hasSubClassEq(RC)) {
Dale Johannesen21b55412009-02-12 23:08:38 +0000581 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFD))
Bill Wendling587daed2009-05-13 21:33:08 +0000582 .addReg(SrcReg,
583 getKillRegState(isKill)),
584 FrameIdx));
Craig Topperc9099502012-04-20 06:31:50 +0000585 } else if (PPC::F4RCRegClass.hasSubClassEq(RC)) {
Dale Johannesen21b55412009-02-12 23:08:38 +0000586 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFS))
Bill Wendling587daed2009-05-13 21:33:08 +0000587 .addReg(SrcReg,
588 getKillRegState(isKill)),
589 FrameIdx));
Craig Topperc9099502012-04-20 06:31:50 +0000590 } else if (PPC::CRRCRegClass.hasSubClassEq(RC)) {
Hal Finkel7285e8d2013-03-12 14:12:16 +0000591 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CR))
592 .addReg(SrcReg,
593 getKillRegState(isKill)),
594 FrameIdx));
595 return true;
Craig Topperc9099502012-04-20 06:31:50 +0000596 } else if (PPC::CRBITRCRegClass.hasSubClassEq(RC)) {
Nicolas Geoffray0404cd92008-03-10 14:12:10 +0000597 // FIXME: We use CRi here because there is no mtcrf on a bit. Since the
598 // backend currently only uses CR1EQ as an individual bit, this should
599 // not cause any bug. If we need other uses of CR bits, the following
600 // code may be invalid.
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000601 unsigned Reg = 0;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000602 if (SrcReg == PPC::CR0LT || SrcReg == PPC::CR0GT ||
603 SrcReg == PPC::CR0EQ || SrcReg == PPC::CR0UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000604 Reg = PPC::CR0;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000605 else if (SrcReg == PPC::CR1LT || SrcReg == PPC::CR1GT ||
606 SrcReg == PPC::CR1EQ || SrcReg == PPC::CR1UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000607 Reg = PPC::CR1;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000608 else if (SrcReg == PPC::CR2LT || SrcReg == PPC::CR2GT ||
609 SrcReg == PPC::CR2EQ || SrcReg == PPC::CR2UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000610 Reg = PPC::CR2;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000611 else if (SrcReg == PPC::CR3LT || SrcReg == PPC::CR3GT ||
612 SrcReg == PPC::CR3EQ || SrcReg == PPC::CR3UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000613 Reg = PPC::CR3;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000614 else if (SrcReg == PPC::CR4LT || SrcReg == PPC::CR4GT ||
615 SrcReg == PPC::CR4EQ || SrcReg == PPC::CR4UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000616 Reg = PPC::CR4;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000617 else if (SrcReg == PPC::CR5LT || SrcReg == PPC::CR5GT ||
618 SrcReg == PPC::CR5EQ || SrcReg == PPC::CR5UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000619 Reg = PPC::CR5;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000620 else if (SrcReg == PPC::CR6LT || SrcReg == PPC::CR6GT ||
621 SrcReg == PPC::CR6EQ || SrcReg == PPC::CR6UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000622 Reg = PPC::CR6;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000623 else if (SrcReg == PPC::CR7LT || SrcReg == PPC::CR7GT ||
624 SrcReg == PPC::CR7EQ || SrcReg == PPC::CR7UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000625 Reg = PPC::CR7;
626
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000627 return StoreRegToStackSlot(MF, Reg, isKill, FrameIdx,
Hal Finkel3f2c0472013-03-23 22:06:03 +0000628 &PPC::CRRCRegClass, NewMIs, NonRI, SpillsVRS);
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000629
Craig Topperc9099502012-04-20 06:31:50 +0000630 } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) {
Hal Finkel32497292013-03-17 04:43:44 +0000631 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STVX))
632 .addReg(SrcReg,
633 getKillRegState(isKill)),
634 FrameIdx));
635 NonRI = true;
Hal Finkel10f7f2a2013-03-21 19:03:21 +0000636 } else if (PPC::VRSAVERCRegClass.hasSubClassEq(RC)) {
Hal Finkelb7e11e42013-03-27 00:02:20 +0000637 assert(TM.getSubtargetImpl()->isDarwin() &&
638 "VRSAVE only needs spill/restore on Darwin");
Hal Finkel10f7f2a2013-03-21 19:03:21 +0000639 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_VRSAVE))
640 .addReg(SrcReg,
641 getKillRegState(isKill)),
642 FrameIdx));
Hal Finkel3f2c0472013-03-23 22:06:03 +0000643 SpillsVRS = true;
Owen Andersonf6372aa2008-01-01 21:11:32 +0000644 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000645 llvm_unreachable("Unknown regclass!");
Owen Andersonf6372aa2008-01-01 21:11:32 +0000646 }
Bill Wendling7194aaf2008-03-03 22:19:16 +0000647
648 return false;
Owen Andersonf6372aa2008-01-01 21:11:32 +0000649}
650
651void
652PPCInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
Bill Wendling7194aaf2008-03-03 22:19:16 +0000653 MachineBasicBlock::iterator MI,
654 unsigned SrcReg, bool isKill, int FrameIdx,
Evan Cheng746ad692010-05-06 19:06:44 +0000655 const TargetRegisterClass *RC,
656 const TargetRegisterInfo *TRI) const {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000657 MachineFunction &MF = *MBB.getParent();
Owen Andersonf6372aa2008-01-01 21:11:32 +0000658 SmallVector<MachineInstr*, 4> NewMIs;
Bill Wendling7194aaf2008-03-03 22:19:16 +0000659
Hal Finkel0cfb42a2013-03-15 05:06:04 +0000660 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
661 FuncInfo->setHasSpills();
662
Hal Finkel3f2c0472013-03-23 22:06:03 +0000663 bool NonRI = false, SpillsVRS = false;
664 if (StoreRegToStackSlot(MF, SrcReg, isKill, FrameIdx, RC, NewMIs,
665 NonRI, SpillsVRS))
Bill Wendling7194aaf2008-03-03 22:19:16 +0000666 FuncInfo->setSpillsCR();
Bill Wendling7194aaf2008-03-03 22:19:16 +0000667
Hal Finkel3f2c0472013-03-23 22:06:03 +0000668 if (SpillsVRS)
669 FuncInfo->setSpillsVRSAVE();
670
Hal Finkel32497292013-03-17 04:43:44 +0000671 if (NonRI)
672 FuncInfo->setHasNonRISpills();
673
Owen Andersonf6372aa2008-01-01 21:11:32 +0000674 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
675 MBB.insert(MI, NewMIs[i]);
Jakob Stoklund Olesen7a79fcb2010-07-16 18:22:00 +0000676
677 const MachineFrameInfo &MFI = *MF.getFrameInfo();
678 MachineMemOperand *MMO =
Jay Foad978e0df2011-11-15 07:34:52 +0000679 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx),
Chris Lattner59db5492010-09-21 04:39:43 +0000680 MachineMemOperand::MOStore,
Jakob Stoklund Olesen7a79fcb2010-07-16 18:22:00 +0000681 MFI.getObjectSize(FrameIdx),
682 MFI.getObjectAlignment(FrameIdx));
683 NewMIs.back()->addMemOperand(MF, MMO);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000684}
685
Hal Finkeld21e9302011-12-06 20:55:36 +0000686bool
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000687PPCInstrInfo::LoadRegFromStackSlot(MachineFunction &MF, DebugLoc DL,
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000688 unsigned DestReg, int FrameIdx,
Bill Wendling4a66e9a2008-03-10 22:49:16 +0000689 const TargetRegisterClass *RC,
Hal Finkel32497292013-03-17 04:43:44 +0000690 SmallVectorImpl<MachineInstr*> &NewMIs,
Hal Finkel3f2c0472013-03-23 22:06:03 +0000691 bool &NonRI, bool &SpillsVRS) const{
Hal Finkelf25f93b2013-03-27 21:21:15 +0000692 // Note: If additional load instructions are added here,
693 // update isLoadFromStackSlot.
694
Craig Topperc9099502012-04-20 06:31:50 +0000695 if (PPC::GPRCRegClass.hasSubClassEq(RC)) {
Hal Finkelfc805862013-03-27 19:10:40 +0000696 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ),
697 DestReg), FrameIdx));
Craig Topperc9099502012-04-20 06:31:50 +0000698 } else if (PPC::G8RCRegClass.hasSubClassEq(RC)) {
Hal Finkelfc805862013-03-27 19:10:40 +0000699 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LD), DestReg),
700 FrameIdx));
Craig Topperc9099502012-04-20 06:31:50 +0000701 } else if (PPC::F8RCRegClass.hasSubClassEq(RC)) {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000702 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFD), DestReg),
Owen Andersonf6372aa2008-01-01 21:11:32 +0000703 FrameIdx));
Craig Topperc9099502012-04-20 06:31:50 +0000704 } else if (PPC::F4RCRegClass.hasSubClassEq(RC)) {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000705 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFS), DestReg),
Owen Andersonf6372aa2008-01-01 21:11:32 +0000706 FrameIdx));
Craig Topperc9099502012-04-20 06:31:50 +0000707 } else if (PPC::CRRCRegClass.hasSubClassEq(RC)) {
Hal Finkel7285e8d2013-03-12 14:12:16 +0000708 NewMIs.push_back(addFrameReference(BuildMI(MF, DL,
709 get(PPC::RESTORE_CR), DestReg),
710 FrameIdx));
711 return true;
Craig Topperc9099502012-04-20 06:31:50 +0000712 } else if (PPC::CRBITRCRegClass.hasSubClassEq(RC)) {
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000713
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000714 unsigned Reg = 0;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000715 if (DestReg == PPC::CR0LT || DestReg == PPC::CR0GT ||
716 DestReg == PPC::CR0EQ || DestReg == PPC::CR0UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000717 Reg = PPC::CR0;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000718 else if (DestReg == PPC::CR1LT || DestReg == PPC::CR1GT ||
719 DestReg == PPC::CR1EQ || DestReg == PPC::CR1UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000720 Reg = PPC::CR1;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000721 else if (DestReg == PPC::CR2LT || DestReg == PPC::CR2GT ||
722 DestReg == PPC::CR2EQ || DestReg == PPC::CR2UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000723 Reg = PPC::CR2;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000724 else if (DestReg == PPC::CR3LT || DestReg == PPC::CR3GT ||
725 DestReg == PPC::CR3EQ || DestReg == PPC::CR3UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000726 Reg = PPC::CR3;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000727 else if (DestReg == PPC::CR4LT || DestReg == PPC::CR4GT ||
728 DestReg == PPC::CR4EQ || DestReg == PPC::CR4UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000729 Reg = PPC::CR4;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000730 else if (DestReg == PPC::CR5LT || DestReg == PPC::CR5GT ||
731 DestReg == PPC::CR5EQ || DestReg == PPC::CR5UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000732 Reg = PPC::CR5;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000733 else if (DestReg == PPC::CR6LT || DestReg == PPC::CR6GT ||
734 DestReg == PPC::CR6EQ || DestReg == PPC::CR6UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000735 Reg = PPC::CR6;
Tilmann Scheller6a3a1ba2009-07-03 06:47:55 +0000736 else if (DestReg == PPC::CR7LT || DestReg == PPC::CR7GT ||
737 DestReg == PPC::CR7EQ || DestReg == PPC::CR7UN)
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000738 Reg = PPC::CR7;
739
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000740 return LoadRegFromStackSlot(MF, DL, Reg, FrameIdx,
Hal Finkel3f2c0472013-03-23 22:06:03 +0000741 &PPC::CRRCRegClass, NewMIs, NonRI, SpillsVRS);
Nicolas Geoffray9348c692008-03-10 17:46:45 +0000742
Craig Topperc9099502012-04-20 06:31:50 +0000743 } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) {
Hal Finkel32497292013-03-17 04:43:44 +0000744 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LVX), DestReg),
745 FrameIdx));
746 NonRI = true;
Hal Finkel10f7f2a2013-03-21 19:03:21 +0000747 } else if (PPC::VRSAVERCRegClass.hasSubClassEq(RC)) {
Hal Finkelb7e11e42013-03-27 00:02:20 +0000748 assert(TM.getSubtargetImpl()->isDarwin() &&
749 "VRSAVE only needs spill/restore on Darwin");
Hal Finkel10f7f2a2013-03-21 19:03:21 +0000750 NewMIs.push_back(addFrameReference(BuildMI(MF, DL,
751 get(PPC::RESTORE_VRSAVE),
752 DestReg),
753 FrameIdx));
Hal Finkel3f2c0472013-03-23 22:06:03 +0000754 SpillsVRS = true;
Owen Andersonf6372aa2008-01-01 21:11:32 +0000755 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000756 llvm_unreachable("Unknown regclass!");
Owen Andersonf6372aa2008-01-01 21:11:32 +0000757 }
Hal Finkeld21e9302011-12-06 20:55:36 +0000758
759 return false;
Owen Andersonf6372aa2008-01-01 21:11:32 +0000760}
761
762void
763PPCInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
Bill Wendling7194aaf2008-03-03 22:19:16 +0000764 MachineBasicBlock::iterator MI,
765 unsigned DestReg, int FrameIdx,
Evan Cheng746ad692010-05-06 19:06:44 +0000766 const TargetRegisterClass *RC,
767 const TargetRegisterInfo *TRI) const {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000768 MachineFunction &MF = *MBB.getParent();
Owen Andersonf6372aa2008-01-01 21:11:32 +0000769 SmallVector<MachineInstr*, 4> NewMIs;
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000770 DebugLoc DL;
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000771 if (MI != MBB.end()) DL = MI->getDebugLoc();
Hal Finkel32497292013-03-17 04:43:44 +0000772
773 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
774 FuncInfo->setHasSpills();
775
Hal Finkel3f2c0472013-03-23 22:06:03 +0000776 bool NonRI = false, SpillsVRS = false;
777 if (LoadRegFromStackSlot(MF, DL, DestReg, FrameIdx, RC, NewMIs,
778 NonRI, SpillsVRS))
Hal Finkeld21e9302011-12-06 20:55:36 +0000779 FuncInfo->setSpillsCR();
Hal Finkel32497292013-03-17 04:43:44 +0000780
Hal Finkel3f2c0472013-03-23 22:06:03 +0000781 if (SpillsVRS)
782 FuncInfo->setSpillsVRSAVE();
783
Hal Finkel32497292013-03-17 04:43:44 +0000784 if (NonRI)
785 FuncInfo->setHasNonRISpills();
786
Owen Andersonf6372aa2008-01-01 21:11:32 +0000787 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
788 MBB.insert(MI, NewMIs[i]);
Jakob Stoklund Olesen7a79fcb2010-07-16 18:22:00 +0000789
790 const MachineFrameInfo &MFI = *MF.getFrameInfo();
791 MachineMemOperand *MMO =
Jay Foad978e0df2011-11-15 07:34:52 +0000792 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx),
Chris Lattner59db5492010-09-21 04:39:43 +0000793 MachineMemOperand::MOLoad,
Jakob Stoklund Olesen7a79fcb2010-07-16 18:22:00 +0000794 MFI.getObjectSize(FrameIdx),
795 MFI.getObjectAlignment(FrameIdx));
796 NewMIs.back()->addMemOperand(MF, MMO);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000797}
798
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000799bool PPCInstrInfo::
Owen Anderson44eb65c2008-08-14 22:49:33 +0000800ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
Chris Lattner7c4fe252006-10-21 06:03:11 +0000801 assert(Cond.size() == 2 && "Invalid PPC branch opcode!");
Hal Finkel99f823f2012-06-08 15:38:21 +0000802 if (Cond[1].getReg() == PPC::CTR8 || Cond[1].getReg() == PPC::CTR)
803 Cond[0].setImm(Cond[0].getImm() == 0 ? 1 : 0);
804 else
805 // Leave the CR# the same, but invert the condition.
806 Cond[0].setImm(PPC::InvertPredicate((PPC::Predicate)Cond[0].getImm()));
Chris Lattner7c4fe252006-10-21 06:03:11 +0000807 return false;
Chris Lattnerc50e2bc2006-10-13 21:21:17 +0000808}
Nicolas Geoffray52e724a2008-04-16 20:10:13 +0000809
Hal Finkel839b9092013-04-06 19:30:30 +0000810bool PPCInstrInfo::FoldImmediate(MachineInstr *UseMI, MachineInstr *DefMI,
811 unsigned Reg, MachineRegisterInfo *MRI) const {
812 // For some instructions, it is legal to fold ZERO into the RA register field.
813 // A zero immediate should always be loaded with a single li.
814 unsigned DefOpc = DefMI->getOpcode();
815 if (DefOpc != PPC::LI && DefOpc != PPC::LI8)
816 return false;
817 if (!DefMI->getOperand(1).isImm())
818 return false;
819 if (DefMI->getOperand(1).getImm() != 0)
820 return false;
821
822 // Note that we cannot here invert the arguments of an isel in order to fold
823 // a ZERO into what is presented as the second argument. All we have here
824 // is the condition bit, and that might come from a CR-logical bit operation.
825
826 const MCInstrDesc &UseMCID = UseMI->getDesc();
827
828 // Only fold into real machine instructions.
829 if (UseMCID.isPseudo())
830 return false;
831
832 unsigned UseIdx;
833 for (UseIdx = 0; UseIdx < UseMI->getNumOperands(); ++UseIdx)
834 if (UseMI->getOperand(UseIdx).isReg() &&
835 UseMI->getOperand(UseIdx).getReg() == Reg)
836 break;
837
838 assert(UseIdx < UseMI->getNumOperands() && "Cannot find Reg in UseMI");
839 assert(UseIdx < UseMCID.getNumOperands() && "No operand description for Reg");
840
841 const MCOperandInfo *UseInfo = &UseMCID.OpInfo[UseIdx];
842
843 // We can fold the zero if this register requires a GPRC_NOR0/G8RC_NOX0
844 // register (which might also be specified as a pointer class kind).
845 if (UseInfo->isLookupPtrRegClass()) {
846 if (UseInfo->RegClass /* Kind */ != 1)
847 return false;
848 } else {
849 if (UseInfo->RegClass != PPC::GPRC_NOR0RegClassID &&
850 UseInfo->RegClass != PPC::G8RC_NOX0RegClassID)
851 return false;
852 }
853
854 // Make sure this is not tied to an output register (or otherwise
855 // constrained). This is true for ST?UX registers, for example, which
856 // are tied to their output registers.
857 if (UseInfo->Constraints != 0)
858 return false;
859
860 unsigned ZeroReg;
861 if (UseInfo->isLookupPtrRegClass()) {
862 bool isPPC64 = TM.getSubtargetImpl()->isPPC64();
863 ZeroReg = isPPC64 ? PPC::ZERO8 : PPC::ZERO;
864 } else {
865 ZeroReg = UseInfo->RegClass == PPC::G8RC_NOX0RegClassID ?
866 PPC::ZERO8 : PPC::ZERO;
867 }
868
869 bool DeleteDef = MRI->hasOneNonDBGUse(Reg);
870 UseMI->getOperand(UseIdx).setReg(ZeroReg);
871
872 if (DeleteDef)
873 DefMI->eraseFromParent();
874
875 return true;
876}
877
Hal Finkelda47e172013-04-10 18:30:16 +0000878static bool MBBDefinesCTR(MachineBasicBlock &MBB) {
879 for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end();
880 I != IE; ++I)
881 if (I->definesRegister(PPC::CTR) || I->definesRegister(PPC::CTR8))
882 return true;
883 return false;
884}
885
886// We should make sure that, if we're going to predicate both sides of a
887// condition (a diamond), that both sides don't define the counter register. We
888// can predicate counter-decrement-based branches, but while that predicates
889// the branching, it does not predicate the counter decrement. If we tried to
890// merge the triangle into one predicated block, we'd decrement the counter
891// twice.
892bool PPCInstrInfo::isProfitableToIfCvt(MachineBasicBlock &TMBB,
893 unsigned NumT, unsigned ExtraT,
894 MachineBasicBlock &FMBB,
895 unsigned NumF, unsigned ExtraF,
896 const BranchProbability &Probability) const {
897 return !(MBBDefinesCTR(TMBB) && MBBDefinesCTR(FMBB));
898}
899
900
Hal Finkel7eb0d812013-04-09 22:58:37 +0000901bool PPCInstrInfo::isPredicated(const MachineInstr *MI) const {
Hal Finkel4b040292013-04-11 01:23:34 +0000902 // The predicated branches are identified by their type, not really by the
903 // explicit presence of a predicate. Furthermore, some of them can be
904 // predicated more than once. Because if conversion won't try to predicate
905 // any instruction which already claims to be predicated (by returning true
906 // here), always return false. In doing so, we let isPredicable() be the
907 // final word on whether not the instruction can be (further) predicated.
908
909 return false;
Hal Finkel7eb0d812013-04-09 22:58:37 +0000910}
911
912bool PPCInstrInfo::isUnpredicatedTerminator(const MachineInstr *MI) const {
913 if (!MI->isTerminator())
914 return false;
915
916 // Conditional branch is a special case.
917 if (MI->isBranch() && !MI->isBarrier())
918 return true;
919
920 return !isPredicated(MI);
921}
922
923bool PPCInstrInfo::PredicateInstruction(
924 MachineInstr *MI,
925 const SmallVectorImpl<MachineOperand> &Pred) const {
926 unsigned OpC = MI->getOpcode();
927 if (OpC == PPC::BLR) {
928 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) {
929 bool isPPC64 = TM.getSubtargetImpl()->isPPC64();
930 MI->setDesc(get(Pred[0].getImm() ?
931 (isPPC64 ? PPC::BDNZLR8 : PPC::BDNZLR) :
932 (isPPC64 ? PPC::BDZLR8 : PPC::BDZLR)));
933 } else {
934 MI->setDesc(get(PPC::BCLR));
935 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
936 .addImm(Pred[0].getImm())
937 .addReg(Pred[1].getReg());
938 }
939
940 return true;
941 } else if (OpC == PPC::B) {
942 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) {
943 bool isPPC64 = TM.getSubtargetImpl()->isPPC64();
944 MI->setDesc(get(Pred[0].getImm() ?
945 (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
946 (isPPC64 ? PPC::BDZ8 : PPC::BDZ)));
947 } else {
948 MachineBasicBlock *MBB = MI->getOperand(0).getMBB();
949 MI->RemoveOperand(0);
950
951 MI->setDesc(get(PPC::BCC));
952 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
953 .addImm(Pred[0].getImm())
954 .addReg(Pred[1].getReg())
955 .addMBB(MBB);
956 }
957
958 return true;
Hal Finkel90dd7fd2013-04-10 06:42:34 +0000959 } else if (OpC == PPC::BCTR || OpC == PPC::BCTR8 ||
960 OpC == PPC::BCTRL || OpC == PPC::BCTRL8) {
961 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR)
962 llvm_unreachable("Cannot predicate bctr[l] on the ctr register");
963
964 bool setLR = OpC == PPC::BCTRL || OpC == PPC::BCTRL8;
965 bool isPPC64 = TM.getSubtargetImpl()->isPPC64();
966 MI->setDesc(get(isPPC64 ? (setLR ? PPC::BCCTRL8 : PPC::BCCTR8) :
967 (setLR ? PPC::BCCTRL : PPC::BCCTR)));
968 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
969 .addImm(Pred[0].getImm())
970 .addReg(Pred[1].getReg());
971 return true;
Hal Finkel7eb0d812013-04-09 22:58:37 +0000972 }
973
974 return false;
975}
976
977bool PPCInstrInfo::SubsumesPredicate(
978 const SmallVectorImpl<MachineOperand> &Pred1,
979 const SmallVectorImpl<MachineOperand> &Pred2) const {
980 assert(Pred1.size() == 2 && "Invalid PPC first predicate");
981 assert(Pred2.size() == 2 && "Invalid PPC second predicate");
982
983 if (Pred1[1].getReg() == PPC::CTR8 || Pred1[1].getReg() == PPC::CTR)
984 return false;
985 if (Pred2[1].getReg() == PPC::CTR8 || Pred2[1].getReg() == PPC::CTR)
986 return false;
987
988 PPC::Predicate P1 = (PPC::Predicate) Pred1[0].getImm();
989 PPC::Predicate P2 = (PPC::Predicate) Pred2[0].getImm();
990
991 if (P1 == P2)
992 return true;
993
994 // Does P1 subsume P2, e.g. GE subsumes GT.
995 if (P1 == PPC::PRED_LE &&
996 (P2 == PPC::PRED_LT || P2 == PPC::PRED_EQ))
997 return true;
998 if (P1 == PPC::PRED_GE &&
999 (P2 == PPC::PRED_GT || P2 == PPC::PRED_EQ))
1000 return true;
1001
1002 return false;
1003}
1004
1005bool PPCInstrInfo::DefinesPredicate(MachineInstr *MI,
1006 std::vector<MachineOperand> &Pred) const {
1007 // Note: At the present time, the contents of Pred from this function is
1008 // unused by IfConversion. This implementation follows ARM by pushing the
1009 // CR-defining operand. Because the 'DZ' and 'DNZ' count as types of
1010 // predicate, instructions defining CTR or CTR8 are also included as
1011 // predicate-defining instructions.
1012
1013 const TargetRegisterClass *RCs[] =
1014 { &PPC::CRRCRegClass, &PPC::CRBITRCRegClass,
1015 &PPC::CTRRCRegClass, &PPC::CTRRC8RegClass };
1016
1017 bool Found = false;
1018 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1019 const MachineOperand &MO = MI->getOperand(i);
Hal Finkel4e317282013-04-10 07:17:47 +00001020 for (unsigned c = 0; c < array_lengthof(RCs) && !Found; ++c) {
Hal Finkel7eb0d812013-04-09 22:58:37 +00001021 const TargetRegisterClass *RC = RCs[c];
Hal Finkel4e317282013-04-10 07:17:47 +00001022 if (MO.isReg()) {
1023 if (MO.isDef() && RC->contains(MO.getReg())) {
Hal Finkel7eb0d812013-04-09 22:58:37 +00001024 Pred.push_back(MO);
1025 Found = true;
1026 }
Hal Finkel4e317282013-04-10 07:17:47 +00001027 } else if (MO.isRegMask()) {
1028 for (TargetRegisterClass::iterator I = RC->begin(),
1029 IE = RC->end(); I != IE; ++I)
1030 if (MO.clobbersPhysReg(*I)) {
1031 Pred.push_back(MO);
1032 Found = true;
1033 }
Hal Finkel7eb0d812013-04-09 22:58:37 +00001034 }
1035 }
1036 }
1037
1038 return Found;
1039}
1040
1041bool PPCInstrInfo::isPredicable(MachineInstr *MI) const {
1042 unsigned OpC = MI->getOpcode();
1043 switch (OpC) {
1044 default:
1045 return false;
1046 case PPC::B:
1047 case PPC::BLR:
Hal Finkel90dd7fd2013-04-10 06:42:34 +00001048 case PPC::BCTR:
1049 case PPC::BCTR8:
1050 case PPC::BCTRL:
1051 case PPC::BCTRL8:
Hal Finkel7eb0d812013-04-09 22:58:37 +00001052 return true;
1053 }
1054}
1055
Hal Finkel860c08c2013-04-18 22:15:08 +00001056bool PPCInstrInfo::analyzeCompare(const MachineInstr *MI,
1057 unsigned &SrcReg, unsigned &SrcReg2,
1058 int &Mask, int &Value) const {
1059 unsigned Opc = MI->getOpcode();
1060
1061 switch (Opc) {
1062 default: return false;
1063 case PPC::CMPWI:
1064 case PPC::CMPLWI:
1065 case PPC::CMPDI:
1066 case PPC::CMPLDI:
1067 SrcReg = MI->getOperand(1).getReg();
1068 SrcReg2 = 0;
1069 Value = MI->getOperand(2).getImm();
1070 Mask = 0xFFFF;
1071 return true;
1072 case PPC::CMPW:
1073 case PPC::CMPLW:
1074 case PPC::CMPD:
1075 case PPC::CMPLD:
1076 case PPC::FCMPUS:
1077 case PPC::FCMPUD:
1078 SrcReg = MI->getOperand(1).getReg();
1079 SrcReg2 = MI->getOperand(2).getReg();
1080 return true;
1081 }
1082}
Hal Finkel87c1e422013-04-19 22:08:38 +00001083
Hal Finkel860c08c2013-04-18 22:15:08 +00001084bool PPCInstrInfo::optimizeCompareInstr(MachineInstr *CmpInstr,
1085 unsigned SrcReg, unsigned SrcReg2,
1086 int Mask, int Value,
1087 const MachineRegisterInfo *MRI) const {
Hal Finkel4029c3f2013-04-18 22:54:25 +00001088 if (DisableCmpOpt)
1089 return false;
1090
Hal Finkel860c08c2013-04-18 22:15:08 +00001091 int OpC = CmpInstr->getOpcode();
1092 unsigned CRReg = CmpInstr->getOperand(0).getReg();
Hal Finkelb45eb9f2013-05-08 12:16:14 +00001093
1094 // FP record forms set CR1 based on the execption status bits, not a
1095 // comparison with zero.
1096 if (OpC == PPC::FCMPUS || OpC == PPC::FCMPUD)
1097 return false;
Hal Finkel860c08c2013-04-18 22:15:08 +00001098
1099 // The record forms set the condition register based on a signed comparison
1100 // with zero (so says the ISA manual). This is not as straightforward as it
1101 // seems, however, because this is always a 64-bit comparison on PPC64, even
1102 // for instructions that are 32-bit in nature (like slw for example).
1103 // So, on PPC32, for unsigned comparisons, we can use the record forms only
1104 // for equality checks (as those don't depend on the sign). On PPC64,
1105 // we are restricted to equality for unsigned 64-bit comparisons and for
1106 // signed 32-bit comparisons the applicability is more restricted.
1107 bool isPPC64 = TM.getSubtargetImpl()->isPPC64();
1108 bool is32BitSignedCompare = OpC == PPC::CMPWI || OpC == PPC::CMPW;
1109 bool is32BitUnsignedCompare = OpC == PPC::CMPLWI || OpC == PPC::CMPLW;
1110 bool is64BitUnsignedCompare = OpC == PPC::CMPLDI || OpC == PPC::CMPLD;
1111
1112 // Get the unique definition of SrcReg.
1113 MachineInstr *MI = MRI->getUniqueVRegDef(SrcReg);
1114 if (!MI) return false;
1115 int MIOpC = MI->getOpcode();
1116
1117 bool equalityOnly = false;
1118 bool noSub = false;
1119 if (isPPC64) {
1120 if (is32BitSignedCompare) {
1121 // We can perform this optimization only if MI is sign-extending.
1122 if (MIOpC == PPC::SRAW || MIOpC == PPC::SRAWo ||
1123 MIOpC == PPC::SRAWI || MIOpC == PPC::SRAWIo ||
1124 MIOpC == PPC::EXTSB || MIOpC == PPC::EXTSBo ||
1125 MIOpC == PPC::EXTSH || MIOpC == PPC::EXTSHo ||
1126 MIOpC == PPC::EXTSW || MIOpC == PPC::EXTSWo) {
1127 noSub = true;
1128 } else
1129 return false;
1130 } else if (is32BitUnsignedCompare) {
1131 // We can perform this optimization, equality only, if MI is
1132 // zero-extending.
1133 if (MIOpC == PPC::CNTLZW || MIOpC == PPC::CNTLZWo ||
1134 MIOpC == PPC::SLW || MIOpC == PPC::SLWo ||
1135 MIOpC == PPC::SRW || MIOpC == PPC::SRWo) {
1136 noSub = true;
1137 equalityOnly = true;
1138 } else
1139 return false;
Hal Finkelb45eb9f2013-05-08 12:16:14 +00001140 } else
Hal Finkel860c08c2013-04-18 22:15:08 +00001141 equalityOnly = is64BitUnsignedCompare;
Hal Finkelb45eb9f2013-05-08 12:16:14 +00001142 } else
Hal Finkel860c08c2013-04-18 22:15:08 +00001143 equalityOnly = is32BitUnsignedCompare;
1144
1145 if (equalityOnly) {
1146 // We need to check the uses of the condition register in order to reject
1147 // non-equality comparisons.
1148 for (MachineRegisterInfo::use_iterator I = MRI->use_begin(CRReg),
1149 IE = MRI->use_end(); I != IE; ++I) {
1150 MachineInstr *UseMI = &*I;
1151 if (UseMI->getOpcode() == PPC::BCC) {
1152 unsigned Pred = UseMI->getOperand(0).getImm();
Hal Finkel8a88cad2013-05-07 17:49:55 +00001153 if (Pred != PPC::PRED_EQ && Pred != PPC::PRED_NE)
1154 return false;
Hal Finkel860c08c2013-04-18 22:15:08 +00001155 } else if (UseMI->getOpcode() == PPC::ISEL ||
1156 UseMI->getOpcode() == PPC::ISEL8) {
1157 unsigned SubIdx = UseMI->getOperand(3).getSubReg();
Hal Finkel8a88cad2013-05-07 17:49:55 +00001158 if (SubIdx != PPC::sub_eq)
1159 return false;
Hal Finkel860c08c2013-04-18 22:15:08 +00001160 } else
1161 return false;
1162 }
1163 }
1164
Hal Finkel8a88cad2013-05-07 17:49:55 +00001165 MachineBasicBlock::iterator I = CmpInstr;
Hal Finkel860c08c2013-04-18 22:15:08 +00001166
1167 // Scan forward to find the first use of the compare.
1168 for (MachineBasicBlock::iterator EL = CmpInstr->getParent()->end();
1169 I != EL; ++I) {
1170 bool FoundUse = false;
1171 for (MachineRegisterInfo::use_iterator J = MRI->use_begin(CRReg),
1172 JE = MRI->use_end(); J != JE; ++J)
1173 if (&*J == &*I) {
1174 FoundUse = true;
1175 break;
1176 }
1177
1178 if (FoundUse)
1179 break;
1180 }
1181
Hal Finkel860c08c2013-04-18 22:15:08 +00001182 // There are two possible candidates which can be changed to set CR[01].
1183 // One is MI, the other is a SUB instruction.
1184 // For CMPrr(r1,r2), we are looking for SUB(r1,r2) or SUB(r2,r1).
1185 MachineInstr *Sub = NULL;
1186 if (SrcReg2 != 0)
1187 // MI is not a candidate for CMPrr.
1188 MI = NULL;
1189 // FIXME: Conservatively refuse to convert an instruction which isn't in the
1190 // same BB as the comparison. This is to allow the check below to avoid calls
1191 // (and other explicit clobbers); instead we should really check for these
1192 // more explicitly (in at least a few predecessors).
1193 else if (MI->getParent() != CmpInstr->getParent() || Value != 0) {
1194 // PPC does not have a record-form SUBri.
1195 return false;
1196 }
1197
1198 // Search for Sub.
1199 const TargetRegisterInfo *TRI = &getRegisterInfo();
1200 --I;
Hal Finkel8a88cad2013-05-07 17:49:55 +00001201
1202 // Get ready to iterate backward from CmpInstr.
1203 MachineBasicBlock::iterator E = MI,
1204 B = CmpInstr->getParent()->begin();
1205
Hal Finkel860c08c2013-04-18 22:15:08 +00001206 for (; I != E && !noSub; --I) {
1207 const MachineInstr &Instr = *I;
1208 unsigned IOpC = Instr.getOpcode();
1209
1210 if (&*I != CmpInstr && (
Hal Finkelb45eb9f2013-05-08 12:16:14 +00001211 Instr.modifiesRegister(PPC::CR0, TRI) ||
1212 Instr.readsRegister(PPC::CR0, TRI)))
Hal Finkel860c08c2013-04-18 22:15:08 +00001213 // This instruction modifies or uses the record condition register after
1214 // the one we want to change. While we could do this transformation, it
1215 // would likely not be profitable. This transformation removes one
1216 // instruction, and so even forcing RA to generate one move probably
1217 // makes it unprofitable.
1218 return false;
1219
1220 // Check whether CmpInstr can be made redundant by the current instruction.
1221 if ((OpC == PPC::CMPW || OpC == PPC::CMPLW ||
1222 OpC == PPC::CMPD || OpC == PPC::CMPLD) &&
1223 (IOpC == PPC::SUBF || IOpC == PPC::SUBF8) &&
1224 ((Instr.getOperand(1).getReg() == SrcReg &&
1225 Instr.getOperand(2).getReg() == SrcReg2) ||
1226 (Instr.getOperand(1).getReg() == SrcReg2 &&
1227 Instr.getOperand(2).getReg() == SrcReg))) {
1228 Sub = &*I;
1229 break;
1230 }
1231
Hal Finkel860c08c2013-04-18 22:15:08 +00001232 if (I == B)
1233 // The 'and' is below the comparison instruction.
1234 return false;
1235 }
1236
1237 // Return false if no candidates exist.
1238 if (!MI && !Sub)
1239 return false;
1240
1241 // The single candidate is called MI.
1242 if (!MI) MI = Sub;
1243
1244 int NewOpC = -1;
1245 MIOpC = MI->getOpcode();
1246 if (MIOpC == PPC::ANDIo || MIOpC == PPC::ANDIo8)
1247 NewOpC = MIOpC;
1248 else {
1249 NewOpC = PPC::getRecordFormOpcode(MIOpC);
1250 if (NewOpC == -1 && PPC::getNonRecordFormOpcode(MIOpC) != -1)
1251 NewOpC = MIOpC;
1252 }
1253
1254 // FIXME: On the non-embedded POWER architectures, only some of the record
1255 // forms are fast, and we should use only the fast ones.
1256
1257 // The defining instruction has a record form (or is already a record
1258 // form). It is possible, however, that we'll need to reverse the condition
1259 // code of the users.
1260 if (NewOpC == -1)
1261 return false;
1262
Hal Finkel87c1e422013-04-19 22:08:38 +00001263 SmallVector<std::pair<MachineOperand*, PPC::Predicate>, 4> PredsToUpdate;
1264 SmallVector<std::pair<MachineOperand*, unsigned>, 4> SubRegsToUpdate;
Hal Finkel860c08c2013-04-18 22:15:08 +00001265
1266 // If we have SUB(r1, r2) and CMP(r2, r1), the condition code based on CMP
1267 // needs to be updated to be based on SUB. Push the condition code
1268 // operands to OperandsToUpdate. If it is safe to remove CmpInstr, the
1269 // condition code of these operands will be modified.
1270 bool ShouldSwap = false;
1271 if (Sub) {
1272 ShouldSwap = SrcReg2 != 0 && Sub->getOperand(1).getReg() == SrcReg2 &&
1273 Sub->getOperand(2).getReg() == SrcReg;
1274
1275 // The operands to subf are the opposite of sub, so only in the fixed-point
1276 // case, invert the order.
Hal Finkelb45eb9f2013-05-08 12:16:14 +00001277 ShouldSwap = !ShouldSwap;
Hal Finkel860c08c2013-04-18 22:15:08 +00001278 }
1279
1280 if (ShouldSwap)
1281 for (MachineRegisterInfo::use_iterator I = MRI->use_begin(CRReg),
1282 IE = MRI->use_end(); I != IE; ++I) {
1283 MachineInstr *UseMI = &*I;
1284 if (UseMI->getOpcode() == PPC::BCC) {
1285 PPC::Predicate Pred = (PPC::Predicate) UseMI->getOperand(0).getImm();
Hal Finkel87c1e422013-04-19 22:08:38 +00001286 assert((!equalityOnly ||
1287 Pred == PPC::PRED_EQ || Pred == PPC::PRED_NE) &&
1288 "Invalid predicate for equality-only optimization");
1289 PredsToUpdate.push_back(std::make_pair(&((*I).getOperand(0)),
Hal Finkelabe64dc2013-04-20 05:16:26 +00001290 PPC::getSwappedPredicate(Pred)));
Hal Finkel860c08c2013-04-18 22:15:08 +00001291 } else if (UseMI->getOpcode() == PPC::ISEL ||
1292 UseMI->getOpcode() == PPC::ISEL8) {
Hal Finkel87c1e422013-04-19 22:08:38 +00001293 unsigned NewSubReg = UseMI->getOperand(3).getSubReg();
1294 assert((!equalityOnly || NewSubReg == PPC::sub_eq) &&
1295 "Invalid CR bit for equality-only optimization");
1296
1297 if (NewSubReg == PPC::sub_lt)
1298 NewSubReg = PPC::sub_gt;
1299 else if (NewSubReg == PPC::sub_gt)
1300 NewSubReg = PPC::sub_lt;
1301
1302 SubRegsToUpdate.push_back(std::make_pair(&((*I).getOperand(3)),
1303 NewSubReg));
Hal Finkel860c08c2013-04-18 22:15:08 +00001304 } else // We need to abort on a user we don't understand.
1305 return false;
1306 }
1307
1308 // Create a new virtual register to hold the value of the CR set by the
1309 // record-form instruction. If the instruction was not previously in
1310 // record form, then set the kill flag on the CR.
1311 CmpInstr->eraseFromParent();
1312
1313 MachineBasicBlock::iterator MII = MI;
1314 BuildMI(*MI->getParent(), llvm::next(MII), MI->getDebugLoc(),
1315 get(TargetOpcode::COPY), CRReg)
Hal Finkelb45eb9f2013-05-08 12:16:14 +00001316 .addReg(PPC::CR0, MIOpC != NewOpC ? RegState::Kill : 0);
Hal Finkel860c08c2013-04-18 22:15:08 +00001317
1318 if (MIOpC != NewOpC) {
1319 // We need to be careful here: we're replacing one instruction with
1320 // another, and we need to make sure that we get all of the right
1321 // implicit uses and defs. On the other hand, the caller may be holding
1322 // an iterator to this instruction, and so we can't delete it (this is
1323 // specifically the case if this is the instruction directly after the
1324 // compare).
1325
1326 const MCInstrDesc &NewDesc = get(NewOpC);
1327 MI->setDesc(NewDesc);
1328
1329 if (NewDesc.ImplicitDefs)
1330 for (const uint16_t *ImpDefs = NewDesc.getImplicitDefs();
1331 *ImpDefs; ++ImpDefs)
1332 if (!MI->definesRegister(*ImpDefs))
1333 MI->addOperand(*MI->getParent()->getParent(),
1334 MachineOperand::CreateReg(*ImpDefs, true, true));
1335 if (NewDesc.ImplicitUses)
1336 for (const uint16_t *ImpUses = NewDesc.getImplicitUses();
1337 *ImpUses; ++ImpUses)
1338 if (!MI->readsRegister(*ImpUses))
1339 MI->addOperand(*MI->getParent()->getParent(),
1340 MachineOperand::CreateReg(*ImpUses, false, true));
1341 }
1342
1343 // Modify the condition code of operands in OperandsToUpdate.
1344 // Since we have SUB(r1, r2) and CMP(r2, r1), the condition code needs to
1345 // be changed from r2 > r1 to r1 < r2, from r2 < r1 to r1 > r2, etc.
Hal Finkel87c1e422013-04-19 22:08:38 +00001346 for (unsigned i = 0, e = PredsToUpdate.size(); i < e; i++)
1347 PredsToUpdate[i].first->setImm(PredsToUpdate[i].second);
Hal Finkel860c08c2013-04-18 22:15:08 +00001348
Hal Finkel87c1e422013-04-19 22:08:38 +00001349 for (unsigned i = 0, e = SubRegsToUpdate.size(); i < e; i++)
1350 SubRegsToUpdate[i].first->setSubReg(SubRegsToUpdate[i].second);
Hal Finkel860c08c2013-04-18 22:15:08 +00001351
1352 return true;
1353}
1354
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00001355/// GetInstSize - Return the number of bytes of code the specified
1356/// instruction may be. This returns the maximum number of bytes.
1357///
1358unsigned PPCInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
1359 switch (MI->getOpcode()) {
1360 case PPC::INLINEASM: { // Inline Asm: Variable size.
1361 const MachineFunction *MF = MI->getParent()->getParent();
1362 const char *AsmStr = MI->getOperand(0).getSymbolName();
Chris Lattneraf76e592009-08-22 20:48:53 +00001363 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00001364 }
Bill Wendling7431bea2010-07-16 22:20:36 +00001365 case PPC::PROLOG_LABEL:
Dan Gohman44066042008-07-01 00:05:16 +00001366 case PPC::EH_LABEL:
1367 case PPC::GC_LABEL:
Dale Johannesen375be772010-04-07 19:51:44 +00001368 case PPC::DBG_VALUE:
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00001369 return 0;
Ulrich Weigand86765fb2013-03-22 15:24:13 +00001370 case PPC::BL8_NOP:
1371 case PPC::BLA8_NOP:
Hal Finkel5b00cea2012-03-31 14:45:15 +00001372 return 8;
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00001373 default:
1374 return 4; // PowerPC instructions are all 4 bytes
1375 }
1376}
Hal Finkel5ee67e82013-04-08 16:24:03 +00001377
1378#undef DEBUG_TYPE
1379#define DEBUG_TYPE "ppc-early-ret"
1380STATISTIC(NumBCLR, "Number of early conditional returns");
1381STATISTIC(NumBLR, "Number of early returns");
1382
1383namespace llvm {
1384 void initializePPCEarlyReturnPass(PassRegistry&);
1385}
1386
1387namespace {
1388 // PPCEarlyReturn pass - For simple functions without epilogue code, move
1389 // returns up, and create conditional returns, to avoid unnecessary
1390 // branch-to-blr sequences.
1391 struct PPCEarlyReturn : public MachineFunctionPass {
1392 static char ID;
1393 PPCEarlyReturn() : MachineFunctionPass(ID) {
1394 initializePPCEarlyReturnPass(*PassRegistry::getPassRegistry());
1395 }
1396
1397 const PPCTargetMachine *TM;
1398 const PPCInstrInfo *TII;
1399
1400protected:
Hal Finkel13049ae2013-04-09 18:25:18 +00001401 bool processBlock(MachineBasicBlock &ReturnMBB) {
Hal Finkel5ee67e82013-04-08 16:24:03 +00001402 bool Changed = false;
1403
Hal Finkel13049ae2013-04-09 18:25:18 +00001404 MachineBasicBlock::iterator I = ReturnMBB.begin();
1405 I = ReturnMBB.SkipPHIsAndLabels(I);
Hal Finkel5ee67e82013-04-08 16:24:03 +00001406
1407 // The block must be essentially empty except for the blr.
Hal Finkel13049ae2013-04-09 18:25:18 +00001408 if (I == ReturnMBB.end() || I->getOpcode() != PPC::BLR ||
1409 I != ReturnMBB.getLastNonDebugInstr())
Hal Finkel5ee67e82013-04-08 16:24:03 +00001410 return Changed;
1411
1412 SmallVector<MachineBasicBlock*, 8> PredToRemove;
Hal Finkel13049ae2013-04-09 18:25:18 +00001413 for (MachineBasicBlock::pred_iterator PI = ReturnMBB.pred_begin(),
1414 PIE = ReturnMBB.pred_end(); PI != PIE; ++PI) {
Hal Finkel5ee67e82013-04-08 16:24:03 +00001415 bool OtherReference = false, BlockChanged = false;
Hal Finkel13049ae2013-04-09 18:25:18 +00001416 for (MachineBasicBlock::iterator J = (*PI)->getLastNonDebugInstr();;) {
Hal Finkel5ee67e82013-04-08 16:24:03 +00001417 if (J->getOpcode() == PPC::B) {
Hal Finkel13049ae2013-04-09 18:25:18 +00001418 if (J->getOperand(0).getMBB() == &ReturnMBB) {
Hal Finkel5ee67e82013-04-08 16:24:03 +00001419 // This is an unconditional branch to the return. Replace the
1420 // branch with a blr.
1421 BuildMI(**PI, J, J->getDebugLoc(), TII->get(PPC::BLR));
Hal Finkel13049ae2013-04-09 18:25:18 +00001422 MachineBasicBlock::iterator K = J--;
Hal Finkel5ee67e82013-04-08 16:24:03 +00001423 K->eraseFromParent();
1424 BlockChanged = true;
1425 ++NumBLR;
1426 continue;
1427 }
1428 } else if (J->getOpcode() == PPC::BCC) {
Hal Finkel13049ae2013-04-09 18:25:18 +00001429 if (J->getOperand(2).getMBB() == &ReturnMBB) {
Hal Finkel5ee67e82013-04-08 16:24:03 +00001430 // This is a conditional branch to the return. Replace the branch
1431 // with a bclr.
1432 BuildMI(**PI, J, J->getDebugLoc(), TII->get(PPC::BCLR))
1433 .addImm(J->getOperand(0).getImm())
1434 .addReg(J->getOperand(1).getReg());
Hal Finkel13049ae2013-04-09 18:25:18 +00001435 MachineBasicBlock::iterator K = J--;
Hal Finkel5ee67e82013-04-08 16:24:03 +00001436 K->eraseFromParent();
1437 BlockChanged = true;
1438 ++NumBCLR;
1439 continue;
1440 }
1441 } else if (J->isBranch()) {
1442 if (J->isIndirectBranch()) {
Hal Finkel13049ae2013-04-09 18:25:18 +00001443 if (ReturnMBB.hasAddressTaken())
Hal Finkel5ee67e82013-04-08 16:24:03 +00001444 OtherReference = true;
1445 } else
1446 for (unsigned i = 0; i < J->getNumOperands(); ++i)
1447 if (J->getOperand(i).isMBB() &&
Hal Finkel13049ae2013-04-09 18:25:18 +00001448 J->getOperand(i).getMBB() == &ReturnMBB)
Hal Finkel5ee67e82013-04-08 16:24:03 +00001449 OtherReference = true;
Hal Finkel13049ae2013-04-09 18:25:18 +00001450 } else if (!J->isTerminator() && !J->isDebugValue())
1451 break;
Hal Finkel5ee67e82013-04-08 16:24:03 +00001452
Hal Finkel13049ae2013-04-09 18:25:18 +00001453 if (J == (*PI)->begin())
1454 break;
1455
1456 --J;
Hal Finkel5ee67e82013-04-08 16:24:03 +00001457 }
1458
Hal Finkel13049ae2013-04-09 18:25:18 +00001459 if ((*PI)->canFallThrough() && (*PI)->isLayoutSuccessor(&ReturnMBB))
Hal Finkel5ee67e82013-04-08 16:24:03 +00001460 OtherReference = true;
1461
1462 // Predecessors are stored in a vector and can't be removed here.
1463 if (!OtherReference && BlockChanged) {
1464 PredToRemove.push_back(*PI);
1465 }
1466
1467 if (BlockChanged)
1468 Changed = true;
1469 }
1470
1471 for (unsigned i = 0, ie = PredToRemove.size(); i != ie; ++i)
Hal Finkel13049ae2013-04-09 18:25:18 +00001472 PredToRemove[i]->removeSuccessor(&ReturnMBB);
Hal Finkel5ee67e82013-04-08 16:24:03 +00001473
Hal Finkel13049ae2013-04-09 18:25:18 +00001474 if (Changed && !ReturnMBB.hasAddressTaken()) {
Hal Finkel5ee67e82013-04-08 16:24:03 +00001475 // We now might be able to merge this blr-only block into its
1476 // by-layout predecessor.
Hal Finkel13049ae2013-04-09 18:25:18 +00001477 if (ReturnMBB.pred_size() == 1 &&
1478 (*ReturnMBB.pred_begin())->isLayoutSuccessor(&ReturnMBB)) {
Hal Finkel5ee67e82013-04-08 16:24:03 +00001479 // Move the blr into the preceding block.
Hal Finkel13049ae2013-04-09 18:25:18 +00001480 MachineBasicBlock &PrevMBB = **ReturnMBB.pred_begin();
1481 PrevMBB.splice(PrevMBB.end(), &ReturnMBB, I);
1482 PrevMBB.removeSuccessor(&ReturnMBB);
Hal Finkel5ee67e82013-04-08 16:24:03 +00001483 }
1484
Hal Finkel13049ae2013-04-09 18:25:18 +00001485 if (ReturnMBB.pred_empty())
1486 ReturnMBB.eraseFromParent();
Hal Finkel5ee67e82013-04-08 16:24:03 +00001487 }
1488
1489 return Changed;
1490 }
1491
1492public:
1493 virtual bool runOnMachineFunction(MachineFunction &MF) {
1494 TM = static_cast<const PPCTargetMachine *>(&MF.getTarget());
1495 TII = TM->getInstrInfo();
1496
1497 bool Changed = false;
1498
Hal Finkel13049ae2013-04-09 18:25:18 +00001499 // If the function does not have at least two blocks, then there is
Hal Finkel5ee67e82013-04-08 16:24:03 +00001500 // nothing to do.
1501 if (MF.size() < 2)
1502 return Changed;
1503
1504 for (MachineFunction::iterator I = MF.begin(); I != MF.end();) {
1505 MachineBasicBlock &B = *I++;
1506 if (processBlock(B))
1507 Changed = true;
1508 }
1509
1510 return Changed;
1511 }
1512
1513 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
1514 MachineFunctionPass::getAnalysisUsage(AU);
1515 }
1516 };
1517}
1518
1519INITIALIZE_PASS(PPCEarlyReturn, DEBUG_TYPE,
1520 "PowerPC Early-Return Creation", false, false)
1521
1522char PPCEarlyReturn::ID = 0;
1523FunctionPass*
1524llvm::createPPCEarlyReturnPass() { return new PPCEarlyReturn(); }
1525