Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 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
35 changes: 35 additions & 0 deletions llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3645,12 +3645,47 @@ static bool hasAffectedValue(Value *V, SmallPtrSetImpl<Value *> &Affected,
return false;
}

// Checks for following pattern:
// ```
// %any1 = select i1 %any0, float 1.000000e+00, float 0.000000e+00
// ```
// which then gets folded into:
// ```
// %any1 = uitofp i1 %any0 to float
// ```
// (also works with double)
static std::optional<Instruction*> mabyeFoldIntoCast(Value* CondVal, ConstantFP *TrueVal, ConstantFP *FalseVal, Type *SelType, llvm::StringRef out) {
if (TrueVal->getValueAPF().convertToDouble() != 1.0) {
return std::optional<Instruction*>();
}

if (FalseVal->getValueAPF().convertToDouble() != 0.0) {
return std::optional<Instruction*>();
}

return CastInst::Create(llvm::Instruction::UIToFP, CondVal, SelType, out);
}


Instruction *InstCombinerImpl::visitSelectInst(SelectInst &SI) {
Value *CondVal = SI.getCondition();
Value *TrueVal = SI.getTrueValue();
Value *FalseVal = SI.getFalseValue();
Type *SelType = SI.getType();


if (ConstantFP *True = dyn_cast<ConstantFP>(TrueVal)) {
if (ConstantFP *False = dyn_cast<ConstantFP>(FalseVal)) {
if (SelType->isFloatTy() || SelType->isDoubleTy()) {
std::optional<Instruction*> folded = mabyeFoldIntoCast(CondVal, True, False, SelType, SI.getName());

if (folded.has_value()) {
return folded.value();
}
}
}
}

if (Value *V = simplifySelectInst(CondVal, TrueVal, FalseVal,
SQ.getWithInstruction(&SI)))
return replaceInstUsesWith(SI, V);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
; RUN: opt < %s -passes=instcombine -S | FileCheck %s

define noundef float @ifelse(i1 noundef zeroext %x) unnamed_addr {
start:
; CHECK: %.= uitofp i1 %x to float
%. = select i1 %x, float 1.000000e+00, float 0.000000e+00
ret float %.
}
Loading