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