blob: a9c2203e8400c5badbc308aa27ce81b3dcd9aace [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
Jakob Stoklund Olesen7f6ece82012-12-20 18:08:09 +000025class MachineInstrBuilder;
Evan Chenge837dea2011-06-28 19:10:37 +000026class MCInstrDesc;
Dale Johannesen06a26632010-03-06 00:03:23 +000027class SDDbgValue;
Dan Gohmanbcea8592009-10-10 01:32:21 +000028
29class InstrEmitter {
30 MachineFunction *MF;
31 MachineRegisterInfo *MRI;
32 const TargetMachine *TM;
33 const TargetInstrInfo *TII;
34 const TargetRegisterInfo *TRI;
35 const TargetLowering *TLI;
36
37 MachineBasicBlock *MBB;
38 MachineBasicBlock::iterator InsertPos;
39
40 /// EmitCopyFromReg - Generate machine code for an CopyFromReg node or an
41 /// implicit physical register output.
42 void EmitCopyFromReg(SDNode *Node, unsigned ResNo,
43 bool IsClone, bool IsCloned,
44 unsigned SrcReg,
45 DenseMap<SDValue, unsigned> &VRBaseMap);
46
47 /// getDstOfCopyToRegUse - If the only use of the specified result number of
48 /// node is a CopyToReg, return its destination register. Return 0 otherwise.
49 unsigned getDstOfOnlyCopyToRegUse(SDNode *Node,
50 unsigned ResNo) const;
51
Jakob Stoklund Olesen7f6ece82012-12-20 18:08:09 +000052 void CreateVirtualRegisters(SDNode *Node,
53 MachineInstrBuilder &MIB,
Evan Chenge837dea2011-06-28 19:10:37 +000054 const MCInstrDesc &II,
Dan Gohmanbcea8592009-10-10 01:32:21 +000055 bool IsClone, bool IsCloned,
56 DenseMap<SDValue, unsigned> &VRBaseMap);
57
58 /// getVR - Return the virtual register corresponding to the specified result
59 /// of the specified node.
60 unsigned getVR(SDValue Op,
61 DenseMap<SDValue, unsigned> &VRBaseMap);
62
63 /// AddRegisterOperand - Add the specified register as an operand to the
64 /// specified machine instr. Insert register copies if the register is
65 /// not in the required register class.
Jakob Stoklund Olesen7f6ece82012-12-20 18:08:09 +000066 void AddRegisterOperand(MachineInstrBuilder &MIB,
67 SDValue Op,
Dan Gohmanbcea8592009-10-10 01:32:21 +000068 unsigned IIOpNum,
Evan Chenge837dea2011-06-28 19:10:37 +000069 const MCInstrDesc *II,
Evan Chengbfcb3052010-03-25 01:38:16 +000070 DenseMap<SDValue, unsigned> &VRBaseMap,
Dan Gohman8b3a8f52010-05-14 22:01:14 +000071 bool IsDebug, bool IsClone, bool IsCloned);
Dan Gohmanbcea8592009-10-10 01:32:21 +000072
73 /// AddOperand - Add the specified operand to the specified machine instr. II
74 /// specifies the instruction information for the node, and IIOpNum is the
75 /// operand number (in the II) that we are adding. IIOpNum and II are used for
76 /// assertions only.
Jakob Stoklund Olesen7f6ece82012-12-20 18:08:09 +000077 void AddOperand(MachineInstrBuilder &MIB,
78 SDValue Op,
Dan Gohmanbcea8592009-10-10 01:32:21 +000079 unsigned IIOpNum,
Evan Chenge837dea2011-06-28 19:10:37 +000080 const MCInstrDesc *II,
Evan Chengbfcb3052010-03-25 01:38:16 +000081 DenseMap<SDValue, unsigned> &VRBaseMap,
Dan Gohman8b3a8f52010-05-14 22:01:14 +000082 bool IsDebug, bool IsClone, bool IsCloned);
Dan Gohmanbcea8592009-10-10 01:32:21 +000083
Jakob Stoklund Olesend2ed2d72011-10-05 20:26:40 +000084 /// ConstrainForSubReg - Try to constrain VReg to a register class that
85 /// supports SubIdx sub-registers. Emit a copy if that isn't possible.
86 /// Return the virtual register to use.
87 unsigned ConstrainForSubReg(unsigned VReg, unsigned SubIdx,
Patrik Hagglunda61b17c2012-12-13 06:34:11 +000088 MVT VT, DebugLoc DL);
Jakob Stoklund Olesend2ed2d72011-10-05 20:26:40 +000089
Dan Gohmanbcea8592009-10-10 01:32:21 +000090 /// EmitSubregNode - Generate machine code for subreg nodes.
91 ///
Dan Gohman8b3a8f52010-05-14 22:01:14 +000092 void EmitSubregNode(SDNode *Node, DenseMap<SDValue, unsigned> &VRBaseMap,
93 bool IsClone, bool IsCloned);
Dan Gohmanbcea8592009-10-10 01:32:21 +000094
95 /// EmitCopyToRegClassNode - Generate machine code for COPY_TO_REGCLASS nodes.
96 /// COPY_TO_REGCLASS is just a normal copy, except that the destination
97 /// register is constrained to be in a particular register class.
98 ///
99 void EmitCopyToRegClassNode(SDNode *Node,
100 DenseMap<SDValue, unsigned> &VRBaseMap);
101
Evan Chengba609c82010-05-04 00:22:40 +0000102 /// EmitRegSequence - Generate machine code for REG_SEQUENCE nodes.
103 ///
Dan Gohman8b3a8f52010-05-14 22:01:14 +0000104 void EmitRegSequence(SDNode *Node, DenseMap<SDValue, unsigned> &VRBaseMap,
105 bool IsClone, bool IsCloned);
Dan Gohmanbcea8592009-10-10 01:32:21 +0000106public:
107 /// CountResults - The results of target nodes have register or immediate
108 /// operands first, then an optional chain, and optional flag operands
109 /// (which do not go into the machine instrs.)
110 static unsigned CountResults(SDNode *Node);
111
Evan Chengbfcb3052010-03-25 01:38:16 +0000112 /// EmitDbgValue - Generate machine instruction for a dbg_value node.
113 ///
114 MachineInstr *EmitDbgValue(SDDbgValue *SD,
Dan Gohman891ff8f2010-04-30 19:35:33 +0000115 DenseMap<SDValue, unsigned> &VRBaseMap);
Dale Johannesen06a26632010-03-06 00:03:23 +0000116
Dan Gohman552c0df2009-11-16 20:35:59 +0000117 /// EmitNode - Generate machine code for a node and needed dependencies.
Dan Gohmanbcea8592009-10-10 01:32:21 +0000118 ///
119 void EmitNode(SDNode *Node, bool IsClone, bool IsCloned,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000120 DenseMap<SDValue, unsigned> &VRBaseMap) {
Chris Lattner3d7d07e2010-03-25 04:41:16 +0000121 if (Node->isMachineOpcode())
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000122 EmitMachineNode(Node, IsClone, IsCloned, VRBaseMap);
Chris Lattner3d7d07e2010-03-25 04:41:16 +0000123 else
124 EmitSpecialNode(Node, IsClone, IsCloned, VRBaseMap);
125 }
Dan Gohmanbcea8592009-10-10 01:32:21 +0000126
127 /// getBlock - Return the current basic block.
128 MachineBasicBlock *getBlock() { return MBB; }
129
130 /// getInsertPos - Return the current insertion position.
131 MachineBasicBlock::iterator getInsertPos() { return InsertPos; }
132
133 /// InstrEmitter - Construct an InstrEmitter and set it to start inserting
134 /// at the given position in the given block.
135 InstrEmitter(MachineBasicBlock *mbb, MachineBasicBlock::iterator insertpos);
Chris Lattner3d7d07e2010-03-25 04:41:16 +0000136
137private:
138 void EmitMachineNode(SDNode *Node, bool IsClone, bool IsCloned,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000139 DenseMap<SDValue, unsigned> &VRBaseMap);
Chris Lattner3d7d07e2010-03-25 04:41:16 +0000140 void EmitSpecialNode(SDNode *Node, bool IsClone, bool IsCloned,
141 DenseMap<SDValue, unsigned> &VRBaseMap);
Dan Gohmanbcea8592009-10-10 01:32:21 +0000142};
143
144}
145
146#endif