diff --git a/llvm/lib/Transforms/Scalar/LoopInterchange.cpp b/llvm/lib/Transforms/Scalar/LoopInterchange.cpp index 91286ebcea33aa14db71f69580f59ad276d1ff75..b761e5842f5224f8b9fd242d64c41e02aa660b54 100644 --- a/llvm/lib/Transforms/Scalar/LoopInterchange.cpp +++ b/llvm/lib/Transforms/Scalar/LoopInterchange.cpp @@ -49,12 +49,13 @@ using namespace llvm; + #define DEBUG_TYPE "loop-interchange" STATISTIC(LoopsInterchanged, "Number of loops interchanged"); static cl::opt LoopInterchangeCostThreshold( - "loop-interchange-threshold", cl::init(0), cl::Hidden, + "loop-interchange-threshold", cl::init(1), cl::Hidden, cl::desc("Interchange if you gain more than this number")); namespace { @@ -114,6 +115,12 @@ static bool populateDependencyMatrix(CharMatrix &DepMatrix, unsigned Level, for (I = MemInstr.begin(), IE = MemInstr.end(); I != IE; ++I) { for (J = I, JE = MemInstr.end(); J != JE; ++J) { + + if(I==J){ + LLVM_DEBUG(dbgs()<<"Skipping self-dependency analysis\n"); + continue; + } + std::vector Dep; Instruction *Src = cast(*I); Instruction *Dst = cast(*J); @@ -540,6 +547,11 @@ struct LoopInterchange { bool LoopInterchangeLegality::containsUnsafeInstructions(BasicBlock *BB) { return any_of(*BB, [](const Instruction &I) { + if (auto *St = dyn_cast(&I)) + return !St->isSimple(); + if(auto *Ld = dyn_cast(&I)) + return !Ld->isSimple(); + return I.mayHaveSideEffects() || I.mayReadFromMemory(); }); } @@ -561,15 +573,19 @@ bool LoopInterchangeLegality::tightlyNested(Loop *OuterLoop, Loop *InnerLoop) { for (BasicBlock *Succ : successors(OuterLoopHeaderBI)) if (Succ != InnerLoopPreHeader && Succ != InnerLoop->getHeader() && - Succ != OuterLoopLatch) - return false; + Succ != OuterLoopLatch){ + errs() << "first step error" << "\n"; + return false; + } LLVM_DEBUG(dbgs() << "Checking instructions in Loop header and Loop latch\n"); // We do not have any basic block in between now make sure the outer header // and outer loop latch doesn't contain any unsafe instructions. if (containsUnsafeInstructions(OuterLoopHeader) || - containsUnsafeInstructions(OuterLoopLatch)) + containsUnsafeInstructions(OuterLoopLatch)){ + errs() << "second step error" << "\n"; return false; + } // Also make sure the inner loop preheader does not contain any unsafe // instructions. Note that all instructions in the preheader will be moved to @@ -1089,7 +1105,7 @@ int LoopInterchangeProfitability::getInstrOrderCost() { } } } - return GoodOrder - BadOrder; + return GoodOrder - BadOrder; } std::optional @@ -1123,7 +1139,7 @@ LoopInterchangeProfitability::isProfitablePerInstrOrderCost() { // reordering if number of bad orders is more than good. int Cost = getInstrOrderCost(); LLVM_DEBUG(dbgs() << "Cost = " << Cost << "\n"); - if (Cost < 0 && Cost < LoopInterchangeCostThreshold) + if (Cost < 1 && Cost < LoopInterchangeCostThreshold) return std::optional(true); return std::nullopt; @@ -1712,6 +1728,8 @@ PreservedAnalyses LoopInterchangePass::run(LoopNest &LN, LoopAnalysisManager &AM, LoopStandardAnalysisResults &AR, LPMUpdater &U) { + + //errs()<<"the 5th try"<<"\n"; Function &F = *LN.getParent(); DependenceInfo DI(&F, &AR.AA, &AR.SE, &AR.LI);