Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion llvm/include/llvm/Transforms/Scalar/LoopRotation.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ class Loop;
class LoopRotatePass : public PassInfoMixin<LoopRotatePass> {
public:
LoopRotatePass(bool EnableHeaderDuplication = true,
bool PrepareForLTO = false);
bool PrepareForLTO = false,
bool RotateComputable = true);
PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM,
LoopStandardAnalysisResults &AR, LPMUpdater &U);

Expand All @@ -34,6 +35,7 @@ class LoopRotatePass : public PassInfoMixin<LoopRotatePass> {
private:
const bool EnableHeaderDuplication;
const bool PrepareForLTO;
const bool RotateComputable;
};
}

Expand Down
3 changes: 2 additions & 1 deletion llvm/include/llvm/Transforms/Utils/LoopRotationUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ LLVM_ABI bool LoopRotation(Loop *L, LoopInfo *LI,
DominatorTree *DT, ScalarEvolution *SE,
MemorySSAUpdater *MSSAU, const SimplifyQuery &SQ,
bool RotationOnly, unsigned Threshold,
bool IsUtilMode, bool PrepareForLTO = false);
bool IsUtilMode, bool PrepareForLTO = false,
bool RotateComputable = true);

} // namespace llvm

Expand Down
5 changes: 4 additions & 1 deletion llvm/lib/Passes/PassBuilderPipelines.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -489,8 +489,11 @@ PassBuilder::buildO1FunctionSimplificationPipeline(OptimizationLevel Level,
LPM1.addPass(LICMPass(PTO.LicmMssaOptCap, PTO.LicmMssaNoAccForPromotionCap,
/*AllowSpeculation=*/false));

// Don't enable rotation of computable loops as that would require computing
// SCEV just for loop rotate and impact compile time..
LPM1.addPass(LoopRotatePass(/* Disable header duplication */ true,
isLTOPreLink(Phase)));
isLTOPreLink(Phase),
/* RotateComputable */ false));
// TODO: Investigate promotion cap for O1.
LPM1.addPass(LICMPass(PTO.LicmMssaOptCap, PTO.LicmMssaNoAccForPromotionCap,
/*AllowSpeculation=*/true));
Expand Down
8 changes: 5 additions & 3 deletions llvm/lib/Transforms/Scalar/LoopRotation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ static cl::opt<bool> PrepareForLTOOption(
cl::desc("Run loop-rotation in the prepare-for-lto stage. This option "
"should be used for testing only."));

LoopRotatePass::LoopRotatePass(bool EnableHeaderDuplication, bool PrepareForLTO)
LoopRotatePass::LoopRotatePass(bool EnableHeaderDuplication, bool PrepareForLTO,
bool RotateComputable)
: EnableHeaderDuplication(EnableHeaderDuplication),
PrepareForLTO(PrepareForLTO) {}
PrepareForLTO(PrepareForLTO), RotateComputable(RotateComputable) {}

