202 changes: 202 additions & 0 deletions llvm/test/tools/llvm-reduce/reduce-flags.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
; RUN: llvm-reduce --abort-on-invalid-reduction --delta-passes=instruction-flags --test FileCheck --test-arg --check-prefixes=INTERESTING,CHECK --test-arg %s --test-arg --input-file %s -o %t
; RUN: FileCheck -check-prefixes=RESULT,CHECK %s < %t

; CHECK-LABEL: @add_nuw_nsw_none(
; INTERESTING: = add
; RESULT: add i32
define i32 @add_nuw_nsw_none(i32 %a, i32 %b) {
%op = add nuw nsw i32 %a, %b
ret i32 %op
}

; CHECK-LABEL: @add_nuw_nsw_keep_nuw(
; INTERESTING: nuw
; RESULT: add nuw i32
define i32 @add_nuw_nsw_keep_nuw(i32 %a, i32 %b) {
%op = add nuw nsw i32 %a, %b
ret i32 %op
}

; CHECK-LABEL: @add_nuw_nsw_keep_nsw(
; INTERESTING: nuw
; RESULT: add nuw i32
define i32 @add_nuw_nsw_keep_nsw(i32 %a, i32 %b) {
%op = add nuw nsw i32 %a, %b
ret i32 %op
}

; CHECK-LABEL: @add_nuw_keep_nuw(
; INTERESTING: nuw
; RESULT: add nuw i32
define i32 @add_nuw_keep_nuw(i32 %a, i32 %b) {
%op = add nuw i32 %a, %b
ret i32 %op
}

; CHECK-LABEL: @add_nsw_keep_nsw(
; INTERESTING: nsw
; RESULT: add nsw i32
define i32 @add_nsw_keep_nsw(i32 %a, i32 %b) {
%op = add nsw i32 %a, %b
ret i32 %op
}

; CHECK-LABEL: @ashr_exact_drop(
; INTERESTING: = ashr
; RESULT: ashr i32
define i32 @ashr_exact_drop(i32 %a, i32 %b) {
%op = ashr exact i32 %a, %b
ret i32 %op
}

; CHECK-LABEL: @ashr_exact_keep(
; INTERESTING: exact
; RESULT: ashr exact i32
define i32 @ashr_exact_keep(i32 %a, i32 %b) {
%op = ashr exact i32 %a, %b
ret i32 %op
}

; CHECK-LABEL: @getelementptr_inbounds_drop(
; INTERESTING: getelementptr
; RESULT: getelementptr i32, ptr %a, i64 %b
define ptr @getelementptr_inbounds_drop(ptr %a, i64 %b) {
%op = getelementptr inbounds i32, ptr %a, i64 %b
ret ptr %op
}

; CHECK-LABEL: @getelementptr_inbounds_keep(
; INTERESTING: inbounds
; RESULT: getelementptr inbounds i32, ptr %a, i64 %b
define ptr @getelementptr_inbounds_keep(ptr %a, i64 %b) {
%op = getelementptr inbounds i32, ptr %a, i64 %b
ret ptr %op
}

; CHECK-LABEL: @fadd_reassoc_none(
; INTERESTING: = fadd
; RESULT: fadd float
define float @fadd_reassoc_none(float %a, float %b) {
%op = fadd reassoc float %a, %b
ret float %op
}

; CHECK-LABEL: @fadd_reassoc_keep(
; INTERESTING: fadd reassoc
; RESULT: fadd reassoc float
define float @fadd_reassoc_keep(float %a, float %b) {
%op = fadd reassoc float %a, %b
ret float %op
}

; CHECK-LABEL: @fadd_nnan_none(
; INTERESTING: = fadd
; RESULT: fadd float
define float @fadd_nnan_none(float %a, float %b) {
%op = fadd nnan float %a, %b
ret float %op
}

; CHECK-LABEL: @fadd_nnan_keep(
; INTERESTING: fadd nnan
; RESULT: fadd nnan float
define float @fadd_nnan_keep(float %a, float %b) {
%op = fadd nnan float %a, %b
ret float %op
}

; CHECK-LABEL: @fadd_ninf_none(
; INTERESTING: = fadd
; RESULT: fadd float
define float @fadd_ninf_none(float %a, float %b) {
%op = fadd ninf float %a, %b
ret float %op
}

; CHECK-LABEL: @fadd_ninf_keep(
; INTERESTING: fadd ninf
; RESULT: fadd ninf float
define float @fadd_ninf_keep(float %a, float %b) {
%op = fadd ninf float %a, %b
ret float %op
}

; CHECK-LABEL: @fadd_nsz_none(
; INTERESTING: = fadd
; RESULT: fadd float
define float @fadd_nsz_none(float %a, float %b) {
%op = fadd nsz float %a, %b
ret float %op
}

; CHECK-LABEL: @fadd_nsz_keep(
; INTERESTING: fadd nsz
; RESULT: fadd nsz float
define float @fadd_nsz_keep(float %a, float %b) {
%op = fadd nsz float %a, %b
ret float %op
}

; CHECK-LABEL: @fadd_arcp_none(
; INTERESTING: = fadd
; RESULT: fadd float
define float @fadd_arcp_none(float %a, float %b) {
%op = fadd arcp float %a, %b
ret float %op
}

; CHECK-LABEL: @fadd_arcp_keep(
; INTERESTING: fadd arcp
; RESULT: fadd arcp float
define float @fadd_arcp_keep(float %a, float %b) {
%op = fadd arcp float %a, %b
ret float %op
}

; CHECK-LABEL: @fadd_contract_none(
; INTERESTING: = fadd
; RESULT: fadd float
define float @fadd_contract_none(float %a, float %b) {
%op = fadd contract float %a, %b
ret float %op
}

; CHECK-LABEL: @fadd_contract_keep(
; INTERESTING: fadd contract
; RESULT: fadd contract float
define float @fadd_contract_keep(float %a, float %b) {
%op = fadd contract float %a, %b
ret float %op
}

; CHECK-LABEL: @fadd_afn_none(
; INTERESTING: = fadd
; RESULT: fadd float
define float @fadd_afn_none(float %a, float %b) {
%op = fadd afn float %a, %b
ret float %op
}

; CHECK-LABEL: @fadd_afn_keep(
; INTERESTING: fadd afn
; RESULT: fadd afn float
define float @fadd_afn_keep(float %a, float %b) {
%op = fadd afn float %a, %b
ret float %op
}

; CHECK-LABEL: @fadd_fast_none(
; INTERESTING: = fadd
; RESULT: fadd float
define float @fadd_fast_none(float %a, float %b) {
%op = fadd fast float %a, %b
ret float %op
}

; CHECK-LABEL: @fadd_nnan_ninf_keep_nnan(
; INTERESTING: nnan
; RESULT: fadd nnan float
define float @fadd_nnan_ninf_keep_nnan(float %a, float %b) {
%op = fadd nnan ninf float %a, %b
ret float %op
}
42 changes: 21 additions & 21 deletions llvm/test/tools/llvm-reduce/reduce-opcodes.ll
Original file line number Diff line number Diff line change
@@ -1,120 +1,120 @@
; RUN: llvm-reduce --abort-on-invalid-reduction --delta-passes=opcodes --test FileCheck --test-arg --check-prefixes=CHECK-INTERESTINGNESS --test-arg %s --test-arg --input-file %s -o %t
; RUN: FileCheck -check-prefix=RESULT %s < %t
; RUN: llvm-reduce --abort-on-invalid-reduction --delta-passes=opcodes --test FileCheck --test-arg %s --test-arg --input-file %s -o %t
; RUN: FileCheck -check-prefixes=CHECK,RESULT %s < %t

; CHECK-INTERESTINGNESS: @fdiv_fast(
; CHECK-LABEL: @fdiv_fast(
; RESULT: %op = fmul fast float %a, %b, !dbg !7, !fpmath !13
define float @fdiv_fast(float %a, float %b) {
%op = fdiv fast float %a, %b, !dbg !7, !fpmath !13
ret float %op
}

; CHECK-INTERESTINGNESS: @frem_nnan(
; CHECK-LABEL: @frem_nnan(
; RESULT: %op = fmul nnan float %a, %b, !dbg !7, !fpmath !13
define float @frem_nnan(float %a, float %b) {
%op = frem nnan float %a, %b, !dbg !7, !fpmath !13
ret float %op
}

; CHECK-INTERESTINGNESS: @udiv(
; CHECK-LABEL: @udiv(
; RESULT: %op = mul i32 %a, %b, !dbg !7
define i32 @udiv(i32 %a, i32 %b) {
%op = udiv i32 %a, %b, !dbg !7
ret i32 %op
}

; CHECK-INTERESTINGNESS: @udiv_vec(
; CHECK-LABEL: @udiv_vec(
; RESULT: %op = mul <2 x i32> %a, %b, !dbg !7
define <2 x i32> @udiv_vec(<2 x i32> %a, <2 x i32> %b) {
%op = udiv <2 x i32> %a, %b, !dbg !7
ret <2 x i32> %op
}

; CHECK-INTERESTINGNESS: @sdiv(
; CHECK-LABEL: @sdiv(
; RESULT: %op = mul i32 %a, %b{{$}}
define i32 @sdiv(i32 %a, i32 %b) {
%op = sdiv i32 %a, %b
ret i32 %op
}

; CHECK-INTERESTINGNESS: @sdiv_exact(
; CHECK-LABEL: @sdiv_exact(
; RESULT: %op = mul i32 %a, %b, !dbg !7
define i32 @sdiv_exact(i32 %a, i32 %b) {
%op = sdiv exact i32 %a, %b, !dbg !7
ret i32 %op
}

; CHECK-INTERESTINGNESS: @urem(
; CHECK-LABEL: @urem(
; RESULT: %op = mul i32 %a, %b, !dbg !7
define i32 @urem(i32 %a, i32 %b) {
%op = urem i32 %a, %b, !dbg !7
ret i32 %op
}

; CHECK-INTERESTINGNESS: @srem(
; CHECK-LABEL: @srem(
; RESULT: %op = mul i32 %a, %b, !dbg !7
define i32 @srem(i32 %a, i32 %b) {
%op = srem i32 %a, %b, !dbg !7
ret i32 %op
}

; Make sure there's no crash if the IRBuilder decided to constant fold something
; CHECK-INTERESTINGNESS: @add_constant_fold(
; CHECK-LABEL: @add_constant_fold(
; RESULT: %op = add i32 0, 0, !dbg !7
define i32 @add_constant_fold() {
%op = add i32 0, 0, !dbg !7
ret i32 %op
}

; CHECK-INTERESTINGNESS: @add(
; CHECK-LABEL: @add(
; RESULT: %op = or i32 %a, %b, !dbg !7
define i32 @add(i32 %a, i32 %b) {
%op = add i32 %a, %b, !dbg !7
ret i32 %op
}

; CHECK-INTERESTINGNESS: @add_nuw(
; CHECK-LABEL: @add_nuw(
; RESULT: %op = or i32 %a, %b, !dbg !7
define i32 @add_nuw(i32 %a, i32 %b) {
%op = add nuw i32 %a, %b, !dbg !7
ret i32 %op
}

; CHECK-INTERESTINGNESS: @add_nsw(
; CHECK-LABEL: @add_nsw(
; RESULT: %op = or i32 %a, %b, !dbg !7
define i32 @add_nsw(i32 %a, i32 %b) {
%op = add nsw i32 %a, %b, !dbg !7
ret i32 %op
}

; CHECK-INTERESTINGNESS: @sub_nuw_nsw(
; CHECK-LABEL: @sub_nuw_nsw(
; RESULT: %op = or i32 %a, %b, !dbg !7
define i32 @sub_nuw_nsw(i32 %a, i32 %b) {
%op = sub nuw nsw i32 %a, %b, !dbg !7
ret i32 %op
}

; CHECK-INTERESTINGNESS: @workitem_id_y(
; CHECK-LABEL: @workitem_id_y(
; RESULT: %id = call i32 @llvm.amdgcn.workitem.id.x(), !dbg !7
define i32 @workitem_id_y() {
%id = call i32 @llvm.amdgcn.workitem.id.y(), !dbg !7
ret i32 %id
}

; CHECK-INTERESTINGNESS: @workitem_id_z(
; CHECK-LABEL: @workitem_id_z(
; RESULT: %id = call i32 @llvm.amdgcn.workitem.id.x(), !dbg !7
define i32 @workitem_id_z() {
%id = call i32 @llvm.amdgcn.workitem.id.z(), !dbg !7
ret i32 %id
}

; CHECK-INTERESTINGNESS: @workgroup_id_y(
; CHECK-LABEL: @workgroup_id_y(
; RESULT: %id = call i32 @llvm.amdgcn.workgroup.id.x(), !dbg !7
define i32 @workgroup_id_y() {
%id = call i32 @llvm.amdgcn.workgroup.id.y(), !dbg !7
ret i32 %id
}

; CHECK-INTERESTINGNESS: @workgroup_id_z(
; CHECK-LABEL: @workgroup_id_z(
; RESULT: %id = call i32 @llvm.amdgcn.workgroup.id.x(), !dbg !7
define i32 @workgroup_id_z() {
%id = call i32 @llvm.amdgcn.workgroup.id.z(), !dbg !7
Expand All @@ -135,14 +135,14 @@ define float @maxnum_nsz(float %a, float %b) {
ret float %op
}

; CHECK-LABEL: @minimum(
; CHECK-LABEL: @minimum_nsz(
; RESULT: %op = fmul nsz float %a, %b, !dbg !7
define float @minimum_nsz(float %a, float %b) {
%op = call nsz float @llvm.minimum.f32(float %a, float %b), !dbg !7
ret float %op
}

; CHECK-LABEL: @maximum(
; CHECK-LABEL: @maximum_nsz(
; RESULT: %op = fmul nsz float %a, %b, !dbg !7
define float @maximum_nsz(float %a, float %b) {
%op = call nsz float @llvm.maximum.f32(float %a, float %b), !dbg !7
Expand Down
135 changes: 135 additions & 0 deletions llvm/test/tools/llvm-reduce/reduce-volatile.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
; RUN: llvm-reduce --abort-on-invalid-reduction --delta-passes=volatile --test FileCheck --test-arg --check-prefixes=INTERESTING,CHECK --test-arg %s --test-arg --input-file %s -o %t
; RUN: FileCheck -check-prefixes=RESULT,CHECK %s < %t

; CHECK-LABEL: @load_volatile_keep(
; INTERESTING: load volatile
; RESULT: %op = load volatile i32,
define i32 @load_volatile_keep(ptr %ptr) {
%op = load volatile i32, ptr %ptr
ret i32 %op
}

; CHECK-LABEL: @load_volatile_drop(
; INTERESTING: load
; RESULT: %op = load i32,
define i32 @load_volatile_drop(ptr %ptr) {
%op = load volatile i32, ptr %ptr
ret i32 %op
}

; CHECK-LABEL: @store_volatile_keep(
; INTERESTING: store volatile
; RESULT: store volatile i32 0,
define void @store_volatile_keep(ptr %ptr) {
store volatile i32 0, ptr %ptr
ret void
}

; CHECK-LABEL: @store_volatile_drop(
; INTERESTING: store
; RESULT: store i32 0,
define void @store_volatile_drop(ptr %ptr) {
store volatile i32 0, ptr %ptr
ret void
}

; CHECK-LABEL: @atomicrmw_volatile_keep(
; INTERESTING: atomicrmw volatile
; RESULT: atomicrmw volatile add ptr %ptr
define i32 @atomicrmw_volatile_keep(ptr %ptr) {
%val = atomicrmw volatile add ptr %ptr, i32 3 seq_cst
ret i32 %val
}

; CHECK-LABEL: @atomicrmw_volatile_drop(
; INTERESTING: atomicrmw
; RESULT: atomicrmw add ptr %ptr
define i32 @atomicrmw_volatile_drop(ptr %ptr) {
%val = atomicrmw volatile add ptr %ptr, i32 3 seq_cst
ret i32 %val
}

; CHECK-LABEL: @cmpxchg_volatile_keep(
; INTERESTING: cmpxchg volatile
; RESULT: cmpxchg volatile ptr %ptr, i32 %old, i32 %in seq_cst seq_cst
define { i32, i1 } @cmpxchg_volatile_keep(ptr %ptr, i32 %old, i32 %in) {
%val = cmpxchg volatile ptr %ptr, i32 %old, i32 %in seq_cst seq_cst
ret { i32, i1 } %val
}

; CHECK-LABEL: @cmpxchg_volatile_drop(
; INTERESTING: cmpxchg
; RESULT: cmpxchg ptr %ptr, i32 %old, i32 %in seq_cst seq_cst
define { i32, i1 } @cmpxchg_volatile_drop(ptr %ptr, i32 %old, i32 %in) {
%val = cmpxchg volatile ptr %ptr, i32 %old, i32 %in seq_cst seq_cst
ret { i32, i1 } %val
}

; CHECK-LABEL: @memcpy_volatile_keep(
; INTERESTING: i1 true
; RESULT: call void @llvm.memcpy.p0.p0.i64(ptr %dst, ptr %src, i64 %size, i1 true)
define void @memcpy_volatile_keep(ptr %dst, ptr %src, i64 %size) {
call void @llvm.memcpy.p0.p0.i64(ptr %dst, ptr %src, i64 %size, i1 true)
ret void
}

; CHECK-LABEL: @memcpy_volatile_drop(
; INTERESTING: llvm.memcpy
; RESULT: call void @llvm.memcpy.p0.p0.i64(ptr %dst, ptr %src, i64 %size, i1 false)
define void @memcpy_volatile_drop(ptr %dst, ptr %src, i64 %size) {
call void @llvm.memcpy.p0.p0.i64(ptr %dst, ptr %src, i64 %size, i1 true)
ret void
}

; CHECK-LABEL: @memcpy_inline_volatile_keep(
; INTERESTING: i1 true
; RESULT: call void @llvm.memcpy.inline.p0.p0.i64(ptr %dst, ptr %src, i64 256, i1 true)
define void @memcpy_inline_volatile_keep(ptr %dst, ptr %src) {
call void @llvm.memcpy.inline.p0.p0.i64(ptr %dst, ptr %src, i64 256, i1 true)
ret void
}

; CHECK-LABEL: @memcpy_inline_volatile_drop(
; INTERESTING: llvm.memcpy
; RESULT: call void @llvm.memcpy.inline.p0.p0.i64(ptr %dst, ptr %src, i64 256, i1 false)
define void @memcpy_inline_volatile_drop(ptr %dst, ptr %src) {
call void @llvm.memcpy.inline.p0.p0.i64(ptr %dst, ptr %src, i64 256, i1 true)
ret void
}

; CHECK-LABEL: @memmove_volatile_keep(
; INTERESTING: i1 true
; RESULT: call void @llvm.memmove.p0.p0.i64(ptr %dst, ptr %src, i64 256, i1 true)
define void @memmove_volatile_keep(ptr %dst, ptr %src) {
call void @llvm.memmove.p0.p0.i64(ptr %dst, ptr %src, i64 256, i1 true)
ret void
}

; CHECK-LABEL: @memmove_volatile_drop(
; INTERESTING: llvm.memmove
; RESULT: call void @llvm.memmove.p0.p0.i64(ptr %dst, ptr %src, i64 %size, i1 false)
define void @memmove_volatile_drop(ptr %dst, ptr %src, i64 %size) {
call void @llvm.memmove.p0.p0.i64(ptr %dst, ptr %src, i64 %size, i1 true)
ret void
}

; CHECK-LABEL: @memset_volatile_keep(
; INTERESTING: i1 true
; RESULT: call void @llvm.memset.p0.i64(ptr %ptr, i8 %val, i64 %size, i1 true)
define void @memset_volatile_keep(ptr %ptr, i8 %val, i64 %size) {
call void @llvm.memset.p0.i64(ptr %ptr, i8 %val, i64 %size, i1 true)
ret void
}

; CHECK-LABEL: @memset_volatile_drop(
; INTERESTING: llvm.memset
; RESULT: call void @llvm.memset.p0.i64(ptr %ptr, i8 %val, i64 %size, i1 false)
define void @memset_volatile_drop(ptr %ptr, i8 %val, i64 %size) {
call void @llvm.memset.p0.i64(ptr %ptr, i8 %val, i64 %size, i1 true)
ret void
}

declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg)
declare void @llvm.memmove.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg)
declare void @llvm.memcpy.inline.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64 immarg, i1 immarg)
declare void @llvm.memset.p0.i64(ptr noalias nocapture readonly, i8, i64, i1 immarg)
2 changes: 1 addition & 1 deletion llvm/test/tools/llvm-reduce/remove-bbs-unreachable.ll
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
; CHECK-INTERESTINGNESS: test

; CHECK: define void @test() {
; CHECK-NEXT: unreachable:
; CHECK-NEXT: entry:
; CHECK-NEXT: ret void

define void @test() {
Expand Down
11 changes: 9 additions & 2 deletions llvm/test/tools/llvm-reduce/remove-bbs.ll
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
; Test that llvm-reduce can remove uninteresting Basic Blocks, and remove them from instructions (i.e. SwitchInst, BranchInst and IndirectBrInst)
; Note: if an uninteresting BB is the default case for a switch, the instruction is removed altogether (since the default case cannot be replaced)
;
; RUN: llvm-reduce -abort-on-invalid-reduction --delta-passes=basic-blocks --test %python --test-arg %p/Inputs/remove-bbs.py %s -o %t
; RUN: llvm-reduce -abort-on-invalid-reduction --delta-passes=basic-blocks --test FileCheck --test-arg --check-prefix=CHECK-INTERESTINGNESS --test-arg %s --test-arg --input-file %s -o %t
; RUN: FileCheck -implicit-check-not=uninteresting %s < %t

; CHECK-INTERESTINGNESS: store i32 0
; CHECK-INTERESTINGNESS: store i32 1
; CHECK-INTERESTINGNESS: store i32 2

define void @main() {
interesting:
store i32 0, ptr null
; CHECK-NOT: switch i32 0, label %uninteresting
switch i32 0, label %uninteresting [
i32 0, label %uninteresting
i32 1, label %interesting2
]

uninteresting:
ret void

interesting2:
store i32 1, ptr null
; CHECK: switch i32 1, label %interesting3
switch i32 1, label %interesting3 [
; CHECK-NOT: i32 0, label %uninteresting
Expand All @@ -24,6 +30,7 @@ interesting2:
]

interesting3:
store i32 2, ptr null
; CHECK: br label %interesting2
br i1 true, label %interesting2, label %uninteresting
}
2 changes: 2 additions & 0 deletions llvm/tools/llvm-reduce/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ add_llvm_tool(llvm-reduce
deltas/ReduceGlobalVarInitializers.cpp
deltas/ReduceGlobalVars.cpp
deltas/ReduceInstructions.cpp
deltas/ReduceInstructionFlags.cpp
deltas/ReduceMetadata.cpp
deltas/ReduceModuleData.cpp
deltas/ReduceMemoryOperations.cpp
deltas/ReduceOperandBundles.cpp
deltas/ReduceOpcodes.cpp
deltas/ReduceSpecialGlobals.cpp
Expand Down
8 changes: 7 additions & 1 deletion llvm/tools/llvm-reduce/DeltaManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@
#include "deltas/ReduceGlobalVarInitializers.h"
#include "deltas/ReduceGlobalVars.h"
#include "deltas/ReduceIRReferences.h"
#include "deltas/ReduceInstructionFlags.h"
#include "deltas/ReduceInstructionFlagsMIR.h"
#include "deltas/ReduceInstructions.h"
#include "deltas/ReduceInstructionsMIR.h"
#include "deltas/ReduceMemoryOperations.h"
#include "deltas/ReduceMetadata.h"
#include "deltas/ReduceModuleData.h"
#include "deltas/ReduceOpcodes.h"
Expand Down Expand Up @@ -93,7 +95,11 @@ static cl::list<std::string>
DELTA_PASS("attributes", reduceAttributesDeltaPass) \
DELTA_PASS("module-data", reduceModuleDataDeltaPass) \
DELTA_PASS("opcodes", reduceOpcodesDeltaPass) \
} while (false)
DELTA_PASS("volatile", reduceVolatileInstructionsDeltaPass) \
DELTA_PASS("atomic-ordering", reduceAtomicOrderingDeltaPass) \
DELTA_PASS("syncscopes", reduceAtomicSyncScopesDeltaPass) \
DELTA_PASS("instruction-flags", reduceInstructionFlagsDeltaPass) \
} while (false)

#define DELTA_PASSES_MIR \
do { \
Expand Down
38 changes: 6 additions & 32 deletions llvm/tools/llvm-reduce/deltas/ReduceBasicBlocks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,44 +102,18 @@ removeUninterestingBBsFromSwitch(SwitchInst &SwInst,
}
}

/// It's OK to add a block to the set of removed blocks if the first
/// basic block in the function that survives all of the deletions is
/// a legal entry block. Keep at least one block in a function.
static bool okToRemove(BasicBlock &Candidate, Function &F,
const DenseSet<BasicBlock *> &BBsToDelete) {
size_t NumBlocksDeleted = 0;
bool FoundNewEntryBlock = false;
for (auto &B : F) {
if (&B == &Candidate)
continue;
if (BBsToDelete.count(&B)) {
++NumBlocksDeleted;
continue;
}
if (!FoundNewEntryBlock) {
/// Ok we've found the first block that's not going to be deleted,
/// it will be the new entry block -- that's only legal if this
/// block has no predecessors among blocks that survive the
/// deletions
for (BasicBlock *Pred : predecessors(&B)) {
if (!BBsToDelete.contains(Pred))
return false;
}
FoundNewEntryBlock = true;
}
}
// Don't delete the last block.
return NumBlocksDeleted + 1 < F.size();
}

/// Removes out-of-chunk arguments from functions, and modifies their calls
/// accordingly. It also removes allocations of out-of-chunk arguments.
static void extractBasicBlocksFromModule(Oracle &O, Module &Program) {
DenseSet<BasicBlock *> BBsToKeep, BBsToDelete;

for (auto &F : Program) {
for (auto &BB : F) {
if (!okToRemove(BB, F, BBsToDelete) || O.shouldKeep())
if (F.empty())
continue;

// Never try to delete the entry block.
for (auto &BB : make_range(++F.begin(), F.end())) {
if (O.shouldKeep())
BBsToKeep.insert(&BB);
else
BBsToDelete.insert(&BB);
Expand Down
66 changes: 66 additions & 0 deletions llvm/tools/llvm-reduce/deltas/ReduceInstructionFlags.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//===- ReduceInstructionFlags.cpp - Specialized Delta Pass ----------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Try to remove optimization flags on instructions
//
//===----------------------------------------------------------------------===//

#include "ReduceInstructionFlags.h"
#include "Delta.h"
#include "llvm/IR/InstIterator.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/Operator.h"

static void reduceFlagsInModule(Oracle &O, Module &Mod) {
for (Function &F : Mod) {
for (Instruction &I : instructions(F)) {
if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(&I)) {
if (OBO->hasNoSignedWrap() && !O.shouldKeep())
I.setHasNoSignedWrap(false);
if (OBO->hasNoUnsignedWrap() && !O.shouldKeep())
I.setHasNoUnsignedWrap(false);
} else if (auto *PE = dyn_cast<PossiblyExactOperator>(&I)) {
if (PE->isExact() && !O.shouldKeep())
I.setIsExact(false);
} else if (auto *GEP = dyn_cast<GetElementPtrInst>(&I)) {
if (GEP->isInBounds() && !O.shouldKeep())
GEP->setIsInBounds(false);
} else if (auto *FPOp = dyn_cast<FPMathOperator>(&I)) {
FastMathFlags Flags = FPOp->getFastMathFlags();

if (Flags.allowReassoc() && !O.shouldKeep())
Flags.setAllowReassoc(false);

if (Flags.noNaNs() && !O.shouldKeep())
Flags.setNoNaNs(false);

if (Flags.noInfs() && !O.shouldKeep())
Flags.setNoInfs(false);

if (Flags.noSignedZeros() && !O.shouldKeep())
Flags.setNoSignedZeros(false);

if (Flags.allowReciprocal() && !O.shouldKeep())
Flags.setAllowReciprocal(false);

if (Flags.allowContract() && !O.shouldKeep())
Flags.setAllowContract(false);

if (Flags.approxFunc() && !O.shouldKeep())
Flags.setApproxFunc(false);

I.copyFastMathFlags(Flags);
}
}
}
}

void llvm::reduceInstructionFlagsDeltaPass(TestRunner &Test) {
runDeltaPass(Test, reduceFlagsInModule, "Reducing Instruction Flags");
}
18 changes: 18 additions & 0 deletions llvm/tools/llvm-reduce/deltas/ReduceInstructionFlags.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//===- ReduceInstructionFlags.h - Specialized Delta Pass --------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_TOOLS_LLVM_REDUCE_DELTAS_REDUCEINSTRUCTIONFLAGS_H
#define LLVM_TOOLS_LLVM_REDUCE_DELTAS_REDUCEINSTRUCTIONFLAGS_H

#include "TestRunner.h"

namespace llvm {
void reduceInstructionFlagsDeltaPass(TestRunner &Test);
} // namespace llvm

#endif
107 changes: 107 additions & 0 deletions llvm/tools/llvm-reduce/deltas/ReduceMemoryOperations.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
//===- ReduceOpcodes.cpp - Specialized Delta Pass -------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "ReduceMemoryOperations.h"
#include "Delta.h"
#include "llvm/IR/InstIterator.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h"

static void removeVolatileInFunction(Oracle &O, Function &F) {
LLVMContext &Ctx = F.getContext();
for (Instruction &I : instructions(F)) {
if (LoadInst *LI = dyn_cast<LoadInst>(&I)) {
if (LI->isVolatile() && !O.shouldKeep())
LI->setVolatile(false);
} else if (StoreInst *SI = dyn_cast<StoreInst>(&I)) {
if (SI->isVolatile() && !O.shouldKeep())
SI->setVolatile(false);
} else if (AtomicRMWInst *RMW = dyn_cast<AtomicRMWInst>(&I)) {
if (RMW->isVolatile() && !O.shouldKeep())
RMW->setVolatile(false);
} else if (AtomicCmpXchgInst *CmpXChg = dyn_cast<AtomicCmpXchgInst>(&I)) {
if (CmpXChg->isVolatile() && !O.shouldKeep())
CmpXChg->setVolatile(false);
} else if (MemIntrinsic *MemIntrin = dyn_cast<MemIntrinsic>(&I)) {
if (MemIntrin->isVolatile() && !O.shouldKeep())
MemIntrin->setVolatile(ConstantInt::getFalse(Ctx));
}
}
}

static void removeVolatileInModule(Oracle &O, Module &Mod) {
for (Function &F : Mod)
removeVolatileInFunction(O, F);
}

void llvm::reduceVolatileInstructionsDeltaPass(TestRunner &Test) {
runDeltaPass(Test, removeVolatileInModule, "Reducing Volatile Instructions");
}

static void reduceAtomicSyncScopesInFunction(Oracle &O, Function &F) {
for (Instruction &I : instructions(F)) {
if (LoadInst *LI = dyn_cast<LoadInst>(&I)) {
if (LI->getSyncScopeID() != SyncScope::System && !O.shouldKeep())
LI->setSyncScopeID(SyncScope::System);
} else if (StoreInst *SI = dyn_cast<StoreInst>(&I)) {
if (SI->getSyncScopeID() != SyncScope::System && !O.shouldKeep())
SI->setSyncScopeID(SyncScope::System);
} else if (AtomicRMWInst *RMW = dyn_cast<AtomicRMWInst>(&I)) {
if (RMW->getSyncScopeID() != SyncScope::System && !O.shouldKeep())
RMW->setSyncScopeID(SyncScope::System);
} else if (AtomicCmpXchgInst *CmpXChg = dyn_cast<AtomicCmpXchgInst>(&I)) {
if (CmpXChg->getSyncScopeID() != SyncScope::System && !O.shouldKeep())
CmpXChg->setSyncScopeID(SyncScope::System);
} else if (FenceInst *Fence = dyn_cast<FenceInst>(&I)) {
if (Fence->getSyncScopeID() != SyncScope::System && !O.shouldKeep())
Fence->setSyncScopeID(SyncScope::System);
}
}
}

static void reduceAtomicSyncScopesInModule(Oracle &O, Module &Mod) {
for (Function &F : Mod)
reduceAtomicSyncScopesInFunction(O, F);
}

void llvm::reduceAtomicSyncScopesDeltaPass(TestRunner &Test) {
runDeltaPass(Test, reduceAtomicSyncScopesInModule,
"Reducing Atomic Sync Scopes");
}

// TODO: Might be helpful to incrementally relax orders
static void reduceAtomicOrderingInFunction(Oracle &O, Function &F) {
for (Instruction &I : instructions(F)) {
if (LoadInst *LI = dyn_cast<LoadInst>(&I)) {
if (LI->getOrdering() != AtomicOrdering::NotAtomic && !O.shouldKeep())
LI->setAtomic(AtomicOrdering::NotAtomic);
} else if (StoreInst *SI = dyn_cast<StoreInst>(&I)) {
if (SI->getOrdering() != AtomicOrdering::NotAtomic && !O.shouldKeep())
SI->setAtomic(AtomicOrdering::NotAtomic);
} else if (AtomicRMWInst *RMW = dyn_cast<AtomicRMWInst>(&I)) {
if (RMW->getOrdering() != AtomicOrdering::Monotonic && !O.shouldKeep())
RMW->setOrdering(AtomicOrdering::Monotonic);
} else if (AtomicCmpXchgInst *CmpXChg = dyn_cast<AtomicCmpXchgInst>(&I)) {
if (CmpXChg->getSuccessOrdering() != AtomicOrdering::Monotonic &&
!O.shouldKeep())
CmpXChg->setSuccessOrdering(AtomicOrdering::Monotonic);
if (CmpXChg->getFailureOrdering() != AtomicOrdering::Monotonic &&
!O.shouldKeep())
CmpXChg->setFailureOrdering(AtomicOrdering::Monotonic);
}
}
}

static void reduceAtomicOrderingInModule(Oracle &O, Module &Mod) {
for (Function &F : Mod)
reduceAtomicOrderingInFunction(O, F);
}

void llvm::reduceAtomicOrderingDeltaPass(TestRunner &Test) {
runDeltaPass(Test, reduceAtomicOrderingInModule, "Reducing Atomic Odering");
}
20 changes: 20 additions & 0 deletions llvm/tools/llvm-reduce/deltas/ReduceMemoryOperations.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===- ReduceMemoryOperations.h - Specialized Delta Pass --------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_TOOLS_LLVM_REDUCE_DELTAS_REDUCEMEMORYOPERATIONS_H
#define LLVM_TOOLS_LLVM_REDUCE_DELTAS_REDUCEMEMORYOPERATIONS_H

#include "TestRunner.h"

namespace llvm {
void reduceVolatileInstructionsDeltaPass(TestRunner &Test);
void reduceAtomicSyncScopesDeltaPass(TestRunner &Test);
void reduceAtomicOrderingDeltaPass(TestRunner &Test);
} // namespace llvm

#endif