LowerMemIntrinsics.cpp 20 KB

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