| 1 | //===- LowerVectorIntrinsics.cpp ------------------------------------------===// |
| 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 "llvm/Transforms/Utils/LowerVectorIntrinsics.h" |
| 10 | #include "llvm/IR/IRBuilder.h" |
| 11 | #include "llvm/IR/Module.h" |
| 12 | |
| 13 | #define DEBUG_TYPE "lower-vector-intrinsics" |
| 14 | |
| 15 | using namespace llvm; |
| 16 | |
| 17 | bool llvm::lowerUnaryVectorIntrinsicAsLoop(Module &M, CallInst *CI) { |
| 18 | Type *ArgTy = CI->getArgOperand(i: 0)->getType(); |
| 19 | VectorType *VecTy = cast<VectorType>(Val: ArgTy); |
| 20 | |
| 21 | BasicBlock *PreLoopBB = CI->getParent(); |
| 22 | BasicBlock *PostLoopBB = nullptr; |
| 23 | Function *ParentFunc = PreLoopBB->getParent(); |
| 24 | LLVMContext &Ctx = PreLoopBB->getContext(); |
| 25 | Type *IdxTy = M.getDataLayout().getIndexType(C&: Ctx, AddressSpace: 0); |
| 26 | |
| 27 | PostLoopBB = PreLoopBB->splitBasicBlock(I: CI); |
| 28 | BasicBlock *LoopBB = BasicBlock::Create(Context&: Ctx, Name: "" , Parent: ParentFunc, InsertBefore: PostLoopBB); |
| 29 | PreLoopBB->getTerminator()->setSuccessor(Idx: 0, BB: LoopBB); |
| 30 | |
| 31 | // Loop preheader |
| 32 | IRBuilder<> PreLoopBuilder(PreLoopBB->getTerminator()); |
| 33 | Value *LoopEnd = |
| 34 | PreLoopBuilder.CreateElementCount(Ty: IdxTy, EC: VecTy->getElementCount()); |
| 35 | |
| 36 | // Loop body |
| 37 | IRBuilder<> LoopBuilder(LoopBB); |
| 38 | |
| 39 | PHINode *LoopIndex = LoopBuilder.CreatePHI(Ty: IdxTy, NumReservedValues: 2); |
| 40 | LoopIndex->addIncoming(V: ConstantInt::get(Ty: IdxTy, V: 0U), BB: PreLoopBB); |
| 41 | PHINode *Vec = LoopBuilder.CreatePHI(Ty: VecTy, NumReservedValues: 2); |
| 42 | Vec->addIncoming(V: CI->getArgOperand(i: 0), BB: PreLoopBB); |
| 43 | |
| 44 | Value *Elem = LoopBuilder.CreateExtractElement(Vec, Idx: LoopIndex); |
| 45 | Function *Exp = Intrinsic::getOrInsertDeclaration(M: &M, id: CI->getIntrinsicID(), |
| 46 | OverloadTys: VecTy->getElementType()); |
| 47 | Value *Res = LoopBuilder.CreateCall(Callee: Exp, Args: Elem); |
| 48 | Value *NewVec = LoopBuilder.CreateInsertElement(Vec, NewElt: Res, Idx: LoopIndex); |
| 49 | Vec->addIncoming(V: NewVec, BB: LoopBB); |
| 50 | |
| 51 | Value *One = ConstantInt::get(Ty: IdxTy, V: 1U); |
| 52 | Value *NextLoopIndex = LoopBuilder.CreateAdd(LHS: LoopIndex, RHS: One); |
| 53 | LoopIndex->addIncoming(V: NextLoopIndex, BB: LoopBB); |
| 54 | |
| 55 | Value *ExitCond = |
| 56 | LoopBuilder.CreateICmp(P: CmpInst::ICMP_EQ, LHS: NextLoopIndex, RHS: LoopEnd); |
| 57 | LoopBuilder.CreateCondBr(Cond: ExitCond, True: PostLoopBB, False: LoopBB); |
| 58 | |
| 59 | CI->replaceAllUsesWith(V: NewVec); |
| 60 | CI->eraseFromParent(); |
| 61 | return true; |
| 62 | } |
| 63 | |