blob: 1f98fe62126b0cfa28f433f00c8234873d26086b [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"
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +000023#include "llvm/CodeGen/MachineFrameInfo.h"
Hal Finkelb5aa7e52013-04-08 16:24:03 +000024#include "llvm/CodeGen/MachineFunctionPass.h"
Misha Brukman116f9272004-08-17 04:55:41 +000025#include "llvm/CodeGen/MachineInstrBuilder.h"
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +000026#include "llvm/CodeGen/MachineMemOperand.h"
Jakob Stoklund Olesenddbf7a82010-02-26 21:09:24 +000027#include "llvm/CodeGen/MachineRegisterInfo.h"
Hal Finkel9f9f8922012-04-01 19:22:40 +000028#include "llvm/CodeGen/PseudoSourceValue.h"
Evan Chengc5e6d2f2011-07-11 03:57:24 +000029#include "llvm/MC/MCAsmInfo.h"
Bill Wendling1af20ad2008-03-04 23:13:51 +000030#include "llvm/Support/CommandLine.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000031#include "llvm/Support/ErrorHandling.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000032#include "llvm/Support/TargetRegistry.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000033#include "llvm/Support/raw_ostream.h"
Misha Brukman116f9272004-08-17 04:55:41 +000034
Hal Finkel82656cb2013-04-18 22:15:08 +000035#define GET_INSTRMAP_INFO
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000036#define GET_INSTRINFO_CTOR_DTOR
Evan Cheng1e210d02011-06-28 20:07:07 +000037#include "PPCGenInstrInfo.inc"
38
Dan Gohman20857192010-04-15 17:20:57 +000039using namespace llvm;
Bill Wendling1af20ad2008-03-04 23:13:51 +000040
Hal Finkel821e0012012-06-08 15:38:25 +000041static cl::
Hal Finkelc6b5deb2012-06-08 19:19:53 +000042opt<bool> DisableCTRLoopAnal("disable-ppc-ctrloop-analysis", cl::Hidden,
43 cl::desc("Disable analysis for CTR loops"));
Hal Finkel821e0012012-06-08 15:38:25 +000044
Hal Finkele6322392013-04-19 22:08:38 +000045static cl::opt<bool> DisableCmpOpt("disable-ppc-cmp-opt",
Hal Finkelb12da6b2013-04-18 22:54:25 +000046cl::desc("Disable compare instruction optimization"), cl::Hidden);
47
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000048// Pin the vtable to this file.
49void PPCInstrInfo::anchor() {}
50
Chris Lattner49cadab2006-06-17 00:01:04 +000051PPCInstrInfo::PPCInstrInfo(PPCTargetMachine &tm)
Evan Cheng703a0fb2011-07-01 17:57:27 +000052 : PPCGenInstrInfo(PPC::ADJCALLSTACKDOWN, PPC::ADJCALLSTACKUP),
Bill Wendling5e7656b2013-06-07 07:55:53 +000053 TM(tm), RI(*TM.getSubtargetImpl()) {}
Chris Lattner49cadab2006-06-17 00:01:04 +000054
Andrew Trick10ffc2b2010-12-24 05:03:26 +000055/// CreateTargetHazardRecognizer - Return the hazard recognizer to use for
56/// this target when scheduling the DAG.
57ScheduleHazardRecognizer *PPCInstrInfo::CreateTargetHazardRecognizer(
58 const TargetMachine *TM,
59 const ScheduleDAG *DAG) const {
Hal Finkel6fa56972011-10-17 04:03:49 +000060 unsigned Directive = TM->getSubtarget<PPCSubtarget>().getDarwinDirective();
Hal Finkel742b5352012-08-28 16:12:39 +000061 if (Directive == PPC::DIR_440 || Directive == PPC::DIR_A2 ||
62 Directive == PPC::DIR_E500mc || Directive == PPC::DIR_E5500) {
Hal Finkel6f0ae782011-11-22 16:21:04 +000063 const InstrItineraryData *II = TM->getInstrItineraryData();
Hal Finkel563cc052013-12-02 23:52:46 +000064 return new ScoreboardHazardRecognizer(II, DAG);
Hal Finkel6fa56972011-10-17 04:03:49 +000065 }
Hal Finkel58ca3602011-12-02 04:58:02 +000066
Jakob Stoklund Olesen9de596e2012-11-28 02:35:17 +000067 return TargetInstrInfo::CreateTargetHazardRecognizer(TM, DAG);
Andrew Trick10ffc2b2010-12-24 05:03:26 +000068}
69
Hal Finkel58ca3602011-12-02 04:58:02 +000070/// CreateTargetPostRAHazardRecognizer - Return the postRA hazard recognizer
71/// to use for this target when scheduling the DAG.
72ScheduleHazardRecognizer *PPCInstrInfo::CreateTargetPostRAHazardRecognizer(
73 const InstrItineraryData *II,
74 const ScheduleDAG *DAG) const {
75 unsigned Directive = TM.getSubtarget<PPCSubtarget>().getDarwinDirective();
76
Hal Finkelceb1f122013-12-12 00:19:11 +000077 if (Directive == PPC::DIR_PWR7)
78 return new PPCDispatchGroupSBHazardRecognizer(II, DAG);
79
Hal Finkel58ca3602011-12-02 04:58:02 +000080 // Most subtargets use a PPC970 recognizer.
Hal Finkel742b5352012-08-28 16:12:39 +000081 if (Directive != PPC::DIR_440 && Directive != PPC::DIR_A2 &&
82 Directive != PPC::DIR_E500mc && Directive != PPC::DIR_E5500) {
Benjamin Kramerf0ec1992013-06-07 11:23:35 +000083 assert(TM.getInstrInfo() && "No InstrInfo?");
Hal Finkel58ca3602011-12-02 04:58:02 +000084
Bill Wendling5e7656b2013-06-07 07:55:53 +000085 return new PPCHazardRecognizer970(TM);
Hal Finkel58ca3602011-12-02 04:58:02 +000086 }
87
Hal Finkel563cc052013-12-02 23:52:46 +000088 return new ScoreboardHazardRecognizer(II, DAG);
Hal Finkel58ca3602011-12-02 04:58:02 +000089}
Jakob Stoklund Olesen0f855e42012-06-19 21:14:34 +000090
Hal Finkelceb1f122013-12-12 00:19:11 +000091
92int PPCInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
93 const MachineInstr *DefMI, unsigned DefIdx,
94 const MachineInstr *UseMI,
95 unsigned UseIdx) const {
96 int Latency = PPCGenInstrInfo::getOperandLatency(ItinData, DefMI, DefIdx,
97 UseMI, UseIdx);
98
99 const MachineOperand &DefMO = DefMI->getOperand(DefIdx);
100 unsigned Reg = DefMO.getReg();
101
102 const TargetRegisterInfo *TRI = &getRegisterInfo();
103 bool IsRegCR;
104 if (TRI->isVirtualRegister(Reg)) {
105 const MachineRegisterInfo *MRI =
106 &DefMI->getParent()->getParent()->getRegInfo();
107 IsRegCR = MRI->getRegClass(Reg)->hasSuperClassEq(&PPC::CRRCRegClass) ||
108 MRI->getRegClass(Reg)->hasSuperClassEq(&PPC::CRBITRCRegClass);
109 } else {
110 IsRegCR = PPC::CRRCRegClass.contains(Reg) ||
111 PPC::CRBITRCRegClass.contains(Reg);
112 }
113
114 if (UseMI->isBranch() && IsRegCR) {
115 if (Latency < 0)
116 Latency = getInstrLatency(ItinData, DefMI);
117
118 // On some cores, there is an additional delay between writing to a condition
119 // register, and using it from a branch.
120 unsigned Directive = TM.getSubtarget<PPCSubtarget>().getDarwinDirective();
121 switch (Directive) {
122 default: break;
123 case PPC::DIR_7400:
124 case PPC::DIR_750:
125 case PPC::DIR_970:
126 case PPC::DIR_E5500:
127 case PPC::DIR_PWR4:
128 case PPC::DIR_PWR5:
129 case PPC::DIR_PWR5X:
130 case PPC::DIR_PWR6:
131 case PPC::DIR_PWR6X:
132 case PPC::DIR_PWR7:
133 Latency += 2;
134 break;
135 }
136 }
137
138 return Latency;
139}
140
Jakob Stoklund Olesen0f855e42012-06-19 21:14:34 +0000141// Detect 32 -> 64-bit extensions where we may reuse the low sub-register.
142bool PPCInstrInfo::isCoalescableExtInstr(const MachineInstr &MI,
143 unsigned &SrcReg, unsigned &DstReg,
144 unsigned &SubIdx) const {
145 switch (MI.getOpcode()) {
146 default: return false;
147 case PPC::EXTSW:
148 case PPC::EXTSW_32_64:
149 SrcReg = MI.getOperand(1).getReg();
150 DstReg = MI.getOperand(0).getReg();
151 SubIdx = PPC::sub_32;
152 return true;
153 }
154}
155
Andrew Trickc416ba62010-12-24 04:28:06 +0000156unsigned PPCInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
Chris Lattner91400bd2006-03-16 22:24:02 +0000157 int &FrameIndex) const {
Hal Finkel37714b82013-03-27 21:21:15 +0000158 // Note: This list must be kept consistent with LoadRegFromStackSlot.
Chris Lattnerbb53acd2006-02-02 20:12:32 +0000159 switch (MI->getOpcode()) {
160 default: break;
161 case PPC::LD:
162 case PPC::LWZ:
163 case PPC::LFS:
164 case PPC::LFD:
Hal Finkel37714b82013-03-27 21:21:15 +0000165 case PPC::RESTORE_CR:
Hal Finkel940ab932014-02-28 00:27:01 +0000166 case PPC::RESTORE_CRBIT:
Hal Finkel37714b82013-03-27 21:21:15 +0000167 case PPC::LVX:
Hal Finkel27774d92014-03-13 07:58:58 +0000168 case PPC::LXVD2X:
Hal Finkel37714b82013-03-27 21:21:15 +0000169 case PPC::RESTORE_VRSAVE:
170 // Check for the operands added by addFrameReference (the immediate is the
171 // offset which defaults to 0).
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000172 if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
173 MI->getOperand(2).isFI()) {
Chris Lattnera5bb3702007-12-30 23:10:15 +0000174 FrameIndex = MI->getOperand(2).getIndex();
Chris Lattnerbb53acd2006-02-02 20:12:32 +0000175 return MI->getOperand(0).getReg();
176 }
177 break;
178 }
179 return 0;
Chris Lattnerc327d712006-02-02 20:16:12 +0000180}
Chris Lattnerbb53acd2006-02-02 20:12:32 +0000181
Andrew Trickc416ba62010-12-24 04:28:06 +0000182unsigned PPCInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
Chris Lattnerc327d712006-02-02 20:16:12 +0000183 int &FrameIndex) const {
Hal Finkel37714b82013-03-27 21:21:15 +0000184 // Note: This list must be kept consistent with StoreRegToStackSlot.
Chris Lattnerc327d712006-02-02 20:16:12 +0000185 switch (MI->getOpcode()) {
186 default: break;
Nate Begeman4efb3282006-02-02 21:07:50 +0000187 case PPC::STD:
Chris Lattnerc327d712006-02-02 20:16:12 +0000188 case PPC::STW:
189 case PPC::STFS:
190 case PPC::STFD:
Hal Finkel37714b82013-03-27 21:21:15 +0000191 case PPC::SPILL_CR:
Hal Finkel940ab932014-02-28 00:27:01 +0000192 case PPC::SPILL_CRBIT:
Hal Finkel37714b82013-03-27 21:21:15 +0000193 case PPC::STVX:
Hal Finkel27774d92014-03-13 07:58:58 +0000194 case PPC::STXVD2X:
Hal Finkel37714b82013-03-27 21:21:15 +0000195 case PPC::SPILL_VRSAVE:
196 // Check for the operands added by addFrameReference (the immediate is the
197 // offset which defaults to 0).
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000198 if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
199 MI->getOperand(2).isFI()) {
Chris Lattnera5bb3702007-12-30 23:10:15 +0000200 FrameIndex = MI->getOperand(2).getIndex();
Chris Lattnerc327d712006-02-02 20:16:12 +0000201 return MI->getOperand(0).getReg();
202 }
203 break;
204 }
205 return 0;
206}
Chris Lattnerbb53acd2006-02-02 20:12:32 +0000207
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000208// commuteInstruction - We can commute rlwimi instructions, but only if the
209// rotate amt is zero. We also have to munge the immediates a bit.
Evan Cheng03553bb2008-06-16 07:33:11 +0000210MachineInstr *
211PPCInstrInfo::commuteInstruction(MachineInstr *MI, bool NewMI) const {
Dan Gohman3b460302008-07-07 23:14:23 +0000212 MachineFunction &MF = *MI->getParent()->getParent();
213
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000214 // Normal instructions can be commuted the obvious way.
Hal Finkel654d43b2013-04-12 02:18:09 +0000215 if (MI->getOpcode() != PPC::RLWIMI &&
Hal Finkel940ab932014-02-28 00:27:01 +0000216 MI->getOpcode() != PPC::RLWIMIo &&
217 MI->getOpcode() != PPC::RLWIMI8 &&
218 MI->getOpcode() != PPC::RLWIMI8o)
Jakob Stoklund Olesen9de596e2012-11-28 02:35:17 +0000219 return TargetInstrInfo::commuteInstruction(MI, NewMI);
Andrew Trickc416ba62010-12-24 04:28:06 +0000220
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000221 // Cannot commute if it has a non-zero rotate count.
Chris Lattner5c463782007-12-30 20:49:49 +0000222 if (MI->getOperand(3).getImm() != 0)
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000223 return 0;
Andrew Trickc416ba62010-12-24 04:28:06 +0000224
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000225 // If we have a zero rotate count, we have:
226 // M = mask(MB,ME)
227 // Op0 = (Op1 & ~M) | (Op2 & M)
228 // Change this to:
229 // M = mask((ME+1)&31, (MB-1)&31)
230 // Op0 = (Op2 & ~M) | (Op1 & M)
231
232 // Swap op1/op2
Evan Cheng244183e2008-02-13 02:46:49 +0000233 unsigned Reg0 = MI->getOperand(0).getReg();
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000234 unsigned Reg1 = MI->getOperand(1).getReg();
235 unsigned Reg2 = MI->getOperand(2).getReg();
Andrew Tricke3398282013-12-17 04:50:45 +0000236 unsigned SubReg1 = MI->getOperand(1).getSubReg();
237 unsigned SubReg2 = MI->getOperand(2).getSubReg();
Evan Chengdc2c8742006-11-15 20:58:11 +0000238 bool Reg1IsKill = MI->getOperand(1).isKill();
239 bool Reg2IsKill = MI->getOperand(2).isKill();
Evan Cheng03553bb2008-06-16 07:33:11 +0000240 bool ChangeReg0 = false;
Evan Cheng244183e2008-02-13 02:46:49 +0000241 // If machine instrs are no longer in two-address forms, update
242 // destination register as well.
243 if (Reg0 == Reg1) {
244 // Must be two address instruction!
Evan Cheng6cc775f2011-06-28 19:10:37 +0000245 assert(MI->getDesc().getOperandConstraint(0, MCOI::TIED_TO) &&
Evan Cheng244183e2008-02-13 02:46:49 +0000246 "Expecting a two-address instruction!");
Andrew Tricke3398282013-12-17 04:50:45 +0000247 assert(MI->getOperand(0).getSubReg() == SubReg1 && "Tied subreg mismatch");
Evan Cheng244183e2008-02-13 02:46:49 +0000248 Reg2IsKill = false;
Evan Cheng03553bb2008-06-16 07:33:11 +0000249 ChangeReg0 = true;
Evan Cheng244183e2008-02-13 02:46:49 +0000250 }
Evan Cheng03553bb2008-06-16 07:33:11 +0000251
252 // Masks.
253 unsigned MB = MI->getOperand(4).getImm();
254 unsigned ME = MI->getOperand(5).getImm();
255
256 if (NewMI) {
257 // Create a new instruction.
258 unsigned Reg0 = ChangeReg0 ? Reg2 : MI->getOperand(0).getReg();
259 bool Reg0IsDead = MI->getOperand(0).isDead();
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000260 return BuildMI(MF, MI->getDebugLoc(), MI->getDesc())
Bill Wendlingf7b83c72009-05-13 21:33:08 +0000261 .addReg(Reg0, RegState::Define | getDeadRegState(Reg0IsDead))
262 .addReg(Reg2, getKillRegState(Reg2IsKill))
263 .addReg(Reg1, getKillRegState(Reg1IsKill))
Evan Cheng03553bb2008-06-16 07:33:11 +0000264 .addImm((ME+1) & 31)
265 .addImm((MB-1) & 31);
266 }
267
Andrew Tricke3398282013-12-17 04:50:45 +0000268 if (ChangeReg0) {
Evan Cheng03553bb2008-06-16 07:33:11 +0000269 MI->getOperand(0).setReg(Reg2);
Andrew Tricke3398282013-12-17 04:50:45 +0000270 MI->getOperand(0).setSubReg(SubReg2);
271 }
Chris Lattner10d63412006-05-04 17:52:23 +0000272 MI->getOperand(2).setReg(Reg1);
273 MI->getOperand(1).setReg(Reg2);
Andrew Tricke3398282013-12-17 04:50:45 +0000274 MI->getOperand(2).setSubReg(SubReg1);
275 MI->getOperand(1).setSubReg(SubReg2);
Chris Lattner60055892007-12-30 21:56:09 +0000276 MI->getOperand(2).setIsKill(Reg1IsKill);
277 MI->getOperand(1).setIsKill(Reg2IsKill);
Andrew Trickc416ba62010-12-24 04:28:06 +0000278
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000279 // Swap the mask around.
Chris Lattner5c463782007-12-30 20:49:49 +0000280 MI->getOperand(4).setImm((ME+1) & 31);
281 MI->getOperand(5).setImm((MB-1) & 31);
Chris Lattnerc37a2f12005-09-09 18:17:41 +0000282 return MI;
283}
Chris Lattnerea79d9fd732006-03-05 23:49:55 +0000284
Hal Finkel6c32ff32014-03-25 19:26:43 +0000285bool PPCInstrInfo::findCommutedOpIndices(MachineInstr *MI, unsigned &SrcOpIdx1,
286 unsigned &SrcOpIdx2) const {
287 // For VSX A-Type FMA instructions, it is the first two operands that can be
288 // commuted, however, because the non-encoded tied input operand is listed
289 // first, the operands to swap are actually the second and third.
290
291 int AltOpc = PPC::getAltVSXFMAOpcode(MI->getOpcode());
292 if (AltOpc == -1)
293 return TargetInstrInfo::findCommutedOpIndices(MI, SrcOpIdx1, SrcOpIdx2);
294
295 SrcOpIdx1 = 2;
296 SrcOpIdx2 = 3;
297 return true;
298}
299
Andrew Trickc416ba62010-12-24 04:28:06 +0000300void PPCInstrInfo::insertNoop(MachineBasicBlock &MBB,
Chris Lattnerea79d9fd732006-03-05 23:49:55 +0000301 MachineBasicBlock::iterator MI) const {
Hal Finkelceb1f122013-12-12 00:19:11 +0000302 // This function is used for scheduling, and the nop wanted here is the type
303 // that terminates dispatch groups on the POWER cores.
304 unsigned Directive = TM.getSubtarget<PPCSubtarget>().getDarwinDirective();
305 unsigned Opcode;
306 switch (Directive) {
307 default: Opcode = PPC::NOP; break;
308 case PPC::DIR_PWR6: Opcode = PPC::NOP_GT_PWR6; break;
309 case PPC::DIR_PWR7: Opcode = PPC::NOP_GT_PWR7; break;
310 }
Chris Lattnera47294ed2006-10-13 21:21:17 +0000311
Hal Finkelceb1f122013-12-12 00:19:11 +0000312 DebugLoc DL;
313 BuildMI(MBB, MI, DL, get(Opcode));
314}
Chris Lattnera47294ed2006-10-13 21:21:17 +0000315
316// Branch analysis.
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000317// Note: If the condition register is set to CTR or CTR8 then this is a
318// BDNZ (imm == 1) or BDZ (imm == 0) branch.
Chris Lattnera47294ed2006-10-13 21:21:17 +0000319bool PPCInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
320 MachineBasicBlock *&FBB,
Evan Cheng64dfcac2009-02-09 07:14:22 +0000321 SmallVectorImpl<MachineOperand> &Cond,
322 bool AllowModify) const {
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000323 bool isPPC64 = TM.getSubtargetImpl()->isPPC64();
324
Chris Lattnera47294ed2006-10-13 21:21:17 +0000325 // If the block has no terminators, it just falls into the block after it.
326 MachineBasicBlock::iterator I = MBB.end();
Dale Johannesen4244d122010-04-02 01:38:09 +0000327 if (I == MBB.begin())
328 return false;
329 --I;
330 while (I->isDebugValue()) {
331 if (I == MBB.begin())
332 return false;
333 --I;
334 }
335 if (!isUnpredicatedTerminator(I))
Chris Lattnera47294ed2006-10-13 21:21:17 +0000336 return false;
337
338 // Get the last instruction in the block.
339 MachineInstr *LastInst = I;
Andrew Trickc416ba62010-12-24 04:28:06 +0000340
Chris Lattnera47294ed2006-10-13 21:21:17 +0000341 // If there is only one terminator instruction, process it.
Evan Cheng5514bbe2007-06-08 21:59:56 +0000342 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
Chris Lattnera47294ed2006-10-13 21:21:17 +0000343 if (LastInst->getOpcode() == PPC::B) {
Evan Cheng8f43afd2009-05-08 23:09:25 +0000344 if (!LastInst->getOperand(0).isMBB())
345 return true;
Chris Lattnera5bb3702007-12-30 23:10:15 +0000346 TBB = LastInst->getOperand(0).getMBB();
Chris Lattnera47294ed2006-10-13 21:21:17 +0000347 return false;
Chris Lattnere0263792006-11-17 22:14:47 +0000348 } else if (LastInst->getOpcode() == PPC::BCC) {
Evan Cheng8f43afd2009-05-08 23:09:25 +0000349 if (!LastInst->getOperand(2).isMBB())
350 return true;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000351 // Block ends with fall-through condbranch.
Chris Lattnera5bb3702007-12-30 23:10:15 +0000352 TBB = LastInst->getOperand(2).getMBB();
Chris Lattnera47294ed2006-10-13 21:21:17 +0000353 Cond.push_back(LastInst->getOperand(0));
354 Cond.push_back(LastInst->getOperand(1));
Chris Lattner23f22de2006-10-21 06:03:11 +0000355 return false;
Hal Finkel940ab932014-02-28 00:27:01 +0000356 } else if (LastInst->getOpcode() == PPC::BC) {
357 if (!LastInst->getOperand(1).isMBB())
358 return true;
359 // Block ends with fall-through condbranch.
360 TBB = LastInst->getOperand(1).getMBB();
361 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET));
362 Cond.push_back(LastInst->getOperand(0));
363 return false;
364 } else if (LastInst->getOpcode() == PPC::BCn) {
365 if (!LastInst->getOperand(1).isMBB())
366 return true;
367 // Block ends with fall-through condbranch.
368 TBB = LastInst->getOperand(1).getMBB();
369 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_UNSET));
370 Cond.push_back(LastInst->getOperand(0));
371 return false;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000372 } else if (LastInst->getOpcode() == PPC::BDNZ8 ||
373 LastInst->getOpcode() == PPC::BDNZ) {
374 if (!LastInst->getOperand(0).isMBB())
375 return true;
Hal Finkelc6b5deb2012-06-08 19:19:53 +0000376 if (DisableCTRLoopAnal)
Hal Finkel821e0012012-06-08 15:38:25 +0000377 return true;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000378 TBB = LastInst->getOperand(0).getMBB();
379 Cond.push_back(MachineOperand::CreateImm(1));
380 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
381 true));
382 return false;
383 } else if (LastInst->getOpcode() == PPC::BDZ8 ||
384 LastInst->getOpcode() == PPC::BDZ) {
385 if (!LastInst->getOperand(0).isMBB())
386 return true;
Hal Finkelc6b5deb2012-06-08 19:19:53 +0000387 if (DisableCTRLoopAnal)
Hal Finkel821e0012012-06-08 15:38:25 +0000388 return true;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000389 TBB = LastInst->getOperand(0).getMBB();
390 Cond.push_back(MachineOperand::CreateImm(0));
391 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
392 true));
393 return false;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000394 }
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000395
Chris Lattnera47294ed2006-10-13 21:21:17 +0000396 // Otherwise, don't know what this is.
397 return true;
398 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000399
Chris Lattnera47294ed2006-10-13 21:21:17 +0000400 // Get the instruction before it if it's a terminator.
401 MachineInstr *SecondLastInst = I;
402
403 // If there are three terminators, we don't know what sort of block this is.
404 if (SecondLastInst && I != MBB.begin() &&
Evan Cheng5514bbe2007-06-08 21:59:56 +0000405 isUnpredicatedTerminator(--I))
Chris Lattnera47294ed2006-10-13 21:21:17 +0000406 return true;
Andrew Trickc416ba62010-12-24 04:28:06 +0000407
Chris Lattnere0263792006-11-17 22:14:47 +0000408 // If the block ends with PPC::B and PPC:BCC, handle it.
Andrew Trickc416ba62010-12-24 04:28:06 +0000409 if (SecondLastInst->getOpcode() == PPC::BCC &&
Chris Lattnera47294ed2006-10-13 21:21:17 +0000410 LastInst->getOpcode() == PPC::B) {
Evan Cheng8f43afd2009-05-08 23:09:25 +0000411 if (!SecondLastInst->getOperand(2).isMBB() ||
412 !LastInst->getOperand(0).isMBB())
413 return true;
Chris Lattnera5bb3702007-12-30 23:10:15 +0000414 TBB = SecondLastInst->getOperand(2).getMBB();
Chris Lattnera47294ed2006-10-13 21:21:17 +0000415 Cond.push_back(SecondLastInst->getOperand(0));
416 Cond.push_back(SecondLastInst->getOperand(1));
Chris Lattnera5bb3702007-12-30 23:10:15 +0000417 FBB = LastInst->getOperand(0).getMBB();
Chris Lattnera47294ed2006-10-13 21:21:17 +0000418 return false;
Hal Finkel940ab932014-02-28 00:27:01 +0000419 } else if (SecondLastInst->getOpcode() == PPC::BC &&
420 LastInst->getOpcode() == PPC::B) {
421 if (!SecondLastInst->getOperand(1).isMBB() ||
422 !LastInst->getOperand(0).isMBB())
423 return true;
424 TBB = SecondLastInst->getOperand(1).getMBB();
425 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET));
426 Cond.push_back(SecondLastInst->getOperand(0));
427 FBB = LastInst->getOperand(0).getMBB();
428 return false;
429 } else if (SecondLastInst->getOpcode() == PPC::BCn &&
430 LastInst->getOpcode() == PPC::B) {
431 if (!SecondLastInst->getOperand(1).isMBB() ||
432 !LastInst->getOperand(0).isMBB())
433 return true;
434 TBB = SecondLastInst->getOperand(1).getMBB();
435 Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_UNSET));
436 Cond.push_back(SecondLastInst->getOperand(0));
437 FBB = LastInst->getOperand(0).getMBB();
438 return false;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000439 } else if ((SecondLastInst->getOpcode() == PPC::BDNZ8 ||
440 SecondLastInst->getOpcode() == PPC::BDNZ) &&
441 LastInst->getOpcode() == PPC::B) {
442 if (!SecondLastInst->getOperand(0).isMBB() ||
443 !LastInst->getOperand(0).isMBB())
444 return true;
Hal Finkelc6b5deb2012-06-08 19:19:53 +0000445 if (DisableCTRLoopAnal)
Hal Finkel821e0012012-06-08 15:38:25 +0000446 return true;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000447 TBB = SecondLastInst->getOperand(0).getMBB();
448 Cond.push_back(MachineOperand::CreateImm(1));
449 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
450 true));
451 FBB = LastInst->getOperand(0).getMBB();
452 return false;
453 } else if ((SecondLastInst->getOpcode() == PPC::BDZ8 ||
454 SecondLastInst->getOpcode() == PPC::BDZ) &&
455 LastInst->getOpcode() == PPC::B) {
456 if (!SecondLastInst->getOperand(0).isMBB() ||
457 !LastInst->getOperand(0).isMBB())
458 return true;
Hal Finkelc6b5deb2012-06-08 19:19:53 +0000459 if (DisableCTRLoopAnal)
Hal Finkel821e0012012-06-08 15:38:25 +0000460 return true;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000461 TBB = SecondLastInst->getOperand(0).getMBB();
462 Cond.push_back(MachineOperand::CreateImm(0));
463 Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
464 true));
465 FBB = LastInst->getOperand(0).getMBB();
466 return false;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000467 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000468
Dale Johannesenc6855462007-06-13 17:59:52 +0000469 // If the block ends with two PPC:Bs, handle it. The second one is not
470 // executed, so remove it.
Andrew Trickc416ba62010-12-24 04:28:06 +0000471 if (SecondLastInst->getOpcode() == PPC::B &&
Dale Johannesenc6855462007-06-13 17:59:52 +0000472 LastInst->getOpcode() == PPC::B) {
Evan Cheng8f43afd2009-05-08 23:09:25 +0000473 if (!SecondLastInst->getOperand(0).isMBB())
474 return true;
Chris Lattnera5bb3702007-12-30 23:10:15 +0000475 TBB = SecondLastInst->getOperand(0).getMBB();
Dale Johannesenc6855462007-06-13 17:59:52 +0000476 I = LastInst;
Evan Cheng64dfcac2009-02-09 07:14:22 +0000477 if (AllowModify)
478 I->eraseFromParent();
Dale Johannesenc6855462007-06-13 17:59:52 +0000479 return false;
480 }
481
Chris Lattnera47294ed2006-10-13 21:21:17 +0000482 // Otherwise, can't handle this.
483 return true;
484}
485
Evan Cheng99be49d2007-05-18 00:05:48 +0000486unsigned PPCInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
Chris Lattnera47294ed2006-10-13 21:21:17 +0000487 MachineBasicBlock::iterator I = MBB.end();
Evan Cheng99be49d2007-05-18 00:05:48 +0000488 if (I == MBB.begin()) return 0;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000489 --I;
Dale Johannesen4244d122010-04-02 01:38:09 +0000490 while (I->isDebugValue()) {
491 if (I == MBB.begin())
492 return 0;
493 --I;
494 }
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000495 if (I->getOpcode() != PPC::B && I->getOpcode() != PPC::BCC &&
Hal Finkel940ab932014-02-28 00:27:01 +0000496 I->getOpcode() != PPC::BC && I->getOpcode() != PPC::BCn &&
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000497 I->getOpcode() != PPC::BDNZ8 && I->getOpcode() != PPC::BDNZ &&
498 I->getOpcode() != PPC::BDZ8 && I->getOpcode() != PPC::BDZ)
Evan Cheng99be49d2007-05-18 00:05:48 +0000499 return 0;
Andrew Trickc416ba62010-12-24 04:28:06 +0000500
Chris Lattnera47294ed2006-10-13 21:21:17 +0000501 // Remove the branch.
502 I->eraseFromParent();
Andrew Trickc416ba62010-12-24 04:28:06 +0000503
Chris Lattnera47294ed2006-10-13 21:21:17 +0000504 I = MBB.end();
505
Evan Cheng99be49d2007-05-18 00:05:48 +0000506 if (I == MBB.begin()) return 1;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000507 --I;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000508 if (I->getOpcode() != PPC::BCC &&
Hal Finkel940ab932014-02-28 00:27:01 +0000509 I->getOpcode() != PPC::BC && I->getOpcode() != PPC::BCn &&
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000510 I->getOpcode() != PPC::BDNZ8 && I->getOpcode() != PPC::BDNZ &&
511 I->getOpcode() != PPC::BDZ8 && I->getOpcode() != PPC::BDZ)
Evan Cheng99be49d2007-05-18 00:05:48 +0000512 return 1;
Andrew Trickc416ba62010-12-24 04:28:06 +0000513
Chris Lattnera47294ed2006-10-13 21:21:17 +0000514 // Remove the branch.
515 I->eraseFromParent();
Evan Cheng99be49d2007-05-18 00:05:48 +0000516 return 2;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000517}
518
Evan Cheng99be49d2007-05-18 00:05:48 +0000519unsigned
520PPCInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
521 MachineBasicBlock *FBB,
Stuart Hastings0125b642010-06-17 22:43:56 +0000522 const SmallVectorImpl<MachineOperand> &Cond,
523 DebugLoc DL) const {
Chris Lattnera61f0102006-10-17 18:06:55 +0000524 // Shouldn't be a fall through.
525 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Andrew Trickc416ba62010-12-24 04:28:06 +0000526 assert((Cond.size() == 2 || Cond.size() == 0) &&
Chris Lattner94e04442006-10-21 05:36:13 +0000527 "PPC branch conditions have two components!");
Andrew Trickc416ba62010-12-24 04:28:06 +0000528
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000529 bool isPPC64 = TM.getSubtargetImpl()->isPPC64();
530
Chris Lattner94e04442006-10-21 05:36:13 +0000531 // One-way branch.
Chris Lattnera61f0102006-10-17 18:06:55 +0000532 if (FBB == 0) {
Chris Lattner94e04442006-10-21 05:36:13 +0000533 if (Cond.empty()) // Unconditional branch
Stuart Hastings0125b642010-06-17 22:43:56 +0000534 BuildMI(&MBB, DL, get(PPC::B)).addMBB(TBB);
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000535 else if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
536 BuildMI(&MBB, DL, get(Cond[0].getImm() ?
537 (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
538 (isPPC64 ? PPC::BDZ8 : PPC::BDZ))).addMBB(TBB);
Hal Finkel940ab932014-02-28 00:27:01 +0000539 else if (Cond[0].getImm() == PPC::PRED_BIT_SET)
540 BuildMI(&MBB, DL, get(PPC::BC)).addOperand(Cond[1]).addMBB(TBB);
541 else if (Cond[0].getImm() == PPC::PRED_BIT_UNSET)
542 BuildMI(&MBB, DL, get(PPC::BCn)).addOperand(Cond[1]).addMBB(TBB);
Chris Lattner94e04442006-10-21 05:36:13 +0000543 else // Conditional branch
Stuart Hastings0125b642010-06-17 22:43:56 +0000544 BuildMI(&MBB, DL, get(PPC::BCC))
Hal Finkel940ab932014-02-28 00:27:01 +0000545 .addImm(Cond[0].getImm()).addOperand(Cond[1]).addMBB(TBB);
Evan Cheng99be49d2007-05-18 00:05:48 +0000546 return 1;
Chris Lattnera61f0102006-10-17 18:06:55 +0000547 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000548
Chris Lattnerd8816602006-10-21 05:42:09 +0000549 // Two-way Conditional Branch.
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000550 if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
551 BuildMI(&MBB, DL, get(Cond[0].getImm() ?
552 (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
553 (isPPC64 ? PPC::BDZ8 : PPC::BDZ))).addMBB(TBB);
Hal Finkel940ab932014-02-28 00:27:01 +0000554 else if (Cond[0].getImm() == PPC::PRED_BIT_SET)
555 BuildMI(&MBB, DL, get(PPC::BC)).addOperand(Cond[1]).addMBB(TBB);
556 else if (Cond[0].getImm() == PPC::PRED_BIT_UNSET)
557 BuildMI(&MBB, DL, get(PPC::BCn)).addOperand(Cond[1]).addMBB(TBB);
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000558 else
559 BuildMI(&MBB, DL, get(PPC::BCC))
Hal Finkel940ab932014-02-28 00:27:01 +0000560 .addImm(Cond[0].getImm()).addOperand(Cond[1]).addMBB(TBB);
Stuart Hastings0125b642010-06-17 22:43:56 +0000561 BuildMI(&MBB, DL, get(PPC::B)).addMBB(FBB);
Evan Cheng99be49d2007-05-18 00:05:48 +0000562 return 2;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000563}
564
Hal Finkeled6a2852013-04-05 23:29:01 +0000565// Select analysis.
566bool PPCInstrInfo::canInsertSelect(const MachineBasicBlock &MBB,
567 const SmallVectorImpl<MachineOperand> &Cond,
568 unsigned TrueReg, unsigned FalseReg,
569 int &CondCycles, int &TrueCycles, int &FalseCycles) const {
570 if (!TM.getSubtargetImpl()->hasISEL())
571 return false;
572
573 if (Cond.size() != 2)
574 return false;
575
576 // If this is really a bdnz-like condition, then it cannot be turned into a
577 // select.
578 if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
579 return false;
580
581 // Check register classes.
582 const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
583 const TargetRegisterClass *RC =
584 RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg));
585 if (!RC)
586 return false;
587
588 // isel is for regular integer GPRs only.
589 if (!PPC::GPRCRegClass.hasSubClassEq(RC) &&
Hal Finkel8e8618a2013-07-15 20:22:58 +0000590 !PPC::GPRC_NOR0RegClass.hasSubClassEq(RC) &&
591 !PPC::G8RCRegClass.hasSubClassEq(RC) &&
592 !PPC::G8RC_NOX0RegClass.hasSubClassEq(RC))
Hal Finkeled6a2852013-04-05 23:29:01 +0000593 return false;
594
595 // FIXME: These numbers are for the A2, how well they work for other cores is
596 // an open question. On the A2, the isel instruction has a 2-cycle latency
597 // but single-cycle throughput. These numbers are used in combination with
598 // the MispredictPenalty setting from the active SchedMachineModel.
599 CondCycles = 1;
600 TrueCycles = 1;
601 FalseCycles = 1;
602
603 return true;
604}
605
606void PPCInstrInfo::insertSelect(MachineBasicBlock &MBB,
607 MachineBasicBlock::iterator MI, DebugLoc dl,
608 unsigned DestReg,
609 const SmallVectorImpl<MachineOperand> &Cond,
610 unsigned TrueReg, unsigned FalseReg) const {
611 assert(Cond.size() == 2 &&
612 "PPC branch conditions have two components!");
613
614 assert(TM.getSubtargetImpl()->hasISEL() &&
615 "Cannot insert select on target without ISEL support");
616
617 // Get the register classes.
618 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
619 const TargetRegisterClass *RC =
620 RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg));
621 assert(RC && "TrueReg and FalseReg must have overlapping register classes");
Hal Finkel8e8618a2013-07-15 20:22:58 +0000622
623 bool Is64Bit = PPC::G8RCRegClass.hasSubClassEq(RC) ||
624 PPC::G8RC_NOX0RegClass.hasSubClassEq(RC);
625 assert((Is64Bit ||
626 PPC::GPRCRegClass.hasSubClassEq(RC) ||
627 PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) &&
Hal Finkeled6a2852013-04-05 23:29:01 +0000628 "isel is for regular integer GPRs only");
629
Hal Finkel8e8618a2013-07-15 20:22:58 +0000630 unsigned OpCode = Is64Bit ? PPC::ISEL8 : PPC::ISEL;
Hal Finkeled6a2852013-04-05 23:29:01 +0000631 unsigned SelectPred = Cond[0].getImm();
632
633 unsigned SubIdx;
634 bool SwapOps;
635 switch (SelectPred) {
636 default: llvm_unreachable("invalid predicate for isel");
637 case PPC::PRED_EQ: SubIdx = PPC::sub_eq; SwapOps = false; break;
638 case PPC::PRED_NE: SubIdx = PPC::sub_eq; SwapOps = true; break;
639 case PPC::PRED_LT: SubIdx = PPC::sub_lt; SwapOps = false; break;
640 case PPC::PRED_GE: SubIdx = PPC::sub_lt; SwapOps = true; break;
641 case PPC::PRED_GT: SubIdx = PPC::sub_gt; SwapOps = false; break;
642 case PPC::PRED_LE: SubIdx = PPC::sub_gt; SwapOps = true; break;
643 case PPC::PRED_UN: SubIdx = PPC::sub_un; SwapOps = false; break;
644 case PPC::PRED_NU: SubIdx = PPC::sub_un; SwapOps = true; break;
Hal Finkel940ab932014-02-28 00:27:01 +0000645 case PPC::PRED_BIT_SET: SubIdx = 0; SwapOps = false; break;
646 case PPC::PRED_BIT_UNSET: SubIdx = 0; SwapOps = true; break;
Hal Finkeled6a2852013-04-05 23:29:01 +0000647 }
648
649 unsigned FirstReg = SwapOps ? FalseReg : TrueReg,
650 SecondReg = SwapOps ? TrueReg : FalseReg;
651
652 // The first input register of isel cannot be r0. If it is a member
653 // of a register class that can be r0, then copy it first (the
654 // register allocator should eliminate the copy).
655 if (MRI.getRegClass(FirstReg)->contains(PPC::R0) ||
656 MRI.getRegClass(FirstReg)->contains(PPC::X0)) {
657 const TargetRegisterClass *FirstRC =
658 MRI.getRegClass(FirstReg)->contains(PPC::X0) ?
659 &PPC::G8RC_NOX0RegClass : &PPC::GPRC_NOR0RegClass;
660 unsigned OldFirstReg = FirstReg;
661 FirstReg = MRI.createVirtualRegister(FirstRC);
662 BuildMI(MBB, MI, dl, get(TargetOpcode::COPY), FirstReg)
663 .addReg(OldFirstReg);
664 }
665
666 BuildMI(MBB, MI, dl, get(OpCode), DestReg)
667 .addReg(FirstReg).addReg(SecondReg)
668 .addReg(Cond[1].getReg(), 0, SubIdx);
669}
670
Jakob Stoklund Olesen0d611972010-07-11 07:31:00 +0000671void PPCInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
672 MachineBasicBlock::iterator I, DebugLoc DL,
673 unsigned DestReg, unsigned SrcReg,
674 bool KillSrc) const {
Hal Finkel27774d92014-03-13 07:58:58 +0000675 // We can end up with self copies and similar things as a result of VSX copy
676 // legalization. Promote (or just ignore) them here.
677 const TargetRegisterInfo *TRI = &getRegisterInfo();
678 if (PPC::F8RCRegClass.contains(DestReg) &&
679 PPC::VSLRCRegClass.contains(SrcReg)) {
680 unsigned SuperReg =
681 TRI->getMatchingSuperReg(DestReg, PPC::sub_64, &PPC::VSRCRegClass);
682
683 if (SrcReg == SuperReg)
684 return;
685
686 DestReg = SuperReg;
687 } else if (PPC::VRRCRegClass.contains(DestReg) &&
688 PPC::VSHRCRegClass.contains(SrcReg)) {
689 unsigned SuperReg =
690 TRI->getMatchingSuperReg(DestReg, PPC::sub_128, &PPC::VSRCRegClass);
691
692 if (SrcReg == SuperReg)
693 return;
694
695 DestReg = SuperReg;
696 } else if (PPC::F8RCRegClass.contains(SrcReg) &&
697 PPC::VSLRCRegClass.contains(DestReg)) {
698 unsigned SuperReg =
699 TRI->getMatchingSuperReg(SrcReg, PPC::sub_64, &PPC::VSRCRegClass);
700
701 if (DestReg == SuperReg)
702 return;
703
704 SrcReg = SuperReg;
705 } else if (PPC::VRRCRegClass.contains(SrcReg) &&
706 PPC::VSHRCRegClass.contains(DestReg)) {
707 unsigned SuperReg =
708 TRI->getMatchingSuperReg(SrcReg, PPC::sub_128, &PPC::VSRCRegClass);
709
710 if (DestReg == SuperReg)
711 return;
712
713 SrcReg = SuperReg;
714 }
715
Jakob Stoklund Olesen0d611972010-07-11 07:31:00 +0000716 unsigned Opc;
717 if (PPC::GPRCRegClass.contains(DestReg, SrcReg))
718 Opc = PPC::OR;
719 else if (PPC::G8RCRegClass.contains(DestReg, SrcReg))
720 Opc = PPC::OR8;
721 else if (PPC::F4RCRegClass.contains(DestReg, SrcReg))
722 Opc = PPC::FMR;
723 else if (PPC::CRRCRegClass.contains(DestReg, SrcReg))
724 Opc = PPC::MCRF;
725 else if (PPC::VRRCRegClass.contains(DestReg, SrcReg))
726 Opc = PPC::VOR;
Hal Finkel27774d92014-03-13 07:58:58 +0000727 else if (PPC::VSRCRegClass.contains(DestReg, SrcReg))
Hal Finkelbbad2332014-03-24 09:36:36 +0000728 // There are two different ways this can be done:
Hal Finkel27774d92014-03-13 07:58:58 +0000729 // 1. xxlor : This has lower latency (on the P7), 2 cycles, but can only
730 // issue in VSU pipeline 0.
731 // 2. xmovdp/xmovsp: This has higher latency (on the P7), 6 cycles, but
732 // can go to either pipeline.
Hal Finkelbbad2332014-03-24 09:36:36 +0000733 // We'll always use xxlor here, because in practically all cases where
734 // copies are generated, they are close enough to some use that the
735 // lower-latency form is preferable.
Hal Finkel27774d92014-03-13 07:58:58 +0000736 Opc = PPC::XXLOR;
Jakob Stoklund Olesen0d611972010-07-11 07:31:00 +0000737 else if (PPC::CRBITRCRegClass.contains(DestReg, SrcReg))
738 Opc = PPC::CROR;
739 else
740 llvm_unreachable("Impossible reg-to-reg copy");
Owen Anderson7a73ae92007-12-31 06:32:00 +0000741
Evan Cheng6cc775f2011-06-28 19:10:37 +0000742 const MCInstrDesc &MCID = get(Opc);
743 if (MCID.getNumOperands() == 3)
744 BuildMI(MBB, I, DL, MCID, DestReg)
Jakob Stoklund Olesen0d611972010-07-11 07:31:00 +0000745 .addReg(SrcReg).addReg(SrcReg, getKillRegState(KillSrc));
746 else
Evan Cheng6cc775f2011-06-28 19:10:37 +0000747 BuildMI(MBB, I, DL, MCID, DestReg).addReg(SrcReg, getKillRegState(KillSrc));
Owen Anderson7a73ae92007-12-31 06:32:00 +0000748}
749
Hal Finkel8f6834d2011-12-05 17:55:17 +0000750// This function returns true if a CR spill is necessary and false otherwise.
Bill Wendlingc6c48fc2008-03-10 22:49:16 +0000751bool
Dan Gohman3b460302008-07-07 23:14:23 +0000752PPCInstrInfo::StoreRegToStackSlot(MachineFunction &MF,
753 unsigned SrcReg, bool isKill,
Bill Wendlingc6c48fc2008-03-10 22:49:16 +0000754 int FrameIdx,
755 const TargetRegisterClass *RC,
Hal Finkelfcc51d42013-03-17 04:43:44 +0000756 SmallVectorImpl<MachineInstr*> &NewMIs,
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000757 bool &NonRI, bool &SpillsVRS) const{
Hal Finkel37714b82013-03-27 21:21:15 +0000758 // Note: If additional store instructions are added here,
759 // update isStoreToStackSlot.
760
Chris Lattner6f306d72010-04-02 20:16:16 +0000761 DebugLoc DL;
Hal Finkel4e703bc2014-01-28 05:32:58 +0000762 if (PPC::GPRCRegClass.hasSubClassEq(RC) ||
763 PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) {
Hal Finkel794e05b2013-03-23 17:14:27 +0000764 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
765 .addReg(SrcReg,
766 getKillRegState(isKill)),
767 FrameIdx));
Hal Finkel4e703bc2014-01-28 05:32:58 +0000768 } else if (PPC::G8RCRegClass.hasSubClassEq(RC) ||
769 PPC::G8RC_NOX0RegClass.hasSubClassEq(RC)) {
Hal Finkel794e05b2013-03-23 17:14:27 +0000770 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STD))
771 .addReg(SrcReg,
772 getKillRegState(isKill)),
773 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000774 } else if (PPC::F8RCRegClass.hasSubClassEq(RC)) {
Dale Johannesen6b8c76a2009-02-12 23:08:38 +0000775 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFD))
Bill Wendlingf7b83c72009-05-13 21:33:08 +0000776 .addReg(SrcReg,
777 getKillRegState(isKill)),
778 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000779 } else if (PPC::F4RCRegClass.hasSubClassEq(RC)) {
Dale Johannesen6b8c76a2009-02-12 23:08:38 +0000780 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFS))
Bill Wendlingf7b83c72009-05-13 21:33:08 +0000781 .addReg(SrcReg,
782 getKillRegState(isKill)),
783 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000784 } else if (PPC::CRRCRegClass.hasSubClassEq(RC)) {
Hal Finkele154c8f2013-03-12 14:12:16 +0000785 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CR))
786 .addReg(SrcReg,
787 getKillRegState(isKill)),
788 FrameIdx));
789 return true;
Craig Topperabadc662012-04-20 06:31:50 +0000790 } else if (PPC::CRBITRCRegClass.hasSubClassEq(RC)) {
Hal Finkel940ab932014-02-28 00:27:01 +0000791 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CRBIT))
792 .addReg(SrcReg,
793 getKillRegState(isKill)),
794 FrameIdx));
795 return true;
Craig Topperabadc662012-04-20 06:31:50 +0000796 } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) {
Hal Finkelfcc51d42013-03-17 04:43:44 +0000797 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STVX))
798 .addReg(SrcReg,
799 getKillRegState(isKill)),
800 FrameIdx));
801 NonRI = true;
Hal Finkel27774d92014-03-13 07:58:58 +0000802 } else if (PPC::VSRCRegClass.hasSubClassEq(RC)) {
803 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STXVD2X))
804 .addReg(SrcReg,
805 getKillRegState(isKill)),
806 FrameIdx));
807 NonRI = true;
Hal Finkela1431df2013-03-21 19:03:21 +0000808 } else if (PPC::VRSAVERCRegClass.hasSubClassEq(RC)) {
Hal Finkela7b06302013-03-27 00:02:20 +0000809 assert(TM.getSubtargetImpl()->isDarwin() &&
810 "VRSAVE only needs spill/restore on Darwin");
Hal Finkela1431df2013-03-21 19:03:21 +0000811 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_VRSAVE))
812 .addReg(SrcReg,
813 getKillRegState(isKill)),
814 FrameIdx));
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000815 SpillsVRS = true;
Owen Andersoneee14602008-01-01 21:11:32 +0000816 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000817 llvm_unreachable("Unknown regclass!");
Owen Andersoneee14602008-01-01 21:11:32 +0000818 }
Bill Wendling632ea652008-03-03 22:19:16 +0000819
820 return false;
Owen Andersoneee14602008-01-01 21:11:32 +0000821}
822
823void
824PPCInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
Bill Wendling632ea652008-03-03 22:19:16 +0000825 MachineBasicBlock::iterator MI,
826 unsigned SrcReg, bool isKill, int FrameIdx,
Evan Chengefb126a2010-05-06 19:06:44 +0000827 const TargetRegisterClass *RC,
828 const TargetRegisterInfo *TRI) const {
Dan Gohman3b460302008-07-07 23:14:23 +0000829 MachineFunction &MF = *MBB.getParent();
Owen Andersoneee14602008-01-01 21:11:32 +0000830 SmallVector<MachineInstr*, 4> NewMIs;
Bill Wendling632ea652008-03-03 22:19:16 +0000831
Hal Finkelbb420f12013-03-15 05:06:04 +0000832 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
833 FuncInfo->setHasSpills();
834
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000835 bool NonRI = false, SpillsVRS = false;
836 if (StoreRegToStackSlot(MF, SrcReg, isKill, FrameIdx, RC, NewMIs,
837 NonRI, SpillsVRS))
Bill Wendling632ea652008-03-03 22:19:16 +0000838 FuncInfo->setSpillsCR();
Bill Wendling632ea652008-03-03 22:19:16 +0000839
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000840 if (SpillsVRS)
841 FuncInfo->setSpillsVRSAVE();
842
Hal Finkelfcc51d42013-03-17 04:43:44 +0000843 if (NonRI)
844 FuncInfo->setHasNonRISpills();
845
Owen Andersoneee14602008-01-01 21:11:32 +0000846 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
847 MBB.insert(MI, NewMIs[i]);
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +0000848
849 const MachineFrameInfo &MFI = *MF.getFrameInfo();
850 MachineMemOperand *MMO =
Jay Foad465101b2011-11-15 07:34:52 +0000851 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx),
Chris Lattnere3d864b2010-09-21 04:39:43 +0000852 MachineMemOperand::MOStore,
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +0000853 MFI.getObjectSize(FrameIdx),
854 MFI.getObjectAlignment(FrameIdx));
855 NewMIs.back()->addMemOperand(MF, MMO);
Owen Andersoneee14602008-01-01 21:11:32 +0000856}
857
Hal Finkelbde7f8f2011-12-06 20:55:36 +0000858bool
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000859PPCInstrInfo::LoadRegFromStackSlot(MachineFunction &MF, DebugLoc DL,
Dan Gohman3b460302008-07-07 23:14:23 +0000860 unsigned DestReg, int FrameIdx,
Bill Wendlingc6c48fc2008-03-10 22:49:16 +0000861 const TargetRegisterClass *RC,
Hal Finkelfcc51d42013-03-17 04:43:44 +0000862 SmallVectorImpl<MachineInstr*> &NewMIs,
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000863 bool &NonRI, bool &SpillsVRS) const{
Hal Finkel37714b82013-03-27 21:21:15 +0000864 // Note: If additional load instructions are added here,
865 // update isLoadFromStackSlot.
866
Hal Finkel4e703bc2014-01-28 05:32:58 +0000867 if (PPC::GPRCRegClass.hasSubClassEq(RC) ||
868 PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) {
Hal Finkel5791f512013-03-27 19:10:40 +0000869 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ),
870 DestReg), FrameIdx));
Hal Finkel4e703bc2014-01-28 05:32:58 +0000871 } else if (PPC::G8RCRegClass.hasSubClassEq(RC) ||
872 PPC::G8RC_NOX0RegClass.hasSubClassEq(RC)) {
Hal Finkel5791f512013-03-27 19:10:40 +0000873 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LD), DestReg),
874 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000875 } else if (PPC::F8RCRegClass.hasSubClassEq(RC)) {
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000876 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFD), DestReg),
Owen Andersoneee14602008-01-01 21:11:32 +0000877 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000878 } else if (PPC::F4RCRegClass.hasSubClassEq(RC)) {
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000879 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFS), DestReg),
Owen Andersoneee14602008-01-01 21:11:32 +0000880 FrameIdx));
Craig Topperabadc662012-04-20 06:31:50 +0000881 } else if (PPC::CRRCRegClass.hasSubClassEq(RC)) {
Hal Finkele154c8f2013-03-12 14:12:16 +0000882 NewMIs.push_back(addFrameReference(BuildMI(MF, DL,
883 get(PPC::RESTORE_CR), DestReg),
884 FrameIdx));
885 return true;
Craig Topperabadc662012-04-20 06:31:50 +0000886 } else if (PPC::CRBITRCRegClass.hasSubClassEq(RC)) {
Hal Finkel940ab932014-02-28 00:27:01 +0000887 NewMIs.push_back(addFrameReference(BuildMI(MF, DL,
888 get(PPC::RESTORE_CRBIT), DestReg),
889 FrameIdx));
890 return true;
Craig Topperabadc662012-04-20 06:31:50 +0000891 } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) {
Hal Finkelfcc51d42013-03-17 04:43:44 +0000892 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LVX), DestReg),
893 FrameIdx));
894 NonRI = true;
Hal Finkel27774d92014-03-13 07:58:58 +0000895 } else if (PPC::VSRCRegClass.hasSubClassEq(RC)) {
896 NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LXVD2X), DestReg),
897 FrameIdx));
898 NonRI = true;
Hal Finkela1431df2013-03-21 19:03:21 +0000899 } else if (PPC::VRSAVERCRegClass.hasSubClassEq(RC)) {
Hal Finkela7b06302013-03-27 00:02:20 +0000900 assert(TM.getSubtargetImpl()->isDarwin() &&
901 "VRSAVE only needs spill/restore on Darwin");
Hal Finkela1431df2013-03-21 19:03:21 +0000902 NewMIs.push_back(addFrameReference(BuildMI(MF, DL,
903 get(PPC::RESTORE_VRSAVE),
904 DestReg),
905 FrameIdx));
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000906 SpillsVRS = true;
Owen Andersoneee14602008-01-01 21:11:32 +0000907 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000908 llvm_unreachable("Unknown regclass!");
Owen Andersoneee14602008-01-01 21:11:32 +0000909 }
Hal Finkelbde7f8f2011-12-06 20:55:36 +0000910
911 return false;
Owen Andersoneee14602008-01-01 21:11:32 +0000912}
913
914void
915PPCInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
Bill Wendling632ea652008-03-03 22:19:16 +0000916 MachineBasicBlock::iterator MI,
917 unsigned DestReg, int FrameIdx,
Evan Chengefb126a2010-05-06 19:06:44 +0000918 const TargetRegisterClass *RC,
919 const TargetRegisterInfo *TRI) const {
Dan Gohman3b460302008-07-07 23:14:23 +0000920 MachineFunction &MF = *MBB.getParent();
Owen Andersoneee14602008-01-01 21:11:32 +0000921 SmallVector<MachineInstr*, 4> NewMIs;
Chris Lattner6f306d72010-04-02 20:16:16 +0000922 DebugLoc DL;
Bill Wendlingf6d609a2009-02-12 00:02:55 +0000923 if (MI != MBB.end()) DL = MI->getDebugLoc();
Hal Finkelfcc51d42013-03-17 04:43:44 +0000924
925 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
926 FuncInfo->setHasSpills();
927
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000928 bool NonRI = false, SpillsVRS = false;
929 if (LoadRegFromStackSlot(MF, DL, DestReg, FrameIdx, RC, NewMIs,
930 NonRI, SpillsVRS))
Hal Finkelbde7f8f2011-12-06 20:55:36 +0000931 FuncInfo->setSpillsCR();
Hal Finkelfcc51d42013-03-17 04:43:44 +0000932
Hal Finkelcc1eeda2013-03-23 22:06:03 +0000933 if (SpillsVRS)
934 FuncInfo->setSpillsVRSAVE();
935
Hal Finkelfcc51d42013-03-17 04:43:44 +0000936 if (NonRI)
937 FuncInfo->setHasNonRISpills();
938
Owen Andersoneee14602008-01-01 21:11:32 +0000939 for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
940 MBB.insert(MI, NewMIs[i]);
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +0000941
942 const MachineFrameInfo &MFI = *MF.getFrameInfo();
943 MachineMemOperand *MMO =
Jay Foad465101b2011-11-15 07:34:52 +0000944 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx),
Chris Lattnere3d864b2010-09-21 04:39:43 +0000945 MachineMemOperand::MOLoad,
Jakob Stoklund Olesen6353e532010-07-16 18:22:00 +0000946 MFI.getObjectSize(FrameIdx),
947 MFI.getObjectAlignment(FrameIdx));
948 NewMIs.back()->addMemOperand(MF, MMO);
Owen Andersoneee14602008-01-01 21:11:32 +0000949}
950
Chris Lattnera47294ed2006-10-13 21:21:17 +0000951bool PPCInstrInfo::
Owen Anderson4f6bf042008-08-14 22:49:33 +0000952ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
Chris Lattner23f22de2006-10-21 06:03:11 +0000953 assert(Cond.size() == 2 && "Invalid PPC branch opcode!");
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000954 if (Cond[1].getReg() == PPC::CTR8 || Cond[1].getReg() == PPC::CTR)
955 Cond[0].setImm(Cond[0].getImm() == 0 ? 1 : 0);
956 else
957 // Leave the CR# the same, but invert the condition.
958 Cond[0].setImm(PPC::InvertPredicate((PPC::Predicate)Cond[0].getImm()));
Chris Lattner23f22de2006-10-21 06:03:11 +0000959 return false;
Chris Lattnera47294ed2006-10-13 21:21:17 +0000960}
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +0000961
Hal Finkeld61d4f82013-04-06 19:30:30 +0000962bool PPCInstrInfo::FoldImmediate(MachineInstr *UseMI, MachineInstr *DefMI,
963 unsigned Reg, MachineRegisterInfo *MRI) const {
964 // For some instructions, it is legal to fold ZERO into the RA register field.
965 // A zero immediate should always be loaded with a single li.
966 unsigned DefOpc = DefMI->getOpcode();
967 if (DefOpc != PPC::LI && DefOpc != PPC::LI8)
968 return false;
969 if (!DefMI->getOperand(1).isImm())
970 return false;
971 if (DefMI->getOperand(1).getImm() != 0)
972 return false;
973
974 // Note that we cannot here invert the arguments of an isel in order to fold
975 // a ZERO into what is presented as the second argument. All we have here
976 // is the condition bit, and that might come from a CR-logical bit operation.
977
978 const MCInstrDesc &UseMCID = UseMI->getDesc();
979
980 // Only fold into real machine instructions.
981 if (UseMCID.isPseudo())
982 return false;
983
984 unsigned UseIdx;
985 for (UseIdx = 0; UseIdx < UseMI->getNumOperands(); ++UseIdx)
986 if (UseMI->getOperand(UseIdx).isReg() &&
987 UseMI->getOperand(UseIdx).getReg() == Reg)
988 break;
989
990 assert(UseIdx < UseMI->getNumOperands() && "Cannot find Reg in UseMI");
991 assert(UseIdx < UseMCID.getNumOperands() && "No operand description for Reg");
992
993 const MCOperandInfo *UseInfo = &UseMCID.OpInfo[UseIdx];
994
995 // We can fold the zero if this register requires a GPRC_NOR0/G8RC_NOX0
996 // register (which might also be specified as a pointer class kind).
997 if (UseInfo->isLookupPtrRegClass()) {
998 if (UseInfo->RegClass /* Kind */ != 1)
999 return false;
1000 } else {
1001 if (UseInfo->RegClass != PPC::GPRC_NOR0RegClassID &&
1002 UseInfo->RegClass != PPC::G8RC_NOX0RegClassID)
1003 return false;
1004 }
1005
1006 // Make sure this is not tied to an output register (or otherwise
1007 // constrained). This is true for ST?UX registers, for example, which
1008 // are tied to their output registers.
1009 if (UseInfo->Constraints != 0)
1010 return false;
1011
1012 unsigned ZeroReg;
1013 if (UseInfo->isLookupPtrRegClass()) {
1014 bool isPPC64 = TM.getSubtargetImpl()->isPPC64();
1015 ZeroReg = isPPC64 ? PPC::ZERO8 : PPC::ZERO;
1016 } else {
1017 ZeroReg = UseInfo->RegClass == PPC::G8RC_NOX0RegClassID ?
1018 PPC::ZERO8 : PPC::ZERO;
1019 }
1020
1021 bool DeleteDef = MRI->hasOneNonDBGUse(Reg);
1022 UseMI->getOperand(UseIdx).setReg(ZeroReg);
1023
1024 if (DeleteDef)
1025 DefMI->eraseFromParent();
1026
1027 return true;
1028}
1029
Hal Finkel30ae2292013-04-10 18:30:16 +00001030static bool MBBDefinesCTR(MachineBasicBlock &MBB) {
1031 for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end();
1032 I != IE; ++I)
1033 if (I->definesRegister(PPC::CTR) || I->definesRegister(PPC::CTR8))
1034 return true;
1035 return false;
1036}
1037
1038// We should make sure that, if we're going to predicate both sides of a
1039// condition (a diamond), that both sides don't define the counter register. We
1040// can predicate counter-decrement-based branches, but while that predicates
1041// the branching, it does not predicate the counter decrement. If we tried to
1042// merge the triangle into one predicated block, we'd decrement the counter
1043// twice.
1044bool PPCInstrInfo::isProfitableToIfCvt(MachineBasicBlock &TMBB,
1045 unsigned NumT, unsigned ExtraT,
1046 MachineBasicBlock &FMBB,
1047 unsigned NumF, unsigned ExtraF,
1048 const BranchProbability &Probability) const {
1049 return !(MBBDefinesCTR(TMBB) && MBBDefinesCTR(FMBB));
1050}
1051
1052
Hal Finkel5711eca2013-04-09 22:58:37 +00001053bool PPCInstrInfo::isPredicated(const MachineInstr *MI) const {
Hal Finkelf29285a2013-04-11 01:23:34 +00001054 // The predicated branches are identified by their type, not really by the
1055 // explicit presence of a predicate. Furthermore, some of them can be
1056 // predicated more than once. Because if conversion won't try to predicate
1057 // any instruction which already claims to be predicated (by returning true
1058 // here), always return false. In doing so, we let isPredicable() be the
1059 // final word on whether not the instruction can be (further) predicated.
1060
1061 return false;
Hal Finkel5711eca2013-04-09 22:58:37 +00001062}
1063
1064bool PPCInstrInfo::isUnpredicatedTerminator(const MachineInstr *MI) const {
1065 if (!MI->isTerminator())
1066 return false;
1067
1068 // Conditional branch is a special case.
1069 if (MI->isBranch() && !MI->isBarrier())
1070 return true;
1071
1072 return !isPredicated(MI);
1073}
1074
1075bool PPCInstrInfo::PredicateInstruction(
1076 MachineInstr *MI,
1077 const SmallVectorImpl<MachineOperand> &Pred) const {
1078 unsigned OpC = MI->getOpcode();
1079 if (OpC == PPC::BLR) {
1080 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) {
1081 bool isPPC64 = TM.getSubtargetImpl()->isPPC64();
1082 MI->setDesc(get(Pred[0].getImm() ?
1083 (isPPC64 ? PPC::BDNZLR8 : PPC::BDNZLR) :
1084 (isPPC64 ? PPC::BDZLR8 : PPC::BDZLR)));
Hal Finkel940ab932014-02-28 00:27:01 +00001085 } else if (Pred[0].getImm() == PPC::PRED_BIT_SET) {
Hal Finkel5711eca2013-04-09 22:58:37 +00001086 MI->setDesc(get(PPC::BCLR));
1087 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
Hal Finkel940ab932014-02-28 00:27:01 +00001088 .addReg(Pred[1].getReg());
1089 } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) {
1090 MI->setDesc(get(PPC::BCLRn));
1091 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1092 .addReg(Pred[1].getReg());
1093 } else {
1094 MI->setDesc(get(PPC::BCCLR));
1095 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
Hal Finkel5711eca2013-04-09 22:58:37 +00001096 .addImm(Pred[0].getImm())
1097 .addReg(Pred[1].getReg());
1098 }
1099
1100 return true;
1101 } else if (OpC == PPC::B) {
1102 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) {
1103 bool isPPC64 = TM.getSubtargetImpl()->isPPC64();
1104 MI->setDesc(get(Pred[0].getImm() ?
1105 (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
1106 (isPPC64 ? PPC::BDZ8 : PPC::BDZ)));
Hal Finkel940ab932014-02-28 00:27:01 +00001107 } else if (Pred[0].getImm() == PPC::PRED_BIT_SET) {
1108 MachineBasicBlock *MBB = MI->getOperand(0).getMBB();
1109 MI->RemoveOperand(0);
1110
1111 MI->setDesc(get(PPC::BC));
1112 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1113 .addReg(Pred[1].getReg())
1114 .addMBB(MBB);
1115 } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) {
1116 MachineBasicBlock *MBB = MI->getOperand(0).getMBB();
1117 MI->RemoveOperand(0);
1118
1119 MI->setDesc(get(PPC::BCn));
1120 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1121 .addReg(Pred[1].getReg())
1122 .addMBB(MBB);
Hal Finkel5711eca2013-04-09 22:58:37 +00001123 } else {
1124 MachineBasicBlock *MBB = MI->getOperand(0).getMBB();
1125 MI->RemoveOperand(0);
1126
1127 MI->setDesc(get(PPC::BCC));
1128 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1129 .addImm(Pred[0].getImm())
1130 .addReg(Pred[1].getReg())
1131 .addMBB(MBB);
1132 }
1133
1134 return true;
Hal Finkel500b0042013-04-10 06:42:34 +00001135 } else if (OpC == PPC::BCTR || OpC == PPC::BCTR8 ||
1136 OpC == PPC::BCTRL || OpC == PPC::BCTRL8) {
1137 if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR)
1138 llvm_unreachable("Cannot predicate bctr[l] on the ctr register");
1139
1140 bool setLR = OpC == PPC::BCTRL || OpC == PPC::BCTRL8;
1141 bool isPPC64 = TM.getSubtargetImpl()->isPPC64();
Hal Finkel940ab932014-02-28 00:27:01 +00001142
1143 if (Pred[0].getImm() == PPC::PRED_BIT_SET) {
1144 MI->setDesc(get(isPPC64 ? (setLR ? PPC::BCCTRL8 : PPC::BCCTR8) :
1145 (setLR ? PPC::BCCTRL : PPC::BCCTR)));
1146 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1147 .addReg(Pred[1].getReg());
1148 return true;
1149 } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) {
1150 MI->setDesc(get(isPPC64 ? (setLR ? PPC::BCCTRL8n : PPC::BCCTR8n) :
1151 (setLR ? PPC::BCCTRLn : PPC::BCCTRn)));
1152 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1153 .addReg(Pred[1].getReg());
1154 return true;
1155 }
1156
1157 MI->setDesc(get(isPPC64 ? (setLR ? PPC::BCCCTRL8 : PPC::BCCCTR8) :
1158 (setLR ? PPC::BCCCTRL : PPC::BCCCTR)));
Hal Finkel500b0042013-04-10 06:42:34 +00001159 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1160 .addImm(Pred[0].getImm())
1161 .addReg(Pred[1].getReg());
1162 return true;
Hal Finkel5711eca2013-04-09 22:58:37 +00001163 }
1164
1165 return false;
1166}
1167
1168bool PPCInstrInfo::SubsumesPredicate(
1169 const SmallVectorImpl<MachineOperand> &Pred1,
1170 const SmallVectorImpl<MachineOperand> &Pred2) const {
1171 assert(Pred1.size() == 2 && "Invalid PPC first predicate");
1172 assert(Pred2.size() == 2 && "Invalid PPC second predicate");
1173
1174 if (Pred1[1].getReg() == PPC::CTR8 || Pred1[1].getReg() == PPC::CTR)
1175 return false;
1176 if (Pred2[1].getReg() == PPC::CTR8 || Pred2[1].getReg() == PPC::CTR)
1177 return false;
1178
Hal Finkel94a6f382013-12-11 23:12:25 +00001179 // P1 can only subsume P2 if they test the same condition register.
1180 if (Pred1[1].getReg() != Pred2[1].getReg())
1181 return false;
1182
Hal Finkel5711eca2013-04-09 22:58:37 +00001183 PPC::Predicate P1 = (PPC::Predicate) Pred1[0].getImm();
1184 PPC::Predicate P2 = (PPC::Predicate) Pred2[0].getImm();
1185
1186 if (P1 == P2)
1187 return true;
1188
1189 // Does P1 subsume P2, e.g. GE subsumes GT.
1190 if (P1 == PPC::PRED_LE &&
1191 (P2 == PPC::PRED_LT || P2 == PPC::PRED_EQ))
1192 return true;
1193 if (P1 == PPC::PRED_GE &&
1194 (P2 == PPC::PRED_GT || P2 == PPC::PRED_EQ))
1195 return true;
1196
1197 return false;
1198}
1199
1200bool PPCInstrInfo::DefinesPredicate(MachineInstr *MI,
1201 std::vector<MachineOperand> &Pred) const {
1202 // Note: At the present time, the contents of Pred from this function is
1203 // unused by IfConversion. This implementation follows ARM by pushing the
1204 // CR-defining operand. Because the 'DZ' and 'DNZ' count as types of
1205 // predicate, instructions defining CTR or CTR8 are also included as
1206 // predicate-defining instructions.
1207
1208 const TargetRegisterClass *RCs[] =
1209 { &PPC::CRRCRegClass, &PPC::CRBITRCRegClass,
1210 &PPC::CTRRCRegClass, &PPC::CTRRC8RegClass };
1211
1212 bool Found = false;
1213 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1214 const MachineOperand &MO = MI->getOperand(i);
Hal Finkelaf822012013-04-10 07:17:47 +00001215 for (unsigned c = 0; c < array_lengthof(RCs) && !Found; ++c) {
Hal Finkel5711eca2013-04-09 22:58:37 +00001216 const TargetRegisterClass *RC = RCs[c];
Hal Finkelaf822012013-04-10 07:17:47 +00001217 if (MO.isReg()) {
1218 if (MO.isDef() && RC->contains(MO.getReg())) {
Hal Finkel5711eca2013-04-09 22:58:37 +00001219 Pred.push_back(MO);
1220 Found = true;
1221 }
Hal Finkelaf822012013-04-10 07:17:47 +00001222 } else if (MO.isRegMask()) {
1223 for (TargetRegisterClass::iterator I = RC->begin(),
1224 IE = RC->end(); I != IE; ++I)
1225 if (MO.clobbersPhysReg(*I)) {
1226 Pred.push_back(MO);
1227 Found = true;
1228 }
Hal Finkel5711eca2013-04-09 22:58:37 +00001229 }
1230 }
1231 }
1232
1233 return Found;
1234}
1235
1236bool PPCInstrInfo::isPredicable(MachineInstr *MI) const {
1237 unsigned OpC = MI->getOpcode();
1238 switch (OpC) {
1239 default:
1240 return false;
1241 case PPC::B:
1242 case PPC::BLR:
Hal Finkel500b0042013-04-10 06:42:34 +00001243 case PPC::BCTR:
1244 case PPC::BCTR8:
1245 case PPC::BCTRL:
1246 case PPC::BCTRL8:
Hal Finkel5711eca2013-04-09 22:58:37 +00001247 return true;
1248 }
1249}
1250
Hal Finkel82656cb2013-04-18 22:15:08 +00001251bool PPCInstrInfo::analyzeCompare(const MachineInstr *MI,
1252 unsigned &SrcReg, unsigned &SrcReg2,
1253 int &Mask, int &Value) const {
1254 unsigned Opc = MI->getOpcode();
1255
1256 switch (Opc) {
1257 default: return false;
1258 case PPC::CMPWI:
1259 case PPC::CMPLWI:
1260 case PPC::CMPDI:
1261 case PPC::CMPLDI:
1262 SrcReg = MI->getOperand(1).getReg();
1263 SrcReg2 = 0;
1264 Value = MI->getOperand(2).getImm();
1265 Mask = 0xFFFF;
1266 return true;
1267 case PPC::CMPW:
1268 case PPC::CMPLW:
1269 case PPC::CMPD:
1270 case PPC::CMPLD:
1271 case PPC::FCMPUS:
1272 case PPC::FCMPUD:
1273 SrcReg = MI->getOperand(1).getReg();
1274 SrcReg2 = MI->getOperand(2).getReg();
1275 return true;
1276 }
1277}
Hal Finkele6322392013-04-19 22:08:38 +00001278
Hal Finkel82656cb2013-04-18 22:15:08 +00001279bool PPCInstrInfo::optimizeCompareInstr(MachineInstr *CmpInstr,
1280 unsigned SrcReg, unsigned SrcReg2,
1281 int Mask, int Value,
1282 const MachineRegisterInfo *MRI) const {
Hal Finkelb12da6b2013-04-18 22:54:25 +00001283 if (DisableCmpOpt)
1284 return false;
1285
Hal Finkel82656cb2013-04-18 22:15:08 +00001286 int OpC = CmpInstr->getOpcode();
1287 unsigned CRReg = CmpInstr->getOperand(0).getReg();
Hal Finkel08e53ee2013-05-08 12:16:14 +00001288
1289 // FP record forms set CR1 based on the execption status bits, not a
1290 // comparison with zero.
1291 if (OpC == PPC::FCMPUS || OpC == PPC::FCMPUD)
1292 return false;
Hal Finkel82656cb2013-04-18 22:15:08 +00001293
1294 // The record forms set the condition register based on a signed comparison
1295 // with zero (so says the ISA manual). This is not as straightforward as it
1296 // seems, however, because this is always a 64-bit comparison on PPC64, even
1297 // for instructions that are 32-bit in nature (like slw for example).
1298 // So, on PPC32, for unsigned comparisons, we can use the record forms only
1299 // for equality checks (as those don't depend on the sign). On PPC64,
1300 // we are restricted to equality for unsigned 64-bit comparisons and for
1301 // signed 32-bit comparisons the applicability is more restricted.
1302 bool isPPC64 = TM.getSubtargetImpl()->isPPC64();
1303 bool is32BitSignedCompare = OpC == PPC::CMPWI || OpC == PPC::CMPW;
1304 bool is32BitUnsignedCompare = OpC == PPC::CMPLWI || OpC == PPC::CMPLW;
1305 bool is64BitUnsignedCompare = OpC == PPC::CMPLDI || OpC == PPC::CMPLD;
1306
1307 // Get the unique definition of SrcReg.
1308 MachineInstr *MI = MRI->getUniqueVRegDef(SrcReg);
1309 if (!MI) return false;
1310 int MIOpC = MI->getOpcode();
1311
1312 bool equalityOnly = false;
1313 bool noSub = false;
1314 if (isPPC64) {
1315 if (is32BitSignedCompare) {
1316 // We can perform this optimization only if MI is sign-extending.
1317 if (MIOpC == PPC::SRAW || MIOpC == PPC::SRAWo ||
1318 MIOpC == PPC::SRAWI || MIOpC == PPC::SRAWIo ||
1319 MIOpC == PPC::EXTSB || MIOpC == PPC::EXTSBo ||
1320 MIOpC == PPC::EXTSH || MIOpC == PPC::EXTSHo ||
1321 MIOpC == PPC::EXTSW || MIOpC == PPC::EXTSWo) {
1322 noSub = true;
1323 } else
1324 return false;
1325 } else if (is32BitUnsignedCompare) {
1326 // We can perform this optimization, equality only, if MI is
1327 // zero-extending.
1328 if (MIOpC == PPC::CNTLZW || MIOpC == PPC::CNTLZWo ||
1329 MIOpC == PPC::SLW || MIOpC == PPC::SLWo ||
1330 MIOpC == PPC::SRW || MIOpC == PPC::SRWo) {
1331 noSub = true;
1332 equalityOnly = true;
1333 } else
1334 return false;
Hal Finkel08e53ee2013-05-08 12:16:14 +00001335 } else
Hal Finkel82656cb2013-04-18 22:15:08 +00001336 equalityOnly = is64BitUnsignedCompare;
Hal Finkel08e53ee2013-05-08 12:16:14 +00001337 } else
Hal Finkel82656cb2013-04-18 22:15:08 +00001338 equalityOnly = is32BitUnsignedCompare;
1339
1340 if (equalityOnly) {
1341 // We need to check the uses of the condition register in order to reject
1342 // non-equality comparisons.
Owen Anderson16c6bf42014-03-13 23:12:04 +00001343 for (MachineRegisterInfo::use_instr_iterator I =MRI->use_instr_begin(CRReg),
1344 IE = MRI->use_instr_end(); I != IE; ++I) {
Hal Finkel82656cb2013-04-18 22:15:08 +00001345 MachineInstr *UseMI = &*I;
1346 if (UseMI->getOpcode() == PPC::BCC) {
1347 unsigned Pred = UseMI->getOperand(0).getImm();
Hal Finkelc3632452013-05-07 17:49:55 +00001348 if (Pred != PPC::PRED_EQ && Pred != PPC::PRED_NE)
1349 return false;
Hal Finkel82656cb2013-04-18 22:15:08 +00001350 } else if (UseMI->getOpcode() == PPC::ISEL ||
1351 UseMI->getOpcode() == PPC::ISEL8) {
1352 unsigned SubIdx = UseMI->getOperand(3).getSubReg();
Hal Finkelc3632452013-05-07 17:49:55 +00001353 if (SubIdx != PPC::sub_eq)
1354 return false;
Hal Finkel82656cb2013-04-18 22:15:08 +00001355 } else
1356 return false;
1357 }
1358 }
1359
Hal Finkelc3632452013-05-07 17:49:55 +00001360 MachineBasicBlock::iterator I = CmpInstr;
Hal Finkel82656cb2013-04-18 22:15:08 +00001361
1362 // Scan forward to find the first use of the compare.
1363 for (MachineBasicBlock::iterator EL = CmpInstr->getParent()->end();
1364 I != EL; ++I) {
1365 bool FoundUse = false;
Owen Anderson16c6bf42014-03-13 23:12:04 +00001366 for (MachineRegisterInfo::use_instr_iterator J =MRI->use_instr_begin(CRReg),
1367 JE = MRI->use_instr_end(); J != JE; ++J)
Hal Finkel82656cb2013-04-18 22:15:08 +00001368 if (&*J == &*I) {
1369 FoundUse = true;
1370 break;
1371 }
1372
1373 if (FoundUse)
1374 break;
1375 }
1376
Hal Finkel82656cb2013-04-18 22:15:08 +00001377 // There are two possible candidates which can be changed to set CR[01].
1378 // One is MI, the other is a SUB instruction.
1379 // For CMPrr(r1,r2), we are looking for SUB(r1,r2) or SUB(r2,r1).
1380 MachineInstr *Sub = NULL;
1381 if (SrcReg2 != 0)
1382 // MI is not a candidate for CMPrr.
1383 MI = NULL;
1384 // FIXME: Conservatively refuse to convert an instruction which isn't in the
1385 // same BB as the comparison. This is to allow the check below to avoid calls
1386 // (and other explicit clobbers); instead we should really check for these
1387 // more explicitly (in at least a few predecessors).
1388 else if (MI->getParent() != CmpInstr->getParent() || Value != 0) {
1389 // PPC does not have a record-form SUBri.
1390 return false;
1391 }
1392
1393 // Search for Sub.
1394 const TargetRegisterInfo *TRI = &getRegisterInfo();
1395 --I;
Hal Finkelc3632452013-05-07 17:49:55 +00001396
1397 // Get ready to iterate backward from CmpInstr.
1398 MachineBasicBlock::iterator E = MI,
1399 B = CmpInstr->getParent()->begin();
1400
Hal Finkel82656cb2013-04-18 22:15:08 +00001401 for (; I != E && !noSub; --I) {
1402 const MachineInstr &Instr = *I;
1403 unsigned IOpC = Instr.getOpcode();
1404
1405 if (&*I != CmpInstr && (
Hal Finkel08e53ee2013-05-08 12:16:14 +00001406 Instr.modifiesRegister(PPC::CR0, TRI) ||
1407 Instr.readsRegister(PPC::CR0, TRI)))
Hal Finkel82656cb2013-04-18 22:15:08 +00001408 // This instruction modifies or uses the record condition register after
1409 // the one we want to change. While we could do this transformation, it
1410 // would likely not be profitable. This transformation removes one
1411 // instruction, and so even forcing RA to generate one move probably
1412 // makes it unprofitable.
1413 return false;
1414
1415 // Check whether CmpInstr can be made redundant by the current instruction.
1416 if ((OpC == PPC::CMPW || OpC == PPC::CMPLW ||
1417 OpC == PPC::CMPD || OpC == PPC::CMPLD) &&
1418 (IOpC == PPC::SUBF || IOpC == PPC::SUBF8) &&
1419 ((Instr.getOperand(1).getReg() == SrcReg &&
1420 Instr.getOperand(2).getReg() == SrcReg2) ||
1421 (Instr.getOperand(1).getReg() == SrcReg2 &&
1422 Instr.getOperand(2).getReg() == SrcReg))) {
1423 Sub = &*I;
1424 break;
1425 }
1426
Hal Finkel82656cb2013-04-18 22:15:08 +00001427 if (I == B)
1428 // The 'and' is below the comparison instruction.
1429 return false;
1430 }
1431
1432 // Return false if no candidates exist.
1433 if (!MI && !Sub)
1434 return false;
1435
1436 // The single candidate is called MI.
1437 if (!MI) MI = Sub;
1438
1439 int NewOpC = -1;
1440 MIOpC = MI->getOpcode();
1441 if (MIOpC == PPC::ANDIo || MIOpC == PPC::ANDIo8)
1442 NewOpC = MIOpC;
1443 else {
1444 NewOpC = PPC::getRecordFormOpcode(MIOpC);
1445 if (NewOpC == -1 && PPC::getNonRecordFormOpcode(MIOpC) != -1)
1446 NewOpC = MIOpC;
1447 }
1448
1449 // FIXME: On the non-embedded POWER architectures, only some of the record
1450 // forms are fast, and we should use only the fast ones.
1451
1452 // The defining instruction has a record form (or is already a record
1453 // form). It is possible, however, that we'll need to reverse the condition
1454 // code of the users.
1455 if (NewOpC == -1)
1456 return false;
1457
Hal Finkele6322392013-04-19 22:08:38 +00001458 SmallVector<std::pair<MachineOperand*, PPC::Predicate>, 4> PredsToUpdate;
1459 SmallVector<std::pair<MachineOperand*, unsigned>, 4> SubRegsToUpdate;
Hal Finkel82656cb2013-04-18 22:15:08 +00001460
1461 // If we have SUB(r1, r2) and CMP(r2, r1), the condition code based on CMP
1462 // needs to be updated to be based on SUB. Push the condition code
1463 // operands to OperandsToUpdate. If it is safe to remove CmpInstr, the
1464 // condition code of these operands will be modified.
1465 bool ShouldSwap = false;
1466 if (Sub) {
1467 ShouldSwap = SrcReg2 != 0 && Sub->getOperand(1).getReg() == SrcReg2 &&
1468 Sub->getOperand(2).getReg() == SrcReg;
1469
1470 // The operands to subf are the opposite of sub, so only in the fixed-point
1471 // case, invert the order.
Hal Finkel08e53ee2013-05-08 12:16:14 +00001472 ShouldSwap = !ShouldSwap;
Hal Finkel82656cb2013-04-18 22:15:08 +00001473 }
1474
1475 if (ShouldSwap)
Owen Anderson16c6bf42014-03-13 23:12:04 +00001476 for (MachineRegisterInfo::use_instr_iterator
1477 I = MRI->use_instr_begin(CRReg), IE = MRI->use_instr_end();
1478 I != IE; ++I) {
Hal Finkel82656cb2013-04-18 22:15:08 +00001479 MachineInstr *UseMI = &*I;
1480 if (UseMI->getOpcode() == PPC::BCC) {
1481 PPC::Predicate Pred = (PPC::Predicate) UseMI->getOperand(0).getImm();
Hal Finkele6322392013-04-19 22:08:38 +00001482 assert((!equalityOnly ||
1483 Pred == PPC::PRED_EQ || Pred == PPC::PRED_NE) &&
1484 "Invalid predicate for equality-only optimization");
Owen Anderson16c6bf42014-03-13 23:12:04 +00001485 PredsToUpdate.push_back(std::make_pair(&(UseMI->getOperand(0)),
Hal Finkel0f64e212013-04-20 05:16:26 +00001486 PPC::getSwappedPredicate(Pred)));
Hal Finkel82656cb2013-04-18 22:15:08 +00001487 } else if (UseMI->getOpcode() == PPC::ISEL ||
1488 UseMI->getOpcode() == PPC::ISEL8) {
Hal Finkele6322392013-04-19 22:08:38 +00001489 unsigned NewSubReg = UseMI->getOperand(3).getSubReg();
1490 assert((!equalityOnly || NewSubReg == PPC::sub_eq) &&
1491 "Invalid CR bit for equality-only optimization");
1492
1493 if (NewSubReg == PPC::sub_lt)
1494 NewSubReg = PPC::sub_gt;
1495 else if (NewSubReg == PPC::sub_gt)
1496 NewSubReg = PPC::sub_lt;
1497
Owen Anderson16c6bf42014-03-13 23:12:04 +00001498 SubRegsToUpdate.push_back(std::make_pair(&(UseMI->getOperand(3)),
Hal Finkele6322392013-04-19 22:08:38 +00001499 NewSubReg));
Hal Finkel82656cb2013-04-18 22:15:08 +00001500 } else // We need to abort on a user we don't understand.
1501 return false;
1502 }
1503
1504 // Create a new virtual register to hold the value of the CR set by the
1505 // record-form instruction. If the instruction was not previously in
1506 // record form, then set the kill flag on the CR.
1507 CmpInstr->eraseFromParent();
1508
1509 MachineBasicBlock::iterator MII = MI;
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001510 BuildMI(*MI->getParent(), std::next(MII), MI->getDebugLoc(),
Hal Finkel82656cb2013-04-18 22:15:08 +00001511 get(TargetOpcode::COPY), CRReg)
Hal Finkel08e53ee2013-05-08 12:16:14 +00001512 .addReg(PPC::CR0, MIOpC != NewOpC ? RegState::Kill : 0);
Hal Finkel82656cb2013-04-18 22:15:08 +00001513
1514 if (MIOpC != NewOpC) {
1515 // We need to be careful here: we're replacing one instruction with
1516 // another, and we need to make sure that we get all of the right
1517 // implicit uses and defs. On the other hand, the caller may be holding
1518 // an iterator to this instruction, and so we can't delete it (this is
1519 // specifically the case if this is the instruction directly after the
1520 // compare).
1521
1522 const MCInstrDesc &NewDesc = get(NewOpC);
1523 MI->setDesc(NewDesc);
1524
1525 if (NewDesc.ImplicitDefs)
1526 for (const uint16_t *ImpDefs = NewDesc.getImplicitDefs();
1527 *ImpDefs; ++ImpDefs)
1528 if (!MI->definesRegister(*ImpDefs))
1529 MI->addOperand(*MI->getParent()->getParent(),
1530 MachineOperand::CreateReg(*ImpDefs, true, true));
1531 if (NewDesc.ImplicitUses)
1532 for (const uint16_t *ImpUses = NewDesc.getImplicitUses();
1533 *ImpUses; ++ImpUses)
1534 if (!MI->readsRegister(*ImpUses))
1535 MI->addOperand(*MI->getParent()->getParent(),
1536 MachineOperand::CreateReg(*ImpUses, false, true));
1537 }
1538
1539 // Modify the condition code of operands in OperandsToUpdate.
1540 // Since we have SUB(r1, r2) and CMP(r2, r1), the condition code needs to
1541 // be changed from r2 > r1 to r1 < r2, from r2 < r1 to r1 > r2, etc.
Hal Finkele6322392013-04-19 22:08:38 +00001542 for (unsigned i = 0, e = PredsToUpdate.size(); i < e; i++)
1543 PredsToUpdate[i].first->setImm(PredsToUpdate[i].second);
Hal Finkel82656cb2013-04-18 22:15:08 +00001544
Hal Finkele6322392013-04-19 22:08:38 +00001545 for (unsigned i = 0, e = SubRegsToUpdate.size(); i < e; i++)
1546 SubRegsToUpdate[i].first->setSubReg(SubRegsToUpdate[i].second);
Hal Finkel82656cb2013-04-18 22:15:08 +00001547
1548 return true;
1549}
1550
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +00001551/// GetInstSize - Return the number of bytes of code the specified
1552/// instruction may be. This returns the maximum number of bytes.
1553///
1554unsigned PPCInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
Hal Finkela7bbaf62014-02-02 06:12:27 +00001555 unsigned Opcode = MI->getOpcode();
1556
1557 if (Opcode == PPC::INLINEASM) {
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +00001558 const MachineFunction *MF = MI->getParent()->getParent();
1559 const char *AsmStr = MI->getOperand(0).getSymbolName();
Chris Lattner7b26fce2009-08-22 20:48:53 +00001560 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
Hal Finkela7bbaf62014-02-02 06:12:27 +00001561 } else {
1562 const MCInstrDesc &Desc = get(Opcode);
1563 return Desc.getSize();
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +00001564 }
1565}
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001566
Hal Finkel27774d92014-03-13 07:58:58 +00001567
1568#undef DEBUG_TYPE
1569#define DEBUG_TYPE "ppc-vsx-copy"
1570
1571namespace llvm {
1572 void initializePPCVSXCopyPass(PassRegistry&);
1573}
1574
1575namespace {
1576 // PPCVSXCopy pass - For copies between VSX registers and non-VSX registers
1577 // (Altivec and scalar floating-point registers), we need to transform the
1578 // copies into subregister copies with other restrictions.
1579 struct PPCVSXCopy : public MachineFunctionPass {
1580 static char ID;
1581 PPCVSXCopy() : MachineFunctionPass(ID) {
1582 initializePPCVSXCopyPass(*PassRegistry::getPassRegistry());
1583 }
1584
1585 const PPCTargetMachine *TM;
1586 const PPCInstrInfo *TII;
1587
1588 bool IsRegInClass(unsigned Reg, const TargetRegisterClass *RC,
1589 MachineRegisterInfo &MRI) {
1590 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1591 return RC->hasSubClassEq(MRI.getRegClass(Reg));
1592 } else if (RC->contains(Reg)) {
1593 return true;
1594 }
1595
1596 return false;
1597 }
1598
1599 bool IsVSReg(unsigned Reg, MachineRegisterInfo &MRI) {
1600 return IsRegInClass(Reg, &PPC::VSRCRegClass, MRI);
1601 }
1602
1603 bool IsVRReg(unsigned Reg, MachineRegisterInfo &MRI) {
1604 return IsRegInClass(Reg, &PPC::VRRCRegClass, MRI);
1605 }
1606
1607 bool IsF8Reg(unsigned Reg, MachineRegisterInfo &MRI) {
1608 return IsRegInClass(Reg, &PPC::F8RCRegClass, MRI);
1609 }
1610
1611protected:
1612 bool processBlock(MachineBasicBlock &MBB) {
1613 bool Changed = false;
1614
1615 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
1616 for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end();
1617 I != IE; ++I) {
1618 MachineInstr *MI = I;
1619 if (!MI->isFullCopy())
1620 continue;
1621
1622 MachineOperand &DstMO = MI->getOperand(0);
1623 MachineOperand &SrcMO = MI->getOperand(1);
1624
1625 if ( IsVSReg(DstMO.getReg(), MRI) &&
1626 !IsVSReg(SrcMO.getReg(), MRI)) {
1627 // This is a copy *to* a VSX register from a non-VSX register.
1628 Changed = true;
1629
1630 const TargetRegisterClass *SrcRC =
1631 IsVRReg(SrcMO.getReg(), MRI) ? &PPC::VSHRCRegClass :
1632 &PPC::VSLRCRegClass;
1633 assert((IsF8Reg(SrcMO.getReg(), MRI) ||
1634 IsVRReg(SrcMO.getReg(), MRI)) &&
1635 "Unknown source for a VSX copy");
1636
1637 unsigned NewVReg = MRI.createVirtualRegister(SrcRC);
1638 BuildMI(MBB, MI, MI->getDebugLoc(),
1639 TII->get(TargetOpcode::SUBREG_TO_REG), NewVReg)
1640 .addImm(1) // add 1, not 0, because there is no implicit clearing
1641 // of the high bits.
1642 .addOperand(SrcMO)
1643 .addImm(IsVRReg(SrcMO.getReg(), MRI) ? PPC::sub_128 :
1644 PPC::sub_64);
1645
1646 // The source of the original copy is now the new virtual register.
1647 SrcMO.setReg(NewVReg);
1648 } else if (!IsVSReg(DstMO.getReg(), MRI) &&
1649 IsVSReg(SrcMO.getReg(), MRI)) {
1650 // This is a copy *from* a VSX register to a non-VSX register.
1651 Changed = true;
1652
1653 const TargetRegisterClass *DstRC =
1654 IsVRReg(DstMO.getReg(), MRI) ? &PPC::VSHRCRegClass :
1655 &PPC::VSLRCRegClass;
1656 assert((IsF8Reg(DstMO.getReg(), MRI) ||
1657 IsVRReg(DstMO.getReg(), MRI)) &&
1658 "Unknown destination for a VSX copy");
1659
1660 // Copy the VSX value into a new VSX register of the correct subclass.
1661 unsigned NewVReg = MRI.createVirtualRegister(DstRC);
1662 BuildMI(MBB, MI, MI->getDebugLoc(),
1663 TII->get(TargetOpcode::COPY), NewVReg)
1664 .addOperand(SrcMO);
1665
1666 // Transform the original copy into a subregister extraction copy.
1667 SrcMO.setReg(NewVReg);
1668 SrcMO.setSubReg(IsVRReg(DstMO.getReg(), MRI) ? PPC::sub_128 :
1669 PPC::sub_64);
1670 }
1671 }
1672
1673 return Changed;
1674 }
1675
1676public:
1677 virtual bool runOnMachineFunction(MachineFunction &MF) {
1678 TM = static_cast<const PPCTargetMachine *>(&MF.getTarget());
1679 TII = TM->getInstrInfo();
1680
1681 bool Changed = false;
1682
1683 for (MachineFunction::iterator I = MF.begin(); I != MF.end();) {
1684 MachineBasicBlock &B = *I++;
1685 if (processBlock(B))
1686 Changed = true;
1687 }
1688
1689 return Changed;
1690 }
1691
1692 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
1693 MachineFunctionPass::getAnalysisUsage(AU);
1694 }
1695 };
1696}
1697
1698INITIALIZE_PASS(PPCVSXCopy, DEBUG_TYPE,
1699 "PowerPC VSX Copy Legalization", false, false)
1700
1701char PPCVSXCopy::ID = 0;
1702FunctionPass*
1703llvm::createPPCVSXCopyPass() { return new PPCVSXCopy(); }
1704
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001705#undef DEBUG_TYPE
1706#define DEBUG_TYPE "ppc-early-ret"
1707STATISTIC(NumBCLR, "Number of early conditional returns");
1708STATISTIC(NumBLR, "Number of early returns");
1709
1710namespace llvm {
1711 void initializePPCEarlyReturnPass(PassRegistry&);
1712}
1713
1714namespace {
1715 // PPCEarlyReturn pass - For simple functions without epilogue code, move
1716 // returns up, and create conditional returns, to avoid unnecessary
1717 // branch-to-blr sequences.
1718 struct PPCEarlyReturn : public MachineFunctionPass {
1719 static char ID;
1720 PPCEarlyReturn() : MachineFunctionPass(ID) {
1721 initializePPCEarlyReturnPass(*PassRegistry::getPassRegistry());
1722 }
1723
1724 const PPCTargetMachine *TM;
1725 const PPCInstrInfo *TII;
1726
1727protected:
Hal Finkel21aad9a2013-04-09 18:25:18 +00001728 bool processBlock(MachineBasicBlock &ReturnMBB) {
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001729 bool Changed = false;
1730
Hal Finkel21aad9a2013-04-09 18:25:18 +00001731 MachineBasicBlock::iterator I = ReturnMBB.begin();
1732 I = ReturnMBB.SkipPHIsAndLabels(I);
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001733
1734 // The block must be essentially empty except for the blr.
Hal Finkel21aad9a2013-04-09 18:25:18 +00001735 if (I == ReturnMBB.end() || I->getOpcode() != PPC::BLR ||
1736 I != ReturnMBB.getLastNonDebugInstr())
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001737 return Changed;
1738
1739 SmallVector<MachineBasicBlock*, 8> PredToRemove;
Hal Finkel21aad9a2013-04-09 18:25:18 +00001740 for (MachineBasicBlock::pred_iterator PI = ReturnMBB.pred_begin(),
1741 PIE = ReturnMBB.pred_end(); PI != PIE; ++PI) {
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001742 bool OtherReference = false, BlockChanged = false;
Hal Finkel21aad9a2013-04-09 18:25:18 +00001743 for (MachineBasicBlock::iterator J = (*PI)->getLastNonDebugInstr();;) {
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001744 if (J->getOpcode() == PPC::B) {
Hal Finkel21aad9a2013-04-09 18:25:18 +00001745 if (J->getOperand(0).getMBB() == &ReturnMBB) {
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001746 // This is an unconditional branch to the return. Replace the
Andrew Trick9defbd82013-12-17 04:50:40 +00001747 // branch with a blr.
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001748 BuildMI(**PI, J, J->getDebugLoc(), TII->get(PPC::BLR));
Hal Finkel21aad9a2013-04-09 18:25:18 +00001749 MachineBasicBlock::iterator K = J--;
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001750 K->eraseFromParent();
1751 BlockChanged = true;
1752 ++NumBLR;
1753 continue;
1754 }
1755 } else if (J->getOpcode() == PPC::BCC) {
Hal Finkel21aad9a2013-04-09 18:25:18 +00001756 if (J->getOperand(2).getMBB() == &ReturnMBB) {
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001757 // This is a conditional branch to the return. Replace the branch
1758 // with a bclr.
Hal Finkel940ab932014-02-28 00:27:01 +00001759 BuildMI(**PI, J, J->getDebugLoc(), TII->get(PPC::BCCLR))
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001760 .addImm(J->getOperand(0).getImm())
1761 .addReg(J->getOperand(1).getReg());
Hal Finkel21aad9a2013-04-09 18:25:18 +00001762 MachineBasicBlock::iterator K = J--;
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001763 K->eraseFromParent();
1764 BlockChanged = true;
1765 ++NumBCLR;
1766 continue;
1767 }
Hal Finkel940ab932014-02-28 00:27:01 +00001768 } else if (J->getOpcode() == PPC::BC || J->getOpcode() == PPC::BCn) {
1769 if (J->getOperand(1).getMBB() == &ReturnMBB) {
1770 // This is a conditional branch to the return. Replace the branch
1771 // with a bclr.
1772 BuildMI(**PI, J, J->getDebugLoc(),
1773 TII->get(J->getOpcode() == PPC::BC ?
1774 PPC::BCLR : PPC::BCLRn))
1775 .addReg(J->getOperand(0).getReg());
1776 MachineBasicBlock::iterator K = J--;
1777 K->eraseFromParent();
1778 BlockChanged = true;
1779 ++NumBCLR;
1780 continue;
1781 }
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001782 } else if (J->isBranch()) {
1783 if (J->isIndirectBranch()) {
Hal Finkel21aad9a2013-04-09 18:25:18 +00001784 if (ReturnMBB.hasAddressTaken())
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001785 OtherReference = true;
1786 } else
1787 for (unsigned i = 0; i < J->getNumOperands(); ++i)
1788 if (J->getOperand(i).isMBB() &&
Hal Finkel21aad9a2013-04-09 18:25:18 +00001789 J->getOperand(i).getMBB() == &ReturnMBB)
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001790 OtherReference = true;
Hal Finkel21aad9a2013-04-09 18:25:18 +00001791 } else if (!J->isTerminator() && !J->isDebugValue())
1792 break;
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001793
Hal Finkel21aad9a2013-04-09 18:25:18 +00001794 if (J == (*PI)->begin())
1795 break;
1796
1797 --J;
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001798 }
1799
Hal Finkel21aad9a2013-04-09 18:25:18 +00001800 if ((*PI)->canFallThrough() && (*PI)->isLayoutSuccessor(&ReturnMBB))
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001801 OtherReference = true;
1802
Andrew Trick9defbd82013-12-17 04:50:40 +00001803 // Predecessors are stored in a vector and can't be removed here.
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001804 if (!OtherReference && BlockChanged) {
1805 PredToRemove.push_back(*PI);
1806 }
1807
1808 if (BlockChanged)
1809 Changed = true;
1810 }
1811
1812 for (unsigned i = 0, ie = PredToRemove.size(); i != ie; ++i)
Hal Finkel21aad9a2013-04-09 18:25:18 +00001813 PredToRemove[i]->removeSuccessor(&ReturnMBB);
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001814
Hal Finkel21aad9a2013-04-09 18:25:18 +00001815 if (Changed && !ReturnMBB.hasAddressTaken()) {
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001816 // We now might be able to merge this blr-only block into its
1817 // by-layout predecessor.
Hal Finkel21aad9a2013-04-09 18:25:18 +00001818 if (ReturnMBB.pred_size() == 1 &&
1819 (*ReturnMBB.pred_begin())->isLayoutSuccessor(&ReturnMBB)) {
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001820 // Move the blr into the preceding block.
Hal Finkel21aad9a2013-04-09 18:25:18 +00001821 MachineBasicBlock &PrevMBB = **ReturnMBB.pred_begin();
1822 PrevMBB.splice(PrevMBB.end(), &ReturnMBB, I);
1823 PrevMBB.removeSuccessor(&ReturnMBB);
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001824 }
1825
Hal Finkel21aad9a2013-04-09 18:25:18 +00001826 if (ReturnMBB.pred_empty())
1827 ReturnMBB.eraseFromParent();
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001828 }
1829
1830 return Changed;
1831 }
1832
1833public:
1834 virtual bool runOnMachineFunction(MachineFunction &MF) {
1835 TM = static_cast<const PPCTargetMachine *>(&MF.getTarget());
1836 TII = TM->getInstrInfo();
1837
1838 bool Changed = false;
1839
Hal Finkel21aad9a2013-04-09 18:25:18 +00001840 // If the function does not have at least two blocks, then there is
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001841 // nothing to do.
1842 if (MF.size() < 2)
1843 return Changed;
1844
1845 for (MachineFunction::iterator I = MF.begin(); I != MF.end();) {
Andrew Trick9defbd82013-12-17 04:50:40 +00001846 MachineBasicBlock &B = *I++;
Hal Finkelb5aa7e52013-04-08 16:24:03 +00001847 if (processBlock(B))
1848 Changed = true;
1849 }
1850
1851 return Changed;
1852 }
1853
1854 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
1855 MachineFunctionPass::getAnalysisUsage(AU);
1856 }
1857 };
1858}
1859
1860INITIALIZE_PASS(PPCEarlyReturn, DEBUG_TYPE,
1861 "PowerPC Early-Return Creation", false, false)
1862
1863char PPCEarlyReturn::ID = 0;
1864FunctionPass*
1865llvm::createPPCEarlyReturnPass() { return new PPCEarlyReturn(); }