blob: 860fc9a4f8b6157360d6c5cfb58d6ec0dc0b4217 [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
Eugene Zelenko76bf48d2017-06-26 22:44:03 +000029InstructionSelector::InstructionSelector() = default;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000030
Daniel Sandersa6e2ceb2017-06-20 12:36:34 +000031bool InstructionSelector::constrainOperandRegToRegClass(
32 MachineInstr &I, unsigned OpIdx, const TargetRegisterClass &RC,
33 const TargetInstrInfo &TII, const TargetRegisterInfo &TRI,
34 const RegisterBankInfo &RBI) const {
35 MachineBasicBlock &MBB = *I.getParent();
36 MachineFunction &MF = *MBB.getParent();
37 MachineRegisterInfo &MRI = MF.getRegInfo();
38
Eugene Zelenko76bf48d2017-06-26 22:44:03 +000039 return
40 constrainRegToClass(MRI, TII, RBI, I, I.getOperand(OpIdx).getReg(), RC);
Daniel Sandersa6e2ceb2017-06-20 12:36:34 +000041}
42
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000043bool InstructionSelector::constrainSelectedInstRegOperands(
44 MachineInstr &I, const TargetInstrInfo &TII, const TargetRegisterInfo &TRI,
45 const RegisterBankInfo &RBI) const {
46 MachineBasicBlock &MBB = *I.getParent();
47 MachineFunction &MF = *MBB.getParent();
48 MachineRegisterInfo &MRI = MF.getRegInfo();
49
50 for (unsigned OpI = 0, OpE = I.getNumExplicitOperands(); OpI != OpE; ++OpI) {
51 MachineOperand &MO = I.getOperand(OpI);
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000052
Tim Northoverbdf16242016-10-10 21:50:00 +000053 // There's nothing to be done on non-register operands.
54 if (!MO.isReg())
Ahmed Bougacha7adfac52016-07-29 16:56:16 +000055 continue;
56
57 DEBUG(dbgs() << "Converting operand: " << MO << '\n');
58 assert(MO.isReg() && "Unsupported non-reg operand");
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000059
Quentin Colombetb4e71182016-12-22 21:56:19 +000060 unsigned Reg = MO.getReg();
Ahmed Bougachae4c03ab2016-08-16 14:37:46 +000061 // Physical registers don't need to be constrained.
Quentin Colombetb4e71182016-12-22 21:56:19 +000062 if (TRI.isPhysicalRegister(Reg))
Ahmed Bougachae4c03ab2016-08-16 14:37:46 +000063 continue;
64
Diana Picus812caee2016-12-16 12:54:46 +000065 // Register operands with a value of 0 (e.g. predicate operands) don't need
66 // to be constrained.
Quentin Colombetb4e71182016-12-22 21:56:19 +000067 if (Reg == 0)
Diana Picus812caee2016-12-16 12:54:46 +000068 continue;
69
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000070 // If the operand is a vreg, we should constrain its regclass, and only
71 // insert COPYs if that's impossible.
Quentin Colombetb4e71182016-12-22 21:56:19 +000072 // constrainOperandRegClass does that for us.
73 MO.setReg(constrainOperandRegClass(MF, TRI, MRI, TII, RBI, I, I.getDesc(),
74 Reg, OpI));
Igor Bregerf7359d82017-02-22 12:25:09 +000075
Daniel Sanderse9fdba32017-04-29 17:30:09 +000076 // Tie uses to defs as indicated in MCInstrDesc if this hasn't already been
77 // done.
Igor Bregerf7359d82017-02-22 12:25:09 +000078 if (MO.isUse()) {
79 int DefIdx = I.getDesc().getOperandConstraint(OpI, MCOI::TIED_TO);
Daniel Sanderse9fdba32017-04-29 17:30:09 +000080 if (DefIdx != -1 && !I.isRegTiedToUseOperand(DefIdx))
Igor Bregerf7359d82017-02-22 12:25:09 +000081 I.tieOperands(DefIdx, OpI);
82 }
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000083 }
84 return true;
85}
Ahmed Bougacha7f2d1732017-03-19 16:12:48 +000086
87bool InstructionSelector::isOperandImmEqual(
88 const MachineOperand &MO, int64_t Value,
89 const MachineRegisterInfo &MRI) const {
Daniel Sanders89e93082017-05-18 10:33:36 +000090 if (MO.isReg() && MO.getReg())
Ahmed Bougacha2d29998f2017-03-27 16:35:27 +000091 if (auto VRegVal = getConstantVRegVal(MO.getReg(), MRI))
92 return *VRegVal == Value;
Ahmed Bougacha7f2d1732017-03-19 16:12:48 +000093 return false;
94}
Daniel Sandersbee57392017-04-04 13:25:23 +000095
96bool InstructionSelector::isObviouslySafeToFold(MachineInstr &MI) const {
97 return !MI.mayLoadOrStore() && !MI.hasUnmodeledSideEffects() &&
98 MI.implicit_operands().begin() == MI.implicit_operands().end();
99}