Jia Liu | b22310f | 2012-02-18 12:03:15 +0000 | [diff] [blame] | 1 | //===-- PPCInstrInfo.cpp - PowerPC Instruction Information ----------------===// |
Misha Brukman | b440243 | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 2 | // |
Misha Brukman | 116f927 | 2004-08-17 04:55:41 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | b440243 | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 7 | // |
Misha Brukman | 116f927 | 2004-08-17 04:55:41 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file contains the PowerPC implementation of the TargetInstrInfo class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chris Lattner | 6f3b954 | 2005-10-14 23:59:06 +0000 | [diff] [blame] | 14 | #include "PPCInstrInfo.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 15 | #include "MCTargetDesc/PPCPredicates.h" |
Evan Cheng | c5e6d2f | 2011-07-11 03:57:24 +0000 | [diff] [blame] | 16 | #include "PPC.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 17 | #include "PPCHazardRecognizers.h" |
Owen Anderson | eee1460 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 18 | #include "PPCInstrBuilder.h" |
Bill Wendling | 632ea65 | 2008-03-03 22:19:16 +0000 | [diff] [blame] | 19 | #include "PPCMachineFunctionInfo.h" |
Chris Lattner | 49cadab | 2006-06-17 00:01:04 +0000 | [diff] [blame] | 20 | #include "PPCTargetMachine.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/Statistic.h" |
Hal Finkel | 174e590 | 2014-03-25 23:29:21 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/LiveIntervalAnalysis.h" |
Jakob Stoklund Olesen | 6353e53 | 2010-07-16 18:22:00 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Hal Finkel | b5aa7e5 | 2013-04-08 16:24:03 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Misha Brukman | 116f927 | 2004-08-17 04:55:41 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Jakob Stoklund Olesen | 6353e53 | 2010-07-16 18:22:00 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/MachineMemOperand.h" |
Jakob Stoklund Olesen | ddbf7a8 | 2010-02-26 21:09:24 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Hal Finkel | 9f9f892 | 2012-04-01 19:22:40 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/PseudoSourceValue.h" |
Eric Christopher | 1dcea73 | 2014-06-12 21:48:52 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/ScheduleDAG.h" |
Hal Finkel | 174e590 | 2014-03-25 23:29:21 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/SlotIndexes.h" |
Hal Finkel | 934361a | 2015-01-14 01:07:51 +0000 | [diff] [blame] | 32 | #include "llvm/CodeGen/StackMaps.h" |
Evan Cheng | c5e6d2f | 2011-07-11 03:57:24 +0000 | [diff] [blame] | 33 | #include "llvm/MC/MCAsmInfo.h" |
Bill Wendling | 1af20ad | 2008-03-04 23:13:51 +0000 | [diff] [blame] | 34 | #include "llvm/Support/CommandLine.h" |
Hal Finkel | 174e590 | 2014-03-25 23:29:21 +0000 | [diff] [blame] | 35 | #include "llvm/Support/Debug.h" |
Torok Edwin | fb8d6d5 | 2009-07-08 20:53:28 +0000 | [diff] [blame] | 36 | #include "llvm/Support/ErrorHandling.h" |
Evan Cheng | 2bb4035 | 2011-08-24 18:08:43 +0000 | [diff] [blame] | 37 | #include "llvm/Support/TargetRegistry.h" |
Torok Edwin | fb8d6d5 | 2009-07-08 20:53:28 +0000 | [diff] [blame] | 38 | #include "llvm/Support/raw_ostream.h" |
Misha Brukman | 116f927 | 2004-08-17 04:55:41 +0000 | [diff] [blame] | 39 | |
Dan Gohman | 2085719 | 2010-04-15 17:20:57 +0000 | [diff] [blame] | 40 | using namespace llvm; |
Bill Wendling | 1af20ad | 2008-03-04 23:13:51 +0000 | [diff] [blame] | 41 | |
Chandler Carruth | e96dd89 | 2014-04-21 22:55:11 +0000 | [diff] [blame] | 42 | #define DEBUG_TYPE "ppc-instr-info" |
| 43 | |
Chandler Carruth | d174b72 | 2014-04-22 02:03:14 +0000 | [diff] [blame] | 44 | #define GET_INSTRMAP_INFO |
| 45 | #define GET_INSTRINFO_CTOR_DTOR |
| 46 | #include "PPCGenInstrInfo.inc" |
| 47 | |
Hal Finkel | 821e001 | 2012-06-08 15:38:25 +0000 | [diff] [blame] | 48 | static cl:: |
Hal Finkel | c6b5deb | 2012-06-08 19:19:53 +0000 | [diff] [blame] | 49 | opt<bool> DisableCTRLoopAnal("disable-ppc-ctrloop-analysis", cl::Hidden, |
| 50 | cl::desc("Disable analysis for CTR loops")); |
Hal Finkel | 821e001 | 2012-06-08 15:38:25 +0000 | [diff] [blame] | 51 | |
Hal Finkel | e632239 | 2013-04-19 22:08:38 +0000 | [diff] [blame] | 52 | static cl::opt<bool> DisableCmpOpt("disable-ppc-cmp-opt", |
Hal Finkel | b12da6b | 2013-04-18 22:54:25 +0000 | [diff] [blame] | 53 | cl::desc("Disable compare instruction optimization"), cl::Hidden); |
| 54 | |
Hal Finkel | 9dcb358 | 2014-03-27 22:46:28 +0000 | [diff] [blame] | 55 | static cl::opt<bool> VSXSelfCopyCrash("crash-on-ppc-vsx-self-copy", |
| 56 | cl::desc("Causes the backend to crash instead of generating a nop VSX copy"), |
| 57 | cl::Hidden); |
| 58 | |
Juergen Ributzka | d12ccbd | 2013-11-19 00:57:56 +0000 | [diff] [blame] | 59 | // Pin the vtable to this file. |
| 60 | void PPCInstrInfo::anchor() {} |
| 61 | |
Eric Christopher | 1dcea73 | 2014-06-12 21:48:52 +0000 | [diff] [blame] | 62 | PPCInstrInfo::PPCInstrInfo(PPCSubtarget &STI) |
| 63 | : PPCGenInstrInfo(PPC::ADJCALLSTACKDOWN, PPC::ADJCALLSTACKUP), |
| 64 | Subtarget(STI), RI(STI) {} |
Chris Lattner | 49cadab | 2006-06-17 00:01:04 +0000 | [diff] [blame] | 65 | |
Andrew Trick | 10ffc2b | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 66 | /// CreateTargetHazardRecognizer - Return the hazard recognizer to use for |
| 67 | /// this target when scheduling the DAG. |
Eric Christopher | f047bfd | 2014-06-13 22:38:52 +0000 | [diff] [blame] | 68 | ScheduleHazardRecognizer * |
| 69 | PPCInstrInfo::CreateTargetHazardRecognizer(const TargetSubtargetInfo *STI, |
| 70 | const ScheduleDAG *DAG) const { |
| 71 | unsigned Directive = |
| 72 | static_cast<const PPCSubtarget *>(STI)->getDarwinDirective(); |
Hal Finkel | 742b535 | 2012-08-28 16:12:39 +0000 | [diff] [blame] | 73 | if (Directive == PPC::DIR_440 || Directive == PPC::DIR_A2 || |
| 74 | Directive == PPC::DIR_E500mc || Directive == PPC::DIR_E5500) { |
Eric Christopher | f047bfd | 2014-06-13 22:38:52 +0000 | [diff] [blame] | 75 | const InstrItineraryData *II = |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 76 | static_cast<const PPCSubtarget *>(STI)->getInstrItineraryData(); |
Hal Finkel | 563cc05 | 2013-12-02 23:52:46 +0000 | [diff] [blame] | 77 | return new ScoreboardHazardRecognizer(II, DAG); |
Hal Finkel | 6fa5697 | 2011-10-17 04:03:49 +0000 | [diff] [blame] | 78 | } |
Hal Finkel | 58ca360 | 2011-12-02 04:58:02 +0000 | [diff] [blame] | 79 | |
Eric Christopher | f047bfd | 2014-06-13 22:38:52 +0000 | [diff] [blame] | 80 | return TargetInstrInfo::CreateTargetHazardRecognizer(STI, DAG); |
Andrew Trick | 10ffc2b | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Hal Finkel | 58ca360 | 2011-12-02 04:58:02 +0000 | [diff] [blame] | 83 | /// CreateTargetPostRAHazardRecognizer - Return the postRA hazard recognizer |
| 84 | /// to use for this target when scheduling the DAG. |
Eric Christopher | cccae79 | 2015-01-30 22:02:31 +0000 | [diff] [blame] | 85 | ScheduleHazardRecognizer * |
| 86 | PPCInstrInfo::CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II, |
| 87 | const ScheduleDAG *DAG) const { |
Eric Christopher | 1dcea73 | 2014-06-12 21:48:52 +0000 | [diff] [blame] | 88 | unsigned Directive = |
Eric Christopher | cccae79 | 2015-01-30 22:02:31 +0000 | [diff] [blame] | 89 | DAG->MF.getSubtarget<PPCSubtarget>().getDarwinDirective(); |
Hal Finkel | 58ca360 | 2011-12-02 04:58:02 +0000 | [diff] [blame] | 90 | |
Will Schmidt | 970ff64 | 2014-06-26 13:36:19 +0000 | [diff] [blame] | 91 | if (Directive == PPC::DIR_PWR7 || Directive == PPC::DIR_PWR8) |
Hal Finkel | ceb1f12 | 2013-12-12 00:19:11 +0000 | [diff] [blame] | 92 | return new PPCDispatchGroupSBHazardRecognizer(II, DAG); |
| 93 | |
Hal Finkel | 58ca360 | 2011-12-02 04:58:02 +0000 | [diff] [blame] | 94 | // Most subtargets use a PPC970 recognizer. |
Hal Finkel | 742b535 | 2012-08-28 16:12:39 +0000 | [diff] [blame] | 95 | if (Directive != PPC::DIR_440 && Directive != PPC::DIR_A2 && |
| 96 | Directive != PPC::DIR_E500mc && Directive != PPC::DIR_E5500) { |
Eric Christopher | 1dcea73 | 2014-06-12 21:48:52 +0000 | [diff] [blame] | 97 | assert(DAG->TII && "No InstrInfo?"); |
Hal Finkel | 58ca360 | 2011-12-02 04:58:02 +0000 | [diff] [blame] | 98 | |
Eric Christopher | 1dcea73 | 2014-06-12 21:48:52 +0000 | [diff] [blame] | 99 | return new PPCHazardRecognizer970(*DAG); |
Hal Finkel | 58ca360 | 2011-12-02 04:58:02 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Hal Finkel | 563cc05 | 2013-12-02 23:52:46 +0000 | [diff] [blame] | 102 | return new ScoreboardHazardRecognizer(II, DAG); |
Hal Finkel | 58ca360 | 2011-12-02 04:58:02 +0000 | [diff] [blame] | 103 | } |
Jakob Stoklund Olesen | 0f855e4 | 2012-06-19 21:14:34 +0000 | [diff] [blame] | 104 | |
Hal Finkel | ceb1f12 | 2013-12-12 00:19:11 +0000 | [diff] [blame] | 105 | |
| 106 | int PPCInstrInfo::getOperandLatency(const InstrItineraryData *ItinData, |
| 107 | const MachineInstr *DefMI, unsigned DefIdx, |
| 108 | const MachineInstr *UseMI, |
| 109 | unsigned UseIdx) const { |
| 110 | int Latency = PPCGenInstrInfo::getOperandLatency(ItinData, DefMI, DefIdx, |
| 111 | UseMI, UseIdx); |
| 112 | |
| 113 | const MachineOperand &DefMO = DefMI->getOperand(DefIdx); |
| 114 | unsigned Reg = DefMO.getReg(); |
| 115 | |
| 116 | const TargetRegisterInfo *TRI = &getRegisterInfo(); |
| 117 | bool IsRegCR; |
| 118 | if (TRI->isVirtualRegister(Reg)) { |
| 119 | const MachineRegisterInfo *MRI = |
| 120 | &DefMI->getParent()->getParent()->getRegInfo(); |
| 121 | IsRegCR = MRI->getRegClass(Reg)->hasSuperClassEq(&PPC::CRRCRegClass) || |
| 122 | MRI->getRegClass(Reg)->hasSuperClassEq(&PPC::CRBITRCRegClass); |
| 123 | } else { |
| 124 | IsRegCR = PPC::CRRCRegClass.contains(Reg) || |
| 125 | PPC::CRBITRCRegClass.contains(Reg); |
| 126 | } |
| 127 | |
| 128 | if (UseMI->isBranch() && IsRegCR) { |
| 129 | if (Latency < 0) |
| 130 | Latency = getInstrLatency(ItinData, DefMI); |
| 131 | |
| 132 | // On some cores, there is an additional delay between writing to a condition |
| 133 | // register, and using it from a branch. |
Eric Christopher | 1dcea73 | 2014-06-12 21:48:52 +0000 | [diff] [blame] | 134 | unsigned Directive = Subtarget.getDarwinDirective(); |
Hal Finkel | ceb1f12 | 2013-12-12 00:19:11 +0000 | [diff] [blame] | 135 | switch (Directive) { |
| 136 | default: break; |
| 137 | case PPC::DIR_7400: |
| 138 | case PPC::DIR_750: |
| 139 | case PPC::DIR_970: |
| 140 | case PPC::DIR_E5500: |
| 141 | case PPC::DIR_PWR4: |
| 142 | case PPC::DIR_PWR5: |
| 143 | case PPC::DIR_PWR5X: |
| 144 | case PPC::DIR_PWR6: |
| 145 | case PPC::DIR_PWR6X: |
| 146 | case PPC::DIR_PWR7: |
Will Schmidt | 970ff64 | 2014-06-26 13:36:19 +0000 | [diff] [blame] | 147 | case PPC::DIR_PWR8: |
Hal Finkel | ceb1f12 | 2013-12-12 00:19:11 +0000 | [diff] [blame] | 148 | Latency += 2; |
| 149 | break; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | return Latency; |
| 154 | } |
| 155 | |
Jakob Stoklund Olesen | 0f855e4 | 2012-06-19 21:14:34 +0000 | [diff] [blame] | 156 | // Detect 32 -> 64-bit extensions where we may reuse the low sub-register. |
| 157 | bool PPCInstrInfo::isCoalescableExtInstr(const MachineInstr &MI, |
| 158 | unsigned &SrcReg, unsigned &DstReg, |
| 159 | unsigned &SubIdx) const { |
| 160 | switch (MI.getOpcode()) { |
| 161 | default: return false; |
| 162 | case PPC::EXTSW: |
| 163 | case PPC::EXTSW_32_64: |
| 164 | SrcReg = MI.getOperand(1).getReg(); |
| 165 | DstReg = MI.getOperand(0).getReg(); |
| 166 | SubIdx = PPC::sub_32; |
| 167 | return true; |
| 168 | } |
| 169 | } |
| 170 | |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 171 | unsigned PPCInstrInfo::isLoadFromStackSlot(const MachineInstr *MI, |
Chris Lattner | 91400bd | 2006-03-16 22:24:02 +0000 | [diff] [blame] | 172 | int &FrameIndex) const { |
Hal Finkel | 37714b8 | 2013-03-27 21:21:15 +0000 | [diff] [blame] | 173 | // Note: This list must be kept consistent with LoadRegFromStackSlot. |
Chris Lattner | bb53acd | 2006-02-02 20:12:32 +0000 | [diff] [blame] | 174 | switch (MI->getOpcode()) { |
| 175 | default: break; |
| 176 | case PPC::LD: |
| 177 | case PPC::LWZ: |
| 178 | case PPC::LFS: |
| 179 | case PPC::LFD: |
Hal Finkel | 37714b8 | 2013-03-27 21:21:15 +0000 | [diff] [blame] | 180 | case PPC::RESTORE_CR: |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 181 | case PPC::RESTORE_CRBIT: |
Hal Finkel | 37714b8 | 2013-03-27 21:21:15 +0000 | [diff] [blame] | 182 | case PPC::LVX: |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 183 | case PPC::LXVD2X: |
Hal Finkel | 37714b8 | 2013-03-27 21:21:15 +0000 | [diff] [blame] | 184 | case PPC::RESTORE_VRSAVE: |
| 185 | // Check for the operands added by addFrameReference (the immediate is the |
| 186 | // offset which defaults to 0). |
Dan Gohman | 0d1e9a8 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 187 | if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() && |
| 188 | MI->getOperand(2).isFI()) { |
Chris Lattner | a5bb370 | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 189 | FrameIndex = MI->getOperand(2).getIndex(); |
Chris Lattner | bb53acd | 2006-02-02 20:12:32 +0000 | [diff] [blame] | 190 | return MI->getOperand(0).getReg(); |
| 191 | } |
| 192 | break; |
| 193 | } |
| 194 | return 0; |
Chris Lattner | c327d71 | 2006-02-02 20:16:12 +0000 | [diff] [blame] | 195 | } |
Chris Lattner | bb53acd | 2006-02-02 20:12:32 +0000 | [diff] [blame] | 196 | |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 197 | unsigned PPCInstrInfo::isStoreToStackSlot(const MachineInstr *MI, |
Chris Lattner | c327d71 | 2006-02-02 20:16:12 +0000 | [diff] [blame] | 198 | int &FrameIndex) const { |
Hal Finkel | 37714b8 | 2013-03-27 21:21:15 +0000 | [diff] [blame] | 199 | // Note: This list must be kept consistent with StoreRegToStackSlot. |
Chris Lattner | c327d71 | 2006-02-02 20:16:12 +0000 | [diff] [blame] | 200 | switch (MI->getOpcode()) { |
| 201 | default: break; |
Nate Begeman | 4efb328 | 2006-02-02 21:07:50 +0000 | [diff] [blame] | 202 | case PPC::STD: |
Chris Lattner | c327d71 | 2006-02-02 20:16:12 +0000 | [diff] [blame] | 203 | case PPC::STW: |
| 204 | case PPC::STFS: |
| 205 | case PPC::STFD: |
Hal Finkel | 37714b8 | 2013-03-27 21:21:15 +0000 | [diff] [blame] | 206 | case PPC::SPILL_CR: |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 207 | case PPC::SPILL_CRBIT: |
Hal Finkel | 37714b8 | 2013-03-27 21:21:15 +0000 | [diff] [blame] | 208 | case PPC::STVX: |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 209 | case PPC::STXVD2X: |
Hal Finkel | 37714b8 | 2013-03-27 21:21:15 +0000 | [diff] [blame] | 210 | case PPC::SPILL_VRSAVE: |
| 211 | // Check for the operands added by addFrameReference (the immediate is the |
| 212 | // offset which defaults to 0). |
Dan Gohman | 0d1e9a8 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 213 | if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() && |
| 214 | MI->getOperand(2).isFI()) { |
Chris Lattner | a5bb370 | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 215 | FrameIndex = MI->getOperand(2).getIndex(); |
Chris Lattner | c327d71 | 2006-02-02 20:16:12 +0000 | [diff] [blame] | 216 | return MI->getOperand(0).getReg(); |
| 217 | } |
| 218 | break; |
| 219 | } |
| 220 | return 0; |
| 221 | } |
Chris Lattner | bb53acd | 2006-02-02 20:12:32 +0000 | [diff] [blame] | 222 | |
Chris Lattner | c37a2f1 | 2005-09-09 18:17:41 +0000 | [diff] [blame] | 223 | // commuteInstruction - We can commute rlwimi instructions, but only if the |
| 224 | // rotate amt is zero. We also have to munge the immediates a bit. |
Evan Cheng | 03553bb | 2008-06-16 07:33:11 +0000 | [diff] [blame] | 225 | MachineInstr * |
| 226 | PPCInstrInfo::commuteInstruction(MachineInstr *MI, bool NewMI) const { |
Dan Gohman | 3b46030 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 227 | MachineFunction &MF = *MI->getParent()->getParent(); |
| 228 | |
Chris Lattner | c37a2f1 | 2005-09-09 18:17:41 +0000 | [diff] [blame] | 229 | // Normal instructions can be commuted the obvious way. |
Hal Finkel | 654d43b | 2013-04-12 02:18:09 +0000 | [diff] [blame] | 230 | if (MI->getOpcode() != PPC::RLWIMI && |
Hal Finkel | 4c6658f | 2014-12-12 23:59:36 +0000 | [diff] [blame] | 231 | MI->getOpcode() != PPC::RLWIMIo) |
Jakob Stoklund Olesen | 9de596e | 2012-11-28 02:35:17 +0000 | [diff] [blame] | 232 | return TargetInstrInfo::commuteInstruction(MI, NewMI); |
Hal Finkel | 4c6658f | 2014-12-12 23:59:36 +0000 | [diff] [blame] | 233 | // Note that RLWIMI can be commuted as a 32-bit instruction, but not as a |
| 234 | // 64-bit instruction (so we don't handle PPC::RLWIMI8 here), because |
| 235 | // changing the relative order of the mask operands might change what happens |
| 236 | // to the high-bits of the mask (and, thus, the result). |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 237 | |
Chris Lattner | c37a2f1 | 2005-09-09 18:17:41 +0000 | [diff] [blame] | 238 | // Cannot commute if it has a non-zero rotate count. |
Chris Lattner | 5c46378 | 2007-12-30 20:49:49 +0000 | [diff] [blame] | 239 | if (MI->getOperand(3).getImm() != 0) |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 240 | return nullptr; |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 241 | |
Chris Lattner | c37a2f1 | 2005-09-09 18:17:41 +0000 | [diff] [blame] | 242 | // If we have a zero rotate count, we have: |
| 243 | // M = mask(MB,ME) |
| 244 | // Op0 = (Op1 & ~M) | (Op2 & M) |
| 245 | // Change this to: |
| 246 | // M = mask((ME+1)&31, (MB-1)&31) |
| 247 | // Op0 = (Op2 & ~M) | (Op1 & M) |
| 248 | |
| 249 | // Swap op1/op2 |
Evan Cheng | 244183e | 2008-02-13 02:46:49 +0000 | [diff] [blame] | 250 | unsigned Reg0 = MI->getOperand(0).getReg(); |
Chris Lattner | c37a2f1 | 2005-09-09 18:17:41 +0000 | [diff] [blame] | 251 | unsigned Reg1 = MI->getOperand(1).getReg(); |
| 252 | unsigned Reg2 = MI->getOperand(2).getReg(); |
Andrew Trick | e339828 | 2013-12-17 04:50:45 +0000 | [diff] [blame] | 253 | unsigned SubReg1 = MI->getOperand(1).getSubReg(); |
| 254 | unsigned SubReg2 = MI->getOperand(2).getSubReg(); |
Evan Cheng | dc2c874 | 2006-11-15 20:58:11 +0000 | [diff] [blame] | 255 | bool Reg1IsKill = MI->getOperand(1).isKill(); |
| 256 | bool Reg2IsKill = MI->getOperand(2).isKill(); |
Evan Cheng | 03553bb | 2008-06-16 07:33:11 +0000 | [diff] [blame] | 257 | bool ChangeReg0 = false; |
Evan Cheng | 244183e | 2008-02-13 02:46:49 +0000 | [diff] [blame] | 258 | // If machine instrs are no longer in two-address forms, update |
| 259 | // destination register as well. |
| 260 | if (Reg0 == Reg1) { |
| 261 | // Must be two address instruction! |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 262 | assert(MI->getDesc().getOperandConstraint(0, MCOI::TIED_TO) && |
Evan Cheng | 244183e | 2008-02-13 02:46:49 +0000 | [diff] [blame] | 263 | "Expecting a two-address instruction!"); |
Andrew Trick | e339828 | 2013-12-17 04:50:45 +0000 | [diff] [blame] | 264 | assert(MI->getOperand(0).getSubReg() == SubReg1 && "Tied subreg mismatch"); |
Evan Cheng | 244183e | 2008-02-13 02:46:49 +0000 | [diff] [blame] | 265 | Reg2IsKill = false; |
Evan Cheng | 03553bb | 2008-06-16 07:33:11 +0000 | [diff] [blame] | 266 | ChangeReg0 = true; |
Evan Cheng | 244183e | 2008-02-13 02:46:49 +0000 | [diff] [blame] | 267 | } |
Evan Cheng | 03553bb | 2008-06-16 07:33:11 +0000 | [diff] [blame] | 268 | |
| 269 | // Masks. |
| 270 | unsigned MB = MI->getOperand(4).getImm(); |
| 271 | unsigned ME = MI->getOperand(5).getImm(); |
| 272 | |
| 273 | if (NewMI) { |
| 274 | // Create a new instruction. |
| 275 | unsigned Reg0 = ChangeReg0 ? Reg2 : MI->getOperand(0).getReg(); |
| 276 | bool Reg0IsDead = MI->getOperand(0).isDead(); |
Bill Wendling | f6d609a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 277 | return BuildMI(MF, MI->getDebugLoc(), MI->getDesc()) |
Bill Wendling | f7b83c7 | 2009-05-13 21:33:08 +0000 | [diff] [blame] | 278 | .addReg(Reg0, RegState::Define | getDeadRegState(Reg0IsDead)) |
| 279 | .addReg(Reg2, getKillRegState(Reg2IsKill)) |
| 280 | .addReg(Reg1, getKillRegState(Reg1IsKill)) |
Evan Cheng | 03553bb | 2008-06-16 07:33:11 +0000 | [diff] [blame] | 281 | .addImm((ME+1) & 31) |
| 282 | .addImm((MB-1) & 31); |
| 283 | } |
| 284 | |
Andrew Trick | e339828 | 2013-12-17 04:50:45 +0000 | [diff] [blame] | 285 | if (ChangeReg0) { |
Evan Cheng | 03553bb | 2008-06-16 07:33:11 +0000 | [diff] [blame] | 286 | MI->getOperand(0).setReg(Reg2); |
Andrew Trick | e339828 | 2013-12-17 04:50:45 +0000 | [diff] [blame] | 287 | MI->getOperand(0).setSubReg(SubReg2); |
| 288 | } |
Chris Lattner | 10d6341 | 2006-05-04 17:52:23 +0000 | [diff] [blame] | 289 | MI->getOperand(2).setReg(Reg1); |
| 290 | MI->getOperand(1).setReg(Reg2); |
Andrew Trick | e339828 | 2013-12-17 04:50:45 +0000 | [diff] [blame] | 291 | MI->getOperand(2).setSubReg(SubReg1); |
| 292 | MI->getOperand(1).setSubReg(SubReg2); |
Chris Lattner | 6005589 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 293 | MI->getOperand(2).setIsKill(Reg1IsKill); |
| 294 | MI->getOperand(1).setIsKill(Reg2IsKill); |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 295 | |
Chris Lattner | c37a2f1 | 2005-09-09 18:17:41 +0000 | [diff] [blame] | 296 | // Swap the mask around. |
Chris Lattner | 5c46378 | 2007-12-30 20:49:49 +0000 | [diff] [blame] | 297 | MI->getOperand(4).setImm((ME+1) & 31); |
| 298 | MI->getOperand(5).setImm((MB-1) & 31); |
Chris Lattner | c37a2f1 | 2005-09-09 18:17:41 +0000 | [diff] [blame] | 299 | return MI; |
| 300 | } |
Chris Lattner | ea79d9fd73 | 2006-03-05 23:49:55 +0000 | [diff] [blame] | 301 | |
Hal Finkel | 6c32ff3 | 2014-03-25 19:26:43 +0000 | [diff] [blame] | 302 | bool PPCInstrInfo::findCommutedOpIndices(MachineInstr *MI, unsigned &SrcOpIdx1, |
| 303 | unsigned &SrcOpIdx2) const { |
| 304 | // For VSX A-Type FMA instructions, it is the first two operands that can be |
| 305 | // commuted, however, because the non-encoded tied input operand is listed |
| 306 | // first, the operands to swap are actually the second and third. |
| 307 | |
| 308 | int AltOpc = PPC::getAltVSXFMAOpcode(MI->getOpcode()); |
| 309 | if (AltOpc == -1) |
| 310 | return TargetInstrInfo::findCommutedOpIndices(MI, SrcOpIdx1, SrcOpIdx2); |
| 311 | |
| 312 | SrcOpIdx1 = 2; |
| 313 | SrcOpIdx2 = 3; |
| 314 | return true; |
| 315 | } |
| 316 | |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 317 | void PPCInstrInfo::insertNoop(MachineBasicBlock &MBB, |
Chris Lattner | ea79d9fd73 | 2006-03-05 23:49:55 +0000 | [diff] [blame] | 318 | MachineBasicBlock::iterator MI) const { |
Hal Finkel | ceb1f12 | 2013-12-12 00:19:11 +0000 | [diff] [blame] | 319 | // This function is used for scheduling, and the nop wanted here is the type |
| 320 | // that terminates dispatch groups on the POWER cores. |
Eric Christopher | 1dcea73 | 2014-06-12 21:48:52 +0000 | [diff] [blame] | 321 | unsigned Directive = Subtarget.getDarwinDirective(); |
Hal Finkel | ceb1f12 | 2013-12-12 00:19:11 +0000 | [diff] [blame] | 322 | unsigned Opcode; |
| 323 | switch (Directive) { |
| 324 | default: Opcode = PPC::NOP; break; |
| 325 | case PPC::DIR_PWR6: Opcode = PPC::NOP_GT_PWR6; break; |
| 326 | case PPC::DIR_PWR7: Opcode = PPC::NOP_GT_PWR7; break; |
Will Schmidt | 970ff64 | 2014-06-26 13:36:19 +0000 | [diff] [blame] | 327 | case PPC::DIR_PWR8: Opcode = PPC::NOP_GT_PWR7; break; /* FIXME: Update when P8 InstrScheduling model is ready */ |
Hal Finkel | ceb1f12 | 2013-12-12 00:19:11 +0000 | [diff] [blame] | 328 | } |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 329 | |
Hal Finkel | ceb1f12 | 2013-12-12 00:19:11 +0000 | [diff] [blame] | 330 | DebugLoc DL; |
| 331 | BuildMI(MBB, MI, DL, get(Opcode)); |
| 332 | } |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 333 | |
Joerg Sonnenberger | 7ee0f31 | 2014-08-08 19:13:23 +0000 | [diff] [blame] | 334 | /// getNoopForMachoTarget - Return the noop instruction to use for a noop. |
| 335 | void PPCInstrInfo::getNoopForMachoTarget(MCInst &NopInst) const { |
| 336 | NopInst.setOpcode(PPC::NOP); |
| 337 | } |
| 338 | |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 339 | // Branch analysis. |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 340 | // Note: If the condition register is set to CTR or CTR8 then this is a |
| 341 | // BDNZ (imm == 1) or BDZ (imm == 0) branch. |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 342 | bool PPCInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB, |
| 343 | MachineBasicBlock *&FBB, |
Evan Cheng | 64dfcac | 2009-02-09 07:14:22 +0000 | [diff] [blame] | 344 | SmallVectorImpl<MachineOperand> &Cond, |
| 345 | bool AllowModify) const { |
Eric Christopher | 1dcea73 | 2014-06-12 21:48:52 +0000 | [diff] [blame] | 346 | bool isPPC64 = Subtarget.isPPC64(); |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 347 | |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 348 | // If the block has no terminators, it just falls into the block after it. |
| 349 | MachineBasicBlock::iterator I = MBB.end(); |
Dale Johannesen | 4244d12 | 2010-04-02 01:38:09 +0000 | [diff] [blame] | 350 | if (I == MBB.begin()) |
| 351 | return false; |
| 352 | --I; |
| 353 | while (I->isDebugValue()) { |
| 354 | if (I == MBB.begin()) |
| 355 | return false; |
| 356 | --I; |
| 357 | } |
| 358 | if (!isUnpredicatedTerminator(I)) |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 359 | return false; |
| 360 | |
| 361 | // Get the last instruction in the block. |
| 362 | MachineInstr *LastInst = I; |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 363 | |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 364 | // If there is only one terminator instruction, process it. |
Evan Cheng | 5514bbe | 2007-06-08 21:59:56 +0000 | [diff] [blame] | 365 | if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) { |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 366 | if (LastInst->getOpcode() == PPC::B) { |
Evan Cheng | 8f43afd | 2009-05-08 23:09:25 +0000 | [diff] [blame] | 367 | if (!LastInst->getOperand(0).isMBB()) |
| 368 | return true; |
Chris Lattner | a5bb370 | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 369 | TBB = LastInst->getOperand(0).getMBB(); |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 370 | return false; |
Chris Lattner | e026379 | 2006-11-17 22:14:47 +0000 | [diff] [blame] | 371 | } else if (LastInst->getOpcode() == PPC::BCC) { |
Evan Cheng | 8f43afd | 2009-05-08 23:09:25 +0000 | [diff] [blame] | 372 | if (!LastInst->getOperand(2).isMBB()) |
| 373 | return true; |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 374 | // Block ends with fall-through condbranch. |
Chris Lattner | a5bb370 | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 375 | TBB = LastInst->getOperand(2).getMBB(); |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 376 | Cond.push_back(LastInst->getOperand(0)); |
| 377 | Cond.push_back(LastInst->getOperand(1)); |
Chris Lattner | 23f22de | 2006-10-21 06:03:11 +0000 | [diff] [blame] | 378 | return false; |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 379 | } else if (LastInst->getOpcode() == PPC::BC) { |
| 380 | if (!LastInst->getOperand(1).isMBB()) |
| 381 | return true; |
| 382 | // Block ends with fall-through condbranch. |
| 383 | TBB = LastInst->getOperand(1).getMBB(); |
| 384 | Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET)); |
| 385 | Cond.push_back(LastInst->getOperand(0)); |
| 386 | return false; |
| 387 | } else if (LastInst->getOpcode() == PPC::BCn) { |
| 388 | if (!LastInst->getOperand(1).isMBB()) |
| 389 | return true; |
| 390 | // Block ends with fall-through condbranch. |
| 391 | TBB = LastInst->getOperand(1).getMBB(); |
| 392 | Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_UNSET)); |
| 393 | Cond.push_back(LastInst->getOperand(0)); |
| 394 | return false; |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 395 | } else if (LastInst->getOpcode() == PPC::BDNZ8 || |
| 396 | LastInst->getOpcode() == PPC::BDNZ) { |
| 397 | if (!LastInst->getOperand(0).isMBB()) |
| 398 | return true; |
Hal Finkel | c6b5deb | 2012-06-08 19:19:53 +0000 | [diff] [blame] | 399 | if (DisableCTRLoopAnal) |
Hal Finkel | 821e001 | 2012-06-08 15:38:25 +0000 | [diff] [blame] | 400 | return true; |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 401 | TBB = LastInst->getOperand(0).getMBB(); |
| 402 | Cond.push_back(MachineOperand::CreateImm(1)); |
| 403 | Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR, |
| 404 | true)); |
| 405 | return false; |
| 406 | } else if (LastInst->getOpcode() == PPC::BDZ8 || |
| 407 | LastInst->getOpcode() == PPC::BDZ) { |
| 408 | if (!LastInst->getOperand(0).isMBB()) |
| 409 | return true; |
Hal Finkel | c6b5deb | 2012-06-08 19:19:53 +0000 | [diff] [blame] | 410 | if (DisableCTRLoopAnal) |
Hal Finkel | 821e001 | 2012-06-08 15:38:25 +0000 | [diff] [blame] | 411 | return true; |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 412 | TBB = LastInst->getOperand(0).getMBB(); |
| 413 | Cond.push_back(MachineOperand::CreateImm(0)); |
| 414 | Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR, |
| 415 | true)); |
| 416 | return false; |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 417 | } |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 418 | |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 419 | // Otherwise, don't know what this is. |
| 420 | return true; |
| 421 | } |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 422 | |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 423 | // Get the instruction before it if it's a terminator. |
| 424 | MachineInstr *SecondLastInst = I; |
| 425 | |
| 426 | // If there are three terminators, we don't know what sort of block this is. |
| 427 | if (SecondLastInst && I != MBB.begin() && |
Evan Cheng | 5514bbe | 2007-06-08 21:59:56 +0000 | [diff] [blame] | 428 | isUnpredicatedTerminator(--I)) |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 429 | return true; |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 430 | |
Chris Lattner | e026379 | 2006-11-17 22:14:47 +0000 | [diff] [blame] | 431 | // If the block ends with PPC::B and PPC:BCC, handle it. |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 432 | if (SecondLastInst->getOpcode() == PPC::BCC && |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 433 | LastInst->getOpcode() == PPC::B) { |
Evan Cheng | 8f43afd | 2009-05-08 23:09:25 +0000 | [diff] [blame] | 434 | if (!SecondLastInst->getOperand(2).isMBB() || |
| 435 | !LastInst->getOperand(0).isMBB()) |
| 436 | return true; |
Chris Lattner | a5bb370 | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 437 | TBB = SecondLastInst->getOperand(2).getMBB(); |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 438 | Cond.push_back(SecondLastInst->getOperand(0)); |
| 439 | Cond.push_back(SecondLastInst->getOperand(1)); |
Chris Lattner | a5bb370 | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 440 | FBB = LastInst->getOperand(0).getMBB(); |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 441 | return false; |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 442 | } else if (SecondLastInst->getOpcode() == PPC::BC && |
| 443 | LastInst->getOpcode() == PPC::B) { |
| 444 | if (!SecondLastInst->getOperand(1).isMBB() || |
| 445 | !LastInst->getOperand(0).isMBB()) |
| 446 | return true; |
| 447 | TBB = SecondLastInst->getOperand(1).getMBB(); |
| 448 | Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET)); |
| 449 | Cond.push_back(SecondLastInst->getOperand(0)); |
| 450 | FBB = LastInst->getOperand(0).getMBB(); |
| 451 | return false; |
| 452 | } else if (SecondLastInst->getOpcode() == PPC::BCn && |
| 453 | LastInst->getOpcode() == PPC::B) { |
| 454 | if (!SecondLastInst->getOperand(1).isMBB() || |
| 455 | !LastInst->getOperand(0).isMBB()) |
| 456 | return true; |
| 457 | TBB = SecondLastInst->getOperand(1).getMBB(); |
| 458 | Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_UNSET)); |
| 459 | Cond.push_back(SecondLastInst->getOperand(0)); |
| 460 | FBB = LastInst->getOperand(0).getMBB(); |
| 461 | return false; |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 462 | } else if ((SecondLastInst->getOpcode() == PPC::BDNZ8 || |
| 463 | SecondLastInst->getOpcode() == PPC::BDNZ) && |
| 464 | LastInst->getOpcode() == PPC::B) { |
| 465 | if (!SecondLastInst->getOperand(0).isMBB() || |
| 466 | !LastInst->getOperand(0).isMBB()) |
| 467 | return true; |
Hal Finkel | c6b5deb | 2012-06-08 19:19:53 +0000 | [diff] [blame] | 468 | if (DisableCTRLoopAnal) |
Hal Finkel | 821e001 | 2012-06-08 15:38:25 +0000 | [diff] [blame] | 469 | return true; |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 470 | TBB = SecondLastInst->getOperand(0).getMBB(); |
| 471 | Cond.push_back(MachineOperand::CreateImm(1)); |
| 472 | Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR, |
| 473 | true)); |
| 474 | FBB = LastInst->getOperand(0).getMBB(); |
| 475 | return false; |
| 476 | } else if ((SecondLastInst->getOpcode() == PPC::BDZ8 || |
| 477 | SecondLastInst->getOpcode() == PPC::BDZ) && |
| 478 | LastInst->getOpcode() == PPC::B) { |
| 479 | if (!SecondLastInst->getOperand(0).isMBB() || |
| 480 | !LastInst->getOperand(0).isMBB()) |
| 481 | return true; |
Hal Finkel | c6b5deb | 2012-06-08 19:19:53 +0000 | [diff] [blame] | 482 | if (DisableCTRLoopAnal) |
Hal Finkel | 821e001 | 2012-06-08 15:38:25 +0000 | [diff] [blame] | 483 | return true; |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 484 | TBB = SecondLastInst->getOperand(0).getMBB(); |
| 485 | Cond.push_back(MachineOperand::CreateImm(0)); |
| 486 | Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR, |
| 487 | true)); |
| 488 | FBB = LastInst->getOperand(0).getMBB(); |
| 489 | return false; |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 490 | } |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 491 | |
Dale Johannesen | c685546 | 2007-06-13 17:59:52 +0000 | [diff] [blame] | 492 | // If the block ends with two PPC:Bs, handle it. The second one is not |
| 493 | // executed, so remove it. |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 494 | if (SecondLastInst->getOpcode() == PPC::B && |
Dale Johannesen | c685546 | 2007-06-13 17:59:52 +0000 | [diff] [blame] | 495 | LastInst->getOpcode() == PPC::B) { |
Evan Cheng | 8f43afd | 2009-05-08 23:09:25 +0000 | [diff] [blame] | 496 | if (!SecondLastInst->getOperand(0).isMBB()) |
| 497 | return true; |
Chris Lattner | a5bb370 | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 498 | TBB = SecondLastInst->getOperand(0).getMBB(); |
Dale Johannesen | c685546 | 2007-06-13 17:59:52 +0000 | [diff] [blame] | 499 | I = LastInst; |
Evan Cheng | 64dfcac | 2009-02-09 07:14:22 +0000 | [diff] [blame] | 500 | if (AllowModify) |
| 501 | I->eraseFromParent(); |
Dale Johannesen | c685546 | 2007-06-13 17:59:52 +0000 | [diff] [blame] | 502 | return false; |
| 503 | } |
| 504 | |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 505 | // Otherwise, can't handle this. |
| 506 | return true; |
| 507 | } |
| 508 | |
Evan Cheng | 99be49d | 2007-05-18 00:05:48 +0000 | [diff] [blame] | 509 | unsigned PPCInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const { |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 510 | MachineBasicBlock::iterator I = MBB.end(); |
Evan Cheng | 99be49d | 2007-05-18 00:05:48 +0000 | [diff] [blame] | 511 | if (I == MBB.begin()) return 0; |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 512 | --I; |
Dale Johannesen | 4244d12 | 2010-04-02 01:38:09 +0000 | [diff] [blame] | 513 | while (I->isDebugValue()) { |
| 514 | if (I == MBB.begin()) |
| 515 | return 0; |
| 516 | --I; |
| 517 | } |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 518 | if (I->getOpcode() != PPC::B && I->getOpcode() != PPC::BCC && |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 519 | I->getOpcode() != PPC::BC && I->getOpcode() != PPC::BCn && |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 520 | I->getOpcode() != PPC::BDNZ8 && I->getOpcode() != PPC::BDNZ && |
| 521 | I->getOpcode() != PPC::BDZ8 && I->getOpcode() != PPC::BDZ) |
Evan Cheng | 99be49d | 2007-05-18 00:05:48 +0000 | [diff] [blame] | 522 | return 0; |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 523 | |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 524 | // Remove the branch. |
| 525 | I->eraseFromParent(); |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 526 | |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 527 | I = MBB.end(); |
| 528 | |
Evan Cheng | 99be49d | 2007-05-18 00:05:48 +0000 | [diff] [blame] | 529 | if (I == MBB.begin()) return 1; |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 530 | --I; |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 531 | if (I->getOpcode() != PPC::BCC && |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 532 | I->getOpcode() != PPC::BC && I->getOpcode() != PPC::BCn && |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 533 | I->getOpcode() != PPC::BDNZ8 && I->getOpcode() != PPC::BDNZ && |
| 534 | I->getOpcode() != PPC::BDZ8 && I->getOpcode() != PPC::BDZ) |
Evan Cheng | 99be49d | 2007-05-18 00:05:48 +0000 | [diff] [blame] | 535 | return 1; |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 536 | |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 537 | // Remove the branch. |
| 538 | I->eraseFromParent(); |
Evan Cheng | 99be49d | 2007-05-18 00:05:48 +0000 | [diff] [blame] | 539 | return 2; |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 540 | } |
| 541 | |
Evan Cheng | 99be49d | 2007-05-18 00:05:48 +0000 | [diff] [blame] | 542 | unsigned |
| 543 | PPCInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB, |
| 544 | MachineBasicBlock *FBB, |
Stuart Hastings | 0125b64 | 2010-06-17 22:43:56 +0000 | [diff] [blame] | 545 | const SmallVectorImpl<MachineOperand> &Cond, |
| 546 | DebugLoc DL) const { |
Chris Lattner | a61f010 | 2006-10-17 18:06:55 +0000 | [diff] [blame] | 547 | // Shouldn't be a fall through. |
| 548 | assert(TBB && "InsertBranch must not be told to insert a fallthrough"); |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 549 | assert((Cond.size() == 2 || Cond.size() == 0) && |
Chris Lattner | 94e0444 | 2006-10-21 05:36:13 +0000 | [diff] [blame] | 550 | "PPC branch conditions have two components!"); |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 551 | |
Eric Christopher | 1dcea73 | 2014-06-12 21:48:52 +0000 | [diff] [blame] | 552 | bool isPPC64 = Subtarget.isPPC64(); |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 553 | |
Chris Lattner | 94e0444 | 2006-10-21 05:36:13 +0000 | [diff] [blame] | 554 | // One-way branch. |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 555 | if (!FBB) { |
Chris Lattner | 94e0444 | 2006-10-21 05:36:13 +0000 | [diff] [blame] | 556 | if (Cond.empty()) // Unconditional branch |
Stuart Hastings | 0125b64 | 2010-06-17 22:43:56 +0000 | [diff] [blame] | 557 | BuildMI(&MBB, DL, get(PPC::B)).addMBB(TBB); |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 558 | else if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8) |
| 559 | BuildMI(&MBB, DL, get(Cond[0].getImm() ? |
| 560 | (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) : |
| 561 | (isPPC64 ? PPC::BDZ8 : PPC::BDZ))).addMBB(TBB); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 562 | else if (Cond[0].getImm() == PPC::PRED_BIT_SET) |
| 563 | BuildMI(&MBB, DL, get(PPC::BC)).addOperand(Cond[1]).addMBB(TBB); |
| 564 | else if (Cond[0].getImm() == PPC::PRED_BIT_UNSET) |
| 565 | BuildMI(&MBB, DL, get(PPC::BCn)).addOperand(Cond[1]).addMBB(TBB); |
Chris Lattner | 94e0444 | 2006-10-21 05:36:13 +0000 | [diff] [blame] | 566 | else // Conditional branch |
Stuart Hastings | 0125b64 | 2010-06-17 22:43:56 +0000 | [diff] [blame] | 567 | BuildMI(&MBB, DL, get(PPC::BCC)) |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 568 | .addImm(Cond[0].getImm()).addOperand(Cond[1]).addMBB(TBB); |
Evan Cheng | 99be49d | 2007-05-18 00:05:48 +0000 | [diff] [blame] | 569 | return 1; |
Chris Lattner | a61f010 | 2006-10-17 18:06:55 +0000 | [diff] [blame] | 570 | } |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 571 | |
Chris Lattner | d881660 | 2006-10-21 05:42:09 +0000 | [diff] [blame] | 572 | // Two-way Conditional Branch. |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 573 | if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8) |
| 574 | BuildMI(&MBB, DL, get(Cond[0].getImm() ? |
| 575 | (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) : |
| 576 | (isPPC64 ? PPC::BDZ8 : PPC::BDZ))).addMBB(TBB); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 577 | else if (Cond[0].getImm() == PPC::PRED_BIT_SET) |
| 578 | BuildMI(&MBB, DL, get(PPC::BC)).addOperand(Cond[1]).addMBB(TBB); |
| 579 | else if (Cond[0].getImm() == PPC::PRED_BIT_UNSET) |
| 580 | BuildMI(&MBB, DL, get(PPC::BCn)).addOperand(Cond[1]).addMBB(TBB); |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 581 | else |
| 582 | BuildMI(&MBB, DL, get(PPC::BCC)) |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 583 | .addImm(Cond[0].getImm()).addOperand(Cond[1]).addMBB(TBB); |
Stuart Hastings | 0125b64 | 2010-06-17 22:43:56 +0000 | [diff] [blame] | 584 | BuildMI(&MBB, DL, get(PPC::B)).addMBB(FBB); |
Evan Cheng | 99be49d | 2007-05-18 00:05:48 +0000 | [diff] [blame] | 585 | return 2; |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 586 | } |
| 587 | |
Hal Finkel | ed6a285 | 2013-04-05 23:29:01 +0000 | [diff] [blame] | 588 | // Select analysis. |
| 589 | bool PPCInstrInfo::canInsertSelect(const MachineBasicBlock &MBB, |
| 590 | const SmallVectorImpl<MachineOperand> &Cond, |
| 591 | unsigned TrueReg, unsigned FalseReg, |
| 592 | int &CondCycles, int &TrueCycles, int &FalseCycles) const { |
Eric Christopher | 1dcea73 | 2014-06-12 21:48:52 +0000 | [diff] [blame] | 593 | if (!Subtarget.hasISEL()) |
Hal Finkel | ed6a285 | 2013-04-05 23:29:01 +0000 | [diff] [blame] | 594 | return false; |
| 595 | |
| 596 | if (Cond.size() != 2) |
| 597 | return false; |
| 598 | |
| 599 | // If this is really a bdnz-like condition, then it cannot be turned into a |
| 600 | // select. |
| 601 | if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8) |
| 602 | return false; |
| 603 | |
| 604 | // Check register classes. |
| 605 | const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); |
| 606 | const TargetRegisterClass *RC = |
| 607 | RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg)); |
| 608 | if (!RC) |
| 609 | return false; |
| 610 | |
| 611 | // isel is for regular integer GPRs only. |
| 612 | if (!PPC::GPRCRegClass.hasSubClassEq(RC) && |
Hal Finkel | 8e8618a | 2013-07-15 20:22:58 +0000 | [diff] [blame] | 613 | !PPC::GPRC_NOR0RegClass.hasSubClassEq(RC) && |
| 614 | !PPC::G8RCRegClass.hasSubClassEq(RC) && |
| 615 | !PPC::G8RC_NOX0RegClass.hasSubClassEq(RC)) |
Hal Finkel | ed6a285 | 2013-04-05 23:29:01 +0000 | [diff] [blame] | 616 | return false; |
| 617 | |
| 618 | // FIXME: These numbers are for the A2, how well they work for other cores is |
| 619 | // an open question. On the A2, the isel instruction has a 2-cycle latency |
| 620 | // but single-cycle throughput. These numbers are used in combination with |
| 621 | // the MispredictPenalty setting from the active SchedMachineModel. |
| 622 | CondCycles = 1; |
| 623 | TrueCycles = 1; |
| 624 | FalseCycles = 1; |
| 625 | |
| 626 | return true; |
| 627 | } |
| 628 | |
| 629 | void PPCInstrInfo::insertSelect(MachineBasicBlock &MBB, |
| 630 | MachineBasicBlock::iterator MI, DebugLoc dl, |
| 631 | unsigned DestReg, |
| 632 | const SmallVectorImpl<MachineOperand> &Cond, |
| 633 | unsigned TrueReg, unsigned FalseReg) const { |
| 634 | assert(Cond.size() == 2 && |
| 635 | "PPC branch conditions have two components!"); |
| 636 | |
Eric Christopher | 1dcea73 | 2014-06-12 21:48:52 +0000 | [diff] [blame] | 637 | assert(Subtarget.hasISEL() && |
Hal Finkel | ed6a285 | 2013-04-05 23:29:01 +0000 | [diff] [blame] | 638 | "Cannot insert select on target without ISEL support"); |
| 639 | |
| 640 | // Get the register classes. |
| 641 | MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); |
| 642 | const TargetRegisterClass *RC = |
| 643 | RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg)); |
| 644 | assert(RC && "TrueReg and FalseReg must have overlapping register classes"); |
Hal Finkel | 8e8618a | 2013-07-15 20:22:58 +0000 | [diff] [blame] | 645 | |
| 646 | bool Is64Bit = PPC::G8RCRegClass.hasSubClassEq(RC) || |
| 647 | PPC::G8RC_NOX0RegClass.hasSubClassEq(RC); |
| 648 | assert((Is64Bit || |
| 649 | PPC::GPRCRegClass.hasSubClassEq(RC) || |
| 650 | PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) && |
Hal Finkel | ed6a285 | 2013-04-05 23:29:01 +0000 | [diff] [blame] | 651 | "isel is for regular integer GPRs only"); |
| 652 | |
Hal Finkel | 8e8618a | 2013-07-15 20:22:58 +0000 | [diff] [blame] | 653 | unsigned OpCode = Is64Bit ? PPC::ISEL8 : PPC::ISEL; |
Hal Finkel | ed6a285 | 2013-04-05 23:29:01 +0000 | [diff] [blame] | 654 | unsigned SelectPred = Cond[0].getImm(); |
| 655 | |
| 656 | unsigned SubIdx; |
| 657 | bool SwapOps; |
| 658 | switch (SelectPred) { |
| 659 | default: llvm_unreachable("invalid predicate for isel"); |
| 660 | case PPC::PRED_EQ: SubIdx = PPC::sub_eq; SwapOps = false; break; |
| 661 | case PPC::PRED_NE: SubIdx = PPC::sub_eq; SwapOps = true; break; |
| 662 | case PPC::PRED_LT: SubIdx = PPC::sub_lt; SwapOps = false; break; |
| 663 | case PPC::PRED_GE: SubIdx = PPC::sub_lt; SwapOps = true; break; |
| 664 | case PPC::PRED_GT: SubIdx = PPC::sub_gt; SwapOps = false; break; |
| 665 | case PPC::PRED_LE: SubIdx = PPC::sub_gt; SwapOps = true; break; |
| 666 | case PPC::PRED_UN: SubIdx = PPC::sub_un; SwapOps = false; break; |
| 667 | case PPC::PRED_NU: SubIdx = PPC::sub_un; SwapOps = true; break; |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 668 | case PPC::PRED_BIT_SET: SubIdx = 0; SwapOps = false; break; |
| 669 | case PPC::PRED_BIT_UNSET: SubIdx = 0; SwapOps = true; break; |
Hal Finkel | ed6a285 | 2013-04-05 23:29:01 +0000 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | unsigned FirstReg = SwapOps ? FalseReg : TrueReg, |
| 673 | SecondReg = SwapOps ? TrueReg : FalseReg; |
| 674 | |
| 675 | // The first input register of isel cannot be r0. If it is a member |
| 676 | // of a register class that can be r0, then copy it first (the |
| 677 | // register allocator should eliminate the copy). |
| 678 | if (MRI.getRegClass(FirstReg)->contains(PPC::R0) || |
| 679 | MRI.getRegClass(FirstReg)->contains(PPC::X0)) { |
| 680 | const TargetRegisterClass *FirstRC = |
| 681 | MRI.getRegClass(FirstReg)->contains(PPC::X0) ? |
| 682 | &PPC::G8RC_NOX0RegClass : &PPC::GPRC_NOR0RegClass; |
| 683 | unsigned OldFirstReg = FirstReg; |
| 684 | FirstReg = MRI.createVirtualRegister(FirstRC); |
| 685 | BuildMI(MBB, MI, dl, get(TargetOpcode::COPY), FirstReg) |
| 686 | .addReg(OldFirstReg); |
| 687 | } |
| 688 | |
| 689 | BuildMI(MBB, MI, dl, get(OpCode), DestReg) |
| 690 | .addReg(FirstReg).addReg(SecondReg) |
| 691 | .addReg(Cond[1].getReg(), 0, SubIdx); |
| 692 | } |
| 693 | |
Jakob Stoklund Olesen | 0d61197 | 2010-07-11 07:31:00 +0000 | [diff] [blame] | 694 | void PPCInstrInfo::copyPhysReg(MachineBasicBlock &MBB, |
| 695 | MachineBasicBlock::iterator I, DebugLoc DL, |
| 696 | unsigned DestReg, unsigned SrcReg, |
| 697 | bool KillSrc) const { |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 698 | // We can end up with self copies and similar things as a result of VSX copy |
Hal Finkel | 9dcb358 | 2014-03-27 22:46:28 +0000 | [diff] [blame] | 699 | // legalization. Promote them here. |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 700 | const TargetRegisterInfo *TRI = &getRegisterInfo(); |
| 701 | if (PPC::F8RCRegClass.contains(DestReg) && |
| 702 | PPC::VSLRCRegClass.contains(SrcReg)) { |
| 703 | unsigned SuperReg = |
| 704 | TRI->getMatchingSuperReg(DestReg, PPC::sub_64, &PPC::VSRCRegClass); |
| 705 | |
Hal Finkel | 9dcb358 | 2014-03-27 22:46:28 +0000 | [diff] [blame] | 706 | if (VSXSelfCopyCrash && SrcReg == SuperReg) |
| 707 | llvm_unreachable("nop VSX copy"); |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 708 | |
| 709 | DestReg = SuperReg; |
| 710 | } else if (PPC::VRRCRegClass.contains(DestReg) && |
| 711 | PPC::VSHRCRegClass.contains(SrcReg)) { |
| 712 | unsigned SuperReg = |
| 713 | TRI->getMatchingSuperReg(DestReg, PPC::sub_128, &PPC::VSRCRegClass); |
| 714 | |
Hal Finkel | 9dcb358 | 2014-03-27 22:46:28 +0000 | [diff] [blame] | 715 | if (VSXSelfCopyCrash && SrcReg == SuperReg) |
| 716 | llvm_unreachable("nop VSX copy"); |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 717 | |
| 718 | DestReg = SuperReg; |
| 719 | } else if (PPC::F8RCRegClass.contains(SrcReg) && |
| 720 | PPC::VSLRCRegClass.contains(DestReg)) { |
| 721 | unsigned SuperReg = |
| 722 | TRI->getMatchingSuperReg(SrcReg, PPC::sub_64, &PPC::VSRCRegClass); |
| 723 | |
Hal Finkel | 9dcb358 | 2014-03-27 22:46:28 +0000 | [diff] [blame] | 724 | if (VSXSelfCopyCrash && DestReg == SuperReg) |
| 725 | llvm_unreachable("nop VSX copy"); |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 726 | |
| 727 | SrcReg = SuperReg; |
| 728 | } else if (PPC::VRRCRegClass.contains(SrcReg) && |
| 729 | PPC::VSHRCRegClass.contains(DestReg)) { |
| 730 | unsigned SuperReg = |
| 731 | TRI->getMatchingSuperReg(SrcReg, PPC::sub_128, &PPC::VSRCRegClass); |
| 732 | |
Hal Finkel | 9dcb358 | 2014-03-27 22:46:28 +0000 | [diff] [blame] | 733 | if (VSXSelfCopyCrash && DestReg == SuperReg) |
| 734 | llvm_unreachable("nop VSX copy"); |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 735 | |
| 736 | SrcReg = SuperReg; |
| 737 | } |
| 738 | |
Jakob Stoklund Olesen | 0d61197 | 2010-07-11 07:31:00 +0000 | [diff] [blame] | 739 | unsigned Opc; |
| 740 | if (PPC::GPRCRegClass.contains(DestReg, SrcReg)) |
| 741 | Opc = PPC::OR; |
| 742 | else if (PPC::G8RCRegClass.contains(DestReg, SrcReg)) |
| 743 | Opc = PPC::OR8; |
| 744 | else if (PPC::F4RCRegClass.contains(DestReg, SrcReg)) |
| 745 | Opc = PPC::FMR; |
| 746 | else if (PPC::CRRCRegClass.contains(DestReg, SrcReg)) |
| 747 | Opc = PPC::MCRF; |
| 748 | else if (PPC::VRRCRegClass.contains(DestReg, SrcReg)) |
| 749 | Opc = PPC::VOR; |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 750 | else if (PPC::VSRCRegClass.contains(DestReg, SrcReg)) |
Hal Finkel | bbad233 | 2014-03-24 09:36:36 +0000 | [diff] [blame] | 751 | // There are two different ways this can be done: |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 752 | // 1. xxlor : This has lower latency (on the P7), 2 cycles, but can only |
| 753 | // issue in VSU pipeline 0. |
| 754 | // 2. xmovdp/xmovsp: This has higher latency (on the P7), 6 cycles, but |
| 755 | // can go to either pipeline. |
Hal Finkel | bbad233 | 2014-03-24 09:36:36 +0000 | [diff] [blame] | 756 | // We'll always use xxlor here, because in practically all cases where |
| 757 | // copies are generated, they are close enough to some use that the |
| 758 | // lower-latency form is preferable. |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 759 | Opc = PPC::XXLOR; |
Hal Finkel | 19be506 | 2014-03-29 05:29:01 +0000 | [diff] [blame] | 760 | else if (PPC::VSFRCRegClass.contains(DestReg, SrcReg)) |
| 761 | Opc = PPC::XXLORf; |
Jakob Stoklund Olesen | 0d61197 | 2010-07-11 07:31:00 +0000 | [diff] [blame] | 762 | else if (PPC::CRBITRCRegClass.contains(DestReg, SrcReg)) |
| 763 | Opc = PPC::CROR; |
| 764 | else |
| 765 | llvm_unreachable("Impossible reg-to-reg copy"); |
Owen Anderson | 7a73ae9 | 2007-12-31 06:32:00 +0000 | [diff] [blame] | 766 | |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 767 | const MCInstrDesc &MCID = get(Opc); |
| 768 | if (MCID.getNumOperands() == 3) |
| 769 | BuildMI(MBB, I, DL, MCID, DestReg) |
Jakob Stoklund Olesen | 0d61197 | 2010-07-11 07:31:00 +0000 | [diff] [blame] | 770 | .addReg(SrcReg).addReg(SrcReg, getKillRegState(KillSrc)); |
| 771 | else |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 772 | BuildMI(MBB, I, DL, MCID, DestReg).addReg(SrcReg, getKillRegState(KillSrc)); |
Owen Anderson | 7a73ae9 | 2007-12-31 06:32:00 +0000 | [diff] [blame] | 773 | } |
| 774 | |
Hal Finkel | 8f6834d | 2011-12-05 17:55:17 +0000 | [diff] [blame] | 775 | // This function returns true if a CR spill is necessary and false otherwise. |
Bill Wendling | c6c48fc | 2008-03-10 22:49:16 +0000 | [diff] [blame] | 776 | bool |
Dan Gohman | 3b46030 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 777 | PPCInstrInfo::StoreRegToStackSlot(MachineFunction &MF, |
| 778 | unsigned SrcReg, bool isKill, |
Bill Wendling | c6c48fc | 2008-03-10 22:49:16 +0000 | [diff] [blame] | 779 | int FrameIdx, |
| 780 | const TargetRegisterClass *RC, |
Hal Finkel | fcc51d4 | 2013-03-17 04:43:44 +0000 | [diff] [blame] | 781 | SmallVectorImpl<MachineInstr*> &NewMIs, |
Hal Finkel | cc1eeda | 2013-03-23 22:06:03 +0000 | [diff] [blame] | 782 | bool &NonRI, bool &SpillsVRS) const{ |
Hal Finkel | 37714b8 | 2013-03-27 21:21:15 +0000 | [diff] [blame] | 783 | // Note: If additional store instructions are added here, |
| 784 | // update isStoreToStackSlot. |
| 785 | |
Chris Lattner | 6f306d7 | 2010-04-02 20:16:16 +0000 | [diff] [blame] | 786 | DebugLoc DL; |
Hal Finkel | 4e703bc | 2014-01-28 05:32:58 +0000 | [diff] [blame] | 787 | if (PPC::GPRCRegClass.hasSubClassEq(RC) || |
| 788 | PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) { |
Hal Finkel | 794e05b | 2013-03-23 17:14:27 +0000 | [diff] [blame] | 789 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW)) |
| 790 | .addReg(SrcReg, |
| 791 | getKillRegState(isKill)), |
| 792 | FrameIdx)); |
Hal Finkel | 4e703bc | 2014-01-28 05:32:58 +0000 | [diff] [blame] | 793 | } else if (PPC::G8RCRegClass.hasSubClassEq(RC) || |
| 794 | PPC::G8RC_NOX0RegClass.hasSubClassEq(RC)) { |
Hal Finkel | 794e05b | 2013-03-23 17:14:27 +0000 | [diff] [blame] | 795 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STD)) |
| 796 | .addReg(SrcReg, |
| 797 | getKillRegState(isKill)), |
| 798 | FrameIdx)); |
Craig Topper | abadc66 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 799 | } else if (PPC::F8RCRegClass.hasSubClassEq(RC)) { |
Dale Johannesen | 6b8c76a | 2009-02-12 23:08:38 +0000 | [diff] [blame] | 800 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFD)) |
Bill Wendling | f7b83c7 | 2009-05-13 21:33:08 +0000 | [diff] [blame] | 801 | .addReg(SrcReg, |
| 802 | getKillRegState(isKill)), |
| 803 | FrameIdx)); |
Craig Topper | abadc66 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 804 | } else if (PPC::F4RCRegClass.hasSubClassEq(RC)) { |
Dale Johannesen | 6b8c76a | 2009-02-12 23:08:38 +0000 | [diff] [blame] | 805 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFS)) |
Bill Wendling | f7b83c7 | 2009-05-13 21:33:08 +0000 | [diff] [blame] | 806 | .addReg(SrcReg, |
| 807 | getKillRegState(isKill)), |
| 808 | FrameIdx)); |
Craig Topper | abadc66 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 809 | } else if (PPC::CRRCRegClass.hasSubClassEq(RC)) { |
Hal Finkel | e154c8f | 2013-03-12 14:12:16 +0000 | [diff] [blame] | 810 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CR)) |
| 811 | .addReg(SrcReg, |
| 812 | getKillRegState(isKill)), |
| 813 | FrameIdx)); |
| 814 | return true; |
Craig Topper | abadc66 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 815 | } else if (PPC::CRBITRCRegClass.hasSubClassEq(RC)) { |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 816 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CRBIT)) |
| 817 | .addReg(SrcReg, |
| 818 | getKillRegState(isKill)), |
| 819 | FrameIdx)); |
| 820 | return true; |
Craig Topper | abadc66 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 821 | } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) { |
Hal Finkel | fcc51d4 | 2013-03-17 04:43:44 +0000 | [diff] [blame] | 822 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STVX)) |
| 823 | .addReg(SrcReg, |
| 824 | getKillRegState(isKill)), |
| 825 | FrameIdx)); |
| 826 | NonRI = true; |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 827 | } else if (PPC::VSRCRegClass.hasSubClassEq(RC)) { |
| 828 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STXVD2X)) |
| 829 | .addReg(SrcReg, |
| 830 | getKillRegState(isKill)), |
| 831 | FrameIdx)); |
| 832 | NonRI = true; |
Hal Finkel | 19be506 | 2014-03-29 05:29:01 +0000 | [diff] [blame] | 833 | } else if (PPC::VSFRCRegClass.hasSubClassEq(RC)) { |
| 834 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STXSDX)) |
| 835 | .addReg(SrcReg, |
| 836 | getKillRegState(isKill)), |
| 837 | FrameIdx)); |
| 838 | NonRI = true; |
Hal Finkel | a1431df | 2013-03-21 19:03:21 +0000 | [diff] [blame] | 839 | } else if (PPC::VRSAVERCRegClass.hasSubClassEq(RC)) { |
Eric Christopher | 1dcea73 | 2014-06-12 21:48:52 +0000 | [diff] [blame] | 840 | assert(Subtarget.isDarwin() && |
Hal Finkel | a7b0630 | 2013-03-27 00:02:20 +0000 | [diff] [blame] | 841 | "VRSAVE only needs spill/restore on Darwin"); |
Hal Finkel | a1431df | 2013-03-21 19:03:21 +0000 | [diff] [blame] | 842 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_VRSAVE)) |
| 843 | .addReg(SrcReg, |
| 844 | getKillRegState(isKill)), |
| 845 | FrameIdx)); |
Hal Finkel | cc1eeda | 2013-03-23 22:06:03 +0000 | [diff] [blame] | 846 | SpillsVRS = true; |
Owen Anderson | eee1460 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 847 | } else { |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 848 | llvm_unreachable("Unknown regclass!"); |
Owen Anderson | eee1460 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 849 | } |
Bill Wendling | 632ea65 | 2008-03-03 22:19:16 +0000 | [diff] [blame] | 850 | |
| 851 | return false; |
Owen Anderson | eee1460 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | void |
| 855 | PPCInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB, |
Bill Wendling | 632ea65 | 2008-03-03 22:19:16 +0000 | [diff] [blame] | 856 | MachineBasicBlock::iterator MI, |
| 857 | unsigned SrcReg, bool isKill, int FrameIdx, |
Evan Cheng | efb126a | 2010-05-06 19:06:44 +0000 | [diff] [blame] | 858 | const TargetRegisterClass *RC, |
| 859 | const TargetRegisterInfo *TRI) const { |
Dan Gohman | 3b46030 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 860 | MachineFunction &MF = *MBB.getParent(); |
Owen Anderson | eee1460 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 861 | SmallVector<MachineInstr*, 4> NewMIs; |
Bill Wendling | 632ea65 | 2008-03-03 22:19:16 +0000 | [diff] [blame] | 862 | |
Hal Finkel | bb420f1 | 2013-03-15 05:06:04 +0000 | [diff] [blame] | 863 | PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); |
| 864 | FuncInfo->setHasSpills(); |
| 865 | |
Hal Finkel | cc1eeda | 2013-03-23 22:06:03 +0000 | [diff] [blame] | 866 | bool NonRI = false, SpillsVRS = false; |
| 867 | if (StoreRegToStackSlot(MF, SrcReg, isKill, FrameIdx, RC, NewMIs, |
| 868 | NonRI, SpillsVRS)) |
Bill Wendling | 632ea65 | 2008-03-03 22:19:16 +0000 | [diff] [blame] | 869 | FuncInfo->setSpillsCR(); |
Bill Wendling | 632ea65 | 2008-03-03 22:19:16 +0000 | [diff] [blame] | 870 | |
Hal Finkel | cc1eeda | 2013-03-23 22:06:03 +0000 | [diff] [blame] | 871 | if (SpillsVRS) |
| 872 | FuncInfo->setSpillsVRSAVE(); |
| 873 | |
Hal Finkel | fcc51d4 | 2013-03-17 04:43:44 +0000 | [diff] [blame] | 874 | if (NonRI) |
| 875 | FuncInfo->setHasNonRISpills(); |
| 876 | |
Owen Anderson | eee1460 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 877 | for (unsigned i = 0, e = NewMIs.size(); i != e; ++i) |
| 878 | MBB.insert(MI, NewMIs[i]); |
Jakob Stoklund Olesen | 6353e53 | 2010-07-16 18:22:00 +0000 | [diff] [blame] | 879 | |
| 880 | const MachineFrameInfo &MFI = *MF.getFrameInfo(); |
| 881 | MachineMemOperand *MMO = |
Jay Foad | 465101b | 2011-11-15 07:34:52 +0000 | [diff] [blame] | 882 | MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx), |
Chris Lattner | e3d864b | 2010-09-21 04:39:43 +0000 | [diff] [blame] | 883 | MachineMemOperand::MOStore, |
Jakob Stoklund Olesen | 6353e53 | 2010-07-16 18:22:00 +0000 | [diff] [blame] | 884 | MFI.getObjectSize(FrameIdx), |
| 885 | MFI.getObjectAlignment(FrameIdx)); |
| 886 | NewMIs.back()->addMemOperand(MF, MMO); |
Owen Anderson | eee1460 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 887 | } |
| 888 | |
Hal Finkel | bde7f8f | 2011-12-06 20:55:36 +0000 | [diff] [blame] | 889 | bool |
Bill Wendling | f6d609a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 890 | PPCInstrInfo::LoadRegFromStackSlot(MachineFunction &MF, DebugLoc DL, |
Dan Gohman | 3b46030 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 891 | unsigned DestReg, int FrameIdx, |
Bill Wendling | c6c48fc | 2008-03-10 22:49:16 +0000 | [diff] [blame] | 892 | const TargetRegisterClass *RC, |
Hal Finkel | fcc51d4 | 2013-03-17 04:43:44 +0000 | [diff] [blame] | 893 | SmallVectorImpl<MachineInstr*> &NewMIs, |
Hal Finkel | cc1eeda | 2013-03-23 22:06:03 +0000 | [diff] [blame] | 894 | bool &NonRI, bool &SpillsVRS) const{ |
Hal Finkel | 37714b8 | 2013-03-27 21:21:15 +0000 | [diff] [blame] | 895 | // Note: If additional load instructions are added here, |
| 896 | // update isLoadFromStackSlot. |
| 897 | |
Hal Finkel | 4e703bc | 2014-01-28 05:32:58 +0000 | [diff] [blame] | 898 | if (PPC::GPRCRegClass.hasSubClassEq(RC) || |
| 899 | PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) { |
Hal Finkel | 5791f51 | 2013-03-27 19:10:40 +0000 | [diff] [blame] | 900 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ), |
| 901 | DestReg), FrameIdx)); |
Hal Finkel | 4e703bc | 2014-01-28 05:32:58 +0000 | [diff] [blame] | 902 | } else if (PPC::G8RCRegClass.hasSubClassEq(RC) || |
| 903 | PPC::G8RC_NOX0RegClass.hasSubClassEq(RC)) { |
Hal Finkel | 5791f51 | 2013-03-27 19:10:40 +0000 | [diff] [blame] | 904 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LD), DestReg), |
| 905 | FrameIdx)); |
Craig Topper | abadc66 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 906 | } else if (PPC::F8RCRegClass.hasSubClassEq(RC)) { |
Bill Wendling | f6d609a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 907 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFD), DestReg), |
Owen Anderson | eee1460 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 908 | FrameIdx)); |
Craig Topper | abadc66 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 909 | } else if (PPC::F4RCRegClass.hasSubClassEq(RC)) { |
Bill Wendling | f6d609a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 910 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFS), DestReg), |
Owen Anderson | eee1460 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 911 | FrameIdx)); |
Craig Topper | abadc66 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 912 | } else if (PPC::CRRCRegClass.hasSubClassEq(RC)) { |
Hal Finkel | e154c8f | 2013-03-12 14:12:16 +0000 | [diff] [blame] | 913 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, |
| 914 | get(PPC::RESTORE_CR), DestReg), |
| 915 | FrameIdx)); |
| 916 | return true; |
Craig Topper | abadc66 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 917 | } else if (PPC::CRBITRCRegClass.hasSubClassEq(RC)) { |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 918 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, |
| 919 | get(PPC::RESTORE_CRBIT), DestReg), |
| 920 | FrameIdx)); |
| 921 | return true; |
Craig Topper | abadc66 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 922 | } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) { |
Hal Finkel | fcc51d4 | 2013-03-17 04:43:44 +0000 | [diff] [blame] | 923 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LVX), DestReg), |
| 924 | FrameIdx)); |
| 925 | NonRI = true; |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 926 | } else if (PPC::VSRCRegClass.hasSubClassEq(RC)) { |
| 927 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LXVD2X), DestReg), |
| 928 | FrameIdx)); |
| 929 | NonRI = true; |
Hal Finkel | 19be506 | 2014-03-29 05:29:01 +0000 | [diff] [blame] | 930 | } else if (PPC::VSFRCRegClass.hasSubClassEq(RC)) { |
| 931 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LXSDX), DestReg), |
| 932 | FrameIdx)); |
| 933 | NonRI = true; |
Hal Finkel | a1431df | 2013-03-21 19:03:21 +0000 | [diff] [blame] | 934 | } else if (PPC::VRSAVERCRegClass.hasSubClassEq(RC)) { |
Eric Christopher | 1dcea73 | 2014-06-12 21:48:52 +0000 | [diff] [blame] | 935 | assert(Subtarget.isDarwin() && |
Hal Finkel | a7b0630 | 2013-03-27 00:02:20 +0000 | [diff] [blame] | 936 | "VRSAVE only needs spill/restore on Darwin"); |
Hal Finkel | a1431df | 2013-03-21 19:03:21 +0000 | [diff] [blame] | 937 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, |
| 938 | get(PPC::RESTORE_VRSAVE), |
| 939 | DestReg), |
| 940 | FrameIdx)); |
Hal Finkel | cc1eeda | 2013-03-23 22:06:03 +0000 | [diff] [blame] | 941 | SpillsVRS = true; |
Owen Anderson | eee1460 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 942 | } else { |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 943 | llvm_unreachable("Unknown regclass!"); |
Owen Anderson | eee1460 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 944 | } |
Hal Finkel | bde7f8f | 2011-12-06 20:55:36 +0000 | [diff] [blame] | 945 | |
| 946 | return false; |
Owen Anderson | eee1460 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 947 | } |
| 948 | |
| 949 | void |
| 950 | PPCInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB, |
Bill Wendling | 632ea65 | 2008-03-03 22:19:16 +0000 | [diff] [blame] | 951 | MachineBasicBlock::iterator MI, |
| 952 | unsigned DestReg, int FrameIdx, |
Evan Cheng | efb126a | 2010-05-06 19:06:44 +0000 | [diff] [blame] | 953 | const TargetRegisterClass *RC, |
| 954 | const TargetRegisterInfo *TRI) const { |
Dan Gohman | 3b46030 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 955 | MachineFunction &MF = *MBB.getParent(); |
Owen Anderson | eee1460 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 956 | SmallVector<MachineInstr*, 4> NewMIs; |
Chris Lattner | 6f306d7 | 2010-04-02 20:16:16 +0000 | [diff] [blame] | 957 | DebugLoc DL; |
Bill Wendling | f6d609a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 958 | if (MI != MBB.end()) DL = MI->getDebugLoc(); |
Hal Finkel | fcc51d4 | 2013-03-17 04:43:44 +0000 | [diff] [blame] | 959 | |
| 960 | PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); |
| 961 | FuncInfo->setHasSpills(); |
| 962 | |
Hal Finkel | cc1eeda | 2013-03-23 22:06:03 +0000 | [diff] [blame] | 963 | bool NonRI = false, SpillsVRS = false; |
| 964 | if (LoadRegFromStackSlot(MF, DL, DestReg, FrameIdx, RC, NewMIs, |
| 965 | NonRI, SpillsVRS)) |
Hal Finkel | bde7f8f | 2011-12-06 20:55:36 +0000 | [diff] [blame] | 966 | FuncInfo->setSpillsCR(); |
Hal Finkel | fcc51d4 | 2013-03-17 04:43:44 +0000 | [diff] [blame] | 967 | |
Hal Finkel | cc1eeda | 2013-03-23 22:06:03 +0000 | [diff] [blame] | 968 | if (SpillsVRS) |
| 969 | FuncInfo->setSpillsVRSAVE(); |
| 970 | |
Hal Finkel | fcc51d4 | 2013-03-17 04:43:44 +0000 | [diff] [blame] | 971 | if (NonRI) |
| 972 | FuncInfo->setHasNonRISpills(); |
| 973 | |
Owen Anderson | eee1460 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 974 | for (unsigned i = 0, e = NewMIs.size(); i != e; ++i) |
| 975 | MBB.insert(MI, NewMIs[i]); |
Jakob Stoklund Olesen | 6353e53 | 2010-07-16 18:22:00 +0000 | [diff] [blame] | 976 | |
| 977 | const MachineFrameInfo &MFI = *MF.getFrameInfo(); |
| 978 | MachineMemOperand *MMO = |
Jay Foad | 465101b | 2011-11-15 07:34:52 +0000 | [diff] [blame] | 979 | MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx), |
Chris Lattner | e3d864b | 2010-09-21 04:39:43 +0000 | [diff] [blame] | 980 | MachineMemOperand::MOLoad, |
Jakob Stoklund Olesen | 6353e53 | 2010-07-16 18:22:00 +0000 | [diff] [blame] | 981 | MFI.getObjectSize(FrameIdx), |
| 982 | MFI.getObjectAlignment(FrameIdx)); |
| 983 | NewMIs.back()->addMemOperand(MF, MMO); |
Owen Anderson | eee1460 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 984 | } |
| 985 | |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 986 | bool PPCInstrInfo:: |
Owen Anderson | 4f6bf04 | 2008-08-14 22:49:33 +0000 | [diff] [blame] | 987 | ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const { |
Chris Lattner | 23f22de | 2006-10-21 06:03:11 +0000 | [diff] [blame] | 988 | assert(Cond.size() == 2 && "Invalid PPC branch opcode!"); |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 989 | if (Cond[1].getReg() == PPC::CTR8 || Cond[1].getReg() == PPC::CTR) |
| 990 | Cond[0].setImm(Cond[0].getImm() == 0 ? 1 : 0); |
| 991 | else |
| 992 | // Leave the CR# the same, but invert the condition. |
| 993 | Cond[0].setImm(PPC::InvertPredicate((PPC::Predicate)Cond[0].getImm())); |
Chris Lattner | 23f22de | 2006-10-21 06:03:11 +0000 | [diff] [blame] | 994 | return false; |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 995 | } |
Nicolas Geoffray | ae84bbd | 2008-04-16 20:10:13 +0000 | [diff] [blame] | 996 | |
Hal Finkel | d61d4f8 | 2013-04-06 19:30:30 +0000 | [diff] [blame] | 997 | bool PPCInstrInfo::FoldImmediate(MachineInstr *UseMI, MachineInstr *DefMI, |
| 998 | unsigned Reg, MachineRegisterInfo *MRI) const { |
| 999 | // For some instructions, it is legal to fold ZERO into the RA register field. |
| 1000 | // A zero immediate should always be loaded with a single li. |
| 1001 | unsigned DefOpc = DefMI->getOpcode(); |
| 1002 | if (DefOpc != PPC::LI && DefOpc != PPC::LI8) |
| 1003 | return false; |
| 1004 | if (!DefMI->getOperand(1).isImm()) |
| 1005 | return false; |
| 1006 | if (DefMI->getOperand(1).getImm() != 0) |
| 1007 | return false; |
| 1008 | |
| 1009 | // Note that we cannot here invert the arguments of an isel in order to fold |
| 1010 | // a ZERO into what is presented as the second argument. All we have here |
| 1011 | // is the condition bit, and that might come from a CR-logical bit operation. |
| 1012 | |
| 1013 | const MCInstrDesc &UseMCID = UseMI->getDesc(); |
| 1014 | |
| 1015 | // Only fold into real machine instructions. |
| 1016 | if (UseMCID.isPseudo()) |
| 1017 | return false; |
| 1018 | |
| 1019 | unsigned UseIdx; |
| 1020 | for (UseIdx = 0; UseIdx < UseMI->getNumOperands(); ++UseIdx) |
| 1021 | if (UseMI->getOperand(UseIdx).isReg() && |
| 1022 | UseMI->getOperand(UseIdx).getReg() == Reg) |
| 1023 | break; |
| 1024 | |
| 1025 | assert(UseIdx < UseMI->getNumOperands() && "Cannot find Reg in UseMI"); |
| 1026 | assert(UseIdx < UseMCID.getNumOperands() && "No operand description for Reg"); |
| 1027 | |
| 1028 | const MCOperandInfo *UseInfo = &UseMCID.OpInfo[UseIdx]; |
| 1029 | |
| 1030 | // We can fold the zero if this register requires a GPRC_NOR0/G8RC_NOX0 |
| 1031 | // register (which might also be specified as a pointer class kind). |
| 1032 | if (UseInfo->isLookupPtrRegClass()) { |
| 1033 | if (UseInfo->RegClass /* Kind */ != 1) |
| 1034 | return false; |
| 1035 | } else { |
| 1036 | if (UseInfo->RegClass != PPC::GPRC_NOR0RegClassID && |
| 1037 | UseInfo->RegClass != PPC::G8RC_NOX0RegClassID) |
| 1038 | return false; |
| 1039 | } |
| 1040 | |
| 1041 | // Make sure this is not tied to an output register (or otherwise |
| 1042 | // constrained). This is true for ST?UX registers, for example, which |
| 1043 | // are tied to their output registers. |
| 1044 | if (UseInfo->Constraints != 0) |
| 1045 | return false; |
| 1046 | |
| 1047 | unsigned ZeroReg; |
| 1048 | if (UseInfo->isLookupPtrRegClass()) { |
Eric Christopher | 1dcea73 | 2014-06-12 21:48:52 +0000 | [diff] [blame] | 1049 | bool isPPC64 = Subtarget.isPPC64(); |
Hal Finkel | d61d4f8 | 2013-04-06 19:30:30 +0000 | [diff] [blame] | 1050 | ZeroReg = isPPC64 ? PPC::ZERO8 : PPC::ZERO; |
| 1051 | } else { |
| 1052 | ZeroReg = UseInfo->RegClass == PPC::G8RC_NOX0RegClassID ? |
| 1053 | PPC::ZERO8 : PPC::ZERO; |
| 1054 | } |
| 1055 | |
| 1056 | bool DeleteDef = MRI->hasOneNonDBGUse(Reg); |
| 1057 | UseMI->getOperand(UseIdx).setReg(ZeroReg); |
| 1058 | |
| 1059 | if (DeleteDef) |
| 1060 | DefMI->eraseFromParent(); |
| 1061 | |
| 1062 | return true; |
| 1063 | } |
| 1064 | |
Hal Finkel | 30ae229 | 2013-04-10 18:30:16 +0000 | [diff] [blame] | 1065 | static bool MBBDefinesCTR(MachineBasicBlock &MBB) { |
| 1066 | for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end(); |
| 1067 | I != IE; ++I) |
| 1068 | if (I->definesRegister(PPC::CTR) || I->definesRegister(PPC::CTR8)) |
| 1069 | return true; |
| 1070 | return false; |
| 1071 | } |
| 1072 | |
| 1073 | // We should make sure that, if we're going to predicate both sides of a |
| 1074 | // condition (a diamond), that both sides don't define the counter register. We |
| 1075 | // can predicate counter-decrement-based branches, but while that predicates |
| 1076 | // the branching, it does not predicate the counter decrement. If we tried to |
| 1077 | // merge the triangle into one predicated block, we'd decrement the counter |
| 1078 | // twice. |
| 1079 | bool PPCInstrInfo::isProfitableToIfCvt(MachineBasicBlock &TMBB, |
| 1080 | unsigned NumT, unsigned ExtraT, |
| 1081 | MachineBasicBlock &FMBB, |
| 1082 | unsigned NumF, unsigned ExtraF, |
| 1083 | const BranchProbability &Probability) const { |
| 1084 | return !(MBBDefinesCTR(TMBB) && MBBDefinesCTR(FMBB)); |
| 1085 | } |
| 1086 | |
| 1087 | |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1088 | bool PPCInstrInfo::isPredicated(const MachineInstr *MI) const { |
Hal Finkel | f29285a | 2013-04-11 01:23:34 +0000 | [diff] [blame] | 1089 | // The predicated branches are identified by their type, not really by the |
| 1090 | // explicit presence of a predicate. Furthermore, some of them can be |
| 1091 | // predicated more than once. Because if conversion won't try to predicate |
| 1092 | // any instruction which already claims to be predicated (by returning true |
| 1093 | // here), always return false. In doing so, we let isPredicable() be the |
| 1094 | // final word on whether not the instruction can be (further) predicated. |
| 1095 | |
| 1096 | return false; |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1097 | } |
| 1098 | |
| 1099 | bool PPCInstrInfo::isUnpredicatedTerminator(const MachineInstr *MI) const { |
| 1100 | if (!MI->isTerminator()) |
| 1101 | return false; |
| 1102 | |
| 1103 | // Conditional branch is a special case. |
| 1104 | if (MI->isBranch() && !MI->isBarrier()) |
| 1105 | return true; |
| 1106 | |
| 1107 | return !isPredicated(MI); |
| 1108 | } |
| 1109 | |
| 1110 | bool PPCInstrInfo::PredicateInstruction( |
| 1111 | MachineInstr *MI, |
| 1112 | const SmallVectorImpl<MachineOperand> &Pred) const { |
| 1113 | unsigned OpC = MI->getOpcode(); |
Hal Finkel | f4a22c0 | 2015-01-13 17:47:54 +0000 | [diff] [blame] | 1114 | if (OpC == PPC::BLR || OpC == PPC::BLR8) { |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1115 | if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) { |
Eric Christopher | 1dcea73 | 2014-06-12 21:48:52 +0000 | [diff] [blame] | 1116 | bool isPPC64 = Subtarget.isPPC64(); |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1117 | MI->setDesc(get(Pred[0].getImm() ? |
| 1118 | (isPPC64 ? PPC::BDNZLR8 : PPC::BDNZLR) : |
| 1119 | (isPPC64 ? PPC::BDZLR8 : PPC::BDZLR))); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 1120 | } else if (Pred[0].getImm() == PPC::PRED_BIT_SET) { |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1121 | MI->setDesc(get(PPC::BCLR)); |
| 1122 | MachineInstrBuilder(*MI->getParent()->getParent(), MI) |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 1123 | .addReg(Pred[1].getReg()); |
| 1124 | } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) { |
| 1125 | MI->setDesc(get(PPC::BCLRn)); |
| 1126 | MachineInstrBuilder(*MI->getParent()->getParent(), MI) |
| 1127 | .addReg(Pred[1].getReg()); |
| 1128 | } else { |
| 1129 | MI->setDesc(get(PPC::BCCLR)); |
| 1130 | MachineInstrBuilder(*MI->getParent()->getParent(), MI) |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1131 | .addImm(Pred[0].getImm()) |
| 1132 | .addReg(Pred[1].getReg()); |
| 1133 | } |
| 1134 | |
| 1135 | return true; |
| 1136 | } else if (OpC == PPC::B) { |
| 1137 | if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) { |
Eric Christopher | 1dcea73 | 2014-06-12 21:48:52 +0000 | [diff] [blame] | 1138 | bool isPPC64 = Subtarget.isPPC64(); |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1139 | MI->setDesc(get(Pred[0].getImm() ? |
| 1140 | (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) : |
| 1141 | (isPPC64 ? PPC::BDZ8 : PPC::BDZ))); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 1142 | } else if (Pred[0].getImm() == PPC::PRED_BIT_SET) { |
| 1143 | MachineBasicBlock *MBB = MI->getOperand(0).getMBB(); |
| 1144 | MI->RemoveOperand(0); |
| 1145 | |
| 1146 | MI->setDesc(get(PPC::BC)); |
| 1147 | MachineInstrBuilder(*MI->getParent()->getParent(), MI) |
| 1148 | .addReg(Pred[1].getReg()) |
| 1149 | .addMBB(MBB); |
| 1150 | } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) { |
| 1151 | MachineBasicBlock *MBB = MI->getOperand(0).getMBB(); |
| 1152 | MI->RemoveOperand(0); |
| 1153 | |
| 1154 | MI->setDesc(get(PPC::BCn)); |
| 1155 | MachineInstrBuilder(*MI->getParent()->getParent(), MI) |
| 1156 | .addReg(Pred[1].getReg()) |
| 1157 | .addMBB(MBB); |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1158 | } else { |
| 1159 | MachineBasicBlock *MBB = MI->getOperand(0).getMBB(); |
| 1160 | MI->RemoveOperand(0); |
| 1161 | |
| 1162 | MI->setDesc(get(PPC::BCC)); |
| 1163 | MachineInstrBuilder(*MI->getParent()->getParent(), MI) |
| 1164 | .addImm(Pred[0].getImm()) |
| 1165 | .addReg(Pred[1].getReg()) |
| 1166 | .addMBB(MBB); |
| 1167 | } |
| 1168 | |
| 1169 | return true; |
Hal Finkel | 500b004 | 2013-04-10 06:42:34 +0000 | [diff] [blame] | 1170 | } else if (OpC == PPC::BCTR || OpC == PPC::BCTR8 || |
| 1171 | OpC == PPC::BCTRL || OpC == PPC::BCTRL8) { |
| 1172 | if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) |
| 1173 | llvm_unreachable("Cannot predicate bctr[l] on the ctr register"); |
| 1174 | |
| 1175 | bool setLR = OpC == PPC::BCTRL || OpC == PPC::BCTRL8; |
Eric Christopher | 1dcea73 | 2014-06-12 21:48:52 +0000 | [diff] [blame] | 1176 | bool isPPC64 = Subtarget.isPPC64(); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 1177 | |
| 1178 | if (Pred[0].getImm() == PPC::PRED_BIT_SET) { |
| 1179 | MI->setDesc(get(isPPC64 ? (setLR ? PPC::BCCTRL8 : PPC::BCCTR8) : |
| 1180 | (setLR ? PPC::BCCTRL : PPC::BCCTR))); |
| 1181 | MachineInstrBuilder(*MI->getParent()->getParent(), MI) |
| 1182 | .addReg(Pred[1].getReg()); |
| 1183 | return true; |
| 1184 | } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) { |
| 1185 | MI->setDesc(get(isPPC64 ? (setLR ? PPC::BCCTRL8n : PPC::BCCTR8n) : |
| 1186 | (setLR ? PPC::BCCTRLn : PPC::BCCTRn))); |
| 1187 | MachineInstrBuilder(*MI->getParent()->getParent(), MI) |
| 1188 | .addReg(Pred[1].getReg()); |
| 1189 | return true; |
| 1190 | } |
| 1191 | |
| 1192 | MI->setDesc(get(isPPC64 ? (setLR ? PPC::BCCCTRL8 : PPC::BCCCTR8) : |
| 1193 | (setLR ? PPC::BCCCTRL : PPC::BCCCTR))); |
Hal Finkel | 500b004 | 2013-04-10 06:42:34 +0000 | [diff] [blame] | 1194 | MachineInstrBuilder(*MI->getParent()->getParent(), MI) |
| 1195 | .addImm(Pred[0].getImm()) |
| 1196 | .addReg(Pred[1].getReg()); |
| 1197 | return true; |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1198 | } |
| 1199 | |
| 1200 | return false; |
| 1201 | } |
| 1202 | |
| 1203 | bool PPCInstrInfo::SubsumesPredicate( |
| 1204 | const SmallVectorImpl<MachineOperand> &Pred1, |
| 1205 | const SmallVectorImpl<MachineOperand> &Pred2) const { |
| 1206 | assert(Pred1.size() == 2 && "Invalid PPC first predicate"); |
| 1207 | assert(Pred2.size() == 2 && "Invalid PPC second predicate"); |
| 1208 | |
| 1209 | if (Pred1[1].getReg() == PPC::CTR8 || Pred1[1].getReg() == PPC::CTR) |
| 1210 | return false; |
| 1211 | if (Pred2[1].getReg() == PPC::CTR8 || Pred2[1].getReg() == PPC::CTR) |
| 1212 | return false; |
| 1213 | |
Hal Finkel | 94a6f38 | 2013-12-11 23:12:25 +0000 | [diff] [blame] | 1214 | // P1 can only subsume P2 if they test the same condition register. |
| 1215 | if (Pred1[1].getReg() != Pred2[1].getReg()) |
| 1216 | return false; |
| 1217 | |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1218 | PPC::Predicate P1 = (PPC::Predicate) Pred1[0].getImm(); |
| 1219 | PPC::Predicate P2 = (PPC::Predicate) Pred2[0].getImm(); |
| 1220 | |
| 1221 | if (P1 == P2) |
| 1222 | return true; |
| 1223 | |
| 1224 | // Does P1 subsume P2, e.g. GE subsumes GT. |
| 1225 | if (P1 == PPC::PRED_LE && |
| 1226 | (P2 == PPC::PRED_LT || P2 == PPC::PRED_EQ)) |
| 1227 | return true; |
| 1228 | if (P1 == PPC::PRED_GE && |
| 1229 | (P2 == PPC::PRED_GT || P2 == PPC::PRED_EQ)) |
| 1230 | return true; |
| 1231 | |
| 1232 | return false; |
| 1233 | } |
| 1234 | |
| 1235 | bool PPCInstrInfo::DefinesPredicate(MachineInstr *MI, |
| 1236 | std::vector<MachineOperand> &Pred) const { |
| 1237 | // Note: At the present time, the contents of Pred from this function is |
| 1238 | // unused by IfConversion. This implementation follows ARM by pushing the |
| 1239 | // CR-defining operand. Because the 'DZ' and 'DNZ' count as types of |
| 1240 | // predicate, instructions defining CTR or CTR8 are also included as |
| 1241 | // predicate-defining instructions. |
| 1242 | |
| 1243 | const TargetRegisterClass *RCs[] = |
| 1244 | { &PPC::CRRCRegClass, &PPC::CRBITRCRegClass, |
| 1245 | &PPC::CTRRCRegClass, &PPC::CTRRC8RegClass }; |
| 1246 | |
| 1247 | bool Found = false; |
| 1248 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 1249 | const MachineOperand &MO = MI->getOperand(i); |
Hal Finkel | af82201 | 2013-04-10 07:17:47 +0000 | [diff] [blame] | 1250 | for (unsigned c = 0; c < array_lengthof(RCs) && !Found; ++c) { |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1251 | const TargetRegisterClass *RC = RCs[c]; |
Hal Finkel | af82201 | 2013-04-10 07:17:47 +0000 | [diff] [blame] | 1252 | if (MO.isReg()) { |
| 1253 | if (MO.isDef() && RC->contains(MO.getReg())) { |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1254 | Pred.push_back(MO); |
| 1255 | Found = true; |
| 1256 | } |
Hal Finkel | af82201 | 2013-04-10 07:17:47 +0000 | [diff] [blame] | 1257 | } else if (MO.isRegMask()) { |
| 1258 | for (TargetRegisterClass::iterator I = RC->begin(), |
| 1259 | IE = RC->end(); I != IE; ++I) |
| 1260 | if (MO.clobbersPhysReg(*I)) { |
| 1261 | Pred.push_back(MO); |
| 1262 | Found = true; |
| 1263 | } |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1264 | } |
| 1265 | } |
| 1266 | } |
| 1267 | |
| 1268 | return Found; |
| 1269 | } |
| 1270 | |
| 1271 | bool PPCInstrInfo::isPredicable(MachineInstr *MI) const { |
| 1272 | unsigned OpC = MI->getOpcode(); |
| 1273 | switch (OpC) { |
| 1274 | default: |
| 1275 | return false; |
| 1276 | case PPC::B: |
| 1277 | case PPC::BLR: |
Hal Finkel | f4a22c0 | 2015-01-13 17:47:54 +0000 | [diff] [blame] | 1278 | case PPC::BLR8: |
Hal Finkel | 500b004 | 2013-04-10 06:42:34 +0000 | [diff] [blame] | 1279 | case PPC::BCTR: |
| 1280 | case PPC::BCTR8: |
| 1281 | case PPC::BCTRL: |
| 1282 | case PPC::BCTRL8: |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1283 | return true; |
| 1284 | } |
| 1285 | } |
| 1286 | |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1287 | bool PPCInstrInfo::analyzeCompare(const MachineInstr *MI, |
| 1288 | unsigned &SrcReg, unsigned &SrcReg2, |
| 1289 | int &Mask, int &Value) const { |
| 1290 | unsigned Opc = MI->getOpcode(); |
| 1291 | |
| 1292 | switch (Opc) { |
| 1293 | default: return false; |
| 1294 | case PPC::CMPWI: |
| 1295 | case PPC::CMPLWI: |
| 1296 | case PPC::CMPDI: |
| 1297 | case PPC::CMPLDI: |
| 1298 | SrcReg = MI->getOperand(1).getReg(); |
| 1299 | SrcReg2 = 0; |
| 1300 | Value = MI->getOperand(2).getImm(); |
| 1301 | Mask = 0xFFFF; |
| 1302 | return true; |
| 1303 | case PPC::CMPW: |
| 1304 | case PPC::CMPLW: |
| 1305 | case PPC::CMPD: |
| 1306 | case PPC::CMPLD: |
| 1307 | case PPC::FCMPUS: |
| 1308 | case PPC::FCMPUD: |
| 1309 | SrcReg = MI->getOperand(1).getReg(); |
| 1310 | SrcReg2 = MI->getOperand(2).getReg(); |
| 1311 | return true; |
| 1312 | } |
| 1313 | } |
Hal Finkel | e632239 | 2013-04-19 22:08:38 +0000 | [diff] [blame] | 1314 | |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1315 | bool PPCInstrInfo::optimizeCompareInstr(MachineInstr *CmpInstr, |
| 1316 | unsigned SrcReg, unsigned SrcReg2, |
| 1317 | int Mask, int Value, |
| 1318 | const MachineRegisterInfo *MRI) const { |
Hal Finkel | b12da6b | 2013-04-18 22:54:25 +0000 | [diff] [blame] | 1319 | if (DisableCmpOpt) |
| 1320 | return false; |
| 1321 | |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1322 | int OpC = CmpInstr->getOpcode(); |
| 1323 | unsigned CRReg = CmpInstr->getOperand(0).getReg(); |
Hal Finkel | 08e53ee | 2013-05-08 12:16:14 +0000 | [diff] [blame] | 1324 | |
| 1325 | // FP record forms set CR1 based on the execption status bits, not a |
| 1326 | // comparison with zero. |
| 1327 | if (OpC == PPC::FCMPUS || OpC == PPC::FCMPUD) |
| 1328 | return false; |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1329 | |
| 1330 | // The record forms set the condition register based on a signed comparison |
| 1331 | // with zero (so says the ISA manual). This is not as straightforward as it |
| 1332 | // seems, however, because this is always a 64-bit comparison on PPC64, even |
| 1333 | // for instructions that are 32-bit in nature (like slw for example). |
| 1334 | // So, on PPC32, for unsigned comparisons, we can use the record forms only |
| 1335 | // for equality checks (as those don't depend on the sign). On PPC64, |
| 1336 | // we are restricted to equality for unsigned 64-bit comparisons and for |
| 1337 | // signed 32-bit comparisons the applicability is more restricted. |
Eric Christopher | 1dcea73 | 2014-06-12 21:48:52 +0000 | [diff] [blame] | 1338 | bool isPPC64 = Subtarget.isPPC64(); |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1339 | bool is32BitSignedCompare = OpC == PPC::CMPWI || OpC == PPC::CMPW; |
| 1340 | bool is32BitUnsignedCompare = OpC == PPC::CMPLWI || OpC == PPC::CMPLW; |
| 1341 | bool is64BitUnsignedCompare = OpC == PPC::CMPLDI || OpC == PPC::CMPLD; |
| 1342 | |
| 1343 | // Get the unique definition of SrcReg. |
| 1344 | MachineInstr *MI = MRI->getUniqueVRegDef(SrcReg); |
| 1345 | if (!MI) return false; |
| 1346 | int MIOpC = MI->getOpcode(); |
| 1347 | |
| 1348 | bool equalityOnly = false; |
| 1349 | bool noSub = false; |
| 1350 | if (isPPC64) { |
| 1351 | if (is32BitSignedCompare) { |
| 1352 | // We can perform this optimization only if MI is sign-extending. |
| 1353 | if (MIOpC == PPC::SRAW || MIOpC == PPC::SRAWo || |
| 1354 | MIOpC == PPC::SRAWI || MIOpC == PPC::SRAWIo || |
| 1355 | MIOpC == PPC::EXTSB || MIOpC == PPC::EXTSBo || |
| 1356 | MIOpC == PPC::EXTSH || MIOpC == PPC::EXTSHo || |
| 1357 | MIOpC == PPC::EXTSW || MIOpC == PPC::EXTSWo) { |
| 1358 | noSub = true; |
| 1359 | } else |
| 1360 | return false; |
| 1361 | } else if (is32BitUnsignedCompare) { |
| 1362 | // We can perform this optimization, equality only, if MI is |
| 1363 | // zero-extending. |
| 1364 | if (MIOpC == PPC::CNTLZW || MIOpC == PPC::CNTLZWo || |
| 1365 | MIOpC == PPC::SLW || MIOpC == PPC::SLWo || |
| 1366 | MIOpC == PPC::SRW || MIOpC == PPC::SRWo) { |
| 1367 | noSub = true; |
| 1368 | equalityOnly = true; |
| 1369 | } else |
| 1370 | return false; |
Hal Finkel | 08e53ee | 2013-05-08 12:16:14 +0000 | [diff] [blame] | 1371 | } else |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1372 | equalityOnly = is64BitUnsignedCompare; |
Hal Finkel | 08e53ee | 2013-05-08 12:16:14 +0000 | [diff] [blame] | 1373 | } else |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1374 | equalityOnly = is32BitUnsignedCompare; |
| 1375 | |
| 1376 | if (equalityOnly) { |
| 1377 | // We need to check the uses of the condition register in order to reject |
| 1378 | // non-equality comparisons. |
Owen Anderson | 16c6bf4 | 2014-03-13 23:12:04 +0000 | [diff] [blame] | 1379 | for (MachineRegisterInfo::use_instr_iterator I =MRI->use_instr_begin(CRReg), |
| 1380 | IE = MRI->use_instr_end(); I != IE; ++I) { |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1381 | MachineInstr *UseMI = &*I; |
| 1382 | if (UseMI->getOpcode() == PPC::BCC) { |
| 1383 | unsigned Pred = UseMI->getOperand(0).getImm(); |
Hal Finkel | c363245 | 2013-05-07 17:49:55 +0000 | [diff] [blame] | 1384 | if (Pred != PPC::PRED_EQ && Pred != PPC::PRED_NE) |
| 1385 | return false; |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1386 | } else if (UseMI->getOpcode() == PPC::ISEL || |
| 1387 | UseMI->getOpcode() == PPC::ISEL8) { |
| 1388 | unsigned SubIdx = UseMI->getOperand(3).getSubReg(); |
Hal Finkel | c363245 | 2013-05-07 17:49:55 +0000 | [diff] [blame] | 1389 | if (SubIdx != PPC::sub_eq) |
| 1390 | return false; |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1391 | } else |
| 1392 | return false; |
| 1393 | } |
| 1394 | } |
| 1395 | |
Hal Finkel | c363245 | 2013-05-07 17:49:55 +0000 | [diff] [blame] | 1396 | MachineBasicBlock::iterator I = CmpInstr; |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1397 | |
| 1398 | // Scan forward to find the first use of the compare. |
| 1399 | for (MachineBasicBlock::iterator EL = CmpInstr->getParent()->end(); |
| 1400 | I != EL; ++I) { |
| 1401 | bool FoundUse = false; |
Owen Anderson | 16c6bf4 | 2014-03-13 23:12:04 +0000 | [diff] [blame] | 1402 | for (MachineRegisterInfo::use_instr_iterator J =MRI->use_instr_begin(CRReg), |
| 1403 | JE = MRI->use_instr_end(); J != JE; ++J) |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1404 | if (&*J == &*I) { |
| 1405 | FoundUse = true; |
| 1406 | break; |
| 1407 | } |
| 1408 | |
| 1409 | if (FoundUse) |
| 1410 | break; |
| 1411 | } |
| 1412 | |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1413 | // There are two possible candidates which can be changed to set CR[01]. |
| 1414 | // One is MI, the other is a SUB instruction. |
| 1415 | // For CMPrr(r1,r2), we are looking for SUB(r1,r2) or SUB(r2,r1). |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 1416 | MachineInstr *Sub = nullptr; |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1417 | if (SrcReg2 != 0) |
| 1418 | // MI is not a candidate for CMPrr. |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 1419 | MI = nullptr; |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1420 | // FIXME: Conservatively refuse to convert an instruction which isn't in the |
| 1421 | // same BB as the comparison. This is to allow the check below to avoid calls |
| 1422 | // (and other explicit clobbers); instead we should really check for these |
| 1423 | // more explicitly (in at least a few predecessors). |
| 1424 | else if (MI->getParent() != CmpInstr->getParent() || Value != 0) { |
| 1425 | // PPC does not have a record-form SUBri. |
| 1426 | return false; |
| 1427 | } |
| 1428 | |
| 1429 | // Search for Sub. |
| 1430 | const TargetRegisterInfo *TRI = &getRegisterInfo(); |
| 1431 | --I; |
Hal Finkel | c363245 | 2013-05-07 17:49:55 +0000 | [diff] [blame] | 1432 | |
| 1433 | // Get ready to iterate backward from CmpInstr. |
| 1434 | MachineBasicBlock::iterator E = MI, |
| 1435 | B = CmpInstr->getParent()->begin(); |
| 1436 | |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1437 | for (; I != E && !noSub; --I) { |
| 1438 | const MachineInstr &Instr = *I; |
| 1439 | unsigned IOpC = Instr.getOpcode(); |
| 1440 | |
| 1441 | if (&*I != CmpInstr && ( |
Hal Finkel | 08e53ee | 2013-05-08 12:16:14 +0000 | [diff] [blame] | 1442 | Instr.modifiesRegister(PPC::CR0, TRI) || |
| 1443 | Instr.readsRegister(PPC::CR0, TRI))) |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1444 | // This instruction modifies or uses the record condition register after |
| 1445 | // the one we want to change. While we could do this transformation, it |
| 1446 | // would likely not be profitable. This transformation removes one |
| 1447 | // instruction, and so even forcing RA to generate one move probably |
| 1448 | // makes it unprofitable. |
| 1449 | return false; |
| 1450 | |
| 1451 | // Check whether CmpInstr can be made redundant by the current instruction. |
| 1452 | if ((OpC == PPC::CMPW || OpC == PPC::CMPLW || |
| 1453 | OpC == PPC::CMPD || OpC == PPC::CMPLD) && |
| 1454 | (IOpC == PPC::SUBF || IOpC == PPC::SUBF8) && |
| 1455 | ((Instr.getOperand(1).getReg() == SrcReg && |
| 1456 | Instr.getOperand(2).getReg() == SrcReg2) || |
| 1457 | (Instr.getOperand(1).getReg() == SrcReg2 && |
| 1458 | Instr.getOperand(2).getReg() == SrcReg))) { |
| 1459 | Sub = &*I; |
| 1460 | break; |
| 1461 | } |
| 1462 | |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1463 | if (I == B) |
| 1464 | // The 'and' is below the comparison instruction. |
| 1465 | return false; |
| 1466 | } |
| 1467 | |
| 1468 | // Return false if no candidates exist. |
| 1469 | if (!MI && !Sub) |
| 1470 | return false; |
| 1471 | |
| 1472 | // The single candidate is called MI. |
| 1473 | if (!MI) MI = Sub; |
| 1474 | |
| 1475 | int NewOpC = -1; |
| 1476 | MIOpC = MI->getOpcode(); |
| 1477 | if (MIOpC == PPC::ANDIo || MIOpC == PPC::ANDIo8) |
| 1478 | NewOpC = MIOpC; |
| 1479 | else { |
| 1480 | NewOpC = PPC::getRecordFormOpcode(MIOpC); |
| 1481 | if (NewOpC == -1 && PPC::getNonRecordFormOpcode(MIOpC) != -1) |
| 1482 | NewOpC = MIOpC; |
| 1483 | } |
| 1484 | |
| 1485 | // FIXME: On the non-embedded POWER architectures, only some of the record |
| 1486 | // forms are fast, and we should use only the fast ones. |
| 1487 | |
| 1488 | // The defining instruction has a record form (or is already a record |
| 1489 | // form). It is possible, however, that we'll need to reverse the condition |
| 1490 | // code of the users. |
| 1491 | if (NewOpC == -1) |
| 1492 | return false; |
| 1493 | |
Hal Finkel | e632239 | 2013-04-19 22:08:38 +0000 | [diff] [blame] | 1494 | SmallVector<std::pair<MachineOperand*, PPC::Predicate>, 4> PredsToUpdate; |
| 1495 | SmallVector<std::pair<MachineOperand*, unsigned>, 4> SubRegsToUpdate; |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1496 | |
| 1497 | // If we have SUB(r1, r2) and CMP(r2, r1), the condition code based on CMP |
| 1498 | // needs to be updated to be based on SUB. Push the condition code |
| 1499 | // operands to OperandsToUpdate. If it is safe to remove CmpInstr, the |
| 1500 | // condition code of these operands will be modified. |
| 1501 | bool ShouldSwap = false; |
| 1502 | if (Sub) { |
| 1503 | ShouldSwap = SrcReg2 != 0 && Sub->getOperand(1).getReg() == SrcReg2 && |
| 1504 | Sub->getOperand(2).getReg() == SrcReg; |
| 1505 | |
| 1506 | // The operands to subf are the opposite of sub, so only in the fixed-point |
| 1507 | // case, invert the order. |
Hal Finkel | 08e53ee | 2013-05-08 12:16:14 +0000 | [diff] [blame] | 1508 | ShouldSwap = !ShouldSwap; |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1509 | } |
| 1510 | |
| 1511 | if (ShouldSwap) |
Owen Anderson | 16c6bf4 | 2014-03-13 23:12:04 +0000 | [diff] [blame] | 1512 | for (MachineRegisterInfo::use_instr_iterator |
| 1513 | I = MRI->use_instr_begin(CRReg), IE = MRI->use_instr_end(); |
| 1514 | I != IE; ++I) { |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1515 | MachineInstr *UseMI = &*I; |
| 1516 | if (UseMI->getOpcode() == PPC::BCC) { |
| 1517 | PPC::Predicate Pred = (PPC::Predicate) UseMI->getOperand(0).getImm(); |
Hal Finkel | e632239 | 2013-04-19 22:08:38 +0000 | [diff] [blame] | 1518 | assert((!equalityOnly || |
| 1519 | Pred == PPC::PRED_EQ || Pred == PPC::PRED_NE) && |
| 1520 | "Invalid predicate for equality-only optimization"); |
Owen Anderson | 16c6bf4 | 2014-03-13 23:12:04 +0000 | [diff] [blame] | 1521 | PredsToUpdate.push_back(std::make_pair(&(UseMI->getOperand(0)), |
Hal Finkel | 0f64e21 | 2013-04-20 05:16:26 +0000 | [diff] [blame] | 1522 | PPC::getSwappedPredicate(Pred))); |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1523 | } else if (UseMI->getOpcode() == PPC::ISEL || |
| 1524 | UseMI->getOpcode() == PPC::ISEL8) { |
Hal Finkel | e632239 | 2013-04-19 22:08:38 +0000 | [diff] [blame] | 1525 | unsigned NewSubReg = UseMI->getOperand(3).getSubReg(); |
| 1526 | assert((!equalityOnly || NewSubReg == PPC::sub_eq) && |
| 1527 | "Invalid CR bit for equality-only optimization"); |
| 1528 | |
| 1529 | if (NewSubReg == PPC::sub_lt) |
| 1530 | NewSubReg = PPC::sub_gt; |
| 1531 | else if (NewSubReg == PPC::sub_gt) |
| 1532 | NewSubReg = PPC::sub_lt; |
| 1533 | |
Owen Anderson | 16c6bf4 | 2014-03-13 23:12:04 +0000 | [diff] [blame] | 1534 | SubRegsToUpdate.push_back(std::make_pair(&(UseMI->getOperand(3)), |
Hal Finkel | e632239 | 2013-04-19 22:08:38 +0000 | [diff] [blame] | 1535 | NewSubReg)); |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1536 | } else // We need to abort on a user we don't understand. |
| 1537 | return false; |
| 1538 | } |
| 1539 | |
| 1540 | // Create a new virtual register to hold the value of the CR set by the |
| 1541 | // record-form instruction. If the instruction was not previously in |
| 1542 | // record form, then set the kill flag on the CR. |
| 1543 | CmpInstr->eraseFromParent(); |
| 1544 | |
| 1545 | MachineBasicBlock::iterator MII = MI; |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 1546 | BuildMI(*MI->getParent(), std::next(MII), MI->getDebugLoc(), |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1547 | get(TargetOpcode::COPY), CRReg) |
Hal Finkel | 08e53ee | 2013-05-08 12:16:14 +0000 | [diff] [blame] | 1548 | .addReg(PPC::CR0, MIOpC != NewOpC ? RegState::Kill : 0); |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1549 | |
| 1550 | if (MIOpC != NewOpC) { |
| 1551 | // We need to be careful here: we're replacing one instruction with |
| 1552 | // another, and we need to make sure that we get all of the right |
| 1553 | // implicit uses and defs. On the other hand, the caller may be holding |
| 1554 | // an iterator to this instruction, and so we can't delete it (this is |
| 1555 | // specifically the case if this is the instruction directly after the |
| 1556 | // compare). |
| 1557 | |
| 1558 | const MCInstrDesc &NewDesc = get(NewOpC); |
| 1559 | MI->setDesc(NewDesc); |
| 1560 | |
| 1561 | if (NewDesc.ImplicitDefs) |
| 1562 | for (const uint16_t *ImpDefs = NewDesc.getImplicitDefs(); |
| 1563 | *ImpDefs; ++ImpDefs) |
| 1564 | if (!MI->definesRegister(*ImpDefs)) |
| 1565 | MI->addOperand(*MI->getParent()->getParent(), |
| 1566 | MachineOperand::CreateReg(*ImpDefs, true, true)); |
| 1567 | if (NewDesc.ImplicitUses) |
| 1568 | for (const uint16_t *ImpUses = NewDesc.getImplicitUses(); |
| 1569 | *ImpUses; ++ImpUses) |
| 1570 | if (!MI->readsRegister(*ImpUses)) |
| 1571 | MI->addOperand(*MI->getParent()->getParent(), |
| 1572 | MachineOperand::CreateReg(*ImpUses, false, true)); |
| 1573 | } |
| 1574 | |
| 1575 | // Modify the condition code of operands in OperandsToUpdate. |
| 1576 | // Since we have SUB(r1, r2) and CMP(r2, r1), the condition code needs to |
| 1577 | // be changed from r2 > r1 to r1 < r2, from r2 < r1 to r1 > r2, etc. |
Hal Finkel | e632239 | 2013-04-19 22:08:38 +0000 | [diff] [blame] | 1578 | for (unsigned i = 0, e = PredsToUpdate.size(); i < e; i++) |
| 1579 | PredsToUpdate[i].first->setImm(PredsToUpdate[i].second); |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1580 | |
Hal Finkel | e632239 | 2013-04-19 22:08:38 +0000 | [diff] [blame] | 1581 | for (unsigned i = 0, e = SubRegsToUpdate.size(); i < e; i++) |
| 1582 | SubRegsToUpdate[i].first->setSubReg(SubRegsToUpdate[i].second); |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1583 | |
| 1584 | return true; |
| 1585 | } |
| 1586 | |
Nicolas Geoffray | ae84bbd | 2008-04-16 20:10:13 +0000 | [diff] [blame] | 1587 | /// GetInstSize - Return the number of bytes of code the specified |
| 1588 | /// instruction may be. This returns the maximum number of bytes. |
| 1589 | /// |
| 1590 | unsigned PPCInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const { |
Hal Finkel | a7bbaf6 | 2014-02-02 06:12:27 +0000 | [diff] [blame] | 1591 | unsigned Opcode = MI->getOpcode(); |
| 1592 | |
| 1593 | if (Opcode == PPC::INLINEASM) { |
Nicolas Geoffray | ae84bbd | 2008-04-16 20:10:13 +0000 | [diff] [blame] | 1594 | const MachineFunction *MF = MI->getParent()->getParent(); |
| 1595 | const char *AsmStr = MI->getOperand(0).getSymbolName(); |
Chris Lattner | 7b26fce | 2009-08-22 20:48:53 +0000 | [diff] [blame] | 1596 | return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo()); |
Hal Finkel | 934361a | 2015-01-14 01:07:51 +0000 | [diff] [blame] | 1597 | } else if (Opcode == TargetOpcode::STACKMAP) { |
| 1598 | return MI->getOperand(1).getImm(); |
| 1599 | } else if (Opcode == TargetOpcode::PATCHPOINT) { |
| 1600 | PatchPointOpers Opers(MI); |
| 1601 | return Opers.getMetaOper(PatchPointOpers::NBytesPos).getImm(); |
Hal Finkel | a7bbaf6 | 2014-02-02 06:12:27 +0000 | [diff] [blame] | 1602 | } else { |
| 1603 | const MCInstrDesc &Desc = get(Opcode); |
| 1604 | return Desc.getSize(); |
Nicolas Geoffray | ae84bbd | 2008-04-16 20:10:13 +0000 | [diff] [blame] | 1605 | } |
| 1606 | } |
Hal Finkel | b5aa7e5 | 2013-04-08 16:24:03 +0000 | [diff] [blame] | 1607 | |