Skip to content

Commit 97d6bec

Browse files
committed
[InstCombine] Enable select freeze poison folding when storing value
The non-freeze poison argument to select can be one of the following: global, constant, and noundef arguments. Alive2 test validation: https://alive2.llvm.org/ce/z/jbtCS6
1 parent 397696b commit 97d6bec

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4812,16 +4812,21 @@ Instruction *InstCombinerImpl::visitFreeze(FreezeInst &I) {
48124812
//
48134813
// TODO: This could use getBinopAbsorber() / getBinopIdentity() to avoid
48144814
// duplicating logic for binops at least.
4815-
auto getUndefReplacement = [&I](Type *Ty) {
4816-
Constant *BestValue = nullptr;
4817-
Constant *NullValue = Constant::getNullValue(Ty);
4815+
auto getUndefReplacement = [&I, &AC = this->AC, &DT = this->DT](Type *Ty) {
4816+
Value *SelectArgument = nullptr;
4817+
if (I.hasOneUse() &&
4818+
match(I.user_back(),
4819+
m_c_Select(m_Specific(&I), m_Value(SelectArgument))) &&
4820+
isGuaranteedNotToBeUndefOrPoison(SelectArgument, &AC, &I, &DT))
4821+
return SelectArgument;
4822+
Value *BestValue = nullptr;
4823+
Value *NullValue = Constant::getNullValue(Ty);
48184824
for (const auto *U : I.users()) {
4819-
Constant *C = NullValue;
4825+
Value *C = NullValue;
48204826
if (match(U, m_Or(m_Value(), m_Value())))
48214827
C = ConstantInt::getAllOnesValue(Ty);
48224828
else if (match(U, m_Select(m_Specific(&I), m_Constant(), m_Value())))
48234829
C = ConstantInt::getTrue(Ty);
4824-
48254830
if (!BestValue)
48264831
BestValue = C;
48274832
else if (BestValue != C)
@@ -4842,8 +4847,11 @@ Instruction *InstCombinerImpl::visitFreeze(FreezeInst &I) {
48424847

48434848
Constant *C;
48444849
if (match(Op0, m_Constant(C)) && C->containsUndefOrPoisonElement()) {
4845-
Constant *ReplaceC = getUndefReplacement(I.getType()->getScalarType());
4846-
return replaceInstUsesWith(I, Constant::replaceUndefsWith(C, ReplaceC));
4850+
Value *Replace = getUndefReplacement(I.getType()->getScalarType());
4851+
if (Constant *ReplaceC = dyn_cast<Constant>(Replace))
4852+
return replaceInstUsesWith(I, Constant::replaceUndefsWith(C, ReplaceC));
4853+
else
4854+
return replaceInstUsesWith(I, Replace);
48474855
}
48484856

48494857
// Replace uses of Op with freeze(Op).

llvm/test/Transforms/InstCombine/select.ll

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4904,8 +4904,7 @@ define i32 @src_simplify_2x_at_once_and(i32 %x, i32 %y) {
49044904

49054905
define void @select_freeze_poison_parameter(ptr noundef %addr.src, ptr %addr.tgt, i1 %cond) {
49064906
; CHECK-LABEL: @select_freeze_poison_parameter(
4907-
; CHECK-NEXT: [[ADDR_SRC:%.*]] = select i1 [[COND:%.*]], ptr [[ADDR_SRC1:%.*]], ptr null
4908-
; CHECK-NEXT: store ptr [[ADDR_SRC]], ptr [[ADDR_TGT:%.*]], align 8
4907+
; CHECK-NEXT: store ptr [[ADDR_SRC:%.*]], ptr [[ADDR_TGT:%.*]], align 8
49094908
; CHECK-NEXT: ret void
49104909
;
49114910
%freeze = freeze ptr poison
@@ -4918,8 +4917,7 @@ define void @select_freeze_poison_parameter(ptr noundef %addr.src, ptr %addr.tgt
49184917

49194918
define void @select_freeze_poison_global(ptr %addr.tgt, i1 %cond) {
49204919
; CHECK-LABEL: @select_freeze_poison_global(
4921-
; CHECK-NEXT: [[SELECT_ADDR:%.*]] = select i1 [[COND:%.*]], ptr @glb, ptr null
4922-
; CHECK-NEXT: store ptr [[SELECT_ADDR]], ptr [[ADDR_TGT:%.*]], align 8
4920+
; CHECK-NEXT: store ptr @glb, ptr [[ADDR_TGT:%.*]], align 8
49234921
; CHECK-NEXT: ret void
49244922
;
49254923
%freeze = freeze ptr poison
@@ -4930,8 +4928,7 @@ define void @select_freeze_poison_global(ptr %addr.tgt, i1 %cond) {
49304928

49314929
define void @select_freeze_poison_constant(ptr %addr.tgt, i1 %cond) {
49324930
; CHECK-LABEL: @select_freeze_poison_constant(
4933-
; CHECK-NEXT: [[SELECT_ADDR:%.*]] = select i1 [[COND:%.*]], i32 72, i32 0
4934-
; CHECK-NEXT: store i32 [[SELECT_ADDR]], ptr [[ADDR_TGT:%.*]], align 4
4931+
; CHECK-NEXT: store i32 72, ptr [[ADDR_TGT:%.*]], align 4
49354932
; CHECK-NEXT: ret void
49364933
;
49374934
%freeze = freeze i32 poison

0 commit comments

Comments
 (0)