DelaySlotFiller.cpp revision c0f64ffab93d11fb27a3b8a0707b77400918a20e
1//===-- DelaySlotFiller.cpp - SPARC delay slot filler ---------------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This is a simple local pass that fills delay slots with NOPs.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sparc.h"
15#include "llvm/CodeGen/MachineFunctionPass.h"
16#include "llvm/CodeGen/MachineInstrBuilder.h"
17#include "llvm/Target/TargetMachine.h"
18#include "llvm/Target/TargetInstrInfo.h"
19#include "llvm/ADT/Statistic.h"
20using namespace llvm;
21
22namespace {
23  Statistic<> FilledSlots("delayslotfiller", "Num. of delay slots filled");
24
25  struct Filler : public MachineFunctionPass {
26    /// Target machine description which we query for reg. names, data
27    /// layout, etc.
28    ///
29    TargetMachine &TM;
30    const TargetInstrInfo *TII;
31
32    Filler(TargetMachine &tm) : TM(tm), TII(tm.getInstrInfo()) { }
33
34    virtual const char *getPassName() const {
35      return "SPARC Delay Slot Filler";
36    }
37
38    bool runOnMachineBasicBlock(MachineBasicBlock &MBB);
39    bool runOnMachineFunction(MachineFunction &F) {
40      bool Changed = false;
41      for (MachineFunction::iterator FI = F.begin(), FE = F.end();
42           FI != FE; ++FI)
43        Changed |= runOnMachineBasicBlock(*FI);
44      return Changed;
45    }
46
47  };
48} // end of anonymous namespace
49
50/// createSparcDelaySlotFillerPass - Returns a pass that fills in delay
51/// slots in Sparc MachineFunctions
52///
53FunctionPass *llvm::createSparcDelaySlotFillerPass(TargetMachine &tm) {
54  return new Filler(tm);
55}
56
57/// runOnMachineBasicBlock - Fill in delay slots for the given basic block.
58/// Currently, we fill delay slots with NOPs. We assume there is only one
59/// delay slot per delayed instruction.
60///
61bool Filler::runOnMachineBasicBlock(MachineBasicBlock &MBB) {
62  bool Changed = false;
63  for (MachineBasicBlock::iterator I = MBB.begin(); I != MBB.end(); ++I)
64    if (TII->hasDelaySlot(I->getOpcode())) {
65      MachineBasicBlock::iterator J = I;
66      ++J;
67      BuildMI(MBB, J, TII->get(SP::NOP));
68      ++FilledSlots;
69      Changed = true;
70    }
71  return Changed;
72}
73