2 //***************************************************************************
7 // Support for performing several peephole opts in one or a few passes
8 // over the machine code of a method.
9 //**************************************************************************/
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"
22 //********************* Internal Class Declarations ************************/
25 //************************* Internal Functions *****************************/
28 DeleteInstruction(MachineCodeForBasicBlock& mvec,
29 MachineCodeForBasicBlock::iterator& BBI,
30 const TargetMachine& target)
32 // Check if this instruction is in a delay slot of its predecessor.
33 if (BBI != mvec.begin())
35 const MachineInstrInfo& mii = target.getInstrInfo();
36 MachineInstr* predMI = *(BBI-1);
37 if (unsigned ndelay = mii.getNumDelaySlots(predMI->getOpCode()))
39 // This instruction is in a delay slot of its predecessor, so
40 // replace it with a nop. By replacing in place, we save having
41 // to update the I-I maps.
43 assert(ndelay == 1 && "Not yet handling multiple-delay-slot targets");
44 (*BBI)->replace(mii.getNOPOpCode(), 0);
49 // The instruction is not in a delay slot, so we can simply erase it.
54 //******************* Individual Peephole Optimizations ********************/
58 RemoveUselessCopies(MachineCodeForBasicBlock& mvec,
59 MachineCodeForBasicBlock::iterator& BBI,
60 const TargetMachine& target)
62 if (target.getOptInfo().IsUselessCopy(*BBI))
64 DeleteInstruction(mvec, BBI, target);
71 //************************ Class Implementations **************************/
73 class PeepholeOpts: public BasicBlockPass {
74 const TargetMachine ⌖
75 bool visit(MachineCodeForBasicBlock& mvec,
76 MachineCodeForBasicBlock::iterator BBI) const;
78 PeepholeOpts(const TargetMachine &T): target(T) { }
79 bool runOnBasicBlock(BasicBlock &BB); // apply this pass to each BB
83 // Register the pass with llc only, and not opt...
84 static RegisterLLC<PeepholeOpts>
85 X("peephole", "Peephole Optimization", createPeepholeOptsPass);
88 /* Apply a list of peephole optimizations to this machine instruction
89 * within its local context. They are allowed to delete MI or any
90 * instruction before MI, but not
93 PeepholeOpts::visit(MachineCodeForBasicBlock& mvec,
94 MachineCodeForBasicBlock::iterator BBI) const
98 /* Remove redundant copy instructions */
99 changed |= RemoveUselessCopies(mvec, BBI, target);
100 if (BBI == mvec.end()) // nothing more to do!
108 PeepholeOpts::runOnBasicBlock(BasicBlock &BB)
110 // Get the machine instructions for this BB
111 MachineCodeForBasicBlock& mvec = MachineCodeForBasicBlock::get(&BB);
113 // Iterate over all machine instructions in the BB
114 // Use a reverse iterator to allow deletion of MI or any instruction after it.
115 // Insertions or deletions *before* MI are not safe.
117 for (MachineCodeForBasicBlock::reverse_iterator RI=mvec.rbegin(),
118 RE=mvec.rend(); RI != RE; )
120 MachineCodeForBasicBlock::iterator BBI = RI.base()-1; // save before incr
121 ++RI; // pre-increment to delete MI or after it
129 //===----------------------------------------------------------------------===//
130 // createPeepholeOptsPass - Public entrypoint for peephole optimization
131 // and this file as a whole...
134 createPeepholeOptsPass(TargetMachine &T)
136 return new PeepholeOpts(T);