1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
// $Id$ -*-c++-*-
//***************************************************************************
// File:
// PeepholeOpts.h
//
// Purpose:
// Support for performing several peephole opts in one or a few passes
// over the machine code of a method.
//**************************************************************************/
#include "llvm/CodeGen/PeepholeOpts.h"
#include "llvm/CodeGen/MachineCodeForBasicBlock.h"
#include "llvm/CodeGen/MachineInstr.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/Target/MachineInstrInfo.h"
#include "llvm/Target/MachineOptInfo.h"
#include "llvm/BasicBlock.h"
#include "llvm/Pass.h"
//********************* Internal Class Declarations ************************/
//************************* Internal Functions *****************************/
inline void
DeleteInstruction(MachineCodeForBasicBlock& mvec,
MachineCodeForBasicBlock::iterator& BBI,
const TargetMachine& target)
{
// Check if this instruction is in a delay slot of its predecessor.
// If so, replace this instruction with a nop, else just delete it.
// By replacing in place, we save having to update the I-I maps.
if (BBI != mvec.begin())
{
const MachineInstrInfo& mii = target.getInstrInfo();
MachineInstr* predMI = *(BBI-1);
if (unsigned ndelay = mii.getNumDelaySlots(predMI->getOpCode()))
{
assert(ndelay == 1 && "Not yet handling multiple-delay-slot targets");
(*BBI)->replace(mii.getNOPOpCode(), 0);
}
}
else
{
mvec.erase(BBI);
BBI = mvec.end();
}
}
//******************* Individual Peephole Optimizations ********************/
inline bool
RemoveUselessCopies(MachineCodeForBasicBlock& mvec,
MachineCodeForBasicBlock::iterator& BBI,
const TargetMachine& target)
{
if (target.getOptInfo().IsUselessCopy(*BBI))
{
DeleteInstruction(mvec, BBI, target);
return true;
}
return false;
}
//************************ Class Implementations **************************/
class PeepholeOpts: public BasicBlockPass {
const TargetMachine ⌖
bool visit(MachineCodeForBasicBlock& mvec,
MachineCodeForBasicBlock::iterator BBI) const;
public:
PeepholeOpts(const TargetMachine &T): target(T) { }
bool runOnBasicBlock(BasicBlock &BB); // apply this pass to each BB
};
// Register the pass with llc only, and not opt...
static RegisterLLC<PeepholeOpts>
X("peephole", "Peephole Optimization", createPeepholeOptsPass);
/* Apply a list of peephole optimizations to this machine instruction
* within its local context. They are allowed to delete MI or any
* instruction before MI, but not
*/
bool
PeepholeOpts::visit(MachineCodeForBasicBlock& mvec,
MachineCodeForBasicBlock::iterator BBI) const
{
bool changed = false;
/* Remove redundant copy instructions */
changed |= RemoveUselessCopies(mvec, BBI, target);
if (BBI == mvec.end()) // nothing more to do!
return changed;
return changed;
}
bool
PeepholeOpts::runOnBasicBlock(BasicBlock &BB)
{
// Get the machine instructions for this BB
MachineCodeForBasicBlock& mvec = MachineCodeForBasicBlock::get(&BB);
// Iterate over all machine instructions in the BB
// Use a reverse iterator to allow deletion of MI or any instruction after it.
// Insertions or deletions *before* MI are not safe.
//
for (MachineCodeForBasicBlock::reverse_iterator RI=mvec.rbegin(),
RE=mvec.rend(); RI != RE; )
{
MachineCodeForBasicBlock::iterator BBI = RI.base()-1; // save before incr
++RI; // pre-increment to delete MI or after it
visit(mvec, BBI);
}
return true;
}
//===----------------------------------------------------------------------===//
// createPeepholeOptsPass - Public entrypoint for peephole optimization
// and this file as a whole...
//
Pass*
createPeepholeOptsPass(TargetMachine &T)
{
return new PeepholeOpts(T);
}
|