LLVM 24.0.0git
LowerVectorIntrinsics.cpp
Go to the documentation of this file.
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
10#include "llvm/IR/IRBuilder.h"
11#include "llvm/IR/Module.h"
14
15#define DEBUG_TYPE "lower-vector-intrinsics"
16
17using namespace llvm;
18
20 Type *RetTy = CI->getType();
21 auto *StructRetTy = dyn_cast<StructType>(RetTy);
22 unsigned NumResults = StructRetTy ? StructRetTy->getNumElements() : 1;
23 auto *VecTy =
24 cast<VectorType>(StructRetTy ? StructRetTy->getElementType(0) : RetTy);
25
26 BasicBlock *PreLoopBB = CI->getParent();
27 BasicBlock *PostLoopBB = nullptr;
28 Function *ParentFunc = PreLoopBB->getParent();
29 LLVMContext &Ctx = PreLoopBB->getContext();
30 Type *IdxTy = M.getDataLayout().getIndexType(Ctx, 0);
31
32 PostLoopBB = PreLoopBB->splitBasicBlock(CI);
33 BasicBlock *LoopBB = BasicBlock::Create(Ctx, "", ParentFunc, PostLoopBB);
34 PreLoopBB->getTerminator()->setSuccessor(0, LoopBB);
35
36 // Loop preheader
37 IRBuilder<> PreLoopBuilder(PreLoopBB->getTerminator());
38 Value *LoopEnd =
39 PreLoopBuilder.CreateElementCount(IdxTy, VecTy->getElementCount());
40
41 // Loop body
42 IRBuilder<> LoopBuilder(LoopBB);
43
44 PHINode *LoopIndex = LoopBuilder.CreatePHI(IdxTy, 2);
45 LoopIndex->addIncoming(ConstantInt::get(IdxTy, 0U), PreLoopBB);
46
47 SmallVector<PHINode *, 2> ResultPhis(NumResults);
48 for (unsigned I = 0; I != NumResults; ++I) {
49 ResultPhis[I] = LoopBuilder.CreatePHI(VecTy, 2);
50 ResultPhis[I]->addIncoming(PoisonValue::get(VecTy), PreLoopBB);
51 }
52
53 unsigned NumArgs = CI->arg_size();
54 SmallVector<Value *, 4> Args(NumArgs);
55 SmallVector<Type *, 4> ArgTys(NumArgs);
56 for (unsigned I = 0; I != NumArgs; ++I) {
57 Value *Arg = CI->getArgOperand(I);
58 if (Arg->getType()->isVectorTy())
59 Arg = LoopBuilder.CreateExtractElement(Arg, LoopIndex);
60 Args[I] = Arg;
61 ArgTys[I] = Arg->getType();
62 }
63 Type *ScalarRetTy = toScalarizedTy(RetTy);
65 ScalarRetTy, ArgTys);
66
67 CallInst *ScalarCall = LoopBuilder.CreateCall(Fn, Args);
68 if (isa<FPMathOperator>(CI))
69 ScalarCall->copyFastMathFlags(CI);
70
71 SmallVector<Value *, 2> NewVecs(NumResults);
72 for (unsigned I = 0; I != NumResults; ++I) {
73 Value *ScalarRes = ScalarCall;
74 if (StructRetTy)
75 ScalarRes = LoopBuilder.CreateExtractValue(ScalarCall, I);
76 NewVecs[I] =
77 LoopBuilder.CreateInsertElement(ResultPhis[I], ScalarRes, LoopIndex);
78 ResultPhis[I]->addIncoming(NewVecs[I], LoopBB);
79 }
80
81 Value *One = ConstantInt::get(IdxTy, 1U);
82 Value *NextLoopIndex = LoopBuilder.CreateAdd(LoopIndex, One);
83 LoopIndex->addIncoming(NextLoopIndex, LoopBB);
84
85 Value *ExitCond =
86 LoopBuilder.CreateICmp(CmpInst::ICMP_EQ, NextLoopIndex, LoopEnd);
87 CondBrInst *Br = LoopBuilder.CreateCondBr(ExitCond, PostLoopBB, LoopBB);
89
90 Value *Res = NewVecs[0];
91 if (StructRetTy) {
92 IRBuilder<> PostLoopBuilder(CI);
93 Res = PoisonValue::get(RetTy);
94 for (unsigned I = 0; I != NumResults; ++I)
95 Res = PostLoopBuilder.CreateInsertValue(Res, NewVecs[I], I);
96 }
97
98 CI->replaceAllUsesWith(Res);
99 CI->eraseFromParent();
100 return true;
101}
#define DEBUG_TYPE
Module.h This file contains the declarations for the Module class.
#define I(x, y, z)
Definition MD5.cpp:57
This file contains the declarations for profiling metadata utility functions.
LLVM Basic Block Representation.
Definition BasicBlock.h:61
LLVM_ABI BasicBlock * splitBasicBlock(iterator I, const Twine &BBName="")
Split the basic block into two basic blocks at the specified instruction.
const Function * getParent() const
Return the enclosing method, or null if none.
Definition BasicBlock.h:212
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Definition BasicBlock.h:205
LLVM_ABI LLVMContext & getContext() const
Get the context in which this basic block lives.
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction; assumes that the block is well-formed.
Definition BasicBlock.h:236
Value * getArgOperand(unsigned i) const
LLVM_ABI Intrinsic::ID getIntrinsicID() const
Returns the intrinsic ID of the intrinsic called or Intrinsic::not_intrinsic if the called function i...
unsigned arg_size() const
This class represents a function call, abstracting a target machine's calling convention.
Conditional Branch instruction.
Value * CreateInsertElement(Type *VecTy, Value *NewElt, Value *Idx, const Twine &Name="")
Definition IRBuilder.h:2748
Value * CreateInsertValue(Value *Agg, Value *Val, ArrayRef< unsigned > Idxs, const Twine &Name="")
Definition IRBuilder.h:2802
Value * CreateExtractElement(Value *Vec, Value *Idx, const Twine &Name="")
Definition IRBuilder.h:2736
CondBrInst * CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False, MDNode *BranchWeights=nullptr, MDNode *Unpredictable=nullptr)
Create a conditional 'br Cond, TrueDest, FalseDest' instruction.
Definition IRBuilder.h:1218
Value * CreateExtractValue(Value *Agg, ArrayRef< unsigned > Idxs, const Twine &Name="")
Definition IRBuilder.h:2795
Value * CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS, const Twine &Name="", bool HasSameSign=false)
Definition IRBuilder.h:2568
PHINode * CreatePHI(Type *Ty, unsigned NumReservedValues, const Twine &Name="")
Definition IRBuilder.h:2626
Value * CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition IRBuilder.h:1424
CallInst * CreateCall(FunctionType *FTy, Value *Callee, ArrayRef< Value * > Args={}, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition IRBuilder.h:2640
LLVM_ABI Value * CreateElementCount(Type *Ty, ElementCount EC)
Create an expression which evaluates to the number of elements in EC at runtime.
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition IRBuilder.h:2990
LLVM_ABI void copyFastMathFlags(FastMathFlags FMF)
Convenience function for transferring all fast-math flag values to this instruction,...
LLVM_ABI InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
LLVM_ABI void setSuccessor(unsigned Idx, BasicBlock *BB)
Update the specified successor to point at the provided block.
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:76
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:68
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
bool isVectorTy() const
True if this is an instance of VectorType.
Definition Type.h:283
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:257
LLVM_ABI void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition Value.cpp:548
const ParentTy * getParent() const
Definition ilist_node.h:34
LLVM_ABI Function * getOrInsertDeclaration(Module *M, ID id, ArrayRef< Type * > OverloadTys={})
Look up the Function declaration of the intrinsic id in the Module M.
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI void setExplicitlyUnknownBranchWeightsIfProfiled(Instruction &I, StringRef PassName, const Function *F=nullptr)
Like setExplicitlyUnknownBranchWeights(...), but only sets unknown branch weights in the new instruct...
Type * toScalarizedTy(Type *Ty)
A helper for converting vectorized types to scalarized (non-vector) types.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
LLVM_ABI bool lowerVectorIntrinsicAsLoop(Module &M, CallInst *CI)
Lower CI as a loop.
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559