blob: d9907a0baf3bdd94f5b5919020a2f52cf885b873 [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"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "llvm/ADT/STLExtras.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000022#include "llvm/ADT/Statistic.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:
Hal Finkel940ab932014-02-28 00:27:01 +0000166 case PPC::RESTORE_CRBIT:
Hal Finkel37714b82013-03-27 21:21:15 +0000167 case PPC::LVX:
168 case PPC::RESTORE_VRSAVE:
169 // Check for the operands added by addFrameReference (the immediate is the
170 // offset which defaults to 0).
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000171 if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
172 MI->getOperand(2).isFI()) {
Chris Lattnera5bb3702007-12-30 23:10:15 +0000173 FrameIndex = MI->getOperand(2).getIndex();
Chris Lattnerbb53acd2006-02-02 20:12:32 +0000174 return MI->getOperand(0).getReg();
175 }
176 break;
177 }
178 return 0;
Chris Lattnerc327d712006-02-02 20:16:12 +0000179}
Chris Lattnerbb53acd2006-02-02 20:12:32 +0000180
Andrew Trickc416ba62010-12-24 04:28:06 +0000181unsigned PPCInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
Chris Lattnerc327d712006-02-02 20:16:12 +0000182 int &FrameIndex) const {
Hal Finkel37714b82013-03-27 21:21:15 +0000183 // Note: This list must be kept consistent with StoreRegToStackSlot.
Chris Lattnerc327d712006-02-02 20:16:12 +0000184 switch (MI->getOpcode()) {
185 default: break;
Nate Begeman4efb3282006-02-02 21:07:50 +0000186 case PPC::STD:
Chris Lattnerc327d712006-02-02 20:16:12 +0000187 case PPC::STW:
188 case PPC::STFS:
189 case PPC::STFD:
Hal Finkel37714b82013-03-27 21:21:15 +0000190 case PPC::SPILL_CR:
Hal Finkel940ab932014-02-28 00:27:01 +0000191 case PPC::SPILL_CRBIT:
Hal Finkel37714b82013-03-27 21:21:15 +0000192 case PPC::STVX:
193 case PPC::SPILL_VRSAVE:
194 // Check for the operands added by addFrameReference (the immediate is the
195 // offset which defaults to 0).
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000196 if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
197 MI->getOperand(2).isFI()) {
Chris Lattnera5bb3702007-12-30 23:10:15 +0000198 FrameIndex = MI->getOperand(2).getIndex();
Chris Lattnerc327d712006-02-02 20:16:12 +0000199 return MI->getOperand(0).getReg();
200 }
201 break;
202 }
203 return 0;
204}
Chris Lattnerbb53acd2006-02-02 20:12:32 +0000205
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000206// commuteInstruction - We can commute rlwimi instructions, but only if the
207// rotate amt is zero. We also have to munge the immediates a bit.
Evan Cheng03553bb2008-06-16 07:33:11 +0000208MachineInstr *
209PPCInstrInfo::commuteInstruction(MachineInstr *MI, bool NewMI) const {
Dan Gohman3b460302008-07-07 23:14:23 +0000210 MachineFunction &MF = *MI->getParent()->getParent();
211
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000212 // Normal instructions can be commuted the obvious way.
Hal Finkel654d43b2013-04-12 02:18:09 +0000213 if (MI->getOpcode() != PPC::RLWIMI &&
Hal Finkel940ab932014-02-28 00:27:01 +0000214 MI->getOpcode() != PPC::RLWIMIo &&
215 MI->getOpcode() != PPC::RLWIMI8 &&
216 MI->getOpcode() != PPC::RLWIMI8o)
Jakob Stoklund Olesen9de596e2012-11-28 02:35:17 +0000217 return TargetInstrInfo::commuteInstruction(MI, NewMI);
Andrew Trickc416ba62010-12-24 04:28:06 +0000218
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000219 // Cannot commute if it has a non-zero rotate count.
Chris Lattner5c463782007-12-30 20:49:49 +0000220 if (MI->getOperand(3).getImm() != 0)
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000221 return 0;
Andrew Trickc416ba62010-12-24 04:28:06 +0000222
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000223 // If we have a zero rotate count, we have:
224 // M = mask(MB,ME)
225 // Op0 = (Op1 & ~M) | (Op2 & M)
226 // Change this to:
227 // M = mask((ME+1)&31, (MB-1)&31)
228 // Op0 = (Op2 & ~M) | (Op1 & M)
229
230 // Swap op1/op2
Evan Cheng244183e2008-02-13 02:46:49 +0000231 unsigned Reg0 = MI->getOperand(0).getReg();
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000232 unsigned Reg1 = MI->getOperand(1).getReg();
233 unsigned Reg2 = MI->getOperand(2).getReg();
Andrew Tricke3398282013-12-17 04:50:45 +0000234 unsigned SubReg1 = MI->getOperand(1).getSubReg();
235 unsigned SubReg2 = MI->getOperand(2).getSubReg();
Evan Chengdc2c8742006-11-15 20:58:11 +0000236 bool Reg1IsKill = MI->getOperand(1).isKill();
237 bool Reg2IsKill = MI->getOperand(2).isKill();
Evan Cheng03553bb2008-06-16 07:33:11 +0000238 bool ChangeReg0 = false;
Evan Cheng244183e2008-02-13 02:46:49 +0000239 // If machine instrs are no longer in two-address forms, update
240 // destination register as well.
241 if (Reg0 == Reg1) {
242 // Must be two address instruction!
Evan Cheng6cc775f2011-06-28 19:10:37 +0000243 assert(MI->getDesc().getOperandConstraint(0, MCOI::TIED_TO) &&
Evan Cheng244183e2008-02-13 02:46:49 +0000244 "Expecting a two-address instruction!");
Andrew Tricke3398282013-12-17 04:50:45 +0000245 assert(MI->getOperand(0).getSubReg() == SubReg1 && "Tied subreg mismatch");
Evan Cheng244183e2008-02-13 02:46:49 +0000246 Reg2IsKill = false;
Evan Cheng03553bb2008-06-16 07:33:11 +0000247 ChangeReg0 = true;
Evan Cheng244183e2008-02-13 02:46:49 +0000248 }
Evan Cheng03553bb2008-06-16 07:33:11 +0000249
250 // Masks.
251 unsigned MB = MI->getOperand(4).getImm();
252 unsigned ME = MI->getOperand(5).getImm();
253
254 if (NewMI) {
255 // Create a new instruction.
256 unsigned Reg0 = ChangeReg0 ? Reg2 : MI->getOperand(0).getReg();
257 bool Reg0IsDead = MI->getOperand(0).isDead();
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000258 return BuildMI(MF, MI->getDebugLoc(), MI->getDesc())
Bill Wendlingf7b83c72009-05-13 21:33:08 +0000259 .addReg(Reg0, RegState::Define | getDeadRegState(Reg0IsDead))
260 .addReg(Reg2, getKillRegState(Reg2IsKill))
261 .addReg(Reg1, getKillRegState(Reg1IsKill))
Evan Cheng03553bb2008-06-16 07:33:11 +0000262 .addImm((ME+1) & 31)
263 .addImm((MB-1) & 31);
264 }
265
Andrew Tricke3398282013-12-17 04:50:45 +0000266 if (ChangeReg0) {
Evan Cheng03553bb2008-06-16 07:33:11 +0000267 MI->getOperand(0).setReg(Reg2);
Andrew Tricke3398282013-12-17 04:50:45 +0000268 MI->getOperand(0).setSubReg(SubReg2);
269 }
Chris Lattner10d63412006-05-04 17:52:23 +0000270 MI->getOperand(2).setReg(Reg1);
271 MI->getOperand(1).setReg(Reg2);
Andrew Tricke3398282013-12-17 04:50:45 +0000272 MI->getOperand(2).setSubReg(SubReg1);
273 MI->getOperand(1).setSubReg(SubReg2);
Chris Lattner60055892007-12-30 21:56:09 +0000274 MI->getOperand(2).setIsKill(Reg1IsKill);
275 MI->getOperand(1).setIsKill(Reg2IsKill);
Andrew Trickc416ba62010-12-24 04:28:06 +0000276
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000277 // Swap the mask around.
Chris Lattner5c463782007-12-30 20:49:49 +0000278 MI->getOperand(4).setImm((ME+1) & 31);
279 MI->getOperand(5).setImm((MB-1) & 31);
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000280 return MI;
281}
Chris Lattnerea79d9fd732006-03-05 23:49:55 +0000282
Andrew Trickc416ba62010-12-24 04:28:06 +0000283void PPCInstrInfo::insertNoop(MachineBasicBlock &MBB,
Chris Lattnerea79d9fd732006-03-05 23:49:55 +0000284 MachineBasicBlock::iterator MI) const {
Hal Finkelceb1f122013-12-12 00:19:11 +0000285 // This function is used for scheduling, and the nop wanted here is the type
286 // that terminates dispatch groups on the POWER cores.
287 unsigned Directive = TM.getSubtarget<PPCSubtarget>().getDarwinDirective();
288 unsigned Opcode;
289 switch (Directive) {
290 default: Opcode = PPC::NOP; break;
291 case PPC::DIR_PWR6: Opcode = PPC::NOP_GT_PWR6; break;
292 case PPC::DIR_PWR7: Opcode = PPC::NOP_GT_PWR7; break;
293 }
Chris Lattnera47294ed2006-10-13 21:21:17 +0000294
Hal Finkelceb1f122013-12-12 00:19:11 +0000295 DebugLoc DL;
296 BuildMI(MBB, MI, DL, get(Opcode));
297}
Chris Lattnera47294ed2006-10-13 21:21:17 +0000298
299// Branch analysis.
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000300// Note: If the condition register is set to CTR or CTR8 then this is a
301// BDNZ (imm == 1) or BDZ (imm == 0) branch.
Chris Lattnera47294ed2006-10-13 21:21:17 +0000302bool PPCInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
303 MachineBasicBlock *&FBB,
Evan Cheng64dfcac2009-02-09 07:14:22 +0000304 SmallVectorImpl<MachineOperand> &Cond,
305 bool AllowModify) const {
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000306 bool isPPC64 = TM.getSubtargetImpl()->isPPC64();
307
Chris Lattnera47294ed2006-10-13 21:21:17 +0000308 // If the block has no terminators, it just falls into the block after it.
309 MachineBasicBlock::iterator I = MBB.end();
Dale Johannesen4244d122010-04-02 01:38:09 +0000310 if (I == MBB.begin())
311 return false;
312 --I;
313 while (I->isDebugValue()) {
314 if (I == MBB.begin())
315 return false;
316 --I;
317 }
318 if (!isUnpredicatedTerminator(I))
Chris Lattnera47294ed2006-10-13 21:21:17 +0000319 return false;
320
321 // Get the last instruction in the block.
322 MachineInstr *LastInst = I;
Andrew Trickc416ba62010-12-24 04:28:06 +0000323
Chris Lattnera47294ed2006-10-13 21:21:17 +0000324 // If there is only one terminator instruction, process it.
Evan Cheng5514bbe2007-06-08 21:59:56 +0000325 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
Chris Lattnera47294ed2006-10-13 21:21:17 +0000326 if (LastInst->getOpcode() == PPC::B) {
Evan Cheng8f43afd2009-05-08 23:09:25 +0000327 if (!LastInst->getOperand(0).isMBB())
328 return true;
Chris Lattnera5bb3702007-12-30 23:10:15 +0000329 TBB = LastInst->getOperand(0).getMBB();
Chris Lattnera47294ed2006-10-13 21:21:17 +0000330 return false;
Chris Lattnere0263792006-11-17 22:14:47 +0000331 } else if (LastInst->getOpcode() == PPC::BCC) {
Evan Cheng8f43afd2009-05-08 23:09:25 +0000332 if (!LastInst->getOperand(2).isMBB())
333 return true;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000334 // Block ends with fall-through condbranch.
Chris Lattnera5bb3702007-12-30 23:10:15 +0000335 TBB = LastInst->getOperand(2).getMBB();
Chris Lattnera47294ed2006-10-13 21:21:17 +0000336 Cond.push_back(LastInst->getOperand(0));
337 Cond.push_back(LastInst->getOperand(1));
Chris Lattner23f22de2006-10-21 06:03:11 +0000338 return false;
Hal Finkel940ab932014-02-28 00:27:01 +0000339 } else if (LastInst->getOpcode() == PPC::BC) {
340 if (!LastInst->getOperand(1).isMBB())
341 return true;
342 // Block ends with fall-through condbranch.
343 TBB = LastInst->getOperand(1).getMBB();
344 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET));
345 Cond.push_back(LastInst->getOperand(0));
346 return false;
347 } else if (LastInst->getOpcode() == PPC::BCn) {
348 if (!LastInst->getOperand(1).isMBB())
349 return true;
350 // Block ends with fall-through condbranch.
351 TBB = LastInst->getOperand(1).getMBB();
352 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_UNSET));
353 Cond.push_back(LastInst->getOperand(0));
354 return false;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000355 } else if (LastInst->getOpcode() == PPC::BDNZ8 ||
356 LastInst->getOpcode() == PPC::BDNZ) {
357 if (!LastInst->getOperand(0).isMBB())
358 return true;
Hal Finkelc6b5deb2012-06-08 19:19:53 +0000359 if (DisableCTRLoopAnal)
Hal Finkel821e0012012-06-08 15:38:25 +0000360 return true;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000361 TBB = LastInst->getOperand(0).getMBB();
362 Cond.push_back(MachineOperand::CreateImm(1));
363 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
364 true));
365 return false;
366 } else if (LastInst->getOpcode() == PPC::BDZ8 ||
367 LastInst->getOpcode() == PPC::BDZ) {
368 if (!LastInst->getOperand(0).isMBB())
369 return true;
Hal Finkelc6b5deb2012-06-08 19:19:53 +0000370 if (DisableCTRLoopAnal)
Hal Finkel821e0012012-06-08 15:38:25 +0000371 return true;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000372 TBB = LastInst->getOperand(0).getMBB();
373 Cond.push_back(MachineOperand::CreateImm(0));
374 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
375 true));
376 return false;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000377 }
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000378
Chris Lattnera47294ed2006-10-13 21:21:17 +0000379 // Otherwise, don't know what this is.
380 return true;
381 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000382
Chris Lattnera47294ed2006-10-13 21:21:17 +0000383 // Get the instruction before it if it's a terminator.
384 MachineInstr *SecondLastInst = I;
385
386 // If there are three terminators, we don't know what sort of block this is.
387 if (SecondLastInst && I != MBB.begin() &&
Evan Cheng5514bbe2007-06-08 21:59:56 +0000388 isUnpredicatedTerminator(--I))
Chris Lattnera47294ed2006-10-13 21:21:17 +0000389 return true;
Andrew Trickc416ba62010-12-24 04:28:06 +0000390
Chris Lattnere0263792006-11-17 22:14:47 +0000391 // If the block ends with PPC::B and PPC:BCC, handle it.
Andrew Trickc416ba62010-12-24 04:28:06 +0000392 if (SecondLastInst->getOpcode() == PPC::BCC &&
Chris Lattnera47294ed2006-10-13 21:21:17 +0000393 LastInst->getOpcode() == PPC::B) {
Evan Cheng8f43afd2009-05-08 23:09:25 +0000394 if (!SecondLastInst->getOperand(2).isMBB() ||
395 !LastInst->getOperand(0).isMBB())
396 return true;
Chris Lattnera5bb3702007-12-30 23:10:15 +0000397 TBB = SecondLastInst->getOperand(2).getMBB();
Chris Lattnera47294ed2006-10-13 21:21:17 +0000398 Cond.push_back(SecondLastInst->getOperand(0));
399 Cond.push_back(SecondLastInst->getOperand(1));
Chris Lattnera5bb3702007-12-30 23:10:15 +0000400 FBB = LastInst->getOperand(0).getMBB();
Chris Lattnera47294ed2006-10-13 21:21:17 +0000401 return false;
Hal Finkel940ab932014-02-28 00:27:01 +0000402 } else if (SecondLastInst->getOpcode() == PPC::BC &&
403 LastInst->getOpcode() == PPC::B) {
404 if (!SecondLastInst->getOperand(1).isMBB() ||
405 !LastInst->getOperand(0).isMBB())
406 return true;
407 TBB = SecondLastInst->getOperand(1).getMBB();
408 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET));
409 Cond.push_back(SecondLastInst->getOperand(0));
410 FBB = LastInst->getOperand(0).getMBB();
411 return false;
412 } else if (SecondLastInst->getOpcode() == PPC::BCn &&
413 LastInst->getOpcode() == PPC::B) {
414 if (!SecondLastInst->getOperand(1).isMBB() ||
415 !LastInst->getOperand(0).isMBB())
416 return true;
417 TBB = SecondLastInst->getOperand(1).getMBB();
418 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_UNSET));
419 Cond.push_back(SecondLastInst->getOperand(0));
420 FBB = LastInst->getOperand(0).getMBB();
421 return false;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000422 } else if ((SecondLastInst->getOpcode() == PPC::BDNZ8 ||
423 SecondLastInst->getOpcode() == PPC::BDNZ) &&
424 LastInst->getOpcode() == PPC::B) {
425 if (!SecondLastInst->getOperand(0).isMBB() ||
426 !LastInst->getOperand(0).isMBB())
427 return true;
Hal Finkelc6b5deb2012-06-08 19:19:53 +0000428 if (DisableCTRLoopAnal)
Hal Finkel821e0012012-06-08 15:38:25 +0000429 return true;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000430 TBB = SecondLastInst->getOperand(0).getMBB();
431 Cond.push_back(MachineOperand::CreateImm(1));
432 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
433 true));
434 FBB = LastInst->getOperand(0).getMBB();
435 return false;
436 } else if ((SecondLastInst->getOpcode() == PPC::BDZ8 ||
437 SecondLastInst->getOpcode() == PPC::BDZ) &&
438 LastInst->getOpcode() == PPC::B) {
439 if (!SecondLastInst->getOperand(0).isMBB() ||
440 !LastInst->getOperand(0).isMBB())
441 return true;
Hal Finkelc6b5deb2012-06-08 19:19:53 +0000442 if (DisableCTRLoopAnal)
Hal Finkel821e0012012-06-08 15:38:25 +0000443 return true;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000444 TBB = SecondLastInst->getOperand(0).getMBB();
445 Cond.push_back(MachineOperand::CreateImm(0));
446 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
447 true));
448 FBB = LastInst->getOperand(0).getMBB();
449 return false;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000450 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000451
Dale Johannesenc6855462007-06-13 17:59:52 +0000452 // If the block ends with two PPC:Bs, handle it. The second one is not
453 // executed, so remove it.
Andrew Trickc416ba62010-12-24 04:28:06 +0000454 if (SecondLastInst->getOpcode() == PPC::B &&
Dale Johannesenc6855462007-06-13 17:59:52 +0000455 LastInst->getOpcode() == PPC::B) {
Evan Cheng8f43afd2009-05-08 23:09:25 +0000456 if (!SecondLastInst->getOperand(0).isMBB())
457 return true;
Chris Lattnera5bb3702007-12-30 23:10:15 +0000458 TBB = SecondLastInst->getOperand(0).getMBB();
Dale Johannesenc6855462007-06-13 17:59:52 +0000459 I = LastInst;
Evan Cheng64dfcac2009-02-09 07:14:22 +0000460 if (AllowModify)
461 I->eraseFromParent();
Dale Johannesenc6855462007-06-13 17:59:52 +0000462 return false;
463 }
464
Chris Lattnera47294ed2006-10-13 21:21:17 +0000465 // Otherwise, can't handle this.
466 return true;
467}
468
Evan Cheng99be49d2007-05-18 00:05:48 +0000469unsigned PPCInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
Chris Lattnera47294ed2006-10-13 21:21:17 +0000470 MachineBasicBlock::iterator I = MBB.end();
Evan Cheng99be49d2007-05-18 00:05:48 +0000471 if (I == MBB.begin()) return 0;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000472 --I;
Dale Johannesen4244d122010-04-02 01:38:09 +0000473 while (I->isDebugValue()) {
474 if (I == MBB.begin())
475 return 0;
476 --I;
477 }
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000478 if (I->getOpcode() != PPC::B && I->getOpcode() != PPC::BCC &&
Hal Finkel940ab932014-02-28 00:27:01 +0000479 I->getOpcode() != PPC::BC && I->getOpcode() != PPC::BCn &&
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000480 I->getOpcode() != PPC::BDNZ8 && I->getOpcode() != PPC::BDNZ &&
481 I->getOpcode() != PPC::BDZ8 && I->getOpcode() != PPC::BDZ)
Evan Cheng99be49d2007-05-18 00:05:48 +0000482 return 0;
Andrew Trickc416ba62010-12-24 04:28:06 +0000483
Chris Lattnera47294ed2006-10-13 21:21:17 +0000484 // Remove the branch.
485 I->eraseFromParent();
Andrew Trickc416ba62010-12-24 04:28:06 +0000486
Chris Lattnera47294ed2006-10-13 21:21:17 +0000487 I = MBB.end();
488
Evan Cheng99be49d2007-05-18 00:05:48 +0000489 if (I == MBB.begin()) return 1;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000490 --I;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000491 if (I->getOpcode() != PPC::BCC &&
Hal Finkel940ab932014-02-28 00:27:01 +0000492 I->getOpcode() != PPC::BC && I->getOpcode() != PPC::BCn &&
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000493 I->getOpcode() != PPC::BDNZ8 && I->getOpcode() != PPC::BDNZ &&
494 I->getOpcode() != PPC::BDZ8 && I->getOpcode() != PPC::BDZ)
Evan Cheng99be49d2007-05-18 00:05:48 +0000495 return 1;
Andrew Trickc416ba62010-12-24 04:28:06 +0000496
Chris Lattnera47294ed2006-10-13 21:21:17 +0000497 // Remove the branch.
498 I->eraseFromParent();
Evan Cheng99be49d2007-05-18 00:05:48 +0000499 return 2;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000500}
501
Evan Cheng99be49d2007-05-18 00:05:48 +0000502unsigned
503PPCInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
504 MachineBasicBlock *FBB,
Stuart Hastings0125b642010-06-17 22:43:56 +0000505 const SmallVectorImpl<MachineOperand> &Cond,
506 DebugLoc DL) const {
Chris Lattnera61f0102006-10-17 18:06:55 +0000507 // Shouldn't be a fall through.
508 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Andrew Trickc416ba62010-12-24 04:28:06 +0000509 assert((Cond.size() == 2 || Cond.size() == 0) &&
Chris Lattner94e04442006-10-21 05:36:13 +0000510 "PPC branch conditions have two components!");
Andrew Trickc416ba62010-12-24 04:28:06 +0000511
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000512 bool isPPC64 = TM.getSubtargetImpl()->isPPC64();
513
Chris Lattner94e04442006-10-21 05:36:13 +0000514 // One-way branch.
Chris Lattnera61f0102006-10-17 18:06:55 +0000515 if (FBB == 0) {
Chris Lattner94e04442006-10-21 05:36:13 +0000516 if (Cond.empty()) // Unconditional branch
Stuart Hastings0125b642010-06-17 22:43:56 +0000517 BuildMI(&MBB, DL, get(PPC::B)).addMBB(TBB);
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000518 else if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
519 BuildMI(&MBB, DL, get(Cond[0].getImm() ?
520 (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
521 (isPPC64 ? PPC::BDZ8 : PPC::BDZ))).addMBB(TBB);
Hal Finkel940ab932014-02-28 00:27:01 +0000522 else if (Cond[0].getImm() == PPC::PRED_BIT_SET)
523 BuildMI(&MBB, DL, get(PPC::BC)).addOperand(Cond[1]).addMBB(TBB);
524 else if (Cond[0].getImm() == PPC::PRED_BIT_UNSET)
525 BuildMI(&MBB, DL, get(PPC::BCn)).addOperand(Cond[1]).addMBB(TBB);
Chris Lattner94e04442006-10-21 05:36:13 +0000526 else // Conditional branch
Stuart Hastings0125b642010-06-17 22:43:56 +0000527 BuildMI(&MBB, DL, get(PPC::BCC))
Hal Finkel940ab932014-02-28 00:27:01 +0000528 .addImm(Cond[0].getImm()).addOperand(Cond[1]).addMBB(TBB);
Evan Cheng99be49d2007-05-18 00:05:48 +0000529 return 1;
Chris Lattnera61f0102006-10-17 18:06:55 +0000530 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000531
Chris Lattnerd8816602006-10-21 05:42:09 +0000532 // Two-way Conditional Branch.
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000533 if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
534 BuildMI(&MBB, DL, get(Cond[0].getImm() ?
535 (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
536 (isPPC64 ? PPC::BDZ8 : PPC::BDZ))).addMBB(TBB);
Hal Finkel940ab932014-02-28 00:27:01 +0000537 else if (Cond[0].getImm() == PPC::PRED_BIT_SET)
538 BuildMI(&MBB, DL, get(PPC::BC)).addOperand(Cond[1]).addMBB(TBB);
539 else if (Cond[0].getImm() == PPC::PRED_BIT_UNSET)
540 BuildMI(&MBB, DL, get(PPC::BCn)).addOperand(Cond[1]).addMBB(TBB);
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000541 else
542 BuildMI(&MBB, DL, get(PPC::BCC))
Hal Finkel940ab932014-02-28 00:27:01 +0000543 .addImm(Cond[0].getImm()).addOperand(Cond[1]).addMBB(TBB);
Stuart Hastings0125b642010-06-17 22:43:56 +0000544 BuildMI(&MBB, DL, get(PPC::B)).addMBB(FBB);
Evan Cheng99be49d2007-05-18 00:05:48 +0000545 return 2;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000546}
547
Hal Finkeled6a2852013-04-05 23:29:01 +0000548// Select analysis.
549bool PPCInstrInfo::canInsertSelect(const MachineBasicBlock &MBB,
550 const SmallVectorImpl<MachineOperand> &Cond,
551 unsigned TrueReg, unsigned FalseReg,
552 int &CondCycles, int &TrueCycles, int &FalseCycles) const {
553 if (!TM.getSubtargetImpl()->hasISEL())
554 return false;
555
556 if (Cond.size() != 2)
557 return false;
558
559 // If this is really a bdnz-like condition, then it cannot be turned into a
560 // select.
561 if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
562 return false;
563
564 // Check register classes.
565 const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
566 const TargetRegisterClass *RC =
567 RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg));
568 if (!RC)
569 return false;
570
571 // isel is for regular integer GPRs only.
572 if (!PPC::GPRCRegClass.hasSubClassEq(RC) &&
Hal Finkel8e8618a2013-07-15 20:22:58 +0000573 !PPC::GPRC_NOR0RegClass.hasSubClassEq(RC) &&
574 !PPC::G8RCRegClass.hasSubClassEq(RC) &&
575 !PPC::G8RC_NOX0RegClass.hasSubClassEq(RC))
Hal Finkeled6a2852013-04-05 23:29:01 +0000576 return false;
577
578 // FIXME: These numbers are for the A2, how well they work for other cores is
579 // an open question. On the A2, the isel instruction has a 2-cycle latency
580 // but single-cycle throughput. These numbers are used in combination with
581 // the MispredictPenalty setting from the active SchedMachineModel.
582 CondCycles = 1;
583 TrueCycles = 1;
584 FalseCycles = 1;
585
586 return true;
587}
588
589void PPCInstrInfo::insertSelect(MachineBasicBlock &MBB,
590 MachineBasicBlock::iterator MI, DebugLoc dl,
591 unsigned DestReg,
592 const SmallVectorImpl<MachineOperand> &Cond,
593 unsigned TrueReg, unsigned FalseReg) const {
594 assert(Cond.size() == 2 &&
595 "PPC branch conditions have two components!");
596
597 assert(TM.getSubtargetImpl()->hasISEL() &&
598 "Cannot insert select on target without ISEL support");
599
600 // Get the register classes.
601 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
602 const TargetRegisterClass *RC =
603 RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg));
604 assert(RC && "TrueReg and FalseReg must have overlapping register classes");
Hal Finkel8e8618a2013-07-15 20:22:58 +0000605
606 bool Is64Bit = PPC::G8RCRegClass.hasSubClassEq(RC) ||
607 PPC::G8RC_NOX0RegClass.hasSubClassEq(RC);
608 assert((Is64Bit ||
609 PPC::GPRCRegClass.hasSubClassEq(RC) ||
610 PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) &&
Hal Finkeled6a2852013-04-05 23:29:01 +0000611 "isel is for regular integer GPRs only");
612
Hal Finkel8e8618a2013-07-15 20:22:58 +0000613 unsigned OpCode = Is64Bit ? PPC::ISEL8 : PPC::ISEL;
Hal Finkeled6a2852013-04-05 23:29:01 +0000614 unsigned SelectPred = Cond[0].getImm();
615
616 unsigned SubIdx;
617 bool SwapOps;
618 switch (SelectPred) {
619 default: llvm_unreachable("invalid predicate for isel");
620 case PPC::PRED_EQ: SubIdx = PPC::sub_eq; SwapOps = false; break;
621 case PPC::PRED_NE: SubIdx = PPC::sub_eq; SwapOps = true; break;
622 case PPC::PRED_LT: SubIdx = PPC::sub_lt; SwapOps = false; break;
623 case PPC::PRED_GE: SubIdx = PPC::sub_lt; SwapOps = true; break;
624 case PPC::PRED_GT: SubIdx = PPC::sub_gt; SwapOps = false; break;
625 case PPC::PRED_LE: SubIdx = PPC::sub_gt; SwapOps = true; break;
626 case PPC::PRED_UN: SubIdx = PPC::sub_un; SwapOps = false; break;
627 case PPC::PRED_NU: SubIdx = PPC::sub_un; SwapOps = true; break;
Hal Finkel940ab932014-02-28 00:27:01 +0000628 case PPC::PRED_BIT_SET: SubIdx = 0; SwapOps = false; break;
629 case PPC::PRED_BIT_UNSET: SubIdx = 0; SwapOps = true; break;
Hal Finkeled6a2852013-04-05 23:29:01 +0000630 }
631
632 unsigned FirstReg = SwapOps ? FalseReg : TrueReg,
633 SecondReg = SwapOps ? TrueReg : FalseReg;
634
635 // The first input register of isel cannot be r0. If it is a member
636 // of a register class that can be r0, then copy it first (the
637 // register allocator should eliminate the copy).
638 if (MRI.getRegClass(FirstReg)->contains(PPC::R0) ||
639 MRI.getRegClass(FirstReg)->contains(PPC::X0)) {
640 const TargetRegisterClass *FirstRC =
641 MRI.getRegClass(FirstReg)->contains(PPC::X0) ?
642 &PPC::G8RC_NOX0RegClass : &PPC::GPRC_NOR0RegClass;
643 unsigned OldFirstReg = FirstReg;
644 FirstReg = MRI.createVirtualRegister(FirstRC);
645 BuildMI(MBB, MI, dl, get(TargetOpcode::COPY), FirstReg)
646 .addReg(OldFirstReg);
647 }
648
649 BuildMI(MBB, MI, dl, get(OpCode), DestReg)
650 .addReg(FirstReg).addReg(SecondReg)
651 .addReg(Cond[1].getReg(), 0, SubIdx);
652}
653
Jakob Stoklund Olesen0d611972010-07-11 07:31:00 +0000654void PPCInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
655 MachineBasicBlock::iterator I, DebugLoc DL,
656 unsigned DestReg, unsigned SrcReg,
657 bool KillSrc) const {
658 unsigned Opc;
659 if (PPC::GPRCRegClass.contains(DestReg, SrcReg))
660 Opc = PPC::OR;
661 else if (PPC::G8RCRegClass.contains(DestReg, SrcReg))
662 Opc = PPC::OR8;
663 else if (PPC::F4RCRegClass.contains(DestReg, SrcReg))
664 Opc = PPC::FMR;
665 else if (PPC::CRRCRegClass.contains(DestReg, SrcReg))
666 Opc = PPC::MCRF;
667 else if (PPC::VRRCRegClass.contains(DestReg, SrcReg))
668 Opc = PPC::VOR;
669 else if (PPC::CRBITRCRegClass.contains(DestReg, SrcReg))
670 Opc = PPC::CROR;
671 else
672 llvm_unreachable("Impossible reg-to-reg copy");
Owen Anderson7a73ae92007-12-31 06:32:00 +0000673
Evan Cheng6cc775f2011-06-28 19:10:37 +0000674 const MCInstrDesc &MCID = get(Opc);
675 if (MCID.getNumOperands() == 3)
676 BuildMI(MBB, I, DL, MCID, DestReg)
Jakob Stoklund Olesen0d611972010-07-11 07:31:00 +0000677 .addReg(SrcReg).addReg(SrcReg, getKillRegState(KillSrc));
678 else
Evan Cheng6cc775f2011-06-28 19:10:37 +0000679 BuildMI(MBB, I, DL, MCID, DestReg).addReg(SrcReg, getKillRegState(KillSrc));
Owen Anderson7a73ae92007-12-31 06:32:00 +0000680}
681
Hal Finkel8f6834d2011-12-05 17:55:17 +0000682// This function returns true if a CR spill is necessary and false otherwise.
Bill Wendlingc6c48fc2008-03-10 22:49:16 +0000683bool
Dan Gohman3b460302008-07-07 23:14:23 +0000684PPCInstrInfo::StoreRegToStackSlot(MachineFunction &MF,
685 unsigned SrcReg, bool isKill,
Bill Wendlingc6c48fc2008-03-10 22:49:16 +0000686 int FrameIdx,
687 const TargetRegisterClass *RC,
Hal Finkelfcc51d42013-03-17 04:43:44 +0000688 SmallVectorImpl<MachineInstr*> &NewMIs,
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000689 bool &NonRI, bool &SpillsVRS) const{
Hal Finkel37714b82013-03-27 21:21:15 +0000690 // Note: If additional store instructions are added here,
691 // update isStoreToStackSlot.
692
Chris Lattner6f306d72010-04-02 20:16:16 +0000693 DebugLoc DL;
Hal Finkel4e703bc2014-01-28 05:32:58 +0000694 if (PPC::GPRCRegClass.hasSubClassEq(RC) ||
695 PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) {
Hal Finkel794e05b2013-03-23 17:14:27 +0000696 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
697 .addReg(SrcReg,
698 getKillRegState(isKill)),
699 FrameIdx));
Hal Finkel4e703bc2014-01-28 05:32:58 +0000700 } else if (PPC::G8RCRegClass.hasSubClassEq(RC) ||
701 PPC::G8RC_NOX0RegClass.hasSubClassEq(RC)) {
Hal Finkel794e05b2013-03-23 17:14:27 +0000702 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STD))
703 .addReg(SrcReg,
704 getKillRegState(isKill)),
705 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000706 } else if (PPC::F8RCRegClass.hasSubClassEq(RC)) {
Dale Johannesen6b8c76a2009-02-12 23:08:38 +0000707 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFD))
Bill Wendlingf7b83c72009-05-13 21:33:08 +0000708 .addReg(SrcReg,
709 getKillRegState(isKill)),
710 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000711 } else if (PPC::F4RCRegClass.hasSubClassEq(RC)) {
Dale Johannesen6b8c76a2009-02-12 23:08:38 +0000712 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFS))
Bill Wendlingf7b83c72009-05-13 21:33:08 +0000713 .addReg(SrcReg,
714 getKillRegState(isKill)),
715 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000716 } else if (PPC::CRRCRegClass.hasSubClassEq(RC)) {
Hal Finkele154c8f2013-03-12 14:12:16 +0000717 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CR))
718 .addReg(SrcReg,
719 getKillRegState(isKill)),
720 FrameIdx));
721 return true;
Craig Topperabadc662012-04-20 06:31:50 +0000722 } else if (PPC::CRBITRCRegClass.hasSubClassEq(RC)) {
Hal Finkel940ab932014-02-28 00:27:01 +0000723 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CRBIT))
724 .addReg(SrcReg,
725 getKillRegState(isKill)),
726 FrameIdx));
727 return true;
Craig Topperabadc662012-04-20 06:31:50 +0000728 } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) {
Hal Finkelfcc51d42013-03-17 04:43:44 +0000729 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STVX))
730 .addReg(SrcReg,
731 getKillRegState(isKill)),
732 FrameIdx));
733 NonRI = true;
Hal Finkela1431df2013-03-21 19:03:21 +0000734 } else if (PPC::VRSAVERCRegClass.hasSubClassEq(RC)) {
Hal Finkela7b06302013-03-27 00:02:20 +0000735 assert(TM.getSubtargetImpl()->isDarwin() &&
736 "VRSAVE only needs spill/restore on Darwin");
Hal Finkela1431df2013-03-21 19:03:21 +0000737 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_VRSAVE))
738 .addReg(SrcReg,
739 getKillRegState(isKill)),
740 FrameIdx));
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000741 SpillsVRS = true;
Owen Andersoneee14602008-01-01 21:11:32 +0000742 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000743 llvm_unreachable("Unknown regclass!");
Owen Andersoneee14602008-01-01 21:11:32 +0000744 }
Bill Wendling632ea652008-03-03 22:19:16 +0000745
746 return false;
Owen Andersoneee14602008-01-01 21:11:32 +0000747}
748
749void
750PPCInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
Bill Wendling632ea652008-03-03 22:19:16 +0000751 MachineBasicBlock::iterator MI,
752 unsigned SrcReg, bool isKill, int FrameIdx,
Evan Chengefb126a2010-05-06 19:06:44 +0000753 const TargetRegisterClass *RC,
754 const TargetRegisterInfo *TRI) const {
Dan Gohman3b460302008-07-07 23:14:23 +0000755 MachineFunction &MF = *MBB.getParent();
Owen Andersoneee14602008-01-01 21:11:32 +0000756 SmallVector<MachineInstr*, 4> NewMIs;
Bill Wendling632ea652008-03-03 22:19:16 +0000757
Hal Finkelbb420f12013-03-15 05:06:04 +0000758 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
759 FuncInfo->setHasSpills();
760
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000761 bool NonRI = false, SpillsVRS = false;
762 if (StoreRegToStackSlot(MF, SrcReg, isKill, FrameIdx, RC, NewMIs,
763 NonRI, SpillsVRS))
Bill Wendling632ea652008-03-03 22:19:16 +0000764 FuncInfo->setSpillsCR();
Bill Wendling632ea652008-03-03 22:19:16 +0000765
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000766 if (SpillsVRS)
767 FuncInfo->setSpillsVRSAVE();
768
Hal Finkelfcc51d42013-03-17 04:43:44 +0000769 if (NonRI)
770 FuncInfo->setHasNonRISpills();
771
Owen Andersoneee14602008-01-01 21:11:32 +0000772 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
773 MBB.insert(MI, NewMIs[i]);
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +0000774
775 const MachineFrameInfo &MFI = *MF.getFrameInfo();
776 MachineMemOperand *MMO =
Jay Foad465101b2011-11-15 07:34:52 +0000777 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx),
Chris Lattnere3d864b2010-09-21 04:39:43 +0000778 MachineMemOperand::MOStore,
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +0000779 MFI.getObjectSize(FrameIdx),
780 MFI.getObjectAlignment(FrameIdx));
781 NewMIs.back()->addMemOperand(MF, MMO);
Owen Andersoneee14602008-01-01 21:11:32 +0000782}
783
Hal Finkelbde7f8f2011-12-06 20:55:36 +0000784bool
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000785PPCInstrInfo::LoadRegFromStackSlot(MachineFunction &MF, DebugLoc DL,
Dan Gohman3b460302008-07-07 23:14:23 +0000786 unsigned DestReg, int FrameIdx,
Bill Wendlingc6c48fc2008-03-10 22:49:16 +0000787 const TargetRegisterClass *RC,
Hal Finkelfcc51d42013-03-17 04:43:44 +0000788 SmallVectorImpl<MachineInstr*> &NewMIs,
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000789 bool &NonRI, bool &SpillsVRS) const{
Hal Finkel37714b82013-03-27 21:21:15 +0000790 // Note: If additional load instructions are added here,
791 // update isLoadFromStackSlot.
792
Hal Finkel4e703bc2014-01-28 05:32:58 +0000793 if (PPC::GPRCRegClass.hasSubClassEq(RC) ||
794 PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) {
Hal Finkel5791f512013-03-27 19:10:40 +0000795 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ),
796 DestReg), FrameIdx));
Hal Finkel4e703bc2014-01-28 05:32:58 +0000797 } else if (PPC::G8RCRegClass.hasSubClassEq(RC) ||
798 PPC::G8RC_NOX0RegClass.hasSubClassEq(RC)) {
Hal Finkel5791f512013-03-27 19:10:40 +0000799 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LD), DestReg),
800 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000801 } else if (PPC::F8RCRegClass.hasSubClassEq(RC)) {
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000802 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFD), DestReg),
Owen Andersoneee14602008-01-01 21:11:32 +0000803 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000804 } else if (PPC::F4RCRegClass.hasSubClassEq(RC)) {
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000805 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFS), DestReg),
Owen Andersoneee14602008-01-01 21:11:32 +0000806 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000807 } else if (PPC::CRRCRegClass.hasSubClassEq(RC)) {
Hal Finkele154c8f2013-03-12 14:12:16 +0000808 NewMIs.push_back(addFrameReference(BuildMI(MF, DL,
809 get(PPC::RESTORE_CR), DestReg),
810 FrameIdx));
811 return true;
Craig Topperabadc662012-04-20 06:31:50 +0000812 } else if (PPC::CRBITRCRegClass.hasSubClassEq(RC)) {
Hal Finkel940ab932014-02-28 00:27:01 +0000813 NewMIs.push_back(addFrameReference(BuildMI(MF, DL,
814 get(PPC::RESTORE_CRBIT), DestReg),
815 FrameIdx));
816 return true;
Craig Topperabadc662012-04-20 06:31:50 +0000817 } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) {
Hal Finkelfcc51d42013-03-17 04:43:44 +0000818 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LVX), DestReg),
819 FrameIdx));
820 NonRI = true;
Hal Finkela1431df2013-03-21 19:03:21 +0000821 } else if (PPC::VRSAVERCRegClass.hasSubClassEq(RC)) {
Hal Finkela7b06302013-03-27 00:02:20 +0000822 assert(TM.getSubtargetImpl()->isDarwin() &&
823 "VRSAVE only needs spill/restore on Darwin");
Hal Finkela1431df2013-03-21 19:03:21 +0000824 NewMIs.push_back(addFrameReference(BuildMI(MF, DL,
825 get(PPC::RESTORE_VRSAVE),
826 DestReg),
827 FrameIdx));
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000828 SpillsVRS = true;
Owen Andersoneee14602008-01-01 21:11:32 +0000829 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000830 llvm_unreachable("Unknown regclass!");
Owen Andersoneee14602008-01-01 21:11:32 +0000831 }
Hal Finkelbde7f8f2011-12-06 20:55:36 +0000832
833 return false;
Owen Andersoneee14602008-01-01 21:11:32 +0000834}
835
836void
837PPCInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
Bill Wendling632ea652008-03-03 22:19:16 +0000838 MachineBasicBlock::iterator MI,
839 unsigned DestReg, int FrameIdx,
Evan Chengefb126a2010-05-06 19:06:44 +0000840 const TargetRegisterClass *RC,
841 const TargetRegisterInfo *TRI) const {
Dan Gohman3b460302008-07-07 23:14:23 +0000842 MachineFunction &MF = *MBB.getParent();
Owen Andersoneee14602008-01-01 21:11:32 +0000843 SmallVector<MachineInstr*, 4> NewMIs;
Chris Lattner6f306d72010-04-02 20:16:16 +0000844 DebugLoc DL;
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000845 if (MI != MBB.end()) DL = MI->getDebugLoc();
Hal Finkelfcc51d42013-03-17 04:43:44 +0000846
847 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
848 FuncInfo->setHasSpills();
849
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000850 bool NonRI = false, SpillsVRS = false;
851 if (LoadRegFromStackSlot(MF, DL, DestReg, FrameIdx, RC, NewMIs,
852 NonRI, SpillsVRS))
Hal Finkelbde7f8f2011-12-06 20:55:36 +0000853 FuncInfo->setSpillsCR();
Hal Finkelfcc51d42013-03-17 04:43:44 +0000854
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000855 if (SpillsVRS)
856 FuncInfo->setSpillsVRSAVE();
857
Hal Finkelfcc51d42013-03-17 04:43:44 +0000858 if (NonRI)
859 FuncInfo->setHasNonRISpills();
860
Owen Andersoneee14602008-01-01 21:11:32 +0000861 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
862 MBB.insert(MI, NewMIs[i]);
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +0000863
864 const MachineFrameInfo &MFI = *MF.getFrameInfo();
865 MachineMemOperand *MMO =
Jay Foad465101b2011-11-15 07:34:52 +0000866 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx),
Chris Lattnere3d864b2010-09-21 04:39:43 +0000867 MachineMemOperand::MOLoad,
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +0000868 MFI.getObjectSize(FrameIdx),
869 MFI.getObjectAlignment(FrameIdx));
870 NewMIs.back()->addMemOperand(MF, MMO);
Owen Andersoneee14602008-01-01 21:11:32 +0000871}
872
Chris Lattnera47294ed2006-10-13 21:21:17 +0000873bool PPCInstrInfo::
Owen Anderson4f6bf042008-08-14 22:49:33 +0000874ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
Chris Lattner23f22de2006-10-21 06:03:11 +0000875 assert(Cond.size() == 2 && "Invalid PPC branch opcode!");
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000876 if (Cond[1].getReg() == PPC::CTR8 || Cond[1].getReg() == PPC::CTR)
877 Cond[0].setImm(Cond[0].getImm() == 0 ? 1 : 0);
878 else
879 // Leave the CR# the same, but invert the condition.
880 Cond[0].setImm(PPC::InvertPredicate((PPC::Predicate)Cond[0].getImm()));
Chris Lattner23f22de2006-10-21 06:03:11 +0000881 return false;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000882}
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +0000883
Hal Finkeld61d4f82013-04-06 19:30:30 +0000884bool PPCInstrInfo::FoldImmediate(MachineInstr *UseMI, MachineInstr *DefMI,
885 unsigned Reg, MachineRegisterInfo *MRI) const {
886 // For some instructions, it is legal to fold ZERO into the RA register field.
887 // A zero immediate should always be loaded with a single li.
888 unsigned DefOpc = DefMI->getOpcode();
889 if (DefOpc != PPC::LI && DefOpc != PPC::LI8)
890 return false;
891 if (!DefMI->getOperand(1).isImm())
892 return false;
893 if (DefMI->getOperand(1).getImm() != 0)
894 return false;
895
896 // Note that we cannot here invert the arguments of an isel in order to fold
897 // a ZERO into what is presented as the second argument. All we have here
898 // is the condition bit, and that might come from a CR-logical bit operation.
899
900 const MCInstrDesc &UseMCID = UseMI->getDesc();
901
902 // Only fold into real machine instructions.
903 if (UseMCID.isPseudo())
904 return false;
905
906 unsigned UseIdx;
907 for (UseIdx = 0; UseIdx < UseMI->getNumOperands(); ++UseIdx)
908 if (UseMI->getOperand(UseIdx).isReg() &&
909 UseMI->getOperand(UseIdx).getReg() == Reg)
910 break;
911
912 assert(UseIdx < UseMI->getNumOperands() && "Cannot find Reg in UseMI");
913 assert(UseIdx < UseMCID.getNumOperands() && "No operand description for Reg");
914
915 const MCOperandInfo *UseInfo = &UseMCID.OpInfo[UseIdx];
916
917 // We can fold the zero if this register requires a GPRC_NOR0/G8RC_NOX0
918 // register (which might also be specified as a pointer class kind).
919 if (UseInfo->isLookupPtrRegClass()) {
920 if (UseInfo->RegClass /* Kind */ != 1)
921 return false;
922 } else {
923 if (UseInfo->RegClass != PPC::GPRC_NOR0RegClassID &&
924 UseInfo->RegClass != PPC::G8RC_NOX0RegClassID)
925 return false;
926 }
927
928 // Make sure this is not tied to an output register (or otherwise
929 // constrained). This is true for ST?UX registers, for example, which
930 // are tied to their output registers.
931 if (UseInfo->Constraints != 0)
932 return false;
933
934 unsigned ZeroReg;
935 if (UseInfo->isLookupPtrRegClass()) {
936 bool isPPC64 = TM.getSubtargetImpl()->isPPC64();
937 ZeroReg = isPPC64 ? PPC::ZERO8 : PPC::ZERO;
938 } else {
939 ZeroReg = UseInfo->RegClass == PPC::G8RC_NOX0RegClassID ?
940 PPC::ZERO8 : PPC::ZERO;
941 }
942
943 bool DeleteDef = MRI->hasOneNonDBGUse(Reg);
944 UseMI->getOperand(UseIdx).setReg(ZeroReg);
945
946 if (DeleteDef)
947 DefMI->eraseFromParent();
948
949 return true;
950}
951
Hal Finkel30ae2292013-04-10 18:30:16 +0000952static bool MBBDefinesCTR(MachineBasicBlock &MBB) {
953 for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end();
954 I != IE; ++I)
955 if (I->definesRegister(PPC::CTR) || I->definesRegister(PPC::CTR8))
956 return true;
957 return false;
958}
959
960// We should make sure that, if we're going to predicate both sides of a
961// condition (a diamond), that both sides don't define the counter register. We
962// can predicate counter-decrement-based branches, but while that predicates
963// the branching, it does not predicate the counter decrement. If we tried to
964// merge the triangle into one predicated block, we'd decrement the counter
965// twice.
966bool PPCInstrInfo::isProfitableToIfCvt(MachineBasicBlock &TMBB,
967 unsigned NumT, unsigned ExtraT,
968 MachineBasicBlock &FMBB,
969 unsigned NumF, unsigned ExtraF,
970 const BranchProbability &Probability) const {
971 return !(MBBDefinesCTR(TMBB) && MBBDefinesCTR(FMBB));
972}
973
974
Hal Finkel5711eca2013-04-09 22:58:37 +0000975bool PPCInstrInfo::isPredicated(const MachineInstr *MI) const {
Hal Finkelf29285a2013-04-11 01:23:34 +0000976 // The predicated branches are identified by their type, not really by the
977 // explicit presence of a predicate. Furthermore, some of them can be
978 // predicated more than once. Because if conversion won't try to predicate
979 // any instruction which already claims to be predicated (by returning true
980 // here), always return false. In doing so, we let isPredicable() be the
981 // final word on whether not the instruction can be (further) predicated.
982
983 return false;
Hal Finkel5711eca2013-04-09 22:58:37 +0000984}
985
986bool PPCInstrInfo::isUnpredicatedTerminator(const MachineInstr *MI) const {
987 if (!MI->isTerminator())
988 return false;
989
990 // Conditional branch is a special case.
991 if (MI->isBranch() && !MI->isBarrier())
992 return true;
993
994 return !isPredicated(MI);
995}
996
997bool PPCInstrInfo::PredicateInstruction(
998 MachineInstr *MI,
999 const SmallVectorImpl<MachineOperand> &Pred) const {
1000 unsigned OpC = MI->getOpcode();
1001 if (OpC == PPC::BLR) {
1002 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) {
1003 bool isPPC64 = TM.getSubtargetImpl()->isPPC64();
1004 MI->setDesc(get(Pred[0].getImm() ?
1005 (isPPC64 ? PPC::BDNZLR8 : PPC::BDNZLR) :
1006 (isPPC64 ? PPC::BDZLR8 : PPC::BDZLR)));
Hal Finkel940ab932014-02-28 00:27:01 +00001007 } else if (Pred[0].getImm() == PPC::PRED_BIT_SET) {
Hal Finkel5711eca2013-04-09 22:58:37 +00001008 MI->setDesc(get(PPC::BCLR));
1009 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
Hal Finkel940ab932014-02-28 00:27:01 +00001010 .addReg(Pred[1].getReg());
1011 } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) {
1012 MI->setDesc(get(PPC::BCLRn));
1013 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1014 .addReg(Pred[1].getReg());
1015 } else {
1016 MI->setDesc(get(PPC::BCCLR));
1017 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
Hal Finkel5711eca2013-04-09 22:58:37 +00001018 .addImm(Pred[0].getImm())
1019 .addReg(Pred[1].getReg());
1020 }
1021
1022 return true;
1023 } else if (OpC == PPC::B) {
1024 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) {
1025 bool isPPC64 = TM.getSubtargetImpl()->isPPC64();
1026 MI->setDesc(get(Pred[0].getImm() ?
1027 (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
1028 (isPPC64 ? PPC::BDZ8 : PPC::BDZ)));
Hal Finkel940ab932014-02-28 00:27:01 +00001029 } else if (Pred[0].getImm() == PPC::PRED_BIT_SET) {
1030 MachineBasicBlock *MBB = MI->getOperand(0).getMBB();
1031 MI->RemoveOperand(0);
1032
1033 MI->setDesc(get(PPC::BC));
1034 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1035 .addReg(Pred[1].getReg())
1036 .addMBB(MBB);
1037 } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) {
1038 MachineBasicBlock *MBB = MI->getOperand(0).getMBB();
1039 MI->RemoveOperand(0);
1040
1041 MI->setDesc(get(PPC::BCn));
1042 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1043 .addReg(Pred[1].getReg())
1044 .addMBB(MBB);
Hal Finkel5711eca2013-04-09 22:58:37 +00001045 } else {
1046 MachineBasicBlock *MBB = MI->getOperand(0).getMBB();
1047 MI->RemoveOperand(0);
1048
1049 MI->setDesc(get(PPC::BCC));
1050 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1051 .addImm(Pred[0].getImm())
1052 .addReg(Pred[1].getReg())
1053 .addMBB(MBB);
1054 }
1055
1056 return true;
Hal Finkel500b0042013-04-10 06:42:34 +00001057 } else if (OpC == PPC::BCTR || OpC == PPC::BCTR8 ||
1058 OpC == PPC::BCTRL || OpC == PPC::BCTRL8) {
1059 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR)
1060 llvm_unreachable("Cannot predicate bctr[l] on the ctr register");
1061
1062 bool setLR = OpC == PPC::BCTRL || OpC == PPC::BCTRL8;
1063 bool isPPC64 = TM.getSubtargetImpl()->isPPC64();
Hal Finkel940ab932014-02-28 00:27:01 +00001064
1065 if (Pred[0].getImm() == PPC::PRED_BIT_SET) {
1066 MI->setDesc(get(isPPC64 ? (setLR ? PPC::BCCTRL8 : PPC::BCCTR8) :
1067 (setLR ? PPC::BCCTRL : PPC::BCCTR)));
1068 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1069 .addReg(Pred[1].getReg());
1070 return true;
1071 } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) {
1072 MI->setDesc(get(isPPC64 ? (setLR ? PPC::BCCTRL8n : PPC::BCCTR8n) :
1073 (setLR ? PPC::BCCTRLn : PPC::BCCTRn)));
1074 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1075 .addReg(Pred[1].getReg());
1076 return true;
1077 }
1078
1079 MI->setDesc(get(isPPC64 ? (setLR ? PPC::BCCCTRL8 : PPC::BCCCTR8) :
1080 (setLR ? PPC::BCCCTRL : PPC::BCCCTR)));
Hal Finkel500b0042013-04-10 06:42:34 +00001081 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1082 .addImm(Pred[0].getImm())
1083 .addReg(Pred[1].getReg());
1084 return true;
Hal Finkel5711eca2013-04-09 22:58:37 +00001085 }
1086
1087 return false;
1088}
1089
1090bool PPCInstrInfo::SubsumesPredicate(
1091 const SmallVectorImpl<MachineOperand> &Pred1,
1092 const SmallVectorImpl<MachineOperand> &Pred2) const {
1093 assert(Pred1.size() == 2 && "Invalid PPC first predicate");
1094 assert(Pred2.size() == 2 && "Invalid PPC second predicate");
1095
1096 if (Pred1[1].getReg() == PPC::CTR8 || Pred1[1].getReg() == PPC::CTR)
1097 return false;
1098 if (Pred2[1].getReg() == PPC::CTR8 || Pred2[1].getReg() == PPC::CTR)
1099 return false;
1100
Hal Finkel94a6f382013-12-11 23:12:25 +00001101 // P1 can only subsume P2 if they test the same condition register.
1102 if (Pred1[1].getReg() != Pred2[1].getReg())
1103 return false;
1104
Hal Finkel5711eca2013-04-09 22:58:37 +00001105 PPC::Predicate P1 = (PPC::Predicate) Pred1[0].getImm();
1106 PPC::Predicate P2 = (PPC::Predicate) Pred2[0].getImm();
1107
1108 if (P1 == P2)
1109 return true;
1110
1111 // Does P1 subsume P2, e.g. GE subsumes GT.
1112 if (P1 == PPC::PRED_LE &&
1113 (P2 == PPC::PRED_LT || P2 == PPC::PRED_EQ))
1114 return true;
1115 if (P1 == PPC::PRED_GE &&
1116 (P2 == PPC::PRED_GT || P2 == PPC::PRED_EQ))
1117 return true;
1118
1119 return false;
1120}
1121
1122bool PPCInstrInfo::DefinesPredicate(MachineInstr *MI,
1123 std::vector<MachineOperand> &Pred) const {
1124 // Note: At the present time, the contents of Pred from this function is
1125 // unused by IfConversion. This implementation follows ARM by pushing the
1126 // CR-defining operand. Because the 'DZ' and 'DNZ' count as types of
1127 // predicate, instructions defining CTR or CTR8 are also included as
1128 // predicate-defining instructions.
1129
1130 const TargetRegisterClass *RCs[] =
1131 { &PPC::CRRCRegClass, &PPC::CRBITRCRegClass,
1132 &PPC::CTRRCRegClass, &PPC::CTRRC8RegClass };
1133
1134 bool Found = false;
1135 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1136 const MachineOperand &MO = MI->getOperand(i);
Hal Finkelaf822012013-04-10 07:17:47 +00001137 for (unsigned c = 0; c < array_lengthof(RCs) && !Found; ++c) {
Hal Finkel5711eca2013-04-09 22:58:37 +00001138 const TargetRegisterClass *RC = RCs[c];
Hal Finkelaf822012013-04-10 07:17:47 +00001139 if (MO.isReg()) {
1140 if (MO.isDef() && RC->contains(MO.getReg())) {
Hal Finkel5711eca2013-04-09 22:58:37 +00001141 Pred.push_back(MO);
1142 Found = true;
1143 }
Hal Finkelaf822012013-04-10 07:17:47 +00001144 } else if (MO.isRegMask()) {
1145 for (TargetRegisterClass::iterator I = RC->begin(),
1146 IE = RC->end(); I != IE; ++I)
1147 if (MO.clobbersPhysReg(*I)) {
1148 Pred.push_back(MO);
1149 Found = true;
1150 }
Hal Finkel5711eca2013-04-09 22:58:37 +00001151 }
1152 }
1153 }
1154
1155 return Found;
1156}
1157
1158bool PPCInstrInfo::isPredicable(MachineInstr *MI) const {
1159 unsigned OpC = MI->getOpcode();
1160 switch (OpC) {
1161 default:
1162 return false;
1163 case PPC::B:
1164 case PPC::BLR:
Hal Finkel500b0042013-04-10 06:42:34 +00001165 case PPC::BCTR:
1166 case PPC::BCTR8:
1167 case PPC::BCTRL:
1168 case PPC::BCTRL8:
Hal Finkel5711eca2013-04-09 22:58:37 +00001169 return true;
1170 }
1171}
1172
Hal Finkel82656cb2013-04-18 22:15:08 +00001173bool PPCInstrInfo::analyzeCompare(const MachineInstr *MI,
1174 unsigned &SrcReg, unsigned &SrcReg2,
1175 int &Mask, int &Value) const {
1176 unsigned Opc = MI->getOpcode();
1177
1178 switch (Opc) {
1179 default: return false;
1180 case PPC::CMPWI:
1181 case PPC::CMPLWI:
1182 case PPC::CMPDI:
1183 case PPC::CMPLDI:
1184 SrcReg = MI->getOperand(1).getReg();
1185 SrcReg2 = 0;
1186 Value = MI->getOperand(2).getImm();
1187 Mask = 0xFFFF;
1188 return true;
1189 case PPC::CMPW:
1190 case PPC::CMPLW:
1191 case PPC::CMPD:
1192 case PPC::CMPLD:
1193 case PPC::FCMPUS:
1194 case PPC::FCMPUD:
1195 SrcReg = MI->getOperand(1).getReg();
1196 SrcReg2 = MI->getOperand(2).getReg();
1197 return true;
1198 }
1199}
Hal Finkele6322392013-04-19 22:08:38 +00001200
Hal Finkel82656cb2013-04-18 22:15:08 +00001201bool PPCInstrInfo::optimizeCompareInstr(MachineInstr *CmpInstr,
1202 unsigned SrcReg, unsigned SrcReg2,
1203 int Mask, int Value,
1204 const MachineRegisterInfo *MRI) const {
Hal Finkelb12da6b2013-04-18 22:54:25 +00001205 if (DisableCmpOpt)
1206 return false;
1207
Hal Finkel82656cb2013-04-18 22:15:08 +00001208 int OpC = CmpInstr->getOpcode();
1209 unsigned CRReg = CmpInstr->getOperand(0).getReg();
Hal Finkel08e53ee2013-05-08 12:16:14 +00001210
1211 // FP record forms set CR1 based on the execption status bits, not a
1212 // comparison with zero.
1213 if (OpC == PPC::FCMPUS || OpC == PPC::FCMPUD)
1214 return false;
Hal Finkel82656cb2013-04-18 22:15:08 +00001215
1216 // The record forms set the condition register based on a signed comparison
1217 // with zero (so says the ISA manual). This is not as straightforward as it
1218 // seems, however, because this is always a 64-bit comparison on PPC64, even
1219 // for instructions that are 32-bit in nature (like slw for example).
1220 // So, on PPC32, for unsigned comparisons, we can use the record forms only
1221 // for equality checks (as those don't depend on the sign). On PPC64,
1222 // we are restricted to equality for unsigned 64-bit comparisons and for
1223 // signed 32-bit comparisons the applicability is more restricted.
1224 bool isPPC64 = TM.getSubtargetImpl()->isPPC64();
1225 bool is32BitSignedCompare = OpC == PPC::CMPWI || OpC == PPC::CMPW;
1226 bool is32BitUnsignedCompare = OpC == PPC::CMPLWI || OpC == PPC::CMPLW;
1227 bool is64BitUnsignedCompare = OpC == PPC::CMPLDI || OpC == PPC::CMPLD;
1228
1229 // Get the unique definition of SrcReg.
1230 MachineInstr *MI = MRI->getUniqueVRegDef(SrcReg);
1231 if (!MI) return false;
1232 int MIOpC = MI->getOpcode();
1233
1234 bool equalityOnly = false;
1235 bool noSub = false;
1236 if (isPPC64) {
1237 if (is32BitSignedCompare) {
1238 // We can perform this optimization only if MI is sign-extending.
1239 if (MIOpC == PPC::SRAW || MIOpC == PPC::SRAWo ||
1240 MIOpC == PPC::SRAWI || MIOpC == PPC::SRAWIo ||
1241 MIOpC == PPC::EXTSB || MIOpC == PPC::EXTSBo ||
1242 MIOpC == PPC::EXTSH || MIOpC == PPC::EXTSHo ||
1243 MIOpC == PPC::EXTSW || MIOpC == PPC::EXTSWo) {
1244 noSub = true;
1245 } else
1246 return false;
1247 } else if (is32BitUnsignedCompare) {
1248 // We can perform this optimization, equality only, if MI is
1249 // zero-extending.
1250 if (MIOpC == PPC::CNTLZW || MIOpC == PPC::CNTLZWo ||
1251 MIOpC == PPC::SLW || MIOpC == PPC::SLWo ||
1252 MIOpC == PPC::SRW || MIOpC == PPC::SRWo) {
1253 noSub = true;
1254 equalityOnly = true;
1255 } else
1256 return false;
Hal Finkel08e53ee2013-05-08 12:16:14 +00001257 } else
Hal Finkel82656cb2013-04-18 22:15:08 +00001258 equalityOnly = is64BitUnsignedCompare;
Hal Finkel08e53ee2013-05-08 12:16:14 +00001259 } else
Hal Finkel82656cb2013-04-18 22:15:08 +00001260 equalityOnly = is32BitUnsignedCompare;
1261
1262 if (equalityOnly) {
1263 // We need to check the uses of the condition register in order to reject
1264 // non-equality comparisons.
1265 for (MachineRegisterInfo::use_iterator I = MRI->use_begin(CRReg),
1266 IE = MRI->use_end(); I != IE; ++I) {
1267 MachineInstr *UseMI = &*I;
1268 if (UseMI->getOpcode() == PPC::BCC) {
1269 unsigned Pred = UseMI->getOperand(0).getImm();
Hal Finkelc3632452013-05-07 17:49:55 +00001270 if (Pred != PPC::PRED_EQ && Pred != PPC::PRED_NE)
1271 return false;
Hal Finkel82656cb2013-04-18 22:15:08 +00001272 } else if (UseMI->getOpcode() == PPC::ISEL ||
1273 UseMI->getOpcode() == PPC::ISEL8) {
1274 unsigned SubIdx = UseMI->getOperand(3).getSubReg();
Hal Finkelc3632452013-05-07 17:49:55 +00001275 if (SubIdx != PPC::sub_eq)
1276 return false;
Hal Finkel82656cb2013-04-18 22:15:08 +00001277 } else
1278 return false;
1279 }
1280 }
1281
Hal Finkelc3632452013-05-07 17:49:55 +00001282 MachineBasicBlock::iterator I = CmpInstr;
Hal Finkel82656cb2013-04-18 22:15:08 +00001283
1284 // Scan forward to find the first use of the compare.
1285 for (MachineBasicBlock::iterator EL = CmpInstr->getParent()->end();
1286 I != EL; ++I) {
1287 bool FoundUse = false;
1288 for (MachineRegisterInfo::use_iterator J = MRI->use_begin(CRReg),
1289 JE = MRI->use_end(); J != JE; ++J)
1290 if (&*J == &*I) {
1291 FoundUse = true;
1292 break;
1293 }
1294
1295 if (FoundUse)
1296 break;
1297 }
1298
Hal Finkel82656cb2013-04-18 22:15:08 +00001299 // There are two possible candidates which can be changed to set CR[01].
1300 // One is MI, the other is a SUB instruction.
1301 // For CMPrr(r1,r2), we are looking for SUB(r1,r2) or SUB(r2,r1).
1302 MachineInstr *Sub = NULL;
1303 if (SrcReg2 != 0)
1304 // MI is not a candidate for CMPrr.
1305 MI = NULL;
1306 // FIXME: Conservatively refuse to convert an instruction which isn't in the
1307 // same BB as the comparison. This is to allow the check below to avoid calls
1308 // (and other explicit clobbers); instead we should really check for these
1309 // more explicitly (in at least a few predecessors).
1310 else if (MI->getParent() != CmpInstr->getParent() || Value != 0) {
1311 // PPC does not have a record-form SUBri.
1312 return false;
1313 }
1314
1315 // Search for Sub.
1316 const TargetRegisterInfo *TRI = &getRegisterInfo();
1317 --I;
Hal Finkelc3632452013-05-07 17:49:55 +00001318
1319 // Get ready to iterate backward from CmpInstr.
1320 MachineBasicBlock::iterator E = MI,
1321 B = CmpInstr->getParent()->begin();
1322
Hal Finkel82656cb2013-04-18 22:15:08 +00001323 for (; I != E && !noSub; --I) {
1324 const MachineInstr &Instr = *I;
1325 unsigned IOpC = Instr.getOpcode();
1326
1327 if (&*I != CmpInstr && (
Hal Finkel08e53ee2013-05-08 12:16:14 +00001328 Instr.modifiesRegister(PPC::CR0, TRI) ||
1329 Instr.readsRegister(PPC::CR0, TRI)))
Hal Finkel82656cb2013-04-18 22:15:08 +00001330 // This instruction modifies or uses the record condition register after
1331 // the one we want to change. While we could do this transformation, it
1332 // would likely not be profitable. This transformation removes one
1333 // instruction, and so even forcing RA to generate one move probably
1334 // makes it unprofitable.
1335 return false;
1336
1337 // Check whether CmpInstr can be made redundant by the current instruction.
1338 if ((OpC == PPC::CMPW || OpC == PPC::CMPLW ||
1339 OpC == PPC::CMPD || OpC == PPC::CMPLD) &&
1340 (IOpC == PPC::SUBF || IOpC == PPC::SUBF8) &&
1341 ((Instr.getOperand(1).getReg() == SrcReg &&
1342 Instr.getOperand(2).getReg() == SrcReg2) ||
1343 (Instr.getOperand(1).getReg() == SrcReg2 &&
1344 Instr.getOperand(2).getReg() == SrcReg))) {
1345 Sub = &*I;
1346 break;
1347 }
1348
Hal Finkel82656cb2013-04-18 22:15:08 +00001349 if (I == B)
1350 // The 'and' is below the comparison instruction.
1351 return false;
1352 }
1353
1354 // Return false if no candidates exist.
1355 if (!MI && !Sub)
1356 return false;
1357
1358 // The single candidate is called MI.
1359 if (!MI) MI = Sub;
1360
1361 int NewOpC = -1;
1362 MIOpC = MI->getOpcode();
1363 if (MIOpC == PPC::ANDIo || MIOpC == PPC::ANDIo8)
1364 NewOpC = MIOpC;
1365 else {
1366 NewOpC = PPC::getRecordFormOpcode(MIOpC);
1367 if (NewOpC == -1 && PPC::getNonRecordFormOpcode(MIOpC) != -1)
1368 NewOpC = MIOpC;
1369 }
1370
1371 // FIXME: On the non-embedded POWER architectures, only some of the record
1372 // forms are fast, and we should use only the fast ones.
1373
1374 // The defining instruction has a record form (or is already a record
1375 // form). It is possible, however, that we'll need to reverse the condition
1376 // code of the users.
1377 if (NewOpC == -1)
1378 return false;
1379
Hal Finkele6322392013-04-19 22:08:38 +00001380 SmallVector<std::pair<MachineOperand*, PPC::Predicate>, 4> PredsToUpdate;
1381 SmallVector<std::pair<MachineOperand*, unsigned>, 4> SubRegsToUpdate;
Hal Finkel82656cb2013-04-18 22:15:08 +00001382
1383 // If we have SUB(r1, r2) and CMP(r2, r1), the condition code based on CMP
1384 // needs to be updated to be based on SUB. Push the condition code
1385 // operands to OperandsToUpdate. If it is safe to remove CmpInstr, the
1386 // condition code of these operands will be modified.
1387 bool ShouldSwap = false;
1388 if (Sub) {
1389 ShouldSwap = SrcReg2 != 0 && Sub->getOperand(1).getReg() == SrcReg2 &&
1390 Sub->getOperand(2).getReg() == SrcReg;
1391
1392 // The operands to subf are the opposite of sub, so only in the fixed-point
1393 // case, invert the order.
Hal Finkel08e53ee2013-05-08 12:16:14 +00001394 ShouldSwap = !ShouldSwap;
Hal Finkel82656cb2013-04-18 22:15:08 +00001395 }
1396
1397 if (ShouldSwap)
1398 for (MachineRegisterInfo::use_iterator I = MRI->use_begin(CRReg),
1399 IE = MRI->use_end(); I != IE; ++I) {
1400 MachineInstr *UseMI = &*I;
1401 if (UseMI->getOpcode() == PPC::BCC) {
1402 PPC::Predicate Pred = (PPC::Predicate) UseMI->getOperand(0).getImm();
Hal Finkele6322392013-04-19 22:08:38 +00001403 assert((!equalityOnly ||
1404 Pred == PPC::PRED_EQ || Pred == PPC::PRED_NE) &&
1405 "Invalid predicate for equality-only optimization");
1406 PredsToUpdate.push_back(std::make_pair(&((*I).getOperand(0)),
Hal Finkel0f64e212013-04-20 05:16:26 +00001407 PPC::getSwappedPredicate(Pred)));
Hal Finkel82656cb2013-04-18 22:15:08 +00001408 } else if (UseMI->getOpcode() == PPC::ISEL ||
1409 UseMI->getOpcode() == PPC::ISEL8) {
Hal Finkele6322392013-04-19 22:08:38 +00001410 unsigned NewSubReg = UseMI->getOperand(3).getSubReg();
1411 assert((!equalityOnly || NewSubReg == PPC::sub_eq) &&
1412 "Invalid CR bit for equality-only optimization");
1413
1414 if (NewSubReg == PPC::sub_lt)
1415 NewSubReg = PPC::sub_gt;
1416 else if (NewSubReg == PPC::sub_gt)
1417 NewSubReg = PPC::sub_lt;
1418
1419 SubRegsToUpdate.push_back(std::make_pair(&((*I).getOperand(3)),
1420 NewSubReg));
Hal Finkel82656cb2013-04-18 22:15:08 +00001421 } else // We need to abort on a user we don't understand.
1422 return false;
1423 }
1424
1425 // Create a new virtual register to hold the value of the CR set by the
1426 // record-form instruction. If the instruction was not previously in
1427 // record form, then set the kill flag on the CR.
1428 CmpInstr->eraseFromParent();
1429
1430 MachineBasicBlock::iterator MII = MI;
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001431 BuildMI(*MI->getParent(), std::next(MII), MI->getDebugLoc(),
Hal Finkel82656cb2013-04-18 22:15:08 +00001432 get(TargetOpcode::COPY), CRReg)
Hal Finkel08e53ee2013-05-08 12:16:14 +00001433 .addReg(PPC::CR0, MIOpC != NewOpC ? RegState::Kill : 0);
Hal Finkel82656cb2013-04-18 22:15:08 +00001434
1435 if (MIOpC != NewOpC) {
1436 // We need to be careful here: we're replacing one instruction with
1437 // another, and we need to make sure that we get all of the right
1438 // implicit uses and defs. On the other hand, the caller may be holding
1439 // an iterator to this instruction, and so we can't delete it (this is
1440 // specifically the case if this is the instruction directly after the
1441 // compare).
1442
1443 const MCInstrDesc &NewDesc = get(NewOpC);
1444 MI->setDesc(NewDesc);
1445
1446 if (NewDesc.ImplicitDefs)
1447 for (const uint16_t *ImpDefs = NewDesc.getImplicitDefs();
1448 *ImpDefs; ++ImpDefs)
1449 if (!MI->definesRegister(*ImpDefs))
1450 MI->addOperand(*MI->getParent()->getParent(),
1451 MachineOperand::CreateReg(*ImpDefs, true, true));
1452 if (NewDesc.ImplicitUses)
1453 for (const uint16_t *ImpUses = NewDesc.getImplicitUses();
1454 *ImpUses; ++ImpUses)
1455 if (!MI->readsRegister(*ImpUses))
1456 MI->addOperand(*MI->getParent()->getParent(),
1457 MachineOperand::CreateReg(*ImpUses, false, true));
1458 }
1459
1460 // Modify the condition code of operands in OperandsToUpdate.
1461 // Since we have SUB(r1, r2) and CMP(r2, r1), the condition code needs to
1462 // be changed from r2 > r1 to r1 < r2, from r2 < r1 to r1 > r2, etc.
Hal Finkele6322392013-04-19 22:08:38 +00001463 for (unsigned i = 0, e = PredsToUpdate.size(); i < e; i++)
1464 PredsToUpdate[i].first->setImm(PredsToUpdate[i].second);
Hal Finkel82656cb2013-04-18 22:15:08 +00001465
Hal Finkele6322392013-04-19 22:08:38 +00001466 for (unsigned i = 0, e = SubRegsToUpdate.size(); i < e; i++)
1467 SubRegsToUpdate[i].first->setSubReg(SubRegsToUpdate[i].second);
Hal Finkel82656cb2013-04-18 22:15:08 +00001468
1469 return true;
1470}
1471
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +00001472/// GetInstSize - Return the number of bytes of code the specified
1473/// instruction may be. This returns the maximum number of bytes.
1474///
1475unsigned PPCInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
Hal Finkela7bbaf62014-02-02 06:12:27 +00001476 unsigned Opcode = MI->getOpcode();
1477
1478 if (Opcode == PPC::INLINEASM) {
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +00001479 const MachineFunction *MF = MI->getParent()->getParent();
1480 const char *AsmStr = MI->getOperand(0).getSymbolName();
Chris Lattner7b26fce2009-08-22 20:48:53 +00001481 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
Hal Finkela7bbaf62014-02-02 06:12:27 +00001482 } else {
1483 const MCInstrDesc &Desc = get(Opcode);
1484 return Desc.getSize();
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +00001485 }
1486}
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001487
1488#undef DEBUG_TYPE
1489#define DEBUG_TYPE "ppc-early-ret"
1490STATISTIC(NumBCLR, "Number of early conditional returns");
1491STATISTIC(NumBLR, "Number of early returns");
1492
1493namespace llvm {
1494 void initializePPCEarlyReturnPass(PassRegistry&);
1495}
1496
1497namespace {
1498 // PPCEarlyReturn pass - For simple functions without epilogue code, move
1499 // returns up, and create conditional returns, to avoid unnecessary
1500 // branch-to-blr sequences.
1501 struct PPCEarlyReturn : public MachineFunctionPass {
1502 static char ID;
1503 PPCEarlyReturn() : MachineFunctionPass(ID) {
1504 initializePPCEarlyReturnPass(*PassRegistry::getPassRegistry());
1505 }
1506
1507 const PPCTargetMachine *TM;
1508 const PPCInstrInfo *TII;
1509
1510protected:
Hal Finkel21aad9a2013-04-09 18:25:18 +00001511 bool processBlock(MachineBasicBlock &ReturnMBB) {
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001512 bool Changed = false;
1513
Hal Finkel21aad9a2013-04-09 18:25:18 +00001514 MachineBasicBlock::iterator I = ReturnMBB.begin();
1515 I = ReturnMBB.SkipPHIsAndLabels(I);
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001516
1517 // The block must be essentially empty except for the blr.
Hal Finkel21aad9a2013-04-09 18:25:18 +00001518 if (I == ReturnMBB.end() || I->getOpcode() != PPC::BLR ||
1519 I != ReturnMBB.getLastNonDebugInstr())
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001520 return Changed;
1521
1522 SmallVector<MachineBasicBlock*, 8> PredToRemove;
Hal Finkel21aad9a2013-04-09 18:25:18 +00001523 for (MachineBasicBlock::pred_iterator PI = ReturnMBB.pred_begin(),
1524 PIE = ReturnMBB.pred_end(); PI != PIE; ++PI) {
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001525 bool OtherReference = false, BlockChanged = false;
Hal Finkel21aad9a2013-04-09 18:25:18 +00001526 for (MachineBasicBlock::iterator J = (*PI)->getLastNonDebugInstr();;) {
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001527 if (J->getOpcode() == PPC::B) {
Hal Finkel21aad9a2013-04-09 18:25:18 +00001528 if (J->getOperand(0).getMBB() == &ReturnMBB) {
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001529 // This is an unconditional branch to the return. Replace the
Andrew Trick9defbd82013-12-17 04:50:40 +00001530 // branch with a blr.
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001531 BuildMI(**PI, J, J->getDebugLoc(), TII->get(PPC::BLR));
Hal Finkel21aad9a2013-04-09 18:25:18 +00001532 MachineBasicBlock::iterator K = J--;
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001533 K->eraseFromParent();
1534 BlockChanged = true;
1535 ++NumBLR;
1536 continue;
1537 }
1538 } else if (J->getOpcode() == PPC::BCC) {
Hal Finkel21aad9a2013-04-09 18:25:18 +00001539 if (J->getOperand(2).getMBB() == &ReturnMBB) {
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001540 // This is a conditional branch to the return. Replace the branch
1541 // with a bclr.
Hal Finkel940ab932014-02-28 00:27:01 +00001542 BuildMI(**PI, J, J->getDebugLoc(), TII->get(PPC::BCCLR))
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001543 .addImm(J->getOperand(0).getImm())
1544 .addReg(J->getOperand(1).getReg());
Hal Finkel21aad9a2013-04-09 18:25:18 +00001545 MachineBasicBlock::iterator K = J--;
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001546 K->eraseFromParent();
1547 BlockChanged = true;
1548 ++NumBCLR;
1549 continue;
1550 }
Hal Finkel940ab932014-02-28 00:27:01 +00001551 } else if (J->getOpcode() == PPC::BC || J->getOpcode() == PPC::BCn) {
1552 if (J->getOperand(1).getMBB() == &ReturnMBB) {
1553 // This is a conditional branch to the return. Replace the branch
1554 // with a bclr.
1555 BuildMI(**PI, J, J->getDebugLoc(),
1556 TII->get(J->getOpcode() == PPC::BC ?
1557 PPC::BCLR : PPC::BCLRn))
1558 .addReg(J->getOperand(0).getReg());
1559 MachineBasicBlock::iterator K = J--;
1560 K->eraseFromParent();
1561 BlockChanged = true;
1562 ++NumBCLR;
1563 continue;
1564 }
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001565 } else if (J->isBranch()) {
1566 if (J->isIndirectBranch()) {
Hal Finkel21aad9a2013-04-09 18:25:18 +00001567 if (ReturnMBB.hasAddressTaken())
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001568 OtherReference = true;
1569 } else
1570 for (unsigned i = 0; i < J->getNumOperands(); ++i)
1571 if (J->getOperand(i).isMBB() &&
Hal Finkel21aad9a2013-04-09 18:25:18 +00001572 J->getOperand(i).getMBB() == &ReturnMBB)
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001573 OtherReference = true;
Hal Finkel21aad9a2013-04-09 18:25:18 +00001574 } else if (!J->isTerminator() && !J->isDebugValue())
1575 break;
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001576
Hal Finkel21aad9a2013-04-09 18:25:18 +00001577 if (J == (*PI)->begin())
1578 break;
1579
1580 --J;
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001581 }
1582
Hal Finkel21aad9a2013-04-09 18:25:18 +00001583 if ((*PI)->canFallThrough() && (*PI)->isLayoutSuccessor(&ReturnMBB))
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001584 OtherReference = true;
1585
Andrew Trick9defbd82013-12-17 04:50:40 +00001586 // Predecessors are stored in a vector and can't be removed here.
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001587 if (!OtherReference && BlockChanged) {
1588 PredToRemove.push_back(*PI);
1589 }
1590
1591 if (BlockChanged)
1592 Changed = true;
1593 }
1594
1595 for (unsigned i = 0, ie = PredToRemove.size(); i != ie; ++i)
Hal Finkel21aad9a2013-04-09 18:25:18 +00001596 PredToRemove[i]->removeSuccessor(&ReturnMBB);
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001597
Hal Finkel21aad9a2013-04-09 18:25:18 +00001598 if (Changed && !ReturnMBB.hasAddressTaken()) {
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001599 // We now might be able to merge this blr-only block into its
1600 // by-layout predecessor.
Hal Finkel21aad9a2013-04-09 18:25:18 +00001601 if (ReturnMBB.pred_size() == 1 &&
1602 (*ReturnMBB.pred_begin())->isLayoutSuccessor(&ReturnMBB)) {
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001603 // Move the blr into the preceding block.
Hal Finkel21aad9a2013-04-09 18:25:18 +00001604 MachineBasicBlock &PrevMBB = **ReturnMBB.pred_begin();
1605 PrevMBB.splice(PrevMBB.end(), &ReturnMBB, I);
1606 PrevMBB.removeSuccessor(&ReturnMBB);
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001607 }
1608
Hal Finkel21aad9a2013-04-09 18:25:18 +00001609 if (ReturnMBB.pred_empty())
1610 ReturnMBB.eraseFromParent();
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001611 }
1612
1613 return Changed;
1614 }
1615
1616public:
1617 virtual bool runOnMachineFunction(MachineFunction &MF) {
1618 TM = static_cast<const PPCTargetMachine *>(&MF.getTarget());
1619 TII = TM->getInstrInfo();
1620
1621 bool Changed = false;
1622
Hal Finkel21aad9a2013-04-09 18:25:18 +00001623 // If the function does not have at least two blocks, then there is
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001624 // nothing to do.
1625 if (MF.size() < 2)
1626 return Changed;
1627
1628 for (MachineFunction::iterator I = MF.begin(); I != MF.end();) {
Andrew Trick9defbd82013-12-17 04:50:40 +00001629 MachineBasicBlock &B = *I++;
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001630 if (processBlock(B))
1631 Changed = true;
1632 }
1633
1634 return Changed;
1635 }
1636
1637 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
1638 MachineFunctionPass::getAnalysisUsage(AU);
1639 }
1640 };
1641}
1642
1643INITIALIZE_PASS(PPCEarlyReturn, DEBUG_TYPE,
1644 "PowerPC Early-Return Creation", false, false)
1645
1646char PPCEarlyReturn::ID = 0;
1647FunctionPass*
1648llvm::createPPCEarlyReturnPass() { return new PPCEarlyReturn(); }