Chris Lattner | 55291ea | 2002-10-28 01:41:47 +0000 | [diff] [blame] | 1 | //===-- PeepholeOpts.cpp --------------------------------------------------===// |
Vikram S. Adve | 25d80cd | 2002-09-20 00:42:11 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | 55291ea | 2002-10-28 01:41:47 +0000 | [diff] [blame] | 3 | // Support for performing several peephole opts in one or a few passes over the |
| 4 | // machine code of a method. |
| 5 | // |
| 6 | //===----------------------------------------------------------------------===// |
Vikram S. Adve | 25d80cd | 2002-09-20 00:42:11 +0000 | [diff] [blame] | 7 | |
| 8 | #include "llvm/CodeGen/PeepholeOpts.h" |
Misha Brukman | b7551ef | 2002-10-28 20:00:31 +0000 | [diff] [blame] | 9 | #include "llvm/CodeGen/MachineFunction.h" |
Vikram S. Adve | 25d80cd | 2002-09-20 00:42:11 +0000 | [diff] [blame] | 10 | #include "llvm/CodeGen/MachineInstr.h" |
| 11 | #include "llvm/Target/TargetMachine.h" |
Chris Lattner | 3501fea | 2003-01-14 22:00:31 +0000 | [diff] [blame] | 12 | #include "llvm/Target/TargetInstrInfo.h" |
Chris Lattner | f27eeea | 2002-12-29 02:50:35 +0000 | [diff] [blame] | 13 | #include "llvm/Target/TargetOptInfo.h" |
Vikram S. Adve | 25d80cd | 2002-09-20 00:42:11 +0000 | [diff] [blame] | 14 | #include "llvm/BasicBlock.h" |
| 15 | #include "llvm/Pass.h" |
| 16 | |
Vikram S. Adve | 25d80cd | 2002-09-20 00:42:11 +0000 | [diff] [blame] | 17 | //************************* Internal Functions *****************************/ |
| 18 | |
| 19 | inline void |
Chris Lattner | 55291ea | 2002-10-28 01:41:47 +0000 | [diff] [blame] | 20 | DeleteInstruction(MachineBasicBlock& mvec, |
| 21 | MachineBasicBlock::iterator& BBI, |
Vikram S. Adve | 25d80cd | 2002-09-20 00:42:11 +0000 | [diff] [blame] | 22 | const TargetMachine& target) |
| 23 | { |
| 24 | // Check if this instruction is in a delay slot of its predecessor. |
Misha Brukman | f96eb64 | 2003-05-23 19:20:57 +0000 | [diff] [blame] | 25 | if (BBI != mvec.begin()) { |
Chris Lattner | 3501fea | 2003-01-14 22:00:31 +0000 | [diff] [blame] | 26 | const TargetInstrInfo& mii = target.getInstrInfo(); |
Vikram S. Adve | 25d80cd | 2002-09-20 00:42:11 +0000 | [diff] [blame] | 27 | MachineInstr* predMI = *(BBI-1); |
Misha Brukman | f96eb64 | 2003-05-23 19:20:57 +0000 | [diff] [blame] | 28 | if (unsigned ndelay = mii.getNumDelaySlots(predMI->getOpCode())) { |
| 29 | // This instruction is in a delay slot of its predecessor, so |
| 30 | // replace it with a nop. By replacing in place, we save having |
| 31 | // to update the I-I maps. |
| 32 | // |
| 33 | assert(ndelay == 1 && "Not yet handling multiple-delay-slot targets"); |
| 34 | (*BBI)->replace(mii.getNOPOpCode(), 0); |
| 35 | return; |
| 36 | } |
| 37 | } |
Vikram S. Adve | 7e5167a | 2002-09-27 14:27:37 +0000 | [diff] [blame] | 38 | |
| 39 | // The instruction is not in a delay slot, so we can simply erase it. |
| 40 | mvec.erase(BBI); |
| 41 | BBI = mvec.end(); |
Vikram S. Adve | 25d80cd | 2002-09-20 00:42:11 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | //******************* Individual Peephole Optimizations ********************/ |
| 45 | |
| 46 | |
| 47 | inline bool |
Chris Lattner | 55291ea | 2002-10-28 01:41:47 +0000 | [diff] [blame] | 48 | RemoveUselessCopies(MachineBasicBlock& mvec, |
| 49 | MachineBasicBlock::iterator& BBI, |
Vikram S. Adve | 25d80cd | 2002-09-20 00:42:11 +0000 | [diff] [blame] | 50 | const TargetMachine& target) |
| 51 | { |
Misha Brukman | f96eb64 | 2003-05-23 19:20:57 +0000 | [diff] [blame] | 52 | if (target.getOptInfo().IsUselessCopy(*BBI)) { |
| 53 | DeleteInstruction(mvec, BBI, target); |
| 54 | return true; |
| 55 | } |
Vikram S. Adve | 25d80cd | 2002-09-20 00:42:11 +0000 | [diff] [blame] | 56 | return false; |
| 57 | } |
| 58 | |
| 59 | |
| 60 | //************************ Class Implementations **************************/ |
| 61 | |
| 62 | class PeepholeOpts: public BasicBlockPass { |
| 63 | const TargetMachine ⌖ |
Chris Lattner | 55291ea | 2002-10-28 01:41:47 +0000 | [diff] [blame] | 64 | bool visit(MachineBasicBlock& mvec, |
| 65 | MachineBasicBlock::iterator BBI) const; |
Vikram S. Adve | 25d80cd | 2002-09-20 00:42:11 +0000 | [diff] [blame] | 66 | public: |
| 67 | PeepholeOpts(const TargetMachine &T): target(T) { } |
| 68 | bool runOnBasicBlock(BasicBlock &BB); // apply this pass to each BB |
Chris Lattner | a8e40f5 | 2003-08-14 14:43:24 +0000 | [diff] [blame^] | 69 | virtual const char *getPassName() const { return "Peephole Optimization"; } |
Vikram S. Adve | 25d80cd | 2002-09-20 00:42:11 +0000 | [diff] [blame] | 70 | }; |
| 71 | |
Vikram S. Adve | 25d80cd | 2002-09-20 00:42:11 +0000 | [diff] [blame] | 72 | /* Apply a list of peephole optimizations to this machine instruction |
| 73 | * within its local context. They are allowed to delete MI or any |
| 74 | * instruction before MI, but not |
| 75 | */ |
| 76 | bool |
Chris Lattner | 55291ea | 2002-10-28 01:41:47 +0000 | [diff] [blame] | 77 | PeepholeOpts::visit(MachineBasicBlock& mvec, |
| 78 | MachineBasicBlock::iterator BBI) const |
Vikram S. Adve | 25d80cd | 2002-09-20 00:42:11 +0000 | [diff] [blame] | 79 | { |
| 80 | bool changed = false; |
| 81 | |
| 82 | /* Remove redundant copy instructions */ |
| 83 | changed |= RemoveUselessCopies(mvec, BBI, target); |
| 84 | if (BBI == mvec.end()) // nothing more to do! |
| 85 | return changed; |
| 86 | |
| 87 | return changed; |
| 88 | } |
| 89 | |
| 90 | |
| 91 | bool |
| 92 | PeepholeOpts::runOnBasicBlock(BasicBlock &BB) |
| 93 | { |
| 94 | // Get the machine instructions for this BB |
Misha Brukman | b7551ef | 2002-10-28 20:00:31 +0000 | [diff] [blame] | 95 | // FIXME: MachineBasicBlock::get() is deprecated, hence inlining the function |
| 96 | const Function *F = BB.getParent(); |
| 97 | MachineFunction &MF = MachineFunction::get(F); |
| 98 | MachineBasicBlock *MBB = NULL; |
| 99 | for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) { |
| 100 | if (I->getBasicBlock() == &BB) |
| 101 | MBB = I; |
| 102 | } |
| 103 | assert(MBB && "MachineBasicBlock object not found for specified block!"); |
| 104 | MachineBasicBlock &mvec = *MBB; |
Vikram S. Adve | 25d80cd | 2002-09-20 00:42:11 +0000 | [diff] [blame] | 105 | |
| 106 | // Iterate over all machine instructions in the BB |
| 107 | // Use a reverse iterator to allow deletion of MI or any instruction after it. |
| 108 | // Insertions or deletions *before* MI are not safe. |
| 109 | // |
Chris Lattner | 55291ea | 2002-10-28 01:41:47 +0000 | [diff] [blame] | 110 | for (MachineBasicBlock::reverse_iterator RI=mvec.rbegin(), |
Vikram S. Adve | 25d80cd | 2002-09-20 00:42:11 +0000 | [diff] [blame] | 111 | RE=mvec.rend(); RI != RE; ) |
Misha Brukman | f96eb64 | 2003-05-23 19:20:57 +0000 | [diff] [blame] | 112 | { |
| 113 | MachineBasicBlock::iterator BBI = RI.base()-1; // save before incr |
| 114 | ++RI; // pre-increment to delete MI or after it |
| 115 | visit(mvec, BBI); |
| 116 | } |
Vikram S. Adve | 25d80cd | 2002-09-20 00:42:11 +0000 | [diff] [blame] | 117 | |
| 118 | return true; |
| 119 | } |
| 120 | |
| 121 | |
| 122 | //===----------------------------------------------------------------------===// |
| 123 | // createPeepholeOptsPass - Public entrypoint for peephole optimization |
| 124 | // and this file as a whole... |
| 125 | // |
Brian Gaeke | 277aad2 | 2003-08-14 06:04:49 +0000 | [diff] [blame] | 126 | FunctionPass* |
Vikram S. Adve | 25d80cd | 2002-09-20 00:42:11 +0000 | [diff] [blame] | 127 | createPeepholeOptsPass(TargetMachine &T) |
| 128 | { |
| 129 | return new PeepholeOpts(T); |
| 130 | } |