blob: 1e1d03924831f595a22515573f970ce81134dbcf [file] [log] [blame]
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001//===- 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"
Quentin Colombetb4e71182016-12-22 21:56:19 +000015#include "llvm/CodeGen/GlobalISel/Utils.h"
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000016#include "llvm/CodeGen/MachineInstr.h"
17#include "llvm/Target/TargetInstrInfo.h"
18#include "llvm/Target/TargetRegisterInfo.h"
19
20#define DEBUG_TYPE "instructionselector"
21
22using namespace llvm;
23
24InstructionSelector::InstructionSelector() {}
25
26bool InstructionSelector::constrainSelectedInstRegOperands(
27 MachineInstr &I, const TargetInstrInfo &TII, const TargetRegisterInfo &TRI,
28 const RegisterBankInfo &RBI) const {
29 MachineBasicBlock &MBB = *I.getParent();
30 MachineFunction &MF = *MBB.getParent();
31 MachineRegisterInfo &MRI = MF.getRegInfo();
32
33 for (unsigned OpI = 0, OpE = I.getNumExplicitOperands(); OpI != OpE; ++OpI) {
34 MachineOperand &MO = I.getOperand(OpI);
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000035
Tim Northoverbdf16242016-10-10 21:50:00 +000036 // There's nothing to be done on non-register operands.
37 if (!MO.isReg())
Ahmed Bougacha7adfac52016-07-29 16:56:16 +000038 continue;
39
40 DEBUG(dbgs() << "Converting operand: " << MO << '\n');
41 assert(MO.isReg() && "Unsupported non-reg operand");
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000042
Quentin Colombetb4e71182016-12-22 21:56:19 +000043 unsigned Reg = MO.getReg();
Ahmed Bougachae4c03ab2016-08-16 14:37:46 +000044 // Physical registers don't need to be constrained.
Quentin Colombetb4e71182016-12-22 21:56:19 +000045 if (TRI.isPhysicalRegister(Reg))
Ahmed Bougachae4c03ab2016-08-16 14:37:46 +000046 continue;
47
Diana Picus812caee2016-12-16 12:54:46 +000048 // Register operands with a value of 0 (e.g. predicate operands) don't need
49 // to be constrained.
Quentin Colombetb4e71182016-12-22 21:56:19 +000050 if (Reg == 0)
Diana Picus812caee2016-12-16 12:54:46 +000051 continue;
52
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000053 // If the operand is a vreg, we should constrain its regclass, and only
54 // insert COPYs if that's impossible.
Quentin Colombetb4e71182016-12-22 21:56:19 +000055 // constrainOperandRegClass does that for us.
56 MO.setReg(constrainOperandRegClass(MF, TRI, MRI, TII, RBI, I, I.getDesc(),
57 Reg, OpI));
Igor Bregerf7359d82017-02-22 12:25:09 +000058
59 // Tie uses to defs as indicated in MCInstrDesc.
60 if (MO.isUse()) {
61 int DefIdx = I.getDesc().getOperandConstraint(OpI, MCOI::TIED_TO);
62 if (DefIdx != -1)
63 I.tieOperands(DefIdx, OpI);
64 }
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000065 }
66 return true;
67}