blob: 8f466d913bbbbff090d3d006843dc4bc7dfc9324 [file] [log] [blame]
Dan Gohman2048b852009-11-23 18:04:58 +00001//===-- SelectionDAGBuilder.h - Selection-DAG building --------------------===//
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002//
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 implements routines for translating from LLVM IR into SelectionDAG IR.
11//
12//===----------------------------------------------------------------------===//
13
Dan Gohman2048b852009-11-23 18:04:58 +000014#ifndef SELECTIONDAGBUILDER_H
15#define SELECTIONDAGBUILDER_H
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000016
17#include "llvm/Constants.h"
Owen Anderson0a5372e2009-07-13 04:09:18 +000018#include "llvm/CodeGen/SelectionDAG.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000019#include "llvm/ADT/APInt.h"
20#include "llvm/ADT/DenseMap.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000021#include "llvm/CodeGen/SelectionDAGNodes.h"
Bill Wendling0eb96fd2009-02-03 01:32:22 +000022#include "llvm/CodeGen/ValueTypes.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000023#include "llvm/Support/CallSite.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000024#include "llvm/Support/ErrorHandling.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000025#include <vector>
26#include <set>
27
28namespace llvm {
29
30class AliasAnalysis;
31class AllocaInst;
32class BasicBlock;
33class BitCastInst;
34class BranchInst;
35class CallInst;
Devang Patel4cf81c42010-08-26 23:35:15 +000036class DbgValueInst;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000037class ExtractElementInst;
38class ExtractValueInst;
39class FCmpInst;
40class FPExtInst;
41class FPToSIInst;
42class FPToUIInst;
43class FPTruncInst;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000044class Function;
Dan Gohman6277eb22009-11-23 17:16:22 +000045class FunctionLoweringInfo;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000046class GetElementPtrInst;
47class GCFunctionInfo;
48class ICmpInst;
49class IntToPtrInst;
Chris Lattnerab21db72009-10-28 00:19:10 +000050class IndirectBrInst;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000051class InvokeInst;
52class InsertElementInst;
53class InsertValueInst;
54class Instruction;
55class LoadInst;
56class MachineBasicBlock;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000057class MachineInstr;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000058class MachineRegisterInfo;
Evan Cheng2ad0fcf2010-04-28 23:08:54 +000059class MDNode;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000060class PHINode;
61class PtrToIntInst;
62class ReturnInst;
63class SDISelAsmOperandInfo;
Dale Johannesenbdc09d92010-07-16 00:02:08 +000064class SDDbgValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000065class SExtInst;
66class SelectInst;
67class ShuffleVectorInst;
68class SIToFPInst;
69class StoreInst;
70class SwitchInst;
71class TargetData;
72class TargetLowering;
73class TruncInst;
74class UIToFPInst;
75class UnreachableInst;
76class UnwindInst;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000077class VAArgInst;
78class ZExtInst;
79
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000080//===----------------------------------------------------------------------===//
Dan Gohman2048b852009-11-23 18:04:58 +000081/// SelectionDAGBuilder - This is the common target-independent lowering
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000082/// implementation that is parameterized by a TargetLowering object.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000083///
Dan Gohman2048b852009-11-23 18:04:58 +000084class SelectionDAGBuilder {
Dale Johannesen66978ee2009-01-31 02:22:37 +000085 /// CurDebugLoc - current file + line number. Changes as we build the DAG.
86 DebugLoc CurDebugLoc;
87
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000088 DenseMap<const Value*, SDValue> NodeMap;
Devang Patel9126c0d2010-06-01 19:59:01 +000089
90 /// UnusedArgNodeMap - Maps argument value for unused arguments. This is used
91 /// to preserve debug information for incoming arguments.
92 DenseMap<const Value*, SDValue> UnusedArgNodeMap;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000093
Dale Johannesenbdc09d92010-07-16 00:02:08 +000094 /// DanglingDebugInfo - Helper type for DanglingDebugInfoMap.
95 class DanglingDebugInfo {
Devang Patel4cf81c42010-08-26 23:35:15 +000096 const DbgValueInst* DI;
Dale Johannesenbdc09d92010-07-16 00:02:08 +000097 DebugLoc dl;
98 unsigned SDNodeOrder;
99 public:
100 DanglingDebugInfo() : DI(0), dl(DebugLoc()), SDNodeOrder(0) { }
Devang Patel4cf81c42010-08-26 23:35:15 +0000101 DanglingDebugInfo(const DbgValueInst *di, DebugLoc DL, unsigned SDNO) :
Dale Johannesenbdc09d92010-07-16 00:02:08 +0000102 DI(di), dl(DL), SDNodeOrder(SDNO) { }
Devang Patel4cf81c42010-08-26 23:35:15 +0000103 const DbgValueInst* getDI() { return DI; }
Dale Johannesenbdc09d92010-07-16 00:02:08 +0000104 DebugLoc getdl() { return dl; }
105 unsigned getSDNodeOrder() { return SDNodeOrder; }
106 };
107
108 /// DanglingDebugInfoMap - Keeps track of dbg_values for which we have not
109 /// yet seen the referent. We defer handling these until we do see it.
110 DenseMap<const Value*, DanglingDebugInfo> DanglingDebugInfoMap;
111
Chris Lattner8047d9a2009-12-24 00:37:38 +0000112public:
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000113 /// PendingLoads - Loads are not emitted to the program immediately. We bunch
114 /// them up and then emit token factor nodes when possible. This allows us to
115 /// get simple disambiguation between loads without worrying about alias
116 /// analysis.
117 SmallVector<SDValue, 8> PendingLoads;
Chris Lattner8047d9a2009-12-24 00:37:38 +0000118private:
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000119
120 /// PendingExports - CopyToReg nodes that copy values to virtual registers
121 /// for export to other blocks need to be emitted before any terminator
122 /// instruction, but they have no other ordering requirements. We bunch them
123 /// up and the emit a single tokenfactor for them just before terminator
124 /// instructions.
125 SmallVector<SDValue, 8> PendingExports;
126
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +0000127 /// SDNodeOrder - A unique monotonically increasing number used to order the
128 /// SDNodes we create.
129 unsigned SDNodeOrder;
130
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000131 /// Case - A struct to record the Value for a switch case, and the
132 /// case's target basic block.
133 struct Case {
134 Constant* Low;
135 Constant* High;
136 MachineBasicBlock* BB;
137
138 Case() : Low(0), High(0), BB(0) { }
139 Case(Constant* low, Constant* high, MachineBasicBlock* bb) :
140 Low(low), High(high), BB(bb) { }
Chris Lattnere880efe2009-11-07 07:50:34 +0000141 APInt size() const {
142 const APInt &rHigh = cast<ConstantInt>(High)->getValue();
143 const APInt &rLow = cast<ConstantInt>(Low)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000144 return (rHigh - rLow + 1ULL);
145 }
146 };
147
148 struct CaseBits {
149 uint64_t Mask;
150 MachineBasicBlock* BB;
151 unsigned Bits;
152
153 CaseBits(uint64_t mask, MachineBasicBlock* bb, unsigned bits):
154 Mask(mask), BB(bb), Bits(bits) { }
155 };
156
157 typedef std::vector<Case> CaseVector;
158 typedef std::vector<CaseBits> CaseBitsVector;
159 typedef CaseVector::iterator CaseItr;
160 typedef std::pair<CaseItr, CaseItr> CaseRange;
161
162 /// CaseRec - A struct with ctor used in lowering switches to a binary tree
163 /// of conditional branches.
164 struct CaseRec {
Dan Gohman46510a72010-04-15 01:51:59 +0000165 CaseRec(MachineBasicBlock *bb, const Constant *lt, const Constant *ge,
166 CaseRange r) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000167 CaseBB(bb), LT(lt), GE(ge), Range(r) {}
168
169 /// CaseBB - The MBB in which to emit the compare and branch
170 MachineBasicBlock *CaseBB;
171 /// LT, GE - If nonzero, we know the current case value must be less-than or
172 /// greater-than-or-equal-to these Constants.
Dan Gohman46510a72010-04-15 01:51:59 +0000173 const Constant *LT;
174 const Constant *GE;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000175 /// Range - A pair of iterators representing the range of case values to be
176 /// processed at this point in the binary search tree.
177 CaseRange Range;
178 };
179
180 typedef std::vector<CaseRec> CaseRecVector;
181
182 /// The comparison function for sorting the switch case values in the vector.
183 /// WARNING: Case ranges should be disjoint!
184 struct CaseCmp {
Chris Lattner53334ca2010-01-01 23:37:34 +0000185 bool operator()(const Case &C1, const Case &C2) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000186 assert(isa<ConstantInt>(C1.Low) && isa<ConstantInt>(C2.High));
187 const ConstantInt* CI1 = cast<const ConstantInt>(C1.Low);
188 const ConstantInt* CI2 = cast<const ConstantInt>(C2.High);
189 return CI1->getValue().slt(CI2->getValue());
190 }
191 };
192
193 struct CaseBitsCmp {
Chris Lattner53334ca2010-01-01 23:37:34 +0000194 bool operator()(const CaseBits &C1, const CaseBits &C2) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000195 return C1.Bits > C2.Bits;
196 }
197 };
198
Chris Lattner53334ca2010-01-01 23:37:34 +0000199 size_t Clusterify(CaseVector &Cases, const SwitchInst &SI);
Anton Korobeynikov23218582008-12-23 22:25:27 +0000200
Dan Gohman2048b852009-11-23 18:04:58 +0000201 /// CaseBlock - This structure is used to communicate between
202 /// SelectionDAGBuilder and SDISel for the code generation of additional basic
203 /// blocks needed by multi-case switch statements.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000204 struct CaseBlock {
Dan Gohman46510a72010-04-15 01:51:59 +0000205 CaseBlock(ISD::CondCode cc, const Value *cmplhs, const Value *cmprhs,
206 const Value *cmpmiddle,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000207 MachineBasicBlock *truebb, MachineBasicBlock *falsebb,
208 MachineBasicBlock *me)
209 : CC(cc), CmpLHS(cmplhs), CmpMHS(cmpmiddle), CmpRHS(cmprhs),
210 TrueBB(truebb), FalseBB(falsebb), ThisBB(me) {}
211 // CC - the condition code to use for the case block's setcc node
212 ISD::CondCode CC;
213 // CmpLHS/CmpRHS/CmpMHS - The LHS/MHS/RHS of the comparison to emit.
214 // Emit by default LHS op RHS. MHS is used for range comparisons:
215 // If MHS is not null: (LHS <= MHS) and (MHS <= RHS).
Dan Gohman46510a72010-04-15 01:51:59 +0000216 const Value *CmpLHS, *CmpMHS, *CmpRHS;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000217 // TrueBB/FalseBB - the block to branch to if the setcc is true/false.
218 MachineBasicBlock *TrueBB, *FalseBB;
219 // ThisBB - the block into which to emit the code for the setcc and branches
220 MachineBasicBlock *ThisBB;
221 };
222 struct JumpTable {
223 JumpTable(unsigned R, unsigned J, MachineBasicBlock *M,
224 MachineBasicBlock *D): Reg(R), JTI(J), MBB(M), Default(D) {}
225
226 /// Reg - the virtual register containing the index of the jump table entry
227 //. to jump to.
228 unsigned Reg;
229 /// JTI - the JumpTableIndex for this jump table in the function.
230 unsigned JTI;
231 /// MBB - the MBB into which to emit the code for the indirect jump.
232 MachineBasicBlock *MBB;
233 /// Default - the MBB of the default bb, which is a successor of the range
234 /// check MBB. This is when updating PHI nodes in successors.
235 MachineBasicBlock *Default;
236 };
237 struct JumpTableHeader {
Dan Gohman46510a72010-04-15 01:51:59 +0000238 JumpTableHeader(APInt F, APInt L, const Value *SV, MachineBasicBlock *H,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000239 bool E = false):
240 First(F), Last(L), SValue(SV), HeaderBB(H), Emitted(E) {}
Anton Korobeynikov23218582008-12-23 22:25:27 +0000241 APInt First;
242 APInt Last;
Dan Gohman46510a72010-04-15 01:51:59 +0000243 const Value *SValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000244 MachineBasicBlock *HeaderBB;
245 bool Emitted;
246 };
247 typedef std::pair<JumpTableHeader, JumpTable> JumpTableBlock;
248
249 struct BitTestCase {
250 BitTestCase(uint64_t M, MachineBasicBlock* T, MachineBasicBlock* Tr):
251 Mask(M), ThisBB(T), TargetBB(Tr) { }
252 uint64_t Mask;
Chris Lattner53334ca2010-01-01 23:37:34 +0000253 MachineBasicBlock *ThisBB;
254 MachineBasicBlock *TargetBB;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000255 };
256
257 typedef SmallVector<BitTestCase, 3> BitTestInfo;
258
259 struct BitTestBlock {
Dan Gohman46510a72010-04-15 01:51:59 +0000260 BitTestBlock(APInt F, APInt R, const Value* SV,
Evan Chengd08e5b42011-01-06 01:02:44 +0000261 unsigned Rg, EVT RgVT, bool E,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000262 MachineBasicBlock* P, MachineBasicBlock* D,
263 const BitTestInfo& C):
Evan Chengd08e5b42011-01-06 01:02:44 +0000264 First(F), Range(R), SValue(SV), Reg(Rg), RegVT(RgVT), Emitted(E),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000265 Parent(P), Default(D), Cases(C) { }
Anton Korobeynikov23218582008-12-23 22:25:27 +0000266 APInt First;
267 APInt Range;
Dan Gohman46510a72010-04-15 01:51:59 +0000268 const Value *SValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000269 unsigned Reg;
Evan Chengd08e5b42011-01-06 01:02:44 +0000270 EVT RegVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000271 bool Emitted;
272 MachineBasicBlock *Parent;
273 MachineBasicBlock *Default;
274 BitTestInfo Cases;
275 };
276
277public:
278 // TLI - This is information that describes the available target features we
279 // need for lowering. This indicates when operations are unavailable,
280 // implemented with a libcall, etc.
Dan Gohman55e59c12010-04-19 19:05:59 +0000281 const TargetMachine &TM;
Dan Gohmand858e902010-04-17 15:26:15 +0000282 const TargetLowering &TLI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000283 SelectionDAG &DAG;
284 const TargetData *TD;
285 AliasAnalysis *AA;
286
287 /// SwitchCases - Vector of CaseBlock structures used to communicate
288 /// SwitchInst code generation information.
289 std::vector<CaseBlock> SwitchCases;
290 /// JTCases - Vector of JumpTable structures used to communicate
291 /// SwitchInst code generation information.
292 std::vector<JumpTableBlock> JTCases;
293 /// BitTestCases - Vector of BitTestBlock structures used to communicate
294 /// SwitchInst code generation information.
295 std::vector<BitTestBlock> BitTestCases;
Evan Chengfb2e7522009-09-18 21:02:19 +0000296
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000297 // Emit PHI-node-operand constants only once even if used by multiple
298 // PHI nodes.
Dan Gohman46510a72010-04-15 01:51:59 +0000299 DenseMap<const Constant *, unsigned> ConstantsOut;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000300
301 /// FuncInfo - Information about the function as a whole.
302 ///
303 FunctionLoweringInfo &FuncInfo;
Bill Wendlingdfdacee2009-02-19 21:12:54 +0000304
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000305 /// OptLevel - What optimization level we're generating code for.
Bill Wendlingdfdacee2009-02-19 21:12:54 +0000306 ///
Bill Wendling98a366d2009-04-29 23:29:43 +0000307 CodeGenOpt::Level OptLevel;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000308
309 /// GFI - Garbage collection metadata for the function.
310 GCFunctionInfo *GFI;
311
Dan Gohman98ca4f22009-08-05 01:29:28 +0000312 /// HasTailCall - This is set to true if a call in the current
313 /// block has been translated as a tail call. In this case,
314 /// no subsequent DAG nodes should be created.
315 ///
316 bool HasTailCall;
317
Owen Anderson0a5372e2009-07-13 04:09:18 +0000318 LLVMContext *Context;
319
Dan Gohman55e59c12010-04-19 19:05:59 +0000320 SelectionDAGBuilder(SelectionDAG &dag, FunctionLoweringInfo &funcinfo,
Dan Gohman2048b852009-11-23 18:04:58 +0000321 CodeGenOpt::Level ol)
Dan Gohman55e59c12010-04-19 19:05:59 +0000322 : SDNodeOrder(0), TM(dag.getTarget()), TLI(dag.getTargetLoweringInfo()),
323 DAG(dag), FuncInfo(funcinfo), OptLevel(ol),
Chris Lattnera4f2bb02010-04-02 20:17:23 +0000324 HasTailCall(false), Context(dag.getContext()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000325 }
326
327 void init(GCFunctionInfo *gfi, AliasAnalysis &aa);
328
Dan Gohmanb02b62a2010-04-14 18:24:06 +0000329 /// clear - Clear out the current SelectionDAG and the associated
Dan Gohman2048b852009-11-23 18:04:58 +0000330 /// state and prepare this SelectionDAGBuilder object to be used
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000331 /// for a new block. This doesn't clear out information about
332 /// additional blocks that are needed to complete switch lowering
333 /// or PHI node updating; that information is cleared out as it is
334 /// consumed.
335 void clear();
336
337 /// getRoot - Return the current virtual root of the Selection DAG,
338 /// flushing any PendingLoad items. This must be done before emitting
339 /// a store or any other node that may need to be ordered after any
340 /// prior load instructions.
341 ///
342 SDValue getRoot();
343
344 /// getControlRoot - Similar to getRoot, but instead of flushing all the
345 /// PendingLoad items, flush all the PendingExports items. It is necessary
346 /// to do this before emitting a terminator instruction.
347 ///
348 SDValue getControlRoot();
349
Dale Johannesen66978ee2009-01-31 02:22:37 +0000350 DebugLoc getCurDebugLoc() const { return CurDebugLoc; }
351
Bill Wendling3ea3c242009-12-22 02:10:19 +0000352 unsigned getSDNodeOrder() const { return SDNodeOrder; }
353
Dan Gohman46510a72010-04-15 01:51:59 +0000354 void CopyValueToVirtualRegister(const Value *V, unsigned Reg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000355
Bill Wendling4533cac2010-01-28 21:51:40 +0000356 /// AssignOrderingToNode - Assign an ordering to the node. The order is gotten
357 /// from how the code appeared in the source. The ordering is used by the
358 /// scheduler to effectively turn off scheduling.
359 void AssignOrderingToNode(const SDNode *Node);
360
Dan Gohman46510a72010-04-15 01:51:59 +0000361 void visit(const Instruction &I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000362
Dan Gohman46510a72010-04-15 01:51:59 +0000363 void visit(unsigned Opcode, const User &I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000364
Dale Johannesenbdc09d92010-07-16 00:02:08 +0000365 // resolveDanglingDebugInfo - if we saw an earlier dbg_value referring to V,
366 // generate the debug data structures now that we've seen its definition.
367 void resolveDanglingDebugInfo(const Value *V, SDValue Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000368 SDValue getValue(const Value *V);
Dan Gohman28a17352010-07-01 01:59:43 +0000369 SDValue getNonRegisterValue(const Value *V);
370 SDValue getValueImpl(const Value *V);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000371
372 void setValue(const Value *V, SDValue NewN) {
373 SDValue &N = NodeMap[V];
374 assert(N.getNode() == 0 && "Already set a value for this node!");
375 N = NewN;
376 }
377
Devang Patel9126c0d2010-06-01 19:59:01 +0000378 void setUnusedArgValue(const Value *V, SDValue NewN) {
379 SDValue &N = UnusedArgNodeMap[V];
380 assert(N.getNode() == 0 && "Already set a value for this node!");
381 N = NewN;
382 }
383
Dale Johannesen8e3455b2008-09-24 23:13:09 +0000384 void GetRegistersForValue(SDISelAsmOperandInfo &OpInfo,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000385 std::set<unsigned> &OutputRegs,
386 std::set<unsigned> &InputRegs);
387
Dan Gohman46510a72010-04-15 01:51:59 +0000388 void FindMergedConditions(const Value *Cond, MachineBasicBlock *TBB,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000389 MachineBasicBlock *FBB, MachineBasicBlock *CurBB,
Dan Gohman99be8ae2010-04-19 22:41:47 +0000390 MachineBasicBlock *SwitchBB, unsigned Opc);
Dan Gohman46510a72010-04-15 01:51:59 +0000391 void EmitBranchForMergedCondition(const Value *Cond, MachineBasicBlock *TBB,
Dan Gohmanc2277342008-10-17 21:16:08 +0000392 MachineBasicBlock *FBB,
Dan Gohman99be8ae2010-04-19 22:41:47 +0000393 MachineBasicBlock *CurBB,
394 MachineBasicBlock *SwitchBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000395 bool ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases);
Dan Gohman46510a72010-04-15 01:51:59 +0000396 bool isExportableFromCurrentBlock(const Value *V, const BasicBlock *FromBB);
397 void CopyToExportRegsIfNeeded(const Value *V);
398 void ExportFromCurrentBlock(const Value *V);
399 void LowerCallTo(ImmutableCallSite CS, SDValue Callee, bool IsTailCall,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000400 MachineBasicBlock *LandingPad = NULL);
401
Jakob Stoklund Olesen2622f462010-09-30 19:44:31 +0000402 /// UpdateSplitBlock - When an MBB was split during scheduling, update the
403 /// references that ned to refer to the last resulting block.
404 void UpdateSplitBlock(MachineBasicBlock *First, MachineBasicBlock *Last);
405
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000406private:
407 // Terminator instructions.
Dan Gohman46510a72010-04-15 01:51:59 +0000408 void visitRet(const ReturnInst &I);
409 void visitBr(const BranchInst &I);
410 void visitSwitch(const SwitchInst &I);
411 void visitIndirectBr(const IndirectBrInst &I);
Bill Wendlinga60f0e72010-07-15 23:42:21 +0000412 void visitUnreachable(const UnreachableInst &I) { /* noop */ }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000413
414 // Helpers for visitSwitch
415 bool handleSmallSwitchRange(CaseRec& CR,
416 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +0000417 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +0000418 MachineBasicBlock* Default,
419 MachineBasicBlock *SwitchBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000420 bool handleJTSwitchCase(CaseRec& CR,
421 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +0000422 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +0000423 MachineBasicBlock* Default,
424 MachineBasicBlock *SwitchBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000425 bool handleBTSplitSwitchCase(CaseRec& CR,
426 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +0000427 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +0000428 MachineBasicBlock* Default,
429 MachineBasicBlock *SwitchBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000430 bool handleBitTestsSwitchCase(CaseRec& CR,
431 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +0000432 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +0000433 MachineBasicBlock* Default,
434 MachineBasicBlock *SwitchBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000435public:
Dan Gohman99be8ae2010-04-19 22:41:47 +0000436 void visitSwitchCase(CaseBlock &CB,
437 MachineBasicBlock *SwitchBB);
438 void visitBitTestHeader(BitTestBlock &B, MachineBasicBlock *SwitchBB);
Evan Chengd08e5b42011-01-06 01:02:44 +0000439 void visitBitTestCase(BitTestBlock &BB,
440 MachineBasicBlock* NextMBB,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000441 unsigned Reg,
Dan Gohman99be8ae2010-04-19 22:41:47 +0000442 BitTestCase &B,
443 MachineBasicBlock *SwitchBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000444 void visitJumpTable(JumpTable &JT);
Dan Gohman99be8ae2010-04-19 22:41:47 +0000445 void visitJumpTableHeader(JumpTable &JT, JumpTableHeader &JTH,
446 MachineBasicBlock *SwitchBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000447
448private:
449 // These all get lowered before this pass.
Dan Gohman46510a72010-04-15 01:51:59 +0000450 void visitInvoke(const InvokeInst &I);
451 void visitUnwind(const UnwindInst &I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000452
Dan Gohman46510a72010-04-15 01:51:59 +0000453 void visitBinary(const User &I, unsigned OpCode);
454 void visitShift(const User &I, unsigned Opcode);
455 void visitAdd(const User &I) { visitBinary(I, ISD::ADD); }
456 void visitFAdd(const User &I) { visitBinary(I, ISD::FADD); }
457 void visitSub(const User &I) { visitBinary(I, ISD::SUB); }
458 void visitFSub(const User &I);
459 void visitMul(const User &I) { visitBinary(I, ISD::MUL); }
460 void visitFMul(const User &I) { visitBinary(I, ISD::FMUL); }
461 void visitURem(const User &I) { visitBinary(I, ISD::UREM); }
462 void visitSRem(const User &I) { visitBinary(I, ISD::SREM); }
463 void visitFRem(const User &I) { visitBinary(I, ISD::FREM); }
464 void visitUDiv(const User &I) { visitBinary(I, ISD::UDIV); }
465 void visitSDiv(const User &I) { visitBinary(I, ISD::SDIV); }
466 void visitFDiv(const User &I) { visitBinary(I, ISD::FDIV); }
467 void visitAnd (const User &I) { visitBinary(I, ISD::AND); }
468 void visitOr (const User &I) { visitBinary(I, ISD::OR); }
469 void visitXor (const User &I) { visitBinary(I, ISD::XOR); }
470 void visitShl (const User &I) { visitShift(I, ISD::SHL); }
471 void visitLShr(const User &I) { visitShift(I, ISD::SRL); }
472 void visitAShr(const User &I) { visitShift(I, ISD::SRA); }
473 void visitICmp(const User &I);
474 void visitFCmp(const User &I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000475 // Visit the conversion instructions
Dan Gohman46510a72010-04-15 01:51:59 +0000476 void visitTrunc(const User &I);
477 void visitZExt(const User &I);
478 void visitSExt(const User &I);
479 void visitFPTrunc(const User &I);
480 void visitFPExt(const User &I);
481 void visitFPToUI(const User &I);
482 void visitFPToSI(const User &I);
483 void visitUIToFP(const User &I);
484 void visitSIToFP(const User &I);
485 void visitPtrToInt(const User &I);
486 void visitIntToPtr(const User &I);
487 void visitBitCast(const User &I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000488
Dan Gohman46510a72010-04-15 01:51:59 +0000489 void visitExtractElement(const User &I);
490 void visitInsertElement(const User &I);
491 void visitShuffleVector(const User &I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000492
Dan Gohman46510a72010-04-15 01:51:59 +0000493 void visitExtractValue(const ExtractValueInst &I);
494 void visitInsertValue(const InsertValueInst &I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000495
Dan Gohman46510a72010-04-15 01:51:59 +0000496 void visitGetElementPtr(const User &I);
497 void visitSelect(const User &I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000498
Dan Gohman46510a72010-04-15 01:51:59 +0000499 void visitAlloca(const AllocaInst &I);
500 void visitLoad(const LoadInst &I);
501 void visitStore(const StoreInst &I);
Dan Gohmanba5be5c2010-04-20 15:00:41 +0000502 void visitPHI(const PHINode &I);
Dan Gohman46510a72010-04-15 01:51:59 +0000503 void visitCall(const CallInst &I);
504 bool visitMemCmpCall(const CallInst &I);
Chris Lattner8047d9a2009-12-24 00:37:38 +0000505
Dan Gohman46510a72010-04-15 01:51:59 +0000506 void visitInlineAsm(ImmutableCallSite CS);
507 const char *visitIntrinsicCall(const CallInst &I, unsigned Intrinsic);
508 void visitTargetIntrinsic(const CallInst &I, unsigned Intrinsic);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000509
Dan Gohman46510a72010-04-15 01:51:59 +0000510 void visitPow(const CallInst &I);
511 void visitExp2(const CallInst &I);
512 void visitExp(const CallInst &I);
513 void visitLog(const CallInst &I);
514 void visitLog2(const CallInst &I);
515 void visitLog10(const CallInst &I);
Dale Johannesen601d3c02008-09-05 01:48:15 +0000516
Dan Gohman46510a72010-04-15 01:51:59 +0000517 void visitVAStart(const CallInst &I);
518 void visitVAArg(const VAArgInst &I);
519 void visitVAEnd(const CallInst &I);
520 void visitVACopy(const CallInst &I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000521
Dan Gohman46510a72010-04-15 01:51:59 +0000522 void visitUserOp1(const Instruction &I) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000523 llvm_unreachable("UserOp1 should not exist at instruction selection time!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000524 }
Dan Gohman46510a72010-04-15 01:51:59 +0000525 void visitUserOp2(const Instruction &I) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000526 llvm_unreachable("UserOp2 should not exist at instruction selection time!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000527 }
528
Dan Gohman46510a72010-04-15 01:51:59 +0000529 const char *implVisitBinaryAtomic(const CallInst& I, ISD::NodeType Op);
530 const char *implVisitAluOverflow(const CallInst &I, ISD::NodeType Op);
Dan Gohmanc105a2b2010-04-22 20:55:53 +0000531
532 void HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB);
Evan Cheng2ad0fcf2010-04-28 23:08:54 +0000533
Devang Patelab43add2010-08-25 20:41:24 +0000534 /// EmitFuncArgumentDbgValue - If V is an function argument then create
535 /// corresponding DBG_VALUE machine instruction for it now. At the end of
536 /// instruction selection, they will be inserted to the entry BB.
Devang Patel78a06e52010-08-25 20:39:26 +0000537 bool EmitFuncArgumentDbgValue(const Value *V, MDNode *Variable,
Devang Patel34ca5ed2010-08-31 06:12:08 +0000538 int64_t Offset, const SDValue &N);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000539};
540
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000541} // end namespace llvm
542
543#endif