diff --git a/lib/Transforms/Scalar/LoopUnrollPass.cpp b/lib/Transforms/Scalar/LoopUnrollPass.cpp index e4771dbc89b..63d3cc6f628 100644 --- a/lib/Transforms/Scalar/LoopUnrollPass.cpp +++ b/lib/Transforms/Scalar/LoopUnrollPass.cpp @@ -509,6 +509,15 @@ public: // worklist. SmallPtrSet OperandSet; + // Lambda to enque operands onto the worklist. + auto EnqueueOperands = [&](Instruction &I) { + OperandSet.clear(); + for (auto *Op : I.operand_values()) + if (auto *OpI = dyn_cast(Op)) + if (OperandSet.insert(OpI).second) + Worklist.push_back(OpI); + }; + // Start by initializing worklist with simplified instructions. for (auto &FoldedKeyValue : SimplifiedValues) if (auto *FoldedInst = dyn_cast(FoldedKeyValue.first)) { @@ -516,11 +525,7 @@ public: // Add each instruction operand of this dead instruction to the // worklist. - OperandSet.clear(); - for (auto *Op : FoldedInst->operand_values()) - if (auto *OpI = dyn_cast(Op)) - if (OperandSet.insert(OpI).second) - Worklist.push_back(OpI); + EnqueueOperands(*FoldedInst); } // If a definition of an insn is only used by simplified or dead @@ -545,11 +550,7 @@ public: if (AllUsersFolded) { NumberOfOptimizedInstructions += TTI.getUserCost(I); DeadInstructions.insert(I); - OperandSet.clear(); - for (auto *Op : I->operand_values()) - if (auto *OpI = dyn_cast(Op)) - if (OperandSet.insert(OpI).second) - Worklist.push_back(OpI); + EnqueueOperands(*I); } } return NumberOfOptimizedInstructions;