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 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())
37 const MachineInstrInfo& mii = target.getInstrInfo();
38 MachineInstr* predMI = *(BBI-1);
39 if (unsigned ndelay = mii.getNumDelaySlots(predMI->getOpCode()))
41 assert(ndelay == 1 && "Not yet handling multiple-delay-slot targets");
42 (*BBI)->replace(mii.getNOPOpCode(), 0);
52 //******************* Individual Peephole Optimizations ********************/
56 RemoveUselessCopies(MachineCodeForBasicBlock& mvec,
57 MachineCodeForBasicBlock::iterator& BBI,
58 const TargetMachine& target)
60 if (target.getOptInfo().IsUselessCopy(*BBI))
62 DeleteInstruction(mvec, BBI, target);
69 //************************ Class Implementations **************************/
71 class PeepholeOpts: public BasicBlockPass {
72 const TargetMachine ⌖
73 bool visit(MachineCodeForBasicBlock& mvec,
74 MachineCodeForBasicBlock::iterator BBI) const;
76 PeepholeOpts(const TargetMachine &T): target(T) { }
77 bool runOnBasicBlock(BasicBlock &BB); // apply this pass to each BB
81 // Register the pass with llc only, and not opt...
82 static RegisterLLC<PeepholeOpts>
83 X("peephole", "Peephole Optimization", createPeepholeOptsPass);
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
91 PeepholeOpts::visit(MachineCodeForBasicBlock& mvec,
92 MachineCodeForBasicBlock::iterator BBI) const
96 /* Remove redundant copy instructions */
97 changed |= RemoveUselessCopies(mvec, BBI, target);
98 if (BBI == mvec.end()) // nothing more to do!
106 PeepholeOpts::runOnBasicBlock(BasicBlock &BB)
108 // Get the machine instructions for this BB
109 MachineCodeForBasicBlock& mvec = MachineCodeForBasicBlock::get(&BB);
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 (MachineCodeForBasicBlock::reverse_iterator RI=mvec.rbegin(),
116 RE=mvec.rend(); RI != RE; )
118 MachineCodeForBasicBlock::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);