Index: lib/IR/ConstantFold.cpp =================================================================== --- lib/IR/ConstantFold.cpp +++ lib/IR/ConstantFold.cpp @@ -1500,7 +1500,8 @@ assert(isa(V2) && "Canonicalization guarantee!"); // GlobalVals can never be null unless they have external weak linkage. // We don't try to evaluate aliases here. - if (!GV->hasExternalWeakLinkage() && !isa(GV)) + if (!GV->hasExternalWeakLinkage() && !isa(GV) && + GV->getType()->getAddressSpace() == 0) return ICmpInst::ICMP_NE; } } else if (const BlockAddress *BA = dyn_cast(V1)) { @@ -1730,7 +1731,8 @@ if (C1->isNullValue()) { if (const GlobalValue *GV = dyn_cast(C2)) // Don't try to evaluate aliases. External weak GV can be null. - if (!isa(GV) && !GV->hasExternalWeakLinkage()) { + if (!isa(GV) && !GV->hasExternalWeakLinkage() && + GV->getType()->getAddressSpace() == 0) { if (pred == ICmpInst::ICMP_EQ) return ConstantInt::getFalse(C1->getContext()); else if (pred == ICmpInst::ICMP_NE) @@ -1740,7 +1742,8 @@ } else if (C2->isNullValue()) { if (const GlobalValue *GV = dyn_cast(C1)) // Don't try to evaluate aliases. External weak GV can be null. - if (!isa(GV) && !GV->hasExternalWeakLinkage()) { + if (!isa(GV) && !GV->hasExternalWeakLinkage() && + GV->getType()->getAddressSpace() == 0) { if (pred == ICmpInst::ICMP_EQ) return ConstantInt::getFalse(C1->getContext()); else if (pred == ICmpInst::ICMP_NE) Index: test/Assembler/ConstantExprNoFold.ll =================================================================== --- test/Assembler/ConstantExprNoFold.ll +++ test/Assembler/ConstantExprNoFold.ll @@ -42,6 +42,17 @@ @empty.2 = external global [0 x i8], align 1 @empty.cmp = global i1 icmp eq ([0 x i8]* @empty.1, [0 x i8]* @empty.2) +@addrspace3 = internal addrspace(3) global i32 undef + +; CHECK: @no.fold.addrspace.icmp.eq.gv.null = global i1 icmp eq (i32 addrspace(3)* @addrspace3, i32 addrspace(3)* null) +; CHECK: @no.fold.addrspace.icmp.eq.null.gv = global i1 icmp eq (i32 addrspace(3)* @addrspace3, i32 addrspace(3)* null) +; CHECK: @no.fold.addrspace.icmp.ne.gv.null = global i1 icmp ne (i32 addrspace(3)* @addrspace3, i32 addrspace(3)* null) +; CHECK: @no.fold.addrspace.icmp.ne.null.gv = global i1 icmp ne (i32 addrspace(3)* @addrspace3, i32 addrspace(3)* null) +@no.fold.addrspace.icmp.eq.gv.null = global i1 icmp eq (i32 addrspace(3)* @addrspace3, i32 addrspace(3)* null) +@no.fold.addrspace.icmp.eq.null.gv = global i1 icmp eq (i32 addrspace(3)* null, i32 addrspace(3)* @addrspace3) +@no.fold.addrspace.icmp.ne.gv.null = global i1 icmp ne (i32 addrspace(3)* @addrspace3, i32 addrspace(3)* null) +@no.fold.addrspace.icmp.ne.null.gv = global i1 icmp ne (i32 addrspace(3)* null, i32 addrspace(3)* @addrspace3) + ; Don't add an inbounds on @glob.a3, since it's not inbounds. ; CHECK: @glob.a3 = alias i32, getelementptr (i32, i32* @glob.a2, i32 1) @glob = global i32 0 Index: test/Transforms/InstCombine/constant-fold-address-space-pointer.ll =================================================================== --- test/Transforms/InstCombine/constant-fold-address-space-pointer.ll +++ test/Transforms/InstCombine/constant-fold-address-space-pointer.ll @@ -118,14 +118,14 @@ define i1 @constant_fold_ptrtoint_null() { ; CHECK-LABEL: @constant_fold_ptrtoint_null( -; CHECK-NEXT: ret i1 false +; CHECK-NEXT: ret i1 icmp eq (i32 addrspace(3)* @g, i32 addrspace(3)* null) %x = icmp eq i16 ptrtoint (i32 addrspace(3)* @g to i16), ptrtoint (i32 addrspace(3)* null to i16) ret i1 %x } define i1 @constant_fold_ptrtoint_null_2() { ; CHECK-LABEL: @constant_fold_ptrtoint_null_2( -; CHECK-NEXT: ret i1 false +; CHECK-NEXT: ret i1 icmp eq (i32 addrspace(3)* @g, i32 addrspace(3)* null) %x = icmp eq i16 ptrtoint (i32 addrspace(3)* null to i16), ptrtoint (i32 addrspace(3)* @g to i16) ret i1 %x }