Duraid Madina | badf0d9 | 2006-01-25 02:23:38 +0000 | [diff] [blame] | 1 | //===-- IA64Bundling.cpp - IA-64 instruction bundling pass. ------------ --===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Duraid Madina | d92f116 | 2006-01-26 09:08:31 +0000 | [diff] [blame] | 5 | // 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 Madina | badf0d9 | 2006-01-25 02:23:38 +0000 | [diff] [blame] | 7 | // |
| 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 Lattner | 95b2c7d | 2006-12-19 22:59:26 +0000 | [diff] [blame] | 23 | #define DEBUG_TYPE "ia64-codegen" |
Duraid Madina | badf0d9 | 2006-01-25 02:23:38 +0000 | [diff] [blame] | 24 | #include "IA64.h" |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 25 | #include "IA64InstrInfo.h" |
| 26 | #include "IA64TargetMachine.h" |
Duraid Madina | badf0d9 | 2006-01-25 02:23:38 +0000 | [diff] [blame] | 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" |
| 32 | #include <set> |
Duraid Madina | badf0d9 | 2006-01-25 02:23:38 +0000 | [diff] [blame] | 33 | using namespace llvm; |
| 34 | |
Chris Lattner | 95b2c7d | 2006-12-19 22:59:26 +0000 | [diff] [blame] | 35 | STATISTIC(StopBitsAdded, "Number of stop bits added"); |
Duraid Madina | badf0d9 | 2006-01-25 02:23:38 +0000 | [diff] [blame] | 36 | |
Chris Lattner | 95b2c7d | 2006-12-19 22:59:26 +0000 | [diff] [blame] | 37 | namespace { |
Duraid Madina | badf0d9 | 2006-01-25 02:23:38 +0000 | [diff] [blame] | 38 | struct IA64BundlingPass : public MachineFunctionPass { |
| 39 | /// Target machine description which we query for reg. names, data |
| 40 | /// layout, etc. |
| 41 | /// |
Evan Cheng | c4c6257 | 2006-03-13 23:20:37 +0000 | [diff] [blame] | 42 | IA64TargetMachine &TM; |
Duraid Madina | badf0d9 | 2006-01-25 02:23:38 +0000 | [diff] [blame] | 43 | |
Evan Cheng | c4c6257 | 2006-03-13 23:20:37 +0000 | [diff] [blame] | 44 | IA64BundlingPass(IA64TargetMachine &tm) : TM(tm) { } |
Duraid Madina | badf0d9 | 2006-01-25 02:23:38 +0000 | [diff] [blame] | 45 | |
| 46 | virtual const char *getPassName() const { |
| 47 | return "IA64 (Itanium) Bundling Pass"; |
| 48 | } |
| 49 | |
| 50 | bool runOnMachineBasicBlock(MachineBasicBlock &MBB); |
| 51 | bool runOnMachineFunction(MachineFunction &F) { |
| 52 | bool Changed = false; |
| 53 | for (MachineFunction::iterator FI = F.begin(), FE = F.end(); |
| 54 | FI != FE; ++FI) |
| 55 | Changed |= runOnMachineBasicBlock(*FI); |
| 56 | return Changed; |
| 57 | } |
| 58 | |
Anton Korobeynikov | bed2946 | 2007-04-16 18:10:23 +0000 | [diff] [blame^] | 59 | // XXX: ugly global, but pending writes can cross basic blocks. Note that |
| 60 | // taken branches end instruction groups. So we only need to worry about |
| 61 | // 'fallthrough' code |
| 62 | std::set<unsigned> PendingRegWrites; |
Duraid Madina | badf0d9 | 2006-01-25 02:23:38 +0000 | [diff] [blame] | 63 | }; |
| 64 | } // end of anonymous namespace |
| 65 | |
| 66 | /// createIA64BundlingPass - Returns a pass that adds STOP (;;) instructions |
| 67 | /// and arranges the result into bundles. |
| 68 | /// |
Evan Cheng | c4c6257 | 2006-03-13 23:20:37 +0000 | [diff] [blame] | 69 | FunctionPass *llvm::createIA64BundlingPass(IA64TargetMachine &tm) { |
Duraid Madina | badf0d9 | 2006-01-25 02:23:38 +0000 | [diff] [blame] | 70 | return new IA64BundlingPass(tm); |
| 71 | } |
| 72 | |
| 73 | /// runOnMachineBasicBlock - add stops and bundle this MBB. |
| 74 | /// |
| 75 | bool IA64BundlingPass::runOnMachineBasicBlock(MachineBasicBlock &MBB) { |
| 76 | bool Changed = false; |
| 77 | |
| 78 | for (MachineBasicBlock::iterator I = MBB.begin(); I != MBB.end(); ) { |
| 79 | MachineInstr *CurrentInsn = I++; |
| 80 | std::set<unsigned> CurrentReads, CurrentWrites, OrigWrites; |
| 81 | |
| 82 | for(unsigned i=0; i < CurrentInsn->getNumOperands(); i++) { |
| 83 | MachineOperand &MO=CurrentInsn->getOperand(i); |
| 84 | if(MO.isRegister()) { |
| 85 | if(MO.isUse()) { // TODO: exclude p0 |
| 86 | CurrentReads.insert(MO.getReg()); |
| 87 | } |
| 88 | if(MO.isDef()) { // TODO: exclude p0 |
| 89 | CurrentWrites.insert(MO.getReg()); |
| 90 | OrigWrites.insert(MO.getReg()); // FIXME: use a nondestructive |
| 91 | // set_intersect instead? |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | // CurrentReads/CurrentWrites contain info for the current instruction. |
| 97 | // Does it read or write any registers that are pending a write? |
| 98 | // (i.e. not separated by a stop) |
| 99 | set_intersect(CurrentReads, PendingRegWrites); |
| 100 | set_intersect(CurrentWrites, PendingRegWrites); |
| 101 | |
| 102 | if(! (CurrentReads.empty() && CurrentWrites.empty()) ) { |
| 103 | // there is a conflict, insert a stop and reset PendingRegWrites |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 104 | CurrentInsn = BuildMI(MBB, CurrentInsn, |
| 105 | TM.getInstrInfo()->get(IA64::STOP), 0); |
Duraid Madina | badf0d9 | 2006-01-25 02:23:38 +0000 | [diff] [blame] | 106 | PendingRegWrites=OrigWrites; // carry over current writes to next insn |
| 107 | Changed=true; StopBitsAdded++; // update stats |
| 108 | } else { // otherwise, track additional pending writes |
| 109 | set_union(PendingRegWrites, OrigWrites); |
| 110 | } |
| 111 | } // onto the next insn in the MBB |
| 112 | |
| 113 | return Changed; |
| 114 | } |
| 115 | |