llvm.org GIT mirror llvm / 2c17c4d
Pluggable coalescers inplementation. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@41743 91177308-0d34-0410-b5e6-96231b3b80d8 David Greene 12 years ago
7 changed file(s) with 238 addition(s) and 2 deletion(s). Raw diff Collapse all Expand all
3131 (void) llvm::createLocalRegisterAllocator();
3232 (void) llvm::createBigBlockRegisterAllocator();
3333 (void) llvm::createLinearScanRegisterAllocator();
34
35 (void) llvm::createSimpleRegisterCoalescer();
3436
3537 (void) llvm::createBFS_DAGScheduler(NULL, NULL, NULL);
3638 (void) llvm::createSimpleDAGScheduler(NULL, NULL, NULL);
2222 class FunctionPass;
2323 class PassInfo;
2424 class TargetMachine;
25 class RegisterCoalescer;
2526
2627 /// createUnreachableBlockEliminationPass - The LLVM code generator does not
2728 /// work well with unreachable basic blocks (what live ranges make sense for a
8384 ///
8485 FunctionPass *createLinearScanRegisterAllocator();
8586
87 /// SimpleRegisterCoalescing Pass - Coalesce all copies possible. Can run
88 /// independently of the register allocator.
89 ///
90 RegisterCoalescer *createSimpleRegisterCoalescer();
91
8692 /// PrologEpilogCodeInserter Pass - This pass inserts prolog and epilog code,
8793 /// and eliminates abstract frame references.
8894 ///
0 //===-- RegisterCoalescer.h - Register Coalescing Interface ------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file was developed by the LLVM research group and is distributed under
5 // the University of Illinois Open Source License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains the abstract interface for register coalescers,
10 // allowing them to interact with and query register allocators.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/System/IncludeFile.h"
15 #include "llvm/CodeGen/MachineInstr.h"
16 #include "llvm/CodeGen/LiveIntervalAnalysis.h"
17 #include "llvm/CodeGen/LiveVariables.h"
18 #include "llvm/Target/MRegisterInfo.h"
19 #include "llvm/Support/Debug.h"
20
21 #ifndef LLVM_CODEGEN_REGISTER_COALESCER_H
22 #define LLVM_CODEGEN_REGISTER_COALESCER_H
23
24 namespace llvm
25 {
26 class MachineFunction;
27 class RegallocQuery;
28 class AnalysisUsage;
29 class LiveIntervals;
30 class MachineInstr;
31 class MRegisterInfo;
32
33 /// An abstract interface for register coalescers. Coalescers must
34 /// implement this interface to be part of the coalescer analysis
35 /// group.
36 class RegisterCoalescer {
37 public:
38 static char ID; // Class identification, replacement for typeinfo
39 RegisterCoalescer() {}
40 virtual ~RegisterCoalescer(); // We want to be subclassed
41
42 /// Run the coalescer on this function, providing interference
43 /// data to query. Return whether we removed any copies.
44 virtual bool coalesceFunction(MachineFunction &mf,
45 RegallocQuery &ifd) = 0;
46
47 /// Reset state. Can be used to allow a coalescer run by
48 /// PassManager to be run again by the register allocator.
49 virtual void reset(MachineFunction &mf) {};
50
51 /// Register allocators must call this from their own
52 /// getAnalysisUsage to cover the case where the coalescer is not
53 /// a Pass in the proper sense and isn't managed by PassManager.
54 /// PassManager needs to know which analyses to make available and
55 /// which to invalidate when running the register allocator or any
56 /// pass that might call coalescing. The long-term solution is to
57 /// allow hierarchies of PassManagers.
58 virtual void getAnalysisUsage(AnalysisUsage &AU) const {};
59 };
60
61 /// An abstract interface for register allocators to interact with
62 /// coalescers
63 ///
64 /// Example:
65 ///
66 /// This is simply an example of how to use the RegallocQuery
67 /// interface. It is not meant to be used in production.
68 ///
69 /// class LinearScanRegallocQuery : public RegallocQuery {
70 /// private:
71 /// const LiveIntervals &li;
72 ///
73 /// public:
74 /// LinearScanRegallocQuery(LiveIntervals &intervals)
75 /// : li(intervals) {};
76 ///
77 /// /// This is pretty slow and conservative, but since linear scan
78 /// /// allocation doesn't pre-compute interference information it's
79 /// /// the best we can do. Coalescers are always free to ignore this
80 /// /// and implement their own discovery strategy. See
81 /// /// SimpleRegisterCoalescing for an example.
82 /// void getInterferences(IntervalSet &interferences,
83 /// const LiveInterval &a) const {
84 /// for(LiveIntervals::const_iterator iv = li.begin(),
85 /// ivend = li.end();
86 /// iv != ivend;
87 /// ++iv) {
88 /// if (interfere(a, iv->second)) {
89 /// interferences.insert(&iv->second);
90 /// }
91 /// }
92 /// };
93 ///
94 /// /// This is *really* slow and stupid. See above.
95 /// int getNumberOfInterferences(const LiveInterval &a) const {
96 /// IntervalSet intervals;
97 /// getInterferences(intervals, a);
98 /// return(intervals.size());
99 /// };
100 /// };
101 ///
102 /// In the allocator:
103 ///
104 /// RegisterCoalescer &coalescer = getAnalysis();
105 ///
106 /// // We don't reset the coalescer so if it's already been run this
107 /// // takes almost no time.
108 /// LinearScanRegallocQuery ifd(*li_);
109 /// coalescer.coalesceFunction(fn, ifd);
110 ///
111 class RegallocQuery {
112 public:
113 typedef SmallPtrSet IntervalSet;
114
115 virtual ~RegallocQuery() {};
116
117 /// Return whether two live ranges interfere.
118 virtual bool interfere(const LiveInterval &a,
119 const LiveInterval &b) const {
120 // A naive test
121 return(a.overlaps(b));
122 };
123
124 /// Return the set of intervals that interfere with this one.
125 virtual void getInterferences(IntervalSet &interferences,
126 const LiveInterval &a) const = 0;
127
128 /// This can often be cheaper than actually returning the
129 /// interferences.
130 virtual int getNumberOfInterferences(const LiveInterval &a) const = 0;
131
132 /// Make any data structure updates necessary to reflect
133 /// coalescing or other modifications.
134 virtual void updateDataForMerge(const LiveInterval &a,
135 const LiveInterval &b,
136 const MachineInstr ©) {};
137
138 /// Allow the register allocator to communicate when it doesn't
139 /// want a copy coalesced. This may be due to assumptions made by
140 /// the allocator about various invariants and so this question is
141 /// a matter of legality, not performance. Performance decisions
142 /// about which copies to coalesce should be made by the
143 /// coalescer.
144 virtual bool isLegalToCoalesce(const MachineInstr &inst) const {
145 return(true);
146 }
147 };
148 }
149
150 // Because of the way .a files work, we must force the SimpleRC
151 // implementation to be pulled in if the RegisterCoalescing header is
152 // included. Otherwise we run the risk of RegisterCoalescing being
153 // used, but the default implementation not being linked into the tool
154 // that uses it.
155 FORCE_DEFINING_FILE_TO_BE_LINKED(RegisterCoalescer)
156 FORCE_DEFINING_FILE_TO_BE_LINKED(SimpleRegisterCoalescing)
157
158 #endif
1616 #include "llvm/CodeGen/MachineFunctionPass.h"
1717 #include "llvm/CodeGen/LiveInterval.h"
1818 #include "llvm/CodeGen/LiveIntervalAnalysis.h"
19 #include "llvm/CodeGen/RegisterCoalescer.h"
1920 #include "llvm/ADT/BitVector.h"
2021 #include "llvm/ADT/IndexedMap.h"
2122
2627 class TargetInstrInfo;
2728 class VirtRegMap;
2829
29 class SimpleRegisterCoalescing : public MachineFunctionPass {
30 class SimpleRegisterCoalescing : public MachineFunctionPass,
31 public RegisterCoalescer {
3032 MachineFunction* mf_;
3133 const TargetMachine* tm_;
3234 const MRegisterInfo* mri_;
7476
7577 /// runOnMachineFunction - pass entry point
7678 virtual bool runOnMachineFunction(MachineFunction&);
79
80 bool coalesceFunction(MachineFunction &mf, RegallocQuery &) {
81 // This runs as an independent pass, so don't do anything.
82 return(false);
83 };
7784
7885 /// print - Implement the dump method.
7986 virtual void print(std::ostream &O, const Module* = 0) const;
2020 #include "llvm/CodeGen/MachineInstr.h"
2121 #include "llvm/CodeGen/Passes.h"
2222 #include "llvm/CodeGen/RegAllocRegistry.h"
23 #include "llvm/CodeGen/RegisterCoalescer.h"
2324 #include "llvm/CodeGen/SSARegMap.h"
2425 #include "llvm/Target/MRegisterInfo.h"
2526 #include "llvm/Target/TargetMachine.h"
9596
9697 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
9798 AU.addRequired();
98 AU.addRequiredID(SimpleRegisterCoalescingID);
99 // Make sure PassManager knows which analyses to make available
100 // to coalescing and which analyses coalescing invalidates.
101 AU.addRequiredTransitive();
99102 MachineFunctionPass::getAnalysisUsage(AU);
100103 }
101104
192195 tm_ = &fn.getTarget();
193196 mri_ = tm_->getRegisterInfo();
194197 li_ = &getAnalysis();
198
199 // We don't run the coalescer here because we have no reason to
200 // interact with it. If the coalescer requires interaction, it
201 // won't do anything. If it doesn't require interaction, we assume
202 // it was run as a separate pass.
195203
196204 // If this is the first function compiled, compute the related reg classes.
197205 if (RelatedRegClasses.empty())
0 //===- RegisterCoalescer.cpp - Generic Register Coalescing Interface -------==//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file was developed by the LLVM research group and is distributed under
5 // the University of Illinois Open Source License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the generic RegisterCoalescer interface which
10 // is used as the common interface used by all clients and
11 // implementations of register coalescing.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/CodeGen/RegisterCoalescer.h"
16 #include "llvm/CodeGen/LiveIntervalAnalysis.h"
17 #include "llvm/CodeGen/MachineInstr.h"
18 #include "llvm/Target/MRegisterInfo.h"
19 #include "llvm/Pass.h"
20
21 using namespace llvm;
22
23 // Register the RegisterCoalescer interface, providing a nice name to refer to.
24 namespace {
25 RegisterAnalysisGroup Z("Register Coalescer");
26 }
27 char RegisterCoalescer::ID = 0;
28
29 // RegisterCoalescer destructor: DO NOT move this to the header file
30 // for RegisterCoalescer or else clients of the RegisterCoalescer
31 // class may not depend on the RegisterCoalescer.o file in the current
32 // .a file, causing alias analysis support to not be included in the
33 // tool correctly!
34 //
35 RegisterCoalescer::~RegisterCoalescer() {}
36
37 // Because of the way .a files work, we must force the SimpleRC
38 // implementation to be pulled in if the RegisterCoalescer classes are
39 // pulled in. Otherwise we run the risk of RegisterCoalescer being
40 // used, but the default implementation not being linked into the tool
41 // that uses it.
42 DEFINING_FILE_FOR(RegisterCoalescer)
2222 #include "llvm/CodeGen/MachineInstr.h"
2323 #include "llvm/CodeGen/Passes.h"
2424 #include "llvm/CodeGen/SSARegMap.h"
25 #include "llvm/CodeGen/RegisterCoalescer.h"
2526 #include "llvm/Target/MRegisterInfo.h"
2627 #include "llvm/Target/TargetInstrInfo.h"
2728 #include "llvm/Target/TargetMachine.h"
4748
4849 RegisterPass
4950 X("simple-register-coalescing", "Simple Register Coalescing");
51
52 // Declare that we implement the RegisterCoalescer interface
53 RegisterAnalysisGroup V(X);
5054 }
5155
5256 const PassInfo *llvm::SimpleRegisterCoalescingID = X.getPassInfo();
11901194 void SimpleRegisterCoalescing::print(std::ostream &O, const Module* m) const {
11911195 li_->print(O, m);
11921196 }
1197
1198 RegisterCoalescer* llvm::createSimpleRegisterCoalescer() {
1199 return new SimpleRegisterCoalescing();
1200 }
1201
1202 // Make sure that anything that uses RegisterCoalescer pulls in this file...
1203 DEFINING_FILE_FOR(SimpleRegisterCoalescing)