|
16 | 16 | #include "llvm/ADT/Statistic.h"
|
17 | 17 | #include "llvm/Analysis/PostDominators.h"
|
18 | 18 | #include "llvm/Analysis/ValueTracking.h"
|
19 |
| -#include "llvm/Transforms/Utils/BasicBlockUtils.h" |
20 | 19 |
|
21 | 20 | using namespace llvm;
|
22 | 21 |
|
@@ -254,66 +253,49 @@ static bool checkLoopsStructure(const Loop &OuterLoop, const Loop &InnerLoop,
|
254 | 253 | // Ensure the only branch that may exist between the loops is the inner loop
|
255 | 254 | // guard.
|
256 | 255 | if (OuterLoopHeader != InnerLoopPreHeader) {
|
257 |
| - const BasicBlock &SingleSucc = |
258 |
| - skipEmptyBlockUntil(OuterLoopHeader, InnerLoopPreHeader); |
259 |
| - |
260 |
| - // no conditional branch present |
261 |
| - if (&SingleSucc != InnerLoopPreHeader) { |
262 |
| - const BranchInst *BI = dyn_cast<BranchInst>(SingleSucc.getTerminator()); |
263 |
| - |
264 |
| - if (!BI || BI != InnerLoop.getLoopGuardBranch()) |
265 |
| - return false; |
266 |
| - |
267 |
| - bool InnerLoopExitContainsLCSSA = ContainsLCSSAPhi(*InnerLoopExit); |
268 |
| - |
269 |
| - // The successors of the inner loop guard should be the inner loop |
270 |
| - // preheader or the outer loop latch possibly through empty blocks. |
271 |
| - for (const BasicBlock *Succ : BI->successors()) { |
272 |
| - const BasicBlock *PotentialInnerPreHeader = Succ; |
273 |
| - const BasicBlock *PotentialOuterLatch = Succ; |
274 |
| - |
275 |
| - // Ensure the inner loop guard successor is empty before skipping |
276 |
| - // blocks. |
277 |
| - if (Succ->getInstList().size() == 1) { |
278 |
| - PotentialInnerPreHeader = |
279 |
| - &skipEmptyBlockUntil(Succ, InnerLoopPreHeader); |
280 |
| - PotentialOuterLatch = &skipEmptyBlockUntil(Succ, OuterLoopLatch); |
281 |
| - } |
282 |
| - |
283 |
| - if (PotentialInnerPreHeader == InnerLoopPreHeader) |
284 |
| - continue; |
285 |
| - if (PotentialOuterLatch == OuterLoopLatch) |
286 |
| - continue; |
287 |
| - |
288 |
| - // If `InnerLoopExit` contains LCSSA Phi instructions, additional block |
289 |
| - // may be inserted before the `OuterLoopLatch` to which `BI` jumps. The |
290 |
| - // loops are still considered perfectly nested if the extra block only |
291 |
| - // contains Phi instructions from InnerLoopExit and OuterLoopHeader. |
292 |
| - if (InnerLoopExitContainsLCSSA && IsExtraPhiBlock(*Succ) && |
293 |
| - Succ->getSingleSuccessor() == OuterLoopLatch) { |
294 |
| - // Points to the extra block so that we can reference it later in the |
295 |
| - // final check. We can also conclude that the inner loop is |
296 |
| - // guarded and there exists LCSSA Phi node in the exit block later if |
297 |
| - // we see a non-null `ExtraPhiBlock`. |
298 |
| - ExtraPhiBlock = Succ; |
299 |
| - continue; |
300 |
| - } |
301 |
| - |
302 |
| - DEBUG_WITH_TYPE(VerboseDebug, { |
303 |
| - dbgs() << "Inner loop guard successor " << Succ->getName() |
304 |
| - << " doesn't lead to inner loop preheader or " |
305 |
| - "outer loop latch.\n"; |
306 |
| - }); |
307 |
| - return false; |
| 256 | + const BranchInst *BI = |
| 257 | + dyn_cast<BranchInst>(OuterLoopHeader->getTerminator()); |
| 258 | + |
| 259 | + if (!BI || BI != InnerLoop.getLoopGuardBranch()) |
| 260 | + return false; |
| 261 | + |
| 262 | + bool InnerLoopExitContainsLCSSA = ContainsLCSSAPhi(*InnerLoopExit); |
| 263 | + |
| 264 | + // The successors of the inner loop guard should be the inner loop |
| 265 | + // preheader and the outer loop latch. |
| 266 | + for (const BasicBlock *Succ : BI->successors()) { |
| 267 | + if (Succ == InnerLoopPreHeader) |
| 268 | + continue; |
| 269 | + if (Succ == OuterLoopLatch) |
| 270 | + continue; |
| 271 | + |
| 272 | + // If `InnerLoopExit` contains LCSSA Phi instructions, additional block |
| 273 | + // may be inserted before the `OuterLoopLatch` to which `BI` jumps. The |
| 274 | + // loops are still considered perfectly nested if the extra block only |
| 275 | + // contains Phi instructions from InnerLoopExit and OuterLoopHeader. |
| 276 | + if (InnerLoopExitContainsLCSSA && IsExtraPhiBlock(*Succ) && |
| 277 | + Succ->getSingleSuccessor() == OuterLoopLatch) { |
| 278 | + // Points to the extra block so that we can reference it later in the |
| 279 | + // final check. We can also conclude that the inner loop is |
| 280 | + // guarded and there exists LCSSA Phi node in the exit block later if we |
| 281 | + // see a non-null `ExtraPhiBlock`. |
| 282 | + ExtraPhiBlock = Succ; |
| 283 | + continue; |
308 | 284 | }
|
| 285 | + |
| 286 | + DEBUG_WITH_TYPE(VerboseDebug, { |
| 287 | + dbgs() << "Inner loop guard successor " << Succ->getName() |
| 288 | + << " doesn't lead to inner loop preheader or " |
| 289 | + "outer loop latch.\n"; |
| 290 | + }); |
| 291 | + return false; |
309 | 292 | }
|
310 | 293 | }
|
311 | 294 |
|
312 |
| - // Ensure the inner loop exit block lead to the outer loop latch possibly |
313 |
| - // through empty blocks. |
314 |
| - const BasicBlock &SuccInner = |
315 |
| - skipEmptyBlockUntil(InnerLoop.getExitBlock(), OuterLoopLatch); |
316 |
| - if (&SuccInner != OuterLoopLatch && &SuccInner != ExtraPhiBlock) { |
| 295 | + // Ensure the inner loop exit block leads to the outer loop latch. |
| 296 | + const BasicBlock *SuccInner = InnerLoopExit->getSingleSuccessor(); |
| 297 | + if (!SuccInner || |
| 298 | + (SuccInner != OuterLoopLatch && SuccInner != ExtraPhiBlock)) { |
317 | 299 | DEBUG_WITH_TYPE(
|
318 | 300 | VerboseDebug,
|
319 | 301 | dbgs() << "Inner loop exit block " << *InnerLoopExit
|
|
0 commit comments