blob: 784f1bdfeb579cf9012fa92bddb4ad80bc932e26 [file] [log] [blame]
Chris Lattner7c90f732006-02-05 05:50:24 +00001//===-- DelaySlotFiller.cpp - SPARC delay slot filler ---------------------===//
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002//
Brian Gaeke20117102004-04-06 23:21:45 +00003// 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.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00007//
Brian Gaeke20117102004-04-06 23:21:45 +00008//===----------------------------------------------------------------------===//
9//
Brian Gaeke870248b2004-09-30 04:04:47 +000010// This is a simple local pass that fills delay slots with NOPs.
Brian Gaeke20117102004-04-06 23:21:45 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner95b2c7d2006-12-19 22:59:26 +000014#define DEBUG_TYPE "delayslotfiller"
Chris Lattner7c90f732006-02-05 05:50:24 +000015#include "Sparc.h"
Brian Gaeke20117102004-04-06 23:21:45 +000016#include "llvm/CodeGen/MachineFunctionPass.h"
Brian Gaeke20117102004-04-06 23:21:45 +000017#include "llvm/CodeGen/MachineInstrBuilder.h"
Brian Gaeke870248b2004-09-30 04:04:47 +000018#include "llvm/Target/TargetMachine.h"
19#include "llvm/Target/TargetInstrInfo.h"
Brian Gaeke74dfcf12004-09-02 02:37:43 +000020#include "llvm/ADT/Statistic.h"
Brian Gaeke20117102004-04-06 23:21:45 +000021using namespace llvm;
22
Chris Lattner95b2c7d2006-12-19 22:59:26 +000023STATISTIC(FilledSlots, "Number of delay slots filled");
Brian Gaeke20117102004-04-06 23:21:45 +000024
Chris Lattner95b2c7d2006-12-19 22:59:26 +000025namespace {
Brian Gaeke20117102004-04-06 23:21:45 +000026 struct Filler : public MachineFunctionPass {
27 /// Target machine description which we query for reg. names, data
28 /// layout, etc.
29 ///
30 TargetMachine &TM;
Brian Gaeke870248b2004-09-30 04:04:47 +000031 const TargetInstrInfo *TII;
Brian Gaeke20117102004-04-06 23:21:45 +000032
Devang Patel19974732007-05-03 01:11:54 +000033 static char ID;
Devang Patel794fd752007-05-01 21:15:47 +000034 Filler(TargetMachine &tm)
35 : MachineFunctionPass((intptr_t)&ID), TM(tm), TII(tm.getInstrInfo()) { }
Brian Gaeke20117102004-04-06 23:21:45 +000036
Chris Lattner7c90f732006-02-05 05:50:24 +000037 virtual const char *getPassName() const {
38 return "SPARC Delay Slot Filler";
Brian Gaeke20117102004-04-06 23:21:45 +000039 }
40
Chris Lattner7c90f732006-02-05 05:50:24 +000041 bool runOnMachineBasicBlock(MachineBasicBlock &MBB);
42 bool runOnMachineFunction(MachineFunction &F) {
Brian Gaeke20117102004-04-06 23:21:45 +000043 bool Changed = false;
Chris Lattner7c90f732006-02-05 05:50:24 +000044 for (MachineFunction::iterator FI = F.begin(), FE = F.end();
Brian Gaeke20117102004-04-06 23:21:45 +000045 FI != FE; ++FI)
Chris Lattner7c90f732006-02-05 05:50:24 +000046 Changed |= runOnMachineBasicBlock(*FI);
Brian Gaeke20117102004-04-06 23:21:45 +000047 return Changed;
48 }
49
50 };
Devang Patel19974732007-05-03 01:11:54 +000051 char Filler::ID = 0;
Brian Gaeke20117102004-04-06 23:21:45 +000052} // end of anonymous namespace
53
Chris Lattner7c90f732006-02-05 05:50:24 +000054/// createSparcDelaySlotFillerPass - Returns a pass that fills in delay
55/// slots in Sparc MachineFunctions
Brian Gaeke20117102004-04-06 23:21:45 +000056///
Chris Lattner7c90f732006-02-05 05:50:24 +000057FunctionPass *llvm::createSparcDelaySlotFillerPass(TargetMachine &tm) {
58 return new Filler(tm);
Brian Gaeke20117102004-04-06 23:21:45 +000059}
60
Brian Gaeke20117102004-04-06 23:21:45 +000061/// runOnMachineBasicBlock - Fill in delay slots for the given basic block.
Brian Gaeke0f51cc12004-04-07 04:05:12 +000062/// Currently, we fill delay slots with NOPs. We assume there is only one
63/// delay slot per delayed instruction.
Brian Gaeke20117102004-04-06 23:21:45 +000064///
Chris Lattner7c90f732006-02-05 05:50:24 +000065bool Filler::runOnMachineBasicBlock(MachineBasicBlock &MBB) {
Brian Gaeke0f51cc12004-04-07 04:05:12 +000066 bool Changed = false;
Chris Lattner7c90f732006-02-05 05:50:24 +000067 for (MachineBasicBlock::iterator I = MBB.begin(); I != MBB.end(); ++I)
68 if (TII->hasDelaySlot(I->getOpcode())) {
Brian Gaeke20117102004-04-06 23:21:45 +000069 MachineBasicBlock::iterator J = I;
70 ++J;
Evan Chengc0f64ff2006-11-27 23:37:22 +000071 BuildMI(MBB, J, TII->get(SP::NOP));
Brian Gaeke20117102004-04-06 23:21:45 +000072 ++FilledSlots;
Brian Gaeke0f51cc12004-04-07 04:05:12 +000073 Changed = true;
Brian Gaeke20117102004-04-06 23:21:45 +000074 }
Brian Gaeke0f51cc12004-04-07 04:05:12 +000075 return Changed;
Brian Gaeke20117102004-04-06 23:21:45 +000076}