Skip to content

Commit d8731d3

Browse files
committed
pass original select to MDFrom argument of CreateSelect
1 parent 52d9c46 commit d8731d3

File tree

1 file changed

+10
-16
lines changed

1 file changed

+10
-16
lines changed

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1363,6 +1363,8 @@ Value *InstCombinerImpl::SimplifySelectsFeedingBinaryOp(BinaryOperator &I,
13631363
if (!LHSIsSelect && !RHSIsSelect)
13641364
return nullptr;
13651365

1366+
SelectInst *SI = cast<SelectInst>(LHSIsSelect ? LHS : RHS);
1367+
13661368
FastMathFlags FMF;
13671369
BuilderTy::FastMathFlagGuard Guard(Builder);
13681370
if (isa<FPMathOperator>(&I)) {
@@ -1374,7 +1376,6 @@ Value *InstCombinerImpl::SimplifySelectsFeedingBinaryOp(BinaryOperator &I,
13741376
SimplifyQuery Q = SQ.getWithInstruction(&I);
13751377

13761378
Value *Cond, *True = nullptr, *False = nullptr;
1377-
MDNode *ProfileData = nullptr;
13781379

13791380
// Special-case for add/negate combination. Replace the zero in the negation
13801381
// with the trailing add operand:
@@ -1384,18 +1385,16 @@ Value *InstCombinerImpl::SimplifySelectsFeedingBinaryOp(BinaryOperator &I,
13841385
// We need an 'add' and exactly 1 arm of the select to have been simplified.
13851386
if (Opcode != Instruction::Add || (!True && !False) || (True && False))
13861387
return nullptr;
1387-
Value *N, *SI = nullptr;
1388+
Value *N;
13881389
if (True && match(FVal, m_Neg(m_Value(N)))) {
13891390
Value *Sub = Builder.CreateSub(Z, N);
1390-
SI = Builder.CreateSelect(Cond, True, Sub, I.getName());
1391+
return Builder.CreateSelect(Cond, True, Sub, I.getName(), SI);
13911392
}
13921393
if (False && match(TVal, m_Neg(m_Value(N)))) {
13931394
Value *Sub = Builder.CreateSub(Z, N);
1394-
SI = Builder.CreateSelect(Cond, Sub, False, I.getName());
1395+
return Builder.CreateSelect(Cond, Sub, False, I.getName(), SI);
13951396
}
1396-
if (!ProfcheckDisableMetadataFixes && SI)
1397-
cast<SelectInst>(SI)->setMetadata(LLVMContext::MD_prof, ProfileData);
1398-
return SI;
1397+
return nullptr;
13991398
};
14001399

14011400
if (LHSIsSelect && RHSIsSelect && A == D) {
@@ -1405,8 +1404,7 @@ Value *InstCombinerImpl::SimplifySelectsFeedingBinaryOp(BinaryOperator &I,
14051404
False = simplifyBinOp(Opcode, C, F, FMF, Q);
14061405
// Profile weights for both LHS and RHS should be the same because they have
14071406
// the same idempotent conditional.
1408-
ProfileData = cast<SelectInst>(LHS)->getMetadata(LLVMContext::MD_prof);
1409-
assert(ProfileData ==
1407+
assert(cast<SelectInst>(LHS)->getMetadata(LLVMContext::MD_prof) ==
14101408
cast<SelectInst>(RHS)->getMetadata(LLVMContext::MD_prof) &&
14111409
"LHS and RHS select statements have different metadata!");
14121410

@@ -1418,15 +1416,13 @@ Value *InstCombinerImpl::SimplifySelectsFeedingBinaryOp(BinaryOperator &I,
14181416
}
14191417
} else if (LHSIsSelect && LHS->hasOneUse()) {
14201418
// (A ? B : C) op Y -> A ? (B op Y) : (C op Y)
1421-
ProfileData = cast<SelectInst>(LHS)->getMetadata(LLVMContext::MD_prof);
14221419
Cond = A;
14231420
True = simplifyBinOp(Opcode, B, RHS, FMF, Q);
14241421
False = simplifyBinOp(Opcode, C, RHS, FMF, Q);
14251422
if (Value *NewSel = foldAddNegate(B, C, RHS))
14261423
return NewSel;
14271424
} else if (RHSIsSelect && RHS->hasOneUse()) {
14281425
// X op (D ? E : F) -> D ? (X op E) : (X op F)
1429-
ProfileData = cast<SelectInst>(RHS)->getMetadata(LLVMContext::MD_prof);
14301426
Cond = D;
14311427
True = simplifyBinOp(Opcode, LHS, E, FMF, Q);
14321428
False = simplifyBinOp(Opcode, LHS, F, FMF, Q);
@@ -1437,11 +1433,9 @@ Value *InstCombinerImpl::SimplifySelectsFeedingBinaryOp(BinaryOperator &I,
14371433
if (!True || !False)
14381434
return nullptr;
14391435

1440-
Value *SI = Builder.CreateSelect(Cond, True, False);
1441-
SI->takeName(&I);
1442-
if (!ProfcheckDisableMetadataFixes)
1443-
cast<SelectInst>(SI)->setMetadata(LLVMContext::MD_prof, ProfileData);
1444-
return SI;
1436+
Value *NewSI = Builder.CreateSelect(Cond, True, False, I.getName(), SI);
1437+
NewSI->takeName(&I);
1438+
return NewSI;
14451439
}
14461440

14471441
/// Freely adapt every user of V as-if V was changed to !V.

0 commit comments

Comments
 (0)