Skip to content

Commit 9397e71

Browse files
committed
[IRBuilder] Remove source inst
1 parent 702b2c0 commit 9397e71

File tree

2 files changed

+45
-42
lines changed

2 files changed

+45
-42
lines changed

llvm/include/llvm/IR/IRBuilder.h

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -89,16 +89,19 @@ class IRBuilderCallbackInserter : public IRBuilderDefaultInserter {
8989

9090
/// This provides a helper for copying FMF from an instruction or setting
9191
/// specified flags.
92-
struct FMFSource final {
93-
Instruction *Source;
92+
class FMFSource {
9493
std::optional<FastMathFlags> FMF;
9594

96-
FMFSource() : Source(nullptr) {}
97-
FMFSource(Instruction *Source) : Source(Source) {
95+
public:
96+
FMFSource() = default;
97+
FMFSource(Instruction *Source) {
9898
if (Source)
9999
FMF = Source->getFastMathFlags();
100100
}
101-
FMFSource(FastMathFlags FMF) : Source(nullptr), FMF(FMF) {}
101+
FMFSource(FastMathFlags FMF) : FMF(FMF) {}
102+
FastMathFlags get(FastMathFlags Default) const {
103+
return FMF.value_or(Default);
104+
}
102105
};
103106

104107
/// Common base class shared among various IRBuilders.
@@ -1575,11 +1578,11 @@ class IRBuilderBase {
15751578
return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fadd,
15761579
L, R, FMFSource, Name, FPMD);
15771580

1578-
if (Value *V = Folder.FoldBinOpFMF(Instruction::FAdd, L, R,
1579-
FMFSource.FMF.value_or(FMF)))
1581+
if (Value *V =
1582+
Folder.FoldBinOpFMF(Instruction::FAdd, L, R, FMFSource.get(FMF)))
15801583
return V;
1581-
Instruction *I = setFPAttrs(BinaryOperator::CreateFAdd(L, R), FPMD,
1582-
FMFSource.FMF.value_or(FMF));
1584+
Instruction *I =
1585+
setFPAttrs(BinaryOperator::CreateFAdd(L, R), FPMD, FMFSource.get(FMF));
15831586
return Insert(I, Name);
15841587
}
15851588

@@ -1594,11 +1597,11 @@ class IRBuilderBase {
15941597
return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fsub,
15951598
L, R, FMFSource, Name, FPMD);
15961599

1597-
if (Value *V = Folder.FoldBinOpFMF(Instruction::FSub, L, R,
1598-
FMFSource.FMF.value_or(FMF)))
1600+
if (Value *V =
1601+
Folder.FoldBinOpFMF(Instruction::FSub, L, R, FMFSource.get(FMF)))
15991602
return V;
1600-
Instruction *I = setFPAttrs(BinaryOperator::CreateFSub(L, R), FPMD,
1601-
FMFSource.FMF.value_or(FMF));
1603+
Instruction *I =
1604+
setFPAttrs(BinaryOperator::CreateFSub(L, R), FPMD, FMFSource.get(FMF));
16021605
return Insert(I, Name);
16031606
}
16041607

@@ -1613,11 +1616,11 @@ class IRBuilderBase {
16131616
return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fmul,
16141617
L, R, FMFSource, Name, FPMD);
16151618

1616-
if (Value *V = Folder.FoldBinOpFMF(Instruction::FMul, L, R,
1617-
FMFSource.FMF.value_or(FMF)))
1619+
if (Value *V =
1620+
Folder.FoldBinOpFMF(Instruction::FMul, L, R, FMFSource.get(FMF)))
16181621
return V;
1619-
Instruction *I = setFPAttrs(BinaryOperator::CreateFMul(L, R), FPMD,
1620-
FMFSource.FMF.value_or(FMF));
1622+
Instruction *I =
1623+
setFPAttrs(BinaryOperator::CreateFMul(L, R), FPMD, FMFSource.get(FMF));
16211624
return Insert(I, Name);
16221625
}
16231626

