blob: 0bf448339c23c4974607d7d59ee3e15b6d8d7e50 [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
Chris Lattnere96e2632003-09-01 20:24:06 +000019static inline void
Chris Lattner55291ea2002-10-28 01:41:47 +000020DeleteInstruction(MachineBasicBlock& mvec,
21 MachineBasicBlock::iterator& BBI,
Chris Lattnere96e2632003-09-01 20:24:06 +000022 const TargetMachine& target) {
Vikram S. Adve25d80cd2002-09-20 00:42:11 +000023 // Check if this instruction is in a delay slot of its predecessor.
Misha Brukmanf96eb642003-05-23 19:20:57 +000024 if (BBI != mvec.begin()) {
Chris Lattner3501fea2003-01-14 22:00:31 +000025 const TargetInstrInfo& mii = target.getInstrInfo();
Vikram S. Adve25d80cd2002-09-20 00:42:11 +000026 MachineInstr* predMI = *(BBI-1);
Misha Brukmanf96eb642003-05-23 19:20:57 +000027 if (unsigned ndelay = mii.getNumDelaySlots(predMI->getOpCode())) {
28 // This instruction is in a delay slot of its predecessor, so
29 // replace it with a nop. By replacing in place, we save having
30 // to update the I-I maps.
31 //
32 assert(ndelay == 1 && "Not yet handling multiple-delay-slot targets");
33 (*BBI)->replace(mii.getNOPOpCode(), 0);
34 return;
35 }
36 }
Vikram S. Adve7e5167a2002-09-27 14:27:37 +000037
38 // The instruction is not in a delay slot, so we can simply erase it.
39 mvec.erase(BBI);
40 BBI = mvec.end();
Vikram S. Adve25d80cd2002-09-20 00:42:11 +000041}
42
43//******************* Individual Peephole Optimizations ********************/
44
Vikram S. Adve25d80cd2002-09-20 00:42:11 +000045inline bool
Chris Lattner55291ea2002-10-28 01:41:47 +000046RemoveUselessCopies(MachineBasicBlock& mvec,
47 MachineBasicBlock::iterator& BBI,
Chris Lattnere96e2632003-09-01 20:24:06 +000048 const TargetMachine& target) {
Misha Brukmanf96eb642003-05-23 19:20:57 +000049 if (target.getOptInfo().IsUselessCopy(*BBI)) {
50 DeleteInstruction(mvec, BBI, target);
51 return true;
52 }
Vikram S. Adve25d80cd2002-09-20 00:42:11 +000053 return false;
54}
55
56
57//************************ Class Implementations **************************/
58
59class PeepholeOpts: public BasicBlockPass {
60 const TargetMachine ⌖
Chris Lattner55291ea2002-10-28 01:41:47 +000061 bool visit(MachineBasicBlock& mvec,
62 MachineBasicBlock::iterator BBI) const;
Vikram S. Adve25d80cd2002-09-20 00:42:11 +000063public:
64 PeepholeOpts(const TargetMachine &T): target(T) { }
65 bool runOnBasicBlock(BasicBlock &BB); // apply this pass to each BB
Chris Lattnera8e40f52003-08-14 14:43:24 +000066 virtual const char *getPassName() const { return "Peephole Optimization"; }
Vikram S. Adve25d80cd2002-09-20 00:42:11 +000067};
68
Chris Lattnere96e2632003-09-01 20:24:06 +000069// Apply a list of peephole optimizations to this machine instruction
70// within its local context. They are allowed to delete MI or any
71// instruction before MI, but not
72//
73bool PeepholeOpts::visit(MachineBasicBlock& mvec,
74 MachineBasicBlock::iterator BBI) const {
Vikram S. Adve25d80cd2002-09-20 00:42:11 +000075 /* Remove redundant copy instructions */
Chris Lattnere96e2632003-09-01 20:24:06 +000076 return RemoveUselessCopies(mvec, BBI, target);
Vikram S. Adve25d80cd2002-09-20 00:42:11 +000077}
78
79
Chris Lattnere96e2632003-09-01 20:24:06 +000080bool PeepholeOpts::runOnBasicBlock(BasicBlock &BB) {
Vikram S. Adve25d80cd2002-09-20 00:42:11 +000081 // Get the machine instructions for this BB
Misha Brukmanb7551ef2002-10-28 20:00:31 +000082 // FIXME: MachineBasicBlock::get() is deprecated, hence inlining the function
83 const Function *F = BB.getParent();
84 MachineFunction &MF = MachineFunction::get(F);
85 MachineBasicBlock *MBB = NULL;
Chris Lattnere96e2632003-09-01 20:24:06 +000086 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
Misha Brukmanb7551ef2002-10-28 20:00:31 +000087 if (I->getBasicBlock() == &BB)
88 MBB = I;
Chris Lattnere96e2632003-09-01 20:24:06 +000089
Misha Brukmanb7551ef2002-10-28 20:00:31 +000090 assert(MBB && "MachineBasicBlock object not found for specified block!");
91 MachineBasicBlock &mvec = *MBB;
Vikram S. Adve25d80cd2002-09-20 00:42:11 +000092
93 // Iterate over all machine instructions in the BB
94 // Use a reverse iterator to allow deletion of MI or any instruction after it.
95 // Insertions or deletions *before* MI are not safe.
96 //
Chris Lattner55291ea2002-10-28 01:41:47 +000097 for (MachineBasicBlock::reverse_iterator RI=mvec.rbegin(),
Chris Lattnere96e2632003-09-01 20:24:06 +000098 RE=mvec.rend(); RI != RE; ) {
Misha Brukmanf96eb642003-05-23 19:20:57 +000099 MachineBasicBlock::iterator BBI = RI.base()-1; // save before incr
100 ++RI; // pre-increment to delete MI or after it
101 visit(mvec, BBI);
102 }
Vikram S. Adve25d80cd2002-09-20 00:42:11 +0000103
104 return true;
105}
106
107
108//===----------------------------------------------------------------------===//
109// createPeepholeOptsPass - Public entrypoint for peephole optimization
110// and this file as a whole...
111//
Chris Lattnere96e2632003-09-01 20:24:06 +0000112FunctionPass* createPeepholeOptsPass(TargetMachine &T) {
Vikram S. Adve25d80cd2002-09-20 00:42:11 +0000113 return new PeepholeOpts(T);
114}