blob: 3b0f1efbd02ac449adb10f392620cbf5d19408e2 [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 Finkel940ab932014-02-28 00:27:01 +0000233 MI->getOpcode() != PPC::RLWIMIo &&
234 MI->getOpcode() != PPC::RLWIMI8 &&
235 MI->getOpcode() != PPC::RLWIMI8o)
Jakob Stoklund Olesen9de596e2012-11-28 02:35:17 +0000236 return TargetInstrInfo::commuteInstruction(MI, NewMI);
Andrew Trickc416ba62010-12-24 04:28:06 +0000237
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000238 // Cannot commute if it has a non-zero rotate count.
Chris Lattner5c463782007-12-30 20:49:49 +0000239 if (MI->getOperand(3).getImm() != 0)
Craig Topper062a2ba2014-04-25 05:30:21 +0000240 return nullptr;
Andrew Trickc416ba62010-12-24 04:28:06 +0000241
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000242 // If we have a zero rotate count, we have:
243 // M = mask(MB,ME)
244 // Op0 = (Op1 & ~M) | (Op2 & M)
245 // Change this to:
246 // M = mask((ME+1)&31, (MB-1)&31)
247 // Op0 = (Op2 & ~M) | (Op1 & M)
248
249 // Swap op1/op2
Evan Cheng244183e2008-02-13 02:46:49 +0000250 unsigned Reg0 = MI->getOperand(0).getReg();
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000251 unsigned Reg1 = MI->getOperand(1).getReg();
252 unsigned Reg2 = MI->getOperand(2).getReg();
Andrew Tricke3398282013-12-17 04:50:45 +0000253 unsigned SubReg1 = MI->getOperand(1).getSubReg();
254 unsigned SubReg2 = MI->getOperand(2).getSubReg();
Evan Chengdc2c8742006-11-15 20:58:11 +0000255 bool Reg1IsKill = MI->getOperand(1).isKill();
256 bool Reg2IsKill = MI->getOperand(2).isKill();
Evan Cheng03553bb2008-06-16 07:33:11 +0000257 bool ChangeReg0 = false;
Evan Cheng244183e2008-02-13 02:46:49 +0000258 // If machine instrs are no longer in two-address forms, update
259 // destination register as well.
260 if (Reg0 == Reg1) {
261 // Must be two address instruction!
Evan Cheng6cc775f2011-06-28 19:10:37 +0000262 assert(MI->getDesc().getOperandConstraint(0, MCOI::TIED_TO) &&
Evan Cheng244183e2008-02-13 02:46:49 +0000263 "Expecting a two-address instruction!");
Andrew Tricke3398282013-12-17 04:50:45 +0000264 assert(MI->getOperand(0).getSubReg() == SubReg1 && "Tied subreg mismatch");
Evan Cheng244183e2008-02-13 02:46:49 +0000265 Reg2IsKill = false;
Evan Cheng03553bb2008-06-16 07:33:11 +0000266 ChangeReg0 = true;
Evan Cheng244183e2008-02-13 02:46:49 +0000267 }
Evan Cheng03553bb2008-06-16 07:33:11 +0000268
269 // Masks.
270 unsigned MB = MI->getOperand(4).getImm();
271 unsigned ME = MI->getOperand(5).getImm();
272
273 if (NewMI) {
274 // Create a new instruction.
275 unsigned Reg0 = ChangeReg0 ? Reg2 : MI->getOperand(0).getReg();
276 bool Reg0IsDead = MI->getOperand(0).isDead();
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000277 return BuildMI(MF, MI->getDebugLoc(), MI->getDesc())
Bill Wendlingf7b83c72009-05-13 21:33:08 +0000278 .addReg(Reg0, RegState::Define | getDeadRegState(Reg0IsDead))
279 .addReg(Reg2, getKillRegState(Reg2IsKill))
280 .addReg(Reg1, getKillRegState(Reg1IsKill))
Evan Cheng03553bb2008-06-16 07:33:11 +0000281 .addImm((ME+1) & 31)
282 .addImm((MB-1) & 31);
283 }
284
Andrew Tricke3398282013-12-17 04:50:45 +0000285 if (ChangeReg0) {
Evan Cheng03553bb2008-06-16 07:33:11 +0000286 MI->getOperand(0).setReg(Reg2);
Andrew Tricke3398282013-12-17 04:50:45 +0000287 MI->getOperand(0).setSubReg(SubReg2);
288 }
Chris Lattner10d63412006-05-04 17:52:23 +0000289 MI->getOperand(2).setReg(Reg1);
290 MI->getOperand(1).setReg(Reg2);
Andrew Tricke3398282013-12-17 04:50:45 +0000291 MI->getOperand(2).setSubReg(SubReg1);
292 MI->getOperand(1).setSubReg(SubReg2);
Chris Lattner60055892007-12-30 21:56:09 +0000293 MI->getOperand(2).setIsKill(Reg1IsKill);
294 MI->getOperand(1).setIsKill(Reg2IsKill);
Andrew Trickc416ba62010-12-24 04:28:06 +0000295
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000296 // Swap the mask around.
Chris Lattner5c463782007-12-30 20:49:49 +0000297 MI->getOperand(4).setImm((ME+1) & 31);
298 MI->getOperand(5).setImm((MB-1) & 31);
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000299 return MI;
300}
Chris Lattnerea79d9fd732006-03-05 23:49:55 +0000301
Hal Finkel6c32ff32014-03-25 19:26:43 +0000302bool PPCInstrInfo::findCommutedOpIndices(MachineInstr *MI, unsigned &SrcOpIdx1,
303 unsigned &SrcOpIdx2) const {
304 // For VSX A-Type FMA instructions, it is the first two operands that can be
305 // commuted, however, because the non-encoded tied input operand is listed
306 // first, the operands to swap are actually the second and third.
307
308 int AltOpc = PPC::getAltVSXFMAOpcode(MI->getOpcode());
309 if (AltOpc == -1)
310 return TargetInstrInfo::findCommutedOpIndices(MI, SrcOpIdx1, SrcOpIdx2);
311
312 SrcOpIdx1 = 2;
313 SrcOpIdx2 = 3;
314 return true;
315}
316
Andrew Trickc416ba62010-12-24 04:28:06 +0000317void PPCInstrInfo::insertNoop(MachineBasicBlock &MBB,
Chris Lattnerea79d9fd732006-03-05 23:49:55 +0000318 MachineBasicBlock::iterator MI) const {
Hal Finkelceb1f122013-12-12 00:19:11 +0000319 // This function is used for scheduling, and the nop wanted here is the type
320 // that terminates dispatch groups on the POWER cores.
Eric Christopher1dcea732014-06-12 21:48:52 +0000321 unsigned Directive = Subtarget.getDarwinDirective();
Hal Finkelceb1f122013-12-12 00:19:11 +0000322 unsigned Opcode;
323 switch (Directive) {
324 default: Opcode = PPC::NOP; break;
325 case PPC::DIR_PWR6: Opcode = PPC::NOP_GT_PWR6; break;
326 case PPC::DIR_PWR7: Opcode = PPC::NOP_GT_PWR7; break;
Will Schmidt970ff642014-06-26 13:36:19 +0000327 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 +0000328 }
Chris Lattnera47294ed2006-10-13 21:21:17 +0000329
Hal Finkelceb1f122013-12-12 00:19:11 +0000330 DebugLoc DL;
331 BuildMI(MBB, MI, DL, get(Opcode));
332}
Chris Lattnera47294ed2006-10-13 21:21:17 +0000333
Joerg Sonnenberger7ee0f312014-08-08 19:13:23 +0000334/// getNoopForMachoTarget - Return the noop instruction to use for a noop.
335void PPCInstrInfo::getNoopForMachoTarget(MCInst &NopInst) const {
336 NopInst.setOpcode(PPC::NOP);
337}
338
Chris Lattnera47294ed2006-10-13 21:21:17 +0000339// Branch analysis.
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000340// Note: If the condition register is set to CTR or CTR8 then this is a
341// BDNZ (imm == 1) or BDZ (imm == 0) branch.
Chris Lattnera47294ed2006-10-13 21:21:17 +0000342bool PPCInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
343 MachineBasicBlock *&FBB,
Evan Cheng64dfcac2009-02-09 07:14:22 +0000344 SmallVectorImpl<MachineOperand> &Cond,
345 bool AllowModify) const {
Eric Christopher1dcea732014-06-12 21:48:52 +0000346 bool isPPC64 = Subtarget.isPPC64();
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000347
Chris Lattnera47294ed2006-10-13 21:21:17 +0000348 // If the block has no terminators, it just falls into the block after it.
349 MachineBasicBlock::iterator I = MBB.end();
Dale Johannesen4244d122010-04-02 01:38:09 +0000350 if (I == MBB.begin())
351 return false;
352 --I;
353 while (I->isDebugValue()) {
354 if (I == MBB.begin())
355 return false;
356 --I;
357 }
358 if (!isUnpredicatedTerminator(I))
Chris Lattnera47294ed2006-10-13 21:21:17 +0000359 return false;
360
361 // Get the last instruction in the block.
362 MachineInstr *LastInst = I;
Andrew Trickc416ba62010-12-24 04:28:06 +0000363
Chris Lattnera47294ed2006-10-13 21:21:17 +0000364 // If there is only one terminator instruction, process it.
Evan Cheng5514bbe2007-06-08 21:59:56 +0000365 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
Chris Lattnera47294ed2006-10-13 21:21:17 +0000366 if (LastInst->getOpcode() == PPC::B) {
Evan Cheng8f43afd2009-05-08 23:09:25 +0000367 if (!LastInst->getOperand(0).isMBB())
368 return true;
Chris Lattnera5bb3702007-12-30 23:10:15 +0000369 TBB = LastInst->getOperand(0).getMBB();
Chris Lattnera47294ed2006-10-13 21:21:17 +0000370 return false;
Chris Lattnere0263792006-11-17 22:14:47 +0000371 } else if (LastInst->getOpcode() == PPC::BCC) {
Evan Cheng8f43afd2009-05-08 23:09:25 +0000372 if (!LastInst->getOperand(2).isMBB())
373 return true;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000374 // Block ends with fall-through condbranch.
Chris Lattnera5bb3702007-12-30 23:10:15 +0000375 TBB = LastInst->getOperand(2).getMBB();
Chris Lattnera47294ed2006-10-13 21:21:17 +0000376 Cond.push_back(LastInst->getOperand(0));
377 Cond.push_back(LastInst->getOperand(1));
Chris Lattner23f22de2006-10-21 06:03:11 +0000378 return false;
Hal Finkel940ab932014-02-28 00:27:01 +0000379 } else if (LastInst->getOpcode() == PPC::BC) {
380 if (!LastInst->getOperand(1).isMBB())
381 return true;
382 // Block ends with fall-through condbranch.
383 TBB = LastInst->getOperand(1).getMBB();
384 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET));
385 Cond.push_back(LastInst->getOperand(0));
386 return false;
387 } else if (LastInst->getOpcode() == PPC::BCn) {
388 if (!LastInst->getOperand(1).isMBB())
389 return true;
390 // Block ends with fall-through condbranch.
391 TBB = LastInst->getOperand(1).getMBB();
392 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_UNSET));
393 Cond.push_back(LastInst->getOperand(0));
394 return false;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000395 } else if (LastInst->getOpcode() == PPC::BDNZ8 ||
396 LastInst->getOpcode() == PPC::BDNZ) {
397 if (!LastInst->getOperand(0).isMBB())
398 return true;
Hal Finkelc6b5deb2012-06-08 19:19:53 +0000399 if (DisableCTRLoopAnal)
Hal Finkel821e0012012-06-08 15:38:25 +0000400 return true;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000401 TBB = LastInst->getOperand(0).getMBB();
402 Cond.push_back(MachineOperand::CreateImm(1));
403 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
404 true));
405 return false;
406 } else if (LastInst->getOpcode() == PPC::BDZ8 ||
407 LastInst->getOpcode() == PPC::BDZ) {
408 if (!LastInst->getOperand(0).isMBB())
409 return true;
Hal Finkelc6b5deb2012-06-08 19:19:53 +0000410 if (DisableCTRLoopAnal)
Hal Finkel821e0012012-06-08 15:38:25 +0000411 return true;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000412 TBB = LastInst->getOperand(0).getMBB();
413 Cond.push_back(MachineOperand::CreateImm(0));
414 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
415 true));
416 return false;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000417 }
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000418
Chris Lattnera47294ed2006-10-13 21:21:17 +0000419 // Otherwise, don't know what this is.
420 return true;
421 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000422
Chris Lattnera47294ed2006-10-13 21:21:17 +0000423 // Get the instruction before it if it's a terminator.
424 MachineInstr *SecondLastInst = I;
425
426 // If there are three terminators, we don't know what sort of block this is.
427 if (SecondLastInst && I != MBB.begin() &&
Evan Cheng5514bbe2007-06-08 21:59:56 +0000428 isUnpredicatedTerminator(--I))
Chris Lattnera47294ed2006-10-13 21:21:17 +0000429 return true;
Andrew Trickc416ba62010-12-24 04:28:06 +0000430
Chris Lattnere0263792006-11-17 22:14:47 +0000431 // If the block ends with PPC::B and PPC:BCC, handle it.
Andrew Trickc416ba62010-12-24 04:28:06 +0000432 if (SecondLastInst->getOpcode() == PPC::BCC &&
Chris Lattnera47294ed2006-10-13 21:21:17 +0000433 LastInst->getOpcode() == PPC::B) {
Evan Cheng8f43afd2009-05-08 23:09:25 +0000434 if (!SecondLastInst->getOperand(2).isMBB() ||
435 !LastInst->getOperand(0).isMBB())
436 return true;
Chris Lattnera5bb3702007-12-30 23:10:15 +0000437 TBB = SecondLastInst->getOperand(2).getMBB();
Chris Lattnera47294ed2006-10-13 21:21:17 +0000438 Cond.push_back(SecondLastInst->getOperand(0));
439 Cond.push_back(SecondLastInst->getOperand(1));
Chris Lattnera5bb3702007-12-30 23:10:15 +0000440 FBB = LastInst->getOperand(0).getMBB();
Chris Lattnera47294ed2006-10-13 21:21:17 +0000441 return false;
Hal Finkel940ab932014-02-28 00:27:01 +0000442 } else if (SecondLastInst->getOpcode() == PPC::BC &&
443 LastInst->getOpcode() == PPC::B) {
444 if (!SecondLastInst->getOperand(1).isMBB() ||
445 !LastInst->getOperand(0).isMBB())
446 return true;
447 TBB = SecondLastInst->getOperand(1).getMBB();
448 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET));
449 Cond.push_back(SecondLastInst->getOperand(0));
450 FBB = LastInst->getOperand(0).getMBB();
451 return false;
452 } else if (SecondLastInst->getOpcode() == PPC::BCn &&
453 LastInst->getOpcode() == PPC::B) {
454 if (!SecondLastInst->getOperand(1).isMBB() ||
455 !LastInst->getOperand(0).isMBB())
456 return true;
457 TBB = SecondLastInst->getOperand(1).getMBB();
458 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_UNSET));
459 Cond.push_back(SecondLastInst->getOperand(0));
460 FBB = LastInst->getOperand(0).getMBB();
461 return false;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000462 } else if ((SecondLastInst->getOpcode() == PPC::BDNZ8 ||
463 SecondLastInst->getOpcode() == PPC::BDNZ) &&
464 LastInst->getOpcode() == PPC::B) {
465 if (!SecondLastInst->getOperand(0).isMBB() ||
466 !LastInst->getOperand(0).isMBB())
467 return true;
Hal Finkelc6b5deb2012-06-08 19:19:53 +0000468 if (DisableCTRLoopAnal)
Hal Finkel821e0012012-06-08 15:38:25 +0000469 return true;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000470 TBB = SecondLastInst->getOperand(0).getMBB();
471 Cond.push_back(MachineOperand::CreateImm(1));
472 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
473 true));
474 FBB = LastInst->getOperand(0).getMBB();
475 return false;
476 } else if ((SecondLastInst->getOpcode() == PPC::BDZ8 ||
477 SecondLastInst->getOpcode() == PPC::BDZ) &&
478 LastInst->getOpcode() == PPC::B) {
479 if (!SecondLastInst->getOperand(0).isMBB() ||
480 !LastInst->getOperand(0).isMBB())
481 return true;
Hal Finkelc6b5deb2012-06-08 19:19:53 +0000482 if (DisableCTRLoopAnal)
Hal Finkel821e0012012-06-08 15:38:25 +0000483 return true;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000484 TBB = SecondLastInst->getOperand(0).getMBB();
485 Cond.push_back(MachineOperand::CreateImm(0));
486 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
487 true));
488 FBB = LastInst->getOperand(0).getMBB();
489 return false;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000490 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000491
Dale Johannesenc6855462007-06-13 17:59:52 +0000492 // If the block ends with two PPC:Bs, handle it. The second one is not
493 // executed, so remove it.
Andrew Trickc416ba62010-12-24 04:28:06 +0000494 if (SecondLastInst->getOpcode() == PPC::B &&
Dale Johannesenc6855462007-06-13 17:59:52 +0000495 LastInst->getOpcode() == PPC::B) {
Evan Cheng8f43afd2009-05-08 23:09:25 +0000496 if (!SecondLastInst->getOperand(0).isMBB())
497 return true;
Chris Lattnera5bb3702007-12-30 23:10:15 +0000498 TBB = SecondLastInst->getOperand(0).getMBB();
Dale Johannesenc6855462007-06-13 17:59:52 +0000499 I = LastInst;
Evan Cheng64dfcac2009-02-09 07:14:22 +0000500 if (AllowModify)
501 I->eraseFromParent();
Dale Johannesenc6855462007-06-13 17:59:52 +0000502 return false;
503 }
504
Chris Lattnera47294ed2006-10-13 21:21:17 +0000505 // Otherwise, can't handle this.
506 return true;
507}
508
Evan Cheng99be49d2007-05-18 00:05:48 +0000509unsigned PPCInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
Chris Lattnera47294ed2006-10-13 21:21:17 +0000510 MachineBasicBlock::iterator I = MBB.end();
Evan Cheng99be49d2007-05-18 00:05:48 +0000511 if (I == MBB.begin()) return 0;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000512 --I;
Dale Johannesen4244d122010-04-02 01:38:09 +0000513 while (I->isDebugValue()) {
514 if (I == MBB.begin())
515 return 0;
516 --I;
517 }
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000518 if (I->getOpcode() != PPC::B && I->getOpcode() != PPC::BCC &&
Hal Finkel940ab932014-02-28 00:27:01 +0000519 I->getOpcode() != PPC::BC && I->getOpcode() != PPC::BCn &&
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000520 I->getOpcode() != PPC::BDNZ8 && I->getOpcode() != PPC::BDNZ &&
521 I->getOpcode() != PPC::BDZ8 && I->getOpcode() != PPC::BDZ)
Evan Cheng99be49d2007-05-18 00:05:48 +0000522 return 0;
Andrew Trickc416ba62010-12-24 04:28:06 +0000523
Chris Lattnera47294ed2006-10-13 21:21:17 +0000524 // Remove the branch.
525 I->eraseFromParent();
Andrew Trickc416ba62010-12-24 04:28:06 +0000526
Chris Lattnera47294ed2006-10-13 21:21:17 +0000527 I = MBB.end();
528
Evan Cheng99be49d2007-05-18 00:05:48 +0000529 if (I == MBB.begin()) return 1;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000530 --I;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000531 if (I->getOpcode() != PPC::BCC &&
Hal Finkel940ab932014-02-28 00:27:01 +0000532 I->getOpcode() != PPC::BC && I->getOpcode() != PPC::BCn &&
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000533 I->getOpcode() != PPC::BDNZ8 && I->getOpcode() != PPC::BDNZ &&
534 I->getOpcode() != PPC::BDZ8 && I->getOpcode() != PPC::BDZ)
Evan Cheng99be49d2007-05-18 00:05:48 +0000535 return 1;
Andrew Trickc416ba62010-12-24 04:28:06 +0000536
Chris Lattnera47294ed2006-10-13 21:21:17 +0000537 // Remove the branch.
538 I->eraseFromParent();
Evan Cheng99be49d2007-05-18 00:05:48 +0000539 return 2;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000540}
541
Evan Cheng99be49d2007-05-18 00:05:48 +0000542unsigned
543PPCInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
544 MachineBasicBlock *FBB,
Stuart Hastings0125b642010-06-17 22:43:56 +0000545 const SmallVectorImpl<MachineOperand> &Cond,
546 DebugLoc DL) const {
Chris Lattnera61f0102006-10-17 18:06:55 +0000547 // Shouldn't be a fall through.
548 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Andrew Trickc416ba62010-12-24 04:28:06 +0000549 assert((Cond.size() == 2 || Cond.size() == 0) &&
Chris Lattner94e04442006-10-21 05:36:13 +0000550 "PPC branch conditions have two components!");
Andrew Trickc416ba62010-12-24 04:28:06 +0000551
Eric Christopher1dcea732014-06-12 21:48:52 +0000552 bool isPPC64 = Subtarget.isPPC64();
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000553
Chris Lattner94e04442006-10-21 05:36:13 +0000554 // One-way branch.
Craig Topper062a2ba2014-04-25 05:30:21 +0000555 if (!FBB) {
Chris Lattner94e04442006-10-21 05:36:13 +0000556 if (Cond.empty()) // Unconditional branch
Stuart Hastings0125b642010-06-17 22:43:56 +0000557 BuildMI(&MBB, DL, get(PPC::B)).addMBB(TBB);
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000558 else if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
559 BuildMI(&MBB, DL, get(Cond[0].getImm() ?
560 (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
561 (isPPC64 ? PPC::BDZ8 : PPC::BDZ))).addMBB(TBB);
Hal Finkel940ab932014-02-28 00:27:01 +0000562 else if (Cond[0].getImm() == PPC::PRED_BIT_SET)
563 BuildMI(&MBB, DL, get(PPC::BC)).addOperand(Cond[1]).addMBB(TBB);
564 else if (Cond[0].getImm() == PPC::PRED_BIT_UNSET)
565 BuildMI(&MBB, DL, get(PPC::BCn)).addOperand(Cond[1]).addMBB(TBB);
Chris Lattner94e04442006-10-21 05:36:13 +0000566 else // Conditional branch
Stuart Hastings0125b642010-06-17 22:43:56 +0000567 BuildMI(&MBB, DL, get(PPC::BCC))
Hal Finkel940ab932014-02-28 00:27:01 +0000568 .addImm(Cond[0].getImm()).addOperand(Cond[1]).addMBB(TBB);
Evan Cheng99be49d2007-05-18 00:05:48 +0000569 return 1;
Chris Lattnera61f0102006-10-17 18:06:55 +0000570 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000571
Chris Lattnerd8816602006-10-21 05:42:09 +0000572 // Two-way Conditional Branch.
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000573 if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
574 BuildMI(&MBB, DL, get(Cond[0].getImm() ?
575 (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
576 (isPPC64 ? PPC::BDZ8 : PPC::BDZ))).addMBB(TBB);
Hal Finkel940ab932014-02-28 00:27:01 +0000577 else if (Cond[0].getImm() == PPC::PRED_BIT_SET)
578 BuildMI(&MBB, DL, get(PPC::BC)).addOperand(Cond[1]).addMBB(TBB);
579 else if (Cond[0].getImm() == PPC::PRED_BIT_UNSET)
580 BuildMI(&MBB, DL, get(PPC::BCn)).addOperand(Cond[1]).addMBB(TBB);
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000581 else
582 BuildMI(&MBB, DL, get(PPC::BCC))
Hal Finkel940ab932014-02-28 00:27:01 +0000583 .addImm(Cond[0].getImm()).addOperand(Cond[1]).addMBB(TBB);
Stuart Hastings0125b642010-06-17 22:43:56 +0000584 BuildMI(&MBB, DL, get(PPC::B)).addMBB(FBB);
Evan Cheng99be49d2007-05-18 00:05:48 +0000585 return 2;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000586}
587
Hal Finkeled6a2852013-04-05 23:29:01 +0000588// Select analysis.
589bool PPCInstrInfo::canInsertSelect(const MachineBasicBlock &MBB,
590 const SmallVectorImpl<MachineOperand> &Cond,
591 unsigned TrueReg, unsigned FalseReg,
592 int &CondCycles, int &TrueCycles, int &FalseCycles) const {
Eric Christopher1dcea732014-06-12 21:48:52 +0000593 if (!Subtarget.hasISEL())
Hal Finkeled6a2852013-04-05 23:29:01 +0000594 return false;
595
596 if (Cond.size() != 2)
597 return false;
598
599 // If this is really a bdnz-like condition, then it cannot be turned into a
600 // select.
601 if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
602 return false;
603
604 // Check register classes.
605 const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
606 const TargetRegisterClass *RC =
607 RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg));
608 if (!RC)
609 return false;
610
611 // isel is for regular integer GPRs only.
612 if (!PPC::GPRCRegClass.hasSubClassEq(RC) &&
Hal Finkel8e8618a2013-07-15 20:22:58 +0000613 !PPC::GPRC_NOR0RegClass.hasSubClassEq(RC) &&
614 !PPC::G8RCRegClass.hasSubClassEq(RC) &&
615 !PPC::G8RC_NOX0RegClass.hasSubClassEq(RC))
Hal Finkeled6a2852013-04-05 23:29:01 +0000616 return false;
617
618 // FIXME: These numbers are for the A2, how well they work for other cores is
619 // an open question. On the A2, the isel instruction has a 2-cycle latency
620 // but single-cycle throughput. These numbers are used in combination with
621 // the MispredictPenalty setting from the active SchedMachineModel.
622 CondCycles = 1;
623 TrueCycles = 1;
624 FalseCycles = 1;
625
626 return true;
627}
628
629void PPCInstrInfo::insertSelect(MachineBasicBlock &MBB,
630 MachineBasicBlock::iterator MI, DebugLoc dl,
631 unsigned DestReg,
632 const SmallVectorImpl<MachineOperand> &Cond,
633 unsigned TrueReg, unsigned FalseReg) const {
634 assert(Cond.size() == 2 &&
635 "PPC branch conditions have two components!");
636
Eric Christopher1dcea732014-06-12 21:48:52 +0000637 assert(Subtarget.hasISEL() &&
Hal Finkeled6a2852013-04-05 23:29:01 +0000638 "Cannot insert select on target without ISEL support");
639
640 // Get the register classes.
641 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
642 const TargetRegisterClass *RC =
643 RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg));
644 assert(RC && "TrueReg and FalseReg must have overlapping register classes");
Hal Finkel8e8618a2013-07-15 20:22:58 +0000645
646 bool Is64Bit = PPC::G8RCRegClass.hasSubClassEq(RC) ||
647 PPC::G8RC_NOX0RegClass.hasSubClassEq(RC);
648 assert((Is64Bit ||
649 PPC::GPRCRegClass.hasSubClassEq(RC) ||
650 PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) &&
Hal Finkeled6a2852013-04-05 23:29:01 +0000651 "isel is for regular integer GPRs only");
652
Hal Finkel8e8618a2013-07-15 20:22:58 +0000653 unsigned OpCode = Is64Bit ? PPC::ISEL8 : PPC::ISEL;
Hal Finkeled6a2852013-04-05 23:29:01 +0000654 unsigned SelectPred = Cond[0].getImm();
655
656 unsigned SubIdx;
657 bool SwapOps;
658 switch (SelectPred) {
659 default: llvm_unreachable("invalid predicate for isel");
660 case PPC::PRED_EQ: SubIdx = PPC::sub_eq; SwapOps = false; break;
661 case PPC::PRED_NE: SubIdx = PPC::sub_eq; SwapOps = true; break;
662 case PPC::PRED_LT: SubIdx = PPC::sub_lt; SwapOps = false; break;
663 case PPC::PRED_GE: SubIdx = PPC::sub_lt; SwapOps = true; break;
664 case PPC::PRED_GT: SubIdx = PPC::sub_gt; SwapOps = false; break;
665 case PPC::PRED_LE: SubIdx = PPC::sub_gt; SwapOps = true; break;
666 case PPC::PRED_UN: SubIdx = PPC::sub_un; SwapOps = false; break;
667 case PPC::PRED_NU: SubIdx = PPC::sub_un; SwapOps = true; break;
Hal Finkel940ab932014-02-28 00:27:01 +0000668 case PPC::PRED_BIT_SET: SubIdx = 0; SwapOps = false; break;
669 case PPC::PRED_BIT_UNSET: SubIdx = 0; SwapOps = true; break;
Hal Finkeled6a2852013-04-05 23:29:01 +0000670 }
671
672 unsigned FirstReg = SwapOps ? FalseReg : TrueReg,
673 SecondReg = SwapOps ? TrueReg : FalseReg;
674
675 // The first input register of isel cannot be r0. If it is a member
676 // of a register class that can be r0, then copy it first (the
677 // register allocator should eliminate the copy).
678 if (MRI.getRegClass(FirstReg)->contains(PPC::R0) ||
679 MRI.getRegClass(FirstReg)->contains(PPC::X0)) {
680 const TargetRegisterClass *FirstRC =
681 MRI.getRegClass(FirstReg)->contains(PPC::X0) ?
682 &PPC::G8RC_NOX0RegClass : &PPC::GPRC_NOR0RegClass;
683 unsigned OldFirstReg = FirstReg;
684 FirstReg = MRI.createVirtualRegister(FirstRC);
685 BuildMI(MBB, MI, dl, get(TargetOpcode::COPY), FirstReg)
686 .addReg(OldFirstReg);
687 }
688
689 BuildMI(MBB, MI, dl, get(OpCode), DestReg)
690 .addReg(FirstReg).addReg(SecondReg)
691 .addReg(Cond[1].getReg(), 0, SubIdx);
692}
693
Jakob Stoklund Olesen0d611972010-07-11 07:31:00 +0000694void PPCInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
695 MachineBasicBlock::iterator I, DebugLoc DL,
696 unsigned DestReg, unsigned SrcReg,
697 bool KillSrc) const {
Hal Finkel27774d92014-03-13 07:58:58 +0000698 // We can end up with self copies and similar things as a result of VSX copy
Hal Finkel9dcb3582014-03-27 22:46:28 +0000699 // legalization. Promote them here.
Hal Finkel27774d92014-03-13 07:58:58 +0000700 const TargetRegisterInfo *TRI = &getRegisterInfo();
701 if (PPC::F8RCRegClass.contains(DestReg) &&
702 PPC::VSLRCRegClass.contains(SrcReg)) {
703 unsigned SuperReg =
704 TRI->getMatchingSuperReg(DestReg, PPC::sub_64, &PPC::VSRCRegClass);
705
Hal Finkel9dcb3582014-03-27 22:46:28 +0000706 if (VSXSelfCopyCrash && SrcReg == SuperReg)
707 llvm_unreachable("nop VSX copy");
Hal Finkel27774d92014-03-13 07:58:58 +0000708
709 DestReg = SuperReg;
710 } else if (PPC::VRRCRegClass.contains(DestReg) &&
711 PPC::VSHRCRegClass.contains(SrcReg)) {
712 unsigned SuperReg =
713 TRI->getMatchingSuperReg(DestReg, PPC::sub_128, &PPC::VSRCRegClass);
714
Hal Finkel9dcb3582014-03-27 22:46:28 +0000715 if (VSXSelfCopyCrash && SrcReg == SuperReg)
716 llvm_unreachable("nop VSX copy");
Hal Finkel27774d92014-03-13 07:58:58 +0000717
718 DestReg = SuperReg;
719 } else if (PPC::F8RCRegClass.contains(SrcReg) &&
720 PPC::VSLRCRegClass.contains(DestReg)) {
721 unsigned SuperReg =
722 TRI->getMatchingSuperReg(SrcReg, PPC::sub_64, &PPC::VSRCRegClass);
723
Hal Finkel9dcb3582014-03-27 22:46:28 +0000724 if (VSXSelfCopyCrash && DestReg == SuperReg)
725 llvm_unreachable("nop VSX copy");
Hal Finkel27774d92014-03-13 07:58:58 +0000726
727 SrcReg = SuperReg;
728 } else if (PPC::VRRCRegClass.contains(SrcReg) &&
729 PPC::VSHRCRegClass.contains(DestReg)) {
730 unsigned SuperReg =
731 TRI->getMatchingSuperReg(SrcReg, PPC::sub_128, &PPC::VSRCRegClass);
732
Hal Finkel9dcb3582014-03-27 22:46:28 +0000733 if (VSXSelfCopyCrash && DestReg == SuperReg)
734 llvm_unreachable("nop VSX copy");
Hal Finkel27774d92014-03-13 07:58:58 +0000735
736 SrcReg = SuperReg;
737 }
738
Jakob Stoklund Olesen0d611972010-07-11 07:31:00 +0000739 unsigned Opc;
740 if (PPC::GPRCRegClass.contains(DestReg, SrcReg))
741 Opc = PPC::OR;
742 else if (PPC::G8RCRegClass.contains(DestReg, SrcReg))
743 Opc = PPC::OR8;
744 else if (PPC::F4RCRegClass.contains(DestReg, SrcReg))
745 Opc = PPC::FMR;
746 else if (PPC::CRRCRegClass.contains(DestReg, SrcReg))
747 Opc = PPC::MCRF;
748 else if (PPC::VRRCRegClass.contains(DestReg, SrcReg))
749 Opc = PPC::VOR;
Hal Finkel27774d92014-03-13 07:58:58 +0000750 else if (PPC::VSRCRegClass.contains(DestReg, SrcReg))
Hal Finkelbbad2332014-03-24 09:36:36 +0000751 // There are two different ways this can be done:
Hal Finkel27774d92014-03-13 07:58:58 +0000752 // 1. xxlor : This has lower latency (on the P7), 2 cycles, but can only
753 // issue in VSU pipeline 0.
754 // 2. xmovdp/xmovsp: This has higher latency (on the P7), 6 cycles, but
755 // can go to either pipeline.
Hal Finkelbbad2332014-03-24 09:36:36 +0000756 // We'll always use xxlor here, because in practically all cases where
757 // copies are generated, they are close enough to some use that the
758 // lower-latency form is preferable.
Hal Finkel27774d92014-03-13 07:58:58 +0000759 Opc = PPC::XXLOR;
Hal Finkel19be5062014-03-29 05:29:01 +0000760 else if (PPC::VSFRCRegClass.contains(DestReg, SrcReg))
761 Opc = PPC::XXLORf;
Jakob Stoklund Olesen0d611972010-07-11 07:31:00 +0000762 else if (PPC::CRBITRCRegClass.contains(DestReg, SrcReg))
763 Opc = PPC::CROR;
764 else
765 llvm_unreachable("Impossible reg-to-reg copy");
Owen Anderson7a73ae92007-12-31 06:32:00 +0000766
Evan Cheng6cc775f2011-06-28 19:10:37 +0000767 const MCInstrDesc &MCID = get(Opc);
768 if (MCID.getNumOperands() == 3)
769 BuildMI(MBB, I, DL, MCID, DestReg)
Jakob Stoklund Olesen0d611972010-07-11 07:31:00 +0000770 .addReg(SrcReg).addReg(SrcReg, getKillRegState(KillSrc));
771 else
Evan Cheng6cc775f2011-06-28 19:10:37 +0000772 BuildMI(MBB, I, DL, MCID, DestReg).addReg(SrcReg, getKillRegState(KillSrc));
Owen Anderson7a73ae92007-12-31 06:32:00 +0000773}
774
Hal Finkel8f6834d2011-12-05 17:55:17 +0000775// This function returns true if a CR spill is necessary and false otherwise.
Bill Wendlingc6c48fc2008-03-10 22:49:16 +0000776bool
Dan Gohman3b460302008-07-07 23:14:23 +0000777PPCInstrInfo::StoreRegToStackSlot(MachineFunction &MF,
778 unsigned SrcReg, bool isKill,
Bill Wendlingc6c48fc2008-03-10 22:49:16 +0000779 int FrameIdx,
780 const TargetRegisterClass *RC,
Hal Finkelfcc51d42013-03-17 04:43:44 +0000781 SmallVectorImpl<MachineInstr*> &NewMIs,
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000782 bool &NonRI, bool &SpillsVRS) const{
Hal Finkel37714b82013-03-27 21:21:15 +0000783 // Note: If additional store instructions are added here,
784 // update isStoreToStackSlot.
785
Chris Lattner6f306d72010-04-02 20:16:16 +0000786 DebugLoc DL;
Hal Finkel4e703bc2014-01-28 05:32:58 +0000787 if (PPC::GPRCRegClass.hasSubClassEq(RC) ||
788 PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) {
Hal Finkel794e05b2013-03-23 17:14:27 +0000789 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
790 .addReg(SrcReg,
791 getKillRegState(isKill)),
792 FrameIdx));
Hal Finkel4e703bc2014-01-28 05:32:58 +0000793 } else if (PPC::G8RCRegClass.hasSubClassEq(RC) ||
794 PPC::G8RC_NOX0RegClass.hasSubClassEq(RC)) {
Hal Finkel794e05b2013-03-23 17:14:27 +0000795 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STD))
796 .addReg(SrcReg,
797 getKillRegState(isKill)),
798 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000799 } else if (PPC::F8RCRegClass.hasSubClassEq(RC)) {
Dale Johannesen6b8c76a2009-02-12 23:08:38 +0000800 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFD))
Bill Wendlingf7b83c72009-05-13 21:33:08 +0000801 .addReg(SrcReg,
802 getKillRegState(isKill)),
803 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000804 } else if (PPC::F4RCRegClass.hasSubClassEq(RC)) {
Dale Johannesen6b8c76a2009-02-12 23:08:38 +0000805 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFS))
Bill Wendlingf7b83c72009-05-13 21:33:08 +0000806 .addReg(SrcReg,
807 getKillRegState(isKill)),
808 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000809 } else if (PPC::CRRCRegClass.hasSubClassEq(RC)) {
Hal Finkele154c8f2013-03-12 14:12:16 +0000810 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CR))
811 .addReg(SrcReg,
812 getKillRegState(isKill)),
813 FrameIdx));
814 return true;
Craig Topperabadc662012-04-20 06:31:50 +0000815 } else if (PPC::CRBITRCRegClass.hasSubClassEq(RC)) {
Hal Finkel940ab932014-02-28 00:27:01 +0000816 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CRBIT))
817 .addReg(SrcReg,
818 getKillRegState(isKill)),
819 FrameIdx));
820 return true;
Craig Topperabadc662012-04-20 06:31:50 +0000821 } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) {
Hal Finkelfcc51d42013-03-17 04:43:44 +0000822 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STVX))
823 .addReg(SrcReg,
824 getKillRegState(isKill)),
825 FrameIdx));
826 NonRI = true;
Hal Finkel27774d92014-03-13 07:58:58 +0000827 } else if (PPC::VSRCRegClass.hasSubClassEq(RC)) {
828 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STXVD2X))
829 .addReg(SrcReg,
830 getKillRegState(isKill)),
831 FrameIdx));
832 NonRI = true;
Hal Finkel19be5062014-03-29 05:29:01 +0000833 } else if (PPC::VSFRCRegClass.hasSubClassEq(RC)) {
834 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STXSDX))
835 .addReg(SrcReg,
836 getKillRegState(isKill)),
837 FrameIdx));
838 NonRI = true;
Hal Finkela1431df2013-03-21 19:03:21 +0000839 } else if (PPC::VRSAVERCRegClass.hasSubClassEq(RC)) {
Eric Christopher1dcea732014-06-12 21:48:52 +0000840 assert(Subtarget.isDarwin() &&
Hal Finkela7b06302013-03-27 00:02:20 +0000841 "VRSAVE only needs spill/restore on Darwin");
Hal Finkela1431df2013-03-21 19:03:21 +0000842 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_VRSAVE))
843 .addReg(SrcReg,
844 getKillRegState(isKill)),
845 FrameIdx));
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000846 SpillsVRS = true;
Owen Andersoneee14602008-01-01 21:11:32 +0000847 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000848 llvm_unreachable("Unknown regclass!");
Owen Andersoneee14602008-01-01 21:11:32 +0000849 }
Bill Wendling632ea652008-03-03 22:19:16 +0000850
851 return false;
Owen Andersoneee14602008-01-01 21:11:32 +0000852}
853
854void
855PPCInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
Bill Wendling632ea652008-03-03 22:19:16 +0000856 MachineBasicBlock::iterator MI,
857 unsigned SrcReg, bool isKill, int FrameIdx,
Evan Chengefb126a2010-05-06 19:06:44 +0000858 const TargetRegisterClass *RC,
859 const TargetRegisterInfo *TRI) const {
Dan Gohman3b460302008-07-07 23:14:23 +0000860 MachineFunction &MF = *MBB.getParent();
Owen Andersoneee14602008-01-01 21:11:32 +0000861 SmallVector<MachineInstr*, 4> NewMIs;
Bill Wendling632ea652008-03-03 22:19:16 +0000862
Hal Finkelbb420f12013-03-15 05:06:04 +0000863 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
864 FuncInfo->setHasSpills();
865
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000866 bool NonRI = false, SpillsVRS = false;
867 if (StoreRegToStackSlot(MF, SrcReg, isKill, FrameIdx, RC, NewMIs,
868 NonRI, SpillsVRS))
Bill Wendling632ea652008-03-03 22:19:16 +0000869 FuncInfo->setSpillsCR();
Bill Wendling632ea652008-03-03 22:19:16 +0000870
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000871 if (SpillsVRS)
872 FuncInfo->setSpillsVRSAVE();
873
Hal Finkelfcc51d42013-03-17 04:43:44 +0000874 if (NonRI)
875 FuncInfo->setHasNonRISpills();
876
Owen Andersoneee14602008-01-01 21:11:32 +0000877 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
878 MBB.insert(MI, NewMIs[i]);
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +0000879
880 const MachineFrameInfo &MFI = *MF.getFrameInfo();
881 MachineMemOperand *MMO =
Jay Foad465101b2011-11-15 07:34:52 +0000882 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx),
Chris Lattnere3d864b2010-09-21 04:39:43 +0000883 MachineMemOperand::MOStore,
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +0000884 MFI.getObjectSize(FrameIdx),
885 MFI.getObjectAlignment(FrameIdx));
886 NewMIs.back()->addMemOperand(MF, MMO);
Owen Andersoneee14602008-01-01 21:11:32 +0000887}
888
Hal Finkelbde7f8f2011-12-06 20:55:36 +0000889bool
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000890PPCInstrInfo::LoadRegFromStackSlot(MachineFunction &MF, DebugLoc DL,
Dan Gohman3b460302008-07-07 23:14:23 +0000891 unsigned DestReg, int FrameIdx,
Bill Wendlingc6c48fc2008-03-10 22:49:16 +0000892 const TargetRegisterClass *RC,
Hal Finkelfcc51d42013-03-17 04:43:44 +0000893 SmallVectorImpl<MachineInstr*> &NewMIs,
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000894 bool &NonRI, bool &SpillsVRS) const{
Hal Finkel37714b82013-03-27 21:21:15 +0000895 // Note: If additional load instructions are added here,
896 // update isLoadFromStackSlot.
897
Hal Finkel4e703bc2014-01-28 05:32:58 +0000898 if (PPC::GPRCRegClass.hasSubClassEq(RC) ||
899 PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) {
Hal Finkel5791f512013-03-27 19:10:40 +0000900 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ),
901 DestReg), FrameIdx));
Hal Finkel4e703bc2014-01-28 05:32:58 +0000902 } else if (PPC::G8RCRegClass.hasSubClassEq(RC) ||
903 PPC::G8RC_NOX0RegClass.hasSubClassEq(RC)) {
Hal Finkel5791f512013-03-27 19:10:40 +0000904 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LD), DestReg),
905 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000906 } else if (PPC::F8RCRegClass.hasSubClassEq(RC)) {
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000907 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFD), DestReg),
Owen Andersoneee14602008-01-01 21:11:32 +0000908 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000909 } else if (PPC::F4RCRegClass.hasSubClassEq(RC)) {
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000910 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFS), DestReg),
Owen Andersoneee14602008-01-01 21:11:32 +0000911 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000912 } else if (PPC::CRRCRegClass.hasSubClassEq(RC)) {
Hal Finkele154c8f2013-03-12 14:12:16 +0000913 NewMIs.push_back(addFrameReference(BuildMI(MF, DL,
914 get(PPC::RESTORE_CR), DestReg),
915 FrameIdx));
916 return true;
Craig Topperabadc662012-04-20 06:31:50 +0000917 } else if (PPC::CRBITRCRegClass.hasSubClassEq(RC)) {
Hal Finkel940ab932014-02-28 00:27:01 +0000918 NewMIs.push_back(addFrameReference(BuildMI(MF, DL,
919 get(PPC::RESTORE_CRBIT), DestReg),
920 FrameIdx));
921 return true;
Craig Topperabadc662012-04-20 06:31:50 +0000922 } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) {
Hal Finkelfcc51d42013-03-17 04:43:44 +0000923 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LVX), DestReg),
924 FrameIdx));
925 NonRI = true;
Hal Finkel27774d92014-03-13 07:58:58 +0000926 } else if (PPC::VSRCRegClass.hasSubClassEq(RC)) {
927 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LXVD2X), DestReg),
928 FrameIdx));
929 NonRI = true;
Hal Finkel19be5062014-03-29 05:29:01 +0000930 } else if (PPC::VSFRCRegClass.hasSubClassEq(RC)) {
931 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LXSDX), DestReg),
932 FrameIdx));
933 NonRI = true;
Hal Finkela1431df2013-03-21 19:03:21 +0000934 } else if (PPC::VRSAVERCRegClass.hasSubClassEq(RC)) {
Eric Christopher1dcea732014-06-12 21:48:52 +0000935 assert(Subtarget.isDarwin() &&
Hal Finkela7b06302013-03-27 00:02:20 +0000936 "VRSAVE only needs spill/restore on Darwin");
Hal Finkela1431df2013-03-21 19:03:21 +0000937 NewMIs.push_back(addFrameReference(BuildMI(MF, DL,
938 get(PPC::RESTORE_VRSAVE),
939 DestReg),
940 FrameIdx));
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000941 SpillsVRS = true;
Owen Andersoneee14602008-01-01 21:11:32 +0000942 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000943 llvm_unreachable("Unknown regclass!");
Owen Andersoneee14602008-01-01 21:11:32 +0000944 }
Hal Finkelbde7f8f2011-12-06 20:55:36 +0000945
946 return false;
Owen Andersoneee14602008-01-01 21:11:32 +0000947}
948
949void
950PPCInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
Bill Wendling632ea652008-03-03 22:19:16 +0000951 MachineBasicBlock::iterator MI,
952 unsigned DestReg, int FrameIdx,
Evan Chengefb126a2010-05-06 19:06:44 +0000953 const TargetRegisterClass *RC,
954 const TargetRegisterInfo *TRI) const {
Dan Gohman3b460302008-07-07 23:14:23 +0000955 MachineFunction &MF = *MBB.getParent();
Owen Andersoneee14602008-01-01 21:11:32 +0000956 SmallVector<MachineInstr*, 4> NewMIs;
Chris Lattner6f306d72010-04-02 20:16:16 +0000957 DebugLoc DL;
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000958 if (MI != MBB.end()) DL = MI->getDebugLoc();
Hal Finkelfcc51d42013-03-17 04:43:44 +0000959
960 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
961 FuncInfo->setHasSpills();
962
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000963 bool NonRI = false, SpillsVRS = false;
964 if (LoadRegFromStackSlot(MF, DL, DestReg, FrameIdx, RC, NewMIs,
965 NonRI, SpillsVRS))
Hal Finkelbde7f8f2011-12-06 20:55:36 +0000966 FuncInfo->setSpillsCR();
Hal Finkelfcc51d42013-03-17 04:43:44 +0000967
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000968 if (SpillsVRS)
969 FuncInfo->setSpillsVRSAVE();
970
Hal Finkelfcc51d42013-03-17 04:43:44 +0000971 if (NonRI)
972 FuncInfo->setHasNonRISpills();
973
Owen Andersoneee14602008-01-01 21:11:32 +0000974 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
975 MBB.insert(MI, NewMIs[i]);
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +0000976
977 const MachineFrameInfo &MFI = *MF.getFrameInfo();
978 MachineMemOperand *MMO =
Jay Foad465101b2011-11-15 07:34:52 +0000979 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx),
Chris Lattnere3d864b2010-09-21 04:39:43 +0000980 MachineMemOperand::MOLoad,
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +0000981 MFI.getObjectSize(FrameIdx),
982 MFI.getObjectAlignment(FrameIdx));
983 NewMIs.back()->addMemOperand(MF, MMO);
Owen Andersoneee14602008-01-01 21:11:32 +0000984}
985
Chris Lattnera47294ed2006-10-13 21:21:17 +0000986bool PPCInstrInfo::
Owen Anderson4f6bf042008-08-14 22:49:33 +0000987ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
Chris Lattner23f22de2006-10-21 06:03:11 +0000988 assert(Cond.size() == 2 && "Invalid PPC branch opcode!");
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000989 if (Cond[1].getReg() == PPC::CTR8 || Cond[1].getReg() == PPC::CTR)
990 Cond[0].setImm(Cond[0].getImm() == 0 ? 1 : 0);
991 else
992 // Leave the CR# the same, but invert the condition.
993 Cond[0].setImm(PPC::InvertPredicate((PPC::Predicate)Cond[0].getImm()));
Chris Lattner23f22de2006-10-21 06:03:11 +0000994 return false;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000995}
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +0000996
Hal Finkeld61d4f82013-04-06 19:30:30 +0000997bool PPCInstrInfo::FoldImmediate(MachineInstr *UseMI, MachineInstr *DefMI,
998 unsigned Reg, MachineRegisterInfo *MRI) const {
999 // For some instructions, it is legal to fold ZERO into the RA register field.
1000 // A zero immediate should always be loaded with a single li.
1001 unsigned DefOpc = DefMI->getOpcode();
1002 if (DefOpc != PPC::LI && DefOpc != PPC::LI8)
1003 return false;
1004 if (!DefMI->getOperand(1).isImm())
1005 return false;
1006 if (DefMI->getOperand(1).getImm() != 0)
1007 return false;
1008
1009 // Note that we cannot here invert the arguments of an isel in order to fold
1010 // a ZERO into what is presented as the second argument. All we have here
1011 // is the condition bit, and that might come from a CR-logical bit operation.
1012
1013 const MCInstrDesc &UseMCID = UseMI->getDesc();
1014
1015 // Only fold into real machine instructions.
1016 if (UseMCID.isPseudo())
1017 return false;
1018
1019 unsigned UseIdx;
1020 for (UseIdx = 0; UseIdx < UseMI->getNumOperands(); ++UseIdx)
1021 if (UseMI->getOperand(UseIdx).isReg() &&
1022 UseMI->getOperand(UseIdx).getReg() == Reg)
1023 break;
1024
1025 assert(UseIdx < UseMI->getNumOperands() && "Cannot find Reg in UseMI");
1026 assert(UseIdx < UseMCID.getNumOperands() && "No operand description for Reg");
1027
1028 const MCOperandInfo *UseInfo = &UseMCID.OpInfo[UseIdx];
1029
1030 // We can fold the zero if this register requires a GPRC_NOR0/G8RC_NOX0
1031 // register (which might also be specified as a pointer class kind).
1032 if (UseInfo->isLookupPtrRegClass()) {
1033 if (UseInfo->RegClass /* Kind */ != 1)
1034 return false;
1035 } else {
1036 if (UseInfo->RegClass != PPC::GPRC_NOR0RegClassID &&
1037 UseInfo->RegClass != PPC::G8RC_NOX0RegClassID)
1038 return false;
1039 }
1040
1041 // Make sure this is not tied to an output register (or otherwise
1042 // constrained). This is true for ST?UX registers, for example, which
1043 // are tied to their output registers.
1044 if (UseInfo->Constraints != 0)
1045 return false;
1046
1047 unsigned ZeroReg;
1048 if (UseInfo->isLookupPtrRegClass()) {
Eric Christopher1dcea732014-06-12 21:48:52 +00001049 bool isPPC64 = Subtarget.isPPC64();
Hal Finkeld61d4f82013-04-06 19:30:30 +00001050 ZeroReg = isPPC64 ? PPC::ZERO8 : PPC::ZERO;
1051 } else {
1052 ZeroReg = UseInfo->RegClass == PPC::G8RC_NOX0RegClassID ?
1053 PPC::ZERO8 : PPC::ZERO;
1054 }
1055
1056 bool DeleteDef = MRI->hasOneNonDBGUse(Reg);
1057 UseMI->getOperand(UseIdx).setReg(ZeroReg);
1058
1059 if (DeleteDef)
1060 DefMI->eraseFromParent();
1061
1062 return true;
1063}
1064
Hal Finkel30ae2292013-04-10 18:30:16 +00001065static bool MBBDefinesCTR(MachineBasicBlock &MBB) {
1066 for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end();
1067 I != IE; ++I)
1068 if (I->definesRegister(PPC::CTR) || I->definesRegister(PPC::CTR8))
1069 return true;
1070 return false;
1071}
1072
1073// We should make sure that, if we're going to predicate both sides of a
1074// condition (a diamond), that both sides don't define the counter register. We
1075// can predicate counter-decrement-based branches, but while that predicates
1076// the branching, it does not predicate the counter decrement. If we tried to
1077// merge the triangle into one predicated block, we'd decrement the counter
1078// twice.
1079bool PPCInstrInfo::isProfitableToIfCvt(MachineBasicBlock &TMBB,
1080 unsigned NumT, unsigned ExtraT,
1081 MachineBasicBlock &FMBB,
1082 unsigned NumF, unsigned ExtraF,
1083 const BranchProbability &Probability) const {
1084 return !(MBBDefinesCTR(TMBB) && MBBDefinesCTR(FMBB));
1085}
1086
1087
Hal Finkel5711eca2013-04-09 22:58:37 +00001088bool PPCInstrInfo::isPredicated(const MachineInstr *MI) const {
Hal Finkelf29285a2013-04-11 01:23:34 +00001089 // The predicated branches are identified by their type, not really by the
1090 // explicit presence of a predicate. Furthermore, some of them can be
1091 // predicated more than once. Because if conversion won't try to predicate
1092 // any instruction which already claims to be predicated (by returning true
1093 // here), always return false. In doing so, we let isPredicable() be the
1094 // final word on whether not the instruction can be (further) predicated.
1095
1096 return false;
Hal Finkel5711eca2013-04-09 22:58:37 +00001097}
1098
1099bool PPCInstrInfo::isUnpredicatedTerminator(const MachineInstr *MI) const {
1100 if (!MI->isTerminator())
1101 return false;
1102
1103 // Conditional branch is a special case.
1104 if (MI->isBranch() && !MI->isBarrier())
1105 return true;
1106
1107 return !isPredicated(MI);
1108}
1109
1110bool PPCInstrInfo::PredicateInstruction(
1111 MachineInstr *MI,
1112 const SmallVectorImpl<MachineOperand> &Pred) const {
1113 unsigned OpC = MI->getOpcode();
1114 if (OpC == PPC::BLR) {
1115 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) {
Eric Christopher1dcea732014-06-12 21:48:52 +00001116 bool isPPC64 = Subtarget.isPPC64();
Hal Finkel5711eca2013-04-09 22:58:37 +00001117 MI->setDesc(get(Pred[0].getImm() ?
1118 (isPPC64 ? PPC::BDNZLR8 : PPC::BDNZLR) :
1119 (isPPC64 ? PPC::BDZLR8 : PPC::BDZLR)));
Hal Finkel940ab932014-02-28 00:27:01 +00001120 } else if (Pred[0].getImm() == PPC::PRED_BIT_SET) {
Hal Finkel5711eca2013-04-09 22:58:37 +00001121 MI->setDesc(get(PPC::BCLR));
1122 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
Hal Finkel940ab932014-02-28 00:27:01 +00001123 .addReg(Pred[1].getReg());
1124 } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) {
1125 MI->setDesc(get(PPC::BCLRn));
1126 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1127 .addReg(Pred[1].getReg());
1128 } else {
1129 MI->setDesc(get(PPC::BCCLR));
1130 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
Hal Finkel5711eca2013-04-09 22:58:37 +00001131 .addImm(Pred[0].getImm())
1132 .addReg(Pred[1].getReg());
1133 }
1134
1135 return true;
1136 } else if (OpC == PPC::B) {
1137 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) {
Eric Christopher1dcea732014-06-12 21:48:52 +00001138 bool isPPC64 = Subtarget.isPPC64();
Hal Finkel5711eca2013-04-09 22:58:37 +00001139 MI->setDesc(get(Pred[0].getImm() ?
1140 (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
1141 (isPPC64 ? PPC::BDZ8 : PPC::BDZ)));
Hal Finkel940ab932014-02-28 00:27:01 +00001142 } else if (Pred[0].getImm() == PPC::PRED_BIT_SET) {
1143 MachineBasicBlock *MBB = MI->getOperand(0).getMBB();
1144 MI->RemoveOperand(0);
1145
1146 MI->setDesc(get(PPC::BC));
1147 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1148 .addReg(Pred[1].getReg())
1149 .addMBB(MBB);
1150 } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) {
1151 MachineBasicBlock *MBB = MI->getOperand(0).getMBB();
1152 MI->RemoveOperand(0);
1153
1154 MI->setDesc(get(PPC::BCn));
1155 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1156 .addReg(Pred[1].getReg())
1157 .addMBB(MBB);
Hal Finkel5711eca2013-04-09 22:58:37 +00001158 } else {
1159 MachineBasicBlock *MBB = MI->getOperand(0).getMBB();
1160 MI->RemoveOperand(0);
1161
1162 MI->setDesc(get(PPC::BCC));
1163 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1164 .addImm(Pred[0].getImm())
1165 .addReg(Pred[1].getReg())
1166 .addMBB(MBB);
1167 }
1168
1169 return true;
Hal Finkel500b0042013-04-10 06:42:34 +00001170 } else if (OpC == PPC::BCTR || OpC == PPC::BCTR8 ||
1171 OpC == PPC::BCTRL || OpC == PPC::BCTRL8) {
1172 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR)
1173 llvm_unreachable("Cannot predicate bctr[l] on the ctr register");
1174
1175 bool setLR = OpC == PPC::BCTRL || OpC == PPC::BCTRL8;
Eric Christopher1dcea732014-06-12 21:48:52 +00001176 bool isPPC64 = Subtarget.isPPC64();
Hal Finkel940ab932014-02-28 00:27:01 +00001177
1178 if (Pred[0].getImm() == PPC::PRED_BIT_SET) {
1179 MI->setDesc(get(isPPC64 ? (setLR ? PPC::BCCTRL8 : PPC::BCCTR8) :
1180 (setLR ? PPC::BCCTRL : PPC::BCCTR)));
1181 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1182 .addReg(Pred[1].getReg());
1183 return true;
1184 } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) {
1185 MI->setDesc(get(isPPC64 ? (setLR ? PPC::BCCTRL8n : PPC::BCCTR8n) :
1186 (setLR ? PPC::BCCTRLn : PPC::BCCTRn)));
1187 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1188 .addReg(Pred[1].getReg());
1189 return true;
1190 }
1191
1192 MI->setDesc(get(isPPC64 ? (setLR ? PPC::BCCCTRL8 : PPC::BCCCTR8) :
1193 (setLR ? PPC::BCCCTRL : PPC::BCCCTR)));
Hal Finkel500b0042013-04-10 06:42:34 +00001194 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1195 .addImm(Pred[0].getImm())
1196 .addReg(Pred[1].getReg());
1197 return true;
Hal Finkel5711eca2013-04-09 22:58:37 +00001198 }
1199
1200 return false;
1201}
1202
1203bool PPCInstrInfo::SubsumesPredicate(
1204 const SmallVectorImpl<MachineOperand> &Pred1,
1205 const SmallVectorImpl<MachineOperand> &Pred2) const {
1206 assert(Pred1.size() == 2 && "Invalid PPC first predicate");
1207 assert(Pred2.size() == 2 && "Invalid PPC second predicate");
1208
1209 if (Pred1[1].getReg() == PPC::CTR8 || Pred1[1].getReg() == PPC::CTR)
1210 return false;
1211 if (Pred2[1].getReg() == PPC::CTR8 || Pred2[1].getReg() == PPC::CTR)
1212 return false;
1213
Hal Finkel94a6f382013-12-11 23:12:25 +00001214 // P1 can only subsume P2 if they test the same condition register.
1215 if (Pred1[1].getReg() != Pred2[1].getReg())
1216 return false;
1217
Hal Finkel5711eca2013-04-09 22:58:37 +00001218 PPC::Predicate P1 = (PPC::Predicate) Pred1[0].getImm();
1219 PPC::Predicate P2 = (PPC::Predicate) Pred2[0].getImm();
1220
1221 if (P1 == P2)
1222 return true;
1223
1224 // Does P1 subsume P2, e.g. GE subsumes GT.
1225 if (P1 == PPC::PRED_LE &&
1226 (P2 == PPC::PRED_LT || P2 == PPC::PRED_EQ))
1227 return true;
1228 if (P1 == PPC::PRED_GE &&
1229 (P2 == PPC::PRED_GT || P2 == PPC::PRED_EQ))
1230 return true;
1231
1232 return false;
1233}
1234
1235bool PPCInstrInfo::DefinesPredicate(MachineInstr *MI,
1236 std::vector<MachineOperand> &Pred) const {
1237 // Note: At the present time, the contents of Pred from this function is
1238 // unused by IfConversion. This implementation follows ARM by pushing the
1239 // CR-defining operand. Because the 'DZ' and 'DNZ' count as types of
1240 // predicate, instructions defining CTR or CTR8 are also included as
1241 // predicate-defining instructions.
1242
1243 const TargetRegisterClass *RCs[] =
1244 { &PPC::CRRCRegClass, &PPC::CRBITRCRegClass,
1245 &PPC::CTRRCRegClass, &PPC::CTRRC8RegClass };
1246
1247 bool Found = false;
1248 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1249 const MachineOperand &MO = MI->getOperand(i);
Hal Finkelaf822012013-04-10 07:17:47 +00001250 for (unsigned c = 0; c < array_lengthof(RCs) && !Found; ++c) {
Hal Finkel5711eca2013-04-09 22:58:37 +00001251 const TargetRegisterClass *RC = RCs[c];
Hal Finkelaf822012013-04-10 07:17:47 +00001252 if (MO.isReg()) {
1253 if (MO.isDef() && RC->contains(MO.getReg())) {
Hal Finkel5711eca2013-04-09 22:58:37 +00001254 Pred.push_back(MO);
1255 Found = true;
1256 }
Hal Finkelaf822012013-04-10 07:17:47 +00001257 } else if (MO.isRegMask()) {
1258 for (TargetRegisterClass::iterator I = RC->begin(),
1259 IE = RC->end(); I != IE; ++I)
1260 if (MO.clobbersPhysReg(*I)) {
1261 Pred.push_back(MO);
1262 Found = true;
1263 }
Hal Finkel5711eca2013-04-09 22:58:37 +00001264 }
1265 }
1266 }
1267
1268 return Found;
1269}
1270
1271bool PPCInstrInfo::isPredicable(MachineInstr *MI) const {
1272 unsigned OpC = MI->getOpcode();
1273 switch (OpC) {
1274 default:
1275 return false;
1276 case PPC::B:
1277 case PPC::BLR:
Hal Finkel500b0042013-04-10 06:42:34 +00001278 case PPC::BCTR:
1279 case PPC::BCTR8:
1280 case PPC::BCTRL:
1281 case PPC::BCTRL8:
Hal Finkel5711eca2013-04-09 22:58:37 +00001282 return true;
1283 }
1284}
1285
Hal Finkel82656cb2013-04-18 22:15:08 +00001286bool PPCInstrInfo::analyzeCompare(const MachineInstr *MI,
1287 unsigned &SrcReg, unsigned &SrcReg2,
1288 int &Mask, int &Value) const {
1289 unsigned Opc = MI->getOpcode();
1290
1291 switch (Opc) {
1292 default: return false;
1293 case PPC::CMPWI:
1294 case PPC::CMPLWI:
1295 case PPC::CMPDI:
1296 case PPC::CMPLDI:
1297 SrcReg = MI->getOperand(1).getReg();
1298 SrcReg2 = 0;
1299 Value = MI->getOperand(2).getImm();
1300 Mask = 0xFFFF;
1301 return true;
1302 case PPC::CMPW:
1303 case PPC::CMPLW:
1304 case PPC::CMPD:
1305 case PPC::CMPLD:
1306 case PPC::FCMPUS:
1307 case PPC::FCMPUD:
1308 SrcReg = MI->getOperand(1).getReg();
1309 SrcReg2 = MI->getOperand(2).getReg();
1310 return true;
1311 }
1312}
Hal Finkele6322392013-04-19 22:08:38 +00001313
Hal Finkel82656cb2013-04-18 22:15:08 +00001314bool PPCInstrInfo::optimizeCompareInstr(MachineInstr *CmpInstr,
1315 unsigned SrcReg, unsigned SrcReg2,
1316 int Mask, int Value,
1317 const MachineRegisterInfo *MRI) const {
Hal Finkelb12da6b2013-04-18 22:54:25 +00001318 if (DisableCmpOpt)
1319 return false;
1320
Hal Finkel82656cb2013-04-18 22:15:08 +00001321 int OpC = CmpInstr->getOpcode();
1322 unsigned CRReg = CmpInstr->getOperand(0).getReg();
Hal Finkel08e53ee2013-05-08 12:16:14 +00001323
1324 // FP record forms set CR1 based on the execption status bits, not a
1325 // comparison with zero.
1326 if (OpC == PPC::FCMPUS || OpC == PPC::FCMPUD)
1327 return false;
Hal Finkel82656cb2013-04-18 22:15:08 +00001328
1329 // The record forms set the condition register based on a signed comparison
1330 // with zero (so says the ISA manual). This is not as straightforward as it
1331 // seems, however, because this is always a 64-bit comparison on PPC64, even
1332 // for instructions that are 32-bit in nature (like slw for example).
1333 // So, on PPC32, for unsigned comparisons, we can use the record forms only
1334 // for equality checks (as those don't depend on the sign). On PPC64,
1335 // we are restricted to equality for unsigned 64-bit comparisons and for
1336 // signed 32-bit comparisons the applicability is more restricted.
Eric Christopher1dcea732014-06-12 21:48:52 +00001337 bool isPPC64 = Subtarget.isPPC64();
Hal Finkel82656cb2013-04-18 22:15:08 +00001338 bool is32BitSignedCompare = OpC == PPC::CMPWI || OpC == PPC::CMPW;
1339 bool is32BitUnsignedCompare = OpC == PPC::CMPLWI || OpC == PPC::CMPLW;
1340 bool is64BitUnsignedCompare = OpC == PPC::CMPLDI || OpC == PPC::CMPLD;
1341
1342 // Get the unique definition of SrcReg.
1343 MachineInstr *MI = MRI->getUniqueVRegDef(SrcReg);
1344 if (!MI) return false;
1345 int MIOpC = MI->getOpcode();
1346
1347 bool equalityOnly = false;
1348 bool noSub = false;
1349 if (isPPC64) {
1350 if (is32BitSignedCompare) {
1351 // We can perform this optimization only if MI is sign-extending.
1352 if (MIOpC == PPC::SRAW || MIOpC == PPC::SRAWo ||
1353 MIOpC == PPC::SRAWI || MIOpC == PPC::SRAWIo ||
1354 MIOpC == PPC::EXTSB || MIOpC == PPC::EXTSBo ||
1355 MIOpC == PPC::EXTSH || MIOpC == PPC::EXTSHo ||
1356 MIOpC == PPC::EXTSW || MIOpC == PPC::EXTSWo) {
1357 noSub = true;
1358 } else
1359 return false;
1360 } else if (is32BitUnsignedCompare) {
1361 // We can perform this optimization, equality only, if MI is
1362 // zero-extending.
1363 if (MIOpC == PPC::CNTLZW || MIOpC == PPC::CNTLZWo ||
1364 MIOpC == PPC::SLW || MIOpC == PPC::SLWo ||
1365 MIOpC == PPC::SRW || MIOpC == PPC::SRWo) {
1366 noSub = true;
1367 equalityOnly = true;
1368 } else
1369 return false;
Hal Finkel08e53ee2013-05-08 12:16:14 +00001370 } else
Hal Finkel82656cb2013-04-18 22:15:08 +00001371 equalityOnly = is64BitUnsignedCompare;
Hal Finkel08e53ee2013-05-08 12:16:14 +00001372 } else
Hal Finkel82656cb2013-04-18 22:15:08 +00001373 equalityOnly = is32BitUnsignedCompare;
1374
1375 if (equalityOnly) {
1376 // We need to check the uses of the condition register in order to reject
1377 // non-equality comparisons.
Owen Anderson16c6bf42014-03-13 23:12:04 +00001378 for (MachineRegisterInfo::use_instr_iterator I =MRI->use_instr_begin(CRReg),
1379 IE = MRI->use_instr_end(); I != IE; ++I) {
Hal Finkel82656cb2013-04-18 22:15:08 +00001380 MachineInstr *UseMI = &*I;
1381 if (UseMI->getOpcode() == PPC::BCC) {
1382 unsigned Pred = UseMI->getOperand(0).getImm();
Hal Finkelc3632452013-05-07 17:49:55 +00001383 if (Pred != PPC::PRED_EQ && Pred != PPC::PRED_NE)
1384 return false;
Hal Finkel82656cb2013-04-18 22:15:08 +00001385 } else if (UseMI->getOpcode() == PPC::ISEL ||
1386 UseMI->getOpcode() == PPC::ISEL8) {
1387 unsigned SubIdx = UseMI->getOperand(3).getSubReg();
Hal Finkelc3632452013-05-07 17:49:55 +00001388 if (SubIdx != PPC::sub_eq)
1389 return false;
Hal Finkel82656cb2013-04-18 22:15:08 +00001390 } else
1391 return false;
1392 }
1393 }
1394
Hal Finkelc3632452013-05-07 17:49:55 +00001395 MachineBasicBlock::iterator I = CmpInstr;
Hal Finkel82656cb2013-04-18 22:15:08 +00001396
1397 // Scan forward to find the first use of the compare.
1398 for (MachineBasicBlock::iterator EL = CmpInstr->getParent()->end();
1399 I != EL; ++I) {
1400 bool FoundUse = false;
Owen Anderson16c6bf42014-03-13 23:12:04 +00001401 for (MachineRegisterInfo::use_instr_iterator J =MRI->use_instr_begin(CRReg),
1402 JE = MRI->use_instr_end(); J != JE; ++J)
Hal Finkel82656cb2013-04-18 22:15:08 +00001403 if (&*J == &*I) {
1404 FoundUse = true;
1405 break;
1406 }
1407
1408 if (FoundUse)
1409 break;
1410 }
1411
Hal Finkel82656cb2013-04-18 22:15:08 +00001412 // There are two possible candidates which can be changed to set CR[01].
1413 // One is MI, the other is a SUB instruction.
1414 // For CMPrr(r1,r2), we are looking for SUB(r1,r2) or SUB(r2,r1).
Craig Topper062a2ba2014-04-25 05:30:21 +00001415 MachineInstr *Sub = nullptr;
Hal Finkel82656cb2013-04-18 22:15:08 +00001416 if (SrcReg2 != 0)
1417 // MI is not a candidate for CMPrr.
Craig Topper062a2ba2014-04-25 05:30:21 +00001418 MI = nullptr;
Hal Finkel82656cb2013-04-18 22:15:08 +00001419 // FIXME: Conservatively refuse to convert an instruction which isn't in the
1420 // same BB as the comparison. This is to allow the check below to avoid calls
1421 // (and other explicit clobbers); instead we should really check for these
1422 // more explicitly (in at least a few predecessors).
1423 else if (MI->getParent() != CmpInstr->getParent() || Value != 0) {
1424 // PPC does not have a record-form SUBri.
1425 return false;
1426 }
1427
1428 // Search for Sub.
1429 const TargetRegisterInfo *TRI = &getRegisterInfo();
1430 --I;
Hal Finkelc3632452013-05-07 17:49:55 +00001431
1432 // Get ready to iterate backward from CmpInstr.
1433 MachineBasicBlock::iterator E = MI,
1434 B = CmpInstr->getParent()->begin();
1435
Hal Finkel82656cb2013-04-18 22:15:08 +00001436 for (; I != E && !noSub; --I) {
1437 const MachineInstr &Instr = *I;
1438 unsigned IOpC = Instr.getOpcode();
1439
1440 if (&*I != CmpInstr && (
Hal Finkel08e53ee2013-05-08 12:16:14 +00001441 Instr.modifiesRegister(PPC::CR0, TRI) ||
1442 Instr.readsRegister(PPC::CR0, TRI)))
Hal Finkel82656cb2013-04-18 22:15:08 +00001443 // This instruction modifies or uses the record condition register after
1444 // the one we want to change. While we could do this transformation, it
1445 // would likely not be profitable. This transformation removes one
1446 // instruction, and so even forcing RA to generate one move probably
1447 // makes it unprofitable.
1448 return false;
1449
1450 // Check whether CmpInstr can be made redundant by the current instruction.
1451 if ((OpC == PPC::CMPW || OpC == PPC::CMPLW ||
1452 OpC == PPC::CMPD || OpC == PPC::CMPLD) &&
1453 (IOpC == PPC::SUBF || IOpC == PPC::SUBF8) &&
1454 ((Instr.getOperand(1).getReg() == SrcReg &&
1455 Instr.getOperand(2).getReg() == SrcReg2) ||
1456 (Instr.getOperand(1).getReg() == SrcReg2 &&
1457 Instr.getOperand(2).getReg() == SrcReg))) {
1458 Sub = &*I;
1459 break;
1460 }
1461
Hal Finkel82656cb2013-04-18 22:15:08 +00001462 if (I == B)
1463 // The 'and' is below the comparison instruction.
1464 return false;
1465 }
1466
1467 // Return false if no candidates exist.
1468 if (!MI && !Sub)
1469 return false;
1470
1471 // The single candidate is called MI.
1472 if (!MI) MI = Sub;
1473
1474 int NewOpC = -1;
1475 MIOpC = MI->getOpcode();
1476 if (MIOpC == PPC::ANDIo || MIOpC == PPC::ANDIo8)
1477 NewOpC = MIOpC;
1478 else {
1479 NewOpC = PPC::getRecordFormOpcode(MIOpC);
1480 if (NewOpC == -1 && PPC::getNonRecordFormOpcode(MIOpC) != -1)
1481 NewOpC = MIOpC;
1482 }
1483
1484 // FIXME: On the non-embedded POWER architectures, only some of the record
1485 // forms are fast, and we should use only the fast ones.
1486
1487 // The defining instruction has a record form (or is already a record
1488 // form). It is possible, however, that we'll need to reverse the condition
1489 // code of the users.
1490 if (NewOpC == -1)
1491 return false;
1492
Hal Finkele6322392013-04-19 22:08:38 +00001493 SmallVector<std::pair<MachineOperand*, PPC::Predicate>, 4> PredsToUpdate;
1494 SmallVector<std::pair<MachineOperand*, unsigned>, 4> SubRegsToUpdate;
Hal Finkel82656cb2013-04-18 22:15:08 +00001495
1496 // If we have SUB(r1, r2) and CMP(r2, r1), the condition code based on CMP
1497 // needs to be updated to be based on SUB. Push the condition code
1498 // operands to OperandsToUpdate. If it is safe to remove CmpInstr, the
1499 // condition code of these operands will be modified.
1500 bool ShouldSwap = false;
1501 if (Sub) {
1502 ShouldSwap = SrcReg2 != 0 && Sub->getOperand(1).getReg() == SrcReg2 &&
1503 Sub->getOperand(2).getReg() == SrcReg;
1504
1505 // The operands to subf are the opposite of sub, so only in the fixed-point
1506 // case, invert the order.
Hal Finkel08e53ee2013-05-08 12:16:14 +00001507 ShouldSwap = !ShouldSwap;
Hal Finkel82656cb2013-04-18 22:15:08 +00001508 }
1509
1510 if (ShouldSwap)
Owen Anderson16c6bf42014-03-13 23:12:04 +00001511 for (MachineRegisterInfo::use_instr_iterator
1512 I = MRI->use_instr_begin(CRReg), IE = MRI->use_instr_end();
1513 I != IE; ++I) {
Hal Finkel82656cb2013-04-18 22:15:08 +00001514 MachineInstr *UseMI = &*I;
1515 if (UseMI->getOpcode() == PPC::BCC) {
1516 PPC::Predicate Pred = (PPC::Predicate) UseMI->getOperand(0).getImm();
Hal Finkele6322392013-04-19 22:08:38 +00001517 assert((!equalityOnly ||
1518 Pred == PPC::PRED_EQ || Pred == PPC::PRED_NE) &&
1519 "Invalid predicate for equality-only optimization");
Owen Anderson16c6bf42014-03-13 23:12:04 +00001520 PredsToUpdate.push_back(std::make_pair(&(UseMI->getOperand(0)),
Hal Finkel0f64e212013-04-20 05:16:26 +00001521 PPC::getSwappedPredicate(Pred)));
Hal Finkel82656cb2013-04-18 22:15:08 +00001522 } else if (UseMI->getOpcode() == PPC::ISEL ||
1523 UseMI->getOpcode() == PPC::ISEL8) {
Hal Finkele6322392013-04-19 22:08:38 +00001524 unsigned NewSubReg = UseMI->getOperand(3).getSubReg();
1525 assert((!equalityOnly || NewSubReg == PPC::sub_eq) &&
1526 "Invalid CR bit for equality-only optimization");
1527
1528 if (NewSubReg == PPC::sub_lt)
1529 NewSubReg = PPC::sub_gt;
1530 else if (NewSubReg == PPC::sub_gt)
1531 NewSubReg = PPC::sub_lt;
1532
Owen Anderson16c6bf42014-03-13 23:12:04 +00001533 SubRegsToUpdate.push_back(std::make_pair(&(UseMI->getOperand(3)),
Hal Finkele6322392013-04-19 22:08:38 +00001534 NewSubReg));
Hal Finkel82656cb2013-04-18 22:15:08 +00001535 } else // We need to abort on a user we don't understand.
1536 return false;
1537 }
1538
1539 // Create a new virtual register to hold the value of the CR set by the
1540 // record-form instruction. If the instruction was not previously in
1541 // record form, then set the kill flag on the CR.
1542 CmpInstr->eraseFromParent();
1543
1544 MachineBasicBlock::iterator MII = MI;
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001545 BuildMI(*MI->getParent(), std::next(MII), MI->getDebugLoc(),
Hal Finkel82656cb2013-04-18 22:15:08 +00001546 get(TargetOpcode::COPY), CRReg)
Hal Finkel08e53ee2013-05-08 12:16:14 +00001547 .addReg(PPC::CR0, MIOpC != NewOpC ? RegState::Kill : 0);
Hal Finkel82656cb2013-04-18 22:15:08 +00001548
1549 if (MIOpC != NewOpC) {
1550 // We need to be careful here: we're replacing one instruction with
1551 // another, and we need to make sure that we get all of the right
1552 // implicit uses and defs. On the other hand, the caller may be holding
1553 // an iterator to this instruction, and so we can't delete it (this is
1554 // specifically the case if this is the instruction directly after the
1555 // compare).
1556
1557 const MCInstrDesc &NewDesc = get(NewOpC);
1558 MI->setDesc(NewDesc);
1559
1560 if (NewDesc.ImplicitDefs)
1561 for (const uint16_t *ImpDefs = NewDesc.getImplicitDefs();
1562 *ImpDefs; ++ImpDefs)
1563 if (!MI->definesRegister(*ImpDefs))
1564 MI->addOperand(*MI->getParent()->getParent(),
1565 MachineOperand::CreateReg(*ImpDefs, true, true));
1566 if (NewDesc.ImplicitUses)
1567 for (const uint16_t *ImpUses = NewDesc.getImplicitUses();
1568 *ImpUses; ++ImpUses)
1569 if (!MI->readsRegister(*ImpUses))
1570 MI->addOperand(*MI->getParent()->getParent(),
1571 MachineOperand::CreateReg(*ImpUses, false, true));
1572 }
1573
1574 // Modify the condition code of operands in OperandsToUpdate.
1575 // Since we have SUB(r1, r2) and CMP(r2, r1), the condition code needs to
1576 // be changed from r2 > r1 to r1 < r2, from r2 < r1 to r1 > r2, etc.
Hal Finkele6322392013-04-19 22:08:38 +00001577 for (unsigned i = 0, e = PredsToUpdate.size(); i < e; i++)
1578 PredsToUpdate[i].first->setImm(PredsToUpdate[i].second);
Hal Finkel82656cb2013-04-18 22:15:08 +00001579
Hal Finkele6322392013-04-19 22:08:38 +00001580 for (unsigned i = 0, e = SubRegsToUpdate.size(); i < e; i++)
1581 SubRegsToUpdate[i].first->setSubReg(SubRegsToUpdate[i].second);
Hal Finkel82656cb2013-04-18 22:15:08 +00001582
1583 return true;
1584}
1585
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +00001586/// GetInstSize - Return the number of bytes of code the specified
1587/// instruction may be. This returns the maximum number of bytes.
1588///
1589unsigned PPCInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
Hal Finkela7bbaf62014-02-02 06:12:27 +00001590 unsigned Opcode = MI->getOpcode();
1591
1592 if (Opcode == PPC::INLINEASM) {
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +00001593 const MachineFunction *MF = MI->getParent()->getParent();
1594 const char *AsmStr = MI->getOperand(0).getSymbolName();
Chris Lattner7b26fce2009-08-22 20:48:53 +00001595 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
Hal Finkela7bbaf62014-02-02 06:12:27 +00001596 } else {
1597 const MCInstrDesc &Desc = get(Opcode);
1598 return Desc.getSize();
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +00001599 }
1600}
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001601
Hal Finkel174e5902014-03-25 23:29:21 +00001602#undef DEBUG_TYPE
1603#define DEBUG_TYPE "ppc-vsx-fma-mutate"
1604
1605namespace {
1606 // PPCVSXFMAMutate pass - For copies between VSX registers and non-VSX registers
1607 // (Altivec and scalar floating-point registers), we need to transform the
1608 // copies into subregister copies with other restrictions.
1609 struct PPCVSXFMAMutate : public MachineFunctionPass {
1610 static char ID;
1611 PPCVSXFMAMutate() : MachineFunctionPass(ID) {
1612 initializePPCVSXFMAMutatePass(*PassRegistry::getPassRegistry());
1613 }
1614
1615 LiveIntervals *LIS;
1616
1617 const PPCTargetMachine *TM;
1618 const PPCInstrInfo *TII;
1619
1620protected:
1621 bool processBlock(MachineBasicBlock &MBB) {
1622 bool Changed = false;
1623
1624 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
Bill Schmidta087d742014-10-17 21:02:44 +00001625 const TargetRegisterInfo *TRI = &TII->getRegisterInfo();
Hal Finkel174e5902014-03-25 23:29:21 +00001626 for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end();
1627 I != IE; ++I) {
1628 MachineInstr *MI = I;
1629
1630 // The default (A-type) VSX FMA form kills the addend (it is taken from
1631 // the target register, which is then updated to reflect the result of
1632 // the FMA). If the instruction, however, kills one of the registers
1633 // used for the product, then we can use the M-form instruction (which
1634 // will take that value from the to-be-defined register).
1635
1636 int AltOpc = PPC::getAltVSXFMAOpcode(MI->getOpcode());
1637 if (AltOpc == -1)
1638 continue;
1639
1640 // This pass is run after register coalescing, and so we're looking for
1641 // a situation like this:
1642 // ...
1643 // %vreg5<def> = COPY %vreg9; VSLRC:%vreg5,%vreg9
1644 // %vreg5<def,tied1> = XSMADDADP %vreg5<tied0>, %vreg17, %vreg16,
1645 // %RM<imp-use>; VSLRC:%vreg5,%vreg17,%vreg16
1646 // ...
1647 // %vreg9<def,tied1> = XSMADDADP %vreg9<tied0>, %vreg17, %vreg19,
1648 // %RM<imp-use>; VSLRC:%vreg9,%vreg17,%vreg19
1649 // ...
1650 // Where we can eliminate the copy by changing from the A-type to the
1651 // M-type instruction. Specifically, for this example, this means:
1652 // %vreg5<def,tied1> = XSMADDADP %vreg5<tied0>, %vreg17, %vreg16,
1653 // %RM<imp-use>; VSLRC:%vreg5,%vreg17,%vreg16
1654 // is replaced by:
1655 // %vreg16<def,tied1> = XSMADDMDP %vreg16<tied0>, %vreg18, %vreg9,
1656 // %RM<imp-use>; VSLRC:%vreg16,%vreg18,%vreg9
1657 // and we remove: %vreg5<def> = COPY %vreg9; VSLRC:%vreg5,%vreg9
1658
1659 SlotIndex FMAIdx = LIS->getInstructionIndex(MI);
1660
1661 VNInfo *AddendValNo =
1662 LIS->getInterval(MI->getOperand(1).getReg()).Query(FMAIdx).valueIn();
1663 MachineInstr *AddendMI = LIS->getInstructionFromIndex(AddendValNo->def);
1664
1665 // The addend and this instruction must be in the same block.
1666
Hal Finkel19be5062014-03-29 05:29:01 +00001667 if (!AddendMI || AddendMI->getParent() != MI->getParent())
Hal Finkel174e5902014-03-25 23:29:21 +00001668 continue;
1669
1670 // The addend must be a full copy within the same register class.
1671
1672 if (!AddendMI->isFullCopy())
1673 continue;
1674
Hal Finkel19be5062014-03-29 05:29:01 +00001675 unsigned AddendSrcReg = AddendMI->getOperand(1).getReg();
1676 if (TargetRegisterInfo::isVirtualRegister(AddendSrcReg)) {
1677 if (MRI.getRegClass(AddendMI->getOperand(0).getReg()) !=
1678 MRI.getRegClass(AddendSrcReg))
1679 continue;
1680 } else {
1681 // If AddendSrcReg is a physical register, make sure the destination
1682 // register class contains it.
1683 if (!MRI.getRegClass(AddendMI->getOperand(0).getReg())
1684 ->contains(AddendSrcReg))
1685 continue;
1686 }
Hal Finkel174e5902014-03-25 23:29:21 +00001687
1688 // In theory, there could be other uses of the addend copy before this
1689 // fma. We could deal with this, but that would require additional
1690 // logic below and I suspect it will not occur in any relevant
Bill Schmidta087d742014-10-17 21:02:44 +00001691 // situations. Additionally, check whether the copy source is killed
1692 // prior to the fma. In order to replace the addend here with the
1693 // source of the copy, it must still be live here. We can't use
1694 // interval testing for a physical register, so as long as we're
1695 // walking the MIs we may as well test liveness here.
1696 bool OtherUsers = false, KillsAddendSrc = false;
Hal Finkel174e5902014-03-25 23:29:21 +00001697 for (auto J = std::prev(I), JE = MachineBasicBlock::iterator(AddendMI);
Bill Schmidta087d742014-10-17 21:02:44 +00001698 J != JE; --J) {
Hal Finkel174e5902014-03-25 23:29:21 +00001699 if (J->readsVirtualRegister(AddendMI->getOperand(0).getReg())) {
1700 OtherUsers = true;
1701 break;
1702 }
Bill Schmidta087d742014-10-17 21:02:44 +00001703 if (J->modifiesRegister(AddendSrcReg, TRI) ||
1704 J->killsRegister(AddendSrcReg, TRI)) {
1705 KillsAddendSrc = true;
1706 break;
1707 }
1708 }
Hal Finkel174e5902014-03-25 23:29:21 +00001709
Bill Schmidta087d742014-10-17 21:02:44 +00001710 if (OtherUsers || KillsAddendSrc)
Hal Finkel174e5902014-03-25 23:29:21 +00001711 continue;
1712
1713 // Find one of the product operands that is killed by this instruction.
1714
1715 unsigned KilledProdOp = 0, OtherProdOp = 0;
1716 if (LIS->getInterval(MI->getOperand(2).getReg())
1717 .Query(FMAIdx).isKill()) {
1718 KilledProdOp = 2;
1719 OtherProdOp = 3;
1720 } else if (LIS->getInterval(MI->getOperand(3).getReg())
1721 .Query(FMAIdx).isKill()) {
1722 KilledProdOp = 3;
1723 OtherProdOp = 2;
1724 }
1725
Hal Finkel19be5062014-03-29 05:29:01 +00001726 // If there are no killed product operands, then this transformation is
1727 // likely not profitable.
Hal Finkel174e5902014-03-25 23:29:21 +00001728 if (!KilledProdOp)
1729 continue;
1730
Bill Schmidta087d742014-10-17 21:02:44 +00001731 // For virtual registers, verify that the addend source register
1732 // is live here (as should have been assured above).
1733 if (TargetRegisterInfo::isVirtualRegister(AddendSrcReg))
1734 assert(LIS->getInterval(AddendSrcReg).liveAt(FMAIdx) &&
1735 "Addend source register is not available!");
Hal Finkel174e5902014-03-25 23:29:21 +00001736
1737 // Transform: (O2 * O3) + O1 -> (O2 * O1) + O3.
1738
1739 unsigned AddReg = AddendMI->getOperand(1).getReg();
1740 unsigned KilledProdReg = MI->getOperand(KilledProdOp).getReg();
1741 unsigned OtherProdReg = MI->getOperand(OtherProdOp).getReg();
1742
1743 unsigned AddSubReg = AddendMI->getOperand(1).getSubReg();
1744 unsigned KilledProdSubReg = MI->getOperand(KilledProdOp).getSubReg();
1745 unsigned OtherProdSubReg = MI->getOperand(OtherProdOp).getSubReg();
1746
1747 bool AddRegKill = AddendMI->getOperand(1).isKill();
1748 bool KilledProdRegKill = MI->getOperand(KilledProdOp).isKill();
1749 bool OtherProdRegKill = MI->getOperand(OtherProdOp).isKill();
1750
1751 bool AddRegUndef = AddendMI->getOperand(1).isUndef();
1752 bool KilledProdRegUndef = MI->getOperand(KilledProdOp).isUndef();
1753 bool OtherProdRegUndef = MI->getOperand(OtherProdOp).isUndef();
1754
1755 unsigned OldFMAReg = MI->getOperand(0).getReg();
1756
1757 assert(OldFMAReg == AddendMI->getOperand(0).getReg() &&
1758 "Addend copy not tied to old FMA output!");
1759
1760 DEBUG(dbgs() << "VSX FMA Mutation:\n " << *MI;);
1761
1762 MI->getOperand(0).setReg(KilledProdReg);
1763 MI->getOperand(1).setReg(KilledProdReg);
1764 MI->getOperand(3).setReg(AddReg);
1765 MI->getOperand(2).setReg(OtherProdReg);
1766
1767 MI->getOperand(0).setSubReg(KilledProdSubReg);
1768 MI->getOperand(1).setSubReg(KilledProdSubReg);
1769 MI->getOperand(3).setSubReg(AddSubReg);
1770 MI->getOperand(2).setSubReg(OtherProdSubReg);
1771
1772 MI->getOperand(1).setIsKill(KilledProdRegKill);
1773 MI->getOperand(3).setIsKill(AddRegKill);
1774 MI->getOperand(2).setIsKill(OtherProdRegKill);
1775
1776 MI->getOperand(1).setIsUndef(KilledProdRegUndef);
1777 MI->getOperand(3).setIsUndef(AddRegUndef);
1778 MI->getOperand(2).setIsUndef(OtherProdRegUndef);
1779
1780 MI->setDesc(TII->get(AltOpc));
1781
1782 DEBUG(dbgs() << " -> " << *MI);
1783
1784 // The killed product operand was killed here, so we can reuse it now
1785 // for the result of the fma.
1786
1787 LiveInterval &FMAInt = LIS->getInterval(OldFMAReg);
1788 VNInfo *FMAValNo = FMAInt.getVNInfoAt(FMAIdx.getRegSlot());
1789 for (auto UI = MRI.reg_nodbg_begin(OldFMAReg), UE = MRI.reg_nodbg_end();
1790 UI != UE;) {
1791 MachineOperand &UseMO = *UI;
1792 MachineInstr *UseMI = UseMO.getParent();
1793 ++UI;
1794
1795 // Don't replace the result register of the copy we're about to erase.
1796 if (UseMI == AddendMI)
1797 continue;
1798
1799 UseMO.setReg(KilledProdReg);
1800 UseMO.setSubReg(KilledProdSubReg);
1801 }
1802
1803 // Extend the live intervals of the killed product operand to hold the
1804 // fma result.
1805
1806 LiveInterval &NewFMAInt = LIS->getInterval(KilledProdReg);
1807 for (LiveInterval::iterator AI = FMAInt.begin(), AE = FMAInt.end();
1808 AI != AE; ++AI) {
1809 // Don't add the segment that corresponds to the original copy.
1810 if (AI->valno == AddendValNo)
1811 continue;
1812
1813 VNInfo *NewFMAValNo =
1814 NewFMAInt.getNextValue(AI->start,
1815 LIS->getVNInfoAllocator());
1816
1817 NewFMAInt.addSegment(LiveInterval::Segment(AI->start, AI->end,
1818 NewFMAValNo));
1819 }
1820 DEBUG(dbgs() << " extended: " << NewFMAInt << '\n');
1821
1822 FMAInt.removeValNo(FMAValNo);
1823 DEBUG(dbgs() << " trimmed: " << FMAInt << '\n');
1824
1825 // Remove the (now unused) copy.
1826
1827 DEBUG(dbgs() << " removing: " << *AddendMI << '\n');
1828 LIS->RemoveMachineInstrFromMaps(AddendMI);
1829 AddendMI->eraseFromParent();
1830
1831 Changed = true;
1832 }
1833
1834 return Changed;
1835 }
1836
1837public:
Craig Topper0d3fa922014-04-29 07:57:37 +00001838 bool runOnMachineFunction(MachineFunction &MF) override {
Eric Christopherd71e4442014-05-22 01:21:35 +00001839 TM = static_cast<const PPCTargetMachine *>(&MF.getTarget());
1840 // If we don't have VSX then go ahead and return without doing
1841 // anything.
1842 if (!TM->getSubtargetImpl()->hasVSX())
1843 return false;
1844
Hal Finkel174e5902014-03-25 23:29:21 +00001845 LIS = &getAnalysis<LiveIntervals>();
1846
Eric Christopherd9134482014-08-04 21:25:23 +00001847 TII = TM->getSubtargetImpl()->getInstrInfo();
Hal Finkel174e5902014-03-25 23:29:21 +00001848
1849 bool Changed = false;
1850
1851 if (DisableVSXFMAMutate)
1852 return Changed;
1853
1854 for (MachineFunction::iterator I = MF.begin(); I != MF.end();) {
1855 MachineBasicBlock &B = *I++;
1856 if (processBlock(B))
1857 Changed = true;
1858 }
1859
1860 return Changed;
1861 }
1862
Craig Topper0d3fa922014-04-29 07:57:37 +00001863 void getAnalysisUsage(AnalysisUsage &AU) const override {
Hal Finkel174e5902014-03-25 23:29:21 +00001864 AU.addRequired<LiveIntervals>();
1865 AU.addPreserved<LiveIntervals>();
1866 AU.addRequired<SlotIndexes>();
1867 AU.addPreserved<SlotIndexes>();
1868 MachineFunctionPass::getAnalysisUsage(AU);
1869 }
1870 };
1871}
1872
1873INITIALIZE_PASS_BEGIN(PPCVSXFMAMutate, DEBUG_TYPE,
1874 "PowerPC VSX FMA Mutation", false, false)
1875INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
1876INITIALIZE_PASS_DEPENDENCY(SlotIndexes)
1877INITIALIZE_PASS_END(PPCVSXFMAMutate, DEBUG_TYPE,
1878 "PowerPC VSX FMA Mutation", false, false)
1879
1880char &llvm::PPCVSXFMAMutateID = PPCVSXFMAMutate::ID;
1881
1882char PPCVSXFMAMutate::ID = 0;
1883FunctionPass*
1884llvm::createPPCVSXFMAMutatePass() { return new PPCVSXFMAMutate(); }
Hal Finkel27774d92014-03-13 07:58:58 +00001885
1886#undef DEBUG_TYPE
1887#define DEBUG_TYPE "ppc-vsx-copy"
1888
1889namespace llvm {
1890 void initializePPCVSXCopyPass(PassRegistry&);
1891}
1892
1893namespace {
1894 // PPCVSXCopy pass - For copies between VSX registers and non-VSX registers
1895 // (Altivec and scalar floating-point registers), we need to transform the
1896 // copies into subregister copies with other restrictions.
1897 struct PPCVSXCopy : public MachineFunctionPass {
1898 static char ID;
1899 PPCVSXCopy() : MachineFunctionPass(ID) {
1900 initializePPCVSXCopyPass(*PassRegistry::getPassRegistry());
1901 }
1902
1903 const PPCTargetMachine *TM;
1904 const PPCInstrInfo *TII;
1905
1906 bool IsRegInClass(unsigned Reg, const TargetRegisterClass *RC,
1907 MachineRegisterInfo &MRI) {
1908 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1909 return RC->hasSubClassEq(MRI.getRegClass(Reg));
1910 } else if (RC->contains(Reg)) {
1911 return true;
1912 }
1913
1914 return false;
1915 }
1916
1917 bool IsVSReg(unsigned Reg, MachineRegisterInfo &MRI) {
1918 return IsRegInClass(Reg, &PPC::VSRCRegClass, MRI);
1919 }
1920
1921 bool IsVRReg(unsigned Reg, MachineRegisterInfo &MRI) {
1922 return IsRegInClass(Reg, &PPC::VRRCRegClass, MRI);
1923 }
1924
1925 bool IsF8Reg(unsigned Reg, MachineRegisterInfo &MRI) {
1926 return IsRegInClass(Reg, &PPC::F8RCRegClass, MRI);
1927 }
1928
1929protected:
1930 bool processBlock(MachineBasicBlock &MBB) {
1931 bool Changed = false;
1932
1933 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
1934 for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end();
1935 I != IE; ++I) {
1936 MachineInstr *MI = I;
1937 if (!MI->isFullCopy())
1938 continue;
1939
1940 MachineOperand &DstMO = MI->getOperand(0);
1941 MachineOperand &SrcMO = MI->getOperand(1);
1942
1943 if ( IsVSReg(DstMO.getReg(), MRI) &&
1944 !IsVSReg(SrcMO.getReg(), MRI)) {
1945 // This is a copy *to* a VSX register from a non-VSX register.
1946 Changed = true;
1947
1948 const TargetRegisterClass *SrcRC =
1949 IsVRReg(SrcMO.getReg(), MRI) ? &PPC::VSHRCRegClass :
1950 &PPC::VSLRCRegClass;
1951 assert((IsF8Reg(SrcMO.getReg(), MRI) ||
1952 IsVRReg(SrcMO.getReg(), MRI)) &&
1953 "Unknown source for a VSX copy");
1954
1955 unsigned NewVReg = MRI.createVirtualRegister(SrcRC);
1956 BuildMI(MBB, MI, MI->getDebugLoc(),
1957 TII->get(TargetOpcode::SUBREG_TO_REG), NewVReg)
1958 .addImm(1) // add 1, not 0, because there is no implicit clearing
1959 // of the high bits.
1960 .addOperand(SrcMO)
1961 .addImm(IsVRReg(SrcMO.getReg(), MRI) ? PPC::sub_128 :
1962 PPC::sub_64);
1963
1964 // The source of the original copy is now the new virtual register.
1965 SrcMO.setReg(NewVReg);
1966 } else if (!IsVSReg(DstMO.getReg(), MRI) &&
1967 IsVSReg(SrcMO.getReg(), MRI)) {
1968 // This is a copy *from* a VSX register to a non-VSX register.
1969 Changed = true;
1970
1971 const TargetRegisterClass *DstRC =
1972 IsVRReg(DstMO.getReg(), MRI) ? &PPC::VSHRCRegClass :
1973 &PPC::VSLRCRegClass;
1974 assert((IsF8Reg(DstMO.getReg(), MRI) ||
1975 IsVRReg(DstMO.getReg(), MRI)) &&
1976 "Unknown destination for a VSX copy");
1977
1978 // Copy the VSX value into a new VSX register of the correct subclass.
1979 unsigned NewVReg = MRI.createVirtualRegister(DstRC);
1980 BuildMI(MBB, MI, MI->getDebugLoc(),
1981 TII->get(TargetOpcode::COPY), NewVReg)
1982 .addOperand(SrcMO);
1983
1984 // Transform the original copy into a subregister extraction copy.
1985 SrcMO.setReg(NewVReg);
1986 SrcMO.setSubReg(IsVRReg(DstMO.getReg(), MRI) ? PPC::sub_128 :
1987 PPC::sub_64);
1988 }
1989 }
1990
1991 return Changed;
1992 }
1993
1994public:
Craig Topper0d3fa922014-04-29 07:57:37 +00001995 bool runOnMachineFunction(MachineFunction &MF) override {
Hal Finkel27774d92014-03-13 07:58:58 +00001996 TM = static_cast<const PPCTargetMachine *>(&MF.getTarget());
Eric Christopherd71e4442014-05-22 01:21:35 +00001997 // If we don't have VSX on the subtarget, don't do anything.
1998 if (!TM->getSubtargetImpl()->hasVSX())
1999 return false;
Eric Christopherd9134482014-08-04 21:25:23 +00002000 TII = TM->getSubtargetImpl()->getInstrInfo();
Hal Finkel27774d92014-03-13 07:58:58 +00002001
2002 bool Changed = false;
2003
2004 for (MachineFunction::iterator I = MF.begin(); I != MF.end();) {
2005 MachineBasicBlock &B = *I++;
2006 if (processBlock(B))
2007 Changed = true;
2008 }
2009
2010 return Changed;
2011 }
2012
Craig Topper0d3fa922014-04-29 07:57:37 +00002013 void getAnalysisUsage(AnalysisUsage &AU) const override {
Hal Finkel27774d92014-03-13 07:58:58 +00002014 MachineFunctionPass::getAnalysisUsage(AU);
2015 }
2016 };
2017}
2018
2019INITIALIZE_PASS(PPCVSXCopy, DEBUG_TYPE,
2020 "PowerPC VSX Copy Legalization", false, false)
2021
2022char PPCVSXCopy::ID = 0;
2023FunctionPass*
2024llvm::createPPCVSXCopyPass() { return new PPCVSXCopy(); }
2025
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002026#undef DEBUG_TYPE
Hal Finkelc6fc9b82014-03-27 23:12:31 +00002027#define DEBUG_TYPE "ppc-vsx-copy-cleanup"
2028
2029namespace llvm {
2030 void initializePPCVSXCopyCleanupPass(PassRegistry&);
2031}
2032
2033namespace {
2034 // PPCVSXCopyCleanup pass - We sometimes end up generating self copies of VSX
2035 // registers (mostly because the ABI code still places all values into the
2036 // "traditional" floating-point and vector registers). Remove them here.
2037 struct PPCVSXCopyCleanup : public MachineFunctionPass {
2038 static char ID;
2039 PPCVSXCopyCleanup() : MachineFunctionPass(ID) {
2040 initializePPCVSXCopyCleanupPass(*PassRegistry::getPassRegistry());
2041 }
2042
2043 const PPCTargetMachine *TM;
2044 const PPCInstrInfo *TII;
2045
2046protected:
2047 bool processBlock(MachineBasicBlock &MBB) {
2048 bool Changed = false;
2049
2050 SmallVector<MachineInstr *, 4> ToDelete;
2051 for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end();
2052 I != IE; ++I) {
2053 MachineInstr *MI = I;
2054 if (MI->getOpcode() == PPC::XXLOR &&
2055 MI->getOperand(0).getReg() == MI->getOperand(1).getReg() &&
2056 MI->getOperand(0).getReg() == MI->getOperand(2).getReg())
2057 ToDelete.push_back(MI);
2058 }
2059
2060 if (!ToDelete.empty())
2061 Changed = true;
2062
2063 for (unsigned i = 0, ie = ToDelete.size(); i != ie; ++i) {
2064 DEBUG(dbgs() << "Removing VSX self-copy: " << *ToDelete[i]);
2065 ToDelete[i]->eraseFromParent();
2066 }
2067
2068 return Changed;
2069 }
2070
2071public:
Craig Topper0d3fa922014-04-29 07:57:37 +00002072 bool runOnMachineFunction(MachineFunction &MF) override {
Hal Finkelc6fc9b82014-03-27 23:12:31 +00002073 TM = static_cast<const PPCTargetMachine *>(&MF.getTarget());
Eric Christopherd71e4442014-05-22 01:21:35 +00002074 // If we don't have VSX don't bother doing anything here.
2075 if (!TM->getSubtargetImpl()->hasVSX())
2076 return false;
Eric Christopherd9134482014-08-04 21:25:23 +00002077 TII = TM->getSubtargetImpl()->getInstrInfo();
Hal Finkelc6fc9b82014-03-27 23:12:31 +00002078
2079 bool Changed = false;
2080
2081 for (MachineFunction::iterator I = MF.begin(); I != MF.end();) {
2082 MachineBasicBlock &B = *I++;
2083 if (processBlock(B))
2084 Changed = true;
2085 }
2086
2087 return Changed;
2088 }
2089
Craig Topper0d3fa922014-04-29 07:57:37 +00002090 void getAnalysisUsage(AnalysisUsage &AU) const override {
Hal Finkelc6fc9b82014-03-27 23:12:31 +00002091 MachineFunctionPass::getAnalysisUsage(AU);
2092 }
2093 };
2094}
2095
2096INITIALIZE_PASS(PPCVSXCopyCleanup, DEBUG_TYPE,
2097 "PowerPC VSX Copy Cleanup", false, false)
2098
2099char PPCVSXCopyCleanup::ID = 0;
2100FunctionPass*
2101llvm::createPPCVSXCopyCleanupPass() { return new PPCVSXCopyCleanup(); }
2102
2103#undef DEBUG_TYPE
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002104#define DEBUG_TYPE "ppc-early-ret"
2105STATISTIC(NumBCLR, "Number of early conditional returns");
2106STATISTIC(NumBLR, "Number of early returns");
2107
2108namespace llvm {
2109 void initializePPCEarlyReturnPass(PassRegistry&);
2110}
2111
2112namespace {
2113 // PPCEarlyReturn pass - For simple functions without epilogue code, move
2114 // returns up, and create conditional returns, to avoid unnecessary
2115 // branch-to-blr sequences.
2116 struct PPCEarlyReturn : public MachineFunctionPass {
2117 static char ID;
2118 PPCEarlyReturn() : MachineFunctionPass(ID) {
2119 initializePPCEarlyReturnPass(*PassRegistry::getPassRegistry());
2120 }
2121
2122 const PPCTargetMachine *TM;
2123 const PPCInstrInfo *TII;
2124
2125protected:
Hal Finkel21aad9a2013-04-09 18:25:18 +00002126 bool processBlock(MachineBasicBlock &ReturnMBB) {
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002127 bool Changed = false;
2128
Hal Finkel21aad9a2013-04-09 18:25:18 +00002129 MachineBasicBlock::iterator I = ReturnMBB.begin();
2130 I = ReturnMBB.SkipPHIsAndLabels(I);
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002131
2132 // The block must be essentially empty except for the blr.
Hal Finkel21aad9a2013-04-09 18:25:18 +00002133 if (I == ReturnMBB.end() || I->getOpcode() != PPC::BLR ||
2134 I != ReturnMBB.getLastNonDebugInstr())
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002135 return Changed;
2136
2137 SmallVector<MachineBasicBlock*, 8> PredToRemove;
Hal Finkel21aad9a2013-04-09 18:25:18 +00002138 for (MachineBasicBlock::pred_iterator PI = ReturnMBB.pred_begin(),
2139 PIE = ReturnMBB.pred_end(); PI != PIE; ++PI) {
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002140 bool OtherReference = false, BlockChanged = false;
Hal Finkel21aad9a2013-04-09 18:25:18 +00002141 for (MachineBasicBlock::iterator J = (*PI)->getLastNonDebugInstr();;) {
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002142 if (J->getOpcode() == PPC::B) {
Hal Finkel21aad9a2013-04-09 18:25:18 +00002143 if (J->getOperand(0).getMBB() == &ReturnMBB) {
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002144 // This is an unconditional branch to the return. Replace the
Andrew Trick9defbd82013-12-17 04:50:40 +00002145 // branch with a blr.
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002146 BuildMI(**PI, J, J->getDebugLoc(), TII->get(PPC::BLR));
Hal Finkel21aad9a2013-04-09 18:25:18 +00002147 MachineBasicBlock::iterator K = J--;
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002148 K->eraseFromParent();
2149 BlockChanged = true;
2150 ++NumBLR;
2151 continue;
2152 }
2153 } else if (J->getOpcode() == PPC::BCC) {
Hal Finkel21aad9a2013-04-09 18:25:18 +00002154 if (J->getOperand(2).getMBB() == &ReturnMBB) {
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002155 // This is a conditional branch to the return. Replace the branch
2156 // with a bclr.
Hal Finkel940ab932014-02-28 00:27:01 +00002157 BuildMI(**PI, J, J->getDebugLoc(), TII->get(PPC::BCCLR))
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002158 .addImm(J->getOperand(0).getImm())
2159 .addReg(J->getOperand(1).getReg());
Hal Finkel21aad9a2013-04-09 18:25:18 +00002160 MachineBasicBlock::iterator K = J--;
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002161 K->eraseFromParent();
2162 BlockChanged = true;
2163 ++NumBCLR;
2164 continue;
2165 }
Hal Finkel940ab932014-02-28 00:27:01 +00002166 } else if (J->getOpcode() == PPC::BC || J->getOpcode() == PPC::BCn) {
2167 if (J->getOperand(1).getMBB() == &ReturnMBB) {
2168 // This is a conditional branch to the return. Replace the branch
2169 // with a bclr.
2170 BuildMI(**PI, J, J->getDebugLoc(),
2171 TII->get(J->getOpcode() == PPC::BC ?
2172 PPC::BCLR : PPC::BCLRn))
2173 .addReg(J->getOperand(0).getReg());
2174 MachineBasicBlock::iterator K = J--;
2175 K->eraseFromParent();
2176 BlockChanged = true;
2177 ++NumBCLR;
2178 continue;
2179 }
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002180 } else if (J->isBranch()) {
2181 if (J->isIndirectBranch()) {
Hal Finkel21aad9a2013-04-09 18:25:18 +00002182 if (ReturnMBB.hasAddressTaken())
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002183 OtherReference = true;
2184 } else
2185 for (unsigned i = 0; i < J->getNumOperands(); ++i)
2186 if (J->getOperand(i).isMBB() &&
Hal Finkel21aad9a2013-04-09 18:25:18 +00002187 J->getOperand(i).getMBB() == &ReturnMBB)
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002188 OtherReference = true;
Hal Finkel21aad9a2013-04-09 18:25:18 +00002189 } else if (!J->isTerminator() && !J->isDebugValue())
2190 break;
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002191
Hal Finkel21aad9a2013-04-09 18:25:18 +00002192 if (J == (*PI)->begin())
2193 break;
2194
2195 --J;
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002196 }
2197
Hal Finkel21aad9a2013-04-09 18:25:18 +00002198 if ((*PI)->canFallThrough() && (*PI)->isLayoutSuccessor(&ReturnMBB))
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002199 OtherReference = true;
2200
Andrew Trick9defbd82013-12-17 04:50:40 +00002201 // Predecessors are stored in a vector and can't be removed here.
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002202 if (!OtherReference && BlockChanged) {
2203 PredToRemove.push_back(*PI);
2204 }
2205
2206 if (BlockChanged)
2207 Changed = true;
2208 }
2209
2210 for (unsigned i = 0, ie = PredToRemove.size(); i != ie; ++i)
Hal Finkel21aad9a2013-04-09 18:25:18 +00002211 PredToRemove[i]->removeSuccessor(&ReturnMBB);
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002212
Hal Finkel21aad9a2013-04-09 18:25:18 +00002213 if (Changed && !ReturnMBB.hasAddressTaken()) {
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002214 // We now might be able to merge this blr-only block into its
2215 // by-layout predecessor.
Hal Finkel21aad9a2013-04-09 18:25:18 +00002216 if (ReturnMBB.pred_size() == 1 &&
2217 (*ReturnMBB.pred_begin())->isLayoutSuccessor(&ReturnMBB)) {
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002218 // Move the blr into the preceding block.
Hal Finkel21aad9a2013-04-09 18:25:18 +00002219 MachineBasicBlock &PrevMBB = **ReturnMBB.pred_begin();
2220 PrevMBB.splice(PrevMBB.end(), &ReturnMBB, I);
2221 PrevMBB.removeSuccessor(&ReturnMBB);
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002222 }
2223
Hal Finkel21aad9a2013-04-09 18:25:18 +00002224 if (ReturnMBB.pred_empty())
2225 ReturnMBB.eraseFromParent();
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002226 }
2227
2228 return Changed;
2229 }
2230
2231public:
Craig Topper0d3fa922014-04-29 07:57:37 +00002232 bool runOnMachineFunction(MachineFunction &MF) override {
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002233 TM = static_cast<const PPCTargetMachine *>(&MF.getTarget());
Eric Christopherd9134482014-08-04 21:25:23 +00002234 TII = TM->getSubtargetImpl()->getInstrInfo();
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002235
2236 bool Changed = false;
2237
Hal Finkel21aad9a2013-04-09 18:25:18 +00002238 // If the function does not have at least two blocks, then there is
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002239 // nothing to do.
2240 if (MF.size() < 2)
2241 return Changed;
2242
2243 for (MachineFunction::iterator I = MF.begin(); I != MF.end();) {
Andrew Trick9defbd82013-12-17 04:50:40 +00002244 MachineBasicBlock &B = *I++;
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002245 if (processBlock(B))
2246 Changed = true;
2247 }
2248
2249 return Changed;
2250 }
2251
Craig Topper0d3fa922014-04-29 07:57:37 +00002252 void getAnalysisUsage(AnalysisUsage &AU) const override {
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002253 MachineFunctionPass::getAnalysisUsage(AU);
2254 }
2255 };
2256}
2257
2258INITIALIZE_PASS(PPCEarlyReturn, DEBUG_TYPE,
2259 "PowerPC Early-Return Creation", false, false)
2260
2261char PPCEarlyReturn::ID = 0;
2262FunctionPass*
2263llvm::createPPCEarlyReturnPass() { return new PPCEarlyReturn(); }