Vikram S. Adve | 25d80cd | 2002-09-20 00:42:11 +0000 | [diff] [blame^] | 1 | // $Id$ -*-c++-*- |
| 2 | //*************************************************************************** |
| 3 | // File: |
| 4 | // PeepholeOpts.h |
| 5 | // |
| 6 | // Purpose: |
| 7 | // Support for performing several peephole opts in one or a few passes |
| 8 | // over the machine code of a method. |
| 9 | //**************************************************************************/ |
| 10 | |
| 11 | |
| 12 | #include "llvm/CodeGen/PeepholeOpts.h" |
| 13 | #include "llvm/CodeGen/MachineCodeForBasicBlock.h" |
| 14 | #include "llvm/CodeGen/MachineInstr.h" |
| 15 | #include "llvm/Target/TargetMachine.h" |
| 16 | #include "llvm/Target/MachineInstrInfo.h" |
| 17 | #include "llvm/Target/MachineOptInfo.h" |
| 18 | #include "llvm/BasicBlock.h" |
| 19 | #include "llvm/Pass.h" |
| 20 | |
| 21 | |
| 22 | //********************* Internal Class Declarations ************************/ |
| 23 | |
| 24 | |
| 25 | //************************* Internal Functions *****************************/ |
| 26 | |
| 27 | inline void |
| 28 | DeleteInstruction(MachineCodeForBasicBlock& mvec, |
| 29 | MachineCodeForBasicBlock::iterator& BBI, |
| 30 | const TargetMachine& target) |
| 31 | { |
| 32 | // Check if this instruction is in a delay slot of its predecessor. |
| 33 | // If so, replace this instruction with a nop, else just delete it. |
| 34 | // By replacing in place, we save having to update the I-I maps. |
| 35 | if (BBI != mvec.begin()) |
| 36 | { |
| 37 | const MachineInstrInfo& mii = target.getInstrInfo(); |
| 38 | MachineInstr* predMI = *(BBI-1); |
| 39 | if (unsigned ndelay = mii.getNumDelaySlots(predMI->getOpCode())) |
| 40 | { |
| 41 | assert(ndelay == 1 && "Not yet handling multiple-delay-slot targets"); |
| 42 | (*BBI)->replace(mii.getNOPOpCode(), 0); |
| 43 | } |
| 44 | } |
| 45 | else |
| 46 | { |
| 47 | mvec.erase(BBI); |
| 48 | BBI = mvec.end(); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | //******************* Individual Peephole Optimizations ********************/ |
| 53 | |
| 54 | |
| 55 | inline bool |
| 56 | RemoveUselessCopies(MachineCodeForBasicBlock& mvec, |
| 57 | MachineCodeForBasicBlock::iterator& BBI, |
| 58 | const TargetMachine& target) |
| 59 | { |
| 60 | if (target.getOptInfo().IsUselessCopy(*BBI)) |
| 61 | { |
| 62 | DeleteInstruction(mvec, BBI, target); |
| 63 | return true; |
| 64 | } |
| 65 | return false; |
| 66 | } |
| 67 | |
| 68 | |
| 69 | //************************ Class Implementations **************************/ |
| 70 | |
| 71 | class PeepholeOpts: public BasicBlockPass { |
| 72 | const TargetMachine ⌖ |
| 73 | bool visit(MachineCodeForBasicBlock& mvec, |
| 74 | MachineCodeForBasicBlock::iterator BBI) const; |
| 75 | public: |
| 76 | PeepholeOpts(const TargetMachine &T): target(T) { } |
| 77 | bool runOnBasicBlock(BasicBlock &BB); // apply this pass to each BB |
| 78 | }; |
| 79 | |
| 80 | |
| 81 | // Register the pass with llc only, and not opt... |
| 82 | static RegisterLLC<PeepholeOpts> |
| 83 | X("peephole", "Peephole Optimization", createPeepholeOptsPass); |
| 84 | |
| 85 | |
| 86 | /* Apply a list of peephole optimizations to this machine instruction |
| 87 | * within its local context. They are allowed to delete MI or any |
| 88 | * instruction before MI, but not |
| 89 | */ |
| 90 | bool |
| 91 | PeepholeOpts::visit(MachineCodeForBasicBlock& mvec, |
| 92 | MachineCodeForBasicBlock::iterator BBI) const |
| 93 | { |
| 94 | bool changed = false; |
| 95 | |
| 96 | /* Remove redundant copy instructions */ |
| 97 | changed |= RemoveUselessCopies(mvec, BBI, target); |
| 98 | if (BBI == mvec.end()) // nothing more to do! |
| 99 | return changed; |
| 100 | |
| 101 | return changed; |
| 102 | } |
| 103 | |
| 104 | |
| 105 | bool |
| 106 | PeepholeOpts::runOnBasicBlock(BasicBlock &BB) |
| 107 | { |
| 108 | // Get the machine instructions for this BB |
| 109 | MachineCodeForBasicBlock& mvec = MachineCodeForBasicBlock::get(&BB); |
| 110 | |
| 111 | // Iterate over all machine instructions in the BB |
| 112 | // Use a reverse iterator to allow deletion of MI or any instruction after it. |
| 113 | // Insertions or deletions *before* MI are not safe. |
| 114 | // |
| 115 | for (MachineCodeForBasicBlock::reverse_iterator RI=mvec.rbegin(), |
| 116 | RE=mvec.rend(); RI != RE; ) |
| 117 | { |
| 118 | MachineCodeForBasicBlock::iterator BBI = RI.base()-1; // save before incr |
| 119 | ++RI; // pre-increment to delete MI or after it |
| 120 | visit(mvec, BBI); |
| 121 | } |
| 122 | |
| 123 | return true; |
| 124 | } |
| 125 | |
| 126 | |
| 127 | //===----------------------------------------------------------------------===// |
| 128 | // createPeepholeOptsPass - Public entrypoint for peephole optimization |
| 129 | // and this file as a whole... |
| 130 | // |
| 131 | Pass* |
| 132 | createPeepholeOptsPass(TargetMachine &T) |
| 133 | { |
| 134 | return new PeepholeOpts(T); |
| 135 | } |
| 136 | |