Preserve AA metadata when combining (cast (load (...))) -> (load (cast
authorChandler Carruth <chandlerc@gmail.com>
Sat, 18 Oct 2014 11:00:12 +0000 (11:00 +0000)
committerChandler Carruth <chandlerc@gmail.com>
Sat, 18 Oct 2014 11:00:12 +0000 (11:00 +0000)
(...))).

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@220141 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
test/Transforms/InstCombine/loadstore-aa-metadata.ll [new file with mode: 0644]

index cfdfa00e80566407e05df2e39573e7ad57e9bd5d..e57091340c02170dd824cff71d801757c7303421 100644 (file)
@@ -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 (file)
index 0000000..691f7f8
--- /dev/null
@@ -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 }