blob: fb83cbbe8e0d80d0ec807410cd3118b6cc4580a5 [file] [log] [blame]
Vikram S. Adve25d80cd2002-09-20 00:42:11 +00001// $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
27inline void
28DeleteInstruction(MachineCodeForBasicBlock& mvec,
29 MachineCodeForBasicBlock::iterator& BBI,
30 const TargetMachine& target)
31{
32 // Check if this instruction is in a delay slot of its predecessor.
Vikram S. Adve25d80cd2002-09-20 00:42:11 +000033 if (BBI != mvec.begin())
34 {
35 const MachineInstrInfo& mii = target.getInstrInfo();
36 MachineInstr* predMI = *(BBI-1);
37 if (unsigned ndelay = mii.getNumDelaySlots(predMI->getOpCode()))
38 {
Vikram S. Adve7e5167a2002-09-27 14:27:37 +000039 // 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.
42 //
Vikram S. Adve25d80cd2002-09-20 00:42:11 +000043 assert(ndelay == 1 && "Not yet handling multiple-delay-slot targets");
44 (*BBI)->replace(mii.getNOPOpCode(), 0);
Vikram S. Adve7e5167a2002-09-27 14:27:37 +000045 return;
Vikram S. Adve25d80cd2002-09-20 00:42:11 +000046 }
47 }
Vikram S. Adve7e5167a2002-09-27 14:27:37 +000048
49 // The instruction is not in a delay slot, so we can simply erase it.
50 mvec.erase(BBI);
51 BBI = mvec.end();
Vikram S. Adve25d80cd2002-09-20 00:42:11 +000052}
53
54//******************* Individual Peephole Optimizations ********************/
55
56
57inline bool
58RemoveUselessCopies(MachineCodeForBasicBlock& mvec,
59 MachineCodeForBasicBlock::iterator& BBI,
60 const TargetMachine& target)
61{
62 if (target.getOptInfo().IsUselessCopy(*BBI))
63 {
64 DeleteInstruction(mvec, BBI, target);
65 return true;
66 }
67 return false;
68}
69
70
71//************************ Class Implementations **************************/
72
73class PeepholeOpts: public BasicBlockPass {
74 const TargetMachine ⌖
75 bool visit(MachineCodeForBasicBlock& mvec,
76 MachineCodeForBasicBlock::iterator BBI) const;
77public:
78 PeepholeOpts(const TargetMachine &T): target(T) { }
79 bool runOnBasicBlock(BasicBlock &BB); // apply this pass to each BB
80};
81
82
83// Register the pass with llc only, and not opt...
84static RegisterLLC<PeepholeOpts>
85X("peephole", "Peephole Optimization", createPeepholeOptsPass);
86
87
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
91 */
92bool
93PeepholeOpts::visit(MachineCodeForBasicBlock& mvec,
94 MachineCodeForBasicBlock::iterator BBI) const
95{
96 bool changed = false;
97
98 /* Remove redundant copy instructions */
99 changed |= RemoveUselessCopies(mvec, BBI, target);
100 if (BBI == mvec.end()) // nothing more to do!
101 return changed;
102
103 return changed;
104}
105
106
107bool
108PeepholeOpts::runOnBasicBlock(BasicBlock &BB)
109{
110 // Get the machine instructions for this BB
111 MachineCodeForBasicBlock& mvec = MachineCodeForBasicBlock::get(&BB);
112
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.
116 //
117 for (MachineCodeForBasicBlock::reverse_iterator RI=mvec.rbegin(),
118 RE=mvec.rend(); RI != RE; )
119 {
120 MachineCodeForBasicBlock::iterator BBI = RI.base()-1; // save before incr
121 ++RI; // pre-increment to delete MI or after it
122 visit(mvec, BBI);
123 }
124
125 return true;
126}
127
128
129//===----------------------------------------------------------------------===//
130// createPeepholeOptsPass - Public entrypoint for peephole optimization
131// and this file as a whole...
132//
133Pass*
134createPeepholeOptsPass(TargetMachine &T)
135{
136 return new PeepholeOpts(T);
137}
138