#include "llvm/Analysis/Dominators.h"
#include "llvm/Assembly/Writer.h"
#include "llvm/Support/CFG.h"
-#include "llvm/Support/Streams.h"
#include "llvm/ADT/DepthFirstIterator.h"
#include "llvm/ADT/SmallPtrSet.h"
#include <algorithm>
/// If InsertPt is specified, it is the point to hoist instructions to.
/// If null, the terminator of the loop preheader is used.
///
-bool Loop::makeLoopInvariant(Value *V, Instruction *InsertPt) const {
+bool Loop::makeLoopInvariant(Value *V, bool &Changed,
+ Instruction *InsertPt) const {
if (Instruction *I = dyn_cast<Instruction>(V))
- return makeLoopInvariant(I);
+ return makeLoopInvariant(I, Changed, InsertPt);
return true; // All non-instructions are loop-invariant.
}
/// If InsertPt is specified, it is the point to hoist instructions to.
/// If null, the terminator of the loop preheader is used.
///
-bool Loop::makeLoopInvariant(Instruction *I, Instruction *InsertPt) const {
+bool Loop::makeLoopInvariant(Instruction *I, bool &Changed,
+ Instruction *InsertPt) const {
// Test if the value is already loop-invariant.
if (isLoopInvariant(I))
return true;
- // Don't hoist instructions with side-effects.
- if (I->isTrapping())
+ if (!I->isSafeToSpeculativelyExecute())
return false;
- // Don't hoist PHI nodes.
- if (isa<PHINode>(I))
- return false;
- // Don't hoist allocation instructions.
- if (isa<AllocationInst>(I))
+ if (I->mayReadFromMemory())
return false;
// Determine the insertion point, unless one was given.
if (!InsertPt) {
}
// Don't hoist instructions with loop-variant operands.
for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
- if (!makeLoopInvariant(I->getOperand(i), InsertPt))
+ if (!makeLoopInvariant(I->getOperand(i), Changed, InsertPt))
return false;
// Hoist.
I->moveBefore(InsertPt);
+ Changed = true;
return true;
}
return true;
}
+
+/// isLoopSimplifyForm - Return true if the Loop is in the form that
+/// the LoopSimplify form transforms loops to, which is sometimes called
+/// normal form.
+bool Loop::isLoopSimplifyForm() const {
+ // Normal-form loops have a preheader.
+ if (!getLoopPreheader())
+ return false;
+ // Normal-form loops have a single backedge.
+ if (!getLoopLatch())
+ return false;
+ // Each predecessor of each exit block of a normal loop is contained
+ // within the loop.
+ SmallVector<BasicBlock *, 4> ExitBlocks;
+ getExitBlocks(ExitBlocks);
+ for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i)
+ for (pred_iterator PI = pred_begin(ExitBlocks[i]),
+ PE = pred_end(ExitBlocks[i]); PI != PE; ++PI)
+ if (!contains(*PI))
+ return false;
+ // All the requirements are met.
+ return true;
+}
+
//===----------------------------------------------------------------------===//
// LoopInfo implementation
//
AU.setPreservesAll();
AU.addRequired<DominatorTree>();
}
+
+void LoopInfo::print(raw_ostream &OS, const Module*) const {
+ LI.print(OS);
+}
+