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