Index: llvm/include/llvm/ADT/BitVector.h =================================================================== --- llvm/include/llvm/ADT/BitVector.h +++ llvm/include/llvm/ADT/BitVector.h @@ -568,6 +568,18 @@ return false; } + template + static BitVector& apply(F&& f, BitVector& Out, ArgTys const&... Args) { + static_assert(sizeof...(ArgTys) > 0, "at least one argument"); + std::initializer_list Sizes = { Args.size()...}; + size_t CommonSize = *std::min_element(Sizes.begin(), Sizes.end()); + if (Out.size() < CommonSize) + Out.resize(CommonSize); + for (size_t i = 0, e = Out.NumBitWords(CommonSize); i != e; ++i) + Out.Bits[i] = f(Args.Bits[i]...); + return Out; + } + BitVector &operator|=(const BitVector &RHS) { if (size() < RHS.size()) resize(RHS.size()); Index: llvm/lib/CodeGen/CFIInstrInserter.cpp =================================================================== --- llvm/lib/CodeGen/CFIInstrInserter.cpp +++ llvm/lib/CodeGen/CFIInstrInserter.cpp @@ -265,9 +265,9 @@ MBBInfo.OutgoingCFARegister = SetRegister; // Update outgoing CSR info. - MBBInfo.OutgoingCSRSaved = MBBInfo.IncomingCSRSaved; - MBBInfo.OutgoingCSRSaved |= CSRSaved; - MBBInfo.OutgoingCSRSaved.reset(CSRRestored); + BitVector::apply([](auto x, auto y, auto z) { return (x | y) & ~z;}, + MBBInfo.OutgoingCSRSaved, + MBBInfo.IncomingCSRSaved, CSRSaved, CSRRestored); } void CFIInstrInserter::updateSuccCFAInfo(MBBCFAInfo &MBBInfo) { @@ -295,6 +295,7 @@ const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo(); bool InsertedCFIInstr = false; + BitVector SetDifference; for (MachineBasicBlock &MBB : MF) { // Skip the first MBB in a function if (MBB.getNumber() == MF.front().getNumber()) continue; @@ -346,8 +347,7 @@ continue; } - BitVector SetDifference = PrevMBBInfo->OutgoingCSRSaved; - SetDifference.reset(MBBInfo.IncomingCSRSaved); + BitVector::apply([](auto x, auto y) { return x & ~y;}, SetDifference, PrevMBBInfo->OutgoingCSRSaved, MBBInfo.IncomingCSRSaved); for (int Reg : SetDifference.set_bits()) { unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createRestore(nullptr, Reg)); @@ -356,8 +356,7 @@ InsertedCFIInstr = true; } - SetDifference = MBBInfo.IncomingCSRSaved; - SetDifference.reset(PrevMBBInfo->OutgoingCSRSaved); + BitVector::apply([](auto x, auto y) { return x & ~y;}, SetDifference, MBBInfo.IncomingCSRSaved, PrevMBBInfo->OutgoingCSRSaved); for (int Reg : SetDifference.set_bits()) { auto it = CSRLocMap.find(Reg); assert(it != CSRLocMap.end() && "Reg should have an entry in CSRLocMap");