Michael Gottesman | f87a6ae | 2013-07-17 18:53:29 +0000 | [diff] [blame] | 1 | //===- InstrEmitter.h - Emit MachineInstrs for the SelectionDAG -*- C++ -*--==// |
Dan Gohman | b812077 | 2009-10-10 01:32:21 +0000 | [diff] [blame] | 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 | |
Benjamin Kramer | a7c40ef | 2014-08-13 16:26:38 +0000 | [diff] [blame] | 16 | #ifndef LLVM_LIB_CODEGEN_SELECTIONDAG_INSTREMITTER_H |
| 17 | #define LLVM_LIB_CODEGEN_SELECTIONDAG_INSTREMITTER_H |
Dan Gohman | b812077 | 2009-10-10 01:32:21 +0000 | [diff] [blame] | 18 | |
Dan Gohman | b812077 | 2009-10-10 01:32:21 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/DenseMap.h" |
Chandler Carruth | 802d755 | 2012-12-04 07:12:27 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineBasicBlock.h" |
| 21 | #include "llvm/CodeGen/SelectionDAG.h" |
Dan Gohman | b812077 | 2009-10-10 01:32:21 +0000 | [diff] [blame] | 22 | |
| 23 | namespace llvm { |
| 24 | |
Jakob Stoklund Olesen | b109a7b | 2012-12-20 18:08:09 +0000 | [diff] [blame] | 25 | class MachineInstrBuilder; |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 26 | class MCInstrDesc; |
Dale Johannesen | 10a77ad | 2010-03-06 00:03:23 +0000 | [diff] [blame] | 27 | class SDDbgValue; |
Dan Gohman | b812077 | 2009-10-10 01:32:21 +0000 | [diff] [blame] | 28 | |
Benjamin Kramer | 85b2815 | 2015-07-01 14:55:10 +0000 | [diff] [blame] | 29 | class LLVM_LIBRARY_VISIBILITY InstrEmitter { |
Dan Gohman | b812077 | 2009-10-10 01:32:21 +0000 | [diff] [blame] | 30 | MachineFunction *MF; |
| 31 | MachineRegisterInfo *MRI; |
Dan Gohman | b812077 | 2009-10-10 01:32:21 +0000 | [diff] [blame] | 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 | |
Jakob Stoklund Olesen | b109a7b | 2012-12-20 18:08:09 +0000 | [diff] [blame] | 51 | void CreateVirtualRegisters(SDNode *Node, |
| 52 | MachineInstrBuilder &MIB, |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 53 | const MCInstrDesc &II, |
Dan Gohman | b812077 | 2009-10-10 01:32:21 +0000 | [diff] [blame] | 54 | 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 Olesen | b109a7b | 2012-12-20 18:08:09 +0000 | [diff] [blame] | 65 | void AddRegisterOperand(MachineInstrBuilder &MIB, |
| 66 | SDValue Op, |
Dan Gohman | b812077 | 2009-10-10 01:32:21 +0000 | [diff] [blame] | 67 | unsigned IIOpNum, |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 68 | const MCInstrDesc *II, |
Evan Cheng | 563fe3c | 2010-03-25 01:38:16 +0000 | [diff] [blame] | 69 | DenseMap<SDValue, unsigned> &VRBaseMap, |
Dan Gohman | 2f277c8 | 2010-05-14 22:01:14 +0000 | [diff] [blame] | 70 | bool IsDebug, bool IsClone, bool IsCloned); |
Dan Gohman | b812077 | 2009-10-10 01:32:21 +0000 | [diff] [blame] | 71 | |
| 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 Olesen | b109a7b | 2012-12-20 18:08:09 +0000 | [diff] [blame] | 76 | void AddOperand(MachineInstrBuilder &MIB, |
| 77 | SDValue Op, |
Dan Gohman | b812077 | 2009-10-10 01:32:21 +0000 | [diff] [blame] | 78 | unsigned IIOpNum, |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 79 | const MCInstrDesc *II, |
Evan Cheng | 563fe3c | 2010-03-25 01:38:16 +0000 | [diff] [blame] | 80 | DenseMap<SDValue, unsigned> &VRBaseMap, |
Dan Gohman | 2f277c8 | 2010-05-14 22:01:14 +0000 | [diff] [blame] | 81 | bool IsDebug, bool IsClone, bool IsCloned); |
Dan Gohman | b812077 | 2009-10-10 01:32:21 +0000 | [diff] [blame] | 82 | |
Jakob Stoklund Olesen | f7957a9 | 2011-10-05 20:26:40 +0000 | [diff] [blame] | 83 | /// 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 Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 86 | unsigned ConstrainForSubReg(unsigned VReg, unsigned SubIdx, MVT VT, |
| 87 | const DebugLoc &DL); |
Jakob Stoklund Olesen | f7957a9 | 2011-10-05 20:26:40 +0000 | [diff] [blame] | 88 | |
Dan Gohman | b812077 | 2009-10-10 01:32:21 +0000 | [diff] [blame] | 89 | /// EmitSubregNode - Generate machine code for subreg nodes. |
| 90 | /// |
Dan Gohman | 2f277c8 | 2010-05-14 22:01:14 +0000 | [diff] [blame] | 91 | void EmitSubregNode(SDNode *Node, DenseMap<SDValue, unsigned> &VRBaseMap, |
| 92 | bool IsClone, bool IsCloned); |
Dan Gohman | b812077 | 2009-10-10 01:32:21 +0000 | [diff] [blame] | 93 | |
| 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 Cheng | f869d9a | 2010-05-04 00:22:40 +0000 | [diff] [blame] | 101 | /// EmitRegSequence - Generate machine code for REG_SEQUENCE nodes. |
| 102 | /// |
Dan Gohman | 2f277c8 | 2010-05-14 22:01:14 +0000 | [diff] [blame] | 103 | void EmitRegSequence(SDNode *Node, DenseMap<SDValue, unsigned> &VRBaseMap, |
| 104 | bool IsClone, bool IsCloned); |
Dan Gohman | b812077 | 2009-10-10 01:32:21 +0000 | [diff] [blame] | 105 | public: |
| 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 Cheng | 563fe3c | 2010-03-25 01:38:16 +0000 | [diff] [blame] | 111 | /// EmitDbgValue - Generate machine instruction for a dbg_value node. |
| 112 | /// |
| 113 | MachineInstr *EmitDbgValue(SDDbgValue *SD, |
Dan Gohman | 8acc8f7 | 2010-04-30 19:35:33 +0000 | [diff] [blame] | 114 | DenseMap<SDValue, unsigned> &VRBaseMap); |
Dale Johannesen | 10a77ad | 2010-03-06 00:03:23 +0000 | [diff] [blame] | 115 | |
Dan Gohman | 6b3f32e | 2009-11-16 20:35:59 +0000 | [diff] [blame] | 116 | /// EmitNode - Generate machine code for a node and needed dependencies. |
Dan Gohman | b812077 | 2009-10-10 01:32:21 +0000 | [diff] [blame] | 117 | /// |
| 118 | void EmitNode(SDNode *Node, bool IsClone, bool IsCloned, |
Dan Gohman | 25c1653 | 2010-05-01 00:01:06 +0000 | [diff] [blame] | 119 | DenseMap<SDValue, unsigned> &VRBaseMap) { |
Chris Lattner | e2a504e | 2010-03-25 04:41:16 +0000 | [diff] [blame] | 120 | if (Node->isMachineOpcode()) |
Dan Gohman | 25c1653 | 2010-05-01 00:01:06 +0000 | [diff] [blame] | 121 | EmitMachineNode(Node, IsClone, IsCloned, VRBaseMap); |
Chris Lattner | e2a504e | 2010-03-25 04:41:16 +0000 | [diff] [blame] | 122 | else |
| 123 | EmitSpecialNode(Node, IsClone, IsCloned, VRBaseMap); |
| 124 | } |
Dan Gohman | b812077 | 2009-10-10 01:32:21 +0000 | [diff] [blame] | 125 | |
| 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 Park | a26e93b | 2016-03-26 06:04:55 +0000 | [diff] [blame] | 135 | |
Chris Lattner | e2a504e | 2010-03-25 04:41:16 +0000 | [diff] [blame] | 136 | private: |
| 137 | void EmitMachineNode(SDNode *Node, bool IsClone, bool IsCloned, |
Dan Gohman | 25c1653 | 2010-05-01 00:01:06 +0000 | [diff] [blame] | 138 | DenseMap<SDValue, unsigned> &VRBaseMap); |
Chris Lattner | e2a504e | 2010-03-25 04:41:16 +0000 | [diff] [blame] | 139 | void EmitSpecialNode(SDNode *Node, bool IsClone, bool IsCloned, |
| 140 | DenseMap<SDValue, unsigned> &VRBaseMap); |
Dan Gohman | b812077 | 2009-10-10 01:32:21 +0000 | [diff] [blame] | 141 | }; |
| 142 | |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 143 | } |
Dan Gohman | b812077 | 2009-10-10 01:32:21 +0000 | [diff] [blame] | 144 | |
| 145 | #endif |