LLVM 19.0.0git
NVPTXLowerAggrCopies.cpp
Go to the documentation of this file.
1//===- NVPTXLowerAggrCopies.cpp - ------------------------------*- C++ -*--===//
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// \file
10// Lower aggregate copies, memset, memcpy, memmov intrinsics into loops when
11// the size is large or is not a compile-time constant.
12//
13//===----------------------------------------------------------------------===//
14
18#include "llvm/IR/Constants.h"
19#include "llvm/IR/DataLayout.h"
20#include "llvm/IR/Function.h"
21#include "llvm/IR/IRBuilder.h"
24#include "llvm/IR/Intrinsics.h"
25#include "llvm/IR/LLVMContext.h"
26#include "llvm/IR/Module.h"
27#include "llvm/Support/Debug.h"
30
31#define DEBUG_TYPE "nvptx"
32
33using namespace llvm;
34
35namespace {
36
37// actual analysis class, which is a functionpass
38struct NVPTXLowerAggrCopies : public FunctionPass {
39 static char ID;
40
41 NVPTXLowerAggrCopies() : FunctionPass(ID) {}
42
43 void getAnalysisUsage(AnalysisUsage &AU) const override {
46 }
47
48 bool runOnFunction(Function &F) override;
49
50 static const unsigned MaxAggrCopySize = 128;
51
52 StringRef getPassName() const override {
53 return "Lower aggregate copies/intrinsics into loops";
54 }
55};
56
57char NVPTXLowerAggrCopies::ID = 0;
58
59bool NVPTXLowerAggrCopies::runOnFunction(Function &F) {
62
63 const DataLayout &DL = F.getParent()->getDataLayout();
64 LLVMContext &Context = F.getParent()->getContext();
66 getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
67
68 // Collect all aggregate loads and mem* calls.
69 for (BasicBlock &BB : F) {
70 for (Instruction &I : BB) {
71 if (LoadInst *LI = dyn_cast<LoadInst>(&I)) {
72 if (!LI->hasOneUse())
73 continue;
74
75 if (DL.getTypeStoreSize(LI->getType()) < MaxAggrCopySize)
76 continue;
77
78 if (StoreInst *SI = dyn_cast<StoreInst>(LI->user_back())) {
79 if (SI->getOperand(0) != LI)
80 continue;
81 AggrLoads.push_back(LI);
82 }
83 } else if (MemIntrinsic *IntrCall = dyn_cast<MemIntrinsic>(&I)) {
84 // Convert intrinsic calls with variable size or with constant size
85 // larger than the MaxAggrCopySize threshold.
86 if (ConstantInt *LenCI = dyn_cast<ConstantInt>(IntrCall->getLength())) {
87 if (LenCI->getZExtValue() >= MaxAggrCopySize) {
88 MemCalls.push_back(IntrCall);
89 }
90 } else {
91 MemCalls.push_back(IntrCall);
92 }
93 }
94 }
95 }
96
97 if (AggrLoads.size() == 0 && MemCalls.size() == 0) {
98 return false;
99 }
100
101 //
102 // Do the transformation of an aggr load/copy/set to a loop
103 //
104 for (LoadInst *LI : AggrLoads) {
105 auto *SI = cast<StoreInst>(*LI->user_begin());
106 Value *SrcAddr = LI->getOperand(0);
107 Value *DstAddr = SI->getOperand(1);
108 unsigned NumLoads = DL.getTypeStoreSize(LI->getType());
109 ConstantInt *CopyLen =
110 ConstantInt::get(Type::getInt32Ty(Context), NumLoads);
111
112 createMemCpyLoopKnownSize(/* ConvertedInst */ SI,
113 /* SrcAddr */ SrcAddr, /* DstAddr */ DstAddr,
114 /* CopyLen */ CopyLen,
115 /* SrcAlign */ LI->getAlign(),
116 /* DestAlign */ SI->getAlign(),
117 /* SrcIsVolatile */ LI->isVolatile(),
118 /* DstIsVolatile */ SI->isVolatile(),
119 /* CanOverlap */ true, TTI);
120
121 SI->eraseFromParent();
122 LI->eraseFromParent();
123 }
124
125 // Transform mem* intrinsic calls.
126 for (MemIntrinsic *MemCall : MemCalls) {
127 if (MemCpyInst *Memcpy = dyn_cast<MemCpyInst>(MemCall)) {
128 expandMemCpyAsLoop(Memcpy, TTI);
129 } else if (MemMoveInst *Memmove = dyn_cast<MemMoveInst>(MemCall)) {
130 expandMemMoveAsLoop(Memmove, TTI);
131 } else if (MemSetInst *Memset = dyn_cast<MemSetInst>(MemCall)) {
132 expandMemSetAsLoop(Memset);
133 }
134 MemCall->eraseFromParent();
135 }
136
137 return true;
138}
139
140} // namespace
141
142namespace llvm {
144}
145
146INITIALIZE_PASS(NVPTXLowerAggrCopies, "nvptx-lower-aggr-copies",
147 "Lower aggregate copies, and llvm.mem* intrinsics into loops",
148 false, false)
149
151 return new NVPTXLowerAggrCopies();
152}
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
This file contains the declarations for the subclasses of Constant, which represent the different fla...
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
Module.h This file contains the declarations for the Module class.
LLVMContext & Context
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
This pass exposes codegen information to IR-level passes.
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
LLVM Basic Block Representation.
Definition: BasicBlock.h:60
This is the shared class of boolean and integer constants.
Definition: Constants.h:79
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:110
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:311
virtual bool runOnFunction(Function &F)=0
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
An instruction for reading from memory.
Definition: Instructions.h:184
This class wraps the llvm.memcpy intrinsic.
This is the common base class for memset/memcpy/memmove.
This class wraps the llvm.memmove intrinsic.
This class wraps the llvm.memset and llvm.memset.inline intrinsics.
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
Definition: PassRegistry.h:37
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: Pass.cpp:98
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:81
size_t size() const
Definition: SmallVector.h:91
void push_back(const T &Elt)
Definition: SmallVector.h:426
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
An instruction for storing to memory.
Definition: Instructions.h:317
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Wrapper pass for TargetTransformInfo.
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
static IntegerType * getInt32Ty(LLVMContext &C)
LLVM Value Representation.
Definition: Value.h:74
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void createMemCpyLoopKnownSize(Instruction *InsertBefore, Value *SrcAddr, Value *DstAddr, ConstantInt *CopyLen, Align SrcAlign, Align DestAlign, bool SrcIsVolatile, bool DstIsVolatile, bool CanOverlap, const TargetTransformInfo &TTI, std::optional< uint32_t > AtomicCpySize=std::nullopt)
Emit a loop implementing the semantics of an llvm.memcpy whose size is a compile time constant.
void initializeNVPTXLowerAggrCopiesPass(PassRegistry &)
bool expandMemMoveAsLoop(MemMoveInst *MemMove, const TargetTransformInfo &TTI)
Expand MemMove as a loop.
FunctionPass * createLowerAggrCopies()
void expandMemCpyAsLoop(MemCpyInst *MemCpy, const TargetTransformInfo &TTI, ScalarEvolution *SE=nullptr)
Expand MemCpy as a loop. MemCpy is not deleted.
void expandMemSetAsLoop(MemSetInst *MemSet)
Expand MemSet as a loop. MemSet is not deleted.