Index: lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp =================================================================== --- lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp +++ lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp @@ -17,6 +17,7 @@ #include "llvm/IR/DataLayout.h" #include "llvm/IR/LLVMContext.h" #include "llvm/IR/IntrinsicInst.h" +#include "llvm/IR/MDBuilder.h" #include "llvm/Transforms/Utils/BasicBlockUtils.h" #include "llvm/Transforms/Utils/Local.h" using namespace llvm; @@ -335,10 +336,16 @@ break; case LLVMContext::MD_nonnull: - // FIXME: We should translate this into range metadata for integer types - // and vice versa. if (NewTy->isPointerTy()) NewLoad->setMetadata(ID, N); + else if (NewTy->isIntegerTy()) { + // If it's integral, translate it to !range metadata. + const auto *ITy = cast(NewTy); + NewLoad->setMetadata(LLVMContext::MD_range, + MDBuilder(NewLoad->getContext()) + .createRange(APInt(ITy->getBitWidth(), 1), + ITy->getMask())); + } break; case LLVMContext::MD_range: Index: test/Transforms/InstCombine/loadstore-metadata.ll =================================================================== --- test/Transforms/InstCombine/loadstore-metadata.ll +++ test/Transforms/InstCombine/loadstore-metadata.ll @@ -83,20 +83,19 @@ define void @test_load_cast_combine_nonnull(float** %ptr) { ; We can't preserve nonnull metadata when converting a load of a pointer to ; a load of an integer. -; FIXME: We should really transform this into range metadata and vice versa. ; ; CHECK-LABEL: @test_load_cast_combine_nonnull( -; CHECK: %[[V:.*]] = load i64* %{{.*}} +; CHECK: %[[V:.*]] = load i64* %{{.*}}, !range ![[MD:[0-9]+]] ; CHECK-NOT: !nonnull ; CHECK: store i64 %[[V]], i64* entry: %p = load float** %ptr, !nonnull !3 %gep = getelementptr float** %ptr, i32 42 store float* %p, float** %gep - ret void } +; CHECK: ![[MD]] = !{i64 1, i64 -1} !0 = !{ !1, !1, i64 0 } !1 = !{ !1 } !2 = !{ !2, !1 }