blob: 73e9ec942a3f157f8e726f785fd1cdb5a898f7f1 [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.
Misha Brukmanf96eb642003-05-23 19:20:57 +000025 if (BBI != mvec.begin()) {
Chris Lattner3501fea2003-01-14 22:00:31 +000026 const TargetInstrInfo& mii = target.getInstrInfo();
Vikram S. Adve25d80cd2002-09-20 00:42:11 +000027 MachineInstr* predMI = *(BBI-1);
Misha Brukmanf96eb642003-05-23 19:20:57 +000028 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.
32 //
33 assert(ndelay == 1 && "Not yet handling multiple-delay-slot targets");
34 (*BBI)->replace(mii.getNOPOpCode(), 0);
35 return;
36 }
37 }
Vikram S. Adve7e5167a2002-09-27 14:27:37 +000038
39 // The instruction is not in a delay slot, so we can simply erase it.
40 mvec.erase(BBI);
41 BBI = mvec.end();
Vikram S. Adve25d80cd2002-09-20 00:42:11 +000042}
43
44//******************* Individual Peephole Optimizations ********************/
45
46
47inline bool
Chris Lattner55291ea2002-10-28 01:41:47 +000048RemoveUselessCopies(MachineBasicBlock& mvec,
49 MachineBasicBlock::iterator& BBI,
Vikram S. Adve25d80cd2002-09-20 00:42:11 +000050 const TargetMachine& target)
51{
Misha Brukmanf96eb642003-05-23 19:20:57 +000052 if (target.getOptInfo().IsUselessCopy(*BBI)) {
53 DeleteInstruction(mvec, BBI, target);
54 return true;
55 }
Vikram S. Adve25d80cd2002-09-20 00:42:11 +000056 return false;
57}
58
59
60//************************ Class Implementations **************************/
61
62class PeepholeOpts: public BasicBlockPass {
63 const TargetMachine ⌖
Chris Lattner55291ea2002-10-28 01:41:47 +000064 bool visit(MachineBasicBlock& mvec,
65 MachineBasicBlock::iterator BBI) const;
Vikram S. Adve25d80cd2002-09-20 00:42:11 +000066public:
67 PeepholeOpts(const TargetMachine &T): target(T) { }
68 bool runOnBasicBlock(BasicBlock &BB); // apply this pass to each BB
69};
70
71
72// Register the pass with llc only, and not opt...
73static RegisterLLC<PeepholeOpts>
74X("peephole", "Peephole Optimization", createPeepholeOptsPass);
75
76
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
80 */
81bool
Chris Lattner55291ea2002-10-28 01:41:47 +000082PeepholeOpts::visit(MachineBasicBlock& mvec,
83 MachineBasicBlock::iterator BBI) const
Vikram S. Adve25d80cd2002-09-20 00:42:11 +000084{
85 bool changed = false;
86
87 /* Remove redundant copy instructions */
88 changed |= RemoveUselessCopies(mvec, BBI, target);
89 if (BBI == mvec.end()) // nothing more to do!
90 return changed;
91
92 return changed;
93}
94
95
96bool
97PeepholeOpts::runOnBasicBlock(BasicBlock &BB)
98{
99 // Get the machine instructions for this BB
Misha Brukmanb7551ef2002-10-28 20:00:31 +0000100 // 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)
106 MBB = I;
107 }
108 assert(MBB && "MachineBasicBlock object not found for specified block!");
109 MachineBasicBlock &mvec = *MBB;
Vikram S. Adve25d80cd2002-09-20 00:42:11 +0000110
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.
114 //
Chris Lattner55291ea2002-10-28 01:41:47 +0000115 for (MachineBasicBlock::reverse_iterator RI=mvec.rbegin(),
Vikram S. Adve25d80cd2002-09-20 00:42:11 +0000116 RE=mvec.rend(); RI != RE; )
Misha Brukmanf96eb642003-05-23 19:20:57 +0000117 {
118 MachineBasicBlock::iterator BBI = RI.base()-1; // save before incr
119 ++RI; // pre-increment to delete MI or after it
120 visit(mvec, BBI);
121 }
Vikram S. Adve25d80cd2002-09-20 00:42:11 +0000122
123 return true;
124}
125
126
127//===----------------------------------------------------------------------===//
128// createPeepholeOptsPass - Public entrypoint for peephole optimization
129// and this file as a whole...
130//
131Pass*
132createPeepholeOptsPass(TargetMachine &T)
133{
134 return new PeepholeOpts(T);
135}