15f621567SSimon Moll //===- VectorBuilder.cpp - Builder for VP Intrinsics ----------------------===//
25f621567SSimon Moll //
35f621567SSimon Moll // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f621567SSimon Moll // See https://llvm.org/LICENSE.txt for license information.
55f621567SSimon Moll // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65f621567SSimon Moll //
75f621567SSimon Moll //===----------------------------------------------------------------------===//
85f621567SSimon Moll //
95f621567SSimon Moll // This file implements the VectorBuilder class, which is used as a convenient
105f621567SSimon Moll // way to create VP intrinsics as if they were LLVM instructions with a
115f621567SSimon Moll // consistent and simplified interface.
125f621567SSimon Moll //
135f621567SSimon Moll //===----------------------------------------------------------------------===//
145f621567SSimon Moll 
155f621567SSimon Moll #include <llvm/ADT/SmallVector.h>
165f621567SSimon Moll #include <llvm/IR/FPEnv.h>
175f621567SSimon Moll #include <llvm/IR/Instructions.h>
185f621567SSimon Moll #include <llvm/IR/IntrinsicInst.h>
195f621567SSimon Moll #include <llvm/IR/Intrinsics.h>
205f621567SSimon Moll #include <llvm/IR/VectorBuilder.h>
215f621567SSimon Moll 
225f621567SSimon Moll namespace llvm {
235f621567SSimon Moll 
handleError(const char * ErrorMsg) const245f621567SSimon Moll void VectorBuilder::handleError(const char *ErrorMsg) const {
255f621567SSimon Moll   if (ErrorHandling == Behavior::SilentlyReturnNone)
265f621567SSimon Moll     return;
275f621567SSimon Moll   report_fatal_error(ErrorMsg);
285f621567SSimon Moll }
295f621567SSimon Moll 
getModule() const305f621567SSimon Moll Module &VectorBuilder::getModule() const {
315f621567SSimon Moll   return *Builder.GetInsertBlock()->getModule();
325f621567SSimon Moll }
335f621567SSimon Moll 
getAllTrueMask()345f621567SSimon Moll Value *VectorBuilder::getAllTrueMask() {
355f621567SSimon Moll   auto *BoolTy = Builder.getInt1Ty();
365f621567SSimon Moll   auto *MaskTy = VectorType::get(BoolTy, StaticVectorLength);
375f621567SSimon Moll   return ConstantInt::getAllOnesValue(MaskTy);
385f621567SSimon Moll }
395f621567SSimon Moll 
requestMask()405f621567SSimon Moll Value &VectorBuilder::requestMask() {
415f621567SSimon Moll   if (Mask)
425f621567SSimon Moll     return *Mask;
435f621567SSimon Moll 
445f621567SSimon Moll   return *getAllTrueMask();
455f621567SSimon Moll }
465f621567SSimon Moll 
requestEVL()475f621567SSimon Moll Value &VectorBuilder::requestEVL() {
485f621567SSimon Moll   if (ExplicitVectorLength)
495f621567SSimon Moll     return *ExplicitVectorLength;
505f621567SSimon Moll 
515f621567SSimon Moll   assert(!StaticVectorLength.isScalable() && "TODO vscale lowering");
525f621567SSimon Moll   auto *IntTy = Builder.getInt32Ty();
535f621567SSimon Moll   return *ConstantInt::get(IntTy, StaticVectorLength.getFixedValue());
545f621567SSimon Moll }
555f621567SSimon Moll 
createVectorInstruction(unsigned Opcode,Type * ReturnTy,ArrayRef<Value * > InstOpArray,const Twine & Name)565f621567SSimon Moll Value *VectorBuilder::createVectorInstruction(unsigned Opcode, Type *ReturnTy,
575f621567SSimon Moll                                               ArrayRef<Value *> InstOpArray,
585f621567SSimon Moll                                               const Twine &Name) {
595f621567SSimon Moll   auto VPID = VPIntrinsic::getForOpcode(Opcode);
605f621567SSimon Moll   if (VPID == Intrinsic::not_intrinsic)
615f621567SSimon Moll     return returnWithError<Value *>("No VPIntrinsic for this opcode");
625f621567SSimon Moll 
635f621567SSimon Moll   auto MaskPosOpt = VPIntrinsic::getMaskParamPos(VPID);
645f621567SSimon Moll   auto VLenPosOpt = VPIntrinsic::getVectorLengthParamPos(VPID);
655f621567SSimon Moll   size_t NumInstParams = InstOpArray.size();
665f621567SSimon Moll   size_t NumVPParams =
670916d96dSKazu Hirata       NumInstParams + MaskPosOpt.has_value() + VLenPosOpt.has_value();
685f621567SSimon Moll 
695f621567SSimon Moll   SmallVector<Value *, 6> IntrinParams;
705f621567SSimon Moll 
715f621567SSimon Moll   // Whether the mask and vlen parameter are at the end of the parameter list.
725f621567SSimon Moll   bool TrailingMaskAndVLen =
73129b531cSKazu Hirata       std::min<size_t>(MaskPosOpt.value_or(NumInstParams),
74129b531cSKazu Hirata                        VLenPosOpt.value_or(NumInstParams)) >= NumInstParams;
755f621567SSimon Moll 
765f621567SSimon Moll   if (TrailingMaskAndVLen) {
775f621567SSimon Moll     // Fast path for trailing mask, vector length.
785f621567SSimon Moll     IntrinParams.append(InstOpArray.begin(), InstOpArray.end());
795f621567SSimon Moll     IntrinParams.resize(NumVPParams);
805f621567SSimon Moll   } else {
815f621567SSimon Moll     IntrinParams.resize(NumVPParams);
825f621567SSimon Moll     // Insert mask and evl operands in between the instruction operands.
835f621567SSimon Moll     for (size_t VPParamIdx = 0, ParamIdx = 0; VPParamIdx < NumVPParams;
845f621567SSimon Moll          ++VPParamIdx) {
85129b531cSKazu Hirata       if ((MaskPosOpt && MaskPosOpt.value_or(NumVPParams) == VPParamIdx) ||
86129b531cSKazu Hirata           (VLenPosOpt && VLenPosOpt.value_or(NumVPParams) == VPParamIdx))
875f621567SSimon Moll         continue;
885f621567SSimon Moll       assert(ParamIdx < NumInstParams);
895f621567SSimon Moll       IntrinParams[VPParamIdx] = InstOpArray[ParamIdx++];
905f621567SSimon Moll     }
915f621567SSimon Moll   }
925f621567SSimon Moll 
93*a7938c74SKazu Hirata   if (MaskPosOpt)
945f621567SSimon Moll     IntrinParams[*MaskPosOpt] = &requestMask();
95*a7938c74SKazu Hirata   if (VLenPosOpt)
965f621567SSimon Moll     IntrinParams[*VLenPosOpt] = &requestEVL();
975f621567SSimon Moll 
985f621567SSimon Moll   auto *VPDecl = VPIntrinsic::getDeclarationForParams(&getModule(), VPID,
995f621567SSimon Moll                                                       ReturnTy, IntrinParams);
1005f621567SSimon Moll   return Builder.CreateCall(VPDecl, IntrinParams, Name);
1015f621567SSimon Moll }
1025f621567SSimon Moll 
1035f621567SSimon Moll } // namespace llvm
104