Index: lib/Analysis/LoopInfo.cpp =================================================================== --- lib/Analysis/LoopInfo.cpp +++ lib/Analysis/LoopInfo.cpp @@ -360,12 +360,18 @@ } BranchInst *Loop::getLoopGuardBranch() const { - assert(isLoopSimplifyForm() && "Only valid for loop in simplify form"); + if (!isLoopSimplifyForm()) + return nullptr; + BasicBlock *Preheader = getLoopPreheader(); BasicBlock *Latch = getLoopLatch(); + assert(Preheader && Latch && "Expecting a loop with valid preheader and latch"); - assert(isLoopExiting(Latch) && "Only valid for rotated loop"); + + // Loop should be in rotate form. + if (isLoopExiting(Latch)) + return nullptr; Instruction *LatchTI = Latch->getTerminator(); if (!LatchTI || LatchTI->getNumSuccessors() != 2) Index: unittests/Analysis/LoopInfoTest.cpp =================================================================== --- unittests/Analysis/LoopInfoTest.cpp +++ unittests/Analysis/LoopInfoTest.cpp @@ -283,8 +283,10 @@ EXPECT_EQ(Bounds->getDirection(), Loop::LoopBounds::Direction::Increasing); EXPECT_EQ(L->getInductionVariable(SE)->getName(), "i"); - EXPECT_EQ(L->getLoopGuardBranch(), Guard); - EXPECT_TRUE(L->isGuarded()); + // No guard, loop latch is not exiting. + EXPECT_NE(L->getLoopGuardBranch(), Guard); + EXPECT_EQ(L->getLoopGuardBranch(), nullptr); + EXPECT_FALSE(L->isGuarded()); }); } @@ -341,8 +343,9 @@ EXPECT_EQ(Bounds->getDirection(), Loop::LoopBounds::Direction::Increasing); EXPECT_EQ(L->getInductionVariable(SE)->getName(), "i"); - EXPECT_EQ(L->getLoopGuardBranch(), Guard); - EXPECT_TRUE(L->isGuarded()); + EXPECT_NE(L->getLoopGuardBranch(), Guard); + EXPECT_EQ(L->getLoopGuardBranch(), nullptr); + EXPECT_FALSE(L->isGuarded()); }); } @@ -399,8 +402,10 @@ EXPECT_EQ(Bounds->getDirection(), Loop::LoopBounds::Direction::Increasing); EXPECT_EQ(L->getInductionVariable(SE)->getName(), "i"); - EXPECT_EQ(L->getLoopGuardBranch(), Guard); - EXPECT_TRUE(L->isGuarded()); + // No guard, loop latch is not exiting + EXPECT_NE(L->getLoopGuardBranch(), Guard); + EXPECT_EQ(L->getLoopGuardBranch(), nullptr); + EXPECT_FALSE(L->isGuarded()); }); } @@ -457,8 +462,9 @@ EXPECT_EQ(Bounds->getDirection(), Loop::LoopBounds::Direction::Increasing); EXPECT_EQ(L->getInductionVariable(SE)->getName(), "i"); - EXPECT_EQ(L->getLoopGuardBranch(), Guard); - EXPECT_TRUE(L->isGuarded()); + EXPECT_NE(L->getLoopGuardBranch(), Guard); + EXPECT_EQ(L->getLoopGuardBranch(), nullptr); + EXPECT_FALSE(L->isGuarded()); }); } @@ -515,8 +521,9 @@ EXPECT_EQ(Bounds->getDirection(), Loop::LoopBounds::Direction::Increasing); EXPECT_EQ(L->getInductionVariable(SE)->getName(), "i"); - EXPECT_EQ(L->getLoopGuardBranch(), Guard); - EXPECT_TRUE(L->isGuarded()); + EXPECT_NE(L->getLoopGuardBranch(), Guard); + EXPECT_EQ(L->getLoopGuardBranch(), nullptr); + EXPECT_FALSE(L->isGuarded()); }); } @@ -574,8 +581,10 @@ EXPECT_EQ(Bounds->getDirection(), Loop::LoopBounds::Direction::Increasing); EXPECT_EQ(L->getInductionVariable(SE)->getName(), "i"); - EXPECT_EQ(L->getLoopGuardBranch(), Guard); - EXPECT_TRUE(L->isGuarded()); + // No guard, loop latch is not exiting. + EXPECT_NE(L->getLoopGuardBranch(), Guard); + EXPECT_EQ(L->getLoopGuardBranch(), nullptr); + EXPECT_FALSE(L->isGuarded()); }); } @@ -629,8 +638,10 @@ EXPECT_EQ(Bounds->getCanonicalPredicate(), ICmpInst::ICMP_SLT); EXPECT_EQ(Bounds->getDirection(), Loop::LoopBounds::Direction::Unknown); EXPECT_EQ(L->getInductionVariable(SE)->getName(), "i"); - EXPECT_EQ(L->getLoopGuardBranch(), Guard); - EXPECT_TRUE(L->isGuarded()); + // No guard, loop latch is not exiting + EXPECT_NE(L->getLoopGuardBranch(), Guard); + EXPECT_EQ(L->getLoopGuardBranch(), nullptr); + EXPECT_FALSE(L->isGuarded()); }); } @@ -687,8 +698,9 @@ EXPECT_EQ(Bounds->getDirection(), Loop::LoopBounds::Direction::Increasing); EXPECT_EQ(L->getInductionVariable(SE)->getName(), "i"); - EXPECT_EQ(L->getLoopGuardBranch(), Guard); - EXPECT_TRUE(L->isGuarded()); + EXPECT_NE(L->getLoopGuardBranch(), Guard); + EXPECT_EQ(L->getLoopGuardBranch(), nullptr); + EXPECT_FALSE(L->isGuarded()); }); } @@ -745,8 +757,10 @@ EXPECT_EQ(Bounds->getDirection(), Loop::LoopBounds::Direction::Decreasing); EXPECT_EQ(L->getInductionVariable(SE)->getName(), "i"); - EXPECT_EQ(L->getLoopGuardBranch(), Guard); - EXPECT_TRUE(L->isGuarded()); + // No guard, loop latch is not exiting. + EXPECT_NE(L->getLoopGuardBranch(), Guard); + EXPECT_EQ(L->getLoopGuardBranch(), nullptr); + EXPECT_FALSE(L->isGuarded()); }); } @@ -802,8 +816,10 @@ ICmpInst::BAD_ICMP_PREDICATE); EXPECT_EQ(Bounds->getDirection(), Loop::LoopBounds::Direction::Unknown); EXPECT_EQ(L->getInductionVariable(SE)->getName(), "i"); - EXPECT_EQ(L->getLoopGuardBranch(), Guard); - EXPECT_TRUE(L->isGuarded()); + // No guard becuase loop latch is not exiting. + EXPECT_NE(L->getLoopGuardBranch(), Guard); + EXPECT_EQ(L->getLoopGuardBranch(), nullptr); + EXPECT_FALSE(L->isGuarded()); }); } @@ -863,8 +879,10 @@ EXPECT_EQ(Bounds->getDirection(), Loop::LoopBounds::Direction::Increasing); EXPECT_EQ(L->getInductionVariable(SE)->getName(), "indvars.iv"); - EXPECT_EQ(L->getLoopGuardBranch(), Guard); - EXPECT_TRUE(L->isGuarded()); + // No guard, loop latch is not exiting. + EXPECT_NE(L->getLoopGuardBranch(), Guard); + EXPECT_EQ(L->getLoopGuardBranch(), nullptr); + EXPECT_FALSE(L->isGuarded()); }); } @@ -972,8 +990,9 @@ EXPECT_EQ(Bounds->getDirection(), Loop::LoopBounds::Direction::Increasing); EXPECT_EQ(L->getInductionVariable(SE)->getName(), "i"); - EXPECT_EQ(L->getLoopGuardBranch(), Guard); - EXPECT_TRUE(L->isGuarded()); + EXPECT_NE(L->getLoopGuardBranch(), Guard); + EXPECT_EQ(L->getLoopGuardBranch(), nullptr); + EXPECT_FALSE(L->isGuarded()); }); } @@ -1042,8 +1061,10 @@ EXPECT_EQ(Bounds->getDirection(), Loop::LoopBounds::Direction::Increasing); EXPECT_EQ(L->getInductionVariable(SE)->getName(), "j"); - EXPECT_EQ(L->getLoopGuardBranch(), OuterGuard); - EXPECT_TRUE(L->isGuarded()); + // No guard, loop latch is not exiting. + EXPECT_NE(L->getLoopGuardBranch(), OuterGuard); + EXPECT_EQ(L->getLoopGuardBranch(), nullptr); + EXPECT_FALSE(L->isGuarded()); // Next two basic blocks are for.outer and for.inner.preheader - skip // them. @@ -1066,8 +1087,9 @@ EXPECT_EQ(InnerBounds->getDirection(), Loop::LoopBounds::Direction::Increasing); EXPECT_EQ(L->getInductionVariable(SE)->getName(), "i"); - EXPECT_EQ(L->getLoopGuardBranch(), InnerGuard); - EXPECT_TRUE(L->isGuarded()); + EXPECT_NE(L->getLoopGuardBranch(), InnerGuard); + EXPECT_EQ(L->getLoopGuardBranch(), nullptr); + EXPECT_FALSE(L->isGuarded()); }); } @@ -1147,8 +1169,10 @@ PHINode &Instruction_mulopcode = cast(*(++II)); EXPECT_FALSE( L->isAuxiliaryInductionVariable(Instruction_mulopcode, SE)); - EXPECT_EQ(L->getLoopGuardBranch(), Guard); - EXPECT_TRUE(L->isGuarded()); + // No guard, loop latch is not exiting + EXPECT_NE(L->getLoopGuardBranch(), Guard); + EXPECT_EQ(L->getLoopGuardBranch(), nullptr); + EXPECT_FALSE(L->isGuarded()); }); }