Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Transforms/Utils/LowerMemIntrinsics.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- LowerMemIntrinsics.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
#include "llvm/Transforms/Utils/LowerMemIntrinsics.h"
10
#include "llvm/Analysis/TargetTransformInfo.h"
11
#include "llvm/IR/IRBuilder.h"
12
#include "llvm/IR/IntrinsicInst.h"
13
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
14
15
using namespace llvm;
16
17
15
static unsigned getLoopOperandSizeInBytes(Type *Type) {
18
15
  if (VectorType *VTy = dyn_cast<VectorType>(Type)) {
19
0
    return VTy->getBitWidth() / 8;
20
0
  }
21
15
22
15
  return Type->getPrimitiveSizeInBits() / 8;
23
15
}
24
25
void llvm::createMemCpyLoopKnownSize(Instruction *InsertBefore, Value *SrcAddr,
26
                                     Value *DstAddr, ConstantInt *CopyLen,
27
                                     unsigned SrcAlign, unsigned DestAlign,
28
                                     bool SrcIsVolatile, bool DstIsVolatile,
29
3
                                     const TargetTransformInfo &TTI) {
30
3
  // No need to expand zero length copies.
31
3
  if (CopyLen->isZero())
32
0
    return;
33
3
34
3
  BasicBlock *PreLoopBB = InsertBefore->getParent();
35
3
  BasicBlock *PostLoopBB = nullptr;
36
3
  Function *ParentFunc = PreLoopBB->getParent();
37
3
  LLVMContext &Ctx = PreLoopBB->getContext();
38
3
39
3
  Type *TypeOfCopyLen = CopyLen->getType();
40
3
  Type *LoopOpType =
41
3
      TTI.getMemcpyLoopLoweringType(Ctx, CopyLen, SrcAlign, DestAlign);
42
3
43
3
  unsigned LoopOpSize = getLoopOperandSizeInBytes(LoopOpType);
44
3
  uint64_t LoopEndCount = CopyLen->getZExtValue() / LoopOpSize;
45
3
46
3
  unsigned SrcAS = cast<PointerType>(SrcAddr->getType())->getAddressSpace();
47
3
  unsigned DstAS = cast<PointerType>(DstAddr->getType())->getAddressSpace();
48
3
49
3
  if (LoopEndCount != 0) {
50
3
    // Split
51
3
    PostLoopBB = PreLoopBB->splitBasicBlock(InsertBefore, "memcpy-split");
52
3
    BasicBlock *LoopBB =
53
3
        BasicBlock::Create(Ctx, "load-store-loop", ParentFunc, PostLoopBB);
54
3
    PreLoopBB->getTerminator()->setSuccessor(0, LoopBB);
55
3
56
3
    IRBuilder<> PLBuilder(PreLoopBB->getTerminator());
57
3
58
3
    // Cast the Src and Dst pointers to pointers to the loop operand type (if
59
3
    // needed).
60
3
    PointerType *SrcOpType = PointerType::get(LoopOpType, SrcAS);
61
3
    PointerType *DstOpType = PointerType::get(LoopOpType, DstAS);
62
3
    if (SrcAddr->getType() != SrcOpType) {
63
0
      SrcAddr = PLBuilder.CreateBitCast(SrcAddr, SrcOpType);
64
0
    }
65
3
    if (DstAddr->getType() != DstOpType) {
66
0
      DstAddr = PLBuilder.CreateBitCast(DstAddr, DstOpType);
67
0
    }
68
3
69
3
    IRBuilder<> LoopBuilder(LoopBB);
70
3
    PHINode *LoopIndex = LoopBuilder.CreatePHI(TypeOfCopyLen, 2, "loop-index");
71
3
    LoopIndex->addIncoming(ConstantInt::get(TypeOfCopyLen, 0U), PreLoopBB);
72
3
    // Loop Body
73
3
    Value *SrcGEP =
74
3
        LoopBuilder.CreateInBoundsGEP(LoopOpType, SrcAddr, LoopIndex);
75
3
    Value *Load = LoopBuilder.CreateLoad(LoopOpType, SrcGEP, SrcIsVolatile);
76
3
    Value *DstGEP =
77
3
        LoopBuilder.CreateInBoundsGEP(LoopOpType, DstAddr, LoopIndex);
78
3
    LoopBuilder.CreateStore(Load, DstGEP, DstIsVolatile);
79
3
80
3
    Value *NewIndex =
81
3
        LoopBuilder.CreateAdd(LoopIndex, ConstantInt::get(TypeOfCopyLen, 1U));
82
3
    LoopIndex->addIncoming(NewIndex, LoopBB);
83
3
84
3
    // Create the loop branch condition.
85
3
    Constant *LoopEndCI = ConstantInt::get(TypeOfCopyLen, LoopEndCount);
86
3
    LoopBuilder.CreateCondBr(LoopBuilder.CreateICmpULT(NewIndex, LoopEndCI),
87
3
                             LoopBB, PostLoopBB);
88
3
  }
89
3
90
3
  uint64_t BytesCopied = LoopEndCount * LoopOpSize;
91
3
  uint64_t RemainingBytes = CopyLen->getZExtValue() - BytesCopied;
92
3
  if (RemainingBytes) {
93
0
    IRBuilder<> RBuilder(PostLoopBB ? PostLoopBB->getFirstNonPHI()
94
0
                                    : InsertBefore);
95
0
96
0
    // Update the alignment based on the copy size used in the loop body.
97
0
    SrcAlign = std::min(SrcAlign, LoopOpSize);
98
0
    DestAlign = std::min(DestAlign, LoopOpSize);
99
0
100
0
    SmallVector<Type *, 5> RemainingOps;
101
0
    TTI.getMemcpyLoopResidualLoweringType(RemainingOps, Ctx, RemainingBytes,
102
0
                                          SrcAlign, DestAlign);
103
0
104
0
    for (auto OpTy : RemainingOps) {
105
0
      // Calaculate the new index
106
0
      unsigned OperandSize = getLoopOperandSizeInBytes(OpTy);
107
0
      uint64_t GepIndex = BytesCopied / OperandSize;
108
0
      assert(GepIndex * OperandSize == BytesCopied &&
109
0
             "Division should have no Remainder!");
110
0
      // Cast source to operand type and load
111
0
      PointerType *SrcPtrType = PointerType::get(OpTy, SrcAS);
112
0
      Value *CastedSrc = SrcAddr->getType() == SrcPtrType
113
0
                             ? SrcAddr
114
0
                             : RBuilder.CreateBitCast(SrcAddr, SrcPtrType);
115
0
      Value *SrcGEP = RBuilder.CreateInBoundsGEP(
116
0
          OpTy, CastedSrc, ConstantInt::get(TypeOfCopyLen, GepIndex));
117
0
      Value *Load = RBuilder.CreateLoad(OpTy, SrcGEP, SrcIsVolatile);
118
0
119
0
      // Cast destination to operand type and store.
120
0
      PointerType *DstPtrType = PointerType::get(OpTy, DstAS);
121
0
      Value *CastedDst = DstAddr->getType() == DstPtrType
122
0
                             ? DstAddr
123
0
                             : RBuilder.CreateBitCast(DstAddr, DstPtrType);
124
0
      Value *DstGEP = RBuilder.CreateInBoundsGEP(
125
0
          OpTy, CastedDst, ConstantInt::get(TypeOfCopyLen, GepIndex));
126
0
      RBuilder.CreateStore(Load, DstGEP, DstIsVolatile);
127
0
128
0
      BytesCopied += OperandSize;
129
0
    }
130
0
  }
131
3
  assert(BytesCopied == CopyLen->getZExtValue() &&
132
3
         "Bytes copied should match size in the call!");
133
3
}
134
135
void llvm::createMemCpyLoopUnknownSize(Instruction *InsertBefore,
136
                                       Value *SrcAddr, Value *DstAddr,
137
                                       Value *CopyLen, unsigned SrcAlign,
138
                                       unsigned DestAlign, bool SrcIsVolatile,
139
                                       bool DstIsVolatile,
140
12
                                       const TargetTransformInfo &TTI) {
141
12
  BasicBlock *PreLoopBB = InsertBefore->getParent();
142
12
  BasicBlock *PostLoopBB =
143
12
      PreLoopBB->splitBasicBlock(InsertBefore, "post-loop-memcpy-expansion");
144
12
145
12
  Function *ParentFunc = PreLoopBB->getParent();
146
12
  LLVMContext &Ctx = PreLoopBB->getContext();
147
12
148
12
  Type *LoopOpType =
149
12
      TTI.getMemcpyLoopLoweringType(Ctx, CopyLen, SrcAlign, DestAlign);
150
12
  unsigned LoopOpSize = getLoopOperandSizeInBytes(LoopOpType);
151
12
152
12
  IRBuilder<> PLBuilder(PreLoopBB->getTerminator());
153
12
154
12
  unsigned SrcAS = cast<PointerType>(SrcAddr->getType())->getAddressSpace();
155
12
  unsigned DstAS = cast<PointerType>(DstAddr->getType())->getAddressSpace();
156
12
  PointerType *SrcOpType = PointerType::get(LoopOpType, SrcAS);
157
12
  PointerType *DstOpType = PointerType::get(LoopOpType, DstAS);
158
12
  if (SrcAddr->getType() != SrcOpType) {
159
0
    SrcAddr = PLBuilder.CreateBitCast(SrcAddr, SrcOpType);
160
0
  }
161
12
  if (DstAddr->getType() != DstOpType) {
162
0
    DstAddr = PLBuilder.CreateBitCast(DstAddr, DstOpType);
163
0
  }
164
12
165
12
  // Calculate the loop trip count, and remaining bytes to copy after the loop.
166
12
  Type *CopyLenType = CopyLen->getType();
167
12
  IntegerType *ILengthType = dyn_cast<IntegerType>(CopyLenType);
168
12
  assert(ILengthType &&
169
12
         "expected size argument to memcpy to be an integer type!");
170
12
  Type *Int8Type = Type::getInt8Ty(Ctx);
171
12
  bool LoopOpIsInt8 = LoopOpType == Int8Type;
172
12
  ConstantInt *CILoopOpSize = ConstantInt::get(ILengthType, LoopOpSize);
173
12
  Value *RuntimeLoopCount = LoopOpIsInt8 ?
174
12
                            CopyLen :
175
12
                            
PLBuilder.CreateUDiv(CopyLen, CILoopOpSize)0
;
176
12
  BasicBlock *LoopBB =
177
12
      BasicBlock::Create(Ctx, "loop-memcpy-expansion", ParentFunc, PostLoopBB);
178
12
  IRBuilder<> LoopBuilder(LoopBB);
179
12
180
12
  PHINode *LoopIndex = LoopBuilder.CreatePHI(CopyLenType, 2, "loop-index");
181
12
  LoopIndex->addIncoming(ConstantInt::get(CopyLenType, 0U), PreLoopBB);
182
12
183
12
  Value *SrcGEP = LoopBuilder.CreateInBoundsGEP(LoopOpType, SrcAddr, LoopIndex);
184
12
  Value *Load = LoopBuilder.CreateLoad(LoopOpType, SrcGEP, SrcIsVolatile);
185
12
  Value *DstGEP = LoopBuilder.CreateInBoundsGEP(LoopOpType, DstAddr, LoopIndex);
186
12
  LoopBuilder.CreateStore(Load, DstGEP, DstIsVolatile);
187
12
188
12
  Value *NewIndex =
189
12
      LoopBuilder.CreateAdd(LoopIndex, ConstantInt::get(CopyLenType, 1U));
190
12
  LoopIndex->addIncoming(NewIndex, LoopBB);
191
12
192
12
  if (!LoopOpIsInt8) {
193
0
   // Add in the
194
0
   Value *RuntimeResidual = PLBuilder.CreateURem(CopyLen, CILoopOpSize);
195
0
   Value *RuntimeBytesCopied = PLBuilder.CreateSub(CopyLen, RuntimeResidual);
196
0
197
0
    // Loop body for the residual copy.
198
0
    BasicBlock *ResLoopBB = BasicBlock::Create(Ctx, "loop-memcpy-residual",
199
0
                                               PreLoopBB->getParent(),
200
0
                                               PostLoopBB);
201
0
    // Residual loop header.
202
0
    BasicBlock *ResHeaderBB = BasicBlock::Create(
203
0
        Ctx, "loop-memcpy-residual-header", PreLoopBB->getParent(), nullptr);
204
0
205
0
    // Need to update the pre-loop basic block to branch to the correct place.
206
0
    // branch to the main loop if the count is non-zero, branch to the residual
207
0
    // loop if the copy size is smaller then 1 iteration of the main loop but
208
0
    // non-zero and finally branch to after the residual loop if the memcpy
209
0
    //  size is zero.
210
0
    ConstantInt *Zero = ConstantInt::get(ILengthType, 0U);
211
0
    PLBuilder.CreateCondBr(PLBuilder.CreateICmpNE(RuntimeLoopCount, Zero),
212
0
                           LoopBB, ResHeaderBB);
213
0
    PreLoopBB->getTerminator()->eraseFromParent();
214
0
215
0
    LoopBuilder.CreateCondBr(
216
0
        LoopBuilder.CreateICmpULT(NewIndex, RuntimeLoopCount), LoopBB,
217
0
        ResHeaderBB);
218
0
219
0
    // Determine if we need to branch to the residual loop or bypass it.
220
0
    IRBuilder<> RHBuilder(ResHeaderBB);
221
0
    RHBuilder.CreateCondBr(RHBuilder.CreateICmpNE(RuntimeResidual, Zero),
222
0
                           ResLoopBB, PostLoopBB);
223
0
224
0
    // Copy the residual with single byte load/store loop.
225
0
    IRBuilder<> ResBuilder(ResLoopBB);
226
0
    PHINode *ResidualIndex =
227
0
        ResBuilder.CreatePHI(CopyLenType, 2, "residual-loop-index");
228
0
    ResidualIndex->addIncoming(Zero, ResHeaderBB);
229
0
230
0
    Value *SrcAsInt8 =
231
0
        ResBuilder.CreateBitCast(SrcAddr, PointerType::get(Int8Type, SrcAS));
232
0
    Value *DstAsInt8 =
233
0
        ResBuilder.CreateBitCast(DstAddr, PointerType::get(Int8Type, DstAS));
234
0
    Value *FullOffset = ResBuilder.CreateAdd(RuntimeBytesCopied, ResidualIndex);
235
0
    Value *SrcGEP =
236
0
        ResBuilder.CreateInBoundsGEP(Int8Type, SrcAsInt8, FullOffset);
237
0
    Value *Load = ResBuilder.CreateLoad(Int8Type, SrcGEP, SrcIsVolatile);
238
0
    Value *DstGEP =
239
0
        ResBuilder.CreateInBoundsGEP(Int8Type, DstAsInt8, FullOffset);
240
0
    ResBuilder.CreateStore(Load, DstGEP, DstIsVolatile);
241
0
242
0
    Value *ResNewIndex =
243
0
        ResBuilder.CreateAdd(ResidualIndex, ConstantInt::get(CopyLenType, 1U));
244
0
    ResidualIndex->addIncoming(ResNewIndex, ResLoopBB);
245
0
246
0
    // Create the loop branch condition.
247
0
    ResBuilder.CreateCondBr(
248
0
        ResBuilder.CreateICmpULT(ResNewIndex, RuntimeResidual), ResLoopBB,
249
0
        PostLoopBB);
250
12
  } else {
251
12
    // In this case the loop operand type was a byte, and there is no need for a
252
12
    // residual loop to copy the remaining memory after the main loop.
253
12
    // We do however need to patch up the control flow by creating the
254
12
    // terminators for the preloop block and the memcpy loop.
255
12
    ConstantInt *Zero = ConstantInt::get(ILengthType, 0U);
256
12
    PLBuilder.CreateCondBr(PLBuilder.CreateICmpNE(RuntimeLoopCount, Zero),
257
12
                           LoopBB, PostLoopBB);
258
12
    PreLoopBB->getTerminator()->eraseFromParent();
259
12
    LoopBuilder.CreateCondBr(
260
12
        LoopBuilder.CreateICmpULT(NewIndex, RuntimeLoopCount), LoopBB,
261
12
        PostLoopBB);
262
12
  }
263
12
}
264
265
// Lower memmove to IR. memmove is required to correctly copy overlapping memory
266
// regions; therefore, it has to check the relative positions of the source and
267
// destination pointers and choose the copy direction accordingly.
268
//
269
// The code below is an IR rendition of this C function:
270
//
271
// void* memmove(void* dst, const void* src, size_t n) {
272
//   unsigned char* d = dst;
273
//   const unsigned char* s = src;
274
//   if (s < d) {
275
//     // copy backwards
276
//     while (n--) {
277
//       d[n] = s[n];
278
//     }
279
//   } else {
280
//     // copy forward
281
//     for (size_t i = 0; i < n; ++i) {
282
//       d[i] = s[i];
283
//     }
284
//   }
285
//   return dst;
286
// }
287
static void createMemMoveLoop(Instruction *InsertBefore,
288
                              Value *SrcAddr, Value *DstAddr, Value *CopyLen,
289
                              unsigned SrcAlign, unsigned DestAlign,
290
3
                              bool SrcIsVolatile, bool DstIsVolatile) {
291
3
  Type *TypeOfCopyLen = CopyLen->getType();
292
3
  BasicBlock *OrigBB = InsertBefore->getParent();
293
3
  Function *F = OrigBB->getParent();
294
3
295
3
  Type *EltTy = cast<PointerType>(SrcAddr->getType())->getElementType();
296
3
297
3
  // Create the a comparison of src and dst, based on which we jump to either
298
3
  // the forward-copy part of the function (if src >= dst) or the backwards-copy
299
3
  // part (if src < dst).
300
3
  // SplitBlockAndInsertIfThenElse conveniently creates the basic if-then-else
301
3
  // structure. Its block terminators (unconditional branches) are replaced by
302
3
  // the appropriate conditional branches when the loop is built.
303
3
  ICmpInst *PtrCompare = new ICmpInst(InsertBefore, ICmpInst::ICMP_ULT,
304
3
                                      SrcAddr, DstAddr, "compare_src_dst");
305
3
  Instruction *ThenTerm, *ElseTerm;
306
3
  SplitBlockAndInsertIfThenElse(PtrCompare, InsertBefore, &ThenTerm,
307
3
                                &ElseTerm);
308
3
309
3
  // Each part of the function consists of two blocks:
310
3
  //   copy_backwards:        used to skip the loop when n == 0
311
3
  //   copy_backwards_loop:   the actual backwards loop BB
312
3
  //   copy_forward:          used to skip the loop when n == 0
313
3
  //   copy_forward_loop:     the actual forward loop BB
314
3
  BasicBlock *CopyBackwardsBB = ThenTerm->getParent();
315
3
  CopyBackwardsBB->setName("copy_backwards");
316
3
  BasicBlock *CopyForwardBB = ElseTerm->getParent();
317
3
  CopyForwardBB->setName("copy_forward");
318
3
  BasicBlock *ExitBB = InsertBefore->getParent();
319
3
  ExitBB->setName("memmove_done");
320
3
321
3
  // Initial comparison of n == 0 that lets us skip the loops altogether. Shared
322
3
  // between both backwards and forward copy clauses.
323
3
  ICmpInst *CompareN =
324
3
      new ICmpInst(OrigBB->getTerminator(), ICmpInst::ICMP_EQ, CopyLen,
325
3
                   ConstantInt::get(TypeOfCopyLen, 0), "compare_n_to_0");
326
3
327
3
  // Copying backwards.
328
3
  BasicBlock *LoopBB =
329
3
    BasicBlock::Create(F->getContext(), "copy_backwards_loop", F, CopyForwardBB);
330
3
  IRBuilder<> LoopBuilder(LoopBB);
331
3
  PHINode *LoopPhi = LoopBuilder.CreatePHI(TypeOfCopyLen, 0);
332
3
  Value *IndexPtr = LoopBuilder.CreateSub(
333
3
      LoopPhi, ConstantInt::get(TypeOfCopyLen, 1), "index_ptr");
334
3
  Value *Element = LoopBuilder.CreateLoad(
335
3
      EltTy, LoopBuilder.CreateInBoundsGEP(EltTy, SrcAddr, IndexPtr),
336
3
      "element");
337
3
  LoopBuilder.CreateStore(
338
3
      Element, LoopBuilder.CreateInBoundsGEP(EltTy, DstAddr, IndexPtr));
339
3
  LoopBuilder.CreateCondBr(
340
3
      LoopBuilder.CreateICmpEQ(IndexPtr, ConstantInt::get(TypeOfCopyLen, 0)),
341
3
      ExitBB, LoopBB);
342
3
  LoopPhi->addIncoming(IndexPtr, LoopBB);
343
3
  LoopPhi->addIncoming(CopyLen, CopyBackwardsBB);
344
3
  BranchInst::Create(ExitBB, LoopBB, CompareN, ThenTerm);
345
3
  ThenTerm->eraseFromParent();
346
3
347
3
  // Copying forward.
348
3
  BasicBlock *FwdLoopBB =
349
3
    BasicBlock::Create(F->getContext(), "copy_forward_loop", F, ExitBB);
350
3
  IRBuilder<> FwdLoopBuilder(FwdLoopBB);
351
3
  PHINode *FwdCopyPhi = FwdLoopBuilder.CreatePHI(TypeOfCopyLen, 0, "index_ptr");
352
3
  Value *FwdElement = FwdLoopBuilder.CreateLoad(
353
3
      EltTy, FwdLoopBuilder.CreateInBoundsGEP(EltTy, SrcAddr, FwdCopyPhi),
354
3
      "element");
355
3
  FwdLoopBuilder.CreateStore(
356
3
      FwdElement, FwdLoopBuilder.CreateInBoundsGEP(EltTy, DstAddr, FwdCopyPhi));
357
3
  Value *FwdIndexPtr = FwdLoopBuilder.CreateAdd(
358
3
      FwdCopyPhi, ConstantInt::get(TypeOfCopyLen, 1), "index_increment");
359
3
  FwdLoopBuilder.CreateCondBr(FwdLoopBuilder.CreateICmpEQ(FwdIndexPtr, CopyLen),
360
3
                              ExitBB, FwdLoopBB);
361
3
  FwdCopyPhi->addIncoming(FwdIndexPtr, FwdLoopBB);
362
3
  FwdCopyPhi->addIncoming(ConstantInt::get(TypeOfCopyLen, 0), CopyForwardBB);
363
3
364
3
  BranchInst::Create(ExitBB, FwdLoopBB, CompareN, ElseTerm);
365
3
  ElseTerm->eraseFromParent();
366
3
}
367
368
static void createMemSetLoop(Instruction *InsertBefore,
369
                             Value *DstAddr, Value *CopyLen, Value *SetValue,
370
7
                             unsigned Align, bool IsVolatile) {
371
7
  Type *TypeOfCopyLen = CopyLen->getType();
372
7
  BasicBlock *OrigBB = InsertBefore->getParent();
373
7
  Function *F = OrigBB->getParent();
374
7
  BasicBlock *NewBB =
375
7
      OrigBB->splitBasicBlock(InsertBefore, "split");
376
7
  BasicBlock *LoopBB
377
7
    = BasicBlock::Create(F->getContext(), "loadstoreloop", F, NewBB);
378
7
379
7
  IRBuilder<> Builder(OrigBB->getTerminator());
380
7
381
7
  // Cast pointer to the type of value getting stored
382
7
  unsigned dstAS = cast<PointerType>(DstAddr->getType())->getAddressSpace();
383
7
  DstAddr = Builder.CreateBitCast(DstAddr,
384
7
                                  PointerType::get(SetValue->getType(), dstAS));
385
7
386
7
  Builder.CreateCondBr(
387
7
      Builder.CreateICmpEQ(ConstantInt::get(TypeOfCopyLen, 0), CopyLen), NewBB,
388
7
      LoopBB);
389
7
  OrigBB->getTerminator()->eraseFromParent();
390
7
391
7
  IRBuilder<> LoopBuilder(LoopBB);
392
7
  PHINode *LoopIndex = LoopBuilder.CreatePHI(TypeOfCopyLen, 0);
393
7
  LoopIndex->addIncoming(ConstantInt::get(TypeOfCopyLen, 0), OrigBB);
394
7
395
7
  LoopBuilder.CreateStore(
396
7
      SetValue,
397
7
      LoopBuilder.CreateInBoundsGEP(SetValue->getType(), DstAddr, LoopIndex),
398
7
      IsVolatile);
399
7
400
7
  Value *NewIndex =
401
7
      LoopBuilder.CreateAdd(LoopIndex, ConstantInt::get(TypeOfCopyLen, 1));
402
7
  LoopIndex->addIncoming(NewIndex, LoopBB);
403
7
404
7
  LoopBuilder.CreateCondBr(LoopBuilder.CreateICmpULT(NewIndex, CopyLen), LoopBB,
405
7
                           NewBB);
406
7
}
407
408
void llvm::expandMemCpyAsLoop(MemCpyInst *Memcpy,
409
15
                              const TargetTransformInfo &TTI) {
410
15
  if (ConstantInt *CI = dyn_cast<ConstantInt>(Memcpy->getLength())) {
411
3
    createMemCpyLoopKnownSize(/* InsertBefore */ Memcpy,
412
3
                              /* SrcAddr */ Memcpy->getRawSource(),
413
3
                              /* DstAddr */ Memcpy->getRawDest(),
414
3
                              /* CopyLen */ CI,
415
3
                              /* SrcAlign */ Memcpy->getSourceAlignment(),
416
3
                              /* DestAlign */ Memcpy->getDestAlignment(),
417
3
                              /* SrcIsVolatile */ Memcpy->isVolatile(),
418
3
                              /* DstIsVolatile */ Memcpy->isVolatile(),
419
3
                              /* TargetTransformInfo */ TTI);
420
12
  } else {
421
12
    createMemCpyLoopUnknownSize(/* InsertBefore */ Memcpy,
422
12
                                /* SrcAddr */ Memcpy->getRawSource(),
423
12
                                /* DstAddr */ Memcpy->getRawDest(),
424
12
                                /* CopyLen */ Memcpy->getLength(),
425
12
                                /* SrcAlign */ Memcpy->getSourceAlignment(),
426
12
                                /* DestAlign */ Memcpy->getDestAlignment(),
427
12
                                /* SrcIsVolatile */ Memcpy->isVolatile(),
428
12
                                /* DstIsVolatile */ Memcpy->isVolatile(),
429
12
                                /* TargetTransfomrInfo */ TTI);
430
12
  }
431
15
}
432
433
3
void llvm::expandMemMoveAsLoop(MemMoveInst *Memmove) {
434
3
  createMemMoveLoop(/* InsertBefore */ Memmove,
435
3
                    /* SrcAddr */ Memmove->getRawSource(),
436
3
                    /* DstAddr */ Memmove->getRawDest(),
437
3
                    /* CopyLen */ Memmove->getLength(),
438
3
                    /* SrcAlign */ Memmove->getSourceAlignment(),
439
3
                    /* DestAlign */ Memmove->getDestAlignment(),
440
3
                    /* SrcIsVolatile */ Memmove->isVolatile(),
441
3
                    /* DstIsVolatile */ Memmove->isVolatile());
442
3
}
443
444
7
void llvm::expandMemSetAsLoop(MemSetInst *Memset) {
445
7
  createMemSetLoop(/* InsertBefore */ Memset,
446
7
                   /* DstAddr */ Memset->getRawDest(),
447
7
                   /* CopyLen */ Memset->getLength(),
448
7
                   /* SetValue */ Memset->getValue(),
449
7
                   /* Alignment */ Memset->getDestAlignment(),
450
7
                   Memset->isVolatile());
451
7
}