2016-07-27 22:31:55 +08:00
|
|
|
//===- llvm/CodeGen/GlobalISel/InstructionSelector.cpp -----------*- C++ -*-==//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// \file
|
|
|
|
/// This file implements the InstructionSelector class.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/CodeGen/GlobalISel/InstructionSelector.h"
|
|
|
|
#include "llvm/CodeGen/GlobalISel/RegisterBankInfo.h"
|
2016-12-23 05:56:19 +08:00
|
|
|
#include "llvm/CodeGen/GlobalISel/Utils.h"
|
2016-07-27 22:31:55 +08:00
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
2017-03-20 00:12:48 +08:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
|
|
|
#include "llvm/IR/Constants.h"
|
2016-07-27 22:31:55 +08:00
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
|
|
|
#include "llvm/Target/TargetRegisterInfo.h"
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "instructionselector"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
InstructionSelector::InstructionSelector() {}
|
|
|
|
|
|
|
|
bool InstructionSelector::constrainSelectedInstRegOperands(
|
|
|
|
MachineInstr &I, const TargetInstrInfo &TII, const TargetRegisterInfo &TRI,
|
|
|
|
const RegisterBankInfo &RBI) const {
|
|
|
|
MachineBasicBlock &MBB = *I.getParent();
|
|
|
|
MachineFunction &MF = *MBB.getParent();
|
|
|
|
MachineRegisterInfo &MRI = MF.getRegInfo();
|
|
|
|
|
|
|
|
for (unsigned OpI = 0, OpE = I.getNumExplicitOperands(); OpI != OpE; ++OpI) {
|
|
|
|
MachineOperand &MO = I.getOperand(OpI);
|
|
|
|
|
2016-10-11 05:50:00 +08:00
|
|
|
// There's nothing to be done on non-register operands.
|
|
|
|
if (!MO.isReg())
|
2016-07-30 00:56:16 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
DEBUG(dbgs() << "Converting operand: " << MO << '\n');
|
|
|
|
assert(MO.isReg() && "Unsupported non-reg operand");
|
2016-07-27 22:31:55 +08:00
|
|
|
|
2016-12-23 05:56:19 +08:00
|
|
|
unsigned Reg = MO.getReg();
|
2016-08-16 22:37:46 +08:00
|
|
|
// Physical registers don't need to be constrained.
|
2016-12-23 05:56:19 +08:00
|
|
|
if (TRI.isPhysicalRegister(Reg))
|
2016-08-16 22:37:46 +08:00
|
|
|
continue;
|
|
|
|
|
2016-12-16 20:54:46 +08:00
|
|
|
// Register operands with a value of 0 (e.g. predicate operands) don't need
|
|
|
|
// to be constrained.
|
2016-12-23 05:56:19 +08:00
|
|
|
if (Reg == 0)
|
2016-12-16 20:54:46 +08:00
|
|
|
continue;
|
|
|
|
|
2016-07-27 22:31:55 +08:00
|
|
|
// If the operand is a vreg, we should constrain its regclass, and only
|
|
|
|
// insert COPYs if that's impossible.
|
2016-12-23 05:56:19 +08:00
|
|
|
// constrainOperandRegClass does that for us.
|
|
|
|
MO.setReg(constrainOperandRegClass(MF, TRI, MRI, TII, RBI, I, I.getDesc(),
|
|
|
|
Reg, OpI));
|
2017-02-22 20:25:09 +08:00
|
|
|
|
2017-04-30 01:30:09 +08:00
|
|
|
// Tie uses to defs as indicated in MCInstrDesc if this hasn't already been
|
|
|
|
// done.
|
2017-02-22 20:25:09 +08:00
|
|
|
if (MO.isUse()) {
|
|
|
|
int DefIdx = I.getDesc().getOperandConstraint(OpI, MCOI::TIED_TO);
|
2017-04-30 01:30:09 +08:00
|
|
|
if (DefIdx != -1 && !I.isRegTiedToUseOperand(DefIdx))
|
2017-02-22 20:25:09 +08:00
|
|
|
I.tieOperands(DefIdx, OpI);
|
|
|
|
}
|
2016-07-27 22:31:55 +08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2017-03-20 00:12:48 +08:00
|
|
|
|
|
|
|
bool InstructionSelector::isOperandImmEqual(
|
|
|
|
const MachineOperand &MO, int64_t Value,
|
|
|
|
const MachineRegisterInfo &MRI) const {
|
|
|
|
|
2017-05-18 18:33:36 +08:00
|
|
|
if (MO.isReg() && MO.getReg())
|
2017-03-28 00:35:27 +08:00
|
|
|
if (auto VRegVal = getConstantVRegVal(MO.getReg(), MRI))
|
|
|
|
return *VRegVal == Value;
|
2017-03-20 00:12:48 +08:00
|
|
|
return false;
|
|
|
|
}
|
[tablegen][globalisel] Add support for nested instruction matching.
Summary:
Lift the restrictions that prevented the tree walking introduced in the
previous change and add support for patterns like:
(G_ADD (G_MUL (G_SEXT $src1), (G_SEXT $src2)), $src3) -> SMADDWrrr $dst, $src1, $src2, $src3
Also adds support for G_SEXT and G_ZEXT to support these cases.
One particular aspect of this that I should draw attention to is that I've
tried to be overly conservative in determining the safety of matches that
involve non-adjacent instructions and multiple basic blocks. This is intended
to be used as a cheap initial check and we may add a more expensive check in
the future. The current rules are:
* Reject if any instruction may load/store (we'd need to check for intervening
memory operations.
* Reject if any instruction has implicit operands.
* Reject if any instruction has unmodelled side-effects.
See isObviouslySafeToFold().
Reviewers: t.p.northover, javed.absar, qcolombet, aditya_nandakumar, ab, rovka
Reviewed By: ab
Subscribers: igorb, dberris, llvm-commits, kristof.beyls
Differential Revision: https://reviews.llvm.org/D30539
llvm-svn: 299430
2017-04-04 21:25:23 +08:00
|
|
|
|
|
|
|
bool InstructionSelector::isObviouslySafeToFold(MachineInstr &MI) const {
|
|
|
|
return !MI.mayLoadOrStore() && !MI.hasUnmodeledSideEffects() &&
|
|
|
|
MI.implicit_operands().begin() == MI.implicit_operands().end();
|
|
|
|
}
|