Index: lib/Transforms/Scalar/LoopUnrollPass.cpp =================================================================== --- lib/Transforms/Scalar/LoopUnrollPass.cpp +++ lib/Transforms/Scalar/LoopUnrollPass.cpp @@ -278,8 +278,9 @@ public: UnrolledInstAnalyzer(unsigned Iteration, DenseMap &SimplifiedValues, + SmallSet &DeadInstructions, const Loop *L, ScalarEvolution &SE) - : Iteration(Iteration), SimplifiedValues(SimplifiedValues), L(L), SE(SE) { + : Iteration(Iteration), SimplifiedValues(SimplifiedValues), DeadInstructions(DeadInstructions), L(L), SE(SE) { IterationNumber = SE.getConstant(APInt(64, Iteration)); } @@ -316,6 +317,17 @@ /// post-unrolling. DenseMap &SimplifiedValues; + /// \brief Keeps track of all instructions known to become dead. + /// + /// After one operand in an instruction is constant-folded and the + /// instruction is simplified, the other operand might become dead. + /// For example: + /// X = <...> + /// A[i] = X * B[i] + /// If at one iteration B[i] becomes 0, A[i] also becomes 0, and we no longer + /// need to compute X (assuming it has no other uses). + SmallSet &DeadInstructions; + const Loop *L; ScalarEvolution &SE; @@ -542,6 +554,7 @@ SmallSetVector BBWorklist; DenseMap SimplifiedValues; + SmallSet DeadInstructions; // The estimated cost of the unrolled form of the loop. We try to estimate // this by simplifying as much as we can while computing the estimate. @@ -559,7 +572,8 @@ // we literally have to go through all loop's iterations. for (unsigned Iteration = 0; Iteration < TripCount; ++Iteration) { SimplifiedValues.clear(); - UnrolledInstAnalyzer Analyzer(Iteration, SimplifiedValues, L, SE); + DeadInstructions.clear(); + UnrolledInstAnalyzer Analyzer(Iteration, SimplifiedValues, DeadInstructions, L, SE); BBWorklist.clear(); BBWorklist.insert(L->getHeader()); @@ -620,6 +634,25 @@ // won't find them on later ones too. if (UnrolledCost == RolledDynamicCost) return None; + + for (unsigned Idx = BBWorklist.size() - 1; Idx != 0; --Idx) { + BasicBlock *BB = BBWorklist[Idx]; + if (BB->empty()) + continue; + for (BasicBlock::reverse_iterator I = BB->rbegin(), E = BB->rend(); I != E; ++I) { + if (SimplifiedValues.count(&*I)) + continue; + if (DeadInstructions.count(&*I)) + continue; + if (std::all_of(I->user_begin(), I->user_end(), [&](User *U) { + return SimplifiedValues.count(cast(U)) + + DeadInstructions.count(cast(U)); + })) { + UnrolledCost -= TTI.getUserCost(&*I); + DeadInstructions.insert(&*I); + } + } + } } return {{UnrolledCost, RolledDynamicCost}}; } Index: test/Transforms/LoopUnroll/full-unroll-heuristics-dce.ll =================================================================== --- /dev/null +++ test/Transforms/LoopUnroll/full-unroll-heuristics-dce.ll @@ -0,0 +1,38 @@ +; RUN: opt < %s -S -loop-unroll -unroll-max-iteration-count-to-analyze=100 -unroll-dynamic-cost-savings-discount=1000 -unroll-threshold=20 -unroll-percent-dynamic-cost-saved-threshold=50 | FileCheck %s +target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128" + +@known_constant = internal unnamed_addr constant [10 x i32] [i32 0, i32 0, i32 0, i32 0, i32 1, i32 0, i32 0, i32 0, i32 0, i32 0], align 16 + +; If a load becomes a constant after loop unrolling, we sometimes can simplify +; CFG. This test verifies that we handle such cases. +; After one operand in an instruction is constant-folded and the +; instruction is simplified, the other operand might become dead. +; In this test we have:: +; for i in 1..10: +; r += A[i] * B[i] +; A[i] is 0 almost at every iteration, so there is no need in loading B[i] at +; all. + + +; CHECK-LABEL: @unroll_dce +; CHECK-NOT: br i1 %exitcond, label %for.end, label %for.body +define i32 @unroll_dce(i32* noalias nocapture readonly %b) { +entry: + br label %for.body + +for.body: ; preds = %for.body, %entry + %iv.0 = phi i64 [ 0, %entry ], [ %iv.1, %for.body ] + %r.0 = phi i32 [ 0, %entry ], [ %r.1, %for.body ] + %arrayidx1 = getelementptr inbounds [10 x i32], [10 x i32]* @known_constant, i64 0, i64 %iv.0 + %x1 = load i32, i32* %arrayidx1, align 4 + %arrayidx2 = getelementptr inbounds i32, i32* %b, i64 %iv.0 + %x2 = load i32, i32* %arrayidx2, align 4 + %mul = mul i32 %x1, %x2 + %r.1 = add i32 %mul, %r.0 + %iv.1 = add nuw nsw i64 %iv.0, 1 + %exitcond = icmp eq i64 %iv.1, 10 + br i1 %exitcond, label %for.end, label %for.body + +for.end: ; preds = %for.body + ret i32 %r.1 +}