1 //===-- PeepholeOpts.cpp --------------------------------------------------===//
3 // Support for performing several peephole opts in one or a few passes over the
4 // machine code of a method.
6 //===----------------------------------------------------------------------===//
8 #include "llvm/CodeGen/PeepholeOpts.h"
9 #include "llvm/CodeGen/MachineFunction.h"
10 #include "llvm/CodeGen/MachineInstr.h"
11 #include "llvm/Target/TargetMachine.h"
12 #include "llvm/Target/TargetInstrInfo.h"
13 #include "llvm/Target/TargetOptInfo.h"
14 #include "llvm/BasicBlock.h"
15 #include "llvm/Pass.h"
17 //************************* Internal Functions *****************************/
20 DeleteInstruction(MachineBasicBlock& mvec,
21 MachineBasicBlock::iterator& BBI,
22 const TargetMachine& target)
24 // Check if this instruction is in a delay slot of its predecessor.
25 if (BBI != mvec.begin()) {
26 const TargetInstrInfo& mii = target.getInstrInfo();
27 MachineInstr* predMI = *(BBI-1);
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.
33 assert(ndelay == 1 && "Not yet handling multiple-delay-slot targets");
34 (*BBI)->replace(mii.getNOPOpCode(), 0);
39 // The instruction is not in a delay slot, so we can simply erase it.
44 //******************* Individual Peephole Optimizations ********************/
48 RemoveUselessCopies(MachineBasicBlock& mvec,
49 MachineBasicBlock::iterator& BBI,
50 const TargetMachine& target)
52 if (target.getOptInfo().IsUselessCopy(*BBI)) {
53 DeleteInstruction(mvec, BBI, target);
60 //************************ Class Implementations **************************/
62 class PeepholeOpts: public BasicBlockPass {
63 const TargetMachine ⌖
64 bool visit(MachineBasicBlock& mvec,
65 MachineBasicBlock::iterator BBI) const;
67 PeepholeOpts(const TargetMachine &T): target(T) { }
68 bool runOnBasicBlock(BasicBlock &BB); // apply this pass to each BB
72 // Register the pass with llc only, and not opt...
73 static RegisterLLC<PeepholeOpts>
74 X("peephole", "Peephole Optimization", createPeepholeOptsPass);
77 /* Apply a list of peephole optimizations to this machine instruction
78 * within its local context. They are allowed to delete MI or any
79 * instruction before MI, but not
82 PeepholeOpts::visit(MachineBasicBlock& mvec,
83 MachineBasicBlock::iterator BBI) const
87 /* Remove redundant copy instructions */
88 changed |= RemoveUselessCopies(mvec, BBI, target);
89 if (BBI == mvec.end()) // nothing more to do!
97 PeepholeOpts::runOnBasicBlock(BasicBlock &BB)
99 // Get the machine instructions for this BB
100 // FIXME: MachineBasicBlock::get() is deprecated, hence inlining the function
101 const Function *F = BB.getParent();
102 MachineFunction &MF = MachineFunction::get(F);
103 MachineBasicBlock *MBB = NULL;
104 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
105 if (I->getBasicBlock() == &BB)
108 assert(MBB && "MachineBasicBlock object not found for specified block!");
109 MachineBasicBlock &mvec = *MBB;
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.
115 for (MachineBasicBlock::reverse_iterator RI=mvec.rbegin(),
116 RE=mvec.rend(); RI != RE; )
118 MachineBasicBlock::iterator BBI = RI.base()-1; // save before incr
119 ++RI; // pre-increment to delete MI or after it
127 //===----------------------------------------------------------------------===//
128 // createPeepholeOptsPass - Public entrypoint for peephole optimization
129 // and this file as a whole...
132 createPeepholeOptsPass(TargetMachine &T)
134 return new PeepholeOpts(T);