LLVM  6.0.0svn
NVPTXLowerAggrCopies.cpp
Go to the documentation of this file.
1 //===- NVPTXLowerAggrCopies.cpp - ------------------------------*- C++ -*--===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // \file
11 // Lower aggregate copies, memset, memcpy, memmov intrinsics into loops when
12 // the size is large or is not a compile-time constant.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #include "NVPTXLowerAggrCopies.h"
19 #include "llvm/IR/Constants.h"
20 #include "llvm/IR/DataLayout.h"
21 #include "llvm/IR/Function.h"
22 #include "llvm/IR/IRBuilder.h"
23 #include "llvm/IR/Instructions.h"
24 #include "llvm/IR/IntrinsicInst.h"
25 #include "llvm/IR/Intrinsics.h"
26 #include "llvm/IR/LLVMContext.h"
27 #include "llvm/IR/Module.h"
28 #include "llvm/Support/Debug.h"
31 
32 #define DEBUG_TYPE "nvptx"
33 
34 using namespace llvm;
35 
36 namespace {
37 
38 // actual analysis class, which is a functionpass
39 struct NVPTXLowerAggrCopies : public FunctionPass {
40  static char ID;
41 
42  NVPTXLowerAggrCopies() : FunctionPass(ID) {}
43 
44  void getAnalysisUsage(AnalysisUsage &AU) const override {
47  }
48 
49  bool runOnFunction(Function &F) override;
50 
51  static const unsigned MaxAggrCopySize = 128;
52 
53  StringRef getPassName() const override {
54  return "Lower aggregate copies/intrinsics into loops";
55  }
56 };
57 
59 
60 bool NVPTXLowerAggrCopies::runOnFunction(Function &F) {
63 
64  const DataLayout &DL = F.getParent()->getDataLayout();
66  const TargetTransformInfo &TTI =
67  getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
68 
69  // Collect all aggregate loads and mem* calls.
70  for (Function::iterator BI = F.begin(), BE = F.end(); BI != BE; ++BI) {
71  for (BasicBlock::iterator II = BI->begin(), IE = BI->end(); II != IE;
72  ++II) {
73  if (LoadInst *LI = dyn_cast<LoadInst>(II)) {
74  if (!LI->hasOneUse())
75  continue;
76 
77  if (DL.getTypeStoreSize(LI->getType()) < MaxAggrCopySize)
78  continue;
79 
80  if (StoreInst *SI = dyn_cast<StoreInst>(LI->user_back())) {
81  if (SI->getOperand(0) != LI)
82  continue;
83  AggrLoads.push_back(LI);
84  }
85  } else if (MemIntrinsic *IntrCall = dyn_cast<MemIntrinsic>(II)) {
86  // Convert intrinsic calls with variable size or with constant size
87  // larger than the MaxAggrCopySize threshold.
88  if (ConstantInt *LenCI = dyn_cast<ConstantInt>(IntrCall->getLength())) {
89  if (LenCI->getZExtValue() >= MaxAggrCopySize) {
90  MemCalls.push_back(IntrCall);
91  }
92  } else {
93  MemCalls.push_back(IntrCall);
94  }
95  }
96  }
97  }
98 
99  if (AggrLoads.size() == 0 && MemCalls.size() == 0) {
100  return false;
101  }
102 
103  //
104  // Do the transformation of an aggr load/copy/set to a loop
105  //
106  for (LoadInst *LI : AggrLoads) {
107  StoreInst *SI = dyn_cast<StoreInst>(*LI->user_begin());
108  Value *SrcAddr = LI->getOperand(0);
109  Value *DstAddr = SI->getOperand(1);
110  unsigned NumLoads = DL.getTypeStoreSize(LI->getType());
111  ConstantInt *CopyLen =
112  ConstantInt::get(Type::getInt32Ty(Context), NumLoads);
113 
114  if (!TTI.useWideIRMemcpyLoopLowering()) {
115  createMemCpyLoop(/* ConvertedInst */ SI,
116  /* SrcAddr */ SrcAddr, /* DstAddr */ DstAddr,
117  /* CopyLen */ CopyLen,
118  /* SrcAlign */ LI->getAlignment(),
119  /* DestAlign */ SI->getAlignment(),
120  /* SrcIsVolatile */ LI->isVolatile(),
121  /* DstIsVolatile */ SI->isVolatile());
122  } else {
123  createMemCpyLoopKnownSize(/* ConvertedInst */ SI,
124  /* SrcAddr */ SrcAddr, /* DstAddr */ DstAddr,
125  /* CopyLen */ CopyLen,
126  /* SrcAlign */ LI->getAlignment(),
127  /* DestAlign */ SI->getAlignment(),
128  /* SrcIsVolatile */ LI->isVolatile(),
129  /* DstIsVolatile */ SI->isVolatile(), TTI);
130  }
131 
132  SI->eraseFromParent();
133  LI->eraseFromParent();
134  }
135 
136  // Transform mem* intrinsic calls.
137  for (MemIntrinsic *MemCall : MemCalls) {
138  if (MemCpyInst *Memcpy = dyn_cast<MemCpyInst>(MemCall)) {
139  expandMemCpyAsLoop(Memcpy, TTI);
140  } else if (MemMoveInst *Memmove = dyn_cast<MemMoveInst>(MemCall)) {
141  expandMemMoveAsLoop(Memmove);
142  } else if (MemSetInst *Memset = dyn_cast<MemSetInst>(MemCall)) {
143  expandMemSetAsLoop(Memset);
144  }
145  MemCall->eraseFromParent();
146  }
147 
148  return true;
149 }
150 
151 } // namespace
152 
153 namespace llvm {
155 }
156 
157 INITIALIZE_PASS(NVPTXLowerAggrCopies, "nvptx-lower-aggr-copies",
158  "Lower aggregate copies, and llvm.mem* intrinsics into loops",
159  false, false)
160 
162  return new NVPTXLowerAggrCopies();
163 }
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks &#39;this&#39; from the containing basic block and deletes it.
Definition: Instruction.cpp:69
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:109
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
LLVMContext & Context
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
void expandMemMoveAsLoop(MemMoveInst *MemMove)
Expand MemMove as a loop. MemMove is not deleted.
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
Definition: SmallVector.h:136
iterator end()
Definition: Function.h:590
This class wraps the llvm.memset intrinsic.
F(f)
An instruction for reading from memory.
Definition: Instructions.h:164
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:361
This class wraps the llvm.memmove intrinsic.
LLVMContext & getContext() const
Get the global data context.
Definition: Module.h:237
void createMemCpyLoop(Instruction *InsertBefore, Value *SrcAddr, Value *DstAddr, Value *CopyLen, unsigned SrcAlign, unsigned DestAlign, bool SrcIsVolatile, bool DstIsVolatile)
Emit a loop implementing the semantics of llvm.memcpy with the equivalent arguments at InsertBefore...
An instruction for storing to memory.
Definition: Instructions.h:306
iterator begin()
Definition: Function.h:588
Value * getOperand(unsigned i) const
Definition: User.h:154
Wrapper pass for TargetTransformInfo.
void initializeNVPTXLowerAggrCopiesPass(PassRegistry &)
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:69
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:285
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:84
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:864
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:560
bool isVolatile() const
Return true if this is a store to a volatile memory location.
Definition: Instructions.h:339
FunctionPass * createLowerAggrCopies()
static IntegerType * getInt32Ty(LLVMContext &C)
Definition: Type.cpp:176
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:323
unsigned getAlignment() const
Return the alignment of the access that is being performed.
Definition: Instructions.h:351
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:545
LLVM Value Representation.
Definition: Value.h:73
uint64_t getTypeStoreSize(Type *Ty) const
Returns the maximum number of bytes that may be overwritten by storing the specified type...
Definition: DataLayout.h:388
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
Definition: PassRegistry.h:39
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