LLVM  5.0.0
InstructionSelector.cpp
Go to the documentation of this file.
1 //===- llvm/CodeGen/GlobalISel/InstructionSelector.cpp --------------------===//
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 
21 #include "llvm/MC/MCInstrDesc.h"
22 #include "llvm/IR/Constants.h"
24 #include "llvm/Support/Debug.h"
27 #include <cassert>
28 
29 #define DEBUG_TYPE "instructionselector"
30 
31 using namespace llvm;
32 
34  : Renderers(MaxRenderers, nullptr), MIs() {}
35 
37 
39  MachineInstr &I, unsigned OpIdx, const TargetRegisterClass &RC,
40  const TargetInstrInfo &TII, const TargetRegisterInfo &TRI,
41  const RegisterBankInfo &RBI) const {
42  MachineBasicBlock &MBB = *I.getParent();
43  MachineFunction &MF = *MBB.getParent();
45 
46  return
47  constrainRegToClass(MRI, TII, RBI, I, I.getOperand(OpIdx).getReg(), RC);
48 }
49 
52  const RegisterBankInfo &RBI) const {
53  MachineBasicBlock &MBB = *I.getParent();
54  MachineFunction &MF = *MBB.getParent();
56 
57  for (unsigned OpI = 0, OpE = I.getNumExplicitOperands(); OpI != OpE; ++OpI) {
58  MachineOperand &MO = I.getOperand(OpI);
59 
60  // There's nothing to be done on non-register operands.
61  if (!MO.isReg())
62  continue;
63 
64  DEBUG(dbgs() << "Converting operand: " << MO << '\n');
65  assert(MO.isReg() && "Unsupported non-reg operand");
66 
67  unsigned Reg = MO.getReg();
68  // Physical registers don't need to be constrained.
69  if (TRI.isPhysicalRegister(Reg))
70  continue;
71 
72  // Register operands with a value of 0 (e.g. predicate operands) don't need
73  // to be constrained.
74  if (Reg == 0)
75  continue;
76 
77  // If the operand is a vreg, we should constrain its regclass, and only
78  // insert COPYs if that's impossible.
79  // constrainOperandRegClass does that for us.
80  MO.setReg(constrainOperandRegClass(MF, TRI, MRI, TII, RBI, I, I.getDesc(),
81  Reg, OpI));
82 
83  // Tie uses to defs as indicated in MCInstrDesc if this hasn't already been
84  // done.
85  if (MO.isUse()) {
86  int DefIdx = I.getDesc().getOperandConstraint(OpI, MCOI::TIED_TO);
87  if (DefIdx != -1 && !I.isRegTiedToUseOperand(DefIdx))
88  I.tieOperands(DefIdx, OpI);
89  }
90  }
91  return true;
92 }
93 
95  const MachineOperand &MO, int64_t Value,
96  const MachineRegisterInfo &MRI) const {
97  if (MO.isReg() && MO.getReg())
98  if (auto VRegVal = getConstantVRegVal(MO.getReg(), MRI))
99  return *VRegVal == Value;
100  return false;
101 }
102 
104  return !MI.mayLoadOrStore() && !MI.hasUnmodeledSideEffects() &&
105  MI.implicit_operands().begin() == MI.implicit_operands().end();
106 }
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
unsigned constrainOperandRegClass(const MachineFunction &MF, const TargetRegisterInfo &TRI, MachineRegisterInfo &MRI, const TargetInstrInfo &TII, const RegisterBankInfo &RBI, MachineInstr &InsertPt, const MCInstrDesc &II, unsigned Reg, unsigned OpIdx)
Try to constrain Reg so that it is usable by argument OpIdx of the provided MCInstrDesc II...
Definition: Utils.cpp:46
bool constrainSelectedInstRegOperands(MachineInstr &I, const TargetInstrInfo &TII, const TargetRegisterInfo &TRI, const RegisterBankInfo &RBI) const
Mutate the newly-selected instruction I to constrain its (possibly generic) virtual register operands...
const MCInstrDesc & getDesc() const
Returns the target instruction descriptor of this MachineInstr.
Definition: MachineInstr.h:276
Holds all the information related to register banks.
const HexagonInstrInfo * TII
bool isReg() const
isReg - Tests if this is a MO_Register operand.
Reg
All possible values of the reg field in the ModR/M byte.
const MachineBasicBlock * getParent() const
Definition: MachineInstr.h:139
TargetInstrInfo - Interface to description of machine instruction set.
unsigned const MachineRegisterInfo * MRI
This file contains the declarations for the subclasses of Constant, which represent the different fla...
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:284
bool isRegTiedToUseOperand(unsigned DefOpIdx, unsigned *UseOpIdx=nullptr) const
Given the index of a register def operand, check if the register def is tied to a source operand...
bool isOperandImmEqual(const MachineOperand &MO, int64_t Value, const MachineRegisterInfo &MRI) const
unsigned getNumExplicitOperands() const
Returns the number of non-implicit operands.
bool hasUnmodeledSideEffects() const
Return true if this instruction has side effects that are not modeled by mayLoad / mayStore...
unsigned constrainRegToClass(MachineRegisterInfo &MRI, const TargetInstrInfo &TII, const RegisterBankInfo &RBI, MachineInstr &InsertPt, unsigned Reg, const TargetRegisterClass &RegClass)
Try to constrain Reg to the specified register class.
Definition: Utils.cpp:29
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
int getOperandConstraint(unsigned OpNum, MCOI::OperandConstraint Constraint) const
Returns the value of the specific constraint if it is set.
Definition: MCInstrDesc.h:187
MachineOperand class - Representation of each machine instruction operand.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
iterator_range< mop_iterator > implicit_operands()
Definition: MachineInstr.h:320
Optional< int64_t > getConstantVRegVal(unsigned VReg, const MachineRegisterInfo &MRI)
Definition: Utils.cpp:106
bool isObviouslySafeToFold(MachineInstr &MI) const
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
Representation of each machine instruction.
Definition: MachineInstr.h:59
static bool isPhysicalRegister(unsigned Reg)
Return true if the specified register number is in the physical register namespace.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
bool mayLoadOrStore(QueryType Type=AnyInBundle) const
Return true if this instruction could possibly read or modify memory.
Definition: MachineInstr.h:623
void setReg(unsigned Reg)
Change the register this operand corresponds to.
#define I(x, y, z)
Definition: MD5.cpp:58
unsigned getReg() const
getReg - Returns the register number.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:73
bool constrainOperandRegToRegClass(MachineInstr &I, unsigned OpIdx, const TargetRegisterClass &RC, const TargetInstrInfo &TII, const TargetRegisterInfo &TRI, const RegisterBankInfo &RBI) const
Constrain a register operand of an instruction I to a specified register class.
#define DEBUG(X)
Definition: Debug.h:118
IRTranslator LLVM IR MI
void tieOperands(unsigned DefIdx, unsigned UseIdx)
Add a tie between the register operands at DefIdx and UseIdx.