1 //===-- IA64Bundling.cpp - IA-64 instruction bundling pass. ------------ --===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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:
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
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.
21 //===----------------------------------------------------------------------===//
23 #define DEBUG_TYPE "ia64-codegen"
25 #include "IA64InstrInfo.h"
26 #include "IA64TargetMachine.h"
27 #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"
35 STATISTIC(StopBitsAdded
, "Number of stop bits added");
38 struct IA64BundlingPass
: public MachineFunctionPass
{
40 /// Target machine description which we query for reg. names, data
43 IA64TargetMachine
&TM
;
45 IA64BundlingPass(IA64TargetMachine
&tm
)
46 : MachineFunctionPass(&ID
), TM(tm
) { }
48 virtual const char *getPassName() const {
49 return "IA64 (Itanium) Bundling Pass";
52 bool runOnMachineBasicBlock(MachineBasicBlock
&MBB
);
53 bool runOnMachineFunction(MachineFunction
&F
) {
55 for (MachineFunction::iterator FI
= F
.begin(), FE
= F
.end();
57 Changed
|= runOnMachineBasicBlock(*FI
);
61 // 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
64 std::set
<unsigned> PendingRegWrites
;
66 char IA64BundlingPass::ID
= 0;
67 } // end of anonymous namespace
69 /// createIA64BundlingPass - Returns a pass that adds STOP (;;) instructions
70 /// and arranges the result into bundles.
72 FunctionPass
*llvm::createIA64BundlingPass(IA64TargetMachine
&tm
) {
73 return new IA64BundlingPass(tm
);
76 /// runOnMachineBasicBlock - add stops and bundle this MBB.
78 bool IA64BundlingPass::runOnMachineBasicBlock(MachineBasicBlock
&MBB
) {
81 for (MachineBasicBlock::iterator I
= MBB
.begin(); I
!= MBB
.end(); ) {
82 MachineInstr
*CurrentInsn
= I
++;
83 std::set
<unsigned> CurrentReads
, CurrentWrites
, OrigWrites
;
85 for(unsigned i
=0; i
< CurrentInsn
->getNumOperands(); i
++) {
86 MachineOperand
&MO
=CurrentInsn
->getOperand(i
);
88 if(MO
.isUse()) { // TODO: exclude p0
89 CurrentReads
.insert(MO
.getReg());
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?
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
);
105 if(! (CurrentReads
.empty() && CurrentWrites
.empty()) ) {
106 // there is a conflict, insert a stop and reset PendingRegWrites
107 CurrentInsn
= BuildMI(MBB
, CurrentInsn
, CurrentInsn
->getDebugLoc(),
108 TM
.getInstrInfo()->get(IA64::STOP
), 0);
109 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
);
114 } // onto the next insn in the MBB