blob: 521e4d39384a741b79eaef48d2a2440569963f6a [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>
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000026
27namespace llvm {
28
29class AliasAnalysis;
30class AllocaInst;
31class BasicBlock;
32class BitCastInst;
33class BranchInst;
34class CallInst;
Devang Patel4cf81c42010-08-26 23:35:15 +000035class DbgValueInst;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000036class ExtractElementInst;
37class ExtractValueInst;
38class FCmpInst;
39class FPExtInst;
40class FPToSIInst;
41class FPToUIInst;
42class FPTruncInst;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000043class Function;
Dan Gohman6277eb22009-11-23 17:16:22 +000044class FunctionLoweringInfo;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000045class GetElementPtrInst;
46class GCFunctionInfo;
47class ICmpInst;
48class IntToPtrInst;
Chris Lattnerab21db72009-10-28 00:19:10 +000049class IndirectBrInst;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000050class InvokeInst;
51class InsertElementInst;
52class InsertValueInst;
53class Instruction;
54class LoadInst;
55class MachineBasicBlock;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000056class MachineInstr;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000057class MachineRegisterInfo;
Evan Cheng2ad0fcf2010-04-28 23:08:54 +000058class MDNode;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000059class PHINode;
60class PtrToIntInst;
61class ReturnInst;
Dale Johannesenbdc09d92010-07-16 00:02:08 +000062class SDDbgValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000063class SExtInst;
64class SelectInst;
65class ShuffleVectorInst;
66class SIToFPInst;
67class StoreInst;
68class SwitchInst;
69class TargetData;
70class TargetLowering;
71class TruncInst;
72class UIToFPInst;
73class UnreachableInst;
74class UnwindInst;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000075class VAArgInst;
76class ZExtInst;
77
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000078//===----------------------------------------------------------------------===//
Dan Gohman2048b852009-11-23 18:04:58 +000079/// SelectionDAGBuilder - This is the common target-independent lowering
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000080/// implementation that is parameterized by a TargetLowering object.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000081///
Dan Gohman2048b852009-11-23 18:04:58 +000082class SelectionDAGBuilder {
Dale Johannesen66978ee2009-01-31 02:22:37 +000083 /// CurDebugLoc - current file + line number. Changes as we build the DAG.
84 DebugLoc CurDebugLoc;
85
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000086 DenseMap<const Value*, SDValue> NodeMap;
Devang Patel9126c0d2010-06-01 19:59:01 +000087
88 /// UnusedArgNodeMap - Maps argument value for unused arguments. This is used
89 /// to preserve debug information for incoming arguments.
90 DenseMap<const Value*, SDValue> UnusedArgNodeMap;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000091
Dale Johannesenbdc09d92010-07-16 00:02:08 +000092 /// DanglingDebugInfo - Helper type for DanglingDebugInfoMap.
93 class DanglingDebugInfo {
Devang Patel4cf81c42010-08-26 23:35:15 +000094 const DbgValueInst* DI;
Dale Johannesenbdc09d92010-07-16 00:02:08 +000095 DebugLoc dl;
96 unsigned SDNodeOrder;
97 public:
98 DanglingDebugInfo() : DI(0), dl(DebugLoc()), SDNodeOrder(0) { }
Devang Patel4cf81c42010-08-26 23:35:15 +000099 DanglingDebugInfo(const DbgValueInst *di, DebugLoc DL, unsigned SDNO) :
Dale Johannesenbdc09d92010-07-16 00:02:08 +0000100 DI(di), dl(DL), SDNodeOrder(SDNO) { }
Devang Patel4cf81c42010-08-26 23:35:15 +0000101 const DbgValueInst* getDI() { return DI; }
Dale Johannesenbdc09d92010-07-16 00:02:08 +0000102 DebugLoc getdl() { return dl; }
103 unsigned getSDNodeOrder() { return SDNodeOrder; }
104 };
105
106 /// DanglingDebugInfoMap - Keeps track of dbg_values for which we have not
107 /// yet seen the referent. We defer handling these until we do see it.
108 DenseMap<const Value*, DanglingDebugInfo> DanglingDebugInfoMap;
109
Chris Lattner8047d9a2009-12-24 00:37:38 +0000110public:
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000111 /// PendingLoads - Loads are not emitted to the program immediately. We bunch
112 /// them up and then emit token factor nodes when possible. This allows us to
113 /// get simple disambiguation between loads without worrying about alias
114 /// analysis.
115 SmallVector<SDValue, 8> PendingLoads;
Chris Lattner8047d9a2009-12-24 00:37:38 +0000116private:
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000117
118 /// PendingExports - CopyToReg nodes that copy values to virtual registers
119 /// for export to other blocks need to be emitted before any terminator
120 /// instruction, but they have no other ordering requirements. We bunch them
121 /// up and the emit a single tokenfactor for them just before terminator
122 /// instructions.
123 SmallVector<SDValue, 8> PendingExports;
124
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +0000125 /// SDNodeOrder - A unique monotonically increasing number used to order the
126 /// SDNodes we create.
127 unsigned SDNodeOrder;
128
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000129 /// Case - A struct to record the Value for a switch case, and the
130 /// case's target basic block.
131 struct Case {
132 Constant* Low;
133 Constant* High;
134 MachineBasicBlock* BB;
Jakub Staszakc8f34de2011-07-29 22:25:21 +0000135 uint32_t ExtraWeight;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000136
Jakub Staszakc8f34de2011-07-29 22:25:21 +0000137 Case() : Low(0), High(0), BB(0), ExtraWeight(0) { }
138 Case(Constant* low, Constant* high, MachineBasicBlock* bb,
139 uint32_t extraweight) : Low(low), High(high), BB(bb),
140 ExtraWeight(extraweight) { }
141
Chris Lattnere880efe2009-11-07 07:50:34 +0000142 APInt size() const {
143 const APInt &rHigh = cast<ConstantInt>(High)->getValue();
144 const APInt &rLow = cast<ConstantInt>(Low)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000145 return (rHigh - rLow + 1ULL);
146 }
147 };
148
149 struct CaseBits {
150 uint64_t Mask;
151 MachineBasicBlock* BB;
152 unsigned Bits;
153
154 CaseBits(uint64_t mask, MachineBasicBlock* bb, unsigned bits):
155 Mask(mask), BB(bb), Bits(bits) { }
156 };
157
158 typedef std::vector<Case> CaseVector;
159 typedef std::vector<CaseBits> CaseBitsVector;
160 typedef CaseVector::iterator CaseItr;
161 typedef std::pair<CaseItr, CaseItr> CaseRange;
162
163 /// CaseRec - A struct with ctor used in lowering switches to a binary tree
164 /// of conditional branches.
165 struct CaseRec {
Dan Gohman46510a72010-04-15 01:51:59 +0000166 CaseRec(MachineBasicBlock *bb, const Constant *lt, const Constant *ge,
167 CaseRange r) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000168 CaseBB(bb), LT(lt), GE(ge), Range(r) {}
169
170 /// CaseBB - The MBB in which to emit the compare and branch
171 MachineBasicBlock *CaseBB;
172 /// LT, GE - If nonzero, we know the current case value must be less-than or
173 /// greater-than-or-equal-to these Constants.
Dan Gohman46510a72010-04-15 01:51:59 +0000174 const Constant *LT;
175 const Constant *GE;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000176 /// Range - A pair of iterators representing the range of case values to be
177 /// processed at this point in the binary search tree.
178 CaseRange Range;
179 };
180
181 typedef std::vector<CaseRec> CaseRecVector;
182
183 /// The comparison function for sorting the switch case values in the vector.
184 /// WARNING: Case ranges should be disjoint!
185 struct CaseCmp {
Chris Lattner53334ca2010-01-01 23:37:34 +0000186 bool operator()(const Case &C1, const Case &C2) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000187 assert(isa<ConstantInt>(C1.Low) && isa<ConstantInt>(C2.High));
188 const ConstantInt* CI1 = cast<const ConstantInt>(C1.Low);
189 const ConstantInt* CI2 = cast<const ConstantInt>(C2.High);
190 return CI1->getValue().slt(CI2->getValue());
191 }
192 };
193
194 struct CaseBitsCmp {
Chris Lattner53334ca2010-01-01 23:37:34 +0000195 bool operator()(const CaseBits &C1, const CaseBits &C2) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000196 return C1.Bits > C2.Bits;
197 }
198 };
199
Chris Lattner53334ca2010-01-01 23:37:34 +0000200 size_t Clusterify(CaseVector &Cases, const SwitchInst &SI);
Anton Korobeynikov23218582008-12-23 22:25:27 +0000201
Dan Gohman2048b852009-11-23 18:04:58 +0000202 /// CaseBlock - This structure is used to communicate between
203 /// SelectionDAGBuilder and SDISel for the code generation of additional basic
204 /// blocks needed by multi-case switch statements.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000205 struct CaseBlock {
Dan Gohman46510a72010-04-15 01:51:59 +0000206 CaseBlock(ISD::CondCode cc, const Value *cmplhs, const Value *cmprhs,
207 const Value *cmpmiddle,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000208 MachineBasicBlock *truebb, MachineBasicBlock *falsebb,
Jakub Staszakc8f34de2011-07-29 22:25:21 +0000209 MachineBasicBlock *me,
210 uint32_t trueweight = 0, uint32_t falseweight = 0)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000211 : CC(cc), CmpLHS(cmplhs), CmpMHS(cmpmiddle), CmpRHS(cmprhs),
Jakub Staszakc8f34de2011-07-29 22:25:21 +0000212 TrueBB(truebb), FalseBB(falsebb), ThisBB(me),
213 TrueWeight(trueweight), FalseWeight(falseweight) { }
214
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000215 // CC - the condition code to use for the case block's setcc node
216 ISD::CondCode CC;
Jakub Staszakc8f34de2011-07-29 22:25:21 +0000217
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000218 // CmpLHS/CmpRHS/CmpMHS - The LHS/MHS/RHS of the comparison to emit.
219 // Emit by default LHS op RHS. MHS is used for range comparisons:
220 // If MHS is not null: (LHS <= MHS) and (MHS <= RHS).
Dan Gohman46510a72010-04-15 01:51:59 +0000221 const Value *CmpLHS, *CmpMHS, *CmpRHS;
Jakub Staszakc8f34de2011-07-29 22:25:21 +0000222
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000223 // TrueBB/FalseBB - the block to branch to if the setcc is true/false.
224 MachineBasicBlock *TrueBB, *FalseBB;
Jakub Staszakc8f34de2011-07-29 22:25:21 +0000225
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000226 // ThisBB - the block into which to emit the code for the setcc and branches
227 MachineBasicBlock *ThisBB;
Jakub Staszakc8f34de2011-07-29 22:25:21 +0000228
229 // TrueWeight/FalseWeight - branch weights.
230 uint32_t TrueWeight, FalseWeight;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000231 };
Jakub Staszakc8f34de2011-07-29 22:25:21 +0000232
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000233 struct JumpTable {
234 JumpTable(unsigned R, unsigned J, MachineBasicBlock *M,
235 MachineBasicBlock *D): Reg(R), JTI(J), MBB(M), Default(D) {}
236
237 /// Reg - the virtual register containing the index of the jump table entry
238 //. to jump to.
239 unsigned Reg;
240 /// JTI - the JumpTableIndex for this jump table in the function.
241 unsigned JTI;
242 /// MBB - the MBB into which to emit the code for the indirect jump.
243 MachineBasicBlock *MBB;
244 /// Default - the MBB of the default bb, which is a successor of the range
245 /// check MBB. This is when updating PHI nodes in successors.
246 MachineBasicBlock *Default;
247 };
248 struct JumpTableHeader {
Dan Gohman46510a72010-04-15 01:51:59 +0000249 JumpTableHeader(APInt F, APInt L, const Value *SV, MachineBasicBlock *H,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000250 bool E = false):
251 First(F), Last(L), SValue(SV), HeaderBB(H), Emitted(E) {}
Anton Korobeynikov23218582008-12-23 22:25:27 +0000252 APInt First;
253 APInt Last;
Dan Gohman46510a72010-04-15 01:51:59 +0000254 const Value *SValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000255 MachineBasicBlock *HeaderBB;
256 bool Emitted;
257 };
258 typedef std::pair<JumpTableHeader, JumpTable> JumpTableBlock;
259
260 struct BitTestCase {
261 BitTestCase(uint64_t M, MachineBasicBlock* T, MachineBasicBlock* Tr):
262 Mask(M), ThisBB(T), TargetBB(Tr) { }
263 uint64_t Mask;
Chris Lattner53334ca2010-01-01 23:37:34 +0000264 MachineBasicBlock *ThisBB;
265 MachineBasicBlock *TargetBB;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000266 };
267
268 typedef SmallVector<BitTestCase, 3> BitTestInfo;
269
270 struct BitTestBlock {
Dan Gohman46510a72010-04-15 01:51:59 +0000271 BitTestBlock(APInt F, APInt R, const Value* SV,
Evan Chengd08e5b42011-01-06 01:02:44 +0000272 unsigned Rg, EVT RgVT, bool E,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000273 MachineBasicBlock* P, MachineBasicBlock* D,
274 const BitTestInfo& C):
Evan Chengd08e5b42011-01-06 01:02:44 +0000275 First(F), Range(R), SValue(SV), Reg(Rg), RegVT(RgVT), Emitted(E),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000276 Parent(P), Default(D), Cases(C) { }
Anton Korobeynikov23218582008-12-23 22:25:27 +0000277 APInt First;
278 APInt Range;
Dan Gohman46510a72010-04-15 01:51:59 +0000279 const Value *SValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000280 unsigned Reg;
Evan Chengd08e5b42011-01-06 01:02:44 +0000281 EVT RegVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000282 bool Emitted;
283 MachineBasicBlock *Parent;
284 MachineBasicBlock *Default;
285 BitTestInfo Cases;
286 };
287
288public:
289 // TLI - This is information that describes the available target features we
290 // need for lowering. This indicates when operations are unavailable,
291 // implemented with a libcall, etc.
Dan Gohman55e59c12010-04-19 19:05:59 +0000292 const TargetMachine &TM;
Dan Gohmand858e902010-04-17 15:26:15 +0000293 const TargetLowering &TLI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000294 SelectionDAG &DAG;
295 const TargetData *TD;
296 AliasAnalysis *AA;
297
298 /// SwitchCases - Vector of CaseBlock structures used to communicate
299 /// SwitchInst code generation information.
300 std::vector<CaseBlock> SwitchCases;
301 /// JTCases - Vector of JumpTable structures used to communicate
302 /// SwitchInst code generation information.
303 std::vector<JumpTableBlock> JTCases;
304 /// BitTestCases - Vector of BitTestBlock structures used to communicate
305 /// SwitchInst code generation information.
306 std::vector<BitTestBlock> BitTestCases;
Evan Chengfb2e7522009-09-18 21:02:19 +0000307
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000308 // Emit PHI-node-operand constants only once even if used by multiple
309 // PHI nodes.
Dan Gohman46510a72010-04-15 01:51:59 +0000310 DenseMap<const Constant *, unsigned> ConstantsOut;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000311
312 /// FuncInfo - Information about the function as a whole.
313 ///
314 FunctionLoweringInfo &FuncInfo;
Bill Wendlingdfdacee2009-02-19 21:12:54 +0000315
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000316 /// OptLevel - What optimization level we're generating code for.
Bill Wendlingdfdacee2009-02-19 21:12:54 +0000317 ///
Bill Wendling98a366d2009-04-29 23:29:43 +0000318 CodeGenOpt::Level OptLevel;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000319
320 /// GFI - Garbage collection metadata for the function.
321 GCFunctionInfo *GFI;
322
Bill Wendlinga8512ed2011-10-04 22:00:35 +0000323 /// LPadToBeginLabelMap - Map a landing pad to the begin label.
324 DenseMap<MachineBasicBlock*, unsigned> LPadToCallSiteMap;
325
Dan Gohman98ca4f22009-08-05 01:29:28 +0000326 /// HasTailCall - This is set to true if a call in the current
327 /// block has been translated as a tail call. In this case,
328 /// no subsequent DAG nodes should be created.
329 ///
330 bool HasTailCall;
331
Owen Anderson0a5372e2009-07-13 04:09:18 +0000332 LLVMContext *Context;
333
Dan Gohman55e59c12010-04-19 19:05:59 +0000334 SelectionDAGBuilder(SelectionDAG &dag, FunctionLoweringInfo &funcinfo,
Dan Gohman2048b852009-11-23 18:04:58 +0000335 CodeGenOpt::Level ol)
Dan Gohman55e59c12010-04-19 19:05:59 +0000336 : SDNodeOrder(0), TM(dag.getTarget()), TLI(dag.getTargetLoweringInfo()),
337 DAG(dag), FuncInfo(funcinfo), OptLevel(ol),
Chris Lattnera4f2bb02010-04-02 20:17:23 +0000338 HasTailCall(false), Context(dag.getContext()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000339 }
340
341 void init(GCFunctionInfo *gfi, AliasAnalysis &aa);
342
Dan Gohmanb02b62a2010-04-14 18:24:06 +0000343 /// clear - Clear out the current SelectionDAG and the associated
Dan Gohman2048b852009-11-23 18:04:58 +0000344 /// state and prepare this SelectionDAGBuilder object to be used
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000345 /// for a new block. This doesn't clear out information about
346 /// additional blocks that are needed to complete switch lowering
347 /// or PHI node updating; that information is cleared out as it is
348 /// consumed.
349 void clear();
350
Devang Patel23385752011-05-23 17:44:13 +0000351 /// clearDanglingDebugInfo - Clear the dangling debug information
352 /// map. This function is seperated from the clear so that debug
353 /// information that is dangling in a basic block can be properly
354 /// resolved in a different basic block. This allows the
355 /// SelectionDAG to resolve dangling debug information attached
356 /// to PHI nodes.
357 void clearDanglingDebugInfo();
358
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000359 /// getRoot - Return the current virtual root of the Selection DAG,
360 /// flushing any PendingLoad items. This must be done before emitting
361 /// a store or any other node that may need to be ordered after any
362 /// prior load instructions.
363 ///
364 SDValue getRoot();
365
366 /// getControlRoot - Similar to getRoot, but instead of flushing all the
367 /// PendingLoad items, flush all the PendingExports items. It is necessary
368 /// to do this before emitting a terminator instruction.
369 ///
370 SDValue getControlRoot();
371
Dale Johannesen66978ee2009-01-31 02:22:37 +0000372 DebugLoc getCurDebugLoc() const { return CurDebugLoc; }
Devang Patel68e6bee2011-02-21 23:21:26 +0000373
Bill Wendling3ea3c242009-12-22 02:10:19 +0000374 unsigned getSDNodeOrder() const { return SDNodeOrder; }
375
Dan Gohman46510a72010-04-15 01:51:59 +0000376 void CopyValueToVirtualRegister(const Value *V, unsigned Reg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000377
Bill Wendling4533cac2010-01-28 21:51:40 +0000378 /// AssignOrderingToNode - Assign an ordering to the node. The order is gotten
379 /// from how the code appeared in the source. The ordering is used by the
380 /// scheduler to effectively turn off scheduling.
381 void AssignOrderingToNode(const SDNode *Node);
382
Dan Gohman46510a72010-04-15 01:51:59 +0000383 void visit(const Instruction &I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000384
Dan Gohman46510a72010-04-15 01:51:59 +0000385 void visit(unsigned Opcode, const User &I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000386
Dale Johannesenbdc09d92010-07-16 00:02:08 +0000387 // resolveDanglingDebugInfo - if we saw an earlier dbg_value referring to V,
388 // generate the debug data structures now that we've seen its definition.
389 void resolveDanglingDebugInfo(const Value *V, SDValue Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000390 SDValue getValue(const Value *V);
Dan Gohman28a17352010-07-01 01:59:43 +0000391 SDValue getNonRegisterValue(const Value *V);
392 SDValue getValueImpl(const Value *V);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000393
394 void setValue(const Value *V, SDValue NewN) {
395 SDValue &N = NodeMap[V];
396 assert(N.getNode() == 0 && "Already set a value for this node!");
397 N = NewN;
398 }
399
Devang Patel9126c0d2010-06-01 19:59:01 +0000400 void setUnusedArgValue(const Value *V, SDValue NewN) {
401 SDValue &N = UnusedArgNodeMap[V];
402 assert(N.getNode() == 0 && "Already set a value for this node!");
403 N = NewN;
404 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000405
Dan Gohman46510a72010-04-15 01:51:59 +0000406 void FindMergedConditions(const Value *Cond, MachineBasicBlock *TBB,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000407 MachineBasicBlock *FBB, MachineBasicBlock *CurBB,
Dan Gohman99be8ae2010-04-19 22:41:47 +0000408 MachineBasicBlock *SwitchBB, unsigned Opc);
Dan Gohman46510a72010-04-15 01:51:59 +0000409 void EmitBranchForMergedCondition(const Value *Cond, MachineBasicBlock *TBB,
Dan Gohmanc2277342008-10-17 21:16:08 +0000410 MachineBasicBlock *FBB,
Dan Gohman99be8ae2010-04-19 22:41:47 +0000411 MachineBasicBlock *CurBB,
412 MachineBasicBlock *SwitchBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000413 bool ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases);
Dan Gohman46510a72010-04-15 01:51:59 +0000414 bool isExportableFromCurrentBlock(const Value *V, const BasicBlock *FromBB);
415 void CopyToExportRegsIfNeeded(const Value *V);
416 void ExportFromCurrentBlock(const Value *V);
417 void LowerCallTo(ImmutableCallSite CS, SDValue Callee, bool IsTailCall,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000418 MachineBasicBlock *LandingPad = NULL);
419
Jakob Stoklund Olesen2622f462010-09-30 19:44:31 +0000420 /// UpdateSplitBlock - When an MBB was split during scheduling, update the
421 /// references that ned to refer to the last resulting block.
422 void UpdateSplitBlock(MachineBasicBlock *First, MachineBasicBlock *Last);
423
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000424private:
425 // Terminator instructions.
Dan Gohman46510a72010-04-15 01:51:59 +0000426 void visitRet(const ReturnInst &I);
427 void visitBr(const BranchInst &I);
428 void visitSwitch(const SwitchInst &I);
429 void visitIndirectBr(const IndirectBrInst &I);
Bill Wendlinga60f0e72010-07-15 23:42:21 +0000430 void visitUnreachable(const UnreachableInst &I) { /* noop */ }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000431
432 // Helpers for visitSwitch
433 bool handleSmallSwitchRange(CaseRec& CR,
434 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +0000435 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +0000436 MachineBasicBlock* Default,
437 MachineBasicBlock *SwitchBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000438 bool handleJTSwitchCase(CaseRec& CR,
439 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +0000440 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +0000441 MachineBasicBlock* Default,
442 MachineBasicBlock *SwitchBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000443 bool handleBTSplitSwitchCase(CaseRec& CR,
444 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +0000445 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +0000446 MachineBasicBlock* Default,
447 MachineBasicBlock *SwitchBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000448 bool handleBitTestsSwitchCase(CaseRec& CR,
449 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +0000450 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +0000451 MachineBasicBlock* Default,
452 MachineBasicBlock *SwitchBB);
Jakub Staszak7cc2b072011-06-16 20:22:37 +0000453
454 uint32_t getEdgeWeight(MachineBasicBlock *Src, MachineBasicBlock *Dst);
Jakub Staszakc8f34de2011-07-29 22:25:21 +0000455 void addSuccessorWithWeight(MachineBasicBlock *Src, MachineBasicBlock *Dst,
456 uint32_t Weight = 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000457public:
Dan Gohman99be8ae2010-04-19 22:41:47 +0000458 void visitSwitchCase(CaseBlock &CB,
459 MachineBasicBlock *SwitchBB);
460 void visitBitTestHeader(BitTestBlock &B, MachineBasicBlock *SwitchBB);
Evan Chengd08e5b42011-01-06 01:02:44 +0000461 void visitBitTestCase(BitTestBlock &BB,
462 MachineBasicBlock* NextMBB,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000463 unsigned Reg,
Dan Gohman99be8ae2010-04-19 22:41:47 +0000464 BitTestCase &B,
465 MachineBasicBlock *SwitchBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000466 void visitJumpTable(JumpTable &JT);
Dan Gohman99be8ae2010-04-19 22:41:47 +0000467 void visitJumpTableHeader(JumpTable &JT, JumpTableHeader &JTH,
468 MachineBasicBlock *SwitchBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000469
470private:
471 // These all get lowered before this pass.
Dan Gohman46510a72010-04-15 01:51:59 +0000472 void visitInvoke(const InvokeInst &I);
Bill Wendlingdccc03b2011-07-31 06:30:59 +0000473 void visitResume(const ResumeInst &I);
Dan Gohman46510a72010-04-15 01:51:59 +0000474 void visitUnwind(const UnwindInst &I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000475
Dan Gohman46510a72010-04-15 01:51:59 +0000476 void visitBinary(const User &I, unsigned OpCode);
477 void visitShift(const User &I, unsigned Opcode);
478 void visitAdd(const User &I) { visitBinary(I, ISD::ADD); }
479 void visitFAdd(const User &I) { visitBinary(I, ISD::FADD); }
480 void visitSub(const User &I) { visitBinary(I, ISD::SUB); }
481 void visitFSub(const User &I);
482 void visitMul(const User &I) { visitBinary(I, ISD::MUL); }
483 void visitFMul(const User &I) { visitBinary(I, ISD::FMUL); }
484 void visitURem(const User &I) { visitBinary(I, ISD::UREM); }
485 void visitSRem(const User &I) { visitBinary(I, ISD::SREM); }
486 void visitFRem(const User &I) { visitBinary(I, ISD::FREM); }
487 void visitUDiv(const User &I) { visitBinary(I, ISD::UDIV); }
Benjamin Kramer9c640302011-07-08 10:31:30 +0000488 void visitSDiv(const User &I);
Dan Gohman46510a72010-04-15 01:51:59 +0000489 void visitFDiv(const User &I) { visitBinary(I, ISD::FDIV); }
490 void visitAnd (const User &I) { visitBinary(I, ISD::AND); }
491 void visitOr (const User &I) { visitBinary(I, ISD::OR); }
492 void visitXor (const User &I) { visitBinary(I, ISD::XOR); }
493 void visitShl (const User &I) { visitShift(I, ISD::SHL); }
494 void visitLShr(const User &I) { visitShift(I, ISD::SRL); }
495 void visitAShr(const User &I) { visitShift(I, ISD::SRA); }
496 void visitICmp(const User &I);
497 void visitFCmp(const User &I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000498 // Visit the conversion instructions
Dan Gohman46510a72010-04-15 01:51:59 +0000499 void visitTrunc(const User &I);
500 void visitZExt(const User &I);
501 void visitSExt(const User &I);
502 void visitFPTrunc(const User &I);
503 void visitFPExt(const User &I);
504 void visitFPToUI(const User &I);
505 void visitFPToSI(const User &I);
506 void visitUIToFP(const User &I);
507 void visitSIToFP(const User &I);
508 void visitPtrToInt(const User &I);
509 void visitIntToPtr(const User &I);
510 void visitBitCast(const User &I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000511
Dan Gohman46510a72010-04-15 01:51:59 +0000512 void visitExtractElement(const User &I);
513 void visitInsertElement(const User &I);
514 void visitShuffleVector(const User &I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000515
Dan Gohman46510a72010-04-15 01:51:59 +0000516 void visitExtractValue(const ExtractValueInst &I);
517 void visitInsertValue(const InsertValueInst &I);
Bill Wendlinge6e88262011-08-12 20:24:12 +0000518 void visitLandingPad(const LandingPadInst &I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000519
Dan Gohman46510a72010-04-15 01:51:59 +0000520 void visitGetElementPtr(const User &I);
521 void visitSelect(const User &I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000522
Dan Gohman46510a72010-04-15 01:51:59 +0000523 void visitAlloca(const AllocaInst &I);
524 void visitLoad(const LoadInst &I);
525 void visitStore(const StoreInst &I);
Eli Friedmanff030482011-07-28 21:48:00 +0000526 void visitAtomicCmpXchg(const AtomicCmpXchgInst &I);
527 void visitAtomicRMW(const AtomicRMWInst &I);
Eli Friedman47f35132011-07-25 23:16:38 +0000528 void visitFence(const FenceInst &I);
Dan Gohmanba5be5c2010-04-20 15:00:41 +0000529 void visitPHI(const PHINode &I);
Dan Gohman46510a72010-04-15 01:51:59 +0000530 void visitCall(const CallInst &I);
531 bool visitMemCmpCall(const CallInst &I);
Eli Friedman327236c2011-08-24 20:50:09 +0000532 void visitAtomicLoad(const LoadInst &I);
533 void visitAtomicStore(const StoreInst &I);
534
Dan Gohman46510a72010-04-15 01:51:59 +0000535 void visitInlineAsm(ImmutableCallSite CS);
536 const char *visitIntrinsicCall(const CallInst &I, unsigned Intrinsic);
537 void visitTargetIntrinsic(const CallInst &I, unsigned Intrinsic);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000538
Dan Gohman46510a72010-04-15 01:51:59 +0000539 void visitPow(const CallInst &I);
540 void visitExp2(const CallInst &I);
541 void visitExp(const CallInst &I);
542 void visitLog(const CallInst &I);
543 void visitLog2(const CallInst &I);
544 void visitLog10(const CallInst &I);
Dale Johannesen601d3c02008-09-05 01:48:15 +0000545
Dan Gohman46510a72010-04-15 01:51:59 +0000546 void visitVAStart(const CallInst &I);
547 void visitVAArg(const VAArgInst &I);
548 void visitVAEnd(const CallInst &I);
549 void visitVACopy(const CallInst &I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000550
Dan Gohman46510a72010-04-15 01:51:59 +0000551 void visitUserOp1(const Instruction &I) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000552 llvm_unreachable("UserOp1 should not exist at instruction selection time!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000553 }
Dan Gohman46510a72010-04-15 01:51:59 +0000554 void visitUserOp2(const Instruction &I) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000555 llvm_unreachable("UserOp2 should not exist at instruction selection time!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000556 }
557
Dan Gohman46510a72010-04-15 01:51:59 +0000558 const char *implVisitBinaryAtomic(const CallInst& I, ISD::NodeType Op);
559 const char *implVisitAluOverflow(const CallInst &I, ISD::NodeType Op);
Dan Gohmanc105a2b2010-04-22 20:55:53 +0000560
561 void HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB);
Evan Cheng2ad0fcf2010-04-28 23:08:54 +0000562
Devang Patelab43add2010-08-25 20:41:24 +0000563 /// EmitFuncArgumentDbgValue - If V is an function argument then create
564 /// corresponding DBG_VALUE machine instruction for it now. At the end of
565 /// instruction selection, they will be inserted to the entry BB.
Devang Patel78a06e52010-08-25 20:39:26 +0000566 bool EmitFuncArgumentDbgValue(const Value *V, MDNode *Variable,
Devang Patel34ca5ed2010-08-31 06:12:08 +0000567 int64_t Offset, const SDValue &N);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000568};
569
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000570} // end namespace llvm
571
572#endif