blob: 8a8a1bbd18f71dcddfe153987d1f8e890826a44a [file] [log] [blame]
Michael Gottesmanf87a6ae2013-07-17 18:53:29 +00001//===- InstrEmitter.h - Emit MachineInstrs for the SelectionDAG -*- C++ -*--==//
Dan Gohmanb8120772009-10-10 01:32:21 +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//
10// This declares the Emit routines for the SelectionDAG class, which creates
11// MachineInstrs based on the decisions of the SelectionDAG instruction
12// selection.
13//
14//===----------------------------------------------------------------------===//
15
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000016#ifndef LLVM_LIB_CODEGEN_SELECTIONDAG_INSTREMITTER_H
17#define LLVM_LIB_CODEGEN_SELECTIONDAG_INSTREMITTER_H
Dan Gohmanb8120772009-10-10 01:32:21 +000018
Dan Gohmanb8120772009-10-10 01:32:21 +000019#include "llvm/ADT/DenseMap.h"
Chandler Carruth802d7552012-12-04 07:12:27 +000020#include "llvm/CodeGen/MachineBasicBlock.h"
21#include "llvm/CodeGen/SelectionDAG.h"
Dan Gohmanb8120772009-10-10 01:32:21 +000022
23namespace llvm {
24
Jakob Stoklund Olesenb109a7b2012-12-20 18:08:09 +000025class MachineInstrBuilder;
Evan Cheng6cc775f2011-06-28 19:10:37 +000026class MCInstrDesc;
Dale Johannesen10a77ad2010-03-06 00:03:23 +000027class SDDbgValue;
Dan Gohmanb8120772009-10-10 01:32:21 +000028
Benjamin Kramer85b28152015-07-01 14:55:10 +000029class LLVM_LIBRARY_VISIBILITY InstrEmitter {
Dan Gohmanb8120772009-10-10 01:32:21 +000030 MachineFunction *MF;
31 MachineRegisterInfo *MRI;
Dan Gohmanb8120772009-10-10 01:32:21 +000032 const TargetInstrInfo *TII;
33 const TargetRegisterInfo *TRI;
34 const TargetLowering *TLI;
35
36 MachineBasicBlock *MBB;
37 MachineBasicBlock::iterator InsertPos;
38
39 /// EmitCopyFromReg - Generate machine code for an CopyFromReg node or an
40 /// implicit physical register output.
41 void EmitCopyFromReg(SDNode *Node, unsigned ResNo,
42 bool IsClone, bool IsCloned,
43 unsigned SrcReg,
44 DenseMap<SDValue, unsigned> &VRBaseMap);
45
46 /// getDstOfCopyToRegUse - If the only use of the specified result number of
47 /// node is a CopyToReg, return its destination register. Return 0 otherwise.
48 unsigned getDstOfOnlyCopyToRegUse(SDNode *Node,
49 unsigned ResNo) const;
50
Jakob Stoklund Olesenb109a7b2012-12-20 18:08:09 +000051 void CreateVirtualRegisters(SDNode *Node,
52 MachineInstrBuilder &MIB,
Evan Cheng6cc775f2011-06-28 19:10:37 +000053 const MCInstrDesc &II,
Dan Gohmanb8120772009-10-10 01:32:21 +000054 bool IsClone, bool IsCloned,
55 DenseMap<SDValue, unsigned> &VRBaseMap);
56
57 /// getVR - Return the virtual register corresponding to the specified result
58 /// of the specified node.
59 unsigned getVR(SDValue Op,
60 DenseMap<SDValue, unsigned> &VRBaseMap);
61
62 /// AddRegisterOperand - Add the specified register as an operand to the
63 /// specified machine instr. Insert register copies if the register is
64 /// not in the required register class.
Jakob Stoklund Olesenb109a7b2012-12-20 18:08:09 +000065 void AddRegisterOperand(MachineInstrBuilder &MIB,
66 SDValue Op,
Dan Gohmanb8120772009-10-10 01:32:21 +000067 unsigned IIOpNum,
Evan Cheng6cc775f2011-06-28 19:10:37 +000068 const MCInstrDesc *II,
Evan Cheng563fe3c2010-03-25 01:38:16 +000069 DenseMap<SDValue, unsigned> &VRBaseMap,
Dan Gohman2f277c82010-05-14 22:01:14 +000070 bool IsDebug, bool IsClone, bool IsCloned);
Dan Gohmanb8120772009-10-10 01:32:21 +000071
72 /// AddOperand - Add the specified operand to the specified machine instr. II
73 /// specifies the instruction information for the node, and IIOpNum is the
74 /// operand number (in the II) that we are adding. IIOpNum and II are used for
75 /// assertions only.
Jakob Stoklund Olesenb109a7b2012-12-20 18:08:09 +000076 void AddOperand(MachineInstrBuilder &MIB,
77 SDValue Op,
Dan Gohmanb8120772009-10-10 01:32:21 +000078 unsigned IIOpNum,
Evan Cheng6cc775f2011-06-28 19:10:37 +000079 const MCInstrDesc *II,
Evan Cheng563fe3c2010-03-25 01:38:16 +000080 DenseMap<SDValue, unsigned> &VRBaseMap,
Dan Gohman2f277c82010-05-14 22:01:14 +000081 bool IsDebug, bool IsClone, bool IsCloned);
Dan Gohmanb8120772009-10-10 01:32:21 +000082
Jakob Stoklund Olesenf7957a92011-10-05 20:26:40 +000083 /// ConstrainForSubReg - Try to constrain VReg to a register class that
84 /// supports SubIdx sub-registers. Emit a copy if that isn't possible.
85 /// Return the virtual register to use.
Benjamin Kramerbdc49562016-06-12 15:39:02 +000086 unsigned ConstrainForSubReg(unsigned VReg, unsigned SubIdx, MVT VT,
87 const DebugLoc &DL);
Jakob Stoklund Olesenf7957a92011-10-05 20:26:40 +000088
Dan Gohmanb8120772009-10-10 01:32:21 +000089 /// EmitSubregNode - Generate machine code for subreg nodes.
90 ///
Dan Gohman2f277c82010-05-14 22:01:14 +000091 void EmitSubregNode(SDNode *Node, DenseMap<SDValue, unsigned> &VRBaseMap,
92 bool IsClone, bool IsCloned);
Dan Gohmanb8120772009-10-10 01:32:21 +000093
94 /// EmitCopyToRegClassNode - Generate machine code for COPY_TO_REGCLASS nodes.
95 /// COPY_TO_REGCLASS is just a normal copy, except that the destination
96 /// register is constrained to be in a particular register class.
97 ///
98 void EmitCopyToRegClassNode(SDNode *Node,
99 DenseMap<SDValue, unsigned> &VRBaseMap);
100
Evan Chengf869d9a2010-05-04 00:22:40 +0000101 /// EmitRegSequence - Generate machine code for REG_SEQUENCE nodes.
102 ///
Dan Gohman2f277c82010-05-14 22:01:14 +0000103 void EmitRegSequence(SDNode *Node, DenseMap<SDValue, unsigned> &VRBaseMap,
104 bool IsClone, bool IsCloned);
Dan Gohmanb8120772009-10-10 01:32:21 +0000105public:
106 /// CountResults - The results of target nodes have register or immediate
107 /// operands first, then an optional chain, and optional flag operands
108 /// (which do not go into the machine instrs.)
109 static unsigned CountResults(SDNode *Node);
110
Evan Cheng563fe3c2010-03-25 01:38:16 +0000111 /// EmitDbgValue - Generate machine instruction for a dbg_value node.
112 ///
113 MachineInstr *EmitDbgValue(SDDbgValue *SD,
Dan Gohman8acc8f72010-04-30 19:35:33 +0000114 DenseMap<SDValue, unsigned> &VRBaseMap);
Dale Johannesen10a77ad2010-03-06 00:03:23 +0000115
Dan Gohman6b3f32e2009-11-16 20:35:59 +0000116 /// EmitNode - Generate machine code for a node and needed dependencies.
Dan Gohmanb8120772009-10-10 01:32:21 +0000117 ///
118 void EmitNode(SDNode *Node, bool IsClone, bool IsCloned,
Dan Gohman25c16532010-05-01 00:01:06 +0000119 DenseMap<SDValue, unsigned> &VRBaseMap) {
Chris Lattnere2a504e2010-03-25 04:41:16 +0000120 if (Node->isMachineOpcode())
Dan Gohman25c16532010-05-01 00:01:06 +0000121 EmitMachineNode(Node, IsClone, IsCloned, VRBaseMap);
Chris Lattnere2a504e2010-03-25 04:41:16 +0000122 else
123 EmitSpecialNode(Node, IsClone, IsCloned, VRBaseMap);
124 }
Dan Gohmanb8120772009-10-10 01:32:21 +0000125
126 /// getBlock - Return the current basic block.
127 MachineBasicBlock *getBlock() { return MBB; }
128
129 /// getInsertPos - Return the current insertion position.
130 MachineBasicBlock::iterator getInsertPos() { return InsertPos; }
131
132 /// InstrEmitter - Construct an InstrEmitter and set it to start inserting
133 /// at the given position in the given block.
134 InstrEmitter(MachineBasicBlock *mbb, MachineBasicBlock::iterator insertpos);
Junmo Parka26e93b2016-03-26 06:04:55 +0000135
Chris Lattnere2a504e2010-03-25 04:41:16 +0000136private:
137 void EmitMachineNode(SDNode *Node, bool IsClone, bool IsCloned,
Dan Gohman25c16532010-05-01 00:01:06 +0000138 DenseMap<SDValue, unsigned> &VRBaseMap);
Chris Lattnere2a504e2010-03-25 04:41:16 +0000139 void EmitSpecialNode(SDNode *Node, bool IsClone, bool IsCloned,
140 DenseMap<SDValue, unsigned> &VRBaseMap);
Dan Gohmanb8120772009-10-10 01:32:21 +0000141};
142
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000143}
Dan Gohmanb8120772009-10-10 01:32:21 +0000144
145#endif