|
39 | 39 |
|
40 | 40 | using namespace swift;
|
41 | 41 |
|
42 |
| -//===----------------------------------------------------------------------===// |
43 |
| -// Utility |
44 |
| -//===----------------------------------------------------------------------===// |
45 |
| - |
46 |
| -/// \returns whether all the values are of trivial type in the provided |
47 |
| -/// function. |
48 |
| -template <typename Values> |
49 |
| -static bool areAllValuesTrivial(Values values, SILFunction &function) { |
50 |
| - return llvm::all_of(values, [&](SILValue value) -> bool { |
51 |
| - return value->getType().isTrivial(function); |
52 |
| - }); |
53 |
| -} |
54 |
| - |
55 |
| -//===----------------------------------------------------------------------===// |
56 |
| -// MandatoryCombiner Interface |
57 |
| -//===----------------------------------------------------------------------===// |
58 |
| - |
59 | 42 | namespace {
|
60 | 43 |
|
61 | 44 | class MandatoryCombiner final
|
@@ -88,186 +71,182 @@ class MandatoryCombiner final
|
88 | 71 | [&](SILInstruction *instruction) { worklist.add(instruction); }),
|
89 | 72 | createdInstructions(createdInstructions){};
|
90 | 73 |
|
91 |
| - void addReachableCodeToWorklist(SILFunction &function); |
92 |
| - |
93 |
| - /// \return whether a change was made. |
94 |
| - bool doOneIteration(SILFunction &function, unsigned iteration); |
| 74 | + /// Base visitor that does not do anything. |
| 75 | + SILInstruction *visitSILInstruction(SILInstruction *) { return nullptr; } |
95 | 76 |
|
96 |
| - void clear() { |
97 |
| - iteration = 0; |
98 |
| - worklist.resetChecked(); |
99 |
| - madeChange = false; |
| 77 | + /// \returns whether all the values are of trivial type in the provided |
| 78 | + /// function. |
| 79 | + template <typename Values> |
| 80 | + static bool areAllValuesTrivial(Values values, SILFunction &function) { |
| 81 | + return llvm::all_of(values, [&](SILValue value) -> bool { |
| 82 | + return value->getType().isTrivial(function); |
| 83 | + }); |
100 | 84 | }
|
101 | 85 |
|
102 |
| - /// Applies the MandatoryCombiner to the provided function. |
103 |
| - /// |
104 |
| - /// \param function the function to which to apply the MandatoryCombiner. |
105 |
| - /// |
106 |
| - /// \return whether a change was made. |
107 |
| - bool runOnFunction(SILFunction &function) { |
108 |
| - bool changed = false; |
109 |
| - |
110 |
| - while (doOneIteration(function, iteration)) { |
111 |
| - changed = true; |
112 |
| - ++iteration; |
| 86 | + SILInstruction *visitApplyInst(ApplyInst *instruction) { |
| 87 | + // Apply this pass only to partial applies all of whose arguments are |
| 88 | + // trivial. |
| 89 | + auto calledValue = instruction->getCallee(); |
| 90 | + if (calledValue == nullptr) { |
| 91 | + return nullptr; |
| 92 | + } |
| 93 | + auto fullApplyCallee = calledValue->getDefiningInstruction(); |
| 94 | + if (fullApplyCallee == nullptr) { |
| 95 | + return nullptr; |
| 96 | + } |
| 97 | + auto partialApply = dyn_cast<PartialApplyInst>(fullApplyCallee); |
| 98 | + if (partialApply == nullptr) { |
| 99 | + return nullptr; |
| 100 | + } |
| 101 | + auto *function = partialApply->getCalleeFunction(); |
| 102 | + if (function == nullptr) { |
| 103 | + return nullptr; |
| 104 | + } |
| 105 | + ApplySite fullApplySite(instruction); |
| 106 | + auto fullApplyArguments = fullApplySite.getArguments(); |
| 107 | + if (!areAllValuesTrivial(fullApplyArguments, *function)) { |
| 108 | + return nullptr; |
| 109 | + } |
| 110 | + auto partialApplyArguments = ApplySite(partialApply).getArguments(); |
| 111 | + if (!areAllValuesTrivial(partialApplyArguments, *function)) { |
| 112 | + return nullptr; |
113 | 113 | }
|
114 | 114 |
|
115 |
| - return changed; |
116 |
| - } |
| 115 | + auto callee = partialApply->getCallee(); |
117 | 116 |
|
118 |
| - /// Base visitor that does not do anything. |
119 |
| - SILInstruction *visitSILInstruction(SILInstruction *) { return nullptr; } |
120 |
| - SILInstruction *visitApplyInst(ApplyInst *instruction); |
121 |
| -}; |
| 117 | + ApplySite partialApplySite(partialApply); |
122 | 118 |
|
123 |
| -} // end anonymous namespace |
| 119 | + SmallVector<SILValue, 8> argsVec; |
| 120 | + llvm::copy(fullApplyArguments, std::back_inserter(argsVec)); |
| 121 | + llvm::copy(partialApplyArguments, std::back_inserter(argsVec)); |
124 | 122 |
|
125 |
| -//===----------------------------------------------------------------------===// |
126 |
| -// MandatoryCombiner Non-Visitor Utility Methods |
127 |
| -//===----------------------------------------------------------------------===// |
| 123 | + SILBuilderWithScope builder(instruction, &createdInstructions); |
| 124 | + ApplyInst *replacement = builder.createApply( |
| 125 | + /*Loc=*/instruction->getDebugLocation().getLocation(), /*Fn=*/callee, |
| 126 | + /*Subs=*/partialApply->getSubstitutionMap(), |
| 127 | + /*Args*/ argsVec, |
| 128 | + /*isNonThrowing=*/instruction->isNonThrowing(), |
| 129 | + /*SpecializationInfo=*/partialApply->getSpecializationInfo()); |
128 | 130 |
|
129 |
| -void MandatoryCombiner::addReachableCodeToWorklist(SILFunction &function) { |
130 |
| - SmallBlotSetVector<SILBasicBlock *, 32> blockWorklist; |
131 |
| - SmallBlotSetVector<SILBasicBlock *, 32> blocksVisited; |
132 |
| - SmallVector<SILInstruction *, 128> instructions; |
133 |
| - |
134 |
| - blockWorklist.insert(&*function.begin()); |
135 |
| - while (!blockWorklist.empty()) { |
136 |
| - auto *block = blockWorklist.pop_back_val().getValueOr(nullptr); |
137 |
| - if (block == nullptr) { |
138 |
| - continue; |
139 |
| - } |
| 131 | + worklist.replaceInstructionWithInstruction(instruction, replacement |
| 132 | +#ifndef NDEBUG |
| 133 | + , |
| 134 | + /*instructionDescription=*/"" |
| 135 | +#endif |
| 136 | + ); |
| 137 | + tryDeleteDeadClosure(partialApply, instModCallbacks); |
| 138 | + return nullptr; |
| 139 | + } |
140 | 140 |
|
141 |
| - if (!blocksVisited.insert(block).second) { |
142 |
| - continue; |
143 |
| - } |
| 141 | + void addReachableCodeToWorklist(SILFunction &function) { |
| 142 | + SmallBlotSetVector<SILBasicBlock *, 32> blockWorklist; |
| 143 | + SmallBlotSetVector<SILBasicBlock *, 32> blocksVisited; |
| 144 | + SmallVector<SILInstruction *, 128> instructions; |
144 | 145 |
|
145 |
| - for (auto iterator = block->begin(), end = block->end(); iterator != end;) { |
146 |
| - auto *instruction = &*iterator; |
147 |
| - ++iterator; |
| 146 | + blockWorklist.insert(&*function.begin()); |
| 147 | + while (!blockWorklist.empty()) { |
| 148 | + auto *block = blockWorklist.pop_back_val().getValueOr(nullptr); |
| 149 | + if (block == nullptr) { |
| 150 | + continue; |
| 151 | + } |
148 | 152 |
|
149 |
| - if (isInstructionTriviallyDead(instruction)) { |
| 153 | + if (!blocksVisited.insert(block).second) { |
150 | 154 | continue;
|
151 | 155 | }
|
152 | 156 |
|
153 |
| - instructions.push_back(instruction); |
| 157 | + for (auto iterator = block->begin(), end = block->end(); iterator != end;) { |
| 158 | + auto *instruction = &*iterator; |
| 159 | + ++iterator; |
| 160 | + |
| 161 | + if (isInstructionTriviallyDead(instruction)) { |
| 162 | + continue; |
| 163 | + } |
| 164 | + |
| 165 | + instructions.push_back(instruction); |
| 166 | + } |
| 167 | + |
| 168 | + for_each(block->getSuccessorBlocks(), [&](SILBasicBlock *block) { |
| 169 | + blockWorklist.insert(block); |
| 170 | + }); |
154 | 171 | }
|
155 | 172 |
|
156 |
| - for_each(block->getSuccessorBlocks(), |
157 |
| - [&](SILBasicBlock *block) { blockWorklist.insert(block); }); |
| 173 | + worklist.addInitialGroup(instructions); |
158 | 174 | }
|
159 | 175 |
|
160 |
| - worklist.addInitialGroup(instructions); |
161 |
| -} |
162 |
| - |
163 |
| -bool MandatoryCombiner::doOneIteration(SILFunction &function, |
164 |
| - unsigned iteration) { |
165 |
| - madeChange = false; |
| 176 | + /// \return whether a change was made. |
| 177 | + bool doOneIteration(SILFunction &function, unsigned iteration) { |
| 178 | + madeChange = false; |
166 | 179 |
|
167 |
| - addReachableCodeToWorklist(function); |
| 180 | + addReachableCodeToWorklist(function); |
168 | 181 |
|
169 |
| - while (!worklist.isEmpty()) { |
170 |
| - auto *instruction = worklist.pop_back_val(); |
171 |
| - if (instruction == nullptr) { |
172 |
| - continue; |
173 |
| - } |
| 182 | + while (!worklist.isEmpty()) { |
| 183 | + auto *instruction = worklist.pop_back_val(); |
| 184 | + if (instruction == nullptr) { |
| 185 | + continue; |
| 186 | + } |
174 | 187 |
|
175 | 188 | #ifndef NDEBUG
|
176 |
| - std::string instructionDescription; |
| 189 | + std::string instructionDescription; |
177 | 190 | #endif
|
178 |
| - LLVM_DEBUG(llvm::raw_string_ostream SS(instructionDescription); |
179 |
| - instruction->print(SS); instructionDescription = SS.str();); |
180 |
| - LLVM_DEBUG(llvm::dbgs() |
181 |
| - << "MC: Visiting: " << instructionDescription << '\n'); |
| 191 | + LLVM_DEBUG(llvm::raw_string_ostream SS(instructionDescription); |
| 192 | + instruction->print(SS); instructionDescription = SS.str();); |
| 193 | + LLVM_DEBUG(llvm::dbgs() |
| 194 | + << "MC: Visiting: " << instructionDescription << '\n'); |
182 | 195 |
|
183 |
| - if (auto replacement = visit(instruction)) { |
184 |
| - worklist.replaceInstructionWithInstruction(instruction, replacement |
| 196 | + if (auto replacement = visit(instruction)) { |
| 197 | + worklist.replaceInstructionWithInstruction(instruction, replacement |
185 | 198 | #ifndef NDEBUG
|
186 |
| - , |
187 |
| - instructionDescription |
| 199 | + , instructionDescription |
188 | 200 | #endif
|
189 |
| - ); |
190 |
| - } |
| 201 | + ); |
| 202 | + } |
191 | 203 |
|
192 |
| - for (SILInstruction *instruction : instructionsPendingDeletion) { |
193 |
| - worklist.eraseInstFromFunction(*instruction); |
194 |
| - } |
195 |
| - instructionsPendingDeletion.clear(); |
196 |
| - |
197 |
| - // Our tracking list has been accumulating instructions created by the |
198 |
| - // SILBuilder during this iteration. Go through the tracking list and add |
199 |
| - // its contents to the worklist and then clear said list in preparation |
200 |
| - // for the next iteration. |
201 |
| - for (SILInstruction *instruction : createdInstructions) { |
202 |
| - LLVM_DEBUG(llvm::dbgs() << "MC: add " << *instruction |
203 |
| - << " from tracking list to worklist\n"); |
204 |
| - worklist.add(instruction); |
| 204 | + for (SILInstruction *instruction : instructionsPendingDeletion) { |
| 205 | + worklist.eraseInstFromFunction(*instruction); |
| 206 | + } |
| 207 | + instructionsPendingDeletion.clear(); |
| 208 | + |
| 209 | + // Our tracking list has been accumulating instructions created by the |
| 210 | + // SILBuilder during this iteration. Go through the tracking list and add |
| 211 | + // its contents to the worklist and then clear said list in preparation |
| 212 | + // for the next iteration. |
| 213 | + for (SILInstruction *instruction : createdInstructions) { |
| 214 | + LLVM_DEBUG(llvm::dbgs() << "MC: add " << *instruction |
| 215 | + << " from tracking list to worklist\n"); |
| 216 | + worklist.add(instruction); |
| 217 | + } |
| 218 | + createdInstructions.clear(); |
205 | 219 | }
|
206 |
| - createdInstructions.clear(); |
207 |
| - } |
208 |
| - |
209 |
| - worklist.resetChecked(); |
210 |
| - return madeChange; |
211 |
| -} |
212 | 220 |
|
213 |
| -//===----------------------------------------------------------------------===// |
214 |
| -// MandatoryCombiner Visitor Methods |
215 |
| -//===----------------------------------------------------------------------===// |
216 |
| - |
217 |
| -SILInstruction *MandatoryCombiner::visitApplyInst(ApplyInst *instruction) { |
218 |
| - // Apply this pass only to partial applies all of whose arguments are |
219 |
| - // trivial. |
220 |
| - auto calledValue = instruction->getCallee(); |
221 |
| - if (calledValue == nullptr) { |
222 |
| - return nullptr; |
223 |
| - } |
224 |
| - auto fullApplyCallee = calledValue->getDefiningInstruction(); |
225 |
| - if (fullApplyCallee == nullptr) { |
226 |
| - return nullptr; |
227 |
| - } |
228 |
| - auto partialApply = dyn_cast<PartialApplyInst>(fullApplyCallee); |
229 |
| - if (partialApply == nullptr) { |
230 |
| - return nullptr; |
231 |
| - } |
232 |
| - auto *function = partialApply->getCalleeFunction(); |
233 |
| - if (function == nullptr) { |
234 |
| - return nullptr; |
235 |
| - } |
236 |
| - ApplySite fullApplySite(instruction); |
237 |
| - auto fullApplyArguments = fullApplySite.getArguments(); |
238 |
| - if (!areAllValuesTrivial(fullApplyArguments, *function)) { |
239 |
| - return nullptr; |
| 221 | + worklist.resetChecked(); |
| 222 | + return madeChange; |
240 | 223 | }
|
241 |
| - auto partialApplyArguments = ApplySite(partialApply).getArguments(); |
242 |
| - if (!areAllValuesTrivial(partialApplyArguments, *function)) { |
243 |
| - return nullptr; |
| 224 | + |
| 225 | + void clear() { |
| 226 | + iteration = 0; |
| 227 | + worklist.resetChecked(); |
| 228 | + madeChange = false; |
244 | 229 | }
|
245 | 230 |
|
246 |
| - auto callee = partialApply->getCallee(); |
247 | 231 |
|
248 |
| - ApplySite partialApplySite(partialApply); |
| 232 | + /// Applies the MandatoryCombiner to the provided function. |
| 233 | + /// |
| 234 | + /// \param function the function to which to apply the MandatoryCombiner. |
| 235 | + /// |
| 236 | + /// \return whether a change was made. |
| 237 | + bool runOnFunction(SILFunction &function) { |
| 238 | + bool changed = false; |
249 | 239 |
|
250 |
| - SmallVector<SILValue, 8> argsVec; |
251 |
| - llvm::copy(fullApplyArguments, std::back_inserter(argsVec)); |
252 |
| - llvm::copy(partialApplyArguments, std::back_inserter(argsVec)); |
| 240 | + while (doOneIteration(function, iteration)) { |
| 241 | + changed = true; |
| 242 | + ++iteration; |
| 243 | + } |
253 | 244 |
|
254 |
| - SILBuilderWithScope builder(instruction, &createdInstructions); |
255 |
| - ApplyInst *replacement = builder.createApply( |
256 |
| - /*Loc=*/instruction->getDebugLocation().getLocation(), /*Fn=*/callee, |
257 |
| - /*Subs=*/partialApply->getSubstitutionMap(), |
258 |
| - /*Args*/ argsVec, |
259 |
| - /*isNonThrowing=*/instruction->isNonThrowing(), |
260 |
| - /*SpecializationInfo=*/partialApply->getSpecializationInfo()); |
| 245 | + return changed; |
| 246 | + } |
| 247 | +}; |
261 | 248 |
|
262 |
| - worklist.replaceInstructionWithInstruction(instruction, replacement |
263 |
| -#ifndef NDEBUG |
264 |
| - , |
265 |
| - /*instructionDescription=*/"" |
266 |
| -#endif |
267 |
| - ); |
268 |
| - tryDeleteDeadClosure(partialApply, instModCallbacks); |
269 |
| - return nullptr; |
270 |
| -} |
| 249 | +} // end anonymous namespace |
271 | 250 |
|
272 | 251 | //===----------------------------------------------------------------------===//
|
273 | 252 | // Top Level Entrypoint
|
|
0 commit comments