Skip to content

llvm-reduce: Skip setting br conditions on already constant branches #133841

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
; RUN: llvm-reduce --abort-on-invalid-reduction --delta-passes=simplify-conditionals-false --test FileCheck --test-arg --check-prefixes=INTERESTING --test-arg %s --test-arg --input-file %s -o %t
; RUN: FileCheck -check-prefixes=RESULT-FALSE,CHECK %s < %t

; RUN: llvm-reduce --abort-on-invalid-reduction --delta-passes=simplify-conditionals-true --test FileCheck --test-arg --check-prefixes=INTERESTING --test-arg %s --test-arg --input-file %s -o %t
; RUN: FileCheck -check-prefixes=RESULT-TRUE,CHECK %s < %t

; Check that simplify-conditionals-true/false do not attempt block
; simplification in cases that happened to already use a constant
; true/false branch. We should not get the side effect of running
; simplifycfg on blocks where we did not change the terminator value,
; and not introduce unreachable code.


; CHECK-LABEL: @br_false(
; RESULT-FALSE: br i1 false, label %will_be_unreachable, label %exit
; RESULT-TRUE: br label %will_be_unreachable
define i1 @br_false(i64 %int) {
entry:
%i2p = inttoptr i64 %int to ptr
br i1 false, label %will_be_unreachable, label %exit

will_be_unreachable:
%load = load ptr, ptr %i2p, align 8
br label %for.body

for.body:
br label %for.body

exit:
ret i1 false
}

; CHECK-LABEL: @br_false_keep_in_unreachable(
; CHECK: entry
; INTERESTING: [[I2P:%.+]] = inttoptr i64 %int to ptr
; INTERESTING: load ptr, ptr [[I2P]]

; RESULT-FALSE: br i1 false, label %will_be_unreachable, label %exit
; RESULT-TRUE: br label %will_be_unreachable
define i1 @br_false_keep_in_unreachable(i64 %int) {
entry:
br i1 false, label %will_be_unreachable, label %exit

will_be_unreachable:
%i2p = inttoptr i64 %int to ptr
%load = load ptr, ptr %i2p, align 8
br label %for.body

for.body:
br label %for.body

exit:
ret i1 false
}

; CHECK-LABEL: @br_true(

; RESULT-FALSE: br label %will_be_unreachable
; RESULT-TRUE: br i1 true, label %exit, label %will_be_unreachable
define i1 @br_true(i64 %int) {
entry:
%i2p = inttoptr i64 %int to ptr
br i1 true, label %exit, label %will_be_unreachable

will_be_unreachable:
%load = load ptr, ptr %i2p, align 8
br label %for.body

for.body:
br label %for.body

exit:
ret i1 false
}

; CHECK-LABEL: @br_true_keep_in_unreachable(
; CHECK: entry:
; INTERESTING: [[I2P:%.+]] = inttoptr i64 %int to ptr
; INTERESTING: load ptr, ptr [[I2P]]

; RESULT-FALSE: br label %will_be_unreachable
; RESULT-TRUE: br i1 true, label %exit, label %will_be_unreachable
define i1 @br_true_keep_in_unreachable(i64 %int) {
entry:
%i2p = inttoptr i64 %int to ptr
br i1 true, label %exit, label %will_be_unreachable

will_be_unreachable:
%load = load ptr, ptr %i2p, align 8
br label %for.body

for.body:
br label %for.body

exit:
ret i1 false
}

; CHECK-LABEL: @br_poison(
; RESULT-FALSE: br label %will_be_unreachable
; RESULT-TRUE: br label %exit
define i1 @br_poison(i64 %int) {
entry:
%i2p = inttoptr i64 %int to ptr
br i1 poison, label %exit, label %will_be_unreachable

will_be_unreachable:
%load = load ptr, ptr %i2p, align 8
br label %for.body

for.body:
br label %for.body

exit:
ret i1 false
}

; CHECK-LABEL: @br_poison_keep_in_unreachable(
; CHECK: entry:
; INTERESTING: [[I2P:%.+]] = inttoptr i64 %int to ptr
; INTERESTING: load ptr, ptr [[I2P]]

; RESULT-FALSE: br label %will_be_unreachable
; RESULT-TRUE: br label %exit
define i1 @br_poison_keep_in_unreachable(i64 %int) {
entry:
%i2p = inttoptr i64 %int to ptr
br i1 poison, label %exit, label %will_be_unreachable

will_be_unreachable:
%load = load ptr, ptr %i2p, align 8
br label %for.body

for.body:
br label %for.body

exit:
ret i1 false
}
13 changes: 7 additions & 6 deletions llvm/tools/llvm-reduce/deltas/ReduceUsingSimplifyCFG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,18 @@ static void reduceConditionals(Oracle &O, ReducerWorkItem &WorkItem,
Module &M = WorkItem.getModule();
SmallVector<BasicBlock *, 16> ToSimplify;

LLVMContext &Ctx = M.getContext();
ConstantInt *ConstValToSet =
Direction ? ConstantInt::getTrue(Ctx) : ConstantInt::getFalse(Ctx);

for (auto &F : M) {
for (auto &BB : F) {
auto *BR = dyn_cast<BranchInst>(BB.getTerminator());
if (!BR || !BR->isConditional() || O.shouldKeep())
if (!BR || !BR->isConditional() || BR->getCondition() == ConstValToSet ||
O.shouldKeep())
continue;

if (Direction)
BR->setCondition(ConstantInt::getTrue(BR->getContext()));
else
BR->setCondition(ConstantInt::getFalse(BR->getContext()));

BR->setCondition(ConstValToSet);
ToSimplify.push_back(&BB);
}
}
Expand Down