blob: 208f6304217e834bf23428a52f5465ad6d5fc32a [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"
Hal Finkel174e5902014-03-25 23:29:21 +000023#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +000024#include "llvm/CodeGen/MachineFrameInfo.h"
Hal Finkelb5aa7e52013-04-08 16:24:03 +000025#include "llvm/CodeGen/MachineFunctionPass.h"
Misha Brukman116f9272004-08-17 04:55:41 +000026#include "llvm/CodeGen/MachineInstrBuilder.h"
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +000027#include "llvm/CodeGen/MachineMemOperand.h"
Jakob Stoklund Olesenddbf7a82010-02-26 21:09:24 +000028#include "llvm/CodeGen/MachineRegisterInfo.h"
Hal Finkel9f9f8922012-04-01 19:22:40 +000029#include "llvm/CodeGen/PseudoSourceValue.h"
Hal Finkel174e5902014-03-25 23:29:21 +000030#include "llvm/CodeGen/SlotIndexes.h"
Evan Chengc5e6d2f2011-07-11 03:57:24 +000031#include "llvm/MC/MCAsmInfo.h"
Bill Wendling1af20ad2008-03-04 23:13:51 +000032#include "llvm/Support/CommandLine.h"
Hal Finkel174e5902014-03-25 23:29:21 +000033#include "llvm/Support/Debug.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000034#include "llvm/Support/ErrorHandling.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000035#include "llvm/Support/TargetRegistry.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000036#include "llvm/Support/raw_ostream.h"
Misha Brukman116f9272004-08-17 04:55:41 +000037
Hal Finkel82656cb2013-04-18 22:15:08 +000038#define GET_INSTRMAP_INFO
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000039#define GET_INSTRINFO_CTOR_DTOR
Evan Cheng1e210d02011-06-28 20:07:07 +000040#include "PPCGenInstrInfo.inc"
41
Dan Gohman20857192010-04-15 17:20:57 +000042using namespace llvm;
Bill Wendling1af20ad2008-03-04 23:13:51 +000043
Chandler Carruthe96dd892014-04-21 22:55:11 +000044#define DEBUG_TYPE "ppc-instr-info"
45
Hal Finkel821e0012012-06-08 15:38:25 +000046static cl::
Hal Finkelc6b5deb2012-06-08 19:19:53 +000047opt<bool> DisableCTRLoopAnal("disable-ppc-ctrloop-analysis", cl::Hidden,
48 cl::desc("Disable analysis for CTR loops"));
Hal Finkel821e0012012-06-08 15:38:25 +000049
Hal Finkele6322392013-04-19 22:08:38 +000050static cl::opt<bool> DisableCmpOpt("disable-ppc-cmp-opt",
Hal Finkelb12da6b2013-04-18 22:54:25 +000051cl::desc("Disable compare instruction optimization"), cl::Hidden);
52
Hal Finkel174e5902014-03-25 23:29:21 +000053static cl::opt<bool> DisableVSXFMAMutate("disable-ppc-vsx-fma-mutation",
54cl::desc("Disable VSX FMA instruction mutation"), cl::Hidden);
55
Hal Finkel9dcb3582014-03-27 22:46:28 +000056static cl::opt<bool> VSXSelfCopyCrash("crash-on-ppc-vsx-self-copy",
57cl::desc("Causes the backend to crash instead of generating a nop VSX copy"),
58cl::Hidden);
59
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000060// Pin the vtable to this file.
61void PPCInstrInfo::anchor() {}
62
Chris Lattner49cadab2006-06-17 00:01:04 +000063PPCInstrInfo::PPCInstrInfo(PPCTargetMachine &tm)
Evan Cheng703a0fb2011-07-01 17:57:27 +000064 : PPCGenInstrInfo(PPC::ADJCALLSTACKDOWN, PPC::ADJCALLSTACKUP),
Bill Wendling5e7656b2013-06-07 07:55:53 +000065 TM(tm), RI(*TM.getSubtargetImpl()) {}
Chris Lattner49cadab2006-06-17 00:01:04 +000066
Andrew Trick10ffc2b2010-12-24 05:03:26 +000067/// CreateTargetHazardRecognizer - Return the hazard recognizer to use for
68/// this target when scheduling the DAG.
69ScheduleHazardRecognizer *PPCInstrInfo::CreateTargetHazardRecognizer(
70 const TargetMachine *TM,
71 const ScheduleDAG *DAG) const {
Hal Finkel6fa56972011-10-17 04:03:49 +000072 unsigned Directive = TM->getSubtarget<PPCSubtarget>().getDarwinDirective();
Hal Finkel742b5352012-08-28 16:12:39 +000073 if (Directive == PPC::DIR_440 || Directive == PPC::DIR_A2 ||
74 Directive == PPC::DIR_E500mc || Directive == PPC::DIR_E5500) {
Hal Finkel6f0ae782011-11-22 16:21:04 +000075 const InstrItineraryData *II = TM->getInstrItineraryData();
Hal Finkel563cc052013-12-02 23:52:46 +000076 return new ScoreboardHazardRecognizer(II, DAG);
Hal Finkel6fa56972011-10-17 04:03:49 +000077 }
Hal Finkel58ca3602011-12-02 04:58:02 +000078
Jakob Stoklund Olesen9de596e2012-11-28 02:35:17 +000079 return TargetInstrInfo::CreateTargetHazardRecognizer(TM, DAG);
Andrew Trick10ffc2b2010-12-24 05:03:26 +000080}
81
Hal Finkel58ca3602011-12-02 04:58:02 +000082/// CreateTargetPostRAHazardRecognizer - Return the postRA hazard recognizer
83/// to use for this target when scheduling the DAG.
84ScheduleHazardRecognizer *PPCInstrInfo::CreateTargetPostRAHazardRecognizer(
85 const InstrItineraryData *II,
86 const ScheduleDAG *DAG) const {
87 unsigned Directive = TM.getSubtarget<PPCSubtarget>().getDarwinDirective();
88
Hal Finkelceb1f122013-12-12 00:19:11 +000089 if (Directive == PPC::DIR_PWR7)
90 return new PPCDispatchGroupSBHazardRecognizer(II, DAG);
91
Hal Finkel58ca3602011-12-02 04:58:02 +000092 // Most subtargets use a PPC970 recognizer.
Hal Finkel742b5352012-08-28 16:12:39 +000093 if (Directive != PPC::DIR_440 && Directive != PPC::DIR_A2 &&
94 Directive != PPC::DIR_E500mc && Directive != PPC::DIR_E5500) {
Benjamin Kramerf0ec1992013-06-07 11:23:35 +000095 assert(TM.getInstrInfo() && "No InstrInfo?");
Hal Finkel58ca3602011-12-02 04:58:02 +000096
Bill Wendling5e7656b2013-06-07 07:55:53 +000097 return new PPCHazardRecognizer970(TM);
Hal Finkel58ca3602011-12-02 04:58:02 +000098 }
99
Hal Finkel563cc052013-12-02 23:52:46 +0000100 return new ScoreboardHazardRecognizer(II, DAG);
Hal Finkel58ca3602011-12-02 04:58:02 +0000101}
Jakob Stoklund Olesen0f855e42012-06-19 21:14:34 +0000102
Hal Finkelceb1f122013-12-12 00:19:11 +0000103
104int PPCInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
105 const MachineInstr *DefMI, unsigned DefIdx,
106 const MachineInstr *UseMI,
107 unsigned UseIdx) const {
108 int Latency = PPCGenInstrInfo::getOperandLatency(ItinData, DefMI, DefIdx,
109 UseMI, UseIdx);
110
111 const MachineOperand &DefMO = DefMI->getOperand(DefIdx);
112 unsigned Reg = DefMO.getReg();
113
114 const TargetRegisterInfo *TRI = &getRegisterInfo();
115 bool IsRegCR;
116 if (TRI->isVirtualRegister(Reg)) {
117 const MachineRegisterInfo *MRI =
118 &DefMI->getParent()->getParent()->getRegInfo();
119 IsRegCR = MRI->getRegClass(Reg)->hasSuperClassEq(&PPC::CRRCRegClass) ||
120 MRI->getRegClass(Reg)->hasSuperClassEq(&PPC::CRBITRCRegClass);
121 } else {
122 IsRegCR = PPC::CRRCRegClass.contains(Reg) ||
123 PPC::CRBITRCRegClass.contains(Reg);
124 }
125
126 if (UseMI->isBranch() && IsRegCR) {
127 if (Latency < 0)
128 Latency = getInstrLatency(ItinData, DefMI);
129
130 // On some cores, there is an additional delay between writing to a condition
131 // register, and using it from a branch.
132 unsigned Directive = TM.getSubtarget<PPCSubtarget>().getDarwinDirective();
133 switch (Directive) {
134 default: break;
135 case PPC::DIR_7400:
136 case PPC::DIR_750:
137 case PPC::DIR_970:
138 case PPC::DIR_E5500:
139 case PPC::DIR_PWR4:
140 case PPC::DIR_PWR5:
141 case PPC::DIR_PWR5X:
142 case PPC::DIR_PWR6:
143 case PPC::DIR_PWR6X:
144 case PPC::DIR_PWR7:
145 Latency += 2;
146 break;
147 }
148 }
149
150 return Latency;
151}
152
Jakob Stoklund Olesen0f855e42012-06-19 21:14:34 +0000153// Detect 32 -> 64-bit extensions where we may reuse the low sub-register.
154bool PPCInstrInfo::isCoalescableExtInstr(const MachineInstr &MI,
155 unsigned &SrcReg, unsigned &DstReg,
156 unsigned &SubIdx) const {
157 switch (MI.getOpcode()) {
158 default: return false;
159 case PPC::EXTSW:
160 case PPC::EXTSW_32_64:
161 SrcReg = MI.getOperand(1).getReg();
162 DstReg = MI.getOperand(0).getReg();
163 SubIdx = PPC::sub_32;
164 return true;
165 }
166}
167
Andrew Trickc416ba62010-12-24 04:28:06 +0000168unsigned PPCInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
Chris Lattner91400bd2006-03-16 22:24:02 +0000169 int &FrameIndex) const {
Hal Finkel37714b82013-03-27 21:21:15 +0000170 // Note: This list must be kept consistent with LoadRegFromStackSlot.
Chris Lattnerbb53acd2006-02-02 20:12:32 +0000171 switch (MI->getOpcode()) {
172 default: break;
173 case PPC::LD:
174 case PPC::LWZ:
175 case PPC::LFS:
176 case PPC::LFD:
Hal Finkel37714b82013-03-27 21:21:15 +0000177 case PPC::RESTORE_CR:
Hal Finkel940ab932014-02-28 00:27:01 +0000178 case PPC::RESTORE_CRBIT:
Hal Finkel37714b82013-03-27 21:21:15 +0000179 case PPC::LVX:
Hal Finkel27774d92014-03-13 07:58:58 +0000180 case PPC::LXVD2X:
Hal Finkel37714b82013-03-27 21:21:15 +0000181 case PPC::RESTORE_VRSAVE:
182 // Check for the operands added by addFrameReference (the immediate is the
183 // offset which defaults to 0).
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000184 if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
185 MI->getOperand(2).isFI()) {
Chris Lattnera5bb3702007-12-30 23:10:15 +0000186 FrameIndex = MI->getOperand(2).getIndex();
Chris Lattnerbb53acd2006-02-02 20:12:32 +0000187 return MI->getOperand(0).getReg();
188 }
189 break;
190 }
191 return 0;
Chris Lattnerc327d712006-02-02 20:16:12 +0000192}
Chris Lattnerbb53acd2006-02-02 20:12:32 +0000193
Andrew Trickc416ba62010-12-24 04:28:06 +0000194unsigned PPCInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
Chris Lattnerc327d712006-02-02 20:16:12 +0000195 int &FrameIndex) const {
Hal Finkel37714b82013-03-27 21:21:15 +0000196 // Note: This list must be kept consistent with StoreRegToStackSlot.
Chris Lattnerc327d712006-02-02 20:16:12 +0000197 switch (MI->getOpcode()) {
198 default: break;
Nate Begeman4efb3282006-02-02 21:07:50 +0000199 case PPC::STD:
Chris Lattnerc327d712006-02-02 20:16:12 +0000200 case PPC::STW:
201 case PPC::STFS:
202 case PPC::STFD:
Hal Finkel37714b82013-03-27 21:21:15 +0000203 case PPC::SPILL_CR:
Hal Finkel940ab932014-02-28 00:27:01 +0000204 case PPC::SPILL_CRBIT:
Hal Finkel37714b82013-03-27 21:21:15 +0000205 case PPC::STVX:
Hal Finkel27774d92014-03-13 07:58:58 +0000206 case PPC::STXVD2X:
Hal Finkel37714b82013-03-27 21:21:15 +0000207 case PPC::SPILL_VRSAVE:
208 // Check for the operands added by addFrameReference (the immediate is the
209 // offset which defaults to 0).
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000210 if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
211 MI->getOperand(2).isFI()) {
Chris Lattnera5bb3702007-12-30 23:10:15 +0000212 FrameIndex = MI->getOperand(2).getIndex();
Chris Lattnerc327d712006-02-02 20:16:12 +0000213 return MI->getOperand(0).getReg();
214 }
215 break;
216 }
217 return 0;
218}
Chris Lattnerbb53acd2006-02-02 20:12:32 +0000219
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000220// commuteInstruction - We can commute rlwimi instructions, but only if the
221// rotate amt is zero. We also have to munge the immediates a bit.
Evan Cheng03553bb2008-06-16 07:33:11 +0000222MachineInstr *
223PPCInstrInfo::commuteInstruction(MachineInstr *MI, bool NewMI) const {
Dan Gohman3b460302008-07-07 23:14:23 +0000224 MachineFunction &MF = *MI->getParent()->getParent();
225
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000226 // Normal instructions can be commuted the obvious way.
Hal Finkel654d43b2013-04-12 02:18:09 +0000227 if (MI->getOpcode() != PPC::RLWIMI &&
Hal Finkel940ab932014-02-28 00:27:01 +0000228 MI->getOpcode() != PPC::RLWIMIo &&
229 MI->getOpcode() != PPC::RLWIMI8 &&
230 MI->getOpcode() != PPC::RLWIMI8o)
Jakob Stoklund Olesen9de596e2012-11-28 02:35:17 +0000231 return TargetInstrInfo::commuteInstruction(MI, NewMI);
Andrew Trickc416ba62010-12-24 04:28:06 +0000232
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000233 // Cannot commute if it has a non-zero rotate count.
Chris Lattner5c463782007-12-30 20:49:49 +0000234 if (MI->getOperand(3).getImm() != 0)
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000235 return 0;
Andrew Trickc416ba62010-12-24 04:28:06 +0000236
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000237 // If we have a zero rotate count, we have:
238 // M = mask(MB,ME)
239 // Op0 = (Op1 & ~M) | (Op2 & M)
240 // Change this to:
241 // M = mask((ME+1)&31, (MB-1)&31)
242 // Op0 = (Op2 & ~M) | (Op1 & M)
243
244 // Swap op1/op2
Evan Cheng244183e2008-02-13 02:46:49 +0000245 unsigned Reg0 = MI->getOperand(0).getReg();
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000246 unsigned Reg1 = MI->getOperand(1).getReg();
247 unsigned Reg2 = MI->getOperand(2).getReg();
Andrew Tricke3398282013-12-17 04:50:45 +0000248 unsigned SubReg1 = MI->getOperand(1).getSubReg();
249 unsigned SubReg2 = MI->getOperand(2).getSubReg();
Evan Chengdc2c8742006-11-15 20:58:11 +0000250 bool Reg1IsKill = MI->getOperand(1).isKill();
251 bool Reg2IsKill = MI->getOperand(2).isKill();
Evan Cheng03553bb2008-06-16 07:33:11 +0000252 bool ChangeReg0 = false;
Evan Cheng244183e2008-02-13 02:46:49 +0000253 // If machine instrs are no longer in two-address forms, update
254 // destination register as well.
255 if (Reg0 == Reg1) {
256 // Must be two address instruction!
Evan Cheng6cc775f2011-06-28 19:10:37 +0000257 assert(MI->getDesc().getOperandConstraint(0, MCOI::TIED_TO) &&
Evan Cheng244183e2008-02-13 02:46:49 +0000258 "Expecting a two-address instruction!");
Andrew Tricke3398282013-12-17 04:50:45 +0000259 assert(MI->getOperand(0).getSubReg() == SubReg1 && "Tied subreg mismatch");
Evan Cheng244183e2008-02-13 02:46:49 +0000260 Reg2IsKill = false;
Evan Cheng03553bb2008-06-16 07:33:11 +0000261 ChangeReg0 = true;
Evan Cheng244183e2008-02-13 02:46:49 +0000262 }
Evan Cheng03553bb2008-06-16 07:33:11 +0000263
264 // Masks.
265 unsigned MB = MI->getOperand(4).getImm();
266 unsigned ME = MI->getOperand(5).getImm();
267
268 if (NewMI) {
269 // Create a new instruction.
270 unsigned Reg0 = ChangeReg0 ? Reg2 : MI->getOperand(0).getReg();
271 bool Reg0IsDead = MI->getOperand(0).isDead();
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000272 return BuildMI(MF, MI->getDebugLoc(), MI->getDesc())
Bill Wendlingf7b83c72009-05-13 21:33:08 +0000273 .addReg(Reg0, RegState::Define | getDeadRegState(Reg0IsDead))
274 .addReg(Reg2, getKillRegState(Reg2IsKill))
275 .addReg(Reg1, getKillRegState(Reg1IsKill))
Evan Cheng03553bb2008-06-16 07:33:11 +0000276 .addImm((ME+1) & 31)
277 .addImm((MB-1) & 31);
278 }
279
Andrew Tricke3398282013-12-17 04:50:45 +0000280 if (ChangeReg0) {
Evan Cheng03553bb2008-06-16 07:33:11 +0000281 MI->getOperand(0).setReg(Reg2);
Andrew Tricke3398282013-12-17 04:50:45 +0000282 MI->getOperand(0).setSubReg(SubReg2);
283 }
Chris Lattner10d63412006-05-04 17:52:23 +0000284 MI->getOperand(2).setReg(Reg1);
285 MI->getOperand(1).setReg(Reg2);
Andrew Tricke3398282013-12-17 04:50:45 +0000286 MI->getOperand(2).setSubReg(SubReg1);
287 MI->getOperand(1).setSubReg(SubReg2);
Chris Lattner60055892007-12-30 21:56:09 +0000288 MI->getOperand(2).setIsKill(Reg1IsKill);
289 MI->getOperand(1).setIsKill(Reg2IsKill);
Andrew Trickc416ba62010-12-24 04:28:06 +0000290
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000291 // Swap the mask around.
Chris Lattner5c463782007-12-30 20:49:49 +0000292 MI->getOperand(4).setImm((ME+1) & 31);
293 MI->getOperand(5).setImm((MB-1) & 31);
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000294 return MI;
295}
Chris Lattnerea79d9fd732006-03-05 23:49:55 +0000296
Hal Finkel6c32ff32014-03-25 19:26:43 +0000297bool PPCInstrInfo::findCommutedOpIndices(MachineInstr *MI, unsigned &SrcOpIdx1,
298 unsigned &SrcOpIdx2) const {
299 // For VSX A-Type FMA instructions, it is the first two operands that can be
300 // commuted, however, because the non-encoded tied input operand is listed
301 // first, the operands to swap are actually the second and third.
302
303 int AltOpc = PPC::getAltVSXFMAOpcode(MI->getOpcode());
304 if (AltOpc == -1)
305 return TargetInstrInfo::findCommutedOpIndices(MI, SrcOpIdx1, SrcOpIdx2);
306
307 SrcOpIdx1 = 2;
308 SrcOpIdx2 = 3;
309 return true;
310}
311
Andrew Trickc416ba62010-12-24 04:28:06 +0000312void PPCInstrInfo::insertNoop(MachineBasicBlock &MBB,
Chris Lattnerea79d9fd732006-03-05 23:49:55 +0000313 MachineBasicBlock::iterator MI) const {
Hal Finkelceb1f122013-12-12 00:19:11 +0000314 // This function is used for scheduling, and the nop wanted here is the type
315 // that terminates dispatch groups on the POWER cores.
316 unsigned Directive = TM.getSubtarget<PPCSubtarget>().getDarwinDirective();
317 unsigned Opcode;
318 switch (Directive) {
319 default: Opcode = PPC::NOP; break;
320 case PPC::DIR_PWR6: Opcode = PPC::NOP_GT_PWR6; break;
321 case PPC::DIR_PWR7: Opcode = PPC::NOP_GT_PWR7; break;
322 }
Chris Lattnera47294ed2006-10-13 21:21:17 +0000323
Hal Finkelceb1f122013-12-12 00:19:11 +0000324 DebugLoc DL;
325 BuildMI(MBB, MI, DL, get(Opcode));
326}
Chris Lattnera47294ed2006-10-13 21:21:17 +0000327
328// Branch analysis.
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000329// Note: If the condition register is set to CTR or CTR8 then this is a
330// BDNZ (imm == 1) or BDZ (imm == 0) branch.
Chris Lattnera47294ed2006-10-13 21:21:17 +0000331bool PPCInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
332 MachineBasicBlock *&FBB,
Evan Cheng64dfcac2009-02-09 07:14:22 +0000333 SmallVectorImpl<MachineOperand> &Cond,
334 bool AllowModify) const {
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000335 bool isPPC64 = TM.getSubtargetImpl()->isPPC64();
336
Chris Lattnera47294ed2006-10-13 21:21:17 +0000337 // If the block has no terminators, it just falls into the block after it.
338 MachineBasicBlock::iterator I = MBB.end();
Dale Johannesen4244d122010-04-02 01:38:09 +0000339 if (I == MBB.begin())
340 return false;
341 --I;
342 while (I->isDebugValue()) {
343 if (I == MBB.begin())
344 return false;
345 --I;
346 }
347 if (!isUnpredicatedTerminator(I))
Chris Lattnera47294ed2006-10-13 21:21:17 +0000348 return false;
349
350 // Get the last instruction in the block.
351 MachineInstr *LastInst = I;
Andrew Trickc416ba62010-12-24 04:28:06 +0000352
Chris Lattnera47294ed2006-10-13 21:21:17 +0000353 // If there is only one terminator instruction, process it.
Evan Cheng5514bbe2007-06-08 21:59:56 +0000354 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
Chris Lattnera47294ed2006-10-13 21:21:17 +0000355 if (LastInst->getOpcode() == PPC::B) {
Evan Cheng8f43afd2009-05-08 23:09:25 +0000356 if (!LastInst->getOperand(0).isMBB())
357 return true;
Chris Lattnera5bb3702007-12-30 23:10:15 +0000358 TBB = LastInst->getOperand(0).getMBB();
Chris Lattnera47294ed2006-10-13 21:21:17 +0000359 return false;
Chris Lattnere0263792006-11-17 22:14:47 +0000360 } else if (LastInst->getOpcode() == PPC::BCC) {
Evan Cheng8f43afd2009-05-08 23:09:25 +0000361 if (!LastInst->getOperand(2).isMBB())
362 return true;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000363 // Block ends with fall-through condbranch.
Chris Lattnera5bb3702007-12-30 23:10:15 +0000364 TBB = LastInst->getOperand(2).getMBB();
Chris Lattnera47294ed2006-10-13 21:21:17 +0000365 Cond.push_back(LastInst->getOperand(0));
366 Cond.push_back(LastInst->getOperand(1));
Chris Lattner23f22de2006-10-21 06:03:11 +0000367 return false;
Hal Finkel940ab932014-02-28 00:27:01 +0000368 } else if (LastInst->getOpcode() == PPC::BC) {
369 if (!LastInst->getOperand(1).isMBB())
370 return true;
371 // Block ends with fall-through condbranch.
372 TBB = LastInst->getOperand(1).getMBB();
373 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET));
374 Cond.push_back(LastInst->getOperand(0));
375 return false;
376 } else if (LastInst->getOpcode() == PPC::BCn) {
377 if (!LastInst->getOperand(1).isMBB())
378 return true;
379 // Block ends with fall-through condbranch.
380 TBB = LastInst->getOperand(1).getMBB();
381 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_UNSET));
382 Cond.push_back(LastInst->getOperand(0));
383 return false;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000384 } else if (LastInst->getOpcode() == PPC::BDNZ8 ||
385 LastInst->getOpcode() == PPC::BDNZ) {
386 if (!LastInst->getOperand(0).isMBB())
387 return true;
Hal Finkelc6b5deb2012-06-08 19:19:53 +0000388 if (DisableCTRLoopAnal)
Hal Finkel821e0012012-06-08 15:38:25 +0000389 return true;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000390 TBB = LastInst->getOperand(0).getMBB();
391 Cond.push_back(MachineOperand::CreateImm(1));
392 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
393 true));
394 return false;
395 } else if (LastInst->getOpcode() == PPC::BDZ8 ||
396 LastInst->getOpcode() == PPC::BDZ) {
397 if (!LastInst->getOperand(0).isMBB())
398 return true;
Hal Finkelc6b5deb2012-06-08 19:19:53 +0000399 if (DisableCTRLoopAnal)
Hal Finkel821e0012012-06-08 15:38:25 +0000400 return true;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000401 TBB = LastInst->getOperand(0).getMBB();
402 Cond.push_back(MachineOperand::CreateImm(0));
403 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
404 true));
405 return false;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000406 }
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000407
Chris Lattnera47294ed2006-10-13 21:21:17 +0000408 // Otherwise, don't know what this is.
409 return true;
410 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000411
Chris Lattnera47294ed2006-10-13 21:21:17 +0000412 // Get the instruction before it if it's a terminator.
413 MachineInstr *SecondLastInst = I;
414
415 // If there are three terminators, we don't know what sort of block this is.
416 if (SecondLastInst && I != MBB.begin() &&
Evan Cheng5514bbe2007-06-08 21:59:56 +0000417 isUnpredicatedTerminator(--I))
Chris Lattnera47294ed2006-10-13 21:21:17 +0000418 return true;
Andrew Trickc416ba62010-12-24 04:28:06 +0000419
Chris Lattnere0263792006-11-17 22:14:47 +0000420 // If the block ends with PPC::B and PPC:BCC, handle it.
Andrew Trickc416ba62010-12-24 04:28:06 +0000421 if (SecondLastInst->getOpcode() == PPC::BCC &&
Chris Lattnera47294ed2006-10-13 21:21:17 +0000422 LastInst->getOpcode() == PPC::B) {
Evan Cheng8f43afd2009-05-08 23:09:25 +0000423 if (!SecondLastInst->getOperand(2).isMBB() ||
424 !LastInst->getOperand(0).isMBB())
425 return true;
Chris Lattnera5bb3702007-12-30 23:10:15 +0000426 TBB = SecondLastInst->getOperand(2).getMBB();
Chris Lattnera47294ed2006-10-13 21:21:17 +0000427 Cond.push_back(SecondLastInst->getOperand(0));
428 Cond.push_back(SecondLastInst->getOperand(1));
Chris Lattnera5bb3702007-12-30 23:10:15 +0000429 FBB = LastInst->getOperand(0).getMBB();
Chris Lattnera47294ed2006-10-13 21:21:17 +0000430 return false;
Hal Finkel940ab932014-02-28 00:27:01 +0000431 } else if (SecondLastInst->getOpcode() == PPC::BC &&
432 LastInst->getOpcode() == PPC::B) {
433 if (!SecondLastInst->getOperand(1).isMBB() ||
434 !LastInst->getOperand(0).isMBB())
435 return true;
436 TBB = SecondLastInst->getOperand(1).getMBB();
437 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET));
438 Cond.push_back(SecondLastInst->getOperand(0));
439 FBB = LastInst->getOperand(0).getMBB();
440 return false;
441 } else if (SecondLastInst->getOpcode() == PPC::BCn &&
442 LastInst->getOpcode() == PPC::B) {
443 if (!SecondLastInst->getOperand(1).isMBB() ||
444 !LastInst->getOperand(0).isMBB())
445 return true;
446 TBB = SecondLastInst->getOperand(1).getMBB();
447 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_UNSET));
448 Cond.push_back(SecondLastInst->getOperand(0));
449 FBB = LastInst->getOperand(0).getMBB();
450 return false;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000451 } else if ((SecondLastInst->getOpcode() == PPC::BDNZ8 ||
452 SecondLastInst->getOpcode() == PPC::BDNZ) &&
453 LastInst->getOpcode() == PPC::B) {
454 if (!SecondLastInst->getOperand(0).isMBB() ||
455 !LastInst->getOperand(0).isMBB())
456 return true;
Hal Finkelc6b5deb2012-06-08 19:19:53 +0000457 if (DisableCTRLoopAnal)
Hal Finkel821e0012012-06-08 15:38:25 +0000458 return true;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000459 TBB = SecondLastInst->getOperand(0).getMBB();
460 Cond.push_back(MachineOperand::CreateImm(1));
461 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
462 true));
463 FBB = LastInst->getOperand(0).getMBB();
464 return false;
465 } else if ((SecondLastInst->getOpcode() == PPC::BDZ8 ||
466 SecondLastInst->getOpcode() == PPC::BDZ) &&
467 LastInst->getOpcode() == PPC::B) {
468 if (!SecondLastInst->getOperand(0).isMBB() ||
469 !LastInst->getOperand(0).isMBB())
470 return true;
Hal Finkelc6b5deb2012-06-08 19:19:53 +0000471 if (DisableCTRLoopAnal)
Hal Finkel821e0012012-06-08 15:38:25 +0000472 return true;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000473 TBB = SecondLastInst->getOperand(0).getMBB();
474 Cond.push_back(MachineOperand::CreateImm(0));
475 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
476 true));
477 FBB = LastInst->getOperand(0).getMBB();
478 return false;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000479 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000480
Dale Johannesenc6855462007-06-13 17:59:52 +0000481 // If the block ends with two PPC:Bs, handle it. The second one is not
482 // executed, so remove it.
Andrew Trickc416ba62010-12-24 04:28:06 +0000483 if (SecondLastInst->getOpcode() == PPC::B &&
Dale Johannesenc6855462007-06-13 17:59:52 +0000484 LastInst->getOpcode() == PPC::B) {
Evan Cheng8f43afd2009-05-08 23:09:25 +0000485 if (!SecondLastInst->getOperand(0).isMBB())
486 return true;
Chris Lattnera5bb3702007-12-30 23:10:15 +0000487 TBB = SecondLastInst->getOperand(0).getMBB();
Dale Johannesenc6855462007-06-13 17:59:52 +0000488 I = LastInst;
Evan Cheng64dfcac2009-02-09 07:14:22 +0000489 if (AllowModify)
490 I->eraseFromParent();
Dale Johannesenc6855462007-06-13 17:59:52 +0000491 return false;
492 }
493
Chris Lattnera47294ed2006-10-13 21:21:17 +0000494 // Otherwise, can't handle this.
495 return true;
496}
497
Evan Cheng99be49d2007-05-18 00:05:48 +0000498unsigned PPCInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
Chris Lattnera47294ed2006-10-13 21:21:17 +0000499 MachineBasicBlock::iterator I = MBB.end();
Evan Cheng99be49d2007-05-18 00:05:48 +0000500 if (I == MBB.begin()) return 0;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000501 --I;
Dale Johannesen4244d122010-04-02 01:38:09 +0000502 while (I->isDebugValue()) {
503 if (I == MBB.begin())
504 return 0;
505 --I;
506 }
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000507 if (I->getOpcode() != PPC::B && I->getOpcode() != PPC::BCC &&
Hal Finkel940ab932014-02-28 00:27:01 +0000508 I->getOpcode() != PPC::BC && I->getOpcode() != PPC::BCn &&
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000509 I->getOpcode() != PPC::BDNZ8 && I->getOpcode() != PPC::BDNZ &&
510 I->getOpcode() != PPC::BDZ8 && I->getOpcode() != PPC::BDZ)
Evan Cheng99be49d2007-05-18 00:05:48 +0000511 return 0;
Andrew Trickc416ba62010-12-24 04:28:06 +0000512
Chris Lattnera47294ed2006-10-13 21:21:17 +0000513 // Remove the branch.
514 I->eraseFromParent();
Andrew Trickc416ba62010-12-24 04:28:06 +0000515
Chris Lattnera47294ed2006-10-13 21:21:17 +0000516 I = MBB.end();
517
Evan Cheng99be49d2007-05-18 00:05:48 +0000518 if (I == MBB.begin()) return 1;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000519 --I;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000520 if (I->getOpcode() != PPC::BCC &&
Hal Finkel940ab932014-02-28 00:27:01 +0000521 I->getOpcode() != PPC::BC && I->getOpcode() != PPC::BCn &&
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000522 I->getOpcode() != PPC::BDNZ8 && I->getOpcode() != PPC::BDNZ &&
523 I->getOpcode() != PPC::BDZ8 && I->getOpcode() != PPC::BDZ)
Evan Cheng99be49d2007-05-18 00:05:48 +0000524 return 1;
Andrew Trickc416ba62010-12-24 04:28:06 +0000525
Chris Lattnera47294ed2006-10-13 21:21:17 +0000526 // Remove the branch.
527 I->eraseFromParent();
Evan Cheng99be49d2007-05-18 00:05:48 +0000528 return 2;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000529}
530
Evan Cheng99be49d2007-05-18 00:05:48 +0000531unsigned
532PPCInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
533 MachineBasicBlock *FBB,
Stuart Hastings0125b642010-06-17 22:43:56 +0000534 const SmallVectorImpl<MachineOperand> &Cond,
535 DebugLoc DL) const {
Chris Lattnera61f0102006-10-17 18:06:55 +0000536 // Shouldn't be a fall through.
537 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Andrew Trickc416ba62010-12-24 04:28:06 +0000538 assert((Cond.size() == 2 || Cond.size() == 0) &&
Chris Lattner94e04442006-10-21 05:36:13 +0000539 "PPC branch conditions have two components!");
Andrew Trickc416ba62010-12-24 04:28:06 +0000540
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000541 bool isPPC64 = TM.getSubtargetImpl()->isPPC64();
542
Chris Lattner94e04442006-10-21 05:36:13 +0000543 // One-way branch.
Chris Lattnera61f0102006-10-17 18:06:55 +0000544 if (FBB == 0) {
Chris Lattner94e04442006-10-21 05:36:13 +0000545 if (Cond.empty()) // Unconditional branch
Stuart Hastings0125b642010-06-17 22:43:56 +0000546 BuildMI(&MBB, DL, get(PPC::B)).addMBB(TBB);
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000547 else if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
548 BuildMI(&MBB, DL, get(Cond[0].getImm() ?
549 (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
550 (isPPC64 ? PPC::BDZ8 : PPC::BDZ))).addMBB(TBB);
Hal Finkel940ab932014-02-28 00:27:01 +0000551 else if (Cond[0].getImm() == PPC::PRED_BIT_SET)
552 BuildMI(&MBB, DL, get(PPC::BC)).addOperand(Cond[1]).addMBB(TBB);
553 else if (Cond[0].getImm() == PPC::PRED_BIT_UNSET)
554 BuildMI(&MBB, DL, get(PPC::BCn)).addOperand(Cond[1]).addMBB(TBB);
Chris Lattner94e04442006-10-21 05:36:13 +0000555 else // Conditional branch
Stuart Hastings0125b642010-06-17 22:43:56 +0000556 BuildMI(&MBB, DL, get(PPC::BCC))
Hal Finkel940ab932014-02-28 00:27:01 +0000557 .addImm(Cond[0].getImm()).addOperand(Cond[1]).addMBB(TBB);
Evan Cheng99be49d2007-05-18 00:05:48 +0000558 return 1;
Chris Lattnera61f0102006-10-17 18:06:55 +0000559 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000560
Chris Lattnerd8816602006-10-21 05:42:09 +0000561 // Two-way Conditional Branch.
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000562 if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
563 BuildMI(&MBB, DL, get(Cond[0].getImm() ?
564 (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
565 (isPPC64 ? PPC::BDZ8 : PPC::BDZ))).addMBB(TBB);
Hal Finkel940ab932014-02-28 00:27:01 +0000566 else if (Cond[0].getImm() == PPC::PRED_BIT_SET)
567 BuildMI(&MBB, DL, get(PPC::BC)).addOperand(Cond[1]).addMBB(TBB);
568 else if (Cond[0].getImm() == PPC::PRED_BIT_UNSET)
569 BuildMI(&MBB, DL, get(PPC::BCn)).addOperand(Cond[1]).addMBB(TBB);
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000570 else
571 BuildMI(&MBB, DL, get(PPC::BCC))
Hal Finkel940ab932014-02-28 00:27:01 +0000572 .addImm(Cond[0].getImm()).addOperand(Cond[1]).addMBB(TBB);
Stuart Hastings0125b642010-06-17 22:43:56 +0000573 BuildMI(&MBB, DL, get(PPC::B)).addMBB(FBB);
Evan Cheng99be49d2007-05-18 00:05:48 +0000574 return 2;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000575}
576
Hal Finkeled6a2852013-04-05 23:29:01 +0000577// Select analysis.
578bool PPCInstrInfo::canInsertSelect(const MachineBasicBlock &MBB,
579 const SmallVectorImpl<MachineOperand> &Cond,
580 unsigned TrueReg, unsigned FalseReg,
581 int &CondCycles, int &TrueCycles, int &FalseCycles) const {
582 if (!TM.getSubtargetImpl()->hasISEL())
583 return false;
584
585 if (Cond.size() != 2)
586 return false;
587
588 // If this is really a bdnz-like condition, then it cannot be turned into a
589 // select.
590 if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
591 return false;
592
593 // Check register classes.
594 const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
595 const TargetRegisterClass *RC =
596 RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg));
597 if (!RC)
598 return false;
599
600 // isel is for regular integer GPRs only.
601 if (!PPC::GPRCRegClass.hasSubClassEq(RC) &&
Hal Finkel8e8618a2013-07-15 20:22:58 +0000602 !PPC::GPRC_NOR0RegClass.hasSubClassEq(RC) &&
603 !PPC::G8RCRegClass.hasSubClassEq(RC) &&
604 !PPC::G8RC_NOX0RegClass.hasSubClassEq(RC))
Hal Finkeled6a2852013-04-05 23:29:01 +0000605 return false;
606
607 // FIXME: These numbers are for the A2, how well they work for other cores is
608 // an open question. On the A2, the isel instruction has a 2-cycle latency
609 // but single-cycle throughput. These numbers are used in combination with
610 // the MispredictPenalty setting from the active SchedMachineModel.
611 CondCycles = 1;
612 TrueCycles = 1;
613 FalseCycles = 1;
614
615 return true;
616}
617
618void PPCInstrInfo::insertSelect(MachineBasicBlock &MBB,
619 MachineBasicBlock::iterator MI, DebugLoc dl,
620 unsigned DestReg,
621 const SmallVectorImpl<MachineOperand> &Cond,
622 unsigned TrueReg, unsigned FalseReg) const {
623 assert(Cond.size() == 2 &&
624 "PPC branch conditions have two components!");
625
626 assert(TM.getSubtargetImpl()->hasISEL() &&
627 "Cannot insert select on target without ISEL support");
628
629 // Get the register classes.
630 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
631 const TargetRegisterClass *RC =
632 RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg));
633 assert(RC && "TrueReg and FalseReg must have overlapping register classes");
Hal Finkel8e8618a2013-07-15 20:22:58 +0000634
635 bool Is64Bit = PPC::G8RCRegClass.hasSubClassEq(RC) ||
636 PPC::G8RC_NOX0RegClass.hasSubClassEq(RC);
637 assert((Is64Bit ||
638 PPC::GPRCRegClass.hasSubClassEq(RC) ||
639 PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) &&
Hal Finkeled6a2852013-04-05 23:29:01 +0000640 "isel is for regular integer GPRs only");
641
Hal Finkel8e8618a2013-07-15 20:22:58 +0000642 unsigned OpCode = Is64Bit ? PPC::ISEL8 : PPC::ISEL;
Hal Finkeled6a2852013-04-05 23:29:01 +0000643 unsigned SelectPred = Cond[0].getImm();
644
645 unsigned SubIdx;
646 bool SwapOps;
647 switch (SelectPred) {
648 default: llvm_unreachable("invalid predicate for isel");
649 case PPC::PRED_EQ: SubIdx = PPC::sub_eq; SwapOps = false; break;
650 case PPC::PRED_NE: SubIdx = PPC::sub_eq; SwapOps = true; break;
651 case PPC::PRED_LT: SubIdx = PPC::sub_lt; SwapOps = false; break;
652 case PPC::PRED_GE: SubIdx = PPC::sub_lt; SwapOps = true; break;
653 case PPC::PRED_GT: SubIdx = PPC::sub_gt; SwapOps = false; break;
654 case PPC::PRED_LE: SubIdx = PPC::sub_gt; SwapOps = true; break;
655 case PPC::PRED_UN: SubIdx = PPC::sub_un; SwapOps = false; break;
656 case PPC::PRED_NU: SubIdx = PPC::sub_un; SwapOps = true; break;
Hal Finkel940ab932014-02-28 00:27:01 +0000657 case PPC::PRED_BIT_SET: SubIdx = 0; SwapOps = false; break;
658 case PPC::PRED_BIT_UNSET: SubIdx = 0; SwapOps = true; break;
Hal Finkeled6a2852013-04-05 23:29:01 +0000659 }
660
661 unsigned FirstReg = SwapOps ? FalseReg : TrueReg,
662 SecondReg = SwapOps ? TrueReg : FalseReg;
663
664 // The first input register of isel cannot be r0. If it is a member
665 // of a register class that can be r0, then copy it first (the
666 // register allocator should eliminate the copy).
667 if (MRI.getRegClass(FirstReg)->contains(PPC::R0) ||
668 MRI.getRegClass(FirstReg)->contains(PPC::X0)) {
669 const TargetRegisterClass *FirstRC =
670 MRI.getRegClass(FirstReg)->contains(PPC::X0) ?
671 &PPC::G8RC_NOX0RegClass : &PPC::GPRC_NOR0RegClass;
672 unsigned OldFirstReg = FirstReg;
673 FirstReg = MRI.createVirtualRegister(FirstRC);
674 BuildMI(MBB, MI, dl, get(TargetOpcode::COPY), FirstReg)
675 .addReg(OldFirstReg);
676 }
677
678 BuildMI(MBB, MI, dl, get(OpCode), DestReg)
679 .addReg(FirstReg).addReg(SecondReg)
680 .addReg(Cond[1].getReg(), 0, SubIdx);
681}
682
Jakob Stoklund Olesen0d611972010-07-11 07:31:00 +0000683void PPCInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
684 MachineBasicBlock::iterator I, DebugLoc DL,
685 unsigned DestReg, unsigned SrcReg,
686 bool KillSrc) const {
Hal Finkel27774d92014-03-13 07:58:58 +0000687 // We can end up with self copies and similar things as a result of VSX copy
Hal Finkel9dcb3582014-03-27 22:46:28 +0000688 // legalization. Promote them here.
Hal Finkel27774d92014-03-13 07:58:58 +0000689 const TargetRegisterInfo *TRI = &getRegisterInfo();
690 if (PPC::F8RCRegClass.contains(DestReg) &&
691 PPC::VSLRCRegClass.contains(SrcReg)) {
692 unsigned SuperReg =
693 TRI->getMatchingSuperReg(DestReg, PPC::sub_64, &PPC::VSRCRegClass);
694
Hal Finkel9dcb3582014-03-27 22:46:28 +0000695 if (VSXSelfCopyCrash && SrcReg == SuperReg)
696 llvm_unreachable("nop VSX copy");
Hal Finkel27774d92014-03-13 07:58:58 +0000697
698 DestReg = SuperReg;
699 } else if (PPC::VRRCRegClass.contains(DestReg) &&
700 PPC::VSHRCRegClass.contains(SrcReg)) {
701 unsigned SuperReg =
702 TRI->getMatchingSuperReg(DestReg, PPC::sub_128, &PPC::VSRCRegClass);
703
Hal Finkel9dcb3582014-03-27 22:46:28 +0000704 if (VSXSelfCopyCrash && SrcReg == SuperReg)
705 llvm_unreachable("nop VSX copy");
Hal Finkel27774d92014-03-13 07:58:58 +0000706
707 DestReg = SuperReg;
708 } else if (PPC::F8RCRegClass.contains(SrcReg) &&
709 PPC::VSLRCRegClass.contains(DestReg)) {
710 unsigned SuperReg =
711 TRI->getMatchingSuperReg(SrcReg, PPC::sub_64, &PPC::VSRCRegClass);
712
Hal Finkel9dcb3582014-03-27 22:46:28 +0000713 if (VSXSelfCopyCrash && DestReg == SuperReg)
714 llvm_unreachable("nop VSX copy");
Hal Finkel27774d92014-03-13 07:58:58 +0000715
716 SrcReg = SuperReg;
717 } else if (PPC::VRRCRegClass.contains(SrcReg) &&
718 PPC::VSHRCRegClass.contains(DestReg)) {
719 unsigned SuperReg =
720 TRI->getMatchingSuperReg(SrcReg, PPC::sub_128, &PPC::VSRCRegClass);
721
Hal Finkel9dcb3582014-03-27 22:46:28 +0000722 if (VSXSelfCopyCrash && DestReg == SuperReg)
723 llvm_unreachable("nop VSX copy");
Hal Finkel27774d92014-03-13 07:58:58 +0000724
725 SrcReg = SuperReg;
726 }
727
Jakob Stoklund Olesen0d611972010-07-11 07:31:00 +0000728 unsigned Opc;
729 if (PPC::GPRCRegClass.contains(DestReg, SrcReg))
730 Opc = PPC::OR;
731 else if (PPC::G8RCRegClass.contains(DestReg, SrcReg))
732 Opc = PPC::OR8;
733 else if (PPC::F4RCRegClass.contains(DestReg, SrcReg))
734 Opc = PPC::FMR;
735 else if (PPC::CRRCRegClass.contains(DestReg, SrcReg))
736 Opc = PPC::MCRF;
737 else if (PPC::VRRCRegClass.contains(DestReg, SrcReg))
738 Opc = PPC::VOR;
Hal Finkel27774d92014-03-13 07:58:58 +0000739 else if (PPC::VSRCRegClass.contains(DestReg, SrcReg))
Hal Finkelbbad2332014-03-24 09:36:36 +0000740 // There are two different ways this can be done:
Hal Finkel27774d92014-03-13 07:58:58 +0000741 // 1. xxlor : This has lower latency (on the P7), 2 cycles, but can only
742 // issue in VSU pipeline 0.
743 // 2. xmovdp/xmovsp: This has higher latency (on the P7), 6 cycles, but
744 // can go to either pipeline.
Hal Finkelbbad2332014-03-24 09:36:36 +0000745 // We'll always use xxlor here, because in practically all cases where
746 // copies are generated, they are close enough to some use that the
747 // lower-latency form is preferable.
Hal Finkel27774d92014-03-13 07:58:58 +0000748 Opc = PPC::XXLOR;
Hal Finkel19be5062014-03-29 05:29:01 +0000749 else if (PPC::VSFRCRegClass.contains(DestReg, SrcReg))
750 Opc = PPC::XXLORf;
Jakob Stoklund Olesen0d611972010-07-11 07:31:00 +0000751 else if (PPC::CRBITRCRegClass.contains(DestReg, SrcReg))
752 Opc = PPC::CROR;
753 else
754 llvm_unreachable("Impossible reg-to-reg copy");
Owen Anderson7a73ae92007-12-31 06:32:00 +0000755
Evan Cheng6cc775f2011-06-28 19:10:37 +0000756 const MCInstrDesc &MCID = get(Opc);
757 if (MCID.getNumOperands() == 3)
758 BuildMI(MBB, I, DL, MCID, DestReg)
Jakob Stoklund Olesen0d611972010-07-11 07:31:00 +0000759 .addReg(SrcReg).addReg(SrcReg, getKillRegState(KillSrc));
760 else
Evan Cheng6cc775f2011-06-28 19:10:37 +0000761 BuildMI(MBB, I, DL, MCID, DestReg).addReg(SrcReg, getKillRegState(KillSrc));
Owen Anderson7a73ae92007-12-31 06:32:00 +0000762}
763
Hal Finkel8f6834d2011-12-05 17:55:17 +0000764// This function returns true if a CR spill is necessary and false otherwise.
Bill Wendlingc6c48fc2008-03-10 22:49:16 +0000765bool
Dan Gohman3b460302008-07-07 23:14:23 +0000766PPCInstrInfo::StoreRegToStackSlot(MachineFunction &MF,
767 unsigned SrcReg, bool isKill,
Bill Wendlingc6c48fc2008-03-10 22:49:16 +0000768 int FrameIdx,
769 const TargetRegisterClass *RC,
Hal Finkelfcc51d42013-03-17 04:43:44 +0000770 SmallVectorImpl<MachineInstr*> &NewMIs,
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000771 bool &NonRI, bool &SpillsVRS) const{
Hal Finkel37714b82013-03-27 21:21:15 +0000772 // Note: If additional store instructions are added here,
773 // update isStoreToStackSlot.
774
Chris Lattner6f306d72010-04-02 20:16:16 +0000775 DebugLoc DL;
Hal Finkel4e703bc2014-01-28 05:32:58 +0000776 if (PPC::GPRCRegClass.hasSubClassEq(RC) ||
777 PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) {
Hal Finkel794e05b2013-03-23 17:14:27 +0000778 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
779 .addReg(SrcReg,
780 getKillRegState(isKill)),
781 FrameIdx));
Hal Finkel4e703bc2014-01-28 05:32:58 +0000782 } else if (PPC::G8RCRegClass.hasSubClassEq(RC) ||
783 PPC::G8RC_NOX0RegClass.hasSubClassEq(RC)) {
Hal Finkel794e05b2013-03-23 17:14:27 +0000784 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STD))
785 .addReg(SrcReg,
786 getKillRegState(isKill)),
787 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000788 } else if (PPC::F8RCRegClass.hasSubClassEq(RC)) {
Dale Johannesen6b8c76a2009-02-12 23:08:38 +0000789 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFD))
Bill Wendlingf7b83c72009-05-13 21:33:08 +0000790 .addReg(SrcReg,
791 getKillRegState(isKill)),
792 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000793 } else if (PPC::F4RCRegClass.hasSubClassEq(RC)) {
Dale Johannesen6b8c76a2009-02-12 23:08:38 +0000794 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFS))
Bill Wendlingf7b83c72009-05-13 21:33:08 +0000795 .addReg(SrcReg,
796 getKillRegState(isKill)),
797 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000798 } else if (PPC::CRRCRegClass.hasSubClassEq(RC)) {
Hal Finkele154c8f2013-03-12 14:12:16 +0000799 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CR))
800 .addReg(SrcReg,
801 getKillRegState(isKill)),
802 FrameIdx));
803 return true;
Craig Topperabadc662012-04-20 06:31:50 +0000804 } else if (PPC::CRBITRCRegClass.hasSubClassEq(RC)) {
Hal Finkel940ab932014-02-28 00:27:01 +0000805 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CRBIT))
806 .addReg(SrcReg,
807 getKillRegState(isKill)),
808 FrameIdx));
809 return true;
Craig Topperabadc662012-04-20 06:31:50 +0000810 } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) {
Hal Finkelfcc51d42013-03-17 04:43:44 +0000811 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STVX))
812 .addReg(SrcReg,
813 getKillRegState(isKill)),
814 FrameIdx));
815 NonRI = true;
Hal Finkel27774d92014-03-13 07:58:58 +0000816 } else if (PPC::VSRCRegClass.hasSubClassEq(RC)) {
817 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STXVD2X))
818 .addReg(SrcReg,
819 getKillRegState(isKill)),
820 FrameIdx));
821 NonRI = true;
Hal Finkel19be5062014-03-29 05:29:01 +0000822 } else if (PPC::VSFRCRegClass.hasSubClassEq(RC)) {
823 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STXSDX))
824 .addReg(SrcReg,
825 getKillRegState(isKill)),
826 FrameIdx));
827 NonRI = true;
Hal Finkela1431df2013-03-21 19:03:21 +0000828 } else if (PPC::VRSAVERCRegClass.hasSubClassEq(RC)) {
Hal Finkela7b06302013-03-27 00:02:20 +0000829 assert(TM.getSubtargetImpl()->isDarwin() &&
830 "VRSAVE only needs spill/restore on Darwin");
Hal Finkela1431df2013-03-21 19:03:21 +0000831 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_VRSAVE))
832 .addReg(SrcReg,
833 getKillRegState(isKill)),
834 FrameIdx));
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000835 SpillsVRS = true;
Owen Andersoneee14602008-01-01 21:11:32 +0000836 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000837 llvm_unreachable("Unknown regclass!");
Owen Andersoneee14602008-01-01 21:11:32 +0000838 }
Bill Wendling632ea652008-03-03 22:19:16 +0000839
840 return false;
Owen Andersoneee14602008-01-01 21:11:32 +0000841}
842
843void
844PPCInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
Bill Wendling632ea652008-03-03 22:19:16 +0000845 MachineBasicBlock::iterator MI,
846 unsigned SrcReg, bool isKill, int FrameIdx,
Evan Chengefb126a2010-05-06 19:06:44 +0000847 const TargetRegisterClass *RC,
848 const TargetRegisterInfo *TRI) const {
Dan Gohman3b460302008-07-07 23:14:23 +0000849 MachineFunction &MF = *MBB.getParent();
Owen Andersoneee14602008-01-01 21:11:32 +0000850 SmallVector<MachineInstr*, 4> NewMIs;
Bill Wendling632ea652008-03-03 22:19:16 +0000851
Hal Finkelbb420f12013-03-15 05:06:04 +0000852 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
853 FuncInfo->setHasSpills();
854
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000855 bool NonRI = false, SpillsVRS = false;
856 if (StoreRegToStackSlot(MF, SrcReg, isKill, FrameIdx, RC, NewMIs,
857 NonRI, SpillsVRS))
Bill Wendling632ea652008-03-03 22:19:16 +0000858 FuncInfo->setSpillsCR();
Bill Wendling632ea652008-03-03 22:19:16 +0000859
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000860 if (SpillsVRS)
861 FuncInfo->setSpillsVRSAVE();
862
Hal Finkelfcc51d42013-03-17 04:43:44 +0000863 if (NonRI)
864 FuncInfo->setHasNonRISpills();
865
Owen Andersoneee14602008-01-01 21:11:32 +0000866 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
867 MBB.insert(MI, NewMIs[i]);
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +0000868
869 const MachineFrameInfo &MFI = *MF.getFrameInfo();
870 MachineMemOperand *MMO =
Jay Foad465101b2011-11-15 07:34:52 +0000871 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx),
Chris Lattnere3d864b2010-09-21 04:39:43 +0000872 MachineMemOperand::MOStore,
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +0000873 MFI.getObjectSize(FrameIdx),
874 MFI.getObjectAlignment(FrameIdx));
875 NewMIs.back()->addMemOperand(MF, MMO);
Owen Andersoneee14602008-01-01 21:11:32 +0000876}
877
Hal Finkelbde7f8f2011-12-06 20:55:36 +0000878bool
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000879PPCInstrInfo::LoadRegFromStackSlot(MachineFunction &MF, DebugLoc DL,
Dan Gohman3b460302008-07-07 23:14:23 +0000880 unsigned DestReg, int FrameIdx,
Bill Wendlingc6c48fc2008-03-10 22:49:16 +0000881 const TargetRegisterClass *RC,
Hal Finkelfcc51d42013-03-17 04:43:44 +0000882 SmallVectorImpl<MachineInstr*> &NewMIs,
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000883 bool &NonRI, bool &SpillsVRS) const{
Hal Finkel37714b82013-03-27 21:21:15 +0000884 // Note: If additional load instructions are added here,
885 // update isLoadFromStackSlot.
886
Hal Finkel4e703bc2014-01-28 05:32:58 +0000887 if (PPC::GPRCRegClass.hasSubClassEq(RC) ||
888 PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) {
Hal Finkel5791f512013-03-27 19:10:40 +0000889 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ),
890 DestReg), FrameIdx));
Hal Finkel4e703bc2014-01-28 05:32:58 +0000891 } else if (PPC::G8RCRegClass.hasSubClassEq(RC) ||
892 PPC::G8RC_NOX0RegClass.hasSubClassEq(RC)) {
Hal Finkel5791f512013-03-27 19:10:40 +0000893 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LD), DestReg),
894 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000895 } else if (PPC::F8RCRegClass.hasSubClassEq(RC)) {
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000896 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFD), DestReg),
Owen Andersoneee14602008-01-01 21:11:32 +0000897 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000898 } else if (PPC::F4RCRegClass.hasSubClassEq(RC)) {
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000899 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFS), DestReg),
Owen Andersoneee14602008-01-01 21:11:32 +0000900 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000901 } else if (PPC::CRRCRegClass.hasSubClassEq(RC)) {
Hal Finkele154c8f2013-03-12 14:12:16 +0000902 NewMIs.push_back(addFrameReference(BuildMI(MF, DL,
903 get(PPC::RESTORE_CR), DestReg),
904 FrameIdx));
905 return true;
Craig Topperabadc662012-04-20 06:31:50 +0000906 } else if (PPC::CRBITRCRegClass.hasSubClassEq(RC)) {
Hal Finkel940ab932014-02-28 00:27:01 +0000907 NewMIs.push_back(addFrameReference(BuildMI(MF, DL,
908 get(PPC::RESTORE_CRBIT), DestReg),
909 FrameIdx));
910 return true;
Craig Topperabadc662012-04-20 06:31:50 +0000911 } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) {
Hal Finkelfcc51d42013-03-17 04:43:44 +0000912 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LVX), DestReg),
913 FrameIdx));
914 NonRI = true;
Hal Finkel27774d92014-03-13 07:58:58 +0000915 } else if (PPC::VSRCRegClass.hasSubClassEq(RC)) {
916 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LXVD2X), DestReg),
917 FrameIdx));
918 NonRI = true;
Hal Finkel19be5062014-03-29 05:29:01 +0000919 } else if (PPC::VSFRCRegClass.hasSubClassEq(RC)) {
920 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LXSDX), DestReg),
921 FrameIdx));
922 NonRI = true;
Hal Finkela1431df2013-03-21 19:03:21 +0000923 } else if (PPC::VRSAVERCRegClass.hasSubClassEq(RC)) {
Hal Finkela7b06302013-03-27 00:02:20 +0000924 assert(TM.getSubtargetImpl()->isDarwin() &&
925 "VRSAVE only needs spill/restore on Darwin");
Hal Finkela1431df2013-03-21 19:03:21 +0000926 NewMIs.push_back(addFrameReference(BuildMI(MF, DL,
927 get(PPC::RESTORE_VRSAVE),
928 DestReg),
929 FrameIdx));
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000930 SpillsVRS = true;
Owen Andersoneee14602008-01-01 21:11:32 +0000931 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000932 llvm_unreachable("Unknown regclass!");
Owen Andersoneee14602008-01-01 21:11:32 +0000933 }
Hal Finkelbde7f8f2011-12-06 20:55:36 +0000934
935 return false;
Owen Andersoneee14602008-01-01 21:11:32 +0000936}
937
938void
939PPCInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
Bill Wendling632ea652008-03-03 22:19:16 +0000940 MachineBasicBlock::iterator MI,
941 unsigned DestReg, int FrameIdx,
Evan Chengefb126a2010-05-06 19:06:44 +0000942 const TargetRegisterClass *RC,
943 const TargetRegisterInfo *TRI) const {
Dan Gohman3b460302008-07-07 23:14:23 +0000944 MachineFunction &MF = *MBB.getParent();
Owen Andersoneee14602008-01-01 21:11:32 +0000945 SmallVector<MachineInstr*, 4> NewMIs;
Chris Lattner6f306d72010-04-02 20:16:16 +0000946 DebugLoc DL;
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000947 if (MI != MBB.end()) DL = MI->getDebugLoc();
Hal Finkelfcc51d42013-03-17 04:43:44 +0000948
949 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
950 FuncInfo->setHasSpills();
951
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000952 bool NonRI = false, SpillsVRS = false;
953 if (LoadRegFromStackSlot(MF, DL, DestReg, FrameIdx, RC, NewMIs,
954 NonRI, SpillsVRS))
Hal Finkelbde7f8f2011-12-06 20:55:36 +0000955 FuncInfo->setSpillsCR();
Hal Finkelfcc51d42013-03-17 04:43:44 +0000956
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000957 if (SpillsVRS)
958 FuncInfo->setSpillsVRSAVE();
959
Hal Finkelfcc51d42013-03-17 04:43:44 +0000960 if (NonRI)
961 FuncInfo->setHasNonRISpills();
962
Owen Andersoneee14602008-01-01 21:11:32 +0000963 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
964 MBB.insert(MI, NewMIs[i]);
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +0000965
966 const MachineFrameInfo &MFI = *MF.getFrameInfo();
967 MachineMemOperand *MMO =
Jay Foad465101b2011-11-15 07:34:52 +0000968 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx),
Chris Lattnere3d864b2010-09-21 04:39:43 +0000969 MachineMemOperand::MOLoad,
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +0000970 MFI.getObjectSize(FrameIdx),
971 MFI.getObjectAlignment(FrameIdx));
972 NewMIs.back()->addMemOperand(MF, MMO);
Owen Andersoneee14602008-01-01 21:11:32 +0000973}
974
Chris Lattnera47294ed2006-10-13 21:21:17 +0000975bool PPCInstrInfo::
Owen Anderson4f6bf042008-08-14 22:49:33 +0000976ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
Chris Lattner23f22de2006-10-21 06:03:11 +0000977 assert(Cond.size() == 2 && "Invalid PPC branch opcode!");
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000978 if (Cond[1].getReg() == PPC::CTR8 || Cond[1].getReg() == PPC::CTR)
979 Cond[0].setImm(Cond[0].getImm() == 0 ? 1 : 0);
980 else
981 // Leave the CR# the same, but invert the condition.
982 Cond[0].setImm(PPC::InvertPredicate((PPC::Predicate)Cond[0].getImm()));
Chris Lattner23f22de2006-10-21 06:03:11 +0000983 return false;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000984}
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +0000985
Hal Finkeld61d4f82013-04-06 19:30:30 +0000986bool PPCInstrInfo::FoldImmediate(MachineInstr *UseMI, MachineInstr *DefMI,
987 unsigned Reg, MachineRegisterInfo *MRI) const {
988 // For some instructions, it is legal to fold ZERO into the RA register field.
989 // A zero immediate should always be loaded with a single li.
990 unsigned DefOpc = DefMI->getOpcode();
991 if (DefOpc != PPC::LI && DefOpc != PPC::LI8)
992 return false;
993 if (!DefMI->getOperand(1).isImm())
994 return false;
995 if (DefMI->getOperand(1).getImm() != 0)
996 return false;
997
998 // Note that we cannot here invert the arguments of an isel in order to fold
999 // a ZERO into what is presented as the second argument. All we have here
1000 // is the condition bit, and that might come from a CR-logical bit operation.
1001
1002 const MCInstrDesc &UseMCID = UseMI->getDesc();
1003
1004 // Only fold into real machine instructions.
1005 if (UseMCID.isPseudo())
1006 return false;
1007
1008 unsigned UseIdx;
1009 for (UseIdx = 0; UseIdx < UseMI->getNumOperands(); ++UseIdx)
1010 if (UseMI->getOperand(UseIdx).isReg() &&
1011 UseMI->getOperand(UseIdx).getReg() == Reg)
1012 break;
1013
1014 assert(UseIdx < UseMI->getNumOperands() && "Cannot find Reg in UseMI");
1015 assert(UseIdx < UseMCID.getNumOperands() && "No operand description for Reg");
1016
1017 const MCOperandInfo *UseInfo = &UseMCID.OpInfo[UseIdx];
1018
1019 // We can fold the zero if this register requires a GPRC_NOR0/G8RC_NOX0
1020 // register (which might also be specified as a pointer class kind).
1021 if (UseInfo->isLookupPtrRegClass()) {
1022 if (UseInfo->RegClass /* Kind */ != 1)
1023 return false;
1024 } else {
1025 if (UseInfo->RegClass != PPC::GPRC_NOR0RegClassID &&
1026 UseInfo->RegClass != PPC::G8RC_NOX0RegClassID)
1027 return false;
1028 }
1029
1030 // Make sure this is not tied to an output register (or otherwise
1031 // constrained). This is true for ST?UX registers, for example, which
1032 // are tied to their output registers.
1033 if (UseInfo->Constraints != 0)
1034 return false;
1035
1036 unsigned ZeroReg;
1037 if (UseInfo->isLookupPtrRegClass()) {
1038 bool isPPC64 = TM.getSubtargetImpl()->isPPC64();
1039 ZeroReg = isPPC64 ? PPC::ZERO8 : PPC::ZERO;
1040 } else {
1041 ZeroReg = UseInfo->RegClass == PPC::G8RC_NOX0RegClassID ?
1042 PPC::ZERO8 : PPC::ZERO;
1043 }
1044
1045 bool DeleteDef = MRI->hasOneNonDBGUse(Reg);
1046 UseMI->getOperand(UseIdx).setReg(ZeroReg);
1047
1048 if (DeleteDef)
1049 DefMI->eraseFromParent();
1050
1051 return true;
1052}
1053
Hal Finkel30ae2292013-04-10 18:30:16 +00001054static bool MBBDefinesCTR(MachineBasicBlock &MBB) {
1055 for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end();
1056 I != IE; ++I)
1057 if (I->definesRegister(PPC::CTR) || I->definesRegister(PPC::CTR8))
1058 return true;
1059 return false;
1060}
1061
1062// We should make sure that, if we're going to predicate both sides of a
1063// condition (a diamond), that both sides don't define the counter register. We
1064// can predicate counter-decrement-based branches, but while that predicates
1065// the branching, it does not predicate the counter decrement. If we tried to
1066// merge the triangle into one predicated block, we'd decrement the counter
1067// twice.
1068bool PPCInstrInfo::isProfitableToIfCvt(MachineBasicBlock &TMBB,
1069 unsigned NumT, unsigned ExtraT,
1070 MachineBasicBlock &FMBB,
1071 unsigned NumF, unsigned ExtraF,
1072 const BranchProbability &Probability) const {
1073 return !(MBBDefinesCTR(TMBB) && MBBDefinesCTR(FMBB));
1074}
1075
1076
Hal Finkel5711eca2013-04-09 22:58:37 +00001077bool PPCInstrInfo::isPredicated(const MachineInstr *MI) const {
Hal Finkelf29285a2013-04-11 01:23:34 +00001078 // The predicated branches are identified by their type, not really by the
1079 // explicit presence of a predicate. Furthermore, some of them can be
1080 // predicated more than once. Because if conversion won't try to predicate
1081 // any instruction which already claims to be predicated (by returning true
1082 // here), always return false. In doing so, we let isPredicable() be the
1083 // final word on whether not the instruction can be (further) predicated.
1084
1085 return false;
Hal Finkel5711eca2013-04-09 22:58:37 +00001086}
1087
1088bool PPCInstrInfo::isUnpredicatedTerminator(const MachineInstr *MI) const {
1089 if (!MI->isTerminator())
1090 return false;
1091
1092 // Conditional branch is a special case.
1093 if (MI->isBranch() && !MI->isBarrier())
1094 return true;
1095
1096 return !isPredicated(MI);
1097}
1098
1099bool PPCInstrInfo::PredicateInstruction(
1100 MachineInstr *MI,
1101 const SmallVectorImpl<MachineOperand> &Pred) const {
1102 unsigned OpC = MI->getOpcode();
1103 if (OpC == PPC::BLR) {
1104 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) {
1105 bool isPPC64 = TM.getSubtargetImpl()->isPPC64();
1106 MI->setDesc(get(Pred[0].getImm() ?
1107 (isPPC64 ? PPC::BDNZLR8 : PPC::BDNZLR) :
1108 (isPPC64 ? PPC::BDZLR8 : PPC::BDZLR)));
Hal Finkel940ab932014-02-28 00:27:01 +00001109 } else if (Pred[0].getImm() == PPC::PRED_BIT_SET) {
Hal Finkel5711eca2013-04-09 22:58:37 +00001110 MI->setDesc(get(PPC::BCLR));
1111 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
Hal Finkel940ab932014-02-28 00:27:01 +00001112 .addReg(Pred[1].getReg());
1113 } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) {
1114 MI->setDesc(get(PPC::BCLRn));
1115 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1116 .addReg(Pred[1].getReg());
1117 } else {
1118 MI->setDesc(get(PPC::BCCLR));
1119 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
Hal Finkel5711eca2013-04-09 22:58:37 +00001120 .addImm(Pred[0].getImm())
1121 .addReg(Pred[1].getReg());
1122 }
1123
1124 return true;
1125 } else if (OpC == PPC::B) {
1126 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) {
1127 bool isPPC64 = TM.getSubtargetImpl()->isPPC64();
1128 MI->setDesc(get(Pred[0].getImm() ?
1129 (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
1130 (isPPC64 ? PPC::BDZ8 : PPC::BDZ)));
Hal Finkel940ab932014-02-28 00:27:01 +00001131 } else if (Pred[0].getImm() == PPC::PRED_BIT_SET) {
1132 MachineBasicBlock *MBB = MI->getOperand(0).getMBB();
1133 MI->RemoveOperand(0);
1134
1135 MI->setDesc(get(PPC::BC));
1136 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1137 .addReg(Pred[1].getReg())
1138 .addMBB(MBB);
1139 } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) {
1140 MachineBasicBlock *MBB = MI->getOperand(0).getMBB();
1141 MI->RemoveOperand(0);
1142
1143 MI->setDesc(get(PPC::BCn));
1144 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1145 .addReg(Pred[1].getReg())
1146 .addMBB(MBB);
Hal Finkel5711eca2013-04-09 22:58:37 +00001147 } else {
1148 MachineBasicBlock *MBB = MI->getOperand(0).getMBB();
1149 MI->RemoveOperand(0);
1150
1151 MI->setDesc(get(PPC::BCC));
1152 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1153 .addImm(Pred[0].getImm())
1154 .addReg(Pred[1].getReg())
1155 .addMBB(MBB);
1156 }
1157
1158 return true;
Hal Finkel500b0042013-04-10 06:42:34 +00001159 } else if (OpC == PPC::BCTR || OpC == PPC::BCTR8 ||
1160 OpC == PPC::BCTRL || OpC == PPC::BCTRL8) {
1161 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR)
1162 llvm_unreachable("Cannot predicate bctr[l] on the ctr register");
1163
1164 bool setLR = OpC == PPC::BCTRL || OpC == PPC::BCTRL8;
1165 bool isPPC64 = TM.getSubtargetImpl()->isPPC64();
Hal Finkel940ab932014-02-28 00:27:01 +00001166
1167 if (Pred[0].getImm() == PPC::PRED_BIT_SET) {
1168 MI->setDesc(get(isPPC64 ? (setLR ? PPC::BCCTRL8 : PPC::BCCTR8) :
1169 (setLR ? PPC::BCCTRL : PPC::BCCTR)));
1170 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1171 .addReg(Pred[1].getReg());
1172 return true;
1173 } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) {
1174 MI->setDesc(get(isPPC64 ? (setLR ? PPC::BCCTRL8n : PPC::BCCTR8n) :
1175 (setLR ? PPC::BCCTRLn : PPC::BCCTRn)));
1176 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1177 .addReg(Pred[1].getReg());
1178 return true;
1179 }
1180
1181 MI->setDesc(get(isPPC64 ? (setLR ? PPC::BCCCTRL8 : PPC::BCCCTR8) :
1182 (setLR ? PPC::BCCCTRL : PPC::BCCCTR)));
Hal Finkel500b0042013-04-10 06:42:34 +00001183 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1184 .addImm(Pred[0].getImm())
1185 .addReg(Pred[1].getReg());
1186 return true;
Hal Finkel5711eca2013-04-09 22:58:37 +00001187 }
1188
1189 return false;
1190}
1191
1192bool PPCInstrInfo::SubsumesPredicate(
1193 const SmallVectorImpl<MachineOperand> &Pred1,
1194 const SmallVectorImpl<MachineOperand> &Pred2) const {
1195 assert(Pred1.size() == 2 && "Invalid PPC first predicate");
1196 assert(Pred2.size() == 2 && "Invalid PPC second predicate");
1197
1198 if (Pred1[1].getReg() == PPC::CTR8 || Pred1[1].getReg() == PPC::CTR)
1199 return false;
1200 if (Pred2[1].getReg() == PPC::CTR8 || Pred2[1].getReg() == PPC::CTR)
1201 return false;
1202
Hal Finkel94a6f382013-12-11 23:12:25 +00001203 // P1 can only subsume P2 if they test the same condition register.
1204 if (Pred1[1].getReg() != Pred2[1].getReg())
1205 return false;
1206
Hal Finkel5711eca2013-04-09 22:58:37 +00001207 PPC::Predicate P1 = (PPC::Predicate) Pred1[0].getImm();
1208 PPC::Predicate P2 = (PPC::Predicate) Pred2[0].getImm();
1209
1210 if (P1 == P2)
1211 return true;
1212
1213 // Does P1 subsume P2, e.g. GE subsumes GT.
1214 if (P1 == PPC::PRED_LE &&
1215 (P2 == PPC::PRED_LT || P2 == PPC::PRED_EQ))
1216 return true;
1217 if (P1 == PPC::PRED_GE &&
1218 (P2 == PPC::PRED_GT || P2 == PPC::PRED_EQ))
1219 return true;
1220
1221 return false;
1222}
1223
1224bool PPCInstrInfo::DefinesPredicate(MachineInstr *MI,
1225 std::vector<MachineOperand> &Pred) const {
1226 // Note: At the present time, the contents of Pred from this function is
1227 // unused by IfConversion. This implementation follows ARM by pushing the
1228 // CR-defining operand. Because the 'DZ' and 'DNZ' count as types of
1229 // predicate, instructions defining CTR or CTR8 are also included as
1230 // predicate-defining instructions.
1231
1232 const TargetRegisterClass *RCs[] =
1233 { &PPC::CRRCRegClass, &PPC::CRBITRCRegClass,
1234 &PPC::CTRRCRegClass, &PPC::CTRRC8RegClass };
1235
1236 bool Found = false;
1237 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1238 const MachineOperand &MO = MI->getOperand(i);
Hal Finkelaf822012013-04-10 07:17:47 +00001239 for (unsigned c = 0; c < array_lengthof(RCs) && !Found; ++c) {
Hal Finkel5711eca2013-04-09 22:58:37 +00001240 const TargetRegisterClass *RC = RCs[c];
Hal Finkelaf822012013-04-10 07:17:47 +00001241 if (MO.isReg()) {
1242 if (MO.isDef() && RC->contains(MO.getReg())) {
Hal Finkel5711eca2013-04-09 22:58:37 +00001243 Pred.push_back(MO);
1244 Found = true;
1245 }
Hal Finkelaf822012013-04-10 07:17:47 +00001246 } else if (MO.isRegMask()) {
1247 for (TargetRegisterClass::iterator I = RC->begin(),
1248 IE = RC->end(); I != IE; ++I)
1249 if (MO.clobbersPhysReg(*I)) {
1250 Pred.push_back(MO);
1251 Found = true;
1252 }
Hal Finkel5711eca2013-04-09 22:58:37 +00001253 }
1254 }
1255 }
1256
1257 return Found;
1258}
1259
1260bool PPCInstrInfo::isPredicable(MachineInstr *MI) const {
1261 unsigned OpC = MI->getOpcode();
1262 switch (OpC) {
1263 default:
1264 return false;
1265 case PPC::B:
1266 case PPC::BLR:
Hal Finkel500b0042013-04-10 06:42:34 +00001267 case PPC::BCTR:
1268 case PPC::BCTR8:
1269 case PPC::BCTRL:
1270 case PPC::BCTRL8:
Hal Finkel5711eca2013-04-09 22:58:37 +00001271 return true;
1272 }
1273}
1274
Hal Finkel82656cb2013-04-18 22:15:08 +00001275bool PPCInstrInfo::analyzeCompare(const MachineInstr *MI,
1276 unsigned &SrcReg, unsigned &SrcReg2,
1277 int &Mask, int &Value) const {
1278 unsigned Opc = MI->getOpcode();
1279
1280 switch (Opc) {
1281 default: return false;
1282 case PPC::CMPWI:
1283 case PPC::CMPLWI:
1284 case PPC::CMPDI:
1285 case PPC::CMPLDI:
1286 SrcReg = MI->getOperand(1).getReg();
1287 SrcReg2 = 0;
1288 Value = MI->getOperand(2).getImm();
1289 Mask = 0xFFFF;
1290 return true;
1291 case PPC::CMPW:
1292 case PPC::CMPLW:
1293 case PPC::CMPD:
1294 case PPC::CMPLD:
1295 case PPC::FCMPUS:
1296 case PPC::FCMPUD:
1297 SrcReg = MI->getOperand(1).getReg();
1298 SrcReg2 = MI->getOperand(2).getReg();
1299 return true;
1300 }
1301}
Hal Finkele6322392013-04-19 22:08:38 +00001302
Hal Finkel82656cb2013-04-18 22:15:08 +00001303bool PPCInstrInfo::optimizeCompareInstr(MachineInstr *CmpInstr,
1304 unsigned SrcReg, unsigned SrcReg2,
1305 int Mask, int Value,
1306 const MachineRegisterInfo *MRI) const {
Hal Finkelb12da6b2013-04-18 22:54:25 +00001307 if (DisableCmpOpt)
1308 return false;
1309
Hal Finkel82656cb2013-04-18 22:15:08 +00001310 int OpC = CmpInstr->getOpcode();
1311 unsigned CRReg = CmpInstr->getOperand(0).getReg();
Hal Finkel08e53ee2013-05-08 12:16:14 +00001312
1313 // FP record forms set CR1 based on the execption status bits, not a
1314 // comparison with zero.
1315 if (OpC == PPC::FCMPUS || OpC == PPC::FCMPUD)
1316 return false;
Hal Finkel82656cb2013-04-18 22:15:08 +00001317
1318 // The record forms set the condition register based on a signed comparison
1319 // with zero (so says the ISA manual). This is not as straightforward as it
1320 // seems, however, because this is always a 64-bit comparison on PPC64, even
1321 // for instructions that are 32-bit in nature (like slw for example).
1322 // So, on PPC32, for unsigned comparisons, we can use the record forms only
1323 // for equality checks (as those don't depend on the sign). On PPC64,
1324 // we are restricted to equality for unsigned 64-bit comparisons and for
1325 // signed 32-bit comparisons the applicability is more restricted.
1326 bool isPPC64 = TM.getSubtargetImpl()->isPPC64();
1327 bool is32BitSignedCompare = OpC == PPC::CMPWI || OpC == PPC::CMPW;
1328 bool is32BitUnsignedCompare = OpC == PPC::CMPLWI || OpC == PPC::CMPLW;
1329 bool is64BitUnsignedCompare = OpC == PPC::CMPLDI || OpC == PPC::CMPLD;
1330
1331 // Get the unique definition of SrcReg.
1332 MachineInstr *MI = MRI->getUniqueVRegDef(SrcReg);
1333 if (!MI) return false;
1334 int MIOpC = MI->getOpcode();
1335
1336 bool equalityOnly = false;
1337 bool noSub = false;
1338 if (isPPC64) {
1339 if (is32BitSignedCompare) {
1340 // We can perform this optimization only if MI is sign-extending.
1341 if (MIOpC == PPC::SRAW || MIOpC == PPC::SRAWo ||
1342 MIOpC == PPC::SRAWI || MIOpC == PPC::SRAWIo ||
1343 MIOpC == PPC::EXTSB || MIOpC == PPC::EXTSBo ||
1344 MIOpC == PPC::EXTSH || MIOpC == PPC::EXTSHo ||
1345 MIOpC == PPC::EXTSW || MIOpC == PPC::EXTSWo) {
1346 noSub = true;
1347 } else
1348 return false;
1349 } else if (is32BitUnsignedCompare) {
1350 // We can perform this optimization, equality only, if MI is
1351 // zero-extending.
1352 if (MIOpC == PPC::CNTLZW || MIOpC == PPC::CNTLZWo ||
1353 MIOpC == PPC::SLW || MIOpC == PPC::SLWo ||
1354 MIOpC == PPC::SRW || MIOpC == PPC::SRWo) {
1355 noSub = true;
1356 equalityOnly = true;
1357 } else
1358 return false;
Hal Finkel08e53ee2013-05-08 12:16:14 +00001359 } else
Hal Finkel82656cb2013-04-18 22:15:08 +00001360 equalityOnly = is64BitUnsignedCompare;
Hal Finkel08e53ee2013-05-08 12:16:14 +00001361 } else
Hal Finkel82656cb2013-04-18 22:15:08 +00001362 equalityOnly = is32BitUnsignedCompare;
1363
1364 if (equalityOnly) {
1365 // We need to check the uses of the condition register in order to reject
1366 // non-equality comparisons.
Owen Anderson16c6bf42014-03-13 23:12:04 +00001367 for (MachineRegisterInfo::use_instr_iterator I =MRI->use_instr_begin(CRReg),
1368 IE = MRI->use_instr_end(); I != IE; ++I) {
Hal Finkel82656cb2013-04-18 22:15:08 +00001369 MachineInstr *UseMI = &*I;
1370 if (UseMI->getOpcode() == PPC::BCC) {
1371 unsigned Pred = UseMI->getOperand(0).getImm();
Hal Finkelc3632452013-05-07 17:49:55 +00001372 if (Pred != PPC::PRED_EQ && Pred != PPC::PRED_NE)
1373 return false;
Hal Finkel82656cb2013-04-18 22:15:08 +00001374 } else if (UseMI->getOpcode() == PPC::ISEL ||
1375 UseMI->getOpcode() == PPC::ISEL8) {
1376 unsigned SubIdx = UseMI->getOperand(3).getSubReg();
Hal Finkelc3632452013-05-07 17:49:55 +00001377 if (SubIdx != PPC::sub_eq)
1378 return false;
Hal Finkel82656cb2013-04-18 22:15:08 +00001379 } else
1380 return false;
1381 }
1382 }
1383
Hal Finkelc3632452013-05-07 17:49:55 +00001384 MachineBasicBlock::iterator I = CmpInstr;
Hal Finkel82656cb2013-04-18 22:15:08 +00001385
1386 // Scan forward to find the first use of the compare.
1387 for (MachineBasicBlock::iterator EL = CmpInstr->getParent()->end();
1388 I != EL; ++I) {
1389 bool FoundUse = false;
Owen Anderson16c6bf42014-03-13 23:12:04 +00001390 for (MachineRegisterInfo::use_instr_iterator J =MRI->use_instr_begin(CRReg),
1391 JE = MRI->use_instr_end(); J != JE; ++J)
Hal Finkel82656cb2013-04-18 22:15:08 +00001392 if (&*J == &*I) {
1393 FoundUse = true;
1394 break;
1395 }
1396
1397 if (FoundUse)
1398 break;
1399 }
1400
Hal Finkel82656cb2013-04-18 22:15:08 +00001401 // There are two possible candidates which can be changed to set CR[01].
1402 // One is MI, the other is a SUB instruction.
1403 // For CMPrr(r1,r2), we are looking for SUB(r1,r2) or SUB(r2,r1).
1404 MachineInstr *Sub = NULL;
1405 if (SrcReg2 != 0)
1406 // MI is not a candidate for CMPrr.
1407 MI = NULL;
1408 // FIXME: Conservatively refuse to convert an instruction which isn't in the
1409 // same BB as the comparison. This is to allow the check below to avoid calls
1410 // (and other explicit clobbers); instead we should really check for these
1411 // more explicitly (in at least a few predecessors).
1412 else if (MI->getParent() != CmpInstr->getParent() || Value != 0) {
1413 // PPC does not have a record-form SUBri.
1414 return false;
1415 }
1416
1417 // Search for Sub.
1418 const TargetRegisterInfo *TRI = &getRegisterInfo();
1419 --I;
Hal Finkelc3632452013-05-07 17:49:55 +00001420
1421 // Get ready to iterate backward from CmpInstr.
1422 MachineBasicBlock::iterator E = MI,
1423 B = CmpInstr->getParent()->begin();
1424
Hal Finkel82656cb2013-04-18 22:15:08 +00001425 for (; I != E && !noSub; --I) {
1426 const MachineInstr &Instr = *I;
1427 unsigned IOpC = Instr.getOpcode();
1428
1429 if (&*I != CmpInstr && (
Hal Finkel08e53ee2013-05-08 12:16:14 +00001430 Instr.modifiesRegister(PPC::CR0, TRI) ||
1431 Instr.readsRegister(PPC::CR0, TRI)))
Hal Finkel82656cb2013-04-18 22:15:08 +00001432 // This instruction modifies or uses the record condition register after
1433 // the one we want to change. While we could do this transformation, it
1434 // would likely not be profitable. This transformation removes one
1435 // instruction, and so even forcing RA to generate one move probably
1436 // makes it unprofitable.
1437 return false;
1438
1439 // Check whether CmpInstr can be made redundant by the current instruction.
1440 if ((OpC == PPC::CMPW || OpC == PPC::CMPLW ||
1441 OpC == PPC::CMPD || OpC == PPC::CMPLD) &&
1442 (IOpC == PPC::SUBF || IOpC == PPC::SUBF8) &&
1443 ((Instr.getOperand(1).getReg() == SrcReg &&
1444 Instr.getOperand(2).getReg() == SrcReg2) ||
1445 (Instr.getOperand(1).getReg() == SrcReg2 &&
1446 Instr.getOperand(2).getReg() == SrcReg))) {
1447 Sub = &*I;
1448 break;
1449 }
1450
Hal Finkel82656cb2013-04-18 22:15:08 +00001451 if (I == B)
1452 // The 'and' is below the comparison instruction.
1453 return false;
1454 }
1455
1456 // Return false if no candidates exist.
1457 if (!MI && !Sub)
1458 return false;
1459
1460 // The single candidate is called MI.
1461 if (!MI) MI = Sub;
1462
1463 int NewOpC = -1;
1464 MIOpC = MI->getOpcode();
1465 if (MIOpC == PPC::ANDIo || MIOpC == PPC::ANDIo8)
1466 NewOpC = MIOpC;
1467 else {
1468 NewOpC = PPC::getRecordFormOpcode(MIOpC);
1469 if (NewOpC == -1 && PPC::getNonRecordFormOpcode(MIOpC) != -1)
1470 NewOpC = MIOpC;
1471 }
1472
1473 // FIXME: On the non-embedded POWER architectures, only some of the record
1474 // forms are fast, and we should use only the fast ones.
1475
1476 // The defining instruction has a record form (or is already a record
1477 // form). It is possible, however, that we'll need to reverse the condition
1478 // code of the users.
1479 if (NewOpC == -1)
1480 return false;
1481
Hal Finkele6322392013-04-19 22:08:38 +00001482 SmallVector<std::pair<MachineOperand*, PPC::Predicate>, 4> PredsToUpdate;
1483 SmallVector<std::pair<MachineOperand*, unsigned>, 4> SubRegsToUpdate;
Hal Finkel82656cb2013-04-18 22:15:08 +00001484
1485 // If we have SUB(r1, r2) and CMP(r2, r1), the condition code based on CMP
1486 // needs to be updated to be based on SUB. Push the condition code
1487 // operands to OperandsToUpdate. If it is safe to remove CmpInstr, the
1488 // condition code of these operands will be modified.
1489 bool ShouldSwap = false;
1490 if (Sub) {
1491 ShouldSwap = SrcReg2 != 0 && Sub->getOperand(1).getReg() == SrcReg2 &&
1492 Sub->getOperand(2).getReg() == SrcReg;
1493
1494 // The operands to subf are the opposite of sub, so only in the fixed-point
1495 // case, invert the order.
Hal Finkel08e53ee2013-05-08 12:16:14 +00001496 ShouldSwap = !ShouldSwap;
Hal Finkel82656cb2013-04-18 22:15:08 +00001497 }
1498
1499 if (ShouldSwap)
Owen Anderson16c6bf42014-03-13 23:12:04 +00001500 for (MachineRegisterInfo::use_instr_iterator
1501 I = MRI->use_instr_begin(CRReg), IE = MRI->use_instr_end();
1502 I != IE; ++I) {
Hal Finkel82656cb2013-04-18 22:15:08 +00001503 MachineInstr *UseMI = &*I;
1504 if (UseMI->getOpcode() == PPC::BCC) {
1505 PPC::Predicate Pred = (PPC::Predicate) UseMI->getOperand(0).getImm();
Hal Finkele6322392013-04-19 22:08:38 +00001506 assert((!equalityOnly ||
1507 Pred == PPC::PRED_EQ || Pred == PPC::PRED_NE) &&
1508 "Invalid predicate for equality-only optimization");
Owen Anderson16c6bf42014-03-13 23:12:04 +00001509 PredsToUpdate.push_back(std::make_pair(&(UseMI->getOperand(0)),
Hal Finkel0f64e212013-04-20 05:16:26 +00001510 PPC::getSwappedPredicate(Pred)));
Hal Finkel82656cb2013-04-18 22:15:08 +00001511 } else if (UseMI->getOpcode() == PPC::ISEL ||
1512 UseMI->getOpcode() == PPC::ISEL8) {
Hal Finkele6322392013-04-19 22:08:38 +00001513 unsigned NewSubReg = UseMI->getOperand(3).getSubReg();
1514 assert((!equalityOnly || NewSubReg == PPC::sub_eq) &&
1515 "Invalid CR bit for equality-only optimization");
1516
1517 if (NewSubReg == PPC::sub_lt)
1518 NewSubReg = PPC::sub_gt;
1519 else if (NewSubReg == PPC::sub_gt)
1520 NewSubReg = PPC::sub_lt;
1521
Owen Anderson16c6bf42014-03-13 23:12:04 +00001522 SubRegsToUpdate.push_back(std::make_pair(&(UseMI->getOperand(3)),
Hal Finkele6322392013-04-19 22:08:38 +00001523 NewSubReg));
Hal Finkel82656cb2013-04-18 22:15:08 +00001524 } else // We need to abort on a user we don't understand.
1525 return false;
1526 }
1527
1528 // Create a new virtual register to hold the value of the CR set by the
1529 // record-form instruction. If the instruction was not previously in
1530 // record form, then set the kill flag on the CR.
1531 CmpInstr->eraseFromParent();
1532
1533 MachineBasicBlock::iterator MII = MI;
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001534 BuildMI(*MI->getParent(), std::next(MII), MI->getDebugLoc(),
Hal Finkel82656cb2013-04-18 22:15:08 +00001535 get(TargetOpcode::COPY), CRReg)
Hal Finkel08e53ee2013-05-08 12:16:14 +00001536 .addReg(PPC::CR0, MIOpC != NewOpC ? RegState::Kill : 0);
Hal Finkel82656cb2013-04-18 22:15:08 +00001537
1538 if (MIOpC != NewOpC) {
1539 // We need to be careful here: we're replacing one instruction with
1540 // another, and we need to make sure that we get all of the right
1541 // implicit uses and defs. On the other hand, the caller may be holding
1542 // an iterator to this instruction, and so we can't delete it (this is
1543 // specifically the case if this is the instruction directly after the
1544 // compare).
1545
1546 const MCInstrDesc &NewDesc = get(NewOpC);
1547 MI->setDesc(NewDesc);
1548
1549 if (NewDesc.ImplicitDefs)
1550 for (const uint16_t *ImpDefs = NewDesc.getImplicitDefs();
1551 *ImpDefs; ++ImpDefs)
1552 if (!MI->definesRegister(*ImpDefs))
1553 MI->addOperand(*MI->getParent()->getParent(),
1554 MachineOperand::CreateReg(*ImpDefs, true, true));
1555 if (NewDesc.ImplicitUses)
1556 for (const uint16_t *ImpUses = NewDesc.getImplicitUses();
1557 *ImpUses; ++ImpUses)
1558 if (!MI->readsRegister(*ImpUses))
1559 MI->addOperand(*MI->getParent()->getParent(),
1560 MachineOperand::CreateReg(*ImpUses, false, true));
1561 }
1562
1563 // Modify the condition code of operands in OperandsToUpdate.
1564 // Since we have SUB(r1, r2) and CMP(r2, r1), the condition code needs to
1565 // be changed from r2 > r1 to r1 < r2, from r2 < r1 to r1 > r2, etc.
Hal Finkele6322392013-04-19 22:08:38 +00001566 for (unsigned i = 0, e = PredsToUpdate.size(); i < e; i++)
1567 PredsToUpdate[i].first->setImm(PredsToUpdate[i].second);
Hal Finkel82656cb2013-04-18 22:15:08 +00001568
Hal Finkele6322392013-04-19 22:08:38 +00001569 for (unsigned i = 0, e = SubRegsToUpdate.size(); i < e; i++)
1570 SubRegsToUpdate[i].first->setSubReg(SubRegsToUpdate[i].second);
Hal Finkel82656cb2013-04-18 22:15:08 +00001571
1572 return true;
1573}
1574
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +00001575/// GetInstSize - Return the number of bytes of code the specified
1576/// instruction may be. This returns the maximum number of bytes.
1577///
1578unsigned PPCInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
Hal Finkela7bbaf62014-02-02 06:12:27 +00001579 unsigned Opcode = MI->getOpcode();
1580
1581 if (Opcode == PPC::INLINEASM) {
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +00001582 const MachineFunction *MF = MI->getParent()->getParent();
1583 const char *AsmStr = MI->getOperand(0).getSymbolName();
Chris Lattner7b26fce2009-08-22 20:48:53 +00001584 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
Hal Finkela7bbaf62014-02-02 06:12:27 +00001585 } else {
1586 const MCInstrDesc &Desc = get(Opcode);
1587 return Desc.getSize();
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +00001588 }
1589}
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001590
Hal Finkel174e5902014-03-25 23:29:21 +00001591#undef DEBUG_TYPE
1592#define DEBUG_TYPE "ppc-vsx-fma-mutate"
1593
1594namespace {
1595 // PPCVSXFMAMutate pass - For copies between VSX registers and non-VSX registers
1596 // (Altivec and scalar floating-point registers), we need to transform the
1597 // copies into subregister copies with other restrictions.
1598 struct PPCVSXFMAMutate : public MachineFunctionPass {
1599 static char ID;
1600 PPCVSXFMAMutate() : MachineFunctionPass(ID) {
1601 initializePPCVSXFMAMutatePass(*PassRegistry::getPassRegistry());
1602 }
1603
1604 LiveIntervals *LIS;
1605
1606 const PPCTargetMachine *TM;
1607 const PPCInstrInfo *TII;
1608
1609protected:
1610 bool processBlock(MachineBasicBlock &MBB) {
1611 bool Changed = false;
1612
1613 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
1614 for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end();
1615 I != IE; ++I) {
1616 MachineInstr *MI = I;
1617
1618 // The default (A-type) VSX FMA form kills the addend (it is taken from
1619 // the target register, which is then updated to reflect the result of
1620 // the FMA). If the instruction, however, kills one of the registers
1621 // used for the product, then we can use the M-form instruction (which
1622 // will take that value from the to-be-defined register).
1623
1624 int AltOpc = PPC::getAltVSXFMAOpcode(MI->getOpcode());
1625 if (AltOpc == -1)
1626 continue;
1627
1628 // This pass is run after register coalescing, and so we're looking for
1629 // a situation like this:
1630 // ...
1631 // %vreg5<def> = COPY %vreg9; VSLRC:%vreg5,%vreg9
1632 // %vreg5<def,tied1> = XSMADDADP %vreg5<tied0>, %vreg17, %vreg16,
1633 // %RM<imp-use>; VSLRC:%vreg5,%vreg17,%vreg16
1634 // ...
1635 // %vreg9<def,tied1> = XSMADDADP %vreg9<tied0>, %vreg17, %vreg19,
1636 // %RM<imp-use>; VSLRC:%vreg9,%vreg17,%vreg19
1637 // ...
1638 // Where we can eliminate the copy by changing from the A-type to the
1639 // M-type instruction. Specifically, for this example, this means:
1640 // %vreg5<def,tied1> = XSMADDADP %vreg5<tied0>, %vreg17, %vreg16,
1641 // %RM<imp-use>; VSLRC:%vreg5,%vreg17,%vreg16
1642 // is replaced by:
1643 // %vreg16<def,tied1> = XSMADDMDP %vreg16<tied0>, %vreg18, %vreg9,
1644 // %RM<imp-use>; VSLRC:%vreg16,%vreg18,%vreg9
1645 // and we remove: %vreg5<def> = COPY %vreg9; VSLRC:%vreg5,%vreg9
1646
1647 SlotIndex FMAIdx = LIS->getInstructionIndex(MI);
1648
1649 VNInfo *AddendValNo =
1650 LIS->getInterval(MI->getOperand(1).getReg()).Query(FMAIdx).valueIn();
1651 MachineInstr *AddendMI = LIS->getInstructionFromIndex(AddendValNo->def);
1652
1653 // The addend and this instruction must be in the same block.
1654
Hal Finkel19be5062014-03-29 05:29:01 +00001655 if (!AddendMI || AddendMI->getParent() != MI->getParent())
Hal Finkel174e5902014-03-25 23:29:21 +00001656 continue;
1657
1658 // The addend must be a full copy within the same register class.
1659
1660 if (!AddendMI->isFullCopy())
1661 continue;
1662
Hal Finkel19be5062014-03-29 05:29:01 +00001663 unsigned AddendSrcReg = AddendMI->getOperand(1).getReg();
1664 if (TargetRegisterInfo::isVirtualRegister(AddendSrcReg)) {
1665 if (MRI.getRegClass(AddendMI->getOperand(0).getReg()) !=
1666 MRI.getRegClass(AddendSrcReg))
1667 continue;
1668 } else {
1669 // If AddendSrcReg is a physical register, make sure the destination
1670 // register class contains it.
1671 if (!MRI.getRegClass(AddendMI->getOperand(0).getReg())
1672 ->contains(AddendSrcReg))
1673 continue;
1674 }
Hal Finkel174e5902014-03-25 23:29:21 +00001675
1676 // In theory, there could be other uses of the addend copy before this
1677 // fma. We could deal with this, but that would require additional
1678 // logic below and I suspect it will not occur in any relevant
1679 // situations.
1680 bool OtherUsers = false;
1681 for (auto J = std::prev(I), JE = MachineBasicBlock::iterator(AddendMI);
1682 J != JE; --J)
1683 if (J->readsVirtualRegister(AddendMI->getOperand(0).getReg())) {
1684 OtherUsers = true;
1685 break;
1686 }
1687
1688 if (OtherUsers)
1689 continue;
1690
1691 // Find one of the product operands that is killed by this instruction.
1692
1693 unsigned KilledProdOp = 0, OtherProdOp = 0;
1694 if (LIS->getInterval(MI->getOperand(2).getReg())
1695 .Query(FMAIdx).isKill()) {
1696 KilledProdOp = 2;
1697 OtherProdOp = 3;
1698 } else if (LIS->getInterval(MI->getOperand(3).getReg())
1699 .Query(FMAIdx).isKill()) {
1700 KilledProdOp = 3;
1701 OtherProdOp = 2;
1702 }
1703
Hal Finkel19be5062014-03-29 05:29:01 +00001704 // If there are no killed product operands, then this transformation is
1705 // likely not profitable.
Hal Finkel174e5902014-03-25 23:29:21 +00001706 if (!KilledProdOp)
1707 continue;
1708
1709 // In order to replace the addend here with the source of the copy,
1710 // it must still be live here.
1711 if (!LIS->getInterval(AddendMI->getOperand(1).getReg()).liveAt(FMAIdx))
1712 continue;
1713
1714 // Transform: (O2 * O3) + O1 -> (O2 * O1) + O3.
1715
1716 unsigned AddReg = AddendMI->getOperand(1).getReg();
1717 unsigned KilledProdReg = MI->getOperand(KilledProdOp).getReg();
1718 unsigned OtherProdReg = MI->getOperand(OtherProdOp).getReg();
1719
1720 unsigned AddSubReg = AddendMI->getOperand(1).getSubReg();
1721 unsigned KilledProdSubReg = MI->getOperand(KilledProdOp).getSubReg();
1722 unsigned OtherProdSubReg = MI->getOperand(OtherProdOp).getSubReg();
1723
1724 bool AddRegKill = AddendMI->getOperand(1).isKill();
1725 bool KilledProdRegKill = MI->getOperand(KilledProdOp).isKill();
1726 bool OtherProdRegKill = MI->getOperand(OtherProdOp).isKill();
1727
1728 bool AddRegUndef = AddendMI->getOperand(1).isUndef();
1729 bool KilledProdRegUndef = MI->getOperand(KilledProdOp).isUndef();
1730 bool OtherProdRegUndef = MI->getOperand(OtherProdOp).isUndef();
1731
1732 unsigned OldFMAReg = MI->getOperand(0).getReg();
1733
1734 assert(OldFMAReg == AddendMI->getOperand(0).getReg() &&
1735 "Addend copy not tied to old FMA output!");
1736
1737 DEBUG(dbgs() << "VSX FMA Mutation:\n " << *MI;);
1738
1739 MI->getOperand(0).setReg(KilledProdReg);
1740 MI->getOperand(1).setReg(KilledProdReg);
1741 MI->getOperand(3).setReg(AddReg);
1742 MI->getOperand(2).setReg(OtherProdReg);
1743
1744 MI->getOperand(0).setSubReg(KilledProdSubReg);
1745 MI->getOperand(1).setSubReg(KilledProdSubReg);
1746 MI->getOperand(3).setSubReg(AddSubReg);
1747 MI->getOperand(2).setSubReg(OtherProdSubReg);
1748
1749 MI->getOperand(1).setIsKill(KilledProdRegKill);
1750 MI->getOperand(3).setIsKill(AddRegKill);
1751 MI->getOperand(2).setIsKill(OtherProdRegKill);
1752
1753 MI->getOperand(1).setIsUndef(KilledProdRegUndef);
1754 MI->getOperand(3).setIsUndef(AddRegUndef);
1755 MI->getOperand(2).setIsUndef(OtherProdRegUndef);
1756
1757 MI->setDesc(TII->get(AltOpc));
1758
1759 DEBUG(dbgs() << " -> " << *MI);
1760
1761 // The killed product operand was killed here, so we can reuse it now
1762 // for the result of the fma.
1763
1764 LiveInterval &FMAInt = LIS->getInterval(OldFMAReg);
1765 VNInfo *FMAValNo = FMAInt.getVNInfoAt(FMAIdx.getRegSlot());
1766 for (auto UI = MRI.reg_nodbg_begin(OldFMAReg), UE = MRI.reg_nodbg_end();
1767 UI != UE;) {
1768 MachineOperand &UseMO = *UI;
1769 MachineInstr *UseMI = UseMO.getParent();
1770 ++UI;
1771
1772 // Don't replace the result register of the copy we're about to erase.
1773 if (UseMI == AddendMI)
1774 continue;
1775
1776 UseMO.setReg(KilledProdReg);
1777 UseMO.setSubReg(KilledProdSubReg);
1778 }
1779
1780 // Extend the live intervals of the killed product operand to hold the
1781 // fma result.
1782
1783 LiveInterval &NewFMAInt = LIS->getInterval(KilledProdReg);
1784 for (LiveInterval::iterator AI = FMAInt.begin(), AE = FMAInt.end();
1785 AI != AE; ++AI) {
1786 // Don't add the segment that corresponds to the original copy.
1787 if (AI->valno == AddendValNo)
1788 continue;
1789
1790 VNInfo *NewFMAValNo =
1791 NewFMAInt.getNextValue(AI->start,
1792 LIS->getVNInfoAllocator());
1793
1794 NewFMAInt.addSegment(LiveInterval::Segment(AI->start, AI->end,
1795 NewFMAValNo));
1796 }
1797 DEBUG(dbgs() << " extended: " << NewFMAInt << '\n');
1798
1799 FMAInt.removeValNo(FMAValNo);
1800 DEBUG(dbgs() << " trimmed: " << FMAInt << '\n');
1801
1802 // Remove the (now unused) copy.
1803
1804 DEBUG(dbgs() << " removing: " << *AddendMI << '\n');
1805 LIS->RemoveMachineInstrFromMaps(AddendMI);
1806 AddendMI->eraseFromParent();
1807
1808 Changed = true;
1809 }
1810
1811 return Changed;
1812 }
1813
1814public:
1815 virtual bool runOnMachineFunction(MachineFunction &MF) {
1816 LIS = &getAnalysis<LiveIntervals>();
1817
1818 TM = static_cast<const PPCTargetMachine *>(&MF.getTarget());
1819 TII = TM->getInstrInfo();
1820
1821 bool Changed = false;
1822
1823 if (DisableVSXFMAMutate)
1824 return Changed;
1825
1826 for (MachineFunction::iterator I = MF.begin(); I != MF.end();) {
1827 MachineBasicBlock &B = *I++;
1828 if (processBlock(B))
1829 Changed = true;
1830 }
1831
1832 return Changed;
1833 }
1834
1835 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
1836 AU.addRequired<LiveIntervals>();
1837 AU.addPreserved<LiveIntervals>();
1838 AU.addRequired<SlotIndexes>();
1839 AU.addPreserved<SlotIndexes>();
1840 MachineFunctionPass::getAnalysisUsage(AU);
1841 }
1842 };
1843}
1844
1845INITIALIZE_PASS_BEGIN(PPCVSXFMAMutate, DEBUG_TYPE,
1846 "PowerPC VSX FMA Mutation", false, false)
1847INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
1848INITIALIZE_PASS_DEPENDENCY(SlotIndexes)
1849INITIALIZE_PASS_END(PPCVSXFMAMutate, DEBUG_TYPE,
1850 "PowerPC VSX FMA Mutation", false, false)
1851
1852char &llvm::PPCVSXFMAMutateID = PPCVSXFMAMutate::ID;
1853
1854char PPCVSXFMAMutate::ID = 0;
1855FunctionPass*
1856llvm::createPPCVSXFMAMutatePass() { return new PPCVSXFMAMutate(); }
Hal Finkel27774d92014-03-13 07:58:58 +00001857
1858#undef DEBUG_TYPE
1859#define DEBUG_TYPE "ppc-vsx-copy"
1860
1861namespace llvm {
1862 void initializePPCVSXCopyPass(PassRegistry&);
1863}
1864
1865namespace {
1866 // PPCVSXCopy pass - For copies between VSX registers and non-VSX registers
1867 // (Altivec and scalar floating-point registers), we need to transform the
1868 // copies into subregister copies with other restrictions.
1869 struct PPCVSXCopy : public MachineFunctionPass {
1870 static char ID;
1871 PPCVSXCopy() : MachineFunctionPass(ID) {
1872 initializePPCVSXCopyPass(*PassRegistry::getPassRegistry());
1873 }
1874
1875 const PPCTargetMachine *TM;
1876 const PPCInstrInfo *TII;
1877
1878 bool IsRegInClass(unsigned Reg, const TargetRegisterClass *RC,
1879 MachineRegisterInfo &MRI) {
1880 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1881 return RC->hasSubClassEq(MRI.getRegClass(Reg));
1882 } else if (RC->contains(Reg)) {
1883 return true;
1884 }
1885
1886 return false;
1887 }
1888
1889 bool IsVSReg(unsigned Reg, MachineRegisterInfo &MRI) {
1890 return IsRegInClass(Reg, &PPC::VSRCRegClass, MRI);
1891 }
1892
1893 bool IsVRReg(unsigned Reg, MachineRegisterInfo &MRI) {
1894 return IsRegInClass(Reg, &PPC::VRRCRegClass, MRI);
1895 }
1896
1897 bool IsF8Reg(unsigned Reg, MachineRegisterInfo &MRI) {
1898 return IsRegInClass(Reg, &PPC::F8RCRegClass, MRI);
1899 }
1900
1901protected:
1902 bool processBlock(MachineBasicBlock &MBB) {
1903 bool Changed = false;
1904
1905 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
1906 for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end();
1907 I != IE; ++I) {
1908 MachineInstr *MI = I;
1909 if (!MI->isFullCopy())
1910 continue;
1911
1912 MachineOperand &DstMO = MI->getOperand(0);
1913 MachineOperand &SrcMO = MI->getOperand(1);
1914
1915 if ( IsVSReg(DstMO.getReg(), MRI) &&
1916 !IsVSReg(SrcMO.getReg(), MRI)) {
1917 // This is a copy *to* a VSX register from a non-VSX register.
1918 Changed = true;
1919
1920 const TargetRegisterClass *SrcRC =
1921 IsVRReg(SrcMO.getReg(), MRI) ? &PPC::VSHRCRegClass :
1922 &PPC::VSLRCRegClass;
1923 assert((IsF8Reg(SrcMO.getReg(), MRI) ||
1924 IsVRReg(SrcMO.getReg(), MRI)) &&
1925 "Unknown source for a VSX copy");
1926
1927 unsigned NewVReg = MRI.createVirtualRegister(SrcRC);
1928 BuildMI(MBB, MI, MI->getDebugLoc(),
1929 TII->get(TargetOpcode::SUBREG_TO_REG), NewVReg)
1930 .addImm(1) // add 1, not 0, because there is no implicit clearing
1931 // of the high bits.
1932 .addOperand(SrcMO)
1933 .addImm(IsVRReg(SrcMO.getReg(), MRI) ? PPC::sub_128 :
1934 PPC::sub_64);
1935
1936 // The source of the original copy is now the new virtual register.
1937 SrcMO.setReg(NewVReg);
1938 } else if (!IsVSReg(DstMO.getReg(), MRI) &&
1939 IsVSReg(SrcMO.getReg(), MRI)) {
1940 // This is a copy *from* a VSX register to a non-VSX register.
1941 Changed = true;
1942
1943 const TargetRegisterClass *DstRC =
1944 IsVRReg(DstMO.getReg(), MRI) ? &PPC::VSHRCRegClass :
1945 &PPC::VSLRCRegClass;
1946 assert((IsF8Reg(DstMO.getReg(), MRI) ||
1947 IsVRReg(DstMO.getReg(), MRI)) &&
1948 "Unknown destination for a VSX copy");
1949
1950 // Copy the VSX value into a new VSX register of the correct subclass.
1951 unsigned NewVReg = MRI.createVirtualRegister(DstRC);
1952 BuildMI(MBB, MI, MI->getDebugLoc(),
1953 TII->get(TargetOpcode::COPY), NewVReg)
1954 .addOperand(SrcMO);
1955
1956 // Transform the original copy into a subregister extraction copy.
1957 SrcMO.setReg(NewVReg);
1958 SrcMO.setSubReg(IsVRReg(DstMO.getReg(), MRI) ? PPC::sub_128 :
1959 PPC::sub_64);
1960 }
1961 }
1962
1963 return Changed;
1964 }
1965
1966public:
1967 virtual bool runOnMachineFunction(MachineFunction &MF) {
1968 TM = static_cast<const PPCTargetMachine *>(&MF.getTarget());
1969 TII = TM->getInstrInfo();
1970
1971 bool Changed = false;
1972
1973 for (MachineFunction::iterator I = MF.begin(); I != MF.end();) {
1974 MachineBasicBlock &B = *I++;
1975 if (processBlock(B))
1976 Changed = true;
1977 }
1978
1979 return Changed;
1980 }
1981
1982 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
1983 MachineFunctionPass::getAnalysisUsage(AU);
1984 }
1985 };
1986}
1987
1988INITIALIZE_PASS(PPCVSXCopy, DEBUG_TYPE,
1989 "PowerPC VSX Copy Legalization", false, false)
1990
1991char PPCVSXCopy::ID = 0;
1992FunctionPass*
1993llvm::createPPCVSXCopyPass() { return new PPCVSXCopy(); }
1994
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001995#undef DEBUG_TYPE
Hal Finkelc6fc9b82014-03-27 23:12:31 +00001996#define DEBUG_TYPE "ppc-vsx-copy-cleanup"
1997
1998namespace llvm {
1999 void initializePPCVSXCopyCleanupPass(PassRegistry&);
2000}
2001
2002namespace {
2003 // PPCVSXCopyCleanup pass - We sometimes end up generating self copies of VSX
2004 // registers (mostly because the ABI code still places all values into the
2005 // "traditional" floating-point and vector registers). Remove them here.
2006 struct PPCVSXCopyCleanup : public MachineFunctionPass {
2007 static char ID;
2008 PPCVSXCopyCleanup() : MachineFunctionPass(ID) {
2009 initializePPCVSXCopyCleanupPass(*PassRegistry::getPassRegistry());
2010 }
2011
2012 const PPCTargetMachine *TM;
2013 const PPCInstrInfo *TII;
2014
2015protected:
2016 bool processBlock(MachineBasicBlock &MBB) {
2017 bool Changed = false;
2018
2019 SmallVector<MachineInstr *, 4> ToDelete;
2020 for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end();
2021 I != IE; ++I) {
2022 MachineInstr *MI = I;
2023 if (MI->getOpcode() == PPC::XXLOR &&
2024 MI->getOperand(0).getReg() == MI->getOperand(1).getReg() &&
2025 MI->getOperand(0).getReg() == MI->getOperand(2).getReg())
2026 ToDelete.push_back(MI);
2027 }
2028
2029 if (!ToDelete.empty())
2030 Changed = true;
2031
2032 for (unsigned i = 0, ie = ToDelete.size(); i != ie; ++i) {
2033 DEBUG(dbgs() << "Removing VSX self-copy: " << *ToDelete[i]);
2034 ToDelete[i]->eraseFromParent();
2035 }
2036
2037 return Changed;
2038 }
2039
2040public:
2041 virtual bool runOnMachineFunction(MachineFunction &MF) {
2042 TM = static_cast<const PPCTargetMachine *>(&MF.getTarget());
2043 TII = TM->getInstrInfo();
2044
2045 bool Changed = false;
2046
2047 for (MachineFunction::iterator I = MF.begin(); I != MF.end();) {
2048 MachineBasicBlock &B = *I++;
2049 if (processBlock(B))
2050 Changed = true;
2051 }
2052
2053 return Changed;
2054 }
2055
2056 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
2057 MachineFunctionPass::getAnalysisUsage(AU);
2058 }
2059 };
2060}
2061
2062INITIALIZE_PASS(PPCVSXCopyCleanup, DEBUG_TYPE,
2063 "PowerPC VSX Copy Cleanup", false, false)
2064
2065char PPCVSXCopyCleanup::ID = 0;
2066FunctionPass*
2067llvm::createPPCVSXCopyCleanupPass() { return new PPCVSXCopyCleanup(); }
2068
2069#undef DEBUG_TYPE
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002070#define DEBUG_TYPE "ppc-early-ret"
2071STATISTIC(NumBCLR, "Number of early conditional returns");
2072STATISTIC(NumBLR, "Number of early returns");
2073
2074namespace llvm {
2075 void initializePPCEarlyReturnPass(PassRegistry&);
2076}
2077
2078namespace {
2079 // PPCEarlyReturn pass - For simple functions without epilogue code, move
2080 // returns up, and create conditional returns, to avoid unnecessary
2081 // branch-to-blr sequences.
2082 struct PPCEarlyReturn : public MachineFunctionPass {
2083 static char ID;
2084 PPCEarlyReturn() : MachineFunctionPass(ID) {
2085 initializePPCEarlyReturnPass(*PassRegistry::getPassRegistry());
2086 }
2087
2088 const PPCTargetMachine *TM;
2089 const PPCInstrInfo *TII;
2090
2091protected:
Hal Finkel21aad9a2013-04-09 18:25:18 +00002092 bool processBlock(MachineBasicBlock &ReturnMBB) {
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002093 bool Changed = false;
2094
Hal Finkel21aad9a2013-04-09 18:25:18 +00002095 MachineBasicBlock::iterator I = ReturnMBB.begin();
2096 I = ReturnMBB.SkipPHIsAndLabels(I);
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002097
2098 // The block must be essentially empty except for the blr.
Hal Finkel21aad9a2013-04-09 18:25:18 +00002099 if (I == ReturnMBB.end() || I->getOpcode() != PPC::BLR ||
2100 I != ReturnMBB.getLastNonDebugInstr())
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002101 return Changed;
2102
2103 SmallVector<MachineBasicBlock*, 8> PredToRemove;
Hal Finkel21aad9a2013-04-09 18:25:18 +00002104 for (MachineBasicBlock::pred_iterator PI = ReturnMBB.pred_begin(),
2105 PIE = ReturnMBB.pred_end(); PI != PIE; ++PI) {
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002106 bool OtherReference = false, BlockChanged = false;
Hal Finkel21aad9a2013-04-09 18:25:18 +00002107 for (MachineBasicBlock::iterator J = (*PI)->getLastNonDebugInstr();;) {
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002108 if (J->getOpcode() == PPC::B) {
Hal Finkel21aad9a2013-04-09 18:25:18 +00002109 if (J->getOperand(0).getMBB() == &ReturnMBB) {
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002110 // This is an unconditional branch to the return. Replace the
Andrew Trick9defbd82013-12-17 04:50:40 +00002111 // branch with a blr.
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002112 BuildMI(**PI, J, J->getDebugLoc(), TII->get(PPC::BLR));
Hal Finkel21aad9a2013-04-09 18:25:18 +00002113 MachineBasicBlock::iterator K = J--;
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002114 K->eraseFromParent();
2115 BlockChanged = true;
2116 ++NumBLR;
2117 continue;
2118 }
2119 } else if (J->getOpcode() == PPC::BCC) {
Hal Finkel21aad9a2013-04-09 18:25:18 +00002120 if (J->getOperand(2).getMBB() == &ReturnMBB) {
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002121 // This is a conditional branch to the return. Replace the branch
2122 // with a bclr.
Hal Finkel940ab932014-02-28 00:27:01 +00002123 BuildMI(**PI, J, J->getDebugLoc(), TII->get(PPC::BCCLR))
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002124 .addImm(J->getOperand(0).getImm())
2125 .addReg(J->getOperand(1).getReg());
Hal Finkel21aad9a2013-04-09 18:25:18 +00002126 MachineBasicBlock::iterator K = J--;
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002127 K->eraseFromParent();
2128 BlockChanged = true;
2129 ++NumBCLR;
2130 continue;
2131 }
Hal Finkel940ab932014-02-28 00:27:01 +00002132 } else if (J->getOpcode() == PPC::BC || J->getOpcode() == PPC::BCn) {
2133 if (J->getOperand(1).getMBB() == &ReturnMBB) {
2134 // This is a conditional branch to the return. Replace the branch
2135 // with a bclr.
2136 BuildMI(**PI, J, J->getDebugLoc(),
2137 TII->get(J->getOpcode() == PPC::BC ?
2138 PPC::BCLR : PPC::BCLRn))
2139 .addReg(J->getOperand(0).getReg());
2140 MachineBasicBlock::iterator K = J--;
2141 K->eraseFromParent();
2142 BlockChanged = true;
2143 ++NumBCLR;
2144 continue;
2145 }
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002146 } else if (J->isBranch()) {
2147 if (J->isIndirectBranch()) {
Hal Finkel21aad9a2013-04-09 18:25:18 +00002148 if (ReturnMBB.hasAddressTaken())
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002149 OtherReference = true;
2150 } else
2151 for (unsigned i = 0; i < J->getNumOperands(); ++i)
2152 if (J->getOperand(i).isMBB() &&
Hal Finkel21aad9a2013-04-09 18:25:18 +00002153 J->getOperand(i).getMBB() == &ReturnMBB)
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002154 OtherReference = true;
Hal Finkel21aad9a2013-04-09 18:25:18 +00002155 } else if (!J->isTerminator() && !J->isDebugValue())
2156 break;
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002157
Hal Finkel21aad9a2013-04-09 18:25:18 +00002158 if (J == (*PI)->begin())
2159 break;
2160
2161 --J;
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002162 }
2163
Hal Finkel21aad9a2013-04-09 18:25:18 +00002164 if ((*PI)->canFallThrough() && (*PI)->isLayoutSuccessor(&ReturnMBB))
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002165 OtherReference = true;
2166
Andrew Trick9defbd82013-12-17 04:50:40 +00002167 // Predecessors are stored in a vector and can't be removed here.
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002168 if (!OtherReference && BlockChanged) {
2169 PredToRemove.push_back(*PI);
2170 }
2171
2172 if (BlockChanged)
2173 Changed = true;
2174 }
2175
2176 for (unsigned i = 0, ie = PredToRemove.size(); i != ie; ++i)
Hal Finkel21aad9a2013-04-09 18:25:18 +00002177 PredToRemove[i]->removeSuccessor(&ReturnMBB);
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002178
Hal Finkel21aad9a2013-04-09 18:25:18 +00002179 if (Changed && !ReturnMBB.hasAddressTaken()) {
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002180 // We now might be able to merge this blr-only block into its
2181 // by-layout predecessor.
Hal Finkel21aad9a2013-04-09 18:25:18 +00002182 if (ReturnMBB.pred_size() == 1 &&
2183 (*ReturnMBB.pred_begin())->isLayoutSuccessor(&ReturnMBB)) {
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002184 // Move the blr into the preceding block.
Hal Finkel21aad9a2013-04-09 18:25:18 +00002185 MachineBasicBlock &PrevMBB = **ReturnMBB.pred_begin();
2186 PrevMBB.splice(PrevMBB.end(), &ReturnMBB, I);
2187 PrevMBB.removeSuccessor(&ReturnMBB);
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002188 }
2189
Hal Finkel21aad9a2013-04-09 18:25:18 +00002190 if (ReturnMBB.pred_empty())
2191 ReturnMBB.eraseFromParent();
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002192 }
2193
2194 return Changed;
2195 }
2196
2197public:
2198 virtual bool runOnMachineFunction(MachineFunction &MF) {
2199 TM = static_cast<const PPCTargetMachine *>(&MF.getTarget());
2200 TII = TM->getInstrInfo();
2201
2202 bool Changed = false;
2203
Hal Finkel21aad9a2013-04-09 18:25:18 +00002204 // If the function does not have at least two blocks, then there is
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002205 // nothing to do.
2206 if (MF.size() < 2)
2207 return Changed;
2208
2209 for (MachineFunction::iterator I = MF.begin(); I != MF.end();) {
Andrew Trick9defbd82013-12-17 04:50:40 +00002210 MachineBasicBlock &B = *I++;
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002211 if (processBlock(B))
2212 Changed = true;
2213 }
2214
2215 return Changed;
2216 }
2217
2218 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
2219 MachineFunctionPass::getAnalysisUsage(AU);
2220 }
2221 };
2222}
2223
2224INITIALIZE_PASS(PPCEarlyReturn, DEBUG_TYPE,
2225 "PowerPC Early-Return Creation", false, false)
2226
2227char PPCEarlyReturn::ID = 0;
2228FunctionPass*
2229llvm::createPPCEarlyReturnPass() { return new PPCEarlyReturn(); }