1b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman//===-- FastISel.h - Definition of the FastISel class ---------------------===//
2b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman//
3b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman//                     The LLVM Compiler Infrastructure
4b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman//
5b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman// This file is distributed under the University of Illinois Open Source
6b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman// License. See LICENSE.TXT for details.
7b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman//
8b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman//===----------------------------------------------------------------------===//
9b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman//
10b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman// This file defines the FastISel class.
112ce5bf188dfa4329eb246df6011dd1edde5a5979Owen Anderson//
12b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman//===----------------------------------------------------------------------===//
132ce5bf188dfa4329eb246df6011dd1edde5a5979Owen Anderson
14b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman#ifndef LLVM_CODEGEN_FASTISEL_H
15b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman#define LLVM_CODEGEN_FASTISEL_H
16b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman
17b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman#include "llvm/ADT/DenseMap.h"
18833ec1deed4568410e4fdfa0de9021d32c1479ffDan Gohman#include "llvm/CodeGen/ValueTypes.h"
1984023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman#include "llvm/CodeGen/MachineBasicBlock.h"
20b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman
21b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohmannamespace llvm {
22b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman
230586d91bb3e516d5826826522d9a90ed6ef74d86Dan Gohmanclass AllocaInst;
24066b5d8403483bf3a8bb033b690da318fbc68e79Benjamin Kramerclass Constant;
2510df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohmanclass ConstantFP;
26a4160c3434b08288d1f79f1acbe453d1b9610b22Dan Gohmanclass FunctionLoweringInfo;
27dd5b58ad7be78be90390074f0df138778af5c895Dan Gohmanclass Instruction;
28066b5d8403483bf3a8bb033b690da318fbc68e79Benjamin Kramerclass LoadInst;
29b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohmanclass MachineBasicBlock;
3095267a1e671efc3c14e916b6978bbb15973b4cdcOwen Andersonclass MachineConstantPool;
31b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohmanclass MachineFunction;
32f81eca0ab908fdcf98ae0efaa75acccc8ba40dc2Dan Gohmanclass MachineInstr;
330586d91bb3e516d5826826522d9a90ed6ef74d86Dan Gohmanclass MachineFrameInfo;
34bb466331e7e50d03497ce40ee344870236fd9c32Dan Gohmanclass MachineRegisterInfo;
3583785c80968165b30fcdd111ceb2c28d38bcff86Evan Chengclass TargetData;
36b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohmanclass TargetInstrInfo;
3783785c80968165b30fcdd111ceb2c28d38bcff86Evan Chengclass TargetLowering;
3822bb31103de3337f0bb74c7bee16d1817d4dca14Dan Gohmanclass TargetMachine;
39b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohmanclass TargetRegisterClass;
40db4971259ce94cea26e555e9ade82672a3581f5cDan Gohmanclass TargetRegisterInfo;
41066b5d8403483bf3a8bb033b690da318fbc68e79Benjamin Kramerclass User;
42066b5d8403483bf3a8bb033b690da318fbc68e79Benjamin Kramerclass Value;
43b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman
4440610241d00e219341ff4b7106c5baff08ad407bDan Gohman/// FastISel - This is a fast-path instruction selection class that
4540610241d00e219341ff4b7106c5baff08ad407bDan Gohman/// generates poor code and doesn't support illegal types or non-trivial
4640610241d00e219341ff4b7106c5baff08ad407bDan Gohman/// lowering, but runs quickly.
47b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohmanclass FastISel {
4822bb31103de3337f0bb74c7bee16d1817d4dca14Dan Gohmanprotected:
49104e4ce1629ea84736691bd1ee7867bdf90e8a2eDan Gohman  DenseMap<const Value *, unsigned> LocalValueMap;
50a4160c3434b08288d1f79f1acbe453d1b9610b22Dan Gohman  FunctionLoweringInfo &FuncInfo;
51bb466331e7e50d03497ce40ee344870236fd9c32Dan Gohman  MachineRegisterInfo &MRI;
520586d91bb3e516d5826826522d9a90ed6ef74d86Dan Gohman  MachineFrameInfo &MFI;
530586d91bb3e516d5826826522d9a90ed6ef74d86Dan Gohman  MachineConstantPool &MCP;
549bc96a57206cbebaa9b0ba9979f949eb10c1592cBill Wendling  DebugLoc DL;
5522bb31103de3337f0bb74c7bee16d1817d4dca14Dan Gohman  const TargetMachine &TM;
5683785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng  const TargetData &TD;
57bb466331e7e50d03497ce40ee344870236fd9c32Dan Gohman  const TargetInstrInfo &TII;
5822bb31103de3337f0bb74c7bee16d1817d4dca14Dan Gohman  const TargetLowering &TLI;
59db4971259ce94cea26e555e9ade82672a3581f5cDan Gohman  const TargetRegisterInfo &TRI;
6074af88a6661ad5185924bf39164fb4aa144d32cfIvan Krasin
6174af88a6661ad5185924bf39164fb4aa144d32cfIvan Krasin  /// The position of the last instruction for materializing constants
6274af88a6661ad5185924bf39164fb4aa144d32cfIvan Krasin  /// for use in the current block. It resets to EmitStartPt when it
6374af88a6661ad5185924bf39164fb4aa144d32cfIvan Krasin  /// makes sense (for example, it's usually profitable to avoid function
6474af88a6661ad5185924bf39164fb4aa144d32cfIvan Krasin  /// calls between the definition and the use)
6584023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman  MachineInstr *LastLocalValue;
66b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman
6774af88a6661ad5185924bf39164fb4aa144d32cfIvan Krasin  /// The top most instruction in the current block that is allowed for
6874af88a6661ad5185924bf39164fb4aa144d32cfIvan Krasin  /// emitting local variables. LastLocalValue resets to EmitStartPt when
6974af88a6661ad5185924bf39164fb4aa144d32cfIvan Krasin  /// it makes sense (for example, on function calls)
7074af88a6661ad5185924bf39164fb4aa144d32cfIvan Krasin  MachineInstr *EmitStartPt;
7174af88a6661ad5185924bf39164fb4aa144d32cfIvan Krasin
72b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohmanpublic:
7384023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman  /// getLastLocalValue - Return the position of the last instruction
7484023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman  /// emitted for materializing constants for use in the current block.
7584023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman  MachineInstr *getLastLocalValue() { return LastLocalValue; }
7684023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman
7784023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman  /// setLastLocalValue - Update the position of the last instruction
7884023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman  /// emitted for materializing constants for use in the current block.
7974af88a6661ad5185924bf39164fb4aa144d32cfIvan Krasin  void setLastLocalValue(MachineInstr *I) {
8074af88a6661ad5185924bf39164fb4aa144d32cfIvan Krasin    EmitStartPt = I;
8174af88a6661ad5185924bf39164fb4aa144d32cfIvan Krasin    LastLocalValue = I;
8274af88a6661ad5185924bf39164fb4aa144d32cfIvan Krasin  }
8384023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman
849bc96a57206cbebaa9b0ba9979f949eb10c1592cBill Wendling  /// startNewBlock - Set the current block to which generated machine
859bc96a57206cbebaa9b0ba9979f949eb10c1592cBill Wendling  /// instructions will be appended, and clear the local CSE map.
86241f464d24a6c22721607841069bbeb17b3f71e6Dan Gohman  ///
8784023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman  void startNewBlock();
88241f464d24a6c22721607841069bbeb17b3f71e6Dan Gohman
89390f3ace34855a3d4c9e0adf468976375f8c6dc1Devang Patel  /// getCurDebugLoc() - Return current debug location information.
90390f3ace34855a3d4c9e0adf468976375f8c6dc1Devang Patel  DebugLoc getCurDebugLoc() const { return DL; }
91390f3ace34855a3d4c9e0adf468976375f8c6dc1Devang Patel
923df24e667f04a7003342b534310919abc9c87418Dan Gohman  /// SelectInstruction - Do "fast" instruction selection for the given
933df24e667f04a7003342b534310919abc9c87418Dan Gohman  /// LLVM IR instruction, and append generated machine instructions to
943df24e667f04a7003342b534310919abc9c87418Dan Gohman  /// the current block. Return true if selection was successful.
953df24e667f04a7003342b534310919abc9c87418Dan Gohman  ///
9646510a73e977273ec67747eb34cbdb43f815e451Dan Gohman  bool SelectInstruction(const Instruction *I);
97b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman
98e2d0af4d7802a36e183897cc061747ba88819226Dan Gohman  /// SelectOperator - Do "fast" instruction selection for the given
9940b189e4e257924d90aaf63bf2e12bc7bbca961aDan Gohman  /// LLVM IR operator (Instruction or ConstantExpr), and append
10040b189e4e257924d90aaf63bf2e12bc7bbca961aDan Gohman  /// generated machine instructions to the current block. Return true
10140b189e4e257924d90aaf63bf2e12bc7bbca961aDan Gohman  /// if selection was successful.
10240b189e4e257924d90aaf63bf2e12bc7bbca961aDan Gohman  ///
10346510a73e977273ec67747eb34cbdb43f815e451Dan Gohman  bool SelectOperator(const User *I, unsigned Opcode);
10440b189e4e257924d90aaf63bf2e12bc7bbca961aDan Gohman
1053df24e667f04a7003342b534310919abc9c87418Dan Gohman  /// getRegForValue - Create a virtual register and arrange for it to
1063df24e667f04a7003342b534310919abc9c87418Dan Gohman  /// be assigned the value for the given LLVM value.
10746510a73e977273ec67747eb34cbdb43f815e451Dan Gohman  unsigned getRegForValue(const Value *V);
10899b218218c0ca3ebfdd568ddfeafa07842e9d69dDan Gohman
10959fbc80f6b3b5c71dfb84149f589625f7ed510e3Evan Cheng  /// lookUpRegForValue - Look up the value to see if its value is already
11059fbc80f6b3b5c71dfb84149f589625f7ed510e3Evan Cheng  /// cached in a register. It may be defined by instructions across blocks or
11159fbc80f6b3b5c71dfb84149f589625f7ed510e3Evan Cheng  /// defined locally.
11246510a73e977273ec67747eb34cbdb43f815e451Dan Gohman  unsigned lookUpRegForValue(const Value *V);
11359fbc80f6b3b5c71dfb84149f589625f7ed510e3Evan Cheng
114c8a1a3c426209e9c7b35e279e1578a89edc40af6Dan Gohman  /// getRegForGEPIndex - This is a wrapper around getRegForValue that also
115c8a1a3c426209e9c7b35e279e1578a89edc40af6Dan Gohman  /// takes care of truncating or sign-extending the given getelementptr
116c8a1a3c426209e9c7b35e279e1578a89edc40af6Dan Gohman  /// index value.
117a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman  std::pair<unsigned, bool> getRegForGEPIndex(const Value *V);
118c8a1a3c426209e9c7b35e279e1578a89edc40af6Dan Gohman
119beac75da3784929aee9f0357fc5cd76d49d6c3d7Chris Lattner  /// TryToFoldLoad - The specified machine instr operand is a vreg, and that
120beac75da3784929aee9f0357fc5cd76d49d6c3d7Chris Lattner  /// vreg is being provided by the specified load instruction.  If possible,
121beac75da3784929aee9f0357fc5cd76d49d6c3d7Chris Lattner  /// try to fold the load as an operand to the instruction, returning true if
122beac75da3784929aee9f0357fc5cd76d49d6c3d7Chris Lattner  /// possible.
123beac75da3784929aee9f0357fc5cd76d49d6c3d7Chris Lattner  virtual bool TryToFoldLoad(MachineInstr * /*MI*/, unsigned /*OpNo*/,
124beac75da3784929aee9f0357fc5cd76d49d6c3d7Chris Lattner                             const LoadInst * /*LI*/) {
125beac75da3784929aee9f0357fc5cd76d49d6c3d7Chris Lattner    return false;
126beac75da3784929aee9f0357fc5cd76d49d6c3d7Chris Lattner  }
1272ce5bf188dfa4329eb246df6011dd1edde5a5979Owen Anderson
128beac75da3784929aee9f0357fc5cd76d49d6c3d7Chris Lattner  /// recomputeInsertPt - Reset InsertPt to prepare for inserting instructions
12984023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman  /// into the current block.
13084023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman  void recomputeInsertPt();
13184023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman
132a10b8494a50108482302f6f077d72fbc76d776edDan Gohman  struct SavePoint {
133a10b8494a50108482302f6f077d72fbc76d776edDan Gohman    MachineBasicBlock::iterator InsertPt;
134a10b8494a50108482302f6f077d72fbc76d776edDan Gohman    DebugLoc DL;
135a10b8494a50108482302f6f077d72fbc76d776edDan Gohman  };
136a10b8494a50108482302f6f077d72fbc76d776edDan Gohman
13784023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman  /// enterLocalValueArea - Prepare InsertPt to begin inserting instructions
13884023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman  /// into the local value area and return the old insert position.
139a10b8494a50108482302f6f077d72fbc76d776edDan Gohman  SavePoint enterLocalValueArea();
14084023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman
141a10b8494a50108482302f6f077d72fbc76d776edDan Gohman  /// leaveLocalValueArea - Reset InsertPt to the given old insert position.
142a10b8494a50108482302f6f077d72fbc76d776edDan Gohman  void leaveLocalValueArea(SavePoint Old);
14384023e0fbefc406a4c611d3d64a10df5d3a97dd7Dan Gohman
144cc8430f742b0f1e567292c8a776e94fc1c930b2aDan Gohman  virtual ~FastISel();
145cc8430f742b0f1e567292c8a776e94fc1c930b2aDan Gohman
146b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohmanprotected:
147a4160c3434b08288d1f79f1acbe453d1b9610b22Dan Gohman  explicit FastISel(FunctionLoweringInfo &funcInfo);
148e285a74f7cf9dd3ccf4fe758576cf83301f8a43eDan Gohman
1496e3ff375474c4fd78feb0b8463eb273a23cb4404Dan Gohman  /// TargetSelectInstruction - This method is called by target-independent
1506e3ff375474c4fd78feb0b8463eb273a23cb4404Dan Gohman  /// code when the normal FastISel process fails to select an instruction.
1516e3ff375474c4fd78feb0b8463eb273a23cb4404Dan Gohman  /// This gives targets a chance to emit code for anything that doesn't
1526e3ff375474c4fd78feb0b8463eb273a23cb4404Dan Gohman  /// fit into FastISel's framework. It returns true if it was successful.
1536e3ff375474c4fd78feb0b8463eb273a23cb4404Dan Gohman  ///
1546e3ff375474c4fd78feb0b8463eb273a23cb4404Dan Gohman  virtual bool
15546510a73e977273ec67747eb34cbdb43f815e451Dan Gohman  TargetSelectInstruction(const Instruction *I) = 0;
1566e3ff375474c4fd78feb0b8463eb273a23cb4404Dan Gohman
157bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman  /// FastEmit_r - This method is called by target-independent code
158bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman  /// to request that an instruction with the given type and opcode
159bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman  /// be emitted.
160825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  virtual unsigned FastEmit_(MVT VT,
161825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson                             MVT RetVT,
1627c3ecb6838ef7a2ca306c0f3cd68022f0855ae71Dan Gohman                             unsigned Opcode);
163bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman
164bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman  /// FastEmit_r - This method is called by target-independent code
165bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman  /// to request that an instruction with the given type, opcode, and
166bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman  /// register operand be emitted.
167bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman  ///
168825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  virtual unsigned FastEmit_r(MVT VT,
169825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson                              MVT RetVT,
170a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                              unsigned Opcode,
171a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                              unsigned Op0, bool Op0IsKill);
172bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman
173bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman  /// FastEmit_rr - This method is called by target-independent code
174bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman  /// to request that an instruction with the given type, opcode, and
175bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman  /// register operands be emitted.
176bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman  ///
177825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  virtual unsigned FastEmit_rr(MVT VT,
178825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson                               MVT RetVT,
1797c3ecb6838ef7a2ca306c0f3cd68022f0855ae71Dan Gohman                               unsigned Opcode,
180a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                               unsigned Op0, bool Op0IsKill,
181a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                               unsigned Op1, bool Op1IsKill);
182b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman
18383785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng  /// FastEmit_ri - This method is called by target-independent code
18483785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng  /// to request that an instruction with the given type, opcode, and
18583785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng  /// register and immediate operands be emitted.
18683785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng  ///
187825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  virtual unsigned FastEmit_ri(MVT VT,
188825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson                               MVT RetVT,
1897c3ecb6838ef7a2ca306c0f3cd68022f0855ae71Dan Gohman                               unsigned Opcode,
190a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                               unsigned Op0, bool Op0IsKill,
191a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                               uint64_t Imm);
192d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman
19310df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman  /// FastEmit_rf - This method is called by target-independent code
19410df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman  /// to request that an instruction with the given type, opcode, and
19510df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman  /// register and floating-point immediate operands be emitted.
19610df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman  ///
197825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  virtual unsigned FastEmit_rf(MVT VT,
198825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson                               MVT RetVT,
1997c3ecb6838ef7a2ca306c0f3cd68022f0855ae71Dan Gohman                               unsigned Opcode,
200a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                               unsigned Op0, bool Op0IsKill,
201a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                               const ConstantFP *FPImm);
20210df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman
203d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman  /// FastEmit_rri - This method is called by target-independent code
204d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman  /// to request that an instruction with the given type, opcode, and
205d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman  /// register and immediate operands be emitted.
206d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman  ///
207825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  virtual unsigned FastEmit_rri(MVT VT,
208825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson                                MVT RetVT,
2097c3ecb6838ef7a2ca306c0f3cd68022f0855ae71Dan Gohman                                unsigned Opcode,
210a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                                unsigned Op0, bool Op0IsKill,
211a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                                unsigned Op1, bool Op1IsKill,
212a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                                uint64_t Imm);
21383785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng
21483785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng  /// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries
21583785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng  /// to emit an instruction with an immediate operand using FastEmit_ri.
21683785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng  /// If that fails, it materializes the immediate into a register and try
21783785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng  /// FastEmit_rr instead.
218825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  unsigned FastEmit_ri_(MVT VT,
2197c3ecb6838ef7a2ca306c0f3cd68022f0855ae71Dan Gohman                        unsigned Opcode,
220a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                        unsigned Op0, bool Op0IsKill,
221a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                        uint64_t Imm, MVT ImmType);
2222ce5bf188dfa4329eb246df6011dd1edde5a5979Owen Anderson
2236d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Anderson  /// FastEmit_i - This method is called by target-independent code
2246d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Anderson  /// to request that an instruction with the given type, opcode, and
2256d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Anderson  /// immediate operand be emitted.
226825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  virtual unsigned FastEmit_i(MVT VT,
227825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson                              MVT RetVT,
2287c3ecb6838ef7a2ca306c0f3cd68022f0855ae71Dan Gohman                              unsigned Opcode,
2296d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Anderson                              uint64_t Imm);
23083785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng
23110df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman  /// FastEmit_f - This method is called by target-independent code
23210df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman  /// to request that an instruction with the given type, opcode, and
23310df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman  /// floating-point immediate operand be emitted.
234825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  virtual unsigned FastEmit_f(MVT VT,
235825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson                              MVT RetVT,
2367c3ecb6838ef7a2ca306c0f3cd68022f0855ae71Dan Gohman                              unsigned Opcode,
23746510a73e977273ec67747eb34cbdb43f815e451Dan Gohman                              const ConstantFP *FPImm);
23810df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman
239bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman  /// FastEmitInst_ - Emit a MachineInstr with no operands and a
240bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman  /// result register in the given register class.
241bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman  ///
242b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman  unsigned FastEmitInst_(unsigned MachineInstOpcode,
243b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman                         const TargetRegisterClass *RC);
244bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman
245d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman  /// FastEmitInst_r - Emit a MachineInstr with one register operand
246bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman  /// and a result register in the given register class.
247bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman  ///
248b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman  unsigned FastEmitInst_r(unsigned MachineInstOpcode,
249b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman                          const TargetRegisterClass *RC,
250a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                          unsigned Op0, bool Op0IsKill);
251bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman
252d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman  /// FastEmitInst_rr - Emit a MachineInstr with two register operands
253bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman  /// and a result register in the given register class.
254bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman  ///
255b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman  unsigned FastEmitInst_rr(unsigned MachineInstOpcode,
256b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman                           const TargetRegisterClass *RC,
257a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                           unsigned Op0, bool Op0IsKill,
258a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                           unsigned Op1, bool Op1IsKill);
259bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman
260d71867a8f4b1ab6ab8cc8f5b1a732184ec5bad1bOwen Anderson  /// FastEmitInst_rrr - Emit a MachineInstr with three register operands
261d71867a8f4b1ab6ab8cc8f5b1a732184ec5bad1bOwen Anderson  /// and a result register in the given register class.
262d71867a8f4b1ab6ab8cc8f5b1a732184ec5bad1bOwen Anderson  ///
263d71867a8f4b1ab6ab8cc8f5b1a732184ec5bad1bOwen Anderson  unsigned FastEmitInst_rrr(unsigned MachineInstOpcode,
264d71867a8f4b1ab6ab8cc8f5b1a732184ec5bad1bOwen Anderson                           const TargetRegisterClass *RC,
265d71867a8f4b1ab6ab8cc8f5b1a732184ec5bad1bOwen Anderson                           unsigned Op0, bool Op0IsKill,
266d71867a8f4b1ab6ab8cc8f5b1a732184ec5bad1bOwen Anderson                           unsigned Op1, bool Op1IsKill,
267d71867a8f4b1ab6ab8cc8f5b1a732184ec5bad1bOwen Anderson                           unsigned Op2, bool Op2IsKill);
268d71867a8f4b1ab6ab8cc8f5b1a732184ec5bad1bOwen Anderson
2693728b4ad8bfb42171cc04bb01bc95302fa66810bEric Christopher  /// FastEmitInst_ri - Emit a MachineInstr with a register operand,
2703728b4ad8bfb42171cc04bb01bc95302fa66810bEric Christopher  /// an immediate, and a result register in the given register class.
271d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman  ///
272d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman  unsigned FastEmitInst_ri(unsigned MachineInstOpcode,
273d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman                           const TargetRegisterClass *RC,
274a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                           unsigned Op0, bool Op0IsKill,
275a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                           uint64_t Imm);
276d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman
2772ce5bf188dfa4329eb246df6011dd1edde5a5979Owen Anderson  /// FastEmitInst_rii - Emit a MachineInstr with one register operand
2782ce5bf188dfa4329eb246df6011dd1edde5a5979Owen Anderson  /// and two immediate operands.
2792ce5bf188dfa4329eb246df6011dd1edde5a5979Owen Anderson  ///
2802ce5bf188dfa4329eb246df6011dd1edde5a5979Owen Anderson  unsigned FastEmitInst_rii(unsigned MachineInstOpcode,
2812ce5bf188dfa4329eb246df6011dd1edde5a5979Owen Anderson                           const TargetRegisterClass *RC,
2822ce5bf188dfa4329eb246df6011dd1edde5a5979Owen Anderson                           unsigned Op0, bool Op0IsKill,
2832ce5bf188dfa4329eb246df6011dd1edde5a5979Owen Anderson                           uint64_t Imm1, uint64_t Imm2);
2842ce5bf188dfa4329eb246df6011dd1edde5a5979Owen Anderson
28510df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman  /// FastEmitInst_rf - Emit a MachineInstr with two register operands
28610df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman  /// and a result register in the given register class.
28710df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman  ///
28810df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman  unsigned FastEmitInst_rf(unsigned MachineInstOpcode,
28910df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman                           const TargetRegisterClass *RC,
290a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                           unsigned Op0, bool Op0IsKill,
291a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                           const ConstantFP *FPImm);
29210df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman
293d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman  /// FastEmitInst_rri - Emit a MachineInstr with two register operands,
294d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman  /// an immediate, and a result register in the given register class.
295d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman  ///
296d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman  unsigned FastEmitInst_rri(unsigned MachineInstOpcode,
297d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman                            const TargetRegisterClass *RC,
298a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                            unsigned Op0, bool Op0IsKill,
299a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                            unsigned Op1, bool Op1IsKill,
300a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                            uint64_t Imm);
3012ce5bf188dfa4329eb246df6011dd1edde5a5979Owen Anderson
3026d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Anderson  /// FastEmitInst_i - Emit a MachineInstr with a single immediate
3036d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Anderson  /// operand, and a result register in the given register class.
3046d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Anderson  unsigned FastEmitInst_i(unsigned MachineInstrOpcode,
3056d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Anderson                          const TargetRegisterClass *RC,
3066d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Anderson                          uint64_t Imm);
307d74ea775af55782e3b0d5b10fa7602f8822a2f72Owen Anderson
308d74ea775af55782e3b0d5b10fa7602f8822a2f72Owen Anderson  /// FastEmitInst_ii - Emit a MachineInstr with a two immediate operands.
309d74ea775af55782e3b0d5b10fa7602f8822a2f72Owen Anderson  unsigned FastEmitInst_ii(unsigned MachineInstrOpcode,
310d74ea775af55782e3b0d5b10fa7602f8822a2f72Owen Anderson                          const TargetRegisterClass *RC,
311d74ea775af55782e3b0d5b10fa7602f8822a2f72Owen Anderson                          uint64_t Imm1, uint64_t Imm2);
312d74ea775af55782e3b0d5b10fa7602f8822a2f72Owen Anderson
3138970f00deff00ffce1f35cf00883357e1582daa1Owen Anderson  /// FastEmitInst_extractsubreg - Emit a MachineInstr for an extract_subreg
314536ab130ec95cbb7bf30530251dafa7dfecc8471Evan Cheng  /// from a specified index of a superregister to a specified type.
315825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  unsigned FastEmitInst_extractsubreg(MVT RetVT,
316a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                                      unsigned Op0, bool Op0IsKill,
317a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                                      uint32_t Idx);
3188970f00deff00ffce1f35cf00883357e1582daa1Owen Anderson
31914ea1ec2324cb595f2e035bbf54ddcd483f17c11Dan Gohman  /// FastEmitZExtFromI1 - Emit MachineInstrs to compute the value of Op
32014ea1ec2324cb595f2e035bbf54ddcd483f17c11Dan Gohman  /// with all but the least significant bit set to zero.
321825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  unsigned FastEmitZExtFromI1(MVT VT,
322a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                              unsigned Op0, bool Op0IsKill);
32314ea1ec2324cb595f2e035bbf54ddcd483f17c11Dan Gohman
324d98d6203e429b2d7208b6687931e9079e85e95ecDan Gohman  /// FastEmitBranch - Emit an unconditional branch to the given block,
325d98d6203e429b2d7208b6687931e9079e85e95ecDan Gohman  /// unless it is the immediate (fall-through) successor, and update
326d98d6203e429b2d7208b6687931e9079e85e95ecDan Gohman  /// the CFG.
3273bf912593301152b65accb9d9c37a95172f1df5aStuart Hastings  void FastEmitBranch(MachineBasicBlock *MBB, DebugLoc DL);
328d98d6203e429b2d7208b6687931e9079e85e95ecDan Gohman
329482feb33b2bba677d47bab859d9e1e95d67016bdEli Friedman  void UpdateValueMap(const Value* I, unsigned Reg, unsigned NumRegs = 1);
330ea09f4f4691a0db65772b54fe8163a48c9dce01dEvan Cheng
331c7f72de3b4ef21828ea4780f0693bf0acd04e1c5Dan Gohman  unsigned createResultReg(const TargetRegisterClass *RC);
3322ce5bf188dfa4329eb246df6011dd1edde5a5979Owen Anderson
3332ce5bf188dfa4329eb246df6011dd1edde5a5979Owen Anderson  /// TargetMaterializeConstant - Emit a constant in a register using
3349c7216f984111eb8f1716741bc9039ed86ec4a9bOwen Anderson  /// target-specific logic, such as constant pool loads.
33546510a73e977273ec67747eb34cbdb43f815e451Dan Gohman  virtual unsigned TargetMaterializeConstant(const Constant* C) {
3360586d91bb3e516d5826826522d9a90ed6ef74d86Dan Gohman    return 0;
3370586d91bb3e516d5826826522d9a90ed6ef74d86Dan Gohman  }
3380586d91bb3e516d5826826522d9a90ed6ef74d86Dan Gohman
3390586d91bb3e516d5826826522d9a90ed6ef74d86Dan Gohman  /// TargetMaterializeAlloca - Emit an alloca address in a register using
3400586d91bb3e516d5826826522d9a90ed6ef74d86Dan Gohman  /// target-specific logic.
34146510a73e977273ec67747eb34cbdb43f815e451Dan Gohman  virtual unsigned TargetMaterializeAlloca(const AllocaInst* C) {
34295267a1e671efc3c14e916b6978bbb15973b4cdcOwen Anderson    return 0;
34395267a1e671efc3c14e916b6978bbb15973b4cdcOwen Anderson  }
344c7f72de3b4ef21828ea4780f0693bf0acd04e1c5Dan Gohman
3452790ba8e5a7bb6e00fdac9997d840598fb60271cEli Friedman  virtual unsigned TargetMaterializeFloatZero(const ConstantFP* CF) {
3462790ba8e5a7bb6e00fdac9997d840598fb60271cEli Friedman    return 0;
3472790ba8e5a7bb6e00fdac9997d840598fb60271cEli Friedman  }
3482790ba8e5a7bb6e00fdac9997d840598fb60271cEli Friedman
349ea09f4f4691a0db65772b54fe8163a48c9dce01dEvan Chengprivate:
35046510a73e977273ec67747eb34cbdb43f815e451Dan Gohman  bool SelectBinaryOp(const User *I, unsigned ISDOpcode);
351bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman
35246510a73e977273ec67747eb34cbdb43f815e451Dan Gohman  bool SelectFNeg(const User *I);
3533d45a853db014fdddcdb79424e663dfed5eccbc7Dan Gohman
35446510a73e977273ec67747eb34cbdb43f815e451Dan Gohman  bool SelectGetElementPtr(const User *I);
355763d89343be210eb62a13318ca0cc9321ce46bfbDan Gohman
35646510a73e977273ec67747eb34cbdb43f815e451Dan Gohman  bool SelectCall(const User *I);
35733134c4a75558288d663267c8991f6bd37a530afDan Gohman
35846510a73e977273ec67747eb34cbdb43f815e451Dan Gohman  bool SelectBitCast(const User *I);
3592ce5bf188dfa4329eb246df6011dd1edde5a5979Owen Anderson
36046510a73e977273ec67747eb34cbdb43f815e451Dan Gohman  bool SelectCast(const User *I, unsigned Opcode);
361e8c92dd439581bec7e3516cbdbea74e2e60fe7f0Dan Gohman
3622586b8f9366aed5a1efa44d3f18d095511601642Eli Friedman  bool SelectExtractValue(const User *I);
3632586b8f9366aed5a1efa44d3f18d095511601642Eli Friedman
364cd462d055ffc18a526a9a1d343261d8550e99280Chad Rosier  bool SelectInsertValue(const User *I);
365cd462d055ffc18a526a9a1d343261d8550e99280Chad Rosier
366e8c92dd439581bec7e3516cbdbea74e2e60fe7f0Dan Gohman  /// HandlePHINodesInSuccessorBlocks - Handle PHI nodes in successor blocks.
367e8c92dd439581bec7e3516cbdbea74e2e60fe7f0Dan Gohman  /// Emit code to ensure constants are copied into registers when needed.
368e8c92dd439581bec7e3516cbdbea74e2e60fe7f0Dan Gohman  /// Remember the virtual registers that need to be added to the Machine PHI
369e8c92dd439581bec7e3516cbdbea74e2e60fe7f0Dan Gohman  /// nodes as input.  We cannot just directly add them, because expansion
370e8c92dd439581bec7e3516cbdbea74e2e60fe7f0Dan Gohman  /// might result in multiple MBB's for one BB.  As such, the start of the
371e8c92dd439581bec7e3516cbdbea74e2e60fe7f0Dan Gohman  /// BB might correspond to a different MBB than the end.
372e8c92dd439581bec7e3516cbdbea74e2e60fe7f0Dan Gohman  bool HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB);
3731fdc614bee2a324fcc210d1e46d9b6fca3ca324bDan Gohman
3741fdc614bee2a324fcc210d1e46d9b6fca3ca324bDan Gohman  /// materializeRegForValue - Helper for getRegForVale. This function is
3751fdc614bee2a324fcc210d1e46d9b6fca3ca324bDan Gohman  /// called when the value isn't already available in a register and must
3761fdc614bee2a324fcc210d1e46d9b6fca3ca324bDan Gohman  /// be materialized with new instructions.
3771fdc614bee2a324fcc210d1e46d9b6fca3ca324bDan Gohman  unsigned materializeRegForValue(const Value *V, MVT VT);
378a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman
37974af88a6661ad5185924bf39164fb4aa144d32cfIvan Krasin  /// flushLocalValueMap - clears LocalValueMap and moves the area for the
38074af88a6661ad5185924bf39164fb4aa144d32cfIvan Krasin  /// new local variables to the beginning of the block. It helps to avoid
38174af88a6661ad5185924bf39164fb4aa144d32cfIvan Krasin  /// spilling cached variables across heavy instructions like calls.
38274af88a6661ad5185924bf39164fb4aa144d32cfIvan Krasin  void flushLocalValueMap();
38374af88a6661ad5185924bf39164fb4aa144d32cfIvan Krasin
384a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman  /// hasTrivialKill - Test whether the given value has exactly one use.
385a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman  bool hasTrivialKill(const Value *V) const;
386ae6f2cb1fc520aa56777dab1e7603aee9429f6ddChad Rosier
387ae6f2cb1fc520aa56777dab1e7603aee9429f6ddChad Rosier  /// removeDeadCode - Remove all dead instructions between the I and E.
388ae6f2cb1fc520aa56777dab1e7603aee9429f6ddChad Rosier  void removeDeadCode(MachineBasicBlock::iterator I,
389ae6f2cb1fc520aa56777dab1e7603aee9429f6ddChad Rosier                      MachineBasicBlock::iterator E);
390b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman};
391b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman
392b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman}
393b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman
394b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman#endif
395