We're not going to spend 100% of time in interrupts, do we? :)
[llvm/msp430.git] / lib / CodeGen / SelectionDAG / SelectionDAGBuild.h
blob578aa591ce67f2941b306c04204a9dc9baa8a1cb
1 //===-- SelectionDAGBuild.h - Selection-DAG building ----------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This implements routines for translating from LLVM IR into SelectionDAG IR.
12 //===----------------------------------------------------------------------===//
14 #ifndef SELECTIONDAGBUILD_H
15 #define SELECTIONDAGBUILD_H
17 #include "llvm/Constants.h"
18 #include "llvm/ADT/APInt.h"
19 #include "llvm/ADT/DenseMap.h"
20 #ifndef NDEBUG
21 #include "llvm/ADT/SmallSet.h"
22 #endif
23 #include "llvm/CodeGen/SelectionDAGNodes.h"
24 #include "llvm/CodeGen/ValueTypes.h"
25 #include "llvm/Support/CallSite.h"
26 #include "llvm/Target/TargetMachine.h"
27 #include <vector>
28 #include <set>
30 namespace llvm {
32 class AliasAnalysis;
33 class AllocaInst;
34 class BasicBlock;
35 class BitCastInst;
36 class BranchInst;
37 class CallInst;
38 class ExtractElementInst;
39 class ExtractValueInst;
40 class FCmpInst;
41 class FPExtInst;
42 class FPToSIInst;
43 class FPToUIInst;
44 class FPTruncInst;
45 class FreeInst;
46 class Function;
47 class GetElementPtrInst;
48 class GCFunctionInfo;
49 class ICmpInst;
50 class IntToPtrInst;
51 class InvokeInst;
52 class InsertElementInst;
53 class InsertValueInst;
54 class Instruction;
55 class LoadInst;
56 class MachineBasicBlock;
57 class MachineFunction;
58 class MachineInstr;
59 class MachineModuleInfo;
60 class MachineRegisterInfo;
61 class MallocInst;
62 class PHINode;
63 class PtrToIntInst;
64 class ReturnInst;
65 class SDISelAsmOperandInfo;
66 class SExtInst;
67 class SelectInst;
68 class ShuffleVectorInst;
69 class SIToFPInst;
70 class StoreInst;
71 class SwitchInst;
72 class TargetData;
73 class TargetLowering;
74 class TruncInst;
75 class UIToFPInst;
76 class UnreachableInst;
77 class UnwindInst;
78 class VICmpInst;
79 class VFCmpInst;
80 class VAArgInst;
81 class ZExtInst;
83 //===--------------------------------------------------------------------===//
84 /// FunctionLoweringInfo - This contains information that is global to a
85 /// function that is used when lowering a region of the function.
86 ///
87 class FunctionLoweringInfo {
88 public:
89 TargetLowering &TLI;
90 Function *Fn;
91 MachineFunction *MF;
92 MachineRegisterInfo *RegInfo;
94 explicit FunctionLoweringInfo(TargetLowering &TLI);
96 /// set - Initialize this FunctionLoweringInfo with the given Function
97 /// and its associated MachineFunction.
98 ///
99 void set(Function &Fn, MachineFunction &MF, SelectionDAG &DAG,
100 bool EnableFastISel);
102 /// MBBMap - A mapping from LLVM basic blocks to their machine code entry.
103 DenseMap<const BasicBlock*, MachineBasicBlock *> MBBMap;
105 /// ValueMap - Since we emit code for the function a basic block at a time,
106 /// we must remember which virtual registers hold the values for
107 /// cross-basic-block values.
108 DenseMap<const Value*, unsigned> ValueMap;
110 /// StaticAllocaMap - Keep track of frame indices for fixed sized allocas in
111 /// the entry block. This allows the allocas to be efficiently referenced
112 /// anywhere in the function.
113 DenseMap<const AllocaInst*, int> StaticAllocaMap;
115 #ifndef NDEBUG
116 SmallSet<Instruction*, 8> CatchInfoLost;
117 SmallSet<Instruction*, 8> CatchInfoFound;
118 #endif
120 unsigned MakeReg(MVT VT);
122 /// isExportedInst - Return true if the specified value is an instruction
123 /// exported from its block.
124 bool isExportedInst(const Value *V) {
125 return ValueMap.count(V);
128 unsigned CreateRegForValue(const Value *V);
130 unsigned InitializeRegForValue(const Value *V) {
131 unsigned &R = ValueMap[V];
132 assert(R == 0 && "Already initialized this value register!");
133 return R = CreateRegForValue(V);
136 struct LiveOutInfo {
137 unsigned NumSignBits;
138 APInt KnownOne, KnownZero;
139 LiveOutInfo() : NumSignBits(0), KnownOne(1, 0), KnownZero(1, 0) {}
142 /// LiveOutRegInfo - Information about live out vregs, indexed by their
143 /// register number offset by 'FirstVirtualRegister'.
144 std::vector<LiveOutInfo> LiveOutRegInfo;
146 /// clear - Clear out all the function-specific state. This returns this
147 /// FunctionLoweringInfo to an empty state, ready to be used for a
148 /// different function.
149 void clear() {
150 MBBMap.clear();
151 ValueMap.clear();
152 StaticAllocaMap.clear();
153 #ifndef NDEBUG
154 CatchInfoLost.clear();
155 CatchInfoFound.clear();
156 #endif
157 LiveOutRegInfo.clear();
161 //===----------------------------------------------------------------------===//
162 /// SelectionDAGLowering - This is the common target-independent lowering
163 /// implementation that is parameterized by a TargetLowering object.
164 /// Also, targets can overload any lowering method.
166 class SelectionDAGLowering {
167 MachineBasicBlock *CurMBB;
169 /// CurDebugLoc - current file + line number. Changes as we build the DAG.
170 DebugLoc CurDebugLoc;
172 DenseMap<const Value*, SDValue> NodeMap;
174 /// PendingLoads - Loads are not emitted to the program immediately. We bunch
175 /// them up and then emit token factor nodes when possible. This allows us to
176 /// get simple disambiguation between loads without worrying about alias
177 /// analysis.
178 SmallVector<SDValue, 8> PendingLoads;
180 /// PendingExports - CopyToReg nodes that copy values to virtual registers
181 /// for export to other blocks need to be emitted before any terminator
182 /// instruction, but they have no other ordering requirements. We bunch them
183 /// up and the emit a single tokenfactor for them just before terminator
184 /// instructions.
185 SmallVector<SDValue, 8> PendingExports;
187 /// Case - A struct to record the Value for a switch case, and the
188 /// case's target basic block.
189 struct Case {
190 Constant* Low;
191 Constant* High;
192 MachineBasicBlock* BB;
194 Case() : Low(0), High(0), BB(0) { }
195 Case(Constant* low, Constant* high, MachineBasicBlock* bb) :
196 Low(low), High(high), BB(bb) { }
197 uint64_t size() const {
198 uint64_t rHigh = cast<ConstantInt>(High)->getSExtValue();
199 uint64_t rLow = cast<ConstantInt>(Low)->getSExtValue();
200 return (rHigh - rLow + 1ULL);
204 struct CaseBits {
205 uint64_t Mask;
206 MachineBasicBlock* BB;
207 unsigned Bits;
209 CaseBits(uint64_t mask, MachineBasicBlock* bb, unsigned bits):
210 Mask(mask), BB(bb), Bits(bits) { }
213 typedef std::vector<Case> CaseVector;
214 typedef std::vector<CaseBits> CaseBitsVector;
215 typedef CaseVector::iterator CaseItr;
216 typedef std::pair<CaseItr, CaseItr> CaseRange;
218 /// CaseRec - A struct with ctor used in lowering switches to a binary tree
219 /// of conditional branches.
220 struct CaseRec {
221 CaseRec(MachineBasicBlock *bb, Constant *lt, Constant *ge, CaseRange r) :
222 CaseBB(bb), LT(lt), GE(ge), Range(r) {}
224 /// CaseBB - The MBB in which to emit the compare and branch
225 MachineBasicBlock *CaseBB;
226 /// LT, GE - If nonzero, we know the current case value must be less-than or
227 /// greater-than-or-equal-to these Constants.
228 Constant *LT;
229 Constant *GE;
230 /// Range - A pair of iterators representing the range of case values to be
231 /// processed at this point in the binary search tree.
232 CaseRange Range;
235 typedef std::vector<CaseRec> CaseRecVector;
237 /// The comparison function for sorting the switch case values in the vector.
238 /// WARNING: Case ranges should be disjoint!
239 struct CaseCmp {
240 bool operator () (const Case& C1, const Case& C2) {
241 assert(isa<ConstantInt>(C1.Low) && isa<ConstantInt>(C2.High));
242 const ConstantInt* CI1 = cast<const ConstantInt>(C1.Low);
243 const ConstantInt* CI2 = cast<const ConstantInt>(C2.High);
244 return CI1->getValue().slt(CI2->getValue());
248 struct CaseBitsCmp {
249 bool operator () (const CaseBits& C1, const CaseBits& C2) {
250 return C1.Bits > C2.Bits;
254 size_t Clusterify(CaseVector& Cases, const SwitchInst &SI);
256 /// CaseBlock - This structure is used to communicate between SDLowering and
257 /// SDISel for the code generation of additional basic blocks needed by multi-
258 /// case switch statements.
259 struct CaseBlock {
260 CaseBlock(ISD::CondCode cc, Value *cmplhs, Value *cmprhs, Value *cmpmiddle,
261 MachineBasicBlock *truebb, MachineBasicBlock *falsebb,
262 MachineBasicBlock *me)
263 : CC(cc), CmpLHS(cmplhs), CmpMHS(cmpmiddle), CmpRHS(cmprhs),
264 TrueBB(truebb), FalseBB(falsebb), ThisBB(me) {}
265 // CC - the condition code to use for the case block's setcc node
266 ISD::CondCode CC;
267 // CmpLHS/CmpRHS/CmpMHS - The LHS/MHS/RHS of the comparison to emit.
268 // Emit by default LHS op RHS. MHS is used for range comparisons:
269 // If MHS is not null: (LHS <= MHS) and (MHS <= RHS).
270 Value *CmpLHS, *CmpMHS, *CmpRHS;
271 // TrueBB/FalseBB - the block to branch to if the setcc is true/false.
272 MachineBasicBlock *TrueBB, *FalseBB;
273 // ThisBB - the block into which to emit the code for the setcc and branches
274 MachineBasicBlock *ThisBB;
276 struct JumpTable {
277 JumpTable(unsigned R, unsigned J, MachineBasicBlock *M,
278 MachineBasicBlock *D): Reg(R), JTI(J), MBB(M), Default(D) {}
280 /// Reg - the virtual register containing the index of the jump table entry
281 //. to jump to.
282 unsigned Reg;
283 /// JTI - the JumpTableIndex for this jump table in the function.
284 unsigned JTI;
285 /// MBB - the MBB into which to emit the code for the indirect jump.
286 MachineBasicBlock *MBB;
287 /// Default - the MBB of the default bb, which is a successor of the range
288 /// check MBB. This is when updating PHI nodes in successors.
289 MachineBasicBlock *Default;
291 struct JumpTableHeader {
292 JumpTableHeader(APInt F, APInt L, Value* SV, MachineBasicBlock* H,
293 bool E = false):
294 First(F), Last(L), SValue(SV), HeaderBB(H), Emitted(E) {}
295 APInt First;
296 APInt Last;
297 Value *SValue;
298 MachineBasicBlock *HeaderBB;
299 bool Emitted;
301 typedef std::pair<JumpTableHeader, JumpTable> JumpTableBlock;
303 struct BitTestCase {
304 BitTestCase(uint64_t M, MachineBasicBlock* T, MachineBasicBlock* Tr):
305 Mask(M), ThisBB(T), TargetBB(Tr) { }
306 uint64_t Mask;
307 MachineBasicBlock* ThisBB;
308 MachineBasicBlock* TargetBB;
311 typedef SmallVector<BitTestCase, 3> BitTestInfo;
313 struct BitTestBlock {
314 BitTestBlock(APInt F, APInt R, Value* SV,
315 unsigned Rg, bool E,
316 MachineBasicBlock* P, MachineBasicBlock* D,
317 const BitTestInfo& C):
318 First(F), Range(R), SValue(SV), Reg(Rg), Emitted(E),
319 Parent(P), Default(D), Cases(C) { }
320 APInt First;
321 APInt Range;
322 Value *SValue;
323 unsigned Reg;
324 bool Emitted;
325 MachineBasicBlock *Parent;
326 MachineBasicBlock *Default;
327 BitTestInfo Cases;
330 public:
331 // TLI - This is information that describes the available target features we
332 // need for lowering. This indicates when operations are unavailable,
333 // implemented with a libcall, etc.
334 TargetLowering &TLI;
335 SelectionDAG &DAG;
336 const TargetData *TD;
337 AliasAnalysis *AA;
339 /// SwitchCases - Vector of CaseBlock structures used to communicate
340 /// SwitchInst code generation information.
341 std::vector<CaseBlock> SwitchCases;
342 /// JTCases - Vector of JumpTable structures used to communicate
343 /// SwitchInst code generation information.
344 std::vector<JumpTableBlock> JTCases;
345 /// BitTestCases - Vector of BitTestBlock structures used to communicate
346 /// SwitchInst code generation information.
347 std::vector<BitTestBlock> BitTestCases;
349 std::vector<std::pair<MachineInstr*, unsigned> > PHINodesToUpdate;
351 // Emit PHI-node-operand constants only once even if used by multiple
352 // PHI nodes.
353 DenseMap<Constant*, unsigned> ConstantsOut;
355 /// FuncInfo - Information about the function as a whole.
357 FunctionLoweringInfo &FuncInfo;
359 /// OptLevel - What optimization level we're generating code for.
360 ///
361 CodeGenOpt::Level OptLevel;
363 /// GFI - Garbage collection metadata for the function.
364 GCFunctionInfo *GFI;
366 SelectionDAGLowering(SelectionDAG &dag, TargetLowering &tli,
367 FunctionLoweringInfo &funcinfo,
368 CodeGenOpt::Level ol)
369 : CurDebugLoc(DebugLoc::getUnknownLoc()),
370 TLI(tli), DAG(dag), FuncInfo(funcinfo), OptLevel(ol) {
373 void init(GCFunctionInfo *gfi, AliasAnalysis &aa);
375 /// clear - Clear out the curret SelectionDAG and the associated
376 /// state and prepare this SelectionDAGLowering object to be used
377 /// for a new block. This doesn't clear out information about
378 /// additional blocks that are needed to complete switch lowering
379 /// or PHI node updating; that information is cleared out as it is
380 /// consumed.
381 void clear();
383 /// getRoot - Return the current virtual root of the Selection DAG,
384 /// flushing any PendingLoad items. This must be done before emitting
385 /// a store or any other node that may need to be ordered after any
386 /// prior load instructions.
388 SDValue getRoot();
390 /// getControlRoot - Similar to getRoot, but instead of flushing all the
391 /// PendingLoad items, flush all the PendingExports items. It is necessary
392 /// to do this before emitting a terminator instruction.
394 SDValue getControlRoot();
396 DebugLoc getCurDebugLoc() const { return CurDebugLoc; }
397 void setCurDebugLoc(DebugLoc dl) { CurDebugLoc = dl; }
399 void CopyValueToVirtualRegister(Value *V, unsigned Reg);
401 void visit(Instruction &I);
403 void visit(unsigned Opcode, User &I);
405 void setCurrentBasicBlock(MachineBasicBlock *MBB) { CurMBB = MBB; }
407 SDValue getValue(const Value *V);
409 void setValue(const Value *V, SDValue NewN) {
410 SDValue &N = NodeMap[V];
411 assert(N.getNode() == 0 && "Already set a value for this node!");
412 N = NewN;
415 void GetRegistersForValue(SDISelAsmOperandInfo &OpInfo,
416 std::set<unsigned> &OutputRegs,
417 std::set<unsigned> &InputRegs);
419 void FindMergedConditions(Value *Cond, MachineBasicBlock *TBB,
420 MachineBasicBlock *FBB, MachineBasicBlock *CurBB,
421 unsigned Opc);
422 void EmitBranchForMergedCondition(Value *Cond, MachineBasicBlock *TBB,
423 MachineBasicBlock *FBB,
424 MachineBasicBlock *CurBB);
425 bool ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases);
426 bool isExportableFromCurrentBlock(Value *V, const BasicBlock *FromBB);
427 void CopyToExportRegsIfNeeded(Value *V);
428 void ExportFromCurrentBlock(Value *V);
429 void LowerCallTo(CallSite CS, SDValue Callee, bool IsTailCall,
430 MachineBasicBlock *LandingPad = NULL);
432 private:
433 // Terminator instructions.
434 void visitRet(ReturnInst &I);
435 void visitBr(BranchInst &I);
436 void visitSwitch(SwitchInst &I);
437 void visitUnreachable(UnreachableInst &I) { /* noop */ }
439 // Helpers for visitSwitch
440 bool handleSmallSwitchRange(CaseRec& CR,
441 CaseRecVector& WorkList,
442 Value* SV,
443 MachineBasicBlock* Default);
444 bool handleJTSwitchCase(CaseRec& CR,
445 CaseRecVector& WorkList,
446 Value* SV,
447 MachineBasicBlock* Default);
448 bool handleBTSplitSwitchCase(CaseRec& CR,
449 CaseRecVector& WorkList,
450 Value* SV,
451 MachineBasicBlock* Default);
452 bool handleBitTestsSwitchCase(CaseRec& CR,
453 CaseRecVector& WorkList,
454 Value* SV,
455 MachineBasicBlock* Default);
456 public:
457 void visitSwitchCase(CaseBlock &CB);
458 void visitBitTestHeader(BitTestBlock &B);
459 void visitBitTestCase(MachineBasicBlock* NextMBB,
460 unsigned Reg,
461 BitTestCase &B);
462 void visitJumpTable(JumpTable &JT);
463 void visitJumpTableHeader(JumpTable &JT, JumpTableHeader &JTH);
465 private:
466 // These all get lowered before this pass.
467 void visitInvoke(InvokeInst &I);
468 void visitUnwind(UnwindInst &I);
470 void visitBinary(User &I, unsigned OpCode);
471 void visitShift(User &I, unsigned Opcode);
472 void visitAdd(User &I);
473 void visitSub(User &I);
474 void visitMul(User &I);
475 void visitURem(User &I) { visitBinary(I, ISD::UREM); }
476 void visitSRem(User &I) { visitBinary(I, ISD::SREM); }
477 void visitFRem(User &I) { visitBinary(I, ISD::FREM); }
478 void visitUDiv(User &I) { visitBinary(I, ISD::UDIV); }
479 void visitSDiv(User &I) { visitBinary(I, ISD::SDIV); }
480 void visitFDiv(User &I) { visitBinary(I, ISD::FDIV); }
481 void visitAnd (User &I) { visitBinary(I, ISD::AND); }
482 void visitOr (User &I) { visitBinary(I, ISD::OR); }
483 void visitXor (User &I) { visitBinary(I, ISD::XOR); }
484 void visitShl (User &I) { visitShift(I, ISD::SHL); }
485 void visitLShr(User &I) { visitShift(I, ISD::SRL); }
486 void visitAShr(User &I) { visitShift(I, ISD::SRA); }
487 void visitICmp(User &I);
488 void visitFCmp(User &I);
489 void visitVICmp(User &I);
490 void visitVFCmp(User &I);
491 // Visit the conversion instructions
492 void visitTrunc(User &I);
493 void visitZExt(User &I);
494 void visitSExt(User &I);
495 void visitFPTrunc(User &I);
496 void visitFPExt(User &I);
497 void visitFPToUI(User &I);
498 void visitFPToSI(User &I);
499 void visitUIToFP(User &I);
500 void visitSIToFP(User &I);
501 void visitPtrToInt(User &I);
502 void visitIntToPtr(User &I);
503 void visitBitCast(User &I);
505 void visitExtractElement(User &I);
506 void visitInsertElement(User &I);
507 void visitShuffleVector(User &I);
509 void visitExtractValue(ExtractValueInst &I);
510 void visitInsertValue(InsertValueInst &I);
512 void visitGetElementPtr(User &I);
513 void visitSelect(User &I);
515 void visitMalloc(MallocInst &I);
516 void visitFree(FreeInst &I);
517 void visitAlloca(AllocaInst &I);
518 void visitLoad(LoadInst &I);
519 void visitStore(StoreInst &I);
520 void visitPHI(PHINode &I) { } // PHI nodes are handled specially.
521 void visitCall(CallInst &I);
522 void visitInlineAsm(CallSite CS);
523 const char *visitIntrinsicCall(CallInst &I, unsigned Intrinsic);
524 void visitTargetIntrinsic(CallInst &I, unsigned Intrinsic);
526 void visitPow(CallInst &I);
527 void visitExp2(CallInst &I);
528 void visitExp(CallInst &I);
529 void visitLog(CallInst &I);
530 void visitLog2(CallInst &I);
531 void visitLog10(CallInst &I);
533 void visitVAStart(CallInst &I);
534 void visitVAArg(VAArgInst &I);
535 void visitVAEnd(CallInst &I);
536 void visitVACopy(CallInst &I);
538 void visitUserOp1(Instruction &I) {
539 assert(0 && "UserOp1 should not exist at instruction selection time!");
540 abort();
542 void visitUserOp2(Instruction &I) {
543 assert(0 && "UserOp2 should not exist at instruction selection time!");
544 abort();
547 const char *implVisitBinaryAtomic(CallInst& I, ISD::NodeType Op);
548 const char *implVisitAluOverflow(CallInst &I, ISD::NodeType Op);
551 /// AddCatchInfo - Extract the personality and type infos from an eh.selector
552 /// call, and add them to the specified machine basic block.
553 void AddCatchInfo(CallInst &I, MachineModuleInfo *MMI,
554 MachineBasicBlock *MBB);
556 } // end namespace llvm
558 #endif