60
60
#include " llvm/ADT/SmallVector.h"
61
61
#include " llvm/IR/IRBuilder.h"
62
62
63
- namespace lgc {
64
-
65
63
class TypeLowering ;
66
64
67
65
// / Given a type, check if it should be replaced.
68
66
// /
69
- // / Return an empty vector if this function doesn't know how to handle the given type. Subsequent conversion rules will
70
- // / then be considered.
67
+ // / Return an empty vector if this function doesn't know how to handle the given
68
+ // / type. Subsequent conversion rules will then be considered.
71
69
// /
72
- // / Otherwise, return a vector with the replacement type(s). If the type is known to remain unchanged, return a
73
- // / singleton vector containing just the original type.
70
+ // / Otherwise, return a vector with the replacement type(s). If the type is
71
+ // / known to remain unchanged, return a singleton vector containing just the
72
+ // / original type.
74
73
using TypeLoweringFn = llvm::SmallVector<llvm::Type *>(TypeLowering &, llvm::Type *);
75
74
76
- // / Given a constant that is known to be meant to be replaced based on its type, attempt to replace it.
75
+ // / Given a constant that is known to be meant to be replaced based on its type,
76
+ // / attempt to replace it.
77
77
// /
78
78
// / Return a non-empty vector if this function was able to handle the constant.
79
79
// /
80
- // / Otherwise, return an empty vector, and subsequent rules will be applied. Default rules exist for poison, undef,
81
- // / and "null-like" (zeroinitializer etc.).
80
+ // / Otherwise, return an empty vector, and subsequent rules will be applied.
81
+ // / Default rules exist for poison, undef, and "null-like" (zeroinitializer
82
+ // / etc.).
82
83
using ConstantTypeLoweringFn = llvm::SmallVector<llvm::Constant *>(TypeLowering &, llvm::Constant *,
83
84
llvm::ArrayRef<llvm::Type *>);
84
85
85
86
// =====================================================================================================================
86
- // / Helper for lowerings that need to replace values of one type by one or more values of another type.
87
+ // / Helper for lowerings that need to replace values of one type by one or more
88
+ // / values of another type.
87
89
// /
88
90
// / This helper really has two parts:
89
91
// /
90
- // / - A type-level part that applies @ref TypeLoweringFn rules and caches the result
91
- // / - A value-level part that maintains a mapping of replaced values and provides generic handlers for core
92
+ // / - A type-level part that applies @ref TypeLoweringFn rules and caches the
93
+ // / result
94
+ // / - A value-level part that maintains a mapping of replaced values and
95
+ // / provides generic handlers for core
92
96
// / instructions like phi, select, and alloca
93
97
// /
94
- // / The type-level part can be reused even as the value-level part is cleared by @ref finishCleanup, assuming that the
95
- // / type replacements are consistent (which they might not always be, e.g. where the replacement depends on the target
96
- // / architecture).
98
+ // / The type-level part can be reused even as the value-level part is cleared by
99
+ // / @ref finishCleanup, assuming that the type replacements are consistent
100
+ // / (which they might not always be, e.g. where the replacement depends on the
101
+ // / target architecture).
97
102
// /
98
- // / The value-level part is meant to be used as a nested @ref llvm_dialects::Visitor client. It requires RPO traversal
99
- // / order. Its intended use is along the following lines:
103
+ // / The value-level part is meant to be used as a nested @ref
104
+ // / llvm_dialects::Visitor client. It requires RPO traversal order. Its intended
105
+ // / use is along the following lines:
100
106
// / @code
101
107
// / struct MyPayload {
102
108
// / TypeLowering lowering;
@@ -108,8 +114,9 @@ using ConstantTypeLoweringFn = llvm::SmallVector<llvm::Constant *>(TypeLowering
108
114
// /
109
115
// / MyPayload payload;
110
116
// /
111
- // / // Reverse post order traversal through functions, replacing instructions with converted types as we go.
112
- // / static const auto visitor = VisitorBuilder<MyPayload>
117
+ // / // Reverse post order traversal through functions, replacing instructions
118
+ // / with converted types as we go. static const auto visitor =
119
+ // / VisitorBuilder<MyPayload>
113
120
// / .add(...)
114
121
// / .nest(&TypeLowering::registerVisitors)
115
122
// / .build();
@@ -118,42 +125,42 @@ using ConstantTypeLoweringFn = llvm::SmallVector<llvm::Constant *>(TypeLowering
118
125
// / // Fixup phi nodes.
119
126
// / payload.lowering.finishPhis();
120
127
// /
121
- // / // Erase all instructions that "have been replaced" (by calling replaceInstruction for them).
122
- // / payload.lowering.finishCleanup();
128
+ // / // Erase all instructions that "have been replaced" (by calling
129
+ // / replaceInstruction for them). payload.lowering.finishCleanup();
123
130
// / @endcode
124
131
class TypeLowering {
125
132
public:
126
- TypeLowering (llvm::LLVMContext &context );
133
+ TypeLowering (llvm::LLVMContext &);
127
134
128
135
llvm::LLVMContext &getContext () const { return m_builder.getContext (); }
129
136
130
- void addRule (std::function<TypeLoweringFn> rule );
131
- void addConstantRule (std::function<ConstantTypeLoweringFn> rule );
137
+ void addRule (std::function<TypeLoweringFn>);
138
+ void addConstantRule (std::function<ConstantTypeLoweringFn>);
132
139
133
- llvm::ArrayRef<llvm::Type *> convertType (llvm::Type *type );
140
+ llvm::ArrayRef<llvm::Type *> convertType (llvm::Type *);
134
141
135
- static void registerVisitors (llvm_dialects::VisitorBuilder<TypeLowering> &builder );
142
+ static void registerVisitors (llvm_dialects::VisitorBuilder<TypeLowering> &);
136
143
137
- llvm::SmallVector<llvm::Value *> getValue (llvm::Value *value );
138
- llvm::SmallVector<llvm::Value *> getValueOptional (llvm::Value *value );
139
- void replaceInstruction (llvm::Instruction *inst , llvm::ArrayRef<llvm::Value *> mapping );
140
- void eraseInstruction (llvm::Instruction *inst );
144
+ llvm::SmallVector<llvm::Value *> getValue (llvm::Value *);
145
+ llvm::SmallVector<llvm::Value *> getValueOptional (llvm::Value *);
146
+ void replaceInstruction (llvm::Instruction *, llvm::ArrayRef<llvm::Value *>);
147
+ void eraseInstruction (llvm::Instruction *);
141
148
142
- llvm::Function *lowerFunctionArguments (llvm::Function &fn );
149
+ llvm::Function *lowerFunctionArguments (llvm::Function &);
143
150
void finishPhis ();
144
151
bool finishCleanup ();
145
152
146
153
private:
147
- void recordValue (llvm::Value *value , llvm::ArrayRef<llvm::Value *> mapping );
148
- void replaceMappingWith (llvm::Value *toReplace , llvm::Value *with );
154
+ void recordValue (llvm::Value *, llvm::ArrayRef<llvm::Value *>);
155
+ void replaceMappingWith (llvm::Value *, llvm::Value *);
149
156
150
- void visitAlloca (llvm::AllocaInst &alloca );
151
- void visitExtract (llvm::ExtractValueInst &extract );
152
- void visitInsert (llvm::InsertValueInst &insert );
153
- void visitLoad (llvm::LoadInst &load );
154
- void visitPhi (llvm::PHINode &phi );
155
- void visitSelect (llvm::SelectInst &select );
156
- void visitStore (llvm::StoreInst &store );
157
+ void visitAlloca (llvm::AllocaInst &);
158
+ void visitExtract (llvm::ExtractValueInst &);
159
+ void visitInsert (llvm::InsertValueInst &);
160
+ void visitLoad (llvm::LoadInst &);
161
+ void visitPhi (llvm::PHINode &);
162
+ void visitSelect (llvm::SelectInst &);
163
+ void visitStore (llvm::StoreInst &);
157
164
158
165
// / Type conversion rules.
159
166
llvm::SmallVector<std::function<TypeLoweringFn>> m_rules;
@@ -170,18 +177,18 @@ class TypeLowering {
170
177
// / Map original values to type-converted values.
171
178
// /
172
179
// / For 1-1 mappings, this stores a value pointer.
173
- // / For 1-N mappings, this stores ((index << 1) | 1), where index is the index into m_convertedValueList at which the
174
- // / converted values can be found.
180
+ // / For 1-N mappings, this stores ((index << 1) | 1), where index is the index
181
+ // / into m_convertedValueList at which the converted values can be found.
175
182
llvm::DenseMap<llvm::Value *, uintptr_t > m_valueMap;
176
183
std::vector<llvm::Value *> m_convertedValueList;
177
184
178
- // / Reverse map of values that occur as type-converted values to where they occur. The vector elements are either a
179
- // / value pointer (for 1-1 mapped values) or ((index << 1) | 1), where index is the index into m_convertedValueList.
185
+ // / Reverse map of values that occur as type-converted values to where they
186
+ // / occur. The vector elements are either a value pointer (for 1-1 mapped
187
+ // / values) or ((index << 1) | 1), where index is the index into
188
+ // / m_convertedValueList.
180
189
llvm::DenseMap<llvm::Value *, llvm::SmallVector<uintptr_t >> m_valueReverseMap;
181
190
182
191
std::vector<std::pair<llvm::PHINode *, llvm::SmallVector<llvm::PHINode *>>> m_phis;
183
192
std::vector<llvm::Instruction *> m_instructionsToErase;
184
- llvm::SmallVector<llvm::Function *> m_functionToErase ;
193
+ llvm::SmallVector<llvm::Function *> m_functionsToErase ;
185
194
};
186
-
187
- } // namespace lgc
0 commit comments