diff --git a/llvm/lib/CodeGen/SelectionDAG/FunctionLoweringInfo.cpp b/llvm/lib/CodeGen/SelectionDAG/FunctionLoweringInfo.cpp --- a/llvm/lib/CodeGen/SelectionDAG/FunctionLoweringInfo.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/FunctionLoweringInfo.cpp @@ -57,28 +57,6 @@ return false; } -static ISD::NodeType getPreferredExtendForValue(const Value *V) { - // For the users of the source value being used for compare instruction, if - // the number of signed predicate is greater than unsigned predicate, we - // prefer to use SIGN_EXTEND. - // - // With this optimization, we would be able to reduce some redundant sign or - // zero extension instruction, and eventually more machine CSE opportunities - // can be exposed. - ISD::NodeType ExtendKind = ISD::ANY_EXTEND; - unsigned NumOfSigned = 0, NumOfUnsigned = 0; - for (const User *U : V->users()) { - if (const auto *CI = dyn_cast(U)) { - NumOfSigned += CI->isSigned(); - NumOfUnsigned += CI->isUnsigned(); - } - } - if (NumOfSigned > NumOfUnsigned) - ExtendKind = ISD::SIGN_EXTEND; - - return ExtendKind; -} - void FunctionLoweringInfo::set(const Function &fn, MachineFunction &mf, SelectionDAG *DAG) { Fn = &fn; @@ -233,9 +211,6 @@ if (isUsedOutsideOfDefiningBlock(&I)) if (!isa(I) || !StaticAllocaMap.count(cast(&I))) InitializeRegForValue(&I); - - // Decide the preferred extend type for a value. - PreferredExtendType[&I] = getPreferredExtendForValue(&I); } } diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp --- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp @@ -9859,6 +9859,34 @@ llvm_unreachable("LowerOperation not implemented for this target!"); } +static ISD::NodeType getPreferredExtendForValue(const Value *V) { + ISD::NodeType ExtendKind = ISD::ANY_EXTEND; + + // TODO: Skip Arguments to match behavior when this was calculated only for + // instructions. + if (isa(V)) + return ExtendKind; + + // For the users of the source value being used for compare instruction, if + // the number of signed predicate is greater than unsigned predicate, we + // prefer to use SIGN_EXTEND. + // + // With this optimization, we would be able to reduce some redundant sign or + // zero extension instruction, and eventually more machine CSE opportunities + // can be exposed. + unsigned NumOfSigned = 0, NumOfUnsigned = 0; + for (const User *U : V->users()) { + if (const auto *CI = dyn_cast(U)) { + NumOfSigned += CI->isSigned(); + NumOfUnsigned += CI->isUnsigned(); + } + } + if (NumOfSigned > NumOfUnsigned) + ExtendKind = ISD::SIGN_EXTEND; + + return ExtendKind; +} + void SelectionDAGBuilder::CopyValueToVirtualRegister(const Value *V, unsigned Reg) { SDValue Op = getNonRegisterValue(V); @@ -9875,10 +9903,12 @@ None); // This is not an ABI copy. SDValue Chain = DAG.getEntryNode(); - ISD::NodeType ExtendType = ISD::ANY_EXTEND; - auto PreferredExtendIt = FuncInfo.PreferredExtendType.find(V); - if (PreferredExtendIt != FuncInfo.PreferredExtendType.end()) - ExtendType = PreferredExtendIt->second; + // Look up the preferred extend kind if we've already computed it. Otherwise, + // compute it and cache it. + ISD::NodeType &ExtendType = FuncInfo.PreferredExtendType[V]; + if (!ExtendType) + ExtendType = getPreferredExtendForValue(V); + RFV.getCopyToRegs(Op, DAG, getCurSDLoc(), Chain, nullptr, V, ExtendType); PendingExports.push_back(Chain); }