|
1 |
| -//===--- RedundantPhiElimination.cpp - Remove redundant phi arguments -----===// |
| 1 | +//===--- PhiArgumentOptimizations.cpp - phi argument optimizations --------===// |
2 | 2 | //
|
3 | 3 | // This source file is part of the Swift.org open source project
|
4 | 4 | //
|
|
10 | 10 | //
|
11 | 11 | //===----------------------------------------------------------------------===//
|
12 | 12 | //
|
13 |
| -// This pass eliminates redundant basic block arguments. |
| 13 | +// This file contains optimizations for basic block phi arguments. |
14 | 14 | //
|
15 | 15 | //===----------------------------------------------------------------------===//
|
16 | 16 |
|
@@ -200,8 +200,186 @@ bool RedundantPhiEliminationPass::valuesAreEqual(SILValue val1, SILValue val2) {
|
200 | 200 | return true;
|
201 | 201 | }
|
202 | 202 |
|
| 203 | +/// Replaces struct phi-arguments by a struct field. |
| 204 | +/// |
| 205 | +/// If only a single field of a struct phi-argument is used, replace the |
| 206 | +/// argument by the field value. |
| 207 | +/// |
| 208 | +/// \code |
| 209 | +/// br bb(%str) |
| 210 | +/// bb(%phi): |
| 211 | +/// %f = struct_extract %phi, #Field // the only use of %phi |
| 212 | +/// use %f |
| 213 | +/// \endcode |
| 214 | +/// |
| 215 | +/// is replaced with |
| 216 | +/// |
| 217 | +/// \code |
| 218 | +/// %f = struct_extract %str, #Field |
| 219 | +/// br bb(%f) |
| 220 | +/// bb(%phi): |
| 221 | +/// use %phi |
| 222 | +/// \endcode |
| 223 | +/// |
| 224 | +/// This also works if the phi-argument is in a def-use cycle. |
| 225 | +/// |
| 226 | +/// TODO: Handle tuples (but this is not so important). |
| 227 | +/// |
| 228 | +/// The PhiExpansionPass is not part of SimplifyCFG because |
| 229 | +/// * no other SimplifyCFG optimization depends on it. |
| 230 | +/// * compile time: it doesn't need to run every time SimplifyCFG runs. |
| 231 | +/// |
| 232 | +class PhiExpansionPass : public SILFunctionTransform { |
| 233 | +public: |
| 234 | + PhiExpansionPass() {} |
| 235 | + |
| 236 | + void run() override; |
| 237 | + |
| 238 | +private: |
| 239 | + bool optimizeArg(SILPhiArgument *initialArg); |
| 240 | +}; |
| 241 | + |
| 242 | +void PhiExpansionPass::run() { |
| 243 | + SILFunction *F = getFunction(); |
| 244 | + if (!F->shouldOptimize()) |
| 245 | + return; |
| 246 | + |
| 247 | + LLVM_DEBUG(llvm::dbgs() << "*** PhiReduction on function: " |
| 248 | + << F->getName() << " ***\n"); |
| 249 | + |
| 250 | + bool changed = false; |
| 251 | + for (SILBasicBlock &block : *getFunction()) { |
| 252 | + for (auto argAndIdx : enumerate(block.getArguments())) { |
| 253 | + if (!argAndIdx.value()->isPhiArgument()) |
| 254 | + continue; |
| 255 | + |
| 256 | + unsigned idx = argAndIdx.index(); |
| 257 | + |
| 258 | + // Try multiple times on the same argument to handle nested structs. |
| 259 | + while (optimizeArg(cast<SILPhiArgument>(block.getArgument(idx)))) { |
| 260 | + changed = true; |
| 261 | + } |
| 262 | + } |
| 263 | + } |
| 264 | + |
| 265 | + if (changed) { |
| 266 | + invalidateAnalysis(SILAnalysis::InvalidationKind::Instructions); |
| 267 | + } |
| 268 | +} |
| 269 | + |
| 270 | +bool PhiExpansionPass::optimizeArg(SILPhiArgument *initialArg) { |
| 271 | + llvm::SmallVector<const SILPhiArgument *, 8> collectedPhiArgs; |
| 272 | + llvm::SmallPtrSet<const SILPhiArgument *, 8> handled; |
| 273 | + collectedPhiArgs.push_back(initialArg); |
| 274 | + handled.insert(initialArg); |
| 275 | + |
| 276 | + VarDecl *field = nullptr; |
| 277 | + SILType newType; |
| 278 | + Optional<SILLocation> loc; |
| 279 | + |
| 280 | + // First step: collect all phi-arguments which can be transformed. |
| 281 | + unsigned workIdx = 0; |
| 282 | + while (workIdx < collectedPhiArgs.size()) { |
| 283 | + const SILArgument *arg = collectedPhiArgs[workIdx++]; |
| 284 | + for (Operand *use : arg->getUses()) { |
| 285 | + SILInstruction *user = use->getUser(); |
| 286 | + if (isa<DebugValueInst>(user)) |
| 287 | + continue; |
| 288 | + |
| 289 | + if (auto *extr = dyn_cast<StructExtractInst>(user)) { |
| 290 | + if (field && extr->getField() != field) |
| 291 | + return false; |
| 292 | + field = extr->getField(); |
| 293 | + newType = extr->getType(); |
| 294 | + loc = extr->getLoc(); |
| 295 | + continue; |
| 296 | + } |
| 297 | + if (auto *branch = dyn_cast<BranchInst>(user)) { |
| 298 | + const SILPhiArgument *destArg = branch->getArgForOperand(use); |
| 299 | + assert(destArg); |
| 300 | + if (handled.insert(destArg).second) |
| 301 | + collectedPhiArgs.push_back(destArg); |
| 302 | + continue; |
| 303 | + } |
| 304 | + if (auto *branch = dyn_cast<CondBranchInst>(user)) { |
| 305 | + const SILPhiArgument *destArg = branch->getArgForOperand(use); |
| 306 | + |
| 307 | + // destArg is null if the use is the condition and not a block argument. |
| 308 | + if (!destArg) |
| 309 | + return false; |
| 310 | + |
| 311 | + if (handled.insert(destArg).second) |
| 312 | + collectedPhiArgs.push_back(destArg); |
| 313 | + continue; |
| 314 | + } |
| 315 | + // An unexpected use -> bail. |
| 316 | + return false; |
| 317 | + } |
| 318 | + } |
| 319 | + |
| 320 | + if (!field) |
| 321 | + return false; |
| 322 | + |
| 323 | + // Second step: do the transformation. |
| 324 | + for (const SILPhiArgument *arg : collectedPhiArgs) { |
| 325 | + SILBasicBlock *block = arg->getParent(); |
| 326 | + SILArgument *newArg = block->replacePhiArgumentAndReplaceAllUses( |
| 327 | + arg->getIndex(), newType, arg->getOwnershipKind()); |
| 328 | + |
| 329 | + // First collect all users, then do the transformation. |
| 330 | + // We don't want to modify the use list while iterating over it. |
| 331 | + llvm::SmallVector<DebugValueInst *, 8> debugValueUsers; |
| 332 | + llvm::SmallVector<StructExtractInst *, 8> structExtractUsers; |
| 333 | + |
| 334 | + for (Operand *use : newArg->getUses()) { |
| 335 | + SILInstruction *user = use->getUser(); |
| 336 | + if (auto *dvi = dyn_cast<DebugValueInst>(user)) { |
| 337 | + debugValueUsers.push_back(dvi); |
| 338 | + continue; |
| 339 | + } |
| 340 | + if (auto *sei = dyn_cast<StructExtractInst>(user)) { |
| 341 | + structExtractUsers.push_back(sei); |
| 342 | + continue; |
| 343 | + } |
| 344 | + // Branches are handled below by handling incoming phi operands. |
| 345 | + assert(isa<BranchInst>(user) || isa<CondBranchInst>(user)); |
| 346 | + } |
| 347 | + |
| 348 | + for (DebugValueInst *dvi : debugValueUsers) { |
| 349 | + dvi->eraseFromParent(); |
| 350 | + } |
| 351 | + for (StructExtractInst *sei : structExtractUsers) { |
| 352 | + sei->replaceAllUsesWith(sei->getOperand()); |
| 353 | + sei->eraseFromParent(); |
| 354 | + } |
| 355 | + |
| 356 | + // "Move" the struct_extract to the predecessors. |
| 357 | + llvm::SmallVector<Operand *, 8> incomingOps; |
| 358 | + bool success = newArg->getIncomingPhiOperands(incomingOps); |
| 359 | + (void)success; |
| 360 | + assert(success && "could not get all incoming phi values"); |
| 361 | + |
| 362 | + for (Operand *op : incomingOps) { |
| 363 | + // Did we already handle the operand? |
| 364 | + if (op->get()->getType() == newType) |
| 365 | + continue; |
| 366 | + |
| 367 | + SILInstruction *branchInst = op->getUser(); |
| 368 | + SILBuilder builder(branchInst); |
| 369 | + auto *strExtract = builder.createStructExtract(loc.getValue(), |
| 370 | + op->get(), field, newType); |
| 371 | + op->set(strExtract); |
| 372 | + } |
| 373 | + } |
| 374 | + return true; |
| 375 | +} |
| 376 | + |
203 | 377 | } // end anonymous namespace
|
204 | 378 |
|
205 | 379 | SILTransform *swift::createRedundantPhiElimination() {
|
206 | 380 | return new RedundantPhiEliminationPass();
|
207 | 381 | }
|
| 382 | + |
| 383 | +SILTransform *swift::createPhiExpansion() { |
| 384 | + return new PhiExpansionPass(); |
| 385 | +} |
0 commit comments