diff --git a/llvm/test/tools/llvm-reduce/remove-global-vars-terminator.ll b/llvm/test/tools/llvm-reduce/remove-global-vars-terminator.ll new file mode 100644 --- /dev/null +++ b/llvm/test/tools/llvm-reduce/remove-global-vars-terminator.ll @@ -0,0 +1,21 @@ +; Test that llvm-reduce does not create invalid IR when the global-variables +; delta pass processes a global variable that is used by a basic block +; terminator + +; RUN: llvm-reduce --abort-on-invalid-reduction --delta-passes=global-variables --test FileCheck --test-arg --check-prefixes=CHECK-ALL,CHECK-INTERESTINGNESS --test-arg %s --test-arg --input-file %s -o %t +; RUN: cat %t | FileCheck --check-prefixes=CHECK-ALL,CHECK-FINAL --implicit-check-not=uninteresting %s + +; CHECK-INTERESTINGNESS: @interesting = {{.*}}global i32{{.*}}, align 4 + +; CHECK-FINAL: @interesting = global i32 0, align 4 +@interesting = global i32 0, align 4 + +@uninteresting = global i32 1, align 4 + +define i32* @foo() { +entry: + ; CHECK-ALL: store i32 5, i32* @interesting, align 4 + store i32 5, i32* @interesting, align 4 + + ret i32* @uninteresting +} diff --git a/llvm/tools/llvm-reduce/deltas/ReduceGlobalVars.cpp b/llvm/tools/llvm-reduce/deltas/ReduceGlobalVars.cpp --- a/llvm/tools/llvm-reduce/deltas/ReduceGlobalVars.cpp +++ b/llvm/tools/llvm-reduce/deltas/ReduceGlobalVars.cpp @@ -38,7 +38,8 @@ if (!GVsToKeep.count(&GV)) { for (auto *U : GV.users()) if (auto *Inst = dyn_cast(U)) - InstToRemove.push_back(Inst); + if (!Inst->isTerminator()) + InstToRemove.push_back(Inst); GV.replaceAllUsesWith(UndefValue::get(GV.getType())); ToRemove.push_back(&GV);