1 //===-- llvm/CodeGen/SelectionDAGISel.h - Common Base Class------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the SelectionDAGISel class, which is used as the common
11 // base class for SelectionDAG-based instruction selectors.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CODEGEN_SELECTIONDAG_ISEL_H
16 #define LLVM_CODEGEN_SELECTIONDAG_ISEL_H
18 #include "llvm/Pass.h"
19 #include "llvm/Constant.h"
20 #include "llvm/CodeGen/SelectionDAG.h"
21 #include "llvm/CodeGen/SelectionDAGNodes.h"
24 class SelectionDAGLowering;
27 class MachineBasicBlock;
28 class MachineFunction;
31 class FunctionLoweringInfo;
32 class HazardRecognizer;
34 /// SelectionDAGISel - This is the common base class used for SelectionDAG-based
35 /// pattern-matching instruction selectors.
36 class SelectionDAGISel : public FunctionPass {
41 MachineBasicBlock *BB;
42 std::vector<SDNode*> TopOrder;
45 explicit SelectionDAGISel(TargetLowering &tli) : TLI(tli), DAGSize(0) {}
47 TargetLowering &getTargetLowering() { return TLI; }
49 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
51 virtual bool runOnFunction(Function &Fn);
53 unsigned MakeReg(MVT::ValueType VT);
55 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {}
56 virtual void InstructionSelectBasicBlock(SelectionDAG &SD) = 0;
57 virtual void SelectRootInit() {
58 DAGSize = CurDAG->AssignTopologicalOrder(TopOrder);
61 /// SelectInlineAsmMemoryOperand - Select the specified address as a target
62 /// addressing mode, according to the specified constraint code. If this does
63 /// not match or is not implemented, return true. The resultant operands
64 /// (which will appear in the machine instruction) should be added to the
66 virtual bool SelectInlineAsmMemoryOperand(const SDOperand &Op,
68 std::vector<SDOperand> &OutOps,
73 /// CanBeFoldedBy - Returns true if the specific operand node N of U can be
74 /// folded during instruction selection that starts at Root?
75 virtual bool CanBeFoldedBy(SDNode *N, SDNode *U, SDNode *Root) { return true;}
77 /// CreateTargetHazardRecognizer - Return a newly allocated hazard recognizer
78 /// to use for this target when scheduling the DAG.
79 virtual HazardRecognizer *CreateTargetHazardRecognizer();
81 /// CaseBlock - This structure is used to communicate between SDLowering and
82 /// SDISel for the code generation of additional basic blocks needed by multi-
83 /// case switch statements.
85 CaseBlock(ISD::CondCode cc, Value *cmplhs, Value *cmprhs, Value *cmpmiddle,
86 MachineBasicBlock *truebb, MachineBasicBlock *falsebb,
87 MachineBasicBlock *me)
88 : CC(cc), CmpLHS(cmplhs), CmpMHS(cmpmiddle), CmpRHS(cmprhs),
89 TrueBB(truebb), FalseBB(falsebb), ThisBB(me) {}
90 // CC - the condition code to use for the case block's setcc node
92 // CmpLHS/CmpRHS/CmpMHS - The LHS/MHS/RHS of the comparison to emit.
93 // Emit by default LHS op RHS. MHS is used for range comparisons:
94 // If MHS is not null: (LHS <= MHS) and (MHS <= RHS).
95 Value *CmpLHS, *CmpMHS, *CmpRHS;
96 // TrueBB/FalseBB - the block to branch to if the setcc is true/false.
97 MachineBasicBlock *TrueBB, *FalseBB;
98 // ThisBB - the block into which to emit the code for the setcc and branches
99 MachineBasicBlock *ThisBB;
102 JumpTable(unsigned R, unsigned J, MachineBasicBlock *M,
103 MachineBasicBlock *D): Reg(R), JTI(J), MBB(M), Default(D) {};
105 /// Reg - the virtual register containing the index of the jump table entry
108 /// JTI - the JumpTableIndex for this jump table in the function.
110 /// MBB - the MBB into which to emit the code for the indirect jump.
111 MachineBasicBlock *MBB;
112 /// Default - the MBB of the default bb, which is a successor of the range
113 /// check MBB. This is when updating PHI nodes in successors.
114 MachineBasicBlock *Default;
116 struct JumpTableHeader {
117 JumpTableHeader(uint64_t F, uint64_t L, Value* SV, MachineBasicBlock* H,
119 First(F), Last(L), SValue(SV), HeaderBB(H), Emitted(E) {};
123 MachineBasicBlock *HeaderBB;
126 typedef std::pair<JumpTableHeader, JumpTable> JumpTableBlock;
129 BitTestCase(uint64_t M, MachineBasicBlock* T, MachineBasicBlock* Tr):
130 Mask(M), ThisBB(T), TargetBB(Tr) { };
132 MachineBasicBlock* ThisBB;
133 MachineBasicBlock* TargetBB;
136 typedef SmallVector<BitTestCase, 3> BitTestInfo;
138 struct BitTestBlock {
139 BitTestBlock(uint64_t F, uint64_t R, Value* SV,
141 MachineBasicBlock* P, MachineBasicBlock* D,
142 const BitTestInfo& C):
143 First(F), Range(R), SValue(SV), Reg(Rg), Emitted(E),
144 Parent(P), Default(D), Cases(C) { };
150 MachineBasicBlock *Parent;
151 MachineBasicBlock *Default;
155 /// Pick a safe ordering and emit instructions for each target node in the
157 void ScheduleAndEmitDAG(SelectionDAG &DAG);
159 /// SelectInlineAsmMemoryOperands - Calls to this are automatically generated
160 /// by tblgen. Others should not call it.
161 void SelectInlineAsmMemoryOperands(std::vector<SDOperand> &Ops,
164 // Calls to these predicates are generated by tblgen.
165 bool CheckAndMask(SDOperand LHS, ConstantSDNode *RHS, int64_t DesiredMaskS);
166 bool CheckOrMask(SDOperand LHS, ConstantSDNode *RHS, int64_t DesiredMaskS);
169 void SelectBasicBlock(BasicBlock *BB, MachineFunction &MF,
170 FunctionLoweringInfo &FuncInfo);
172 void BuildSelectionDAG(SelectionDAG &DAG, BasicBlock *LLVMBB,
173 std::vector<std::pair<MachineInstr*, unsigned> > &PHINodesToUpdate,
174 FunctionLoweringInfo &FuncInfo);
175 void CodeGenAndEmitDAG(SelectionDAG &DAG);
176 void LowerArguments(BasicBlock *BB, SelectionDAGLowering &SDL,
177 std::vector<SDOperand> &UnorderedChains);
179 /// SwitchCases - Vector of CaseBlock structures used to communicate
180 /// SwitchInst code generation information.
181 std::vector<CaseBlock> SwitchCases;
183 /// JTCases - Vector of JumpTable structures which holds necessary information
184 /// for emitting a jump tables during SwitchInst code generation.
185 std::vector<JumpTableBlock> JTCases;
187 std::vector<BitTestBlock> BitTestCases;
192 #endif /* LLVM_CODEGEN_SELECTIONDAG_ISEL_H */