blob: 6c9fa299b228cbc73e7e9ddb69f6dc53da9100b2 [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
Chris Lattner95b2c7d2006-12-19 22:59:26 +000023#define DEBUG_TYPE "ia64-codegen"
Duraid Madinabadf0d92006-01-25 02:23:38 +000024#include "IA64.h"
Evan Chengc0f64ff2006-11-27 23:37:22 +000025#include "IA64InstrInfo.h"
26#include "IA64TargetMachine.h"
Duraid Madinabadf0d92006-01-25 02:23:38 +000027#include "llvm/CodeGen/MachineFunctionPass.h"
28#include "llvm/CodeGen/MachineInstrBuilder.h"
29#include "llvm/ADT/SetOperations.h"
30#include "llvm/ADT/Statistic.h"
31#include "llvm/Support/Debug.h"
32#include <set>
Duraid Madinabadf0d92006-01-25 02:23:38 +000033using namespace llvm;
34
Chris Lattner95b2c7d2006-12-19 22:59:26 +000035STATISTIC(StopBitsAdded, "Number of stop bits added");
Duraid Madinabadf0d92006-01-25 02:23:38 +000036
Chris Lattner95b2c7d2006-12-19 22:59:26 +000037namespace {
Duraid Madinabadf0d92006-01-25 02:23:38 +000038 struct IA64BundlingPass : public MachineFunctionPass {
Devang Patel19974732007-05-03 01:11:54 +000039 static char ID;
Duraid Madinabadf0d92006-01-25 02:23:38 +000040 /// Target machine description which we query for reg. names, data
41 /// layout, etc.
42 ///
Evan Chengc4c62572006-03-13 23:20:37 +000043 IA64TargetMachine &TM;
Duraid Madinabadf0d92006-01-25 02:23:38 +000044
Devang Patel794fd752007-05-01 21:15:47 +000045 IA64BundlingPass(IA64TargetMachine &tm)
46 : MachineFunctionPass((intptr_t)&ID), TM(tm) { }
Duraid Madinabadf0d92006-01-25 02:23:38 +000047
48 virtual const char *getPassName() const {
49 return "IA64 (Itanium) Bundling Pass";
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
Anton Korobeynikovbed29462007-04-16 18:10:23 +000061 // XXX: ugly global, but pending writes can cross basic blocks. Note that
62 // taken branches end instruction groups. So we only need to worry about
63 // 'fallthrough' code
64 std::set<unsigned> PendingRegWrites;
Duraid Madinabadf0d92006-01-25 02:23:38 +000065 };
Devang Patel19974732007-05-03 01:11:54 +000066 char IA64BundlingPass::ID = 0;
Duraid Madinabadf0d92006-01-25 02:23:38 +000067} // end of anonymous namespace
68
69/// createIA64BundlingPass - Returns a pass that adds STOP (;;) instructions
70/// and arranges the result into bundles.
71///
Evan Chengc4c62572006-03-13 23:20:37 +000072FunctionPass *llvm::createIA64BundlingPass(IA64TargetMachine &tm) {
Duraid Madinabadf0d92006-01-25 02:23:38 +000073 return new IA64BundlingPass(tm);
74}
75
76/// runOnMachineBasicBlock - add stops and bundle this MBB.
77///
78bool IA64BundlingPass::runOnMachineBasicBlock(MachineBasicBlock &MBB) {
79 bool Changed = false;
80
81 for (MachineBasicBlock::iterator I = MBB.begin(); I != MBB.end(); ) {
82 MachineInstr *CurrentInsn = I++;
83 std::set<unsigned> CurrentReads, CurrentWrites, OrigWrites;
84
85 for(unsigned i=0; i < CurrentInsn->getNumOperands(); i++) {
86 MachineOperand &MO=CurrentInsn->getOperand(i);
87 if(MO.isRegister()) {
88 if(MO.isUse()) { // TODO: exclude p0
89 CurrentReads.insert(MO.getReg());
90 }
91 if(MO.isDef()) { // TODO: exclude p0
92 CurrentWrites.insert(MO.getReg());
93 OrigWrites.insert(MO.getReg()); // FIXME: use a nondestructive
94 // set_intersect instead?
95 }
96 }
97 }
98
99 // CurrentReads/CurrentWrites contain info for the current instruction.
100 // Does it read or write any registers that are pending a write?
101 // (i.e. not separated by a stop)
102 set_intersect(CurrentReads, PendingRegWrites);
103 set_intersect(CurrentWrites, PendingRegWrites);
104
105 if(! (CurrentReads.empty() && CurrentWrites.empty()) ) {
106 // there is a conflict, insert a stop and reset PendingRegWrites
Evan Chengc0f64ff2006-11-27 23:37:22 +0000107 CurrentInsn = BuildMI(MBB, CurrentInsn,
108 TM.getInstrInfo()->get(IA64::STOP), 0);
Duraid Madinabadf0d92006-01-25 02:23:38 +0000109 PendingRegWrites=OrigWrites; // carry over current writes to next insn
110 Changed=true; StopBitsAdded++; // update stats
111 } else { // otherwise, track additional pending writes
112 set_union(PendingRegWrites, OrigWrites);
113 }
114 } // onto the next insn in the MBB
115
116 return Changed;
117}
118