Index: lib/Analysis/ScalarEvolutionExpander.cpp =================================================================== --- lib/Analysis/ScalarEvolutionExpander.cpp +++ lib/Analysis/ScalarEvolutionExpander.cpp @@ -487,15 +487,29 @@ // the base to i8* and do an ugly getelementptr with that. It's still // better than ptrtoint+arithmetic+inttoptr at least. if (!AnyNonZeroIndices) { - // Cast the base to i8*. - V = InsertNoopCastOfTo(V, - Type::getInt8PtrTy(Ty->getContext(), PTy->getAddressSpace())); + const SCEV *AddExpr = SE.getAddExpr(Ops); + // Depending on how things worked out in the caller, we may want to switch + // base and offset. + Value *Idx = nullptr; + if (AddExpr->getType()->isPointerTy() && V->getType()->isIntegerTy()) { + // The AddExpr is of pointer type, while V is of integer type, so + // use the AddExpr as the base. + Idx = InsertNoopCastOfTo(V, Ty); + auto BasePtrTy = cast(AddExpr->getType()); + V = expandCodeFor(AddExpr, + Type::getInt8PtrTy(BasePtrTy->getContext(), + BasePtrTy->getAddressSpace())); + } else { + // Cast the base to i8*. + V = InsertNoopCastOfTo( + V, Type::getInt8PtrTy(Ty->getContext(), PTy->getAddressSpace())); - assert(!isa(V) || - SE.DT.dominates(cast(V), &*Builder.GetInsertPoint())); + assert(!isa(V) || + SE.DT.dominates(cast(V), &*Builder.GetInsertPoint())); - // Expand the operands for a plain byte offset. - Value *Idx = expandCodeFor(SE.getAddExpr(Ops), Ty); + // Expand the operands for a plain byte offset. + Idx = expandCodeFor(AddExpr, Ty); + } // Fold a GEP with constant operands. if (Constant *CLHS = dyn_cast(V)) @@ -1305,12 +1319,17 @@ // Expand the core addrec. If we need post-loop scaling, force it to // expand to an integer type to avoid the need for additional casting. Type *ExpandTy = PostLoopScale ? IntTy : STy; + // We can't use a pointer type for the addrec is the pointer type is + // non-integral. + Type *AddRecPHIExpandTy = + DL.isNonIntegralPointerType(STy) ? Normalized->getType() : ExpandTy; + // In some cases, we decide to reuse an existing phi node but need to truncate // it and/or invert the step. Type *TruncTy = nullptr; bool InvertStep = false; - PHINode *PN = getAddRecExprPHILiterally(Normalized, L, ExpandTy, IntTy, - TruncTy, InvertStep); + PHINode *PN = getAddRecExprPHILiterally(Normalized, L, AddRecPHIExpandTy, + IntTy, TruncTy, InvertStep); // Accommodate post-inc mode, if necessary. Value *Result; Index: test/Transforms/LoopStrengthReduce/nonintegral.ll =================================================================== --- /dev/null +++ test/Transforms/LoopStrengthReduce/nonintegral.ll @@ -0,0 +1,45 @@ +; RUN: opt -S -loop-reduce < %s | FileCheck %s + +; Address Space 10 is non-integral. The optimizer is not allowed to use +; ptrtoint/inttoptr instructions. Make sure that this doesn't happen +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128-ni:10:11:12" +target triple = "x86_64-unknown-linux-gnu" + +define void @japi1__unsafe_getindex_65028(i64 addrspace(10)* %arg) { +; CHECK-NOT: inttoptr +; CHECK-NOT: ptrtoint +; How exactly SCEV chooses to materialize isn't all that important, as +; long as it doesn't try to round-trip through integers. As of this writing, +; it emits a byte-wise gep, which is fine. +; CHECK: getelementptr i8, i8 addrspace(10)* {{.*}}, i64 {{.*}} +top: + br label %L86 + +L86: ; preds = %L86, %top + %i.0 = phi i64 [ 0, %top ], [ %0, %L86 ] + %0 = add i64 %i.0, 1 + br i1 undef, label %L86, label %if29 + +if29: ; preds = %L86 + %1 = shl i64 %0, 1 + %2 = add i64 %1, -2 + br label %if31 + +if31: ; preds = %if38, %if29 + %"#temp#1.sroa.0.022" = phi i64 [ 0, %if29 ], [ %3, %if38 ] + br label %L119 + +L119: ; preds = %L119, %if31 + %i5.0 = phi i64 [ %"#temp#1.sroa.0.022", %if31 ], [ %3, %L119 ] + %3 = add i64 %i5.0, 1 + br i1 undef, label %L119, label %if38 + +if38: ; preds = %L119 + %4 = add i64 %2, %i5.0 + %5 = getelementptr i64, i64 addrspace(10)* %arg, i64 %4 + %6 = load i64, i64 addrspace(10)* %5 + br i1 undef, label %done, label %if31 + +done: + ret void +}