LLVM  10.0.0svn
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 
15 #include "NVPTXLowerAggrCopies.h"
18 #include "llvm/IR/Constants.h"
19 #include "llvm/IR/DataLayout.h"
20 #include "llvm/IR/Function.h"
21 #include "llvm/IR/IRBuilder.h"
22 #include "llvm/IR/Instructions.h"
23 #include "llvm/IR/IntrinsicInst.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 
33 using namespace llvm;
34 
35 namespace {
36 
37 // actual analysis class, which is a functionpass
38 struct 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 
58 
62 
63  const DataLayout &DL = F.getParent()->getDataLayout();
65  const TargetTransformInfo &TTI =
66  getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
67 
68  // Collect all aggregate loads and mem* calls.
69  for (Function::iterator BI = F.begin(), BE = F.end(); BI != BE; ++BI) {
70  for (BasicBlock::iterator II = BI->begin(), IE = BI->end(); II != IE;
71  ++II) {
72  if (LoadInst *LI = dyn_cast<LoadInst>(II)) {
73  if (!LI->hasOneUse())
74  continue;
75 
76  if (DL.getTypeStoreSize(LI->getType()) < MaxAggrCopySize)
77  continue;
78 
79  if (StoreInst *SI = dyn_cast<StoreInst>(LI->user_back())) {
80  if (SI->getOperand(0) != LI)
81  continue;
82  AggrLoads.push_back(LI);
83  }
84  } else if (MemIntrinsic *IntrCall = dyn_cast<MemIntrinsic>(II)) {
85  // Convert intrinsic calls with variable size or with constant size
86  // larger than the MaxAggrCopySize threshold.
87  if (ConstantInt *LenCI = dyn_cast<ConstantInt>(IntrCall->getLength())) {
88  if (LenCI->getZExtValue() >= MaxAggrCopySize) {
89  MemCalls.push_back(IntrCall);
90  }
91  } else {
92  MemCalls.push_back(IntrCall);
93  }
94  }
95  }
96  }
97 
98  if (AggrLoads.size() == 0 && MemCalls.size() == 0) {
99  return false;
100  }
101 
102  //
103  // Do the transformation of an aggr load/copy/set to a loop
104  //
105  for (LoadInst *LI : AggrLoads) {
106  auto *SI = cast<StoreInst>(*LI->user_begin());
107  Value *SrcAddr = LI->getOperand(0);
108  Value *DstAddr = SI->getOperand(1);
109  unsigned NumLoads = DL.getTypeStoreSize(LI->getType());
110  ConstantInt *CopyLen =
111  ConstantInt::get(Type::getInt32Ty(Context), NumLoads);
112 
113  createMemCpyLoopKnownSize(/* ConvertedInst */ SI,
114  /* SrcAddr */ SrcAddr, /* DstAddr */ DstAddr,
115  /* CopyLen */ CopyLen,
116  /* SrcAlign */ LI->getAlignment(),
117  /* DestAlign */ SI->getAlignment(),
118  /* SrcIsVolatile */ LI->isVolatile(),
119  /* DstIsVolatile */ SI->isVolatile(), 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);
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 
142 namespace llvm {
144 }
145 
146 INITIALIZE_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 }
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:111
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
LLVMContext & Context
This class represents lattice values for constants.
Definition: AllocatorList.h:23
void expandMemMoveAsLoop(MemMoveInst *MemMove)
Expand MemMove as a loop. MemMove is not deleted.
iterator end()
Definition: Function.h:687
This class wraps the llvm.memset intrinsic.
F(f)
An instruction for reading from memory.
Definition: Instructions.h:169
void expandMemSetAsLoop(MemSetInst *MemSet)
Expand MemSet as a loop. MemSet is not deleted.
AnalysisUsage & addRequired()
const DataLayout & getDataLayout() const
Get the data layout for the module&#39;s target platform.
Definition: Module.cpp:369
This class wraps the llvm.memmove intrinsic.
LLVMContext & getContext() const
Get the global data context.
Definition: Module.h:245
TypeSize getTypeStoreSize(Type *Ty) const
Returns the maximum number of bytes that may be overwritten by storing the specified type...
Definition: DataLayout.h:454
An instruction for storing to memory.
Definition: Instructions.h:325
iterator begin()
Definition: Function.h:685
static bool runOnFunction(Function &F, bool PostInlining)
Wrapper pass for TargetTransformInfo.
void initializeNVPTXLowerAggrCopiesPass(PassRegistry &)
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:64
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:284
size_t size() const
Definition: SmallVector.h:52
This is the common base class for memset/memcpy/memmove.
Iterator for intrusive lists based on ilist_node.
This is the shared class of boolean and integer constants.
Definition: Constants.h:83
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:837
Module.h This file contains the declarations for the Module class.
This class wraps the llvm.memcpy intrinsic.
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
Definition: Constants.cpp:653
FunctionPass * createLowerAggrCopies()
static IntegerType * getInt32Ty(LLVMContext &C)
Definition: Type.cpp:180
void createMemCpyLoopKnownSize(Instruction *InsertBefore, Value *SrcAddr, Value *DstAddr, ConstantInt *CopyLen, unsigned SrcAlign, unsigned DestAlign, bool SrcIsVolatile, bool DstIsVolatile, const TargetTransformInfo &TTI)
Emit a loop implementing the semantics of an llvm.memcpy whose size is a compile time constant...
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:575
LLVM Value Representation.
Definition: Value.h:74
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
Definition: PassRegistry.h:38
This pass exposes codegen information to IR-level passes.
void expandMemCpyAsLoop(MemCpyInst *MemCpy, const TargetTransformInfo &TTI)
Expand MemCpy as a loop. MemCpy is not deleted.
INITIALIZE_PASS(NVPTXLowerAggrCopies, "nvptx-lower-aggr-copies", "Lower aggregate copies, and llvm.mem* intrinsics into loops", false, false) FunctionPass *llvm