blob: f1373791a9e84e12c242f894cd953ac9d269f49a [file] [log] [blame]
Eugene Zelenko76bf48d2017-06-26 22:44:03 +00001//===- llvm/CodeGen/GlobalISel/InstructionSelector.cpp --------------------===//
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00002//
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"
Quentin Colombetb4e71182016-12-22 21:56:19 +000014#include "llvm/CodeGen/GlobalISel/Utils.h"
Eugene Zelenko76bf48d2017-06-26 22:44:03 +000015#include "llvm/CodeGen/MachineBasicBlock.h"
16#include "llvm/CodeGen/MachineFunction.h"
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000017#include "llvm/CodeGen/MachineInstr.h"
Eugene Zelenko76bf48d2017-06-26 22:44:03 +000018#include "llvm/CodeGen/MachineOperand.h"
19#include "llvm/MC/MCInstrDesc.h"
20#include "llvm/Support/Debug.h"
21#include "llvm/Support/raw_ostream.h"
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000022#include "llvm/Target/TargetRegisterInfo.h"
Eugene Zelenko76bf48d2017-06-26 22:44:03 +000023#include <cassert>
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000024
25#define DEBUG_TYPE "instructionselector"
26
27using namespace llvm;
28
Daniel Sanders6ab0daa2017-07-04 14:35:06 +000029InstructionSelector::MatcherState::MatcherState(unsigned MaxRenderers)
30 : Renderers(MaxRenderers, nullptr), MIs() {}
31
Eugene Zelenko76bf48d2017-06-26 22:44:03 +000032InstructionSelector::InstructionSelector() = default;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000033
Daniel Sandersa6e2ceb2017-06-20 12:36:34 +000034bool InstructionSelector::constrainOperandRegToRegClass(
35 MachineInstr &I, unsigned OpIdx, const TargetRegisterClass &RC,
36 const TargetInstrInfo &TII, const TargetRegisterInfo &TRI,
37 const RegisterBankInfo &RBI) const {
38 MachineBasicBlock &MBB = *I.getParent();
39 MachineFunction &MF = *MBB.getParent();
40 MachineRegisterInfo &MRI = MF.getRegInfo();
41
Eugene Zelenko76bf48d2017-06-26 22:44:03 +000042 return
43 constrainRegToClass(MRI, TII, RBI, I, I.getOperand(OpIdx).getReg(), RC);
Daniel Sandersa6e2ceb2017-06-20 12:36:34 +000044}
45
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000046bool InstructionSelector::constrainSelectedInstRegOperands(
47 MachineInstr &I, const TargetInstrInfo &TII, const TargetRegisterInfo &TRI,
48 const RegisterBankInfo &RBI) const {
49 MachineBasicBlock &MBB = *I.getParent();
50 MachineFunction &MF = *MBB.getParent();
51 MachineRegisterInfo &MRI = MF.getRegInfo();
52
53 for (unsigned OpI = 0, OpE = I.getNumExplicitOperands(); OpI != OpE; ++OpI) {
54 MachineOperand &MO = I.getOperand(OpI);
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000055
Tim Northoverbdf16242016-10-10 21:50:00 +000056 // There's nothing to be done on non-register operands.
57 if (!MO.isReg())
Ahmed Bougacha7adfac52016-07-29 16:56:16 +000058 continue;
59
60 DEBUG(dbgs() << "Converting operand: " << MO << '\n');
61 assert(MO.isReg() && "Unsupported non-reg operand");
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000062
Quentin Colombetb4e71182016-12-22 21:56:19 +000063 unsigned Reg = MO.getReg();
Ahmed Bougachae4c03ab2016-08-16 14:37:46 +000064 // Physical registers don't need to be constrained.
Quentin Colombetb4e71182016-12-22 21:56:19 +000065 if (TRI.isPhysicalRegister(Reg))
Ahmed Bougachae4c03ab2016-08-16 14:37:46 +000066 continue;
67
Diana Picus812caee2016-12-16 12:54:46 +000068 // Register operands with a value of 0 (e.g. predicate operands) don't need
69 // to be constrained.
Quentin Colombetb4e71182016-12-22 21:56:19 +000070 if (Reg == 0)
Diana Picus812caee2016-12-16 12:54:46 +000071 continue;
72
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000073 // If the operand is a vreg, we should constrain its regclass, and only
74 // insert COPYs if that's impossible.
Quentin Colombetb4e71182016-12-22 21:56:19 +000075 // constrainOperandRegClass does that for us.
76 MO.setReg(constrainOperandRegClass(MF, TRI, MRI, TII, RBI, I, I.getDesc(),
77 Reg, OpI));
Igor Bregerf7359d82017-02-22 12:25:09 +000078
Daniel Sanderse9fdba32017-04-29 17:30:09 +000079 // Tie uses to defs as indicated in MCInstrDesc if this hasn't already been
80 // done.
Igor Bregerf7359d82017-02-22 12:25:09 +000081 if (MO.isUse()) {
82 int DefIdx = I.getDesc().getOperandConstraint(OpI, MCOI::TIED_TO);
Daniel Sanderse9fdba32017-04-29 17:30:09 +000083 if (DefIdx != -1 && !I.isRegTiedToUseOperand(DefIdx))
Igor Bregerf7359d82017-02-22 12:25:09 +000084 I.tieOperands(DefIdx, OpI);
85 }
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000086 }
87 return true;
88}
Ahmed Bougacha7f2d1732017-03-19 16:12:48 +000089
90bool InstructionSelector::isOperandImmEqual(
91 const MachineOperand &MO, int64_t Value,
92 const MachineRegisterInfo &MRI) const {
Daniel Sanders89e93082017-05-18 10:33:36 +000093 if (MO.isReg() && MO.getReg())
Ahmed Bougacha2d29998f2017-03-27 16:35:27 +000094 if (auto VRegVal = getConstantVRegVal(MO.getReg(), MRI))
95 return *VRegVal == Value;
Ahmed Bougacha7f2d1732017-03-19 16:12:48 +000096 return false;
97}
Daniel Sandersbee57392017-04-04 13:25:23 +000098
99bool InstructionSelector::isObviouslySafeToFold(MachineInstr &MI) const {
100 return !MI.mayLoadOrStore() && !MI.hasUnmodeledSideEffects() &&
101 MI.implicit_operands().begin() == MI.implicit_operands().end();
102}