Jia Liu | 31d157a | 2012-02-18 12:03:15 +0000 | [diff] [blame] | 1 | //===-- PPCInstrInfo.cpp - PowerPC Instruction Information ----------------===// |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 2 | // |
Misha Brukman | f2ccb77 | 2004-08-17 04:55:41 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 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 | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 7 | // |
Misha Brukman | f2ccb77 | 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 | 16e71f2 | 2005-10-14 23:59:06 +0000 | [diff] [blame] | 14 | #include "PPCInstrInfo.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 15 | #include "MCTargetDesc/PPCPredicates.h" |
Evan Cheng | 59ee62d | 2011-07-11 03:57:24 +0000 | [diff] [blame] | 16 | #include "PPC.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 17 | #include "PPCHazardRecognizers.h" |
Owen Anderson | f6372aa | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 18 | #include "PPCInstrBuilder.h" |
Bill Wendling | 7194aaf | 2008-03-03 22:19:16 +0000 | [diff] [blame] | 19 | #include "PPCMachineFunctionInfo.h" |
Chris Lattner | b1d26f6 | 2006-06-17 00:01:04 +0000 | [diff] [blame] | 20 | #include "PPCTargetMachine.h" |
Hal Finkel | 5ee67e8 | 2013-04-08 16:24:03 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/Statistic.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/STLExtras.h" |
Jakob Stoklund Olesen | 7a79fcb | 2010-07-16 18:22:00 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Hal Finkel | 5ee67e8 | 2013-04-08 16:24:03 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Misha Brukman | f2ccb77 | 2004-08-17 04:55:41 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Jakob Stoklund Olesen | 7a79fcb | 2010-07-16 18:22:00 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/MachineMemOperand.h" |
Jakob Stoklund Olesen | 2432966 | 2010-02-26 21:09:24 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Hal Finkel | 4d989ac | 2012-04-01 19:22:40 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/PseudoSourceValue.h" |
Evan Cheng | 59ee62d | 2011-07-11 03:57:24 +0000 | [diff] [blame] | 29 | #include "llvm/MC/MCAsmInfo.h" |
Bill Wendling | 880d0f6 | 2008-03-04 23:13:51 +0000 | [diff] [blame] | 30 | #include "llvm/Support/CommandLine.h" |
Torok Edwin | dac237e | 2009-07-08 20:53:28 +0000 | [diff] [blame] | 31 | #include "llvm/Support/ErrorHandling.h" |
Evan Cheng | 3e74d6f | 2011-08-24 18:08:43 +0000 | [diff] [blame] | 32 | #include "llvm/Support/TargetRegistry.h" |
Torok Edwin | dac237e | 2009-07-08 20:53:28 +0000 | [diff] [blame] | 33 | #include "llvm/Support/raw_ostream.h" |
Misha Brukman | f2ccb77 | 2004-08-17 04:55:41 +0000 | [diff] [blame] | 34 | |
Hal Finkel | 860c08c | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 35 | #define GET_INSTRMAP_INFO |
Evan Cheng | 4db3cff | 2011-07-01 17:57:27 +0000 | [diff] [blame] | 36 | #define GET_INSTRINFO_CTOR |
Evan Cheng | 22fee2d | 2011-06-28 20:07:07 +0000 | [diff] [blame] | 37 | #include "PPCGenInstrInfo.inc" |
| 38 | |
Dan Gohman | 82bcd23 | 2010-04-15 17:20:57 +0000 | [diff] [blame] | 39 | using namespace llvm; |
Bill Wendling | 880d0f6 | 2008-03-04 23:13:51 +0000 | [diff] [blame] | 40 | |
Hal Finkel | 09fdc7b | 2012-06-08 15:38:25 +0000 | [diff] [blame] | 41 | static cl:: |
Hal Finkel | 7255d2a | 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 | 09fdc7b | 2012-06-08 15:38:25 +0000 | [diff] [blame] | 44 | |
Chris Lattner | b1d26f6 | 2006-06-17 00:01:04 +0000 | [diff] [blame] | 45 | PPCInstrInfo::PPCInstrInfo(PPCTargetMachine &tm) |
Evan Cheng | 4db3cff | 2011-07-01 17:57:27 +0000 | [diff] [blame] | 46 | : PPCGenInstrInfo(PPC::ADJCALLSTACKDOWN, PPC::ADJCALLSTACKUP), |
Evan Cheng | d5b03f2 | 2011-06-28 21:14:33 +0000 | [diff] [blame] | 47 | TM(tm), RI(*TM.getSubtargetImpl(), *this) {} |
Chris Lattner | b1d26f6 | 2006-06-17 00:01:04 +0000 | [diff] [blame] | 48 | |
Andrew Trick | 2da8bc8 | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 49 | /// CreateTargetHazardRecognizer - Return the hazard recognizer to use for |
| 50 | /// this target when scheduling the DAG. |
| 51 | ScheduleHazardRecognizer *PPCInstrInfo::CreateTargetHazardRecognizer( |
| 52 | const TargetMachine *TM, |
| 53 | const ScheduleDAG *DAG) const { |
Hal Finkel | c6d08f1 | 2011-10-17 04:03:49 +0000 | [diff] [blame] | 54 | unsigned Directive = TM->getSubtarget<PPCSubtarget>().getDarwinDirective(); |
Hal Finkel | 621b77a | 2012-08-28 16:12:39 +0000 | [diff] [blame] | 55 | if (Directive == PPC::DIR_440 || Directive == PPC::DIR_A2 || |
| 56 | Directive == PPC::DIR_E500mc || Directive == PPC::DIR_E5500) { |
Hal Finkel | 768c65f | 2011-11-22 16:21:04 +0000 | [diff] [blame] | 57 | const InstrItineraryData *II = TM->getInstrItineraryData(); |
Hal Finkel | 5b00cea | 2012-03-31 14:45:15 +0000 | [diff] [blame] | 58 | return new PPCScoreboardHazardRecognizer(II, DAG); |
Hal Finkel | c6d08f1 | 2011-10-17 04:03:49 +0000 | [diff] [blame] | 59 | } |
Hal Finkel | 64c34e2 | 2011-12-02 04:58:02 +0000 | [diff] [blame] | 60 | |
Jakob Stoklund Olesen | a9fa4fd | 2012-11-28 02:35:17 +0000 | [diff] [blame] | 61 | return TargetInstrInfo::CreateTargetHazardRecognizer(TM, DAG); |
Andrew Trick | 2da8bc8 | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Hal Finkel | 64c34e2 | 2011-12-02 04:58:02 +0000 | [diff] [blame] | 64 | /// CreateTargetPostRAHazardRecognizer - Return the postRA hazard recognizer |
| 65 | /// to use for this target when scheduling the DAG. |
| 66 | ScheduleHazardRecognizer *PPCInstrInfo::CreateTargetPostRAHazardRecognizer( |
| 67 | const InstrItineraryData *II, |
| 68 | const ScheduleDAG *DAG) const { |
| 69 | unsigned Directive = TM.getSubtarget<PPCSubtarget>().getDarwinDirective(); |
| 70 | |
| 71 | // Most subtargets use a PPC970 recognizer. |
Hal Finkel | 621b77a | 2012-08-28 16:12:39 +0000 | [diff] [blame] | 72 | if (Directive != PPC::DIR_440 && Directive != PPC::DIR_A2 && |
| 73 | Directive != PPC::DIR_E500mc && Directive != PPC::DIR_E5500) { |
Hal Finkel | 64c34e2 | 2011-12-02 04:58:02 +0000 | [diff] [blame] | 74 | const TargetInstrInfo *TII = TM.getInstrInfo(); |
| 75 | assert(TII && "No InstrInfo?"); |
| 76 | |
| 77 | return new PPCHazardRecognizer970(*TII); |
| 78 | } |
| 79 | |
Hal Finkel | 4d989ac | 2012-04-01 19:22:40 +0000 | [diff] [blame] | 80 | return new PPCScoreboardHazardRecognizer(II, DAG); |
Hal Finkel | 64c34e2 | 2011-12-02 04:58:02 +0000 | [diff] [blame] | 81 | } |
Jakob Stoklund Olesen | 7164288 | 2012-06-19 21:14:34 +0000 | [diff] [blame] | 82 | |
| 83 | // Detect 32 -> 64-bit extensions where we may reuse the low sub-register. |
| 84 | bool PPCInstrInfo::isCoalescableExtInstr(const MachineInstr &MI, |
| 85 | unsigned &SrcReg, unsigned &DstReg, |
| 86 | unsigned &SubIdx) const { |
| 87 | switch (MI.getOpcode()) { |
| 88 | default: return false; |
| 89 | case PPC::EXTSW: |
| 90 | case PPC::EXTSW_32_64: |
| 91 | SrcReg = MI.getOperand(1).getReg(); |
| 92 | DstReg = MI.getOperand(0).getReg(); |
| 93 | SubIdx = PPC::sub_32; |
| 94 | return true; |
| 95 | } |
| 96 | } |
| 97 | |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 98 | unsigned PPCInstrInfo::isLoadFromStackSlot(const MachineInstr *MI, |
Chris Lattner | 9c09c9e | 2006-03-16 22:24:02 +0000 | [diff] [blame] | 99 | int &FrameIndex) const { |
Hal Finkel | f25f93b | 2013-03-27 21:21:15 +0000 | [diff] [blame] | 100 | // Note: This list must be kept consistent with LoadRegFromStackSlot. |
Chris Lattner | 4083960 | 2006-02-02 20:12:32 +0000 | [diff] [blame] | 101 | switch (MI->getOpcode()) { |
| 102 | default: break; |
| 103 | case PPC::LD: |
| 104 | case PPC::LWZ: |
| 105 | case PPC::LFS: |
| 106 | case PPC::LFD: |
Hal Finkel | f25f93b | 2013-03-27 21:21:15 +0000 | [diff] [blame] | 107 | case PPC::RESTORE_CR: |
| 108 | case PPC::LVX: |
| 109 | case PPC::RESTORE_VRSAVE: |
| 110 | // Check for the operands added by addFrameReference (the immediate is the |
| 111 | // offset which defaults to 0). |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 112 | if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() && |
| 113 | MI->getOperand(2).isFI()) { |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 114 | FrameIndex = MI->getOperand(2).getIndex(); |
Chris Lattner | 4083960 | 2006-02-02 20:12:32 +0000 | [diff] [blame] | 115 | return MI->getOperand(0).getReg(); |
| 116 | } |
| 117 | break; |
| 118 | } |
| 119 | return 0; |
Chris Lattner | 6524287 | 2006-02-02 20:16:12 +0000 | [diff] [blame] | 120 | } |
Chris Lattner | 4083960 | 2006-02-02 20:12:32 +0000 | [diff] [blame] | 121 | |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 122 | unsigned PPCInstrInfo::isStoreToStackSlot(const MachineInstr *MI, |
Chris Lattner | 6524287 | 2006-02-02 20:16:12 +0000 | [diff] [blame] | 123 | int &FrameIndex) const { |
Hal Finkel | f25f93b | 2013-03-27 21:21:15 +0000 | [diff] [blame] | 124 | // Note: This list must be kept consistent with StoreRegToStackSlot. |
Chris Lattner | 6524287 | 2006-02-02 20:16:12 +0000 | [diff] [blame] | 125 | switch (MI->getOpcode()) { |
| 126 | default: break; |
Nate Begeman | 3b478b3 | 2006-02-02 21:07:50 +0000 | [diff] [blame] | 127 | case PPC::STD: |
Chris Lattner | 6524287 | 2006-02-02 20:16:12 +0000 | [diff] [blame] | 128 | case PPC::STW: |
| 129 | case PPC::STFS: |
| 130 | case PPC::STFD: |
Hal Finkel | f25f93b | 2013-03-27 21:21:15 +0000 | [diff] [blame] | 131 | case PPC::SPILL_CR: |
| 132 | case PPC::STVX: |
| 133 | case PPC::SPILL_VRSAVE: |
| 134 | // Check for the operands added by addFrameReference (the immediate is the |
| 135 | // offset which defaults to 0). |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 136 | if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() && |
| 137 | MI->getOperand(2).isFI()) { |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 138 | FrameIndex = MI->getOperand(2).getIndex(); |
Chris Lattner | 6524287 | 2006-02-02 20:16:12 +0000 | [diff] [blame] | 139 | return MI->getOperand(0).getReg(); |
| 140 | } |
| 141 | break; |
| 142 | } |
| 143 | return 0; |
| 144 | } |
Chris Lattner | 4083960 | 2006-02-02 20:12:32 +0000 | [diff] [blame] | 145 | |
Chris Lattner | 043870d | 2005-09-09 18:17:41 +0000 | [diff] [blame] | 146 | // commuteInstruction - We can commute rlwimi instructions, but only if the |
| 147 | // rotate amt is zero. We also have to munge the immediates a bit. |
Evan Cheng | 58dcb0e | 2008-06-16 07:33:11 +0000 | [diff] [blame] | 148 | MachineInstr * |
| 149 | PPCInstrInfo::commuteInstruction(MachineInstr *MI, bool NewMI) const { |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 150 | MachineFunction &MF = *MI->getParent()->getParent(); |
| 151 | |
Chris Lattner | 043870d | 2005-09-09 18:17:41 +0000 | [diff] [blame] | 152 | // Normal instructions can be commuted the obvious way. |
Hal Finkel | 171a8ad | 2013-04-12 02:18:09 +0000 | [diff] [blame] | 153 | if (MI->getOpcode() != PPC::RLWIMI && |
| 154 | MI->getOpcode() != PPC::RLWIMIo) |
Jakob Stoklund Olesen | a9fa4fd | 2012-11-28 02:35:17 +0000 | [diff] [blame] | 155 | return TargetInstrInfo::commuteInstruction(MI, NewMI); |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 156 | |
Chris Lattner | 043870d | 2005-09-09 18:17:41 +0000 | [diff] [blame] | 157 | // Cannot commute if it has a non-zero rotate count. |
Chris Lattner | 9a1ceae | 2007-12-30 20:49:49 +0000 | [diff] [blame] | 158 | if (MI->getOperand(3).getImm() != 0) |
Chris Lattner | 043870d | 2005-09-09 18:17:41 +0000 | [diff] [blame] | 159 | return 0; |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 160 | |
Chris Lattner | 043870d | 2005-09-09 18:17:41 +0000 | [diff] [blame] | 161 | // If we have a zero rotate count, we have: |
| 162 | // M = mask(MB,ME) |
| 163 | // Op0 = (Op1 & ~M) | (Op2 & M) |
| 164 | // Change this to: |
| 165 | // M = mask((ME+1)&31, (MB-1)&31) |
| 166 | // Op0 = (Op2 & ~M) | (Op1 & M) |
| 167 | |
| 168 | // Swap op1/op2 |
Evan Cheng | a4d16a1 | 2008-02-13 02:46:49 +0000 | [diff] [blame] | 169 | unsigned Reg0 = MI->getOperand(0).getReg(); |
Chris Lattner | 043870d | 2005-09-09 18:17:41 +0000 | [diff] [blame] | 170 | unsigned Reg1 = MI->getOperand(1).getReg(); |
| 171 | unsigned Reg2 = MI->getOperand(2).getReg(); |
Evan Cheng | 6ce7dc2 | 2006-11-15 20:58:11 +0000 | [diff] [blame] | 172 | bool Reg1IsKill = MI->getOperand(1).isKill(); |
| 173 | bool Reg2IsKill = MI->getOperand(2).isKill(); |
Evan Cheng | 58dcb0e | 2008-06-16 07:33:11 +0000 | [diff] [blame] | 174 | bool ChangeReg0 = false; |
Evan Cheng | a4d16a1 | 2008-02-13 02:46:49 +0000 | [diff] [blame] | 175 | // If machine instrs are no longer in two-address forms, update |
| 176 | // destination register as well. |
| 177 | if (Reg0 == Reg1) { |
| 178 | // Must be two address instruction! |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 179 | assert(MI->getDesc().getOperandConstraint(0, MCOI::TIED_TO) && |
Evan Cheng | a4d16a1 | 2008-02-13 02:46:49 +0000 | [diff] [blame] | 180 | "Expecting a two-address instruction!"); |
Evan Cheng | a4d16a1 | 2008-02-13 02:46:49 +0000 | [diff] [blame] | 181 | Reg2IsKill = false; |
Evan Cheng | 58dcb0e | 2008-06-16 07:33:11 +0000 | [diff] [blame] | 182 | ChangeReg0 = true; |
Evan Cheng | a4d16a1 | 2008-02-13 02:46:49 +0000 | [diff] [blame] | 183 | } |
Evan Cheng | 58dcb0e | 2008-06-16 07:33:11 +0000 | [diff] [blame] | 184 | |
| 185 | // Masks. |
| 186 | unsigned MB = MI->getOperand(4).getImm(); |
| 187 | unsigned ME = MI->getOperand(5).getImm(); |
| 188 | |
| 189 | if (NewMI) { |
| 190 | // Create a new instruction. |
| 191 | unsigned Reg0 = ChangeReg0 ? Reg2 : MI->getOperand(0).getReg(); |
| 192 | bool Reg0IsDead = MI->getOperand(0).isDead(); |
Bill Wendling | d1c321a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 193 | return BuildMI(MF, MI->getDebugLoc(), MI->getDesc()) |
Bill Wendling | 587daed | 2009-05-13 21:33:08 +0000 | [diff] [blame] | 194 | .addReg(Reg0, RegState::Define | getDeadRegState(Reg0IsDead)) |
| 195 | .addReg(Reg2, getKillRegState(Reg2IsKill)) |
| 196 | .addReg(Reg1, getKillRegState(Reg1IsKill)) |
Evan Cheng | 58dcb0e | 2008-06-16 07:33:11 +0000 | [diff] [blame] | 197 | .addImm((ME+1) & 31) |
| 198 | .addImm((MB-1) & 31); |
| 199 | } |
| 200 | |
| 201 | if (ChangeReg0) |
| 202 | MI->getOperand(0).setReg(Reg2); |
Chris Lattner | e53f4a0 | 2006-05-04 17:52:23 +0000 | [diff] [blame] | 203 | MI->getOperand(2).setReg(Reg1); |
| 204 | MI->getOperand(1).setReg(Reg2); |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 205 | MI->getOperand(2).setIsKill(Reg1IsKill); |
| 206 | MI->getOperand(1).setIsKill(Reg2IsKill); |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 207 | |
Chris Lattner | 043870d | 2005-09-09 18:17:41 +0000 | [diff] [blame] | 208 | // Swap the mask around. |
Chris Lattner | 9a1ceae | 2007-12-30 20:49:49 +0000 | [diff] [blame] | 209 | MI->getOperand(4).setImm((ME+1) & 31); |
| 210 | MI->getOperand(5).setImm((MB-1) & 31); |
Chris Lattner | 043870d | 2005-09-09 18:17:41 +0000 | [diff] [blame] | 211 | return MI; |
| 212 | } |
Chris Lattner | bbf1c72 | 2006-03-05 23:49:55 +0000 | [diff] [blame] | 213 | |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 214 | void PPCInstrInfo::insertNoop(MachineBasicBlock &MBB, |
Chris Lattner | bbf1c72 | 2006-03-05 23:49:55 +0000 | [diff] [blame] | 215 | MachineBasicBlock::iterator MI) const { |
Chris Lattner | c7f3ace | 2010-04-02 20:16:16 +0000 | [diff] [blame] | 216 | DebugLoc DL; |
Bill Wendling | d1c321a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 217 | BuildMI(MBB, MI, DL, get(PPC::NOP)); |
Chris Lattner | bbf1c72 | 2006-03-05 23:49:55 +0000 | [diff] [blame] | 218 | } |
Chris Lattner | c50e2bc | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 219 | |
| 220 | |
| 221 | // Branch analysis. |
Hal Finkel | 99f823f | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 222 | // Note: If the condition register is set to CTR or CTR8 then this is a |
| 223 | // BDNZ (imm == 1) or BDZ (imm == 0) branch. |
Chris Lattner | c50e2bc | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 224 | bool PPCInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB, |
| 225 | MachineBasicBlock *&FBB, |
Evan Cheng | dc54d31 | 2009-02-09 07:14:22 +0000 | [diff] [blame] | 226 | SmallVectorImpl<MachineOperand> &Cond, |
| 227 | bool AllowModify) const { |
Hal Finkel | 99f823f | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 228 | bool isPPC64 = TM.getSubtargetImpl()->isPPC64(); |
| 229 | |
Chris Lattner | c50e2bc | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 230 | // If the block has no terminators, it just falls into the block after it. |
| 231 | MachineBasicBlock::iterator I = MBB.end(); |
Dale Johannesen | 93d6a7e | 2010-04-02 01:38:09 +0000 | [diff] [blame] | 232 | if (I == MBB.begin()) |
| 233 | return false; |
| 234 | --I; |
| 235 | while (I->isDebugValue()) { |
| 236 | if (I == MBB.begin()) |
| 237 | return false; |
| 238 | --I; |
| 239 | } |
| 240 | if (!isUnpredicatedTerminator(I)) |
Chris Lattner | c50e2bc | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 241 | return false; |
| 242 | |
| 243 | // Get the last instruction in the block. |
| 244 | MachineInstr *LastInst = I; |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 245 | |
Chris Lattner | c50e2bc | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 246 | // If there is only one terminator instruction, process it. |
Evan Cheng | bfd2ec4 | 2007-06-08 21:59:56 +0000 | [diff] [blame] | 247 | if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) { |
Chris Lattner | c50e2bc | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 248 | if (LastInst->getOpcode() == PPC::B) { |
Evan Cheng | 82ae933 | 2009-05-08 23:09:25 +0000 | [diff] [blame] | 249 | if (!LastInst->getOperand(0).isMBB()) |
| 250 | return true; |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 251 | TBB = LastInst->getOperand(0).getMBB(); |
Chris Lattner | c50e2bc | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 252 | return false; |
Chris Lattner | 289c2d5 | 2006-11-17 22:14:47 +0000 | [diff] [blame] | 253 | } else if (LastInst->getOpcode() == PPC::BCC) { |
Evan Cheng | 82ae933 | 2009-05-08 23:09:25 +0000 | [diff] [blame] | 254 | if (!LastInst->getOperand(2).isMBB()) |
| 255 | return true; |
Chris Lattner | c50e2bc | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 256 | // Block ends with fall-through condbranch. |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 257 | TBB = LastInst->getOperand(2).getMBB(); |
Chris Lattner | c50e2bc | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 258 | Cond.push_back(LastInst->getOperand(0)); |
| 259 | Cond.push_back(LastInst->getOperand(1)); |
Chris Lattner | 7c4fe25 | 2006-10-21 06:03:11 +0000 | [diff] [blame] | 260 | return false; |
Hal Finkel | 99f823f | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 261 | } else if (LastInst->getOpcode() == PPC::BDNZ8 || |
| 262 | LastInst->getOpcode() == PPC::BDNZ) { |
| 263 | if (!LastInst->getOperand(0).isMBB()) |
| 264 | return true; |
Hal Finkel | 7255d2a | 2012-06-08 19:19:53 +0000 | [diff] [blame] | 265 | if (DisableCTRLoopAnal) |
Hal Finkel | 09fdc7b | 2012-06-08 15:38:25 +0000 | [diff] [blame] | 266 | return true; |
Hal Finkel | 99f823f | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 267 | TBB = LastInst->getOperand(0).getMBB(); |
| 268 | Cond.push_back(MachineOperand::CreateImm(1)); |
| 269 | Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR, |
| 270 | true)); |
| 271 | return false; |
| 272 | } else if (LastInst->getOpcode() == PPC::BDZ8 || |
| 273 | LastInst->getOpcode() == PPC::BDZ) { |
| 274 | if (!LastInst->getOperand(0).isMBB()) |
| 275 | return true; |
Hal Finkel | 7255d2a | 2012-06-08 19:19:53 +0000 | [diff] [blame] | 276 | if (DisableCTRLoopAnal) |
Hal Finkel | 09fdc7b | 2012-06-08 15:38:25 +0000 | [diff] [blame] | 277 | return true; |
Hal Finkel | 99f823f | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 278 | TBB = LastInst->getOperand(0).getMBB(); |
| 279 | Cond.push_back(MachineOperand::CreateImm(0)); |
| 280 | Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR, |
| 281 | true)); |
| 282 | return false; |
Chris Lattner | c50e2bc | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 283 | } |
Hal Finkel | 99f823f | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 284 | |
Chris Lattner | c50e2bc | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 285 | // Otherwise, don't know what this is. |
| 286 | return true; |
| 287 | } |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 288 | |
Chris Lattner | c50e2bc | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 289 | // Get the instruction before it if it's a terminator. |
| 290 | MachineInstr *SecondLastInst = I; |
| 291 | |
| 292 | // If there are three terminators, we don't know what sort of block this is. |
| 293 | if (SecondLastInst && I != MBB.begin() && |
Evan Cheng | bfd2ec4 | 2007-06-08 21:59:56 +0000 | [diff] [blame] | 294 | isUnpredicatedTerminator(--I)) |
Chris Lattner | c50e2bc | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 295 | return true; |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 296 | |
Chris Lattner | 289c2d5 | 2006-11-17 22:14:47 +0000 | [diff] [blame] | 297 | // If the block ends with PPC::B and PPC:BCC, handle it. |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 298 | if (SecondLastInst->getOpcode() == PPC::BCC && |
Chris Lattner | c50e2bc | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 299 | LastInst->getOpcode() == PPC::B) { |
Evan Cheng | 82ae933 | 2009-05-08 23:09:25 +0000 | [diff] [blame] | 300 | if (!SecondLastInst->getOperand(2).isMBB() || |
| 301 | !LastInst->getOperand(0).isMBB()) |
| 302 | return true; |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 303 | TBB = SecondLastInst->getOperand(2).getMBB(); |
Chris Lattner | c50e2bc | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 304 | Cond.push_back(SecondLastInst->getOperand(0)); |
| 305 | Cond.push_back(SecondLastInst->getOperand(1)); |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 306 | FBB = LastInst->getOperand(0).getMBB(); |
Chris Lattner | c50e2bc | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 307 | return false; |
Hal Finkel | 99f823f | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 308 | } else if ((SecondLastInst->getOpcode() == PPC::BDNZ8 || |
| 309 | SecondLastInst->getOpcode() == PPC::BDNZ) && |
| 310 | LastInst->getOpcode() == PPC::B) { |
| 311 | if (!SecondLastInst->getOperand(0).isMBB() || |
| 312 | !LastInst->getOperand(0).isMBB()) |
| 313 | return true; |
Hal Finkel | 7255d2a | 2012-06-08 19:19:53 +0000 | [diff] [blame] | 314 | if (DisableCTRLoopAnal) |
Hal Finkel | 09fdc7b | 2012-06-08 15:38:25 +0000 | [diff] [blame] | 315 | return true; |
Hal Finkel | 99f823f | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 316 | TBB = SecondLastInst->getOperand(0).getMBB(); |
| 317 | Cond.push_back(MachineOperand::CreateImm(1)); |
| 318 | Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR, |
| 319 | true)); |
| 320 | FBB = LastInst->getOperand(0).getMBB(); |
| 321 | return false; |
| 322 | } else if ((SecondLastInst->getOpcode() == PPC::BDZ8 || |
| 323 | SecondLastInst->getOpcode() == PPC::BDZ) && |
| 324 | LastInst->getOpcode() == PPC::B) { |
| 325 | if (!SecondLastInst->getOperand(0).isMBB() || |
| 326 | !LastInst->getOperand(0).isMBB()) |
| 327 | return true; |
Hal Finkel | 7255d2a | 2012-06-08 19:19:53 +0000 | [diff] [blame] | 328 | if (DisableCTRLoopAnal) |
Hal Finkel | 09fdc7b | 2012-06-08 15:38:25 +0000 | [diff] [blame] | 329 | return true; |
Hal Finkel | 99f823f | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 330 | TBB = SecondLastInst->getOperand(0).getMBB(); |
| 331 | Cond.push_back(MachineOperand::CreateImm(0)); |
| 332 | Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR, |
| 333 | true)); |
| 334 | FBB = LastInst->getOperand(0).getMBB(); |
| 335 | return false; |
Chris Lattner | c50e2bc | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 336 | } |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 337 | |
Dale Johannesen | 13e8b51 | 2007-06-13 17:59:52 +0000 | [diff] [blame] | 338 | // If the block ends with two PPC:Bs, handle it. The second one is not |
| 339 | // executed, so remove it. |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 340 | if (SecondLastInst->getOpcode() == PPC::B && |
Dale Johannesen | 13e8b51 | 2007-06-13 17:59:52 +0000 | [diff] [blame] | 341 | LastInst->getOpcode() == PPC::B) { |
Evan Cheng | 82ae933 | 2009-05-08 23:09:25 +0000 | [diff] [blame] | 342 | if (!SecondLastInst->getOperand(0).isMBB()) |
| 343 | return true; |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 344 | TBB = SecondLastInst->getOperand(0).getMBB(); |
Dale Johannesen | 13e8b51 | 2007-06-13 17:59:52 +0000 | [diff] [blame] | 345 | I = LastInst; |
Evan Cheng | dc54d31 | 2009-02-09 07:14:22 +0000 | [diff] [blame] | 346 | if (AllowModify) |
| 347 | I->eraseFromParent(); |
Dale Johannesen | 13e8b51 | 2007-06-13 17:59:52 +0000 | [diff] [blame] | 348 | return false; |
| 349 | } |
| 350 | |
Chris Lattner | c50e2bc | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 351 | // Otherwise, can't handle this. |
| 352 | return true; |
| 353 | } |
| 354 | |
Evan Cheng | b5cdaa2 | 2007-05-18 00:05:48 +0000 | [diff] [blame] | 355 | unsigned PPCInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const { |
Chris Lattner | c50e2bc | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 356 | MachineBasicBlock::iterator I = MBB.end(); |
Evan Cheng | b5cdaa2 | 2007-05-18 00:05:48 +0000 | [diff] [blame] | 357 | if (I == MBB.begin()) return 0; |
Chris Lattner | c50e2bc | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 358 | --I; |
Dale Johannesen | 93d6a7e | 2010-04-02 01:38:09 +0000 | [diff] [blame] | 359 | while (I->isDebugValue()) { |
| 360 | if (I == MBB.begin()) |
| 361 | return 0; |
| 362 | --I; |
| 363 | } |
Hal Finkel | 99f823f | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 364 | if (I->getOpcode() != PPC::B && I->getOpcode() != PPC::BCC && |
| 365 | I->getOpcode() != PPC::BDNZ8 && I->getOpcode() != PPC::BDNZ && |
| 366 | I->getOpcode() != PPC::BDZ8 && I->getOpcode() != PPC::BDZ) |
Evan Cheng | b5cdaa2 | 2007-05-18 00:05:48 +0000 | [diff] [blame] | 367 | return 0; |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 368 | |
Chris Lattner | c50e2bc | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 369 | // Remove the branch. |
| 370 | I->eraseFromParent(); |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 371 | |
Chris Lattner | c50e2bc | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 372 | I = MBB.end(); |
| 373 | |
Evan Cheng | b5cdaa2 | 2007-05-18 00:05:48 +0000 | [diff] [blame] | 374 | if (I == MBB.begin()) return 1; |
Chris Lattner | c50e2bc | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 375 | --I; |
Hal Finkel | 99f823f | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 376 | if (I->getOpcode() != PPC::BCC && |
| 377 | I->getOpcode() != PPC::BDNZ8 && I->getOpcode() != PPC::BDNZ && |
| 378 | I->getOpcode() != PPC::BDZ8 && I->getOpcode() != PPC::BDZ) |
Evan Cheng | b5cdaa2 | 2007-05-18 00:05:48 +0000 | [diff] [blame] | 379 | return 1; |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 380 | |
Chris Lattner | c50e2bc | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 381 | // Remove the branch. |
| 382 | I->eraseFromParent(); |
Evan Cheng | b5cdaa2 | 2007-05-18 00:05:48 +0000 | [diff] [blame] | 383 | return 2; |
Chris Lattner | c50e2bc | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 384 | } |
| 385 | |
Evan Cheng | b5cdaa2 | 2007-05-18 00:05:48 +0000 | [diff] [blame] | 386 | unsigned |
| 387 | PPCInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB, |
| 388 | MachineBasicBlock *FBB, |
Stuart Hastings | 3bf9125 | 2010-06-17 22:43:56 +0000 | [diff] [blame] | 389 | const SmallVectorImpl<MachineOperand> &Cond, |
| 390 | DebugLoc DL) const { |
Chris Lattner | 2dc7723 | 2006-10-17 18:06:55 +0000 | [diff] [blame] | 391 | // Shouldn't be a fall through. |
| 392 | assert(TBB && "InsertBranch must not be told to insert a fallthrough"); |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 393 | assert((Cond.size() == 2 || Cond.size() == 0) && |
Chris Lattner | 5410806 | 2006-10-21 05:36:13 +0000 | [diff] [blame] | 394 | "PPC branch conditions have two components!"); |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 395 | |
Hal Finkel | 99f823f | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 396 | bool isPPC64 = TM.getSubtargetImpl()->isPPC64(); |
| 397 | |
Chris Lattner | 5410806 | 2006-10-21 05:36:13 +0000 | [diff] [blame] | 398 | // One-way branch. |
Chris Lattner | 2dc7723 | 2006-10-17 18:06:55 +0000 | [diff] [blame] | 399 | if (FBB == 0) { |
Chris Lattner | 5410806 | 2006-10-21 05:36:13 +0000 | [diff] [blame] | 400 | if (Cond.empty()) // Unconditional branch |
Stuart Hastings | 3bf9125 | 2010-06-17 22:43:56 +0000 | [diff] [blame] | 401 | BuildMI(&MBB, DL, get(PPC::B)).addMBB(TBB); |
Hal Finkel | 99f823f | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 402 | else if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8) |
| 403 | BuildMI(&MBB, DL, get(Cond[0].getImm() ? |
| 404 | (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) : |
| 405 | (isPPC64 ? PPC::BDZ8 : PPC::BDZ))).addMBB(TBB); |
Chris Lattner | 5410806 | 2006-10-21 05:36:13 +0000 | [diff] [blame] | 406 | else // Conditional branch |
Stuart Hastings | 3bf9125 | 2010-06-17 22:43:56 +0000 | [diff] [blame] | 407 | BuildMI(&MBB, DL, get(PPC::BCC)) |
Chris Lattner | 18258c6 | 2006-11-17 22:37:34 +0000 | [diff] [blame] | 408 | .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB); |
Evan Cheng | b5cdaa2 | 2007-05-18 00:05:48 +0000 | [diff] [blame] | 409 | return 1; |
Chris Lattner | 2dc7723 | 2006-10-17 18:06:55 +0000 | [diff] [blame] | 410 | } |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 411 | |
Chris Lattner | 879d09c | 2006-10-21 05:42:09 +0000 | [diff] [blame] | 412 | // Two-way Conditional Branch. |
Hal Finkel | 99f823f | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 413 | if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8) |
| 414 | BuildMI(&MBB, DL, get(Cond[0].getImm() ? |
| 415 | (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) : |
| 416 | (isPPC64 ? PPC::BDZ8 : PPC::BDZ))).addMBB(TBB); |
| 417 | else |
| 418 | BuildMI(&MBB, DL, get(PPC::BCC)) |
| 419 | .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB); |
Stuart Hastings | 3bf9125 | 2010-06-17 22:43:56 +0000 | [diff] [blame] | 420 | BuildMI(&MBB, DL, get(PPC::B)).addMBB(FBB); |
Evan Cheng | b5cdaa2 | 2007-05-18 00:05:48 +0000 | [diff] [blame] | 421 | return 2; |
Chris Lattner | c50e2bc | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 422 | } |
| 423 | |
Hal Finkel | ff56d1a | 2013-04-05 23:29:01 +0000 | [diff] [blame] | 424 | // Select analysis. |
| 425 | bool PPCInstrInfo::canInsertSelect(const MachineBasicBlock &MBB, |
| 426 | const SmallVectorImpl<MachineOperand> &Cond, |
| 427 | unsigned TrueReg, unsigned FalseReg, |
| 428 | int &CondCycles, int &TrueCycles, int &FalseCycles) const { |
| 429 | if (!TM.getSubtargetImpl()->hasISEL()) |
| 430 | return false; |
| 431 | |
| 432 | if (Cond.size() != 2) |
| 433 | return false; |
| 434 | |
| 435 | // If this is really a bdnz-like condition, then it cannot be turned into a |
| 436 | // select. |
| 437 | if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8) |
| 438 | return false; |
| 439 | |
| 440 | // Check register classes. |
| 441 | const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); |
| 442 | const TargetRegisterClass *RC = |
| 443 | RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg)); |
| 444 | if (!RC) |
| 445 | return false; |
| 446 | |
| 447 | // isel is for regular integer GPRs only. |
| 448 | if (!PPC::GPRCRegClass.hasSubClassEq(RC) && |
| 449 | !PPC::G8RCRegClass.hasSubClassEq(RC)) |
| 450 | return false; |
| 451 | |
| 452 | // FIXME: These numbers are for the A2, how well they work for other cores is |
| 453 | // an open question. On the A2, the isel instruction has a 2-cycle latency |
| 454 | // but single-cycle throughput. These numbers are used in combination with |
| 455 | // the MispredictPenalty setting from the active SchedMachineModel. |
| 456 | CondCycles = 1; |
| 457 | TrueCycles = 1; |
| 458 | FalseCycles = 1; |
| 459 | |
| 460 | return true; |
| 461 | } |
| 462 | |
| 463 | void PPCInstrInfo::insertSelect(MachineBasicBlock &MBB, |
| 464 | MachineBasicBlock::iterator MI, DebugLoc dl, |
| 465 | unsigned DestReg, |
| 466 | const SmallVectorImpl<MachineOperand> &Cond, |
| 467 | unsigned TrueReg, unsigned FalseReg) const { |
| 468 | assert(Cond.size() == 2 && |
| 469 | "PPC branch conditions have two components!"); |
| 470 | |
| 471 | assert(TM.getSubtargetImpl()->hasISEL() && |
| 472 | "Cannot insert select on target without ISEL support"); |
| 473 | |
| 474 | // Get the register classes. |
| 475 | MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); |
| 476 | const TargetRegisterClass *RC = |
| 477 | RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg)); |
| 478 | assert(RC && "TrueReg and FalseReg must have overlapping register classes"); |
| 479 | assert((PPC::GPRCRegClass.hasSubClassEq(RC) || |
| 480 | PPC::G8RCRegClass.hasSubClassEq(RC)) && |
| 481 | "isel is for regular integer GPRs only"); |
| 482 | |
| 483 | unsigned OpCode = |
| 484 | PPC::GPRCRegClass.hasSubClassEq(RC) ? PPC::ISEL : PPC::ISEL8; |
| 485 | unsigned SelectPred = Cond[0].getImm(); |
| 486 | |
| 487 | unsigned SubIdx; |
| 488 | bool SwapOps; |
| 489 | switch (SelectPred) { |
| 490 | default: llvm_unreachable("invalid predicate for isel"); |
| 491 | case PPC::PRED_EQ: SubIdx = PPC::sub_eq; SwapOps = false; break; |
| 492 | case PPC::PRED_NE: SubIdx = PPC::sub_eq; SwapOps = true; break; |
| 493 | case PPC::PRED_LT: SubIdx = PPC::sub_lt; SwapOps = false; break; |
| 494 | case PPC::PRED_GE: SubIdx = PPC::sub_lt; SwapOps = true; break; |
| 495 | case PPC::PRED_GT: SubIdx = PPC::sub_gt; SwapOps = false; break; |
| 496 | case PPC::PRED_LE: SubIdx = PPC::sub_gt; SwapOps = true; break; |
| 497 | case PPC::PRED_UN: SubIdx = PPC::sub_un; SwapOps = false; break; |
| 498 | case PPC::PRED_NU: SubIdx = PPC::sub_un; SwapOps = true; break; |
| 499 | } |
| 500 | |
| 501 | unsigned FirstReg = SwapOps ? FalseReg : TrueReg, |
| 502 | SecondReg = SwapOps ? TrueReg : FalseReg; |
| 503 | |
| 504 | // The first input register of isel cannot be r0. If it is a member |
| 505 | // of a register class that can be r0, then copy it first (the |
| 506 | // register allocator should eliminate the copy). |
| 507 | if (MRI.getRegClass(FirstReg)->contains(PPC::R0) || |
| 508 | MRI.getRegClass(FirstReg)->contains(PPC::X0)) { |
| 509 | const TargetRegisterClass *FirstRC = |
| 510 | MRI.getRegClass(FirstReg)->contains(PPC::X0) ? |
| 511 | &PPC::G8RC_NOX0RegClass : &PPC::GPRC_NOR0RegClass; |
| 512 | unsigned OldFirstReg = FirstReg; |
| 513 | FirstReg = MRI.createVirtualRegister(FirstRC); |
| 514 | BuildMI(MBB, MI, dl, get(TargetOpcode::COPY), FirstReg) |
| 515 | .addReg(OldFirstReg); |
| 516 | } |
| 517 | |
| 518 | BuildMI(MBB, MI, dl, get(OpCode), DestReg) |
| 519 | .addReg(FirstReg).addReg(SecondReg) |
| 520 | .addReg(Cond[1].getReg(), 0, SubIdx); |
| 521 | } |
| 522 | |
Jakob Stoklund Olesen | 27689b0 | 2010-07-11 07:31:00 +0000 | [diff] [blame] | 523 | void PPCInstrInfo::copyPhysReg(MachineBasicBlock &MBB, |
| 524 | MachineBasicBlock::iterator I, DebugLoc DL, |
| 525 | unsigned DestReg, unsigned SrcReg, |
| 526 | bool KillSrc) const { |
| 527 | unsigned Opc; |
| 528 | if (PPC::GPRCRegClass.contains(DestReg, SrcReg)) |
| 529 | Opc = PPC::OR; |
| 530 | else if (PPC::G8RCRegClass.contains(DestReg, SrcReg)) |
| 531 | Opc = PPC::OR8; |
| 532 | else if (PPC::F4RCRegClass.contains(DestReg, SrcReg)) |
| 533 | Opc = PPC::FMR; |
| 534 | else if (PPC::CRRCRegClass.contains(DestReg, SrcReg)) |
| 535 | Opc = PPC::MCRF; |
| 536 | else if (PPC::VRRCRegClass.contains(DestReg, SrcReg)) |
| 537 | Opc = PPC::VOR; |
| 538 | else if (PPC::CRBITRCRegClass.contains(DestReg, SrcReg)) |
| 539 | Opc = PPC::CROR; |
| 540 | else |
| 541 | llvm_unreachable("Impossible reg-to-reg copy"); |
Owen Anderson | d10fd97 | 2007-12-31 06:32:00 +0000 | [diff] [blame] | 542 | |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 543 | const MCInstrDesc &MCID = get(Opc); |
| 544 | if (MCID.getNumOperands() == 3) |
| 545 | BuildMI(MBB, I, DL, MCID, DestReg) |
Jakob Stoklund Olesen | 27689b0 | 2010-07-11 07:31:00 +0000 | [diff] [blame] | 546 | .addReg(SrcReg).addReg(SrcReg, getKillRegState(KillSrc)); |
| 547 | else |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 548 | BuildMI(MBB, I, DL, MCID, DestReg).addReg(SrcReg, getKillRegState(KillSrc)); |
Owen Anderson | d10fd97 | 2007-12-31 06:32:00 +0000 | [diff] [blame] | 549 | } |
| 550 | |
Hal Finkel | 3fd0018 | 2011-12-05 17:55:17 +0000 | [diff] [blame] | 551 | // This function returns true if a CR spill is necessary and false otherwise. |
Bill Wendling | 4a66e9a | 2008-03-10 22:49:16 +0000 | [diff] [blame] | 552 | bool |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 553 | PPCInstrInfo::StoreRegToStackSlot(MachineFunction &MF, |
| 554 | unsigned SrcReg, bool isKill, |
Bill Wendling | 4a66e9a | 2008-03-10 22:49:16 +0000 | [diff] [blame] | 555 | int FrameIdx, |
| 556 | const TargetRegisterClass *RC, |
Hal Finkel | 3249729 | 2013-03-17 04:43:44 +0000 | [diff] [blame] | 557 | SmallVectorImpl<MachineInstr*> &NewMIs, |
Hal Finkel | 3f2c047 | 2013-03-23 22:06:03 +0000 | [diff] [blame] | 558 | bool &NonRI, bool &SpillsVRS) const{ |
Hal Finkel | f25f93b | 2013-03-27 21:21:15 +0000 | [diff] [blame] | 559 | // Note: If additional store instructions are added here, |
| 560 | // update isStoreToStackSlot. |
| 561 | |
Chris Lattner | c7f3ace | 2010-04-02 20:16:16 +0000 | [diff] [blame] | 562 | DebugLoc DL; |
Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 563 | if (PPC::GPRCRegClass.hasSubClassEq(RC)) { |
Hal Finkel | 7257fda | 2013-03-23 17:14:27 +0000 | [diff] [blame] | 564 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW)) |
| 565 | .addReg(SrcReg, |
| 566 | getKillRegState(isKill)), |
| 567 | FrameIdx)); |
Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 568 | } else if (PPC::G8RCRegClass.hasSubClassEq(RC)) { |
Hal Finkel | 7257fda | 2013-03-23 17:14:27 +0000 | [diff] [blame] | 569 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STD)) |
| 570 | .addReg(SrcReg, |
| 571 | getKillRegState(isKill)), |
| 572 | FrameIdx)); |
Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 573 | } else if (PPC::F8RCRegClass.hasSubClassEq(RC)) { |
Dale Johannesen | 21b5541 | 2009-02-12 23:08:38 +0000 | [diff] [blame] | 574 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFD)) |
Bill Wendling | 587daed | 2009-05-13 21:33:08 +0000 | [diff] [blame] | 575 | .addReg(SrcReg, |
| 576 | getKillRegState(isKill)), |
| 577 | FrameIdx)); |
Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 578 | } else if (PPC::F4RCRegClass.hasSubClassEq(RC)) { |
Dale Johannesen | 21b5541 | 2009-02-12 23:08:38 +0000 | [diff] [blame] | 579 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFS)) |
Bill Wendling | 587daed | 2009-05-13 21:33:08 +0000 | [diff] [blame] | 580 | .addReg(SrcReg, |
| 581 | getKillRegState(isKill)), |
| 582 | FrameIdx)); |
Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 583 | } else if (PPC::CRRCRegClass.hasSubClassEq(RC)) { |
Hal Finkel | 7285e8d | 2013-03-12 14:12:16 +0000 | [diff] [blame] | 584 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CR)) |
| 585 | .addReg(SrcReg, |
| 586 | getKillRegState(isKill)), |
| 587 | FrameIdx)); |
| 588 | return true; |
Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 589 | } else if (PPC::CRBITRCRegClass.hasSubClassEq(RC)) { |
Nicolas Geoffray | 0404cd9 | 2008-03-10 14:12:10 +0000 | [diff] [blame] | 590 | // FIXME: We use CRi here because there is no mtcrf on a bit. Since the |
| 591 | // backend currently only uses CR1EQ as an individual bit, this should |
| 592 | // not cause any bug. If we need other uses of CR bits, the following |
| 593 | // code may be invalid. |
Nicolas Geoffray | 9348c69 | 2008-03-10 17:46:45 +0000 | [diff] [blame] | 594 | unsigned Reg = 0; |
Tilmann Scheller | 6a3a1ba | 2009-07-03 06:47:55 +0000 | [diff] [blame] | 595 | if (SrcReg == PPC::CR0LT || SrcReg == PPC::CR0GT || |
| 596 | SrcReg == PPC::CR0EQ || SrcReg == PPC::CR0UN) |
Nicolas Geoffray | 9348c69 | 2008-03-10 17:46:45 +0000 | [diff] [blame] | 597 | Reg = PPC::CR0; |
Tilmann Scheller | 6a3a1ba | 2009-07-03 06:47:55 +0000 | [diff] [blame] | 598 | else if (SrcReg == PPC::CR1LT || SrcReg == PPC::CR1GT || |
| 599 | SrcReg == PPC::CR1EQ || SrcReg == PPC::CR1UN) |
Nicolas Geoffray | 9348c69 | 2008-03-10 17:46:45 +0000 | [diff] [blame] | 600 | Reg = PPC::CR1; |
Tilmann Scheller | 6a3a1ba | 2009-07-03 06:47:55 +0000 | [diff] [blame] | 601 | else if (SrcReg == PPC::CR2LT || SrcReg == PPC::CR2GT || |
| 602 | SrcReg == PPC::CR2EQ || SrcReg == PPC::CR2UN) |
Nicolas Geoffray | 9348c69 | 2008-03-10 17:46:45 +0000 | [diff] [blame] | 603 | Reg = PPC::CR2; |
Tilmann Scheller | 6a3a1ba | 2009-07-03 06:47:55 +0000 | [diff] [blame] | 604 | else if (SrcReg == PPC::CR3LT || SrcReg == PPC::CR3GT || |
| 605 | SrcReg == PPC::CR3EQ || SrcReg == PPC::CR3UN) |
Nicolas Geoffray | 9348c69 | 2008-03-10 17:46:45 +0000 | [diff] [blame] | 606 | Reg = PPC::CR3; |
Tilmann Scheller | 6a3a1ba | 2009-07-03 06:47:55 +0000 | [diff] [blame] | 607 | else if (SrcReg == PPC::CR4LT || SrcReg == PPC::CR4GT || |
| 608 | SrcReg == PPC::CR4EQ || SrcReg == PPC::CR4UN) |
Nicolas Geoffray | 9348c69 | 2008-03-10 17:46:45 +0000 | [diff] [blame] | 609 | Reg = PPC::CR4; |
Tilmann Scheller | 6a3a1ba | 2009-07-03 06:47:55 +0000 | [diff] [blame] | 610 | else if (SrcReg == PPC::CR5LT || SrcReg == PPC::CR5GT || |
| 611 | SrcReg == PPC::CR5EQ || SrcReg == PPC::CR5UN) |
Nicolas Geoffray | 9348c69 | 2008-03-10 17:46:45 +0000 | [diff] [blame] | 612 | Reg = PPC::CR5; |
Tilmann Scheller | 6a3a1ba | 2009-07-03 06:47:55 +0000 | [diff] [blame] | 613 | else if (SrcReg == PPC::CR6LT || SrcReg == PPC::CR6GT || |
| 614 | SrcReg == PPC::CR6EQ || SrcReg == PPC::CR6UN) |
Nicolas Geoffray | 9348c69 | 2008-03-10 17:46:45 +0000 | [diff] [blame] | 615 | Reg = PPC::CR6; |
Tilmann Scheller | 6a3a1ba | 2009-07-03 06:47:55 +0000 | [diff] [blame] | 616 | else if (SrcReg == PPC::CR7LT || SrcReg == PPC::CR7GT || |
| 617 | SrcReg == PPC::CR7EQ || SrcReg == PPC::CR7UN) |
Nicolas Geoffray | 9348c69 | 2008-03-10 17:46:45 +0000 | [diff] [blame] | 618 | Reg = PPC::CR7; |
| 619 | |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 620 | return StoreRegToStackSlot(MF, Reg, isKill, FrameIdx, |
Hal Finkel | 3f2c047 | 2013-03-23 22:06:03 +0000 | [diff] [blame] | 621 | &PPC::CRRCRegClass, NewMIs, NonRI, SpillsVRS); |
Nicolas Geoffray | 9348c69 | 2008-03-10 17:46:45 +0000 | [diff] [blame] | 622 | |
Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 623 | } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) { |
Hal Finkel | 3249729 | 2013-03-17 04:43:44 +0000 | [diff] [blame] | 624 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STVX)) |
| 625 | .addReg(SrcReg, |
| 626 | getKillRegState(isKill)), |
| 627 | FrameIdx)); |
| 628 | NonRI = true; |
Hal Finkel | 10f7f2a | 2013-03-21 19:03:21 +0000 | [diff] [blame] | 629 | } else if (PPC::VRSAVERCRegClass.hasSubClassEq(RC)) { |
Hal Finkel | b7e11e4 | 2013-03-27 00:02:20 +0000 | [diff] [blame] | 630 | assert(TM.getSubtargetImpl()->isDarwin() && |
| 631 | "VRSAVE only needs spill/restore on Darwin"); |
Hal Finkel | 10f7f2a | 2013-03-21 19:03:21 +0000 | [diff] [blame] | 632 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_VRSAVE)) |
| 633 | .addReg(SrcReg, |
| 634 | getKillRegState(isKill)), |
| 635 | FrameIdx)); |
Hal Finkel | 3f2c047 | 2013-03-23 22:06:03 +0000 | [diff] [blame] | 636 | SpillsVRS = true; |
Owen Anderson | f6372aa | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 637 | } else { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 638 | llvm_unreachable("Unknown regclass!"); |
Owen Anderson | f6372aa | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 639 | } |
Bill Wendling | 7194aaf | 2008-03-03 22:19:16 +0000 | [diff] [blame] | 640 | |
| 641 | return false; |
Owen Anderson | f6372aa | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | void |
| 645 | PPCInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB, |
Bill Wendling | 7194aaf | 2008-03-03 22:19:16 +0000 | [diff] [blame] | 646 | MachineBasicBlock::iterator MI, |
| 647 | unsigned SrcReg, bool isKill, int FrameIdx, |
Evan Cheng | 746ad69 | 2010-05-06 19:06:44 +0000 | [diff] [blame] | 648 | const TargetRegisterClass *RC, |
| 649 | const TargetRegisterInfo *TRI) const { |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 650 | MachineFunction &MF = *MBB.getParent(); |
Owen Anderson | f6372aa | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 651 | SmallVector<MachineInstr*, 4> NewMIs; |
Bill Wendling | 7194aaf | 2008-03-03 22:19:16 +0000 | [diff] [blame] | 652 | |
Hal Finkel | 0cfb42a | 2013-03-15 05:06:04 +0000 | [diff] [blame] | 653 | PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); |
| 654 | FuncInfo->setHasSpills(); |
| 655 | |
Hal Finkel | 3f2c047 | 2013-03-23 22:06:03 +0000 | [diff] [blame] | 656 | bool NonRI = false, SpillsVRS = false; |
| 657 | if (StoreRegToStackSlot(MF, SrcReg, isKill, FrameIdx, RC, NewMIs, |
| 658 | NonRI, SpillsVRS)) |
Bill Wendling | 7194aaf | 2008-03-03 22:19:16 +0000 | [diff] [blame] | 659 | FuncInfo->setSpillsCR(); |
Bill Wendling | 7194aaf | 2008-03-03 22:19:16 +0000 | [diff] [blame] | 660 | |
Hal Finkel | 3f2c047 | 2013-03-23 22:06:03 +0000 | [diff] [blame] | 661 | if (SpillsVRS) |
| 662 | FuncInfo->setSpillsVRSAVE(); |
| 663 | |
Hal Finkel | 3249729 | 2013-03-17 04:43:44 +0000 | [diff] [blame] | 664 | if (NonRI) |
| 665 | FuncInfo->setHasNonRISpills(); |
| 666 | |
Owen Anderson | f6372aa | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 667 | for (unsigned i = 0, e = NewMIs.size(); i != e; ++i) |
| 668 | MBB.insert(MI, NewMIs[i]); |
Jakob Stoklund Olesen | 7a79fcb | 2010-07-16 18:22:00 +0000 | [diff] [blame] | 669 | |
| 670 | const MachineFrameInfo &MFI = *MF.getFrameInfo(); |
| 671 | MachineMemOperand *MMO = |
Jay Foad | 978e0df | 2011-11-15 07:34:52 +0000 | [diff] [blame] | 672 | MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx), |
Chris Lattner | 59db549 | 2010-09-21 04:39:43 +0000 | [diff] [blame] | 673 | MachineMemOperand::MOStore, |
Jakob Stoklund Olesen | 7a79fcb | 2010-07-16 18:22:00 +0000 | [diff] [blame] | 674 | MFI.getObjectSize(FrameIdx), |
| 675 | MFI.getObjectAlignment(FrameIdx)); |
| 676 | NewMIs.back()->addMemOperand(MF, MMO); |
Owen Anderson | f6372aa | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 677 | } |
| 678 | |
Hal Finkel | d21e930 | 2011-12-06 20:55:36 +0000 | [diff] [blame] | 679 | bool |
Bill Wendling | d1c321a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 680 | PPCInstrInfo::LoadRegFromStackSlot(MachineFunction &MF, DebugLoc DL, |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 681 | unsigned DestReg, int FrameIdx, |
Bill Wendling | 4a66e9a | 2008-03-10 22:49:16 +0000 | [diff] [blame] | 682 | const TargetRegisterClass *RC, |
Hal Finkel | 3249729 | 2013-03-17 04:43:44 +0000 | [diff] [blame] | 683 | SmallVectorImpl<MachineInstr*> &NewMIs, |
Hal Finkel | 3f2c047 | 2013-03-23 22:06:03 +0000 | [diff] [blame] | 684 | bool &NonRI, bool &SpillsVRS) const{ |
Hal Finkel | f25f93b | 2013-03-27 21:21:15 +0000 | [diff] [blame] | 685 | // Note: If additional load instructions are added here, |
| 686 | // update isLoadFromStackSlot. |
| 687 | |
Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 688 | if (PPC::GPRCRegClass.hasSubClassEq(RC)) { |
Hal Finkel | fc80586 | 2013-03-27 19:10:40 +0000 | [diff] [blame] | 689 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ), |
| 690 | DestReg), FrameIdx)); |
Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 691 | } else if (PPC::G8RCRegClass.hasSubClassEq(RC)) { |
Hal Finkel | fc80586 | 2013-03-27 19:10:40 +0000 | [diff] [blame] | 692 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LD), DestReg), |
| 693 | FrameIdx)); |
Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 694 | } else if (PPC::F8RCRegClass.hasSubClassEq(RC)) { |
Bill Wendling | d1c321a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 695 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFD), DestReg), |
Owen Anderson | f6372aa | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 696 | FrameIdx)); |
Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 697 | } else if (PPC::F4RCRegClass.hasSubClassEq(RC)) { |
Bill Wendling | d1c321a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 698 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFS), DestReg), |
Owen Anderson | f6372aa | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 699 | FrameIdx)); |
Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 700 | } else if (PPC::CRRCRegClass.hasSubClassEq(RC)) { |
Hal Finkel | 7285e8d | 2013-03-12 14:12:16 +0000 | [diff] [blame] | 701 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, |
| 702 | get(PPC::RESTORE_CR), DestReg), |
| 703 | FrameIdx)); |
| 704 | return true; |
Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 705 | } else if (PPC::CRBITRCRegClass.hasSubClassEq(RC)) { |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 706 | |
Nicolas Geoffray | 9348c69 | 2008-03-10 17:46:45 +0000 | [diff] [blame] | 707 | unsigned Reg = 0; |
Tilmann Scheller | 6a3a1ba | 2009-07-03 06:47:55 +0000 | [diff] [blame] | 708 | if (DestReg == PPC::CR0LT || DestReg == PPC::CR0GT || |
| 709 | DestReg == PPC::CR0EQ || DestReg == PPC::CR0UN) |
Nicolas Geoffray | 9348c69 | 2008-03-10 17:46:45 +0000 | [diff] [blame] | 710 | Reg = PPC::CR0; |
Tilmann Scheller | 6a3a1ba | 2009-07-03 06:47:55 +0000 | [diff] [blame] | 711 | else if (DestReg == PPC::CR1LT || DestReg == PPC::CR1GT || |
| 712 | DestReg == PPC::CR1EQ || DestReg == PPC::CR1UN) |
Nicolas Geoffray | 9348c69 | 2008-03-10 17:46:45 +0000 | [diff] [blame] | 713 | Reg = PPC::CR1; |
Tilmann Scheller | 6a3a1ba | 2009-07-03 06:47:55 +0000 | [diff] [blame] | 714 | else if (DestReg == PPC::CR2LT || DestReg == PPC::CR2GT || |
| 715 | DestReg == PPC::CR2EQ || DestReg == PPC::CR2UN) |
Nicolas Geoffray | 9348c69 | 2008-03-10 17:46:45 +0000 | [diff] [blame] | 716 | Reg = PPC::CR2; |
Tilmann Scheller | 6a3a1ba | 2009-07-03 06:47:55 +0000 | [diff] [blame] | 717 | else if (DestReg == PPC::CR3LT || DestReg == PPC::CR3GT || |
| 718 | DestReg == PPC::CR3EQ || DestReg == PPC::CR3UN) |
Nicolas Geoffray | 9348c69 | 2008-03-10 17:46:45 +0000 | [diff] [blame] | 719 | Reg = PPC::CR3; |
Tilmann Scheller | 6a3a1ba | 2009-07-03 06:47:55 +0000 | [diff] [blame] | 720 | else if (DestReg == PPC::CR4LT || DestReg == PPC::CR4GT || |
| 721 | DestReg == PPC::CR4EQ || DestReg == PPC::CR4UN) |
Nicolas Geoffray | 9348c69 | 2008-03-10 17:46:45 +0000 | [diff] [blame] | 722 | Reg = PPC::CR4; |
Tilmann Scheller | 6a3a1ba | 2009-07-03 06:47:55 +0000 | [diff] [blame] | 723 | else if (DestReg == PPC::CR5LT || DestReg == PPC::CR5GT || |
| 724 | DestReg == PPC::CR5EQ || DestReg == PPC::CR5UN) |
Nicolas Geoffray | 9348c69 | 2008-03-10 17:46:45 +0000 | [diff] [blame] | 725 | Reg = PPC::CR5; |
Tilmann Scheller | 6a3a1ba | 2009-07-03 06:47:55 +0000 | [diff] [blame] | 726 | else if (DestReg == PPC::CR6LT || DestReg == PPC::CR6GT || |
| 727 | DestReg == PPC::CR6EQ || DestReg == PPC::CR6UN) |
Nicolas Geoffray | 9348c69 | 2008-03-10 17:46:45 +0000 | [diff] [blame] | 728 | Reg = PPC::CR6; |
Tilmann Scheller | 6a3a1ba | 2009-07-03 06:47:55 +0000 | [diff] [blame] | 729 | else if (DestReg == PPC::CR7LT || DestReg == PPC::CR7GT || |
| 730 | DestReg == PPC::CR7EQ || DestReg == PPC::CR7UN) |
Nicolas Geoffray | 9348c69 | 2008-03-10 17:46:45 +0000 | [diff] [blame] | 731 | Reg = PPC::CR7; |
| 732 | |
Andrew Trick | 6e8f4c4 | 2010-12-24 04:28:06 +0000 | [diff] [blame] | 733 | return LoadRegFromStackSlot(MF, DL, Reg, FrameIdx, |
Hal Finkel | 3f2c047 | 2013-03-23 22:06:03 +0000 | [diff] [blame] | 734 | &PPC::CRRCRegClass, NewMIs, NonRI, SpillsVRS); |
Nicolas Geoffray | 9348c69 | 2008-03-10 17:46:45 +0000 | [diff] [blame] | 735 | |
Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 736 | } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) { |
Hal Finkel | 3249729 | 2013-03-17 04:43:44 +0000 | [diff] [blame] | 737 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LVX), DestReg), |
| 738 | FrameIdx)); |
| 739 | NonRI = true; |
Hal Finkel | 10f7f2a | 2013-03-21 19:03:21 +0000 | [diff] [blame] | 740 | } else if (PPC::VRSAVERCRegClass.hasSubClassEq(RC)) { |
Hal Finkel | b7e11e4 | 2013-03-27 00:02:20 +0000 | [diff] [blame] | 741 | assert(TM.getSubtargetImpl()->isDarwin() && |
| 742 | "VRSAVE only needs spill/restore on Darwin"); |
Hal Finkel | 10f7f2a | 2013-03-21 19:03:21 +0000 | [diff] [blame] | 743 | NewMIs.push_back(addFrameReference(BuildMI(MF, DL, |
| 744 | get(PPC::RESTORE_VRSAVE), |
| 745 | DestReg), |
| 746 | FrameIdx)); |
Hal Finkel | 3f2c047 | 2013-03-23 22:06:03 +0000 | [diff] [blame] | 747 | SpillsVRS = true; |
Owen Anderson | f6372aa | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 748 | } else { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 749 | llvm_unreachable("Unknown regclass!"); |
Owen Anderson | f6372aa | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 750 | } |
Hal Finkel | d21e930 | 2011-12-06 20:55:36 +0000 | [diff] [blame] | 751 | |
| 752 | return false; |
Owen Anderson | f6372aa | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | void |
| 756 | PPCInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB, |
Bill Wendling | 7194aaf | 2008-03-03 22:19:16 +0000 | [diff] [blame] | 757 | MachineBasicBlock::iterator MI, |
| 758 | unsigned DestReg, int FrameIdx, |
Evan Cheng | 746ad69 | 2010-05-06 19:06:44 +0000 | [diff] [blame] | 759 | const TargetRegisterClass *RC, |
| 760 | const TargetRegisterInfo *TRI) const { |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 761 | MachineFunction &MF = *MBB.getParent(); |
Owen Anderson | f6372aa | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 762 | SmallVector<MachineInstr*, 4> NewMIs; |
Chris Lattner | c7f3ace | 2010-04-02 20:16:16 +0000 | [diff] [blame] | 763 | DebugLoc DL; |
Bill Wendling | d1c321a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 764 | if (MI != MBB.end()) DL = MI->getDebugLoc(); |
Hal Finkel | 3249729 | 2013-03-17 04:43:44 +0000 | [diff] [blame] | 765 | |
| 766 | PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); |
| 767 | FuncInfo->setHasSpills(); |
| 768 | |
Hal Finkel | 3f2c047 | 2013-03-23 22:06:03 +0000 | [diff] [blame] | 769 | bool NonRI = false, SpillsVRS = false; |
| 770 | if (LoadRegFromStackSlot(MF, DL, DestReg, FrameIdx, RC, NewMIs, |
| 771 | NonRI, SpillsVRS)) |
Hal Finkel | d21e930 | 2011-12-06 20:55:36 +0000 | [diff] [blame] | 772 | FuncInfo->setSpillsCR(); |
Hal Finkel | 3249729 | 2013-03-17 04:43:44 +0000 | [diff] [blame] | 773 | |
Hal Finkel | 3f2c047 | 2013-03-23 22:06:03 +0000 | [diff] [blame] | 774 | if (SpillsVRS) |
| 775 | FuncInfo->setSpillsVRSAVE(); |
| 776 | |
Hal Finkel | 3249729 | 2013-03-17 04:43:44 +0000 | [diff] [blame] | 777 | if (NonRI) |
| 778 | FuncInfo->setHasNonRISpills(); |
| 779 | |
Owen Anderson | f6372aa | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 780 | for (unsigned i = 0, e = NewMIs.size(); i != e; ++i) |
| 781 | MBB.insert(MI, NewMIs[i]); |
Jakob Stoklund Olesen | 7a79fcb | 2010-07-16 18:22:00 +0000 | [diff] [blame] | 782 | |
| 783 | const MachineFrameInfo &MFI = *MF.getFrameInfo(); |
| 784 | MachineMemOperand *MMO = |
Jay Foad | 978e0df | 2011-11-15 07:34:52 +0000 | [diff] [blame] | 785 | MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx), |
Chris Lattner | 59db549 | 2010-09-21 04:39:43 +0000 | [diff] [blame] | 786 | MachineMemOperand::MOLoad, |
Jakob Stoklund Olesen | 7a79fcb | 2010-07-16 18:22:00 +0000 | [diff] [blame] | 787 | MFI.getObjectSize(FrameIdx), |
| 788 | MFI.getObjectAlignment(FrameIdx)); |
| 789 | NewMIs.back()->addMemOperand(MF, MMO); |
Owen Anderson | f6372aa | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 790 | } |
| 791 | |
Evan Cheng | 0965217 | 2010-04-26 07:39:36 +0000 | [diff] [blame] | 792 | MachineInstr* |
| 793 | PPCInstrInfo::emitFrameIndexDebugValue(MachineFunction &MF, |
Evan Cheng | 8601a3d | 2010-04-29 01:13:30 +0000 | [diff] [blame] | 794 | int FrameIx, uint64_t Offset, |
Evan Cheng | 0965217 | 2010-04-26 07:39:36 +0000 | [diff] [blame] | 795 | const MDNode *MDPtr, |
| 796 | DebugLoc DL) const { |
| 797 | MachineInstrBuilder MIB = BuildMI(MF, DL, get(PPC::DBG_VALUE)); |
| 798 | addFrameReference(MIB, FrameIx, 0, false).addImm(Offset).addMetadata(MDPtr); |
| 799 | return &*MIB; |
| 800 | } |
| 801 | |
Chris Lattner | c50e2bc | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 802 | bool PPCInstrInfo:: |
Owen Anderson | 44eb65c | 2008-08-14 22:49:33 +0000 | [diff] [blame] | 803 | ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const { |
Chris Lattner | 7c4fe25 | 2006-10-21 06:03:11 +0000 | [diff] [blame] | 804 | assert(Cond.size() == 2 && "Invalid PPC branch opcode!"); |
Hal Finkel | 99f823f | 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 | 7c4fe25 | 2006-10-21 06:03:11 +0000 | [diff] [blame] | 810 | return false; |
Chris Lattner | c50e2bc | 2006-10-13 21:21:17 +0000 | [diff] [blame] | 811 | } |
Nicolas Geoffray | 52e724a | 2008-04-16 20:10:13 +0000 | [diff] [blame] | 812 | |
Hal Finkel | 839b909 | 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 | da47e17 | 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 | 7eb0d81 | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 904 | bool PPCInstrInfo::isPredicated(const MachineInstr *MI) const { |
Hal Finkel | 4b04029 | 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 | 7eb0d81 | 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 | 90dd7fd | 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 | 7eb0d81 | 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 | |
| 991 | PPC::Predicate P1 = (PPC::Predicate) Pred1[0].getImm(); |
| 992 | PPC::Predicate P2 = (PPC::Predicate) Pred2[0].getImm(); |
| 993 | |
| 994 | if (P1 == P2) |
| 995 | return true; |
| 996 | |
| 997 | // Does P1 subsume P2, e.g. GE subsumes GT. |
| 998 | if (P1 == PPC::PRED_LE && |
| 999 | (P2 == PPC::PRED_LT || P2 == PPC::PRED_EQ)) |
| 1000 | return true; |
| 1001 | if (P1 == PPC::PRED_GE && |
| 1002 | (P2 == PPC::PRED_GT || P2 == PPC::PRED_EQ)) |
| 1003 | return true; |
| 1004 | |
| 1005 | return false; |
| 1006 | } |
| 1007 | |
| 1008 | bool PPCInstrInfo::DefinesPredicate(MachineInstr *MI, |
| 1009 | std::vector<MachineOperand> &Pred) const { |
| 1010 | // Note: At the present time, the contents of Pred from this function is |
| 1011 | // unused by IfConversion. This implementation follows ARM by pushing the |
| 1012 | // CR-defining operand. Because the 'DZ' and 'DNZ' count as types of |
| 1013 | // predicate, instructions defining CTR or CTR8 are also included as |
| 1014 | // predicate-defining instructions. |
| 1015 | |
| 1016 | const TargetRegisterClass *RCs[] = |
| 1017 | { &PPC::CRRCRegClass, &PPC::CRBITRCRegClass, |
| 1018 | &PPC::CTRRCRegClass, &PPC::CTRRC8RegClass }; |
| 1019 | |
| 1020 | bool Found = false; |
| 1021 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 1022 | const MachineOperand &MO = MI->getOperand(i); |
Hal Finkel | 4e31728 | 2013-04-10 07:17:47 +0000 | [diff] [blame] | 1023 | for (unsigned c = 0; c < array_lengthof(RCs) && !Found; ++c) { |
Hal Finkel | 7eb0d81 | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1024 | const TargetRegisterClass *RC = RCs[c]; |
Hal Finkel | 4e31728 | 2013-04-10 07:17:47 +0000 | [diff] [blame] | 1025 | if (MO.isReg()) { |
| 1026 | if (MO.isDef() && RC->contains(MO.getReg())) { |
Hal Finkel | 7eb0d81 | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1027 | Pred.push_back(MO); |
| 1028 | Found = true; |
| 1029 | } |
Hal Finkel | 4e31728 | 2013-04-10 07:17:47 +0000 | [diff] [blame] | 1030 | } else if (MO.isRegMask()) { |
| 1031 | for (TargetRegisterClass::iterator I = RC->begin(), |
| 1032 | IE = RC->end(); I != IE; ++I) |
| 1033 | if (MO.clobbersPhysReg(*I)) { |
| 1034 | Pred.push_back(MO); |
| 1035 | Found = true; |
| 1036 | } |
Hal Finkel | 7eb0d81 | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1037 | } |
| 1038 | } |
| 1039 | } |
| 1040 | |
| 1041 | return Found; |
| 1042 | } |
| 1043 | |
| 1044 | bool PPCInstrInfo::isPredicable(MachineInstr *MI) const { |
| 1045 | unsigned OpC = MI->getOpcode(); |
| 1046 | switch (OpC) { |
| 1047 | default: |
| 1048 | return false; |
| 1049 | case PPC::B: |
| 1050 | case PPC::BLR: |
Hal Finkel | 90dd7fd | 2013-04-10 06:42:34 +0000 | [diff] [blame] | 1051 | case PPC::BCTR: |
| 1052 | case PPC::BCTR8: |
| 1053 | case PPC::BCTRL: |
| 1054 | case PPC::BCTRL8: |
Hal Finkel | 7eb0d81 | 2013-04-09 22:58:37 +0000 | [diff] [blame] | 1055 | return true; |
| 1056 | } |
| 1057 | } |
| 1058 | |
Hal Finkel | 860c08c | 2013-04-18 22:15:08 +0000 | [diff] [blame] | 1059 | bool PPCInstrInfo::analyzeCompare(const MachineInstr *MI, |
| 1060 | unsigned &SrcReg, unsigned &SrcReg2, |
| 1061 | int &Mask, int &Value) const { |
| 1062 | unsigned Opc = MI->getOpcode(); |
| 1063 | |
| 1064 | switch (Opc) { |
| 1065 | default: return false; |
| 1066 | case PPC::CMPWI: |
| 1067 | case PPC::CMPLWI: |
| 1068 | case PPC::CMPDI: |
| 1069 | case PPC::CMPLDI: |
| 1070 | SrcReg = MI->getOperand(1).getReg(); |
| 1071 | SrcReg2 = 0; |
| 1072 | Value = MI->getOperand(2).getImm(); |
| 1073 | Mask = 0xFFFF; |
| 1074 | return true; |
| 1075 | case PPC::CMPW: |
| 1076 | case PPC::CMPLW: |
| 1077 | case PPC::CMPD: |
| 1078 | case PPC::CMPLD: |
| 1079 | case PPC::FCMPUS: |
| 1080 | case PPC::FCMPUD: |
| 1081 | SrcReg = MI->getOperand(1).getReg(); |
| 1082 | SrcReg2 = MI->getOperand(2).getReg(); |
| 1083 | return true; |
| 1084 | } |
| 1085 | } |
| 1086 | |
| 1087 | bool PPCInstrInfo::optimizeCompareInstr(MachineInstr *CmpInstr, |
| 1088 | unsigned SrcReg, unsigned SrcReg2, |
| 1089 | int Mask, int Value, |
| 1090 | const MachineRegisterInfo *MRI) const { |
| 1091 | int OpC = CmpInstr->getOpcode(); |
| 1092 | unsigned CRReg = CmpInstr->getOperand(0).getReg(); |
| 1093 | bool isFP = OpC == PPC::FCMPUS || OpC == PPC::FCMPUD; |
| 1094 | unsigned CRRecReg = isFP ? PPC::CR1 : PPC::CR0; |
| 1095 | |
| 1096 | // The record forms set the condition register based on a signed comparison |
| 1097 | // with zero (so says the ISA manual). This is not as straightforward as it |
| 1098 | // seems, however, because this is always a 64-bit comparison on PPC64, even |
| 1099 | // for instructions that are 32-bit in nature (like slw for example). |
| 1100 | // So, on PPC32, for unsigned comparisons, we can use the record forms only |
| 1101 | // for equality checks (as those don't depend on the sign). On PPC64, |
| 1102 | // we are restricted to equality for unsigned 64-bit comparisons and for |
| 1103 | // signed 32-bit comparisons the applicability is more restricted. |
| 1104 | bool isPPC64 = TM.getSubtargetImpl()->isPPC64(); |
| 1105 | bool is32BitSignedCompare = OpC == PPC::CMPWI || OpC == PPC::CMPW; |
| 1106 | bool is32BitUnsignedCompare = OpC == PPC::CMPLWI || OpC == PPC::CMPLW; |
| 1107 | bool is64BitUnsignedCompare = OpC == PPC::CMPLDI || OpC == PPC::CMPLD; |
| 1108 | |
| 1109 | // Get the unique definition of SrcReg. |
| 1110 | MachineInstr *MI = MRI->getUniqueVRegDef(SrcReg); |
| 1111 | if (!MI) return false; |
| 1112 | int MIOpC = MI->getOpcode(); |
| 1113 | |
| 1114 | bool equalityOnly = false; |
| 1115 | bool noSub = false; |
| 1116 | if (isPPC64) { |
| 1117 | if (is32BitSignedCompare) { |
| 1118 | // We can perform this optimization only if MI is sign-extending. |
| 1119 | if (MIOpC == PPC::SRAW || MIOpC == PPC::SRAWo || |
| 1120 | MIOpC == PPC::SRAWI || MIOpC == PPC::SRAWIo || |
| 1121 | MIOpC == PPC::EXTSB || MIOpC == PPC::EXTSBo || |
| 1122 | MIOpC == PPC::EXTSH || MIOpC == PPC::EXTSHo || |
| 1123 | MIOpC == PPC::EXTSW || MIOpC == PPC::EXTSWo) { |
| 1124 | noSub = true; |
| 1125 | } else |
| 1126 | return false; |
| 1127 | } else if (is32BitUnsignedCompare) { |
| 1128 | // We can perform this optimization, equality only, if MI is |
| 1129 | // zero-extending. |
| 1130 | if (MIOpC == PPC::CNTLZW || MIOpC == PPC::CNTLZWo || |
| 1131 | MIOpC == PPC::SLW || MIOpC == PPC::SLWo || |
| 1132 | MIOpC == PPC::SRW || MIOpC == PPC::SRWo) { |
| 1133 | noSub = true; |
| 1134 | equalityOnly = true; |
| 1135 | } else |
| 1136 | return false; |
| 1137 | } else if (!isFP) |
| 1138 | equalityOnly = is64BitUnsignedCompare; |
| 1139 | } else if (!isFP) |
| 1140 | equalityOnly = is32BitUnsignedCompare; |
| 1141 | |
| 1142 | if (equalityOnly) { |
| 1143 | // We need to check the uses of the condition register in order to reject |
| 1144 | // non-equality comparisons. |
| 1145 | for (MachineRegisterInfo::use_iterator I = MRI->use_begin(CRReg), |
| 1146 | IE = MRI->use_end(); I != IE; ++I) { |
| 1147 | MachineInstr *UseMI = &*I; |
| 1148 | if (UseMI->getOpcode() == PPC::BCC) { |
| 1149 | unsigned Pred = UseMI->getOperand(0).getImm(); |
| 1150 | if (Pred == PPC::PRED_EQ || Pred == PPC::PRED_NE) |
| 1151 | continue; |
| 1152 | |
| 1153 | return false; |
| 1154 | } else if (UseMI->getOpcode() == PPC::ISEL || |
| 1155 | UseMI->getOpcode() == PPC::ISEL8) { |
| 1156 | unsigned SubIdx = UseMI->getOperand(3).getSubReg(); |
| 1157 | if (SubIdx == PPC::sub_eq) |
| 1158 | continue; |
| 1159 | |
| 1160 | return false; |
| 1161 | } else |
| 1162 | return false; |
| 1163 | } |
| 1164 | } |
| 1165 | |
| 1166 | // Get ready to iterate backward from CmpInstr. |
| 1167 | MachineBasicBlock::iterator I = CmpInstr, E = MI, |
| 1168 | B = CmpInstr->getParent()->begin(); |
| 1169 | |
| 1170 | // Scan forward to find the first use of the compare. |
| 1171 | for (MachineBasicBlock::iterator EL = CmpInstr->getParent()->end(); |
| 1172 | I != EL; ++I) { |
| 1173 | bool FoundUse = false; |
| 1174 | for (MachineRegisterInfo::use_iterator J = MRI->use_begin(CRReg), |
| 1175 | JE = MRI->use_end(); J != JE; ++J) |
| 1176 | if (&*J == &*I) { |
| 1177 | FoundUse = true; |
| 1178 | break; |
| 1179 | } |
| 1180 | |
| 1181 | if (FoundUse) |
| 1182 | break; |
| 1183 | } |
| 1184 | |
| 1185 | // Early exit if we're at the beginning of the BB. |
| 1186 | if (I == B) return false; |
| 1187 | |
| 1188 | // There are two possible candidates which can be changed to set CR[01]. |
| 1189 | // One is MI, the other is a SUB instruction. |
| 1190 | // For CMPrr(r1,r2), we are looking for SUB(r1,r2) or SUB(r2,r1). |
| 1191 | MachineInstr *Sub = NULL; |
| 1192 | if (SrcReg2 != 0) |
| 1193 | // MI is not a candidate for CMPrr. |
| 1194 | MI = NULL; |
| 1195 | // FIXME: Conservatively refuse to convert an instruction which isn't in the |
| 1196 | // same BB as the comparison. This is to allow the check below to avoid calls |
| 1197 | // (and other explicit clobbers); instead we should really check for these |
| 1198 | // more explicitly (in at least a few predecessors). |
| 1199 | else if (MI->getParent() != CmpInstr->getParent() || Value != 0) { |
| 1200 | // PPC does not have a record-form SUBri. |
| 1201 | return false; |
| 1202 | } |
| 1203 | |
| 1204 | // Search for Sub. |
| 1205 | const TargetRegisterInfo *TRI = &getRegisterInfo(); |
| 1206 | --I; |
| 1207 | for (; I != E && !noSub; --I) { |
| 1208 | const MachineInstr &Instr = *I; |
| 1209 | unsigned IOpC = Instr.getOpcode(); |
| 1210 | |
| 1211 | if (&*I != CmpInstr && ( |
| 1212 | Instr.modifiesRegister(CRRecReg, TRI) || |
| 1213 | Instr.readsRegister(CRRecReg, TRI))) |
| 1214 | // This instruction modifies or uses the record condition register after |
| 1215 | // the one we want to change. While we could do this transformation, it |
| 1216 | // would likely not be profitable. This transformation removes one |
| 1217 | // instruction, and so even forcing RA to generate one move probably |
| 1218 | // makes it unprofitable. |
| 1219 | return false; |
| 1220 | |
| 1221 | // Check whether CmpInstr can be made redundant by the current instruction. |
| 1222 | if ((OpC == PPC::CMPW || OpC == PPC::CMPLW || |
| 1223 | OpC == PPC::CMPD || OpC == PPC::CMPLD) && |
| 1224 | (IOpC == PPC::SUBF || IOpC == PPC::SUBF8) && |
| 1225 | ((Instr.getOperand(1).getReg() == SrcReg && |
| 1226 | Instr.getOperand(2).getReg() == SrcReg2) || |
| 1227 | (Instr.getOperand(1).getReg() == SrcReg2 && |
| 1228 | Instr.getOperand(2).getReg() == SrcReg))) { |
| 1229 | Sub = &*I; |
| 1230 | break; |
| 1231 | } |
| 1232 | |
| 1233 | if (isFP && (IOpC == PPC::FSUB || IOpC == PPC::FSUBS) && |
| 1234 | ((Instr.getOperand(1).getReg() == SrcReg && |
| 1235 | Instr.getOperand(2).getReg() == SrcReg2) || |
| 1236 | (Instr.getOperand(1).getReg() == SrcReg2 && |
| 1237 | Instr.getOperand(2).getReg() == SrcReg))) { |
| 1238 | Sub = &*I; |
| 1239 | break; |
| 1240 | } |
| 1241 | |
| 1242 | if (I == B) |
| 1243 | // The 'and' is below the comparison instruction. |
| 1244 | return false; |
| 1245 | } |
| 1246 | |
| 1247 | // Return false if no candidates exist. |
| 1248 | if (!MI && !Sub) |
| 1249 | return false; |
| 1250 | |
| 1251 | // The single candidate is called MI. |
| 1252 | if (!MI) MI = Sub; |
| 1253 | |
| 1254 | int NewOpC = -1; |
| 1255 | MIOpC = MI->getOpcode(); |
| 1256 | if (MIOpC == PPC::ANDIo || MIOpC == PPC::ANDIo8) |
| 1257 | NewOpC = MIOpC; |
| 1258 | else { |
| 1259 | NewOpC = PPC::getRecordFormOpcode(MIOpC); |
| 1260 | if (NewOpC == -1 && PPC::getNonRecordFormOpcode(MIOpC) != -1) |
| 1261 | NewOpC = MIOpC; |
| 1262 | } |
| 1263 | |
| 1264 | // FIXME: On the non-embedded POWER architectures, only some of the record |
| 1265 | // forms are fast, and we should use only the fast ones. |
| 1266 | |
| 1267 | // The defining instruction has a record form (or is already a record |
| 1268 | // form). It is possible, however, that we'll need to reverse the condition |
| 1269 | // code of the users. |
| 1270 | if (NewOpC == -1) |
| 1271 | return false; |
| 1272 | |
| 1273 | SmallVector<std::pair<MachineOperand*, PPC::Predicate>, 4> |
| 1274 | OperandsToUpdate; |
| 1275 | SmallVector<std::pair<MachineOperand*, MachineOperand*>, 4> |
| 1276 | OperandsToSwap; |
| 1277 | |
| 1278 | // If we have SUB(r1, r2) and CMP(r2, r1), the condition code based on CMP |
| 1279 | // needs to be updated to be based on SUB. Push the condition code |
| 1280 | // operands to OperandsToUpdate. If it is safe to remove CmpInstr, the |
| 1281 | // condition code of these operands will be modified. |
| 1282 | bool ShouldSwap = false; |
| 1283 | if (Sub) { |
| 1284 | ShouldSwap = SrcReg2 != 0 && Sub->getOperand(1).getReg() == SrcReg2 && |
| 1285 | Sub->getOperand(2).getReg() == SrcReg; |
| 1286 | |
| 1287 | // The operands to subf are the opposite of sub, so only in the fixed-point |
| 1288 | // case, invert the order. |
| 1289 | if (!isFP) |
| 1290 | ShouldSwap = !ShouldSwap; |
| 1291 | } |
| 1292 | |
| 1293 | if (ShouldSwap) |
| 1294 | for (MachineRegisterInfo::use_iterator I = MRI->use_begin(CRReg), |
| 1295 | IE = MRI->use_end(); I != IE; ++I) { |
| 1296 | MachineInstr *UseMI = &*I; |
| 1297 | if (UseMI->getOpcode() == PPC::BCC) { |
| 1298 | PPC::Predicate Pred = (PPC::Predicate) UseMI->getOperand(0).getImm(); |
| 1299 | if (ShouldSwap) |
| 1300 | OperandsToUpdate.push_back(std::make_pair(&((*I).getOperand(0)), |
| 1301 | PPC::InvertPredicate(Pred))); |
| 1302 | } else if (UseMI->getOpcode() == PPC::ISEL || |
| 1303 | UseMI->getOpcode() == PPC::ISEL8) { |
| 1304 | if (ShouldSwap) |
| 1305 | OperandsToSwap.push_back(std::make_pair(&((*I).getOperand(1)), |
| 1306 | &((*I).getOperand(2)))); |
| 1307 | } else // We need to abort on a user we don't understand. |
| 1308 | return false; |
| 1309 | } |
| 1310 | |
| 1311 | // Create a new virtual register to hold the value of the CR set by the |
| 1312 | // record-form instruction. If the instruction was not previously in |
| 1313 | // record form, then set the kill flag on the CR. |
| 1314 | CmpInstr->eraseFromParent(); |
| 1315 | |
| 1316 | MachineBasicBlock::iterator MII = MI; |
| 1317 | BuildMI(*MI->getParent(), llvm::next(MII), MI->getDebugLoc(), |
| 1318 | get(TargetOpcode::COPY), CRReg) |
| 1319 | .addReg(CRRecReg, MIOpC != NewOpC ? RegState::Kill : 0); |
| 1320 | |
| 1321 | if (MIOpC != NewOpC) { |
| 1322 | // We need to be careful here: we're replacing one instruction with |
| 1323 | // another, and we need to make sure that we get all of the right |
| 1324 | // implicit uses and defs. On the other hand, the caller may be holding |
| 1325 | // an iterator to this instruction, and so we can't delete it (this is |
| 1326 | // specifically the case if this is the instruction directly after the |
| 1327 | // compare). |
| 1328 | |
| 1329 | const MCInstrDesc &NewDesc = get(NewOpC); |
| 1330 | MI->setDesc(NewDesc); |
| 1331 | |
| 1332 | if (NewDesc.ImplicitDefs) |
| 1333 | for (const uint16_t *ImpDefs = NewDesc.getImplicitDefs(); |
| 1334 | *ImpDefs; ++ImpDefs) |
| 1335 | if (!MI->definesRegister(*ImpDefs)) |
| 1336 | MI->addOperand(*MI->getParent()->getParent(), |
| 1337 | MachineOperand::CreateReg(*ImpDefs, true, true)); |
| 1338 | if (NewDesc.ImplicitUses) |
| 1339 | for (const uint16_t *ImpUses = NewDesc.getImplicitUses(); |
| 1340 | *ImpUses; ++ImpUses) |
| 1341 | if (!MI->readsRegister(*ImpUses)) |
| 1342 | MI->addOperand(*MI->getParent()->getParent(), |
| 1343 | MachineOperand::CreateReg(*ImpUses, false, true)); |
| 1344 | } |
| 1345 | |
| 1346 | // Modify the condition code of operands in OperandsToUpdate. |
| 1347 | // Since we have SUB(r1, r2) and CMP(r2, r1), the condition code needs to |
| 1348 | // be changed from r2 > r1 to r1 < r2, from r2 < r1 to r1 > r2, etc. |
| 1349 | for (unsigned i = 0, e = OperandsToUpdate.size(); i < e; i++) |
| 1350 | OperandsToUpdate[i].first->setImm(OperandsToUpdate[i].second); |
| 1351 | |
| 1352 | for (unsigned i = 0, e = OperandsToSwap.size(); i < e; i++) |
| 1353 | std::swap(*OperandsToSwap[i].first, *OperandsToSwap[i].second); |
| 1354 | |
| 1355 | return true; |
| 1356 | } |
| 1357 | |
Nicolas Geoffray | 52e724a | 2008-04-16 20:10:13 +0000 | [diff] [blame] | 1358 | /// GetInstSize - Return the number of bytes of code the specified |
| 1359 | /// instruction may be. This returns the maximum number of bytes. |
| 1360 | /// |
| 1361 | unsigned PPCInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const { |
| 1362 | switch (MI->getOpcode()) { |
| 1363 | case PPC::INLINEASM: { // Inline Asm: Variable size. |
| 1364 | const MachineFunction *MF = MI->getParent()->getParent(); |
| 1365 | const char *AsmStr = MI->getOperand(0).getSymbolName(); |
Chris Lattner | af76e59 | 2009-08-22 20:48:53 +0000 | [diff] [blame] | 1366 | return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo()); |
Nicolas Geoffray | 52e724a | 2008-04-16 20:10:13 +0000 | [diff] [blame] | 1367 | } |
Bill Wendling | 7431bea | 2010-07-16 22:20:36 +0000 | [diff] [blame] | 1368 | case PPC::PROLOG_LABEL: |
Dan Gohman | 4406604 | 2008-07-01 00:05:16 +0000 | [diff] [blame] | 1369 | case PPC::EH_LABEL: |
| 1370 | case PPC::GC_LABEL: |
Dale Johannesen | 375be77 | 2010-04-07 19:51:44 +0000 | [diff] [blame] | 1371 | case PPC::DBG_VALUE: |
Nicolas Geoffray | 52e724a | 2008-04-16 20:10:13 +0000 | [diff] [blame] | 1372 | return 0; |
Ulrich Weigand | 86765fb | 2013-03-22 15:24:13 +0000 | [diff] [blame] | 1373 | case PPC::BL8_NOP: |
| 1374 | case PPC::BLA8_NOP: |
Hal Finkel | 5b00cea | 2012-03-31 14:45:15 +0000 | [diff] [blame] | 1375 | return 8; |
Nicolas Geoffray | 52e724a | 2008-04-16 20:10:13 +0000 | [diff] [blame] | 1376 | default: |
| 1377 | return 4; // PowerPC instructions are all 4 bytes |
| 1378 | } |
| 1379 | } |
Hal Finkel | 5ee67e8 | 2013-04-08 16:24:03 +0000 | [diff] [blame] | 1380 | |
| 1381 | #undef DEBUG_TYPE |
| 1382 | #define DEBUG_TYPE "ppc-early-ret" |
| 1383 | STATISTIC(NumBCLR, "Number of early conditional returns"); |
| 1384 | STATISTIC(NumBLR, "Number of early returns"); |
| 1385 | |
| 1386 | namespace llvm { |
| 1387 | void initializePPCEarlyReturnPass(PassRegistry&); |
| 1388 | } |
| 1389 | |
| 1390 | namespace { |
| 1391 | // PPCEarlyReturn pass - For simple functions without epilogue code, move |
| 1392 | // returns up, and create conditional returns, to avoid unnecessary |
| 1393 | // branch-to-blr sequences. |
| 1394 | struct PPCEarlyReturn : public MachineFunctionPass { |
| 1395 | static char ID; |
| 1396 | PPCEarlyReturn() : MachineFunctionPass(ID) { |
| 1397 | initializePPCEarlyReturnPass(*PassRegistry::getPassRegistry()); |
| 1398 | } |
| 1399 | |
| 1400 | const PPCTargetMachine *TM; |
| 1401 | const PPCInstrInfo *TII; |
| 1402 | |
| 1403 | protected: |
Hal Finkel | 13049ae | 2013-04-09 18:25:18 +0000 | [diff] [blame] | 1404 | bool processBlock(MachineBasicBlock &ReturnMBB) { |
Hal Finkel | 5ee67e8 | 2013-04-08 16:24:03 +0000 | [diff] [blame] | 1405 | bool Changed = false; |
| 1406 | |
Hal Finkel | 13049ae | 2013-04-09 18:25:18 +0000 | [diff] [blame] | 1407 | MachineBasicBlock::iterator I = ReturnMBB.begin(); |
| 1408 | I = ReturnMBB.SkipPHIsAndLabels(I); |
Hal Finkel | 5ee67e8 | 2013-04-08 16:24:03 +0000 | [diff] [blame] | 1409 | |
| 1410 | // The block must be essentially empty except for the blr. |
Hal Finkel | 13049ae | 2013-04-09 18:25:18 +0000 | [diff] [blame] | 1411 | if (I == ReturnMBB.end() || I->getOpcode() != PPC::BLR || |
| 1412 | I != ReturnMBB.getLastNonDebugInstr()) |
Hal Finkel | 5ee67e8 | 2013-04-08 16:24:03 +0000 | [diff] [blame] | 1413 | return Changed; |
| 1414 | |
| 1415 | SmallVector<MachineBasicBlock*, 8> PredToRemove; |
Hal Finkel | 13049ae | 2013-04-09 18:25:18 +0000 | [diff] [blame] | 1416 | for (MachineBasicBlock::pred_iterator PI = ReturnMBB.pred_begin(), |
| 1417 | PIE = ReturnMBB.pred_end(); PI != PIE; ++PI) { |
Hal Finkel | 5ee67e8 | 2013-04-08 16:24:03 +0000 | [diff] [blame] | 1418 | bool OtherReference = false, BlockChanged = false; |
Hal Finkel | 13049ae | 2013-04-09 18:25:18 +0000 | [diff] [blame] | 1419 | for (MachineBasicBlock::iterator J = (*PI)->getLastNonDebugInstr();;) { |
Hal Finkel | 5ee67e8 | 2013-04-08 16:24:03 +0000 | [diff] [blame] | 1420 | if (J->getOpcode() == PPC::B) { |
Hal Finkel | 13049ae | 2013-04-09 18:25:18 +0000 | [diff] [blame] | 1421 | if (J->getOperand(0).getMBB() == &ReturnMBB) { |
Hal Finkel | 5ee67e8 | 2013-04-08 16:24:03 +0000 | [diff] [blame] | 1422 | // This is an unconditional branch to the return. Replace the |
| 1423 | // branch with a blr. |
| 1424 | BuildMI(**PI, J, J->getDebugLoc(), TII->get(PPC::BLR)); |
Hal Finkel | 13049ae | 2013-04-09 18:25:18 +0000 | [diff] [blame] | 1425 | MachineBasicBlock::iterator K = J--; |
Hal Finkel | 5ee67e8 | 2013-04-08 16:24:03 +0000 | [diff] [blame] | 1426 | K->eraseFromParent(); |
| 1427 | BlockChanged = true; |
| 1428 | ++NumBLR; |
| 1429 | continue; |
| 1430 | } |
| 1431 | } else if (J->getOpcode() == PPC::BCC) { |
Hal Finkel | 13049ae | 2013-04-09 18:25:18 +0000 | [diff] [blame] | 1432 | if (J->getOperand(2).getMBB() == &ReturnMBB) { |
Hal Finkel | 5ee67e8 | 2013-04-08 16:24:03 +0000 | [diff] [blame] | 1433 | // This is a conditional branch to the return. Replace the branch |
| 1434 | // with a bclr. |
| 1435 | BuildMI(**PI, J, J->getDebugLoc(), TII->get(PPC::BCLR)) |
| 1436 | .addImm(J->getOperand(0).getImm()) |
| 1437 | .addReg(J->getOperand(1).getReg()); |
Hal Finkel | 13049ae | 2013-04-09 18:25:18 +0000 | [diff] [blame] | 1438 | MachineBasicBlock::iterator K = J--; |
Hal Finkel | 5ee67e8 | 2013-04-08 16:24:03 +0000 | [diff] [blame] | 1439 | K->eraseFromParent(); |
| 1440 | BlockChanged = true; |
| 1441 | ++NumBCLR; |
| 1442 | continue; |
| 1443 | } |
| 1444 | } else if (J->isBranch()) { |
| 1445 | if (J->isIndirectBranch()) { |
Hal Finkel | 13049ae | 2013-04-09 18:25:18 +0000 | [diff] [blame] | 1446 | if (ReturnMBB.hasAddressTaken()) |
Hal Finkel | 5ee67e8 | 2013-04-08 16:24:03 +0000 | [diff] [blame] | 1447 | OtherReference = true; |
| 1448 | } else |
| 1449 | for (unsigned i = 0; i < J->getNumOperands(); ++i) |
| 1450 | if (J->getOperand(i).isMBB() && |
Hal Finkel | 13049ae | 2013-04-09 18:25:18 +0000 | [diff] [blame] | 1451 | J->getOperand(i).getMBB() == &ReturnMBB) |
Hal Finkel | 5ee67e8 | 2013-04-08 16:24:03 +0000 | [diff] [blame] | 1452 | OtherReference = true; |
Hal Finkel | 13049ae | 2013-04-09 18:25:18 +0000 | [diff] [blame] | 1453 | } else if (!J->isTerminator() && !J->isDebugValue()) |
| 1454 | break; |
Hal Finkel | 5ee67e8 | 2013-04-08 16:24:03 +0000 | [diff] [blame] | 1455 | |
Hal Finkel | 13049ae | 2013-04-09 18:25:18 +0000 | [diff] [blame] | 1456 | if (J == (*PI)->begin()) |
| 1457 | break; |
| 1458 | |
| 1459 | --J; |
Hal Finkel | 5ee67e8 | 2013-04-08 16:24:03 +0000 | [diff] [blame] | 1460 | } |
| 1461 | |
Hal Finkel | 13049ae | 2013-04-09 18:25:18 +0000 | [diff] [blame] | 1462 | if ((*PI)->canFallThrough() && (*PI)->isLayoutSuccessor(&ReturnMBB)) |
Hal Finkel | 5ee67e8 | 2013-04-08 16:24:03 +0000 | [diff] [blame] | 1463 | OtherReference = true; |
| 1464 | |
| 1465 | // Predecessors are stored in a vector and can't be removed here. |
| 1466 | if (!OtherReference && BlockChanged) { |
| 1467 | PredToRemove.push_back(*PI); |
| 1468 | } |
| 1469 | |
| 1470 | if (BlockChanged) |
| 1471 | Changed = true; |
| 1472 | } |
| 1473 | |
| 1474 | for (unsigned i = 0, ie = PredToRemove.size(); i != ie; ++i) |
Hal Finkel | 13049ae | 2013-04-09 18:25:18 +0000 | [diff] [blame] | 1475 | PredToRemove[i]->removeSuccessor(&ReturnMBB); |
Hal Finkel | 5ee67e8 | 2013-04-08 16:24:03 +0000 | [diff] [blame] | 1476 | |
Hal Finkel | 13049ae | 2013-04-09 18:25:18 +0000 | [diff] [blame] | 1477 | if (Changed && !ReturnMBB.hasAddressTaken()) { |
Hal Finkel | 5ee67e8 | 2013-04-08 16:24:03 +0000 | [diff] [blame] | 1478 | // We now might be able to merge this blr-only block into its |
| 1479 | // by-layout predecessor. |
Hal Finkel | 13049ae | 2013-04-09 18:25:18 +0000 | [diff] [blame] | 1480 | if (ReturnMBB.pred_size() == 1 && |
| 1481 | (*ReturnMBB.pred_begin())->isLayoutSuccessor(&ReturnMBB)) { |
Hal Finkel | 5ee67e8 | 2013-04-08 16:24:03 +0000 | [diff] [blame] | 1482 | // Move the blr into the preceding block. |
Hal Finkel | 13049ae | 2013-04-09 18:25:18 +0000 | [diff] [blame] | 1483 | MachineBasicBlock &PrevMBB = **ReturnMBB.pred_begin(); |
| 1484 | PrevMBB.splice(PrevMBB.end(), &ReturnMBB, I); |
| 1485 | PrevMBB.removeSuccessor(&ReturnMBB); |
Hal Finkel | 5ee67e8 | 2013-04-08 16:24:03 +0000 | [diff] [blame] | 1486 | } |
| 1487 | |
Hal Finkel | 13049ae | 2013-04-09 18:25:18 +0000 | [diff] [blame] | 1488 | if (ReturnMBB.pred_empty()) |
| 1489 | ReturnMBB.eraseFromParent(); |
Hal Finkel | 5ee67e8 | 2013-04-08 16:24:03 +0000 | [diff] [blame] | 1490 | } |
| 1491 | |
| 1492 | return Changed; |
| 1493 | } |
| 1494 | |
| 1495 | public: |
| 1496 | virtual bool runOnMachineFunction(MachineFunction &MF) { |
| 1497 | TM = static_cast<const PPCTargetMachine *>(&MF.getTarget()); |
| 1498 | TII = TM->getInstrInfo(); |
| 1499 | |
| 1500 | bool Changed = false; |
| 1501 | |
Hal Finkel | 13049ae | 2013-04-09 18:25:18 +0000 | [diff] [blame] | 1502 | // If the function does not have at least two blocks, then there is |
Hal Finkel | 5ee67e8 | 2013-04-08 16:24:03 +0000 | [diff] [blame] | 1503 | // nothing to do. |
| 1504 | if (MF.size() < 2) |
| 1505 | return Changed; |
| 1506 | |
| 1507 | for (MachineFunction::iterator I = MF.begin(); I != MF.end();) { |
| 1508 | MachineBasicBlock &B = *I++; |
| 1509 | if (processBlock(B)) |
| 1510 | Changed = true; |
| 1511 | } |
| 1512 | |
| 1513 | return Changed; |
| 1514 | } |
| 1515 | |
| 1516 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 1517 | MachineFunctionPass::getAnalysisUsage(AU); |
| 1518 | } |
| 1519 | }; |
| 1520 | } |
| 1521 | |
| 1522 | INITIALIZE_PASS(PPCEarlyReturn, DEBUG_TYPE, |
| 1523 | "PowerPC Early-Return Creation", false, false) |
| 1524 | |
| 1525 | char PPCEarlyReturn::ID = 0; |
| 1526 | FunctionPass* |
| 1527 | llvm::createPPCEarlyReturnPass() { return new PPCEarlyReturn(); } |
| 1528 | |