blob: 32424e5f3957699ac1a0089cc51f10d298c28e17 [file] [log] [blame]
Wesley Pecka70f28c2010-02-23 19:15:24 +00001//===-- 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 Pecka0603832010-10-27 00:23:01 +000010// 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 Pecka70f28c2010-02-23 19:15:24 +000012//
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 Peck4e9141f2010-10-21 03:57:26 +000023#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 Pecka70f28c2010-02-23 19:15:24 +000027
28using namespace llvm;
29
30STATISTIC(FilledSlots, "Number of delay slots filled");
31
32namespace {
33 struct Filler : public MachineFunctionPass {
34
35 TargetMachine &TM;
36 const TargetInstrInfo *TII;
37
38 static char ID;
Wesley Peck0a67d922010-11-08 19:40:01 +000039 Filler(TargetMachine &tm)
Owen Anderson90c579d2010-08-06 18:33:48 +000040 : MachineFunctionPass(ID), TM(tm), TII(tm.getInstrInfo()) { }
Wesley Pecka70f28c2010-02-23 19:15:24 +000041
42 virtual const char *getPassName() const {
43 return "MBlaze Delay Slot Filler";
44 }
45
46 bool runOnMachineBasicBlock(MachineBasicBlock &MBB);
47 bool runOnMachineFunction(MachineFunction &F) {
48 bool Changed = false;
49 for (MachineFunction::iterator FI = F.begin(), FE = F.end();
50 FI != FE; ++FI)
51 Changed |= runOnMachineBasicBlock(*FI);
52 return Changed;
53 }
54
55 };
56 char Filler::ID = 0;
57} // end of anonymous namespace
58
Wesley Peck0a67d922010-11-08 19:40:01 +000059static bool hasImmInstruction(MachineBasicBlock::iterator &candidate) {
Wesley Peck4e9141f2010-10-21 03:57:26 +000060 // Any instruction with an immediate mode operand greater than
61 // 16-bits requires an implicit IMM instruction.
62 unsigned numOper = candidate->getNumOperands();
Wesley Peck0a67d922010-11-08 19:40:01 +000063 for (unsigned op = 0; op < numOper; ++op) {
64 if (candidate->getOperand(op).isImm() &&
65 (candidate->getOperand(op).getImm() & 0xFFFFFFFFFFFF0000LL) != 0)
Wesley Peck4e9141f2010-10-21 03:57:26 +000066 return true;
67
68 // FIXME: we could probably check to see if the FP value happens
69 // to not need an IMM instruction. For now we just always
70 // assume that FP values always do.
Wesley Peck0a67d922010-11-08 19:40:01 +000071 if (candidate->getOperand(op).isFPImm())
Wesley Peck4e9141f2010-10-21 03:57:26 +000072 return true;
73 }
74
75 return false;
76}
77
Wesley Peck0a67d922010-11-08 19:40:01 +000078static bool delayHasHazard(MachineBasicBlock::iterator &candidate,
79 MachineBasicBlock::iterator &slot) {
Wesley Peck4e9141f2010-10-21 03:57:26 +000080
81 // Loop over all of the operands in the branch instruction
82 // and make sure that none of them are defined by the
83 // candidate instruction.
84 unsigned numOper = slot->getNumOperands();
Wesley Peck0a67d922010-11-08 19:40:01 +000085 for (unsigned op = 0; op < numOper; ++op) {
86 if (!slot->getOperand(op).isReg() ||
Wesley Peck4e9141f2010-10-21 03:57:26 +000087 !slot->getOperand(op).isUse() ||
Wesley Peck0a67d922010-11-08 19:40:01 +000088 slot->getOperand(op).isImplicit())
Wesley Peck4e9141f2010-10-21 03:57:26 +000089 continue;
90
91 unsigned cnumOper = candidate->getNumOperands();
Wesley Peck0a67d922010-11-08 19:40:01 +000092 for (unsigned cop = 0; cop < cnumOper; ++cop) {
93 if (candidate->getOperand(cop).isReg() &&
Wesley Peck4e9141f2010-10-21 03:57:26 +000094 candidate->getOperand(cop).isDef() &&
Wesley Peck0a67d922010-11-08 19:40:01 +000095 candidate->getOperand(cop).getReg() ==
96 slot->getOperand(op).getReg())
Wesley Peck4e9141f2010-10-21 03:57:26 +000097 return true;
98 }
99 }
100
101 // There are no hazards between the two instructions
102 return false;
103}
104
Wesley Peck0a67d922010-11-08 19:40:01 +0000105static bool usedBeforeDelaySlot(MachineBasicBlock::iterator &candidate,
106 MachineBasicBlock::iterator &slot) {
Wesley Peck4e9141f2010-10-21 03:57:26 +0000107 MachineBasicBlock::iterator I = candidate;
108 for (++I; I != slot; ++I) {
109 unsigned numOper = I->getNumOperands();
Wesley Peck0a67d922010-11-08 19:40:01 +0000110 for (unsigned op = 0; op < numOper; ++op) {
111 if (I->getOperand(op).isReg() &&
112 I->getOperand(op).isUse()) {
Wesley Peck4e9141f2010-10-21 03:57:26 +0000113 unsigned reg = I->getOperand(op).getReg();
114 unsigned cops = candidate->getNumOperands();
Wesley Peck0a67d922010-11-08 19:40:01 +0000115 for (unsigned cop = 0; cop < cops; ++cop) {
116 if (candidate->getOperand(cop).isReg() &&
Wesley Peck4e9141f2010-10-21 03:57:26 +0000117 candidate->getOperand(cop).isDef() &&
Wesley Peck0a67d922010-11-08 19:40:01 +0000118 candidate->getOperand(cop).getReg() == reg)
Wesley Peck4e9141f2010-10-21 03:57:26 +0000119 return true;
120 }
121 }
122 }
123 }
124
125 return false;
126}
127
128static MachineBasicBlock::iterator
129findDelayInstr(MachineBasicBlock &MBB,MachineBasicBlock::iterator &slot) {
130 MachineBasicBlock::iterator found = MBB.end();
131 for (MachineBasicBlock::iterator I = MBB.begin(); I != slot; ++I) {
132 TargetInstrDesc desc = I->getDesc();
Wesley Peck0a67d922010-11-08 19:40:01 +0000133 if (desc.hasDelaySlot() || desc.isBranch() ||
134 desc.mayLoad() || desc. mayStore() ||
135 hasImmInstruction(I) || delayHasHazard(I,slot) ||
Wesley Peck4e9141f2010-10-21 03:57:26 +0000136 usedBeforeDelaySlot(I,slot)) continue;
137
138 found = I;
139 }
140
141 return found;
142}
143
Wesley Pecka70f28c2010-02-23 19:15:24 +0000144/// runOnMachineBasicBlock - Fill in delay slots for the given basic block.
145/// Currently, we fill delay slots with NOPs. We assume there is only one
146/// delay slot per delayed instruction.
147bool Filler::runOnMachineBasicBlock(MachineBasicBlock &MBB) {
148 bool Changed = false;
149 for (MachineBasicBlock::iterator I = MBB.begin(); I != MBB.end(); ++I)
150 if (I->getDesc().hasDelaySlot()) {
151 MachineBasicBlock::iterator J = I;
Wesley Peck4e9141f2010-10-21 03:57:26 +0000152 MachineBasicBlock::iterator D = findDelayInstr(MBB,I);
153
Wesley Pecka70f28c2010-02-23 19:15:24 +0000154 ++J;
Wesley Pecka70f28c2010-02-23 19:15:24 +0000155 ++FilledSlots;
156 Changed = true;
Wesley Peck4e9141f2010-10-21 03:57:26 +0000157
Wesley Peck0a67d922010-11-08 19:40:01 +0000158 if (D == MBB.end())
Wesley Peck4e9141f2010-10-21 03:57:26 +0000159 BuildMI(MBB, J, I->getDebugLoc(), TII->get(MBlaze::NOP));
160 else
Wesley Peck0a67d922010-11-08 19:40:01 +0000161 MBB.splice(J, &MBB, D);
Wesley Pecka70f28c2010-02-23 19:15:24 +0000162 }
163 return Changed;
164}
165
166/// createMBlazeDelaySlotFillerPass - Returns a pass that fills in delay
167/// slots in MBlaze MachineFunctions
168FunctionPass *llvm::createMBlazeDelaySlotFillerPass(MBlazeTargetMachine &tm) {
169 return new Filler(tm);
170}
171