LLVM  13.0.0git
VPlanTransforms.cpp
Go to the documentation of this file.
1 //===-- VPlanTransforms.cpp - Utility VPlan to VPlan transforms -----------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 /// This file implements a set of utility VPlan to VPlan transformations.
11 ///
12 //===----------------------------------------------------------------------===//
13 
14 #include "VPlanTransforms.h"
16 
17 using namespace llvm;
18 
20  Loop *OrigLoop, VPlanPtr &Plan,
22  SmallPtrSetImpl<Instruction *> &DeadInstructions, ScalarEvolution &SE) {
23 
24  auto *TopRegion = cast<VPRegionBlock>(Plan->getEntry());
25  ReversePostOrderTraversal<VPBlockBase *> RPOT(TopRegion->getEntry());
26 
27  for (VPBlockBase *Base : RPOT) {
28  // Do not widen instructions in pre-header and exit blocks.
29  if (Base->getNumPredecessors() == 0 || Base->getNumSuccessors() == 0)
30  continue;
31 
32  VPBasicBlock *VPBB = Base->getEntryBasicBlock();
33  // Introduce each ingredient into VPlan.
34  for (auto I = VPBB->begin(), E = VPBB->end(); I != E;) {
35  VPRecipeBase *Ingredient = &*I++;
36  VPValue *VPV = Ingredient->getVPValue();
37  Instruction *Inst = cast<Instruction>(VPV->getUnderlyingValue());
38  if (DeadInstructions.count(Inst)) {
39  VPValue DummyValue;
40  VPV->replaceAllUsesWith(&DummyValue);
41  Ingredient->eraseFromParent();
42  continue;
43  }
44 
45  VPRecipeBase *NewRecipe = nullptr;
46  if (auto *VPPhi = dyn_cast<VPWidenPHIRecipe>(Ingredient)) {
47  auto *Phi = cast<PHINode>(VPPhi->getUnderlyingValue());
48  InductionDescriptor II = Inductions.lookup(Phi);
51  VPValue *Start = Plan->getOrAddVPValue(II.getStartValue());
52  NewRecipe = new VPWidenIntOrFpInductionRecipe(Phi, Start, nullptr);
53  } else {
54  Plan->addVPValue(Phi, VPPhi);
55  continue;
56  }
57  } else {
58  assert(isa<VPInstruction>(Ingredient) &&
59  "only VPInstructions expected here");
60  assert(!isa<PHINode>(Inst) && "phis should be handled above");
61  // Create VPWidenMemoryInstructionRecipe for loads and stores.
62  if (LoadInst *Load = dyn_cast<LoadInst>(Inst)) {
63  NewRecipe = new VPWidenMemoryInstructionRecipe(
64  *Load, Plan->getOrAddVPValue(getLoadStorePointerOperand(Inst)),
65  nullptr /*Mask*/);
66  } else if (StoreInst *Store = dyn_cast<StoreInst>(Inst)) {
67  NewRecipe = new VPWidenMemoryInstructionRecipe(
68  *Store, Plan->getOrAddVPValue(getLoadStorePointerOperand(Inst)),
69  Plan->getOrAddVPValue(Store->getValueOperand()),
70  nullptr /*Mask*/);
71  } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Inst)) {
72  NewRecipe = new VPWidenGEPRecipe(
73  GEP, Plan->mapToVPValues(GEP->operands()), OrigLoop);
74  } else if (CallInst *CI = dyn_cast<CallInst>(Inst)) {
75  NewRecipe = new VPWidenCallRecipe(
76  *CI, Plan->mapToVPValues(CI->arg_operands()));
77  } else if (SelectInst *SI = dyn_cast<SelectInst>(Inst)) {
78  bool InvariantCond =
79  SE.isLoopInvariant(SE.getSCEV(SI->getOperand(0)), OrigLoop);
80  NewRecipe = new VPWidenSelectRecipe(
81  *SI, Plan->mapToVPValues(SI->operands()), InvariantCond);
82  } else {
83  NewRecipe =
84  new VPWidenRecipe(*Inst, Plan->mapToVPValues(Inst->operands()));
85  }
86  }
87 
88  NewRecipe->insertBefore(Ingredient);
89  if (NewRecipe->getNumDefinedValues() == 1)
90  VPV->replaceAllUsesWith(NewRecipe->getVPValue());
91  else
92  assert(NewRecipe->getNumDefinedValues() == 0 &&
93  "Only recpies with zero or one defined values expected");
94  Ingredient->eraseFromParent();
95  Plan->removeVPValueFor(Inst);
96  for (auto *Def : NewRecipe->definedValues()) {
97  Plan->addVPValue(Inst, Def);
98  }
99  }
100  }
101 }
llvm
Definition: AllocatorList.h:23
llvm::tgtok::Def
@ Def
Definition: TGLexer.h:50
llvm::VPRecipeBase::eraseFromParent
iplist< VPRecipeBase >::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
Definition: VPlan.cpp:535
llvm::User::operands
op_range operands()
Definition: User.h:242
llvm::VPBasicBlock
VPBasicBlock serves as the leaf of the Hierarchical Control-Flow Graph.
Definition: VPlan.h:1430
llvm::Loop
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:529
llvm::ScalarEvolution
The main scalar evolution driver.
Definition: ScalarEvolution.h:443
llvm::InductionDescriptor::IK_IntInduction
@ IK_IntInduction
Integer induction variable. Step = C.
Definition: IVDescriptors.h:272
llvm::VPlanPtr
std::unique_ptr< VPlan > VPlanPtr
Definition: VPlan.h:91
llvm::MapVector< PHINode *, InductionDescriptor >
llvm::VPWidenCallRecipe
A recipe for widening Call instructions.
Definition: VPlan.h:834
llvm::InductionDescriptor::getKind
InductionKind getKind() const
Definition: IVDescriptors.h:282
llvm::VPBlockBase
VPBlockBase is the building block of the Hierarchical Control-Flow Graph.
Definition: VPlan.h:345
llvm::InductionDescriptor
A struct for saving information about induction variables.
Definition: IVDescriptors.h:267
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
SI
@ SI
Definition: SIInstrInfo.cpp:7342
llvm::Instruction
Definition: Instruction.h:45
llvm::VPValue::replaceAllUsesWith
void replaceAllUsesWith(VPValue *New)
Definition: VPlan.cpp:1137
llvm::VPValue
Definition: VPlanValue.h:44
llvm::VPDef::getNumDefinedValues
unsigned getNumDefinedValues() const
Returns the number of values defined by the VPDef.
Definition: VPlanValue.h:347
llvm::VPBasicBlock::end
iterator end()
Definition: VPlan.h:1461
llvm::ScalarEvolution::getSCEV
const SCEV * getSCEV(Value *V)
Return a SCEV expression for the full generality of the specified expression.
Definition: ScalarEvolution.cpp:3885
llvm::InductionDescriptor::getStartValue
Value * getStartValue() const
Definition: IVDescriptors.h:281
llvm::StoreInst
An instruction for storing to memory.
Definition: Instructions.h:303
llvm::VPWidenRecipe
VPWidenRecipe is a recipe for producing a copy of vector type its ingredient.
Definition: VPlan.h:806
I
#define I(x, y, z)
Definition: MD5.cpp:59
llvm::GetElementPtrInst
an instruction for type-safe pointer arithmetic to access elements of arrays and structs
Definition: Instructions.h:905
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
VPlanTransforms.h
llvm::VPWidenIntOrFpInductionRecipe
A recipe for handling phi nodes of integer and floating-point inductions, producing their vector and ...
Definition: VPlan.h:932
llvm::VPWidenGEPRecipe
A recipe for handling GEP instructions.
Definition: VPlan.h:891
llvm::InductionDescriptor::IK_FpInduction
@ IK_FpInduction
Floating point induction variable.
Definition: IVDescriptors.h:274
llvm::SelectInst
This class represents the LLVM 'select' instruction.
Definition: Instructions.h:1715
llvm::VPWidenMemoryInstructionRecipe
A Recipe for widening load/store operations.
Definition: VPlan.h:1338
llvm::VPWidenSelectRecipe
A recipe for widening select instructions.
Definition: VPlan.h:860
llvm::SmallPtrSetImpl::count
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:382
llvm::SPII::Load
@ Load
Definition: SparcInstrInfo.h:32
llvm::MapVector::lookup
ValueT lookup(const KeyT &Key) const
Definition: MapVector.h:110
llvm::VPlanTransforms::VPInstructionsToVPRecipes
static void VPInstructionsToVPRecipes(Loop *OrigLoop, VPlanPtr &Plan, LoopVectorizationLegality::InductionList &Inductions, SmallPtrSetImpl< Instruction * > &DeadInstructions, ScalarEvolution &SE)
Replaces the VPInstructions in Plan with corresponding widen recipes.
Definition: VPlanTransforms.cpp:19
llvm::LoadInst
An instruction for reading from memory.
Definition: Instructions.h:174
llvm::VPBlockBase::getEntryBasicBlock
const VPBasicBlock * getEntryBasicBlock() const
Definition: VPlan.cpp:140
llvm::SPII::Store
@ Store
Definition: SparcInstrInfo.h:33
llvm::ScalarEvolution::isLoopInvariant
bool isLoopInvariant(const SCEV *S, const Loop *L)
Return true if the value of the given SCEV is unchanging in the specified loop.
Definition: ScalarEvolution.cpp:12480
llvm::VPRecipeBase
VPRecipeBase is a base class modeling a sequence of one or more output IR instructions.
Definition: VPlan.h:617
llvm::ReversePostOrderTraversal
Definition: PostOrderIterator.h:290
PostOrderIterator.h
llvm::VPBasicBlock::begin
iterator begin()
Recipe iterator methods.
Definition: VPlan.h:1459
llvm::VPDef::definedValues
ArrayRef< VPValue * > definedValues()
Returns an ArrayRef of the values defined by the VPDef.
Definition: VPlanValue.h:342
llvm::SmallPtrSetImpl< Instruction * >
llvm::getLoadStorePointerOperand
const Value * getLoadStorePointerOperand(const Value *V)
A helper function that returns the pointer operand of a load or store instruction.
Definition: Instructions.h:5237
llvm::CallInst
This class represents a function call, abstracting a target machine's calling convention.
Definition: Instructions.h:1450
GEP
Hexagon Common GEP
Definition: HexagonCommonGEP.cpp:171
llvm::VPDef::getVPValue
VPValue * getVPValue(unsigned I=0)
Returns the VPValue with index I defined by the VPDef.
Definition: VPlanValue.h:332
llvm::VPValue::getUnderlyingValue
Value * getUnderlyingValue()
Return the underlying Value attached to this VPValue.
Definition: VPlanValue.h:84
llvm::VPRecipeBase::insertBefore
void insertBefore(VPRecipeBase *InsertPos)
Insert an unlinked recipe into a basic block immediately before the specified recipe.
Definition: VPlan.cpp:513