Skip to content

Commit 2962f9d

Browse files
committed
stop llvm-reduce from introducing undefs
Differential Revision: https://reviews.llvm.org/D128317
1 parent 1dd2c93 commit 2962f9d

22 files changed

+75
-61
lines changed

llvm/test/tools/llvm-reduce/operands-to-args.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
; INTERESTING: store i32 %[[KEEP]], i32* @GlobalKeep, align 4
2121

2222
; INTERESTING-LABEL: define void @func_caller() {
23-
; REDUCED: call void @func(i32 21, i32* undef, i32* undef, float* undef)
23+
; REDUCED: call void @func(i32 21, i32* null, i32* null, float* null)
2424

2525

2626
@Global = global i32 42

llvm/test/tools/llvm-reduce/remove-all-of-multiple-args.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ define i32 @t(i32 %a0, i32 %a1, i32 %a2) {
66
; CHECK-FINAL: () {
77
;
88
; CHECK-INTERESTINGNESS: ret i32
9-
; CHECK-FINAL: ret i32 undef
9+
; CHECK-FINAL: ret i32 0
1010

1111
ret i32 %a1
1212
}

llvm/test/tools/llvm-reduce/remove-args-used-by-ret.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ define i32 @t(i32 %a0, i32 %a1, i32 %a2) {
88
; CHECK-FINAL-NOT: %a1
99
;
1010
; CHECK-INTERESTINGNESS: ret i32
11-
; CHECK-FINAL: ret i32 undef
11+
; CHECK-FINAL: ret i32 0
1212

1313
ret i32 %a1
1414
}

llvm/test/tools/llvm-reduce/remove-bbs-ret-nonvoid.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ define i32 @main(i1 %c) {
1313
; CHECK-NEXT: br label %interesting2
1414

1515
; CHECK-LABEL: interesting2:
16-
; CHECK-NEXT: ret i32 undef
16+
; CHECK-NEXT: ret i32 0
1717

1818
interesting:
1919
br label %interesting2

llvm/test/tools/llvm-reduce/remove-bbs-unwinded-to.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ bb1:
2525
; CHECK-ALL: bb3:
2626
bb3:
2727
; CHECK-INTERESTINGNESS: call void @did_not_throw(i32
28-
; CHECK-FINAL: call void @did_not_throw(i32 undef)
28+
; CHECK-FINAL: call void @did_not_throw(i32 0)
2929
; CHECK-ALL: br label %bb4
3030
call void @did_not_throw(i32 %i0)
3131
br label %bb4

llvm/test/tools/llvm-reduce/remove-global-vars.ll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
; Test that llvm-reduce can remove uninteresting Global Variables as well as
2-
; their direct uses (which in turn are replaced with 'undef').
2+
; their direct uses (which in turn are replaced with '0').
33

44
; RUN: llvm-reduce --delta-passes=global-variables,global-initializers --test FileCheck --test-arg --check-prefixes=CHECK-ALL,CHECK-INTERESTINGNESS --test-arg %s --test-arg --input-file %s -o %t
55
; RUN: cat %t | FileCheck --check-prefixes=CHECK-ALL,CHECK-FINAL --implicit-check-not=uninteresting %s
@@ -31,11 +31,11 @@ entry:
3131
%0 = load i32, i32* @uninteresting, align 4
3232

3333
; CHECK-INTERESTINGNESS: store i32 {{.*}}, i32* @interesting, align 4
34-
; CHECK-FINAL: store i32 undef, i32* @interesting, align 4
34+
; CHECK-FINAL: store i32 0, i32* @interesting, align 4
3535
store i32 %0, i32* @interesting, align 4
3636

3737
; CHECK-INTERESTINGNESS: store i32 {{.*}}, i32* @interesting3, align 4
38-
; CHECK-FINAL: store i32 undef, i32* @interesting3, align 4
38+
; CHECK-FINAL: store i32 0, i32* @interesting3, align 4
3939
store i32 %0, i32* @interesting3, align 4
4040

4141
; CHECK-ALL: load i32, i32* @interesting, align 4

llvm/test/tools/llvm-reduce/remove-operands-fp.ll

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
; Test that llvm-reduce can reduce floating point operands
22
;
3-
; RUN: llvm-reduce --abort-on-invalid-reduction --delta-passes=operands-undef --test FileCheck --test-arg --check-prefixes=CHECK-INTERESTINGNESS --test-arg %s --test-arg --input-file %s -o %t
4-
; RUN: FileCheck --check-prefixes=CHECK,UNDEF %s < %t
5-
63
; RUN: llvm-reduce --abort-on-invalid-reduction --delta-passes=operands-one --test FileCheck --test-arg --check-prefixes=CHECK-INTERESTINGNESS --test-arg %s --test-arg --input-file %s -o %t
74
; RUN: FileCheck --check-prefixes=CHECK,ONE %s < %t
85

@@ -28,19 +25,6 @@
2825

2926
; CHECK-LABEL: define void @foo(
3027

31-
; UNDEF: %fadd0 = fadd float %arg0, undef
32-
; UNDEF: %fadd1 = fadd float undef, undef
33-
; UNDEF: %fadd2 = fadd float undef, 0.000000e+00
34-
; UNDEF: %fadd3 = fadd float undef, 1.000000e+00
35-
; UNDEF: %fadd4 = fadd float undef, 0x7FF8000000000000
36-
; UNDEF: %fadd5 = fadd float undef, undef
37-
; UNDEF: %fadd6 = fadd <2 x float> %arg2, undef
38-
; UNDEF: %fadd7 = fadd <2 x float> undef, undef
39-
; UNDEF: %fadd8 = fadd <2 x float> undef, zeroinitializer
40-
; UNDEF: %fadd9 = fadd <2 x float> undef, <float 1.000000e+00, float 1.000000e+00>
41-
; UNDEF: %fadd10 = fadd <2 x float> undef, undef
42-
; UNDEF: %fadd11 = fadd <2 x float> undef, <float 0x7FF8000000000000, float 0x7FF8000000000000>
43-
4428

4529
; ONE: %fadd0 = fadd float %arg0, 1.000000e+00
4630
; ONE: %fadd1 = fadd float 1.000000e+00, 1.000000e+00

llvm/test/tools/llvm-reduce/remove-operands.ll

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
; Test that llvm-reduce can reduce operands
22
;
3-
; RUN: llvm-reduce --abort-on-invalid-reduction --delta-passes=operands-undef --test FileCheck --test-arg --check-prefixes=CHECK-INTERESTINGNESS --test-arg %s --test-arg --input-file %s -o %t
4-
; RUN: cat %t | FileCheck %s --check-prefixes=CHECK,UNDEF
53
; RUN: llvm-reduce --abort-on-invalid-reduction --delta-passes=operands-one --test FileCheck --test-arg --check-prefixes=CHECK-INTERESTINGNESS --test-arg %s --test-arg --input-file %s -o %t
64
; RUN: cat %t | FileCheck %s --check-prefixes=CHECK,ONE
75
; RUN: llvm-reduce --abort-on-invalid-reduction --delta-passes=operands-zero --test FileCheck --test-arg --check-prefixes=CHECK-INTERESTINGNESS --test-arg %s --test-arg --input-file %s -o %t
@@ -24,10 +22,6 @@ declare void @llvm.foo(metadata)
2422
define i32 @main(%t* %a, i32 %a2) {
2523

2624
; CHECK-LABEL: lb1:
27-
; UNDEF: inttoptr i16 0
28-
; UNDEF: inttoptr i16 1
29-
; UNDEF: inttoptr i16 2
30-
; UNDEF: inttoptr i16 undef
3125
; ONE: inttoptr i16 0
3226
; ONE: inttoptr i16 1
3327
; ONE: inttoptr i16 1
@@ -47,7 +41,6 @@ lb1:
4741
br label %lb2
4842

4943
; CHECK-LABEL: lb2:
50-
; UNDEF: ret i32 undef
5144
; ONE: ret i32 1
5245
; ZERO: ret i32 0
5346
lb2:

llvm/tools/llvm-reduce/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ add_llvm_tool(llvm-reduce
2121
ReducerWorkItem.cpp
2222
TestRunner.cpp
2323
deltas/Delta.cpp
24+
deltas/Utils.cpp
2425
deltas/ReduceAliases.cpp
2526
deltas/ReduceArguments.cpp
2627
deltas/ReduceAttributes.cpp

llvm/tools/llvm-reduce/DeltaManager.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ static cl::opt<std::string>
6666
DELTA_PASS("simplify-instructions", simplifyInstructionsDeltaPass) \
6767
DELTA_PASS("operands-zero", reduceOperandsZeroDeltaPass) \
6868
DELTA_PASS("operands-one", reduceOperandsOneDeltaPass) \
69-
DELTA_PASS("operands-undef", reduceOperandsUndefDeltaPass) \
7069
DELTA_PASS("operands-to-args", reduceOperandsToArgsDeltaPass) \
7170
DELTA_PASS("operands-skip", reduceOperandsSkipDeltaPass) \
7271
DELTA_PASS("operand-bundles", reduceOperandBundesDeltaPass) \

llvm/tools/llvm-reduce/deltas/ReduceArguments.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#include "ReduceArguments.h"
1515
#include "Delta.h"
16+
#include "Utils.h"
1617
#include "llvm/ADT/SmallVector.h"
1718
#include "llvm/IR/Constants.h"
1819
#include "llvm/IR/Instructions.h"
@@ -79,7 +80,7 @@ static void extractArgumentsFromModule(Oracle &O, Module &Program) {
7980
if (!ArgsToKeep.count(&A)) {
8081
// By adding undesired arguments to the VMap, CloneFunction will remove
8182
// them from the resulting Function
82-
VMap[&A] = UndefValue::get(A.getType());
83+
VMap[&A] = getDefaultValue(A.getType());
8384
for (auto *U : A.users())
8485
if (auto *I = dyn_cast<Instruction>(*&U))
8586
InstToDelete.push_back(I);
@@ -89,7 +90,7 @@ static void extractArgumentsFromModule(Oracle &O, Module &Program) {
8990
if (!V)
9091
continue;
9192
auto *I = cast<Instruction>(V);
92-
I->replaceAllUsesWith(UndefValue::get(I->getType()));
93+
I->replaceAllUsesWith(getDefaultValue(I->getType()));
9394
if (!I->isTerminator())
9495
I->eraseFromParent();
9596
}

llvm/tools/llvm-reduce/deltas/ReduceBasicBlocks.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
//===----------------------------------------------------------------------===//
1313

1414
#include "ReduceBasicBlocks.h"
15+
#include "Utils.h"
1516
#include "llvm/ADT/DenseSet.h"
1617
#include "llvm/IR/BasicBlock.h"
1718
#include "llvm/IR/Constants.h"
@@ -43,7 +44,7 @@ static void replaceBranchTerminator(BasicBlock &BB,
4344
if (auto *IndBI = dyn_cast<IndirectBrInst>(Term))
4445
Address = IndBI->getAddress();
4546

46-
Term->replaceAllUsesWith(UndefValue::get(Term->getType()));
47+
Term->replaceAllUsesWith(getDefaultValue(Term->getType()));
4748
Term->eraseFromParent();
4849

4950
if (ChunkSuccessors.empty()) {
@@ -62,7 +63,7 @@ static void replaceBranchTerminator(BasicBlock &BB,
6263
// If that fails then resort to replacing with a ret.
6364
auto *FnRetTy = BB.getParent()->getReturnType();
6465
ReturnInst::Create(BB.getContext(),
65-
FnRetTy->isVoidTy() ? nullptr : UndefValue::get(FnRetTy),
66+
FnRetTy->isVoidTy() ? nullptr : getDefaultValue(FnRetTy),
6667
&BB);
6768
return;
6869
}
@@ -87,7 +88,7 @@ removeUninterestingBBsFromSwitch(SwitchInst &SwInst,
8788
if (!BBsToKeep.count(SwInst.getDefaultDest())) {
8889
auto *FnRetTy = SwInst.getParent()->getParent()->getReturnType();
8990
ReturnInst::Create(SwInst.getContext(),
90-
FnRetTy->isVoidTy() ? nullptr : UndefValue::get(FnRetTy),
91+
FnRetTy->isVoidTy() ? nullptr : getDefaultValue(FnRetTy),
9192
SwInst.getParent());
9293
SwInst.eraseFromParent();
9394
} else
@@ -133,7 +134,7 @@ static void extractBasicBlocksFromModule(Oracle &O, Module &Program) {
133134
for (auto &BB : BBsToDelete) {
134135
// Instructions might be referenced in other BBs
135136
for (auto &I : *BB)
136-
I.replaceAllUsesWith(UndefValue::get(I.getType()));
137+
I.replaceAllUsesWith(getDefaultValue(I.getType()));
137138
if (BB->getParent()->size() == 1) {
138139
// this is the last basic block of the function, thus we must also make
139140
// sure to remove comdat and set linkage to external

llvm/tools/llvm-reduce/deltas/ReduceFunctions.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include "ReduceFunctions.h"
1616
#include "Delta.h"
17+
#include "Utils.h"
1718
#include "llvm/ADT/STLExtras.h"
1819
#include "llvm/IR/Constants.h"
1920
#include "llvm/IR/Instructions.h"
@@ -42,8 +43,8 @@ static void extractFunctionsFromModule(Oracle &O, Module &Program) {
4243

4344
// And finally, we can actually delete them.
4445
for (Function &F : FuncsToRemove) {
45-
// Replace all *still* remaining uses with undef.
46-
F.replaceAllUsesWith(UndefValue::get(F.getType()));
46+
// Replace all *still* remaining uses with the default value.
47+
F.replaceAllUsesWith(getDefaultValue(F.getType()));
4748
// And finally, fully drop it.
4849
F.eraseFromParent();
4950
}

llvm/tools/llvm-reduce/deltas/ReduceGlobalVars.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
//===----------------------------------------------------------------------===//
1313

1414
#include "ReduceGlobalVars.h"
15+
#include "Utils.h"
1516
#include "llvm/IR/Constants.h"
1617
#include <set>
1718

@@ -40,7 +41,7 @@ static void extractGVsFromModule(Oracle &O, Module &Program) {
4041
if (auto *Inst = dyn_cast<Instruction>(U))
4142
InstToRemove.push_back(Inst);
4243

43-
GV.replaceAllUsesWith(UndefValue::get(GV.getType()));
44+
GV.replaceAllUsesWith(getDefaultValue(GV.getType()));
4445
ToRemove.push_back(&GV);
4546
}
4647

@@ -49,7 +50,7 @@ static void extractGVsFromModule(Oracle &O, Module &Program) {
4950
if (!V)
5051
continue;
5152
auto *Inst = cast<Instruction>(V);
52-
Inst->replaceAllUsesWith(UndefValue::get(Inst->getType()));
53+
Inst->replaceAllUsesWith(getDefaultValue(Inst->getType()));
5354
Inst->eraseFromParent();
5455
}
5556

llvm/tools/llvm-reduce/deltas/ReduceInstructions.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
//===----------------------------------------------------------------------===//
1313

1414
#include "ReduceInstructions.h"
15+
#include "Utils.h"
1516
#include "llvm/IR/Constants.h"
1617

1718
using namespace llvm;
@@ -42,7 +43,7 @@ static void extractInstrFromModule(Oracle &O, Module &Program) {
4243
for (auto &BB : F)
4344
for (auto &Inst : BB)
4445
if (!InstToKeep.count(&Inst)) {
45-
Inst.replaceAllUsesWith(UndefValue::get(Inst.getType()));
46+
Inst.replaceAllUsesWith(getDefaultValue(Inst.getType()));
4647
InstToDelete.push_back(&Inst);
4748
}
4849

llvm/tools/llvm-reduce/deltas/ReduceOperands.cpp

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,6 @@ static bool shouldReduceOperand(Use &Op) {
6262
return true;
6363
}
6464

65-
void llvm::reduceOperandsUndefDeltaPass(TestRunner &Test) {
66-
errs() << "*** Reducing Operands to undef...\n";
67-
auto ReduceValue = [](Use &Op) -> Value * {
68-
if (!shouldReduceOperand(Op))
69-
return nullptr;
70-
// Don't replace existing ConstantData Uses.
71-
return isa<ConstantData>(*Op) ? nullptr : UndefValue::get(Op->getType());
72-
};
73-
runDeltaPass(Test, [ReduceValue](Oracle &O, Module &Program) {
74-
extractOperandsFromModule(O, Program, ReduceValue);
75-
});
76-
}
77-
7865
void llvm::reduceOperandsOneDeltaPass(TestRunner &Test) {
7966
errs() << "*** Reducing Operands to one...\n";
8067
auto ReduceValue = [](Use &Op) -> Value * {

llvm/tools/llvm-reduce/deltas/ReduceOperands.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#include "Delta.h"
1313

1414
namespace llvm {
15-
void reduceOperandsUndefDeltaPass(TestRunner &Test);
1615
void reduceOperandsOneDeltaPass(TestRunner &Test);
1716
void reduceOperandsZeroDeltaPass(TestRunner &Test);
1817
} // namespace llvm

llvm/tools/llvm-reduce/deltas/ReduceOperandsSkip.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ static bool shouldReduceOperand(Use &Op) {
6161
static int classifyReductivePower(Value *V) {
6262
if (auto *C = dyn_cast<ConstantData>(V)) {
6363
if (isa<UndefValue>(V))
64-
return 4;
64+
return -2;
6565
if (C->isNullValue())
6666
return 7;
6767
if (C->isOneValue())

llvm/tools/llvm-reduce/deltas/ReduceOperandsToArgs.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "ReduceOperandsToArgs.h"
1010
#include "Delta.h"
11+
#include "Utils.h"
1112
#include "llvm/ADT/Sequence.h"
1213
#include "llvm/IR/Constants.h"
1314
#include "llvm/IR/InstIterator.h"
@@ -67,10 +68,10 @@ static void replaceFunctionCalls(Function *OldF, Function *NewF) {
6768
// Call arguments for NewF.
6869
SmallVector<Value *> Args(NewF->arg_size(), nullptr);
6970

70-
// Fill up the additional parameters with undef values.
71+
// Fill up the additional parameters with default values.
7172
for (auto ArgIdx : llvm::seq<size_t>(OldF->arg_size(), NewF->arg_size())) {
7273
Type *NewArgTy = NewF->getArg(ArgIdx)->getType();
73-
Args[ArgIdx] = UndefValue::get(NewArgTy);
74+
Args[ArgIdx] = getDefaultValue(NewArgTy);
7475
}
7576

7677
for (CallBase *CI : Callers) {

llvm/tools/llvm-reduce/deltas/ReduceSpecialGlobals.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "ReduceSpecialGlobals.h"
1818
#include "Delta.h"
19+
#include "Utils.h"
1920
#include "llvm/ADT/StringRef.h"
2021
#include "llvm/IR/Constants.h"
2122
#include "llvm/IR/GlobalValue.h"
@@ -29,7 +30,7 @@ static StringRef SpecialGlobalNames[] = {"llvm.used", "llvm.compiler.used"};
2930
static void extractSpecialGlobalsFromModule(Oracle &O, Module &Program) {
3031
for (StringRef Name : SpecialGlobalNames) {
3132
if (auto *Used = Program.getNamedGlobal(Name)) {
32-
Used->replaceAllUsesWith(UndefValue::get(Used->getType()));
33+
Used->replaceAllUsesWith(getDefaultValue(Used->getType()));
3334
Used->eraseFromParent();
3435
}
3536
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===- Utils.cpp - llvm-reduce utility functions --------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This file contains some utility functions supporting llvm-reduce.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "Utils.h"
14+
#include "llvm/IR/Constants.h"
15+
16+
using namespace llvm;
17+
18+
Value *llvm::getDefaultValue(Type *T) {
19+
return T->isVoidTy() ? PoisonValue::get(T) : Constant::getNullValue(T);
20+
}

llvm/tools/llvm-reduce/deltas/Utils.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//===- Utils.h - llvm-reduce utility functions ----------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This file contains some utility functions supporting llvm-reduce.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef LLVM_TOOLS_LLVM_REDUCE_DELTAS_UTILS_H
14+
#define LLVM_TOOLS_LLVM_REDUCE_DELTAS_UTILS_H
15+
16+
#include "llvm/IR/Value.h"
17+
18+
namespace llvm {
19+
20+
Value *getDefaultValue(Type *T);
21+
22+
} // namespace llvm
23+
24+
#endif

0 commit comments

Comments
 (0)