1 //===- llvm/CodeGen/GlobalISel/InstructionSelect.cpp - InstructionSelect ---==// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 /// \file 10 /// This file implements the InstructionSelect class. 11 //===----------------------------------------------------------------------===// 12 13 #include "llvm/CodeGen/GlobalISel/InstructionSelect.h" 14 #include "llvm/ADT/PostOrderIterator.h" 15 #include "llvm/ADT/ScopeExit.h" 16 #include "llvm/ADT/Twine.h" 17 #include "llvm/CodeGen/GlobalISel/InstructionSelector.h" 18 #include "llvm/CodeGen/GlobalISel/LegalizerInfo.h" 19 #include "llvm/CodeGen/GlobalISel/Utils.h" 20 #include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h" 21 #include "llvm/CodeGen/MachineRegisterInfo.h" 22 #include "llvm/CodeGen/TargetPassConfig.h" 23 #include "llvm/IR/Constants.h" 24 #include "llvm/IR/Function.h" 25 #include "llvm/Support/CommandLine.h" 26 #include "llvm/Support/Debug.h" 27 #include "llvm/Target/TargetSubtargetInfo.h" 28 29 #define DEBUG_TYPE "instruction-select" 30 31 using namespace llvm; 32 33 char InstructionSelect::ID = 0; 34 INITIALIZE_PASS_BEGIN(InstructionSelect, DEBUG_TYPE, 35 "Select target instructions out of generic instructions", 36 false, false) 37 INITIALIZE_PASS_DEPENDENCY(TargetPassConfig) 38 INITIALIZE_PASS_END(InstructionSelect, DEBUG_TYPE, 39 "Select target instructions out of generic instructions", 40 false, false) 41 42 InstructionSelect::InstructionSelect() : MachineFunctionPass(ID) { 43 initializeInstructionSelectPass(*PassRegistry::getPassRegistry()); 44 } 45 46 void InstructionSelect::getAnalysisUsage(AnalysisUsage &AU) const { 47 AU.addRequired<TargetPassConfig>(); 48 MachineFunctionPass::getAnalysisUsage(AU); 49 } 50 51 bool InstructionSelect::runOnMachineFunction(MachineFunction &MF) { 52 const MachineRegisterInfo &MRI = MF.getRegInfo(); 53 54 // No matter what happens, whether we successfully select the function or not, 55 // nothing is going to use the vreg types after us. Make sure they disappear. 56 auto ClearVRegTypesOnReturn = 57 make_scope_exit([&]() { MRI.getVRegToType().clear(); }); 58 59 // If the ISel pipeline failed, do not bother running that pass. 60 if (MF.getProperties().hasProperty( 61 MachineFunctionProperties::Property::FailedISel)) 62 return false; 63 64 DEBUG(dbgs() << "Selecting function: " << MF.getName() << '\n'); 65 66 const TargetPassConfig &TPC = getAnalysis<TargetPassConfig>(); 67 const InstructionSelector *ISel = MF.getSubtarget().getInstructionSelector(); 68 assert(ISel && "Cannot work without InstructionSelector"); 69 70 // An optimization remark emitter. Used to report failures. 71 MachineOptimizationRemarkEmitter MORE(MF, /*MBFI=*/nullptr); 72 73 // FIXME: freezeReservedRegs is now done in IRTranslator, but there are many 74 // other MF/MFI fields we need to initialize. 75 76 #ifndef NDEBUG 77 // Check that our input is fully legal: we require the function to have the 78 // Legalized property, so it should be. 79 // FIXME: This should be in the MachineVerifier, but it can't use the 80 // LegalizerInfo as it's currently in the separate GlobalISel library. 81 // The RegBankSelected property is already checked in the verifier. Note 82 // that it has the same layering problem, but we only use inline methods so 83 // end up not needing to link against the GlobalISel library. 84 if (const LegalizerInfo *MLI = MF.getSubtarget().getLegalizerInfo()) 85 for (MachineBasicBlock &MBB : MF) 86 for (MachineInstr &MI : MBB) 87 if (isPreISelGenericOpcode(MI.getOpcode()) && !MLI->isLegal(MI, MRI)) { 88 reportGISelFailure(MF, TPC, MORE, "gisel-select", 89 "instruction is not legal", MI); 90 return false; 91 } 92 93 #endif 94 // FIXME: We could introduce new blocks and will need to fix the outer loop. 95 // Until then, keep track of the number of blocks to assert that we don't. 96 const size_t NumBlocks = MF.size(); 97 98 for (MachineBasicBlock *MBB : post_order(&MF)) { 99 if (MBB->empty()) 100 continue; 101 102 // Select instructions in reverse block order. We permit erasing so have 103 // to resort to manually iterating and recognizing the begin (rend) case. 104 bool ReachedBegin = false; 105 for (auto MII = std::prev(MBB->end()), Begin = MBB->begin(); 106 !ReachedBegin;) { 107 #ifndef NDEBUG 108 // Keep track of the insertion range for debug printing. 109 const auto AfterIt = std::next(MII); 110 #endif 111 // Select this instruction. 112 MachineInstr &MI = *MII; 113 114 // And have our iterator point to the next instruction, if there is one. 115 if (MII == Begin) 116 ReachedBegin = true; 117 else 118 --MII; 119 120 DEBUG(dbgs() << "Selecting: \n " << MI); 121 122 if (!ISel->select(MI)) { 123 // FIXME: It would be nice to dump all inserted instructions. It's 124 // not obvious how, esp. considering select() can insert after MI. 125 reportGISelFailure(MF, TPC, MORE, "gisel-select", "cannot select", MI); 126 return false; 127 } 128 129 // Dump the range of instructions that MI expanded into. 130 DEBUG({ 131 auto InsertedBegin = ReachedBegin ? MBB->begin() : std::next(MII); 132 dbgs() << "Into:\n"; 133 for (auto &InsertedMI : make_range(InsertedBegin, AfterIt)) 134 dbgs() << " " << InsertedMI; 135 dbgs() << '\n'; 136 }); 137 } 138 } 139 140 // Now that selection is complete, there are no more generic vregs. Verify 141 // that the size of the now-constrained vreg is unchanged and that it has a 142 // register class. 143 for (auto &VRegToType : MRI.getVRegToType()) { 144 unsigned VReg = VRegToType.first; 145 auto *RC = MRI.getRegClassOrNull(VReg); 146 MachineInstr *MI = nullptr; 147 if (!MRI.def_empty(VReg)) 148 MI = &*MRI.def_instr_begin(VReg); 149 else if (!MRI.use_empty(VReg)) 150 MI = &*MRI.use_instr_begin(VReg); 151 152 if (MI && !RC) { 153 reportGISelFailure(MF, TPC, MORE, "gisel-select", 154 "VReg has no regclass after selection", *MI); 155 return false; 156 } else if (!RC) 157 continue; 158 159 if (VRegToType.second.isValid() && 160 VRegToType.second.getSizeInBits() > (RC->getSize() * 8)) { 161 reportGISelFailure(MF, TPC, MORE, "gisel-select", 162 "VReg has explicit size different from class size", 163 *MI); 164 return false; 165 } 166 } 167 168 if (MF.size() != NumBlocks) { 169 MachineOptimizationRemarkMissed R("gisel-select", "GISelFailure", 170 MF.getFunction()->getSubprogram(), 171 /*MBB=*/nullptr); 172 R << "inserting blocks is not supported yet"; 173 reportGISelFailure(MF, TPC, MORE, R); 174 return false; 175 } 176 177 // FIXME: Should we accurately track changes? 178 return true; 179 } 180 181 bool InstructionSelector::isOperandImmEqual( 182 const MachineOperand &MO, int64_t Value, 183 const MachineRegisterInfo &MRI) const { 184 // TODO: We should also test isImm() and isCImm() too but this isn't required 185 // until a DAGCombine equivalent is implemented. 186 187 if (MO.isReg()) { 188 MachineInstr *Def = MRI.getVRegDef(MO.getReg()); 189 if (Def->getOpcode() != TargetOpcode::G_CONSTANT) 190 return false; 191 assert(Def->getOperand(1).isCImm() && 192 "G_CONSTANT values must be constants"); 193 const ConstantInt &Imm = *Def->getOperand(1).getCImm(); 194 return Imm.getBitWidth() <= 64 && Imm.getSExtValue() == Value; 195 } 196 197 return false; 198 } 199