@@ -1632,11 +1635,11 @@ class IRBuilderBase {
16321635
return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fdiv,
16331636
L, R, FMFSource, Name, FPMD);
16341637

1635-
if (Value *V = Folder.FoldBinOpFMF(Instruction::FDiv, L, R,
1636-
FMFSource.FMF.value_or(FMF)))
1638+
if (Value *V =
1639+
Folder.FoldBinOpFMF(Instruction::FDiv, L, R, FMFSource.get(FMF)))
16371640
return V;
1638-
Instruction *I = setFPAttrs(BinaryOperator::CreateFDiv(L, R), FPMD,
1639-
FMFSource.FMF.value_or(FMF));
1641+
Instruction *I =
1642+
setFPAttrs(BinaryOperator::CreateFDiv(L, R), FPMD, FMFSource.get(FMF));
16401643
return Insert(I, Name);
16411644
}
16421645

@@ -1651,11 +1654,11 @@ class IRBuilderBase {
16511654
return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_frem,
16521655
L, R, FMFSource, Name, FPMD);
16531656

1654-
if (Value *V = Folder.FoldBinOpFMF(Instruction::FRem, L, R,
1655-
FMFSource.FMF.value_or(FMF)))
1657+
if (Value *V =
1658+
Folder.FoldBinOpFMF(Instruction::FRem, L, R, FMFSource.get(FMF)))
16561659
return V;
1657-
Instruction *I = setFPAttrs(BinaryOperator::CreateFRem(L, R), FPMD,
1658-
FMFSource.FMF.value_or(FMF));
1660+
Instruction *I =
1661+
setFPAttrs(BinaryOperator::CreateFRem(L, R), FPMD, FMFSource.get(FMF));
16591662
return Insert(I, Name);
16601663
}
16611664

@@ -1672,7 +1675,7 @@ class IRBuilderBase {
16721675
return V;
16731676
Instruction *BinOp = BinaryOperator::Create(Opc, LHS, RHS);
16741677
if (isa<FPMathOperator>(BinOp))
1675-
setFPAttrs(BinOp, FPMathTag, FMFSource.FMF.value_or(FMF));
1678+
setFPAttrs(BinOp, FPMathTag, FMFSource.get(FMF));
16761679
return Insert(BinOp, Name);
16771680
}
16781681

