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