[llvm] 4402d0d - [SCEV] Add a clarifying comment in howManyLessThans

Philip Reames via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 19 15:14:04 PDT 2021


Author: Philip Reames
Date: 2021-07-19T15:13:48-07:00
New Revision: 4402d0d4fbdb903bd61238a06cfad29b4dd2da0b

URL: https://github.com/llvm/llvm-project/commit/4402d0d4fbdb903bd61238a06cfad29b4dd2da0b
DIFF: https://github.com/llvm/llvm-project/commit/4402d0d4fbdb903bd61238a06cfad29b4dd2da0b.diff

LOG: [SCEV] Add a clarifying comment in howManyLessThans

Wrap semantics are subtle when combined with multiple exits.  This has caused several rounds of confusion during recent reviews, so try to document the subtly distinction between when wrap flags provide <u and <=u facts.

Added: 
    

Modified: 
    llvm/lib/Analysis/ScalarEvolution.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp
index cb37958aca4d..d502ac2f3ab3 100644
--- a/llvm/lib/Analysis/ScalarEvolution.cpp
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp
@@ -11587,6 +11587,16 @@ ScalarEvolution::howManyLessThans(const SCEV *LHS, const SCEV *RHS,
   if (!IV || IV->getLoop() != L || !IV->isAffine())
     return getCouldNotCompute();
 
+  // A precondition of this method is that the condition being analyzed
+  // reaches an exiting branch which dominates the latch.  Given that, we can
+  // assume that an increment which violates the nowrap specification and
+  // produces poison must cause undefined behavior when the resulting poison
+  // value is branched upon and thus we can conclude that the backedge is
+  // taken no more often than would be required to produce that poison value.
+  // Note that a well defined loop can exit on the iteration which violates
+  // the nowrap specification if there is another exit (either explicit or
+  // implicit/exceptional) which causes the loop to execute before the
+  // exiting instruction we're analyzing would trigger UB.
   auto WrapType = IsSigned ? SCEV::FlagNSW : SCEV::FlagNUW;
   bool NoWrap = ControlsExit && IV->getNoWrapFlags(WrapType);
   ICmpInst::Predicate Cond = IsSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;


        


More information about the llvm-commits mailing list