blob: 3d5779227ebbdfc3959582d990a913bf85efdddb [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.
70ScheduleHazardRecognizer *PPCInstrInfo::CreateTargetHazardRecognizer(
71 const TargetMachine *TM,
72 const ScheduleDAG *DAG) const {
Hal Finkel6fa56972011-10-17 04:03:49 +000073 unsigned Directive = TM->getSubtarget<PPCSubtarget>().getDarwinDirective();
Hal Finkel742b5352012-08-28 16:12:39 +000074 if (Directive == PPC::DIR_440 || Directive == PPC::DIR_A2 ||
75 Directive == PPC::DIR_E500mc || Directive == PPC::DIR_E5500) {
Hal Finkel6f0ae782011-11-22 16:21:04 +000076 const InstrItineraryData *II = TM->getInstrItineraryData();
Hal Finkel563cc052013-12-02 23:52:46 +000077 return new ScoreboardHazardRecognizer(II, DAG);
Hal Finkel6fa56972011-10-17 04:03:49 +000078 }
Hal Finkel58ca3602011-12-02 04:58:02 +000079
Jakob Stoklund Olesen9de596e2012-11-28 02:35:17 +000080 return TargetInstrInfo::CreateTargetHazardRecognizer(TM, DAG);
Andrew Trick10ffc2b2010-12-24 05:03:26 +000081}
82
Hal Finkel58ca3602011-12-02 04:58:02 +000083/// CreateTargetPostRAHazardRecognizer - Return the postRA hazard recognizer
84/// to use for this target when scheduling the DAG.
85ScheduleHazardRecognizer *PPCInstrInfo::CreateTargetPostRAHazardRecognizer(
86 const InstrItineraryData *II,
87 const ScheduleDAG *DAG) const {
Eric Christopher1dcea732014-06-12 21:48:52 +000088 unsigned Directive =
89 DAG->TM.getSubtarget<PPCSubtarget>().getDarwinDirective();
Hal Finkel58ca3602011-12-02 04:58:02 +000090
Hal Finkelceb1f122013-12-12 00:19:11 +000091 if (Directive == PPC::DIR_PWR7)
92 return new PPCDispatchGroupSBHazardRecognizer(II, DAG);
93
Hal Finkel58ca3602011-12-02 04:58:02 +000094 // Most subtargets use a PPC970 recognizer.
Hal Finkel742b5352012-08-28 16:12:39 +000095 if (Directive != PPC::DIR_440 && Directive != PPC::DIR_A2 &&
96 Directive != PPC::DIR_E500mc && Directive != PPC::DIR_E5500) {
Eric Christopher1dcea732014-06-12 21:48:52 +000097 assert(DAG->TII && "No InstrInfo?");
Hal Finkel58ca3602011-12-02 04:58:02 +000098
Eric Christopher1dcea732014-06-12 21:48:52 +000099 return new PPCHazardRecognizer970(*DAG);
Hal Finkel58ca3602011-12-02 04:58:02 +0000100 }
101
Hal Finkel563cc052013-12-02 23:52:46 +0000102 return new ScoreboardHazardRecognizer(II, DAG);
Hal Finkel58ca3602011-12-02 04:58:02 +0000103}
Jakob Stoklund Olesen0f855e42012-06-19 21:14:34 +0000104
Hal Finkelceb1f122013-12-12 00:19:11 +0000105
106int PPCInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
107 const MachineInstr *DefMI, unsigned DefIdx,
108 const MachineInstr *UseMI,
109 unsigned UseIdx) const {
110 int Latency = PPCGenInstrInfo::getOperandLatency(ItinData, DefMI, DefIdx,
111 UseMI, UseIdx);
112
113 const MachineOperand &DefMO = DefMI->getOperand(DefIdx);
114 unsigned Reg = DefMO.getReg();
115
116 const TargetRegisterInfo *TRI = &getRegisterInfo();
117 bool IsRegCR;
118 if (TRI->isVirtualRegister(Reg)) {
119 const MachineRegisterInfo *MRI =
120 &DefMI->getParent()->getParent()->getRegInfo();
121 IsRegCR = MRI->getRegClass(Reg)->hasSuperClassEq(&PPC::CRRCRegClass) ||
122 MRI->getRegClass(Reg)->hasSuperClassEq(&PPC::CRBITRCRegClass);
123 } else {
124 IsRegCR = PPC::CRRCRegClass.contains(Reg) ||
125 PPC::CRBITRCRegClass.contains(Reg);
126 }
127
128 if (UseMI->isBranch() && IsRegCR) {
129 if (Latency < 0)
130 Latency = getInstrLatency(ItinData, DefMI);
131
132 // On some cores, there is an additional delay between writing to a condition
133 // register, and using it from a branch.
Eric Christopher1dcea732014-06-12 21:48:52 +0000134 unsigned Directive = Subtarget.getDarwinDirective();
Hal Finkelceb1f122013-12-12 00:19:11 +0000135 switch (Directive) {
136 default: break;
137 case PPC::DIR_7400:
138 case PPC::DIR_750:
139 case PPC::DIR_970:
140 case PPC::DIR_E5500:
141 case PPC::DIR_PWR4:
142 case PPC::DIR_PWR5:
143 case PPC::DIR_PWR5X:
144 case PPC::DIR_PWR6:
145 case PPC::DIR_PWR6X:
146 case PPC::DIR_PWR7:
147 Latency += 2;
148 break;
149 }
150 }
151
152 return Latency;
153}
154
Jakob Stoklund Olesen0f855e42012-06-19 21:14:34 +0000155// Detect 32 -> 64-bit extensions where we may reuse the low sub-register.
156bool PPCInstrInfo::isCoalescableExtInstr(const MachineInstr &MI,
157 unsigned &SrcReg, unsigned &DstReg,
158 unsigned &SubIdx) const {
159 switch (MI.getOpcode()) {
160 default: return false;
161 case PPC::EXTSW:
162 case PPC::EXTSW_32_64:
163 SrcReg = MI.getOperand(1).getReg();
164 DstReg = MI.getOperand(0).getReg();
165 SubIdx = PPC::sub_32;
166 return true;
167 }
168}
169
Andrew Trickc416ba62010-12-24 04:28:06 +0000170unsigned PPCInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
Chris Lattner91400bd2006-03-16 22:24:02 +0000171 int &FrameIndex) const {
Hal Finkel37714b82013-03-27 21:21:15 +0000172 // Note: This list must be kept consistent with LoadRegFromStackSlot.
Chris Lattnerbb53acd2006-02-02 20:12:32 +0000173 switch (MI->getOpcode()) {
174 default: break;
175 case PPC::LD:
176 case PPC::LWZ:
177 case PPC::LFS:
178 case PPC::LFD:
Hal Finkel37714b82013-03-27 21:21:15 +0000179 case PPC::RESTORE_CR:
Hal Finkel940ab932014-02-28 00:27:01 +0000180 case PPC::RESTORE_CRBIT:
Hal Finkel37714b82013-03-27 21:21:15 +0000181 case PPC::LVX:
Hal Finkel27774d92014-03-13 07:58:58 +0000182 case PPC::LXVD2X:
Hal Finkel37714b82013-03-27 21:21:15 +0000183 case PPC::RESTORE_VRSAVE:
184 // Check for the operands added by addFrameReference (the immediate is the
185 // offset which defaults to 0).
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000186 if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
187 MI->getOperand(2).isFI()) {
Chris Lattnera5bb3702007-12-30 23:10:15 +0000188 FrameIndex = MI->getOperand(2).getIndex();
Chris Lattnerbb53acd2006-02-02 20:12:32 +0000189 return MI->getOperand(0).getReg();
190 }
191 break;
192 }
193 return 0;
Chris Lattnerc327d712006-02-02 20:16:12 +0000194}
Chris Lattnerbb53acd2006-02-02 20:12:32 +0000195
Andrew Trickc416ba62010-12-24 04:28:06 +0000196unsigned PPCInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
Chris Lattnerc327d712006-02-02 20:16:12 +0000197 int &FrameIndex) const {
Hal Finkel37714b82013-03-27 21:21:15 +0000198 // Note: This list must be kept consistent with StoreRegToStackSlot.
Chris Lattnerc327d712006-02-02 20:16:12 +0000199 switch (MI->getOpcode()) {
200 default: break;
Nate Begeman4efb3282006-02-02 21:07:50 +0000201 case PPC::STD:
Chris Lattnerc327d712006-02-02 20:16:12 +0000202 case PPC::STW:
203 case PPC::STFS:
204 case PPC::STFD:
Hal Finkel37714b82013-03-27 21:21:15 +0000205 case PPC::SPILL_CR:
Hal Finkel940ab932014-02-28 00:27:01 +0000206 case PPC::SPILL_CRBIT:
Hal Finkel37714b82013-03-27 21:21:15 +0000207 case PPC::STVX:
Hal Finkel27774d92014-03-13 07:58:58 +0000208 case PPC::STXVD2X:
Hal Finkel37714b82013-03-27 21:21:15 +0000209 case PPC::SPILL_VRSAVE:
210 // Check for the operands added by addFrameReference (the immediate is the
211 // offset which defaults to 0).
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000212 if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
213 MI->getOperand(2).isFI()) {
Chris Lattnera5bb3702007-12-30 23:10:15 +0000214 FrameIndex = MI->getOperand(2).getIndex();
Chris Lattnerc327d712006-02-02 20:16:12 +0000215 return MI->getOperand(0).getReg();
216 }
217 break;
218 }
219 return 0;
220}
Chris Lattnerbb53acd2006-02-02 20:12:32 +0000221
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000222// commuteInstruction - We can commute rlwimi instructions, but only if the
223// rotate amt is zero. We also have to munge the immediates a bit.
Evan Cheng03553bb2008-06-16 07:33:11 +0000224MachineInstr *
225PPCInstrInfo::commuteInstruction(MachineInstr *MI, bool NewMI) const {
Dan Gohman3b460302008-07-07 23:14:23 +0000226 MachineFunction &MF = *MI->getParent()->getParent();
227
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000228 // Normal instructions can be commuted the obvious way.
Hal Finkel654d43b2013-04-12 02:18:09 +0000229 if (MI->getOpcode() != PPC::RLWIMI &&
Hal Finkel940ab932014-02-28 00:27:01 +0000230 MI->getOpcode() != PPC::RLWIMIo &&
231 MI->getOpcode() != PPC::RLWIMI8 &&
232 MI->getOpcode() != PPC::RLWIMI8o)
Jakob Stoklund Olesen9de596e2012-11-28 02:35:17 +0000233 return TargetInstrInfo::commuteInstruction(MI, NewMI);
Andrew Trickc416ba62010-12-24 04:28:06 +0000234
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000235 // Cannot commute if it has a non-zero rotate count.
Chris Lattner5c463782007-12-30 20:49:49 +0000236 if (MI->getOperand(3).getImm() != 0)
Craig Topper062a2ba2014-04-25 05:30:21 +0000237 return nullptr;
Andrew Trickc416ba62010-12-24 04:28:06 +0000238
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000239 // If we have a zero rotate count, we have:
240 // M = mask(MB,ME)
241 // Op0 = (Op1 & ~M) | (Op2 & M)
242 // Change this to:
243 // M = mask((ME+1)&31, (MB-1)&31)
244 // Op0 = (Op2 & ~M) | (Op1 & M)
245
246 // Swap op1/op2
Evan Cheng244183e2008-02-13 02:46:49 +0000247 unsigned Reg0 = MI->getOperand(0).getReg();
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000248 unsigned Reg1 = MI->getOperand(1).getReg();
249 unsigned Reg2 = MI->getOperand(2).getReg();
Andrew Tricke3398282013-12-17 04:50:45 +0000250 unsigned SubReg1 = MI->getOperand(1).getSubReg();
251 unsigned SubReg2 = MI->getOperand(2).getSubReg();
Evan Chengdc2c8742006-11-15 20:58:11 +0000252 bool Reg1IsKill = MI->getOperand(1).isKill();
253 bool Reg2IsKill = MI->getOperand(2).isKill();
Evan Cheng03553bb2008-06-16 07:33:11 +0000254 bool ChangeReg0 = false;
Evan Cheng244183e2008-02-13 02:46:49 +0000255 // If machine instrs are no longer in two-address forms, update
256 // destination register as well.
257 if (Reg0 == Reg1) {
258 // Must be two address instruction!
Evan Cheng6cc775f2011-06-28 19:10:37 +0000259 assert(MI->getDesc().getOperandConstraint(0, MCOI::TIED_TO) &&
Evan Cheng244183e2008-02-13 02:46:49 +0000260 "Expecting a two-address instruction!");
Andrew Tricke3398282013-12-17 04:50:45 +0000261 assert(MI->getOperand(0).getSubReg() == SubReg1 && "Tied subreg mismatch");
Evan Cheng244183e2008-02-13 02:46:49 +0000262 Reg2IsKill = false;
Evan Cheng03553bb2008-06-16 07:33:11 +0000263 ChangeReg0 = true;
Evan Cheng244183e2008-02-13 02:46:49 +0000264 }
Evan Cheng03553bb2008-06-16 07:33:11 +0000265
266 // Masks.
267 unsigned MB = MI->getOperand(4).getImm();
268 unsigned ME = MI->getOperand(5).getImm();
269
270 if (NewMI) {
271 // Create a new instruction.
272 unsigned Reg0 = ChangeReg0 ? Reg2 : MI->getOperand(0).getReg();
273 bool Reg0IsDead = MI->getOperand(0).isDead();
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000274 return BuildMI(MF, MI->getDebugLoc(), MI->getDesc())
Bill Wendlingf7b83c72009-05-13 21:33:08 +0000275 .addReg(Reg0, RegState::Define | getDeadRegState(Reg0IsDead))
276 .addReg(Reg2, getKillRegState(Reg2IsKill))
277 .addReg(Reg1, getKillRegState(Reg1IsKill))
Evan Cheng03553bb2008-06-16 07:33:11 +0000278 .addImm((ME+1) & 31)
279 .addImm((MB-1) & 31);
280 }
281
Andrew Tricke3398282013-12-17 04:50:45 +0000282 if (ChangeReg0) {
Evan Cheng03553bb2008-06-16 07:33:11 +0000283 MI->getOperand(0).setReg(Reg2);
Andrew Tricke3398282013-12-17 04:50:45 +0000284 MI->getOperand(0).setSubReg(SubReg2);
285 }
Chris Lattner10d63412006-05-04 17:52:23 +0000286 MI->getOperand(2).setReg(Reg1);
287 MI->getOperand(1).setReg(Reg2);
Andrew Tricke3398282013-12-17 04:50:45 +0000288 MI->getOperand(2).setSubReg(SubReg1);
289 MI->getOperand(1).setSubReg(SubReg2);
Chris Lattner60055892007-12-30 21:56:09 +0000290 MI->getOperand(2).setIsKill(Reg1IsKill);
291 MI->getOperand(1).setIsKill(Reg2IsKill);
Andrew Trickc416ba62010-12-24 04:28:06 +0000292
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000293 // Swap the mask around.
Chris Lattner5c463782007-12-30 20:49:49 +0000294 MI->getOperand(4).setImm((ME+1) & 31);
295 MI->getOperand(5).setImm((MB-1) & 31);
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000296 return MI;
297}
Chris Lattnerea79d9fd732006-03-05 23:49:55 +0000298
Hal Finkel6c32ff32014-03-25 19:26:43 +0000299bool PPCInstrInfo::findCommutedOpIndices(MachineInstr *MI, unsigned &SrcOpIdx1,
300 unsigned &SrcOpIdx2) const {
301 // For VSX A-Type FMA instructions, it is the first two operands that can be
302 // commuted, however, because the non-encoded tied input operand is listed
303 // first, the operands to swap are actually the second and third.
304
305 int AltOpc = PPC::getAltVSXFMAOpcode(MI->getOpcode());
306 if (AltOpc == -1)
307 return TargetInstrInfo::findCommutedOpIndices(MI, SrcOpIdx1, SrcOpIdx2);
308
309 SrcOpIdx1 = 2;
310 SrcOpIdx2 = 3;
311 return true;
312}
313
Andrew Trickc416ba62010-12-24 04:28:06 +0000314void PPCInstrInfo::insertNoop(MachineBasicBlock &MBB,
Chris Lattnerea79d9fd732006-03-05 23:49:55 +0000315 MachineBasicBlock::iterator MI) const {
Hal Finkelceb1f122013-12-12 00:19:11 +0000316 // This function is used for scheduling, and the nop wanted here is the type
317 // that terminates dispatch groups on the POWER cores.
Eric Christopher1dcea732014-06-12 21:48:52 +0000318 unsigned Directive = Subtarget.getDarwinDirective();
Hal Finkelceb1f122013-12-12 00:19:11 +0000319 unsigned Opcode;
320 switch (Directive) {
321 default: Opcode = PPC::NOP; break;
322 case PPC::DIR_PWR6: Opcode = PPC::NOP_GT_PWR6; break;
323 case PPC::DIR_PWR7: Opcode = PPC::NOP_GT_PWR7; break;
324 }
Chris Lattnera47294ed2006-10-13 21:21:17 +0000325
Hal Finkelceb1f122013-12-12 00:19:11 +0000326 DebugLoc DL;
327 BuildMI(MBB, MI, DL, get(Opcode));
328}
Chris Lattnera47294ed2006-10-13 21:21:17 +0000329
330// Branch analysis.
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000331// Note: If the condition register is set to CTR or CTR8 then this is a
332// BDNZ (imm == 1) or BDZ (imm == 0) branch.
Chris Lattnera47294ed2006-10-13 21:21:17 +0000333bool PPCInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
334 MachineBasicBlock *&FBB,
Evan Cheng64dfcac2009-02-09 07:14:22 +0000335 SmallVectorImpl<MachineOperand> &Cond,
336 bool AllowModify) const {
Eric Christopher1dcea732014-06-12 21:48:52 +0000337 bool isPPC64 = Subtarget.isPPC64();
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000338
Chris Lattnera47294ed2006-10-13 21:21:17 +0000339 // If the block has no terminators, it just falls into the block after it.
340 MachineBasicBlock::iterator I = MBB.end();
Dale Johannesen4244d122010-04-02 01:38:09 +0000341 if (I == MBB.begin())
342 return false;
343 --I;
344 while (I->isDebugValue()) {
345 if (I == MBB.begin())
346 return false;
347 --I;
348 }
349 if (!isUnpredicatedTerminator(I))
Chris Lattnera47294ed2006-10-13 21:21:17 +0000350 return false;
351
352 // Get the last instruction in the block.
353 MachineInstr *LastInst = I;
Andrew Trickc416ba62010-12-24 04:28:06 +0000354
Chris Lattnera47294ed2006-10-13 21:21:17 +0000355 // If there is only one terminator instruction, process it.
Evan Cheng5514bbe2007-06-08 21:59:56 +0000356 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
Chris Lattnera47294ed2006-10-13 21:21:17 +0000357 if (LastInst->getOpcode() == PPC::B) {
Evan Cheng8f43afd2009-05-08 23:09:25 +0000358 if (!LastInst->getOperand(0).isMBB())
359 return true;
Chris Lattnera5bb3702007-12-30 23:10:15 +0000360 TBB = LastInst->getOperand(0).getMBB();
Chris Lattnera47294ed2006-10-13 21:21:17 +0000361 return false;
Chris Lattnere0263792006-11-17 22:14:47 +0000362 } else if (LastInst->getOpcode() == PPC::BCC) {
Evan Cheng8f43afd2009-05-08 23:09:25 +0000363 if (!LastInst->getOperand(2).isMBB())
364 return true;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000365 // Block ends with fall-through condbranch.
Chris Lattnera5bb3702007-12-30 23:10:15 +0000366 TBB = LastInst->getOperand(2).getMBB();
Chris Lattnera47294ed2006-10-13 21:21:17 +0000367 Cond.push_back(LastInst->getOperand(0));
368 Cond.push_back(LastInst->getOperand(1));
Chris Lattner23f22de2006-10-21 06:03:11 +0000369 return false;
Hal Finkel940ab932014-02-28 00:27:01 +0000370 } else if (LastInst->getOpcode() == PPC::BC) {
371 if (!LastInst->getOperand(1).isMBB())
372 return true;
373 // Block ends with fall-through condbranch.
374 TBB = LastInst->getOperand(1).getMBB();
375 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET));
376 Cond.push_back(LastInst->getOperand(0));
377 return false;
378 } else if (LastInst->getOpcode() == PPC::BCn) {
379 if (!LastInst->getOperand(1).isMBB())
380 return true;
381 // Block ends with fall-through condbranch.
382 TBB = LastInst->getOperand(1).getMBB();
383 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_UNSET));
384 Cond.push_back(LastInst->getOperand(0));
385 return false;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000386 } else if (LastInst->getOpcode() == PPC::BDNZ8 ||
387 LastInst->getOpcode() == PPC::BDNZ) {
388 if (!LastInst->getOperand(0).isMBB())
389 return true;
Hal Finkelc6b5deb2012-06-08 19:19:53 +0000390 if (DisableCTRLoopAnal)
Hal Finkel821e0012012-06-08 15:38:25 +0000391 return true;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000392 TBB = LastInst->getOperand(0).getMBB();
393 Cond.push_back(MachineOperand::CreateImm(1));
394 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
395 true));
396 return false;
397 } else if (LastInst->getOpcode() == PPC::BDZ8 ||
398 LastInst->getOpcode() == PPC::BDZ) {
399 if (!LastInst->getOperand(0).isMBB())
400 return true;
Hal Finkelc6b5deb2012-06-08 19:19:53 +0000401 if (DisableCTRLoopAnal)
Hal Finkel821e0012012-06-08 15:38:25 +0000402 return true;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000403 TBB = LastInst->getOperand(0).getMBB();
404 Cond.push_back(MachineOperand::CreateImm(0));
405 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
406 true));
407 return false;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000408 }
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000409
Chris Lattnera47294ed2006-10-13 21:21:17 +0000410 // Otherwise, don't know what this is.
411 return true;
412 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000413
Chris Lattnera47294ed2006-10-13 21:21:17 +0000414 // Get the instruction before it if it's a terminator.
415 MachineInstr *SecondLastInst = I;
416
417 // If there are three terminators, we don't know what sort of block this is.
418 if (SecondLastInst && I != MBB.begin() &&
Evan Cheng5514bbe2007-06-08 21:59:56 +0000419 isUnpredicatedTerminator(--I))
Chris Lattnera47294ed2006-10-13 21:21:17 +0000420 return true;
Andrew Trickc416ba62010-12-24 04:28:06 +0000421
Chris Lattnere0263792006-11-17 22:14:47 +0000422 // If the block ends with PPC::B and PPC:BCC, handle it.
Andrew Trickc416ba62010-12-24 04:28:06 +0000423 if (SecondLastInst->getOpcode() == PPC::BCC &&
Chris Lattnera47294ed2006-10-13 21:21:17 +0000424 LastInst->getOpcode() == PPC::B) {
Evan Cheng8f43afd2009-05-08 23:09:25 +0000425 if (!SecondLastInst->getOperand(2).isMBB() ||
426 !LastInst->getOperand(0).isMBB())
427 return true;
Chris Lattnera5bb3702007-12-30 23:10:15 +0000428 TBB = SecondLastInst->getOperand(2).getMBB();
Chris Lattnera47294ed2006-10-13 21:21:17 +0000429 Cond.push_back(SecondLastInst->getOperand(0));
430 Cond.push_back(SecondLastInst->getOperand(1));
Chris Lattnera5bb3702007-12-30 23:10:15 +0000431 FBB = LastInst->getOperand(0).getMBB();
Chris Lattnera47294ed2006-10-13 21:21:17 +0000432 return false;
Hal Finkel940ab932014-02-28 00:27:01 +0000433 } else if (SecondLastInst->getOpcode() == PPC::BC &&
434 LastInst->getOpcode() == PPC::B) {
435 if (!SecondLastInst->getOperand(1).isMBB() ||
436 !LastInst->getOperand(0).isMBB())
437 return true;
438 TBB = SecondLastInst->getOperand(1).getMBB();
439 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET));
440 Cond.push_back(SecondLastInst->getOperand(0));
441 FBB = LastInst->getOperand(0).getMBB();
442 return false;
443 } else if (SecondLastInst->getOpcode() == PPC::BCn &&
444 LastInst->getOpcode() == PPC::B) {
445 if (!SecondLastInst->getOperand(1).isMBB() ||
446 !LastInst->getOperand(0).isMBB())
447 return true;
448 TBB = SecondLastInst->getOperand(1).getMBB();
449 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_UNSET));
450 Cond.push_back(SecondLastInst->getOperand(0));
451 FBB = LastInst->getOperand(0).getMBB();
452 return false;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000453 } else if ((SecondLastInst->getOpcode() == PPC::BDNZ8 ||
454 SecondLastInst->getOpcode() == PPC::BDNZ) &&
455 LastInst->getOpcode() == PPC::B) {
456 if (!SecondLastInst->getOperand(0).isMBB() ||
457 !LastInst->getOperand(0).isMBB())
458 return true;
Hal Finkelc6b5deb2012-06-08 19:19:53 +0000459 if (DisableCTRLoopAnal)
Hal Finkel821e0012012-06-08 15:38:25 +0000460 return true;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000461 TBB = SecondLastInst->getOperand(0).getMBB();
462 Cond.push_back(MachineOperand::CreateImm(1));
463 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
464 true));
465 FBB = LastInst->getOperand(0).getMBB();
466 return false;
467 } else if ((SecondLastInst->getOpcode() == PPC::BDZ8 ||
468 SecondLastInst->getOpcode() == PPC::BDZ) &&
469 LastInst->getOpcode() == PPC::B) {
470 if (!SecondLastInst->getOperand(0).isMBB() ||
471 !LastInst->getOperand(0).isMBB())
472 return true;
Hal Finkelc6b5deb2012-06-08 19:19:53 +0000473 if (DisableCTRLoopAnal)
Hal Finkel821e0012012-06-08 15:38:25 +0000474 return true;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000475 TBB = SecondLastInst->getOperand(0).getMBB();
476 Cond.push_back(MachineOperand::CreateImm(0));
477 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
478 true));
479 FBB = LastInst->getOperand(0).getMBB();
480 return false;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000481 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000482
Dale Johannesenc6855462007-06-13 17:59:52 +0000483 // If the block ends with two PPC:Bs, handle it. The second one is not
484 // executed, so remove it.
Andrew Trickc416ba62010-12-24 04:28:06 +0000485 if (SecondLastInst->getOpcode() == PPC::B &&
Dale Johannesenc6855462007-06-13 17:59:52 +0000486 LastInst->getOpcode() == PPC::B) {
Evan Cheng8f43afd2009-05-08 23:09:25 +0000487 if (!SecondLastInst->getOperand(0).isMBB())
488 return true;
Chris Lattnera5bb3702007-12-30 23:10:15 +0000489 TBB = SecondLastInst->getOperand(0).getMBB();
Dale Johannesenc6855462007-06-13 17:59:52 +0000490 I = LastInst;
Evan Cheng64dfcac2009-02-09 07:14:22 +0000491 if (AllowModify)
492 I->eraseFromParent();
Dale Johannesenc6855462007-06-13 17:59:52 +0000493 return false;
494 }
495
Chris Lattnera47294ed2006-10-13 21:21:17 +0000496 // Otherwise, can't handle this.
497 return true;
498}
499
Evan Cheng99be49d2007-05-18 00:05:48 +0000500unsigned PPCInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
Chris Lattnera47294ed2006-10-13 21:21:17 +0000501 MachineBasicBlock::iterator I = MBB.end();
Evan Cheng99be49d2007-05-18 00:05:48 +0000502 if (I == MBB.begin()) return 0;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000503 --I;
Dale Johannesen4244d122010-04-02 01:38:09 +0000504 while (I->isDebugValue()) {
505 if (I == MBB.begin())
506 return 0;
507 --I;
508 }
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000509 if (I->getOpcode() != PPC::B && I->getOpcode() != PPC::BCC &&
Hal Finkel940ab932014-02-28 00:27:01 +0000510 I->getOpcode() != PPC::BC && I->getOpcode() != PPC::BCn &&
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000511 I->getOpcode() != PPC::BDNZ8 && I->getOpcode() != PPC::BDNZ &&
512 I->getOpcode() != PPC::BDZ8 && I->getOpcode() != PPC::BDZ)
Evan Cheng99be49d2007-05-18 00:05:48 +0000513 return 0;
Andrew Trickc416ba62010-12-24 04:28:06 +0000514
Chris Lattnera47294ed2006-10-13 21:21:17 +0000515 // Remove the branch.
516 I->eraseFromParent();
Andrew Trickc416ba62010-12-24 04:28:06 +0000517
Chris Lattnera47294ed2006-10-13 21:21:17 +0000518 I = MBB.end();
519
Evan Cheng99be49d2007-05-18 00:05:48 +0000520 if (I == MBB.begin()) return 1;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000521 --I;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000522 if (I->getOpcode() != PPC::BCC &&
Hal Finkel940ab932014-02-28 00:27:01 +0000523 I->getOpcode() != PPC::BC && I->getOpcode() != PPC::BCn &&
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000524 I->getOpcode() != PPC::BDNZ8 && I->getOpcode() != PPC::BDNZ &&
525 I->getOpcode() != PPC::BDZ8 && I->getOpcode() != PPC::BDZ)
Evan Cheng99be49d2007-05-18 00:05:48 +0000526 return 1;
Andrew Trickc416ba62010-12-24 04:28:06 +0000527
Chris Lattnera47294ed2006-10-13 21:21:17 +0000528 // Remove the branch.
529 I->eraseFromParent();
Evan Cheng99be49d2007-05-18 00:05:48 +0000530 return 2;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000531}
532
Evan Cheng99be49d2007-05-18 00:05:48 +0000533unsigned
534PPCInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
535 MachineBasicBlock *FBB,
Stuart Hastings0125b642010-06-17 22:43:56 +0000536 const SmallVectorImpl<MachineOperand> &Cond,
537 DebugLoc DL) const {
Chris Lattnera61f0102006-10-17 18:06:55 +0000538 // Shouldn't be a fall through.
539 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Andrew Trickc416ba62010-12-24 04:28:06 +0000540 assert((Cond.size() == 2 || Cond.size() == 0) &&
Chris Lattner94e04442006-10-21 05:36:13 +0000541 "PPC branch conditions have two components!");
Andrew Trickc416ba62010-12-24 04:28:06 +0000542
Eric Christopher1dcea732014-06-12 21:48:52 +0000543 bool isPPC64 = Subtarget.isPPC64();
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000544
Chris Lattner94e04442006-10-21 05:36:13 +0000545 // One-way branch.
Craig Topper062a2ba2014-04-25 05:30:21 +0000546 if (!FBB) {
Chris Lattner94e04442006-10-21 05:36:13 +0000547 if (Cond.empty()) // Unconditional branch
Stuart Hastings0125b642010-06-17 22:43:56 +0000548 BuildMI(&MBB, DL, get(PPC::B)).addMBB(TBB);
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000549 else if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
550 BuildMI(&MBB, DL, get(Cond[0].getImm() ?
551 (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
552 (isPPC64 ? PPC::BDZ8 : PPC::BDZ))).addMBB(TBB);
Hal Finkel940ab932014-02-28 00:27:01 +0000553 else if (Cond[0].getImm() == PPC::PRED_BIT_SET)
554 BuildMI(&MBB, DL, get(PPC::BC)).addOperand(Cond[1]).addMBB(TBB);
555 else if (Cond[0].getImm() == PPC::PRED_BIT_UNSET)
556 BuildMI(&MBB, DL, get(PPC::BCn)).addOperand(Cond[1]).addMBB(TBB);
Chris Lattner94e04442006-10-21 05:36:13 +0000557 else // Conditional branch
Stuart Hastings0125b642010-06-17 22:43:56 +0000558 BuildMI(&MBB, DL, get(PPC::BCC))
Hal Finkel940ab932014-02-28 00:27:01 +0000559 .addImm(Cond[0].getImm()).addOperand(Cond[1]).addMBB(TBB);
Evan Cheng99be49d2007-05-18 00:05:48 +0000560 return 1;
Chris Lattnera61f0102006-10-17 18:06:55 +0000561 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000562
Chris Lattnerd8816602006-10-21 05:42:09 +0000563 // Two-way Conditional Branch.
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000564 if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
565 BuildMI(&MBB, DL, get(Cond[0].getImm() ?
566 (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
567 (isPPC64 ? PPC::BDZ8 : PPC::BDZ))).addMBB(TBB);
Hal Finkel940ab932014-02-28 00:27:01 +0000568 else if (Cond[0].getImm() == PPC::PRED_BIT_SET)
569 BuildMI(&MBB, DL, get(PPC::BC)).addOperand(Cond[1]).addMBB(TBB);
570 else if (Cond[0].getImm() == PPC::PRED_BIT_UNSET)
571 BuildMI(&MBB, DL, get(PPC::BCn)).addOperand(Cond[1]).addMBB(TBB);
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000572 else
573 BuildMI(&MBB, DL, get(PPC::BCC))
Hal Finkel940ab932014-02-28 00:27:01 +0000574 .addImm(Cond[0].getImm()).addOperand(Cond[1]).addMBB(TBB);
Stuart Hastings0125b642010-06-17 22:43:56 +0000575 BuildMI(&MBB, DL, get(PPC::B)).addMBB(FBB);
Evan Cheng99be49d2007-05-18 00:05:48 +0000576 return 2;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000577}
578
Hal Finkeled6a2852013-04-05 23:29:01 +0000579// Select analysis.
580bool PPCInstrInfo::canInsertSelect(const MachineBasicBlock &MBB,
581 const SmallVectorImpl<MachineOperand> &Cond,
582 unsigned TrueReg, unsigned FalseReg,
583 int &CondCycles, int &TrueCycles, int &FalseCycles) const {
Eric Christopher1dcea732014-06-12 21:48:52 +0000584 if (!Subtarget.hasISEL())
Hal Finkeled6a2852013-04-05 23:29:01 +0000585 return false;
586
587 if (Cond.size() != 2)
588 return false;
589
590 // If this is really a bdnz-like condition, then it cannot be turned into a
591 // select.
592 if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
593 return false;
594
595 // Check register classes.
596 const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
597 const TargetRegisterClass *RC =
598 RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg));
599 if (!RC)
600 return false;
601
602 // isel is for regular integer GPRs only.
603 if (!PPC::GPRCRegClass.hasSubClassEq(RC) &&
Hal Finkel8e8618a2013-07-15 20:22:58 +0000604 !PPC::GPRC_NOR0RegClass.hasSubClassEq(RC) &&
605 !PPC::G8RCRegClass.hasSubClassEq(RC) &&
606 !PPC::G8RC_NOX0RegClass.hasSubClassEq(RC))
Hal Finkeled6a2852013-04-05 23:29:01 +0000607 return false;
608
609 // FIXME: These numbers are for the A2, how well they work for other cores is
610 // an open question. On the A2, the isel instruction has a 2-cycle latency
611 // but single-cycle throughput. These numbers are used in combination with
612 // the MispredictPenalty setting from the active SchedMachineModel.
613 CondCycles = 1;
614 TrueCycles = 1;
615 FalseCycles = 1;
616
617 return true;
618}
619
620void PPCInstrInfo::insertSelect(MachineBasicBlock &MBB,
621 MachineBasicBlock::iterator MI, DebugLoc dl,
622 unsigned DestReg,
623 const SmallVectorImpl<MachineOperand> &Cond,
624 unsigned TrueReg, unsigned FalseReg) const {
625 assert(Cond.size() == 2 &&
626 "PPC branch conditions have two components!");
627
Eric Christopher1dcea732014-06-12 21:48:52 +0000628 assert(Subtarget.hasISEL() &&
Hal Finkeled6a2852013-04-05 23:29:01 +0000629 "Cannot insert select on target without ISEL support");
630
631 // Get the register classes.
632 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
633 const TargetRegisterClass *RC =
634 RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg));
635 assert(RC && "TrueReg and FalseReg must have overlapping register classes");
Hal Finkel8e8618a2013-07-15 20:22:58 +0000636
637 bool Is64Bit = PPC::G8RCRegClass.hasSubClassEq(RC) ||
638 PPC::G8RC_NOX0RegClass.hasSubClassEq(RC);
639 assert((Is64Bit ||
640 PPC::GPRCRegClass.hasSubClassEq(RC) ||
641 PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) &&
Hal Finkeled6a2852013-04-05 23:29:01 +0000642 "isel is for regular integer GPRs only");
643
Hal Finkel8e8618a2013-07-15 20:22:58 +0000644 unsigned OpCode = Is64Bit ? PPC::ISEL8 : PPC::ISEL;
Hal Finkeled6a2852013-04-05 23:29:01 +0000645 unsigned SelectPred = Cond[0].getImm();
646
647 unsigned SubIdx;
648 bool SwapOps;
649 switch (SelectPred) {
650 default: llvm_unreachable("invalid predicate for isel");
651 case PPC::PRED_EQ: SubIdx = PPC::sub_eq; SwapOps = false; break;
652 case PPC::PRED_NE: SubIdx = PPC::sub_eq; SwapOps = true; break;
653 case PPC::PRED_LT: SubIdx = PPC::sub_lt; SwapOps = false; break;
654 case PPC::PRED_GE: SubIdx = PPC::sub_lt; SwapOps = true; break;
655 case PPC::PRED_GT: SubIdx = PPC::sub_gt; SwapOps = false; break;
656 case PPC::PRED_LE: SubIdx = PPC::sub_gt; SwapOps = true; break;
657 case PPC::PRED_UN: SubIdx = PPC::sub_un; SwapOps = false; break;
658 case PPC::PRED_NU: SubIdx = PPC::sub_un; SwapOps = true; break;
Hal Finkel940ab932014-02-28 00:27:01 +0000659 case PPC::PRED_BIT_SET: SubIdx = 0; SwapOps = false; break;
660 case PPC::PRED_BIT_UNSET: SubIdx = 0; SwapOps = true; break;
Hal Finkeled6a2852013-04-05 23:29:01 +0000661 }
662
663 unsigned FirstReg = SwapOps ? FalseReg : TrueReg,
664 SecondReg = SwapOps ? TrueReg : FalseReg;
665
666 // The first input register of isel cannot be r0. If it is a member
667 // of a register class that can be r0, then copy it first (the
668 // register allocator should eliminate the copy).
669 if (MRI.getRegClass(FirstReg)->contains(PPC::R0) ||
670 MRI.getRegClass(FirstReg)->contains(PPC::X0)) {
671 const TargetRegisterClass *FirstRC =
672 MRI.getRegClass(FirstReg)->contains(PPC::X0) ?
673 &PPC::G8RC_NOX0RegClass : &PPC::GPRC_NOR0RegClass;
674 unsigned OldFirstReg = FirstReg;
675 FirstReg = MRI.createVirtualRegister(FirstRC);
676 BuildMI(MBB, MI, dl, get(TargetOpcode::COPY), FirstReg)
677 .addReg(OldFirstReg);
678 }
679
680 BuildMI(MBB, MI, dl, get(OpCode), DestReg)
681 .addReg(FirstReg).addReg(SecondReg)
682 .addReg(Cond[1].getReg(), 0, SubIdx);
683}
684
Jakob Stoklund Olesen0d611972010-07-11 07:31:00 +0000685void PPCInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
686 MachineBasicBlock::iterator I, DebugLoc DL,
687 unsigned DestReg, unsigned SrcReg,
688 bool KillSrc) const {
Hal Finkel27774d92014-03-13 07:58:58 +0000689 // We can end up with self copies and similar things as a result of VSX copy
Hal Finkel9dcb3582014-03-27 22:46:28 +0000690 // legalization. Promote them here.
Hal Finkel27774d92014-03-13 07:58:58 +0000691 const TargetRegisterInfo *TRI = &getRegisterInfo();
692 if (PPC::F8RCRegClass.contains(DestReg) &&
693 PPC::VSLRCRegClass.contains(SrcReg)) {
694 unsigned SuperReg =
695 TRI->getMatchingSuperReg(DestReg, PPC::sub_64, &PPC::VSRCRegClass);
696
Hal Finkel9dcb3582014-03-27 22:46:28 +0000697 if (VSXSelfCopyCrash && SrcReg == SuperReg)
698 llvm_unreachable("nop VSX copy");
Hal Finkel27774d92014-03-13 07:58:58 +0000699
700 DestReg = SuperReg;
701 } else if (PPC::VRRCRegClass.contains(DestReg) &&
702 PPC::VSHRCRegClass.contains(SrcReg)) {
703 unsigned SuperReg =
704 TRI->getMatchingSuperReg(DestReg, PPC::sub_128, &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::F8RCRegClass.contains(SrcReg) &&
711 PPC::VSLRCRegClass.contains(DestReg)) {
712 unsigned SuperReg =
713 TRI->getMatchingSuperReg(SrcReg, PPC::sub_64, &PPC::VSRCRegClass);
714
Hal Finkel9dcb3582014-03-27 22:46:28 +0000715 if (VSXSelfCopyCrash && DestReg == SuperReg)
716 llvm_unreachable("nop VSX copy");
Hal Finkel27774d92014-03-13 07:58:58 +0000717
718 SrcReg = SuperReg;
719 } else if (PPC::VRRCRegClass.contains(SrcReg) &&
720 PPC::VSHRCRegClass.contains(DestReg)) {
721 unsigned SuperReg =
722 TRI->getMatchingSuperReg(SrcReg, PPC::sub_128, &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 }
729
Jakob Stoklund Olesen0d611972010-07-11 07:31:00 +0000730 unsigned Opc;
731 if (PPC::GPRCRegClass.contains(DestReg, SrcReg))
732 Opc = PPC::OR;
733 else if (PPC::G8RCRegClass.contains(DestReg, SrcReg))
734 Opc = PPC::OR8;
735 else if (PPC::F4RCRegClass.contains(DestReg, SrcReg))
736 Opc = PPC::FMR;
737 else if (PPC::CRRCRegClass.contains(DestReg, SrcReg))
738 Opc = PPC::MCRF;
739 else if (PPC::VRRCRegClass.contains(DestReg, SrcReg))
740 Opc = PPC::VOR;
Hal Finkel27774d92014-03-13 07:58:58 +0000741 else if (PPC::VSRCRegClass.contains(DestReg, SrcReg))
Hal Finkelbbad2332014-03-24 09:36:36 +0000742 // There are two different ways this can be done:
Hal Finkel27774d92014-03-13 07:58:58 +0000743 // 1. xxlor : This has lower latency (on the P7), 2 cycles, but can only
744 // issue in VSU pipeline 0.
745 // 2. xmovdp/xmovsp: This has higher latency (on the P7), 6 cycles, but
746 // can go to either pipeline.
Hal Finkelbbad2332014-03-24 09:36:36 +0000747 // We'll always use xxlor here, because in practically all cases where
748 // copies are generated, they are close enough to some use that the
749 // lower-latency form is preferable.
Hal Finkel27774d92014-03-13 07:58:58 +0000750 Opc = PPC::XXLOR;
Hal Finkel19be5062014-03-29 05:29:01 +0000751 else if (PPC::VSFRCRegClass.contains(DestReg, SrcReg))
752 Opc = PPC::XXLORf;
Jakob Stoklund Olesen0d611972010-07-11 07:31:00 +0000753 else if (PPC::CRBITRCRegClass.contains(DestReg, SrcReg))
754 Opc = PPC::CROR;
755 else
756 llvm_unreachable("Impossible reg-to-reg copy");
Owen Anderson7a73ae92007-12-31 06:32:00 +0000757
Evan Cheng6cc775f2011-06-28 19:10:37 +0000758 const MCInstrDesc &MCID = get(Opc);
759 if (MCID.getNumOperands() == 3)
760 BuildMI(MBB, I, DL, MCID, DestReg)
Jakob Stoklund Olesen0d611972010-07-11 07:31:00 +0000761 .addReg(SrcReg).addReg(SrcReg, getKillRegState(KillSrc));
762 else
Evan Cheng6cc775f2011-06-28 19:10:37 +0000763 BuildMI(MBB, I, DL, MCID, DestReg).addReg(SrcReg, getKillRegState(KillSrc));
Owen Anderson7a73ae92007-12-31 06:32:00 +0000764}
765
Hal Finkel8f6834d2011-12-05 17:55:17 +0000766// This function returns true if a CR spill is necessary and false otherwise.
Bill Wendlingc6c48fc2008-03-10 22:49:16 +0000767bool
Dan Gohman3b460302008-07-07 23:14:23 +0000768PPCInstrInfo::StoreRegToStackSlot(MachineFunction &MF,
769 unsigned SrcReg, bool isKill,
Bill Wendlingc6c48fc2008-03-10 22:49:16 +0000770 int FrameIdx,
771 const TargetRegisterClass *RC,
Hal Finkelfcc51d42013-03-17 04:43:44 +0000772 SmallVectorImpl<MachineInstr*> &NewMIs,
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000773 bool &NonRI, bool &SpillsVRS) const{
Hal Finkel37714b82013-03-27 21:21:15 +0000774 // Note: If additional store instructions are added here,
775 // update isStoreToStackSlot.
776
Chris Lattner6f306d72010-04-02 20:16:16 +0000777 DebugLoc DL;
Hal Finkel4e703bc2014-01-28 05:32:58 +0000778 if (PPC::GPRCRegClass.hasSubClassEq(RC) ||
779 PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) {
Hal Finkel794e05b2013-03-23 17:14:27 +0000780 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
781 .addReg(SrcReg,
782 getKillRegState(isKill)),
783 FrameIdx));
Hal Finkel4e703bc2014-01-28 05:32:58 +0000784 } else if (PPC::G8RCRegClass.hasSubClassEq(RC) ||
785 PPC::G8RC_NOX0RegClass.hasSubClassEq(RC)) {
Hal Finkel794e05b2013-03-23 17:14:27 +0000786 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STD))
787 .addReg(SrcReg,
788 getKillRegState(isKill)),
789 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000790 } else if (PPC::F8RCRegClass.hasSubClassEq(RC)) {
Dale Johannesen6b8c76a2009-02-12 23:08:38 +0000791 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFD))
Bill Wendlingf7b83c72009-05-13 21:33:08 +0000792 .addReg(SrcReg,
793 getKillRegState(isKill)),
794 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000795 } else if (PPC::F4RCRegClass.hasSubClassEq(RC)) {
Dale Johannesen6b8c76a2009-02-12 23:08:38 +0000796 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFS))
Bill Wendlingf7b83c72009-05-13 21:33:08 +0000797 .addReg(SrcReg,
798 getKillRegState(isKill)),
799 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000800 } else if (PPC::CRRCRegClass.hasSubClassEq(RC)) {
Hal Finkele154c8f2013-03-12 14:12:16 +0000801 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CR))
802 .addReg(SrcReg,
803 getKillRegState(isKill)),
804 FrameIdx));
805 return true;
Craig Topperabadc662012-04-20 06:31:50 +0000806 } else if (PPC::CRBITRCRegClass.hasSubClassEq(RC)) {
Hal Finkel940ab932014-02-28 00:27:01 +0000807 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CRBIT))
808 .addReg(SrcReg,
809 getKillRegState(isKill)),
810 FrameIdx));
811 return true;
Craig Topperabadc662012-04-20 06:31:50 +0000812 } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) {
Hal Finkelfcc51d42013-03-17 04:43:44 +0000813 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STVX))
814 .addReg(SrcReg,
815 getKillRegState(isKill)),
816 FrameIdx));
817 NonRI = true;
Hal Finkel27774d92014-03-13 07:58:58 +0000818 } else if (PPC::VSRCRegClass.hasSubClassEq(RC)) {
819 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STXVD2X))
820 .addReg(SrcReg,
821 getKillRegState(isKill)),
822 FrameIdx));
823 NonRI = true;
Hal Finkel19be5062014-03-29 05:29:01 +0000824 } else if (PPC::VSFRCRegClass.hasSubClassEq(RC)) {
825 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STXSDX))
826 .addReg(SrcReg,
827 getKillRegState(isKill)),
828 FrameIdx));
829 NonRI = true;
Hal Finkela1431df2013-03-21 19:03:21 +0000830 } else if (PPC::VRSAVERCRegClass.hasSubClassEq(RC)) {
Eric Christopher1dcea732014-06-12 21:48:52 +0000831 assert(Subtarget.isDarwin() &&
Hal Finkela7b06302013-03-27 00:02:20 +0000832 "VRSAVE only needs spill/restore on Darwin");
Hal Finkela1431df2013-03-21 19:03:21 +0000833 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_VRSAVE))
834 .addReg(SrcReg,
835 getKillRegState(isKill)),
836 FrameIdx));
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000837 SpillsVRS = true;
Owen Andersoneee14602008-01-01 21:11:32 +0000838 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000839 llvm_unreachable("Unknown regclass!");
Owen Andersoneee14602008-01-01 21:11:32 +0000840 }
Bill Wendling632ea652008-03-03 22:19:16 +0000841
842 return false;
Owen Andersoneee14602008-01-01 21:11:32 +0000843}
844
845void
846PPCInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
Bill Wendling632ea652008-03-03 22:19:16 +0000847 MachineBasicBlock::iterator MI,
848 unsigned SrcReg, bool isKill, int FrameIdx,
Evan Chengefb126a2010-05-06 19:06:44 +0000849 const TargetRegisterClass *RC,
850 const TargetRegisterInfo *TRI) const {
Dan Gohman3b460302008-07-07 23:14:23 +0000851 MachineFunction &MF = *MBB.getParent();
Owen Andersoneee14602008-01-01 21:11:32 +0000852 SmallVector<MachineInstr*, 4> NewMIs;
Bill Wendling632ea652008-03-03 22:19:16 +0000853
Hal Finkelbb420f12013-03-15 05:06:04 +0000854 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
855 FuncInfo->setHasSpills();
856
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000857 bool NonRI = false, SpillsVRS = false;
858 if (StoreRegToStackSlot(MF, SrcReg, isKill, FrameIdx, RC, NewMIs,
859 NonRI, SpillsVRS))
Bill Wendling632ea652008-03-03 22:19:16 +0000860 FuncInfo->setSpillsCR();
Bill Wendling632ea652008-03-03 22:19:16 +0000861
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000862 if (SpillsVRS)
863 FuncInfo->setSpillsVRSAVE();
864
Hal Finkelfcc51d42013-03-17 04:43:44 +0000865 if (NonRI)
866 FuncInfo->setHasNonRISpills();
867
Owen Andersoneee14602008-01-01 21:11:32 +0000868 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
869 MBB.insert(MI, NewMIs[i]);
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +0000870
871 const MachineFrameInfo &MFI = *MF.getFrameInfo();
872 MachineMemOperand *MMO =
Jay Foad465101b2011-11-15 07:34:52 +0000873 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx),
Chris Lattnere3d864b2010-09-21 04:39:43 +0000874 MachineMemOperand::MOStore,
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +0000875 MFI.getObjectSize(FrameIdx),
876 MFI.getObjectAlignment(FrameIdx));
877 NewMIs.back()->addMemOperand(MF, MMO);
Owen Andersoneee14602008-01-01 21:11:32 +0000878}
879
Hal Finkelbde7f8f2011-12-06 20:55:36 +0000880bool
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000881PPCInstrInfo::LoadRegFromStackSlot(MachineFunction &MF, DebugLoc DL,
Dan Gohman3b460302008-07-07 23:14:23 +0000882 unsigned DestReg, int FrameIdx,
Bill Wendlingc6c48fc2008-03-10 22:49:16 +0000883 const TargetRegisterClass *RC,
Hal Finkelfcc51d42013-03-17 04:43:44 +0000884 SmallVectorImpl<MachineInstr*> &NewMIs,
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000885 bool &NonRI, bool &SpillsVRS) const{
Hal Finkel37714b82013-03-27 21:21:15 +0000886 // Note: If additional load instructions are added here,
887 // update isLoadFromStackSlot.
888
Hal Finkel4e703bc2014-01-28 05:32:58 +0000889 if (PPC::GPRCRegClass.hasSubClassEq(RC) ||
890 PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) {
Hal Finkel5791f512013-03-27 19:10:40 +0000891 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ),
892 DestReg), FrameIdx));
Hal Finkel4e703bc2014-01-28 05:32:58 +0000893 } else if (PPC::G8RCRegClass.hasSubClassEq(RC) ||
894 PPC::G8RC_NOX0RegClass.hasSubClassEq(RC)) {
Hal Finkel5791f512013-03-27 19:10:40 +0000895 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LD), DestReg),
896 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000897 } else if (PPC::F8RCRegClass.hasSubClassEq(RC)) {
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000898 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFD), DestReg),
Owen Andersoneee14602008-01-01 21:11:32 +0000899 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000900 } else if (PPC::F4RCRegClass.hasSubClassEq(RC)) {
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000901 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFS), DestReg),
Owen Andersoneee14602008-01-01 21:11:32 +0000902 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000903 } else if (PPC::CRRCRegClass.hasSubClassEq(RC)) {
Hal Finkele154c8f2013-03-12 14:12:16 +0000904 NewMIs.push_back(addFrameReference(BuildMI(MF, DL,
905 get(PPC::RESTORE_CR), DestReg),
906 FrameIdx));
907 return true;
Craig Topperabadc662012-04-20 06:31:50 +0000908 } else if (PPC::CRBITRCRegClass.hasSubClassEq(RC)) {
Hal Finkel940ab932014-02-28 00:27:01 +0000909 NewMIs.push_back(addFrameReference(BuildMI(MF, DL,
910 get(PPC::RESTORE_CRBIT), DestReg),
911 FrameIdx));
912 return true;
Craig Topperabadc662012-04-20 06:31:50 +0000913 } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) {
Hal Finkelfcc51d42013-03-17 04:43:44 +0000914 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LVX), DestReg),
915 FrameIdx));
916 NonRI = true;
Hal Finkel27774d92014-03-13 07:58:58 +0000917 } else if (PPC::VSRCRegClass.hasSubClassEq(RC)) {
918 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LXVD2X), DestReg),
919 FrameIdx));
920 NonRI = true;
Hal Finkel19be5062014-03-29 05:29:01 +0000921 } else if (PPC::VSFRCRegClass.hasSubClassEq(RC)) {
922 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LXSDX), DestReg),
923 FrameIdx));
924 NonRI = true;
Hal Finkela1431df2013-03-21 19:03:21 +0000925 } else if (PPC::VRSAVERCRegClass.hasSubClassEq(RC)) {
Eric Christopher1dcea732014-06-12 21:48:52 +0000926 assert(Subtarget.isDarwin() &&
Hal Finkela7b06302013-03-27 00:02:20 +0000927 "VRSAVE only needs spill/restore on Darwin");
Hal Finkela1431df2013-03-21 19:03:21 +0000928 NewMIs.push_back(addFrameReference(BuildMI(MF, DL,
929 get(PPC::RESTORE_VRSAVE),
930 DestReg),
931 FrameIdx));
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000932 SpillsVRS = true;
Owen Andersoneee14602008-01-01 21:11:32 +0000933 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000934 llvm_unreachable("Unknown regclass!");
Owen Andersoneee14602008-01-01 21:11:32 +0000935 }
Hal Finkelbde7f8f2011-12-06 20:55:36 +0000936
937 return false;
Owen Andersoneee14602008-01-01 21:11:32 +0000938}
939
940void
941PPCInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
Bill Wendling632ea652008-03-03 22:19:16 +0000942 MachineBasicBlock::iterator MI,
943 unsigned DestReg, int FrameIdx,
Evan Chengefb126a2010-05-06 19:06:44 +0000944 const TargetRegisterClass *RC,
945 const TargetRegisterInfo *TRI) const {
Dan Gohman3b460302008-07-07 23:14:23 +0000946 MachineFunction &MF = *MBB.getParent();
Owen Andersoneee14602008-01-01 21:11:32 +0000947 SmallVector<MachineInstr*, 4> NewMIs;
Chris Lattner6f306d72010-04-02 20:16:16 +0000948 DebugLoc DL;
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000949 if (MI != MBB.end()) DL = MI->getDebugLoc();
Hal Finkelfcc51d42013-03-17 04:43:44 +0000950
951 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
952 FuncInfo->setHasSpills();
953
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000954 bool NonRI = false, SpillsVRS = false;
955 if (LoadRegFromStackSlot(MF, DL, DestReg, FrameIdx, RC, NewMIs,
956 NonRI, SpillsVRS))
Hal Finkelbde7f8f2011-12-06 20:55:36 +0000957 FuncInfo->setSpillsCR();
Hal Finkelfcc51d42013-03-17 04:43:44 +0000958
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000959 if (SpillsVRS)
960 FuncInfo->setSpillsVRSAVE();
961
Hal Finkelfcc51d42013-03-17 04:43:44 +0000962 if (NonRI)
963 FuncInfo->setHasNonRISpills();
964
Owen Andersoneee14602008-01-01 21:11:32 +0000965 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
966 MBB.insert(MI, NewMIs[i]);
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +0000967
968 const MachineFrameInfo &MFI = *MF.getFrameInfo();
969 MachineMemOperand *MMO =
Jay Foad465101b2011-11-15 07:34:52 +0000970 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx),
Chris Lattnere3d864b2010-09-21 04:39:43 +0000971 MachineMemOperand::MOLoad,
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +0000972 MFI.getObjectSize(FrameIdx),
973 MFI.getObjectAlignment(FrameIdx));
974 NewMIs.back()->addMemOperand(MF, MMO);
Owen Andersoneee14602008-01-01 21:11:32 +0000975}
976
Chris Lattnera47294ed2006-10-13 21:21:17 +0000977bool PPCInstrInfo::
Owen Anderson4f6bf042008-08-14 22:49:33 +0000978ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
Chris Lattner23f22de2006-10-21 06:03:11 +0000979 assert(Cond.size() == 2 && "Invalid PPC branch opcode!");
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000980 if (Cond[1].getReg() == PPC::CTR8 || Cond[1].getReg() == PPC::CTR)
981 Cond[0].setImm(Cond[0].getImm() == 0 ? 1 : 0);
982 else
983 // Leave the CR# the same, but invert the condition.
984 Cond[0].setImm(PPC::InvertPredicate((PPC::Predicate)Cond[0].getImm()));
Chris Lattner23f22de2006-10-21 06:03:11 +0000985 return false;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000986}
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +0000987
Hal Finkeld61d4f82013-04-06 19:30:30 +0000988bool PPCInstrInfo::FoldImmediate(MachineInstr *UseMI, MachineInstr *DefMI,
989 unsigned Reg, MachineRegisterInfo *MRI) const {
990 // For some instructions, it is legal to fold ZERO into the RA register field.
991 // A zero immediate should always be loaded with a single li.
992 unsigned DefOpc = DefMI->getOpcode();
993 if (DefOpc != PPC::LI && DefOpc != PPC::LI8)
994 return false;
995 if (!DefMI->getOperand(1).isImm())
996 return false;
997 if (DefMI->getOperand(1).getImm() != 0)
998 return false;
999
1000 // Note that we cannot here invert the arguments of an isel in order to fold
1001 // a ZERO into what is presented as the second argument. All we have here
1002 // is the condition bit, and that might come from a CR-logical bit operation.
1003
1004 const MCInstrDesc &UseMCID = UseMI->getDesc();
1005
1006 // Only fold into real machine instructions.
1007 if (UseMCID.isPseudo())
1008 return false;
1009
1010 unsigned UseIdx;
1011 for (UseIdx = 0; UseIdx < UseMI->getNumOperands(); ++UseIdx)
1012 if (UseMI->getOperand(UseIdx).isReg() &&
1013 UseMI->getOperand(UseIdx).getReg() == Reg)
1014 break;
1015
1016 assert(UseIdx < UseMI->getNumOperands() && "Cannot find Reg in UseMI");
1017 assert(UseIdx < UseMCID.getNumOperands() && "No operand description for Reg");
1018
1019 const MCOperandInfo *UseInfo = &UseMCID.OpInfo[UseIdx];
1020
1021 // We can fold the zero if this register requires a GPRC_NOR0/G8RC_NOX0
1022 // register (which might also be specified as a pointer class kind).
1023 if (UseInfo->isLookupPtrRegClass()) {
1024 if (UseInfo->RegClass /* Kind */ != 1)
1025 return false;
1026 } else {
1027 if (UseInfo->RegClass != PPC::GPRC_NOR0RegClassID &&
1028 UseInfo->RegClass != PPC::G8RC_NOX0RegClassID)
1029 return false;
1030 }
1031
1032 // Make sure this is not tied to an output register (or otherwise
1033 // constrained). This is true for ST?UX registers, for example, which
1034 // are tied to their output registers.
1035 if (UseInfo->Constraints != 0)
1036 return false;
1037
1038 unsigned ZeroReg;
1039 if (UseInfo->isLookupPtrRegClass()) {
Eric Christopher1dcea732014-06-12 21:48:52 +00001040 bool isPPC64 = Subtarget.isPPC64();
Hal Finkeld61d4f82013-04-06 19:30:30 +00001041 ZeroReg = isPPC64 ? PPC::ZERO8 : PPC::ZERO;
1042 } else {
1043 ZeroReg = UseInfo->RegClass == PPC::G8RC_NOX0RegClassID ?
1044 PPC::ZERO8 : PPC::ZERO;
1045 }
1046
1047 bool DeleteDef = MRI->hasOneNonDBGUse(Reg);
1048 UseMI->getOperand(UseIdx).setReg(ZeroReg);
1049
1050 if (DeleteDef)
1051 DefMI->eraseFromParent();
1052
1053 return true;
1054}
1055
Hal Finkel30ae2292013-04-10 18:30:16 +00001056static bool MBBDefinesCTR(MachineBasicBlock &MBB) {
1057 for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end();
1058 I != IE; ++I)
1059 if (I->definesRegister(PPC::CTR) || I->definesRegister(PPC::CTR8))
1060 return true;
1061 return false;
1062}
1063
1064// We should make sure that, if we're going to predicate both sides of a
1065// condition (a diamond), that both sides don't define the counter register. We
1066// can predicate counter-decrement-based branches, but while that predicates
1067// the branching, it does not predicate the counter decrement. If we tried to
1068// merge the triangle into one predicated block, we'd decrement the counter
1069// twice.
1070bool PPCInstrInfo::isProfitableToIfCvt(MachineBasicBlock &TMBB,
1071 unsigned NumT, unsigned ExtraT,
1072 MachineBasicBlock &FMBB,
1073 unsigned NumF, unsigned ExtraF,
1074 const BranchProbability &Probability) const {
1075 return !(MBBDefinesCTR(TMBB) && MBBDefinesCTR(FMBB));
1076}
1077
1078
Hal Finkel5711eca2013-04-09 22:58:37 +00001079bool PPCInstrInfo::isPredicated(const MachineInstr *MI) const {
Hal Finkelf29285a2013-04-11 01:23:34 +00001080 // The predicated branches are identified by their type, not really by the
1081 // explicit presence of a predicate. Furthermore, some of them can be
1082 // predicated more than once. Because if conversion won't try to predicate
1083 // any instruction which already claims to be predicated (by returning true
1084 // here), always return false. In doing so, we let isPredicable() be the
1085 // final word on whether not the instruction can be (further) predicated.
1086
1087 return false;
Hal Finkel5711eca2013-04-09 22:58:37 +00001088}
1089
1090bool PPCInstrInfo::isUnpredicatedTerminator(const MachineInstr *MI) const {
1091 if (!MI->isTerminator())
1092 return false;
1093
1094 // Conditional branch is a special case.
1095 if (MI->isBranch() && !MI->isBarrier())
1096 return true;
1097
1098 return !isPredicated(MI);
1099}
1100
1101bool PPCInstrInfo::PredicateInstruction(
1102 MachineInstr *MI,
1103 const SmallVectorImpl<MachineOperand> &Pred) const {
1104 unsigned OpC = MI->getOpcode();
1105 if (OpC == PPC::BLR) {
1106 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) {
Eric Christopher1dcea732014-06-12 21:48:52 +00001107 bool isPPC64 = Subtarget.isPPC64();
Hal Finkel5711eca2013-04-09 22:58:37 +00001108 MI->setDesc(get(Pred[0].getImm() ?
1109 (isPPC64 ? PPC::BDNZLR8 : PPC::BDNZLR) :
1110 (isPPC64 ? PPC::BDZLR8 : PPC::BDZLR)));
Hal Finkel940ab932014-02-28 00:27:01 +00001111 } else if (Pred[0].getImm() == PPC::PRED_BIT_SET) {
Hal Finkel5711eca2013-04-09 22:58:37 +00001112 MI->setDesc(get(PPC::BCLR));
1113 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
Hal Finkel940ab932014-02-28 00:27:01 +00001114 .addReg(Pred[1].getReg());
1115 } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) {
1116 MI->setDesc(get(PPC::BCLRn));
1117 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1118 .addReg(Pred[1].getReg());
1119 } else {
1120 MI->setDesc(get(PPC::BCCLR));
1121 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
Hal Finkel5711eca2013-04-09 22:58:37 +00001122 .addImm(Pred[0].getImm())
1123 .addReg(Pred[1].getReg());
1124 }
1125
1126 return true;
1127 } else if (OpC == PPC::B) {
1128 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) {
Eric Christopher1dcea732014-06-12 21:48:52 +00001129 bool isPPC64 = Subtarget.isPPC64();
Hal Finkel5711eca2013-04-09 22:58:37 +00001130 MI->setDesc(get(Pred[0].getImm() ?
1131 (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
1132 (isPPC64 ? PPC::BDZ8 : PPC::BDZ)));
Hal Finkel940ab932014-02-28 00:27:01 +00001133 } else if (Pred[0].getImm() == PPC::PRED_BIT_SET) {
1134 MachineBasicBlock *MBB = MI->getOperand(0).getMBB();
1135 MI->RemoveOperand(0);
1136
1137 MI->setDesc(get(PPC::BC));
1138 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1139 .addReg(Pred[1].getReg())
1140 .addMBB(MBB);
1141 } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) {
1142 MachineBasicBlock *MBB = MI->getOperand(0).getMBB();
1143 MI->RemoveOperand(0);
1144
1145 MI->setDesc(get(PPC::BCn));
1146 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1147 .addReg(Pred[1].getReg())
1148 .addMBB(MBB);
Hal Finkel5711eca2013-04-09 22:58:37 +00001149 } else {
1150 MachineBasicBlock *MBB = MI->getOperand(0).getMBB();
1151 MI->RemoveOperand(0);
1152
1153 MI->setDesc(get(PPC::BCC));
1154 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1155 .addImm(Pred[0].getImm())
1156 .addReg(Pred[1].getReg())
1157 .addMBB(MBB);
1158 }
1159
1160 return true;
Hal Finkel500b0042013-04-10 06:42:34 +00001161 } else if (OpC == PPC::BCTR || OpC == PPC::BCTR8 ||
1162 OpC == PPC::BCTRL || OpC == PPC::BCTRL8) {
1163 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR)
1164 llvm_unreachable("Cannot predicate bctr[l] on the ctr register");
1165
1166 bool setLR = OpC == PPC::BCTRL || OpC == PPC::BCTRL8;
Eric Christopher1dcea732014-06-12 21:48:52 +00001167 bool isPPC64 = Subtarget.isPPC64();
Hal Finkel940ab932014-02-28 00:27:01 +00001168
1169 if (Pred[0].getImm() == PPC::PRED_BIT_SET) {
1170 MI->setDesc(get(isPPC64 ? (setLR ? PPC::BCCTRL8 : PPC::BCCTR8) :
1171 (setLR ? PPC::BCCTRL : PPC::BCCTR)));
1172 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1173 .addReg(Pred[1].getReg());
1174 return true;
1175 } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) {
1176 MI->setDesc(get(isPPC64 ? (setLR ? PPC::BCCTRL8n : PPC::BCCTR8n) :
1177 (setLR ? PPC::BCCTRLn : PPC::BCCTRn)));
1178 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1179 .addReg(Pred[1].getReg());
1180 return true;
1181 }
1182
1183 MI->setDesc(get(isPPC64 ? (setLR ? PPC::BCCCTRL8 : PPC::BCCCTR8) :
1184 (setLR ? PPC::BCCCTRL : PPC::BCCCTR)));
Hal Finkel500b0042013-04-10 06:42:34 +00001185 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1186 .addImm(Pred[0].getImm())
1187 .addReg(Pred[1].getReg());
1188 return true;
Hal Finkel5711eca2013-04-09 22:58:37 +00001189 }
1190
1191 return false;
1192}
1193
1194bool PPCInstrInfo::SubsumesPredicate(
1195 const SmallVectorImpl<MachineOperand> &Pred1,
1196 const SmallVectorImpl<MachineOperand> &Pred2) const {
1197 assert(Pred1.size() == 2 && "Invalid PPC first predicate");
1198 assert(Pred2.size() == 2 && "Invalid PPC second predicate");
1199
1200 if (Pred1[1].getReg() == PPC::CTR8 || Pred1[1].getReg() == PPC::CTR)
1201 return false;
1202 if (Pred2[1].getReg() == PPC::CTR8 || Pred2[1].getReg() == PPC::CTR)
1203 return false;
1204
Hal Finkel94a6f382013-12-11 23:12:25 +00001205 // P1 can only subsume P2 if they test the same condition register.
1206 if (Pred1[1].getReg() != Pred2[1].getReg())
1207 return false;
1208
Hal Finkel5711eca2013-04-09 22:58:37 +00001209 PPC::Predicate P1 = (PPC::Predicate) Pred1[0].getImm();
1210 PPC::Predicate P2 = (PPC::Predicate) Pred2[0].getImm();
1211
1212 if (P1 == P2)
1213 return true;
1214
1215 // Does P1 subsume P2, e.g. GE subsumes GT.
1216 if (P1 == PPC::PRED_LE &&
1217 (P2 == PPC::PRED_LT || P2 == PPC::PRED_EQ))
1218 return true;
1219 if (P1 == PPC::PRED_GE &&
1220 (P2 == PPC::PRED_GT || P2 == PPC::PRED_EQ))
1221 return true;
1222
1223 return false;
1224}
1225
1226bool PPCInstrInfo::DefinesPredicate(MachineInstr *MI,
1227 std::vector<MachineOperand> &Pred) const {
1228 // Note: At the present time, the contents of Pred from this function is
1229 // unused by IfConversion. This implementation follows ARM by pushing the
1230 // CR-defining operand. Because the 'DZ' and 'DNZ' count as types of
1231 // predicate, instructions defining CTR or CTR8 are also included as
1232 // predicate-defining instructions.
1233
1234 const TargetRegisterClass *RCs[] =
1235 { &PPC::CRRCRegClass, &PPC::CRBITRCRegClass,
1236 &PPC::CTRRCRegClass, &PPC::CTRRC8RegClass };
1237
1238 bool Found = false;
1239 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1240 const MachineOperand &MO = MI->getOperand(i);
Hal Finkelaf822012013-04-10 07:17:47 +00001241 for (unsigned c = 0; c < array_lengthof(RCs) && !Found; ++c) {
Hal Finkel5711eca2013-04-09 22:58:37 +00001242 const TargetRegisterClass *RC = RCs[c];
Hal Finkelaf822012013-04-10 07:17:47 +00001243 if (MO.isReg()) {
1244 if (MO.isDef() && RC->contains(MO.getReg())) {
Hal Finkel5711eca2013-04-09 22:58:37 +00001245 Pred.push_back(MO);
1246 Found = true;
1247 }
Hal Finkelaf822012013-04-10 07:17:47 +00001248 } else if (MO.isRegMask()) {
1249 for (TargetRegisterClass::iterator I = RC->begin(),
1250 IE = RC->end(); I != IE; ++I)
1251 if (MO.clobbersPhysReg(*I)) {
1252 Pred.push_back(MO);
1253 Found = true;
1254 }
Hal Finkel5711eca2013-04-09 22:58:37 +00001255 }
1256 }
1257 }
1258
1259 return Found;
1260}
1261
1262bool PPCInstrInfo::isPredicable(MachineInstr *MI) const {
1263 unsigned OpC = MI->getOpcode();
1264 switch (OpC) {
1265 default:
1266 return false;
1267 case PPC::B:
1268 case PPC::BLR:
Hal Finkel500b0042013-04-10 06:42:34 +00001269 case PPC::BCTR:
1270 case PPC::BCTR8:
1271 case PPC::BCTRL:
1272 case PPC::BCTRL8:
Hal Finkel5711eca2013-04-09 22:58:37 +00001273 return true;
1274 }
1275}
1276
Hal Finkel82656cb2013-04-18 22:15:08 +00001277bool PPCInstrInfo::analyzeCompare(const MachineInstr *MI,
1278 unsigned &SrcReg, unsigned &SrcReg2,
1279 int &Mask, int &Value) const {
1280 unsigned Opc = MI->getOpcode();
1281
1282 switch (Opc) {
1283 default: return false;
1284 case PPC::CMPWI:
1285 case PPC::CMPLWI:
1286 case PPC::CMPDI:
1287 case PPC::CMPLDI:
1288 SrcReg = MI->getOperand(1).getReg();
1289 SrcReg2 = 0;
1290 Value = MI->getOperand(2).getImm();
1291 Mask = 0xFFFF;
1292 return true;
1293 case PPC::CMPW:
1294 case PPC::CMPLW:
1295 case PPC::CMPD:
1296 case PPC::CMPLD:
1297 case PPC::FCMPUS:
1298 case PPC::FCMPUD:
1299 SrcReg = MI->getOperand(1).getReg();
1300 SrcReg2 = MI->getOperand(2).getReg();
1301 return true;
1302 }
1303}
Hal Finkele6322392013-04-19 22:08:38 +00001304
Hal Finkel82656cb2013-04-18 22:15:08 +00001305bool PPCInstrInfo::optimizeCompareInstr(MachineInstr *CmpInstr,
1306 unsigned SrcReg, unsigned SrcReg2,
1307 int Mask, int Value,
1308 const MachineRegisterInfo *MRI) const {
Hal Finkelb12da6b2013-04-18 22:54:25 +00001309 if (DisableCmpOpt)
1310 return false;
1311
Hal Finkel82656cb2013-04-18 22:15:08 +00001312 int OpC = CmpInstr->getOpcode();
1313 unsigned CRReg = CmpInstr->getOperand(0).getReg();
Hal Finkel08e53ee2013-05-08 12:16:14 +00001314
1315 // FP record forms set CR1 based on the execption status bits, not a
1316 // comparison with zero.
1317 if (OpC == PPC::FCMPUS || OpC == PPC::FCMPUD)
1318 return false;
Hal Finkel82656cb2013-04-18 22:15:08 +00001319
1320 // The record forms set the condition register based on a signed comparison
1321 // with zero (so says the ISA manual). This is not as straightforward as it
1322 // seems, however, because this is always a 64-bit comparison on PPC64, even
1323 // for instructions that are 32-bit in nature (like slw for example).
1324 // So, on PPC32, for unsigned comparisons, we can use the record forms only
1325 // for equality checks (as those don't depend on the sign). On PPC64,
1326 // we are restricted to equality for unsigned 64-bit comparisons and for
1327 // signed 32-bit comparisons the applicability is more restricted.
Eric Christopher1dcea732014-06-12 21:48:52 +00001328 bool isPPC64 = Subtarget.isPPC64();
Hal Finkel82656cb2013-04-18 22:15:08 +00001329 bool is32BitSignedCompare = OpC == PPC::CMPWI || OpC == PPC::CMPW;
1330 bool is32BitUnsignedCompare = OpC == PPC::CMPLWI || OpC == PPC::CMPLW;
1331 bool is64BitUnsignedCompare = OpC == PPC::CMPLDI || OpC == PPC::CMPLD;
1332
1333 // Get the unique definition of SrcReg.
1334 MachineInstr *MI = MRI->getUniqueVRegDef(SrcReg);
1335 if (!MI) return false;
1336 int MIOpC = MI->getOpcode();
1337
1338 bool equalityOnly = false;
1339 bool noSub = false;
1340 if (isPPC64) {
1341 if (is32BitSignedCompare) {
1342 // We can perform this optimization only if MI is sign-extending.
1343 if (MIOpC == PPC::SRAW || MIOpC == PPC::SRAWo ||
1344 MIOpC == PPC::SRAWI || MIOpC == PPC::SRAWIo ||
1345 MIOpC == PPC::EXTSB || MIOpC == PPC::EXTSBo ||
1346 MIOpC == PPC::EXTSH || MIOpC == PPC::EXTSHo ||
1347 MIOpC == PPC::EXTSW || MIOpC == PPC::EXTSWo) {
1348 noSub = true;
1349 } else
1350 return false;
1351 } else if (is32BitUnsignedCompare) {
1352 // We can perform this optimization, equality only, if MI is
1353 // zero-extending.
1354 if (MIOpC == PPC::CNTLZW || MIOpC == PPC::CNTLZWo ||
1355 MIOpC == PPC::SLW || MIOpC == PPC::SLWo ||
1356 MIOpC == PPC::SRW || MIOpC == PPC::SRWo) {
1357 noSub = true;
1358 equalityOnly = true;
1359 } else
1360 return false;
Hal Finkel08e53ee2013-05-08 12:16:14 +00001361 } else
Hal Finkel82656cb2013-04-18 22:15:08 +00001362 equalityOnly = is64BitUnsignedCompare;
Hal Finkel08e53ee2013-05-08 12:16:14 +00001363 } else
Hal Finkel82656cb2013-04-18 22:15:08 +00001364 equalityOnly = is32BitUnsignedCompare;
1365
1366 if (equalityOnly) {
1367 // We need to check the uses of the condition register in order to reject
1368 // non-equality comparisons.
Owen Anderson16c6bf42014-03-13 23:12:04 +00001369 for (MachineRegisterInfo::use_instr_iterator I =MRI->use_instr_begin(CRReg),
1370 IE = MRI->use_instr_end(); I != IE; ++I) {
Hal Finkel82656cb2013-04-18 22:15:08 +00001371 MachineInstr *UseMI = &*I;
1372 if (UseMI->getOpcode() == PPC::BCC) {
1373 unsigned Pred = UseMI->getOperand(0).getImm();
Hal Finkelc3632452013-05-07 17:49:55 +00001374 if (Pred != PPC::PRED_EQ && Pred != PPC::PRED_NE)
1375 return false;
Hal Finkel82656cb2013-04-18 22:15:08 +00001376 } else if (UseMI->getOpcode() == PPC::ISEL ||
1377 UseMI->getOpcode() == PPC::ISEL8) {
1378 unsigned SubIdx = UseMI->getOperand(3).getSubReg();
Hal Finkelc3632452013-05-07 17:49:55 +00001379 if (SubIdx != PPC::sub_eq)
1380 return false;
Hal Finkel82656cb2013-04-18 22:15:08 +00001381 } else
1382 return false;
1383 }
1384 }
1385
Hal Finkelc3632452013-05-07 17:49:55 +00001386 MachineBasicBlock::iterator I = CmpInstr;
Hal Finkel82656cb2013-04-18 22:15:08 +00001387
1388 // Scan forward to find the first use of the compare.
1389 for (MachineBasicBlock::iterator EL = CmpInstr->getParent()->end();
1390 I != EL; ++I) {
1391 bool FoundUse = false;
Owen Anderson16c6bf42014-03-13 23:12:04 +00001392 for (MachineRegisterInfo::use_instr_iterator J =MRI->use_instr_begin(CRReg),
1393 JE = MRI->use_instr_end(); J != JE; ++J)
Hal Finkel82656cb2013-04-18 22:15:08 +00001394 if (&*J == &*I) {
1395 FoundUse = true;
1396 break;
1397 }
1398
1399 if (FoundUse)
1400 break;
1401 }
1402
Hal Finkel82656cb2013-04-18 22:15:08 +00001403 // There are two possible candidates which can be changed to set CR[01].
1404 // One is MI, the other is a SUB instruction.
1405 // For CMPrr(r1,r2), we are looking for SUB(r1,r2) or SUB(r2,r1).
Craig Topper062a2ba2014-04-25 05:30:21 +00001406 MachineInstr *Sub = nullptr;
Hal Finkel82656cb2013-04-18 22:15:08 +00001407 if (SrcReg2 != 0)
1408 // MI is not a candidate for CMPrr.
Craig Topper062a2ba2014-04-25 05:30:21 +00001409 MI = nullptr;
Hal Finkel82656cb2013-04-18 22:15:08 +00001410 // FIXME: Conservatively refuse to convert an instruction which isn't in the
1411 // same BB as the comparison. This is to allow the check below to avoid calls
1412 // (and other explicit clobbers); instead we should really check for these
1413 // more explicitly (in at least a few predecessors).
1414 else if (MI->getParent() != CmpInstr->getParent() || Value != 0) {
1415 // PPC does not have a record-form SUBri.
1416 return false;
1417 }
1418
1419 // Search for Sub.
1420 const TargetRegisterInfo *TRI = &getRegisterInfo();
1421 --I;
Hal Finkelc3632452013-05-07 17:49:55 +00001422
1423 // Get ready to iterate backward from CmpInstr.
1424 MachineBasicBlock::iterator E = MI,
1425 B = CmpInstr->getParent()->begin();
1426
Hal Finkel82656cb2013-04-18 22:15:08 +00001427 for (; I != E && !noSub; --I) {
1428 const MachineInstr &Instr = *I;
1429 unsigned IOpC = Instr.getOpcode();
1430
1431 if (&*I != CmpInstr && (
Hal Finkel08e53ee2013-05-08 12:16:14 +00001432 Instr.modifiesRegister(PPC::CR0, TRI) ||
1433 Instr.readsRegister(PPC::CR0, TRI)))
Hal Finkel82656cb2013-04-18 22:15:08 +00001434 // This instruction modifies or uses the record condition register after
1435 // the one we want to change. While we could do this transformation, it
1436 // would likely not be profitable. This transformation removes one
1437 // instruction, and so even forcing RA to generate one move probably
1438 // makes it unprofitable.
1439 return false;
1440
1441 // Check whether CmpInstr can be made redundant by the current instruction.
1442 if ((OpC == PPC::CMPW || OpC == PPC::CMPLW ||
1443 OpC == PPC::CMPD || OpC == PPC::CMPLD) &&
1444 (IOpC == PPC::SUBF || IOpC == PPC::SUBF8) &&
1445 ((Instr.getOperand(1).getReg() == SrcReg &&
1446 Instr.getOperand(2).getReg() == SrcReg2) ||
1447 (Instr.getOperand(1).getReg() == SrcReg2 &&
1448 Instr.getOperand(2).getReg() == SrcReg))) {
1449 Sub = &*I;
1450 break;
1451 }
1452
Hal Finkel82656cb2013-04-18 22:15:08 +00001453 if (I == B)
1454 // The 'and' is below the comparison instruction.
1455 return false;
1456 }
1457
1458 // Return false if no candidates exist.
1459 if (!MI && !Sub)
1460 return false;
1461
1462 // The single candidate is called MI.
1463 if (!MI) MI = Sub;
1464
1465 int NewOpC = -1;
1466 MIOpC = MI->getOpcode();
1467 if (MIOpC == PPC::ANDIo || MIOpC == PPC::ANDIo8)
1468 NewOpC = MIOpC;
1469 else {
1470 NewOpC = PPC::getRecordFormOpcode(MIOpC);
1471 if (NewOpC == -1 && PPC::getNonRecordFormOpcode(MIOpC) != -1)
1472 NewOpC = MIOpC;
1473 }
1474
1475 // FIXME: On the non-embedded POWER architectures, only some of the record
1476 // forms are fast, and we should use only the fast ones.
1477
1478 // The defining instruction has a record form (or is already a record
1479 // form). It is possible, however, that we'll need to reverse the condition
1480 // code of the users.
1481 if (NewOpC == -1)
1482 return false;
1483
Hal Finkele6322392013-04-19 22:08:38 +00001484 SmallVector<std::pair<MachineOperand*, PPC::Predicate>, 4> PredsToUpdate;
1485 SmallVector<std::pair<MachineOperand*, unsigned>, 4> SubRegsToUpdate;
Hal Finkel82656cb2013-04-18 22:15:08 +00001486
1487 // If we have SUB(r1, r2) and CMP(r2, r1), the condition code based on CMP
1488 // needs to be updated to be based on SUB. Push the condition code
1489 // operands to OperandsToUpdate. If it is safe to remove CmpInstr, the
1490 // condition code of these operands will be modified.
1491 bool ShouldSwap = false;
1492 if (Sub) {
1493 ShouldSwap = SrcReg2 != 0 && Sub->getOperand(1).getReg() == SrcReg2 &&
1494 Sub->getOperand(2).getReg() == SrcReg;
1495
1496 // The operands to subf are the opposite of sub, so only in the fixed-point
1497 // case, invert the order.
Hal Finkel08e53ee2013-05-08 12:16:14 +00001498 ShouldSwap = !ShouldSwap;
Hal Finkel82656cb2013-04-18 22:15:08 +00001499 }
1500
1501 if (ShouldSwap)
Owen Anderson16c6bf42014-03-13 23:12:04 +00001502 for (MachineRegisterInfo::use_instr_iterator
1503 I = MRI->use_instr_begin(CRReg), IE = MRI->use_instr_end();
1504 I != IE; ++I) {
Hal Finkel82656cb2013-04-18 22:15:08 +00001505 MachineInstr *UseMI = &*I;
1506 if (UseMI->getOpcode() == PPC::BCC) {
1507 PPC::Predicate Pred = (PPC::Predicate) UseMI->getOperand(0).getImm();
Hal Finkele6322392013-04-19 22:08:38 +00001508 assert((!equalityOnly ||
1509 Pred == PPC::PRED_EQ || Pred == PPC::PRED_NE) &&
1510 "Invalid predicate for equality-only optimization");
Owen Anderson16c6bf42014-03-13 23:12:04 +00001511 PredsToUpdate.push_back(std::make_pair(&(UseMI->getOperand(0)),
Hal Finkel0f64e212013-04-20 05:16:26 +00001512 PPC::getSwappedPredicate(Pred)));
Hal Finkel82656cb2013-04-18 22:15:08 +00001513 } else if (UseMI->getOpcode() == PPC::ISEL ||
1514 UseMI->getOpcode() == PPC::ISEL8) {
Hal Finkele6322392013-04-19 22:08:38 +00001515 unsigned NewSubReg = UseMI->getOperand(3).getSubReg();
1516 assert((!equalityOnly || NewSubReg == PPC::sub_eq) &&
1517 "Invalid CR bit for equality-only optimization");
1518
1519 if (NewSubReg == PPC::sub_lt)
1520 NewSubReg = PPC::sub_gt;
1521 else if (NewSubReg == PPC::sub_gt)
1522 NewSubReg = PPC::sub_lt;
1523
Owen Anderson16c6bf42014-03-13 23:12:04 +00001524 SubRegsToUpdate.push_back(std::make_pair(&(UseMI->getOperand(3)),
Hal Finkele6322392013-04-19 22:08:38 +00001525 NewSubReg));
Hal Finkel82656cb2013-04-18 22:15:08 +00001526 } else // We need to abort on a user we don't understand.
1527 return false;
1528 }
1529
1530 // Create a new virtual register to hold the value of the CR set by the
1531 // record-form instruction. If the instruction was not previously in
1532 // record form, then set the kill flag on the CR.
1533 CmpInstr->eraseFromParent();
1534
1535 MachineBasicBlock::iterator MII = MI;
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001536 BuildMI(*MI->getParent(), std::next(MII), MI->getDebugLoc(),
Hal Finkel82656cb2013-04-18 22:15:08 +00001537 get(TargetOpcode::COPY), CRReg)
Hal Finkel08e53ee2013-05-08 12:16:14 +00001538 .addReg(PPC::CR0, MIOpC != NewOpC ? RegState::Kill : 0);
Hal Finkel82656cb2013-04-18 22:15:08 +00001539
1540 if (MIOpC != NewOpC) {
1541 // We need to be careful here: we're replacing one instruction with
1542 // another, and we need to make sure that we get all of the right
1543 // implicit uses and defs. On the other hand, the caller may be holding
1544 // an iterator to this instruction, and so we can't delete it (this is
1545 // specifically the case if this is the instruction directly after the
1546 // compare).
1547
1548 const MCInstrDesc &NewDesc = get(NewOpC);
1549 MI->setDesc(NewDesc);
1550
1551 if (NewDesc.ImplicitDefs)
1552 for (const uint16_t *ImpDefs = NewDesc.getImplicitDefs();
1553 *ImpDefs; ++ImpDefs)
1554 if (!MI->definesRegister(*ImpDefs))
1555 MI->addOperand(*MI->getParent()->getParent(),
1556 MachineOperand::CreateReg(*ImpDefs, true, true));
1557 if (NewDesc.ImplicitUses)
1558 for (const uint16_t *ImpUses = NewDesc.getImplicitUses();
1559 *ImpUses; ++ImpUses)
1560 if (!MI->readsRegister(*ImpUses))
1561 MI->addOperand(*MI->getParent()->getParent(),
1562 MachineOperand::CreateReg(*ImpUses, false, true));
1563 }
1564
1565 // Modify the condition code of operands in OperandsToUpdate.
1566 // Since we have SUB(r1, r2) and CMP(r2, r1), the condition code needs to
1567 // be changed from r2 > r1 to r1 < r2, from r2 < r1 to r1 > r2, etc.
Hal Finkele6322392013-04-19 22:08:38 +00001568 for (unsigned i = 0, e = PredsToUpdate.size(); i < e; i++)
1569 PredsToUpdate[i].first->setImm(PredsToUpdate[i].second);
Hal Finkel82656cb2013-04-18 22:15:08 +00001570
Hal Finkele6322392013-04-19 22:08:38 +00001571 for (unsigned i = 0, e = SubRegsToUpdate.size(); i < e; i++)
1572 SubRegsToUpdate[i].first->setSubReg(SubRegsToUpdate[i].second);
Hal Finkel82656cb2013-04-18 22:15:08 +00001573
1574 return true;
1575}
1576
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +00001577/// GetInstSize - Return the number of bytes of code the specified
1578/// instruction may be. This returns the maximum number of bytes.
1579///
1580unsigned PPCInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
Hal Finkela7bbaf62014-02-02 06:12:27 +00001581 unsigned Opcode = MI->getOpcode();
1582
1583 if (Opcode == PPC::INLINEASM) {
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +00001584 const MachineFunction *MF = MI->getParent()->getParent();
1585 const char *AsmStr = MI->getOperand(0).getSymbolName();
Chris Lattner7b26fce2009-08-22 20:48:53 +00001586 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
Hal Finkela7bbaf62014-02-02 06:12:27 +00001587 } else {
1588 const MCInstrDesc &Desc = get(Opcode);
1589 return Desc.getSize();
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +00001590 }
1591}
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001592
Hal Finkel174e5902014-03-25 23:29:21 +00001593#undef DEBUG_TYPE
1594#define DEBUG_TYPE "ppc-vsx-fma-mutate"
1595
1596namespace {
1597 // PPCVSXFMAMutate pass - For copies between VSX registers and non-VSX registers
1598 // (Altivec and scalar floating-point registers), we need to transform the
1599 // copies into subregister copies with other restrictions.
1600 struct PPCVSXFMAMutate : public MachineFunctionPass {
1601 static char ID;
1602 PPCVSXFMAMutate() : MachineFunctionPass(ID) {
1603 initializePPCVSXFMAMutatePass(*PassRegistry::getPassRegistry());
1604 }
1605
1606 LiveIntervals *LIS;
1607
1608 const PPCTargetMachine *TM;
1609 const PPCInstrInfo *TII;
1610
1611protected:
1612 bool processBlock(MachineBasicBlock &MBB) {
1613 bool Changed = false;
1614
1615 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
1616 for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end();
1617 I != IE; ++I) {
1618 MachineInstr *MI = I;
1619
1620 // The default (A-type) VSX FMA form kills the addend (it is taken from
1621 // the target register, which is then updated to reflect the result of
1622 // the FMA). If the instruction, however, kills one of the registers
1623 // used for the product, then we can use the M-form instruction (which
1624 // will take that value from the to-be-defined register).
1625
1626 int AltOpc = PPC::getAltVSXFMAOpcode(MI->getOpcode());
1627 if (AltOpc == -1)
1628 continue;
1629
1630 // This pass is run after register coalescing, and so we're looking for
1631 // a situation like this:
1632 // ...
1633 // %vreg5<def> = COPY %vreg9; VSLRC:%vreg5,%vreg9
1634 // %vreg5<def,tied1> = XSMADDADP %vreg5<tied0>, %vreg17, %vreg16,
1635 // %RM<imp-use>; VSLRC:%vreg5,%vreg17,%vreg16
1636 // ...
1637 // %vreg9<def,tied1> = XSMADDADP %vreg9<tied0>, %vreg17, %vreg19,
1638 // %RM<imp-use>; VSLRC:%vreg9,%vreg17,%vreg19
1639 // ...
1640 // Where we can eliminate the copy by changing from the A-type to the
1641 // M-type instruction. Specifically, for this example, this means:
1642 // %vreg5<def,tied1> = XSMADDADP %vreg5<tied0>, %vreg17, %vreg16,
1643 // %RM<imp-use>; VSLRC:%vreg5,%vreg17,%vreg16
1644 // is replaced by:
1645 // %vreg16<def,tied1> = XSMADDMDP %vreg16<tied0>, %vreg18, %vreg9,
1646 // %RM<imp-use>; VSLRC:%vreg16,%vreg18,%vreg9
1647 // and we remove: %vreg5<def> = COPY %vreg9; VSLRC:%vreg5,%vreg9
1648
1649 SlotIndex FMAIdx = LIS->getInstructionIndex(MI);
1650
1651 VNInfo *AddendValNo =
1652 LIS->getInterval(MI->getOperand(1).getReg()).Query(FMAIdx).valueIn();
1653 MachineInstr *AddendMI = LIS->getInstructionFromIndex(AddendValNo->def);
1654
1655 // The addend and this instruction must be in the same block.
1656
Hal Finkel19be5062014-03-29 05:29:01 +00001657 if (!AddendMI || AddendMI->getParent() != MI->getParent())
Hal Finkel174e5902014-03-25 23:29:21 +00001658 continue;
1659
1660 // The addend must be a full copy within the same register class.
1661
1662 if (!AddendMI->isFullCopy())
1663 continue;
1664
Hal Finkel19be5062014-03-29 05:29:01 +00001665 unsigned AddendSrcReg = AddendMI->getOperand(1).getReg();
1666 if (TargetRegisterInfo::isVirtualRegister(AddendSrcReg)) {
1667 if (MRI.getRegClass(AddendMI->getOperand(0).getReg()) !=
1668 MRI.getRegClass(AddendSrcReg))
1669 continue;
1670 } else {
1671 // If AddendSrcReg is a physical register, make sure the destination
1672 // register class contains it.
1673 if (!MRI.getRegClass(AddendMI->getOperand(0).getReg())
1674 ->contains(AddendSrcReg))
1675 continue;
1676 }
Hal Finkel174e5902014-03-25 23:29:21 +00001677
1678 // In theory, there could be other uses of the addend copy before this
1679 // fma. We could deal with this, but that would require additional
1680 // logic below and I suspect it will not occur in any relevant
1681 // situations.
1682 bool OtherUsers = false;
1683 for (auto J = std::prev(I), JE = MachineBasicBlock::iterator(AddendMI);
1684 J != JE; --J)
1685 if (J->readsVirtualRegister(AddendMI->getOperand(0).getReg())) {
1686 OtherUsers = true;
1687 break;
1688 }
1689
1690 if (OtherUsers)
1691 continue;
1692
1693 // Find one of the product operands that is killed by this instruction.
1694
1695 unsigned KilledProdOp = 0, OtherProdOp = 0;
1696 if (LIS->getInterval(MI->getOperand(2).getReg())
1697 .Query(FMAIdx).isKill()) {
1698 KilledProdOp = 2;
1699 OtherProdOp = 3;
1700 } else if (LIS->getInterval(MI->getOperand(3).getReg())
1701 .Query(FMAIdx).isKill()) {
1702 KilledProdOp = 3;
1703 OtherProdOp = 2;
1704 }
1705
Hal Finkel19be5062014-03-29 05:29:01 +00001706 // If there are no killed product operands, then this transformation is
1707 // likely not profitable.
Hal Finkel174e5902014-03-25 23:29:21 +00001708 if (!KilledProdOp)
1709 continue;
1710
1711 // In order to replace the addend here with the source of the copy,
1712 // it must still be live here.
1713 if (!LIS->getInterval(AddendMI->getOperand(1).getReg()).liveAt(FMAIdx))
1714 continue;
1715
1716 // Transform: (O2 * O3) + O1 -> (O2 * O1) + O3.
1717
1718 unsigned AddReg = AddendMI->getOperand(1).getReg();
1719 unsigned KilledProdReg = MI->getOperand(KilledProdOp).getReg();
1720 unsigned OtherProdReg = MI->getOperand(OtherProdOp).getReg();
1721
1722 unsigned AddSubReg = AddendMI->getOperand(1).getSubReg();
1723 unsigned KilledProdSubReg = MI->getOperand(KilledProdOp).getSubReg();
1724 unsigned OtherProdSubReg = MI->getOperand(OtherProdOp).getSubReg();
1725
1726 bool AddRegKill = AddendMI->getOperand(1).isKill();
1727 bool KilledProdRegKill = MI->getOperand(KilledProdOp).isKill();
1728 bool OtherProdRegKill = MI->getOperand(OtherProdOp).isKill();
1729
1730 bool AddRegUndef = AddendMI->getOperand(1).isUndef();
1731 bool KilledProdRegUndef = MI->getOperand(KilledProdOp).isUndef();
1732 bool OtherProdRegUndef = MI->getOperand(OtherProdOp).isUndef();
1733
1734 unsigned OldFMAReg = MI->getOperand(0).getReg();
1735
1736 assert(OldFMAReg == AddendMI->getOperand(0).getReg() &&
1737 "Addend copy not tied to old FMA output!");
1738
1739 DEBUG(dbgs() << "VSX FMA Mutation:\n " << *MI;);
1740
1741 MI->getOperand(0).setReg(KilledProdReg);
1742 MI->getOperand(1).setReg(KilledProdReg);
1743 MI->getOperand(3).setReg(AddReg);
1744 MI->getOperand(2).setReg(OtherProdReg);
1745
1746 MI->getOperand(0).setSubReg(KilledProdSubReg);
1747 MI->getOperand(1).setSubReg(KilledProdSubReg);
1748 MI->getOperand(3).setSubReg(AddSubReg);
1749 MI->getOperand(2).setSubReg(OtherProdSubReg);
1750
1751 MI->getOperand(1).setIsKill(KilledProdRegKill);
1752 MI->getOperand(3).setIsKill(AddRegKill);
1753 MI->getOperand(2).setIsKill(OtherProdRegKill);
1754
1755 MI->getOperand(1).setIsUndef(KilledProdRegUndef);
1756 MI->getOperand(3).setIsUndef(AddRegUndef);
1757 MI->getOperand(2).setIsUndef(OtherProdRegUndef);
1758
1759 MI->setDesc(TII->get(AltOpc));
1760
1761 DEBUG(dbgs() << " -> " << *MI);
1762
1763 // The killed product operand was killed here, so we can reuse it now
1764 // for the result of the fma.
1765
1766 LiveInterval &FMAInt = LIS->getInterval(OldFMAReg);
1767 VNInfo *FMAValNo = FMAInt.getVNInfoAt(FMAIdx.getRegSlot());
1768 for (auto UI = MRI.reg_nodbg_begin(OldFMAReg), UE = MRI.reg_nodbg_end();
1769 UI != UE;) {
1770 MachineOperand &UseMO = *UI;
1771 MachineInstr *UseMI = UseMO.getParent();
1772 ++UI;
1773
1774 // Don't replace the result register of the copy we're about to erase.
1775 if (UseMI == AddendMI)
1776 continue;
1777
1778 UseMO.setReg(KilledProdReg);
1779 UseMO.setSubReg(KilledProdSubReg);
1780 }
1781
1782 // Extend the live intervals of the killed product operand to hold the
1783 // fma result.
1784
1785 LiveInterval &NewFMAInt = LIS->getInterval(KilledProdReg);
1786 for (LiveInterval::iterator AI = FMAInt.begin(), AE = FMAInt.end();
1787 AI != AE; ++AI) {
1788 // Don't add the segment that corresponds to the original copy.
1789 if (AI->valno == AddendValNo)
1790 continue;
1791
1792 VNInfo *NewFMAValNo =
1793 NewFMAInt.getNextValue(AI->start,
1794 LIS->getVNInfoAllocator());
1795
1796 NewFMAInt.addSegment(LiveInterval::Segment(AI->start, AI->end,
1797 NewFMAValNo));
1798 }
1799 DEBUG(dbgs() << " extended: " << NewFMAInt << '\n');
1800
1801 FMAInt.removeValNo(FMAValNo);
1802 DEBUG(dbgs() << " trimmed: " << FMAInt << '\n');
1803
1804 // Remove the (now unused) copy.
1805
1806 DEBUG(dbgs() << " removing: " << *AddendMI << '\n');
1807 LIS->RemoveMachineInstrFromMaps(AddendMI);
1808 AddendMI->eraseFromParent();
1809
1810 Changed = true;
1811 }
1812
1813 return Changed;
1814 }
1815
1816public:
Craig Topper0d3fa922014-04-29 07:57:37 +00001817 bool runOnMachineFunction(MachineFunction &MF) override {
Eric Christopherd71e4442014-05-22 01:21:35 +00001818 TM = static_cast<const PPCTargetMachine *>(&MF.getTarget());
1819 // If we don't have VSX then go ahead and return without doing
1820 // anything.
1821 if (!TM->getSubtargetImpl()->hasVSX())
1822 return false;
1823
Hal Finkel174e5902014-03-25 23:29:21 +00001824 LIS = &getAnalysis<LiveIntervals>();
1825
Hal Finkel174e5902014-03-25 23:29:21 +00001826 TII = TM->getInstrInfo();
1827
1828 bool Changed = false;
1829
1830 if (DisableVSXFMAMutate)
1831 return Changed;
1832
1833 for (MachineFunction::iterator I = MF.begin(); I != MF.end();) {
1834 MachineBasicBlock &B = *I++;
1835 if (processBlock(B))
1836 Changed = true;
1837 }
1838
1839 return Changed;
1840 }
1841
Craig Topper0d3fa922014-04-29 07:57:37 +00001842 void getAnalysisUsage(AnalysisUsage &AU) const override {
Hal Finkel174e5902014-03-25 23:29:21 +00001843 AU.addRequired<LiveIntervals>();
1844 AU.addPreserved<LiveIntervals>();
1845 AU.addRequired<SlotIndexes>();
1846 AU.addPreserved<SlotIndexes>();
1847 MachineFunctionPass::getAnalysisUsage(AU);
1848 }
1849 };
1850}
1851
1852INITIALIZE_PASS_BEGIN(PPCVSXFMAMutate, DEBUG_TYPE,
1853 "PowerPC VSX FMA Mutation", false, false)
1854INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
1855INITIALIZE_PASS_DEPENDENCY(SlotIndexes)
1856INITIALIZE_PASS_END(PPCVSXFMAMutate, DEBUG_TYPE,
1857 "PowerPC VSX FMA Mutation", false, false)
1858
1859char &llvm::PPCVSXFMAMutateID = PPCVSXFMAMutate::ID;
1860
1861char PPCVSXFMAMutate::ID = 0;
1862FunctionPass*
1863llvm::createPPCVSXFMAMutatePass() { return new PPCVSXFMAMutate(); }
Hal Finkel27774d92014-03-13 07:58:58 +00001864
1865#undef DEBUG_TYPE
1866#define DEBUG_TYPE "ppc-vsx-copy"
1867
1868namespace llvm {
1869 void initializePPCVSXCopyPass(PassRegistry&);
1870}
1871
1872namespace {
1873 // PPCVSXCopy pass - For copies between VSX registers and non-VSX registers
1874 // (Altivec and scalar floating-point registers), we need to transform the
1875 // copies into subregister copies with other restrictions.
1876 struct PPCVSXCopy : public MachineFunctionPass {
1877 static char ID;
1878 PPCVSXCopy() : MachineFunctionPass(ID) {
1879 initializePPCVSXCopyPass(*PassRegistry::getPassRegistry());
1880 }
1881
1882 const PPCTargetMachine *TM;
1883 const PPCInstrInfo *TII;
1884
1885 bool IsRegInClass(unsigned Reg, const TargetRegisterClass *RC,
1886 MachineRegisterInfo &MRI) {
1887 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1888 return RC->hasSubClassEq(MRI.getRegClass(Reg));
1889 } else if (RC->contains(Reg)) {
1890 return true;
1891 }
1892
1893 return false;
1894 }
1895
1896 bool IsVSReg(unsigned Reg, MachineRegisterInfo &MRI) {
1897 return IsRegInClass(Reg, &PPC::VSRCRegClass, MRI);
1898 }
1899
1900 bool IsVRReg(unsigned Reg, MachineRegisterInfo &MRI) {
1901 return IsRegInClass(Reg, &PPC::VRRCRegClass, MRI);
1902 }
1903
1904 bool IsF8Reg(unsigned Reg, MachineRegisterInfo &MRI) {
1905 return IsRegInClass(Reg, &PPC::F8RCRegClass, MRI);
1906 }
1907
1908protected:
1909 bool processBlock(MachineBasicBlock &MBB) {
1910 bool Changed = false;
1911
1912 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
1913 for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end();
1914 I != IE; ++I) {
1915 MachineInstr *MI = I;
1916 if (!MI->isFullCopy())
1917 continue;
1918
1919 MachineOperand &DstMO = MI->getOperand(0);
1920 MachineOperand &SrcMO = MI->getOperand(1);
1921
1922 if ( IsVSReg(DstMO.getReg(), MRI) &&
1923 !IsVSReg(SrcMO.getReg(), MRI)) {
1924 // This is a copy *to* a VSX register from a non-VSX register.
1925 Changed = true;
1926
1927 const TargetRegisterClass *SrcRC =
1928 IsVRReg(SrcMO.getReg(), MRI) ? &PPC::VSHRCRegClass :
1929 &PPC::VSLRCRegClass;
1930 assert((IsF8Reg(SrcMO.getReg(), MRI) ||
1931 IsVRReg(SrcMO.getReg(), MRI)) &&
1932 "Unknown source for a VSX copy");
1933
1934 unsigned NewVReg = MRI.createVirtualRegister(SrcRC);
1935 BuildMI(MBB, MI, MI->getDebugLoc(),
1936 TII->get(TargetOpcode::SUBREG_TO_REG), NewVReg)
1937 .addImm(1) // add 1, not 0, because there is no implicit clearing
1938 // of the high bits.
1939 .addOperand(SrcMO)
1940 .addImm(IsVRReg(SrcMO.getReg(), MRI) ? PPC::sub_128 :
1941 PPC::sub_64);
1942
1943 // The source of the original copy is now the new virtual register.
1944 SrcMO.setReg(NewVReg);
1945 } else if (!IsVSReg(DstMO.getReg(), MRI) &&
1946 IsVSReg(SrcMO.getReg(), MRI)) {
1947 // This is a copy *from* a VSX register to a non-VSX register.
1948 Changed = true;
1949
1950 const TargetRegisterClass *DstRC =
1951 IsVRReg(DstMO.getReg(), MRI) ? &PPC::VSHRCRegClass :
1952 &PPC::VSLRCRegClass;
1953 assert((IsF8Reg(DstMO.getReg(), MRI) ||
1954 IsVRReg(DstMO.getReg(), MRI)) &&
1955 "Unknown destination for a VSX copy");
1956
1957 // Copy the VSX value into a new VSX register of the correct subclass.
1958 unsigned NewVReg = MRI.createVirtualRegister(DstRC);
1959 BuildMI(MBB, MI, MI->getDebugLoc(),
1960 TII->get(TargetOpcode::COPY), NewVReg)
1961 .addOperand(SrcMO);
1962
1963 // Transform the original copy into a subregister extraction copy.
1964 SrcMO.setReg(NewVReg);
1965 SrcMO.setSubReg(IsVRReg(DstMO.getReg(), MRI) ? PPC::sub_128 :
1966 PPC::sub_64);
1967 }
1968 }
1969
1970 return Changed;
1971 }
1972
1973public:
Craig Topper0d3fa922014-04-29 07:57:37 +00001974 bool runOnMachineFunction(MachineFunction &MF) override {
Hal Finkel27774d92014-03-13 07:58:58 +00001975 TM = static_cast<const PPCTargetMachine *>(&MF.getTarget());
Eric Christopherd71e4442014-05-22 01:21:35 +00001976 // If we don't have VSX on the subtarget, don't do anything.
1977 if (!TM->getSubtargetImpl()->hasVSX())
1978 return false;
Hal Finkel27774d92014-03-13 07:58:58 +00001979 TII = TM->getInstrInfo();
1980
1981 bool Changed = false;
1982
1983 for (MachineFunction::iterator I = MF.begin(); I != MF.end();) {
1984 MachineBasicBlock &B = *I++;
1985 if (processBlock(B))
1986 Changed = true;
1987 }
1988
1989 return Changed;
1990 }
1991
Craig Topper0d3fa922014-04-29 07:57:37 +00001992 void getAnalysisUsage(AnalysisUsage &AU) const override {
Hal Finkel27774d92014-03-13 07:58:58 +00001993 MachineFunctionPass::getAnalysisUsage(AU);
1994 }
1995 };
1996}
1997
1998INITIALIZE_PASS(PPCVSXCopy, DEBUG_TYPE,
1999 "PowerPC VSX Copy Legalization", false, false)
2000
2001char PPCVSXCopy::ID = 0;
2002FunctionPass*
2003llvm::createPPCVSXCopyPass() { return new PPCVSXCopy(); }
2004
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002005#undef DEBUG_TYPE
Hal Finkelc6fc9b82014-03-27 23:12:31 +00002006#define DEBUG_TYPE "ppc-vsx-copy-cleanup"
2007
2008namespace llvm {
2009 void initializePPCVSXCopyCleanupPass(PassRegistry&);
2010}
2011
2012namespace {
2013 // PPCVSXCopyCleanup pass - We sometimes end up generating self copies of VSX
2014 // registers (mostly because the ABI code still places all values into the
2015 // "traditional" floating-point and vector registers). Remove them here.
2016 struct PPCVSXCopyCleanup : public MachineFunctionPass {
2017 static char ID;
2018 PPCVSXCopyCleanup() : MachineFunctionPass(ID) {
2019 initializePPCVSXCopyCleanupPass(*PassRegistry::getPassRegistry());
2020 }
2021
2022 const PPCTargetMachine *TM;
2023 const PPCInstrInfo *TII;
2024
2025protected:
2026 bool processBlock(MachineBasicBlock &MBB) {
2027 bool Changed = false;
2028
2029 SmallVector<MachineInstr *, 4> ToDelete;
2030 for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end();
2031 I != IE; ++I) {
2032 MachineInstr *MI = I;
2033 if (MI->getOpcode() == PPC::XXLOR &&
2034 MI->getOperand(0).getReg() == MI->getOperand(1).getReg() &&
2035 MI->getOperand(0).getReg() == MI->getOperand(2).getReg())
2036 ToDelete.push_back(MI);
2037 }
2038
2039 if (!ToDelete.empty())
2040 Changed = true;
2041
2042 for (unsigned i = 0, ie = ToDelete.size(); i != ie; ++i) {
2043 DEBUG(dbgs() << "Removing VSX self-copy: " << *ToDelete[i]);
2044 ToDelete[i]->eraseFromParent();
2045 }
2046
2047 return Changed;
2048 }
2049
2050public:
Craig Topper0d3fa922014-04-29 07:57:37 +00002051 bool runOnMachineFunction(MachineFunction &MF) override {
Hal Finkelc6fc9b82014-03-27 23:12:31 +00002052 TM = static_cast<const PPCTargetMachine *>(&MF.getTarget());
Eric Christopherd71e4442014-05-22 01:21:35 +00002053 // If we don't have VSX don't bother doing anything here.
2054 if (!TM->getSubtargetImpl()->hasVSX())
2055 return false;
Hal Finkelc6fc9b82014-03-27 23:12:31 +00002056 TII = TM->getInstrInfo();
2057
2058 bool Changed = false;
2059
2060 for (MachineFunction::iterator I = MF.begin(); I != MF.end();) {
2061 MachineBasicBlock &B = *I++;
2062 if (processBlock(B))
2063 Changed = true;
2064 }
2065
2066 return Changed;
2067 }
2068
Craig Topper0d3fa922014-04-29 07:57:37 +00002069 void getAnalysisUsage(AnalysisUsage &AU) const override {
Hal Finkelc6fc9b82014-03-27 23:12:31 +00002070 MachineFunctionPass::getAnalysisUsage(AU);
2071 }
2072 };
2073}
2074
2075INITIALIZE_PASS(PPCVSXCopyCleanup, DEBUG_TYPE,
2076 "PowerPC VSX Copy Cleanup", false, false)
2077
2078char PPCVSXCopyCleanup::ID = 0;
2079FunctionPass*
2080llvm::createPPCVSXCopyCleanupPass() { return new PPCVSXCopyCleanup(); }
2081
2082#undef DEBUG_TYPE
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002083#define DEBUG_TYPE "ppc-early-ret"
2084STATISTIC(NumBCLR, "Number of early conditional returns");
2085STATISTIC(NumBLR, "Number of early returns");
2086
2087namespace llvm {
2088 void initializePPCEarlyReturnPass(PassRegistry&);
2089}
2090
2091namespace {
2092 // PPCEarlyReturn pass - For simple functions without epilogue code, move
2093 // returns up, and create conditional returns, to avoid unnecessary
2094 // branch-to-blr sequences.
2095 struct PPCEarlyReturn : public MachineFunctionPass {
2096 static char ID;
2097 PPCEarlyReturn() : MachineFunctionPass(ID) {
2098 initializePPCEarlyReturnPass(*PassRegistry::getPassRegistry());
2099 }
2100
2101 const PPCTargetMachine *TM;
2102 const PPCInstrInfo *TII;
2103
2104protected:
Hal Finkel21aad9a2013-04-09 18:25:18 +00002105 bool processBlock(MachineBasicBlock &ReturnMBB) {
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002106 bool Changed = false;
2107
Hal Finkel21aad9a2013-04-09 18:25:18 +00002108 MachineBasicBlock::iterator I = ReturnMBB.begin();
2109 I = ReturnMBB.SkipPHIsAndLabels(I);
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002110
2111 // The block must be essentially empty except for the blr.
Hal Finkel21aad9a2013-04-09 18:25:18 +00002112 if (I == ReturnMBB.end() || I->getOpcode() != PPC::BLR ||
2113 I != ReturnMBB.getLastNonDebugInstr())
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002114 return Changed;
2115
2116 SmallVector<MachineBasicBlock*, 8> PredToRemove;
Hal Finkel21aad9a2013-04-09 18:25:18 +00002117 for (MachineBasicBlock::pred_iterator PI = ReturnMBB.pred_begin(),
2118 PIE = ReturnMBB.pred_end(); PI != PIE; ++PI) {
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002119 bool OtherReference = false, BlockChanged = false;
Hal Finkel21aad9a2013-04-09 18:25:18 +00002120 for (MachineBasicBlock::iterator J = (*PI)->getLastNonDebugInstr();;) {
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002121 if (J->getOpcode() == PPC::B) {
Hal Finkel21aad9a2013-04-09 18:25:18 +00002122 if (J->getOperand(0).getMBB() == &ReturnMBB) {
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002123 // This is an unconditional branch to the return. Replace the
Andrew Trick9defbd82013-12-17 04:50:40 +00002124 // branch with a blr.
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002125 BuildMI(**PI, J, J->getDebugLoc(), TII->get(PPC::BLR));
Hal Finkel21aad9a2013-04-09 18:25:18 +00002126 MachineBasicBlock::iterator K = J--;
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002127 K->eraseFromParent();
2128 BlockChanged = true;
2129 ++NumBLR;
2130 continue;
2131 }
2132 } else if (J->getOpcode() == PPC::BCC) {
Hal Finkel21aad9a2013-04-09 18:25:18 +00002133 if (J->getOperand(2).getMBB() == &ReturnMBB) {
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002134 // This is a conditional branch to the return. Replace the branch
2135 // with a bclr.
Hal Finkel940ab932014-02-28 00:27:01 +00002136 BuildMI(**PI, J, J->getDebugLoc(), TII->get(PPC::BCCLR))
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002137 .addImm(J->getOperand(0).getImm())
2138 .addReg(J->getOperand(1).getReg());
Hal Finkel21aad9a2013-04-09 18:25:18 +00002139 MachineBasicBlock::iterator K = J--;
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002140 K->eraseFromParent();
2141 BlockChanged = true;
2142 ++NumBCLR;
2143 continue;
2144 }
Hal Finkel940ab932014-02-28 00:27:01 +00002145 } else if (J->getOpcode() == PPC::BC || J->getOpcode() == PPC::BCn) {
2146 if (J->getOperand(1).getMBB() == &ReturnMBB) {
2147 // This is a conditional branch to the return. Replace the branch
2148 // with a bclr.
2149 BuildMI(**PI, J, J->getDebugLoc(),
2150 TII->get(J->getOpcode() == PPC::BC ?
2151 PPC::BCLR : PPC::BCLRn))
2152 .addReg(J->getOperand(0).getReg());
2153 MachineBasicBlock::iterator K = J--;
2154 K->eraseFromParent();
2155 BlockChanged = true;
2156 ++NumBCLR;
2157 continue;
2158 }
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002159 } else if (J->isBranch()) {
2160 if (J->isIndirectBranch()) {
Hal Finkel21aad9a2013-04-09 18:25:18 +00002161 if (ReturnMBB.hasAddressTaken())
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002162 OtherReference = true;
2163 } else
2164 for (unsigned i = 0; i < J->getNumOperands(); ++i)
2165 if (J->getOperand(i).isMBB() &&
Hal Finkel21aad9a2013-04-09 18:25:18 +00002166 J->getOperand(i).getMBB() == &ReturnMBB)
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002167 OtherReference = true;
Hal Finkel21aad9a2013-04-09 18:25:18 +00002168 } else if (!J->isTerminator() && !J->isDebugValue())
2169 break;
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002170
Hal Finkel21aad9a2013-04-09 18:25:18 +00002171 if (J == (*PI)->begin())
2172 break;
2173
2174 --J;
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002175 }
2176
Hal Finkel21aad9a2013-04-09 18:25:18 +00002177 if ((*PI)->canFallThrough() && (*PI)->isLayoutSuccessor(&ReturnMBB))
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002178 OtherReference = true;
2179
Andrew Trick9defbd82013-12-17 04:50:40 +00002180 // Predecessors are stored in a vector and can't be removed here.
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002181 if (!OtherReference && BlockChanged) {
2182 PredToRemove.push_back(*PI);
2183 }
2184
2185 if (BlockChanged)
2186 Changed = true;
2187 }
2188
2189 for (unsigned i = 0, ie = PredToRemove.size(); i != ie; ++i)
Hal Finkel21aad9a2013-04-09 18:25:18 +00002190 PredToRemove[i]->removeSuccessor(&ReturnMBB);
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002191
Hal Finkel21aad9a2013-04-09 18:25:18 +00002192 if (Changed && !ReturnMBB.hasAddressTaken()) {
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002193 // We now might be able to merge this blr-only block into its
2194 // by-layout predecessor.
Hal Finkel21aad9a2013-04-09 18:25:18 +00002195 if (ReturnMBB.pred_size() == 1 &&
2196 (*ReturnMBB.pred_begin())->isLayoutSuccessor(&ReturnMBB)) {
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002197 // Move the blr into the preceding block.
Hal Finkel21aad9a2013-04-09 18:25:18 +00002198 MachineBasicBlock &PrevMBB = **ReturnMBB.pred_begin();
2199 PrevMBB.splice(PrevMBB.end(), &ReturnMBB, I);
2200 PrevMBB.removeSuccessor(&ReturnMBB);
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002201 }
2202
Hal Finkel21aad9a2013-04-09 18:25:18 +00002203 if (ReturnMBB.pred_empty())
2204 ReturnMBB.eraseFromParent();
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002205 }
2206
2207 return Changed;
2208 }
2209
2210public:
Craig Topper0d3fa922014-04-29 07:57:37 +00002211 bool runOnMachineFunction(MachineFunction &MF) override {
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002212 TM = static_cast<const PPCTargetMachine *>(&MF.getTarget());
2213 TII = TM->getInstrInfo();
2214
2215 bool Changed = false;
2216
Hal Finkel21aad9a2013-04-09 18:25:18 +00002217 // If the function does not have at least two blocks, then there is
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002218 // nothing to do.
2219 if (MF.size() < 2)
2220 return Changed;
2221
2222 for (MachineFunction::iterator I = MF.begin(); I != MF.end();) {
Andrew Trick9defbd82013-12-17 04:50:40 +00002223 MachineBasicBlock &B = *I++;
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002224 if (processBlock(B))
2225 Changed = true;
2226 }
2227
2228 return Changed;
2229 }
2230
Craig Topper0d3fa922014-04-29 07:57:37 +00002231 void getAnalysisUsage(AnalysisUsage &AU) const override {
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002232 MachineFunctionPass::getAnalysisUsage(AU);
2233 }
2234 };
2235}
2236
2237INITIALIZE_PASS(PPCEarlyReturn, DEBUG_TYPE,
2238 "PowerPC Early-Return Creation", false, false)
2239
2240char PPCEarlyReturn::ID = 0;
2241FunctionPass*
2242llvm::createPPCEarlyReturnPass() { return new PPCEarlyReturn(); }