Skip to content

Commit 785e706

Browse files
committed
[VPlan] Don't rely on underlying instr in VPWidenRecipe (NFCI).
VPWidenRecipe only needs the opcode to widen, all other information (flags, debug loc and operands) is already modeled directly via the recipe. This removes the remaining uses of the underlying instruction from VPWidenRecipe::execute.
1 parent ad2453a commit 785e706

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

llvm/lib/Transforms/Vectorize/VPlan.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1150,11 +1150,13 @@ class VPInstruction : public VPRecipeWithIRFlags, public VPValue {
11501150
/// ingredient. This recipe covers most of the traditional vectorization cases
11511151
/// where each ingredient transforms into a vectorized version of itself.
11521152
class VPWidenRecipe : public VPRecipeWithIRFlags, public VPValue {
1153+
unsigned Opcode;
11531154

11541155
public:
11551156
template <typename IterT>
11561157
VPWidenRecipe(Instruction &I, iterator_range<IterT> Operands)
1157-
: VPRecipeWithIRFlags(VPDef::VPWidenSC, Operands, I), VPValue(this, &I) {}
1158+
: VPRecipeWithIRFlags(VPDef::VPWidenSC, Operands, I), VPValue(this, &I),
1159+
Opcode(I.getOpcode()) {}
11581160

11591161
~VPWidenRecipe() override = default;
11601162

llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -654,9 +654,8 @@ void VPRecipeWithIRFlags::printFlags(raw_ostream &O) const {
654654

655655
void VPWidenRecipe::execute(VPTransformState &State) {
656656
State.setDebugLocFrom(getDebugLoc());
657-
auto &I = *cast<Instruction>(getUnderlyingValue());
658657
auto &Builder = State.Builder;
659-
switch (I.getOpcode()) {
658+
switch (Opcode) {
660659
case Instruction::Call:
661660
case Instruction::Br:
662661
case Instruction::PHI:
@@ -688,14 +687,14 @@ void VPWidenRecipe::execute(VPTransformState &State) {
688687
for (VPValue *VPOp : operands())
689688
Ops.push_back(State.get(VPOp, Part));
690689

691-
Value *V = Builder.CreateNAryOp(I.getOpcode(), Ops);
690+
Value *V = Builder.CreateNAryOp(Opcode, Ops);
692691

693692
if (auto *VecOp = dyn_cast<Instruction>(V))
694693
setFlags(VecOp);
695694

696695
// Use this vector value for all users of the original instruction.
697696
State.set(this, V, Part);
698-
State.addMetadata(V, &I);
697+
State.addMetadata(V, dyn_cast_or_null<Instruction>(getUnderlyingValue()));
699698
}
700699

701700
break;
@@ -712,29 +711,30 @@ void VPWidenRecipe::execute(VPTransformState &State) {
712711
case Instruction::ICmp:
713712
case Instruction::FCmp: {
714713
// Widen compares. Generate vector compares.
715-
bool FCmp = (I.getOpcode() == Instruction::FCmp);
716-
auto *Cmp = cast<CmpInst>(&I);
714+
bool FCmp = Opcode == Instruction::FCmp;
717715
for (unsigned Part = 0; Part < State.UF; ++Part) {
718716
Value *A = State.get(getOperand(0), Part);
719717
Value *B = State.get(getOperand(1), Part);
720718
Value *C = nullptr;
721719
if (FCmp) {
722720
// Propagate fast math flags.
723721
IRBuilder<>::FastMathFlagGuard FMFG(Builder);
724-
Builder.setFastMathFlags(Cmp->getFastMathFlags());
725-
C = Builder.CreateFCmp(Cmp->getPredicate(), A, B);
722+
if (auto *I = dyn_cast_or_null<Instruction>(getUnderlyingValue()))
723+
Builder.setFastMathFlags(I->getFastMathFlags());
724+
C = Builder.CreateFCmp(getPredicate(), A, B);
726725
} else {
727-
C = Builder.CreateICmp(Cmp->getPredicate(), A, B);
726+
C = Builder.CreateICmp(getPredicate(), A, B);
728727
}
729728
State.set(this, C, Part);
730-
State.addMetadata(C, &I);
729+
State.addMetadata(C, dyn_cast_or_null<Instruction>(getUnderlyingValue()));
731730
}
732731

733732
break;
734733
}
735734
default:
736735
// This instruction is not vectorized by simple widening.
737-
LLVM_DEBUG(dbgs() << "LV: Found an unhandled instruction: " << I);
736+
LLVM_DEBUG(dbgs() << "LV: Found an unhandled opcode : "
737+
<< Instruction::getOpcodeName(Opcode));
738738
llvm_unreachable("Unhandled instruction!");
739739
} // end of switch.
740740
}
@@ -743,8 +743,7 @@ void VPWidenRecipe::print(raw_ostream &O, const Twine &Indent,
743743
VPSlotTracker &SlotTracker) const {
744744
O << Indent << "WIDEN ";
745745
printAsOperand(O, SlotTracker);
746-
const Instruction *UI = getUnderlyingInstr();
747-
O << " = " << UI->getOpcodeName();
746+
O << " = " << Instruction::getOpcodeName(Opcode);
748747
printFlags(O);
749748
printOperands(O, SlotTracker);
750749
}

0 commit comments

Comments
 (0)