Index: lib/Transforms/Utils/SimplifyIndVar.cpp =================================================================== --- lib/Transforms/Utils/SimplifyIndVar.cpp +++ lib/Transforms/Utils/SimplifyIndVar.cpp @@ -536,6 +536,29 @@ return false; } +static Constant *ExtractConstant(const SCEV *S, Type *T) { + if (auto *C = dyn_cast(S)) { + Constant *V = C->getValue(); + + if (V->getType() != T) { + // Since only integers and pointers are SCEVable and SCEVConstant are + // integers, the instruction can only have pointer type it does not have + // the same time with the SCEVConstant. + auto *PT = cast(T); + assert(V->isNullValue() && + "SCEV only folds ConstantPointerNull to constant!"); + V = ConstantPointerNull::get(PT); + } + + return V; + } + + if (auto *U = dyn_cast(S)) + return dyn_cast(U->getValue()); + + return nullptr; +} + /// Replace the UseInst with a constant if possible bool SimplifyIndvar::foldConstantSCEV(Instruction *I) { if (!SE->isSCEVable(I->getType())) @@ -546,19 +569,12 @@ const Loop *L = LI->getLoopFor(I->getParent()); S = SE->getSCEVAtScope(S, L); - auto *C = dyn_cast(S); + auto *C = ExtractConstant(S, I->getType()); if (!C) 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; - - I->replaceAllUsesWith(V); + I->replaceAllUsesWith(C); DEBUG(dbgs() << "INDVARS: Replace IV user: " << *I << " with constant: " << *C << '\n'); ++NumFoldedUser; Index: test/Transforms/IndVarSimplify/constant-fold-1.ll =================================================================== --- test/Transforms/IndVarSimplify/constant-fold-1.ll +++ test/Transforms/IndVarSimplify/constant-fold-1.ll @@ -3,37 +3,54 @@ 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 { +@G = external global i32 + +define void @test0(i64* %arg) { bb: - switch i64 undef, label %bb1 [ - ] + br label %bb2 -bb1: ; preds = %bb +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 +} -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 - ] +; Should not fail when SCEV is fold to ConstantPointerNull +; CHECK-LABEL: void @test0 +; CHECK: load i32, i32* null -bb3: ; preds = %bb2, %bb2 - %tmp4 = call fastcc i32* @wobble(i64* nonnull %tmp, i32* null) - %tmp5 = load i32, i32* %tmp4 , align 8 - br label %bb6 +define void @test1(i64* %arg) { +bb: + br label %bb2 -bb5: ; preds = %bb2 - unreachable +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 +} + +; Should not fail on strange constant +; CHECK-LABEL: void @test1 +; CHECK: load i32, i32* inttoptr (i64 4 to i32*) + +define void @test2(i64* %arg) { +bb: + br label %bb2 -bb6: ; preds = %bb3, %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 } -declare i32* @wobble(i64*, i32* returned) +; Should not fail on strange constant +; CHECK-LABEL: void @test2 +; CHECK: load i32, i32* @G -; Should not fail when SCEV is fold to ConstantPointerNull -; CHECK-LABEL: void @test -; CHECK: load i32, i32* %{{[a-zA-Z$._0-9]+}} +declare i32* @wobble(i64*, i32* returned)