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