Rework codegen of case ranges
- Fix multiple issues with the way case ranges were emitted, see test
cases details about the specific issues.
- The root issue was not being careful about how basic blocks were
emitted which led to them being chained together incorrectly,
resulting in improper control flow.
- Fixes <rdar://problem/6098585>
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@54006 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/test/CodeGen/rdr-6098585-default-after-caserange.c b/test/CodeGen/rdr-6098585-default-after-caserange.c
new file mode 100644
index 0000000..1a7a53f
--- /dev/null
+++ b/test/CodeGen/rdr-6098585-default-after-caserange.c
@@ -0,0 +1,18 @@
+// RUN: clang --emit-llvm-bc -o - %s | opt -std-compile-opts | llvm-dis > %t &&
+// RUN: grep "ret i32" %t | count 1 &&
+// RUN: grep "ret i32 10" %t | count 1
+
+// Ensure that default after a case range is not ignored.
+
+static int f1(unsigned x) {
+ switch(x) {
+ case 10 ... 0xFFFFFFFF:
+ return 0;
+ default:
+ return 10;
+ }
+}
+
+int g() {
+ return f1(2);
+}
diff --git a/test/CodeGen/rdr-6098585-default-fallthrough-to-caserange.c b/test/CodeGen/rdr-6098585-default-fallthrough-to-caserange.c
new file mode 100644
index 0000000..7cb9384
--- /dev/null
+++ b/test/CodeGen/rdr-6098585-default-fallthrough-to-caserange.c
@@ -0,0 +1,20 @@
+// RUN: clang --emit-llvm-bc -o - %s | opt -std-compile-opts | llvm-dis > %t &&
+// RUN: grep "ret i32 10" %t
+
+// Ensure that this doesn't compile to infinite loop in g() due to
+// miscompilation of fallthrough from default to a (tested) case
+// range.
+
+static int f0(unsigned x) {
+ switch(x) {
+ default:
+ x += 1;
+ case 10 ... 0xFFFFFFFF:
+ return 0;
+ }
+}
+
+int g() {
+ f0(1);
+ return 10;
+}
diff --git a/test/CodeGen/rdr-6098585-empty-case-range.c b/test/CodeGen/rdr-6098585-empty-case-range.c
new file mode 100644
index 0000000..2dc3cf3
--- /dev/null
+++ b/test/CodeGen/rdr-6098585-empty-case-range.c
@@ -0,0 +1,23 @@
+// RUN: clang --emit-llvm-bc -o - %s | opt -std-compile-opts | llvm-dis > %t &&
+// RUN: grep "ret i32" %t | count 2 &&
+// RUN: grep "ret i32 3" %t | count 2
+
+// This generated incorrect code because of poor switch chaining.
+int f1(int x) {
+ switch(x) {
+ default:
+ return 3;
+ case 10 ... 0xFFFFFFFF:
+ return 0;
+ }
+}
+
+// This just asserted because of the way case ranges were calculated.
+int f2(int x) {
+ switch (x) {
+ default:
+ return 3;
+ case 10 ... -1:
+ return 0;
+ }
+}
diff --git a/test/CodeGen/rdr-6098585-fallthrough-to-empty-range.c b/test/CodeGen/rdr-6098585-fallthrough-to-empty-range.c
new file mode 100644
index 0000000..007a0c7
--- /dev/null
+++ b/test/CodeGen/rdr-6098585-fallthrough-to-empty-range.c
@@ -0,0 +1,15 @@
+// RUN: clang --emit-llvm-bc -o - %s | opt -std-compile-opts | llvm-dis > %t &&
+// RUN: grep "ret i32 %" %t
+
+// Make sure return is not constant (if empty range is skipped or miscompiled)
+
+int f0(unsigned x) {
+ switch(x) {
+ case 2:
+ // fallthrough empty range
+ case 10 ... 9:
+ return 10;
+ default:
+ return 0;
+ }
+}
diff --git a/test/CodeGen/rdr-6098585-unsigned-caserange.c b/test/CodeGen/rdr-6098585-unsigned-caserange.c
new file mode 100644
index 0000000..adf825c
--- /dev/null
+++ b/test/CodeGen/rdr-6098585-unsigned-caserange.c
@@ -0,0 +1,12 @@
+// RUN: clang --emit-llvm-bc -o - %s | opt -std-compile-opts | llvm-dis > %t &&
+// RUN: grep "ret i32" %t | count 1 &&
+// RUN: grep "ret i32 3" %t | count 1
+
+int f2(unsigned x) {
+ switch(x) {
+ default:
+ return 3;
+ case 0xFFFFFFFF ... 1: // This range should be empty because x is unsigned.
+ return 0;
+ }
+}