Michael Gottesman | 3b5c6ea | 2013-07-17 18:53:29 +0000 | [diff] [blame^] | 1 | //===- InstrEmitter.h - Emit MachineInstrs for the SelectionDAG -*- C++ -*--==// |
Dan Gohman | bcea859 | 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 | |
| 16 | #ifndef INSTREMITTER_H |
| 17 | #define INSTREMITTER_H |
| 18 | |
Dan Gohman | bcea859 | 2009-10-10 01:32:21 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/DenseMap.h" |
Chandler Carruth | a1514e2 | 2012-12-04 07:12:27 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineBasicBlock.h" |
| 21 | #include "llvm/CodeGen/SelectionDAG.h" |
Dan Gohman | bcea859 | 2009-10-10 01:32:21 +0000 | [diff] [blame] | 22 | |
| 23 | namespace llvm { |
| 24 | |
Jakob Stoklund Olesen | 7f6ece8 | 2012-12-20 18:08:09 +0000 | [diff] [blame] | 25 | class MachineInstrBuilder; |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 26 | class MCInstrDesc; |
Dale Johannesen | 06a2663 | 2010-03-06 00:03:23 +0000 | [diff] [blame] | 27 | class SDDbgValue; |
Dan Gohman | bcea859 | 2009-10-10 01:32:21 +0000 | [diff] [blame] | 28 | |
| 29 | class 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 Olesen | 7f6ece8 | 2012-12-20 18:08:09 +0000 | [diff] [blame] | 52 | void CreateVirtualRegisters(SDNode *Node, |
| 53 | MachineInstrBuilder &MIB, |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 54 | const MCInstrDesc &II, |
Dan Gohman | bcea859 | 2009-10-10 01:32:21 +0000 | [diff] [blame] | 55 | 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 Olesen | 7f6ece8 | 2012-12-20 18:08:09 +0000 | [diff] [blame] | 66 | void AddRegisterOperand(MachineInstrBuilder &MIB, |
| 67 | SDValue Op, |
Dan Gohman | bcea859 | 2009-10-10 01:32:21 +0000 | [diff] [blame] | 68 | unsigned IIOpNum, |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 69 | const MCInstrDesc *II, |
Evan Cheng | bfcb305 | 2010-03-25 01:38:16 +0000 | [diff] [blame] | 70 | DenseMap<SDValue, unsigned> &VRBaseMap, |
Dan Gohman | 8b3a8f5 | 2010-05-14 22:01:14 +0000 | [diff] [blame] | 71 | bool IsDebug, bool IsClone, bool IsCloned); |
Dan Gohman | bcea859 | 2009-10-10 01:32:21 +0000 | [diff] [blame] | 72 | |
| 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 Olesen | 7f6ece8 | 2012-12-20 18:08:09 +0000 | [diff] [blame] | 77 | void AddOperand(MachineInstrBuilder &MIB, |
| 78 | SDValue Op, |
Dan Gohman | bcea859 | 2009-10-10 01:32:21 +0000 | [diff] [blame] | 79 | unsigned IIOpNum, |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 80 | const MCInstrDesc *II, |
Evan Cheng | bfcb305 | 2010-03-25 01:38:16 +0000 | [diff] [blame] | 81 | DenseMap<SDValue, unsigned> &VRBaseMap, |
Dan Gohman | 8b3a8f5 | 2010-05-14 22:01:14 +0000 | [diff] [blame] | 82 | bool IsDebug, bool IsClone, bool IsCloned); |
Dan Gohman | bcea859 | 2009-10-10 01:32:21 +0000 | [diff] [blame] | 83 | |
Jakob Stoklund Olesen | d2ed2d7 | 2011-10-05 20:26:40 +0000 | [diff] [blame] | 84 | /// 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 Hagglund | a61b17c | 2012-12-13 06:34:11 +0000 | [diff] [blame] | 88 | MVT VT, DebugLoc DL); |
Jakob Stoklund Olesen | d2ed2d7 | 2011-10-05 20:26:40 +0000 | [diff] [blame] | 89 | |
Dan Gohman | bcea859 | 2009-10-10 01:32:21 +0000 | [diff] [blame] | 90 | /// EmitSubregNode - Generate machine code for subreg nodes. |
| 91 | /// |
Dan Gohman | 8b3a8f5 | 2010-05-14 22:01:14 +0000 | [diff] [blame] | 92 | void EmitSubregNode(SDNode *Node, DenseMap<SDValue, unsigned> &VRBaseMap, |
| 93 | bool IsClone, bool IsCloned); |
Dan Gohman | bcea859 | 2009-10-10 01:32:21 +0000 | [diff] [blame] | 94 | |
| 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 Cheng | ba609c8 | 2010-05-04 00:22:40 +0000 | [diff] [blame] | 102 | /// EmitRegSequence - Generate machine code for REG_SEQUENCE nodes. |
| 103 | /// |
Dan Gohman | 8b3a8f5 | 2010-05-14 22:01:14 +0000 | [diff] [blame] | 104 | void EmitRegSequence(SDNode *Node, DenseMap<SDValue, unsigned> &VRBaseMap, |
| 105 | bool IsClone, bool IsCloned); |
Dan Gohman | bcea859 | 2009-10-10 01:32:21 +0000 | [diff] [blame] | 106 | public: |
| 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 Cheng | bfcb305 | 2010-03-25 01:38:16 +0000 | [diff] [blame] | 112 | /// EmitDbgValue - Generate machine instruction for a dbg_value node. |
| 113 | /// |
| 114 | MachineInstr *EmitDbgValue(SDDbgValue *SD, |
Dan Gohman | 891ff8f | 2010-04-30 19:35:33 +0000 | [diff] [blame] | 115 | DenseMap<SDValue, unsigned> &VRBaseMap); |
Dale Johannesen | 06a2663 | 2010-03-06 00:03:23 +0000 | [diff] [blame] | 116 | |
Dan Gohman | 552c0df | 2009-11-16 20:35:59 +0000 | [diff] [blame] | 117 | /// EmitNode - Generate machine code for a node and needed dependencies. |
Dan Gohman | bcea859 | 2009-10-10 01:32:21 +0000 | [diff] [blame] | 118 | /// |
| 119 | void EmitNode(SDNode *Node, bool IsClone, bool IsCloned, |
Dan Gohman | af1d8ca | 2010-05-01 00:01:06 +0000 | [diff] [blame] | 120 | DenseMap<SDValue, unsigned> &VRBaseMap) { |
Chris Lattner | 3d7d07e | 2010-03-25 04:41:16 +0000 | [diff] [blame] | 121 | if (Node->isMachineOpcode()) |
Dan Gohman | af1d8ca | 2010-05-01 00:01:06 +0000 | [diff] [blame] | 122 | EmitMachineNode(Node, IsClone, IsCloned, VRBaseMap); |
Chris Lattner | 3d7d07e | 2010-03-25 04:41:16 +0000 | [diff] [blame] | 123 | else |
| 124 | EmitSpecialNode(Node, IsClone, IsCloned, VRBaseMap); |
| 125 | } |
Dan Gohman | bcea859 | 2009-10-10 01:32:21 +0000 | [diff] [blame] | 126 | |
| 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 Lattner | 3d7d07e | 2010-03-25 04:41:16 +0000 | [diff] [blame] | 136 | |
| 137 | private: |
| 138 | void EmitMachineNode(SDNode *Node, bool IsClone, bool IsCloned, |
Dan Gohman | af1d8ca | 2010-05-01 00:01:06 +0000 | [diff] [blame] | 139 | DenseMap<SDValue, unsigned> &VRBaseMap); |
Chris Lattner | 3d7d07e | 2010-03-25 04:41:16 +0000 | [diff] [blame] | 140 | void EmitSpecialNode(SDNode *Node, bool IsClone, bool IsCloned, |
| 141 | DenseMap<SDValue, unsigned> &VRBaseMap); |
Dan Gohman | bcea859 | 2009-10-10 01:32:21 +0000 | [diff] [blame] | 142 | }; |
| 143 | |
| 144 | } |
| 145 | |
| 146 | #endif |