@@ -1737,12 +1740,12 @@ class IRBuilderBase {
17371740

17381741
Value *CreateFNegFMF(Value *V, FMFSource FMFSource, const Twine &Name = "",
17391742
MDNode *FPMathTag = nullptr) {
1740-
if (Value *Res = Folder.FoldUnOpFMF(Instruction::FNeg, V,
1741-
FMFSource.FMF.value_or(FMF)))
1743+
if (Value *Res =
1744+
Folder.FoldUnOpFMF(Instruction::FNeg, V, FMFSource.get(FMF)))
17421745
return Res;
1743-
return Insert(setFPAttrs(UnaryOperator::CreateFNeg(V), FPMathTag,
1744-
FMFSource.FMF.value_or(FMF)),
1745-
Name);
1746+
return Insert(
1747+
setFPAttrs(UnaryOperator::CreateFNeg(V), FPMathTag, FMFSource.get(FMF)),
1748+
Name);
17461749
}
17471750

17481751
Value *CreateNot(Value *V, const Twine &Name = "") {
@@ -2183,7 +2186,7 @@ class IRBuilderBase {
21832186
return Folded;
21842187
Instruction *Cast = CastInst::Create(Op, V, DestTy);
21852188
if (isa<FPMathOperator>(Cast))
2186-
setFPAttrs(Cast, FPMathTag, FMFSource.FMF.value_or(FMF));
2189+
setFPAttrs(Cast, FPMathTag, FMFSource.get(FMF));
21872190
return Insert(Cast, Name);
21882191
}
21892192

llvm/lib/IR/IRBuilder.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ IRBuilderBase::createCallHelper(Function *Callee, ArrayRef<Value *> Ops,
8181
const Twine &Name, FMFSource FMFSource,
8282
ArrayRef<OperandBundleDef> OpBundles) {
8383
CallInst *CI = CreateCall(Callee, Ops, OpBundles, Name);
84-
if (FMFSource.FMF.has_value())
85-
CI->setFastMathFlags(*FMFSource.FMF);
84+
if (isa<FPMathOperator>(CI))
85+
CI->setFastMathFlags(FMFSource.get(FMF));
8686
return CI;
8787
}
8888

@@ -882,7 +882,7 @@ Value *IRBuilderBase::CreateBinaryIntrinsic(Intrinsic::ID ID, Value *LHS,
882882
Module *M = BB->getModule();
883883
Function *Fn = Intrinsic::getOrInsertDeclaration(M, ID, {LHS->getType()});
884884
if (Value *V = Folder.FoldBinaryIntrinsic(ID, LHS, RHS, Fn->getReturnType(),
885-
FMFSource.Source))
885+
/*FMFSource=*/nullptr))
886886
return V;
887887
return createCallHelper(Fn, {LHS, RHS}, Name, FMFSource);
888888
}
@@ -931,7 +931,7 @@ CallInst *IRBuilderBase::CreateConstrainedFPBinOp(
931931
Value *RoundingV = getConstrainedFPRounding(Rounding);
932932
Value *ExceptV = getConstrainedFPExcept(Except);
933933

934-
FastMathFlags UseFMF = FMFSource.FMF.value_or(FMF);
934+
FastMathFlags UseFMF = FMFSource.get(FMF);
935935

936936
CallInst *C = CreateIntrinsic(ID, {L->getType()},
937937
{L, R, RoundingV, ExceptV}, nullptr, Name);
@@ -946,7 +946,7 @@ CallInst *IRBuilderBase::CreateConstrainedFPUnroundedBinOp(
946946
std::optional<fp::ExceptionBehavior> Except) {
947947
Value *ExceptV = getConstrainedFPExcept(Except);
948948

949-
FastMathFlags UseFMF = FMFSource.FMF.value_or(FMF);
949+
FastMathFlags UseFMF = FMFSource.get(FMF);
950950

951951
CallInst *C =
952952
CreateIntrinsic(ID, {L->getType()}, {L, R, ExceptV}, nullptr, Name);
@@ -976,7 +976,7 @@ CallInst *IRBuilderBase::CreateConstrainedFPCast(
976976
std::optional<fp::ExceptionBehavior> Except) {
977977
Value *ExceptV = getConstrainedFPExcept(Except);
978978

979-
FastMathFlags UseFMF = FMFSource.FMF.value_or(FMF);
979+
FastMathFlags UseFMF = FMFSource.get(FMF);
980980

981981
CallInst *C;
982982
if (Intrinsic::hasConstrainedFPRoundingModeOperand(ID)) {
@@ -1006,9 +1006,9 @@ Value *IRBuilderBase::CreateFCmpHelper(CmpInst::Predicate P, Value *LHS,
10061006

10071007
if (auto *V = Folder.FoldCmp(P, LHS, RHS))
10081008
return V;
1009-
return Insert(setFPAttrs(new FCmpInst(P, LHS, RHS), FPMathTag,
1010-
FMFSource.FMF.value_or(FMF)),
1011-
Name);
1009+
return Insert(
1010+
setFPAttrs(new FCmpInst(P, LHS, RHS), FPMathTag, FMFSource.get(FMF)),
1011+
Name);
10121012
}
10131013

10141014
CallInst *IRBuilderBase::CreateConstrainedFPCmp(
@@ -1058,7 +1058,7 @@ Value *IRBuilderBase::CreateSelectFMF(Value *C, Value *True, Value *False,
10581058
Sel = addBranchMetadata(Sel, Prof, Unpred);
10591059
}
10601060
if (isa<FPMathOperator>(Sel))
1061-
setFPAttrs(Sel, /*MDNode=*/nullptr, FMFSource.FMF.value_or(FMF));
1061+
setFPAttrs(Sel, /*MDNode=*/nullptr, FMFSource.get(FMF));
10621062
return Insert(Sel, Name);
10631063
}
10641064

0 commit comments

Comments
 (0)