|
| 1 | +//===- VPlanAnalysis.cpp - Various Analyses working on VPlan ----*- C++ -*-===// |
| 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 | +#include "VPlanAnalysis.h" |
| 10 | +#include "VPlan.h" |
| 11 | + |
| 12 | +using namespace llvm; |
| 13 | + |
| 14 | +#define DEBUG_TYPE "vplan" |
| 15 | + |
| 16 | +Type *VPTypeAnalysis::inferType(const VPBlendRecipe *R) { |
| 17 | + return inferType(R->getIncomingValue(0)); |
| 18 | +} |
| 19 | + |
| 20 | +Type *VPTypeAnalysis::inferType(const VPInstruction *R) { |
| 21 | + switch (R->getOpcode()) { |
| 22 | + case Instruction::Select: |
| 23 | + return inferType(R->getOperand(1)); |
| 24 | + case VPInstruction::FirstOrderRecurrenceSplice: |
| 25 | + return inferType(R->getOperand(0)); |
| 26 | + default: |
| 27 | + llvm_unreachable("Unhandled instruction!"); |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +Type *VPTypeAnalysis::inferType(const VPInterleaveRecipe *R) { return nullptr; } |
| 32 | + |
| 33 | +Type *VPTypeAnalysis::inferType(const VPReductionPHIRecipe *R) { |
| 34 | + return R->getOperand(0)->getLiveInIRValue()->getType(); |
| 35 | +} |
| 36 | + |
| 37 | +Type *VPTypeAnalysis::inferType(const VPWidenRecipe *R) { |
| 38 | + unsigned Opcode = R->getOpcode(); |
| 39 | + switch (Opcode) { |
| 40 | + case Instruction::ICmp: |
| 41 | + case Instruction::FCmp: |
| 42 | + return IntegerType::get(Ctx, 1); |
| 43 | + case Instruction::UDiv: |
| 44 | + case Instruction::SDiv: |
| 45 | + case Instruction::SRem: |
| 46 | + case Instruction::URem: |
| 47 | + case Instruction::Add: |
| 48 | + case Instruction::FAdd: |
| 49 | + case Instruction::Sub: |
| 50 | + case Instruction::FSub: |
| 51 | + case Instruction::FNeg: |
| 52 | + case Instruction::Mul: |
| 53 | + case Instruction::FMul: |
| 54 | + case Instruction::FDiv: |
| 55 | + case Instruction::FRem: |
| 56 | + case Instruction::Shl: |
| 57 | + case Instruction::LShr: |
| 58 | + case Instruction::AShr: |
| 59 | + case Instruction::And: |
| 60 | + case Instruction::Or: |
| 61 | + case Instruction::Xor: { |
| 62 | + Type *ResTy = inferType(R->getOperand(0)); |
| 63 | + if (Opcode != Instruction::FNeg) { |
| 64 | + assert(ResTy == inferType(R->getOperand(1))); |
| 65 | + CachedTypes[R->getOperand(1)] = ResTy; |
| 66 | + } |
| 67 | + return ResTy; |
| 68 | + } |
| 69 | + case Instruction::Freeze: |
| 70 | + return inferType(R->getOperand(0)); |
| 71 | + default: |
| 72 | + // This instruction is not vectorized by simple widening. |
| 73 | + // LLVM_DEBUG(dbgs() << "LV: Found an unhandled instruction: " << I); |
| 74 | + llvm_unreachable("Unhandled instruction!"); |
| 75 | + } |
| 76 | + |
| 77 | + return nullptr; |
| 78 | +} |
| 79 | + |
| 80 | +Type *VPTypeAnalysis::inferType(const VPWidenCallRecipe *R) { |
| 81 | + auto &CI = *cast<CallInst>(R->getUnderlyingInstr()); |
| 82 | + return CI.getType(); |
| 83 | +} |
| 84 | + |
| 85 | +Type *VPTypeAnalysis::inferType(const VPWidenIntOrFpInductionRecipe *R) { |
| 86 | + return R->getScalarType(); |
| 87 | +} |
| 88 | + |
| 89 | +Type *VPTypeAnalysis::inferType(const VPWidenMemoryInstructionRecipe *R) { |
| 90 | + if (R->isStore()) |
| 91 | + return cast<StoreInst>(&R->getIngredient())->getValueOperand()->getType(); |
| 92 | + |
| 93 | + return cast<LoadInst>(&R->getIngredient())->getType(); |
| 94 | +} |
| 95 | + |
| 96 | +Type *VPTypeAnalysis::inferType(const VPWidenSelectRecipe *R) { |
| 97 | + return inferType(R->getOperand(1)); |
| 98 | +} |
| 99 | + |
| 100 | +Type *VPTypeAnalysis::inferType(const VPReplicateRecipe *R) { |
| 101 | + switch (R->getUnderlyingInstr()->getOpcode()) { |
| 102 | + case Instruction::Call: { |
| 103 | + unsigned CallIdx = R->getNumOperands() - (R->isPredicated() ? 2 : 1); |
| 104 | + return cast<Function>(R->getOperand(CallIdx)->getLiveInIRValue()) |
| 105 | + ->getReturnType(); |
| 106 | + } |
| 107 | + case Instruction::UDiv: |
| 108 | + case Instruction::SDiv: |
| 109 | + case Instruction::SRem: |
| 110 | + case Instruction::URem: |
| 111 | + case Instruction::Add: |
| 112 | + case Instruction::FAdd: |
| 113 | + case Instruction::Sub: |
| 114 | + case Instruction::FSub: |
| 115 | + case Instruction::FNeg: |
| 116 | + case Instruction::Mul: |
| 117 | + case Instruction::FMul: |
| 118 | + case Instruction::FDiv: |
| 119 | + case Instruction::FRem: |
| 120 | + case Instruction::Shl: |
| 121 | + case Instruction::LShr: |
| 122 | + case Instruction::AShr: |
| 123 | + case Instruction::And: |
| 124 | + case Instruction::Or: |
| 125 | + case Instruction::Xor: |
| 126 | + case Instruction::ICmp: |
| 127 | + case Instruction::FCmp: { |
| 128 | + Type *ResTy = inferType(R->getOperand(0)); |
| 129 | + assert(ResTy == inferType(R->getOperand(1))); |
| 130 | + CachedTypes[R->getOperand(1)] = ResTy; |
| 131 | + return ResTy; |
| 132 | + } |
| 133 | + case Instruction::Trunc: |
| 134 | + case Instruction::SExt: |
| 135 | + case Instruction::ZExt: |
| 136 | + case Instruction::FPExt: |
| 137 | + case Instruction::FPTrunc: |
| 138 | + return R->getUnderlyingInstr()->getType(); |
| 139 | + case Instruction::ExtractValue: { |
| 140 | + return R->getUnderlyingValue()->getType(); |
| 141 | + } |
| 142 | + case Instruction::Freeze: |
| 143 | + return inferType(R->getOperand(0)); |
| 144 | + case Instruction::Load: |
| 145 | + return cast<LoadInst>(R->getUnderlyingInstr())->getType(); |
| 146 | + case Instruction::Store: |
| 147 | + return cast<StoreInst>(R->getUnderlyingInstr()) |
| 148 | + ->getValueOperand() |
| 149 | + ->getType(); |
| 150 | + default: |
| 151 | + llvm_unreachable("Unhandled instruction"); |
| 152 | + } |
| 153 | + |
| 154 | + return nullptr; |
| 155 | +} |
| 156 | + |
| 157 | +Type *VPTypeAnalysis::inferType(const VPValue *V) { |
| 158 | + auto Iter = CachedTypes.find(V); |
| 159 | + if (Iter != CachedTypes.end()) |
| 160 | + return Iter->second; |
| 161 | + |
| 162 | + Type *ResultTy = nullptr; |
| 163 | + if (V->isLiveIn()) |
| 164 | + ResultTy = V->getLiveInIRValue()->getType(); |
| 165 | + else { |
| 166 | + const VPRecipeBase *Def = V->getDefiningRecipe(); |
| 167 | + switch (Def->getVPDefID()) { |
| 168 | + case VPDef::VPBlendSC: |
| 169 | + ResultTy = inferType(cast<VPBlendRecipe>(Def)); |
| 170 | + break; |
| 171 | + case VPDef::VPCanonicalIVPHISC: |
| 172 | + ResultTy = cast<VPCanonicalIVPHIRecipe>(Def)->getScalarType(); |
| 173 | + break; |
| 174 | + case VPDef::VPFirstOrderRecurrencePHISC: |
| 175 | + ResultTy = Def->getOperand(0)->getLiveInIRValue()->getType(); |
| 176 | + break; |
| 177 | + case VPDef::VPInstructionSC: |
| 178 | + ResultTy = inferType(cast<VPInstruction>(Def)); |
| 179 | + break; |
| 180 | + case VPDef::VPInterleaveSC: |
| 181 | + ResultTy = V->getUnderlyingValue() |
| 182 | + ->getType(); // inferType(cast<VPInterleaveRecipe>(Def)); |
| 183 | + break; |
| 184 | + case VPDef::VPPredInstPHISC: |
| 185 | + ResultTy = inferType(Def->getOperand(0)); |
| 186 | + break; |
| 187 | + case VPDef::VPReductionPHISC: |
| 188 | + ResultTy = inferType(cast<VPReductionPHIRecipe>(Def)); |
| 189 | + break; |
| 190 | + case VPDef::VPReplicateSC: |
| 191 | + ResultTy = inferType(cast<VPReplicateRecipe>(Def)); |
| 192 | + break; |
| 193 | + case VPDef::VPScalarIVStepsSC: |
| 194 | + return inferType(Def->getOperand(0)); |
| 195 | + break; |
| 196 | + case VPDef::VPWidenSC: |
| 197 | + ResultTy = inferType(cast<VPWidenRecipe>(Def)); |
| 198 | + break; |
| 199 | + case VPDef::VPWidenPHISC: |
| 200 | + return inferType(Def->getOperand(0)); |
| 201 | + case VPDef::VPWidenPointerInductionSC: |
| 202 | + return inferType(Def->getOperand(0)); |
| 203 | + case VPDef::VPWidenCallSC: |
| 204 | + ResultTy = inferType(cast<VPWidenCallRecipe>(Def)); |
| 205 | + break; |
| 206 | + case VPDef::VPWidenCastSC: |
| 207 | + ResultTy = cast<VPWidenCastRecipe>(Def)->getResultType(); |
| 208 | + break; |
| 209 | + case VPDef::VPWidenGEPSC: |
| 210 | + ResultTy = PointerType::get(Ctx, 0); |
| 211 | + break; |
| 212 | + case VPDef::VPWidenIntOrFpInductionSC: |
| 213 | + ResultTy = inferType(cast<VPWidenIntOrFpInductionRecipe>(Def)); |
| 214 | + break; |
| 215 | + case VPDef::VPWidenMemoryInstructionSC: |
| 216 | + ResultTy = inferType(cast<VPWidenMemoryInstructionRecipe>(Def)); |
| 217 | + break; |
| 218 | + case VPDef::VPWidenSelectSC: |
| 219 | + ResultTy = inferType(cast<VPWidenSelectRecipe>(Def)); |
| 220 | + break; |
| 221 | + } |
| 222 | + } |
| 223 | + CachedTypes[V] = ResultTy; |
| 224 | + return ResultTy; |
| 225 | +} |
0 commit comments