blob: 9cff89f5ac573965ff78bad2491ab3d729f2cac8 [file] [log] [blame]
Chris Lattner55291ea2002-10-28 01:41:47 +00001//===-- PeepholeOpts.cpp --------------------------------------------------===//
Vikram S. Adve25d80cd2002-09-20 00:42:11 +00002//
Chris Lattner55291ea2002-10-28 01:41:47 +00003// Support for performing several peephole opts in one or a few passes over the
4// machine code of a method.
5//
6//===----------------------------------------------------------------------===//
Vikram S. Adve25d80cd2002-09-20 00:42:11 +00007
8#include "llvm/CodeGen/PeepholeOpts.h"
Misha Brukmanb7551ef2002-10-28 20:00:31 +00009#include "llvm/CodeGen/MachineFunction.h"
Vikram S. Adve25d80cd2002-09-20 00:42:11 +000010#include "llvm/CodeGen/MachineInstr.h"
11#include "llvm/Target/TargetMachine.h"
Chris Lattner3501fea2003-01-14 22:00:31 +000012#include "llvm/Target/TargetInstrInfo.h"
Chris Lattnerf27eeea2002-12-29 02:50:35 +000013#include "llvm/Target/TargetOptInfo.h"
Vikram S. Adve25d80cd2002-09-20 00:42:11 +000014#include "llvm/BasicBlock.h"
15#include "llvm/Pass.h"
16
Vikram S. Adve25d80cd2002-09-20 00:42:11 +000017//************************* Internal Functions *****************************/
18
19inline void
Chris Lattner55291ea2002-10-28 01:41:47 +000020DeleteInstruction(MachineBasicBlock& mvec,
21 MachineBasicBlock::iterator& BBI,
Vikram S. Adve25d80cd2002-09-20 00:42:11 +000022 const TargetMachine& target)
23{
24 // Check if this instruction is in a delay slot of its predecessor.
Vikram S. Adve25d80cd2002-09-20 00:42:11 +000025 if (BBI != mvec.begin())
26 {
Chris Lattner3501fea2003-01-14 22:00:31 +000027 const TargetInstrInfo& mii = target.getInstrInfo();
Vikram S. Adve25d80cd2002-09-20 00:42:11 +000028 MachineInstr* predMI = *(BBI-1);
29 if (unsigned ndelay = mii.getNumDelaySlots(predMI->getOpCode()))
30 {
Vikram S. Adve7e5167a2002-09-27 14:27:37 +000031 // This instruction is in a delay slot of its predecessor, so
32 // replace it with a nop. By replacing in place, we save having
33 // to update the I-I maps.
34 //
Vikram S. Adve25d80cd2002-09-20 00:42:11 +000035 assert(ndelay == 1 && "Not yet handling multiple-delay-slot targets");
36 (*BBI)->replace(mii.getNOPOpCode(), 0);
Vikram S. Adve7e5167a2002-09-27 14:27:37 +000037 return;
Vikram S. Adve25d80cd2002-09-20 00:42:11 +000038 }
39 }
Vikram S. Adve7e5167a2002-09-27 14:27:37 +000040
41 // The instruction is not in a delay slot, so we can simply erase it.
42 mvec.erase(BBI);
43 BBI = mvec.end();
Vikram S. Adve25d80cd2002-09-20 00:42:11 +000044}
45
46//******************* Individual Peephole Optimizations ********************/
47
48
49inline bool
Chris Lattner55291ea2002-10-28 01:41:47 +000050RemoveUselessCopies(MachineBasicBlock& mvec,
51 MachineBasicBlock::iterator& BBI,
Vikram S. Adve25d80cd2002-09-20 00:42:11 +000052 const TargetMachine& target)
53{
54 if (target.getOptInfo().IsUselessCopy(*BBI))
55 {
56 DeleteInstruction(mvec, BBI, target);
57 return true;
58 }
59 return false;
60}
61
62
63//************************ Class Implementations **************************/
64
65class PeepholeOpts: public BasicBlockPass {
66 const TargetMachine ⌖
Chris Lattner55291ea2002-10-28 01:41:47 +000067 bool visit(MachineBasicBlock& mvec,
68 MachineBasicBlock::iterator BBI) const;
Vikram S. Adve25d80cd2002-09-20 00:42:11 +000069public:
70 PeepholeOpts(const TargetMachine &T): target(T) { }
71 bool runOnBasicBlock(BasicBlock &BB); // apply this pass to each BB
72};
73
74
75// Register the pass with llc only, and not opt...
76static RegisterLLC<PeepholeOpts>
77X("peephole", "Peephole Optimization", createPeepholeOptsPass);
78
79
80/* Apply a list of peephole optimizations to this machine instruction
81 * within its local context. They are allowed to delete MI or any
82 * instruction before MI, but not
83 */
84bool
Chris Lattner55291ea2002-10-28 01:41:47 +000085PeepholeOpts::visit(MachineBasicBlock& mvec,
86 MachineBasicBlock::iterator BBI) const
Vikram S. Adve25d80cd2002-09-20 00:42:11 +000087{
88 bool changed = false;
89
90 /* Remove redundant copy instructions */
91 changed |= RemoveUselessCopies(mvec, BBI, target);
92 if (BBI == mvec.end()) // nothing more to do!
93 return changed;
94
95 return changed;
96}
97
98
99bool
100PeepholeOpts::runOnBasicBlock(BasicBlock &BB)
101{
102 // Get the machine instructions for this BB
Misha Brukmanb7551ef2002-10-28 20:00:31 +0000103 // FIXME: MachineBasicBlock::get() is deprecated, hence inlining the function
104 const Function *F = BB.getParent();
105 MachineFunction &MF = MachineFunction::get(F);
106 MachineBasicBlock *MBB = NULL;
107 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
108 if (I->getBasicBlock() == &BB)
109 MBB = I;
110 }
111 assert(MBB && "MachineBasicBlock object not found for specified block!");
112 MachineBasicBlock &mvec = *MBB;
Vikram S. Adve25d80cd2002-09-20 00:42:11 +0000113
114 // Iterate over all machine instructions in the BB
115 // Use a reverse iterator to allow deletion of MI or any instruction after it.
116 // Insertions or deletions *before* MI are not safe.
117 //
Chris Lattner55291ea2002-10-28 01:41:47 +0000118 for (MachineBasicBlock::reverse_iterator RI=mvec.rbegin(),
Vikram S. Adve25d80cd2002-09-20 00:42:11 +0000119 RE=mvec.rend(); RI != RE; )
120 {
Chris Lattner55291ea2002-10-28 01:41:47 +0000121 MachineBasicBlock::iterator BBI = RI.base()-1; // save before incr
Vikram S. Adve25d80cd2002-09-20 00:42:11 +0000122 ++RI; // pre-increment to delete MI or after it
123 visit(mvec, BBI);
124 }
125
126 return true;
127}
128
129
130//===----------------------------------------------------------------------===//
131// createPeepholeOptsPass - Public entrypoint for peephole optimization
132// and this file as a whole...
133//
134Pass*
135createPeepholeOptsPass(TargetMachine &T)
136{
137 return new PeepholeOpts(T);
138}
139