From 79fe1bea6bde1a937c6469cb1735a6cf6e493c46 Mon Sep 17 00:00:00 2001 From: Quentin Colombet Date: Fri, 20 May 2016 18:37:33 +0000 Subject: [PATCH] [RegBankSelect] Look for the best mapping in greedy mode. The Fast mode takes the first mapping, the greedy mode loops over all the possible mapping for an instruction and choose the cheaper one. Test case will come with target specific code, since we currently do not have instructions that have several mappings. llvm-svn: 270249 --- .../llvm/CodeGen/GlobalISel/RegBankSelect.h | 7 +++ llvm/lib/CodeGen/GlobalISel/RegBankSelect.cpp | 48 +++++++++++++++---- 2 files changed, 45 insertions(+), 10 deletions(-) diff --git a/llvm/include/llvm/CodeGen/GlobalISel/RegBankSelect.h b/llvm/include/llvm/CodeGen/GlobalISel/RegBankSelect.h index 7b8601fc3ae9..e8454e82f89d 100644 --- a/llvm/include/llvm/CodeGen/GlobalISel/RegBankSelect.h +++ b/llvm/include/llvm/CodeGen/GlobalISel/RegBankSelect.h @@ -525,6 +525,13 @@ private: RegBankSelect::RepairingPlacement &RepairPt, const iterator_range::iterator> &NewVRegs); + /// Find the best mapping for \p MI from \p PossibleMappings. + /// \return a reference on the best mapping in \p PossibleMappings. + RegisterBankInfo::InstructionMapping & + findBestMapping(MachineInstr &MI, + RegisterBankInfo::InstructionMappings &PossibleMappings, + SmallVectorImpl &RepairPts); + /// Compute the cost of mapping \p MI with \p InstrMapping and /// compute the repairing placement for such mapping in \p /// RepairPts. diff --git a/llvm/lib/CodeGen/GlobalISel/RegBankSelect.cpp b/llvm/lib/CodeGen/GlobalISel/RegBankSelect.cpp index 152fc0c306c7..4dc2bd8c81a4 100644 --- a/llvm/lib/CodeGen/GlobalISel/RegBankSelect.cpp +++ b/llvm/lib/CodeGen/GlobalISel/RegBankSelect.cpp @@ -136,6 +136,27 @@ void RegBankSelect::repairReg( // Legalize NewInstrs if need be. } +RegisterBankInfo::InstructionMapping &RegBankSelect::findBestMapping( + MachineInstr &MI, RegisterBankInfo::InstructionMappings &PossibleMappings, + SmallVectorImpl &RepairPts) { + + RegisterBankInfo::InstructionMapping *BestMapping = nullptr; + MappingCost Cost = MappingCost::ImpossibleCost(); + SmallVector LocalRepairPts; + for (RegisterBankInfo::InstructionMapping &CurMapping : PossibleMappings) { + MappingCost CurCost = computeMapping(MI, CurMapping, LocalRepairPts, &Cost); + if (CurCost < Cost) { + Cost = CurCost; + BestMapping = &CurMapping; + RepairPts.clear(); + for (RepairingPlacement &RepairPt : LocalRepairPts) + RepairPts.emplace_back(std::move(RepairPt)); + } + } + assert(BestMapping && "No suitable mapping for instruction"); + return *BestMapping; +} + void RegBankSelect::tryAvoidingSplit( RegBankSelect::RepairingPlacement &RepairPt, const MachineOperand &MO, const RegisterBankInfo::ValueMapping &ValMapping) const { @@ -433,22 +454,29 @@ void RegBankSelect::applyMapping( void RegBankSelect::assignInstr(MachineInstr &MI) { DEBUG(dbgs() << "Assign: " << MI); - RegisterBankInfo::InstructionMapping DefaultMapping = - RBI->getInstrMapping(MI); // Remember the repairing placement for all the operands. SmallVector RepairPts; - MappingCost DefaultCost = computeMapping(MI, DefaultMapping, RepairPts); - (void)DefaultCost; - assert(DefaultCost != MappingCost::ImpossibleCost() && - "Default mapping is not suited"); - + RegisterBankInfo::InstructionMapping BestMapping; + if (OptMode == RegBankSelect::Mode::Fast) { + BestMapping = RBI->getInstrMapping(MI); + MappingCost DefaultCost = computeMapping(MI, BestMapping, RepairPts); + (void)DefaultCost; + assert(DefaultCost != MappingCost::ImpossibleCost() && + "Default mapping is not suited"); + } else { + RegisterBankInfo::InstructionMappings PossibleMappings = + RBI->getInstrPossibleMappings(MI); + assert(!PossibleMappings.empty() && + "Do not know how to map this instruction"); + BestMapping = std::move(findBestMapping(MI, PossibleMappings, RepairPts)); + } // Make sure the mapping is valid for MI. - assert(DefaultMapping.verify(MI) && "Invalid instruction mapping"); + assert(BestMapping.verify(MI) && "Invalid instruction mapping"); - DEBUG(dbgs() << "Mapping: " << DefaultMapping << '\n'); + DEBUG(dbgs() << "Mapping: " << BestMapping << '\n'); - applyMapping(MI, DefaultMapping, RepairPts); + applyMapping(MI, BestMapping, RepairPts); DEBUG(dbgs() << "Assigned: " << MI); }