[PATCH] Correctly handle range metadata when hoisting instructions out of then/else into if blocks

Björn Steinbrink bsteinbr at gmail.com
Fri Jul 18 17:10:10 PDT 2014


When there are loads in the then and else blocks, we can still hoist
them up into the if block, but we have to take caret to adjust the range
metadata.

http://reviews.llvm.org/D4595

Files:
  lib/Transforms/Utils/SimplifyCFG.cpp
  test/Transforms/SimplifyCFG/hoist-with-range.ll

Index: lib/Transforms/Utils/SimplifyCFG.cpp
===================================================================
--- lib/Transforms/Utils/SimplifyCFG.cpp
+++ lib/Transforms/Utils/SimplifyCFG.cpp
@@ -1040,6 +1040,10 @@
     if (!I2->use_empty())
       I2->replaceAllUsesWith(I1);
     I1->intersectOptionalDataWith(I2);
+    I1->setMetadata(LLVMContext::MD_range,
+        MDNode::getMostGenericRange(
+          I1->getMetadata(LLVMContext::MD_range),
+          I2->getMetadata(LLVMContext::MD_range)));
     I2->eraseFromParent();
     Changed = true;
 
Index: test/Transforms/SimplifyCFG/hoist-with-range.ll
===================================================================
--- /dev/null
+++ test/Transforms/SimplifyCFG/hoist-with-range.ll
@@ -0,0 +1,20 @@
+; RUN: opt < %s -simplifycfg -S | FileCheck %s
+
+define void @foo(i1 %c, i8* %p) {
+; CHECK: if:
+; CHECK-NEXT: load i8* %p, !range !0
+; CHECK: !0 = metadata !{i8 0, i8 1, i8 3, i8 5}
+if:
+  br i1 %c, label %then, label %else
+then:
+  %t = load i8* %p, !range !0
+  br label %out
+else:
+  %e = load i8* %p, !range !1
+  br label %out
+out:
+  ret void
+}
+
+!0 = metadata !{ i8 0, i8 1 }
+!1 = metadata !{ i8 3, i8 5 }
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D4595.11677.patch
Type: text/x-patch
Size: 1187 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20140719/e205d7d4/attachment.bin>


More information about the llvm-commits mailing list