1 //===- llvm/CodeGen/GlobalISel/InstructionSelector.cpp -----------*- C++ -*-==//
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 InstructionSelector class.
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/CodeGen/GlobalISel/InstructionSelector.h"
14 #include "llvm/CodeGen/GlobalISel/RegisterBankInfo.h"
15 #include "llvm/CodeGen/MachineInstr.h"
16 #include "llvm/Target/TargetInstrInfo.h"
17 #include "llvm/Target/TargetRegisterInfo.h"
18 
19 #define DEBUG_TYPE "instructionselector"
20 
21 using namespace llvm;
22 
23 InstructionSelector::InstructionSelector() {}
24 
25 bool InstructionSelector::constrainSelectedInstRegOperands(
26     MachineInstr &I, const TargetInstrInfo &TII, const TargetRegisterInfo &TRI,
27     const RegisterBankInfo &RBI) const {
28   MachineBasicBlock &MBB = *I.getParent();
29   MachineFunction &MF = *MBB.getParent();
30   MachineRegisterInfo &MRI = MF.getRegInfo();
31 
32   for (unsigned OpI = 0, OpE = I.getNumExplicitOperands(); OpI != OpE; ++OpI) {
33     MachineOperand &MO = I.getOperand(OpI);
34 
35     // There's nothing to be done on non-register operands.
36     if (!MO.isReg())
37       continue;
38 
39     DEBUG(dbgs() << "Converting operand: " << MO << '\n');
40     assert(MO.isReg() && "Unsupported non-reg operand");
41 
42     // Physical registers don't need to be constrained.
43     if (TRI.isPhysicalRegister(MO.getReg()))
44       continue;
45 
46     const TargetRegisterClass *RC = TII.getRegClass(I.getDesc(), OpI, &TRI, MF);
47     assert(RC && "Selected inst should have regclass operand");
48 
49     // If the operand is a vreg, we should constrain its regclass, and only
50     // insert COPYs if that's impossible.
51     // If the operand is a physreg, we only insert COPYs if the register class
52     // doesn't contain the register.
53     if (RBI.constrainGenericRegister(MO.getReg(), *RC, MRI))
54       continue;
55 
56     DEBUG(dbgs() << "Constraining with COPYs isn't implemented yet");
57     return false;
58   }
59   return true;
60 }
61