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