blob: c743d66ee04b5b03e9f827d31405be4ce486ac5f [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"
Eric Christopher1dcea732014-06-12 21:48:52 +000030#include "llvm/CodeGen/ScheduleDAG.h"
Hal Finkel174e5902014-03-25 23:29:21 +000031#include "llvm/CodeGen/SlotIndexes.h"
Evan Chengc5e6d2f2011-07-11 03:57:24 +000032#include "llvm/MC/MCAsmInfo.h"
Bill Wendling1af20ad2008-03-04 23:13:51 +000033#include "llvm/Support/CommandLine.h"
Hal Finkel174e5902014-03-25 23:29:21 +000034#include "llvm/Support/Debug.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000035#include "llvm/Support/ErrorHandling.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000036#include "llvm/Support/TargetRegistry.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000037#include "llvm/Support/raw_ostream.h"
Misha Brukman116f9272004-08-17 04:55:41 +000038
Dan Gohman20857192010-04-15 17:20:57 +000039using namespace llvm;
Bill Wendling1af20ad2008-03-04 23:13:51 +000040
Chandler Carruthe96dd892014-04-21 22:55:11 +000041#define DEBUG_TYPE "ppc-instr-info"
42
Chandler Carruthd174b722014-04-22 02:03:14 +000043#define GET_INSTRMAP_INFO
44#define GET_INSTRINFO_CTOR_DTOR
45#include "PPCGenInstrInfo.inc"
46
Hal Finkel821e0012012-06-08 15:38:25 +000047static cl::
Hal Finkelc6b5deb2012-06-08 19:19:53 +000048opt<bool> DisableCTRLoopAnal("disable-ppc-ctrloop-analysis", cl::Hidden,
49 cl::desc("Disable analysis for CTR loops"));
Hal Finkel821e0012012-06-08 15:38:25 +000050
Hal Finkele6322392013-04-19 22:08:38 +000051static cl::opt<bool> DisableCmpOpt("disable-ppc-cmp-opt",
Hal Finkelb12da6b2013-04-18 22:54:25 +000052cl::desc("Disable compare instruction optimization"), cl::Hidden);
53
Hal Finkel174e5902014-03-25 23:29:21 +000054static cl::opt<bool> DisableVSXFMAMutate("disable-ppc-vsx-fma-mutation",
55cl::desc("Disable VSX FMA instruction mutation"), cl::Hidden);
56
Hal Finkel9dcb3582014-03-27 22:46:28 +000057static cl::opt<bool> VSXSelfCopyCrash("crash-on-ppc-vsx-self-copy",
58cl::desc("Causes the backend to crash instead of generating a nop VSX copy"),
59cl::Hidden);
60
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000061// Pin the vtable to this file.
62void PPCInstrInfo::anchor() {}
63
Eric Christopher1dcea732014-06-12 21:48:52 +000064PPCInstrInfo::PPCInstrInfo(PPCSubtarget &STI)
65 : PPCGenInstrInfo(PPC::ADJCALLSTACKDOWN, PPC::ADJCALLSTACKUP),
66 Subtarget(STI), RI(STI) {}
Chris Lattner49cadab2006-06-17 00:01:04 +000067
Andrew Trick10ffc2b2010-12-24 05:03:26 +000068/// CreateTargetHazardRecognizer - Return the hazard recognizer to use for
69/// this target when scheduling the DAG.
Eric Christopherf047bfd2014-06-13 22:38:52 +000070ScheduleHazardRecognizer *
71PPCInstrInfo::CreateTargetHazardRecognizer(const TargetSubtargetInfo *STI,
72 const ScheduleDAG *DAG) const {
73 unsigned Directive =
74 static_cast<const PPCSubtarget *>(STI)->getDarwinDirective();
Hal Finkel742b5352012-08-28 16:12:39 +000075 if (Directive == PPC::DIR_440 || Directive == PPC::DIR_A2 ||
76 Directive == PPC::DIR_E500mc || Directive == PPC::DIR_E5500) {
Eric Christopherf047bfd2014-06-13 22:38:52 +000077 const InstrItineraryData *II =
Eric Christopherd9134482014-08-04 21:25:23 +000078 static_cast<const PPCSubtarget *>(STI)->getInstrItineraryData();
Hal Finkel563cc052013-12-02 23:52:46 +000079 return new ScoreboardHazardRecognizer(II, DAG);
Hal Finkel6fa56972011-10-17 04:03:49 +000080 }
Hal Finkel58ca3602011-12-02 04:58:02 +000081
Eric Christopherf047bfd2014-06-13 22:38:52 +000082 return TargetInstrInfo::CreateTargetHazardRecognizer(STI, DAG);
Andrew Trick10ffc2b2010-12-24 05:03:26 +000083}
84
Hal Finkel58ca3602011-12-02 04:58:02 +000085/// CreateTargetPostRAHazardRecognizer - Return the postRA hazard recognizer
86/// to use for this target when scheduling the DAG.
87ScheduleHazardRecognizer *PPCInstrInfo::CreateTargetPostRAHazardRecognizer(
88 const InstrItineraryData *II,
89 const ScheduleDAG *DAG) const {
Eric Christopher1dcea732014-06-12 21:48:52 +000090 unsigned Directive =
91 DAG->TM.getSubtarget<PPCSubtarget>().getDarwinDirective();
Hal Finkel58ca3602011-12-02 04:58:02 +000092
Will Schmidt970ff642014-06-26 13:36:19 +000093 if (Directive == PPC::DIR_PWR7 || Directive == PPC::DIR_PWR8)
Hal Finkelceb1f122013-12-12 00:19:11 +000094 return new PPCDispatchGroupSBHazardRecognizer(II, DAG);
95
Hal Finkel58ca3602011-12-02 04:58:02 +000096 // Most subtargets use a PPC970 recognizer.
Hal Finkel742b5352012-08-28 16:12:39 +000097 if (Directive != PPC::DIR_440 && Directive != PPC::DIR_A2 &&
98 Directive != PPC::DIR_E500mc && Directive != PPC::DIR_E5500) {
Eric Christopher1dcea732014-06-12 21:48:52 +000099 assert(DAG->TII && "No InstrInfo?");
Hal Finkel58ca3602011-12-02 04:58:02 +0000100
Eric Christopher1dcea732014-06-12 21:48:52 +0000101 return new PPCHazardRecognizer970(*DAG);
Hal Finkel58ca3602011-12-02 04:58:02 +0000102 }
103
Hal Finkel563cc052013-12-02 23:52:46 +0000104 return new ScoreboardHazardRecognizer(II, DAG);
Hal Finkel58ca3602011-12-02 04:58:02 +0000105}
Jakob Stoklund Olesen0f855e42012-06-19 21:14:34 +0000106
Hal Finkelceb1f122013-12-12 00:19:11 +0000107
108int PPCInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
109 const MachineInstr *DefMI, unsigned DefIdx,
110 const MachineInstr *UseMI,
111 unsigned UseIdx) const {
112 int Latency = PPCGenInstrInfo::getOperandLatency(ItinData, DefMI, DefIdx,
113 UseMI, UseIdx);
114
115 const MachineOperand &DefMO = DefMI->getOperand(DefIdx);
116 unsigned Reg = DefMO.getReg();
117
118 const TargetRegisterInfo *TRI = &getRegisterInfo();
119 bool IsRegCR;
120 if (TRI->isVirtualRegister(Reg)) {
121 const MachineRegisterInfo *MRI =
122 &DefMI->getParent()->getParent()->getRegInfo();
123 IsRegCR = MRI->getRegClass(Reg)->hasSuperClassEq(&PPC::CRRCRegClass) ||
124 MRI->getRegClass(Reg)->hasSuperClassEq(&PPC::CRBITRCRegClass);
125 } else {
126 IsRegCR = PPC::CRRCRegClass.contains(Reg) ||
127 PPC::CRBITRCRegClass.contains(Reg);
128 }
129
130 if (UseMI->isBranch() && IsRegCR) {
131 if (Latency < 0)
132 Latency = getInstrLatency(ItinData, DefMI);
133
134 // On some cores, there is an additional delay between writing to a condition
135 // register, and using it from a branch.
Eric Christopher1dcea732014-06-12 21:48:52 +0000136 unsigned Directive = Subtarget.getDarwinDirective();
Hal Finkelceb1f122013-12-12 00:19:11 +0000137 switch (Directive) {
138 default: break;
139 case PPC::DIR_7400:
140 case PPC::DIR_750:
141 case PPC::DIR_970:
142 case PPC::DIR_E5500:
143 case PPC::DIR_PWR4:
144 case PPC::DIR_PWR5:
145 case PPC::DIR_PWR5X:
146 case PPC::DIR_PWR6:
147 case PPC::DIR_PWR6X:
148 case PPC::DIR_PWR7:
Will Schmidt970ff642014-06-26 13:36:19 +0000149 case PPC::DIR_PWR8:
Hal Finkelceb1f122013-12-12 00:19:11 +0000150 Latency += 2;
151 break;
152 }
153 }
154
155 return Latency;
156}
157
Jakob Stoklund Olesen0f855e42012-06-19 21:14:34 +0000158// Detect 32 -> 64-bit extensions where we may reuse the low sub-register.
159bool PPCInstrInfo::isCoalescableExtInstr(const MachineInstr &MI,
160 unsigned &SrcReg, unsigned &DstReg,
161 unsigned &SubIdx) const {
162 switch (MI.getOpcode()) {
163 default: return false;
164 case PPC::EXTSW:
165 case PPC::EXTSW_32_64:
166 SrcReg = MI.getOperand(1).getReg();
167 DstReg = MI.getOperand(0).getReg();
168 SubIdx = PPC::sub_32;
169 return true;
170 }
171}
172
Andrew Trickc416ba62010-12-24 04:28:06 +0000173unsigned PPCInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
Chris Lattner91400bd2006-03-16 22:24:02 +0000174 int &FrameIndex) const {
Hal Finkel37714b82013-03-27 21:21:15 +0000175 // Note: This list must be kept consistent with LoadRegFromStackSlot.
Chris Lattnerbb53acd2006-02-02 20:12:32 +0000176 switch (MI->getOpcode()) {
177 default: break;
178 case PPC::LD:
179 case PPC::LWZ:
180 case PPC::LFS:
181 case PPC::LFD:
Hal Finkel37714b82013-03-27 21:21:15 +0000182 case PPC::RESTORE_CR:
Hal Finkel940ab932014-02-28 00:27:01 +0000183 case PPC::RESTORE_CRBIT:
Hal Finkel37714b82013-03-27 21:21:15 +0000184 case PPC::LVX:
Hal Finkel27774d92014-03-13 07:58:58 +0000185 case PPC::LXVD2X:
Hal Finkel37714b82013-03-27 21:21:15 +0000186 case PPC::RESTORE_VRSAVE:
187 // Check for the operands added by addFrameReference (the immediate is the
188 // offset which defaults to 0).
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000189 if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
190 MI->getOperand(2).isFI()) {
Chris Lattnera5bb3702007-12-30 23:10:15 +0000191 FrameIndex = MI->getOperand(2).getIndex();
Chris Lattnerbb53acd2006-02-02 20:12:32 +0000192 return MI->getOperand(0).getReg();
193 }
194 break;
195 }
196 return 0;
Chris Lattnerc327d712006-02-02 20:16:12 +0000197}
Chris Lattnerbb53acd2006-02-02 20:12:32 +0000198
Andrew Trickc416ba62010-12-24 04:28:06 +0000199unsigned PPCInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
Chris Lattnerc327d712006-02-02 20:16:12 +0000200 int &FrameIndex) const {
Hal Finkel37714b82013-03-27 21:21:15 +0000201 // Note: This list must be kept consistent with StoreRegToStackSlot.
Chris Lattnerc327d712006-02-02 20:16:12 +0000202 switch (MI->getOpcode()) {
203 default: break;
Nate Begeman4efb3282006-02-02 21:07:50 +0000204 case PPC::STD:
Chris Lattnerc327d712006-02-02 20:16:12 +0000205 case PPC::STW:
206 case PPC::STFS:
207 case PPC::STFD:
Hal Finkel37714b82013-03-27 21:21:15 +0000208 case PPC::SPILL_CR:
Hal Finkel940ab932014-02-28 00:27:01 +0000209 case PPC::SPILL_CRBIT:
Hal Finkel37714b82013-03-27 21:21:15 +0000210 case PPC::STVX:
Hal Finkel27774d92014-03-13 07:58:58 +0000211 case PPC::STXVD2X:
Hal Finkel37714b82013-03-27 21:21:15 +0000212 case PPC::SPILL_VRSAVE:
213 // Check for the operands added by addFrameReference (the immediate is the
214 // offset which defaults to 0).
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000215 if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
216 MI->getOperand(2).isFI()) {
Chris Lattnera5bb3702007-12-30 23:10:15 +0000217 FrameIndex = MI->getOperand(2).getIndex();
Chris Lattnerc327d712006-02-02 20:16:12 +0000218 return MI->getOperand(0).getReg();
219 }
220 break;
221 }
222 return 0;
223}
Chris Lattnerbb53acd2006-02-02 20:12:32 +0000224
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000225// commuteInstruction - We can commute rlwimi instructions, but only if the
226// rotate amt is zero. We also have to munge the immediates a bit.
Evan Cheng03553bb2008-06-16 07:33:11 +0000227MachineInstr *
228PPCInstrInfo::commuteInstruction(MachineInstr *MI, bool NewMI) const {
Dan Gohman3b460302008-07-07 23:14:23 +0000229 MachineFunction &MF = *MI->getParent()->getParent();
230
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000231 // Normal instructions can be commuted the obvious way.
Hal Finkel654d43b2013-04-12 02:18:09 +0000232 if (MI->getOpcode() != PPC::RLWIMI &&
Hal Finkel4c6658f2014-12-12 23:59:36 +0000233 MI->getOpcode() != PPC::RLWIMIo)
Jakob Stoklund Olesen9de596e2012-11-28 02:35:17 +0000234 return TargetInstrInfo::commuteInstruction(MI, NewMI);
Hal Finkel4c6658f2014-12-12 23:59:36 +0000235 // Note that RLWIMI can be commuted as a 32-bit instruction, but not as a
236 // 64-bit instruction (so we don't handle PPC::RLWIMI8 here), because
237 // changing the relative order of the mask operands might change what happens
238 // to the high-bits of the mask (and, thus, the result).
Andrew Trickc416ba62010-12-24 04:28:06 +0000239
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000240 // Cannot commute if it has a non-zero rotate count.
Chris Lattner5c463782007-12-30 20:49:49 +0000241 if (MI->getOperand(3).getImm() != 0)
Craig Topper062a2ba2014-04-25 05:30:21 +0000242 return nullptr;
Andrew Trickc416ba62010-12-24 04:28:06 +0000243
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000244 // If we have a zero rotate count, we have:
245 // M = mask(MB,ME)
246 // Op0 = (Op1 & ~M) | (Op2 & M)
247 // Change this to:
248 // M = mask((ME+1)&31, (MB-1)&31)
249 // Op0 = (Op2 & ~M) | (Op1 & M)
250
251 // Swap op1/op2
Evan Cheng244183e2008-02-13 02:46:49 +0000252 unsigned Reg0 = MI->getOperand(0).getReg();
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000253 unsigned Reg1 = MI->getOperand(1).getReg();
254 unsigned Reg2 = MI->getOperand(2).getReg();
Andrew Tricke3398282013-12-17 04:50:45 +0000255 unsigned SubReg1 = MI->getOperand(1).getSubReg();
256 unsigned SubReg2 = MI->getOperand(2).getSubReg();
Evan Chengdc2c8742006-11-15 20:58:11 +0000257 bool Reg1IsKill = MI->getOperand(1).isKill();
258 bool Reg2IsKill = MI->getOperand(2).isKill();
Evan Cheng03553bb2008-06-16 07:33:11 +0000259 bool ChangeReg0 = false;
Evan Cheng244183e2008-02-13 02:46:49 +0000260 // If machine instrs are no longer in two-address forms, update
261 // destination register as well.
262 if (Reg0 == Reg1) {
263 // Must be two address instruction!
Evan Cheng6cc775f2011-06-28 19:10:37 +0000264 assert(MI->getDesc().getOperandConstraint(0, MCOI::TIED_TO) &&
Evan Cheng244183e2008-02-13 02:46:49 +0000265 "Expecting a two-address instruction!");
Andrew Tricke3398282013-12-17 04:50:45 +0000266 assert(MI->getOperand(0).getSubReg() == SubReg1 && "Tied subreg mismatch");
Evan Cheng244183e2008-02-13 02:46:49 +0000267 Reg2IsKill = false;
Evan Cheng03553bb2008-06-16 07:33:11 +0000268 ChangeReg0 = true;
Evan Cheng244183e2008-02-13 02:46:49 +0000269 }
Evan Cheng03553bb2008-06-16 07:33:11 +0000270
271 // Masks.
272 unsigned MB = MI->getOperand(4).getImm();
273 unsigned ME = MI->getOperand(5).getImm();
274
275 if (NewMI) {
276 // Create a new instruction.
277 unsigned Reg0 = ChangeReg0 ? Reg2 : MI->getOperand(0).getReg();
278 bool Reg0IsDead = MI->getOperand(0).isDead();
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000279 return BuildMI(MF, MI->getDebugLoc(), MI->getDesc())
Bill Wendlingf7b83c72009-05-13 21:33:08 +0000280 .addReg(Reg0, RegState::Define | getDeadRegState(Reg0IsDead))
281 .addReg(Reg2, getKillRegState(Reg2IsKill))
282 .addReg(Reg1, getKillRegState(Reg1IsKill))
Evan Cheng03553bb2008-06-16 07:33:11 +0000283 .addImm((ME+1) & 31)
284 .addImm((MB-1) & 31);
285 }
286
Andrew Tricke3398282013-12-17 04:50:45 +0000287 if (ChangeReg0) {
Evan Cheng03553bb2008-06-16 07:33:11 +0000288 MI->getOperand(0).setReg(Reg2);
Andrew Tricke3398282013-12-17 04:50:45 +0000289 MI->getOperand(0).setSubReg(SubReg2);
290 }
Chris Lattner10d63412006-05-04 17:52:23 +0000291 MI->getOperand(2).setReg(Reg1);
292 MI->getOperand(1).setReg(Reg2);
Andrew Tricke3398282013-12-17 04:50:45 +0000293 MI->getOperand(2).setSubReg(SubReg1);
294 MI->getOperand(1).setSubReg(SubReg2);
Chris Lattner60055892007-12-30 21:56:09 +0000295 MI->getOperand(2).setIsKill(Reg1IsKill);
296 MI->getOperand(1).setIsKill(Reg2IsKill);
Andrew Trickc416ba62010-12-24 04:28:06 +0000297
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000298 // Swap the mask around.
Chris Lattner5c463782007-12-30 20:49:49 +0000299 MI->getOperand(4).setImm((ME+1) & 31);
300 MI->getOperand(5).setImm((MB-1) & 31);
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000301 return MI;
302}
Chris Lattnerea79d9fd732006-03-05 23:49:55 +0000303
Hal Finkel6c32ff32014-03-25 19:26:43 +0000304bool PPCInstrInfo::findCommutedOpIndices(MachineInstr *MI, unsigned &SrcOpIdx1,
305 unsigned &SrcOpIdx2) const {
306 // For VSX A-Type FMA instructions, it is the first two operands that can be
307 // commuted, however, because the non-encoded tied input operand is listed
308 // first, the operands to swap are actually the second and third.
309
310 int AltOpc = PPC::getAltVSXFMAOpcode(MI->getOpcode());
311 if (AltOpc == -1)
312 return TargetInstrInfo::findCommutedOpIndices(MI, SrcOpIdx1, SrcOpIdx2);
313
314 SrcOpIdx1 = 2;
315 SrcOpIdx2 = 3;
316 return true;
317}
318
Andrew Trickc416ba62010-12-24 04:28:06 +0000319void PPCInstrInfo::insertNoop(MachineBasicBlock &MBB,
Chris Lattnerea79d9fd732006-03-05 23:49:55 +0000320 MachineBasicBlock::iterator MI) const {
Hal Finkelceb1f122013-12-12 00:19:11 +0000321 // This function is used for scheduling, and the nop wanted here is the type
322 // that terminates dispatch groups on the POWER cores.
Eric Christopher1dcea732014-06-12 21:48:52 +0000323 unsigned Directive = Subtarget.getDarwinDirective();
Hal Finkelceb1f122013-12-12 00:19:11 +0000324 unsigned Opcode;
325 switch (Directive) {
326 default: Opcode = PPC::NOP; break;
327 case PPC::DIR_PWR6: Opcode = PPC::NOP_GT_PWR6; break;
328 case PPC::DIR_PWR7: Opcode = PPC::NOP_GT_PWR7; break;
Will Schmidt970ff642014-06-26 13:36:19 +0000329 case PPC::DIR_PWR8: Opcode = PPC::NOP_GT_PWR7; break; /* FIXME: Update when P8 InstrScheduling model is ready */
Hal Finkelceb1f122013-12-12 00:19:11 +0000330 }
Chris Lattnera47294ed2006-10-13 21:21:17 +0000331
Hal Finkelceb1f122013-12-12 00:19:11 +0000332 DebugLoc DL;
333 BuildMI(MBB, MI, DL, get(Opcode));
334}
Chris Lattnera47294ed2006-10-13 21:21:17 +0000335
Joerg Sonnenberger7ee0f312014-08-08 19:13:23 +0000336/// getNoopForMachoTarget - Return the noop instruction to use for a noop.
337void PPCInstrInfo::getNoopForMachoTarget(MCInst &NopInst) const {
338 NopInst.setOpcode(PPC::NOP);
339}
340
Chris Lattnera47294ed2006-10-13 21:21:17 +0000341// Branch analysis.
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000342// Note: If the condition register is set to CTR or CTR8 then this is a
343// BDNZ (imm == 1) or BDZ (imm == 0) branch.
Chris Lattnera47294ed2006-10-13 21:21:17 +0000344bool PPCInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
345 MachineBasicBlock *&FBB,
Evan Cheng64dfcac2009-02-09 07:14:22 +0000346 SmallVectorImpl<MachineOperand> &Cond,
347 bool AllowModify) const {
Eric Christopher1dcea732014-06-12 21:48:52 +0000348 bool isPPC64 = Subtarget.isPPC64();
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000349
Chris Lattnera47294ed2006-10-13 21:21:17 +0000350 // If the block has no terminators, it just falls into the block after it.
351 MachineBasicBlock::iterator I = MBB.end();
Dale Johannesen4244d122010-04-02 01:38:09 +0000352 if (I == MBB.begin())
353 return false;
354 --I;
355 while (I->isDebugValue()) {
356 if (I == MBB.begin())
357 return false;
358 --I;
359 }
360 if (!isUnpredicatedTerminator(I))
Chris Lattnera47294ed2006-10-13 21:21:17 +0000361 return false;
362
363 // Get the last instruction in the block.
364 MachineInstr *LastInst = I;
Andrew Trickc416ba62010-12-24 04:28:06 +0000365
Chris Lattnera47294ed2006-10-13 21:21:17 +0000366 // If there is only one terminator instruction, process it.
Evan Cheng5514bbe2007-06-08 21:59:56 +0000367 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
Chris Lattnera47294ed2006-10-13 21:21:17 +0000368 if (LastInst->getOpcode() == PPC::B) {
Evan Cheng8f43afd2009-05-08 23:09:25 +0000369 if (!LastInst->getOperand(0).isMBB())
370 return true;
Chris Lattnera5bb3702007-12-30 23:10:15 +0000371 TBB = LastInst->getOperand(0).getMBB();
Chris Lattnera47294ed2006-10-13 21:21:17 +0000372 return false;
Chris Lattnere0263792006-11-17 22:14:47 +0000373 } else if (LastInst->getOpcode() == PPC::BCC) {
Evan Cheng8f43afd2009-05-08 23:09:25 +0000374 if (!LastInst->getOperand(2).isMBB())
375 return true;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000376 // Block ends with fall-through condbranch.
Chris Lattnera5bb3702007-12-30 23:10:15 +0000377 TBB = LastInst->getOperand(2).getMBB();
Chris Lattnera47294ed2006-10-13 21:21:17 +0000378 Cond.push_back(LastInst->getOperand(0));
379 Cond.push_back(LastInst->getOperand(1));
Chris Lattner23f22de2006-10-21 06:03:11 +0000380 return false;
Hal Finkel940ab932014-02-28 00:27:01 +0000381 } else if (LastInst->getOpcode() == PPC::BC) {
382 if (!LastInst->getOperand(1).isMBB())
383 return true;
384 // Block ends with fall-through condbranch.
385 TBB = LastInst->getOperand(1).getMBB();
386 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET));
387 Cond.push_back(LastInst->getOperand(0));
388 return false;
389 } else if (LastInst->getOpcode() == PPC::BCn) {
390 if (!LastInst->getOperand(1).isMBB())
391 return true;
392 // Block ends with fall-through condbranch.
393 TBB = LastInst->getOperand(1).getMBB();
394 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_UNSET));
395 Cond.push_back(LastInst->getOperand(0));
396 return false;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000397 } else if (LastInst->getOpcode() == PPC::BDNZ8 ||
398 LastInst->getOpcode() == PPC::BDNZ) {
399 if (!LastInst->getOperand(0).isMBB())
400 return true;
Hal Finkelc6b5deb2012-06-08 19:19:53 +0000401 if (DisableCTRLoopAnal)
Hal Finkel821e0012012-06-08 15:38:25 +0000402 return true;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000403 TBB = LastInst->getOperand(0).getMBB();
404 Cond.push_back(MachineOperand::CreateImm(1));
405 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
406 true));
407 return false;
408 } else if (LastInst->getOpcode() == PPC::BDZ8 ||
409 LastInst->getOpcode() == PPC::BDZ) {
410 if (!LastInst->getOperand(0).isMBB())
411 return true;
Hal Finkelc6b5deb2012-06-08 19:19:53 +0000412 if (DisableCTRLoopAnal)
Hal Finkel821e0012012-06-08 15:38:25 +0000413 return true;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000414 TBB = LastInst->getOperand(0).getMBB();
415 Cond.push_back(MachineOperand::CreateImm(0));
416 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
417 true));
418 return false;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000419 }
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000420
Chris Lattnera47294ed2006-10-13 21:21:17 +0000421 // Otherwise, don't know what this is.
422 return true;
423 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000424
Chris Lattnera47294ed2006-10-13 21:21:17 +0000425 // Get the instruction before it if it's a terminator.
426 MachineInstr *SecondLastInst = I;
427
428 // If there are three terminators, we don't know what sort of block this is.
429 if (SecondLastInst && I != MBB.begin() &&
Evan Cheng5514bbe2007-06-08 21:59:56 +0000430 isUnpredicatedTerminator(--I))
Chris Lattnera47294ed2006-10-13 21:21:17 +0000431 return true;
Andrew Trickc416ba62010-12-24 04:28:06 +0000432
Chris Lattnere0263792006-11-17 22:14:47 +0000433 // If the block ends with PPC::B and PPC:BCC, handle it.
Andrew Trickc416ba62010-12-24 04:28:06 +0000434 if (SecondLastInst->getOpcode() == PPC::BCC &&
Chris Lattnera47294ed2006-10-13 21:21:17 +0000435 LastInst->getOpcode() == PPC::B) {
Evan Cheng8f43afd2009-05-08 23:09:25 +0000436 if (!SecondLastInst->getOperand(2).isMBB() ||
437 !LastInst->getOperand(0).isMBB())
438 return true;
Chris Lattnera5bb3702007-12-30 23:10:15 +0000439 TBB = SecondLastInst->getOperand(2).getMBB();
Chris Lattnera47294ed2006-10-13 21:21:17 +0000440 Cond.push_back(SecondLastInst->getOperand(0));
441 Cond.push_back(SecondLastInst->getOperand(1));
Chris Lattnera5bb3702007-12-30 23:10:15 +0000442 FBB = LastInst->getOperand(0).getMBB();
Chris Lattnera47294ed2006-10-13 21:21:17 +0000443 return false;
Hal Finkel940ab932014-02-28 00:27:01 +0000444 } else if (SecondLastInst->getOpcode() == PPC::BC &&
445 LastInst->getOpcode() == PPC::B) {
446 if (!SecondLastInst->getOperand(1).isMBB() ||
447 !LastInst->getOperand(0).isMBB())
448 return true;
449 TBB = SecondLastInst->getOperand(1).getMBB();
450 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET));
451 Cond.push_back(SecondLastInst->getOperand(0));
452 FBB = LastInst->getOperand(0).getMBB();
453 return false;
454 } else if (SecondLastInst->getOpcode() == PPC::BCn &&
455 LastInst->getOpcode() == PPC::B) {
456 if (!SecondLastInst->getOperand(1).isMBB() ||
457 !LastInst->getOperand(0).isMBB())
458 return true;
459 TBB = SecondLastInst->getOperand(1).getMBB();
460 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_UNSET));
461 Cond.push_back(SecondLastInst->getOperand(0));
462 FBB = LastInst->getOperand(0).getMBB();
463 return false;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000464 } else if ((SecondLastInst->getOpcode() == PPC::BDNZ8 ||
465 SecondLastInst->getOpcode() == PPC::BDNZ) &&
466 LastInst->getOpcode() == PPC::B) {
467 if (!SecondLastInst->getOperand(0).isMBB() ||
468 !LastInst->getOperand(0).isMBB())
469 return true;
Hal Finkelc6b5deb2012-06-08 19:19:53 +0000470 if (DisableCTRLoopAnal)
Hal Finkel821e0012012-06-08 15:38:25 +0000471 return true;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000472 TBB = SecondLastInst->getOperand(0).getMBB();
473 Cond.push_back(MachineOperand::CreateImm(1));
474 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
475 true));
476 FBB = LastInst->getOperand(0).getMBB();
477 return false;
478 } else if ((SecondLastInst->getOpcode() == PPC::BDZ8 ||
479 SecondLastInst->getOpcode() == PPC::BDZ) &&
480 LastInst->getOpcode() == PPC::B) {
481 if (!SecondLastInst->getOperand(0).isMBB() ||
482 !LastInst->getOperand(0).isMBB())
483 return true;
Hal Finkelc6b5deb2012-06-08 19:19:53 +0000484 if (DisableCTRLoopAnal)
Hal Finkel821e0012012-06-08 15:38:25 +0000485 return true;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000486 TBB = SecondLastInst->getOperand(0).getMBB();
487 Cond.push_back(MachineOperand::CreateImm(0));
488 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
489 true));
490 FBB = LastInst->getOperand(0).getMBB();
491 return false;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000492 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000493
Dale Johannesenc6855462007-06-13 17:59:52 +0000494 // If the block ends with two PPC:Bs, handle it. The second one is not
495 // executed, so remove it.
Andrew Trickc416ba62010-12-24 04:28:06 +0000496 if (SecondLastInst->getOpcode() == PPC::B &&
Dale Johannesenc6855462007-06-13 17:59:52 +0000497 LastInst->getOpcode() == PPC::B) {
Evan Cheng8f43afd2009-05-08 23:09:25 +0000498 if (!SecondLastInst->getOperand(0).isMBB())
499 return true;
Chris Lattnera5bb3702007-12-30 23:10:15 +0000500 TBB = SecondLastInst->getOperand(0).getMBB();
Dale Johannesenc6855462007-06-13 17:59:52 +0000501 I = LastInst;
Evan Cheng64dfcac2009-02-09 07:14:22 +0000502 if (AllowModify)
503 I->eraseFromParent();
Dale Johannesenc6855462007-06-13 17:59:52 +0000504 return false;
505 }
506
Chris Lattnera47294ed2006-10-13 21:21:17 +0000507 // Otherwise, can't handle this.
508 return true;
509}
510
Evan Cheng99be49d2007-05-18 00:05:48 +0000511unsigned PPCInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
Chris Lattnera47294ed2006-10-13 21:21:17 +0000512 MachineBasicBlock::iterator I = MBB.end();
Evan Cheng99be49d2007-05-18 00:05:48 +0000513 if (I == MBB.begin()) return 0;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000514 --I;
Dale Johannesen4244d122010-04-02 01:38:09 +0000515 while (I->isDebugValue()) {
516 if (I == MBB.begin())
517 return 0;
518 --I;
519 }
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000520 if (I->getOpcode() != PPC::B && 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 0;
Andrew Trickc416ba62010-12-24 04:28:06 +0000525
Chris Lattnera47294ed2006-10-13 21:21:17 +0000526 // Remove the branch.
527 I->eraseFromParent();
Andrew Trickc416ba62010-12-24 04:28:06 +0000528
Chris Lattnera47294ed2006-10-13 21:21:17 +0000529 I = MBB.end();
530
Evan Cheng99be49d2007-05-18 00:05:48 +0000531 if (I == MBB.begin()) return 1;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000532 --I;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000533 if (I->getOpcode() != PPC::BCC &&
Hal Finkel940ab932014-02-28 00:27:01 +0000534 I->getOpcode() != PPC::BC && I->getOpcode() != PPC::BCn &&
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000535 I->getOpcode() != PPC::BDNZ8 && I->getOpcode() != PPC::BDNZ &&
536 I->getOpcode() != PPC::BDZ8 && I->getOpcode() != PPC::BDZ)
Evan Cheng99be49d2007-05-18 00:05:48 +0000537 return 1;
Andrew Trickc416ba62010-12-24 04:28:06 +0000538
Chris Lattnera47294ed2006-10-13 21:21:17 +0000539 // Remove the branch.
540 I->eraseFromParent();
Evan Cheng99be49d2007-05-18 00:05:48 +0000541 return 2;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000542}
543
Evan Cheng99be49d2007-05-18 00:05:48 +0000544unsigned
545PPCInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
546 MachineBasicBlock *FBB,
Stuart Hastings0125b642010-06-17 22:43:56 +0000547 const SmallVectorImpl<MachineOperand> &Cond,
548 DebugLoc DL) const {
Chris Lattnera61f0102006-10-17 18:06:55 +0000549 // Shouldn't be a fall through.
550 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Andrew Trickc416ba62010-12-24 04:28:06 +0000551 assert((Cond.size() == 2 || Cond.size() == 0) &&
Chris Lattner94e04442006-10-21 05:36:13 +0000552 "PPC branch conditions have two components!");
Andrew Trickc416ba62010-12-24 04:28:06 +0000553
Eric Christopher1dcea732014-06-12 21:48:52 +0000554 bool isPPC64 = Subtarget.isPPC64();
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000555
Chris Lattner94e04442006-10-21 05:36:13 +0000556 // One-way branch.
Craig Topper062a2ba2014-04-25 05:30:21 +0000557 if (!FBB) {
Chris Lattner94e04442006-10-21 05:36:13 +0000558 if (Cond.empty()) // Unconditional branch
Stuart Hastings0125b642010-06-17 22:43:56 +0000559 BuildMI(&MBB, DL, get(PPC::B)).addMBB(TBB);
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000560 else if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
561 BuildMI(&MBB, DL, get(Cond[0].getImm() ?
562 (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
563 (isPPC64 ? PPC::BDZ8 : PPC::BDZ))).addMBB(TBB);
Hal Finkel940ab932014-02-28 00:27:01 +0000564 else if (Cond[0].getImm() == PPC::PRED_BIT_SET)
565 BuildMI(&MBB, DL, get(PPC::BC)).addOperand(Cond[1]).addMBB(TBB);
566 else if (Cond[0].getImm() == PPC::PRED_BIT_UNSET)
567 BuildMI(&MBB, DL, get(PPC::BCn)).addOperand(Cond[1]).addMBB(TBB);
Chris Lattner94e04442006-10-21 05:36:13 +0000568 else // Conditional branch
Stuart Hastings0125b642010-06-17 22:43:56 +0000569 BuildMI(&MBB, DL, get(PPC::BCC))
Hal Finkel940ab932014-02-28 00:27:01 +0000570 .addImm(Cond[0].getImm()).addOperand(Cond[1]).addMBB(TBB);
Evan Cheng99be49d2007-05-18 00:05:48 +0000571 return 1;
Chris Lattnera61f0102006-10-17 18:06:55 +0000572 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000573
Chris Lattnerd8816602006-10-21 05:42:09 +0000574 // Two-way Conditional Branch.
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000575 if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
576 BuildMI(&MBB, DL, get(Cond[0].getImm() ?
577 (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
578 (isPPC64 ? PPC::BDZ8 : PPC::BDZ))).addMBB(TBB);
Hal Finkel940ab932014-02-28 00:27:01 +0000579 else if (Cond[0].getImm() == PPC::PRED_BIT_SET)
580 BuildMI(&MBB, DL, get(PPC::BC)).addOperand(Cond[1]).addMBB(TBB);
581 else if (Cond[0].getImm() == PPC::PRED_BIT_UNSET)
582 BuildMI(&MBB, DL, get(PPC::BCn)).addOperand(Cond[1]).addMBB(TBB);
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000583 else
584 BuildMI(&MBB, DL, get(PPC::BCC))
Hal Finkel940ab932014-02-28 00:27:01 +0000585 .addImm(Cond[0].getImm()).addOperand(Cond[1]).addMBB(TBB);
Stuart Hastings0125b642010-06-17 22:43:56 +0000586 BuildMI(&MBB, DL, get(PPC::B)).addMBB(FBB);
Evan Cheng99be49d2007-05-18 00:05:48 +0000587 return 2;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000588}
589
Hal Finkeled6a2852013-04-05 23:29:01 +0000590// Select analysis.
591bool PPCInstrInfo::canInsertSelect(const MachineBasicBlock &MBB,
592 const SmallVectorImpl<MachineOperand> &Cond,
593 unsigned TrueReg, unsigned FalseReg,
594 int &CondCycles, int &TrueCycles, int &FalseCycles) const {
Eric Christopher1dcea732014-06-12 21:48:52 +0000595 if (!Subtarget.hasISEL())
Hal Finkeled6a2852013-04-05 23:29:01 +0000596 return false;
597
598 if (Cond.size() != 2)
599 return false;
600
601 // If this is really a bdnz-like condition, then it cannot be turned into a
602 // select.
603 if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
604 return false;
605
606 // Check register classes.
607 const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
608 const TargetRegisterClass *RC =
609 RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg));
610 if (!RC)
611 return false;
612
613 // isel is for regular integer GPRs only.
614 if (!PPC::GPRCRegClass.hasSubClassEq(RC) &&
Hal Finkel8e8618a2013-07-15 20:22:58 +0000615 !PPC::GPRC_NOR0RegClass.hasSubClassEq(RC) &&
616 !PPC::G8RCRegClass.hasSubClassEq(RC) &&
617 !PPC::G8RC_NOX0RegClass.hasSubClassEq(RC))
Hal Finkeled6a2852013-04-05 23:29:01 +0000618 return false;
619
620 // FIXME: These numbers are for the A2, how well they work for other cores is
621 // an open question. On the A2, the isel instruction has a 2-cycle latency
622 // but single-cycle throughput. These numbers are used in combination with
623 // the MispredictPenalty setting from the active SchedMachineModel.
624 CondCycles = 1;
625 TrueCycles = 1;
626 FalseCycles = 1;
627
628 return true;
629}
630
631void PPCInstrInfo::insertSelect(MachineBasicBlock &MBB,
632 MachineBasicBlock::iterator MI, DebugLoc dl,
633 unsigned DestReg,
634 const SmallVectorImpl<MachineOperand> &Cond,
635 unsigned TrueReg, unsigned FalseReg) const {
636 assert(Cond.size() == 2 &&
637 "PPC branch conditions have two components!");
638
Eric Christopher1dcea732014-06-12 21:48:52 +0000639 assert(Subtarget.hasISEL() &&
Hal Finkeled6a2852013-04-05 23:29:01 +0000640 "Cannot insert select on target without ISEL support");
641
642 // Get the register classes.
643 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
644 const TargetRegisterClass *RC =
645 RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg));
646 assert(RC && "TrueReg and FalseReg must have overlapping register classes");
Hal Finkel8e8618a2013-07-15 20:22:58 +0000647
648 bool Is64Bit = PPC::G8RCRegClass.hasSubClassEq(RC) ||
649 PPC::G8RC_NOX0RegClass.hasSubClassEq(RC);
650 assert((Is64Bit ||
651 PPC::GPRCRegClass.hasSubClassEq(RC) ||
652 PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) &&
Hal Finkeled6a2852013-04-05 23:29:01 +0000653 "isel is for regular integer GPRs only");
654
Hal Finkel8e8618a2013-07-15 20:22:58 +0000655 unsigned OpCode = Is64Bit ? PPC::ISEL8 : PPC::ISEL;
Hal Finkeled6a2852013-04-05 23:29:01 +0000656 unsigned SelectPred = Cond[0].getImm();
657
658 unsigned SubIdx;
659 bool SwapOps;
660 switch (SelectPred) {
661 default: llvm_unreachable("invalid predicate for isel");
662 case PPC::PRED_EQ: SubIdx = PPC::sub_eq; SwapOps = false; break;
663 case PPC::PRED_NE: SubIdx = PPC::sub_eq; SwapOps = true; break;
664 case PPC::PRED_LT: SubIdx = PPC::sub_lt; SwapOps = false; break;
665 case PPC::PRED_GE: SubIdx = PPC::sub_lt; SwapOps = true; break;
666 case PPC::PRED_GT: SubIdx = PPC::sub_gt; SwapOps = false; break;
667 case PPC::PRED_LE: SubIdx = PPC::sub_gt; SwapOps = true; break;
668 case PPC::PRED_UN: SubIdx = PPC::sub_un; SwapOps = false; break;
669 case PPC::PRED_NU: SubIdx = PPC::sub_un; SwapOps = true; break;
Hal Finkel940ab932014-02-28 00:27:01 +0000670 case PPC::PRED_BIT_SET: SubIdx = 0; SwapOps = false; break;
671 case PPC::PRED_BIT_UNSET: SubIdx = 0; SwapOps = true; break;
Hal Finkeled6a2852013-04-05 23:29:01 +0000672 }
673
674 unsigned FirstReg = SwapOps ? FalseReg : TrueReg,
675 SecondReg = SwapOps ? TrueReg : FalseReg;
676
677 // The first input register of isel cannot be r0. If it is a member
678 // of a register class that can be r0, then copy it first (the
679 // register allocator should eliminate the copy).
680 if (MRI.getRegClass(FirstReg)->contains(PPC::R0) ||
681 MRI.getRegClass(FirstReg)->contains(PPC::X0)) {
682 const TargetRegisterClass *FirstRC =
683 MRI.getRegClass(FirstReg)->contains(PPC::X0) ?
684 &PPC::G8RC_NOX0RegClass : &PPC::GPRC_NOR0RegClass;
685 unsigned OldFirstReg = FirstReg;
686 FirstReg = MRI.createVirtualRegister(FirstRC);
687 BuildMI(MBB, MI, dl, get(TargetOpcode::COPY), FirstReg)
688 .addReg(OldFirstReg);
689 }
690
691 BuildMI(MBB, MI, dl, get(OpCode), DestReg)
692 .addReg(FirstReg).addReg(SecondReg)
693 .addReg(Cond[1].getReg(), 0, SubIdx);
694}
695
Jakob Stoklund Olesen0d611972010-07-11 07:31:00 +0000696void PPCInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
697 MachineBasicBlock::iterator I, DebugLoc DL,
698 unsigned DestReg, unsigned SrcReg,
699 bool KillSrc) const {
Hal Finkel27774d92014-03-13 07:58:58 +0000700 // We can end up with self copies and similar things as a result of VSX copy
Hal Finkel9dcb3582014-03-27 22:46:28 +0000701 // legalization. Promote them here.
Hal Finkel27774d92014-03-13 07:58:58 +0000702 const TargetRegisterInfo *TRI = &getRegisterInfo();
703 if (PPC::F8RCRegClass.contains(DestReg) &&
704 PPC::VSLRCRegClass.contains(SrcReg)) {
705 unsigned SuperReg =
706 TRI->getMatchingSuperReg(DestReg, PPC::sub_64, &PPC::VSRCRegClass);
707
Hal Finkel9dcb3582014-03-27 22:46:28 +0000708 if (VSXSelfCopyCrash && SrcReg == SuperReg)
709 llvm_unreachable("nop VSX copy");
Hal Finkel27774d92014-03-13 07:58:58 +0000710
711 DestReg = SuperReg;
712 } else if (PPC::VRRCRegClass.contains(DestReg) &&
713 PPC::VSHRCRegClass.contains(SrcReg)) {
714 unsigned SuperReg =
715 TRI->getMatchingSuperReg(DestReg, PPC::sub_128, &PPC::VSRCRegClass);
716
Hal Finkel9dcb3582014-03-27 22:46:28 +0000717 if (VSXSelfCopyCrash && SrcReg == SuperReg)
718 llvm_unreachable("nop VSX copy");
Hal Finkel27774d92014-03-13 07:58:58 +0000719
720 DestReg = SuperReg;
721 } else if (PPC::F8RCRegClass.contains(SrcReg) &&
722 PPC::VSLRCRegClass.contains(DestReg)) {
723 unsigned SuperReg =
724 TRI->getMatchingSuperReg(SrcReg, PPC::sub_64, &PPC::VSRCRegClass);
725
Hal Finkel9dcb3582014-03-27 22:46:28 +0000726 if (VSXSelfCopyCrash && DestReg == SuperReg)
727 llvm_unreachable("nop VSX copy");
Hal Finkel27774d92014-03-13 07:58:58 +0000728
729 SrcReg = SuperReg;
730 } else if (PPC::VRRCRegClass.contains(SrcReg) &&
731 PPC::VSHRCRegClass.contains(DestReg)) {
732 unsigned SuperReg =
733 TRI->getMatchingSuperReg(SrcReg, PPC::sub_128, &PPC::VSRCRegClass);
734
Hal Finkel9dcb3582014-03-27 22:46:28 +0000735 if (VSXSelfCopyCrash && DestReg == SuperReg)
736 llvm_unreachable("nop VSX copy");
Hal Finkel27774d92014-03-13 07:58:58 +0000737
738 SrcReg = SuperReg;
739 }
740
Jakob Stoklund Olesen0d611972010-07-11 07:31:00 +0000741 unsigned Opc;
742 if (PPC::GPRCRegClass.contains(DestReg, SrcReg))
743 Opc = PPC::OR;
744 else if (PPC::G8RCRegClass.contains(DestReg, SrcReg))
745 Opc = PPC::OR8;
746 else if (PPC::F4RCRegClass.contains(DestReg, SrcReg))
747 Opc = PPC::FMR;
748 else if (PPC::CRRCRegClass.contains(DestReg, SrcReg))
749 Opc = PPC::MCRF;
750 else if (PPC::VRRCRegClass.contains(DestReg, SrcReg))
751 Opc = PPC::VOR;
Hal Finkel27774d92014-03-13 07:58:58 +0000752 else if (PPC::VSRCRegClass.contains(DestReg, SrcReg))
Hal Finkelbbad2332014-03-24 09:36:36 +0000753 // There are two different ways this can be done:
Hal Finkel27774d92014-03-13 07:58:58 +0000754 // 1. xxlor : This has lower latency (on the P7), 2 cycles, but can only
755 // issue in VSU pipeline 0.
756 // 2. xmovdp/xmovsp: This has higher latency (on the P7), 6 cycles, but
757 // can go to either pipeline.
Hal Finkelbbad2332014-03-24 09:36:36 +0000758 // We'll always use xxlor here, because in practically all cases where
759 // copies are generated, they are close enough to some use that the
760 // lower-latency form is preferable.
Hal Finkel27774d92014-03-13 07:58:58 +0000761 Opc = PPC::XXLOR;
Hal Finkel19be5062014-03-29 05:29:01 +0000762 else if (PPC::VSFRCRegClass.contains(DestReg, SrcReg))
763 Opc = PPC::XXLORf;
Jakob Stoklund Olesen0d611972010-07-11 07:31:00 +0000764 else if (PPC::CRBITRCRegClass.contains(DestReg, SrcReg))
765 Opc = PPC::CROR;
766 else
767 llvm_unreachable("Impossible reg-to-reg copy");
Owen Anderson7a73ae92007-12-31 06:32:00 +0000768
Evan Cheng6cc775f2011-06-28 19:10:37 +0000769 const MCInstrDesc &MCID = get(Opc);
770 if (MCID.getNumOperands() == 3)
771 BuildMI(MBB, I, DL, MCID, DestReg)
Jakob Stoklund Olesen0d611972010-07-11 07:31:00 +0000772 .addReg(SrcReg).addReg(SrcReg, getKillRegState(KillSrc));
773 else
Evan Cheng6cc775f2011-06-28 19:10:37 +0000774 BuildMI(MBB, I, DL, MCID, DestReg).addReg(SrcReg, getKillRegState(KillSrc));
Owen Anderson7a73ae92007-12-31 06:32:00 +0000775}
776
Hal Finkel8f6834d2011-12-05 17:55:17 +0000777// This function returns true if a CR spill is necessary and false otherwise.
Bill Wendlingc6c48fc2008-03-10 22:49:16 +0000778bool
Dan Gohman3b460302008-07-07 23:14:23 +0000779PPCInstrInfo::StoreRegToStackSlot(MachineFunction &MF,
780 unsigned SrcReg, bool isKill,
Bill Wendlingc6c48fc2008-03-10 22:49:16 +0000781 int FrameIdx,
782 const TargetRegisterClass *RC,
Hal Finkelfcc51d42013-03-17 04:43:44 +0000783 SmallVectorImpl<MachineInstr*> &NewMIs,
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000784 bool &NonRI, bool &SpillsVRS) const{
Hal Finkel37714b82013-03-27 21:21:15 +0000785 // Note: If additional store instructions are added here,
786 // update isStoreToStackSlot.
787
Chris Lattner6f306d72010-04-02 20:16:16 +0000788 DebugLoc DL;
Hal Finkel4e703bc2014-01-28 05:32:58 +0000789 if (PPC::GPRCRegClass.hasSubClassEq(RC) ||
790 PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) {
Hal Finkel794e05b2013-03-23 17:14:27 +0000791 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
792 .addReg(SrcReg,
793 getKillRegState(isKill)),
794 FrameIdx));
Hal Finkel4e703bc2014-01-28 05:32:58 +0000795 } else if (PPC::G8RCRegClass.hasSubClassEq(RC) ||
796 PPC::G8RC_NOX0RegClass.hasSubClassEq(RC)) {
Hal Finkel794e05b2013-03-23 17:14:27 +0000797 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STD))
798 .addReg(SrcReg,
799 getKillRegState(isKill)),
800 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000801 } else if (PPC::F8RCRegClass.hasSubClassEq(RC)) {
Dale Johannesen6b8c76a2009-02-12 23:08:38 +0000802 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFD))
Bill Wendlingf7b83c72009-05-13 21:33:08 +0000803 .addReg(SrcReg,
804 getKillRegState(isKill)),
805 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000806 } else if (PPC::F4RCRegClass.hasSubClassEq(RC)) {
Dale Johannesen6b8c76a2009-02-12 23:08:38 +0000807 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFS))
Bill Wendlingf7b83c72009-05-13 21:33:08 +0000808 .addReg(SrcReg,
809 getKillRegState(isKill)),
810 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000811 } else if (PPC::CRRCRegClass.hasSubClassEq(RC)) {
Hal Finkele154c8f2013-03-12 14:12:16 +0000812 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CR))
813 .addReg(SrcReg,
814 getKillRegState(isKill)),
815 FrameIdx));
816 return true;
Craig Topperabadc662012-04-20 06:31:50 +0000817 } else if (PPC::CRBITRCRegClass.hasSubClassEq(RC)) {
Hal Finkel940ab932014-02-28 00:27:01 +0000818 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CRBIT))
819 .addReg(SrcReg,
820 getKillRegState(isKill)),
821 FrameIdx));
822 return true;
Craig Topperabadc662012-04-20 06:31:50 +0000823 } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) {
Hal Finkelfcc51d42013-03-17 04:43:44 +0000824 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STVX))
825 .addReg(SrcReg,
826 getKillRegState(isKill)),
827 FrameIdx));
828 NonRI = true;
Hal Finkel27774d92014-03-13 07:58:58 +0000829 } else if (PPC::VSRCRegClass.hasSubClassEq(RC)) {
830 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STXVD2X))
831 .addReg(SrcReg,
832 getKillRegState(isKill)),
833 FrameIdx));
834 NonRI = true;
Hal Finkel19be5062014-03-29 05:29:01 +0000835 } else if (PPC::VSFRCRegClass.hasSubClassEq(RC)) {
836 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STXSDX))
837 .addReg(SrcReg,
838 getKillRegState(isKill)),
839 FrameIdx));
840 NonRI = true;
Hal Finkela1431df2013-03-21 19:03:21 +0000841 } else if (PPC::VRSAVERCRegClass.hasSubClassEq(RC)) {
Eric Christopher1dcea732014-06-12 21:48:52 +0000842 assert(Subtarget.isDarwin() &&
Hal Finkela7b06302013-03-27 00:02:20 +0000843 "VRSAVE only needs spill/restore on Darwin");
Hal Finkela1431df2013-03-21 19:03:21 +0000844 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_VRSAVE))
845 .addReg(SrcReg,
846 getKillRegState(isKill)),
847 FrameIdx));
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000848 SpillsVRS = true;
Owen Andersoneee14602008-01-01 21:11:32 +0000849 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000850 llvm_unreachable("Unknown regclass!");
Owen Andersoneee14602008-01-01 21:11:32 +0000851 }
Bill Wendling632ea652008-03-03 22:19:16 +0000852
853 return false;
Owen Andersoneee14602008-01-01 21:11:32 +0000854}
855
856void
857PPCInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
Bill Wendling632ea652008-03-03 22:19:16 +0000858 MachineBasicBlock::iterator MI,
859 unsigned SrcReg, bool isKill, int FrameIdx,
Evan Chengefb126a2010-05-06 19:06:44 +0000860 const TargetRegisterClass *RC,
861 const TargetRegisterInfo *TRI) const {
Dan Gohman3b460302008-07-07 23:14:23 +0000862 MachineFunction &MF = *MBB.getParent();
Owen Andersoneee14602008-01-01 21:11:32 +0000863 SmallVector<MachineInstr*, 4> NewMIs;
Bill Wendling632ea652008-03-03 22:19:16 +0000864
Hal Finkelbb420f12013-03-15 05:06:04 +0000865 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
866 FuncInfo->setHasSpills();
867
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000868 bool NonRI = false, SpillsVRS = false;
869 if (StoreRegToStackSlot(MF, SrcReg, isKill, FrameIdx, RC, NewMIs,
870 NonRI, SpillsVRS))
Bill Wendling632ea652008-03-03 22:19:16 +0000871 FuncInfo->setSpillsCR();
Bill Wendling632ea652008-03-03 22:19:16 +0000872
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000873 if (SpillsVRS)
874 FuncInfo->setSpillsVRSAVE();
875
Hal Finkelfcc51d42013-03-17 04:43:44 +0000876 if (NonRI)
877 FuncInfo->setHasNonRISpills();
878
Owen Andersoneee14602008-01-01 21:11:32 +0000879 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
880 MBB.insert(MI, NewMIs[i]);
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +0000881
882 const MachineFrameInfo &MFI = *MF.getFrameInfo();
883 MachineMemOperand *MMO =
Jay Foad465101b2011-11-15 07:34:52 +0000884 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx),
Chris Lattnere3d864b2010-09-21 04:39:43 +0000885 MachineMemOperand::MOStore,
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +0000886 MFI.getObjectSize(FrameIdx),
887 MFI.getObjectAlignment(FrameIdx));
888 NewMIs.back()->addMemOperand(MF, MMO);
Owen Andersoneee14602008-01-01 21:11:32 +0000889}
890
Hal Finkelbde7f8f2011-12-06 20:55:36 +0000891bool
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000892PPCInstrInfo::LoadRegFromStackSlot(MachineFunction &MF, DebugLoc DL,
Dan Gohman3b460302008-07-07 23:14:23 +0000893 unsigned DestReg, int FrameIdx,
Bill Wendlingc6c48fc2008-03-10 22:49:16 +0000894 const TargetRegisterClass *RC,
Hal Finkelfcc51d42013-03-17 04:43:44 +0000895 SmallVectorImpl<MachineInstr*> &NewMIs,
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000896 bool &NonRI, bool &SpillsVRS) const{
Hal Finkel37714b82013-03-27 21:21:15 +0000897 // Note: If additional load instructions are added here,
898 // update isLoadFromStackSlot.
899
Hal Finkel4e703bc2014-01-28 05:32:58 +0000900 if (PPC::GPRCRegClass.hasSubClassEq(RC) ||
901 PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) {
Hal Finkel5791f512013-03-27 19:10:40 +0000902 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ),
903 DestReg), FrameIdx));
Hal Finkel4e703bc2014-01-28 05:32:58 +0000904 } else if (PPC::G8RCRegClass.hasSubClassEq(RC) ||
905 PPC::G8RC_NOX0RegClass.hasSubClassEq(RC)) {
Hal Finkel5791f512013-03-27 19:10:40 +0000906 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LD), DestReg),
907 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000908 } else if (PPC::F8RCRegClass.hasSubClassEq(RC)) {
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000909 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFD), DestReg),
Owen Andersoneee14602008-01-01 21:11:32 +0000910 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000911 } else if (PPC::F4RCRegClass.hasSubClassEq(RC)) {
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000912 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFS), DestReg),
Owen Andersoneee14602008-01-01 21:11:32 +0000913 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000914 } else if (PPC::CRRCRegClass.hasSubClassEq(RC)) {
Hal Finkele154c8f2013-03-12 14:12:16 +0000915 NewMIs.push_back(addFrameReference(BuildMI(MF, DL,
916 get(PPC::RESTORE_CR), DestReg),
917 FrameIdx));
918 return true;
Craig Topperabadc662012-04-20 06:31:50 +0000919 } else if (PPC::CRBITRCRegClass.hasSubClassEq(RC)) {
Hal Finkel940ab932014-02-28 00:27:01 +0000920 NewMIs.push_back(addFrameReference(BuildMI(MF, DL,
921 get(PPC::RESTORE_CRBIT), DestReg),
922 FrameIdx));
923 return true;
Craig Topperabadc662012-04-20 06:31:50 +0000924 } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) {
Hal Finkelfcc51d42013-03-17 04:43:44 +0000925 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LVX), DestReg),
926 FrameIdx));
927 NonRI = true;
Hal Finkel27774d92014-03-13 07:58:58 +0000928 } else if (PPC::VSRCRegClass.hasSubClassEq(RC)) {
929 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LXVD2X), DestReg),
930 FrameIdx));
931 NonRI = true;
Hal Finkel19be5062014-03-29 05:29:01 +0000932 } else if (PPC::VSFRCRegClass.hasSubClassEq(RC)) {
933 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LXSDX), DestReg),
934 FrameIdx));
935 NonRI = true;
Hal Finkela1431df2013-03-21 19:03:21 +0000936 } else if (PPC::VRSAVERCRegClass.hasSubClassEq(RC)) {
Eric Christopher1dcea732014-06-12 21:48:52 +0000937 assert(Subtarget.isDarwin() &&
Hal Finkela7b06302013-03-27 00:02:20 +0000938 "VRSAVE only needs spill/restore on Darwin");
Hal Finkela1431df2013-03-21 19:03:21 +0000939 NewMIs.push_back(addFrameReference(BuildMI(MF, DL,
940 get(PPC::RESTORE_VRSAVE),
941 DestReg),
942 FrameIdx));
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000943 SpillsVRS = true;
Owen Andersoneee14602008-01-01 21:11:32 +0000944 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000945 llvm_unreachable("Unknown regclass!");
Owen Andersoneee14602008-01-01 21:11:32 +0000946 }
Hal Finkelbde7f8f2011-12-06 20:55:36 +0000947
948 return false;
Owen Andersoneee14602008-01-01 21:11:32 +0000949}
950
951void
952PPCInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
Bill Wendling632ea652008-03-03 22:19:16 +0000953 MachineBasicBlock::iterator MI,
954 unsigned DestReg, int FrameIdx,
Evan Chengefb126a2010-05-06 19:06:44 +0000955 const TargetRegisterClass *RC,
956 const TargetRegisterInfo *TRI) const {
Dan Gohman3b460302008-07-07 23:14:23 +0000957 MachineFunction &MF = *MBB.getParent();
Owen Andersoneee14602008-01-01 21:11:32 +0000958 SmallVector<MachineInstr*, 4> NewMIs;
Chris Lattner6f306d72010-04-02 20:16:16 +0000959 DebugLoc DL;
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000960 if (MI != MBB.end()) DL = MI->getDebugLoc();
Hal Finkelfcc51d42013-03-17 04:43:44 +0000961
962 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
963 FuncInfo->setHasSpills();
964
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000965 bool NonRI = false, SpillsVRS = false;
966 if (LoadRegFromStackSlot(MF, DL, DestReg, FrameIdx, RC, NewMIs,
967 NonRI, SpillsVRS))
Hal Finkelbde7f8f2011-12-06 20:55:36 +0000968 FuncInfo->setSpillsCR();
Hal Finkelfcc51d42013-03-17 04:43:44 +0000969
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000970 if (SpillsVRS)
971 FuncInfo->setSpillsVRSAVE();
972
Hal Finkelfcc51d42013-03-17 04:43:44 +0000973 if (NonRI)
974 FuncInfo->setHasNonRISpills();
975
Owen Andersoneee14602008-01-01 21:11:32 +0000976 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
977 MBB.insert(MI, NewMIs[i]);
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +0000978
979 const MachineFrameInfo &MFI = *MF.getFrameInfo();
980 MachineMemOperand *MMO =
Jay Foad465101b2011-11-15 07:34:52 +0000981 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx),
Chris Lattnere3d864b2010-09-21 04:39:43 +0000982 MachineMemOperand::MOLoad,
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +0000983 MFI.getObjectSize(FrameIdx),
984 MFI.getObjectAlignment(FrameIdx));
985 NewMIs.back()->addMemOperand(MF, MMO);
Owen Andersoneee14602008-01-01 21:11:32 +0000986}
987
Chris Lattnera47294ed2006-10-13 21:21:17 +0000988bool PPCInstrInfo::
Owen Anderson4f6bf042008-08-14 22:49:33 +0000989ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
Chris Lattner23f22de2006-10-21 06:03:11 +0000990 assert(Cond.size() == 2 && "Invalid PPC branch opcode!");
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000991 if (Cond[1].getReg() == PPC::CTR8 || Cond[1].getReg() == PPC::CTR)
992 Cond[0].setImm(Cond[0].getImm() == 0 ? 1 : 0);
993 else
994 // Leave the CR# the same, but invert the condition.
995 Cond[0].setImm(PPC::InvertPredicate((PPC::Predicate)Cond[0].getImm()));
Chris Lattner23f22de2006-10-21 06:03:11 +0000996 return false;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000997}
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +0000998
Hal Finkeld61d4f82013-04-06 19:30:30 +0000999bool PPCInstrInfo::FoldImmediate(MachineInstr *UseMI, MachineInstr *DefMI,
1000 unsigned Reg, MachineRegisterInfo *MRI) const {
1001 // For some instructions, it is legal to fold ZERO into the RA register field.
1002 // A zero immediate should always be loaded with a single li.
1003 unsigned DefOpc = DefMI->getOpcode();
1004 if (DefOpc != PPC::LI && DefOpc != PPC::LI8)
1005 return false;
1006 if (!DefMI->getOperand(1).isImm())
1007 return false;
1008 if (DefMI->getOperand(1).getImm() != 0)
1009 return false;
1010
1011 // Note that we cannot here invert the arguments of an isel in order to fold
1012 // a ZERO into what is presented as the second argument. All we have here
1013 // is the condition bit, and that might come from a CR-logical bit operation.
1014
1015 const MCInstrDesc &UseMCID = UseMI->getDesc();
1016
1017 // Only fold into real machine instructions.
1018 if (UseMCID.isPseudo())
1019 return false;
1020
1021 unsigned UseIdx;
1022 for (UseIdx = 0; UseIdx < UseMI->getNumOperands(); ++UseIdx)
1023 if (UseMI->getOperand(UseIdx).isReg() &&
1024 UseMI->getOperand(UseIdx).getReg() == Reg)
1025 break;
1026
1027 assert(UseIdx < UseMI->getNumOperands() && "Cannot find Reg in UseMI");
1028 assert(UseIdx < UseMCID.getNumOperands() && "No operand description for Reg");
1029
1030 const MCOperandInfo *UseInfo = &UseMCID.OpInfo[UseIdx];
1031
1032 // We can fold the zero if this register requires a GPRC_NOR0/G8RC_NOX0
1033 // register (which might also be specified as a pointer class kind).
1034 if (UseInfo->isLookupPtrRegClass()) {
1035 if (UseInfo->RegClass /* Kind */ != 1)
1036 return false;
1037 } else {
1038 if (UseInfo->RegClass != PPC::GPRC_NOR0RegClassID &&
1039 UseInfo->RegClass != PPC::G8RC_NOX0RegClassID)
1040 return false;
1041 }
1042
1043 // Make sure this is not tied to an output register (or otherwise
1044 // constrained). This is true for ST?UX registers, for example, which
1045 // are tied to their output registers.
1046 if (UseInfo->Constraints != 0)
1047 return false;
1048
1049 unsigned ZeroReg;
1050 if (UseInfo->isLookupPtrRegClass()) {
Eric Christopher1dcea732014-06-12 21:48:52 +00001051 bool isPPC64 = Subtarget.isPPC64();
Hal Finkeld61d4f82013-04-06 19:30:30 +00001052 ZeroReg = isPPC64 ? PPC::ZERO8 : PPC::ZERO;
1053 } else {
1054 ZeroReg = UseInfo->RegClass == PPC::G8RC_NOX0RegClassID ?
1055 PPC::ZERO8 : PPC::ZERO;
1056 }
1057
1058 bool DeleteDef = MRI->hasOneNonDBGUse(Reg);
1059 UseMI->getOperand(UseIdx).setReg(ZeroReg);
1060
1061 if (DeleteDef)
1062 DefMI->eraseFromParent();
1063
1064 return true;
1065}
1066
Hal Finkel30ae2292013-04-10 18:30:16 +00001067static bool MBBDefinesCTR(MachineBasicBlock &MBB) {
1068 for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end();
1069 I != IE; ++I)
1070 if (I->definesRegister(PPC::CTR) || I->definesRegister(PPC::CTR8))
1071 return true;
1072 return false;
1073}
1074
1075// We should make sure that, if we're going to predicate both sides of a
1076// condition (a diamond), that both sides don't define the counter register. We
1077// can predicate counter-decrement-based branches, but while that predicates
1078// the branching, it does not predicate the counter decrement. If we tried to
1079// merge the triangle into one predicated block, we'd decrement the counter
1080// twice.
1081bool PPCInstrInfo::isProfitableToIfCvt(MachineBasicBlock &TMBB,
1082 unsigned NumT, unsigned ExtraT,
1083 MachineBasicBlock &FMBB,
1084 unsigned NumF, unsigned ExtraF,
1085 const BranchProbability &Probability) const {
1086 return !(MBBDefinesCTR(TMBB) && MBBDefinesCTR(FMBB));
1087}
1088
1089
Hal Finkel5711eca2013-04-09 22:58:37 +00001090bool PPCInstrInfo::isPredicated(const MachineInstr *MI) const {
Hal Finkelf29285a2013-04-11 01:23:34 +00001091 // The predicated branches are identified by their type, not really by the
1092 // explicit presence of a predicate. Furthermore, some of them can be
1093 // predicated more than once. Because if conversion won't try to predicate
1094 // any instruction which already claims to be predicated (by returning true
1095 // here), always return false. In doing so, we let isPredicable() be the
1096 // final word on whether not the instruction can be (further) predicated.
1097
1098 return false;
Hal Finkel5711eca2013-04-09 22:58:37 +00001099}
1100
1101bool PPCInstrInfo::isUnpredicatedTerminator(const MachineInstr *MI) const {
1102 if (!MI->isTerminator())
1103 return false;
1104
1105 // Conditional branch is a special case.
1106 if (MI->isBranch() && !MI->isBarrier())
1107 return true;
1108
1109 return !isPredicated(MI);
1110}
1111
1112bool PPCInstrInfo::PredicateInstruction(
1113 MachineInstr *MI,
1114 const SmallVectorImpl<MachineOperand> &Pred) const {
1115 unsigned OpC = MI->getOpcode();
1116 if (OpC == PPC::BLR) {
1117 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) {
Eric Christopher1dcea732014-06-12 21:48:52 +00001118 bool isPPC64 = Subtarget.isPPC64();
Hal Finkel5711eca2013-04-09 22:58:37 +00001119 MI->setDesc(get(Pred[0].getImm() ?
1120 (isPPC64 ? PPC::BDNZLR8 : PPC::BDNZLR) :
1121 (isPPC64 ? PPC::BDZLR8 : PPC::BDZLR)));
Hal Finkel940ab932014-02-28 00:27:01 +00001122 } else if (Pred[0].getImm() == PPC::PRED_BIT_SET) {
Hal Finkel5711eca2013-04-09 22:58:37 +00001123 MI->setDesc(get(PPC::BCLR));
1124 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
Hal Finkel940ab932014-02-28 00:27:01 +00001125 .addReg(Pred[1].getReg());
1126 } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) {
1127 MI->setDesc(get(PPC::BCLRn));
1128 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1129 .addReg(Pred[1].getReg());
1130 } else {
1131 MI->setDesc(get(PPC::BCCLR));
1132 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
Hal Finkel5711eca2013-04-09 22:58:37 +00001133 .addImm(Pred[0].getImm())
1134 .addReg(Pred[1].getReg());
1135 }
1136
1137 return true;
1138 } else if (OpC == PPC::B) {
1139 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) {
Eric Christopher1dcea732014-06-12 21:48:52 +00001140 bool isPPC64 = Subtarget.isPPC64();
Hal Finkel5711eca2013-04-09 22:58:37 +00001141 MI->setDesc(get(Pred[0].getImm() ?
1142 (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
1143 (isPPC64 ? PPC::BDZ8 : PPC::BDZ)));
Hal Finkel940ab932014-02-28 00:27:01 +00001144 } else if (Pred[0].getImm() == PPC::PRED_BIT_SET) {
1145 MachineBasicBlock *MBB = MI->getOperand(0).getMBB();
1146 MI->RemoveOperand(0);
1147
1148 MI->setDesc(get(PPC::BC));
1149 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1150 .addReg(Pred[1].getReg())
1151 .addMBB(MBB);
1152 } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) {
1153 MachineBasicBlock *MBB = MI->getOperand(0).getMBB();
1154 MI->RemoveOperand(0);
1155
1156 MI->setDesc(get(PPC::BCn));
1157 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1158 .addReg(Pred[1].getReg())
1159 .addMBB(MBB);
Hal Finkel5711eca2013-04-09 22:58:37 +00001160 } else {
1161 MachineBasicBlock *MBB = MI->getOperand(0).getMBB();
1162 MI->RemoveOperand(0);
1163
1164 MI->setDesc(get(PPC::BCC));
1165 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1166 .addImm(Pred[0].getImm())
1167 .addReg(Pred[1].getReg())
1168 .addMBB(MBB);
1169 }
1170
1171 return true;
Hal Finkel500b0042013-04-10 06:42:34 +00001172 } else if (OpC == PPC::BCTR || OpC == PPC::BCTR8 ||
1173 OpC == PPC::BCTRL || OpC == PPC::BCTRL8) {
1174 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR)
1175 llvm_unreachable("Cannot predicate bctr[l] on the ctr register");
1176
1177 bool setLR = OpC == PPC::BCTRL || OpC == PPC::BCTRL8;
Eric Christopher1dcea732014-06-12 21:48:52 +00001178 bool isPPC64 = Subtarget.isPPC64();
Hal Finkel940ab932014-02-28 00:27:01 +00001179
1180 if (Pred[0].getImm() == PPC::PRED_BIT_SET) {
1181 MI->setDesc(get(isPPC64 ? (setLR ? PPC::BCCTRL8 : PPC::BCCTR8) :
1182 (setLR ? PPC::BCCTRL : PPC::BCCTR)));
1183 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1184 .addReg(Pred[1].getReg());
1185 return true;
1186 } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) {
1187 MI->setDesc(get(isPPC64 ? (setLR ? PPC::BCCTRL8n : PPC::BCCTR8n) :
1188 (setLR ? PPC::BCCTRLn : PPC::BCCTRn)));
1189 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1190 .addReg(Pred[1].getReg());
1191 return true;
1192 }
1193
1194 MI->setDesc(get(isPPC64 ? (setLR ? PPC::BCCCTRL8 : PPC::BCCCTR8) :
1195 (setLR ? PPC::BCCCTRL : PPC::BCCCTR)));
Hal Finkel500b0042013-04-10 06:42:34 +00001196 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1197 .addImm(Pred[0].getImm())
1198 .addReg(Pred[1].getReg());
1199 return true;
Hal Finkel5711eca2013-04-09 22:58:37 +00001200 }
1201
1202 return false;
1203}
1204
1205bool PPCInstrInfo::SubsumesPredicate(
1206 const SmallVectorImpl<MachineOperand> &Pred1,
1207 const SmallVectorImpl<MachineOperand> &Pred2) const {
1208 assert(Pred1.size() == 2 && "Invalid PPC first predicate");
1209 assert(Pred2.size() == 2 && "Invalid PPC second predicate");
1210
1211 if (Pred1[1].getReg() == PPC::CTR8 || Pred1[1].getReg() == PPC::CTR)
1212 return false;
1213 if (Pred2[1].getReg() == PPC::CTR8 || Pred2[1].getReg() == PPC::CTR)
1214 return false;
1215
Hal Finkel94a6f382013-12-11 23:12:25 +00001216 // P1 can only subsume P2 if they test the same condition register.
1217 if (Pred1[1].getReg() != Pred2[1].getReg())
1218 return false;
1219
Hal Finkel5711eca2013-04-09 22:58:37 +00001220 PPC::Predicate P1 = (PPC::Predicate) Pred1[0].getImm();
1221 PPC::Predicate P2 = (PPC::Predicate) Pred2[0].getImm();
1222
1223 if (P1 == P2)
1224 return true;
1225
1226 // Does P1 subsume P2, e.g. GE subsumes GT.
1227 if (P1 == PPC::PRED_LE &&
1228 (P2 == PPC::PRED_LT || P2 == PPC::PRED_EQ))
1229 return true;
1230 if (P1 == PPC::PRED_GE &&
1231 (P2 == PPC::PRED_GT || P2 == PPC::PRED_EQ))
1232 return true;
1233
1234 return false;
1235}
1236
1237bool PPCInstrInfo::DefinesPredicate(MachineInstr *MI,
1238 std::vector<MachineOperand> &Pred) const {
1239 // Note: At the present time, the contents of Pred from this function is
1240 // unused by IfConversion. This implementation follows ARM by pushing the
1241 // CR-defining operand. Because the 'DZ' and 'DNZ' count as types of
1242 // predicate, instructions defining CTR or CTR8 are also included as
1243 // predicate-defining instructions.
1244
1245 const TargetRegisterClass *RCs[] =
1246 { &PPC::CRRCRegClass, &PPC::CRBITRCRegClass,
1247 &PPC::CTRRCRegClass, &PPC::CTRRC8RegClass };
1248
1249 bool Found = false;
1250 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1251 const MachineOperand &MO = MI->getOperand(i);
Hal Finkelaf822012013-04-10 07:17:47 +00001252 for (unsigned c = 0; c < array_lengthof(RCs) && !Found; ++c) {
Hal Finkel5711eca2013-04-09 22:58:37 +00001253 const TargetRegisterClass *RC = RCs[c];
Hal Finkelaf822012013-04-10 07:17:47 +00001254 if (MO.isReg()) {
1255 if (MO.isDef() && RC->contains(MO.getReg())) {
Hal Finkel5711eca2013-04-09 22:58:37 +00001256 Pred.push_back(MO);
1257 Found = true;
1258 }
Hal Finkelaf822012013-04-10 07:17:47 +00001259 } else if (MO.isRegMask()) {
1260 for (TargetRegisterClass::iterator I = RC->begin(),
1261 IE = RC->end(); I != IE; ++I)
1262 if (MO.clobbersPhysReg(*I)) {
1263 Pred.push_back(MO);
1264 Found = true;
1265 }
Hal Finkel5711eca2013-04-09 22:58:37 +00001266 }
1267 }
1268 }
1269
1270 return Found;
1271}
1272
1273bool PPCInstrInfo::isPredicable(MachineInstr *MI) const {
1274 unsigned OpC = MI->getOpcode();
1275 switch (OpC) {
1276 default:
1277 return false;
1278 case PPC::B:
1279 case PPC::BLR:
Hal Finkel500b0042013-04-10 06:42:34 +00001280 case PPC::BCTR:
1281 case PPC::BCTR8:
1282 case PPC::BCTRL:
1283 case PPC::BCTRL8:
Hal Finkel5711eca2013-04-09 22:58:37 +00001284 return true;
1285 }
1286}
1287
Hal Finkel82656cb2013-04-18 22:15:08 +00001288bool PPCInstrInfo::analyzeCompare(const MachineInstr *MI,
1289 unsigned &SrcReg, unsigned &SrcReg2,
1290 int &Mask, int &Value) const {
1291 unsigned Opc = MI->getOpcode();
1292
1293 switch (Opc) {
1294 default: return false;
1295 case PPC::CMPWI:
1296 case PPC::CMPLWI:
1297 case PPC::CMPDI:
1298 case PPC::CMPLDI:
1299 SrcReg = MI->getOperand(1).getReg();
1300 SrcReg2 = 0;
1301 Value = MI->getOperand(2).getImm();
1302 Mask = 0xFFFF;
1303 return true;
1304 case PPC::CMPW:
1305 case PPC::CMPLW:
1306 case PPC::CMPD:
1307 case PPC::CMPLD:
1308 case PPC::FCMPUS:
1309 case PPC::FCMPUD:
1310 SrcReg = MI->getOperand(1).getReg();
1311 SrcReg2 = MI->getOperand(2).getReg();
1312 return true;
1313 }
1314}
Hal Finkele6322392013-04-19 22:08:38 +00001315
Hal Finkel82656cb2013-04-18 22:15:08 +00001316bool PPCInstrInfo::optimizeCompareInstr(MachineInstr *CmpInstr,
1317 unsigned SrcReg, unsigned SrcReg2,
1318 int Mask, int Value,
1319 const MachineRegisterInfo *MRI) const {
Hal Finkelb12da6b2013-04-18 22:54:25 +00001320 if (DisableCmpOpt)
1321 return false;
1322
Hal Finkel82656cb2013-04-18 22:15:08 +00001323 int OpC = CmpInstr->getOpcode();
1324 unsigned CRReg = CmpInstr->getOperand(0).getReg();
Hal Finkel08e53ee2013-05-08 12:16:14 +00001325
1326 // FP record forms set CR1 based on the execption status bits, not a
1327 // comparison with zero.
1328 if (OpC == PPC::FCMPUS || OpC == PPC::FCMPUD)
1329 return false;
Hal Finkel82656cb2013-04-18 22:15:08 +00001330
1331 // The record forms set the condition register based on a signed comparison
1332 // with zero (so says the ISA manual). This is not as straightforward as it
1333 // seems, however, because this is always a 64-bit comparison on PPC64, even
1334 // for instructions that are 32-bit in nature (like slw for example).
1335 // So, on PPC32, for unsigned comparisons, we can use the record forms only
1336 // for equality checks (as those don't depend on the sign). On PPC64,
1337 // we are restricted to equality for unsigned 64-bit comparisons and for
1338 // signed 32-bit comparisons the applicability is more restricted.
Eric Christopher1dcea732014-06-12 21:48:52 +00001339 bool isPPC64 = Subtarget.isPPC64();
Hal Finkel82656cb2013-04-18 22:15:08 +00001340 bool is32BitSignedCompare = OpC == PPC::CMPWI || OpC == PPC::CMPW;
1341 bool is32BitUnsignedCompare = OpC == PPC::CMPLWI || OpC == PPC::CMPLW;
1342 bool is64BitUnsignedCompare = OpC == PPC::CMPLDI || OpC == PPC::CMPLD;
1343
1344 // Get the unique definition of SrcReg.
1345 MachineInstr *MI = MRI->getUniqueVRegDef(SrcReg);
1346 if (!MI) return false;
1347 int MIOpC = MI->getOpcode();
1348
1349 bool equalityOnly = false;
1350 bool noSub = false;
1351 if (isPPC64) {
1352 if (is32BitSignedCompare) {
1353 // We can perform this optimization only if MI is sign-extending.
1354 if (MIOpC == PPC::SRAW || MIOpC == PPC::SRAWo ||
1355 MIOpC == PPC::SRAWI || MIOpC == PPC::SRAWIo ||
1356 MIOpC == PPC::EXTSB || MIOpC == PPC::EXTSBo ||
1357 MIOpC == PPC::EXTSH || MIOpC == PPC::EXTSHo ||
1358 MIOpC == PPC::EXTSW || MIOpC == PPC::EXTSWo) {
1359 noSub = true;
1360 } else
1361 return false;
1362 } else if (is32BitUnsignedCompare) {
1363 // We can perform this optimization, equality only, if MI is
1364 // zero-extending.
1365 if (MIOpC == PPC::CNTLZW || MIOpC == PPC::CNTLZWo ||
1366 MIOpC == PPC::SLW || MIOpC == PPC::SLWo ||
1367 MIOpC == PPC::SRW || MIOpC == PPC::SRWo) {
1368 noSub = true;
1369 equalityOnly = true;
1370 } else
1371 return false;
Hal Finkel08e53ee2013-05-08 12:16:14 +00001372 } else
Hal Finkel82656cb2013-04-18 22:15:08 +00001373 equalityOnly = is64BitUnsignedCompare;
Hal Finkel08e53ee2013-05-08 12:16:14 +00001374 } else
Hal Finkel82656cb2013-04-18 22:15:08 +00001375 equalityOnly = is32BitUnsignedCompare;
1376
1377 if (equalityOnly) {
1378 // We need to check the uses of the condition register in order to reject
1379 // non-equality comparisons.
Owen Anderson16c6bf42014-03-13 23:12:04 +00001380 for (MachineRegisterInfo::use_instr_iterator I =MRI->use_instr_begin(CRReg),
1381 IE = MRI->use_instr_end(); I != IE; ++I) {
Hal Finkel82656cb2013-04-18 22:15:08 +00001382 MachineInstr *UseMI = &*I;
1383 if (UseMI->getOpcode() == PPC::BCC) {
1384 unsigned Pred = UseMI->getOperand(0).getImm();
Hal Finkelc3632452013-05-07 17:49:55 +00001385 if (Pred != PPC::PRED_EQ && Pred != PPC::PRED_NE)
1386 return false;
Hal Finkel82656cb2013-04-18 22:15:08 +00001387 } else if (UseMI->getOpcode() == PPC::ISEL ||
1388 UseMI->getOpcode() == PPC::ISEL8) {
1389 unsigned SubIdx = UseMI->getOperand(3).getSubReg();
Hal Finkelc3632452013-05-07 17:49:55 +00001390 if (SubIdx != PPC::sub_eq)
1391 return false;
Hal Finkel82656cb2013-04-18 22:15:08 +00001392 } else
1393 return false;
1394 }
1395 }
1396
Hal Finkelc3632452013-05-07 17:49:55 +00001397 MachineBasicBlock::iterator I = CmpInstr;
Hal Finkel82656cb2013-04-18 22:15:08 +00001398
1399 // Scan forward to find the first use of the compare.
1400 for (MachineBasicBlock::iterator EL = CmpInstr->getParent()->end();
1401 I != EL; ++I) {
1402 bool FoundUse = false;
Owen Anderson16c6bf42014-03-13 23:12:04 +00001403 for (MachineRegisterInfo::use_instr_iterator J =MRI->use_instr_begin(CRReg),
1404 JE = MRI->use_instr_end(); J != JE; ++J)
Hal Finkel82656cb2013-04-18 22:15:08 +00001405 if (&*J == &*I) {
1406 FoundUse = true;
1407 break;
1408 }
1409
1410 if (FoundUse)
1411 break;
1412 }
1413
Hal Finkel82656cb2013-04-18 22:15:08 +00001414 // There are two possible candidates which can be changed to set CR[01].
1415 // One is MI, the other is a SUB instruction.
1416 // For CMPrr(r1,r2), we are looking for SUB(r1,r2) or SUB(r2,r1).
Craig Topper062a2ba2014-04-25 05:30:21 +00001417 MachineInstr *Sub = nullptr;
Hal Finkel82656cb2013-04-18 22:15:08 +00001418 if (SrcReg2 != 0)
1419 // MI is not a candidate for CMPrr.
Craig Topper062a2ba2014-04-25 05:30:21 +00001420 MI = nullptr;
Hal Finkel82656cb2013-04-18 22:15:08 +00001421 // FIXME: Conservatively refuse to convert an instruction which isn't in the
1422 // same BB as the comparison. This is to allow the check below to avoid calls
1423 // (and other explicit clobbers); instead we should really check for these
1424 // more explicitly (in at least a few predecessors).
1425 else if (MI->getParent() != CmpInstr->getParent() || Value != 0) {
1426 // PPC does not have a record-form SUBri.
1427 return false;
1428 }
1429
1430 // Search for Sub.
1431 const TargetRegisterInfo *TRI = &getRegisterInfo();
1432 --I;
Hal Finkelc3632452013-05-07 17:49:55 +00001433
1434 // Get ready to iterate backward from CmpInstr.
1435 MachineBasicBlock::iterator E = MI,
1436 B = CmpInstr->getParent()->begin();
1437
Hal Finkel82656cb2013-04-18 22:15:08 +00001438 for (; I != E && !noSub; --I) {
1439 const MachineInstr &Instr = *I;
1440 unsigned IOpC = Instr.getOpcode();
1441
1442 if (&*I != CmpInstr && (
Hal Finkel08e53ee2013-05-08 12:16:14 +00001443 Instr.modifiesRegister(PPC::CR0, TRI) ||
1444 Instr.readsRegister(PPC::CR0, TRI)))
Hal Finkel82656cb2013-04-18 22:15:08 +00001445 // This instruction modifies or uses the record condition register after
1446 // the one we want to change. While we could do this transformation, it
1447 // would likely not be profitable. This transformation removes one
1448 // instruction, and so even forcing RA to generate one move probably
1449 // makes it unprofitable.
1450 return false;
1451
1452 // Check whether CmpInstr can be made redundant by the current instruction.
1453 if ((OpC == PPC::CMPW || OpC == PPC::CMPLW ||
1454 OpC == PPC::CMPD || OpC == PPC::CMPLD) &&
1455 (IOpC == PPC::SUBF || IOpC == PPC::SUBF8) &&
1456 ((Instr.getOperand(1).getReg() == SrcReg &&
1457 Instr.getOperand(2).getReg() == SrcReg2) ||
1458 (Instr.getOperand(1).getReg() == SrcReg2 &&
1459 Instr.getOperand(2).getReg() == SrcReg))) {
1460 Sub = &*I;
1461 break;
1462 }
1463
Hal Finkel82656cb2013-04-18 22:15:08 +00001464 if (I == B)
1465 // The 'and' is below the comparison instruction.
1466 return false;
1467 }
1468
1469 // Return false if no candidates exist.
1470 if (!MI && !Sub)
1471 return false;
1472
1473 // The single candidate is called MI.
1474 if (!MI) MI = Sub;
1475
1476 int NewOpC = -1;
1477 MIOpC = MI->getOpcode();
1478 if (MIOpC == PPC::ANDIo || MIOpC == PPC::ANDIo8)
1479 NewOpC = MIOpC;
1480 else {
1481 NewOpC = PPC::getRecordFormOpcode(MIOpC);
1482 if (NewOpC == -1 && PPC::getNonRecordFormOpcode(MIOpC) != -1)
1483 NewOpC = MIOpC;
1484 }
1485
1486 // FIXME: On the non-embedded POWER architectures, only some of the record
1487 // forms are fast, and we should use only the fast ones.
1488
1489 // The defining instruction has a record form (or is already a record
1490 // form). It is possible, however, that we'll need to reverse the condition
1491 // code of the users.
1492 if (NewOpC == -1)
1493 return false;
1494
Hal Finkele6322392013-04-19 22:08:38 +00001495 SmallVector<std::pair<MachineOperand*, PPC::Predicate>, 4> PredsToUpdate;
1496 SmallVector<std::pair<MachineOperand*, unsigned>, 4> SubRegsToUpdate;
Hal Finkel82656cb2013-04-18 22:15:08 +00001497
1498 // If we have SUB(r1, r2) and CMP(r2, r1), the condition code based on CMP
1499 // needs to be updated to be based on SUB. Push the condition code
1500 // operands to OperandsToUpdate. If it is safe to remove CmpInstr, the
1501 // condition code of these operands will be modified.
1502 bool ShouldSwap = false;
1503 if (Sub) {
1504 ShouldSwap = SrcReg2 != 0 && Sub->getOperand(1).getReg() == SrcReg2 &&
1505 Sub->getOperand(2).getReg() == SrcReg;
1506
1507 // The operands to subf are the opposite of sub, so only in the fixed-point
1508 // case, invert the order.
Hal Finkel08e53ee2013-05-08 12:16:14 +00001509 ShouldSwap = !ShouldSwap;
Hal Finkel82656cb2013-04-18 22:15:08 +00001510 }
1511
1512 if (ShouldSwap)
Owen Anderson16c6bf42014-03-13 23:12:04 +00001513 for (MachineRegisterInfo::use_instr_iterator
1514 I = MRI->use_instr_begin(CRReg), IE = MRI->use_instr_end();
1515 I != IE; ++I) {
Hal Finkel82656cb2013-04-18 22:15:08 +00001516 MachineInstr *UseMI = &*I;
1517 if (UseMI->getOpcode() == PPC::BCC) {
1518 PPC::Predicate Pred = (PPC::Predicate) UseMI->getOperand(0).getImm();
Hal Finkele6322392013-04-19 22:08:38 +00001519 assert((!equalityOnly ||
1520 Pred == PPC::PRED_EQ || Pred == PPC::PRED_NE) &&
1521 "Invalid predicate for equality-only optimization");
Owen Anderson16c6bf42014-03-13 23:12:04 +00001522 PredsToUpdate.push_back(std::make_pair(&(UseMI->getOperand(0)),
Hal Finkel0f64e212013-04-20 05:16:26 +00001523 PPC::getSwappedPredicate(Pred)));
Hal Finkel82656cb2013-04-18 22:15:08 +00001524 } else if (UseMI->getOpcode() == PPC::ISEL ||
1525 UseMI->getOpcode() == PPC::ISEL8) {
Hal Finkele6322392013-04-19 22:08:38 +00001526 unsigned NewSubReg = UseMI->getOperand(3).getSubReg();
1527 assert((!equalityOnly || NewSubReg == PPC::sub_eq) &&
1528 "Invalid CR bit for equality-only optimization");
1529
1530 if (NewSubReg == PPC::sub_lt)
1531 NewSubReg = PPC::sub_gt;
1532 else if (NewSubReg == PPC::sub_gt)
1533 NewSubReg = PPC::sub_lt;
1534
Owen Anderson16c6bf42014-03-13 23:12:04 +00001535 SubRegsToUpdate.push_back(std::make_pair(&(UseMI->getOperand(3)),
Hal Finkele6322392013-04-19 22:08:38 +00001536 NewSubReg));
Hal Finkel82656cb2013-04-18 22:15:08 +00001537 } else // We need to abort on a user we don't understand.
1538 return false;
1539 }
1540
1541 // Create a new virtual register to hold the value of the CR set by the
1542 // record-form instruction. If the instruction was not previously in
1543 // record form, then set the kill flag on the CR.
1544 CmpInstr->eraseFromParent();
1545
1546 MachineBasicBlock::iterator MII = MI;
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001547 BuildMI(*MI->getParent(), std::next(MII), MI->getDebugLoc(),
Hal Finkel82656cb2013-04-18 22:15:08 +00001548 get(TargetOpcode::COPY), CRReg)
Hal Finkel08e53ee2013-05-08 12:16:14 +00001549 .addReg(PPC::CR0, MIOpC != NewOpC ? RegState::Kill : 0);
Hal Finkel82656cb2013-04-18 22:15:08 +00001550
1551 if (MIOpC != NewOpC) {
1552 // We need to be careful here: we're replacing one instruction with
1553 // another, and we need to make sure that we get all of the right
1554 // implicit uses and defs. On the other hand, the caller may be holding
1555 // an iterator to this instruction, and so we can't delete it (this is
1556 // specifically the case if this is the instruction directly after the
1557 // compare).
1558
1559 const MCInstrDesc &NewDesc = get(NewOpC);
1560 MI->setDesc(NewDesc);
1561
1562 if (NewDesc.ImplicitDefs)
1563 for (const uint16_t *ImpDefs = NewDesc.getImplicitDefs();
1564 *ImpDefs; ++ImpDefs)
1565 if (!MI->definesRegister(*ImpDefs))
1566 MI->addOperand(*MI->getParent()->getParent(),
1567 MachineOperand::CreateReg(*ImpDefs, true, true));
1568 if (NewDesc.ImplicitUses)
1569 for (const uint16_t *ImpUses = NewDesc.getImplicitUses();
1570 *ImpUses; ++ImpUses)
1571 if (!MI->readsRegister(*ImpUses))
1572 MI->addOperand(*MI->getParent()->getParent(),
1573 MachineOperand::CreateReg(*ImpUses, false, true));
1574 }
1575
1576 // Modify the condition code of operands in OperandsToUpdate.
1577 // Since we have SUB(r1, r2) and CMP(r2, r1), the condition code needs to
1578 // be changed from r2 > r1 to r1 < r2, from r2 < r1 to r1 > r2, etc.
Hal Finkele6322392013-04-19 22:08:38 +00001579 for (unsigned i = 0, e = PredsToUpdate.size(); i < e; i++)
1580 PredsToUpdate[i].first->setImm(PredsToUpdate[i].second);
Hal Finkel82656cb2013-04-18 22:15:08 +00001581
Hal Finkele6322392013-04-19 22:08:38 +00001582 for (unsigned i = 0, e = SubRegsToUpdate.size(); i < e; i++)
1583 SubRegsToUpdate[i].first->setSubReg(SubRegsToUpdate[i].second);
Hal Finkel82656cb2013-04-18 22:15:08 +00001584
1585 return true;
1586}
1587
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +00001588/// GetInstSize - Return the number of bytes of code the specified
1589/// instruction may be. This returns the maximum number of bytes.
1590///
1591unsigned PPCInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
Hal Finkela7bbaf62014-02-02 06:12:27 +00001592 unsigned Opcode = MI->getOpcode();
1593
1594 if (Opcode == PPC::INLINEASM) {
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +00001595 const MachineFunction *MF = MI->getParent()->getParent();
1596 const char *AsmStr = MI->getOperand(0).getSymbolName();
Chris Lattner7b26fce2009-08-22 20:48:53 +00001597 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
Hal Finkela7bbaf62014-02-02 06:12:27 +00001598 } else {
1599 const MCInstrDesc &Desc = get(Opcode);
1600 return Desc.getSize();
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +00001601 }
1602}
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001603
Hal Finkel174e5902014-03-25 23:29:21 +00001604#undef DEBUG_TYPE
1605#define DEBUG_TYPE "ppc-vsx-fma-mutate"
1606
1607namespace {
1608 // PPCVSXFMAMutate pass - For copies between VSX registers and non-VSX registers
1609 // (Altivec and scalar floating-point registers), we need to transform the
1610 // copies into subregister copies with other restrictions.
1611 struct PPCVSXFMAMutate : public MachineFunctionPass {
1612 static char ID;
1613 PPCVSXFMAMutate() : MachineFunctionPass(ID) {
1614 initializePPCVSXFMAMutatePass(*PassRegistry::getPassRegistry());
1615 }
1616
1617 LiveIntervals *LIS;
1618
1619 const PPCTargetMachine *TM;
1620 const PPCInstrInfo *TII;
1621
1622protected:
1623 bool processBlock(MachineBasicBlock &MBB) {
1624 bool Changed = false;
1625
1626 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
Bill Schmidta087d742014-10-17 21:02:44 +00001627 const TargetRegisterInfo *TRI = &TII->getRegisterInfo();
Hal Finkel174e5902014-03-25 23:29:21 +00001628 for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end();
1629 I != IE; ++I) {
1630 MachineInstr *MI = I;
1631
1632 // The default (A-type) VSX FMA form kills the addend (it is taken from
1633 // the target register, which is then updated to reflect the result of
1634 // the FMA). If the instruction, however, kills one of the registers
1635 // used for the product, then we can use the M-form instruction (which
1636 // will take that value from the to-be-defined register).
1637
1638 int AltOpc = PPC::getAltVSXFMAOpcode(MI->getOpcode());
1639 if (AltOpc == -1)
1640 continue;
1641
1642 // This pass is run after register coalescing, and so we're looking for
1643 // a situation like this:
1644 // ...
1645 // %vreg5<def> = COPY %vreg9; VSLRC:%vreg5,%vreg9
1646 // %vreg5<def,tied1> = XSMADDADP %vreg5<tied0>, %vreg17, %vreg16,
1647 // %RM<imp-use>; VSLRC:%vreg5,%vreg17,%vreg16
1648 // ...
1649 // %vreg9<def,tied1> = XSMADDADP %vreg9<tied0>, %vreg17, %vreg19,
1650 // %RM<imp-use>; VSLRC:%vreg9,%vreg17,%vreg19
1651 // ...
1652 // Where we can eliminate the copy by changing from the A-type to the
1653 // M-type instruction. Specifically, for this example, this means:
1654 // %vreg5<def,tied1> = XSMADDADP %vreg5<tied0>, %vreg17, %vreg16,
1655 // %RM<imp-use>; VSLRC:%vreg5,%vreg17,%vreg16
1656 // is replaced by:
1657 // %vreg16<def,tied1> = XSMADDMDP %vreg16<tied0>, %vreg18, %vreg9,
1658 // %RM<imp-use>; VSLRC:%vreg16,%vreg18,%vreg9
1659 // and we remove: %vreg5<def> = COPY %vreg9; VSLRC:%vreg5,%vreg9
1660
1661 SlotIndex FMAIdx = LIS->getInstructionIndex(MI);
1662
1663 VNInfo *AddendValNo =
1664 LIS->getInterval(MI->getOperand(1).getReg()).Query(FMAIdx).valueIn();
1665 MachineInstr *AddendMI = LIS->getInstructionFromIndex(AddendValNo->def);
1666
1667 // The addend and this instruction must be in the same block.
1668
Hal Finkel19be5062014-03-29 05:29:01 +00001669 if (!AddendMI || AddendMI->getParent() != MI->getParent())
Hal Finkel174e5902014-03-25 23:29:21 +00001670 continue;
1671
1672 // The addend must be a full copy within the same register class.
1673
1674 if (!AddendMI->isFullCopy())
1675 continue;
1676
Hal Finkel19be5062014-03-29 05:29:01 +00001677 unsigned AddendSrcReg = AddendMI->getOperand(1).getReg();
1678 if (TargetRegisterInfo::isVirtualRegister(AddendSrcReg)) {
1679 if (MRI.getRegClass(AddendMI->getOperand(0).getReg()) !=
1680 MRI.getRegClass(AddendSrcReg))
1681 continue;
1682 } else {
1683 // If AddendSrcReg is a physical register, make sure the destination
1684 // register class contains it.
1685 if (!MRI.getRegClass(AddendMI->getOperand(0).getReg())
1686 ->contains(AddendSrcReg))
1687 continue;
1688 }
Hal Finkel174e5902014-03-25 23:29:21 +00001689
1690 // In theory, there could be other uses of the addend copy before this
1691 // fma. We could deal with this, but that would require additional
1692 // logic below and I suspect it will not occur in any relevant
Bill Schmidta087d742014-10-17 21:02:44 +00001693 // situations. Additionally, check whether the copy source is killed
1694 // prior to the fma. In order to replace the addend here with the
1695 // source of the copy, it must still be live here. We can't use
1696 // interval testing for a physical register, so as long as we're
1697 // walking the MIs we may as well test liveness here.
1698 bool OtherUsers = false, KillsAddendSrc = false;
Hal Finkel174e5902014-03-25 23:29:21 +00001699 for (auto J = std::prev(I), JE = MachineBasicBlock::iterator(AddendMI);
Bill Schmidta087d742014-10-17 21:02:44 +00001700 J != JE; --J) {
Hal Finkel174e5902014-03-25 23:29:21 +00001701 if (J->readsVirtualRegister(AddendMI->getOperand(0).getReg())) {
1702 OtherUsers = true;
1703 break;
1704 }
Bill Schmidta087d742014-10-17 21:02:44 +00001705 if (J->modifiesRegister(AddendSrcReg, TRI) ||
1706 J->killsRegister(AddendSrcReg, TRI)) {
1707 KillsAddendSrc = true;
1708 break;
1709 }
1710 }
Hal Finkel174e5902014-03-25 23:29:21 +00001711
Bill Schmidta087d742014-10-17 21:02:44 +00001712 if (OtherUsers || KillsAddendSrc)
Hal Finkel174e5902014-03-25 23:29:21 +00001713 continue;
1714
1715 // Find one of the product operands that is killed by this instruction.
1716
1717 unsigned KilledProdOp = 0, OtherProdOp = 0;
1718 if (LIS->getInterval(MI->getOperand(2).getReg())
1719 .Query(FMAIdx).isKill()) {
1720 KilledProdOp = 2;
1721 OtherProdOp = 3;
1722 } else if (LIS->getInterval(MI->getOperand(3).getReg())
1723 .Query(FMAIdx).isKill()) {
1724 KilledProdOp = 3;
1725 OtherProdOp = 2;
1726 }
1727
Hal Finkel19be5062014-03-29 05:29:01 +00001728 // If there are no killed product operands, then this transformation is
1729 // likely not profitable.
Hal Finkel174e5902014-03-25 23:29:21 +00001730 if (!KilledProdOp)
1731 continue;
1732
Bill Schmidta087d742014-10-17 21:02:44 +00001733 // For virtual registers, verify that the addend source register
1734 // is live here (as should have been assured above).
Bill Schmidtba637db2014-10-17 21:19:59 +00001735 assert((!TargetRegisterInfo::isVirtualRegister(AddendSrcReg) ||
1736 LIS->getInterval(AddendSrcReg).liveAt(FMAIdx)) &&
1737 "Addend source register is not live!");
Hal Finkel174e5902014-03-25 23:29:21 +00001738
1739 // Transform: (O2 * O3) + O1 -> (O2 * O1) + O3.
1740
1741 unsigned AddReg = AddendMI->getOperand(1).getReg();
1742 unsigned KilledProdReg = MI->getOperand(KilledProdOp).getReg();
1743 unsigned OtherProdReg = MI->getOperand(OtherProdOp).getReg();
1744
1745 unsigned AddSubReg = AddendMI->getOperand(1).getSubReg();
1746 unsigned KilledProdSubReg = MI->getOperand(KilledProdOp).getSubReg();
1747 unsigned OtherProdSubReg = MI->getOperand(OtherProdOp).getSubReg();
1748
1749 bool AddRegKill = AddendMI->getOperand(1).isKill();
1750 bool KilledProdRegKill = MI->getOperand(KilledProdOp).isKill();
1751 bool OtherProdRegKill = MI->getOperand(OtherProdOp).isKill();
1752
1753 bool AddRegUndef = AddendMI->getOperand(1).isUndef();
1754 bool KilledProdRegUndef = MI->getOperand(KilledProdOp).isUndef();
1755 bool OtherProdRegUndef = MI->getOperand(OtherProdOp).isUndef();
1756
1757 unsigned OldFMAReg = MI->getOperand(0).getReg();
1758
Bill Schmidt5c6cb812014-10-21 13:02:37 +00001759 // The transformation doesn't work well with things like:
1760 // %vreg5 = A-form-op %vreg5, %vreg11, %vreg5;
1761 // so leave such things alone.
1762 if (OldFMAReg == KilledProdReg)
1763 continue;
1764
Hal Finkel174e5902014-03-25 23:29:21 +00001765 assert(OldFMAReg == AddendMI->getOperand(0).getReg() &&
1766 "Addend copy not tied to old FMA output!");
1767
1768 DEBUG(dbgs() << "VSX FMA Mutation:\n " << *MI;);
1769
1770 MI->getOperand(0).setReg(KilledProdReg);
1771 MI->getOperand(1).setReg(KilledProdReg);
1772 MI->getOperand(3).setReg(AddReg);
1773 MI->getOperand(2).setReg(OtherProdReg);
1774
1775 MI->getOperand(0).setSubReg(KilledProdSubReg);
1776 MI->getOperand(1).setSubReg(KilledProdSubReg);
1777 MI->getOperand(3).setSubReg(AddSubReg);
1778 MI->getOperand(2).setSubReg(OtherProdSubReg);
1779
1780 MI->getOperand(1).setIsKill(KilledProdRegKill);
1781 MI->getOperand(3).setIsKill(AddRegKill);
1782 MI->getOperand(2).setIsKill(OtherProdRegKill);
1783
1784 MI->getOperand(1).setIsUndef(KilledProdRegUndef);
1785 MI->getOperand(3).setIsUndef(AddRegUndef);
1786 MI->getOperand(2).setIsUndef(OtherProdRegUndef);
1787
1788 MI->setDesc(TII->get(AltOpc));
1789
1790 DEBUG(dbgs() << " -> " << *MI);
1791
1792 // The killed product operand was killed here, so we can reuse it now
1793 // for the result of the fma.
1794
1795 LiveInterval &FMAInt = LIS->getInterval(OldFMAReg);
1796 VNInfo *FMAValNo = FMAInt.getVNInfoAt(FMAIdx.getRegSlot());
1797 for (auto UI = MRI.reg_nodbg_begin(OldFMAReg), UE = MRI.reg_nodbg_end();
1798 UI != UE;) {
1799 MachineOperand &UseMO = *UI;
1800 MachineInstr *UseMI = UseMO.getParent();
1801 ++UI;
1802
1803 // Don't replace the result register of the copy we're about to erase.
1804 if (UseMI == AddendMI)
1805 continue;
1806
1807 UseMO.setReg(KilledProdReg);
1808 UseMO.setSubReg(KilledProdSubReg);
1809 }
1810
1811 // Extend the live intervals of the killed product operand to hold the
1812 // fma result.
1813
1814 LiveInterval &NewFMAInt = LIS->getInterval(KilledProdReg);
1815 for (LiveInterval::iterator AI = FMAInt.begin(), AE = FMAInt.end();
1816 AI != AE; ++AI) {
1817 // Don't add the segment that corresponds to the original copy.
1818 if (AI->valno == AddendValNo)
1819 continue;
1820
1821 VNInfo *NewFMAValNo =
1822 NewFMAInt.getNextValue(AI->start,
1823 LIS->getVNInfoAllocator());
1824
1825 NewFMAInt.addSegment(LiveInterval::Segment(AI->start, AI->end,
1826 NewFMAValNo));
1827 }
1828 DEBUG(dbgs() << " extended: " << NewFMAInt << '\n');
1829
1830 FMAInt.removeValNo(FMAValNo);
1831 DEBUG(dbgs() << " trimmed: " << FMAInt << '\n');
1832
1833 // Remove the (now unused) copy.
1834
1835 DEBUG(dbgs() << " removing: " << *AddendMI << '\n');
1836 LIS->RemoveMachineInstrFromMaps(AddendMI);
1837 AddendMI->eraseFromParent();
1838
1839 Changed = true;
1840 }
1841
1842 return Changed;
1843 }
1844
1845public:
Craig Topper0d3fa922014-04-29 07:57:37 +00001846 bool runOnMachineFunction(MachineFunction &MF) override {
Eric Christopherd71e4442014-05-22 01:21:35 +00001847 TM = static_cast<const PPCTargetMachine *>(&MF.getTarget());
1848 // If we don't have VSX then go ahead and return without doing
1849 // anything.
1850 if (!TM->getSubtargetImpl()->hasVSX())
1851 return false;
1852
Hal Finkel174e5902014-03-25 23:29:21 +00001853 LIS = &getAnalysis<LiveIntervals>();
1854
Eric Christopherd9134482014-08-04 21:25:23 +00001855 TII = TM->getSubtargetImpl()->getInstrInfo();
Hal Finkel174e5902014-03-25 23:29:21 +00001856
1857 bool Changed = false;
1858
1859 if (DisableVSXFMAMutate)
1860 return Changed;
1861
1862 for (MachineFunction::iterator I = MF.begin(); I != MF.end();) {
1863 MachineBasicBlock &B = *I++;
1864 if (processBlock(B))
1865 Changed = true;
1866 }
1867
1868 return Changed;
1869 }
1870
Craig Topper0d3fa922014-04-29 07:57:37 +00001871 void getAnalysisUsage(AnalysisUsage &AU) const override {
Hal Finkel174e5902014-03-25 23:29:21 +00001872 AU.addRequired<LiveIntervals>();
1873 AU.addPreserved<LiveIntervals>();
1874 AU.addRequired<SlotIndexes>();
1875 AU.addPreserved<SlotIndexes>();
1876 MachineFunctionPass::getAnalysisUsage(AU);
1877 }
1878 };
1879}
1880
1881INITIALIZE_PASS_BEGIN(PPCVSXFMAMutate, DEBUG_TYPE,
1882 "PowerPC VSX FMA Mutation", false, false)
1883INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
1884INITIALIZE_PASS_DEPENDENCY(SlotIndexes)
1885INITIALIZE_PASS_END(PPCVSXFMAMutate, DEBUG_TYPE,
1886 "PowerPC VSX FMA Mutation", false, false)
1887
1888char &llvm::PPCVSXFMAMutateID = PPCVSXFMAMutate::ID;
1889
1890char PPCVSXFMAMutate::ID = 0;
1891FunctionPass*
1892llvm::createPPCVSXFMAMutatePass() { return new PPCVSXFMAMutate(); }
Hal Finkel27774d92014-03-13 07:58:58 +00001893
1894#undef DEBUG_TYPE
1895#define DEBUG_TYPE "ppc-vsx-copy"
1896
1897namespace llvm {
1898 void initializePPCVSXCopyPass(PassRegistry&);
1899}
1900
1901namespace {
1902 // PPCVSXCopy pass - For copies between VSX registers and non-VSX registers
1903 // (Altivec and scalar floating-point registers), we need to transform the
1904 // copies into subregister copies with other restrictions.
1905 struct PPCVSXCopy : public MachineFunctionPass {
1906 static char ID;
1907 PPCVSXCopy() : MachineFunctionPass(ID) {
1908 initializePPCVSXCopyPass(*PassRegistry::getPassRegistry());
1909 }
1910
1911 const PPCTargetMachine *TM;
1912 const PPCInstrInfo *TII;
1913
1914 bool IsRegInClass(unsigned Reg, const TargetRegisterClass *RC,
1915 MachineRegisterInfo &MRI) {
1916 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1917 return RC->hasSubClassEq(MRI.getRegClass(Reg));
1918 } else if (RC->contains(Reg)) {
1919 return true;
1920 }
1921
1922 return false;
1923 }
1924
1925 bool IsVSReg(unsigned Reg, MachineRegisterInfo &MRI) {
1926 return IsRegInClass(Reg, &PPC::VSRCRegClass, MRI);
1927 }
1928
1929 bool IsVRReg(unsigned Reg, MachineRegisterInfo &MRI) {
1930 return IsRegInClass(Reg, &PPC::VRRCRegClass, MRI);
1931 }
1932
1933 bool IsF8Reg(unsigned Reg, MachineRegisterInfo &MRI) {
1934 return IsRegInClass(Reg, &PPC::F8RCRegClass, MRI);
1935 }
1936
1937protected:
1938 bool processBlock(MachineBasicBlock &MBB) {
1939 bool Changed = false;
1940
1941 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
1942 for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end();
1943 I != IE; ++I) {
1944 MachineInstr *MI = I;
1945 if (!MI->isFullCopy())
1946 continue;
1947
1948 MachineOperand &DstMO = MI->getOperand(0);
1949 MachineOperand &SrcMO = MI->getOperand(1);
1950
1951 if ( IsVSReg(DstMO.getReg(), MRI) &&
1952 !IsVSReg(SrcMO.getReg(), MRI)) {
1953 // This is a copy *to* a VSX register from a non-VSX register.
1954 Changed = true;
1955
1956 const TargetRegisterClass *SrcRC =
1957 IsVRReg(SrcMO.getReg(), MRI) ? &PPC::VSHRCRegClass :
1958 &PPC::VSLRCRegClass;
1959 assert((IsF8Reg(SrcMO.getReg(), MRI) ||
1960 IsVRReg(SrcMO.getReg(), MRI)) &&
1961 "Unknown source for a VSX copy");
1962
1963 unsigned NewVReg = MRI.createVirtualRegister(SrcRC);
1964 BuildMI(MBB, MI, MI->getDebugLoc(),
1965 TII->get(TargetOpcode::SUBREG_TO_REG), NewVReg)
1966 .addImm(1) // add 1, not 0, because there is no implicit clearing
1967 // of the high bits.
1968 .addOperand(SrcMO)
1969 .addImm(IsVRReg(SrcMO.getReg(), MRI) ? PPC::sub_128 :
1970 PPC::sub_64);
1971
1972 // The source of the original copy is now the new virtual register.
1973 SrcMO.setReg(NewVReg);
1974 } else if (!IsVSReg(DstMO.getReg(), MRI) &&
1975 IsVSReg(SrcMO.getReg(), MRI)) {
1976 // This is a copy *from* a VSX register to a non-VSX register.
1977 Changed = true;
1978
1979 const TargetRegisterClass *DstRC =
1980 IsVRReg(DstMO.getReg(), MRI) ? &PPC::VSHRCRegClass :
1981 &PPC::VSLRCRegClass;
1982 assert((IsF8Reg(DstMO.getReg(), MRI) ||
1983 IsVRReg(DstMO.getReg(), MRI)) &&
1984 "Unknown destination for a VSX copy");
1985
1986 // Copy the VSX value into a new VSX register of the correct subclass.
1987 unsigned NewVReg = MRI.createVirtualRegister(DstRC);
1988 BuildMI(MBB, MI, MI->getDebugLoc(),
1989 TII->get(TargetOpcode::COPY), NewVReg)
1990 .addOperand(SrcMO);
1991
1992 // Transform the original copy into a subregister extraction copy.
1993 SrcMO.setReg(NewVReg);
1994 SrcMO.setSubReg(IsVRReg(DstMO.getReg(), MRI) ? PPC::sub_128 :
1995 PPC::sub_64);
1996 }
1997 }
1998
1999 return Changed;
2000 }
2001
2002public:
Craig Topper0d3fa922014-04-29 07:57:37 +00002003 bool runOnMachineFunction(MachineFunction &MF) override {
Hal Finkel27774d92014-03-13 07:58:58 +00002004 TM = static_cast<const PPCTargetMachine *>(&MF.getTarget());
Eric Christopherd71e4442014-05-22 01:21:35 +00002005 // If we don't have VSX on the subtarget, don't do anything.
2006 if (!TM->getSubtargetImpl()->hasVSX())
2007 return false;
Eric Christopherd9134482014-08-04 21:25:23 +00002008 TII = TM->getSubtargetImpl()->getInstrInfo();
Hal Finkel27774d92014-03-13 07:58:58 +00002009
2010 bool Changed = false;
2011
2012 for (MachineFunction::iterator I = MF.begin(); I != MF.end();) {
2013 MachineBasicBlock &B = *I++;
2014 if (processBlock(B))
2015 Changed = true;
2016 }
2017
2018 return Changed;
2019 }
2020
Craig Topper0d3fa922014-04-29 07:57:37 +00002021 void getAnalysisUsage(AnalysisUsage &AU) const override {
Hal Finkel27774d92014-03-13 07:58:58 +00002022 MachineFunctionPass::getAnalysisUsage(AU);
2023 }
2024 };
2025}
2026
2027INITIALIZE_PASS(PPCVSXCopy, DEBUG_TYPE,
2028 "PowerPC VSX Copy Legalization", false, false)
2029
2030char PPCVSXCopy::ID = 0;
2031FunctionPass*
2032llvm::createPPCVSXCopyPass() { return new PPCVSXCopy(); }
2033
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002034#undef DEBUG_TYPE
Hal Finkelc6fc9b82014-03-27 23:12:31 +00002035#define DEBUG_TYPE "ppc-vsx-copy-cleanup"
2036
2037namespace llvm {
2038 void initializePPCVSXCopyCleanupPass(PassRegistry&);
2039}
2040
2041namespace {
2042 // PPCVSXCopyCleanup pass - We sometimes end up generating self copies of VSX
2043 // registers (mostly because the ABI code still places all values into the
2044 // "traditional" floating-point and vector registers). Remove them here.
2045 struct PPCVSXCopyCleanup : public MachineFunctionPass {
2046 static char ID;
2047 PPCVSXCopyCleanup() : MachineFunctionPass(ID) {
2048 initializePPCVSXCopyCleanupPass(*PassRegistry::getPassRegistry());
2049 }
2050
2051 const PPCTargetMachine *TM;
2052 const PPCInstrInfo *TII;
2053
2054protected:
2055 bool processBlock(MachineBasicBlock &MBB) {
2056 bool Changed = false;
2057
2058 SmallVector<MachineInstr *, 4> ToDelete;
2059 for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end();
2060 I != IE; ++I) {
2061 MachineInstr *MI = I;
2062 if (MI->getOpcode() == PPC::XXLOR &&
2063 MI->getOperand(0).getReg() == MI->getOperand(1).getReg() &&
2064 MI->getOperand(0).getReg() == MI->getOperand(2).getReg())
2065 ToDelete.push_back(MI);
2066 }
2067
2068 if (!ToDelete.empty())
2069 Changed = true;
2070
2071 for (unsigned i = 0, ie = ToDelete.size(); i != ie; ++i) {
2072 DEBUG(dbgs() << "Removing VSX self-copy: " << *ToDelete[i]);
2073 ToDelete[i]->eraseFromParent();
2074 }
2075
2076 return Changed;
2077 }
2078
2079public:
Craig Topper0d3fa922014-04-29 07:57:37 +00002080 bool runOnMachineFunction(MachineFunction &MF) override {
Hal Finkelc6fc9b82014-03-27 23:12:31 +00002081 TM = static_cast<const PPCTargetMachine *>(&MF.getTarget());
Eric Christopherd71e4442014-05-22 01:21:35 +00002082 // If we don't have VSX don't bother doing anything here.
2083 if (!TM->getSubtargetImpl()->hasVSX())
2084 return false;
Eric Christopherd9134482014-08-04 21:25:23 +00002085 TII = TM->getSubtargetImpl()->getInstrInfo();
Hal Finkelc6fc9b82014-03-27 23:12:31 +00002086
2087 bool Changed = false;
2088
2089 for (MachineFunction::iterator I = MF.begin(); I != MF.end();) {
2090 MachineBasicBlock &B = *I++;
2091 if (processBlock(B))
2092 Changed = true;
2093 }
2094
2095 return Changed;
2096 }
2097
Craig Topper0d3fa922014-04-29 07:57:37 +00002098 void getAnalysisUsage(AnalysisUsage &AU) const override {
Hal Finkelc6fc9b82014-03-27 23:12:31 +00002099 MachineFunctionPass::getAnalysisUsage(AU);
2100 }
2101 };
2102}
2103
2104INITIALIZE_PASS(PPCVSXCopyCleanup, DEBUG_TYPE,
2105 "PowerPC VSX Copy Cleanup", false, false)
2106
2107char PPCVSXCopyCleanup::ID = 0;
2108FunctionPass*
2109llvm::createPPCVSXCopyCleanupPass() { return new PPCVSXCopyCleanup(); }
2110
2111#undef DEBUG_TYPE
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002112#define DEBUG_TYPE "ppc-early-ret"
2113STATISTIC(NumBCLR, "Number of early conditional returns");
2114STATISTIC(NumBLR, "Number of early returns");
2115
2116namespace llvm {
2117 void initializePPCEarlyReturnPass(PassRegistry&);
2118}
2119
2120namespace {
2121 // PPCEarlyReturn pass - For simple functions without epilogue code, move
2122 // returns up, and create conditional returns, to avoid unnecessary
2123 // branch-to-blr sequences.
2124 struct PPCEarlyReturn : public MachineFunctionPass {
2125 static char ID;
2126 PPCEarlyReturn() : MachineFunctionPass(ID) {
2127 initializePPCEarlyReturnPass(*PassRegistry::getPassRegistry());
2128 }
2129
2130 const PPCTargetMachine *TM;
2131 const PPCInstrInfo *TII;
2132
2133protected:
Hal Finkel21aad9a2013-04-09 18:25:18 +00002134 bool processBlock(MachineBasicBlock &ReturnMBB) {
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002135 bool Changed = false;
2136
Hal Finkel21aad9a2013-04-09 18:25:18 +00002137 MachineBasicBlock::iterator I = ReturnMBB.begin();
2138 I = ReturnMBB.SkipPHIsAndLabels(I);
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002139
2140 // The block must be essentially empty except for the blr.
Hal Finkel21aad9a2013-04-09 18:25:18 +00002141 if (I == ReturnMBB.end() || I->getOpcode() != PPC::BLR ||
2142 I != ReturnMBB.getLastNonDebugInstr())
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002143 return Changed;
2144
2145 SmallVector<MachineBasicBlock*, 8> PredToRemove;
Hal Finkel21aad9a2013-04-09 18:25:18 +00002146 for (MachineBasicBlock::pred_iterator PI = ReturnMBB.pred_begin(),
2147 PIE = ReturnMBB.pred_end(); PI != PIE; ++PI) {
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002148 bool OtherReference = false, BlockChanged = false;
Hal Finkel21aad9a2013-04-09 18:25:18 +00002149 for (MachineBasicBlock::iterator J = (*PI)->getLastNonDebugInstr();;) {
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002150 if (J->getOpcode() == PPC::B) {
Hal Finkel21aad9a2013-04-09 18:25:18 +00002151 if (J->getOperand(0).getMBB() == &ReturnMBB) {
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002152 // This is an unconditional branch to the return. Replace the
Andrew Trick9defbd82013-12-17 04:50:40 +00002153 // branch with a blr.
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002154 BuildMI(**PI, J, J->getDebugLoc(), TII->get(PPC::BLR));
Hal Finkel21aad9a2013-04-09 18:25:18 +00002155 MachineBasicBlock::iterator K = J--;
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002156 K->eraseFromParent();
2157 BlockChanged = true;
2158 ++NumBLR;
2159 continue;
2160 }
2161 } else if (J->getOpcode() == PPC::BCC) {
Hal Finkel21aad9a2013-04-09 18:25:18 +00002162 if (J->getOperand(2).getMBB() == &ReturnMBB) {
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002163 // This is a conditional branch to the return. Replace the branch
2164 // with a bclr.
Hal Finkel940ab932014-02-28 00:27:01 +00002165 BuildMI(**PI, J, J->getDebugLoc(), TII->get(PPC::BCCLR))
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002166 .addImm(J->getOperand(0).getImm())
2167 .addReg(J->getOperand(1).getReg());
Hal Finkel21aad9a2013-04-09 18:25:18 +00002168 MachineBasicBlock::iterator K = J--;
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002169 K->eraseFromParent();
2170 BlockChanged = true;
2171 ++NumBCLR;
2172 continue;
2173 }
Hal Finkel940ab932014-02-28 00:27:01 +00002174 } else if (J->getOpcode() == PPC::BC || J->getOpcode() == PPC::BCn) {
2175 if (J->getOperand(1).getMBB() == &ReturnMBB) {
2176 // This is a conditional branch to the return. Replace the branch
2177 // with a bclr.
2178 BuildMI(**PI, J, J->getDebugLoc(),
2179 TII->get(J->getOpcode() == PPC::BC ?
2180 PPC::BCLR : PPC::BCLRn))
2181 .addReg(J->getOperand(0).getReg());
2182 MachineBasicBlock::iterator K = J--;
2183 K->eraseFromParent();
2184 BlockChanged = true;
2185 ++NumBCLR;
2186 continue;
2187 }
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002188 } else if (J->isBranch()) {
2189 if (J->isIndirectBranch()) {
Hal Finkel21aad9a2013-04-09 18:25:18 +00002190 if (ReturnMBB.hasAddressTaken())
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002191 OtherReference = true;
2192 } else
2193 for (unsigned i = 0; i < J->getNumOperands(); ++i)
2194 if (J->getOperand(i).isMBB() &&
Hal Finkel21aad9a2013-04-09 18:25:18 +00002195 J->getOperand(i).getMBB() == &ReturnMBB)
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002196 OtherReference = true;
Hal Finkel21aad9a2013-04-09 18:25:18 +00002197 } else if (!J->isTerminator() && !J->isDebugValue())
2198 break;
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002199
Hal Finkel21aad9a2013-04-09 18:25:18 +00002200 if (J == (*PI)->begin())
2201 break;
2202
2203 --J;
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002204 }
2205
Hal Finkel21aad9a2013-04-09 18:25:18 +00002206 if ((*PI)->canFallThrough() && (*PI)->isLayoutSuccessor(&ReturnMBB))
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002207 OtherReference = true;
2208
Andrew Trick9defbd82013-12-17 04:50:40 +00002209 // Predecessors are stored in a vector and can't be removed here.
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002210 if (!OtherReference && BlockChanged) {
2211 PredToRemove.push_back(*PI);
2212 }
2213
2214 if (BlockChanged)
2215 Changed = true;
2216 }
2217
2218 for (unsigned i = 0, ie = PredToRemove.size(); i != ie; ++i)
Hal Finkel21aad9a2013-04-09 18:25:18 +00002219 PredToRemove[i]->removeSuccessor(&ReturnMBB);
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002220
Hal Finkel21aad9a2013-04-09 18:25:18 +00002221 if (Changed && !ReturnMBB.hasAddressTaken()) {
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002222 // We now might be able to merge this blr-only block into its
2223 // by-layout predecessor.
Hal Finkel21aad9a2013-04-09 18:25:18 +00002224 if (ReturnMBB.pred_size() == 1 &&
2225 (*ReturnMBB.pred_begin())->isLayoutSuccessor(&ReturnMBB)) {
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002226 // Move the blr into the preceding block.
Hal Finkel21aad9a2013-04-09 18:25:18 +00002227 MachineBasicBlock &PrevMBB = **ReturnMBB.pred_begin();
2228 PrevMBB.splice(PrevMBB.end(), &ReturnMBB, I);
2229 PrevMBB.removeSuccessor(&ReturnMBB);
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002230 }
2231
Hal Finkel21aad9a2013-04-09 18:25:18 +00002232 if (ReturnMBB.pred_empty())
2233 ReturnMBB.eraseFromParent();
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002234 }
2235
2236 return Changed;
2237 }
2238
2239public:
Craig Topper0d3fa922014-04-29 07:57:37 +00002240 bool runOnMachineFunction(MachineFunction &MF) override {
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002241 TM = static_cast<const PPCTargetMachine *>(&MF.getTarget());
Eric Christopherd9134482014-08-04 21:25:23 +00002242 TII = TM->getSubtargetImpl()->getInstrInfo();
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002243
2244 bool Changed = false;
2245
Hal Finkel21aad9a2013-04-09 18:25:18 +00002246 // If the function does not have at least two blocks, then there is
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002247 // nothing to do.
2248 if (MF.size() < 2)
2249 return Changed;
2250
2251 for (MachineFunction::iterator I = MF.begin(); I != MF.end();) {
Andrew Trick9defbd82013-12-17 04:50:40 +00002252 MachineBasicBlock &B = *I++;
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002253 if (processBlock(B))
2254 Changed = true;
2255 }
2256
2257 return Changed;
2258 }
2259
Craig Topper0d3fa922014-04-29 07:57:37 +00002260 void getAnalysisUsage(AnalysisUsage &AU) const override {
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002261 MachineFunctionPass::getAnalysisUsage(AU);
2262 }
2263 };
2264}
2265
2266INITIALIZE_PASS(PPCEarlyReturn, DEBUG_TYPE,
2267 "PowerPC Early-Return Creation", false, false)
2268
2269char PPCEarlyReturn::ID = 0;
2270FunctionPass*
2271llvm::createPPCEarlyReturnPass() { return new PPCEarlyReturn(); }