blob: 8168bd96ae6e546763d5ac853800b3b27845dff7 [file] [log] [blame]
Dan Gohmanbcea8592009-10-10 01:32:21 +00001//===---- InstrEmitter.h - Emit MachineInstrs for the SelectionDAG class ---==//
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//
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
16#ifndef INSTREMITTER_H
17#define INSTREMITTER_H
18
Dan Gohmanbcea8592009-10-10 01:32:21 +000019#include "llvm/ADT/DenseMap.h"
Chandler Carrutha1514e22012-12-04 07:12:27 +000020#include "llvm/CodeGen/MachineBasicBlock.h"
21#include "llvm/CodeGen/SelectionDAG.h"
Dan Gohmanbcea8592009-10-10 01:32:21 +000022
23namespace llvm {
24
Evan Chenge837dea2011-06-28 19:10:37 +000025class MCInstrDesc;
Dale Johannesen06a26632010-03-06 00:03:23 +000026class SDDbgValue;
Dan Gohmanbcea8592009-10-10 01:32:21 +000027
28class InstrEmitter {
29 MachineFunction *MF;
30 MachineRegisterInfo *MRI;
31 const TargetMachine *TM;
32 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
51 void CreateVirtualRegisters(SDNode *Node, MachineInstr *MI,
Evan Chenge837dea2011-06-28 19:10:37 +000052 const MCInstrDesc &II,
Dan Gohmanbcea8592009-10-10 01:32:21 +000053 bool IsClone, bool IsCloned,
54 DenseMap<SDValue, unsigned> &VRBaseMap);
55
56 /// getVR - Return the virtual register corresponding to the specified result
57 /// of the specified node.
58 unsigned getVR(SDValue Op,
59 DenseMap<SDValue, unsigned> &VRBaseMap);
60
61 /// AddRegisterOperand - Add the specified register as an operand to the
62 /// specified machine instr. Insert register copies if the register is
63 /// not in the required register class.
64 void AddRegisterOperand(MachineInstr *MI, SDValue Op,
65 unsigned IIOpNum,
Evan Chenge837dea2011-06-28 19:10:37 +000066 const MCInstrDesc *II,
Evan Chengbfcb3052010-03-25 01:38:16 +000067 DenseMap<SDValue, unsigned> &VRBaseMap,
Dan Gohman8b3a8f52010-05-14 22:01:14 +000068 bool IsDebug, bool IsClone, bool IsCloned);
Dan Gohmanbcea8592009-10-10 01:32:21 +000069
70 /// AddOperand - Add the specified operand to the specified machine instr. II
71 /// specifies the instruction information for the node, and IIOpNum is the
72 /// operand number (in the II) that we are adding. IIOpNum and II are used for
73 /// assertions only.
74 void AddOperand(MachineInstr *MI, SDValue Op,
75 unsigned IIOpNum,
Evan Chenge837dea2011-06-28 19:10:37 +000076 const MCInstrDesc *II,
Evan Chengbfcb3052010-03-25 01:38:16 +000077 DenseMap<SDValue, unsigned> &VRBaseMap,
Dan Gohman8b3a8f52010-05-14 22:01:14 +000078 bool IsDebug, bool IsClone, bool IsCloned);
Dan Gohmanbcea8592009-10-10 01:32:21 +000079
Jakob Stoklund Olesend2ed2d72011-10-05 20:26:40 +000080 /// ConstrainForSubReg - Try to constrain VReg to a register class that
81 /// supports SubIdx sub-registers. Emit a copy if that isn't possible.
82 /// Return the virtual register to use.
83 unsigned ConstrainForSubReg(unsigned VReg, unsigned SubIdx,
Patrik Hagglunda61b17c2012-12-13 06:34:11 +000084 MVT VT, DebugLoc DL);
Jakob Stoklund Olesend2ed2d72011-10-05 20:26:40 +000085
Dan Gohmanbcea8592009-10-10 01:32:21 +000086 /// EmitSubregNode - Generate machine code for subreg nodes.
87 ///
Dan Gohman8b3a8f52010-05-14 22:01:14 +000088 void EmitSubregNode(SDNode *Node, DenseMap<SDValue, unsigned> &VRBaseMap,
89 bool IsClone, bool IsCloned);
Dan Gohmanbcea8592009-10-10 01:32:21 +000090
91 /// EmitCopyToRegClassNode - Generate machine code for COPY_TO_REGCLASS nodes.
92 /// COPY_TO_REGCLASS is just a normal copy, except that the destination
93 /// register is constrained to be in a particular register class.
94 ///
95 void EmitCopyToRegClassNode(SDNode *Node,
96 DenseMap<SDValue, unsigned> &VRBaseMap);
97
Evan Chengba609c82010-05-04 00:22:40 +000098 /// EmitRegSequence - Generate machine code for REG_SEQUENCE nodes.
99 ///
Dan Gohman8b3a8f52010-05-14 22:01:14 +0000100 void EmitRegSequence(SDNode *Node, DenseMap<SDValue, unsigned> &VRBaseMap,
101 bool IsClone, bool IsCloned);
Dan Gohmanbcea8592009-10-10 01:32:21 +0000102public:
103 /// CountResults - The results of target nodes have register or immediate
104 /// operands first, then an optional chain, and optional flag operands
105 /// (which do not go into the machine instrs.)
106 static unsigned CountResults(SDNode *Node);
107
Evan Chengbfcb3052010-03-25 01:38:16 +0000108 /// EmitDbgValue - Generate machine instruction for a dbg_value node.
109 ///
110 MachineInstr *EmitDbgValue(SDDbgValue *SD,
Dan Gohman891ff8f2010-04-30 19:35:33 +0000111 DenseMap<SDValue, unsigned> &VRBaseMap);
Dale Johannesen06a26632010-03-06 00:03:23 +0000112
Dan Gohman552c0df2009-11-16 20:35:59 +0000113 /// EmitNode - Generate machine code for a node and needed dependencies.
Dan Gohmanbcea8592009-10-10 01:32:21 +0000114 ///
115 void EmitNode(SDNode *Node, bool IsClone, bool IsCloned,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000116 DenseMap<SDValue, unsigned> &VRBaseMap) {
Chris Lattner3d7d07e2010-03-25 04:41:16 +0000117 if (Node->isMachineOpcode())
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000118 EmitMachineNode(Node, IsClone, IsCloned, VRBaseMap);
Chris Lattner3d7d07e2010-03-25 04:41:16 +0000119 else
120 EmitSpecialNode(Node, IsClone, IsCloned, VRBaseMap);
121 }
Dan Gohmanbcea8592009-10-10 01:32:21 +0000122
123 /// getBlock - Return the current basic block.
124 MachineBasicBlock *getBlock() { return MBB; }
125
126 /// getInsertPos - Return the current insertion position.
127 MachineBasicBlock::iterator getInsertPos() { return InsertPos; }
128
129 /// InstrEmitter - Construct an InstrEmitter and set it to start inserting
130 /// at the given position in the given block.
131 InstrEmitter(MachineBasicBlock *mbb, MachineBasicBlock::iterator insertpos);
Chris Lattner3d7d07e2010-03-25 04:41:16 +0000132
133private:
134 void EmitMachineNode(SDNode *Node, bool IsClone, bool IsCloned,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000135 DenseMap<SDValue, unsigned> &VRBaseMap);
Chris Lattner3d7d07e2010-03-25 04:41:16 +0000136 void EmitSpecialNode(SDNode *Node, bool IsClone, bool IsCloned,
137 DenseMap<SDValue, unsigned> &VRBaseMap);
Dan Gohmanbcea8592009-10-10 01:32:21 +0000138};
139
140}
141
142#endif