Index: lib/Transforms/Utils/LoopUnrollRuntime.cpp =================================================================== --- lib/Transforms/Utils/LoopUnrollRuntime.cpp +++ lib/Transforms/Utils/LoopUnrollRuntime.cpp @@ -313,7 +313,10 @@ BasicBlock *Header = L->getHeader(); const DataLayout &DL = Header->getModule()->getDataLayout(); SCEVExpander Expander(*SE, DL, "loop-unroll"); - if (!AllowExpensiveTripCount && Expander.isHighCostExpansion(TripCountSC, L)) + + // When the unroll factor is 2, we don't need to check cost expansion. + if (Count != 2 && !AllowExpensiveTripCount && + Expander.isHighCostExpansion(TripCountSC, L)) return false; // We only handle cases when the unroll factor is a power of 2.