Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 1 | //===-- DelaySlotFiller.cpp - MBlaze delay slot filler --------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Wesley Peck | a060383 | 2010-10-27 00:23:01 +0000 | [diff] [blame] | 10 | // A pass that attempts to fill instructions with delay slots. If no |
| 11 | // instructions can be moved into the delay slot then a NOP is placed there. |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #define DEBUG_TYPE "delay-slot-filler" |
| 16 | |
| 17 | #include "MBlaze.h" |
| 18 | #include "MBlazeTargetMachine.h" |
| 19 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 20 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 21 | #include "llvm/Target/TargetInstrInfo.h" |
| 22 | #include "llvm/ADT/Statistic.h" |
Wesley Peck | 4e9141f | 2010-10-21 03:57:26 +0000 | [diff] [blame] | 23 | #include "llvm/Support/CommandLine.h" |
| 24 | #include "llvm/Support/Debug.h" |
| 25 | #include "llvm/Support/ErrorHandling.h" |
| 26 | #include "llvm/Support/raw_ostream.h" |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 27 | |
| 28 | using namespace llvm; |
| 29 | |
| 30 | STATISTIC(FilledSlots, "Number of delay slots filled"); |
| 31 | |
Benjamin Kramer | 0861f57 | 2011-11-26 23:01:57 +0000 | [diff] [blame] | 32 | static cl::opt<bool> MBDisableDelaySlotFiller( |
Wesley Peck | 1e8cdd5 | 2010-12-06 21:11:01 +0000 | [diff] [blame] | 33 | "disable-mblaze-delay-filler", |
| 34 | cl::init(false), |
| 35 | cl::desc("Disable the MBlaze delay slot filter."), |
| 36 | cl::Hidden); |
Wesley Peck | 1e8cdd5 | 2010-12-06 21:11:01 +0000 | [diff] [blame] | 37 | |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 38 | namespace { |
| 39 | struct Filler : public MachineFunctionPass { |
| 40 | |
| 41 | TargetMachine &TM; |
| 42 | const TargetInstrInfo *TII; |
| 43 | |
| 44 | static char ID; |
Wesley Peck | 0a67d92 | 2010-11-08 19:40:01 +0000 | [diff] [blame] | 45 | Filler(TargetMachine &tm) |
Owen Anderson | 90c579d | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 46 | : MachineFunctionPass(ID), TM(tm), TII(tm.getInstrInfo()) { } |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 47 | |
| 48 | virtual const char *getPassName() const { |
| 49 | return "MBlaze Delay Slot Filler"; |
| 50 | } |
| 51 | |
| 52 | bool runOnMachineBasicBlock(MachineBasicBlock &MBB); |
| 53 | bool runOnMachineFunction(MachineFunction &F) { |
| 54 | bool Changed = false; |
| 55 | for (MachineFunction::iterator FI = F.begin(), FE = F.end(); |
| 56 | FI != FE; ++FI) |
| 57 | Changed |= runOnMachineBasicBlock(*FI); |
| 58 | return Changed; |
| 59 | } |
| 60 | |
| 61 | }; |
| 62 | char Filler::ID = 0; |
| 63 | } // end of anonymous namespace |
| 64 | |
Wesley Peck | 0a67d92 | 2010-11-08 19:40:01 +0000 | [diff] [blame] | 65 | static bool hasImmInstruction(MachineBasicBlock::iterator &candidate) { |
Wesley Peck | 4e9141f | 2010-10-21 03:57:26 +0000 | [diff] [blame] | 66 | // Any instruction with an immediate mode operand greater than |
| 67 | // 16-bits requires an implicit IMM instruction. |
| 68 | unsigned numOper = candidate->getNumOperands(); |
Wesley Peck | 0a67d92 | 2010-11-08 19:40:01 +0000 | [diff] [blame] | 69 | for (unsigned op = 0; op < numOper; ++op) { |
Wesley Peck | 1e8cdd5 | 2010-12-06 21:11:01 +0000 | [diff] [blame] | 70 | MachineOperand &mop = candidate->getOperand(op); |
| 71 | |
| 72 | // The operand requires more than 16-bits to represent. |
| 73 | if (mop.isImm() && (mop.getImm() < -0x8000 || mop.getImm() > 0x7fff)) |
| 74 | return true; |
| 75 | |
| 76 | // We must assume that unknown immediate values require more than |
| 77 | // 16-bits to represent. |
Wesley Peck | ef9d9fd | 2011-04-11 22:45:02 +0000 | [diff] [blame] | 78 | if (mop.isGlobal() || mop.isSymbol() || mop.isJTI() || mop.isCPI()) |
Wesley Peck | 1e8cdd5 | 2010-12-06 21:11:01 +0000 | [diff] [blame] | 79 | return true; |
Wesley Peck | 4e9141f | 2010-10-21 03:57:26 +0000 | [diff] [blame] | 80 | |
| 81 | // FIXME: we could probably check to see if the FP value happens |
| 82 | // to not need an IMM instruction. For now we just always |
Wesley Peck | 1e8cdd5 | 2010-12-06 21:11:01 +0000 | [diff] [blame] | 83 | // assume that FP values do. |
| 84 | if (mop.isFPImm()) |
| 85 | return true; |
Wesley Peck | 4e9141f | 2010-10-21 03:57:26 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | return false; |
| 89 | } |
| 90 | |
Wesley Peck | 025c458 | 2010-12-22 00:22:59 +0000 | [diff] [blame] | 91 | static unsigned getLastRealOperand(MachineBasicBlock::iterator &instr) { |
| 92 | switch (instr->getOpcode()) { |
| 93 | default: return instr->getNumOperands(); |
| 94 | |
| 95 | // These instructions have a variable number of operands but the first two |
| 96 | // are the "real" operands that we care about during hazard detection. |
| 97 | case MBlaze::BRLID: |
| 98 | case MBlaze::BRALID: |
| 99 | case MBlaze::BRLD: |
| 100 | case MBlaze::BRALD: |
| 101 | return 2; |
| 102 | } |
| 103 | } |
| 104 | |
Wesley Peck | 0a67d92 | 2010-11-08 19:40:01 +0000 | [diff] [blame] | 105 | static bool delayHasHazard(MachineBasicBlock::iterator &candidate, |
| 106 | MachineBasicBlock::iterator &slot) { |
Wesley Peck | 1e8cdd5 | 2010-12-06 21:11:01 +0000 | [diff] [blame] | 107 | // Hazard check |
| 108 | MachineBasicBlock::iterator a = candidate; |
| 109 | MachineBasicBlock::iterator b = slot; |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 110 | MCInstrDesc desc = candidate->getDesc(); |
Wesley Peck | 4e9141f | 2010-10-21 03:57:26 +0000 | [diff] [blame] | 111 | |
Wesley Peck | 1e8cdd5 | 2010-12-06 21:11:01 +0000 | [diff] [blame] | 112 | // MBB layout:- |
| 113 | // candidate := a0 = operation(a1, a2) |
| 114 | // ...middle bit... |
| 115 | // slot := b0 = operation(b1, b2) |
| 116 | |
| 117 | // Possible hazards:-/ |
| 118 | // 1. a1 or a2 was written during the middle bit |
| 119 | // 2. a0 was read or written during the middle bit |
| 120 | // 3. a0 is one or more of {b0, b1, b2} |
| 121 | // 4. b0 is one or more of {a1, a2} |
| 122 | // 5. a accesses memory, and the middle bit |
| 123 | // contains a store operation. |
| 124 | bool a_is_memory = desc.mayLoad() || desc.mayStore(); |
| 125 | |
Wesley Peck | 025c458 | 2010-12-22 00:22:59 +0000 | [diff] [blame] | 126 | // Determine the number of operands in the slot instruction and in the |
| 127 | // candidate instruction. |
| 128 | const unsigned aend = getLastRealOperand(a); |
| 129 | const unsigned bend = getLastRealOperand(b); |
| 130 | |
Wesley Peck | 1e8cdd5 | 2010-12-06 21:11:01 +0000 | [diff] [blame] | 131 | // Check hazards type 1, 2 and 5 by scanning the middle bit |
| 132 | MachineBasicBlock::iterator m = a; |
| 133 | for (++m; m != b; ++m) { |
Wesley Peck | 025c458 | 2010-12-22 00:22:59 +0000 | [diff] [blame] | 134 | for (unsigned aop = 0; aop<aend; ++aop) { |
Wesley Peck | 1e8cdd5 | 2010-12-06 21:11:01 +0000 | [diff] [blame] | 135 | bool aop_is_reg = a->getOperand(aop).isReg(); |
| 136 | if (!aop_is_reg) continue; |
| 137 | |
| 138 | bool aop_is_def = a->getOperand(aop).isDef(); |
| 139 | unsigned aop_reg = a->getOperand(aop).getReg(); |
| 140 | |
Wesley Peck | 025c458 | 2010-12-22 00:22:59 +0000 | [diff] [blame] | 141 | const unsigned mend = getLastRealOperand(m); |
| 142 | for (unsigned mop = 0; mop<mend; ++mop) { |
Wesley Peck | 1e8cdd5 | 2010-12-06 21:11:01 +0000 | [diff] [blame] | 143 | bool mop_is_reg = m->getOperand(mop).isReg(); |
| 144 | if (!mop_is_reg) continue; |
| 145 | |
| 146 | bool mop_is_def = m->getOperand(mop).isDef(); |
| 147 | unsigned mop_reg = m->getOperand(mop).getReg(); |
| 148 | |
| 149 | if (aop_is_def && (mop_reg == aop_reg)) |
| 150 | return true; // Hazard type 2, because aop = a0 |
| 151 | else if (mop_is_def && (mop_reg == aop_reg)) |
| 152 | return true; // Hazard type 1, because aop in {a1, a2} |
| 153 | } |
Wesley Peck | 4e9141f | 2010-10-21 03:57:26 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Wesley Peck | 1e8cdd5 | 2010-12-06 21:11:01 +0000 | [diff] [blame] | 156 | // Check hazard type 5 |
| 157 | if (a_is_memory && m->getDesc().mayStore()) |
| 158 | return true; |
| 159 | } |
Wesley Peck | 4e9141f | 2010-10-21 03:57:26 +0000 | [diff] [blame] | 160 | |
Wesley Peck | 1e8cdd5 | 2010-12-06 21:11:01 +0000 | [diff] [blame] | 161 | // Check hazard type 3 & 4 |
Wesley Peck | 025c458 | 2010-12-22 00:22:59 +0000 | [diff] [blame] | 162 | for (unsigned aop = 0; aop<aend; ++aop) { |
Wesley Peck | 1e8cdd5 | 2010-12-06 21:11:01 +0000 | [diff] [blame] | 163 | if (a->getOperand(aop).isReg()) { |
| 164 | unsigned aop_reg = a->getOperand(aop).getReg(); |
| 165 | |
Wesley Peck | 025c458 | 2010-12-22 00:22:59 +0000 | [diff] [blame] | 166 | for (unsigned bop = 0; bop<bend; ++bop) { |
| 167 | if (b->getOperand(bop).isReg() && !b->getOperand(bop).isImplicit()) { |
Wesley Peck | 1e8cdd5 | 2010-12-06 21:11:01 +0000 | [diff] [blame] | 168 | unsigned bop_reg = b->getOperand(bop).getReg(); |
| 169 | if (aop_reg == bop_reg) |
| 170 | return true; |
Wesley Peck | 4e9141f | 2010-10-21 03:57:26 +0000 | [diff] [blame] | 171 | } |
Wesley Peck | 1e8cdd5 | 2010-12-06 21:11:01 +0000 | [diff] [blame] | 172 | } |
| 173 | } |
Wesley Peck | 4e9141f | 2010-10-21 03:57:26 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | return false; |
| 177 | } |
| 178 | |
Wesley Peck | 5437ba4 | 2010-11-21 21:36:12 +0000 | [diff] [blame] | 179 | static bool isDelayFiller(MachineBasicBlock &MBB, |
| 180 | MachineBasicBlock::iterator candidate) { |
| 181 | if (candidate == MBB.begin()) |
| 182 | return false; |
Wesley Peck | 4e9141f | 2010-10-21 03:57:26 +0000 | [diff] [blame] | 183 | |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 184 | MCInstrDesc brdesc = (--candidate)->getDesc(); |
Wesley Peck | 5437ba4 | 2010-11-21 21:36:12 +0000 | [diff] [blame] | 185 | return (brdesc.hasDelaySlot()); |
| 186 | } |
| 187 | |
Evan Cheng | c36b706 | 2011-01-07 23:50:32 +0000 | [diff] [blame] | 188 | static bool hasUnknownSideEffects(MachineBasicBlock::iterator &I) { |
| 189 | if (!I->hasUnmodeledSideEffects()) |
Wesley Peck | 8f40b24 | 2010-12-12 22:22:49 +0000 | [diff] [blame] | 190 | return false; |
| 191 | |
| 192 | unsigned op = I->getOpcode(); |
| 193 | if (op == MBlaze::ADDK || op == MBlaze::ADDIK || |
| 194 | op == MBlaze::ADDC || op == MBlaze::ADDIC || |
| 195 | op == MBlaze::ADDKC || op == MBlaze::ADDIKC || |
| 196 | op == MBlaze::RSUBK || op == MBlaze::RSUBIK || |
| 197 | op == MBlaze::RSUBC || op == MBlaze::RSUBIC || |
| 198 | op == MBlaze::RSUBKC || op == MBlaze::RSUBIKC) |
| 199 | return false; |
| 200 | |
| 201 | return true; |
| 202 | } |
| 203 | |
Wesley Peck | 5437ba4 | 2010-11-21 21:36:12 +0000 | [diff] [blame] | 204 | static MachineBasicBlock::iterator |
| 205 | findDelayInstr(MachineBasicBlock &MBB,MachineBasicBlock::iterator slot) { |
| 206 | MachineBasicBlock::iterator I = slot; |
| 207 | while (true) { |
| 208 | if (I == MBB.begin()) |
| 209 | break; |
| 210 | |
| 211 | --I; |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 212 | MCInstrDesc desc = I->getDesc(); |
Wesley Peck | 1e8cdd5 | 2010-12-06 21:11:01 +0000 | [diff] [blame] | 213 | if (desc.hasDelaySlot() || desc.isBranch() || isDelayFiller(MBB,I) || |
| 214 | desc.isCall() || desc.isReturn() || desc.isBarrier() || |
Evan Cheng | c36b706 | 2011-01-07 23:50:32 +0000 | [diff] [blame] | 215 | hasUnknownSideEffects(I)) |
Wesley Peck | 5437ba4 | 2010-11-21 21:36:12 +0000 | [diff] [blame] | 216 | break; |
| 217 | |
Wesley Peck | 1e8cdd5 | 2010-12-06 21:11:01 +0000 | [diff] [blame] | 218 | if (hasImmInstruction(I) || delayHasHazard(I,slot)) |
Wesley Peck | 5437ba4 | 2010-11-21 21:36:12 +0000 | [diff] [blame] | 219 | continue; |
| 220 | |
| 221 | return I; |
Wesley Peck | 4e9141f | 2010-10-21 03:57:26 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Wesley Peck | 5437ba4 | 2010-11-21 21:36:12 +0000 | [diff] [blame] | 224 | return MBB.end(); |
Wesley Peck | 4e9141f | 2010-10-21 03:57:26 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 227 | /// runOnMachineBasicBlock - Fill in delay slots for the given basic block. |
| 228 | /// Currently, we fill delay slots with NOPs. We assume there is only one |
| 229 | /// delay slot per delayed instruction. |
| 230 | bool Filler::runOnMachineBasicBlock(MachineBasicBlock &MBB) { |
| 231 | bool Changed = false; |
| 232 | for (MachineBasicBlock::iterator I = MBB.begin(); I != MBB.end(); ++I) |
| 233 | if (I->getDesc().hasDelaySlot()) { |
Wesley Peck | 1e8cdd5 | 2010-12-06 21:11:01 +0000 | [diff] [blame] | 234 | MachineBasicBlock::iterator D = MBB.end(); |
Wesley Peck | 5437ba4 | 2010-11-21 21:36:12 +0000 | [diff] [blame] | 235 | MachineBasicBlock::iterator J = I; |
Wesley Peck | 4e9141f | 2010-10-21 03:57:26 +0000 | [diff] [blame] | 236 | |
Wesley Peck | a18f083 | 2011-11-26 21:50:38 +0000 | [diff] [blame] | 237 | if (!MBDisableDelaySlotFiller) |
Wesley Peck | 1e8cdd5 | 2010-12-06 21:11:01 +0000 | [diff] [blame] | 238 | D = findDelayInstr(MBB,I); |
| 239 | |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 240 | ++FilledSlots; |
| 241 | Changed = true; |
Wesley Peck | 4e9141f | 2010-10-21 03:57:26 +0000 | [diff] [blame] | 242 | |
Wesley Peck | 0a67d92 | 2010-11-08 19:40:01 +0000 | [diff] [blame] | 243 | if (D == MBB.end()) |
Wesley Peck | 5437ba4 | 2010-11-21 21:36:12 +0000 | [diff] [blame] | 244 | BuildMI(MBB, ++J, I->getDebugLoc(), TII->get(MBlaze::NOP)); |
Wesley Peck | 4e9141f | 2010-10-21 03:57:26 +0000 | [diff] [blame] | 245 | else |
Wesley Peck | 5437ba4 | 2010-11-21 21:36:12 +0000 | [diff] [blame] | 246 | MBB.splice(++J, &MBB, D); |
Wesley Peck | a70f28c | 2010-02-23 19:15:24 +0000 | [diff] [blame] | 247 | } |
| 248 | return Changed; |
| 249 | } |
| 250 | |
| 251 | /// createMBlazeDelaySlotFillerPass - Returns a pass that fills in delay |
| 252 | /// slots in MBlaze MachineFunctions |
| 253 | FunctionPass *llvm::createMBlazeDelaySlotFillerPass(MBlazeTargetMachine &tm) { |
| 254 | return new Filler(tm); |
| 255 | } |
| 256 | |