void LoopRotatePass::printPipeline(
raw_ostream &OS, function_ref<StringRef(StringRef)> MapClassName2PassName) {
Expand Down Expand Up @@ -76,7 +77,8 @@ PreservedAnalyses LoopRotatePass::run(Loop &L, LoopAnalysisManager &AM,
MSSAU = MemorySSAUpdater(AR.MSSA);
bool Changed = LoopRotation(&L, &AR.LI, &AR.TTI, &AR.AC, &AR.DT, &AR.SE,
MSSAU ? &*MSSAU : nullptr, SQ, false, Threshold,
false, PrepareForLTO || PrepareForLTOOption);
false, PrepareForLTO || PrepareForLTOOption,
RotateComputable);

if (!Changed)
return PreservedAnalyses::all();
Expand Down
27 changes: 22 additions & 5 deletions llvm/lib/Transforms/Utils/LoopRotationUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,18 @@ class LoopRotate {
bool RotationOnly;
bool IsUtilMode;
bool PrepareForLTO;
bool RotateComputable;

public:
LoopRotate(unsigned MaxHeaderSize, LoopInfo *LI,
const TargetTransformInfo *TTI, AssumptionCache *AC,
DominatorTree *DT, ScalarEvolution *SE, MemorySSAUpdater *MSSAU,
const SimplifyQuery &SQ, bool RotationOnly, bool IsUtilMode,
bool PrepareForLTO)
bool PrepareForLTO, bool RotateComputable)
: MaxHeaderSize(MaxHeaderSize), LI(LI), TTI(TTI), AC(AC), DT(DT), SE(SE),
MSSAU(MSSAU), SQ(SQ), RotationOnly(RotationOnly),
IsUtilMode(IsUtilMode), PrepareForLTO(PrepareForLTO) {}
IsUtilMode(IsUtilMode), PrepareForLTO(PrepareForLTO),
RotateComputable(RotateComputable) {}
bool processLoop(Loop *L);

private:
Expand Down Expand Up @@ -200,6 +202,19 @@ static bool profitableToRotateLoopExitingLatch(Loop *L) {
return false;
}

// Checks that if loop gets rotated it makes the exit latch count computable.
// This form is beneficial to runtime loop unrolling as well as loop
// vectorization, which requires the loop to be bottom-tested.
static bool rotationMakesLoopComputable(Loop *L, ScalarEvolution *SE) {
BasicBlock *Header = L->getHeader();
BranchInst *BI = dyn_cast<BranchInst>(Header->getTerminator());
assert(BI && BI->isConditional() && "need header with conditional exit");
if (SE && isa<SCEVCouldNotCompute>(SE->getExitCount(L, L->getLoopLatch())) &&
!isa<SCEVCouldNotCompute>(SE->getExitCount(L, Header)))
return true;
return false;
}

static void updateBranchWeights(BranchInst &PreHeaderBI, BranchInst &LoopBI,
bool HasConditionalPreHeader,
bool SuccsSwapped) {
Expand Down Expand Up @@ -364,7 +379,8 @@ bool LoopRotate::rotateLoop(Loop *L, bool SimplifiedLatch) {
// Rotate if the loop latch was just simplified. Or if it makes the loop exit
// count computable. Or if we think it will be profitable.
if (L->isLoopExiting(OrigLatch) && !SimplifiedLatch && IsUtilMode == false &&
!profitableToRotateLoopExitingLatch(L))
!profitableToRotateLoopExitingLatch(L) && !(RotateComputable &&
rotationMakesLoopComputable(L, SE)))
return Rotated;

// Check size of original header and reject loop if it is very big or we can't
Expand Down Expand Up @@ -968,8 +984,9 @@ bool llvm::LoopRotation(Loop *L, LoopInfo *LI, const TargetTransformInfo *TTI,
ScalarEvolution *SE, MemorySSAUpdater *MSSAU,
const SimplifyQuery &SQ, bool RotationOnly = true,
unsigned Threshold = unsigned(-1),
bool IsUtilMode = true, bool PrepareForLTO) {
bool IsUtilMode = true, bool PrepareForLTO,
bool RotateComputable) {
LoopRotate LR(Threshold, LI, TTI, AC, DT, SE, MSSAU, SQ, RotationOnly,
IsUtilMode, PrepareForLTO);
IsUtilMode, PrepareForLTO, RotateComputable);
return LR.processLoop(L);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
; RUN: opt --passes='loop(loop-rotate),loop-unroll' -unroll-runtime=true -unroll-runtime-other-exit-predictable=1 -S %s | FileCheck %s
; RUN: opt --passes='loop-unroll' -unroll-runtime=true -unroll-runtime-other-exit-predictable=1 -S %s | FileCheck %s -check-prefix=NO-ROTATE

target triple = "x86_64-unknown-linux-gnu"

; Test that loop gets unrolled if rotated (becomes computable after rotation).
define void @test(i64 %0, ptr %1) {
; CHECK-LABEL: define void @test(
; CHECK-SAME: i64 [[TMP0:%.*]], ptr [[TMP1:%.*]]) {
; CHECK-NEXT: [[ENTRY:.*:]]
; CHECK-NEXT: [[B1:%.*]] = icmp eq i64 [[TMP0]], 0
; CHECK-NEXT: br i1 [[B1]], label %[[AFTER:.*]], label %[[BODY_LR_PH:.*]]
; CHECK: [[BODY_LR_PH]]:
; CHECK-NEXT: [[TMP5:%.*]] = sub i64 0, [[TMP0]]
; CHECK-NEXT: [[TMP2:%.*]] = freeze i64 [[TMP5]]
; CHECK-NEXT: [[TMP3:%.*]] = add i64 [[TMP2]], -1
; CHECK-NEXT: [[XTRAITER:%.*]] = and i64 [[TMP2]], 7
; CHECK-NEXT: [[LCMP_MOD:%.*]] = icmp ne i64 [[XTRAITER]], 0
; CHECK-NEXT: br i1 [[LCMP_MOD]], label %[[BODY_PROL_PREHEADER:.*]], label %[[BODY_PROL_LOOPEXIT:.*]]
; CHECK: [[BODY_PROL_PREHEADER]]:
; CHECK-NEXT: br label %[[BODY_PROL:.*]]
; CHECK: [[BODY_PROL]]:
; CHECK-NEXT: [[A2_PROL:%.*]] = phi i64 [ [[TMP0]], %[[BODY_PROL_PREHEADER]] ], [ [[A_PROL:%.*]], %[[HEADER_PROL:.*]] ]
; CHECK-NEXT: [[PROL_ITER:%.*]] = phi i64 [ 0, %[[BODY_PROL_PREHEADER]] ], [ [[PROL_ITER_NEXT:%.*]], %[[HEADER_PROL]] ]
; CHECK-NEXT: [[C_PROL:%.*]] = add i64 [[A2_PROL]], 1
; CHECK-NEXT: [[D_PROL:%.*]] = load i32, ptr [[TMP1]], align 4
; CHECK-NEXT: [[E_PROL:%.*]] = icmp eq i32 [[D_PROL]], 0
; CHECK-NEXT: br i1 [[E_PROL]], label %[[END_LOOPEXIT3:.*]], label %[[HEADER_PROL]]
; CHECK: [[HEADER_PROL]]:
; CHECK-NEXT: [[A_PROL]] = phi i64 [ [[C_PROL]], %[[BODY_PROL]] ]
; CHECK-NEXT: [[B_PROL:%.*]] = icmp eq i64 [[A_PROL]], 0
; CHECK-NEXT: [[PROL_ITER_NEXT]] = add i64 [[PROL_ITER]], 1
; CHECK-NEXT: [[PROL_ITER_CMP:%.*]] = icmp ne i64 [[PROL_ITER_NEXT]], [[XTRAITER]]
; CHECK-NEXT: br i1 [[PROL_ITER_CMP]], label %[[BODY_PROL]], label %[[BODY_PROL_LOOPEXIT_UNR_LCSSA:.*]], !llvm.loop [[LOOP0:![0-9]+]]
; CHECK: [[BODY_PROL_LOOPEXIT_UNR_LCSSA]]:
; CHECK-NEXT: [[A2_UNR_PH:%.*]] = phi i64 [ [[A_PROL]], %[[HEADER_PROL]] ]
; CHECK-NEXT: br label %[[BODY_PROL_LOOPEXIT]]
; CHECK: [[BODY_PROL_LOOPEXIT]]:
; CHECK-NEXT: [[A2_UNR:%.*]] = phi i64 [ [[TMP0]], %[[BODY_LR_PH]] ], [ [[A2_UNR_PH]], %[[BODY_PROL_LOOPEXIT_UNR_LCSSA]] ]
; CHECK-NEXT: [[TMP6:%.*]] = icmp ult i64 [[TMP3]], 7
; CHECK-NEXT: br i1 [[TMP6]], label %[[HEADER_AFTER_CRIT_EDGE:.*]], label %[[BODY_LR_PH_NEW:.*]]
; CHECK: [[BODY_LR_PH_NEW]]:
; CHECK-NEXT: br label %[[BODY:.*]]
; CHECK: [[HEADER:.*]]:
; CHECK-NEXT: br i1 false, label %[[END_LOOPEXIT:.*]], label %[[HEADER_1:.*]]
; CHECK: [[HEADER_1]]:
; CHECK-NEXT: br i1 false, label %[[END_LOOPEXIT]], label %[[HEADER_2:.*]]
; CHECK: [[HEADER_2]]:
; CHECK-NEXT: br i1 false, label %[[END_LOOPEXIT]], label %[[HEADER_3:.*]]
; CHECK: [[HEADER_3]]:
; CHECK-NEXT: br i1 false, label %[[END_LOOPEXIT]], label %[[HEADER_4:.*]]
; CHECK: [[HEADER_4]]:
; CHECK-NEXT: br i1 false, label %[[END_LOOPEXIT]], label %[[HEADER_5:.*]]
; CHECK: [[HEADER_5]]:
; CHECK-NEXT: br i1 false, label %[[END_LOOPEXIT]], label %[[HEADER_6:.*]]
; CHECK: [[HEADER_6]]:
; CHECK-NEXT: [[C_7:%.*]] = add i64 [[A2:%.*]], 8
; CHECK-NEXT: br i1 false, label %[[END_LOOPEXIT]], label %[[HEADER_7:.*]]
; CHECK: [[HEADER_7]]:
; CHECK-NEXT: [[B_7:%.*]] = icmp eq i64 [[C_7]], 0
; CHECK-NEXT: br i1 [[B_7]], label %[[HEADER_AFTER_CRIT_EDGE_UNR_LCSSA:.*]], label %[[BODY]]
; CHECK: [[BODY]]:
; CHECK-NEXT: [[A2]] = phi i64 [ [[A2_UNR]], %[[BODY_LR_PH_NEW]] ], [ [[C_7]], %[[HEADER_7]] ]
; CHECK-NEXT: [[D:%.*]] = load i32, ptr [[TMP1]], align 4
; CHECK-NEXT: [[E:%.*]] = icmp eq i32 [[D]], 0
; CHECK-NEXT: br i1 [[E]], label %[[END_LOOPEXIT]], label %[[HEADER]]
; CHECK: [[END_LOOPEXIT]]:
; CHECK-NEXT: br label %[[END:.*]]
; CHECK: [[END_LOOPEXIT3]]:
; CHECK-NEXT: br label %[[END]]
; CHECK: [[END]]:
; CHECK-NEXT: ret void
; CHECK: [[HEADER_AFTER_CRIT_EDGE_UNR_LCSSA]]:
; CHECK-NEXT: br label %[[HEADER_AFTER_CRIT_EDGE]]
; CHECK: [[HEADER_AFTER_CRIT_EDGE]]:
; CHECK-NEXT: br label %[[AFTER]]
; CHECK: [[AFTER]]:
; CHECK-NEXT: ret void
;
; NO-ROTATE-LABEL: define void @test(
; NO-ROTATE-SAME: i64 [[TMP0:%.*]], ptr [[TMP1:%.*]]) {
; NO-ROTATE-NEXT: [[ENTRY:.*]]:
; NO-ROTATE-NEXT: br label %[[HEADER:.*]]
; NO-ROTATE: [[HEADER]]:
; NO-ROTATE-NEXT: [[A_PROL:%.*]] = phi i64 [ [[TMP0]], %[[ENTRY]] ], [ [[C:%.*]], %[[BODY:.*]] ]
; NO-ROTATE-NEXT: [[B_PROL:%.*]] = icmp eq i64 [[A_PROL]], 0
; NO-ROTATE-NEXT: br i1 [[B_PROL]], label %[[AFTER:.*]], label %[[BODY]]
; NO-ROTATE: [[BODY]]:
; NO-ROTATE-NEXT: [[C]] = add i64 [[A_PROL]], 1
; NO-ROTATE-NEXT: [[D:%.*]] = load i32, ptr [[TMP1]], align 4
; NO-ROTATE-NEXT: [[E:%.*]] = icmp eq i32 [[D]], 0
; NO-ROTATE-NEXT: br i1 [[E]], label %[[END:.*]], label %[[HEADER]]
; NO-ROTATE: [[END]]:
; NO-ROTATE-NEXT: ret void
; NO-ROTATE: [[AFTER]]:
; NO-ROTATE-NEXT: ret void
;
entry:
br label %header

header:
%a = phi i64 [ %0, %entry ], [ %c, %body ]
%b = icmp eq i64 %a, 0
br i1 %b, label %after, label %body

body:
%c = add i64 %a, 1
%d = load i32, ptr %1, align 4
%e = icmp eq i32 %d, 0
br i1 %e, label %end, label %header

end:
ret void

after:
ret void
}
;.
; CHECK: [[LOOP0]] = distinct !{[[LOOP0]], [[META1:![0-9]+]]}
; CHECK: [[META1]] = !{!"llvm.loop.unroll.disable"}
;.
Loading