blob: 606a59680a3d4a618caedf55077da371be44df55 [file] [log] [blame]
Quentin Colombetb4e71182016-12-22 21:56:19 +00001//===- llvm/CodeGen/GlobalISel/Utils.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 This file implements the utility functions used by the GlobalISel
10/// pipeline.
11//===----------------------------------------------------------------------===//
12
13#include "llvm/CodeGen/GlobalISel/Utils.h"
Ahmed Bougachaae9dade2017-02-23 21:05:42 +000014#include "llvm/ADT/Twine.h"
Quentin Colombetb4e71182016-12-22 21:56:19 +000015#include "llvm/CodeGen/GlobalISel/RegisterBankInfo.h"
16#include "llvm/CodeGen/MachineInstr.h"
17#include "llvm/CodeGen/MachineInstrBuilder.h"
Ahmed Bougachaae9dade2017-02-23 21:05:42 +000018#include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
Quentin Colombetb4e71182016-12-22 21:56:19 +000019#include "llvm/CodeGen/MachineRegisterInfo.h"
Ahmed Bougachaae9dade2017-02-23 21:05:42 +000020#include "llvm/CodeGen/TargetPassConfig.h"
Quentin Colombetb4e71182016-12-22 21:56:19 +000021#include "llvm/Target/TargetInstrInfo.h"
22#include "llvm/Target/TargetRegisterInfo.h"
23
24#define DEBUG_TYPE "globalisel-utils"
25
26using namespace llvm;
27
28unsigned llvm::constrainOperandRegClass(
29 const MachineFunction &MF, const TargetRegisterInfo &TRI,
30 MachineRegisterInfo &MRI, const TargetInstrInfo &TII,
31 const RegisterBankInfo &RBI, MachineInstr &InsertPt, const MCInstrDesc &II,
32 unsigned Reg, unsigned OpIdx) {
33 // Assume physical registers are properly constrained.
34 assert(TargetRegisterInfo::isVirtualRegister(Reg) &&
35 "PhysReg not implemented");
36
37 const TargetRegisterClass *RegClass = TII.getRegClass(II, OpIdx, &TRI, MF);
38
39 if (!RBI.constrainGenericRegister(Reg, *RegClass, MRI)) {
40 unsigned NewReg = MRI.createVirtualRegister(RegClass);
41 BuildMI(*InsertPt.getParent(), InsertPt, InsertPt.getDebugLoc(),
42 TII.get(TargetOpcode::COPY), NewReg)
43 .addReg(Reg);
44 return NewReg;
45 }
46
47 return Reg;
48}
Ahmed Bougachaae9dade2017-02-23 21:05:42 +000049
Volkan Keles47debae2017-03-21 10:47:35 +000050bool llvm::isTriviallyDead(const MachineInstr &MI,
51 const MachineRegisterInfo &MRI) {
52 // If we can move an instruction, we can remove it. Otherwise, it has
53 // a side-effect of some sort.
54 bool SawStore = false;
55 if (!MI.isSafeToMove(/*AA=*/nullptr, SawStore))
56 return false;
57
58 // Instructions without side-effects are dead iff they only define dead vregs.
59 for (auto &MO : MI.operands()) {
60 if (!MO.isReg() || !MO.isDef())
61 continue;
62
63 unsigned Reg = MO.getReg();
Ahmed Bougacha15b3e8a2017-03-21 23:42:54 +000064 if (TargetRegisterInfo::isPhysicalRegister(Reg) ||
65 !MRI.use_nodbg_empty(Reg))
Volkan Keles47debae2017-03-21 10:47:35 +000066 return false;
67 }
68 return true;
69}
70
Ahmed Bougachaae9dade2017-02-23 21:05:42 +000071void llvm::reportGISelFailure(MachineFunction &MF, const TargetPassConfig &TPC,
72 MachineOptimizationRemarkEmitter &MORE,
73 MachineOptimizationRemarkMissed &R) {
74 MF.getProperties().set(MachineFunctionProperties::Property::FailedISel);
75
76 // Print the function name explicitly if we don't have a debug location (which
77 // makes the diagnostic less useful) or if we're going to emit a raw error.
78 if (!R.getLocation().isValid() || TPC.isGlobalISelAbortEnabled())
79 R << (" (in function: " + MF.getName() + ")").str();
80
81 if (TPC.isGlobalISelAbortEnabled())
82 report_fatal_error(R.getMsg());
83 else
84 MORE.emit(R);
85}
86
87void llvm::reportGISelFailure(MachineFunction &MF, const TargetPassConfig &TPC,
88 MachineOptimizationRemarkEmitter &MORE,
89 const char *PassName, StringRef Msg,
90 const MachineInstr &MI) {
91 MachineOptimizationRemarkMissed R(PassName, "GISelFailure: ",
92 MI.getDebugLoc(), MI.getParent());
93 R << Msg << ": " << ore::MNV("Inst", MI);
94 reportGISelFailure(MF, TPC, MORE, R);
95}