blob: af0bdedf17f1d9071be864cc53865d071cdd3943 [file] [log] [blame]
Jia Liub22310f2012-02-18 12:03:15 +00001//===-- PPCInstrInfo.cpp - PowerPC Instruction Information ----------------===//
Misha Brukmanb4402432005-04-21 23:30:14 +00002//
Misha Brukman116f9272004-08-17 04:55:41 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb4402432005-04-21 23:30:14 +00007//
Misha Brukman116f9272004-08-17 04:55:41 +00008//===----------------------------------------------------------------------===//
9//
10// This file contains the PowerPC implementation of the TargetInstrInfo class.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner6f3b9542005-10-14 23:59:06 +000014#include "PPCInstrInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "MCTargetDesc/PPCPredicates.h"
Evan Chengc5e6d2f2011-07-11 03:57:24 +000016#include "PPC.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "PPCHazardRecognizers.h"
Owen Andersoneee14602008-01-01 21:11:32 +000018#include "PPCInstrBuilder.h"
Bill Wendling632ea652008-03-03 22:19:16 +000019#include "PPCMachineFunctionInfo.h"
Chris Lattner49cadab2006-06-17 00:01:04 +000020#include "PPCTargetMachine.h"
Hal Finkelb5aa7e52013-04-08 16:24:03 +000021#include "llvm/ADT/Statistic.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000022#include "llvm/ADT/STLExtras.h"
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +000023#include "llvm/CodeGen/MachineFrameInfo.h"
Hal Finkelb5aa7e52013-04-08 16:24:03 +000024#include "llvm/CodeGen/MachineFunctionPass.h"
Misha Brukman116f9272004-08-17 04:55:41 +000025#include "llvm/CodeGen/MachineInstrBuilder.h"
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +000026#include "llvm/CodeGen/MachineMemOperand.h"
Jakob Stoklund Olesenddbf7a82010-02-26 21:09:24 +000027#include "llvm/CodeGen/MachineRegisterInfo.h"
Hal Finkel9f9f8922012-04-01 19:22:40 +000028#include "llvm/CodeGen/PseudoSourceValue.h"
Evan Chengc5e6d2f2011-07-11 03:57:24 +000029#include "llvm/MC/MCAsmInfo.h"
Bill Wendling1af20ad2008-03-04 23:13:51 +000030#include "llvm/Support/CommandLine.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000031#include "llvm/Support/ErrorHandling.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000032#include "llvm/Support/TargetRegistry.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000033#include "llvm/Support/raw_ostream.h"
Misha Brukman116f9272004-08-17 04:55:41 +000034
Hal Finkel82656cb2013-04-18 22:15:08 +000035#define GET_INSTRMAP_INFO
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000036#define GET_INSTRINFO_CTOR_DTOR
Evan Cheng1e210d02011-06-28 20:07:07 +000037#include "PPCGenInstrInfo.inc"
38
Dan Gohman20857192010-04-15 17:20:57 +000039using namespace llvm;
Bill Wendling1af20ad2008-03-04 23:13:51 +000040
Hal Finkel821e0012012-06-08 15:38:25 +000041static cl::
Hal Finkelc6b5deb2012-06-08 19:19:53 +000042opt<bool> DisableCTRLoopAnal("disable-ppc-ctrloop-analysis", cl::Hidden,
43 cl::desc("Disable analysis for CTR loops"));
Hal Finkel821e0012012-06-08 15:38:25 +000044
Hal Finkele6322392013-04-19 22:08:38 +000045static cl::opt<bool> DisableCmpOpt("disable-ppc-cmp-opt",
Hal Finkelb12da6b2013-04-18 22:54:25 +000046cl::desc("Disable compare instruction optimization"), cl::Hidden);
47
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000048// Pin the vtable to this file.
49void PPCInstrInfo::anchor() {}
50
Chris Lattner49cadab2006-06-17 00:01:04 +000051PPCInstrInfo::PPCInstrInfo(PPCTargetMachine &tm)
Evan Cheng703a0fb2011-07-01 17:57:27 +000052 : PPCGenInstrInfo(PPC::ADJCALLSTACKDOWN, PPC::ADJCALLSTACKUP),
Bill Wendling5e7656b2013-06-07 07:55:53 +000053 TM(tm), RI(*TM.getSubtargetImpl()) {}
Chris Lattner49cadab2006-06-17 00:01:04 +000054
Andrew Trick10ffc2b2010-12-24 05:03:26 +000055/// CreateTargetHazardRecognizer - Return the hazard recognizer to use for
56/// this target when scheduling the DAG.
57ScheduleHazardRecognizer *PPCInstrInfo::CreateTargetHazardRecognizer(
58 const TargetMachine *TM,
59 const ScheduleDAG *DAG) const {
Hal Finkel6fa56972011-10-17 04:03:49 +000060 unsigned Directive = TM->getSubtarget<PPCSubtarget>().getDarwinDirective();
Hal Finkel742b5352012-08-28 16:12:39 +000061 if (Directive == PPC::DIR_440 || Directive == PPC::DIR_A2 ||
62 Directive == PPC::DIR_E500mc || Directive == PPC::DIR_E5500) {
Hal Finkel6f0ae782011-11-22 16:21:04 +000063 const InstrItineraryData *II = TM->getInstrItineraryData();
Hal Finkel563cc052013-12-02 23:52:46 +000064 return new ScoreboardHazardRecognizer(II, DAG);
Hal Finkel6fa56972011-10-17 04:03:49 +000065 }
Hal Finkel58ca3602011-12-02 04:58:02 +000066
Jakob Stoklund Olesen9de596e2012-11-28 02:35:17 +000067 return TargetInstrInfo::CreateTargetHazardRecognizer(TM, DAG);
Andrew Trick10ffc2b2010-12-24 05:03:26 +000068}
69
Hal Finkel58ca3602011-12-02 04:58:02 +000070/// CreateTargetPostRAHazardRecognizer - Return the postRA hazard recognizer
71/// to use for this target when scheduling the DAG.
72ScheduleHazardRecognizer *PPCInstrInfo::CreateTargetPostRAHazardRecognizer(
73 const InstrItineraryData *II,
74 const ScheduleDAG *DAG) const {
75 unsigned Directive = TM.getSubtarget<PPCSubtarget>().getDarwinDirective();
76
Hal Finkelceb1f122013-12-12 00:19:11 +000077 if (Directive == PPC::DIR_PWR7)
78 return new PPCDispatchGroupSBHazardRecognizer(II, DAG);
79
Hal Finkel58ca3602011-12-02 04:58:02 +000080 // Most subtargets use a PPC970 recognizer.
Hal Finkel742b5352012-08-28 16:12:39 +000081 if (Directive != PPC::DIR_440 && Directive != PPC::DIR_A2 &&
82 Directive != PPC::DIR_E500mc && Directive != PPC::DIR_E5500) {
Benjamin Kramerf0ec1992013-06-07 11:23:35 +000083 assert(TM.getInstrInfo() && "No InstrInfo?");
Hal Finkel58ca3602011-12-02 04:58:02 +000084
Bill Wendling5e7656b2013-06-07 07:55:53 +000085 return new PPCHazardRecognizer970(TM);
Hal Finkel58ca3602011-12-02 04:58:02 +000086 }
87
Hal Finkel563cc052013-12-02 23:52:46 +000088 return new ScoreboardHazardRecognizer(II, DAG);
Hal Finkel58ca3602011-12-02 04:58:02 +000089}
Jakob Stoklund Olesen0f855e42012-06-19 21:14:34 +000090
Hal Finkelceb1f122013-12-12 00:19:11 +000091
92int PPCInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
93 const MachineInstr *DefMI, unsigned DefIdx,
94 const MachineInstr *UseMI,
95 unsigned UseIdx) const {
96 int Latency = PPCGenInstrInfo::getOperandLatency(ItinData, DefMI, DefIdx,
97 UseMI, UseIdx);
98
99 const MachineOperand &DefMO = DefMI->getOperand(DefIdx);
100 unsigned Reg = DefMO.getReg();
101
102 const TargetRegisterInfo *TRI = &getRegisterInfo();
103 bool IsRegCR;
104 if (TRI->isVirtualRegister(Reg)) {
105 const MachineRegisterInfo *MRI =
106 &DefMI->getParent()->getParent()->getRegInfo();
107 IsRegCR = MRI->getRegClass(Reg)->hasSuperClassEq(&PPC::CRRCRegClass) ||
108 MRI->getRegClass(Reg)->hasSuperClassEq(&PPC::CRBITRCRegClass);
109 } else {
110 IsRegCR = PPC::CRRCRegClass.contains(Reg) ||
111 PPC::CRBITRCRegClass.contains(Reg);
112 }
113
114 if (UseMI->isBranch() && IsRegCR) {
115 if (Latency < 0)
116 Latency = getInstrLatency(ItinData, DefMI);
117
118 // On some cores, there is an additional delay between writing to a condition
119 // register, and using it from a branch.
120 unsigned Directive = TM.getSubtarget<PPCSubtarget>().getDarwinDirective();
121 switch (Directive) {
122 default: break;
123 case PPC::DIR_7400:
124 case PPC::DIR_750:
125 case PPC::DIR_970:
126 case PPC::DIR_E5500:
127 case PPC::DIR_PWR4:
128 case PPC::DIR_PWR5:
129 case PPC::DIR_PWR5X:
130 case PPC::DIR_PWR6:
131 case PPC::DIR_PWR6X:
132 case PPC::DIR_PWR7:
133 Latency += 2;
134 break;
135 }
136 }
137
138 return Latency;
139}
140
Jakob Stoklund Olesen0f855e42012-06-19 21:14:34 +0000141// Detect 32 -> 64-bit extensions where we may reuse the low sub-register.
142bool PPCInstrInfo::isCoalescableExtInstr(const MachineInstr &MI,
143 unsigned &SrcReg, unsigned &DstReg,
144 unsigned &SubIdx) const {
145 switch (MI.getOpcode()) {
146 default: return false;
147 case PPC::EXTSW:
148 case PPC::EXTSW_32_64:
149 SrcReg = MI.getOperand(1).getReg();
150 DstReg = MI.getOperand(0).getReg();
151 SubIdx = PPC::sub_32;
152 return true;
153 }
154}
155
Andrew Trickc416ba62010-12-24 04:28:06 +0000156unsigned PPCInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
Chris Lattner91400bd2006-03-16 22:24:02 +0000157 int &FrameIndex) const {
Hal Finkel37714b82013-03-27 21:21:15 +0000158 // Note: This list must be kept consistent with LoadRegFromStackSlot.
Chris Lattnerbb53acd2006-02-02 20:12:32 +0000159 switch (MI->getOpcode()) {
160 default: break;
161 case PPC::LD:
162 case PPC::LWZ:
163 case PPC::LFS:
164 case PPC::LFD:
Hal Finkel37714b82013-03-27 21:21:15 +0000165 case PPC::RESTORE_CR:
166 case PPC::LVX:
167 case PPC::RESTORE_VRSAVE:
168 // Check for the operands added by addFrameReference (the immediate is the
169 // offset which defaults to 0).
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000170 if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
171 MI->getOperand(2).isFI()) {
Chris Lattnera5bb3702007-12-30 23:10:15 +0000172 FrameIndex = MI->getOperand(2).getIndex();
Chris Lattnerbb53acd2006-02-02 20:12:32 +0000173 return MI->getOperand(0).getReg();
174 }
175 break;
176 }
177 return 0;
Chris Lattnerc327d712006-02-02 20:16:12 +0000178}
Chris Lattnerbb53acd2006-02-02 20:12:32 +0000179
Andrew Trickc416ba62010-12-24 04:28:06 +0000180unsigned PPCInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
Chris Lattnerc327d712006-02-02 20:16:12 +0000181 int &FrameIndex) const {
Hal Finkel37714b82013-03-27 21:21:15 +0000182 // Note: This list must be kept consistent with StoreRegToStackSlot.
Chris Lattnerc327d712006-02-02 20:16:12 +0000183 switch (MI->getOpcode()) {
184 default: break;
Nate Begeman4efb3282006-02-02 21:07:50 +0000185 case PPC::STD:
Chris Lattnerc327d712006-02-02 20:16:12 +0000186 case PPC::STW:
187 case PPC::STFS:
188 case PPC::STFD:
Hal Finkel37714b82013-03-27 21:21:15 +0000189 case PPC::SPILL_CR:
190 case PPC::STVX:
191 case PPC::SPILL_VRSAVE:
192 // Check for the operands added by addFrameReference (the immediate is the
193 // offset which defaults to 0).
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000194 if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
195 MI->getOperand(2).isFI()) {
Chris Lattnera5bb3702007-12-30 23:10:15 +0000196 FrameIndex = MI->getOperand(2).getIndex();
Chris Lattnerc327d712006-02-02 20:16:12 +0000197 return MI->getOperand(0).getReg();
198 }
199 break;
200 }
201 return 0;
202}
Chris Lattnerbb53acd2006-02-02 20:12:32 +0000203
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000204// commuteInstruction - We can commute rlwimi instructions, but only if the
205// rotate amt is zero. We also have to munge the immediates a bit.
Evan Cheng03553bb2008-06-16 07:33:11 +0000206MachineInstr *
207PPCInstrInfo::commuteInstruction(MachineInstr *MI, bool NewMI) const {
Dan Gohman3b460302008-07-07 23:14:23 +0000208 MachineFunction &MF = *MI->getParent()->getParent();
209
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000210 // Normal instructions can be commuted the obvious way.
Hal Finkel654d43b2013-04-12 02:18:09 +0000211 if (MI->getOpcode() != PPC::RLWIMI &&
212 MI->getOpcode() != PPC::RLWIMIo)
Jakob Stoklund Olesen9de596e2012-11-28 02:35:17 +0000213 return TargetInstrInfo::commuteInstruction(MI, NewMI);
Andrew Trickc416ba62010-12-24 04:28:06 +0000214
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000215 // Cannot commute if it has a non-zero rotate count.
Chris Lattner5c463782007-12-30 20:49:49 +0000216 if (MI->getOperand(3).getImm() != 0)
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000217 return 0;
Andrew Trickc416ba62010-12-24 04:28:06 +0000218
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000219 // If we have a zero rotate count, we have:
220 // M = mask(MB,ME)
221 // Op0 = (Op1 & ~M) | (Op2 & M)
222 // Change this to:
223 // M = mask((ME+1)&31, (MB-1)&31)
224 // Op0 = (Op2 & ~M) | (Op1 & M)
225
226 // Swap op1/op2
Evan Cheng244183e2008-02-13 02:46:49 +0000227 unsigned Reg0 = MI->getOperand(0).getReg();
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000228 unsigned Reg1 = MI->getOperand(1).getReg();
229 unsigned Reg2 = MI->getOperand(2).getReg();
Evan Chengdc2c8742006-11-15 20:58:11 +0000230 bool Reg1IsKill = MI->getOperand(1).isKill();
231 bool Reg2IsKill = MI->getOperand(2).isKill();
Evan Cheng03553bb2008-06-16 07:33:11 +0000232 bool ChangeReg0 = false;
Evan Cheng244183e2008-02-13 02:46:49 +0000233 // If machine instrs are no longer in two-address forms, update
234 // destination register as well.
235 if (Reg0 == Reg1) {
236 // Must be two address instruction!
Evan Cheng6cc775f2011-06-28 19:10:37 +0000237 assert(MI->getDesc().getOperandConstraint(0, MCOI::TIED_TO) &&
Evan Cheng244183e2008-02-13 02:46:49 +0000238 "Expecting a two-address instruction!");
Evan Cheng244183e2008-02-13 02:46:49 +0000239 Reg2IsKill = false;
Evan Cheng03553bb2008-06-16 07:33:11 +0000240 ChangeReg0 = true;
Evan Cheng244183e2008-02-13 02:46:49 +0000241 }
Evan Cheng03553bb2008-06-16 07:33:11 +0000242
243 // Masks.
244 unsigned MB = MI->getOperand(4).getImm();
245 unsigned ME = MI->getOperand(5).getImm();
246
247 if (NewMI) {
248 // Create a new instruction.
249 unsigned Reg0 = ChangeReg0 ? Reg2 : MI->getOperand(0).getReg();
250 bool Reg0IsDead = MI->getOperand(0).isDead();
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000251 return BuildMI(MF, MI->getDebugLoc(), MI->getDesc())
Bill Wendlingf7b83c72009-05-13 21:33:08 +0000252 .addReg(Reg0, RegState::Define | getDeadRegState(Reg0IsDead))
253 .addReg(Reg2, getKillRegState(Reg2IsKill))
254 .addReg(Reg1, getKillRegState(Reg1IsKill))
Evan Cheng03553bb2008-06-16 07:33:11 +0000255 .addImm((ME+1) & 31)
256 .addImm((MB-1) & 31);
257 }
258
259 if (ChangeReg0)
260 MI->getOperand(0).setReg(Reg2);
Chris Lattner10d63412006-05-04 17:52:23 +0000261 MI->getOperand(2).setReg(Reg1);
262 MI->getOperand(1).setReg(Reg2);
Chris Lattner60055892007-12-30 21:56:09 +0000263 MI->getOperand(2).setIsKill(Reg1IsKill);
264 MI->getOperand(1).setIsKill(Reg2IsKill);
Andrew Trickc416ba62010-12-24 04:28:06 +0000265
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000266 // Swap the mask around.
Chris Lattner5c463782007-12-30 20:49:49 +0000267 MI->getOperand(4).setImm((ME+1) & 31);
268 MI->getOperand(5).setImm((MB-1) & 31);
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000269 return MI;
270}
Chris Lattnerea79d9fd732006-03-05 23:49:55 +0000271
Andrew Trickc416ba62010-12-24 04:28:06 +0000272void PPCInstrInfo::insertNoop(MachineBasicBlock &MBB,
Chris Lattnerea79d9fd732006-03-05 23:49:55 +0000273 MachineBasicBlock::iterator MI) const {
Hal Finkelceb1f122013-12-12 00:19:11 +0000274 // This function is used for scheduling, and the nop wanted here is the type
275 // that terminates dispatch groups on the POWER cores.
276 unsigned Directive = TM.getSubtarget<PPCSubtarget>().getDarwinDirective();
277 unsigned Opcode;
278 switch (Directive) {
279 default: Opcode = PPC::NOP; break;
280 case PPC::DIR_PWR6: Opcode = PPC::NOP_GT_PWR6; break;
281 case PPC::DIR_PWR7: Opcode = PPC::NOP_GT_PWR7; break;
282 }
Chris Lattnera47294ed2006-10-13 21:21:17 +0000283
Hal Finkelceb1f122013-12-12 00:19:11 +0000284 DebugLoc DL;
285 BuildMI(MBB, MI, DL, get(Opcode));
286}
Chris Lattnera47294ed2006-10-13 21:21:17 +0000287
288// Branch analysis.
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000289// Note: If the condition register is set to CTR or CTR8 then this is a
290// BDNZ (imm == 1) or BDZ (imm == 0) branch.
Chris Lattnera47294ed2006-10-13 21:21:17 +0000291bool PPCInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
292 MachineBasicBlock *&FBB,
Evan Cheng64dfcac2009-02-09 07:14:22 +0000293 SmallVectorImpl<MachineOperand> &Cond,
294 bool AllowModify) const {
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000295 bool isPPC64 = TM.getSubtargetImpl()->isPPC64();
296
Chris Lattnera47294ed2006-10-13 21:21:17 +0000297 // If the block has no terminators, it just falls into the block after it.
298 MachineBasicBlock::iterator I = MBB.end();
Dale Johannesen4244d122010-04-02 01:38:09 +0000299 if (I == MBB.begin())
300 return false;
301 --I;
302 while (I->isDebugValue()) {
303 if (I == MBB.begin())
304 return false;
305 --I;
306 }
307 if (!isUnpredicatedTerminator(I))
Chris Lattnera47294ed2006-10-13 21:21:17 +0000308 return false;
309
310 // Get the last instruction in the block.
311 MachineInstr *LastInst = I;
Andrew Trickc416ba62010-12-24 04:28:06 +0000312
Chris Lattnera47294ed2006-10-13 21:21:17 +0000313 // If there is only one terminator instruction, process it.
Evan Cheng5514bbe2007-06-08 21:59:56 +0000314 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
Chris Lattnera47294ed2006-10-13 21:21:17 +0000315 if (LastInst->getOpcode() == PPC::B) {
Evan Cheng8f43afd2009-05-08 23:09:25 +0000316 if (!LastInst->getOperand(0).isMBB())
317 return true;
Chris Lattnera5bb3702007-12-30 23:10:15 +0000318 TBB = LastInst->getOperand(0).getMBB();
Chris Lattnera47294ed2006-10-13 21:21:17 +0000319 return false;
Chris Lattnere0263792006-11-17 22:14:47 +0000320 } else if (LastInst->getOpcode() == PPC::BCC) {
Evan Cheng8f43afd2009-05-08 23:09:25 +0000321 if (!LastInst->getOperand(2).isMBB())
322 return true;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000323 // Block ends with fall-through condbranch.
Chris Lattnera5bb3702007-12-30 23:10:15 +0000324 TBB = LastInst->getOperand(2).getMBB();
Chris Lattnera47294ed2006-10-13 21:21:17 +0000325 Cond.push_back(LastInst->getOperand(0));
326 Cond.push_back(LastInst->getOperand(1));
Chris Lattner23f22de2006-10-21 06:03:11 +0000327 return false;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000328 } else if (LastInst->getOpcode() == PPC::BDNZ8 ||
329 LastInst->getOpcode() == PPC::BDNZ) {
330 if (!LastInst->getOperand(0).isMBB())
331 return true;
Hal Finkelc6b5deb2012-06-08 19:19:53 +0000332 if (DisableCTRLoopAnal)
Hal Finkel821e0012012-06-08 15:38:25 +0000333 return true;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000334 TBB = LastInst->getOperand(0).getMBB();
335 Cond.push_back(MachineOperand::CreateImm(1));
336 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
337 true));
338 return false;
339 } else if (LastInst->getOpcode() == PPC::BDZ8 ||
340 LastInst->getOpcode() == PPC::BDZ) {
341 if (!LastInst->getOperand(0).isMBB())
342 return true;
Hal Finkelc6b5deb2012-06-08 19:19:53 +0000343 if (DisableCTRLoopAnal)
Hal Finkel821e0012012-06-08 15:38:25 +0000344 return true;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000345 TBB = LastInst->getOperand(0).getMBB();
346 Cond.push_back(MachineOperand::CreateImm(0));
347 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
348 true));
349 return false;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000350 }
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000351
Chris Lattnera47294ed2006-10-13 21:21:17 +0000352 // Otherwise, don't know what this is.
353 return true;
354 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000355
Chris Lattnera47294ed2006-10-13 21:21:17 +0000356 // Get the instruction before it if it's a terminator.
357 MachineInstr *SecondLastInst = I;
358
359 // If there are three terminators, we don't know what sort of block this is.
360 if (SecondLastInst && I != MBB.begin() &&
Evan Cheng5514bbe2007-06-08 21:59:56 +0000361 isUnpredicatedTerminator(--I))
Chris Lattnera47294ed2006-10-13 21:21:17 +0000362 return true;
Andrew Trickc416ba62010-12-24 04:28:06 +0000363
Chris Lattnere0263792006-11-17 22:14:47 +0000364 // If the block ends with PPC::B and PPC:BCC, handle it.
Andrew Trickc416ba62010-12-24 04:28:06 +0000365 if (SecondLastInst->getOpcode() == PPC::BCC &&
Chris Lattnera47294ed2006-10-13 21:21:17 +0000366 LastInst->getOpcode() == PPC::B) {
Evan Cheng8f43afd2009-05-08 23:09:25 +0000367 if (!SecondLastInst->getOperand(2).isMBB() ||
368 !LastInst->getOperand(0).isMBB())
369 return true;
Chris Lattnera5bb3702007-12-30 23:10:15 +0000370 TBB = SecondLastInst->getOperand(2).getMBB();
Chris Lattnera47294ed2006-10-13 21:21:17 +0000371 Cond.push_back(SecondLastInst->getOperand(0));
372 Cond.push_back(SecondLastInst->getOperand(1));
Chris Lattnera5bb3702007-12-30 23:10:15 +0000373 FBB = LastInst->getOperand(0).getMBB();
Chris Lattnera47294ed2006-10-13 21:21:17 +0000374 return false;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000375 } else if ((SecondLastInst->getOpcode() == PPC::BDNZ8 ||
376 SecondLastInst->getOpcode() == PPC::BDNZ) &&
377 LastInst->getOpcode() == PPC::B) {
378 if (!SecondLastInst->getOperand(0).isMBB() ||
379 !LastInst->getOperand(0).isMBB())
380 return true;
Hal Finkelc6b5deb2012-06-08 19:19:53 +0000381 if (DisableCTRLoopAnal)
Hal Finkel821e0012012-06-08 15:38:25 +0000382 return true;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000383 TBB = SecondLastInst->getOperand(0).getMBB();
384 Cond.push_back(MachineOperand::CreateImm(1));
385 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
386 true));
387 FBB = LastInst->getOperand(0).getMBB();
388 return false;
389 } else if ((SecondLastInst->getOpcode() == PPC::BDZ8 ||
390 SecondLastInst->getOpcode() == PPC::BDZ) &&
391 LastInst->getOpcode() == PPC::B) {
392 if (!SecondLastInst->getOperand(0).isMBB() ||
393 !LastInst->getOperand(0).isMBB())
394 return true;
Hal Finkelc6b5deb2012-06-08 19:19:53 +0000395 if (DisableCTRLoopAnal)
Hal Finkel821e0012012-06-08 15:38:25 +0000396 return true;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000397 TBB = SecondLastInst->getOperand(0).getMBB();
398 Cond.push_back(MachineOperand::CreateImm(0));
399 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
400 true));
401 FBB = LastInst->getOperand(0).getMBB();
402 return false;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000403 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000404
Dale Johannesenc6855462007-06-13 17:59:52 +0000405 // If the block ends with two PPC:Bs, handle it. The second one is not
406 // executed, so remove it.
Andrew Trickc416ba62010-12-24 04:28:06 +0000407 if (SecondLastInst->getOpcode() == PPC::B &&
Dale Johannesenc6855462007-06-13 17:59:52 +0000408 LastInst->getOpcode() == PPC::B) {
Evan Cheng8f43afd2009-05-08 23:09:25 +0000409 if (!SecondLastInst->getOperand(0).isMBB())
410 return true;
Chris Lattnera5bb3702007-12-30 23:10:15 +0000411 TBB = SecondLastInst->getOperand(0).getMBB();
Dale Johannesenc6855462007-06-13 17:59:52 +0000412 I = LastInst;
Evan Cheng64dfcac2009-02-09 07:14:22 +0000413 if (AllowModify)
414 I->eraseFromParent();
Dale Johannesenc6855462007-06-13 17:59:52 +0000415 return false;
416 }
417
Chris Lattnera47294ed2006-10-13 21:21:17 +0000418 // Otherwise, can't handle this.
419 return true;
420}
421
Evan Cheng99be49d2007-05-18 00:05:48 +0000422unsigned PPCInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
Chris Lattnera47294ed2006-10-13 21:21:17 +0000423 MachineBasicBlock::iterator I = MBB.end();
Evan Cheng99be49d2007-05-18 00:05:48 +0000424 if (I == MBB.begin()) return 0;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000425 --I;
Dale Johannesen4244d122010-04-02 01:38:09 +0000426 while (I->isDebugValue()) {
427 if (I == MBB.begin())
428 return 0;
429 --I;
430 }
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000431 if (I->getOpcode() != PPC::B && I->getOpcode() != PPC::BCC &&
432 I->getOpcode() != PPC::BDNZ8 && I->getOpcode() != PPC::BDNZ &&
433 I->getOpcode() != PPC::BDZ8 && I->getOpcode() != PPC::BDZ)
Evan Cheng99be49d2007-05-18 00:05:48 +0000434 return 0;
Andrew Trickc416ba62010-12-24 04:28:06 +0000435
Chris Lattnera47294ed2006-10-13 21:21:17 +0000436 // Remove the branch.
437 I->eraseFromParent();
Andrew Trickc416ba62010-12-24 04:28:06 +0000438
Chris Lattnera47294ed2006-10-13 21:21:17 +0000439 I = MBB.end();
440
Evan Cheng99be49d2007-05-18 00:05:48 +0000441 if (I == MBB.begin()) return 1;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000442 --I;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000443 if (I->getOpcode() != PPC::BCC &&
444 I->getOpcode() != PPC::BDNZ8 && I->getOpcode() != PPC::BDNZ &&
445 I->getOpcode() != PPC::BDZ8 && I->getOpcode() != PPC::BDZ)
Evan Cheng99be49d2007-05-18 00:05:48 +0000446 return 1;
Andrew Trickc416ba62010-12-24 04:28:06 +0000447
Chris Lattnera47294ed2006-10-13 21:21:17 +0000448 // Remove the branch.
449 I->eraseFromParent();
Evan Cheng99be49d2007-05-18 00:05:48 +0000450 return 2;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000451}
452
Evan Cheng99be49d2007-05-18 00:05:48 +0000453unsigned
454PPCInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
455 MachineBasicBlock *FBB,
Stuart Hastings0125b642010-06-17 22:43:56 +0000456 const SmallVectorImpl<MachineOperand> &Cond,
457 DebugLoc DL) const {
Chris Lattnera61f0102006-10-17 18:06:55 +0000458 // Shouldn't be a fall through.
459 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Andrew Trickc416ba62010-12-24 04:28:06 +0000460 assert((Cond.size() == 2 || Cond.size() == 0) &&
Chris Lattner94e04442006-10-21 05:36:13 +0000461 "PPC branch conditions have two components!");
Andrew Trickc416ba62010-12-24 04:28:06 +0000462
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000463 bool isPPC64 = TM.getSubtargetImpl()->isPPC64();
464
Chris Lattner94e04442006-10-21 05:36:13 +0000465 // One-way branch.
Chris Lattnera61f0102006-10-17 18:06:55 +0000466 if (FBB == 0) {
Chris Lattner94e04442006-10-21 05:36:13 +0000467 if (Cond.empty()) // Unconditional branch
Stuart Hastings0125b642010-06-17 22:43:56 +0000468 BuildMI(&MBB, DL, get(PPC::B)).addMBB(TBB);
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000469 else if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
470 BuildMI(&MBB, DL, get(Cond[0].getImm() ?
471 (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
472 (isPPC64 ? PPC::BDZ8 : PPC::BDZ))).addMBB(TBB);
Chris Lattner94e04442006-10-21 05:36:13 +0000473 else // Conditional branch
Stuart Hastings0125b642010-06-17 22:43:56 +0000474 BuildMI(&MBB, DL, get(PPC::BCC))
Chris Lattnerbe9377a2006-11-17 22:37:34 +0000475 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
Evan Cheng99be49d2007-05-18 00:05:48 +0000476 return 1;
Chris Lattnera61f0102006-10-17 18:06:55 +0000477 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000478
Chris Lattnerd8816602006-10-21 05:42:09 +0000479 // Two-way Conditional Branch.
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000480 if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
481 BuildMI(&MBB, DL, get(Cond[0].getImm() ?
482 (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
483 (isPPC64 ? PPC::BDZ8 : PPC::BDZ))).addMBB(TBB);
484 else
485 BuildMI(&MBB, DL, get(PPC::BCC))
486 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
Stuart Hastings0125b642010-06-17 22:43:56 +0000487 BuildMI(&MBB, DL, get(PPC::B)).addMBB(FBB);
Evan Cheng99be49d2007-05-18 00:05:48 +0000488 return 2;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000489}
490
Hal Finkeled6a2852013-04-05 23:29:01 +0000491// Select analysis.
492bool PPCInstrInfo::canInsertSelect(const MachineBasicBlock &MBB,
493 const SmallVectorImpl<MachineOperand> &Cond,
494 unsigned TrueReg, unsigned FalseReg,
495 int &CondCycles, int &TrueCycles, int &FalseCycles) const {
496 if (!TM.getSubtargetImpl()->hasISEL())
497 return false;
498
499 if (Cond.size() != 2)
500 return false;
501
502 // If this is really a bdnz-like condition, then it cannot be turned into a
503 // select.
504 if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
505 return false;
506
507 // Check register classes.
508 const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
509 const TargetRegisterClass *RC =
510 RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg));
511 if (!RC)
512 return false;
513
514 // isel is for regular integer GPRs only.
515 if (!PPC::GPRCRegClass.hasSubClassEq(RC) &&
Hal Finkel8e8618a2013-07-15 20:22:58 +0000516 !PPC::GPRC_NOR0RegClass.hasSubClassEq(RC) &&
517 !PPC::G8RCRegClass.hasSubClassEq(RC) &&
518 !PPC::G8RC_NOX0RegClass.hasSubClassEq(RC))
Hal Finkeled6a2852013-04-05 23:29:01 +0000519 return false;
520
521 // FIXME: These numbers are for the A2, how well they work for other cores is
522 // an open question. On the A2, the isel instruction has a 2-cycle latency
523 // but single-cycle throughput. These numbers are used in combination with
524 // the MispredictPenalty setting from the active SchedMachineModel.
525 CondCycles = 1;
526 TrueCycles = 1;
527 FalseCycles = 1;
528
529 return true;
530}
531
532void PPCInstrInfo::insertSelect(MachineBasicBlock &MBB,
533 MachineBasicBlock::iterator MI, DebugLoc dl,
534 unsigned DestReg,
535 const SmallVectorImpl<MachineOperand> &Cond,
536 unsigned TrueReg, unsigned FalseReg) const {
537 assert(Cond.size() == 2 &&
538 "PPC branch conditions have two components!");
539
540 assert(TM.getSubtargetImpl()->hasISEL() &&
541 "Cannot insert select on target without ISEL support");
542
543 // Get the register classes.
544 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
545 const TargetRegisterClass *RC =
546 RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg));
547 assert(RC && "TrueReg and FalseReg must have overlapping register classes");
Hal Finkel8e8618a2013-07-15 20:22:58 +0000548
549 bool Is64Bit = PPC::G8RCRegClass.hasSubClassEq(RC) ||
550 PPC::G8RC_NOX0RegClass.hasSubClassEq(RC);
551 assert((Is64Bit ||
552 PPC::GPRCRegClass.hasSubClassEq(RC) ||
553 PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) &&
Hal Finkeled6a2852013-04-05 23:29:01 +0000554 "isel is for regular integer GPRs only");
555
Hal Finkel8e8618a2013-07-15 20:22:58 +0000556 unsigned OpCode = Is64Bit ? PPC::ISEL8 : PPC::ISEL;
Hal Finkeled6a2852013-04-05 23:29:01 +0000557 unsigned SelectPred = Cond[0].getImm();
558
559 unsigned SubIdx;
560 bool SwapOps;
561 switch (SelectPred) {
562 default: llvm_unreachable("invalid predicate for isel");
563 case PPC::PRED_EQ: SubIdx = PPC::sub_eq; SwapOps = false; break;
564 case PPC::PRED_NE: SubIdx = PPC::sub_eq; SwapOps = true; break;
565 case PPC::PRED_LT: SubIdx = PPC::sub_lt; SwapOps = false; break;
566 case PPC::PRED_GE: SubIdx = PPC::sub_lt; SwapOps = true; break;
567 case PPC::PRED_GT: SubIdx = PPC::sub_gt; SwapOps = false; break;
568 case PPC::PRED_LE: SubIdx = PPC::sub_gt; SwapOps = true; break;
569 case PPC::PRED_UN: SubIdx = PPC::sub_un; SwapOps = false; break;
570 case PPC::PRED_NU: SubIdx = PPC::sub_un; SwapOps = true; break;
571 }
572
573 unsigned FirstReg = SwapOps ? FalseReg : TrueReg,
574 SecondReg = SwapOps ? TrueReg : FalseReg;
575
576 // The first input register of isel cannot be r0. If it is a member
577 // of a register class that can be r0, then copy it first (the
578 // register allocator should eliminate the copy).
579 if (MRI.getRegClass(FirstReg)->contains(PPC::R0) ||
580 MRI.getRegClass(FirstReg)->contains(PPC::X0)) {
581 const TargetRegisterClass *FirstRC =
582 MRI.getRegClass(FirstReg)->contains(PPC::X0) ?
583 &PPC::G8RC_NOX0RegClass : &PPC::GPRC_NOR0RegClass;
584 unsigned OldFirstReg = FirstReg;
585 FirstReg = MRI.createVirtualRegister(FirstRC);
586 BuildMI(MBB, MI, dl, get(TargetOpcode::COPY), FirstReg)
587 .addReg(OldFirstReg);
588 }
589
590 BuildMI(MBB, MI, dl, get(OpCode), DestReg)
591 .addReg(FirstReg).addReg(SecondReg)
592 .addReg(Cond[1].getReg(), 0, SubIdx);
593}
594
Jakob Stoklund Olesen0d611972010-07-11 07:31:00 +0000595void PPCInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
596 MachineBasicBlock::iterator I, DebugLoc DL,
597 unsigned DestReg, unsigned SrcReg,
598 bool KillSrc) const {
599 unsigned Opc;
600 if (PPC::GPRCRegClass.contains(DestReg, SrcReg))
601 Opc = PPC::OR;
602 else if (PPC::G8RCRegClass.contains(DestReg, SrcReg))
603 Opc = PPC::OR8;
604 else if (PPC::F4RCRegClass.contains(DestReg, SrcReg))
605 Opc = PPC::FMR;
606 else if (PPC::CRRCRegClass.contains(DestReg, SrcReg))
607 Opc = PPC::MCRF;
608 else if (PPC::VRRCRegClass.contains(DestReg, SrcReg))
609 Opc = PPC::VOR;
610 else if (PPC::CRBITRCRegClass.contains(DestReg, SrcReg))
611 Opc = PPC::CROR;
612 else
613 llvm_unreachable("Impossible reg-to-reg copy");
Owen Anderson7a73ae92007-12-31 06:32:00 +0000614
Evan Cheng6cc775f2011-06-28 19:10:37 +0000615 const MCInstrDesc &MCID = get(Opc);
616 if (MCID.getNumOperands() == 3)
617 BuildMI(MBB, I, DL, MCID, DestReg)
Jakob Stoklund Olesen0d611972010-07-11 07:31:00 +0000618 .addReg(SrcReg).addReg(SrcReg, getKillRegState(KillSrc));
619 else
Evan Cheng6cc775f2011-06-28 19:10:37 +0000620 BuildMI(MBB, I, DL, MCID, DestReg).addReg(SrcReg, getKillRegState(KillSrc));
Owen Anderson7a73ae92007-12-31 06:32:00 +0000621}
622
Hal Finkel8f6834d2011-12-05 17:55:17 +0000623// This function returns true if a CR spill is necessary and false otherwise.
Bill Wendlingc6c48fc2008-03-10 22:49:16 +0000624bool
Dan Gohman3b460302008-07-07 23:14:23 +0000625PPCInstrInfo::StoreRegToStackSlot(MachineFunction &MF,
626 unsigned SrcReg, bool isKill,
Bill Wendlingc6c48fc2008-03-10 22:49:16 +0000627 int FrameIdx,
628 const TargetRegisterClass *RC,
Hal Finkelfcc51d42013-03-17 04:43:44 +0000629 SmallVectorImpl<MachineInstr*> &NewMIs,
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000630 bool &NonRI, bool &SpillsVRS) const{
Hal Finkel37714b82013-03-27 21:21:15 +0000631 // Note: If additional store instructions are added here,
632 // update isStoreToStackSlot.
633
Chris Lattner6f306d72010-04-02 20:16:16 +0000634 DebugLoc DL;
Craig Topperabadc662012-04-20 06:31:50 +0000635 if (PPC::GPRCRegClass.hasSubClassEq(RC)) {
Hal Finkel794e05b2013-03-23 17:14:27 +0000636 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
637 .addReg(SrcReg,
638 getKillRegState(isKill)),
639 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000640 } else if (PPC::G8RCRegClass.hasSubClassEq(RC)) {
Hal Finkel794e05b2013-03-23 17:14:27 +0000641 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STD))
642 .addReg(SrcReg,
643 getKillRegState(isKill)),
644 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000645 } else if (PPC::F8RCRegClass.hasSubClassEq(RC)) {
Dale Johannesen6b8c76a2009-02-12 23:08:38 +0000646 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFD))
Bill Wendlingf7b83c72009-05-13 21:33:08 +0000647 .addReg(SrcReg,
648 getKillRegState(isKill)),
649 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000650 } else if (PPC::F4RCRegClass.hasSubClassEq(RC)) {
Dale Johannesen6b8c76a2009-02-12 23:08:38 +0000651 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFS))
Bill Wendlingf7b83c72009-05-13 21:33:08 +0000652 .addReg(SrcReg,
653 getKillRegState(isKill)),
654 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000655 } else if (PPC::CRRCRegClass.hasSubClassEq(RC)) {
Hal Finkele154c8f2013-03-12 14:12:16 +0000656 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CR))
657 .addReg(SrcReg,
658 getKillRegState(isKill)),
659 FrameIdx));
660 return true;
Craig Topperabadc662012-04-20 06:31:50 +0000661 } else if (PPC::CRBITRCRegClass.hasSubClassEq(RC)) {
Nicolas Geoffrayb1de7a32008-03-10 14:12:10 +0000662 // FIXME: We use CRi here because there is no mtcrf on a bit. Since the
663 // backend currently only uses CR1EQ as an individual bit, this should
664 // not cause any bug. If we need other uses of CR bits, the following
665 // code may be invalid.
Nicolas Geoffray708784e2008-03-10 17:46:45 +0000666 unsigned Reg = 0;
Tilmann Scheller9db3e702009-07-03 06:47:55 +0000667 if (SrcReg == PPC::CR0LT || SrcReg == PPC::CR0GT ||
668 SrcReg == PPC::CR0EQ || SrcReg == PPC::CR0UN)
Nicolas Geoffray708784e2008-03-10 17:46:45 +0000669 Reg = PPC::CR0;
Tilmann Scheller9db3e702009-07-03 06:47:55 +0000670 else if (SrcReg == PPC::CR1LT || SrcReg == PPC::CR1GT ||
671 SrcReg == PPC::CR1EQ || SrcReg == PPC::CR1UN)
Nicolas Geoffray708784e2008-03-10 17:46:45 +0000672 Reg = PPC::CR1;
Tilmann Scheller9db3e702009-07-03 06:47:55 +0000673 else if (SrcReg == PPC::CR2LT || SrcReg == PPC::CR2GT ||
674 SrcReg == PPC::CR2EQ || SrcReg == PPC::CR2UN)
Nicolas Geoffray708784e2008-03-10 17:46:45 +0000675 Reg = PPC::CR2;
Tilmann Scheller9db3e702009-07-03 06:47:55 +0000676 else if (SrcReg == PPC::CR3LT || SrcReg == PPC::CR3GT ||
677 SrcReg == PPC::CR3EQ || SrcReg == PPC::CR3UN)
Nicolas Geoffray708784e2008-03-10 17:46:45 +0000678 Reg = PPC::CR3;
Tilmann Scheller9db3e702009-07-03 06:47:55 +0000679 else if (SrcReg == PPC::CR4LT || SrcReg == PPC::CR4GT ||
680 SrcReg == PPC::CR4EQ || SrcReg == PPC::CR4UN)
Nicolas Geoffray708784e2008-03-10 17:46:45 +0000681 Reg = PPC::CR4;
Tilmann Scheller9db3e702009-07-03 06:47:55 +0000682 else if (SrcReg == PPC::CR5LT || SrcReg == PPC::CR5GT ||
683 SrcReg == PPC::CR5EQ || SrcReg == PPC::CR5UN)
Nicolas Geoffray708784e2008-03-10 17:46:45 +0000684 Reg = PPC::CR5;
Tilmann Scheller9db3e702009-07-03 06:47:55 +0000685 else if (SrcReg == PPC::CR6LT || SrcReg == PPC::CR6GT ||
686 SrcReg == PPC::CR6EQ || SrcReg == PPC::CR6UN)
Nicolas Geoffray708784e2008-03-10 17:46:45 +0000687 Reg = PPC::CR6;
Tilmann Scheller9db3e702009-07-03 06:47:55 +0000688 else if (SrcReg == PPC::CR7LT || SrcReg == PPC::CR7GT ||
689 SrcReg == PPC::CR7EQ || SrcReg == PPC::CR7UN)
Nicolas Geoffray708784e2008-03-10 17:46:45 +0000690 Reg = PPC::CR7;
691
Andrew Trickc416ba62010-12-24 04:28:06 +0000692 return StoreRegToStackSlot(MF, Reg, isKill, FrameIdx,
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000693 &PPC::CRRCRegClass, NewMIs, NonRI, SpillsVRS);
Nicolas Geoffray708784e2008-03-10 17:46:45 +0000694
Craig Topperabadc662012-04-20 06:31:50 +0000695 } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) {
Hal Finkelfcc51d42013-03-17 04:43:44 +0000696 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STVX))
697 .addReg(SrcReg,
698 getKillRegState(isKill)),
699 FrameIdx));
700 NonRI = true;
Hal Finkela1431df2013-03-21 19:03:21 +0000701 } else if (PPC::VRSAVERCRegClass.hasSubClassEq(RC)) {
Hal Finkela7b06302013-03-27 00:02:20 +0000702 assert(TM.getSubtargetImpl()->isDarwin() &&
703 "VRSAVE only needs spill/restore on Darwin");
Hal Finkela1431df2013-03-21 19:03:21 +0000704 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_VRSAVE))
705 .addReg(SrcReg,
706 getKillRegState(isKill)),
707 FrameIdx));
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000708 SpillsVRS = true;
Owen Andersoneee14602008-01-01 21:11:32 +0000709 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000710 llvm_unreachable("Unknown regclass!");
Owen Andersoneee14602008-01-01 21:11:32 +0000711 }
Bill Wendling632ea652008-03-03 22:19:16 +0000712
713 return false;
Owen Andersoneee14602008-01-01 21:11:32 +0000714}
715
716void
717PPCInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
Bill Wendling632ea652008-03-03 22:19:16 +0000718 MachineBasicBlock::iterator MI,
719 unsigned SrcReg, bool isKill, int FrameIdx,
Evan Chengefb126a2010-05-06 19:06:44 +0000720 const TargetRegisterClass *RC,
721 const TargetRegisterInfo *TRI) const {
Dan Gohman3b460302008-07-07 23:14:23 +0000722 MachineFunction &MF = *MBB.getParent();
Owen Andersoneee14602008-01-01 21:11:32 +0000723 SmallVector<MachineInstr*, 4> NewMIs;
Bill Wendling632ea652008-03-03 22:19:16 +0000724
Hal Finkelbb420f12013-03-15 05:06:04 +0000725 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
726 FuncInfo->setHasSpills();
727
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000728 bool NonRI = false, SpillsVRS = false;
729 if (StoreRegToStackSlot(MF, SrcReg, isKill, FrameIdx, RC, NewMIs,
730 NonRI, SpillsVRS))
Bill Wendling632ea652008-03-03 22:19:16 +0000731 FuncInfo->setSpillsCR();
Bill Wendling632ea652008-03-03 22:19:16 +0000732
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000733 if (SpillsVRS)
734 FuncInfo->setSpillsVRSAVE();
735
Hal Finkelfcc51d42013-03-17 04:43:44 +0000736 if (NonRI)
737 FuncInfo->setHasNonRISpills();
738
Owen Andersoneee14602008-01-01 21:11:32 +0000739 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
740 MBB.insert(MI, NewMIs[i]);
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +0000741
742 const MachineFrameInfo &MFI = *MF.getFrameInfo();
743 MachineMemOperand *MMO =
Jay Foad465101b2011-11-15 07:34:52 +0000744 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx),
Chris Lattnere3d864b2010-09-21 04:39:43 +0000745 MachineMemOperand::MOStore,
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +0000746 MFI.getObjectSize(FrameIdx),
747 MFI.getObjectAlignment(FrameIdx));
748 NewMIs.back()->addMemOperand(MF, MMO);
Owen Andersoneee14602008-01-01 21:11:32 +0000749}
750
Hal Finkelbde7f8f2011-12-06 20:55:36 +0000751bool
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000752PPCInstrInfo::LoadRegFromStackSlot(MachineFunction &MF, DebugLoc DL,
Dan Gohman3b460302008-07-07 23:14:23 +0000753 unsigned DestReg, int FrameIdx,
Bill Wendlingc6c48fc2008-03-10 22:49:16 +0000754 const TargetRegisterClass *RC,
Hal Finkelfcc51d42013-03-17 04:43:44 +0000755 SmallVectorImpl<MachineInstr*> &NewMIs,
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000756 bool &NonRI, bool &SpillsVRS) const{
Hal Finkel37714b82013-03-27 21:21:15 +0000757 // Note: If additional load instructions are added here,
758 // update isLoadFromStackSlot.
759
Craig Topperabadc662012-04-20 06:31:50 +0000760 if (PPC::GPRCRegClass.hasSubClassEq(RC)) {
Hal Finkel5791f512013-03-27 19:10:40 +0000761 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ),
762 DestReg), FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000763 } else if (PPC::G8RCRegClass.hasSubClassEq(RC)) {
Hal Finkel5791f512013-03-27 19:10:40 +0000764 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LD), DestReg),
765 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000766 } else if (PPC::F8RCRegClass.hasSubClassEq(RC)) {
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000767 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFD), DestReg),
Owen Andersoneee14602008-01-01 21:11:32 +0000768 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000769 } else if (PPC::F4RCRegClass.hasSubClassEq(RC)) {
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000770 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFS), DestReg),
Owen Andersoneee14602008-01-01 21:11:32 +0000771 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000772 } else if (PPC::CRRCRegClass.hasSubClassEq(RC)) {
Hal Finkele154c8f2013-03-12 14:12:16 +0000773 NewMIs.push_back(addFrameReference(BuildMI(MF, DL,
774 get(PPC::RESTORE_CR), DestReg),
775 FrameIdx));
776 return true;
Craig Topperabadc662012-04-20 06:31:50 +0000777 } else if (PPC::CRBITRCRegClass.hasSubClassEq(RC)) {
Andrew Trickc416ba62010-12-24 04:28:06 +0000778
Nicolas Geoffray708784e2008-03-10 17:46:45 +0000779 unsigned Reg = 0;
Tilmann Scheller9db3e702009-07-03 06:47:55 +0000780 if (DestReg == PPC::CR0LT || DestReg == PPC::CR0GT ||
781 DestReg == PPC::CR0EQ || DestReg == PPC::CR0UN)
Nicolas Geoffray708784e2008-03-10 17:46:45 +0000782 Reg = PPC::CR0;
Tilmann Scheller9db3e702009-07-03 06:47:55 +0000783 else if (DestReg == PPC::CR1LT || DestReg == PPC::CR1GT ||
784 DestReg == PPC::CR1EQ || DestReg == PPC::CR1UN)
Nicolas Geoffray708784e2008-03-10 17:46:45 +0000785 Reg = PPC::CR1;
Tilmann Scheller9db3e702009-07-03 06:47:55 +0000786 else if (DestReg == PPC::CR2LT || DestReg == PPC::CR2GT ||
787 DestReg == PPC::CR2EQ || DestReg == PPC::CR2UN)
Nicolas Geoffray708784e2008-03-10 17:46:45 +0000788 Reg = PPC::CR2;
Tilmann Scheller9db3e702009-07-03 06:47:55 +0000789 else if (DestReg == PPC::CR3LT || DestReg == PPC::CR3GT ||
790 DestReg == PPC::CR3EQ || DestReg == PPC::CR3UN)
Nicolas Geoffray708784e2008-03-10 17:46:45 +0000791 Reg = PPC::CR3;
Tilmann Scheller9db3e702009-07-03 06:47:55 +0000792 else if (DestReg == PPC::CR4LT || DestReg == PPC::CR4GT ||
793 DestReg == PPC::CR4EQ || DestReg == PPC::CR4UN)
Nicolas Geoffray708784e2008-03-10 17:46:45 +0000794 Reg = PPC::CR4;
Tilmann Scheller9db3e702009-07-03 06:47:55 +0000795 else if (DestReg == PPC::CR5LT || DestReg == PPC::CR5GT ||
796 DestReg == PPC::CR5EQ || DestReg == PPC::CR5UN)
Nicolas Geoffray708784e2008-03-10 17:46:45 +0000797 Reg = PPC::CR5;
Tilmann Scheller9db3e702009-07-03 06:47:55 +0000798 else if (DestReg == PPC::CR6LT || DestReg == PPC::CR6GT ||
799 DestReg == PPC::CR6EQ || DestReg == PPC::CR6UN)
Nicolas Geoffray708784e2008-03-10 17:46:45 +0000800 Reg = PPC::CR6;
Tilmann Scheller9db3e702009-07-03 06:47:55 +0000801 else if (DestReg == PPC::CR7LT || DestReg == PPC::CR7GT ||
802 DestReg == PPC::CR7EQ || DestReg == PPC::CR7UN)
Nicolas Geoffray708784e2008-03-10 17:46:45 +0000803 Reg = PPC::CR7;
804
Andrew Trickc416ba62010-12-24 04:28:06 +0000805 return LoadRegFromStackSlot(MF, DL, Reg, FrameIdx,
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000806 &PPC::CRRCRegClass, NewMIs, NonRI, SpillsVRS);
Nicolas Geoffray708784e2008-03-10 17:46:45 +0000807
Craig Topperabadc662012-04-20 06:31:50 +0000808 } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) {
Hal Finkelfcc51d42013-03-17 04:43:44 +0000809 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LVX), DestReg),
810 FrameIdx));
811 NonRI = true;
Hal Finkela1431df2013-03-21 19:03:21 +0000812 } else if (PPC::VRSAVERCRegClass.hasSubClassEq(RC)) {
Hal Finkela7b06302013-03-27 00:02:20 +0000813 assert(TM.getSubtargetImpl()->isDarwin() &&
814 "VRSAVE only needs spill/restore on Darwin");
Hal Finkela1431df2013-03-21 19:03:21 +0000815 NewMIs.push_back(addFrameReference(BuildMI(MF, DL,
816 get(PPC::RESTORE_VRSAVE),
817 DestReg),
818 FrameIdx));
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000819 SpillsVRS = true;
Owen Andersoneee14602008-01-01 21:11:32 +0000820 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000821 llvm_unreachable("Unknown regclass!");
Owen Andersoneee14602008-01-01 21:11:32 +0000822 }
Hal Finkelbde7f8f2011-12-06 20:55:36 +0000823
824 return false;
Owen Andersoneee14602008-01-01 21:11:32 +0000825}
826
827void
828PPCInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
Bill Wendling632ea652008-03-03 22:19:16 +0000829 MachineBasicBlock::iterator MI,
830 unsigned DestReg, int FrameIdx,
Evan Chengefb126a2010-05-06 19:06:44 +0000831 const TargetRegisterClass *RC,
832 const TargetRegisterInfo *TRI) const {
Dan Gohman3b460302008-07-07 23:14:23 +0000833 MachineFunction &MF = *MBB.getParent();
Owen Andersoneee14602008-01-01 21:11:32 +0000834 SmallVector<MachineInstr*, 4> NewMIs;
Chris Lattner6f306d72010-04-02 20:16:16 +0000835 DebugLoc DL;
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000836 if (MI != MBB.end()) DL = MI->getDebugLoc();
Hal Finkelfcc51d42013-03-17 04:43:44 +0000837
838 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
839 FuncInfo->setHasSpills();
840
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000841 bool NonRI = false, SpillsVRS = false;
842 if (LoadRegFromStackSlot(MF, DL, DestReg, FrameIdx, RC, NewMIs,
843 NonRI, SpillsVRS))
Hal Finkelbde7f8f2011-12-06 20:55:36 +0000844 FuncInfo->setSpillsCR();
Hal Finkelfcc51d42013-03-17 04:43:44 +0000845
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000846 if (SpillsVRS)
847 FuncInfo->setSpillsVRSAVE();
848
Hal Finkelfcc51d42013-03-17 04:43:44 +0000849 if (NonRI)
850 FuncInfo->setHasNonRISpills();
851
Owen Andersoneee14602008-01-01 21:11:32 +0000852 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
853 MBB.insert(MI, NewMIs[i]);
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +0000854
855 const MachineFrameInfo &MFI = *MF.getFrameInfo();
856 MachineMemOperand *MMO =
Jay Foad465101b2011-11-15 07:34:52 +0000857 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx),
Chris Lattnere3d864b2010-09-21 04:39:43 +0000858 MachineMemOperand::MOLoad,
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +0000859 MFI.getObjectSize(FrameIdx),
860 MFI.getObjectAlignment(FrameIdx));
861 NewMIs.back()->addMemOperand(MF, MMO);
Owen Andersoneee14602008-01-01 21:11:32 +0000862}
863
Chris Lattnera47294ed2006-10-13 21:21:17 +0000864bool PPCInstrInfo::
Owen Anderson4f6bf042008-08-14 22:49:33 +0000865ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
Chris Lattner23f22de2006-10-21 06:03:11 +0000866 assert(Cond.size() == 2 && "Invalid PPC branch opcode!");
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000867 if (Cond[1].getReg() == PPC::CTR8 || Cond[1].getReg() == PPC::CTR)
868 Cond[0].setImm(Cond[0].getImm() == 0 ? 1 : 0);
869 else
870 // Leave the CR# the same, but invert the condition.
871 Cond[0].setImm(PPC::InvertPredicate((PPC::Predicate)Cond[0].getImm()));
Chris Lattner23f22de2006-10-21 06:03:11 +0000872 return false;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000873}
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +0000874
Hal Finkeld61d4f82013-04-06 19:30:30 +0000875bool PPCInstrInfo::FoldImmediate(MachineInstr *UseMI, MachineInstr *DefMI,
876 unsigned Reg, MachineRegisterInfo *MRI) const {
877 // For some instructions, it is legal to fold ZERO into the RA register field.
878 // A zero immediate should always be loaded with a single li.
879 unsigned DefOpc = DefMI->getOpcode();
880 if (DefOpc != PPC::LI && DefOpc != PPC::LI8)
881 return false;
882 if (!DefMI->getOperand(1).isImm())
883 return false;
884 if (DefMI->getOperand(1).getImm() != 0)
885 return false;
886
887 // Note that we cannot here invert the arguments of an isel in order to fold
888 // a ZERO into what is presented as the second argument. All we have here
889 // is the condition bit, and that might come from a CR-logical bit operation.
890
891 const MCInstrDesc &UseMCID = UseMI->getDesc();
892
893 // Only fold into real machine instructions.
894 if (UseMCID.isPseudo())
895 return false;
896
897 unsigned UseIdx;
898 for (UseIdx = 0; UseIdx < UseMI->getNumOperands(); ++UseIdx)
899 if (UseMI->getOperand(UseIdx).isReg() &&
900 UseMI->getOperand(UseIdx).getReg() == Reg)
901 break;
902
903 assert(UseIdx < UseMI->getNumOperands() && "Cannot find Reg in UseMI");
904 assert(UseIdx < UseMCID.getNumOperands() && "No operand description for Reg");
905
906 const MCOperandInfo *UseInfo = &UseMCID.OpInfo[UseIdx];
907
908 // We can fold the zero if this register requires a GPRC_NOR0/G8RC_NOX0
909 // register (which might also be specified as a pointer class kind).
910 if (UseInfo->isLookupPtrRegClass()) {
911 if (UseInfo->RegClass /* Kind */ != 1)
912 return false;
913 } else {
914 if (UseInfo->RegClass != PPC::GPRC_NOR0RegClassID &&
915 UseInfo->RegClass != PPC::G8RC_NOX0RegClassID)
916 return false;
917 }
918
919 // Make sure this is not tied to an output register (or otherwise
920 // constrained). This is true for ST?UX registers, for example, which
921 // are tied to their output registers.
922 if (UseInfo->Constraints != 0)
923 return false;
924
925 unsigned ZeroReg;
926 if (UseInfo->isLookupPtrRegClass()) {
927 bool isPPC64 = TM.getSubtargetImpl()->isPPC64();
928 ZeroReg = isPPC64 ? PPC::ZERO8 : PPC::ZERO;
929 } else {
930 ZeroReg = UseInfo->RegClass == PPC::G8RC_NOX0RegClassID ?
931 PPC::ZERO8 : PPC::ZERO;
932 }
933
934 bool DeleteDef = MRI->hasOneNonDBGUse(Reg);
935 UseMI->getOperand(UseIdx).setReg(ZeroReg);
936
937 if (DeleteDef)
938 DefMI->eraseFromParent();
939
940 return true;
941}
942
Hal Finkel30ae2292013-04-10 18:30:16 +0000943static bool MBBDefinesCTR(MachineBasicBlock &MBB) {
944 for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end();
945 I != IE; ++I)
946 if (I->definesRegister(PPC::CTR) || I->definesRegister(PPC::CTR8))
947 return true;
948 return false;
949}
950
951// We should make sure that, if we're going to predicate both sides of a
952// condition (a diamond), that both sides don't define the counter register. We
953// can predicate counter-decrement-based branches, but while that predicates
954// the branching, it does not predicate the counter decrement. If we tried to
955// merge the triangle into one predicated block, we'd decrement the counter
956// twice.
957bool PPCInstrInfo::isProfitableToIfCvt(MachineBasicBlock &TMBB,
958 unsigned NumT, unsigned ExtraT,
959 MachineBasicBlock &FMBB,
960 unsigned NumF, unsigned ExtraF,
961 const BranchProbability &Probability) const {
962 return !(MBBDefinesCTR(TMBB) && MBBDefinesCTR(FMBB));
963}
964
965
Hal Finkel5711eca2013-04-09 22:58:37 +0000966bool PPCInstrInfo::isPredicated(const MachineInstr *MI) const {
Hal Finkelf29285a2013-04-11 01:23:34 +0000967 // The predicated branches are identified by their type, not really by the
968 // explicit presence of a predicate. Furthermore, some of them can be
969 // predicated more than once. Because if conversion won't try to predicate
970 // any instruction which already claims to be predicated (by returning true
971 // here), always return false. In doing so, we let isPredicable() be the
972 // final word on whether not the instruction can be (further) predicated.
973
974 return false;
Hal Finkel5711eca2013-04-09 22:58:37 +0000975}
976
977bool PPCInstrInfo::isUnpredicatedTerminator(const MachineInstr *MI) const {
978 if (!MI->isTerminator())
979 return false;
980
981 // Conditional branch is a special case.
982 if (MI->isBranch() && !MI->isBarrier())
983 return true;
984
985 return !isPredicated(MI);
986}
987
988bool PPCInstrInfo::PredicateInstruction(
989 MachineInstr *MI,
990 const SmallVectorImpl<MachineOperand> &Pred) const {
991 unsigned OpC = MI->getOpcode();
992 if (OpC == PPC::BLR) {
993 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) {
994 bool isPPC64 = TM.getSubtargetImpl()->isPPC64();
995 MI->setDesc(get(Pred[0].getImm() ?
996 (isPPC64 ? PPC::BDNZLR8 : PPC::BDNZLR) :
997 (isPPC64 ? PPC::BDZLR8 : PPC::BDZLR)));
998 } else {
999 MI->setDesc(get(PPC::BCLR));
1000 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1001 .addImm(Pred[0].getImm())
1002 .addReg(Pred[1].getReg());
1003 }
1004
1005 return true;
1006 } else if (OpC == PPC::B) {
1007 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) {
1008 bool isPPC64 = TM.getSubtargetImpl()->isPPC64();
1009 MI->setDesc(get(Pred[0].getImm() ?
1010 (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
1011 (isPPC64 ? PPC::BDZ8 : PPC::BDZ)));
1012 } else {
1013 MachineBasicBlock *MBB = MI->getOperand(0).getMBB();
1014 MI->RemoveOperand(0);
1015
1016 MI->setDesc(get(PPC::BCC));
1017 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1018 .addImm(Pred[0].getImm())
1019 .addReg(Pred[1].getReg())
1020 .addMBB(MBB);
1021 }
1022
1023 return true;
Hal Finkel500b0042013-04-10 06:42:34 +00001024 } else if (OpC == PPC::BCTR || OpC == PPC::BCTR8 ||
1025 OpC == PPC::BCTRL || OpC == PPC::BCTRL8) {
1026 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR)
1027 llvm_unreachable("Cannot predicate bctr[l] on the ctr register");
1028
1029 bool setLR = OpC == PPC::BCTRL || OpC == PPC::BCTRL8;
1030 bool isPPC64 = TM.getSubtargetImpl()->isPPC64();
1031 MI->setDesc(get(isPPC64 ? (setLR ? PPC::BCCTRL8 : PPC::BCCTR8) :
1032 (setLR ? PPC::BCCTRL : PPC::BCCTR)));
1033 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1034 .addImm(Pred[0].getImm())
1035 .addReg(Pred[1].getReg());
1036 return true;
Hal Finkel5711eca2013-04-09 22:58:37 +00001037 }
1038
1039 return false;
1040}
1041
1042bool PPCInstrInfo::SubsumesPredicate(
1043 const SmallVectorImpl<MachineOperand> &Pred1,
1044 const SmallVectorImpl<MachineOperand> &Pred2) const {
1045 assert(Pred1.size() == 2 && "Invalid PPC first predicate");
1046 assert(Pred2.size() == 2 && "Invalid PPC second predicate");
1047
1048 if (Pred1[1].getReg() == PPC::CTR8 || Pred1[1].getReg() == PPC::CTR)
1049 return false;
1050 if (Pred2[1].getReg() == PPC::CTR8 || Pred2[1].getReg() == PPC::CTR)
1051 return false;
1052
Hal Finkel94a6f382013-12-11 23:12:25 +00001053 // P1 can only subsume P2 if they test the same condition register.
1054 if (Pred1[1].getReg() != Pred2[1].getReg())
1055 return false;
1056
Hal Finkel5711eca2013-04-09 22:58:37 +00001057 PPC::Predicate P1 = (PPC::Predicate) Pred1[0].getImm();
1058 PPC::Predicate P2 = (PPC::Predicate) Pred2[0].getImm();
1059
1060 if (P1 == P2)
1061 return true;
1062
1063 // Does P1 subsume P2, e.g. GE subsumes GT.
1064 if (P1 == PPC::PRED_LE &&
1065 (P2 == PPC::PRED_LT || P2 == PPC::PRED_EQ))
1066 return true;
1067 if (P1 == PPC::PRED_GE &&
1068 (P2 == PPC::PRED_GT || P2 == PPC::PRED_EQ))
1069 return true;
1070
1071 return false;
1072}
1073
1074bool PPCInstrInfo::DefinesPredicate(MachineInstr *MI,
1075 std::vector<MachineOperand> &Pred) const {
1076 // Note: At the present time, the contents of Pred from this function is
1077 // unused by IfConversion. This implementation follows ARM by pushing the
1078 // CR-defining operand. Because the 'DZ' and 'DNZ' count as types of
1079 // predicate, instructions defining CTR or CTR8 are also included as
1080 // predicate-defining instructions.
1081
1082 const TargetRegisterClass *RCs[] =
1083 { &PPC::CRRCRegClass, &PPC::CRBITRCRegClass,
1084 &PPC::CTRRCRegClass, &PPC::CTRRC8RegClass };
1085
1086 bool Found = false;
1087 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1088 const MachineOperand &MO = MI->getOperand(i);
Hal Finkelaf822012013-04-10 07:17:47 +00001089 for (unsigned c = 0; c < array_lengthof(RCs) && !Found; ++c) {
Hal Finkel5711eca2013-04-09 22:58:37 +00001090 const TargetRegisterClass *RC = RCs[c];
Hal Finkelaf822012013-04-10 07:17:47 +00001091 if (MO.isReg()) {
1092 if (MO.isDef() && RC->contains(MO.getReg())) {
Hal Finkel5711eca2013-04-09 22:58:37 +00001093 Pred.push_back(MO);
1094 Found = true;
1095 }
Hal Finkelaf822012013-04-10 07:17:47 +00001096 } else if (MO.isRegMask()) {
1097 for (TargetRegisterClass::iterator I = RC->begin(),
1098 IE = RC->end(); I != IE; ++I)
1099 if (MO.clobbersPhysReg(*I)) {
1100 Pred.push_back(MO);
1101 Found = true;
1102 }
Hal Finkel5711eca2013-04-09 22:58:37 +00001103 }
1104 }
1105 }
1106
1107 return Found;
1108}
1109
1110bool PPCInstrInfo::isPredicable(MachineInstr *MI) const {
1111 unsigned OpC = MI->getOpcode();
1112 switch (OpC) {
1113 default:
1114 return false;
1115 case PPC::B:
1116 case PPC::BLR:
Hal Finkel500b0042013-04-10 06:42:34 +00001117 case PPC::BCTR:
1118 case PPC::BCTR8:
1119 case PPC::BCTRL:
1120 case PPC::BCTRL8:
Hal Finkel5711eca2013-04-09 22:58:37 +00001121 return true;
1122 }
1123}
1124
Hal Finkel82656cb2013-04-18 22:15:08 +00001125bool PPCInstrInfo::analyzeCompare(const MachineInstr *MI,
1126 unsigned &SrcReg, unsigned &SrcReg2,
1127 int &Mask, int &Value) const {
1128 unsigned Opc = MI->getOpcode();
1129
1130 switch (Opc) {
1131 default: return false;
1132 case PPC::CMPWI:
1133 case PPC::CMPLWI:
1134 case PPC::CMPDI:
1135 case PPC::CMPLDI:
1136 SrcReg = MI->getOperand(1).getReg();
1137 SrcReg2 = 0;
1138 Value = MI->getOperand(2).getImm();
1139 Mask = 0xFFFF;
1140 return true;
1141 case PPC::CMPW:
1142 case PPC::CMPLW:
1143 case PPC::CMPD:
1144 case PPC::CMPLD:
1145 case PPC::FCMPUS:
1146 case PPC::FCMPUD:
1147 SrcReg = MI->getOperand(1).getReg();
1148 SrcReg2 = MI->getOperand(2).getReg();
1149 return true;
1150 }
1151}
Hal Finkele6322392013-04-19 22:08:38 +00001152
Hal Finkel82656cb2013-04-18 22:15:08 +00001153bool PPCInstrInfo::optimizeCompareInstr(MachineInstr *CmpInstr,
1154 unsigned SrcReg, unsigned SrcReg2,
1155 int Mask, int Value,
1156 const MachineRegisterInfo *MRI) const {
Hal Finkelb12da6b2013-04-18 22:54:25 +00001157 if (DisableCmpOpt)
1158 return false;
1159
Hal Finkel82656cb2013-04-18 22:15:08 +00001160 int OpC = CmpInstr->getOpcode();
1161 unsigned CRReg = CmpInstr->getOperand(0).getReg();
Hal Finkel08e53ee2013-05-08 12:16:14 +00001162
1163 // FP record forms set CR1 based on the execption status bits, not a
1164 // comparison with zero.
1165 if (OpC == PPC::FCMPUS || OpC == PPC::FCMPUD)
1166 return false;
Hal Finkel82656cb2013-04-18 22:15:08 +00001167
1168 // The record forms set the condition register based on a signed comparison
1169 // with zero (so says the ISA manual). This is not as straightforward as it
1170 // seems, however, because this is always a 64-bit comparison on PPC64, even
1171 // for instructions that are 32-bit in nature (like slw for example).
1172 // So, on PPC32, for unsigned comparisons, we can use the record forms only
1173 // for equality checks (as those don't depend on the sign). On PPC64,
1174 // we are restricted to equality for unsigned 64-bit comparisons and for
1175 // signed 32-bit comparisons the applicability is more restricted.
1176 bool isPPC64 = TM.getSubtargetImpl()->isPPC64();
1177 bool is32BitSignedCompare = OpC == PPC::CMPWI || OpC == PPC::CMPW;
1178 bool is32BitUnsignedCompare = OpC == PPC::CMPLWI || OpC == PPC::CMPLW;
1179 bool is64BitUnsignedCompare = OpC == PPC::CMPLDI || OpC == PPC::CMPLD;
1180
1181 // Get the unique definition of SrcReg.
1182 MachineInstr *MI = MRI->getUniqueVRegDef(SrcReg);
1183 if (!MI) return false;
1184 int MIOpC = MI->getOpcode();
1185
1186 bool equalityOnly = false;
1187 bool noSub = false;
1188 if (isPPC64) {
1189 if (is32BitSignedCompare) {
1190 // We can perform this optimization only if MI is sign-extending.
1191 if (MIOpC == PPC::SRAW || MIOpC == PPC::SRAWo ||
1192 MIOpC == PPC::SRAWI || MIOpC == PPC::SRAWIo ||
1193 MIOpC == PPC::EXTSB || MIOpC == PPC::EXTSBo ||
1194 MIOpC == PPC::EXTSH || MIOpC == PPC::EXTSHo ||
1195 MIOpC == PPC::EXTSW || MIOpC == PPC::EXTSWo) {
1196 noSub = true;
1197 } else
1198 return false;
1199 } else if (is32BitUnsignedCompare) {
1200 // We can perform this optimization, equality only, if MI is
1201 // zero-extending.
1202 if (MIOpC == PPC::CNTLZW || MIOpC == PPC::CNTLZWo ||
1203 MIOpC == PPC::SLW || MIOpC == PPC::SLWo ||
1204 MIOpC == PPC::SRW || MIOpC == PPC::SRWo) {
1205 noSub = true;
1206 equalityOnly = true;
1207 } else
1208 return false;
Hal Finkel08e53ee2013-05-08 12:16:14 +00001209 } else
Hal Finkel82656cb2013-04-18 22:15:08 +00001210 equalityOnly = is64BitUnsignedCompare;
Hal Finkel08e53ee2013-05-08 12:16:14 +00001211 } else
Hal Finkel82656cb2013-04-18 22:15:08 +00001212 equalityOnly = is32BitUnsignedCompare;
1213
1214 if (equalityOnly) {
1215 // We need to check the uses of the condition register in order to reject
1216 // non-equality comparisons.
1217 for (MachineRegisterInfo::use_iterator I = MRI->use_begin(CRReg),
1218 IE = MRI->use_end(); I != IE; ++I) {
1219 MachineInstr *UseMI = &*I;
1220 if (UseMI->getOpcode() == PPC::BCC) {
1221 unsigned Pred = UseMI->getOperand(0).getImm();
Hal Finkelc3632452013-05-07 17:49:55 +00001222 if (Pred != PPC::PRED_EQ && Pred != PPC::PRED_NE)
1223 return false;
Hal Finkel82656cb2013-04-18 22:15:08 +00001224 } else if (UseMI->getOpcode() == PPC::ISEL ||
1225 UseMI->getOpcode() == PPC::ISEL8) {
1226 unsigned SubIdx = UseMI->getOperand(3).getSubReg();
Hal Finkelc3632452013-05-07 17:49:55 +00001227 if (SubIdx != PPC::sub_eq)
1228 return false;
Hal Finkel82656cb2013-04-18 22:15:08 +00001229 } else
1230 return false;
1231 }
1232 }
1233
Hal Finkelc3632452013-05-07 17:49:55 +00001234 MachineBasicBlock::iterator I = CmpInstr;
Hal Finkel82656cb2013-04-18 22:15:08 +00001235
1236 // Scan forward to find the first use of the compare.
1237 for (MachineBasicBlock::iterator EL = CmpInstr->getParent()->end();
1238 I != EL; ++I) {
1239 bool FoundUse = false;
1240 for (MachineRegisterInfo::use_iterator J = MRI->use_begin(CRReg),
1241 JE = MRI->use_end(); J != JE; ++J)
1242 if (&*J == &*I) {
1243 FoundUse = true;
1244 break;
1245 }
1246
1247 if (FoundUse)
1248 break;
1249 }
1250
Hal Finkel82656cb2013-04-18 22:15:08 +00001251 // There are two possible candidates which can be changed to set CR[01].
1252 // One is MI, the other is a SUB instruction.
1253 // For CMPrr(r1,r2), we are looking for SUB(r1,r2) or SUB(r2,r1).
1254 MachineInstr *Sub = NULL;
1255 if (SrcReg2 != 0)
1256 // MI is not a candidate for CMPrr.
1257 MI = NULL;
1258 // FIXME: Conservatively refuse to convert an instruction which isn't in the
1259 // same BB as the comparison. This is to allow the check below to avoid calls
1260 // (and other explicit clobbers); instead we should really check for these
1261 // more explicitly (in at least a few predecessors).
1262 else if (MI->getParent() != CmpInstr->getParent() || Value != 0) {
1263 // PPC does not have a record-form SUBri.
1264 return false;
1265 }
1266
1267 // Search for Sub.
1268 const TargetRegisterInfo *TRI = &getRegisterInfo();
1269 --I;
Hal Finkelc3632452013-05-07 17:49:55 +00001270
1271 // Get ready to iterate backward from CmpInstr.
1272 MachineBasicBlock::iterator E = MI,
1273 B = CmpInstr->getParent()->begin();
1274
Hal Finkel82656cb2013-04-18 22:15:08 +00001275 for (; I != E && !noSub; --I) {
1276 const MachineInstr &Instr = *I;
1277 unsigned IOpC = Instr.getOpcode();
1278
1279 if (&*I != CmpInstr && (
Hal Finkel08e53ee2013-05-08 12:16:14 +00001280 Instr.modifiesRegister(PPC::CR0, TRI) ||
1281 Instr.readsRegister(PPC::CR0, TRI)))
Hal Finkel82656cb2013-04-18 22:15:08 +00001282 // This instruction modifies or uses the record condition register after
1283 // the one we want to change. While we could do this transformation, it
1284 // would likely not be profitable. This transformation removes one
1285 // instruction, and so even forcing RA to generate one move probably
1286 // makes it unprofitable.
1287 return false;
1288
1289 // Check whether CmpInstr can be made redundant by the current instruction.
1290 if ((OpC == PPC::CMPW || OpC == PPC::CMPLW ||
1291 OpC == PPC::CMPD || OpC == PPC::CMPLD) &&
1292 (IOpC == PPC::SUBF || IOpC == PPC::SUBF8) &&
1293 ((Instr.getOperand(1).getReg() == SrcReg &&
1294 Instr.getOperand(2).getReg() == SrcReg2) ||
1295 (Instr.getOperand(1).getReg() == SrcReg2 &&
1296 Instr.getOperand(2).getReg() == SrcReg))) {
1297 Sub = &*I;
1298 break;
1299 }
1300
Hal Finkel82656cb2013-04-18 22:15:08 +00001301 if (I == B)
1302 // The 'and' is below the comparison instruction.
1303 return false;
1304 }
1305
1306 // Return false if no candidates exist.
1307 if (!MI && !Sub)
1308 return false;
1309
1310 // The single candidate is called MI.
1311 if (!MI) MI = Sub;
1312
1313 int NewOpC = -1;
1314 MIOpC = MI->getOpcode();
1315 if (MIOpC == PPC::ANDIo || MIOpC == PPC::ANDIo8)
1316 NewOpC = MIOpC;
1317 else {
1318 NewOpC = PPC::getRecordFormOpcode(MIOpC);
1319 if (NewOpC == -1 && PPC::getNonRecordFormOpcode(MIOpC) != -1)
1320 NewOpC = MIOpC;
1321 }
1322
1323 // FIXME: On the non-embedded POWER architectures, only some of the record
1324 // forms are fast, and we should use only the fast ones.
1325
1326 // The defining instruction has a record form (or is already a record
1327 // form). It is possible, however, that we'll need to reverse the condition
1328 // code of the users.
1329 if (NewOpC == -1)
1330 return false;
1331
Hal Finkele6322392013-04-19 22:08:38 +00001332 SmallVector<std::pair<MachineOperand*, PPC::Predicate>, 4> PredsToUpdate;
1333 SmallVector<std::pair<MachineOperand*, unsigned>, 4> SubRegsToUpdate;
Hal Finkel82656cb2013-04-18 22:15:08 +00001334
1335 // If we have SUB(r1, r2) and CMP(r2, r1), the condition code based on CMP
1336 // needs to be updated to be based on SUB. Push the condition code
1337 // operands to OperandsToUpdate. If it is safe to remove CmpInstr, the
1338 // condition code of these operands will be modified.
1339 bool ShouldSwap = false;
1340 if (Sub) {
1341 ShouldSwap = SrcReg2 != 0 && Sub->getOperand(1).getReg() == SrcReg2 &&
1342 Sub->getOperand(2).getReg() == SrcReg;
1343
1344 // The operands to subf are the opposite of sub, so only in the fixed-point
1345 // case, invert the order.
Hal Finkel08e53ee2013-05-08 12:16:14 +00001346 ShouldSwap = !ShouldSwap;
Hal Finkel82656cb2013-04-18 22:15:08 +00001347 }
1348
1349 if (ShouldSwap)
1350 for (MachineRegisterInfo::use_iterator I = MRI->use_begin(CRReg),
1351 IE = MRI->use_end(); I != IE; ++I) {
1352 MachineInstr *UseMI = &*I;
1353 if (UseMI->getOpcode() == PPC::BCC) {
1354 PPC::Predicate Pred = (PPC::Predicate) UseMI->getOperand(0).getImm();
Hal Finkele6322392013-04-19 22:08:38 +00001355 assert((!equalityOnly ||
1356 Pred == PPC::PRED_EQ || Pred == PPC::PRED_NE) &&
1357 "Invalid predicate for equality-only optimization");
1358 PredsToUpdate.push_back(std::make_pair(&((*I).getOperand(0)),
Hal Finkel0f64e212013-04-20 05:16:26 +00001359 PPC::getSwappedPredicate(Pred)));
Hal Finkel82656cb2013-04-18 22:15:08 +00001360 } else if (UseMI->getOpcode() == PPC::ISEL ||
1361 UseMI->getOpcode() == PPC::ISEL8) {
Hal Finkele6322392013-04-19 22:08:38 +00001362 unsigned NewSubReg = UseMI->getOperand(3).getSubReg();
1363 assert((!equalityOnly || NewSubReg == PPC::sub_eq) &&
1364 "Invalid CR bit for equality-only optimization");
1365
1366 if (NewSubReg == PPC::sub_lt)
1367 NewSubReg = PPC::sub_gt;
1368 else if (NewSubReg == PPC::sub_gt)
1369 NewSubReg = PPC::sub_lt;
1370
1371 SubRegsToUpdate.push_back(std::make_pair(&((*I).getOperand(3)),
1372 NewSubReg));
Hal Finkel82656cb2013-04-18 22:15:08 +00001373 } else // We need to abort on a user we don't understand.
1374 return false;
1375 }
1376
1377 // Create a new virtual register to hold the value of the CR set by the
1378 // record-form instruction. If the instruction was not previously in
1379 // record form, then set the kill flag on the CR.
1380 CmpInstr->eraseFromParent();
1381
1382 MachineBasicBlock::iterator MII = MI;
1383 BuildMI(*MI->getParent(), llvm::next(MII), MI->getDebugLoc(),
1384 get(TargetOpcode::COPY), CRReg)
Hal Finkel08e53ee2013-05-08 12:16:14 +00001385 .addReg(PPC::CR0, MIOpC != NewOpC ? RegState::Kill : 0);
Hal Finkel82656cb2013-04-18 22:15:08 +00001386
1387 if (MIOpC != NewOpC) {
1388 // We need to be careful here: we're replacing one instruction with
1389 // another, and we need to make sure that we get all of the right
1390 // implicit uses and defs. On the other hand, the caller may be holding
1391 // an iterator to this instruction, and so we can't delete it (this is
1392 // specifically the case if this is the instruction directly after the
1393 // compare).
1394
1395 const MCInstrDesc &NewDesc = get(NewOpC);
1396 MI->setDesc(NewDesc);
1397
1398 if (NewDesc.ImplicitDefs)
1399 for (const uint16_t *ImpDefs = NewDesc.getImplicitDefs();
1400 *ImpDefs; ++ImpDefs)
1401 if (!MI->definesRegister(*ImpDefs))
1402 MI->addOperand(*MI->getParent()->getParent(),
1403 MachineOperand::CreateReg(*ImpDefs, true, true));
1404 if (NewDesc.ImplicitUses)
1405 for (const uint16_t *ImpUses = NewDesc.getImplicitUses();
1406 *ImpUses; ++ImpUses)
1407 if (!MI->readsRegister(*ImpUses))
1408 MI->addOperand(*MI->getParent()->getParent(),
1409 MachineOperand::CreateReg(*ImpUses, false, true));
1410 }
1411
1412 // Modify the condition code of operands in OperandsToUpdate.
1413 // Since we have SUB(r1, r2) and CMP(r2, r1), the condition code needs to
1414 // be changed from r2 > r1 to r1 < r2, from r2 < r1 to r1 > r2, etc.
Hal Finkele6322392013-04-19 22:08:38 +00001415 for (unsigned i = 0, e = PredsToUpdate.size(); i < e; i++)
1416 PredsToUpdate[i].first->setImm(PredsToUpdate[i].second);
Hal Finkel82656cb2013-04-18 22:15:08 +00001417
Hal Finkele6322392013-04-19 22:08:38 +00001418 for (unsigned i = 0, e = SubRegsToUpdate.size(); i < e; i++)
1419 SubRegsToUpdate[i].first->setSubReg(SubRegsToUpdate[i].second);
Hal Finkel82656cb2013-04-18 22:15:08 +00001420
1421 return true;
1422}
1423
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +00001424/// GetInstSize - Return the number of bytes of code the specified
1425/// instruction may be. This returns the maximum number of bytes.
1426///
1427unsigned PPCInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
1428 switch (MI->getOpcode()) {
1429 case PPC::INLINEASM: { // Inline Asm: Variable size.
1430 const MachineFunction *MF = MI->getParent()->getParent();
1431 const char *AsmStr = MI->getOperand(0).getSymbolName();
Chris Lattner7b26fce2009-08-22 20:48:53 +00001432 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +00001433 }
Bill Wendling499f7972010-07-16 22:20:36 +00001434 case PPC::PROLOG_LABEL:
Dan Gohmanfb19f942008-07-01 00:05:16 +00001435 case PPC::EH_LABEL:
1436 case PPC::GC_LABEL:
Dale Johannesen60b28972010-04-07 19:51:44 +00001437 case PPC::DBG_VALUE:
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +00001438 return 0;
Ulrich Weigandf62e83f2013-03-22 15:24:13 +00001439 case PPC::BL8_NOP:
1440 case PPC::BLA8_NOP:
Hal Finkel51861b42012-03-31 14:45:15 +00001441 return 8;
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +00001442 default:
1443 return 4; // PowerPC instructions are all 4 bytes
1444 }
1445}
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001446
1447#undef DEBUG_TYPE
1448#define DEBUG_TYPE "ppc-early-ret"
1449STATISTIC(NumBCLR, "Number of early conditional returns");
1450STATISTIC(NumBLR, "Number of early returns");
1451
1452namespace llvm {
1453 void initializePPCEarlyReturnPass(PassRegistry&);
1454}
1455
1456namespace {
1457 // PPCEarlyReturn pass - For simple functions without epilogue code, move
1458 // returns up, and create conditional returns, to avoid unnecessary
1459 // branch-to-blr sequences.
1460 struct PPCEarlyReturn : public MachineFunctionPass {
1461 static char ID;
1462 PPCEarlyReturn() : MachineFunctionPass(ID) {
1463 initializePPCEarlyReturnPass(*PassRegistry::getPassRegistry());
1464 }
1465
1466 const PPCTargetMachine *TM;
1467 const PPCInstrInfo *TII;
1468
1469protected:
Hal Finkel21aad9a2013-04-09 18:25:18 +00001470 bool processBlock(MachineBasicBlock &ReturnMBB) {
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001471 bool Changed = false;
1472
Hal Finkel21aad9a2013-04-09 18:25:18 +00001473 MachineBasicBlock::iterator I = ReturnMBB.begin();
1474 I = ReturnMBB.SkipPHIsAndLabels(I);
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001475
1476 // The block must be essentially empty except for the blr.
Hal Finkel21aad9a2013-04-09 18:25:18 +00001477 if (I == ReturnMBB.end() || I->getOpcode() != PPC::BLR ||
1478 I != ReturnMBB.getLastNonDebugInstr())
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001479 return Changed;
1480
1481 SmallVector<MachineBasicBlock*, 8> PredToRemove;
Hal Finkel21aad9a2013-04-09 18:25:18 +00001482 for (MachineBasicBlock::pred_iterator PI = ReturnMBB.pred_begin(),
1483 PIE = ReturnMBB.pred_end(); PI != PIE; ++PI) {
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001484 bool OtherReference = false, BlockChanged = false;
Hal Finkel21aad9a2013-04-09 18:25:18 +00001485 for (MachineBasicBlock::iterator J = (*PI)->getLastNonDebugInstr();;) {
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001486 if (J->getOpcode() == PPC::B) {
Hal Finkel21aad9a2013-04-09 18:25:18 +00001487 if (J->getOperand(0).getMBB() == &ReturnMBB) {
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001488 // This is an unconditional branch to the return. Replace the
Andrew Trick9defbd82013-12-17 04:50:40 +00001489 // branch with a blr.
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001490 BuildMI(**PI, J, J->getDebugLoc(), TII->get(PPC::BLR));
Hal Finkel21aad9a2013-04-09 18:25:18 +00001491 MachineBasicBlock::iterator K = J--;
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001492 K->eraseFromParent();
1493 BlockChanged = true;
1494 ++NumBLR;
1495 continue;
1496 }
1497 } else if (J->getOpcode() == PPC::BCC) {
Hal Finkel21aad9a2013-04-09 18:25:18 +00001498 if (J->getOperand(2).getMBB() == &ReturnMBB) {
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001499 // This is a conditional branch to the return. Replace the branch
1500 // with a bclr.
1501 BuildMI(**PI, J, J->getDebugLoc(), TII->get(PPC::BCLR))
1502 .addImm(J->getOperand(0).getImm())
1503 .addReg(J->getOperand(1).getReg());
Hal Finkel21aad9a2013-04-09 18:25:18 +00001504 MachineBasicBlock::iterator K = J--;
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001505 K->eraseFromParent();
1506 BlockChanged = true;
1507 ++NumBCLR;
1508 continue;
1509 }
1510 } else if (J->isBranch()) {
1511 if (J->isIndirectBranch()) {
Hal Finkel21aad9a2013-04-09 18:25:18 +00001512 if (ReturnMBB.hasAddressTaken())
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001513 OtherReference = true;
1514 } else
1515 for (unsigned i = 0; i < J->getNumOperands(); ++i)
1516 if (J->getOperand(i).isMBB() &&
Hal Finkel21aad9a2013-04-09 18:25:18 +00001517 J->getOperand(i).getMBB() == &ReturnMBB)
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001518 OtherReference = true;
Hal Finkel21aad9a2013-04-09 18:25:18 +00001519 } else if (!J->isTerminator() && !J->isDebugValue())
1520 break;
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001521
Hal Finkel21aad9a2013-04-09 18:25:18 +00001522 if (J == (*PI)->begin())
1523 break;
1524
1525 --J;
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001526 }
1527
Hal Finkel21aad9a2013-04-09 18:25:18 +00001528 if ((*PI)->canFallThrough() && (*PI)->isLayoutSuccessor(&ReturnMBB))
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001529 OtherReference = true;
1530
Andrew Trick9defbd82013-12-17 04:50:40 +00001531 // Predecessors are stored in a vector and can't be removed here.
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001532 if (!OtherReference && BlockChanged) {
1533 PredToRemove.push_back(*PI);
1534 }
1535
1536 if (BlockChanged)
1537 Changed = true;
1538 }
1539
1540 for (unsigned i = 0, ie = PredToRemove.size(); i != ie; ++i)
Hal Finkel21aad9a2013-04-09 18:25:18 +00001541 PredToRemove[i]->removeSuccessor(&ReturnMBB);
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001542
Hal Finkel21aad9a2013-04-09 18:25:18 +00001543 if (Changed && !ReturnMBB.hasAddressTaken()) {
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001544 // We now might be able to merge this blr-only block into its
1545 // by-layout predecessor.
Hal Finkel21aad9a2013-04-09 18:25:18 +00001546 if (ReturnMBB.pred_size() == 1 &&
1547 (*ReturnMBB.pred_begin())->isLayoutSuccessor(&ReturnMBB)) {
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001548 // Move the blr into the preceding block.
Hal Finkel21aad9a2013-04-09 18:25:18 +00001549 MachineBasicBlock &PrevMBB = **ReturnMBB.pred_begin();
1550 PrevMBB.splice(PrevMBB.end(), &ReturnMBB, I);
1551 PrevMBB.removeSuccessor(&ReturnMBB);
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001552 }
1553
Hal Finkel21aad9a2013-04-09 18:25:18 +00001554 if (ReturnMBB.pred_empty())
1555 ReturnMBB.eraseFromParent();
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001556 }
1557
1558 return Changed;
1559 }
1560
1561public:
1562 virtual bool runOnMachineFunction(MachineFunction &MF) {
1563 TM = static_cast<const PPCTargetMachine *>(&MF.getTarget());
1564 TII = TM->getInstrInfo();
1565
1566 bool Changed = false;
1567
Hal Finkel21aad9a2013-04-09 18:25:18 +00001568 // If the function does not have at least two blocks, then there is
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001569 // nothing to do.
1570 if (MF.size() < 2)
1571 return Changed;
1572
1573 for (MachineFunction::iterator I = MF.begin(); I != MF.end();) {
Andrew Trick9defbd82013-12-17 04:50:40 +00001574 MachineBasicBlock &B = *I++;
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001575 if (processBlock(B))
1576 Changed = true;
1577 }
1578
1579 return Changed;
1580 }
1581
1582 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
1583 MachineFunctionPass::getAnalysisUsage(AU);
1584 }
1585 };
1586}
1587
1588INITIALIZE_PASS(PPCEarlyReturn, DEBUG_TYPE,
1589 "PowerPC Early-Return Creation", false, false)
1590
1591char PPCEarlyReturn::ID = 0;
1592FunctionPass*
1593llvm::createPPCEarlyReturnPass() { return new PPCEarlyReturn(); }
1594