blob: 2492229dc319b291f82de2751f64a750a7689b4b [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"
Hal Finkel934361a2015-01-14 01:07:51 +000032#include "llvm/CodeGen/StackMaps.h"
Evan Chengc5e6d2f2011-07-11 03:57:24 +000033#include "llvm/MC/MCAsmInfo.h"
Bill Wendling1af20ad2008-03-04 23:13:51 +000034#include "llvm/Support/CommandLine.h"
Hal Finkel174e5902014-03-25 23:29:21 +000035#include "llvm/Support/Debug.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000036#include "llvm/Support/ErrorHandling.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000037#include "llvm/Support/TargetRegistry.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000038#include "llvm/Support/raw_ostream.h"
Misha Brukman116f9272004-08-17 04:55:41 +000039
Dan Gohman20857192010-04-15 17:20:57 +000040using namespace llvm;
Bill Wendling1af20ad2008-03-04 23:13:51 +000041
Chandler Carruthe96dd892014-04-21 22:55:11 +000042#define DEBUG_TYPE "ppc-instr-info"
43
Chandler Carruthd174b722014-04-22 02:03:14 +000044#define GET_INSTRMAP_INFO
45#define GET_INSTRINFO_CTOR_DTOR
46#include "PPCGenInstrInfo.inc"
47
Hal Finkel821e0012012-06-08 15:38:25 +000048static cl::
Hal Finkelc6b5deb2012-06-08 19:19:53 +000049opt<bool> DisableCTRLoopAnal("disable-ppc-ctrloop-analysis", cl::Hidden,
50 cl::desc("Disable analysis for CTR loops"));
Hal Finkel821e0012012-06-08 15:38:25 +000051
Hal Finkele6322392013-04-19 22:08:38 +000052static cl::opt<bool> DisableCmpOpt("disable-ppc-cmp-opt",
Hal Finkelb12da6b2013-04-18 22:54:25 +000053cl::desc("Disable compare instruction optimization"), cl::Hidden);
54
Hal Finkel174e5902014-03-25 23:29:21 +000055static cl::opt<bool> DisableVSXFMAMutate("disable-ppc-vsx-fma-mutation",
56cl::desc("Disable VSX FMA instruction mutation"), cl::Hidden);
57
Hal Finkel9dcb3582014-03-27 22:46:28 +000058static cl::opt<bool> VSXSelfCopyCrash("crash-on-ppc-vsx-self-copy",
59cl::desc("Causes the backend to crash instead of generating a nop VSX copy"),
60cl::Hidden);
61
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000062// Pin the vtable to this file.
63void PPCInstrInfo::anchor() {}
64
Eric Christopher1dcea732014-06-12 21:48:52 +000065PPCInstrInfo::PPCInstrInfo(PPCSubtarget &STI)
66 : PPCGenInstrInfo(PPC::ADJCALLSTACKDOWN, PPC::ADJCALLSTACKUP),
67 Subtarget(STI), RI(STI) {}
Chris Lattner49cadab2006-06-17 00:01:04 +000068
Andrew Trick10ffc2b2010-12-24 05:03:26 +000069/// CreateTargetHazardRecognizer - Return the hazard recognizer to use for
70/// this target when scheduling the DAG.
Eric Christopherf047bfd2014-06-13 22:38:52 +000071ScheduleHazardRecognizer *
72PPCInstrInfo::CreateTargetHazardRecognizer(const TargetSubtargetInfo *STI,
73 const ScheduleDAG *DAG) const {
74 unsigned Directive =
75 static_cast<const PPCSubtarget *>(STI)->getDarwinDirective();
Hal Finkel742b5352012-08-28 16:12:39 +000076 if (Directive == PPC::DIR_440 || Directive == PPC::DIR_A2 ||
77 Directive == PPC::DIR_E500mc || Directive == PPC::DIR_E5500) {
Eric Christopherf047bfd2014-06-13 22:38:52 +000078 const InstrItineraryData *II =
Eric Christopherd9134482014-08-04 21:25:23 +000079 static_cast<const PPCSubtarget *>(STI)->getInstrItineraryData();
Hal Finkel563cc052013-12-02 23:52:46 +000080 return new ScoreboardHazardRecognizer(II, DAG);
Hal Finkel6fa56972011-10-17 04:03:49 +000081 }
Hal Finkel58ca3602011-12-02 04:58:02 +000082
Eric Christopherf047bfd2014-06-13 22:38:52 +000083 return TargetInstrInfo::CreateTargetHazardRecognizer(STI, DAG);
Andrew Trick10ffc2b2010-12-24 05:03:26 +000084}
85
Hal Finkel58ca3602011-12-02 04:58:02 +000086/// CreateTargetPostRAHazardRecognizer - Return the postRA hazard recognizer
87/// to use for this target when scheduling the DAG.
88ScheduleHazardRecognizer *PPCInstrInfo::CreateTargetPostRAHazardRecognizer(
89 const InstrItineraryData *II,
90 const ScheduleDAG *DAG) const {
Eric Christopher1dcea732014-06-12 21:48:52 +000091 unsigned Directive =
92 DAG->TM.getSubtarget<PPCSubtarget>().getDarwinDirective();
Hal Finkel58ca3602011-12-02 04:58:02 +000093
Will Schmidt970ff642014-06-26 13:36:19 +000094 if (Directive == PPC::DIR_PWR7 || Directive == PPC::DIR_PWR8)
Hal Finkelceb1f122013-12-12 00:19:11 +000095 return new PPCDispatchGroupSBHazardRecognizer(II, DAG);
96
Hal Finkel58ca3602011-12-02 04:58:02 +000097 // Most subtargets use a PPC970 recognizer.
Hal Finkel742b5352012-08-28 16:12:39 +000098 if (Directive != PPC::DIR_440 && Directive != PPC::DIR_A2 &&
99 Directive != PPC::DIR_E500mc && Directive != PPC::DIR_E5500) {
Eric Christopher1dcea732014-06-12 21:48:52 +0000100 assert(DAG->TII && "No InstrInfo?");
Hal Finkel58ca3602011-12-02 04:58:02 +0000101
Eric Christopher1dcea732014-06-12 21:48:52 +0000102 return new PPCHazardRecognizer970(*DAG);
Hal Finkel58ca3602011-12-02 04:58:02 +0000103 }
104
Hal Finkel563cc052013-12-02 23:52:46 +0000105 return new ScoreboardHazardRecognizer(II, DAG);
Hal Finkel58ca3602011-12-02 04:58:02 +0000106}
Jakob Stoklund Olesen0f855e42012-06-19 21:14:34 +0000107
Hal Finkelceb1f122013-12-12 00:19:11 +0000108
109int PPCInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
110 const MachineInstr *DefMI, unsigned DefIdx,
111 const MachineInstr *UseMI,
112 unsigned UseIdx) const {
113 int Latency = PPCGenInstrInfo::getOperandLatency(ItinData, DefMI, DefIdx,
114 UseMI, UseIdx);
115
116 const MachineOperand &DefMO = DefMI->getOperand(DefIdx);
117 unsigned Reg = DefMO.getReg();
118
119 const TargetRegisterInfo *TRI = &getRegisterInfo();
120 bool IsRegCR;
121 if (TRI->isVirtualRegister(Reg)) {
122 const MachineRegisterInfo *MRI =
123 &DefMI->getParent()->getParent()->getRegInfo();
124 IsRegCR = MRI->getRegClass(Reg)->hasSuperClassEq(&PPC::CRRCRegClass) ||
125 MRI->getRegClass(Reg)->hasSuperClassEq(&PPC::CRBITRCRegClass);
126 } else {
127 IsRegCR = PPC::CRRCRegClass.contains(Reg) ||
128 PPC::CRBITRCRegClass.contains(Reg);
129 }
130
131 if (UseMI->isBranch() && IsRegCR) {
132 if (Latency < 0)
133 Latency = getInstrLatency(ItinData, DefMI);
134
135 // On some cores, there is an additional delay between writing to a condition
136 // register, and using it from a branch.
Eric Christopher1dcea732014-06-12 21:48:52 +0000137 unsigned Directive = Subtarget.getDarwinDirective();
Hal Finkelceb1f122013-12-12 00:19:11 +0000138 switch (Directive) {
139 default: break;
140 case PPC::DIR_7400:
141 case PPC::DIR_750:
142 case PPC::DIR_970:
143 case PPC::DIR_E5500:
144 case PPC::DIR_PWR4:
145 case PPC::DIR_PWR5:
146 case PPC::DIR_PWR5X:
147 case PPC::DIR_PWR6:
148 case PPC::DIR_PWR6X:
149 case PPC::DIR_PWR7:
Will Schmidt970ff642014-06-26 13:36:19 +0000150 case PPC::DIR_PWR8:
Hal Finkelceb1f122013-12-12 00:19:11 +0000151 Latency += 2;
152 break;
153 }
154 }
155
156 return Latency;
157}
158
Jakob Stoklund Olesen0f855e42012-06-19 21:14:34 +0000159// Detect 32 -> 64-bit extensions where we may reuse the low sub-register.
160bool PPCInstrInfo::isCoalescableExtInstr(const MachineInstr &MI,
161 unsigned &SrcReg, unsigned &DstReg,
162 unsigned &SubIdx) const {
163 switch (MI.getOpcode()) {
164 default: return false;
165 case PPC::EXTSW:
166 case PPC::EXTSW_32_64:
167 SrcReg = MI.getOperand(1).getReg();
168 DstReg = MI.getOperand(0).getReg();
169 SubIdx = PPC::sub_32;
170 return true;
171 }
172}
173
Andrew Trickc416ba62010-12-24 04:28:06 +0000174unsigned PPCInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
Chris Lattner91400bd2006-03-16 22:24:02 +0000175 int &FrameIndex) const {
Hal Finkel37714b82013-03-27 21:21:15 +0000176 // Note: This list must be kept consistent with LoadRegFromStackSlot.
Chris Lattnerbb53acd2006-02-02 20:12:32 +0000177 switch (MI->getOpcode()) {
178 default: break;
179 case PPC::LD:
180 case PPC::LWZ:
181 case PPC::LFS:
182 case PPC::LFD:
Hal Finkel37714b82013-03-27 21:21:15 +0000183 case PPC::RESTORE_CR:
Hal Finkel940ab932014-02-28 00:27:01 +0000184 case PPC::RESTORE_CRBIT:
Hal Finkel37714b82013-03-27 21:21:15 +0000185 case PPC::LVX:
Hal Finkel27774d92014-03-13 07:58:58 +0000186 case PPC::LXVD2X:
Hal Finkel37714b82013-03-27 21:21:15 +0000187 case PPC::RESTORE_VRSAVE:
188 // Check for the operands added by addFrameReference (the immediate is the
189 // offset which defaults to 0).
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000190 if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
191 MI->getOperand(2).isFI()) {
Chris Lattnera5bb3702007-12-30 23:10:15 +0000192 FrameIndex = MI->getOperand(2).getIndex();
Chris Lattnerbb53acd2006-02-02 20:12:32 +0000193 return MI->getOperand(0).getReg();
194 }
195 break;
196 }
197 return 0;
Chris Lattnerc327d712006-02-02 20:16:12 +0000198}
Chris Lattnerbb53acd2006-02-02 20:12:32 +0000199
Andrew Trickc416ba62010-12-24 04:28:06 +0000200unsigned PPCInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
Chris Lattnerc327d712006-02-02 20:16:12 +0000201 int &FrameIndex) const {
Hal Finkel37714b82013-03-27 21:21:15 +0000202 // Note: This list must be kept consistent with StoreRegToStackSlot.
Chris Lattnerc327d712006-02-02 20:16:12 +0000203 switch (MI->getOpcode()) {
204 default: break;
Nate Begeman4efb3282006-02-02 21:07:50 +0000205 case PPC::STD:
Chris Lattnerc327d712006-02-02 20:16:12 +0000206 case PPC::STW:
207 case PPC::STFS:
208 case PPC::STFD:
Hal Finkel37714b82013-03-27 21:21:15 +0000209 case PPC::SPILL_CR:
Hal Finkel940ab932014-02-28 00:27:01 +0000210 case PPC::SPILL_CRBIT:
Hal Finkel37714b82013-03-27 21:21:15 +0000211 case PPC::STVX:
Hal Finkel27774d92014-03-13 07:58:58 +0000212 case PPC::STXVD2X:
Hal Finkel37714b82013-03-27 21:21:15 +0000213 case PPC::SPILL_VRSAVE:
214 // Check for the operands added by addFrameReference (the immediate is the
215 // offset which defaults to 0).
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000216 if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
217 MI->getOperand(2).isFI()) {
Chris Lattnera5bb3702007-12-30 23:10:15 +0000218 FrameIndex = MI->getOperand(2).getIndex();
Chris Lattnerc327d712006-02-02 20:16:12 +0000219 return MI->getOperand(0).getReg();
220 }
221 break;
222 }
223 return 0;
224}
Chris Lattnerbb53acd2006-02-02 20:12:32 +0000225
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000226// commuteInstruction - We can commute rlwimi instructions, but only if the
227// rotate amt is zero. We also have to munge the immediates a bit.
Evan Cheng03553bb2008-06-16 07:33:11 +0000228MachineInstr *
229PPCInstrInfo::commuteInstruction(MachineInstr *MI, bool NewMI) const {
Dan Gohman3b460302008-07-07 23:14:23 +0000230 MachineFunction &MF = *MI->getParent()->getParent();
231
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000232 // Normal instructions can be commuted the obvious way.
Hal Finkel654d43b2013-04-12 02:18:09 +0000233 if (MI->getOpcode() != PPC::RLWIMI &&
Hal Finkel4c6658f2014-12-12 23:59:36 +0000234 MI->getOpcode() != PPC::RLWIMIo)
Jakob Stoklund Olesen9de596e2012-11-28 02:35:17 +0000235 return TargetInstrInfo::commuteInstruction(MI, NewMI);
Hal Finkel4c6658f2014-12-12 23:59:36 +0000236 // Note that RLWIMI can be commuted as a 32-bit instruction, but not as a
237 // 64-bit instruction (so we don't handle PPC::RLWIMI8 here), because
238 // changing the relative order of the mask operands might change what happens
239 // to the high-bits of the mask (and, thus, the result).
Andrew Trickc416ba62010-12-24 04:28:06 +0000240
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000241 // Cannot commute if it has a non-zero rotate count.
Chris Lattner5c463782007-12-30 20:49:49 +0000242 if (MI->getOperand(3).getImm() != 0)
Craig Topper062a2ba2014-04-25 05:30:21 +0000243 return nullptr;
Andrew Trickc416ba62010-12-24 04:28:06 +0000244
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000245 // If we have a zero rotate count, we have:
246 // M = mask(MB,ME)
247 // Op0 = (Op1 & ~M) | (Op2 & M)
248 // Change this to:
249 // M = mask((ME+1)&31, (MB-1)&31)
250 // Op0 = (Op2 & ~M) | (Op1 & M)
251
252 // Swap op1/op2
Evan Cheng244183e2008-02-13 02:46:49 +0000253 unsigned Reg0 = MI->getOperand(0).getReg();
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000254 unsigned Reg1 = MI->getOperand(1).getReg();
255 unsigned Reg2 = MI->getOperand(2).getReg();
Andrew Tricke3398282013-12-17 04:50:45 +0000256 unsigned SubReg1 = MI->getOperand(1).getSubReg();
257 unsigned SubReg2 = MI->getOperand(2).getSubReg();
Evan Chengdc2c8742006-11-15 20:58:11 +0000258 bool Reg1IsKill = MI->getOperand(1).isKill();
259 bool Reg2IsKill = MI->getOperand(2).isKill();
Evan Cheng03553bb2008-06-16 07:33:11 +0000260 bool ChangeReg0 = false;
Evan Cheng244183e2008-02-13 02:46:49 +0000261 // If machine instrs are no longer in two-address forms, update
262 // destination register as well.
263 if (Reg0 == Reg1) {
264 // Must be two address instruction!
Evan Cheng6cc775f2011-06-28 19:10:37 +0000265 assert(MI->getDesc().getOperandConstraint(0, MCOI::TIED_TO) &&
Evan Cheng244183e2008-02-13 02:46:49 +0000266 "Expecting a two-address instruction!");
Andrew Tricke3398282013-12-17 04:50:45 +0000267 assert(MI->getOperand(0).getSubReg() == SubReg1 && "Tied subreg mismatch");
Evan Cheng244183e2008-02-13 02:46:49 +0000268 Reg2IsKill = false;
Evan Cheng03553bb2008-06-16 07:33:11 +0000269 ChangeReg0 = true;
Evan Cheng244183e2008-02-13 02:46:49 +0000270 }
Evan Cheng03553bb2008-06-16 07:33:11 +0000271
272 // Masks.
273 unsigned MB = MI->getOperand(4).getImm();
274 unsigned ME = MI->getOperand(5).getImm();
275
276 if (NewMI) {
277 // Create a new instruction.
278 unsigned Reg0 = ChangeReg0 ? Reg2 : MI->getOperand(0).getReg();
279 bool Reg0IsDead = MI->getOperand(0).isDead();
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000280 return BuildMI(MF, MI->getDebugLoc(), MI->getDesc())
Bill Wendlingf7b83c72009-05-13 21:33:08 +0000281 .addReg(Reg0, RegState::Define | getDeadRegState(Reg0IsDead))
282 .addReg(Reg2, getKillRegState(Reg2IsKill))
283 .addReg(Reg1, getKillRegState(Reg1IsKill))
Evan Cheng03553bb2008-06-16 07:33:11 +0000284 .addImm((ME+1) & 31)
285 .addImm((MB-1) & 31);
286 }
287
Andrew Tricke3398282013-12-17 04:50:45 +0000288 if (ChangeReg0) {
Evan Cheng03553bb2008-06-16 07:33:11 +0000289 MI->getOperand(0).setReg(Reg2);
Andrew Tricke3398282013-12-17 04:50:45 +0000290 MI->getOperand(0).setSubReg(SubReg2);
291 }
Chris Lattner10d63412006-05-04 17:52:23 +0000292 MI->getOperand(2).setReg(Reg1);
293 MI->getOperand(1).setReg(Reg2);
Andrew Tricke3398282013-12-17 04:50:45 +0000294 MI->getOperand(2).setSubReg(SubReg1);
295 MI->getOperand(1).setSubReg(SubReg2);
Chris Lattner60055892007-12-30 21:56:09 +0000296 MI->getOperand(2).setIsKill(Reg1IsKill);
297 MI->getOperand(1).setIsKill(Reg2IsKill);
Andrew Trickc416ba62010-12-24 04:28:06 +0000298
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000299 // Swap the mask around.
Chris Lattner5c463782007-12-30 20:49:49 +0000300 MI->getOperand(4).setImm((ME+1) & 31);
301 MI->getOperand(5).setImm((MB-1) & 31);
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000302 return MI;
303}
Chris Lattnerea79d9fd732006-03-05 23:49:55 +0000304
Hal Finkel6c32ff32014-03-25 19:26:43 +0000305bool PPCInstrInfo::findCommutedOpIndices(MachineInstr *MI, unsigned &SrcOpIdx1,
306 unsigned &SrcOpIdx2) const {
307 // For VSX A-Type FMA instructions, it is the first two operands that can be
308 // commuted, however, because the non-encoded tied input operand is listed
309 // first, the operands to swap are actually the second and third.
310
311 int AltOpc = PPC::getAltVSXFMAOpcode(MI->getOpcode());
312 if (AltOpc == -1)
313 return TargetInstrInfo::findCommutedOpIndices(MI, SrcOpIdx1, SrcOpIdx2);
314
315 SrcOpIdx1 = 2;
316 SrcOpIdx2 = 3;
317 return true;
318}
319
Andrew Trickc416ba62010-12-24 04:28:06 +0000320void PPCInstrInfo::insertNoop(MachineBasicBlock &MBB,
Chris Lattnerea79d9fd732006-03-05 23:49:55 +0000321 MachineBasicBlock::iterator MI) const {
Hal Finkelceb1f122013-12-12 00:19:11 +0000322 // This function is used for scheduling, and the nop wanted here is the type
323 // that terminates dispatch groups on the POWER cores.
Eric Christopher1dcea732014-06-12 21:48:52 +0000324 unsigned Directive = Subtarget.getDarwinDirective();
Hal Finkelceb1f122013-12-12 00:19:11 +0000325 unsigned Opcode;
326 switch (Directive) {
327 default: Opcode = PPC::NOP; break;
328 case PPC::DIR_PWR6: Opcode = PPC::NOP_GT_PWR6; break;
329 case PPC::DIR_PWR7: Opcode = PPC::NOP_GT_PWR7; break;
Will Schmidt970ff642014-06-26 13:36:19 +0000330 case PPC::DIR_PWR8: Opcode = PPC::NOP_GT_PWR7; break; /* FIXME: Update when P8 InstrScheduling model is ready */
Hal Finkelceb1f122013-12-12 00:19:11 +0000331 }
Chris Lattnera47294ed2006-10-13 21:21:17 +0000332
Hal Finkelceb1f122013-12-12 00:19:11 +0000333 DebugLoc DL;
334 BuildMI(MBB, MI, DL, get(Opcode));
335}
Chris Lattnera47294ed2006-10-13 21:21:17 +0000336
Joerg Sonnenberger7ee0f312014-08-08 19:13:23 +0000337/// getNoopForMachoTarget - Return the noop instruction to use for a noop.
338void PPCInstrInfo::getNoopForMachoTarget(MCInst &NopInst) const {
339 NopInst.setOpcode(PPC::NOP);
340}
341
Chris Lattnera47294ed2006-10-13 21:21:17 +0000342// Branch analysis.
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000343// Note: If the condition register is set to CTR or CTR8 then this is a
344// BDNZ (imm == 1) or BDZ (imm == 0) branch.
Chris Lattnera47294ed2006-10-13 21:21:17 +0000345bool PPCInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
346 MachineBasicBlock *&FBB,
Evan Cheng64dfcac2009-02-09 07:14:22 +0000347 SmallVectorImpl<MachineOperand> &Cond,
348 bool AllowModify) const {
Eric Christopher1dcea732014-06-12 21:48:52 +0000349 bool isPPC64 = Subtarget.isPPC64();
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000350
Chris Lattnera47294ed2006-10-13 21:21:17 +0000351 // If the block has no terminators, it just falls into the block after it.
352 MachineBasicBlock::iterator I = MBB.end();
Dale Johannesen4244d122010-04-02 01:38:09 +0000353 if (I == MBB.begin())
354 return false;
355 --I;
356 while (I->isDebugValue()) {
357 if (I == MBB.begin())
358 return false;
359 --I;
360 }
361 if (!isUnpredicatedTerminator(I))
Chris Lattnera47294ed2006-10-13 21:21:17 +0000362 return false;
363
364 // Get the last instruction in the block.
365 MachineInstr *LastInst = I;
Andrew Trickc416ba62010-12-24 04:28:06 +0000366
Chris Lattnera47294ed2006-10-13 21:21:17 +0000367 // If there is only one terminator instruction, process it.
Evan Cheng5514bbe2007-06-08 21:59:56 +0000368 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
Chris Lattnera47294ed2006-10-13 21:21:17 +0000369 if (LastInst->getOpcode() == PPC::B) {
Evan Cheng8f43afd2009-05-08 23:09:25 +0000370 if (!LastInst->getOperand(0).isMBB())
371 return true;
Chris Lattnera5bb3702007-12-30 23:10:15 +0000372 TBB = LastInst->getOperand(0).getMBB();
Chris Lattnera47294ed2006-10-13 21:21:17 +0000373 return false;
Chris Lattnere0263792006-11-17 22:14:47 +0000374 } else if (LastInst->getOpcode() == PPC::BCC) {
Evan Cheng8f43afd2009-05-08 23:09:25 +0000375 if (!LastInst->getOperand(2).isMBB())
376 return true;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000377 // Block ends with fall-through condbranch.
Chris Lattnera5bb3702007-12-30 23:10:15 +0000378 TBB = LastInst->getOperand(2).getMBB();
Chris Lattnera47294ed2006-10-13 21:21:17 +0000379 Cond.push_back(LastInst->getOperand(0));
380 Cond.push_back(LastInst->getOperand(1));
Chris Lattner23f22de2006-10-21 06:03:11 +0000381 return false;
Hal Finkel940ab932014-02-28 00:27:01 +0000382 } else if (LastInst->getOpcode() == PPC::BC) {
383 if (!LastInst->getOperand(1).isMBB())
384 return true;
385 // Block ends with fall-through condbranch.
386 TBB = LastInst->getOperand(1).getMBB();
387 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET));
388 Cond.push_back(LastInst->getOperand(0));
389 return false;
390 } else if (LastInst->getOpcode() == PPC::BCn) {
391 if (!LastInst->getOperand(1).isMBB())
392 return true;
393 // Block ends with fall-through condbranch.
394 TBB = LastInst->getOperand(1).getMBB();
395 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_UNSET));
396 Cond.push_back(LastInst->getOperand(0));
397 return false;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000398 } else if (LastInst->getOpcode() == PPC::BDNZ8 ||
399 LastInst->getOpcode() == PPC::BDNZ) {
400 if (!LastInst->getOperand(0).isMBB())
401 return true;
Hal Finkelc6b5deb2012-06-08 19:19:53 +0000402 if (DisableCTRLoopAnal)
Hal Finkel821e0012012-06-08 15:38:25 +0000403 return true;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000404 TBB = LastInst->getOperand(0).getMBB();
405 Cond.push_back(MachineOperand::CreateImm(1));
406 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
407 true));
408 return false;
409 } else if (LastInst->getOpcode() == PPC::BDZ8 ||
410 LastInst->getOpcode() == PPC::BDZ) {
411 if (!LastInst->getOperand(0).isMBB())
412 return true;
Hal Finkelc6b5deb2012-06-08 19:19:53 +0000413 if (DisableCTRLoopAnal)
Hal Finkel821e0012012-06-08 15:38:25 +0000414 return true;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000415 TBB = LastInst->getOperand(0).getMBB();
416 Cond.push_back(MachineOperand::CreateImm(0));
417 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
418 true));
419 return false;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000420 }
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000421
Chris Lattnera47294ed2006-10-13 21:21:17 +0000422 // Otherwise, don't know what this is.
423 return true;
424 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000425
Chris Lattnera47294ed2006-10-13 21:21:17 +0000426 // Get the instruction before it if it's a terminator.
427 MachineInstr *SecondLastInst = I;
428
429 // If there are three terminators, we don't know what sort of block this is.
430 if (SecondLastInst && I != MBB.begin() &&
Evan Cheng5514bbe2007-06-08 21:59:56 +0000431 isUnpredicatedTerminator(--I))
Chris Lattnera47294ed2006-10-13 21:21:17 +0000432 return true;
Andrew Trickc416ba62010-12-24 04:28:06 +0000433
Chris Lattnere0263792006-11-17 22:14:47 +0000434 // If the block ends with PPC::B and PPC:BCC, handle it.
Andrew Trickc416ba62010-12-24 04:28:06 +0000435 if (SecondLastInst->getOpcode() == PPC::BCC &&
Chris Lattnera47294ed2006-10-13 21:21:17 +0000436 LastInst->getOpcode() == PPC::B) {
Evan Cheng8f43afd2009-05-08 23:09:25 +0000437 if (!SecondLastInst->getOperand(2).isMBB() ||
438 !LastInst->getOperand(0).isMBB())
439 return true;
Chris Lattnera5bb3702007-12-30 23:10:15 +0000440 TBB = SecondLastInst->getOperand(2).getMBB();
Chris Lattnera47294ed2006-10-13 21:21:17 +0000441 Cond.push_back(SecondLastInst->getOperand(0));
442 Cond.push_back(SecondLastInst->getOperand(1));
Chris Lattnera5bb3702007-12-30 23:10:15 +0000443 FBB = LastInst->getOperand(0).getMBB();
Chris Lattnera47294ed2006-10-13 21:21:17 +0000444 return false;
Hal Finkel940ab932014-02-28 00:27:01 +0000445 } else if (SecondLastInst->getOpcode() == PPC::BC &&
446 LastInst->getOpcode() == PPC::B) {
447 if (!SecondLastInst->getOperand(1).isMBB() ||
448 !LastInst->getOperand(0).isMBB())
449 return true;
450 TBB = SecondLastInst->getOperand(1).getMBB();
451 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET));
452 Cond.push_back(SecondLastInst->getOperand(0));
453 FBB = LastInst->getOperand(0).getMBB();
454 return false;
455 } else if (SecondLastInst->getOpcode() == PPC::BCn &&
456 LastInst->getOpcode() == PPC::B) {
457 if (!SecondLastInst->getOperand(1).isMBB() ||
458 !LastInst->getOperand(0).isMBB())
459 return true;
460 TBB = SecondLastInst->getOperand(1).getMBB();
461 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_UNSET));
462 Cond.push_back(SecondLastInst->getOperand(0));
463 FBB = LastInst->getOperand(0).getMBB();
464 return false;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000465 } else if ((SecondLastInst->getOpcode() == PPC::BDNZ8 ||
466 SecondLastInst->getOpcode() == PPC::BDNZ) &&
467 LastInst->getOpcode() == PPC::B) {
468 if (!SecondLastInst->getOperand(0).isMBB() ||
469 !LastInst->getOperand(0).isMBB())
470 return true;
Hal Finkelc6b5deb2012-06-08 19:19:53 +0000471 if (DisableCTRLoopAnal)
Hal Finkel821e0012012-06-08 15:38:25 +0000472 return true;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000473 TBB = SecondLastInst->getOperand(0).getMBB();
474 Cond.push_back(MachineOperand::CreateImm(1));
475 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
476 true));
477 FBB = LastInst->getOperand(0).getMBB();
478 return false;
479 } else if ((SecondLastInst->getOpcode() == PPC::BDZ8 ||
480 SecondLastInst->getOpcode() == PPC::BDZ) &&
481 LastInst->getOpcode() == PPC::B) {
482 if (!SecondLastInst->getOperand(0).isMBB() ||
483 !LastInst->getOperand(0).isMBB())
484 return true;
Hal Finkelc6b5deb2012-06-08 19:19:53 +0000485 if (DisableCTRLoopAnal)
Hal Finkel821e0012012-06-08 15:38:25 +0000486 return true;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000487 TBB = SecondLastInst->getOperand(0).getMBB();
488 Cond.push_back(MachineOperand::CreateImm(0));
489 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
490 true));
491 FBB = LastInst->getOperand(0).getMBB();
492 return false;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000493 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000494
Dale Johannesenc6855462007-06-13 17:59:52 +0000495 // If the block ends with two PPC:Bs, handle it. The second one is not
496 // executed, so remove it.
Andrew Trickc416ba62010-12-24 04:28:06 +0000497 if (SecondLastInst->getOpcode() == PPC::B &&
Dale Johannesenc6855462007-06-13 17:59:52 +0000498 LastInst->getOpcode() == PPC::B) {
Evan Cheng8f43afd2009-05-08 23:09:25 +0000499 if (!SecondLastInst->getOperand(0).isMBB())
500 return true;
Chris Lattnera5bb3702007-12-30 23:10:15 +0000501 TBB = SecondLastInst->getOperand(0).getMBB();
Dale Johannesenc6855462007-06-13 17:59:52 +0000502 I = LastInst;
Evan Cheng64dfcac2009-02-09 07:14:22 +0000503 if (AllowModify)
504 I->eraseFromParent();
Dale Johannesenc6855462007-06-13 17:59:52 +0000505 return false;
506 }
507
Chris Lattnera47294ed2006-10-13 21:21:17 +0000508 // Otherwise, can't handle this.
509 return true;
510}
511
Evan Cheng99be49d2007-05-18 00:05:48 +0000512unsigned PPCInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
Chris Lattnera47294ed2006-10-13 21:21:17 +0000513 MachineBasicBlock::iterator I = MBB.end();
Evan Cheng99be49d2007-05-18 00:05:48 +0000514 if (I == MBB.begin()) return 0;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000515 --I;
Dale Johannesen4244d122010-04-02 01:38:09 +0000516 while (I->isDebugValue()) {
517 if (I == MBB.begin())
518 return 0;
519 --I;
520 }
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000521 if (I->getOpcode() != PPC::B && I->getOpcode() != PPC::BCC &&
Hal Finkel940ab932014-02-28 00:27:01 +0000522 I->getOpcode() != PPC::BC && I->getOpcode() != PPC::BCn &&
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000523 I->getOpcode() != PPC::BDNZ8 && I->getOpcode() != PPC::BDNZ &&
524 I->getOpcode() != PPC::BDZ8 && I->getOpcode() != PPC::BDZ)
Evan Cheng99be49d2007-05-18 00:05:48 +0000525 return 0;
Andrew Trickc416ba62010-12-24 04:28:06 +0000526
Chris Lattnera47294ed2006-10-13 21:21:17 +0000527 // Remove the branch.
528 I->eraseFromParent();
Andrew Trickc416ba62010-12-24 04:28:06 +0000529
Chris Lattnera47294ed2006-10-13 21:21:17 +0000530 I = MBB.end();
531
Evan Cheng99be49d2007-05-18 00:05:48 +0000532 if (I == MBB.begin()) return 1;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000533 --I;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000534 if (I->getOpcode() != PPC::BCC &&
Hal Finkel940ab932014-02-28 00:27:01 +0000535 I->getOpcode() != PPC::BC && I->getOpcode() != PPC::BCn &&
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000536 I->getOpcode() != PPC::BDNZ8 && I->getOpcode() != PPC::BDNZ &&
537 I->getOpcode() != PPC::BDZ8 && I->getOpcode() != PPC::BDZ)
Evan Cheng99be49d2007-05-18 00:05:48 +0000538 return 1;
Andrew Trickc416ba62010-12-24 04:28:06 +0000539
Chris Lattnera47294ed2006-10-13 21:21:17 +0000540 // Remove the branch.
541 I->eraseFromParent();
Evan Cheng99be49d2007-05-18 00:05:48 +0000542 return 2;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000543}
544
Evan Cheng99be49d2007-05-18 00:05:48 +0000545unsigned
546PPCInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
547 MachineBasicBlock *FBB,
Stuart Hastings0125b642010-06-17 22:43:56 +0000548 const SmallVectorImpl<MachineOperand> &Cond,
549 DebugLoc DL) const {
Chris Lattnera61f0102006-10-17 18:06:55 +0000550 // Shouldn't be a fall through.
551 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Andrew Trickc416ba62010-12-24 04:28:06 +0000552 assert((Cond.size() == 2 || Cond.size() == 0) &&
Chris Lattner94e04442006-10-21 05:36:13 +0000553 "PPC branch conditions have two components!");
Andrew Trickc416ba62010-12-24 04:28:06 +0000554
Eric Christopher1dcea732014-06-12 21:48:52 +0000555 bool isPPC64 = Subtarget.isPPC64();
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000556
Chris Lattner94e04442006-10-21 05:36:13 +0000557 // One-way branch.
Craig Topper062a2ba2014-04-25 05:30:21 +0000558 if (!FBB) {
Chris Lattner94e04442006-10-21 05:36:13 +0000559 if (Cond.empty()) // Unconditional branch
Stuart Hastings0125b642010-06-17 22:43:56 +0000560 BuildMI(&MBB, DL, get(PPC::B)).addMBB(TBB);
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000561 else if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
562 BuildMI(&MBB, DL, get(Cond[0].getImm() ?
563 (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
564 (isPPC64 ? PPC::BDZ8 : PPC::BDZ))).addMBB(TBB);
Hal Finkel940ab932014-02-28 00:27:01 +0000565 else if (Cond[0].getImm() == PPC::PRED_BIT_SET)
566 BuildMI(&MBB, DL, get(PPC::BC)).addOperand(Cond[1]).addMBB(TBB);
567 else if (Cond[0].getImm() == PPC::PRED_BIT_UNSET)
568 BuildMI(&MBB, DL, get(PPC::BCn)).addOperand(Cond[1]).addMBB(TBB);
Chris Lattner94e04442006-10-21 05:36:13 +0000569 else // Conditional branch
Stuart Hastings0125b642010-06-17 22:43:56 +0000570 BuildMI(&MBB, DL, get(PPC::BCC))
Hal Finkel940ab932014-02-28 00:27:01 +0000571 .addImm(Cond[0].getImm()).addOperand(Cond[1]).addMBB(TBB);
Evan Cheng99be49d2007-05-18 00:05:48 +0000572 return 1;
Chris Lattnera61f0102006-10-17 18:06:55 +0000573 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000574
Chris Lattnerd8816602006-10-21 05:42:09 +0000575 // Two-way Conditional Branch.
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000576 if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
577 BuildMI(&MBB, DL, get(Cond[0].getImm() ?
578 (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
579 (isPPC64 ? PPC::BDZ8 : PPC::BDZ))).addMBB(TBB);
Hal Finkel940ab932014-02-28 00:27:01 +0000580 else if (Cond[0].getImm() == PPC::PRED_BIT_SET)
581 BuildMI(&MBB, DL, get(PPC::BC)).addOperand(Cond[1]).addMBB(TBB);
582 else if (Cond[0].getImm() == PPC::PRED_BIT_UNSET)
583 BuildMI(&MBB, DL, get(PPC::BCn)).addOperand(Cond[1]).addMBB(TBB);
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000584 else
585 BuildMI(&MBB, DL, get(PPC::BCC))
Hal Finkel940ab932014-02-28 00:27:01 +0000586 .addImm(Cond[0].getImm()).addOperand(Cond[1]).addMBB(TBB);
Stuart Hastings0125b642010-06-17 22:43:56 +0000587 BuildMI(&MBB, DL, get(PPC::B)).addMBB(FBB);
Evan Cheng99be49d2007-05-18 00:05:48 +0000588 return 2;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000589}
590
Hal Finkeled6a2852013-04-05 23:29:01 +0000591// Select analysis.
592bool PPCInstrInfo::canInsertSelect(const MachineBasicBlock &MBB,
593 const SmallVectorImpl<MachineOperand> &Cond,
594 unsigned TrueReg, unsigned FalseReg,
595 int &CondCycles, int &TrueCycles, int &FalseCycles) const {
Eric Christopher1dcea732014-06-12 21:48:52 +0000596 if (!Subtarget.hasISEL())
Hal Finkeled6a2852013-04-05 23:29:01 +0000597 return false;
598
599 if (Cond.size() != 2)
600 return false;
601
602 // If this is really a bdnz-like condition, then it cannot be turned into a
603 // select.
604 if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
605 return false;
606
607 // Check register classes.
608 const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
609 const TargetRegisterClass *RC =
610 RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg));
611 if (!RC)
612 return false;
613
614 // isel is for regular integer GPRs only.
615 if (!PPC::GPRCRegClass.hasSubClassEq(RC) &&
Hal Finkel8e8618a2013-07-15 20:22:58 +0000616 !PPC::GPRC_NOR0RegClass.hasSubClassEq(RC) &&
617 !PPC::G8RCRegClass.hasSubClassEq(RC) &&
618 !PPC::G8RC_NOX0RegClass.hasSubClassEq(RC))
Hal Finkeled6a2852013-04-05 23:29:01 +0000619 return false;
620
621 // FIXME: These numbers are for the A2, how well they work for other cores is
622 // an open question. On the A2, the isel instruction has a 2-cycle latency
623 // but single-cycle throughput. These numbers are used in combination with
624 // the MispredictPenalty setting from the active SchedMachineModel.
625 CondCycles = 1;
626 TrueCycles = 1;
627 FalseCycles = 1;
628
629 return true;
630}
631
632void PPCInstrInfo::insertSelect(MachineBasicBlock &MBB,
633 MachineBasicBlock::iterator MI, DebugLoc dl,
634 unsigned DestReg,
635 const SmallVectorImpl<MachineOperand> &Cond,
636 unsigned TrueReg, unsigned FalseReg) const {
637 assert(Cond.size() == 2 &&
638 "PPC branch conditions have two components!");
639
Eric Christopher1dcea732014-06-12 21:48:52 +0000640 assert(Subtarget.hasISEL() &&
Hal Finkeled6a2852013-04-05 23:29:01 +0000641 "Cannot insert select on target without ISEL support");
642
643 // Get the register classes.
644 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
645 const TargetRegisterClass *RC =
646 RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg));
647 assert(RC && "TrueReg and FalseReg must have overlapping register classes");
Hal Finkel8e8618a2013-07-15 20:22:58 +0000648
649 bool Is64Bit = PPC::G8RCRegClass.hasSubClassEq(RC) ||
650 PPC::G8RC_NOX0RegClass.hasSubClassEq(RC);
651 assert((Is64Bit ||
652 PPC::GPRCRegClass.hasSubClassEq(RC) ||
653 PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) &&
Hal Finkeled6a2852013-04-05 23:29:01 +0000654 "isel is for regular integer GPRs only");
655
Hal Finkel8e8618a2013-07-15 20:22:58 +0000656 unsigned OpCode = Is64Bit ? PPC::ISEL8 : PPC::ISEL;
Hal Finkeled6a2852013-04-05 23:29:01 +0000657 unsigned SelectPred = Cond[0].getImm();
658
659 unsigned SubIdx;
660 bool SwapOps;
661 switch (SelectPred) {
662 default: llvm_unreachable("invalid predicate for isel");
663 case PPC::PRED_EQ: SubIdx = PPC::sub_eq; SwapOps = false; break;
664 case PPC::PRED_NE: SubIdx = PPC::sub_eq; SwapOps = true; break;
665 case PPC::PRED_LT: SubIdx = PPC::sub_lt; SwapOps = false; break;
666 case PPC::PRED_GE: SubIdx = PPC::sub_lt; SwapOps = true; break;
667 case PPC::PRED_GT: SubIdx = PPC::sub_gt; SwapOps = false; break;
668 case PPC::PRED_LE: SubIdx = PPC::sub_gt; SwapOps = true; break;
669 case PPC::PRED_UN: SubIdx = PPC::sub_un; SwapOps = false; break;
670 case PPC::PRED_NU: SubIdx = PPC::sub_un; SwapOps = true; break;
Hal Finkel940ab932014-02-28 00:27:01 +0000671 case PPC::PRED_BIT_SET: SubIdx = 0; SwapOps = false; break;
672 case PPC::PRED_BIT_UNSET: SubIdx = 0; SwapOps = true; break;
Hal Finkeled6a2852013-04-05 23:29:01 +0000673 }
674
675 unsigned FirstReg = SwapOps ? FalseReg : TrueReg,
676 SecondReg = SwapOps ? TrueReg : FalseReg;
677
678 // The first input register of isel cannot be r0. If it is a member
679 // of a register class that can be r0, then copy it first (the
680 // register allocator should eliminate the copy).
681 if (MRI.getRegClass(FirstReg)->contains(PPC::R0) ||
682 MRI.getRegClass(FirstReg)->contains(PPC::X0)) {
683 const TargetRegisterClass *FirstRC =
684 MRI.getRegClass(FirstReg)->contains(PPC::X0) ?
685 &PPC::G8RC_NOX0RegClass : &PPC::GPRC_NOR0RegClass;
686 unsigned OldFirstReg = FirstReg;
687 FirstReg = MRI.createVirtualRegister(FirstRC);
688 BuildMI(MBB, MI, dl, get(TargetOpcode::COPY), FirstReg)
689 .addReg(OldFirstReg);
690 }
691
692 BuildMI(MBB, MI, dl, get(OpCode), DestReg)
693 .addReg(FirstReg).addReg(SecondReg)
694 .addReg(Cond[1].getReg(), 0, SubIdx);
695}
696
Jakob Stoklund Olesen0d611972010-07-11 07:31:00 +0000697void PPCInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
698 MachineBasicBlock::iterator I, DebugLoc DL,
699 unsigned DestReg, unsigned SrcReg,
700 bool KillSrc) const {
Hal Finkel27774d92014-03-13 07:58:58 +0000701 // We can end up with self copies and similar things as a result of VSX copy
Hal Finkel9dcb3582014-03-27 22:46:28 +0000702 // legalization. Promote them here.
Hal Finkel27774d92014-03-13 07:58:58 +0000703 const TargetRegisterInfo *TRI = &getRegisterInfo();
704 if (PPC::F8RCRegClass.contains(DestReg) &&
705 PPC::VSLRCRegClass.contains(SrcReg)) {
706 unsigned SuperReg =
707 TRI->getMatchingSuperReg(DestReg, PPC::sub_64, &PPC::VSRCRegClass);
708
Hal Finkel9dcb3582014-03-27 22:46:28 +0000709 if (VSXSelfCopyCrash && SrcReg == SuperReg)
710 llvm_unreachable("nop VSX copy");
Hal Finkel27774d92014-03-13 07:58:58 +0000711
712 DestReg = SuperReg;
713 } else if (PPC::VRRCRegClass.contains(DestReg) &&
714 PPC::VSHRCRegClass.contains(SrcReg)) {
715 unsigned SuperReg =
716 TRI->getMatchingSuperReg(DestReg, PPC::sub_128, &PPC::VSRCRegClass);
717
Hal Finkel9dcb3582014-03-27 22:46:28 +0000718 if (VSXSelfCopyCrash && SrcReg == SuperReg)
719 llvm_unreachable("nop VSX copy");
Hal Finkel27774d92014-03-13 07:58:58 +0000720
721 DestReg = SuperReg;
722 } else if (PPC::F8RCRegClass.contains(SrcReg) &&
723 PPC::VSLRCRegClass.contains(DestReg)) {
724 unsigned SuperReg =
725 TRI->getMatchingSuperReg(SrcReg, PPC::sub_64, &PPC::VSRCRegClass);
726
Hal Finkel9dcb3582014-03-27 22:46:28 +0000727 if (VSXSelfCopyCrash && DestReg == SuperReg)
728 llvm_unreachable("nop VSX copy");
Hal Finkel27774d92014-03-13 07:58:58 +0000729
730 SrcReg = SuperReg;
731 } else if (PPC::VRRCRegClass.contains(SrcReg) &&
732 PPC::VSHRCRegClass.contains(DestReg)) {
733 unsigned SuperReg =
734 TRI->getMatchingSuperReg(SrcReg, PPC::sub_128, &PPC::VSRCRegClass);
735
Hal Finkel9dcb3582014-03-27 22:46:28 +0000736 if (VSXSelfCopyCrash && DestReg == SuperReg)
737 llvm_unreachable("nop VSX copy");
Hal Finkel27774d92014-03-13 07:58:58 +0000738
739 SrcReg = SuperReg;
740 }
741
Jakob Stoklund Olesen0d611972010-07-11 07:31:00 +0000742 unsigned Opc;
743 if (PPC::GPRCRegClass.contains(DestReg, SrcReg))
744 Opc = PPC::OR;
745 else if (PPC::G8RCRegClass.contains(DestReg, SrcReg))
746 Opc = PPC::OR8;
747 else if (PPC::F4RCRegClass.contains(DestReg, SrcReg))
748 Opc = PPC::FMR;
749 else if (PPC::CRRCRegClass.contains(DestReg, SrcReg))
750 Opc = PPC::MCRF;
751 else if (PPC::VRRCRegClass.contains(DestReg, SrcReg))
752 Opc = PPC::VOR;
Hal Finkel27774d92014-03-13 07:58:58 +0000753 else if (PPC::VSRCRegClass.contains(DestReg, SrcReg))
Hal Finkelbbad2332014-03-24 09:36:36 +0000754 // There are two different ways this can be done:
Hal Finkel27774d92014-03-13 07:58:58 +0000755 // 1. xxlor : This has lower latency (on the P7), 2 cycles, but can only
756 // issue in VSU pipeline 0.
757 // 2. xmovdp/xmovsp: This has higher latency (on the P7), 6 cycles, but
758 // can go to either pipeline.
Hal Finkelbbad2332014-03-24 09:36:36 +0000759 // We'll always use xxlor here, because in practically all cases where
760 // copies are generated, they are close enough to some use that the
761 // lower-latency form is preferable.
Hal Finkel27774d92014-03-13 07:58:58 +0000762 Opc = PPC::XXLOR;
Hal Finkel19be5062014-03-29 05:29:01 +0000763 else if (PPC::VSFRCRegClass.contains(DestReg, SrcReg))
764 Opc = PPC::XXLORf;
Jakob Stoklund Olesen0d611972010-07-11 07:31:00 +0000765 else if (PPC::CRBITRCRegClass.contains(DestReg, SrcReg))
766 Opc = PPC::CROR;
767 else
768 llvm_unreachable("Impossible reg-to-reg copy");
Owen Anderson7a73ae92007-12-31 06:32:00 +0000769
Evan Cheng6cc775f2011-06-28 19:10:37 +0000770 const MCInstrDesc &MCID = get(Opc);
771 if (MCID.getNumOperands() == 3)
772 BuildMI(MBB, I, DL, MCID, DestReg)
Jakob Stoklund Olesen0d611972010-07-11 07:31:00 +0000773 .addReg(SrcReg).addReg(SrcReg, getKillRegState(KillSrc));
774 else
Evan Cheng6cc775f2011-06-28 19:10:37 +0000775 BuildMI(MBB, I, DL, MCID, DestReg).addReg(SrcReg, getKillRegState(KillSrc));
Owen Anderson7a73ae92007-12-31 06:32:00 +0000776}
777
Hal Finkel8f6834d2011-12-05 17:55:17 +0000778// This function returns true if a CR spill is necessary and false otherwise.
Bill Wendlingc6c48fc2008-03-10 22:49:16 +0000779bool
Dan Gohman3b460302008-07-07 23:14:23 +0000780PPCInstrInfo::StoreRegToStackSlot(MachineFunction &MF,
781 unsigned SrcReg, bool isKill,
Bill Wendlingc6c48fc2008-03-10 22:49:16 +0000782 int FrameIdx,
783 const TargetRegisterClass *RC,
Hal Finkelfcc51d42013-03-17 04:43:44 +0000784 SmallVectorImpl<MachineInstr*> &NewMIs,
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000785 bool &NonRI, bool &SpillsVRS) const{
Hal Finkel37714b82013-03-27 21:21:15 +0000786 // Note: If additional store instructions are added here,
787 // update isStoreToStackSlot.
788
Chris Lattner6f306d72010-04-02 20:16:16 +0000789 DebugLoc DL;
Hal Finkel4e703bc2014-01-28 05:32:58 +0000790 if (PPC::GPRCRegClass.hasSubClassEq(RC) ||
791 PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) {
Hal Finkel794e05b2013-03-23 17:14:27 +0000792 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
793 .addReg(SrcReg,
794 getKillRegState(isKill)),
795 FrameIdx));
Hal Finkel4e703bc2014-01-28 05:32:58 +0000796 } else if (PPC::G8RCRegClass.hasSubClassEq(RC) ||
797 PPC::G8RC_NOX0RegClass.hasSubClassEq(RC)) {
Hal Finkel794e05b2013-03-23 17:14:27 +0000798 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STD))
799 .addReg(SrcReg,
800 getKillRegState(isKill)),
801 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000802 } else if (PPC::F8RCRegClass.hasSubClassEq(RC)) {
Dale Johannesen6b8c76a2009-02-12 23:08:38 +0000803 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFD))
Bill Wendlingf7b83c72009-05-13 21:33:08 +0000804 .addReg(SrcReg,
805 getKillRegState(isKill)),
806 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000807 } else if (PPC::F4RCRegClass.hasSubClassEq(RC)) {
Dale Johannesen6b8c76a2009-02-12 23:08:38 +0000808 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFS))
Bill Wendlingf7b83c72009-05-13 21:33:08 +0000809 .addReg(SrcReg,
810 getKillRegState(isKill)),
811 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000812 } else if (PPC::CRRCRegClass.hasSubClassEq(RC)) {
Hal Finkele154c8f2013-03-12 14:12:16 +0000813 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CR))
814 .addReg(SrcReg,
815 getKillRegState(isKill)),
816 FrameIdx));
817 return true;
Craig Topperabadc662012-04-20 06:31:50 +0000818 } else if (PPC::CRBITRCRegClass.hasSubClassEq(RC)) {
Hal Finkel940ab932014-02-28 00:27:01 +0000819 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CRBIT))
820 .addReg(SrcReg,
821 getKillRegState(isKill)),
822 FrameIdx));
823 return true;
Craig Topperabadc662012-04-20 06:31:50 +0000824 } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) {
Hal Finkelfcc51d42013-03-17 04:43:44 +0000825 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STVX))
826 .addReg(SrcReg,
827 getKillRegState(isKill)),
828 FrameIdx));
829 NonRI = true;
Hal Finkel27774d92014-03-13 07:58:58 +0000830 } else if (PPC::VSRCRegClass.hasSubClassEq(RC)) {
831 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STXVD2X))
832 .addReg(SrcReg,
833 getKillRegState(isKill)),
834 FrameIdx));
835 NonRI = true;
Hal Finkel19be5062014-03-29 05:29:01 +0000836 } else if (PPC::VSFRCRegClass.hasSubClassEq(RC)) {
837 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STXSDX))
838 .addReg(SrcReg,
839 getKillRegState(isKill)),
840 FrameIdx));
841 NonRI = true;
Hal Finkela1431df2013-03-21 19:03:21 +0000842 } else if (PPC::VRSAVERCRegClass.hasSubClassEq(RC)) {
Eric Christopher1dcea732014-06-12 21:48:52 +0000843 assert(Subtarget.isDarwin() &&
Hal Finkela7b06302013-03-27 00:02:20 +0000844 "VRSAVE only needs spill/restore on Darwin");
Hal Finkela1431df2013-03-21 19:03:21 +0000845 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_VRSAVE))
846 .addReg(SrcReg,
847 getKillRegState(isKill)),
848 FrameIdx));
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000849 SpillsVRS = true;
Owen Andersoneee14602008-01-01 21:11:32 +0000850 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000851 llvm_unreachable("Unknown regclass!");
Owen Andersoneee14602008-01-01 21:11:32 +0000852 }
Bill Wendling632ea652008-03-03 22:19:16 +0000853
854 return false;
Owen Andersoneee14602008-01-01 21:11:32 +0000855}
856
857void
858PPCInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
Bill Wendling632ea652008-03-03 22:19:16 +0000859 MachineBasicBlock::iterator MI,
860 unsigned SrcReg, bool isKill, int FrameIdx,
Evan Chengefb126a2010-05-06 19:06:44 +0000861 const TargetRegisterClass *RC,
862 const TargetRegisterInfo *TRI) const {
Dan Gohman3b460302008-07-07 23:14:23 +0000863 MachineFunction &MF = *MBB.getParent();
Owen Andersoneee14602008-01-01 21:11:32 +0000864 SmallVector<MachineInstr*, 4> NewMIs;
Bill Wendling632ea652008-03-03 22:19:16 +0000865
Hal Finkelbb420f12013-03-15 05:06:04 +0000866 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
867 FuncInfo->setHasSpills();
868
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000869 bool NonRI = false, SpillsVRS = false;
870 if (StoreRegToStackSlot(MF, SrcReg, isKill, FrameIdx, RC, NewMIs,
871 NonRI, SpillsVRS))
Bill Wendling632ea652008-03-03 22:19:16 +0000872 FuncInfo->setSpillsCR();
Bill Wendling632ea652008-03-03 22:19:16 +0000873
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000874 if (SpillsVRS)
875 FuncInfo->setSpillsVRSAVE();
876
Hal Finkelfcc51d42013-03-17 04:43:44 +0000877 if (NonRI)
878 FuncInfo->setHasNonRISpills();
879
Owen Andersoneee14602008-01-01 21:11:32 +0000880 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
881 MBB.insert(MI, NewMIs[i]);
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +0000882
883 const MachineFrameInfo &MFI = *MF.getFrameInfo();
884 MachineMemOperand *MMO =
Jay Foad465101b2011-11-15 07:34:52 +0000885 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx),
Chris Lattnere3d864b2010-09-21 04:39:43 +0000886 MachineMemOperand::MOStore,
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +0000887 MFI.getObjectSize(FrameIdx),
888 MFI.getObjectAlignment(FrameIdx));
889 NewMIs.back()->addMemOperand(MF, MMO);
Owen Andersoneee14602008-01-01 21:11:32 +0000890}
891
Hal Finkelbde7f8f2011-12-06 20:55:36 +0000892bool
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000893PPCInstrInfo::LoadRegFromStackSlot(MachineFunction &MF, DebugLoc DL,
Dan Gohman3b460302008-07-07 23:14:23 +0000894 unsigned DestReg, int FrameIdx,
Bill Wendlingc6c48fc2008-03-10 22:49:16 +0000895 const TargetRegisterClass *RC,
Hal Finkelfcc51d42013-03-17 04:43:44 +0000896 SmallVectorImpl<MachineInstr*> &NewMIs,
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000897 bool &NonRI, bool &SpillsVRS) const{
Hal Finkel37714b82013-03-27 21:21:15 +0000898 // Note: If additional load instructions are added here,
899 // update isLoadFromStackSlot.
900
Hal Finkel4e703bc2014-01-28 05:32:58 +0000901 if (PPC::GPRCRegClass.hasSubClassEq(RC) ||
902 PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) {
Hal Finkel5791f512013-03-27 19:10:40 +0000903 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ),
904 DestReg), FrameIdx));
Hal Finkel4e703bc2014-01-28 05:32:58 +0000905 } else if (PPC::G8RCRegClass.hasSubClassEq(RC) ||
906 PPC::G8RC_NOX0RegClass.hasSubClassEq(RC)) {
Hal Finkel5791f512013-03-27 19:10:40 +0000907 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LD), DestReg),
908 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000909 } else if (PPC::F8RCRegClass.hasSubClassEq(RC)) {
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000910 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFD), DestReg),
Owen Andersoneee14602008-01-01 21:11:32 +0000911 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000912 } else if (PPC::F4RCRegClass.hasSubClassEq(RC)) {
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000913 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFS), DestReg),
Owen Andersoneee14602008-01-01 21:11:32 +0000914 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000915 } else if (PPC::CRRCRegClass.hasSubClassEq(RC)) {
Hal Finkele154c8f2013-03-12 14:12:16 +0000916 NewMIs.push_back(addFrameReference(BuildMI(MF, DL,
917 get(PPC::RESTORE_CR), DestReg),
918 FrameIdx));
919 return true;
Craig Topperabadc662012-04-20 06:31:50 +0000920 } else if (PPC::CRBITRCRegClass.hasSubClassEq(RC)) {
Hal Finkel940ab932014-02-28 00:27:01 +0000921 NewMIs.push_back(addFrameReference(BuildMI(MF, DL,
922 get(PPC::RESTORE_CRBIT), DestReg),
923 FrameIdx));
924 return true;
Craig Topperabadc662012-04-20 06:31:50 +0000925 } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) {
Hal Finkelfcc51d42013-03-17 04:43:44 +0000926 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LVX), DestReg),
927 FrameIdx));
928 NonRI = true;
Hal Finkel27774d92014-03-13 07:58:58 +0000929 } else if (PPC::VSRCRegClass.hasSubClassEq(RC)) {
930 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LXVD2X), DestReg),
931 FrameIdx));
932 NonRI = true;
Hal Finkel19be5062014-03-29 05:29:01 +0000933 } else if (PPC::VSFRCRegClass.hasSubClassEq(RC)) {
934 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LXSDX), DestReg),
935 FrameIdx));
936 NonRI = true;
Hal Finkela1431df2013-03-21 19:03:21 +0000937 } else if (PPC::VRSAVERCRegClass.hasSubClassEq(RC)) {
Eric Christopher1dcea732014-06-12 21:48:52 +0000938 assert(Subtarget.isDarwin() &&
Hal Finkela7b06302013-03-27 00:02:20 +0000939 "VRSAVE only needs spill/restore on Darwin");
Hal Finkela1431df2013-03-21 19:03:21 +0000940 NewMIs.push_back(addFrameReference(BuildMI(MF, DL,
941 get(PPC::RESTORE_VRSAVE),
942 DestReg),
943 FrameIdx));
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000944 SpillsVRS = true;
Owen Andersoneee14602008-01-01 21:11:32 +0000945 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000946 llvm_unreachable("Unknown regclass!");
Owen Andersoneee14602008-01-01 21:11:32 +0000947 }
Hal Finkelbde7f8f2011-12-06 20:55:36 +0000948
949 return false;
Owen Andersoneee14602008-01-01 21:11:32 +0000950}
951
952void
953PPCInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
Bill Wendling632ea652008-03-03 22:19:16 +0000954 MachineBasicBlock::iterator MI,
955 unsigned DestReg, int FrameIdx,
Evan Chengefb126a2010-05-06 19:06:44 +0000956 const TargetRegisterClass *RC,
957 const TargetRegisterInfo *TRI) const {
Dan Gohman3b460302008-07-07 23:14:23 +0000958 MachineFunction &MF = *MBB.getParent();
Owen Andersoneee14602008-01-01 21:11:32 +0000959 SmallVector<MachineInstr*, 4> NewMIs;
Chris Lattner6f306d72010-04-02 20:16:16 +0000960 DebugLoc DL;
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000961 if (MI != MBB.end()) DL = MI->getDebugLoc();
Hal Finkelfcc51d42013-03-17 04:43:44 +0000962
963 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
964 FuncInfo->setHasSpills();
965
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000966 bool NonRI = false, SpillsVRS = false;
967 if (LoadRegFromStackSlot(MF, DL, DestReg, FrameIdx, RC, NewMIs,
968 NonRI, SpillsVRS))
Hal Finkelbde7f8f2011-12-06 20:55:36 +0000969 FuncInfo->setSpillsCR();
Hal Finkelfcc51d42013-03-17 04:43:44 +0000970
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000971 if (SpillsVRS)
972 FuncInfo->setSpillsVRSAVE();
973
Hal Finkelfcc51d42013-03-17 04:43:44 +0000974 if (NonRI)
975 FuncInfo->setHasNonRISpills();
976
Owen Andersoneee14602008-01-01 21:11:32 +0000977 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
978 MBB.insert(MI, NewMIs[i]);
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +0000979
980 const MachineFrameInfo &MFI = *MF.getFrameInfo();
981 MachineMemOperand *MMO =
Jay Foad465101b2011-11-15 07:34:52 +0000982 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx),
Chris Lattnere3d864b2010-09-21 04:39:43 +0000983 MachineMemOperand::MOLoad,
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +0000984 MFI.getObjectSize(FrameIdx),
985 MFI.getObjectAlignment(FrameIdx));
986 NewMIs.back()->addMemOperand(MF, MMO);
Owen Andersoneee14602008-01-01 21:11:32 +0000987}
988
Chris Lattnera47294ed2006-10-13 21:21:17 +0000989bool PPCInstrInfo::
Owen Anderson4f6bf042008-08-14 22:49:33 +0000990ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
Chris Lattner23f22de2006-10-21 06:03:11 +0000991 assert(Cond.size() == 2 && "Invalid PPC branch opcode!");
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000992 if (Cond[1].getReg() == PPC::CTR8 || Cond[1].getReg() == PPC::CTR)
993 Cond[0].setImm(Cond[0].getImm() == 0 ? 1 : 0);
994 else
995 // Leave the CR# the same, but invert the condition.
996 Cond[0].setImm(PPC::InvertPredicate((PPC::Predicate)Cond[0].getImm()));
Chris Lattner23f22de2006-10-21 06:03:11 +0000997 return false;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000998}
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +0000999
Hal Finkeld61d4f82013-04-06 19:30:30 +00001000bool PPCInstrInfo::FoldImmediate(MachineInstr *UseMI, MachineInstr *DefMI,
1001 unsigned Reg, MachineRegisterInfo *MRI) const {
1002 // For some instructions, it is legal to fold ZERO into the RA register field.
1003 // A zero immediate should always be loaded with a single li.
1004 unsigned DefOpc = DefMI->getOpcode();
1005 if (DefOpc != PPC::LI && DefOpc != PPC::LI8)
1006 return false;
1007 if (!DefMI->getOperand(1).isImm())
1008 return false;
1009 if (DefMI->getOperand(1).getImm() != 0)
1010 return false;
1011
1012 // Note that we cannot here invert the arguments of an isel in order to fold
1013 // a ZERO into what is presented as the second argument. All we have here
1014 // is the condition bit, and that might come from a CR-logical bit operation.
1015
1016 const MCInstrDesc &UseMCID = UseMI->getDesc();
1017
1018 // Only fold into real machine instructions.
1019 if (UseMCID.isPseudo())
1020 return false;
1021
1022 unsigned UseIdx;
1023 for (UseIdx = 0; UseIdx < UseMI->getNumOperands(); ++UseIdx)
1024 if (UseMI->getOperand(UseIdx).isReg() &&
1025 UseMI->getOperand(UseIdx).getReg() == Reg)
1026 break;
1027
1028 assert(UseIdx < UseMI->getNumOperands() && "Cannot find Reg in UseMI");
1029 assert(UseIdx < UseMCID.getNumOperands() && "No operand description for Reg");
1030
1031 const MCOperandInfo *UseInfo = &UseMCID.OpInfo[UseIdx];
1032
1033 // We can fold the zero if this register requires a GPRC_NOR0/G8RC_NOX0
1034 // register (which might also be specified as a pointer class kind).
1035 if (UseInfo->isLookupPtrRegClass()) {
1036 if (UseInfo->RegClass /* Kind */ != 1)
1037 return false;
1038 } else {
1039 if (UseInfo->RegClass != PPC::GPRC_NOR0RegClassID &&
1040 UseInfo->RegClass != PPC::G8RC_NOX0RegClassID)
1041 return false;
1042 }
1043
1044 // Make sure this is not tied to an output register (or otherwise
1045 // constrained). This is true for ST?UX registers, for example, which
1046 // are tied to their output registers.
1047 if (UseInfo->Constraints != 0)
1048 return false;
1049
1050 unsigned ZeroReg;
1051 if (UseInfo->isLookupPtrRegClass()) {
Eric Christopher1dcea732014-06-12 21:48:52 +00001052 bool isPPC64 = Subtarget.isPPC64();
Hal Finkeld61d4f82013-04-06 19:30:30 +00001053 ZeroReg = isPPC64 ? PPC::ZERO8 : PPC::ZERO;
1054 } else {
1055 ZeroReg = UseInfo->RegClass == PPC::G8RC_NOX0RegClassID ?
1056 PPC::ZERO8 : PPC::ZERO;
1057 }
1058
1059 bool DeleteDef = MRI->hasOneNonDBGUse(Reg);
1060 UseMI->getOperand(UseIdx).setReg(ZeroReg);
1061
1062 if (DeleteDef)
1063 DefMI->eraseFromParent();
1064
1065 return true;
1066}
1067
Hal Finkel30ae2292013-04-10 18:30:16 +00001068static bool MBBDefinesCTR(MachineBasicBlock &MBB) {
1069 for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end();
1070 I != IE; ++I)
1071 if (I->definesRegister(PPC::CTR) || I->definesRegister(PPC::CTR8))
1072 return true;
1073 return false;
1074}
1075
1076// We should make sure that, if we're going to predicate both sides of a
1077// condition (a diamond), that both sides don't define the counter register. We
1078// can predicate counter-decrement-based branches, but while that predicates
1079// the branching, it does not predicate the counter decrement. If we tried to
1080// merge the triangle into one predicated block, we'd decrement the counter
1081// twice.
1082bool PPCInstrInfo::isProfitableToIfCvt(MachineBasicBlock &TMBB,
1083 unsigned NumT, unsigned ExtraT,
1084 MachineBasicBlock &FMBB,
1085 unsigned NumF, unsigned ExtraF,
1086 const BranchProbability &Probability) const {
1087 return !(MBBDefinesCTR(TMBB) && MBBDefinesCTR(FMBB));
1088}
1089
1090
Hal Finkel5711eca2013-04-09 22:58:37 +00001091bool PPCInstrInfo::isPredicated(const MachineInstr *MI) const {
Hal Finkelf29285a2013-04-11 01:23:34 +00001092 // The predicated branches are identified by their type, not really by the
1093 // explicit presence of a predicate. Furthermore, some of them can be
1094 // predicated more than once. Because if conversion won't try to predicate
1095 // any instruction which already claims to be predicated (by returning true
1096 // here), always return false. In doing so, we let isPredicable() be the
1097 // final word on whether not the instruction can be (further) predicated.
1098
1099 return false;
Hal Finkel5711eca2013-04-09 22:58:37 +00001100}
1101
1102bool PPCInstrInfo::isUnpredicatedTerminator(const MachineInstr *MI) const {
1103 if (!MI->isTerminator())
1104 return false;
1105
1106 // Conditional branch is a special case.
1107 if (MI->isBranch() && !MI->isBarrier())
1108 return true;
1109
1110 return !isPredicated(MI);
1111}
1112
1113bool PPCInstrInfo::PredicateInstruction(
1114 MachineInstr *MI,
1115 const SmallVectorImpl<MachineOperand> &Pred) const {
1116 unsigned OpC = MI->getOpcode();
Hal Finkelf4a22c02015-01-13 17:47:54 +00001117 if (OpC == PPC::BLR || OpC == PPC::BLR8) {
Hal Finkel5711eca2013-04-09 22:58:37 +00001118 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) {
Eric Christopher1dcea732014-06-12 21:48:52 +00001119 bool isPPC64 = Subtarget.isPPC64();
Hal Finkel5711eca2013-04-09 22:58:37 +00001120 MI->setDesc(get(Pred[0].getImm() ?
1121 (isPPC64 ? PPC::BDNZLR8 : PPC::BDNZLR) :
1122 (isPPC64 ? PPC::BDZLR8 : PPC::BDZLR)));
Hal Finkel940ab932014-02-28 00:27:01 +00001123 } else if (Pred[0].getImm() == PPC::PRED_BIT_SET) {
Hal Finkel5711eca2013-04-09 22:58:37 +00001124 MI->setDesc(get(PPC::BCLR));
1125 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
Hal Finkel940ab932014-02-28 00:27:01 +00001126 .addReg(Pred[1].getReg());
1127 } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) {
1128 MI->setDesc(get(PPC::BCLRn));
1129 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1130 .addReg(Pred[1].getReg());
1131 } else {
1132 MI->setDesc(get(PPC::BCCLR));
1133 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
Hal Finkel5711eca2013-04-09 22:58:37 +00001134 .addImm(Pred[0].getImm())
1135 .addReg(Pred[1].getReg());
1136 }
1137
1138 return true;
1139 } else if (OpC == PPC::B) {
1140 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) {
Eric Christopher1dcea732014-06-12 21:48:52 +00001141 bool isPPC64 = Subtarget.isPPC64();
Hal Finkel5711eca2013-04-09 22:58:37 +00001142 MI->setDesc(get(Pred[0].getImm() ?
1143 (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
1144 (isPPC64 ? PPC::BDZ8 : PPC::BDZ)));
Hal Finkel940ab932014-02-28 00:27:01 +00001145 } else if (Pred[0].getImm() == PPC::PRED_BIT_SET) {
1146 MachineBasicBlock *MBB = MI->getOperand(0).getMBB();
1147 MI->RemoveOperand(0);
1148
1149 MI->setDesc(get(PPC::BC));
1150 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1151 .addReg(Pred[1].getReg())
1152 .addMBB(MBB);
1153 } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) {
1154 MachineBasicBlock *MBB = MI->getOperand(0).getMBB();
1155 MI->RemoveOperand(0);
1156
1157 MI->setDesc(get(PPC::BCn));
1158 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1159 .addReg(Pred[1].getReg())
1160 .addMBB(MBB);
Hal Finkel5711eca2013-04-09 22:58:37 +00001161 } else {
1162 MachineBasicBlock *MBB = MI->getOperand(0).getMBB();
1163 MI->RemoveOperand(0);
1164
1165 MI->setDesc(get(PPC::BCC));
1166 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1167 .addImm(Pred[0].getImm())
1168 .addReg(Pred[1].getReg())
1169 .addMBB(MBB);
1170 }
1171
1172 return true;
Hal Finkel500b0042013-04-10 06:42:34 +00001173 } else if (OpC == PPC::BCTR || OpC == PPC::BCTR8 ||
1174 OpC == PPC::BCTRL || OpC == PPC::BCTRL8) {
1175 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR)
1176 llvm_unreachable("Cannot predicate bctr[l] on the ctr register");
1177
1178 bool setLR = OpC == PPC::BCTRL || OpC == PPC::BCTRL8;
Eric Christopher1dcea732014-06-12 21:48:52 +00001179 bool isPPC64 = Subtarget.isPPC64();
Hal Finkel940ab932014-02-28 00:27:01 +00001180
1181 if (Pred[0].getImm() == PPC::PRED_BIT_SET) {
1182 MI->setDesc(get(isPPC64 ? (setLR ? PPC::BCCTRL8 : PPC::BCCTR8) :
1183 (setLR ? PPC::BCCTRL : PPC::BCCTR)));
1184 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1185 .addReg(Pred[1].getReg());
1186 return true;
1187 } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) {
1188 MI->setDesc(get(isPPC64 ? (setLR ? PPC::BCCTRL8n : PPC::BCCTR8n) :
1189 (setLR ? PPC::BCCTRLn : PPC::BCCTRn)));
1190 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1191 .addReg(Pred[1].getReg());
1192 return true;
1193 }
1194
1195 MI->setDesc(get(isPPC64 ? (setLR ? PPC::BCCCTRL8 : PPC::BCCCTR8) :
1196 (setLR ? PPC::BCCCTRL : PPC::BCCCTR)));
Hal Finkel500b0042013-04-10 06:42:34 +00001197 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1198 .addImm(Pred[0].getImm())
1199 .addReg(Pred[1].getReg());
1200 return true;
Hal Finkel5711eca2013-04-09 22:58:37 +00001201 }
1202
1203 return false;
1204}
1205
1206bool PPCInstrInfo::SubsumesPredicate(
1207 const SmallVectorImpl<MachineOperand> &Pred1,
1208 const SmallVectorImpl<MachineOperand> &Pred2) const {
1209 assert(Pred1.size() == 2 && "Invalid PPC first predicate");
1210 assert(Pred2.size() == 2 && "Invalid PPC second predicate");
1211
1212 if (Pred1[1].getReg() == PPC::CTR8 || Pred1[1].getReg() == PPC::CTR)
1213 return false;
1214 if (Pred2[1].getReg() == PPC::CTR8 || Pred2[1].getReg() == PPC::CTR)
1215 return false;
1216
Hal Finkel94a6f382013-12-11 23:12:25 +00001217 // P1 can only subsume P2 if they test the same condition register.
1218 if (Pred1[1].getReg() != Pred2[1].getReg())
1219 return false;
1220
Hal Finkel5711eca2013-04-09 22:58:37 +00001221 PPC::Predicate P1 = (PPC::Predicate) Pred1[0].getImm();
1222 PPC::Predicate P2 = (PPC::Predicate) Pred2[0].getImm();
1223
1224 if (P1 == P2)
1225 return true;
1226
1227 // Does P1 subsume P2, e.g. GE subsumes GT.
1228 if (P1 == PPC::PRED_LE &&
1229 (P2 == PPC::PRED_LT || P2 == PPC::PRED_EQ))
1230 return true;
1231 if (P1 == PPC::PRED_GE &&
1232 (P2 == PPC::PRED_GT || P2 == PPC::PRED_EQ))
1233 return true;
1234
1235 return false;
1236}
1237
1238bool PPCInstrInfo::DefinesPredicate(MachineInstr *MI,
1239 std::vector<MachineOperand> &Pred) const {
1240 // Note: At the present time, the contents of Pred from this function is
1241 // unused by IfConversion. This implementation follows ARM by pushing the
1242 // CR-defining operand. Because the 'DZ' and 'DNZ' count as types of
1243 // predicate, instructions defining CTR or CTR8 are also included as
1244 // predicate-defining instructions.
1245
1246 const TargetRegisterClass *RCs[] =
1247 { &PPC::CRRCRegClass, &PPC::CRBITRCRegClass,
1248 &PPC::CTRRCRegClass, &PPC::CTRRC8RegClass };
1249
1250 bool Found = false;
1251 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1252 const MachineOperand &MO = MI->getOperand(i);
Hal Finkelaf822012013-04-10 07:17:47 +00001253 for (unsigned c = 0; c < array_lengthof(RCs) && !Found; ++c) {
Hal Finkel5711eca2013-04-09 22:58:37 +00001254 const TargetRegisterClass *RC = RCs[c];
Hal Finkelaf822012013-04-10 07:17:47 +00001255 if (MO.isReg()) {
1256 if (MO.isDef() && RC->contains(MO.getReg())) {
Hal Finkel5711eca2013-04-09 22:58:37 +00001257 Pred.push_back(MO);
1258 Found = true;
1259 }
Hal Finkelaf822012013-04-10 07:17:47 +00001260 } else if (MO.isRegMask()) {
1261 for (TargetRegisterClass::iterator I = RC->begin(),
1262 IE = RC->end(); I != IE; ++I)
1263 if (MO.clobbersPhysReg(*I)) {
1264 Pred.push_back(MO);
1265 Found = true;
1266 }
Hal Finkel5711eca2013-04-09 22:58:37 +00001267 }
1268 }
1269 }
1270
1271 return Found;
1272}
1273
1274bool PPCInstrInfo::isPredicable(MachineInstr *MI) const {
1275 unsigned OpC = MI->getOpcode();
1276 switch (OpC) {
1277 default:
1278 return false;
1279 case PPC::B:
1280 case PPC::BLR:
Hal Finkelf4a22c02015-01-13 17:47:54 +00001281 case PPC::BLR8:
Hal Finkel500b0042013-04-10 06:42:34 +00001282 case PPC::BCTR:
1283 case PPC::BCTR8:
1284 case PPC::BCTRL:
1285 case PPC::BCTRL8:
Hal Finkel5711eca2013-04-09 22:58:37 +00001286 return true;
1287 }
1288}
1289
Hal Finkel82656cb2013-04-18 22:15:08 +00001290bool PPCInstrInfo::analyzeCompare(const MachineInstr *MI,
1291 unsigned &SrcReg, unsigned &SrcReg2,
1292 int &Mask, int &Value) const {
1293 unsigned Opc = MI->getOpcode();
1294
1295 switch (Opc) {
1296 default: return false;
1297 case PPC::CMPWI:
1298 case PPC::CMPLWI:
1299 case PPC::CMPDI:
1300 case PPC::CMPLDI:
1301 SrcReg = MI->getOperand(1).getReg();
1302 SrcReg2 = 0;
1303 Value = MI->getOperand(2).getImm();
1304 Mask = 0xFFFF;
1305 return true;
1306 case PPC::CMPW:
1307 case PPC::CMPLW:
1308 case PPC::CMPD:
1309 case PPC::CMPLD:
1310 case PPC::FCMPUS:
1311 case PPC::FCMPUD:
1312 SrcReg = MI->getOperand(1).getReg();
1313 SrcReg2 = MI->getOperand(2).getReg();
1314 return true;
1315 }
1316}
Hal Finkele6322392013-04-19 22:08:38 +00001317
Hal Finkel82656cb2013-04-18 22:15:08 +00001318bool PPCInstrInfo::optimizeCompareInstr(MachineInstr *CmpInstr,
1319 unsigned SrcReg, unsigned SrcReg2,
1320 int Mask, int Value,
1321 const MachineRegisterInfo *MRI) const {
Hal Finkelb12da6b2013-04-18 22:54:25 +00001322 if (DisableCmpOpt)
1323 return false;
1324
Hal Finkel82656cb2013-04-18 22:15:08 +00001325 int OpC = CmpInstr->getOpcode();
1326 unsigned CRReg = CmpInstr->getOperand(0).getReg();
Hal Finkel08e53ee2013-05-08 12:16:14 +00001327
1328 // FP record forms set CR1 based on the execption status bits, not a
1329 // comparison with zero.
1330 if (OpC == PPC::FCMPUS || OpC == PPC::FCMPUD)
1331 return false;
Hal Finkel82656cb2013-04-18 22:15:08 +00001332
1333 // The record forms set the condition register based on a signed comparison
1334 // with zero (so says the ISA manual). This is not as straightforward as it
1335 // seems, however, because this is always a 64-bit comparison on PPC64, even
1336 // for instructions that are 32-bit in nature (like slw for example).
1337 // So, on PPC32, for unsigned comparisons, we can use the record forms only
1338 // for equality checks (as those don't depend on the sign). On PPC64,
1339 // we are restricted to equality for unsigned 64-bit comparisons and for
1340 // signed 32-bit comparisons the applicability is more restricted.
Eric Christopher1dcea732014-06-12 21:48:52 +00001341 bool isPPC64 = Subtarget.isPPC64();
Hal Finkel82656cb2013-04-18 22:15:08 +00001342 bool is32BitSignedCompare = OpC == PPC::CMPWI || OpC == PPC::CMPW;
1343 bool is32BitUnsignedCompare = OpC == PPC::CMPLWI || OpC == PPC::CMPLW;
1344 bool is64BitUnsignedCompare = OpC == PPC::CMPLDI || OpC == PPC::CMPLD;
1345
1346 // Get the unique definition of SrcReg.
1347 MachineInstr *MI = MRI->getUniqueVRegDef(SrcReg);
1348 if (!MI) return false;
1349 int MIOpC = MI->getOpcode();
1350
1351 bool equalityOnly = false;
1352 bool noSub = false;
1353 if (isPPC64) {
1354 if (is32BitSignedCompare) {
1355 // We can perform this optimization only if MI is sign-extending.
1356 if (MIOpC == PPC::SRAW || MIOpC == PPC::SRAWo ||
1357 MIOpC == PPC::SRAWI || MIOpC == PPC::SRAWIo ||
1358 MIOpC == PPC::EXTSB || MIOpC == PPC::EXTSBo ||
1359 MIOpC == PPC::EXTSH || MIOpC == PPC::EXTSHo ||
1360 MIOpC == PPC::EXTSW || MIOpC == PPC::EXTSWo) {
1361 noSub = true;
1362 } else
1363 return false;
1364 } else if (is32BitUnsignedCompare) {
1365 // We can perform this optimization, equality only, if MI is
1366 // zero-extending.
1367 if (MIOpC == PPC::CNTLZW || MIOpC == PPC::CNTLZWo ||
1368 MIOpC == PPC::SLW || MIOpC == PPC::SLWo ||
1369 MIOpC == PPC::SRW || MIOpC == PPC::SRWo) {
1370 noSub = true;
1371 equalityOnly = true;
1372 } else
1373 return false;
Hal Finkel08e53ee2013-05-08 12:16:14 +00001374 } else
Hal Finkel82656cb2013-04-18 22:15:08 +00001375 equalityOnly = is64BitUnsignedCompare;
Hal Finkel08e53ee2013-05-08 12:16:14 +00001376 } else
Hal Finkel82656cb2013-04-18 22:15:08 +00001377 equalityOnly = is32BitUnsignedCompare;
1378
1379 if (equalityOnly) {
1380 // We need to check the uses of the condition register in order to reject
1381 // non-equality comparisons.
Owen Anderson16c6bf42014-03-13 23:12:04 +00001382 for (MachineRegisterInfo::use_instr_iterator I =MRI->use_instr_begin(CRReg),
1383 IE = MRI->use_instr_end(); I != IE; ++I) {
Hal Finkel82656cb2013-04-18 22:15:08 +00001384 MachineInstr *UseMI = &*I;
1385 if (UseMI->getOpcode() == PPC::BCC) {
1386 unsigned Pred = UseMI->getOperand(0).getImm();
Hal Finkelc3632452013-05-07 17:49:55 +00001387 if (Pred != PPC::PRED_EQ && Pred != PPC::PRED_NE)
1388 return false;
Hal Finkel82656cb2013-04-18 22:15:08 +00001389 } else if (UseMI->getOpcode() == PPC::ISEL ||
1390 UseMI->getOpcode() == PPC::ISEL8) {
1391 unsigned SubIdx = UseMI->getOperand(3).getSubReg();
Hal Finkelc3632452013-05-07 17:49:55 +00001392 if (SubIdx != PPC::sub_eq)
1393 return false;
Hal Finkel82656cb2013-04-18 22:15:08 +00001394 } else
1395 return false;
1396 }
1397 }
1398
Hal Finkelc3632452013-05-07 17:49:55 +00001399 MachineBasicBlock::iterator I = CmpInstr;
Hal Finkel82656cb2013-04-18 22:15:08 +00001400
1401 // Scan forward to find the first use of the compare.
1402 for (MachineBasicBlock::iterator EL = CmpInstr->getParent()->end();
1403 I != EL; ++I) {
1404 bool FoundUse = false;
Owen Anderson16c6bf42014-03-13 23:12:04 +00001405 for (MachineRegisterInfo::use_instr_iterator J =MRI->use_instr_begin(CRReg),
1406 JE = MRI->use_instr_end(); J != JE; ++J)
Hal Finkel82656cb2013-04-18 22:15:08 +00001407 if (&*J == &*I) {
1408 FoundUse = true;
1409 break;
1410 }
1411
1412 if (FoundUse)
1413 break;
1414 }
1415
Hal Finkel82656cb2013-04-18 22:15:08 +00001416 // There are two possible candidates which can be changed to set CR[01].
1417 // One is MI, the other is a SUB instruction.
1418 // For CMPrr(r1,r2), we are looking for SUB(r1,r2) or SUB(r2,r1).
Craig Topper062a2ba2014-04-25 05:30:21 +00001419 MachineInstr *Sub = nullptr;
Hal Finkel82656cb2013-04-18 22:15:08 +00001420 if (SrcReg2 != 0)
1421 // MI is not a candidate for CMPrr.
Craig Topper062a2ba2014-04-25 05:30:21 +00001422 MI = nullptr;
Hal Finkel82656cb2013-04-18 22:15:08 +00001423 // FIXME: Conservatively refuse to convert an instruction which isn't in the
1424 // same BB as the comparison. This is to allow the check below to avoid calls
1425 // (and other explicit clobbers); instead we should really check for these
1426 // more explicitly (in at least a few predecessors).
1427 else if (MI->getParent() != CmpInstr->getParent() || Value != 0) {
1428 // PPC does not have a record-form SUBri.
1429 return false;
1430 }
1431
1432 // Search for Sub.
1433 const TargetRegisterInfo *TRI = &getRegisterInfo();
1434 --I;
Hal Finkelc3632452013-05-07 17:49:55 +00001435
1436 // Get ready to iterate backward from CmpInstr.
1437 MachineBasicBlock::iterator E = MI,
1438 B = CmpInstr->getParent()->begin();
1439
Hal Finkel82656cb2013-04-18 22:15:08 +00001440 for (; I != E && !noSub; --I) {
1441 const MachineInstr &Instr = *I;
1442 unsigned IOpC = Instr.getOpcode();
1443
1444 if (&*I != CmpInstr && (
Hal Finkel08e53ee2013-05-08 12:16:14 +00001445 Instr.modifiesRegister(PPC::CR0, TRI) ||
1446 Instr.readsRegister(PPC::CR0, TRI)))
Hal Finkel82656cb2013-04-18 22:15:08 +00001447 // This instruction modifies or uses the record condition register after
1448 // the one we want to change. While we could do this transformation, it
1449 // would likely not be profitable. This transformation removes one
1450 // instruction, and so even forcing RA to generate one move probably
1451 // makes it unprofitable.
1452 return false;
1453
1454 // Check whether CmpInstr can be made redundant by the current instruction.
1455 if ((OpC == PPC::CMPW || OpC == PPC::CMPLW ||
1456 OpC == PPC::CMPD || OpC == PPC::CMPLD) &&
1457 (IOpC == PPC::SUBF || IOpC == PPC::SUBF8) &&
1458 ((Instr.getOperand(1).getReg() == SrcReg &&
1459 Instr.getOperand(2).getReg() == SrcReg2) ||
1460 (Instr.getOperand(1).getReg() == SrcReg2 &&
1461 Instr.getOperand(2).getReg() == SrcReg))) {
1462 Sub = &*I;
1463 break;
1464 }
1465
Hal Finkel82656cb2013-04-18 22:15:08 +00001466 if (I == B)
1467 // The 'and' is below the comparison instruction.
1468 return false;
1469 }
1470
1471 // Return false if no candidates exist.
1472 if (!MI && !Sub)
1473 return false;
1474
1475 // The single candidate is called MI.
1476 if (!MI) MI = Sub;
1477
1478 int NewOpC = -1;
1479 MIOpC = MI->getOpcode();
1480 if (MIOpC == PPC::ANDIo || MIOpC == PPC::ANDIo8)
1481 NewOpC = MIOpC;
1482 else {
1483 NewOpC = PPC::getRecordFormOpcode(MIOpC);
1484 if (NewOpC == -1 && PPC::getNonRecordFormOpcode(MIOpC) != -1)
1485 NewOpC = MIOpC;
1486 }
1487
1488 // FIXME: On the non-embedded POWER architectures, only some of the record
1489 // forms are fast, and we should use only the fast ones.
1490
1491 // The defining instruction has a record form (or is already a record
1492 // form). It is possible, however, that we'll need to reverse the condition
1493 // code of the users.
1494 if (NewOpC == -1)
1495 return false;
1496
Hal Finkele6322392013-04-19 22:08:38 +00001497 SmallVector<std::pair<MachineOperand*, PPC::Predicate>, 4> PredsToUpdate;
1498 SmallVector<std::pair<MachineOperand*, unsigned>, 4> SubRegsToUpdate;
Hal Finkel82656cb2013-04-18 22:15:08 +00001499
1500 // If we have SUB(r1, r2) and CMP(r2, r1), the condition code based on CMP
1501 // needs to be updated to be based on SUB. Push the condition code
1502 // operands to OperandsToUpdate. If it is safe to remove CmpInstr, the
1503 // condition code of these operands will be modified.
1504 bool ShouldSwap = false;
1505 if (Sub) {
1506 ShouldSwap = SrcReg2 != 0 && Sub->getOperand(1).getReg() == SrcReg2 &&
1507 Sub->getOperand(2).getReg() == SrcReg;
1508
1509 // The operands to subf are the opposite of sub, so only in the fixed-point
1510 // case, invert the order.
Hal Finkel08e53ee2013-05-08 12:16:14 +00001511 ShouldSwap = !ShouldSwap;
Hal Finkel82656cb2013-04-18 22:15:08 +00001512 }
1513
1514 if (ShouldSwap)
Owen Anderson16c6bf42014-03-13 23:12:04 +00001515 for (MachineRegisterInfo::use_instr_iterator
1516 I = MRI->use_instr_begin(CRReg), IE = MRI->use_instr_end();
1517 I != IE; ++I) {
Hal Finkel82656cb2013-04-18 22:15:08 +00001518 MachineInstr *UseMI = &*I;
1519 if (UseMI->getOpcode() == PPC::BCC) {
1520 PPC::Predicate Pred = (PPC::Predicate) UseMI->getOperand(0).getImm();
Hal Finkele6322392013-04-19 22:08:38 +00001521 assert((!equalityOnly ||
1522 Pred == PPC::PRED_EQ || Pred == PPC::PRED_NE) &&
1523 "Invalid predicate for equality-only optimization");
Owen Anderson16c6bf42014-03-13 23:12:04 +00001524 PredsToUpdate.push_back(std::make_pair(&(UseMI->getOperand(0)),
Hal Finkel0f64e212013-04-20 05:16:26 +00001525 PPC::getSwappedPredicate(Pred)));
Hal Finkel82656cb2013-04-18 22:15:08 +00001526 } else if (UseMI->getOpcode() == PPC::ISEL ||
1527 UseMI->getOpcode() == PPC::ISEL8) {
Hal Finkele6322392013-04-19 22:08:38 +00001528 unsigned NewSubReg = UseMI->getOperand(3).getSubReg();
1529 assert((!equalityOnly || NewSubReg == PPC::sub_eq) &&
1530 "Invalid CR bit for equality-only optimization");
1531
1532 if (NewSubReg == PPC::sub_lt)
1533 NewSubReg = PPC::sub_gt;
1534 else if (NewSubReg == PPC::sub_gt)
1535 NewSubReg = PPC::sub_lt;
1536
Owen Anderson16c6bf42014-03-13 23:12:04 +00001537 SubRegsToUpdate.push_back(std::make_pair(&(UseMI->getOperand(3)),
Hal Finkele6322392013-04-19 22:08:38 +00001538 NewSubReg));
Hal Finkel82656cb2013-04-18 22:15:08 +00001539 } else // We need to abort on a user we don't understand.
1540 return false;
1541 }
1542
1543 // Create a new virtual register to hold the value of the CR set by the
1544 // record-form instruction. If the instruction was not previously in
1545 // record form, then set the kill flag on the CR.
1546 CmpInstr->eraseFromParent();
1547
1548 MachineBasicBlock::iterator MII = MI;
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001549 BuildMI(*MI->getParent(), std::next(MII), MI->getDebugLoc(),
Hal Finkel82656cb2013-04-18 22:15:08 +00001550 get(TargetOpcode::COPY), CRReg)
Hal Finkel08e53ee2013-05-08 12:16:14 +00001551 .addReg(PPC::CR0, MIOpC != NewOpC ? RegState::Kill : 0);
Hal Finkel82656cb2013-04-18 22:15:08 +00001552
1553 if (MIOpC != NewOpC) {
1554 // We need to be careful here: we're replacing one instruction with
1555 // another, and we need to make sure that we get all of the right
1556 // implicit uses and defs. On the other hand, the caller may be holding
1557 // an iterator to this instruction, and so we can't delete it (this is
1558 // specifically the case if this is the instruction directly after the
1559 // compare).
1560
1561 const MCInstrDesc &NewDesc = get(NewOpC);
1562 MI->setDesc(NewDesc);
1563
1564 if (NewDesc.ImplicitDefs)
1565 for (const uint16_t *ImpDefs = NewDesc.getImplicitDefs();
1566 *ImpDefs; ++ImpDefs)
1567 if (!MI->definesRegister(*ImpDefs))
1568 MI->addOperand(*MI->getParent()->getParent(),
1569 MachineOperand::CreateReg(*ImpDefs, true, true));
1570 if (NewDesc.ImplicitUses)
1571 for (const uint16_t *ImpUses = NewDesc.getImplicitUses();
1572 *ImpUses; ++ImpUses)
1573 if (!MI->readsRegister(*ImpUses))
1574 MI->addOperand(*MI->getParent()->getParent(),
1575 MachineOperand::CreateReg(*ImpUses, false, true));
1576 }
1577
1578 // Modify the condition code of operands in OperandsToUpdate.
1579 // Since we have SUB(r1, r2) and CMP(r2, r1), the condition code needs to
1580 // be changed from r2 > r1 to r1 < r2, from r2 < r1 to r1 > r2, etc.
Hal Finkele6322392013-04-19 22:08:38 +00001581 for (unsigned i = 0, e = PredsToUpdate.size(); i < e; i++)
1582 PredsToUpdate[i].first->setImm(PredsToUpdate[i].second);
Hal Finkel82656cb2013-04-18 22:15:08 +00001583
Hal Finkele6322392013-04-19 22:08:38 +00001584 for (unsigned i = 0, e = SubRegsToUpdate.size(); i < e; i++)
1585 SubRegsToUpdate[i].first->setSubReg(SubRegsToUpdate[i].second);
Hal Finkel82656cb2013-04-18 22:15:08 +00001586
1587 return true;
1588}
1589
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +00001590/// GetInstSize - Return the number of bytes of code the specified
1591/// instruction may be. This returns the maximum number of bytes.
1592///
1593unsigned PPCInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
Hal Finkela7bbaf62014-02-02 06:12:27 +00001594 unsigned Opcode = MI->getOpcode();
1595
1596 if (Opcode == PPC::INLINEASM) {
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +00001597 const MachineFunction *MF = MI->getParent()->getParent();
1598 const char *AsmStr = MI->getOperand(0).getSymbolName();
Chris Lattner7b26fce2009-08-22 20:48:53 +00001599 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
Hal Finkel934361a2015-01-14 01:07:51 +00001600 } else if (Opcode == TargetOpcode::STACKMAP) {
1601 return MI->getOperand(1).getImm();
1602 } else if (Opcode == TargetOpcode::PATCHPOINT) {
1603 PatchPointOpers Opers(MI);
1604 return Opers.getMetaOper(PatchPointOpers::NBytesPos).getImm();
Hal Finkela7bbaf62014-02-02 06:12:27 +00001605 } else {
1606 const MCInstrDesc &Desc = get(Opcode);
1607 return Desc.getSize();
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +00001608 }
1609}
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001610
Hal Finkel174e5902014-03-25 23:29:21 +00001611#undef DEBUG_TYPE
1612#define DEBUG_TYPE "ppc-vsx-fma-mutate"
1613
1614namespace {
1615 // PPCVSXFMAMutate pass - For copies between VSX registers and non-VSX registers
1616 // (Altivec and scalar floating-point registers), we need to transform the
1617 // copies into subregister copies with other restrictions.
1618 struct PPCVSXFMAMutate : public MachineFunctionPass {
1619 static char ID;
1620 PPCVSXFMAMutate() : MachineFunctionPass(ID) {
1621 initializePPCVSXFMAMutatePass(*PassRegistry::getPassRegistry());
1622 }
1623
1624 LiveIntervals *LIS;
1625
1626 const PPCTargetMachine *TM;
1627 const PPCInstrInfo *TII;
1628
1629protected:
1630 bool processBlock(MachineBasicBlock &MBB) {
1631 bool Changed = false;
1632
1633 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
Bill Schmidta087d742014-10-17 21:02:44 +00001634 const TargetRegisterInfo *TRI = &TII->getRegisterInfo();
Hal Finkel174e5902014-03-25 23:29:21 +00001635 for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end();
1636 I != IE; ++I) {
1637 MachineInstr *MI = I;
1638
1639 // The default (A-type) VSX FMA form kills the addend (it is taken from
1640 // the target register, which is then updated to reflect the result of
1641 // the FMA). If the instruction, however, kills one of the registers
1642 // used for the product, then we can use the M-form instruction (which
1643 // will take that value from the to-be-defined register).
1644
1645 int AltOpc = PPC::getAltVSXFMAOpcode(MI->getOpcode());
1646 if (AltOpc == -1)
1647 continue;
1648
1649 // This pass is run after register coalescing, and so we're looking for
1650 // a situation like this:
1651 // ...
1652 // %vreg5<def> = COPY %vreg9; VSLRC:%vreg5,%vreg9
1653 // %vreg5<def,tied1> = XSMADDADP %vreg5<tied0>, %vreg17, %vreg16,
1654 // %RM<imp-use>; VSLRC:%vreg5,%vreg17,%vreg16
1655 // ...
1656 // %vreg9<def,tied1> = XSMADDADP %vreg9<tied0>, %vreg17, %vreg19,
1657 // %RM<imp-use>; VSLRC:%vreg9,%vreg17,%vreg19
1658 // ...
1659 // Where we can eliminate the copy by changing from the A-type to the
1660 // M-type instruction. Specifically, for this example, this means:
1661 // %vreg5<def,tied1> = XSMADDADP %vreg5<tied0>, %vreg17, %vreg16,
1662 // %RM<imp-use>; VSLRC:%vreg5,%vreg17,%vreg16
1663 // is replaced by:
1664 // %vreg16<def,tied1> = XSMADDMDP %vreg16<tied0>, %vreg18, %vreg9,
1665 // %RM<imp-use>; VSLRC:%vreg16,%vreg18,%vreg9
1666 // and we remove: %vreg5<def> = COPY %vreg9; VSLRC:%vreg5,%vreg9
1667
1668 SlotIndex FMAIdx = LIS->getInstructionIndex(MI);
1669
1670 VNInfo *AddendValNo =
1671 LIS->getInterval(MI->getOperand(1).getReg()).Query(FMAIdx).valueIn();
1672 MachineInstr *AddendMI = LIS->getInstructionFromIndex(AddendValNo->def);
1673
1674 // The addend and this instruction must be in the same block.
1675
Hal Finkel19be5062014-03-29 05:29:01 +00001676 if (!AddendMI || AddendMI->getParent() != MI->getParent())
Hal Finkel174e5902014-03-25 23:29:21 +00001677 continue;
1678
1679 // The addend must be a full copy within the same register class.
1680
1681 if (!AddendMI->isFullCopy())
1682 continue;
1683
Hal Finkel19be5062014-03-29 05:29:01 +00001684 unsigned AddendSrcReg = AddendMI->getOperand(1).getReg();
1685 if (TargetRegisterInfo::isVirtualRegister(AddendSrcReg)) {
1686 if (MRI.getRegClass(AddendMI->getOperand(0).getReg()) !=
1687 MRI.getRegClass(AddendSrcReg))
1688 continue;
1689 } else {
1690 // If AddendSrcReg is a physical register, make sure the destination
1691 // register class contains it.
1692 if (!MRI.getRegClass(AddendMI->getOperand(0).getReg())
1693 ->contains(AddendSrcReg))
1694 continue;
1695 }
Hal Finkel174e5902014-03-25 23:29:21 +00001696
1697 // In theory, there could be other uses of the addend copy before this
1698 // fma. We could deal with this, but that would require additional
1699 // logic below and I suspect it will not occur in any relevant
Bill Schmidta087d742014-10-17 21:02:44 +00001700 // situations. Additionally, check whether the copy source is killed
1701 // prior to the fma. In order to replace the addend here with the
1702 // source of the copy, it must still be live here. We can't use
1703 // interval testing for a physical register, so as long as we're
1704 // walking the MIs we may as well test liveness here.
1705 bool OtherUsers = false, KillsAddendSrc = false;
Hal Finkel174e5902014-03-25 23:29:21 +00001706 for (auto J = std::prev(I), JE = MachineBasicBlock::iterator(AddendMI);
Bill Schmidta087d742014-10-17 21:02:44 +00001707 J != JE; --J) {
Hal Finkel174e5902014-03-25 23:29:21 +00001708 if (J->readsVirtualRegister(AddendMI->getOperand(0).getReg())) {
1709 OtherUsers = true;
1710 break;
1711 }
Bill Schmidta087d742014-10-17 21:02:44 +00001712 if (J->modifiesRegister(AddendSrcReg, TRI) ||
1713 J->killsRegister(AddendSrcReg, TRI)) {
1714 KillsAddendSrc = true;
1715 break;
1716 }
1717 }
Hal Finkel174e5902014-03-25 23:29:21 +00001718
Bill Schmidta087d742014-10-17 21:02:44 +00001719 if (OtherUsers || KillsAddendSrc)
Hal Finkel174e5902014-03-25 23:29:21 +00001720 continue;
1721
1722 // Find one of the product operands that is killed by this instruction.
1723
1724 unsigned KilledProdOp = 0, OtherProdOp = 0;
1725 if (LIS->getInterval(MI->getOperand(2).getReg())
1726 .Query(FMAIdx).isKill()) {
1727 KilledProdOp = 2;
1728 OtherProdOp = 3;
1729 } else if (LIS->getInterval(MI->getOperand(3).getReg())
1730 .Query(FMAIdx).isKill()) {
1731 KilledProdOp = 3;
1732 OtherProdOp = 2;
1733 }
1734
Hal Finkel19be5062014-03-29 05:29:01 +00001735 // If there are no killed product operands, then this transformation is
1736 // likely not profitable.
Hal Finkel174e5902014-03-25 23:29:21 +00001737 if (!KilledProdOp)
1738 continue;
1739
Bill Schmidta087d742014-10-17 21:02:44 +00001740 // For virtual registers, verify that the addend source register
1741 // is live here (as should have been assured above).
Bill Schmidtba637db2014-10-17 21:19:59 +00001742 assert((!TargetRegisterInfo::isVirtualRegister(AddendSrcReg) ||
1743 LIS->getInterval(AddendSrcReg).liveAt(FMAIdx)) &&
1744 "Addend source register is not live!");
Hal Finkel174e5902014-03-25 23:29:21 +00001745
1746 // Transform: (O2 * O3) + O1 -> (O2 * O1) + O3.
1747
1748 unsigned AddReg = AddendMI->getOperand(1).getReg();
1749 unsigned KilledProdReg = MI->getOperand(KilledProdOp).getReg();
1750 unsigned OtherProdReg = MI->getOperand(OtherProdOp).getReg();
1751
1752 unsigned AddSubReg = AddendMI->getOperand(1).getSubReg();
1753 unsigned KilledProdSubReg = MI->getOperand(KilledProdOp).getSubReg();
1754 unsigned OtherProdSubReg = MI->getOperand(OtherProdOp).getSubReg();
1755
1756 bool AddRegKill = AddendMI->getOperand(1).isKill();
1757 bool KilledProdRegKill = MI->getOperand(KilledProdOp).isKill();
1758 bool OtherProdRegKill = MI->getOperand(OtherProdOp).isKill();
1759
1760 bool AddRegUndef = AddendMI->getOperand(1).isUndef();
1761 bool KilledProdRegUndef = MI->getOperand(KilledProdOp).isUndef();
1762 bool OtherProdRegUndef = MI->getOperand(OtherProdOp).isUndef();
1763
1764 unsigned OldFMAReg = MI->getOperand(0).getReg();
1765
Bill Schmidt5c6cb812014-10-21 13:02:37 +00001766 // The transformation doesn't work well with things like:
1767 // %vreg5 = A-form-op %vreg5, %vreg11, %vreg5;
1768 // so leave such things alone.
1769 if (OldFMAReg == KilledProdReg)
1770 continue;
1771
Hal Finkel174e5902014-03-25 23:29:21 +00001772 assert(OldFMAReg == AddendMI->getOperand(0).getReg() &&
1773 "Addend copy not tied to old FMA output!");
1774
1775 DEBUG(dbgs() << "VSX FMA Mutation:\n " << *MI;);
1776
1777 MI->getOperand(0).setReg(KilledProdReg);
1778 MI->getOperand(1).setReg(KilledProdReg);
1779 MI->getOperand(3).setReg(AddReg);
1780 MI->getOperand(2).setReg(OtherProdReg);
1781
1782 MI->getOperand(0).setSubReg(KilledProdSubReg);
1783 MI->getOperand(1).setSubReg(KilledProdSubReg);
1784 MI->getOperand(3).setSubReg(AddSubReg);
1785 MI->getOperand(2).setSubReg(OtherProdSubReg);
1786
1787 MI->getOperand(1).setIsKill(KilledProdRegKill);
1788 MI->getOperand(3).setIsKill(AddRegKill);
1789 MI->getOperand(2).setIsKill(OtherProdRegKill);
1790
1791 MI->getOperand(1).setIsUndef(KilledProdRegUndef);
1792 MI->getOperand(3).setIsUndef(AddRegUndef);
1793 MI->getOperand(2).setIsUndef(OtherProdRegUndef);
1794
1795 MI->setDesc(TII->get(AltOpc));
1796
1797 DEBUG(dbgs() << " -> " << *MI);
1798
1799 // The killed product operand was killed here, so we can reuse it now
1800 // for the result of the fma.
1801
1802 LiveInterval &FMAInt = LIS->getInterval(OldFMAReg);
1803 VNInfo *FMAValNo = FMAInt.getVNInfoAt(FMAIdx.getRegSlot());
1804 for (auto UI = MRI.reg_nodbg_begin(OldFMAReg), UE = MRI.reg_nodbg_end();
1805 UI != UE;) {
1806 MachineOperand &UseMO = *UI;
1807 MachineInstr *UseMI = UseMO.getParent();
1808 ++UI;
1809
1810 // Don't replace the result register of the copy we're about to erase.
1811 if (UseMI == AddendMI)
1812 continue;
1813
1814 UseMO.setReg(KilledProdReg);
1815 UseMO.setSubReg(KilledProdSubReg);
1816 }
1817
1818 // Extend the live intervals of the killed product operand to hold the
1819 // fma result.
1820
1821 LiveInterval &NewFMAInt = LIS->getInterval(KilledProdReg);
1822 for (LiveInterval::iterator AI = FMAInt.begin(), AE = FMAInt.end();
1823 AI != AE; ++AI) {
1824 // Don't add the segment that corresponds to the original copy.
1825 if (AI->valno == AddendValNo)
1826 continue;
1827
1828 VNInfo *NewFMAValNo =
1829 NewFMAInt.getNextValue(AI->start,
1830 LIS->getVNInfoAllocator());
1831
1832 NewFMAInt.addSegment(LiveInterval::Segment(AI->start, AI->end,
1833 NewFMAValNo));
1834 }
1835 DEBUG(dbgs() << " extended: " << NewFMAInt << '\n');
1836
1837 FMAInt.removeValNo(FMAValNo);
1838 DEBUG(dbgs() << " trimmed: " << FMAInt << '\n');
1839
1840 // Remove the (now unused) copy.
1841
1842 DEBUG(dbgs() << " removing: " << *AddendMI << '\n');
1843 LIS->RemoveMachineInstrFromMaps(AddendMI);
1844 AddendMI->eraseFromParent();
1845
1846 Changed = true;
1847 }
1848
1849 return Changed;
1850 }
1851
1852public:
Craig Topper0d3fa922014-04-29 07:57:37 +00001853 bool runOnMachineFunction(MachineFunction &MF) override {
Eric Christopherd71e4442014-05-22 01:21:35 +00001854 TM = static_cast<const PPCTargetMachine *>(&MF.getTarget());
1855 // If we don't have VSX then go ahead and return without doing
1856 // anything.
1857 if (!TM->getSubtargetImpl()->hasVSX())
1858 return false;
1859
Hal Finkel174e5902014-03-25 23:29:21 +00001860 LIS = &getAnalysis<LiveIntervals>();
1861
Eric Christopherd9134482014-08-04 21:25:23 +00001862 TII = TM->getSubtargetImpl()->getInstrInfo();
Hal Finkel174e5902014-03-25 23:29:21 +00001863
1864 bool Changed = false;
1865
1866 if (DisableVSXFMAMutate)
1867 return Changed;
1868
1869 for (MachineFunction::iterator I = MF.begin(); I != MF.end();) {
1870 MachineBasicBlock &B = *I++;
1871 if (processBlock(B))
1872 Changed = true;
1873 }
1874
1875 return Changed;
1876 }
1877
Craig Topper0d3fa922014-04-29 07:57:37 +00001878 void getAnalysisUsage(AnalysisUsage &AU) const override {
Hal Finkel174e5902014-03-25 23:29:21 +00001879 AU.addRequired<LiveIntervals>();
1880 AU.addPreserved<LiveIntervals>();
1881 AU.addRequired<SlotIndexes>();
1882 AU.addPreserved<SlotIndexes>();
1883 MachineFunctionPass::getAnalysisUsage(AU);
1884 }
1885 };
1886}
1887
1888INITIALIZE_PASS_BEGIN(PPCVSXFMAMutate, DEBUG_TYPE,
1889 "PowerPC VSX FMA Mutation", false, false)
1890INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
1891INITIALIZE_PASS_DEPENDENCY(SlotIndexes)
1892INITIALIZE_PASS_END(PPCVSXFMAMutate, DEBUG_TYPE,
1893 "PowerPC VSX FMA Mutation", false, false)
1894
1895char &llvm::PPCVSXFMAMutateID = PPCVSXFMAMutate::ID;
1896
1897char PPCVSXFMAMutate::ID = 0;
1898FunctionPass*
1899llvm::createPPCVSXFMAMutatePass() { return new PPCVSXFMAMutate(); }
Hal Finkel27774d92014-03-13 07:58:58 +00001900
1901#undef DEBUG_TYPE
1902#define DEBUG_TYPE "ppc-vsx-copy"
1903
1904namespace llvm {
1905 void initializePPCVSXCopyPass(PassRegistry&);
1906}
1907
1908namespace {
1909 // PPCVSXCopy pass - For copies between VSX registers and non-VSX registers
1910 // (Altivec and scalar floating-point registers), we need to transform the
1911 // copies into subregister copies with other restrictions.
1912 struct PPCVSXCopy : public MachineFunctionPass {
1913 static char ID;
1914 PPCVSXCopy() : MachineFunctionPass(ID) {
1915 initializePPCVSXCopyPass(*PassRegistry::getPassRegistry());
1916 }
1917
1918 const PPCTargetMachine *TM;
1919 const PPCInstrInfo *TII;
1920
1921 bool IsRegInClass(unsigned Reg, const TargetRegisterClass *RC,
1922 MachineRegisterInfo &MRI) {
1923 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1924 return RC->hasSubClassEq(MRI.getRegClass(Reg));
1925 } else if (RC->contains(Reg)) {
1926 return true;
1927 }
1928
1929 return false;
1930 }
1931
1932 bool IsVSReg(unsigned Reg, MachineRegisterInfo &MRI) {
1933 return IsRegInClass(Reg, &PPC::VSRCRegClass, MRI);
1934 }
1935
1936 bool IsVRReg(unsigned Reg, MachineRegisterInfo &MRI) {
1937 return IsRegInClass(Reg, &PPC::VRRCRegClass, MRI);
1938 }
1939
1940 bool IsF8Reg(unsigned Reg, MachineRegisterInfo &MRI) {
1941 return IsRegInClass(Reg, &PPC::F8RCRegClass, MRI);
1942 }
1943
1944protected:
1945 bool processBlock(MachineBasicBlock &MBB) {
1946 bool Changed = false;
1947
1948 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
1949 for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end();
1950 I != IE; ++I) {
1951 MachineInstr *MI = I;
1952 if (!MI->isFullCopy())
1953 continue;
1954
1955 MachineOperand &DstMO = MI->getOperand(0);
1956 MachineOperand &SrcMO = MI->getOperand(1);
1957
1958 if ( IsVSReg(DstMO.getReg(), MRI) &&
1959 !IsVSReg(SrcMO.getReg(), MRI)) {
1960 // This is a copy *to* a VSX register from a non-VSX register.
1961 Changed = true;
1962
1963 const TargetRegisterClass *SrcRC =
1964 IsVRReg(SrcMO.getReg(), MRI) ? &PPC::VSHRCRegClass :
1965 &PPC::VSLRCRegClass;
1966 assert((IsF8Reg(SrcMO.getReg(), MRI) ||
1967 IsVRReg(SrcMO.getReg(), MRI)) &&
1968 "Unknown source for a VSX copy");
1969
1970 unsigned NewVReg = MRI.createVirtualRegister(SrcRC);
1971 BuildMI(MBB, MI, MI->getDebugLoc(),
1972 TII->get(TargetOpcode::SUBREG_TO_REG), NewVReg)
1973 .addImm(1) // add 1, not 0, because there is no implicit clearing
1974 // of the high bits.
1975 .addOperand(SrcMO)
1976 .addImm(IsVRReg(SrcMO.getReg(), MRI) ? PPC::sub_128 :
1977 PPC::sub_64);
1978
1979 // The source of the original copy is now the new virtual register.
1980 SrcMO.setReg(NewVReg);
1981 } else if (!IsVSReg(DstMO.getReg(), MRI) &&
1982 IsVSReg(SrcMO.getReg(), MRI)) {
1983 // This is a copy *from* a VSX register to a non-VSX register.
1984 Changed = true;
1985
1986 const TargetRegisterClass *DstRC =
1987 IsVRReg(DstMO.getReg(), MRI) ? &PPC::VSHRCRegClass :
1988 &PPC::VSLRCRegClass;
1989 assert((IsF8Reg(DstMO.getReg(), MRI) ||
1990 IsVRReg(DstMO.getReg(), MRI)) &&
1991 "Unknown destination for a VSX copy");
1992
1993 // Copy the VSX value into a new VSX register of the correct subclass.
1994 unsigned NewVReg = MRI.createVirtualRegister(DstRC);
1995 BuildMI(MBB, MI, MI->getDebugLoc(),
1996 TII->get(TargetOpcode::COPY), NewVReg)
1997 .addOperand(SrcMO);
1998
1999 // Transform the original copy into a subregister extraction copy.
2000 SrcMO.setReg(NewVReg);
2001 SrcMO.setSubReg(IsVRReg(DstMO.getReg(), MRI) ? PPC::sub_128 :
2002 PPC::sub_64);
2003 }
2004 }
2005
2006 return Changed;
2007 }
2008
2009public:
Craig Topper0d3fa922014-04-29 07:57:37 +00002010 bool runOnMachineFunction(MachineFunction &MF) override {
Hal Finkel27774d92014-03-13 07:58:58 +00002011 TM = static_cast<const PPCTargetMachine *>(&MF.getTarget());
Eric Christopherd71e4442014-05-22 01:21:35 +00002012 // If we don't have VSX on the subtarget, don't do anything.
2013 if (!TM->getSubtargetImpl()->hasVSX())
2014 return false;
Eric Christopherd9134482014-08-04 21:25:23 +00002015 TII = TM->getSubtargetImpl()->getInstrInfo();
Hal Finkel27774d92014-03-13 07:58:58 +00002016
2017 bool Changed = false;
2018
2019 for (MachineFunction::iterator I = MF.begin(); I != MF.end();) {
2020 MachineBasicBlock &B = *I++;
2021 if (processBlock(B))
2022 Changed = true;
2023 }
2024
2025 return Changed;
2026 }
2027
Craig Topper0d3fa922014-04-29 07:57:37 +00002028 void getAnalysisUsage(AnalysisUsage &AU) const override {
Hal Finkel27774d92014-03-13 07:58:58 +00002029 MachineFunctionPass::getAnalysisUsage(AU);
2030 }
2031 };
2032}
2033
2034INITIALIZE_PASS(PPCVSXCopy, DEBUG_TYPE,
2035 "PowerPC VSX Copy Legalization", false, false)
2036
2037char PPCVSXCopy::ID = 0;
2038FunctionPass*
2039llvm::createPPCVSXCopyPass() { return new PPCVSXCopy(); }
2040
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002041#undef DEBUG_TYPE
Hal Finkelc6fc9b82014-03-27 23:12:31 +00002042#define DEBUG_TYPE "ppc-vsx-copy-cleanup"
2043
2044namespace llvm {
2045 void initializePPCVSXCopyCleanupPass(PassRegistry&);
2046}
2047
2048namespace {
2049 // PPCVSXCopyCleanup pass - We sometimes end up generating self copies of VSX
2050 // registers (mostly because the ABI code still places all values into the
2051 // "traditional" floating-point and vector registers). Remove them here.
2052 struct PPCVSXCopyCleanup : public MachineFunctionPass {
2053 static char ID;
2054 PPCVSXCopyCleanup() : MachineFunctionPass(ID) {
2055 initializePPCVSXCopyCleanupPass(*PassRegistry::getPassRegistry());
2056 }
2057
2058 const PPCTargetMachine *TM;
2059 const PPCInstrInfo *TII;
2060
2061protected:
2062 bool processBlock(MachineBasicBlock &MBB) {
2063 bool Changed = false;
2064
2065 SmallVector<MachineInstr *, 4> ToDelete;
2066 for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end();
2067 I != IE; ++I) {
2068 MachineInstr *MI = I;
2069 if (MI->getOpcode() == PPC::XXLOR &&
2070 MI->getOperand(0).getReg() == MI->getOperand(1).getReg() &&
2071 MI->getOperand(0).getReg() == MI->getOperand(2).getReg())
2072 ToDelete.push_back(MI);
2073 }
2074
2075 if (!ToDelete.empty())
2076 Changed = true;
2077
2078 for (unsigned i = 0, ie = ToDelete.size(); i != ie; ++i) {
2079 DEBUG(dbgs() << "Removing VSX self-copy: " << *ToDelete[i]);
2080 ToDelete[i]->eraseFromParent();
2081 }
2082
2083 return Changed;
2084 }
2085
2086public:
Craig Topper0d3fa922014-04-29 07:57:37 +00002087 bool runOnMachineFunction(MachineFunction &MF) override {
Hal Finkelc6fc9b82014-03-27 23:12:31 +00002088 TM = static_cast<const PPCTargetMachine *>(&MF.getTarget());
Eric Christopherd71e4442014-05-22 01:21:35 +00002089 // If we don't have VSX don't bother doing anything here.
2090 if (!TM->getSubtargetImpl()->hasVSX())
2091 return false;
Eric Christopherd9134482014-08-04 21:25:23 +00002092 TII = TM->getSubtargetImpl()->getInstrInfo();
Hal Finkelc6fc9b82014-03-27 23:12:31 +00002093
2094 bool Changed = false;
2095
2096 for (MachineFunction::iterator I = MF.begin(); I != MF.end();) {
2097 MachineBasicBlock &B = *I++;
2098 if (processBlock(B))
2099 Changed = true;
2100 }
2101
2102 return Changed;
2103 }
2104
Craig Topper0d3fa922014-04-29 07:57:37 +00002105 void getAnalysisUsage(AnalysisUsage &AU) const override {
Hal Finkelc6fc9b82014-03-27 23:12:31 +00002106 MachineFunctionPass::getAnalysisUsage(AU);
2107 }
2108 };
2109}
2110
2111INITIALIZE_PASS(PPCVSXCopyCleanup, DEBUG_TYPE,
2112 "PowerPC VSX Copy Cleanup", false, false)
2113
2114char PPCVSXCopyCleanup::ID = 0;
2115FunctionPass*
2116llvm::createPPCVSXCopyCleanupPass() { return new PPCVSXCopyCleanup(); }
2117
2118#undef DEBUG_TYPE
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002119#define DEBUG_TYPE "ppc-early-ret"
2120STATISTIC(NumBCLR, "Number of early conditional returns");
2121STATISTIC(NumBLR, "Number of early returns");
2122
2123namespace llvm {
2124 void initializePPCEarlyReturnPass(PassRegistry&);
2125}
2126
2127namespace {
2128 // PPCEarlyReturn pass - For simple functions without epilogue code, move
2129 // returns up, and create conditional returns, to avoid unnecessary
2130 // branch-to-blr sequences.
2131 struct PPCEarlyReturn : public MachineFunctionPass {
2132 static char ID;
2133 PPCEarlyReturn() : MachineFunctionPass(ID) {
2134 initializePPCEarlyReturnPass(*PassRegistry::getPassRegistry());
2135 }
2136
2137 const PPCTargetMachine *TM;
2138 const PPCInstrInfo *TII;
2139
2140protected:
Hal Finkel21aad9a2013-04-09 18:25:18 +00002141 bool processBlock(MachineBasicBlock &ReturnMBB) {
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002142 bool Changed = false;
2143
Hal Finkel21aad9a2013-04-09 18:25:18 +00002144 MachineBasicBlock::iterator I = ReturnMBB.begin();
2145 I = ReturnMBB.SkipPHIsAndLabels(I);
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002146
2147 // The block must be essentially empty except for the blr.
Hal Finkelf4a22c02015-01-13 17:47:54 +00002148 if (I == ReturnMBB.end() ||
2149 (I->getOpcode() != PPC::BLR && I->getOpcode() != PPC::BLR8) ||
Hal Finkel21aad9a2013-04-09 18:25:18 +00002150 I != ReturnMBB.getLastNonDebugInstr())
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002151 return Changed;
2152
2153 SmallVector<MachineBasicBlock*, 8> PredToRemove;
Hal Finkel21aad9a2013-04-09 18:25:18 +00002154 for (MachineBasicBlock::pred_iterator PI = ReturnMBB.pred_begin(),
2155 PIE = ReturnMBB.pred_end(); PI != PIE; ++PI) {
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002156 bool OtherReference = false, BlockChanged = false;
Hal Finkel21aad9a2013-04-09 18:25:18 +00002157 for (MachineBasicBlock::iterator J = (*PI)->getLastNonDebugInstr();;) {
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002158 if (J->getOpcode() == PPC::B) {
Hal Finkel21aad9a2013-04-09 18:25:18 +00002159 if (J->getOperand(0).getMBB() == &ReturnMBB) {
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002160 // This is an unconditional branch to the return. Replace the
Andrew Trick9defbd82013-12-17 04:50:40 +00002161 // branch with a blr.
Hal Finkelf4a22c02015-01-13 17:47:54 +00002162 BuildMI(**PI, J, J->getDebugLoc(), TII->get(I->getOpcode()));
Hal Finkel21aad9a2013-04-09 18:25:18 +00002163 MachineBasicBlock::iterator K = J--;
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002164 K->eraseFromParent();
2165 BlockChanged = true;
2166 ++NumBLR;
2167 continue;
2168 }
2169 } else if (J->getOpcode() == PPC::BCC) {
Hal Finkel21aad9a2013-04-09 18:25:18 +00002170 if (J->getOperand(2).getMBB() == &ReturnMBB) {
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002171 // This is a conditional branch to the return. Replace the branch
2172 // with a bclr.
Hal Finkel940ab932014-02-28 00:27:01 +00002173 BuildMI(**PI, J, J->getDebugLoc(), TII->get(PPC::BCCLR))
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002174 .addImm(J->getOperand(0).getImm())
2175 .addReg(J->getOperand(1).getReg());
Hal Finkel21aad9a2013-04-09 18:25:18 +00002176 MachineBasicBlock::iterator K = J--;
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002177 K->eraseFromParent();
2178 BlockChanged = true;
2179 ++NumBCLR;
2180 continue;
2181 }
Hal Finkel940ab932014-02-28 00:27:01 +00002182 } else if (J->getOpcode() == PPC::BC || J->getOpcode() == PPC::BCn) {
2183 if (J->getOperand(1).getMBB() == &ReturnMBB) {
2184 // This is a conditional branch to the return. Replace the branch
2185 // with a bclr.
2186 BuildMI(**PI, J, J->getDebugLoc(),
2187 TII->get(J->getOpcode() == PPC::BC ?
2188 PPC::BCLR : PPC::BCLRn))
2189 .addReg(J->getOperand(0).getReg());
2190 MachineBasicBlock::iterator K = J--;
2191 K->eraseFromParent();
2192 BlockChanged = true;
2193 ++NumBCLR;
2194 continue;
2195 }
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002196 } else if (J->isBranch()) {
2197 if (J->isIndirectBranch()) {
Hal Finkel21aad9a2013-04-09 18:25:18 +00002198 if (ReturnMBB.hasAddressTaken())
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002199 OtherReference = true;
2200 } else
2201 for (unsigned i = 0; i < J->getNumOperands(); ++i)
2202 if (J->getOperand(i).isMBB() &&
Hal Finkel21aad9a2013-04-09 18:25:18 +00002203 J->getOperand(i).getMBB() == &ReturnMBB)
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002204 OtherReference = true;
Hal Finkel21aad9a2013-04-09 18:25:18 +00002205 } else if (!J->isTerminator() && !J->isDebugValue())
2206 break;
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002207
Hal Finkel21aad9a2013-04-09 18:25:18 +00002208 if (J == (*PI)->begin())
2209 break;
2210
2211 --J;
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002212 }
2213
Hal Finkel21aad9a2013-04-09 18:25:18 +00002214 if ((*PI)->canFallThrough() && (*PI)->isLayoutSuccessor(&ReturnMBB))
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002215 OtherReference = true;
2216
Andrew Trick9defbd82013-12-17 04:50:40 +00002217 // Predecessors are stored in a vector and can't be removed here.
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002218 if (!OtherReference && BlockChanged) {
2219 PredToRemove.push_back(*PI);
2220 }
2221
2222 if (BlockChanged)
2223 Changed = true;
2224 }
2225
2226 for (unsigned i = 0, ie = PredToRemove.size(); i != ie; ++i)
Hal Finkel21aad9a2013-04-09 18:25:18 +00002227 PredToRemove[i]->removeSuccessor(&ReturnMBB);
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002228
Hal Finkel21aad9a2013-04-09 18:25:18 +00002229 if (Changed && !ReturnMBB.hasAddressTaken()) {
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002230 // We now might be able to merge this blr-only block into its
2231 // by-layout predecessor.
Hal Finkel21aad9a2013-04-09 18:25:18 +00002232 if (ReturnMBB.pred_size() == 1 &&
2233 (*ReturnMBB.pred_begin())->isLayoutSuccessor(&ReturnMBB)) {
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002234 // Move the blr into the preceding block.
Hal Finkel21aad9a2013-04-09 18:25:18 +00002235 MachineBasicBlock &PrevMBB = **ReturnMBB.pred_begin();
2236 PrevMBB.splice(PrevMBB.end(), &ReturnMBB, I);
2237 PrevMBB.removeSuccessor(&ReturnMBB);
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002238 }
2239
Hal Finkel21aad9a2013-04-09 18:25:18 +00002240 if (ReturnMBB.pred_empty())
2241 ReturnMBB.eraseFromParent();
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002242 }
2243
2244 return Changed;
2245 }
2246
2247public:
Craig Topper0d3fa922014-04-29 07:57:37 +00002248 bool runOnMachineFunction(MachineFunction &MF) override {
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002249 TM = static_cast<const PPCTargetMachine *>(&MF.getTarget());
Eric Christopherd9134482014-08-04 21:25:23 +00002250 TII = TM->getSubtargetImpl()->getInstrInfo();
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002251
2252 bool Changed = false;
2253
Hal Finkel21aad9a2013-04-09 18:25:18 +00002254 // If the function does not have at least two blocks, then there is
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002255 // nothing to do.
2256 if (MF.size() < 2)
2257 return Changed;
2258
2259 for (MachineFunction::iterator I = MF.begin(); I != MF.end();) {
Andrew Trick9defbd82013-12-17 04:50:40 +00002260 MachineBasicBlock &B = *I++;
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002261 if (processBlock(B))
2262 Changed = true;
2263 }
2264
2265 return Changed;
2266 }
2267
Craig Topper0d3fa922014-04-29 07:57:37 +00002268 void getAnalysisUsage(AnalysisUsage &AU) const override {
Hal Finkelb5aa7e52013-04-08 16:24:03 +00002269 MachineFunctionPass::getAnalysisUsage(AU);
2270 }
2271 };
2272}
2273
2274INITIALIZE_PASS(PPCEarlyReturn, DEBUG_TYPE,
2275 "PowerPC Early-Return Creation", false, false)
2276
2277char PPCEarlyReturn::ID = 0;
2278FunctionPass*
2279llvm::createPPCEarlyReturnPass() { return new PPCEarlyReturn(); }