Index: lib/Transforms/Utils/SimplifyIndVar.cpp =================================================================== --- lib/Transforms/Utils/SimplifyIndVar.cpp +++ lib/Transforms/Utils/SimplifyIndVar.cpp @@ -19,7 +19,7 @@ #include "llvm/ADT/Statistic.h" #include "llvm/Analysis/LoopInfo.h" #include "llvm/Analysis/LoopPass.h" -#include "llvm/Analysis/ScalarEvolutionExpressions.h" +#include "llvm/Analysis/ScalarEvolutionExpander.h" #include "llvm/IR/DataLayout.h" #include "llvm/IR/Dominators.h" #include "llvm/IR/IRBuilder.h" @@ -77,7 +77,7 @@ Value *foldIVUser(Instruction *UseInst, Instruction *IVOperand); bool eliminateIdentitySCEV(Instruction *UseInst, Instruction *IVOperand); - bool foldConstantSCEV(Instruction *UseInst); + bool replaceIVUserWithLoopInvariant(Instruction *UseInst); bool eliminateOverflowIntrinsic(CallInst *CI); bool eliminateIVUser(Instruction *UseInst, Instruction *IVOperand); @@ -537,7 +537,7 @@ } /// Replace the UseInst with a constant if possible -bool SimplifyIndvar::foldConstantSCEV(Instruction *I) { +bool SimplifyIndvar::replaceIVUserWithLoopInvariant(Instruction *I) { if (!SE->isSCEVable(I->getType())) return false; @@ -546,21 +546,16 @@ const Loop *L = LI->getLoopFor(I->getParent()); S = SE->getSCEVAtScope(S, L); - auto *C = dyn_cast(S); - if (!C) + if (!SE->isLoopInvariant(S, L)) return false; - Constant *V = C->getValue(); - // The SCEV will have a different type than the instruction if the instruction - // has a pointer type. Skip the replacement - // TODO: Replace ConstantInt Zero by ConstantPointerNull - if (V->getType() != I->getType()) - return false; + SCEVExpander Expander(*SE, SE->getDataLayout(), "loop.invarant"); + auto *Invariant = Expander.expandCodeFor(S, I->getType(), I); - I->replaceAllUsesWith(V); - DEBUG(dbgs() << "INDVARS: Replace IV user: " << *I << " with constant: " << *C - << '\n'); + I->replaceAllUsesWith(Invariant); + DEBUG(dbgs() << "INDVARS: Replace IV user: " << *I + << " with loop invariant: " << *S << '\n'); ++NumFoldedUser; Changed = true; DeadInsts.emplace_back(I); @@ -774,8 +769,9 @@ // Bypass back edges to avoid extra work. if (UseInst == CurrIV) continue; - // Try to replace UseInst with a constant before any other simplifications - if (foldConstantSCEV(UseInst)) + // Try to replace UseInst with a loop invariant before any other + // simplifications + if (replaceIVUserWithLoopInvariant(UseInst)) continue; Instruction *IVOperand = UseOper.second; Index: test/Transforms/IndVarSimplify/constant-fold-1.ll =================================================================== --- test/Transforms/IndVarSimplify/constant-fold-1.ll +++ /dev/null @@ -1,39 +0,0 @@ -; RUN: opt < %s -indvars -S | FileCheck %s - -target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" -target triple = "x86_64-unknown-linux-gnu" - -define void @test(i64* %arg) unnamed_addr align 2 { -bb: - switch i64 undef, label %bb1 [ - ] - -bb1: ; preds = %bb - br label %bb2 - -bb2: ; preds = %bb6, %bb1 - %tmp = phi i64* [%arg, %bb1 ], [ %tmp7, %bb6 ] - switch i2 undef, label %bb6 [ - i2 1, label %bb5 - i2 -2, label %bb3 - i2 -1, label %bb3 - ] - -bb3: ; preds = %bb2, %bb2 - %tmp4 = call fastcc i32* @wobble(i64* nonnull %tmp, i32* null) - %tmp5 = load i32, i32* %tmp4 , align 8 - br label %bb6 - -bb5: ; preds = %bb2 - unreachable - -bb6: ; preds = %bb3, %bb2 - %tmp7 = load i64*, i64** undef, align 8 - br label %bb2 -} - -declare i32* @wobble(i64*, i32* returned) - -; Should not fail when SCEV is fold to ConstantPointerNull -; CHECK-LABEL: void @test -; CHECK: load i32, i32* %{{[a-zA-Z$._0-9]+}} Index: test/Transforms/IndVarSimplify/replace-iv-with-loop-invariant.ll =================================================================== --- /dev/null +++ test/Transforms/IndVarSimplify/replace-iv-with-loop-invariant.ll @@ -0,0 +1,69 @@ +; RUN: opt < %s -indvars -S | FileCheck %s + +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +@G = external global i32 + +define void @test0(i64* %arg) { +bb: + br label %bb2 + +bb2: + %tmp = phi i64* [%arg, %bb ], [ %tmp7, %bb2 ] + %tmp4 = call i32* @wobble(i64* nonnull %tmp, i32* null) + %tmp5 = load i32, i32* %tmp4, align 8 + %tmp7 = load i64*, i64** undef, align 8 + br label %bb2 +} + +; CHECK-LABEL: void @test0 +; CHECK: load i32, i32* null + +define void @test1(i64* %arg) { +bb: + br label %bb2 + +bb2: + %tmp = phi i64* [%arg, %bb ], [ %tmp7, %bb2 ] + %tmp4 = call i32* @wobble(i64* nonnull %tmp, i32* inttoptr (i64 4 to i32*)) + %tmp5 = load i32, i32* %tmp4 + %tmp7 = load i64*, i64** undef, align 8 + br label %bb2 +} + +; CHECK-LABEL: void @test1 +; CHECK: load i32, i32* inttoptr (i64 4 to i32*) + +define void @test2(i64* %arg) { +bb: + br label %bb2 + +bb2: + %tmp = phi i64* [%arg, %bb ], [ %tmp7, %bb2 ] + %tmp4 = call i32* @wobble(i64* nonnull %tmp, i32* @G) + %tmp5 = load i32, i32* %tmp4 + %tmp7 = load i64*, i64** undef, align 8 + br label %bb2 +} + +; CHECK-LABEL: void @test2 +; CHECK: load i32, i32* @G + + +define void @test3(i64* %arg, i32* %loop.invariant) { +bb: + br label %bb2 + +bb2: + %tmp = phi i64* [%arg, %bb ], [ %tmp7, %bb2 ] + %tmp4 = call i32* @wobble(i64* nonnull %tmp, i32* %loop.invariant) + %tmp5 = load i32, i32* %tmp4 + %tmp7 = load i64*, i64** undef, align 8 + br label %bb2 +} + +; CHECK-LABEL: void @test3 +; CHECK: load i32, i32* %loop.invariant + +declare i32* @wobble(i64*, i32* returned)