Index: lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp =================================================================== --- lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp +++ lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp @@ -3534,7 +3534,12 @@ unsigned SrcAS = SV->getType()->getPointerAddressSpace(); unsigned DestAS = I.getType()->getPointerAddressSpace(); - if (!TLI.isNoopAddrSpaceCast(SrcAS, DestAS)) + // Do not insert address space conversion code when the target doesn't + // require it. Also skip cast expansion when casting an undefined + // value, as a casted undefined value is still an undefined value. + // to make it point + if (!TLI.isNoopAddrSpaceCast(SrcAS, DestAS) && + !isa(SV)) N = DAG.getAddrSpaceCast(getCurSDLoc(), DestVT, N, SrcAS, DestAS); setValue(&I, N); Index: test/CodeGen/AVR/undef-addrspacecast.ll =================================================================== --- /dev/null +++ test/CodeGen/AVR/undef-addrspacecast.ll @@ -0,0 +1,10 @@ +; RUN: llc -mattr=avr6 < %s -march=avr | FileCheck %s + +; CHECK-LABEL: foo +define void @foo() addrspace(1) { +start: + %a = addrspacecast {} addrspace(1)* undef to {}* + store {}* %a, {}** undef, align 1 + ret void +} +