Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 1 | //===- SPUInstrInfo.cpp - Cell SPU Instruction Information ----------------===// |
Scott Michel | 6637752 | 2007-12-04 22:35:58 +0000 | [diff] [blame] | 2 | // |
| 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. |
Scott Michel | 6637752 | 2007-12-04 22:35:58 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file contains the Cell SPU implementation of the TargetInstrInfo class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "SPURegisterNames.h" |
| 15 | #include "SPUInstrInfo.h" |
Owen Anderson | f6372aa | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 16 | #include "SPUInstrBuilder.h" |
Scott Michel | 6637752 | 2007-12-04 22:35:58 +0000 | [diff] [blame] | 17 | #include "SPUTargetMachine.h" |
| 18 | #include "SPUGenInstrInfo.inc" |
Andrew Trick | 2da8bc8 | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 19 | #include "SPUHazardRecognizers.h" |
Scott Michel | 6637752 | 2007-12-04 22:35:58 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Scott Michel | 9bd7a37 | 2009-01-02 20:52:08 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Debug.h" |
Torok Edwin | dac237e | 2009-07-08 20:53:28 +0000 | [diff] [blame] | 22 | #include "llvm/Support/ErrorHandling.h" |
Benjamin Kramer | 072a56e | 2009-08-23 11:52:17 +0000 | [diff] [blame] | 23 | #include "llvm/Support/raw_ostream.h" |
Scott Michel | 6637752 | 2007-12-04 22:35:58 +0000 | [diff] [blame] | 24 | |
| 25 | using namespace llvm; |
| 26 | |
Scott Michel | aedc637 | 2008-12-10 00:15:19 +0000 | [diff] [blame] | 27 | namespace { |
| 28 | //! Predicate for an unconditional branch instruction |
| 29 | inline bool isUncondBranch(const MachineInstr *I) { |
| 30 | unsigned opc = I->getOpcode(); |
| 31 | |
| 32 | return (opc == SPU::BR |
Scott Michel | 19c10e6 | 2009-01-26 03:37:41 +0000 | [diff] [blame] | 33 | || opc == SPU::BRA |
| 34 | || opc == SPU::BI); |
Scott Michel | aedc637 | 2008-12-10 00:15:19 +0000 | [diff] [blame] | 35 | } |
| 36 | |
Scott Michel | 52d0001 | 2009-01-03 00:27:53 +0000 | [diff] [blame] | 37 | //! Predicate for a conditional branch instruction |
Scott Michel | aedc637 | 2008-12-10 00:15:19 +0000 | [diff] [blame] | 38 | inline bool isCondBranch(const MachineInstr *I) { |
| 39 | unsigned opc = I->getOpcode(); |
| 40 | |
Scott Michel | f0569be | 2008-12-27 04:51:36 +0000 | [diff] [blame] | 41 | return (opc == SPU::BRNZr32 |
| 42 | || opc == SPU::BRNZv4i32 |
Scott Michel | 19c10e6 | 2009-01-26 03:37:41 +0000 | [diff] [blame] | 43 | || opc == SPU::BRZr32 |
| 44 | || opc == SPU::BRZv4i32 |
| 45 | || opc == SPU::BRHNZr16 |
| 46 | || opc == SPU::BRHNZv8i16 |
| 47 | || opc == SPU::BRHZr16 |
| 48 | || opc == SPU::BRHZv8i16); |
Scott Michel | aedc637 | 2008-12-10 00:15:19 +0000 | [diff] [blame] | 49 | } |
| 50 | } |
| 51 | |
Scott Michel | 6637752 | 2007-12-04 22:35:58 +0000 | [diff] [blame] | 52 | SPUInstrInfo::SPUInstrInfo(SPUTargetMachine &tm) |
Chris Lattner | 6410552 | 2008-01-01 01:03:04 +0000 | [diff] [blame] | 53 | : TargetInstrInfoImpl(SPUInsts, sizeof(SPUInsts)/sizeof(SPUInsts[0])), |
Scott Michel | 6637752 | 2007-12-04 22:35:58 +0000 | [diff] [blame] | 54 | TM(tm), |
| 55 | RI(*TM.getSubtargetImpl(), *this) |
Scott Michel | 52d0001 | 2009-01-03 00:27:53 +0000 | [diff] [blame] | 56 | { /* NOP */ } |
Scott Michel | 6637752 | 2007-12-04 22:35:58 +0000 | [diff] [blame] | 57 | |
Andrew Trick | 2da8bc8 | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 58 | /// CreateTargetHazardRecognizer - Return the hazard recognizer to use for |
| 59 | /// this target when scheduling the DAG. |
| 60 | ScheduleHazardRecognizer *SPUInstrInfo::CreateTargetHazardRecognizer( |
| 61 | const TargetMachine *TM, |
| 62 | const ScheduleDAG *DAG) const { |
| 63 | const TargetInstrInfo *TII = TM->getInstrInfo(); |
| 64 | assert(TII && "No InstrInfo?"); |
| 65 | return new SPUHazardRecognizer(*TII); |
| 66 | } |
| 67 | |
Scott Michel | 6637752 | 2007-12-04 22:35:58 +0000 | [diff] [blame] | 68 | unsigned |
Dan Gohman | cbad42c | 2008-11-18 19:49:32 +0000 | [diff] [blame] | 69 | SPUInstrInfo::isLoadFromStackSlot(const MachineInstr *MI, |
| 70 | int &FrameIndex) const { |
Scott Michel | 6637752 | 2007-12-04 22:35:58 +0000 | [diff] [blame] | 71 | switch (MI->getOpcode()) { |
| 72 | default: break; |
| 73 | case SPU::LQDv16i8: |
| 74 | case SPU::LQDv8i16: |
| 75 | case SPU::LQDv4i32: |
| 76 | case SPU::LQDv4f32: |
| 77 | case SPU::LQDv2f64: |
| 78 | case SPU::LQDr128: |
| 79 | case SPU::LQDr64: |
| 80 | case SPU::LQDr32: |
Scott Michel | aedc637 | 2008-12-10 00:15:19 +0000 | [diff] [blame] | 81 | case SPU::LQDr16: { |
| 82 | const MachineOperand MOp1 = MI->getOperand(1); |
| 83 | const MachineOperand MOp2 = MI->getOperand(2); |
Scott Michel | 52d0001 | 2009-01-03 00:27:53 +0000 | [diff] [blame] | 84 | if (MOp1.isImm() && MOp2.isFI()) { |
| 85 | FrameIndex = MOp2.getIndex(); |
Scott Michel | aedc637 | 2008-12-10 00:15:19 +0000 | [diff] [blame] | 86 | return MI->getOperand(0).getReg(); |
| 87 | } |
| 88 | break; |
| 89 | } |
Scott Michel | 6637752 | 2007-12-04 22:35:58 +0000 | [diff] [blame] | 90 | } |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | unsigned |
Dan Gohman | cbad42c | 2008-11-18 19:49:32 +0000 | [diff] [blame] | 95 | SPUInstrInfo::isStoreToStackSlot(const MachineInstr *MI, |
| 96 | int &FrameIndex) const { |
Scott Michel | 6637752 | 2007-12-04 22:35:58 +0000 | [diff] [blame] | 97 | switch (MI->getOpcode()) { |
| 98 | default: break; |
| 99 | case SPU::STQDv16i8: |
| 100 | case SPU::STQDv8i16: |
| 101 | case SPU::STQDv4i32: |
| 102 | case SPU::STQDv4f32: |
| 103 | case SPU::STQDv2f64: |
| 104 | case SPU::STQDr128: |
| 105 | case SPU::STQDr64: |
| 106 | case SPU::STQDr32: |
| 107 | case SPU::STQDr16: |
Scott Michel | aedc637 | 2008-12-10 00:15:19 +0000 | [diff] [blame] | 108 | case SPU::STQDr8: { |
| 109 | const MachineOperand MOp1 = MI->getOperand(1); |
| 110 | const MachineOperand MOp2 = MI->getOperand(2); |
Scott Michel | f0569be | 2008-12-27 04:51:36 +0000 | [diff] [blame] | 111 | if (MOp1.isImm() && MOp2.isFI()) { |
| 112 | FrameIndex = MOp2.getIndex(); |
Scott Michel | aedc637 | 2008-12-10 00:15:19 +0000 | [diff] [blame] | 113 | return MI->getOperand(0).getReg(); |
| 114 | } |
| 115 | break; |
| 116 | } |
Scott Michel | 6637752 | 2007-12-04 22:35:58 +0000 | [diff] [blame] | 117 | } |
| 118 | return 0; |
| 119 | } |
Owen Anderson | d10fd97 | 2007-12-31 06:32:00 +0000 | [diff] [blame] | 120 | |
Jakob Stoklund Olesen | 377b7b7 | 2010-07-11 07:31:03 +0000 | [diff] [blame] | 121 | void SPUInstrInfo::copyPhysReg(MachineBasicBlock &MBB, |
| 122 | MachineBasicBlock::iterator I, DebugLoc DL, |
| 123 | unsigned DestReg, unsigned SrcReg, |
| 124 | bool KillSrc) const |
Owen Anderson | d10fd97 | 2007-12-31 06:32:00 +0000 | [diff] [blame] | 125 | { |
Chris Lattner | 5e09da2 | 2008-03-09 20:31:11 +0000 | [diff] [blame] | 126 | // We support cross register class moves for our aliases, such as R3 in any |
| 127 | // reg class to any other reg class containing R3. This is required because |
| 128 | // we instruction select bitconvert i64 -> f64 as a noop for example, so our |
| 129 | // types have no specific meaning. |
Scott Michel | 02d711b | 2008-12-30 23:28:25 +0000 | [diff] [blame] | 130 | |
Jakob Stoklund Olesen | 377b7b7 | 2010-07-11 07:31:03 +0000 | [diff] [blame] | 131 | BuildMI(MBB, I, DL, get(SPU::LRr128), DestReg) |
| 132 | .addReg(SrcReg, getKillRegState(KillSrc)); |
Owen Anderson | d10fd97 | 2007-12-31 06:32:00 +0000 | [diff] [blame] | 133 | } |
Owen Anderson | f6372aa | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 134 | |
| 135 | void |
| 136 | SPUInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB, |
Evan Cheng | 746ad69 | 2010-05-06 19:06:44 +0000 | [diff] [blame] | 137 | MachineBasicBlock::iterator MI, |
| 138 | unsigned SrcReg, bool isKill, int FrameIdx, |
| 139 | const TargetRegisterClass *RC, |
| 140 | const TargetRegisterInfo *TRI) const |
Owen Anderson | f6372aa | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 141 | { |
Chris Lattner | cc8cd0c | 2008-01-07 02:48:55 +0000 | [diff] [blame] | 142 | unsigned opc; |
Anton Korobeynikov | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame^] | 143 | bool isValidFrameIdx = (FrameIdx < SPUFrameLowering::maxFrameOffset()); |
Owen Anderson | f6372aa | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 144 | if (RC == SPU::GPRCRegisterClass) { |
Scott Michel | aedc637 | 2008-12-10 00:15:19 +0000 | [diff] [blame] | 145 | opc = (isValidFrameIdx ? SPU::STQDr128 : SPU::STQXr128); |
Owen Anderson | f6372aa | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 146 | } else if (RC == SPU::R64CRegisterClass) { |
Scott Michel | aedc637 | 2008-12-10 00:15:19 +0000 | [diff] [blame] | 147 | opc = (isValidFrameIdx ? SPU::STQDr64 : SPU::STQXr64); |
Owen Anderson | f6372aa | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 148 | } else if (RC == SPU::R64FPRegisterClass) { |
Scott Michel | aedc637 | 2008-12-10 00:15:19 +0000 | [diff] [blame] | 149 | opc = (isValidFrameIdx ? SPU::STQDr64 : SPU::STQXr64); |
Owen Anderson | f6372aa | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 150 | } else if (RC == SPU::R32CRegisterClass) { |
Scott Michel | aedc637 | 2008-12-10 00:15:19 +0000 | [diff] [blame] | 151 | opc = (isValidFrameIdx ? SPU::STQDr32 : SPU::STQXr32); |
Owen Anderson | f6372aa | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 152 | } else if (RC == SPU::R32FPRegisterClass) { |
Scott Michel | aedc637 | 2008-12-10 00:15:19 +0000 | [diff] [blame] | 153 | opc = (isValidFrameIdx ? SPU::STQDr32 : SPU::STQXr32); |
Owen Anderson | f6372aa | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 154 | } else if (RC == SPU::R16CRegisterClass) { |
Scott Michel | aedc637 | 2008-12-10 00:15:19 +0000 | [diff] [blame] | 155 | opc = (isValidFrameIdx ? SPU::STQDr16 : SPU::STQXr16); |
| 156 | } else if (RC == SPU::R8CRegisterClass) { |
| 157 | opc = (isValidFrameIdx ? SPU::STQDr8 : SPU::STQXr8); |
Scott Michel | f0569be | 2008-12-27 04:51:36 +0000 | [diff] [blame] | 158 | } else if (RC == SPU::VECREGRegisterClass) { |
| 159 | opc = (isValidFrameIdx) ? SPU::STQDv16i8 : SPU::STQXv16i8; |
Owen Anderson | f6372aa | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 160 | } else { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 161 | llvm_unreachable("Unknown regclass!"); |
Owen Anderson | f6372aa | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Chris Lattner | c7f3ace | 2010-04-02 20:16:16 +0000 | [diff] [blame] | 164 | DebugLoc DL; |
Bill Wendling | d1c321a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 165 | if (MI != MBB.end()) DL = MI->getDebugLoc(); |
| 166 | addFrameReference(BuildMI(MBB, MI, DL, get(opc)) |
Bill Wendling | 587daed | 2009-05-13 21:33:08 +0000 | [diff] [blame] | 167 | .addReg(SrcReg, getKillRegState(isKill)), FrameIdx); |
Owen Anderson | f6372aa | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Owen Anderson | f6372aa | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 170 | void |
| 171 | SPUInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB, |
Evan Cheng | 746ad69 | 2010-05-06 19:06:44 +0000 | [diff] [blame] | 172 | MachineBasicBlock::iterator MI, |
| 173 | unsigned DestReg, int FrameIdx, |
| 174 | const TargetRegisterClass *RC, |
| 175 | const TargetRegisterInfo *TRI) const |
Owen Anderson | f6372aa | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 176 | { |
Chris Lattner | cc8cd0c | 2008-01-07 02:48:55 +0000 | [diff] [blame] | 177 | unsigned opc; |
Anton Korobeynikov | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame^] | 178 | bool isValidFrameIdx = (FrameIdx < SPUFrameLowering::maxFrameOffset()); |
Owen Anderson | f6372aa | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 179 | if (RC == SPU::GPRCRegisterClass) { |
Scott Michel | aedc637 | 2008-12-10 00:15:19 +0000 | [diff] [blame] | 180 | opc = (isValidFrameIdx ? SPU::LQDr128 : SPU::LQXr128); |
Owen Anderson | f6372aa | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 181 | } else if (RC == SPU::R64CRegisterClass) { |
Scott Michel | aedc637 | 2008-12-10 00:15:19 +0000 | [diff] [blame] | 182 | opc = (isValidFrameIdx ? SPU::LQDr64 : SPU::LQXr64); |
Owen Anderson | f6372aa | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 183 | } else if (RC == SPU::R64FPRegisterClass) { |
Scott Michel | aedc637 | 2008-12-10 00:15:19 +0000 | [diff] [blame] | 184 | opc = (isValidFrameIdx ? SPU::LQDr64 : SPU::LQXr64); |
Owen Anderson | f6372aa | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 185 | } else if (RC == SPU::R32CRegisterClass) { |
Scott Michel | aedc637 | 2008-12-10 00:15:19 +0000 | [diff] [blame] | 186 | opc = (isValidFrameIdx ? SPU::LQDr32 : SPU::LQXr32); |
Owen Anderson | f6372aa | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 187 | } else if (RC == SPU::R32FPRegisterClass) { |
Scott Michel | aedc637 | 2008-12-10 00:15:19 +0000 | [diff] [blame] | 188 | opc = (isValidFrameIdx ? SPU::LQDr32 : SPU::LQXr32); |
Owen Anderson | f6372aa | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 189 | } else if (RC == SPU::R16CRegisterClass) { |
Scott Michel | aedc637 | 2008-12-10 00:15:19 +0000 | [diff] [blame] | 190 | opc = (isValidFrameIdx ? SPU::LQDr16 : SPU::LQXr16); |
| 191 | } else if (RC == SPU::R8CRegisterClass) { |
| 192 | opc = (isValidFrameIdx ? SPU::LQDr8 : SPU::LQXr8); |
Scott Michel | f0569be | 2008-12-27 04:51:36 +0000 | [diff] [blame] | 193 | } else if (RC == SPU::VECREGRegisterClass) { |
| 194 | opc = (isValidFrameIdx) ? SPU::LQDv16i8 : SPU::LQXv16i8; |
Owen Anderson | f6372aa | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 195 | } else { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 196 | llvm_unreachable("Unknown regclass in loadRegFromStackSlot!"); |
Owen Anderson | f6372aa | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Chris Lattner | c7f3ace | 2010-04-02 20:16:16 +0000 | [diff] [blame] | 199 | DebugLoc DL; |
Bill Wendling | d1c321a | 2009-02-12 00:02:55 +0000 | [diff] [blame] | 200 | if (MI != MBB.end()) DL = MI->getDebugLoc(); |
Jakob Stoklund Olesen | f2c3f6a | 2009-05-16 07:25:44 +0000 | [diff] [blame] | 201 | addFrameReference(BuildMI(MBB, MI, DL, get(opc), DestReg), FrameIdx); |
Owen Anderson | f6372aa | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Scott Michel | aedc637 | 2008-12-10 00:15:19 +0000 | [diff] [blame] | 204 | //! Branch analysis |
Scott Michel | 9bd7a37 | 2009-01-02 20:52:08 +0000 | [diff] [blame] | 205 | /*! |
Scott Michel | aedc637 | 2008-12-10 00:15:19 +0000 | [diff] [blame] | 206 | \note This code was kiped from PPC. There may be more branch analysis for |
| 207 | CellSPU than what's currently done here. |
| 208 | */ |
| 209 | bool |
| 210 | SPUInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB, |
Scott Michel | 19c10e6 | 2009-01-26 03:37:41 +0000 | [diff] [blame] | 211 | MachineBasicBlock *&FBB, |
Evan Cheng | dc54d31 | 2009-02-09 07:14:22 +0000 | [diff] [blame] | 212 | SmallVectorImpl<MachineOperand> &Cond, |
| 213 | bool AllowModify) const { |
Scott Michel | aedc637 | 2008-12-10 00:15:19 +0000 | [diff] [blame] | 214 | // If the block has no terminators, it just falls into the block after it. |
| 215 | MachineBasicBlock::iterator I = MBB.end(); |
Dale Johannesen | 93d6a7e | 2010-04-02 01:38:09 +0000 | [diff] [blame] | 216 | if (I == MBB.begin()) |
| 217 | return false; |
| 218 | --I; |
| 219 | while (I->isDebugValue()) { |
| 220 | if (I == MBB.begin()) |
| 221 | return false; |
| 222 | --I; |
| 223 | } |
| 224 | if (!isUnpredicatedTerminator(I)) |
Scott Michel | aedc637 | 2008-12-10 00:15:19 +0000 | [diff] [blame] | 225 | return false; |
| 226 | |
| 227 | // Get the last instruction in the block. |
| 228 | MachineInstr *LastInst = I; |
Scott Michel | 02d711b | 2008-12-30 23:28:25 +0000 | [diff] [blame] | 229 | |
Scott Michel | aedc637 | 2008-12-10 00:15:19 +0000 | [diff] [blame] | 230 | // If there is only one terminator instruction, process it. |
| 231 | if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) { |
| 232 | if (isUncondBranch(LastInst)) { |
Kalle Raiskila | 2320a44 | 2010-05-11 11:00:02 +0000 | [diff] [blame] | 233 | // Check for jump tables |
| 234 | if (!LastInst->getOperand(0).isMBB()) |
| 235 | return true; |
Scott Michel | aedc637 | 2008-12-10 00:15:19 +0000 | [diff] [blame] | 236 | TBB = LastInst->getOperand(0).getMBB(); |
| 237 | return false; |
| 238 | } else if (isCondBranch(LastInst)) { |
| 239 | // Block ends with fall-through condbranch. |
| 240 | TBB = LastInst->getOperand(1).getMBB(); |
Benjamin Kramer | 072a56e | 2009-08-23 11:52:17 +0000 | [diff] [blame] | 241 | DEBUG(errs() << "Pushing LastInst: "); |
Scott Michel | 9bd7a37 | 2009-01-02 20:52:08 +0000 | [diff] [blame] | 242 | DEBUG(LastInst->dump()); |
| 243 | Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode())); |
Scott Michel | aedc637 | 2008-12-10 00:15:19 +0000 | [diff] [blame] | 244 | Cond.push_back(LastInst->getOperand(0)); |
Scott Michel | aedc637 | 2008-12-10 00:15:19 +0000 | [diff] [blame] | 245 | return false; |
| 246 | } |
| 247 | // Otherwise, don't know what this is. |
| 248 | return true; |
| 249 | } |
Scott Michel | 02d711b | 2008-12-30 23:28:25 +0000 | [diff] [blame] | 250 | |
Scott Michel | aedc637 | 2008-12-10 00:15:19 +0000 | [diff] [blame] | 251 | // Get the instruction before it if it's a terminator. |
| 252 | MachineInstr *SecondLastInst = I; |
| 253 | |
| 254 | // If there are three terminators, we don't know what sort of block this is. |
| 255 | if (SecondLastInst && I != MBB.begin() && |
| 256 | isUnpredicatedTerminator(--I)) |
| 257 | return true; |
Scott Michel | 02d711b | 2008-12-30 23:28:25 +0000 | [diff] [blame] | 258 | |
Scott Michel | aedc637 | 2008-12-10 00:15:19 +0000 | [diff] [blame] | 259 | // If the block ends with a conditional and unconditional branch, handle it. |
| 260 | if (isCondBranch(SecondLastInst) && isUncondBranch(LastInst)) { |
| 261 | TBB = SecondLastInst->getOperand(1).getMBB(); |
Benjamin Kramer | 072a56e | 2009-08-23 11:52:17 +0000 | [diff] [blame] | 262 | DEBUG(errs() << "Pushing SecondLastInst: "); |
Scott Michel | 9bd7a37 | 2009-01-02 20:52:08 +0000 | [diff] [blame] | 263 | DEBUG(SecondLastInst->dump()); |
| 264 | Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode())); |
Scott Michel | aedc637 | 2008-12-10 00:15:19 +0000 | [diff] [blame] | 265 | Cond.push_back(SecondLastInst->getOperand(0)); |
Scott Michel | aedc637 | 2008-12-10 00:15:19 +0000 | [diff] [blame] | 266 | FBB = LastInst->getOperand(0).getMBB(); |
| 267 | return false; |
| 268 | } |
Scott Michel | 02d711b | 2008-12-30 23:28:25 +0000 | [diff] [blame] | 269 | |
Scott Michel | aedc637 | 2008-12-10 00:15:19 +0000 | [diff] [blame] | 270 | // If the block ends with two unconditional branches, handle it. The second |
| 271 | // one is not executed, so remove it. |
| 272 | if (isUncondBranch(SecondLastInst) && isUncondBranch(LastInst)) { |
| 273 | TBB = SecondLastInst->getOperand(0).getMBB(); |
| 274 | I = LastInst; |
Evan Cheng | dc54d31 | 2009-02-09 07:14:22 +0000 | [diff] [blame] | 275 | if (AllowModify) |
| 276 | I->eraseFromParent(); |
Scott Michel | aedc637 | 2008-12-10 00:15:19 +0000 | [diff] [blame] | 277 | return false; |
| 278 | } |
| 279 | |
| 280 | // Otherwise, can't handle this. |
| 281 | return true; |
| 282 | } |
Scott Michel | 02d711b | 2008-12-30 23:28:25 +0000 | [diff] [blame] | 283 | |
Scott Michel | aedc637 | 2008-12-10 00:15:19 +0000 | [diff] [blame] | 284 | unsigned |
| 285 | SPUInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const { |
| 286 | MachineBasicBlock::iterator I = MBB.end(); |
| 287 | if (I == MBB.begin()) |
| 288 | return 0; |
| 289 | --I; |
Dale Johannesen | 93d6a7e | 2010-04-02 01:38:09 +0000 | [diff] [blame] | 290 | while (I->isDebugValue()) { |
| 291 | if (I == MBB.begin()) |
| 292 | return 0; |
| 293 | --I; |
| 294 | } |
Scott Michel | aedc637 | 2008-12-10 00:15:19 +0000 | [diff] [blame] | 295 | if (!isCondBranch(I) && !isUncondBranch(I)) |
| 296 | return 0; |
| 297 | |
| 298 | // Remove the first branch. |
Benjamin Kramer | 072a56e | 2009-08-23 11:52:17 +0000 | [diff] [blame] | 299 | DEBUG(errs() << "Removing branch: "); |
Scott Michel | 9bd7a37 | 2009-01-02 20:52:08 +0000 | [diff] [blame] | 300 | DEBUG(I->dump()); |
Scott Michel | aedc637 | 2008-12-10 00:15:19 +0000 | [diff] [blame] | 301 | I->eraseFromParent(); |
| 302 | I = MBB.end(); |
| 303 | if (I == MBB.begin()) |
| 304 | return 1; |
| 305 | |
| 306 | --I; |
Scott Michel | 9bd7a37 | 2009-01-02 20:52:08 +0000 | [diff] [blame] | 307 | if (!(isCondBranch(I) || isUncondBranch(I))) |
Scott Michel | aedc637 | 2008-12-10 00:15:19 +0000 | [diff] [blame] | 308 | return 1; |
| 309 | |
| 310 | // Remove the second branch. |
Benjamin Kramer | 072a56e | 2009-08-23 11:52:17 +0000 | [diff] [blame] | 311 | DEBUG(errs() << "Removing second branch: "); |
Scott Michel | 9bd7a37 | 2009-01-02 20:52:08 +0000 | [diff] [blame] | 312 | DEBUG(I->dump()); |
Scott Michel | aedc637 | 2008-12-10 00:15:19 +0000 | [diff] [blame] | 313 | I->eraseFromParent(); |
| 314 | return 2; |
| 315 | } |
Scott Michel | 02d711b | 2008-12-30 23:28:25 +0000 | [diff] [blame] | 316 | |
Scott Michel | aedc637 | 2008-12-10 00:15:19 +0000 | [diff] [blame] | 317 | unsigned |
| 318 | SPUInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB, |
Scott Michel | 19c10e6 | 2009-01-26 03:37:41 +0000 | [diff] [blame] | 319 | MachineBasicBlock *FBB, |
Stuart Hastings | 3bf9125 | 2010-06-17 22:43:56 +0000 | [diff] [blame] | 320 | const SmallVectorImpl<MachineOperand> &Cond, |
| 321 | DebugLoc DL) const { |
Scott Michel | aedc637 | 2008-12-10 00:15:19 +0000 | [diff] [blame] | 322 | // Shouldn't be a fall through. |
| 323 | assert(TBB && "InsertBranch must not be told to insert a fallthrough"); |
Scott Michel | 02d711b | 2008-12-30 23:28:25 +0000 | [diff] [blame] | 324 | assert((Cond.size() == 2 || Cond.size() == 0) && |
Scott Michel | aedc637 | 2008-12-10 00:15:19 +0000 | [diff] [blame] | 325 | "SPU branch conditions have two components!"); |
Scott Michel | 02d711b | 2008-12-30 23:28:25 +0000 | [diff] [blame] | 326 | |
Scott Michel | aedc637 | 2008-12-10 00:15:19 +0000 | [diff] [blame] | 327 | // One-way branch. |
| 328 | if (FBB == 0) { |
Scott Michel | 9bd7a37 | 2009-01-02 20:52:08 +0000 | [diff] [blame] | 329 | if (Cond.empty()) { |
| 330 | // Unconditional branch |
Stuart Hastings | 3bf9125 | 2010-06-17 22:43:56 +0000 | [diff] [blame] | 331 | MachineInstrBuilder MIB = BuildMI(&MBB, DL, get(SPU::BR)); |
Scott Michel | 9bd7a37 | 2009-01-02 20:52:08 +0000 | [diff] [blame] | 332 | MIB.addMBB(TBB); |
| 333 | |
Benjamin Kramer | 072a56e | 2009-08-23 11:52:17 +0000 | [diff] [blame] | 334 | DEBUG(errs() << "Inserted one-way uncond branch: "); |
Scott Michel | 9bd7a37 | 2009-01-02 20:52:08 +0000 | [diff] [blame] | 335 | DEBUG((*MIB).dump()); |
| 336 | } else { |
| 337 | // Conditional branch |
Stuart Hastings | 3bf9125 | 2010-06-17 22:43:56 +0000 | [diff] [blame] | 338 | MachineInstrBuilder MIB = BuildMI(&MBB, DL, get(Cond[0].getImm())); |
Scott Michel | 9bd7a37 | 2009-01-02 20:52:08 +0000 | [diff] [blame] | 339 | MIB.addReg(Cond[1].getReg()).addMBB(TBB); |
| 340 | |
Benjamin Kramer | 072a56e | 2009-08-23 11:52:17 +0000 | [diff] [blame] | 341 | DEBUG(errs() << "Inserted one-way cond branch: "); |
Scott Michel | 9bd7a37 | 2009-01-02 20:52:08 +0000 | [diff] [blame] | 342 | DEBUG((*MIB).dump()); |
Scott Michel | aedc637 | 2008-12-10 00:15:19 +0000 | [diff] [blame] | 343 | } |
| 344 | return 1; |
Scott Michel | 9bd7a37 | 2009-01-02 20:52:08 +0000 | [diff] [blame] | 345 | } else { |
Stuart Hastings | 3bf9125 | 2010-06-17 22:43:56 +0000 | [diff] [blame] | 346 | MachineInstrBuilder MIB = BuildMI(&MBB, DL, get(Cond[0].getImm())); |
| 347 | MachineInstrBuilder MIB2 = BuildMI(&MBB, DL, get(SPU::BR)); |
Scott Michel | 9bd7a37 | 2009-01-02 20:52:08 +0000 | [diff] [blame] | 348 | |
| 349 | // Two-way Conditional Branch. |
| 350 | MIB.addReg(Cond[1].getReg()).addMBB(TBB); |
| 351 | MIB2.addMBB(FBB); |
| 352 | |
Benjamin Kramer | 072a56e | 2009-08-23 11:52:17 +0000 | [diff] [blame] | 353 | DEBUG(errs() << "Inserted conditional branch: "); |
Scott Michel | 9bd7a37 | 2009-01-02 20:52:08 +0000 | [diff] [blame] | 354 | DEBUG((*MIB).dump()); |
Benjamin Kramer | 072a56e | 2009-08-23 11:52:17 +0000 | [diff] [blame] | 355 | DEBUG(errs() << "part 2: "); |
Scott Michel | 9bd7a37 | 2009-01-02 20:52:08 +0000 | [diff] [blame] | 356 | DEBUG((*MIB2).dump()); |
| 357 | return 2; |
Scott Michel | aedc637 | 2008-12-10 00:15:19 +0000 | [diff] [blame] | 358 | } |
Scott Michel | aedc637 | 2008-12-10 00:15:19 +0000 | [diff] [blame] | 359 | } |
| 360 | |
Scott Michel | 52d0001 | 2009-01-03 00:27:53 +0000 | [diff] [blame] | 361 | //! Reverses a branch's condition, returning false on success. |
| 362 | bool |
| 363 | SPUInstrInfo::ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) |
| 364 | const { |
| 365 | // Pretty brainless way of inverting the condition, but it works, considering |
| 366 | // there are only two conditions... |
| 367 | static struct { |
| 368 | unsigned Opc; //! The incoming opcode |
| 369 | unsigned RevCondOpc; //! The reversed condition opcode |
| 370 | } revconds[] = { |
| 371 | { SPU::BRNZr32, SPU::BRZr32 }, |
| 372 | { SPU::BRNZv4i32, SPU::BRZv4i32 }, |
| 373 | { SPU::BRZr32, SPU::BRNZr32 }, |
| 374 | { SPU::BRZv4i32, SPU::BRNZv4i32 }, |
| 375 | { SPU::BRHNZr16, SPU::BRHZr16 }, |
| 376 | { SPU::BRHNZv8i16, SPU::BRHZv8i16 }, |
| 377 | { SPU::BRHZr16, SPU::BRHNZr16 }, |
| 378 | { SPU::BRHZv8i16, SPU::BRHNZv8i16 } |
| 379 | }; |
Scott Michel | aedc637 | 2008-12-10 00:15:19 +0000 | [diff] [blame] | 380 | |
Scott Michel | 52d0001 | 2009-01-03 00:27:53 +0000 | [diff] [blame] | 381 | unsigned Opc = unsigned(Cond[0].getImm()); |
| 382 | // Pretty dull mapping between the two conditions that SPU can generate: |
Misha Brukman | 93c65c8 | 2009-01-07 23:07:29 +0000 | [diff] [blame] | 383 | for (int i = sizeof(revconds)/sizeof(revconds[0]) - 1; i >= 0; --i) { |
Scott Michel | 52d0001 | 2009-01-03 00:27:53 +0000 | [diff] [blame] | 384 | if (revconds[i].Opc == Opc) { |
| 385 | Cond[0].setImm(revconds[i].RevCondOpc); |
| 386 | return false; |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | return true; |
| 391 | } |