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" |
Hal Finkel | 174e590 | 2014-03-25 23:29:21 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/SlotIndexes.h" |
Evan Cheng | c5e6d2f | 2011-07-11 03:57:24 +0000 | [diff] [blame] | 31 | #include "llvm/MC/MCAsmInfo.h" |
Bill Wendling | 1af20ad | 2008-03-04 23:13:51 +0000 | [diff] [blame] | 32 | #include "llvm/Support/CommandLine.h" |
Hal Finkel | 174e590 | 2014-03-25 23:29:21 +0000 | [diff] [blame] | 33 | #include "llvm/Support/Debug.h" |
Torok Edwin | fb8d6d5 | 2009-07-08 20:53:28 +0000 | [diff] [blame] | 34 | #include "llvm/Support/ErrorHandling.h" |
Evan Cheng | 2bb4035 | 2011-08-24 18:08:43 +0000 | [diff] [blame] | 35 | #include "llvm/Support/TargetRegistry.h" |
Torok Edwin | fb8d6d5 | 2009-07-08 20:53:28 +0000 | [diff] [blame] | 36 | #include "llvm/Support/raw_ostream.h" |
Misha Brukman | 116f927 | 2004-08-17 04:55:41 +0000 | [diff] [blame] | 37 | |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 38 | #define GET_INSTRMAP_INFO |
Juergen Ributzka | d12ccbd | 2013-11-19 00:57:56 +0000 | [diff] [blame] | 39 | #define GET_INSTRINFO_CTOR_DTOR |
Evan Cheng | 1e210d0 | 2011-06-28 20:07:07 +0000 | [diff] [blame] | 40 | #include "PPCGenInstrInfo.inc" |
| 41 | |
Dan Gohman | 2085719 | 2010-04-15 17:20:57 +0000 | [diff] [blame] | 42 | using namespace llvm; |
Bill Wendling | 1af20ad | 2008-03-04 23:13:51 +0000 | [diff] [blame] | 43 | |
Chandler Carruth | e96dd89 | 2014-04-21 22:55:11 +0000 | [diff] [blame^] | 44 | #define DEBUG_TYPE "ppc-instr-info" |
| 45 | |
Hal Finkel | 821e001 | 2012-06-08 15:38:25 +0000 | [diff] [blame] | 46 | static cl:: |
Hal Finkel | c6b5deb | 2012-06-08 19:19:53 +0000 | [diff] [blame] | 47 | opt<bool> DisableCTRLoopAnal("disable-ppc-ctrloop-analysis", cl::Hidden, |
| 48 | cl::desc("Disable analysis for CTR loops")); |
Hal Finkel | 821e001 | 2012-06-08 15:38:25 +0000 | [diff] [blame] | 49 | |
Hal Finkel | e632239 | 2013-04-19 22:08:38 +0000 | [diff] [blame] | 50 | static cl::opt<bool> DisableCmpOpt("disable-ppc-cmp-opt", |
Hal Finkel | b12da6b | 2013-04-18 22:54:25 +0000 | [diff] [blame] | 51 | cl::desc("Disable compare instruction optimization"), cl::Hidden); |
| 52 | |
Hal Finkel | 174e590 | 2014-03-25 23:29:21 +0000 | [diff] [blame] | 53 | static cl::opt<bool> DisableVSXFMAMutate("disable-ppc-vsx-fma-mutation", |
| 54 | cl::desc("Disable VSX FMA instruction mutation"), cl::Hidden); |
| 55 | |
Hal Finkel | 9dcb358 | 2014-03-27 22:46:28 +0000 | [diff] [blame] | 56 | static cl::opt<bool> VSXSelfCopyCrash("crash-on-ppc-vsx-self-copy", |
| 57 | cl::desc("Causes the backend to crash instead of generating a nop VSX copy"), |
| 58 | cl::Hidden); |
| 59 | |
Juergen Ributzka | d12ccbd | 2013-11-19 00:57:56 +0000 | [diff] [blame] | 60 | // Pin the vtable to this file. |
| 61 | void PPCInstrInfo::anchor() {} |
| 62 | |
Chris Lattner | 49cadab | 2006-06-17 00:01:04 +0000 | [diff] [blame] | 63 | PPCInstrInfo::PPCInstrInfo(PPCTargetMachine &tm) |
Evan Cheng | 703a0fb | 2011-07-01 17:57:27 +0000 | [diff] [blame] | 64 | : PPCGenInstrInfo(PPC::ADJCALLSTACKDOWN, PPC::ADJCALLSTACKUP), |
Bill Wendling | 5e7656b | 2013-06-07 07:55:53 +0000 | [diff] [blame] | 65 | TM(tm), RI(*TM.getSubtargetImpl()) {} |
Chris Lattner | 49cadab | 2006-06-17 00:01:04 +0000 | [diff] [blame] | 66 | |
Andrew Trick | 10ffc2b | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 67 | /// CreateTargetHazardRecognizer - Return the hazard recognizer to use for |
| 68 | /// this target when scheduling the DAG. |
| 69 | ScheduleHazardRecognizer *PPCInstrInfo::CreateTargetHazardRecognizer( |
| 70 | const TargetMachine *TM, |
| 71 | const ScheduleDAG *DAG) const { |
Hal Finkel | 6fa5697 | 2011-10-17 04:03:49 +0000 | [diff] [blame] | 72 | unsigned Directive = TM->getSubtarget<PPCSubtarget>().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) { |
Hal Finkel | 6f0ae78 | 2011-11-22 16:21:04 +0000 | [diff] [blame] | 75 | const InstrItineraryData *II = TM->getInstrItineraryData(); |
Hal Finkel | 563cc05 | 2013-12-02 23:52:46 +0000 | [diff] [blame] | 76 | return new ScoreboardHazardRecognizer(II, DAG); |
Hal Finkel | 6fa5697 | 2011-10-17 04:03:49 +0000 | [diff] [blame] | 77 | } |
Hal Finkel | 58ca360 | 2011-12-02 04:58:02 +0000 | [diff] [blame] | 78 | |
Jakob Stoklund Olesen | 9de596e | 2012-11-28 02:35:17 +0000 | [diff] [blame] | 79 | return TargetInstrInfo::CreateTargetHazardRecognizer(TM, DAG); |
Andrew Trick | 10ffc2b | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Hal Finkel | 58ca360 | 2011-12-02 04:58:02 +0000 | [diff] [blame] | 82 | /// CreateTargetPostRAHazardRecognizer - Return the postRA hazard recognizer |
| 83 | /// to use for this target when scheduling the DAG. |
| 84 | ScheduleHazardRecognizer *PPCInstrInfo::CreateTargetPostRAHazardRecognizer( |
| 85 | const InstrItineraryData *II, |
| 86 | const ScheduleDAG *DAG) const { |
| 87 | unsigned Directive = TM.getSubtarget<PPCSubtarget>().getDarwinDirective(); |
| 88 | |
Hal Finkel | ceb1f12 | 2013-12-12 00:19:11 +0000 | [diff] [blame] | 89 | if (Directive == PPC::DIR_PWR7) |
| 90 | return new PPCDispatchGroupSBHazardRecognizer(II, DAG); |
| 91 | |
Hal Finkel | 58ca360 | 2011-12-02 04:58:02 +0000 | [diff] [blame] | 92 | // Most subtargets use a PPC970 recognizer. |
Hal Finkel | 742b535 | 2012-08-28 16:12:39 +0000 | [diff] [blame] | 93 | if (Directive != PPC::DIR_440 && Directive != PPC::DIR_A2 && |
| 94 | Directive != PPC::DIR_E500mc && Directive != PPC::DIR_E5500) { |
Benjamin Kramer | f0ec199 | 2013-06-07 11:23:35 +0000 | [diff] [blame] | 95 | assert(TM.getInstrInfo() && "No InstrInfo?"); |
Hal Finkel | 58ca360 | 2011-12-02 04:58:02 +0000 | [diff] [blame] | 96 | |
Bill Wendling | 5e7656b | 2013-06-07 07:55:53 +0000 | [diff] [blame] | 97 | return new PPCHazardRecognizer970(TM); |
Hal Finkel | 58ca360 | 2011-12-02 04:58:02 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Hal Finkel | 563cc05 | 2013-12-02 23:52:46 +0000 | [diff] [blame] | 100 | return new ScoreboardHazardRecognizer(II, DAG); |
Hal Finkel | 58ca360 | 2011-12-02 04:58:02 +0000 | [diff] [blame] | 101 | } |
Jakob Stoklund Olesen | 0f855e4 | 2012-06-19 21:14:34 +0000 | [diff] [blame] | 102 | |
Hal Finkel | ceb1f12 | 2013-12-12 00:19:11 +0000 | [diff] [blame] | 103 | |
| 104 | int PPCInstrInfo::getOperandLatency(const InstrItineraryData *ItinData, |
| 105 | const MachineInstr *DefMI, unsigned DefIdx, |
| 106 | const MachineInstr *UseMI, |
| 107 | unsigned UseIdx) const { |
| 108 | int Latency = PPCGenInstrInfo::getOperandLatency(ItinData, DefMI, DefIdx, |
| 109 | UseMI, UseIdx); |
| 110 | |
| 111 | const MachineOperand &DefMO = DefMI->getOperand(DefIdx); |
| 112 | unsigned Reg = DefMO.getReg(); |
| 113 | |
| 114 | const TargetRegisterInfo *TRI = &getRegisterInfo(); |
| 115 | bool IsRegCR; |
| 116 | if (TRI->isVirtualRegister(Reg)) { |
| 117 | const MachineRegisterInfo *MRI = |
| 118 | &DefMI->getParent()->getParent()->getRegInfo(); |
| 119 | IsRegCR = MRI->getRegClass(Reg)->hasSuperClassEq(&PPC::CRRCRegClass) || |
| 120 | MRI->getRegClass(Reg)->hasSuperClassEq(&PPC::CRBITRCRegClass); |
| 121 | } else { |
| 122 | IsRegCR = PPC::CRRCRegClass.contains(Reg) || |
| 123 | PPC::CRBITRCRegClass.contains(Reg); |
| 124 | } |
| 125 | |
| 126 | if (UseMI->isBranch() && IsRegCR) { |
| 127 | if (Latency < 0) |
| 128 | Latency = getInstrLatency(ItinData, DefMI); |
| 129 | |
| 130 | // On some cores, there is an additional delay between writing to a condition |
| 131 | // register, and using it from a branch. |
| 132 | unsigned Directive = TM.getSubtarget<PPCSubtarget>().getDarwinDirective(); |
| 133 | switch (Directive) { |
| 134 | default: break; |
| 135 | case PPC::DIR_7400: |
| 136 | case PPC::DIR_750: |
| 137 | case PPC::DIR_970: |
| 138 | case PPC::DIR_E5500: |
| 139 | case PPC::DIR_PWR4: |
| 140 | case PPC::DIR_PWR5: |
| 141 | case PPC::DIR_PWR5X: |
| 142 | case PPC::DIR_PWR6: |
| 143 | case PPC::DIR_PWR6X: |
| 144 | case PPC::DIR_PWR7: |
| 145 | Latency += 2; |
| 146 | break; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | return Latency; |
| 151 | } |
| 152 | |
Jakob Stoklund Olesen | 0f855e4 | 2012-06-19 21:14:34 +0000 | [diff] [blame] | 153 | // Detect 32 -> 64-bit extensions where we may reuse the low sub-register. |
| 154 | bool PPCInstrInfo::isCoalescableExtInstr(const MachineInstr &MI, |
| 155 | unsigned &SrcReg, unsigned &DstReg, |
| 156 | unsigned &SubIdx) const { |
| 157 | switch (MI.getOpcode()) { |
| 158 | default: return false; |
| 159 | case PPC::EXTSW: |
| 160 | case PPC::EXTSW_32_64: |
| 161 | SrcReg = MI.getOperand(1).getReg(); |
| 162 | DstReg = MI.getOperand(0).getReg(); |
| 163 | SubIdx = PPC::sub_32; |
| 164 | return true; |
| 165 | } |
| 166 | } |
| 167 | |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 168 | unsigned PPCInstrInfo::isLoadFromStackSlot(const MachineInstr *MI, |
Chris Lattner | 91400bd | 2006-03-16 22:24:02 +0000 | [diff] [blame] | 169 | int &FrameIndex) const { |
Hal Finkel | 37714b8 | 2013-03-27 21:21:15 +0000 | [diff] [blame] | 170 | // Note: This list must be kept consistent with LoadRegFromStackSlot. |
Chris Lattner | bb53acd | 2006-02-02 20:12:32 +0000 | [diff] [blame] | 171 | switch (MI->getOpcode()) { |
| 172 | default: break; |
| 173 | case PPC::LD: |
| 174 | case PPC::LWZ: |
| 175 | case PPC::LFS: |
| 176 | case PPC::LFD: |
Hal Finkel | 37714b8 | 2013-03-27 21:21:15 +0000 | [diff] [blame] | 177 | case PPC::RESTORE_CR: |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 178 | case PPC::RESTORE_CRBIT: |
Hal Finkel | 37714b8 | 2013-03-27 21:21:15 +0000 | [diff] [blame] | 179 | case PPC::LVX: |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 180 | case PPC::LXVD2X: |
Hal Finkel | 37714b8 | 2013-03-27 21:21:15 +0000 | [diff] [blame] | 181 | case PPC::RESTORE_VRSAVE: |
| 182 | // Check for the operands added by addFrameReference (the immediate is the |
| 183 | // offset which defaults to 0). |
Dan Gohman | 0d1e9a8 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 184 | if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() && |
| 185 | MI->getOperand(2).isFI()) { |
Chris Lattner | a5bb370 | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 186 | FrameIndex = MI->getOperand(2).getIndex(); |
Chris Lattner | bb53acd | 2006-02-02 20:12:32 +0000 | [diff] [blame] | 187 | return MI->getOperand(0).getReg(); |
| 188 | } |
| 189 | break; |
| 190 | } |
| 191 | return 0; |
Chris Lattner | c327d71 | 2006-02-02 20:16:12 +0000 | [diff] [blame] | 192 | } |
Chris Lattner | bb53acd | 2006-02-02 20:12:32 +0000 | [diff] [blame] | 193 | |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 194 | unsigned PPCInstrInfo::isStoreToStackSlot(const MachineInstr *MI, |
Chris Lattner | c327d71 | 2006-02-02 20:16:12 +0000 | [diff] [blame] | 195 | int &FrameIndex) const { |
Hal Finkel | 37714b8 | 2013-03-27 21:21:15 +0000 | [diff] [blame] | 196 | // Note: This list must be kept consistent with StoreRegToStackSlot. |
Chris Lattner | c327d71 | 2006-02-02 20:16:12 +0000 | [diff] [blame] | 197 | switch (MI->getOpcode()) { |
| 198 | default: break; |
Nate Begeman | 4efb328 | 2006-02-02 21:07:50 +0000 | [diff] [blame] | 199 | case PPC::STD: |
Chris Lattner | c327d71 | 2006-02-02 20:16:12 +0000 | [diff] [blame] | 200 | case PPC::STW: |
| 201 | case PPC::STFS: |
| 202 | case PPC::STFD: |
Hal Finkel | 37714b8 | 2013-03-27 21:21:15 +0000 | [diff] [blame] | 203 | case PPC::SPILL_CR: |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 204 | case PPC::SPILL_CRBIT: |
Hal Finkel | 37714b8 | 2013-03-27 21:21:15 +0000 | [diff] [blame] | 205 | case PPC::STVX: |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 206 | case PPC::STXVD2X: |
Hal Finkel | 37714b8 | 2013-03-27 21:21:15 +0000 | [diff] [blame] | 207 | case PPC::SPILL_VRSAVE: |
| 208 | // Check for the operands added by addFrameReference (the immediate is the |
| 209 | // offset which defaults to 0). |
Dan Gohman | 0d1e9a8 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 210 | if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() && |
| 211 | MI->getOperand(2).isFI()) { |
Chris Lattner | a5bb370 | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 212 | FrameIndex = MI->getOperand(2).getIndex(); |
Chris Lattner | c327d71 | 2006-02-02 20:16:12 +0000 | [diff] [blame] | 213 | return MI->getOperand(0).getReg(); |
| 214 | } |
| 215 | break; |
| 216 | } |
| 217 | return 0; |
| 218 | } |
Chris Lattner | bb53acd | 2006-02-02 20:12:32 +0000 | [diff] [blame] | 219 | |
Chris Lattner | c37a2f1 | 2005-09-09 18:17:41 +0000 | [diff] [blame] | 220 | // commuteInstruction - We can commute rlwimi instructions, but only if the |
| 221 | // 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] | 222 | MachineInstr * |
| 223 | PPCInstrInfo::commuteInstruction(MachineInstr *MI, bool NewMI) const { |
Dan Gohman | 3b46030 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 224 | MachineFunction &MF = *MI->getParent()->getParent(); |
| 225 | |
Chris Lattner | c37a2f1 | 2005-09-09 18:17:41 +0000 | [diff] [blame] | 226 | // Normal instructions can be commuted the obvious way. |
Hal Finkel | 654d43b | 2013-04-12 02:18:09 +0000 | [diff] [blame] | 227 | if (MI->getOpcode() != PPC::RLWIMI && |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 228 | MI->getOpcode() != PPC::RLWIMIo && |
| 229 | MI->getOpcode() != PPC::RLWIMI8 && |
| 230 | MI->getOpcode() != PPC::RLWIMI8o) |
Jakob Stoklund Olesen | 9de596e | 2012-11-28 02:35:17 +0000 | [diff] [blame] | 231 | return TargetInstrInfo::commuteInstruction(MI, NewMI); |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 232 | |
Chris Lattner | c37a2f1 | 2005-09-09 18:17:41 +0000 | [diff] [blame] | 233 | // Cannot commute if it has a non-zero rotate count. |
Chris Lattner | 5c46378 | 2007-12-30 20:49:49 +0000 | [diff] [blame] | 234 | if (MI->getOperand(3).getImm() != 0) |
Chris Lattner | c37a2f1 | 2005-09-09 18:17:41 +0000 | [diff] [blame] | 235 | return 0; |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 236 | |
Chris Lattner | c37a2f1 | 2005-09-09 18:17:41 +0000 | [diff] [blame] | 237 | // If we have a zero rotate count, we have: |
| 238 | // M = mask(MB,ME) |
| 239 | // Op0 = (Op1 & ~M) | (Op2 & M) |
| 240 | // Change this to: |
| 241 | // M = mask((ME+1)&31, (MB-1)&31) |
| 242 | // Op0 = (Op2 & ~M) | (Op1 & M) |
| 243 | |
| 244 | // Swap op1/op2 |
Evan Cheng | 244183e | 2008-02-13 02:46:49 +0000 | [diff] [blame] | 245 | unsigned Reg0 = MI->getOperand(0).getReg(); |
Chris Lattner | c37a2f1 | 2005-09-09 18:17:41 +0000 | [diff] [blame] | 246 | unsigned Reg1 = MI->getOperand(1).getReg(); |
| 247 | unsigned Reg2 = MI->getOperand(2).getReg(); |
Andrew Trick | e339828 | 2013-12-17 04:50:45 +0000 | [diff] [blame] | 248 | unsigned SubReg1 = MI->getOperand(1).getSubReg(); |
| 249 | unsigned SubReg2 = MI->getOperand(2).getSubReg(); |
Evan Cheng | dc2c874 | 2006-11-15 20:58:11 +0000 | [diff] [blame] | 250 | bool Reg1IsKill = MI->getOperand(1).isKill(); |
| 251 | bool Reg2IsKill = MI->getOperand(2).isKill(); |
Evan Cheng | 03553bb | 2008-06-16 07:33:11 +0000 | [diff] [blame] | 252 | bool ChangeReg0 = false; |
Evan Cheng | 244183e | 2008-02-13 02:46:49 +0000 | [diff] [blame] | 253 | // If machine instrs are no longer in two-address forms, update |
| 254 | // destination register as well. |
| 255 | if (Reg0 == Reg1) { |
| 256 | // Must be two address instruction! |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 257 | assert(MI->getDesc().getOperandConstraint(0, MCOI::TIED_TO) && |
Evan Cheng | 244183e | 2008-02-13 02:46:49 +0000 | [diff] [blame] | 258 | "Expecting a two-address instruction!"); |
Andrew Trick | e339828 | 2013-12-17 04:50:45 +0000 | [diff] [blame] | 259 | assert(MI->getOperand(0).getSubReg() == SubReg1 && "Tied subreg mismatch"); |
Evan Cheng | 244183e | 2008-02-13 02:46:49 +0000 | [diff] [blame] | 260 | Reg2IsKill = false; |
Evan Cheng | 03553bb | 2008-06-16 07:33:11 +0000 | [diff] [blame] | 261 | ChangeReg0 = true; |
Evan Cheng | 244183e | 2008-02-13 02:46:49 +0000 | [diff] [blame] | 262 | } |
Evan Cheng | 03553bb | 2008-06-16 07:33:11 +0000 | [diff] [blame] | 263 | |
| 264 | // Masks. |
| 265 | unsigned MB = MI->getOperand(4).getImm(); |
| 266 | unsigned ME = MI->getOperand(5).getImm(); |
| 267 | |
| 268 | if (NewMI) { |
| 269 | // Create a new instruction. |
| 270 | unsigned Reg0 = ChangeReg0 ? Reg2 : MI->getOperand(0).getReg(); |
| 271 | bool Reg0IsDead = MI->getOperand(0).isDead(); |
Bill Wendling | f6d609a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 272 | return BuildMI(MF, MI->getDebugLoc(), MI->getDesc()) |
Bill Wendling | f7b83c7 | 2009-05-13 21:33:08 +0000 | [diff] [blame] | 273 | .addReg(Reg0, RegState::Define | getDeadRegState(Reg0IsDead)) |
| 274 | .addReg(Reg2, getKillRegState(Reg2IsKill)) |
| 275 | .addReg(Reg1, getKillRegState(Reg1IsKill)) |
Evan Cheng | 03553bb | 2008-06-16 07:33:11 +0000 | [diff] [blame] | 276 | .addImm((ME+1) & 31) |
| 277 | .addImm((MB-1) & 31); |
| 278 | } |
| 279 | |
Andrew Trick | e339828 | 2013-12-17 04:50:45 +0000 | [diff] [blame] | 280 | if (ChangeReg0) { |
Evan Cheng | 03553bb | 2008-06-16 07:33:11 +0000 | [diff] [blame] | 281 | MI->getOperand(0).setReg(Reg2); |
Andrew Trick | e339828 | 2013-12-17 04:50:45 +0000 | [diff] [blame] | 282 | MI->getOperand(0).setSubReg(SubReg2); |
| 283 | } |
Chris Lattner | 10d6341 | 2006-05-04 17:52:23 +0000 | [diff] [blame] | 284 | MI->getOperand(2).setReg(Reg1); |
| 285 | MI->getOperand(1).setReg(Reg2); |
Andrew Trick | e339828 | 2013-12-17 04:50:45 +0000 | [diff] [blame] | 286 | MI->getOperand(2).setSubReg(SubReg1); |
| 287 | MI->getOperand(1).setSubReg(SubReg2); |
Chris Lattner | 6005589 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 288 | MI->getOperand(2).setIsKill(Reg1IsKill); |
| 289 | MI->getOperand(1).setIsKill(Reg2IsKill); |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 290 | |
Chris Lattner | c37a2f1 | 2005-09-09 18:17:41 +0000 | [diff] [blame] | 291 | // Swap the mask around. |
Chris Lattner | 5c46378 | 2007-12-30 20:49:49 +0000 | [diff] [blame] | 292 | MI->getOperand(4).setImm((ME+1) & 31); |
| 293 | MI->getOperand(5).setImm((MB-1) & 31); |
Chris Lattner | c37a2f1 | 2005-09-09 18:17:41 +0000 | [diff] [blame] | 294 | return MI; |
| 295 | } |
Chris Lattner | ea79d9fd73 | 2006-03-05 23:49:55 +0000 | [diff] [blame] | 296 | |
Hal Finkel | 6c32ff3 | 2014-03-25 19:26:43 +0000 | [diff] [blame] | 297 | bool PPCInstrInfo::findCommutedOpIndices(MachineInstr *MI, unsigned &SrcOpIdx1, |
| 298 | unsigned &SrcOpIdx2) const { |
| 299 | // For VSX A-Type FMA instructions, it is the first two operands that can be |
| 300 | // commuted, however, because the non-encoded tied input operand is listed |
| 301 | // first, the operands to swap are actually the second and third. |
| 302 | |
| 303 | int AltOpc = PPC::getAltVSXFMAOpcode(MI->getOpcode()); |
| 304 | if (AltOpc == -1) |
| 305 | return TargetInstrInfo::findCommutedOpIndices(MI, SrcOpIdx1, SrcOpIdx2); |
| 306 | |
| 307 | SrcOpIdx1 = 2; |
| 308 | SrcOpIdx2 = 3; |
| 309 | return true; |
| 310 | } |
| 311 | |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 312 | void PPCInstrInfo::insertNoop(MachineBasicBlock &MBB, |
Chris Lattner | ea79d9fd73 | 2006-03-05 23:49:55 +0000 | [diff] [blame] | 313 | MachineBasicBlock::iterator MI) const { |
Hal Finkel | ceb1f12 | 2013-12-12 00:19:11 +0000 | [diff] [blame] | 314 | // This function is used for scheduling, and the nop wanted here is the type |
| 315 | // that terminates dispatch groups on the POWER cores. |
| 316 | unsigned Directive = TM.getSubtarget<PPCSubtarget>().getDarwinDirective(); |
| 317 | unsigned Opcode; |
| 318 | switch (Directive) { |
| 319 | default: Opcode = PPC::NOP; break; |
| 320 | case PPC::DIR_PWR6: Opcode = PPC::NOP_GT_PWR6; break; |
| 321 | case PPC::DIR_PWR7: Opcode = PPC::NOP_GT_PWR7; break; |
| 322 | } |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 323 | |
Hal Finkel | ceb1f12 | 2013-12-12 00:19:11 +0000 | [diff] [blame] | 324 | DebugLoc DL; |
| 325 | BuildMI(MBB, MI, DL, get(Opcode)); |
| 326 | } |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 327 | |
| 328 | // Branch analysis. |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 329 | // Note: If the condition register is set to CTR or CTR8 then this is a |
| 330 | // BDNZ (imm == 1) or BDZ (imm == 0) branch. |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 331 | bool PPCInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB, |
| 332 | MachineBasicBlock *&FBB, |
Evan Cheng | 64dfcac | 2009-02-09 07:14:22 +0000 | [diff] [blame] | 333 | SmallVectorImpl<MachineOperand> &Cond, |
| 334 | bool AllowModify) const { |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 335 | bool isPPC64 = TM.getSubtargetImpl()->isPPC64(); |
| 336 | |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 337 | // If the block has no terminators, it just falls into the block after it. |
| 338 | MachineBasicBlock::iterator I = MBB.end(); |
Dale Johannesen | 4244d12 | 2010-04-02 01:38:09 +0000 | [diff] [blame] | 339 | if (I == MBB.begin()) |
| 340 | return false; |
| 341 | --I; |
| 342 | while (I->isDebugValue()) { |
| 343 | if (I == MBB.begin()) |
| 344 | return false; |
| 345 | --I; |
| 346 | } |
| 347 | if (!isUnpredicatedTerminator(I)) |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 348 | return false; |
| 349 | |
| 350 | // Get the last instruction in the block. |
| 351 | MachineInstr *LastInst = I; |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 352 | |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 353 | // If there is only one terminator instruction, process it. |
Evan Cheng | 5514bbe | 2007-06-08 21:59:56 +0000 | [diff] [blame] | 354 | if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) { |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 355 | if (LastInst->getOpcode() == PPC::B) { |
Evan Cheng | 8f43afd | 2009-05-08 23:09:25 +0000 | [diff] [blame] | 356 | if (!LastInst->getOperand(0).isMBB()) |
| 357 | return true; |
Chris Lattner | a5bb370 | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 358 | TBB = LastInst->getOperand(0).getMBB(); |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 359 | return false; |
Chris Lattner | e026379 | 2006-11-17 22:14:47 +0000 | [diff] [blame] | 360 | } else if (LastInst->getOpcode() == PPC::BCC) { |
Evan Cheng | 8f43afd | 2009-05-08 23:09:25 +0000 | [diff] [blame] | 361 | if (!LastInst->getOperand(2).isMBB()) |
| 362 | return true; |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 363 | // Block ends with fall-through condbranch. |
Chris Lattner | a5bb370 | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 364 | TBB = LastInst->getOperand(2).getMBB(); |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 365 | Cond.push_back(LastInst->getOperand(0)); |
| 366 | Cond.push_back(LastInst->getOperand(1)); |
Chris Lattner | 23f22de | 2006-10-21 06:03:11 +0000 | [diff] [blame] | 367 | return false; |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 368 | } else if (LastInst->getOpcode() == PPC::BC) { |
| 369 | if (!LastInst->getOperand(1).isMBB()) |
| 370 | return true; |
| 371 | // Block ends with fall-through condbranch. |
| 372 | TBB = LastInst->getOperand(1).getMBB(); |
| 373 | Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET)); |
| 374 | Cond.push_back(LastInst->getOperand(0)); |
| 375 | return false; |
| 376 | } else if (LastInst->getOpcode() == PPC::BCn) { |
| 377 | if (!LastInst->getOperand(1).isMBB()) |
| 378 | return true; |
| 379 | // Block ends with fall-through condbranch. |
| 380 | TBB = LastInst->getOperand(1).getMBB(); |
| 381 | Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_UNSET)); |
| 382 | Cond.push_back(LastInst->getOperand(0)); |
| 383 | return false; |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 384 | } else if (LastInst->getOpcode() == PPC::BDNZ8 || |
| 385 | LastInst->getOpcode() == PPC::BDNZ) { |
| 386 | if (!LastInst->getOperand(0).isMBB()) |
| 387 | return true; |
Hal Finkel | c6b5deb | 2012-06-08 19:19:53 +0000 | [diff] [blame] | 388 | if (DisableCTRLoopAnal) |
Hal Finkel | 821e001 | 2012-06-08 15:38:25 +0000 | [diff] [blame] | 389 | return true; |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 390 | TBB = LastInst->getOperand(0).getMBB(); |
| 391 | Cond.push_back(MachineOperand::CreateImm(1)); |
| 392 | Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR, |
| 393 | true)); |
| 394 | return false; |
| 395 | } else if (LastInst->getOpcode() == PPC::BDZ8 || |
| 396 | LastInst->getOpcode() == PPC::BDZ) { |
| 397 | if (!LastInst->getOperand(0).isMBB()) |
| 398 | return true; |
Hal Finkel | c6b5deb | 2012-06-08 19:19:53 +0000 | [diff] [blame] | 399 | if (DisableCTRLoopAnal) |
Hal Finkel | 821e001 | 2012-06-08 15:38:25 +0000 | [diff] [blame] | 400 | return true; |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 401 | TBB = LastInst->getOperand(0).getMBB(); |
| 402 | Cond.push_back(MachineOperand::CreateImm(0)); |
| 403 | Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR, |
| 404 | true)); |
| 405 | return false; |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 406 | } |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 407 | |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 408 | // Otherwise, don't know what this is. |
| 409 | return true; |
| 410 | } |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 411 | |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 412 | // Get the instruction before it if it's a terminator. |
| 413 | MachineInstr *SecondLastInst = I; |
| 414 | |
| 415 | // If there are three terminators, we don't know what sort of block this is. |
| 416 | if (SecondLastInst && I != MBB.begin() && |
Evan Cheng | 5514bbe | 2007-06-08 21:59:56 +0000 | [diff] [blame] | 417 | isUnpredicatedTerminator(--I)) |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 418 | return true; |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 419 | |
Chris Lattner | e026379 | 2006-11-17 22:14:47 +0000 | [diff] [blame] | 420 | // If the block ends with PPC::B and PPC:BCC, handle it. |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 421 | if (SecondLastInst->getOpcode() == PPC::BCC && |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 422 | LastInst->getOpcode() == PPC::B) { |
Evan Cheng | 8f43afd | 2009-05-08 23:09:25 +0000 | [diff] [blame] | 423 | if (!SecondLastInst->getOperand(2).isMBB() || |
| 424 | !LastInst->getOperand(0).isMBB()) |
| 425 | return true; |
Chris Lattner | a5bb370 | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 426 | TBB = SecondLastInst->getOperand(2).getMBB(); |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 427 | Cond.push_back(SecondLastInst->getOperand(0)); |
| 428 | Cond.push_back(SecondLastInst->getOperand(1)); |
Chris Lattner | a5bb370 | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 429 | FBB = LastInst->getOperand(0).getMBB(); |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 430 | return false; |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 431 | } else if (SecondLastInst->getOpcode() == PPC::BC && |
| 432 | LastInst->getOpcode() == PPC::B) { |
| 433 | if (!SecondLastInst->getOperand(1).isMBB() || |
| 434 | !LastInst->getOperand(0).isMBB()) |
| 435 | return true; |
| 436 | TBB = SecondLastInst->getOperand(1).getMBB(); |
| 437 | Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_SET)); |
| 438 | Cond.push_back(SecondLastInst->getOperand(0)); |
| 439 | FBB = LastInst->getOperand(0).getMBB(); |
| 440 | return false; |
| 441 | } else if (SecondLastInst->getOpcode() == PPC::BCn && |
| 442 | LastInst->getOpcode() == PPC::B) { |
| 443 | if (!SecondLastInst->getOperand(1).isMBB() || |
| 444 | !LastInst->getOperand(0).isMBB()) |
| 445 | return true; |
| 446 | TBB = SecondLastInst->getOperand(1).getMBB(); |
| 447 | Cond.push_back(MachineOperand::CreateImm(PPC::PRED_BIT_UNSET)); |
| 448 | Cond.push_back(SecondLastInst->getOperand(0)); |
| 449 | FBB = LastInst->getOperand(0).getMBB(); |
| 450 | return false; |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 451 | } else if ((SecondLastInst->getOpcode() == PPC::BDNZ8 || |
| 452 | SecondLastInst->getOpcode() == PPC::BDNZ) && |
| 453 | LastInst->getOpcode() == PPC::B) { |
| 454 | if (!SecondLastInst->getOperand(0).isMBB() || |
| 455 | !LastInst->getOperand(0).isMBB()) |
| 456 | return true; |
Hal Finkel | c6b5deb | 2012-06-08 19:19:53 +0000 | [diff] [blame] | 457 | if (DisableCTRLoopAnal) |
Hal Finkel | 821e001 | 2012-06-08 15:38:25 +0000 | [diff] [blame] | 458 | return true; |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 459 | TBB = SecondLastInst->getOperand(0).getMBB(); |
| 460 | Cond.push_back(MachineOperand::CreateImm(1)); |
| 461 | Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR, |
| 462 | true)); |
| 463 | FBB = LastInst->getOperand(0).getMBB(); |
| 464 | return false; |
| 465 | } else if ((SecondLastInst->getOpcode() == PPC::BDZ8 || |
| 466 | SecondLastInst->getOpcode() == PPC::BDZ) && |
| 467 | LastInst->getOpcode() == PPC::B) { |
| 468 | if (!SecondLastInst->getOperand(0).isMBB() || |
| 469 | !LastInst->getOperand(0).isMBB()) |
| 470 | return true; |
Hal Finkel | c6b5deb | 2012-06-08 19:19:53 +0000 | [diff] [blame] | 471 | if (DisableCTRLoopAnal) |
Hal Finkel | 821e001 | 2012-06-08 15:38:25 +0000 | [diff] [blame] | 472 | return true; |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 473 | TBB = SecondLastInst->getOperand(0).getMBB(); |
| 474 | Cond.push_back(MachineOperand::CreateImm(0)); |
| 475 | Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR, |
| 476 | true)); |
| 477 | FBB = LastInst->getOperand(0).getMBB(); |
| 478 | return false; |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 479 | } |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 480 | |
Dale Johannesen | c685546 | 2007-06-13 17:59:52 +0000 | [diff] [blame] | 481 | // If the block ends with two PPC:Bs, handle it. The second one is not |
| 482 | // executed, so remove it. |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 483 | if (SecondLastInst->getOpcode() == PPC::B && |
Dale Johannesen | c685546 | 2007-06-13 17:59:52 +0000 | [diff] [blame] | 484 | LastInst->getOpcode() == PPC::B) { |
Evan Cheng | 8f43afd | 2009-05-08 23:09:25 +0000 | [diff] [blame] | 485 | if (!SecondLastInst->getOperand(0).isMBB()) |
| 486 | return true; |
Chris Lattner | a5bb370 | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 487 | TBB = SecondLastInst->getOperand(0).getMBB(); |
Dale Johannesen | c685546 | 2007-06-13 17:59:52 +0000 | [diff] [blame] | 488 | I = LastInst; |
Evan Cheng | 64dfcac | 2009-02-09 07:14:22 +0000 | [diff] [blame] | 489 | if (AllowModify) |
| 490 | I->eraseFromParent(); |
Dale Johannesen | c685546 | 2007-06-13 17:59:52 +0000 | [diff] [blame] | 491 | return false; |
| 492 | } |
| 493 | |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 494 | // Otherwise, can't handle this. |
| 495 | return true; |
| 496 | } |
| 497 | |
Evan Cheng | 99be49d | 2007-05-18 00:05:48 +0000 | [diff] [blame] | 498 | unsigned PPCInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const { |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 499 | MachineBasicBlock::iterator I = MBB.end(); |
Evan Cheng | 99be49d | 2007-05-18 00:05:48 +0000 | [diff] [blame] | 500 | if (I == MBB.begin()) return 0; |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 501 | --I; |
Dale Johannesen | 4244d12 | 2010-04-02 01:38:09 +0000 | [diff] [blame] | 502 | while (I->isDebugValue()) { |
| 503 | if (I == MBB.begin()) |
| 504 | return 0; |
| 505 | --I; |
| 506 | } |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 507 | if (I->getOpcode() != PPC::B && I->getOpcode() != PPC::BCC && |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 508 | I->getOpcode() != PPC::BC && I->getOpcode() != PPC::BCn && |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 509 | I->getOpcode() != PPC::BDNZ8 && I->getOpcode() != PPC::BDNZ && |
| 510 | I->getOpcode() != PPC::BDZ8 && I->getOpcode() != PPC::BDZ) |
Evan Cheng | 99be49d | 2007-05-18 00:05:48 +0000 | [diff] [blame] | 511 | return 0; |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 512 | |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 513 | // Remove the branch. |
| 514 | I->eraseFromParent(); |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 515 | |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 516 | I = MBB.end(); |
| 517 | |
Evan Cheng | 99be49d | 2007-05-18 00:05:48 +0000 | [diff] [blame] | 518 | if (I == MBB.begin()) return 1; |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 519 | --I; |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 520 | if (I->getOpcode() != PPC::BCC && |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 521 | I->getOpcode() != PPC::BC && I->getOpcode() != PPC::BCn && |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 522 | I->getOpcode() != PPC::BDNZ8 && I->getOpcode() != PPC::BDNZ && |
| 523 | I->getOpcode() != PPC::BDZ8 && I->getOpcode() != PPC::BDZ) |
Evan Cheng | 99be49d | 2007-05-18 00:05:48 +0000 | [diff] [blame] | 524 | return 1; |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 525 | |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 526 | // Remove the branch. |
| 527 | I->eraseFromParent(); |
Evan Cheng | 99be49d | 2007-05-18 00:05:48 +0000 | [diff] [blame] | 528 | return 2; |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 529 | } |
| 530 | |
Evan Cheng | 99be49d | 2007-05-18 00:05:48 +0000 | [diff] [blame] | 531 | unsigned |
| 532 | PPCInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB, |
| 533 | MachineBasicBlock *FBB, |
Stuart Hastings | 0125b64 | 2010-06-17 22:43:56 +0000 | [diff] [blame] | 534 | const SmallVectorImpl<MachineOperand> &Cond, |
| 535 | DebugLoc DL) const { |
Chris Lattner | a61f010 | 2006-10-17 18:06:55 +0000 | [diff] [blame] | 536 | // Shouldn't be a fall through. |
| 537 | assert(TBB && "InsertBranch must not be told to insert a fallthrough"); |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 538 | assert((Cond.size() == 2 || Cond.size() == 0) && |
Chris Lattner | 94e0444 | 2006-10-21 05:36:13 +0000 | [diff] [blame] | 539 | "PPC branch conditions have two components!"); |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 540 | |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 541 | bool isPPC64 = TM.getSubtargetImpl()->isPPC64(); |
| 542 | |
Chris Lattner | 94e0444 | 2006-10-21 05:36:13 +0000 | [diff] [blame] | 543 | // One-way branch. |
Chris Lattner | a61f010 | 2006-10-17 18:06:55 +0000 | [diff] [blame] | 544 | if (FBB == 0) { |
Chris Lattner | 94e0444 | 2006-10-21 05:36:13 +0000 | [diff] [blame] | 545 | if (Cond.empty()) // Unconditional branch |
Stuart Hastings | 0125b64 | 2010-06-17 22:43:56 +0000 | [diff] [blame] | 546 | BuildMI(&MBB, DL, get(PPC::B)).addMBB(TBB); |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 547 | else if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8) |
| 548 | BuildMI(&MBB, DL, get(Cond[0].getImm() ? |
| 549 | (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) : |
| 550 | (isPPC64 ? PPC::BDZ8 : PPC::BDZ))).addMBB(TBB); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 551 | else if (Cond[0].getImm() == PPC::PRED_BIT_SET) |
| 552 | BuildMI(&MBB, DL, get(PPC::BC)).addOperand(Cond[1]).addMBB(TBB); |
| 553 | else if (Cond[0].getImm() == PPC::PRED_BIT_UNSET) |
| 554 | BuildMI(&MBB, DL, get(PPC::BCn)).addOperand(Cond[1]).addMBB(TBB); |
Chris Lattner | 94e0444 | 2006-10-21 05:36:13 +0000 | [diff] [blame] | 555 | else // Conditional branch |
Stuart Hastings | 0125b64 | 2010-06-17 22:43:56 +0000 | [diff] [blame] | 556 | BuildMI(&MBB, DL, get(PPC::BCC)) |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 557 | .addImm(Cond[0].getImm()).addOperand(Cond[1]).addMBB(TBB); |
Evan Cheng | 99be49d | 2007-05-18 00:05:48 +0000 | [diff] [blame] | 558 | return 1; |
Chris Lattner | a61f010 | 2006-10-17 18:06:55 +0000 | [diff] [blame] | 559 | } |
Andrew Trick | c416ba6 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 560 | |
Chris Lattner | d881660 | 2006-10-21 05:42:09 +0000 | [diff] [blame] | 561 | // Two-way Conditional Branch. |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 562 | if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8) |
| 563 | BuildMI(&MBB, DL, get(Cond[0].getImm() ? |
| 564 | (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) : |
| 565 | (isPPC64 ? PPC::BDZ8 : PPC::BDZ))).addMBB(TBB); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 566 | else if (Cond[0].getImm() == PPC::PRED_BIT_SET) |
| 567 | BuildMI(&MBB, DL, get(PPC::BC)).addOperand(Cond[1]).addMBB(TBB); |
| 568 | else if (Cond[0].getImm() == PPC::PRED_BIT_UNSET) |
| 569 | BuildMI(&MBB, DL, get(PPC::BCn)).addOperand(Cond[1]).addMBB(TBB); |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 570 | else |
| 571 | BuildMI(&MBB, DL, get(PPC::BCC)) |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 572 | .addImm(Cond[0].getImm()).addOperand(Cond[1]).addMBB(TBB); |
Stuart Hastings | 0125b64 | 2010-06-17 22:43:56 +0000 | [diff] [blame] | 573 | BuildMI(&MBB, DL, get(PPC::B)).addMBB(FBB); |
Evan Cheng | 99be49d | 2007-05-18 00:05:48 +0000 | [diff] [blame] | 574 | return 2; |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 575 | } |
| 576 | |
Hal Finkel | ed6a285 | 2013-04-05 23:29:01 +0000 | [diff] [blame] | 577 | // Select analysis. |
| 578 | bool PPCInstrInfo::canInsertSelect(const MachineBasicBlock &MBB, |
| 579 | const SmallVectorImpl<MachineOperand> &Cond, |
| 580 | unsigned TrueReg, unsigned FalseReg, |
| 581 | int &CondCycles, int &TrueCycles, int &FalseCycles) const { |
| 582 | if (!TM.getSubtargetImpl()->hasISEL()) |
| 583 | return false; |
| 584 | |
| 585 | if (Cond.size() != 2) |
| 586 | return false; |
| 587 | |
| 588 | // If this is really a bdnz-like condition, then it cannot be turned into a |
| 589 | // select. |
| 590 | if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8) |
| 591 | return false; |
| 592 | |
| 593 | // Check register classes. |
| 594 | const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); |
| 595 | const TargetRegisterClass *RC = |
| 596 | RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg)); |
| 597 | if (!RC) |
| 598 | return false; |
| 599 | |
| 600 | // isel is for regular integer GPRs only. |
| 601 | if (!PPC::GPRCRegClass.hasSubClassEq(RC) && |
Hal Finkel | 8e8618a | 2013-07-15 20:22:58 +0000 | [diff] [blame] | 602 | !PPC::GPRC_NOR0RegClass.hasSubClassEq(RC) && |
| 603 | !PPC::G8RCRegClass.hasSubClassEq(RC) && |
| 604 | !PPC::G8RC_NOX0RegClass.hasSubClassEq(RC)) |
Hal Finkel | ed6a285 | 2013-04-05 23:29:01 +0000 | [diff] [blame] | 605 | return false; |
| 606 | |
| 607 | // FIXME: These numbers are for the A2, how well they work for other cores is |
| 608 | // an open question. On the A2, the isel instruction has a 2-cycle latency |
| 609 | // but single-cycle throughput. These numbers are used in combination with |
| 610 | // the MispredictPenalty setting from the active SchedMachineModel. |
| 611 | CondCycles = 1; |
| 612 | TrueCycles = 1; |
| 613 | FalseCycles = 1; |
| 614 | |
| 615 | return true; |
| 616 | } |
| 617 | |
| 618 | void PPCInstrInfo::insertSelect(MachineBasicBlock &MBB, |
| 619 | MachineBasicBlock::iterator MI, DebugLoc dl, |
| 620 | unsigned DestReg, |
| 621 | const SmallVectorImpl<MachineOperand> &Cond, |
| 622 | unsigned TrueReg, unsigned FalseReg) const { |
| 623 | assert(Cond.size() == 2 && |
| 624 | "PPC branch conditions have two components!"); |
| 625 | |
| 626 | assert(TM.getSubtargetImpl()->hasISEL() && |
| 627 | "Cannot insert select on target without ISEL support"); |
| 628 | |
| 629 | // Get the register classes. |
| 630 | MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); |
| 631 | const TargetRegisterClass *RC = |
| 632 | RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg)); |
| 633 | assert(RC && "TrueReg and FalseReg must have overlapping register classes"); |
Hal Finkel | 8e8618a | 2013-07-15 20:22:58 +0000 | [diff] [blame] | 634 | |
| 635 | bool Is64Bit = PPC::G8RCRegClass.hasSubClassEq(RC) || |
| 636 | PPC::G8RC_NOX0RegClass.hasSubClassEq(RC); |
| 637 | assert((Is64Bit || |
| 638 | PPC::GPRCRegClass.hasSubClassEq(RC) || |
| 639 | PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) && |
Hal Finkel | ed6a285 | 2013-04-05 23:29:01 +0000 | [diff] [blame] | 640 | "isel is for regular integer GPRs only"); |
| 641 | |
Hal Finkel | 8e8618a | 2013-07-15 20:22:58 +0000 | [diff] [blame] | 642 | unsigned OpCode = Is64Bit ? PPC::ISEL8 : PPC::ISEL; |
Hal Finkel | ed6a285 | 2013-04-05 23:29:01 +0000 | [diff] [blame] | 643 | unsigned SelectPred = Cond[0].getImm(); |
| 644 | |
| 645 | unsigned SubIdx; |
| 646 | bool SwapOps; |
| 647 | switch (SelectPred) { |
| 648 | default: llvm_unreachable("invalid predicate for isel"); |
| 649 | case PPC::PRED_EQ: SubIdx = PPC::sub_eq; SwapOps = false; break; |
| 650 | case PPC::PRED_NE: SubIdx = PPC::sub_eq; SwapOps = true; break; |
| 651 | case PPC::PRED_LT: SubIdx = PPC::sub_lt; SwapOps = false; break; |
| 652 | case PPC::PRED_GE: SubIdx = PPC::sub_lt; SwapOps = true; break; |
| 653 | case PPC::PRED_GT: SubIdx = PPC::sub_gt; SwapOps = false; break; |
| 654 | case PPC::PRED_LE: SubIdx = PPC::sub_gt; SwapOps = true; break; |
| 655 | case PPC::PRED_UN: SubIdx = PPC::sub_un; SwapOps = false; break; |
| 656 | case PPC::PRED_NU: SubIdx = PPC::sub_un; SwapOps = true; break; |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 657 | case PPC::PRED_BIT_SET: SubIdx = 0; SwapOps = false; break; |
| 658 | case PPC::PRED_BIT_UNSET: SubIdx = 0; SwapOps = true; break; |
Hal Finkel | ed6a285 | 2013-04-05 23:29:01 +0000 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | unsigned FirstReg = SwapOps ? FalseReg : TrueReg, |
| 662 | SecondReg = SwapOps ? TrueReg : FalseReg; |
| 663 | |
| 664 | // The first input register of isel cannot be r0. If it is a member |
| 665 | // of a register class that can be r0, then copy it first (the |
| 666 | // register allocator should eliminate the copy). |
| 667 | if (MRI.getRegClass(FirstReg)->contains(PPC::R0) || |
| 668 | MRI.getRegClass(FirstReg)->contains(PPC::X0)) { |
| 669 | const TargetRegisterClass *FirstRC = |
| 670 | MRI.getRegClass(FirstReg)->contains(PPC::X0) ? |
| 671 | &PPC::G8RC_NOX0RegClass : &PPC::GPRC_NOR0RegClass; |
| 672 | unsigned OldFirstReg = FirstReg; |
| 673 | FirstReg = MRI.createVirtualRegister(FirstRC); |
| 674 | BuildMI(MBB, MI, dl, get(TargetOpcode::COPY), FirstReg) |
| 675 | .addReg(OldFirstReg); |
| 676 | } |
| 677 | |
| 678 | BuildMI(MBB, MI, dl, get(OpCode), DestReg) |
| 679 | .addReg(FirstReg).addReg(SecondReg) |
| 680 | .addReg(Cond[1].getReg(), 0, SubIdx); |
| 681 | } |
| 682 | |
Jakob Stoklund Olesen | 0d61197 | 2010-07-11 07:31:00 +0000 | [diff] [blame] | 683 | void PPCInstrInfo::copyPhysReg(MachineBasicBlock &MBB, |
| 684 | MachineBasicBlock::iterator I, DebugLoc DL, |
| 685 | unsigned DestReg, unsigned SrcReg, |
| 686 | bool KillSrc) const { |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 687 | // 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] | 688 | // legalization. Promote them here. |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 689 | const TargetRegisterInfo *TRI = &getRegisterInfo(); |
| 690 | if (PPC::F8RCRegClass.contains(DestReg) && |
| 691 | PPC::VSLRCRegClass.contains(SrcReg)) { |
| 692 | unsigned SuperReg = |
| 693 | TRI->getMatchingSuperReg(DestReg, PPC::sub_64, &PPC::VSRCRegClass); |
| 694 | |
Hal Finkel | 9dcb358 | 2014-03-27 22:46:28 +0000 | [diff] [blame] | 695 | if (VSXSelfCopyCrash && SrcReg == SuperReg) |
| 696 | llvm_unreachable("nop VSX copy"); |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 697 | |
| 698 | DestReg = SuperReg; |
| 699 | } else if (PPC::VRRCRegClass.contains(DestReg) && |
| 700 | PPC::VSHRCRegClass.contains(SrcReg)) { |
| 701 | unsigned SuperReg = |
| 702 | TRI->getMatchingSuperReg(DestReg, PPC::sub_128, &PPC::VSRCRegClass); |
| 703 | |
Hal Finkel | 9dcb358 | 2014-03-27 22:46:28 +0000 | [diff] [blame] | 704 | if (VSXSelfCopyCrash && SrcReg == SuperReg) |
| 705 | llvm_unreachable("nop VSX copy"); |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 706 | |
| 707 | DestReg = SuperReg; |
| 708 | } else if (PPC::F8RCRegClass.contains(SrcReg) && |
| 709 | PPC::VSLRCRegClass.contains(DestReg)) { |
| 710 | unsigned SuperReg = |
| 711 | TRI->getMatchingSuperReg(SrcReg, PPC::sub_64, &PPC::VSRCRegClass); |
| 712 | |
Hal Finkel | 9dcb358 | 2014-03-27 22:46:28 +0000 | [diff] [blame] | 713 | if (VSXSelfCopyCrash && DestReg == SuperReg) |
| 714 | llvm_unreachable("nop VSX copy"); |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 715 | |
| 716 | SrcReg = SuperReg; |
| 717 | } else if (PPC::VRRCRegClass.contains(SrcReg) && |
| 718 | PPC::VSHRCRegClass.contains(DestReg)) { |
| 719 | unsigned SuperReg = |
| 720 | TRI->getMatchingSuperReg(SrcReg, PPC::sub_128, &PPC::VSRCRegClass); |
| 721 | |
Hal Finkel | 9dcb358 | 2014-03-27 22:46:28 +0000 | [diff] [blame] | 722 | if (VSXSelfCopyCrash && DestReg == SuperReg) |
| 723 | llvm_unreachable("nop VSX copy"); |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 724 | |
| 725 | SrcReg = SuperReg; |
| 726 | } |
| 727 | |
Jakob Stoklund Olesen | 0d61197 | 2010-07-11 07:31:00 +0000 | [diff] [blame] | 728 | unsigned Opc; |
| 729 | if (PPC::GPRCRegClass.contains(DestReg, SrcReg)) |
| 730 | Opc = PPC::OR; |
| 731 | else if (PPC::G8RCRegClass.contains(DestReg, SrcReg)) |
| 732 | Opc = PPC::OR8; |
| 733 | else if (PPC::F4RCRegClass.contains(DestReg, SrcReg)) |
| 734 | Opc = PPC::FMR; |
| 735 | else if (PPC::CRRCRegClass.contains(DestReg, SrcReg)) |
| 736 | Opc = PPC::MCRF; |
| 737 | else if (PPC::VRRCRegClass.contains(DestReg, SrcReg)) |
| 738 | Opc = PPC::VOR; |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 739 | else if (PPC::VSRCRegClass.contains(DestReg, SrcReg)) |
Hal Finkel | bbad233 | 2014-03-24 09:36:36 +0000 | [diff] [blame] | 740 | // There are two different ways this can be done: |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 741 | // 1. xxlor : This has lower latency (on the P7), 2 cycles, but can only |
| 742 | // issue in VSU pipeline 0. |
| 743 | // 2. xmovdp/xmovsp: This has higher latency (on the P7), 6 cycles, but |
| 744 | // can go to either pipeline. |
Hal Finkel | bbad233 | 2014-03-24 09:36:36 +0000 | [diff] [blame] | 745 | // We'll always use xxlor here, because in practically all cases where |
| 746 | // copies are generated, they are close enough to some use that the |
| 747 | // lower-latency form is preferable. |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 748 | Opc = PPC::XXLOR; |
Hal Finkel | 19be506 | 2014-03-29 05:29:01 +0000 | [diff] [blame] | 749 | else if (PPC::VSFRCRegClass.contains(DestReg, SrcReg)) |
| 750 | Opc = PPC::XXLORf; |
Jakob Stoklund Olesen | 0d61197 | 2010-07-11 07:31:00 +0000 | [diff] [blame] | 751 | else if (PPC::CRBITRCRegClass.contains(DestReg, SrcReg)) |
| 752 | Opc = PPC::CROR; |
| 753 | else |
| 754 | llvm_unreachable("Impossible reg-to-reg copy"); |
Owen Anderson | 7a73ae9 | 2007-12-31 06:32:00 +0000 | [diff] [blame] | 755 | |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 756 | const MCInstrDesc &MCID = get(Opc); |
| 757 | if (MCID.getNumOperands() == 3) |
| 758 | BuildMI(MBB, I, DL, MCID, DestReg) |
Jakob Stoklund Olesen | 0d61197 | 2010-07-11 07:31:00 +0000 | [diff] [blame] | 759 | .addReg(SrcReg).addReg(SrcReg, getKillRegState(KillSrc)); |
| 760 | else |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 761 | BuildMI(MBB, I, DL, MCID, DestReg).addReg(SrcReg, getKillRegState(KillSrc)); |
Owen Anderson | 7a73ae9 | 2007-12-31 06:32:00 +0000 | [diff] [blame] | 762 | } |
| 763 | |
Hal Finkel | 8f6834d | 2011-12-05 17:55:17 +0000 | [diff] [blame] | 764 | // 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] | 765 | bool |
Dan Gohman | 3b46030 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 766 | PPCInstrInfo::StoreRegToStackSlot(MachineFunction &MF, |
| 767 | unsigned SrcReg, bool isKill, |
Bill Wendling | c6c48fc | 2008-03-10 22:49:16 +0000 | [diff] [blame] | 768 | int FrameIdx, |
| 769 | const TargetRegisterClass *RC, |
Hal Finkel | fcc51d4 | 2013-03-17 04:43:44 +0000 | [diff] [blame] | 770 | SmallVectorImpl<MachineInstr*> &NewMIs, |
Hal Finkel | cc1eeda | 2013-03-23 22:06:03 +0000 | [diff] [blame] | 771 | bool &NonRI, bool &SpillsVRS) const{ |
Hal Finkel | 37714b8 | 2013-03-27 21:21:15 +0000 | [diff] [blame] | 772 | // Note: If additional store instructions are added here, |
| 773 | // update isStoreToStackSlot. |
| 774 | |
Chris Lattner | 6f306d7 | 2010-04-02 20:16:16 +0000 | [diff] [blame] | 775 | DebugLoc DL; |
Hal Finkel | 4e703bc | 2014-01-28 05:32:58 +0000 | [diff] [blame] | 776 | if (PPC::GPRCRegClass.hasSubClassEq(RC) || |
| 777 | PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) { |
Hal Finkel | 794e05b | 2013-03-23 17:14:27 +0000 | [diff] [blame] | 778 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW)) |
| 779 | .addReg(SrcReg, |
| 780 | getKillRegState(isKill)), |
| 781 | FrameIdx)); |
Hal Finkel | 4e703bc | 2014-01-28 05:32:58 +0000 | [diff] [blame] | 782 | } else if (PPC::G8RCRegClass.hasSubClassEq(RC) || |
| 783 | PPC::G8RC_NOX0RegClass.hasSubClassEq(RC)) { |
Hal Finkel | 794e05b | 2013-03-23 17:14:27 +0000 | [diff] [blame] | 784 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STD)) |
| 785 | .addReg(SrcReg, |
| 786 | getKillRegState(isKill)), |
| 787 | FrameIdx)); |
Craig Topper | abadc66 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 788 | } else if (PPC::F8RCRegClass.hasSubClassEq(RC)) { |
Dale Johannesen | 6b8c76a | 2009-02-12 23:08:38 +0000 | [diff] [blame] | 789 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFD)) |
Bill Wendling | f7b83c7 | 2009-05-13 21:33:08 +0000 | [diff] [blame] | 790 | .addReg(SrcReg, |
| 791 | getKillRegState(isKill)), |
| 792 | FrameIdx)); |
Craig Topper | abadc66 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 793 | } else if (PPC::F4RCRegClass.hasSubClassEq(RC)) { |
Dale Johannesen | 6b8c76a | 2009-02-12 23:08:38 +0000 | [diff] [blame] | 794 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFS)) |
Bill Wendling | f7b83c7 | 2009-05-13 21:33:08 +0000 | [diff] [blame] | 795 | .addReg(SrcReg, |
| 796 | getKillRegState(isKill)), |
| 797 | FrameIdx)); |
Craig Topper | abadc66 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 798 | } else if (PPC::CRRCRegClass.hasSubClassEq(RC)) { |
Hal Finkel | e154c8f | 2013-03-12 14:12:16 +0000 | [diff] [blame] | 799 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CR)) |
| 800 | .addReg(SrcReg, |
| 801 | getKillRegState(isKill)), |
| 802 | FrameIdx)); |
| 803 | return true; |
Craig Topper | abadc66 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 804 | } else if (PPC::CRBITRCRegClass.hasSubClassEq(RC)) { |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 805 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CRBIT)) |
| 806 | .addReg(SrcReg, |
| 807 | getKillRegState(isKill)), |
| 808 | FrameIdx)); |
| 809 | return true; |
Craig Topper | abadc66 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 810 | } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) { |
Hal Finkel | fcc51d4 | 2013-03-17 04:43:44 +0000 | [diff] [blame] | 811 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STVX)) |
| 812 | .addReg(SrcReg, |
| 813 | getKillRegState(isKill)), |
| 814 | FrameIdx)); |
| 815 | NonRI = true; |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 816 | } else if (PPC::VSRCRegClass.hasSubClassEq(RC)) { |
| 817 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STXVD2X)) |
| 818 | .addReg(SrcReg, |
| 819 | getKillRegState(isKill)), |
| 820 | FrameIdx)); |
| 821 | NonRI = true; |
Hal Finkel | 19be506 | 2014-03-29 05:29:01 +0000 | [diff] [blame] | 822 | } else if (PPC::VSFRCRegClass.hasSubClassEq(RC)) { |
| 823 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STXSDX)) |
| 824 | .addReg(SrcReg, |
| 825 | getKillRegState(isKill)), |
| 826 | FrameIdx)); |
| 827 | NonRI = true; |
Hal Finkel | a1431df | 2013-03-21 19:03:21 +0000 | [diff] [blame] | 828 | } else if (PPC::VRSAVERCRegClass.hasSubClassEq(RC)) { |
Hal Finkel | a7b0630 | 2013-03-27 00:02:20 +0000 | [diff] [blame] | 829 | assert(TM.getSubtargetImpl()->isDarwin() && |
| 830 | "VRSAVE only needs spill/restore on Darwin"); |
Hal Finkel | a1431df | 2013-03-21 19:03:21 +0000 | [diff] [blame] | 831 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_VRSAVE)) |
| 832 | .addReg(SrcReg, |
| 833 | getKillRegState(isKill)), |
| 834 | FrameIdx)); |
Hal Finkel | cc1eeda | 2013-03-23 22:06:03 +0000 | [diff] [blame] | 835 | SpillsVRS = true; |
Owen Anderson | eee1460 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 836 | } else { |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 837 | llvm_unreachable("Unknown regclass!"); |
Owen Anderson | eee1460 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 838 | } |
Bill Wendling | 632ea65 | 2008-03-03 22:19:16 +0000 | [diff] [blame] | 839 | |
| 840 | return false; |
Owen Anderson | eee1460 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 841 | } |
| 842 | |
| 843 | void |
| 844 | PPCInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB, |
Bill Wendling | 632ea65 | 2008-03-03 22:19:16 +0000 | [diff] [blame] | 845 | MachineBasicBlock::iterator MI, |
| 846 | unsigned SrcReg, bool isKill, int FrameIdx, |
Evan Cheng | efb126a | 2010-05-06 19:06:44 +0000 | [diff] [blame] | 847 | const TargetRegisterClass *RC, |
| 848 | const TargetRegisterInfo *TRI) const { |
Dan Gohman | 3b46030 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 849 | MachineFunction &MF = *MBB.getParent(); |
Owen Anderson | eee1460 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 850 | SmallVector<MachineInstr*, 4> NewMIs; |
Bill Wendling | 632ea65 | 2008-03-03 22:19:16 +0000 | [diff] [blame] | 851 | |
Hal Finkel | bb420f1 | 2013-03-15 05:06:04 +0000 | [diff] [blame] | 852 | PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); |
| 853 | FuncInfo->setHasSpills(); |
| 854 | |
Hal Finkel | cc1eeda | 2013-03-23 22:06:03 +0000 | [diff] [blame] | 855 | bool NonRI = false, SpillsVRS = false; |
| 856 | if (StoreRegToStackSlot(MF, SrcReg, isKill, FrameIdx, RC, NewMIs, |
| 857 | NonRI, SpillsVRS)) |
Bill Wendling | 632ea65 | 2008-03-03 22:19:16 +0000 | [diff] [blame] | 858 | FuncInfo->setSpillsCR(); |
Bill Wendling | 632ea65 | 2008-03-03 22:19:16 +0000 | [diff] [blame] | 859 | |
Hal Finkel | cc1eeda | 2013-03-23 22:06:03 +0000 | [diff] [blame] | 860 | if (SpillsVRS) |
| 861 | FuncInfo->setSpillsVRSAVE(); |
| 862 | |
Hal Finkel | fcc51d4 | 2013-03-17 04:43:44 +0000 | [diff] [blame] | 863 | if (NonRI) |
| 864 | FuncInfo->setHasNonRISpills(); |
| 865 | |
Owen Anderson | eee1460 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 866 | for (unsigned i = 0, e = NewMIs.size(); i != e; ++i) |
| 867 | MBB.insert(MI, NewMIs[i]); |
Jakob Stoklund Olesen | 6353e53 | 2010-07-16 18:22:00 +0000 | [diff] [blame] | 868 | |
| 869 | const MachineFrameInfo &MFI = *MF.getFrameInfo(); |
| 870 | MachineMemOperand *MMO = |
Jay Foad | 465101b | 2011-11-15 07:34:52 +0000 | [diff] [blame] | 871 | MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx), |
Chris Lattner | e3d864b | 2010-09-21 04:39:43 +0000 | [diff] [blame] | 872 | MachineMemOperand::MOStore, |
Jakob Stoklund Olesen | 6353e53 | 2010-07-16 18:22:00 +0000 | [diff] [blame] | 873 | MFI.getObjectSize(FrameIdx), |
| 874 | MFI.getObjectAlignment(FrameIdx)); |
| 875 | NewMIs.back()->addMemOperand(MF, MMO); |
Owen Anderson | eee1460 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 876 | } |
| 877 | |
Hal Finkel | bde7f8f | 2011-12-06 20:55:36 +0000 | [diff] [blame] | 878 | bool |
Bill Wendling | f6d609a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 879 | PPCInstrInfo::LoadRegFromStackSlot(MachineFunction &MF, DebugLoc DL, |
Dan Gohman | 3b46030 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 880 | unsigned DestReg, int FrameIdx, |
Bill Wendling | c6c48fc | 2008-03-10 22:49:16 +0000 | [diff] [blame] | 881 | const TargetRegisterClass *RC, |
Hal Finkel | fcc51d4 | 2013-03-17 04:43:44 +0000 | [diff] [blame] | 882 | SmallVectorImpl<MachineInstr*> &NewMIs, |
Hal Finkel | cc1eeda | 2013-03-23 22:06:03 +0000 | [diff] [blame] | 883 | bool &NonRI, bool &SpillsVRS) const{ |
Hal Finkel | 37714b8 | 2013-03-27 21:21:15 +0000 | [diff] [blame] | 884 | // Note: If additional load instructions are added here, |
| 885 | // update isLoadFromStackSlot. |
| 886 | |
Hal Finkel | 4e703bc | 2014-01-28 05:32:58 +0000 | [diff] [blame] | 887 | if (PPC::GPRCRegClass.hasSubClassEq(RC) || |
| 888 | PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) { |
Hal Finkel | 5791f51 | 2013-03-27 19:10:40 +0000 | [diff] [blame] | 889 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ), |
| 890 | DestReg), FrameIdx)); |
Hal Finkel | 4e703bc | 2014-01-28 05:32:58 +0000 | [diff] [blame] | 891 | } else if (PPC::G8RCRegClass.hasSubClassEq(RC) || |
| 892 | PPC::G8RC_NOX0RegClass.hasSubClassEq(RC)) { |
Hal Finkel | 5791f51 | 2013-03-27 19:10:40 +0000 | [diff] [blame] | 893 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LD), DestReg), |
| 894 | FrameIdx)); |
Craig Topper | abadc66 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 895 | } else if (PPC::F8RCRegClass.hasSubClassEq(RC)) { |
Bill Wendling | f6d609a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 896 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFD), DestReg), |
Owen Anderson | eee1460 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 897 | FrameIdx)); |
Craig Topper | abadc66 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 898 | } else if (PPC::F4RCRegClass.hasSubClassEq(RC)) { |
Bill Wendling | f6d609a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 899 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFS), DestReg), |
Owen Anderson | eee1460 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 900 | FrameIdx)); |
Craig Topper | abadc66 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 901 | } else if (PPC::CRRCRegClass.hasSubClassEq(RC)) { |
Hal Finkel | e154c8f | 2013-03-12 14:12:16 +0000 | [diff] [blame] | 902 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, |
| 903 | get(PPC::RESTORE_CR), DestReg), |
| 904 | FrameIdx)); |
| 905 | return true; |
Craig Topper | abadc66 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 906 | } else if (PPC::CRBITRCRegClass.hasSubClassEq(RC)) { |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 907 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, |
| 908 | get(PPC::RESTORE_CRBIT), DestReg), |
| 909 | FrameIdx)); |
| 910 | return true; |
Craig Topper | abadc66 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 911 | } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) { |
Hal Finkel | fcc51d4 | 2013-03-17 04:43:44 +0000 | [diff] [blame] | 912 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LVX), DestReg), |
| 913 | FrameIdx)); |
| 914 | NonRI = true; |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 915 | } else if (PPC::VSRCRegClass.hasSubClassEq(RC)) { |
| 916 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LXVD2X), DestReg), |
| 917 | FrameIdx)); |
| 918 | NonRI = true; |
Hal Finkel | 19be506 | 2014-03-29 05:29:01 +0000 | [diff] [blame] | 919 | } else if (PPC::VSFRCRegClass.hasSubClassEq(RC)) { |
| 920 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LXSDX), DestReg), |
| 921 | FrameIdx)); |
| 922 | NonRI = true; |
Hal Finkel | a1431df | 2013-03-21 19:03:21 +0000 | [diff] [blame] | 923 | } else if (PPC::VRSAVERCRegClass.hasSubClassEq(RC)) { |
Hal Finkel | a7b0630 | 2013-03-27 00:02:20 +0000 | [diff] [blame] | 924 | assert(TM.getSubtargetImpl()->isDarwin() && |
| 925 | "VRSAVE only needs spill/restore on Darwin"); |
Hal Finkel | a1431df | 2013-03-21 19:03:21 +0000 | [diff] [blame] | 926 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, |
| 927 | get(PPC::RESTORE_VRSAVE), |
| 928 | DestReg), |
| 929 | FrameIdx)); |
Hal Finkel | cc1eeda | 2013-03-23 22:06:03 +0000 | [diff] [blame] | 930 | SpillsVRS = true; |
Owen Anderson | eee1460 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 931 | } else { |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 932 | llvm_unreachable("Unknown regclass!"); |
Owen Anderson | eee1460 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 933 | } |
Hal Finkel | bde7f8f | 2011-12-06 20:55:36 +0000 | [diff] [blame] | 934 | |
| 935 | return false; |
Owen Anderson | eee1460 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | void |
| 939 | PPCInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB, |
Bill Wendling | 632ea65 | 2008-03-03 22:19:16 +0000 | [diff] [blame] | 940 | MachineBasicBlock::iterator MI, |
| 941 | unsigned DestReg, int FrameIdx, |
Evan Cheng | efb126a | 2010-05-06 19:06:44 +0000 | [diff] [blame] | 942 | const TargetRegisterClass *RC, |
| 943 | const TargetRegisterInfo *TRI) const { |
Dan Gohman | 3b46030 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 944 | MachineFunction &MF = *MBB.getParent(); |
Owen Anderson | eee1460 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 945 | SmallVector<MachineInstr*, 4> NewMIs; |
Chris Lattner | 6f306d7 | 2010-04-02 20:16:16 +0000 | [diff] [blame] | 946 | DebugLoc DL; |
Bill Wendling | f6d609a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 947 | if (MI != MBB.end()) DL = MI->getDebugLoc(); |
Hal Finkel | fcc51d4 | 2013-03-17 04:43:44 +0000 | [diff] [blame] | 948 | |
| 949 | PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); |
| 950 | FuncInfo->setHasSpills(); |
| 951 | |
Hal Finkel | cc1eeda | 2013-03-23 22:06:03 +0000 | [diff] [blame] | 952 | bool NonRI = false, SpillsVRS = false; |
| 953 | if (LoadRegFromStackSlot(MF, DL, DestReg, FrameIdx, RC, NewMIs, |
| 954 | NonRI, SpillsVRS)) |
Hal Finkel | bde7f8f | 2011-12-06 20:55:36 +0000 | [diff] [blame] | 955 | FuncInfo->setSpillsCR(); |
Hal Finkel | fcc51d4 | 2013-03-17 04:43:44 +0000 | [diff] [blame] | 956 | |
Hal Finkel | cc1eeda | 2013-03-23 22:06:03 +0000 | [diff] [blame] | 957 | if (SpillsVRS) |
| 958 | FuncInfo->setSpillsVRSAVE(); |
| 959 | |
Hal Finkel | fcc51d4 | 2013-03-17 04:43:44 +0000 | [diff] [blame] | 960 | if (NonRI) |
| 961 | FuncInfo->setHasNonRISpills(); |
| 962 | |
Owen Anderson | eee1460 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 963 | for (unsigned i = 0, e = NewMIs.size(); i != e; ++i) |
| 964 | MBB.insert(MI, NewMIs[i]); |
Jakob Stoklund Olesen | 6353e53 | 2010-07-16 18:22:00 +0000 | [diff] [blame] | 965 | |
| 966 | const MachineFrameInfo &MFI = *MF.getFrameInfo(); |
| 967 | MachineMemOperand *MMO = |
Jay Foad | 465101b | 2011-11-15 07:34:52 +0000 | [diff] [blame] | 968 | MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx), |
Chris Lattner | e3d864b | 2010-09-21 04:39:43 +0000 | [diff] [blame] | 969 | MachineMemOperand::MOLoad, |
Jakob Stoklund Olesen | 6353e53 | 2010-07-16 18:22:00 +0000 | [diff] [blame] | 970 | MFI.getObjectSize(FrameIdx), |
| 971 | MFI.getObjectAlignment(FrameIdx)); |
| 972 | NewMIs.back()->addMemOperand(MF, MMO); |
Owen Anderson | eee1460 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 973 | } |
| 974 | |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 975 | bool PPCInstrInfo:: |
Owen Anderson | 4f6bf04 | 2008-08-14 22:49:33 +0000 | [diff] [blame] | 976 | ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const { |
Chris Lattner | 23f22de | 2006-10-21 06:03:11 +0000 | [diff] [blame] | 977 | assert(Cond.size() == 2 && "Invalid PPC branch opcode!"); |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 978 | if (Cond[1].getReg() == PPC::CTR8 || Cond[1].getReg() == PPC::CTR) |
| 979 | Cond[0].setImm(Cond[0].getImm() == 0 ? 1 : 0); |
| 980 | else |
| 981 | // Leave the CR# the same, but invert the condition. |
| 982 | Cond[0].setImm(PPC::InvertPredicate((PPC::Predicate)Cond[0].getImm())); |
Chris Lattner | 23f22de | 2006-10-21 06:03:11 +0000 | [diff] [blame] | 983 | return false; |
Chris Lattner | a47294ed | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 984 | } |
Nicolas Geoffray | ae84bbd | 2008-04-16 20:10:13 +0000 | [diff] [blame] | 985 | |
Hal Finkel | d61d4f8 | 2013-04-06 19:30:30 +0000 | [diff] [blame] | 986 | bool PPCInstrInfo::FoldImmediate(MachineInstr *UseMI, MachineInstr *DefMI, |
| 987 | unsigned Reg, MachineRegisterInfo *MRI) const { |
| 988 | // For some instructions, it is legal to fold ZERO into the RA register field. |
| 989 | // A zero immediate should always be loaded with a single li. |
| 990 | unsigned DefOpc = DefMI->getOpcode(); |
| 991 | if (DefOpc != PPC::LI && DefOpc != PPC::LI8) |
| 992 | return false; |
| 993 | if (!DefMI->getOperand(1).isImm()) |
| 994 | return false; |
| 995 | if (DefMI->getOperand(1).getImm() != 0) |
| 996 | return false; |
| 997 | |
| 998 | // Note that we cannot here invert the arguments of an isel in order to fold |
| 999 | // a ZERO into what is presented as the second argument. All we have here |
| 1000 | // is the condition bit, and that might come from a CR-logical bit operation. |
| 1001 | |
| 1002 | const MCInstrDesc &UseMCID = UseMI->getDesc(); |
| 1003 | |
| 1004 | // Only fold into real machine instructions. |
| 1005 | if (UseMCID.isPseudo()) |
| 1006 | return false; |
| 1007 | |
| 1008 | unsigned UseIdx; |
| 1009 | for (UseIdx = 0; UseIdx < UseMI->getNumOperands(); ++UseIdx) |
| 1010 | if (UseMI->getOperand(UseIdx).isReg() && |
| 1011 | UseMI->getOperand(UseIdx).getReg() == Reg) |
| 1012 | break; |
| 1013 | |
| 1014 | assert(UseIdx < UseMI->getNumOperands() && "Cannot find Reg in UseMI"); |
| 1015 | assert(UseIdx < UseMCID.getNumOperands() && "No operand description for Reg"); |
| 1016 | |
| 1017 | const MCOperandInfo *UseInfo = &UseMCID.OpInfo[UseIdx]; |
| 1018 | |
| 1019 | // We can fold the zero if this register requires a GPRC_NOR0/G8RC_NOX0 |
| 1020 | // register (which might also be specified as a pointer class kind). |
| 1021 | if (UseInfo->isLookupPtrRegClass()) { |
| 1022 | if (UseInfo->RegClass /* Kind */ != 1) |
| 1023 | return false; |
| 1024 | } else { |
| 1025 | if (UseInfo->RegClass != PPC::GPRC_NOR0RegClassID && |
| 1026 | UseInfo->RegClass != PPC::G8RC_NOX0RegClassID) |
| 1027 | return false; |
| 1028 | } |
| 1029 | |
| 1030 | // Make sure this is not tied to an output register (or otherwise |
| 1031 | // constrained). This is true for ST?UX registers, for example, which |
| 1032 | // are tied to their output registers. |
| 1033 | if (UseInfo->Constraints != 0) |
| 1034 | return false; |
| 1035 | |
| 1036 | unsigned ZeroReg; |
| 1037 | if (UseInfo->isLookupPtrRegClass()) { |
| 1038 | bool isPPC64 = TM.getSubtargetImpl()->isPPC64(); |
| 1039 | ZeroReg = isPPC64 ? PPC::ZERO8 : PPC::ZERO; |
| 1040 | } else { |
| 1041 | ZeroReg = UseInfo->RegClass == PPC::G8RC_NOX0RegClassID ? |
| 1042 | PPC::ZERO8 : PPC::ZERO; |
| 1043 | } |
| 1044 | |
| 1045 | bool DeleteDef = MRI->hasOneNonDBGUse(Reg); |
| 1046 | UseMI->getOperand(UseIdx).setReg(ZeroReg); |
| 1047 | |
| 1048 | if (DeleteDef) |
| 1049 | DefMI->eraseFromParent(); |
| 1050 | |
| 1051 | return true; |
| 1052 | } |
| 1053 | |
Hal Finkel | 30ae229 | 2013-04-10 18:30:16 +0000 | [diff] [blame] | 1054 | static bool MBBDefinesCTR(MachineBasicBlock &MBB) { |
| 1055 | for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end(); |
| 1056 | I != IE; ++I) |
| 1057 | if (I->definesRegister(PPC::CTR) || I->definesRegister(PPC::CTR8)) |
| 1058 | return true; |
| 1059 | return false; |
| 1060 | } |
| 1061 | |
| 1062 | // We should make sure that, if we're going to predicate both sides of a |
| 1063 | // condition (a diamond), that both sides don't define the counter register. We |
| 1064 | // can predicate counter-decrement-based branches, but while that predicates |
| 1065 | // the branching, it does not predicate the counter decrement. If we tried to |
| 1066 | // merge the triangle into one predicated block, we'd decrement the counter |
| 1067 | // twice. |
| 1068 | bool PPCInstrInfo::isProfitableToIfCvt(MachineBasicBlock &TMBB, |
| 1069 | unsigned NumT, unsigned ExtraT, |
| 1070 | MachineBasicBlock &FMBB, |
| 1071 | unsigned NumF, unsigned ExtraF, |
| 1072 | const BranchProbability &Probability) const { |
| 1073 | return !(MBBDefinesCTR(TMBB) && MBBDefinesCTR(FMBB)); |
| 1074 | } |
| 1075 | |
| 1076 | |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1077 | bool PPCInstrInfo::isPredicated(const MachineInstr *MI) const { |
Hal Finkel | f29285a | 2013-04-11 01:23:34 +0000 | [diff] [blame] | 1078 | // The predicated branches are identified by their type, not really by the |
| 1079 | // explicit presence of a predicate. Furthermore, some of them can be |
| 1080 | // predicated more than once. Because if conversion won't try to predicate |
| 1081 | // any instruction which already claims to be predicated (by returning true |
| 1082 | // here), always return false. In doing so, we let isPredicable() be the |
| 1083 | // final word on whether not the instruction can be (further) predicated. |
| 1084 | |
| 1085 | return false; |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1086 | } |
| 1087 | |
| 1088 | bool PPCInstrInfo::isUnpredicatedTerminator(const MachineInstr *MI) const { |
| 1089 | if (!MI->isTerminator()) |
| 1090 | return false; |
| 1091 | |
| 1092 | // Conditional branch is a special case. |
| 1093 | if (MI->isBranch() && !MI->isBarrier()) |
| 1094 | return true; |
| 1095 | |
| 1096 | return !isPredicated(MI); |
| 1097 | } |
| 1098 | |
| 1099 | bool PPCInstrInfo::PredicateInstruction( |
| 1100 | MachineInstr *MI, |
| 1101 | const SmallVectorImpl<MachineOperand> &Pred) const { |
| 1102 | unsigned OpC = MI->getOpcode(); |
| 1103 | if (OpC == PPC::BLR) { |
| 1104 | if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) { |
| 1105 | bool isPPC64 = TM.getSubtargetImpl()->isPPC64(); |
| 1106 | MI->setDesc(get(Pred[0].getImm() ? |
| 1107 | (isPPC64 ? PPC::BDNZLR8 : PPC::BDNZLR) : |
| 1108 | (isPPC64 ? PPC::BDZLR8 : PPC::BDZLR))); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 1109 | } else if (Pred[0].getImm() == PPC::PRED_BIT_SET) { |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1110 | MI->setDesc(get(PPC::BCLR)); |
| 1111 | MachineInstrBuilder(*MI->getParent()->getParent(), MI) |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 1112 | .addReg(Pred[1].getReg()); |
| 1113 | } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) { |
| 1114 | MI->setDesc(get(PPC::BCLRn)); |
| 1115 | MachineInstrBuilder(*MI->getParent()->getParent(), MI) |
| 1116 | .addReg(Pred[1].getReg()); |
| 1117 | } else { |
| 1118 | MI->setDesc(get(PPC::BCCLR)); |
| 1119 | MachineInstrBuilder(*MI->getParent()->getParent(), MI) |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1120 | .addImm(Pred[0].getImm()) |
| 1121 | .addReg(Pred[1].getReg()); |
| 1122 | } |
| 1123 | |
| 1124 | return true; |
| 1125 | } else if (OpC == PPC::B) { |
| 1126 | if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) { |
| 1127 | bool isPPC64 = TM.getSubtargetImpl()->isPPC64(); |
| 1128 | MI->setDesc(get(Pred[0].getImm() ? |
| 1129 | (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) : |
| 1130 | (isPPC64 ? PPC::BDZ8 : PPC::BDZ))); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 1131 | } else if (Pred[0].getImm() == PPC::PRED_BIT_SET) { |
| 1132 | MachineBasicBlock *MBB = MI->getOperand(0).getMBB(); |
| 1133 | MI->RemoveOperand(0); |
| 1134 | |
| 1135 | MI->setDesc(get(PPC::BC)); |
| 1136 | MachineInstrBuilder(*MI->getParent()->getParent(), MI) |
| 1137 | .addReg(Pred[1].getReg()) |
| 1138 | .addMBB(MBB); |
| 1139 | } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) { |
| 1140 | MachineBasicBlock *MBB = MI->getOperand(0).getMBB(); |
| 1141 | MI->RemoveOperand(0); |
| 1142 | |
| 1143 | MI->setDesc(get(PPC::BCn)); |
| 1144 | MachineInstrBuilder(*MI->getParent()->getParent(), MI) |
| 1145 | .addReg(Pred[1].getReg()) |
| 1146 | .addMBB(MBB); |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1147 | } else { |
| 1148 | MachineBasicBlock *MBB = MI->getOperand(0).getMBB(); |
| 1149 | MI->RemoveOperand(0); |
| 1150 | |
| 1151 | MI->setDesc(get(PPC::BCC)); |
| 1152 | MachineInstrBuilder(*MI->getParent()->getParent(), MI) |
| 1153 | .addImm(Pred[0].getImm()) |
| 1154 | .addReg(Pred[1].getReg()) |
| 1155 | .addMBB(MBB); |
| 1156 | } |
| 1157 | |
| 1158 | return true; |
Hal Finkel | 500b004 | 2013-04-10 06:42:34 +0000 | [diff] [blame] | 1159 | } else if (OpC == PPC::BCTR || OpC == PPC::BCTR8 || |
| 1160 | OpC == PPC::BCTRL || OpC == PPC::BCTRL8) { |
| 1161 | if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) |
| 1162 | llvm_unreachable("Cannot predicate bctr[l] on the ctr register"); |
| 1163 | |
| 1164 | bool setLR = OpC == PPC::BCTRL || OpC == PPC::BCTRL8; |
| 1165 | bool isPPC64 = TM.getSubtargetImpl()->isPPC64(); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 1166 | |
| 1167 | if (Pred[0].getImm() == PPC::PRED_BIT_SET) { |
| 1168 | MI->setDesc(get(isPPC64 ? (setLR ? PPC::BCCTRL8 : PPC::BCCTR8) : |
| 1169 | (setLR ? PPC::BCCTRL : PPC::BCCTR))); |
| 1170 | MachineInstrBuilder(*MI->getParent()->getParent(), MI) |
| 1171 | .addReg(Pred[1].getReg()); |
| 1172 | return true; |
| 1173 | } else if (Pred[0].getImm() == PPC::PRED_BIT_UNSET) { |
| 1174 | MI->setDesc(get(isPPC64 ? (setLR ? PPC::BCCTRL8n : PPC::BCCTR8n) : |
| 1175 | (setLR ? PPC::BCCTRLn : PPC::BCCTRn))); |
| 1176 | MachineInstrBuilder(*MI->getParent()->getParent(), MI) |
| 1177 | .addReg(Pred[1].getReg()); |
| 1178 | return true; |
| 1179 | } |
| 1180 | |
| 1181 | MI->setDesc(get(isPPC64 ? (setLR ? PPC::BCCCTRL8 : PPC::BCCCTR8) : |
| 1182 | (setLR ? PPC::BCCCTRL : PPC::BCCCTR))); |
Hal Finkel | 500b004 | 2013-04-10 06:42:34 +0000 | [diff] [blame] | 1183 | MachineInstrBuilder(*MI->getParent()->getParent(), MI) |
| 1184 | .addImm(Pred[0].getImm()) |
| 1185 | .addReg(Pred[1].getReg()); |
| 1186 | return true; |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1187 | } |
| 1188 | |
| 1189 | return false; |
| 1190 | } |
| 1191 | |
| 1192 | bool PPCInstrInfo::SubsumesPredicate( |
| 1193 | const SmallVectorImpl<MachineOperand> &Pred1, |
| 1194 | const SmallVectorImpl<MachineOperand> &Pred2) const { |
| 1195 | assert(Pred1.size() == 2 && "Invalid PPC first predicate"); |
| 1196 | assert(Pred2.size() == 2 && "Invalid PPC second predicate"); |
| 1197 | |
| 1198 | if (Pred1[1].getReg() == PPC::CTR8 || Pred1[1].getReg() == PPC::CTR) |
| 1199 | return false; |
| 1200 | if (Pred2[1].getReg() == PPC::CTR8 || Pred2[1].getReg() == PPC::CTR) |
| 1201 | return false; |
| 1202 | |
Hal Finkel | 94a6f38 | 2013-12-11 23:12:25 +0000 | [diff] [blame] | 1203 | // P1 can only subsume P2 if they test the same condition register. |
| 1204 | if (Pred1[1].getReg() != Pred2[1].getReg()) |
| 1205 | return false; |
| 1206 | |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1207 | PPC::Predicate P1 = (PPC::Predicate) Pred1[0].getImm(); |
| 1208 | PPC::Predicate P2 = (PPC::Predicate) Pred2[0].getImm(); |
| 1209 | |
| 1210 | if (P1 == P2) |
| 1211 | return true; |
| 1212 | |
| 1213 | // Does P1 subsume P2, e.g. GE subsumes GT. |
| 1214 | if (P1 == PPC::PRED_LE && |
| 1215 | (P2 == PPC::PRED_LT || P2 == PPC::PRED_EQ)) |
| 1216 | return true; |
| 1217 | if (P1 == PPC::PRED_GE && |
| 1218 | (P2 == PPC::PRED_GT || P2 == PPC::PRED_EQ)) |
| 1219 | return true; |
| 1220 | |
| 1221 | return false; |
| 1222 | } |
| 1223 | |
| 1224 | bool PPCInstrInfo::DefinesPredicate(MachineInstr *MI, |
| 1225 | std::vector<MachineOperand> &Pred) const { |
| 1226 | // Note: At the present time, the contents of Pred from this function is |
| 1227 | // unused by IfConversion. This implementation follows ARM by pushing the |
| 1228 | // CR-defining operand. Because the 'DZ' and 'DNZ' count as types of |
| 1229 | // predicate, instructions defining CTR or CTR8 are also included as |
| 1230 | // predicate-defining instructions. |
| 1231 | |
| 1232 | const TargetRegisterClass *RCs[] = |
| 1233 | { &PPC::CRRCRegClass, &PPC::CRBITRCRegClass, |
| 1234 | &PPC::CTRRCRegClass, &PPC::CTRRC8RegClass }; |
| 1235 | |
| 1236 | bool Found = false; |
| 1237 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 1238 | const MachineOperand &MO = MI->getOperand(i); |
Hal Finkel | af82201 | 2013-04-10 07:17:47 +0000 | [diff] [blame] | 1239 | for (unsigned c = 0; c < array_lengthof(RCs) && !Found; ++c) { |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1240 | const TargetRegisterClass *RC = RCs[c]; |
Hal Finkel | af82201 | 2013-04-10 07:17:47 +0000 | [diff] [blame] | 1241 | if (MO.isReg()) { |
| 1242 | if (MO.isDef() && RC->contains(MO.getReg())) { |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1243 | Pred.push_back(MO); |
| 1244 | Found = true; |
| 1245 | } |
Hal Finkel | af82201 | 2013-04-10 07:17:47 +0000 | [diff] [blame] | 1246 | } else if (MO.isRegMask()) { |
| 1247 | for (TargetRegisterClass::iterator I = RC->begin(), |
| 1248 | IE = RC->end(); I != IE; ++I) |
| 1249 | if (MO.clobbersPhysReg(*I)) { |
| 1250 | Pred.push_back(MO); |
| 1251 | Found = true; |
| 1252 | } |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1253 | } |
| 1254 | } |
| 1255 | } |
| 1256 | |
| 1257 | return Found; |
| 1258 | } |
| 1259 | |
| 1260 | bool PPCInstrInfo::isPredicable(MachineInstr *MI) const { |
| 1261 | unsigned OpC = MI->getOpcode(); |
| 1262 | switch (OpC) { |
| 1263 | default: |
| 1264 | return false; |
| 1265 | case PPC::B: |
| 1266 | case PPC::BLR: |
Hal Finkel | 500b004 | 2013-04-10 06:42:34 +0000 | [diff] [blame] | 1267 | case PPC::BCTR: |
| 1268 | case PPC::BCTR8: |
| 1269 | case PPC::BCTRL: |
| 1270 | case PPC::BCTRL8: |
Hal Finkel | 5711eca | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1271 | return true; |
| 1272 | } |
| 1273 | } |
| 1274 | |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1275 | bool PPCInstrInfo::analyzeCompare(const MachineInstr *MI, |
| 1276 | unsigned &SrcReg, unsigned &SrcReg2, |
| 1277 | int &Mask, int &Value) const { |
| 1278 | unsigned Opc = MI->getOpcode(); |
| 1279 | |
| 1280 | switch (Opc) { |
| 1281 | default: return false; |
| 1282 | case PPC::CMPWI: |
| 1283 | case PPC::CMPLWI: |
| 1284 | case PPC::CMPDI: |
| 1285 | case PPC::CMPLDI: |
| 1286 | SrcReg = MI->getOperand(1).getReg(); |
| 1287 | SrcReg2 = 0; |
| 1288 | Value = MI->getOperand(2).getImm(); |
| 1289 | Mask = 0xFFFF; |
| 1290 | return true; |
| 1291 | case PPC::CMPW: |
| 1292 | case PPC::CMPLW: |
| 1293 | case PPC::CMPD: |
| 1294 | case PPC::CMPLD: |
| 1295 | case PPC::FCMPUS: |
| 1296 | case PPC::FCMPUD: |
| 1297 | SrcReg = MI->getOperand(1).getReg(); |
| 1298 | SrcReg2 = MI->getOperand(2).getReg(); |
| 1299 | return true; |
| 1300 | } |
| 1301 | } |
Hal Finkel | e632239 | 2013-04-19 22:08:38 +0000 | [diff] [blame] | 1302 | |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1303 | bool PPCInstrInfo::optimizeCompareInstr(MachineInstr *CmpInstr, |
| 1304 | unsigned SrcReg, unsigned SrcReg2, |
| 1305 | int Mask, int Value, |
| 1306 | const MachineRegisterInfo *MRI) const { |
Hal Finkel | b12da6b | 2013-04-18 22:54:25 +0000 | [diff] [blame] | 1307 | if (DisableCmpOpt) |
| 1308 | return false; |
| 1309 | |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1310 | int OpC = CmpInstr->getOpcode(); |
| 1311 | unsigned CRReg = CmpInstr->getOperand(0).getReg(); |
Hal Finkel | 08e53ee | 2013-05-08 12:16:14 +0000 | [diff] [blame] | 1312 | |
| 1313 | // FP record forms set CR1 based on the execption status bits, not a |
| 1314 | // comparison with zero. |
| 1315 | if (OpC == PPC::FCMPUS || OpC == PPC::FCMPUD) |
| 1316 | return false; |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1317 | |
| 1318 | // The record forms set the condition register based on a signed comparison |
| 1319 | // with zero (so says the ISA manual). This is not as straightforward as it |
| 1320 | // seems, however, because this is always a 64-bit comparison on PPC64, even |
| 1321 | // for instructions that are 32-bit in nature (like slw for example). |
| 1322 | // So, on PPC32, for unsigned comparisons, we can use the record forms only |
| 1323 | // for equality checks (as those don't depend on the sign). On PPC64, |
| 1324 | // we are restricted to equality for unsigned 64-bit comparisons and for |
| 1325 | // signed 32-bit comparisons the applicability is more restricted. |
| 1326 | bool isPPC64 = TM.getSubtargetImpl()->isPPC64(); |
| 1327 | bool is32BitSignedCompare = OpC == PPC::CMPWI || OpC == PPC::CMPW; |
| 1328 | bool is32BitUnsignedCompare = OpC == PPC::CMPLWI || OpC == PPC::CMPLW; |
| 1329 | bool is64BitUnsignedCompare = OpC == PPC::CMPLDI || OpC == PPC::CMPLD; |
| 1330 | |
| 1331 | // Get the unique definition of SrcReg. |
| 1332 | MachineInstr *MI = MRI->getUniqueVRegDef(SrcReg); |
| 1333 | if (!MI) return false; |
| 1334 | int MIOpC = MI->getOpcode(); |
| 1335 | |
| 1336 | bool equalityOnly = false; |
| 1337 | bool noSub = false; |
| 1338 | if (isPPC64) { |
| 1339 | if (is32BitSignedCompare) { |
| 1340 | // We can perform this optimization only if MI is sign-extending. |
| 1341 | if (MIOpC == PPC::SRAW || MIOpC == PPC::SRAWo || |
| 1342 | MIOpC == PPC::SRAWI || MIOpC == PPC::SRAWIo || |
| 1343 | MIOpC == PPC::EXTSB || MIOpC == PPC::EXTSBo || |
| 1344 | MIOpC == PPC::EXTSH || MIOpC == PPC::EXTSHo || |
| 1345 | MIOpC == PPC::EXTSW || MIOpC == PPC::EXTSWo) { |
| 1346 | noSub = true; |
| 1347 | } else |
| 1348 | return false; |
| 1349 | } else if (is32BitUnsignedCompare) { |
| 1350 | // We can perform this optimization, equality only, if MI is |
| 1351 | // zero-extending. |
| 1352 | if (MIOpC == PPC::CNTLZW || MIOpC == PPC::CNTLZWo || |
| 1353 | MIOpC == PPC::SLW || MIOpC == PPC::SLWo || |
| 1354 | MIOpC == PPC::SRW || MIOpC == PPC::SRWo) { |
| 1355 | noSub = true; |
| 1356 | equalityOnly = true; |
| 1357 | } else |
| 1358 | return false; |
Hal Finkel | 08e53ee | 2013-05-08 12:16:14 +0000 | [diff] [blame] | 1359 | } else |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1360 | equalityOnly = is64BitUnsignedCompare; |
Hal Finkel | 08e53ee | 2013-05-08 12:16:14 +0000 | [diff] [blame] | 1361 | } else |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1362 | equalityOnly = is32BitUnsignedCompare; |
| 1363 | |
| 1364 | if (equalityOnly) { |
| 1365 | // We need to check the uses of the condition register in order to reject |
| 1366 | // non-equality comparisons. |
Owen Anderson | 16c6bf4 | 2014-03-13 23:12:04 +0000 | [diff] [blame] | 1367 | for (MachineRegisterInfo::use_instr_iterator I =MRI->use_instr_begin(CRReg), |
| 1368 | IE = MRI->use_instr_end(); I != IE; ++I) { |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1369 | MachineInstr *UseMI = &*I; |
| 1370 | if (UseMI->getOpcode() == PPC::BCC) { |
| 1371 | unsigned Pred = UseMI->getOperand(0).getImm(); |
Hal Finkel | c363245 | 2013-05-07 17:49:55 +0000 | [diff] [blame] | 1372 | if (Pred != PPC::PRED_EQ && Pred != PPC::PRED_NE) |
| 1373 | return false; |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1374 | } else if (UseMI->getOpcode() == PPC::ISEL || |
| 1375 | UseMI->getOpcode() == PPC::ISEL8) { |
| 1376 | unsigned SubIdx = UseMI->getOperand(3).getSubReg(); |
Hal Finkel | c363245 | 2013-05-07 17:49:55 +0000 | [diff] [blame] | 1377 | if (SubIdx != PPC::sub_eq) |
| 1378 | return false; |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1379 | } else |
| 1380 | return false; |
| 1381 | } |
| 1382 | } |
| 1383 | |
Hal Finkel | c363245 | 2013-05-07 17:49:55 +0000 | [diff] [blame] | 1384 | MachineBasicBlock::iterator I = CmpInstr; |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1385 | |
| 1386 | // Scan forward to find the first use of the compare. |
| 1387 | for (MachineBasicBlock::iterator EL = CmpInstr->getParent()->end(); |
| 1388 | I != EL; ++I) { |
| 1389 | bool FoundUse = false; |
Owen Anderson | 16c6bf4 | 2014-03-13 23:12:04 +0000 | [diff] [blame] | 1390 | for (MachineRegisterInfo::use_instr_iterator J =MRI->use_instr_begin(CRReg), |
| 1391 | JE = MRI->use_instr_end(); J != JE; ++J) |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1392 | if (&*J == &*I) { |
| 1393 | FoundUse = true; |
| 1394 | break; |
| 1395 | } |
| 1396 | |
| 1397 | if (FoundUse) |
| 1398 | break; |
| 1399 | } |
| 1400 | |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1401 | // There are two possible candidates which can be changed to set CR[01]. |
| 1402 | // One is MI, the other is a SUB instruction. |
| 1403 | // For CMPrr(r1,r2), we are looking for SUB(r1,r2) or SUB(r2,r1). |
| 1404 | MachineInstr *Sub = NULL; |
| 1405 | if (SrcReg2 != 0) |
| 1406 | // MI is not a candidate for CMPrr. |
| 1407 | MI = NULL; |
| 1408 | // FIXME: Conservatively refuse to convert an instruction which isn't in the |
| 1409 | // same BB as the comparison. This is to allow the check below to avoid calls |
| 1410 | // (and other explicit clobbers); instead we should really check for these |
| 1411 | // more explicitly (in at least a few predecessors). |
| 1412 | else if (MI->getParent() != CmpInstr->getParent() || Value != 0) { |
| 1413 | // PPC does not have a record-form SUBri. |
| 1414 | return false; |
| 1415 | } |
| 1416 | |
| 1417 | // Search for Sub. |
| 1418 | const TargetRegisterInfo *TRI = &getRegisterInfo(); |
| 1419 | --I; |
Hal Finkel | c363245 | 2013-05-07 17:49:55 +0000 | [diff] [blame] | 1420 | |
| 1421 | // Get ready to iterate backward from CmpInstr. |
| 1422 | MachineBasicBlock::iterator E = MI, |
| 1423 | B = CmpInstr->getParent()->begin(); |
| 1424 | |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1425 | for (; I != E && !noSub; --I) { |
| 1426 | const MachineInstr &Instr = *I; |
| 1427 | unsigned IOpC = Instr.getOpcode(); |
| 1428 | |
| 1429 | if (&*I != CmpInstr && ( |
Hal Finkel | 08e53ee | 2013-05-08 12:16:14 +0000 | [diff] [blame] | 1430 | Instr.modifiesRegister(PPC::CR0, TRI) || |
| 1431 | Instr.readsRegister(PPC::CR0, TRI))) |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1432 | // This instruction modifies or uses the record condition register after |
| 1433 | // the one we want to change. While we could do this transformation, it |
| 1434 | // would likely not be profitable. This transformation removes one |
| 1435 | // instruction, and so even forcing RA to generate one move probably |
| 1436 | // makes it unprofitable. |
| 1437 | return false; |
| 1438 | |
| 1439 | // Check whether CmpInstr can be made redundant by the current instruction. |
| 1440 | if ((OpC == PPC::CMPW || OpC == PPC::CMPLW || |
| 1441 | OpC == PPC::CMPD || OpC == PPC::CMPLD) && |
| 1442 | (IOpC == PPC::SUBF || IOpC == PPC::SUBF8) && |
| 1443 | ((Instr.getOperand(1).getReg() == SrcReg && |
| 1444 | Instr.getOperand(2).getReg() == SrcReg2) || |
| 1445 | (Instr.getOperand(1).getReg() == SrcReg2 && |
| 1446 | Instr.getOperand(2).getReg() == SrcReg))) { |
| 1447 | Sub = &*I; |
| 1448 | break; |
| 1449 | } |
| 1450 | |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1451 | if (I == B) |
| 1452 | // The 'and' is below the comparison instruction. |
| 1453 | return false; |
| 1454 | } |
| 1455 | |
| 1456 | // Return false if no candidates exist. |
| 1457 | if (!MI && !Sub) |
| 1458 | return false; |
| 1459 | |
| 1460 | // The single candidate is called MI. |
| 1461 | if (!MI) MI = Sub; |
| 1462 | |
| 1463 | int NewOpC = -1; |
| 1464 | MIOpC = MI->getOpcode(); |
| 1465 | if (MIOpC == PPC::ANDIo || MIOpC == PPC::ANDIo8) |
| 1466 | NewOpC = MIOpC; |
| 1467 | else { |
| 1468 | NewOpC = PPC::getRecordFormOpcode(MIOpC); |
| 1469 | if (NewOpC == -1 && PPC::getNonRecordFormOpcode(MIOpC) != -1) |
| 1470 | NewOpC = MIOpC; |
| 1471 | } |
| 1472 | |
| 1473 | // FIXME: On the non-embedded POWER architectures, only some of the record |
| 1474 | // forms are fast, and we should use only the fast ones. |
| 1475 | |
| 1476 | // The defining instruction has a record form (or is already a record |
| 1477 | // form). It is possible, however, that we'll need to reverse the condition |
| 1478 | // code of the users. |
| 1479 | if (NewOpC == -1) |
| 1480 | return false; |
| 1481 | |
Hal Finkel | e632239 | 2013-04-19 22:08:38 +0000 | [diff] [blame] | 1482 | SmallVector<std::pair<MachineOperand*, PPC::Predicate>, 4> PredsToUpdate; |
| 1483 | SmallVector<std::pair<MachineOperand*, unsigned>, 4> SubRegsToUpdate; |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1484 | |
| 1485 | // If we have SUB(r1, r2) and CMP(r2, r1), the condition code based on CMP |
| 1486 | // needs to be updated to be based on SUB. Push the condition code |
| 1487 | // operands to OperandsToUpdate. If it is safe to remove CmpInstr, the |
| 1488 | // condition code of these operands will be modified. |
| 1489 | bool ShouldSwap = false; |
| 1490 | if (Sub) { |
| 1491 | ShouldSwap = SrcReg2 != 0 && Sub->getOperand(1).getReg() == SrcReg2 && |
| 1492 | Sub->getOperand(2).getReg() == SrcReg; |
| 1493 | |
| 1494 | // The operands to subf are the opposite of sub, so only in the fixed-point |
| 1495 | // case, invert the order. |
Hal Finkel | 08e53ee | 2013-05-08 12:16:14 +0000 | [diff] [blame] | 1496 | ShouldSwap = !ShouldSwap; |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1497 | } |
| 1498 | |
| 1499 | if (ShouldSwap) |
Owen Anderson | 16c6bf4 | 2014-03-13 23:12:04 +0000 | [diff] [blame] | 1500 | for (MachineRegisterInfo::use_instr_iterator |
| 1501 | I = MRI->use_instr_begin(CRReg), IE = MRI->use_instr_end(); |
| 1502 | I != IE; ++I) { |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1503 | MachineInstr *UseMI = &*I; |
| 1504 | if (UseMI->getOpcode() == PPC::BCC) { |
| 1505 | PPC::Predicate Pred = (PPC::Predicate) UseMI->getOperand(0).getImm(); |
Hal Finkel | e632239 | 2013-04-19 22:08:38 +0000 | [diff] [blame] | 1506 | assert((!equalityOnly || |
| 1507 | Pred == PPC::PRED_EQ || Pred == PPC::PRED_NE) && |
| 1508 | "Invalid predicate for equality-only optimization"); |
Owen Anderson | 16c6bf4 | 2014-03-13 23:12:04 +0000 | [diff] [blame] | 1509 | PredsToUpdate.push_back(std::make_pair(&(UseMI->getOperand(0)), |
Hal Finkel | 0f64e21 | 2013-04-20 05:16:26 +0000 | [diff] [blame] | 1510 | PPC::getSwappedPredicate(Pred))); |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1511 | } else if (UseMI->getOpcode() == PPC::ISEL || |
| 1512 | UseMI->getOpcode() == PPC::ISEL8) { |
Hal Finkel | e632239 | 2013-04-19 22:08:38 +0000 | [diff] [blame] | 1513 | unsigned NewSubReg = UseMI->getOperand(3).getSubReg(); |
| 1514 | assert((!equalityOnly || NewSubReg == PPC::sub_eq) && |
| 1515 | "Invalid CR bit for equality-only optimization"); |
| 1516 | |
| 1517 | if (NewSubReg == PPC::sub_lt) |
| 1518 | NewSubReg = PPC::sub_gt; |
| 1519 | else if (NewSubReg == PPC::sub_gt) |
| 1520 | NewSubReg = PPC::sub_lt; |
| 1521 | |
Owen Anderson | 16c6bf4 | 2014-03-13 23:12:04 +0000 | [diff] [blame] | 1522 | SubRegsToUpdate.push_back(std::make_pair(&(UseMI->getOperand(3)), |
Hal Finkel | e632239 | 2013-04-19 22:08:38 +0000 | [diff] [blame] | 1523 | NewSubReg)); |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1524 | } else // We need to abort on a user we don't understand. |
| 1525 | return false; |
| 1526 | } |
| 1527 | |
| 1528 | // Create a new virtual register to hold the value of the CR set by the |
| 1529 | // record-form instruction. If the instruction was not previously in |
| 1530 | // record form, then set the kill flag on the CR. |
| 1531 | CmpInstr->eraseFromParent(); |
| 1532 | |
| 1533 | MachineBasicBlock::iterator MII = MI; |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 1534 | BuildMI(*MI->getParent(), std::next(MII), MI->getDebugLoc(), |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1535 | get(TargetOpcode::COPY), CRReg) |
Hal Finkel | 08e53ee | 2013-05-08 12:16:14 +0000 | [diff] [blame] | 1536 | .addReg(PPC::CR0, MIOpC != NewOpC ? RegState::Kill : 0); |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1537 | |
| 1538 | if (MIOpC != NewOpC) { |
| 1539 | // We need to be careful here: we're replacing one instruction with |
| 1540 | // another, and we need to make sure that we get all of the right |
| 1541 | // implicit uses and defs. On the other hand, the caller may be holding |
| 1542 | // an iterator to this instruction, and so we can't delete it (this is |
| 1543 | // specifically the case if this is the instruction directly after the |
| 1544 | // compare). |
| 1545 | |
| 1546 | const MCInstrDesc &NewDesc = get(NewOpC); |
| 1547 | MI->setDesc(NewDesc); |
| 1548 | |
| 1549 | if (NewDesc.ImplicitDefs) |
| 1550 | for (const uint16_t *ImpDefs = NewDesc.getImplicitDefs(); |
| 1551 | *ImpDefs; ++ImpDefs) |
| 1552 | if (!MI->definesRegister(*ImpDefs)) |
| 1553 | MI->addOperand(*MI->getParent()->getParent(), |
| 1554 | MachineOperand::CreateReg(*ImpDefs, true, true)); |
| 1555 | if (NewDesc.ImplicitUses) |
| 1556 | for (const uint16_t *ImpUses = NewDesc.getImplicitUses(); |
| 1557 | *ImpUses; ++ImpUses) |
| 1558 | if (!MI->readsRegister(*ImpUses)) |
| 1559 | MI->addOperand(*MI->getParent()->getParent(), |
| 1560 | MachineOperand::CreateReg(*ImpUses, false, true)); |
| 1561 | } |
| 1562 | |
| 1563 | // Modify the condition code of operands in OperandsToUpdate. |
| 1564 | // Since we have SUB(r1, r2) and CMP(r2, r1), the condition code needs to |
| 1565 | // 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] | 1566 | for (unsigned i = 0, e = PredsToUpdate.size(); i < e; i++) |
| 1567 | PredsToUpdate[i].first->setImm(PredsToUpdate[i].second); |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1568 | |
Hal Finkel | e632239 | 2013-04-19 22:08:38 +0000 | [diff] [blame] | 1569 | for (unsigned i = 0, e = SubRegsToUpdate.size(); i < e; i++) |
| 1570 | SubRegsToUpdate[i].first->setSubReg(SubRegsToUpdate[i].second); |
Hal Finkel | 82656cb | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1571 | |
| 1572 | return true; |
| 1573 | } |
| 1574 | |
Nicolas Geoffray | ae84bbd | 2008-04-16 20:10:13 +0000 | [diff] [blame] | 1575 | /// GetInstSize - Return the number of bytes of code the specified |
| 1576 | /// instruction may be. This returns the maximum number of bytes. |
| 1577 | /// |
| 1578 | unsigned PPCInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const { |
Hal Finkel | a7bbaf6 | 2014-02-02 06:12:27 +0000 | [diff] [blame] | 1579 | unsigned Opcode = MI->getOpcode(); |
| 1580 | |
| 1581 | if (Opcode == PPC::INLINEASM) { |
Nicolas Geoffray | ae84bbd | 2008-04-16 20:10:13 +0000 | [diff] [blame] | 1582 | const MachineFunction *MF = MI->getParent()->getParent(); |
| 1583 | const char *AsmStr = MI->getOperand(0).getSymbolName(); |
Chris Lattner | 7b26fce | 2009-08-22 20:48:53 +0000 | [diff] [blame] | 1584 | return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo()); |
Hal Finkel | a7bbaf6 | 2014-02-02 06:12:27 +0000 | [diff] [blame] | 1585 | } else { |
| 1586 | const MCInstrDesc &Desc = get(Opcode); |
| 1587 | return Desc.getSize(); |
Nicolas Geoffray | ae84bbd | 2008-04-16 20:10:13 +0000 | [diff] [blame] | 1588 | } |
| 1589 | } |
Hal Finkel | b5aa7e5 | 2013-04-08 16:24:03 +0000 | [diff] [blame] | 1590 | |
Hal Finkel | 174e590 | 2014-03-25 23:29:21 +0000 | [diff] [blame] | 1591 | #undef DEBUG_TYPE |
| 1592 | #define DEBUG_TYPE "ppc-vsx-fma-mutate" |
| 1593 | |
| 1594 | namespace { |
| 1595 | // PPCVSXFMAMutate pass - For copies between VSX registers and non-VSX registers |
| 1596 | // (Altivec and scalar floating-point registers), we need to transform the |
| 1597 | // copies into subregister copies with other restrictions. |
| 1598 | struct PPCVSXFMAMutate : public MachineFunctionPass { |
| 1599 | static char ID; |
| 1600 | PPCVSXFMAMutate() : MachineFunctionPass(ID) { |
| 1601 | initializePPCVSXFMAMutatePass(*PassRegistry::getPassRegistry()); |
| 1602 | } |
| 1603 | |
| 1604 | LiveIntervals *LIS; |
| 1605 | |
| 1606 | const PPCTargetMachine *TM; |
| 1607 | const PPCInstrInfo *TII; |
| 1608 | |
| 1609 | protected: |
| 1610 | bool processBlock(MachineBasicBlock &MBB) { |
| 1611 | bool Changed = false; |
| 1612 | |
| 1613 | MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); |
| 1614 | for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end(); |
| 1615 | I != IE; ++I) { |
| 1616 | MachineInstr *MI = I; |
| 1617 | |
| 1618 | // The default (A-type) VSX FMA form kills the addend (it is taken from |
| 1619 | // the target register, which is then updated to reflect the result of |
| 1620 | // the FMA). If the instruction, however, kills one of the registers |
| 1621 | // used for the product, then we can use the M-form instruction (which |
| 1622 | // will take that value from the to-be-defined register). |
| 1623 | |
| 1624 | int AltOpc = PPC::getAltVSXFMAOpcode(MI->getOpcode()); |
| 1625 | if (AltOpc == -1) |
| 1626 | continue; |
| 1627 | |
| 1628 | // This pass is run after register coalescing, and so we're looking for |
| 1629 | // a situation like this: |
| 1630 | // ... |
| 1631 | // %vreg5<def> = COPY %vreg9; VSLRC:%vreg5,%vreg9 |
| 1632 | // %vreg5<def,tied1> = XSMADDADP %vreg5<tied0>, %vreg17, %vreg16, |
| 1633 | // %RM<imp-use>; VSLRC:%vreg5,%vreg17,%vreg16 |
| 1634 | // ... |
| 1635 | // %vreg9<def,tied1> = XSMADDADP %vreg9<tied0>, %vreg17, %vreg19, |
| 1636 | // %RM<imp-use>; VSLRC:%vreg9,%vreg17,%vreg19 |
| 1637 | // ... |
| 1638 | // Where we can eliminate the copy by changing from the A-type to the |
| 1639 | // M-type instruction. Specifically, for this example, this means: |
| 1640 | // %vreg5<def,tied1> = XSMADDADP %vreg5<tied0>, %vreg17, %vreg16, |
| 1641 | // %RM<imp-use>; VSLRC:%vreg5,%vreg17,%vreg16 |
| 1642 | // is replaced by: |
| 1643 | // %vreg16<def,tied1> = XSMADDMDP %vreg16<tied0>, %vreg18, %vreg9, |
| 1644 | // %RM<imp-use>; VSLRC:%vreg16,%vreg18,%vreg9 |
| 1645 | // and we remove: %vreg5<def> = COPY %vreg9; VSLRC:%vreg5,%vreg9 |
| 1646 | |
| 1647 | SlotIndex FMAIdx = LIS->getInstructionIndex(MI); |
| 1648 | |
| 1649 | VNInfo *AddendValNo = |
| 1650 | LIS->getInterval(MI->getOperand(1).getReg()).Query(FMAIdx).valueIn(); |
| 1651 | MachineInstr *AddendMI = LIS->getInstructionFromIndex(AddendValNo->def); |
| 1652 | |
| 1653 | // The addend and this instruction must be in the same block. |
| 1654 | |
Hal Finkel | 19be506 | 2014-03-29 05:29:01 +0000 | [diff] [blame] | 1655 | if (!AddendMI || AddendMI->getParent() != MI->getParent()) |
Hal Finkel | 174e590 | 2014-03-25 23:29:21 +0000 | [diff] [blame] | 1656 | continue; |
| 1657 | |
| 1658 | // The addend must be a full copy within the same register class. |
| 1659 | |
| 1660 | if (!AddendMI->isFullCopy()) |
| 1661 | continue; |
| 1662 | |
Hal Finkel | 19be506 | 2014-03-29 05:29:01 +0000 | [diff] [blame] | 1663 | unsigned AddendSrcReg = AddendMI->getOperand(1).getReg(); |
| 1664 | if (TargetRegisterInfo::isVirtualRegister(AddendSrcReg)) { |
| 1665 | if (MRI.getRegClass(AddendMI->getOperand(0).getReg()) != |
| 1666 | MRI.getRegClass(AddendSrcReg)) |
| 1667 | continue; |
| 1668 | } else { |
| 1669 | // If AddendSrcReg is a physical register, make sure the destination |
| 1670 | // register class contains it. |
| 1671 | if (!MRI.getRegClass(AddendMI->getOperand(0).getReg()) |
| 1672 | ->contains(AddendSrcReg)) |
| 1673 | continue; |
| 1674 | } |
Hal Finkel | 174e590 | 2014-03-25 23:29:21 +0000 | [diff] [blame] | 1675 | |
| 1676 | // In theory, there could be other uses of the addend copy before this |
| 1677 | // fma. We could deal with this, but that would require additional |
| 1678 | // logic below and I suspect it will not occur in any relevant |
| 1679 | // situations. |
| 1680 | bool OtherUsers = false; |
| 1681 | for (auto J = std::prev(I), JE = MachineBasicBlock::iterator(AddendMI); |
| 1682 | J != JE; --J) |
| 1683 | if (J->readsVirtualRegister(AddendMI->getOperand(0).getReg())) { |
| 1684 | OtherUsers = true; |
| 1685 | break; |
| 1686 | } |
| 1687 | |
| 1688 | if (OtherUsers) |
| 1689 | continue; |
| 1690 | |
| 1691 | // Find one of the product operands that is killed by this instruction. |
| 1692 | |
| 1693 | unsigned KilledProdOp = 0, OtherProdOp = 0; |
| 1694 | if (LIS->getInterval(MI->getOperand(2).getReg()) |
| 1695 | .Query(FMAIdx).isKill()) { |
| 1696 | KilledProdOp = 2; |
| 1697 | OtherProdOp = 3; |
| 1698 | } else if (LIS->getInterval(MI->getOperand(3).getReg()) |
| 1699 | .Query(FMAIdx).isKill()) { |
| 1700 | KilledProdOp = 3; |
| 1701 | OtherProdOp = 2; |
| 1702 | } |
| 1703 | |
Hal Finkel | 19be506 | 2014-03-29 05:29:01 +0000 | [diff] [blame] | 1704 | // If there are no killed product operands, then this transformation is |
| 1705 | // likely not profitable. |
Hal Finkel | 174e590 | 2014-03-25 23:29:21 +0000 | [diff] [blame] | 1706 | if (!KilledProdOp) |
| 1707 | continue; |
| 1708 | |
| 1709 | // In order to replace the addend here with the source of the copy, |
| 1710 | // it must still be live here. |
| 1711 | if (!LIS->getInterval(AddendMI->getOperand(1).getReg()).liveAt(FMAIdx)) |
| 1712 | continue; |
| 1713 | |
| 1714 | // Transform: (O2 * O3) + O1 -> (O2 * O1) + O3. |
| 1715 | |
| 1716 | unsigned AddReg = AddendMI->getOperand(1).getReg(); |
| 1717 | unsigned KilledProdReg = MI->getOperand(KilledProdOp).getReg(); |
| 1718 | unsigned OtherProdReg = MI->getOperand(OtherProdOp).getReg(); |
| 1719 | |
| 1720 | unsigned AddSubReg = AddendMI->getOperand(1).getSubReg(); |
| 1721 | unsigned KilledProdSubReg = MI->getOperand(KilledProdOp).getSubReg(); |
| 1722 | unsigned OtherProdSubReg = MI->getOperand(OtherProdOp).getSubReg(); |
| 1723 | |
| 1724 | bool AddRegKill = AddendMI->getOperand(1).isKill(); |
| 1725 | bool KilledProdRegKill = MI->getOperand(KilledProdOp).isKill(); |
| 1726 | bool OtherProdRegKill = MI->getOperand(OtherProdOp).isKill(); |
| 1727 | |
| 1728 | bool AddRegUndef = AddendMI->getOperand(1).isUndef(); |
| 1729 | bool KilledProdRegUndef = MI->getOperand(KilledProdOp).isUndef(); |
| 1730 | bool OtherProdRegUndef = MI->getOperand(OtherProdOp).isUndef(); |
| 1731 | |
| 1732 | unsigned OldFMAReg = MI->getOperand(0).getReg(); |
| 1733 | |
| 1734 | assert(OldFMAReg == AddendMI->getOperand(0).getReg() && |
| 1735 | "Addend copy not tied to old FMA output!"); |
| 1736 | |
| 1737 | DEBUG(dbgs() << "VSX FMA Mutation:\n " << *MI;); |
| 1738 | |
| 1739 | MI->getOperand(0).setReg(KilledProdReg); |
| 1740 | MI->getOperand(1).setReg(KilledProdReg); |
| 1741 | MI->getOperand(3).setReg(AddReg); |
| 1742 | MI->getOperand(2).setReg(OtherProdReg); |
| 1743 | |
| 1744 | MI->getOperand(0).setSubReg(KilledProdSubReg); |
| 1745 | MI->getOperand(1).setSubReg(KilledProdSubReg); |
| 1746 | MI->getOperand(3).setSubReg(AddSubReg); |
| 1747 | MI->getOperand(2).setSubReg(OtherProdSubReg); |
| 1748 | |
| 1749 | MI->getOperand(1).setIsKill(KilledProdRegKill); |
| 1750 | MI->getOperand(3).setIsKill(AddRegKill); |
| 1751 | MI->getOperand(2).setIsKill(OtherProdRegKill); |
| 1752 | |
| 1753 | MI->getOperand(1).setIsUndef(KilledProdRegUndef); |
| 1754 | MI->getOperand(3).setIsUndef(AddRegUndef); |
| 1755 | MI->getOperand(2).setIsUndef(OtherProdRegUndef); |
| 1756 | |
| 1757 | MI->setDesc(TII->get(AltOpc)); |
| 1758 | |
| 1759 | DEBUG(dbgs() << " -> " << *MI); |
| 1760 | |
| 1761 | // The killed product operand was killed here, so we can reuse it now |
| 1762 | // for the result of the fma. |
| 1763 | |
| 1764 | LiveInterval &FMAInt = LIS->getInterval(OldFMAReg); |
| 1765 | VNInfo *FMAValNo = FMAInt.getVNInfoAt(FMAIdx.getRegSlot()); |
| 1766 | for (auto UI = MRI.reg_nodbg_begin(OldFMAReg), UE = MRI.reg_nodbg_end(); |
| 1767 | UI != UE;) { |
| 1768 | MachineOperand &UseMO = *UI; |
| 1769 | MachineInstr *UseMI = UseMO.getParent(); |
| 1770 | ++UI; |
| 1771 | |
| 1772 | // Don't replace the result register of the copy we're about to erase. |
| 1773 | if (UseMI == AddendMI) |
| 1774 | continue; |
| 1775 | |
| 1776 | UseMO.setReg(KilledProdReg); |
| 1777 | UseMO.setSubReg(KilledProdSubReg); |
| 1778 | } |
| 1779 | |
| 1780 | // Extend the live intervals of the killed product operand to hold the |
| 1781 | // fma result. |
| 1782 | |
| 1783 | LiveInterval &NewFMAInt = LIS->getInterval(KilledProdReg); |
| 1784 | for (LiveInterval::iterator AI = FMAInt.begin(), AE = FMAInt.end(); |
| 1785 | AI != AE; ++AI) { |
| 1786 | // Don't add the segment that corresponds to the original copy. |
| 1787 | if (AI->valno == AddendValNo) |
| 1788 | continue; |
| 1789 | |
| 1790 | VNInfo *NewFMAValNo = |
| 1791 | NewFMAInt.getNextValue(AI->start, |
| 1792 | LIS->getVNInfoAllocator()); |
| 1793 | |
| 1794 | NewFMAInt.addSegment(LiveInterval::Segment(AI->start, AI->end, |
| 1795 | NewFMAValNo)); |
| 1796 | } |
| 1797 | DEBUG(dbgs() << " extended: " << NewFMAInt << '\n'); |
| 1798 | |
| 1799 | FMAInt.removeValNo(FMAValNo); |
| 1800 | DEBUG(dbgs() << " trimmed: " << FMAInt << '\n'); |
| 1801 | |
| 1802 | // Remove the (now unused) copy. |
| 1803 | |
| 1804 | DEBUG(dbgs() << " removing: " << *AddendMI << '\n'); |
| 1805 | LIS->RemoveMachineInstrFromMaps(AddendMI); |
| 1806 | AddendMI->eraseFromParent(); |
| 1807 | |
| 1808 | Changed = true; |
| 1809 | } |
| 1810 | |
| 1811 | return Changed; |
| 1812 | } |
| 1813 | |
| 1814 | public: |
| 1815 | virtual bool runOnMachineFunction(MachineFunction &MF) { |
| 1816 | LIS = &getAnalysis<LiveIntervals>(); |
| 1817 | |
| 1818 | TM = static_cast<const PPCTargetMachine *>(&MF.getTarget()); |
| 1819 | TII = TM->getInstrInfo(); |
| 1820 | |
| 1821 | bool Changed = false; |
| 1822 | |
| 1823 | if (DisableVSXFMAMutate) |
| 1824 | return Changed; |
| 1825 | |
| 1826 | for (MachineFunction::iterator I = MF.begin(); I != MF.end();) { |
| 1827 | MachineBasicBlock &B = *I++; |
| 1828 | if (processBlock(B)) |
| 1829 | Changed = true; |
| 1830 | } |
| 1831 | |
| 1832 | return Changed; |
| 1833 | } |
| 1834 | |
| 1835 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 1836 | AU.addRequired<LiveIntervals>(); |
| 1837 | AU.addPreserved<LiveIntervals>(); |
| 1838 | AU.addRequired<SlotIndexes>(); |
| 1839 | AU.addPreserved<SlotIndexes>(); |
| 1840 | MachineFunctionPass::getAnalysisUsage(AU); |
| 1841 | } |
| 1842 | }; |
| 1843 | } |
| 1844 | |
| 1845 | INITIALIZE_PASS_BEGIN(PPCVSXFMAMutate, DEBUG_TYPE, |
| 1846 | "PowerPC VSX FMA Mutation", false, false) |
| 1847 | INITIALIZE_PASS_DEPENDENCY(LiveIntervals) |
| 1848 | INITIALIZE_PASS_DEPENDENCY(SlotIndexes) |
| 1849 | INITIALIZE_PASS_END(PPCVSXFMAMutate, DEBUG_TYPE, |
| 1850 | "PowerPC VSX FMA Mutation", false, false) |
| 1851 | |
| 1852 | char &llvm::PPCVSXFMAMutateID = PPCVSXFMAMutate::ID; |
| 1853 | |
| 1854 | char PPCVSXFMAMutate::ID = 0; |
| 1855 | FunctionPass* |
| 1856 | llvm::createPPCVSXFMAMutatePass() { return new PPCVSXFMAMutate(); } |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 1857 | |
| 1858 | #undef DEBUG_TYPE |
| 1859 | #define DEBUG_TYPE "ppc-vsx-copy" |
| 1860 | |
| 1861 | namespace llvm { |
| 1862 | void initializePPCVSXCopyPass(PassRegistry&); |
| 1863 | } |
| 1864 | |
| 1865 | namespace { |
| 1866 | // PPCVSXCopy pass - For copies between VSX registers and non-VSX registers |
| 1867 | // (Altivec and scalar floating-point registers), we need to transform the |
| 1868 | // copies into subregister copies with other restrictions. |
| 1869 | struct PPCVSXCopy : public MachineFunctionPass { |
| 1870 | static char ID; |
| 1871 | PPCVSXCopy() : MachineFunctionPass(ID) { |
| 1872 | initializePPCVSXCopyPass(*PassRegistry::getPassRegistry()); |
| 1873 | } |
| 1874 | |
| 1875 | const PPCTargetMachine *TM; |
| 1876 | const PPCInstrInfo *TII; |
| 1877 | |
| 1878 | bool IsRegInClass(unsigned Reg, const TargetRegisterClass *RC, |
| 1879 | MachineRegisterInfo &MRI) { |
| 1880 | if (TargetRegisterInfo::isVirtualRegister(Reg)) { |
| 1881 | return RC->hasSubClassEq(MRI.getRegClass(Reg)); |
| 1882 | } else if (RC->contains(Reg)) { |
| 1883 | return true; |
| 1884 | } |
| 1885 | |
| 1886 | return false; |
| 1887 | } |
| 1888 | |
| 1889 | bool IsVSReg(unsigned Reg, MachineRegisterInfo &MRI) { |
| 1890 | return IsRegInClass(Reg, &PPC::VSRCRegClass, MRI); |
| 1891 | } |
| 1892 | |
| 1893 | bool IsVRReg(unsigned Reg, MachineRegisterInfo &MRI) { |
| 1894 | return IsRegInClass(Reg, &PPC::VRRCRegClass, MRI); |
| 1895 | } |
| 1896 | |
| 1897 | bool IsF8Reg(unsigned Reg, MachineRegisterInfo &MRI) { |
| 1898 | return IsRegInClass(Reg, &PPC::F8RCRegClass, MRI); |
| 1899 | } |
| 1900 | |
| 1901 | protected: |
| 1902 | bool processBlock(MachineBasicBlock &MBB) { |
| 1903 | bool Changed = false; |
| 1904 | |
| 1905 | MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); |
| 1906 | for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end(); |
| 1907 | I != IE; ++I) { |
| 1908 | MachineInstr *MI = I; |
| 1909 | if (!MI->isFullCopy()) |
| 1910 | continue; |
| 1911 | |
| 1912 | MachineOperand &DstMO = MI->getOperand(0); |
| 1913 | MachineOperand &SrcMO = MI->getOperand(1); |
| 1914 | |
| 1915 | if ( IsVSReg(DstMO.getReg(), MRI) && |
| 1916 | !IsVSReg(SrcMO.getReg(), MRI)) { |
| 1917 | // This is a copy *to* a VSX register from a non-VSX register. |
| 1918 | Changed = true; |
| 1919 | |
| 1920 | const TargetRegisterClass *SrcRC = |
| 1921 | IsVRReg(SrcMO.getReg(), MRI) ? &PPC::VSHRCRegClass : |
| 1922 | &PPC::VSLRCRegClass; |
| 1923 | assert((IsF8Reg(SrcMO.getReg(), MRI) || |
| 1924 | IsVRReg(SrcMO.getReg(), MRI)) && |
| 1925 | "Unknown source for a VSX copy"); |
| 1926 | |
| 1927 | unsigned NewVReg = MRI.createVirtualRegister(SrcRC); |
| 1928 | BuildMI(MBB, MI, MI->getDebugLoc(), |
| 1929 | TII->get(TargetOpcode::SUBREG_TO_REG), NewVReg) |
| 1930 | .addImm(1) // add 1, not 0, because there is no implicit clearing |
| 1931 | // of the high bits. |
| 1932 | .addOperand(SrcMO) |
| 1933 | .addImm(IsVRReg(SrcMO.getReg(), MRI) ? PPC::sub_128 : |
| 1934 | PPC::sub_64); |
| 1935 | |
| 1936 | // The source of the original copy is now the new virtual register. |
| 1937 | SrcMO.setReg(NewVReg); |
| 1938 | } else if (!IsVSReg(DstMO.getReg(), MRI) && |
| 1939 | IsVSReg(SrcMO.getReg(), MRI)) { |
| 1940 | // This is a copy *from* a VSX register to a non-VSX register. |
| 1941 | Changed = true; |
| 1942 | |
| 1943 | const TargetRegisterClass *DstRC = |
| 1944 | IsVRReg(DstMO.getReg(), MRI) ? &PPC::VSHRCRegClass : |
| 1945 | &PPC::VSLRCRegClass; |
| 1946 | assert((IsF8Reg(DstMO.getReg(), MRI) || |
| 1947 | IsVRReg(DstMO.getReg(), MRI)) && |
| 1948 | "Unknown destination for a VSX copy"); |
| 1949 | |
| 1950 | // Copy the VSX value into a new VSX register of the correct subclass. |
| 1951 | unsigned NewVReg = MRI.createVirtualRegister(DstRC); |
| 1952 | BuildMI(MBB, MI, MI->getDebugLoc(), |
| 1953 | TII->get(TargetOpcode::COPY), NewVReg) |
| 1954 | .addOperand(SrcMO); |
| 1955 | |
| 1956 | // Transform the original copy into a subregister extraction copy. |
| 1957 | SrcMO.setReg(NewVReg); |
| 1958 | SrcMO.setSubReg(IsVRReg(DstMO.getReg(), MRI) ? PPC::sub_128 : |
| 1959 | PPC::sub_64); |
| 1960 | } |
| 1961 | } |
| 1962 | |
| 1963 | return Changed; |
| 1964 | } |
| 1965 | |
| 1966 | public: |
| 1967 | virtual bool runOnMachineFunction(MachineFunction &MF) { |
| 1968 | TM = static_cast<const PPCTargetMachine *>(&MF.getTarget()); |
| 1969 | TII = TM->getInstrInfo(); |
| 1970 | |
| 1971 | bool Changed = false; |
| 1972 | |
| 1973 | for (MachineFunction::iterator I = MF.begin(); I != MF.end();) { |
| 1974 | MachineBasicBlock &B = *I++; |
| 1975 | if (processBlock(B)) |
| 1976 | Changed = true; |
| 1977 | } |
| 1978 | |
| 1979 | return Changed; |
| 1980 | } |
| 1981 | |
| 1982 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 1983 | MachineFunctionPass::getAnalysisUsage(AU); |
| 1984 | } |
| 1985 | }; |
| 1986 | } |
| 1987 | |
| 1988 | INITIALIZE_PASS(PPCVSXCopy, DEBUG_TYPE, |
| 1989 | "PowerPC VSX Copy Legalization", false, false) |
| 1990 | |
| 1991 | char PPCVSXCopy::ID = 0; |
| 1992 | FunctionPass* |
| 1993 | llvm::createPPCVSXCopyPass() { return new PPCVSXCopy(); } |
| 1994 | |
Hal Finkel | b5aa7e5 | 2013-04-08 16:24:03 +0000 | [diff] [blame] | 1995 | #undef DEBUG_TYPE |
Hal Finkel | c6fc9b8 | 2014-03-27 23:12:31 +0000 | [diff] [blame] | 1996 | #define DEBUG_TYPE "ppc-vsx-copy-cleanup" |
| 1997 | |
| 1998 | namespace llvm { |
| 1999 | void initializePPCVSXCopyCleanupPass(PassRegistry&); |
| 2000 | } |
| 2001 | |
| 2002 | namespace { |
| 2003 | // PPCVSXCopyCleanup pass - We sometimes end up generating self copies of VSX |
| 2004 | // registers (mostly because the ABI code still places all values into the |
| 2005 | // "traditional" floating-point and vector registers). Remove them here. |
| 2006 | struct PPCVSXCopyCleanup : public MachineFunctionPass { |
| 2007 | static char ID; |
| 2008 | PPCVSXCopyCleanup() : MachineFunctionPass(ID) { |
| 2009 | initializePPCVSXCopyCleanupPass(*PassRegistry::getPassRegistry()); |
| 2010 | } |
| 2011 | |
| 2012 | const PPCTargetMachine *TM; |
| 2013 | const PPCInstrInfo *TII; |
| 2014 | |
| 2015 | protected: |
| 2016 | bool processBlock(MachineBasicBlock &MBB) { |
| 2017 | bool Changed = false; |
| 2018 | |
| 2019 | SmallVector<MachineInstr *, 4> ToDelete; |
| 2020 | for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end(); |
| 2021 | I != IE; ++I) { |
| 2022 | MachineInstr *MI = I; |
| 2023 | if (MI->getOpcode() == PPC::XXLOR && |
| 2024 | MI->getOperand(0).getReg() == MI->getOperand(1).getReg() && |
| 2025 | MI->getOperand(0).getReg() == MI->getOperand(2).getReg()) |
| 2026 | ToDelete.push_back(MI); |
| 2027 | } |
| 2028 | |
| 2029 | if (!ToDelete.empty()) |
| 2030 | Changed = true; |
| 2031 | |
| 2032 | for (unsigned i = 0, ie = ToDelete.size(); i != ie; ++i) { |
| 2033 | DEBUG(dbgs() << "Removing VSX self-copy: " << *ToDelete[i]); |
| 2034 | ToDelete[i]->eraseFromParent(); |
| 2035 | } |
| 2036 | |
| 2037 | return Changed; |
| 2038 | } |
| 2039 | |
| 2040 | public: |
| 2041 | virtual bool runOnMachineFunction(MachineFunction &MF) { |
| 2042 | TM = static_cast<const PPCTargetMachine *>(&MF.getTarget()); |
| 2043 | TII = TM->getInstrInfo(); |
| 2044 | |
| 2045 | bool Changed = false; |
| 2046 | |
| 2047 | for (MachineFunction::iterator I = MF.begin(); I != MF.end();) { |
| 2048 | MachineBasicBlock &B = *I++; |
| 2049 | if (processBlock(B)) |
| 2050 | Changed = true; |
| 2051 | } |
| 2052 | |
| 2053 | return Changed; |
| 2054 | } |
| 2055 | |
| 2056 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 2057 | MachineFunctionPass::getAnalysisUsage(AU); |
| 2058 | } |
| 2059 | }; |
| 2060 | } |
| 2061 | |
| 2062 | INITIALIZE_PASS(PPCVSXCopyCleanup, DEBUG_TYPE, |
| 2063 | "PowerPC VSX Copy Cleanup", false, false) |
| 2064 | |
| 2065 | char PPCVSXCopyCleanup::ID = 0; |
| 2066 | FunctionPass* |
| 2067 | llvm::createPPCVSXCopyCleanupPass() { return new PPCVSXCopyCleanup(); } |
| 2068 | |
| 2069 | #undef DEBUG_TYPE |
Hal Finkel | b5aa7e5 | 2013-04-08 16:24:03 +0000 | [diff] [blame] | 2070 | #define DEBUG_TYPE "ppc-early-ret" |
| 2071 | STATISTIC(NumBCLR, "Number of early conditional returns"); |
| 2072 | STATISTIC(NumBLR, "Number of early returns"); |
| 2073 | |
| 2074 | namespace llvm { |
| 2075 | void initializePPCEarlyReturnPass(PassRegistry&); |
| 2076 | } |
| 2077 | |
| 2078 | namespace { |
| 2079 | // PPCEarlyReturn pass - For simple functions without epilogue code, move |
| 2080 | // returns up, and create conditional returns, to avoid unnecessary |
| 2081 | // branch-to-blr sequences. |
| 2082 | struct PPCEarlyReturn : public MachineFunctionPass { |
| 2083 | static char ID; |
| 2084 | PPCEarlyReturn() : MachineFunctionPass(ID) { |
| 2085 | initializePPCEarlyReturnPass(*PassRegistry::getPassRegistry()); |
| 2086 | } |
| 2087 | |
| 2088 | const PPCTargetMachine *TM; |
| 2089 | const PPCInstrInfo *TII; |
| 2090 | |
| 2091 | protected: |
Hal Finkel | 21aad9a | 2013-04-09 18:25:18 +0000 | [diff] [blame] | 2092 | bool processBlock(MachineBasicBlock &ReturnMBB) { |
Hal Finkel | b5aa7e5 | 2013-04-08 16:24:03 +0000 | [diff] [blame] | 2093 | bool Changed = false; |
| 2094 | |
Hal Finkel | 21aad9a | 2013-04-09 18:25:18 +0000 | [diff] [blame] | 2095 | MachineBasicBlock::iterator I = ReturnMBB.begin(); |
| 2096 | I = ReturnMBB.SkipPHIsAndLabels(I); |
Hal Finkel | b5aa7e5 | 2013-04-08 16:24:03 +0000 | [diff] [blame] | 2097 | |
| 2098 | // The block must be essentially empty except for the blr. |
Hal Finkel | 21aad9a | 2013-04-09 18:25:18 +0000 | [diff] [blame] | 2099 | if (I == ReturnMBB.end() || I->getOpcode() != PPC::BLR || |
| 2100 | I != ReturnMBB.getLastNonDebugInstr()) |
Hal Finkel | b5aa7e5 | 2013-04-08 16:24:03 +0000 | [diff] [blame] | 2101 | return Changed; |
| 2102 | |
| 2103 | SmallVector<MachineBasicBlock*, 8> PredToRemove; |
Hal Finkel | 21aad9a | 2013-04-09 18:25:18 +0000 | [diff] [blame] | 2104 | for (MachineBasicBlock::pred_iterator PI = ReturnMBB.pred_begin(), |
| 2105 | PIE = ReturnMBB.pred_end(); PI != PIE; ++PI) { |
Hal Finkel | b5aa7e5 | 2013-04-08 16:24:03 +0000 | [diff] [blame] | 2106 | bool OtherReference = false, BlockChanged = false; |
Hal Finkel | 21aad9a | 2013-04-09 18:25:18 +0000 | [diff] [blame] | 2107 | for (MachineBasicBlock::iterator J = (*PI)->getLastNonDebugInstr();;) { |
Hal Finkel | b5aa7e5 | 2013-04-08 16:24:03 +0000 | [diff] [blame] | 2108 | if (J->getOpcode() == PPC::B) { |
Hal Finkel | 21aad9a | 2013-04-09 18:25:18 +0000 | [diff] [blame] | 2109 | if (J->getOperand(0).getMBB() == &ReturnMBB) { |
Hal Finkel | b5aa7e5 | 2013-04-08 16:24:03 +0000 | [diff] [blame] | 2110 | // This is an unconditional branch to the return. Replace the |
Andrew Trick | 9defbd8 | 2013-12-17 04:50:40 +0000 | [diff] [blame] | 2111 | // branch with a blr. |
Hal Finkel | b5aa7e5 | 2013-04-08 16:24:03 +0000 | [diff] [blame] | 2112 | BuildMI(**PI, J, J->getDebugLoc(), TII->get(PPC::BLR)); |
Hal Finkel | 21aad9a | 2013-04-09 18:25:18 +0000 | [diff] [blame] | 2113 | MachineBasicBlock::iterator K = J--; |
Hal Finkel | b5aa7e5 | 2013-04-08 16:24:03 +0000 | [diff] [blame] | 2114 | K->eraseFromParent(); |
| 2115 | BlockChanged = true; |
| 2116 | ++NumBLR; |
| 2117 | continue; |
| 2118 | } |
| 2119 | } else if (J->getOpcode() == PPC::BCC) { |
Hal Finkel | 21aad9a | 2013-04-09 18:25:18 +0000 | [diff] [blame] | 2120 | if (J->getOperand(2).getMBB() == &ReturnMBB) { |
Hal Finkel | b5aa7e5 | 2013-04-08 16:24:03 +0000 | [diff] [blame] | 2121 | // This is a conditional branch to the return. Replace the branch |
| 2122 | // with a bclr. |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 2123 | BuildMI(**PI, J, J->getDebugLoc(), TII->get(PPC::BCCLR)) |
Hal Finkel | b5aa7e5 | 2013-04-08 16:24:03 +0000 | [diff] [blame] | 2124 | .addImm(J->getOperand(0).getImm()) |
| 2125 | .addReg(J->getOperand(1).getReg()); |
Hal Finkel | 21aad9a | 2013-04-09 18:25:18 +0000 | [diff] [blame] | 2126 | MachineBasicBlock::iterator K = J--; |
Hal Finkel | b5aa7e5 | 2013-04-08 16:24:03 +0000 | [diff] [blame] | 2127 | K->eraseFromParent(); |
| 2128 | BlockChanged = true; |
| 2129 | ++NumBCLR; |
| 2130 | continue; |
| 2131 | } |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 2132 | } else if (J->getOpcode() == PPC::BC || J->getOpcode() == PPC::BCn) { |
| 2133 | if (J->getOperand(1).getMBB() == &ReturnMBB) { |
| 2134 | // This is a conditional branch to the return. Replace the branch |
| 2135 | // with a bclr. |
| 2136 | BuildMI(**PI, J, J->getDebugLoc(), |
| 2137 | TII->get(J->getOpcode() == PPC::BC ? |
| 2138 | PPC::BCLR : PPC::BCLRn)) |
| 2139 | .addReg(J->getOperand(0).getReg()); |
| 2140 | MachineBasicBlock::iterator K = J--; |
| 2141 | K->eraseFromParent(); |
| 2142 | BlockChanged = true; |
| 2143 | ++NumBCLR; |
| 2144 | continue; |
| 2145 | } |
Hal Finkel | b5aa7e5 | 2013-04-08 16:24:03 +0000 | [diff] [blame] | 2146 | } else if (J->isBranch()) { |
| 2147 | if (J->isIndirectBranch()) { |
Hal Finkel | 21aad9a | 2013-04-09 18:25:18 +0000 | [diff] [blame] | 2148 | if (ReturnMBB.hasAddressTaken()) |
Hal Finkel | b5aa7e5 | 2013-04-08 16:24:03 +0000 | [diff] [blame] | 2149 | OtherReference = true; |
| 2150 | } else |
| 2151 | for (unsigned i = 0; i < J->getNumOperands(); ++i) |
| 2152 | if (J->getOperand(i).isMBB() && |
Hal Finkel | 21aad9a | 2013-04-09 18:25:18 +0000 | [diff] [blame] | 2153 | J->getOperand(i).getMBB() == &ReturnMBB) |
Hal Finkel | b5aa7e5 | 2013-04-08 16:24:03 +0000 | [diff] [blame] | 2154 | OtherReference = true; |
Hal Finkel | 21aad9a | 2013-04-09 18:25:18 +0000 | [diff] [blame] | 2155 | } else if (!J->isTerminator() && !J->isDebugValue()) |
| 2156 | break; |
Hal Finkel | b5aa7e5 | 2013-04-08 16:24:03 +0000 | [diff] [blame] | 2157 | |
Hal Finkel | 21aad9a | 2013-04-09 18:25:18 +0000 | [diff] [blame] | 2158 | if (J == (*PI)->begin()) |
| 2159 | break; |
| 2160 | |
| 2161 | --J; |
Hal Finkel | b5aa7e5 | 2013-04-08 16:24:03 +0000 | [diff] [blame] | 2162 | } |
| 2163 | |
Hal Finkel | 21aad9a | 2013-04-09 18:25:18 +0000 | [diff] [blame] | 2164 | if ((*PI)->canFallThrough() && (*PI)->isLayoutSuccessor(&ReturnMBB)) |
Hal Finkel | b5aa7e5 | 2013-04-08 16:24:03 +0000 | [diff] [blame] | 2165 | OtherReference = true; |
| 2166 | |
Andrew Trick | 9defbd8 | 2013-12-17 04:50:40 +0000 | [diff] [blame] | 2167 | // Predecessors are stored in a vector and can't be removed here. |
Hal Finkel | b5aa7e5 | 2013-04-08 16:24:03 +0000 | [diff] [blame] | 2168 | if (!OtherReference && BlockChanged) { |
| 2169 | PredToRemove.push_back(*PI); |
| 2170 | } |
| 2171 | |
| 2172 | if (BlockChanged) |
| 2173 | Changed = true; |
| 2174 | } |
| 2175 | |
| 2176 | for (unsigned i = 0, ie = PredToRemove.size(); i != ie; ++i) |
Hal Finkel | 21aad9a | 2013-04-09 18:25:18 +0000 | [diff] [blame] | 2177 | PredToRemove[i]->removeSuccessor(&ReturnMBB); |
Hal Finkel | b5aa7e5 | 2013-04-08 16:24:03 +0000 | [diff] [blame] | 2178 | |
Hal Finkel | 21aad9a | 2013-04-09 18:25:18 +0000 | [diff] [blame] | 2179 | if (Changed && !ReturnMBB.hasAddressTaken()) { |
Hal Finkel | b5aa7e5 | 2013-04-08 16:24:03 +0000 | [diff] [blame] | 2180 | // We now might be able to merge this blr-only block into its |
| 2181 | // by-layout predecessor. |
Hal Finkel | 21aad9a | 2013-04-09 18:25:18 +0000 | [diff] [blame] | 2182 | if (ReturnMBB.pred_size() == 1 && |
| 2183 | (*ReturnMBB.pred_begin())->isLayoutSuccessor(&ReturnMBB)) { |
Hal Finkel | b5aa7e5 | 2013-04-08 16:24:03 +0000 | [diff] [blame] | 2184 | // Move the blr into the preceding block. |
Hal Finkel | 21aad9a | 2013-04-09 18:25:18 +0000 | [diff] [blame] | 2185 | MachineBasicBlock &PrevMBB = **ReturnMBB.pred_begin(); |
| 2186 | PrevMBB.splice(PrevMBB.end(), &ReturnMBB, I); |
| 2187 | PrevMBB.removeSuccessor(&ReturnMBB); |
Hal Finkel | b5aa7e5 | 2013-04-08 16:24:03 +0000 | [diff] [blame] | 2188 | } |
| 2189 | |
Hal Finkel | 21aad9a | 2013-04-09 18:25:18 +0000 | [diff] [blame] | 2190 | if (ReturnMBB.pred_empty()) |
| 2191 | ReturnMBB.eraseFromParent(); |
Hal Finkel | b5aa7e5 | 2013-04-08 16:24:03 +0000 | [diff] [blame] | 2192 | } |
| 2193 | |
| 2194 | return Changed; |
| 2195 | } |
| 2196 | |
| 2197 | public: |
| 2198 | virtual bool runOnMachineFunction(MachineFunction &MF) { |
| 2199 | TM = static_cast<const PPCTargetMachine *>(&MF.getTarget()); |
| 2200 | TII = TM->getInstrInfo(); |
| 2201 | |
| 2202 | bool Changed = false; |
| 2203 | |
Hal Finkel | 21aad9a | 2013-04-09 18:25:18 +0000 | [diff] [blame] | 2204 | // If the function does not have at least two blocks, then there is |
Hal Finkel | b5aa7e5 | 2013-04-08 16:24:03 +0000 | [diff] [blame] | 2205 | // nothing to do. |
| 2206 | if (MF.size() < 2) |
| 2207 | return Changed; |
| 2208 | |
| 2209 | for (MachineFunction::iterator I = MF.begin(); I != MF.end();) { |
Andrew Trick | 9defbd8 | 2013-12-17 04:50:40 +0000 | [diff] [blame] | 2210 | MachineBasicBlock &B = *I++; |
Hal Finkel | b5aa7e5 | 2013-04-08 16:24:03 +0000 | [diff] [blame] | 2211 | if (processBlock(B)) |
| 2212 | Changed = true; |
| 2213 | } |
| 2214 | |
| 2215 | return Changed; |
| 2216 | } |
| 2217 | |
| 2218 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 2219 | MachineFunctionPass::getAnalysisUsage(AU); |
| 2220 | } |
| 2221 | }; |
| 2222 | } |
| 2223 | |
| 2224 | INITIALIZE_PASS(PPCEarlyReturn, DEBUG_TYPE, |
| 2225 | "PowerPC Early-Return Creation", false, false) |
| 2226 | |
| 2227 | char PPCEarlyReturn::ID = 0; |
| 2228 | FunctionPass* |
| 2229 | llvm::createPPCEarlyReturnPass() { return new PPCEarlyReturn(); } |