blob: 5466efd7e90f45831e80e1b12d19b59e3a5a9734 [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"
Ahmed Bougacha7f2d1732017-03-19 16:12:48 +000017#include "llvm/CodeGen/MachineRegisterInfo.h"
18#include "llvm/IR/Constants.h"
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000019#include "llvm/Target/TargetInstrInfo.h"
20#include "llvm/Target/TargetRegisterInfo.h"
21
22#define DEBUG_TYPE "instructionselector"
23
24using namespace llvm;
25
26InstructionSelector::InstructionSelector() {}
27
Daniel Sandersa6e2ceb2017-06-20 12:36:34 +000028bool InstructionSelector::constrainOperandRegToRegClass(
29 MachineInstr &I, unsigned OpIdx, const TargetRegisterClass &RC,
30 const TargetInstrInfo &TII, const TargetRegisterInfo &TRI,
31 const RegisterBankInfo &RBI) const {
32 MachineBasicBlock &MBB = *I.getParent();
33 MachineFunction &MF = *MBB.getParent();
34 MachineRegisterInfo &MRI = MF.getRegInfo();
35
36 return llvm::constrainRegToClass(MRI, TII, RBI, I,
37 I.getOperand(OpIdx).getReg(), RC);
38}
39
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000040bool InstructionSelector::constrainSelectedInstRegOperands(
41 MachineInstr &I, const TargetInstrInfo &TII, const TargetRegisterInfo &TRI,
42 const RegisterBankInfo &RBI) const {
43 MachineBasicBlock &MBB = *I.getParent();
44 MachineFunction &MF = *MBB.getParent();
45 MachineRegisterInfo &MRI = MF.getRegInfo();
46
47 for (unsigned OpI = 0, OpE = I.getNumExplicitOperands(); OpI != OpE; ++OpI) {
48 MachineOperand &MO = I.getOperand(OpI);
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000049
Tim Northoverbdf16242016-10-10 21:50:00 +000050 // There's nothing to be done on non-register operands.
51 if (!MO.isReg())
Ahmed Bougacha7adfac52016-07-29 16:56:16 +000052 continue;
53
54 DEBUG(dbgs() << "Converting operand: " << MO << '\n');
55 assert(MO.isReg() && "Unsupported non-reg operand");
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000056
Quentin Colombetb4e71182016-12-22 21:56:19 +000057 unsigned Reg = MO.getReg();
Ahmed Bougachae4c03ab2016-08-16 14:37:46 +000058 // Physical registers don't need to be constrained.
Quentin Colombetb4e71182016-12-22 21:56:19 +000059 if (TRI.isPhysicalRegister(Reg))
Ahmed Bougachae4c03ab2016-08-16 14:37:46 +000060 continue;
61
Diana Picus812caee2016-12-16 12:54:46 +000062 // Register operands with a value of 0 (e.g. predicate operands) don't need
63 // to be constrained.
Quentin Colombetb4e71182016-12-22 21:56:19 +000064 if (Reg == 0)
Diana Picus812caee2016-12-16 12:54:46 +000065 continue;
66
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000067 // If the operand is a vreg, we should constrain its regclass, and only
68 // insert COPYs if that's impossible.
Quentin Colombetb4e71182016-12-22 21:56:19 +000069 // constrainOperandRegClass does that for us.
70 MO.setReg(constrainOperandRegClass(MF, TRI, MRI, TII, RBI, I, I.getDesc(),
71 Reg, OpI));
Igor Bregerf7359d82017-02-22 12:25:09 +000072
Daniel Sanderse9fdba32017-04-29 17:30:09 +000073 // Tie uses to defs as indicated in MCInstrDesc if this hasn't already been
74 // done.
Igor Bregerf7359d82017-02-22 12:25:09 +000075 if (MO.isUse()) {
76 int DefIdx = I.getDesc().getOperandConstraint(OpI, MCOI::TIED_TO);
Daniel Sanderse9fdba32017-04-29 17:30:09 +000077 if (DefIdx != -1 && !I.isRegTiedToUseOperand(DefIdx))
Igor Bregerf7359d82017-02-22 12:25:09 +000078 I.tieOperands(DefIdx, OpI);
79 }
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000080 }
81 return true;
82}
Ahmed Bougacha7f2d1732017-03-19 16:12:48 +000083
84bool InstructionSelector::isOperandImmEqual(
85 const MachineOperand &MO, int64_t Value,
86 const MachineRegisterInfo &MRI) const {
Ahmed Bougacha7f2d1732017-03-19 16:12:48 +000087
Daniel Sanders89e93082017-05-18 10:33:36 +000088 if (MO.isReg() && MO.getReg())
Ahmed Bougacha2d29998f2017-03-27 16:35:27 +000089 if (auto VRegVal = getConstantVRegVal(MO.getReg(), MRI))
90 return *VRegVal == Value;
Ahmed Bougacha7f2d1732017-03-19 16:12:48 +000091 return false;
92}
Daniel Sandersbee57392017-04-04 13:25:23 +000093
94bool InstructionSelector::isObviouslySafeToFold(MachineInstr &MI) const {
95 return !MI.mayLoadOrStore() && !MI.hasUnmodeledSideEffects() &&
96 MI.implicit_operands().begin() == MI.implicit_operands().end();
97}