From 35e351f5142d5dcbe140bfbabf216e568c39143d Mon Sep 17 00:00:00 2001 From: Roman Lebedev Date: Tue, 29 Jun 2021 13:25:32 +0300 Subject: [PATCH] [NFC][InstCombine] foldAggregateConstructionIntoAggregateReuse(): cast to Instruction eagerly In all of these, the value must be an instruction for us to succeed anyway, so change it to maybe hopefully make further changes more straight-forward. --- .../InstCombine/InstCombineVectorOps.cpp | 27 +++++++++---------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/lib/Transforms/InstCombine/InstCombineVectorOps.cpp b/lib/Transforms/InstCombine/InstCombineVectorOps.cpp index 1f9be3bbf37..c207f079bfc 100644 --- a/lib/Transforms/InstCombine/InstCombineVectorOps.cpp +++ b/lib/Transforms/InstCombine/InstCombineVectorOps.cpp @@ -796,12 +796,12 @@ Instruction *InstCombinerImpl::foldAggregateConstructionIntoAggregateReuse( // Try to find a value of each element of an aggregate. // FIXME: deal with more complex, not one-dimensional, aggregate types - SmallVector, 2> AggElts(NumAggElts, NotFound); + SmallVector, 2> AggElts(NumAggElts, NotFound); // Do we know values for each element of the aggregate? auto KnowAllElts = [&AggElts]() { return all_of(AggElts, - [](Optional Elt) { return Elt != NotFound; }); + [](Optional Elt) { return Elt != NotFound; }); }; int Depth = 0; @@ -816,7 +816,11 @@ Instruction *InstCombinerImpl::foldAggregateConstructionIntoAggregateReuse( Depth < DepthLimit && CurrIVI && !KnowAllElts(); CurrIVI = dyn_cast(CurrIVI->getAggregateOperand()), ++Depth) { - Value *InsertedValue = CurrIVI->getInsertedValueOperand(); + auto *InsertedValue = + dyn_cast(CurrIVI->getInsertedValueOperand()); + if (!InsertedValue) + return nullptr; // Inserted value must be produced by an instruction. + ArrayRef Indices = CurrIVI->getIndices(); // Don't bother with more than single-level aggregates. @@ -826,7 +830,7 @@ Instruction *InstCombinerImpl::foldAggregateConstructionIntoAggregateReuse( // Now, we may have already previously recorded the value for this element // of an aggregate. If we did, that means the CurrIVI will later be // overwritten with the already-recorded value. But if not, let's record it! - Optional &Elt = AggElts[Indices.front()]; + Optional &Elt = AggElts[Indices.front()]; Elt = Elt.getValueOr(InsertedValue); // FIXME: should we handle chain-terminating undef base operand? @@ -870,15 +874,15 @@ Instruction *InstCombinerImpl::foldAggregateConstructionIntoAggregateReuse( // If found, return the source aggregate from which the extraction was. // If \p PredBB is provided, does PHI translation of an \p Elt first. auto FindSourceAggregate = - [&](Value *Elt, unsigned EltIdx, Optional UseBB, + [&](Instruction *Elt, unsigned EltIdx, Optional UseBB, Optional PredBB) -> Optional { // For now(?), only deal with, at most, a single level of PHI indirection. if (UseBB && PredBB) - Elt = Elt->DoPHITranslation(*UseBB, *PredBB); + Elt = dyn_cast(Elt->DoPHITranslation(*UseBB, *PredBB)); // FIXME: deal with multiple levels of PHI indirection? // Did we find an extraction? - auto *EVI = dyn_cast(Elt); + auto *EVI = dyn_cast_or_null(Elt); if (!EVI) return NotFound; @@ -966,13 +970,8 @@ Instruction *InstCombinerImpl::foldAggregateConstructionIntoAggregateReuse( // they all should be defined in the same basic block. BasicBlock *UseBB = nullptr; - for (const Optional &Elt : AggElts) { - // If this element's value was not defined by an instruction, ignore it. - auto *I = dyn_cast(*Elt); - if (!I) - continue; - // Otherwise, in which basic block is this instruction located? - BasicBlock *BB = I->getParent(); + for (const Optional &I : AggElts) { + BasicBlock *BB = (*I)->getParent(); // If it's the first instruction we've encountered, record the basic block. if (!UseBB) { UseBB = BB;