From 797e9b812e2a8d29e95d6735774c117390512e8e Mon Sep 17 00:00:00 2001 From: Chandler Carruth Date: Sat, 18 Oct 2014 11:00:12 +0000 Subject: [PATCH] Preserve AA metadata when combining (cast (load (...))) -> (load (cast (...))). git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@220141 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../InstCombineLoadStoreAlloca.cpp | 3 +++ .../InstCombine/loadstore-aa-metadata.ll | 25 +++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 test/Transforms/InstCombine/loadstore-aa-metadata.ll diff --git a/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp b/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp index cfdfa00e805..e57091340c0 100644 --- a/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp +++ b/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp @@ -319,6 +319,8 @@ static Instruction *combineLoadToOperationType(InstCombiner &IC, LoadInst &LI) { Value *Ptr = LI.getPointerOperand(); unsigned AS = LI.getPointerAddressSpace(); + AAMDNodes AAInfo; + LI.getAAMetadata(AAInfo); // Fold away bit casts of the loaded value by loading the desired type. if (LI.hasOneUse()) @@ -326,6 +328,7 @@ static Instruction *combineLoadToOperationType(InstCombiner &IC, LoadInst &LI) { LoadInst *NewLoad = IC.Builder->CreateAlignedLoad( IC.Builder->CreateBitCast(Ptr, BC->getDestTy()->getPointerTo(AS)), LI.getAlignment(), LI.getName()); + NewLoad->setAAMetadata(AAInfo); BC->replaceAllUsesWith(NewLoad); IC.EraseInstFromFunction(*BC); return &LI; diff --git a/test/Transforms/InstCombine/loadstore-aa-metadata.ll b/test/Transforms/InstCombine/loadstore-aa-metadata.ll new file mode 100644 index 00000000000..691f7f8345f --- /dev/null +++ b/test/Transforms/InstCombine/loadstore-aa-metadata.ll @@ -0,0 +1,25 @@ +; RUN: opt -instcombine -S < %s | FileCheck %s + +define i32 @test_load_cast_combine_tbaa(float* %ptr) { +; Ensure (cast (load (...))) -> (load (cast (...))) preserves TBAA. +; CHECK-LABEL: @test_load_cast_combine_tbaa( +; CHECK: load i32* %{{.*}}, !tbaa !0 +entry: + %l = load float* %ptr, !tbaa !0 + %c = bitcast float %l to i32 + ret i32 %c +} + +define i32 @test_load_cast_combine_noalias(float* %ptr) { +; Ensure (cast (load (...))) -> (load (cast (...))) preserves no-alias metadata. +; CHECK-LABEL: @test_load_cast_combine_noalias( +; CHECK: load i32* %{{.*}}, !alias.scope !2, !noalias !1 +entry: + %l = load float* %ptr, !alias.scope !2, !noalias !1 + %c = bitcast float %l to i32 + ret i32 %c +} + +!0 = metadata !{ metadata !1, metadata !1, i64 0 } +!1 = metadata !{ metadata !1 } +!2 = metadata !{ metadata !2, metadata !1 }