blob: 9b5e4c741e4f9fb676d21083d6181b257e325e39 [file] [log] [blame]
Duraid Madinabadf0d92006-01-25 02:23:38 +00001//===-- IA64Bundling.cpp - IA-64 instruction bundling pass. ------------ --===//
2//
3// The LLVM Compiler Infrastructure
4//
Duraid Madinad92f1162006-01-26 09:08:31 +00005// This file was developed by Duraid Madina and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
Duraid Madinabadf0d92006-01-25 02:23:38 +00007//
8//===----------------------------------------------------------------------===//
9//
10// Add stops where required to prevent read-after-write and write-after-write
11// dependencies, for both registers and memory addresses. There are exceptions:
12//
13// - Compare instructions (cmp*, tbit, tnat, fcmp, frcpa) are OK with
14// WAW dependencies so long as they all target p0, or are of parallel
15// type (.and*/.or*)
16//
17// FIXME: bundling, for now, is left to the assembler.
18// FIXME: this might be an appropriate place to translate between different
19// instructions that do the same thing, if this helps bundling.
20//
21//===----------------------------------------------------------------------===//
22
23#include "IA64.h"
Evan Chengc0f64ff2006-11-27 23:37:22 +000024#include "IA64InstrInfo.h"
25#include "IA64TargetMachine.h"
Duraid Madinabadf0d92006-01-25 02:23:38 +000026#include "llvm/CodeGen/MachineFunctionPass.h"
27#include "llvm/CodeGen/MachineInstrBuilder.h"
28#include "llvm/ADT/SetOperations.h"
29#include "llvm/ADT/Statistic.h"
30#include "llvm/Support/Debug.h"
31#include <set>
Duraid Madinabadf0d92006-01-25 02:23:38 +000032using namespace llvm;
33
34namespace {
Chris Lattnerac0b6ae2006-12-06 17:46:33 +000035 Statistic StopBitsAdded("ia64-codegen", "Number of stop bits added");
Duraid Madinabadf0d92006-01-25 02:23:38 +000036
37 struct IA64BundlingPass : public MachineFunctionPass {
38 /// Target machine description which we query for reg. names, data
39 /// layout, etc.
40 ///
Evan Chengc4c62572006-03-13 23:20:37 +000041 IA64TargetMachine &TM;
Duraid Madinabadf0d92006-01-25 02:23:38 +000042
Evan Chengc4c62572006-03-13 23:20:37 +000043 IA64BundlingPass(IA64TargetMachine &tm) : TM(tm) { }
Duraid Madinabadf0d92006-01-25 02:23:38 +000044
45 virtual const char *getPassName() const {
46 return "IA64 (Itanium) Bundling Pass";
47 }
48
49 bool runOnMachineBasicBlock(MachineBasicBlock &MBB);
50 bool runOnMachineFunction(MachineFunction &F) {
51 bool Changed = false;
52 for (MachineFunction::iterator FI = F.begin(), FE = F.end();
53 FI != FE; ++FI)
54 Changed |= runOnMachineBasicBlock(*FI);
55 return Changed;
56 }
57
58 std::set<unsigned> PendingRegWrites; // XXX: ugly global, but
59 // pending writes can cross basic blocks. Note that
Duraid Madinad92f1162006-01-26 09:08:31 +000060 // taken branches end instruction groups. So we
61 // only need to worry about 'fallthrough' code
Duraid Madinabadf0d92006-01-25 02:23:38 +000062 };
63} // end of anonymous namespace
64
65/// createIA64BundlingPass - Returns a pass that adds STOP (;;) instructions
66/// and arranges the result into bundles.
67///
Evan Chengc4c62572006-03-13 23:20:37 +000068FunctionPass *llvm::createIA64BundlingPass(IA64TargetMachine &tm) {
Duraid Madinabadf0d92006-01-25 02:23:38 +000069 return new IA64BundlingPass(tm);
70}
71
72/// runOnMachineBasicBlock - add stops and bundle this MBB.
73///
74bool IA64BundlingPass::runOnMachineBasicBlock(MachineBasicBlock &MBB) {
75 bool Changed = false;
76
77 for (MachineBasicBlock::iterator I = MBB.begin(); I != MBB.end(); ) {
78 MachineInstr *CurrentInsn = I++;
79 std::set<unsigned> CurrentReads, CurrentWrites, OrigWrites;
80
81 for(unsigned i=0; i < CurrentInsn->getNumOperands(); i++) {
82 MachineOperand &MO=CurrentInsn->getOperand(i);
83 if(MO.isRegister()) {
84 if(MO.isUse()) { // TODO: exclude p0
85 CurrentReads.insert(MO.getReg());
86 }
87 if(MO.isDef()) { // TODO: exclude p0
88 CurrentWrites.insert(MO.getReg());
89 OrigWrites.insert(MO.getReg()); // FIXME: use a nondestructive
90 // set_intersect instead?
91 }
92 }
93 }
94
95 // CurrentReads/CurrentWrites contain info for the current instruction.
96 // Does it read or write any registers that are pending a write?
97 // (i.e. not separated by a stop)
98 set_intersect(CurrentReads, PendingRegWrites);
99 set_intersect(CurrentWrites, PendingRegWrites);
100
101 if(! (CurrentReads.empty() && CurrentWrites.empty()) ) {
102 // there is a conflict, insert a stop and reset PendingRegWrites
Evan Chengc0f64ff2006-11-27 23:37:22 +0000103 CurrentInsn = BuildMI(MBB, CurrentInsn,
104 TM.getInstrInfo()->get(IA64::STOP), 0);
Duraid Madinabadf0d92006-01-25 02:23:38 +0000105 PendingRegWrites=OrigWrites; // carry over current writes to next insn
106 Changed=true; StopBitsAdded++; // update stats
107 } else { // otherwise, track additional pending writes
108 set_union(PendingRegWrites, OrigWrites);
109 }
110 } // onto the next insn in the MBB
111
112 return Changed;
113}
114