Index: lib/Transforms/Scalar/InferAddressSpaces.cpp =================================================================== --- lib/Transforms/Scalar/InferAddressSpaces.cpp +++ lib/Transforms/Scalar/InferAddressSpaces.cpp @@ -308,6 +308,14 @@ } } else if (IntrinsicInst *II = dyn_cast(&I)) { collectRewritableIntrinsicOperands(II, &PostorderStack, &Visited); + } else if (ICmpInst *Cmp = dyn_cast(&I)) { + // FIXME: Handle vectors of pointers + if (Cmp->getOperand(0)->getType()->isPointerTy()) { + appendsGenericAddressExpressionToPostorderStack( + Cmp->getOperand(0), &PostorderStack, &Visited); + appendsGenericAddressExpressionToPostorderStack( + Cmp->getOperand(1), &PostorderStack, &Visited); + } } } @@ -730,7 +738,26 @@ if (handleComplexPtrUse(*CurUser, V, NewV)) continue; - if (isa(U.getUser())) { + if (isa(CurUser)) { + if (ICmpInst *Cmp = dyn_cast(CurUser)) { + int SrcIdx = U.getOperandNo(); + int OtherIdx = (SrcIdx == 0) ? 1 : 0; + Value *OtherSrc = Cmp->getOperand(OtherIdx); + + if (Value *OtherNewV = ValueWithNewAddrSpace.lookup(OtherSrc)) { + Cmp->setOperand(OtherIdx, OtherNewV); + Cmp->setOperand(SrcIdx, NewV); + continue; + } + + if (auto *KOtherSrc = dyn_cast(OtherSrc)) { + Cmp->setOperand(SrcIdx, NewV); + Cmp->setOperand(OtherIdx, + ConstantExpr::getAddrSpaceCast(KOtherSrc, NewV->getType())); + continue; + } + } + // Otherwise, replaces the use with generic(NewV). // TODO: Some optimization opportunities are missed. For example, in // %0 = icmp eq float* %p, %q Index: test/Transforms/InferAddressSpaces/AMDGPU/icmp.ll =================================================================== --- /dev/null +++ test/Transforms/InferAddressSpaces/AMDGPU/icmp.ll @@ -0,0 +1,76 @@ +; RUN: opt -S -mtriple=amdgcn-amd-amdhsa -infer-address-spaces %s | FileCheck %s + +; CHECK-LABEL: @icmp_flat_cmp_self( +; CHECK: %cmp = icmp eq i32 addrspace(3)* %group.ptr.0, %group.ptr.0 +define i1 @icmp_flat_cmp_self(i32 addrspace(3)* %group.ptr.0) #0 { + %cast0 = addrspacecast i32 addrspace(3)* %group.ptr.0 to i32 addrspace(4)* + %cmp = icmp eq i32 addrspace(4)* %cast0, %cast0 + ret i1 %cmp +} + +; CHECK-LABEL: @icmp_flat_flat_from_group( +; CHECK: %cmp = icmp eq i32 addrspace(3)* %group.ptr.0, %group.ptr.1 +define i1 @icmp_flat_flat_from_group(i32 addrspace(3)* %group.ptr.0, i32 addrspace(3)* %group.ptr.1) #0 { + %cast0 = addrspacecast i32 addrspace(3)* %group.ptr.0 to i32 addrspace(4)* + %cast1 = addrspacecast i32 addrspace(3)* %group.ptr.1 to i32 addrspace(4)* + %cmp = icmp eq i32 addrspace(4)* %cast0, %cast1 + ret i1 %cmp +} + + ; CHECK-LABEL: @icmp_flat_group_flat( +; CHECK: %1 = addrspacecast i32 addrspace(3)* %group.ptr.0 to i32 addrspace(4)* +; CHECK: %cmp = icmp eq i32 addrspace(4)* %1, %flat.ptr.1 +define i1 @icmp_flat_group_flat(i32 addrspace(3)* %group.ptr.0, i32 addrspace(4)* %flat.ptr.1) #0 { + %cast0 = addrspacecast i32 addrspace(3)* %group.ptr.0 to i32 addrspace(4)* + %cmp = icmp eq i32 addrspace(4)* %cast0, %flat.ptr.1 + ret i1 %cmp +} + +; CHECK-LABEL: @icmp_flat_flat_group( +; CHECK: %1 = addrspacecast i32 addrspace(3)* %group.ptr.1 to i32 addrspace(4)* +; CHECK: %cmp = icmp eq i32 addrspace(4)* %flat.ptr.0, %1 +define i1 @icmp_flat_flat_group(i32 addrspace(4)* %flat.ptr.0, i32 addrspace(3)* %group.ptr.1) #0 { + %cast1 = addrspacecast i32 addrspace(3)* %group.ptr.1 to i32 addrspace(4)* + %cmp = icmp eq i32 addrspace(4)* %flat.ptr.0, %cast1 + ret i1 %cmp +} + +; Keeping as cmp addrspace(3)* is better +; CHECK-LABEL: @icmp_flat_to_group_cmp( +; CHECK: %cast0 = addrspacecast i32 addrspace(4)* %flat.ptr.0 to i32 addrspace(3)* +; CHECK: %cast1 = addrspacecast i32 addrspace(4)* %flat.ptr.1 to i32 addrspace(3)* +; CHECK: %cmp = icmp eq i32 addrspace(3)* %cast0, %cast1 +define i1 @icmp_flat_to_group_cmp(i32 addrspace(4)* %flat.ptr.0, i32 addrspace(4)* %flat.ptr.1) #0 { + %cast0 = addrspacecast i32 addrspace(4)* %flat.ptr.0 to i32 addrspace(3)* + %cast1 = addrspacecast i32 addrspace(4)* %flat.ptr.1 to i32 addrspace(3)* + %cmp = icmp eq i32 addrspace(3)* %cast0, %cast1 + ret i1 %cmp +} + +; CHECK-LABEL: @icmp_group_flat_cmp_null( +; CHECK: %cmp = icmp eq i32 addrspace(3)* %group.ptr.0, addrspacecast (i32 addrspace(4)* null to i32 addrspace(3)*) +define i1 @icmp_group_flat_cmp_null(i32 addrspace(3)* %group.ptr.0) #0 { + %cast0 = addrspacecast i32 addrspace(3)* %group.ptr.0 to i32 addrspace(4)* + %cmp = icmp eq i32 addrspace(4)* %cast0, null + ret i1 %cmp +} + +; CHECK-LABEL: @icmp_group_flat_cmp_undef( +; CHECK: %cmp = icmp eq i32 addrspace(3)* %group.ptr.0, undef +define i1 @icmp_group_flat_cmp_undef(i32 addrspace(3)* %group.ptr.0) #0 { + %cast0 = addrspacecast i32 addrspace(3)* %group.ptr.0 to i32 addrspace(4)* + %cmp = icmp eq i32 addrspace(4)* %cast0, undef + ret i1 %cmp +} + +; TODO: Should be handled +; CHECK-LABEL: @icmp_flat_flat_from_group_vector( +; CHECK: %cmp = icmp eq <2 x i32 addrspace(4)*> %cast0, %cast1 +define <2 x i1> @icmp_flat_flat_from_group_vector(<2 x i32 addrspace(3)*> %group.ptr.0, <2 x i32 addrspace(3)*> %group.ptr.1) #0 { + %cast0 = addrspacecast <2 x i32 addrspace(3)*> %group.ptr.0 to <2 x i32 addrspace(4)*> + %cast1 = addrspacecast <2 x i32 addrspace(3)*> %group.ptr.1 to <2 x i32 addrspace(4)*> + %cmp = icmp eq <2 x i32 addrspace(4)*> %cast0, %cast1 + ret <2 x i1> %cmp +} + +attributes #0 = { nounwind } Index: test/Transforms/InferAddressSpaces/AMDGPU/infer-address-space.ll =================================================================== --- test/Transforms/InferAddressSpaces/AMDGPU/infer-address-space.ll +++ test/Transforms/InferAddressSpaces/AMDGPU/infer-address-space.ll @@ -106,8 +106,7 @@ ; CHECK-LABEL: @loop( ; CHECK: %p = bitcast [10 x float] addrspace(3)* @array to float addrspace(3)* -; CHECK: %0 = addrspacecast float addrspace(3)* %p to float addrspace(4)* -; CHECK: %end = getelementptr float, float addrspace(4)* %0, i64 10 +; CHECK: %end = getelementptr float, float addrspace(3)* %p, i64 10 ; CHECK: br label %loop ; CHECK: loop: ; preds = %loop, %entry @@ -115,8 +114,8 @@ ; CHECK: %v = load float, float addrspace(3)* %i ; CHECK: call void @use(float %v) ; CHECK: %i2 = getelementptr float, float addrspace(3)* %i, i64 1 -; CHECK: %1 = addrspacecast float addrspace(3)* %i2 to float addrspace(4)* -; CHECK: %exit_cond = icmp eq float addrspace(4)* %1, %end +; CHECK: %exit_cond = icmp eq float addrspace(3)* %i2, %end + ; CHECK: br i1 %exit_cond, label %exit, label %loop define void @loop() #0 { entry: