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