|
| 1 | +//===- VPlanConstantFolder.h - ConstantFolder for VPlan -------------------===// |
| 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 | +#ifndef LLVM_TRANSFORMS_VECTORIZE_VPLANCONSTANTFOLDER_H |
| 10 | +#define LLVM_TRANSFORMS_VECTORIZE_VPLANCONSTANTFOLDER_H |
| 11 | + |
| 12 | +#include "VPlan.h" |
| 13 | +#include "VPlanValue.h" |
| 14 | +#include "llvm/Analysis/TargetFolder.h" |
| 15 | + |
| 16 | +namespace llvm { |
| 17 | +class VPConstantFolder { |
| 18 | + TargetFolder Folder; |
| 19 | + VPTypeAnalysis TypeInfo; |
| 20 | + |
| 21 | + Constant *getIRConstant(VPValue *V) const { |
| 22 | + if (!V->isLiveIn()) |
| 23 | + return nullptr; |
| 24 | + return dyn_cast_if_present<Constant>(V->getLiveInIRValue()); |
| 25 | + } |
| 26 | + |
| 27 | + Value *foldBinOp(Instruction::BinaryOps Opcode, VPValue *LHS, |
| 28 | + VPValue *RHS) const { |
| 29 | + auto *LC = getIRConstant(LHS); |
| 30 | + auto *RC = getIRConstant(RHS); |
| 31 | + if (LC && RC) |
| 32 | + return Folder.FoldBinOp(Opcode, LC, RC); |
| 33 | + return nullptr; |
| 34 | + } |
| 35 | + |
| 36 | + Value *foldNot(VPValue *Op) const { |
| 37 | + auto *C = getIRConstant(Op); |
| 38 | + if (C) |
| 39 | + return Folder.FoldBinOp(Instruction::BinaryOps::Xor, C, |
| 40 | + Constant::getAllOnesValue(C->getType())); |
| 41 | + return nullptr; |
| 42 | + } |
| 43 | + |
| 44 | + Value *foldLogicalAnd(VPValue *LHS, VPValue *RHS) const { |
| 45 | + auto *LC = getIRConstant(LHS); |
| 46 | + auto *RC = getIRConstant(RHS); |
| 47 | + if (LC && RC) |
| 48 | + return Folder.FoldSelect(LC, RC, |
| 49 | + ConstantInt::getNullValue(RC->getType())); |
| 50 | + return nullptr; |
| 51 | + } |
| 52 | + |
| 53 | + Value *foldSelect(VPValue *Cond, VPValue *TrueVal, VPValue *FalseVal) const { |
| 54 | + auto *CC = getIRConstant(Cond); |
| 55 | + auto *TV = getIRConstant(TrueVal); |
| 56 | + auto *FV = getIRConstant(FalseVal); |
| 57 | + if (CC && TV && FV) |
| 58 | + return Folder.FoldSelect(CC, TV, FV); |
| 59 | + return nullptr; |
| 60 | + } |
| 61 | + |
| 62 | + Value *foldCmp(CmpInst::Predicate Pred, VPValue *LHS, VPValue *RHS) const { |
| 63 | + auto *LC = getIRConstant(LHS); |
| 64 | + auto *RC = getIRConstant(RHS); |
| 65 | + if (LC && RC) |
| 66 | + return Folder.FoldCmp(Pred, LC, RC); |
| 67 | + return nullptr; |
| 68 | + } |
| 69 | + |
| 70 | + Value *foldGEP(Type *Ty, VPValue *Base, ArrayRef<VPValue *> Offsets, |
| 71 | + GEPNoWrapFlags NW) const { |
| 72 | + auto *BC = getIRConstant(Base); |
| 73 | + if (!BC) |
| 74 | + return nullptr; |
| 75 | + SmallVector<Value *> IdxList; |
| 76 | + for (auto *O : Offsets) { |
| 77 | + if (auto *OffsetV = getIRConstant(O)) |
| 78 | + IdxList.emplace_back(OffsetV); |
| 79 | + else |
| 80 | + return nullptr; |
| 81 | + } |
| 82 | + return Folder.FoldGEP(Ty, BC, IdxList, NW); |
| 83 | + } |
| 84 | + |
| 85 | + Value *foldInsertElement(VPValue *Vec, VPValue *NewElt, VPValue *Idx) const { |
| 86 | + auto *VC = getIRConstant(Vec); |
| 87 | + auto *EC = getIRConstant(NewElt); |
| 88 | + auto *IC = getIRConstant(Idx); |
| 89 | + if (VC && EC && IC) |
| 90 | + Folder.FoldInsertElement(VC, EC, IC); |
| 91 | + return nullptr; |
| 92 | + } |
| 93 | + |
| 94 | + Value *foldExtractElement(VPValue *Vec, VPValue *Idx) const { |
| 95 | + auto *VC = getIRConstant(Vec); |
| 96 | + auto *IC = getIRConstant(Idx); |
| 97 | + if (VC && IC) |
| 98 | + Folder.FoldExtractElement(VC, IC); |
| 99 | + return nullptr; |
| 100 | + } |
| 101 | + |
| 102 | + Value *foldCast(Instruction::CastOps Opcode, VPValue *Op, |
| 103 | + Type *DestTy) const { |
| 104 | + auto *C = getIRConstant(Op); |
| 105 | + if (C) |
| 106 | + return Folder.FoldCast(Opcode, C, DestTy); |
| 107 | + return nullptr; |
| 108 | + } |
| 109 | + |
| 110 | +public: |
| 111 | + VPConstantFolder(const DataLayout &DL, const VPTypeAnalysis &TypeInfo) |
| 112 | + : Folder(DL), TypeInfo(TypeInfo) {} |
| 113 | + |
| 114 | + Value *tryToConstantFold(VPRecipeBase &R, unsigned Opcode, |
| 115 | + ArrayRef<VPValue *> Ops) { |
| 116 | + switch (Opcode) { |
| 117 | + case Instruction::BinaryOps::Add: |
| 118 | + case Instruction::BinaryOps::Sub: |
| 119 | + case Instruction::BinaryOps::Mul: |
| 120 | + case Instruction::BinaryOps::AShr: |
| 121 | + case Instruction::BinaryOps::LShr: |
| 122 | + case Instruction::BinaryOps::And: |
| 123 | + case Instruction::BinaryOps::Or: |
| 124 | + case Instruction::BinaryOps::Xor: |
| 125 | + return foldBinOp(static_cast<Instruction::BinaryOps>(Opcode), Ops[0], |
| 126 | + Ops[1]); |
| 127 | + case VPInstruction::LogicalAnd: |
| 128 | + return foldLogicalAnd(Ops[0], Ops[1]); |
| 129 | + case VPInstruction::Not: |
| 130 | + return foldNot(Ops[0]); |
| 131 | + case Instruction::Select: |
| 132 | + return foldSelect(Ops[0], Ops[1], Ops[2]); |
| 133 | + case Instruction::ICmp: |
| 134 | + case Instruction::FCmp: |
| 135 | + return foldCmp(cast<VPRecipeWithIRFlags>(R).getPredicate(), Ops[0], |
| 136 | + Ops[1]); |
| 137 | + case Instruction::GetElementPtr: |
| 138 | + case VPInstruction::PtrAdd: |
| 139 | + return foldGEP(TypeInfo.inferScalarType(R.getVPSingleValue()), Ops[0], |
| 140 | + Ops.drop_front(), |
| 141 | + cast<VPRecipeWithIRFlags>(R).getGEPNoWrapFlags()); |
| 142 | + case Instruction::InsertElement: |
| 143 | + return foldInsertElement(Ops[0], Ops[1], Ops[2]); |
| 144 | + case Instruction::ExtractElement: |
| 145 | + return foldExtractElement(Ops[0], Ops[1]); |
| 146 | + case Instruction::CastOps::SExt: |
| 147 | + case Instruction::CastOps::ZExt: |
| 148 | + case Instruction::CastOps::Trunc: |
| 149 | + return foldCast(static_cast<Instruction::CastOps>(Opcode), Ops[0], |
| 150 | + TypeInfo.inferScalarType(R.getVPSingleValue())); |
| 151 | + } |
| 152 | + return nullptr; |
| 153 | + } |
| 154 | +}; |
| 155 | +} // namespace llvm |
| 156 | + |
| 157 | +#endif |
0 commit comments