UnrollLoopTest.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. //===- UnrollLoopTest.cpp - Unit tests for UnrollLoop ---------------------===//
  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/UnrollLoop.h"
  9. #include "llvm/Analysis/AssumptionCache.h"
  10. #include "llvm/Analysis/LoopInfo.h"
  11. #include "llvm/Analysis/ScalarEvolution.h"
  12. #include "llvm/Analysis/TargetLibraryInfo.h"
  13. #include "llvm/AsmParser/Parser.h"
  14. #include "llvm/IR/BasicBlock.h"
  15. #include "llvm/IR/Dominators.h"
  16. #include "llvm/IR/LLVMContext.h"
  17. #include "llvm/Support/SourceMgr.h"
  18. #include "gtest/gtest.h"
  19. using namespace llvm;
  20. static std::unique_ptr<Module> parseIR(LLVMContext &C, const char *IR) {
  21. SMDiagnostic Err;
  22. std::unique_ptr<Module> Mod = parseAssemblyString(IR, Err, C);
  23. if (!Mod)
  24. Err.print("UnrollLoopTests", errs());
  25. return Mod;
  26. }
  27. TEST(LoopUnrollRuntime, Latch) {
  28. LLVMContext C;
  29. std::unique_ptr<Module> M = parseIR(
  30. C,
  31. R"(define i32 @test(i32* %a, i32* %b, i32* %c, i64 %n) {
  32. entry:
  33. br label %while.cond
  34. while.cond: ; preds = %while.body, %entry
  35. %i.0 = phi i64 [ 0, %entry ], [ %inc, %while.body ]
  36. %cmp = icmp slt i64 %i.0, %n
  37. br i1 %cmp, label %while.body, label %while.end
  38. while.body: ; preds = %while.cond
  39. %arrayidx = getelementptr inbounds i32, i32* %b, i64 %i.0
  40. %0 = load i32, i32* %arrayidx
  41. %arrayidx1 = getelementptr inbounds i32, i32* %c, i64 %i.0
  42. %1 = load i32, i32* %arrayidx1
  43. %mul = mul nsw i32 %0, %1
  44. %arrayidx2 = getelementptr inbounds i32, i32* %a, i64 %i.0
  45. store i32 %mul, i32* %arrayidx2
  46. %inc = add nsw i64 %i.0, 1
  47. br label %while.cond
  48. while.end: ; preds = %while.cond
  49. ret i32 0
  50. })"
  51. );
  52. auto *F = M->getFunction("test");
  53. DominatorTree DT(*F);
  54. LoopInfo LI(DT);
  55. AssumptionCache AC(*F);
  56. TargetLibraryInfoImpl TLII;
  57. TargetLibraryInfo TLI(TLII);
  58. ScalarEvolution SE(*F, TLI, AC, DT, LI);
  59. Loop *L = *LI.begin();
  60. bool PreserveLCSSA = L->isRecursivelyLCSSAForm(DT,LI);
  61. bool ret = UnrollRuntimeLoopRemainder(L, 4, true, false, false, false, &LI,
  62. &SE, &DT, &AC, PreserveLCSSA);
  63. EXPECT_FALSE(ret);
  64. }